aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/i2c/busses/i2c-sh_mobile.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/i2c/busses/i2c-sh_mobile.c')
-rw-r--r--drivers/i2c/busses/i2c-sh_mobile.c225
1 files changed, 107 insertions, 118 deletions
diff --git a/drivers/i2c/busses/i2c-sh_mobile.c b/drivers/i2c/busses/i2c-sh_mobile.c
index 1d79585ba4b3..8b5e79cb4468 100644
--- a/drivers/i2c/busses/i2c-sh_mobile.c
+++ b/drivers/i2c/busses/i2c-sh_mobile.c
@@ -32,6 +32,7 @@
32#include <linux/clk.h> 32#include <linux/clk.h>
33#include <linux/io.h> 33#include <linux/io.h>
34#include <linux/slab.h> 34#include <linux/slab.h>
35#include <linux/of_device.h>
35#include <linux/i2c/i2c-sh_mobile.h> 36#include <linux/i2c/i2c-sh_mobile.h>
36 37
37/* Transmit operation: */ 38/* Transmit operation: */
@@ -139,6 +140,10 @@ struct sh_mobile_i2c_data {
139 bool send_stop; 140 bool send_stop;
140}; 141};
141 142
143struct sh_mobile_dt_config {
144 int clks_per_count;
145};
146
142#define IIC_FLAG_HAS_ICIC67 (1 << 0) 147#define IIC_FLAG_HAS_ICIC67 (1 << 0)
143 148
144#define STANDARD_MODE 100000 149#define STANDARD_MODE 100000
@@ -194,7 +199,7 @@ static void iic_set_clr(struct sh_mobile_i2c_data *pd, int offs,
194 iic_wr(pd, offs, (iic_rd(pd, offs) | set) & ~clr); 199 iic_wr(pd, offs, (iic_rd(pd, offs) | set) & ~clr);
195} 200}
196 201
197static u32 sh_mobile_i2c_iccl(unsigned long count_khz, u32 tLOW, u32 tf, int offset) 202static u32 sh_mobile_i2c_iccl(unsigned long count_khz, u32 tLOW, u32 tf)
198{ 203{
199 /* 204 /*
200 * Conditional expression: 205 * Conditional expression:
@@ -206,10 +211,10 @@ static u32 sh_mobile_i2c_iccl(unsigned long count_khz, u32 tLOW, u32 tf, int off
206 * account the fall time of SCL signal (tf). Default tf value 211 * account the fall time of SCL signal (tf). Default tf value
207 * should be 0.3 us, for safety. 212 * should be 0.3 us, for safety.
208 */ 213 */
209 return (((count_khz * (tLOW + tf)) + 5000) / 10000) + offset; 214 return (((count_khz * (tLOW + tf)) + 5000) / 10000);
210} 215}
211 216
212static u32 sh_mobile_i2c_icch(unsigned long count_khz, u32 tHIGH, u32 tf, int offset) 217static u32 sh_mobile_i2c_icch(unsigned long count_khz, u32 tHIGH, u32 tf)
213{ 218{
214 /* 219 /*
215 * Conditional expression: 220 * Conditional expression:
@@ -225,52 +230,58 @@ static u32 sh_mobile_i2c_icch(unsigned long count_khz, u32 tHIGH, u32 tf, int of
225 * to take into account the fall time of SDA signal (tf) at START 230 * 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. 231 * condition, in order to meet both tHIGH and tHD;STA specs.
227 */ 232 */
228 return (((count_khz * (tHIGH + tf)) + 5000) / 10000) + offset; 233 return (((count_khz * (tHIGH + tf)) + 5000) / 10000);
229} 234}
230 235
231static void sh_mobile_i2c_init(struct sh_mobile_i2c_data *pd) 236static int sh_mobile_i2c_init(struct sh_mobile_i2c_data *pd)
232{ 237{
233 unsigned long i2c_clk_khz; 238 unsigned long i2c_clk_khz;
234 u32 tHIGH, tLOW, tf; 239 u32 tHIGH, tLOW, tf;
235 int offset; 240 uint16_t max_val;
236 241
237 /* Get clock rate after clock is enabled */ 242 /* Get clock rate after clock is enabled */
238 clk_prepare_enable(pd->clk); 243 clk_prepare_enable(pd->clk);
239 i2c_clk_khz = clk_get_rate(pd->clk) / 1000; 244 i2c_clk_khz = clk_get_rate(pd->clk) / 1000;
245 clk_disable_unprepare(pd->clk);
240 i2c_clk_khz /= pd->clks_per_count; 246 i2c_clk_khz /= pd->clks_per_count;
241 247
242 if (pd->bus_speed == STANDARD_MODE) { 248 if (pd->bus_speed == STANDARD_MODE) {
243 tLOW = 47; /* tLOW = 4.7 us */ 249 tLOW = 47; /* tLOW = 4.7 us */
244 tHIGH = 40; /* tHD;STA = tHIGH = 4.0 us */ 250 tHIGH = 40; /* tHD;STA = tHIGH = 4.0 us */
245 tf = 3; /* tf = 0.3 us */ 251 tf = 3; /* tf = 0.3 us */
246 offset = 0; /* No offset */
247 } else if (pd->bus_speed == FAST_MODE) { 252 } else if (pd->bus_speed == FAST_MODE) {
248 tLOW = 13; /* tLOW = 1.3 us */ 253 tLOW = 13; /* tLOW = 1.3 us */
249 tHIGH = 6; /* tHD;STA = tHIGH = 0.6 us */ 254 tHIGH = 6; /* tHD;STA = tHIGH = 0.6 us */
250 tf = 3; /* tf = 0.3 us */ 255 tf = 3; /* tf = 0.3 us */
251 offset = 0; /* No offset */
252 } else { 256 } else {
253 dev_err(pd->dev, "unrecognized bus speed %lu Hz\n", 257 dev_err(pd->dev, "unrecognized bus speed %lu Hz\n",
254 pd->bus_speed); 258 pd->bus_speed);
255 goto out; 259 return -EINVAL;
260 }
261
262 pd->iccl = sh_mobile_i2c_iccl(i2c_clk_khz, tLOW, tf);
263 pd->icch = sh_mobile_i2c_icch(i2c_clk_khz, tHIGH, tf);
264
265 max_val = pd->flags & IIC_FLAG_HAS_ICIC67 ? 0x1ff : 0xff;
266 if (pd->iccl > max_val || pd->icch > max_val) {
267 dev_err(pd->dev, "timing values out of range: L/H=0x%x/0x%x\n",
268 pd->iccl, pd->icch);
269 return -EINVAL;
256 } 270 }
257 271
258 pd->iccl = sh_mobile_i2c_iccl(i2c_clk_khz, tLOW, tf, offset);
259 /* one more bit of ICCL in ICIC */ 272 /* one more bit of ICCL in ICIC */
260 if ((pd->iccl > 0xff) && (pd->flags & IIC_FLAG_HAS_ICIC67)) 273 if (pd->iccl & 0x100)
261 pd->icic |= ICIC_ICCLB8; 274 pd->icic |= ICIC_ICCLB8;
262 else 275 else
263 pd->icic &= ~ICIC_ICCLB8; 276 pd->icic &= ~ICIC_ICCLB8;
264 277
265 pd->icch = sh_mobile_i2c_icch(i2c_clk_khz, tHIGH, tf, offset);
266 /* one more bit of ICCH in ICIC */ 278 /* one more bit of ICCH in ICIC */
267 if ((pd->icch > 0xff) && (pd->flags & IIC_FLAG_HAS_ICIC67)) 279 if (pd->icch & 0x100)
268 pd->icic |= ICIC_ICCHB8; 280 pd->icic |= ICIC_ICCHB8;
269 else 281 else
270 pd->icic &= ~ICIC_ICCHB8; 282 pd->icic &= ~ICIC_ICCHB8;
271 283
272out: 284 return 0;
273 clk_disable_unprepare(pd->clk);
274} 285}
275 286
276static void activate_ch(struct sh_mobile_i2c_data *pd) 287static void activate_ch(struct sh_mobile_i2c_data *pd)
@@ -316,7 +327,7 @@ static unsigned char i2c_op(struct sh_mobile_i2c_data *pd,
316 327
317 switch (op) { 328 switch (op) {
318 case OP_START: /* issue start and trigger DTE interrupt */ 329 case OP_START: /* issue start and trigger DTE interrupt */
319 iic_wr(pd, ICCR, 0x94); 330 iic_wr(pd, ICCR, ICCR_ICE | ICCR_TRS | ICCR_BBSY);
320 break; 331 break;
321 case OP_TX_FIRST: /* disable DTE interrupt and write data */ 332 case OP_TX_FIRST: /* disable DTE interrupt and write data */
322 iic_wr(pd, ICIC, ICIC_WAITE | ICIC_ALE | ICIC_TACKE); 333 iic_wr(pd, ICIC, ICIC_WAITE | ICIC_ALE | ICIC_TACKE);
@@ -327,10 +338,11 @@ static unsigned char i2c_op(struct sh_mobile_i2c_data *pd,
327 break; 338 break;
328 case OP_TX_STOP: /* write data and issue a stop afterwards */ 339 case OP_TX_STOP: /* write data and issue a stop afterwards */
329 iic_wr(pd, ICDR, data); 340 iic_wr(pd, ICDR, data);
330 iic_wr(pd, ICCR, pd->send_stop ? 0x90 : 0x94); 341 iic_wr(pd, ICCR, pd->send_stop ? ICCR_ICE | ICCR_TRS
342 : ICCR_ICE | ICCR_TRS | ICCR_BBSY);
331 break; 343 break;
332 case OP_TX_TO_RX: /* select read mode */ 344 case OP_TX_TO_RX: /* select read mode */
333 iic_wr(pd, ICCR, 0x81); 345 iic_wr(pd, ICCR, ICCR_ICE | ICCR_SCP);
334 break; 346 break;
335 case OP_RX: /* just read data */ 347 case OP_RX: /* just read data */
336 ret = iic_rd(pd, ICDR); 348 ret = iic_rd(pd, ICDR);
@@ -338,13 +350,13 @@ static unsigned char i2c_op(struct sh_mobile_i2c_data *pd,
338 case OP_RX_STOP: /* enable DTE interrupt, issue stop */ 350 case OP_RX_STOP: /* enable DTE interrupt, issue stop */
339 iic_wr(pd, ICIC, 351 iic_wr(pd, ICIC,
340 ICIC_DTEE | ICIC_WAITE | ICIC_ALE | ICIC_TACKE); 352 ICIC_DTEE | ICIC_WAITE | ICIC_ALE | ICIC_TACKE);
341 iic_wr(pd, ICCR, 0xc0); 353 iic_wr(pd, ICCR, ICCR_ICE | ICCR_RACK);
342 break; 354 break;
343 case OP_RX_STOP_DATA: /* enable DTE interrupt, read data, issue stop */ 355 case OP_RX_STOP_DATA: /* enable DTE interrupt, read data, issue stop */
344 iic_wr(pd, ICIC, 356 iic_wr(pd, ICIC,
345 ICIC_DTEE | ICIC_WAITE | ICIC_ALE | ICIC_TACKE); 357 ICIC_DTEE | ICIC_WAITE | ICIC_ALE | ICIC_TACKE);
346 ret = iic_rd(pd, ICDR); 358 ret = iic_rd(pd, ICDR);
347 iic_wr(pd, ICCR, 0xc0); 359 iic_wr(pd, ICCR, ICCR_ICE | ICCR_RACK);
348 break; 360 break;
349 } 361 }
350 362
@@ -479,7 +491,7 @@ static int start_ch(struct sh_mobile_i2c_data *pd, struct i2c_msg *usr_msg,
479{ 491{
480 if (usr_msg->len == 0 && (usr_msg->flags & I2C_M_RD)) { 492 if (usr_msg->len == 0 && (usr_msg->flags & I2C_M_RD)) {
481 dev_err(pd->dev, "Unsupported zero length i2c read\n"); 493 dev_err(pd->dev, "Unsupported zero length i2c read\n");
482 return -EIO; 494 return -EOPNOTSUPP;
483 } 495 }
484 496
485 if (do_init) { 497 if (do_init) {
@@ -514,17 +526,12 @@ static int poll_dte(struct sh_mobile_i2c_data *pd)
514 break; 526 break;
515 527
516 if (val & ICSR_TACK) 528 if (val & ICSR_TACK)
517 return -EIO; 529 return -ENXIO;
518 530
519 udelay(10); 531 udelay(10);
520 } 532 }
521 533
522 if (!i) { 534 return i ? 0 : -ETIMEDOUT;
523 dev_warn(pd->dev, "Timeout polling for DTE!\n");
524 return -ETIMEDOUT;
525 }
526
527 return 0;
528} 535}
529 536
530static int poll_busy(struct sh_mobile_i2c_data *pd) 537static int poll_busy(struct sh_mobile_i2c_data *pd)
@@ -542,20 +549,18 @@ static int poll_busy(struct sh_mobile_i2c_data *pd)
542 */ 549 */
543 if (!(val & ICSR_BUSY)) { 550 if (!(val & ICSR_BUSY)) {
544 /* handle missing acknowledge and arbitration lost */ 551 /* handle missing acknowledge and arbitration lost */
545 if ((val | pd->sr) & (ICSR_TACK | ICSR_AL)) 552 val |= pd->sr;
546 return -EIO; 553 if (val & ICSR_TACK)
554 return -ENXIO;
555 if (val & ICSR_AL)
556 return -EAGAIN;
547 break; 557 break;
548 } 558 }
549 559
550 udelay(10); 560 udelay(10);
551 } 561 }
552 562
553 if (!i) { 563 return i ? 0 : -ETIMEDOUT;
554 dev_err(pd->dev, "Polling timed out\n");
555 return -ETIMEDOUT;
556 }
557
558 return 0;
559} 564}
560 565
561static int sh_mobile_i2c_xfer(struct i2c_adapter *adapter, 566static int sh_mobile_i2c_xfer(struct i2c_adapter *adapter,
@@ -617,42 +622,44 @@ static struct i2c_algorithm sh_mobile_i2c_algorithm = {
617 .master_xfer = sh_mobile_i2c_xfer, 622 .master_xfer = sh_mobile_i2c_xfer,
618}; 623};
619 624
620static int sh_mobile_i2c_hook_irqs(struct platform_device *dev, int hook) 625static const struct sh_mobile_dt_config default_dt_config = {
626 .clks_per_count = 1,
627};
628
629static const struct sh_mobile_dt_config rcar_gen2_dt_config = {
630 .clks_per_count = 2,
631};
632
633static const struct of_device_id sh_mobile_i2c_dt_ids[] = {
634 { .compatible = "renesas,rmobile-iic", .data = &default_dt_config },
635 { .compatible = "renesas,iic-r8a7790", .data = &rcar_gen2_dt_config },
636 { .compatible = "renesas,iic-r8a7791", .data = &rcar_gen2_dt_config },
637 { .compatible = "renesas,iic-r8a7792", .data = &rcar_gen2_dt_config },
638 { .compatible = "renesas,iic-r8a7793", .data = &rcar_gen2_dt_config },
639 { .compatible = "renesas,iic-r8a7794", .data = &rcar_gen2_dt_config },
640 {},
641};
642MODULE_DEVICE_TABLE(of, sh_mobile_i2c_dt_ids);
643
644static int sh_mobile_i2c_hook_irqs(struct platform_device *dev)
621{ 645{
622 struct resource *res; 646 struct resource *res;
623 int ret = -ENXIO; 647 resource_size_t n;
624 int n, k = 0; 648 int k = 0, ret;
625 649
626 while ((res = platform_get_resource(dev, IORESOURCE_IRQ, k))) { 650 while ((res = platform_get_resource(dev, IORESOURCE_IRQ, k))) {
627 for (n = res->start; hook && n <= res->end; n++) { 651 for (n = res->start; n <= res->end; n++) {
628 if (request_irq(n, sh_mobile_i2c_isr, 0, 652 ret = devm_request_irq(&dev->dev, n, sh_mobile_i2c_isr,
629 dev_name(&dev->dev), dev)) { 653 0, dev_name(&dev->dev), dev);
630 for (n--; n >= res->start; n--) 654 if (ret) {
631 free_irq(n, dev); 655 dev_err(&dev->dev, "cannot request IRQ %pa\n", &n);
632 656 return ret;
633 goto rollback;
634 } 657 }
635 } 658 }
636 k++; 659 k++;
637 } 660 }
638 661
639 if (hook) 662 return k > 0 ? 0 : -ENOENT;
640 return k > 0 ? 0 : -ENOENT;
641
642 ret = 0;
643
644 rollback:
645 k--;
646
647 while (k >= 0) {
648 res = platform_get_resource(dev, IORESOURCE_IRQ, k);
649 for (n = res->start; n <= res->end; n++)
650 free_irq(n, dev);
651
652 k--;
653 }
654
655 return ret;
656} 663}
657 664
658static int sh_mobile_i2c_probe(struct platform_device *dev) 665static int sh_mobile_i2c_probe(struct platform_device *dev)
@@ -661,62 +668,64 @@ static int sh_mobile_i2c_probe(struct platform_device *dev)
661 struct sh_mobile_i2c_data *pd; 668 struct sh_mobile_i2c_data *pd;
662 struct i2c_adapter *adap; 669 struct i2c_adapter *adap;
663 struct resource *res; 670 struct resource *res;
664 int size;
665 int ret; 671 int ret;
672 u32 bus_speed;
666 673
667 pd = kzalloc(sizeof(struct sh_mobile_i2c_data), GFP_KERNEL); 674 pd = devm_kzalloc(&dev->dev, sizeof(struct sh_mobile_i2c_data), GFP_KERNEL);
668 if (pd == NULL) { 675 if (!pd)
669 dev_err(&dev->dev, "cannot allocate private data\n");
670 return -ENOMEM; 676 return -ENOMEM;
671 }
672 677
673 pd->clk = clk_get(&dev->dev, NULL); 678 pd->clk = devm_clk_get(&dev->dev, NULL);
674 if (IS_ERR(pd->clk)) { 679 if (IS_ERR(pd->clk)) {
675 dev_err(&dev->dev, "cannot get clock\n"); 680 dev_err(&dev->dev, "cannot get clock\n");
676 ret = PTR_ERR(pd->clk); 681 return PTR_ERR(pd->clk);
677 goto err;
678 } 682 }
679 683
680 ret = sh_mobile_i2c_hook_irqs(dev, 1); 684 ret = sh_mobile_i2c_hook_irqs(dev);
681 if (ret) { 685 if (ret)
682 dev_err(&dev->dev, "cannot request IRQ\n"); 686 return ret;
683 goto err_clk;
684 }
685 687
686 pd->dev = &dev->dev; 688 pd->dev = &dev->dev;
687 platform_set_drvdata(dev, pd); 689 platform_set_drvdata(dev, pd);
688 690
689 res = platform_get_resource(dev, IORESOURCE_MEM, 0); 691 res = platform_get_resource(dev, IORESOURCE_MEM, 0);
690 if (res == NULL) {
691 dev_err(&dev->dev, "cannot find IO resource\n");
692 ret = -ENOENT;
693 goto err_irq;
694 }
695
696 size = resource_size(res);
697 692
698 pd->reg = ioremap(res->start, size); 693 pd->reg = devm_ioremap_resource(&dev->dev, res);
699 if (pd->reg == NULL) { 694 if (IS_ERR(pd->reg))
700 dev_err(&dev->dev, "cannot map IO\n"); 695 return PTR_ERR(pd->reg);
701 ret = -ENXIO;
702 goto err_irq;
703 }
704 696
705 /* Use platform data bus speed or STANDARD_MODE */ 697 /* Use platform data bus speed or STANDARD_MODE */
706 pd->bus_speed = STANDARD_MODE; 698 ret = of_property_read_u32(dev->dev.of_node, "clock-frequency", &bus_speed);
707 if (pdata && pdata->bus_speed) 699 pd->bus_speed = ret ? STANDARD_MODE : bus_speed;
708 pd->bus_speed = pdata->bus_speed; 700
709 pd->clks_per_count = 1; 701 pd->clks_per_count = 1;
710 if (pdata && pdata->clks_per_count) 702
711 pd->clks_per_count = pdata->clks_per_count; 703 if (dev->dev.of_node) {
704 const struct of_device_id *match;
705
706 match = of_match_device(sh_mobile_i2c_dt_ids, &dev->dev);
707 if (match) {
708 const struct sh_mobile_dt_config *config;
709
710 config = match->data;
711 pd->clks_per_count = config->clks_per_count;
712 }
713 } else {
714 if (pdata && pdata->bus_speed)
715 pd->bus_speed = pdata->bus_speed;
716 if (pdata && pdata->clks_per_count)
717 pd->clks_per_count = pdata->clks_per_count;
718 }
712 719
713 /* The IIC blocks on SH-Mobile ARM processors 720 /* The IIC blocks on SH-Mobile ARM processors
714 * come with two new bits in ICIC. 721 * come with two new bits in ICIC.
715 */ 722 */
716 if (size > 0x17) 723 if (resource_size(res) > 0x17)
717 pd->flags |= IIC_FLAG_HAS_ICIC67; 724 pd->flags |= IIC_FLAG_HAS_ICIC67;
718 725
719 sh_mobile_i2c_init(pd); 726 ret = sh_mobile_i2c_init(pd);
727 if (ret)
728 return ret;
720 729
721 /* Enable Runtime PM for this device. 730 /* Enable Runtime PM for this device.
722 * 731 *
@@ -750,24 +759,14 @@ static int sh_mobile_i2c_probe(struct platform_device *dev)
750 ret = i2c_add_numbered_adapter(adap); 759 ret = i2c_add_numbered_adapter(adap);
751 if (ret < 0) { 760 if (ret < 0) {
752 dev_err(&dev->dev, "cannot add numbered adapter\n"); 761 dev_err(&dev->dev, "cannot add numbered adapter\n");
753 goto err_all; 762 return ret;
754 } 763 }
755 764
756 dev_info(&dev->dev, 765 dev_info(&dev->dev,
757 "I2C adapter %d with bus speed %lu Hz (L/H=%x/%x)\n", 766 "I2C adapter %d with bus speed %lu Hz (L/H=0x%x/0x%x)\n",
758 adap->nr, pd->bus_speed, pd->iccl, pd->icch); 767 adap->nr, pd->bus_speed, pd->iccl, pd->icch);
759 768
760 return 0; 769 return 0;
761
762 err_all:
763 iounmap(pd->reg);
764 err_irq:
765 sh_mobile_i2c_hook_irqs(dev, 0);
766 err_clk:
767 clk_put(pd->clk);
768 err:
769 kfree(pd);
770 return ret;
771} 770}
772 771
773static int sh_mobile_i2c_remove(struct platform_device *dev) 772static int sh_mobile_i2c_remove(struct platform_device *dev)
@@ -775,11 +774,7 @@ static int sh_mobile_i2c_remove(struct platform_device *dev)
775 struct sh_mobile_i2c_data *pd = platform_get_drvdata(dev); 774 struct sh_mobile_i2c_data *pd = platform_get_drvdata(dev);
776 775
777 i2c_del_adapter(&pd->adap); 776 i2c_del_adapter(&pd->adap);
778 iounmap(pd->reg);
779 sh_mobile_i2c_hook_irqs(dev, 0);
780 clk_put(pd->clk);
781 pm_runtime_disable(&dev->dev); 777 pm_runtime_disable(&dev->dev);
782 kfree(pd);
783 return 0; 778 return 0;
784} 779}
785 780
@@ -800,12 +795,6 @@ static const struct dev_pm_ops sh_mobile_i2c_dev_pm_ops = {
800 .runtime_resume = sh_mobile_i2c_runtime_nop, 795 .runtime_resume = sh_mobile_i2c_runtime_nop,
801}; 796};
802 797
803static const struct of_device_id sh_mobile_i2c_dt_ids[] = {
804 { .compatible = "renesas,rmobile-iic", },
805 {},
806};
807MODULE_DEVICE_TABLE(of, sh_mobile_i2c_dt_ids);
808
809static struct platform_driver sh_mobile_i2c_driver = { 798static struct platform_driver sh_mobile_i2c_driver = {
810 .driver = { 799 .driver = {
811 .name = "i2c-sh_mobile", 800 .name = "i2c-sh_mobile",