blob: 6c0d8eb5f4f532503f7b1595c5fc3189ed1e091f [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
Peng Fand684adb2017-02-24 09:54:18 +08006#include <errno.h>
Simon Glass0f2af882020-05-10 11:40:05 -06007#include <log.h>
Peng Fand684adb2017-02-24 09:54:18 +08008#include <asm/io.h>
9#include <asm/arch/clock.h>
10#include <asm/arch/imx-regs.h>
Ye Lid9103f82018-07-08 11:46:40 +080011#include <imx_lpi2c.h>
Peng Fand684adb2017-02-24 09:54:18 +080012#include <asm/arch/sys_proto.h>
13#include <dm.h>
14#include <fdtdec.h>
15#include <i2c.h>
Simon Glass9bc15642020-02-03 07:36:16 -070016#include <dm/device_compat.h>
Peng Fand684adb2017-02-24 09:54:18 +080017
Peng Fand684adb2017-02-24 09:54:18 +080018#define LPI2C_FIFO_SIZE 4
Gao Pan52d457b2018-07-08 11:46:41 +080019#define LPI2C_NACK_TOUT_MS 1
Peng Fand684adb2017-02-24 09:54:18 +080020#define LPI2C_TIMEOUT_MS 100
21
Ye Li2c695462018-07-08 11:46:43 +080022static int bus_i2c_init(struct udevice *bus, int speed);
23
Peng Fand684adb2017-02-24 09:54:18 +080024/* Weak linked function for overridden by some SoC power function */
25int __weak init_i2c_power(unsigned i2c_num)
26{
27 return 0;
28}
29
Simon Glassba1dea42017-05-17 17:18:05 -060030static int imx_lpci2c_check_busy_bus(const struct imx_lpi2c_reg *regs)
Peng Fand684adb2017-02-24 09:54:18 +080031{
Peng Fand684adb2017-02-24 09:54:18 +080032 lpi2c_status_t result = LPI2C_SUCESS;
33 u32 status;
34
35 status = readl(&regs->msr);
36
37 if ((status & LPI2C_MSR_BBF_MASK) && !(status & LPI2C_MSR_MBF_MASK))
38 result = LPI2C_BUSY;
39
40 return result;
41}
42
Simon Glassba1dea42017-05-17 17:18:05 -060043static int imx_lpci2c_check_clear_error(struct imx_lpi2c_reg *regs)
Peng Fand684adb2017-02-24 09:54:18 +080044{
Peng Fand684adb2017-02-24 09:54:18 +080045 lpi2c_status_t result = LPI2C_SUCESS;
46 u32 val, status;
47
48 status = readl(&regs->msr);
49 /* errors to check for */
50 status &= LPI2C_MSR_NDF_MASK | LPI2C_MSR_ALF_MASK |
51 LPI2C_MSR_FEF_MASK | LPI2C_MSR_PLTF_MASK;
52
53 if (status) {
54 if (status & LPI2C_MSR_PLTF_MASK)
55 result = LPI2C_PIN_LOW_TIMEOUT_ERR;
56 else if (status & LPI2C_MSR_ALF_MASK)
57 result = LPI2C_ARB_LOST_ERR;
58 else if (status & LPI2C_MSR_NDF_MASK)
59 result = LPI2C_NAK_ERR;
60 else if (status & LPI2C_MSR_FEF_MASK)
61 result = LPI2C_FIFO_ERR;
62
63 /* clear status flags */
64 writel(0x7f00, &regs->msr);
65 /* reset fifos */
66 val = readl(&regs->mcr);
67 val |= LPI2C_MCR_RRF_MASK | LPI2C_MCR_RTF_MASK;
68 writel(val, &regs->mcr);
69 }
70
71 return result;
72}
73
Simon Glassba1dea42017-05-17 17:18:05 -060074static int bus_i2c_wait_for_tx_ready(struct imx_lpi2c_reg *regs)
Peng Fand684adb2017-02-24 09:54:18 +080075{
Peng Fand684adb2017-02-24 09:54:18 +080076 lpi2c_status_t result = LPI2C_SUCESS;
77 u32 txcount = 0;
78 ulong start_time = get_timer(0);
79
80 do {
81 txcount = LPI2C_MFSR_TXCOUNT(readl(&regs->mfsr));
82 txcount = LPI2C_FIFO_SIZE - txcount;
Simon Glassba1dea42017-05-17 17:18:05 -060083 result = imx_lpci2c_check_clear_error(regs);
Peng Fand684adb2017-02-24 09:54:18 +080084 if (result) {
85 debug("i2c: wait for tx ready: result 0x%x\n", result);
86 return result;
87 }
88 if (get_timer(start_time) > LPI2C_TIMEOUT_MS) {
89 debug("i2c: wait for tx ready: timeout\n");
90 return -1;
91 }
92 } while (!txcount);
93
94 return result;
95}
96
Ye Li2c695462018-07-08 11:46:43 +080097static int bus_i2c_send(struct udevice *bus, u8 *txbuf, int len)
Peng Fand684adb2017-02-24 09:54:18 +080098{
Ye Li890813f2020-06-09 20:29:50 -070099 struct imx_lpi2c_bus *i2c_bus = dev_get_priv(bus);
100 struct imx_lpi2c_reg *regs = (struct imx_lpi2c_reg *)(i2c_bus->base);
Peng Fand684adb2017-02-24 09:54:18 +0800101 lpi2c_status_t result = LPI2C_SUCESS;
102
103 /* empty tx */
104 if (!len)
105 return result;
106
107 while (len--) {
Simon Glassba1dea42017-05-17 17:18:05 -0600108 result = bus_i2c_wait_for_tx_ready(regs);
Peng Fand684adb2017-02-24 09:54:18 +0800109 if (result) {
Anatolij Gustschin422f8a82018-10-18 16:36:01 +0200110 debug("i2c: send wait for tx ready: %d\n", result);
Peng Fand684adb2017-02-24 09:54:18 +0800111 return result;
112 }
113 writel(*txbuf++, &regs->mtdr);
114 }
115
116 return result;
117}
118
Ye Li2c695462018-07-08 11:46:43 +0800119static int bus_i2c_receive(struct udevice *bus, u8 *rxbuf, int len)
Peng Fand684adb2017-02-24 09:54:18 +0800120{
Ye Li890813f2020-06-09 20:29:50 -0700121 struct imx_lpi2c_bus *i2c_bus = dev_get_priv(bus);
122 struct imx_lpi2c_reg *regs = (struct imx_lpi2c_reg *)(i2c_bus->base);
Peng Fand684adb2017-02-24 09:54:18 +0800123 lpi2c_status_t result = LPI2C_SUCESS;
124 u32 val;
125 ulong start_time = get_timer(0);
126
127 /* empty read */
128 if (!len)
129 return result;
130
Simon Glassba1dea42017-05-17 17:18:05 -0600131 result = bus_i2c_wait_for_tx_ready(regs);
Peng Fand684adb2017-02-24 09:54:18 +0800132 if (result) {
133 debug("i2c: receive wait fot tx ready: %d\n", result);
134 return result;
135 }
136
137 /* clear all status flags */
138 writel(0x7f00, &regs->msr);
139 /* send receive command */
140 val = LPI2C_MTDR_CMD(0x1) | LPI2C_MTDR_DATA(len - 1);
141 writel(val, &regs->mtdr);
142
143 while (len--) {
144 do {
Simon Glassba1dea42017-05-17 17:18:05 -0600145 result = imx_lpci2c_check_clear_error(regs);
Peng Fand684adb2017-02-24 09:54:18 +0800146 if (result) {
Simon Glassba1dea42017-05-17 17:18:05 -0600147 debug("i2c: receive check clear error: %d\n",
148 result);
Peng Fand684adb2017-02-24 09:54:18 +0800149 return result;
150 }
151 if (get_timer(start_time) > LPI2C_TIMEOUT_MS) {
152 debug("i2c: receive mrdr: timeout\n");
153 return -1;
154 }
155 val = readl(&regs->mrdr);
156 } while (val & LPI2C_MRDR_RXEMPTY_MASK);
157 *rxbuf++ = LPI2C_MRDR_DATA(val);
158 }
159
160 return result;
161}
162
Ye Li2c695462018-07-08 11:46:43 +0800163static int bus_i2c_start(struct udevice *bus, u8 addr, u8 dir)
Peng Fand684adb2017-02-24 09:54:18 +0800164{
Heinrich Schuchardt1a5d4852018-03-18 11:14:56 +0100165 lpi2c_status_t result;
Ye Li890813f2020-06-09 20:29:50 -0700166 struct imx_lpi2c_bus *i2c_bus = dev_get_priv(bus);
167 struct imx_lpi2c_reg *regs = (struct imx_lpi2c_reg *)(i2c_bus->base);
Peng Fand684adb2017-02-24 09:54:18 +0800168 u32 val;
169
Simon Glassba1dea42017-05-17 17:18:05 -0600170 result = imx_lpci2c_check_busy_bus(regs);
Peng Fand684adb2017-02-24 09:54:18 +0800171 if (result) {
172 debug("i2c: start check busy bus: 0x%x\n", result);
Ye Li2c695462018-07-08 11:46:43 +0800173
174 /* Try to init the lpi2c then check the bus busy again */
Simon Glassf0c99c52020-01-23 11:48:22 -0700175 bus_i2c_init(bus, I2C_SPEED_STANDARD_RATE);
Ye Li2c695462018-07-08 11:46:43 +0800176 result = imx_lpci2c_check_busy_bus(regs);
177 if (result) {
178 printf("i2c: Error check busy bus: 0x%x\n", result);
179 return result;
180 }
Peng Fand684adb2017-02-24 09:54:18 +0800181 }
182 /* clear all status flags */
183 writel(0x7f00, &regs->msr);
184 /* turn off auto-stop condition */
185 val = readl(&regs->mcfgr1) & ~LPI2C_MCFGR1_AUTOSTOP_MASK;
186 writel(val, &regs->mcfgr1);
187 /* wait tx fifo ready */
Simon Glassba1dea42017-05-17 17:18:05 -0600188 result = bus_i2c_wait_for_tx_ready(regs);
Peng Fand684adb2017-02-24 09:54:18 +0800189 if (result) {
190 debug("i2c: start wait for tx ready: 0x%x\n", result);
191 return result;
192 }
193 /* issue start command */
194 val = LPI2C_MTDR_CMD(0x4) | (addr << 0x1) | dir;
195 writel(val, &regs->mtdr);
196
197 return result;
198}
Simon Glassba1dea42017-05-17 17:18:05 -0600199
Ye Li2c695462018-07-08 11:46:43 +0800200static int bus_i2c_stop(struct udevice *bus)
Peng Fand684adb2017-02-24 09:54:18 +0800201{
Heinrich Schuchardt1a5d4852018-03-18 11:14:56 +0100202 lpi2c_status_t result;
Ye Li890813f2020-06-09 20:29:50 -0700203 struct imx_lpi2c_bus *i2c_bus = dev_get_priv(bus);
204 struct imx_lpi2c_reg *regs = (struct imx_lpi2c_reg *)(i2c_bus->base);
Peng Fand684adb2017-02-24 09:54:18 +0800205 u32 status;
Gao Pan52d457b2018-07-08 11:46:41 +0800206 ulong start_time;
Peng Fand684adb2017-02-24 09:54:18 +0800207
Simon Glassba1dea42017-05-17 17:18:05 -0600208 result = bus_i2c_wait_for_tx_ready(regs);
Peng Fand684adb2017-02-24 09:54:18 +0800209 if (result) {
210 debug("i2c: stop wait for tx ready: 0x%x\n", result);
211 return result;
212 }
213
214 /* send stop command */
215 writel(LPI2C_MTDR_CMD(0x2), &regs->mtdr);
216
Gao Pan52d457b2018-07-08 11:46:41 +0800217 start_time = get_timer(0);
218 while (1) {
Peng Fand684adb2017-02-24 09:54:18 +0800219 status = readl(&regs->msr);
Simon Glassba1dea42017-05-17 17:18:05 -0600220 result = imx_lpci2c_check_clear_error(regs);
Peng Fand684adb2017-02-24 09:54:18 +0800221 /* stop detect flag */
222 if (status & LPI2C_MSR_SDF_MASK) {
223 /* clear stop flag */
224 status &= LPI2C_MSR_SDF_MASK;
225 writel(status, &regs->msr);
226 break;
227 }
Gao Pan52d457b2018-07-08 11:46:41 +0800228
229 if (get_timer(start_time) > LPI2C_NACK_TOUT_MS) {
230 debug("stop timeout\n");
231 return -ETIMEDOUT;
232 }
Peng Fand684adb2017-02-24 09:54:18 +0800233 }
234
235 return result;
236}
237
Ye Li2c695462018-07-08 11:46:43 +0800238static int bus_i2c_read(struct udevice *bus, u32 chip, u8 *buf, int len)
Peng Fand684adb2017-02-24 09:54:18 +0800239{
Heinrich Schuchardt1a5d4852018-03-18 11:14:56 +0100240 lpi2c_status_t result;
Peng Fand684adb2017-02-24 09:54:18 +0800241
Ye Li2c695462018-07-08 11:46:43 +0800242 result = bus_i2c_start(bus, chip, 1);
Peng Fand684adb2017-02-24 09:54:18 +0800243 if (result)
244 return result;
Ye Li2c695462018-07-08 11:46:43 +0800245 result = bus_i2c_receive(bus, buf, len);
Peng Fand684adb2017-02-24 09:54:18 +0800246 if (result)
247 return result;
Peng Fand684adb2017-02-24 09:54:18 +0800248
249 return result;
250}
251
Ye Li2c695462018-07-08 11:46:43 +0800252static int bus_i2c_write(struct udevice *bus, u32 chip, u8 *buf, int len)
Peng Fand684adb2017-02-24 09:54:18 +0800253{
Heinrich Schuchardt1a5d4852018-03-18 11:14:56 +0100254 lpi2c_status_t result;
Peng Fand684adb2017-02-24 09:54:18 +0800255
Ye Li2c695462018-07-08 11:46:43 +0800256 result = bus_i2c_start(bus, chip, 0);
Peng Fand684adb2017-02-24 09:54:18 +0800257 if (result)
258 return result;
Ye Li2c695462018-07-08 11:46:43 +0800259 result = bus_i2c_send(bus, buf, len);
Peng Fand684adb2017-02-24 09:54:18 +0800260 if (result)
261 return result;
Peng Fand684adb2017-02-24 09:54:18 +0800262
263 return result;
264}
265
266
Peng Fan5b269c42018-07-17 20:38:33 +0800267u32 __weak imx_get_i2cclk(u32 i2c_num)
268{
269 return 0;
270}
271
Peng Fand684adb2017-02-24 09:54:18 +0800272static int bus_i2c_set_bus_speed(struct udevice *bus, int speed)
273{
Peng Fan5b269c42018-07-17 20:38:33 +0800274 struct imx_lpi2c_bus *i2c_bus = dev_get_priv(bus);
Ye Li890813f2020-06-09 20:29:50 -0700275 struct imx_lpi2c_reg *regs = (struct imx_lpi2c_reg *)(i2c_bus->base);
Peng Fand684adb2017-02-24 09:54:18 +0800276 u32 val;
277 u32 preescale = 0, best_pre = 0, clkhi = 0;
278 u32 best_clkhi = 0, abs_error = 0, rate;
279 u32 error = 0xffffffff;
280 u32 clock_rate;
281 bool mode;
282 int i;
283
Ye Li725a7ac2023-04-06 18:26:35 +0800284 if (CONFIG_IS_ENABLED(CLK)) {
Peng Fan5b269c42018-07-17 20:38:33 +0800285 clock_rate = clk_get_rate(&i2c_bus->per_clk);
286 if (clock_rate <= 0) {
287 dev_err(bus, "Failed to get i2c clk: %d\n", clock_rate);
288 return clock_rate;
289 }
290 } else {
Simon Glass75e534b2020-12-16 21:20:07 -0700291 clock_rate = imx_get_i2cclk(dev_seq(bus));
Peng Fan5b269c42018-07-17 20:38:33 +0800292 if (!clock_rate)
293 return -EPERM;
294 }
Peng Fand684adb2017-02-24 09:54:18 +0800295
296 mode = (readl(&regs->mcr) & LPI2C_MCR_MEN_MASK) >> LPI2C_MCR_MEN_SHIFT;
297 /* disable master mode */
298 val = readl(&regs->mcr) & ~LPI2C_MCR_MEN_MASK;
299 writel(val | LPI2C_MCR_MEN(0), &regs->mcr);
300
301 for (preescale = 1; (preescale <= 128) &&
302 (error != 0); preescale = 2 * preescale) {
303 for (clkhi = 1; clkhi < 32; clkhi++) {
304 if (clkhi == 1)
305 rate = (clock_rate / preescale) / (1 + 3 + 2 + 2 / preescale);
306 else
307 rate = (clock_rate / preescale / (3 * clkhi + 2 + 2 / preescale));
308
309 abs_error = speed > rate ? speed - rate : rate - speed;
310
311 if (abs_error < error) {
312 best_pre = preescale;
313 best_clkhi = clkhi;
314 error = abs_error;
315 if (abs_error == 0)
316 break;
317 }
318 }
319 }
320
321 /* Standard, fast, fast mode plus and ultra-fast transfers. */
322 val = LPI2C_MCCR0_CLKHI(best_clkhi);
323 if (best_clkhi < 2)
324 val |= LPI2C_MCCR0_CLKLO(3) | LPI2C_MCCR0_SETHOLD(2) | LPI2C_MCCR0_DATAVD(1);
325 else
326 val |= LPI2C_MCCR0_CLKLO(2 * best_clkhi) | LPI2C_MCCR0_SETHOLD(best_clkhi) |
327 LPI2C_MCCR0_DATAVD(best_clkhi / 2);
328 writel(val, &regs->mccr0);
329
330 for (i = 0; i < 8; i++) {
331 if (best_pre == (1 << i)) {
332 best_pre = i;
333 break;
334 }
335 }
336
337 val = readl(&regs->mcfgr1) & ~LPI2C_MCFGR1_PRESCALE_MASK;
338 writel(val | LPI2C_MCFGR1_PRESCALE(best_pre), &regs->mcfgr1);
339
340 if (mode) {
341 val = readl(&regs->mcr) & ~LPI2C_MCR_MEN_MASK;
342 writel(val | LPI2C_MCR_MEN(1), &regs->mcr);
343 }
344
345 return 0;
346}
347
348static int bus_i2c_init(struct udevice *bus, int speed)
349{
Peng Fand684adb2017-02-24 09:54:18 +0800350 u32 val;
351 int ret;
352
Ye Li890813f2020-06-09 20:29:50 -0700353 struct imx_lpi2c_bus *i2c_bus = dev_get_priv(bus);
354 struct imx_lpi2c_reg *regs = (struct imx_lpi2c_reg *)(i2c_bus->base);
Peng Fand684adb2017-02-24 09:54:18 +0800355 /* reset peripheral */
356 writel(LPI2C_MCR_RST_MASK, &regs->mcr);
357 writel(0x0, &regs->mcr);
358 /* Disable Dozen mode */
359 writel(LPI2C_MCR_DBGEN(0) | LPI2C_MCR_DOZEN(1), &regs->mcr);
360 /* host request disable, active high, external pin */
361 val = readl(&regs->mcfgr0);
362 val &= (~(LPI2C_MCFGR0_HREN_MASK | LPI2C_MCFGR0_HRPOL_MASK |
363 LPI2C_MCFGR0_HRSEL_MASK));
364 val |= LPI2C_MCFGR0_HRPOL(0x1);
365 writel(val, &regs->mcfgr0);
366 /* pincfg and ignore ack */
367 val = readl(&regs->mcfgr1);
368 val &= ~(LPI2C_MCFGR1_PINCFG_MASK | LPI2C_MCFGR1_IGNACK_MASK);
369 val |= LPI2C_MCFGR1_PINCFG(0x0); /* 2 pin open drain */
370 val |= LPI2C_MCFGR1_IGNACK(0x0); /* ignore nack */
371 writel(val, &regs->mcfgr1);
372
373 ret = bus_i2c_set_bus_speed(bus, speed);
374
375 /* enable lpi2c in master mode */
376 val = readl(&regs->mcr) & ~LPI2C_MCR_MEN_MASK;
377 writel(val | LPI2C_MCR_MEN(1), &regs->mcr);
378
Simon Glass75e534b2020-12-16 21:20:07 -0700379 debug("i2c : controller bus %d, speed %d:\n", dev_seq(bus), speed);
Peng Fand684adb2017-02-24 09:54:18 +0800380
381 return ret;
382}
383
384static int imx_lpi2c_probe_chip(struct udevice *bus, u32 chip,
385 u32 chip_flags)
386{
Heinrich Schuchardt1a5d4852018-03-18 11:14:56 +0100387 lpi2c_status_t result;
Peng Fand684adb2017-02-24 09:54:18 +0800388
Ye Li2c695462018-07-08 11:46:43 +0800389 result = bus_i2c_start(bus, chip, 0);
Peng Fand684adb2017-02-24 09:54:18 +0800390 if (result) {
Ye Li2c695462018-07-08 11:46:43 +0800391 bus_i2c_stop(bus);
Simon Glassf0c99c52020-01-23 11:48:22 -0700392 bus_i2c_init(bus, I2C_SPEED_STANDARD_RATE);
Peng Fand684adb2017-02-24 09:54:18 +0800393 return result;
394 }
395
Ye Li2c695462018-07-08 11:46:43 +0800396 result = bus_i2c_stop(bus);
Gao Pan52d457b2018-07-08 11:46:41 +0800397 if (result)
Simon Glassf0c99c52020-01-23 11:48:22 -0700398 bus_i2c_init(bus, I2C_SPEED_STANDARD_RATE);
Peng Fand684adb2017-02-24 09:54:18 +0800399
400 return result;
401}
402
403static int imx_lpi2c_xfer(struct udevice *bus, struct i2c_msg *msg, int nmsgs)
404{
Ye Lia917ed92018-07-08 11:46:42 +0800405 int ret = 0, ret_stop;
Peng Fand684adb2017-02-24 09:54:18 +0800406
407 for (; nmsgs > 0; nmsgs--, msg++) {
408 debug("i2c_xfer: chip=0x%x, len=0x%x\n", msg->addr, msg->len);
409 if (msg->flags & I2C_M_RD)
Ye Li2c695462018-07-08 11:46:43 +0800410 ret = bus_i2c_read(bus, msg->addr, msg->buf, msg->len);
Peng Fand684adb2017-02-24 09:54:18 +0800411 else {
Ye Li2c695462018-07-08 11:46:43 +0800412 ret = bus_i2c_write(bus, msg->addr, msg->buf,
Peng Fand684adb2017-02-24 09:54:18 +0800413 msg->len);
414 if (ret)
415 break;
416 }
417 }
418
419 if (ret)
420 debug("i2c_write: error sending\n");
421
Ye Li2c695462018-07-08 11:46:43 +0800422 ret_stop = bus_i2c_stop(bus);
Ye Lia917ed92018-07-08 11:46:42 +0800423 if (ret_stop)
424 debug("i2c_xfer: stop bus error\n");
425
426 ret |= ret_stop;
427
Peng Fand684adb2017-02-24 09:54:18 +0800428 return ret;
429}
430
431static int imx_lpi2c_set_bus_speed(struct udevice *bus, unsigned int speed)
432{
433 return bus_i2c_set_bus_speed(bus, speed);
434}
435
Peng Fan5b269c42018-07-17 20:38:33 +0800436__weak int enable_i2c_clk(unsigned char enable, unsigned int i2c_num)
437{
438 return 0;
439}
440
Peng Fand684adb2017-02-24 09:54:18 +0800441static int imx_lpi2c_probe(struct udevice *bus)
442{
443 struct imx_lpi2c_bus *i2c_bus = dev_get_priv(bus);
444 fdt_addr_t addr;
445 int ret;
446
447 i2c_bus->driver_data = dev_get_driver_data(bus);
448
Masahiro Yamadaa89b4de2020-07-17 14:36:48 +0900449 addr = dev_read_addr(bus);
Peng Fand684adb2017-02-24 09:54:18 +0800450 if (addr == FDT_ADDR_T_NONE)
Simon Glassf44b4bf2017-09-17 16:54:53 -0600451 return -EINVAL;
Peng Fand684adb2017-02-24 09:54:18 +0800452
453 i2c_bus->base = addr;
Simon Glass75e534b2020-12-16 21:20:07 -0700454 i2c_bus->index = dev_seq(bus);
Peng Fand684adb2017-02-24 09:54:18 +0800455 i2c_bus->bus = bus;
456
457 /* power up i2c resource */
Simon Glass75e534b2020-12-16 21:20:07 -0700458 ret = init_i2c_power(dev_seq(bus));
Peng Fand684adb2017-02-24 09:54:18 +0800459 if (ret) {
460 debug("init_i2c_power err = %d\n", ret);
461 return ret;
462 }
463
Ye Li725a7ac2023-04-06 18:26:35 +0800464 if (CONFIG_IS_ENABLED(CLK)) {
Peng Fan5b269c42018-07-17 20:38:33 +0800465 ret = clk_get_by_name(bus, "per", &i2c_bus->per_clk);
466 if (ret) {
467 dev_err(bus, "Failed to get per clk\n");
468 return ret;
469 }
470 ret = clk_enable(&i2c_bus->per_clk);
471 if (ret) {
472 dev_err(bus, "Failed to enable per clk\n");
473 return ret;
474 }
Peng Fanf0fb3862019-07-24 08:54:16 +0000475
476 ret = clk_get_by_name(bus, "ipg", &i2c_bus->ipg_clk);
477 if (ret) {
478 dev_err(bus, "Failed to get ipg clk\n");
479 return ret;
480 }
481 ret = clk_enable(&i2c_bus->ipg_clk);
482 if (ret) {
483 dev_err(bus, "Failed to enable ipg clk\n");
484 return ret;
485 }
Peng Fan5b269c42018-07-17 20:38:33 +0800486 } else {
487 /* To i.MX7ULP, only i2c4-7 can be handled by A7 core */
Simon Glass75e534b2020-12-16 21:20:07 -0700488 ret = enable_i2c_clk(1, dev_seq(bus));
Peng Fan5b269c42018-07-17 20:38:33 +0800489 if (ret < 0)
490 return ret;
491 }
Peng Fand684adb2017-02-24 09:54:18 +0800492
Simon Glassf0c99c52020-01-23 11:48:22 -0700493 ret = bus_i2c_init(bus, I2C_SPEED_STANDARD_RATE);
Peng Fand684adb2017-02-24 09:54:18 +0800494 if (ret < 0)
495 return ret;
496
Anatolij Gustschin422f8a82018-10-18 16:36:01 +0200497 debug("i2c : controller bus %d at 0x%lx , speed %d: ",
Simon Glass75e534b2020-12-16 21:20:07 -0700498 dev_seq(bus), i2c_bus->base,
Peng Fand684adb2017-02-24 09:54:18 +0800499 i2c_bus->speed);
500
501 return 0;
502}
503
504static const struct dm_i2c_ops imx_lpi2c_ops = {
505 .xfer = imx_lpi2c_xfer,
506 .probe_chip = imx_lpi2c_probe_chip,
507 .set_bus_speed = imx_lpi2c_set_bus_speed,
508};
509
510static const struct udevice_id imx_lpi2c_ids[] = {
511 { .compatible = "fsl,imx7ulp-lpi2c", },
Ye Lid9103f82018-07-08 11:46:40 +0800512 { .compatible = "fsl,imx8qm-lpi2c", },
Peng Fand684adb2017-02-24 09:54:18 +0800513 {}
514};
515
516U_BOOT_DRIVER(imx_lpi2c) = {
517 .name = "imx_lpi2c",
518 .id = UCLASS_I2C,
519 .of_match = imx_lpi2c_ids,
520 .probe = imx_lpi2c_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700521 .priv_auto = sizeof(struct imx_lpi2c_bus),
Peng Fand684adb2017-02-24 09:54:18 +0800522 .ops = &imx_lpi2c_ops,
523};