1 // SPDX-License-Identifier: GPL-2.0+
16 #include <linux/delay.h>
17 #include "designware_i2c.h"
18 #include <dm/device_compat.h>
19 #include <linux/err.h>
22 * This assigned unique hex value is constant and is derived from the two ASCII
23 * letters 'DW' followed by a 16-bit unsigned number
25 #define DW_I2C_COMP_TYPE 0x44570140
27 #ifdef CONFIG_SYS_I2C_DW_ENABLE_STATUS_UNSUPPORTED
28 static int dw_i2c_enable(struct i2c_regs *i2c_base, bool enable)
30 u32 ena = enable ? IC_ENABLE_0B : 0;
32 writel(ena, &i2c_base->ic_enable);
37 static int dw_i2c_enable(struct i2c_regs *i2c_base, bool enable)
39 u32 ena = enable ? IC_ENABLE_0B : 0;
43 writel(ena, &i2c_base->ic_enable);
44 if ((readl(&i2c_base->ic_enable_status) & IC_ENABLE_0B) == ena)
48 * Wait 10 times the signaling period of the highest I2C
49 * transfer supported by the driver (for 400KHz this is
50 * 25us) as described in the DesignWare I2C databook.
54 printf("timeout in %sabling I2C adapter\n", enable ? "en" : "dis");
60 /* High and low times in different speed modes (in ns) */
63 DEFAULT_SDA_HOLD_TIME = 300,
67 * calc_counts() - Convert a period to a number of IC clk cycles
69 * @ic_clk: Input clock in Hz
70 * @period_ns: Period to represent, in ns
71 * @return calculated count
73 static uint calc_counts(uint ic_clk, uint period_ns)
75 return DIV_ROUND_UP(ic_clk / 1000 * period_ns, NANO_TO_KILO);
79 * struct i2c_mode_info - Information about an I2C speed mode
81 * Each speed mode has its own characteristics. This struct holds these to aid
82 * calculations in dw_i2c_calc_timing().
85 * @min_scl_lowtime_ns: Minimum value for SCL low period in ns
86 * @min_scl_hightime_ns: Minimum value for SCL high period in ns
87 * @def_rise_time_ns: Default rise time in ns
88 * @def_fall_time_ns: Default fall time in ns
90 struct i2c_mode_info {
92 int min_scl_hightime_ns;
93 int min_scl_lowtime_ns;
98 static const struct i2c_mode_info info_for_mode[] = {
99 [IC_SPEED_MODE_STANDARD] = {
100 I2C_SPEED_STANDARD_RATE,
106 [IC_SPEED_MODE_FAST] = {
113 [IC_SPEED_MODE_FAST_PLUS] = {
114 I2C_SPEED_FAST_PLUS_RATE,
120 [IC_SPEED_MODE_HIGH] = {
130 * dw_i2c_calc_timing() - Calculate the timings to use for a bus
132 * @priv: Bus private information (NULL if not using driver model)
133 * @mode: Speed mode to use
134 * @ic_clk: IC clock speed in Hz
135 * @spk_cnt: Spike-suppression count
136 * @config: Returns value to use
137 * @return 0 if OK, -EINVAL if the calculation failed due to invalid data
139 static int dw_i2c_calc_timing(struct dw_i2c *priv, enum i2c_speed_mode mode,
140 int ic_clk, int spk_cnt,
141 struct dw_i2c_speed_config *config)
143 int fall_cnt, rise_cnt, min_tlow_cnt, min_thigh_cnt;
144 int hcnt, lcnt, period_cnt, diff, tot;
145 int sda_hold_time_ns, scl_rise_time_ns, scl_fall_time_ns;
146 const struct i2c_mode_info *info;
149 * Find the period, rise, fall, min tlow, and min thigh in terms of
150 * counts of the IC clock
152 info = &info_for_mode[mode];
153 period_cnt = ic_clk / info->speed;
154 scl_rise_time_ns = priv && priv->scl_rise_time_ns ?
155 priv->scl_rise_time_ns : info->def_rise_time_ns;
156 scl_fall_time_ns = priv && priv->scl_fall_time_ns ?
157 priv->scl_fall_time_ns : info->def_fall_time_ns;
158 rise_cnt = calc_counts(ic_clk, scl_rise_time_ns);
159 fall_cnt = calc_counts(ic_clk, scl_fall_time_ns);
160 min_tlow_cnt = calc_counts(ic_clk, info->min_scl_lowtime_ns);
161 min_thigh_cnt = calc_counts(ic_clk, info->min_scl_hightime_ns);
163 debug("dw_i2c: mode %d, ic_clk %d, speed %d, period %d rise %d fall %d tlow %d thigh %d spk %d\n",
164 mode, ic_clk, info->speed, period_cnt, rise_cnt, fall_cnt,
165 min_tlow_cnt, min_thigh_cnt, spk_cnt);
168 * Back-solve for hcnt and lcnt according to the following equations:
169 * SCL_High_time = [(HCNT + IC_*_SPKLEN + 7) * ic_clk] + SCL_Fall_time
170 * SCL_Low_time = [(LCNT + 1) * ic_clk] - SCL_Fall_time + SCL_Rise_time
172 hcnt = min_thigh_cnt - fall_cnt - 7 - spk_cnt;
173 lcnt = min_tlow_cnt - rise_cnt + fall_cnt - 1;
175 if (hcnt < 0 || lcnt < 0) {
176 debug("dw_i2c: bad counts. hcnt = %d lcnt = %d\n", hcnt, lcnt);
177 return log_msg_ret("counts", -EINVAL);
181 * Now add things back up to ensure the period is hit. If it is off,
182 * split the difference and bias to lcnt for remainder
184 tot = hcnt + lcnt + 7 + spk_cnt + rise_cnt + 1;
186 if (tot < period_cnt) {
187 diff = (period_cnt - tot) / 2;
190 tot = hcnt + lcnt + 7 + spk_cnt + rise_cnt + 1;
191 lcnt += period_cnt - tot;
194 config->scl_lcnt = lcnt;
195 config->scl_hcnt = hcnt;
197 /* Use internal default unless other value is specified */
198 sda_hold_time_ns = priv && priv->sda_hold_time_ns ?
199 priv->sda_hold_time_ns : DEFAULT_SDA_HOLD_TIME;
200 config->sda_hold = calc_counts(ic_clk, sda_hold_time_ns);
202 debug("dw_i2c: hcnt = %d lcnt = %d sda hold = %d\n", hcnt, lcnt,
209 * calc_bus_speed() - Calculate the config to use for a particular i2c speed
211 * @priv: Private information for the driver (NULL if not using driver model)
212 * @i2c_base: Registers for the I2C controller
213 * @speed: Required i2c speed in Hz
214 * @bus_clk: Input clock to the I2C controller in Hz (e.g. IC_CLK)
215 * @config: Returns the config to use for this speed
216 * @return 0 if OK, -ve on error
218 static int calc_bus_speed(struct dw_i2c *priv, struct i2c_regs *regs, int speed,
219 ulong bus_clk, struct dw_i2c_speed_config *config)
221 const struct dw_scl_sda_cfg *scl_sda_cfg = NULL;
222 enum i2c_speed_mode i2c_spd;
227 scl_sda_cfg = priv->scl_sda_cfg;
228 /* Allow high speed if there is no config, or the config allows it */
229 if (speed >= I2C_SPEED_HIGH_RATE)
230 i2c_spd = IC_SPEED_MODE_HIGH;
231 else if (speed >= I2C_SPEED_FAST_PLUS_RATE)
232 i2c_spd = IC_SPEED_MODE_FAST_PLUS;
233 else if (speed >= I2C_SPEED_FAST_RATE)
234 i2c_spd = IC_SPEED_MODE_FAST;
236 i2c_spd = IC_SPEED_MODE_STANDARD;
238 /* Check is high speed possible and fall back to fast mode if not */
239 if (i2c_spd == IC_SPEED_MODE_HIGH) {
242 comp_param1 = readl(®s->comp_param1);
243 if ((comp_param1 & DW_IC_COMP_PARAM_1_SPEED_MODE_MASK)
244 != DW_IC_COMP_PARAM_1_SPEED_MODE_HIGH)
245 i2c_spd = IC_SPEED_MODE_FAST;
248 /* Get the proper spike-suppression count based on target speed */
249 if (!priv || !priv->has_spk_cnt)
251 else if (i2c_spd >= IC_SPEED_MODE_HIGH)
252 spk_cnt = readl(®s->hs_spklen);
254 spk_cnt = readl(®s->fs_spklen);
256 config->sda_hold = scl_sda_cfg->sda_hold;
257 if (i2c_spd == IC_SPEED_MODE_STANDARD) {
258 config->scl_hcnt = scl_sda_cfg->ss_hcnt;
259 config->scl_lcnt = scl_sda_cfg->ss_lcnt;
260 } else if (i2c_spd == IC_SPEED_MODE_HIGH) {
261 config->scl_hcnt = scl_sda_cfg->hs_hcnt;
262 config->scl_lcnt = scl_sda_cfg->hs_lcnt;
264 config->scl_hcnt = scl_sda_cfg->fs_hcnt;
265 config->scl_lcnt = scl_sda_cfg->fs_lcnt;
268 ret = dw_i2c_calc_timing(priv, i2c_spd, bus_clk, spk_cnt,
271 return log_msg_ret("gen_confg", ret);
273 config->speed_mode = i2c_spd;
279 * _dw_i2c_set_bus_speed() - Set the i2c speed
281 * @priv: Private information for the driver (NULL if not using driver model)
282 * @i2c_base: Registers for the I2C controller
283 * @speed: Required i2c speed in Hz
284 * @bus_clk: Input clock to the I2C controller in Hz (e.g. IC_CLK)
285 * @return 0 if OK, -ve on error
287 static int _dw_i2c_set_bus_speed(struct dw_i2c *priv, struct i2c_regs *i2c_base,
288 unsigned int speed, unsigned int bus_clk)
290 struct dw_i2c_speed_config config;
295 ret = calc_bus_speed(priv, i2c_base, speed, bus_clk, &config);
299 /* Get enable setting for restore later */
300 ena = readl(&i2c_base->ic_enable) & IC_ENABLE_0B;
302 /* to set speed cltr must be disabled */
303 dw_i2c_enable(i2c_base, false);
305 cntl = (readl(&i2c_base->ic_con) & (~IC_CON_SPD_MSK));
307 switch (config.speed_mode) {
308 case IC_SPEED_MODE_HIGH:
309 cntl |= IC_CON_SPD_HS;
310 writel(config.scl_hcnt, &i2c_base->ic_hs_scl_hcnt);
311 writel(config.scl_lcnt, &i2c_base->ic_hs_scl_lcnt);
313 case IC_SPEED_MODE_STANDARD:
314 cntl |= IC_CON_SPD_SS;
315 writel(config.scl_hcnt, &i2c_base->ic_ss_scl_hcnt);
316 writel(config.scl_lcnt, &i2c_base->ic_ss_scl_lcnt);
318 case IC_SPEED_MODE_FAST_PLUS:
319 case IC_SPEED_MODE_FAST:
321 cntl |= IC_CON_SPD_FS;
322 writel(config.scl_hcnt, &i2c_base->ic_fs_scl_hcnt);
323 writel(config.scl_lcnt, &i2c_base->ic_fs_scl_lcnt);
327 writel(cntl, &i2c_base->ic_con);
329 /* Configure SDA Hold Time if required */
331 writel(config.sda_hold, &i2c_base->ic_sda_hold);
333 /* Restore back i2c now speed set */
334 if (ena == IC_ENABLE_0B)
335 dw_i2c_enable(i2c_base, true);
337 priv->config = config;
342 int dw_i2c_gen_speed_config(const struct udevice *dev, int speed_hz,
343 struct dw_i2c_speed_config *config)
345 struct dw_i2c *priv = dev_get_priv(dev);
349 #if CONFIG_IS_ENABLED(CLK)
350 rate = clk_get_rate(&priv->clk);
351 if (IS_ERR_VALUE(rate))
352 return log_msg_ret("clk", -EINVAL);
357 ret = calc_bus_speed(priv, priv->regs, speed_hz, rate, config);
359 printf("%s: ret=%d\n", __func__, ret);
361 return log_msg_ret("calc_bus_speed", ret);
367 * i2c_setaddress - Sets the target slave address
368 * @i2c_addr: target i2c address
370 * Sets the target slave address.
372 static void i2c_setaddress(struct i2c_regs *i2c_base, unsigned int i2c_addr)
375 dw_i2c_enable(i2c_base, false);
377 writel(i2c_addr, &i2c_base->ic_tar);
380 dw_i2c_enable(i2c_base, true);
384 * i2c_flush_rxfifo - Flushes the i2c RX FIFO
386 * Flushes the i2c RX FIFO
388 static void i2c_flush_rxfifo(struct i2c_regs *i2c_base)
390 while (readl(&i2c_base->ic_status) & IC_STATUS_RFNE)
391 readl(&i2c_base->ic_cmd_data);
395 * i2c_wait_for_bb - Waits for bus busy
399 static int i2c_wait_for_bb(struct i2c_regs *i2c_base)
401 unsigned long start_time_bb = get_timer(0);
403 while ((readl(&i2c_base->ic_status) & IC_STATUS_MA) ||
404 !(readl(&i2c_base->ic_status) & IC_STATUS_TFE)) {
406 /* Evaluate timeout */
407 if (get_timer(start_time_bb) > (unsigned long)(I2C_BYTE_TO_BB))
414 static int i2c_xfer_init(struct i2c_regs *i2c_base, uchar chip, uint addr,
417 if (i2c_wait_for_bb(i2c_base))
420 i2c_setaddress(i2c_base, chip);
423 /* high byte address going out first */
424 writel((addr >> (alen * 8)) & 0xff,
425 &i2c_base->ic_cmd_data);
430 static int i2c_xfer_finish(struct i2c_regs *i2c_base)
432 ulong start_stop_det = get_timer(0);
435 if ((readl(&i2c_base->ic_raw_intr_stat) & IC_STOP_DET)) {
436 readl(&i2c_base->ic_clr_stop_det);
438 } else if (get_timer(start_stop_det) > I2C_STOPDET_TO) {
443 if (i2c_wait_for_bb(i2c_base)) {
444 printf("Timed out waiting for bus\n");
448 i2c_flush_rxfifo(i2c_base);
454 * i2c_read - Read from i2c memory
455 * @chip: target i2c address
456 * @addr: address to read from
458 * @buffer: buffer for read data
459 * @len: no of bytes to be read
461 * Read from i2c memory.
463 static int __dw_i2c_read(struct i2c_regs *i2c_base, u8 dev, uint addr,
464 int alen, u8 *buffer, int len)
466 unsigned long start_time_rx;
467 unsigned int active = 0;
469 #ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW
471 * EEPROM chips that implement "address overflow" are ones
472 * like Catalyst 24WC04/08/16 which has 9/10/11 bits of
473 * address and the extra bits end up in the "chip address"
474 * bit slots. This makes a 24WC08 (1Kbyte) chip look like
475 * four 256 byte chips.
477 * Note that we consider the length of the address field to
478 * still be one byte because the extra address bits are
479 * hidden in the chip address.
481 dev |= ((addr >> (alen * 8)) & CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW);
482 addr &= ~(CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW << (alen * 8));
484 debug("%s: fix addr_overflow: dev %02x addr %02x\n", __func__, dev,
488 if (i2c_xfer_init(i2c_base, dev, addr, alen))
491 start_time_rx = get_timer(0);
495 * Avoid writing to ic_cmd_data multiple times
496 * in case this loop spins too quickly and the
497 * ic_status RFNE bit isn't set after the first
498 * write. Subsequent writes to ic_cmd_data can
499 * trigger spurious i2c transfer.
502 writel(IC_CMD | IC_STOP, &i2c_base->ic_cmd_data);
504 writel(IC_CMD, &i2c_base->ic_cmd_data);
508 if (readl(&i2c_base->ic_status) & IC_STATUS_RFNE) {
509 *buffer++ = (uchar)readl(&i2c_base->ic_cmd_data);
511 start_time_rx = get_timer(0);
513 } else if (get_timer(start_time_rx) > I2C_BYTE_TO) {
518 return i2c_xfer_finish(i2c_base);
522 * i2c_write - Write to i2c memory
523 * @chip: target i2c address
524 * @addr: address to read from
526 * @buffer: buffer for read data
527 * @len: no of bytes to be read
529 * Write to i2c memory.
531 static int __dw_i2c_write(struct i2c_regs *i2c_base, u8 dev, uint addr,
532 int alen, u8 *buffer, int len)
535 unsigned long start_time_tx;
537 #ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW
539 * EEPROM chips that implement "address overflow" are ones
540 * like Catalyst 24WC04/08/16 which has 9/10/11 bits of
541 * address and the extra bits end up in the "chip address"
542 * bit slots. This makes a 24WC08 (1Kbyte) chip look like
543 * four 256 byte chips.
545 * Note that we consider the length of the address field to
546 * still be one byte because the extra address bits are
547 * hidden in the chip address.
549 dev |= ((addr >> (alen * 8)) & CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW);
550 addr &= ~(CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW << (alen * 8));
552 debug("%s: fix addr_overflow: dev %02x addr %02x\n", __func__, dev,
556 if (i2c_xfer_init(i2c_base, dev, addr, alen))
559 start_time_tx = get_timer(0);
561 if (readl(&i2c_base->ic_status) & IC_STATUS_TFNF) {
563 writel(*buffer | IC_STOP,
564 &i2c_base->ic_cmd_data);
566 writel(*buffer, &i2c_base->ic_cmd_data);
569 start_time_tx = get_timer(0);
571 } else if (get_timer(start_time_tx) > (nb * I2C_BYTE_TO)) {
572 printf("Timed out. i2c write Failed\n");
577 return i2c_xfer_finish(i2c_base);
581 * __dw_i2c_init - Init function
582 * @speed: required i2c speed
583 * @slaveaddr: slave address for the device
585 * Initialization function.
587 static int __dw_i2c_init(struct i2c_regs *i2c_base, int speed, int slaveaddr)
592 ret = dw_i2c_enable(i2c_base, false);
596 writel(IC_CON_SD | IC_CON_RE | IC_CON_SPD_FS | IC_CON_MM,
598 writel(IC_RX_TL, &i2c_base->ic_rx_tl);
599 writel(IC_TX_TL, &i2c_base->ic_tx_tl);
600 writel(IC_STOP_DET, &i2c_base->ic_intr_mask);
601 #ifndef CONFIG_DM_I2C
602 _dw_i2c_set_bus_speed(NULL, i2c_base, speed, IC_CLK);
603 writel(slaveaddr, &i2c_base->ic_sar);
607 ret = dw_i2c_enable(i2c_base, true);
614 #ifndef CONFIG_DM_I2C
616 * The legacy I2C functions. These need to get removed once
617 * all users of this driver are converted to DM.
619 static struct i2c_regs *i2c_get_base(struct i2c_adapter *adap)
621 switch (adap->hwadapnr) {
622 #if CONFIG_SYS_I2C_BUS_MAX >= 4
624 return (struct i2c_regs *)CONFIG_SYS_I2C_BASE3;
626 #if CONFIG_SYS_I2C_BUS_MAX >= 3
628 return (struct i2c_regs *)CONFIG_SYS_I2C_BASE2;
630 #if CONFIG_SYS_I2C_BUS_MAX >= 2
632 return (struct i2c_regs *)CONFIG_SYS_I2C_BASE1;
635 return (struct i2c_regs *)CONFIG_SYS_I2C_BASE;
637 printf("Wrong I2C-adapter number %d\n", adap->hwadapnr);
643 static unsigned int dw_i2c_set_bus_speed(struct i2c_adapter *adap,
647 return _dw_i2c_set_bus_speed(NULL, i2c_get_base(adap), speed, IC_CLK);
650 static void dw_i2c_init(struct i2c_adapter *adap, int speed, int slaveaddr)
652 __dw_i2c_init(i2c_get_base(adap), speed, slaveaddr);
655 static int dw_i2c_read(struct i2c_adapter *adap, u8 dev, uint addr,
656 int alen, u8 *buffer, int len)
658 return __dw_i2c_read(i2c_get_base(adap), dev, addr, alen, buffer, len);
661 static int dw_i2c_write(struct i2c_adapter *adap, u8 dev, uint addr,
662 int alen, u8 *buffer, int len)
664 return __dw_i2c_write(i2c_get_base(adap), dev, addr, alen, buffer, len);
667 /* dw_i2c_probe - Probe the i2c chip */
668 static int dw_i2c_probe(struct i2c_adapter *adap, u8 dev)
670 struct i2c_regs *i2c_base = i2c_get_base(adap);
675 * Try to read the first location of the chip.
677 ret = __dw_i2c_read(i2c_base, dev, 0, 1, (uchar *)&tmp, 1);
679 dw_i2c_init(adap, adap->speed, adap->slaveaddr);
684 U_BOOT_I2C_ADAP_COMPLETE(dw_0, dw_i2c_init, dw_i2c_probe, dw_i2c_read,
685 dw_i2c_write, dw_i2c_set_bus_speed,
686 CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE, 0)
688 #if CONFIG_SYS_I2C_BUS_MAX >= 2
689 U_BOOT_I2C_ADAP_COMPLETE(dw_1, dw_i2c_init, dw_i2c_probe, dw_i2c_read,
690 dw_i2c_write, dw_i2c_set_bus_speed,
691 CONFIG_SYS_I2C_SPEED1, CONFIG_SYS_I2C_SLAVE1, 1)
694 #if CONFIG_SYS_I2C_BUS_MAX >= 3
695 U_BOOT_I2C_ADAP_COMPLETE(dw_2, dw_i2c_init, dw_i2c_probe, dw_i2c_read,
696 dw_i2c_write, dw_i2c_set_bus_speed,
697 CONFIG_SYS_I2C_SPEED2, CONFIG_SYS_I2C_SLAVE2, 2)
700 #if CONFIG_SYS_I2C_BUS_MAX >= 4
701 U_BOOT_I2C_ADAP_COMPLETE(dw_3, dw_i2c_init, dw_i2c_probe, dw_i2c_read,
702 dw_i2c_write, dw_i2c_set_bus_speed,
703 CONFIG_SYS_I2C_SPEED3, CONFIG_SYS_I2C_SLAVE3, 3)
706 #else /* CONFIG_DM_I2C */
707 /* The DM I2C functions */
709 static int designware_i2c_xfer(struct udevice *bus, struct i2c_msg *msg,
712 struct dw_i2c *i2c = dev_get_priv(bus);
715 debug("i2c_xfer: %d messages\n", nmsgs);
716 for (; nmsgs > 0; nmsgs--, msg++) {
717 debug("i2c_xfer: chip=0x%x, len=0x%x\n", msg->addr, msg->len);
718 if (msg->flags & I2C_M_RD) {
719 ret = __dw_i2c_read(i2c->regs, msg->addr, 0, 0,
722 ret = __dw_i2c_write(i2c->regs, msg->addr, 0, 0,
726 debug("i2c_write: error sending\n");
734 static int designware_i2c_set_bus_speed(struct udevice *bus, unsigned int speed)
736 struct dw_i2c *i2c = dev_get_priv(bus);
739 #if CONFIG_IS_ENABLED(CLK)
740 rate = clk_get_rate(&i2c->clk);
741 if (IS_ERR_VALUE(rate))
742 return log_ret(-EINVAL);
746 return _dw_i2c_set_bus_speed(i2c, i2c->regs, speed, rate);
749 static int designware_i2c_probe_chip(struct udevice *bus, uint chip_addr,
752 struct dw_i2c *i2c = dev_get_priv(bus);
753 struct i2c_regs *i2c_base = i2c->regs;
757 /* Try to read the first location of the chip */
758 ret = __dw_i2c_read(i2c_base, chip_addr, 0, 1, (uchar *)&tmp, 1);
760 __dw_i2c_init(i2c_base, 0, 0);
765 int designware_i2c_ofdata_to_platdata(struct udevice *bus)
767 struct dw_i2c *priv = dev_get_priv(bus);
771 priv->regs = dev_read_addr_ptr(bus);
772 dev_read_u32(bus, "i2c-scl-rising-time-ns", &priv->scl_rise_time_ns);
773 dev_read_u32(bus, "i2c-scl-falling-time-ns", &priv->scl_fall_time_ns);
774 dev_read_u32(bus, "i2c-sda-hold-time-ns", &priv->sda_hold_time_ns);
776 ret = reset_get_bulk(bus, &priv->resets);
778 if (ret != -ENOTSUPP)
779 dev_warn(bus, "Can't get reset: %d\n", ret);
781 reset_deassert_bulk(&priv->resets);
784 #if CONFIG_IS_ENABLED(CLK)
785 ret = clk_get_by_index(bus, 0, &priv->clk);
789 ret = clk_enable(&priv->clk);
790 if (ret && ret != -ENOSYS && ret != -ENOTSUPP) {
791 clk_free(&priv->clk);
792 dev_err(bus, "failed to enable clock\n");
800 int designware_i2c_probe(struct udevice *bus)
802 struct dw_i2c *priv = dev_get_priv(bus);
805 comp_type = readl(&priv->regs->comp_type);
806 if (comp_type != DW_I2C_COMP_TYPE) {
807 log_err("I2C bus %s has unknown type %#x\n", bus->name,
812 log_debug("I2C bus %s version %#x\n", bus->name,
813 readl(&priv->regs->comp_version));
815 return __dw_i2c_init(priv->regs, 0, 0);
818 int designware_i2c_remove(struct udevice *dev)
820 struct dw_i2c *priv = dev_get_priv(dev);
822 #if CONFIG_IS_ENABLED(CLK)
823 clk_disable(&priv->clk);
824 clk_free(&priv->clk);
827 return reset_release_bulk(&priv->resets);
830 const struct dm_i2c_ops designware_i2c_ops = {
831 .xfer = designware_i2c_xfer,
832 .probe_chip = designware_i2c_probe_chip,
833 .set_bus_speed = designware_i2c_set_bus_speed,
836 static const struct udevice_id designware_i2c_ids[] = {
837 { .compatible = "snps,designware-i2c" },
841 U_BOOT_DRIVER(i2c_designware) = {
842 .name = "i2c_designware",
844 .of_match = designware_i2c_ids,
845 .ofdata_to_platdata = designware_i2c_ofdata_to_platdata,
846 .probe = designware_i2c_probe,
847 .priv_auto = sizeof(struct dw_i2c),
848 .remove = designware_i2c_remove,
849 .flags = DM_FLAG_OS_PREPARE,
850 .ops = &designware_i2c_ops,
853 #endif /* CONFIG_DM_I2C */