1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * This is i.MX low power i2c controller driver.
4 *
5 * Copyright 2016 Freescale Semiconductor, Inc.
6 */
7
8 #include <linux/bitfield.h>
9 #include <linux/clk.h>
10 #include <linux/completion.h>
11 #include <linux/delay.h>
12 #include <linux/dma-mapping.h>
13 #include <linux/dmaengine.h>
14 #include <linux/err.h>
15 #include <linux/errno.h>
16 #include <linux/i2c.h>
17 #include <linux/init.h>
18 #include <linux/interrupt.h>
19 #include <linux/io.h>
20 #include <linux/iopoll.h>
21 #include <linux/kernel.h>
22 #include <linux/module.h>
23 #include <linux/of.h>
24 #include <linux/pinctrl/consumer.h>
25 #include <linux/platform_device.h>
26 #include <linux/pm_runtime.h>
27 #include <linux/sched.h>
28 #include <linux/slab.h>
29
30 #define DRIVER_NAME "imx-lpi2c"
31
32 #define LPI2C_VERID 0x00 /* i2c version ID */
33 #define LPI2C_PARAM 0x04 /* i2c RX/TX FIFO size */
34 #define LPI2C_MCR 0x10 /* i2c contrl register */
35 #define LPI2C_MSR 0x14 /* i2c status register */
36 #define LPI2C_MIER 0x18 /* i2c interrupt enable */
37 #define LPI2C_MDER 0x1C /* i2c DMA enable */
38 #define LPI2C_MCFGR0 0x20 /* i2c master configuration */
39 #define LPI2C_MCFGR1 0x24 /* i2c master configuration */
40 #define LPI2C_MCFGR2 0x28 /* i2c master configuration */
41 #define LPI2C_MCFGR3 0x2C /* i2c master configuration */
42 #define LPI2C_MCCR0 0x48 /* i2c master clk configuration */
43 #define LPI2C_MCCR1 0x50 /* i2c master clk configuration */
44 #define LPI2C_MFCR 0x58 /* i2c master FIFO control */
45 #define LPI2C_MFSR 0x5C /* i2c master FIFO status */
46 #define LPI2C_MTDR 0x60 /* i2c master TX data register */
47 #define LPI2C_MRDR 0x70 /* i2c master RX data register */
48
49 #define LPI2C_SCR 0x110 /* i2c target control register */
50 #define LPI2C_SSR 0x114 /* i2c target status register */
51 #define LPI2C_SIER 0x118 /* i2c target interrupt enable */
52 #define LPI2C_SDER 0x11C /* i2c target DMA enable */
53 #define LPI2C_SCFGR0 0x120 /* i2c target configuration */
54 #define LPI2C_SCFGR1 0x124 /* i2c target configuration */
55 #define LPI2C_SCFGR2 0x128 /* i2c target configuration */
56 #define LPI2C_SAMR 0x140 /* i2c target address match */
57 #define LPI2C_SASR 0x150 /* i2c target address status */
58 #define LPI2C_STAR 0x154 /* i2c target transmit ACK */
59 #define LPI2C_STDR 0x160 /* i2c target transmit data */
60 #define LPI2C_SRDR 0x170 /* i2c target receive data */
61 #define LPI2C_SRDROR 0x178 /* i2c target receive data read only */
62
63 /* i2c command */
64 #define TRAN_DATA 0X00
65 #define RECV_DATA 0X01
66 #define GEN_STOP 0X02
67 #define RECV_DISCARD 0X03
68 #define GEN_START 0X04
69 #define START_NACK 0X05
70 #define START_HIGH 0X06
71 #define START_HIGH_NACK 0X07
72
73 #define MCR_MEN BIT(0)
74 #define MCR_RST BIT(1)
75 #define MCR_DOZEN BIT(2)
76 #define MCR_DBGEN BIT(3)
77 #define MCR_RTF BIT(8)
78 #define MCR_RRF BIT(9)
79 #define MSR_TDF BIT(0)
80 #define MSR_RDF BIT(1)
81 #define MSR_SDF BIT(9)
82 #define MSR_NDF BIT(10)
83 #define MSR_ALF BIT(11)
84 #define MSR_MBF BIT(24)
85 #define MSR_BBF BIT(25)
86 #define MIER_TDIE BIT(0)
87 #define MIER_RDIE BIT(1)
88 #define MIER_SDIE BIT(9)
89 #define MIER_NDIE BIT(10)
90 #define MCFGR1_AUTOSTOP BIT(8)
91 #define MCFGR1_IGNACK BIT(9)
92 #define MRDR_RXEMPTY BIT(14)
93 #define MDER_TDDE BIT(0)
94 #define MDER_RDDE BIT(1)
95 #define MSR_RDF_ASSERTED(x) FIELD_GET(MSR_RDF, (x))
96
97 #define SCR_SEN BIT(0)
98 #define SCR_RST BIT(1)
99 #define SCR_FILTEN BIT(4)
100 #define SCR_RTF BIT(8)
101 #define SCR_RRF BIT(9)
102 #define SSR_TDF BIT(0)
103 #define SSR_RDF BIT(1)
104 #define SSR_AVF BIT(2)
105 #define SSR_TAF BIT(3)
106 #define SSR_RSF BIT(8)
107 #define SSR_SDF BIT(9)
108 #define SSR_BEF BIT(10)
109 #define SSR_FEF BIT(11)
110 #define SSR_SBF BIT(24)
111 #define SSR_BBF BIT(25)
112 #define SSR_CLEAR_BITS (SSR_RSF | SSR_SDF | SSR_BEF | SSR_FEF)
113 #define SIER_TDIE BIT(0)
114 #define SIER_RDIE BIT(1)
115 #define SIER_AVIE BIT(2)
116 #define SIER_TAIE BIT(3)
117 #define SIER_RSIE BIT(8)
118 #define SIER_SDIE BIT(9)
119 #define SIER_BEIE BIT(10)
120 #define SIER_FEIE BIT(11)
121 #define SIER_AM0F BIT(12)
122 #define SCFGR1_RXSTALL BIT(1)
123 #define SCFGR1_TXDSTALL BIT(2)
124 #define SCFGR2_FILTSDA_SHIFT 24
125 #define SCFGR2_FILTSCL_SHIFT 16
126 #define SCFGR2_CLKHOLD(x) (x)
127 #define SCFGR2_FILTSDA(x) ((x) << SCFGR2_FILTSDA_SHIFT)
128 #define SCFGR2_FILTSCL(x) ((x) << SCFGR2_FILTSCL_SHIFT)
129 #define SASR_READ_REQ 0x1
130 #define SLAVE_INT_FLAG (SIER_TDIE | SIER_RDIE | SIER_AVIE | \
131 SIER_SDIE | SIER_BEIE)
132
133 #define I2C_CLK_RATIO 2
134 #define CHUNK_DATA 256
135
136 #define I2C_PM_TIMEOUT 10 /* ms */
137 #define I2C_PM_LONG_TIMEOUT_MS 1000 /* Avoid dead lock caused by big clock prepare lock */
138 #define I2C_DMA_THRESHOLD 8 /* bytes */
139
140 /* Bit 0 indicates the presence of the target feature */
141 #define VERID_FEATURE_TARGET_PRESENT BIT(0)
142
143 enum lpi2c_imx_mode {
144 STANDARD, /* 100+Kbps */
145 FAST, /* 400+Kbps */
146 FAST_PLUS, /* 1.0+Mbps */
147 HS, /* 3.4+Mbps */
148 ULTRA_FAST, /* 5.0+Mbps */
149 };
150
151 enum lpi2c_imx_pincfg {
152 TWO_PIN_OD,
153 TWO_PIN_OO,
154 TWO_PIN_PP,
155 FOUR_PIN_PP,
156 };
157
158 struct imx_lpi2c_hwdata {
159 bool need_request_free_irq; /* Needed by irqsteer */
160 bool need_prepare_unprepare_clk; /* Needed by LPCG */
161 };
162
163 struct lpi2c_imx_dma {
164 bool using_pio_mode;
165 u8 rx_cmd_buf_len;
166 u8 *dma_buf;
167 u16 *rx_cmd_buf;
168 unsigned int dma_len;
169 unsigned int tx_burst_num;
170 unsigned int rx_burst_num;
171 unsigned long dma_msg_flag;
172 resource_size_t phy_addr;
173 dma_addr_t dma_tx_addr;
174 dma_addr_t dma_addr;
175 enum dma_data_direction dma_data_dir;
176 enum dma_transfer_direction dma_transfer_dir;
177 struct dma_chan *chan_tx;
178 struct dma_chan *chan_rx;
179 };
180
181 struct lpi2c_imx_struct {
182 struct i2c_adapter adapter;
183 int num_clks;
184 struct clk_bulk_data *clks;
185 void __iomem *base;
186 __u8 *rx_buf;
187 __u8 *tx_buf;
188 struct completion complete;
189 unsigned long rate_per;
190 unsigned int msglen;
191 unsigned int delivered;
192 unsigned int block_data;
193 unsigned int bitrate;
194 unsigned int txfifosize;
195 unsigned int rxfifosize;
196 enum lpi2c_imx_mode mode;
197 struct i2c_bus_recovery_info rinfo;
198 bool can_use_dma;
199 struct lpi2c_imx_dma *dma;
200 struct i2c_client *target;
201 bool target_supported;
202 int irq;
203 const struct imx_lpi2c_hwdata *hwdata;
204 };
205
206 static const struct imx_lpi2c_hwdata imx7ulp_lpi2c_hwdata = {
207 };
208
209 static const struct imx_lpi2c_hwdata imx8qxp_lpi2c_hwdata = {
210 .need_request_free_irq = true,
211 .need_prepare_unprepare_clk = true,
212 };
213
214 static const struct imx_lpi2c_hwdata imx8qm_lpi2c_hwdata = {
215 .need_request_free_irq = true,
216 .need_prepare_unprepare_clk = true,
217 };
218
219 #define lpi2c_imx_read_msr_poll_timeout(atomic, val, cond) \
220 (atomic ? readl_poll_timeout_atomic(lpi2c_imx->base + LPI2C_MSR, val, \
221 cond, 0, 500000) : \
222 readl_poll_timeout(lpi2c_imx->base + LPI2C_MSR, val, cond, \
223 0, 500000))
224
lpi2c_imx_intctrl(struct lpi2c_imx_struct * lpi2c_imx,unsigned int enable)225 static void lpi2c_imx_intctrl(struct lpi2c_imx_struct *lpi2c_imx,
226 unsigned int enable)
227 {
228 writel(enable, lpi2c_imx->base + LPI2C_MIER);
229 }
230
lpi2c_imx_bus_busy(struct lpi2c_imx_struct * lpi2c_imx,bool atomic)231 static int lpi2c_imx_bus_busy(struct lpi2c_imx_struct *lpi2c_imx, bool atomic)
232 {
233 unsigned int temp;
234 int err;
235
236 err = lpi2c_imx_read_msr_poll_timeout(atomic, temp,
237 temp & (MSR_ALF | MSR_BBF | MSR_MBF));
238
239 /* check for arbitration lost, clear if set */
240 if (temp & MSR_ALF) {
241 writel(temp, lpi2c_imx->base + LPI2C_MSR);
242 return -EAGAIN;
243 }
244
245 /* check for bus not busy */
246 if (err) {
247 dev_dbg(&lpi2c_imx->adapter.dev, "bus not work\n");
248 if (lpi2c_imx->adapter.bus_recovery_info)
249 i2c_recover_bus(&lpi2c_imx->adapter);
250 return -ETIMEDOUT;
251 }
252
253 return 0;
254 }
255
lpi2c_imx_txfifo_cnt(struct lpi2c_imx_struct * lpi2c_imx)256 static u32 lpi2c_imx_txfifo_cnt(struct lpi2c_imx_struct *lpi2c_imx)
257 {
258 return readl(lpi2c_imx->base + LPI2C_MFSR) & 0xff;
259 }
260
lpi2c_imx_set_mode(struct lpi2c_imx_struct * lpi2c_imx)261 static void lpi2c_imx_set_mode(struct lpi2c_imx_struct *lpi2c_imx)
262 {
263 unsigned int bitrate = lpi2c_imx->bitrate;
264 enum lpi2c_imx_mode mode;
265
266 if (bitrate < I2C_MAX_FAST_MODE_FREQ)
267 mode = STANDARD;
268 else if (bitrate < I2C_MAX_FAST_MODE_PLUS_FREQ)
269 mode = FAST;
270 else if (bitrate < I2C_MAX_HIGH_SPEED_MODE_FREQ)
271 mode = FAST_PLUS;
272 else if (bitrate < I2C_MAX_ULTRA_FAST_MODE_FREQ)
273 mode = HS;
274 else
275 mode = ULTRA_FAST;
276
277 lpi2c_imx->mode = mode;
278 }
279
lpi2c_imx_start(struct lpi2c_imx_struct * lpi2c_imx,struct i2c_msg * msgs,bool atomic)280 static int lpi2c_imx_start(struct lpi2c_imx_struct *lpi2c_imx,
281 struct i2c_msg *msgs, bool atomic)
282 {
283 unsigned int temp;
284
285 temp = readl(lpi2c_imx->base + LPI2C_MCR);
286 temp |= MCR_RRF | MCR_RTF;
287 writel(temp, lpi2c_imx->base + LPI2C_MCR);
288 writel(0x7f00, lpi2c_imx->base + LPI2C_MSR);
289
290 temp = i2c_8bit_addr_from_msg(msgs) | (GEN_START << 8);
291 writel(temp, lpi2c_imx->base + LPI2C_MTDR);
292
293 return lpi2c_imx_bus_busy(lpi2c_imx, atomic);
294 }
295
lpi2c_imx_stop(struct lpi2c_imx_struct * lpi2c_imx,bool atomic)296 static void lpi2c_imx_stop(struct lpi2c_imx_struct *lpi2c_imx, bool atomic)
297 {
298 unsigned int temp;
299 int err;
300
301 writel(GEN_STOP << 8, lpi2c_imx->base + LPI2C_MTDR);
302
303 err = lpi2c_imx_read_msr_poll_timeout(atomic, temp, temp & MSR_SDF);
304
305 if (err) {
306 dev_dbg(&lpi2c_imx->adapter.dev, "stop timeout\n");
307 if (lpi2c_imx->adapter.bus_recovery_info)
308 i2c_recover_bus(&lpi2c_imx->adapter);
309 }
310 }
311
312 /* CLKLO = I2C_CLK_RATIO * CLKHI, SETHOLD = CLKHI, DATAVD = CLKHI/2 */
lpi2c_imx_config(struct lpi2c_imx_struct * lpi2c_imx)313 static int lpi2c_imx_config(struct lpi2c_imx_struct *lpi2c_imx)
314 {
315 u8 prescale, filt, sethold, datavd;
316 unsigned int clk_rate, clk_cycle, clkhi, clklo;
317 enum lpi2c_imx_pincfg pincfg;
318 unsigned int temp;
319
320 lpi2c_imx_set_mode(lpi2c_imx);
321
322 clk_rate = lpi2c_imx->rate_per;
323
324 if (lpi2c_imx->mode == HS || lpi2c_imx->mode == ULTRA_FAST)
325 filt = 0;
326 else
327 filt = 2;
328
329 for (prescale = 0; prescale <= 7; prescale++) {
330 clk_cycle = clk_rate / ((1 << prescale) * lpi2c_imx->bitrate)
331 - 3 - (filt >> 1);
332 clkhi = DIV_ROUND_UP(clk_cycle, I2C_CLK_RATIO + 1);
333 clklo = clk_cycle - clkhi;
334 if (clklo < 64)
335 break;
336 }
337
338 if (prescale > 7)
339 return -EINVAL;
340
341 /* set MCFGR1: PINCFG, PRESCALE, IGNACK */
342 if (lpi2c_imx->mode == ULTRA_FAST)
343 pincfg = TWO_PIN_OO;
344 else
345 pincfg = TWO_PIN_OD;
346 temp = prescale | pincfg << 24;
347
348 if (lpi2c_imx->mode == ULTRA_FAST)
349 temp |= MCFGR1_IGNACK;
350
351 writel(temp, lpi2c_imx->base + LPI2C_MCFGR1);
352
353 /* set MCFGR2: FILTSDA, FILTSCL */
354 temp = (filt << 16) | (filt << 24);
355 writel(temp, lpi2c_imx->base + LPI2C_MCFGR2);
356
357 /* set MCCR: DATAVD, SETHOLD, CLKHI, CLKLO */
358 sethold = clkhi;
359 datavd = clkhi >> 1;
360 temp = datavd << 24 | sethold << 16 | clkhi << 8 | clklo;
361
362 if (lpi2c_imx->mode == HS)
363 writel(temp, lpi2c_imx->base + LPI2C_MCCR1);
364 else
365 writel(temp, lpi2c_imx->base + LPI2C_MCCR0);
366
367 return 0;
368 }
369
lpi2c_imx_master_enable(struct lpi2c_imx_struct * lpi2c_imx)370 static int lpi2c_imx_master_enable(struct lpi2c_imx_struct *lpi2c_imx)
371 {
372 unsigned int temp;
373 int ret;
374
375 ret = pm_runtime_resume_and_get(lpi2c_imx->adapter.dev.parent);
376 if (ret < 0)
377 return ret;
378
379 temp = MCR_RST;
380 writel(temp, lpi2c_imx->base + LPI2C_MCR);
381 writel(0, lpi2c_imx->base + LPI2C_MCR);
382
383 ret = lpi2c_imx_config(lpi2c_imx);
384 if (ret)
385 goto rpm_put;
386
387 temp = readl(lpi2c_imx->base + LPI2C_MCR);
388 temp |= MCR_MEN;
389 writel(temp, lpi2c_imx->base + LPI2C_MCR);
390
391 return 0;
392
393 rpm_put:
394 pm_runtime_put_autosuspend(lpi2c_imx->adapter.dev.parent);
395
396 return ret;
397 }
398
lpi2c_imx_master_disable(struct lpi2c_imx_struct * lpi2c_imx)399 static int lpi2c_imx_master_disable(struct lpi2c_imx_struct *lpi2c_imx)
400 {
401 u32 temp;
402
403 temp = readl(lpi2c_imx->base + LPI2C_MCR);
404 temp &= ~MCR_MEN;
405 writel(temp, lpi2c_imx->base + LPI2C_MCR);
406
407 pm_runtime_put_autosuspend(lpi2c_imx->adapter.dev.parent);
408
409 return 0;
410 }
411
lpi2c_imx_pio_msg_complete(struct lpi2c_imx_struct * lpi2c_imx)412 static int lpi2c_imx_pio_msg_complete(struct lpi2c_imx_struct *lpi2c_imx)
413 {
414 unsigned long time_left;
415
416 time_left = wait_for_completion_timeout(&lpi2c_imx->complete, HZ);
417
418 return time_left ? 0 : -ETIMEDOUT;
419 }
420
lpi2c_imx_txfifo_empty(struct lpi2c_imx_struct * lpi2c_imx,bool atomic)421 static int lpi2c_imx_txfifo_empty(struct lpi2c_imx_struct *lpi2c_imx, bool atomic)
422 {
423 unsigned int temp;
424 int err;
425
426 err = lpi2c_imx_read_msr_poll_timeout(atomic, temp,
427 (temp & MSR_NDF) || !lpi2c_imx_txfifo_cnt(lpi2c_imx));
428
429 if (temp & MSR_NDF) {
430 dev_dbg(&lpi2c_imx->adapter.dev, "NDF detected\n");
431 return -EIO;
432 }
433
434 if (err) {
435 dev_dbg(&lpi2c_imx->adapter.dev, "txfifo empty timeout\n");
436 if (lpi2c_imx->adapter.bus_recovery_info)
437 i2c_recover_bus(&lpi2c_imx->adapter);
438 return -ETIMEDOUT;
439 }
440
441 return 0;
442 }
443
lpi2c_imx_set_tx_watermark(struct lpi2c_imx_struct * lpi2c_imx)444 static void lpi2c_imx_set_tx_watermark(struct lpi2c_imx_struct *lpi2c_imx)
445 {
446 writel(lpi2c_imx->txfifosize >> 1, lpi2c_imx->base + LPI2C_MFCR);
447 }
448
lpi2c_imx_set_rx_watermark(struct lpi2c_imx_struct * lpi2c_imx)449 static void lpi2c_imx_set_rx_watermark(struct lpi2c_imx_struct *lpi2c_imx)
450 {
451 unsigned int temp, remaining;
452
453 remaining = lpi2c_imx->msglen - lpi2c_imx->delivered;
454
455 if (remaining > (lpi2c_imx->rxfifosize >> 1))
456 temp = lpi2c_imx->rxfifosize >> 1;
457 else
458 temp = 0;
459
460 writel(temp << 16, lpi2c_imx->base + LPI2C_MFCR);
461 }
462
lpi2c_imx_write_txfifo(struct lpi2c_imx_struct * lpi2c_imx,bool atomic)463 static bool lpi2c_imx_write_txfifo(struct lpi2c_imx_struct *lpi2c_imx, bool atomic)
464 {
465 unsigned int data, txcnt;
466
467 txcnt = readl(lpi2c_imx->base + LPI2C_MFSR) & 0xff;
468
469 while (txcnt < lpi2c_imx->txfifosize) {
470 if (lpi2c_imx->delivered == lpi2c_imx->msglen)
471 break;
472
473 data = lpi2c_imx->tx_buf[lpi2c_imx->delivered++];
474 writel(data, lpi2c_imx->base + LPI2C_MTDR);
475 txcnt++;
476 }
477
478 if (lpi2c_imx->delivered < lpi2c_imx->msglen) {
479 if (!atomic)
480 lpi2c_imx_intctrl(lpi2c_imx, MIER_TDIE | MIER_NDIE);
481 return false;
482 }
483
484 if (!atomic)
485 complete(&lpi2c_imx->complete);
486
487 return true;
488 }
489
lpi2c_imx_read_rxfifo(struct lpi2c_imx_struct * lpi2c_imx,bool atomic)490 static bool lpi2c_imx_read_rxfifo(struct lpi2c_imx_struct *lpi2c_imx, bool atomic)
491 {
492 unsigned int remaining;
493 unsigned int temp, data;
494
495 do {
496 data = readl(lpi2c_imx->base + LPI2C_MRDR);
497 if (data & MRDR_RXEMPTY)
498 break;
499
500 lpi2c_imx->rx_buf[lpi2c_imx->delivered++] = data & 0xff;
501 } while (1);
502
503 remaining = lpi2c_imx->msglen - lpi2c_imx->delivered;
504
505 if (!remaining) {
506 if (!atomic)
507 complete(&lpi2c_imx->complete);
508 return true;
509 }
510
511 /* not finished, still waiting for rx data */
512 lpi2c_imx_set_rx_watermark(lpi2c_imx);
513
514 /* multiple receive commands */
515 if (!(lpi2c_imx->delivered & 0xff)) {
516 temp = (remaining > CHUNK_DATA ? CHUNK_DATA : remaining) - 1;
517 temp |= (RECV_DATA << 8);
518 writel(temp, lpi2c_imx->base + LPI2C_MTDR);
519 }
520
521 if (!atomic)
522 lpi2c_imx_intctrl(lpi2c_imx, MIER_RDIE);
523
524 return false;
525 }
526
lpi2c_imx_write(struct lpi2c_imx_struct * lpi2c_imx,struct i2c_msg * msgs)527 static void lpi2c_imx_write(struct lpi2c_imx_struct *lpi2c_imx,
528 struct i2c_msg *msgs)
529 {
530 lpi2c_imx->tx_buf = msgs->buf;
531 lpi2c_imx_set_tx_watermark(lpi2c_imx);
532 lpi2c_imx_write_txfifo(lpi2c_imx, false);
533 }
534
lpi2c_imx_write_atomic(struct lpi2c_imx_struct * lpi2c_imx,struct i2c_msg * msgs)535 static int lpi2c_imx_write_atomic(struct lpi2c_imx_struct *lpi2c_imx,
536 struct i2c_msg *msgs)
537 {
538 u32 temp;
539 int err;
540
541 lpi2c_imx->tx_buf = msgs->buf;
542
543 err = lpi2c_imx_read_msr_poll_timeout(true, temp,
544 (temp & MSR_NDF) ||
545 lpi2c_imx_write_txfifo(lpi2c_imx, true));
546
547 if (temp & MSR_NDF)
548 return -EIO;
549
550 return err;
551 }
552
lpi2c_SMBus_block_read_length_byte(struct lpi2c_imx_struct * lpi2c_imx)553 static unsigned int lpi2c_SMBus_block_read_length_byte(struct lpi2c_imx_struct *lpi2c_imx)
554 {
555 unsigned int data;
556
557 data = readl(lpi2c_imx->base + LPI2C_MRDR);
558 lpi2c_imx->rx_buf[lpi2c_imx->delivered++] = data & 0xff;
559
560 return data;
561 }
562
lpi2c_imx_read_init(struct lpi2c_imx_struct * lpi2c_imx,struct i2c_msg * msgs)563 static int lpi2c_imx_read_init(struct lpi2c_imx_struct *lpi2c_imx,
564 struct i2c_msg *msgs)
565 {
566 unsigned int temp, val, block_len;
567 int ret;
568
569 lpi2c_imx->rx_buf = msgs->buf;
570 lpi2c_imx->block_data = msgs->flags & I2C_M_RECV_LEN;
571
572 lpi2c_imx_set_rx_watermark(lpi2c_imx);
573
574 if (!lpi2c_imx->block_data) {
575 temp = msgs->len > CHUNK_DATA ? CHUNK_DATA - 1 : msgs->len - 1;
576 temp |= (RECV_DATA << 8);
577 writel(temp, lpi2c_imx->base + LPI2C_MTDR);
578 } else {
579 /*
580 * The LPI2C controller automatically sends a NACK after the last byte of a
581 * receive command, unless the next command in MTDR is also a receive command.
582 * If MTDR is empty when a receive completes, a NACK is sent by default.
583 *
584 * To comply with the SMBus block read spec, we start with a 2-byte read:
585 * The first byte in RXFIFO is the block length. Once this byte arrives, the
586 * controller immediately updates MTDR with the next read command, ensuring
587 * continuous ACK instead of NACK.
588 *
589 * The second byte is the first block data byte. Therefore, the subsequent
590 * read command should request (block_len - 1) bytes, since one data byte
591 * has already been read.
592 */
593
594 writel((RECV_DATA << 8) | 0x01, lpi2c_imx->base + LPI2C_MTDR);
595
596 ret = readl_poll_timeout(lpi2c_imx->base + LPI2C_MSR, val,
597 MSR_RDF_ASSERTED(val), 1, 1000);
598 if (ret) {
599 dev_err(&lpi2c_imx->adapter.dev, "SMBus read count failed %d\n", ret);
600 return ret;
601 }
602
603 /* Read block length byte and confirm this SMBus transfer meets protocol */
604 block_len = lpi2c_SMBus_block_read_length_byte(lpi2c_imx);
605 if (block_len == 0 || block_len > I2C_SMBUS_BLOCK_MAX) {
606 dev_err(&lpi2c_imx->adapter.dev, "Invalid SMBus block read length\n");
607 return -EPROTO;
608 }
609
610 /*
611 * When block_len shows more bytes need to be read, update second read command to
612 * keep MTDR non-empty and ensuring continuous ACKs. Only update command register
613 * here. All block bytes will be read out at IRQ handler or lpi2c_imx_read_atomic()
614 * function.
615 */
616 if (block_len > 1)
617 writel((RECV_DATA << 8) | (block_len - 2), lpi2c_imx->base + LPI2C_MTDR);
618
619 lpi2c_imx->msglen += block_len;
620 msgs->len += block_len;
621 }
622
623 return 0;
624 }
625
lpi2c_imx_read_chunk_atomic(struct lpi2c_imx_struct * lpi2c_imx)626 static bool lpi2c_imx_read_chunk_atomic(struct lpi2c_imx_struct *lpi2c_imx)
627 {
628 u32 rxcnt;
629
630 rxcnt = (readl(lpi2c_imx->base + LPI2C_MFSR) >> 16) & 0xFF;
631 if (!rxcnt)
632 return false;
633
634 if (!lpi2c_imx_read_rxfifo(lpi2c_imx, true))
635 return false;
636
637 return true;
638 }
639
lpi2c_imx_read_atomic(struct lpi2c_imx_struct * lpi2c_imx,struct i2c_msg * msgs)640 static int lpi2c_imx_read_atomic(struct lpi2c_imx_struct *lpi2c_imx,
641 struct i2c_msg *msgs)
642 {
643 u32 temp;
644 int tmo_us;
645
646 tmo_us = 1000000;
647 do {
648 if (lpi2c_imx_read_chunk_atomic(lpi2c_imx))
649 return 0;
650
651 temp = readl(lpi2c_imx->base + LPI2C_MSR);
652
653 if (temp & MSR_NDF)
654 return -EIO;
655
656 udelay(100);
657 tmo_us -= 100;
658 } while (tmo_us > 0);
659
660 return -ETIMEDOUT;
661 }
662
is_use_dma(struct lpi2c_imx_struct * lpi2c_imx,struct i2c_msg * msg)663 static bool is_use_dma(struct lpi2c_imx_struct *lpi2c_imx, struct i2c_msg *msg)
664 {
665 if (!lpi2c_imx->can_use_dma)
666 return false;
667
668 /* DMA is not suitable for SMBus block read */
669 if (msg->flags & I2C_M_RECV_LEN)
670 return false;
671
672 /*
673 * A system-wide suspend or resume transition is in progress. LPI2C should use PIO to
674 * transfer data to avoid issue caused by no ready DMA HW resource.
675 */
676 if (pm_suspend_in_progress())
677 return false;
678
679 /*
680 * When the length of data is less than I2C_DMA_THRESHOLD,
681 * cpu mode is used directly to avoid low performance.
682 */
683 return !(msg->len < I2C_DMA_THRESHOLD);
684 }
685
lpi2c_imx_pio_xfer(struct lpi2c_imx_struct * lpi2c_imx,struct i2c_msg * msg)686 static int lpi2c_imx_pio_xfer(struct lpi2c_imx_struct *lpi2c_imx,
687 struct i2c_msg *msg)
688 {
689 int ret;
690
691 reinit_completion(&lpi2c_imx->complete);
692
693 if (msg->flags & I2C_M_RD) {
694 ret = lpi2c_imx_read_init(lpi2c_imx, msg);
695 if (ret)
696 return ret;
697 lpi2c_imx_intctrl(lpi2c_imx, MIER_RDIE | MIER_NDIE);
698 } else {
699 lpi2c_imx_write(lpi2c_imx, msg);
700 }
701
702 return lpi2c_imx_pio_msg_complete(lpi2c_imx);
703 }
704
lpi2c_imx_pio_xfer_atomic(struct lpi2c_imx_struct * lpi2c_imx,struct i2c_msg * msg)705 static int lpi2c_imx_pio_xfer_atomic(struct lpi2c_imx_struct *lpi2c_imx,
706 struct i2c_msg *msg)
707 {
708 int ret;
709
710 if (msg->flags & I2C_M_RD) {
711 ret = lpi2c_imx_read_init(lpi2c_imx, msg);
712 if (ret)
713 return ret;
714 return lpi2c_imx_read_atomic(lpi2c_imx, msg);
715 }
716
717 return lpi2c_imx_write_atomic(lpi2c_imx, msg);
718 }
719
lpi2c_imx_dma_timeout_calculate(struct lpi2c_imx_struct * lpi2c_imx)720 static int lpi2c_imx_dma_timeout_calculate(struct lpi2c_imx_struct *lpi2c_imx)
721 {
722 unsigned long time = 0;
723
724 time = 8 * lpi2c_imx->dma->dma_len * 1000 / lpi2c_imx->bitrate;
725
726 /* Add extra second for scheduler related activities */
727 time += 1;
728
729 /* Double calculated time */
730 return secs_to_jiffies(time);
731 }
732
lpi2c_imx_alloc_rx_cmd_buf(struct lpi2c_imx_struct * lpi2c_imx)733 static int lpi2c_imx_alloc_rx_cmd_buf(struct lpi2c_imx_struct *lpi2c_imx)
734 {
735 struct lpi2c_imx_dma *dma = lpi2c_imx->dma;
736 u16 rx_remain = dma->dma_len;
737 int cmd_num;
738 u16 temp;
739
740 /*
741 * Calculate the number of rx command words via the DMA TX channel
742 * writing into command register based on the i2c msg len, and build
743 * the rx command words buffer.
744 */
745 cmd_num = DIV_ROUND_UP(rx_remain, CHUNK_DATA);
746 dma->rx_cmd_buf = kcalloc(cmd_num, sizeof(u16), GFP_KERNEL);
747 dma->rx_cmd_buf_len = cmd_num * sizeof(u16);
748
749 if (!dma->rx_cmd_buf) {
750 dev_err(&lpi2c_imx->adapter.dev, "Alloc RX cmd buffer failed\n");
751 return -ENOMEM;
752 }
753
754 for (int i = 0; i < cmd_num ; i++) {
755 temp = rx_remain > CHUNK_DATA ? CHUNK_DATA - 1 : rx_remain - 1;
756 temp |= (RECV_DATA << 8);
757 rx_remain -= CHUNK_DATA;
758 dma->rx_cmd_buf[i] = temp;
759 }
760
761 return 0;
762 }
763
lpi2c_imx_dma_msg_complete(struct lpi2c_imx_struct * lpi2c_imx)764 static int lpi2c_imx_dma_msg_complete(struct lpi2c_imx_struct *lpi2c_imx)
765 {
766 unsigned long time_left, time;
767
768 time = lpi2c_imx_dma_timeout_calculate(lpi2c_imx);
769 time_left = wait_for_completion_timeout(&lpi2c_imx->complete, time);
770 if (time_left == 0) {
771 dev_err(&lpi2c_imx->adapter.dev, "I/O Error in DMA Data Transfer\n");
772 return -ETIMEDOUT;
773 }
774
775 return 0;
776 }
777
lpi2c_dma_unmap(struct lpi2c_imx_dma * dma)778 static void lpi2c_dma_unmap(struct lpi2c_imx_dma *dma)
779 {
780 struct dma_chan *chan = dma->dma_data_dir == DMA_FROM_DEVICE
781 ? dma->chan_rx : dma->chan_tx;
782
783 dma_unmap_single(chan->device->dev, dma->dma_addr,
784 dma->dma_len, dma->dma_data_dir);
785
786 dma->dma_data_dir = DMA_NONE;
787 }
788
lpi2c_cleanup_rx_cmd_dma(struct lpi2c_imx_dma * dma)789 static void lpi2c_cleanup_rx_cmd_dma(struct lpi2c_imx_dma *dma)
790 {
791 dmaengine_terminate_sync(dma->chan_tx);
792 dma_unmap_single(dma->chan_tx->device->dev, dma->dma_tx_addr,
793 dma->rx_cmd_buf_len, DMA_TO_DEVICE);
794 }
795
lpi2c_cleanup_dma(struct lpi2c_imx_dma * dma)796 static void lpi2c_cleanup_dma(struct lpi2c_imx_dma *dma)
797 {
798 if (dma->dma_data_dir == DMA_FROM_DEVICE)
799 dmaengine_terminate_sync(dma->chan_rx);
800 else if (dma->dma_data_dir == DMA_TO_DEVICE)
801 dmaengine_terminate_sync(dma->chan_tx);
802
803 lpi2c_dma_unmap(dma);
804 }
805
lpi2c_dma_callback(void * data)806 static void lpi2c_dma_callback(void *data)
807 {
808 struct lpi2c_imx_struct *lpi2c_imx = (struct lpi2c_imx_struct *)data;
809
810 complete(&lpi2c_imx->complete);
811 }
812
lpi2c_dma_rx_cmd_submit(struct lpi2c_imx_struct * lpi2c_imx)813 static int lpi2c_dma_rx_cmd_submit(struct lpi2c_imx_struct *lpi2c_imx)
814 {
815 struct dma_async_tx_descriptor *rx_cmd_desc;
816 struct lpi2c_imx_dma *dma = lpi2c_imx->dma;
817 struct dma_chan *txchan = dma->chan_tx;
818 dma_cookie_t cookie;
819
820 dma->dma_tx_addr = dma_map_single(txchan->device->dev,
821 dma->rx_cmd_buf, dma->rx_cmd_buf_len,
822 DMA_TO_DEVICE);
823 if (dma_mapping_error(txchan->device->dev, dma->dma_tx_addr)) {
824 dev_err(&lpi2c_imx->adapter.dev, "DMA map failed, use pio\n");
825 return -EINVAL;
826 }
827
828 rx_cmd_desc = dmaengine_prep_slave_single(txchan, dma->dma_tx_addr,
829 dma->rx_cmd_buf_len, DMA_MEM_TO_DEV,
830 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
831 if (!rx_cmd_desc) {
832 dev_err(&lpi2c_imx->adapter.dev, "DMA prep slave sg failed, use pio\n");
833 goto desc_prepare_err_exit;
834 }
835
836 cookie = dmaengine_submit(rx_cmd_desc);
837 if (dma_submit_error(cookie)) {
838 dev_err(&lpi2c_imx->adapter.dev, "submitting DMA failed, use pio\n");
839 goto submit_err_exit;
840 }
841
842 dma_async_issue_pending(txchan);
843
844 return 0;
845
846 desc_prepare_err_exit:
847 dma_unmap_single(txchan->device->dev, dma->dma_tx_addr,
848 dma->rx_cmd_buf_len, DMA_TO_DEVICE);
849 return -EINVAL;
850
851 submit_err_exit:
852 dma_unmap_single(txchan->device->dev, dma->dma_tx_addr,
853 dma->rx_cmd_buf_len, DMA_TO_DEVICE);
854 dmaengine_desc_free(rx_cmd_desc);
855 return -EINVAL;
856 }
857
lpi2c_dma_submit(struct lpi2c_imx_struct * lpi2c_imx)858 static int lpi2c_dma_submit(struct lpi2c_imx_struct *lpi2c_imx)
859 {
860 struct lpi2c_imx_dma *dma = lpi2c_imx->dma;
861 struct dma_async_tx_descriptor *desc;
862 struct dma_chan *chan;
863 dma_cookie_t cookie;
864
865 if (dma->dma_msg_flag & I2C_M_RD) {
866 chan = dma->chan_rx;
867 dma->dma_data_dir = DMA_FROM_DEVICE;
868 dma->dma_transfer_dir = DMA_DEV_TO_MEM;
869 } else {
870 chan = dma->chan_tx;
871 dma->dma_data_dir = DMA_TO_DEVICE;
872 dma->dma_transfer_dir = DMA_MEM_TO_DEV;
873 }
874
875 dma->dma_addr = dma_map_single(chan->device->dev,
876 dma->dma_buf, dma->dma_len, dma->dma_data_dir);
877 if (dma_mapping_error(chan->device->dev, dma->dma_addr)) {
878 dev_err(&lpi2c_imx->adapter.dev, "DMA map failed, use pio\n");
879 return -EINVAL;
880 }
881
882 desc = dmaengine_prep_slave_single(chan, dma->dma_addr,
883 dma->dma_len, dma->dma_transfer_dir,
884 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
885 if (!desc) {
886 dev_err(&lpi2c_imx->adapter.dev, "DMA prep slave sg failed, use pio\n");
887 goto desc_prepare_err_exit;
888 }
889
890 reinit_completion(&lpi2c_imx->complete);
891 desc->callback = lpi2c_dma_callback;
892 desc->callback_param = lpi2c_imx;
893
894 cookie = dmaengine_submit(desc);
895 if (dma_submit_error(cookie)) {
896 dev_err(&lpi2c_imx->adapter.dev, "submitting DMA failed, use pio\n");
897 goto submit_err_exit;
898 }
899
900 /* Can't switch to PIO mode when DMA have started transfer */
901 dma->using_pio_mode = false;
902
903 dma_async_issue_pending(chan);
904
905 return 0;
906
907 desc_prepare_err_exit:
908 lpi2c_dma_unmap(dma);
909 return -EINVAL;
910
911 submit_err_exit:
912 lpi2c_dma_unmap(dma);
913 dmaengine_desc_free(desc);
914 return -EINVAL;
915 }
916
lpi2c_imx_find_max_burst_num(unsigned int fifosize,unsigned int len)917 static int lpi2c_imx_find_max_burst_num(unsigned int fifosize, unsigned int len)
918 {
919 unsigned int i;
920
921 for (i = fifosize / 2; i > 0; i--)
922 if (!(len % i))
923 break;
924
925 return i;
926 }
927
928 /*
929 * For a highest DMA efficiency, tx/rx burst number should be calculated according
930 * to the FIFO depth.
931 */
lpi2c_imx_dma_burst_num_calculate(struct lpi2c_imx_struct * lpi2c_imx)932 static void lpi2c_imx_dma_burst_num_calculate(struct lpi2c_imx_struct *lpi2c_imx)
933 {
934 struct lpi2c_imx_dma *dma = lpi2c_imx->dma;
935 unsigned int cmd_num;
936
937 if (dma->dma_msg_flag & I2C_M_RD) {
938 /*
939 * One RX cmd word can trigger DMA receive no more than 256 bytes.
940 * The number of RX cmd words should be calculated based on the data
941 * length.
942 */
943 cmd_num = DIV_ROUND_UP(dma->dma_len, CHUNK_DATA);
944 dma->tx_burst_num = lpi2c_imx_find_max_burst_num(lpi2c_imx->txfifosize,
945 cmd_num);
946 dma->rx_burst_num = lpi2c_imx_find_max_burst_num(lpi2c_imx->rxfifosize,
947 dma->dma_len);
948 } else {
949 dma->tx_burst_num = lpi2c_imx_find_max_burst_num(lpi2c_imx->txfifosize,
950 dma->dma_len);
951 }
952 }
953
lpi2c_dma_config(struct lpi2c_imx_struct * lpi2c_imx)954 static int lpi2c_dma_config(struct lpi2c_imx_struct *lpi2c_imx)
955 {
956 struct lpi2c_imx_dma *dma = lpi2c_imx->dma;
957 struct dma_slave_config rx = {}, tx = {};
958 int ret;
959
960 lpi2c_imx_dma_burst_num_calculate(lpi2c_imx);
961
962 if (dma->dma_msg_flag & I2C_M_RD) {
963 tx.dst_addr = dma->phy_addr + LPI2C_MTDR;
964 tx.dst_addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
965 tx.dst_maxburst = dma->tx_burst_num;
966 tx.direction = DMA_MEM_TO_DEV;
967 ret = dmaengine_slave_config(dma->chan_tx, &tx);
968 if (ret < 0)
969 return ret;
970
971 rx.src_addr = dma->phy_addr + LPI2C_MRDR;
972 rx.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
973 rx.src_maxburst = dma->rx_burst_num;
974 rx.direction = DMA_DEV_TO_MEM;
975 ret = dmaengine_slave_config(dma->chan_rx, &rx);
976 if (ret < 0)
977 return ret;
978 } else {
979 tx.dst_addr = dma->phy_addr + LPI2C_MTDR;
980 tx.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
981 tx.dst_maxburst = dma->tx_burst_num;
982 tx.direction = DMA_MEM_TO_DEV;
983 ret = dmaengine_slave_config(dma->chan_tx, &tx);
984 if (ret < 0)
985 return ret;
986 }
987
988 return 0;
989 }
990
lpi2c_dma_enable(struct lpi2c_imx_struct * lpi2c_imx)991 static void lpi2c_dma_enable(struct lpi2c_imx_struct *lpi2c_imx)
992 {
993 struct lpi2c_imx_dma *dma = lpi2c_imx->dma;
994 /*
995 * TX interrupt will be triggered when the number of words in
996 * the transmit FIFO is equal or less than TX watermark.
997 * RX interrupt will be triggered when the number of words in
998 * the receive FIFO is greater than RX watermark.
999 * In order to trigger the DMA interrupt, TX watermark should be
1000 * set equal to the DMA TX burst number but RX watermark should
1001 * be set less than the DMA RX burst number.
1002 */
1003 if (dma->dma_msg_flag & I2C_M_RD) {
1004 /* Set I2C TX/RX watermark */
1005 writel(dma->tx_burst_num | (dma->rx_burst_num - 1) << 16,
1006 lpi2c_imx->base + LPI2C_MFCR);
1007 /* Enable I2C DMA TX/RX function */
1008 writel(MDER_TDDE | MDER_RDDE, lpi2c_imx->base + LPI2C_MDER);
1009 } else {
1010 /* Set I2C TX watermark */
1011 writel(dma->tx_burst_num, lpi2c_imx->base + LPI2C_MFCR);
1012 /* Enable I2C DMA TX function */
1013 writel(MDER_TDDE, lpi2c_imx->base + LPI2C_MDER);
1014 }
1015
1016 /* Enable NACK detected */
1017 lpi2c_imx_intctrl(lpi2c_imx, MIER_NDIE);
1018 };
1019
1020 /*
1021 * When lpi2c is in TX DMA mode we can use one DMA TX channel to write
1022 * data word into TXFIFO, but in RX DMA mode it is different.
1023 *
1024 * The LPI2C MTDR register is a command data and transmit data register.
1025 * Bits 8-10 are the command data field and Bits 0-7 are the transmit
1026 * data field. When the LPI2C master needs to read data, the number of
1027 * bytes to read should be set in the command field and RECV_DATA should
1028 * be set into the command data field to receive (DATA[7:0] + 1) bytes.
1029 * The recv data command word is made of RECV_DATA in the command data
1030 * field and the number of bytes to read in transmit data field. When the
1031 * length of data to be read exceeds 256 bytes, recv data command word
1032 * needs to be written to TXFIFO multiple times.
1033 *
1034 * So when in RX DMA mode, the TX channel also must to be configured to
1035 * send RX command words and the RX command word must be set in advance
1036 * before transmitting.
1037 */
lpi2c_imx_dma_xfer(struct lpi2c_imx_struct * lpi2c_imx,struct i2c_msg * msg)1038 static int lpi2c_imx_dma_xfer(struct lpi2c_imx_struct *lpi2c_imx,
1039 struct i2c_msg *msg)
1040 {
1041 struct lpi2c_imx_dma *dma = lpi2c_imx->dma;
1042 int ret;
1043
1044 /* When DMA mode fails before transferring, CPU mode can be used. */
1045 dma->using_pio_mode = true;
1046
1047 dma->dma_len = msg->len;
1048 dma->dma_msg_flag = msg->flags;
1049 dma->dma_buf = i2c_get_dma_safe_msg_buf(msg, I2C_DMA_THRESHOLD);
1050 if (!dma->dma_buf)
1051 return -ENOMEM;
1052
1053 ret = lpi2c_dma_config(lpi2c_imx);
1054 if (ret) {
1055 dev_err(&lpi2c_imx->adapter.dev, "Failed to configure DMA (%d)\n", ret);
1056 goto disable_dma;
1057 }
1058
1059 lpi2c_dma_enable(lpi2c_imx);
1060
1061 ret = lpi2c_dma_submit(lpi2c_imx);
1062 if (ret) {
1063 dev_err(&lpi2c_imx->adapter.dev, "DMA submission failed (%d)\n", ret);
1064 goto disable_dma;
1065 }
1066
1067 if (dma->dma_msg_flag & I2C_M_RD) {
1068 ret = lpi2c_imx_alloc_rx_cmd_buf(lpi2c_imx);
1069 if (ret)
1070 goto disable_cleanup_data_dma;
1071
1072 ret = lpi2c_dma_rx_cmd_submit(lpi2c_imx);
1073 if (ret)
1074 goto disable_cleanup_data_dma;
1075 }
1076
1077 ret = lpi2c_imx_dma_msg_complete(lpi2c_imx);
1078 if (ret)
1079 goto disable_cleanup_all_dma;
1080
1081 /* When encountering NACK in transfer, clean up all DMA transfers */
1082 if ((readl(lpi2c_imx->base + LPI2C_MSR) & MSR_NDF) && !ret) {
1083 ret = -EIO;
1084 goto disable_cleanup_all_dma;
1085 }
1086
1087 if (dma->dma_msg_flag & I2C_M_RD)
1088 dma_unmap_single(dma->chan_tx->device->dev, dma->dma_tx_addr,
1089 dma->rx_cmd_buf_len, DMA_TO_DEVICE);
1090 lpi2c_dma_unmap(dma);
1091
1092 goto disable_dma;
1093
1094 disable_cleanup_all_dma:
1095 if (dma->dma_msg_flag & I2C_M_RD)
1096 lpi2c_cleanup_rx_cmd_dma(dma);
1097 disable_cleanup_data_dma:
1098 lpi2c_cleanup_dma(dma);
1099 disable_dma:
1100 /* Disable I2C DMA function */
1101 writel(0, lpi2c_imx->base + LPI2C_MDER);
1102
1103 if (dma->dma_msg_flag & I2C_M_RD)
1104 kfree(dma->rx_cmd_buf);
1105
1106 if (ret)
1107 i2c_put_dma_safe_msg_buf(dma->dma_buf, msg, false);
1108 else
1109 i2c_put_dma_safe_msg_buf(dma->dma_buf, msg, true);
1110
1111 return ret;
1112 }
1113
lpi2c_imx_xfer_common(struct i2c_adapter * adapter,struct i2c_msg * msgs,int num,bool atomic)1114 static int lpi2c_imx_xfer_common(struct i2c_adapter *adapter,
1115 struct i2c_msg *msgs, int num, bool atomic)
1116 {
1117 struct lpi2c_imx_struct *lpi2c_imx = i2c_get_adapdata(adapter);
1118 unsigned int temp;
1119 int i, result;
1120
1121 result = lpi2c_imx_master_enable(lpi2c_imx);
1122 if (result)
1123 return result;
1124
1125 for (i = 0; i < num; i++) {
1126 result = lpi2c_imx_start(lpi2c_imx, &msgs[i], atomic);
1127 if (result)
1128 goto disable;
1129
1130 /* quick smbus */
1131 if (num == 1 && msgs[0].len == 0)
1132 goto stop;
1133
1134 lpi2c_imx->rx_buf = NULL;
1135 lpi2c_imx->tx_buf = NULL;
1136 lpi2c_imx->delivered = 0;
1137 lpi2c_imx->msglen = msgs[i].len;
1138
1139 if (atomic) {
1140 result = lpi2c_imx_pio_xfer_atomic(lpi2c_imx, &msgs[i]);
1141 } else {
1142 init_completion(&lpi2c_imx->complete);
1143
1144 if (is_use_dma(lpi2c_imx, &msgs[i])) {
1145 result = lpi2c_imx_dma_xfer(lpi2c_imx, &msgs[i]);
1146 if (result && lpi2c_imx->dma->using_pio_mode)
1147 result = lpi2c_imx_pio_xfer(lpi2c_imx, &msgs[i]);
1148 } else {
1149 result = lpi2c_imx_pio_xfer(lpi2c_imx, &msgs[i]);
1150 }
1151 }
1152
1153 if (result)
1154 goto stop;
1155
1156 if (!(msgs[i].flags & I2C_M_RD)) {
1157 result = lpi2c_imx_txfifo_empty(lpi2c_imx, atomic);
1158 if (result)
1159 goto stop;
1160 }
1161 }
1162
1163 stop:
1164 lpi2c_imx_stop(lpi2c_imx, atomic);
1165
1166 temp = readl(lpi2c_imx->base + LPI2C_MSR);
1167 if ((temp & MSR_NDF) && !result)
1168 result = -EIO;
1169
1170 disable:
1171 lpi2c_imx_master_disable(lpi2c_imx);
1172
1173 dev_dbg(&lpi2c_imx->adapter.dev, "<%s> exit with: %s: %d\n", __func__,
1174 (result < 0) ? "error" : "success msg",
1175 (result < 0) ? result : num);
1176
1177 return (result < 0) ? result : num;
1178 }
1179
lpi2c_imx_xfer(struct i2c_adapter * adapter,struct i2c_msg * msgs,int num)1180 static int lpi2c_imx_xfer(struct i2c_adapter *adapter, struct i2c_msg *msgs, int num)
1181 {
1182 return lpi2c_imx_xfer_common(adapter, msgs, num, false);
1183 }
1184
lpi2c_imx_xfer_atomic(struct i2c_adapter * adapter,struct i2c_msg * msgs,int num)1185 static int lpi2c_imx_xfer_atomic(struct i2c_adapter *adapter, struct i2c_msg *msgs, int num)
1186 {
1187 return lpi2c_imx_xfer_common(adapter, msgs, num, true);
1188 }
1189
lpi2c_imx_target_isr(struct lpi2c_imx_struct * lpi2c_imx,u32 ssr,u32 sier_filter)1190 static irqreturn_t lpi2c_imx_target_isr(struct lpi2c_imx_struct *lpi2c_imx,
1191 u32 ssr, u32 sier_filter)
1192 {
1193 u8 value;
1194 u32 sasr;
1195
1196 /* Arbitration lost */
1197 if (sier_filter & SSR_BEF) {
1198 writel(0, lpi2c_imx->base + LPI2C_SIER);
1199 return IRQ_HANDLED;
1200 }
1201
1202 /* Address detected */
1203 if (sier_filter & SSR_AVF) {
1204 sasr = readl(lpi2c_imx->base + LPI2C_SASR);
1205 if (SASR_READ_REQ & sasr) {
1206 /* Read request */
1207 i2c_slave_event(lpi2c_imx->target, I2C_SLAVE_READ_REQUESTED, &value);
1208 writel(value, lpi2c_imx->base + LPI2C_STDR);
1209 goto ret;
1210 } else {
1211 /* Write request */
1212 i2c_slave_event(lpi2c_imx->target, I2C_SLAVE_WRITE_REQUESTED, &value);
1213 }
1214 }
1215
1216 if (sier_filter & SSR_SDF)
1217 /* STOP */
1218 i2c_slave_event(lpi2c_imx->target, I2C_SLAVE_STOP, &value);
1219
1220 if (sier_filter & SSR_TDF) {
1221 /* Target send data */
1222 i2c_slave_event(lpi2c_imx->target, I2C_SLAVE_READ_PROCESSED, &value);
1223 writel(value, lpi2c_imx->base + LPI2C_STDR);
1224 }
1225
1226 if (sier_filter & SSR_RDF) {
1227 /* Target receive data */
1228 value = readl(lpi2c_imx->base + LPI2C_SRDR);
1229 i2c_slave_event(lpi2c_imx->target, I2C_SLAVE_WRITE_RECEIVED, &value);
1230 }
1231
1232 ret:
1233 /* Clear SSR */
1234 writel(ssr & SSR_CLEAR_BITS, lpi2c_imx->base + LPI2C_SSR);
1235 return IRQ_HANDLED;
1236 }
1237
lpi2c_imx_master_isr(struct lpi2c_imx_struct * lpi2c_imx)1238 static irqreturn_t lpi2c_imx_master_isr(struct lpi2c_imx_struct *lpi2c_imx)
1239 {
1240 unsigned int enabled;
1241 unsigned int temp;
1242
1243 enabled = readl(lpi2c_imx->base + LPI2C_MIER);
1244
1245 lpi2c_imx_intctrl(lpi2c_imx, 0);
1246 temp = readl(lpi2c_imx->base + LPI2C_MSR);
1247 temp &= enabled;
1248
1249 if (temp & MSR_NDF)
1250 complete(&lpi2c_imx->complete);
1251 else if (temp & MSR_RDF)
1252 lpi2c_imx_read_rxfifo(lpi2c_imx, false);
1253 else if (temp & MSR_TDF)
1254 lpi2c_imx_write_txfifo(lpi2c_imx, false);
1255
1256 return IRQ_HANDLED;
1257 }
1258
lpi2c_imx_isr(int irq,void * dev_id)1259 static irqreturn_t lpi2c_imx_isr(int irq, void *dev_id)
1260 {
1261 struct lpi2c_imx_struct *lpi2c_imx = dev_id;
1262
1263 if (lpi2c_imx->target) {
1264 u32 scr = readl(lpi2c_imx->base + LPI2C_SCR);
1265 u32 ssr = readl(lpi2c_imx->base + LPI2C_SSR);
1266 u32 sier_filter = ssr & readl(lpi2c_imx->base + LPI2C_SIER);
1267
1268 /*
1269 * The target is enabled and an interrupt has been triggered.
1270 * Enter the target's irq handler.
1271 */
1272 if ((scr & SCR_SEN) && sier_filter)
1273 return lpi2c_imx_target_isr(lpi2c_imx, ssr, sier_filter);
1274 }
1275
1276 /*
1277 * Otherwise the interrupt has been triggered by the master.
1278 * Enter the master's irq handler.
1279 */
1280 return lpi2c_imx_master_isr(lpi2c_imx);
1281 }
1282
lpi2c_imx_target_init(struct lpi2c_imx_struct * lpi2c_imx)1283 static void lpi2c_imx_target_init(struct lpi2c_imx_struct *lpi2c_imx)
1284 {
1285 u32 temp;
1286
1287 /* reset target module */
1288 writel(SCR_RST, lpi2c_imx->base + LPI2C_SCR);
1289 writel(0, lpi2c_imx->base + LPI2C_SCR);
1290
1291 /* Set target address */
1292 writel((lpi2c_imx->target->addr << 1), lpi2c_imx->base + LPI2C_SAMR);
1293
1294 writel(SCFGR1_RXSTALL | SCFGR1_TXDSTALL, lpi2c_imx->base + LPI2C_SCFGR1);
1295
1296 /*
1297 * set SCFGR2: FILTSDA, FILTSCL and CLKHOLD
1298 *
1299 * FILTSCL/FILTSDA can eliminate signal skew. It should generally be
1300 * set to the same value and should be set >= 50ns.
1301 *
1302 * CLKHOLD is only used when clock stretching is enabled, but it will
1303 * extend the clock stretching to ensure there is an additional delay
1304 * between the target driving SDA and the target releasing the SCL pin.
1305 *
1306 * CLKHOLD setting is crucial for lpi2c target. When master read data
1307 * from target, if there is a delay caused by cpu idle, excessive load,
1308 * or other delays between two bytes in one message transmission, it
1309 * will cause a short interval time between the driving SDA signal and
1310 * releasing SCL signal. The lpi2c master will mistakenly think it is a stop
1311 * signal resulting in an arbitration failure. This issue can be avoided
1312 * by setting CLKHOLD.
1313 *
1314 * In order to ensure lpi2c function normally when the lpi2c speed is as
1315 * low as 100kHz, CLKHOLD should be set to 3 and it is also compatible with
1316 * higher clock frequency like 400kHz and 1MHz.
1317 */
1318 temp = SCFGR2_FILTSDA(2) | SCFGR2_FILTSCL(2) | SCFGR2_CLKHOLD(3);
1319 writel(temp, lpi2c_imx->base + LPI2C_SCFGR2);
1320
1321 /*
1322 * Enable module:
1323 * SCR_FILTEN can enable digital filter and output delay counter for LPI2C
1324 * target mode. So SCR_FILTEN need be asserted when enable SDA/SCL FILTER
1325 * and CLKHOLD.
1326 */
1327 writel(SCR_SEN | SCR_FILTEN, lpi2c_imx->base + LPI2C_SCR);
1328
1329 /* Enable interrupt from i2c module */
1330 writel(SLAVE_INT_FLAG, lpi2c_imx->base + LPI2C_SIER);
1331 }
1332
lpi2c_imx_register_target(struct i2c_client * client)1333 static int lpi2c_imx_register_target(struct i2c_client *client)
1334 {
1335 struct lpi2c_imx_struct *lpi2c_imx = i2c_get_adapdata(client->adapter);
1336 int ret;
1337
1338 /* Reject target-mode registration on controllers that don't support it. */
1339 if (!lpi2c_imx->target_supported)
1340 return -EOPNOTSUPP;
1341
1342 if (lpi2c_imx->target)
1343 return -EBUSY;
1344
1345 lpi2c_imx->target = client;
1346
1347 ret = pm_runtime_resume_and_get(lpi2c_imx->adapter.dev.parent);
1348 if (ret < 0) {
1349 dev_err(&lpi2c_imx->adapter.dev, "failed to resume i2c controller");
1350 return ret;
1351 }
1352
1353 lpi2c_imx_target_init(lpi2c_imx);
1354
1355 return 0;
1356 }
1357
lpi2c_imx_unregister_target(struct i2c_client * client)1358 static int lpi2c_imx_unregister_target(struct i2c_client *client)
1359 {
1360 struct lpi2c_imx_struct *lpi2c_imx = i2c_get_adapdata(client->adapter);
1361 int ret;
1362
1363 if (!lpi2c_imx->target)
1364 return -EINVAL;
1365
1366 /* Reset target address. */
1367 writel(0, lpi2c_imx->base + LPI2C_SAMR);
1368
1369 writel(SCR_RST, lpi2c_imx->base + LPI2C_SCR);
1370 writel(0, lpi2c_imx->base + LPI2C_SCR);
1371
1372 lpi2c_imx->target = NULL;
1373
1374 ret = pm_runtime_put_sync(lpi2c_imx->adapter.dev.parent);
1375 if (ret < 0)
1376 dev_err(&lpi2c_imx->adapter.dev, "failed to suspend i2c controller");
1377
1378 return ret;
1379 }
1380
lpi2c_imx_init_recovery_info(struct lpi2c_imx_struct * lpi2c_imx,struct platform_device * pdev)1381 static int lpi2c_imx_init_recovery_info(struct lpi2c_imx_struct *lpi2c_imx,
1382 struct platform_device *pdev)
1383 {
1384 struct i2c_bus_recovery_info *bri = &lpi2c_imx->rinfo;
1385
1386 bri->pinctrl = devm_pinctrl_get(&pdev->dev);
1387 if (IS_ERR(bri->pinctrl))
1388 return PTR_ERR(bri->pinctrl);
1389
1390 lpi2c_imx->adapter.bus_recovery_info = bri;
1391
1392 return 0;
1393 }
1394
lpi2c_dma_init(struct device * dev,dma_addr_t phy_addr)1395 static int lpi2c_dma_init(struct device *dev, dma_addr_t phy_addr)
1396 {
1397 struct lpi2c_imx_struct *lpi2c_imx = dev_get_drvdata(dev);
1398 struct lpi2c_imx_dma *dma;
1399 void *group;
1400 int ret;
1401
1402 /*
1403 * Open a devres group so that all resources allocated within
1404 * this function can be released together if DMA init fails but
1405 * probe continues in PIO mode.
1406 */
1407 group = devres_open_group(dev, NULL, GFP_KERNEL);
1408 if (!group)
1409 return -ENOMEM;
1410
1411 dma = devm_kzalloc(dev, sizeof(*dma), GFP_KERNEL);
1412 if (!dma) {
1413 ret = -ENOMEM;
1414 goto release_group;
1415 }
1416
1417 dma->phy_addr = phy_addr;
1418
1419 /* Prepare for TX DMA: */
1420 dma->chan_tx = devm_dma_request_chan(dev, "tx");
1421 if (IS_ERR(dma->chan_tx)) {
1422 ret = PTR_ERR(dma->chan_tx);
1423 if (ret != -ENODEV && ret != -EPROBE_DEFER)
1424 dev_err(dev, "can't request DMA tx channel (%d)\n", ret);
1425 goto release_group;
1426 }
1427
1428 /* Prepare for RX DMA: */
1429 dma->chan_rx = devm_dma_request_chan(dev, "rx");
1430 if (IS_ERR(dma->chan_rx)) {
1431 ret = PTR_ERR(dma->chan_rx);
1432 if (ret != -ENODEV && ret != -EPROBE_DEFER)
1433 dev_err(dev, "can't request DMA rx channel (%d)\n", ret);
1434 goto release_group;
1435 }
1436
1437 /*
1438 * DMA init succeeded. Remove the group marker but keep all resources
1439 * bound to the device, they will be freed at device removal.
1440 */
1441 devres_remove_group(dev, group);
1442
1443 lpi2c_imx->can_use_dma = true;
1444 lpi2c_imx->dma = dma;
1445 return 0;
1446
1447 release_group:
1448 /*
1449 * DMA init failed. Release ALL resources allocated inside this
1450 * group (dma memory, TX channel if already acquired, etc.) so
1451 * that a successful PIO-mode probe does not hold unused resources
1452 * for the entire device lifetime.
1453 */
1454 devres_release_group(dev, group);
1455 return ret;
1456 }
1457
lpi2c_imx_func(struct i2c_adapter * adapter)1458 static u32 lpi2c_imx_func(struct i2c_adapter *adapter)
1459 {
1460 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL |
1461 I2C_FUNC_SMBUS_READ_BLOCK_DATA;
1462 }
1463
1464 static const struct i2c_algorithm lpi2c_imx_algo = {
1465 .xfer = lpi2c_imx_xfer,
1466 .xfer_atomic = lpi2c_imx_xfer_atomic,
1467 .functionality = lpi2c_imx_func,
1468 .reg_target = lpi2c_imx_register_target,
1469 .unreg_target = lpi2c_imx_unregister_target,
1470 };
1471
1472 static const struct of_device_id lpi2c_imx_of_match[] = {
1473 { .compatible = "fsl,imx7ulp-lpi2c", .data = &imx7ulp_lpi2c_hwdata,},
1474 { .compatible = "fsl,imx8qxp-lpi2c", .data = &imx8qxp_lpi2c_hwdata,},
1475 { .compatible = "fsl,imx8qm-lpi2c", .data = &imx8qm_lpi2c_hwdata,},
1476 { }
1477 };
1478 MODULE_DEVICE_TABLE(of, lpi2c_imx_of_match);
1479
lpi2c_imx_probe(struct platform_device * pdev)1480 static int lpi2c_imx_probe(struct platform_device *pdev)
1481 {
1482 struct lpi2c_imx_struct *lpi2c_imx;
1483 struct resource *res;
1484 dma_addr_t phy_addr;
1485 unsigned int temp;
1486 int ret;
1487
1488 lpi2c_imx = devm_kzalloc(&pdev->dev, sizeof(*lpi2c_imx), GFP_KERNEL);
1489 if (!lpi2c_imx)
1490 return -ENOMEM;
1491
1492 lpi2c_imx->hwdata = of_device_get_match_data(&pdev->dev);
1493 if (!lpi2c_imx->hwdata)
1494 return -ENODEV;
1495
1496 lpi2c_imx->base = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
1497 if (IS_ERR(lpi2c_imx->base))
1498 return PTR_ERR(lpi2c_imx->base);
1499
1500 lpi2c_imx->irq = platform_get_irq(pdev, 0);
1501 if (lpi2c_imx->irq < 0)
1502 return lpi2c_imx->irq;
1503
1504 lpi2c_imx->adapter.owner = THIS_MODULE;
1505 lpi2c_imx->adapter.algo = &lpi2c_imx_algo;
1506 lpi2c_imx->adapter.dev.parent = &pdev->dev;
1507 lpi2c_imx->adapter.dev.of_node = pdev->dev.of_node;
1508 strscpy(lpi2c_imx->adapter.name, pdev->name,
1509 sizeof(lpi2c_imx->adapter.name));
1510 phy_addr = (dma_addr_t)res->start;
1511
1512 ret = devm_clk_bulk_get_all(&pdev->dev, &lpi2c_imx->clks);
1513 if (ret < 0)
1514 return dev_err_probe(&pdev->dev, ret, "can't get I2C peripheral clock\n");
1515 lpi2c_imx->num_clks = ret;
1516
1517 ret = of_property_read_u32(pdev->dev.of_node,
1518 "clock-frequency", &lpi2c_imx->bitrate);
1519 if (ret)
1520 lpi2c_imx->bitrate = I2C_MAX_STANDARD_MODE_FREQ;
1521
1522 i2c_set_adapdata(&lpi2c_imx->adapter, lpi2c_imx);
1523 platform_set_drvdata(pdev, lpi2c_imx);
1524
1525 ret = clk_bulk_prepare_enable(lpi2c_imx->num_clks, lpi2c_imx->clks);
1526 if (ret)
1527 return ret;
1528
1529 /*
1530 * Lock the parent clock rate to avoid getting parent clock upon
1531 * each transfer
1532 */
1533 ret = devm_clk_rate_exclusive_get(&pdev->dev, lpi2c_imx->clks[0].clk);
1534 if (ret) {
1535 ret = dev_err_probe(&pdev->dev, ret,
1536 "can't lock I2C peripheral clock rate\n");
1537 goto clk_disable;
1538 }
1539
1540 lpi2c_imx->rate_per = clk_get_rate(lpi2c_imx->clks[0].clk);
1541 if (!lpi2c_imx->rate_per) {
1542 ret = dev_err_probe(&pdev->dev, -EINVAL,
1543 "can't get I2C peripheral clock rate\n");
1544 goto clk_disable;
1545 }
1546
1547 if (lpi2c_imx->hwdata->need_prepare_unprepare_clk)
1548 pm_runtime_set_autosuspend_delay(&pdev->dev, I2C_PM_LONG_TIMEOUT_MS);
1549 else
1550 pm_runtime_set_autosuspend_delay(&pdev->dev, I2C_PM_TIMEOUT);
1551
1552 pm_runtime_use_autosuspend(&pdev->dev);
1553 pm_runtime_get_noresume(&pdev->dev);
1554 pm_runtime_set_active(&pdev->dev);
1555 pm_runtime_enable(&pdev->dev);
1556
1557 /*
1558 * Reset all internal controller registers to avoid effects of any
1559 * state left over from a previous stage (e.g. the bootloader).
1560 *
1561 * The Master block (MCR) is present on every controller, so reset it
1562 * unconditionally. VERID shows whether the target feature is supported.
1563 * Do not touch the Target block (SCR) on a master-only controller to
1564 * avoid an asynchronous SError.
1565 */
1566 writel(MCR_RST, lpi2c_imx->base + LPI2C_MCR);
1567 writel(0, lpi2c_imx->base + LPI2C_MCR);
1568
1569 lpi2c_imx->target_supported = !!(readl(lpi2c_imx->base + LPI2C_VERID) &
1570 VERID_FEATURE_TARGET_PRESENT);
1571 if (lpi2c_imx->target_supported) {
1572 writel(SCR_RST, lpi2c_imx->base + LPI2C_SCR);
1573 writel(0, lpi2c_imx->base + LPI2C_SCR);
1574 }
1575
1576 ret = devm_request_irq(&pdev->dev, lpi2c_imx->irq, lpi2c_imx_isr, IRQF_NO_SUSPEND,
1577 pdev->name, lpi2c_imx);
1578 if (ret)
1579 goto rpm_disable;
1580
1581 temp = readl(lpi2c_imx->base + LPI2C_PARAM);
1582 lpi2c_imx->txfifosize = 1 << (temp & 0x0f);
1583 lpi2c_imx->rxfifosize = 1 << ((temp >> 8) & 0x0f);
1584
1585 /* Init optional bus recovery function */
1586 ret = lpi2c_imx_init_recovery_info(lpi2c_imx, pdev);
1587 /* Give it another chance if pinctrl used is not ready yet */
1588 if (ret == -EPROBE_DEFER)
1589 goto rpm_disable;
1590
1591 /* Init DMA */
1592 ret = lpi2c_dma_init(&pdev->dev, phy_addr);
1593 if (ret) {
1594 if (ret == -EPROBE_DEFER)
1595 goto rpm_disable;
1596 dev_info(&pdev->dev, "use pio mode\n");
1597 }
1598
1599 ret = i2c_add_adapter(&lpi2c_imx->adapter);
1600 if (ret)
1601 goto rpm_disable;
1602
1603 pm_runtime_put_autosuspend(&pdev->dev);
1604
1605 dev_info(&lpi2c_imx->adapter.dev, "LPI2C adapter registered\n");
1606
1607 return 0;
1608
1609 rpm_disable:
1610 pm_runtime_dont_use_autosuspend(&pdev->dev);
1611 pm_runtime_disable(&pdev->dev);
1612 pm_runtime_set_suspended(&pdev->dev);
1613 pm_runtime_put_noidle(&pdev->dev);
1614 clk_disable:
1615 clk_bulk_disable_unprepare(lpi2c_imx->num_clks, lpi2c_imx->clks);
1616
1617 return ret;
1618 }
1619
lpi2c_imx_remove(struct platform_device * pdev)1620 static void lpi2c_imx_remove(struct platform_device *pdev)
1621 {
1622 struct lpi2c_imx_struct *lpi2c_imx = platform_get_drvdata(pdev);
1623
1624 i2c_del_adapter(&lpi2c_imx->adapter);
1625
1626 pm_runtime_disable(&pdev->dev);
1627 pm_runtime_dont_use_autosuspend(&pdev->dev);
1628 }
1629
lpi2c_runtime_suspend(struct device * dev)1630 static int __maybe_unused lpi2c_runtime_suspend(struct device *dev)
1631 {
1632 struct lpi2c_imx_struct *lpi2c_imx = dev_get_drvdata(dev);
1633 bool need_prepare_unprepare_clk = lpi2c_imx->hwdata->need_prepare_unprepare_clk;
1634 bool need_request_free_irq = lpi2c_imx->hwdata->need_request_free_irq;
1635
1636 if (need_request_free_irq)
1637 devm_free_irq(dev, lpi2c_imx->irq, lpi2c_imx);
1638
1639 if (need_prepare_unprepare_clk)
1640 clk_bulk_disable_unprepare(lpi2c_imx->num_clks, lpi2c_imx->clks);
1641 else
1642 clk_bulk_disable(lpi2c_imx->num_clks, lpi2c_imx->clks);
1643 pinctrl_pm_select_sleep_state(dev);
1644
1645 return 0;
1646 }
1647
lpi2c_runtime_resume(struct device * dev)1648 static int __maybe_unused lpi2c_runtime_resume(struct device *dev)
1649 {
1650 struct lpi2c_imx_struct *lpi2c_imx = dev_get_drvdata(dev);
1651 bool need_prepare_unprepare_clk = lpi2c_imx->hwdata->need_prepare_unprepare_clk;
1652 bool need_request_free_irq = lpi2c_imx->hwdata->need_request_free_irq;
1653 int ret;
1654
1655 pinctrl_pm_select_default_state(dev);
1656 if (need_prepare_unprepare_clk) {
1657 ret = clk_bulk_prepare_enable(lpi2c_imx->num_clks, lpi2c_imx->clks);
1658 if (ret) {
1659 dev_err(dev, "failed to enable I2C clock, ret=%d\n", ret);
1660 return ret;
1661 }
1662 } else {
1663 ret = clk_bulk_enable(lpi2c_imx->num_clks, lpi2c_imx->clks);
1664 if (ret) {
1665 dev_err(dev, "failed to enable clock %d\n", ret);
1666 return ret;
1667 }
1668 }
1669
1670 if (need_request_free_irq) {
1671 ret = devm_request_irq(dev, lpi2c_imx->irq, lpi2c_imx_isr, IRQF_NO_SUSPEND,
1672 dev_name(dev), lpi2c_imx);
1673 if (ret) {
1674 dev_err(dev, "can't claim irq %d\n", lpi2c_imx->irq);
1675 return ret;
1676 }
1677 }
1678
1679 return 0;
1680 }
1681
lpi2c_suspend_noirq(struct device * dev)1682 static int __maybe_unused lpi2c_suspend_noirq(struct device *dev)
1683 {
1684 struct lpi2c_imx_struct *lpi2c_imx = dev_get_drvdata(dev);
1685 int ret;
1686
1687 i2c_mark_adapter_suspended(&lpi2c_imx->adapter);
1688
1689 ret = pm_runtime_force_suspend(dev);
1690 if (ret) {
1691 i2c_mark_adapter_resumed(&lpi2c_imx->adapter);
1692 return ret;
1693 }
1694
1695 return 0;
1696 }
1697
lpi2c_resume_noirq(struct device * dev)1698 static int __maybe_unused lpi2c_resume_noirq(struct device *dev)
1699 {
1700 struct lpi2c_imx_struct *lpi2c_imx = dev_get_drvdata(dev);
1701 int ret;
1702
1703 ret = pm_runtime_force_resume(dev);
1704 if (ret)
1705 return ret;
1706
1707 /*
1708 * If the I2C module powers down during system suspend,
1709 * the register values will be lost. Therefore, reinitialize
1710 * the target when the system resumes.
1711 */
1712 if (lpi2c_imx->target)
1713 lpi2c_imx_target_init(lpi2c_imx);
1714
1715 i2c_mark_adapter_resumed(&lpi2c_imx->adapter);
1716
1717 return 0;
1718 }
1719
lpi2c_suspend(struct device * dev)1720 static int lpi2c_suspend(struct device *dev)
1721 {
1722 /*
1723 * Some I2C devices may need the I2C controller to remain active
1724 * during resume_noirq() or suspend_noirq(). If the controller is
1725 * autosuspended, there is no way to wake it up once runtime PM is
1726 * disabled (in suspend_late()).
1727 *
1728 * During system resume, the I2C controller will be available only
1729 * after runtime PM is re-enabled (in resume_early()). However, this
1730 * may be too late for some devices.
1731 *
1732 * Wake up the controller in the suspend() callback while runtime PM
1733 * is still enabled. The I2C controller will remain available until
1734 * the suspend_noirq() callback (pm_runtime_force_suspend()) is
1735 * called. During resume, the I2C controller can be restored by the
1736 * resume_noirq() callback (pm_runtime_force_resume()).
1737 *
1738 * Finally, the resume() callback re-enables autosuspend, ensuring
1739 * the I2C controller remains available until the system enters
1740 * suspend_noirq() and from resume_noirq().
1741 */
1742 return pm_runtime_resume_and_get(dev);
1743 }
1744
lpi2c_resume(struct device * dev)1745 static int lpi2c_resume(struct device *dev)
1746 {
1747 pm_runtime_put_autosuspend(dev);
1748
1749 return 0;
1750 }
1751
1752 static const struct dev_pm_ops lpi2c_pm_ops = {
1753 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(lpi2c_suspend_noirq,
1754 lpi2c_resume_noirq)
1755 SYSTEM_SLEEP_PM_OPS(lpi2c_suspend, lpi2c_resume)
1756 SET_RUNTIME_PM_OPS(lpi2c_runtime_suspend,
1757 lpi2c_runtime_resume, NULL)
1758 };
1759
1760 static struct platform_driver lpi2c_imx_driver = {
1761 .probe = lpi2c_imx_probe,
1762 .remove = lpi2c_imx_remove,
1763 .driver = {
1764 .name = DRIVER_NAME,
1765 .of_match_table = lpi2c_imx_of_match,
1766 .pm = &lpi2c_pm_ops,
1767 },
1768 };
1769
1770 module_platform_driver(lpi2c_imx_driver);
1771
1772 MODULE_AUTHOR("Gao Pan <pandy.gao@nxp.com>");
1773 MODULE_DESCRIPTION("I2C adapter driver for LPI2C bus");
1774 MODULE_LICENSE("GPL");
1775