st_sensors.h 9.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * STMicroelectronics sensors library driver
 *
 * Copyright 2012-2013 STMicroelectronics Inc.
 *
 * Denis Ciocca <denis.ciocca@st.com>
 *
 * Licensed under the GPL-2.
 */

#ifndef ST_SENSORS_H
#define ST_SENSORS_H

#include <linux/i2c.h>
#include <linux/spi/spi.h>
#include <linux/irqreturn.h>
#include <linux/iio/trigger.h>
18
#include <linux/bitops.h>
19
#include <linux/regulator/consumer.h>
20

21 22
#include <linux/platform_data/st_sensors_pdata.h>

23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
#define ST_SENSORS_TX_MAX_LENGTH		2
#define ST_SENSORS_RX_MAX_LENGTH		6

#define ST_SENSORS_ODR_LIST_MAX			10
#define ST_SENSORS_FULLSCALE_AVL_MAX		10

#define ST_SENSORS_NUMBER_ALL_CHANNELS		4
#define ST_SENSORS_ENABLE_ALL_AXIS		0x07
#define ST_SENSORS_SCAN_X			0
#define ST_SENSORS_SCAN_Y			1
#define ST_SENSORS_SCAN_Z			2
#define ST_SENSORS_DEFAULT_POWER_ON_VALUE	0x01
#define ST_SENSORS_DEFAULT_POWER_OFF_VALUE	0x00
#define ST_SENSORS_DEFAULT_WAI_ADDRESS		0x0f
#define ST_SENSORS_DEFAULT_AXIS_ADDR		0x20
#define ST_SENSORS_DEFAULT_AXIS_MASK		0x07
#define ST_SENSORS_DEFAULT_AXIS_N_BIT		3
40
#define ST_SENSORS_DEFAULT_STAT_ADDR		0x27
41 42 43 44

#define ST_SENSORS_MAX_NAME			17
#define ST_SENSORS_MAX_4WAI			7

45 46
#define ST_SENSORS_LSM_CHANNELS(device_type, mask, index, mod, \
					ch2, s, endian, rbits, sbits, addr) \
47 48
{ \
	.type = device_type, \
49 50
	.modified = mod, \
	.info_mask_separate = mask, \
51
	.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
52
	.scan_index = index, \
53
	.channel2 = ch2, \
54 55
	.address = addr, \
	.scan_type = { \
56 57 58 59
		.sign = s, \
		.realbits = rbits, \
		.shift = sbits - rbits, \
		.storagebits = sbits, \
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
		.endianness = endian, \
	}, \
}

#define ST_SENSORS_DEV_ATTR_SAMP_FREQ_AVAIL() \
		IIO_DEV_ATTR_SAMP_FREQ_AVAIL( \
			st_sensors_sysfs_sampling_frequency_avail)

#define ST_SENSORS_DEV_ATTR_SCALE_AVAIL(name) \
		IIO_DEVICE_ATTR(name, S_IRUGO, \
			st_sensors_sysfs_scale_avail, NULL , 0);

struct st_sensor_odr_avl {
	unsigned int hz;
	u8 value;
};

struct st_sensor_odr {
	u8 addr;
	u8 mask;
	struct st_sensor_odr_avl odr_avl[ST_SENSORS_ODR_LIST_MAX];
};

struct st_sensor_power {
	u8 addr;
	u8 mask;
	u8 value_off;
	u8 value_on;
};

struct st_sensor_axis {
	u8 addr;
	u8 mask;
};

struct st_sensor_fullscale_avl {
	unsigned int num;
	u8 value;
	unsigned int gain;
	unsigned int gain2;
};

struct st_sensor_fullscale {
	u8 addr;
	u8 mask;
	struct st_sensor_fullscale_avl fs_avl[ST_SENSORS_FULLSCALE_AVL_MAX];
};

108 109 110 111 112
struct st_sensor_sim {
	u8 addr;
	u8 value;
};

113 114 115 116 117 118 119 120 121 122
/**
 * struct st_sensor_bdu - ST sensor device block data update
 * @addr: address of the register.
 * @mask: mask to write the block data update flag.
 */
struct st_sensor_bdu {
	u8 addr;
	u8 mask;
};

123 124 125 126 127 128 129 130 131 132
/**
 * struct st_sensor_das - ST sensor device data alignment selection
 * @addr: address of the register.
 * @mask: mask to write the das flag for left alignment.
 */
struct st_sensor_das {
	u8 addr;
	u8 mask;
};

133 134 135
/**
 * struct st_sensor_data_ready_irq - ST sensor device data-ready interrupt
 * @addr: address of the register.
136 137
 * @mask_int1: mask to enable/disable IRQ on INT1 pin.
 * @mask_int2: mask to enable/disable IRQ on INT2 pin.
138 139
 * @addr_ihl: address to enable/disable active low on the INT lines.
 * @mask_ihl: mask to enable/disable active low on the INT lines.
140 141
 * @addr_od: address to enable/disable Open Drain on the INT lines.
 * @mask_od: mask to enable/disable Open Drain on the INT lines.
142
 * @addr_stat_drdy: address to read status of DRDY (data ready) interrupt
143 144 145 146 147 148
 * struct ig1 - represents the Interrupt Generator 1 of sensors.
 * @en_addr: address of the enable ig1 register.
 * @en_mask: mask to write the on/off value for enable.
 */
struct st_sensor_data_ready_irq {
	u8 addr;
149 150
	u8 mask_int1;
	u8 mask_int2;
151 152
	u8 addr_ihl;
	u8 mask_ihl;
153 154
	u8 addr_od;
	u8 mask_od;
155
	u8 addr_stat_drdy;
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
	struct {
		u8 en_addr;
		u8 en_mask;
	} ig1;
};

/**
 * struct st_sensor_transfer_buffer - ST sensor device I/O buffer
 * @buf_lock: Mutex to protect rx and tx buffers.
 * @tx_buf: Buffer used by SPI transfer function to send data to the sensors.
 *	This buffer is used to avoid DMA not-aligned issue.
 * @rx_buf: Buffer used by SPI transfer to receive data from sensors.
 *	This buffer is used to avoid DMA not-aligned issue.
 */
struct st_sensor_transfer_buffer {
	struct mutex buf_lock;
	u8 rx_buf[ST_SENSORS_RX_MAX_LENGTH];
	u8 tx_buf[ST_SENSORS_TX_MAX_LENGTH] ____cacheline_aligned;
};

/**
 * struct st_sensor_transfer_function - ST sensor device I/O function
 * @read_byte: Function used to read one byte.
 * @write_byte: Function used to write one byte.
 * @read_multiple_byte: Function used to read multiple byte.
 */
struct st_sensor_transfer_function {
	int (*read_byte) (struct st_sensor_transfer_buffer *tb,
				struct device *dev, u8 reg_addr, u8 *res_byte);
	int (*write_byte) (struct st_sensor_transfer_buffer *tb,
				struct device *dev, u8 reg_addr, u8 data);
	int (*read_multiple_byte) (struct st_sensor_transfer_buffer *tb,
		struct device *dev, u8 reg_addr, int len, u8 *data,
							bool multiread_bit);
};

/**
193
 * struct st_sensor_settings - ST specific sensor settings
194
 * @wai: Contents of WhoAmI register.
195
 * @wai_addr: The address of WhoAmI register.
196 197 198 199 200 201 202
 * @sensors_supported: List of supported sensors by struct itself.
 * @ch: IIO channels for the sensor.
 * @odr: Output data rate register and ODR list available.
 * @pw: Power register of the sensor.
 * @enable_axis: Enable one or more axis of the sensor.
 * @fs: Full scale register and full scale list available.
 * @bdu: Block data update register.
203
 * @das: Data Alignment Selection register.
204
 * @drdy_irq: Data ready register of the sensor.
205
 * @sim: SPI serial interface mode register of the sensor.
206 207 208
 * @multi_read_bit: Use or not particular bit for [I2C/SPI] multi-read.
 * @bootime: samples to discard when sensor passing from power-down to power-up.
 */
209
struct st_sensor_settings {
210
	u8 wai;
211
	u8 wai_addr;
212 213
	char sensors_supported[ST_SENSORS_MAX_4WAI][ST_SENSORS_MAX_NAME];
	struct iio_chan_spec *ch;
214
	int num_ch;
215 216 217 218 219
	struct st_sensor_odr odr;
	struct st_sensor_power pw;
	struct st_sensor_axis enable_axis;
	struct st_sensor_fullscale fs;
	struct st_sensor_bdu bdu;
220
	struct st_sensor_das das;
221
	struct st_sensor_data_ready_irq drdy_irq;
222
	struct st_sensor_sim sim;
223 224 225 226 227 228 229 230
	bool multi_read_bit;
	unsigned int bootime;
};

/**
 * struct st_sensor_data - ST sensor device status
 * @dev: Pointer to instance of struct device (I2C or SPI).
 * @trig: The trigger in use by the core driver.
231
 * @sensor_settings: Pointer to the specific sensor settings in use.
232
 * @current_fullscale: Maximum range of measure by the sensor.
233
 * @vdd: Pointer to sensor's Vdd power supply
234
 * @vdd_io: Pointer to sensor's Vdd-IO power supply
235 236 237 238
 * @enabled: Status of the sensor (false->off, true->on).
 * @multiread_bit: Use or not particular bit for [I2C/SPI] multiread.
 * @buffer_data: Data used by buffer part.
 * @odr: Output data rate of the sensor [Hz].
239
 * num_data_channels: Number of data channels used in buffer.
240
 * @drdy_int_pin: Redirect DRDY on pin 1 (1) or pin 2 (2).
241
 * @int_pin_open_drain: Set the interrupt/DRDY to open drain.
242 243 244
 * @get_irq_data_ready: Function to get the IRQ used for data ready signal.
 * @tf: Transfer function structure used by I/O operations.
 * @tb: Transfer buffers and mutex used by I/O operations.
245
 * @edge_irq: the IRQ triggers on edges and need special handling.
246 247
 * @hw_irq_trigger: if we're using the hardware interrupt on the sensor.
 * @hw_timestamp: Latest timestamp from the interrupt handler, when in use.
248 249 250 251
 */
struct st_sensor_data {
	struct device *dev;
	struct iio_trigger *trig;
252
	struct st_sensor_settings *sensor_settings;
253
	struct st_sensor_fullscale_avl *current_fullscale;
254
	struct regulator *vdd;
255
	struct regulator *vdd_io;
256 257 258 259 260 261 262

	bool enabled;
	bool multiread_bit;

	char *buffer_data;

	unsigned int odr;
263
	unsigned int num_data_channels;
264

265
	u8 drdy_int_pin;
266
	bool int_pin_open_drain;
267

268 269 270 271
	unsigned int (*get_irq_data_ready) (struct iio_dev *indio_dev);

	const struct st_sensor_transfer_function *tf;
	struct st_sensor_transfer_buffer tb;
272

273
	bool edge_irq;
274 275
	bool hw_irq_trigger;
	s64 hw_timestamp;
276 277 278
};

#ifdef CONFIG_IIO_BUFFER
279 280 281 282
irqreturn_t st_sensors_trigger_handler(int irq, void *p);
#endif

#ifdef CONFIG_IIO_TRIGGER
283 284 285 286
int st_sensors_allocate_trigger(struct iio_dev *indio_dev,
				const struct iio_trigger_ops *trigger_ops);

void st_sensors_deallocate_trigger(struct iio_dev *indio_dev);
287 288
int st_sensors_validate_device(struct iio_trigger *trig,
			       struct iio_dev *indio_dev);
289 290 291 292 293 294 295 296 297 298
#else
static inline int st_sensors_allocate_trigger(struct iio_dev *indio_dev,
				const struct iio_trigger_ops *trigger_ops)
{
	return 0;
}
static inline void st_sensors_deallocate_trigger(struct iio_dev *indio_dev)
{
	return;
}
299
#define st_sensors_validate_device NULL
300 301
#endif

302 303
int st_sensors_init_sensor(struct iio_dev *indio_dev,
					struct st_sensors_platform_data *pdata);
304 305 306 307 308

int st_sensors_set_enable(struct iio_dev *indio_dev, bool enable);

int st_sensors_set_axis_enable(struct iio_dev *indio_dev, u8 axis_enable);

309
int st_sensors_power_enable(struct iio_dev *indio_dev);
310 311 312

void st_sensors_power_disable(struct iio_dev *indio_dev);

313 314 315 316
int st_sensors_debugfs_reg_access(struct iio_dev *indio_dev,
				  unsigned reg, unsigned writeval,
				  unsigned *readval);

317 318 319 320 321 322 323 324 325 326
int st_sensors_set_odr(struct iio_dev *indio_dev, unsigned int odr);

int st_sensors_set_dataready_irq(struct iio_dev *indio_dev, bool enable);

int st_sensors_set_fullscale_by_gain(struct iio_dev *indio_dev, int scale);

int st_sensors_read_info_raw(struct iio_dev *indio_dev,
				struct iio_chan_spec const *ch, int *val);

int st_sensors_check_device_support(struct iio_dev *indio_dev,
327
	int num_sensors_list, const struct st_sensor_settings *sensor_settings);
328 329 330 331 332 333 334 335

ssize_t st_sensors_sysfs_sampling_frequency_avail(struct device *dev,
				struct device_attribute *attr, char *buf);

ssize_t st_sensors_sysfs_scale_avail(struct device *dev,
				struct device_attribute *attr, char *buf);

#endif /* ST_SENSORS_H */