]>
Commit | Line | Data |
---|---|---|
23491b51 DC |
1 | /* |
2 | * STMicroelectronics sensors library driver | |
3 | * | |
4 | * Copyright 2012-2013 STMicroelectronics Inc. | |
5 | * | |
6 | * Denis Ciocca <denis.ciocca@st.com> | |
7 | * | |
8 | * Licensed under the GPL-2. | |
9 | */ | |
10 | ||
11 | #ifndef ST_SENSORS_H | |
12 | #define ST_SENSORS_H | |
13 | ||
14 | #include <linux/i2c.h> | |
15 | #include <linux/spi/spi.h> | |
16 | #include <linux/irqreturn.h> | |
17 | #include <linux/iio/trigger.h> | |
5ea86494 | 18 | #include <linux/bitops.h> |
77448761 | 19 | #include <linux/regulator/consumer.h> |
23491b51 | 20 | |
23cde4d6 DC |
21 | #include <linux/platform_data/st_sensors_pdata.h> |
22 | ||
23491b51 DC |
23 | #define ST_SENSORS_TX_MAX_LENGTH 2 |
24 | #define ST_SENSORS_RX_MAX_LENGTH 6 | |
25 | ||
26 | #define ST_SENSORS_ODR_LIST_MAX 10 | |
27 | #define ST_SENSORS_FULLSCALE_AVL_MAX 10 | |
28 | ||
29 | #define ST_SENSORS_NUMBER_ALL_CHANNELS 4 | |
23491b51 | 30 | #define ST_SENSORS_ENABLE_ALL_AXIS 0x07 |
23491b51 DC |
31 | #define ST_SENSORS_SCAN_X 0 |
32 | #define ST_SENSORS_SCAN_Y 1 | |
33 | #define ST_SENSORS_SCAN_Z 2 | |
23491b51 DC |
34 | #define ST_SENSORS_DEFAULT_POWER_ON_VALUE 0x01 |
35 | #define ST_SENSORS_DEFAULT_POWER_OFF_VALUE 0x00 | |
36 | #define ST_SENSORS_DEFAULT_WAI_ADDRESS 0x0f | |
37 | #define ST_SENSORS_DEFAULT_AXIS_ADDR 0x20 | |
38 | #define ST_SENSORS_DEFAULT_AXIS_MASK 0x07 | |
39 | #define ST_SENSORS_DEFAULT_AXIS_N_BIT 3 | |
97865fe4 | 40 | #define ST_SENSORS_DEFAULT_STAT_ADDR 0x27 |
23491b51 DC |
41 | |
42 | #define ST_SENSORS_MAX_NAME 17 | |
43 | #define ST_SENSORS_MAX_4WAI 7 | |
44 | ||
762011d6 DC |
45 | #define ST_SENSORS_LSM_CHANNELS(device_type, mask, index, mod, \ |
46 | ch2, s, endian, rbits, sbits, addr) \ | |
23491b51 DC |
47 | { \ |
48 | .type = device_type, \ | |
762011d6 DC |
49 | .modified = mod, \ |
50 | .info_mask_separate = mask, \ | |
2d239c9e | 51 | .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \ |
23491b51 | 52 | .scan_index = index, \ |
762011d6 | 53 | .channel2 = ch2, \ |
23491b51 DC |
54 | .address = addr, \ |
55 | .scan_type = { \ | |
762011d6 DC |
56 | .sign = s, \ |
57 | .realbits = rbits, \ | |
58 | .shift = sbits - rbits, \ | |
59 | .storagebits = sbits, \ | |
23491b51 DC |
60 | .endianness = endian, \ |
61 | }, \ | |
62 | } | |
63 | ||
23491b51 DC |
64 | #define ST_SENSORS_DEV_ATTR_SAMP_FREQ_AVAIL() \ |
65 | IIO_DEV_ATTR_SAMP_FREQ_AVAIL( \ | |
66 | st_sensors_sysfs_sampling_frequency_avail) | |
67 | ||
68 | #define ST_SENSORS_DEV_ATTR_SCALE_AVAIL(name) \ | |
69 | IIO_DEVICE_ATTR(name, S_IRUGO, \ | |
70 | st_sensors_sysfs_scale_avail, NULL , 0); | |
71 | ||
72 | struct st_sensor_odr_avl { | |
73 | unsigned int hz; | |
74 | u8 value; | |
75 | }; | |
76 | ||
77 | struct st_sensor_odr { | |
78 | u8 addr; | |
79 | u8 mask; | |
80 | struct st_sensor_odr_avl odr_avl[ST_SENSORS_ODR_LIST_MAX]; | |
81 | }; | |
82 | ||
83 | struct st_sensor_power { | |
84 | u8 addr; | |
85 | u8 mask; | |
86 | u8 value_off; | |
87 | u8 value_on; | |
88 | }; | |
89 | ||
90 | struct st_sensor_axis { | |
91 | u8 addr; | |
92 | u8 mask; | |
93 | }; | |
94 | ||
95 | struct st_sensor_fullscale_avl { | |
96 | unsigned int num; | |
97 | u8 value; | |
98 | unsigned int gain; | |
99 | unsigned int gain2; | |
100 | }; | |
101 | ||
102 | struct st_sensor_fullscale { | |
103 | u8 addr; | |
104 | u8 mask; | |
105 | struct st_sensor_fullscale_avl fs_avl[ST_SENSORS_FULLSCALE_AVL_MAX]; | |
106 | }; | |
107 | ||
108 | /** | |
109 | * struct st_sensor_bdu - ST sensor device block data update | |
110 | * @addr: address of the register. | |
111 | * @mask: mask to write the block data update flag. | |
112 | */ | |
113 | struct st_sensor_bdu { | |
114 | u8 addr; | |
115 | u8 mask; | |
116 | }; | |
117 | ||
118 | /** | |
119 | * struct st_sensor_data_ready_irq - ST sensor device data-ready interrupt | |
120 | * @addr: address of the register. | |
23cde4d6 DC |
121 | * @mask_int1: mask to enable/disable IRQ on INT1 pin. |
122 | * @mask_int2: mask to enable/disable IRQ on INT2 pin. | |
a9fd053b LW |
123 | * @addr_ihl: address to enable/disable active low on the INT lines. |
124 | * @mask_ihl: mask to enable/disable active low on the INT lines. | |
97865fe4 | 125 | * @addr_stat_drdy: address to read status of DRDY (data ready) interrupt |
23491b51 DC |
126 | * struct ig1 - represents the Interrupt Generator 1 of sensors. |
127 | * @en_addr: address of the enable ig1 register. | |
128 | * @en_mask: mask to write the on/off value for enable. | |
129 | */ | |
130 | struct st_sensor_data_ready_irq { | |
131 | u8 addr; | |
23cde4d6 DC |
132 | u8 mask_int1; |
133 | u8 mask_int2; | |
a9fd053b LW |
134 | u8 addr_ihl; |
135 | u8 mask_ihl; | |
97865fe4 | 136 | u8 addr_stat_drdy; |
23491b51 DC |
137 | struct { |
138 | u8 en_addr; | |
139 | u8 en_mask; | |
140 | } ig1; | |
141 | }; | |
142 | ||
143 | /** | |
144 | * struct st_sensor_transfer_buffer - ST sensor device I/O buffer | |
145 | * @buf_lock: Mutex to protect rx and tx buffers. | |
146 | * @tx_buf: Buffer used by SPI transfer function to send data to the sensors. | |
147 | * This buffer is used to avoid DMA not-aligned issue. | |
148 | * @rx_buf: Buffer used by SPI transfer to receive data from sensors. | |
149 | * This buffer is used to avoid DMA not-aligned issue. | |
150 | */ | |
151 | struct st_sensor_transfer_buffer { | |
152 | struct mutex buf_lock; | |
153 | u8 rx_buf[ST_SENSORS_RX_MAX_LENGTH]; | |
154 | u8 tx_buf[ST_SENSORS_TX_MAX_LENGTH] ____cacheline_aligned; | |
155 | }; | |
156 | ||
157 | /** | |
158 | * struct st_sensor_transfer_function - ST sensor device I/O function | |
159 | * @read_byte: Function used to read one byte. | |
160 | * @write_byte: Function used to write one byte. | |
161 | * @read_multiple_byte: Function used to read multiple byte. | |
162 | */ | |
163 | struct st_sensor_transfer_function { | |
164 | int (*read_byte) (struct st_sensor_transfer_buffer *tb, | |
165 | struct device *dev, u8 reg_addr, u8 *res_byte); | |
166 | int (*write_byte) (struct st_sensor_transfer_buffer *tb, | |
167 | struct device *dev, u8 reg_addr, u8 data); | |
168 | int (*read_multiple_byte) (struct st_sensor_transfer_buffer *tb, | |
169 | struct device *dev, u8 reg_addr, int len, u8 *data, | |
170 | bool multiread_bit); | |
171 | }; | |
172 | ||
173 | /** | |
a7ee8839 | 174 | * struct st_sensor_settings - ST specific sensor settings |
23491b51 | 175 | * @wai: Contents of WhoAmI register. |
bc27381e | 176 | * @wai_addr: The address of WhoAmI register. |
23491b51 DC |
177 | * @sensors_supported: List of supported sensors by struct itself. |
178 | * @ch: IIO channels for the sensor. | |
179 | * @odr: Output data rate register and ODR list available. | |
180 | * @pw: Power register of the sensor. | |
181 | * @enable_axis: Enable one or more axis of the sensor. | |
182 | * @fs: Full scale register and full scale list available. | |
183 | * @bdu: Block data update register. | |
184 | * @drdy_irq: Data ready register of the sensor. | |
185 | * @multi_read_bit: Use or not particular bit for [I2C/SPI] multi-read. | |
186 | * @bootime: samples to discard when sensor passing from power-down to power-up. | |
187 | */ | |
a7ee8839 | 188 | struct st_sensor_settings { |
23491b51 | 189 | u8 wai; |
bc27381e | 190 | u8 wai_addr; |
23491b51 DC |
191 | char sensors_supported[ST_SENSORS_MAX_4WAI][ST_SENSORS_MAX_NAME]; |
192 | struct iio_chan_spec *ch; | |
ea01f2c1 | 193 | int num_ch; |
23491b51 DC |
194 | struct st_sensor_odr odr; |
195 | struct st_sensor_power pw; | |
196 | struct st_sensor_axis enable_axis; | |
197 | struct st_sensor_fullscale fs; | |
198 | struct st_sensor_bdu bdu; | |
199 | struct st_sensor_data_ready_irq drdy_irq; | |
200 | bool multi_read_bit; | |
201 | unsigned int bootime; | |
202 | }; | |
203 | ||
204 | /** | |
205 | * struct st_sensor_data - ST sensor device status | |
206 | * @dev: Pointer to instance of struct device (I2C or SPI). | |
207 | * @trig: The trigger in use by the core driver. | |
a7ee8839 | 208 | * @sensor_settings: Pointer to the specific sensor settings in use. |
23491b51 | 209 | * @current_fullscale: Maximum range of measure by the sensor. |
77448761 | 210 | * @vdd: Pointer to sensor's Vdd power supply |
71e1980c | 211 | * @vdd_io: Pointer to sensor's Vdd-IO power supply |
23491b51 DC |
212 | * @enabled: Status of the sensor (false->off, true->on). |
213 | * @multiread_bit: Use or not particular bit for [I2C/SPI] multiread. | |
214 | * @buffer_data: Data used by buffer part. | |
215 | * @odr: Output data rate of the sensor [Hz]. | |
607a568a | 216 | * num_data_channels: Number of data channels used in buffer. |
23cde4d6 | 217 | * @drdy_int_pin: Redirect DRDY on pin 1 (1) or pin 2 (2). |
23491b51 DC |
218 | * @get_irq_data_ready: Function to get the IRQ used for data ready signal. |
219 | * @tf: Transfer function structure used by I/O operations. | |
220 | * @tb: Transfer buffers and mutex used by I/O operations. | |
221 | */ | |
222 | struct st_sensor_data { | |
223 | struct device *dev; | |
224 | struct iio_trigger *trig; | |
a7ee8839 | 225 | struct st_sensor_settings *sensor_settings; |
23491b51 | 226 | struct st_sensor_fullscale_avl *current_fullscale; |
77448761 | 227 | struct regulator *vdd; |
71e1980c | 228 | struct regulator *vdd_io; |
23491b51 DC |
229 | |
230 | bool enabled; | |
231 | bool multiread_bit; | |
232 | ||
233 | char *buffer_data; | |
234 | ||
235 | unsigned int odr; | |
607a568a | 236 | unsigned int num_data_channels; |
23491b51 | 237 | |
23cde4d6 DC |
238 | u8 drdy_int_pin; |
239 | ||
23491b51 DC |
240 | unsigned int (*get_irq_data_ready) (struct iio_dev *indio_dev); |
241 | ||
242 | const struct st_sensor_transfer_function *tf; | |
243 | struct st_sensor_transfer_buffer tb; | |
244 | }; | |
245 | ||
246 | #ifdef CONFIG_IIO_BUFFER | |
5a4d7291 GR |
247 | irqreturn_t st_sensors_trigger_handler(int irq, void *p); |
248 | ||
249 | int st_sensors_get_buffer_element(struct iio_dev *indio_dev, u8 *buf); | |
250 | #endif | |
251 | ||
252 | #ifdef CONFIG_IIO_TRIGGER | |
23491b51 DC |
253 | int st_sensors_allocate_trigger(struct iio_dev *indio_dev, |
254 | const struct iio_trigger_ops *trigger_ops); | |
255 | ||
256 | void st_sensors_deallocate_trigger(struct iio_dev *indio_dev); | |
257 | ||
91ffbabf DC |
258 | #else |
259 | static inline int st_sensors_allocate_trigger(struct iio_dev *indio_dev, | |
260 | const struct iio_trigger_ops *trigger_ops) | |
261 | { | |
262 | return 0; | |
263 | } | |
264 | static inline void st_sensors_deallocate_trigger(struct iio_dev *indio_dev) | |
265 | { | |
266 | return; | |
267 | } | |
23491b51 DC |
268 | #endif |
269 | ||
23cde4d6 DC |
270 | int st_sensors_init_sensor(struct iio_dev *indio_dev, |
271 | struct st_sensors_platform_data *pdata); | |
23491b51 DC |
272 | |
273 | int st_sensors_set_enable(struct iio_dev *indio_dev, bool enable); | |
274 | ||
275 | int st_sensors_set_axis_enable(struct iio_dev *indio_dev, u8 axis_enable); | |
276 | ||
ea7e586b LW |
277 | void st_sensors_power_enable(struct iio_dev *indio_dev); |
278 | ||
279 | void st_sensors_power_disable(struct iio_dev *indio_dev); | |
280 | ||
a0175b9c LW |
281 | int st_sensors_debugfs_reg_access(struct iio_dev *indio_dev, |
282 | unsigned reg, unsigned writeval, | |
283 | unsigned *readval); | |
284 | ||
23491b51 DC |
285 | int st_sensors_set_odr(struct iio_dev *indio_dev, unsigned int odr); |
286 | ||
287 | int st_sensors_set_dataready_irq(struct iio_dev *indio_dev, bool enable); | |
288 | ||
289 | int st_sensors_set_fullscale_by_gain(struct iio_dev *indio_dev, int scale); | |
290 | ||
291 | int st_sensors_read_info_raw(struct iio_dev *indio_dev, | |
292 | struct iio_chan_spec const *ch, int *val); | |
293 | ||
294 | int st_sensors_check_device_support(struct iio_dev *indio_dev, | |
a7ee8839 | 295 | int num_sensors_list, const struct st_sensor_settings *sensor_settings); |
23491b51 | 296 | |
23491b51 DC |
297 | ssize_t st_sensors_sysfs_sampling_frequency_avail(struct device *dev, |
298 | struct device_attribute *attr, char *buf); | |
299 | ||
300 | ssize_t st_sensors_sysfs_scale_avail(struct device *dev, | |
301 | struct device_attribute *attr, char *buf); | |
302 | ||
303 | #endif /* ST_SENSORS_H */ |