1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2016, NVIDIA CORPORATION.
11 #include <asm/arch-tegra/bpmp_abi.h>
12 #include <asm/global_data.h>
13 #include <linux/bitops.h>
15 DECLARE_GLOBAL_DATA_PTR;
17 struct tegra186_bpmp_i2c {
21 static inline void serialize_u16(uint8_t **p, uint16_t val)
28 /* These just happen to have the same values as I2C_M_* and SERIALI2C_* */
29 #define SUPPORTED_FLAGS \
34 I2C_M_REV_DIR_ADDR | \
39 static int tegra186_bpmp_i2c_xfer(struct udevice *dev, struct i2c_msg *msg,
42 struct tegra186_bpmp_i2c *priv = dev_get_priv(dev);
43 struct mrq_i2c_request req;
44 struct mrq_i2c_response resp;
48 req.cmd = CMD_I2C_XFER;
49 req.xfer.bus_id = priv->bpmp_bus_id;
50 p = &req.xfer.data_buf[0];
51 left = ARRAY_SIZE(req.xfer.data_buf);
52 for (i = 0; i < nmsgs; i++) {
54 if (!(msg[i].flags & I2C_M_RD))
56 if ((len >= BIT(16)) || (len > left))
59 if (msg[i].flags & ~SUPPORTED_FLAGS)
62 serialize_u16(&p, msg[i].addr);
63 serialize_u16(&p, msg[i].flags);
64 serialize_u16(&p, msg[i].len);
65 if (!(msg[i].flags & I2C_M_RD)) {
66 memcpy(p, msg[i].buf, msg[i].len);
70 req.xfer.data_size = p - &req.xfer.data_buf[0];
72 ret = misc_call(dev->parent, MRQ_I2C, &req, sizeof(req), &resp,
77 p = &resp.xfer.data_buf[0];
78 left = resp.xfer.data_size;
79 if (left > ARRAY_SIZE(resp.xfer.data_buf))
81 for (i = 0; i < nmsgs; i++) {
82 if (msg[i].flags & I2C_M_RD) {
83 memcpy(msg[i].buf, p, msg[i].len);
91 static int tegra186_bpmp_probe_chip(struct udevice *bus, uint chip_addr,
97 static int tegra186_bpmp_i2c_probe(struct udevice *dev)
99 struct tegra186_bpmp_i2c *priv = dev_get_priv(dev);
101 priv->bpmp_bus_id = fdtdec_get_uint(gd->fdt_blob, dev_of_offset(dev),
102 "nvidia,bpmp-bus-id", U32_MAX);
103 if (priv->bpmp_bus_id == U32_MAX) {
104 debug("%s: could not parse nvidia,bpmp-bus-id\n", __func__);
111 static const struct dm_i2c_ops tegra186_bpmp_i2c_ops = {
112 .xfer = tegra186_bpmp_i2c_xfer,
113 .probe_chip = tegra186_bpmp_probe_chip,
116 static const struct udevice_id tegra186_bpmp_i2c_ids[] = {
117 { .compatible = "nvidia,tegra186-bpmp-i2c" },
121 U_BOOT_DRIVER(i2c_gpio) = {
122 .name = "tegra186_bpmp_i2c",
124 .of_match = tegra186_bpmp_i2c_ids,
125 .probe = tegra186_bpmp_i2c_probe,
126 .priv_auto = sizeof(struct tegra186_bpmp_i2c),
127 .ops = &tegra186_bpmp_i2c_ops,