Denis Ciocca | 23491b5 | 2013-01-25 23:44:00 +0000 | [diff] [blame] | 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> |
Jonathan Cameron | 5ea8649 | 2013-02-27 19:41:59 +0000 | [diff] [blame] | 18 | #include <linux/bitops.h> |
Lee Jones | 7744876 | 2013-09-16 17:02:00 +0100 | [diff] [blame] | 19 | #include <linux/regulator/consumer.h> |
Denis Ciocca | 23491b5 | 2013-01-25 23:44:00 +0000 | [diff] [blame] | 20 | |
Denis CIOCCA | 23cde4d | 2013-06-19 09:28:00 +0100 | [diff] [blame] | 21 | #include <linux/platform_data/st_sensors_pdata.h> |
| 22 | |
Denis Ciocca | 23491b5 | 2013-01-25 23:44:00 +0000 | [diff] [blame] | 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 |
Denis Ciocca | 23491b5 | 2013-01-25 23:44:00 +0000 | [diff] [blame] | 30 | #define ST_SENSORS_ENABLE_ALL_AXIS 0x07 |
Denis Ciocca | 23491b5 | 2013-01-25 23:44:00 +0000 | [diff] [blame] | 31 | #define ST_SENSORS_SCAN_X 0 |
| 32 | #define ST_SENSORS_SCAN_Y 1 |
| 33 | #define ST_SENSORS_SCAN_Z 2 |
Denis Ciocca | 23491b5 | 2013-01-25 23:44:00 +0000 | [diff] [blame] | 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 | |
| 41 | #define ST_SENSORS_MAX_NAME 17 |
| 42 | #define ST_SENSORS_MAX_4WAI 7 |
| 43 | |
Denis CIOCCA | 762011d | 2013-06-03 15:58:00 +0100 | [diff] [blame] | 44 | #define ST_SENSORS_LSM_CHANNELS(device_type, mask, index, mod, \ |
| 45 | ch2, s, endian, rbits, sbits, addr) \ |
Denis Ciocca | 23491b5 | 2013-01-25 23:44:00 +0000 | [diff] [blame] | 46 | { \ |
| 47 | .type = device_type, \ |
Denis CIOCCA | 762011d | 2013-06-03 15:58:00 +0100 | [diff] [blame] | 48 | .modified = mod, \ |
| 49 | .info_mask_separate = mask, \ |
Denis Ciocca | 23491b5 | 2013-01-25 23:44:00 +0000 | [diff] [blame] | 50 | .scan_index = index, \ |
Denis CIOCCA | 762011d | 2013-06-03 15:58:00 +0100 | [diff] [blame] | 51 | .channel2 = ch2, \ |
Denis Ciocca | 23491b5 | 2013-01-25 23:44:00 +0000 | [diff] [blame] | 52 | .address = addr, \ |
| 53 | .scan_type = { \ |
Denis CIOCCA | 762011d | 2013-06-03 15:58:00 +0100 | [diff] [blame] | 54 | .sign = s, \ |
| 55 | .realbits = rbits, \ |
| 56 | .shift = sbits - rbits, \ |
| 57 | .storagebits = sbits, \ |
Denis Ciocca | 23491b5 | 2013-01-25 23:44:00 +0000 | [diff] [blame] | 58 | .endianness = endian, \ |
| 59 | }, \ |
| 60 | } |
| 61 | |
| 62 | #define ST_SENSOR_DEV_ATTR_SAMP_FREQ() \ |
| 63 | IIO_DEV_ATTR_SAMP_FREQ(S_IWUSR | S_IRUGO, \ |
| 64 | st_sensors_sysfs_get_sampling_frequency, \ |
| 65 | st_sensors_sysfs_set_sampling_frequency) |
| 66 | |
| 67 | #define ST_SENSORS_DEV_ATTR_SAMP_FREQ_AVAIL() \ |
| 68 | IIO_DEV_ATTR_SAMP_FREQ_AVAIL( \ |
| 69 | st_sensors_sysfs_sampling_frequency_avail) |
| 70 | |
| 71 | #define ST_SENSORS_DEV_ATTR_SCALE_AVAIL(name) \ |
| 72 | IIO_DEVICE_ATTR(name, S_IRUGO, \ |
| 73 | st_sensors_sysfs_scale_avail, NULL , 0); |
| 74 | |
| 75 | struct st_sensor_odr_avl { |
| 76 | unsigned int hz; |
| 77 | u8 value; |
| 78 | }; |
| 79 | |
| 80 | struct st_sensor_odr { |
| 81 | u8 addr; |
| 82 | u8 mask; |
| 83 | struct st_sensor_odr_avl odr_avl[ST_SENSORS_ODR_LIST_MAX]; |
| 84 | }; |
| 85 | |
| 86 | struct st_sensor_power { |
| 87 | u8 addr; |
| 88 | u8 mask; |
| 89 | u8 value_off; |
| 90 | u8 value_on; |
| 91 | }; |
| 92 | |
| 93 | struct st_sensor_axis { |
| 94 | u8 addr; |
| 95 | u8 mask; |
| 96 | }; |
| 97 | |
| 98 | struct st_sensor_fullscale_avl { |
| 99 | unsigned int num; |
| 100 | u8 value; |
| 101 | unsigned int gain; |
| 102 | unsigned int gain2; |
| 103 | }; |
| 104 | |
| 105 | struct st_sensor_fullscale { |
| 106 | u8 addr; |
| 107 | u8 mask; |
| 108 | struct st_sensor_fullscale_avl fs_avl[ST_SENSORS_FULLSCALE_AVL_MAX]; |
| 109 | }; |
| 110 | |
| 111 | /** |
| 112 | * struct st_sensor_bdu - ST sensor device block data update |
| 113 | * @addr: address of the register. |
| 114 | * @mask: mask to write the block data update flag. |
| 115 | */ |
| 116 | struct st_sensor_bdu { |
| 117 | u8 addr; |
| 118 | u8 mask; |
| 119 | }; |
| 120 | |
| 121 | /** |
| 122 | * struct st_sensor_data_ready_irq - ST sensor device data-ready interrupt |
| 123 | * @addr: address of the register. |
Denis CIOCCA | 23cde4d | 2013-06-19 09:28:00 +0100 | [diff] [blame] | 124 | * @mask_int1: mask to enable/disable IRQ on INT1 pin. |
| 125 | * @mask_int2: mask to enable/disable IRQ on INT2 pin. |
Denis Ciocca | 23491b5 | 2013-01-25 23:44:00 +0000 | [diff] [blame] | 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; |
Denis CIOCCA | 23cde4d | 2013-06-19 09:28:00 +0100 | [diff] [blame] | 132 | u8 mask_int1; |
| 133 | u8 mask_int2; |
Denis Ciocca | 23491b5 | 2013-01-25 23:44:00 +0000 | [diff] [blame] | 134 | struct { |
| 135 | u8 en_addr; |
| 136 | u8 en_mask; |
| 137 | } ig1; |
| 138 | }; |
| 139 | |
| 140 | /** |
| 141 | * struct st_sensor_transfer_buffer - ST sensor device I/O buffer |
| 142 | * @buf_lock: Mutex to protect rx and tx buffers. |
| 143 | * @tx_buf: Buffer used by SPI transfer function to send data to the sensors. |
| 144 | * This buffer is used to avoid DMA not-aligned issue. |
| 145 | * @rx_buf: Buffer used by SPI transfer to receive data from sensors. |
| 146 | * This buffer is used to avoid DMA not-aligned issue. |
| 147 | */ |
| 148 | struct st_sensor_transfer_buffer { |
| 149 | struct mutex buf_lock; |
| 150 | u8 rx_buf[ST_SENSORS_RX_MAX_LENGTH]; |
| 151 | u8 tx_buf[ST_SENSORS_TX_MAX_LENGTH] ____cacheline_aligned; |
| 152 | }; |
| 153 | |
| 154 | /** |
| 155 | * struct st_sensor_transfer_function - ST sensor device I/O function |
| 156 | * @read_byte: Function used to read one byte. |
| 157 | * @write_byte: Function used to write one byte. |
| 158 | * @read_multiple_byte: Function used to read multiple byte. |
| 159 | */ |
| 160 | struct st_sensor_transfer_function { |
| 161 | int (*read_byte) (struct st_sensor_transfer_buffer *tb, |
| 162 | struct device *dev, u8 reg_addr, u8 *res_byte); |
| 163 | int (*write_byte) (struct st_sensor_transfer_buffer *tb, |
| 164 | struct device *dev, u8 reg_addr, u8 data); |
| 165 | int (*read_multiple_byte) (struct st_sensor_transfer_buffer *tb, |
| 166 | struct device *dev, u8 reg_addr, int len, u8 *data, |
| 167 | bool multiread_bit); |
| 168 | }; |
| 169 | |
| 170 | /** |
| 171 | * struct st_sensors - ST sensors list |
| 172 | * @wai: Contents of WhoAmI register. |
| 173 | * @sensors_supported: List of supported sensors by struct itself. |
| 174 | * @ch: IIO channels for the sensor. |
| 175 | * @odr: Output data rate register and ODR list available. |
| 176 | * @pw: Power register of the sensor. |
| 177 | * @enable_axis: Enable one or more axis of the sensor. |
| 178 | * @fs: Full scale register and full scale list available. |
| 179 | * @bdu: Block data update register. |
| 180 | * @drdy_irq: Data ready register of the sensor. |
| 181 | * @multi_read_bit: Use or not particular bit for [I2C/SPI] multi-read. |
| 182 | * @bootime: samples to discard when sensor passing from power-down to power-up. |
| 183 | */ |
| 184 | struct st_sensors { |
| 185 | u8 wai; |
| 186 | char sensors_supported[ST_SENSORS_MAX_4WAI][ST_SENSORS_MAX_NAME]; |
| 187 | struct iio_chan_spec *ch; |
Lee Jones | ea01f2c | 2013-09-10 13:49:00 +0100 | [diff] [blame] | 188 | int num_ch; |
Denis Ciocca | 23491b5 | 2013-01-25 23:44:00 +0000 | [diff] [blame] | 189 | struct st_sensor_odr odr; |
| 190 | struct st_sensor_power pw; |
| 191 | struct st_sensor_axis enable_axis; |
| 192 | struct st_sensor_fullscale fs; |
| 193 | struct st_sensor_bdu bdu; |
| 194 | struct st_sensor_data_ready_irq drdy_irq; |
| 195 | bool multi_read_bit; |
| 196 | unsigned int bootime; |
| 197 | }; |
| 198 | |
| 199 | /** |
| 200 | * struct st_sensor_data - ST sensor device status |
| 201 | * @dev: Pointer to instance of struct device (I2C or SPI). |
| 202 | * @trig: The trigger in use by the core driver. |
| 203 | * @sensor: Pointer to the current sensor struct in use. |
| 204 | * @current_fullscale: Maximum range of measure by the sensor. |
Lee Jones | 7744876 | 2013-09-16 17:02:00 +0100 | [diff] [blame] | 205 | * @vdd: Pointer to sensor's Vdd power supply |
Lee Jones | 71e1980 | 2013-09-16 17:02:00 +0100 | [diff] [blame] | 206 | * @vdd_io: Pointer to sensor's Vdd-IO power supply |
Denis Ciocca | 23491b5 | 2013-01-25 23:44:00 +0000 | [diff] [blame] | 207 | * @enabled: Status of the sensor (false->off, true->on). |
| 208 | * @multiread_bit: Use or not particular bit for [I2C/SPI] multiread. |
| 209 | * @buffer_data: Data used by buffer part. |
| 210 | * @odr: Output data rate of the sensor [Hz]. |
Denis CIOCCA | 607a568 | 2013-06-03 15:58:00 +0100 | [diff] [blame] | 211 | * num_data_channels: Number of data channels used in buffer. |
Denis CIOCCA | 23cde4d | 2013-06-19 09:28:00 +0100 | [diff] [blame] | 212 | * @drdy_int_pin: Redirect DRDY on pin 1 (1) or pin 2 (2). |
Denis Ciocca | 23491b5 | 2013-01-25 23:44:00 +0000 | [diff] [blame] | 213 | * @get_irq_data_ready: Function to get the IRQ used for data ready signal. |
| 214 | * @tf: Transfer function structure used by I/O operations. |
| 215 | * @tb: Transfer buffers and mutex used by I/O operations. |
| 216 | */ |
| 217 | struct st_sensor_data { |
| 218 | struct device *dev; |
| 219 | struct iio_trigger *trig; |
| 220 | struct st_sensors *sensor; |
| 221 | struct st_sensor_fullscale_avl *current_fullscale; |
Lee Jones | 7744876 | 2013-09-16 17:02:00 +0100 | [diff] [blame] | 222 | struct regulator *vdd; |
Lee Jones | 71e1980 | 2013-09-16 17:02:00 +0100 | [diff] [blame] | 223 | struct regulator *vdd_io; |
Denis Ciocca | 23491b5 | 2013-01-25 23:44:00 +0000 | [diff] [blame] | 224 | |
| 225 | bool enabled; |
| 226 | bool multiread_bit; |
| 227 | |
| 228 | char *buffer_data; |
| 229 | |
| 230 | unsigned int odr; |
Denis CIOCCA | 607a568 | 2013-06-03 15:58:00 +0100 | [diff] [blame] | 231 | unsigned int num_data_channels; |
Denis Ciocca | 23491b5 | 2013-01-25 23:44:00 +0000 | [diff] [blame] | 232 | |
Denis CIOCCA | 23cde4d | 2013-06-19 09:28:00 +0100 | [diff] [blame] | 233 | u8 drdy_int_pin; |
| 234 | |
Denis Ciocca | 23491b5 | 2013-01-25 23:44:00 +0000 | [diff] [blame] | 235 | unsigned int (*get_irq_data_ready) (struct iio_dev *indio_dev); |
| 236 | |
| 237 | const struct st_sensor_transfer_function *tf; |
| 238 | struct st_sensor_transfer_buffer tb; |
| 239 | }; |
| 240 | |
| 241 | #ifdef CONFIG_IIO_BUFFER |
Guenter Roeck | 5a4d729 | 2013-03-01 15:57:00 +0000 | [diff] [blame] | 242 | irqreturn_t st_sensors_trigger_handler(int irq, void *p); |
| 243 | |
| 244 | int st_sensors_get_buffer_element(struct iio_dev *indio_dev, u8 *buf); |
| 245 | #endif |
| 246 | |
| 247 | #ifdef CONFIG_IIO_TRIGGER |
Denis Ciocca | 23491b5 | 2013-01-25 23:44:00 +0000 | [diff] [blame] | 248 | int st_sensors_allocate_trigger(struct iio_dev *indio_dev, |
| 249 | const struct iio_trigger_ops *trigger_ops); |
| 250 | |
| 251 | void st_sensors_deallocate_trigger(struct iio_dev *indio_dev); |
| 252 | |
Denis CIOCCA | 91ffbab | 2013-02-07 09:46:00 +0000 | [diff] [blame] | 253 | #else |
| 254 | static inline int st_sensors_allocate_trigger(struct iio_dev *indio_dev, |
| 255 | const struct iio_trigger_ops *trigger_ops) |
| 256 | { |
| 257 | return 0; |
| 258 | } |
| 259 | static inline void st_sensors_deallocate_trigger(struct iio_dev *indio_dev) |
| 260 | { |
| 261 | return; |
| 262 | } |
Denis Ciocca | 23491b5 | 2013-01-25 23:44:00 +0000 | [diff] [blame] | 263 | #endif |
| 264 | |
Denis CIOCCA | 23cde4d | 2013-06-19 09:28:00 +0100 | [diff] [blame] | 265 | int st_sensors_init_sensor(struct iio_dev *indio_dev, |
| 266 | struct st_sensors_platform_data *pdata); |
Denis Ciocca | 23491b5 | 2013-01-25 23:44:00 +0000 | [diff] [blame] | 267 | |
| 268 | int st_sensors_set_enable(struct iio_dev *indio_dev, bool enable); |
| 269 | |
| 270 | int st_sensors_set_axis_enable(struct iio_dev *indio_dev, u8 axis_enable); |
| 271 | |
Denis Ciocca | 23491b5 | 2013-01-25 23:44:00 +0000 | [diff] [blame] | 272 | int st_sensors_set_odr(struct iio_dev *indio_dev, unsigned int odr); |
| 273 | |
| 274 | int st_sensors_set_dataready_irq(struct iio_dev *indio_dev, bool enable); |
| 275 | |
| 276 | int st_sensors_set_fullscale_by_gain(struct iio_dev *indio_dev, int scale); |
| 277 | |
| 278 | int st_sensors_read_info_raw(struct iio_dev *indio_dev, |
| 279 | struct iio_chan_spec const *ch, int *val); |
| 280 | |
| 281 | int st_sensors_check_device_support(struct iio_dev *indio_dev, |
| 282 | int num_sensors_list, const struct st_sensors *sensors); |
| 283 | |
| 284 | ssize_t st_sensors_sysfs_get_sampling_frequency(struct device *dev, |
| 285 | struct device_attribute *attr, char *buf); |
| 286 | |
| 287 | ssize_t st_sensors_sysfs_set_sampling_frequency(struct device *dev, |
| 288 | struct device_attribute *attr, const char *buf, size_t size); |
| 289 | |
| 290 | ssize_t st_sensors_sysfs_sampling_frequency_avail(struct device *dev, |
| 291 | struct device_attribute *attr, char *buf); |
| 292 | |
| 293 | ssize_t st_sensors_sysfs_scale_avail(struct device *dev, |
| 294 | struct device_attribute *attr, char *buf); |
| 295 | |
| 296 | #endif /* ST_SENSORS_H */ |