blob: 5ffd763702dcc3346f49012d7c2fc7d4d8d54f8b [file] [log] [blame]
Denis Ciocca23491b52013-01-25 23:44:00 +00001/*
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>
Jonathan Cameron5ea86492013-02-27 19:41:59 +000018#include <linux/bitops.h>
Denis Ciocca23491b52013-01-25 23:44:00 +000019
20#define ST_SENSORS_TX_MAX_LENGTH 2
21#define ST_SENSORS_RX_MAX_LENGTH 6
22
23#define ST_SENSORS_ODR_LIST_MAX 10
24#define ST_SENSORS_FULLSCALE_AVL_MAX 10
25
26#define ST_SENSORS_NUMBER_ALL_CHANNELS 4
27#define ST_SENSORS_NUMBER_DATA_CHANNELS 3
28#define ST_SENSORS_ENABLE_ALL_AXIS 0x07
29#define ST_SENSORS_BYTE_FOR_CHANNEL 2
30#define ST_SENSORS_SCAN_X 0
31#define ST_SENSORS_SCAN_Y 1
32#define ST_SENSORS_SCAN_Z 2
Denis Ciocca23491b52013-01-25 23:44:00 +000033#define ST_SENSORS_DEFAULT_POWER_ON_VALUE 0x01
34#define ST_SENSORS_DEFAULT_POWER_OFF_VALUE 0x00
35#define ST_SENSORS_DEFAULT_WAI_ADDRESS 0x0f
36#define ST_SENSORS_DEFAULT_AXIS_ADDR 0x20
37#define ST_SENSORS_DEFAULT_AXIS_MASK 0x07
38#define ST_SENSORS_DEFAULT_AXIS_N_BIT 3
39
40#define ST_SENSORS_MAX_NAME 17
41#define ST_SENSORS_MAX_4WAI 7
42
Denis CIOCCA762011d2013-06-03 15:58:00 +010043#define ST_SENSORS_LSM_CHANNELS(device_type, mask, index, mod, \
44 ch2, s, endian, rbits, sbits, addr) \
Denis Ciocca23491b52013-01-25 23:44:00 +000045{ \
46 .type = device_type, \
Denis CIOCCA762011d2013-06-03 15:58:00 +010047 .modified = mod, \
48 .info_mask_separate = mask, \
Denis Ciocca23491b52013-01-25 23:44:00 +000049 .scan_index = index, \
Denis CIOCCA762011d2013-06-03 15:58:00 +010050 .channel2 = ch2, \
Denis Ciocca23491b52013-01-25 23:44:00 +000051 .address = addr, \
52 .scan_type = { \
Denis CIOCCA762011d2013-06-03 15:58:00 +010053 .sign = s, \
54 .realbits = rbits, \
55 .shift = sbits - rbits, \
56 .storagebits = sbits, \
Denis Ciocca23491b52013-01-25 23:44:00 +000057 .endianness = endian, \
58 }, \
59}
60
61#define ST_SENSOR_DEV_ATTR_SAMP_FREQ() \
62 IIO_DEV_ATTR_SAMP_FREQ(S_IWUSR | S_IRUGO, \
63 st_sensors_sysfs_get_sampling_frequency, \
64 st_sensors_sysfs_set_sampling_frequency)
65
66#define ST_SENSORS_DEV_ATTR_SAMP_FREQ_AVAIL() \
67 IIO_DEV_ATTR_SAMP_FREQ_AVAIL( \
68 st_sensors_sysfs_sampling_frequency_avail)
69
70#define ST_SENSORS_DEV_ATTR_SCALE_AVAIL(name) \
71 IIO_DEVICE_ATTR(name, S_IRUGO, \
72 st_sensors_sysfs_scale_avail, NULL , 0);
73
74struct st_sensor_odr_avl {
75 unsigned int hz;
76 u8 value;
77};
78
79struct st_sensor_odr {
80 u8 addr;
81 u8 mask;
82 struct st_sensor_odr_avl odr_avl[ST_SENSORS_ODR_LIST_MAX];
83};
84
85struct st_sensor_power {
86 u8 addr;
87 u8 mask;
88 u8 value_off;
89 u8 value_on;
90};
91
92struct st_sensor_axis {
93 u8 addr;
94 u8 mask;
95};
96
97struct st_sensor_fullscale_avl {
98 unsigned int num;
99 u8 value;
100 unsigned int gain;
101 unsigned int gain2;
102};
103
104struct st_sensor_fullscale {
105 u8 addr;
106 u8 mask;
107 struct st_sensor_fullscale_avl fs_avl[ST_SENSORS_FULLSCALE_AVL_MAX];
108};
109
110/**
111 * struct st_sensor_bdu - ST sensor device block data update
112 * @addr: address of the register.
113 * @mask: mask to write the block data update flag.
114 */
115struct st_sensor_bdu {
116 u8 addr;
117 u8 mask;
118};
119
120/**
121 * struct st_sensor_data_ready_irq - ST sensor device data-ready interrupt
122 * @addr: address of the register.
123 * @mask: mask to write the on/off value.
124 * struct ig1 - represents the Interrupt Generator 1 of sensors.
125 * @en_addr: address of the enable ig1 register.
126 * @en_mask: mask to write the on/off value for enable.
127 */
128struct st_sensor_data_ready_irq {
129 u8 addr;
130 u8 mask;
131 struct {
132 u8 en_addr;
133 u8 en_mask;
134 } ig1;
135};
136
137/**
138 * struct st_sensor_transfer_buffer - ST sensor device I/O buffer
139 * @buf_lock: Mutex to protect rx and tx buffers.
140 * @tx_buf: Buffer used by SPI transfer function to send data to the sensors.
141 * This buffer is used to avoid DMA not-aligned issue.
142 * @rx_buf: Buffer used by SPI transfer to receive data from sensors.
143 * This buffer is used to avoid DMA not-aligned issue.
144 */
145struct st_sensor_transfer_buffer {
146 struct mutex buf_lock;
147 u8 rx_buf[ST_SENSORS_RX_MAX_LENGTH];
148 u8 tx_buf[ST_SENSORS_TX_MAX_LENGTH] ____cacheline_aligned;
149};
150
151/**
152 * struct st_sensor_transfer_function - ST sensor device I/O function
153 * @read_byte: Function used to read one byte.
154 * @write_byte: Function used to write one byte.
155 * @read_multiple_byte: Function used to read multiple byte.
156 */
157struct st_sensor_transfer_function {
158 int (*read_byte) (struct st_sensor_transfer_buffer *tb,
159 struct device *dev, u8 reg_addr, u8 *res_byte);
160 int (*write_byte) (struct st_sensor_transfer_buffer *tb,
161 struct device *dev, u8 reg_addr, u8 data);
162 int (*read_multiple_byte) (struct st_sensor_transfer_buffer *tb,
163 struct device *dev, u8 reg_addr, int len, u8 *data,
164 bool multiread_bit);
165};
166
167/**
168 * struct st_sensors - ST sensors list
169 * @wai: Contents of WhoAmI register.
170 * @sensors_supported: List of supported sensors by struct itself.
171 * @ch: IIO channels for the sensor.
172 * @odr: Output data rate register and ODR list available.
173 * @pw: Power register of the sensor.
174 * @enable_axis: Enable one or more axis of the sensor.
175 * @fs: Full scale register and full scale list available.
176 * @bdu: Block data update register.
177 * @drdy_irq: Data ready register of the sensor.
178 * @multi_read_bit: Use or not particular bit for [I2C/SPI] multi-read.
179 * @bootime: samples to discard when sensor passing from power-down to power-up.
180 */
181struct st_sensors {
182 u8 wai;
183 char sensors_supported[ST_SENSORS_MAX_4WAI][ST_SENSORS_MAX_NAME];
184 struct iio_chan_spec *ch;
185 struct st_sensor_odr odr;
186 struct st_sensor_power pw;
187 struct st_sensor_axis enable_axis;
188 struct st_sensor_fullscale fs;
189 struct st_sensor_bdu bdu;
190 struct st_sensor_data_ready_irq drdy_irq;
191 bool multi_read_bit;
192 unsigned int bootime;
193};
194
195/**
196 * struct st_sensor_data - ST sensor device status
197 * @dev: Pointer to instance of struct device (I2C or SPI).
198 * @trig: The trigger in use by the core driver.
199 * @sensor: Pointer to the current sensor struct in use.
200 * @current_fullscale: Maximum range of measure by the sensor.
201 * @enabled: Status of the sensor (false->off, true->on).
202 * @multiread_bit: Use or not particular bit for [I2C/SPI] multiread.
203 * @buffer_data: Data used by buffer part.
204 * @odr: Output data rate of the sensor [Hz].
205 * @get_irq_data_ready: Function to get the IRQ used for data ready signal.
206 * @tf: Transfer function structure used by I/O operations.
207 * @tb: Transfer buffers and mutex used by I/O operations.
208 */
209struct st_sensor_data {
210 struct device *dev;
211 struct iio_trigger *trig;
212 struct st_sensors *sensor;
213 struct st_sensor_fullscale_avl *current_fullscale;
214
215 bool enabled;
216 bool multiread_bit;
217
218 char *buffer_data;
219
220 unsigned int odr;
221
222 unsigned int (*get_irq_data_ready) (struct iio_dev *indio_dev);
223
224 const struct st_sensor_transfer_function *tf;
225 struct st_sensor_transfer_buffer tb;
226};
227
228#ifdef CONFIG_IIO_BUFFER
Guenter Roeck5a4d7292013-03-01 15:57:00 +0000229irqreturn_t st_sensors_trigger_handler(int irq, void *p);
230
231int st_sensors_get_buffer_element(struct iio_dev *indio_dev, u8 *buf);
232#endif
233
234#ifdef CONFIG_IIO_TRIGGER
Denis Ciocca23491b52013-01-25 23:44:00 +0000235int st_sensors_allocate_trigger(struct iio_dev *indio_dev,
236 const struct iio_trigger_ops *trigger_ops);
237
238void st_sensors_deallocate_trigger(struct iio_dev *indio_dev);
239
Denis CIOCCA91ffbab2013-02-07 09:46:00 +0000240#else
241static inline int st_sensors_allocate_trigger(struct iio_dev *indio_dev,
242 const struct iio_trigger_ops *trigger_ops)
243{
244 return 0;
245}
246static inline void st_sensors_deallocate_trigger(struct iio_dev *indio_dev)
247{
248 return;
249}
Denis Ciocca23491b52013-01-25 23:44:00 +0000250#endif
251
252int st_sensors_init_sensor(struct iio_dev *indio_dev);
253
254int st_sensors_set_enable(struct iio_dev *indio_dev, bool enable);
255
256int st_sensors_set_axis_enable(struct iio_dev *indio_dev, u8 axis_enable);
257
Denis Ciocca23491b52013-01-25 23:44:00 +0000258int st_sensors_set_odr(struct iio_dev *indio_dev, unsigned int odr);
259
260int st_sensors_set_dataready_irq(struct iio_dev *indio_dev, bool enable);
261
262int st_sensors_set_fullscale_by_gain(struct iio_dev *indio_dev, int scale);
263
264int st_sensors_read_info_raw(struct iio_dev *indio_dev,
265 struct iio_chan_spec const *ch, int *val);
266
267int st_sensors_check_device_support(struct iio_dev *indio_dev,
268 int num_sensors_list, const struct st_sensors *sensors);
269
270ssize_t st_sensors_sysfs_get_sampling_frequency(struct device *dev,
271 struct device_attribute *attr, char *buf);
272
273ssize_t st_sensors_sysfs_set_sampling_frequency(struct device *dev,
274 struct device_attribute *attr, const char *buf, size_t size);
275
276ssize_t st_sensors_sysfs_sampling_frequency_avail(struct device *dev,
277 struct device_attribute *attr, char *buf);
278
279ssize_t st_sensors_sysfs_scale_avail(struct device *dev,
280 struct device_attribute *attr, char *buf);
281
282#endif /* ST_SENSORS_H */