2 * drivers/i2c/busses/i2c-rcar.c
4 * Copyright (C) 2012 Renesas Solutions Corp.
5 * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
7 * This file is based on the drivers/i2c/busses/i2c-sh7760.c
8 * (c) 2005-2008 MSC Vertriebsges.m.b.H, Manuel Lauss <mlau@msc-ge.com>
10 * This file used out-of-tree driver i2c-rcar.c
11 * Copyright (C) 2011-2012 Renesas Electronics Corporation
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #include <linux/clk.h>
27 #include <linux/delay.h>
28 #include <linux/err.h>
29 #include <linux/init.h>
30 #include <linux/interrupt.h>
32 #include <linux/i2c.h>
33 #include <linux/i2c/i2c-rcar.h>
34 #include <linux/kernel.h>
35 #include <linux/module.h>
36 #include <linux/platform_device.h>
37 #include <linux/pm_runtime.h>
38 #include <linux/slab.h>
39 #include <linux/spinlock.h>
41 /* register offsets */
42 #define ICSCR 0x00 /* slave ctrl */
43 #define ICMCR 0x04 /* master ctrl */
44 #define ICSSR 0x08 /* slave status */
45 #define ICMSR 0x0C /* master status */
46 #define ICSIER 0x10 /* slave irq enable */
47 #define ICMIER 0x14 /* master irq enable */
48 #define ICCCR 0x18 /* clock dividers */
49 #define ICSAR 0x1C /* slave address */
50 #define ICMAR 0x20 /* master address */
51 #define ICRXTX 0x24 /* data port */
54 #define MDBS (1 << 7) /* non-fifo mode switch */
55 #define FSCL (1 << 6) /* override SCL pin */
56 #define FSDA (1 << 5) /* override SDA pin */
57 #define OBPC (1 << 4) /* override pins */
58 #define MIE (1 << 3) /* master if enable */
60 #define FSB (1 << 1) /* force stop bit */
61 #define ESG (1 << 0) /* en startbit gen */
64 #define MNR (1 << 6) /* nack received */
65 #define MAL (1 << 5) /* arbitration lost */
66 #define MST (1 << 4) /* sent a stop */
70 #define MAT (1 << 0) /* slave addr xfer done */
73 #define MNRE (1 << 6) /* nack irq en */
74 #define MALE (1 << 5) /* arblos irq en */
75 #define MSTE (1 << 4) /* stop irq en */
79 #define MATE (1 << 0) /* address sent irq en */
90 RCAR_IRQ_OPEN_FOR_SEND
,
91 RCAR_IRQ_OPEN_FOR_RECV
,
92 RCAR_IRQ_OPEN_FOR_STOP
,
98 #define ID_LAST_MSG (1 << 0)
99 #define ID_IOERROR (1 << 1)
100 #define ID_DONE (1 << 2)
101 #define ID_ARBLOST (1 << 3)
102 #define ID_NACK (1 << 4)
109 struct rcar_i2c_priv
{
111 struct i2c_adapter adap
;
115 wait_queue_head_t wait
;
121 enum rcar_i2c_type devtype
;
124 #define rcar_i2c_priv_to_dev(p) ((p)->adap.dev.parent)
125 #define rcar_i2c_is_recv(p) ((p)->msg->flags & I2C_M_RD)
127 #define rcar_i2c_flags_set(p, f) ((p)->flags |= (f))
128 #define rcar_i2c_flags_has(p, f) ((p)->flags & (f))
130 #define LOOP_TIMEOUT 1024
135 static void rcar_i2c_write(struct rcar_i2c_priv
*priv
, int reg
, u32 val
)
137 writel(val
, priv
->io
+ reg
);
140 static u32
rcar_i2c_read(struct rcar_i2c_priv
*priv
, int reg
)
142 return readl(priv
->io
+ reg
);
145 static void rcar_i2c_init(struct rcar_i2c_priv
*priv
)
149 * slave mode is not used on this driver
151 rcar_i2c_write(priv
, ICSIER
, 0);
152 rcar_i2c_write(priv
, ICSAR
, 0);
153 rcar_i2c_write(priv
, ICSCR
, 0);
154 rcar_i2c_write(priv
, ICSSR
, 0);
156 /* reset master mode */
157 rcar_i2c_write(priv
, ICMIER
, 0);
158 rcar_i2c_write(priv
, ICMCR
, 0);
159 rcar_i2c_write(priv
, ICMSR
, 0);
160 rcar_i2c_write(priv
, ICMAR
, 0);
163 static void rcar_i2c_irq_mask(struct rcar_i2c_priv
*priv
, int open
)
165 u32 val
= MNRE
| MALE
| MSTE
| MATE
; /* default */
168 case RCAR_IRQ_OPEN_FOR_SEND
:
169 val
|= MDEE
; /* default + send */
171 case RCAR_IRQ_OPEN_FOR_RECV
:
172 val
|= MDRE
; /* default + read */
174 case RCAR_IRQ_OPEN_FOR_STOP
:
175 val
= MSTE
; /* stop irq only */
179 val
= 0; /* all close */
182 rcar_i2c_write(priv
, ICMIER
, val
);
185 static void rcar_i2c_set_addr(struct rcar_i2c_priv
*priv
, u32 recv
)
187 rcar_i2c_write(priv
, ICMAR
, (priv
->msg
->addr
<< 1) | recv
);
191 * bus control functions
193 static int rcar_i2c_bus_barrier(struct rcar_i2c_priv
*priv
)
197 for (i
= 0; i
< LOOP_TIMEOUT
; i
++) {
198 /* make sure that bus is not busy */
199 if (!(rcar_i2c_read(priv
, ICMCR
) & FSDA
))
207 static void rcar_i2c_bus_phase(struct rcar_i2c_priv
*priv
, int phase
)
210 case RCAR_BUS_PHASE_ADDR
:
211 rcar_i2c_write(priv
, ICMCR
, MDBS
| MIE
| ESG
);
213 case RCAR_BUS_PHASE_DATA
:
214 rcar_i2c_write(priv
, ICMCR
, MDBS
| MIE
);
216 case RCAR_BUS_PHASE_STOP
:
217 rcar_i2c_write(priv
, ICMCR
, MDBS
| MIE
| FSB
);
225 static int rcar_i2c_clock_calculate(struct rcar_i2c_priv
*priv
,
229 struct clk
*clkp
= clk_get(NULL
, "peripheral_clk");
236 dev_err(dev
, "there is no peripheral_clk\n");
240 switch (priv
->devtype
) {
248 dev_err(dev
, "device type error\n");
253 * calculate SCL clock
257 * ick = clkp / (1 + CDF)
258 * SCL = ick / (20 + SCGD * 8 + F[(ticf + tr + intd) * ick])
260 * ick : I2C internal clock < 20 MHz
261 * ticf : I2C SCL falling time = 35 ns here
262 * tr : I2C SCL rising time = 200 ns here
263 * intd : LSI internal delay = 50 ns here
264 * clkp : peripheral_clk
265 * F[] : integer up-valuation
267 for (cdf
= 0; cdf
< (1 << cdf_width
); cdf
++) {
268 ick
= clk_get_rate(clkp
) / (1 + cdf
);
272 dev_err(dev
, "there is no best CDF\n");
277 * it is impossible to calculate large scale
278 * number on u32. separate it
280 * F[(ticf + tr + intd) * ick]
281 * = F[(35 + 200 + 50)ns * ick]
282 * = F[285 * ick / 1000000000]
283 * = F[(ick / 1000000) * 285 / 1000]
285 round
= (ick
+ 500000) / 1000000 * 285;
286 round
= (round
+ 500) / 1000;
289 * SCL = ick / (20 + SCGD * 8 + F[(ticf + tr + intd) * ick])
291 * Calculation result (= SCL) should be less than
292 * bus_speed for hardware safety
294 for (scgd
= 0; scgd
< 0x40; scgd
++) {
295 scl
= ick
/ (20 + (scgd
* 8) + round
);
296 if (scl
<= bus_speed
)
299 dev_err(dev
, "it is impossible to calculate best SCL\n");
303 dev_dbg(dev
, "clk %d/%d(%lu), round %u, CDF:0x%x, SCGD: 0x%x\n",
304 scl
, bus_speed
, clk_get_rate(clkp
), round
, cdf
, scgd
);
309 priv
->icccr
= (scgd
<< (cdf_width
) | cdf
);
314 static void rcar_i2c_clock_start(struct rcar_i2c_priv
*priv
)
316 rcar_i2c_write(priv
, ICCCR
, priv
->icccr
);
322 static u32
rcar_i2c_status_get(struct rcar_i2c_priv
*priv
)
324 return rcar_i2c_read(priv
, ICMSR
);
327 #define rcar_i2c_status_clear(priv) rcar_i2c_status_bit_clear(priv, 0xffffffff)
328 static void rcar_i2c_status_bit_clear(struct rcar_i2c_priv
*priv
, u32 bit
)
330 rcar_i2c_write(priv
, ICMSR
, ~bit
);
334 * recv/send functions
336 static int rcar_i2c_recv(struct rcar_i2c_priv
*priv
)
338 rcar_i2c_set_addr(priv
, 1);
339 rcar_i2c_status_clear(priv
);
340 rcar_i2c_bus_phase(priv
, RCAR_BUS_PHASE_ADDR
);
341 rcar_i2c_irq_mask(priv
, RCAR_IRQ_OPEN_FOR_RECV
);
346 static int rcar_i2c_send(struct rcar_i2c_priv
*priv
)
351 * It should check bus status when send case
353 ret
= rcar_i2c_bus_barrier(priv
);
357 rcar_i2c_set_addr(priv
, 0);
358 rcar_i2c_status_clear(priv
);
359 rcar_i2c_bus_phase(priv
, RCAR_BUS_PHASE_ADDR
);
360 rcar_i2c_irq_mask(priv
, RCAR_IRQ_OPEN_FOR_SEND
);
365 #define rcar_i2c_send_restart(priv) rcar_i2c_status_bit_clear(priv, (MAT | MDE))
366 #define rcar_i2c_recv_restart(priv) rcar_i2c_status_bit_clear(priv, (MAT | MDR))
369 * interrupt functions
371 static int rcar_i2c_irq_send(struct rcar_i2c_priv
*priv
, u32 msr
)
373 struct i2c_msg
*msg
= priv
->msg
;
377 * sometimes, unknown interrupt happened.
384 * If address transfer phase finished,
388 rcar_i2c_bus_phase(priv
, RCAR_BUS_PHASE_DATA
);
390 if (priv
->pos
< msg
->len
) {
392 * Prepare next data to ICRXTX register.
393 * This data will go to _SHIFT_ register.
396 * [ICRXTX] -> [SHIFT] -> [I2C bus]
398 rcar_i2c_write(priv
, ICRXTX
, msg
->buf
[priv
->pos
]);
403 * The last data was pushed to ICRXTX on _PREV_ empty irq.
404 * It is on _SHIFT_ register, and will sent to I2C bus.
407 * [ICRXTX] -> [SHIFT] -> [I2C bus]
410 if (priv
->flags
& ID_LAST_MSG
)
412 * If current msg is the _LAST_ msg,
413 * prepare stop condition here.
414 * ID_DONE will be set on STOP irq.
416 rcar_i2c_bus_phase(priv
, RCAR_BUS_PHASE_STOP
);
419 * If current msg is _NOT_ last msg,
420 * it doesn't call stop phase.
421 * thus, there is no STOP irq.
422 * return ID_DONE here.
427 rcar_i2c_send_restart(priv
);
432 static int rcar_i2c_irq_recv(struct rcar_i2c_priv
*priv
, u32 msr
)
434 struct i2c_msg
*msg
= priv
->msg
;
438 * sometimes, unknown interrupt happened.
446 * Address transfer phase finished,
447 * but, there is no data at this point.
450 } else if (priv
->pos
< msg
->len
) {
454 msg
->buf
[priv
->pos
] = rcar_i2c_read(priv
, ICRXTX
);
459 * If next received data is the _LAST_,
461 * otherwise, go to DATA phase.
463 if (priv
->pos
+ 1 >= msg
->len
)
464 rcar_i2c_bus_phase(priv
, RCAR_BUS_PHASE_STOP
);
466 rcar_i2c_bus_phase(priv
, RCAR_BUS_PHASE_DATA
);
468 rcar_i2c_recv_restart(priv
);
473 static irqreturn_t
rcar_i2c_irq(int irq
, void *ptr
)
475 struct rcar_i2c_priv
*priv
= ptr
;
476 struct device
*dev
= rcar_i2c_priv_to_dev(priv
);
479 /*-------------- spin lock -----------------*/
480 spin_lock(&priv
->lock
);
482 msr
= rcar_i2c_status_get(priv
);
491 * When arbitration lost, device become _slave_ mode.
493 dev_dbg(dev
, "Arbitration Lost\n");
494 rcar_i2c_flags_set(priv
, (ID_DONE
| ID_ARBLOST
));
502 dev_dbg(dev
, "Stop\n");
503 rcar_i2c_flags_set(priv
, ID_DONE
);
511 dev_dbg(dev
, "Nack\n");
513 /* go to stop phase */
514 rcar_i2c_bus_phase(priv
, RCAR_BUS_PHASE_STOP
);
515 rcar_i2c_irq_mask(priv
, RCAR_IRQ_OPEN_FOR_STOP
);
516 rcar_i2c_flags_set(priv
, ID_NACK
);
523 if (rcar_i2c_is_recv(priv
))
524 rcar_i2c_flags_set(priv
, rcar_i2c_irq_recv(priv
, msr
));
526 rcar_i2c_flags_set(priv
, rcar_i2c_irq_send(priv
, msr
));
529 if (rcar_i2c_flags_has(priv
, ID_DONE
)) {
530 rcar_i2c_irq_mask(priv
, RCAR_IRQ_CLOSE
);
531 rcar_i2c_status_clear(priv
);
532 wake_up(&priv
->wait
);
535 spin_unlock(&priv
->lock
);
536 /*-------------- spin unlock -----------------*/
541 static int rcar_i2c_master_xfer(struct i2c_adapter
*adap
,
542 struct i2c_msg
*msgs
,
545 struct rcar_i2c_priv
*priv
= i2c_get_adapdata(adap
);
546 struct device
*dev
= rcar_i2c_priv_to_dev(priv
);
550 pm_runtime_get_sync(dev
);
552 /*-------------- spin lock -----------------*/
553 spin_lock_irqsave(&priv
->lock
, flags
);
556 rcar_i2c_clock_start(priv
);
558 spin_unlock_irqrestore(&priv
->lock
, flags
);
559 /*-------------- spin unlock -----------------*/
562 for (i
= 0; i
< num
; i
++) {
563 /* This HW can't send STOP after address phase */
564 if (msgs
[i
].len
== 0) {
569 /*-------------- spin lock -----------------*/
570 spin_lock_irqsave(&priv
->lock
, flags
);
573 priv
->msg
= &msgs
[i
];
576 if (priv
->msg
== &msgs
[num
- 1])
577 rcar_i2c_flags_set(priv
, ID_LAST_MSG
);
579 /* start send/recv */
580 if (rcar_i2c_is_recv(priv
))
581 ret
= rcar_i2c_recv(priv
);
583 ret
= rcar_i2c_send(priv
);
585 spin_unlock_irqrestore(&priv
->lock
, flags
);
586 /*-------------- spin unlock -----------------*/
594 timeout
= wait_event_timeout(priv
->wait
,
595 rcar_i2c_flags_has(priv
, ID_DONE
),
605 if (rcar_i2c_flags_has(priv
, ID_NACK
)) {
610 if (rcar_i2c_flags_has(priv
, ID_ARBLOST
)) {
615 if (rcar_i2c_flags_has(priv
, ID_IOERROR
)) {
620 ret
= i
+ 1; /* The number of transfer */
626 dev_err(dev
, "error %d : %x\n", ret
, priv
->flags
);
631 static u32
rcar_i2c_func(struct i2c_adapter
*adap
)
633 /* This HW can't do SMBUS_QUICK and NOSTART */
634 return I2C_FUNC_I2C
| (I2C_FUNC_SMBUS_EMUL
& ~I2C_FUNC_SMBUS_QUICK
);
637 static const struct i2c_algorithm rcar_i2c_algo
= {
638 .master_xfer
= rcar_i2c_master_xfer
,
639 .functionality
= rcar_i2c_func
,
642 static int rcar_i2c_probe(struct platform_device
*pdev
)
644 struct i2c_rcar_platform_data
*pdata
= dev_get_platdata(&pdev
->dev
);
645 struct rcar_i2c_priv
*priv
;
646 struct i2c_adapter
*adap
;
647 struct resource
*res
;
648 struct device
*dev
= &pdev
->dev
;
652 priv
= devm_kzalloc(dev
, sizeof(struct rcar_i2c_priv
), GFP_KERNEL
);
654 dev_err(dev
, "no mem for private data\n");
658 bus_speed
= 100000; /* default 100 kHz */
659 if (pdata
&& pdata
->bus_speed
)
660 bus_speed
= pdata
->bus_speed
;
662 priv
->devtype
= platform_get_device_id(pdev
)->driver_data
;
664 ret
= rcar_i2c_clock_calculate(priv
, bus_speed
, dev
);
668 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
669 priv
->io
= devm_ioremap_resource(dev
, res
);
670 if (IS_ERR(priv
->io
))
671 return PTR_ERR(priv
->io
);
673 priv
->irq
= platform_get_irq(pdev
, 0);
674 init_waitqueue_head(&priv
->wait
);
675 spin_lock_init(&priv
->lock
);
679 adap
->algo
= &rcar_i2c_algo
;
680 adap
->class = I2C_CLASS_HWMON
| I2C_CLASS_SPD
;
682 adap
->dev
.parent
= dev
;
683 i2c_set_adapdata(adap
, priv
);
684 strlcpy(adap
->name
, pdev
->name
, sizeof(adap
->name
));
686 ret
= devm_request_irq(dev
, priv
->irq
, rcar_i2c_irq
, 0,
687 dev_name(dev
), priv
);
689 dev_err(dev
, "cannot get irq %d\n", priv
->irq
);
693 ret
= i2c_add_numbered_adapter(adap
);
695 dev_err(dev
, "reg adap failed: %d\n", ret
);
699 pm_runtime_enable(dev
);
700 platform_set_drvdata(pdev
, priv
);
702 dev_info(dev
, "probed\n");
707 static int rcar_i2c_remove(struct platform_device
*pdev
)
709 struct rcar_i2c_priv
*priv
= platform_get_drvdata(pdev
);
710 struct device
*dev
= &pdev
->dev
;
712 i2c_del_adapter(&priv
->adap
);
713 pm_runtime_disable(dev
);
718 static struct platform_device_id rcar_i2c_id_table
[] = {
719 { "i2c-rcar", I2C_RCAR_H1
},
720 { "i2c-rcar_h1", I2C_RCAR_H1
},
721 { "i2c-rcar_h2", I2C_RCAR_H2
},
724 MODULE_DEVICE_TABLE(platform
, rcar_i2c_id_table
);
726 static struct platform_driver rcar_i2c_driver
= {
729 .owner
= THIS_MODULE
,
731 .probe
= rcar_i2c_probe
,
732 .remove
= rcar_i2c_remove
,
733 .id_table
= rcar_i2c_id_table
,
736 module_platform_driver(rcar_i2c_driver
);
738 MODULE_LICENSE("GPL");
739 MODULE_DESCRIPTION("Renesas R-Car I2C bus driver");
740 MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");