1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * STMicroelectronics st_lsm6dsr i2c driver
4 *
5 * Copyright 2020 STMicroelectronics Inc.
6 *
7 * Lorenzo Bianconi <lorenzo.bianconi@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_lsm6dsr.h"
19
st_lsm6dsr_i2c_read(struct device * dev,u8 addr,int len,u8 * data)20 static int st_lsm6dsr_i2c_read(struct device *dev, u8 addr,
21 int len, u8 *data)
22 {
23 struct i2c_client *client = to_i2c_client(dev);
24 struct i2c_msg msg[2];
25
26 msg[0].addr = client->addr;
27 msg[0].flags = client->flags;
28 msg[0].len = 1;
29 msg[0].buf = &addr;
30
31 msg[1].addr = client->addr;
32 msg[1].flags = client->flags | I2C_M_RD;
33 msg[1].len = len;
34 msg[1].buf = data;
35
36 return i2c_transfer(client->adapter, msg, 2);
37 }
38
st_lsm6dsr_i2c_write(struct device * dev,u8 addr,int len,const u8 * data)39 static int st_lsm6dsr_i2c_write(struct device *dev, u8 addr, int len,
40 const u8 *data)
41 {
42 struct i2c_client *client = to_i2c_client(dev);
43 struct i2c_msg msg;
44 u8 send[ST_LSM6DSR_TX_MAX_LENGTH];
45
46 send[0] = addr;
47 memcpy(&send[1], data, len * sizeof(u8));
48
49 msg.addr = client->addr;
50 msg.flags = client->flags;
51 msg.len = len + 1;
52 msg.buf = send;
53
54 return i2c_transfer(client->adapter, &msg, 1);
55 }
56
57 static const struct st_lsm6dsr_transfer_function st_lsm6dsr_transfer_fn = {
58 .read = st_lsm6dsr_i2c_read,
59 .write = st_lsm6dsr_i2c_write,
60 };
61
st_lsm6dsr_i2c_probe(struct i2c_client * client,const struct i2c_device_id * id)62 static int st_lsm6dsr_i2c_probe(struct i2c_client *client,
63 const struct i2c_device_id *id)
64 {
65 return st_lsm6dsr_probe(&client->dev, client->irq,
66 &st_lsm6dsr_transfer_fn);
67 }
68
st_lsm6dsr_i2c_remove(struct i2c_client * client)69 static int st_lsm6dsr_i2c_remove(struct i2c_client *client)
70 {
71 return st_lsm6dsr_remove(&client->dev);
72 }
73
74 static const struct of_device_id st_lsm6dsr_i2c_of_match[] = {
75 {
76 .compatible = "st,lsm6dsr",
77 },
78 {},
79 };
80 MODULE_DEVICE_TABLE(of, st_lsm6dsr_i2c_of_match);
81
82 static const struct i2c_device_id st_lsm6dsr_i2c_id_table[] = {
83 { ST_LSM6DSR_DEV_NAME },
84 {},
85 };
86 MODULE_DEVICE_TABLE(i2c, st_lsm6dsr_i2c_id_table);
87
88 static struct i2c_driver st_lsm6dsr_driver = {
89 .driver = {
90 .name = "st_lsm6dsr_i2c",
91 .pm = &st_lsm6dsr_pm_ops,
92 .of_match_table = of_match_ptr(st_lsm6dsr_i2c_of_match),
93 },
94 .probe = st_lsm6dsr_i2c_probe,
95 .remove = st_lsm6dsr_i2c_remove,
96 .id_table = st_lsm6dsr_i2c_id_table,
97 };
98 module_i2c_driver(st_lsm6dsr_driver);
99
100 MODULE_AUTHOR("Lorenzo Bianconi <lorenzo.bianconi@st.com>");
101 MODULE_DESCRIPTION("STMicroelectronics st_lsm6dsr i2c driver");
102 MODULE_LICENSE("GPL");
103