1/* SPDX-License-Identifier: GPL-2.0-only */
2/*
3 * ChromeOS EC sensor hub
4 *
5 * Copyright (C) 2016 Google, Inc
6 */
7
8#ifndef __CROS_EC_SENSORS_CORE_H
9#define __CROS_EC_SENSORS_CORE_H
10
11#include <linux/iio/iio.h>
12#include <linux/irqreturn.h>
13#include <linux/platform_data/cros_ec_commands.h>
14#include <linux/platform_data/cros_ec_proto.h>
15#include <linux/platform_data/cros_ec_sensorhub.h>
16
17enum {
18	CROS_EC_SENSOR_X,
19	CROS_EC_SENSOR_Y,
20	CROS_EC_SENSOR_Z,
21	CROS_EC_SENSOR_MAX_AXIS,
22};
23
24/* EC returns sensor values using signed 16 bit registers */
25#define CROS_EC_SENSOR_BITS 16
26
27/*
28 * 4 16 bit channels are allowed.
29 * Good enough for current sensors, they use up to 3 16 bit vectors.
30 */
31#define CROS_EC_SAMPLE_SIZE  (sizeof(s64) * 2)
32
33typedef irqreturn_t (*cros_ec_sensors_capture_t)(int irq, void *p);
34
35/**
36 * struct cros_ec_sensors_core_state - state data for EC sensors IIO driver
37 * @ec:				cros EC device structure
38 * @cmd_lock:			lock used to prevent simultaneous access to the
39 *				commands.
40 * @msg:			cros EC command structure
41 * @param:			motion sensor parameters structure
42 * @resp:			motion sensor response structure
43 * @type:			type of motion sensor
44 * @range_updated:		True if the range of the sensor has been
45 *				updated.
46 * @curr_range:			If updated, the current range value.
47 *				It will be reapplied at every resume.
48 * @calib:			calibration parameters. Note that trigger
49 *				captured data will always provide the calibrated
50 *				data
51 * @samples:			static array to hold data from a single capture.
52 *				For each channel we need 2 bytes, except for
53 *				the timestamp. The timestamp is always last and
54 *				is always 8-byte aligned.
55 * @read_ec_sensors_data:	function used for accessing sensors values
56 * @fifo_max_event_count:	Size of the EC sensor FIFO
57 * @frequencies:		Table of known available frequencies:
58 *				0, Min and Max in mHz
59 */
60struct cros_ec_sensors_core_state {
61	struct cros_ec_device *ec;
62	struct mutex cmd_lock;
63
64	struct cros_ec_command *msg;
65	struct ec_params_motion_sense param;
66	struct ec_response_motion_sense *resp;
67
68	enum motionsensor_type type;
69
70	bool range_updated;
71	int curr_range;
72
73	struct calib_data {
74		s16 offset;
75		u16 scale;
76	} calib[CROS_EC_SENSOR_MAX_AXIS];
77	s8 sign[CROS_EC_SENSOR_MAX_AXIS];
78	u8 samples[CROS_EC_SAMPLE_SIZE] __aligned(8);
79
80	int (*read_ec_sensors_data)(struct iio_dev *indio_dev,
81				    unsigned long scan_mask, s16 *data);
82
83	u32 fifo_max_event_count;
84	int frequencies[6];
85};
86
87int cros_ec_sensors_read_lpc(struct iio_dev *indio_dev, unsigned long scan_mask,
88			     s16 *data);
89
90int cros_ec_sensors_read_cmd(struct iio_dev *indio_dev, unsigned long scan_mask,
91			     s16 *data);
92
93struct platform_device;
94int cros_ec_sensors_core_init(struct platform_device *pdev,
95			      struct iio_dev *indio_dev, bool physical_device,
96			      cros_ec_sensors_capture_t trigger_capture);
97
98int cros_ec_sensors_core_register(struct device *dev,
99				  struct iio_dev *indio_dev,
100				  cros_ec_sensorhub_push_data_cb_t push_data);
101
102irqreturn_t cros_ec_sensors_capture(int irq, void *p);
103int cros_ec_sensors_push_data(struct iio_dev *indio_dev,
104			      s16 *data,
105			      s64 timestamp);
106
107int cros_ec_motion_send_host_cmd(struct cros_ec_sensors_core_state *st,
108				 u16 opt_length);
109
110int cros_ec_sensors_core_read(struct cros_ec_sensors_core_state *st,
111			      struct iio_chan_spec const *chan,
112			      int *val, int *val2, long mask);
113
114int cros_ec_sensors_core_read_avail(struct iio_dev *indio_dev,
115				    struct iio_chan_spec const *chan,
116				    const int **vals,
117				    int *type,
118				    int *length,
119				    long mask);
120
121int cros_ec_sensors_core_write(struct cros_ec_sensors_core_state *st,
122			       struct iio_chan_spec const *chan,
123			       int val, int val2, long mask);
124
125extern const struct dev_pm_ops cros_ec_sensors_pm_ops;
126
127/* List of extended channel specification for all sensors. */
128extern const struct iio_chan_spec_ext_info cros_ec_sensors_ext_info[];
129
130#endif  /* __CROS_EC_SENSORS_CORE_H */
131