int bma023_write_ee(unsigned char addr, unsigned char data) { int comres; trace_in() ; if (p_bma023==0) { /* check pointers */ trace_out(); return E_bma023_NULL_PTR; } if (p_bma023->delay_msec == 0) trace_out(); return E_bma023_NULL_PTR; comres = bma023_set_ee_w( bma023_EE_W_ON ); addr|=0x20; /* add eeprom address offset to image address if not applied */ comres += bma023_write_reg(addr, &data, 1 ); p_bma023->delay_msec( bma023_EE_W_DELAY ); comres += bma023_set_ee_w( bma023_EE_W_OFF); trace_out(); return comres; }
int bma023_calibrate(struct bma023_data *bma023, struct acceleration orientation, int *tries) { struct acceleration min, max, avg; unsigned short offset_x, offset_y, offset_z; unsigned short old_offset_x, old_offset_y, old_offset_z; int need_calibration = 0, min_max_ok = 0; int ltries; int retry = 30; int on; on = bma023_get_enable(&bma023->client->dev); if (!on) bma023_set_enable(&bma023->client->dev, 1); bma023_update_bits(bma023, BMA023_RANGE, BMA023_RANGE_2G); bma023_update_bits(bma023, BMA023_BANDWIDTH, BMA023_BANDWIDTH_25HZ); bma023_set_ee_w(bma023, 1); bma023_get_offset(bma023, 0, &offset_x); bma023_get_offset(bma023, 1, &offset_y); bma023_get_offset(bma023, 2, &offset_z); old_offset_x = offset_x; old_offset_y = offset_y; old_offset_z = offset_z; ltries = *tries; do { /* read acceleration data min, max, avg */ bma023_read_accel_avg(bma023, 10, &min, &max, &avg); min_max_ok = bma023_verify_min_max(min, max, avg); if (!min_max_ok) { retry--; if (retry <= 0) return -1; } /* check if calibration is needed */ if (min_max_ok) need_calibration = bma023_calc_new_offset(orientation, avg, &offset_x, &offset_y, &offset_z); if (*tries == 0) /*number of maximum tries reached? */ break; if (need_calibration) { /* when needed calibration is updated in image */ (*tries)--; bma023_set_offset(bma023, 0, offset_x); bma023_set_offset(bma023, 1, offset_y); bma023_set_offset(bma023, 2, offset_z); msleep(20); } } while (need_calibration || !min_max_ok); if (*tries > 0 && *tries < ltries) { if (old_offset_x != offset_x) bma023_set_offset_eeprom(bma023, 0, offset_x); if (old_offset_y != offset_y) bma023_set_offset_eeprom(bma023, 1, offset_y); if (old_offset_z != offset_z) bma023_set_offset_eeprom(bma023, 2, offset_z); } bma023_set_ee_w(bma023, 0); msleep(20); *tries = ltries - *tries; if (!on) bma023_set_enable(&bma023->client->dev, 0); return !need_calibration; }