1 /*
2 * STMicroelectronics sensors i2c 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 #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>
16
17 #include <linux/iio/common/st_sensors_i2c.h>
18
19
20 #define ST_SENSORS_I2C_MULTIREAD 0x80
21
st_sensors_i2c_get_irq(struct iio_dev * indio_dev)22 static unsigned int st_sensors_i2c_get_irq(struct iio_dev *indio_dev)
23 {
24 struct st_sensor_data *sdata = iio_priv(indio_dev);
25
26 return to_i2c_client(sdata->dev)->irq;
27 }
28
st_sensors_i2c_read_byte(struct st_sensor_transfer_buffer * tb,struct device * dev,u8 reg_addr,u8 * res_byte)29 static int st_sensors_i2c_read_byte(struct st_sensor_transfer_buffer *tb,
30 struct device *dev, u8 reg_addr, u8 *res_byte)
31 {
32 int err;
33
34 err = i2c_smbus_read_byte_data(to_i2c_client(dev), reg_addr);
35 if (err < 0)
36 goto st_accel_i2c_read_byte_error;
37
38 *res_byte = err & 0xff;
39
40 st_accel_i2c_read_byte_error:
41 return err < 0 ? err : 0;
42 }
43
st_sensors_i2c_read_multiple_byte(struct st_sensor_transfer_buffer * tb,struct device * dev,u8 reg_addr,int len,u8 * data,bool multiread_bit)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)
47 {
48 if (multiread_bit)
49 reg_addr |= ST_SENSORS_I2C_MULTIREAD;
50
51 return i2c_smbus_read_i2c_block_data_or_emulated(to_i2c_client(dev),
52 reg_addr, len, data);
53 }
54
st_sensors_i2c_write_byte(struct st_sensor_transfer_buffer * tb,struct device * dev,u8 reg_addr,u8 data)55 static int st_sensors_i2c_write_byte(struct st_sensor_transfer_buffer *tb,
56 struct device *dev, u8 reg_addr, u8 data)
57 {
58 return i2c_smbus_write_byte_data(to_i2c_client(dev), reg_addr, data);
59 }
60
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,
65 };
66
st_sensors_i2c_configure(struct iio_dev * indio_dev,struct i2c_client * client,struct st_sensor_data * sdata)67 void st_sensors_i2c_configure(struct iio_dev *indio_dev,
68 struct i2c_client *client, struct st_sensor_data *sdata)
69 {
70 i2c_set_clientdata(client, indio_dev);
71
72 indio_dev->dev.parent = &client->dev;
73 indio_dev->name = client->name;
74
75 sdata->dev = &client->dev;
76 sdata->tf = &st_sensors_tf_i2c;
77 sdata->get_irq_data_ready = st_sensors_i2c_get_irq;
78 }
79 EXPORT_SYMBOL(st_sensors_i2c_configure);
80
81 #ifdef CONFIG_OF
82 /**
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.
88 *
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.
93 */
st_sensors_of_i2c_probe(struct i2c_client * client,const struct of_device_id * match)94 void st_sensors_of_i2c_probe(struct i2c_client *client,
95 const struct of_device_id *match)
96 {
97 const struct of_device_id *of_id;
98
99 of_id = of_match_device(match, &client->dev);
100 if (!of_id)
101 return;
102
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';
106 }
107 EXPORT_SYMBOL(st_sensors_of_i2c_probe);
108 #endif
109
110 MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
111 MODULE_DESCRIPTION("STMicroelectronics ST-sensors i2c driver");
112 MODULE_LICENSE("GPL v2");
113