aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@linux-foundation.org>2014-09-25 18:04:06 -0400
committerLinus Torvalds <torvalds@linux-foundation.org>2014-09-25 18:04:06 -0400
commit12df9f376d895011fb667980c8e204e050122772 (patch)
tree4b9715a8b44b91cf58aa26df1e4f8d4d6887dc8f
parentd671e42457e1fef70ad0df07084207078633a9e8 (diff)
parent0aef44e84ad16cd87a88df78773fd81ecca34f11 (diff)
Merge branch 'i2c/for-current' of git://git.kernel.org/pub/scm/linux/kernel/git/wsa/linux
Pull i2c fixes from Wolfram Sang: "This is probably not the kind of pull request you want to see that late in the cycle. Yet, the ACPI refactorization was problematic again and caused another two issues which need fixing. My holidays with limited internet (plus travelling) and the developer's illness didn't help either :( The details: - ACPI code was refactored out into a seperate file and as a side-effect, the i2c-core module got renamed. Jean Delvare rightfully complained about the rename being problematic for distributions. So, Mika and I thought the least problematic way to deal with it is to move all the code back into the main i2c core source file. This is mainly a huge code move with some #ifdeffery applied. No functional code changes. Our personal tests and the testbots did not find problems. (I was thinking about reverting, too, yet that would also have ~800 lines changed) - The new ACPI code also had a NULL pointer exception, thanks to Peter for finding and fixing it. - Mikko fixed a locking problem by decoupling clock_prepare and clock_enable. - Addy learnt that the datasheet was wrong and reimplemented the frequency setup according to the new algorithm. - Fan fixed an off-by-one error when copying data - Janusz fixed a copy'n'paste bug which gave a wrong error message - Sergei made sure that "don't touch" bits are not accessed" * 'i2c/for-current' of git://git.kernel.org/pub/scm/linux/kernel/git/wsa/linux: i2c: acpi: Fix NULL Pointer dereference i2c: move acpi code back into the core i2c: rk3x: fix divisor calculation for SCL frequency i2c: mxs: fix error message in pio transfer i2c: ismt: use correct length when copy buffer i2c: rcar: fix RCAR_IRQ_ACK_{RECV|SEND} i2c: tegra: Move clk_prepare/clk_set_rate to probe
-rw-r--r--MAINTAINERS1
-rw-r--r--drivers/i2c/Makefile5
-rw-r--r--drivers/i2c/busses/i2c-ismt.c4
-rw-r--r--drivers/i2c/busses/i2c-mxs.c2
-rw-r--r--drivers/i2c/busses/i2c-rcar.c4
-rw-r--r--drivers/i2c/busses/i2c-rk3x.c11
-rw-r--r--drivers/i2c/busses/i2c-tegra.c57
-rw-r--r--drivers/i2c/i2c-acpi.c364
-rw-r--r--drivers/i2c/i2c-core.c364
-rw-r--r--include/linux/i2c.h16
10 files changed, 420 insertions, 408 deletions
diff --git a/MAINTAINERS b/MAINTAINERS
index 839f36761f2c..37054306dc9f 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -4476,7 +4476,6 @@ M: Mika Westerberg <mika.westerberg@linux.intel.com>
4476L: linux-i2c@vger.kernel.org 4476L: linux-i2c@vger.kernel.org
4477L: linux-acpi@vger.kernel.org 4477L: linux-acpi@vger.kernel.org
4478S: Maintained 4478S: Maintained
4479F: drivers/i2c/i2c-acpi.c
4480 4479
4481I2C-TAOS-EVM DRIVER 4480I2C-TAOS-EVM DRIVER
4482M: Jean Delvare <jdelvare@suse.de> 4481M: Jean Delvare <jdelvare@suse.de>
diff --git a/drivers/i2c/Makefile b/drivers/i2c/Makefile
index e0228b228256..1722f50f2473 100644
--- a/drivers/i2c/Makefile
+++ b/drivers/i2c/Makefile
@@ -2,11 +2,8 @@
2# Makefile for the i2c core. 2# Makefile for the i2c core.
3# 3#
4 4
5i2ccore-y := i2c-core.o
6i2ccore-$(CONFIG_ACPI) += i2c-acpi.o
7
8obj-$(CONFIG_I2C_BOARDINFO) += i2c-boardinfo.o 5obj-$(CONFIG_I2C_BOARDINFO) += i2c-boardinfo.o
9obj-$(CONFIG_I2C) += i2ccore.o 6obj-$(CONFIG_I2C) += i2c-core.o
10obj-$(CONFIG_I2C_SMBUS) += i2c-smbus.o 7obj-$(CONFIG_I2C_SMBUS) += i2c-smbus.o
11obj-$(CONFIG_I2C_CHARDEV) += i2c-dev.o 8obj-$(CONFIG_I2C_CHARDEV) += i2c-dev.o
12obj-$(CONFIG_I2C_MUX) += i2c-mux.o 9obj-$(CONFIG_I2C_MUX) += i2c-mux.o
diff --git a/drivers/i2c/busses/i2c-ismt.c b/drivers/i2c/busses/i2c-ismt.c
index 984492553e95..d9ee43c80cde 100644
--- a/drivers/i2c/busses/i2c-ismt.c
+++ b/drivers/i2c/busses/i2c-ismt.c
@@ -497,7 +497,7 @@ static int ismt_access(struct i2c_adapter *adap, u16 addr,
497 desc->wr_len_cmd = dma_size; 497 desc->wr_len_cmd = dma_size;
498 desc->control |= ISMT_DESC_BLK; 498 desc->control |= ISMT_DESC_BLK;
499 priv->dma_buffer[0] = command; 499 priv->dma_buffer[0] = command;
500 memcpy(&priv->dma_buffer[1], &data->block[1], dma_size); 500 memcpy(&priv->dma_buffer[1], &data->block[1], dma_size - 1);
501 } else { 501 } else {
502 /* Block Read */ 502 /* Block Read */
503 dev_dbg(dev, "I2C_SMBUS_BLOCK_DATA: READ\n"); 503 dev_dbg(dev, "I2C_SMBUS_BLOCK_DATA: READ\n");
@@ -525,7 +525,7 @@ static int ismt_access(struct i2c_adapter *adap, u16 addr,
525 desc->wr_len_cmd = dma_size; 525 desc->wr_len_cmd = dma_size;
526 desc->control |= ISMT_DESC_I2C; 526 desc->control |= ISMT_DESC_I2C;
527 priv->dma_buffer[0] = command; 527 priv->dma_buffer[0] = command;
528 memcpy(&priv->dma_buffer[1], &data->block[1], dma_size); 528 memcpy(&priv->dma_buffer[1], &data->block[1], dma_size - 1);
529 } else { 529 } else {
530 /* i2c Block Read */ 530 /* i2c Block Read */
531 dev_dbg(dev, "I2C_SMBUS_I2C_BLOCK_DATA: READ\n"); 531 dev_dbg(dev, "I2C_SMBUS_I2C_BLOCK_DATA: READ\n");
diff --git a/drivers/i2c/busses/i2c-mxs.c b/drivers/i2c/busses/i2c-mxs.c
index 7170fc892829..65a21fed08b5 100644
--- a/drivers/i2c/busses/i2c-mxs.c
+++ b/drivers/i2c/busses/i2c-mxs.c
@@ -429,7 +429,7 @@ static int mxs_i2c_pio_setup_xfer(struct i2c_adapter *adap,
429 ret = mxs_i2c_pio_wait_xfer_end(i2c); 429 ret = mxs_i2c_pio_wait_xfer_end(i2c);
430 if (ret) { 430 if (ret) {
431 dev_err(i2c->dev, 431 dev_err(i2c->dev,
432 "PIO: Failed to send SELECT command!\n"); 432 "PIO: Failed to send READ command!\n");
433 goto cleanup; 433 goto cleanup;
434 } 434 }
435 435
diff --git a/drivers/i2c/busses/i2c-rcar.c b/drivers/i2c/busses/i2c-rcar.c
index 1cc146cfc1f3..e506fcd3ca04 100644
--- a/drivers/i2c/busses/i2c-rcar.c
+++ b/drivers/i2c/busses/i2c-rcar.c
@@ -76,8 +76,8 @@
76#define RCAR_IRQ_RECV (MNR | MAL | MST | MAT | MDR) 76#define RCAR_IRQ_RECV (MNR | MAL | MST | MAT | MDR)
77#define RCAR_IRQ_STOP (MST) 77#define RCAR_IRQ_STOP (MST)
78 78
79#define RCAR_IRQ_ACK_SEND (~(MAT | MDE)) 79#define RCAR_IRQ_ACK_SEND (~(MAT | MDE) & 0xFF)
80#define RCAR_IRQ_ACK_RECV (~(MAT | MDR)) 80#define RCAR_IRQ_ACK_RECV (~(MAT | MDR) & 0xFF)
81 81
82#define ID_LAST_MSG (1 << 0) 82#define ID_LAST_MSG (1 << 0)
83#define ID_IOERROR (1 << 1) 83#define ID_IOERROR (1 << 1)
diff --git a/drivers/i2c/busses/i2c-rk3x.c b/drivers/i2c/busses/i2c-rk3x.c
index e637c32ae517..93cfc837200b 100644
--- a/drivers/i2c/busses/i2c-rk3x.c
+++ b/drivers/i2c/busses/i2c-rk3x.c
@@ -433,12 +433,11 @@ static void rk3x_i2c_set_scl_rate(struct rk3x_i2c *i2c, unsigned long scl_rate)
433 unsigned long i2c_rate = clk_get_rate(i2c->clk); 433 unsigned long i2c_rate = clk_get_rate(i2c->clk);
434 unsigned int div; 434 unsigned int div;
435 435
436 /* SCL rate = (clk rate) / (8 * DIV) */ 436 /* set DIV = DIVH = DIVL
437 div = DIV_ROUND_UP(i2c_rate, scl_rate * 8); 437 * SCL rate = (clk rate) / (8 * (DIVH + 1 + DIVL + 1))
438 438 * = (clk rate) / (16 * (DIV + 1))
439 /* The lower and upper half of the CLKDIV reg describe the length of 439 */
440 * SCL low & high periods. */ 440 div = DIV_ROUND_UP(i2c_rate, scl_rate * 16) - 1;
441 div = DIV_ROUND_UP(div, 2);
442 441
443 i2c_writel(i2c, (div << 16) | (div & 0xffff), REG_CLKDIV); 442 i2c_writel(i2c, (div << 16) | (div & 0xffff), REG_CLKDIV);
444} 443}
diff --git a/drivers/i2c/busses/i2c-tegra.c b/drivers/i2c/busses/i2c-tegra.c
index 87d0371cebb7..efba1ebe16ba 100644
--- a/drivers/i2c/busses/i2c-tegra.c
+++ b/drivers/i2c/busses/i2c-tegra.c
@@ -380,34 +380,33 @@ static inline int tegra_i2c_clock_enable(struct tegra_i2c_dev *i2c_dev)
380{ 380{
381 int ret; 381 int ret;
382 if (!i2c_dev->hw->has_single_clk_source) { 382 if (!i2c_dev->hw->has_single_clk_source) {
383 ret = clk_prepare_enable(i2c_dev->fast_clk); 383 ret = clk_enable(i2c_dev->fast_clk);
384 if (ret < 0) { 384 if (ret < 0) {
385 dev_err(i2c_dev->dev, 385 dev_err(i2c_dev->dev,
386 "Enabling fast clk failed, err %d\n", ret); 386 "Enabling fast clk failed, err %d\n", ret);
387 return ret; 387 return ret;
388 } 388 }
389 } 389 }
390 ret = clk_prepare_enable(i2c_dev->div_clk); 390 ret = clk_enable(i2c_dev->div_clk);
391 if (ret < 0) { 391 if (ret < 0) {
392 dev_err(i2c_dev->dev, 392 dev_err(i2c_dev->dev,
393 "Enabling div clk failed, err %d\n", ret); 393 "Enabling div clk failed, err %d\n", ret);
394 clk_disable_unprepare(i2c_dev->fast_clk); 394 clk_disable(i2c_dev->fast_clk);
395 } 395 }
396 return ret; 396 return ret;
397} 397}
398 398
399static inline void tegra_i2c_clock_disable(struct tegra_i2c_dev *i2c_dev) 399static inline void tegra_i2c_clock_disable(struct tegra_i2c_dev *i2c_dev)
400{ 400{
401 clk_disable_unprepare(i2c_dev->div_clk); 401 clk_disable(i2c_dev->div_clk);
402 if (!i2c_dev->hw->has_single_clk_source) 402 if (!i2c_dev->hw->has_single_clk_source)
403 clk_disable_unprepare(i2c_dev->fast_clk); 403 clk_disable(i2c_dev->fast_clk);
404} 404}
405 405
406static int tegra_i2c_init(struct tegra_i2c_dev *i2c_dev) 406static int tegra_i2c_init(struct tegra_i2c_dev *i2c_dev)
407{ 407{
408 u32 val; 408 u32 val;
409 int err = 0; 409 int err = 0;
410 int clk_multiplier = I2C_CLK_MULTIPLIER_STD_FAST_MODE;
411 u32 clk_divisor; 410 u32 clk_divisor;
412 411
413 err = tegra_i2c_clock_enable(i2c_dev); 412 err = tegra_i2c_clock_enable(i2c_dev);
@@ -428,9 +427,6 @@ static int tegra_i2c_init(struct tegra_i2c_dev *i2c_dev)
428 i2c_writel(i2c_dev, val, I2C_CNFG); 427 i2c_writel(i2c_dev, val, I2C_CNFG);
429 i2c_writel(i2c_dev, 0, I2C_INT_MASK); 428 i2c_writel(i2c_dev, 0, I2C_INT_MASK);
430 429
431 clk_multiplier *= (i2c_dev->hw->clk_divisor_std_fast_mode + 1);
432 clk_set_rate(i2c_dev->div_clk, i2c_dev->bus_clk_rate * clk_multiplier);
433
434 /* Make sure clock divisor programmed correctly */ 430 /* Make sure clock divisor programmed correctly */
435 clk_divisor = i2c_dev->hw->clk_divisor_hs_mode; 431 clk_divisor = i2c_dev->hw->clk_divisor_hs_mode;
436 clk_divisor |= i2c_dev->hw->clk_divisor_std_fast_mode << 432 clk_divisor |= i2c_dev->hw->clk_divisor_std_fast_mode <<
@@ -712,6 +708,7 @@ static int tegra_i2c_probe(struct platform_device *pdev)
712 void __iomem *base; 708 void __iomem *base;
713 int irq; 709 int irq;
714 int ret = 0; 710 int ret = 0;
711 int clk_multiplier = I2C_CLK_MULTIPLIER_STD_FAST_MODE;
715 712
716 res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 713 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
717 base = devm_ioremap_resource(&pdev->dev, res); 714 base = devm_ioremap_resource(&pdev->dev, res);
@@ -777,17 +774,39 @@ static int tegra_i2c_probe(struct platform_device *pdev)
777 774
778 platform_set_drvdata(pdev, i2c_dev); 775 platform_set_drvdata(pdev, i2c_dev);
779 776
777 if (!i2c_dev->hw->has_single_clk_source) {
778 ret = clk_prepare(i2c_dev->fast_clk);
779 if (ret < 0) {
780 dev_err(i2c_dev->dev, "Clock prepare failed %d\n", ret);
781 return ret;
782 }
783 }
784
785 clk_multiplier *= (i2c_dev->hw->clk_divisor_std_fast_mode + 1);
786 ret = clk_set_rate(i2c_dev->div_clk,
787 i2c_dev->bus_clk_rate * clk_multiplier);
788 if (ret) {
789 dev_err(i2c_dev->dev, "Clock rate change failed %d\n", ret);
790 goto unprepare_fast_clk;
791 }
792
793 ret = clk_prepare(i2c_dev->div_clk);
794 if (ret < 0) {
795 dev_err(i2c_dev->dev, "Clock prepare failed %d\n", ret);
796 goto unprepare_fast_clk;
797 }
798
780 ret = tegra_i2c_init(i2c_dev); 799 ret = tegra_i2c_init(i2c_dev);
781 if (ret) { 800 if (ret) {
782 dev_err(&pdev->dev, "Failed to initialize i2c controller"); 801 dev_err(&pdev->dev, "Failed to initialize i2c controller");
783 return ret; 802 goto unprepare_div_clk;
784 } 803 }
785 804
786 ret = devm_request_irq(&pdev->dev, i2c_dev->irq, 805 ret = devm_request_irq(&pdev->dev, i2c_dev->irq,
787 tegra_i2c_isr, 0, dev_name(&pdev->dev), i2c_dev); 806 tegra_i2c_isr, 0, dev_name(&pdev->dev), i2c_dev);
788 if (ret) { 807 if (ret) {
789 dev_err(&pdev->dev, "Failed to request irq %i\n", i2c_dev->irq); 808 dev_err(&pdev->dev, "Failed to request irq %i\n", i2c_dev->irq);
790 return ret; 809 goto unprepare_div_clk;
791 } 810 }
792 811
793 i2c_set_adapdata(&i2c_dev->adapter, i2c_dev); 812 i2c_set_adapdata(&i2c_dev->adapter, i2c_dev);
@@ -803,16 +822,30 @@ static int tegra_i2c_probe(struct platform_device *pdev)
803 ret = i2c_add_numbered_adapter(&i2c_dev->adapter); 822 ret = i2c_add_numbered_adapter(&i2c_dev->adapter);
804 if (ret) { 823 if (ret) {
805 dev_err(&pdev->dev, "Failed to add I2C adapter\n"); 824 dev_err(&pdev->dev, "Failed to add I2C adapter\n");
806 return ret; 825 goto unprepare_div_clk;
807 } 826 }
808 827
809 return 0; 828 return 0;
829
830unprepare_div_clk:
831 clk_unprepare(i2c_dev->div_clk);
832
833unprepare_fast_clk:
834 if (!i2c_dev->hw->has_single_clk_source)
835 clk_unprepare(i2c_dev->fast_clk);
836
837 return ret;
810} 838}
811 839
812static int tegra_i2c_remove(struct platform_device *pdev) 840static int tegra_i2c_remove(struct platform_device *pdev)
813{ 841{
814 struct tegra_i2c_dev *i2c_dev = platform_get_drvdata(pdev); 842 struct tegra_i2c_dev *i2c_dev = platform_get_drvdata(pdev);
815 i2c_del_adapter(&i2c_dev->adapter); 843 i2c_del_adapter(&i2c_dev->adapter);
844
845 clk_unprepare(i2c_dev->div_clk);
846 if (!i2c_dev->hw->has_single_clk_source)
847 clk_unprepare(i2c_dev->fast_clk);
848
816 return 0; 849 return 0;
817} 850}
818 851
diff --git a/drivers/i2c/i2c-acpi.c b/drivers/i2c/i2c-acpi.c
deleted file mode 100644
index 0dbc18c15c43..000000000000
--- a/drivers/i2c/i2c-acpi.c
+++ /dev/null
@@ -1,364 +0,0 @@
1/*
2 * I2C ACPI code
3 *
4 * Copyright (C) 2014 Intel Corp
5 *
6 * Author: Lan Tianyu <tianyu.lan@intel.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 * for more details.
16 */
17#define pr_fmt(fmt) "I2C/ACPI : " fmt
18
19#include <linux/kernel.h>
20#include <linux/errno.h>
21#include <linux/err.h>
22#include <linux/i2c.h>
23#include <linux/acpi.h>
24
25struct acpi_i2c_handler_data {
26 struct acpi_connection_info info;
27 struct i2c_adapter *adapter;
28};
29
30struct gsb_buffer {
31 u8 status;
32 u8 len;
33 union {
34 u16 wdata;
35 u8 bdata;
36 u8 data[0];
37 };
38} __packed;
39
40static int acpi_i2c_add_resource(struct acpi_resource *ares, void *data)
41{
42 struct i2c_board_info *info = data;
43
44 if (ares->type == ACPI_RESOURCE_TYPE_SERIAL_BUS) {
45 struct acpi_resource_i2c_serialbus *sb;
46
47 sb = &ares->data.i2c_serial_bus;
48 if (sb->type == ACPI_RESOURCE_SERIAL_TYPE_I2C) {
49 info->addr = sb->slave_address;
50 if (sb->access_mode == ACPI_I2C_10BIT_MODE)
51 info->flags |= I2C_CLIENT_TEN;
52 }
53 } else if (info->irq < 0) {
54 struct resource r;
55
56 if (acpi_dev_resource_interrupt(ares, 0, &r))
57 info->irq = r.start;
58 }
59
60 /* Tell the ACPI core to skip this resource */
61 return 1;
62}
63
64static acpi_status acpi_i2c_add_device(acpi_handle handle, u32 level,
65 void *data, void **return_value)
66{
67 struct i2c_adapter *adapter = data;
68 struct list_head resource_list;
69 struct i2c_board_info info;
70 struct acpi_device *adev;
71 int ret;
72
73 if (acpi_bus_get_device(handle, &adev))
74 return AE_OK;
75 if (acpi_bus_get_status(adev) || !adev->status.present)
76 return AE_OK;
77
78 memset(&info, 0, sizeof(info));
79 info.acpi_node.companion = adev;
80 info.irq = -1;
81
82 INIT_LIST_HEAD(&resource_list);
83 ret = acpi_dev_get_resources(adev, &resource_list,
84 acpi_i2c_add_resource, &info);
85 acpi_dev_free_resource_list(&resource_list);
86
87 if (ret < 0 || !info.addr)
88 return AE_OK;
89
90 adev->power.flags.ignore_parent = true;
91 strlcpy(info.type, dev_name(&adev->dev), sizeof(info.type));
92 if (!i2c_new_device(adapter, &info)) {
93 adev->power.flags.ignore_parent = false;
94 dev_err(&adapter->dev,
95 "failed to add I2C device %s from ACPI\n",
96 dev_name(&adev->dev));
97 }
98
99 return AE_OK;
100}
101
102/**
103 * acpi_i2c_register_devices - enumerate I2C slave devices behind adapter
104 * @adap: pointer to adapter
105 *
106 * Enumerate all I2C slave devices behind this adapter by walking the ACPI
107 * namespace. When a device is found it will be added to the Linux device
108 * model and bound to the corresponding ACPI handle.
109 */
110void acpi_i2c_register_devices(struct i2c_adapter *adap)
111{
112 acpi_handle handle;
113 acpi_status status;
114
115 if (!adap->dev.parent)
116 return;
117
118 handle = ACPI_HANDLE(adap->dev.parent);
119 if (!handle)
120 return;
121
122 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1,
123 acpi_i2c_add_device, NULL,
124 adap, NULL);
125 if (ACPI_FAILURE(status))
126 dev_warn(&adap->dev, "failed to enumerate I2C slaves\n");
127}
128
129#ifdef CONFIG_ACPI_I2C_OPREGION
130static int acpi_gsb_i2c_read_bytes(struct i2c_client *client,
131 u8 cmd, u8 *data, u8 data_len)
132{
133
134 struct i2c_msg msgs[2];
135 int ret;
136 u8 *buffer;
137
138 buffer = kzalloc(data_len, GFP_KERNEL);
139 if (!buffer)
140 return AE_NO_MEMORY;
141
142 msgs[0].addr = client->addr;
143 msgs[0].flags = client->flags;
144 msgs[0].len = 1;
145 msgs[0].buf = &cmd;
146
147 msgs[1].addr = client->addr;
148 msgs[1].flags = client->flags | I2C_M_RD;
149 msgs[1].len = data_len;
150 msgs[1].buf = buffer;
151
152 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
153 if (ret < 0)
154 dev_err(&client->adapter->dev, "i2c read failed\n");
155 else
156 memcpy(data, buffer, data_len);
157
158 kfree(buffer);
159 return ret;
160}
161
162static int acpi_gsb_i2c_write_bytes(struct i2c_client *client,
163 u8 cmd, u8 *data, u8 data_len)
164{
165
166 struct i2c_msg msgs[1];
167 u8 *buffer;
168 int ret = AE_OK;
169
170 buffer = kzalloc(data_len + 1, GFP_KERNEL);
171 if (!buffer)
172 return AE_NO_MEMORY;
173
174 buffer[0] = cmd;
175 memcpy(buffer + 1, data, data_len);
176
177 msgs[0].addr = client->addr;
178 msgs[0].flags = client->flags;
179 msgs[0].len = data_len + 1;
180 msgs[0].buf = buffer;
181
182 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
183 if (ret < 0)
184 dev_err(&client->adapter->dev, "i2c write failed\n");
185
186 kfree(buffer);
187 return ret;
188}
189
190static acpi_status
191acpi_i2c_space_handler(u32 function, acpi_physical_address command,
192 u32 bits, u64 *value64,
193 void *handler_context, void *region_context)
194{
195 struct gsb_buffer *gsb = (struct gsb_buffer *)value64;
196 struct acpi_i2c_handler_data *data = handler_context;
197 struct acpi_connection_info *info = &data->info;
198 struct acpi_resource_i2c_serialbus *sb;
199 struct i2c_adapter *adapter = data->adapter;
200 struct i2c_client client;
201 struct acpi_resource *ares;
202 u32 accessor_type = function >> 16;
203 u8 action = function & ACPI_IO_MASK;
204 acpi_status ret = AE_OK;
205 int status;
206
207 ret = acpi_buffer_to_resource(info->connection, info->length, &ares);
208 if (ACPI_FAILURE(ret))
209 return ret;
210
211 if (!value64 || ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS) {
212 ret = AE_BAD_PARAMETER;
213 goto err;
214 }
215
216 sb = &ares->data.i2c_serial_bus;
217 if (sb->type != ACPI_RESOURCE_SERIAL_TYPE_I2C) {
218 ret = AE_BAD_PARAMETER;
219 goto err;
220 }
221
222 memset(&client, 0, sizeof(client));
223 client.adapter = adapter;
224 client.addr = sb->slave_address;
225 client.flags = 0;
226
227 if (sb->access_mode == ACPI_I2C_10BIT_MODE)
228 client.flags |= I2C_CLIENT_TEN;
229
230 switch (accessor_type) {
231 case ACPI_GSB_ACCESS_ATTRIB_SEND_RCV:
232 if (action == ACPI_READ) {
233 status = i2c_smbus_read_byte(&client);
234 if (status >= 0) {
235 gsb->bdata = status;
236 status = 0;
237 }
238 } else {
239 status = i2c_smbus_write_byte(&client, gsb->bdata);
240 }
241 break;
242
243 case ACPI_GSB_ACCESS_ATTRIB_BYTE:
244 if (action == ACPI_READ) {
245 status = i2c_smbus_read_byte_data(&client, command);
246 if (status >= 0) {
247 gsb->bdata = status;
248 status = 0;
249 }
250 } else {
251 status = i2c_smbus_write_byte_data(&client, command,
252 gsb->bdata);
253 }
254 break;
255
256 case ACPI_GSB_ACCESS_ATTRIB_WORD:
257 if (action == ACPI_READ) {
258 status = i2c_smbus_read_word_data(&client, command);
259 if (status >= 0) {
260 gsb->wdata = status;
261 status = 0;
262 }
263 } else {
264 status = i2c_smbus_write_word_data(&client, command,
265 gsb->wdata);
266 }
267 break;
268
269 case ACPI_GSB_ACCESS_ATTRIB_BLOCK:
270 if (action == ACPI_READ) {
271 status = i2c_smbus_read_block_data(&client, command,
272 gsb->data);
273 if (status >= 0) {
274 gsb->len = status;
275 status = 0;
276 }
277 } else {
278 status = i2c_smbus_write_block_data(&client, command,
279 gsb->len, gsb->data);
280 }
281 break;
282
283 case ACPI_GSB_ACCESS_ATTRIB_MULTIBYTE:
284 if (action == ACPI_READ) {
285 status = acpi_gsb_i2c_read_bytes(&client, command,
286 gsb->data, info->access_length);
287 if (status > 0)
288 status = 0;
289 } else {
290 status = acpi_gsb_i2c_write_bytes(&client, command,
291 gsb->data, info->access_length);
292 }
293 break;
294
295 default:
296 pr_info("protocol(0x%02x) is not supported.\n", accessor_type);
297 ret = AE_BAD_PARAMETER;
298 goto err;
299 }
300
301 gsb->status = status;
302
303 err:
304 ACPI_FREE(ares);
305 return ret;
306}
307
308
309int acpi_i2c_install_space_handler(struct i2c_adapter *adapter)
310{
311 acpi_handle handle = ACPI_HANDLE(adapter->dev.parent);
312 struct acpi_i2c_handler_data *data;
313 acpi_status status;
314
315 if (!handle)
316 return -ENODEV;
317
318 data = kzalloc(sizeof(struct acpi_i2c_handler_data),
319 GFP_KERNEL);
320 if (!data)
321 return -ENOMEM;
322
323 data->adapter = adapter;
324 status = acpi_bus_attach_private_data(handle, (void *)data);
325 if (ACPI_FAILURE(status)) {
326 kfree(data);
327 return -ENOMEM;
328 }
329
330 status = acpi_install_address_space_handler(handle,
331 ACPI_ADR_SPACE_GSBUS,
332 &acpi_i2c_space_handler,
333 NULL,
334 data);
335 if (ACPI_FAILURE(status)) {
336 dev_err(&adapter->dev, "Error installing i2c space handler\n");
337 acpi_bus_detach_private_data(handle);
338 kfree(data);
339 return -ENOMEM;
340 }
341
342 return 0;
343}
344
345void acpi_i2c_remove_space_handler(struct i2c_adapter *adapter)
346{
347 acpi_handle handle = ACPI_HANDLE(adapter->dev.parent);
348 struct acpi_i2c_handler_data *data;
349 acpi_status status;
350
351 if (!handle)
352 return;
353
354 acpi_remove_address_space_handler(handle,
355 ACPI_ADR_SPACE_GSBUS,
356 &acpi_i2c_space_handler);
357
358 status = acpi_bus_get_private_data(handle, (void **)&data);
359 if (ACPI_SUCCESS(status))
360 kfree(data);
361
362 acpi_bus_detach_private_data(handle);
363}
364#endif
diff --git a/drivers/i2c/i2c-core.c b/drivers/i2c/i2c-core.c
index 632057a44615..ccfbbab82a15 100644
--- a/drivers/i2c/i2c-core.c
+++ b/drivers/i2c/i2c-core.c
@@ -27,6 +27,8 @@
27 OF support is copyright (c) 2008 Jochen Friedrich <jochen@scram.de> 27 OF support is copyright (c) 2008 Jochen Friedrich <jochen@scram.de>
28 (based on a previous patch from Jon Smirl <jonsmirl@gmail.com>) and 28 (based on a previous patch from Jon Smirl <jonsmirl@gmail.com>) and
29 (c) 2013 Wolfram Sang <wsa@the-dreams.de> 29 (c) 2013 Wolfram Sang <wsa@the-dreams.de>
30 I2C ACPI code Copyright (C) 2014 Intel Corp
31 Author: Lan Tianyu <tianyu.lan@intel.com>
30 */ 32 */
31 33
32#include <linux/module.h> 34#include <linux/module.h>
@@ -78,6 +80,368 @@ void i2c_transfer_trace_unreg(void)
78 static_key_slow_dec(&i2c_trace_msg); 80 static_key_slow_dec(&i2c_trace_msg);
79} 81}
80 82
83#if defined(CONFIG_ACPI)
84struct acpi_i2c_handler_data {
85 struct acpi_connection_info info;
86 struct i2c_adapter *adapter;
87};
88
89struct gsb_buffer {
90 u8 status;
91 u8 len;
92 union {
93 u16 wdata;
94 u8 bdata;
95 u8 data[0];
96 };
97} __packed;
98
99static int acpi_i2c_add_resource(struct acpi_resource *ares, void *data)
100{
101 struct i2c_board_info *info = data;
102
103 if (ares->type == ACPI_RESOURCE_TYPE_SERIAL_BUS) {
104 struct acpi_resource_i2c_serialbus *sb;
105
106 sb = &ares->data.i2c_serial_bus;
107 if (sb->type == ACPI_RESOURCE_SERIAL_TYPE_I2C) {
108 info->addr = sb->slave_address;
109 if (sb->access_mode == ACPI_I2C_10BIT_MODE)
110 info->flags |= I2C_CLIENT_TEN;
111 }
112 } else if (info->irq < 0) {
113 struct resource r;
114
115 if (acpi_dev_resource_interrupt(ares, 0, &r))
116 info->irq = r.start;
117 }
118
119 /* Tell the ACPI core to skip this resource */
120 return 1;
121}
122
123static acpi_status acpi_i2c_add_device(acpi_handle handle, u32 level,
124 void *data, void **return_value)
125{
126 struct i2c_adapter *adapter = data;
127 struct list_head resource_list;
128 struct i2c_board_info info;
129 struct acpi_device *adev;
130 int ret;
131
132 if (acpi_bus_get_device(handle, &adev))
133 return AE_OK;
134 if (acpi_bus_get_status(adev) || !adev->status.present)
135 return AE_OK;
136
137 memset(&info, 0, sizeof(info));
138 info.acpi_node.companion = adev;
139 info.irq = -1;
140
141 INIT_LIST_HEAD(&resource_list);
142 ret = acpi_dev_get_resources(adev, &resource_list,
143 acpi_i2c_add_resource, &info);
144 acpi_dev_free_resource_list(&resource_list);
145
146 if (ret < 0 || !info.addr)
147 return AE_OK;
148
149 adev->power.flags.ignore_parent = true;
150 strlcpy(info.type, dev_name(&adev->dev), sizeof(info.type));
151 if (!i2c_new_device(adapter, &info)) {
152 adev->power.flags.ignore_parent = false;
153 dev_err(&adapter->dev,
154 "failed to add I2C device %s from ACPI\n",
155 dev_name(&adev->dev));
156 }
157
158 return AE_OK;
159}
160
161/**
162 * acpi_i2c_register_devices - enumerate I2C slave devices behind adapter
163 * @adap: pointer to adapter
164 *
165 * Enumerate all I2C slave devices behind this adapter by walking the ACPI
166 * namespace. When a device is found it will be added to the Linux device
167 * model and bound to the corresponding ACPI handle.
168 */
169static void acpi_i2c_register_devices(struct i2c_adapter *adap)
170{
171 acpi_handle handle;
172 acpi_status status;
173
174 if (!adap->dev.parent)
175 return;
176
177 handle = ACPI_HANDLE(adap->dev.parent);
178 if (!handle)
179 return;
180
181 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1,
182 acpi_i2c_add_device, NULL,
183 adap, NULL);
184 if (ACPI_FAILURE(status))
185 dev_warn(&adap->dev, "failed to enumerate I2C slaves\n");
186}
187
188#else /* CONFIG_ACPI */
189static inline void acpi_i2c_register_devices(struct i2c_adapter *adap) { }
190#endif /* CONFIG_ACPI */
191
192#ifdef CONFIG_ACPI_I2C_OPREGION
193static int acpi_gsb_i2c_read_bytes(struct i2c_client *client,
194 u8 cmd, u8 *data, u8 data_len)
195{
196
197 struct i2c_msg msgs[2];
198 int ret;
199 u8 *buffer;
200
201 buffer = kzalloc(data_len, GFP_KERNEL);
202 if (!buffer)
203 return AE_NO_MEMORY;
204
205 msgs[0].addr = client->addr;
206 msgs[0].flags = client->flags;
207 msgs[0].len = 1;
208 msgs[0].buf = &cmd;
209
210 msgs[1].addr = client->addr;
211 msgs[1].flags = client->flags | I2C_M_RD;
212 msgs[1].len = data_len;
213 msgs[1].buf = buffer;
214
215 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
216 if (ret < 0)
217 dev_err(&client->adapter->dev, "i2c read failed\n");
218 else
219 memcpy(data, buffer, data_len);
220
221 kfree(buffer);
222 return ret;
223}
224
225static int acpi_gsb_i2c_write_bytes(struct i2c_client *client,
226 u8 cmd, u8 *data, u8 data_len)
227{
228
229 struct i2c_msg msgs[1];
230 u8 *buffer;
231 int ret = AE_OK;
232
233 buffer = kzalloc(data_len + 1, GFP_KERNEL);
234 if (!buffer)
235 return AE_NO_MEMORY;
236
237 buffer[0] = cmd;
238 memcpy(buffer + 1, data, data_len);
239
240 msgs[0].addr = client->addr;
241 msgs[0].flags = client->flags;
242 msgs[0].len = data_len + 1;
243 msgs[0].buf = buffer;
244
245 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
246 if (ret < 0)
247 dev_err(&client->adapter->dev, "i2c write failed\n");
248
249 kfree(buffer);
250 return ret;
251}
252
253static acpi_status
254acpi_i2c_space_handler(u32 function, acpi_physical_address command,
255 u32 bits, u64 *value64,
256 void *handler_context, void *region_context)
257{
258 struct gsb_buffer *gsb = (struct gsb_buffer *)value64;
259 struct acpi_i2c_handler_data *data = handler_context;
260 struct acpi_connection_info *info = &data->info;
261 struct acpi_resource_i2c_serialbus *sb;
262 struct i2c_adapter *adapter = data->adapter;
263 struct i2c_client client;
264 struct acpi_resource *ares;
265 u32 accessor_type = function >> 16;
266 u8 action = function & ACPI_IO_MASK;
267 acpi_status ret = AE_OK;
268 int status;
269
270 ret = acpi_buffer_to_resource(info->connection, info->length, &ares);
271 if (ACPI_FAILURE(ret))
272 return ret;
273
274 if (!value64 || ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS) {
275 ret = AE_BAD_PARAMETER;
276 goto err;
277 }
278
279 sb = &ares->data.i2c_serial_bus;
280 if (sb->type != ACPI_RESOURCE_SERIAL_TYPE_I2C) {
281 ret = AE_BAD_PARAMETER;
282 goto err;
283 }
284
285 memset(&client, 0, sizeof(client));
286 client.adapter = adapter;
287 client.addr = sb->slave_address;
288 client.flags = 0;
289
290 if (sb->access_mode == ACPI_I2C_10BIT_MODE)
291 client.flags |= I2C_CLIENT_TEN;
292
293 switch (accessor_type) {
294 case ACPI_GSB_ACCESS_ATTRIB_SEND_RCV:
295 if (action == ACPI_READ) {
296 status = i2c_smbus_read_byte(&client);
297 if (status >= 0) {
298 gsb->bdata = status;
299 status = 0;
300 }
301 } else {
302 status = i2c_smbus_write_byte(&client, gsb->bdata);
303 }
304 break;
305
306 case ACPI_GSB_ACCESS_ATTRIB_BYTE:
307 if (action == ACPI_READ) {
308 status = i2c_smbus_read_byte_data(&client, command);
309 if (status >= 0) {
310 gsb->bdata = status;
311 status = 0;
312 }
313 } else {
314 status = i2c_smbus_write_byte_data(&client, command,
315 gsb->bdata);
316 }
317 break;
318
319 case ACPI_GSB_ACCESS_ATTRIB_WORD:
320 if (action == ACPI_READ) {
321 status = i2c_smbus_read_word_data(&client, command);
322 if (status >= 0) {
323 gsb->wdata = status;
324 status = 0;
325 }
326 } else {
327 status = i2c_smbus_write_word_data(&client, command,
328 gsb->wdata);
329 }
330 break;
331
332 case ACPI_GSB_ACCESS_ATTRIB_BLOCK:
333 if (action == ACPI_READ) {
334 status = i2c_smbus_read_block_data(&client, command,
335 gsb->data);
336 if (status >= 0) {
337 gsb->len = status;
338 status = 0;
339 }
340 } else {
341 status = i2c_smbus_write_block_data(&client, command,
342 gsb->len, gsb->data);
343 }
344 break;
345
346 case ACPI_GSB_ACCESS_ATTRIB_MULTIBYTE:
347 if (action == ACPI_READ) {
348 status = acpi_gsb_i2c_read_bytes(&client, command,
349 gsb->data, info->access_length);
350 if (status > 0)
351 status = 0;
352 } else {
353 status = acpi_gsb_i2c_write_bytes(&client, command,
354 gsb->data, info->access_length);
355 }
356 break;
357
358 default:
359 pr_info("protocol(0x%02x) is not supported.\n", accessor_type);
360 ret = AE_BAD_PARAMETER;
361 goto err;
362 }
363
364 gsb->status = status;
365
366 err:
367 ACPI_FREE(ares);
368 return ret;
369}
370
371
372static int acpi_i2c_install_space_handler(struct i2c_adapter *adapter)
373{
374 acpi_handle handle;
375 struct acpi_i2c_handler_data *data;
376 acpi_status status;
377
378 if (!adapter->dev.parent)
379 return -ENODEV;
380
381 handle = ACPI_HANDLE(adapter->dev.parent);
382
383 if (!handle)
384 return -ENODEV;
385
386 data = kzalloc(sizeof(struct acpi_i2c_handler_data),
387 GFP_KERNEL);
388 if (!data)
389 return -ENOMEM;
390
391 data->adapter = adapter;
392 status = acpi_bus_attach_private_data(handle, (void *)data);
393 if (ACPI_FAILURE(status)) {
394 kfree(data);
395 return -ENOMEM;
396 }
397
398 status = acpi_install_address_space_handler(handle,
399 ACPI_ADR_SPACE_GSBUS,
400 &acpi_i2c_space_handler,
401 NULL,
402 data);
403 if (ACPI_FAILURE(status)) {
404 dev_err(&adapter->dev, "Error installing i2c space handler\n");
405 acpi_bus_detach_private_data(handle);
406 kfree(data);
407 return -ENOMEM;
408 }
409
410 return 0;
411}
412
413static void acpi_i2c_remove_space_handler(struct i2c_adapter *adapter)
414{
415 acpi_handle handle;
416 struct acpi_i2c_handler_data *data;
417 acpi_status status;
418
419 if (!adapter->dev.parent)
420 return;
421
422 handle = ACPI_HANDLE(adapter->dev.parent);
423
424 if (!handle)
425 return;
426
427 acpi_remove_address_space_handler(handle,
428 ACPI_ADR_SPACE_GSBUS,
429 &acpi_i2c_space_handler);
430
431 status = acpi_bus_get_private_data(handle, (void **)&data);
432 if (ACPI_SUCCESS(status))
433 kfree(data);
434
435 acpi_bus_detach_private_data(handle);
436}
437#else /* CONFIG_ACPI_I2C_OPREGION */
438static inline void acpi_i2c_remove_space_handler(struct i2c_adapter *adapter)
439{ }
440
441static inline int acpi_i2c_install_space_handler(struct i2c_adapter *adapter)
442{ return 0; }
443#endif /* CONFIG_ACPI_I2C_OPREGION */
444
81/* ------------------------------------------------------------------------- */ 445/* ------------------------------------------------------------------------- */
82 446
83static const struct i2c_device_id *i2c_match_id(const struct i2c_device_id *id, 447static const struct i2c_device_id *i2c_match_id(const struct i2c_device_id *id,
diff --git a/include/linux/i2c.h b/include/linux/i2c.h
index a95efeb53a8b..b556e0ab946f 100644
--- a/include/linux/i2c.h
+++ b/include/linux/i2c.h
@@ -577,20 +577,4 @@ static inline struct i2c_adapter *of_find_i2c_adapter_by_node(struct device_node
577} 577}
578#endif /* CONFIG_OF */ 578#endif /* CONFIG_OF */
579 579
580#ifdef CONFIG_ACPI
581void acpi_i2c_register_devices(struct i2c_adapter *adap);
582#else
583static inline void acpi_i2c_register_devices(struct i2c_adapter *adap) { }
584#endif /* CONFIG_ACPI */
585
586#ifdef CONFIG_ACPI_I2C_OPREGION
587int acpi_i2c_install_space_handler(struct i2c_adapter *adapter);
588void acpi_i2c_remove_space_handler(struct i2c_adapter *adapter);
589#else
590static inline void acpi_i2c_remove_space_handler(struct i2c_adapter *adapter)
591{ }
592static inline int acpi_i2c_install_space_handler(struct i2c_adapter *adapter)
593{ return 0; }
594#endif /* CONFIG_ACPI_I2C_OPREGION */
595
596#endif /* _LINUX_I2C_H */ 580#endif /* _LINUX_I2C_H */