treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 200
[linux-2.6-block.git] / drivers / i2c / busses / i2c-tegra-bpmp.c
CommitLineData
0297ffa6
SSM
1/*
2 * drivers/i2c/busses/i2c-tegra-bpmp.c
3 *
4 * Copyright (c) 2016 NVIDIA Corporation. All rights reserved.
5 *
6 * Author: Shardar Shariff Md <smohammed@nvidia.com>
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms and conditions of the GNU General Public License,
10 * version 2, as published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21#include <linux/err.h>
22#include <linux/i2c.h>
23#include <linux/init.h>
24#include <linux/kernel.h>
25#include <linux/module.h>
26#include <linux/of_device.h>
27#include <linux/platform_device.h>
28#include <linux/pm_runtime.h>
29
30#include <soc/tegra/bpmp-abi.h>
31#include <soc/tegra/bpmp.h>
32
33/*
34 * Serialized I2C message header size is 6 bytes and includes address, flags
35 * and length
36 */
37#define SERIALI2C_HDR_SIZE 6
38
39struct tegra_bpmp_i2c {
40 struct i2c_adapter adapter;
41 struct device *dev;
42
43 struct tegra_bpmp *bpmp;
44 unsigned int bus;
45};
46
47/*
48 * Linux flags are translated to BPMP defined I2C flags that are used in BPMP
49 * firmware I2C driver to avoid any issues in future if Linux I2C flags are
50 * changed.
51 */
52static int tegra_bpmp_xlate_flags(u16 flags, u16 *out)
53{
54 if (flags & I2C_M_TEN) {
55 *out |= SERIALI2C_TEN;
56 flags &= ~I2C_M_TEN;
57 }
58
59 if (flags & I2C_M_RD) {
60 *out |= SERIALI2C_RD;
61 flags &= ~I2C_M_RD;
62 }
63
64 if (flags & I2C_M_STOP) {
65 *out |= SERIALI2C_STOP;
66 flags &= ~I2C_M_STOP;
67 }
68
69 if (flags & I2C_M_NOSTART) {
70 *out |= SERIALI2C_NOSTART;
71 flags &= ~I2C_M_NOSTART;
72 }
73
74 if (flags & I2C_M_REV_DIR_ADDR) {
75 *out |= SERIALI2C_REV_DIR_ADDR;
76 flags &= ~I2C_M_REV_DIR_ADDR;
77 }
78
79 if (flags & I2C_M_IGNORE_NAK) {
80 *out |= SERIALI2C_IGNORE_NAK;
81 flags &= ~I2C_M_IGNORE_NAK;
82 }
83
84 if (flags & I2C_M_NO_RD_ACK) {
85 *out |= SERIALI2C_NO_RD_ACK;
86 flags &= ~I2C_M_NO_RD_ACK;
87 }
88
89 if (flags & I2C_M_RECV_LEN) {
90 *out |= SERIALI2C_RECV_LEN;
91 flags &= ~I2C_M_RECV_LEN;
92 }
93
94 return (flags != 0) ? -EINVAL : 0;
95}
96
97/**
98 * The serialized I2C format is simply the following:
99 * [addr little-endian][flags little-endian][len little-endian][data if write]
100 * [addr little-endian][flags little-endian][len little-endian][data if write]
101 * ...
102 *
103 * The flags are translated from Linux kernel representation to seriali2c
104 * representation. Any undefined flag being set causes an error.
105 *
106 * The data is there only for writes. Reads have the data transferred in the
107 * other direction, and thus data is not present.
108 *
109 * See deserialize_i2c documentation for the data format in the other direction.
110 */
111static int tegra_bpmp_serialize_i2c_msg(struct tegra_bpmp_i2c *i2c,
112 struct mrq_i2c_request *request,
113 struct i2c_msg *msgs,
114 unsigned int num)
115{
116 char *buf = request->xfer.data_buf;
117 unsigned int i, j, pos = 0;
118 int err;
119
120 for (i = 0; i < num; i++) {
121 struct i2c_msg *msg = &msgs[i];
122 u16 flags = 0;
123
124 err = tegra_bpmp_xlate_flags(msg->flags, &flags);
125 if (err < 0)
126 return err;
127
128 buf[pos++] = msg->addr & 0xff;
129 buf[pos++] = (msg->addr & 0xff00) >> 8;
130 buf[pos++] = flags & 0xff;
131 buf[pos++] = (flags & 0xff00) >> 8;
132 buf[pos++] = msg->len & 0xff;
133 buf[pos++] = (msg->len & 0xff00) >> 8;
134
135 if ((flags & SERIALI2C_RD) == 0) {
136 for (j = 0; j < msg->len; j++)
137 buf[pos++] = msg->buf[j];
138 }
139 }
140
141 request->xfer.data_size = pos;
142
143 return 0;
144}
145
146/**
147 * The data in the BPMP -> CPU direction is composed of sequential blocks for
148 * those messages that have I2C_M_RD. So, for example, if you have:
149 *
150 * - !I2C_M_RD, len == 5, data == a0 01 02 03 04
151 * - !I2C_M_RD, len == 1, data == a0
152 * - I2C_M_RD, len == 2, data == [uninitialized buffer 1]
153 * - !I2C_M_RD, len == 1, data == a2
154 * - I2C_M_RD, len == 2, data == [uninitialized buffer 2]
155 *
156 * ...then the data in the BPMP -> CPU direction would be 4 bytes total, and
157 * would contain 2 bytes that will go to uninitialized buffer 1, and 2 bytes
158 * that will go to uninitialized buffer 2.
159 */
160static int tegra_bpmp_i2c_deserialize(struct tegra_bpmp_i2c *i2c,
161 struct mrq_i2c_response *response,
162 struct i2c_msg *msgs,
163 unsigned int num)
164{
165 size_t size = response->xfer.data_size, len = 0, pos = 0;
166 char *buf = response->xfer.data_buf;
167 unsigned int i;
168
169 for (i = 0; i < num; i++)
170 if (msgs[i].flags & I2C_M_RD)
171 len += msgs[i].len;
172
173 if (len != size)
174 return -EINVAL;
175
176 for (i = 0; i < num; i++) {
177 if (msgs[i].flags & I2C_M_RD) {
178 memcpy(msgs[i].buf, buf + pos, msgs[i].len);
179 pos += msgs[i].len;
180 }
181 }
182
183 return 0;
184}
185
186static int tegra_bpmp_i2c_msg_len_check(struct i2c_msg *msgs, unsigned int num)
187{
188 size_t tx_len = 0, rx_len = 0;
189 unsigned int i;
190
191 for (i = 0; i < num; i++)
192 if (!(msgs[i].flags & I2C_M_RD))
193 tx_len += SERIALI2C_HDR_SIZE + msgs[i].len;
194
195 if (tx_len > TEGRA_I2C_IPC_MAX_IN_BUF_SIZE)
196 return -EINVAL;
197
198 for (i = 0; i < num; i++)
199 if ((msgs[i].flags & I2C_M_RD))
200 rx_len += msgs[i].len;
201
202 if (rx_len > TEGRA_I2C_IPC_MAX_OUT_BUF_SIZE)
203 return -EINVAL;
204
205 return 0;
206}
207
208static int tegra_bpmp_i2c_msg_xfer(struct tegra_bpmp_i2c *i2c,
209 struct mrq_i2c_request *request,
08960b02
WS
210 struct mrq_i2c_response *response,
211 bool atomic)
0297ffa6
SSM
212{
213 struct tegra_bpmp_message msg;
214 int err;
215
216 request->cmd = CMD_I2C_XFER;
217 request->xfer.bus_id = i2c->bus;
218
219 memset(&msg, 0, sizeof(msg));
220 msg.mrq = MRQ_I2C;
221 msg.tx.data = request;
222 msg.tx.size = sizeof(*request);
223 msg.rx.data = response;
224 msg.rx.size = sizeof(*response);
225
08960b02 226 if (atomic)
0297ffa6
SSM
227 err = tegra_bpmp_transfer_atomic(i2c->bpmp, &msg);
228 else
229 err = tegra_bpmp_transfer(i2c->bpmp, &msg);
230
231 return err;
232}
233
08960b02
WS
234static int tegra_bpmp_i2c_xfer_common(struct i2c_adapter *adapter,
235 struct i2c_msg *msgs, int num,
236 bool atomic)
0297ffa6
SSM
237{
238 struct tegra_bpmp_i2c *i2c = i2c_get_adapdata(adapter);
239 struct mrq_i2c_response response;
240 struct mrq_i2c_request request;
241 int err;
242
243 err = tegra_bpmp_i2c_msg_len_check(msgs, num);
244 if (err < 0) {
245 dev_err(i2c->dev, "unsupported message length\n");
246 return err;
247 }
248
249 memset(&request, 0, sizeof(request));
250 memset(&response, 0, sizeof(response));
251
252 err = tegra_bpmp_serialize_i2c_msg(i2c, &request, msgs, num);
253 if (err < 0) {
254 dev_err(i2c->dev, "failed to serialize message: %d\n", err);
255 return err;
256 }
257
08960b02 258 err = tegra_bpmp_i2c_msg_xfer(i2c, &request, &response, atomic);
0297ffa6
SSM
259 if (err < 0) {
260 dev_err(i2c->dev, "failed to transfer message: %d\n", err);
261 return err;
262 }
263
264 err = tegra_bpmp_i2c_deserialize(i2c, &response, msgs, num);
265 if (err < 0) {
266 dev_err(i2c->dev, "failed to deserialize message: %d\n", err);
267 return err;
268 }
269
270 return num;
271}
272
08960b02
WS
273static int tegra_bpmp_i2c_xfer(struct i2c_adapter *adapter,
274 struct i2c_msg *msgs, int num)
275{
276 return tegra_bpmp_i2c_xfer_common(adapter, msgs, num, false);
277}
278
279static int tegra_bpmp_i2c_xfer_atomic(struct i2c_adapter *adapter,
280 struct i2c_msg *msgs, int num)
281{
282 return tegra_bpmp_i2c_xfer_common(adapter, msgs, num, true);
283}
284
0297ffa6
SSM
285static u32 tegra_bpmp_i2c_func(struct i2c_adapter *adapter)
286{
287 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL | I2C_FUNC_10BIT_ADDR |
288 I2C_FUNC_PROTOCOL_MANGLING | I2C_FUNC_NOSTART;
289}
290
291static const struct i2c_algorithm tegra_bpmp_i2c_algo = {
292 .master_xfer = tegra_bpmp_i2c_xfer,
08960b02 293 .master_xfer_atomic = tegra_bpmp_i2c_xfer_atomic,
0297ffa6
SSM
294 .functionality = tegra_bpmp_i2c_func,
295};
296
297static int tegra_bpmp_i2c_probe(struct platform_device *pdev)
298{
299 struct tegra_bpmp_i2c *i2c;
300 u32 value;
301 int err;
302
303 i2c = devm_kzalloc(&pdev->dev, sizeof(*i2c), GFP_KERNEL);
304 if (!i2c)
305 return -ENOMEM;
306
307 i2c->dev = &pdev->dev;
308
309 i2c->bpmp = dev_get_drvdata(pdev->dev.parent);
310 if (!i2c->bpmp)
311 return -ENODEV;
312
313 err = of_property_read_u32(pdev->dev.of_node, "nvidia,bpmp-bus-id",
314 &value);
315 if (err < 0)
316 return err;
317
318 i2c->bus = value;
319
320 i2c_set_adapdata(&i2c->adapter, i2c);
321 i2c->adapter.owner = THIS_MODULE;
322 strlcpy(i2c->adapter.name, "Tegra BPMP I2C adapter",
323 sizeof(i2c->adapter.name));
324 i2c->adapter.algo = &tegra_bpmp_i2c_algo;
325 i2c->adapter.dev.parent = &pdev->dev;
326 i2c->adapter.dev.of_node = pdev->dev.of_node;
327
328 platform_set_drvdata(pdev, i2c);
329
330 return i2c_add_adapter(&i2c->adapter);
331}
332
333static int tegra_bpmp_i2c_remove(struct platform_device *pdev)
334{
335 struct tegra_bpmp_i2c *i2c = platform_get_drvdata(pdev);
336
337 i2c_del_adapter(&i2c->adapter);
338
339 return 0;
340}
341
342static const struct of_device_id tegra_bpmp_i2c_of_match[] = {
343 { .compatible = "nvidia,tegra186-bpmp-i2c", },
344 { }
345};
346MODULE_DEVICE_TABLE(of, tegra_bpmp_i2c_of_match);
347
348static struct platform_driver tegra_bpmp_i2c_driver = {
349 .driver = {
350 .name = "tegra-bpmp-i2c",
351 .of_match_table = tegra_bpmp_i2c_of_match,
352 },
353 .probe = tegra_bpmp_i2c_probe,
354 .remove = tegra_bpmp_i2c_remove,
355};
356module_platform_driver(tegra_bpmp_i2c_driver);
357
8c91fd5e 358MODULE_DESCRIPTION("NVIDIA Tegra BPMP I2C bus controller driver");
0297ffa6
SSM
359MODULE_AUTHOR("Shardar Shariff Md <smohammed@nvidia.com>");
360MODULE_AUTHOR("Juha-Matti Tilli");
361MODULE_LICENSE("GPL v2");