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/sysfs.h>
15 #include <linux/iio/trigger.h>
16 #include <linux/iio/trigger_consumer.h>
17 #include <linux/iio/triggered_buffer.h>
18 #include <linux/kernel.h>
19 #include <linux/module.h>
20 #include <linux/slab.h>
21 #include <linux/platform_data/cros_ec_commands.h>
22 #include <linux/platform_data/cros_ec_proto.h>
23 #include <linux/platform_data/cros_ec_sensorhub.h>
24 #include <linux/platform_device.h>
27 * Hard coded to the first device to support sensor fifo. The EC has a 2048
28 * byte fifo and will trigger an interrupt when fifo is 2/3 full.
30 #define CROS_EC_FIFO_SIZE (2048 * 2 / 3)
32 static int cros_ec_get_host_cmd_version_mask(struct cros_ec_device
*ec_dev
,
33 u16 cmd_offset
, u16 cmd
, u32
*mask
)
37 struct cros_ec_command msg
;
39 struct ec_params_get_cmd_versions params
;
40 struct ec_response_get_cmd_versions resp
;
44 .command
= EC_CMD_GET_CMD_VERSIONS
+ cmd_offset
,
45 .insize
= sizeof(struct ec_response_get_cmd_versions
),
46 .outsize
= sizeof(struct ec_params_get_cmd_versions
)
48 .params
= {.cmd
= cmd
}
51 ret
= cros_ec_cmd_xfer_status(ec_dev
, &buf
.msg
);
53 *mask
= buf
.resp
.version_mask
;
57 static void get_default_min_max_freq(enum motionsensor_type type
,
63 * We don't know fifo size, set to size previously used by older
66 *max_fifo_events
= CROS_EC_FIFO_SIZE
;
69 case MOTIONSENSE_TYPE_ACCEL
:
73 case MOTIONSENSE_TYPE_GYRO
:
77 case MOTIONSENSE_TYPE_MAG
:
81 case MOTIONSENSE_TYPE_PROX
:
82 case MOTIONSENSE_TYPE_LIGHT
:
86 case MOTIONSENSE_TYPE_BARO
:
90 case MOTIONSENSE_TYPE_ACTIVITY
:
98 static int cros_ec_sensor_set_ec_rate(struct cros_ec_sensors_core_state
*st
,
106 mutex_lock(&st
->cmd_lock
);
107 st
->param
.cmd
= MOTIONSENSE_CMD_EC_RATE
;
108 st
->param
.ec_rate
.data
= rate
;
109 ret
= cros_ec_motion_send_host_cmd(st
, 0);
110 mutex_unlock(&st
->cmd_lock
);
114 static ssize_t
cros_ec_sensor_set_report_latency(struct device
*dev
,
115 struct device_attribute
*attr
,
116 const char *buf
, size_t len
)
118 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
119 struct cros_ec_sensors_core_state
*st
= iio_priv(indio_dev
);
120 int integer
, fract
, ret
;
123 ret
= iio_str_to_fixpoint(buf
, 100000, &integer
, &fract
);
127 /* EC rate is in ms. */
128 latency
= integer
* 1000 + fract
/ 1000;
129 ret
= cros_ec_sensor_set_ec_rate(st
, latency
);
136 static ssize_t
cros_ec_sensor_get_report_latency(struct device
*dev
,
137 struct device_attribute
*attr
,
140 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
141 struct cros_ec_sensors_core_state
*st
= iio_priv(indio_dev
);
144 mutex_lock(&st
->cmd_lock
);
145 st
->param
.cmd
= MOTIONSENSE_CMD_EC_RATE
;
146 st
->param
.ec_rate
.data
= EC_MOTION_SENSE_NO_VALUE
;
148 ret
= cros_ec_motion_send_host_cmd(st
, 0);
149 latency
= st
->resp
->ec_rate
.ret
;
150 mutex_unlock(&st
->cmd_lock
);
154 return sprintf(buf
, "%d.%06u\n",
156 (latency
% 1000) * 1000);
159 static IIO_DEVICE_ATTR(hwfifo_timeout
, 0644,
160 cros_ec_sensor_get_report_latency
,
161 cros_ec_sensor_set_report_latency
, 0);
163 static ssize_t
hwfifo_watermark_max_show(struct device
*dev
,
164 struct device_attribute
*attr
,
167 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
168 struct cros_ec_sensors_core_state
*st
= iio_priv(indio_dev
);
170 return sprintf(buf
, "%d\n", st
->fifo_max_event_count
);
173 static IIO_DEVICE_ATTR_RO(hwfifo_watermark_max
, 0);
175 static const struct iio_dev_attr
*cros_ec_sensor_fifo_attributes
[] = {
176 &iio_dev_attr_hwfifo_timeout
,
177 &iio_dev_attr_hwfifo_watermark_max
,
181 int cros_ec_sensors_push_data(struct iio_dev
*indio_dev
,
185 struct cros_ec_sensors_core_state
*st
= iio_priv(indio_dev
);
191 * Ignore samples if the buffer is not set: it is needed if the ODR is
192 * set but the buffer is not enabled yet.
194 if (!iio_buffer_enabled(indio_dev
))
197 out
= (s16
*)st
->samples
;
199 indio_dev
->active_scan_mask
,
200 indio_dev
->masklength
) {
205 if (iio_device_get_clock(indio_dev
) != CLOCK_BOOTTIME
)
206 delta
= iio_get_time_ns(indio_dev
) - cros_ec_get_time_ns();
210 iio_push_to_buffers_with_timestamp(indio_dev
, st
->samples
,
215 EXPORT_SYMBOL_GPL(cros_ec_sensors_push_data
);
217 static void cros_ec_sensors_core_clean(void *arg
)
219 struct platform_device
*pdev
= (struct platform_device
*)arg
;
220 struct cros_ec_sensorhub
*sensor_hub
=
221 dev_get_drvdata(pdev
->dev
.parent
);
222 struct iio_dev
*indio_dev
= platform_get_drvdata(pdev
);
223 struct cros_ec_sensors_core_state
*st
= iio_priv(indio_dev
);
224 u8 sensor_num
= st
->param
.info
.sensor_num
;
226 cros_ec_sensorhub_unregister_push_data(sensor_hub
, sensor_num
);
230 * cros_ec_sensors_core_init() - basic initialization of the core structure
231 * @pdev: platform device created for the sensor
232 * @indio_dev: iio device structure of the device
233 * @physical_device: true if the device refers to a physical device
234 * @trigger_capture: function pointer to call buffer is triggered,
235 * for backward compatibility.
237 * Return: 0 on success, -errno on failure.
239 int cros_ec_sensors_core_init(struct platform_device
*pdev
,
240 struct iio_dev
*indio_dev
,
241 bool physical_device
,
242 cros_ec_sensors_capture_t trigger_capture
)
244 struct device
*dev
= &pdev
->dev
;
245 struct cros_ec_sensors_core_state
*state
= iio_priv(indio_dev
);
246 struct cros_ec_sensorhub
*sensor_hub
= dev_get_drvdata(dev
->parent
);
247 struct cros_ec_dev
*ec
= sensor_hub
->ec
;
248 struct cros_ec_sensor_platform
*sensor_platform
= dev_get_platdata(dev
);
250 int frequencies
[ARRAY_SIZE(state
->frequencies
) / 2] = { 0 };
253 platform_set_drvdata(pdev
, indio_dev
);
255 state
->ec
= ec
->ec_dev
;
256 state
->msg
= devm_kzalloc(&pdev
->dev
, sizeof(*state
->msg
) +
257 max((u16
)sizeof(struct ec_params_motion_sense
),
258 state
->ec
->max_response
), GFP_KERNEL
);
262 state
->resp
= (struct ec_response_motion_sense
*)state
->msg
->data
;
264 mutex_init(&state
->cmd_lock
);
266 ret
= cros_ec_get_host_cmd_version_mask(state
->ec
,
268 EC_CMD_MOTION_SENSE_CMD
,
273 /* Set up the host command structure. */
274 state
->msg
->version
= fls(ver_mask
) - 1;
275 state
->msg
->command
= EC_CMD_MOTION_SENSE_CMD
+ ec
->cmd_offset
;
276 state
->msg
->outsize
= sizeof(struct ec_params_motion_sense
);
278 indio_dev
->name
= pdev
->name
;
280 if (physical_device
) {
281 enum motionsensor_location loc
;
283 state
->param
.cmd
= MOTIONSENSE_CMD_INFO
;
284 state
->param
.info
.sensor_num
= sensor_platform
->sensor_num
;
285 ret
= cros_ec_motion_send_host_cmd(state
, 0);
287 dev_warn(dev
, "Can not access sensor info\n");
290 state
->type
= state
->resp
->info
.type
;
291 loc
= state
->resp
->info
.location
;
292 if (loc
== MOTIONSENSE_LOC_BASE
)
293 indio_dev
->label
= "accel-base";
294 else if (loc
== MOTIONSENSE_LOC_LID
)
295 indio_dev
->label
= "accel-display";
296 else if (loc
== MOTIONSENSE_LOC_CAMERA
)
297 indio_dev
->label
= "accel-camera";
299 /* Set sign vector, only used for backward compatibility. */
300 memset(state
->sign
, 1, CROS_EC_SENSOR_MAX_AXIS
);
302 for (i
= CROS_EC_SENSOR_X
; i
< CROS_EC_SENSOR_MAX_AXIS
; i
++)
303 state
->calib
[i
].scale
= MOTION_SENSE_DEFAULT_SCALE
;
305 /* 0 is a correct value used to stop the device */
306 if (state
->msg
->version
< 3) {
307 get_default_min_max_freq(state
->resp
->info
.type
,
310 &state
->fifo_max_event_count
);
312 if (state
->resp
->info_3
.max_frequency
== 0) {
313 get_default_min_max_freq(state
->resp
->info
.type
,
318 frequencies
[1] = state
->resp
->info_3
.min_frequency
;
319 frequencies
[2] = state
->resp
->info_3
.max_frequency
;
321 state
->fifo_max_event_count
= state
->resp
->info_3
.fifo_max_event_count
;
323 for (i
= 0; i
< ARRAY_SIZE(frequencies
); i
++) {
324 state
->frequencies
[2 * i
] = frequencies
[i
] / 1000;
325 state
->frequencies
[2 * i
+ 1] =
326 (frequencies
[i
] % 1000) * 1000;
329 if (cros_ec_check_features(ec
, EC_FEATURE_MOTION_SENSE_FIFO
)) {
331 * Create a software buffer, feed by the EC FIFO.
332 * We can not use trigger here, as events are generated
333 * as soon as sample_frequency is set.
335 ret
= devm_iio_kfifo_buffer_setup_ext(dev
, indio_dev
, NULL
,
336 cros_ec_sensor_fifo_attributes
);
340 /* Timestamp coming from FIFO are in ns since boot. */
341 ret
= iio_device_set_clock(indio_dev
, CLOCK_BOOTTIME
);
347 * The only way to get samples in buffer is to set a
348 * software trigger (systrig, hrtimer).
350 ret
= devm_iio_triggered_buffer_setup(dev
, indio_dev
,
351 NULL
, trigger_capture
, NULL
);
359 EXPORT_SYMBOL_GPL(cros_ec_sensors_core_init
);
362 * cros_ec_sensors_core_register() - Register callback to FIFO and IIO when
364 * It must be called at the end of the sensor probe routine.
365 * @dev: device created for the sensor
366 * @indio_dev: iio device structure of the device
367 * @push_data: function to call when cros_ec_sensorhub receives
368 * a sample for that sensor.
370 * Return: 0 on success, -errno on failure.
372 int cros_ec_sensors_core_register(struct device
*dev
,
373 struct iio_dev
*indio_dev
,
374 cros_ec_sensorhub_push_data_cb_t push_data
)
376 struct cros_ec_sensor_platform
*sensor_platform
= dev_get_platdata(dev
);
377 struct cros_ec_sensorhub
*sensor_hub
= dev_get_drvdata(dev
->parent
);
378 struct platform_device
*pdev
= to_platform_device(dev
);
379 struct cros_ec_dev
*ec
= sensor_hub
->ec
;
382 ret
= devm_iio_device_register(dev
, indio_dev
);
387 !cros_ec_check_features(ec
, EC_FEATURE_MOTION_SENSE_FIFO
))
390 ret
= cros_ec_sensorhub_register_push_data(
391 sensor_hub
, sensor_platform
->sensor_num
,
392 indio_dev
, push_data
);
396 return devm_add_action_or_reset(
397 dev
, cros_ec_sensors_core_clean
, pdev
);
399 EXPORT_SYMBOL_GPL(cros_ec_sensors_core_register
);
402 * cros_ec_motion_send_host_cmd() - send motion sense host command
403 * @state: pointer to state information for device
404 * @opt_length: optional length to reduce the response size, useful on the data
405 * path. Otherwise, the maximal allowed response size is used
407 * When called, the sub-command is assumed to be set in param->cmd.
409 * Return: 0 on success, -errno on failure.
411 int cros_ec_motion_send_host_cmd(struct cros_ec_sensors_core_state
*state
,
417 state
->msg
->insize
= min(opt_length
, state
->ec
->max_response
);
419 state
->msg
->insize
= state
->ec
->max_response
;
421 memcpy(state
->msg
->data
, &state
->param
, sizeof(state
->param
));
423 ret
= cros_ec_cmd_xfer_status(state
->ec
, state
->msg
);
428 state
->resp
!= (struct ec_response_motion_sense
*)state
->msg
->data
)
429 memcpy(state
->resp
, state
->msg
->data
, ret
);
433 EXPORT_SYMBOL_GPL(cros_ec_motion_send_host_cmd
);
435 static ssize_t
cros_ec_sensors_calibrate(struct iio_dev
*indio_dev
,
436 uintptr_t private, const struct iio_chan_spec
*chan
,
437 const char *buf
, size_t len
)
439 struct cros_ec_sensors_core_state
*st
= iio_priv(indio_dev
);
443 ret
= kstrtobool(buf
, &calibrate
);
449 mutex_lock(&st
->cmd_lock
);
450 st
->param
.cmd
= MOTIONSENSE_CMD_PERFORM_CALIB
;
451 ret
= cros_ec_motion_send_host_cmd(st
, 0);
453 dev_warn(&indio_dev
->dev
, "Unable to calibrate sensor\n");
456 for (i
= CROS_EC_SENSOR_X
; i
< CROS_EC_SENSOR_MAX_AXIS
; i
++)
457 st
->calib
[i
].offset
= st
->resp
->perform_calib
.offset
[i
];
459 mutex_unlock(&st
->cmd_lock
);
461 return ret
? ret
: len
;
464 static ssize_t
cros_ec_sensors_id(struct iio_dev
*indio_dev
,
466 const struct iio_chan_spec
*chan
, char *buf
)
468 struct cros_ec_sensors_core_state
*st
= iio_priv(indio_dev
);
470 return snprintf(buf
, PAGE_SIZE
, "%d\n", st
->param
.info
.sensor_num
);
473 const struct iio_chan_spec_ext_info cros_ec_sensors_ext_info
[] = {
476 .shared
= IIO_SHARED_BY_ALL
,
477 .write
= cros_ec_sensors_calibrate
481 .shared
= IIO_SHARED_BY_ALL
,
482 .read
= cros_ec_sensors_id
486 EXPORT_SYMBOL_GPL(cros_ec_sensors_ext_info
);
489 * cros_ec_sensors_idx_to_reg - convert index into offset in shared memory
490 * @st: pointer to state information for device
491 * @idx: sensor index (should be element of enum sensor_index)
493 * Return: address to read at
495 static unsigned int cros_ec_sensors_idx_to_reg(
496 struct cros_ec_sensors_core_state
*st
,
500 * When using LPC interface, only space for 2 Accel and one Gyro.
501 * First halfword of MOTIONSENSE_TYPE_ACCEL is used by angle.
503 if (st
->type
== MOTIONSENSE_TYPE_ACCEL
)
504 return EC_MEMMAP_ACC_DATA
+ sizeof(u16
) *
505 (1 + idx
+ st
->param
.info
.sensor_num
*
506 CROS_EC_SENSOR_MAX_AXIS
);
508 return EC_MEMMAP_GYRO_DATA
+ sizeof(u16
) * idx
;
511 static int cros_ec_sensors_cmd_read_u8(struct cros_ec_device
*ec
,
512 unsigned int offset
, u8
*dest
)
514 return ec
->cmd_readmem(ec
, offset
, 1, dest
);
517 static int cros_ec_sensors_cmd_read_u16(struct cros_ec_device
*ec
,
518 unsigned int offset
, u16
*dest
)
521 int ret
= ec
->cmd_readmem(ec
, offset
, 2, &tmp
);
524 *dest
= le16_to_cpu(tmp
);
530 * cros_ec_sensors_read_until_not_busy() - read until is not busy
532 * @st: pointer to state information for device
534 * Read from EC status byte until it reads not busy.
535 * Return: 8-bit status if ok, -errno on failure.
537 static int cros_ec_sensors_read_until_not_busy(
538 struct cros_ec_sensors_core_state
*st
)
540 struct cros_ec_device
*ec
= st
->ec
;
542 int ret
, attempts
= 0;
544 ret
= cros_ec_sensors_cmd_read_u8(ec
, EC_MEMMAP_ACC_STATUS
, &status
);
548 while (status
& EC_MEMMAP_ACC_STATUS_BUSY_BIT
) {
549 /* Give up after enough attempts, return error. */
550 if (attempts
++ >= 50)
553 /* Small delay every so often. */
554 if (attempts
% 5 == 0)
557 ret
= cros_ec_sensors_cmd_read_u8(ec
, EC_MEMMAP_ACC_STATUS
,
567 * cros_ec_sensors_read_data_unsafe() - read acceleration data from EC shared memory
568 * @indio_dev: pointer to IIO device
569 * @scan_mask: bitmap of the sensor indices to scan
570 * @data: location to store data
572 * This is the unsafe function for reading the EC data. It does not guarantee
573 * that the EC will not modify the data as it is being read in.
575 * Return: 0 on success, -errno on failure.
577 static int cros_ec_sensors_read_data_unsafe(struct iio_dev
*indio_dev
,
578 unsigned long scan_mask
, s16
*data
)
580 struct cros_ec_sensors_core_state
*st
= iio_priv(indio_dev
);
581 struct cros_ec_device
*ec
= st
->ec
;
585 /* Read all sensors enabled in scan_mask. Each value is 2 bytes. */
586 for_each_set_bit(i
, &scan_mask
, indio_dev
->masklength
) {
587 ret
= cros_ec_sensors_cmd_read_u16(ec
,
588 cros_ec_sensors_idx_to_reg(st
, i
),
593 *data
*= st
->sign
[i
];
601 * cros_ec_sensors_read_lpc() - read acceleration data from EC shared memory.
602 * @indio_dev: pointer to IIO device.
603 * @scan_mask: bitmap of the sensor indices to scan.
604 * @data: location to store data.
606 * Note: this is the safe function for reading the EC data. It guarantees
607 * that the data sampled was not modified by the EC while being read.
609 * Return: 0 on success, -errno on failure.
611 int cros_ec_sensors_read_lpc(struct iio_dev
*indio_dev
,
612 unsigned long scan_mask
, s16
*data
)
614 struct cros_ec_sensors_core_state
*st
= iio_priv(indio_dev
);
615 struct cros_ec_device
*ec
= st
->ec
;
616 u8 samp_id
= 0xff, status
= 0;
617 int ret
, attempts
= 0;
620 * Continually read all data from EC until the status byte after
621 * all reads reflects that the EC is not busy and the sample id
622 * matches the sample id from before all reads. This guarantees
623 * that data read in was not modified by the EC while reading.
625 while ((status
& (EC_MEMMAP_ACC_STATUS_BUSY_BIT
|
626 EC_MEMMAP_ACC_STATUS_SAMPLE_ID_MASK
)) != samp_id
) {
627 /* If we have tried to read too many times, return error. */
631 /* Read status byte until EC is not busy. */
632 ret
= cros_ec_sensors_read_until_not_busy(st
);
637 * Store the current sample id so that we can compare to the
638 * sample id after reading the data.
640 samp_id
= ret
& EC_MEMMAP_ACC_STATUS_SAMPLE_ID_MASK
;
642 /* Read all EC data, format it, and store it into data. */
643 ret
= cros_ec_sensors_read_data_unsafe(indio_dev
, scan_mask
,
648 /* Read status byte. */
649 ret
= cros_ec_sensors_cmd_read_u8(ec
, EC_MEMMAP_ACC_STATUS
,
657 EXPORT_SYMBOL_GPL(cros_ec_sensors_read_lpc
);
660 * cros_ec_sensors_read_cmd() - retrieve data using the EC command protocol
661 * @indio_dev: pointer to IIO device
662 * @scan_mask: bitmap of the sensor indices to scan
663 * @data: location to store data
665 * Return: 0 on success, -errno on failure.
667 int cros_ec_sensors_read_cmd(struct iio_dev
*indio_dev
,
668 unsigned long scan_mask
, s16
*data
)
670 struct cros_ec_sensors_core_state
*st
= iio_priv(indio_dev
);
674 /* Read all sensor data through a command. */
675 st
->param
.cmd
= MOTIONSENSE_CMD_DATA
;
676 ret
= cros_ec_motion_send_host_cmd(st
, sizeof(st
->resp
->data
));
678 dev_warn(&indio_dev
->dev
, "Unable to read sensor data\n");
682 for_each_set_bit(i
, &scan_mask
, indio_dev
->masklength
) {
683 *data
= st
->resp
->data
.data
[i
];
689 EXPORT_SYMBOL_GPL(cros_ec_sensors_read_cmd
);
692 * cros_ec_sensors_capture() - the trigger handler function
693 * @irq: the interrupt number.
694 * @p: a pointer to the poll function.
696 * On a trigger event occurring, if the pollfunc is attached then this
697 * handler is called as a threaded interrupt (and hence may sleep). It
698 * is responsible for grabbing data from the device and pushing it into
699 * the associated buffer.
701 * Return: IRQ_HANDLED
703 irqreturn_t
cros_ec_sensors_capture(int irq
, void *p
)
705 struct iio_poll_func
*pf
= p
;
706 struct iio_dev
*indio_dev
= pf
->indio_dev
;
707 struct cros_ec_sensors_core_state
*st
= iio_priv(indio_dev
);
710 mutex_lock(&st
->cmd_lock
);
712 /* Clear capture data. */
713 memset(st
->samples
, 0, indio_dev
->scan_bytes
);
715 /* Read data based on which channels are enabled in scan mask. */
716 ret
= st
->read_ec_sensors_data(indio_dev
,
717 *(indio_dev
->active_scan_mask
),
722 iio_push_to_buffers_with_timestamp(indio_dev
, st
->samples
,
723 iio_get_time_ns(indio_dev
));
727 * Tell the core we are done with this trigger and ready for the
730 iio_trigger_notify_done(indio_dev
->trig
);
732 mutex_unlock(&st
->cmd_lock
);
736 EXPORT_SYMBOL_GPL(cros_ec_sensors_capture
);
739 * cros_ec_sensors_core_read() - function to request a value from the sensor
740 * @st: pointer to state information for device
741 * @chan: channel specification structure table
742 * @val: will contain one element making up the returned value
743 * @val2: will contain another element making up the returned value
744 * @mask: specifies which values to be requested
746 * Return: the type of value returned by the device
748 int cros_ec_sensors_core_read(struct cros_ec_sensors_core_state
*st
,
749 struct iio_chan_spec
const *chan
,
750 int *val
, int *val2
, long mask
)
755 case IIO_CHAN_INFO_SAMP_FREQ
:
756 st
->param
.cmd
= MOTIONSENSE_CMD_SENSOR_ODR
;
757 st
->param
.sensor_odr
.data
=
758 EC_MOTION_SENSE_NO_VALUE
;
760 ret
= cros_ec_motion_send_host_cmd(st
, 0);
764 frequency
= st
->resp
->sensor_odr
.ret
;
765 *val
= frequency
/ 1000;
766 *val2
= (frequency
% 1000) * 1000;
767 ret
= IIO_VAL_INT_PLUS_MICRO
;
776 EXPORT_SYMBOL_GPL(cros_ec_sensors_core_read
);
779 * cros_ec_sensors_core_read_avail() - get available values
780 * @indio_dev: pointer to state information for device
781 * @chan: channel specification structure table
782 * @vals: list of available values
783 * @type: type of data returned
784 * @length: number of data returned in the array
785 * @mask: specifies which values to be requested
787 * Return: an error code, IIO_AVAIL_RANGE or IIO_AVAIL_LIST
789 int cros_ec_sensors_core_read_avail(struct iio_dev
*indio_dev
,
790 struct iio_chan_spec
const *chan
,
796 struct cros_ec_sensors_core_state
*state
= iio_priv(indio_dev
);
799 case IIO_CHAN_INFO_SAMP_FREQ
:
800 *length
= ARRAY_SIZE(state
->frequencies
);
801 *vals
= (const int *)&state
->frequencies
;
802 *type
= IIO_VAL_INT_PLUS_MICRO
;
803 return IIO_AVAIL_LIST
;
808 EXPORT_SYMBOL_GPL(cros_ec_sensors_core_read_avail
);
811 * cros_ec_sensors_core_write() - function to write a value to the sensor
812 * @st: pointer to state information for device
813 * @chan: channel specification structure table
814 * @val: first part of value to write
815 * @val2: second part of value to write
816 * @mask: specifies which values to write
818 * Return: the type of value returned by the device
820 int cros_ec_sensors_core_write(struct cros_ec_sensors_core_state
*st
,
821 struct iio_chan_spec
const *chan
,
822 int val
, int val2
, long mask
)
827 case IIO_CHAN_INFO_SAMP_FREQ
:
828 frequency
= val
* 1000 + val2
/ 1000;
829 st
->param
.cmd
= MOTIONSENSE_CMD_SENSOR_ODR
;
830 st
->param
.sensor_odr
.data
= frequency
;
832 /* Always roundup, so caller gets at least what it asks for. */
833 st
->param
.sensor_odr
.roundup
= 1;
835 ret
= cros_ec_motion_send_host_cmd(st
, 0);
843 EXPORT_SYMBOL_GPL(cros_ec_sensors_core_write
);
845 static int __maybe_unused
cros_ec_sensors_resume(struct device
*dev
)
847 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
848 struct cros_ec_sensors_core_state
*st
= iio_priv(indio_dev
);
851 if (st
->range_updated
) {
852 mutex_lock(&st
->cmd_lock
);
853 st
->param
.cmd
= MOTIONSENSE_CMD_SENSOR_RANGE
;
854 st
->param
.sensor_range
.data
= st
->curr_range
;
855 st
->param
.sensor_range
.roundup
= 1;
856 ret
= cros_ec_motion_send_host_cmd(st
, 0);
857 mutex_unlock(&st
->cmd_lock
);
862 SIMPLE_DEV_PM_OPS(cros_ec_sensors_pm_ops
, NULL
, cros_ec_sensors_resume
);
863 EXPORT_SYMBOL_GPL(cros_ec_sensors_pm_ops
);
865 MODULE_DESCRIPTION("ChromeOS EC sensor hub core functions");
866 MODULE_LICENSE("GPL v2");