| .. | .. |
|---|
| 20 | 20 | * |
|---|
| 21 | 21 | */ |
|---|
| 22 | 22 | |
|---|
| 23 | +#include <linux/acpi.h> |
|---|
| 23 | 24 | #include <linux/clk.h> |
|---|
| 24 | 25 | #include <linux/completion.h> |
|---|
| 25 | 26 | #include <linux/delay.h> |
|---|
| .. | .. |
|---|
| 33 | 34 | #include <linux/init.h> |
|---|
| 34 | 35 | #include <linux/interrupt.h> |
|---|
| 35 | 36 | #include <linux/io.h> |
|---|
| 37 | +#include <linux/iopoll.h> |
|---|
| 36 | 38 | #include <linux/kernel.h> |
|---|
| 37 | 39 | #include <linux/module.h> |
|---|
| 38 | 40 | #include <linux/of.h> |
|---|
| .. | .. |
|---|
| 47 | 49 | |
|---|
| 48 | 50 | /* This will be the driver name the kernel reports */ |
|---|
| 49 | 51 | #define DRIVER_NAME "imx-i2c" |
|---|
| 50 | | - |
|---|
| 51 | | -/* Default value */ |
|---|
| 52 | | -#define IMX_I2C_BIT_RATE 100000 /* 100kHz */ |
|---|
| 53 | 52 | |
|---|
| 54 | 53 | /* |
|---|
| 55 | 54 | * Enable DMA if transfer byte size is bigger than this threshold. |
|---|
| .. | .. |
|---|
| 255 | 254 | }; |
|---|
| 256 | 255 | MODULE_DEVICE_TABLE(of, i2c_imx_dt_ids); |
|---|
| 257 | 256 | |
|---|
| 257 | +static const struct acpi_device_id i2c_imx_acpi_ids[] = { |
|---|
| 258 | + {"NXP0001", .driver_data = (kernel_ulong_t)&vf610_i2c_hwdata}, |
|---|
| 259 | + { } |
|---|
| 260 | +}; |
|---|
| 261 | +MODULE_DEVICE_TABLE(acpi, i2c_imx_acpi_ids); |
|---|
| 262 | + |
|---|
| 258 | 263 | static inline int is_imx1_i2c(struct imx_i2c_struct *i2c_imx) |
|---|
| 259 | 264 | { |
|---|
| 260 | 265 | return i2c_imx->hwdata->devtype == IMX1_I2C; |
|---|
| .. | .. |
|---|
| 285 | 290 | if (!dma) |
|---|
| 286 | 291 | return; |
|---|
| 287 | 292 | |
|---|
| 288 | | - dma->chan_tx = dma_request_slave_channel(dev, "tx"); |
|---|
| 289 | | - if (!dma->chan_tx) { |
|---|
| 290 | | - dev_dbg(dev, "can't request DMA tx channel\n"); |
|---|
| 293 | + dma->chan_tx = dma_request_chan(dev, "tx"); |
|---|
| 294 | + if (IS_ERR(dma->chan_tx)) { |
|---|
| 295 | + ret = PTR_ERR(dma->chan_tx); |
|---|
| 296 | + if (ret != -ENODEV && ret != -EPROBE_DEFER) |
|---|
| 297 | + dev_err(dev, "can't request DMA tx channel (%d)\n", ret); |
|---|
| 291 | 298 | goto fail_al; |
|---|
| 292 | 299 | } |
|---|
| 293 | 300 | |
|---|
| .. | .. |
|---|
| 298 | 305 | dma_sconfig.direction = DMA_MEM_TO_DEV; |
|---|
| 299 | 306 | ret = dmaengine_slave_config(dma->chan_tx, &dma_sconfig); |
|---|
| 300 | 307 | if (ret < 0) { |
|---|
| 301 | | - dev_dbg(dev, "can't configure tx channel\n"); |
|---|
| 308 | + dev_err(dev, "can't configure tx channel (%d)\n", ret); |
|---|
| 302 | 309 | goto fail_tx; |
|---|
| 303 | 310 | } |
|---|
| 304 | 311 | |
|---|
| 305 | | - dma->chan_rx = dma_request_slave_channel(dev, "rx"); |
|---|
| 306 | | - if (!dma->chan_rx) { |
|---|
| 307 | | - dev_dbg(dev, "can't request DMA rx channel\n"); |
|---|
| 312 | + dma->chan_rx = dma_request_chan(dev, "rx"); |
|---|
| 313 | + if (IS_ERR(dma->chan_rx)) { |
|---|
| 314 | + ret = PTR_ERR(dma->chan_rx); |
|---|
| 315 | + if (ret != -ENODEV && ret != -EPROBE_DEFER) |
|---|
| 316 | + dev_err(dev, "can't request DMA rx channel (%d)\n", ret); |
|---|
| 308 | 317 | goto fail_tx; |
|---|
| 309 | 318 | } |
|---|
| 310 | 319 | |
|---|
| .. | .. |
|---|
| 315 | 324 | dma_sconfig.direction = DMA_DEV_TO_MEM; |
|---|
| 316 | 325 | ret = dmaengine_slave_config(dma->chan_rx, &dma_sconfig); |
|---|
| 317 | 326 | if (ret < 0) { |
|---|
| 318 | | - dev_dbg(dev, "can't configure rx channel\n"); |
|---|
| 327 | + dev_err(dev, "can't configure rx channel (%d)\n", ret); |
|---|
| 319 | 328 | goto fail_rx; |
|---|
| 320 | 329 | } |
|---|
| 321 | 330 | |
|---|
| .. | .. |
|---|
| 332 | 341 | dma_release_channel(dma->chan_tx); |
|---|
| 333 | 342 | fail_al: |
|---|
| 334 | 343 | devm_kfree(dev, dma); |
|---|
| 335 | | - dev_info(dev, "can't use DMA, using PIO instead.\n"); |
|---|
| 336 | 344 | } |
|---|
| 337 | 345 | |
|---|
| 338 | 346 | static void i2c_imx_dma_callback(void *arg) |
|---|
| .. | .. |
|---|
| 417 | 425 | imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2SR); |
|---|
| 418 | 426 | } |
|---|
| 419 | 427 | |
|---|
| 420 | | -static int i2c_imx_bus_busy(struct imx_i2c_struct *i2c_imx, int for_busy) |
|---|
| 428 | +static int i2c_imx_bus_busy(struct imx_i2c_struct *i2c_imx, int for_busy, bool atomic) |
|---|
| 421 | 429 | { |
|---|
| 422 | 430 | unsigned long orig_jiffies = jiffies; |
|---|
| 423 | 431 | unsigned int temp; |
|---|
| .. | .. |
|---|
| 446 | 454 | "<%s> I2C bus is busy\n", __func__); |
|---|
| 447 | 455 | return -ETIMEDOUT; |
|---|
| 448 | 456 | } |
|---|
| 449 | | - schedule(); |
|---|
| 457 | + if (atomic) |
|---|
| 458 | + udelay(100); |
|---|
| 459 | + else |
|---|
| 460 | + schedule(); |
|---|
| 450 | 461 | } |
|---|
| 451 | 462 | |
|---|
| 452 | 463 | return 0; |
|---|
| 453 | 464 | } |
|---|
| 454 | 465 | |
|---|
| 455 | | -static int i2c_imx_trx_complete(struct imx_i2c_struct *i2c_imx) |
|---|
| 466 | +static int i2c_imx_trx_complete(struct imx_i2c_struct *i2c_imx, bool atomic) |
|---|
| 456 | 467 | { |
|---|
| 457 | | - wait_event_timeout(i2c_imx->queue, i2c_imx->i2csr & I2SR_IIF, HZ / 10); |
|---|
| 468 | + if (atomic) { |
|---|
| 469 | + void __iomem *addr = i2c_imx->base + (IMX_I2C_I2SR << i2c_imx->hwdata->regshift); |
|---|
| 470 | + unsigned int regval; |
|---|
| 471 | + |
|---|
| 472 | + /* |
|---|
| 473 | + * The formula for the poll timeout is documented in the RM |
|---|
| 474 | + * Rev.5 on page 1878: |
|---|
| 475 | + * T_min = 10/F_scl |
|---|
| 476 | + * Set the value hard as it is done for the non-atomic use-case. |
|---|
| 477 | + * Use 10 kHz for the calculation since this is the minimum |
|---|
| 478 | + * allowed SMBus frequency. Also add an offset of 100us since it |
|---|
| 479 | + * turned out that the I2SR_IIF bit isn't set correctly within |
|---|
| 480 | + * the minimum timeout in polling mode. |
|---|
| 481 | + */ |
|---|
| 482 | + readb_poll_timeout_atomic(addr, regval, regval & I2SR_IIF, 5, 1000 + 100); |
|---|
| 483 | + i2c_imx->i2csr = regval; |
|---|
| 484 | + i2c_imx_clear_irq(i2c_imx, I2SR_IIF | I2SR_IAL); |
|---|
| 485 | + } else { |
|---|
| 486 | + wait_event_timeout(i2c_imx->queue, i2c_imx->i2csr & I2SR_IIF, HZ / 10); |
|---|
| 487 | + } |
|---|
| 458 | 488 | |
|---|
| 459 | 489 | if (unlikely(!(i2c_imx->i2csr & I2SR_IIF))) { |
|---|
| 460 | 490 | dev_dbg(&i2c_imx->adapter.dev, "<%s> Timeout\n", __func__); |
|---|
| .. | .. |
|---|
| 542 | 572 | return NOTIFY_OK; |
|---|
| 543 | 573 | } |
|---|
| 544 | 574 | |
|---|
| 545 | | -static int i2c_imx_start(struct imx_i2c_struct *i2c_imx) |
|---|
| 575 | +static int i2c_imx_start(struct imx_i2c_struct *i2c_imx, bool atomic) |
|---|
| 546 | 576 | { |
|---|
| 547 | 577 | unsigned int temp = 0; |
|---|
| 548 | 578 | int result; |
|---|
| .. | .. |
|---|
| 555 | 585 | imx_i2c_write_reg(i2c_imx->hwdata->i2cr_ien_opcode, i2c_imx, IMX_I2C_I2CR); |
|---|
| 556 | 586 | |
|---|
| 557 | 587 | /* Wait controller to be stable */ |
|---|
| 558 | | - usleep_range(50, 150); |
|---|
| 588 | + if (atomic) |
|---|
| 589 | + udelay(50); |
|---|
| 590 | + else |
|---|
| 591 | + usleep_range(50, 150); |
|---|
| 559 | 592 | |
|---|
| 560 | 593 | /* Start I2C transaction */ |
|---|
| 561 | 594 | temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2CR); |
|---|
| 562 | 595 | temp |= I2CR_MSTA; |
|---|
| 563 | 596 | imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR); |
|---|
| 564 | | - result = i2c_imx_bus_busy(i2c_imx, 1); |
|---|
| 597 | + result = i2c_imx_bus_busy(i2c_imx, 1, atomic); |
|---|
| 565 | 598 | if (result) |
|---|
| 566 | 599 | return result; |
|---|
| 567 | 600 | |
|---|
| 568 | 601 | temp |= I2CR_IIEN | I2CR_MTX | I2CR_TXAK; |
|---|
| 602 | + if (atomic) |
|---|
| 603 | + temp &= ~I2CR_IIEN; /* Disable interrupt */ |
|---|
| 604 | + |
|---|
| 569 | 605 | temp &= ~I2CR_DMAEN; |
|---|
| 570 | 606 | imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR); |
|---|
| 571 | 607 | return result; |
|---|
| 572 | 608 | } |
|---|
| 573 | 609 | |
|---|
| 574 | | -static void i2c_imx_stop(struct imx_i2c_struct *i2c_imx) |
|---|
| 610 | +static void i2c_imx_stop(struct imx_i2c_struct *i2c_imx, bool atomic) |
|---|
| 575 | 611 | { |
|---|
| 576 | 612 | unsigned int temp = 0; |
|---|
| 577 | 613 | |
|---|
| .. | .. |
|---|
| 595 | 631 | } |
|---|
| 596 | 632 | |
|---|
| 597 | 633 | if (!i2c_imx->stopped) |
|---|
| 598 | | - i2c_imx_bus_busy(i2c_imx, 0); |
|---|
| 634 | + i2c_imx_bus_busy(i2c_imx, 0, atomic); |
|---|
| 599 | 635 | |
|---|
| 600 | 636 | /* Disable I2C controller */ |
|---|
| 601 | 637 | temp = i2c_imx->hwdata->i2cr_ien_opcode ^ I2CR_IEN, |
|---|
| .. | .. |
|---|
| 674 | 710 | /* The last data byte must be transferred by the CPU. */ |
|---|
| 675 | 711 | imx_i2c_write_reg(msgs->buf[msgs->len-1], |
|---|
| 676 | 712 | i2c_imx, IMX_I2C_I2DR); |
|---|
| 677 | | - result = i2c_imx_trx_complete(i2c_imx); |
|---|
| 713 | + result = i2c_imx_trx_complete(i2c_imx, false); |
|---|
| 678 | 714 | if (result) |
|---|
| 679 | 715 | return result; |
|---|
| 680 | 716 | |
|---|
| .. | .. |
|---|
| 733 | 769 | |
|---|
| 734 | 770 | msgs->buf[msgs->len-2] = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2DR); |
|---|
| 735 | 771 | /* read n byte data */ |
|---|
| 736 | | - result = i2c_imx_trx_complete(i2c_imx); |
|---|
| 772 | + result = i2c_imx_trx_complete(i2c_imx, false); |
|---|
| 737 | 773 | if (result) |
|---|
| 738 | 774 | return result; |
|---|
| 739 | 775 | |
|---|
| .. | .. |
|---|
| 749 | 785 | temp &= ~(I2CR_MSTA | I2CR_MTX); |
|---|
| 750 | 786 | imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR); |
|---|
| 751 | 787 | if (!i2c_imx->stopped) |
|---|
| 752 | | - i2c_imx_bus_busy(i2c_imx, 0); |
|---|
| 788 | + i2c_imx_bus_busy(i2c_imx, 0, false); |
|---|
| 753 | 789 | } else { |
|---|
| 754 | 790 | /* |
|---|
| 755 | 791 | * For i2c master receiver repeat restart operation like: |
|---|
| .. | .. |
|---|
| 767 | 803 | return 0; |
|---|
| 768 | 804 | } |
|---|
| 769 | 805 | |
|---|
| 770 | | -static int i2c_imx_write(struct imx_i2c_struct *i2c_imx, struct i2c_msg *msgs) |
|---|
| 806 | +static int i2c_imx_write(struct imx_i2c_struct *i2c_imx, struct i2c_msg *msgs, |
|---|
| 807 | + bool atomic) |
|---|
| 771 | 808 | { |
|---|
| 772 | 809 | int i, result; |
|---|
| 773 | 810 | |
|---|
| .. | .. |
|---|
| 776 | 813 | |
|---|
| 777 | 814 | /* write slave address */ |
|---|
| 778 | 815 | imx_i2c_write_reg(i2c_8bit_addr_from_msg(msgs), i2c_imx, IMX_I2C_I2DR); |
|---|
| 779 | | - result = i2c_imx_trx_complete(i2c_imx); |
|---|
| 816 | + result = i2c_imx_trx_complete(i2c_imx, atomic); |
|---|
| 780 | 817 | if (result) |
|---|
| 781 | 818 | return result; |
|---|
| 782 | 819 | result = i2c_imx_acked(i2c_imx); |
|---|
| .. | .. |
|---|
| 790 | 827 | "<%s> write byte: B%d=0x%X\n", |
|---|
| 791 | 828 | __func__, i, msgs->buf[i]); |
|---|
| 792 | 829 | imx_i2c_write_reg(msgs->buf[i], i2c_imx, IMX_I2C_I2DR); |
|---|
| 793 | | - result = i2c_imx_trx_complete(i2c_imx); |
|---|
| 830 | + result = i2c_imx_trx_complete(i2c_imx, atomic); |
|---|
| 794 | 831 | if (result) |
|---|
| 795 | 832 | return result; |
|---|
| 796 | 833 | result = i2c_imx_acked(i2c_imx); |
|---|
| .. | .. |
|---|
| 800 | 837 | return 0; |
|---|
| 801 | 838 | } |
|---|
| 802 | 839 | |
|---|
| 803 | | -static int i2c_imx_read(struct imx_i2c_struct *i2c_imx, struct i2c_msg *msgs, bool is_lastmsg) |
|---|
| 840 | +static int i2c_imx_read(struct imx_i2c_struct *i2c_imx, struct i2c_msg *msgs, |
|---|
| 841 | + bool is_lastmsg, bool atomic) |
|---|
| 804 | 842 | { |
|---|
| 805 | 843 | int i, result; |
|---|
| 806 | 844 | unsigned int temp; |
|---|
| 807 | 845 | int block_data = msgs->flags & I2C_M_RECV_LEN; |
|---|
| 808 | | - int use_dma = i2c_imx->dma && msgs->len >= DMA_THRESHOLD && !block_data; |
|---|
| 846 | + int use_dma = i2c_imx->dma && msgs->flags & I2C_M_DMA_SAFE && |
|---|
| 847 | + msgs->len >= DMA_THRESHOLD && !block_data; |
|---|
| 809 | 848 | |
|---|
| 810 | 849 | dev_dbg(&i2c_imx->adapter.dev, |
|---|
| 811 | 850 | "<%s> write slave address: addr=0x%x\n", |
|---|
| .. | .. |
|---|
| 813 | 852 | |
|---|
| 814 | 853 | /* write slave address */ |
|---|
| 815 | 854 | imx_i2c_write_reg(i2c_8bit_addr_from_msg(msgs), i2c_imx, IMX_I2C_I2DR); |
|---|
| 816 | | - result = i2c_imx_trx_complete(i2c_imx); |
|---|
| 855 | + result = i2c_imx_trx_complete(i2c_imx, atomic); |
|---|
| 817 | 856 | if (result) |
|---|
| 818 | 857 | return result; |
|---|
| 819 | 858 | result = i2c_imx_acked(i2c_imx); |
|---|
| .. | .. |
|---|
| 846 | 885 | for (i = 0; i < msgs->len; i++) { |
|---|
| 847 | 886 | u8 len = 0; |
|---|
| 848 | 887 | |
|---|
| 849 | | - result = i2c_imx_trx_complete(i2c_imx); |
|---|
| 888 | + result = i2c_imx_trx_complete(i2c_imx, atomic); |
|---|
| 850 | 889 | if (result) |
|---|
| 851 | 890 | return result; |
|---|
| 852 | 891 | /* |
|---|
| .. | .. |
|---|
| 877 | 916 | temp &= ~(I2CR_MSTA | I2CR_MTX); |
|---|
| 878 | 917 | imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR); |
|---|
| 879 | 918 | if (!i2c_imx->stopped) |
|---|
| 880 | | - i2c_imx_bus_busy(i2c_imx, 0); |
|---|
| 919 | + i2c_imx_bus_busy(i2c_imx, 0, atomic); |
|---|
| 881 | 920 | } else { |
|---|
| 882 | 921 | /* |
|---|
| 883 | 922 | * For i2c master receiver repeat restart operation like: |
|---|
| .. | .. |
|---|
| 908 | 947 | return 0; |
|---|
| 909 | 948 | } |
|---|
| 910 | 949 | |
|---|
| 911 | | -static int i2c_imx_xfer(struct i2c_adapter *adapter, |
|---|
| 912 | | - struct i2c_msg *msgs, int num) |
|---|
| 950 | +static int i2c_imx_xfer_common(struct i2c_adapter *adapter, |
|---|
| 951 | + struct i2c_msg *msgs, int num, bool atomic) |
|---|
| 913 | 952 | { |
|---|
| 914 | 953 | unsigned int i, temp; |
|---|
| 915 | 954 | int result; |
|---|
| .. | .. |
|---|
| 918 | 957 | |
|---|
| 919 | 958 | dev_dbg(&i2c_imx->adapter.dev, "<%s>\n", __func__); |
|---|
| 920 | 959 | |
|---|
| 921 | | - result = pm_runtime_get_sync(i2c_imx->adapter.dev.parent); |
|---|
| 922 | | - if (result < 0) |
|---|
| 923 | | - goto out; |
|---|
| 924 | | - |
|---|
| 925 | 960 | /* Start I2C transfer */ |
|---|
| 926 | | - result = i2c_imx_start(i2c_imx); |
|---|
| 961 | + result = i2c_imx_start(i2c_imx, atomic); |
|---|
| 927 | 962 | if (result) { |
|---|
| 928 | | - if (i2c_imx->adapter.bus_recovery_info) { |
|---|
| 963 | + /* |
|---|
| 964 | + * Bus recovery uses gpiod_get_value_cansleep() which is not |
|---|
| 965 | + * allowed within atomic context. |
|---|
| 966 | + */ |
|---|
| 967 | + if (!atomic && i2c_imx->adapter.bus_recovery_info) { |
|---|
| 929 | 968 | i2c_recover_bus(&i2c_imx->adapter); |
|---|
| 930 | | - result = i2c_imx_start(i2c_imx); |
|---|
| 969 | + result = i2c_imx_start(i2c_imx, atomic); |
|---|
| 931 | 970 | } |
|---|
| 932 | 971 | } |
|---|
| 933 | 972 | |
|---|
| .. | .. |
|---|
| 945 | 984 | temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2CR); |
|---|
| 946 | 985 | temp |= I2CR_RSTA; |
|---|
| 947 | 986 | imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR); |
|---|
| 948 | | - result = i2c_imx_bus_busy(i2c_imx, 1); |
|---|
| 987 | + result = i2c_imx_bus_busy(i2c_imx, 1, atomic); |
|---|
| 949 | 988 | if (result) |
|---|
| 950 | 989 | goto fail0; |
|---|
| 951 | 990 | } |
|---|
| .. | .. |
|---|
| 969 | 1008 | (temp & I2SR_SRW ? 1 : 0), (temp & I2SR_IIF ? 1 : 0), |
|---|
| 970 | 1009 | (temp & I2SR_RXAK ? 1 : 0)); |
|---|
| 971 | 1010 | #endif |
|---|
| 972 | | - if (msgs[i].flags & I2C_M_RD) |
|---|
| 973 | | - result = i2c_imx_read(i2c_imx, &msgs[i], is_lastmsg); |
|---|
| 974 | | - else { |
|---|
| 975 | | - if (i2c_imx->dma && msgs[i].len >= DMA_THRESHOLD) |
|---|
| 1011 | + if (msgs[i].flags & I2C_M_RD) { |
|---|
| 1012 | + result = i2c_imx_read(i2c_imx, &msgs[i], is_lastmsg, atomic); |
|---|
| 1013 | + } else { |
|---|
| 1014 | + if (!atomic && |
|---|
| 1015 | + i2c_imx->dma && msgs[i].len >= DMA_THRESHOLD && |
|---|
| 1016 | + msgs[i].flags & I2C_M_DMA_SAFE) |
|---|
| 976 | 1017 | result = i2c_imx_dma_write(i2c_imx, &msgs[i]); |
|---|
| 977 | 1018 | else |
|---|
| 978 | | - result = i2c_imx_write(i2c_imx, &msgs[i]); |
|---|
| 1019 | + result = i2c_imx_write(i2c_imx, &msgs[i], atomic); |
|---|
| 979 | 1020 | } |
|---|
| 980 | 1021 | if (result) |
|---|
| 981 | 1022 | goto fail0; |
|---|
| .. | .. |
|---|
| 983 | 1024 | |
|---|
| 984 | 1025 | fail0: |
|---|
| 985 | 1026 | /* Stop I2C transfer */ |
|---|
| 986 | | - i2c_imx_stop(i2c_imx); |
|---|
| 1027 | + i2c_imx_stop(i2c_imx, atomic); |
|---|
| 987 | 1028 | |
|---|
| 988 | | - pm_runtime_mark_last_busy(i2c_imx->adapter.dev.parent); |
|---|
| 989 | | - pm_runtime_put_autosuspend(i2c_imx->adapter.dev.parent); |
|---|
| 990 | | - |
|---|
| 991 | | -out: |
|---|
| 992 | 1029 | dev_dbg(&i2c_imx->adapter.dev, "<%s> exit with: %s: %d\n", __func__, |
|---|
| 993 | 1030 | (result < 0) ? "error" : "success msg", |
|---|
| 994 | 1031 | (result < 0) ? result : num); |
|---|
| 995 | 1032 | return (result < 0) ? result : num; |
|---|
| 1033 | +} |
|---|
| 1034 | + |
|---|
| 1035 | +static int i2c_imx_xfer(struct i2c_adapter *adapter, |
|---|
| 1036 | + struct i2c_msg *msgs, int num) |
|---|
| 1037 | +{ |
|---|
| 1038 | + struct imx_i2c_struct *i2c_imx = i2c_get_adapdata(adapter); |
|---|
| 1039 | + int result; |
|---|
| 1040 | + |
|---|
| 1041 | + result = pm_runtime_resume_and_get(i2c_imx->adapter.dev.parent); |
|---|
| 1042 | + if (result < 0) |
|---|
| 1043 | + return result; |
|---|
| 1044 | + |
|---|
| 1045 | + result = i2c_imx_xfer_common(adapter, msgs, num, false); |
|---|
| 1046 | + |
|---|
| 1047 | + pm_runtime_mark_last_busy(i2c_imx->adapter.dev.parent); |
|---|
| 1048 | + pm_runtime_put_autosuspend(i2c_imx->adapter.dev.parent); |
|---|
| 1049 | + |
|---|
| 1050 | + return result; |
|---|
| 1051 | +} |
|---|
| 1052 | + |
|---|
| 1053 | +static int i2c_imx_xfer_atomic(struct i2c_adapter *adapter, |
|---|
| 1054 | + struct i2c_msg *msgs, int num) |
|---|
| 1055 | +{ |
|---|
| 1056 | + struct imx_i2c_struct *i2c_imx = i2c_get_adapdata(adapter); |
|---|
| 1057 | + int result; |
|---|
| 1058 | + |
|---|
| 1059 | + result = clk_enable(i2c_imx->clk); |
|---|
| 1060 | + if (result) |
|---|
| 1061 | + return result; |
|---|
| 1062 | + |
|---|
| 1063 | + result = i2c_imx_xfer_common(adapter, msgs, num, true); |
|---|
| 1064 | + |
|---|
| 1065 | + clk_disable(i2c_imx->clk); |
|---|
| 1066 | + |
|---|
| 1067 | + return result; |
|---|
| 996 | 1068 | } |
|---|
| 997 | 1069 | |
|---|
| 998 | 1070 | static void i2c_imx_prepare_recovery(struct i2c_adapter *adap) |
|---|
| .. | .. |
|---|
| 1067 | 1139 | } |
|---|
| 1068 | 1140 | |
|---|
| 1069 | 1141 | static const struct i2c_algorithm i2c_imx_algo = { |
|---|
| 1070 | | - .master_xfer = i2c_imx_xfer, |
|---|
| 1071 | | - .functionality = i2c_imx_func, |
|---|
| 1142 | + .master_xfer = i2c_imx_xfer, |
|---|
| 1143 | + .master_xfer_atomic = i2c_imx_xfer_atomic, |
|---|
| 1144 | + .functionality = i2c_imx_func, |
|---|
| 1072 | 1145 | }; |
|---|
| 1073 | 1146 | |
|---|
| 1074 | 1147 | static int i2c_imx_probe(struct platform_device *pdev) |
|---|
| 1075 | 1148 | { |
|---|
| 1076 | | - const struct of_device_id *of_id = of_match_device(i2c_imx_dt_ids, |
|---|
| 1077 | | - &pdev->dev); |
|---|
| 1078 | 1149 | struct imx_i2c_struct *i2c_imx; |
|---|
| 1079 | 1150 | struct resource *res; |
|---|
| 1080 | 1151 | struct imxi2c_platform_data *pdata = dev_get_platdata(&pdev->dev); |
|---|
| 1081 | 1152 | void __iomem *base; |
|---|
| 1082 | 1153 | int irq, ret; |
|---|
| 1083 | 1154 | dma_addr_t phy_addr; |
|---|
| 1155 | + const struct imx_i2c_hwdata *match; |
|---|
| 1084 | 1156 | |
|---|
| 1085 | 1157 | dev_dbg(&pdev->dev, "<%s>\n", __func__); |
|---|
| 1086 | 1158 | |
|---|
| 1087 | 1159 | irq = platform_get_irq(pdev, 0); |
|---|
| 1088 | | - if (irq < 0) { |
|---|
| 1089 | | - dev_err(&pdev->dev, "can't get irq number\n"); |
|---|
| 1160 | + if (irq < 0) |
|---|
| 1090 | 1161 | return irq; |
|---|
| 1091 | | - } |
|---|
| 1092 | 1162 | |
|---|
| 1093 | 1163 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
|---|
| 1094 | 1164 | base = devm_ioremap_resource(&pdev->dev, res); |
|---|
| .. | .. |
|---|
| 1100 | 1170 | if (!i2c_imx) |
|---|
| 1101 | 1171 | return -ENOMEM; |
|---|
| 1102 | 1172 | |
|---|
| 1103 | | - if (of_id) |
|---|
| 1104 | | - i2c_imx->hwdata = of_id->data; |
|---|
| 1173 | + match = device_get_match_data(&pdev->dev); |
|---|
| 1174 | + if (match) |
|---|
| 1175 | + i2c_imx->hwdata = match; |
|---|
| 1105 | 1176 | else |
|---|
| 1106 | 1177 | i2c_imx->hwdata = (struct imx_i2c_hwdata *) |
|---|
| 1107 | 1178 | platform_get_device_id(pdev)->driver_data; |
|---|
| .. | .. |
|---|
| 1114 | 1185 | i2c_imx->adapter.nr = pdev->id; |
|---|
| 1115 | 1186 | i2c_imx->adapter.dev.of_node = pdev->dev.of_node; |
|---|
| 1116 | 1187 | i2c_imx->base = base; |
|---|
| 1188 | + ACPI_COMPANION_SET(&i2c_imx->adapter.dev, ACPI_COMPANION(&pdev->dev)); |
|---|
| 1117 | 1189 | |
|---|
| 1118 | 1190 | /* Get I2C clock */ |
|---|
| 1119 | 1191 | i2c_imx->clk = devm_clk_get(&pdev->dev, NULL); |
|---|
| 1120 | | - if (IS_ERR(i2c_imx->clk)) { |
|---|
| 1121 | | - if (PTR_ERR(i2c_imx->clk) != -EPROBE_DEFER) |
|---|
| 1122 | | - dev_err(&pdev->dev, "can't get I2C clock\n"); |
|---|
| 1123 | | - return PTR_ERR(i2c_imx->clk); |
|---|
| 1124 | | - } |
|---|
| 1192 | + if (IS_ERR(i2c_imx->clk)) |
|---|
| 1193 | + return dev_err_probe(&pdev->dev, PTR_ERR(i2c_imx->clk), |
|---|
| 1194 | + "can't get I2C clock\n"); |
|---|
| 1125 | 1195 | |
|---|
| 1126 | 1196 | ret = clk_prepare_enable(i2c_imx->clk); |
|---|
| 1127 | 1197 | if (ret) { |
|---|
| .. | .. |
|---|
| 1156 | 1226 | } |
|---|
| 1157 | 1227 | |
|---|
| 1158 | 1228 | /* Set up clock divider */ |
|---|
| 1159 | | - i2c_imx->bitrate = IMX_I2C_BIT_RATE; |
|---|
| 1229 | + i2c_imx->bitrate = I2C_MAX_STANDARD_MODE_FREQ; |
|---|
| 1160 | 1230 | ret = of_property_read_u32(pdev->dev.of_node, |
|---|
| 1161 | 1231 | "clock-frequency", &i2c_imx->bitrate); |
|---|
| 1162 | 1232 | if (ret < 0 && pdata && pdata->bitrate) |
|---|
| .. | .. |
|---|
| 1213 | 1283 | int irq, ret; |
|---|
| 1214 | 1284 | |
|---|
| 1215 | 1285 | ret = pm_runtime_get_sync(&pdev->dev); |
|---|
| 1216 | | - if (ret < 0) |
|---|
| 1217 | | - return ret; |
|---|
| 1218 | 1286 | |
|---|
| 1219 | 1287 | /* remove adapter */ |
|---|
| 1220 | 1288 | dev_dbg(&i2c_imx->adapter.dev, "adapter removed\n"); |
|---|
| .. | .. |
|---|
| 1223 | 1291 | if (i2c_imx->dma) |
|---|
| 1224 | 1292 | i2c_imx_dma_free(i2c_imx); |
|---|
| 1225 | 1293 | |
|---|
| 1226 | | - /* setup chip registers to defaults */ |
|---|
| 1227 | | - imx_i2c_write_reg(0, i2c_imx, IMX_I2C_IADR); |
|---|
| 1228 | | - imx_i2c_write_reg(0, i2c_imx, IMX_I2C_IFDR); |
|---|
| 1229 | | - imx_i2c_write_reg(0, i2c_imx, IMX_I2C_I2CR); |
|---|
| 1230 | | - imx_i2c_write_reg(0, i2c_imx, IMX_I2C_I2SR); |
|---|
| 1294 | + if (ret >= 0) { |
|---|
| 1295 | + /* setup chip registers to defaults */ |
|---|
| 1296 | + imx_i2c_write_reg(0, i2c_imx, IMX_I2C_IADR); |
|---|
| 1297 | + imx_i2c_write_reg(0, i2c_imx, IMX_I2C_IFDR); |
|---|
| 1298 | + imx_i2c_write_reg(0, i2c_imx, IMX_I2C_I2CR); |
|---|
| 1299 | + imx_i2c_write_reg(0, i2c_imx, IMX_I2C_I2SR); |
|---|
| 1300 | + clk_disable(i2c_imx->clk); |
|---|
| 1301 | + } |
|---|
| 1231 | 1302 | |
|---|
| 1232 | 1303 | clk_notifier_unregister(i2c_imx->clk, &i2c_imx->clk_change_nb); |
|---|
| 1233 | 1304 | irq = platform_get_irq(pdev, 0); |
|---|
| 1234 | 1305 | if (irq >= 0) |
|---|
| 1235 | 1306 | free_irq(irq, i2c_imx); |
|---|
| 1236 | | - clk_disable_unprepare(i2c_imx->clk); |
|---|
| 1307 | + |
|---|
| 1308 | + clk_unprepare(i2c_imx->clk); |
|---|
| 1237 | 1309 | |
|---|
| 1238 | 1310 | pm_runtime_put_noidle(&pdev->dev); |
|---|
| 1239 | 1311 | pm_runtime_disable(&pdev->dev); |
|---|
| .. | .. |
|---|
| 1241 | 1313 | return 0; |
|---|
| 1242 | 1314 | } |
|---|
| 1243 | 1315 | |
|---|
| 1244 | | -#ifdef CONFIG_PM |
|---|
| 1245 | | -static int i2c_imx_runtime_suspend(struct device *dev) |
|---|
| 1316 | +static int __maybe_unused i2c_imx_runtime_suspend(struct device *dev) |
|---|
| 1246 | 1317 | { |
|---|
| 1247 | 1318 | struct imx_i2c_struct *i2c_imx = dev_get_drvdata(dev); |
|---|
| 1248 | 1319 | |
|---|
| .. | .. |
|---|
| 1251 | 1322 | return 0; |
|---|
| 1252 | 1323 | } |
|---|
| 1253 | 1324 | |
|---|
| 1254 | | -static int i2c_imx_runtime_resume(struct device *dev) |
|---|
| 1325 | +static int __maybe_unused i2c_imx_runtime_resume(struct device *dev) |
|---|
| 1255 | 1326 | { |
|---|
| 1256 | 1327 | struct imx_i2c_struct *i2c_imx = dev_get_drvdata(dev); |
|---|
| 1257 | 1328 | int ret; |
|---|
| .. | .. |
|---|
| 1267 | 1338 | SET_RUNTIME_PM_OPS(i2c_imx_runtime_suspend, |
|---|
| 1268 | 1339 | i2c_imx_runtime_resume, NULL) |
|---|
| 1269 | 1340 | }; |
|---|
| 1270 | | -#define I2C_IMX_PM_OPS (&i2c_imx_pm_ops) |
|---|
| 1271 | | -#else |
|---|
| 1272 | | -#define I2C_IMX_PM_OPS NULL |
|---|
| 1273 | | -#endif /* CONFIG_PM */ |
|---|
| 1274 | 1341 | |
|---|
| 1275 | 1342 | static struct platform_driver i2c_imx_driver = { |
|---|
| 1276 | 1343 | .probe = i2c_imx_probe, |
|---|
| 1277 | 1344 | .remove = i2c_imx_remove, |
|---|
| 1278 | 1345 | .driver = { |
|---|
| 1279 | 1346 | .name = DRIVER_NAME, |
|---|
| 1280 | | - .pm = I2C_IMX_PM_OPS, |
|---|
| 1347 | + .pm = &i2c_imx_pm_ops, |
|---|
| 1281 | 1348 | .of_match_table = i2c_imx_dt_ids, |
|---|
| 1349 | + .acpi_match_table = i2c_imx_acpi_ids, |
|---|
| 1282 | 1350 | }, |
|---|
| 1283 | 1351 | .id_table = imx_i2c_devtype, |
|---|
| 1284 | 1352 | }; |
|---|