// SPDX-License-Identifier: GPL-2.0 /* * cros_ec_sensors_core - Common function for Chrome OS EC sensor driver. * * Copyright (C) 2016 Google, Inc */ #include <linux/delay.h> #include <linux/device.h> #include <linux/iio/buffer.h> #include <linux/iio/common/cros_ec_sensors_core.h> #include <linux/iio/iio.h> #include <linux/iio/kfifo_buf.h> #include <linux/iio/sysfs.h> #include <linux/iio/trigger.h> #include <linux/iio/trigger_consumer.h> #include <linux/iio/triggered_buffer.h> #include <linux/kernel.h> #include <linux/module.h> #include <linux/slab.h> #include <linux/platform_data/cros_ec_commands.h> #include <linux/platform_data/cros_ec_proto.h> #include <linux/platform_data/cros_ec_sensorhub.h> #include <linux/platform_device.h> /* * Hard coded to the first device to support sensor fifo. The EC has a 2048 * byte fifo and will trigger an interrupt when fifo is 2/3 full. */ #define CROS_EC_FIFO_SIZE … static int cros_ec_get_host_cmd_version_mask(struct cros_ec_device *ec_dev, u16 cmd_offset, u16 cmd, u32 *mask) { … } static void get_default_min_max_freq(enum motionsensor_type type, u32 *min_freq, u32 *max_freq, u32 *max_fifo_events) { … } static int cros_ec_sensor_set_ec_rate(struct cros_ec_sensors_core_state *st, int rate) { … } static ssize_t cros_ec_sensor_set_report_latency(struct device *dev, struct device_attribute *attr, const char *buf, size_t len) { … } static ssize_t cros_ec_sensor_get_report_latency(struct device *dev, struct device_attribute *attr, char *buf) { … } static IIO_DEVICE_ATTR(hwfifo_timeout, 0644, cros_ec_sensor_get_report_latency, cros_ec_sensor_set_report_latency, 0); static ssize_t hwfifo_watermark_max_show(struct device *dev, struct device_attribute *attr, char *buf) { … } static IIO_DEVICE_ATTR_RO(hwfifo_watermark_max, 0); static const struct iio_dev_attr *cros_ec_sensor_fifo_attributes[] = …; int cros_ec_sensors_push_data(struct iio_dev *indio_dev, s16 *data, s64 timestamp) { … } EXPORT_SYMBOL_GPL(…); static void cros_ec_sensors_core_clean(void *arg) { … } /** * cros_ec_sensors_core_init() - basic initialization of the core structure * @pdev: platform device created for the sensor * @indio_dev: iio device structure of the device * @physical_device: true if the device refers to a physical device * @trigger_capture: function pointer to call buffer is triggered, * for backward compatibility. * * Return: 0 on success, -errno on failure. */ int cros_ec_sensors_core_init(struct platform_device *pdev, struct iio_dev *indio_dev, bool physical_device, cros_ec_sensors_capture_t trigger_capture) { … } EXPORT_SYMBOL_GPL(…); /** * cros_ec_sensors_core_register() - Register callback to FIFO and IIO when * sensor is ready. * It must be called at the end of the sensor probe routine. * @dev: device created for the sensor * @indio_dev: iio device structure of the device * @push_data: function to call when cros_ec_sensorhub receives * a sample for that sensor. * * Return: 0 on success, -errno on failure. */ int cros_ec_sensors_core_register(struct device *dev, struct iio_dev *indio_dev, cros_ec_sensorhub_push_data_cb_t push_data) { … } EXPORT_SYMBOL_GPL(…); /** * cros_ec_motion_send_host_cmd() - send motion sense host command * @state: pointer to state information for device * @opt_length: optional length to reduce the response size, useful on the data * path. Otherwise, the maximal allowed response size is used * * When called, the sub-command is assumed to be set in param->cmd. * * Return: 0 on success, -errno on failure. */ int cros_ec_motion_send_host_cmd(struct cros_ec_sensors_core_state *state, u16 opt_length) { … } EXPORT_SYMBOL_GPL(…); static ssize_t cros_ec_sensors_calibrate(struct iio_dev *indio_dev, uintptr_t private, const struct iio_chan_spec *chan, const char *buf, size_t len) { … } static ssize_t cros_ec_sensors_id(struct iio_dev *indio_dev, uintptr_t private, const struct iio_chan_spec *chan, char *buf) { … } const struct iio_chan_spec_ext_info cros_ec_sensors_ext_info[] = …; EXPORT_SYMBOL_GPL(…); /** * cros_ec_sensors_idx_to_reg - convert index into offset in shared memory * @st: pointer to state information for device * @idx: sensor index (should be element of enum sensor_index) * * Return: address to read at */ static unsigned int cros_ec_sensors_idx_to_reg( struct cros_ec_sensors_core_state *st, unsigned int idx) { … } static int cros_ec_sensors_cmd_read_u8(struct cros_ec_device *ec, unsigned int offset, u8 *dest) { … } static int cros_ec_sensors_cmd_read_u16(struct cros_ec_device *ec, unsigned int offset, u16 *dest) { … } /** * cros_ec_sensors_read_until_not_busy() - read until is not busy * * @st: pointer to state information for device * * Read from EC status byte until it reads not busy. * Return: 8-bit status if ok, -errno on failure. */ static int cros_ec_sensors_read_until_not_busy( struct cros_ec_sensors_core_state *st) { … } /** * cros_ec_sensors_read_data_unsafe() - read acceleration data from EC shared memory * @indio_dev: pointer to IIO device * @scan_mask: bitmap of the sensor indices to scan * @data: location to store data * * This is the unsafe function for reading the EC data. It does not guarantee * that the EC will not modify the data as it is being read in. * * Return: 0 on success, -errno on failure. */ static int cros_ec_sensors_read_data_unsafe(struct iio_dev *indio_dev, unsigned long scan_mask, s16 *data) { … } /** * cros_ec_sensors_read_lpc() - read acceleration data from EC shared memory. * @indio_dev: pointer to IIO device. * @scan_mask: bitmap of the sensor indices to scan. * @data: location to store data. * * Note: this is the safe function for reading the EC data. It guarantees * that the data sampled was not modified by the EC while being read. * * Return: 0 on success, -errno on failure. */ int cros_ec_sensors_read_lpc(struct iio_dev *indio_dev, unsigned long scan_mask, s16 *data) { … } EXPORT_SYMBOL_GPL(…); /** * cros_ec_sensors_read_cmd() - retrieve data using the EC command protocol * @indio_dev: pointer to IIO device * @scan_mask: bitmap of the sensor indices to scan * @data: location to store data * * Return: 0 on success, -errno on failure. */ int cros_ec_sensors_read_cmd(struct iio_dev *indio_dev, unsigned long scan_mask, s16 *data) { … } EXPORT_SYMBOL_GPL(…); /** * cros_ec_sensors_capture() - the trigger handler function * @irq: the interrupt number. * @p: a pointer to the poll function. * * On a trigger event occurring, if the pollfunc is attached then this * handler is called as a threaded interrupt (and hence may sleep). It * is responsible for grabbing data from the device and pushing it into * the associated buffer. * * Return: IRQ_HANDLED */ irqreturn_t cros_ec_sensors_capture(int irq, void *p) { … } EXPORT_SYMBOL_GPL(…); /** * cros_ec_sensors_core_read() - function to request a value from the sensor * @st: pointer to state information for device * @chan: channel specification structure table * @val: will contain one element making up the returned value * @val2: will contain another element making up the returned value * @mask: specifies which values to be requested * * Return: the type of value returned by the device */ int cros_ec_sensors_core_read(struct cros_ec_sensors_core_state *st, struct iio_chan_spec const *chan, int *val, int *val2, long mask) { … } EXPORT_SYMBOL_GPL(…); /** * cros_ec_sensors_core_read_avail() - get available values * @indio_dev: pointer to state information for device * @chan: channel specification structure table * @vals: list of available values * @type: type of data returned * @length: number of data returned in the array * @mask: specifies which values to be requested * * Return: an error code, IIO_AVAIL_RANGE or IIO_AVAIL_LIST */ int cros_ec_sensors_core_read_avail(struct iio_dev *indio_dev, struct iio_chan_spec const *chan, const int **vals, int *type, int *length, long mask) { … } EXPORT_SYMBOL_GPL(…); /** * cros_ec_sensors_core_write() - function to write a value to the sensor * @st: pointer to state information for device * @chan: channel specification structure table * @val: first part of value to write * @val2: second part of value to write * @mask: specifies which values to write * * Return: the type of value returned by the device */ int cros_ec_sensors_core_write(struct cros_ec_sensors_core_state *st, struct iio_chan_spec const *chan, int val, int val2, long mask) { … } EXPORT_SYMBOL_GPL(…); static int __maybe_unused cros_ec_sensors_resume(struct device *dev) { … } SIMPLE_DEV_PM_OPS(cros_ec_sensors_pm_ops, NULL, cros_ec_sensors_resume); EXPORT_SYMBOL_GPL(…); MODULE_DESCRIPTION(…) …; MODULE_LICENSE(…) …;