Commit 8880efbd1ffd54bcddb427229ff925151a054257

Authored by Jonas Karlman
Committed by Simon Glass
1 parent 26e2e404b7

i2c_eeprom: add read and write functions

Signed-off-by: Jonas Karlman <jonas@kwiboo.se>
Reviewed-by: Simon Glass <sjg@chromium.org>

Showing 2 changed files with 50 additions and 6 deletions Inline Diff

drivers/misc/i2c_eeprom.c
1 /* 1 /*
2 * Copyright (c) 2014 Google, Inc 2 * Copyright (c) 2014 Google, Inc
3 * 3 *
4 * SPDX-License-Identifier: GPL-2.0+ 4 * SPDX-License-Identifier: GPL-2.0+
5 */ 5 */
6 6
7 #include <common.h> 7 #include <common.h>
8 #include <linux/err.h> 8 #include <linux/err.h>
9 #include <dm.h> 9 #include <dm.h>
10 #include <i2c.h> 10 #include <i2c.h>
11 #include <i2c_eeprom.h> 11 #include <i2c_eeprom.h>
12 12
13 static int i2c_eeprom_read(struct udevice *dev, int offset, uint8_t *buf, 13 int i2c_eeprom_read(struct udevice *dev, int offset, uint8_t *buf, int size)
14 int size)
15 { 14 {
15 const struct i2c_eeprom_ops *ops = device_get_ops(dev);
16
17 if (!ops->read)
18 return -ENOSYS;
19
20 return ops->read(dev, offset, buf, size);
21 }
22
23 int i2c_eeprom_write(struct udevice *dev, int offset, uint8_t *buf, int size)
24 {
25 const struct i2c_eeprom_ops *ops = device_get_ops(dev);
26
27 if (!ops->write)
28 return -ENOSYS;
29
30 return ops->write(dev, offset, buf, size);
31 }
32
33 static int i2c_eeprom_std_read(struct udevice *dev, int offset, uint8_t *buf,
34 int size)
35 {
16 return dm_i2c_read(dev, offset, buf, size); 36 return dm_i2c_read(dev, offset, buf, size);
17 } 37 }
18 38
19 static int i2c_eeprom_write(struct udevice *dev, int offset, 39 static int i2c_eeprom_std_write(struct udevice *dev, int offset,
20 const uint8_t *buf, int size) 40 const uint8_t *buf, int size)
21 { 41 {
22 return -ENODEV; 42 return -ENODEV;
23 } 43 }
24 44
25 struct i2c_eeprom_ops i2c_eeprom_std_ops = { 45 struct i2c_eeprom_ops i2c_eeprom_std_ops = {
26 .read = i2c_eeprom_read, 46 .read = i2c_eeprom_std_read,
27 .write = i2c_eeprom_write, 47 .write = i2c_eeprom_std_write,
28 }; 48 };
29 49
30 static int i2c_eeprom_std_ofdata_to_platdata(struct udevice *dev) 50 static int i2c_eeprom_std_ofdata_to_platdata(struct udevice *dev)
31 { 51 {
32 struct i2c_eeprom *priv = dev_get_priv(dev); 52 struct i2c_eeprom *priv = dev_get_priv(dev);
33 u64 data = dev_get_driver_data(dev); 53 u64 data = dev_get_driver_data(dev);
34 54
35 /* 6 bit -> page size of up to 2^63 (should be sufficient) */ 55 /* 6 bit -> page size of up to 2^63 (should be sufficient) */
36 priv->pagewidth = data & 0x3F; 56 priv->pagewidth = data & 0x3F;
37 priv->pagesize = (1 << priv->pagewidth); 57 priv->pagesize = (1 << priv->pagewidth);
38 58
39 return 0; 59 return 0;
40 } 60 }
41 61
42 int i2c_eeprom_std_probe(struct udevice *dev) 62 int i2c_eeprom_std_probe(struct udevice *dev)
43 { 63 {
44 return 0; 64 return 0;
45 } 65 }
46 66
47 static const struct udevice_id i2c_eeprom_std_ids[] = { 67 static const struct udevice_id i2c_eeprom_std_ids[] = {
48 { .compatible = "i2c-eeprom", .data = 0 }, 68 { .compatible = "i2c-eeprom", .data = 0 },
49 { .compatible = "atmel,24c01a", .data = 3 }, 69 { .compatible = "atmel,24c01a", .data = 3 },
50 { .compatible = "atmel,24c02", .data = 3 }, 70 { .compatible = "atmel,24c02", .data = 3 },
51 { .compatible = "atmel,24c04", .data = 4 }, 71 { .compatible = "atmel,24c04", .data = 4 },
52 { .compatible = "atmel,24c08a", .data = 4 }, 72 { .compatible = "atmel,24c08a", .data = 4 },
53 { .compatible = "atmel,24c16a", .data = 4 }, 73 { .compatible = "atmel,24c16a", .data = 4 },
54 { .compatible = "atmel,24c32", .data = 5 }, 74 { .compatible = "atmel,24c32", .data = 5 },
55 { .compatible = "atmel,24c64", .data = 5 }, 75 { .compatible = "atmel,24c64", .data = 5 },
56 { .compatible = "atmel,24c128", .data = 6 }, 76 { .compatible = "atmel,24c128", .data = 6 },
57 { .compatible = "atmel,24c256", .data = 6 }, 77 { .compatible = "atmel,24c256", .data = 6 },
58 { .compatible = "atmel,24c512", .data = 6 }, 78 { .compatible = "atmel,24c512", .data = 6 },
59 { } 79 { }
60 }; 80 };
61 81
62 U_BOOT_DRIVER(i2c_eeprom_std) = { 82 U_BOOT_DRIVER(i2c_eeprom_std) = {
63 .name = "i2c_eeprom", 83 .name = "i2c_eeprom",
64 .id = UCLASS_I2C_EEPROM, 84 .id = UCLASS_I2C_EEPROM,
65 .of_match = i2c_eeprom_std_ids, 85 .of_match = i2c_eeprom_std_ids,
66 .probe = i2c_eeprom_std_probe, 86 .probe = i2c_eeprom_std_probe,
67 .ofdata_to_platdata = i2c_eeprom_std_ofdata_to_platdata, 87 .ofdata_to_platdata = i2c_eeprom_std_ofdata_to_platdata,
68 .priv_auto_alloc_size = sizeof(struct i2c_eeprom), 88 .priv_auto_alloc_size = sizeof(struct i2c_eeprom),
69 .ops = &i2c_eeprom_std_ops, 89 .ops = &i2c_eeprom_std_ops,
70 }; 90 };
71 91
72 UCLASS_DRIVER(i2c_eeprom) = { 92 UCLASS_DRIVER(i2c_eeprom) = {
73 .id = UCLASS_I2C_EEPROM, 93 .id = UCLASS_I2C_EEPROM,
74 .name = "i2c_eeprom", 94 .name = "i2c_eeprom",
75 }; 95 };
include/i2c_eeprom.h
1 /* 1 /*
2 * Copyright (c) 2014 Google, Inc 2 * Copyright (c) 2014 Google, Inc
3 * 3 *
4 * SPDX-License-Identifier: GPL-2.0+ 4 * SPDX-License-Identifier: GPL-2.0+
5 */ 5 */
6 6
7 #ifndef __I2C_EEPROM 7 #ifndef __I2C_EEPROM
8 #define __I2C_EEPROM 8 #define __I2C_EEPROM
9 9
10 struct i2c_eeprom_ops { 10 struct i2c_eeprom_ops {
11 int (*read)(struct udevice *dev, int offset, uint8_t *buf, int size); 11 int (*read)(struct udevice *dev, int offset, uint8_t *buf, int size);
12 int (*write)(struct udevice *dev, int offset, const uint8_t *buf, 12 int (*write)(struct udevice *dev, int offset, const uint8_t *buf,
13 int size); 13 int size);
14 }; 14 };
15 15
16 struct i2c_eeprom { 16 struct i2c_eeprom {
17 /* The EEPROM's page size in byte */ 17 /* The EEPROM's page size in byte */
18 unsigned long pagesize; 18 unsigned long pagesize;
19 /* The EEPROM's page width in bits (pagesize = 2^pagewidth) */ 19 /* The EEPROM's page width in bits (pagesize = 2^pagewidth) */
20 unsigned pagewidth; 20 unsigned pagewidth;
21 }; 21 };
22 22
23 /*
24 * i2c_eeprom_read() - read bytes from an I2C EEPROM chip
25 *
26 * @dev: Chip to read from
27 * @offset: Offset within chip to start reading
28 * @buf: Place to put data
29 * @size: Number of bytes to read
30 *
31 * @return 0 on success, -ve on failure
32 */
33 int i2c_eeprom_read(struct udevice *dev, int offset, uint8_t *buf, int size);
34
35 /*
36 * i2c_eeprom_write() - write bytes to an I2C EEPROM chip
37 *
38 * @dev: Chip to write to
39 * @offset: Offset within chip to start writing
40 * @buf: Buffer containing data to write
41 * @size: Number of bytes to write
42 *
43 * @return 0 on success, -ve on failure
44 */
45 int i2c_eeprom_write(struct udevice *dev, int offset, uint8_t *buf, int size);
46
23 #endif 47 #endif
24 48