4 * Copyright (C) 2013 Wolfram Sang <wsa@sang-engineering.com>
5 * Copyright (C) 2013 Renesas Solutions Corp.
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License version 2 as published by
9 * the Free Software Foundation.
13 * This i2c core has a lot of interrupts, namely 8. We use their chaining as
14 * some kind of state machine.
16 * 1) The main xfer routine kicks off a transmission by putting the start bit
17 * (or repeated start) on the bus and enabling the transmit interrupt (TIE)
18 * since we need to send the slave address + RW bit in every case.
20 * 2) TIE sends slave address + RW bit and selects how to continue.
22 * 3a) Write case: We keep utilizing TIE as long as we have data to send. If we
23 * are done, we switch over to the transmission done interrupt (TEIE) and mark
24 * the message as completed (includes sending STOP) there.
26 * 3b) Read case: We switch over to receive interrupt (RIE). One dummy read is
27 * needed to start clocking, then we keep receiving until we are done. Note
28 * that we use the RDRFS mode all the time, i.e. we ACK/NACK every byte by
29 * writing to the ACKBT bit. I tried using the RDRFS mode only at the end of a
30 * message to create the final NACK as sketched in the datasheet. This caused
31 * some subtle races (when byte n was processed and byte n+1 was already
32 * waiting), though, and I started with the safe approach.
34 * 4) If we got a NACK somewhere, we flag the error and stop the transmission
37 * Also check the comments in the interrupt routines for some gory details.
40 #include <linux/clk.h>
41 #include <linux/completion.h>
42 #include <linux/err.h>
43 #include <linux/i2c.h>
44 #include <linux/interrupt.h>
46 #include <linux/module.h>
48 #include <linux/platform_device.h>
50 #define RIIC_ICCR1 0x00
51 #define RIIC_ICCR2 0x04
52 #define RIIC_ICMR1 0x08
53 #define RIIC_ICMR3 0x10
54 #define RIIC_ICSER 0x18
55 #define RIIC_ICIER 0x1c
56 #define RIIC_ICSR2 0x24
57 #define RIIC_ICBRL 0x34
58 #define RIIC_ICBRH 0x38
59 #define RIIC_ICDRT 0x3c
60 #define RIIC_ICDRR 0x40
62 #define ICCR1_ICE 0x80
63 #define ICCR1_IICRST 0x40
64 #define ICCR1_SOWP 0x10
66 #define ICCR2_BBSY 0x80
71 #define ICMR1_CKS_MASK 0x70
72 #define ICMR1_BCWP 0x08
73 #define ICMR1_CKS(_x) ((((_x) << 4) & ICMR1_CKS_MASK) | ICMR1_BCWP)
75 #define ICMR3_RDRFS 0x20
76 #define ICMR3_ACKWP 0x10
77 #define ICMR3_ACKBT 0x08
79 #define ICIER_TIE 0x80
80 #define ICIER_TEIE 0x40
81 #define ICIER_RIE 0x20
82 #define ICIER_NAKIE 0x10
83 #define ICIER_SPIE 0x08
85 #define ICSR2_NACKF 0x10
87 /* ICBRx (@ PCLK 33MHz) */
88 #define ICBR_RESERVED 0xe0 /* Should be 1 on writes */
89 #define ICBRL_SP100K (19 | ICBR_RESERVED)
90 #define ICBRH_SP100K (16 | ICBR_RESERVED)
91 #define ICBRL_SP400K (21 | ICBR_RESERVED)
92 #define ICBRH_SP400K (9 | ICBR_RESERVED)
94 #define RIIC_INIT_MSG -1
103 struct completion msg_done
;
104 struct i2c_adapter adapter
;
108 struct riic_irq_desc
{
114 static inline void riic_clear_set_bit(struct riic_dev
*riic
, u8 clear
, u8 set
, u8 reg
)
116 writeb((readb(riic
->base
+ reg
) & ~clear
) | set
, riic
->base
+ reg
);
119 static int riic_xfer(struct i2c_adapter
*adap
, struct i2c_msg msgs
[], int num
)
121 struct riic_dev
*riic
= i2c_get_adapdata(adap
);
122 unsigned long time_left
;
126 ret
= clk_prepare_enable(riic
->clk
);
130 if (readb(riic
->base
+ RIIC_ICCR2
) & ICCR2_BBSY
) {
135 reinit_completion(&riic
->msg_done
);
138 writeb(0, riic
->base
+ RIIC_ICSR2
);
140 for (i
= 0, start_bit
= ICCR2_ST
; i
< num
; i
++) {
141 riic
->bytes_left
= RIIC_INIT_MSG
;
142 riic
->buf
= msgs
[i
].buf
;
143 riic
->msg
= &msgs
[i
];
144 riic
->is_last
= (i
== num
- 1);
146 writeb(ICIER_NAKIE
| ICIER_TIE
, riic
->base
+ RIIC_ICIER
);
148 writeb(start_bit
, riic
->base
+ RIIC_ICCR2
);
150 time_left
= wait_for_completion_timeout(&riic
->msg_done
, riic
->adapter
.timeout
);
152 riic
->err
= -ETIMEDOUT
;
157 start_bit
= ICCR2_RS
;
161 clk_disable_unprepare(riic
->clk
);
163 return riic
->err
?: num
;
166 static irqreturn_t
riic_tdre_isr(int irq
, void *data
)
168 struct riic_dev
*riic
= data
;
171 if (!riic
->bytes_left
)
174 if (riic
->bytes_left
== RIIC_INIT_MSG
) {
175 val
= !!(riic
->msg
->flags
& I2C_M_RD
);
177 /* On read, switch over to receive interrupt */
178 riic_clear_set_bit(riic
, ICIER_TIE
, ICIER_RIE
, RIIC_ICIER
);
180 /* On write, initialize length */
181 riic
->bytes_left
= riic
->msg
->len
;
183 val
|= (riic
->msg
->addr
<< 1);
191 * Switch to transmission ended interrupt when done. Do check here
192 * after bytes_left was initialized to support SMBUS_QUICK (new msg has
195 if (riic
->bytes_left
== 0)
196 riic_clear_set_bit(riic
, ICIER_TIE
, ICIER_TEIE
, RIIC_ICIER
);
199 * This acks the TIE interrupt. We get another TIE immediately if our
200 * value could be moved to the shadow shift register right away. So
201 * this must be after updates to ICIER (where we want to disable TIE)!
203 writeb(val
, riic
->base
+ RIIC_ICDRT
);
208 static irqreturn_t
riic_tend_isr(int irq
, void *data
)
210 struct riic_dev
*riic
= data
;
212 if (readb(riic
->base
+ RIIC_ICSR2
) & ICSR2_NACKF
) {
213 /* We got a NACKIE */
214 readb(riic
->base
+ RIIC_ICDRR
); /* dummy read */
216 } else if (riic
->bytes_left
) {
220 if (riic
->is_last
|| riic
->err
) {
221 riic_clear_set_bit(riic
, ICIER_TEIE
, ICIER_SPIE
, RIIC_ICIER
);
222 writeb(ICCR2_SP
, riic
->base
+ RIIC_ICCR2
);
224 /* Transfer is complete, but do not send STOP */
225 riic_clear_set_bit(riic
, ICIER_TEIE
, 0, RIIC_ICIER
);
226 complete(&riic
->msg_done
);
232 static irqreturn_t
riic_rdrf_isr(int irq
, void *data
)
234 struct riic_dev
*riic
= data
;
236 if (!riic
->bytes_left
)
239 if (riic
->bytes_left
== RIIC_INIT_MSG
) {
240 riic
->bytes_left
= riic
->msg
->len
;
241 readb(riic
->base
+ RIIC_ICDRR
); /* dummy read */
245 if (riic
->bytes_left
== 1) {
246 /* STOP must come before we set ACKBT! */
248 riic_clear_set_bit(riic
, 0, ICIER_SPIE
, RIIC_ICIER
);
249 writeb(ICCR2_SP
, riic
->base
+ RIIC_ICCR2
);
252 riic_clear_set_bit(riic
, 0, ICMR3_ACKBT
, RIIC_ICMR3
);
255 riic_clear_set_bit(riic
, ICMR3_ACKBT
, 0, RIIC_ICMR3
);
258 /* Reading acks the RIE interrupt */
259 *riic
->buf
= readb(riic
->base
+ RIIC_ICDRR
);
266 static irqreturn_t
riic_stop_isr(int irq
, void *data
)
268 struct riic_dev
*riic
= data
;
270 /* read back registers to confirm writes have fully propagated */
271 writeb(0, riic
->base
+ RIIC_ICSR2
);
272 readb(riic
->base
+ RIIC_ICSR2
);
273 writeb(0, riic
->base
+ RIIC_ICIER
);
274 readb(riic
->base
+ RIIC_ICIER
);
276 complete(&riic
->msg_done
);
281 static u32
riic_func(struct i2c_adapter
*adap
)
283 return I2C_FUNC_I2C
| I2C_FUNC_SMBUS_EMUL
;
286 static const struct i2c_algorithm riic_algo
= {
287 .master_xfer
= riic_xfer
,
288 .functionality
= riic_func
,
291 static int riic_init_hw(struct riic_dev
*riic
, u32 spd
)
296 ret
= clk_prepare_enable(riic
->clk
);
301 * TODO: Implement formula to calculate the timing values depending on
302 * variable parent clock rate and arbitrary bus speed
304 rate
= clk_get_rate(riic
->clk
);
305 if (rate
!= 33325000) {
306 dev_err(&riic
->adapter
.dev
,
307 "invalid parent clk (%lu). Must be 33325000Hz\n", rate
);
308 clk_disable_unprepare(riic
->clk
);
312 /* Changing the order of accessing IICRST and ICE may break things! */
313 writeb(ICCR1_IICRST
| ICCR1_SOWP
, riic
->base
+ RIIC_ICCR1
);
314 riic_clear_set_bit(riic
, 0, ICCR1_ICE
, RIIC_ICCR1
);
318 writeb(ICMR1_CKS(3), riic
->base
+ RIIC_ICMR1
);
319 writeb(ICBRH_SP100K
, riic
->base
+ RIIC_ICBRH
);
320 writeb(ICBRL_SP100K
, riic
->base
+ RIIC_ICBRL
);
323 writeb(ICMR1_CKS(1), riic
->base
+ RIIC_ICMR1
);
324 writeb(ICBRH_SP400K
, riic
->base
+ RIIC_ICBRH
);
325 writeb(ICBRL_SP400K
, riic
->base
+ RIIC_ICBRL
);
328 dev_err(&riic
->adapter
.dev
,
329 "unsupported bus speed (%dHz). Use 100000 or 400000\n", spd
);
330 clk_disable_unprepare(riic
->clk
);
334 writeb(0, riic
->base
+ RIIC_ICSER
);
335 writeb(ICMR3_ACKWP
| ICMR3_RDRFS
, riic
->base
+ RIIC_ICMR3
);
337 riic_clear_set_bit(riic
, ICCR1_IICRST
, 0, RIIC_ICCR1
);
339 clk_disable_unprepare(riic
->clk
);
344 static struct riic_irq_desc riic_irqs
[] = {
345 { .res_num
= 0, .isr
= riic_tend_isr
, .name
= "riic-tend" },
346 { .res_num
= 1, .isr
= riic_rdrf_isr
, .name
= "riic-rdrf" },
347 { .res_num
= 2, .isr
= riic_tdre_isr
, .name
= "riic-tdre" },
348 { .res_num
= 3, .isr
= riic_stop_isr
, .name
= "riic-stop" },
349 { .res_num
= 5, .isr
= riic_tend_isr
, .name
= "riic-nack" },
352 static int riic_i2c_probe(struct platform_device
*pdev
)
354 struct device_node
*np
= pdev
->dev
.of_node
;
355 struct riic_dev
*riic
;
356 struct i2c_adapter
*adap
;
357 struct resource
*res
;
361 riic
= devm_kzalloc(&pdev
->dev
, sizeof(*riic
), GFP_KERNEL
);
365 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
366 riic
->base
= devm_ioremap_resource(&pdev
->dev
, res
);
367 if (IS_ERR(riic
->base
))
368 return PTR_ERR(riic
->base
);
370 riic
->clk
= devm_clk_get(&pdev
->dev
, NULL
);
371 if (IS_ERR(riic
->clk
)) {
372 dev_err(&pdev
->dev
, "missing controller clock");
373 return PTR_ERR(riic
->clk
);
376 for (i
= 0; i
< ARRAY_SIZE(riic_irqs
); i
++) {
377 res
= platform_get_resource(pdev
, IORESOURCE_IRQ
, riic_irqs
[i
].res_num
);
381 ret
= devm_request_irq(&pdev
->dev
, res
->start
, riic_irqs
[i
].isr
,
382 0, riic_irqs
[i
].name
, riic
);
384 dev_err(&pdev
->dev
, "failed to request irq %s\n", riic_irqs
[i
].name
);
389 adap
= &riic
->adapter
;
390 i2c_set_adapdata(adap
, riic
);
391 strlcpy(adap
->name
, "Renesas RIIC adapter", sizeof(adap
->name
));
392 adap
->owner
= THIS_MODULE
;
393 adap
->algo
= &riic_algo
;
394 adap
->dev
.parent
= &pdev
->dev
;
395 adap
->dev
.of_node
= pdev
->dev
.of_node
;
397 init_completion(&riic
->msg_done
);
399 of_property_read_u32(np
, "clock-frequency", &bus_rate
);
400 ret
= riic_init_hw(riic
, bus_rate
);
405 ret
= i2c_add_adapter(adap
);
407 dev_err(&pdev
->dev
, "failed to add adapter\n");
411 platform_set_drvdata(pdev
, riic
);
413 dev_info(&pdev
->dev
, "registered with %dHz bus speed\n", bus_rate
);
417 static int riic_i2c_remove(struct platform_device
*pdev
)
419 struct riic_dev
*riic
= platform_get_drvdata(pdev
);
421 writeb(0, riic
->base
+ RIIC_ICIER
);
422 i2c_del_adapter(&riic
->adapter
);
427 static const struct of_device_id riic_i2c_dt_ids
[] = {
428 { .compatible
= "renesas,riic-rz" },
432 static struct platform_driver riic_i2c_driver
= {
433 .probe
= riic_i2c_probe
,
434 .remove
= riic_i2c_remove
,
437 .of_match_table
= riic_i2c_dt_ids
,
441 module_platform_driver(riic_i2c_driver
);
443 MODULE_DESCRIPTION("Renesas RIIC adapter");
444 MODULE_AUTHOR("Wolfram Sang <wsa@sang-engineering.com>");
445 MODULE_LICENSE("GPL v2");
446 MODULE_DEVICE_TABLE(of
, riic_i2c_dt_ids
);