2 * SuperH Mobile I2C Controller
4 * Copyright (C) 2008 Magnus Damm
6 * Portions of the code based on out-of-tree driver i2c-sh7343.c
7 * Copyright (c) 2006 Carlos Munoz <carlos@kenati.com>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <linux/kernel.h>
24 #include <linux/module.h>
25 #include <linux/init.h>
26 #include <linux/delay.h>
27 #include <linux/platform_device.h>
28 #include <linux/interrupt.h>
29 #include <linux/i2c.h>
30 #include <linux/err.h>
31 #include <linux/pm_runtime.h>
32 #include <linux/clk.h>
34 #include <linux/slab.h>
36 /* Transmit operation: */
46 /* BUS: S A8 ACK D8(1) ACK P */
47 /* IRQ: DTE WAIT WAIT */
53 /* BUS: S A8 ACK D8(1) ACK D8(2) ACK P */
54 /* IRQ: DTE WAIT WAIT WAIT */
57 /* ICDR: A8 D8(1) D8(2) */
59 /* 3 bytes or more, +---------+ gets repeated */
62 /* Receive operation: */
64 /* 0 byte receive - not supported since slave may hold SDA low */
66 /* 1 byte receive [TX] | [RX] */
67 /* BUS: S A8 ACK | D8(1) ACK P */
68 /* IRQ: DTE WAIT | WAIT DTE */
69 /* ICIC: -DTE | +DTE */
70 /* ICCR: 0x94 0x81 | 0xc0 */
71 /* ICDR: A8 | D8(1) */
73 /* 2 byte receive [TX]| [RX] */
74 /* BUS: S A8 ACK | D8(1) ACK D8(2) ACK P */
75 /* IRQ: DTE WAIT | WAIT WAIT DTE */
76 /* ICIC: -DTE | +DTE */
77 /* ICCR: 0x94 0x81 | 0xc0 */
78 /* ICDR: A8 | D8(1) D8(2) */
80 /* 3 byte receive [TX] | [RX] */
81 /* BUS: S A8 ACK | D8(1) ACK D8(2) ACK D8(3) ACK P */
82 /* IRQ: DTE WAIT | WAIT WAIT WAIT DTE */
83 /* ICIC: -DTE | +DTE */
84 /* ICCR: 0x94 0x81 | 0xc0 */
85 /* ICDR: A8 | D8(1) D8(2) D8(3) */
87 /* 4 bytes or more, this part is repeated +---------+ */
90 /* Interrupt order and BUSY flag */
92 /* SDA ___\___XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXAAAAAAAAA___/ */
93 /* SCL \_/1\_/2\_/3\_/4\_/5\_/6\_/7\_/8\___/9\_____/ */
95 /* S D7 D6 D5 D4 D3 D2 D1 D0 P */
97 /* WAIT IRQ ________________________________/ \___________ */
98 /* TACK IRQ ____________________________________/ \_______ */
99 /* DTE IRQ __________________________________________/ \_ */
100 /* AL IRQ XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX */
101 /* _______________________________________________ */
105 enum sh_mobile_i2c_op
{
116 struct sh_mobile_i2c_data
{
119 struct i2c_adapter adap
;
128 wait_queue_head_t wait
;
134 #define IIC_FLAG_HAS_ICIC67 (1 << 0)
136 #define NORMAL_SPEED 100000 /* FAST_SPEED 400000 */
138 /* Register offsets */
147 #define ICCR_ICE 0x80
148 #define ICCR_RACK 0x40
149 #define ICCR_TRS 0x10
150 #define ICCR_BBSY 0x04
151 #define ICCR_SCP 0x01
153 #define ICSR_SCLM 0x80
154 #define ICSR_SDAM 0x40
156 #define ICSR_BUSY 0x10
158 #define ICSR_TACK 0x04
159 #define ICSR_WAIT 0x02
160 #define ICSR_DTE 0x01
162 #define ICIC_ICCLB8 0x80
163 #define ICIC_ICCHB8 0x40
164 #define ICIC_ALE 0x08
165 #define ICIC_TACKE 0x04
166 #define ICIC_WAITE 0x02
167 #define ICIC_DTEE 0x01
169 static void iic_wr(struct sh_mobile_i2c_data
*pd
, int offs
, unsigned char data
)
174 iowrite8(data
, pd
->reg
+ offs
);
177 static unsigned char iic_rd(struct sh_mobile_i2c_data
*pd
, int offs
)
179 return ioread8(pd
->reg
+ offs
);
182 static void iic_set_clr(struct sh_mobile_i2c_data
*pd
, int offs
,
183 unsigned char set
, unsigned char clr
)
185 iic_wr(pd
, offs
, (iic_rd(pd
, offs
) | set
) & ~clr
);
188 static void activate_ch(struct sh_mobile_i2c_data
*pd
)
190 unsigned long i2c_clk
;
195 /* Wake up device and enable clock */
196 pm_runtime_get_sync(pd
->dev
);
199 /* Get clock rate after clock is enabled */
200 i2c_clk
= clk_get_rate(pd
->clk
);
202 /* Calculate the value for iccl. From the data sheet:
203 * iccl = (p clock / transfer rate) * (L / (L + H))
204 * where L and H are the SCL low/high ratio (5/4 in this case).
205 * We also round off the result.
208 denom
= NORMAL_SPEED
* 9;
209 tmp
= num
* 10 / denom
;
211 pd
->iccl
= (u_int8_t
)((num
/denom
) + 1);
213 pd
->iccl
= (u_int8_t
)(num
/denom
);
215 /* one more bit of ICCL in ICIC */
216 if (pd
->flags
& IIC_FLAG_HAS_ICIC67
) {
217 if ((num
/denom
) > 0xff)
218 pd
->icic
|= ICIC_ICCLB8
;
220 pd
->icic
&= ~ICIC_ICCLB8
;
223 /* Calculate the value for icch. From the data sheet:
224 icch = (p clock / transfer rate) * (H / (L + H)) */
226 tmp
= num
* 10 / denom
;
228 pd
->icch
= (u_int8_t
)((num
/denom
) + 1);
230 pd
->icch
= (u_int8_t
)(num
/denom
);
232 /* one more bit of ICCH in ICIC */
233 if (pd
->flags
& IIC_FLAG_HAS_ICIC67
) {
234 if ((num
/denom
) > 0xff)
235 pd
->icic
|= ICIC_ICCHB8
;
237 pd
->icic
&= ~ICIC_ICCHB8
;
240 /* Enable channel and configure rx ack */
241 iic_set_clr(pd
, ICCR
, ICCR_ICE
, 0);
243 /* Mask all interrupts */
247 iic_wr(pd
, ICCL
, pd
->iccl
);
248 iic_wr(pd
, ICCH
, pd
->icch
);
251 static void deactivate_ch(struct sh_mobile_i2c_data
*pd
)
253 /* Clear/disable interrupts */
257 /* Disable channel */
258 iic_set_clr(pd
, ICCR
, 0, ICCR_ICE
);
260 /* Disable clock and mark device as idle */
261 clk_disable(pd
->clk
);
262 pm_runtime_put_sync(pd
->dev
);
265 static unsigned char i2c_op(struct sh_mobile_i2c_data
*pd
,
266 enum sh_mobile_i2c_op op
, unsigned char data
)
268 unsigned char ret
= 0;
271 dev_dbg(pd
->dev
, "op %d, data in 0x%02x\n", op
, data
);
273 spin_lock_irqsave(&pd
->lock
, flags
);
276 case OP_START
: /* issue start and trigger DTE interrupt */
277 iic_wr(pd
, ICCR
, 0x94);
279 case OP_TX_FIRST
: /* disable DTE interrupt and write data */
280 iic_wr(pd
, ICIC
, ICIC_WAITE
| ICIC_ALE
| ICIC_TACKE
);
281 iic_wr(pd
, ICDR
, data
);
283 case OP_TX
: /* write data */
284 iic_wr(pd
, ICDR
, data
);
286 case OP_TX_STOP
: /* write data and issue a stop afterwards */
287 iic_wr(pd
, ICDR
, data
);
288 iic_wr(pd
, ICCR
, 0x90);
290 case OP_TX_TO_RX
: /* select read mode */
291 iic_wr(pd
, ICCR
, 0x81);
293 case OP_RX
: /* just read data */
294 ret
= iic_rd(pd
, ICDR
);
296 case OP_RX_STOP
: /* enable DTE interrupt, issue stop */
298 ICIC_DTEE
| ICIC_WAITE
| ICIC_ALE
| ICIC_TACKE
);
299 iic_wr(pd
, ICCR
, 0xc0);
301 case OP_RX_STOP_DATA
: /* enable DTE interrupt, read data, issue stop */
303 ICIC_DTEE
| ICIC_WAITE
| ICIC_ALE
| ICIC_TACKE
);
304 ret
= iic_rd(pd
, ICDR
);
305 iic_wr(pd
, ICCR
, 0xc0);
309 spin_unlock_irqrestore(&pd
->lock
, flags
);
311 dev_dbg(pd
->dev
, "op %d, data out 0x%02x\n", op
, ret
);
315 static int sh_mobile_i2c_is_first_byte(struct sh_mobile_i2c_data
*pd
)
323 static int sh_mobile_i2c_is_last_byte(struct sh_mobile_i2c_data
*pd
)
325 if (pd
->pos
== (pd
->msg
->len
- 1))
331 static void sh_mobile_i2c_get_data(struct sh_mobile_i2c_data
*pd
,
336 *buf
= (pd
->msg
->addr
& 0x7f) << 1;
337 *buf
|= (pd
->msg
->flags
& I2C_M_RD
) ? 1 : 0;
340 *buf
= pd
->msg
->buf
[pd
->pos
];
344 static int sh_mobile_i2c_isr_tx(struct sh_mobile_i2c_data
*pd
)
348 if (pd
->pos
== pd
->msg
->len
)
351 sh_mobile_i2c_get_data(pd
, &data
);
353 if (sh_mobile_i2c_is_last_byte(pd
))
354 i2c_op(pd
, OP_TX_STOP
, data
);
355 else if (sh_mobile_i2c_is_first_byte(pd
))
356 i2c_op(pd
, OP_TX_FIRST
, data
);
358 i2c_op(pd
, OP_TX
, data
);
364 static int sh_mobile_i2c_isr_rx(struct sh_mobile_i2c_data
*pd
)
371 sh_mobile_i2c_get_data(pd
, &data
);
373 if (sh_mobile_i2c_is_first_byte(pd
))
374 i2c_op(pd
, OP_TX_FIRST
, data
);
376 i2c_op(pd
, OP_TX
, data
);
381 i2c_op(pd
, OP_TX_TO_RX
, 0);
385 real_pos
= pd
->pos
- 2;
387 if (pd
->pos
== pd
->msg
->len
) {
389 i2c_op(pd
, OP_RX_STOP
, 0);
392 data
= i2c_op(pd
, OP_RX_STOP_DATA
, 0);
394 data
= i2c_op(pd
, OP_RX
, 0);
397 pd
->msg
->buf
[real_pos
] = data
;
401 return pd
->pos
== (pd
->msg
->len
+ 2);
404 static irqreturn_t
sh_mobile_i2c_isr(int irq
, void *dev_id
)
406 struct platform_device
*dev
= dev_id
;
407 struct sh_mobile_i2c_data
*pd
= platform_get_drvdata(dev
);
411 sr
= iic_rd(pd
, ICSR
);
412 pd
->sr
|= sr
; /* remember state */
414 dev_dbg(pd
->dev
, "i2c_isr 0x%02x 0x%02x %s %d %d!\n", sr
, pd
->sr
,
415 (pd
->msg
->flags
& I2C_M_RD
) ? "read" : "write",
416 pd
->pos
, pd
->msg
->len
);
418 if (sr
& (ICSR_AL
| ICSR_TACK
)) {
419 /* don't interrupt transaction - continue to issue stop */
420 iic_wr(pd
, ICSR
, sr
& ~(ICSR_AL
| ICSR_TACK
));
422 } else if (pd
->msg
->flags
& I2C_M_RD
)
423 wakeup
= sh_mobile_i2c_isr_rx(pd
);
425 wakeup
= sh_mobile_i2c_isr_tx(pd
);
427 if (sr
& ICSR_WAIT
) /* TODO: add delay here to support slow acks */
428 iic_wr(pd
, ICSR
, sr
& ~ICSR_WAIT
);
438 static int start_ch(struct sh_mobile_i2c_data
*pd
, struct i2c_msg
*usr_msg
)
440 if (usr_msg
->len
== 0 && (usr_msg
->flags
& I2C_M_RD
)) {
441 dev_err(pd
->dev
, "Unsupported zero length i2c read\n");
445 /* Initialize channel registers */
446 iic_set_clr(pd
, ICCR
, 0, ICCR_ICE
);
448 /* Enable channel and configure rx ack */
449 iic_set_clr(pd
, ICCR
, ICCR_ICE
, 0);
452 iic_wr(pd
, ICCL
, pd
->iccl
);
453 iic_wr(pd
, ICCH
, pd
->icch
);
459 /* Enable all interrupts to begin with */
460 iic_wr(pd
, ICIC
, ICIC_DTEE
| ICIC_WAITE
| ICIC_ALE
| ICIC_TACKE
);
464 static int sh_mobile_i2c_xfer(struct i2c_adapter
*adapter
,
465 struct i2c_msg
*msgs
,
468 struct sh_mobile_i2c_data
*pd
= i2c_get_adapdata(adapter
);
472 int i
, k
, retry_count
;
476 /* Process all messages */
477 for (i
= 0; i
< num
; i
++) {
480 err
= start_ch(pd
, msg
);
484 i2c_op(pd
, OP_START
, 0);
486 /* The interrupt handler takes care of the rest... */
487 k
= wait_event_timeout(pd
->wait
,
488 pd
->sr
& (ICSR_TACK
| SW_DONE
),
491 dev_err(pd
->dev
, "Transfer request timed out\n");
495 val
= iic_rd(pd
, ICSR
);
497 dev_dbg(pd
->dev
, "val 0x%02x pd->sr 0x%02x\n", val
, pd
->sr
);
499 /* the interrupt handler may wake us up before the
500 * transfer is finished, so poll the hardware
503 if (val
& ICSR_BUSY
) {
509 dev_err(pd
->dev
, "Polling timed out\n");
513 /* handle missing acknowledge and arbitration lost */
514 if ((val
| pd
->sr
) & (ICSR_TACK
| ICSR_AL
)) {
527 static u32
sh_mobile_i2c_func(struct i2c_adapter
*adapter
)
529 return I2C_FUNC_I2C
| I2C_FUNC_SMBUS_EMUL
;
532 static struct i2c_algorithm sh_mobile_i2c_algorithm
= {
533 .functionality
= sh_mobile_i2c_func
,
534 .master_xfer
= sh_mobile_i2c_xfer
,
537 static int sh_mobile_i2c_hook_irqs(struct platform_device
*dev
, int hook
)
539 struct resource
*res
;
543 while ((res
= platform_get_resource(dev
, IORESOURCE_IRQ
, k
))) {
544 for (n
= res
->start
; hook
&& n
<= res
->end
; n
++) {
545 if (request_irq(n
, sh_mobile_i2c_isr
, IRQF_DISABLED
,
546 dev_name(&dev
->dev
), dev
)) {
547 for (n
--; n
>= res
->start
; n
--)
557 return k
> 0 ? 0 : -ENOENT
;
565 res
= platform_get_resource(dev
, IORESOURCE_IRQ
, k
);
566 for (n
= res
->start
; n
<= res
->end
; n
++)
575 static int sh_mobile_i2c_probe(struct platform_device
*dev
)
577 struct sh_mobile_i2c_data
*pd
;
578 struct i2c_adapter
*adap
;
579 struct resource
*res
;
584 pd
= kzalloc(sizeof(struct sh_mobile_i2c_data
), GFP_KERNEL
);
586 dev_err(&dev
->dev
, "cannot allocate private data\n");
590 snprintf(clk_name
, sizeof(clk_name
), "i2c%d", dev
->id
);
591 pd
->clk
= clk_get(&dev
->dev
, clk_name
);
592 if (IS_ERR(pd
->clk
)) {
593 dev_err(&dev
->dev
, "cannot get clock \"%s\"\n", clk_name
);
594 ret
= PTR_ERR(pd
->clk
);
598 ret
= sh_mobile_i2c_hook_irqs(dev
, 1);
600 dev_err(&dev
->dev
, "cannot request IRQ\n");
605 platform_set_drvdata(dev
, pd
);
607 res
= platform_get_resource(dev
, IORESOURCE_MEM
, 0);
609 dev_err(&dev
->dev
, "cannot find IO resource\n");
614 size
= resource_size(res
);
616 pd
->reg
= ioremap(res
->start
, size
);
617 if (pd
->reg
== NULL
) {
618 dev_err(&dev
->dev
, "cannot map IO\n");
623 /* The IIC blocks on SH-Mobile ARM processors
624 * come with two new bits in ICIC.
627 pd
->flags
|= IIC_FLAG_HAS_ICIC67
;
629 /* Enable Runtime PM for this device.
631 * Also tell the Runtime PM core to ignore children
632 * for this device since it is valid for us to suspend
633 * this I2C master driver even though the slave devices
634 * on the I2C bus may not be suspended.
636 * The state of the I2C hardware bus is unaffected by
637 * the Runtime PM state.
639 pm_suspend_ignore_children(&dev
->dev
, true);
640 pm_runtime_enable(&dev
->dev
);
642 /* setup the private data */
644 i2c_set_adapdata(adap
, pd
);
646 adap
->owner
= THIS_MODULE
;
647 adap
->algo
= &sh_mobile_i2c_algorithm
;
648 adap
->dev
.parent
= &dev
->dev
;
652 strlcpy(adap
->name
, dev
->name
, sizeof(adap
->name
));
654 spin_lock_init(&pd
->lock
);
655 init_waitqueue_head(&pd
->wait
);
657 ret
= i2c_add_numbered_adapter(adap
);
659 dev_err(&dev
->dev
, "cannot add numbered adapter\n");
668 sh_mobile_i2c_hook_irqs(dev
, 0);
676 static int sh_mobile_i2c_remove(struct platform_device
*dev
)
678 struct sh_mobile_i2c_data
*pd
= platform_get_drvdata(dev
);
680 i2c_del_adapter(&pd
->adap
);
682 sh_mobile_i2c_hook_irqs(dev
, 0);
684 pm_runtime_disable(&dev
->dev
);
689 static int sh_mobile_i2c_runtime_nop(struct device
*dev
)
691 /* Runtime PM callback shared between ->runtime_suspend()
692 * and ->runtime_resume(). Simply returns success.
694 * This driver re-initializes all registers after
695 * pm_runtime_get_sync() anyway so there is no need
696 * to save and restore registers here.
701 static const struct dev_pm_ops sh_mobile_i2c_dev_pm_ops
= {
702 .runtime_suspend
= sh_mobile_i2c_runtime_nop
,
703 .runtime_resume
= sh_mobile_i2c_runtime_nop
,
706 static struct platform_driver sh_mobile_i2c_driver
= {
708 .name
= "i2c-sh_mobile",
709 .owner
= THIS_MODULE
,
710 .pm
= &sh_mobile_i2c_dev_pm_ops
,
712 .probe
= sh_mobile_i2c_probe
,
713 .remove
= sh_mobile_i2c_remove
,
716 static int __init
sh_mobile_i2c_adap_init(void)
718 return platform_driver_register(&sh_mobile_i2c_driver
);
721 static void __exit
sh_mobile_i2c_adap_exit(void)
723 platform_driver_unregister(&sh_mobile_i2c_driver
);
726 subsys_initcall(sh_mobile_i2c_adap_init
);
727 module_exit(sh_mobile_i2c_adap_exit
);
729 MODULE_DESCRIPTION("SuperH Mobile I2C Bus Controller driver");
730 MODULE_AUTHOR("Magnus Damm");
731 MODULE_LICENSE("GPL v2");