Lines Matching +full:keep +full:- +full:on +full:- +full:stby
1 // SPDX-License-Identifier: GPL-2.0-only
28 #include "kionix-kx022a.h"
88 * The HW does not set WHO_AM_I reg as read-only but we don't want to write it
89 * so we still include it in the read-only ranges.
282 * Prevent toggling the sensor stby/active state (PC1 bit) in the
306 return &data->orientation; in kx022a_get_mount_matrix()
392 * range is typically +-2G/4G/8G/16G, distributed over the amount of bits.
395 * => KX022A uses 16 bit (HiRes mode - assume the low 8 bits are zeroed
396 * in low-power mode(?) )
397 * => +/-2G => 4 / 2^16 * 9,80665
398 * => +/-2G - 0.000598550415
399 * +/-4G - 0.00119710083
400 * +/-8G - 0.00239420166
401 * +/-16G - 0.00478840332
429 return -EINVAL; in kx022a_read_avail()
451 static int kx022a_turn_on_off_unlocked(struct kx022a_data *data, bool on) in kx022a_turn_on_off_unlocked() argument
455 if (on) in kx022a_turn_on_off_unlocked()
456 ret = regmap_set_bits(data->regmap, data->chip_info->cntl, in kx022a_turn_on_off_unlocked()
459 ret = regmap_clear_bits(data->regmap, data->chip_info->cntl, in kx022a_turn_on_off_unlocked()
462 dev_err(data->dev, "Turn %s fail %d\n", str_on_off(on), ret); in kx022a_turn_on_off_unlocked()
471 mutex_lock(&data->mutex); in kx022a_turn_off_lock()
474 mutex_unlock(&data->mutex); in kx022a_turn_off_lock()
484 mutex_unlock(&data->mutex); in kx022a_turn_on_unlock()
499 return -EINVAL; in kx022a_write_raw_get_fmt()
515 * fifo keep matching the freq/scale settings. (Such setup could cause in kx022a_write_raw()
517 * time-limit). in kx022a_write_raw()
527 while (n--) in kx022a_write_raw()
532 ret = -EINVAL; in kx022a_write_raw()
539 ret = regmap_update_bits(data->regmap, in kx022a_write_raw()
540 data->chip_info->odcntl, in kx022a_write_raw()
542 data->odr_ns = kx022a_odrs[n]; in kx022a_write_raw()
548 while (n-- > 0) in kx022a_write_raw()
553 ret = -EINVAL; in kx022a_write_raw()
561 ret = regmap_update_bits(data->regmap, data->chip_info->cntl, in kx022a_write_raw()
567 ret = -EINVAL; in kx022a_write_raw()
581 threshold = data->watermark; in kx022a_fifo_set_wmi()
583 return regmap_update_bits(data->regmap, data->chip_info->buf_cntl1, in kx022a_fifo_set_wmi()
593 ret = regmap_bulk_read(data->regmap, chan->address, &data->buffer[0], in kx022a_get_axis()
598 *val = (s16)le16_to_cpu(data->buffer[0]); in kx022a_get_axis()
617 mutex_lock(&data->mutex); in kx022a_read_raw()
619 mutex_unlock(&data->mutex); in kx022a_read_raw()
626 ret = regmap_read(data->regmap, data->chip_info->odcntl, ®val); in kx022a_read_raw()
632 dev_err(data->dev, "Invalid ODR\n"); in kx022a_read_raw()
633 return -EINVAL; in kx022a_read_raw()
641 ret = regmap_read(data->regmap, data->chip_info->cntl, ®val); in kx022a_read_raw()
650 return -EINVAL; in kx022a_read_raw()
657 val = min(data->chip_info->fifo_length, val); in kx022a_set_watermark()
659 mutex_lock(&data->mutex); in kx022a_set_watermark()
660 data->watermark = val; in kx022a_set_watermark()
661 mutex_unlock(&data->mutex); in kx022a_set_watermark()
674 mutex_lock(&data->mutex); in hwfifo_enabled_show()
675 state = data->state; in hwfifo_enabled_show()
676 mutex_unlock(&data->mutex); in hwfifo_enabled_show()
689 mutex_lock(&data->mutex); in hwfifo_watermark_show()
690 wm = data->watermark; in hwfifo_watermark_show()
691 mutex_unlock(&data->mutex); in hwfifo_watermark_show()
708 * We must clear the old time-stamp to avoid computing the timestamps in kx022a_drop_fifo_contents()
709 * based on samples acquired when buffer was last enabled. in kx022a_drop_fifo_contents()
712 * called from fifo-disable where we can guarantee the sensor is not in kx022a_drop_fifo_contents()
714 * user-space access. in kx022a_drop_fifo_contents()
716 data->timestamp = 0; in kx022a_drop_fifo_contents()
718 return regmap_write(data->regmap, data->chip_info->buf_clear, 0x0); in kx022a_drop_fifo_contents()
725 ret = regmap_read(data->regmap, KX022A_REG_BUF_STATUS_1, &fifo_bytes); in kx022a_get_fifo_bytes_available()
727 dev_err(data->dev, "Error reading buffer status\n"); in kx022a_get_fifo_bytes_available()
742 ret = regmap_bulk_read(data->regmap, data->chip_info->buf_status1, in kx132_get_fifo_bytes_available()
745 dev_err(data->dev, "Error reading buffer status\n"); in kx132_get_fifo_bytes_available()
750 fifo_bytes &= data->chip_info->buf_smp_lvl_mask; in kx132_get_fifo_bytes_available()
751 fifo_bytes = min((unsigned int)fifo_bytes, data->chip_info->fifo_length * in kx132_get_fifo_bytes_available()
767 fifo_bytes = data->chip_info->get_fifo_bytes_available(data); in __kx022a_fifo_flush()
770 dev_warn(data->dev, "Bad FIFO alignment. Data may be corrupt\n"); in __kx022a_fifo_flush()
786 * We need to have the IRQ disabled or we risk of messing-up in __kx022a_fifo_flush()
788 * IRQF_ONESHOT has us covered - but if we are ran by the in __kx022a_fifo_flush()
789 * user-space read we need to disable the IRQ to be on a safe in __kx022a_fifo_flush()
791 * IRQ thread is being ran on other CPU we wait for it to be in __kx022a_fifo_flush()
794 disable_irq(data->irq); in __kx022a_fifo_flush()
797 data->old_timestamp = data->timestamp; in __kx022a_fifo_flush()
798 data->timestamp = iio_get_time_ns(idev); in __kx022a_fifo_flush()
802 * Approximate timestamps for each of the sample based on the sampling in __kx022a_fifo_flush()
810 * based on the timestamp delta between the last two flush operations. in __kx022a_fifo_flush()
812 if (data->old_timestamp) { in __kx022a_fifo_flush()
813 sample_period = data->timestamp - data->old_timestamp; in __kx022a_fifo_flush()
816 sample_period = data->odr_ns; in __kx022a_fifo_flush()
818 tstamp = data->timestamp - (count - 1) * sample_period; in __kx022a_fifo_flush()
826 data->timestamp -= (count - samples) * sample_period; in __kx022a_fifo_flush()
831 ret = regmap_noinc_read(data->regmap, data->chip_info->buf_read, in __kx022a_fifo_flush()
832 data->fifo_buffer, fifo_bytes); in __kx022a_fifo_flush()
837 __le16 *sam = &data->fifo_buffer[i * 3]; in __kx022a_fifo_flush()
841 chs = &data->scan.channels[0]; in __kx022a_fifo_flush()
842 for_each_set_bit(bit, idev->active_scan_mask, AXIS_MAX) in __kx022a_fifo_flush()
845 iio_push_to_buffers_with_timestamp(idev, &data->scan, tstamp); in __kx022a_fifo_flush()
854 enable_irq(data->irq); in __kx022a_fifo_flush()
864 mutex_lock(&data->mutex); in kx022a_fifo_flush()
866 mutex_unlock(&data->mutex); in kx022a_fifo_flush()
885 return regmap_set_bits(data->regmap, data->chip_info->cntl, in kx022a_set_drdy_irq()
888 return regmap_clear_bits(data->regmap, data->chip_info->cntl, in kx022a_set_drdy_irq()
901 ret = regmap_update_bits(data->regmap, data->inc_reg, mask, val); in kx022a_prepare_irq_pin()
908 return regmap_set_bits(data->regmap, data->ien_reg, mask); in kx022a_prepare_irq_pin()
919 ret = regmap_clear_bits(data->regmap, data->ien_reg, KX022A_MASK_WMI); in kx022a_fifo_disable()
923 ret = regmap_clear_bits(data->regmap, data->chip_info->buf_cntl2, in kx022a_fifo_disable()
928 data->state &= ~KX022A_STATE_FIFO; in kx022a_fifo_disable()
932 kfree(data->fifo_buffer); in kx022a_fifo_disable()
937 mutex_unlock(&data->mutex); in kx022a_fifo_disable()
956 data->fifo_buffer = kmalloc_array(data->chip_info->fifo_length, in kx022a_fifo_enable()
959 if (!data->fifo_buffer) in kx022a_fifo_enable()
960 return -ENOMEM; in kx022a_fifo_enable()
972 ret = regmap_set_bits(data->regmap, data->chip_info->buf_cntl2, in kx022a_fifo_enable()
977 data->state |= KX022A_STATE_FIFO; in kx022a_fifo_enable()
978 ret = regmap_set_bits(data->regmap, data->ien_reg, in kx022a_fifo_enable()
986 mutex_unlock(&data->mutex); in kx022a_fifo_enable()
996 * If we use data-ready trigger, then the IRQ masks should be handled by in kx022a_buffer_postenable()
998 * results to the IIO fifo when data-ready triggers. in kx022a_buffer_postenable()
1014 struct iio_dev *idev = pf->indio_dev; in kx022a_trigger_handler()
1018 ret = regmap_bulk_read(data->regmap, data->chip_info->xout_l, data->buffer, in kx022a_trigger_handler()
1023 iio_push_to_buffers_with_timestamp(idev, data->buffer, data->timestamp); in kx022a_trigger_handler()
1025 iio_trigger_notify_done(idev->trig); in kx022a_trigger_handler()
1036 data->old_timestamp = data->timestamp; in kx022a_irq_handler()
1037 data->timestamp = iio_get_time_ns(idev); in kx022a_irq_handler()
1039 if (data->state & KX022A_STATE_FIFO || data->trigger_enabled) in kx022a_irq_handler()
1046 * WMI and data-ready IRQs are acked when results are read. If we add
1047 * TILT/WAKE or other IRQs - then we may need to implement the acking
1056 mutex_lock(&data->mutex); in kx022a_irq_thread_handler()
1058 if (data->trigger_enabled) { in kx022a_irq_thread_handler()
1059 iio_trigger_poll_nested(data->trig); in kx022a_irq_thread_handler()
1063 if (data->state & KX022A_STATE_FIFO) { in kx022a_irq_thread_handler()
1066 ok = __kx022a_fifo_flush(idev, data->chip_info->fifo_length, true); in kx022a_irq_thread_handler()
1071 mutex_unlock(&data->mutex); in kx022a_irq_thread_handler()
1082 mutex_lock(&data->mutex); in kx022a_trigger_set_state()
1084 if (data->trigger_enabled == state) in kx022a_trigger_set_state()
1087 if (data->state & KX022A_STATE_FIFO) { in kx022a_trigger_set_state()
1088 dev_warn(data->dev, "Can't set trigger when FIFO enabled\n"); in kx022a_trigger_set_state()
1089 ret = -EBUSY; in kx022a_trigger_set_state()
1097 data->trigger_enabled = state; in kx022a_trigger_set_state()
1105 mutex_unlock(&data->mutex); in kx022a_trigger_set_state()
1119 ret = regmap_write(data->regmap, data->chip_info->cntl2, KX022A_MASK_SRST); in kx022a_chip_init()
1129 ret = regmap_read_poll_timeout(data->regmap, data->chip_info->cntl2, val, in kx022a_chip_init()
1134 dev_err(data->dev, "Sensor reset %s\n", in kx022a_chip_init()
1139 ret = regmap_reinit_cache(data->regmap, data->chip_info->regmap_config); in kx022a_chip_init()
1141 dev_err(data->dev, "Failed to reinit reg cache\n"); in kx022a_chip_init()
1146 ret = regmap_set_bits(data->regmap, data->chip_info->buf_cntl2, in kx022a_chip_init()
1149 dev_err(data->dev, "Failed to set data resolution\n"); in kx022a_chip_init()
1157 .name = "kx022-accel",
1182 .name = "kx132-1211",
1208 * Despite the naming, KX132ACR-LBZ is not similar to KX132-1211 but it is
1209 * exact subset of KX022A. KX132ACR-LBZ is meant to be used for industrial
1215 .name = "kx132acr-lbz",
1241 static const char * const regulator_names[] = {"io-vdd", "vdd"}; in kx022a_probe_internal()
1254 return -EINVAL; in kx022a_probe_internal()
1259 return -ENODEV; in kx022a_probe_internal()
1263 return -ENOMEM; in kx022a_probe_internal()
1266 data->chip_info = chip_info; in kx022a_probe_internal()
1274 if (ret && ret != -ENODEV) in kx022a_probe_internal()
1277 ret = regmap_read(regmap, chip_info->who, &chip_id); in kx022a_probe_internal()
1281 if (chip_id != chip_info->id) in kx022a_probe_internal()
1286 data->inc_reg = chip_info->inc1; in kx022a_probe_internal()
1287 data->ien_reg = chip_info->inc4; in kx022a_probe_internal()
1293 data->inc_reg = chip_info->inc5; in kx022a_probe_internal()
1294 data->ien_reg = chip_info->inc6; in kx022a_probe_internal()
1297 data->regmap = regmap; in kx022a_probe_internal()
1298 data->dev = dev; in kx022a_probe_internal()
1299 data->irq = irq; in kx022a_probe_internal()
1300 data->odr_ns = KX022A_DEFAULT_PERIOD_NS; in kx022a_probe_internal()
1301 mutex_init(&data->mutex); in kx022a_probe_internal()
1303 idev->channels = chip_info->channels; in kx022a_probe_internal()
1304 idev->num_channels = chip_info->num_channels; in kx022a_probe_internal()
1305 idev->name = chip_info->name; in kx022a_probe_internal()
1306 idev->info = &kx022a_info; in kx022a_probe_internal()
1307 idev->modes = INDIO_DIRECT_MODE | INDIO_BUFFER_SOFTWARE; in kx022a_probe_internal()
1308 idev->available_scan_masks = kx022a_scan_masks; in kx022a_probe_internal()
1311 ret = iio_read_mount_matrix(dev, &data->orientation); in kx022a_probe_internal()
1322 mutex_unlock(&data->mutex); in kx022a_probe_internal()
1338 return dev_err_probe(data->dev, ret, in kx022a_probe_internal()
1340 indio_trig = devm_iio_trigger_alloc(dev, "%sdata-rdy-dev%d", idev->name, in kx022a_probe_internal()
1343 return -ENOMEM; in kx022a_probe_internal()
1345 data->trig = indio_trig; in kx022a_probe_internal()
1347 indio_trig->ops = &kx022a_trigger_ops; in kx022a_probe_internal()
1354 name = devm_kasprintf(data->dev, GFP_KERNEL, "%s-kx022a", in kx022a_probe_internal()
1355 dev_name(data->dev)); in kx022a_probe_internal()
1357 ret = devm_request_threaded_irq(data->dev, irq, kx022a_irq_handler, in kx022a_probe_internal()
1361 return dev_err_probe(data->dev, ret, "Could not request IRQ\n"); in kx022a_probe_internal()
1365 return dev_err_probe(data->dev, ret, in kx022a_probe_internal()
1368 ret = devm_iio_device_register(data->dev, idev); in kx022a_probe_internal()