1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * STMicroelectronics sensors i2c library driver 4 * 5 * Copyright 2012-2013 STMicroelectronics Inc. 6 * 7 * Denis Ciocca <denis.ciocca@st.com> 8 */ 9 10 #include <linux/kernel.h> 11 #include <linux/module.h> 12 #include <linux/slab.h> 13 #include <linux/iio/iio.h> 14 #include <linux/of_device.h> 15 #include <linux/acpi.h> 16 17 #include <linux/iio/common/st_sensors_i2c.h> 18 19 20 #define ST_SENSORS_I2C_MULTIREAD 0x80 21 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 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 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 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 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_ACPI 82 int st_sensors_match_acpi_device(struct device *dev) 83 { 84 const struct acpi_device_id *acpi_id; 85 kernel_ulong_t driver_data = 0; 86 87 if (ACPI_HANDLE(dev)) { 88 acpi_id = acpi_match_device(dev->driver->acpi_match_table, dev); 89 if (!acpi_id) { 90 dev_err(dev, "No driver data\n"); 91 return -EINVAL; 92 } 93 driver_data = acpi_id->driver_data; 94 } 95 return driver_data; 96 } 97 EXPORT_SYMBOL(st_sensors_match_acpi_device); 98 #endif 99 100 MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>"); 101 MODULE_DESCRIPTION("STMicroelectronics ST-sensors i2c driver"); 102 MODULE_LICENSE("GPL v2"); 103