blob: b3ade88113dbc35dbb32dd3babd3fc6aa01c4e3d [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass73845352015-01-12 18:02:08 -07002/*
3 * Copyright (c) 2014 Google, Inc
Simon Glass73845352015-01-12 18:02:08 -07004 */
5
6#include <common.h>
7#include <dm.h>
8#include <errno.h>
9#include <i2c.h>
10
Vignesh R95def3c2016-07-25 16:26:45 +053011static int cur_busnum __attribute__((section(".data")));
Simon Glass73845352015-01-12 18:02:08 -070012
13static 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 Marczak18a7f6a2015-01-27 13:36:28 +010019 ret = i2c_get_chip_for_busnum(cur_busnum, chip_addr, alen, devp);
Simon Glass73845352015-01-12 18:02:08 -070020 if (ret)
21 return ret;
Simon Glasse6f66ec2015-01-25 08:27:13 -070022 chip = dev_get_parent_platdata(*devp);
Simon Glass73845352015-01-12 18:02:08 -070023 if (chip->offset_len != alen) {
Simon Glassd744d562015-01-26 20:29:39 -070024 printf("I2C chip %x: requested alen %d does not match chip offset_len %d\n",
25 chip_addr, alen, chip->offset_len);
Simon Glass73845352015-01-12 18:02:08 -070026 return -EADDRNOTAVAIL;
27 }
28
29 return 0;
30}
31
32int 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
49int 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
62int 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
75int 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
87unsigned int i2c_get_bus_num(void)
88{
89 return cur_busnum;
90}
91
92int i2c_set_bus_num(unsigned int bus)
93{
94 cur_busnum = bus;
95
96 return 0;
97}
Simon Glassd744d562015-01-26 20:29:39 -070098
99void i2c_init(int speed, int slaveaddr)
100{
101 /* Nothing to do here - the init happens through driver model */
102}
103
104void board_i2c_init(const void *blob)
105{
106 /* Nothing to do here - the init happens through driver model */
107}
Simon Glassa2879762015-05-16 15:01:41 -0600108
109uint8_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
120void 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}