diff options
Diffstat (limited to 'drivers/i2c/busses/i2c-nomadik.c')
-rw-r--r-- | drivers/i2c/busses/i2c-nomadik.c | 302 |
1 files changed, 201 insertions, 101 deletions
diff --git a/drivers/i2c/busses/i2c-nomadik.c b/drivers/i2c/busses/i2c-nomadik.c index 73de8ade10b1..0c731ca69f15 100644 --- a/drivers/i2c/busses/i2c-nomadik.c +++ b/drivers/i2c/busses/i2c-nomadik.c | |||
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | * Copyright (C) 2009 ST-Ericsson | 2 | * Copyright (C) 2009 ST-Ericsson SA |
3 | * Copyright (C) 2009 STMicroelectronics | 3 | * Copyright (C) 2009 STMicroelectronics |
4 | * | 4 | * |
5 | * I2C master mode controller driver, used in Nomadik 8815 | 5 | * I2C master mode controller driver, used in Nomadik 8815 |
@@ -15,13 +15,14 @@ | |||
15 | #include <linux/init.h> | 15 | #include <linux/init.h> |
16 | #include <linux/module.h> | 16 | #include <linux/module.h> |
17 | #include <linux/platform_device.h> | 17 | #include <linux/platform_device.h> |
18 | #include <linux/delay.h> | ||
19 | #include <linux/slab.h> | 18 | #include <linux/slab.h> |
20 | #include <linux/interrupt.h> | 19 | #include <linux/interrupt.h> |
21 | #include <linux/i2c.h> | 20 | #include <linux/i2c.h> |
22 | #include <linux/err.h> | 21 | #include <linux/err.h> |
23 | #include <linux/clk.h> | 22 | #include <linux/clk.h> |
24 | #include <linux/io.h> | 23 | #include <linux/io.h> |
24 | #include <linux/regulator/consumer.h> | ||
25 | #include <linux/pm_runtime.h> | ||
25 | 26 | ||
26 | #include <plat/i2c.h> | 27 | #include <plat/i2c.h> |
27 | 28 | ||
@@ -117,15 +118,12 @@ enum i2c_operation { | |||
117 | I2C_READ = 0x01 | 118 | I2C_READ = 0x01 |
118 | }; | 119 | }; |
119 | 120 | ||
120 | /* controller response timeout in ms */ | ||
121 | #define I2C_TIMEOUT_MS 500 | ||
122 | |||
123 | /** | 121 | /** |
124 | * struct i2c_nmk_client - client specific data | 122 | * struct i2c_nmk_client - client specific data |
125 | * @slave_adr: 7-bit slave address | 123 | * @slave_adr: 7-bit slave address |
126 | * @count: no. bytes to be transfered | 124 | * @count: no. bytes to be transferred |
127 | * @buffer: client data buffer | 125 | * @buffer: client data buffer |
128 | * @xfer_bytes: bytes transfered till now | 126 | * @xfer_bytes: bytes transferred till now |
129 | * @operation: current I2C operation | 127 | * @operation: current I2C operation |
130 | */ | 128 | */ |
131 | struct i2c_nmk_client { | 129 | struct i2c_nmk_client { |
@@ -148,6 +146,7 @@ struct i2c_nmk_client { | |||
148 | * @stop: stop condition | 146 | * @stop: stop condition |
149 | * @xfer_complete: acknowledge completion for a I2C message | 147 | * @xfer_complete: acknowledge completion for a I2C message |
150 | * @result: controller propogated result | 148 | * @result: controller propogated result |
149 | * @busy: Busy doing transfer | ||
151 | */ | 150 | */ |
152 | struct nmk_i2c_dev { | 151 | struct nmk_i2c_dev { |
153 | struct platform_device *pdev; | 152 | struct platform_device *pdev; |
@@ -160,6 +159,8 @@ struct nmk_i2c_dev { | |||
160 | int stop; | 159 | int stop; |
161 | struct completion xfer_complete; | 160 | struct completion xfer_complete; |
162 | int result; | 161 | int result; |
162 | struct regulator *regulator; | ||
163 | bool busy; | ||
163 | }; | 164 | }; |
164 | 165 | ||
165 | /* controller's abort causes */ | 166 | /* controller's abort causes */ |
@@ -206,7 +207,7 @@ static int flush_i2c_fifo(struct nmk_i2c_dev *dev) | |||
206 | writel((I2C_CR_FTX | I2C_CR_FRX), dev->virtbase + I2C_CR); | 207 | writel((I2C_CR_FTX | I2C_CR_FRX), dev->virtbase + I2C_CR); |
207 | 208 | ||
208 | for (i = 0; i < LOOP_ATTEMPTS; i++) { | 209 | for (i = 0; i < LOOP_ATTEMPTS; i++) { |
209 | timeout = jiffies + msecs_to_jiffies(I2C_TIMEOUT_MS); | 210 | timeout = jiffies + dev->adap.timeout; |
210 | 211 | ||
211 | while (!time_after(jiffies, timeout)) { | 212 | while (!time_after(jiffies, timeout)) { |
212 | if ((readl(dev->virtbase + I2C_CR) & | 213 | if ((readl(dev->virtbase + I2C_CR) & |
@@ -252,7 +253,7 @@ static int init_hw(struct nmk_i2c_dev *dev) | |||
252 | 253 | ||
253 | stat = flush_i2c_fifo(dev); | 254 | stat = flush_i2c_fifo(dev); |
254 | if (stat) | 255 | if (stat) |
255 | return stat; | 256 | goto exit; |
256 | 257 | ||
257 | /* disable the controller */ | 258 | /* disable the controller */ |
258 | i2c_clr_bit(dev->virtbase + I2C_CR , I2C_CR_PE); | 259 | i2c_clr_bit(dev->virtbase + I2C_CR , I2C_CR_PE); |
@@ -263,7 +264,8 @@ static int init_hw(struct nmk_i2c_dev *dev) | |||
263 | 264 | ||
264 | dev->cli.operation = I2C_NO_OPERATION; | 265 | dev->cli.operation = I2C_NO_OPERATION; |
265 | 266 | ||
266 | return 0; | 267 | exit: |
268 | return stat; | ||
267 | } | 269 | } |
268 | 270 | ||
269 | /* enable peripheral, master mode operation */ | 271 | /* enable peripheral, master mode operation */ |
@@ -322,7 +324,7 @@ static void setup_i2c_controller(struct nmk_i2c_dev *dev) | |||
322 | * slsu defines the data setup time after SCL clock | 324 | * slsu defines the data setup time after SCL clock |
323 | * stretching in terms of i2c clk cycles. The | 325 | * stretching in terms of i2c clk cycles. The |
324 | * needed setup time for the three modes are 250ns, | 326 | * needed setup time for the three modes are 250ns, |
325 | * 100ns, 10ns repectively thus leading to the values | 327 | * 100ns, 10ns respectively thus leading to the values |
326 | * of 14, 6, 2 for a 48 MHz i2c clk. | 328 | * of 14, 6, 2 for a 48 MHz i2c clk. |
327 | */ | 329 | */ |
328 | writel(dev->cfg.slsu << 16, dev->virtbase + I2C_SCR); | 330 | writel(dev->cfg.slsu << 16, dev->virtbase + I2C_SCR); |
@@ -356,7 +358,7 @@ static void setup_i2c_controller(struct nmk_i2c_dev *dev) | |||
356 | /* | 358 | /* |
357 | * set the speed mode. Currently we support | 359 | * set the speed mode. Currently we support |
358 | * only standard and fast mode of operation | 360 | * only standard and fast mode of operation |
359 | * TODO - support for fast mode plus (upto 1Mb/s) | 361 | * TODO - support for fast mode plus (up to 1Mb/s) |
360 | * and high speed (up to 3.4 Mb/s) | 362 | * and high speed (up to 3.4 Mb/s) |
361 | */ | 363 | */ |
362 | if (dev->cfg.sm > I2C_FREQ_MODE_FAST) { | 364 | if (dev->cfg.sm > I2C_FREQ_MODE_FAST) { |
@@ -416,7 +418,7 @@ static int read_i2c(struct nmk_i2c_dev *dev) | |||
416 | dev->virtbase + I2C_IMSCR); | 418 | dev->virtbase + I2C_IMSCR); |
417 | 419 | ||
418 | timeout = wait_for_completion_interruptible_timeout( | 420 | timeout = wait_for_completion_interruptible_timeout( |
419 | &dev->xfer_complete, msecs_to_jiffies(I2C_TIMEOUT_MS)); | 421 | &dev->xfer_complete, dev->adap.timeout); |
420 | 422 | ||
421 | if (timeout < 0) { | 423 | if (timeout < 0) { |
422 | dev_err(&dev->pdev->dev, | 424 | dev_err(&dev->pdev->dev, |
@@ -426,15 +428,32 @@ static int read_i2c(struct nmk_i2c_dev *dev) | |||
426 | } | 428 | } |
427 | 429 | ||
428 | if (timeout == 0) { | 430 | if (timeout == 0) { |
429 | /* controler has timedout, re-init the h/w */ | 431 | /* Controller timed out */ |
430 | dev_err(&dev->pdev->dev, "controller timed out, re-init h/w\n"); | 432 | dev_err(&dev->pdev->dev, "read from slave 0x%x timed out\n", |
431 | (void) init_hw(dev); | 433 | dev->cli.slave_adr); |
432 | status = -ETIMEDOUT; | 434 | status = -ETIMEDOUT; |
433 | } | 435 | } |
434 | |||
435 | return status; | 436 | return status; |
436 | } | 437 | } |
437 | 438 | ||
439 | static void fill_tx_fifo(struct nmk_i2c_dev *dev, int no_bytes) | ||
440 | { | ||
441 | int count; | ||
442 | |||
443 | for (count = (no_bytes - 2); | ||
444 | (count > 0) && | ||
445 | (dev->cli.count != 0); | ||
446 | count--) { | ||
447 | /* write to the Tx FIFO */ | ||
448 | writeb(*dev->cli.buffer, | ||
449 | dev->virtbase + I2C_TFR); | ||
450 | dev->cli.buffer++; | ||
451 | dev->cli.count--; | ||
452 | dev->cli.xfer_bytes++; | ||
453 | } | ||
454 | |||
455 | } | ||
456 | |||
438 | /** | 457 | /** |
439 | * write_i2c() - Write data to I2C client. | 458 | * write_i2c() - Write data to I2C client. |
440 | * @dev: private data of I2C Driver | 459 | * @dev: private data of I2C Driver |
@@ -462,8 +481,13 @@ static int write_i2c(struct nmk_i2c_dev *dev) | |||
462 | init_completion(&dev->xfer_complete); | 481 | init_completion(&dev->xfer_complete); |
463 | 482 | ||
464 | /* enable interrupts by settings the masks */ | 483 | /* enable interrupts by settings the masks */ |
465 | irq_mask = (I2C_IT_TXFNE | I2C_IT_TXFOVR | | 484 | irq_mask = (I2C_IT_TXFOVR | I2C_IT_MAL | I2C_IT_BERR); |
466 | I2C_IT_MAL | I2C_IT_BERR); | 485 | |
486 | /* Fill the TX FIFO with transmit data */ | ||
487 | fill_tx_fifo(dev, MAX_I2C_FIFO_THRESHOLD); | ||
488 | |||
489 | if (dev->cli.count != 0) | ||
490 | irq_mask |= I2C_IT_TXFNE; | ||
467 | 491 | ||
468 | /* | 492 | /* |
469 | * check if we want to transfer a single or multiple bytes, if so | 493 | * check if we want to transfer a single or multiple bytes, if so |
@@ -481,7 +505,7 @@ static int write_i2c(struct nmk_i2c_dev *dev) | |||
481 | dev->virtbase + I2C_IMSCR); | 505 | dev->virtbase + I2C_IMSCR); |
482 | 506 | ||
483 | timeout = wait_for_completion_interruptible_timeout( | 507 | timeout = wait_for_completion_interruptible_timeout( |
484 | &dev->xfer_complete, msecs_to_jiffies(I2C_TIMEOUT_MS)); | 508 | &dev->xfer_complete, dev->adap.timeout); |
485 | 509 | ||
486 | if (timeout < 0) { | 510 | if (timeout < 0) { |
487 | dev_err(&dev->pdev->dev, | 511 | dev_err(&dev->pdev->dev, |
@@ -491,9 +515,9 @@ static int write_i2c(struct nmk_i2c_dev *dev) | |||
491 | } | 515 | } |
492 | 516 | ||
493 | if (timeout == 0) { | 517 | if (timeout == 0) { |
494 | /* controler has timedout, re-init the h/w */ | 518 | /* Controller timed out */ |
495 | dev_err(&dev->pdev->dev, "controller timed out, re-init h/w\n"); | 519 | dev_err(&dev->pdev->dev, "write to slave 0x%x timed out\n", |
496 | (void) init_hw(dev); | 520 | dev->cli.slave_adr); |
497 | status = -ETIMEDOUT; | 521 | status = -ETIMEDOUT; |
498 | } | 522 | } |
499 | 523 | ||
@@ -501,10 +525,55 @@ static int write_i2c(struct nmk_i2c_dev *dev) | |||
501 | } | 525 | } |
502 | 526 | ||
503 | /** | 527 | /** |
528 | * nmk_i2c_xfer_one() - transmit a single I2C message | ||
529 | * @dev: device with a message encoded into it | ||
530 | * @flags: message flags | ||
531 | */ | ||
532 | static int nmk_i2c_xfer_one(struct nmk_i2c_dev *dev, u16 flags) | ||
533 | { | ||
534 | int status; | ||
535 | |||
536 | if (flags & I2C_M_RD) { | ||
537 | /* read operation */ | ||
538 | dev->cli.operation = I2C_READ; | ||
539 | status = read_i2c(dev); | ||
540 | } else { | ||
541 | /* write operation */ | ||
542 | dev->cli.operation = I2C_WRITE; | ||
543 | status = write_i2c(dev); | ||
544 | } | ||
545 | |||
546 | if (status || (dev->result)) { | ||
547 | u32 i2c_sr; | ||
548 | u32 cause; | ||
549 | |||
550 | i2c_sr = readl(dev->virtbase + I2C_SR); | ||
551 | /* | ||
552 | * Check if the controller I2C operation status | ||
553 | * is set to ABORT(11b). | ||
554 | */ | ||
555 | if (((i2c_sr >> 2) & 0x3) == 0x3) { | ||
556 | /* get the abort cause */ | ||
557 | cause = (i2c_sr >> 4) & 0x7; | ||
558 | dev_err(&dev->pdev->dev, "%s\n", cause | ||
559 | >= ARRAY_SIZE(abort_causes) ? | ||
560 | "unknown reason" : | ||
561 | abort_causes[cause]); | ||
562 | } | ||
563 | |||
564 | (void) init_hw(dev); | ||
565 | |||
566 | status = status ? status : dev->result; | ||
567 | } | ||
568 | |||
569 | return status; | ||
570 | } | ||
571 | |||
572 | /** | ||
504 | * nmk_i2c_xfer() - I2C transfer function used by kernel framework | 573 | * nmk_i2c_xfer() - I2C transfer function used by kernel framework |
505 | * @i2c_adap - Adapter pointer to the controller | 574 | * @i2c_adap: Adapter pointer to the controller |
506 | * @msgs[] - Pointer to data to be written. | 575 | * @msgs: Pointer to data to be written. |
507 | * @num_msgs - Number of messages to be executed | 576 | * @num_msgs: Number of messages to be executed |
508 | * | 577 | * |
509 | * This is the function called by the generic kernel i2c_transfer() | 578 | * This is the function called by the generic kernel i2c_transfer() |
510 | * or i2c_smbus...() API calls. Note that this code is protected by the | 579 | * or i2c_smbus...() API calls. Note that this code is protected by the |
@@ -552,49 +621,56 @@ static int nmk_i2c_xfer(struct i2c_adapter *i2c_adap, | |||
552 | { | 621 | { |
553 | int status; | 622 | int status; |
554 | int i; | 623 | int i; |
555 | u32 cause; | ||
556 | struct nmk_i2c_dev *dev = i2c_get_adapdata(i2c_adap); | 624 | struct nmk_i2c_dev *dev = i2c_get_adapdata(i2c_adap); |
625 | int j; | ||
626 | |||
627 | dev->busy = true; | ||
628 | |||
629 | if (dev->regulator) | ||
630 | regulator_enable(dev->regulator); | ||
631 | pm_runtime_get_sync(&dev->pdev->dev); | ||
632 | |||
633 | clk_enable(dev->clk); | ||
557 | 634 | ||
558 | status = init_hw(dev); | 635 | status = init_hw(dev); |
559 | if (status) | 636 | if (status) |
560 | return status; | 637 | goto out; |
561 | 638 | ||
562 | /* setup the i2c controller */ | 639 | /* Attempt three times to send the message queue */ |
563 | setup_i2c_controller(dev); | 640 | for (j = 0; j < 3; j++) { |
641 | /* setup the i2c controller */ | ||
642 | setup_i2c_controller(dev); | ||
564 | 643 | ||
565 | for (i = 0; i < num_msgs; i++) { | 644 | for (i = 0; i < num_msgs; i++) { |
566 | if (unlikely(msgs[i].flags & I2C_M_TEN)) { | 645 | if (unlikely(msgs[i].flags & I2C_M_TEN)) { |
567 | dev_err(&dev->pdev->dev, "10 bit addressing" | 646 | dev_err(&dev->pdev->dev, "10 bit addressing" |
568 | "not supported\n"); | 647 | "not supported\n"); |
569 | return -EINVAL; | 648 | |
570 | } | 649 | status = -EINVAL; |
571 | dev->cli.slave_adr = msgs[i].addr; | 650 | goto out; |
572 | dev->cli.buffer = msgs[i].buf; | 651 | } |
573 | dev->cli.count = msgs[i].len; | 652 | dev->cli.slave_adr = msgs[i].addr; |
574 | dev->stop = (i < (num_msgs - 1)) ? 0 : 1; | 653 | dev->cli.buffer = msgs[i].buf; |
575 | dev->result = 0; | 654 | dev->cli.count = msgs[i].len; |
576 | 655 | dev->stop = (i < (num_msgs - 1)) ? 0 : 1; | |
577 | if (msgs[i].flags & I2C_M_RD) { | 656 | dev->result = 0; |
578 | /* it is a read operation */ | 657 | |
579 | dev->cli.operation = I2C_READ; | 658 | status = nmk_i2c_xfer_one(dev, msgs[i].flags); |
580 | status = read_i2c(dev); | 659 | if (status != 0) |
581 | } else { | 660 | break; |
582 | /* write operation */ | ||
583 | dev->cli.operation = I2C_WRITE; | ||
584 | status = write_i2c(dev); | ||
585 | } | ||
586 | if (status || (dev->result)) { | ||
587 | /* get the abort cause */ | ||
588 | cause = (readl(dev->virtbase + I2C_SR) >> 4) & 0x7; | ||
589 | dev_err(&dev->pdev->dev, "error during I2C" | ||
590 | "message xfer: %d\n", cause); | ||
591 | dev_err(&dev->pdev->dev, "%s\n", | ||
592 | cause >= ARRAY_SIZE(abort_causes) | ||
593 | ? "unknown reason" : abort_causes[cause]); | ||
594 | return status; | ||
595 | } | 661 | } |
596 | mdelay(1); | 662 | if (status == 0) |
663 | break; | ||
597 | } | 664 | } |
665 | |||
666 | out: | ||
667 | clk_disable(dev->clk); | ||
668 | pm_runtime_put_sync(&dev->pdev->dev); | ||
669 | if (dev->regulator) | ||
670 | regulator_disable(dev->regulator); | ||
671 | |||
672 | dev->busy = false; | ||
673 | |||
598 | /* return the no. messages processed */ | 674 | /* return the no. messages processed */ |
599 | if (status) | 675 | if (status) |
600 | return status; | 676 | return status; |
@@ -605,6 +681,7 @@ static int nmk_i2c_xfer(struct i2c_adapter *i2c_adap, | |||
605 | /** | 681 | /** |
606 | * disable_interrupts() - disable the interrupts | 682 | * disable_interrupts() - disable the interrupts |
607 | * @dev: private data of controller | 683 | * @dev: private data of controller |
684 | * @irq: interrupt number | ||
608 | */ | 685 | */ |
609 | static int disable_interrupts(struct nmk_i2c_dev *dev, u32 irq) | 686 | static int disable_interrupts(struct nmk_i2c_dev *dev, u32 irq) |
610 | { | 687 | { |
@@ -653,17 +730,7 @@ static irqreturn_t i2c_irq_handler(int irq, void *arg) | |||
653 | */ | 730 | */ |
654 | disable_interrupts(dev, I2C_IT_TXFNE); | 731 | disable_interrupts(dev, I2C_IT_TXFNE); |
655 | } else { | 732 | } else { |
656 | for (count = (MAX_I2C_FIFO_THRESHOLD - tft - 2); | 733 | fill_tx_fifo(dev, (MAX_I2C_FIFO_THRESHOLD - tft)); |
657 | (count > 0) && | ||
658 | (dev->cli.count != 0); | ||
659 | count--) { | ||
660 | /* write to the Tx FIFO */ | ||
661 | writeb(*dev->cli.buffer, | ||
662 | dev->virtbase + I2C_TFR); | ||
663 | dev->cli.buffer++; | ||
664 | dev->cli.count--; | ||
665 | dev->cli.xfer_bytes++; | ||
666 | } | ||
667 | /* | 734 | /* |
668 | * if done, close the transfer by disabling the | 735 | * if done, close the transfer by disabling the |
669 | * corresponding TXFNE interrupt | 736 | * corresponding TXFNE interrupt |
@@ -716,16 +783,11 @@ static irqreturn_t i2c_irq_handler(int irq, void *arg) | |||
716 | } | 783 | } |
717 | } | 784 | } |
718 | 785 | ||
719 | i2c_set_bit(dev->virtbase + I2C_ICR, I2C_IT_MTD); | 786 | disable_all_interrupts(dev); |
720 | i2c_set_bit(dev->virtbase + I2C_ICR, I2C_IT_MTDWS); | 787 | clear_all_interrupts(dev); |
721 | |||
722 | disable_interrupts(dev, | ||
723 | (I2C_IT_TXFNE | I2C_IT_TXFE | I2C_IT_TXFF | ||
724 | | I2C_IT_TXFOVR | I2C_IT_RXFNF | ||
725 | | I2C_IT_RXFF | I2C_IT_RXFE)); | ||
726 | 788 | ||
727 | if (dev->cli.count) { | 789 | if (dev->cli.count) { |
728 | dev->result = -1; | 790 | dev->result = -EIO; |
729 | dev_err(&dev->pdev->dev, "%lu bytes still remain to be" | 791 | dev_err(&dev->pdev->dev, "%lu bytes still remain to be" |
730 | "xfered\n", dev->cli.count); | 792 | "xfered\n", dev->cli.count); |
731 | (void) init_hw(dev); | 793 | (void) init_hw(dev); |
@@ -736,7 +798,7 @@ static irqreturn_t i2c_irq_handler(int irq, void *arg) | |||
736 | 798 | ||
737 | /* Master Arbitration lost interrupt */ | 799 | /* Master Arbitration lost interrupt */ |
738 | case I2C_IT_MAL: | 800 | case I2C_IT_MAL: |
739 | dev->result = -1; | 801 | dev->result = -EIO; |
740 | (void) init_hw(dev); | 802 | (void) init_hw(dev); |
741 | 803 | ||
742 | i2c_set_bit(dev->virtbase + I2C_ICR, I2C_IT_MAL); | 804 | i2c_set_bit(dev->virtbase + I2C_ICR, I2C_IT_MAL); |
@@ -750,7 +812,7 @@ static irqreturn_t i2c_irq_handler(int irq, void *arg) | |||
750 | * during the transaction. | 812 | * during the transaction. |
751 | */ | 813 | */ |
752 | case I2C_IT_BERR: | 814 | case I2C_IT_BERR: |
753 | dev->result = -1; | 815 | dev->result = -EIO; |
754 | /* get the status */ | 816 | /* get the status */ |
755 | if (((readl(dev->virtbase + I2C_SR) >> 2) & 0x3) == I2C_ABORT) | 817 | if (((readl(dev->virtbase + I2C_SR) >> 2) & 0x3) == I2C_ABORT) |
756 | (void) init_hw(dev); | 818 | (void) init_hw(dev); |
@@ -766,7 +828,7 @@ static irqreturn_t i2c_irq_handler(int irq, void *arg) | |||
766 | * the Tx FIFO is full. | 828 | * the Tx FIFO is full. |
767 | */ | 829 | */ |
768 | case I2C_IT_TXFOVR: | 830 | case I2C_IT_TXFOVR: |
769 | dev->result = -1; | 831 | dev->result = -EIO; |
770 | (void) init_hw(dev); | 832 | (void) init_hw(dev); |
771 | 833 | ||
772 | dev_err(&dev->pdev->dev, "Tx Fifo Over run\n"); | 834 | dev_err(&dev->pdev->dev, "Tx Fifo Over run\n"); |
@@ -792,12 +854,41 @@ static irqreturn_t i2c_irq_handler(int irq, void *arg) | |||
792 | return IRQ_HANDLED; | 854 | return IRQ_HANDLED; |
793 | } | 855 | } |
794 | 856 | ||
857 | |||
858 | #ifdef CONFIG_PM | ||
859 | static int nmk_i2c_suspend(struct device *dev) | ||
860 | { | ||
861 | struct platform_device *pdev = to_platform_device(dev); | ||
862 | struct nmk_i2c_dev *nmk_i2c = platform_get_drvdata(pdev); | ||
863 | |||
864 | if (nmk_i2c->busy) | ||
865 | return -EBUSY; | ||
866 | |||
867 | return 0; | ||
868 | } | ||
869 | |||
870 | static int nmk_i2c_resume(struct device *dev) | ||
871 | { | ||
872 | return 0; | ||
873 | } | ||
874 | #else | ||
875 | #define nmk_i2c_suspend NULL | ||
876 | #define nmk_i2c_resume NULL | ||
877 | #endif | ||
878 | |||
879 | /* | ||
880 | * We use noirq so that we suspend late and resume before the wakeup interrupt | ||
881 | * to ensure that we do the !pm_runtime_suspended() check in resume before | ||
882 | * there has been a regular pm runtime resume (via pm_runtime_get_sync()). | ||
883 | */ | ||
884 | static const struct dev_pm_ops nmk_i2c_pm = { | ||
885 | .suspend_noirq = nmk_i2c_suspend, | ||
886 | .resume_noirq = nmk_i2c_resume, | ||
887 | }; | ||
888 | |||
795 | static unsigned int nmk_i2c_functionality(struct i2c_adapter *adap) | 889 | static unsigned int nmk_i2c_functionality(struct i2c_adapter *adap) |
796 | { | 890 | { |
797 | return I2C_FUNC_I2C | 891 | return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL; |
798 | | I2C_FUNC_SMBUS_BYTE_DATA | ||
799 | | I2C_FUNC_SMBUS_WORD_DATA | ||
800 | | I2C_FUNC_SMBUS_I2C_BLOCK; | ||
801 | } | 892 | } |
802 | 893 | ||
803 | static const struct i2c_algorithm nmk_i2c_algo = { | 894 | static const struct i2c_algorithm nmk_i2c_algo = { |
@@ -820,7 +911,7 @@ static int __devinit nmk_i2c_probe(struct platform_device *pdev) | |||
820 | ret = -ENOMEM; | 911 | ret = -ENOMEM; |
821 | goto err_no_mem; | 912 | goto err_no_mem; |
822 | } | 913 | } |
823 | 914 | dev->busy = false; | |
824 | dev->pdev = pdev; | 915 | dev->pdev = pdev; |
825 | platform_set_drvdata(pdev, dev); | 916 | platform_set_drvdata(pdev, dev); |
826 | 917 | ||
@@ -850,6 +941,15 @@ static int __devinit nmk_i2c_probe(struct platform_device *pdev) | |||
850 | goto err_irq; | 941 | goto err_irq; |
851 | } | 942 | } |
852 | 943 | ||
944 | dev->regulator = regulator_get(&pdev->dev, "v-i2c"); | ||
945 | if (IS_ERR(dev->regulator)) { | ||
946 | dev_warn(&pdev->dev, "could not get i2c regulator\n"); | ||
947 | dev->regulator = NULL; | ||
948 | } | ||
949 | |||
950 | pm_suspend_ignore_children(&pdev->dev, true); | ||
951 | pm_runtime_enable(&pdev->dev); | ||
952 | |||
853 | dev->clk = clk_get(&pdev->dev, NULL); | 953 | dev->clk = clk_get(&pdev->dev, NULL); |
854 | if (IS_ERR(dev->clk)) { | 954 | if (IS_ERR(dev->clk)) { |
855 | dev_err(&pdev->dev, "could not get i2c clock\n"); | 955 | dev_err(&pdev->dev, "could not get i2c clock\n"); |
@@ -857,13 +957,15 @@ static int __devinit nmk_i2c_probe(struct platform_device *pdev) | |||
857 | goto err_no_clk; | 957 | goto err_no_clk; |
858 | } | 958 | } |
859 | 959 | ||
860 | clk_enable(dev->clk); | ||
861 | |||
862 | adap = &dev->adap; | 960 | adap = &dev->adap; |
863 | adap->dev.parent = &pdev->dev; | 961 | adap->dev.parent = &pdev->dev; |
864 | adap->owner = THIS_MODULE; | 962 | adap->owner = THIS_MODULE; |
865 | adap->class = I2C_CLASS_HWMON | I2C_CLASS_SPD; | 963 | adap->class = I2C_CLASS_HWMON | I2C_CLASS_SPD; |
866 | adap->algo = &nmk_i2c_algo; | 964 | adap->algo = &nmk_i2c_algo; |
965 | adap->timeout = pdata->timeout ? msecs_to_jiffies(pdata->timeout) : | ||
966 | msecs_to_jiffies(20000); | ||
967 | snprintf(adap->name, sizeof(adap->name), | ||
968 | "Nomadik I2C%d at %lx", pdev->id, (unsigned long)res->start); | ||
867 | 969 | ||
868 | /* fetch the controller id */ | 970 | /* fetch the controller id */ |
869 | adap->nr = pdev->id; | 971 | adap->nr = pdev->id; |
@@ -877,14 +979,8 @@ static int __devinit nmk_i2c_probe(struct platform_device *pdev) | |||
877 | 979 | ||
878 | i2c_set_adapdata(adap, dev); | 980 | i2c_set_adapdata(adap, dev); |
879 | 981 | ||
880 | ret = init_hw(dev); | 982 | dev_info(&pdev->dev, "initialize %s on virtual " |
881 | if (ret != 0) { | 983 | "base %p\n", adap->name, dev->virtbase); |
882 | dev_err(&pdev->dev, "error in initializing i2c hardware\n"); | ||
883 | goto err_init_hw; | ||
884 | } | ||
885 | |||
886 | dev_dbg(&pdev->dev, "initialize I2C%d bus on virtual " | ||
887 | "base %p\n", pdev->id, dev->virtbase); | ||
888 | 984 | ||
889 | ret = i2c_add_numbered_adapter(adap); | 985 | ret = i2c_add_numbered_adapter(adap); |
890 | if (ret) { | 986 | if (ret) { |
@@ -894,11 +990,12 @@ static int __devinit nmk_i2c_probe(struct platform_device *pdev) | |||
894 | 990 | ||
895 | return 0; | 991 | return 0; |
896 | 992 | ||
897 | err_init_hw: | ||
898 | clk_disable(dev->clk); | ||
899 | err_add_adap: | 993 | err_add_adap: |
900 | clk_put(dev->clk); | 994 | clk_put(dev->clk); |
901 | err_no_clk: | 995 | err_no_clk: |
996 | if (dev->regulator) | ||
997 | regulator_put(dev->regulator); | ||
998 | pm_runtime_disable(&pdev->dev); | ||
902 | free_irq(dev->irq, dev); | 999 | free_irq(dev->irq, dev); |
903 | err_irq: | 1000 | err_irq: |
904 | iounmap(dev->virtbase); | 1001 | iounmap(dev->virtbase); |
@@ -928,8 +1025,10 @@ static int __devexit nmk_i2c_remove(struct platform_device *pdev) | |||
928 | iounmap(dev->virtbase); | 1025 | iounmap(dev->virtbase); |
929 | if (res) | 1026 | if (res) |
930 | release_mem_region(res->start, resource_size(res)); | 1027 | release_mem_region(res->start, resource_size(res)); |
931 | clk_disable(dev->clk); | ||
932 | clk_put(dev->clk); | 1028 | clk_put(dev->clk); |
1029 | if (dev->regulator) | ||
1030 | regulator_put(dev->regulator); | ||
1031 | pm_runtime_disable(&pdev->dev); | ||
933 | platform_set_drvdata(pdev, NULL); | 1032 | platform_set_drvdata(pdev, NULL); |
934 | kfree(dev); | 1033 | kfree(dev); |
935 | 1034 | ||
@@ -940,6 +1039,7 @@ static struct platform_driver nmk_i2c_driver = { | |||
940 | .driver = { | 1039 | .driver = { |
941 | .owner = THIS_MODULE, | 1040 | .owner = THIS_MODULE, |
942 | .name = DRIVER_NAME, | 1041 | .name = DRIVER_NAME, |
1042 | .pm = &nmk_i2c_pm, | ||
943 | }, | 1043 | }, |
944 | .probe = nmk_i2c_probe, | 1044 | .probe = nmk_i2c_probe, |
945 | .remove = __devexit_p(nmk_i2c_remove), | 1045 | .remove = __devexit_p(nmk_i2c_remove), |