2 * STMicroelectronics sensors library driver
4 * Copyright 2012-2013 STMicroelectronics Inc.
6 * Denis Ciocca <denis.ciocca@st.com>
8 * Licensed under the GPL-2.
14 #include <linux/i2c.h>
15 #include <linux/spi/spi.h>
16 #include <linux/irqreturn.h>
17 #include <linux/iio/trigger.h>
18 #include <linux/bitops.h>
19 #include <linux/regulator/consumer.h>
21 #include <linux/platform_data/st_sensors_pdata.h>
23 #define ST_SENSORS_TX_MAX_LENGTH 2
24 #define ST_SENSORS_RX_MAX_LENGTH 6
26 #define ST_SENSORS_ODR_LIST_MAX 10
27 #define ST_SENSORS_FULLSCALE_AVL_MAX 10
29 #define ST_SENSORS_NUMBER_ALL_CHANNELS 4
30 #define ST_SENSORS_ENABLE_ALL_AXIS 0x07
31 #define ST_SENSORS_SCAN_X 0
32 #define ST_SENSORS_SCAN_Y 1
33 #define ST_SENSORS_SCAN_Z 2
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
40 #define ST_SENSORS_DEFAULT_STAT_ADDR 0x27
42 #define ST_SENSORS_MAX_NAME 17
43 #define ST_SENSORS_MAX_4WAI 7
45 #define ST_SENSORS_LSM_CHANNELS(device_type, mask, index, mod, \
46 ch2, s, endian, rbits, sbits, addr) \
48 .type = device_type, \
50 .info_mask_separate = mask, \
51 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
52 .scan_index = index, \
58 .shift = sbits - rbits, \
59 .storagebits = sbits, \
60 .endianness = endian, \
64 #define ST_SENSORS_DEV_ATTR_SAMP_FREQ_AVAIL() \
65 IIO_DEV_ATTR_SAMP_FREQ_AVAIL( \
66 st_sensors_sysfs_sampling_frequency_avail)
68 #define ST_SENSORS_DEV_ATTR_SCALE_AVAIL(name) \
69 IIO_DEVICE_ATTR(name, S_IRUGO, \
70 st_sensors_sysfs_scale_avail, NULL , 0);
72 struct st_sensor_odr_avl
{
77 struct st_sensor_odr
{
80 struct st_sensor_odr_avl odr_avl
[ST_SENSORS_ODR_LIST_MAX
];
83 struct st_sensor_power
{
90 struct st_sensor_axis
{
95 struct st_sensor_fullscale_avl
{
102 struct st_sensor_fullscale
{
105 struct st_sensor_fullscale_avl fs_avl
[ST_SENSORS_FULLSCALE_AVL_MAX
];
108 struct st_sensor_sim
{
114 * struct st_sensor_bdu - ST sensor device block data update
115 * @addr: address of the register.
116 * @mask: mask to write the block data update flag.
118 struct st_sensor_bdu
{
124 * struct st_sensor_das - ST sensor device data alignment selection
125 * @addr: address of the register.
126 * @mask: mask to write the das flag for left alignment.
128 struct st_sensor_das
{
134 * struct st_sensor_int_drdy - ST sensor device drdy line parameters
135 * @addr: address of INT drdy register.
136 * @mask: mask to enable drdy line.
137 * @addr_od: address to enable/disable Open Drain on the INT line.
138 * @mask_od: mask to enable/disable Open Drain on the INT line.
140 struct st_sensor_int_drdy
{
148 * struct st_sensor_data_ready_irq - ST sensor device data-ready interrupt
149 * struct int1 - data-ready configuration register for INT1 pin.
150 * struct int2 - data-ready configuration register for INT2 pin.
151 * @addr_ihl: address to enable/disable active low on the INT lines.
152 * @mask_ihl: mask to enable/disable active low on the INT lines.
153 * struct stat_drdy - status register of DRDY (data ready) interrupt.
154 * struct ig1 - represents the Interrupt Generator 1 of sensors.
155 * @en_addr: address of the enable ig1 register.
156 * @en_mask: mask to write the on/off value for enable.
158 struct st_sensor_data_ready_irq
{
159 struct st_sensor_int_drdy int1
;
160 struct st_sensor_int_drdy int2
;
174 * struct st_sensor_transfer_buffer - ST sensor device I/O buffer
175 * @buf_lock: Mutex to protect rx and tx buffers.
176 * @tx_buf: Buffer used by SPI transfer function to send data to the sensors.
177 * This buffer is used to avoid DMA not-aligned issue.
178 * @rx_buf: Buffer used by SPI transfer to receive data from sensors.
179 * This buffer is used to avoid DMA not-aligned issue.
181 struct st_sensor_transfer_buffer
{
182 struct mutex buf_lock
;
183 u8 rx_buf
[ST_SENSORS_RX_MAX_LENGTH
];
184 u8 tx_buf
[ST_SENSORS_TX_MAX_LENGTH
] ____cacheline_aligned
;
188 * struct st_sensor_transfer_function - ST sensor device I/O function
189 * @read_byte: Function used to read one byte.
190 * @write_byte: Function used to write one byte.
191 * @read_multiple_byte: Function used to read multiple byte.
193 struct st_sensor_transfer_function
{
194 int (*read_byte
) (struct st_sensor_transfer_buffer
*tb
,
195 struct device
*dev
, u8 reg_addr
, u8
*res_byte
);
196 int (*write_byte
) (struct st_sensor_transfer_buffer
*tb
,
197 struct device
*dev
, u8 reg_addr
, u8 data
);
198 int (*read_multiple_byte
) (struct st_sensor_transfer_buffer
*tb
,
199 struct device
*dev
, u8 reg_addr
, int len
, u8
*data
,
204 * struct st_sensor_settings - ST specific sensor settings
205 * @wai: Contents of WhoAmI register.
206 * @wai_addr: The address of WhoAmI register.
207 * @sensors_supported: List of supported sensors by struct itself.
208 * @ch: IIO channels for the sensor.
209 * @odr: Output data rate register and ODR list available.
210 * @pw: Power register of the sensor.
211 * @enable_axis: Enable one or more axis of the sensor.
212 * @fs: Full scale register and full scale list available.
213 * @bdu: Block data update register.
214 * @das: Data Alignment Selection register.
215 * @drdy_irq: Data ready register of the sensor.
216 * @sim: SPI serial interface mode register of the sensor.
217 * @multi_read_bit: Use or not particular bit for [I2C/SPI] multi-read.
218 * @bootime: samples to discard when sensor passing from power-down to power-up.
220 struct st_sensor_settings
{
223 char sensors_supported
[ST_SENSORS_MAX_4WAI
][ST_SENSORS_MAX_NAME
];
224 struct iio_chan_spec
*ch
;
226 struct st_sensor_odr odr
;
227 struct st_sensor_power pw
;
228 struct st_sensor_axis enable_axis
;
229 struct st_sensor_fullscale fs
;
230 struct st_sensor_bdu bdu
;
231 struct st_sensor_das das
;
232 struct st_sensor_data_ready_irq drdy_irq
;
233 struct st_sensor_sim sim
;
235 unsigned int bootime
;
239 * struct st_sensor_data - ST sensor device status
240 * @dev: Pointer to instance of struct device (I2C or SPI).
241 * @trig: The trigger in use by the core driver.
242 * @sensor_settings: Pointer to the specific sensor settings in use.
243 * @current_fullscale: Maximum range of measure by the sensor.
244 * @vdd: Pointer to sensor's Vdd power supply
245 * @vdd_io: Pointer to sensor's Vdd-IO power supply
246 * @enabled: Status of the sensor (false->off, true->on).
247 * @multiread_bit: Use or not particular bit for [I2C/SPI] multiread.
248 * @buffer_data: Data used by buffer part.
249 * @odr: Output data rate of the sensor [Hz].
250 * num_data_channels: Number of data channels used in buffer.
251 * @drdy_int_pin: Redirect DRDY on pin 1 (1) or pin 2 (2).
252 * @int_pin_open_drain: Set the interrupt/DRDY to open drain.
253 * @get_irq_data_ready: Function to get the IRQ used for data ready signal.
254 * @tf: Transfer function structure used by I/O operations.
255 * @tb: Transfer buffers and mutex used by I/O operations.
256 * @edge_irq: the IRQ triggers on edges and need special handling.
257 * @hw_irq_trigger: if we're using the hardware interrupt on the sensor.
258 * @hw_timestamp: Latest timestamp from the interrupt handler, when in use.
260 struct st_sensor_data
{
262 struct iio_trigger
*trig
;
263 struct st_sensor_settings
*sensor_settings
;
264 struct st_sensor_fullscale_avl
*current_fullscale
;
265 struct regulator
*vdd
;
266 struct regulator
*vdd_io
;
274 unsigned int num_data_channels
;
277 bool int_pin_open_drain
;
279 unsigned int (*get_irq_data_ready
) (struct iio_dev
*indio_dev
);
281 const struct st_sensor_transfer_function
*tf
;
282 struct st_sensor_transfer_buffer tb
;
289 #ifdef CONFIG_IIO_BUFFER
290 irqreturn_t
st_sensors_trigger_handler(int irq
, void *p
);
293 #ifdef CONFIG_IIO_TRIGGER
294 int st_sensors_allocate_trigger(struct iio_dev
*indio_dev
,
295 const struct iio_trigger_ops
*trigger_ops
);
297 void st_sensors_deallocate_trigger(struct iio_dev
*indio_dev
);
298 int st_sensors_validate_device(struct iio_trigger
*trig
,
299 struct iio_dev
*indio_dev
);
301 static inline int st_sensors_allocate_trigger(struct iio_dev
*indio_dev
,
302 const struct iio_trigger_ops
*trigger_ops
)
306 static inline void st_sensors_deallocate_trigger(struct iio_dev
*indio_dev
)
310 #define st_sensors_validate_device NULL
313 int st_sensors_init_sensor(struct iio_dev
*indio_dev
,
314 struct st_sensors_platform_data
*pdata
);
316 int st_sensors_set_enable(struct iio_dev
*indio_dev
, bool enable
);
318 int st_sensors_set_axis_enable(struct iio_dev
*indio_dev
, u8 axis_enable
);
320 int st_sensors_power_enable(struct iio_dev
*indio_dev
);
322 void st_sensors_power_disable(struct iio_dev
*indio_dev
);
324 int st_sensors_debugfs_reg_access(struct iio_dev
*indio_dev
,
325 unsigned reg
, unsigned writeval
,
328 int st_sensors_set_odr(struct iio_dev
*indio_dev
, unsigned int odr
);
330 int st_sensors_set_dataready_irq(struct iio_dev
*indio_dev
, bool enable
);
332 int st_sensors_set_fullscale_by_gain(struct iio_dev
*indio_dev
, int scale
);
334 int st_sensors_read_info_raw(struct iio_dev
*indio_dev
,
335 struct iio_chan_spec
const *ch
, int *val
);
337 int st_sensors_check_device_support(struct iio_dev
*indio_dev
,
338 int num_sensors_list
, const struct st_sensor_settings
*sensor_settings
);
340 ssize_t
st_sensors_sysfs_sampling_frequency_avail(struct device
*dev
,
341 struct device_attribute
*attr
, char *buf
);
343 ssize_t
st_sensors_sysfs_scale_avail(struct device
*dev
,
344 struct device_attribute
*attr
, char *buf
);
347 void st_sensors_of_name_probe(struct device
*dev
,
348 const struct of_device_id
*match
,
349 char *name
, int len
);
351 static inline void st_sensors_of_name_probe(struct device
*dev
,
352 const struct of_device_id
*match
,
358 #endif /* ST_SENSORS_H */