1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (C) 2013 Wolfram Sang <wsa@sang-engineering.com>
6 * Copyright (C) 2013 Renesas Solutions Corp.
10 * This i2c core has a lot of interrupts, namely 8. We use their chaining as
11 * some kind of state machine.
13 * 1) The main xfer routine kicks off a transmission by putting the start bit
14 * (or repeated start) on the bus and enabling the transmit interrupt (TIE)
15 * since we need to send the slave address + RW bit in every case.
17 * 2) TIE sends slave address + RW bit and selects how to continue.
19 * 3a) Write case: We keep utilizing TIE as long as we have data to send. If we
20 * are done, we switch over to the transmission done interrupt (TEIE) and mark
21 * the message as completed (includes sending STOP) there.
23 * 3b) Read case: We switch over to receive interrupt (RIE). One dummy read is
24 * needed to start clocking, then we keep receiving until we are done. Note
25 * that we use the RDRFS mode all the time, i.e. we ACK/NACK every byte by
26 * writing to the ACKBT bit. I tried using the RDRFS mode only at the end of a
27 * message to create the final NACK as sketched in the datasheet. This caused
28 * some subtle races (when byte n was processed and byte n+1 was already
29 * waiting), though, and I started with the safe approach.
31 * 4) If we got a NACK somewhere, we flag the error and stop the transmission
34 * Also check the comments in the interrupt routines for some gory details.
37 #include <linux/clk.h>
38 #include <linux/completion.h>
39 #include <linux/err.h>
40 #include <linux/i2c.h>
41 #include <linux/interrupt.h>
43 #include <linux/module.h>
45 #include <linux/platform_device.h>
47 #define RIIC_ICCR1 0x00
48 #define RIIC_ICCR2 0x04
49 #define RIIC_ICMR1 0x08
50 #define RIIC_ICMR3 0x10
51 #define RIIC_ICSER 0x18
52 #define RIIC_ICIER 0x1c
53 #define RIIC_ICSR2 0x24
54 #define RIIC_ICBRL 0x34
55 #define RIIC_ICBRH 0x38
56 #define RIIC_ICDRT 0x3c
57 #define RIIC_ICDRR 0x40
59 #define ICCR1_ICE 0x80
60 #define ICCR1_IICRST 0x40
61 #define ICCR1_SOWP 0x10
63 #define ICCR2_BBSY 0x80
68 #define ICMR1_CKS_MASK 0x70
69 #define ICMR1_BCWP 0x08
70 #define ICMR1_CKS(_x) ((((_x) << 4) & ICMR1_CKS_MASK) | ICMR1_BCWP)
72 #define ICMR3_RDRFS 0x20
73 #define ICMR3_ACKWP 0x10
74 #define ICMR3_ACKBT 0x08
76 #define ICIER_TIE 0x80
77 #define ICIER_TEIE 0x40
78 #define ICIER_RIE 0x20
79 #define ICIER_NAKIE 0x10
80 #define ICIER_SPIE 0x08
82 #define ICSR2_NACKF 0x10
84 #define ICBR_RESERVED 0xe0 /* Should be 1 on writes */
86 #define RIIC_INIT_MSG -1
95 struct completion msg_done
;
96 struct i2c_adapter adapter
;
100 struct riic_irq_desc
{
106 static inline void riic_clear_set_bit(struct riic_dev
*riic
, u8 clear
, u8 set
, u8 reg
)
108 writeb((readb(riic
->base
+ reg
) & ~clear
) | set
, riic
->base
+ reg
);
111 static int riic_xfer(struct i2c_adapter
*adap
, struct i2c_msg msgs
[], int num
)
113 struct riic_dev
*riic
= i2c_get_adapdata(adap
);
114 unsigned long time_left
;
118 ret
= clk_prepare_enable(riic
->clk
);
122 if (readb(riic
->base
+ RIIC_ICCR2
) & ICCR2_BBSY
) {
127 reinit_completion(&riic
->msg_done
);
130 writeb(0, riic
->base
+ RIIC_ICSR2
);
132 for (i
= 0, start_bit
= ICCR2_ST
; i
< num
; i
++) {
133 riic
->bytes_left
= RIIC_INIT_MSG
;
134 riic
->buf
= msgs
[i
].buf
;
135 riic
->msg
= &msgs
[i
];
136 riic
->is_last
= (i
== num
- 1);
138 writeb(ICIER_NAKIE
| ICIER_TIE
, riic
->base
+ RIIC_ICIER
);
140 writeb(start_bit
, riic
->base
+ RIIC_ICCR2
);
142 time_left
= wait_for_completion_timeout(&riic
->msg_done
, riic
->adapter
.timeout
);
144 riic
->err
= -ETIMEDOUT
;
149 start_bit
= ICCR2_RS
;
153 clk_disable_unprepare(riic
->clk
);
155 return riic
->err
?: num
;
158 static irqreturn_t
riic_tdre_isr(int irq
, void *data
)
160 struct riic_dev
*riic
= data
;
163 if (!riic
->bytes_left
)
166 if (riic
->bytes_left
== RIIC_INIT_MSG
) {
167 if (riic
->msg
->flags
& I2C_M_RD
)
168 /* On read, switch over to receive interrupt */
169 riic_clear_set_bit(riic
, ICIER_TIE
, ICIER_RIE
, RIIC_ICIER
);
171 /* On write, initialize length */
172 riic
->bytes_left
= riic
->msg
->len
;
174 val
= i2c_8bit_addr_from_msg(riic
->msg
);
182 * Switch to transmission ended interrupt when done. Do check here
183 * after bytes_left was initialized to support SMBUS_QUICK (new msg has
186 if (riic
->bytes_left
== 0)
187 riic_clear_set_bit(riic
, ICIER_TIE
, ICIER_TEIE
, RIIC_ICIER
);
190 * This acks the TIE interrupt. We get another TIE immediately if our
191 * value could be moved to the shadow shift register right away. So
192 * this must be after updates to ICIER (where we want to disable TIE)!
194 writeb(val
, riic
->base
+ RIIC_ICDRT
);
199 static irqreturn_t
riic_tend_isr(int irq
, void *data
)
201 struct riic_dev
*riic
= data
;
203 if (readb(riic
->base
+ RIIC_ICSR2
) & ICSR2_NACKF
) {
204 /* We got a NACKIE */
205 readb(riic
->base
+ RIIC_ICDRR
); /* dummy read */
206 riic_clear_set_bit(riic
, ICSR2_NACKF
, 0, RIIC_ICSR2
);
208 } else if (riic
->bytes_left
) {
212 if (riic
->is_last
|| riic
->err
) {
213 riic_clear_set_bit(riic
, ICIER_TEIE
, ICIER_SPIE
, RIIC_ICIER
);
214 writeb(ICCR2_SP
, riic
->base
+ RIIC_ICCR2
);
216 /* Transfer is complete, but do not send STOP */
217 riic_clear_set_bit(riic
, ICIER_TEIE
, 0, RIIC_ICIER
);
218 complete(&riic
->msg_done
);
224 static irqreturn_t
riic_rdrf_isr(int irq
, void *data
)
226 struct riic_dev
*riic
= data
;
228 if (!riic
->bytes_left
)
231 if (riic
->bytes_left
== RIIC_INIT_MSG
) {
232 riic
->bytes_left
= riic
->msg
->len
;
233 readb(riic
->base
+ RIIC_ICDRR
); /* dummy read */
237 if (riic
->bytes_left
== 1) {
238 /* STOP must come before we set ACKBT! */
240 riic_clear_set_bit(riic
, 0, ICIER_SPIE
, RIIC_ICIER
);
241 writeb(ICCR2_SP
, riic
->base
+ RIIC_ICCR2
);
244 riic_clear_set_bit(riic
, 0, ICMR3_ACKBT
, RIIC_ICMR3
);
247 riic_clear_set_bit(riic
, ICMR3_ACKBT
, 0, RIIC_ICMR3
);
250 /* Reading acks the RIE interrupt */
251 *riic
->buf
= readb(riic
->base
+ RIIC_ICDRR
);
258 static irqreturn_t
riic_stop_isr(int irq
, void *data
)
260 struct riic_dev
*riic
= data
;
262 /* read back registers to confirm writes have fully propagated */
263 writeb(0, riic
->base
+ RIIC_ICSR2
);
264 readb(riic
->base
+ RIIC_ICSR2
);
265 writeb(0, riic
->base
+ RIIC_ICIER
);
266 readb(riic
->base
+ RIIC_ICIER
);
268 complete(&riic
->msg_done
);
273 static u32
riic_func(struct i2c_adapter
*adap
)
275 return I2C_FUNC_I2C
| I2C_FUNC_SMBUS_EMUL
;
278 static const struct i2c_algorithm riic_algo
= {
279 .master_xfer
= riic_xfer
,
280 .functionality
= riic_func
,
283 static int riic_init_hw(struct riic_dev
*riic
, struct i2c_timings
*t
)
287 int total_ticks
, cks
, brl
, brh
;
289 ret
= clk_prepare_enable(riic
->clk
);
293 if (t
->bus_freq_hz
> 400000) {
294 dev_err(&riic
->adapter
.dev
,
295 "unsupported bus speed (%dHz). 400000 max\n",
297 clk_disable_unprepare(riic
->clk
);
301 rate
= clk_get_rate(riic
->clk
);
304 * Assume the default register settings:
305 * FER.SCLE = 1 (SCL sync circuit enabled, adds 2 or 3 cycles)
306 * FER.NFE = 1 (noise circuit enabled)
307 * MR3.NF = 0 (1 cycle of noise filtered out)
309 * Freq (CKS=000) = (I2CCLK + tr + tf)/ (BRH + 3 + 1) + (BRL + 3 + 1)
310 * Freq (CKS!=000) = (I2CCLK + tr + tf)/ (BRH + 2 + 1) + (BRL + 2 + 1)
314 * Determine reference clock rate. We must be able to get the desired
315 * frequency with only 62 clock ticks max (31 high, 31 low).
316 * Aim for a duty of 60% LOW, 40% HIGH.
318 total_ticks
= DIV_ROUND_UP(rate
, t
->bus_freq_hz
);
320 for (cks
= 0; cks
< 7; cks
++) {
322 * 60% low time must be less than BRL + 2 + 1
323 * BRL max register value is 0x1F.
325 brl
= ((total_ticks
* 6) / 10);
326 if (brl
<= (0x1F + 3))
333 if (brl
> (0x1F + 3)) {
334 dev_err(&riic
->adapter
.dev
, "invalid speed (%lu). Too slow.\n",
335 (unsigned long)t
->bus_freq_hz
);
336 clk_disable_unprepare(riic
->clk
);
340 brh
= total_ticks
- brl
;
342 /* Remove automatic clock ticks for sync circuit and NF */
352 * Remove clock ticks for rise and fall times. Convert ns to clock
355 brl
-= t
->scl_fall_ns
/ (1000000000 / rate
);
356 brh
-= t
->scl_rise_ns
/ (1000000000 / rate
);
358 /* Adjust for min register values for when SCLE=1 and NFE=1 */
364 pr_debug("i2c-riic: freq=%lu, duty=%d, fall=%lu, rise=%lu, cks=%d, brl=%d, brh=%d\n",
365 rate
/ total_ticks
, ((brl
+ 3) * 100) / (brl
+ brh
+ 6),
366 t
->scl_fall_ns
/ (1000000000 / rate
),
367 t
->scl_rise_ns
/ (1000000000 / rate
), cks
, brl
, brh
);
369 /* Changing the order of accessing IICRST and ICE may break things! */
370 writeb(ICCR1_IICRST
| ICCR1_SOWP
, riic
->base
+ RIIC_ICCR1
);
371 riic_clear_set_bit(riic
, 0, ICCR1_ICE
, RIIC_ICCR1
);
373 writeb(ICMR1_CKS(cks
), riic
->base
+ RIIC_ICMR1
);
374 writeb(brh
| ICBR_RESERVED
, riic
->base
+ RIIC_ICBRH
);
375 writeb(brl
| ICBR_RESERVED
, riic
->base
+ RIIC_ICBRL
);
377 writeb(0, riic
->base
+ RIIC_ICSER
);
378 writeb(ICMR3_ACKWP
| ICMR3_RDRFS
, riic
->base
+ RIIC_ICMR3
);
380 riic_clear_set_bit(riic
, ICCR1_IICRST
, 0, RIIC_ICCR1
);
382 clk_disable_unprepare(riic
->clk
);
387 static struct riic_irq_desc riic_irqs
[] = {
388 { .res_num
= 0, .isr
= riic_tend_isr
, .name
= "riic-tend" },
389 { .res_num
= 1, .isr
= riic_rdrf_isr
, .name
= "riic-rdrf" },
390 { .res_num
= 2, .isr
= riic_tdre_isr
, .name
= "riic-tdre" },
391 { .res_num
= 3, .isr
= riic_stop_isr
, .name
= "riic-stop" },
392 { .res_num
= 5, .isr
= riic_tend_isr
, .name
= "riic-nack" },
395 static int riic_i2c_probe(struct platform_device
*pdev
)
397 struct riic_dev
*riic
;
398 struct i2c_adapter
*adap
;
399 struct resource
*res
;
400 struct i2c_timings i2c_t
;
403 riic
= devm_kzalloc(&pdev
->dev
, sizeof(*riic
), GFP_KERNEL
);
407 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
408 riic
->base
= devm_ioremap_resource(&pdev
->dev
, res
);
409 if (IS_ERR(riic
->base
))
410 return PTR_ERR(riic
->base
);
412 riic
->clk
= devm_clk_get(&pdev
->dev
, NULL
);
413 if (IS_ERR(riic
->clk
)) {
414 dev_err(&pdev
->dev
, "missing controller clock");
415 return PTR_ERR(riic
->clk
);
418 for (i
= 0; i
< ARRAY_SIZE(riic_irqs
); i
++) {
419 res
= platform_get_resource(pdev
, IORESOURCE_IRQ
, riic_irqs
[i
].res_num
);
423 ret
= devm_request_irq(&pdev
->dev
, res
->start
, riic_irqs
[i
].isr
,
424 0, riic_irqs
[i
].name
, riic
);
426 dev_err(&pdev
->dev
, "failed to request irq %s\n", riic_irqs
[i
].name
);
431 adap
= &riic
->adapter
;
432 i2c_set_adapdata(adap
, riic
);
433 strlcpy(adap
->name
, "Renesas RIIC adapter", sizeof(adap
->name
));
434 adap
->owner
= THIS_MODULE
;
435 adap
->algo
= &riic_algo
;
436 adap
->dev
.parent
= &pdev
->dev
;
437 adap
->dev
.of_node
= pdev
->dev
.of_node
;
439 init_completion(&riic
->msg_done
);
441 i2c_parse_fw_timings(&pdev
->dev
, &i2c_t
, true);
443 ret
= riic_init_hw(riic
, &i2c_t
);
448 ret
= i2c_add_adapter(adap
);
452 platform_set_drvdata(pdev
, riic
);
454 dev_info(&pdev
->dev
, "registered with %dHz bus speed\n",
459 static int riic_i2c_remove(struct platform_device
*pdev
)
461 struct riic_dev
*riic
= platform_get_drvdata(pdev
);
463 writeb(0, riic
->base
+ RIIC_ICIER
);
464 i2c_del_adapter(&riic
->adapter
);
469 static const struct of_device_id riic_i2c_dt_ids
[] = {
470 { .compatible
= "renesas,riic-rz" },
474 static struct platform_driver riic_i2c_driver
= {
475 .probe
= riic_i2c_probe
,
476 .remove
= riic_i2c_remove
,
479 .of_match_table
= riic_i2c_dt_ids
,
483 module_platform_driver(riic_i2c_driver
);
485 MODULE_DESCRIPTION("Renesas RIIC adapter");
486 MODULE_AUTHOR("Wolfram Sang <wsa@sang-engineering.com>");
487 MODULE_LICENSE("GPL v2");
488 MODULE_DEVICE_TABLE(of
, riic_i2c_dt_ids
);