2 * STMicroelectronics sensors i2c library driver
4 * Copyright 2012-2013 STMicroelectronics Inc.
6 * Denis Ciocca <denis.ciocca@st.com>
8 * Licensed under the GPL-2.
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/slab.h>
14 #include <linux/iio/iio.h>
16 #include <linux/iio/common/st_sensors_i2c.h>
19 #define ST_SENSORS_I2C_MULTIREAD 0x80
21 static unsigned int st_sensors_i2c_get_irq(struct iio_dev
*indio_dev
)
23 struct st_sensor_data
*sdata
= iio_priv(indio_dev
);
25 return to_i2c_client(sdata
->dev
)->irq
;
28 static int st_sensors_i2c_read_byte(struct st_sensor_transfer_buffer
*tb
,
29 struct device
*dev
, u8 reg_addr
, u8
*res_byte
)
33 err
= i2c_smbus_read_byte_data(to_i2c_client(dev
), reg_addr
);
35 goto st_accel_i2c_read_byte_error
;
37 *res_byte
= err
& 0xff;
39 st_accel_i2c_read_byte_error
:
40 return err
< 0 ? err
: 0;
43 static int st_sensors_i2c_read_multiple_byte(
44 struct st_sensor_transfer_buffer
*tb
, struct device
*dev
,
45 u8 reg_addr
, int len
, u8
*data
, bool multiread_bit
)
48 reg_addr
|= ST_SENSORS_I2C_MULTIREAD
;
50 return i2c_smbus_read_i2c_block_data(to_i2c_client(dev
),
54 static int st_sensors_i2c_write_byte(struct st_sensor_transfer_buffer
*tb
,
55 struct device
*dev
, u8 reg_addr
, u8 data
)
57 return i2c_smbus_write_byte_data(to_i2c_client(dev
), reg_addr
, data
);
60 static const struct st_sensor_transfer_function st_sensors_tf_i2c
= {
61 .read_byte
= st_sensors_i2c_read_byte
,
62 .write_byte
= st_sensors_i2c_write_byte
,
63 .read_multiple_byte
= st_sensors_i2c_read_multiple_byte
,
66 void st_sensors_i2c_configure(struct iio_dev
*indio_dev
,
67 struct i2c_client
*client
, struct st_sensor_data
*sdata
)
69 i2c_set_clientdata(client
, indio_dev
);
71 indio_dev
->dev
.parent
= &client
->dev
;
72 indio_dev
->name
= client
->name
;
74 sdata
->tf
= &st_sensors_tf_i2c
;
75 sdata
->get_irq_data_ready
= st_sensors_i2c_get_irq
;
77 EXPORT_SYMBOL(st_sensors_i2c_configure
);
79 MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
80 MODULE_DESCRIPTION("STMicroelectronics ST-sensors i2c driver");
81 MODULE_LICENSE("GPL v2");