blob: f25d0540075d55db29ba8eb256034146fbcd24a7 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass6ca4ba02014-12-10 08:55:54 -07002/*
3 * Copyright (c) 2014 Google, Inc
Simon Glass6ca4ba02014-12-10 08:55:54 -07004 */
5
6#include <common.h>
Masahiro Yamadaf9d52cc2014-12-18 20:00:26 +09007#include <linux/err.h>
Baruch Siach4e4c5ed2019-04-07 12:38:49 +03008#include <linux/kernel.h>
Simon Glass6ca4ba02014-12-10 08:55:54 -07009#include <dm.h>
10#include <i2c.h>
11#include <i2c_eeprom.h>
12
Jonas Karlman8427a242017-04-22 08:57:41 +000013int i2c_eeprom_read(struct udevice *dev, int offset, uint8_t *buf, int size)
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
23int 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
33static int i2c_eeprom_std_read(struct udevice *dev, int offset, uint8_t *buf,
34 int size)
Simon Glass6ca4ba02014-12-10 08:55:54 -070035{
mario.six@gdsys.cc7559ac42016-06-22 15:14:16 +020036 return dm_i2c_read(dev, offset, buf, size);
Simon Glass6ca4ba02014-12-10 08:55:54 -070037}
38
Jonas Karlman8427a242017-04-22 08:57:41 +000039static int i2c_eeprom_std_write(struct udevice *dev, int offset,
40 const uint8_t *buf, int size)
Simon Glass6ca4ba02014-12-10 08:55:54 -070041{
Baruch Siach4e4c5ed2019-04-07 12:38:49 +030042 struct i2c_eeprom *priv = dev_get_priv(dev);
43 int ret;
44
45 while (size > 0) {
46 int write_size = min_t(int, size, priv->pagesize);
47
48 ret = dm_i2c_write(dev, offset, buf, write_size);
49 if (ret)
50 return ret;
51
52 offset += write_size;
53 buf += write_size;
54 size -= write_size;
55
56 udelay(10000);
57 }
58
59 return 0;
Simon Glass6ca4ba02014-12-10 08:55:54 -070060}
61
Masahiro Yamadaa7cc93e82017-06-22 16:51:22 +090062static const struct i2c_eeprom_ops i2c_eeprom_std_ops = {
Jonas Karlman8427a242017-04-22 08:57:41 +000063 .read = i2c_eeprom_std_read,
64 .write = i2c_eeprom_std_write,
Simon Glass6ca4ba02014-12-10 08:55:54 -070065};
66
mario.six@gdsys.cc7559ac42016-06-22 15:14:16 +020067static int i2c_eeprom_std_ofdata_to_platdata(struct udevice *dev)
68{
69 struct i2c_eeprom *priv = dev_get_priv(dev);
70 u64 data = dev_get_driver_data(dev);
Baruch Siach263b5b02019-04-07 12:38:48 +030071 u32 pagesize;
72
73 if (dev_read_u32(dev, "pagesize", &pagesize) == 0) {
74 priv->pagesize = pagesize;
75 return 0;
76 }
mario.six@gdsys.cc7559ac42016-06-22 15:14:16 +020077
78 /* 6 bit -> page size of up to 2^63 (should be sufficient) */
79 priv->pagewidth = data & 0x3F;
80 priv->pagesize = (1 << priv->pagewidth);
81
82 return 0;
83}
84
Masahiro Yamadaa7cc93e82017-06-22 16:51:22 +090085static int i2c_eeprom_std_probe(struct udevice *dev)
Simon Glass6ca4ba02014-12-10 08:55:54 -070086{
87 return 0;
88}
89
90static const struct udevice_id i2c_eeprom_std_ids[] = {
mario.six@gdsys.cc7559ac42016-06-22 15:14:16 +020091 { .compatible = "i2c-eeprom", .data = 0 },
Wenyou Yang6db50422017-07-31 11:25:30 +080092 { .compatible = "microchip,24aa02e48", .data = 3 },
mario.six@gdsys.cc7559ac42016-06-22 15:14:16 +020093 { .compatible = "atmel,24c01a", .data = 3 },
94 { .compatible = "atmel,24c02", .data = 3 },
95 { .compatible = "atmel,24c04", .data = 4 },
Michal Simek191d02e2019-01-21 13:54:57 +010096 { .compatible = "atmel,24c08", .data = 4 },
mario.six@gdsys.cc7559ac42016-06-22 15:14:16 +020097 { .compatible = "atmel,24c08a", .data = 4 },
98 { .compatible = "atmel,24c16a", .data = 4 },
Wenyou Yang911a6102017-07-31 11:25:31 +080099 { .compatible = "atmel,24mac402", .data = 4 },
mario.six@gdsys.cc7559ac42016-06-22 15:14:16 +0200100 { .compatible = "atmel,24c32", .data = 5 },
101 { .compatible = "atmel,24c64", .data = 5 },
102 { .compatible = "atmel,24c128", .data = 6 },
103 { .compatible = "atmel,24c256", .data = 6 },
104 { .compatible = "atmel,24c512", .data = 6 },
Simon Glass6ca4ba02014-12-10 08:55:54 -0700105 { }
106};
107
108U_BOOT_DRIVER(i2c_eeprom_std) = {
mario.six@gdsys.cc7559ac42016-06-22 15:14:16 +0200109 .name = "i2c_eeprom",
110 .id = UCLASS_I2C_EEPROM,
111 .of_match = i2c_eeprom_std_ids,
112 .probe = i2c_eeprom_std_probe,
113 .ofdata_to_platdata = i2c_eeprom_std_ofdata_to_platdata,
114 .priv_auto_alloc_size = sizeof(struct i2c_eeprom),
115 .ops = &i2c_eeprom_std_ops,
Simon Glass6ca4ba02014-12-10 08:55:54 -0700116};
117
118UCLASS_DRIVER(i2c_eeprom) = {
119 .id = UCLASS_I2C_EEPROM,
120 .name = "i2c_eeprom",
121};