1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Driver for older Chrome OS EC accelerometer
4  *
5  * Copyright 2017 Google, Inc
6  *
7  * This driver uses the memory mapper cros-ec interface to communicate
8  * with the Chrome OS EC about accelerometer data or older commands.
9  * Accelerometer access is presented through iio sysfs.
10  */
11 
12 #include <linux/delay.h>
13 #include <linux/device.h>
14 #include <linux/iio/buffer.h>
15 #include <linux/iio/common/cros_ec_sensors_core.h>
16 #include <linux/iio/iio.h>
17 #include <linux/iio/kfifo_buf.h>
18 #include <linux/iio/trigger_consumer.h>
19 #include <linux/iio/triggered_buffer.h>
20 #include <linux/kernel.h>
21 #include <linux/module.h>
22 #include <linux/slab.h>
23 #include <linux/platform_data/cros_ec_commands.h>
24 #include <linux/platform_data/cros_ec_proto.h>
25 #include <linux/platform_device.h>
26 
27 #define DRV_NAME	"cros-ec-accel-legacy"
28 
29 #define CROS_EC_SENSOR_LEGACY_NUM 2
30 /*
31  * Sensor scale hard coded at 10 bits per g, computed as:
32  * g / (2^10 - 1) = 0.009586168; with g = 9.80665 m.s^-2
33  */
34 #define ACCEL_LEGACY_NSCALE 9586168
35 
36 static int cros_ec_accel_legacy_read_cmd(struct iio_dev *indio_dev,
37 				  unsigned long scan_mask, s16 *data)
38 {
39 	struct cros_ec_sensors_core_state *st = iio_priv(indio_dev);
40 	int ret;
41 	unsigned int i;
42 	u8 sensor_num;
43 
44 	/*
45 	 * Read all sensor data through a command.
46 	 * Save sensor_num, it is assumed to stay.
47 	 */
48 	sensor_num = st->param.info.sensor_num;
49 	st->param.cmd = MOTIONSENSE_CMD_DUMP;
50 	st->param.dump.max_sensor_count = CROS_EC_SENSOR_LEGACY_NUM;
51 	ret = cros_ec_motion_send_host_cmd(st,
52 			sizeof(st->resp->dump) + CROS_EC_SENSOR_LEGACY_NUM *
53 			sizeof(struct ec_response_motion_sensor_data));
54 	st->param.info.sensor_num = sensor_num;
55 	if (ret != 0) {
56 		dev_warn(&indio_dev->dev, "Unable to read sensor data\n");
57 		return ret;
58 	}
59 
60 	for_each_set_bit(i, &scan_mask, indio_dev->masklength) {
61 		*data = st->resp->dump.sensor[sensor_num].data[i] *
62 			st->sign[i];
63 		data++;
64 	}
65 
66 	return 0;
67 }
68 
69 static int cros_ec_accel_legacy_read(struct iio_dev *indio_dev,
70 				     struct iio_chan_spec const *chan,
71 				     int *val, int *val2, long mask)
72 {
73 	struct cros_ec_sensors_core_state *st = iio_priv(indio_dev);
74 	s16 data = 0;
75 	int ret;
76 	int idx = chan->scan_index;
77 
78 	mutex_lock(&st->cmd_lock);
79 
80 	switch (mask) {
81 	case IIO_CHAN_INFO_RAW:
82 		ret = st->read_ec_sensors_data(indio_dev, 1 << idx, &data);
83 		if (ret < 0)
84 			break;
85 		ret = IIO_VAL_INT;
86 		*val = data;
87 		break;
88 	case IIO_CHAN_INFO_SCALE:
89 		WARN_ON(st->type != MOTIONSENSE_TYPE_ACCEL);
90 		*val = 0;
91 		*val2 = ACCEL_LEGACY_NSCALE;
92 		ret = IIO_VAL_INT_PLUS_NANO;
93 		break;
94 	case IIO_CHAN_INFO_CALIBBIAS:
95 		/* Calibration not supported. */
96 		*val = 0;
97 		ret = IIO_VAL_INT;
98 		break;
99 	default:
100 		ret = cros_ec_sensors_core_read(st, chan, val, val2,
101 				mask);
102 		break;
103 	}
104 	mutex_unlock(&st->cmd_lock);
105 
106 	return ret;
107 }
108 
109 static int cros_ec_accel_legacy_write(struct iio_dev *indio_dev,
110 				      struct iio_chan_spec const *chan,
111 				      int val, int val2, long mask)
112 {
113 	/*
114 	 * Do nothing but don't return an error code to allow calibration
115 	 * script to work.
116 	 */
117 	if (mask == IIO_CHAN_INFO_CALIBBIAS)
118 		return 0;
119 
120 	return -EINVAL;
121 }
122 
123 static const struct iio_info cros_ec_accel_legacy_info = {
124 	.read_raw = &cros_ec_accel_legacy_read,
125 	.write_raw = &cros_ec_accel_legacy_write,
126 };
127 
128 /*
129  * Present the channel using HTML5 standard:
130  * need to invert X and Y and invert some lid axis.
131  */
132 #define CROS_EC_ACCEL_ROTATE_AXIS(_axis)				\
133 	((_axis) == CROS_EC_SENSOR_Z ? CROS_EC_SENSOR_Z :		\
134 	 ((_axis) == CROS_EC_SENSOR_X ? CROS_EC_SENSOR_Y :		\
135 	  CROS_EC_SENSOR_X))
136 
137 #define CROS_EC_ACCEL_LEGACY_CHAN(_axis)				\
138 	{								\
139 		.type = IIO_ACCEL,					\
140 		.channel2 = IIO_MOD_X + (_axis),			\
141 		.modified = 1,					        \
142 		.info_mask_separate =					\
143 			BIT(IIO_CHAN_INFO_RAW) |			\
144 			BIT(IIO_CHAN_INFO_CALIBBIAS),			\
145 		.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SCALE),	\
146 		.ext_info = cros_ec_sensors_ext_info,			\
147 		.scan_type = {						\
148 			.sign = 's',					\
149 			.realbits = CROS_EC_SENSOR_BITS,		\
150 			.storagebits = CROS_EC_SENSOR_BITS,		\
151 		},							\
152 		.scan_index = CROS_EC_ACCEL_ROTATE_AXIS(_axis),		\
153 	}								\
154 
155 static const struct iio_chan_spec cros_ec_accel_legacy_channels[] = {
156 		CROS_EC_ACCEL_LEGACY_CHAN(CROS_EC_SENSOR_X),
157 		CROS_EC_ACCEL_LEGACY_CHAN(CROS_EC_SENSOR_Y),
158 		CROS_EC_ACCEL_LEGACY_CHAN(CROS_EC_SENSOR_Z),
159 		IIO_CHAN_SOFT_TIMESTAMP(CROS_EC_SENSOR_MAX_AXIS)
160 };
161 
162 static int cros_ec_accel_legacy_probe(struct platform_device *pdev)
163 {
164 	struct device *dev = &pdev->dev;
165 	struct iio_dev *indio_dev;
166 	struct cros_ec_sensors_core_state *state;
167 	int ret;
168 
169 	indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*state));
170 	if (!indio_dev)
171 		return -ENOMEM;
172 
173 	ret = cros_ec_sensors_core_init(pdev, indio_dev, true);
174 	if (ret)
175 		return ret;
176 
177 	indio_dev->info = &cros_ec_accel_legacy_info;
178 	state = iio_priv(indio_dev);
179 
180 	if (state->ec->cmd_readmem != NULL)
181 		state->read_ec_sensors_data = cros_ec_sensors_read_lpc;
182 	else
183 		state->read_ec_sensors_data = cros_ec_accel_legacy_read_cmd;
184 
185 	indio_dev->channels = cros_ec_accel_legacy_channels;
186 	indio_dev->num_channels = ARRAY_SIZE(cros_ec_accel_legacy_channels);
187 	/* The lid sensor needs to be presented inverted. */
188 	if (state->loc == MOTIONSENSE_LOC_LID) {
189 		state->sign[CROS_EC_SENSOR_X] = -1;
190 		state->sign[CROS_EC_SENSOR_Z] = -1;
191 	}
192 
193 	ret = devm_iio_triggered_buffer_setup(dev, indio_dev, NULL,
194 			cros_ec_sensors_capture, NULL);
195 	if (ret)
196 		return ret;
197 
198 	return devm_iio_device_register(dev, indio_dev);
199 }
200 
201 static struct platform_driver cros_ec_accel_platform_driver = {
202 	.driver = {
203 		.name	= DRV_NAME,
204 	},
205 	.probe		= cros_ec_accel_legacy_probe,
206 };
207 module_platform_driver(cros_ec_accel_platform_driver);
208 
209 MODULE_DESCRIPTION("ChromeOS EC legacy accelerometer driver");
210 MODULE_AUTHOR("Gwendal Grignou <gwendal@chromium.org>");
211 MODULE_LICENSE("GPL v2");
212 MODULE_ALIAS("platform:" DRV_NAME);
213