blob: b7b2aafc7f673db0b5b660881122ffa1281f0a2a [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Peng Fand684adb2017-02-24 09:54:18 +08002/*
3 * Copyright 2016 Freescale Semiconductors, Inc.
Peng Fand684adb2017-02-24 09:54:18 +08004 */
5
6#include <common.h>
7#include <errno.h>
Simon Glass0f2af882020-05-10 11:40:05 -06008#include <log.h>
Peng Fand684adb2017-02-24 09:54:18 +08009#include <asm/io.h>
10#include <asm/arch/clock.h>
11#include <asm/arch/imx-regs.h>
Ye Lid9103f82018-07-08 11:46:40 +080012#include <imx_lpi2c.h>
Peng Fand684adb2017-02-24 09:54:18 +080013#include <asm/arch/sys_proto.h>
14#include <dm.h>
15#include <fdtdec.h>
16#include <i2c.h>
Simon Glass9bc15642020-02-03 07:36:16 -070017#include <dm/device_compat.h>
Peng Fand684adb2017-02-24 09:54:18 +080018
Peng Fand684adb2017-02-24 09:54:18 +080019#define LPI2C_FIFO_SIZE 4
Gao Pan52d457b2018-07-08 11:46:41 +080020#define LPI2C_NACK_TOUT_MS 1
Peng Fand684adb2017-02-24 09:54:18 +080021#define LPI2C_TIMEOUT_MS 100
22
Ye Li2c695462018-07-08 11:46:43 +080023static int bus_i2c_init(struct udevice *bus, int speed);
24
Peng Fand684adb2017-02-24 09:54:18 +080025/* Weak linked function for overridden by some SoC power function */
26int __weak init_i2c_power(unsigned i2c_num)
27{
28 return 0;
29}
30
Simon Glassba1dea42017-05-17 17:18:05 -060031static int imx_lpci2c_check_busy_bus(const struct imx_lpi2c_reg *regs)
Peng Fand684adb2017-02-24 09:54:18 +080032{
Peng Fand684adb2017-02-24 09:54:18 +080033 lpi2c_status_t result = LPI2C_SUCESS;
34 u32 status;
35
36 status = readl(&regs->msr);
37
38 if ((status & LPI2C_MSR_BBF_MASK) && !(status & LPI2C_MSR_MBF_MASK))
39 result = LPI2C_BUSY;
40
41 return result;
42}
43
Simon Glassba1dea42017-05-17 17:18:05 -060044static int imx_lpci2c_check_clear_error(struct imx_lpi2c_reg *regs)
Peng Fand684adb2017-02-24 09:54:18 +080045{
Peng Fand684adb2017-02-24 09:54:18 +080046 lpi2c_status_t result = LPI2C_SUCESS;
47 u32 val, status;
48
49 status = readl(&regs->msr);
50 /* errors to check for */
51 status &= LPI2C_MSR_NDF_MASK | LPI2C_MSR_ALF_MASK |
52 LPI2C_MSR_FEF_MASK | LPI2C_MSR_PLTF_MASK;
53
54 if (status) {
55 if (status & LPI2C_MSR_PLTF_MASK)
56 result = LPI2C_PIN_LOW_TIMEOUT_ERR;
57 else if (status & LPI2C_MSR_ALF_MASK)
58 result = LPI2C_ARB_LOST_ERR;
59 else if (status & LPI2C_MSR_NDF_MASK)
60 result = LPI2C_NAK_ERR;
61 else if (status & LPI2C_MSR_FEF_MASK)
62 result = LPI2C_FIFO_ERR;
63
64 /* clear status flags */
65 writel(0x7f00, &regs->msr);
66 /* reset fifos */
67 val = readl(&regs->mcr);
68 val |= LPI2C_MCR_RRF_MASK | LPI2C_MCR_RTF_MASK;
69 writel(val, &regs->mcr);
70 }
71
72 return result;
73}
74
Simon Glassba1dea42017-05-17 17:18:05 -060075static int bus_i2c_wait_for_tx_ready(struct imx_lpi2c_reg *regs)
Peng Fand684adb2017-02-24 09:54:18 +080076{
Peng Fand684adb2017-02-24 09:54:18 +080077 lpi2c_status_t result = LPI2C_SUCESS;
78 u32 txcount = 0;
79 ulong start_time = get_timer(0);
80
81 do {
82 txcount = LPI2C_MFSR_TXCOUNT(readl(&regs->mfsr));
83 txcount = LPI2C_FIFO_SIZE - txcount;
Simon Glassba1dea42017-05-17 17:18:05 -060084 result = imx_lpci2c_check_clear_error(regs);
Peng Fand684adb2017-02-24 09:54:18 +080085 if (result) {
86 debug("i2c: wait for tx ready: result 0x%x\n", result);
87 return result;
88 }
89 if (get_timer(start_time) > LPI2C_TIMEOUT_MS) {
90 debug("i2c: wait for tx ready: timeout\n");
91 return -1;
92 }
93 } while (!txcount);
94
95 return result;
96}
97
Ye Li2c695462018-07-08 11:46:43 +080098static int bus_i2c_send(struct udevice *bus, u8 *txbuf, int len)
Peng Fand684adb2017-02-24 09:54:18 +080099{
Ye Li890813f2020-06-09 20:29:50 -0700100 struct imx_lpi2c_bus *i2c_bus = dev_get_priv(bus);
101 struct imx_lpi2c_reg *regs = (struct imx_lpi2c_reg *)(i2c_bus->base);
Peng Fand684adb2017-02-24 09:54:18 +0800102 lpi2c_status_t result = LPI2C_SUCESS;
103
104 /* empty tx */
105 if (!len)
106 return result;
107
108 while (len--) {
Simon Glassba1dea42017-05-17 17:18:05 -0600109 result = bus_i2c_wait_for_tx_ready(regs);
Peng Fand684adb2017-02-24 09:54:18 +0800110 if (result) {
Anatolij Gustschin422f8a82018-10-18 16:36:01 +0200111 debug("i2c: send wait for tx ready: %d\n", result);
Peng Fand684adb2017-02-24 09:54:18 +0800112 return result;
113 }
114 writel(*txbuf++, &regs->mtdr);
115 }
116
117 return result;
118}
119
Ye Li2c695462018-07-08 11:46:43 +0800120static int bus_i2c_receive(struct udevice *bus, u8 *rxbuf, int len)
Peng Fand684adb2017-02-24 09:54:18 +0800121{
Ye Li890813f2020-06-09 20:29:50 -0700122 struct imx_lpi2c_bus *i2c_bus = dev_get_priv(bus);
123 struct imx_lpi2c_reg *regs = (struct imx_lpi2c_reg *)(i2c_bus->base);
Peng Fand684adb2017-02-24 09:54:18 +0800124 lpi2c_status_t result = LPI2C_SUCESS;
125 u32 val;
126 ulong start_time = get_timer(0);
127
128 /* empty read */
129 if (!len)
130 return result;
131
Simon Glassba1dea42017-05-17 17:18:05 -0600132 result = bus_i2c_wait_for_tx_ready(regs);
Peng Fand684adb2017-02-24 09:54:18 +0800133 if (result) {
134 debug("i2c: receive wait fot tx ready: %d\n", result);
135 return result;
136 }
137
138 /* clear all status flags */
139 writel(0x7f00, &regs->msr);
140 /* send receive command */
141 val = LPI2C_MTDR_CMD(0x1) | LPI2C_MTDR_DATA(len - 1);
142 writel(val, &regs->mtdr);
143
144 while (len--) {
145 do {
Simon Glassba1dea42017-05-17 17:18:05 -0600146 result = imx_lpci2c_check_clear_error(regs);
Peng Fand684adb2017-02-24 09:54:18 +0800147 if (result) {
Simon Glassba1dea42017-05-17 17:18:05 -0600148 debug("i2c: receive check clear error: %d\n",
149 result);
Peng Fand684adb2017-02-24 09:54:18 +0800150 return result;
151 }
152 if (get_timer(start_time) > LPI2C_TIMEOUT_MS) {
153 debug("i2c: receive mrdr: timeout\n");
154 return -1;
155 }
156 val = readl(&regs->mrdr);
157 } while (val & LPI2C_MRDR_RXEMPTY_MASK);
158 *rxbuf++ = LPI2C_MRDR_DATA(val);
159 }
160
161 return result;
162}
163
Ye Li2c695462018-07-08 11:46:43 +0800164static int bus_i2c_start(struct udevice *bus, u8 addr, u8 dir)
Peng Fand684adb2017-02-24 09:54:18 +0800165{
Heinrich Schuchardt1a5d4852018-03-18 11:14:56 +0100166 lpi2c_status_t result;
Ye Li890813f2020-06-09 20:29:50 -0700167 struct imx_lpi2c_bus *i2c_bus = dev_get_priv(bus);
168 struct imx_lpi2c_reg *regs = (struct imx_lpi2c_reg *)(i2c_bus->base);
Peng Fand684adb2017-02-24 09:54:18 +0800169 u32 val;
170
Simon Glassba1dea42017-05-17 17:18:05 -0600171 result = imx_lpci2c_check_busy_bus(regs);
Peng Fand684adb2017-02-24 09:54:18 +0800172 if (result) {
173 debug("i2c: start check busy bus: 0x%x\n", result);
Ye Li2c695462018-07-08 11:46:43 +0800174
175 /* Try to init the lpi2c then check the bus busy again */
Simon Glassf0c99c52020-01-23 11:48:22 -0700176 bus_i2c_init(bus, I2C_SPEED_STANDARD_RATE);
Ye Li2c695462018-07-08 11:46:43 +0800177 result = imx_lpci2c_check_busy_bus(regs);
178 if (result) {
179 printf("i2c: Error check busy bus: 0x%x\n", result);
180 return result;
181 }
Peng Fand684adb2017-02-24 09:54:18 +0800182 }
183 /* clear all status flags */
184 writel(0x7f00, &regs->msr);
185 /* turn off auto-stop condition */
186 val = readl(&regs->mcfgr1) & ~LPI2C_MCFGR1_AUTOSTOP_MASK;
187 writel(val, &regs->mcfgr1);
188 /* wait tx fifo ready */
Simon Glassba1dea42017-05-17 17:18:05 -0600189 result = bus_i2c_wait_for_tx_ready(regs);
Peng Fand684adb2017-02-24 09:54:18 +0800190 if (result) {
191 debug("i2c: start wait for tx ready: 0x%x\n", result);
192 return result;
193 }
194 /* issue start command */
195 val = LPI2C_MTDR_CMD(0x4) | (addr << 0x1) | dir;
196 writel(val, &regs->mtdr);
197
198 return result;
199}
Simon Glassba1dea42017-05-17 17:18:05 -0600200
Ye Li2c695462018-07-08 11:46:43 +0800201static int bus_i2c_stop(struct udevice *bus)
Peng Fand684adb2017-02-24 09:54:18 +0800202{
Heinrich Schuchardt1a5d4852018-03-18 11:14:56 +0100203 lpi2c_status_t result;
Ye Li890813f2020-06-09 20:29:50 -0700204 struct imx_lpi2c_bus *i2c_bus = dev_get_priv(bus);
205 struct imx_lpi2c_reg *regs = (struct imx_lpi2c_reg *)(i2c_bus->base);
Peng Fand684adb2017-02-24 09:54:18 +0800206 u32 status;
Gao Pan52d457b2018-07-08 11:46:41 +0800207 ulong start_time;
Peng Fand684adb2017-02-24 09:54:18 +0800208
Simon Glassba1dea42017-05-17 17:18:05 -0600209 result = bus_i2c_wait_for_tx_ready(regs);
Peng Fand684adb2017-02-24 09:54:18 +0800210 if (result) {
211 debug("i2c: stop wait for tx ready: 0x%x\n", result);
212 return result;
213 }
214
215 /* send stop command */
216 writel(LPI2C_MTDR_CMD(0x2), &regs->mtdr);
217
Gao Pan52d457b2018-07-08 11:46:41 +0800218 start_time = get_timer(0);
219 while (1) {
Peng Fand684adb2017-02-24 09:54:18 +0800220 status = readl(&regs->msr);
Simon Glassba1dea42017-05-17 17:18:05 -0600221 result = imx_lpci2c_check_clear_error(regs);
Peng Fand684adb2017-02-24 09:54:18 +0800222 /* stop detect flag */
223 if (status & LPI2C_MSR_SDF_MASK) {
224 /* clear stop flag */
225 status &= LPI2C_MSR_SDF_MASK;
226 writel(status, &regs->msr);
227 break;
228 }
Gao Pan52d457b2018-07-08 11:46:41 +0800229
230 if (get_timer(start_time) > LPI2C_NACK_TOUT_MS) {
231 debug("stop timeout\n");
232 return -ETIMEDOUT;
233 }
Peng Fand684adb2017-02-24 09:54:18 +0800234 }
235
236 return result;
237}
238
Ye Li2c695462018-07-08 11:46:43 +0800239static int bus_i2c_read(struct udevice *bus, u32 chip, u8 *buf, int len)
Peng Fand684adb2017-02-24 09:54:18 +0800240{
Heinrich Schuchardt1a5d4852018-03-18 11:14:56 +0100241 lpi2c_status_t result;
Peng Fand684adb2017-02-24 09:54:18 +0800242
Ye Li2c695462018-07-08 11:46:43 +0800243 result = bus_i2c_start(bus, chip, 1);
Peng Fand684adb2017-02-24 09:54:18 +0800244 if (result)
245 return result;
Ye Li2c695462018-07-08 11:46:43 +0800246 result = bus_i2c_receive(bus, buf, len);
Peng Fand684adb2017-02-24 09:54:18 +0800247 if (result)
248 return result;
Peng Fand684adb2017-02-24 09:54:18 +0800249
250 return result;
251}
252
Ye Li2c695462018-07-08 11:46:43 +0800253static int bus_i2c_write(struct udevice *bus, u32 chip, u8 *buf, int len)
Peng Fand684adb2017-02-24 09:54:18 +0800254{
Heinrich Schuchardt1a5d4852018-03-18 11:14:56 +0100255 lpi2c_status_t result;
Peng Fand684adb2017-02-24 09:54:18 +0800256
Ye Li2c695462018-07-08 11:46:43 +0800257 result = bus_i2c_start(bus, chip, 0);
Peng Fand684adb2017-02-24 09:54:18 +0800258 if (result)
259 return result;
Ye Li2c695462018-07-08 11:46:43 +0800260 result = bus_i2c_send(bus, buf, len);
Peng Fand684adb2017-02-24 09:54:18 +0800261 if (result)
262 return result;
Peng Fand684adb2017-02-24 09:54:18 +0800263
264 return result;
265}
266
267
Peng Fan5b269c42018-07-17 20:38:33 +0800268u32 __weak imx_get_i2cclk(u32 i2c_num)
269{
270 return 0;
271}
272
Peng Fand684adb2017-02-24 09:54:18 +0800273static int bus_i2c_set_bus_speed(struct udevice *bus, int speed)
274{
Peng Fan5b269c42018-07-17 20:38:33 +0800275 struct imx_lpi2c_bus *i2c_bus = dev_get_priv(bus);
Ye Li890813f2020-06-09 20:29:50 -0700276 struct imx_lpi2c_reg *regs = (struct imx_lpi2c_reg *)(i2c_bus->base);
Peng Fand684adb2017-02-24 09:54:18 +0800277 u32 val;
278 u32 preescale = 0, best_pre = 0, clkhi = 0;
279 u32 best_clkhi = 0, abs_error = 0, rate;
280 u32 error = 0xffffffff;
281 u32 clock_rate;
282 bool mode;
283 int i;
284
Peng Fan5b269c42018-07-17 20:38:33 +0800285 if (IS_ENABLED(CONFIG_CLK)) {
286 clock_rate = clk_get_rate(&i2c_bus->per_clk);
287 if (clock_rate <= 0) {
288 dev_err(bus, "Failed to get i2c clk: %d\n", clock_rate);
289 return clock_rate;
290 }
291 } else {
292 clock_rate = imx_get_i2cclk(bus->seq);
293 if (!clock_rate)
294 return -EPERM;
295 }
Peng Fand684adb2017-02-24 09:54:18 +0800296
297 mode = (readl(&regs->mcr) & LPI2C_MCR_MEN_MASK) >> LPI2C_MCR_MEN_SHIFT;
298 /* disable master mode */
299 val = readl(&regs->mcr) & ~LPI2C_MCR_MEN_MASK;
300 writel(val | LPI2C_MCR_MEN(0), &regs->mcr);
301
302 for (preescale = 1; (preescale <= 128) &&
303 (error != 0); preescale = 2 * preescale) {
304 for (clkhi = 1; clkhi < 32; clkhi++) {
305 if (clkhi == 1)
306 rate = (clock_rate / preescale) / (1 + 3 + 2 + 2 / preescale);
307 else
308 rate = (clock_rate / preescale / (3 * clkhi + 2 + 2 / preescale));
309
310 abs_error = speed > rate ? speed - rate : rate - speed;
311
312 if (abs_error < error) {
313 best_pre = preescale;
314 best_clkhi = clkhi;
315 error = abs_error;
316 if (abs_error == 0)
317 break;
318 }
319 }
320 }
321
322 /* Standard, fast, fast mode plus and ultra-fast transfers. */
323 val = LPI2C_MCCR0_CLKHI(best_clkhi);
324 if (best_clkhi < 2)
325 val |= LPI2C_MCCR0_CLKLO(3) | LPI2C_MCCR0_SETHOLD(2) | LPI2C_MCCR0_DATAVD(1);
326 else
327 val |= LPI2C_MCCR0_CLKLO(2 * best_clkhi) | LPI2C_MCCR0_SETHOLD(best_clkhi) |
328 LPI2C_MCCR0_DATAVD(best_clkhi / 2);
329 writel(val, &regs->mccr0);
330
331 for (i = 0; i < 8; i++) {
332 if (best_pre == (1 << i)) {
333 best_pre = i;
334 break;
335 }
336 }
337
338 val = readl(&regs->mcfgr1) & ~LPI2C_MCFGR1_PRESCALE_MASK;
339 writel(val | LPI2C_MCFGR1_PRESCALE(best_pre), &regs->mcfgr1);
340
341 if (mode) {
342 val = readl(&regs->mcr) & ~LPI2C_MCR_MEN_MASK;
343 writel(val | LPI2C_MCR_MEN(1), &regs->mcr);
344 }
345
346 return 0;
347}
348
349static int bus_i2c_init(struct udevice *bus, int speed)
350{
Peng Fand684adb2017-02-24 09:54:18 +0800351 u32 val;
352 int ret;
353
Ye Li890813f2020-06-09 20:29:50 -0700354 struct imx_lpi2c_bus *i2c_bus = dev_get_priv(bus);
355 struct imx_lpi2c_reg *regs = (struct imx_lpi2c_reg *)(i2c_bus->base);
Peng Fand684adb2017-02-24 09:54:18 +0800356 /* reset peripheral */
357 writel(LPI2C_MCR_RST_MASK, &regs->mcr);
358 writel(0x0, &regs->mcr);
359 /* Disable Dozen mode */
360 writel(LPI2C_MCR_DBGEN(0) | LPI2C_MCR_DOZEN(1), &regs->mcr);
361 /* host request disable, active high, external pin */
362 val = readl(&regs->mcfgr0);
363 val &= (~(LPI2C_MCFGR0_HREN_MASK | LPI2C_MCFGR0_HRPOL_MASK |
364 LPI2C_MCFGR0_HRSEL_MASK));
365 val |= LPI2C_MCFGR0_HRPOL(0x1);
366 writel(val, &regs->mcfgr0);
367 /* pincfg and ignore ack */
368 val = readl(&regs->mcfgr1);
369 val &= ~(LPI2C_MCFGR1_PINCFG_MASK | LPI2C_MCFGR1_IGNACK_MASK);
370 val |= LPI2C_MCFGR1_PINCFG(0x0); /* 2 pin open drain */
371 val |= LPI2C_MCFGR1_IGNACK(0x0); /* ignore nack */
372 writel(val, &regs->mcfgr1);
373
374 ret = bus_i2c_set_bus_speed(bus, speed);
375
376 /* enable lpi2c in master mode */
377 val = readl(&regs->mcr) & ~LPI2C_MCR_MEN_MASK;
378 writel(val | LPI2C_MCR_MEN(1), &regs->mcr);
379
380 debug("i2c : controller bus %d, speed %d:\n", bus->seq, speed);
381
382 return ret;
383}
384
385static int imx_lpi2c_probe_chip(struct udevice *bus, u32 chip,
386 u32 chip_flags)
387{
Heinrich Schuchardt1a5d4852018-03-18 11:14:56 +0100388 lpi2c_status_t result;
Peng Fand684adb2017-02-24 09:54:18 +0800389
Ye Li2c695462018-07-08 11:46:43 +0800390 result = bus_i2c_start(bus, chip, 0);
Peng Fand684adb2017-02-24 09:54:18 +0800391 if (result) {
Ye Li2c695462018-07-08 11:46:43 +0800392 bus_i2c_stop(bus);
Simon Glassf0c99c52020-01-23 11:48:22 -0700393 bus_i2c_init(bus, I2C_SPEED_STANDARD_RATE);
Peng Fand684adb2017-02-24 09:54:18 +0800394 return result;
395 }
396
Ye Li2c695462018-07-08 11:46:43 +0800397 result = bus_i2c_stop(bus);
Gao Pan52d457b2018-07-08 11:46:41 +0800398 if (result)
Simon Glassf0c99c52020-01-23 11:48:22 -0700399 bus_i2c_init(bus, I2C_SPEED_STANDARD_RATE);
Peng Fand684adb2017-02-24 09:54:18 +0800400
401 return result;
402}
403
404static int imx_lpi2c_xfer(struct udevice *bus, struct i2c_msg *msg, int nmsgs)
405{
Ye Lia917ed92018-07-08 11:46:42 +0800406 int ret = 0, ret_stop;
Peng Fand684adb2017-02-24 09:54:18 +0800407
408 for (; nmsgs > 0; nmsgs--, msg++) {
409 debug("i2c_xfer: chip=0x%x, len=0x%x\n", msg->addr, msg->len);
410 if (msg->flags & I2C_M_RD)
Ye Li2c695462018-07-08 11:46:43 +0800411 ret = bus_i2c_read(bus, msg->addr, msg->buf, msg->len);
Peng Fand684adb2017-02-24 09:54:18 +0800412 else {
Ye Li2c695462018-07-08 11:46:43 +0800413 ret = bus_i2c_write(bus, msg->addr, msg->buf,
Peng Fand684adb2017-02-24 09:54:18 +0800414 msg->len);
415 if (ret)
416 break;
417 }
418 }
419
420 if (ret)
421 debug("i2c_write: error sending\n");
422
Ye Li2c695462018-07-08 11:46:43 +0800423 ret_stop = bus_i2c_stop(bus);
Ye Lia917ed92018-07-08 11:46:42 +0800424 if (ret_stop)
425 debug("i2c_xfer: stop bus error\n");
426
427 ret |= ret_stop;
428
Peng Fand684adb2017-02-24 09:54:18 +0800429 return ret;
430}
431
432static int imx_lpi2c_set_bus_speed(struct udevice *bus, unsigned int speed)
433{
434 return bus_i2c_set_bus_speed(bus, speed);
435}
436
Peng Fan5b269c42018-07-17 20:38:33 +0800437__weak int enable_i2c_clk(unsigned char enable, unsigned int i2c_num)
438{
439 return 0;
440}
441
Peng Fand684adb2017-02-24 09:54:18 +0800442static int imx_lpi2c_probe(struct udevice *bus)
443{
444 struct imx_lpi2c_bus *i2c_bus = dev_get_priv(bus);
445 fdt_addr_t addr;
446 int ret;
447
448 i2c_bus->driver_data = dev_get_driver_data(bus);
449
Simon Glassba1dea42017-05-17 17:18:05 -0600450 addr = devfdt_get_addr(bus);
Peng Fand684adb2017-02-24 09:54:18 +0800451 if (addr == FDT_ADDR_T_NONE)
Simon Glassf44b4bf2017-09-17 16:54:53 -0600452 return -EINVAL;
Peng Fand684adb2017-02-24 09:54:18 +0800453
454 i2c_bus->base = addr;
455 i2c_bus->index = bus->seq;
456 i2c_bus->bus = bus;
457
458 /* power up i2c resource */
Peng Fan32306dd2018-01-02 15:41:52 +0800459 ret = init_i2c_power(bus->seq);
Peng Fand684adb2017-02-24 09:54:18 +0800460 if (ret) {
461 debug("init_i2c_power err = %d\n", ret);
462 return ret;
463 }
464
Peng Fan5b269c42018-07-17 20:38:33 +0800465 if (IS_ENABLED(CONFIG_CLK)) {
466 ret = clk_get_by_name(bus, "per", &i2c_bus->per_clk);
467 if (ret) {
468 dev_err(bus, "Failed to get per clk\n");
469 return ret;
470 }
471 ret = clk_enable(&i2c_bus->per_clk);
472 if (ret) {
473 dev_err(bus, "Failed to enable per clk\n");
474 return ret;
475 }
Peng Fanf0fb3862019-07-24 08:54:16 +0000476
477 ret = clk_get_by_name(bus, "ipg", &i2c_bus->ipg_clk);
478 if (ret) {
479 dev_err(bus, "Failed to get ipg clk\n");
480 return ret;
481 }
482 ret = clk_enable(&i2c_bus->ipg_clk);
483 if (ret) {
484 dev_err(bus, "Failed to enable ipg clk\n");
485 return ret;
486 }
Peng Fan5b269c42018-07-17 20:38:33 +0800487 } else {
488 /* To i.MX7ULP, only i2c4-7 can be handled by A7 core */
489 ret = enable_i2c_clk(1, bus->seq);
490 if (ret < 0)
491 return ret;
492 }
Peng Fand684adb2017-02-24 09:54:18 +0800493
Simon Glassf0c99c52020-01-23 11:48:22 -0700494 ret = bus_i2c_init(bus, I2C_SPEED_STANDARD_RATE);
Peng Fand684adb2017-02-24 09:54:18 +0800495 if (ret < 0)
496 return ret;
497
Anatolij Gustschin422f8a82018-10-18 16:36:01 +0200498 debug("i2c : controller bus %d at 0x%lx , speed %d: ",
Peng Fand684adb2017-02-24 09:54:18 +0800499 bus->seq, i2c_bus->base,
500 i2c_bus->speed);
501
502 return 0;
503}
504
505static const struct dm_i2c_ops imx_lpi2c_ops = {
506 .xfer = imx_lpi2c_xfer,
507 .probe_chip = imx_lpi2c_probe_chip,
508 .set_bus_speed = imx_lpi2c_set_bus_speed,
509};
510
511static const struct udevice_id imx_lpi2c_ids[] = {
512 { .compatible = "fsl,imx7ulp-lpi2c", },
Ye Lid9103f82018-07-08 11:46:40 +0800513 { .compatible = "fsl,imx8qm-lpi2c", },
Peng Fand684adb2017-02-24 09:54:18 +0800514 {}
515};
516
517U_BOOT_DRIVER(imx_lpi2c) = {
518 .name = "imx_lpi2c",
519 .id = UCLASS_I2C,
520 .of_match = imx_lpi2c_ids,
521 .probe = imx_lpi2c_probe,
522 .priv_auto_alloc_size = sizeof(struct imx_lpi2c_bus),
523 .ops = &imx_lpi2c_ops,
524};