Lines Matching +full:reset +full:- +full:val

1 /* bmm150.c - Driver for Bosch BMM150 Geomagnetic Sensor */
7 * SPDX-License-Identifier: Apache-2.0
40 const struct bmm150_config *cfg = dev->config; in bmm150_bus_check()
42 return cfg->bus_io->check(&cfg->bus); in bmm150_bus_check()
48 const struct bmm150_config *cfg = dev->config; in bmm150_reg_read()
50 return cfg->bus_io->read(&cfg->bus, start, buf, size); in bmm150_reg_read()
54 uint8_t val) in bmm150_reg_write() argument
56 const struct bmm150_config *cfg = dev->config; in bmm150_reg_write()
58 return cfg->bus_io->write(&cfg->bus, reg, val); in bmm150_reg_write()
100 static int bmm150_set_odr(const struct device *dev, uint8_t val) in bmm150_set_odr() argument
105 if (val <= bmm150_samp_freq_table[i].freq) { in bmm150_set_odr()
113 return -ENOTSUP; in bmm150_set_odr()
119 struct bmm150_data *data = dev->driver->data; in bmm150_read_rep_xy()
120 const struct bmm150_config *config = dev->config; in bmm150_read_rep_xy()
124 return -EIO; in bmm150_read_rep_xy()
127 data->rep_xy = BMM150_REGVAL_TO_REPXY((uint8_t)(reg_val)); in bmm150_read_rep_xy()
134 struct bmm150_data *data = dev->data; in bmm150_read_rep_z()
135 const struct bmm150_config *config = dev->config; in bmm150_read_rep_z()
139 return -EIO; in bmm150_read_rep_z()
142 data->rep_z = BMM150_REGVAL_TO_REPZ((int)(reg_val)); in bmm150_read_rep_z()
150 struct bmm150_data *data = dev->data; in bmm150_compute_max_odr()
153 if (data->rep_xy <= 0) { in bmm150_compute_max_odr()
155 return -EIO; in bmm150_compute_max_odr()
158 rep_xy = data->rep_xy; in bmm150_compute_max_odr()
162 if (data->rep_z <= 0) { in bmm150_compute_max_odr()
164 return -EIO; in bmm150_compute_max_odr()
167 rep_z = data->rep_z; in bmm150_compute_max_odr()
180 struct bmm150_data *data = dev->data; in bmm150_read_odr()
181 const struct bmm150_config *config = dev->config; in bmm150_read_odr()
185 return -EIO; in bmm150_read_odr()
192 data->odr = bmm150_samp_freq_table[i].freq; in bmm150_read_odr()
197 return -ENOTSUP; in bmm150_read_odr()
202 static int bmm150_write_rep_xy(const struct device *dev, int val) in bmm150_write_rep_xy() argument
204 struct bmm150_data *data = dev->data; in bmm150_write_rep_xy()
205 const struct bmm150_config *config = dev->config; in bmm150_write_rep_xy()
210 BMM150_REPXY_TO_REGVAL(val)) < 0) { in bmm150_write_rep_xy()
211 return -EIO; in bmm150_write_rep_xy()
214 data->rep_xy = val; in bmm150_write_rep_xy()
221 static int bmm150_write_rep_z(const struct device *dev, int val) in bmm150_write_rep_z() argument
223 struct bmm150_data *data = dev->data; in bmm150_write_rep_z()
224 const struct bmm150_config *config = dev->config; in bmm150_write_rep_z()
229 BMM150_REPZ_TO_REGVAL(val)) < 0) { in bmm150_write_rep_z()
230 return -EIO; in bmm150_write_rep_z()
233 data->rep_z = val; in bmm150_write_rep_z()
244 int16_t val; in bmm150_compensate_xy() local
253 rhall = tregs->xyz1; in bmm150_compensate_xy()
257 txy1 = tregs->x1; in bmm150_compensate_xy()
258 txy2 = tregs->x2; in bmm150_compensate_xy()
260 txy1 = tregs->y1; in bmm150_compensate_xy()
261 txy2 = tregs->y2; in bmm150_compensate_xy()
264 prevalue = (uint16_t)((((int32_t)tregs->xyz1) << 14) / rhall); in bmm150_compensate_xy()
266 val = (int16_t)((prevalue) - ((uint16_t)0x4000)); in bmm150_compensate_xy()
268 temp1 = (((int32_t)tregs->xy2) * ((((int32_t)val) * ((int32_t)val)) >> 7)); in bmm150_compensate_xy()
270 temp2 = ((int32_t)val) * ((int32_t)(((int16_t)tregs->xy1) << 7)); in bmm150_compensate_xy()
276 val = ((int16_t)((((int32_t)xy) * temp3) >> 13)) + (((int16_t)txy1) << 3); in bmm150_compensate_xy()
278 return (int32_t)val; in bmm150_compensate_xy()
284 int32_t val, temp1, temp2; in bmm150_compensate_z() local
291 temp1 = (((int32_t)(z - tregs->z4)) << 15); in bmm150_compensate_z()
293 temp2 = ((((int32_t)tregs->z3) * in bmm150_compensate_z()
294 ((int32_t)(((int16_t)rhall) - ((int16_t)tregs->xyz1)))) >> 2); in bmm150_compensate_z()
296 temp3 = ((int16_t)(((((int32_t)tregs->z1) * in bmm150_compensate_z()
299 val = ((temp1 - temp2) / (tregs->z2 + temp3)); in bmm150_compensate_z()
301 return val; in bmm150_compensate_z()
308 struct bmm150_data *drv_data = dev->data; in bmm150_sample_fetch()
318 return -EIO; in bmm150_sample_fetch()
331 drv_data->sample_x = bmm150_compensate_xy(&drv_data->tregs, in bmm150_sample_fetch()
333 drv_data->sample_y = bmm150_compensate_xy(&drv_data->tregs, in bmm150_sample_fetch()
335 drv_data->sample_z = bmm150_compensate_z(&drv_data->tregs, in bmm150_sample_fetch()
345 static void bmm150_convert(struct sensor_value *val, int raw_val) in bmm150_convert() argument
347 /* val = raw_val / 1600 */ in bmm150_convert()
348 val->val1 = raw_val / 1600; in bmm150_convert()
349 val->val2 = ((int32_t)raw_val * (1000000 / 1600)) % 1000000; in bmm150_convert()
354 struct sensor_value *val) in bmm150_channel_get() argument
356 struct bmm150_data *drv_data = dev->data; in bmm150_channel_get()
360 bmm150_convert(val, drv_data->sample_x); in bmm150_channel_get()
363 bmm150_convert(val, drv_data->sample_y); in bmm150_channel_get()
366 bmm150_convert(val, drv_data->sample_z); in bmm150_channel_get()
369 bmm150_convert(val, drv_data->sample_x); in bmm150_channel_get()
370 bmm150_convert(val + 1, drv_data->sample_y); in bmm150_channel_get()
371 bmm150_convert(val + 2, drv_data->sample_z); in bmm150_channel_get()
374 return -ENOTSUP; in bmm150_channel_get()
383 const struct sensor_value *val) in bmm150_attr_set_rep() argument
385 struct bmm150_data *data = dev->data; in bmm150_attr_set_rep()
392 if (val->val1 < 1 || val->val1 > 511) { in bmm150_attr_set_rep()
393 return -EINVAL; in bmm150_attr_set_rep()
396 if (bmm150_compute_max_odr(dev, val->val1, 0, in bmm150_attr_set_rep()
398 return -EIO; in bmm150_attr_set_rep()
401 if (data->odr <= 0) { in bmm150_attr_set_rep()
403 return -EIO; in bmm150_attr_set_rep()
407 if (data->odr > max_odr) { in bmm150_attr_set_rep()
408 return -EINVAL; in bmm150_attr_set_rep()
411 if (bmm150_write_rep_xy(dev, val->val1) < 0) { in bmm150_attr_set_rep()
412 return -EIO; in bmm150_attr_set_rep()
419 if (val->val1 < 1 || val->val1 > 256) { in bmm150_attr_set_rep()
420 return -EINVAL; in bmm150_attr_set_rep()
423 if (bmm150_compute_max_odr(dev, 0, val->val1, in bmm150_attr_set_rep()
425 return -EIO; in bmm150_attr_set_rep()
428 if (data->odr <= 0) { in bmm150_attr_set_rep()
430 return -EIO; in bmm150_attr_set_rep()
434 if (data->odr > max_odr) { in bmm150_attr_set_rep()
435 return -EINVAL; in bmm150_attr_set_rep()
438 if (bmm150_write_rep_z(dev, val->val1) < 0) { in bmm150_attr_set_rep()
439 return -EIO; in bmm150_attr_set_rep()
444 return -EINVAL; in bmm150_attr_set_rep()
455 const struct sensor_value *val) in bmm150_attr_set() argument
457 struct bmm150_magn_data *data = dev->data; in bmm150_attr_set()
462 if (data->max_odr <= 0) { in bmm150_attr_set()
464 &data->max_odr) < 0) { in bmm150_attr_set()
465 return -EIO; in bmm150_attr_set()
469 if (data->max_odr < val->val1) { in bmm150_attr_set()
471 return -ENOTSUP; in bmm150_attr_set()
474 if (bmm150_set_odr(dev, (uint8_t)(val->val1)) < 0) { in bmm150_attr_set()
475 return -EIO; in bmm150_attr_set()
481 bmm150_attr_set_rep(dev, chan, val); in bmm150_attr_set()
485 return -EINVAL; in bmm150_attr_set()
508 /* Ensure we are not in suspend mode so soft reset is not ignored */ in bmm150_full_por()
517 /* Soft reset always brings the device into sleep mode */ in bmm150_full_por()
522 LOG_ERR("failed soft reset: %d", ret); in bmm150_full_por()
527 * To perform full POR (after soft reset), bring the device into suspend in bmm150_full_por()
538 /* Full POR - back into sleep mode */ in bmm150_full_por()
552 struct bmm150_data *data = dev->data; in bmm150_init_chip()
555 int ret = -EIO; in bmm150_init_chip()
599 if (bmm150_reg_read(dev, BMM150_REG_TRIM_START, (uint8_t *)&data->tregs, in bmm150_init_chip()
600 sizeof(data->tregs)) < 0) { in bmm150_init_chip()
605 data->rep_xy = 0; in bmm150_init_chip()
606 data->rep_z = 0; in bmm150_init_chip()
607 data->odr = 0; in bmm150_init_chip()
608 data->max_odr = 0; in bmm150_init_chip()
609 data->sample_x = 0; in bmm150_init_chip()
610 data->sample_y = 0; in bmm150_init_chip()
611 data->sample_z = 0; in bmm150_init_chip()
613 data->tregs.xyz1 = sys_le16_to_cpu(data->tregs.xyz1); in bmm150_init_chip()
614 data->tregs.z1 = sys_le16_to_cpu(data->tregs.z1); in bmm150_init_chip()
615 data->tregs.z2 = sys_le16_to_cpu(data->tregs.z2); in bmm150_init_chip()
616 data->tregs.z3 = sys_le16_to_cpu(data->tregs.z3); in bmm150_init_chip()
617 data->tregs.z4 = sys_le16_to_cpu(data->tregs.z4); in bmm150_init_chip()
672 return -ENOTSUP; in pm_action()
691 return -EINVAL; in bmm150_init()
721 * Main instantiation macro, which selects the correct bus-specific