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/clk.h>
34 enum sh_mobile_i2c_op
{
43 struct sh_mobile_i2c_data
{
46 struct i2c_adapter adap
;
53 wait_queue_head_t wait
;
59 #define NORMAL_SPEED 100000 /* FAST_SPEED 400000 */
61 /* Register offsets */
62 #define ICDR(pd) (pd->reg + 0x00)
63 #define ICCR(pd) (pd->reg + 0x04)
64 #define ICSR(pd) (pd->reg + 0x08)
65 #define ICIC(pd) (pd->reg + 0x0c)
66 #define ICCL(pd) (pd->reg + 0x10)
67 #define ICCH(pd) (pd->reg + 0x14)
71 #define ICCR_RACK 0x40
73 #define ICCR_BBSY 0x04
76 #define ICSR_SCLM 0x80
77 #define ICSR_SDAM 0x40
79 #define ICSR_BUSY 0x10
81 #define ICSR_TACK 0x04
82 #define ICSR_WAIT 0x02
86 #define ICIC_TACKE 0x04
87 #define ICIC_WAITE 0x02
88 #define ICIC_DTEE 0x01
90 static void activate_ch(struct sh_mobile_i2c_data
*pd
)
92 /* Make sure the clock is enabled */
95 /* Enable channel and configure rx ack */
96 iowrite8(ioread8(ICCR(pd
)) | ICCR_ICE
, ICCR(pd
));
98 /* Mask all interrupts */
99 iowrite8(0, ICIC(pd
));
102 iowrite8(pd
->iccl
, ICCL(pd
));
103 iowrite8(pd
->icch
, ICCH(pd
));
106 static void deactivate_ch(struct sh_mobile_i2c_data
*pd
)
108 /* Clear/disable interrupts */
109 iowrite8(0, ICSR(pd
));
110 iowrite8(0, ICIC(pd
));
112 /* Disable channel */
113 iowrite8(ioread8(ICCR(pd
)) & ~ICCR_ICE
, ICCR(pd
));
116 clk_disable(pd
->clk
);
119 static unsigned char i2c_op(struct sh_mobile_i2c_data
*pd
,
120 enum sh_mobile_i2c_op op
, unsigned char data
)
122 unsigned char ret
= 0;
125 dev_dbg(pd
->dev
, "op %d, data in 0x%02x\n", op
, data
);
127 spin_lock_irqsave(&pd
->lock
, flags
);
131 iowrite8(0x94, ICCR(pd
));
134 iowrite8(data
, ICDR(pd
));
137 iowrite8(data
, ICDR(pd
));
138 iowrite8(0x90, ICCR(pd
));
139 iowrite8(ICIC_ALE
| ICIC_TACKE
, ICIC(pd
));
142 iowrite8(data
, ICDR(pd
));
143 iowrite8(0x81, ICCR(pd
));
146 ret
= ioread8(ICDR(pd
));
149 ret
= ioread8(ICDR(pd
));
150 iowrite8(0xc0, ICCR(pd
));
154 spin_unlock_irqrestore(&pd
->lock
, flags
);
156 dev_dbg(pd
->dev
, "op %d, data out 0x%02x\n", op
, ret
);
160 static irqreturn_t
sh_mobile_i2c_isr(int irq
, void *dev_id
)
162 struct platform_device
*dev
= dev_id
;
163 struct sh_mobile_i2c_data
*pd
= platform_get_drvdata(dev
);
164 struct i2c_msg
*msg
= pd
->msg
;
165 unsigned char data
, sr
;
168 sr
= ioread8(ICSR(pd
));
171 dev_dbg(pd
->dev
, "i2c_isr 0x%02x 0x%02x %s %d %d!\n", sr
, pd
->sr
,
172 (msg
->flags
& I2C_M_RD
) ? "read" : "write",
175 if (sr
& (ICSR_AL
| ICSR_TACK
)) {
176 iowrite8(0, ICIC(pd
)); /* disable interrupts */
181 if (pd
->pos
== msg
->len
) {
182 i2c_op(pd
, OP_RX_ONLY
, 0);
188 data
= (msg
->addr
& 0x7f) << 1;
189 data
|= (msg
->flags
& I2C_M_RD
) ? 1 : 0;
191 data
= msg
->buf
[pd
->pos
];
193 if ((pd
->pos
== -1) || !(msg
->flags
& I2C_M_RD
)) {
194 if (msg
->flags
& I2C_M_RD
)
195 i2c_op(pd
, OP_TX_TO_RX
, data
);
196 else if (pd
->pos
== (msg
->len
- 1)) {
197 i2c_op(pd
, OP_TX_STOP
, data
);
200 i2c_op(pd
, OP_TX_ONLY
, data
);
202 if (pd
->pos
== (msg
->len
- 1))
203 data
= i2c_op(pd
, OP_RX_STOP
, 0);
205 data
= i2c_op(pd
, OP_RX_ONLY
, 0);
207 msg
->buf
[pd
->pos
] = data
;
220 static int start_ch(struct sh_mobile_i2c_data
*pd
, struct i2c_msg
*usr_msg
)
222 /* Initialize channel registers */
223 iowrite8(ioread8(ICCR(pd
)) & ~ICCR_ICE
, ICCR(pd
));
225 /* Enable channel and configure rx ack */
226 iowrite8(ioread8(ICCR(pd
)) | ICCR_ICE
, ICCR(pd
));
229 iowrite8(pd
->iccl
, ICCL(pd
));
230 iowrite8(pd
->icch
, ICCH(pd
));
236 /* Enable all interrupts except wait */
237 iowrite8(ioread8(ICIC(pd
)) | ICIC_ALE
| ICIC_TACKE
| ICIC_DTEE
,
242 static int sh_mobile_i2c_xfer(struct i2c_adapter
*adapter
,
243 struct i2c_msg
*msgs
,
246 struct sh_mobile_i2c_data
*pd
= i2c_get_adapdata(adapter
);
250 int i
, k
, retry_count
;
254 /* Process all messages */
255 for (i
= 0; i
< num
; i
++) {
258 err
= start_ch(pd
, msg
);
262 i2c_op(pd
, OP_START
, 0);
264 /* The interrupt handler takes care of the rest... */
265 k
= wait_event_timeout(pd
->wait
,
266 pd
->sr
& (ICSR_TACK
| SW_DONE
),
269 dev_err(pd
->dev
, "Transfer request timed out\n");
273 val
= ioread8(ICSR(pd
));
275 dev_dbg(pd
->dev
, "val 0x%02x pd->sr 0x%02x\n", val
, pd
->sr
);
277 if ((val
| pd
->sr
) & (ICSR_TACK
| ICSR_AL
)) {
282 /* the interrupt handler may wake us up before the
283 * transfer is finished, so poll the hardware
287 if (!(!(val
& ICSR_BUSY
) && (val
& ICSR_SCLM
) &&
288 (val
& ICSR_SDAM
))) {
294 dev_err(pd
->dev
, "Polling timed out\n");
306 static u32
sh_mobile_i2c_func(struct i2c_adapter
*adapter
)
308 return I2C_FUNC_I2C
| I2C_FUNC_SMBUS_EMUL
;
311 static struct i2c_algorithm sh_mobile_i2c_algorithm
= {
312 .functionality
= sh_mobile_i2c_func
,
313 .master_xfer
= sh_mobile_i2c_xfer
,
316 static void sh_mobile_i2c_setup_channel(struct platform_device
*dev
)
318 struct sh_mobile_i2c_data
*pd
= platform_get_drvdata(dev
);
319 unsigned long peripheral_clk
= clk_get_rate(pd
->clk
);
324 spin_lock_init(&pd
->lock
);
325 init_waitqueue_head(&pd
->wait
);
327 /* Calculate the value for iccl. From the data sheet:
328 * iccl = (p clock / transfer rate) * (L / (L + H))
329 * where L and H are the SCL low/high ratio (5/4 in this case).
330 * We also round off the result.
332 num
= peripheral_clk
* 5;
333 denom
= NORMAL_SPEED
* 9;
334 tmp
= num
* 10 / denom
;
336 pd
->iccl
= (u_int8_t
)((num
/denom
) + 1);
338 pd
->iccl
= (u_int8_t
)(num
/denom
);
340 /* Calculate the value for icch. From the data sheet:
341 icch = (p clock / transfer rate) * (H / (L + H)) */
342 num
= peripheral_clk
* 4;
343 tmp
= num
* 10 / denom
;
345 pd
->icch
= (u_int8_t
)((num
/denom
) + 1);
347 pd
->icch
= (u_int8_t
)(num
/denom
);
350 static int sh_mobile_i2c_hook_irqs(struct platform_device
*dev
, int hook
)
352 struct resource
*res
;
358 while ((res
= platform_get_resource(dev
, IORESOURCE_IRQ
, k
))) {
359 for (n
= res
->start
; hook
&& n
<= res
->end
; n
++) {
360 if (request_irq(n
, sh_mobile_i2c_isr
, IRQF_DISABLED
,
361 dev
->dev
.bus_id
, dev
))
368 return k
> 0 ? 0 : -ENOENT
;
374 for (q
= k
; k
>= 0; k
--) {
375 for (m
= n
; m
>= res
->start
; m
--)
378 res
= platform_get_resource(dev
, IORESOURCE_IRQ
, k
- 1);
385 static int sh_mobile_i2c_probe(struct platform_device
*dev
)
387 struct sh_mobile_i2c_data
*pd
;
388 struct i2c_adapter
*adap
;
389 struct resource
*res
;
393 pd
= kzalloc(sizeof(struct sh_mobile_i2c_data
), GFP_KERNEL
);
395 dev_err(&dev
->dev
, "cannot allocate private data\n");
399 pd
->clk
= clk_get(&dev
->dev
, "peripheral_clk");
400 if (IS_ERR(pd
->clk
)) {
401 dev_err(&dev
->dev
, "cannot get peripheral clock\n");
402 ret
= PTR_ERR(pd
->clk
);
406 ret
= sh_mobile_i2c_hook_irqs(dev
, 1);
408 dev_err(&dev
->dev
, "cannot request IRQ\n");
413 platform_set_drvdata(dev
, pd
);
415 res
= platform_get_resource(dev
, IORESOURCE_MEM
, 0);
417 dev_err(&dev
->dev
, "cannot find IO resource\n");
422 size
= (res
->end
- res
->start
) + 1;
424 pd
->reg
= ioremap(res
->start
, size
);
425 if (pd
->reg
== NULL
) {
426 dev_err(&dev
->dev
, "cannot map IO\n");
431 /* setup the private data */
433 i2c_set_adapdata(adap
, pd
);
435 adap
->owner
= THIS_MODULE
;
436 adap
->algo
= &sh_mobile_i2c_algorithm
;
437 adap
->dev
.parent
= &dev
->dev
;
441 strlcpy(adap
->name
, dev
->name
, sizeof(adap
->name
));
443 sh_mobile_i2c_setup_channel(dev
);
445 ret
= i2c_add_numbered_adapter(adap
);
447 dev_err(&dev
->dev
, "cannot add numbered adapter\n");
456 sh_mobile_i2c_hook_irqs(dev
, 0);
464 static int sh_mobile_i2c_remove(struct platform_device
*dev
)
466 struct sh_mobile_i2c_data
*pd
= platform_get_drvdata(dev
);
468 i2c_del_adapter(&pd
->adap
);
470 sh_mobile_i2c_hook_irqs(dev
, 0);
476 static struct platform_driver sh_mobile_i2c_driver
= {
478 .name
= "i2c-sh_mobile",
479 .owner
= THIS_MODULE
,
481 .probe
= sh_mobile_i2c_probe
,
482 .remove
= sh_mobile_i2c_remove
,
485 static int __init
sh_mobile_i2c_adap_init(void)
487 return platform_driver_register(&sh_mobile_i2c_driver
);
490 static void __exit
sh_mobile_i2c_adap_exit(void)
492 platform_driver_unregister(&sh_mobile_i2c_driver
);
495 module_init(sh_mobile_i2c_adap_init
);
496 module_exit(sh_mobile_i2c_adap_exit
);
498 MODULE_DESCRIPTION("SuperH Mobile I2C Bus Controller driver");
499 MODULE_AUTHOR("Magnus Damm");
500 MODULE_LICENSE("GPL v2");