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 
17 enum {
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 
33 typedef 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  * @loc:			location where the motion sensor is placed
45  * @range_updated:		True if the range of the sensor has been
46  *				updated.
47  * @curr_range:			If updated, the current range value.
48  *				It will be reapplied at every resume.
49  * @calib:			calibration parameters. Note that trigger
50  *				captured data will always provide the calibrated
51  *				data
52  * @samples:			static array to hold data from a single capture.
53  *				For each channel we need 2 bytes, except for
54  *				the timestamp. The timestamp is always last and
55  *				is always 8-byte aligned.
56  * @read_ec_sensors_data:	function used for accessing sensors values
57  * @fifo_max_event_count:	Size of the EC sensor FIFO
58  * @frequencies:		Table of known available frequencies:
59  *				0, Min and Max in mHz
60  */
61 struct cros_ec_sensors_core_state {
62 	struct cros_ec_device *ec;
63 	struct mutex cmd_lock;
64 
65 	struct cros_ec_command *msg;
66 	struct ec_params_motion_sense param;
67 	struct ec_response_motion_sense *resp;
68 
69 	enum motionsensor_type type;
70 	enum motionsensor_location loc;
71 
72 	bool range_updated;
73 	int curr_range;
74 
75 	struct calib_data {
76 		s16 offset;
77 		u16 scale;
78 	} calib[CROS_EC_SENSOR_MAX_AXIS];
79 	s8 sign[CROS_EC_SENSOR_MAX_AXIS];
80 	u8 samples[CROS_EC_SAMPLE_SIZE] __aligned(8);
81 
82 	int (*read_ec_sensors_data)(struct iio_dev *indio_dev,
83 				    unsigned long scan_mask, s16 *data);
84 
85 	u32 fifo_max_event_count;
86 	int frequencies[6];
87 };
88 
89 int cros_ec_sensors_read_lpc(struct iio_dev *indio_dev, unsigned long scan_mask,
90 			     s16 *data);
91 
92 int cros_ec_sensors_read_cmd(struct iio_dev *indio_dev, unsigned long scan_mask,
93 			     s16 *data);
94 
95 struct platform_device;
96 int cros_ec_sensors_core_init(struct platform_device *pdev,
97 			      struct iio_dev *indio_dev, bool physical_device,
98 			      cros_ec_sensors_capture_t trigger_capture,
99 			      cros_ec_sensorhub_push_data_cb_t push_data);
100 
101 irqreturn_t cros_ec_sensors_capture(int irq, void *p);
102 int cros_ec_sensors_push_data(struct iio_dev *indio_dev,
103 			      s16 *data,
104 			      s64 timestamp);
105 
106 int cros_ec_motion_send_host_cmd(struct cros_ec_sensors_core_state *st,
107 				 u16 opt_length);
108 
109 int cros_ec_sensors_core_read(struct cros_ec_sensors_core_state *st,
110 			      struct iio_chan_spec const *chan,
111 			      int *val, int *val2, long mask);
112 
113 int cros_ec_sensors_core_read_avail(struct iio_dev *indio_dev,
114 				    struct iio_chan_spec const *chan,
115 				    const int **vals,
116 				    int *type,
117 				    int *length,
118 				    long mask);
119 
120 int cros_ec_sensors_core_write(struct cros_ec_sensors_core_state *st,
121 			       struct iio_chan_spec const *chan,
122 			       int val, int val2, long mask);
123 
124 extern const struct dev_pm_ops cros_ec_sensors_pm_ops;
125 
126 /* List of extended channel specification for all sensors. */
127 extern const struct iio_chan_spec_ext_info cros_ec_sensors_ext_info[];
128 
129 #endif  /* __CROS_EC_SENSORS_CORE_H */
130