Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 0275eb1 | 2015-01-12 18:02:08 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2014 Google, Inc |
Simon Glass | 0275eb1 | 2015-01-12 18:02:08 -0700 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <common.h> |
| 7 | #include <dm.h> |
| 8 | #include <errno.h> |
| 9 | #include <i2c.h> |
| 10 | |
Vignesh R | c344b6a | 2016-07-25 16:26:45 +0530 | [diff] [blame] | 11 | static int cur_busnum __attribute__((section(".data"))); |
Simon Glass | 0275eb1 | 2015-01-12 18:02:08 -0700 | [diff] [blame] | 12 | |
| 13 | static int i2c_compat_get_device(uint chip_addr, int alen, |
| 14 | struct udevice **devp) |
| 15 | { |
| 16 | struct dm_i2c_chip *chip; |
| 17 | int ret; |
| 18 | |
Przemyslaw Marczak | 7d73708 | 2015-01-27 13:36:28 +0100 | [diff] [blame] | 19 | ret = i2c_get_chip_for_busnum(cur_busnum, chip_addr, alen, devp); |
Simon Glass | 0275eb1 | 2015-01-12 18:02:08 -0700 | [diff] [blame] | 20 | if (ret) |
| 21 | return ret; |
Simon Glass | 713c3f0 | 2015-01-25 08:27:13 -0700 | [diff] [blame] | 22 | chip = dev_get_parent_platdata(*devp); |
Simon Glass | 0275eb1 | 2015-01-12 18:02:08 -0700 | [diff] [blame] | 23 | if (chip->offset_len != alen) { |
Simon Glass | 4e003c5 | 2015-01-26 20:29:39 -0700 | [diff] [blame] | 24 | printf("I2C chip %x: requested alen %d does not match chip offset_len %d\n", |
| 25 | chip_addr, alen, chip->offset_len); |
Simon Glass | 0275eb1 | 2015-01-12 18:02:08 -0700 | [diff] [blame] | 26 | return -EADDRNOTAVAIL; |
| 27 | } |
| 28 | |
| 29 | return 0; |
| 30 | } |
| 31 | |
| 32 | int i2c_probe(uint8_t chip_addr) |
| 33 | { |
| 34 | struct udevice *bus, *dev; |
| 35 | int ret; |
| 36 | |
| 37 | ret = uclass_get_device_by_seq(UCLASS_I2C, cur_busnum, &bus); |
| 38 | if (ret) { |
| 39 | debug("Cannot find I2C bus %d: err=%d\n", cur_busnum, ret); |
| 40 | return ret; |
| 41 | } |
| 42 | |
| 43 | if (!bus) |
| 44 | return -ENOENT; |
| 45 | |
| 46 | return dm_i2c_probe(bus, chip_addr, 0, &dev); |
| 47 | } |
| 48 | |
| 49 | int i2c_read(uint8_t chip_addr, unsigned int addr, int alen, uint8_t *buffer, |
| 50 | int len) |
| 51 | { |
| 52 | struct udevice *dev; |
| 53 | int ret; |
| 54 | |
| 55 | ret = i2c_compat_get_device(chip_addr, alen, &dev); |
| 56 | if (ret) |
| 57 | return ret; |
| 58 | |
| 59 | return dm_i2c_read(dev, addr, buffer, len); |
| 60 | } |
| 61 | |
| 62 | int i2c_write(uint8_t chip_addr, unsigned int addr, int alen, uint8_t *buffer, |
| 63 | int len) |
| 64 | { |
| 65 | struct udevice *dev; |
| 66 | int ret; |
| 67 | |
| 68 | ret = i2c_compat_get_device(chip_addr, alen, &dev); |
| 69 | if (ret) |
| 70 | return ret; |
| 71 | |
| 72 | return dm_i2c_write(dev, addr, buffer, len); |
| 73 | } |
| 74 | |
| 75 | int i2c_get_bus_num_fdt(int node) |
| 76 | { |
| 77 | struct udevice *bus; |
| 78 | int ret; |
| 79 | |
| 80 | ret = uclass_get_device_by_of_offset(UCLASS_I2C, node, &bus); |
| 81 | if (ret) |
| 82 | return ret; |
| 83 | |
| 84 | return bus->seq; |
| 85 | } |
| 86 | |
| 87 | unsigned int i2c_get_bus_num(void) |
| 88 | { |
| 89 | return cur_busnum; |
| 90 | } |
| 91 | |
| 92 | int i2c_set_bus_num(unsigned int bus) |
| 93 | { |
| 94 | cur_busnum = bus; |
| 95 | |
| 96 | return 0; |
| 97 | } |
Simon Glass | 4e003c5 | 2015-01-26 20:29:39 -0700 | [diff] [blame] | 98 | |
| 99 | void i2c_init(int speed, int slaveaddr) |
| 100 | { |
| 101 | /* Nothing to do here - the init happens through driver model */ |
| 102 | } |
| 103 | |
| 104 | void board_i2c_init(const void *blob) |
| 105 | { |
| 106 | /* Nothing to do here - the init happens through driver model */ |
| 107 | } |
Simon Glass | 8a18dd8 | 2015-05-16 15:01:41 -0600 | [diff] [blame] | 108 | |
| 109 | uint8_t i2c_reg_read(uint8_t chip_addr, uint8_t offset) |
| 110 | { |
| 111 | struct udevice *dev; |
| 112 | int ret; |
| 113 | |
| 114 | ret = i2c_compat_get_device(chip_addr, 1, &dev); |
| 115 | if (ret) |
| 116 | return 0xff; |
| 117 | return dm_i2c_reg_read(dev, offset); |
| 118 | } |
| 119 | |
| 120 | void i2c_reg_write(uint8_t chip_addr, uint8_t offset, uint8_t val) |
| 121 | { |
| 122 | struct udevice *dev; |
| 123 | int ret; |
| 124 | |
| 125 | ret = i2c_compat_get_device(chip_addr, 1, &dev); |
| 126 | if (!ret) |
| 127 | dm_i2c_reg_write(dev, offset, val); |
| 128 | } |