1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * drivers/i2c/busses/i2c-tegra-bpmp.c
4  *
5  * Copyright (c) 2016 NVIDIA Corporation.  All rights reserved.
6  *
7  * Author: Shardar Shariff Md <smohammed@nvidia.com>
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.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  * Serialized I2C message header size is 6 bytes and includes address, flags
24  * and length
25  */
26 #define SERIALI2C_HDR_SIZE 6
27 
28 struct 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  * Linux flags are translated to BPMP defined I2C flags that are used in BPMP
38  * firmware I2C driver to avoid any issues in future if Linux I2C flags are
39  * changed.
40  */
41 static 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  * The serialized I2C format is simply the following:
70  * [addr little-endian][flags little-endian][len little-endian][data if write]
71  * [addr little-endian][flags little-endian][len little-endian][data if write]
72  *  ...
73  *
74  * The flags are translated from Linux kernel representation to seriali2c
75  * representation. Any undefined flag being set causes an error.
76  *
77  * The data is there only for writes. Reads have the data transferred in the
78  * other direction, and thus data is not present.
79  *
80  * See deserialize_i2c documentation for the data format in the other direction.
81  */
82 static 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  * The data in the BPMP -> CPU direction is composed of sequential blocks for
114  * those messages that have I2C_M_RD. So, for example, if you have:
115  *
116  * - !I2C_M_RD, len == 5, data == a0 01 02 03 04
117  * - !I2C_M_RD, len == 1, data == a0
118  * - I2C_M_RD, len == 2, data == [uninitialized buffer 1]
119  * - !I2C_M_RD, len == 1, data == a2
120  * - I2C_M_RD, len == 2, data == [uninitialized buffer 2]
121  *
122  * ...then the data in the BPMP -> CPU direction would be 4 bytes total, and
123  * would contain 2 bytes that will go to uninitialized buffer 1, and 2 bytes
124  * that will go to uninitialized buffer 2.
125  */
126 static 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 
152 static 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 
174 static 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 
225 static 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 
259 static 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 
265 static 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 
271 static 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 
277 static 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 
283 static 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 	strscpy(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 
319 static void 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 
326 static const struct of_device_id tegra_bpmp_i2c_of_match[] = {
327 	{ .compatible = "nvidia,tegra186-bpmp-i2c", },
328 	{ }
329 };
330 MODULE_DEVICE_TABLE(of, tegra_bpmp_i2c_of_match);
331 
332 static struct platform_driver tegra_bpmp_i2c_driver = {
333 	.driver = {
334 		.name = "tegra-bpmp-i2c",
335 		.of_match_table = tegra_bpmp_i2c_of_match,
336 	},
337 	.probe = tegra_bpmp_i2c_probe,
338 	.remove_new = tegra_bpmp_i2c_remove,
339 };
340 module_platform_driver(tegra_bpmp_i2c_driver);
341 
342 MODULE_DESCRIPTION("NVIDIA Tegra BPMP I2C bus controller driver");
343 MODULE_AUTHOR("Shardar Shariff Md <smohammed@nvidia.com>");
344 MODULE_AUTHOR("Juha-Matti Tilli");
345 MODULE_LICENSE("GPL v2");
346