Simon Glass | 6ca4ba0 | 2014-12-10 08:55:54 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2014 Google, Inc |
| 3 | * |
| 4 | * SPDX-License-Identifier: GPL-2.0+ |
| 5 | */ |
| 6 | |
| 7 | #include <common.h> |
Masahiro Yamada | f9d52cc | 2014-12-18 20:00:26 +0900 | [diff] [blame] | 8 | #include <linux/err.h> |
Simon Glass | 6ca4ba0 | 2014-12-10 08:55:54 -0700 | [diff] [blame] | 9 | #include <dm.h> |
| 10 | #include <i2c.h> |
| 11 | #include <i2c_eeprom.h> |
| 12 | |
Jonas Karlman | 8427a24 | 2017-04-22 08:57:41 +0000 | [diff] [blame] | 13 | int 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 | |
| 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) |
Simon Glass | 6ca4ba0 | 2014-12-10 08:55:54 -0700 | [diff] [blame] | 35 | { |
mario.six@gdsys.cc | 7559ac4 | 2016-06-22 15:14:16 +0200 | [diff] [blame] | 36 | return dm_i2c_read(dev, offset, buf, size); |
Simon Glass | 6ca4ba0 | 2014-12-10 08:55:54 -0700 | [diff] [blame] | 37 | } |
| 38 | |
Jonas Karlman | 8427a24 | 2017-04-22 08:57:41 +0000 | [diff] [blame] | 39 | static int i2c_eeprom_std_write(struct udevice *dev, int offset, |
| 40 | const uint8_t *buf, int size) |
Simon Glass | 6ca4ba0 | 2014-12-10 08:55:54 -0700 | [diff] [blame] | 41 | { |
| 42 | return -ENODEV; |
| 43 | } |
| 44 | |
Masahiro Yamada | a7cc93e8 | 2017-06-22 16:51:22 +0900 | [diff] [blame] | 45 | static const struct i2c_eeprom_ops i2c_eeprom_std_ops = { |
Jonas Karlman | 8427a24 | 2017-04-22 08:57:41 +0000 | [diff] [blame] | 46 | .read = i2c_eeprom_std_read, |
| 47 | .write = i2c_eeprom_std_write, |
Simon Glass | 6ca4ba0 | 2014-12-10 08:55:54 -0700 | [diff] [blame] | 48 | }; |
| 49 | |
mario.six@gdsys.cc | 7559ac4 | 2016-06-22 15:14:16 +0200 | [diff] [blame] | 50 | static int i2c_eeprom_std_ofdata_to_platdata(struct udevice *dev) |
| 51 | { |
| 52 | struct i2c_eeprom *priv = dev_get_priv(dev); |
| 53 | u64 data = dev_get_driver_data(dev); |
| 54 | |
| 55 | /* 6 bit -> page size of up to 2^63 (should be sufficient) */ |
| 56 | priv->pagewidth = data & 0x3F; |
| 57 | priv->pagesize = (1 << priv->pagewidth); |
| 58 | |
| 59 | return 0; |
| 60 | } |
| 61 | |
Masahiro Yamada | a7cc93e8 | 2017-06-22 16:51:22 +0900 | [diff] [blame] | 62 | static int i2c_eeprom_std_probe(struct udevice *dev) |
Simon Glass | 6ca4ba0 | 2014-12-10 08:55:54 -0700 | [diff] [blame] | 63 | { |
| 64 | return 0; |
| 65 | } |
| 66 | |
| 67 | static const struct udevice_id i2c_eeprom_std_ids[] = { |
mario.six@gdsys.cc | 7559ac4 | 2016-06-22 15:14:16 +0200 | [diff] [blame] | 68 | { .compatible = "i2c-eeprom", .data = 0 }, |
Wenyou Yang | 6db5042 | 2017-07-31 11:25:30 +0800 | [diff] [blame^] | 69 | { .compatible = "microchip,24aa02e48", .data = 3 }, |
mario.six@gdsys.cc | 7559ac4 | 2016-06-22 15:14:16 +0200 | [diff] [blame] | 70 | { .compatible = "atmel,24c01a", .data = 3 }, |
| 71 | { .compatible = "atmel,24c02", .data = 3 }, |
| 72 | { .compatible = "atmel,24c04", .data = 4 }, |
| 73 | { .compatible = "atmel,24c08a", .data = 4 }, |
| 74 | { .compatible = "atmel,24c16a", .data = 4 }, |
| 75 | { .compatible = "atmel,24c32", .data = 5 }, |
| 76 | { .compatible = "atmel,24c64", .data = 5 }, |
| 77 | { .compatible = "atmel,24c128", .data = 6 }, |
| 78 | { .compatible = "atmel,24c256", .data = 6 }, |
| 79 | { .compatible = "atmel,24c512", .data = 6 }, |
Simon Glass | 6ca4ba0 | 2014-12-10 08:55:54 -0700 | [diff] [blame] | 80 | { } |
| 81 | }; |
| 82 | |
| 83 | U_BOOT_DRIVER(i2c_eeprom_std) = { |
mario.six@gdsys.cc | 7559ac4 | 2016-06-22 15:14:16 +0200 | [diff] [blame] | 84 | .name = "i2c_eeprom", |
| 85 | .id = UCLASS_I2C_EEPROM, |
| 86 | .of_match = i2c_eeprom_std_ids, |
| 87 | .probe = i2c_eeprom_std_probe, |
| 88 | .ofdata_to_platdata = i2c_eeprom_std_ofdata_to_platdata, |
| 89 | .priv_auto_alloc_size = sizeof(struct i2c_eeprom), |
| 90 | .ops = &i2c_eeprom_std_ops, |
Simon Glass | 6ca4ba0 | 2014-12-10 08:55:54 -0700 | [diff] [blame] | 91 | }; |
| 92 | |
| 93 | UCLASS_DRIVER(i2c_eeprom) = { |
| 94 | .id = UCLASS_I2C_EEPROM, |
| 95 | .name = "i2c_eeprom", |
| 96 | }; |