1
2
3
4
5
6
7
8
9
10#include <linux/err.h>
11#include <linux/i2c.h>
12#include <linux/init.h>
13#include <linux/kernel.h>
14#include <linux/module.h>
15#include <linux/of_device.h>
16#include <linux/platform_device.h>
17#include <linux/pm_runtime.h>
18
19#include <soc/tegra/bpmp-abi.h>
20#include <soc/tegra/bpmp.h>
21
22
23
24
25
26#define SERIALI2C_HDR_SIZE 6
27
28struct tegra_bpmp_i2c {
29 struct i2c_adapter adapter;
30 struct device *dev;
31
32 struct tegra_bpmp *bpmp;
33 unsigned int bus;
34};
35
36
37
38
39
40
41static void tegra_bpmp_xlate_flags(u16 flags, u16 *out)
42{
43 if (flags & I2C_M_TEN)
44 *out |= SERIALI2C_TEN;
45
46 if (flags & I2C_M_RD)
47 *out |= SERIALI2C_RD;
48
49 if (flags & I2C_M_STOP)
50 *out |= SERIALI2C_STOP;
51
52 if (flags & I2C_M_NOSTART)
53 *out |= SERIALI2C_NOSTART;
54
55 if (flags & I2C_M_REV_DIR_ADDR)
56 *out |= SERIALI2C_REV_DIR_ADDR;
57
58 if (flags & I2C_M_IGNORE_NAK)
59 *out |= SERIALI2C_IGNORE_NAK;
60
61 if (flags & I2C_M_NO_RD_ACK)
62 *out |= SERIALI2C_NO_RD_ACK;
63
64 if (flags & I2C_M_RECV_LEN)
65 *out |= SERIALI2C_RECV_LEN;
66}
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82static void tegra_bpmp_serialize_i2c_msg(struct tegra_bpmp_i2c *i2c,
83 struct mrq_i2c_request *request,
84 struct i2c_msg *msgs,
85 unsigned int num)
86{
87 char *buf = request->xfer.data_buf;
88 unsigned int i, j, pos = 0;
89
90 for (i = 0; i < num; i++) {
91 struct i2c_msg *msg = &msgs[i];
92 u16 flags = 0;
93
94 tegra_bpmp_xlate_flags(msg->flags, &flags);
95
96 buf[pos++] = msg->addr & 0xff;
97 buf[pos++] = (msg->addr & 0xff00) >> 8;
98 buf[pos++] = flags & 0xff;
99 buf[pos++] = (flags & 0xff00) >> 8;
100 buf[pos++] = msg->len & 0xff;
101 buf[pos++] = (msg->len & 0xff00) >> 8;
102
103 if ((flags & SERIALI2C_RD) == 0) {
104 for (j = 0; j < msg->len; j++)
105 buf[pos++] = msg->buf[j];
106 }
107 }
108
109 request->xfer.data_size = pos;
110}
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126static int tegra_bpmp_i2c_deserialize(struct tegra_bpmp_i2c *i2c,
127 struct mrq_i2c_response *response,
128 struct i2c_msg *msgs,
129 unsigned int num)
130{
131 size_t size = response->xfer.data_size, len = 0, pos = 0;
132 char *buf = response->xfer.data_buf;
133 unsigned int i;
134
135 for (i = 0; i < num; i++)
136 if (msgs[i].flags & I2C_M_RD)
137 len += msgs[i].len;
138
139 if (len != size)
140 return -EINVAL;
141
142 for (i = 0; i < num; i++) {
143 if (msgs[i].flags & I2C_M_RD) {
144 memcpy(msgs[i].buf, buf + pos, msgs[i].len);
145 pos += msgs[i].len;
146 }
147 }
148
149 return 0;
150}
151
152static int tegra_bpmp_i2c_msg_len_check(struct i2c_msg *msgs, unsigned int num)
153{
154 size_t tx_len = 0, rx_len = 0;
155 unsigned int i;
156
157 for (i = 0; i < num; i++)
158 if (!(msgs[i].flags & I2C_M_RD))
159 tx_len += SERIALI2C_HDR_SIZE + msgs[i].len;
160
161 if (tx_len > TEGRA_I2C_IPC_MAX_IN_BUF_SIZE)
162 return -EINVAL;
163
164 for (i = 0; i < num; i++)
165 if ((msgs[i].flags & I2C_M_RD))
166 rx_len += msgs[i].len;
167
168 if (rx_len > TEGRA_I2C_IPC_MAX_OUT_BUF_SIZE)
169 return -EINVAL;
170
171 return 0;
172}
173
174static int tegra_bpmp_i2c_msg_xfer(struct tegra_bpmp_i2c *i2c,
175 struct mrq_i2c_request *request,
176 struct mrq_i2c_response *response,
177 bool atomic)
178{
179 struct tegra_bpmp_message msg;
180 int err;
181
182 request->cmd = CMD_I2C_XFER;
183 request->xfer.bus_id = i2c->bus;
184
185 memset(&msg, 0, sizeof(msg));
186 msg.mrq = MRQ_I2C;
187 msg.tx.data = request;
188 msg.tx.size = sizeof(*request);
189 msg.rx.data = response;
190 msg.rx.size = sizeof(*response);
191
192 if (atomic)
193 err = tegra_bpmp_transfer_atomic(i2c->bpmp, &msg);
194 else
195 err = tegra_bpmp_transfer(i2c->bpmp, &msg);
196
197 if (err < 0) {
198 dev_err(i2c->dev, "failed to transfer message: %d\n", err);
199 return err;
200 }
201
202 if (msg.rx.ret != 0) {
203 if (msg.rx.ret == -BPMP_EAGAIN) {
204 dev_dbg(i2c->dev, "arbitration lost\n");
205 return -EAGAIN;
206 }
207
208 if (msg.rx.ret == -BPMP_ETIMEDOUT) {
209 dev_dbg(i2c->dev, "timeout\n");
210 return -ETIMEDOUT;
211 }
212
213 if (msg.rx.ret == -BPMP_ENXIO) {
214 dev_dbg(i2c->dev, "NAK\n");
215 return -ENXIO;
216 }
217
218 dev_err(i2c->dev, "transaction failed: %d\n", msg.rx.ret);
219 return -EIO;
220 }
221
222 return 0;
223}
224
225static int tegra_bpmp_i2c_xfer_common(struct i2c_adapter *adapter,
226 struct i2c_msg *msgs, int num,
227 bool atomic)
228{
229 struct tegra_bpmp_i2c *i2c = i2c_get_adapdata(adapter);
230 struct mrq_i2c_response response;
231 struct mrq_i2c_request request;
232 int err;
233
234 err = tegra_bpmp_i2c_msg_len_check(msgs, num);
235 if (err < 0) {
236 dev_err(i2c->dev, "unsupported message length\n");
237 return err;
238 }
239
240 memset(&request, 0, sizeof(request));
241 memset(&response, 0, sizeof(response));
242
243 tegra_bpmp_serialize_i2c_msg(i2c, &request, msgs, num);
244 err = tegra_bpmp_i2c_msg_xfer(i2c, &request, &response, atomic);
245 if (err < 0) {
246 dev_err(i2c->dev, "failed to transfer message: %d\n", err);
247 return err;
248 }
249
250 err = tegra_bpmp_i2c_deserialize(i2c, &response, msgs, num);
251 if (err < 0) {
252 dev_err(i2c->dev, "failed to deserialize message: %d\n", err);
253 return err;
254 }
255
256 return num;
257}
258
259static int tegra_bpmp_i2c_xfer(struct i2c_adapter *adapter,
260 struct i2c_msg *msgs, int num)
261{
262 return tegra_bpmp_i2c_xfer_common(adapter, msgs, num, false);
263}
264
265static int tegra_bpmp_i2c_xfer_atomic(struct i2c_adapter *adapter,
266 struct i2c_msg *msgs, int num)
267{
268 return tegra_bpmp_i2c_xfer_common(adapter, msgs, num, true);
269}
270
271static u32 tegra_bpmp_i2c_func(struct i2c_adapter *adapter)
272{
273 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL | I2C_FUNC_10BIT_ADDR |
274 I2C_FUNC_PROTOCOL_MANGLING | I2C_FUNC_NOSTART;
275}
276
277static const struct i2c_algorithm tegra_bpmp_i2c_algo = {
278 .master_xfer = tegra_bpmp_i2c_xfer,
279 .master_xfer_atomic = tegra_bpmp_i2c_xfer_atomic,
280 .functionality = tegra_bpmp_i2c_func,
281};
282
283static int tegra_bpmp_i2c_probe(struct platform_device *pdev)
284{
285 struct tegra_bpmp_i2c *i2c;
286 u32 value;
287 int err;
288
289 i2c = devm_kzalloc(&pdev->dev, sizeof(*i2c), GFP_KERNEL);
290 if (!i2c)
291 return -ENOMEM;
292
293 i2c->dev = &pdev->dev;
294
295 i2c->bpmp = dev_get_drvdata(pdev->dev.parent);
296 if (!i2c->bpmp)
297 return -ENODEV;
298
299 err = of_property_read_u32(pdev->dev.of_node, "nvidia,bpmp-bus-id",
300 &value);
301 if (err < 0)
302 return err;
303
304 i2c->bus = value;
305
306 i2c_set_adapdata(&i2c->adapter, i2c);
307 i2c->adapter.owner = THIS_MODULE;
308 strlcpy(i2c->adapter.name, "Tegra BPMP I2C adapter",
309 sizeof(i2c->adapter.name));
310 i2c->adapter.algo = &tegra_bpmp_i2c_algo;
311 i2c->adapter.dev.parent = &pdev->dev;
312 i2c->adapter.dev.of_node = pdev->dev.of_node;
313
314 platform_set_drvdata(pdev, i2c);
315
316 return i2c_add_adapter(&i2c->adapter);
317}
318
319static int tegra_bpmp_i2c_remove(struct platform_device *pdev)
320{
321 struct tegra_bpmp_i2c *i2c = platform_get_drvdata(pdev);
322
323 i2c_del_adapter(&i2c->adapter);
324
325 return 0;
326}
327
328static const struct of_device_id tegra_bpmp_i2c_of_match[] = {
329 { .compatible = "nvidia,tegra186-bpmp-i2c", },
330 { }
331};
332MODULE_DEVICE_TABLE(of, tegra_bpmp_i2c_of_match);
333
334static struct platform_driver tegra_bpmp_i2c_driver = {
335 .driver = {
336 .name = "tegra-bpmp-i2c",
337 .of_match_table = tegra_bpmp_i2c_of_match,
338 },
339 .probe = tegra_bpmp_i2c_probe,
340 .remove = tegra_bpmp_i2c_remove,
341};
342module_platform_driver(tegra_bpmp_i2c_driver);
343
344MODULE_DESCRIPTION("NVIDIA Tegra BPMP I2C bus controller driver");
345MODULE_AUTHOR("Shardar Shariff Md <smohammed@nvidia.com>");
346MODULE_AUTHOR("Juha-Matti Tilli");
347MODULE_LICENSE("GPL v2");
348