1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2016, NVIDIA CORPORATION.
10 #include <asm/arch-tegra/bpmp_abi.h>
11 #include <asm/global_data.h>
12 #include <linux/bitops.h>
14 DECLARE_GLOBAL_DATA_PTR;
16 struct tegra186_bpmp_i2c {
20 static inline void serialize_u16(uint8_t **p, uint16_t val)
27 /* These just happen to have the same values as I2C_M_* and SERIALI2C_* */
28 #define SUPPORTED_FLAGS \
33 I2C_M_REV_DIR_ADDR | \
38 static int tegra186_bpmp_i2c_xfer(struct udevice *dev, struct i2c_msg *msg,
41 struct tegra186_bpmp_i2c *priv = dev_get_priv(dev);
42 struct mrq_i2c_request req;
43 struct mrq_i2c_response resp;
47 req.cmd = CMD_I2C_XFER;
48 req.xfer.bus_id = priv->bpmp_bus_id;
49 p = &req.xfer.data_buf[0];
50 left = ARRAY_SIZE(req.xfer.data_buf);
51 for (i = 0; i < nmsgs; i++) {
53 if (!(msg[i].flags & I2C_M_RD))
55 if ((len >= BIT(16)) || (len > left))
58 if (msg[i].flags & ~SUPPORTED_FLAGS)
61 serialize_u16(&p, msg[i].addr);
62 serialize_u16(&p, msg[i].flags);
63 serialize_u16(&p, msg[i].len);
64 if (!(msg[i].flags & I2C_M_RD)) {
65 memcpy(p, msg[i].buf, msg[i].len);
69 req.xfer.data_size = p - &req.xfer.data_buf[0];
71 ret = misc_call(dev->parent, MRQ_I2C, &req, sizeof(req), &resp,
76 p = &resp.xfer.data_buf[0];
77 left = resp.xfer.data_size;
78 if (left > ARRAY_SIZE(resp.xfer.data_buf))
80 for (i = 0; i < nmsgs; i++) {
81 if (msg[i].flags & I2C_M_RD) {
82 memcpy(msg[i].buf, p, msg[i].len);
90 static int tegra186_bpmp_probe_chip(struct udevice *bus, uint chip_addr,
96 static int tegra186_bpmp_i2c_probe(struct udevice *dev)
98 struct tegra186_bpmp_i2c *priv = dev_get_priv(dev);
100 priv->bpmp_bus_id = fdtdec_get_uint(gd->fdt_blob, dev_of_offset(dev),
101 "nvidia,bpmp-bus-id", U32_MAX);
102 if (priv->bpmp_bus_id == U32_MAX) {
103 debug("%s: could not parse nvidia,bpmp-bus-id\n", __func__);
110 static const struct dm_i2c_ops tegra186_bpmp_i2c_ops = {
111 .xfer = tegra186_bpmp_i2c_xfer,
112 .probe_chip = tegra186_bpmp_probe_chip,
115 static const struct udevice_id tegra186_bpmp_i2c_ids[] = {
116 { .compatible = "nvidia,tegra186-bpmp-i2c" },
120 U_BOOT_DRIVER(i2c_gpio) = {
121 .name = "tegra186_bpmp_i2c",
123 .of_match = tegra186_bpmp_i2c_ids,
124 .probe = tegra186_bpmp_i2c_probe,
125 .priv_auto = sizeof(struct tegra186_bpmp_i2c),
126 .ops = &tegra186_bpmp_i2c_ops,