1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * drivers/i2c/busses/i2c-sunxi.c
4 *
5 * Copyright (C) 2013 Allwinner.
6 * Pan Nan <pannan@reuuimllatech.com>
7 *
8 * SUNXI TWI Controller Driver
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License as
12 * published by the Free Software Foundation; either version 2 of
13 * the License, or (at your option) any later version.
14 *
15 * 2013.5.3 Mintow <duanmintao@allwinnertech.com>
16 * Adapt to all the new chip of Allwinner. Support sun8i/sun9i
17 *
18 * 2021.8.15 Lewis <liuyu@allwinnertech.com>
19 * Optimization the probe function and all the function involved code
20 *
21 * 2021.10.13 Lewis <liuyu@allwinnertec.com>
22 * Refactored i2c xfer code
23 */
24
25 //#define DEBUG
26 #include <linux/kernel.h>
27 #include <linux/module.h>
28 #include <linux/i2c.h>
29 #include <linux/sched.h>
30 #include <linux/delay.h>
31 #include <linux/interrupt.h>
32 #include <linux/platform_device.h>
33 #include <linux/err.h>
34 #include <linux/clk.h>
35 #include <linux/clkdev.h>
36 #include <linux/clk-provider.h>
37 #include <linux/reset.h>
38 #include <linux/slab.h>
39 #include <linux/io.h>
40 #include <linux/gpio.h>
41 #include <linux/pinctrl/consumer.h>
42 #include <linux/pm_runtime.h>
43 #include <linux/dma-mapping.h>
44 #include <linux/dmaengine.h>
45 #include <linux/dmapool.h>
46 #include <asm/uaccess.h>
47 #include <linux/time.h>
48 #include <linux/of.h>
49 #include <linux/of_irq.h>
50 #include <linux/of_address.h>
51 #include <linux/regulator/consumer.h>
52
53 /* I2C Register Offset */
54 /* 31:8bit reserved,7-1bit for slave addr,0 bit for GCE */
55 #define I2C_ADDR (0x00)
56 /* 31:8bit reserved,7-0bit for second addr in 10bit addr */
57 #define I2C_XADDR (0x04)
58 /* 31:8bit reserved, 7-0bit send or receive data byte */
59 #define I2C_DATA (0x08)
60 /* INT_EN,BUS_EN,M_STA,INT_FLAG,A_ACK */
61 #define I2C_CNTR (0x0C)
62 /* 28 interrupt types + 0xF8 normal type = 29 */
63 #define I2C_STAT (0x10)
64 /* 31:7bit reserved,6-3bit,CLK_M,2-0bit CLK_N */
65 #define I2C_CCR (0x14)
66 /* 31:1bit reserved;0bit,write 1 to clear 0. */
67 #define I2C_SRST (0x18)
68 /* 31:2bit reserved,1:0 bit data byte follow read command */
69 #define I2C_EFR (0x1C)
70 /* 31:6bits reserved 5:0bit for sda&scl control*/
71 #define I2C_LCR (0x20)
72 /* 23:16 VER_BIG 7:0:VER_SMALL */
73 #define I2C_VERSION (0xFC)
74
75 /* I2C_DATA */
76 #define I2C_DATA_MASK (0xff << 0)
77
78 /* I2C_CNTR */
79 #define CLK_COUNT_MODE (0x1 << 0)
80 /* set 1 to send A_ACK,then low level on SDA */
81 #define A_ACK (0x1 << 2)
82 /* INT_FLAG,interrupt status flag: set '1' when interrupt coming */
83 #define INT_FLAG (0x1 << 3)
84 /* M_STP,Automatic clear 0 */
85 #define M_STP (0x1 << 4)
86 /* M_STA,atutomatic clear 0 */
87 #define M_STA (0x1 << 5)
88 /* BUS_EN, master mode should be set 1.*/
89 #define BUS_EN (0x1 << 6)
90 /* INT_EN, set 1 to enable interrupt.*/
91 #define INT_EN (0x1 << 7) /* INT_EN */
92 /* 31:8 bit reserved */
93
94 /* I2C_STAT */
95 /*
96 * -------------------------------------------------------------------
97 * Code Status
98 * 00h Bus error
99 * 08h START condition transmitted
100 * 10h Repeated START condition transmitted
101 * 18h Address + Write bit transmitted, ACK received
102 * 20h Address + Write bit transmitted, ACK not received
103 * 28h Data byte transmitted in master mode, ACK received
104 * 30h Data byte transmitted in master mode, ACK not received
105 * 38h Arbitration lost in address or data byte
106 * 40h Address + Read bit transmitted, ACK received
107 * 48h Address + Read bit transmitted, ACK not received
108 * 50h Data byte received in master mode, ACK transmitted
109 * 58h Data byte received in master mode, not ACK transmitted
110 * 60h Slave address + Write bit received, ACK transmitted
111 * 68h Arbitration lost in address as master,
112 * slave address + Write bit received, ACK transmitted
113 * 70h General Call address received, ACK transmitted
114 * 78h Arbitration lost in address as master,
115 * General Call address received, ACK transmitted
116 * 80h Data byte received after slave address received, ACK transmitted
117 * 88h Data byte received after slave address received, not ACK transmitted
118 * 90h Data byte received after General Call received, ACK transmitted
119 * 98h Data byte received after General Call received, not ACK transmitted
120 * A0h STOP or repeated START condition received in slave mode
121 * A8h Slave address + Read bit received, ACK transmitted
122 * B0h Arbitration lost in address as master,
123 * slave address + Read bit received, ACK transmitted
124 * B8h Data byte transmitted in slave mode, ACK received
125 * C0h Data byte transmitted in slave mode, ACK not received
126 * C8h Last byte transmitted in slave mode, ACK received
127 * D0h Second Address byte + Write bit transmitted, ACK received
128 * D8h Second Address byte + Write bit transmitted, ACK not received
129 * F8h No relevant status information or no interrupt
130 *--------------------------------------------------------------------------
131 */
132 #define I2C_STAT_MASK (0xff)
133 /* 7:0 bits use only,default is 0xF8 */
134 #define I2C_STAT_BUS_ERR (0x00) /* BUS ERROR */
135 /* timeout when sending 9th scl clk*/
136 #define I2C_STAT_TIMEOUT_9CLK (0x01)
137 /*start can't send out*/
138 #define I2C_STAT_TX_NSTA (0x02) //defined by us not spec
139 /* Master mode use only */
140 #define I2C_STAT_TX_STA (0x08) /* START condition transmitted */
141 /* Repeated START condition transmitted */
142 #define I2C_STAT_TX_RESTA (0x10)
143 /* Address+Write bit transmitted, ACK received */
144 #define I2C_STAT_TX_AW_ACK (0x18)
145 /* Address+Write bit transmitted, ACK not received */
146 #define I2C_STAT_TX_AW_NAK (0x20)
147 /* data byte transmitted in master mode,ack received */
148 #define I2C_STAT_TXD_ACK (0x28)
149 /* data byte transmitted in master mode ,ack not received */
150 #define I2C_STAT_TXD_NAK (0x30)
151 /* arbitration lost in address or data byte */
152 #define I2C_STAT_ARBLOST (0x38)
153 /* Address+Read bit transmitted, ACK received */
154 #define I2C_STAT_TX_AR_ACK (0x40)
155 /* Address+Read bit transmitted, ACK not received */
156 #define I2C_STAT_TX_AR_NAK (0x48)
157 /* data byte received in master mode ,ack transmitted */
158 #define I2C_STAT_RXD_ACK (0x50)
159 /* date byte received in master mode,not ack transmitted */
160 #define I2C_STAT_RXD_NAK (0x58)
161 /* Slave mode use only */
162 /* Slave address+Write bit received, ACK transmitted */
163 #define I2C_STAT_RXWS_ACK (0x60)
164 #define I2C_STAT_ARBLOST_RXWS_ACK (0x68)
165 /* General Call address received, ACK transmitted */
166 #define I2C_STAT_RXGCAS_ACK (0x70)
167 #define I2C_STAT_ARBLOST_RXGCAS_ACK (0x78)
168 #define I2C_STAT_RXDS_ACK (0x80)
169 #define I2C_STAT_RXDS_NAK (0x88)
170 #define I2C_STAT_RXDGCAS_ACK (0x90)
171 #define I2C_STAT_RXDGCAS_NAK (0x98)
172 #define I2C_STAT_RXSTPS_RXRESTAS (0xA0)
173 #define I2C_STAT_RXRS_ACK (0xA8)
174 #define I2C_STAT_ARBLOST_SLAR_ACK (0xB0)
175 /* 10bit Address, second part of address */
176 /* Second Address byte+Write bit transmitted,ACK received */
177 #define I2C_STAT_TX_SAW_ACK (0xD0)
178 /* Second Address byte+Write bit transmitted,ACK not received */
179 #define I2C_STAT_TX_SAW_NAK (0xD8)
180 /* No relevant status information,INT_FLAG = 0 */
181 #define I2C_STAT_IDLE (0xF8)
182 /* status erro*/
183 #define I2C_STAT_ERROR (0xF9)
184
185 /*I2C_CCR*/
186 /*
187 * Fin is APB CLOCK INPUT;
188 * Fsample = F0 = Fin/2^CLK_N;
189 * F1 = F0/(CLK_M+1);
190 *
191 * Foscl = F1/10 = Fin/(2^CLK_N * (CLK_M+1)*10);
192 * Foscl is clock SCL;standard mode:100KHz or fast mode:400KHz
193 */
194 #define I2C_CLK_DUTY (0x1 << 7) /* 7bit */
195 #define I2C_CLK_DIV_M_OFFSET 3
196 #define I2C_CLK_DIV_M (0xf << I2C_CLK_DIV_M_OFFSET) /* 6:3bit */
197 #define I2C_CLK_DIV_N_OFFSET 0
198 #define I2C_CLK_DIV_N (0x7 << I2C_CLK_DIV_N_OFFSET) /* 2:0bit */
199
200 /* I2C_SRST */
201 /* write 1 to clear 0, when complete soft reset clear 0 */
202 #define I2C_SOFT_RST (0x1 << 0)
203
204 /* I2C_EFR*/
205 /* default -- 0x0 */
206 /* 00:no,01: 1byte, 10:2 bytes, 11: 3bytes */
207 #define I2C_EFR_MASK (0x3 << 0)
208 #define NO_DATA_WROTE (0x0 << 0)
209 #define BYTE_DATA_WROTE (0x1 << 0)
210 #define BYTES_DATA1_WROTE (0x2 << 0)
211 #define BYTES_DATA2_WROTE (0x3 << 0)
212
213 /* I2C_LCR*/
214 #define SCL_STATE (0x1 << 5)
215 #define SDA_STATE (0x1 << 4)
216 #define SCL_CTL (0x1 << 3)
217 #define SCL_CTL_EN (0x1 << 2)
218 #define SDA_CTL (0x1 << 1)
219 #define SDA_CTL_EN (0x1 << 0)
220
221 #define I2C_DRV_CTRL (0x200)
222 #define I2C_DRV_CFG (0x204)
223 #define I2C_DRV_SLV (0x208)
224 #define I2C_DRV_FMT (0x20C)
225 #define I2C_DRV_BUS_CTRL (0x210)
226 #define I2C_DRV_INT_CTRL (0x214)
227 #define I2C_DRV_DMA_CFG (0x218)
228 #define I2C_DRV_FIFO_CON (0x21C)
229 #define I2C_DRV_SEND_FIFO_ACC (0x300)
230 #define I2C_DRV_RECV_FIFO_ACC (0x304)
231
232 /* I2C_DRV_CTRL */
233 /* 0:module disable; 1:module enable; only use in I2C master Mode */
234 #define I2C_DRV_EN (0x01 << 0)
235 /* 0:normal; 1:reset */
236 #define SOFT_RESET (0x01 << 1)
237 #define TIMEOUT_N_OFFSET 8
238 #define TIMEOUT_N (0xff << TIMEOUT_N_OFFSET)
239 #define I2C_DRV_STAT_OFFSET 16
240 #define I2C_DRV_STAT_MASK (0xff << I2C_DRV_STAT_OFFSET)
241
242 #define TRAN_RESULT (0x07 << 24)
243 /* 0:send slave_id + W; 1:do not send slave_id + W */
244 #define READ_TRAN_MODE (0x01 << 28)
245 /* 0:restart; 1:STOP + START */
246 #define RESTART_MODE (0x01 << 29)
247 /* 0:transmission idle; 1:start transmission */
248 #define START_TRAN (0x01 << 31)
249
250 /* I2C_DRV_CFG */
251 #define PACKET_CNT_OFFSET 0
252 #define PACKET_CNT (0xffff << PACKET_CNT_OFFSET)
253 #define PACKET_INTERVAL_OFFSET 16
254 #define PACKET_INTERVAL (0xffff << PACKET_INTERVAL_OFFSET)
255
256 /* I2C_DRV_SLV */
257 #define SLV_ID_X_OFFSET 0
258 #define SLV_ID_X (0xff << SLV_ID_X_OFFSET)
259 #define CMD (0x01 << 8)
260 #define SLV_ID_OFFSET 9
261 #define SLV_ID (0x7f << SLV_ID_OFFSET)
262
263 /* I2C_DRV_FMT */
264 /* how many bytes be sent/received as data */
265 #define DATA_BYTE_OFFSET 0
266 #define DATA_BYTE (0xffff << DATA_BYTE_OFFSET)
267 /* how many btyes be sent as slave device reg address */
268 #define ADDR_BYTE_OFFSET 16
269 #define ADDR_BYTE (0xff << ADDR_BYTE_OFFSET)
270
271 /* I2C_DRV_BUS_CTRL */
272 /* SDA manual output en */
273 #define SDA_MOE (0x01 << 0)
274 /* SCL manual output en */
275 #define SCL_MOE (0x01 << 1)
276 /* SDA manual output value */
277 #define SDA_MOV (0x01 << 2)
278 /* SCL manual output value */
279 #define SCL_MOV (0x01 << 3)
280 /* SDA current status */
281 #define SDA_STA (0x01 << 6)
282 /* SCL current status */
283 #define SCL_STA (0x01 << 7)
284 #define I2C_DRV_CLK_M_OFFSET 8
285 #define I2C_DRV_CLK_M (0x0f << I2C_DRV_CLK_M_OFFSET)
286 #define I2C_DRV_CLK_N_OFFSET 12
287 #define I2C_DRV_CLK_N (0x07 << I2C_DRV_CLK_N_OFFSET)
288 #define I2C_DRV_CLK_DUTY (0x01 << 15)
289 #define I2C_DRV_COUNT_MODE (0x01 << 16)
290
291 /* I2C_DRV_INT_CTRL */
292 #define TRAN_COM_PD (0x1 << 0)
293 #define TRAN_ERR_PD (0x1 << 1)
294 #define TX_REQ_PD (0x1 << 2)
295 #define RX_REQ_PD (0x1 << 3)
296 #define TRAN_COM_INT_EN (0x1 << 16)
297 #define TRAN_ERR_INT_EN (0x1 << 17)
298 #define TX_REQ_INT_EN (0x1 << 18)
299 #define RX_REQ_INT_EN (0x1 << 19)
300 #define I2C_DRV_INT_EN_MASK (0x0f << 16)
301 #define I2C_DRV_INT_STA_MASK (0x0f << 0)
302
303 /* I2C_DRV_DMA_CFG */
304 #define TX_TRIG_OFFSET 0
305 #define TX_TRIG (0x3f << TX_TRIG_OFFSET)
306 #define DMA_TX_EN (0x01 << 8)
307 #define RX_TRIG_OFFSET 16
308 #define RX_TRIG (0x3f << RX_TRIG_OFFSET)
309 #define DMA_RX_EN (0x01 << 24)
310 #define I2C_DRQEN_MASK (DMA_TX_EN | DMA_RX_EN)
311
312 /* I2C_DRV_FIFO_CON */
313 /* the number of data in SEND_FIFO */
314 #define SEND_FIFO_CONTENT_OFFSET 0
315 #define SEND_FIFO_CONTENT (0x3f << SEND_FIFO_CONTENT_OFFSET)
316 /* Set this bit to clear SEND_FIFO pointer, and this bit cleared automatically */
317 #define SEND_FIFO_CLEAR (0x01 << 5)
318 #define RECV_FIFO_CONTENT_OFFSET 16
319 #define RECV_FIFO_CONTENT (0x3f << RECV_FIFO_CONTENT_OFFSET)
320 #define RECV_FIFO_CLEAR (0x01 << 22)
321
322 /* I2C_DRV_SEND_FIFO_ACC */
323 #define SEND_DATA_FIFO (0xff << 0)
324 /* I2C_DRV_RECV_FIFO_ACC */
325 #define RECV_DATA_FIFO (0xff << 0)
326 /* end of i2c regiter offset */
327
328 #define LOOP_TIMEOUT 1024
329 #define STANDDARD_FREQ 100000
330 #define AUTOSUSPEND_TIMEOUT 5000
331 #define HEXADECIMAL (0x10)
332 #define REG_INTERVAL (0x04)
333 #define REG_CL (0x0c)
334 #define DMA_THRESHOLD 32
335 #define MAX_FIFO 32
336 #define DMA_TIMEOUT 1000
337 #define SUNXI_I2C_DEV_NAME "sunxi-i2c"
338 #define SUNXI_I2C_ID_FORMAT SUNXI_I2C_DEV_NAME"%u"
339
340 /* i2c driver result i2c->result */
341 #define RESULT_IDLE 0
342 #define RESULT_RUNNING 1
343 #define RESULT_COMPLETE 2
344 #define RESULT_ERR 3
345 #define RESULT_NUM 4
346
347 #define I2C_READ true
348 #define I2C_WRITE false
349
350 /* i2c transfer status i2c->status */
351 enum {
352 I2C_XFER_IDLE = 0x1,
353 I2C_XFER_START = 0x2,
354 I2C_XFER_RUNNING = 0x4,
355 };
356
357 struct sunxi_i2c_dma {
358 struct dma_chan *chan;
359 dma_addr_t dma_buf;
360 unsigned int dma_len;
361 enum dma_transfer_direction dma_transfer_dir;
362 enum dma_data_direction dma_data_dir;
363 };
364
365 struct sunxi_i2c {
366 /* i2c framework datai */
367 struct i2c_adapter adap;
368 struct platform_device *pdev;
369 struct device *dev;
370 struct i2c_msg *msg;
371 /* the total num of msg */
372 unsigned int msg_num;
373 /* the current msg index -> msg[msg_idx] */
374 unsigned int msg_idx;
375 /* the current msg's buf data index -> msg->buf[buf_idx] */
376 unsigned int buf_idx;
377 unsigned int result;
378
379 /* dts data */
380 struct resource *res;
381 void __iomem *base_addr;
382 struct clk *bus_clk;
383 struct reset_control *reset;
384 unsigned int bus_freq;
385 struct regulator *regulator;
386 struct pinctrl *pctrl;
387 int irq;
388 int irq_flag;
389 unsigned int twi_drv_used;
390 unsigned int no_suspend;
391 unsigned int pkt_interval;
392 struct sunxi_i2c_dma *dma_tx;
393 struct sunxi_i2c_dma *dma_rx;
394 struct sunxi_i2c_dma *dma_using;
395 u8 *dma_buf;
396
397 /* other data */
398 int bus_num;
399 unsigned int status; /* start, running, idle */
400 unsigned int debug_state; /* log the twi machine state */
401
402 spinlock_t lock; /* syn */
403 wait_queue_head_t wait;
404 struct completion cmd_complete;
405
406 unsigned int reg1[16]; /* store the i2c engined mode resigter status */
407 unsigned int reg2[16]; /* store the i2c drv mode regiter status */
408 };
409
410 #ifdef DEBUG
sunxi_i2c_dump_reg(struct sunxi_i2c * i2c,u32 offset,u32 len)411 void sunxi_i2c_dump_reg(struct sunxi_i2c *i2c, u32 offset, u32 len)
412 {
413 u32 i;
414 u8 buf[64], cnt = 0;
415
416 for (i = 0; i < len; i = i + REG_INTERVAL) {
417 if (i%HEXADECIMAL == 0)
418 cnt += sprintf(buf + cnt, "0x%08x: ",
419 (u32)(i2c->res->start + offset + i));
420
421 cnt += sprintf(buf + cnt, "%08x ",
422 readl(i2c->base_addr + offset + i));
423
424 if (i%HEXADECIMAL == REG_CL) {
425 pr_warn("%s\n", buf);
426 cnt = 0;
427 }
428 }
429 }
430 #else
sunxi_i2c_dump_reg(struct sunxi_i2c * i2c,u32 offset,u32 len)431 void sunxi_i2c_dump_reg(struct sunxi_i2c *i2c, u32 offset, u32 len) { }
432 #endif
433
434 /* clear the interrupt flag,
435 * the i2c bus xfer status (register I2C_STAT) will changed as following
436 */
sunxi_i2c_engine_clear_irq(void __iomem * base_addr)437 static inline void sunxi_i2c_engine_clear_irq(void __iomem *base_addr)
438 {
439 u32 reg_val = readl(base_addr + I2C_CNTR);
440
441 /* start and stop bit should be 0 */
442 reg_val |= INT_FLAG;
443 reg_val &= ~(M_STA | M_STP);
444
445 writel(reg_val, base_addr + I2C_CNTR);
446 }
447
448 /* only when get the last data, we will clear the flag when stop */
449 static inline void
sunxi_i2c_engine_get_byte(void __iomem * base_addr,unsigned char * buffer)450 sunxi_i2c_engine_get_byte(void __iomem *base_addr, unsigned char *buffer)
451 {
452 *buffer = (unsigned char)(I2C_DATA_MASK & readl(base_addr + I2C_DATA));
453 }
454
455 /* write data and clear irq flag to trigger send flow */
456 static inline void
sunxi_i2c_engine_put_byte(struct sunxi_i2c * i2c,const unsigned char * buffer)457 sunxi_i2c_engine_put_byte(struct sunxi_i2c *i2c, const unsigned char *buffer)
458 {
459 unsigned int reg_val;
460
461 reg_val = *buffer & I2C_DATA_MASK;
462 writel(reg_val, i2c->base_addr + I2C_DATA);
463
464 dev_dbg(i2c->dev, "engine-mode: data 0x%x xfered\n", reg_val);
465 }
466
sunxi_i2c_engine_enable_irq(void __iomem * base_addr)467 static inline void sunxi_i2c_engine_enable_irq(void __iomem *base_addr)
468 {
469 unsigned int reg_val = readl(base_addr + I2C_CNTR);
470 /*
471 * 1 when enable irq for next operation, set intflag to 0 to prevent
472 * to clear it by a mistake (intflag bit is write-1-to-clear bit)
473 * 2 Similarly, mask START bit and STOP bit to prevent to set it
474 * twice by a mistake (START bit and STOP bit are self-clear-to-0 bits)
475 */
476 reg_val |= INT_EN;
477 reg_val &= ~(INT_FLAG) | M_STA | M_STP;
478 writel(reg_val, base_addr + I2C_CNTR);
479 }
480
sunxi_i2c_engine_disable_irq(void __iomem * base_addr)481 static inline void sunxi_i2c_engine_disable_irq(void __iomem *base_addr)
482 {
483 unsigned int reg_val = readl(base_addr + I2C_CNTR);
484
485 reg_val &= ~INT_EN;
486 reg_val &= ~(INT_FLAG | M_STA | M_STP);
487 writel(reg_val, base_addr + I2C_CNTR);
488 }
489
sunxi_i2c_bus_enable(struct sunxi_i2c * i2c)490 static inline void sunxi_i2c_bus_enable(struct sunxi_i2c *i2c)
491 {
492 unsigned int reg_val;
493
494 if (i2c->twi_drv_used) {
495 reg_val = readl(i2c->base_addr + I2C_DRV_CTRL);
496 reg_val |= I2C_DRV_EN;
497 writel(reg_val, i2c->base_addr + I2C_DRV_CTRL);
498 } else {
499 reg_val = readl(i2c->base_addr + I2C_CNTR);
500 reg_val |= BUS_EN;
501 writel(reg_val, i2c->base_addr + I2C_CNTR);
502 }
503 }
504
sunxi_i2c_bus_disable(struct sunxi_i2c * i2c)505 static inline void sunxi_i2c_bus_disable(struct sunxi_i2c *i2c)
506 {
507 unsigned int reg_val;
508
509 if (i2c->twi_drv_used) {
510 reg_val = readl(i2c->base_addr + I2C_DRV_CTRL);
511 reg_val &= ~I2C_DRV_EN;
512 writel(reg_val, i2c->base_addr + I2C_DRV_CTRL);
513 } else {
514 reg_val = readl(i2c->base_addr + I2C_CNTR);
515 reg_val &= ~BUS_EN;
516 writel(reg_val, i2c->base_addr + I2C_CNTR);
517 }
518 }
519
520 /* trigger start signal, the start bit will be cleared automatically */
sunxi_i2c_engine_set_start(void __iomem * base_addr)521 static inline void sunxi_i2c_engine_set_start(void __iomem *base_addr)
522 {
523 u32 reg_val = readl(base_addr + I2C_CNTR);
524
525 reg_val |= M_STA;
526 reg_val &= ~(INT_FLAG);
527
528 writel(reg_val, base_addr + I2C_CNTR);
529 }
530
531 /* get start bit status, poll if start signal is sent
532 * return 0 on success, return 1 on failed
533 */
sunxi_i2c_engine_get_start(void __iomem * base_addr)534 static inline unsigned int sunxi_i2c_engine_get_start(void __iomem *base_addr)
535 {
536 unsigned int reg_val = readl(base_addr + I2C_CNTR);
537
538 return reg_val & M_STA;
539 }
540
541 /* trigger stop signal and clear int flag
542 * the stop bit will be cleared automatically
543 */
sunxi_i2c_engine_set_stop(void __iomem * base_addr)544 static inline void sunxi_i2c_engine_set_stop(void __iomem *base_addr)
545 {
546 u32 reg_val = readl(base_addr + I2C_CNTR);
547
548 reg_val |= M_STP;
549 reg_val &= ~INT_FLAG;
550 writel(reg_val, base_addr + I2C_CNTR);
551 }
552
553 /* get stop bit status, poll if stop signal is sent */
sunxi_i2c_engine_get_stop(void __iomem * base_addr)554 static inline unsigned int sunxi_i2c_engine_get_stop(void __iomem *base_addr)
555 {
556 unsigned int reg_val = readl(base_addr + I2C_CNTR);
557
558 return reg_val & M_STP;
559 }
560
561 /* when sending ack or nack, it will send ack automatically */
sunxi_i2c_engine_enable_ack(void __iomem * base_addr)562 static inline void sunxi_i2c_engine_enable_ack(void __iomem *base_addr)
563 {
564 u32 reg_val = readl(base_addr + I2C_CNTR);
565
566 reg_val |= A_ACK;
567
568 writel(reg_val, base_addr + I2C_CNTR);
569 }
570
sunxi_i2c_engine_disable_ack(void __iomem * base_addr)571 static inline void sunxi_i2c_engine_disable_ack(void __iomem *base_addr)
572 {
573 u32 reg_val = readl(base_addr + I2C_CNTR);
574
575 reg_val &= ~A_ACK;
576
577 writel(reg_val, base_addr + I2C_CNTR);
578 }
579
580 /* check the engine-mode or drv-mode irq coming or not with irq enable or not
581 * return 0 on engine-mode interrupt is coming with irq is enabled
582 * return 1 on drv-mode interrupt is coming with irq is enabled
583 * otherwise return the error num
584 */
sunxi_i2c_check_irq(struct sunxi_i2c * i2c)585 static inline unsigned int sunxi_i2c_check_irq(struct sunxi_i2c *i2c)
586 {
587 u32 status;
588
589 if (i2c->twi_drv_used) {
590 status = readl(i2c->base_addr + I2C_DRV_INT_CTRL);
591 if ((status & I2C_DRV_INT_EN_MASK) && (status & I2C_DRV_INT_STA_MASK))
592 return 1;
593 } else {
594 status = readl(i2c->base_addr + I2C_CNTR);
595 if ((status & INT_EN) && (status & INT_FLAG))
596 return 0;
597 }
598
599 return -EINVAL;
600 }
601
602 /* get the i2c controller current xfer status */
sunxi_i2c_get_xfer_sta(struct sunxi_i2c * i2c)603 static inline unsigned int sunxi_i2c_get_xfer_sta(struct sunxi_i2c *i2c)
604 {
605 u32 status;
606
607 if (i2c->twi_drv_used) {
608 status = readl(i2c->base_addr + I2C_DRV_CTRL) & I2C_DRV_STAT_MASK;
609 status >>= I2C_DRV_STAT_OFFSET;
610 } else {
611 status = readl(i2c->base_addr + I2C_STAT) & I2C_STAT_MASK;
612 }
613
614 return status;
615 }
616
617 /* set i2c controller clock
618 * clk_n: clock divider factor n
619 * clk_m: clock divider factor m
620 */
sunxi_i2c_set_clock(struct sunxi_i2c * i2c,u8 clk_m,u8 clk_n)621 static void sunxi_i2c_set_clock(struct sunxi_i2c *i2c, u8 clk_m, u8 clk_n)
622 {
623 u32 clk_n_mask, clk_n_offset, clk_m_offset, clk_m_mask;
624 u32 reg, duty, reg_val;
625
626 /* @IP-TODO
627 * drv-mode set clkm and clkn bit to adjust frequency, finally the
628 * I2C_DRV_BUS_CTRL register will not be changed, the value of the I2C_CCR
629 * register will represent the current drv-mode operating frequency
630 */
631 if (i2c->twi_drv_used) {
632 reg = I2C_DRV_BUS_CTRL;
633 clk_n_mask = I2C_DRV_CLK_N;
634 clk_n_offset = I2C_DRV_CLK_N_OFFSET;
635 clk_m_mask = I2C_DRV_CLK_M;
636 clk_m_offset = I2C_DRV_CLK_M_OFFSET;
637 duty = I2C_DRV_CLK_DUTY;
638 } else {
639 reg = I2C_CCR;
640 clk_n_mask = I2C_CLK_DIV_N;
641 clk_n_offset = I2C_CLK_DIV_N_OFFSET;
642 clk_m_mask = I2C_CLK_DIV_M;
643 clk_m_offset = I2C_CLK_DIV_M_OFFSET;
644 duty = I2C_CLK_DUTY;
645 }
646
647 reg_val = readl(i2c->base_addr + reg);
648
649 reg_val &= ~(clk_m_mask | clk_n_mask);
650 reg_val |= ((clk_m << clk_m_offset) & clk_m_mask);
651 reg_val |= ((clk_n << clk_n_offset) & clk_n_mask);
652 if (i2c->bus_freq > STANDDARD_FREQ)
653 reg_val |= duty;
654 else
655 reg_val &= ~(duty);
656
657 writel(reg_val, i2c->base_addr + reg);
658 }
659
sunxi_i2c_set_frequency(struct sunxi_i2c * i2c)660 static int sunxi_i2c_set_frequency(struct sunxi_i2c *i2c)
661 {
662 u8 clk_m = 0, clk_n = 0, _2_pow_clk_n = 1;
663 unsigned int clk_in, clk_src, divider, clk_real;
664
665 if (i2c->twi_drv_used) {
666 clk_in = 24000000;
667 } else {
668 #if !IS_ENABLED(CONFIG_EVB_PLATFORM)
669 clk_in = 24000000;
670 #endif
671 clk_in = clk_get_rate(i2c->bus_clk);
672 if (clk_in == 0) {
673 dev_err(i2c->dev, "get clocksource clock freq failed!\n");
674 return -1;
675 }
676 }
677
678 clk_src = clk_in / 10;
679 divider = clk_src / i2c->bus_freq;
680
681 if (!divider) {
682 clk_m = 1;
683 goto i2c_set_clk;
684 }
685
686 /*
687 * search clk_n and clk_m,from large to small value so
688 * that can quickly find suitable m & n.
689 */
690 while (clk_n < 8) { /* 3bits max value is 8 */
691 /* (m+1)*2^n = divider -->m = divider/2^n -1 */
692 clk_m = (divider / _2_pow_clk_n) - 1;
693 /* clk_m = (divider >> (_2_pow_clk_n>>1))-1 */
694 while (clk_m < 16) { /* 4bits max value is 16 */
695 /* src_clk/((m+1)*2^n) */
696 clk_real = clk_src / (clk_m + 1) / _2_pow_clk_n;
697 if (clk_real <= i2c->bus_freq)
698 goto i2c_set_clk;
699 else
700 clk_m++;
701 }
702 clk_n++;
703 _2_pow_clk_n *= 2; /* mutilple by 2 */
704 }
705
706 i2c_set_clk:
707 sunxi_i2c_set_clock(i2c, clk_m, clk_n);
708
709 return 0;
710 }
711
712 /*
713 * i2c controller soft_reset can only clear flag bit inside of ip, include the
714 * state machine parameters, counters, various flags, fifo, fifo-cnt.
715 *
716 * But the internal configurations or external register configurations of ip
717 * will not be changed.
718 */
sunxi_i2c_soft_reset(struct sunxi_i2c * i2c)719 static inline void sunxi_i2c_soft_reset(struct sunxi_i2c *i2c)
720 {
721 u32 reg_val, reg, mask;
722
723 if (i2c->twi_drv_used) {
724 reg = I2C_DRV_CTRL;
725 mask = SOFT_RESET;
726 } else {
727 reg = I2C_SRST;
728 mask = I2C_SOFT_RST;
729 }
730
731 reg_val = readl(i2c->base_addr + reg);
732 reg_val |= mask;
733 writel(reg_val, i2c->base_addr + reg);
734
735 /*
736 * drv-mode soft_reset bit will not clear automatically, write 0 to unreset.
737 * The reset only takes one or two CPU clk cycle.
738 */
739 if (i2c->twi_drv_used) {
740 usleep_range(20, 25);
741
742 reg_val &= (~mask);
743 writel(reg_val, i2c->base_addr + reg);
744 }
745 }
746
747 /* iset the data byte number follow read command control */
sunxi_i2c_engine_set_efr(void __iomem * base_addr,u32 efr)748 static inline void sunxi_i2c_engine_set_efr(void __iomem *base_addr, u32 efr)
749 {
750 u32 reg_val;
751
752 reg_val = readl(base_addr + I2C_EFR);
753
754 reg_val &= ~I2C_EFR_MASK;
755 efr &= I2C_EFR_MASK;
756 reg_val |= efr;
757
758 writel(reg_val, base_addr + I2C_EFR);
759 }
760
sunxi_i2c_engine_start(struct sunxi_i2c * i2c)761 static int sunxi_i2c_engine_start(struct sunxi_i2c *i2c)
762 {
763 unsigned int timeout = 0xff;
764
765 sunxi_i2c_engine_set_start(i2c->base_addr);
766 while ((sunxi_i2c_engine_get_start(i2c->base_addr) == 1) && (--timeout))
767 ;
768 if (timeout == 0) {
769 dev_err(i2c->dev, "engine-mode: START can't sendout!\n");
770 return -EINVAL;
771 }
772
773 dev_dbg(i2c->dev, "engine-mode: start signal xfered\n");
774 return 0;
775 }
776
sunxi_i2c_engine_restart(struct sunxi_i2c * i2c)777 static int sunxi_i2c_engine_restart(struct sunxi_i2c *i2c)
778 {
779 unsigned int timeout = 0xff;
780
781 sunxi_i2c_engine_set_start(i2c->base_addr);
782
783 while ((sunxi_i2c_engine_get_start(i2c->base_addr) == 1) && (--timeout))
784 ;
785 if (timeout == 0) {
786 dev_err(i2c->dev, "engine-mode: Restart can't sendout!\n");
787 return -EINVAL;
788 }
789
790 dev_dbg(i2c->dev, "engine-mode: restart signal xfered\n");
791 return 0;
792 }
793
sunxi_i2c_engine_stop(struct sunxi_i2c * i2c)794 static int sunxi_i2c_engine_stop(struct sunxi_i2c *i2c)
795 {
796 unsigned int timeout = 0xff;
797 void __iomem *base_addr = i2c->base_addr;
798
799 sunxi_i2c_engine_set_stop(base_addr);
800 /* i2c bus xfer status will chaned after irq flag cleared */
801 sunxi_i2c_engine_clear_irq(base_addr);
802
803 /* it must delay 1 nop to check stop bit */
804 sunxi_i2c_engine_get_stop(base_addr);
805 while ((sunxi_i2c_engine_get_stop(base_addr) == 1) && (--timeout))
806 ;
807 if (timeout == 0) {
808 dev_err(i2c->dev, "engine-mode: STOP can't sendout!\n");
809 return -EINVAL;
810 }
811
812 timeout = 0xff;
813 while ((sunxi_i2c_get_xfer_sta(i2c) != I2C_STAT_IDLE) && (--timeout))
814 ;
815 if (timeout == 0) {
816 dev_err(i2c->dev, "engine-mode: bus state: 0x%0x, isn't idle\n",
817 sunxi_i2c_get_xfer_sta(i2c));
818 return -EINVAL;
819 }
820
821 dev_dbg(i2c->dev, "engine-mode: stop signal xfered");
822 return 0;
823 }
824
sunxi_i2c_scl_control_enable(struct sunxi_i2c * i2c)825 static void sunxi_i2c_scl_control_enable(struct sunxi_i2c *i2c)
826 {
827 u32 reg_val, reg_ctrl, reg;
828
829 if (i2c->twi_drv_used) {
830 reg = I2C_DRV_BUS_CTRL;
831 reg_ctrl = SCL_MOE;
832 } else {
833 reg = I2C_LCR;
834 reg_ctrl = SCL_CTL_EN;
835 }
836
837 reg_val = readl(i2c->base_addr + reg);
838
839 reg_val |= reg_ctrl;
840
841 writel(reg_val, i2c->base_addr + reg);
842 }
843
sunxi_i2c_scl_control_disable(struct sunxi_i2c * i2c)844 static void sunxi_i2c_scl_control_disable(struct sunxi_i2c *i2c)
845 {
846 u32 reg_val, reg_ctrl, reg;
847
848 if (i2c->twi_drv_used) {
849 reg = I2C_DRV_BUS_CTRL;
850 reg_ctrl = SCL_MOE;
851 } else {
852 reg = I2C_LCR;
853 reg_ctrl = SCL_CTL_EN;
854 }
855
856 reg_val = readl(i2c->base_addr + reg);
857
858 reg_val &= ~(reg_ctrl);
859
860 writel(reg_val, i2c->base_addr + reg);
861 }
862
sunxi_i2c_sda_control_enable(struct sunxi_i2c * i2c)863 static void sunxi_i2c_sda_control_enable(struct sunxi_i2c *i2c)
864 {
865 u32 reg_val, reg_ctrl, reg;
866
867 if (i2c->twi_drv_used) {
868 reg = I2C_DRV_BUS_CTRL;
869 reg_ctrl = SDA_MOE;
870 } else {
871 reg = I2C_LCR;
872 reg_ctrl = SDA_CTL_EN;
873 }
874
875 reg_val = readl(i2c->base_addr + reg);
876
877 reg_val |= reg_ctrl;
878
879 writel(reg_val, i2c->base_addr + reg);
880 }
881
sunxi_i2c_sda_control_disable(struct sunxi_i2c * i2c)882 static void sunxi_i2c_sda_control_disable(struct sunxi_i2c *i2c)
883 {
884 u32 reg_val, reg_ctrl, reg;
885
886 if (i2c->twi_drv_used) {
887 reg = I2C_DRV_BUS_CTRL;
888 reg_ctrl = SDA_MOE;
889 } else {
890 reg = I2C_LCR;
891 reg_ctrl = SDA_CTL_EN;
892 }
893
894 reg_val = readl(i2c->base_addr + reg);
895
896 reg_val &= ~(reg_ctrl);
897
898 writel(reg_val, i2c->base_addr + reg);
899 }
900
sunxi_i2c_get_scl(struct i2c_adapter * adap)901 static int sunxi_i2c_get_scl(struct i2c_adapter *adap)
902 {
903 struct sunxi_i2c *i2c = (struct sunxi_i2c *)adap->algo_data;
904
905 if (i2c->twi_drv_used)
906 return !!(readl(i2c->base_addr + I2C_DRV_BUS_CTRL) & SCL_STA);
907 else
908 return !!(readl(i2c->base_addr + I2C_LCR) & SCL_STATE);
909 }
910
sunxi_i2c_set_scl(struct i2c_adapter * adap,int val)911 static void sunxi_i2c_set_scl(struct i2c_adapter *adap, int val)
912 {
913 struct sunxi_i2c *i2c = (struct sunxi_i2c *)adap->algo_data;
914 u32 reg_val, status, reg;
915
916 sunxi_i2c_scl_control_enable(i2c);
917
918 if (i2c->twi_drv_used) {
919 reg = I2C_DRV_BUS_CTRL;
920 status = SCL_MOV;
921 } else {
922 reg = I2C_LCR;
923 status = SCL_CTL;
924 }
925
926 reg_val = readl(i2c->base_addr + reg);
927 dev_dbg(i2c->dev, "set scl, val:%x, val:%d\n", reg_val, val);
928
929 if (val)
930 reg_val |= status;
931 else
932 reg_val &= ~(status);
933
934 writel(reg_val, i2c->base_addr + reg);
935
936 sunxi_i2c_scl_control_disable(i2c);
937 }
938
sunxi_i2c_get_sda(struct i2c_adapter * adap)939 static int sunxi_i2c_get_sda(struct i2c_adapter *adap)
940 {
941 struct sunxi_i2c *i2c = (struct sunxi_i2c *)adap->algo_data;
942
943 if (i2c->twi_drv_used)
944 return !!(readl(i2c->base_addr + I2C_DRV_BUS_CTRL) & SDA_STA);
945 else
946 return !!(readl(i2c->base_addr + I2C_LCR) & SDA_STATE);
947 }
948
sunxi_i2c_set_sda(struct i2c_adapter * adap,int val)949 static void sunxi_i2c_set_sda(struct i2c_adapter *adap, int val)
950 {
951 struct sunxi_i2c *i2c = (struct sunxi_i2c *)adap->algo_data;
952 u32 reg_val, status, reg;
953
954 sunxi_i2c_sda_control_enable(i2c);
955
956 if (i2c->twi_drv_used) {
957 reg = I2C_DRV_BUS_CTRL;
958 status = SDA_MOV;
959 } else {
960 reg = I2C_LCR;
961 status = SDA_CTL;
962 }
963
964 reg_val = readl(i2c->base_addr + reg);
965 dev_dbg(i2c->dev, "set scl, val:%x, val:%d\n", reg_val, val);
966
967 if (val)
968 reg_val |= status;
969 else
970 reg_val &= ~(status);
971
972 writel(reg_val, i2c->base_addr + reg);
973
974 sunxi_i2c_sda_control_disable(i2c);
975 }
976
sunxi_i2c_get_bus_free(struct i2c_adapter * adap)977 static int sunxi_i2c_get_bus_free(struct i2c_adapter *adap)
978 {
979 return sunxi_i2c_get_sda(adap);
980 }
981
982 /* get the irq enabled */
sunxi_i2c_drv_get_irq(void __iomem * base_addr)983 static unsigned int sunxi_i2c_drv_get_irq(void __iomem *base_addr)
984 {
985 unsigned int sta, irq;
986
987 irq = (readl(base_addr + I2C_DRV_INT_CTRL) & I2C_DRV_INT_EN_MASK) >> 16;
988 sta = readl(base_addr + I2C_DRV_INT_CTRL) & I2C_DRV_INT_STA_MASK;
989
990 return irq & sta;
991 }
992
sunxi_i2c_drv_clear_irq(void __iomem * base_addr,u32 bitmap)993 static void sunxi_i2c_drv_clear_irq(void __iomem *base_addr, u32 bitmap)
994 {
995 unsigned int reg_val = readl(base_addr + I2C_DRV_INT_CTRL);
996
997 reg_val |= (bitmap & I2C_DRV_INT_STA_MASK);
998
999 writel(reg_val, base_addr + I2C_DRV_INT_CTRL);
1000 }
1001
sunxi_i2c_drv_enable_irq(void __iomem * base_addr,u32 bitmap)1002 static void sunxi_i2c_drv_enable_irq(void __iomem *base_addr, u32 bitmap)
1003 {
1004 u32 reg_val = readl(base_addr + I2C_DRV_INT_CTRL);
1005
1006 reg_val |= bitmap;
1007 reg_val &= ~I2C_DRV_INT_STA_MASK;
1008
1009 writel(reg_val, base_addr + I2C_DRV_INT_CTRL);
1010 }
1011
sunxi_i2c_drv_disable_irq(void __iomem * base_addr,u32 bitmap)1012 static void sunxi_i2c_drv_disable_irq(void __iomem *base_addr, u32 bitmap)
1013 {
1014 u32 reg_val = readl(base_addr + I2C_DRV_INT_CTRL);
1015
1016 reg_val &= ~bitmap;
1017 reg_val &= ~I2C_DRV_INT_STA_MASK;
1018
1019 writel(reg_val, base_addr + I2C_DRV_INT_CTRL);
1020 }
1021
sunxi_i2c_drv_enable_dma_irq(void __iomem * base_addr,u32 bitmap)1022 static void sunxi_i2c_drv_enable_dma_irq(void __iomem *base_addr, u32 bitmap)
1023 {
1024 u32 reg_val = readl(base_addr + I2C_DRV_DMA_CFG);
1025
1026 bitmap &= I2C_DRQEN_MASK;
1027 reg_val |= bitmap;
1028
1029 writel(reg_val, base_addr + I2C_DRV_DMA_CFG);
1030 }
1031
sunxi_i2c_drv_disable_dma_irq(void __iomem * base_addr,u32 bitmap)1032 static void sunxi_i2c_drv_disable_dma_irq(void __iomem *base_addr, u32 bitmap)
1033 {
1034 u32 reg_val = readl(base_addr + I2C_DRV_DMA_CFG);
1035
1036 bitmap &= I2C_DRQEN_MASK;
1037 reg_val &= ~bitmap;
1038
1039 writel(reg_val, base_addr + I2C_DRV_DMA_CFG);
1040 }
1041
1042 /*
1043 * The engine-mode bus_en bit is automatically set to 1 after the drv-mode START_TRAN bit set to 1.
1044 * (because drv-mode rely on engine-mode)
1045 */
sunxi_i2c_drv_start_xfer(struct sunxi_i2c * i2c)1046 static inline void sunxi_i2c_drv_start_xfer(struct sunxi_i2c *i2c)
1047 {
1048 u32 reg_val = readl(i2c->base_addr + I2C_DRV_CTRL);
1049
1050 reg_val |= START_TRAN;
1051
1052 writel(reg_val, i2c->base_addr + I2C_DRV_CTRL);
1053
1054 dev_dbg(i2c->dev, "drv-mode: start signal xfered\n");
1055 }
1056
sunxi_i2c_drv_set_tx_trig(void __iomem * base_addr,u32 trig)1057 static void sunxi_i2c_drv_set_tx_trig(void __iomem *base_addr, u32 trig)
1058 {
1059 u32 reg_val = readl(base_addr + I2C_DRV_DMA_CFG);
1060
1061 reg_val &= ~TX_TRIG;
1062 reg_val |= (trig << TX_TRIG_OFFSET);
1063
1064 writel(reg_val, base_addr + I2C_DRV_DMA_CFG);
1065 }
1066
1067 /* When one of the following conditions is met:
1068 * 1. The number of data (in bytes) in RECV_FIFO reaches RX_TRIG;
1069 * 2. Packet read done and RECV_FIFO not empty.
1070 * If RX_REQ is enabled, the rx-pending-bit will be set to 1 and the interrupt will be triggered;
1071 * If RX_REQ is disabled, the rx-pending-bit will be set to 1 but the interrupt will NOT be triggered.
1072 */
sunxi_i2c_drv_set_rx_trig(void __iomem * base_addr,u32 trig)1073 static void sunxi_i2c_drv_set_rx_trig(void __iomem *base_addr, u32 trig)
1074 {
1075 u32 reg_val;
1076
1077 reg_val = readl(base_addr + I2C_DRV_DMA_CFG);
1078
1079 reg_val &= ~RX_TRIG;
1080 reg_val |= (trig << RX_TRIG_OFFSET);
1081
1082 writel(reg_val, base_addr + I2C_DRV_DMA_CFG);
1083 }
1084
1085
1086 /* bytes be send as slave device reg address */
sunxi_i2c_drv_set_addr_byte(void __iomem * base_addr,u32 len)1087 static void sunxi_i2c_drv_set_addr_byte(void __iomem *base_addr, u32 len)
1088 {
1089 u32 reg_val = readl(base_addr + I2C_DRV_FMT);
1090
1091 reg_val &= ~ADDR_BYTE;
1092 reg_val |= (len << ADDR_BYTE_OFFSET);
1093
1094 writel(reg_val, base_addr + I2C_DRV_FMT);
1095 }
1096
1097 /* bytes be send/received as data */
sunxi_i2c_drv_set_data_byte(void __iomem * base_addr,u32 len)1098 static void sunxi_i2c_drv_set_data_byte(void __iomem *base_addr, u32 len)
1099 {
1100 u32 val = readl(base_addr + I2C_DRV_FMT);
1101
1102 val &= ~DATA_BYTE;
1103 val |= (len << DATA_BYTE_OFFSET);
1104
1105 writel(val, base_addr + I2C_DRV_FMT);
1106 }
1107
1108 /* interval between each packet in 32*Fscl cycles */
1109 /*
1110 static void sunxi_i2c_drv_set_packet_interval(void __iomem *base_addr, u32 val)
1111 {
1112 u32 reg_val = readl(base_addr + I2C_DRV_CFG);
1113
1114 reg_val &= ~PACKET_INTERVAL;
1115 reg_val |= (val << PACKET_INTERVAL_OFFSET);
1116
1117 writel(reg_val, base_addr + I2C_DRV_CFG);
1118 }
1119 */
1120
1121 /* FIFO data be transmitted as PACKET_CNT packets in current format */
sunxi_i2c_drv_set_packet_cnt(void __iomem * base_addr,u32 val)1122 static void sunxi_i2c_drv_set_packet_cnt(void __iomem *base_addr, u32 val)
1123 {
1124 u32 reg_val = readl(base_addr + I2C_DRV_CFG);
1125
1126 reg_val &= ~PACKET_CNT;
1127 reg_val |= (val << PACKET_CNT_OFFSET);
1128
1129 writel(reg_val, base_addr + I2C_DRV_CFG);
1130 }
1131
1132 /* do not send slave_id +W */
sunxi_i2c_drv_enable_read_mode(void __iomem * base_addr)1133 static void sunxi_i2c_drv_enable_read_mode(void __iomem *base_addr)
1134 {
1135 u32 reg_val = readl(base_addr + I2C_DRV_CTRL);
1136
1137 reg_val |= READ_TRAN_MODE;
1138
1139 writel(reg_val, base_addr + I2C_DRV_CTRL);
1140 }
1141
1142 /* send slave_id + W */
sunxi_i2c_drv_disable_read_mode(void __iomem * base_addr)1143 static void sunxi_i2c_drv_disable_read_mode(void __iomem *base_addr)
1144 {
1145 u32 reg_val = readl(base_addr + I2C_DRV_CTRL);
1146
1147 reg_val &= ~READ_TRAN_MODE;
1148
1149 writel(reg_val, base_addr + I2C_DRV_CTRL);
1150 }
1151
1152 static void
sunxi_i2c_drv_set_slave_addr(struct sunxi_i2c * i2c,struct i2c_msg * msgs)1153 sunxi_i2c_drv_set_slave_addr(struct sunxi_i2c *i2c, struct i2c_msg *msgs)
1154 {
1155 unsigned int reg_val = 0;
1156
1157 /* read, default value is write */
1158 if (msgs->flags & I2C_M_RD)
1159 reg_val |= CMD;
1160 else
1161 reg_val &= ~CMD;
1162
1163 if (msgs->flags & I2C_M_TEN) {
1164 /* SLV_ID | CMD | SLV_ID_X */
1165 reg_val |= ((0x78 | ((msgs->addr >> 8) & 0x03)) << 9);
1166 reg_val |= (msgs->addr & 0xff);
1167 dev_dbg(i2c->dev, "drv-mode: first 10bit(0x%x) xfered\n", msgs->addr);
1168 } else {
1169 reg_val |= ((msgs->addr & 0x7f) << 9);
1170 dev_dbg(i2c->dev, "drv-mode: 7bits(0x%x) + r/w xfered", msgs->addr);
1171 }
1172
1173 writel(reg_val, i2c->base_addr + I2C_DRV_SLV);
1174 }
1175
1176 /* the number of data in SEND_FIFO */
sunxi_i2c_drv_get_txfifo_cnt(void __iomem * base_addr)1177 static int sunxi_i2c_drv_get_txfifo_cnt(void __iomem *base_addr)
1178 {
1179 u32 reg_val = readl(base_addr + I2C_DRV_FIFO_CON) & SEND_FIFO_CONTENT;
1180
1181 return reg_val >> SEND_FIFO_CONTENT_OFFSET;
1182 }
1183
1184 /* the number of data in RECV_FIFO */
sunxi_i2c_drv_get_rxfifo_cnt(void __iomem * base_addr)1185 static int sunxi_i2c_drv_get_rxfifo_cnt(void __iomem *base_addr)
1186 {
1187 u32 reg_val = readl(base_addr + I2C_DRV_FIFO_CON)& RECV_FIFO_CONTENT;
1188
1189 return reg_val >> RECV_FIFO_CONTENT_OFFSET;
1190 }
1191
sunxi_i2c_drv_clear_txfifo(void __iomem * base_addr)1192 static void sunxi_i2c_drv_clear_txfifo(void __iomem *base_addr)
1193 {
1194 u32 reg_val = readl(base_addr + I2C_DRV_FIFO_CON);
1195
1196 reg_val |= SEND_FIFO_CLEAR;
1197
1198 writel(reg_val, base_addr + I2C_DRV_FIFO_CON);
1199 }
1200
sunxi_i2c_drv_clear_rxfifo(void __iomem * base_addr)1201 static void sunxi_i2c_drv_clear_rxfifo(void __iomem *base_addr)
1202 {
1203 u32 reg_val = readl(base_addr + I2C_DRV_FIFO_CON);
1204
1205 reg_val |= RECV_FIFO_CLEAR;
1206
1207 writel(reg_val, base_addr + I2C_DRV_FIFO_CON);
1208 }
1209
sunxi_i2c_drv_send_msg(struct sunxi_i2c * i2c,struct i2c_msg * msg)1210 static int sunxi_i2c_drv_send_msg(struct sunxi_i2c *i2c, struct i2c_msg *msg)
1211 {
1212 u16 i;
1213 u8 time = 0xff;
1214
1215 dev_dbg(i2c->dev, "drv-mode: tx msg len is %d\n", msg->len);
1216
1217 for (i = 0; i < msg->len; i++) {
1218 while ((sunxi_i2c_drv_get_txfifo_cnt(i2c->base_addr) >= MAX_FIFO)
1219 && time--)
1220 ;
1221 if (time) {
1222 writeb(msg->buf[i], i2c->base_addr + I2C_DRV_SEND_FIFO_ACC);
1223 dev_dbg(i2c->dev, "drv-mode: write Byte[%u]=0x%x,tx fifo len=%d\n",
1224 i, msg->buf[i], sunxi_i2c_drv_get_txfifo_cnt(i2c->base_addr));
1225 } else {
1226 dev_err(i2c->dev, "drv-mode: SEND FIFO overflow, timeout\n");
1227 return -EINVAL;
1228 }
1229 }
1230
1231 return 0;
1232 }
1233
sunxi_i2c_drv_recv_msg(struct sunxi_i2c * i2c,struct i2c_msg * msg)1234 static u32 sunxi_i2c_drv_recv_msg(struct sunxi_i2c *i2c, struct i2c_msg *msg)
1235 {
1236 u16 i;
1237 u8 time = 0xff;
1238
1239 dev_dbg(i2c->dev, "drv-mode: rx msg len is %d\n", msg->len);
1240
1241 for (i = 0; i < msg->len; i++) {
1242 while (!sunxi_i2c_drv_get_rxfifo_cnt(i2c->base_addr) && time--)
1243 ;
1244
1245 if (time) {
1246 msg->buf[i] = readb(i2c->base_addr + I2C_DRV_RECV_FIFO_ACC);
1247 dev_dbg(i2c->dev, "drv-mode: readb: Byte[%d] = 0x%x\n",
1248 i, msg->buf[i]);
1249 } else {
1250 dev_err(i2c->dev, "drv-mode: rerceive fifo empty. timeout\n");
1251 return -EINVAL;
1252 }
1253 }
1254
1255 return 0;
1256 }
1257
1258 /* Functions for DMA support */
sunxi_i2c_dma_request(struct sunxi_i2c * i2c,dma_addr_t phy_addr)1259 static int sunxi_i2c_dma_request(struct sunxi_i2c *i2c, dma_addr_t phy_addr)
1260 {
1261 struct sunxi_i2c_dma *dma_tx, *dma_rx;
1262 struct dma_slave_config dma_sconfig;
1263 int err;
1264
1265 dma_tx = devm_kzalloc(i2c->dev, sizeof(*dma_tx), GFP_KERNEL);
1266 dma_rx = devm_kzalloc(i2c->dev, sizeof(*dma_rx), GFP_KERNEL);
1267 if (IS_ERR_OR_NULL(dma_tx) || IS_ERR_OR_NULL(dma_rx))
1268 return -EINVAL;
1269
1270 dma_tx->chan = dma_request_chan(i2c->dev, "tx");
1271 if (IS_ERR(dma_tx->chan)) {
1272 dev_err(i2c->dev, "can't request DMA tx channel\n");
1273 err = PTR_ERR(dma_tx->chan);
1274 goto err0;
1275 }
1276
1277 dma_sconfig.dst_addr = phy_addr + I2C_DRV_SEND_FIFO_ACC;
1278 dma_sconfig.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
1279 dma_sconfig.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
1280 dma_sconfig.src_maxburst = 16;
1281 dma_sconfig.dst_maxburst = 16;
1282 dma_sconfig.direction = DMA_MEM_TO_DEV;
1283 err = dmaengine_slave_config(dma_tx->chan, &dma_sconfig);
1284 if (err < 0) {
1285 dev_err(i2c->dev, "can't configure tx channel\n");
1286 goto err1;
1287 }
1288 i2c->dma_tx = dma_tx;
1289
1290 dma_rx->chan = dma_request_chan(i2c->dev, "rx");
1291 if (IS_ERR(dma_rx->chan)) {
1292 dev_err(i2c->dev, "can't request DMA rx channel\n");
1293 goto err1;
1294 }
1295 dma_sconfig.src_addr = phy_addr + I2C_DRV_RECV_FIFO_ACC;
1296 dma_sconfig.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
1297 dma_sconfig.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
1298 dma_sconfig.src_maxburst = 16;
1299 dma_sconfig.dst_maxburst = 16;
1300 dma_sconfig.direction = DMA_DEV_TO_MEM;
1301 err = dmaengine_slave_config(dma_rx->chan, &dma_sconfig);
1302 if (err < 0) {
1303 dev_err(i2c->dev, "can't configure rx channel\n");
1304 goto err2;
1305 }
1306 i2c->dma_rx = dma_rx;
1307
1308 dev_dbg(i2c->dev, "using %s (tx) and %s (rx) for DMA transfers\n",
1309 dma_chan_name(i2c->dma_tx->chan), dma_chan_name(i2c->dma_rx->chan));
1310
1311 return 0;
1312
1313 err2:
1314 dma_release_channel(i2c->dma_rx->chan);
1315
1316 err1:
1317 dma_release_channel(i2c->dma_tx->chan);
1318
1319 err0:
1320 return err;
1321 }
1322
sunxi_i2c_dma_release(struct sunxi_i2c * i2c)1323 static void sunxi_i2c_dma_release(struct sunxi_i2c *i2c)
1324 {
1325 if (i2c->dma_tx) {
1326 i2c->dma_tx->dma_buf = 0;
1327 i2c->dma_tx->dma_len = 0;
1328 dma_release_channel(i2c->dma_tx->chan);
1329 i2c->dma_tx->chan = NULL;
1330 }
1331
1332 if (i2c->dma_rx) {
1333 i2c->dma_rx->dma_buf = 0;
1334 i2c->dma_rx->dma_len = 0;
1335 dma_release_channel(i2c->dma_rx->chan);
1336 i2c->dma_rx->chan = NULL;
1337 }
1338 }
1339
sunxi_i2c_dma_callback(void * arg)1340 static void sunxi_i2c_dma_callback(void *arg)
1341 {
1342 struct sunxi_i2c *i2c = (struct sunxi_i2c *)arg;
1343
1344 if (i2c->dma_using == i2c->dma_tx)
1345 dev_err(i2c->dev, "drv-mode: dma write data end\n");
1346 else if (i2c->dma_using == i2c->dma_rx)
1347 dev_err(i2c->dev, "drv-mode: dma read data end\n");
1348 }
1349
1350 /* make preparetions for dma transfer
1351 * then wait tx fifo level trig unitl dma callback
1352 */
sunxi_i2c_drv_dma_xfer_init(struct sunxi_i2c * i2c,bool read)1353 static int sunxi_i2c_drv_dma_xfer_init(struct sunxi_i2c *i2c, bool read)
1354 {
1355 struct sunxi_i2c_dma *dma;
1356 struct device *chan_dev;
1357 struct dma_async_tx_descriptor *dma_desc;
1358 int ret;
1359
1360 if (read) {
1361 i2c->dma_using = i2c->dma_rx;
1362 i2c->dma_using->dma_transfer_dir = DMA_DEV_TO_MEM;
1363 i2c->dma_using->dma_data_dir = DMA_FROM_DEVICE;
1364 i2c->dma_using->dma_len = i2c->msg->len;
1365 } else {
1366 i2c->dma_using = i2c->dma_tx;
1367 i2c->dma_using->dma_transfer_dir = DMA_MEM_TO_DEV;
1368 i2c->dma_using->dma_data_dir = DMA_TO_DEVICE;
1369 i2c->dma_using->dma_len = i2c->msg->len;
1370 }
1371 dma = i2c->dma_using;
1372 chan_dev = dma->chan->device->dev;
1373
1374 /* kzalloc buf to store the dma xfered data */
1375 i2c->dma_buf = i2c_get_dma_safe_msg_buf(i2c->msg, DMA_THRESHOLD);
1376 dma->dma_buf = dma_map_single(chan_dev, i2c->dma_buf,
1377 dma->dma_len, dma->dma_data_dir);
1378 if (dma_mapping_error(chan_dev, dma->dma_buf)) {
1379 dev_err(i2c->dev, "DMA mapping failed\n");
1380 ret = -EINVAL;
1381 goto err0;
1382 }
1383 dma_desc = dmaengine_prep_slave_single(dma->chan, dma->dma_buf,
1384 dma->dma_len, dma->dma_transfer_dir,
1385 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
1386 if (!dma_desc) {
1387 dev_err(i2c->dev, "Not able to get desc for DMA xfer\n");
1388 ret = -EINVAL;
1389 goto err0;
1390 }
1391 dma_desc->callback = sunxi_i2c_dma_callback;
1392 dma_desc->callback_param = i2c;
1393 if (dma_submit_error(dmaengine_submit(dma_desc))) {
1394 dev_err(i2c->dev, "DMA submit failed\n");
1395 ret = -EINVAL;
1396 goto err0;
1397 }
1398
1399 dma_async_issue_pending(dma->chan);
1400 dev_dbg(i2c->dev, "dma issue pending\n");
1401
1402 return 0;
1403
1404 err0:
1405 i2c_put_dma_safe_msg_buf(i2c->dma_buf, i2c->msg, true);
1406 return ret;
1407 }
1408
sunxi_i2c_drv_dma_xfer_deinit(struct sunxi_i2c * i2c)1409 static int sunxi_i2c_drv_dma_xfer_deinit(struct sunxi_i2c *i2c)
1410 {
1411 struct sunxi_i2c_dma *dma = i2c->dma_using;
1412 struct device *chan_dev = dma->chan->device->dev;
1413
1414 dma_unmap_single(chan_dev, dma->dma_buf, dma->dma_len, dma->dma_data_dir);
1415 /* free the buf kzalloc, and copy the data to i2c->msg */
1416 i2c_put_dma_safe_msg_buf(i2c->dma_buf, i2c->msg, true);
1417
1418 return 0;
1419 }
1420
1421
1422 /* Description:
1423 * 7bits addr: 7-1bits addr+0 bit r/w
1424 * 10bits addr: 1111_11xx_xxxx_xxxx-->1111_0xx_rw,xxxx_xxxx
1425 * send the 7 bits addr,or the first part of 10 bits addr
1426 **/
sunxi_i2c_engine_addr_byte(struct sunxi_i2c * i2c)1427 static void sunxi_i2c_engine_addr_byte(struct sunxi_i2c *i2c)
1428 {
1429 unsigned char addr = 0;
1430 unsigned char tmp = 0;
1431
1432 if (i2c->msg[i2c->msg_idx].flags & I2C_M_TEN) {
1433 /* 0111_10xx,ten bits address--9:8bits */
1434 tmp = 0x78 | (((i2c->msg[i2c->msg_idx].addr)>>8) & 0x03);
1435 addr = tmp << 1; /*1111_0xx0*/
1436 /* how about the second part of ten bits addr? */
1437 /* Answer: deal at twi_core_process() */
1438 } else {
1439 /* 7-1bits addr, xxxx_xxx0 */
1440 addr = (i2c->msg[i2c->msg_idx].addr & 0x7f) << 1;
1441 }
1442
1443 /* read, default value is write */
1444 if (i2c->msg[i2c->msg_idx].flags & I2C_M_RD)
1445 addr |= 1;
1446
1447 if (i2c->msg[i2c->msg_idx].flags & I2C_M_TEN)
1448 dev_dbg(i2c->dev, "first part of 10bits = 0x%x\n", addr);
1449 else
1450 dev_dbg(i2c->dev, "engine-mode: 7bits+r/w = 0x%x xfered\n", addr);
1451
1452 /* send 7bits+r/w or the first part of 10bits */
1453 sunxi_i2c_engine_put_byte(i2c, &addr);
1454 }
1455
sunxi_i2c_bus_barrier(struct i2c_adapter * adap)1456 static int sunxi_i2c_bus_barrier(struct i2c_adapter *adap)
1457 {
1458 int i, ret;
1459 struct sunxi_i2c *i2c = (struct sunxi_i2c *)adap->algo_data;
1460
1461 for (i = 0; i < LOOP_TIMEOUT; i++) {
1462 if (sunxi_i2c_get_bus_free(adap))
1463 return 0;
1464
1465 udelay(1);
1466 }
1467
1468 ret = i2c_recover_bus(adap);
1469 sunxi_i2c_soft_reset(i2c);
1470
1471 return ret;
1472 }
1473
1474 /* return 0 on success, otherwise return the negative error num */
sunxi_i2c_drv_complete(struct sunxi_i2c * i2c)1475 static int sunxi_i2c_drv_complete(struct sunxi_i2c *i2c)
1476 {
1477 unsigned long flags, timeout;
1478 int ret = 0;
1479
1480 timeout = wait_event_timeout(i2c->wait, i2c->result, i2c->adap.timeout);
1481 if (timeout == 0) {
1482 dev_err(i2c->dev, "drv-mode: xfer timeout (dev addr:0x%x)\n",
1483 i2c->msg->addr);
1484 sunxi_i2c_dump_reg(i2c, 0x200, 0x20);
1485 ret = -ETIME;
1486 } else {
1487 if (i2c->result == RESULT_ERR) {
1488 dev_err(i2c->dev, "drv-mode: xfer failed (dev addr:0x%x)\n",
1489 i2c->msg->addr);
1490 sunxi_i2c_dump_reg(i2c, 0x200, 0x20);
1491 ret = -EINVAL;
1492 } else if (i2c->result == RESULT_COMPLETE) {
1493 dev_dbg(i2c->dev, "drv-mode: xfer complete\n");
1494 } else {
1495 dev_err(i2c->dev, "drv-mode: result err\n");
1496 ret = -EINVAL;
1497 }
1498 }
1499
1500 spin_lock_irqsave(&i2c->lock, flags);
1501 i2c->result = RESULT_IDLE;
1502 spin_unlock_irqrestore(&i2c->lock, flags);
1503
1504 return ret;
1505 }
1506
1507 /* return the xfer msgs num or the negative error num */
sunxi_i2c_engine_complete(struct sunxi_i2c * i2c)1508 static int sunxi_i2c_engine_complete(struct sunxi_i2c *i2c)
1509 {
1510 unsigned long flags, timeout;
1511 int ret;
1512
1513 timeout = wait_event_timeout(i2c->wait, i2c->result, i2c->adap.timeout);
1514 if (timeout == 0) {
1515 dev_err(i2c->dev, "engine-mode: xfer timeout(dev addr:0x%x)\n",
1516 i2c->msg->addr);
1517 sunxi_i2c_dump_reg(i2c, 0x00, 0x20);
1518 ret = -ETIME;
1519 } else {
1520 if (i2c->result == RESULT_ERR) {
1521 dev_err(i2c->dev, "engine-mode: xfer failed(dev addr:0x%x)\n",
1522 i2c->msg->addr);
1523 sunxi_i2c_dump_reg(i2c, 0x00, 0x20);
1524 ret = -EINVAL;
1525 } else if (i2c->result == RESULT_COMPLETE) {
1526 if (i2c->msg_idx != i2c->msg_num) {
1527 dev_err(i2c->dev, "engine-mode: xfer incomplete(dev addr:0x%x\n",
1528 i2c->msg->addr);
1529 ret = -EINVAL;
1530 } else {
1531 dev_dbg(i2c->dev, "engine-mode: xfer complete\n");
1532 ret = i2c->msg_idx;
1533 }
1534 } else {
1535 dev_err(i2c->dev, "engine-mode: result err\n");
1536 ret = -EINVAL;
1537 }
1538 }
1539
1540 spin_lock_irqsave(&i2c->lock, flags);
1541 i2c->status = I2C_XFER_IDLE;
1542 i2c->result = RESULT_IDLE;
1543 spin_unlock_irqrestore(&i2c->lock, flags);
1544
1545 return ret;
1546 }
1547
sunxi_i2c_drv_core_process(struct sunxi_i2c * i2c)1548 static int sunxi_i2c_drv_core_process(struct sunxi_i2c *i2c)
1549 {
1550 void __iomem *base_addr = i2c->base_addr;
1551 unsigned long flags;
1552 unsigned int irq, err_sta;
1553
1554 spin_lock_irqsave(&i2c->lock, flags);
1555 irq = sunxi_i2c_drv_get_irq(base_addr);
1556 dev_dbg(i2c->dev, "drv-mode: the enabled irq 0x%x coming\n", irq);
1557 sunxi_i2c_drv_clear_irq(i2c->base_addr, irq);
1558
1559 if (irq & TRAN_COM_PD) {
1560 dev_dbg(i2c->dev, "drv-mode: packet transmission completed\n");
1561 sunxi_i2c_drv_disable_irq(i2c->base_addr, I2C_DRV_INT_EN_MASK);
1562 i2c->result = RESULT_COMPLETE;
1563
1564 /* cpu read tiggered by the interrupt tiggered
1565 * dma read tiggered by the fifo level(dma_callback when success)
1566 * */
1567 if ((irq & RX_REQ_PD) && (i2c->msg->len < DMA_THRESHOLD))
1568 if (sunxi_i2c_drv_recv_msg(i2c, i2c->msg))
1569 i2c->result = RESULT_ERR;
1570 wake_up(&i2c->wait);
1571 } else if (irq & TRAN_ERR_PD) {
1572 sunxi_i2c_drv_disable_irq(i2c->base_addr, I2C_DRV_INT_EN_MASK);
1573 err_sta = sunxi_i2c_get_xfer_sta(i2c);
1574 switch (err_sta) {
1575 case 0x00:
1576 dev_err(i2c->dev, "drv-mode: bus error\n");
1577 break;
1578 case 0x01:
1579 dev_err(i2c->dev, "drv-mode: Timeout when sending 9th SCL clk\n");
1580 break;
1581 case 0x20:
1582 dev_err(i2c->dev, "drv-mode: Address + Write bit transmitted,"
1583 "ACK not received\n");
1584 break;
1585 case 0x30:
1586 dev_err(i2c->dev, "drv-mode: Data byte transmitted in master mode,"
1587 "ACK not received\n");
1588 break;
1589 case 0x38:
1590 dev_err(i2c->dev, "drv-mode: Arbitration lost in address, or data byte\n");
1591 break;
1592 case 0x48:
1593 dev_err(i2c->dev, "drv-mode: Address + Read bit transmitted,"
1594 "ACK not received\\n");
1595 break;
1596 case 0x58:
1597 dev_err(i2c->dev, "drv-mode: Data byte received in master mode,"
1598 "ACK not received\n");
1599 break;
1600 default:
1601 dev_err(i2c->dev, "drv-mode: unknown error\n");
1602 break;
1603 }
1604
1605 i2c->msg_idx = err_sta;
1606 i2c->result = RESULT_ERR;
1607 wake_up(&i2c->wait);
1608 spin_unlock_irqrestore(&i2c->lock, flags);
1609 return -err_sta;
1610 }
1611
1612 spin_unlock_irqrestore(&i2c->lock, flags);
1613 return 0;
1614 }
1615
1616
sunxi_i2c_engine_core_process(struct sunxi_i2c * i2c)1617 static int sunxi_i2c_engine_core_process(struct sunxi_i2c *i2c)
1618 {
1619 unsigned long flags;
1620 unsigned char state;
1621 unsigned char tmp;
1622 void __iomem *base_addr = i2c->base_addr;
1623
1624 sunxi_i2c_engine_disable_irq(base_addr);
1625 state = sunxi_i2c_get_xfer_sta(i2c);
1626 dev_dbg(i2c->dev, "engine-mode: [slave address:(0x%x),irq state:(0x%x)]\n",
1627 i2c->msg->addr, state);
1628
1629 spin_lock_irqsave(&i2c->lock, flags);
1630 switch (state) {
1631 case 0xf8: /* On reset or stop the bus is idle, use only at poll method */
1632 goto out_break;
1633
1634 case 0x08: /* A START condition has been transmitted */
1635 case 0x10: /* A repeated start condition has been transmitted */
1636 sunxi_i2c_engine_addr_byte(i2c);/* send slave address */
1637 break; /* break and goto to normal function, */
1638
1639 case 0x18: /* slave_addr + write has been transmitted; ACK received */
1640 /* send second part of 10 bits addr, the remaining 8 bits of address */
1641 if (i2c->msg[i2c->msg_idx].flags & I2C_M_TEN) {
1642 tmp = i2c->msg[i2c->msg_idx].addr & 0xff;
1643 sunxi_i2c_engine_put_byte(i2c, &tmp);
1644 break;
1645 }
1646 /* for 7 bit addr, then directly send data byte--case 0xd0: */
1647 /* fall through */
1648
1649 case 0xd0: /* SLA+W has transmitted,ACK received! */
1650 case 0x28: /* then continue send data or current xfer end */
1651 /* send register address and the write data */
1652 if (i2c->buf_idx < i2c->msg[i2c->msg_idx].len) {
1653 sunxi_i2c_engine_put_byte(i2c,
1654 &(i2c->msg[i2c->msg_idx].buf[i2c->buf_idx]));
1655 i2c->buf_idx++;
1656 } else { /* the other msg */
1657 i2c->msg_idx++;
1658 i2c->buf_idx = 0;
1659
1660 /* all the write msgs xfer success, then wakeup */
1661 if (i2c->msg_idx == i2c->msg_num) {
1662 goto out_success;
1663 } else if (i2c->msg_idx < i2c->msg_num) {
1664 /* for restart pattern, read spec, two msgs */
1665 if (sunxi_i2c_engine_restart(i2c)) {
1666 dev_err(i2c->dev, "when the %d msg xfering, start failed",
1667 i2c->msg_idx);
1668 goto out_failed;
1669 }
1670 } else {
1671 goto out_failed;
1672 }
1673 }
1674 break;
1675
1676 /* SLA+R has been transmitted; ACK has been received, is ready to receive
1677 * with Restart, needn't to send second part of 10 bits addr
1678 */
1679 case 0x40:
1680 /* refer-"I2C-SPEC v2.1" */
1681 /* enable A_ACK need it(receive data len) more than 1. */
1682 if (i2c->msg[i2c->msg_idx].len > 1)
1683 sunxi_i2c_engine_enable_ack(base_addr);/* then jump to case 0x50 */
1684 break;
1685
1686 case 0x50: /* Data bytes has been received; ACK has been transmitted */
1687 /* receive first data byte */
1688 if (i2c->buf_idx < i2c->msg[i2c->msg_idx].len) {
1689 /* more than 2 bytes, the last byte need not to send ACK */
1690 if ((i2c->buf_idx + 2) == i2c->msg[i2c->msg_idx].len)
1691 sunxi_i2c_engine_disable_ack(base_addr);
1692
1693 /* get data then clear flag,then next data coming */
1694 sunxi_i2c_engine_get_byte(base_addr,
1695 &i2c->msg[i2c->msg_idx].buf[i2c->buf_idx]);
1696 i2c->buf_idx++;
1697 break;
1698 } else { /* err process, the last byte should be @case 0x58 */
1699 goto out_failed;
1700 }
1701
1702 case 0x58: /* Data byte has been received; NOT ACK has been transmitted */
1703 /* received the last byte */
1704 if (i2c->buf_idx == (i2c->msg[i2c->msg_idx].len - 1)) {
1705 sunxi_i2c_engine_get_byte(base_addr,
1706 &i2c->msg[i2c->msg_idx].buf[i2c->buf_idx]);
1707 i2c->msg_idx++;
1708 i2c->buf_idx = 0;
1709
1710 /* all the read mags xfer succeed,wakeup the thread */
1711 if (i2c->msg_idx == i2c->msg_num) {
1712 goto out_success;
1713 } else if (i2c->msg_idx < i2c->msg_num) { /* repeat start */
1714 if (sunxi_i2c_engine_restart(i2c)) {
1715 dev_err(i2c->dev, "when the %d msg xfering, start failed",
1716 i2c->msg_idx);
1717 goto out_failed;
1718 }
1719 break;
1720 } else {
1721 goto out_failed;
1722 }
1723 } else {
1724 goto out_failed;
1725 }
1726
1727 case 0xd8:
1728 dev_err(i2c->dev, "second addr has transmitted, ACK not received!");
1729 goto out_failed;
1730
1731 case 0x20:
1732 dev_err(i2c->dev, "SLA+W has been transmitted; ACK not received\n");
1733 goto out_failed;
1734
1735 case 0x30:
1736 dev_err(i2c->dev, "DATA byte transmitted, ACK not receive\n");
1737 goto out_failed;
1738
1739 case 0x38:
1740 dev_err(i2c->dev, "Arbitration lost in SLA+W, SLA+R or data bytes\n");
1741 goto out_failed;
1742
1743 case 0x00:
1744 dev_err(i2c->dev, "Bus error\n");
1745 goto out_failed;
1746
1747 default:
1748 goto out_failed;
1749 }
1750
1751 /* just for debug */
1752 i2c->debug_state = state;
1753
1754 /* this time xfer is't completed,return and enable irq
1755 * then wait new interrupt coming to continue xfer
1756 */
1757 out_break:
1758 sunxi_i2c_engine_clear_irq(base_addr);
1759 sunxi_i2c_engine_enable_irq(base_addr);
1760 spin_unlock_irqrestore(&i2c->lock, flags);
1761 return 0;
1762
1763 /* xfer failed, then send stop and wakeup */
1764 out_failed:
1765 if (sunxi_i2c_engine_stop(i2c))
1766 dev_err(i2c->dev, "STOP failed!\n");
1767
1768 i2c->msg_idx = -state;
1769 i2c->result = RESULT_ERR;
1770 i2c->debug_state = state;
1771 spin_unlock_irqrestore(&i2c->lock, flags);
1772 wake_up(&i2c->wait);
1773 return -state;
1774
1775 /* xfer success, then send wtop and wakeup */
1776 out_success:
1777 if (sunxi_i2c_engine_stop(i2c))
1778 dev_err(i2c->dev, "STOP failed!\n");
1779
1780 i2c->result = RESULT_COMPLETE;
1781 i2c->debug_state = state;
1782 spin_unlock_irqrestore(&i2c->lock, flags);
1783 wake_up(&i2c->wait);
1784 return 0;
1785 }
1786
sunxi_i2c_handler(int this_irq,void * dev_id)1787 static irqreturn_t sunxi_i2c_handler(int this_irq, void *dev_id)
1788 {
1789 struct sunxi_i2c *i2c = (struct sunxi_i2c *)dev_id;
1790 u32 ret = sunxi_i2c_check_irq(i2c);
1791
1792 if (ret == 0) {
1793 sunxi_i2c_engine_core_process(i2c);
1794 } else if (ret == 1) {
1795 sunxi_i2c_drv_core_process(i2c);
1796 } else {
1797 dev_err(i2c->dev, "wrong irq, check irq number!!\n");
1798 return IRQ_NONE;
1799 }
1800
1801 return IRQ_HANDLED;
1802 }
1803
1804
1805 /* i2c controller tx xfer function
1806 * return the xfered msgs num on success,or the negative error num on failed
1807 */
sunxi_i2c_drv_tx_one_msg(struct sunxi_i2c * i2c,struct i2c_msg * msg)1808 static int sunxi_i2c_drv_tx_one_msg(struct sunxi_i2c *i2c, struct i2c_msg *msg)
1809 {
1810 unsigned long flags;
1811 int ret;
1812
1813 dev_dbg(i2c->dev, "drv-mode: one-msg write slave_addr=0x%x, data_len=0x%x\n",
1814 msg->addr, msg->len);
1815
1816 spin_lock_irqsave(&i2c->lock, flags);
1817 i2c->msg = msg;
1818 i2c->buf_idx = 0;
1819 spin_unlock_irqrestore(&i2c->lock, flags);
1820
1821 sunxi_i2c_drv_disable_read_mode(i2c->base_addr);
1822 sunxi_i2c_drv_set_slave_addr(i2c, msg);
1823 if (msg->len == 1) {
1824 sunxi_i2c_drv_set_addr_byte(i2c->base_addr, 0);
1825 sunxi_i2c_drv_set_data_byte(i2c->base_addr, msg->len);
1826 } else {
1827 sunxi_i2c_drv_set_addr_byte(i2c->base_addr, 1);
1828 sunxi_i2c_drv_set_data_byte(i2c->base_addr, msg->len - 1);
1829 }
1830 sunxi_i2c_drv_set_packet_cnt(i2c->base_addr, 1);
1831 sunxi_i2c_drv_enable_irq(i2c->base_addr, TRAN_ERR_INT_EN | TRAN_COM_INT_EN);
1832
1833 if (i2c->dma_tx && (msg->len >= MAX_FIFO)) {
1834 dev_dbg(i2c->dev, "drv-mode: dma write\n");
1835 sunxi_i2c_drv_set_tx_trig(i2c->base_addr, MAX_FIFO/2);
1836 sunxi_i2c_drv_enable_dma_irq(i2c->base_addr, DMA_TX_EN);
1837
1838 ret = sunxi_i2c_drv_dma_xfer_init(i2c, I2C_WRITE);
1839 if (ret) {
1840 dev_err(i2c->dev, "dma tx xfer init failed\n");
1841 goto err_dma;
1842 }
1843
1844 sunxi_i2c_drv_start_xfer(i2c);
1845 } else {
1846 dev_dbg(i2c->dev, "drv-mode: cpu write\n");
1847 sunxi_i2c_drv_set_tx_trig(i2c->base_addr, msg->len);
1848 /*
1849 * if now fifo can't store all the msg data buf
1850 * enable the TX_REQ_INT_EN, and wait TX_REQ_INT,
1851 * then continue send the remaining data to fifo
1852 */
1853 if (sunxi_i2c_drv_send_msg(i2c, i2c->msg))
1854 sunxi_i2c_drv_enable_irq(i2c->base_addr, TX_REQ_INT_EN);
1855
1856 sunxi_i2c_drv_start_xfer(i2c);
1857 }
1858
1859 ret = sunxi_i2c_drv_complete(i2c);
1860
1861 if (i2c->dma_tx && (msg->len > MAX_FIFO)) {
1862 sunxi_i2c_drv_dma_xfer_deinit(i2c);
1863 sunxi_i2c_drv_disable_dma_irq(i2c->base_addr, DMA_TX_EN | DMA_RX_EN);
1864 }
1865 sunxi_i2c_drv_disable_irq(i2c->base_addr, I2C_DRV_INT_EN_MASK);
1866
1867 return ret;
1868
1869 err_dma:
1870 sunxi_i2c_drv_disable_dma_irq(i2c->base_addr, DMA_TX_EN | DMA_RX_EN);
1871 sunxi_i2c_drv_disable_irq(i2c->base_addr, I2C_DRV_INT_EN_MASK);
1872
1873 return ret;
1874 }
1875
1876 /* read xfer msg function
1877 * msg : the msg queue
1878 * num : the number of msg, usually is one or two:
1879 *
1880 * num = 1 : some i2c slave device support one msg to read, need't reg_addr
1881 *
1882 * num = 2 : normally i2c reead need two msg, msg example is as following :
1883 * msg[0]->flag express write, msg[0]->buf store the reg data,
1884 * msg[1]->flag express read, msg[1]-<buf store the data recived
1885 * return 0 on succes.
1886 */
1887 static int
sunxi_i2c_drv_rx_msgs(struct sunxi_i2c * i2c,struct i2c_msg * msgs,int num)1888 sunxi_i2c_drv_rx_msgs(struct sunxi_i2c *i2c, struct i2c_msg *msgs, int num)
1889 {
1890 struct i2c_msg *wmsg, *rmsg;
1891 int ret;
1892
1893 if (num == 1) {
1894 wmsg = NULL;
1895 rmsg = msgs;
1896 dev_dbg(i2c->dev, "drv-mode: one-msg read slave_addr=0x%x, data_len=0x%x\n",
1897 rmsg->addr, rmsg->len);
1898 sunxi_i2c_drv_enable_read_mode(i2c->base_addr);
1899 sunxi_i2c_drv_set_addr_byte(i2c->base_addr, 0);
1900 } else if (num == 2) {
1901 wmsg = msgs;
1902 rmsg = msgs + 1;
1903 dev_dbg(i2c->dev, "drv-mode: two-msg read slave_addr=0x%x, data_len=0x%x\n",
1904 rmsg->addr, rmsg->len);
1905 if (wmsg->addr != rmsg->addr) {
1906 dev_err(i2c->dev, "drv-mode: two msg's addr must be the same\n");
1907 return -EINVAL;
1908 }
1909 sunxi_i2c_drv_disable_read_mode(i2c->base_addr);
1910 sunxi_i2c_drv_set_addr_byte(i2c->base_addr, wmsg->len);
1911 } else {
1912 dev_err(i2c->dev, "i2c read xfer can not transfer %d msgs once", num);
1913 return -EINVAL;
1914 }
1915
1916 i2c->msg = rmsg;
1917
1918 sunxi_i2c_drv_set_slave_addr(i2c, rmsg);
1919 sunxi_i2c_drv_set_packet_cnt(i2c->base_addr, 1);
1920 sunxi_i2c_drv_set_data_byte(i2c->base_addr, rmsg->len);
1921 sunxi_i2c_drv_enable_irq(i2c->base_addr, TRAN_ERR_INT_EN | TRAN_COM_INT_EN);
1922
1923 if (wmsg)
1924 sunxi_i2c_drv_send_msg(i2c, wmsg);
1925
1926 if (i2c->dma_rx && (rmsg->len > MAX_FIFO)) {
1927 dev_dbg(i2c->dev, "drv-mode: rx msgs by dma\n");
1928 sunxi_i2c_drv_set_rx_trig(i2c->base_addr, MAX_FIFO/2);
1929 sunxi_i2c_drv_enable_dma_irq(i2c->base_addr, DMA_RX_EN);
1930
1931 ret = sunxi_i2c_drv_dma_xfer_init(i2c, I2C_READ);
1932 if (ret) {
1933 dev_err(i2c->dev, "dma rx xfer init failed\n");
1934 goto err_dma;
1935 }
1936
1937 sunxi_i2c_drv_start_xfer(i2c);
1938 } else {
1939 dev_dbg(i2c->dev, "drv-mode: rx msgs by cpu\n");
1940 /* set the rx_trigger_level max to avoid the RX_REQ com before COM_REQ */
1941 sunxi_i2c_drv_set_rx_trig(i2c->base_addr, MAX_FIFO);
1942 sunxi_i2c_drv_enable_irq(i2c->base_addr, RX_REQ_INT_EN);
1943 sunxi_i2c_drv_start_xfer(i2c);
1944 }
1945
1946 ret = sunxi_i2c_drv_complete(i2c);
1947
1948 if (rmsg->len > MAX_FIFO) {
1949 sunxi_i2c_drv_dma_xfer_deinit(i2c);
1950 sunxi_i2c_drv_disable_dma_irq(i2c->base_addr, DMA_TX_EN | DMA_RX_EN);
1951 }
1952 sunxi_i2c_drv_disable_irq(i2c->base_addr, I2C_DRV_INT_EN_MASK);
1953
1954 return ret;
1955
1956 err_dma:
1957 sunxi_i2c_drv_disable_dma_irq(i2c->base_addr, DMA_TX_EN | DMA_RX_EN);
1958 sunxi_i2c_drv_disable_irq(i2c->base_addr, I2C_DRV_INT_EN_MASK);
1959
1960 return ret;
1961 }
1962
1963 /**
1964 * @i2c: struct of sunxi_i2c
1965 * @msgs: One or more messages to execute before STOP is issued to terminate
1966 * the operation,and each message begins with a START.
1967 * @num: the Number of messages to be executed.
1968 *
1969 * return the number of xfered or the negative error num
1970 */
1971 static int
sunxi_i2c_drv_xfer(struct sunxi_i2c * i2c,struct i2c_msg * msgs,int num)1972 sunxi_i2c_drv_xfer(struct sunxi_i2c *i2c, struct i2c_msg *msgs, int num)
1973 {
1974 int i = 0;
1975 unsigned long flags;
1976
1977 spin_lock_irqsave(&i2c->lock, flags);
1978 i2c->result = RESULT_IDLE;
1979 spin_unlock_irqrestore(&i2c->lock, flags);
1980
1981 sunxi_i2c_drv_clear_irq(i2c->base_addr, I2C_DRV_INT_STA_MASK);
1982 sunxi_i2c_drv_disable_irq(i2c->base_addr, I2C_DRV_INT_STA_MASK);
1983 sunxi_i2c_drv_disable_dma_irq(i2c->base_addr, DMA_TX_EN | DMA_RX_EN);
1984
1985 sunxi_i2c_drv_clear_txfifo(i2c->base_addr);
1986 sunxi_i2c_drv_clear_rxfifo(i2c->base_addr);
1987
1988 while (i < num) {
1989 dev_dbg(i2c->dev, "drv-mode: addr: 0x%x, flag:%x, len:%d\n",
1990 msgs[i].addr, msgs[i].flags, msgs[i].len);
1991 if (msgs[i].flags & I2C_M_RD) {
1992 if (sunxi_i2c_drv_rx_msgs(i2c, &msgs[i], 1))
1993 return -EINVAL;
1994 i++;
1995 } else if (i + 1 < num && msgs[i + 1].flags & I2C_M_RD) {
1996 if (sunxi_i2c_drv_rx_msgs(i2c, &msgs[i], 2))
1997 return -EINVAL;
1998 i += 2;
1999 } else {
2000 if (sunxi_i2c_drv_tx_one_msg(i2c, &msgs[i]))
2001 return -EINVAL;
2002 i++;
2003 }
2004 }
2005
2006 return i;
2007 }
2008
2009 static int
sunxi_i2c_engine_xfer(struct sunxi_i2c * i2c,struct i2c_msg * msgs,int num)2010 sunxi_i2c_engine_xfer(struct sunxi_i2c *i2c, struct i2c_msg *msgs, int num)
2011 {
2012 unsigned long flags;
2013 int ret;
2014 void __iomem *base_addr = i2c->base_addr;
2015
2016 sunxi_i2c_engine_disable_ack(base_addr);
2017 sunxi_i2c_engine_set_efr(base_addr, NO_DATA_WROTE);
2018 sunxi_i2c_engine_clear_irq(base_addr);
2019
2020 /* may conflict with xfer_complete */
2021 spin_lock_irqsave(&i2c->lock, flags);
2022 i2c->msg = msgs;
2023 i2c->msg_num = num;
2024 i2c->msg_idx = 0;
2025 i2c->result = RESULT_IDLE;
2026 i2c->status = I2C_XFER_START;
2027 spin_unlock_irqrestore(&i2c->lock, flags);
2028
2029 sunxi_i2c_engine_enable_irq(base_addr);
2030 /* then send START signal, and needn't clear int flag */
2031 ret = sunxi_i2c_engine_start(i2c);
2032 if (ret) {
2033 dev_err(i2c->dev, "i2c failed to send start signal\n");
2034 sunxi_i2c_soft_reset(i2c);
2035 sunxi_i2c_engine_disable_irq(i2c->base_addr);
2036 i2c->status = I2C_XFER_IDLE;
2037 return -EINVAL;
2038 }
2039
2040 spin_lock_irqsave(&i2c->lock, flags);
2041 i2c->status = I2C_XFER_RUNNING;
2042 spin_unlock_irqrestore(&i2c->lock, flags);
2043
2044 /*
2045 * sleep and wait, timeput = 5*HZ
2046 * then do the transfer at function : sunxi_i2c_engine_core_process
2047 */
2048 return sunxi_i2c_engine_complete(i2c);
2049 }
2050
2051 static int
sunxi_i2c_xfer(struct i2c_adapter * adap,struct i2c_msg * msgs,int num)2052 sunxi_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
2053 {
2054 struct sunxi_i2c *i2c = (struct sunxi_i2c *)adap->algo_data;
2055 int ret;
2056
2057 if (IS_ERR_OR_NULL(msgs) || (num <= 0)) {
2058 dev_err(i2c->dev, "invalid argument\n");
2059 return -EINVAL;
2060 }
2061
2062 /* then the sunxi_i2c_runtime_reseme() call back */
2063 ret = pm_runtime_get_sync(i2c->dev);
2064 if (ret < 0)
2065 goto out;
2066
2067 sunxi_i2c_soft_reset(i2c);
2068
2069 ret = sunxi_i2c_bus_barrier(&i2c->adap);
2070 if (ret) {
2071 dev_err(i2c->dev, "i2c bus barrier failed, sda is still low!\n");
2072 goto out;
2073 }
2074
2075 /* set the i2c status to idle */
2076 i2c->result = RESULT_IDLE;
2077 if (i2c->twi_drv_used)
2078 ret = sunxi_i2c_drv_xfer(i2c, msgs, num);
2079 else
2080 ret = sunxi_i2c_engine_xfer(i2c, msgs, num);
2081
2082 out:
2083 pm_runtime_mark_last_busy(i2c->dev);
2084 /* asnyc release to ensure other module all suspend */
2085 pm_runtime_put_autosuspend(i2c->dev);
2086
2087 return ret;
2088 }
2089
sunxi_i2c_functionality(struct i2c_adapter * adap)2090 static unsigned int sunxi_i2c_functionality(struct i2c_adapter *adap)
2091 {
2092 return I2C_FUNC_I2C|I2C_FUNC_10BIT_ADDR|I2C_FUNC_SMBUS_EMUL;
2093 }
2094
2095 static const struct i2c_algorithm sunxi_i2c_algorithm = {
2096 .master_xfer = sunxi_i2c_xfer,
2097 .functionality = sunxi_i2c_functionality,
2098 };
2099
2100 static struct i2c_bus_recovery_info sunxi_i2c_bus_recovery = {
2101 .get_scl = sunxi_i2c_get_scl,
2102 .set_scl = sunxi_i2c_set_scl,
2103 .get_sda = sunxi_i2c_get_sda,
2104 .set_sda = sunxi_i2c_set_sda,
2105 .get_bus_free = sunxi_i2c_get_bus_free,
2106 .recover_bus = i2c_generic_scl_recovery,
2107 };
2108
sunxi_i2c_regulator_request(struct sunxi_i2c * i2c)2109 static int sunxi_i2c_regulator_request(struct sunxi_i2c *i2c)
2110 {
2111 if (i2c->regulator)
2112 return 0;
2113
2114 i2c->regulator = regulator_get(i2c->dev, "twi");
2115 if (IS_ERR(i2c->regulator)) {
2116 dev_err(i2c->dev, "get supply failed!\n");
2117 return -EPROBE_DEFER;
2118 }
2119
2120 return 0;
2121 }
2122
sunxi_i2c_regulator_release(struct sunxi_i2c * i2c)2123 static int sunxi_i2c_regulator_release(struct sunxi_i2c *i2c)
2124 {
2125 if (!i2c->regulator)
2126 return 0;
2127
2128 regulator_put(i2c->regulator);
2129
2130 i2c->regulator = NULL;
2131
2132 return 0;
2133 }
2134
sunxi_i2c_regulator_enable(struct sunxi_i2c * i2c)2135 static int sunxi_i2c_regulator_enable(struct sunxi_i2c *i2c)
2136 {
2137 if (!i2c->regulator)
2138 return 0;
2139
2140 if (regulator_enable(i2c->regulator)) {
2141 dev_err(i2c->dev, "enable regulator failed!\n");
2142 return -EINVAL;
2143 }
2144
2145 return 0;
2146 }
2147
sunxi_i2c_regulator_disable(struct sunxi_i2c * i2c)2148 static int sunxi_i2c_regulator_disable(struct sunxi_i2c *i2c)
2149 {
2150 if (!i2c->regulator)
2151 return 0;
2152
2153 if (regulator_is_enabled(i2c->regulator))
2154 regulator_disable(i2c->regulator);
2155
2156 return 0;
2157 }
2158
sunxi_i2c_clk_request(struct sunxi_i2c * i2c)2159 static int sunxi_i2c_clk_request(struct sunxi_i2c *i2c)
2160 {
2161 i2c->bus_clk = devm_clk_get(i2c->dev, "bus");
2162 if (IS_ERR(i2c->bus_clk)) {
2163 dev_err(i2c->dev, "request clock failed\n");
2164 return -EINVAL;
2165 }
2166
2167 i2c->reset = devm_reset_control_get(i2c->dev, NULL);
2168 if (IS_ERR_OR_NULL(i2c->reset)) {
2169 dev_err(i2c->dev, "request reset failed\n");
2170 return -EINVAL;
2171 }
2172 return 0;
2173 }
2174
sunxi_i2c_resource_get(struct device_node * np,struct sunxi_i2c * i2c)2175 static int sunxi_i2c_resource_get(struct device_node *np, struct sunxi_i2c *i2c)
2176 {
2177 int err;
2178
2179 i2c->bus_num = of_alias_get_id(np, "twi");
2180 if (i2c->bus_num < 0) {
2181 dev_err(i2c->dev, "I2C failed to get alias id\n");
2182 return -EINVAL;
2183 }
2184 i2c->pdev->id = i2c->bus_num;
2185
2186 dev_set_name(i2c->dev, SUNXI_I2C_ID_FORMAT, i2c->bus_num);
2187
2188 i2c->res = platform_get_resource(i2c->pdev, IORESOURCE_MEM, 0);
2189 if (!i2c->res) {
2190 dev_err(i2c->dev, "failed to get MEM res\n");
2191 return -ENXIO;
2192 }
2193 i2c->base_addr = devm_ioremap_resource(&i2c->pdev->dev, i2c->res);
2194 if (IS_ERR_OR_NULL(i2c->base_addr)) {
2195 dev_err(i2c->dev, "unable to ioremap\n");
2196 /* PTR_RET is deprecated in Linux 5.10, PTR_ERR_OR_ZERO replaces PTR_RET */
2197 return PTR_ERR_OR_ZERO(i2c->base_addr);
2198 }
2199
2200 err = sunxi_i2c_clk_request(i2c);
2201 if (err) {
2202 dev_err(i2c->dev, "request i2c clk failed!\n");
2203 return err;
2204 }
2205
2206 i2c->pctrl = devm_pinctrl_get(i2c->dev);
2207 if (IS_ERR(i2c->pctrl)) {
2208 dev_err(i2c->dev, "pinctrl_get failed\n");
2209 return PTR_ERR(i2c->pctrl);
2210 }
2211
2212 i2c->no_suspend = 0;
2213 of_property_read_u32(np, "no_suspend", &i2c->no_suspend);
2214 i2c->irq_flag = (i2c->no_suspend) ? IRQF_NO_SUSPEND : 0;
2215
2216 i2c->irq = platform_get_irq(i2c->pdev, 0);
2217 if (i2c->irq < 0) {
2218 dev_err(i2c->dev, "failed to get irq\n");
2219 return i2c->irq;
2220 }
2221 err = devm_request_irq(&i2c->pdev->dev, i2c->irq, sunxi_i2c_handler,
2222 i2c->irq_flag, dev_name(i2c->dev), i2c);
2223 if (err) {
2224 dev_err(i2c->dev, "request irq failed!\n");
2225 return err;
2226 }
2227
2228 err = sunxi_i2c_regulator_request(i2c);
2229 if (err) {
2230 dev_err(i2c->dev, "request regulator failed!\n");
2231 return err;
2232 }
2233
2234 err = of_property_read_u32(np, "clock-frequency", &i2c->bus_freq);
2235 if (err) {
2236 dev_err(i2c->dev, "failed to get clock frequency\n");
2237 goto err0;
2238 }
2239
2240 i2c->pkt_interval = 0;
2241 of_property_read_u32(np, "twi_pkt_interval", &i2c->pkt_interval);
2242 i2c->twi_drv_used = 0;
2243 of_property_read_u32(np, "twi_drv_used", &i2c->twi_drv_used);
2244
2245 if (i2c->twi_drv_used) {
2246 err = sunxi_i2c_dma_request(i2c, (dma_addr_t)i2c->res->start);
2247 if (err)
2248 dev_info(i2c->dev, "dma channel request failed, "
2249 "or no dma comfiguration information in dts\n");
2250 }
2251
2252 return 0;
2253
2254 err0:
2255 sunxi_i2c_regulator_release(i2c);
2256 return err;
2257 }
2258
sunxi_i2c_resource_put(struct sunxi_i2c * i2c)2259 static void sunxi_i2c_resource_put(struct sunxi_i2c *i2c)
2260 {
2261 if (i2c->twi_drv_used)
2262 sunxi_i2c_dma_release(i2c);
2263
2264 sunxi_i2c_regulator_release(i2c);
2265 }
2266
sunxi_i2c_select_pin_state(struct sunxi_i2c * i2c,char * name)2267 static int sunxi_i2c_select_pin_state(struct sunxi_i2c *i2c, char *name)
2268 {
2269 int ret;
2270 struct pinctrl_state *pctrl_state = NULL;
2271
2272 pctrl_state = pinctrl_lookup_state(i2c->pctrl, name);
2273 if (IS_ERR(pctrl_state)) {
2274 dev_err(i2c->dev, "pinctrl_lookup_state(%s) failed! return %p\n",
2275 name, pctrl_state);
2276 return -EINVAL;
2277 }
2278
2279 ret = pinctrl_select_state(i2c->pctrl, pctrl_state);
2280 if (ret < 0)
2281 dev_err(i2c->dev, "pinctrl select state(%s) failed! return %d\n",
2282 name, ret);
2283
2284 return ret;
2285 }
2286
sunxi_i2c_clk_init(struct sunxi_i2c * i2c)2287 static int sunxi_i2c_clk_init(struct sunxi_i2c *i2c)
2288 {
2289 unsigned long clk_rate;
2290 int err;
2291
2292 /*
2293 * i2c will be used in the uboot stage, so it must be reset before
2294 * configuring the module registers to clean up the configuration
2295 * information of the uboot stage
2296 */
2297 if (reset_control_reset(i2c->reset)) {
2298 dev_err(i2c->dev, "reset control deassert failed!\n");
2299 return -EINVAL;
2300 }
2301
2302 if (clk_prepare_enable(i2c->bus_clk)) {
2303 dev_err(i2c->dev, "enable apb_twi clock failed!\n");
2304 return -EINVAL;
2305 }
2306
2307 /* get twi module clock */
2308 clk_rate = clk_get_rate(i2c->bus_clk);
2309 if (clk_rate == 0) {
2310 dev_err(i2c->dev, "get source clock frequency failed!\n");
2311 err = -EINVAL;
2312 goto err0;
2313 }
2314
2315 /* i2c ctroller module clock is controllerd by self */
2316 sunxi_i2c_set_frequency(i2c);
2317
2318 return 0;
2319
2320 err0:
2321 clk_disable_unprepare(i2c->bus_clk);
2322 return err;
2323 }
2324
sunxi_i2c_clk_exit(struct sunxi_i2c * i2c)2325 static void sunxi_i2c_clk_exit(struct sunxi_i2c *i2c)
2326 {
2327 /* disable clk */
2328 if (!IS_ERR_OR_NULL(i2c->bus_clk) && __clk_is_enabled(i2c->bus_clk)) {
2329 clk_disable_unprepare(i2c->bus_clk);
2330 }
2331
2332 if (!IS_ERR_OR_NULL(i2c->reset)) {
2333 reset_control_assert(i2c->reset);
2334 }
2335 }
2336
sunxi_i2c_hw_init(struct sunxi_i2c * i2c)2337 static int sunxi_i2c_hw_init(struct sunxi_i2c *i2c)
2338 {
2339 int err;
2340
2341 err = sunxi_i2c_regulator_enable(i2c);
2342 if (err) {
2343 dev_err(i2c->dev, "enable regulator failed!\n");
2344 goto err0;
2345 }
2346
2347 err = sunxi_i2c_select_pin_state(i2c, PINCTRL_STATE_DEFAULT);
2348 if (err) {
2349 dev_err(i2c->dev, "request i2c gpio failed!\n");
2350 goto err1;
2351 }
2352
2353 err = sunxi_i2c_clk_init(i2c);
2354 if (err) {
2355 dev_err(i2c->dev, "init i2c clock failed!\n");
2356 goto err2;
2357 }
2358
2359 sunxi_i2c_soft_reset(i2c);
2360
2361 sunxi_i2c_bus_enable(i2c);
2362
2363 return 0;
2364
2365 err2:
2366 sunxi_i2c_select_pin_state(i2c, PINCTRL_STATE_SLEEP);
2367
2368 err1:
2369 sunxi_i2c_regulator_disable(i2c);
2370
2371 err0:
2372 return err;
2373 }
2374
sunxi_i2c_hw_exit(struct sunxi_i2c * i2c)2375 static void sunxi_i2c_hw_exit(struct sunxi_i2c *i2c)
2376 {
2377 sunxi_i2c_bus_disable(i2c);
2378
2379 sunxi_i2c_clk_exit(i2c);
2380
2381 sunxi_i2c_select_pin_state(i2c, PINCTRL_STATE_SLEEP);
2382
2383 sunxi_i2c_regulator_disable(i2c);
2384 }
2385
sunxi_i2c_info_show(struct device * dev,struct device_attribute * attr,char * buf)2386 static ssize_t sunxi_i2c_info_show(struct device *dev,
2387 struct device_attribute *attr, char *buf)
2388 {
2389 struct sunxi_i2c *i2c = dev_get_drvdata(dev);
2390
2391 return snprintf(buf, PAGE_SIZE,
2392 "i2c->bus_num = %d\n"
2393 "i2c->name = %s\n"
2394 "i2c->irq = %d\n"
2395 "i2c->freqency = %u\n",
2396 i2c->bus_num,
2397 dev_name(i2c->dev),
2398 i2c->irq,
2399 i2c->bus_freq);
2400 }
2401 static struct device_attribute sunxi_i2c_info_attr =
2402 __ATTR(info, S_IRUGO, sunxi_i2c_info_show, NULL);
2403
2404
sunxi_i2c_status_show(struct device * dev,struct device_attribute * attr,char * buf)2405 static ssize_t sunxi_i2c_status_show(struct device *dev,
2406 struct device_attribute *attr, char *buf)
2407 {
2408 struct sunxi_i2c *i2c = dev_get_drvdata(dev);
2409 static char const *i2c_status[] = {"Unknown", "Idle", "Start",
2410 "Unknown", "Running"};
2411
2412 if (i2c == NULL)
2413 return snprintf(buf, PAGE_SIZE, "%s\n", "sunxi_i2c is NULL!");
2414
2415 return snprintf(buf, PAGE_SIZE,
2416 "i2c->bus_num = %d\n"
2417 "i2c->status = [%u] %s\n"
2418 "i2c->msg_num = %u, ->msg_idx = %u, ->buf_idx = %u\n"
2419 "i2c->bus_freq = %u\n"
2420 "i2c->irq = %d\n"
2421 "i2c->debug_state = %u\n"
2422 "i2c->base_addr = 0x%p, the TWI control register:\n"
2423 "[ADDR] 0x%02x = 0x%08x, [XADDR] 0x%02x = 0x%08x\n"
2424 "[DATA] 0x%02x = 0x%08x, [CNTR] 0x%02x = 0x%08x\n"
2425 "[STAT] 0x%02x = 0x%08x, [CCR] 0x%02x = 0x%08x\n"
2426 "[SRST] 0x%02x = 0x%08x, [EFR] 0x%02x = 0x%08x\n"
2427 "[LCR] 0x%02x = 0x%08x\n",
2428 i2c->bus_num, i2c->status, i2c_status[i2c->status],
2429 i2c->msg_num, i2c->msg_idx, i2c->buf_idx,
2430 i2c->bus_freq, i2c->irq, i2c->debug_state,
2431 i2c->base_addr,
2432 I2C_ADDR, readl(i2c->base_addr + I2C_ADDR),
2433 I2C_XADDR, readl(i2c->base_addr + I2C_XADDR),
2434 I2C_DATA, readl(i2c->base_addr + I2C_DATA),
2435 I2C_CNTR, readl(i2c->base_addr + I2C_CNTR),
2436 I2C_STAT, readl(i2c->base_addr + I2C_STAT),
2437 I2C_CCR, readl(i2c->base_addr + I2C_CCR),
2438 I2C_SRST, readl(i2c->base_addr + I2C_SRST),
2439 I2C_EFR, readl(i2c->base_addr + I2C_EFR),
2440 I2C_LCR, readl(i2c->base_addr + I2C_LCR));
2441 }
2442
2443
2444 static struct device_attribute sunxi_i2c_status_attr =
2445 __ATTR(status, S_IRUGO, sunxi_i2c_status_show, NULL);
2446
sunxi_i2c_create_sysfs(struct platform_device * _pdev)2447 static void sunxi_i2c_create_sysfs(struct platform_device *_pdev)
2448 {
2449 device_create_file(&_pdev->dev, &sunxi_i2c_info_attr);
2450 device_create_file(&_pdev->dev, &sunxi_i2c_status_attr);
2451 }
2452
sunxi_i2c_remove_sysfs(struct platform_device * _pdev)2453 static void sunxi_i2c_remove_sysfs(struct platform_device *_pdev)
2454 {
2455 device_remove_file(&_pdev->dev, &sunxi_i2c_info_attr);
2456 device_remove_file(&_pdev->dev, &sunxi_i2c_status_attr);
2457 }
2458
sunxi_i2c_probe(struct platform_device * pdev)2459 static int sunxi_i2c_probe(struct platform_device *pdev)
2460 {
2461 struct sunxi_i2c *i2c = NULL;
2462 int err;
2463
2464 i2c = devm_kzalloc(&pdev->dev, sizeof(struct sunxi_i2c), GFP_KERNEL);
2465
2466 /* Do not print prompts after kzalloc errors */
2467 if (IS_ERR_OR_NULL(i2c))
2468 return -ENOMEM;
2469
2470 i2c->pdev = pdev;
2471 i2c->dev = &pdev->dev;
2472 pdev->name = dev_name(i2c->dev);
2473
2474 /* get dts resource */
2475 err = sunxi_i2c_resource_get(pdev->dev.of_node, i2c);
2476 if (err) {
2477 dev_err(i2c->dev, "I2C failed to get resource\n");
2478 goto err0;
2479 }
2480
2481 /* i2c controller hardware init */
2482 err = sunxi_i2c_hw_init(i2c);
2483 if (err) {
2484 dev_err(i2c->dev, "hw init failed! try again!!\n");
2485 goto err1;
2486 }
2487
2488 spin_lock_init(&i2c->lock);
2489 init_waitqueue_head(&i2c->wait);
2490
2491 sunxi_i2c_create_sysfs(pdev);
2492
2493 pm_runtime_set_active(i2c->dev);
2494 if (i2c->no_suspend)
2495 pm_runtime_get_noresume(i2c->dev);
2496 pm_runtime_set_autosuspend_delay(i2c->dev, AUTOSUSPEND_TIMEOUT);
2497 pm_runtime_use_autosuspend(i2c->dev);
2498 pm_runtime_enable(i2c->dev);
2499
2500 snprintf(i2c->adap.name, sizeof(i2c->adap.name), dev_name(&pdev->dev));
2501 i2c->status = I2C_XFER_IDLE;
2502 i2c->adap.owner = THIS_MODULE;
2503 i2c->adap.nr = i2c->bus_num;
2504 i2c->adap.retries = 3;
2505 i2c->adap.timeout = 5*HZ;
2506 i2c->adap.class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
2507 i2c->adap.algo = &sunxi_i2c_algorithm;
2508 i2c->adap.bus_recovery_info = &sunxi_i2c_bus_recovery;
2509 i2c->adap.algo_data = i2c;
2510 i2c->adap.dev.parent = &pdev->dev;
2511 i2c->adap.dev.of_node = pdev->dev.of_node;
2512 platform_set_drvdata(pdev, i2c);
2513
2514 /*
2515 * register i2c adapter should be the ending of probe
2516 * before register all the resouce i2c controller need be ready
2517 * (i2c_xfer may occur at any time when register)
2518 */
2519 err = i2c_add_numbered_adapter(&i2c->adap);
2520 if (err) {
2521 dev_err(i2c->dev, "failed to add adapter\n");
2522 goto err2;
2523 }
2524
2525 if (!i2c->no_suspend) {
2526 pm_runtime_mark_last_busy(i2c->dev);
2527 pm_runtime_put_autosuspend(i2c->dev);
2528 }
2529
2530 dev_info(i2c->dev, "probe success\n");
2531 return 0;
2532
2533 err2:
2534 pm_runtime_set_suspended(i2c->dev);
2535 pm_runtime_disable(i2c->dev);
2536 sunxi_i2c_remove_sysfs(pdev);
2537 sunxi_i2c_hw_exit(i2c);
2538
2539 err1:
2540 sunxi_i2c_resource_put(i2c);
2541
2542 err0:
2543 return err;
2544 }
2545
sunxi_i2c_remove(struct platform_device * pdev)2546 static int sunxi_i2c_remove(struct platform_device *pdev)
2547 {
2548 struct sunxi_i2c *i2c = platform_get_drvdata(pdev);
2549
2550 i2c_del_adapter(&i2c->adap);
2551
2552 platform_set_drvdata(pdev, NULL);
2553
2554 pm_runtime_set_suspended(i2c->dev);
2555 pm_runtime_disable(i2c->dev);
2556 sunxi_i2c_remove_sysfs(pdev);
2557 sunxi_i2c_hw_exit(i2c);
2558
2559 sunxi_i2c_resource_put(i2c);
2560
2561 dev_dbg(i2c->dev, "remove\n");
2562
2563 return 0;
2564 }
2565
sunxi_i2c_shutdown(struct platform_device * pdev)2566 static void sunxi_i2c_shutdown(struct platform_device *pdev)
2567 {
2568 int timeout;
2569 struct sunxi_i2c *i2c = platform_get_drvdata(pdev);
2570
2571 for (timeout = 0; timeout < 200; timeout++) {
2572 if (i2c->result == RESULT_IDLE)
2573 break;
2574 mdelay(10);
2575 }
2576
2577 sunxi_i2c_bus_disable(i2c);
2578 }
2579
2580 #if IS_ENABLED(CONFIG_PM)
sunxi_i2c_runtime_suspend(struct device * dev)2581 static int sunxi_i2c_runtime_suspend(struct device *dev)
2582 {
2583 struct sunxi_i2c *i2c = dev_get_drvdata(dev);
2584
2585 sunxi_i2c_hw_exit(i2c);
2586 dev_dbg(i2c->dev, "runtime suspend finish\n");
2587
2588 return 0;
2589 }
2590
sunxi_i2c_runtime_resume(struct device * dev)2591 static int sunxi_i2c_runtime_resume(struct device *dev)
2592 {
2593 struct sunxi_i2c *i2c = dev_get_drvdata(dev);
2594
2595 sunxi_i2c_hw_init(i2c);
2596 dev_dbg(i2c->dev, "runtime resume finish\n");
2597
2598 return 0;
2599 }
2600
sunxi_i2c_suspend_noirq(struct device * dev)2601 static int sunxi_i2c_suspend_noirq(struct device *dev)
2602 {
2603 struct platform_device *pdev = to_platform_device(dev);
2604 struct sunxi_i2c *i2c = platform_get_drvdata(pdev);
2605
2606 if (i2c->twi_drv_used)
2607 sunxi_i2c_bus_disable(i2c);
2608
2609 if (i2c->no_suspend) {
2610 dev_dbg(i2c->dev, "doesn't need to suspend\n");
2611 return 0;
2612 }
2613
2614 dev_dbg(i2c->dev, "suspend noirq\n");
2615 return pm_runtime_force_suspend(dev);
2616 }
2617
sunxi_i2c_resume_noirq(struct device * dev)2618 static int sunxi_i2c_resume_noirq(struct device *dev)
2619 {
2620 struct platform_device *pdev = to_platform_device(dev);
2621 struct sunxi_i2c *i2c = platform_get_drvdata(pdev);
2622
2623 if (i2c->twi_drv_used) {
2624 sunxi_i2c_set_frequency(i2c);
2625 sunxi_i2c_bus_enable(i2c);
2626 }
2627
2628 if (i2c->no_suspend) {
2629 dev_dbg(i2c->dev, "doesn't need to resume\n");
2630 return 0;
2631 }
2632
2633 dev_dbg(i2c->dev, "resume noirq\n");
2634 return pm_runtime_force_resume(dev);
2635 }
2636
2637 static const struct dev_pm_ops sunxi_i2c_dev_pm_ops = {
2638 .suspend_noirq = sunxi_i2c_suspend_noirq,
2639 .resume_noirq = sunxi_i2c_resume_noirq,
2640 .runtime_suspend = sunxi_i2c_runtime_suspend,
2641 .runtime_resume = sunxi_i2c_runtime_resume,
2642 };
2643
2644 #define SUNXI_I2C_DEV_PM_OPS (&sunxi_i2c_dev_pm_ops)
2645 #else
2646 #define SUNXI_I2C_DEV_PM_OPS NULL
2647 #endif
2648
2649 static const struct of_device_id sunxi_i2c_match[] = {
2650 { .compatible = "allwinner,sun8i-twi", },
2651 { .compatible = "allwinner,sun20i-twi", },
2652 { .compatible = "allwinner,sun50i-twi", },
2653 {},
2654 };
2655 MODULE_DEVICE_TABLE(of, sunxi_i2c_match);
2656
2657 static struct platform_driver sunxi_i2c_driver = {
2658 .probe = sunxi_i2c_probe,
2659 .remove = sunxi_i2c_remove,
2660 .shutdown = sunxi_i2c_shutdown,
2661 .driver = {
2662 .name = SUNXI_I2C_DEV_NAME,
2663 .owner = THIS_MODULE,
2664 .pm = SUNXI_I2C_DEV_PM_OPS,
2665 .of_match_table = sunxi_i2c_match,
2666 },
2667 };
2668
sunxi_i2c_adap_init(void)2669 static int __init sunxi_i2c_adap_init(void)
2670 {
2671 return platform_driver_register(&sunxi_i2c_driver);
2672 }
2673
sunxi_i2c_adap_exit(void)2674 static void __exit sunxi_i2c_adap_exit(void)
2675 {
2676 platform_driver_unregister(&sunxi_i2c_driver);
2677 }
2678
2679 subsys_initcall_sync(sunxi_i2c_adap_init);
2680
2681 module_exit(sunxi_i2c_adap_exit);
2682
2683 MODULE_LICENSE("GPL");
2684 MODULE_ALIAS("platform:i2c-sunxi");
2685 MODULE_VERSION("2.0.6");
2686 MODULE_DESCRIPTION("SUNXI I2C Bus Driver");
2687 MODULE_AUTHOR("pannan");
2688