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>
35 #include <linux/i2c/i2c-sh_mobile.h>
37 /* Transmit operation: */
40 /* BUS: S A8 ACK P(*) */
47 /* BUS: S A8 ACK D8(1) ACK P(*) */
48 /* IRQ: DTE WAIT WAIT */
54 /* BUS: S A8 ACK D8(1) ACK D8(2) ACK P(*) */
55 /* IRQ: DTE WAIT WAIT WAIT */
58 /* ICDR: A8 D8(1) D8(2) */
60 /* 3 bytes or more, +---------+ gets repeated */
63 /* Receive operation: */
65 /* 0 byte receive - not supported since slave may hold SDA low */
67 /* 1 byte receive [TX] | [RX] */
68 /* BUS: S A8 ACK | D8(1) ACK P(*) */
69 /* IRQ: DTE WAIT | WAIT DTE */
70 /* ICIC: -DTE | +DTE */
71 /* ICCR: 0x94 0x81 | 0xc0 */
72 /* ICDR: A8 | D8(1) */
74 /* 2 byte receive [TX]| [RX] */
75 /* BUS: S A8 ACK | D8(1) ACK D8(2) ACK P(*) */
76 /* IRQ: DTE WAIT | WAIT WAIT DTE */
77 /* ICIC: -DTE | +DTE */
78 /* ICCR: 0x94 0x81 | 0xc0 */
79 /* ICDR: A8 | D8(1) D8(2) */
81 /* 3 byte receive [TX] | [RX] (*) */
82 /* BUS: S A8 ACK | D8(1) ACK D8(2) ACK D8(3) ACK P */
83 /* IRQ: DTE WAIT | WAIT WAIT WAIT DTE */
84 /* ICIC: -DTE | +DTE */
85 /* ICCR: 0x94 0x81 | 0xc0 */
86 /* ICDR: A8 | D8(1) D8(2) D8(3) */
88 /* 4 bytes or more, this part is repeated +---------+ */
91 /* Interrupt order and BUSY flag */
93 /* SDA ___\___XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXAAAAAAAAA___/ */
94 /* SCL \_/1\_/2\_/3\_/4\_/5\_/6\_/7\_/8\___/9\_____/ */
96 /* S D7 D6 D5 D4 D3 D2 D1 D0 P(*) */
98 /* WAIT IRQ ________________________________/ \___________ */
99 /* TACK IRQ ____________________________________/ \_______ */
100 /* DTE IRQ __________________________________________/ \_ */
101 /* AL IRQ XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX */
102 /* _______________________________________________ */
105 /* (*) The STOP condition is only sent by the master at the end of the last */
106 /* I2C message or if the I2C_M_STOP flag is set. Similarly, the BUSY bit is */
107 /* only cleared after the STOP condition, so, between messages we have to */
108 /* poll for the DTE bit. */
111 enum sh_mobile_i2c_op {
122 struct sh_mobile_i2c_data {
125 struct i2c_adapter adap;
126 unsigned long bus_speed;
127 unsigned int clks_per_count;
135 wait_queue_head_t wait;
142 #define IIC_FLAG_HAS_ICIC67 (1 << 0)
144 #define STANDARD_MODE 100000
145 #define FAST_MODE 400000
147 /* Register offsets */
156 #define ICCR_ICE 0x80
157 #define ICCR_RACK 0x40
158 #define ICCR_TRS 0x10
159 #define ICCR_BBSY 0x04
160 #define ICCR_SCP 0x01
162 #define ICSR_SCLM 0x80
163 #define ICSR_SDAM 0x40
165 #define ICSR_BUSY 0x10
167 #define ICSR_TACK 0x04
168 #define ICSR_WAIT 0x02
169 #define ICSR_DTE 0x01
171 #define ICIC_ICCLB8 0x80
172 #define ICIC_ICCHB8 0x40
173 #define ICIC_ALE 0x08
174 #define ICIC_TACKE 0x04
175 #define ICIC_WAITE 0x02
176 #define ICIC_DTEE 0x01
178 static void iic_wr(struct sh_mobile_i2c_data *pd, int offs, unsigned char data)
183 iowrite8(data, pd->reg + offs);
186 static unsigned char iic_rd(struct sh_mobile_i2c_data *pd, int offs)
188 return ioread8(pd->reg + offs);
191 static void iic_set_clr(struct sh_mobile_i2c_data *pd, int offs,
192 unsigned char set, unsigned char clr)
194 iic_wr(pd, offs, (iic_rd(pd, offs) | set) & ~clr);
197 static u32 sh_mobile_i2c_iccl(unsigned long count_khz, u32 tLOW, u32 tf)
200 * Conditional expression:
201 * ICCL >= COUNT_CLK * (tLOW + tf)
203 * SH-Mobile IIC hardware starts counting the LOW period of
204 * the SCL signal (tLOW) as soon as it pulls the SCL line.
205 * In order to meet the tLOW timing spec, we need to take into
206 * account the fall time of SCL signal (tf). Default tf value
207 * should be 0.3 us, for safety.
209 return (((count_khz * (tLOW + tf)) + 5000) / 10000);
212 static u32 sh_mobile_i2c_icch(unsigned long count_khz, u32 tHIGH, u32 tf)
215 * Conditional expression:
216 * ICCH >= COUNT_CLK * (tHIGH + tf)
218 * SH-Mobile IIC hardware is aware of SCL transition period 'tr',
219 * and can ignore it. SH-Mobile IIC controller starts counting
220 * the HIGH period of the SCL signal (tHIGH) after the SCL input
221 * voltage increases at VIH.
223 * Afterward it turned out calculating ICCH using only tHIGH spec
224 * will result in violation of the tHD;STA timing spec. We need
225 * to take into account the fall time of SDA signal (tf) at START
226 * condition, in order to meet both tHIGH and tHD;STA specs.
228 return (((count_khz * (tHIGH + tf)) + 5000) / 10000);
231 static int sh_mobile_i2c_init(struct sh_mobile_i2c_data *pd)
233 unsigned long i2c_clk_khz;
236 /* Get clock rate after clock is enabled */
237 clk_prepare_enable(pd->clk);
238 i2c_clk_khz = clk_get_rate(pd->clk) / 1000;
239 clk_disable_unprepare(pd->clk);
240 i2c_clk_khz /= pd->clks_per_count;
242 if (pd->bus_speed == STANDARD_MODE) {
243 tLOW = 47; /* tLOW = 4.7 us */
244 tHIGH = 40; /* tHD;STA = tHIGH = 4.0 us */
245 tf = 3; /* tf = 0.3 us */
246 } else if (pd->bus_speed == FAST_MODE) {
247 tLOW = 13; /* tLOW = 1.3 us */
248 tHIGH = 6; /* tHD;STA = tHIGH = 0.6 us */
249 tf = 3; /* tf = 0.3 us */
251 dev_err(pd->dev, "unrecognized bus speed %lu Hz\n",
256 pd->iccl = sh_mobile_i2c_iccl(i2c_clk_khz, tLOW, tf);
257 /* one more bit of ICCL in ICIC */
258 if ((pd->iccl > 0xff) && (pd->flags & IIC_FLAG_HAS_ICIC67))
259 pd->icic |= ICIC_ICCLB8;
261 pd->icic &= ~ICIC_ICCLB8;
263 pd->icch = sh_mobile_i2c_icch(i2c_clk_khz, tHIGH, tf);
264 /* one more bit of ICCH in ICIC */
265 if ((pd->icch > 0xff) && (pd->flags & IIC_FLAG_HAS_ICIC67))
266 pd->icic |= ICIC_ICCHB8;
268 pd->icic &= ~ICIC_ICCHB8;
273 static void activate_ch(struct sh_mobile_i2c_data *pd)
275 /* Wake up device and enable clock */
276 pm_runtime_get_sync(pd->dev);
277 clk_prepare_enable(pd->clk);
279 /* Enable channel and configure rx ack */
280 iic_set_clr(pd, ICCR, ICCR_ICE, 0);
282 /* Mask all interrupts */
286 iic_wr(pd, ICCL, pd->iccl & 0xff);
287 iic_wr(pd, ICCH, pd->icch & 0xff);
290 static void deactivate_ch(struct sh_mobile_i2c_data *pd)
292 /* Clear/disable interrupts */
296 /* Disable channel */
297 iic_set_clr(pd, ICCR, 0, ICCR_ICE);
299 /* Disable clock and mark device as idle */
300 clk_disable_unprepare(pd->clk);
301 pm_runtime_put_sync(pd->dev);
304 static unsigned char i2c_op(struct sh_mobile_i2c_data *pd,
305 enum sh_mobile_i2c_op op, unsigned char data)
307 unsigned char ret = 0;
310 dev_dbg(pd->dev, "op %d, data in 0x%02x\n", op, data);
312 spin_lock_irqsave(&pd->lock, flags);
315 case OP_START: /* issue start and trigger DTE interrupt */
316 iic_wr(pd, ICCR, ICCR_ICE | ICCR_TRS | ICCR_BBSY);
318 case OP_TX_FIRST: /* disable DTE interrupt and write data */
319 iic_wr(pd, ICIC, ICIC_WAITE | ICIC_ALE | ICIC_TACKE);
320 iic_wr(pd, ICDR, data);
322 case OP_TX: /* write data */
323 iic_wr(pd, ICDR, data);
325 case OP_TX_STOP: /* write data and issue a stop afterwards */
326 iic_wr(pd, ICDR, data);
327 iic_wr(pd, ICCR, pd->send_stop ? ICCR_ICE | ICCR_TRS
328 : ICCR_ICE | ICCR_TRS | ICCR_BBSY);
330 case OP_TX_TO_RX: /* select read mode */
331 iic_wr(pd, ICCR, ICCR_ICE | ICCR_SCP);
333 case OP_RX: /* just read data */
334 ret = iic_rd(pd, ICDR);
336 case OP_RX_STOP: /* enable DTE interrupt, issue stop */
338 ICIC_DTEE | ICIC_WAITE | ICIC_ALE | ICIC_TACKE);
339 iic_wr(pd, ICCR, ICCR_ICE | ICCR_RACK);
341 case OP_RX_STOP_DATA: /* enable DTE interrupt, read data, issue stop */
343 ICIC_DTEE | ICIC_WAITE | ICIC_ALE | ICIC_TACKE);
344 ret = iic_rd(pd, ICDR);
345 iic_wr(pd, ICCR, ICCR_ICE | ICCR_RACK);
349 spin_unlock_irqrestore(&pd->lock, flags);
351 dev_dbg(pd->dev, "op %d, data out 0x%02x\n", op, ret);
355 static bool sh_mobile_i2c_is_first_byte(struct sh_mobile_i2c_data *pd)
357 return pd->pos == -1;
360 static bool sh_mobile_i2c_is_last_byte(struct sh_mobile_i2c_data *pd)
362 return pd->pos == pd->msg->len - 1;
365 static void sh_mobile_i2c_get_data(struct sh_mobile_i2c_data *pd,
370 *buf = (pd->msg->addr & 0x7f) << 1;
371 *buf |= (pd->msg->flags & I2C_M_RD) ? 1 : 0;
374 *buf = pd->msg->buf[pd->pos];
378 static int sh_mobile_i2c_isr_tx(struct sh_mobile_i2c_data *pd)
382 if (pd->pos == pd->msg->len)
385 sh_mobile_i2c_get_data(pd, &data);
387 if (sh_mobile_i2c_is_last_byte(pd))
388 i2c_op(pd, OP_TX_STOP, data);
389 else if (sh_mobile_i2c_is_first_byte(pd))
390 i2c_op(pd, OP_TX_FIRST, data);
392 i2c_op(pd, OP_TX, data);
398 static int sh_mobile_i2c_isr_rx(struct sh_mobile_i2c_data *pd)
405 sh_mobile_i2c_get_data(pd, &data);
407 if (sh_mobile_i2c_is_first_byte(pd))
408 i2c_op(pd, OP_TX_FIRST, data);
410 i2c_op(pd, OP_TX, data);
415 i2c_op(pd, OP_TX_TO_RX, 0);
419 real_pos = pd->pos - 2;
421 if (pd->pos == pd->msg->len) {
423 i2c_op(pd, OP_RX_STOP, 0);
426 data = i2c_op(pd, OP_RX_STOP_DATA, 0);
428 data = i2c_op(pd, OP_RX, 0);
431 pd->msg->buf[real_pos] = data;
435 return pd->pos == (pd->msg->len + 2);
438 static irqreturn_t sh_mobile_i2c_isr(int irq, void *dev_id)
440 struct platform_device *dev = dev_id;
441 struct sh_mobile_i2c_data *pd = platform_get_drvdata(dev);
445 sr = iic_rd(pd, ICSR);
446 pd->sr |= sr; /* remember state */
448 dev_dbg(pd->dev, "i2c_isr 0x%02x 0x%02x %s %d %d!\n", sr, pd->sr,
449 (pd->msg->flags & I2C_M_RD) ? "read" : "write",
450 pd->pos, pd->msg->len);
452 if (sr & (ICSR_AL | ICSR_TACK)) {
453 /* don't interrupt transaction - continue to issue stop */
454 iic_wr(pd, ICSR, sr & ~(ICSR_AL | ICSR_TACK));
456 } else if (pd->msg->flags & I2C_M_RD)
457 wakeup = sh_mobile_i2c_isr_rx(pd);
459 wakeup = sh_mobile_i2c_isr_tx(pd);
461 if (sr & ICSR_WAIT) /* TODO: add delay here to support slow acks */
462 iic_wr(pd, ICSR, sr & ~ICSR_WAIT);
469 /* defeat write posting to avoid spurious WAIT interrupts */
475 static int start_ch(struct sh_mobile_i2c_data *pd, struct i2c_msg *usr_msg,
478 if (usr_msg->len == 0 && (usr_msg->flags & I2C_M_RD)) {
479 dev_err(pd->dev, "Unsupported zero length i2c read\n");
484 /* Initialize channel registers */
485 iic_set_clr(pd, ICCR, 0, ICCR_ICE);
487 /* Enable channel and configure rx ack */
488 iic_set_clr(pd, ICCR, ICCR_ICE, 0);
491 iic_wr(pd, ICCL, pd->iccl & 0xff);
492 iic_wr(pd, ICCH, pd->icch & 0xff);
499 /* Enable all interrupts to begin with */
500 iic_wr(pd, ICIC, ICIC_DTEE | ICIC_WAITE | ICIC_ALE | ICIC_TACKE);
504 static int poll_dte(struct sh_mobile_i2c_data *pd)
508 for (i = 1000; i; i--) {
509 u_int8_t val = iic_rd(pd, ICSR);
520 return i ? 0 : -ETIMEDOUT;
523 static int poll_busy(struct sh_mobile_i2c_data *pd)
527 for (i = 1000; i; i--) {
528 u_int8_t val = iic_rd(pd, ICSR);
530 dev_dbg(pd->dev, "val 0x%02x pd->sr 0x%02x\n", val, pd->sr);
532 /* the interrupt handler may wake us up before the
533 * transfer is finished, so poll the hardware
536 if (!(val & ICSR_BUSY)) {
537 /* handle missing acknowledge and arbitration lost */
549 return i ? 0 : -ETIMEDOUT;
552 static int sh_mobile_i2c_xfer(struct i2c_adapter *adapter,
553 struct i2c_msg *msgs,
556 struct sh_mobile_i2c_data *pd = i2c_get_adapdata(adapter);
563 /* Process all messages */
564 for (i = 0; i < num; i++) {
565 bool do_start = pd->send_stop || !i;
567 pd->send_stop = i == num - 1 || msg->flags & I2C_M_STOP;
569 err = start_ch(pd, msg, do_start);
574 i2c_op(pd, OP_START, 0);
576 /* The interrupt handler takes care of the rest... */
577 k = wait_event_timeout(pd->wait,
578 pd->sr & (ICSR_TACK | SW_DONE),
581 dev_err(pd->dev, "Transfer request timed out\n");
601 static u32 sh_mobile_i2c_func(struct i2c_adapter *adapter)
603 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL | I2C_FUNC_PROTOCOL_MANGLING;
606 static struct i2c_algorithm sh_mobile_i2c_algorithm = {
607 .functionality = sh_mobile_i2c_func,
608 .master_xfer = sh_mobile_i2c_xfer,
611 static int sh_mobile_i2c_hook_irqs(struct platform_device *dev)
613 struct resource *res;
617 while ((res = platform_get_resource(dev, IORESOURCE_IRQ, k))) {
618 for (n = res->start; n <= res->end; n++) {
619 ret = devm_request_irq(&dev->dev, n, sh_mobile_i2c_isr,
620 0, dev_name(&dev->dev), dev);
622 dev_err(&dev->dev, "cannot request IRQ %pa\n", &n);
629 return k > 0 ? 0 : -ENOENT;
632 static int sh_mobile_i2c_probe(struct platform_device *dev)
634 struct i2c_sh_mobile_platform_data *pdata = dev_get_platdata(&dev->dev);
635 struct sh_mobile_i2c_data *pd;
636 struct i2c_adapter *adap;
637 struct resource *res;
641 pd = devm_kzalloc(&dev->dev, sizeof(struct sh_mobile_i2c_data), GFP_KERNEL);
645 pd->clk = devm_clk_get(&dev->dev, NULL);
646 if (IS_ERR(pd->clk)) {
647 dev_err(&dev->dev, "cannot get clock\n");
648 return PTR_ERR(pd->clk);
651 ret = sh_mobile_i2c_hook_irqs(dev);
656 platform_set_drvdata(dev, pd);
658 res = platform_get_resource(dev, IORESOURCE_MEM, 0);
660 pd->reg = devm_ioremap_resource(&dev->dev, res);
662 return PTR_ERR(pd->reg);
664 /* Use platform data bus speed or STANDARD_MODE */
665 ret = of_property_read_u32(dev->dev.of_node, "clock-frequency", &bus_speed);
666 pd->bus_speed = ret ? STANDARD_MODE : bus_speed;
668 if (pdata && pdata->bus_speed)
669 pd->bus_speed = pdata->bus_speed;
670 pd->clks_per_count = 1;
671 if (pdata && pdata->clks_per_count)
672 pd->clks_per_count = pdata->clks_per_count;
674 /* The IIC blocks on SH-Mobile ARM processors
675 * come with two new bits in ICIC.
677 if (resource_size(res) > 0x17)
678 pd->flags |= IIC_FLAG_HAS_ICIC67;
680 ret = sh_mobile_i2c_init(pd);
684 /* Enable Runtime PM for this device.
686 * Also tell the Runtime PM core to ignore children
687 * for this device since it is valid for us to suspend
688 * this I2C master driver even though the slave devices
689 * on the I2C bus may not be suspended.
691 * The state of the I2C hardware bus is unaffected by
692 * the Runtime PM state.
694 pm_suspend_ignore_children(&dev->dev, true);
695 pm_runtime_enable(&dev->dev);
697 /* setup the private data */
699 i2c_set_adapdata(adap, pd);
701 adap->owner = THIS_MODULE;
702 adap->algo = &sh_mobile_i2c_algorithm;
703 adap->dev.parent = &dev->dev;
706 adap->dev.of_node = dev->dev.of_node;
708 strlcpy(adap->name, dev->name, sizeof(adap->name));
710 spin_lock_init(&pd->lock);
711 init_waitqueue_head(&pd->wait);
713 ret = i2c_add_numbered_adapter(adap);
715 dev_err(&dev->dev, "cannot add numbered adapter\n");
720 "I2C adapter %d with bus speed %lu Hz (L/H=%x/%x)\n",
721 adap->nr, pd->bus_speed, pd->iccl, pd->icch);
726 static int sh_mobile_i2c_remove(struct platform_device *dev)
728 struct sh_mobile_i2c_data *pd = platform_get_drvdata(dev);
730 i2c_del_adapter(&pd->adap);
731 pm_runtime_disable(&dev->dev);
735 static int sh_mobile_i2c_runtime_nop(struct device *dev)
737 /* Runtime PM callback shared between ->runtime_suspend()
738 * and ->runtime_resume(). Simply returns success.
740 * This driver re-initializes all registers after
741 * pm_runtime_get_sync() anyway so there is no need
742 * to save and restore registers here.
747 static const struct dev_pm_ops sh_mobile_i2c_dev_pm_ops = {
748 .runtime_suspend = sh_mobile_i2c_runtime_nop,
749 .runtime_resume = sh_mobile_i2c_runtime_nop,
752 static const struct of_device_id sh_mobile_i2c_dt_ids[] = {
753 { .compatible = "renesas,rmobile-iic", },
756 MODULE_DEVICE_TABLE(of, sh_mobile_i2c_dt_ids);
758 static struct platform_driver sh_mobile_i2c_driver = {
760 .name = "i2c-sh_mobile",
761 .owner = THIS_MODULE,
762 .pm = &sh_mobile_i2c_dev_pm_ops,
763 .of_match_table = sh_mobile_i2c_dt_ids,
765 .probe = sh_mobile_i2c_probe,
766 .remove = sh_mobile_i2c_remove,
769 static int __init sh_mobile_i2c_adap_init(void)
771 return platform_driver_register(&sh_mobile_i2c_driver);
774 static void __exit sh_mobile_i2c_adap_exit(void)
776 platform_driver_unregister(&sh_mobile_i2c_driver);
779 subsys_initcall(sh_mobile_i2c_adap_init);
780 module_exit(sh_mobile_i2c_adap_exit);
782 MODULE_DESCRIPTION("SuperH Mobile I2C Bus Controller driver");
783 MODULE_AUTHOR("Magnus Damm");
784 MODULE_LICENSE("GPL v2");
785 MODULE_ALIAS("platform:i2c-sh_mobile");