1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun * Copyright (c) 2014 Google, Inc
3*4882a593Smuzhiyun *
4*4882a593Smuzhiyun * SPDX-License-Identifier: GPL-2.0+
5*4882a593Smuzhiyun */
6*4882a593Smuzhiyun
7*4882a593Smuzhiyun #include <common.h>
8*4882a593Smuzhiyun #include <dm.h>
9*4882a593Smuzhiyun #include <errno.h>
10*4882a593Smuzhiyun #include <i2c.h>
11*4882a593Smuzhiyun #include <malloc.h>
12*4882a593Smuzhiyun #include <dm/device-internal.h>
13*4882a593Smuzhiyun #include <dm/lists.h>
14*4882a593Smuzhiyun
15*4882a593Smuzhiyun DECLARE_GLOBAL_DATA_PTR;
16*4882a593Smuzhiyun
17*4882a593Smuzhiyun #define I2C_MAX_OFFSET_LEN 4
18*4882a593Smuzhiyun
19*4882a593Smuzhiyun /* Useful debugging function */
i2c_dump_msgs(struct i2c_msg * msg,int nmsgs)20*4882a593Smuzhiyun void i2c_dump_msgs(struct i2c_msg *msg, int nmsgs)
21*4882a593Smuzhiyun {
22*4882a593Smuzhiyun int i;
23*4882a593Smuzhiyun
24*4882a593Smuzhiyun for (i = 0; i < nmsgs; i++) {
25*4882a593Smuzhiyun struct i2c_msg *m = &msg[i];
26*4882a593Smuzhiyun
27*4882a593Smuzhiyun printf(" %s %x len=%x", m->flags & I2C_M_RD ? "R" : "W",
28*4882a593Smuzhiyun msg->addr, msg->len);
29*4882a593Smuzhiyun if (!(m->flags & I2C_M_RD))
30*4882a593Smuzhiyun printf(": %x", m->buf[0]);
31*4882a593Smuzhiyun printf("\n");
32*4882a593Smuzhiyun }
33*4882a593Smuzhiyun }
34*4882a593Smuzhiyun
35*4882a593Smuzhiyun /**
36*4882a593Smuzhiyun * i2c_setup_offset() - Set up a new message with a chip offset
37*4882a593Smuzhiyun *
38*4882a593Smuzhiyun * @chip: Chip to use
39*4882a593Smuzhiyun * @offset: Byte offset within chip
40*4882a593Smuzhiyun * @offset_buf: Place to put byte offset
41*4882a593Smuzhiyun * @msg: Message buffer
42*4882a593Smuzhiyun * @return 0 if OK, -EADDRNOTAVAIL if the offset length is 0. In that case the
43*4882a593Smuzhiyun * message is still set up but will not contain an offset.
44*4882a593Smuzhiyun */
i2c_setup_offset(struct dm_i2c_chip * chip,uint offset,uint8_t offset_buf[],struct i2c_msg * msg)45*4882a593Smuzhiyun static int i2c_setup_offset(struct dm_i2c_chip *chip, uint offset,
46*4882a593Smuzhiyun uint8_t offset_buf[], struct i2c_msg *msg)
47*4882a593Smuzhiyun {
48*4882a593Smuzhiyun int offset_len;
49*4882a593Smuzhiyun
50*4882a593Smuzhiyun msg->addr = chip->chip_addr;
51*4882a593Smuzhiyun msg->flags = chip->flags & DM_I2C_CHIP_10BIT ? I2C_M_TEN : 0;
52*4882a593Smuzhiyun msg->len = chip->offset_len;
53*4882a593Smuzhiyun msg->buf = offset_buf;
54*4882a593Smuzhiyun if (!chip->offset_len)
55*4882a593Smuzhiyun return -EADDRNOTAVAIL;
56*4882a593Smuzhiyun assert(chip->offset_len <= I2C_MAX_OFFSET_LEN);
57*4882a593Smuzhiyun offset_len = chip->offset_len;
58*4882a593Smuzhiyun while (offset_len--)
59*4882a593Smuzhiyun *offset_buf++ = offset >> (8 * offset_len);
60*4882a593Smuzhiyun
61*4882a593Smuzhiyun return 0;
62*4882a593Smuzhiyun }
63*4882a593Smuzhiyun
i2c_read_bytewise(struct udevice * dev,uint offset,uint8_t * buffer,int len)64*4882a593Smuzhiyun static int i2c_read_bytewise(struct udevice *dev, uint offset,
65*4882a593Smuzhiyun uint8_t *buffer, int len)
66*4882a593Smuzhiyun {
67*4882a593Smuzhiyun struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
68*4882a593Smuzhiyun struct udevice *bus = dev_get_parent(dev);
69*4882a593Smuzhiyun struct dm_i2c_ops *ops = i2c_get_ops(bus);
70*4882a593Smuzhiyun struct i2c_msg msg[2], *ptr;
71*4882a593Smuzhiyun uint8_t offset_buf[I2C_MAX_OFFSET_LEN];
72*4882a593Smuzhiyun int ret;
73*4882a593Smuzhiyun int i;
74*4882a593Smuzhiyun
75*4882a593Smuzhiyun for (i = 0; i < len; i++) {
76*4882a593Smuzhiyun if (i2c_setup_offset(chip, offset + i, offset_buf, msg))
77*4882a593Smuzhiyun return -EINVAL;
78*4882a593Smuzhiyun ptr = msg + 1;
79*4882a593Smuzhiyun ptr->addr = chip->chip_addr;
80*4882a593Smuzhiyun ptr->flags = msg->flags | I2C_M_RD;
81*4882a593Smuzhiyun ptr->len = 1;
82*4882a593Smuzhiyun ptr->buf = &buffer[i];
83*4882a593Smuzhiyun ptr++;
84*4882a593Smuzhiyun
85*4882a593Smuzhiyun ret = ops->xfer(bus, msg, ptr - msg);
86*4882a593Smuzhiyun if (ret)
87*4882a593Smuzhiyun return ret;
88*4882a593Smuzhiyun }
89*4882a593Smuzhiyun
90*4882a593Smuzhiyun return 0;
91*4882a593Smuzhiyun }
92*4882a593Smuzhiyun
i2c_write_bytewise(struct udevice * dev,uint offset,const uint8_t * buffer,int len)93*4882a593Smuzhiyun static int i2c_write_bytewise(struct udevice *dev, uint offset,
94*4882a593Smuzhiyun const uint8_t *buffer, int len)
95*4882a593Smuzhiyun {
96*4882a593Smuzhiyun struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
97*4882a593Smuzhiyun struct udevice *bus = dev_get_parent(dev);
98*4882a593Smuzhiyun struct dm_i2c_ops *ops = i2c_get_ops(bus);
99*4882a593Smuzhiyun struct i2c_msg msg[1];
100*4882a593Smuzhiyun uint8_t buf[I2C_MAX_OFFSET_LEN + 1];
101*4882a593Smuzhiyun int ret;
102*4882a593Smuzhiyun int i;
103*4882a593Smuzhiyun
104*4882a593Smuzhiyun for (i = 0; i < len; i++) {
105*4882a593Smuzhiyun if (i2c_setup_offset(chip, offset + i, buf, msg))
106*4882a593Smuzhiyun return -EINVAL;
107*4882a593Smuzhiyun buf[msg->len++] = buffer[i];
108*4882a593Smuzhiyun
109*4882a593Smuzhiyun ret = ops->xfer(bus, msg, 1);
110*4882a593Smuzhiyun if (ret)
111*4882a593Smuzhiyun return ret;
112*4882a593Smuzhiyun }
113*4882a593Smuzhiyun
114*4882a593Smuzhiyun return 0;
115*4882a593Smuzhiyun }
116*4882a593Smuzhiyun
dm_i2c_read(struct udevice * dev,uint offset,uint8_t * buffer,int len)117*4882a593Smuzhiyun int dm_i2c_read(struct udevice *dev, uint offset, uint8_t *buffer, int len)
118*4882a593Smuzhiyun {
119*4882a593Smuzhiyun struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
120*4882a593Smuzhiyun struct udevice *bus = dev_get_parent(dev);
121*4882a593Smuzhiyun struct dm_i2c_ops *ops = i2c_get_ops(bus);
122*4882a593Smuzhiyun struct i2c_msg msg[2], *ptr;
123*4882a593Smuzhiyun uint8_t offset_buf[I2C_MAX_OFFSET_LEN];
124*4882a593Smuzhiyun int msg_count;
125*4882a593Smuzhiyun
126*4882a593Smuzhiyun if (!ops->xfer)
127*4882a593Smuzhiyun return -ENOSYS;
128*4882a593Smuzhiyun if (chip->flags & DM_I2C_CHIP_RD_ADDRESS)
129*4882a593Smuzhiyun return i2c_read_bytewise(dev, offset, buffer, len);
130*4882a593Smuzhiyun ptr = msg;
131*4882a593Smuzhiyun if (!i2c_setup_offset(chip, offset, offset_buf, ptr))
132*4882a593Smuzhiyun ptr++;
133*4882a593Smuzhiyun
134*4882a593Smuzhiyun if (len) {
135*4882a593Smuzhiyun ptr->addr = chip->chip_addr;
136*4882a593Smuzhiyun ptr->flags = chip->flags & DM_I2C_CHIP_10BIT ? I2C_M_TEN : 0;
137*4882a593Smuzhiyun ptr->flags |= I2C_M_RD;
138*4882a593Smuzhiyun ptr->len = len;
139*4882a593Smuzhiyun ptr->buf = buffer;
140*4882a593Smuzhiyun ptr++;
141*4882a593Smuzhiyun }
142*4882a593Smuzhiyun msg_count = ptr - msg;
143*4882a593Smuzhiyun
144*4882a593Smuzhiyun return ops->xfer(bus, msg, msg_count);
145*4882a593Smuzhiyun }
146*4882a593Smuzhiyun
dm_i2c_write(struct udevice * dev,uint offset,const uint8_t * buffer,int len)147*4882a593Smuzhiyun int dm_i2c_write(struct udevice *dev, uint offset, const uint8_t *buffer,
148*4882a593Smuzhiyun int len)
149*4882a593Smuzhiyun {
150*4882a593Smuzhiyun struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
151*4882a593Smuzhiyun struct udevice *bus = dev_get_parent(dev);
152*4882a593Smuzhiyun struct dm_i2c_ops *ops = i2c_get_ops(bus);
153*4882a593Smuzhiyun struct i2c_msg msg[1];
154*4882a593Smuzhiyun
155*4882a593Smuzhiyun if (!ops->xfer)
156*4882a593Smuzhiyun return -ENOSYS;
157*4882a593Smuzhiyun
158*4882a593Smuzhiyun if (chip->flags & DM_I2C_CHIP_WR_ADDRESS)
159*4882a593Smuzhiyun return i2c_write_bytewise(dev, offset, buffer, len);
160*4882a593Smuzhiyun /*
161*4882a593Smuzhiyun * The simple approach would be to send two messages here: one to
162*4882a593Smuzhiyun * set the offset and one to write the bytes. However some drivers
163*4882a593Smuzhiyun * will not be expecting this, and some chips won't like how the
164*4882a593Smuzhiyun * driver presents this on the I2C bus.
165*4882a593Smuzhiyun *
166*4882a593Smuzhiyun * The API does not support separate offset and data. We could extend
167*4882a593Smuzhiyun * it with a flag indicating that there is data in the next message
168*4882a593Smuzhiyun * that needs to be processed in the same transaction. We could
169*4882a593Smuzhiyun * instead add an additional buffer to each message. For now, handle
170*4882a593Smuzhiyun * this in the uclass since it isn't clear what the impact on drivers
171*4882a593Smuzhiyun * would be with this extra complication. Unfortunately this means
172*4882a593Smuzhiyun * copying the message.
173*4882a593Smuzhiyun *
174*4882a593Smuzhiyun * Use the stack for small messages, malloc() for larger ones. We
175*4882a593Smuzhiyun * need to allow space for the offset (up to 4 bytes) and the message
176*4882a593Smuzhiyun * itself.
177*4882a593Smuzhiyun */
178*4882a593Smuzhiyun if (len < 64) {
179*4882a593Smuzhiyun uint8_t buf[I2C_MAX_OFFSET_LEN + len];
180*4882a593Smuzhiyun
181*4882a593Smuzhiyun i2c_setup_offset(chip, offset, buf, msg);
182*4882a593Smuzhiyun msg->len += len;
183*4882a593Smuzhiyun memcpy(buf + chip->offset_len, buffer, len);
184*4882a593Smuzhiyun
185*4882a593Smuzhiyun return ops->xfer(bus, msg, 1);
186*4882a593Smuzhiyun } else {
187*4882a593Smuzhiyun uint8_t *buf;
188*4882a593Smuzhiyun int ret;
189*4882a593Smuzhiyun
190*4882a593Smuzhiyun buf = malloc(I2C_MAX_OFFSET_LEN + len);
191*4882a593Smuzhiyun if (!buf)
192*4882a593Smuzhiyun return -ENOMEM;
193*4882a593Smuzhiyun i2c_setup_offset(chip, offset, buf, msg);
194*4882a593Smuzhiyun msg->len += len;
195*4882a593Smuzhiyun memcpy(buf + chip->offset_len, buffer, len);
196*4882a593Smuzhiyun
197*4882a593Smuzhiyun ret = ops->xfer(bus, msg, 1);
198*4882a593Smuzhiyun free(buf);
199*4882a593Smuzhiyun return ret;
200*4882a593Smuzhiyun }
201*4882a593Smuzhiyun }
202*4882a593Smuzhiyun
dm_i2c_xfer(struct udevice * dev,struct i2c_msg * msg,int nmsgs)203*4882a593Smuzhiyun int dm_i2c_xfer(struct udevice *dev, struct i2c_msg *msg, int nmsgs)
204*4882a593Smuzhiyun {
205*4882a593Smuzhiyun struct udevice *bus = dev_get_parent(dev);
206*4882a593Smuzhiyun struct dm_i2c_ops *ops = i2c_get_ops(bus);
207*4882a593Smuzhiyun
208*4882a593Smuzhiyun if (!ops->xfer)
209*4882a593Smuzhiyun return -ENOSYS;
210*4882a593Smuzhiyun
211*4882a593Smuzhiyun return ops->xfer(bus, msg, nmsgs);
212*4882a593Smuzhiyun }
213*4882a593Smuzhiyun
dm_i2c_reg_read(struct udevice * dev,uint offset)214*4882a593Smuzhiyun int dm_i2c_reg_read(struct udevice *dev, uint offset)
215*4882a593Smuzhiyun {
216*4882a593Smuzhiyun uint8_t val;
217*4882a593Smuzhiyun int ret;
218*4882a593Smuzhiyun
219*4882a593Smuzhiyun ret = dm_i2c_read(dev, offset, &val, 1);
220*4882a593Smuzhiyun if (ret < 0)
221*4882a593Smuzhiyun return ret;
222*4882a593Smuzhiyun
223*4882a593Smuzhiyun return val;
224*4882a593Smuzhiyun }
225*4882a593Smuzhiyun
dm_i2c_reg_write(struct udevice * dev,uint offset,uint value)226*4882a593Smuzhiyun int dm_i2c_reg_write(struct udevice *dev, uint offset, uint value)
227*4882a593Smuzhiyun {
228*4882a593Smuzhiyun uint8_t val = value;
229*4882a593Smuzhiyun
230*4882a593Smuzhiyun return dm_i2c_write(dev, offset, &val, 1);
231*4882a593Smuzhiyun }
232*4882a593Smuzhiyun
dm_i2c_reg_clrset(struct udevice * dev,uint offset,u32 clr,u32 set)233*4882a593Smuzhiyun int dm_i2c_reg_clrset(struct udevice *dev, uint offset, u32 clr, u32 set)
234*4882a593Smuzhiyun {
235*4882a593Smuzhiyun uint8_t val;
236*4882a593Smuzhiyun int ret;
237*4882a593Smuzhiyun
238*4882a593Smuzhiyun ret = dm_i2c_read(dev, offset, &val, 1);
239*4882a593Smuzhiyun if (ret < 0)
240*4882a593Smuzhiyun return ret;
241*4882a593Smuzhiyun
242*4882a593Smuzhiyun val &= ~clr;
243*4882a593Smuzhiyun val |= set;
244*4882a593Smuzhiyun
245*4882a593Smuzhiyun return dm_i2c_write(dev, offset, &val, 1);
246*4882a593Smuzhiyun }
247*4882a593Smuzhiyun
248*4882a593Smuzhiyun /**
249*4882a593Smuzhiyun * i2c_probe_chip() - probe for a chip on a bus
250*4882a593Smuzhiyun *
251*4882a593Smuzhiyun * @bus: Bus to probe
252*4882a593Smuzhiyun * @chip_addr: Chip address to probe
253*4882a593Smuzhiyun * @flags: Flags for the chip
254*4882a593Smuzhiyun * @return 0 if found, -ENOSYS if the driver is invalid, -EREMOTEIO if the chip
255*4882a593Smuzhiyun * does not respond to probe
256*4882a593Smuzhiyun */
i2c_probe_chip(struct udevice * bus,uint chip_addr,enum dm_i2c_chip_flags chip_flags)257*4882a593Smuzhiyun static int i2c_probe_chip(struct udevice *bus, uint chip_addr,
258*4882a593Smuzhiyun enum dm_i2c_chip_flags chip_flags)
259*4882a593Smuzhiyun {
260*4882a593Smuzhiyun struct dm_i2c_ops *ops = i2c_get_ops(bus);
261*4882a593Smuzhiyun struct i2c_msg msg[1];
262*4882a593Smuzhiyun int ret;
263*4882a593Smuzhiyun
264*4882a593Smuzhiyun if (ops->probe_chip) {
265*4882a593Smuzhiyun ret = ops->probe_chip(bus, chip_addr, chip_flags);
266*4882a593Smuzhiyun if (!ret || ret != -ENOSYS)
267*4882a593Smuzhiyun return ret;
268*4882a593Smuzhiyun }
269*4882a593Smuzhiyun
270*4882a593Smuzhiyun if (!ops->xfer)
271*4882a593Smuzhiyun return -ENOSYS;
272*4882a593Smuzhiyun
273*4882a593Smuzhiyun /* Probe with a zero-length message */
274*4882a593Smuzhiyun msg->addr = chip_addr;
275*4882a593Smuzhiyun msg->flags = chip_flags & DM_I2C_CHIP_10BIT ? I2C_M_TEN : 0;
276*4882a593Smuzhiyun msg->len = 0;
277*4882a593Smuzhiyun msg->buf = NULL;
278*4882a593Smuzhiyun
279*4882a593Smuzhiyun return ops->xfer(bus, msg, 1);
280*4882a593Smuzhiyun }
281*4882a593Smuzhiyun
i2c_bind_driver(struct udevice * bus,uint chip_addr,uint offset_len,struct udevice ** devp)282*4882a593Smuzhiyun static int i2c_bind_driver(struct udevice *bus, uint chip_addr, uint offset_len,
283*4882a593Smuzhiyun struct udevice **devp)
284*4882a593Smuzhiyun {
285*4882a593Smuzhiyun struct dm_i2c_chip *chip;
286*4882a593Smuzhiyun char name[30], *str;
287*4882a593Smuzhiyun struct udevice *dev;
288*4882a593Smuzhiyun int ret;
289*4882a593Smuzhiyun
290*4882a593Smuzhiyun snprintf(name, sizeof(name), "generic_%x", chip_addr);
291*4882a593Smuzhiyun str = strdup(name);
292*4882a593Smuzhiyun if (!str)
293*4882a593Smuzhiyun return -ENOMEM;
294*4882a593Smuzhiyun ret = device_bind_driver(bus, "i2c_generic_chip_drv", str, &dev);
295*4882a593Smuzhiyun debug("%s: device_bind_driver: ret=%d\n", __func__, ret);
296*4882a593Smuzhiyun if (ret)
297*4882a593Smuzhiyun goto err_bind;
298*4882a593Smuzhiyun
299*4882a593Smuzhiyun /* Tell the device what we know about it */
300*4882a593Smuzhiyun chip = dev_get_parent_platdata(dev);
301*4882a593Smuzhiyun chip->chip_addr = chip_addr;
302*4882a593Smuzhiyun chip->offset_len = offset_len;
303*4882a593Smuzhiyun ret = device_probe(dev);
304*4882a593Smuzhiyun debug("%s: device_probe: ret=%d\n", __func__, ret);
305*4882a593Smuzhiyun if (ret)
306*4882a593Smuzhiyun goto err_probe;
307*4882a593Smuzhiyun
308*4882a593Smuzhiyun *devp = dev;
309*4882a593Smuzhiyun return 0;
310*4882a593Smuzhiyun
311*4882a593Smuzhiyun err_probe:
312*4882a593Smuzhiyun /*
313*4882a593Smuzhiyun * If the device failed to probe, unbind it. There is nothing there
314*4882a593Smuzhiyun * on the bus so we don't want to leave it lying around
315*4882a593Smuzhiyun */
316*4882a593Smuzhiyun device_unbind(dev);
317*4882a593Smuzhiyun err_bind:
318*4882a593Smuzhiyun free(str);
319*4882a593Smuzhiyun return ret;
320*4882a593Smuzhiyun }
321*4882a593Smuzhiyun
i2c_get_chip(struct udevice * bus,uint chip_addr,uint offset_len,struct udevice ** devp)322*4882a593Smuzhiyun int i2c_get_chip(struct udevice *bus, uint chip_addr, uint offset_len,
323*4882a593Smuzhiyun struct udevice **devp)
324*4882a593Smuzhiyun {
325*4882a593Smuzhiyun struct udevice *dev;
326*4882a593Smuzhiyun
327*4882a593Smuzhiyun debug("%s: Searching bus '%s' for address %02x: ", __func__,
328*4882a593Smuzhiyun bus->name, chip_addr);
329*4882a593Smuzhiyun for (device_find_first_child(bus, &dev); dev;
330*4882a593Smuzhiyun device_find_next_child(&dev)) {
331*4882a593Smuzhiyun struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
332*4882a593Smuzhiyun int ret;
333*4882a593Smuzhiyun
334*4882a593Smuzhiyun if (chip->chip_addr == chip_addr) {
335*4882a593Smuzhiyun ret = device_probe(dev);
336*4882a593Smuzhiyun debug("found, ret=%d\n", ret);
337*4882a593Smuzhiyun if (ret)
338*4882a593Smuzhiyun return ret;
339*4882a593Smuzhiyun *devp = dev;
340*4882a593Smuzhiyun return 0;
341*4882a593Smuzhiyun }
342*4882a593Smuzhiyun }
343*4882a593Smuzhiyun debug("not found\n");
344*4882a593Smuzhiyun return i2c_bind_driver(bus, chip_addr, offset_len, devp);
345*4882a593Smuzhiyun }
346*4882a593Smuzhiyun
i2c_get_chip_for_busnum(int busnum,int chip_addr,uint offset_len,struct udevice ** devp)347*4882a593Smuzhiyun int i2c_get_chip_for_busnum(int busnum, int chip_addr, uint offset_len,
348*4882a593Smuzhiyun struct udevice **devp)
349*4882a593Smuzhiyun {
350*4882a593Smuzhiyun struct udevice *bus;
351*4882a593Smuzhiyun int ret;
352*4882a593Smuzhiyun
353*4882a593Smuzhiyun ret = uclass_get_device_by_seq(UCLASS_I2C, busnum, &bus);
354*4882a593Smuzhiyun if (ret) {
355*4882a593Smuzhiyun debug("Cannot find I2C bus %d\n", busnum);
356*4882a593Smuzhiyun return ret;
357*4882a593Smuzhiyun }
358*4882a593Smuzhiyun ret = i2c_get_chip(bus, chip_addr, offset_len, devp);
359*4882a593Smuzhiyun if (ret) {
360*4882a593Smuzhiyun debug("Cannot find I2C chip %02x on bus %d\n", chip_addr,
361*4882a593Smuzhiyun busnum);
362*4882a593Smuzhiyun return ret;
363*4882a593Smuzhiyun }
364*4882a593Smuzhiyun
365*4882a593Smuzhiyun return 0;
366*4882a593Smuzhiyun }
367*4882a593Smuzhiyun
dm_i2c_probe(struct udevice * bus,uint chip_addr,uint chip_flags,struct udevice ** devp)368*4882a593Smuzhiyun int dm_i2c_probe(struct udevice *bus, uint chip_addr, uint chip_flags,
369*4882a593Smuzhiyun struct udevice **devp)
370*4882a593Smuzhiyun {
371*4882a593Smuzhiyun int ret;
372*4882a593Smuzhiyun
373*4882a593Smuzhiyun *devp = NULL;
374*4882a593Smuzhiyun
375*4882a593Smuzhiyun /* First probe that chip */
376*4882a593Smuzhiyun ret = i2c_probe_chip(bus, chip_addr, chip_flags);
377*4882a593Smuzhiyun debug("%s: bus='%s', address %02x, ret=%d\n", __func__, bus->name,
378*4882a593Smuzhiyun chip_addr, ret);
379*4882a593Smuzhiyun if (ret)
380*4882a593Smuzhiyun return ret;
381*4882a593Smuzhiyun
382*4882a593Smuzhiyun /* The chip was found, see if we have a driver, and probe it */
383*4882a593Smuzhiyun ret = i2c_get_chip(bus, chip_addr, 1, devp);
384*4882a593Smuzhiyun debug("%s: i2c_get_chip: ret=%d\n", __func__, ret);
385*4882a593Smuzhiyun
386*4882a593Smuzhiyun return ret;
387*4882a593Smuzhiyun }
388*4882a593Smuzhiyun
dm_i2c_set_bus_speed(struct udevice * bus,unsigned int speed)389*4882a593Smuzhiyun int dm_i2c_set_bus_speed(struct udevice *bus, unsigned int speed)
390*4882a593Smuzhiyun {
391*4882a593Smuzhiyun struct dm_i2c_ops *ops = i2c_get_ops(bus);
392*4882a593Smuzhiyun struct dm_i2c_bus *i2c = dev_get_uclass_priv(bus);
393*4882a593Smuzhiyun int ret;
394*4882a593Smuzhiyun
395*4882a593Smuzhiyun /*
396*4882a593Smuzhiyun * If we have a method, call it. If not then the driver probably wants
397*4882a593Smuzhiyun * to deal with speed changes on the next transfer. It can easily read
398*4882a593Smuzhiyun * the current speed from this uclass
399*4882a593Smuzhiyun */
400*4882a593Smuzhiyun if (ops->set_bus_speed) {
401*4882a593Smuzhiyun ret = ops->set_bus_speed(bus, speed);
402*4882a593Smuzhiyun if (ret)
403*4882a593Smuzhiyun return ret;
404*4882a593Smuzhiyun }
405*4882a593Smuzhiyun i2c->speed_hz = speed;
406*4882a593Smuzhiyun
407*4882a593Smuzhiyun return 0;
408*4882a593Smuzhiyun }
409*4882a593Smuzhiyun
dm_i2c_get_bus_speed(struct udevice * bus)410*4882a593Smuzhiyun int dm_i2c_get_bus_speed(struct udevice *bus)
411*4882a593Smuzhiyun {
412*4882a593Smuzhiyun struct dm_i2c_ops *ops = i2c_get_ops(bus);
413*4882a593Smuzhiyun struct dm_i2c_bus *i2c = dev_get_uclass_priv(bus);
414*4882a593Smuzhiyun
415*4882a593Smuzhiyun if (!ops->get_bus_speed)
416*4882a593Smuzhiyun return i2c->speed_hz;
417*4882a593Smuzhiyun
418*4882a593Smuzhiyun return ops->get_bus_speed(bus);
419*4882a593Smuzhiyun }
420*4882a593Smuzhiyun
i2c_set_chip_flags(struct udevice * dev,uint flags)421*4882a593Smuzhiyun int i2c_set_chip_flags(struct udevice *dev, uint flags)
422*4882a593Smuzhiyun {
423*4882a593Smuzhiyun struct udevice *bus = dev->parent;
424*4882a593Smuzhiyun struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
425*4882a593Smuzhiyun struct dm_i2c_ops *ops = i2c_get_ops(bus);
426*4882a593Smuzhiyun int ret;
427*4882a593Smuzhiyun
428*4882a593Smuzhiyun if (ops->set_flags) {
429*4882a593Smuzhiyun ret = ops->set_flags(dev, flags);
430*4882a593Smuzhiyun if (ret)
431*4882a593Smuzhiyun return ret;
432*4882a593Smuzhiyun }
433*4882a593Smuzhiyun chip->flags = flags;
434*4882a593Smuzhiyun
435*4882a593Smuzhiyun return 0;
436*4882a593Smuzhiyun }
437*4882a593Smuzhiyun
i2c_get_chip_flags(struct udevice * dev,uint * flagsp)438*4882a593Smuzhiyun int i2c_get_chip_flags(struct udevice *dev, uint *flagsp)
439*4882a593Smuzhiyun {
440*4882a593Smuzhiyun struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
441*4882a593Smuzhiyun
442*4882a593Smuzhiyun *flagsp = chip->flags;
443*4882a593Smuzhiyun
444*4882a593Smuzhiyun return 0;
445*4882a593Smuzhiyun }
446*4882a593Smuzhiyun
i2c_set_chip_offset_len(struct udevice * dev,uint offset_len)447*4882a593Smuzhiyun int i2c_set_chip_offset_len(struct udevice *dev, uint offset_len)
448*4882a593Smuzhiyun {
449*4882a593Smuzhiyun struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
450*4882a593Smuzhiyun
451*4882a593Smuzhiyun if (offset_len > I2C_MAX_OFFSET_LEN)
452*4882a593Smuzhiyun return -EINVAL;
453*4882a593Smuzhiyun chip->offset_len = offset_len;
454*4882a593Smuzhiyun
455*4882a593Smuzhiyun return 0;
456*4882a593Smuzhiyun }
457*4882a593Smuzhiyun
i2c_get_chip_offset_len(struct udevice * dev)458*4882a593Smuzhiyun int i2c_get_chip_offset_len(struct udevice *dev)
459*4882a593Smuzhiyun {
460*4882a593Smuzhiyun struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
461*4882a593Smuzhiyun
462*4882a593Smuzhiyun return chip->offset_len;
463*4882a593Smuzhiyun }
464*4882a593Smuzhiyun
i2c_deblock(struct udevice * bus)465*4882a593Smuzhiyun int i2c_deblock(struct udevice *bus)
466*4882a593Smuzhiyun {
467*4882a593Smuzhiyun struct dm_i2c_ops *ops = i2c_get_ops(bus);
468*4882a593Smuzhiyun
469*4882a593Smuzhiyun /*
470*4882a593Smuzhiyun * We could implement a software deblocking here if we could get
471*4882a593Smuzhiyun * access to the GPIOs used by I2C, and switch them to GPIO mode
472*4882a593Smuzhiyun * and then back to I2C. This is somewhat beyond our powers in
473*4882a593Smuzhiyun * driver model at present, so for now just fail.
474*4882a593Smuzhiyun *
475*4882a593Smuzhiyun * See https://patchwork.ozlabs.org/patch/399040/
476*4882a593Smuzhiyun */
477*4882a593Smuzhiyun if (!ops->deblock)
478*4882a593Smuzhiyun return -ENOSYS;
479*4882a593Smuzhiyun
480*4882a593Smuzhiyun return ops->deblock(bus);
481*4882a593Smuzhiyun }
482*4882a593Smuzhiyun
483*4882a593Smuzhiyun #if CONFIG_IS_ENABLED(OF_CONTROL)
i2c_chip_ofdata_to_platdata(struct udevice * dev,struct dm_i2c_chip * chip)484*4882a593Smuzhiyun int i2c_chip_ofdata_to_platdata(struct udevice *dev, struct dm_i2c_chip *chip)
485*4882a593Smuzhiyun {
486*4882a593Smuzhiyun int addr;
487*4882a593Smuzhiyun
488*4882a593Smuzhiyun chip->offset_len = dev_read_u32_default(dev, "u-boot,i2c-offset-len",
489*4882a593Smuzhiyun 1);
490*4882a593Smuzhiyun chip->flags = 0;
491*4882a593Smuzhiyun addr = dev_read_u32_default(dev, "reg", -1);
492*4882a593Smuzhiyun if (addr == -1) {
493*4882a593Smuzhiyun debug("%s: I2C Node '%s' has no 'reg' property %s\n", __func__,
494*4882a593Smuzhiyun dev_read_name(dev), dev->name);
495*4882a593Smuzhiyun return -EINVAL;
496*4882a593Smuzhiyun }
497*4882a593Smuzhiyun chip->chip_addr = addr;
498*4882a593Smuzhiyun
499*4882a593Smuzhiyun return 0;
500*4882a593Smuzhiyun }
501*4882a593Smuzhiyun #endif
502*4882a593Smuzhiyun
i2c_post_probe(struct udevice * dev)503*4882a593Smuzhiyun static int i2c_post_probe(struct udevice *dev)
504*4882a593Smuzhiyun {
505*4882a593Smuzhiyun #if CONFIG_IS_ENABLED(OF_CONTROL)
506*4882a593Smuzhiyun struct dm_i2c_bus *i2c = dev_get_uclass_priv(dev);
507*4882a593Smuzhiyun int seq;
508*4882a593Smuzhiyun
509*4882a593Smuzhiyun i2c->speed_hz = dev_read_u32_default(dev, "clock-frequency", 100000);
510*4882a593Smuzhiyun
511*4882a593Smuzhiyun if (dev_read_alias_seq(dev, &seq) >= 0)
512*4882a593Smuzhiyun printf("I2c%d speed: %dHz\n", seq, i2c->speed_hz);
513*4882a593Smuzhiyun else
514*4882a593Smuzhiyun printf("I2c speed: %dHz\n", i2c->speed_hz);
515*4882a593Smuzhiyun
516*4882a593Smuzhiyun return dm_i2c_set_bus_speed(dev, i2c->speed_hz);
517*4882a593Smuzhiyun #else
518*4882a593Smuzhiyun return 0;
519*4882a593Smuzhiyun #endif
520*4882a593Smuzhiyun }
521*4882a593Smuzhiyun
i2c_child_post_bind(struct udevice * dev)522*4882a593Smuzhiyun static int i2c_child_post_bind(struct udevice *dev)
523*4882a593Smuzhiyun {
524*4882a593Smuzhiyun #if CONFIG_IS_ENABLED(OF_CONTROL)
525*4882a593Smuzhiyun struct dm_i2c_chip *plat = dev_get_parent_platdata(dev);
526*4882a593Smuzhiyun
527*4882a593Smuzhiyun if (!dev_of_valid(dev))
528*4882a593Smuzhiyun return 0;
529*4882a593Smuzhiyun return i2c_chip_ofdata_to_platdata(dev, plat);
530*4882a593Smuzhiyun #else
531*4882a593Smuzhiyun return 0;
532*4882a593Smuzhiyun #endif
533*4882a593Smuzhiyun }
534*4882a593Smuzhiyun
535*4882a593Smuzhiyun UCLASS_DRIVER(i2c) = {
536*4882a593Smuzhiyun .id = UCLASS_I2C,
537*4882a593Smuzhiyun .name = "i2c",
538*4882a593Smuzhiyun .flags = DM_UC_FLAG_SEQ_ALIAS,
539*4882a593Smuzhiyun #if CONFIG_IS_ENABLED(OF_CONTROL)
540*4882a593Smuzhiyun .post_bind = dm_scan_fdt_dev,
541*4882a593Smuzhiyun #endif
542*4882a593Smuzhiyun .post_probe = i2c_post_probe,
543*4882a593Smuzhiyun .per_device_auto_alloc_size = sizeof(struct dm_i2c_bus),
544*4882a593Smuzhiyun .per_child_platdata_auto_alloc_size = sizeof(struct dm_i2c_chip),
545*4882a593Smuzhiyun .child_post_bind = i2c_child_post_bind,
546*4882a593Smuzhiyun };
547*4882a593Smuzhiyun
548*4882a593Smuzhiyun UCLASS_DRIVER(i2c_generic) = {
549*4882a593Smuzhiyun .id = UCLASS_I2C_GENERIC,
550*4882a593Smuzhiyun .name = "i2c_generic",
551*4882a593Smuzhiyun };
552*4882a593Smuzhiyun
553*4882a593Smuzhiyun U_BOOT_DRIVER(i2c_generic_chip_drv) = {
554*4882a593Smuzhiyun .name = "i2c_generic_chip_drv",
555*4882a593Smuzhiyun .id = UCLASS_I2C_GENERIC,
556*4882a593Smuzhiyun };
557