2 * cros_ec_sensors - Driver for Chrome OS Embedded Controller sensors.
4 * Copyright (C) 2016 Google, Inc
6 * This software is licensed under the terms of the GNU General Public
7 * License version 2, as published by the Free Software Foundation, and
8 * may be copied, distributed, and modified under those terms.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * This driver uses the cros-ec interface to communicate with the Chrome OS
16 * EC about sensors data. Data access is presented through iio sysfs.
19 #include <linux/delay.h>
20 #include <linux/device.h>
21 #include <linux/iio/buffer.h>
22 #include <linux/iio/iio.h>
23 #include <linux/iio/kfifo_buf.h>
24 #include <linux/iio/trigger_consumer.h>
25 #include <linux/iio/triggered_buffer.h>
26 #include <linux/kernel.h>
27 #include <linux/mfd/cros_ec.h>
28 #include <linux/mfd/cros_ec_commands.h>
29 #include <linux/module.h>
30 #include <linux/platform_device.h>
31 #include <linux/slab.h>
32 #include <linux/sysfs.h>
34 #include "cros_ec_sensors_core.h"
36 #define CROS_EC_SENSORS_MAX_CHANNELS 4
38 /* State data for ec_sensors iio driver. */
39 struct cros_ec_sensors_state {
40 /* Shared by all sensors */
41 struct cros_ec_sensors_core_state core;
43 struct iio_chan_spec channels[CROS_EC_SENSORS_MAX_CHANNELS];
46 static int cros_ec_sensors_read(struct iio_dev *indio_dev,
47 struct iio_chan_spec const *chan,
48 int *val, int *val2, long mask)
50 struct cros_ec_sensors_state *st = iio_priv(indio_dev);
55 int idx = chan->scan_index;
57 mutex_lock(&st->core.cmd_lock);
60 case IIO_CHAN_INFO_RAW:
61 ret = st->core.read_ec_sensors_data(indio_dev, 1 << idx, &data);
67 case IIO_CHAN_INFO_CALIBBIAS:
68 st->core.param.cmd = MOTIONSENSE_CMD_SENSOR_OFFSET;
69 st->core.param.sensor_offset.flags = 0;
71 ret = cros_ec_motion_send_host_cmd(&st->core, 0);
76 for (i = CROS_EC_SENSOR_X; i < CROS_EC_SENSOR_MAX_AXIS; i++)
78 st->core.resp->sensor_offset.offset[i];
80 *val = st->core.calib[idx];
82 case IIO_CHAN_INFO_SCALE:
83 st->core.param.cmd = MOTIONSENSE_CMD_SENSOR_RANGE;
84 st->core.param.sensor_range.data = EC_MOTION_SENSE_NO_VALUE;
86 ret = cros_ec_motion_send_host_cmd(&st->core, 0);
90 val64 = st->core.resp->sensor_range.ret;
91 switch (st->core.type) {
92 case MOTIONSENSE_TYPE_ACCEL:
94 * EC returns data in g, iio exepects m/s^2.
95 * Do not use IIO_G_TO_M_S_2 to avoid precision loss.
97 *val = div_s64(val64 * 980665, 10);
98 *val2 = 10000 << (CROS_EC_SENSOR_BITS - 1);
99 ret = IIO_VAL_FRACTIONAL;
101 case MOTIONSENSE_TYPE_GYRO:
103 * EC returns data in dps, iio expects rad/s.
104 * Do not use IIO_DEGREE_TO_RAD to avoid precision
105 * loss. Round to the nearest integer.
108 *val2 = div_s64(val64 * 3141592653ULL,
109 180 << (CROS_EC_SENSOR_BITS - 1));
110 ret = IIO_VAL_INT_PLUS_NANO;
112 case MOTIONSENSE_TYPE_MAG:
114 * EC returns data in 16LSB / uT,
118 *val2 = 100 << (CROS_EC_SENSOR_BITS - 1);
119 ret = IIO_VAL_FRACTIONAL;
126 ret = cros_ec_sensors_core_read(&st->core, chan, val, val2,
130 mutex_unlock(&st->core.cmd_lock);
135 static int cros_ec_sensors_write(struct iio_dev *indio_dev,
136 struct iio_chan_spec const *chan,
137 int val, int val2, long mask)
139 struct cros_ec_sensors_state *st = iio_priv(indio_dev);
142 int idx = chan->scan_index;
144 mutex_lock(&st->core.cmd_lock);
147 case IIO_CHAN_INFO_CALIBBIAS:
148 st->core.calib[idx] = val;
150 /* Send to EC for each axis, even if not complete */
151 st->core.param.cmd = MOTIONSENSE_CMD_SENSOR_OFFSET;
152 st->core.param.sensor_offset.flags =
153 MOTION_SENSE_SET_OFFSET;
154 for (i = CROS_EC_SENSOR_X; i < CROS_EC_SENSOR_MAX_AXIS; i++)
155 st->core.param.sensor_offset.offset[i] =
157 st->core.param.sensor_offset.temp =
158 EC_MOTION_SENSE_INVALID_CALIB_TEMP;
160 ret = cros_ec_motion_send_host_cmd(&st->core, 0);
162 case IIO_CHAN_INFO_SCALE:
163 if (st->core.type == MOTIONSENSE_TYPE_MAG) {
167 st->core.param.cmd = MOTIONSENSE_CMD_SENSOR_RANGE;
168 st->core.param.sensor_range.data = val;
170 /* Always roundup, so caller gets at least what it asks for. */
171 st->core.param.sensor_range.roundup = 1;
173 ret = cros_ec_motion_send_host_cmd(&st->core, 0);
176 ret = cros_ec_sensors_core_write(
177 &st->core, chan, val, val2, mask);
181 mutex_unlock(&st->core.cmd_lock);
186 static const struct iio_info ec_sensors_info = {
187 .read_raw = &cros_ec_sensors_read,
188 .write_raw = &cros_ec_sensors_write,
189 .driver_module = THIS_MODULE,
192 static int cros_ec_sensors_probe(struct platform_device *pdev)
194 struct device *dev = &pdev->dev;
195 struct cros_ec_dev *ec_dev = dev_get_drvdata(dev->parent);
196 struct cros_ec_device *ec_device;
197 struct iio_dev *indio_dev;
198 struct cros_ec_sensors_state *state;
199 struct iio_chan_spec *channel;
202 if (!ec_dev || !ec_dev->ec_dev) {
203 dev_warn(&pdev->dev, "No CROS EC device found.\n");
206 ec_device = ec_dev->ec_dev;
208 indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*state));
212 ret = cros_ec_sensors_core_init(pdev, indio_dev, true);
216 indio_dev->info = &ec_sensors_info;
217 state = iio_priv(indio_dev);
218 for (channel = state->channels, i = CROS_EC_SENSOR_X;
219 i < CROS_EC_SENSOR_MAX_AXIS; i++, channel++) {
221 channel->info_mask_separate =
222 BIT(IIO_CHAN_INFO_RAW) |
223 BIT(IIO_CHAN_INFO_CALIBBIAS);
224 channel->info_mask_shared_by_all =
225 BIT(IIO_CHAN_INFO_SCALE) |
226 BIT(IIO_CHAN_INFO_FREQUENCY) |
227 BIT(IIO_CHAN_INFO_SAMP_FREQ);
228 channel->scan_type.realbits = CROS_EC_SENSOR_BITS;
229 channel->scan_type.storagebits = CROS_EC_SENSOR_BITS;
230 channel->scan_index = i;
231 channel->ext_info = cros_ec_sensors_ext_info;
232 channel->modified = 1;
233 channel->channel2 = IIO_MOD_X + i;
234 channel->scan_type.sign = 's';
236 /* Sensor specific */
237 switch (state->core.type) {
238 case MOTIONSENSE_TYPE_ACCEL:
239 channel->type = IIO_ACCEL;
241 case MOTIONSENSE_TYPE_GYRO:
242 channel->type = IIO_ANGL_VEL;
244 case MOTIONSENSE_TYPE_MAG:
245 channel->type = IIO_MAGN;
248 dev_err(&pdev->dev, "Unknown motion sensor\n");
254 channel->type = IIO_TIMESTAMP;
255 channel->channel = -1;
256 channel->scan_index = CROS_EC_SENSOR_MAX_AXIS;
257 channel->scan_type.sign = 's';
258 channel->scan_type.realbits = 64;
259 channel->scan_type.storagebits = 64;
261 indio_dev->channels = state->channels;
262 indio_dev->num_channels = CROS_EC_SENSORS_MAX_CHANNELS;
264 /* There is only enough room for accel and gyro in the io space */
265 if ((state->core.ec->cmd_readmem != NULL) &&
266 (state->core.type != MOTIONSENSE_TYPE_MAG))
267 state->core.read_ec_sensors_data = cros_ec_sensors_read_lpc;
269 state->core.read_ec_sensors_data = cros_ec_sensors_read_cmd;
271 ret = devm_iio_triggered_buffer_setup(dev, indio_dev, NULL,
272 cros_ec_sensors_capture, NULL);
276 return devm_iio_device_register(dev, indio_dev);
279 static const struct platform_device_id cros_ec_sensors_ids[] = {
281 .name = "cros-ec-accel",
284 .name = "cros-ec-gyro",
287 .name = "cros-ec-mag",
291 MODULE_DEVICE_TABLE(platform, cros_ec_sensors_ids);
293 static struct platform_driver cros_ec_sensors_platform_driver = {
295 .name = "cros-ec-sensors",
297 .probe = cros_ec_sensors_probe,
298 .id_table = cros_ec_sensors_ids,
300 module_platform_driver(cros_ec_sensors_platform_driver);
302 MODULE_DESCRIPTION("ChromeOS EC 3-axis sensors driver");
303 MODULE_LICENSE("GPL v2");