4 * Copyright (c) 2004 Texas Instruments
6 * This package is free software; you can redistribute it and/or
7 * modify it under the terms of the license found in the file
8 * named COPYING that should have accompanied this file.
10 * THIS PACKAGE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
11 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
12 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
14 * Author: Jian Zhang jzhang@ti.com, Texas Instruments
16 * Copyright (c) 2003 Wolfgang Denk, wd@denx.de
17 * Rewritten to fit into the current U-Boot framework
19 * Adapted for OMAP2420 I2C, r-woodruff2@ti.com
21 * Copyright (c) 2013 Lubomir Popov <lpopov@mm-sol.com>, MM Solutions
22 * New i2c_read, i2c_write and i2c_probe functions, tested on OMAP4
23 * (4430/60/70), OMAP5 (5430) and AM335X (3359); should work on older
24 * OMAPs and derivatives as well. The only anticipated exception would
25 * be the OMAP2420, which shall require driver modification.
26 * - Rewritten i2c_read to operate correctly with all types of chips
27 * (old function could not read consistent data from some I2C slaves).
28 * - Optimized i2c_write.
29 * - New i2c_probe, performs write access vs read. The old probe could
30 * hang the system under certain conditions (e.g. unconfigured pads).
31 * - The read/write/probe functions try to identify unconfigured bus.
32 * - Status functions now read irqstatus_raw as per TRM guidelines
33 * (except for OMAP243X and OMAP34XX).
34 * - Driver now supports up to I2C5 (OMAP5).
36 * Copyright (c) 2014 Hannes Schmelzer <oe5hpm@oevsv.at>, B&R
37 * - Added support for set_speed
45 #include <asm/arch/i2c.h>
48 #include "omap24xx_i2c.h"
50 #define I2C_TIMEOUT 1000
52 /* Absolutely safe for status update at 100 kHz I2C: */
63 static int omap24_i2c_findpsc(u32 *pscl, u32 *psch, uint speed)
65 unsigned long internal_clk = 0, fclk;
66 unsigned int prescaler;
69 * This method is only called for Standard and Fast Mode speeds
71 * For some TI SoCs it is explicitly written in TRM (e,g, SPRUHZ6G,
72 * page 5685, Table 24-7)
73 * that the internal I2C clock (after prescaler) should be between
74 * 7-12 MHz (at least for Fast Mode (FS)).
76 * Such approach is used in v4.9 Linux kernel in:
77 * ./drivers/i2c/busses/i2c-omap.c (omap_i2c_init function).
80 speed /= 1000; /* convert speed to kHz */
87 fclk = I2C_IP_CLK / 1000;
88 prescaler = fclk / internal_clk;
89 prescaler = prescaler - 1;
95 scl = internal_clk / speed;
96 *pscl = scl - (scl / 3) - I2C_FASTSPEED_SCLL_TRIM;
97 *psch = (scl / 3) - I2C_FASTSPEED_SCLH_TRIM;
100 *pscl = internal_clk / (speed * 2) - I2C_FASTSPEED_SCLL_TRIM;
101 *psch = internal_clk / (speed * 2) - I2C_FASTSPEED_SCLH_TRIM;
104 debug("%s: speed [kHz]: %d psc: 0x%x sscl: 0x%x ssch: 0x%x\n",
105 __func__, speed, prescaler, *pscl, *psch);
107 if (*pscl <= 0 || *psch <= 0 || prescaler <= 0)
114 * Wait for the bus to be free by checking the Bus Busy (BB)
115 * bit to become clear
117 static int wait_for_bb(struct i2c *i2c_base, int waitdelay)
119 int timeout = I2C_TIMEOUT;
122 writew(0xFFFF, &i2c_base->stat); /* clear current interrupts...*/
123 #if defined(CONFIG_OMAP34XX)
124 while ((stat = readw(&i2c_base->stat) & I2C_STAT_BB) && timeout--) {
126 /* Read RAW status */
127 while ((stat = readw(&i2c_base->irqstatus_raw) &
128 I2C_STAT_BB) && timeout--) {
130 writew(stat, &i2c_base->stat);
135 printf("Timed out in wait_for_bb: status=%04x\n",
139 writew(0xFFFF, &i2c_base->stat); /* clear delayed stuff*/
144 * Wait for the I2C controller to complete current action
147 static u16 wait_for_event(struct i2c *i2c_base, int waitdelay)
150 int timeout = I2C_TIMEOUT;
154 #if defined(CONFIG_OMAP34XX)
155 status = readw(&i2c_base->stat);
157 /* Read RAW status */
158 status = readw(&i2c_base->irqstatus_raw);
161 (I2C_STAT_ROVR | I2C_STAT_XUDF | I2C_STAT_XRDY |
162 I2C_STAT_RRDY | I2C_STAT_ARDY | I2C_STAT_NACK |
163 I2C_STAT_AL)) && timeout--);
166 printf("Timed out in wait_for_event: status=%04x\n",
169 * If status is still 0 here, probably the bus pads have
170 * not been configured for I2C, and/or pull-ups are missing.
172 printf("Check if pads/pull-ups of bus are properly configured\n");
173 writew(0xFFFF, &i2c_base->stat);
180 static void flush_fifo(struct i2c *i2c_base)
185 * note: if you try and read data when its not there or ready
186 * you get a bus error
189 stat = readw(&i2c_base->stat);
190 if (stat == I2C_STAT_RRDY) {
191 readb(&i2c_base->data);
192 writew(I2C_STAT_RRDY, &i2c_base->stat);
199 static int __omap24_i2c_setspeed(struct i2c *i2c_base, uint speed,
202 int psc, fsscll = 0, fssclh = 0;
203 int hsscll = 0, hssclh = 0;
204 u32 scll = 0, sclh = 0;
206 if (speed >= OMAP_I2C_HIGH_SPEED) {
208 psc = I2C_IP_CLK / I2C_INTERNAL_SAMPLING_CLK;
210 if (psc < I2C_PSC_MIN) {
211 printf("Error : I2C unsupported prescaler %d\n", psc);
215 /* For first phase of HS mode */
216 fsscll = I2C_INTERNAL_SAMPLING_CLK / (2 * speed);
220 fsscll -= I2C_HIGHSPEED_PHASE_ONE_SCLL_TRIM;
221 fssclh -= I2C_HIGHSPEED_PHASE_ONE_SCLH_TRIM;
222 if (((fsscll < 0) || (fssclh < 0)) ||
223 ((fsscll > 255) || (fssclh > 255))) {
224 puts("Error : I2C initializing first phase clock\n");
228 /* For second phase of HS mode */
229 hsscll = hssclh = I2C_INTERNAL_SAMPLING_CLK / (2 * speed);
231 hsscll -= I2C_HIGHSPEED_PHASE_TWO_SCLL_TRIM;
232 hssclh -= I2C_HIGHSPEED_PHASE_TWO_SCLH_TRIM;
233 if (((fsscll < 0) || (fssclh < 0)) ||
234 ((fsscll > 255) || (fssclh > 255))) {
235 puts("Error : I2C initializing second phase clock\n");
239 scll = (unsigned int)hsscll << 8 | (unsigned int)fsscll;
240 sclh = (unsigned int)hssclh << 8 | (unsigned int)fssclh;
243 /* Standard and fast speed */
244 psc = omap24_i2c_findpsc(&scll, &sclh, speed);
246 puts("Error : I2C initializing clock\n");
251 *waitdelay = (10000000 / speed) * 2; /* wait for 20 clkperiods */
252 writew(0, &i2c_base->con);
253 writew(psc, &i2c_base->psc);
254 writew(scll, &i2c_base->scll);
255 writew(sclh, &i2c_base->sclh);
256 writew(I2C_CON_EN, &i2c_base->con);
257 writew(0xFFFF, &i2c_base->stat); /* clear all pending status */
262 static void omap24_i2c_deblock(struct i2c *i2c_base)
268 /* set test mode ST_EN = 1 */
269 orgsystest = readw(&i2c_base->systest);
270 systest = orgsystest;
271 /* enable testmode */
272 systest |= I2C_SYSTEST_ST_EN;
273 writew(systest, &i2c_base->systest);
274 systest &= ~I2C_SYSTEST_TMODE_MASK;
275 systest |= 3 << I2C_SYSTEST_TMODE_SHIFT;
276 writew(systest, &i2c_base->systest);
278 /* set SCL, SDA = 1 */
279 systest |= I2C_SYSTEST_SCL_O | I2C_SYSTEST_SDA_O;
280 writew(systest, &i2c_base->systest);
283 /* toggle scl 9 clocks */
284 for (i = 0; i < 9; i++) {
286 systest &= ~I2C_SYSTEST_SCL_O;
287 writew(systest, &i2c_base->systest);
290 systest |= I2C_SYSTEST_SCL_O;
291 writew(systest, &i2c_base->systest);
296 systest &= ~I2C_SYSTEST_SDA_O;
297 writew(systest, &i2c_base->systest);
299 systest |= I2C_SYSTEST_SCL_O | I2C_SYSTEST_SDA_O;
300 writew(systest, &i2c_base->systest);
303 /* restore original mode */
304 writew(orgsystest, &i2c_base->systest);
307 static void __omap24_i2c_init(struct i2c *i2c_base, int speed, int slaveadd,
310 int timeout = I2C_TIMEOUT;
314 if (readw(&i2c_base->con) & I2C_CON_EN) {
315 writew(0, &i2c_base->con);
319 writew(0x2, &i2c_base->sysc); /* for ES2 after soft reset */
322 writew(I2C_CON_EN, &i2c_base->con);
323 while (!(readw(&i2c_base->syss) & I2C_SYSS_RDONE) && timeout--) {
325 puts("ERROR: Timeout in soft-reset\n");
331 if (0 != __omap24_i2c_setspeed(i2c_base, speed, waitdelay)) {
332 printf("ERROR: failed to setup I2C bus-speed!\n");
337 writew(slaveadd, &i2c_base->oa);
339 #if defined(CONFIG_OMAP34XX)
341 * Have to enable interrupts for OMAP2/3, these IPs don't have
342 * an 'irqstatus_raw' register and we shall have to poll 'stat'
344 writew(I2C_IE_XRDY_IE | I2C_IE_RRDY_IE | I2C_IE_ARDY_IE |
345 I2C_IE_NACK_IE | I2C_IE_AL_IE, &i2c_base->ie);
348 flush_fifo(i2c_base);
349 writew(0xFFFF, &i2c_base->stat);
351 /* Handle possible failed I2C state */
352 if (wait_for_bb(i2c_base, *waitdelay))
354 omap24_i2c_deblock(i2c_base);
361 * i2c_probe: Use write access. Allows to identify addresses that are
362 * write-only (like the config register of dual-port EEPROMs)
364 static int __omap24_i2c_probe(struct i2c *i2c_base, int waitdelay, uchar chip)
367 int res = 1; /* default = fail */
369 if (chip == readw(&i2c_base->oa))
372 /* Wait until bus is free */
373 if (wait_for_bb(i2c_base, waitdelay))
376 /* No data transfer, slave addr only */
377 writew(chip, &i2c_base->sa);
378 /* Stop bit needed here */
379 writew(I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | I2C_CON_TRX |
380 I2C_CON_STP, &i2c_base->con);
382 status = wait_for_event(i2c_base, waitdelay);
384 if ((status & ~I2C_STAT_XRDY) == 0 || (status & I2C_STAT_AL)) {
386 * With current high-level command implementation, notifying
387 * the user shall flood the console with 127 messages. If
388 * silent exit is desired upon unconfigured bus, remove the
389 * following 'if' section:
391 if (status == I2C_STAT_XRDY)
392 printf("i2c_probe: pads on bus probably not configured (status=0x%x)\n",
398 /* Check for ACK (!NAK) */
399 if (!(status & I2C_STAT_NACK)) {
400 res = 0; /* Device found */
401 udelay(waitdelay);/* Required by AM335X in SPL */
402 /* Abort transfer (force idle state) */
403 writew(I2C_CON_MST | I2C_CON_TRX, &i2c_base->con); /* Reset */
405 writew(I2C_CON_EN | I2C_CON_MST | I2C_CON_TRX |
406 I2C_CON_STP, &i2c_base->con); /* STP */
409 flush_fifo(i2c_base);
410 writew(0xFFFF, &i2c_base->stat);
415 * i2c_read: Function now uses a single I2C read transaction with bulk transfer
416 * of the requested number of bytes (note that the 'i2c md' command
417 * limits this to 16 bytes anyway). If CONFIG_I2C_REPEATED_START is
418 * defined in the board config header, this transaction shall be with
419 * Repeated Start (Sr) between the address and data phases; otherwise
420 * Stop-Start (P-S) shall be used (some I2C chips do require a P-S).
421 * The address (reg offset) may be 0, 1 or 2 bytes long.
422 * Function now reads correctly from chips that return more than one
423 * byte of data per addressed register (like TI temperature sensors),
424 * or that do not need a register address at all (such as some clock
427 static int __omap24_i2c_read(struct i2c *i2c_base, int waitdelay, uchar chip,
428 uint addr, int alen, uchar *buffer, int len)
434 puts("I2C read: addr len < 0\n");
438 puts("I2C read: data len < 0\n");
441 if (buffer == NULL) {
442 puts("I2C read: NULL pointer passed\n");
447 printf("I2C read: addr len %d not supported\n", alen);
451 if (addr + len > (1 << 16)) {
452 puts("I2C read: address out of range\n");
456 #ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW
458 * EEPROM chips that implement "address overflow" are ones
459 * like Catalyst 24WC04/08/16 which has 9/10/11 bits of
460 * address and the extra bits end up in the "chip address"
461 * bit slots. This makes a 24WC08 (1Kbyte) chip look like
462 * four 256 byte chips.
464 * Note that we consider the length of the address field to
465 * still be one byte because the extra address bits are
466 * hidden in the chip address.
469 chip |= ((addr >> (alen * 8)) &
470 CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW);
473 /* Wait until bus not busy */
474 if (wait_for_bb(i2c_base, waitdelay))
477 /* Zero, one or two bytes reg address (offset) */
478 writew(alen, &i2c_base->cnt);
479 /* Set slave address */
480 writew(chip, &i2c_base->sa);
483 /* Must write reg offset first */
484 #ifdef CONFIG_I2C_REPEATED_START
485 /* No stop bit, use Repeated Start (Sr) */
486 writew(I2C_CON_EN | I2C_CON_MST | I2C_CON_STT |
487 I2C_CON_TRX, &i2c_base->con);
489 /* Stop - Start (P-S) */
490 writew(I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | I2C_CON_STP |
491 I2C_CON_TRX, &i2c_base->con);
493 /* Send register offset */
495 status = wait_for_event(i2c_base, waitdelay);
496 /* Try to identify bus that is not padconf'd for I2C */
497 if (status == I2C_STAT_XRDY) {
499 printf("i2c_read (addr phase): pads on bus probably not configured (status=0x%x)\n",
503 if (status == 0 || (status & I2C_STAT_NACK)) {
505 printf("i2c_read: error waiting for addr ACK (status=0x%x)\n",
510 if (status & I2C_STAT_XRDY) {
512 /* Do we have to use byte access? */
513 writeb((addr >> (8 * alen)) & 0xff,
515 writew(I2C_STAT_XRDY, &i2c_base->stat);
518 if (status & I2C_STAT_ARDY) {
519 writew(I2C_STAT_ARDY, &i2c_base->stat);
524 /* Set slave address */
525 writew(chip, &i2c_base->sa);
526 /* Read len bytes from slave */
527 writew(len, &i2c_base->cnt);
528 /* Need stop bit here */
529 writew(I2C_CON_EN | I2C_CON_MST |
530 I2C_CON_STT | I2C_CON_STP,
535 status = wait_for_event(i2c_base, waitdelay);
537 * Try to identify bus that is not padconf'd for I2C. This
538 * state could be left over from previous transactions if
539 * the address phase is skipped due to alen=0.
541 if (status == I2C_STAT_XRDY) {
543 printf("i2c_read (data phase): pads on bus probably not configured (status=0x%x)\n",
547 if (status == 0 || (status & I2C_STAT_NACK)) {
551 if (status & I2C_STAT_RRDY) {
552 *buffer++ = readb(&i2c_base->data);
553 writew(I2C_STAT_RRDY, &i2c_base->stat);
555 if (status & I2C_STAT_ARDY) {
556 writew(I2C_STAT_ARDY, &i2c_base->stat);
562 flush_fifo(i2c_base);
563 writew(0xFFFF, &i2c_base->stat);
567 /* i2c_write: Address (reg offset) may be 0, 1 or 2 bytes long. */
568 static int __omap24_i2c_write(struct i2c *i2c_base, int waitdelay, uchar chip,
569 uint addr, int alen, uchar *buffer, int len)
574 int timeout = I2C_TIMEOUT;
577 puts("I2C write: addr len < 0\n");
582 puts("I2C write: data len < 0\n");
586 if (buffer == NULL) {
587 puts("I2C write: NULL pointer passed\n");
592 printf("I2C write: addr len %d not supported\n", alen);
596 if (addr + len > (1 << 16)) {
597 printf("I2C write: address 0x%x + 0x%x out of range\n",
602 #ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW
604 * EEPROM chips that implement "address overflow" are ones
605 * like Catalyst 24WC04/08/16 which has 9/10/11 bits of
606 * address and the extra bits end up in the "chip address"
607 * bit slots. This makes a 24WC08 (1Kbyte) chip look like
608 * four 256 byte chips.
610 * Note that we consider the length of the address field to
611 * still be one byte because the extra address bits are
612 * hidden in the chip address.
615 chip |= ((addr >> (alen * 8)) &
616 CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW);
619 /* Wait until bus not busy */
620 if (wait_for_bb(i2c_base, waitdelay))
623 /* Start address phase - will write regoffset + len bytes data */
624 writew(alen + len, &i2c_base->cnt);
625 /* Set slave address */
626 writew(chip, &i2c_base->sa);
627 /* Stop bit needed here */
628 writew(I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | I2C_CON_TRX |
629 I2C_CON_STP, &i2c_base->con);
632 /* Must write reg offset (one or two bytes) */
633 status = wait_for_event(i2c_base, waitdelay);
634 /* Try to identify bus that is not padconf'd for I2C */
635 if (status == I2C_STAT_XRDY) {
637 printf("i2c_write: pads on bus probably not configured (status=0x%x)\n",
641 if (status == 0 || (status & I2C_STAT_NACK)) {
643 printf("i2c_write: error waiting for addr ACK (status=0x%x)\n",
647 if (status & I2C_STAT_XRDY) {
649 writeb((addr >> (8 * alen)) & 0xff, &i2c_base->data);
650 writew(I2C_STAT_XRDY, &i2c_base->stat);
653 printf("i2c_write: bus not ready for addr Tx (status=0x%x)\n",
658 /* Address phase is over, now write data */
659 for (i = 0; i < len; i++) {
660 status = wait_for_event(i2c_base, waitdelay);
661 if (status == 0 || (status & I2C_STAT_NACK)) {
663 printf("i2c_write: error waiting for data ACK (status=0x%x)\n",
667 if (status & I2C_STAT_XRDY) {
668 writeb(buffer[i], &i2c_base->data);
669 writew(I2C_STAT_XRDY, &i2c_base->stat);
672 printf("i2c_write: bus not ready for data Tx (i=%d)\n",
678 * poll ARDY bit for making sure that last byte really has been
679 * transferred on the bus.
682 status = wait_for_event(i2c_base, waitdelay);
683 } while (!(status & I2C_STAT_ARDY) && timeout--);
685 printf("i2c_write: timed out writig last byte!\n");
688 flush_fifo(i2c_base);
689 writew(0xFFFF, &i2c_base->stat);
693 #ifndef CONFIG_DM_I2C
695 * The legacy I2C functions. These need to get removed once
696 * all users of this driver are converted to DM.
698 static struct i2c *omap24_get_base(struct i2c_adapter *adap)
700 switch (adap->hwadapnr) {
702 return (struct i2c *)I2C_BASE1;
705 return (struct i2c *)I2C_BASE2;
707 #if (CONFIG_SYS_I2C_BUS_MAX > 2)
709 return (struct i2c *)I2C_BASE3;
711 #if (CONFIG_SYS_I2C_BUS_MAX > 3)
713 return (struct i2c *)I2C_BASE4;
715 #if (CONFIG_SYS_I2C_BUS_MAX > 4)
717 return (struct i2c *)I2C_BASE5;
723 printf("wrong hwadapnr: %d\n", adap->hwadapnr);
730 static int omap24_i2c_read(struct i2c_adapter *adap, uchar chip, uint addr,
731 int alen, uchar *buffer, int len)
733 struct i2c *i2c_base = omap24_get_base(adap);
735 return __omap24_i2c_read(i2c_base, adap->waitdelay, chip, addr,
740 static int omap24_i2c_write(struct i2c_adapter *adap, uchar chip, uint addr,
741 int alen, uchar *buffer, int len)
743 struct i2c *i2c_base = omap24_get_base(adap);
745 return __omap24_i2c_write(i2c_base, adap->waitdelay, chip, addr,
749 static uint omap24_i2c_setspeed(struct i2c_adapter *adap, uint speed)
751 struct i2c *i2c_base = omap24_get_base(adap);
754 ret = __omap24_i2c_setspeed(i2c_base, speed, &adap->waitdelay);
756 pr_err("%s: set i2c speed failed\n", __func__);
765 static void omap24_i2c_init(struct i2c_adapter *adap, int speed, int slaveadd)
767 struct i2c *i2c_base = omap24_get_base(adap);
769 return __omap24_i2c_init(i2c_base, speed, slaveadd, &adap->waitdelay);
772 static int omap24_i2c_probe(struct i2c_adapter *adap, uchar chip)
774 struct i2c *i2c_base = omap24_get_base(adap);
776 return __omap24_i2c_probe(i2c_base, adap->waitdelay, chip);
779 #if !defined(CONFIG_SYS_OMAP24_I2C_SPEED1)
780 #define CONFIG_SYS_OMAP24_I2C_SPEED1 CONFIG_SYS_OMAP24_I2C_SPEED
782 #if !defined(CONFIG_SYS_OMAP24_I2C_SLAVE1)
783 #define CONFIG_SYS_OMAP24_I2C_SLAVE1 CONFIG_SYS_OMAP24_I2C_SLAVE
786 U_BOOT_I2C_ADAP_COMPLETE(omap24_0, omap24_i2c_init, omap24_i2c_probe,
787 omap24_i2c_read, omap24_i2c_write, omap24_i2c_setspeed,
788 CONFIG_SYS_OMAP24_I2C_SPEED,
789 CONFIG_SYS_OMAP24_I2C_SLAVE,
791 U_BOOT_I2C_ADAP_COMPLETE(omap24_1, omap24_i2c_init, omap24_i2c_probe,
792 omap24_i2c_read, omap24_i2c_write, omap24_i2c_setspeed,
793 CONFIG_SYS_OMAP24_I2C_SPEED1,
794 CONFIG_SYS_OMAP24_I2C_SLAVE1,
796 #if (CONFIG_SYS_I2C_BUS_MAX > 2)
797 #if !defined(CONFIG_SYS_OMAP24_I2C_SPEED2)
798 #define CONFIG_SYS_OMAP24_I2C_SPEED2 CONFIG_SYS_OMAP24_I2C_SPEED
800 #if !defined(CONFIG_SYS_OMAP24_I2C_SLAVE2)
801 #define CONFIG_SYS_OMAP24_I2C_SLAVE2 CONFIG_SYS_OMAP24_I2C_SLAVE
804 U_BOOT_I2C_ADAP_COMPLETE(omap24_2, omap24_i2c_init, omap24_i2c_probe,
805 omap24_i2c_read, omap24_i2c_write, NULL,
806 CONFIG_SYS_OMAP24_I2C_SPEED2,
807 CONFIG_SYS_OMAP24_I2C_SLAVE2,
809 #if (CONFIG_SYS_I2C_BUS_MAX > 3)
810 #if !defined(CONFIG_SYS_OMAP24_I2C_SPEED3)
811 #define CONFIG_SYS_OMAP24_I2C_SPEED3 CONFIG_SYS_OMAP24_I2C_SPEED
813 #if !defined(CONFIG_SYS_OMAP24_I2C_SLAVE3)
814 #define CONFIG_SYS_OMAP24_I2C_SLAVE3 CONFIG_SYS_OMAP24_I2C_SLAVE
817 U_BOOT_I2C_ADAP_COMPLETE(omap24_3, omap24_i2c_init, omap24_i2c_probe,
818 omap24_i2c_read, omap24_i2c_write, NULL,
819 CONFIG_SYS_OMAP24_I2C_SPEED3,
820 CONFIG_SYS_OMAP24_I2C_SLAVE3,
822 #if (CONFIG_SYS_I2C_BUS_MAX > 4)
823 #if !defined(CONFIG_SYS_OMAP24_I2C_SPEED4)
824 #define CONFIG_SYS_OMAP24_I2C_SPEED4 CONFIG_SYS_OMAP24_I2C_SPEED
826 #if !defined(CONFIG_SYS_OMAP24_I2C_SLAVE4)
827 #define CONFIG_SYS_OMAP24_I2C_SLAVE4 CONFIG_SYS_OMAP24_I2C_SLAVE
830 U_BOOT_I2C_ADAP_COMPLETE(omap24_4, omap24_i2c_init, omap24_i2c_probe,
831 omap24_i2c_read, omap24_i2c_write, NULL,
832 CONFIG_SYS_OMAP24_I2C_SPEED4,
833 CONFIG_SYS_OMAP24_I2C_SLAVE4,
839 #else /* CONFIG_DM_I2C */
841 static int omap_i2c_xfer(struct udevice *bus, struct i2c_msg *msg, int nmsgs)
843 struct omap_i2c *priv = dev_get_priv(bus);
846 debug("i2c_xfer: %d messages\n", nmsgs);
847 for (; nmsgs > 0; nmsgs--, msg++) {
848 debug("i2c_xfer: chip=0x%x, len=0x%x\n", msg->addr, msg->len);
849 if (msg->flags & I2C_M_RD) {
850 ret = __omap24_i2c_read(priv->regs, priv->waitdelay,
851 msg->addr, 0, 0, msg->buf,
854 ret = __omap24_i2c_write(priv->regs, priv->waitdelay,
855 msg->addr, 0, 0, msg->buf,
859 debug("i2c_write: error sending\n");
867 static int omap_i2c_set_bus_speed(struct udevice *bus, unsigned int speed)
869 struct omap_i2c *priv = dev_get_priv(bus);
873 return __omap24_i2c_setspeed(priv->regs, speed, &priv->waitdelay);
876 static int omap_i2c_probe_chip(struct udevice *bus, uint chip_addr,
879 struct omap_i2c *priv = dev_get_priv(bus);
881 return __omap24_i2c_probe(priv->regs, priv->waitdelay, chip_addr);
884 static int omap_i2c_probe(struct udevice *bus)
886 struct omap_i2c *priv = dev_get_priv(bus);
888 __omap24_i2c_init(priv->regs, priv->speed, 0, &priv->waitdelay);
893 static int omap_i2c_ofdata_to_platdata(struct udevice *bus)
895 struct omap_i2c *priv = dev_get_priv(bus);
897 priv->regs = map_physmem(devfdt_get_addr(bus), sizeof(void *),
899 priv->speed = CONFIG_SYS_OMAP24_I2C_SPEED;
904 static const struct dm_i2c_ops omap_i2c_ops = {
905 .xfer = omap_i2c_xfer,
906 .probe_chip = omap_i2c_probe_chip,
907 .set_bus_speed = omap_i2c_set_bus_speed,
910 static const struct udevice_id omap_i2c_ids[] = {
911 { .compatible = "ti,omap3-i2c" },
912 { .compatible = "ti,omap4-i2c" },
916 U_BOOT_DRIVER(i2c_omap) = {
919 .of_match = omap_i2c_ids,
920 .ofdata_to_platdata = omap_i2c_ofdata_to_platdata,
921 .probe = omap_i2c_probe,
922 .priv_auto_alloc_size = sizeof(struct omap_i2c),
923 .ops = &omap_i2c_ops,
924 .flags = DM_FLAG_PRE_RELOC,
927 #endif /* CONFIG_DM_I2C */