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;
310 return -EINVAL;
314 * kmx61_set_mode() - set KMX61 device operating mode
329 int acc_stby = -1, mag_stby = -1;
331 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_STBY);
333 dev_err(&data->client->dev, "Error reading reg_stby\n");
359 ret = i2c_smbus_write_byte_data(data->client, KMX61_REG_STBY, ret);
361 dev_err(&data->client->dev, "Error writing reg_stby\n");
365 if (acc_stby != -1 && update)
366 data->acc_stby = acc_stby;
367 if (mag_stby != -1 && update)
368 data->mag_stby = mag_stby;
377 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_STBY);
379 dev_err(&data->client->dev, "Error reading reg_stby\n");
409 ret = i2c_smbus_write_byte_data(data->client, KMX61_REG_CTRL2,
412 dev_err(&data->client->dev, "Error writing reg_ctrl2\n");
442 ret = i2c_smbus_write_byte_data(data->client, KMX61_REG_ODCNTL,
447 data->odr_bits = odr_bits;
464 lodr_bits = (data->odr_bits >> KMX61_ACC_ODR_SHIFT) &
467 lodr_bits = (data->odr_bits >> KMX61_MAG_ODR_SHIFT) &
470 return -EINVAL;
473 return -EINVAL;
485 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_CTRL1);
487 dev_err(&data->client->dev, "Error reading reg_ctrl1\n");
494 ret = i2c_smbus_write_byte_data(data->client, KMX61_REG_CTRL1, ret);
496 dev_err(&data->client->dev, "Error writing reg_ctrl1\n");
500 data->range = range;
530 return -EINVAL;
537 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_WHO_AM_I);
539 dev_err(&data->client->dev, "Error reading who_am_i\n");
544 dev_err(&data->client->dev,
547 return -EINVAL;
555 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_ODCNTL);
557 dev_err(&data->client->dev, "Error reading reg_odcntl\n");
560 data->odr_bits = ret;
563 * set output data rate for wake up (motion detection) function
579 data->wake_thresh = KMX61_DEFAULT_WAKE_THRESH;
580 data->wake_duration = KMX61_DEFAULT_WAKE_DURATION;
599 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_INC1);
601 dev_err(&data->client->dev, "Error reading reg_ctrl1\n");
618 ret = i2c_smbus_write_byte_data(data->client, KMX61_REG_INC1, ret);
620 dev_err(&data->client->dev, "Error writing reg_int_ctrl1\n");
624 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_CTRL1);
626 dev_err(&data->client->dev, "Error reading reg_ctrl1\n");
635 ret = i2c_smbus_write_byte_data(data->client, KMX61_REG_CTRL1, ret);
637 dev_err(&data->client->dev, "Error writing reg_ctrl1\n");
648 ret = i2c_smbus_write_byte_data(data->client,
650 data->wake_duration);
652 dev_err(&data->client->dev, "Error writing reg_wuf_timer\n");
656 ret = i2c_smbus_write_byte_data(data->client,
658 data->wake_thresh);
660 dev_err(&data->client->dev, "Error writing reg_wuf_thresh\n");
683 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_INC1);
685 dev_err(&data->client->dev, "Error reading reg_inc1\n");
693 ret = i2c_smbus_write_byte_data(data->client, KMX61_REG_INC1, ret);
695 dev_err(&data->client->dev, "Error writing reg_inc1\n");
699 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_CTRL1);
701 dev_err(&data->client->dev, "Error reading reg_ctrl1\n");
710 ret = i2c_smbus_write_byte_data(data->client, KMX61_REG_CTRL1, ret);
712 dev_err(&data->client->dev, "Error writing reg_ctrl1\n");
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)
736 if (on && !data->acc_ps && !data->mag_stby) {
741 data->acc_ps = on;
744 if (on && !data->mag_ps && !data->acc_stby) {
749 data->mag_ps = on;
752 if (on) {
753 ret = pm_runtime_resume_and_get(&data->client->dev);
755 pm_runtime_mark_last_busy(&data->client->dev);
756 ret = pm_runtime_put_autosuspend(&data->client->dev);
759 dev_err(&data->client->dev,
761 on, ret);
774 ret = i2c_smbus_read_word_data(data->client, reg);
776 dev_err(&data->client->dev, "failed to read reg at %x\n", reg);
791 switch (chan->type) {
799 return -EINVAL;
801 mutex_lock(&data->lock);
803 ret = kmx61_set_power_state(data, true, chan->address);
805 mutex_unlock(&data->lock);
809 ret = kmx61_read_measurement(data, base_reg, chan->scan_index);
811 kmx61_set_power_state(data, false, chan->address);
812 mutex_unlock(&data->lock);
815 *val = sign_extend32(ret >> chan->scan_type.shift,
816 chan->scan_type.realbits - 1);
817 ret = kmx61_set_power_state(data, false, chan->address);
819 mutex_unlock(&data->lock);
824 switch (chan->type) {
827 *val2 = kmx61_uscale_table[data->range];
835 return -EINVAL;
838 if (chan->type != IIO_ACCEL && chan->type != IIO_MAGN)
839 return -EINVAL;
841 mutex_lock(&data->lock);
842 ret = kmx61_get_odr(data, val, val2, chan->address);
843 mutex_unlock(&data->lock);
845 return -EINVAL;
848 return -EINVAL;
860 if (chan->type != IIO_ACCEL && chan->type != IIO_MAGN)
861 return -EINVAL;
863 mutex_lock(&data->lock);
864 ret = kmx61_set_odr(data, val, val2, chan->address);
865 mutex_unlock(&data->lock);
868 switch (chan->type) {
871 return -EINVAL;
872 mutex_lock(&data->lock);
874 mutex_unlock(&data->lock);
877 return -EINVAL;
880 return -EINVAL;
896 *val = data->wake_thresh;
899 *val = data->wake_duration;
902 return -EINVAL;
915 if (data->ev_enable_state)
916 return -EBUSY;
920 data->wake_thresh = val;
923 data->wake_duration = val;
926 return -EINVAL;
937 return data->ev_enable_state;
949 if (state && data->ev_enable_state)
952 mutex_lock(&data->lock);
954 if (!state && data->motion_trig_on) {
955 data->ev_enable_state = false;
969 data->ev_enable_state = state;
972 mutex_unlock(&data->lock);
982 if (data->acc_dready_trig != trig && data->motion_trig != trig)
983 return -EINVAL;
993 if (data->mag_dready_trig != trig)
994 return -EINVAL;
1027 mutex_lock(&data->lock);
1029 if (!state && data->ev_enable_state && data->motion_trig_on) {
1030 data->motion_trig_on = false;
1034 if (data->acc_dready_trig == trig || data->motion_trig == trig)
1043 if (data->acc_dready_trig == trig || data->mag_dready_trig == trig)
1052 if (data->acc_dready_trig == trig)
1053 data->acc_dready_trig_on = state;
1054 else if (data->mag_dready_trig == trig)
1055 data->mag_dready_trig_on = state;
1057 data->motion_trig_on = state;
1059 mutex_unlock(&data->lock);
1070 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_INL);
1072 dev_err(&data->client->dev, "Error reading reg_inl\n");
1083 struct iio_dev *indio_dev = data->acc_indio_dev;
1086 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_INS1);
1088 dev_err(&data->client->dev, "Error reading reg_ins1\n");
1093 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_INS2);
1095 dev_err(&data->client->dev, "Error reading reg_ins2\n");
1155 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_CTRL1);
1157 dev_err(&data->client->dev, "Error reading reg_ctrl1\n");
1160 ret = i2c_smbus_write_byte_data(data->client, KMX61_REG_CTRL1, ret);
1162 dev_err(&data->client->dev, "Error writing reg_ctrl1\n");
1164 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_INL);
1166 dev_err(&data->client->dev, "Error reading reg_inl\n");
1175 if (data->acc_dready_trig_on)
1176 iio_trigger_poll(data->acc_dready_trig);
1177 if (data->mag_dready_trig_on)
1178 iio_trigger_poll(data->mag_dready_trig);
1180 if (data->motion_trig_on)
1181 iio_trigger_poll(data->motion_trig);
1183 if (data->ev_enable_state)
1191 struct iio_dev *indio_dev = pf->indio_dev;
1197 if (indio_dev == data->acc_indio_dev)
1202 mutex_lock(&data->lock);
1203 for_each_set_bit(bit, indio_dev->active_scan_mask,
1204 indio_dev->masklength) {
1207 mutex_unlock(&data->lock);
1212 mutex_unlock(&data->lock);
1216 iio_trigger_notify_done(indio_dev->trig);
1225 id = acpi_match_device(dev->driver->acpi_match_table, dev);
1239 indio_dev = devm_iio_device_alloc(&data->client->dev, sizeof(data));
1241 return ERR_PTR(-ENOMEM);
1245 indio_dev->channels = chan;
1246 indio_dev->num_channels = num_channels;
1247 indio_dev->name = name;
1248 indio_dev->modes = INDIO_DIRECT_MODE;
1249 indio_dev->info = info;
1261 trig = devm_iio_trigger_alloc(&data->client->dev,
1262 "%s-%s-dev%d",
1263 indio_dev->name,
1267 return ERR_PTR(-ENOMEM);
1269 trig->ops = &kmx61_trigger_ops;
1286 data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
1288 return -ENOMEM;
1291 data->client = client;
1293 mutex_init(&data->lock);
1296 name = id->name;
1297 else if (ACPI_HANDLE(&client->dev))
1298 name = kmx61_match_acpi_device(&client->dev);
1300 return -ENODEV;
1302 data->acc_indio_dev =
1307 if (IS_ERR(data->acc_indio_dev))
1308 return PTR_ERR(data->acc_indio_dev);
1310 data->mag_indio_dev =
1315 if (IS_ERR(data->mag_indio_dev))
1316 return PTR_ERR(data->mag_indio_dev);
1322 if (client->irq > 0) {
1323 ret = devm_request_threaded_irq(&client->dev, client->irq,
1332 data->acc_dready_trig =
1333 kmx61_trigger_setup(data, data->acc_indio_dev,
1335 if (IS_ERR(data->acc_dready_trig)) {
1336 ret = PTR_ERR(data->acc_dready_trig);
1340 data->mag_dready_trig =
1341 kmx61_trigger_setup(data, data->mag_indio_dev,
1343 if (IS_ERR(data->mag_dready_trig)) {
1344 ret = PTR_ERR(data->mag_dready_trig);
1348 data->motion_trig =
1349 kmx61_trigger_setup(data, data->acc_indio_dev,
1350 "any-motion");
1351 if (IS_ERR(data->motion_trig)) {
1352 ret = PTR_ERR(data->motion_trig);
1356 ret = iio_triggered_buffer_setup(data->acc_indio_dev,
1361 dev_err(&data->client->dev,
1366 ret = iio_triggered_buffer_setup(data->mag_indio_dev,
1371 dev_err(&data->client->dev,
1377 ret = pm_runtime_set_active(&client->dev);
1381 pm_runtime_enable(&client->dev);
1382 pm_runtime_set_autosuspend_delay(&client->dev, KMX61_SLEEP_DELAY_MS);
1383 pm_runtime_use_autosuspend(&client->dev);
1385 ret = iio_device_register(data->acc_indio_dev);
1387 dev_err(&client->dev, "Failed to register acc iio device\n");
1391 ret = iio_device_register(data->mag_indio_dev);
1393 dev_err(&client->dev, "Failed to register mag iio device\n");
1400 iio_device_unregister(data->acc_indio_dev);
1402 pm_runtime_dont_use_autosuspend(&client->dev);
1403 pm_runtime_disable(&client->dev);
1405 if (client->irq > 0)
1406 iio_triggered_buffer_cleanup(data->mag_indio_dev);
1408 if (client->irq > 0)
1409 iio_triggered_buffer_cleanup(data->acc_indio_dev);
1411 iio_trigger_unregister(data->motion_trig);
1413 iio_trigger_unregister(data->mag_dready_trig);
1415 iio_trigger_unregister(data->acc_dready_trig);
1425 iio_device_unregister(data->acc_indio_dev);
1426 iio_device_unregister(data->mag_indio_dev);
1428 pm_runtime_disable(&client->dev);
1429 pm_runtime_set_suspended(&client->dev);
1431 if (client->irq > 0) {
1432 iio_triggered_buffer_cleanup(data->acc_indio_dev);
1433 iio_triggered_buffer_cleanup(data->mag_indio_dev);
1434 iio_trigger_unregister(data->acc_dready_trig);
1435 iio_trigger_unregister(data->mag_dready_trig);
1436 iio_trigger_unregister(data->motion_trig);
1439 mutex_lock(&data->lock);
1441 mutex_unlock(&data->lock);
1449 mutex_lock(&data->lock);
1452 mutex_unlock(&data->lock);
1462 if (data->acc_stby)
1464 if (data->mag_stby)
1475 mutex_lock(&data->lock);
1477 mutex_unlock(&data->lock);
1487 if (!data->acc_ps)
1489 if (!data->mag_ps)