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 #include <linux/acpi.h>
17 
18 #include <linux/iio/common/st_sensors_i2c.h>
19 
20 
21 #define ST_SENSORS_I2C_MULTIREAD	0x80
22 
st_sensors_i2c_get_irq(struct iio_dev * indio_dev)23 static unsigned int st_sensors_i2c_get_irq(struct iio_dev *indio_dev)
24 {
25 	struct st_sensor_data *sdata = iio_priv(indio_dev);
26 
27 	return to_i2c_client(sdata->dev)->irq;
28 }
29 
st_sensors_i2c_read_byte(struct st_sensor_transfer_buffer * tb,struct device * dev,u8 reg_addr,u8 * res_byte)30 static int st_sensors_i2c_read_byte(struct st_sensor_transfer_buffer *tb,
31 				struct device *dev, u8 reg_addr, u8 *res_byte)
32 {
33 	int err;
34 
35 	err = i2c_smbus_read_byte_data(to_i2c_client(dev), reg_addr);
36 	if (err < 0)
37 		goto st_accel_i2c_read_byte_error;
38 
39 	*res_byte = err & 0xff;
40 
41 st_accel_i2c_read_byte_error:
42 	return err < 0 ? err : 0;
43 }
44 
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)45 static int st_sensors_i2c_read_multiple_byte(
46 		struct st_sensor_transfer_buffer *tb, struct device *dev,
47 			u8 reg_addr, int len, u8 *data, bool multiread_bit)
48 {
49 	if (multiread_bit)
50 		reg_addr |= ST_SENSORS_I2C_MULTIREAD;
51 
52 	return i2c_smbus_read_i2c_block_data_or_emulated(to_i2c_client(dev),
53 							 reg_addr, len, data);
54 }
55 
st_sensors_i2c_write_byte(struct st_sensor_transfer_buffer * tb,struct device * dev,u8 reg_addr,u8 data)56 static int st_sensors_i2c_write_byte(struct st_sensor_transfer_buffer *tb,
57 				struct device *dev, u8 reg_addr, u8 data)
58 {
59 	return i2c_smbus_write_byte_data(to_i2c_client(dev), reg_addr, data);
60 }
61 
62 static const struct st_sensor_transfer_function st_sensors_tf_i2c = {
63 	.read_byte = st_sensors_i2c_read_byte,
64 	.write_byte = st_sensors_i2c_write_byte,
65 	.read_multiple_byte = st_sensors_i2c_read_multiple_byte,
66 };
67 
st_sensors_i2c_configure(struct iio_dev * indio_dev,struct i2c_client * client,struct st_sensor_data * sdata)68 void st_sensors_i2c_configure(struct iio_dev *indio_dev,
69 		struct i2c_client *client, struct st_sensor_data *sdata)
70 {
71 	i2c_set_clientdata(client, indio_dev);
72 
73 	indio_dev->dev.parent = &client->dev;
74 	indio_dev->name = client->name;
75 
76 	sdata->dev = &client->dev;
77 	sdata->tf = &st_sensors_tf_i2c;
78 	sdata->get_irq_data_ready = st_sensors_i2c_get_irq;
79 }
80 EXPORT_SYMBOL(st_sensors_i2c_configure);
81 
82 #ifdef CONFIG_ACPI
st_sensors_match_acpi_device(struct device * dev)83 int st_sensors_match_acpi_device(struct device *dev)
84 {
85 	const struct acpi_device_id *acpi_id;
86 	kernel_ulong_t driver_data = 0;
87 
88 	if (ACPI_HANDLE(dev)) {
89 		acpi_id = acpi_match_device(dev->driver->acpi_match_table, dev);
90 		if (!acpi_id) {
91 			dev_err(dev, "No driver data\n");
92 			return -EINVAL;
93 		}
94 		driver_data = acpi_id->driver_data;
95 	}
96 	return driver_data;
97 }
98 EXPORT_SYMBOL(st_sensors_match_acpi_device);
99 #endif
100 
101 MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
102 MODULE_DESCRIPTION("STMicroelectronics ST-sensors i2c driver");
103 MODULE_LICENSE("GPL v2");
104