1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Copyright 2021 Google LLC.
4  *
5  * Code shared between most Semtech SAR sensor driver.
6  */
7 
8 #ifndef IIO_SX_COMMON_H
9 #define IIO_SX_COMMON_H
10 
11 #include <linux/acpi.h>
12 #include <linux/iio/iio.h>
13 #include <linux/iio/types.h>
14 #include <linux/regulator/consumer.h>
15 #include <linux/types.h>
16 
17 struct device;
18 struct i2c_client;
19 struct regmap_config;
20 struct sx_common_data;
21 
22 #define SX_COMMON_REG_IRQ_SRC				0x00
23 
24 #define SX_COMMON_MAX_NUM_CHANNELS	4
25 static_assert(SX_COMMON_MAX_NUM_CHANNELS < BITS_PER_LONG);
26 
27 struct sx_common_reg_default {
28 	u8 reg;
29 	u8 def;
30 	const char *property;
31 };
32 
33 /**
34  * struct sx_common_ops: function pointers needed by common code
35  *
36  * List functions needed by common code to gather information or configure
37  * the sensor.
38  *
39  * @read_prox_data:	Function to read raw proximity data.
40  * @check_whoami:	Set device name based on whoami register.
41  * @init_compensation:	Function to set initial compensation.
42  * @wait_for_sample:	When there are no physical IRQ, function to wait for a
43  *			sample to be ready.
44  * @get_default_reg:	Populate the initial value for a given register.
45  */
46 struct sx_common_ops {
47 	int (*read_prox_data)(struct sx_common_data *data,
48 			      const struct iio_chan_spec *chan, __be16 *val);
49 	int (*check_whoami)(struct device *dev, struct iio_dev *indio_dev);
50 	int (*init_compensation)(struct iio_dev *indio_dev);
51 	int (*wait_for_sample)(struct sx_common_data *data);
52 	const struct sx_common_reg_default  *
53 		(*get_default_reg)(struct device *dev, int idx,
54 				   struct sx_common_reg_default *reg_def);
55 };
56 
57 /**
58  * struct sx_common_chip_info: Semtech Sensor private chip information
59  *
60  * @reg_stat:		Main status register address.
61  * @reg_irq_msk:	IRQ mask register address.
62  * @reg_enable_chan:	Address to enable/disable channels.
63  *			Each phase presented by the sensor is an IIO channel..
64  * @reg_reset:		Reset register address.
65  * @mask_enable_chan:	Mask over the channels bits in the enable channel
66  *			register.
67  * @stat_offset:	Offset to check phase status.
68  * @irq_msk_offset:	Offset to enable interrupt in the IRQ mask
69  *			register.
70  * @num_channels:	Number of channels.
71  * @num_default_regs:	Number of internal registers that can be configured.
72  *
73  * @ops:		Private functions pointers.
74  * @iio_channels:	Description of exposed iio channels.
75  * @num_iio_channels:	Number of iio_channels.
76  * @iio_info:		iio_info structure for this driver.
77  */
78 struct sx_common_chip_info {
79 	unsigned int reg_stat;
80 	unsigned int reg_irq_msk;
81 	unsigned int reg_enable_chan;
82 	unsigned int reg_reset;
83 
84 	unsigned int mask_enable_chan;
85 	unsigned int stat_offset;
86 	unsigned int irq_msk_offset;
87 	unsigned int num_channels;
88 	int num_default_regs;
89 
90 	struct sx_common_ops ops;
91 
92 	const struct iio_chan_spec *iio_channels;
93 	int num_iio_channels;
94 	struct iio_info iio_info;
95 };
96 
97 /**
98  * struct sx_common_data: Semtech Sensor private data structure.
99  *
100  * @chip_info:		Structure defining sensor internals.
101  * @mutex:		Serialize access to registers and channel configuration.
102  * @completion:		completion object to wait for data acquisition.
103  * @client:		I2C client structure.
104  * @trig:		IIO trigger object.
105  * @regmap:		Register map.
106  * @chan_prox_stat:	Last reading of the proximity status for each channel.
107  *			We only send an event to user space when this changes.
108  * @trigger_enabled:	True when the device trigger is enabled.
109  * @buffer:		Buffer to store raw samples.
110  * @suspend_ctrl:	Remember enabled channels and sample rate during suspend.
111  * @chan_read:		Bit field for each raw channel enabled.
112  * @chan_event:		Bit field for each event enabled.
113  */
114 struct sx_common_data {
115 	const struct sx_common_chip_info *chip_info;
116 
117 	struct mutex mutex;
118 	struct completion completion;
119 	struct i2c_client *client;
120 	struct iio_trigger *trig;
121 	struct regmap *regmap;
122 
123 	unsigned long chan_prox_stat;
124 	bool trigger_enabled;
125 
126 	/* Ensure correct alignment of timestamp when present. */
127 	struct {
128 		__be16 channels[SX_COMMON_MAX_NUM_CHANNELS];
129 		s64 ts __aligned(8);
130 	} buffer;
131 
132 	unsigned int suspend_ctrl;
133 	unsigned long chan_read;
134 	unsigned long chan_event;
135 };
136 
137 int sx_common_read_proximity(struct sx_common_data *data,
138 			     const struct iio_chan_spec *chan, int *val);
139 
140 int sx_common_read_event_config(struct iio_dev *indio_dev,
141 				const struct iio_chan_spec *chan,
142 				enum iio_event_type type,
143 				enum iio_event_direction dir);
144 int sx_common_write_event_config(struct iio_dev *indio_dev,
145 				 const struct iio_chan_spec *chan,
146 				 enum iio_event_type type,
147 				 enum iio_event_direction dir, int state);
148 
149 int sx_common_probe(struct i2c_client *client,
150 		    const struct sx_common_chip_info *chip_info,
151 		    const struct regmap_config *regmap_config);
152 
153 void sx_common_get_raw_register_config(struct device *dev,
154 				       struct sx_common_reg_default *reg_def);
155 
156 /* 3 is the number of events defined by a single phase. */
157 extern const struct iio_event_spec sx_common_events[3];
158 
159 #endif  /* IIO_SX_COMMON_H */
160