1 // SPDX-License-Identifier: GPL-2.0-only
3 * drivers/i2c/busses/i2c-tegra-bpmp.c
5 * Copyright (c) 2016 NVIDIA Corporation. All rights reserved.
7 * Author: Shardar Shariff Md <smohammed@nvidia.com>
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>
16 #include <linux/platform_device.h>
17 #include <linux/pm_runtime.h>
19 #include <soc/tegra/bpmp-abi.h>
20 #include <soc/tegra/bpmp.h>
23 * Serialized I2C message header size is 6 bytes and includes address, flags
26 #define SERIALI2C_HDR_SIZE 6
28 struct tegra_bpmp_i2c
{
29 struct i2c_adapter adapter
;
32 struct tegra_bpmp
*bpmp
;
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
41 static void tegra_bpmp_xlate_flags(u16 flags
, u16
*out
)
43 if (flags
& I2C_M_TEN
)
44 *out
|= SERIALI2C_TEN
;
49 if (flags
& I2C_M_STOP
)
50 *out
|= SERIALI2C_STOP
;
52 if (flags
& I2C_M_NOSTART
)
53 *out
|= SERIALI2C_NOSTART
;
55 if (flags
& I2C_M_REV_DIR_ADDR
)
56 *out
|= SERIALI2C_REV_DIR_ADDR
;
58 if (flags
& I2C_M_IGNORE_NAK
)
59 *out
|= SERIALI2C_IGNORE_NAK
;
61 if (flags
& I2C_M_NO_RD_ACK
)
62 *out
|= SERIALI2C_NO_RD_ACK
;
64 if (flags
& I2C_M_RECV_LEN
)
65 *out
|= SERIALI2C_RECV_LEN
;
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]
74 * The flags are translated from Linux kernel representation to seriali2c
75 * representation. Any undefined flag being set causes an error.
77 * The data is there only for writes. Reads have the data transferred in the
78 * other direction, and thus data is not present.
80 * See deserialize_i2c documentation for the data format in the other direction.
82 static void tegra_bpmp_serialize_i2c_msg(struct tegra_bpmp_i2c
*i2c
,
83 struct mrq_i2c_request
*request
,
87 char *buf
= request
->xfer
.data_buf
;
88 unsigned int i
, j
, pos
= 0;
90 for (i
= 0; i
< num
; i
++) {
91 struct i2c_msg
*msg
= &msgs
[i
];
94 tegra_bpmp_xlate_flags(msg
->flags
, &flags
);
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;
103 if ((flags
& SERIALI2C_RD
) == 0) {
104 for (j
= 0; j
< msg
->len
; j
++)
105 buf
[pos
++] = msg
->buf
[j
];
109 request
->xfer
.data_size
= pos
;
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:
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]
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.
126 static int tegra_bpmp_i2c_deserialize(struct tegra_bpmp_i2c
*i2c
,
127 struct mrq_i2c_response
*response
,
128 struct i2c_msg
*msgs
,
131 size_t size
= response
->xfer
.data_size
, len
= 0, pos
= 0;
132 char *buf
= response
->xfer
.data_buf
;
135 for (i
= 0; i
< num
; i
++)
136 if (msgs
[i
].flags
& I2C_M_RD
)
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
);
152 static int tegra_bpmp_i2c_msg_len_check(struct i2c_msg
*msgs
, unsigned int num
)
154 size_t tx_len
= 0, rx_len
= 0;
157 for (i
= 0; i
< num
; i
++)
158 if (!(msgs
[i
].flags
& I2C_M_RD
))
159 tx_len
+= SERIALI2C_HDR_SIZE
+ msgs
[i
].len
;
161 if (tx_len
> TEGRA_I2C_IPC_MAX_IN_BUF_SIZE
)
164 for (i
= 0; i
< num
; i
++)
165 if ((msgs
[i
].flags
& I2C_M_RD
))
166 rx_len
+= msgs
[i
].len
;
168 if (rx_len
> TEGRA_I2C_IPC_MAX_OUT_BUF_SIZE
)
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
,
179 struct tegra_bpmp_message msg
;
182 request
->cmd
= CMD_I2C_XFER
;
183 request
->xfer
.bus_id
= i2c
->bus
;
185 memset(&msg
, 0, sizeof(msg
));
187 msg
.tx
.data
= request
;
188 msg
.tx
.size
= sizeof(*request
);
189 msg
.rx
.data
= response
;
190 msg
.rx
.size
= sizeof(*response
);
193 err
= tegra_bpmp_transfer_atomic(i2c
->bpmp
, &msg
);
195 err
= tegra_bpmp_transfer(i2c
->bpmp
, &msg
);
198 dev_err(i2c
->dev
, "failed to transfer message: %d\n", err
);
202 if (msg
.rx
.ret
!= 0) {
203 if (msg
.rx
.ret
== -BPMP_EAGAIN
) {
204 dev_dbg(i2c
->dev
, "arbitration lost\n");
208 if (msg
.rx
.ret
== -BPMP_ETIMEDOUT
) {
209 dev_dbg(i2c
->dev
, "timeout\n");
213 if (msg
.rx
.ret
== -BPMP_ENXIO
) {
214 dev_dbg(i2c
->dev
, "NAK\n");
218 dev_err(i2c
->dev
, "transaction failed: %d\n", msg
.rx
.ret
);
225 static int tegra_bpmp_i2c_xfer_common(struct i2c_adapter
*adapter
,
226 struct i2c_msg
*msgs
, int num
,
229 struct tegra_bpmp_i2c
*i2c
= i2c_get_adapdata(adapter
);
230 struct mrq_i2c_response response
;
231 struct mrq_i2c_request request
;
234 err
= tegra_bpmp_i2c_msg_len_check(msgs
, num
);
236 dev_err(i2c
->dev
, "unsupported message length\n");
240 memset(&request
, 0, sizeof(request
));
241 memset(&response
, 0, sizeof(response
));
243 tegra_bpmp_serialize_i2c_msg(i2c
, &request
, msgs
, num
);
244 err
= tegra_bpmp_i2c_msg_xfer(i2c
, &request
, &response
, atomic
);
246 dev_err(i2c
->dev
, "failed to transfer message: %d\n", err
);
250 err
= tegra_bpmp_i2c_deserialize(i2c
, &response
, msgs
, num
);
252 dev_err(i2c
->dev
, "failed to deserialize message: %d\n", err
);
259 static int tegra_bpmp_i2c_xfer(struct i2c_adapter
*adapter
,
260 struct i2c_msg
*msgs
, int num
)
262 return tegra_bpmp_i2c_xfer_common(adapter
, msgs
, num
, false);
265 static int tegra_bpmp_i2c_xfer_atomic(struct i2c_adapter
*adapter
,
266 struct i2c_msg
*msgs
, int num
)
268 return tegra_bpmp_i2c_xfer_common(adapter
, msgs
, num
, true);
271 static u32
tegra_bpmp_i2c_func(struct i2c_adapter
*adapter
)
273 return I2C_FUNC_I2C
| I2C_FUNC_SMBUS_EMUL
| I2C_FUNC_10BIT_ADDR
|
274 I2C_FUNC_PROTOCOL_MANGLING
| I2C_FUNC_NOSTART
;
277 static const struct i2c_algorithm tegra_bpmp_i2c_algo
= {
278 .xfer
= tegra_bpmp_i2c_xfer
,
279 .xfer_atomic
= tegra_bpmp_i2c_xfer_atomic
,
280 .functionality
= tegra_bpmp_i2c_func
,
283 static int tegra_bpmp_i2c_probe(struct platform_device
*pdev
)
285 struct tegra_bpmp_i2c
*i2c
;
289 i2c
= devm_kzalloc(&pdev
->dev
, sizeof(*i2c
), GFP_KERNEL
);
293 i2c
->dev
= &pdev
->dev
;
295 i2c
->bpmp
= dev_get_drvdata(pdev
->dev
.parent
);
299 err
= of_property_read_u32(pdev
->dev
.of_node
, "nvidia,bpmp-bus-id",
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
;
314 platform_set_drvdata(pdev
, i2c
);
316 return i2c_add_adapter(&i2c
->adapter
);
319 static void tegra_bpmp_i2c_remove(struct platform_device
*pdev
)
321 struct tegra_bpmp_i2c
*i2c
= platform_get_drvdata(pdev
);
323 i2c_del_adapter(&i2c
->adapter
);
326 static const struct of_device_id tegra_bpmp_i2c_of_match
[] = {
327 { .compatible
= "nvidia,tegra186-bpmp-i2c", },
330 MODULE_DEVICE_TABLE(of
, tegra_bpmp_i2c_of_match
);
332 static struct platform_driver tegra_bpmp_i2c_driver
= {
334 .name
= "tegra-bpmp-i2c",
335 .of_match_table
= tegra_bpmp_i2c_of_match
,
337 .probe
= tegra_bpmp_i2c_probe
,
338 .remove
= tegra_bpmp_i2c_remove
,
340 module_platform_driver(tegra_bpmp_i2c_driver
);
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");