1 /* 2 * STMicroelectronics st_lsm6dsx i2c driver 3 * 4 * Copyright 2016 STMicroelectronics Inc. 5 * 6 * Lorenzo Bianconi <lorenzo.bianconi@st.com> 7 * Denis Ciocca <denis.ciocca@st.com> 8 * 9 * Licensed under the GPL-2. 10 */ 11 12 #include <linux/kernel.h> 13 #include <linux/module.h> 14 #include <linux/i2c.h> 15 #include <linux/slab.h> 16 #include <linux/of.h> 17 18 #include "st_lsm6dsx.h" 19 20 static int st_lsm6dsx_i2c_read(struct device *dev, u8 addr, int len, u8 *data) 21 { 22 struct i2c_client *client = to_i2c_client(dev); 23 struct i2c_msg msg[2]; 24 25 msg[0].addr = client->addr; 26 msg[0].flags = client->flags; 27 msg[0].len = 1; 28 msg[0].buf = &addr; 29 30 msg[1].addr = client->addr; 31 msg[1].flags = client->flags | I2C_M_RD; 32 msg[1].len = len; 33 msg[1].buf = data; 34 35 return i2c_transfer(client->adapter, msg, 2); 36 } 37 38 static int st_lsm6dsx_i2c_write(struct device *dev, u8 addr, int len, u8 *data) 39 { 40 struct i2c_client *client = to_i2c_client(dev); 41 struct i2c_msg msg; 42 u8 send[len + 1]; 43 44 send[0] = addr; 45 memcpy(&send[1], data, len * sizeof(u8)); 46 47 msg.addr = client->addr; 48 msg.flags = client->flags; 49 msg.len = len + 1; 50 msg.buf = send; 51 52 return i2c_transfer(client->adapter, &msg, 1); 53 } 54 55 static const struct st_lsm6dsx_transfer_function st_lsm6dsx_transfer_fn = { 56 .read = st_lsm6dsx_i2c_read, 57 .write = st_lsm6dsx_i2c_write, 58 }; 59 60 static int st_lsm6dsx_i2c_probe(struct i2c_client *client, 61 const struct i2c_device_id *id) 62 { 63 return st_lsm6dsx_probe(&client->dev, client->irq, 64 (int)id->driver_data, id->name, 65 &st_lsm6dsx_transfer_fn); 66 } 67 68 static const struct of_device_id st_lsm6dsx_i2c_of_match[] = { 69 { 70 .compatible = "st,lsm6ds3", 71 .data = (void *)ST_LSM6DS3_ID, 72 }, 73 { 74 .compatible = "st,lsm6ds3h", 75 .data = (void *)ST_LSM6DS3H_ID, 76 }, 77 { 78 .compatible = "st,lsm6dsl", 79 .data = (void *)ST_LSM6DSL_ID, 80 }, 81 { 82 .compatible = "st,lsm6dsm", 83 .data = (void *)ST_LSM6DSM_ID, 84 }, 85 {}, 86 }; 87 MODULE_DEVICE_TABLE(of, st_lsm6dsx_i2c_of_match); 88 89 static const struct i2c_device_id st_lsm6dsx_i2c_id_table[] = { 90 { ST_LSM6DS3_DEV_NAME, ST_LSM6DS3_ID }, 91 { ST_LSM6DS3H_DEV_NAME, ST_LSM6DS3H_ID }, 92 { ST_LSM6DSL_DEV_NAME, ST_LSM6DSL_ID }, 93 { ST_LSM6DSM_DEV_NAME, ST_LSM6DSM_ID }, 94 {}, 95 }; 96 MODULE_DEVICE_TABLE(i2c, st_lsm6dsx_i2c_id_table); 97 98 static struct i2c_driver st_lsm6dsx_driver = { 99 .driver = { 100 .name = "st_lsm6dsx_i2c", 101 .pm = &st_lsm6dsx_pm_ops, 102 .of_match_table = of_match_ptr(st_lsm6dsx_i2c_of_match), 103 }, 104 .probe = st_lsm6dsx_i2c_probe, 105 .id_table = st_lsm6dsx_i2c_id_table, 106 }; 107 module_i2c_driver(st_lsm6dsx_driver); 108 109 MODULE_AUTHOR("Lorenzo Bianconi <lorenzo.bianconi@st.com>"); 110 MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>"); 111 MODULE_DESCRIPTION("STMicroelectronics st_lsm6dsx i2c driver"); 112 MODULE_LICENSE("GPL v2"); 113