Lines Matching +full:autosuspend +full:- +full:period
1 // SPDX-License-Identifier: GPL-2.0-only
3 * 3-axis accelerometer driver supporting many Bosch-Sensortec chips
26 #include "bmc150-accel.h"
212 struct device *dev = regmap_get_device(data->regmap); in bmc150_accel_set_mode()
216 int dur_val = -1; in bmc150_accel_set_mode()
231 return -EINVAL; in bmc150_accel_set_mode()
238 ret = regmap_write(data->regmap, BMC150_ACCEL_REG_PMU_LPW, lpw_bits); in bmc150_accel_set_mode()
256 ret = regmap_write(data->regmap, in bmc150_accel_set_bw()
262 data->bw_bits = in bmc150_accel_set_bw()
268 return -EINVAL; in bmc150_accel_set_bw()
273 struct device *dev = regmap_get_device(data->regmap); in bmc150_accel_update_slope()
276 ret = regmap_write(data->regmap, BMC150_ACCEL_REG_INT_6, in bmc150_accel_update_slope()
277 data->slope_thres); in bmc150_accel_update_slope()
283 ret = regmap_update_bits(data->regmap, BMC150_ACCEL_REG_INT_5, in bmc150_accel_update_slope()
284 BMC150_ACCEL_SLOPE_DUR_MASK, data->slope_dur); in bmc150_accel_update_slope()
290 dev_dbg(dev, "%x %x\n", data->slope_thres, data->slope_dur); in bmc150_accel_update_slope()
299 return bmc150_accel_update_slope(t->data); in bmc150_accel_any_motion_setup()
310 if (bmc150_accel_samp_freq_table[i].bw_bits == data->bw_bits) { in bmc150_accel_get_bw()
317 return -EINVAL; in bmc150_accel_get_bw()
326 if (bmc150_accel_sample_upd_time[i].bw_bits == data->bw_bits) in bmc150_accel_get_startup_times()
335 struct device *dev = regmap_get_device(data->regmap); in bmc150_accel_set_power_state()
364 * There are 2 variants of the BOSC0200 ACPI node. Some 2-in-1s with 360 degree
365 * hinges declare 2 I2C ACPI-resources for 2 accelerometers, 1 in the display
366 * and 1 in the base of the 2-in-1. On these 2-in-1s the ROMS ACPI object
367 * contains the mount-matrix for the sensor in the display and ROMK contains
368 * the mount-matrix for the sensor in the base. On devices using a single
369 * sensor there is a ROTM ACPI object which contains the mount-matrix.
373 * Yoga devices with 2 accelerometers using ROMS + ROMK for the mount-matrices:
377 * Tablets using a single accelerometer using ROTM for the mount-matrix:
393 if (strcmp(dev_name(dev), "i2c-BOSC0200:base") == 0) { in bmc150_apply_bosc0200_acpi_orientation()
395 label = "accel-base"; in bmc150_apply_bosc0200_acpi_orientation()
398 label = "accel-display"; in bmc150_apply_bosc0200_acpi_orientation()
405 indio_dev->label = label; in bmc150_apply_bosc0200_acpi_orientation()
418 if (strcmp(dev_name(dev), "i2c-DUAL250E:base") == 0) in bmc150_apply_dual250e_acpi_orientation()
419 indio_dev->label = "accel-base"; in bmc150_apply_dual250e_acpi_orientation()
421 indio_dev->label = "accel-display"; in bmc150_apply_dual250e_acpi_orientation()
506 struct device *dev = regmap_get_device(data->regmap); in bmc150_accel_interrupts_setup()
512 * so we try to detect which IRQ it is based on the interrupt-names. in bmc150_accel_interrupts_setup()
513 * Without interrupt-names, we assume the irq belongs to INT1. in bmc150_accel_interrupts_setup()
516 if (data->type == BOSCH_BMC156 || in bmc150_accel_interrupts_setup()
521 data->interrupts[i].info = &irq_info[i]; in bmc150_accel_interrupts_setup()
527 struct device *dev = regmap_get_device(data->regmap); in bmc150_accel_set_interrupt()
528 struct bmc150_accel_interrupt *intr = &data->interrupts[i]; in bmc150_accel_set_interrupt()
529 const struct bmc150_accel_interrupt_info *info = intr->info; in bmc150_accel_set_interrupt()
533 if (atomic_inc_return(&intr->users) > 1) in bmc150_accel_set_interrupt()
536 if (atomic_dec_return(&intr->users) > 0) in bmc150_accel_set_interrupt()
544 * by autosuspend delay. in bmc150_accel_set_interrupt()
554 ret = regmap_update_bits(data->regmap, info->map_reg, info->map_bitmask, in bmc150_accel_set_interrupt()
555 (state ? info->map_bitmask : 0)); in bmc150_accel_set_interrupt()
562 ret = regmap_update_bits(data->regmap, info->en_reg, info->en_bitmask, in bmc150_accel_set_interrupt()
563 (state ? info->en_bitmask : 0)); in bmc150_accel_set_interrupt()
578 struct device *dev = regmap_get_device(data->regmap); in bmc150_accel_set_scale()
581 for (i = 0; i < ARRAY_SIZE(data->chip_info->scale_table); ++i) { in bmc150_accel_set_scale()
582 if (data->chip_info->scale_table[i].scale == val) { in bmc150_accel_set_scale()
583 ret = regmap_write(data->regmap, in bmc150_accel_set_scale()
585 data->chip_info->scale_table[i].reg_range); in bmc150_accel_set_scale()
591 data->range = data->chip_info->scale_table[i].reg_range; in bmc150_accel_set_scale()
596 return -EINVAL; in bmc150_accel_set_scale()
601 struct device *dev = regmap_get_device(data->regmap); in bmc150_accel_get_temp()
605 mutex_lock(&data->mutex); in bmc150_accel_get_temp()
607 ret = regmap_read(data->regmap, BMC150_ACCEL_REG_TEMP, &value); in bmc150_accel_get_temp()
610 mutex_unlock(&data->mutex); in bmc150_accel_get_temp()
615 mutex_unlock(&data->mutex); in bmc150_accel_get_temp()
624 struct device *dev = regmap_get_device(data->regmap); in bmc150_accel_get_axis()
626 int axis = chan->scan_index; in bmc150_accel_get_axis()
629 mutex_lock(&data->mutex); in bmc150_accel_get_axis()
632 mutex_unlock(&data->mutex); in bmc150_accel_get_axis()
636 ret = regmap_bulk_read(data->regmap, BMC150_ACCEL_AXIS_TO_REG(axis), in bmc150_accel_get_axis()
641 mutex_unlock(&data->mutex); in bmc150_accel_get_axis()
644 *val = sign_extend32(le16_to_cpu(raw_val) >> chan->scan_type.shift, in bmc150_accel_get_axis()
645 chan->scan_type.realbits - 1); in bmc150_accel_get_axis()
647 mutex_unlock(&data->mutex); in bmc150_accel_get_axis()
663 switch (chan->type) { in bmc150_accel_read_raw()
668 return -EBUSY; in bmc150_accel_read_raw()
672 return -EINVAL; in bmc150_accel_read_raw()
675 if (chan->type == IIO_TEMP) { in bmc150_accel_read_raw()
679 return -EINVAL; in bmc150_accel_read_raw()
683 switch (chan->type) { in bmc150_accel_read_raw()
691 int st_size = ARRAY_SIZE(data->chip_info->scale_table); in bmc150_accel_read_raw()
694 si = &data->chip_info->scale_table[i]; in bmc150_accel_read_raw()
695 if (si->reg_range == data->range) { in bmc150_accel_read_raw()
696 *val2 = si->scale; in bmc150_accel_read_raw()
700 return -EINVAL; in bmc150_accel_read_raw()
703 return -EINVAL; in bmc150_accel_read_raw()
706 mutex_lock(&data->mutex); in bmc150_accel_read_raw()
708 mutex_unlock(&data->mutex); in bmc150_accel_read_raw()
711 return -EINVAL; in bmc150_accel_read_raw()
724 mutex_lock(&data->mutex); in bmc150_accel_write_raw()
726 mutex_unlock(&data->mutex); in bmc150_accel_write_raw()
730 return -EINVAL; in bmc150_accel_write_raw()
732 mutex_lock(&data->mutex); in bmc150_accel_write_raw()
734 mutex_unlock(&data->mutex); in bmc150_accel_write_raw()
737 ret = -EINVAL; in bmc150_accel_write_raw()
755 *val = data->slope_thres; in bmc150_accel_read_event()
758 *val = data->slope_dur; in bmc150_accel_read_event()
761 return -EINVAL; in bmc150_accel_read_event()
776 if (data->ev_enable_state) in bmc150_accel_write_event()
777 return -EBUSY; in bmc150_accel_write_event()
781 data->slope_thres = val & BMC150_ACCEL_SLOPE_THRES_MASK; in bmc150_accel_write_event()
784 data->slope_dur = val & BMC150_ACCEL_SLOPE_DUR_MASK; in bmc150_accel_write_event()
787 return -EINVAL; in bmc150_accel_write_event()
800 return data->ev_enable_state; in bmc150_accel_read_event_config()
812 if (state == data->ev_enable_state) in bmc150_accel_write_event_config()
815 mutex_lock(&data->mutex); in bmc150_accel_write_event_config()
820 mutex_unlock(&data->mutex); in bmc150_accel_write_event_config()
824 data->ev_enable_state = state; in bmc150_accel_write_event_config()
825 mutex_unlock(&data->mutex); in bmc150_accel_write_event_config()
837 if (data->triggers[i].indio_trig == trig) in bmc150_accel_validate_trigger()
841 return -EINVAL; in bmc150_accel_validate_trigger()
852 mutex_lock(&data->mutex); in bmc150_accel_get_fifo_watermark()
853 wm = data->watermark; in bmc150_accel_get_fifo_watermark()
854 mutex_unlock(&data->mutex); in bmc150_accel_get_fifo_watermark()
867 mutex_lock(&data->mutex); in bmc150_accel_get_fifo_state()
868 state = data->fifo_mode; in bmc150_accel_get_fifo_state()
869 mutex_unlock(&data->mutex); in bmc150_accel_get_fifo_state()
880 return &data->orientation; in bmc150_accel_get_mount_matrix()
911 mutex_lock(&data->mutex); in bmc150_accel_set_watermark()
912 data->watermark = val; in bmc150_accel_set_watermark()
913 mutex_unlock(&data->mutex); in bmc150_accel_set_watermark()
925 struct device *dev = regmap_get_device(data->regmap); in bmc150_accel_fifo_transfer()
930 ret = regmap_raw_read(data->regmap, BMC150_ACCEL_REG_FIFO_DATA, in bmc150_accel_fifo_transfer()
943 struct device *dev = regmap_get_device(data->regmap); in __bmc150_accel_fifo_flush()
951 ret = regmap_read(data->regmap, BMC150_ACCEL_REG_FIFO_STATUS, &val); in __bmc150_accel_fifo_flush()
968 * case is at most one sample period. in __bmc150_accel_fifo_flush()
971 data->old_timestamp = data->timestamp; in __bmc150_accel_fifo_flush()
972 data->timestamp = iio_get_time_ns(indio_dev); in __bmc150_accel_fifo_flush()
980 * sample period because the sample rate varies with the device in __bmc150_accel_fifo_flush()
986 * To avoid this issue we compute the actual sample period ourselves in __bmc150_accel_fifo_flush()
989 sample_period = (data->timestamp - data->old_timestamp); in __bmc150_accel_fifo_flush()
991 tstamp = data->timestamp - (count - 1) * sample_period; in __bmc150_accel_fifo_flush()
1011 memcpy(&data->scan.channels[j++], &buffer[i * 3 + bit], in __bmc150_accel_fifo_flush()
1012 sizeof(data->scan.channels[0])); in __bmc150_accel_fifo_flush()
1014 iio_push_to_buffers_with_timestamp(indio_dev, &data->scan, in __bmc150_accel_fifo_flush()
1028 mutex_lock(&data->mutex); in bmc150_accel_fifo_flush()
1030 mutex_unlock(&data->mutex); in bmc150_accel_fifo_flush()
1067 .shift = 16 - (bits), \
1081 .scan_index = -1, \
1099 * The range for the Bosch sensors is typically +-2g/4g/8g/16g, distributed
1102 * e.g. for +-2g and 12 bits: (4 / 2^12) * 9.80665 m/s^2 = 0.0095768... m/s^2
1188 struct iio_dev *indio_dev = pf->indio_dev; in bmc150_accel_trigger_handler()
1192 mutex_lock(&data->mutex); in bmc150_accel_trigger_handler()
1193 ret = regmap_bulk_read(data->regmap, BMC150_ACCEL_REG_XOUT_L, in bmc150_accel_trigger_handler()
1194 data->buffer, AXIS_MAX * 2); in bmc150_accel_trigger_handler()
1195 mutex_unlock(&data->mutex); in bmc150_accel_trigger_handler()
1199 iio_push_to_buffers_with_timestamp(indio_dev, data->buffer, in bmc150_accel_trigger_handler()
1200 pf->timestamp); in bmc150_accel_trigger_handler()
1202 iio_trigger_notify_done(indio_dev->trig); in bmc150_accel_trigger_handler()
1210 struct bmc150_accel_data *data = t->data; in bmc150_accel_trig_reen()
1211 struct device *dev = regmap_get_device(data->regmap); in bmc150_accel_trig_reen()
1215 if (t == &t->data->triggers[BMC150_ACCEL_TRIGGER_DATA_READY]) in bmc150_accel_trig_reen()
1218 mutex_lock(&data->mutex); in bmc150_accel_trig_reen()
1220 ret = regmap_write(data->regmap, BMC150_ACCEL_REG_INT_RST_LATCH, in bmc150_accel_trig_reen()
1223 mutex_unlock(&data->mutex); in bmc150_accel_trig_reen()
1232 struct bmc150_accel_data *data = t->data; in bmc150_accel_trigger_set_state()
1235 mutex_lock(&data->mutex); in bmc150_accel_trigger_set_state()
1237 if (t->enabled == state) { in bmc150_accel_trigger_set_state()
1238 mutex_unlock(&data->mutex); in bmc150_accel_trigger_set_state()
1242 if (t->setup) { in bmc150_accel_trigger_set_state()
1243 ret = t->setup(t, state); in bmc150_accel_trigger_set_state()
1245 mutex_unlock(&data->mutex); in bmc150_accel_trigger_set_state()
1250 ret = bmc150_accel_set_interrupt(data, t->intr, state); in bmc150_accel_trigger_set_state()
1252 mutex_unlock(&data->mutex); in bmc150_accel_trigger_set_state()
1256 t->enabled = state; in bmc150_accel_trigger_set_state()
1258 mutex_unlock(&data->mutex); in bmc150_accel_trigger_set_state()
1271 struct device *dev = regmap_get_device(data->regmap); in bmc150_accel_handle_roc_event()
1276 ret = regmap_read(data->regmap, BMC150_ACCEL_REG_INT_STATUS_2, &val); in bmc150_accel_handle_roc_event()
1294 data->timestamp); in bmc150_accel_handle_roc_event()
1303 data->timestamp); in bmc150_accel_handle_roc_event()
1312 data->timestamp); in bmc150_accel_handle_roc_event()
1321 struct device *dev = regmap_get_device(data->regmap); in bmc150_accel_irq_thread_handler()
1325 mutex_lock(&data->mutex); in bmc150_accel_irq_thread_handler()
1327 if (data->fifo_mode) { in bmc150_accel_irq_thread_handler()
1334 if (data->ev_enable_state) { in bmc150_accel_irq_thread_handler()
1341 ret = regmap_write(data->regmap, BMC150_ACCEL_REG_INT_RST_LATCH, in bmc150_accel_irq_thread_handler()
1352 mutex_unlock(&data->mutex); in bmc150_accel_irq_thread_handler()
1364 data->old_timestamp = data->timestamp; in bmc150_accel_irq_handler()
1365 data->timestamp = iio_get_time_ns(indio_dev); in bmc150_accel_irq_handler()
1368 if (data->triggers[i].enabled) { in bmc150_accel_irq_handler()
1369 iio_trigger_poll(data->triggers[i].indio_trig); in bmc150_accel_irq_handler()
1375 if (data->ev_enable_state || data->fifo_mode) in bmc150_accel_irq_handler()
1391 .name = "%s-dev%d",
1395 .name = "%s-any-motion-dev%d",
1405 for (i = from; i >= 0; i--) { in bmc150_accel_unregister_triggers()
1406 if (data->triggers[i].indio_trig) { in bmc150_accel_unregister_triggers()
1407 iio_trigger_unregister(data->triggers[i].indio_trig); in bmc150_accel_unregister_triggers()
1408 data->triggers[i].indio_trig = NULL; in bmc150_accel_unregister_triggers()
1416 struct device *dev = regmap_get_device(data->regmap); in bmc150_accel_triggers_setup()
1420 struct bmc150_accel_trigger *t = &data->triggers[i]; in bmc150_accel_triggers_setup()
1422 t->indio_trig = devm_iio_trigger_alloc(dev, in bmc150_accel_triggers_setup()
1424 indio_dev->name, in bmc150_accel_triggers_setup()
1426 if (!t->indio_trig) { in bmc150_accel_triggers_setup()
1427 ret = -ENOMEM; in bmc150_accel_triggers_setup()
1431 t->indio_trig->ops = &bmc150_accel_trigger_ops; in bmc150_accel_triggers_setup()
1432 t->intr = bmc150_accel_triggers[i].intr; in bmc150_accel_triggers_setup()
1433 t->data = data; in bmc150_accel_triggers_setup()
1434 t->setup = bmc150_accel_triggers[i].setup; in bmc150_accel_triggers_setup()
1435 iio_trigger_set_drvdata(t->indio_trig, t); in bmc150_accel_triggers_setup()
1437 ret = iio_trigger_register(t->indio_trig); in bmc150_accel_triggers_setup()
1443 bmc150_accel_unregister_triggers(data, i - 1); in bmc150_accel_triggers_setup()
1454 struct device *dev = regmap_get_device(data->regmap); in bmc150_accel_fifo_set_mode()
1458 ret = regmap_write(data->regmap, reg, data->fifo_mode); in bmc150_accel_fifo_set_mode()
1464 if (!data->fifo_mode) in bmc150_accel_fifo_set_mode()
1467 ret = regmap_write(data->regmap, BMC150_ACCEL_REG_FIFO_CONFIG0, in bmc150_accel_fifo_set_mode()
1468 data->watermark); in bmc150_accel_fifo_set_mode()
1490 mutex_lock(&data->mutex); in bmc150_accel_buffer_postenable()
1492 if (!data->watermark) in bmc150_accel_buffer_postenable()
1500 data->fifo_mode = BMC150_ACCEL_FIFO_MODE_FIFO; in bmc150_accel_buffer_postenable()
1504 data->fifo_mode = 0; in bmc150_accel_buffer_postenable()
1510 mutex_unlock(&data->mutex); in bmc150_accel_buffer_postenable()
1522 mutex_lock(&data->mutex); in bmc150_accel_buffer_predisable()
1524 if (!data->fifo_mode) in bmc150_accel_buffer_predisable()
1529 data->fifo_mode = 0; in bmc150_accel_buffer_predisable()
1533 mutex_unlock(&data->mutex); in bmc150_accel_buffer_predisable()
1554 struct device *dev = regmap_get_device(data->regmap); in bmc150_accel_chip_init()
1562 regmap_write(data->regmap, BMC150_ACCEL_REG_RESET, in bmc150_accel_chip_init()
1566 ret = regmap_read(data->regmap, BMC150_ACCEL_REG_CHIP_ID, &val); in bmc150_accel_chip_init()
1575 data->chip_info = &bmc150_accel_chip_info_tbl[i]; in bmc150_accel_chip_init()
1580 if (!data->chip_info) { in bmc150_accel_chip_init()
1582 return -ENODEV; in bmc150_accel_chip_init()
1595 ret = regmap_write(data->regmap, BMC150_ACCEL_REG_PMU_RANGE, in bmc150_accel_chip_init()
1602 data->range = BMC150_ACCEL_DEF_RANGE_4G; in bmc150_accel_chip_init()
1605 data->slope_thres = BMC150_ACCEL_DEF_SLOPE_THRESHOLD; in bmc150_accel_chip_init()
1606 data->slope_dur = BMC150_ACCEL_DEF_SLOPE_DURATION; in bmc150_accel_chip_init()
1612 ret = regmap_write(data->regmap, BMC150_ACCEL_REG_INT_RST_LATCH, in bmc150_accel_chip_init()
1634 return -ENOMEM; in bmc150_accel_core_probe()
1639 data->regmap = regmap; in bmc150_accel_core_probe()
1640 data->type = type; in bmc150_accel_core_probe()
1642 if (!bmc150_apply_acpi_orientation(dev, &data->orientation)) { in bmc150_accel_core_probe()
1643 ret = iio_read_mount_matrix(dev, &data->orientation); in bmc150_accel_core_probe()
1652 data->regulators[0].supply = "vdd"; in bmc150_accel_core_probe()
1653 data->regulators[1].supply = "vddio"; in bmc150_accel_core_probe()
1655 ARRAY_SIZE(data->regulators), in bmc150_accel_core_probe()
1656 data->regulators); in bmc150_accel_core_probe()
1660 ret = regulator_bulk_enable(ARRAY_SIZE(data->regulators), in bmc150_accel_core_probe()
1661 data->regulators); in bmc150_accel_core_probe()
1667 * 2ms or 3ms power-on time according to datasheets, let's better in bmc150_accel_core_probe()
1676 mutex_init(&data->mutex); in bmc150_accel_core_probe()
1678 indio_dev->channels = data->chip_info->channels; in bmc150_accel_core_probe()
1679 indio_dev->num_channels = data->chip_info->num_channels; in bmc150_accel_core_probe()
1680 indio_dev->name = name ? name : data->chip_info->name; in bmc150_accel_core_probe()
1681 indio_dev->available_scan_masks = bmc150_accel_scan_masks; in bmc150_accel_core_probe()
1682 indio_dev->modes = INDIO_DIRECT_MODE; in bmc150_accel_core_probe()
1683 indio_dev->info = &bmc150_accel_info; in bmc150_accel_core_probe()
1686 indio_dev->modes |= INDIO_BUFFER_SOFTWARE; in bmc150_accel_core_probe()
1687 indio_dev->info = &bmc150_accel_info_fifo; in bmc150_accel_core_probe()
1716 * non-latched regardless of this settings (e.g. new data) we in bmc150_accel_core_probe()
1720 ret = regmap_write(data->regmap, BMC150_ACCEL_REG_INT_RST_LATCH, in bmc150_accel_core_probe()
1754 bmc150_accel_unregister_triggers(data, BMC150_ACCEL_TRIGGERS - 1); in bmc150_accel_core_probe()
1758 regulator_bulk_disable(ARRAY_SIZE(data->regulators), in bmc150_accel_core_probe()
1759 data->regulators); in bmc150_accel_core_probe()
1775 bmc150_accel_unregister_triggers(data, BMC150_ACCEL_TRIGGERS - 1); in bmc150_accel_core_remove()
1779 mutex_lock(&data->mutex); in bmc150_accel_core_remove()
1781 mutex_unlock(&data->mutex); in bmc150_accel_core_remove()
1783 regulator_bulk_disable(ARRAY_SIZE(data->regulators), in bmc150_accel_core_remove()
1784 data->regulators); in bmc150_accel_core_remove()
1794 mutex_lock(&data->mutex); in bmc150_accel_suspend()
1796 mutex_unlock(&data->mutex); in bmc150_accel_suspend()
1806 mutex_lock(&data->mutex); in bmc150_accel_resume()
1809 mutex_unlock(&data->mutex); in bmc150_accel_resume()
1811 if (data->resume_callback) in bmc150_accel_resume()
1812 data->resume_callback(dev); in bmc150_accel_resume()
1827 return -EAGAIN; in bmc150_accel_runtime_suspend()