| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
|---|
| 1 | 2 | /* |
|---|
| 2 | 3 | * i2c-xiic.c |
|---|
| 3 | 4 | * Copyright (c) 2002-2007 Xilinx Inc. |
|---|
| 4 | 5 | * Copyright (c) 2009-2010 Intel Corporation |
|---|
| 5 | | - * |
|---|
| 6 | | - * This program is free software; you can redistribute it and/or modify |
|---|
| 7 | | - * it under the terms of the GNU General Public License version 2 as |
|---|
| 8 | | - * published by the Free Software Foundation. |
|---|
| 9 | | - * |
|---|
| 10 | | - * This program is distributed in the hope that it will be useful, |
|---|
| 11 | | - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 12 | | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 13 | | - * GNU General Public License for more details. |
|---|
| 14 | | - * |
|---|
| 15 | 6 | * |
|---|
| 16 | 7 | * This code was implemented by Mocean Laboratories AB when porting linux |
|---|
| 17 | 8 | * to the automotive development board Russellville. The copyright holder |
|---|
| .. | .. |
|---|
| 55 | 46 | |
|---|
| 56 | 47 | /** |
|---|
| 57 | 48 | * struct xiic_i2c - Internal representation of the XIIC I2C bus |
|---|
| 58 | | - * @base: Memory base of the HW registers |
|---|
| 59 | | - * @wait: Wait queue for callers |
|---|
| 60 | | - * @adap: Kernel adapter representation |
|---|
| 61 | | - * @tx_msg: Messages from above to be sent |
|---|
| 62 | | - * @lock: Mutual exclusion |
|---|
| 63 | | - * @tx_pos: Current pos in TX message |
|---|
| 64 | | - * @nmsgs: Number of messages in tx_msg |
|---|
| 65 | | - * @state: See STATE_ |
|---|
| 66 | | - * @rx_msg: Current RX message |
|---|
| 67 | | - * @rx_pos: Position within current RX message |
|---|
| 49 | + * @dev: Pointer to device structure |
|---|
| 50 | + * @base: Memory base of the HW registers |
|---|
| 51 | + * @wait: Wait queue for callers |
|---|
| 52 | + * @adap: Kernel adapter representation |
|---|
| 53 | + * @tx_msg: Messages from above to be sent |
|---|
| 54 | + * @lock: Mutual exclusion |
|---|
| 55 | + * @tx_pos: Current pos in TX message |
|---|
| 56 | + * @nmsgs: Number of messages in tx_msg |
|---|
| 57 | + * @rx_msg: Current RX message |
|---|
| 58 | + * @rx_pos: Position within current RX message |
|---|
| 68 | 59 | * @endianness: big/little-endian byte order |
|---|
| 60 | + * @clk: Pointer to AXI4-lite input clock |
|---|
| 61 | + * @state: See STATE_ |
|---|
| 62 | + * @singlemaster: Indicates bus is single master |
|---|
| 69 | 63 | */ |
|---|
| 70 | 64 | struct xiic_i2c { |
|---|
| 71 | | - struct device *dev; |
|---|
| 72 | | - void __iomem *base; |
|---|
| 73 | | - wait_queue_head_t wait; |
|---|
| 74 | | - struct i2c_adapter adap; |
|---|
| 75 | | - struct i2c_msg *tx_msg; |
|---|
| 76 | | - struct mutex lock; |
|---|
| 77 | | - unsigned int tx_pos; |
|---|
| 78 | | - unsigned int nmsgs; |
|---|
| 79 | | - enum xilinx_i2c_state state; |
|---|
| 80 | | - struct i2c_msg *rx_msg; |
|---|
| 81 | | - int rx_pos; |
|---|
| 82 | | - enum xiic_endian endianness; |
|---|
| 65 | + struct device *dev; |
|---|
| 66 | + void __iomem *base; |
|---|
| 67 | + wait_queue_head_t wait; |
|---|
| 68 | + struct i2c_adapter adap; |
|---|
| 69 | + struct i2c_msg *tx_msg; |
|---|
| 70 | + struct mutex lock; |
|---|
| 71 | + unsigned int tx_pos; |
|---|
| 72 | + unsigned int nmsgs; |
|---|
| 73 | + struct i2c_msg *rx_msg; |
|---|
| 74 | + int rx_pos; |
|---|
| 75 | + enum xiic_endian endianness; |
|---|
| 83 | 76 | struct clk *clk; |
|---|
| 77 | + enum xilinx_i2c_state state; |
|---|
| 78 | + bool singlemaster; |
|---|
| 84 | 79 | }; |
|---|
| 85 | 80 | |
|---|
| 86 | 81 | |
|---|
| .. | .. |
|---|
| 163 | 158 | #define XIIC_RESET_MASK 0xAUL |
|---|
| 164 | 159 | |
|---|
| 165 | 160 | #define XIIC_PM_TIMEOUT 1000 /* ms */ |
|---|
| 161 | +/* timeout waiting for the controller to respond */ |
|---|
| 162 | +#define XIIC_I2C_TIMEOUT (msecs_to_jiffies(1000)) |
|---|
| 166 | 163 | /* |
|---|
| 167 | 164 | * The following constant is used for the device global interrupt enable |
|---|
| 168 | 165 | * register, to enable all interrupts for the device, this is the only bit |
|---|
| .. | .. |
|---|
| 173 | 170 | #define xiic_tx_space(i2c) ((i2c)->tx_msg->len - (i2c)->tx_pos) |
|---|
| 174 | 171 | #define xiic_rx_space(i2c) ((i2c)->rx_msg->len - (i2c)->rx_pos) |
|---|
| 175 | 172 | |
|---|
| 176 | | -static void xiic_start_xfer(struct xiic_i2c *i2c); |
|---|
| 173 | +static int xiic_start_xfer(struct xiic_i2c *i2c); |
|---|
| 177 | 174 | static void __xiic_start_xfer(struct xiic_i2c *i2c); |
|---|
| 178 | 175 | |
|---|
| 179 | 176 | /* |
|---|
| .. | .. |
|---|
| 254 | 251 | xiic_irq_en(i2c, mask); |
|---|
| 255 | 252 | } |
|---|
| 256 | 253 | |
|---|
| 257 | | -static void xiic_clear_rx_fifo(struct xiic_i2c *i2c) |
|---|
| 254 | +static int xiic_clear_rx_fifo(struct xiic_i2c *i2c) |
|---|
| 258 | 255 | { |
|---|
| 259 | 256 | u8 sr; |
|---|
| 257 | + unsigned long timeout; |
|---|
| 258 | + |
|---|
| 259 | + timeout = jiffies + XIIC_I2C_TIMEOUT; |
|---|
| 260 | 260 | for (sr = xiic_getreg8(i2c, XIIC_SR_REG_OFFSET); |
|---|
| 261 | 261 | !(sr & XIIC_SR_RX_FIFO_EMPTY_MASK); |
|---|
| 262 | | - sr = xiic_getreg8(i2c, XIIC_SR_REG_OFFSET)) |
|---|
| 262 | + sr = xiic_getreg8(i2c, XIIC_SR_REG_OFFSET)) { |
|---|
| 263 | 263 | xiic_getreg8(i2c, XIIC_DRR_REG_OFFSET); |
|---|
| 264 | + if (time_after(jiffies, timeout)) { |
|---|
| 265 | + dev_err(i2c->dev, "Failed to clear rx fifo\n"); |
|---|
| 266 | + return -ETIMEDOUT; |
|---|
| 267 | + } |
|---|
| 268 | + } |
|---|
| 269 | + |
|---|
| 270 | + return 0; |
|---|
| 264 | 271 | } |
|---|
| 265 | 272 | |
|---|
| 266 | | -static void xiic_reinit(struct xiic_i2c *i2c) |
|---|
| 273 | +static int xiic_reinit(struct xiic_i2c *i2c) |
|---|
| 267 | 274 | { |
|---|
| 275 | + int ret; |
|---|
| 276 | + |
|---|
| 268 | 277 | xiic_setreg32(i2c, XIIC_RESETR_OFFSET, XIIC_RESET_MASK); |
|---|
| 269 | 278 | |
|---|
| 270 | 279 | /* Set receive Fifo depth to maximum (zero based). */ |
|---|
| .. | .. |
|---|
| 277 | 286 | xiic_setreg8(i2c, XIIC_CR_REG_OFFSET, XIIC_CR_ENABLE_DEVICE_MASK); |
|---|
| 278 | 287 | |
|---|
| 279 | 288 | /* make sure RX fifo is empty */ |
|---|
| 280 | | - xiic_clear_rx_fifo(i2c); |
|---|
| 289 | + ret = xiic_clear_rx_fifo(i2c); |
|---|
| 290 | + if (ret) |
|---|
| 291 | + return ret; |
|---|
| 281 | 292 | |
|---|
| 282 | 293 | /* Enable interrupts */ |
|---|
| 283 | 294 | xiic_setreg32(i2c, XIIC_DGIER_OFFSET, XIIC_GINTR_ENABLE_MASK); |
|---|
| 284 | 295 | |
|---|
| 285 | 296 | xiic_irq_clr_en(i2c, XIIC_INTR_ARB_LOST_MASK); |
|---|
| 297 | + |
|---|
| 298 | + return 0; |
|---|
| 286 | 299 | } |
|---|
| 287 | 300 | |
|---|
| 288 | 301 | static void xiic_deinit(struct xiic_i2c *i2c) |
|---|
| .. | .. |
|---|
| 515 | 528 | if (i2c->tx_msg) |
|---|
| 516 | 529 | return -EBUSY; |
|---|
| 517 | 530 | |
|---|
| 531 | + /* In single master mode bus can only be busy, when in use by this |
|---|
| 532 | + * driver. If the register indicates bus being busy for some reason we |
|---|
| 533 | + * should ignore it, since bus will never be released and i2c will be |
|---|
| 534 | + * stuck forever. |
|---|
| 535 | + */ |
|---|
| 536 | + if (i2c->singlemaster) { |
|---|
| 537 | + return 0; |
|---|
| 538 | + } |
|---|
| 539 | + |
|---|
| 518 | 540 | /* for instance if previous transfer was terminated due to TX error |
|---|
| 519 | 541 | * it might be that the bus is on it's way to become available |
|---|
| 520 | 542 | * give it at most 3 ms to wake |
|---|
| .. | .. |
|---|
| 662 | 684 | |
|---|
| 663 | 685 | } |
|---|
| 664 | 686 | |
|---|
| 665 | | -static void xiic_start_xfer(struct xiic_i2c *i2c) |
|---|
| 687 | +static int xiic_start_xfer(struct xiic_i2c *i2c) |
|---|
| 666 | 688 | { |
|---|
| 689 | + int ret; |
|---|
| 667 | 690 | mutex_lock(&i2c->lock); |
|---|
| 668 | | - xiic_reinit(i2c); |
|---|
| 669 | | - __xiic_start_xfer(i2c); |
|---|
| 691 | + |
|---|
| 692 | + ret = xiic_reinit(i2c); |
|---|
| 693 | + if (!ret) |
|---|
| 694 | + __xiic_start_xfer(i2c); |
|---|
| 695 | + |
|---|
| 670 | 696 | mutex_unlock(&i2c->lock); |
|---|
| 697 | + |
|---|
| 698 | + return ret; |
|---|
| 671 | 699 | } |
|---|
| 672 | 700 | |
|---|
| 673 | 701 | static int xiic_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num) |
|---|
| .. | .. |
|---|
| 678 | 706 | dev_dbg(adap->dev.parent, "%s entry SR: 0x%x\n", __func__, |
|---|
| 679 | 707 | xiic_getreg8(i2c, XIIC_SR_REG_OFFSET)); |
|---|
| 680 | 708 | |
|---|
| 681 | | - err = pm_runtime_get_sync(i2c->dev); |
|---|
| 709 | + err = pm_runtime_resume_and_get(i2c->dev); |
|---|
| 682 | 710 | if (err < 0) |
|---|
| 683 | 711 | return err; |
|---|
| 684 | 712 | |
|---|
| .. | .. |
|---|
| 689 | 717 | i2c->tx_msg = msgs; |
|---|
| 690 | 718 | i2c->nmsgs = num; |
|---|
| 691 | 719 | |
|---|
| 692 | | - xiic_start_xfer(i2c); |
|---|
| 720 | + err = xiic_start_xfer(i2c); |
|---|
| 721 | + if (err < 0) { |
|---|
| 722 | + dev_err(adap->dev.parent, "Error xiic_start_xfer\n"); |
|---|
| 723 | + goto out; |
|---|
| 724 | + } |
|---|
| 693 | 725 | |
|---|
| 694 | 726 | if (wait_event_timeout(i2c->wait, (i2c->state == STATE_ERROR) || |
|---|
| 695 | 727 | (i2c->state == STATE_DONE), HZ)) { |
|---|
| .. | .. |
|---|
| 724 | 756 | |
|---|
| 725 | 757 | static const struct i2c_adapter xiic_adapter = { |
|---|
| 726 | 758 | .owner = THIS_MODULE, |
|---|
| 727 | | - .name = DRIVER_NAME, |
|---|
| 728 | 759 | .class = I2C_CLASS_DEPRECATED, |
|---|
| 729 | 760 | .algo = &xiic_algorithm, |
|---|
| 730 | 761 | .quirks = &xiic_quirks, |
|---|
| .. | .. |
|---|
| 761 | 792 | i2c_set_adapdata(&i2c->adap, i2c); |
|---|
| 762 | 793 | i2c->adap.dev.parent = &pdev->dev; |
|---|
| 763 | 794 | i2c->adap.dev.of_node = pdev->dev.of_node; |
|---|
| 795 | + snprintf(i2c->adap.name, sizeof(i2c->adap.name), |
|---|
| 796 | + DRIVER_NAME " %s", pdev->name); |
|---|
| 764 | 797 | |
|---|
| 765 | 798 | mutex_init(&i2c->lock); |
|---|
| 766 | 799 | init_waitqueue_head(&i2c->wait); |
|---|
| 767 | 800 | |
|---|
| 768 | 801 | i2c->clk = devm_clk_get(&pdev->dev, NULL); |
|---|
| 769 | 802 | if (IS_ERR(i2c->clk)) { |
|---|
| 770 | | - dev_err(&pdev->dev, "input clock not found.\n"); |
|---|
| 803 | + if (PTR_ERR(i2c->clk) != -EPROBE_DEFER) |
|---|
| 804 | + dev_err(&pdev->dev, "input clock not found.\n"); |
|---|
| 771 | 805 | return PTR_ERR(i2c->clk); |
|---|
| 772 | 806 | } |
|---|
| 773 | 807 | ret = clk_prepare_enable(i2c->clk); |
|---|
| .. | .. |
|---|
| 776 | 810 | return ret; |
|---|
| 777 | 811 | } |
|---|
| 778 | 812 | i2c->dev = &pdev->dev; |
|---|
| 779 | | - pm_runtime_enable(i2c->dev); |
|---|
| 780 | 813 | pm_runtime_set_autosuspend_delay(i2c->dev, XIIC_PM_TIMEOUT); |
|---|
| 781 | 814 | pm_runtime_use_autosuspend(i2c->dev); |
|---|
| 782 | 815 | pm_runtime_set_active(i2c->dev); |
|---|
| 816 | + pm_runtime_enable(i2c->dev); |
|---|
| 783 | 817 | ret = devm_request_threaded_irq(&pdev->dev, irq, xiic_isr, |
|---|
| 784 | 818 | xiic_process, IRQF_ONESHOT, |
|---|
| 785 | 819 | pdev->name, i2c); |
|---|
| .. | .. |
|---|
| 788 | 822 | dev_err(&pdev->dev, "Cannot claim IRQ\n"); |
|---|
| 789 | 823 | goto err_clk_dis; |
|---|
| 790 | 824 | } |
|---|
| 825 | + |
|---|
| 826 | + i2c->singlemaster = |
|---|
| 827 | + of_property_read_bool(pdev->dev.of_node, "single-master"); |
|---|
| 791 | 828 | |
|---|
| 792 | 829 | /* |
|---|
| 793 | 830 | * Detect endianness |
|---|
| .. | .. |
|---|
| 801 | 838 | if (!(sr & XIIC_SR_TX_FIFO_EMPTY_MASK)) |
|---|
| 802 | 839 | i2c->endianness = BIG; |
|---|
| 803 | 840 | |
|---|
| 804 | | - xiic_reinit(i2c); |
|---|
| 841 | + ret = xiic_reinit(i2c); |
|---|
| 842 | + if (ret < 0) { |
|---|
| 843 | + dev_err(&pdev->dev, "Cannot xiic_reinit\n"); |
|---|
| 844 | + goto err_clk_dis; |
|---|
| 845 | + } |
|---|
| 805 | 846 | |
|---|
| 806 | 847 | /* add i2c adapter to i2c tree */ |
|---|
| 807 | 848 | ret = i2c_add_adapter(&i2c->adap); |
|---|
| .. | .. |
|---|
| 813 | 854 | if (pdata) { |
|---|
| 814 | 855 | /* add in known devices to the bus */ |
|---|
| 815 | 856 | for (i = 0; i < pdata->num_devices; i++) |
|---|
| 816 | | - i2c_new_device(&i2c->adap, pdata->devices + i); |
|---|
| 857 | + i2c_new_client_device(&i2c->adap, pdata->devices + i); |
|---|
| 817 | 858 | } |
|---|
| 818 | 859 | |
|---|
| 819 | 860 | return 0; |
|---|
| .. | .. |
|---|
| 833 | 874 | /* remove adapter & data */ |
|---|
| 834 | 875 | i2c_del_adapter(&i2c->adap); |
|---|
| 835 | 876 | |
|---|
| 836 | | - ret = clk_prepare_enable(i2c->clk); |
|---|
| 837 | | - if (ret) { |
|---|
| 838 | | - dev_err(&pdev->dev, "Unable to enable clock.\n"); |
|---|
| 877 | + ret = pm_runtime_resume_and_get(i2c->dev); |
|---|
| 878 | + if (ret < 0) |
|---|
| 839 | 879 | return ret; |
|---|
| 840 | | - } |
|---|
| 880 | + |
|---|
| 841 | 881 | xiic_deinit(i2c); |
|---|
| 882 | + pm_runtime_put_sync(i2c->dev); |
|---|
| 842 | 883 | clk_disable_unprepare(i2c->clk); |
|---|
| 843 | 884 | pm_runtime_disable(&pdev->dev); |
|---|
| 885 | + pm_runtime_set_suspended(&pdev->dev); |
|---|
| 886 | + pm_runtime_dont_use_autosuspend(&pdev->dev); |
|---|
| 844 | 887 | |
|---|
| 845 | 888 | return 0; |
|---|
| 846 | 889 | } |
|---|
| .. | .. |
|---|
| 892 | 935 | |
|---|
| 893 | 936 | module_platform_driver(xiic_i2c_driver); |
|---|
| 894 | 937 | |
|---|
| 938 | +MODULE_ALIAS("platform:" DRIVER_NAME); |
|---|
| 895 | 939 | MODULE_AUTHOR("info@mocean-labs.com"); |
|---|
| 896 | 940 | MODULE_DESCRIPTION("Xilinx I2C bus driver"); |
|---|
| 897 | 941 | MODULE_LICENSE("GPL v2"); |
|---|