1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2014 Google, Inc
9 #include <linux/kernel.h>
11 #include <dm/device-internal.h>
13 #include <i2c_eeprom.h>
15 int i2c_eeprom_read(struct udevice *dev, int offset, uint8_t *buf, int size)
17 const struct i2c_eeprom_ops *ops = device_get_ops(dev);
22 return ops->read(dev, offset, buf, size);
25 int i2c_eeprom_write(struct udevice *dev, int offset, uint8_t *buf, int size)
27 const struct i2c_eeprom_ops *ops = device_get_ops(dev);
32 return ops->write(dev, offset, buf, size);
35 static int i2c_eeprom_std_read(struct udevice *dev, int offset, uint8_t *buf,
38 return dm_i2c_read(dev, offset, buf, size);
41 static int i2c_eeprom_std_write(struct udevice *dev, int offset,
42 const uint8_t *buf, int size)
44 struct i2c_eeprom *priv = dev_get_priv(dev);
48 int write_size = min_t(int, size, priv->pagesize);
50 ret = dm_i2c_write(dev, offset, buf, write_size);
64 static const struct i2c_eeprom_ops i2c_eeprom_std_ops = {
65 .read = i2c_eeprom_std_read,
66 .write = i2c_eeprom_std_write,
69 static int i2c_eeprom_std_ofdata_to_platdata(struct udevice *dev)
71 struct i2c_eeprom *priv = dev_get_priv(dev);
72 u64 data = dev_get_driver_data(dev);
75 if (dev_read_u32(dev, "pagesize", &pagesize) == 0) {
76 priv->pagesize = pagesize;
80 /* 6 bit -> page size of up to 2^63 (should be sufficient) */
81 priv->pagewidth = data & 0x3F;
82 priv->pagesize = (1 << priv->pagewidth);
87 static int i2c_eeprom_std_bind(struct udevice *dev)
89 ofnode partitions = ofnode_find_subnode(dev_ofnode(dev), "partitions");
93 if (!ofnode_valid(partitions))
95 if (!ofnode_device_is_compatible(partitions, "fixed-partitions"))
98 ofnode_for_each_subnode(partition, partitions) {
99 name = ofnode_get_name(partition);
103 device_bind_ofnode(dev, DM_GET_DRIVER(i2c_eeprom_partition),
104 name, NULL, partition, NULL);
110 static int i2c_eeprom_std_probe(struct udevice *dev)
115 /* Verify that the chip is functional */
116 ret = i2c_eeprom_read(dev, 0, &test_byte, 1);
123 static const struct udevice_id i2c_eeprom_std_ids[] = {
124 { .compatible = "i2c-eeprom", .data = 0 },
125 { .compatible = "microchip,24aa02e48", .data = 3 },
126 { .compatible = "atmel,24c01a", .data = 3 },
127 { .compatible = "atmel,24c02", .data = 3 },
128 { .compatible = "atmel,24c04", .data = 4 },
129 { .compatible = "atmel,24c08", .data = 4 },
130 { .compatible = "atmel,24c08a", .data = 4 },
131 { .compatible = "atmel,24c16a", .data = 4 },
132 { .compatible = "atmel,24mac402", .data = 4 },
133 { .compatible = "atmel,24c32", .data = 5 },
134 { .compatible = "atmel,24c64", .data = 5 },
135 { .compatible = "atmel,24c128", .data = 6 },
136 { .compatible = "atmel,24c256", .data = 6 },
137 { .compatible = "atmel,24c512", .data = 6 },
141 U_BOOT_DRIVER(i2c_eeprom_std) = {
142 .name = "i2c_eeprom",
143 .id = UCLASS_I2C_EEPROM,
144 .of_match = i2c_eeprom_std_ids,
145 .bind = i2c_eeprom_std_bind,
146 .probe = i2c_eeprom_std_probe,
147 .ofdata_to_platdata = i2c_eeprom_std_ofdata_to_platdata,
148 .priv_auto_alloc_size = sizeof(struct i2c_eeprom),
149 .ops = &i2c_eeprom_std_ops,
152 struct i2c_eeprom_partition {
157 static int i2c_eeprom_partition_probe(struct udevice *dev)
162 static int i2c_eeprom_partition_ofdata_to_platdata(struct udevice *dev)
164 struct i2c_eeprom_partition *priv = dev_get_priv(dev);
168 ret = dev_read_u32(dev, "offset", &offset);
172 ret = dev_read_u32(dev, "size", &size);
176 priv->offset = offset;
182 static int i2c_eeprom_partition_read(struct udevice *dev, int offset,
185 struct i2c_eeprom_partition *priv = dev_get_priv(dev);
186 struct udevice *parent = dev_get_parent(dev);
190 if (offset + size > priv->size)
193 return i2c_eeprom_read(parent, offset + priv->offset, buf, size);
196 static int i2c_eeprom_partition_write(struct udevice *dev, int offset,
197 const u8 *buf, int size)
199 struct i2c_eeprom_partition *priv = dev_get_priv(dev);
200 struct udevice *parent = dev_get_parent(dev);
204 if (offset + size > priv->size)
207 return i2c_eeprom_write(parent, offset + priv->offset, (uint8_t *)buf,
211 static const struct i2c_eeprom_ops i2c_eeprom_partition_ops = {
212 .read = i2c_eeprom_partition_read,
213 .write = i2c_eeprom_partition_write,
216 U_BOOT_DRIVER(i2c_eeprom_partition) = {
217 .name = "i2c_eeprom_partition",
218 .id = UCLASS_I2C_EEPROM,
219 .probe = i2c_eeprom_partition_probe,
220 .ofdata_to_platdata = i2c_eeprom_partition_ofdata_to_platdata,
221 .priv_auto_alloc_size = sizeof(struct i2c_eeprom_partition),
222 .ops = &i2c_eeprom_partition_ops,
225 UCLASS_DRIVER(i2c_eeprom) = {
226 .id = UCLASS_I2C_EEPROM,
227 .name = "i2c_eeprom",