| .. | .. |
|---|
| 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) |
|---|
| .. | .. |
|---|
| 362 | 375 | struct xiic_i2c *i2c = dev_id; |
|---|
| 363 | 376 | u32 pend, isr, ier; |
|---|
| 364 | 377 | u32 clr = 0; |
|---|
| 378 | + int xfer_more = 0; |
|---|
| 379 | + int wakeup_req = 0; |
|---|
| 380 | + int wakeup_code = 0; |
|---|
| 365 | 381 | |
|---|
| 366 | 382 | /* Get the interrupt Status from the IPIF. There is no clearing of |
|---|
| 367 | 383 | * interrupts in the IPIF. Interrupts must be cleared at the source. |
|---|
| .. | .. |
|---|
| 398 | 414 | */ |
|---|
| 399 | 415 | xiic_reinit(i2c); |
|---|
| 400 | 416 | |
|---|
| 401 | | - if (i2c->rx_msg) |
|---|
| 402 | | - xiic_wakeup(i2c, STATE_ERROR); |
|---|
| 403 | | - if (i2c->tx_msg) |
|---|
| 404 | | - xiic_wakeup(i2c, STATE_ERROR); |
|---|
| 417 | + if (i2c->rx_msg) { |
|---|
| 418 | + wakeup_req = 1; |
|---|
| 419 | + wakeup_code = STATE_ERROR; |
|---|
| 420 | + } |
|---|
| 421 | + if (i2c->tx_msg) { |
|---|
| 422 | + wakeup_req = 1; |
|---|
| 423 | + wakeup_code = STATE_ERROR; |
|---|
| 424 | + } |
|---|
| 425 | + /* don't try to handle other events */ |
|---|
| 426 | + goto out; |
|---|
| 405 | 427 | } |
|---|
| 406 | 428 | if (pend & XIIC_INTR_RX_FULL_MASK) { |
|---|
| 407 | 429 | /* Receive register/FIFO is full */ |
|---|
| .. | .. |
|---|
| 435 | 457 | i2c->tx_msg++; |
|---|
| 436 | 458 | dev_dbg(i2c->adap.dev.parent, |
|---|
| 437 | 459 | "%s will start next...\n", __func__); |
|---|
| 438 | | - |
|---|
| 439 | | - __xiic_start_xfer(i2c); |
|---|
| 460 | + xfer_more = 1; |
|---|
| 440 | 461 | } |
|---|
| 441 | 462 | } |
|---|
| 442 | 463 | } |
|---|
| .. | .. |
|---|
| 450 | 471 | if (!i2c->tx_msg) |
|---|
| 451 | 472 | goto out; |
|---|
| 452 | 473 | |
|---|
| 453 | | - if ((i2c->nmsgs == 1) && !i2c->rx_msg && |
|---|
| 454 | | - xiic_tx_space(i2c) == 0) |
|---|
| 455 | | - xiic_wakeup(i2c, STATE_DONE); |
|---|
| 474 | + wakeup_req = 1; |
|---|
| 475 | + |
|---|
| 476 | + if (i2c->nmsgs == 1 && !i2c->rx_msg && |
|---|
| 477 | + xiic_tx_space(i2c) == 0) |
|---|
| 478 | + wakeup_code = STATE_DONE; |
|---|
| 456 | 479 | else |
|---|
| 457 | | - xiic_wakeup(i2c, STATE_ERROR); |
|---|
| 480 | + wakeup_code = STATE_ERROR; |
|---|
| 458 | 481 | } |
|---|
| 459 | 482 | if (pend & (XIIC_INTR_TX_EMPTY_MASK | XIIC_INTR_TX_HALF_MASK)) { |
|---|
| 460 | 483 | /* Transmit register/FIFO is empty or ½ empty */ |
|---|
| .. | .. |
|---|
| 478 | 501 | if (i2c->nmsgs > 1) { |
|---|
| 479 | 502 | i2c->nmsgs--; |
|---|
| 480 | 503 | i2c->tx_msg++; |
|---|
| 481 | | - __xiic_start_xfer(i2c); |
|---|
| 504 | + xfer_more = 1; |
|---|
| 482 | 505 | } else { |
|---|
| 483 | 506 | xiic_irq_dis(i2c, XIIC_INTR_TX_HALF_MASK); |
|---|
| 484 | 507 | |
|---|
| .. | .. |
|---|
| 496 | 519 | dev_dbg(i2c->adap.dev.parent, "%s clr: 0x%x\n", __func__, clr); |
|---|
| 497 | 520 | |
|---|
| 498 | 521 | xiic_setreg32(i2c, XIIC_IISR_OFFSET, clr); |
|---|
| 522 | + if (xfer_more) |
|---|
| 523 | + __xiic_start_xfer(i2c); |
|---|
| 524 | + if (wakeup_req) |
|---|
| 525 | + xiic_wakeup(i2c, wakeup_code); |
|---|
| 526 | + |
|---|
| 527 | + WARN_ON(xfer_more && wakeup_req); |
|---|
| 528 | + |
|---|
| 499 | 529 | mutex_unlock(&i2c->lock); |
|---|
| 500 | 530 | return IRQ_HANDLED; |
|---|
| 501 | 531 | } |
|---|
| .. | .. |
|---|
| 514 | 544 | |
|---|
| 515 | 545 | if (i2c->tx_msg) |
|---|
| 516 | 546 | return -EBUSY; |
|---|
| 547 | + |
|---|
| 548 | + /* In single master mode bus can only be busy, when in use by this |
|---|
| 549 | + * driver. If the register indicates bus being busy for some reason we |
|---|
| 550 | + * should ignore it, since bus will never be released and i2c will be |
|---|
| 551 | + * stuck forever. |
|---|
| 552 | + */ |
|---|
| 553 | + if (i2c->singlemaster) { |
|---|
| 554 | + return 0; |
|---|
| 555 | + } |
|---|
| 517 | 556 | |
|---|
| 518 | 557 | /* for instance if previous transfer was terminated due to TX error |
|---|
| 519 | 558 | * it might be that the bus is on it's way to become available |
|---|
| .. | .. |
|---|
| 662 | 701 | |
|---|
| 663 | 702 | } |
|---|
| 664 | 703 | |
|---|
| 665 | | -static void xiic_start_xfer(struct xiic_i2c *i2c) |
|---|
| 704 | +static int xiic_start_xfer(struct xiic_i2c *i2c) |
|---|
| 666 | 705 | { |
|---|
| 706 | + int ret; |
|---|
| 667 | 707 | mutex_lock(&i2c->lock); |
|---|
| 668 | | - xiic_reinit(i2c); |
|---|
| 669 | | - __xiic_start_xfer(i2c); |
|---|
| 708 | + |
|---|
| 709 | + ret = xiic_reinit(i2c); |
|---|
| 710 | + if (!ret) |
|---|
| 711 | + __xiic_start_xfer(i2c); |
|---|
| 712 | + |
|---|
| 670 | 713 | mutex_unlock(&i2c->lock); |
|---|
| 714 | + |
|---|
| 715 | + return ret; |
|---|
| 671 | 716 | } |
|---|
| 672 | 717 | |
|---|
| 673 | 718 | static int xiic_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num) |
|---|
| .. | .. |
|---|
| 678 | 723 | dev_dbg(adap->dev.parent, "%s entry SR: 0x%x\n", __func__, |
|---|
| 679 | 724 | xiic_getreg8(i2c, XIIC_SR_REG_OFFSET)); |
|---|
| 680 | 725 | |
|---|
| 681 | | - err = pm_runtime_get_sync(i2c->dev); |
|---|
| 726 | + err = pm_runtime_resume_and_get(i2c->dev); |
|---|
| 682 | 727 | if (err < 0) |
|---|
| 683 | 728 | return err; |
|---|
| 684 | 729 | |
|---|
| .. | .. |
|---|
| 689 | 734 | i2c->tx_msg = msgs; |
|---|
| 690 | 735 | i2c->nmsgs = num; |
|---|
| 691 | 736 | |
|---|
| 692 | | - xiic_start_xfer(i2c); |
|---|
| 737 | + err = xiic_start_xfer(i2c); |
|---|
| 738 | + if (err < 0) { |
|---|
| 739 | + dev_err(adap->dev.parent, "Error xiic_start_xfer\n"); |
|---|
| 740 | + goto out; |
|---|
| 741 | + } |
|---|
| 693 | 742 | |
|---|
| 694 | 743 | if (wait_event_timeout(i2c->wait, (i2c->state == STATE_ERROR) || |
|---|
| 695 | 744 | (i2c->state == STATE_DONE), HZ)) { |
|---|
| .. | .. |
|---|
| 724 | 773 | |
|---|
| 725 | 774 | static const struct i2c_adapter xiic_adapter = { |
|---|
| 726 | 775 | .owner = THIS_MODULE, |
|---|
| 727 | | - .name = DRIVER_NAME, |
|---|
| 728 | 776 | .class = I2C_CLASS_DEPRECATED, |
|---|
| 729 | 777 | .algo = &xiic_algorithm, |
|---|
| 730 | 778 | .quirks = &xiic_quirks, |
|---|
| .. | .. |
|---|
| 761 | 809 | i2c_set_adapdata(&i2c->adap, i2c); |
|---|
| 762 | 810 | i2c->adap.dev.parent = &pdev->dev; |
|---|
| 763 | 811 | i2c->adap.dev.of_node = pdev->dev.of_node; |
|---|
| 812 | + snprintf(i2c->adap.name, sizeof(i2c->adap.name), |
|---|
| 813 | + DRIVER_NAME " %s", pdev->name); |
|---|
| 764 | 814 | |
|---|
| 765 | 815 | mutex_init(&i2c->lock); |
|---|
| 766 | 816 | init_waitqueue_head(&i2c->wait); |
|---|
| 767 | 817 | |
|---|
| 768 | 818 | i2c->clk = devm_clk_get(&pdev->dev, NULL); |
|---|
| 769 | 819 | if (IS_ERR(i2c->clk)) { |
|---|
| 770 | | - dev_err(&pdev->dev, "input clock not found.\n"); |
|---|
| 820 | + if (PTR_ERR(i2c->clk) != -EPROBE_DEFER) |
|---|
| 821 | + dev_err(&pdev->dev, "input clock not found.\n"); |
|---|
| 771 | 822 | return PTR_ERR(i2c->clk); |
|---|
| 772 | 823 | } |
|---|
| 773 | 824 | ret = clk_prepare_enable(i2c->clk); |
|---|
| .. | .. |
|---|
| 776 | 827 | return ret; |
|---|
| 777 | 828 | } |
|---|
| 778 | 829 | i2c->dev = &pdev->dev; |
|---|
| 779 | | - pm_runtime_enable(i2c->dev); |
|---|
| 780 | 830 | pm_runtime_set_autosuspend_delay(i2c->dev, XIIC_PM_TIMEOUT); |
|---|
| 781 | 831 | pm_runtime_use_autosuspend(i2c->dev); |
|---|
| 782 | 832 | pm_runtime_set_active(i2c->dev); |
|---|
| 833 | + pm_runtime_enable(i2c->dev); |
|---|
| 783 | 834 | ret = devm_request_threaded_irq(&pdev->dev, irq, xiic_isr, |
|---|
| 784 | 835 | xiic_process, IRQF_ONESHOT, |
|---|
| 785 | 836 | pdev->name, i2c); |
|---|
| .. | .. |
|---|
| 788 | 839 | dev_err(&pdev->dev, "Cannot claim IRQ\n"); |
|---|
| 789 | 840 | goto err_clk_dis; |
|---|
| 790 | 841 | } |
|---|
| 842 | + |
|---|
| 843 | + i2c->singlemaster = |
|---|
| 844 | + of_property_read_bool(pdev->dev.of_node, "single-master"); |
|---|
| 791 | 845 | |
|---|
| 792 | 846 | /* |
|---|
| 793 | 847 | * Detect endianness |
|---|
| .. | .. |
|---|
| 801 | 855 | if (!(sr & XIIC_SR_TX_FIFO_EMPTY_MASK)) |
|---|
| 802 | 856 | i2c->endianness = BIG; |
|---|
| 803 | 857 | |
|---|
| 804 | | - xiic_reinit(i2c); |
|---|
| 858 | + ret = xiic_reinit(i2c); |
|---|
| 859 | + if (ret < 0) { |
|---|
| 860 | + dev_err(&pdev->dev, "Cannot xiic_reinit\n"); |
|---|
| 861 | + goto err_clk_dis; |
|---|
| 862 | + } |
|---|
| 805 | 863 | |
|---|
| 806 | 864 | /* add i2c adapter to i2c tree */ |
|---|
| 807 | 865 | ret = i2c_add_adapter(&i2c->adap); |
|---|
| .. | .. |
|---|
| 813 | 871 | if (pdata) { |
|---|
| 814 | 872 | /* add in known devices to the bus */ |
|---|
| 815 | 873 | for (i = 0; i < pdata->num_devices; i++) |
|---|
| 816 | | - i2c_new_device(&i2c->adap, pdata->devices + i); |
|---|
| 874 | + i2c_new_client_device(&i2c->adap, pdata->devices + i); |
|---|
| 817 | 875 | } |
|---|
| 818 | 876 | |
|---|
| 819 | 877 | return 0; |
|---|
| .. | .. |
|---|
| 833 | 891 | /* remove adapter & data */ |
|---|
| 834 | 892 | i2c_del_adapter(&i2c->adap); |
|---|
| 835 | 893 | |
|---|
| 836 | | - ret = clk_prepare_enable(i2c->clk); |
|---|
| 837 | | - if (ret) { |
|---|
| 838 | | - dev_err(&pdev->dev, "Unable to enable clock.\n"); |
|---|
| 894 | + ret = pm_runtime_resume_and_get(i2c->dev); |
|---|
| 895 | + if (ret < 0) |
|---|
| 839 | 896 | return ret; |
|---|
| 840 | | - } |
|---|
| 897 | + |
|---|
| 841 | 898 | xiic_deinit(i2c); |
|---|
| 899 | + pm_runtime_put_sync(i2c->dev); |
|---|
| 842 | 900 | clk_disable_unprepare(i2c->clk); |
|---|
| 843 | 901 | pm_runtime_disable(&pdev->dev); |
|---|
| 902 | + pm_runtime_set_suspended(&pdev->dev); |
|---|
| 903 | + pm_runtime_dont_use_autosuspend(&pdev->dev); |
|---|
| 844 | 904 | |
|---|
| 845 | 905 | return 0; |
|---|
| 846 | 906 | } |
|---|
| .. | .. |
|---|
| 892 | 952 | |
|---|
| 893 | 953 | module_platform_driver(xiic_i2c_driver); |
|---|
| 894 | 954 | |
|---|
| 955 | +MODULE_ALIAS("platform:" DRIVER_NAME); |
|---|
| 895 | 956 | MODULE_AUTHOR("info@mocean-labs.com"); |
|---|
| 896 | 957 | MODULE_DESCRIPTION("Xilinx I2C bus driver"); |
|---|
| 897 | 958 | MODULE_LICENSE("GPL v2"); |
|---|