137aa055cSEnric Balletbo i Serra // SPDX-License-Identifier: GPL-2.0
211b86c70SGwendal Grignou /*
311b86c70SGwendal Grignou * Driver for older Chrome OS EC accelerometer
411b86c70SGwendal Grignou *
511b86c70SGwendal Grignou * Copyright 2017 Google, Inc
611b86c70SGwendal Grignou *
711b86c70SGwendal Grignou * This driver uses the memory mapper cros-ec interface to communicate
89566cb1dSGwendal Grignou * with the Chrome OS EC about accelerometer data or older commands.
911b86c70SGwendal Grignou * Accelerometer access is presented through iio sysfs.
1011b86c70SGwendal Grignou */
1111b86c70SGwendal Grignou
1211b86c70SGwendal Grignou #include <linux/delay.h>
1311b86c70SGwendal Grignou #include <linux/device.h>
1411b86c70SGwendal Grignou #include <linux/iio/buffer.h>
15d96e2677SGwendal Grignou #include <linux/iio/common/cros_ec_sensors_core.h>
1611b86c70SGwendal Grignou #include <linux/iio/iio.h>
1711b86c70SGwendal Grignou #include <linux/iio/kfifo_buf.h>
1811b86c70SGwendal Grignou #include <linux/iio/trigger_consumer.h>
1911b86c70SGwendal Grignou #include <linux/iio/triggered_buffer.h>
2011b86c70SGwendal Grignou #include <linux/kernel.h>
2111b86c70SGwendal Grignou #include <linux/module.h>
2211b86c70SGwendal Grignou #include <linux/slab.h>
23840d9f13SEnric Balletbo i Serra #include <linux/platform_data/cros_ec_commands.h>
24840d9f13SEnric Balletbo i Serra #include <linux/platform_data/cros_ec_proto.h>
2511b86c70SGwendal Grignou #include <linux/platform_device.h>
2611b86c70SGwendal Grignou
2711b86c70SGwendal Grignou #define DRV_NAME "cros-ec-accel-legacy"
2811b86c70SGwendal Grignou
29d96e2677SGwendal Grignou #define CROS_EC_SENSOR_LEGACY_NUM 2
3011b86c70SGwendal Grignou /*
3111b86c70SGwendal Grignou * Sensor scale hard coded at 10 bits per g, computed as:
3211b86c70SGwendal Grignou * g / (2^10 - 1) = 0.009586168; with g = 9.80665 m.s^-2
3311b86c70SGwendal Grignou */
3411b86c70SGwendal Grignou #define ACCEL_LEGACY_NSCALE 9586168
3511b86c70SGwendal Grignou
369ef38afdSGwendal Grignou /*
379ef38afdSGwendal Grignou * Sensor frequency is hard-coded to 10Hz.
389ef38afdSGwendal Grignou */
399ef38afdSGwendal Grignou static const int cros_ec_legacy_sample_freq[] = { 10, 0 };
409ef38afdSGwendal Grignou
cros_ec_accel_legacy_read_cmd(struct iio_dev * indio_dev,unsigned long scan_mask,s16 * data)419566cb1dSGwendal Grignou static int cros_ec_accel_legacy_read_cmd(struct iio_dev *indio_dev,
429566cb1dSGwendal Grignou unsigned long scan_mask, s16 *data)
439566cb1dSGwendal Grignou {
449566cb1dSGwendal Grignou struct cros_ec_sensors_core_state *st = iio_priv(indio_dev);
459566cb1dSGwendal Grignou int ret;
469566cb1dSGwendal Grignou unsigned int i;
479566cb1dSGwendal Grignou u8 sensor_num;
489566cb1dSGwendal Grignou
499566cb1dSGwendal Grignou /*
509566cb1dSGwendal Grignou * Read all sensor data through a command.
519566cb1dSGwendal Grignou * Save sensor_num, it is assumed to stay.
529566cb1dSGwendal Grignou */
539566cb1dSGwendal Grignou sensor_num = st->param.info.sensor_num;
549566cb1dSGwendal Grignou st->param.cmd = MOTIONSENSE_CMD_DUMP;
559566cb1dSGwendal Grignou st->param.dump.max_sensor_count = CROS_EC_SENSOR_LEGACY_NUM;
569566cb1dSGwendal Grignou ret = cros_ec_motion_send_host_cmd(st,
579566cb1dSGwendal Grignou sizeof(st->resp->dump) + CROS_EC_SENSOR_LEGACY_NUM *
589566cb1dSGwendal Grignou sizeof(struct ec_response_motion_sensor_data));
599566cb1dSGwendal Grignou st->param.info.sensor_num = sensor_num;
609566cb1dSGwendal Grignou if (ret != 0) {
619566cb1dSGwendal Grignou dev_warn(&indio_dev->dev, "Unable to read sensor data\n");
629566cb1dSGwendal Grignou return ret;
639566cb1dSGwendal Grignou }
649566cb1dSGwendal Grignou
65*bac3b7c4SNuno Sa for_each_set_bit(i, &scan_mask, iio_get_masklength(indio_dev)) {
669566cb1dSGwendal Grignou *data = st->resp->dump.sensor[sensor_num].data[i] *
679566cb1dSGwendal Grignou st->sign[i];
689566cb1dSGwendal Grignou data++;
699566cb1dSGwendal Grignou }
709566cb1dSGwendal Grignou
719566cb1dSGwendal Grignou return 0;
729566cb1dSGwendal Grignou }
739566cb1dSGwendal Grignou
cros_ec_accel_legacy_read(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int * val,int * val2,long mask)7411b86c70SGwendal Grignou static int cros_ec_accel_legacy_read(struct iio_dev *indio_dev,
7511b86c70SGwendal Grignou struct iio_chan_spec const *chan,
7611b86c70SGwendal Grignou int *val, int *val2, long mask)
7711b86c70SGwendal Grignou {
78d96e2677SGwendal Grignou struct cros_ec_sensors_core_state *st = iio_priv(indio_dev);
7911b86c70SGwendal Grignou s16 data = 0;
80d96e2677SGwendal Grignou int ret;
81d96e2677SGwendal Grignou int idx = chan->scan_index;
82d96e2677SGwendal Grignou
83d96e2677SGwendal Grignou mutex_lock(&st->cmd_lock);
8411b86c70SGwendal Grignou
8511b86c70SGwendal Grignou switch (mask) {
8611b86c70SGwendal Grignou case IIO_CHAN_INFO_RAW:
87d96e2677SGwendal Grignou ret = st->read_ec_sensors_data(indio_dev, 1 << idx, &data);
88d96e2677SGwendal Grignou if (ret < 0)
89d96e2677SGwendal Grignou break;
90d96e2677SGwendal Grignou ret = IIO_VAL_INT;
9111b86c70SGwendal Grignou *val = data;
92d96e2677SGwendal Grignou break;
9311b86c70SGwendal Grignou case IIO_CHAN_INFO_SCALE:
94d96e2677SGwendal Grignou WARN_ON(st->type != MOTIONSENSE_TYPE_ACCEL);
9511b86c70SGwendal Grignou *val = 0;
9611b86c70SGwendal Grignou *val2 = ACCEL_LEGACY_NSCALE;
97d96e2677SGwendal Grignou ret = IIO_VAL_INT_PLUS_NANO;
98d96e2677SGwendal Grignou break;
9911b86c70SGwendal Grignou case IIO_CHAN_INFO_CALIBBIAS:
10011b86c70SGwendal Grignou /* Calibration not supported. */
10111b86c70SGwendal Grignou *val = 0;
102d96e2677SGwendal Grignou ret = IIO_VAL_INT;
103d96e2677SGwendal Grignou break;
1049ef38afdSGwendal Grignou case IIO_CHAN_INFO_SAMP_FREQ:
1059ef38afdSGwendal Grignou *val = cros_ec_legacy_sample_freq[0];
1069ef38afdSGwendal Grignou *val2 = cros_ec_legacy_sample_freq[1];
1079ef38afdSGwendal Grignou ret = IIO_VAL_INT_PLUS_MICRO;
1089ef38afdSGwendal Grignou break;
10911b86c70SGwendal Grignou default:
110d96e2677SGwendal Grignou ret = cros_ec_sensors_core_read(st, chan, val, val2,
111d96e2677SGwendal Grignou mask);
112d96e2677SGwendal Grignou break;
11311b86c70SGwendal Grignou }
114d96e2677SGwendal Grignou mutex_unlock(&st->cmd_lock);
115d96e2677SGwendal Grignou
116d96e2677SGwendal Grignou return ret;
11711b86c70SGwendal Grignou }
11811b86c70SGwendal Grignou
cros_ec_accel_legacy_write(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int val,int val2,long mask)11911b86c70SGwendal Grignou static int cros_ec_accel_legacy_write(struct iio_dev *indio_dev,
12011b86c70SGwendal Grignou struct iio_chan_spec const *chan,
12111b86c70SGwendal Grignou int val, int val2, long mask)
12211b86c70SGwendal Grignou {
12311b86c70SGwendal Grignou /*
12411b86c70SGwendal Grignou * Do nothing but don't return an error code to allow calibration
12511b86c70SGwendal Grignou * script to work.
12611b86c70SGwendal Grignou */
12711b86c70SGwendal Grignou if (mask == IIO_CHAN_INFO_CALIBBIAS)
12811b86c70SGwendal Grignou return 0;
12911b86c70SGwendal Grignou
13011b86c70SGwendal Grignou return -EINVAL;
13111b86c70SGwendal Grignou }
13211b86c70SGwendal Grignou
1339ef38afdSGwendal Grignou /**
1349ef38afdSGwendal Grignou * cros_ec_accel_legacy_read_avail() - get available values
1359ef38afdSGwendal Grignou * @indio_dev: pointer to state information for device
1369ef38afdSGwendal Grignou * @chan: channel specification structure table
1379ef38afdSGwendal Grignou * @vals: list of available values
1389ef38afdSGwendal Grignou * @type: type of data returned
1399ef38afdSGwendal Grignou * @length: number of data returned in the array
1409ef38afdSGwendal Grignou * @mask: specifies which values to be requested
1419ef38afdSGwendal Grignou *
1429ef38afdSGwendal Grignou * Return: an error code or IIO_AVAIL_LIST
1439ef38afdSGwendal Grignou */
cros_ec_accel_legacy_read_avail(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,const int ** vals,int * type,int * length,long mask)1449ef38afdSGwendal Grignou static int cros_ec_accel_legacy_read_avail(struct iio_dev *indio_dev,
1459ef38afdSGwendal Grignou struct iio_chan_spec const *chan,
1469ef38afdSGwendal Grignou const int **vals,
1479ef38afdSGwendal Grignou int *type,
1489ef38afdSGwendal Grignou int *length,
1499ef38afdSGwendal Grignou long mask)
1509ef38afdSGwendal Grignou {
1519ef38afdSGwendal Grignou switch (mask) {
1529ef38afdSGwendal Grignou case IIO_CHAN_INFO_SAMP_FREQ:
1539ef38afdSGwendal Grignou *length = ARRAY_SIZE(cros_ec_legacy_sample_freq);
1549ef38afdSGwendal Grignou *vals = cros_ec_legacy_sample_freq;
1559ef38afdSGwendal Grignou *type = IIO_VAL_INT_PLUS_MICRO;
1569ef38afdSGwendal Grignou return IIO_AVAIL_LIST;
1579ef38afdSGwendal Grignou }
1589ef38afdSGwendal Grignou
1599ef38afdSGwendal Grignou return -EINVAL;
1609ef38afdSGwendal Grignou }
1619ef38afdSGwendal Grignou
16211b86c70SGwendal Grignou static const struct iio_info cros_ec_accel_legacy_info = {
16311b86c70SGwendal Grignou .read_raw = &cros_ec_accel_legacy_read,
16411b86c70SGwendal Grignou .write_raw = &cros_ec_accel_legacy_write,
1659ef38afdSGwendal Grignou .read_avail = &cros_ec_accel_legacy_read_avail,
16611b86c70SGwendal Grignou };
16711b86c70SGwendal Grignou
16811b86c70SGwendal Grignou /*
169d96e2677SGwendal Grignou * Present the channel using HTML5 standard:
170d96e2677SGwendal Grignou * need to invert X and Y and invert some lid axis.
17111b86c70SGwendal Grignou */
172d96e2677SGwendal Grignou #define CROS_EC_ACCEL_ROTATE_AXIS(_axis) \
173d96e2677SGwendal Grignou ((_axis) == CROS_EC_SENSOR_Z ? CROS_EC_SENSOR_Z : \
174d96e2677SGwendal Grignou ((_axis) == CROS_EC_SENSOR_X ? CROS_EC_SENSOR_Y : \
175d96e2677SGwendal Grignou CROS_EC_SENSOR_X))
17611b86c70SGwendal Grignou
17711b86c70SGwendal Grignou #define CROS_EC_ACCEL_LEGACY_CHAN(_axis) \
17811b86c70SGwendal Grignou { \
17911b86c70SGwendal Grignou .type = IIO_ACCEL, \
18011b86c70SGwendal Grignou .channel2 = IIO_MOD_X + (_axis), \
18111b86c70SGwendal Grignou .modified = 1, \
18211b86c70SGwendal Grignou .info_mask_separate = \
18311b86c70SGwendal Grignou BIT(IIO_CHAN_INFO_RAW) | \
18411b86c70SGwendal Grignou BIT(IIO_CHAN_INFO_CALIBBIAS), \
1859ef38afdSGwendal Grignou .info_mask_shared_by_all = \
1869ef38afdSGwendal Grignou BIT(IIO_CHAN_INFO_SCALE) | \
1879ef38afdSGwendal Grignou BIT(IIO_CHAN_INFO_SAMP_FREQ), \
1889ef38afdSGwendal Grignou .info_mask_shared_by_all_available = \
1899ef38afdSGwendal Grignou BIT(IIO_CHAN_INFO_SAMP_FREQ), \
190d96e2677SGwendal Grignou .ext_info = cros_ec_sensors_ext_info, \
19111b86c70SGwendal Grignou .scan_type = { \
19211b86c70SGwendal Grignou .sign = 's', \
193d96e2677SGwendal Grignou .realbits = CROS_EC_SENSOR_BITS, \
194d96e2677SGwendal Grignou .storagebits = CROS_EC_SENSOR_BITS, \
19511b86c70SGwendal Grignou }, \
196d96e2677SGwendal Grignou .scan_index = CROS_EC_ACCEL_ROTATE_AXIS(_axis), \
19711b86c70SGwendal Grignou } \
19811b86c70SGwendal Grignou
199d96e2677SGwendal Grignou static const struct iio_chan_spec cros_ec_accel_legacy_channels[] = {
200d96e2677SGwendal Grignou CROS_EC_ACCEL_LEGACY_CHAN(CROS_EC_SENSOR_X),
201d96e2677SGwendal Grignou CROS_EC_ACCEL_LEGACY_CHAN(CROS_EC_SENSOR_Y),
202d96e2677SGwendal Grignou CROS_EC_ACCEL_LEGACY_CHAN(CROS_EC_SENSOR_Z),
203d96e2677SGwendal Grignou IIO_CHAN_SOFT_TIMESTAMP(CROS_EC_SENSOR_MAX_AXIS)
20411b86c70SGwendal Grignou };
20511b86c70SGwendal Grignou
cros_ec_accel_legacy_probe(struct platform_device * pdev)20611b86c70SGwendal Grignou static int cros_ec_accel_legacy_probe(struct platform_device *pdev)
20711b86c70SGwendal Grignou {
20811b86c70SGwendal Grignou struct device *dev = &pdev->dev;
20911b86c70SGwendal Grignou struct iio_dev *indio_dev;
210d96e2677SGwendal Grignou struct cros_ec_sensors_core_state *state;
21174c420e0SGustavo A. R. Silva int ret;
21211b86c70SGwendal Grignou
21311b86c70SGwendal Grignou indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*state));
21411b86c70SGwendal Grignou if (!indio_dev)
21511b86c70SGwendal Grignou return -ENOMEM;
21611b86c70SGwendal Grignou
217aa984f1bSGwendal Grignou ret = cros_ec_sensors_core_init(pdev, indio_dev, true,
2180b4ae3f6SGwendal Grignou cros_ec_sensors_capture);
219d96e2677SGwendal Grignou if (ret)
220d96e2677SGwendal Grignou return ret;
22111b86c70SGwendal Grignou
22211b86c70SGwendal Grignou indio_dev->info = &cros_ec_accel_legacy_info;
223d96e2677SGwendal Grignou state = iio_priv(indio_dev);
224d96e2677SGwendal Grignou
2259566cb1dSGwendal Grignou if (state->ec->cmd_readmem != NULL)
226d96e2677SGwendal Grignou state->read_ec_sensors_data = cros_ec_sensors_read_lpc;
2279566cb1dSGwendal Grignou else
2289566cb1dSGwendal Grignou state->read_ec_sensors_data = cros_ec_accel_legacy_read_cmd;
229d96e2677SGwendal Grignou
230d96e2677SGwendal Grignou indio_dev->channels = cros_ec_accel_legacy_channels;
231d96e2677SGwendal Grignou indio_dev->num_channels = ARRAY_SIZE(cros_ec_accel_legacy_channels);
232d96e2677SGwendal Grignou /* The lid sensor needs to be presented inverted. */
2337cbb6681SGwendal Grignou if (!strcmp(indio_dev->label, "accel-display")) {
234d96e2677SGwendal Grignou state->sign[CROS_EC_SENSOR_X] = -1;
235d96e2677SGwendal Grignou state->sign[CROS_EC_SENSOR_Z] = -1;
236d96e2677SGwendal Grignou }
23711b86c70SGwendal Grignou
2380b4ae3f6SGwendal Grignou return cros_ec_sensors_core_register(dev, indio_dev, NULL);
23911b86c70SGwendal Grignou }
24011b86c70SGwendal Grignou
24111b86c70SGwendal Grignou static struct platform_driver cros_ec_accel_platform_driver = {
24211b86c70SGwendal Grignou .driver = {
24311b86c70SGwendal Grignou .name = DRV_NAME,
24411b86c70SGwendal Grignou },
24511b86c70SGwendal Grignou .probe = cros_ec_accel_legacy_probe,
24611b86c70SGwendal Grignou };
24711b86c70SGwendal Grignou module_platform_driver(cros_ec_accel_platform_driver);
24811b86c70SGwendal Grignou
24911b86c70SGwendal Grignou MODULE_DESCRIPTION("ChromeOS EC legacy accelerometer driver");
25011b86c70SGwendal Grignou MODULE_AUTHOR("Gwendal Grignou <gwendal@chromium.org>");
25137aa055cSEnric Balletbo i Serra MODULE_LICENSE("GPL v2");
25211b86c70SGwendal Grignou MODULE_ALIAS("platform:" DRV_NAME);
253