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/module.h>
18 #include <linux/slab.h>
19 #include <linux/platform_data/cros_ec_commands.h>
20 #include <linux/platform_data/cros_ec_proto.h>
21 #include <linux/platform_data/cros_ec_sensorhub.h>
22 #include <linux/platform_device.h>
24 static char *cros_ec_loc[] = {
25 [MOTIONSENSE_LOC_BASE] = "base",
26 [MOTIONSENSE_LOC_LID] = "lid",
27 [MOTIONSENSE_LOC_MAX] = "unknown",
30 static int cros_ec_get_host_cmd_version_mask(struct cros_ec_device *ec_dev,
31 u16 cmd_offset, u16 cmd, u32 *mask)
35 struct cros_ec_command msg;
37 struct ec_params_get_cmd_versions params;
38 struct ec_response_get_cmd_versions resp;
42 .command = EC_CMD_GET_CMD_VERSIONS + cmd_offset,
43 .insize = sizeof(struct ec_response_get_cmd_versions),
44 .outsize = sizeof(struct ec_params_get_cmd_versions)
46 .params = {.cmd = cmd}
49 ret = cros_ec_cmd_xfer_status(ec_dev, &buf.msg);
51 *mask = buf.resp.version_mask;
55 static void get_default_min_max_freq(enum motionsensor_type type,
60 case MOTIONSENSE_TYPE_ACCEL:
61 case MOTIONSENSE_TYPE_GYRO:
65 case MOTIONSENSE_TYPE_MAG:
69 case MOTIONSENSE_TYPE_PROX:
70 case MOTIONSENSE_TYPE_LIGHT:
74 case MOTIONSENSE_TYPE_BARO:
78 case MOTIONSENSE_TYPE_ACTIVITY:
86 int cros_ec_sensors_core_init(struct platform_device *pdev,
87 struct iio_dev *indio_dev,
90 struct device *dev = &pdev->dev;
91 struct cros_ec_sensors_core_state *state = iio_priv(indio_dev);
92 struct cros_ec_sensorhub *sensor_hub = dev_get_drvdata(dev->parent);
93 struct cros_ec_dev *ec = sensor_hub->ec;
94 struct cros_ec_sensor_platform *sensor_platform = dev_get_platdata(dev);
98 platform_set_drvdata(pdev, indio_dev);
100 state->ec = ec->ec_dev;
101 state->msg = devm_kzalloc(&pdev->dev,
102 max((u16)sizeof(struct ec_params_motion_sense),
103 state->ec->max_response), GFP_KERNEL);
107 state->resp = (struct ec_response_motion_sense *)state->msg->data;
109 mutex_init(&state->cmd_lock);
111 ret = cros_ec_get_host_cmd_version_mask(state->ec,
113 EC_CMD_MOTION_SENSE_CMD,
118 /* Set up the host command structure. */
119 state->msg->version = fls(ver_mask) - 1;
120 state->msg->command = EC_CMD_MOTION_SENSE_CMD + ec->cmd_offset;
121 state->msg->outsize = sizeof(struct ec_params_motion_sense);
123 indio_dev->dev.parent = &pdev->dev;
124 indio_dev->name = pdev->name;
126 if (physical_device) {
127 indio_dev->modes = INDIO_DIRECT_MODE;
129 state->param.cmd = MOTIONSENSE_CMD_INFO;
130 state->param.info.sensor_num = sensor_platform->sensor_num;
131 ret = cros_ec_motion_send_host_cmd(state, 0);
133 dev_warn(dev, "Can not access sensor info\n");
136 state->type = state->resp->info.type;
137 state->loc = state->resp->info.location;
139 /* Set sign vector, only used for backward compatibility. */
140 memset(state->sign, 1, CROS_EC_SENSOR_MAX_AXIS);
142 for (i = CROS_EC_SENSOR_X; i < CROS_EC_SENSOR_MAX_AXIS; i++)
143 state->calib[i].scale = MOTION_SENSE_DEFAULT_SCALE;
145 /* 0 is a correct value used to stop the device */
146 state->frequencies[0] = 0;
147 if (state->msg->version < 3) {
148 get_default_min_max_freq(state->resp->info.type,
149 &state->frequencies[1],
150 &state->frequencies[2]);
152 state->frequencies[1] =
153 state->resp->info_3.min_frequency;
154 state->frequencies[2] =
155 state->resp->info_3.max_frequency;
161 EXPORT_SYMBOL_GPL(cros_ec_sensors_core_init);
163 int cros_ec_motion_send_host_cmd(struct cros_ec_sensors_core_state *state,
169 state->msg->insize = min(opt_length, state->ec->max_response);
171 state->msg->insize = state->ec->max_response;
173 memcpy(state->msg->data, &state->param, sizeof(state->param));
175 ret = cros_ec_cmd_xfer_status(state->ec, state->msg);
180 state->resp != (struct ec_response_motion_sense *)state->msg->data)
181 memcpy(state->resp, state->msg->data, ret);
185 EXPORT_SYMBOL_GPL(cros_ec_motion_send_host_cmd);
187 static ssize_t cros_ec_sensors_calibrate(struct iio_dev *indio_dev,
188 uintptr_t private, const struct iio_chan_spec *chan,
189 const char *buf, size_t len)
191 struct cros_ec_sensors_core_state *st = iio_priv(indio_dev);
195 ret = strtobool(buf, &calibrate);
201 mutex_lock(&st->cmd_lock);
202 st->param.cmd = MOTIONSENSE_CMD_PERFORM_CALIB;
203 ret = cros_ec_motion_send_host_cmd(st, 0);
205 dev_warn(&indio_dev->dev, "Unable to calibrate sensor\n");
208 for (i = CROS_EC_SENSOR_X; i < CROS_EC_SENSOR_MAX_AXIS; i++)
209 st->calib[i].offset = st->resp->perform_calib.offset[i];
211 mutex_unlock(&st->cmd_lock);
213 return ret ? ret : len;
216 static ssize_t cros_ec_sensors_id(struct iio_dev *indio_dev,
218 const struct iio_chan_spec *chan, char *buf)
220 struct cros_ec_sensors_core_state *st = iio_priv(indio_dev);
222 return snprintf(buf, PAGE_SIZE, "%d\n", st->param.info.sensor_num);
225 static ssize_t cros_ec_sensors_loc(struct iio_dev *indio_dev,
226 uintptr_t private, const struct iio_chan_spec *chan,
229 struct cros_ec_sensors_core_state *st = iio_priv(indio_dev);
231 return snprintf(buf, PAGE_SIZE, "%s\n", cros_ec_loc[st->loc]);
234 const struct iio_chan_spec_ext_info cros_ec_sensors_ext_info[] = {
237 .shared = IIO_SHARED_BY_ALL,
238 .write = cros_ec_sensors_calibrate
242 .shared = IIO_SHARED_BY_ALL,
243 .read = cros_ec_sensors_id
247 .shared = IIO_SHARED_BY_ALL,
248 .read = cros_ec_sensors_loc
252 EXPORT_SYMBOL_GPL(cros_ec_sensors_ext_info);
255 * cros_ec_sensors_idx_to_reg - convert index into offset in shared memory
256 * @st: pointer to state information for device
257 * @idx: sensor index (should be element of enum sensor_index)
259 * Return: address to read at
261 static unsigned int cros_ec_sensors_idx_to_reg(
262 struct cros_ec_sensors_core_state *st,
266 * When using LPC interface, only space for 2 Accel and one Gyro.
267 * First halfword of MOTIONSENSE_TYPE_ACCEL is used by angle.
269 if (st->type == MOTIONSENSE_TYPE_ACCEL)
270 return EC_MEMMAP_ACC_DATA + sizeof(u16) *
271 (1 + idx + st->param.info.sensor_num *
272 CROS_EC_SENSOR_MAX_AXIS);
274 return EC_MEMMAP_GYRO_DATA + sizeof(u16) * idx;
277 static int cros_ec_sensors_cmd_read_u8(struct cros_ec_device *ec,
278 unsigned int offset, u8 *dest)
280 return ec->cmd_readmem(ec, offset, 1, dest);
283 static int cros_ec_sensors_cmd_read_u16(struct cros_ec_device *ec,
284 unsigned int offset, u16 *dest)
287 int ret = ec->cmd_readmem(ec, offset, 2, &tmp);
290 *dest = le16_to_cpu(tmp);
296 * cros_ec_sensors_read_until_not_busy() - read until is not busy
298 * @st: pointer to state information for device
300 * Read from EC status byte until it reads not busy.
301 * Return: 8-bit status if ok, -errno on failure.
303 static int cros_ec_sensors_read_until_not_busy(
304 struct cros_ec_sensors_core_state *st)
306 struct cros_ec_device *ec = st->ec;
308 int ret, attempts = 0;
310 ret = cros_ec_sensors_cmd_read_u8(ec, EC_MEMMAP_ACC_STATUS, &status);
314 while (status & EC_MEMMAP_ACC_STATUS_BUSY_BIT) {
315 /* Give up after enough attempts, return error. */
316 if (attempts++ >= 50)
319 /* Small delay every so often. */
320 if (attempts % 5 == 0)
323 ret = cros_ec_sensors_cmd_read_u8(ec, EC_MEMMAP_ACC_STATUS,
333 * read_ec_sensors_data_unsafe() - read acceleration data from EC shared memory
334 * @indio_dev: pointer to IIO device
335 * @scan_mask: bitmap of the sensor indices to scan
336 * @data: location to store data
338 * This is the unsafe function for reading the EC data. It does not guarantee
339 * that the EC will not modify the data as it is being read in.
341 * Return: 0 on success, -errno on failure.
343 static int cros_ec_sensors_read_data_unsafe(struct iio_dev *indio_dev,
344 unsigned long scan_mask, s16 *data)
346 struct cros_ec_sensors_core_state *st = iio_priv(indio_dev);
347 struct cros_ec_device *ec = st->ec;
351 /* Read all sensors enabled in scan_mask. Each value is 2 bytes. */
352 for_each_set_bit(i, &scan_mask, indio_dev->masklength) {
353 ret = cros_ec_sensors_cmd_read_u16(ec,
354 cros_ec_sensors_idx_to_reg(st, i),
359 *data *= st->sign[i];
367 * cros_ec_sensors_read_lpc() - read acceleration data from EC shared memory.
368 * @indio_dev: pointer to IIO device.
369 * @scan_mask: bitmap of the sensor indices to scan.
370 * @data: location to store data.
372 * Note: this is the safe function for reading the EC data. It guarantees
373 * that the data sampled was not modified by the EC while being read.
375 * Return: 0 on success, -errno on failure.
377 int cros_ec_sensors_read_lpc(struct iio_dev *indio_dev,
378 unsigned long scan_mask, s16 *data)
380 struct cros_ec_sensors_core_state *st = iio_priv(indio_dev);
381 struct cros_ec_device *ec = st->ec;
382 u8 samp_id = 0xff, status = 0;
383 int ret, attempts = 0;
386 * Continually read all data from EC until the status byte after
387 * all reads reflects that the EC is not busy and the sample id
388 * matches the sample id from before all reads. This guarantees
389 * that data read in was not modified by the EC while reading.
391 while ((status & (EC_MEMMAP_ACC_STATUS_BUSY_BIT |
392 EC_MEMMAP_ACC_STATUS_SAMPLE_ID_MASK)) != samp_id) {
393 /* If we have tried to read too many times, return error. */
397 /* Read status byte until EC is not busy. */
398 ret = cros_ec_sensors_read_until_not_busy(st);
403 * Store the current sample id so that we can compare to the
404 * sample id after reading the data.
406 samp_id = ret & EC_MEMMAP_ACC_STATUS_SAMPLE_ID_MASK;
408 /* Read all EC data, format it, and store it into data. */
409 ret = cros_ec_sensors_read_data_unsafe(indio_dev, scan_mask,
414 /* Read status byte. */
415 ret = cros_ec_sensors_cmd_read_u8(ec, EC_MEMMAP_ACC_STATUS,
423 EXPORT_SYMBOL_GPL(cros_ec_sensors_read_lpc);
425 int cros_ec_sensors_read_cmd(struct iio_dev *indio_dev,
426 unsigned long scan_mask, s16 *data)
428 struct cros_ec_sensors_core_state *st = iio_priv(indio_dev);
432 /* Read all sensor data through a command. */
433 st->param.cmd = MOTIONSENSE_CMD_DATA;
434 ret = cros_ec_motion_send_host_cmd(st, sizeof(st->resp->data));
436 dev_warn(&indio_dev->dev, "Unable to read sensor data\n");
440 for_each_set_bit(i, &scan_mask, indio_dev->masklength) {
441 *data = st->resp->data.data[i];
447 EXPORT_SYMBOL_GPL(cros_ec_sensors_read_cmd);
449 irqreturn_t cros_ec_sensors_capture(int irq, void *p)
451 struct iio_poll_func *pf = p;
452 struct iio_dev *indio_dev = pf->indio_dev;
453 struct cros_ec_sensors_core_state *st = iio_priv(indio_dev);
456 mutex_lock(&st->cmd_lock);
458 /* Clear capture data. */
459 memset(st->samples, 0, indio_dev->scan_bytes);
461 /* Read data based on which channels are enabled in scan mask. */
462 ret = st->read_ec_sensors_data(indio_dev,
463 *(indio_dev->active_scan_mask),
468 iio_push_to_buffers_with_timestamp(indio_dev, st->samples,
469 iio_get_time_ns(indio_dev));
473 * Tell the core we are done with this trigger and ready for the
476 iio_trigger_notify_done(indio_dev->trig);
478 mutex_unlock(&st->cmd_lock);
482 EXPORT_SYMBOL_GPL(cros_ec_sensors_capture);
484 int cros_ec_sensors_core_read(struct cros_ec_sensors_core_state *st,
485 struct iio_chan_spec const *chan,
486 int *val, int *val2, long mask)
491 case IIO_CHAN_INFO_SAMP_FREQ:
492 st->param.cmd = MOTIONSENSE_CMD_EC_RATE;
493 st->param.ec_rate.data =
494 EC_MOTION_SENSE_NO_VALUE;
496 ret = cros_ec_motion_send_host_cmd(st, 0);
500 *val = st->resp->ec_rate.ret;
503 case IIO_CHAN_INFO_FREQUENCY:
504 st->param.cmd = MOTIONSENSE_CMD_SENSOR_ODR;
505 st->param.sensor_odr.data =
506 EC_MOTION_SENSE_NO_VALUE;
508 ret = cros_ec_motion_send_host_cmd(st, 0);
512 *val = st->resp->sensor_odr.ret;
522 EXPORT_SYMBOL_GPL(cros_ec_sensors_core_read);
524 int cros_ec_sensors_core_read_avail(struct iio_dev *indio_dev,
525 struct iio_chan_spec const *chan,
531 struct cros_ec_sensors_core_state *state = iio_priv(indio_dev);
534 case IIO_CHAN_INFO_SAMP_FREQ:
535 *length = ARRAY_SIZE(state->frequencies);
536 *vals = (const int *)&state->frequencies;
538 return IIO_AVAIL_LIST;
543 EXPORT_SYMBOL_GPL(cros_ec_sensors_core_read_avail);
545 int cros_ec_sensors_core_write(struct cros_ec_sensors_core_state *st,
546 struct iio_chan_spec const *chan,
547 int val, int val2, long mask)
552 case IIO_CHAN_INFO_FREQUENCY:
553 st->param.cmd = MOTIONSENSE_CMD_SENSOR_ODR;
554 st->param.sensor_odr.data = val;
556 /* Always roundup, so caller gets at least what it asks for. */
557 st->param.sensor_odr.roundup = 1;
559 ret = cros_ec_motion_send_host_cmd(st, 0);
561 case IIO_CHAN_INFO_SAMP_FREQ:
562 st->param.cmd = MOTIONSENSE_CMD_EC_RATE;
563 st->param.ec_rate.data = val;
565 ret = cros_ec_motion_send_host_cmd(st, 0);
568 st->curr_sampl_freq = val;
576 EXPORT_SYMBOL_GPL(cros_ec_sensors_core_write);
578 static int __maybe_unused cros_ec_sensors_prepare(struct device *dev)
580 struct iio_dev *indio_dev = dev_get_drvdata(dev);
581 struct cros_ec_sensors_core_state *st = iio_priv(indio_dev);
583 if (st->curr_sampl_freq == 0)
587 * If the sensors are sampled at high frequency, we will not be able to
588 * sleep. Set sampling to a long period if necessary.
590 if (st->curr_sampl_freq < CROS_EC_MIN_SUSPEND_SAMPLING_FREQUENCY) {
591 mutex_lock(&st->cmd_lock);
592 st->param.cmd = MOTIONSENSE_CMD_EC_RATE;
593 st->param.ec_rate.data = CROS_EC_MIN_SUSPEND_SAMPLING_FREQUENCY;
594 cros_ec_motion_send_host_cmd(st, 0);
595 mutex_unlock(&st->cmd_lock);
600 static void __maybe_unused cros_ec_sensors_complete(struct device *dev)
602 struct iio_dev *indio_dev = dev_get_drvdata(dev);
603 struct cros_ec_sensors_core_state *st = iio_priv(indio_dev);
605 if (st->curr_sampl_freq == 0)
608 if (st->curr_sampl_freq < CROS_EC_MIN_SUSPEND_SAMPLING_FREQUENCY) {
609 mutex_lock(&st->cmd_lock);
610 st->param.cmd = MOTIONSENSE_CMD_EC_RATE;
611 st->param.ec_rate.data = st->curr_sampl_freq;
612 cros_ec_motion_send_host_cmd(st, 0);
613 mutex_unlock(&st->cmd_lock);
617 const struct dev_pm_ops cros_ec_sensors_pm_ops = {
618 #ifdef CONFIG_PM_SLEEP
619 .prepare = cros_ec_sensors_prepare,
620 .complete = cros_ec_sensors_complete
623 EXPORT_SYMBOL_GPL(cros_ec_sensors_pm_ops);
625 MODULE_DESCRIPTION("ChromeOS EC sensor hub core functions");
626 MODULE_LICENSE("GPL v2");