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>
15 #include <linux/of_device.h>
17 #include <linux/iio/common/st_sensors_i2c.h>
20 #define ST_SENSORS_I2C_MULTIREAD 0x80
22 static unsigned int st_sensors_i2c_get_irq(struct iio_dev
*indio_dev
)
24 struct st_sensor_data
*sdata
= iio_priv(indio_dev
);
26 return to_i2c_client(sdata
->dev
)->irq
;
29 static int st_sensors_i2c_read_byte(struct st_sensor_transfer_buffer
*tb
,
30 struct device
*dev
, u8 reg_addr
, u8
*res_byte
)
34 err
= i2c_smbus_read_byte_data(to_i2c_client(dev
), reg_addr
);
36 goto st_accel_i2c_read_byte_error
;
38 *res_byte
= err
& 0xff;
40 st_accel_i2c_read_byte_error
:
41 return err
< 0 ? err
: 0;
44 static int st_sensors_i2c_read_multiple_byte(
45 struct st_sensor_transfer_buffer
*tb
, struct device
*dev
,
46 u8 reg_addr
, int len
, u8
*data
, bool multiread_bit
)
49 reg_addr
|= ST_SENSORS_I2C_MULTIREAD
;
51 return i2c_smbus_read_i2c_block_data_or_emulated(to_i2c_client(dev
),
55 static int st_sensors_i2c_write_byte(struct st_sensor_transfer_buffer
*tb
,
56 struct device
*dev
, u8 reg_addr
, u8 data
)
58 return i2c_smbus_write_byte_data(to_i2c_client(dev
), reg_addr
, data
);
61 static const struct st_sensor_transfer_function st_sensors_tf_i2c
= {
62 .read_byte
= st_sensors_i2c_read_byte
,
63 .write_byte
= st_sensors_i2c_write_byte
,
64 .read_multiple_byte
= st_sensors_i2c_read_multiple_byte
,
67 void st_sensors_i2c_configure(struct iio_dev
*indio_dev
,
68 struct i2c_client
*client
, struct st_sensor_data
*sdata
)
70 i2c_set_clientdata(client
, indio_dev
);
72 indio_dev
->dev
.parent
= &client
->dev
;
73 indio_dev
->name
= client
->name
;
75 sdata
->dev
= &client
->dev
;
76 sdata
->tf
= &st_sensors_tf_i2c
;
77 sdata
->get_irq_data_ready
= st_sensors_i2c_get_irq
;
79 EXPORT_SYMBOL(st_sensors_i2c_configure
);
83 * st_sensors_of_i2c_probe() - device tree probe for ST I2C sensors
84 * @client: the I2C client device for the sensor
85 * @match: the OF match table for the device, containing compatible strings
86 * but also a .data field with the corresponding internal kernel name
87 * used by this sensor.
89 * In effect this function matches a compatible string to an internal kernel
90 * name for a certain sensor device, so that the rest of the autodetection can
91 * rely on that name from this point on. I2C client devices will be renamed
92 * to match the internal kernel convention.
94 void st_sensors_of_i2c_probe(struct i2c_client
*client
,
95 const struct of_device_id
*match
)
97 const struct of_device_id
*of_id
;
99 of_id
= of_match_device(match
, &client
->dev
);
103 /* The name from the OF match takes precedence if present */
104 strncpy(client
->name
, of_id
->data
, sizeof(client
->name
));
105 client
->name
[sizeof(client
->name
) - 1] = '\0';
107 EXPORT_SYMBOL(st_sensors_of_i2c_probe
);
110 MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
111 MODULE_DESCRIPTION("STMicroelectronics ST-sensors i2c driver");
112 MODULE_LICENSE("GPL v2");