Angel Pons | c74dae9 | 2020-04-02 23:48:16 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Nico Huber | 5817386 | 2017-08-01 17:09:35 +0200 | [diff] [blame] | 2 | |
Nico Huber | 5817386 | 2017-08-01 17:09:35 +0200 | [diff] [blame] | 3 | #include <stdint.h> |
| 4 | #include <console/console.h> |
| 5 | #include <device/smbus.h> |
| 6 | #include <device/i2c_bus.h> |
Nico Huber | 9734af6 | 2017-11-01 13:15:33 +0100 | [diff] [blame] | 7 | #include <commonlib/endian.h> |
Nico Huber | 5817386 | 2017-08-01 17:09:35 +0200 | [diff] [blame] | 8 | |
Matt DeVillier | ee849ba | 2022-05-19 19:03:07 -0500 | [diff] [blame] | 9 | bool i2c_dev_detect(struct device *dev, unsigned int addr) |
| 10 | { |
| 11 | struct i2c_msg seg = { .flags = 0, .slave = addr, .buf = NULL, .len = 0 }; |
| 12 | return dev->ops->ops_i2c_bus->transfer(dev, &seg, 0) == 0; |
| 13 | } |
| 14 | |
Matt DeVillier | 190086e | 2022-03-28 22:24:30 -0500 | [diff] [blame] | 15 | struct bus *i2c_link(const struct device *const dev) |
Nico Huber | 5817386 | 2017-08-01 17:09:35 +0200 | [diff] [blame] | 16 | { |
| 17 | if (!dev || !dev->bus) |
| 18 | return NULL; |
| 19 | |
| 20 | struct bus *link = dev->bus; |
| 21 | while (link) { |
| 22 | struct device *const parent = link->dev; |
| 23 | |
| 24 | if (parent && parent->ops && |
| 25 | (parent->ops->ops_i2c_bus || parent->ops->ops_smbus_bus)) |
| 26 | break; |
| 27 | |
Matt DeVillier | 06abb91 | 2022-06-18 15:22:47 -0500 | [diff] [blame^] | 28 | if (parent && parent->bus && link != parent->bus) |
Nico Huber | 5817386 | 2017-08-01 17:09:35 +0200 | [diff] [blame] | 29 | link = parent->bus; |
| 30 | else |
| 31 | link = NULL; |
| 32 | } |
| 33 | |
Frans Hendriks | 68dc369 | 2021-02-01 11:52:51 +0100 | [diff] [blame] | 34 | if (!link) |
| 35 | printk(BIOS_ALERT, "%s Cannot find I2C or SMBus bus operations", dev_path(dev)); |
Nico Huber | 5817386 | 2017-08-01 17:09:35 +0200 | [diff] [blame] | 36 | |
| 37 | return link; |
| 38 | } |
| 39 | |
Aaron Durbin | 439cee9 | 2018-01-22 21:24:35 -0700 | [diff] [blame] | 40 | int i2c_dev_readb(struct device *const dev) |
Nico Huber | 5817386 | 2017-08-01 17:09:35 +0200 | [diff] [blame] | 41 | { |
| 42 | struct device *const busdev = i2c_busdev(dev); |
| 43 | if (!busdev) |
| 44 | return -1; |
| 45 | |
| 46 | if (busdev->ops->ops_i2c_bus) { |
| 47 | uint8_t val; |
| 48 | const struct i2c_msg msg = { |
| 49 | .flags = I2C_M_RD, |
| 50 | .slave = dev->path.i2c.device, |
| 51 | .buf = &val, |
| 52 | .len = sizeof(val), |
| 53 | }; |
| 54 | |
Frans Hendriks | a9caa50 | 2021-02-01 11:44:37 +0100 | [diff] [blame] | 55 | const int ret = busdev->ops->ops_i2c_bus->transfer(busdev, &msg, 1); |
Nico Huber | 5817386 | 2017-08-01 17:09:35 +0200 | [diff] [blame] | 56 | if (ret) |
| 57 | return ret; |
| 58 | else |
| 59 | return val; |
| 60 | } else if (busdev->ops->ops_smbus_bus->recv_byte) { |
| 61 | return busdev->ops->ops_smbus_bus->recv_byte(dev); |
Nico Huber | 5817386 | 2017-08-01 17:09:35 +0200 | [diff] [blame] | 62 | } |
Frans Hendriks | a9caa50 | 2021-02-01 11:44:37 +0100 | [diff] [blame] | 63 | |
| 64 | printk(BIOS_ERR, "%s Missing ops_smbus_bus->recv_byte", dev_path(busdev)); |
| 65 | return -1; |
Nico Huber | 5817386 | 2017-08-01 17:09:35 +0200 | [diff] [blame] | 66 | } |
| 67 | |
Aaron Durbin | 439cee9 | 2018-01-22 21:24:35 -0700 | [diff] [blame] | 68 | int i2c_dev_writeb(struct device *const dev, uint8_t val) |
Nico Huber | 5817386 | 2017-08-01 17:09:35 +0200 | [diff] [blame] | 69 | { |
| 70 | struct device *const busdev = i2c_busdev(dev); |
| 71 | if (!busdev) |
| 72 | return -1; |
| 73 | |
| 74 | if (busdev->ops->ops_i2c_bus) { |
| 75 | const struct i2c_msg msg = { |
| 76 | .flags = 0, |
| 77 | .slave = dev->path.i2c.device, |
| 78 | .buf = &val, |
| 79 | .len = sizeof(val), |
| 80 | }; |
| 81 | return busdev->ops->ops_i2c_bus->transfer(busdev, &msg, 1); |
| 82 | } else if (busdev->ops->ops_smbus_bus->send_byte) { |
| 83 | return busdev->ops->ops_smbus_bus->send_byte(dev, val); |
Nico Huber | 5817386 | 2017-08-01 17:09:35 +0200 | [diff] [blame] | 84 | } |
Frans Hendriks | a9caa50 | 2021-02-01 11:44:37 +0100 | [diff] [blame] | 85 | |
Frans Hendriks | 68dc369 | 2021-02-01 11:52:51 +0100 | [diff] [blame] | 86 | printk(BIOS_ERR, "%s Missing ops_smbus_bus->send_byte", dev_path(busdev)); |
Frans Hendriks | a9caa50 | 2021-02-01 11:44:37 +0100 | [diff] [blame] | 87 | return -1; |
Nico Huber | 5817386 | 2017-08-01 17:09:35 +0200 | [diff] [blame] | 88 | } |
| 89 | |
Aaron Durbin | 439cee9 | 2018-01-22 21:24:35 -0700 | [diff] [blame] | 90 | int i2c_dev_readb_at(struct device *const dev, uint8_t off) |
Nico Huber | 5817386 | 2017-08-01 17:09:35 +0200 | [diff] [blame] | 91 | { |
| 92 | struct device *const busdev = i2c_busdev(dev); |
| 93 | if (!busdev) |
| 94 | return -1; |
| 95 | |
| 96 | if (busdev->ops->ops_i2c_bus) { |
| 97 | uint8_t val; |
| 98 | const struct i2c_msg msg[] = { |
| 99 | { |
| 100 | .flags = 0, |
| 101 | .slave = dev->path.i2c.device, |
| 102 | .buf = &off, |
| 103 | .len = sizeof(off), |
| 104 | }, |
| 105 | { |
| 106 | .flags = I2C_M_RD, |
| 107 | .slave = dev->path.i2c.device, |
| 108 | .buf = &val, |
| 109 | .len = sizeof(val), |
| 110 | }, |
| 111 | }; |
| 112 | |
Frans Hendriks | a9caa50 | 2021-02-01 11:44:37 +0100 | [diff] [blame] | 113 | const int ret = busdev->ops->ops_i2c_bus->transfer(busdev, msg, |
| 114 | ARRAY_SIZE(msg)); |
Nico Huber | 5817386 | 2017-08-01 17:09:35 +0200 | [diff] [blame] | 115 | if (ret) |
| 116 | return ret; |
| 117 | else |
| 118 | return val; |
| 119 | } else if (busdev->ops->ops_smbus_bus->read_byte) { |
| 120 | return busdev->ops->ops_smbus_bus->read_byte(dev, off); |
Nico Huber | 5817386 | 2017-08-01 17:09:35 +0200 | [diff] [blame] | 121 | } |
Frans Hendriks | a9caa50 | 2021-02-01 11:44:37 +0100 | [diff] [blame] | 122 | |
| 123 | printk(BIOS_ERR, "%s Missing ops_smbus_bus->read_byte", dev_path(busdev)); |
| 124 | return -1; |
Nico Huber | 5817386 | 2017-08-01 17:09:35 +0200 | [diff] [blame] | 125 | } |
| 126 | |
Frans Hendriks | a9caa50 | 2021-02-01 11:44:37 +0100 | [diff] [blame] | 127 | int i2c_dev_writeb_at(struct device *const dev, const uint8_t off, const uint8_t val) |
Nico Huber | 5817386 | 2017-08-01 17:09:35 +0200 | [diff] [blame] | 128 | { |
| 129 | struct device *const busdev = i2c_busdev(dev); |
| 130 | if (!busdev) |
| 131 | return -1; |
| 132 | |
| 133 | if (busdev->ops->ops_i2c_bus) { |
| 134 | uint8_t buf[] = { off, val }; |
| 135 | const struct i2c_msg msg = { |
| 136 | .flags = 0, |
| 137 | .slave = dev->path.i2c.device, |
| 138 | .buf = buf, |
| 139 | .len = sizeof(buf), |
| 140 | }; |
| 141 | return busdev->ops->ops_i2c_bus->transfer(busdev, &msg, 1); |
| 142 | } else if (busdev->ops->ops_smbus_bus->write_byte) { |
| 143 | return busdev->ops->ops_smbus_bus->write_byte(dev, off, val); |
Nico Huber | 5817386 | 2017-08-01 17:09:35 +0200 | [diff] [blame] | 144 | } |
Frans Hendriks | a9caa50 | 2021-02-01 11:44:37 +0100 | [diff] [blame] | 145 | |
Frans Hendriks | 68dc369 | 2021-02-01 11:52:51 +0100 | [diff] [blame] | 146 | printk(BIOS_ERR, "%s Missing ops_smbus_bus->write_byte", dev_path(busdev)); |
Frans Hendriks | a9caa50 | 2021-02-01 11:44:37 +0100 | [diff] [blame] | 147 | return -1; |
Nico Huber | 5817386 | 2017-08-01 17:09:35 +0200 | [diff] [blame] | 148 | } |
Nico Huber | 9734af6 | 2017-11-01 13:15:33 +0100 | [diff] [blame] | 149 | |
Frans Hendriks | a9caa50 | 2021-02-01 11:44:37 +0100 | [diff] [blame] | 150 | int i2c_dev_read_at16(struct device *const dev, uint8_t *const buf, const size_t len, |
| 151 | uint16_t off) |
Nico Huber | 9734af6 | 2017-11-01 13:15:33 +0100 | [diff] [blame] | 152 | { |
| 153 | struct device *const busdev = i2c_busdev(dev); |
| 154 | if (!busdev) |
| 155 | return -1; |
| 156 | |
| 157 | if (busdev->ops->ops_i2c_bus) { |
| 158 | const struct i2c_msg msg[] = { |
| 159 | { |
| 160 | .flags = 0, |
| 161 | .slave = dev->path.i2c.device, |
| 162 | .buf = (uint8_t *)&off, |
| 163 | .len = sizeof(off), |
| 164 | }, |
| 165 | { |
| 166 | .flags = I2C_M_RD, |
| 167 | .slave = dev->path.i2c.device, |
| 168 | .buf = buf, |
| 169 | .len = len, |
| 170 | }, |
| 171 | }; |
| 172 | |
| 173 | write_be16(&off, off); |
Frans Hendriks | a9caa50 | 2021-02-01 11:44:37 +0100 | [diff] [blame] | 174 | const int ret = busdev->ops->ops_i2c_bus->transfer(busdev, msg, |
| 175 | ARRAY_SIZE(msg)); |
Nico Huber | 9734af6 | 2017-11-01 13:15:33 +0100 | [diff] [blame] | 176 | if (ret) |
| 177 | return ret; |
| 178 | else |
| 179 | return len; |
| 180 | } else { |
| 181 | printk(BIOS_ERR, "%s Missing ops_i2c_bus->transfer", dev_path(busdev)); |
| 182 | return -1; |
| 183 | } |
| 184 | } |