1 // SPDX-License-Identifier: GPL-2.0
3 * cros_ec_sensors_core - Common function for Chrome OS EC sensor driver.
5 * Copyright (C) 2016 Google, Inc
8 #include <linux/delay.h>
9 #include <linux/device.h>
10 #include <linux/iio/buffer.h>
11 #include <linux/iio/common/cros_ec_sensors_core.h>
12 #include <linux/iio/iio.h>
13 #include <linux/iio/kfifo_buf.h>
14 #include <linux/iio/trigger_consumer.h>
15 #include <linux/kernel.h>
16 #include <linux/mfd/cros_ec.h>
17 #include <linux/mfd/cros_ec_commands.h>
18 #include <linux/module.h>
19 #include <linux/slab.h>
20 #include <linux/platform_device.h>
22 static char *cros_ec_loc[] = {
23 [MOTIONSENSE_LOC_BASE] = "base",
24 [MOTIONSENSE_LOC_LID] = "lid",
25 [MOTIONSENSE_LOC_MAX] = "unknown",
28 int cros_ec_sensors_core_init(struct platform_device *pdev,
29 struct iio_dev *indio_dev,
32 struct device *dev = &pdev->dev;
33 struct cros_ec_sensors_core_state *state = iio_priv(indio_dev);
34 struct cros_ec_dev *ec = dev_get_drvdata(pdev->dev.parent);
35 struct cros_ec_sensor_platform *sensor_platform = dev_get_platdata(dev);
37 platform_set_drvdata(pdev, indio_dev);
39 state->ec = ec->ec_dev;
40 state->msg = devm_kzalloc(&pdev->dev,
41 max((u16)sizeof(struct ec_params_motion_sense),
42 state->ec->max_response), GFP_KERNEL);
46 state->resp = (struct ec_response_motion_sense *)state->msg->data;
48 mutex_init(&state->cmd_lock);
50 /* Set up the host command structure. */
51 state->msg->version = 2;
52 state->msg->command = EC_CMD_MOTION_SENSE_CMD + ec->cmd_offset;
53 state->msg->outsize = sizeof(struct ec_params_motion_sense);
55 indio_dev->dev.parent = &pdev->dev;
56 indio_dev->name = pdev->name;
58 if (physical_device) {
59 indio_dev->modes = INDIO_DIRECT_MODE;
61 state->param.cmd = MOTIONSENSE_CMD_INFO;
62 state->param.info.sensor_num = sensor_platform->sensor_num;
63 if (cros_ec_motion_send_host_cmd(state, 0)) {
64 dev_warn(dev, "Can not access sensor info\n");
67 state->type = state->resp->info.type;
68 state->loc = state->resp->info.location;
73 EXPORT_SYMBOL_GPL(cros_ec_sensors_core_init);
75 int cros_ec_motion_send_host_cmd(struct cros_ec_sensors_core_state *state,
81 state->msg->insize = min(opt_length, state->ec->max_response);
83 state->msg->insize = state->ec->max_response;
85 memcpy(state->msg->data, &state->param, sizeof(state->param));
87 ret = cros_ec_cmd_xfer_status(state->ec, state->msg);
92 state->resp != (struct ec_response_motion_sense *)state->msg->data)
93 memcpy(state->resp, state->msg->data, ret);
97 EXPORT_SYMBOL_GPL(cros_ec_motion_send_host_cmd);
99 static ssize_t cros_ec_sensors_calibrate(struct iio_dev *indio_dev,
100 uintptr_t private, const struct iio_chan_spec *chan,
101 const char *buf, size_t len)
103 struct cros_ec_sensors_core_state *st = iio_priv(indio_dev);
107 ret = strtobool(buf, &calibrate);
113 mutex_lock(&st->cmd_lock);
114 st->param.cmd = MOTIONSENSE_CMD_PERFORM_CALIB;
115 ret = cros_ec_motion_send_host_cmd(st, 0);
117 dev_warn(&indio_dev->dev, "Unable to calibrate sensor\n");
120 for (i = CROS_EC_SENSOR_X; i < CROS_EC_SENSOR_MAX_AXIS; i++)
121 st->calib[i] = st->resp->perform_calib.offset[i];
123 mutex_unlock(&st->cmd_lock);
125 return ret ? ret : len;
128 static ssize_t cros_ec_sensors_id(struct iio_dev *indio_dev,
130 const struct iio_chan_spec *chan, char *buf)
132 struct cros_ec_sensors_core_state *st = iio_priv(indio_dev);
134 return snprintf(buf, PAGE_SIZE, "%d\n", st->param.info.sensor_num);
137 static ssize_t cros_ec_sensors_loc(struct iio_dev *indio_dev,
138 uintptr_t private, const struct iio_chan_spec *chan,
141 struct cros_ec_sensors_core_state *st = iio_priv(indio_dev);
143 return snprintf(buf, PAGE_SIZE, "%s\n", cros_ec_loc[st->loc]);
146 const struct iio_chan_spec_ext_info cros_ec_sensors_ext_info[] = {
149 .shared = IIO_SHARED_BY_ALL,
150 .write = cros_ec_sensors_calibrate
154 .shared = IIO_SHARED_BY_ALL,
155 .read = cros_ec_sensors_id
159 .shared = IIO_SHARED_BY_ALL,
160 .read = cros_ec_sensors_loc
164 EXPORT_SYMBOL_GPL(cros_ec_sensors_ext_info);
167 * cros_ec_sensors_idx_to_reg - convert index into offset in shared memory
168 * @st: pointer to state information for device
169 * @idx: sensor index (should be element of enum sensor_index)
171 * Return: address to read at
173 static unsigned int cros_ec_sensors_idx_to_reg(
174 struct cros_ec_sensors_core_state *st,
178 * When using LPC interface, only space for 2 Accel and one Gyro.
179 * First halfword of MOTIONSENSE_TYPE_ACCEL is used by angle.
181 if (st->type == MOTIONSENSE_TYPE_ACCEL)
182 return EC_MEMMAP_ACC_DATA + sizeof(u16) *
183 (1 + idx + st->param.info.sensor_num *
184 CROS_EC_SENSOR_MAX_AXIS);
186 return EC_MEMMAP_GYRO_DATA + sizeof(u16) * idx;
189 static int cros_ec_sensors_cmd_read_u8(struct cros_ec_device *ec,
190 unsigned int offset, u8 *dest)
192 return ec->cmd_readmem(ec, offset, 1, dest);
195 static int cros_ec_sensors_cmd_read_u16(struct cros_ec_device *ec,
196 unsigned int offset, u16 *dest)
199 int ret = ec->cmd_readmem(ec, offset, 2, &tmp);
202 *dest = le16_to_cpu(tmp);
208 * cros_ec_sensors_read_until_not_busy() - read until is not busy
210 * @st: pointer to state information for device
212 * Read from EC status byte until it reads not busy.
213 * Return: 8-bit status if ok, -errno on failure.
215 static int cros_ec_sensors_read_until_not_busy(
216 struct cros_ec_sensors_core_state *st)
218 struct cros_ec_device *ec = st->ec;
220 int ret, attempts = 0;
222 ret = cros_ec_sensors_cmd_read_u8(ec, EC_MEMMAP_ACC_STATUS, &status);
226 while (status & EC_MEMMAP_ACC_STATUS_BUSY_BIT) {
227 /* Give up after enough attempts, return error. */
228 if (attempts++ >= 50)
231 /* Small delay every so often. */
232 if (attempts % 5 == 0)
235 ret = cros_ec_sensors_cmd_read_u8(ec, EC_MEMMAP_ACC_STATUS,
245 * read_ec_sensors_data_unsafe() - read acceleration data from EC shared memory
246 * @indio_dev: pointer to IIO device
247 * @scan_mask: bitmap of the sensor indices to scan
248 * @data: location to store data
250 * This is the unsafe function for reading the EC data. It does not guarantee
251 * that the EC will not modify the data as it is being read in.
253 * Return: 0 on success, -errno on failure.
255 static int cros_ec_sensors_read_data_unsafe(struct iio_dev *indio_dev,
256 unsigned long scan_mask, s16 *data)
258 struct cros_ec_sensors_core_state *st = iio_priv(indio_dev);
259 struct cros_ec_device *ec = st->ec;
263 /* Read all sensors enabled in scan_mask. Each value is 2 bytes. */
264 for_each_set_bit(i, &scan_mask, indio_dev->masklength) {
265 ret = cros_ec_sensors_cmd_read_u16(ec,
266 cros_ec_sensors_idx_to_reg(st, i),
278 * cros_ec_sensors_read_lpc() - read acceleration data from EC shared memory.
279 * @indio_dev: pointer to IIO device.
280 * @scan_mask: bitmap of the sensor indices to scan.
281 * @data: location to store data.
283 * Note: this is the safe function for reading the EC data. It guarantees
284 * that the data sampled was not modified by the EC while being read.
286 * Return: 0 on success, -errno on failure.
288 int cros_ec_sensors_read_lpc(struct iio_dev *indio_dev,
289 unsigned long scan_mask, s16 *data)
291 struct cros_ec_sensors_core_state *st = iio_priv(indio_dev);
292 struct cros_ec_device *ec = st->ec;
293 u8 samp_id = 0xff, status = 0;
294 int ret, attempts = 0;
297 * Continually read all data from EC until the status byte after
298 * all reads reflects that the EC is not busy and the sample id
299 * matches the sample id from before all reads. This guarantees
300 * that data read in was not modified by the EC while reading.
302 while ((status & (EC_MEMMAP_ACC_STATUS_BUSY_BIT |
303 EC_MEMMAP_ACC_STATUS_SAMPLE_ID_MASK)) != samp_id) {
304 /* If we have tried to read too many times, return error. */
308 /* Read status byte until EC is not busy. */
309 ret = cros_ec_sensors_read_until_not_busy(st);
314 * Store the current sample id so that we can compare to the
315 * sample id after reading the data.
317 samp_id = ret & EC_MEMMAP_ACC_STATUS_SAMPLE_ID_MASK;
319 /* Read all EC data, format it, and store it into data. */
320 ret = cros_ec_sensors_read_data_unsafe(indio_dev, scan_mask,
325 /* Read status byte. */
326 ret = cros_ec_sensors_cmd_read_u8(ec, EC_MEMMAP_ACC_STATUS,
334 EXPORT_SYMBOL_GPL(cros_ec_sensors_read_lpc);
336 int cros_ec_sensors_read_cmd(struct iio_dev *indio_dev,
337 unsigned long scan_mask, s16 *data)
339 struct cros_ec_sensors_core_state *st = iio_priv(indio_dev);
343 /* Read all sensor data through a command. */
344 st->param.cmd = MOTIONSENSE_CMD_DATA;
345 ret = cros_ec_motion_send_host_cmd(st, sizeof(st->resp->data));
347 dev_warn(&indio_dev->dev, "Unable to read sensor data\n");
351 for_each_set_bit(i, &scan_mask, indio_dev->masklength) {
352 *data = st->resp->data.data[i];
358 EXPORT_SYMBOL_GPL(cros_ec_sensors_read_cmd);
360 irqreturn_t cros_ec_sensors_capture(int irq, void *p)
362 struct iio_poll_func *pf = p;
363 struct iio_dev *indio_dev = pf->indio_dev;
364 struct cros_ec_sensors_core_state *st = iio_priv(indio_dev);
367 mutex_lock(&st->cmd_lock);
369 /* Clear capture data. */
370 memset(st->samples, 0, indio_dev->scan_bytes);
372 /* Read data based on which channels are enabled in scan mask. */
373 ret = st->read_ec_sensors_data(indio_dev,
374 *(indio_dev->active_scan_mask),
379 iio_push_to_buffers_with_timestamp(indio_dev, st->samples,
380 iio_get_time_ns(indio_dev));
384 * Tell the core we are done with this trigger and ready for the
387 iio_trigger_notify_done(indio_dev->trig);
389 mutex_unlock(&st->cmd_lock);
393 EXPORT_SYMBOL_GPL(cros_ec_sensors_capture);
395 int cros_ec_sensors_core_read(struct cros_ec_sensors_core_state *st,
396 struct iio_chan_spec const *chan,
397 int *val, int *val2, long mask)
399 int ret = IIO_VAL_INT;
402 case IIO_CHAN_INFO_SAMP_FREQ:
403 st->param.cmd = MOTIONSENSE_CMD_EC_RATE;
404 st->param.ec_rate.data =
405 EC_MOTION_SENSE_NO_VALUE;
407 if (cros_ec_motion_send_host_cmd(st, 0))
410 *val = st->resp->ec_rate.ret;
412 case IIO_CHAN_INFO_FREQUENCY:
413 st->param.cmd = MOTIONSENSE_CMD_SENSOR_ODR;
414 st->param.sensor_odr.data =
415 EC_MOTION_SENSE_NO_VALUE;
417 if (cros_ec_motion_send_host_cmd(st, 0))
420 *val = st->resp->sensor_odr.ret;
428 EXPORT_SYMBOL_GPL(cros_ec_sensors_core_read);
430 int cros_ec_sensors_core_write(struct cros_ec_sensors_core_state *st,
431 struct iio_chan_spec const *chan,
432 int val, int val2, long mask)
437 case IIO_CHAN_INFO_FREQUENCY:
438 st->param.cmd = MOTIONSENSE_CMD_SENSOR_ODR;
439 st->param.sensor_odr.data = val;
441 /* Always roundup, so caller gets at least what it asks for. */
442 st->param.sensor_odr.roundup = 1;
444 if (cros_ec_motion_send_host_cmd(st, 0))
447 case IIO_CHAN_INFO_SAMP_FREQ:
448 st->param.cmd = MOTIONSENSE_CMD_EC_RATE;
449 st->param.ec_rate.data = val;
451 if (cros_ec_motion_send_host_cmd(st, 0))
454 st->curr_sampl_freq = val;
462 EXPORT_SYMBOL_GPL(cros_ec_sensors_core_write);
464 static int __maybe_unused cros_ec_sensors_prepare(struct device *dev)
466 struct iio_dev *indio_dev = dev_get_drvdata(dev);
467 struct cros_ec_sensors_core_state *st = iio_priv(indio_dev);
469 if (st->curr_sampl_freq == 0)
473 * If the sensors are sampled at high frequency, we will not be able to
474 * sleep. Set sampling to a long period if necessary.
476 if (st->curr_sampl_freq < CROS_EC_MIN_SUSPEND_SAMPLING_FREQUENCY) {
477 mutex_lock(&st->cmd_lock);
478 st->param.cmd = MOTIONSENSE_CMD_EC_RATE;
479 st->param.ec_rate.data = CROS_EC_MIN_SUSPEND_SAMPLING_FREQUENCY;
480 cros_ec_motion_send_host_cmd(st, 0);
481 mutex_unlock(&st->cmd_lock);
486 static void __maybe_unused cros_ec_sensors_complete(struct device *dev)
488 struct iio_dev *indio_dev = dev_get_drvdata(dev);
489 struct cros_ec_sensors_core_state *st = iio_priv(indio_dev);
491 if (st->curr_sampl_freq == 0)
494 if (st->curr_sampl_freq < CROS_EC_MIN_SUSPEND_SAMPLING_FREQUENCY) {
495 mutex_lock(&st->cmd_lock);
496 st->param.cmd = MOTIONSENSE_CMD_EC_RATE;
497 st->param.ec_rate.data = st->curr_sampl_freq;
498 cros_ec_motion_send_host_cmd(st, 0);
499 mutex_unlock(&st->cmd_lock);
503 const struct dev_pm_ops cros_ec_sensors_pm_ops = {
504 #ifdef CONFIG_PM_SLEEP
505 .prepare = cros_ec_sensors_prepare,
506 .complete = cros_ec_sensors_complete
509 EXPORT_SYMBOL_GPL(cros_ec_sensors_pm_ops);
511 MODULE_DESCRIPTION("ChromeOS EC sensor hub core functions");
512 MODULE_LICENSE("GPL v2");