drm/modes: Fix drm_mode_vrefres() docs
[drm/drm-misc.git] / drivers / i2c / busses / i2c-tegra-bpmp.c
blobbb0de6db6391bc2a262d8673c6aa65143a5d411c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
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>
8 */
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>
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
24 * and length
26 #define SERIALI2C_HDR_SIZE 6
28 struct tegra_bpmp_i2c {
29 struct i2c_adapter adapter;
30 struct device *dev;
32 struct tegra_bpmp *bpmp;
33 unsigned int bus;
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.
41 static void tegra_bpmp_xlate_flags(u16 flags, u16 *out)
43 if (flags & I2C_M_TEN)
44 *out |= SERIALI2C_TEN;
46 if (flags & I2C_M_RD)
47 *out |= SERIALI2C_RD;
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]
72 * ...
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,
84 struct i2c_msg *msgs,
85 unsigned int num)
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];
92 u16 flags = 0;
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,
129 unsigned int num)
131 size_t size = response->xfer.data_size, len = 0, pos = 0;
132 char *buf = response->xfer.data_buf;
133 unsigned int i;
135 for (i = 0; i < num; i++)
136 if (msgs[i].flags & I2C_M_RD)
137 len += msgs[i].len;
139 if (len != size)
140 return -EINVAL;
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;
149 return 0;
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;
155 unsigned int i;
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)
162 return -EINVAL;
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)
169 return -EINVAL;
171 return 0;
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)
179 struct tegra_bpmp_message msg;
180 int err;
182 request->cmd = CMD_I2C_XFER;
183 request->xfer.bus_id = i2c->bus;
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);
192 if (atomic)
193 err = tegra_bpmp_transfer_atomic(i2c->bpmp, &msg);
194 else
195 err = tegra_bpmp_transfer(i2c->bpmp, &msg);
197 if (err < 0) {
198 dev_err(i2c->dev, "failed to transfer message: %d\n", err);
199 return err;
202 if (msg.rx.ret != 0) {
203 if (msg.rx.ret == -BPMP_EAGAIN) {
204 dev_dbg(i2c->dev, "arbitration lost\n");
205 return -EAGAIN;
208 if (msg.rx.ret == -BPMP_ETIMEDOUT) {
209 dev_dbg(i2c->dev, "timeout\n");
210 return -ETIMEDOUT;
213 if (msg.rx.ret == -BPMP_ENXIO) {
214 dev_dbg(i2c->dev, "NAK\n");
215 return -ENXIO;
218 dev_err(i2c->dev, "transaction failed: %d\n", msg.rx.ret);
219 return -EIO;
222 return 0;
225 static int tegra_bpmp_i2c_xfer_common(struct i2c_adapter *adapter,
226 struct i2c_msg *msgs, int num,
227 bool atomic)
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;
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;
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);
245 if (err < 0) {
246 dev_err(i2c->dev, "failed to transfer message: %d\n", err);
247 return err;
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;
256 return num;
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;
286 u32 value;
287 int err;
289 i2c = devm_kzalloc(&pdev->dev, sizeof(*i2c), GFP_KERNEL);
290 if (!i2c)
291 return -ENOMEM;
293 i2c->dev = &pdev->dev;
295 i2c->bpmp = dev_get_drvdata(pdev->dev.parent);
296 if (!i2c->bpmp)
297 return -ENODEV;
299 err = of_property_read_u32(pdev->dev.of_node, "nvidia,bpmp-bus-id",
300 &value);
301 if (err < 0)
302 return err;
304 i2c->bus = value;
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 = {
333 .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");