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 struct i2c_eeprom_drv_data {
16 u32 size; /* size in bytes */
17 u32 pagewidth; /* pagesize = 2^pagewidth */
20 int i2c_eeprom_read(struct udevice *dev, int offset, uint8_t *buf, int size)
22 const struct i2c_eeprom_ops *ops = device_get_ops(dev);
27 return ops->read(dev, offset, buf, size);
30 int i2c_eeprom_write(struct udevice *dev, int offset, uint8_t *buf, int size)
32 const struct i2c_eeprom_ops *ops = device_get_ops(dev);
37 return ops->write(dev, offset, buf, size);
40 int i2c_eeprom_size(struct udevice *dev)
42 const struct i2c_eeprom_ops *ops = device_get_ops(dev);
47 return ops->size(dev);
50 static int i2c_eeprom_std_read(struct udevice *dev, int offset, uint8_t *buf,
53 return dm_i2c_read(dev, offset, buf, size);
56 static int i2c_eeprom_std_write(struct udevice *dev, int offset,
57 const uint8_t *buf, int size)
59 struct i2c_eeprom *priv = dev_get_priv(dev);
63 int write_size = min_t(int, size, priv->pagesize);
65 ret = dm_i2c_write(dev, offset, buf, write_size);
79 static int i2c_eeprom_std_size(struct udevice *dev)
81 struct i2c_eeprom *priv = dev_get_priv(dev);
86 static const struct i2c_eeprom_ops i2c_eeprom_std_ops = {
87 .read = i2c_eeprom_std_read,
88 .write = i2c_eeprom_std_write,
89 .size = i2c_eeprom_std_size,
92 static int i2c_eeprom_std_ofdata_to_platdata(struct udevice *dev)
94 struct i2c_eeprom *priv = dev_get_priv(dev);
95 struct i2c_eeprom_drv_data *data =
96 (struct i2c_eeprom_drv_data *)dev_get_driver_data(dev);
100 if (dev_read_u32(dev, "pagesize", &pagesize) == 0) {
101 priv->pagesize = pagesize;
103 /* 6 bit -> page size of up to 2^63 (should be sufficient) */
104 priv->pagewidth = data->pagewidth;
105 priv->pagesize = (1 << priv->pagewidth);
108 if (dev_read_u32(dev, "size", &size) == 0)
111 priv->size = data->size;
116 static int i2c_eeprom_std_bind(struct udevice *dev)
118 ofnode partitions = ofnode_find_subnode(dev_ofnode(dev), "partitions");
122 if (!ofnode_valid(partitions))
124 if (!ofnode_device_is_compatible(partitions, "fixed-partitions"))
127 ofnode_for_each_subnode(partition, partitions) {
128 name = ofnode_get_name(partition);
132 device_bind_ofnode(dev, DM_GET_DRIVER(i2c_eeprom_partition),
133 name, NULL, partition, NULL);
139 static int i2c_eeprom_std_probe(struct udevice *dev)
144 /* Verify that the chip is functional */
145 ret = i2c_eeprom_read(dev, 0, &test_byte, 1);
152 static const struct i2c_eeprom_drv_data eeprom_data = {
157 static const struct i2c_eeprom_drv_data mc24aa02e48_data = {
162 static const struct i2c_eeprom_drv_data atmel24c01a_data = {
167 static const struct i2c_eeprom_drv_data atmel24c02_data = {
172 static const struct i2c_eeprom_drv_data atmel24c04_data = {
177 static const struct i2c_eeprom_drv_data atmel24c08_data = {
182 static const struct i2c_eeprom_drv_data atmel24c08a_data = {
187 static const struct i2c_eeprom_drv_data atmel24c16a_data = {
192 static const struct i2c_eeprom_drv_data atmel24mac402_data = {
197 static const struct i2c_eeprom_drv_data atmel24c32_data = {
202 static const struct i2c_eeprom_drv_data atmel24c64_data = {
207 static const struct i2c_eeprom_drv_data atmel24c128_data = {
212 static const struct i2c_eeprom_drv_data atmel24c256_data = {
217 static const struct i2c_eeprom_drv_data atmel24c512_data = {
222 static const struct udevice_id i2c_eeprom_std_ids[] = {
223 { .compatible = "i2c-eeprom", (ulong)&eeprom_data },
224 { .compatible = "microchip,24aa02e48", (ulong)&mc24aa02e48_data },
225 { .compatible = "atmel,24c01a", (ulong)&atmel24c01a_data },
226 { .compatible = "atmel,24c02", (ulong)&atmel24c02_data },
227 { .compatible = "atmel,24c04", (ulong)&atmel24c04_data },
228 { .compatible = "atmel,24c08", (ulong)&atmel24c08_data },
229 { .compatible = "atmel,24c08a", (ulong)&atmel24c08a_data },
230 { .compatible = "atmel,24c16a", (ulong)&atmel24c16a_data },
231 { .compatible = "atmel,24mac402", (ulong)&atmel24mac402_data },
232 { .compatible = "atmel,24c32", (ulong)&atmel24c32_data },
233 { .compatible = "atmel,24c64", (ulong)&atmel24c64_data },
234 { .compatible = "atmel,24c128", (ulong)&atmel24c128_data },
235 { .compatible = "atmel,24c256", (ulong)&atmel24c256_data },
236 { .compatible = "atmel,24c512", (ulong)&atmel24c512_data },
240 U_BOOT_DRIVER(i2c_eeprom_std) = {
241 .name = "i2c_eeprom",
242 .id = UCLASS_I2C_EEPROM,
243 .of_match = i2c_eeprom_std_ids,
244 .bind = i2c_eeprom_std_bind,
245 .probe = i2c_eeprom_std_probe,
246 .ofdata_to_platdata = i2c_eeprom_std_ofdata_to_platdata,
247 .priv_auto_alloc_size = sizeof(struct i2c_eeprom),
248 .ops = &i2c_eeprom_std_ops,
251 struct i2c_eeprom_partition {
256 static int i2c_eeprom_partition_probe(struct udevice *dev)
261 static int i2c_eeprom_partition_ofdata_to_platdata(struct udevice *dev)
263 struct i2c_eeprom_partition *priv = dev_get_priv(dev);
267 ret = dev_read_u32(dev, "offset", &offset);
271 ret = dev_read_u32(dev, "size", &size);
275 priv->offset = offset;
281 static int i2c_eeprom_partition_read(struct udevice *dev, int offset,
284 struct i2c_eeprom_partition *priv = dev_get_priv(dev);
285 struct udevice *parent = dev_get_parent(dev);
289 if (offset + size > priv->size)
292 return i2c_eeprom_read(parent, offset + priv->offset, buf, size);
295 static int i2c_eeprom_partition_write(struct udevice *dev, int offset,
296 const u8 *buf, int size)
298 struct i2c_eeprom_partition *priv = dev_get_priv(dev);
299 struct udevice *parent = dev_get_parent(dev);
303 if (offset + size > priv->size)
306 return i2c_eeprom_write(parent, offset + priv->offset, (uint8_t *)buf,
310 static int i2c_eeprom_partition_size(struct udevice *dev)
312 struct i2c_eeprom_partition *priv = dev_get_priv(dev);
317 static const struct i2c_eeprom_ops i2c_eeprom_partition_ops = {
318 .read = i2c_eeprom_partition_read,
319 .write = i2c_eeprom_partition_write,
320 .size = i2c_eeprom_partition_size,
323 U_BOOT_DRIVER(i2c_eeprom_partition) = {
324 .name = "i2c_eeprom_partition",
325 .id = UCLASS_I2C_EEPROM,
326 .probe = i2c_eeprom_partition_probe,
327 .ofdata_to_platdata = i2c_eeprom_partition_ofdata_to_platdata,
328 .priv_auto_alloc_size = sizeof(struct i2c_eeprom_partition),
329 .ops = &i2c_eeprom_partition_ops,
332 UCLASS_DRIVER(i2c_eeprom) = {
333 .id = UCLASS_I2C_EEPROM,
334 .name = "i2c_eeprom",