Lines Matching +full:wake +full:- +full:on +full:- +full:motion

1 // SPDX-License-Identifier: GPL-2.0-only
3 * KMX61 - Kionix 6-axis Accelerometer/Magnetometer
7 * IIO driver for KMX61 (7-bit I2C slave address 0x0E or 0x0F).
32 * three 16-bit accelerometer output registers for X/Y/Z axis
45 * one 16-bit temperature output register
51 * three 16-bit magnetometer output registers for X/Y/Z axis
118 /* serialize access to non-atomic ops, e.g set_mode */
299 return -EINVAL; in kmx61_convert_freq_to_bit()
310 return -EINVAL; in kmx61_convert_wake_up_odr_to_bit()
314 * kmx61_set_mode() - set KMX61 device operating mode
329 int acc_stby = -1, mag_stby = -1; in kmx61_set_mode()
331 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_STBY); in kmx61_set_mode()
333 dev_err(&data->client->dev, "Error reading reg_stby\n"); in kmx61_set_mode()
359 ret = i2c_smbus_write_byte_data(data->client, KMX61_REG_STBY, ret); in kmx61_set_mode()
361 dev_err(&data->client->dev, "Error writing reg_stby\n"); in kmx61_set_mode()
365 if (acc_stby != -1 && update) in kmx61_set_mode()
366 data->acc_stby = acc_stby; in kmx61_set_mode()
367 if (mag_stby != -1 && update) in kmx61_set_mode()
368 data->mag_stby = mag_stby; in kmx61_set_mode()
377 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_STBY); in kmx61_get_mode()
379 dev_err(&data->client->dev, "Error reading reg_stby\n"); in kmx61_get_mode()
409 ret = i2c_smbus_write_byte_data(data->client, KMX61_REG_CTRL2, in kmx61_set_wake_up_odr()
412 dev_err(&data->client->dev, "Error writing reg_ctrl2\n"); in kmx61_set_wake_up_odr()
442 ret = i2c_smbus_write_byte_data(data->client, KMX61_REG_ODCNTL, in kmx61_set_odr()
447 data->odr_bits = odr_bits; in kmx61_set_odr()
464 lodr_bits = (data->odr_bits >> KMX61_ACC_ODR_SHIFT) & in kmx61_get_odr()
467 lodr_bits = (data->odr_bits >> KMX61_MAG_ODR_SHIFT) & in kmx61_get_odr()
470 return -EINVAL; in kmx61_get_odr()
473 return -EINVAL; in kmx61_get_odr()
485 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_CTRL1); in kmx61_set_range()
487 dev_err(&data->client->dev, "Error reading reg_ctrl1\n"); in kmx61_set_range()
494 ret = i2c_smbus_write_byte_data(data->client, KMX61_REG_CTRL1, ret); in kmx61_set_range()
496 dev_err(&data->client->dev, "Error writing reg_ctrl1\n"); in kmx61_set_range()
500 data->range = range; in kmx61_set_range()
530 return -EINVAL; in kmx61_set_scale()
537 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_WHO_AM_I); in kmx61_chip_init()
539 dev_err(&data->client->dev, "Error reading who_am_i\n"); in kmx61_chip_init()
544 dev_err(&data->client->dev, in kmx61_chip_init()
547 return -EINVAL; in kmx61_chip_init()
555 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_ODCNTL); in kmx61_chip_init()
557 dev_err(&data->client->dev, "Error reading reg_odcntl\n"); in kmx61_chip_init()
560 data->odr_bits = ret; in kmx61_chip_init()
563 * set output data rate for wake up (motion detection) function in kmx61_chip_init()
579 data->wake_thresh = KMX61_DEFAULT_WAKE_THRESH; in kmx61_chip_init()
580 data->wake_duration = KMX61_DEFAULT_WAKE_DURATION; in kmx61_chip_init()
599 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_INC1); in kmx61_setup_new_data_interrupt()
601 dev_err(&data->client->dev, "Error reading reg_ctrl1\n"); in kmx61_setup_new_data_interrupt()
618 ret = i2c_smbus_write_byte_data(data->client, KMX61_REG_INC1, ret); in kmx61_setup_new_data_interrupt()
620 dev_err(&data->client->dev, "Error writing reg_int_ctrl1\n"); in kmx61_setup_new_data_interrupt()
624 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_CTRL1); in kmx61_setup_new_data_interrupt()
626 dev_err(&data->client->dev, "Error reading reg_ctrl1\n"); in kmx61_setup_new_data_interrupt()
635 ret = i2c_smbus_write_byte_data(data->client, KMX61_REG_CTRL1, ret); in kmx61_setup_new_data_interrupt()
637 dev_err(&data->client->dev, "Error writing reg_ctrl1\n"); in kmx61_setup_new_data_interrupt()
648 ret = i2c_smbus_write_byte_data(data->client, in kmx61_chip_update_thresholds()
650 data->wake_duration); in kmx61_chip_update_thresholds()
652 dev_err(&data->client->dev, "Error writing reg_wuf_timer\n"); in kmx61_chip_update_thresholds()
656 ret = i2c_smbus_write_byte_data(data->client, in kmx61_chip_update_thresholds()
658 data->wake_thresh); in kmx61_chip_update_thresholds()
660 dev_err(&data->client->dev, "Error writing reg_wuf_thresh\n"); in kmx61_chip_update_thresholds()
683 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_INC1); in kmx61_setup_any_motion_interrupt()
685 dev_err(&data->client->dev, "Error reading reg_inc1\n"); in kmx61_setup_any_motion_interrupt()
693 ret = i2c_smbus_write_byte_data(data->client, KMX61_REG_INC1, ret); in kmx61_setup_any_motion_interrupt()
695 dev_err(&data->client->dev, "Error writing reg_inc1\n"); in kmx61_setup_any_motion_interrupt()
699 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_CTRL1); in kmx61_setup_any_motion_interrupt()
701 dev_err(&data->client->dev, "Error reading reg_ctrl1\n"); in kmx61_setup_any_motion_interrupt()
710 ret = i2c_smbus_write_byte_data(data->client, KMX61_REG_CTRL1, ret); in kmx61_setup_any_motion_interrupt()
712 dev_err(&data->client->dev, "Error writing reg_ctrl1\n"); in kmx61_setup_any_motion_interrupt()
720 * kmx61_set_power_state() - set power state for kmx61 @device
722 * @on: power state to be set for @device
723 * @device: bitmask indicating device for which @on state needs to be set
725 * Notice that when ACC power state needs to be set to ON and MAG is in
728 * state needs to be set to ON and ACC is in OPERATION.
730 static int kmx61_set_power_state(struct kmx61_data *data, bool on, u8 device) in kmx61_set_power_state() argument
736 if (on && !data->acc_ps && !data->mag_stby) { in kmx61_set_power_state()
741 data->acc_ps = on; in kmx61_set_power_state()
744 if (on && !data->mag_ps && !data->acc_stby) { in kmx61_set_power_state()
749 data->mag_ps = on; in kmx61_set_power_state()
752 if (on) { in kmx61_set_power_state()
753 ret = pm_runtime_resume_and_get(&data->client->dev); in kmx61_set_power_state()
755 pm_runtime_mark_last_busy(&data->client->dev); in kmx61_set_power_state()
756 ret = pm_runtime_put_autosuspend(&data->client->dev); in kmx61_set_power_state()
759 dev_err(&data->client->dev, in kmx61_set_power_state()
761 on, ret); in kmx61_set_power_state()
774 ret = i2c_smbus_read_word_data(data->client, reg); in kmx61_read_measurement()
776 dev_err(&data->client->dev, "failed to read reg at %x\n", reg); in kmx61_read_measurement()
791 switch (chan->type) { in kmx61_read_raw()
799 return -EINVAL; in kmx61_read_raw()
801 mutex_lock(&data->lock); in kmx61_read_raw()
803 ret = kmx61_set_power_state(data, true, chan->address); in kmx61_read_raw()
805 mutex_unlock(&data->lock); in kmx61_read_raw()
809 ret = kmx61_read_measurement(data, base_reg, chan->scan_index); in kmx61_read_raw()
811 kmx61_set_power_state(data, false, chan->address); in kmx61_read_raw()
812 mutex_unlock(&data->lock); in kmx61_read_raw()
815 *val = sign_extend32(ret >> chan->scan_type.shift, in kmx61_read_raw()
816 chan->scan_type.realbits - 1); in kmx61_read_raw()
817 ret = kmx61_set_power_state(data, false, chan->address); in kmx61_read_raw()
819 mutex_unlock(&data->lock); in kmx61_read_raw()
824 switch (chan->type) { in kmx61_read_raw()
827 *val2 = kmx61_uscale_table[data->range]; in kmx61_read_raw()
835 return -EINVAL; in kmx61_read_raw()
838 if (chan->type != IIO_ACCEL && chan->type != IIO_MAGN) in kmx61_read_raw()
839 return -EINVAL; in kmx61_read_raw()
841 mutex_lock(&data->lock); in kmx61_read_raw()
842 ret = kmx61_get_odr(data, val, val2, chan->address); in kmx61_read_raw()
843 mutex_unlock(&data->lock); in kmx61_read_raw()
845 return -EINVAL; in kmx61_read_raw()
848 return -EINVAL; in kmx61_read_raw()
860 if (chan->type != IIO_ACCEL && chan->type != IIO_MAGN) in kmx61_write_raw()
861 return -EINVAL; in kmx61_write_raw()
863 mutex_lock(&data->lock); in kmx61_write_raw()
864 ret = kmx61_set_odr(data, val, val2, chan->address); in kmx61_write_raw()
865 mutex_unlock(&data->lock); in kmx61_write_raw()
868 switch (chan->type) { in kmx61_write_raw()
871 return -EINVAL; in kmx61_write_raw()
872 mutex_lock(&data->lock); in kmx61_write_raw()
874 mutex_unlock(&data->lock); in kmx61_write_raw()
877 return -EINVAL; in kmx61_write_raw()
880 return -EINVAL; in kmx61_write_raw()
896 *val = data->wake_thresh; in kmx61_read_event()
899 *val = data->wake_duration; in kmx61_read_event()
902 return -EINVAL; in kmx61_read_event()
915 if (data->ev_enable_state) in kmx61_write_event()
916 return -EBUSY; in kmx61_write_event()
920 data->wake_thresh = val; in kmx61_write_event()
923 data->wake_duration = val; in kmx61_write_event()
926 return -EINVAL; in kmx61_write_event()
937 return data->ev_enable_state; in kmx61_read_event_config()
949 if (state && data->ev_enable_state) in kmx61_write_event_config()
952 mutex_lock(&data->lock); in kmx61_write_event_config()
954 if (!state && data->motion_trig_on) { in kmx61_write_event_config()
955 data->ev_enable_state = false; in kmx61_write_event_config()
969 data->ev_enable_state = state; in kmx61_write_event_config()
972 mutex_unlock(&data->lock); in kmx61_write_event_config()
982 if (data->acc_dready_trig != trig && data->motion_trig != trig) in kmx61_acc_validate_trigger()
983 return -EINVAL; in kmx61_acc_validate_trigger()
993 if (data->mag_dready_trig != trig) in kmx61_mag_validate_trigger()
994 return -EINVAL; in kmx61_mag_validate_trigger()
1027 mutex_lock(&data->lock); in kmx61_data_rdy_trigger_set_state()
1029 if (!state && data->ev_enable_state && data->motion_trig_on) { in kmx61_data_rdy_trigger_set_state()
1030 data->motion_trig_on = false; in kmx61_data_rdy_trigger_set_state()
1034 if (data->acc_dready_trig == trig || data->motion_trig == trig) in kmx61_data_rdy_trigger_set_state()
1043 if (data->acc_dready_trig == trig || data->mag_dready_trig == trig) in kmx61_data_rdy_trigger_set_state()
1052 if (data->acc_dready_trig == trig) in kmx61_data_rdy_trigger_set_state()
1053 data->acc_dready_trig_on = state; in kmx61_data_rdy_trigger_set_state()
1054 else if (data->mag_dready_trig == trig) in kmx61_data_rdy_trigger_set_state()
1055 data->mag_dready_trig_on = state; in kmx61_data_rdy_trigger_set_state()
1057 data->motion_trig_on = state; in kmx61_data_rdy_trigger_set_state()
1059 mutex_unlock(&data->lock); in kmx61_data_rdy_trigger_set_state()
1070 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_INL); in kmx61_trig_reenable()
1072 dev_err(&data->client->dev, "Error reading reg_inl\n"); in kmx61_trig_reenable()
1083 struct iio_dev *indio_dev = data->acc_indio_dev; in kmx61_event_handler()
1086 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_INS1); in kmx61_event_handler()
1088 dev_err(&data->client->dev, "Error reading reg_ins1\n"); in kmx61_event_handler()
1093 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_INS2); in kmx61_event_handler()
1095 dev_err(&data->client->dev, "Error reading reg_ins2\n"); in kmx61_event_handler()
1155 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_CTRL1); in kmx61_event_handler()
1157 dev_err(&data->client->dev, "Error reading reg_ctrl1\n"); in kmx61_event_handler()
1160 ret = i2c_smbus_write_byte_data(data->client, KMX61_REG_CTRL1, ret); in kmx61_event_handler()
1162 dev_err(&data->client->dev, "Error writing reg_ctrl1\n"); in kmx61_event_handler()
1164 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_INL); in kmx61_event_handler()
1166 dev_err(&data->client->dev, "Error reading reg_inl\n"); in kmx61_event_handler()
1175 if (data->acc_dready_trig_on) in kmx61_data_rdy_trig_poll()
1176 iio_trigger_poll(data->acc_dready_trig); in kmx61_data_rdy_trig_poll()
1177 if (data->mag_dready_trig_on) in kmx61_data_rdy_trig_poll()
1178 iio_trigger_poll(data->mag_dready_trig); in kmx61_data_rdy_trig_poll()
1180 if (data->motion_trig_on) in kmx61_data_rdy_trig_poll()
1181 iio_trigger_poll(data->motion_trig); in kmx61_data_rdy_trig_poll()
1183 if (data->ev_enable_state) in kmx61_data_rdy_trig_poll()
1191 struct iio_dev *indio_dev = pf->indio_dev; in kmx61_trigger_handler()
1197 if (indio_dev == data->acc_indio_dev) in kmx61_trigger_handler()
1202 mutex_lock(&data->lock); in kmx61_trigger_handler()
1206 mutex_unlock(&data->lock); in kmx61_trigger_handler()
1211 mutex_unlock(&data->lock); in kmx61_trigger_handler()
1215 iio_trigger_notify_done(indio_dev->trig); in kmx61_trigger_handler()
1224 id = acpi_match_device(dev->driver->acpi_match_table, dev); in kmx61_match_acpi_device()
1238 indio_dev = devm_iio_device_alloc(&data->client->dev, sizeof(data)); in kmx61_indiodev_setup()
1240 return ERR_PTR(-ENOMEM); in kmx61_indiodev_setup()
1244 indio_dev->channels = chan; in kmx61_indiodev_setup()
1245 indio_dev->num_channels = num_channels; in kmx61_indiodev_setup()
1246 indio_dev->name = name; in kmx61_indiodev_setup()
1247 indio_dev->modes = INDIO_DIRECT_MODE; in kmx61_indiodev_setup()
1248 indio_dev->info = info; in kmx61_indiodev_setup()
1260 trig = devm_iio_trigger_alloc(&data->client->dev, in kmx61_trigger_setup()
1261 "%s-%s-dev%d", in kmx61_trigger_setup()
1262 indio_dev->name, in kmx61_trigger_setup()
1266 return ERR_PTR(-ENOMEM); in kmx61_trigger_setup()
1268 trig->ops = &kmx61_trigger_ops; in kmx61_trigger_setup()
1285 data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL); in kmx61_probe()
1287 return -ENOMEM; in kmx61_probe()
1290 data->client = client; in kmx61_probe()
1292 mutex_init(&data->lock); in kmx61_probe()
1295 name = id->name; in kmx61_probe()
1296 else if (ACPI_HANDLE(&client->dev)) in kmx61_probe()
1297 name = kmx61_match_acpi_device(&client->dev); in kmx61_probe()
1299 return -ENODEV; in kmx61_probe()
1301 data->acc_indio_dev = in kmx61_probe()
1306 if (IS_ERR(data->acc_indio_dev)) in kmx61_probe()
1307 return PTR_ERR(data->acc_indio_dev); in kmx61_probe()
1309 data->mag_indio_dev = in kmx61_probe()
1314 if (IS_ERR(data->mag_indio_dev)) in kmx61_probe()
1315 return PTR_ERR(data->mag_indio_dev); in kmx61_probe()
1321 if (client->irq > 0) { in kmx61_probe()
1322 ret = devm_request_threaded_irq(&client->dev, client->irq, in kmx61_probe()
1331 data->acc_dready_trig = in kmx61_probe()
1332 kmx61_trigger_setup(data, data->acc_indio_dev, in kmx61_probe()
1334 if (IS_ERR(data->acc_dready_trig)) { in kmx61_probe()
1335 ret = PTR_ERR(data->acc_dready_trig); in kmx61_probe()
1339 data->mag_dready_trig = in kmx61_probe()
1340 kmx61_trigger_setup(data, data->mag_indio_dev, in kmx61_probe()
1342 if (IS_ERR(data->mag_dready_trig)) { in kmx61_probe()
1343 ret = PTR_ERR(data->mag_dready_trig); in kmx61_probe()
1347 data->motion_trig = in kmx61_probe()
1348 kmx61_trigger_setup(data, data->acc_indio_dev, in kmx61_probe()
1349 "any-motion"); in kmx61_probe()
1350 if (IS_ERR(data->motion_trig)) { in kmx61_probe()
1351 ret = PTR_ERR(data->motion_trig); in kmx61_probe()
1355 ret = iio_triggered_buffer_setup(data->acc_indio_dev, in kmx61_probe()
1360 dev_err(&data->client->dev, in kmx61_probe()
1365 ret = iio_triggered_buffer_setup(data->mag_indio_dev, in kmx61_probe()
1370 dev_err(&data->client->dev, in kmx61_probe()
1376 ret = pm_runtime_set_active(&client->dev); in kmx61_probe()
1380 pm_runtime_enable(&client->dev); in kmx61_probe()
1381 pm_runtime_set_autosuspend_delay(&client->dev, KMX61_SLEEP_DELAY_MS); in kmx61_probe()
1382 pm_runtime_use_autosuspend(&client->dev); in kmx61_probe()
1384 ret = iio_device_register(data->acc_indio_dev); in kmx61_probe()
1386 dev_err(&client->dev, "Failed to register acc iio device\n"); in kmx61_probe()
1390 ret = iio_device_register(data->mag_indio_dev); in kmx61_probe()
1392 dev_err(&client->dev, "Failed to register mag iio device\n"); in kmx61_probe()
1399 iio_device_unregister(data->acc_indio_dev); in kmx61_probe()
1401 pm_runtime_dont_use_autosuspend(&client->dev); in kmx61_probe()
1402 pm_runtime_disable(&client->dev); in kmx61_probe()
1404 if (client->irq > 0) in kmx61_probe()
1405 iio_triggered_buffer_cleanup(data->mag_indio_dev); in kmx61_probe()
1407 if (client->irq > 0) in kmx61_probe()
1408 iio_triggered_buffer_cleanup(data->acc_indio_dev); in kmx61_probe()
1410 iio_trigger_unregister(data->motion_trig); in kmx61_probe()
1412 iio_trigger_unregister(data->mag_dready_trig); in kmx61_probe()
1414 iio_trigger_unregister(data->acc_dready_trig); in kmx61_probe()
1424 iio_device_unregister(data->acc_indio_dev); in kmx61_remove()
1425 iio_device_unregister(data->mag_indio_dev); in kmx61_remove()
1427 pm_runtime_disable(&client->dev); in kmx61_remove()
1428 pm_runtime_set_suspended(&client->dev); in kmx61_remove()
1430 if (client->irq > 0) { in kmx61_remove()
1431 iio_triggered_buffer_cleanup(data->acc_indio_dev); in kmx61_remove()
1432 iio_triggered_buffer_cleanup(data->mag_indio_dev); in kmx61_remove()
1433 iio_trigger_unregister(data->acc_dready_trig); in kmx61_remove()
1434 iio_trigger_unregister(data->mag_dready_trig); in kmx61_remove()
1435 iio_trigger_unregister(data->motion_trig); in kmx61_remove()
1438 mutex_lock(&data->lock); in kmx61_remove()
1440 mutex_unlock(&data->lock); in kmx61_remove()
1448 mutex_lock(&data->lock); in kmx61_suspend()
1451 mutex_unlock(&data->lock); in kmx61_suspend()
1461 if (data->acc_stby) in kmx61_resume()
1463 if (data->mag_stby) in kmx61_resume()
1474 mutex_lock(&data->lock); in kmx61_runtime_suspend()
1476 mutex_unlock(&data->lock); in kmx61_runtime_suspend()
1486 if (!data->acc_ps) in kmx61_runtime_resume()
1488 if (!data->mag_ps) in kmx61_runtime_resume()