aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/i2c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/i2c')
-rw-r--r--drivers/i2c/Kconfig15
-rw-r--r--drivers/i2c/Makefile5
-rw-r--r--drivers/i2c/busses/i2c-at91.c32
-rw-r--r--drivers/i2c/busses/i2c-i801.c2
-rw-r--r--drivers/i2c/busses/i2c-ismt.c4
-rw-r--r--drivers/i2c/busses/i2c-mv64xxx.c3
-rw-r--r--drivers/i2c/busses/i2c-mxs.c2
-rw-r--r--drivers/i2c/busses/i2c-rcar.c41
-rw-r--r--drivers/i2c/busses/i2c-rk3x.c15
-rw-r--r--drivers/i2c/busses/i2c-tegra.c57
-rw-r--r--drivers/i2c/i2c-acpi.c362
-rw-r--r--drivers/i2c/i2c-core.c364
12 files changed, 490 insertions, 412 deletions
diff --git a/drivers/i2c/Kconfig b/drivers/i2c/Kconfig
index 3e3b680dc007..b51a402752c4 100644
--- a/drivers/i2c/Kconfig
+++ b/drivers/i2c/Kconfig
@@ -23,17 +23,14 @@ config I2C
23 This I2C support can also be built as a module. If so, the module 23 This I2C support can also be built as a module. If so, the module
24 will be called i2c-core. 24 will be called i2c-core.
25 25
26config I2C_ACPI 26config ACPI_I2C_OPREGION
27 bool "I2C ACPI support" 27 bool "ACPI I2C Operation region support"
28 select I2C 28 depends on I2C=y && ACPI
29 depends on ACPI
30 default y 29 default y
31 help 30 help
32 Say Y here if you want to enable ACPI I2C support. This includes support 31 Say Y here if you want to enable ACPI I2C operation region support.
33 for automatic enumeration of I2C slave devices and support for ACPI I2C 32 Operation Regions allow firmware (BIOS) code to access I2C slave devices,
34 Operation Regions. Operation Regions allow firmware (BIOS) code to 33 such as smart batteries through an I2C host controller driver.
35 access I2C slave devices, such as smart batteries through an I2C host
36 controller driver.
37 34
38if I2C 35if I2C
39 36
diff --git a/drivers/i2c/Makefile b/drivers/i2c/Makefile
index a1f590cbb435..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_I2C_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-at91.c b/drivers/i2c/busses/i2c-at91.c
index 79a68999a696..917d54588d95 100644
--- a/drivers/i2c/busses/i2c-at91.c
+++ b/drivers/i2c/busses/i2c-at91.c
@@ -101,6 +101,7 @@ struct at91_twi_dev {
101 unsigned twi_cwgr_reg; 101 unsigned twi_cwgr_reg;
102 struct at91_twi_pdata *pdata; 102 struct at91_twi_pdata *pdata;
103 bool use_dma; 103 bool use_dma;
104 bool recv_len_abort;
104 struct at91_twi_dma dma; 105 struct at91_twi_dma dma;
105}; 106};
106 107
@@ -267,12 +268,24 @@ static void at91_twi_read_next_byte(struct at91_twi_dev *dev)
267 *dev->buf = at91_twi_read(dev, AT91_TWI_RHR) & 0xff; 268 *dev->buf = at91_twi_read(dev, AT91_TWI_RHR) & 0xff;
268 --dev->buf_len; 269 --dev->buf_len;
269 270
271 /* return if aborting, we only needed to read RHR to clear RXRDY*/
272 if (dev->recv_len_abort)
273 return;
274
270 /* handle I2C_SMBUS_BLOCK_DATA */ 275 /* handle I2C_SMBUS_BLOCK_DATA */
271 if (unlikely(dev->msg->flags & I2C_M_RECV_LEN)) { 276 if (unlikely(dev->msg->flags & I2C_M_RECV_LEN)) {
272 dev->msg->flags &= ~I2C_M_RECV_LEN; 277 /* ensure length byte is a valid value */
273 dev->buf_len += *dev->buf; 278 if (*dev->buf <= I2C_SMBUS_BLOCK_MAX && *dev->buf > 0) {
274 dev->msg->len = dev->buf_len + 1; 279 dev->msg->flags &= ~I2C_M_RECV_LEN;
275 dev_dbg(dev->dev, "received block length %d\n", dev->buf_len); 280 dev->buf_len += *dev->buf;
281 dev->msg->len = dev->buf_len + 1;
282 dev_dbg(dev->dev, "received block length %d\n",
283 dev->buf_len);
284 } else {
285 /* abort and send the stop by reading one more byte */
286 dev->recv_len_abort = true;
287 dev->buf_len = 1;
288 }
276 } 289 }
277 290
278 /* send stop if second but last byte has been read */ 291 /* send stop if second but last byte has been read */
@@ -421,8 +434,8 @@ static int at91_do_twi_transfer(struct at91_twi_dev *dev)
421 } 434 }
422 } 435 }
423 436
424 ret = wait_for_completion_interruptible_timeout(&dev->cmd_complete, 437 ret = wait_for_completion_io_timeout(&dev->cmd_complete,
425 dev->adapter.timeout); 438 dev->adapter.timeout);
426 if (ret == 0) { 439 if (ret == 0) {
427 dev_err(dev->dev, "controller timed out\n"); 440 dev_err(dev->dev, "controller timed out\n");
428 at91_init_twi_bus(dev); 441 at91_init_twi_bus(dev);
@@ -444,6 +457,12 @@ static int at91_do_twi_transfer(struct at91_twi_dev *dev)
444 ret = -EIO; 457 ret = -EIO;
445 goto error; 458 goto error;
446 } 459 }
460 if (dev->recv_len_abort) {
461 dev_err(dev->dev, "invalid smbus block length recvd\n");
462 ret = -EPROTO;
463 goto error;
464 }
465
447 dev_dbg(dev->dev, "transfer complete\n"); 466 dev_dbg(dev->dev, "transfer complete\n");
448 467
449 return 0; 468 return 0;
@@ -500,6 +519,7 @@ static int at91_twi_xfer(struct i2c_adapter *adap, struct i2c_msg *msg, int num)
500 dev->buf_len = m_start->len; 519 dev->buf_len = m_start->len;
501 dev->buf = m_start->buf; 520 dev->buf = m_start->buf;
502 dev->msg = m_start; 521 dev->msg = m_start;
522 dev->recv_len_abort = false;
503 523
504 ret = at91_do_twi_transfer(dev); 524 ret = at91_do_twi_transfer(dev);
505 525
diff --git a/drivers/i2c/busses/i2c-i801.c b/drivers/i2c/busses/i2c-i801.c
index 2994690b26e9..10467a327749 100644
--- a/drivers/i2c/busses/i2c-i801.c
+++ b/drivers/i2c/busses/i2c-i801.c
@@ -164,6 +164,7 @@
164 164
165/* Older devices have their ID defined in <linux/pci_ids.h> */ 165/* Older devices have their ID defined in <linux/pci_ids.h> */
166#define PCI_DEVICE_ID_INTEL_BAYTRAIL_SMBUS 0x0f12 166#define PCI_DEVICE_ID_INTEL_BAYTRAIL_SMBUS 0x0f12
167#define PCI_DEVICE_ID_INTEL_BRASWELL_SMBUS 0x2292
167#define PCI_DEVICE_ID_INTEL_COUGARPOINT_SMBUS 0x1c22 168#define PCI_DEVICE_ID_INTEL_COUGARPOINT_SMBUS 0x1c22
168#define PCI_DEVICE_ID_INTEL_PATSBURG_SMBUS 0x1d22 169#define PCI_DEVICE_ID_INTEL_PATSBURG_SMBUS 0x1d22
169/* Patsburg also has three 'Integrated Device Function' SMBus controllers */ 170/* Patsburg also has three 'Integrated Device Function' SMBus controllers */
@@ -828,6 +829,7 @@ static const struct pci_device_id i801_ids[] = {
828 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_WILDCATPOINT_SMBUS) }, 829 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_WILDCATPOINT_SMBUS) },
829 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_WILDCATPOINT_LP_SMBUS) }, 830 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_WILDCATPOINT_LP_SMBUS) },
830 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_BAYTRAIL_SMBUS) }, 831 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_BAYTRAIL_SMBUS) },
832 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_BRASWELL_SMBUS) },
831 { 0, } 833 { 0, }
832}; 834};
833 835
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-mv64xxx.c b/drivers/i2c/busses/i2c-mv64xxx.c
index 6dc5ded86f62..2f64273d3f2b 100644
--- a/drivers/i2c/busses/i2c-mv64xxx.c
+++ b/drivers/i2c/busses/i2c-mv64xxx.c
@@ -746,8 +746,7 @@ mv64xxx_of_config(struct mv64xxx_i2c_data *drv_data,
746 } 746 }
747 tclk = clk_get_rate(drv_data->clk); 747 tclk = clk_get_rate(drv_data->clk);
748 748
749 rc = of_property_read_u32(np, "clock-frequency", &bus_freq); 749 if (of_property_read_u32(np, "clock-frequency", &bus_freq))
750 if (rc)
751 bus_freq = 100000; /* 100kHz by default */ 750 bus_freq = 100000; /* 100kHz by default */
752 751
753 if (!mv64xxx_find_baud_factors(bus_freq, tclk, 752 if (!mv64xxx_find_baud_factors(bus_freq, tclk,
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 f3c7139dfa25..e506fcd3ca04 100644
--- a/drivers/i2c/busses/i2c-rcar.c
+++ b/drivers/i2c/busses/i2c-rcar.c
@@ -34,6 +34,7 @@
34#include <linux/platform_device.h> 34#include <linux/platform_device.h>
35#include <linux/pm_runtime.h> 35#include <linux/pm_runtime.h>
36#include <linux/slab.h> 36#include <linux/slab.h>
37#include <linux/spinlock.h>
37 38
38/* register offsets */ 39/* register offsets */
39#define ICSCR 0x00 /* slave ctrl */ 40#define ICSCR 0x00 /* slave ctrl */
@@ -75,8 +76,8 @@
75#define RCAR_IRQ_RECV (MNR | MAL | MST | MAT | MDR) 76#define RCAR_IRQ_RECV (MNR | MAL | MST | MAT | MDR)
76#define RCAR_IRQ_STOP (MST) 77#define RCAR_IRQ_STOP (MST)
77 78
78#define RCAR_IRQ_ACK_SEND (~(MAT | MDE)) 79#define RCAR_IRQ_ACK_SEND (~(MAT | MDE) & 0xFF)
79#define RCAR_IRQ_ACK_RECV (~(MAT | MDR)) 80#define RCAR_IRQ_ACK_RECV (~(MAT | MDR) & 0xFF)
80 81
81#define ID_LAST_MSG (1 << 0) 82#define ID_LAST_MSG (1 << 0)
82#define ID_IOERROR (1 << 1) 83#define ID_IOERROR (1 << 1)
@@ -95,6 +96,7 @@ struct rcar_i2c_priv {
95 struct i2c_msg *msg; 96 struct i2c_msg *msg;
96 struct clk *clk; 97 struct clk *clk;
97 98
99 spinlock_t lock;
98 wait_queue_head_t wait; 100 wait_queue_head_t wait;
99 101
100 int pos; 102 int pos;
@@ -365,20 +367,20 @@ static irqreturn_t rcar_i2c_irq(int irq, void *ptr)
365 struct rcar_i2c_priv *priv = ptr; 367 struct rcar_i2c_priv *priv = ptr;
366 u32 msr; 368 u32 msr;
367 369
370 /*-------------- spin lock -----------------*/
371 spin_lock(&priv->lock);
372
368 msr = rcar_i2c_read(priv, ICMSR); 373 msr = rcar_i2c_read(priv, ICMSR);
369 374
375 /* Only handle interrupts that are currently enabled */
376 msr &= rcar_i2c_read(priv, ICMIER);
377
370 /* Arbitration lost */ 378 /* Arbitration lost */
371 if (msr & MAL) { 379 if (msr & MAL) {
372 rcar_i2c_flags_set(priv, (ID_DONE | ID_ARBLOST)); 380 rcar_i2c_flags_set(priv, (ID_DONE | ID_ARBLOST));
373 goto out; 381 goto out;
374 } 382 }
375 383
376 /* Stop */
377 if (msr & MST) {
378 rcar_i2c_flags_set(priv, ID_DONE);
379 goto out;
380 }
381
382 /* Nack */ 384 /* Nack */
383 if (msr & MNR) { 385 if (msr & MNR) {
384 /* go to stop phase */ 386 /* go to stop phase */
@@ -388,6 +390,12 @@ static irqreturn_t rcar_i2c_irq(int irq, void *ptr)
388 goto out; 390 goto out;
389 } 391 }
390 392
393 /* Stop */
394 if (msr & MST) {
395 rcar_i2c_flags_set(priv, ID_DONE);
396 goto out;
397 }
398
391 if (rcar_i2c_is_recv(priv)) 399 if (rcar_i2c_is_recv(priv))
392 rcar_i2c_flags_set(priv, rcar_i2c_irq_recv(priv, msr)); 400 rcar_i2c_flags_set(priv, rcar_i2c_irq_recv(priv, msr));
393 else 401 else
@@ -400,6 +408,9 @@ out:
400 wake_up(&priv->wait); 408 wake_up(&priv->wait);
401 } 409 }
402 410
411 spin_unlock(&priv->lock);
412 /*-------------- spin unlock -----------------*/
413
403 return IRQ_HANDLED; 414 return IRQ_HANDLED;
404} 415}
405 416
@@ -409,14 +420,21 @@ static int rcar_i2c_master_xfer(struct i2c_adapter *adap,
409{ 420{
410 struct rcar_i2c_priv *priv = i2c_get_adapdata(adap); 421 struct rcar_i2c_priv *priv = i2c_get_adapdata(adap);
411 struct device *dev = rcar_i2c_priv_to_dev(priv); 422 struct device *dev = rcar_i2c_priv_to_dev(priv);
423 unsigned long flags;
412 int i, ret, timeout; 424 int i, ret, timeout;
413 425
414 pm_runtime_get_sync(dev); 426 pm_runtime_get_sync(dev);
415 427
428 /*-------------- spin lock -----------------*/
429 spin_lock_irqsave(&priv->lock, flags);
430
416 rcar_i2c_init(priv); 431 rcar_i2c_init(priv);
417 /* start clock */ 432 /* start clock */
418 rcar_i2c_write(priv, ICCCR, priv->icccr); 433 rcar_i2c_write(priv, ICCCR, priv->icccr);
419 434
435 spin_unlock_irqrestore(&priv->lock, flags);
436 /*-------------- spin unlock -----------------*/
437
420 ret = rcar_i2c_bus_barrier(priv); 438 ret = rcar_i2c_bus_barrier(priv);
421 if (ret < 0) 439 if (ret < 0)
422 goto out; 440 goto out;
@@ -428,6 +446,9 @@ static int rcar_i2c_master_xfer(struct i2c_adapter *adap,
428 break; 446 break;
429 } 447 }
430 448
449 /*-------------- spin lock -----------------*/
450 spin_lock_irqsave(&priv->lock, flags);
451
431 /* init each data */ 452 /* init each data */
432 priv->msg = &msgs[i]; 453 priv->msg = &msgs[i];
433 priv->pos = 0; 454 priv->pos = 0;
@@ -437,6 +458,9 @@ static int rcar_i2c_master_xfer(struct i2c_adapter *adap,
437 458
438 ret = rcar_i2c_prepare_msg(priv); 459 ret = rcar_i2c_prepare_msg(priv);
439 460
461 spin_unlock_irqrestore(&priv->lock, flags);
462 /*-------------- spin unlock -----------------*/
463
440 if (ret < 0) 464 if (ret < 0)
441 break; 465 break;
442 466
@@ -540,6 +564,7 @@ static int rcar_i2c_probe(struct platform_device *pdev)
540 564
541 irq = platform_get_irq(pdev, 0); 565 irq = platform_get_irq(pdev, 0);
542 init_waitqueue_head(&priv->wait); 566 init_waitqueue_head(&priv->wait);
567 spin_lock_init(&priv->lock);
543 568
544 adap = &priv->adap; 569 adap = &priv->adap;
545 adap->nr = pdev->id; 570 adap->nr = pdev->id;
diff --git a/drivers/i2c/busses/i2c-rk3x.c b/drivers/i2c/busses/i2c-rk3x.c
index 69e11853e8bf..93cfc837200b 100644
--- a/drivers/i2c/busses/i2c-rk3x.c
+++ b/drivers/i2c/busses/i2c-rk3x.c
@@ -323,6 +323,10 @@ static void rk3x_i2c_handle_read(struct rk3x_i2c *i2c, unsigned int ipd)
323 /* ack interrupt */ 323 /* ack interrupt */
324 i2c_writel(i2c, REG_INT_MBRF, REG_IPD); 324 i2c_writel(i2c, REG_INT_MBRF, REG_IPD);
325 325
326 /* Can only handle a maximum of 32 bytes at a time */
327 if (len > 32)
328 len = 32;
329
326 /* read the data from receive buffer */ 330 /* read the data from receive buffer */
327 for (i = 0; i < len; ++i) { 331 for (i = 0; i < len; ++i) {
328 if (i % 4 == 0) 332 if (i % 4 == 0)
@@ -429,12 +433,11 @@ static void rk3x_i2c_set_scl_rate(struct rk3x_i2c *i2c, unsigned long scl_rate)
429 unsigned long i2c_rate = clk_get_rate(i2c->clk); 433 unsigned long i2c_rate = clk_get_rate(i2c->clk);
430 unsigned int div; 434 unsigned int div;
431 435
432 /* SCL rate = (clk rate) / (8 * DIV) */ 436 /* set DIV = DIVH = DIVL
433 div = DIV_ROUND_UP(i2c_rate, scl_rate * 8); 437 * SCL rate = (clk rate) / (8 * (DIVH + 1 + DIVL + 1))
434 438 * = (clk rate) / (16 * (DIV + 1))
435 /* The lower and upper half of the CLKDIV reg describe the length of 439 */
436 * SCL low & high periods. */ 440 div = DIV_ROUND_UP(i2c_rate, scl_rate * 16) - 1;
437 div = DIV_ROUND_UP(div, 2);
438 441
439 i2c_writel(i2c, (div << 16) | (div & 0xffff), REG_CLKDIV); 442 i2c_writel(i2c, (div << 16) | (div & 0xffff), REG_CLKDIV);
440} 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 e8b61967334b..000000000000
--- a/drivers/i2c/i2c-acpi.c
+++ /dev/null
@@ -1,362 +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
129static int acpi_gsb_i2c_read_bytes(struct i2c_client *client,
130 u8 cmd, u8 *data, u8 data_len)
131{
132
133 struct i2c_msg msgs[2];
134 int ret;
135 u8 *buffer;
136
137 buffer = kzalloc(data_len, GFP_KERNEL);
138 if (!buffer)
139 return AE_NO_MEMORY;
140
141 msgs[0].addr = client->addr;
142 msgs[0].flags = client->flags;
143 msgs[0].len = 1;
144 msgs[0].buf = &cmd;
145
146 msgs[1].addr = client->addr;
147 msgs[1].flags = client->flags | I2C_M_RD;
148 msgs[1].len = data_len;
149 msgs[1].buf = buffer;
150
151 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
152 if (ret < 0)
153 dev_err(&client->adapter->dev, "i2c read failed\n");
154 else
155 memcpy(data, buffer, data_len);
156
157 kfree(buffer);
158 return ret;
159}
160
161static int acpi_gsb_i2c_write_bytes(struct i2c_client *client,
162 u8 cmd, u8 *data, u8 data_len)
163{
164
165 struct i2c_msg msgs[1];
166 u8 *buffer;
167 int ret = AE_OK;
168
169 buffer = kzalloc(data_len + 1, GFP_KERNEL);
170 if (!buffer)
171 return AE_NO_MEMORY;
172
173 buffer[0] = cmd;
174 memcpy(buffer + 1, data, data_len);
175
176 msgs[0].addr = client->addr;
177 msgs[0].flags = client->flags;
178 msgs[0].len = data_len + 1;
179 msgs[0].buf = buffer;
180
181 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
182 if (ret < 0)
183 dev_err(&client->adapter->dev, "i2c write failed\n");
184
185 kfree(buffer);
186 return ret;
187}
188
189static acpi_status
190acpi_i2c_space_handler(u32 function, acpi_physical_address command,
191 u32 bits, u64 *value64,
192 void *handler_context, void *region_context)
193{
194 struct gsb_buffer *gsb = (struct gsb_buffer *)value64;
195 struct acpi_i2c_handler_data *data = handler_context;
196 struct acpi_connection_info *info = &data->info;
197 struct acpi_resource_i2c_serialbus *sb;
198 struct i2c_adapter *adapter = data->adapter;
199 struct i2c_client client;
200 struct acpi_resource *ares;
201 u32 accessor_type = function >> 16;
202 u8 action = function & ACPI_IO_MASK;
203 acpi_status ret = AE_OK;
204 int status;
205
206 ret = acpi_buffer_to_resource(info->connection, info->length, &ares);
207 if (ACPI_FAILURE(ret))
208 return ret;
209
210 if (!value64 || ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS) {
211 ret = AE_BAD_PARAMETER;
212 goto err;
213 }
214
215 sb = &ares->data.i2c_serial_bus;
216 if (sb->type != ACPI_RESOURCE_SERIAL_TYPE_I2C) {
217 ret = AE_BAD_PARAMETER;
218 goto err;
219 }
220
221 memset(&client, 0, sizeof(client));
222 client.adapter = adapter;
223 client.addr = sb->slave_address;
224 client.flags = 0;
225
226 if (sb->access_mode == ACPI_I2C_10BIT_MODE)
227 client.flags |= I2C_CLIENT_TEN;
228
229 switch (accessor_type) {
230 case ACPI_GSB_ACCESS_ATTRIB_SEND_RCV:
231 if (action == ACPI_READ) {
232 status = i2c_smbus_read_byte(&client);
233 if (status >= 0) {
234 gsb->bdata = status;
235 status = 0;
236 }
237 } else {
238 status = i2c_smbus_write_byte(&client, gsb->bdata);
239 }
240 break;
241
242 case ACPI_GSB_ACCESS_ATTRIB_BYTE:
243 if (action == ACPI_READ) {
244 status = i2c_smbus_read_byte_data(&client, command);
245 if (status >= 0) {
246 gsb->bdata = status;
247 status = 0;
248 }
249 } else {
250 status = i2c_smbus_write_byte_data(&client, command,
251 gsb->bdata);
252 }
253 break;
254
255 case ACPI_GSB_ACCESS_ATTRIB_WORD:
256 if (action == ACPI_READ) {
257 status = i2c_smbus_read_word_data(&client, command);
258 if (status >= 0) {
259 gsb->wdata = status;
260 status = 0;
261 }
262 } else {
263 status = i2c_smbus_write_word_data(&client, command,
264 gsb->wdata);
265 }
266 break;
267
268 case ACPI_GSB_ACCESS_ATTRIB_BLOCK:
269 if (action == ACPI_READ) {
270 status = i2c_smbus_read_block_data(&client, command,
271 gsb->data);
272 if (status >= 0) {
273 gsb->len = status;
274 status = 0;
275 }
276 } else {
277 status = i2c_smbus_write_block_data(&client, command,
278 gsb->len, gsb->data);
279 }
280 break;
281
282 case ACPI_GSB_ACCESS_ATTRIB_MULTIBYTE:
283 if (action == ACPI_READ) {
284 status = acpi_gsb_i2c_read_bytes(&client, command,
285 gsb->data, info->access_length);
286 if (status > 0)
287 status = 0;
288 } else {
289 status = acpi_gsb_i2c_write_bytes(&client, command,
290 gsb->data, info->access_length);
291 }
292 break;
293
294 default:
295 pr_info("protocol(0x%02x) is not supported.\n", accessor_type);
296 ret = AE_BAD_PARAMETER;
297 goto err;
298 }
299
300 gsb->status = status;
301
302 err:
303 ACPI_FREE(ares);
304 return ret;
305}
306
307
308int acpi_i2c_install_space_handler(struct i2c_adapter *adapter)
309{
310 acpi_handle handle = ACPI_HANDLE(adapter->dev.parent);
311 struct acpi_i2c_handler_data *data;
312 acpi_status status;
313
314 if (!handle)
315 return -ENODEV;
316
317 data = kzalloc(sizeof(struct acpi_i2c_handler_data),
318 GFP_KERNEL);
319 if (!data)
320 return -ENOMEM;
321
322 data->adapter = adapter;
323 status = acpi_bus_attach_private_data(handle, (void *)data);
324 if (ACPI_FAILURE(status)) {
325 kfree(data);
326 return -ENOMEM;
327 }
328
329 status = acpi_install_address_space_handler(handle,
330 ACPI_ADR_SPACE_GSBUS,
331 &acpi_i2c_space_handler,
332 NULL,
333 data);
334 if (ACPI_FAILURE(status)) {
335 dev_err(&adapter->dev, "Error installing i2c space handler\n");
336 acpi_bus_detach_private_data(handle);
337 kfree(data);
338 return -ENOMEM;
339 }
340
341 return 0;
342}
343
344void acpi_i2c_remove_space_handler(struct i2c_adapter *adapter)
345{
346 acpi_handle handle = ACPI_HANDLE(adapter->dev.parent);
347 struct acpi_i2c_handler_data *data;
348 acpi_status status;
349
350 if (!handle)
351 return;
352
353 acpi_remove_address_space_handler(handle,
354 ACPI_ADR_SPACE_GSBUS,
355 &acpi_i2c_space_handler);
356
357 status = acpi_bus_get_private_data(handle, (void **)&data);
358 if (ACPI_SUCCESS(status))
359 kfree(data);
360
361 acpi_bus_detach_private_data(handle);
362}
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,