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>
16 #include <linux/acpi.h>
18 #include <linux/iio/common/st_sensors_i2c.h>
21 #define ST_SENSORS_I2C_MULTIREAD 0x80
23 static unsigned int st_sensors_i2c_get_irq(struct iio_dev
*indio_dev
)
25 struct st_sensor_data
*sdata
= iio_priv(indio_dev
);
27 return to_i2c_client(sdata
->dev
)->irq
;
30 static int st_sensors_i2c_read_byte(struct st_sensor_transfer_buffer
*tb
,
31 struct device
*dev
, u8 reg_addr
, u8
*res_byte
)
35 err
= i2c_smbus_read_byte_data(to_i2c_client(dev
), reg_addr
);
37 goto st_accel_i2c_read_byte_error
;
39 *res_byte
= err
& 0xff;
41 st_accel_i2c_read_byte_error
:
42 return err
< 0 ? err
: 0;
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
)
50 reg_addr
|= ST_SENSORS_I2C_MULTIREAD
;
52 return i2c_smbus_read_i2c_block_data_or_emulated(to_i2c_client(dev
),
56 static int st_sensors_i2c_write_byte(struct st_sensor_transfer_buffer
*tb
,
57 struct device
*dev
, u8 reg_addr
, u8 data
)
59 return i2c_smbus_write_byte_data(to_i2c_client(dev
), reg_addr
, data
);
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
,
68 void st_sensors_i2c_configure(struct iio_dev
*indio_dev
,
69 struct i2c_client
*client
, struct st_sensor_data
*sdata
)
71 i2c_set_clientdata(client
, indio_dev
);
73 indio_dev
->dev
.parent
= &client
->dev
;
74 indio_dev
->name
= client
->name
;
76 sdata
->dev
= &client
->dev
;
77 sdata
->tf
= &st_sensors_tf_i2c
;
78 sdata
->get_irq_data_ready
= st_sensors_i2c_get_irq
;
80 EXPORT_SYMBOL(st_sensors_i2c_configure
);
83 int st_sensors_match_acpi_device(struct device
*dev
)
85 const struct acpi_device_id
*acpi_id
;
86 kernel_ulong_t driver_data
= 0;
88 if (ACPI_HANDLE(dev
)) {
89 acpi_id
= acpi_match_device(dev
->driver
->acpi_match_table
, dev
);
91 dev_err(dev
, "No driver data\n");
94 driver_data
= acpi_id
->driver_data
;
98 EXPORT_SYMBOL(st_sensors_match_acpi_device
);
101 MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
102 MODULE_DESCRIPTION("STMicroelectronics ST-sensors i2c driver");
103 MODULE_LICENSE("GPL v2");