2 * STMicroelectronics st_lsm6dsx i2c driver
4 * Copyright 2016 STMicroelectronics Inc.
6 * Lorenzo Bianconi <lorenzo.bianconi@st.com>
7 * Denis Ciocca <denis.ciocca@st.com>
9 * Licensed under the GPL-2.
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/i2c.h>
15 #include <linux/slab.h>
18 #include "st_lsm6dsx.h"
20 static int st_lsm6dsx_i2c_read(struct device *dev, u8 addr, int len, u8 *data)
22 struct i2c_client *client = to_i2c_client(dev);
23 struct i2c_msg msg[2];
25 msg[0].addr = client->addr;
26 msg[0].flags = client->flags;
30 msg[1].addr = client->addr;
31 msg[1].flags = client->flags | I2C_M_RD;
35 return i2c_transfer(client->adapter, msg, 2);
38 static int st_lsm6dsx_i2c_write(struct device *dev, u8 addr, int len, u8 *data)
40 struct i2c_client *client = to_i2c_client(dev);
45 memcpy(&send[1], data, len * sizeof(u8));
47 msg.addr = client->addr;
48 msg.flags = client->flags;
52 return i2c_transfer(client->adapter, &msg, 1);
55 static const struct st_lsm6dsx_transfer_function st_lsm6dsx_transfer_fn = {
56 .read = st_lsm6dsx_i2c_read,
57 .write = st_lsm6dsx_i2c_write,
60 static int st_lsm6dsx_i2c_probe(struct i2c_client *client,
61 const struct i2c_device_id *id)
63 return st_lsm6dsx_probe(&client->dev, client->irq,
64 (int)id->driver_data, id->name,
65 &st_lsm6dsx_transfer_fn);
68 static const struct of_device_id st_lsm6dsx_i2c_of_match[] = {
70 .compatible = "st,lsm6ds3",
71 .data = (void *)ST_LSM6DS3_ID,
74 .compatible = "st,lsm6ds3h",
75 .data = (void *)ST_LSM6DS3H_ID,
78 .compatible = "st,lsm6dsl",
79 .data = (void *)ST_LSM6DSL_ID,
82 .compatible = "st,lsm6dsm",
83 .data = (void *)ST_LSM6DSM_ID,
87 MODULE_DEVICE_TABLE(of, st_lsm6dsx_i2c_of_match);
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 },
96 MODULE_DEVICE_TABLE(i2c, st_lsm6dsx_i2c_id_table);
98 static struct i2c_driver st_lsm6dsx_driver = {
100 .name = "st_lsm6dsx_i2c",
101 .pm = &st_lsm6dsx_pm_ops,
102 .of_match_table = of_match_ptr(st_lsm6dsx_i2c_of_match),
104 .probe = st_lsm6dsx_i2c_probe,
105 .id_table = st_lsm6dsx_i2c_id_table,
107 module_i2c_driver(st_lsm6dsx_driver);
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");