Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

Merge branch 'i2c/for-4.13' of git://git.kernel.org/pub/scm/linux/kernel/git/wsa/linux

Pull i2c updates from Wolfram Sang:
"This pull request contains:

- i2c core reorganization. One source file became too monolithic. It
is now split up, yet we still have the same named object as the
final output. This should ease maintenance.

- new drivers: ZTE ZX2967 family, ASPEED 24XX/25XX

- designware driver gained slave mode support

- xgene-slimpro driver gained ACPI support

- bigger overhaul for pca-platform driver

- the algo-bit module now supports messages with enforced STOP

- slightly bigger than usual set of driver updates and improvements

and with much appreciated quality assurance from Andy Shevchenko"

* 'i2c/for-4.13' of git://git.kernel.org/pub/scm/linux/kernel/git/wsa/linux: (51 commits)
i2c: Provide a stub for i2c_detect_slave_mode()
i2c: designware: Let slave adapter support be optional
i2c: designware: Make HW init functions static
i2c: designware: fix spelling mistakes
i2c: pca-platform: propagate error from i2c_pca_add_numbered_bus
i2c: pca-platform: correctly set algo_data.reset_chip
i2c: acpi: Do not create i2c-clients for LNXVIDEO ACPI devices
i2c: designware: enable SLAVE in platform module
i2c: designware: add SLAVE mode functions
i2c: zx2967: drop COMPILE_TEST dependency
i2c: zx2967: always use the same device when printing errors
i2c: pca-platform: use dev_warn/dev_info instead of printk
i2c: pca-platform: use device managed allocations
i2c: pca-platform: add devicetree awareness
i2c: pca-platform: switch to struct gpio_desc
dt-bindings: add bindings for i2c-pca-platform
i2c: cadance: fix ctrl/addr reg write order
i2c: zx2967: add i2c controller driver for ZTE's zx2967 family
dt: bindings: add documentation for zx2967 family i2c controller
i2c: algo-bit: add support for I2C_M_STOP
...

+4949 -2488
+48
Documentation/devicetree/bindings/i2c/i2c-aspeed.txt
··· 1 + Device tree configuration for the I2C busses on the AST24XX and AST25XX SoCs. 2 + 3 + Required Properties: 4 + - #address-cells : should be 1 5 + - #size-cells : should be 0 6 + - reg : address offset and range of bus 7 + - compatible : should be "aspeed,ast2400-i2c-bus" 8 + or "aspeed,ast2500-i2c-bus" 9 + - clocks : root clock of bus, should reference the APB 10 + clock 11 + - interrupts : interrupt number 12 + - interrupt-parent : interrupt controller for bus, should reference a 13 + aspeed,ast2400-i2c-ic or aspeed,ast2500-i2c-ic 14 + interrupt controller 15 + 16 + Optional Properties: 17 + - bus-frequency : frequency of the bus clock in Hz defaults to 100 kHz when not 18 + specified 19 + - multi-master : states that there is another master active on this bus. 20 + 21 + Example: 22 + 23 + i2c { 24 + compatible = "simple-bus"; 25 + #address-cells = <1>; 26 + #size-cells = <1>; 27 + ranges = <0 0x1e78a000 0x1000>; 28 + 29 + i2c_ic: interrupt-controller@0 { 30 + #interrupt-cells = <1>; 31 + compatible = "aspeed,ast2400-i2c-ic"; 32 + reg = <0x0 0x40>; 33 + interrupts = <12>; 34 + interrupt-controller; 35 + }; 36 + 37 + i2c0: i2c-bus@40 { 38 + #address-cells = <1>; 39 + #size-cells = <0>; 40 + #interrupt-cells = <1>; 41 + reg = <0x40 0x40>; 42 + compatible = "aspeed,ast2400-i2c-bus"; 43 + clocks = <&clk_apb>; 44 + bus-frequency = <100000>; 45 + interrupts = <0>; 46 + interrupt-parent = <&i2c_ic>; 47 + }; 48 + };
+15 -1
Documentation/devicetree/bindings/i2c/i2c-designware.txt
··· 20 20 - i2c-sda-falling-time-ns : should contain the SDA falling time in nanoseconds. 21 21 This value which is by default 300ns is used to compute the tHIGH period. 22 22 23 - Example : 23 + Examples : 24 24 25 25 i2c@f0000 { 26 26 #address-cells = <1>; ··· 42 42 i2c-sda-hold-time-ns = <300>; 43 43 i2c-sda-falling-time-ns = <300>; 44 44 i2c-scl-falling-time-ns = <300>; 45 + }; 46 + 47 + i2c@1120000 { 48 + #address-cells = <1>; 49 + #size-cells = <0>; 50 + reg = <0x2000 0x100>; 51 + clock-frequency = <400000>; 52 + clocks = <&i2cclk>; 53 + interrupts = <0>; 54 + 55 + eeprom@64 { 56 + compatible = "linux,slave-24c02"; 57 + reg = <0x40000064>; 58 + }; 45 59 };
+29
Documentation/devicetree/bindings/i2c/i2c-pca-platform.txt
··· 1 + * NXP PCA PCA9564/PCA9665 I2C controller 2 + 3 + The PCA9564/PCA9665 serves as an interface between most standard 4 + parallel-bus microcontrollers/microprocessors and the serial I2C-bus 5 + and allows the parallel bus system to communicate bi-directionally 6 + with the I2C-bus. 7 + 8 + Required properties : 9 + 10 + - reg : Offset and length of the register set for the device 11 + - compatible : one of "nxp,pca9564" or "nxp,pca9665" 12 + 13 + Optional properties 14 + - interrupts : the interrupt number 15 + - interrupt-parent : the phandle for the interrupt controller. 16 + If an interrupt is not specified polling will be used. 17 + - reset-gpios : gpio specifier for gpio connected to RESET_N pin. As the line 18 + is active low, it should be marked GPIO_ACTIVE_LOW. 19 + - clock-frequency : I2C bus frequency. 20 + 21 + Example: 22 + i2c0: i2c@80000 { 23 + compatible = "nxp,pca9564"; 24 + #address-cells = <1>; 25 + #size-cells = <0>; 26 + reg = <0x80000 0x4>; 27 + reset-gpios = <&gpio1 0 GPIO_ACTIVE_LOW>; 28 + clock-frequency = <100000>; 29 + };
+22
Documentation/devicetree/bindings/i2c/i2c-zx2967.txt
··· 1 + ZTE zx2967 I2C controller 2 + 3 + Required properties: 4 + - compatible: must be "zte,zx296718-i2c" 5 + - reg: physical address and length of the device registers 6 + - interrupts: a single interrupt specifier 7 + - clocks: clock for the device 8 + - #address-cells: should be <1> 9 + - #size-cells: should be <0> 10 + - clock-frequency: the desired I2C bus clock frequency. 11 + 12 + Examples: 13 + 14 + i2c@112000 { 15 + compatible = "zte,zx296718-i2c"; 16 + reg = <0x00112000 0x1000>; 17 + interrupts = <GIC_SPI 112 IRQ_TYPE_LEVEL_HIGH>; 18 + clocks = <&osc24m>; 19 + #address-cells = <1> 20 + #size-cells = <0>; 21 + clock-frequency = <1600000>; 22 + };
+4 -1
Documentation/driver-api/i2c.rst
··· 41 41 .. kernel-doc:: drivers/i2c/i2c-boardinfo.c 42 42 :functions: i2c_register_board_info 43 43 44 - .. kernel-doc:: drivers/i2c/i2c-core.c 44 + .. kernel-doc:: drivers/i2c/i2c-core-base.c 45 + :export: 46 + 47 + .. kernel-doc:: drivers/i2c/i2c-core-smbus.c 45 48 :export:
+2
Documentation/i2c/busses/i2c-i801
··· 34 34 * Intel Broxton (SOC) 35 35 * Intel Lewisburg (PCH) 36 36 * Intel Gemini Lake (SOC) 37 + * Intel Cannon Lake-H (PCH) 38 + * Intel Cannon Lake-LP (PCH) 37 39 Datasheets: Publicly available at the Intel website 38 40 39 41 On Intel Patsburg and later chipsets, both the normal host SMBus controller
+7 -7
Documentation/i2c/dev-interface
··· 191 191 4* Other ioctl() calls are converted to in-kernel function calls by 192 192 i2c-dev. Examples include I2C_FUNCS, which queries the I2C adapter 193 193 functionality using i2c.h:i2c_get_functionality(), and I2C_SMBUS, which 194 - performs an SMBus transaction using i2c-core.c:i2c_smbus_xfer(). 194 + performs an SMBus transaction using i2c-core-smbus.c:i2c_smbus_xfer(). 195 195 196 196 The i2c-dev driver is responsible for checking all the parameters that 197 197 come from user-space for validity. After this point, there is no ··· 200 200 directly. This means that I2C bus drivers don't need to implement 201 201 anything special to support access from user-space. 202 202 203 - 5* These i2c-core.c/i2c.h functions are wrappers to the actual 204 - implementation of your I2C bus driver. Each adapter must declare 205 - callback functions implementing these standard calls. 206 - i2c.h:i2c_get_functionality() calls i2c_adapter.algo->functionality(), 207 - while i2c-core.c:i2c_smbus_xfer() calls either 203 + 5* These i2c.h functions are wrappers to the actual implementation of 204 + your I2C bus driver. Each adapter must declare callback functions 205 + implementing these standard calls. i2c.h:i2c_get_functionality() calls 206 + i2c_adapter.algo->functionality(), while 207 + i2c-core-smbus.c:i2c_smbus_xfer() calls either 208 208 adapter.algo->smbus_xfer() if it is implemented, or if not, 209 - i2c-core.c:i2c_smbus_xfer_emulated() which in turn calls 209 + i2c-core-smbus.c:i2c_smbus_xfer_emulated() which in turn calls 210 210 i2c_adapter.algo->master_xfer(). 211 211 212 212 After your I2C bus driver has processed these requests, execution runs
+13
MAINTAINERS
··· 1139 1139 F: arch/arm/boot/dts/aspeed-* 1140 1140 F: drivers/*/*aspeed* 1141 1141 1142 + ARM/ASPEED I2C DRIVER 1143 + M: Brendan Higgins <brendanhiggins@google.com> 1144 + R: Benjamin Herrenschmidt <benh@kernel.crashing.org> 1145 + R: Joel Stanley <joel@jms.id.au> 1146 + L: linux-i2c@vger.kernel.org 1147 + L: openbmc@lists.ozlabs.org 1148 + S: Maintained 1149 + F: drivers/irqchip/irq-aspeed-i2c-ic.c 1150 + F: drivers/i2c/busses/i2c-aspeed.c 1151 + F: Documentation/devicetree/bindings/interrupt-controller/aspeed,ast2400-i2c-ic.txt 1152 + F: Documentation/devicetree/bindings/i2c/i2c-aspeed.txt 1153 + 1142 1154 ARM/ATMEL AT91RM9200, AT91SAM9 AND SAMA5 SOC SUPPORT 1143 1155 M: Nicolas Ferre <nicolas.ferre@microchip.com> 1144 1156 M: Alexandre Belloni <alexandre.belloni@free-electrons.com> ··· 6394 6382 L: linux-i2c@vger.kernel.org 6395 6383 L: linux-acpi@vger.kernel.org 6396 6384 S: Maintained 6385 + F: drivers/i2c/i2c-core-acpi.c 6397 6386 6398 6387 I2C-TAOS-EVM DRIVER 6399 6388 M: Jean Delvare <jdelvare@suse.com>
+6 -1
drivers/i2c/Makefile
··· 4 4 5 5 obj-$(CONFIG_I2C_BOARDINFO) += i2c-boardinfo.o 6 6 obj-$(CONFIG_I2C) += i2c-core.o 7 + i2c-core-objs := i2c-core-base.o i2c-core-smbus.o 8 + i2c-core-$(CONFIG_ACPI) += i2c-core-acpi.o 9 + i2c-core-$(CONFIG_I2C_SLAVE) += i2c-core-slave.o 10 + i2c-core-$(CONFIG_OF) += i2c-core-of.o 11 + 7 12 obj-$(CONFIG_I2C_SMBUS) += i2c-smbus.o 8 13 obj-$(CONFIG_I2C_CHARDEV) += i2c-dev.o 9 14 obj-$(CONFIG_I2C_MUX) += i2c-mux.o ··· 17 12 obj-$(CONFIG_I2C_SLAVE_EEPROM) += i2c-slave-eeprom.o 18 13 19 14 ccflags-$(CONFIG_I2C_DEBUG_CORE) := -DDEBUG 20 - CFLAGS_i2c-core.o := -Wno-deprecated-declarations 15 + CFLAGS_i2c-core-base.o := -Wno-deprecated-declarations
+10 -3
drivers/i2c/algos/i2c-algo-bit.c
··· 553 553 nak_ok = pmsg->flags & I2C_M_IGNORE_NAK; 554 554 if (!(pmsg->flags & I2C_M_NOSTART)) { 555 555 if (i) { 556 - bit_dbg(3, &i2c_adap->dev, "emitting " 557 - "repeated start condition\n"); 558 - i2c_repstart(adap); 556 + if (msgs[i - 1].flags & I2C_M_STOP) { 557 + bit_dbg(3, &i2c_adap->dev, 558 + "emitting enforced stop/start condition\n"); 559 + i2c_stop(adap); 560 + i2c_start(adap); 561 + } else { 562 + bit_dbg(3, &i2c_adap->dev, 563 + "emitting repeated start condition\n"); 564 + i2c_repstart(adap); 565 + } 559 566 } 560 567 ret = bit_doAddress(i2c_adap, pmsg); 561 568 if ((ret != 0) && !nak_ok) {
+33 -1
drivers/i2c/busses/Kconfig
··· 129 129 Broxton (SOC) 130 130 Lewisburg (PCH) 131 131 Gemini Lake (SOC) 132 + Cannon Lake-H (PCH) 133 + Cannon Lake-LP (PCH) 132 134 133 135 This driver can also be built as a module. If so, the module 134 136 will be called i2c-i801. ··· 328 326 329 327 comment "I2C system bus drivers (mostly embedded / system-on-chip)" 330 328 329 + config I2C_ASPEED 330 + tristate "Aspeed I2C Controller" 331 + depends on ARCH_ASPEED || COMPILE_TEST 332 + help 333 + If you say yes to this option, support will be included for the 334 + Aspeed I2C controller. 335 + 336 + This driver can also be built as a module. If so, the module 337 + will be called i2c-aspeed. 338 + 331 339 config I2C_AT91 332 340 tristate "Atmel AT91 I2C Two-Wire interface (TWI)" 333 341 depends on ARCH_AT91 ··· 486 474 depends on (ACPI && COMMON_CLK) || !ACPI 487 475 help 488 476 If you say yes to this option, support will be included for the 489 - Synopsys DesignWare I2C adapter. Only master mode is supported. 477 + Synopsys DesignWare I2C adapter. 490 478 491 479 This driver can also be built as a module. If so, the module 492 480 will be called i2c-designware-platform. 481 + 482 + config I2C_DESIGNWARE_SLAVE 483 + bool "Synopsys DesignWare Slave" 484 + select I2C_SLAVE 485 + depends on I2C_DESIGNWARE_PLATFORM 486 + help 487 + If you say yes to this option, support will be included for the 488 + Synopsys DesignWare I2C slave adapter. 489 + 490 + This is not a standalone module, this module compiles together with 491 + i2c-designware-core. 493 492 494 493 config I2C_DESIGNWARE_PCI 495 494 tristate "Synopsys DesignWare PCI" ··· 1280 1257 1281 1258 This driver can also be built as a module. If so, the module will be 1282 1259 called as i2c-opal. 1260 + 1261 + config I2C_ZX2967 1262 + tristate "ZTE ZX2967 I2C support" 1263 + depends on ARCH_ZX 1264 + default y 1265 + help 1266 + Selecting this option will add ZX2967 I2C driver. 1267 + This driver can also be built as a module. If so, the module will be 1268 + called i2c-zx2967. 1283 1269 1284 1270 endmenu
+6
drivers/i2c/busses/Makefile
··· 29 29 obj-$(CONFIG_I2C_POWERMAC) += i2c-powermac.o 30 30 31 31 # Embedded system I2C/SMBus host controller drivers 32 + obj-$(CONFIG_I2C_ASPEED) += i2c-aspeed.o 32 33 obj-$(CONFIG_I2C_AT91) += i2c-at91.o 33 34 obj-$(CONFIG_I2C_AU1550) += i2c-au1550.o 34 35 obj-$(CONFIG_I2C_AXXIA) += i2c-axxia.o ··· 41 40 obj-$(CONFIG_I2C_CPM) += i2c-cpm.o 42 41 obj-$(CONFIG_I2C_DAVINCI) += i2c-davinci.o 43 42 obj-$(CONFIG_I2C_DESIGNWARE_CORE) += i2c-designware-core.o 43 + i2c-designware-core-objs := i2c-designware-common.o i2c-designware-master.o 44 + ifeq ($(CONFIG_I2C_DESIGNWARE_SLAVE),y) 45 + i2c-designware-core-objs += i2c-designware-slave.o 46 + endif 44 47 obj-$(CONFIG_I2C_DESIGNWARE_PLATFORM) += i2c-designware-platform.o 45 48 i2c-designware-platform-objs := i2c-designware-platdrv.o 46 49 i2c-designware-platform-$(CONFIG_I2C_DESIGNWARE_BAYTRAIL) += i2c-designware-baytrail.o ··· 107 102 obj-$(CONFIG_I2C_XLR) += i2c-xlr.o 108 103 obj-$(CONFIG_I2C_XLP9XX) += i2c-xlp9xx.o 109 104 obj-$(CONFIG_I2C_RCAR) += i2c-rcar.o 105 + obj-$(CONFIG_I2C_ZX2967) += i2c-zx2967.o 110 106 111 107 # External I2C/SMBus adapter drivers 112 108 obj-$(CONFIG_I2C_DIOLAN_U2C) += i2c-diolan-u2c.o
+891
drivers/i2c/busses/i2c-aspeed.c
··· 1 + /* 2 + * Aspeed 24XX/25XX I2C Controller. 3 + * 4 + * Copyright (C) 2012-2017 ASPEED Technology Inc. 5 + * Copyright 2017 IBM Corporation 6 + * Copyright 2017 Google, Inc. 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 + 13 + #include <linux/clk.h> 14 + #include <linux/completion.h> 15 + #include <linux/err.h> 16 + #include <linux/errno.h> 17 + #include <linux/i2c.h> 18 + #include <linux/init.h> 19 + #include <linux/interrupt.h> 20 + #include <linux/io.h> 21 + #include <linux/irq.h> 22 + #include <linux/irqchip/chained_irq.h> 23 + #include <linux/irqdomain.h> 24 + #include <linux/kernel.h> 25 + #include <linux/module.h> 26 + #include <linux/of_address.h> 27 + #include <linux/of_irq.h> 28 + #include <linux/of_platform.h> 29 + #include <linux/platform_device.h> 30 + #include <linux/slab.h> 31 + 32 + /* I2C Register */ 33 + #define ASPEED_I2C_FUN_CTRL_REG 0x00 34 + #define ASPEED_I2C_AC_TIMING_REG1 0x04 35 + #define ASPEED_I2C_AC_TIMING_REG2 0x08 36 + #define ASPEED_I2C_INTR_CTRL_REG 0x0c 37 + #define ASPEED_I2C_INTR_STS_REG 0x10 38 + #define ASPEED_I2C_CMD_REG 0x14 39 + #define ASPEED_I2C_DEV_ADDR_REG 0x18 40 + #define ASPEED_I2C_BYTE_BUF_REG 0x20 41 + 42 + /* Global Register Definition */ 43 + /* 0x00 : I2C Interrupt Status Register */ 44 + /* 0x08 : I2C Interrupt Target Assignment */ 45 + 46 + /* Device Register Definition */ 47 + /* 0x00 : I2CD Function Control Register */ 48 + #define ASPEED_I2CD_MULTI_MASTER_DIS BIT(15) 49 + #define ASPEED_I2CD_SDA_DRIVE_1T_EN BIT(8) 50 + #define ASPEED_I2CD_M_SDA_DRIVE_1T_EN BIT(7) 51 + #define ASPEED_I2CD_M_HIGH_SPEED_EN BIT(6) 52 + #define ASPEED_I2CD_SLAVE_EN BIT(1) 53 + #define ASPEED_I2CD_MASTER_EN BIT(0) 54 + 55 + /* 0x04 : I2CD Clock and AC Timing Control Register #1 */ 56 + #define ASPEED_I2CD_TIME_SCL_HIGH_SHIFT 16 57 + #define ASPEED_I2CD_TIME_SCL_HIGH_MASK GENMASK(19, 16) 58 + #define ASPEED_I2CD_TIME_SCL_LOW_SHIFT 12 59 + #define ASPEED_I2CD_TIME_SCL_LOW_MASK GENMASK(15, 12) 60 + #define ASPEED_I2CD_TIME_BASE_DIVISOR_MASK GENMASK(3, 0) 61 + #define ASPEED_I2CD_TIME_SCL_REG_MAX GENMASK(3, 0) 62 + /* 0x08 : I2CD Clock and AC Timing Control Register #2 */ 63 + #define ASPEED_NO_TIMEOUT_CTRL 0 64 + 65 + /* 0x0c : I2CD Interrupt Control Register & 66 + * 0x10 : I2CD Interrupt Status Register 67 + * 68 + * These share bit definitions, so use the same values for the enable & 69 + * status bits. 70 + */ 71 + #define ASPEED_I2CD_INTR_SDA_DL_TIMEOUT BIT(14) 72 + #define ASPEED_I2CD_INTR_BUS_RECOVER_DONE BIT(13) 73 + #define ASPEED_I2CD_INTR_SLAVE_MATCH BIT(7) 74 + #define ASPEED_I2CD_INTR_SCL_TIMEOUT BIT(6) 75 + #define ASPEED_I2CD_INTR_ABNORMAL BIT(5) 76 + #define ASPEED_I2CD_INTR_NORMAL_STOP BIT(4) 77 + #define ASPEED_I2CD_INTR_ARBIT_LOSS BIT(3) 78 + #define ASPEED_I2CD_INTR_RX_DONE BIT(2) 79 + #define ASPEED_I2CD_INTR_TX_NAK BIT(1) 80 + #define ASPEED_I2CD_INTR_TX_ACK BIT(0) 81 + #define ASPEED_I2CD_INTR_ALL \ 82 + (ASPEED_I2CD_INTR_SDA_DL_TIMEOUT | \ 83 + ASPEED_I2CD_INTR_BUS_RECOVER_DONE | \ 84 + ASPEED_I2CD_INTR_SCL_TIMEOUT | \ 85 + ASPEED_I2CD_INTR_ABNORMAL | \ 86 + ASPEED_I2CD_INTR_NORMAL_STOP | \ 87 + ASPEED_I2CD_INTR_ARBIT_LOSS | \ 88 + ASPEED_I2CD_INTR_RX_DONE | \ 89 + ASPEED_I2CD_INTR_TX_NAK | \ 90 + ASPEED_I2CD_INTR_TX_ACK) 91 + 92 + /* 0x14 : I2CD Command/Status Register */ 93 + #define ASPEED_I2CD_SCL_LINE_STS BIT(18) 94 + #define ASPEED_I2CD_SDA_LINE_STS BIT(17) 95 + #define ASPEED_I2CD_BUS_BUSY_STS BIT(16) 96 + #define ASPEED_I2CD_BUS_RECOVER_CMD BIT(11) 97 + 98 + /* Command Bit */ 99 + #define ASPEED_I2CD_M_STOP_CMD BIT(5) 100 + #define ASPEED_I2CD_M_S_RX_CMD_LAST BIT(4) 101 + #define ASPEED_I2CD_M_RX_CMD BIT(3) 102 + #define ASPEED_I2CD_S_TX_CMD BIT(2) 103 + #define ASPEED_I2CD_M_TX_CMD BIT(1) 104 + #define ASPEED_I2CD_M_START_CMD BIT(0) 105 + 106 + /* 0x18 : I2CD Slave Device Address Register */ 107 + #define ASPEED_I2CD_DEV_ADDR_MASK GENMASK(6, 0) 108 + 109 + enum aspeed_i2c_master_state { 110 + ASPEED_I2C_MASTER_START, 111 + ASPEED_I2C_MASTER_TX_FIRST, 112 + ASPEED_I2C_MASTER_TX, 113 + ASPEED_I2C_MASTER_RX_FIRST, 114 + ASPEED_I2C_MASTER_RX, 115 + ASPEED_I2C_MASTER_STOP, 116 + ASPEED_I2C_MASTER_INACTIVE, 117 + }; 118 + 119 + enum aspeed_i2c_slave_state { 120 + ASPEED_I2C_SLAVE_START, 121 + ASPEED_I2C_SLAVE_READ_REQUESTED, 122 + ASPEED_I2C_SLAVE_READ_PROCESSED, 123 + ASPEED_I2C_SLAVE_WRITE_REQUESTED, 124 + ASPEED_I2C_SLAVE_WRITE_RECEIVED, 125 + ASPEED_I2C_SLAVE_STOP, 126 + }; 127 + 128 + struct aspeed_i2c_bus { 129 + struct i2c_adapter adap; 130 + struct device *dev; 131 + void __iomem *base; 132 + /* Synchronizes I/O mem access to base. */ 133 + spinlock_t lock; 134 + struct completion cmd_complete; 135 + unsigned long parent_clk_frequency; 136 + u32 bus_frequency; 137 + /* Transaction state. */ 138 + enum aspeed_i2c_master_state master_state; 139 + struct i2c_msg *msgs; 140 + size_t buf_index; 141 + size_t msgs_index; 142 + size_t msgs_count; 143 + bool send_stop; 144 + int cmd_err; 145 + /* Protected only by i2c_lock_bus */ 146 + int master_xfer_result; 147 + #if IS_ENABLED(CONFIG_I2C_SLAVE) 148 + struct i2c_client *slave; 149 + enum aspeed_i2c_slave_state slave_state; 150 + #endif /* CONFIG_I2C_SLAVE */ 151 + }; 152 + 153 + static int aspeed_i2c_reset(struct aspeed_i2c_bus *bus); 154 + 155 + static int aspeed_i2c_recover_bus(struct aspeed_i2c_bus *bus) 156 + { 157 + unsigned long time_left, flags; 158 + int ret = 0; 159 + u32 command; 160 + 161 + spin_lock_irqsave(&bus->lock, flags); 162 + command = readl(bus->base + ASPEED_I2C_CMD_REG); 163 + 164 + if (command & ASPEED_I2CD_SDA_LINE_STS) { 165 + /* Bus is idle: no recovery needed. */ 166 + if (command & ASPEED_I2CD_SCL_LINE_STS) 167 + goto out; 168 + dev_dbg(bus->dev, "SCL hung (state %x), attempting recovery\n", 169 + command); 170 + 171 + reinit_completion(&bus->cmd_complete); 172 + writel(ASPEED_I2CD_M_STOP_CMD, bus->base + ASPEED_I2C_CMD_REG); 173 + spin_unlock_irqrestore(&bus->lock, flags); 174 + 175 + time_left = wait_for_completion_timeout( 176 + &bus->cmd_complete, bus->adap.timeout); 177 + 178 + spin_lock_irqsave(&bus->lock, flags); 179 + if (time_left == 0) 180 + goto reset_out; 181 + else if (bus->cmd_err) 182 + goto reset_out; 183 + /* Recovery failed. */ 184 + else if (!(readl(bus->base + ASPEED_I2C_CMD_REG) & 185 + ASPEED_I2CD_SCL_LINE_STS)) 186 + goto reset_out; 187 + /* Bus error. */ 188 + } else { 189 + dev_dbg(bus->dev, "SDA hung (state %x), attempting recovery\n", 190 + command); 191 + 192 + reinit_completion(&bus->cmd_complete); 193 + /* Writes 1 to 8 SCL clock cycles until SDA is released. */ 194 + writel(ASPEED_I2CD_BUS_RECOVER_CMD, 195 + bus->base + ASPEED_I2C_CMD_REG); 196 + spin_unlock_irqrestore(&bus->lock, flags); 197 + 198 + time_left = wait_for_completion_timeout( 199 + &bus->cmd_complete, bus->adap.timeout); 200 + 201 + spin_lock_irqsave(&bus->lock, flags); 202 + if (time_left == 0) 203 + goto reset_out; 204 + else if (bus->cmd_err) 205 + goto reset_out; 206 + /* Recovery failed. */ 207 + else if (!(readl(bus->base + ASPEED_I2C_CMD_REG) & 208 + ASPEED_I2CD_SDA_LINE_STS)) 209 + goto reset_out; 210 + } 211 + 212 + out: 213 + spin_unlock_irqrestore(&bus->lock, flags); 214 + 215 + return ret; 216 + 217 + reset_out: 218 + spin_unlock_irqrestore(&bus->lock, flags); 219 + 220 + return aspeed_i2c_reset(bus); 221 + } 222 + 223 + #if IS_ENABLED(CONFIG_I2C_SLAVE) 224 + static bool aspeed_i2c_slave_irq(struct aspeed_i2c_bus *bus) 225 + { 226 + u32 command, irq_status, status_ack = 0; 227 + struct i2c_client *slave = bus->slave; 228 + bool irq_handled = true; 229 + u8 value; 230 + 231 + spin_lock(&bus->lock); 232 + if (!slave) { 233 + irq_handled = false; 234 + goto out; 235 + } 236 + 237 + command = readl(bus->base + ASPEED_I2C_CMD_REG); 238 + irq_status = readl(bus->base + ASPEED_I2C_INTR_STS_REG); 239 + 240 + /* Slave was requested, restart state machine. */ 241 + if (irq_status & ASPEED_I2CD_INTR_SLAVE_MATCH) { 242 + status_ack |= ASPEED_I2CD_INTR_SLAVE_MATCH; 243 + bus->slave_state = ASPEED_I2C_SLAVE_START; 244 + } 245 + 246 + /* Slave is not currently active, irq was for someone else. */ 247 + if (bus->slave_state == ASPEED_I2C_SLAVE_STOP) { 248 + irq_handled = false; 249 + goto out; 250 + } 251 + 252 + dev_dbg(bus->dev, "slave irq status 0x%08x, cmd 0x%08x\n", 253 + irq_status, command); 254 + 255 + /* Slave was sent something. */ 256 + if (irq_status & ASPEED_I2CD_INTR_RX_DONE) { 257 + value = readl(bus->base + ASPEED_I2C_BYTE_BUF_REG) >> 8; 258 + /* Handle address frame. */ 259 + if (bus->slave_state == ASPEED_I2C_SLAVE_START) { 260 + if (value & 0x1) 261 + bus->slave_state = 262 + ASPEED_I2C_SLAVE_READ_REQUESTED; 263 + else 264 + bus->slave_state = 265 + ASPEED_I2C_SLAVE_WRITE_REQUESTED; 266 + } 267 + status_ack |= ASPEED_I2CD_INTR_RX_DONE; 268 + } 269 + 270 + /* Slave was asked to stop. */ 271 + if (irq_status & ASPEED_I2CD_INTR_NORMAL_STOP) { 272 + status_ack |= ASPEED_I2CD_INTR_NORMAL_STOP; 273 + bus->slave_state = ASPEED_I2C_SLAVE_STOP; 274 + } 275 + if (irq_status & ASPEED_I2CD_INTR_TX_NAK) { 276 + status_ack |= ASPEED_I2CD_INTR_TX_NAK; 277 + bus->slave_state = ASPEED_I2C_SLAVE_STOP; 278 + } 279 + 280 + switch (bus->slave_state) { 281 + case ASPEED_I2C_SLAVE_READ_REQUESTED: 282 + if (irq_status & ASPEED_I2CD_INTR_TX_ACK) 283 + dev_err(bus->dev, "Unexpected ACK on read request.\n"); 284 + bus->slave_state = ASPEED_I2C_SLAVE_READ_PROCESSED; 285 + 286 + i2c_slave_event(slave, I2C_SLAVE_READ_REQUESTED, &value); 287 + writel(value, bus->base + ASPEED_I2C_BYTE_BUF_REG); 288 + writel(ASPEED_I2CD_S_TX_CMD, bus->base + ASPEED_I2C_CMD_REG); 289 + break; 290 + case ASPEED_I2C_SLAVE_READ_PROCESSED: 291 + status_ack |= ASPEED_I2CD_INTR_TX_ACK; 292 + if (!(irq_status & ASPEED_I2CD_INTR_TX_ACK)) 293 + dev_err(bus->dev, 294 + "Expected ACK after processed read.\n"); 295 + i2c_slave_event(slave, I2C_SLAVE_READ_PROCESSED, &value); 296 + writel(value, bus->base + ASPEED_I2C_BYTE_BUF_REG); 297 + writel(ASPEED_I2CD_S_TX_CMD, bus->base + ASPEED_I2C_CMD_REG); 298 + break; 299 + case ASPEED_I2C_SLAVE_WRITE_REQUESTED: 300 + bus->slave_state = ASPEED_I2C_SLAVE_WRITE_RECEIVED; 301 + i2c_slave_event(slave, I2C_SLAVE_WRITE_REQUESTED, &value); 302 + break; 303 + case ASPEED_I2C_SLAVE_WRITE_RECEIVED: 304 + i2c_slave_event(slave, I2C_SLAVE_WRITE_RECEIVED, &value); 305 + break; 306 + case ASPEED_I2C_SLAVE_STOP: 307 + i2c_slave_event(slave, I2C_SLAVE_STOP, &value); 308 + break; 309 + default: 310 + dev_err(bus->dev, "unhandled slave_state: %d\n", 311 + bus->slave_state); 312 + break; 313 + } 314 + 315 + if (status_ack != irq_status) 316 + dev_err(bus->dev, 317 + "irq handled != irq. expected %x, but was %x\n", 318 + irq_status, status_ack); 319 + writel(status_ack, bus->base + ASPEED_I2C_INTR_STS_REG); 320 + 321 + out: 322 + spin_unlock(&bus->lock); 323 + return irq_handled; 324 + } 325 + #endif /* CONFIG_I2C_SLAVE */ 326 + 327 + /* precondition: bus.lock has been acquired. */ 328 + static void aspeed_i2c_do_start(struct aspeed_i2c_bus *bus) 329 + { 330 + u32 command = ASPEED_I2CD_M_START_CMD | ASPEED_I2CD_M_TX_CMD; 331 + struct i2c_msg *msg = &bus->msgs[bus->msgs_index]; 332 + u8 slave_addr = msg->addr << 1; 333 + 334 + bus->master_state = ASPEED_I2C_MASTER_START; 335 + bus->buf_index = 0; 336 + 337 + if (msg->flags & I2C_M_RD) { 338 + slave_addr |= 1; 339 + command |= ASPEED_I2CD_M_RX_CMD; 340 + /* Need to let the hardware know to NACK after RX. */ 341 + if (msg->len == 1 && !(msg->flags & I2C_M_RECV_LEN)) 342 + command |= ASPEED_I2CD_M_S_RX_CMD_LAST; 343 + } 344 + 345 + writel(slave_addr, bus->base + ASPEED_I2C_BYTE_BUF_REG); 346 + writel(command, bus->base + ASPEED_I2C_CMD_REG); 347 + } 348 + 349 + /* precondition: bus.lock has been acquired. */ 350 + static void aspeed_i2c_do_stop(struct aspeed_i2c_bus *bus) 351 + { 352 + bus->master_state = ASPEED_I2C_MASTER_STOP; 353 + writel(ASPEED_I2CD_M_STOP_CMD, bus->base + ASPEED_I2C_CMD_REG); 354 + } 355 + 356 + /* precondition: bus.lock has been acquired. */ 357 + static void aspeed_i2c_next_msg_or_stop(struct aspeed_i2c_bus *bus) 358 + { 359 + if (bus->msgs_index + 1 < bus->msgs_count) { 360 + bus->msgs_index++; 361 + aspeed_i2c_do_start(bus); 362 + } else { 363 + aspeed_i2c_do_stop(bus); 364 + } 365 + } 366 + 367 + static int aspeed_i2c_is_irq_error(u32 irq_status) 368 + { 369 + if (irq_status & ASPEED_I2CD_INTR_ARBIT_LOSS) 370 + return -EAGAIN; 371 + if (irq_status & (ASPEED_I2CD_INTR_SDA_DL_TIMEOUT | 372 + ASPEED_I2CD_INTR_SCL_TIMEOUT)) 373 + return -EBUSY; 374 + if (irq_status & (ASPEED_I2CD_INTR_ABNORMAL)) 375 + return -EPROTO; 376 + 377 + return 0; 378 + } 379 + 380 + static bool aspeed_i2c_master_irq(struct aspeed_i2c_bus *bus) 381 + { 382 + u32 irq_status, status_ack = 0, command = 0; 383 + struct i2c_msg *msg; 384 + u8 recv_byte; 385 + int ret; 386 + 387 + spin_lock(&bus->lock); 388 + irq_status = readl(bus->base + ASPEED_I2C_INTR_STS_REG); 389 + /* Ack all interrupt bits. */ 390 + writel(irq_status, bus->base + ASPEED_I2C_INTR_STS_REG); 391 + 392 + if (irq_status & ASPEED_I2CD_INTR_BUS_RECOVER_DONE) { 393 + bus->master_state = ASPEED_I2C_MASTER_INACTIVE; 394 + status_ack |= ASPEED_I2CD_INTR_BUS_RECOVER_DONE; 395 + goto out_complete; 396 + } 397 + 398 + /* 399 + * We encountered an interrupt that reports an error: the hardware 400 + * should clear the command queue effectively taking us back to the 401 + * INACTIVE state. 402 + */ 403 + ret = aspeed_i2c_is_irq_error(irq_status); 404 + if (ret < 0) { 405 + dev_dbg(bus->dev, "received error interrupt: 0x%08x", 406 + irq_status); 407 + bus->cmd_err = ret; 408 + bus->master_state = ASPEED_I2C_MASTER_INACTIVE; 409 + goto out_complete; 410 + } 411 + 412 + /* We are in an invalid state; reset bus to a known state. */ 413 + if (!bus->msgs && bus->master_state != ASPEED_I2C_MASTER_STOP) { 414 + dev_err(bus->dev, "bus in unknown state"); 415 + bus->cmd_err = -EIO; 416 + aspeed_i2c_do_stop(bus); 417 + goto out_no_complete; 418 + } 419 + msg = &bus->msgs[bus->msgs_index]; 420 + 421 + /* 422 + * START is a special case because we still have to handle a subsequent 423 + * TX or RX immediately after we handle it, so we handle it here and 424 + * then update the state and handle the new state below. 425 + */ 426 + if (bus->master_state == ASPEED_I2C_MASTER_START) { 427 + if (unlikely(!(irq_status & ASPEED_I2CD_INTR_TX_ACK))) { 428 + pr_devel("no slave present at %02x", msg->addr); 429 + status_ack |= ASPEED_I2CD_INTR_TX_NAK; 430 + bus->cmd_err = -ENXIO; 431 + aspeed_i2c_do_stop(bus); 432 + goto out_no_complete; 433 + } 434 + status_ack |= ASPEED_I2CD_INTR_TX_ACK; 435 + if (msg->len == 0) { /* SMBUS_QUICK */ 436 + aspeed_i2c_do_stop(bus); 437 + goto out_no_complete; 438 + } 439 + if (msg->flags & I2C_M_RD) 440 + bus->master_state = ASPEED_I2C_MASTER_RX_FIRST; 441 + else 442 + bus->master_state = ASPEED_I2C_MASTER_TX_FIRST; 443 + } 444 + 445 + switch (bus->master_state) { 446 + case ASPEED_I2C_MASTER_TX: 447 + if (unlikely(irq_status & ASPEED_I2CD_INTR_TX_NAK)) { 448 + dev_dbg(bus->dev, "slave NACKed TX"); 449 + status_ack |= ASPEED_I2CD_INTR_TX_NAK; 450 + goto error_and_stop; 451 + } else if (unlikely(!(irq_status & ASPEED_I2CD_INTR_TX_ACK))) { 452 + dev_err(bus->dev, "slave failed to ACK TX"); 453 + goto error_and_stop; 454 + } 455 + status_ack |= ASPEED_I2CD_INTR_TX_ACK; 456 + /* fallthrough intended */ 457 + case ASPEED_I2C_MASTER_TX_FIRST: 458 + if (bus->buf_index < msg->len) { 459 + bus->master_state = ASPEED_I2C_MASTER_TX; 460 + writel(msg->buf[bus->buf_index++], 461 + bus->base + ASPEED_I2C_BYTE_BUF_REG); 462 + writel(ASPEED_I2CD_M_TX_CMD, 463 + bus->base + ASPEED_I2C_CMD_REG); 464 + } else { 465 + aspeed_i2c_next_msg_or_stop(bus); 466 + } 467 + goto out_no_complete; 468 + case ASPEED_I2C_MASTER_RX_FIRST: 469 + /* RX may not have completed yet (only address cycle) */ 470 + if (!(irq_status & ASPEED_I2CD_INTR_RX_DONE)) 471 + goto out_no_complete; 472 + /* fallthrough intended */ 473 + case ASPEED_I2C_MASTER_RX: 474 + if (unlikely(!(irq_status & ASPEED_I2CD_INTR_RX_DONE))) { 475 + dev_err(bus->dev, "master failed to RX"); 476 + goto error_and_stop; 477 + } 478 + status_ack |= ASPEED_I2CD_INTR_RX_DONE; 479 + 480 + recv_byte = readl(bus->base + ASPEED_I2C_BYTE_BUF_REG) >> 8; 481 + msg->buf[bus->buf_index++] = recv_byte; 482 + 483 + if (msg->flags & I2C_M_RECV_LEN) { 484 + if (unlikely(recv_byte > I2C_SMBUS_BLOCK_MAX)) { 485 + bus->cmd_err = -EPROTO; 486 + aspeed_i2c_do_stop(bus); 487 + goto out_no_complete; 488 + } 489 + msg->len = recv_byte + 490 + ((msg->flags & I2C_CLIENT_PEC) ? 2 : 1); 491 + msg->flags &= ~I2C_M_RECV_LEN; 492 + } 493 + 494 + if (bus->buf_index < msg->len) { 495 + bus->master_state = ASPEED_I2C_MASTER_RX; 496 + command = ASPEED_I2CD_M_RX_CMD; 497 + if (bus->buf_index + 1 == msg->len) 498 + command |= ASPEED_I2CD_M_S_RX_CMD_LAST; 499 + writel(command, bus->base + ASPEED_I2C_CMD_REG); 500 + } else { 501 + aspeed_i2c_next_msg_or_stop(bus); 502 + } 503 + goto out_no_complete; 504 + case ASPEED_I2C_MASTER_STOP: 505 + if (unlikely(!(irq_status & ASPEED_I2CD_INTR_NORMAL_STOP))) { 506 + dev_err(bus->dev, "master failed to STOP"); 507 + bus->cmd_err = -EIO; 508 + /* Do not STOP as we have already tried. */ 509 + } else { 510 + status_ack |= ASPEED_I2CD_INTR_NORMAL_STOP; 511 + } 512 + 513 + bus->master_state = ASPEED_I2C_MASTER_INACTIVE; 514 + goto out_complete; 515 + case ASPEED_I2C_MASTER_INACTIVE: 516 + dev_err(bus->dev, 517 + "master received interrupt 0x%08x, but is inactive", 518 + irq_status); 519 + bus->cmd_err = -EIO; 520 + /* Do not STOP as we should be inactive. */ 521 + goto out_complete; 522 + default: 523 + WARN(1, "unknown master state\n"); 524 + bus->master_state = ASPEED_I2C_MASTER_INACTIVE; 525 + bus->cmd_err = -EINVAL; 526 + goto out_complete; 527 + } 528 + error_and_stop: 529 + bus->cmd_err = -EIO; 530 + aspeed_i2c_do_stop(bus); 531 + goto out_no_complete; 532 + out_complete: 533 + bus->msgs = NULL; 534 + if (bus->cmd_err) 535 + bus->master_xfer_result = bus->cmd_err; 536 + else 537 + bus->master_xfer_result = bus->msgs_index + 1; 538 + complete(&bus->cmd_complete); 539 + out_no_complete: 540 + if (irq_status != status_ack) 541 + dev_err(bus->dev, 542 + "irq handled != irq. expected 0x%08x, but was 0x%08x\n", 543 + irq_status, status_ack); 544 + spin_unlock(&bus->lock); 545 + return !!irq_status; 546 + } 547 + 548 + static irqreturn_t aspeed_i2c_bus_irq(int irq, void *dev_id) 549 + { 550 + struct aspeed_i2c_bus *bus = dev_id; 551 + 552 + #if IS_ENABLED(CONFIG_I2C_SLAVE) 553 + if (aspeed_i2c_slave_irq(bus)) { 554 + dev_dbg(bus->dev, "irq handled by slave.\n"); 555 + return IRQ_HANDLED; 556 + } 557 + #endif /* CONFIG_I2C_SLAVE */ 558 + 559 + return aspeed_i2c_master_irq(bus) ? IRQ_HANDLED : IRQ_NONE; 560 + } 561 + 562 + static int aspeed_i2c_master_xfer(struct i2c_adapter *adap, 563 + struct i2c_msg *msgs, int num) 564 + { 565 + struct aspeed_i2c_bus *bus = i2c_get_adapdata(adap); 566 + unsigned long time_left, flags; 567 + int ret = 0; 568 + 569 + spin_lock_irqsave(&bus->lock, flags); 570 + bus->cmd_err = 0; 571 + 572 + /* If bus is busy, attempt recovery. We assume a single master 573 + * environment. 574 + */ 575 + if (readl(bus->base + ASPEED_I2C_CMD_REG) & ASPEED_I2CD_BUS_BUSY_STS) { 576 + spin_unlock_irqrestore(&bus->lock, flags); 577 + ret = aspeed_i2c_recover_bus(bus); 578 + if (ret) 579 + return ret; 580 + spin_lock_irqsave(&bus->lock, flags); 581 + } 582 + 583 + bus->cmd_err = 0; 584 + bus->msgs = msgs; 585 + bus->msgs_index = 0; 586 + bus->msgs_count = num; 587 + 588 + reinit_completion(&bus->cmd_complete); 589 + aspeed_i2c_do_start(bus); 590 + spin_unlock_irqrestore(&bus->lock, flags); 591 + 592 + time_left = wait_for_completion_timeout(&bus->cmd_complete, 593 + bus->adap.timeout); 594 + 595 + if (time_left == 0) 596 + return -ETIMEDOUT; 597 + else 598 + return bus->master_xfer_result; 599 + } 600 + 601 + static u32 aspeed_i2c_functionality(struct i2c_adapter *adap) 602 + { 603 + return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL | I2C_FUNC_SMBUS_BLOCK_DATA; 604 + } 605 + 606 + #if IS_ENABLED(CONFIG_I2C_SLAVE) 607 + /* precondition: bus.lock has been acquired. */ 608 + static void __aspeed_i2c_reg_slave(struct aspeed_i2c_bus *bus, u16 slave_addr) 609 + { 610 + u32 addr_reg_val, func_ctrl_reg_val; 611 + 612 + /* Set slave addr. */ 613 + addr_reg_val = readl(bus->base + ASPEED_I2C_DEV_ADDR_REG); 614 + addr_reg_val &= ~ASPEED_I2CD_DEV_ADDR_MASK; 615 + addr_reg_val |= slave_addr & ASPEED_I2CD_DEV_ADDR_MASK; 616 + writel(addr_reg_val, bus->base + ASPEED_I2C_DEV_ADDR_REG); 617 + 618 + /* Turn on slave mode. */ 619 + func_ctrl_reg_val = readl(bus->base + ASPEED_I2C_FUN_CTRL_REG); 620 + func_ctrl_reg_val |= ASPEED_I2CD_SLAVE_EN; 621 + writel(func_ctrl_reg_val, bus->base + ASPEED_I2C_FUN_CTRL_REG); 622 + } 623 + 624 + static int aspeed_i2c_reg_slave(struct i2c_client *client) 625 + { 626 + struct aspeed_i2c_bus *bus = i2c_get_adapdata(client->adapter); 627 + unsigned long flags; 628 + 629 + spin_lock_irqsave(&bus->lock, flags); 630 + if (bus->slave) { 631 + spin_unlock_irqrestore(&bus->lock, flags); 632 + return -EINVAL; 633 + } 634 + 635 + __aspeed_i2c_reg_slave(bus, client->addr); 636 + 637 + bus->slave = client; 638 + bus->slave_state = ASPEED_I2C_SLAVE_STOP; 639 + spin_unlock_irqrestore(&bus->lock, flags); 640 + 641 + return 0; 642 + } 643 + 644 + static int aspeed_i2c_unreg_slave(struct i2c_client *client) 645 + { 646 + struct aspeed_i2c_bus *bus = i2c_get_adapdata(client->adapter); 647 + u32 func_ctrl_reg_val; 648 + unsigned long flags; 649 + 650 + spin_lock_irqsave(&bus->lock, flags); 651 + if (!bus->slave) { 652 + spin_unlock_irqrestore(&bus->lock, flags); 653 + return -EINVAL; 654 + } 655 + 656 + /* Turn off slave mode. */ 657 + func_ctrl_reg_val = readl(bus->base + ASPEED_I2C_FUN_CTRL_REG); 658 + func_ctrl_reg_val &= ~ASPEED_I2CD_SLAVE_EN; 659 + writel(func_ctrl_reg_val, bus->base + ASPEED_I2C_FUN_CTRL_REG); 660 + 661 + bus->slave = NULL; 662 + spin_unlock_irqrestore(&bus->lock, flags); 663 + 664 + return 0; 665 + } 666 + #endif /* CONFIG_I2C_SLAVE */ 667 + 668 + static const struct i2c_algorithm aspeed_i2c_algo = { 669 + .master_xfer = aspeed_i2c_master_xfer, 670 + .functionality = aspeed_i2c_functionality, 671 + #if IS_ENABLED(CONFIG_I2C_SLAVE) 672 + .reg_slave = aspeed_i2c_reg_slave, 673 + .unreg_slave = aspeed_i2c_unreg_slave, 674 + #endif /* CONFIG_I2C_SLAVE */ 675 + }; 676 + 677 + static u32 aspeed_i2c_get_clk_reg_val(u32 divisor) 678 + { 679 + u32 base_clk, clk_high, clk_low, tmp; 680 + 681 + /* 682 + * The actual clock frequency of SCL is: 683 + * SCL_freq = APB_freq / (base_freq * (SCL_high + SCL_low)) 684 + * = APB_freq / divisor 685 + * where base_freq is a programmable clock divider; its value is 686 + * base_freq = 1 << base_clk 687 + * SCL_high is the number of base_freq clock cycles that SCL stays high 688 + * and SCL_low is the number of base_freq clock cycles that SCL stays 689 + * low for a period of SCL. 690 + * The actual register has a minimum SCL_high and SCL_low minimum of 1; 691 + * thus, they start counting at zero. So 692 + * SCL_high = clk_high + 1 693 + * SCL_low = clk_low + 1 694 + * Thus, 695 + * SCL_freq = APB_freq / 696 + * ((1 << base_clk) * (clk_high + 1 + clk_low + 1)) 697 + * The documentation recommends clk_high >= 8 and clk_low >= 7 when 698 + * possible; this last constraint gives us the following solution: 699 + */ 700 + base_clk = divisor > 33 ? ilog2((divisor - 1) / 32) + 1 : 0; 701 + tmp = divisor / (1 << base_clk); 702 + clk_high = tmp / 2 + tmp % 2; 703 + clk_low = tmp - clk_high; 704 + 705 + clk_high -= 1; 706 + clk_low -= 1; 707 + 708 + return ((clk_high << ASPEED_I2CD_TIME_SCL_HIGH_SHIFT) 709 + & ASPEED_I2CD_TIME_SCL_HIGH_MASK) 710 + | ((clk_low << ASPEED_I2CD_TIME_SCL_LOW_SHIFT) 711 + & ASPEED_I2CD_TIME_SCL_LOW_MASK) 712 + | (base_clk & ASPEED_I2CD_TIME_BASE_DIVISOR_MASK); 713 + } 714 + 715 + /* precondition: bus.lock has been acquired. */ 716 + static int aspeed_i2c_init_clk(struct aspeed_i2c_bus *bus) 717 + { 718 + u32 divisor, clk_reg_val; 719 + 720 + divisor = bus->parent_clk_frequency / bus->bus_frequency; 721 + clk_reg_val = aspeed_i2c_get_clk_reg_val(divisor); 722 + writel(clk_reg_val, bus->base + ASPEED_I2C_AC_TIMING_REG1); 723 + writel(ASPEED_NO_TIMEOUT_CTRL, bus->base + ASPEED_I2C_AC_TIMING_REG2); 724 + 725 + return 0; 726 + } 727 + 728 + /* precondition: bus.lock has been acquired. */ 729 + static int aspeed_i2c_init(struct aspeed_i2c_bus *bus, 730 + struct platform_device *pdev) 731 + { 732 + u32 fun_ctrl_reg = ASPEED_I2CD_MASTER_EN; 733 + int ret; 734 + 735 + /* Disable everything. */ 736 + writel(0, bus->base + ASPEED_I2C_FUN_CTRL_REG); 737 + 738 + ret = aspeed_i2c_init_clk(bus); 739 + if (ret < 0) 740 + return ret; 741 + 742 + if (!of_property_read_bool(pdev->dev.of_node, "multi-master")) 743 + fun_ctrl_reg |= ASPEED_I2CD_MULTI_MASTER_DIS; 744 + 745 + /* Enable Master Mode */ 746 + writel(readl(bus->base + ASPEED_I2C_FUN_CTRL_REG) | fun_ctrl_reg, 747 + bus->base + ASPEED_I2C_FUN_CTRL_REG); 748 + 749 + #if IS_ENABLED(CONFIG_I2C_SLAVE) 750 + /* If slave has already been registered, re-enable it. */ 751 + if (bus->slave) 752 + __aspeed_i2c_reg_slave(bus, bus->slave->addr); 753 + #endif /* CONFIG_I2C_SLAVE */ 754 + 755 + /* Set interrupt generation of I2C controller */ 756 + writel(ASPEED_I2CD_INTR_ALL, bus->base + ASPEED_I2C_INTR_CTRL_REG); 757 + 758 + return 0; 759 + } 760 + 761 + static int aspeed_i2c_reset(struct aspeed_i2c_bus *bus) 762 + { 763 + struct platform_device *pdev = to_platform_device(bus->dev); 764 + unsigned long flags; 765 + int ret; 766 + 767 + spin_lock_irqsave(&bus->lock, flags); 768 + 769 + /* Disable and ack all interrupts. */ 770 + writel(0, bus->base + ASPEED_I2C_INTR_CTRL_REG); 771 + writel(0xffffffff, bus->base + ASPEED_I2C_INTR_STS_REG); 772 + 773 + ret = aspeed_i2c_init(bus, pdev); 774 + 775 + spin_unlock_irqrestore(&bus->lock, flags); 776 + 777 + return ret; 778 + } 779 + 780 + static int aspeed_i2c_probe_bus(struct platform_device *pdev) 781 + { 782 + struct aspeed_i2c_bus *bus; 783 + struct clk *parent_clk; 784 + struct resource *res; 785 + int irq, ret; 786 + 787 + bus = devm_kzalloc(&pdev->dev, sizeof(*bus), GFP_KERNEL); 788 + if (!bus) 789 + return -ENOMEM; 790 + 791 + res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 792 + bus->base = devm_ioremap_resource(&pdev->dev, res); 793 + if (IS_ERR(bus->base)) 794 + return PTR_ERR(bus->base); 795 + 796 + parent_clk = devm_clk_get(&pdev->dev, NULL); 797 + if (IS_ERR(parent_clk)) 798 + return PTR_ERR(parent_clk); 799 + bus->parent_clk_frequency = clk_get_rate(parent_clk); 800 + /* We just need the clock rate, we don't actually use the clk object. */ 801 + devm_clk_put(&pdev->dev, parent_clk); 802 + 803 + ret = of_property_read_u32(pdev->dev.of_node, 804 + "bus-frequency", &bus->bus_frequency); 805 + if (ret < 0) { 806 + dev_err(&pdev->dev, 807 + "Could not read bus-frequency property\n"); 808 + bus->bus_frequency = 100000; 809 + } 810 + 811 + /* Initialize the I2C adapter */ 812 + spin_lock_init(&bus->lock); 813 + init_completion(&bus->cmd_complete); 814 + bus->adap.owner = THIS_MODULE; 815 + bus->adap.retries = 0; 816 + bus->adap.timeout = 5 * HZ; 817 + bus->adap.algo = &aspeed_i2c_algo; 818 + bus->adap.dev.parent = &pdev->dev; 819 + bus->adap.dev.of_node = pdev->dev.of_node; 820 + strlcpy(bus->adap.name, pdev->name, sizeof(bus->adap.name)); 821 + i2c_set_adapdata(&bus->adap, bus); 822 + 823 + bus->dev = &pdev->dev; 824 + 825 + /* Clean up any left over interrupt state. */ 826 + writel(0, bus->base + ASPEED_I2C_INTR_CTRL_REG); 827 + writel(0xffffffff, bus->base + ASPEED_I2C_INTR_STS_REG); 828 + /* 829 + * bus.lock does not need to be held because the interrupt handler has 830 + * not been enabled yet. 831 + */ 832 + ret = aspeed_i2c_init(bus, pdev); 833 + if (ret < 0) 834 + return ret; 835 + 836 + irq = irq_of_parse_and_map(pdev->dev.of_node, 0); 837 + ret = devm_request_irq(&pdev->dev, irq, aspeed_i2c_bus_irq, 838 + 0, dev_name(&pdev->dev), bus); 839 + if (ret < 0) 840 + return ret; 841 + 842 + ret = i2c_add_adapter(&bus->adap); 843 + if (ret < 0) 844 + return ret; 845 + 846 + platform_set_drvdata(pdev, bus); 847 + 848 + dev_info(bus->dev, "i2c bus %d registered, irq %d\n", 849 + bus->adap.nr, irq); 850 + 851 + return 0; 852 + } 853 + 854 + static int aspeed_i2c_remove_bus(struct platform_device *pdev) 855 + { 856 + struct aspeed_i2c_bus *bus = platform_get_drvdata(pdev); 857 + unsigned long flags; 858 + 859 + spin_lock_irqsave(&bus->lock, flags); 860 + 861 + /* Disable everything. */ 862 + writel(0, bus->base + ASPEED_I2C_FUN_CTRL_REG); 863 + writel(0, bus->base + ASPEED_I2C_INTR_CTRL_REG); 864 + 865 + spin_unlock_irqrestore(&bus->lock, flags); 866 + 867 + i2c_del_adapter(&bus->adap); 868 + 869 + return 0; 870 + } 871 + 872 + static const struct of_device_id aspeed_i2c_bus_of_table[] = { 873 + { .compatible = "aspeed,ast2400-i2c-bus", }, 874 + { .compatible = "aspeed,ast2500-i2c-bus", }, 875 + { }, 876 + }; 877 + MODULE_DEVICE_TABLE(of, aspeed_i2c_bus_of_table); 878 + 879 + static struct platform_driver aspeed_i2c_bus_driver = { 880 + .probe = aspeed_i2c_probe_bus, 881 + .remove = aspeed_i2c_remove_bus, 882 + .driver = { 883 + .name = "aspeed-i2c-bus", 884 + .of_match_table = aspeed_i2c_bus_of_table, 885 + }, 886 + }; 887 + module_platform_driver(aspeed_i2c_bus_driver); 888 + 889 + MODULE_AUTHOR("Brendan Higgins <brendanhiggins@google.com>"); 890 + MODULE_DESCRIPTION("Aspeed I2C Bus Driver"); 891 + MODULE_LICENSE("GPL v2");
+10 -6
drivers/i2c/busses/i2c-at91.c
··· 274 274 if (!dev->use_alt_cmd) 275 275 at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_STOP); 276 276 277 - dev_dbg(dev->dev, "wrote 0x%x, to go %d\n", *dev->buf, dev->buf_len); 277 + dev_dbg(dev->dev, "wrote 0x%x, to go %zu\n", *dev->buf, dev->buf_len); 278 278 279 279 ++dev->buf; 280 280 } ··· 402 402 dev->msg->flags &= ~I2C_M_RECV_LEN; 403 403 dev->buf_len += *dev->buf; 404 404 dev->msg->len = dev->buf_len + 1; 405 - dev_dbg(dev->dev, "received block length %d\n", 405 + dev_dbg(dev->dev, "received block length %zu\n", 406 406 dev->buf_len); 407 407 } else { 408 408 /* abort and send the stop by reading one more byte */ ··· 415 415 if (!dev->use_alt_cmd && dev->buf_len == 1) 416 416 at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_STOP); 417 417 418 - dev_dbg(dev->dev, "read 0x%x, to go %d\n", *dev->buf, dev->buf_len); 418 + dev_dbg(dev->dev, "read 0x%x, to go %zu\n", *dev->buf, dev->buf_len); 419 419 420 420 ++dev->buf; 421 421 } ··· 622 622 * writing the corresponding bit into the Control Register. 623 623 */ 624 624 625 - dev_dbg(dev->dev, "transfer: %s %d bytes.\n", 625 + dev_dbg(dev->dev, "transfer: %s %zu bytes.\n", 626 626 (dev->msg->flags & I2C_M_RD) ? "read" : "write", dev->buf_len); 627 627 628 628 reinit_completion(&dev->cmd_complete); ··· 1083 1083 dev_err(dev->dev, "no clock defined\n"); 1084 1084 return -ENODEV; 1085 1085 } 1086 - clk_prepare_enable(dev->clk); 1086 + rc = clk_prepare_enable(dev->clk); 1087 + if (rc) 1088 + return rc; 1087 1089 1088 1090 if (dev->dev->of_node) { 1089 1091 rc = at91_twi_configure_dma(dev, phy_addr); 1090 - if (rc == -EPROBE_DEFER) 1092 + if (rc == -EPROBE_DEFER) { 1093 + clk_disable_unprepare(dev->clk); 1091 1094 return rc; 1095 + } 1092 1096 } 1093 1097 1094 1098 if (!of_property_read_u32(pdev->dev.of_node, "atmel,fifo-size",
+3 -3
drivers/i2c/busses/i2c-cadence.c
··· 405 405 cdns_i2c_writereg(id->recv_count, CDNS_I2C_XFER_SIZE_OFFSET); 406 406 } 407 407 408 + /* Set the slave address in address register - triggers operation */ 409 + cdns_i2c_writereg(id->p_msg->addr & CDNS_I2C_ADDR_MASK, 410 + CDNS_I2C_ADDR_OFFSET); 408 411 /* Clear the bus hold flag if bytes to receive is less than FIFO size */ 409 412 if (!id->bus_hold_flag && 410 413 ((id->p_msg->flags & I2C_M_RECV_LEN) != I2C_M_RECV_LEN) && 411 414 (id->recv_count <= CDNS_I2C_FIFO_DEPTH)) 412 415 cdns_i2c_clear_bus_hold(id); 413 - /* Set the slave address in address register - triggers operation */ 414 - cdns_i2c_writereg(id->p_msg->addr & CDNS_I2C_ADDR_MASK, 415 - CDNS_I2C_ADDR_OFFSET); 416 416 cdns_i2c_writereg(CDNS_I2C_ENABLED_INTR_MASK, CDNS_I2C_IER_OFFSET); 417 417 } 418 418
+281
drivers/i2c/busses/i2c-designware-common.c
··· 1 + /* 2 + * Synopsys DesignWare I2C adapter driver. 3 + * 4 + * Based on the TI DAVINCI I2C adapter driver. 5 + * 6 + * Copyright (C) 2006 Texas Instruments. 7 + * Copyright (C) 2007 MontaVista Software Inc. 8 + * Copyright (C) 2009 Provigent Ltd. 9 + * 10 + * ---------------------------------------------------------------------------- 11 + * 12 + * This program is free software; you can redistribute it and/or modify 13 + * it under the terms of the GNU General Public License as published by 14 + * the Free Software Foundation; either version 2 of the License, or 15 + * (at your option) any later version. 16 + * 17 + * This program is distributed in the hope that it will be useful, 18 + * but WITHOUT ANY WARRANTY; without even the implied warranty of 19 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 20 + * GNU General Public License for more details. 21 + * ---------------------------------------------------------------------------- 22 + * 23 + */ 24 + #include <linux/delay.h> 25 + #include <linux/export.h> 26 + #include <linux/errno.h> 27 + #include <linux/err.h> 28 + #include <linux/i2c.h> 29 + #include <linux/interrupt.h> 30 + #include <linux/io.h> 31 + #include <linux/module.h> 32 + #include <linux/pm_runtime.h> 33 + 34 + #include "i2c-designware-core.h" 35 + 36 + static char *abort_sources[] = { 37 + [ABRT_7B_ADDR_NOACK] = 38 + "slave address not acknowledged (7bit mode)", 39 + [ABRT_10ADDR1_NOACK] = 40 + "first address byte not acknowledged (10bit mode)", 41 + [ABRT_10ADDR2_NOACK] = 42 + "second address byte not acknowledged (10bit mode)", 43 + [ABRT_TXDATA_NOACK] = 44 + "data not acknowledged", 45 + [ABRT_GCALL_NOACK] = 46 + "no acknowledgement for a general call", 47 + [ABRT_GCALL_READ] = 48 + "read after general call", 49 + [ABRT_SBYTE_ACKDET] = 50 + "start byte acknowledged", 51 + [ABRT_SBYTE_NORSTRT] = 52 + "trying to send start byte when restart is disabled", 53 + [ABRT_10B_RD_NORSTRT] = 54 + "trying to read when restart is disabled (10bit mode)", 55 + [ABRT_MASTER_DIS] = 56 + "trying to use disabled adapter", 57 + [ARB_LOST] = 58 + "lost arbitration", 59 + [ABRT_SLAVE_FLUSH_TXFIFO] = 60 + "read command so flush old data in the TX FIFO", 61 + [ABRT_SLAVE_ARBLOST] = 62 + "slave lost the bus while transmitting data to a remote master", 63 + [ABRT_SLAVE_RD_INTX] = 64 + "incorrect slave-transmitter mode configuration", 65 + }; 66 + 67 + u32 dw_readl(struct dw_i2c_dev *dev, int offset) 68 + { 69 + u32 value; 70 + 71 + if (dev->flags & ACCESS_16BIT) 72 + value = readw_relaxed(dev->base + offset) | 73 + (readw_relaxed(dev->base + offset + 2) << 16); 74 + else 75 + value = readl_relaxed(dev->base + offset); 76 + 77 + if (dev->flags & ACCESS_SWAP) 78 + return swab32(value); 79 + else 80 + return value; 81 + } 82 + 83 + void dw_writel(struct dw_i2c_dev *dev, u32 b, int offset) 84 + { 85 + if (dev->flags & ACCESS_SWAP) 86 + b = swab32(b); 87 + 88 + if (dev->flags & ACCESS_16BIT) { 89 + writew_relaxed((u16)b, dev->base + offset); 90 + writew_relaxed((u16)(b >> 16), dev->base + offset + 2); 91 + } else { 92 + writel_relaxed(b, dev->base + offset); 93 + } 94 + } 95 + 96 + u32 i2c_dw_scl_hcnt(u32 ic_clk, u32 tSYMBOL, u32 tf, int cond, int offset) 97 + { 98 + /* 99 + * DesignWare I2C core doesn't seem to have solid strategy to meet 100 + * the tHD;STA timing spec. Configuring _HCNT based on tHIGH spec 101 + * will result in violation of the tHD;STA spec. 102 + */ 103 + if (cond) 104 + /* 105 + * Conditional expression: 106 + * 107 + * IC_[FS]S_SCL_HCNT + (1+4+3) >= IC_CLK * tHIGH 108 + * 109 + * This is based on the DW manuals, and represents an ideal 110 + * configuration. The resulting I2C bus speed will be 111 + * faster than any of the others. 112 + * 113 + * If your hardware is free from tHD;STA issue, try this one. 114 + */ 115 + return (ic_clk * tSYMBOL + 500000) / 1000000 - 8 + offset; 116 + else 117 + /* 118 + * Conditional expression: 119 + * 120 + * IC_[FS]S_SCL_HCNT + 3 >= IC_CLK * (tHD;STA + tf) 121 + * 122 + * This is just experimental rule; the tHD;STA period turned 123 + * out to be proportinal to (_HCNT + 3). With this setting, 124 + * we could meet both tHIGH and tHD;STA timing specs. 125 + * 126 + * If unsure, you'd better to take this alternative. 127 + * 128 + * The reason why we need to take into account "tf" here, 129 + * is the same as described in i2c_dw_scl_lcnt(). 130 + */ 131 + return (ic_clk * (tSYMBOL + tf) + 500000) / 1000000 132 + - 3 + offset; 133 + } 134 + 135 + u32 i2c_dw_scl_lcnt(u32 ic_clk, u32 tLOW, u32 tf, int offset) 136 + { 137 + /* 138 + * Conditional expression: 139 + * 140 + * IC_[FS]S_SCL_LCNT + 1 >= IC_CLK * (tLOW + tf) 141 + * 142 + * DW I2C core starts counting the SCL CNTs for the LOW period 143 + * of the SCL clock (tLOW) as soon as it pulls the SCL line. 144 + * In order to meet the tLOW timing spec, we need to take into 145 + * account the fall time of SCL signal (tf). Default tf value 146 + * should be 0.3 us, for safety. 147 + */ 148 + return ((ic_clk * (tLOW + tf) + 500000) / 1000000) - 1 + offset; 149 + } 150 + 151 + void __i2c_dw_enable(struct dw_i2c_dev *dev, bool enable) 152 + { 153 + dw_writel(dev, enable, DW_IC_ENABLE); 154 + } 155 + 156 + void __i2c_dw_enable_and_wait(struct dw_i2c_dev *dev, bool enable) 157 + { 158 + int timeout = 100; 159 + 160 + do { 161 + __i2c_dw_enable(dev, enable); 162 + if ((dw_readl(dev, DW_IC_ENABLE_STATUS) & 1) == enable) 163 + return; 164 + 165 + /* 166 + * Wait 10 times the signaling period of the highest I2C 167 + * transfer supported by the driver (for 400KHz this is 168 + * 25us) as described in the DesignWare I2C databook. 169 + */ 170 + usleep_range(25, 250); 171 + } while (timeout--); 172 + 173 + dev_warn(dev->dev, "timeout in %sabling adapter\n", 174 + enable ? "en" : "dis"); 175 + } 176 + 177 + unsigned long i2c_dw_clk_rate(struct dw_i2c_dev *dev) 178 + { 179 + /* 180 + * Clock is not necessary if we got LCNT/HCNT values directly from 181 + * the platform code. 182 + */ 183 + if (WARN_ON_ONCE(!dev->get_clk_rate_khz)) 184 + return 0; 185 + return dev->get_clk_rate_khz(dev); 186 + } 187 + 188 + int i2c_dw_acquire_lock(struct dw_i2c_dev *dev) 189 + { 190 + int ret; 191 + 192 + if (!dev->acquire_lock) 193 + return 0; 194 + 195 + ret = dev->acquire_lock(dev); 196 + if (!ret) 197 + return 0; 198 + 199 + dev_err(dev->dev, "couldn't acquire bus ownership\n"); 200 + 201 + return ret; 202 + } 203 + 204 + void i2c_dw_release_lock(struct dw_i2c_dev *dev) 205 + { 206 + if (dev->release_lock) 207 + dev->release_lock(dev); 208 + } 209 + 210 + /* 211 + * Waiting for bus not busy 212 + */ 213 + int i2c_dw_wait_bus_not_busy(struct dw_i2c_dev *dev) 214 + { 215 + int timeout = TIMEOUT; 216 + 217 + while (dw_readl(dev, DW_IC_STATUS) & DW_IC_STATUS_ACTIVITY) { 218 + if (timeout <= 0) { 219 + dev_warn(dev->dev, "timeout waiting for bus ready\n"); 220 + return -ETIMEDOUT; 221 + } 222 + timeout--; 223 + usleep_range(1000, 1100); 224 + } 225 + 226 + return 0; 227 + } 228 + 229 + int i2c_dw_handle_tx_abort(struct dw_i2c_dev *dev) 230 + { 231 + unsigned long abort_source = dev->abort_source; 232 + int i; 233 + 234 + if (abort_source & DW_IC_TX_ABRT_NOACK) { 235 + for_each_set_bit(i, &abort_source, ARRAY_SIZE(abort_sources)) 236 + dev_dbg(dev->dev, 237 + "%s: %s\n", __func__, abort_sources[i]); 238 + return -EREMOTEIO; 239 + } 240 + 241 + for_each_set_bit(i, &abort_source, ARRAY_SIZE(abort_sources)) 242 + dev_err(dev->dev, "%s: %s\n", __func__, abort_sources[i]); 243 + 244 + if (abort_source & DW_IC_TX_ARB_LOST) 245 + return -EAGAIN; 246 + else if (abort_source & DW_IC_TX_ABRT_GCALL_READ) 247 + return -EINVAL; /* wrong msgs[] data */ 248 + else 249 + return -EIO; 250 + } 251 + 252 + u32 i2c_dw_func(struct i2c_adapter *adap) 253 + { 254 + struct dw_i2c_dev *dev = i2c_get_adapdata(adap); 255 + 256 + return dev->functionality; 257 + } 258 + 259 + void i2c_dw_disable(struct dw_i2c_dev *dev) 260 + { 261 + /* Disable controller */ 262 + __i2c_dw_enable_and_wait(dev, false); 263 + 264 + /* Disable all interupts */ 265 + dw_writel(dev, 0, DW_IC_INTR_MASK); 266 + dw_readl(dev, DW_IC_CLR_INTR); 267 + } 268 + 269 + void i2c_dw_disable_int(struct dw_i2c_dev *dev) 270 + { 271 + dw_writel(dev, 0, DW_IC_INTR_MASK); 272 + } 273 + 274 + u32 i2c_dw_read_comp_param(struct dw_i2c_dev *dev) 275 + { 276 + return dw_readl(dev, DW_IC_COMP_PARAM_1); 277 + } 278 + EXPORT_SYMBOL_GPL(i2c_dw_read_comp_param); 279 + 280 + MODULE_DESCRIPTION("Synopsys DesignWare I2C bus adapter core"); 281 + MODULE_LICENSE("GPL");
+72 -412
drivers/i2c/busses/i2c-designware-core.c drivers/i2c/busses/i2c-designware-master.c
··· 21 21 * ---------------------------------------------------------------------------- 22 22 * 23 23 */ 24 - #include <linux/export.h> 25 - #include <linux/errno.h> 24 + #include <linux/delay.h> 26 25 #include <linux/err.h> 26 + #include <linux/errno.h> 27 + #include <linux/export.h> 27 28 #include <linux/i2c.h> 28 29 #include <linux/interrupt.h> 29 30 #include <linux/io.h> 30 - #include <linux/pm_runtime.h> 31 - #include <linux/delay.h> 32 31 #include <linux/module.h> 32 + #include <linux/pm_runtime.h> 33 + 33 34 #include "i2c-designware-core.h" 34 35 35 - /* 36 - * Registers offset 37 - */ 38 - #define DW_IC_CON 0x0 39 - #define DW_IC_TAR 0x4 40 - #define DW_IC_DATA_CMD 0x10 41 - #define DW_IC_SS_SCL_HCNT 0x14 42 - #define DW_IC_SS_SCL_LCNT 0x18 43 - #define DW_IC_FS_SCL_HCNT 0x1c 44 - #define DW_IC_FS_SCL_LCNT 0x20 45 - #define DW_IC_HS_SCL_HCNT 0x24 46 - #define DW_IC_HS_SCL_LCNT 0x28 47 - #define DW_IC_INTR_STAT 0x2c 48 - #define DW_IC_INTR_MASK 0x30 49 - #define DW_IC_RAW_INTR_STAT 0x34 50 - #define DW_IC_RX_TL 0x38 51 - #define DW_IC_TX_TL 0x3c 52 - #define DW_IC_CLR_INTR 0x40 53 - #define DW_IC_CLR_RX_UNDER 0x44 54 - #define DW_IC_CLR_RX_OVER 0x48 55 - #define DW_IC_CLR_TX_OVER 0x4c 56 - #define DW_IC_CLR_RD_REQ 0x50 57 - #define DW_IC_CLR_TX_ABRT 0x54 58 - #define DW_IC_CLR_RX_DONE 0x58 59 - #define DW_IC_CLR_ACTIVITY 0x5c 60 - #define DW_IC_CLR_STOP_DET 0x60 61 - #define DW_IC_CLR_START_DET 0x64 62 - #define DW_IC_CLR_GEN_CALL 0x68 63 - #define DW_IC_ENABLE 0x6c 64 - #define DW_IC_STATUS 0x70 65 - #define DW_IC_TXFLR 0x74 66 - #define DW_IC_RXFLR 0x78 67 - #define DW_IC_SDA_HOLD 0x7c 68 - #define DW_IC_TX_ABRT_SOURCE 0x80 69 - #define DW_IC_ENABLE_STATUS 0x9c 70 - #define DW_IC_COMP_PARAM_1 0xf4 71 - #define DW_IC_COMP_VERSION 0xf8 72 - #define DW_IC_SDA_HOLD_MIN_VERS 0x3131312A 73 - #define DW_IC_COMP_TYPE 0xfc 74 - #define DW_IC_COMP_TYPE_VALUE 0x44570140 75 - 76 - #define DW_IC_INTR_RX_UNDER 0x001 77 - #define DW_IC_INTR_RX_OVER 0x002 78 - #define DW_IC_INTR_RX_FULL 0x004 79 - #define DW_IC_INTR_TX_OVER 0x008 80 - #define DW_IC_INTR_TX_EMPTY 0x010 81 - #define DW_IC_INTR_RD_REQ 0x020 82 - #define DW_IC_INTR_TX_ABRT 0x040 83 - #define DW_IC_INTR_RX_DONE 0x080 84 - #define DW_IC_INTR_ACTIVITY 0x100 85 - #define DW_IC_INTR_STOP_DET 0x200 86 - #define DW_IC_INTR_START_DET 0x400 87 - #define DW_IC_INTR_GEN_CALL 0x800 88 - 89 - #define DW_IC_INTR_DEFAULT_MASK (DW_IC_INTR_RX_FULL | \ 90 - DW_IC_INTR_TX_EMPTY | \ 91 - DW_IC_INTR_TX_ABRT | \ 92 - DW_IC_INTR_STOP_DET) 93 - 94 - #define DW_IC_STATUS_ACTIVITY 0x1 95 - 96 - #define DW_IC_SDA_HOLD_RX_SHIFT 16 97 - #define DW_IC_SDA_HOLD_RX_MASK GENMASK(23, DW_IC_SDA_HOLD_RX_SHIFT) 98 - 99 - #define DW_IC_ERR_TX_ABRT 0x1 100 - 101 - #define DW_IC_TAR_10BITADDR_MASTER BIT(12) 102 - 103 - #define DW_IC_COMP_PARAM_1_SPEED_MODE_HIGH (BIT(2) | BIT(3)) 104 - #define DW_IC_COMP_PARAM_1_SPEED_MODE_MASK GENMASK(3, 2) 105 - 106 - /* 107 - * status codes 108 - */ 109 - #define STATUS_IDLE 0x0 110 - #define STATUS_WRITE_IN_PROGRESS 0x1 111 - #define STATUS_READ_IN_PROGRESS 0x2 112 - 113 - #define TIMEOUT 20 /* ms */ 114 - 115 - /* 116 - * hardware abort codes from the DW_IC_TX_ABRT_SOURCE register 117 - * 118 - * only expected abort codes are listed here 119 - * refer to the datasheet for the full list 120 - */ 121 - #define ABRT_7B_ADDR_NOACK 0 122 - #define ABRT_10ADDR1_NOACK 1 123 - #define ABRT_10ADDR2_NOACK 2 124 - #define ABRT_TXDATA_NOACK 3 125 - #define ABRT_GCALL_NOACK 4 126 - #define ABRT_GCALL_READ 5 127 - #define ABRT_SBYTE_ACKDET 7 128 - #define ABRT_SBYTE_NORSTRT 9 129 - #define ABRT_10B_RD_NORSTRT 10 130 - #define ABRT_MASTER_DIS 11 131 - #define ARB_LOST 12 132 - 133 - #define DW_IC_TX_ABRT_7B_ADDR_NOACK (1UL << ABRT_7B_ADDR_NOACK) 134 - #define DW_IC_TX_ABRT_10ADDR1_NOACK (1UL << ABRT_10ADDR1_NOACK) 135 - #define DW_IC_TX_ABRT_10ADDR2_NOACK (1UL << ABRT_10ADDR2_NOACK) 136 - #define DW_IC_TX_ABRT_TXDATA_NOACK (1UL << ABRT_TXDATA_NOACK) 137 - #define DW_IC_TX_ABRT_GCALL_NOACK (1UL << ABRT_GCALL_NOACK) 138 - #define DW_IC_TX_ABRT_GCALL_READ (1UL << ABRT_GCALL_READ) 139 - #define DW_IC_TX_ABRT_SBYTE_ACKDET (1UL << ABRT_SBYTE_ACKDET) 140 - #define DW_IC_TX_ABRT_SBYTE_NORSTRT (1UL << ABRT_SBYTE_NORSTRT) 141 - #define DW_IC_TX_ABRT_10B_RD_NORSTRT (1UL << ABRT_10B_RD_NORSTRT) 142 - #define DW_IC_TX_ABRT_MASTER_DIS (1UL << ABRT_MASTER_DIS) 143 - #define DW_IC_TX_ARB_LOST (1UL << ARB_LOST) 144 - 145 - #define DW_IC_TX_ABRT_NOACK (DW_IC_TX_ABRT_7B_ADDR_NOACK | \ 146 - DW_IC_TX_ABRT_10ADDR1_NOACK | \ 147 - DW_IC_TX_ABRT_10ADDR2_NOACK | \ 148 - DW_IC_TX_ABRT_TXDATA_NOACK | \ 149 - DW_IC_TX_ABRT_GCALL_NOACK) 150 - 151 - static char *abort_sources[] = { 152 - [ABRT_7B_ADDR_NOACK] = 153 - "slave address not acknowledged (7bit mode)", 154 - [ABRT_10ADDR1_NOACK] = 155 - "first address byte not acknowledged (10bit mode)", 156 - [ABRT_10ADDR2_NOACK] = 157 - "second address byte not acknowledged (10bit mode)", 158 - [ABRT_TXDATA_NOACK] = 159 - "data not acknowledged", 160 - [ABRT_GCALL_NOACK] = 161 - "no acknowledgement for a general call", 162 - [ABRT_GCALL_READ] = 163 - "read after general call", 164 - [ABRT_SBYTE_ACKDET] = 165 - "start byte acknowledged", 166 - [ABRT_SBYTE_NORSTRT] = 167 - "trying to send start byte when restart is disabled", 168 - [ABRT_10B_RD_NORSTRT] = 169 - "trying to read when restart is disabled (10bit mode)", 170 - [ABRT_MASTER_DIS] = 171 - "trying to use disabled adapter", 172 - [ARB_LOST] = 173 - "lost arbitration", 174 - }; 175 - 176 - static u32 dw_readl(struct dw_i2c_dev *dev, int offset) 36 + static void i2c_dw_configure_fifo_master(struct dw_i2c_dev *dev) 177 37 { 178 - u32 value; 38 + /* Configure Tx/Rx FIFO threshold levels */ 39 + dw_writel(dev, dev->tx_fifo_depth / 2, DW_IC_TX_TL); 40 + dw_writel(dev, 0, DW_IC_RX_TL); 179 41 180 - if (dev->flags & ACCESS_16BIT) 181 - value = readw_relaxed(dev->base + offset) | 182 - (readw_relaxed(dev->base + offset + 2) << 16); 183 - else 184 - value = readl_relaxed(dev->base + offset); 185 - 186 - if (dev->flags & ACCESS_SWAP) 187 - return swab32(value); 188 - else 189 - return value; 190 - } 191 - 192 - static void dw_writel(struct dw_i2c_dev *dev, u32 b, int offset) 193 - { 194 - if (dev->flags & ACCESS_SWAP) 195 - b = swab32(b); 196 - 197 - if (dev->flags & ACCESS_16BIT) { 198 - writew_relaxed((u16)b, dev->base + offset); 199 - writew_relaxed((u16)(b >> 16), dev->base + offset + 2); 200 - } else { 201 - writel_relaxed(b, dev->base + offset); 202 - } 203 - } 204 - 205 - static u32 206 - i2c_dw_scl_hcnt(u32 ic_clk, u32 tSYMBOL, u32 tf, int cond, int offset) 207 - { 208 - /* 209 - * DesignWare I2C core doesn't seem to have solid strategy to meet 210 - * the tHD;STA timing spec. Configuring _HCNT based on tHIGH spec 211 - * will result in violation of the tHD;STA spec. 212 - */ 213 - if (cond) 214 - /* 215 - * Conditional expression: 216 - * 217 - * IC_[FS]S_SCL_HCNT + (1+4+3) >= IC_CLK * tHIGH 218 - * 219 - * This is based on the DW manuals, and represents an ideal 220 - * configuration. The resulting I2C bus speed will be 221 - * faster than any of the others. 222 - * 223 - * If your hardware is free from tHD;STA issue, try this one. 224 - */ 225 - return (ic_clk * tSYMBOL + 500000) / 1000000 - 8 + offset; 226 - else 227 - /* 228 - * Conditional expression: 229 - * 230 - * IC_[FS]S_SCL_HCNT + 3 >= IC_CLK * (tHD;STA + tf) 231 - * 232 - * This is just experimental rule; the tHD;STA period turned 233 - * out to be proportinal to (_HCNT + 3). With this setting, 234 - * we could meet both tHIGH and tHD;STA timing specs. 235 - * 236 - * If unsure, you'd better to take this alternative. 237 - * 238 - * The reason why we need to take into account "tf" here, 239 - * is the same as described in i2c_dw_scl_lcnt(). 240 - */ 241 - return (ic_clk * (tSYMBOL + tf) + 500000) / 1000000 242 - - 3 + offset; 243 - } 244 - 245 - static u32 i2c_dw_scl_lcnt(u32 ic_clk, u32 tLOW, u32 tf, int offset) 246 - { 247 - /* 248 - * Conditional expression: 249 - * 250 - * IC_[FS]S_SCL_LCNT + 1 >= IC_CLK * (tLOW + tf) 251 - * 252 - * DW I2C core starts counting the SCL CNTs for the LOW period 253 - * of the SCL clock (tLOW) as soon as it pulls the SCL line. 254 - * In order to meet the tLOW timing spec, we need to take into 255 - * account the fall time of SCL signal (tf). Default tf value 256 - * should be 0.3 us, for safety. 257 - */ 258 - return ((ic_clk * (tLOW + tf) + 500000) / 1000000) - 1 + offset; 259 - } 260 - 261 - static void __i2c_dw_enable(struct dw_i2c_dev *dev, bool enable) 262 - { 263 - dw_writel(dev, enable, DW_IC_ENABLE); 264 - } 265 - 266 - static void __i2c_dw_enable_and_wait(struct dw_i2c_dev *dev, bool enable) 267 - { 268 - int timeout = 100; 269 - 270 - do { 271 - __i2c_dw_enable(dev, enable); 272 - if ((dw_readl(dev, DW_IC_ENABLE_STATUS) & 1) == enable) 273 - return; 274 - 275 - /* 276 - * Wait 10 times the signaling period of the highest I2C 277 - * transfer supported by the driver (for 400KHz this is 278 - * 25us) as described in the DesignWare I2C databook. 279 - */ 280 - usleep_range(25, 250); 281 - } while (timeout--); 282 - 283 - dev_warn(dev->dev, "timeout in %sabling adapter\n", 284 - enable ? "en" : "dis"); 285 - } 286 - 287 - static unsigned long i2c_dw_clk_rate(struct dw_i2c_dev *dev) 288 - { 289 - /* 290 - * Clock is not necessary if we got LCNT/HCNT values directly from 291 - * the platform code. 292 - */ 293 - if (WARN_ON_ONCE(!dev->get_clk_rate_khz)) 294 - return 0; 295 - return dev->get_clk_rate_khz(dev); 296 - } 297 - 298 - static int i2c_dw_acquire_lock(struct dw_i2c_dev *dev) 299 - { 300 - int ret; 301 - 302 - if (!dev->acquire_lock) 303 - return 0; 304 - 305 - ret = dev->acquire_lock(dev); 306 - if (!ret) 307 - return 0; 308 - 309 - dev_err(dev->dev, "couldn't acquire bus ownership\n"); 310 - 311 - return ret; 312 - } 313 - 314 - static void i2c_dw_release_lock(struct dw_i2c_dev *dev) 315 - { 316 - if (dev->release_lock) 317 - dev->release_lock(dev); 42 + /* Configure the I2C master */ 43 + dw_writel(dev, dev->master_cfg, DW_IC_CON); 318 44 } 319 45 320 46 /** 321 - * i2c_dw_init() - initialize the designware i2c master hardware 47 + * i2c_dw_init() - Initialize the designware I2C master hardware 322 48 * @dev: device private data 323 49 * 324 50 * This functions configures and enables the I2C master. 325 51 * This function is called during I2C init function, and in case of timeout at 326 52 * run time. 327 53 */ 328 - int i2c_dw_init(struct dw_i2c_dev *dev) 54 + static int i2c_dw_init_master(struct dw_i2c_dev *dev) 329 55 { 330 56 u32 hcnt, lcnt; 331 57 u32 reg, comp_param1; ··· 70 344 /* Configure register access mode 16bit */ 71 345 dev->flags |= ACCESS_16BIT; 72 346 } else if (reg != DW_IC_COMP_TYPE_VALUE) { 73 - dev_err(dev->dev, "Unknown Synopsys component type: " 74 - "0x%08x\n", reg); 347 + dev_err(dev->dev, 348 + "Unknown Synopsys component type: 0x%08x\n", reg); 75 349 i2c_dw_release_lock(dev); 76 350 return -ENODEV; 77 351 } ··· 81 355 /* Disable the adapter */ 82 356 __i2c_dw_enable_and_wait(dev, false); 83 357 84 - /* set standard and fast speed deviders for high/low periods */ 358 + /* Set standard and fast speed deviders for high/low periods */ 85 359 86 360 sda_falling_time = dev->sda_falling_time ?: 300; /* ns */ 87 361 scl_falling_time = dev->scl_falling_time ?: 300; /* ns */ ··· 166 440 "Hardware too old to adjust SDA hold time.\n"); 167 441 } 168 442 169 - /* Configure Tx/Rx FIFO threshold levels */ 170 - dw_writel(dev, dev->tx_fifo_depth / 2, DW_IC_TX_TL); 171 - dw_writel(dev, 0, DW_IC_RX_TL); 172 - 173 - /* configure the i2c master */ 174 - dw_writel(dev, dev->master_cfg , DW_IC_CON); 175 - 443 + i2c_dw_configure_fifo_master(dev); 176 444 i2c_dw_release_lock(dev); 177 - 178 - return 0; 179 - } 180 - EXPORT_SYMBOL_GPL(i2c_dw_init); 181 - 182 - /* 183 - * Waiting for bus not busy 184 - */ 185 - static int i2c_dw_wait_bus_not_busy(struct dw_i2c_dev *dev) 186 - { 187 - int timeout = TIMEOUT; 188 - 189 - while (dw_readl(dev, DW_IC_STATUS) & DW_IC_STATUS_ACTIVITY) { 190 - if (timeout <= 0) { 191 - dev_warn(dev->dev, "timeout waiting for bus ready\n"); 192 - return -ETIMEDOUT; 193 - } 194 - timeout--; 195 - usleep_range(1000, 1100); 196 - } 197 445 198 446 return 0; 199 447 } ··· 180 480 /* Disable the adapter */ 181 481 __i2c_dw_enable_and_wait(dev, false); 182 482 183 - /* if the slave address is ten bit address, enable 10BITADDR */ 483 + /* If the slave address is ten bit address, enable 10BITADDR */ 184 484 ic_con = dw_readl(dev, DW_IC_CON); 185 485 if (msgs[dev->msg_write_idx].flags & I2C_M_TEN) { 186 486 ic_con |= DW_IC_CON_10BITADDR_MASTER; ··· 203 503 */ 204 504 dw_writel(dev, msgs[dev->msg_write_idx].addr | ic_tar, DW_IC_TAR); 205 505 206 - /* enforce disabled interrupts (due to HW issues) */ 506 + /* Enforce disabled interrupts (due to HW issues) */ 207 507 i2c_dw_disable_int(dev); 208 508 209 509 /* Enable the adapter */ ··· 211 511 212 512 /* Clear and enable interrupts */ 213 513 dw_readl(dev, DW_IC_CLR_INTR); 214 - dw_writel(dev, DW_IC_INTR_DEFAULT_MASK, DW_IC_INTR_MASK); 514 + dw_writel(dev, DW_IC_INTR_MASTER_MASK, DW_IC_INTR_MASK); 215 515 } 216 516 217 517 /* ··· 231 531 u8 *buf = dev->tx_buf; 232 532 bool need_restart = false; 233 533 234 - intr_mask = DW_IC_INTR_DEFAULT_MASK; 534 + intr_mask = DW_IC_INTR_MASTER_MASK; 235 535 236 536 for (; dev->msg_write_idx < dev->msgs_num; dev->msg_write_idx++) { 237 537 u32 flags = msgs[dev->msg_write_idx].flags; 238 538 239 539 /* 240 - * if target address has changed, we need to 241 - * reprogram the target address in the i2c 242 - * adapter when we are done with this transfer 540 + * If target address has changed, we need to 541 + * reprogram the target address in the I2C 542 + * adapter when we are done with this transfer. 243 543 */ 244 544 if (msgs[dev->msg_write_idx].addr != addr) { 245 545 dev_err(dev->dev, ··· 283 583 */ 284 584 285 585 /* 286 - * i2c-core.c always sets the buffer length of 586 + * i2c-core always sets the buffer length of 287 587 * I2C_FUNC_SMBUS_BLOCK_DATA to 1. The length will 288 588 * be adjusted when receiving the first byte. 289 589 * Thus we can't stop the transaction here. ··· 299 599 300 600 if (msgs[dev->msg_write_idx].flags & I2C_M_RD) { 301 601 302 - /* avoid rx buffer overrun */ 602 + /* Avoid rx buffer overrun */ 303 603 if (dev->rx_outstanding >= dev->rx_fifo_depth) 304 604 break; 305 605 ··· 404 704 } 405 705 } 406 706 407 - static int i2c_dw_handle_tx_abort(struct dw_i2c_dev *dev) 408 - { 409 - unsigned long abort_source = dev->abort_source; 410 - int i; 411 - 412 - if (abort_source & DW_IC_TX_ABRT_NOACK) { 413 - for_each_set_bit(i, &abort_source, ARRAY_SIZE(abort_sources)) 414 - dev_dbg(dev->dev, 415 - "%s: %s\n", __func__, abort_sources[i]); 416 - return -EREMOTEIO; 417 - } 418 - 419 - for_each_set_bit(i, &abort_source, ARRAY_SIZE(abort_sources)) 420 - dev_err(dev->dev, "%s: %s\n", __func__, abort_sources[i]); 421 - 422 - if (abort_source & DW_IC_TX_ARB_LOST) 423 - return -EAGAIN; 424 - else if (abort_source & DW_IC_TX_ABRT_GCALL_READ) 425 - return -EINVAL; /* wrong msgs[] data */ 426 - else 427 - return -EIO; 428 - } 429 - 430 707 /* 431 - * Prepare controller for a transaction and call i2c_dw_xfer_msg 708 + * Prepare controller for a transaction and call i2c_dw_xfer_msg. 432 709 */ 433 710 static int 434 711 i2c_dw_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num) ··· 436 759 if (ret < 0) 437 760 goto done; 438 761 439 - /* start the transfers */ 762 + /* Start the transfers */ 440 763 i2c_dw_xfer_init(dev); 441 764 442 - /* wait for tx to complete */ 765 + /* Wait for tx to complete */ 443 766 if (!wait_for_completion_timeout(&dev->cmd_complete, adap->timeout)) { 444 767 dev_err(dev->dev, "controller timed out\n"); 445 768 /* i2c_dw_init implicitly disables the adapter */ 446 - i2c_dw_init(dev); 769 + i2c_dw_init_master(dev); 447 770 ret = -ETIMEDOUT; 448 771 goto done; 449 772 } ··· 463 786 goto done; 464 787 } 465 788 466 - /* no error */ 789 + /* No error */ 467 790 if (likely(!dev->cmd_err && !dev->status)) { 468 791 ret = num; 469 792 goto done; ··· 491 814 return ret; 492 815 } 493 816 494 - static u32 i2c_dw_func(struct i2c_adapter *adap) 495 - { 496 - struct dw_i2c_dev *dev = i2c_get_adapdata(adap); 497 - return dev->functionality; 498 - } 499 - 500 817 static const struct i2c_algorithm i2c_dw_algo = { 501 - .master_xfer = i2c_dw_xfer, 502 - .functionality = i2c_dw_func, 818 + .master_xfer = i2c_dw_xfer, 819 + .functionality = i2c_dw_func, 503 820 }; 504 821 505 822 static u32 i2c_dw_read_clear_intrbits(struct dw_i2c_dev *dev) ··· 552 881 } 553 882 554 883 /* 555 - * Interrupt service routine. This gets called whenever an I2C interrupt 884 + * Interrupt service routine. This gets called whenever an I2C master interrupt 556 885 * occurs. 557 886 */ 558 - static irqreturn_t i2c_dw_isr(int this_irq, void *dev_id) 887 + static int i2c_dw_irq_handler_master(struct dw_i2c_dev *dev) 559 888 { 560 - struct dw_i2c_dev *dev = dev_id; 561 - u32 stat, enabled; 562 - 563 - enabled = dw_readl(dev, DW_IC_ENABLE); 564 - stat = dw_readl(dev, DW_IC_RAW_INTR_STAT); 565 - dev_dbg(dev->dev, "%s: enabled=%#x stat=%#x\n", __func__, enabled, stat); 566 - if (!enabled || !(stat & ~DW_IC_INTR_ACTIVITY)) 567 - return IRQ_NONE; 889 + u32 stat; 568 890 569 891 stat = i2c_dw_read_clear_intrbits(dev); 570 - 571 892 if (stat & DW_IC_INTR_TX_ABRT) { 572 893 dev->cmd_err |= DW_IC_ERR_TX_ABRT; 573 894 dev->status = STATUS_IDLE; 574 895 575 896 /* 576 897 * Anytime TX_ABRT is set, the contents of the tx/rx 577 - * buffers are flushed. Make sure to skip them. 898 + * buffers are flushed. Make sure to skip them. 578 899 */ 579 900 dw_writel(dev, 0, DW_IC_INTR_MASK); 580 901 goto tx_aborted; ··· 588 925 if ((stat & (DW_IC_INTR_TX_ABRT | DW_IC_INTR_STOP_DET)) || dev->msg_err) 589 926 complete(&dev->cmd_complete); 590 927 else if (unlikely(dev->flags & ACCESS_INTR_MASK)) { 591 - /* workaround to trigger pending interrupt */ 928 + /* Workaround to trigger pending interrupt */ 592 929 stat = dw_readl(dev, DW_IC_INTR_MASK); 593 930 i2c_dw_disable_int(dev); 594 931 dw_writel(dev, stat, DW_IC_INTR_MASK); 595 932 } 596 933 934 + return 0; 935 + } 936 + 937 + static irqreturn_t i2c_dw_isr(int this_irq, void *dev_id) 938 + { 939 + struct dw_i2c_dev *dev = dev_id; 940 + u32 stat, enabled; 941 + 942 + enabled = dw_readl(dev, DW_IC_ENABLE); 943 + stat = dw_readl(dev, DW_IC_RAW_INTR_STAT); 944 + dev_dbg(dev->dev, "enabled=%#x stat=%#x\n", enabled, stat); 945 + if (!enabled || !(stat & ~DW_IC_INTR_ACTIVITY)) 946 + return IRQ_NONE; 947 + 948 + i2c_dw_irq_handler_master(dev); 949 + 597 950 return IRQ_HANDLED; 598 951 } 599 - 600 - void i2c_dw_disable(struct dw_i2c_dev *dev) 601 - { 602 - /* Disable controller */ 603 - __i2c_dw_enable_and_wait(dev, false); 604 - 605 - /* Disable all interupts */ 606 - dw_writel(dev, 0, DW_IC_INTR_MASK); 607 - dw_readl(dev, DW_IC_CLR_INTR); 608 - } 609 - EXPORT_SYMBOL_GPL(i2c_dw_disable); 610 - 611 - void i2c_dw_disable_int(struct dw_i2c_dev *dev) 612 - { 613 - dw_writel(dev, 0, DW_IC_INTR_MASK); 614 - } 615 - EXPORT_SYMBOL_GPL(i2c_dw_disable_int); 616 - 617 - u32 i2c_dw_read_comp_param(struct dw_i2c_dev *dev) 618 - { 619 - return dw_readl(dev, DW_IC_COMP_PARAM_1); 620 - } 621 - EXPORT_SYMBOL_GPL(i2c_dw_read_comp_param); 622 952 623 953 int i2c_dw_probe(struct dw_i2c_dev *dev) 624 954 { 625 955 struct i2c_adapter *adap = &dev->adapter; 626 956 unsigned long irq_flags; 627 - int r; 957 + int ret; 628 958 629 959 init_completion(&dev->cmd_complete); 630 960 631 - r = i2c_dw_init(dev); 632 - if (r) 633 - return r; 961 + dev->init = i2c_dw_init_master; 962 + dev->disable = i2c_dw_disable; 963 + dev->disable_int = i2c_dw_disable_int; 964 + 965 + ret = dev->init(dev); 966 + if (ret) 967 + return ret; 634 968 635 969 snprintf(adap->name, sizeof(adap->name), 636 970 "Synopsys DesignWare I2C adapter"); ··· 644 984 } 645 985 646 986 i2c_dw_disable_int(dev); 647 - r = devm_request_irq(dev->dev, dev->irq, i2c_dw_isr, irq_flags, 648 - dev_name(dev->dev), dev); 649 - if (r) { 987 + ret = devm_request_irq(dev->dev, dev->irq, i2c_dw_isr, irq_flags, 988 + dev_name(dev->dev), dev); 989 + if (ret) { 650 990 dev_err(dev->dev, "failure requesting irq %i: %d\n", 651 - dev->irq, r); 652 - return r; 991 + dev->irq, ret); 992 + return ret; 653 993 } 654 994 655 995 /* ··· 659 999 * registered I2C slaves that do I2C transfers in their probe. 660 1000 */ 661 1001 pm_runtime_get_noresume(dev->dev); 662 - r = i2c_add_numbered_adapter(adap); 663 - if (r) 664 - dev_err(dev->dev, "failure adding adapter: %d\n", r); 1002 + ret = i2c_add_numbered_adapter(adap); 1003 + if (ret) 1004 + dev_err(dev->dev, "failure adding adapter: %d\n", ret); 665 1005 pm_runtime_put_noidle(dev->dev); 666 1006 667 - return r; 1007 + return ret; 668 1008 } 669 1009 EXPORT_SYMBOL_GPL(i2c_dw_probe); 670 1010 671 - MODULE_DESCRIPTION("Synopsys DesignWare I2C bus adapter core"); 1011 + MODULE_DESCRIPTION("Synopsys DesignWare I2C bus master adapter"); 672 1012 MODULE_LICENSE("GPL");
+177 -5
drivers/i2c/busses/i2c-designware-core.h
··· 1 1 /* 2 - * Synopsys DesignWare I2C adapter driver (master only). 2 + * Synopsys DesignWare I2C adapter driver. 3 3 * 4 4 * Based on the TI DAVINCI I2C adapter driver. 5 5 * ··· 37 37 #define DW_IC_CON_SPEED_FAST 0x4 38 38 #define DW_IC_CON_SPEED_HIGH 0x6 39 39 #define DW_IC_CON_SPEED_MASK 0x6 40 + #define DW_IC_CON_10BITADDR_SLAVE 0x8 40 41 #define DW_IC_CON_10BITADDR_MASTER 0x10 41 42 #define DW_IC_CON_RESTART_EN 0x20 42 43 #define DW_IC_CON_SLAVE_DISABLE 0x40 44 + #define DW_IC_CON_STOP_DET_IFADDRESSED 0x80 45 + #define DW_IC_CON_TX_EMPTY_CTRL 0x100 46 + #define DW_IC_CON_RX_FIFO_FULL_HLD_CTRL 0x200 47 + 48 + /* 49 + * Registers offset 50 + */ 51 + #define DW_IC_CON 0x0 52 + #define DW_IC_TAR 0x4 53 + #define DW_IC_SAR 0x8 54 + #define DW_IC_DATA_CMD 0x10 55 + #define DW_IC_SS_SCL_HCNT 0x14 56 + #define DW_IC_SS_SCL_LCNT 0x18 57 + #define DW_IC_FS_SCL_HCNT 0x1c 58 + #define DW_IC_FS_SCL_LCNT 0x20 59 + #define DW_IC_HS_SCL_HCNT 0x24 60 + #define DW_IC_HS_SCL_LCNT 0x28 61 + #define DW_IC_INTR_STAT 0x2c 62 + #define DW_IC_INTR_MASK 0x30 63 + #define DW_IC_RAW_INTR_STAT 0x34 64 + #define DW_IC_RX_TL 0x38 65 + #define DW_IC_TX_TL 0x3c 66 + #define DW_IC_CLR_INTR 0x40 67 + #define DW_IC_CLR_RX_UNDER 0x44 68 + #define DW_IC_CLR_RX_OVER 0x48 69 + #define DW_IC_CLR_TX_OVER 0x4c 70 + #define DW_IC_CLR_RD_REQ 0x50 71 + #define DW_IC_CLR_TX_ABRT 0x54 72 + #define DW_IC_CLR_RX_DONE 0x58 73 + #define DW_IC_CLR_ACTIVITY 0x5c 74 + #define DW_IC_CLR_STOP_DET 0x60 75 + #define DW_IC_CLR_START_DET 0x64 76 + #define DW_IC_CLR_GEN_CALL 0x68 77 + #define DW_IC_ENABLE 0x6c 78 + #define DW_IC_STATUS 0x70 79 + #define DW_IC_TXFLR 0x74 80 + #define DW_IC_RXFLR 0x78 81 + #define DW_IC_SDA_HOLD 0x7c 82 + #define DW_IC_TX_ABRT_SOURCE 0x80 83 + #define DW_IC_ENABLE_STATUS 0x9c 84 + #define DW_IC_CLR_RESTART_DET 0xa8 85 + #define DW_IC_COMP_PARAM_1 0xf4 86 + #define DW_IC_COMP_VERSION 0xf8 87 + #define DW_IC_SDA_HOLD_MIN_VERS 0x3131312A 88 + #define DW_IC_COMP_TYPE 0xfc 89 + #define DW_IC_COMP_TYPE_VALUE 0x44570140 90 + 91 + #define DW_IC_INTR_RX_UNDER 0x001 92 + #define DW_IC_INTR_RX_OVER 0x002 93 + #define DW_IC_INTR_RX_FULL 0x004 94 + #define DW_IC_INTR_TX_OVER 0x008 95 + #define DW_IC_INTR_TX_EMPTY 0x010 96 + #define DW_IC_INTR_RD_REQ 0x020 97 + #define DW_IC_INTR_TX_ABRT 0x040 98 + #define DW_IC_INTR_RX_DONE 0x080 99 + #define DW_IC_INTR_ACTIVITY 0x100 100 + #define DW_IC_INTR_STOP_DET 0x200 101 + #define DW_IC_INTR_START_DET 0x400 102 + #define DW_IC_INTR_GEN_CALL 0x800 103 + #define DW_IC_INTR_RESTART_DET 0x1000 104 + 105 + #define DW_IC_INTR_DEFAULT_MASK (DW_IC_INTR_RX_FULL | \ 106 + DW_IC_INTR_TX_ABRT | \ 107 + DW_IC_INTR_STOP_DET) 108 + #define DW_IC_INTR_MASTER_MASK (DW_IC_INTR_DEFAULT_MASK | \ 109 + DW_IC_INTR_TX_EMPTY) 110 + #define DW_IC_INTR_SLAVE_MASK (DW_IC_INTR_DEFAULT_MASK | \ 111 + DW_IC_INTR_RX_DONE | \ 112 + DW_IC_INTR_RX_UNDER | \ 113 + DW_IC_INTR_RD_REQ) 114 + 115 + #define DW_IC_STATUS_ACTIVITY 0x1 116 + #define DW_IC_STATUS_TFE BIT(2) 117 + #define DW_IC_STATUS_MASTER_ACTIVITY BIT(5) 118 + #define DW_IC_STATUS_SLAVE_ACTIVITY BIT(6) 119 + 120 + #define DW_IC_SDA_HOLD_RX_SHIFT 16 121 + #define DW_IC_SDA_HOLD_RX_MASK GENMASK(23, DW_IC_SDA_HOLD_RX_SHIFT) 122 + 123 + #define DW_IC_ERR_TX_ABRT 0x1 124 + 125 + #define DW_IC_TAR_10BITADDR_MASTER BIT(12) 126 + 127 + #define DW_IC_COMP_PARAM_1_SPEED_MODE_HIGH (BIT(2) | BIT(3)) 128 + #define DW_IC_COMP_PARAM_1_SPEED_MODE_MASK GENMASK(3, 2) 129 + 130 + /* 131 + * status codes 132 + */ 133 + #define STATUS_IDLE 0x0 134 + #define STATUS_WRITE_IN_PROGRESS 0x1 135 + #define STATUS_READ_IN_PROGRESS 0x2 136 + 137 + #define TIMEOUT 20 /* ms */ 138 + 139 + /* 140 + * operation modes 141 + */ 142 + #define DW_IC_MASTER 0 143 + #define DW_IC_SLAVE 1 144 + 145 + /* 146 + * Hardware abort codes from the DW_IC_TX_ABRT_SOURCE register 147 + * 148 + * Only expected abort codes are listed here 149 + * refer to the datasheet for the full list 150 + */ 151 + #define ABRT_7B_ADDR_NOACK 0 152 + #define ABRT_10ADDR1_NOACK 1 153 + #define ABRT_10ADDR2_NOACK 2 154 + #define ABRT_TXDATA_NOACK 3 155 + #define ABRT_GCALL_NOACK 4 156 + #define ABRT_GCALL_READ 5 157 + #define ABRT_SBYTE_ACKDET 7 158 + #define ABRT_SBYTE_NORSTRT 9 159 + #define ABRT_10B_RD_NORSTRT 10 160 + #define ABRT_MASTER_DIS 11 161 + #define ARB_LOST 12 162 + #define ABRT_SLAVE_FLUSH_TXFIFO 13 163 + #define ABRT_SLAVE_ARBLOST 14 164 + #define ABRT_SLAVE_RD_INTX 15 165 + 166 + #define DW_IC_TX_ABRT_7B_ADDR_NOACK (1UL << ABRT_7B_ADDR_NOACK) 167 + #define DW_IC_TX_ABRT_10ADDR1_NOACK (1UL << ABRT_10ADDR1_NOACK) 168 + #define DW_IC_TX_ABRT_10ADDR2_NOACK (1UL << ABRT_10ADDR2_NOACK) 169 + #define DW_IC_TX_ABRT_TXDATA_NOACK (1UL << ABRT_TXDATA_NOACK) 170 + #define DW_IC_TX_ABRT_GCALL_NOACK (1UL << ABRT_GCALL_NOACK) 171 + #define DW_IC_TX_ABRT_GCALL_READ (1UL << ABRT_GCALL_READ) 172 + #define DW_IC_TX_ABRT_SBYTE_ACKDET (1UL << ABRT_SBYTE_ACKDET) 173 + #define DW_IC_TX_ABRT_SBYTE_NORSTRT (1UL << ABRT_SBYTE_NORSTRT) 174 + #define DW_IC_TX_ABRT_10B_RD_NORSTRT (1UL << ABRT_10B_RD_NORSTRT) 175 + #define DW_IC_TX_ABRT_MASTER_DIS (1UL << ABRT_MASTER_DIS) 176 + #define DW_IC_TX_ARB_LOST (1UL << ARB_LOST) 177 + #define DW_IC_RX_ABRT_SLAVE_RD_INTX (1UL << ABRT_SLAVE_RD_INTX) 178 + #define DW_IC_RX_ABRT_SLAVE_ARBLOST (1UL << ABRT_SLAVE_ARBLOST) 179 + #define DW_IC_RX_ABRT_SLAVE_FLUSH_TXFIFO (1UL << ABRT_SLAVE_FLUSH_TXFIFO) 180 + 181 + #define DW_IC_TX_ABRT_NOACK (DW_IC_TX_ABRT_7B_ADDR_NOACK | \ 182 + DW_IC_TX_ABRT_10ADDR1_NOACK | \ 183 + DW_IC_TX_ABRT_10ADDR2_NOACK | \ 184 + DW_IC_TX_ABRT_TXDATA_NOACK | \ 185 + DW_IC_TX_ABRT_GCALL_NOACK) 43 186 44 187 45 188 /** ··· 191 48 * @base: IO registers pointer 192 49 * @cmd_complete: tx completion indicator 193 50 * @clk: input reference clock 51 + * @slave: represent an I2C slave device 194 52 * @cmd_err: run time hadware error code 195 - * @msgs: points to an array of messages currently being transfered 53 + * @msgs: points to an array of messages currently being transferred 196 54 * @msgs_num: the number of elements in msgs 197 55 * @msg_write_idx: the element index of the current tx message in the msgs 198 56 * array ··· 208 64 * @abort_source: copy of the TX_ABRT_SOURCE register 209 65 * @irq: interrupt number for the i2c master 210 66 * @adapter: i2c subsystem adapter node 67 + * @slave_cfg: configuration for the slave device 211 68 * @tx_fifo_depth: depth of the hardware tx fifo 212 69 * @rx_fifo_depth: depth of the hardware rx fifo 213 70 * @rx_outstanding: current master-rx elements in tx fifo ··· 225 80 * @acquire_lock: function to acquire a hardware lock on the bus 226 81 * @release_lock: function to release a hardware lock on the bus 227 82 * @pm_disabled: true if power-management should be disabled for this i2c-bus 83 + * @disable: function to disable the controller 84 + * @disable_int: function to disable all interrupts 85 + * @init: function to initialize the I2C hardware 86 + * @mode: operation mode - DW_IC_MASTER or DW_IC_SLAVE 228 87 * 229 88 * HCNT and LCNT parameters can be used if the platform knows more accurate 230 89 * values than the one computed based only on the input clock frequency. ··· 240 91 struct completion cmd_complete; 241 92 struct clk *clk; 242 93 struct reset_control *rst; 94 + struct i2c_client *slave; 243 95 u32 (*get_clk_rate_khz) (struct dw_i2c_dev *dev); 244 96 struct dw_pci_controller *controller; 245 97 int cmd_err; ··· 260 110 struct i2c_adapter adapter; 261 111 u32 functionality; 262 112 u32 master_cfg; 113 + u32 slave_cfg; 263 114 unsigned int tx_fifo_depth; 264 115 unsigned int rx_fifo_depth; 265 116 int rx_outstanding; ··· 280 129 int (*acquire_lock)(struct dw_i2c_dev *dev); 281 130 void (*release_lock)(struct dw_i2c_dev *dev); 282 131 bool pm_disabled; 132 + void (*disable)(struct dw_i2c_dev *dev); 133 + void (*disable_int)(struct dw_i2c_dev *dev); 134 + int (*init)(struct dw_i2c_dev *dev); 135 + int mode; 283 136 }; 284 137 285 138 #define ACCESS_SWAP 0x00000001 ··· 292 137 293 138 #define MODEL_CHERRYTRAIL 0x00000100 294 139 295 - extern int i2c_dw_init(struct dw_i2c_dev *dev); 296 - extern void i2c_dw_disable(struct dw_i2c_dev *dev); 297 - extern void i2c_dw_disable_int(struct dw_i2c_dev *dev); 140 + u32 dw_readl(struct dw_i2c_dev *dev, int offset); 141 + void dw_writel(struct dw_i2c_dev *dev, u32 b, int offset); 142 + u32 i2c_dw_scl_hcnt(u32 ic_clk, u32 tSYMBOL, u32 tf, int cond, int offset); 143 + u32 i2c_dw_scl_lcnt(u32 ic_clk, u32 tLOW, u32 tf, int offset); 144 + void __i2c_dw_enable(struct dw_i2c_dev *dev, bool enable); 145 + void __i2c_dw_enable_and_wait(struct dw_i2c_dev *dev, bool enable); 146 + unsigned long i2c_dw_clk_rate(struct dw_i2c_dev *dev); 147 + int i2c_dw_acquire_lock(struct dw_i2c_dev *dev); 148 + void i2c_dw_release_lock(struct dw_i2c_dev *dev); 149 + int i2c_dw_wait_bus_not_busy(struct dw_i2c_dev *dev); 150 + int i2c_dw_handle_tx_abort(struct dw_i2c_dev *dev); 151 + u32 i2c_dw_func(struct i2c_adapter *adap); 152 + void i2c_dw_disable(struct dw_i2c_dev *dev); 153 + void i2c_dw_disable_int(struct dw_i2c_dev *dev); 154 + 298 155 extern u32 i2c_dw_read_comp_param(struct dw_i2c_dev *dev); 299 156 extern int i2c_dw_probe(struct dw_i2c_dev *dev); 157 + #if IS_ENABLED(CONFIG_I2C_DESIGNWARE_SLAVE) 158 + extern int i2c_dw_probe_slave(struct dw_i2c_dev *dev); 159 + #else 160 + static inline int i2c_dw_probe_slave(struct dw_i2c_dev *dev) { return -EINVAL; } 161 + #endif 300 162 301 163 #if IS_ENABLED(CONFIG_I2C_DESIGNWARE_BAYTRAIL) 302 164 extern int i2c_dw_probe_lock_support(struct dw_i2c_dev *dev);
+6 -3
drivers/i2c/busses/i2c-designware-pcidrv.c
··· 187 187 static int i2c_dw_pci_suspend(struct device *dev) 188 188 { 189 189 struct pci_dev *pdev = to_pci_dev(dev); 190 + struct dw_i2c_dev *i_dev = pci_get_drvdata(pdev); 190 191 191 - i2c_dw_disable(pci_get_drvdata(pdev)); 192 + i_dev->disable(i_dev); 193 + 192 194 return 0; 193 195 } 194 196 195 197 static int i2c_dw_pci_resume(struct device *dev) 196 198 { 197 199 struct pci_dev *pdev = to_pci_dev(dev); 200 + struct dw_i2c_dev *i_dev = pci_get_drvdata(pdev); 198 201 199 - return i2c_dw_init(pci_get_drvdata(pdev)); 202 + return i_dev->init(i_dev); 200 203 } 201 204 #endif 202 205 ··· 299 296 { 300 297 struct dw_i2c_dev *dev = pci_get_drvdata(pdev); 301 298 302 - i2c_dw_disable(dev); 299 + dev->disable(dev); 303 300 pm_runtime_forbid(&pdev->dev); 304 301 pm_runtime_get_noresume(&pdev->dev); 305 302
+77 -40
drivers/i2c/busses/i2c-designware-platdrv.c
··· 1 1 /* 2 - * Synopsys DesignWare I2C adapter driver (master only). 2 + * Synopsys DesignWare I2C adapter driver. 3 3 * 4 4 * Based on the TI DAVINCI I2C adapter driver. 5 5 * ··· 21 21 * ---------------------------------------------------------------------------- 22 22 * 23 23 */ 24 - #include <linux/kernel.h> 25 - #include <linux/module.h> 24 + #include <linux/acpi.h> 25 + #include <linux/clk-provider.h> 26 + #include <linux/clk.h> 26 27 #include <linux/delay.h> 27 28 #include <linux/dmi.h> 28 - #include <linux/i2c.h> 29 - #include <linux/clk.h> 30 - #include <linux/clk-provider.h> 31 - #include <linux/errno.h> 32 - #include <linux/sched.h> 33 29 #include <linux/err.h> 30 + #include <linux/errno.h> 31 + #include <linux/i2c.h> 34 32 #include <linux/interrupt.h> 33 + #include <linux/io.h> 34 + #include <linux/kernel.h> 35 + #include <linux/module.h> 35 36 #include <linux/of.h> 37 + #include <linux/platform_data/i2c-designware.h> 36 38 #include <linux/platform_device.h> 37 39 #include <linux/pm.h> 38 40 #include <linux/pm_runtime.h> 39 41 #include <linux/property.h> 40 - #include <linux/io.h> 41 42 #include <linux/reset.h> 43 + #include <linux/sched.h> 42 44 #include <linux/slab.h> 43 - #include <linux/acpi.h> 44 - #include <linux/platform_data/i2c-designware.h> 45 + 45 46 #include "i2c-designware-core.h" 46 47 47 48 static u32 i2c_dw_get_clk_rate_khz(struct dw_i2c_dev *dev) ··· 172 171 } 173 172 #endif 174 173 174 + static void i2c_dw_configure_master(struct dw_i2c_dev *dev) 175 + { 176 + dev->functionality = I2C_FUNC_10BIT_ADDR | DW_IC_DEFAULT_FUNCTIONALITY; 177 + 178 + dev->master_cfg = DW_IC_CON_MASTER | DW_IC_CON_SLAVE_DISABLE | 179 + DW_IC_CON_RESTART_EN; 180 + 181 + dev->mode = DW_IC_MASTER; 182 + 183 + switch (dev->clk_freq) { 184 + case 100000: 185 + dev->master_cfg |= DW_IC_CON_SPEED_STD; 186 + break; 187 + case 3400000: 188 + dev->master_cfg |= DW_IC_CON_SPEED_HIGH; 189 + break; 190 + default: 191 + dev->master_cfg |= DW_IC_CON_SPEED_FAST; 192 + } 193 + } 194 + 195 + static void i2c_dw_configure_slave(struct dw_i2c_dev *dev) 196 + { 197 + dev->functionality = I2C_FUNC_SLAVE | DW_IC_DEFAULT_FUNCTIONALITY; 198 + 199 + dev->slave_cfg = DW_IC_CON_RX_FIFO_FULL_HLD_CTRL | 200 + DW_IC_CON_RESTART_EN | DW_IC_CON_STOP_DET_IFADDRESSED | 201 + DW_IC_CON_SPEED_FAST; 202 + 203 + dev->mode = DW_IC_SLAVE; 204 + 205 + switch (dev->clk_freq) { 206 + case 100000: 207 + dev->slave_cfg |= DW_IC_CON_SPEED_STD; 208 + break; 209 + case 3400000: 210 + dev->slave_cfg |= DW_IC_CON_SPEED_HIGH; 211 + break; 212 + default: 213 + dev->slave_cfg |= DW_IC_CON_SPEED_FAST; 214 + } 215 + } 216 + 175 217 static int i2c_dw_plat_prepare_clk(struct dw_i2c_dev *i_dev, bool prepare) 176 218 { 177 219 if (IS_ERR(i_dev->clk)) ··· 253 209 static int dw_i2c_plat_probe(struct platform_device *pdev) 254 210 { 255 211 struct dw_i2c_platform_data *pdata = dev_get_platdata(&pdev->dev); 256 - struct dw_i2c_dev *dev; 257 212 struct i2c_adapter *adap; 258 - struct resource *mem; 259 - int irq, r; 213 + struct dw_i2c_dev *dev; 260 214 u32 acpi_speed, ht = 0; 215 + struct resource *mem; 216 + int irq, ret; 261 217 262 218 irq = platform_get_irq(pdev, 0); 263 219 if (irq < 0) ··· 320 276 && dev->clk_freq != 1000000 && dev->clk_freq != 3400000) { 321 277 dev_err(&pdev->dev, 322 278 "Only 100kHz, 400kHz, 1MHz and 3.4MHz supported"); 323 - r = -EINVAL; 279 + ret = -EINVAL; 324 280 goto exit_reset; 325 281 } 326 282 327 - r = i2c_dw_probe_lock_support(dev); 328 - if (r) 283 + ret = i2c_dw_probe_lock_support(dev); 284 + if (ret) 329 285 goto exit_reset; 330 286 331 - dev->functionality = I2C_FUNC_10BIT_ADDR | DW_IC_DEFAULT_FUNCTIONALITY; 332 - 333 - dev->master_cfg = DW_IC_CON_MASTER | DW_IC_CON_SLAVE_DISABLE | 334 - DW_IC_CON_RESTART_EN; 335 - 336 - switch (dev->clk_freq) { 337 - case 100000: 338 - dev->master_cfg |= DW_IC_CON_SPEED_STD; 339 - break; 340 - case 3400000: 341 - dev->master_cfg |= DW_IC_CON_SPEED_HIGH; 342 - break; 343 - default: 344 - dev->master_cfg |= DW_IC_CON_SPEED_FAST; 345 - } 287 + if (i2c_detect_slave_mode(&pdev->dev)) 288 + i2c_dw_configure_slave(dev); 289 + else 290 + i2c_dw_configure_master(dev); 346 291 347 292 dev->clk = devm_clk_get(&pdev->dev, NULL); 348 293 if (!i2c_dw_plat_prepare_clk(dev, true)) { ··· 360 327 pm_runtime_enable(&pdev->dev); 361 328 } 362 329 363 - r = i2c_dw_probe(dev); 364 - if (r) 330 + if (dev->mode == DW_IC_SLAVE) 331 + ret = i2c_dw_probe_slave(dev); 332 + else 333 + ret = i2c_dw_probe(dev); 334 + 335 + if (ret) 365 336 goto exit_probe; 366 337 367 - return r; 338 + return ret; 368 339 369 340 exit_probe: 370 341 if (!dev->pm_disabled) ··· 376 339 exit_reset: 377 340 if (!IS_ERR_OR_NULL(dev->rst)) 378 341 reset_control_assert(dev->rst); 379 - return r; 342 + return ret; 380 343 } 381 344 382 345 static int dw_i2c_plat_remove(struct platform_device *pdev) ··· 387 350 388 351 i2c_del_adapter(&dev->adapter); 389 352 390 - i2c_dw_disable(dev); 353 + dev->disable(dev); 391 354 392 355 pm_runtime_dont_use_autosuspend(&pdev->dev); 393 356 pm_runtime_put_sync(&pdev->dev); ··· 431 394 struct platform_device *pdev = to_platform_device(dev); 432 395 struct dw_i2c_dev *i_dev = platform_get_drvdata(pdev); 433 396 434 - i2c_dw_disable(i_dev); 397 + i_dev->disable(i_dev); 435 398 i2c_dw_plat_prepare_clk(i_dev, false); 436 399 437 400 return 0; ··· 443 406 struct dw_i2c_dev *i_dev = platform_get_drvdata(pdev); 444 407 445 408 i2c_dw_plat_prepare_clk(i_dev, true); 446 - i2c_dw_init(i_dev); 409 + i_dev->init(i_dev); 447 410 448 411 return 0; 449 412 } ··· 460 423 #define DW_I2C_DEV_PMOPS NULL 461 424 #endif 462 425 463 - /* work with hotplug and coldplug */ 426 + /* Work with hotplug and coldplug */ 464 427 MODULE_ALIAS("platform:i2c_designware"); 465 428 466 429 static struct platform_driver dw_i2c_driver = {
+393
drivers/i2c/busses/i2c-designware-slave.c
··· 1 + /* 2 + * Synopsys DesignWare I2C adapter driver (slave only). 3 + * 4 + * Based on the Synopsys DesignWare I2C adapter driver (master). 5 + * 6 + * Copyright (C) 2016 Synopsys Inc. 7 + * 8 + * ---------------------------------------------------------------------------- 9 + * 10 + * This program is free software; you can redistribute it and/or modify 11 + * it under the terms of the GNU General Public License as published by 12 + * the Free Software Foundation; either version 2 of the License, or 13 + * (at your option) any later version. 14 + * 15 + * This program is distributed in the hope that it will be useful, 16 + * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 + * GNU General Public License for more details. 19 + * ---------------------------------------------------------------------------- 20 + * 21 + */ 22 + #include <linux/delay.h> 23 + #include <linux/err.h> 24 + #include <linux/errno.h> 25 + #include <linux/i2c.h> 26 + #include <linux/interrupt.h> 27 + #include <linux/io.h> 28 + #include <linux/module.h> 29 + #include <linux/pm_runtime.h> 30 + 31 + #include "i2c-designware-core.h" 32 + 33 + static void i2c_dw_configure_fifo_slave(struct dw_i2c_dev *dev) 34 + { 35 + /* Configure Tx/Rx FIFO threshold levels. */ 36 + dw_writel(dev, 0, DW_IC_TX_TL); 37 + dw_writel(dev, 0, DW_IC_RX_TL); 38 + 39 + /* Configure the I2C slave. */ 40 + dw_writel(dev, dev->slave_cfg, DW_IC_CON); 41 + dw_writel(dev, DW_IC_INTR_SLAVE_MASK, DW_IC_INTR_MASK); 42 + } 43 + 44 + /** 45 + * i2c_dw_init_slave() - Initialize the designware i2c slave hardware 46 + * @dev: device private data 47 + * 48 + * This function configures and enables the I2C in slave mode. 49 + * This function is called during I2C init function, and in case of timeout at 50 + * run time. 51 + */ 52 + static int i2c_dw_init_slave(struct dw_i2c_dev *dev) 53 + { 54 + u32 sda_falling_time, scl_falling_time; 55 + u32 reg, comp_param1; 56 + u32 hcnt, lcnt; 57 + int ret; 58 + 59 + ret = i2c_dw_acquire_lock(dev); 60 + if (ret) 61 + return ret; 62 + 63 + reg = dw_readl(dev, DW_IC_COMP_TYPE); 64 + if (reg == ___constant_swab32(DW_IC_COMP_TYPE_VALUE)) { 65 + /* Configure register endianness access. */ 66 + dev->flags |= ACCESS_SWAP; 67 + } else if (reg == (DW_IC_COMP_TYPE_VALUE & 0x0000ffff)) { 68 + /* Configure register access mode 16bit. */ 69 + dev->flags |= ACCESS_16BIT; 70 + } else if (reg != DW_IC_COMP_TYPE_VALUE) { 71 + dev_err(dev->dev, 72 + "Unknown Synopsys component type: 0x%08x\n", reg); 73 + i2c_dw_release_lock(dev); 74 + return -ENODEV; 75 + } 76 + 77 + comp_param1 = dw_readl(dev, DW_IC_COMP_PARAM_1); 78 + 79 + /* Disable the adapter. */ 80 + __i2c_dw_enable_and_wait(dev, false); 81 + 82 + /* Set standard and fast speed deviders for high/low periods. */ 83 + sda_falling_time = dev->sda_falling_time ?: 300; /* ns */ 84 + scl_falling_time = dev->scl_falling_time ?: 300; /* ns */ 85 + 86 + /* Set SCL timing parameters for standard-mode. */ 87 + if (dev->ss_hcnt && dev->ss_lcnt) { 88 + hcnt = dev->ss_hcnt; 89 + lcnt = dev->ss_lcnt; 90 + } else { 91 + hcnt = i2c_dw_scl_hcnt(i2c_dw_clk_rate(dev), 92 + 4000, /* tHD;STA = tHIGH = 4.0 us */ 93 + sda_falling_time, 94 + 0, /* 0: DW default, 1: Ideal */ 95 + 0); /* No offset */ 96 + lcnt = i2c_dw_scl_lcnt(i2c_dw_clk_rate(dev), 97 + 4700, /* tLOW = 4.7 us */ 98 + scl_falling_time, 99 + 0); /* No offset */ 100 + } 101 + dw_writel(dev, hcnt, DW_IC_SS_SCL_HCNT); 102 + dw_writel(dev, lcnt, DW_IC_SS_SCL_LCNT); 103 + dev_dbg(dev->dev, "Standard-mode HCNT:LCNT = %d:%d\n", hcnt, lcnt); 104 + 105 + /* Set SCL timing parameters for fast-mode or fast-mode plus. */ 106 + if ((dev->clk_freq == 1000000) && dev->fp_hcnt && dev->fp_lcnt) { 107 + hcnt = dev->fp_hcnt; 108 + lcnt = dev->fp_lcnt; 109 + } else if (dev->fs_hcnt && dev->fs_lcnt) { 110 + hcnt = dev->fs_hcnt; 111 + lcnt = dev->fs_lcnt; 112 + } else { 113 + hcnt = i2c_dw_scl_hcnt(i2c_dw_clk_rate(dev), 114 + 600, /* tHD;STA = tHIGH = 0.6 us */ 115 + sda_falling_time, 116 + 0, /* 0: DW default, 1: Ideal */ 117 + 0); /* No offset */ 118 + lcnt = i2c_dw_scl_lcnt(i2c_dw_clk_rate(dev), 119 + 1300, /* tLOW = 1.3 us */ 120 + scl_falling_time, 121 + 0); /* No offset */ 122 + } 123 + dw_writel(dev, hcnt, DW_IC_FS_SCL_HCNT); 124 + dw_writel(dev, lcnt, DW_IC_FS_SCL_LCNT); 125 + dev_dbg(dev->dev, "Fast-mode HCNT:LCNT = %d:%d\n", hcnt, lcnt); 126 + 127 + if ((dev->slave_cfg & DW_IC_CON_SPEED_MASK) == 128 + DW_IC_CON_SPEED_HIGH) { 129 + if ((comp_param1 & DW_IC_COMP_PARAM_1_SPEED_MODE_MASK) 130 + != DW_IC_COMP_PARAM_1_SPEED_MODE_HIGH) { 131 + dev_err(dev->dev, "High Speed not supported!\n"); 132 + dev->slave_cfg &= ~DW_IC_CON_SPEED_MASK; 133 + dev->slave_cfg |= DW_IC_CON_SPEED_FAST; 134 + } else if (dev->hs_hcnt && dev->hs_lcnt) { 135 + hcnt = dev->hs_hcnt; 136 + lcnt = dev->hs_lcnt; 137 + dw_writel(dev, hcnt, DW_IC_HS_SCL_HCNT); 138 + dw_writel(dev, lcnt, DW_IC_HS_SCL_LCNT); 139 + dev_dbg(dev->dev, "HighSpeed-mode HCNT:LCNT = %d:%d\n", 140 + hcnt, lcnt); 141 + } 142 + } 143 + 144 + /* Configure SDA Hold Time if required. */ 145 + reg = dw_readl(dev, DW_IC_COMP_VERSION); 146 + if (reg >= DW_IC_SDA_HOLD_MIN_VERS) { 147 + if (!dev->sda_hold_time) { 148 + /* Keep previous hold time setting if no one set it. */ 149 + dev->sda_hold_time = dw_readl(dev, DW_IC_SDA_HOLD); 150 + } 151 + /* 152 + * Workaround for avoiding TX arbitration lost in case I2C 153 + * slave pulls SDA down "too quickly" after falling egde of 154 + * SCL by enabling non-zero SDA RX hold. Specification says it 155 + * extends incoming SDA low to high transition while SCL is 156 + * high but it apprears to help also above issue. 157 + */ 158 + if (!(dev->sda_hold_time & DW_IC_SDA_HOLD_RX_MASK)) 159 + dev->sda_hold_time |= 1 << DW_IC_SDA_HOLD_RX_SHIFT; 160 + dw_writel(dev, dev->sda_hold_time, DW_IC_SDA_HOLD); 161 + } else { 162 + dev_warn(dev->dev, 163 + "Hardware too old to adjust SDA hold time.\n"); 164 + } 165 + 166 + i2c_dw_configure_fifo_slave(dev); 167 + i2c_dw_release_lock(dev); 168 + 169 + return 0; 170 + } 171 + 172 + static int i2c_dw_reg_slave(struct i2c_client *slave) 173 + { 174 + struct dw_i2c_dev *dev = i2c_get_adapdata(slave->adapter); 175 + 176 + if (dev->slave) 177 + return -EBUSY; 178 + if (slave->flags & I2C_CLIENT_TEN) 179 + return -EAFNOSUPPORT; 180 + /* 181 + * Set slave address in the IC_SAR register, 182 + * the address to which the DW_apb_i2c responds. 183 + */ 184 + __i2c_dw_enable(dev, false); 185 + dw_writel(dev, slave->addr, DW_IC_SAR); 186 + dev->slave = slave; 187 + 188 + __i2c_dw_enable(dev, true); 189 + 190 + dev->cmd_err = 0; 191 + dev->msg_write_idx = 0; 192 + dev->msg_read_idx = 0; 193 + dev->msg_err = 0; 194 + dev->status = STATUS_IDLE; 195 + dev->abort_source = 0; 196 + dev->rx_outstanding = 0; 197 + 198 + return 0; 199 + } 200 + 201 + static int i2c_dw_unreg_slave(struct i2c_client *slave) 202 + { 203 + struct dw_i2c_dev *dev = i2c_get_adapdata(slave->adapter); 204 + 205 + dev->disable_int(dev); 206 + dev->disable(dev); 207 + dev->slave = NULL; 208 + 209 + return 0; 210 + } 211 + 212 + static u32 i2c_dw_read_clear_intrbits_slave(struct dw_i2c_dev *dev) 213 + { 214 + u32 stat; 215 + 216 + /* 217 + * The IC_INTR_STAT register just indicates "enabled" interrupts. 218 + * Ths unmasked raw version of interrupt status bits are available 219 + * in the IC_RAW_INTR_STAT register. 220 + * 221 + * That is, 222 + * stat = dw_readl(IC_INTR_STAT); 223 + * equals to, 224 + * stat = dw_readl(IC_RAW_INTR_STAT) & dw_readl(IC_INTR_MASK); 225 + * 226 + * The raw version might be useful for debugging purposes. 227 + */ 228 + stat = dw_readl(dev, DW_IC_INTR_STAT); 229 + 230 + /* 231 + * Do not use the IC_CLR_INTR register to clear interrupts, or 232 + * you'll miss some interrupts, triggered during the period from 233 + * dw_readl(IC_INTR_STAT) to dw_readl(IC_CLR_INTR). 234 + * 235 + * Instead, use the separately-prepared IC_CLR_* registers. 236 + */ 237 + if (stat & DW_IC_INTR_TX_ABRT) 238 + dw_readl(dev, DW_IC_CLR_TX_ABRT); 239 + if (stat & DW_IC_INTR_RX_UNDER) 240 + dw_readl(dev, DW_IC_CLR_RX_UNDER); 241 + if (stat & DW_IC_INTR_RX_OVER) 242 + dw_readl(dev, DW_IC_CLR_RX_OVER); 243 + if (stat & DW_IC_INTR_TX_OVER) 244 + dw_readl(dev, DW_IC_CLR_TX_OVER); 245 + if (stat & DW_IC_INTR_RX_DONE) 246 + dw_readl(dev, DW_IC_CLR_RX_DONE); 247 + if (stat & DW_IC_INTR_ACTIVITY) 248 + dw_readl(dev, DW_IC_CLR_ACTIVITY); 249 + if (stat & DW_IC_INTR_STOP_DET) 250 + dw_readl(dev, DW_IC_CLR_STOP_DET); 251 + if (stat & DW_IC_INTR_START_DET) 252 + dw_readl(dev, DW_IC_CLR_START_DET); 253 + if (stat & DW_IC_INTR_GEN_CALL) 254 + dw_readl(dev, DW_IC_CLR_GEN_CALL); 255 + 256 + return stat; 257 + } 258 + 259 + /* 260 + * Interrupt service routine. This gets called whenever an I2C slave interrupt 261 + * occurs. 262 + */ 263 + 264 + static int i2c_dw_irq_handler_slave(struct dw_i2c_dev *dev) 265 + { 266 + u32 raw_stat, stat, enabled; 267 + u8 val, slave_activity; 268 + 269 + stat = dw_readl(dev, DW_IC_INTR_STAT); 270 + enabled = dw_readl(dev, DW_IC_ENABLE); 271 + raw_stat = dw_readl(dev, DW_IC_RAW_INTR_STAT); 272 + slave_activity = ((dw_readl(dev, DW_IC_STATUS) & 273 + DW_IC_STATUS_SLAVE_ACTIVITY) >> 6); 274 + 275 + if (!enabled || !(raw_stat & ~DW_IC_INTR_ACTIVITY)) 276 + return 0; 277 + 278 + dev_dbg(dev->dev, 279 + "%#x STATUS SLAVE_ACTIVITY=%#x : RAW_INTR_STAT=%#x : INTR_STAT=%#x\n", 280 + enabled, slave_activity, raw_stat, stat); 281 + 282 + if ((stat & DW_IC_INTR_RX_FULL) && (stat & DW_IC_INTR_STOP_DET)) 283 + i2c_slave_event(dev->slave, I2C_SLAVE_WRITE_REQUESTED, &val); 284 + 285 + if (stat & DW_IC_INTR_RD_REQ) { 286 + if (slave_activity) { 287 + if (stat & DW_IC_INTR_RX_FULL) { 288 + val = dw_readl(dev, DW_IC_DATA_CMD); 289 + 290 + if (!i2c_slave_event(dev->slave, 291 + I2C_SLAVE_WRITE_RECEIVED, 292 + &val)) { 293 + dev_vdbg(dev->dev, "Byte %X acked!", 294 + val); 295 + } 296 + dw_readl(dev, DW_IC_CLR_RD_REQ); 297 + stat = i2c_dw_read_clear_intrbits_slave(dev); 298 + } else { 299 + dw_readl(dev, DW_IC_CLR_RD_REQ); 300 + dw_readl(dev, DW_IC_CLR_RX_UNDER); 301 + stat = i2c_dw_read_clear_intrbits_slave(dev); 302 + } 303 + if (!i2c_slave_event(dev->slave, 304 + I2C_SLAVE_READ_REQUESTED, 305 + &val)) 306 + dw_writel(dev, val, DW_IC_DATA_CMD); 307 + } 308 + } 309 + 310 + if (stat & DW_IC_INTR_RX_DONE) { 311 + if (!i2c_slave_event(dev->slave, I2C_SLAVE_READ_PROCESSED, 312 + &val)) 313 + dw_readl(dev, DW_IC_CLR_RX_DONE); 314 + 315 + i2c_slave_event(dev->slave, I2C_SLAVE_STOP, &val); 316 + stat = i2c_dw_read_clear_intrbits_slave(dev); 317 + return 1; 318 + } 319 + 320 + if (stat & DW_IC_INTR_RX_FULL) { 321 + val = dw_readl(dev, DW_IC_DATA_CMD); 322 + if (!i2c_slave_event(dev->slave, I2C_SLAVE_WRITE_RECEIVED, 323 + &val)) 324 + dev_vdbg(dev->dev, "Byte %X acked!", val); 325 + } else { 326 + i2c_slave_event(dev->slave, I2C_SLAVE_STOP, &val); 327 + stat = i2c_dw_read_clear_intrbits_slave(dev); 328 + } 329 + 330 + return 1; 331 + } 332 + 333 + static irqreturn_t i2c_dw_isr_slave(int this_irq, void *dev_id) 334 + { 335 + struct dw_i2c_dev *dev = dev_id; 336 + int ret; 337 + 338 + i2c_dw_read_clear_intrbits_slave(dev); 339 + ret = i2c_dw_irq_handler_slave(dev); 340 + if (ret > 0) 341 + complete(&dev->cmd_complete); 342 + 343 + return IRQ_RETVAL(ret); 344 + } 345 + 346 + static struct i2c_algorithm i2c_dw_algo = { 347 + .functionality = i2c_dw_func, 348 + .reg_slave = i2c_dw_reg_slave, 349 + .unreg_slave = i2c_dw_unreg_slave, 350 + }; 351 + 352 + int i2c_dw_probe_slave(struct dw_i2c_dev *dev) 353 + { 354 + struct i2c_adapter *adap = &dev->adapter; 355 + int ret; 356 + 357 + init_completion(&dev->cmd_complete); 358 + 359 + dev->init = i2c_dw_init_slave; 360 + dev->disable = i2c_dw_disable; 361 + dev->disable_int = i2c_dw_disable_int; 362 + 363 + ret = dev->init(dev); 364 + if (ret) 365 + return ret; 366 + 367 + snprintf(adap->name, sizeof(adap->name), 368 + "Synopsys DesignWare I2C Slave adapter"); 369 + adap->retries = 3; 370 + adap->algo = &i2c_dw_algo; 371 + adap->dev.parent = dev->dev; 372 + i2c_set_adapdata(adap, dev); 373 + 374 + ret = devm_request_irq(dev->dev, dev->irq, i2c_dw_isr_slave, 375 + IRQF_SHARED, dev_name(dev->dev), dev); 376 + if (ret) { 377 + dev_err(dev->dev, "failure requesting irq %i: %d\n", 378 + dev->irq, ret); 379 + return ret; 380 + } 381 + 382 + ret = i2c_add_numbered_adapter(adap); 383 + if (ret) 384 + dev_err(dev->dev, "failure adding adapter: %d\n", ret); 385 + pm_runtime_put_noidle(dev->dev); 386 + 387 + return ret; 388 + } 389 + EXPORT_SYMBOL_GPL(i2c_dw_probe_slave); 390 + 391 + MODULE_AUTHOR("Luis Oliveira <lolivei@synopsys.com>"); 392 + MODULE_DESCRIPTION("Synopsys DesignWare I2C bus slave adapter"); 393 + MODULE_LICENSE("GPL v2");
+3 -1
drivers/i2c/busses/i2c-emev2.c
··· 375 375 if (IS_ERR(priv->sclk)) 376 376 return PTR_ERR(priv->sclk); 377 377 378 - clk_prepare_enable(priv->sclk); 378 + ret = clk_prepare_enable(priv->sclk); 379 + if (ret) 380 + return ret; 379 381 380 382 priv->adap.timeout = msecs_to_jiffies(100); 381 383 priv->adap.retries = 5;
+8
drivers/i2c/busses/i2c-i801.c
··· 66 66 * Lewisburg Supersku (PCH) 0xa223 32 hard yes yes yes 67 67 * Kaby Lake PCH-H (PCH) 0xa2a3 32 hard yes yes yes 68 68 * Gemini Lake (SOC) 0x31d4 32 hard yes yes yes 69 + * Cannon Lake-H (PCH) 0xa323 32 hard yes yes yes 70 + * Cannon Lake-LP (PCH) 0x9da3 32 hard yes yes yes 69 71 * 70 72 * Features supported by this driver: 71 73 * Software PEC no ··· 228 226 #define PCI_DEVICE_ID_INTEL_LYNXPOINT_LP_SMBUS 0x9c22 229 227 #define PCI_DEVICE_ID_INTEL_WILDCATPOINT_LP_SMBUS 0x9ca2 230 228 #define PCI_DEVICE_ID_INTEL_SUNRISEPOINT_LP_SMBUS 0x9d23 229 + #define PCI_DEVICE_ID_INTEL_CANNONLAKE_LP_SMBUS 0x9da3 231 230 #define PCI_DEVICE_ID_INTEL_SUNRISEPOINT_H_SMBUS 0xa123 232 231 #define PCI_DEVICE_ID_INTEL_LEWISBURG_SMBUS 0xa1a3 233 232 #define PCI_DEVICE_ID_INTEL_LEWISBURG_SSKU_SMBUS 0xa223 234 233 #define PCI_DEVICE_ID_INTEL_KABYLAKE_PCH_H_SMBUS 0xa2a3 234 + #define PCI_DEVICE_ID_INTEL_CANNONLAKE_H_SMBUS 0xa323 235 235 236 236 struct i801_mux_config { 237 237 char *gpio_chip; ··· 1030 1026 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_LEWISBURG_SMBUS) }, 1031 1027 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_LEWISBURG_SSKU_SMBUS) }, 1032 1028 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_KABYLAKE_PCH_H_SMBUS) }, 1029 + { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_CANNONLAKE_H_SMBUS) }, 1030 + { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_CANNONLAKE_LP_SMBUS) }, 1033 1031 { 0, } 1034 1032 }; 1035 1033 ··· 1505 1499 switch (dev->device) { 1506 1500 case PCI_DEVICE_ID_INTEL_SUNRISEPOINT_H_SMBUS: 1507 1501 case PCI_DEVICE_ID_INTEL_SUNRISEPOINT_LP_SMBUS: 1502 + case PCI_DEVICE_ID_INTEL_CANNONLAKE_H_SMBUS: 1503 + case PCI_DEVICE_ID_INTEL_CANNONLAKE_LP_SMBUS: 1508 1504 case PCI_DEVICE_ID_INTEL_LEWISBURG_SMBUS: 1509 1505 case PCI_DEVICE_ID_INTEL_LEWISBURG_SSKU_SMBUS: 1510 1506 case PCI_DEVICE_ID_INTEL_DNV_SMBUS:
+3 -3
drivers/i2c/busses/i2c-mxs.c
··· 419 419 420 420 ret = mxs_i2c_pio_wait_xfer_end(i2c); 421 421 if (ret) { 422 - dev_err(i2c->dev, 422 + dev_dbg(i2c->dev, 423 423 "PIO: Failed to send SELECT command!\n"); 424 424 goto cleanup; 425 425 } ··· 431 431 432 432 ret = mxs_i2c_pio_wait_xfer_end(i2c); 433 433 if (ret) { 434 - dev_err(i2c->dev, 434 + dev_dbg(i2c->dev, 435 435 "PIO: Failed to send READ command!\n"); 436 436 goto cleanup; 437 437 } ··· 528 528 /* Wait for the end of the transfer. */ 529 529 ret = mxs_i2c_pio_wait_xfer_end(i2c); 530 530 if (ret) { 531 - dev_err(i2c->dev, 531 + dev_dbg(i2c->dev, 532 532 "PIO: Failed to finish WRITE cmd!\n"); 533 533 break; 534 534 }
+60 -73
drivers/i2c/busses/i2c-pca-platform.c
··· 22 22 #include <linux/i2c-algo-pca.h> 23 23 #include <linux/i2c-pca-platform.h> 24 24 #include <linux/gpio.h> 25 + #include <linux/gpio/consumer.h> 25 26 #include <linux/io.h> 27 + #include <linux/of.h> 28 + #include <linux/of_device.h> 26 29 27 30 #include <asm/irq.h> 28 31 29 32 struct i2c_pca_pf_data { 30 33 void __iomem *reg_base; 31 34 int irq; /* if 0, use polling */ 32 - int gpio; 35 + struct gpio_desc *gpio; 33 36 wait_queue_head_t wait; 34 37 struct i2c_adapter adap; 35 38 struct i2c_algo_pca_data algo_data; ··· 107 104 static void i2c_pca_pf_dummyreset(void *pd) 108 105 { 109 106 struct i2c_pca_pf_data *i2c = pd; 110 - printk(KERN_WARNING "%s: No reset-pin found. Chip may get stuck!\n", 111 - i2c->adap.name); 107 + 108 + dev_warn(&i2c->adap.dev, "No reset-pin found. Chip may get stuck!\n"); 112 109 } 113 110 114 111 static void i2c_pca_pf_resetchip(void *pd) 115 112 { 116 113 struct i2c_pca_pf_data *i2c = pd; 117 114 118 - gpio_set_value(i2c->gpio, 0); 115 + gpiod_set_value(i2c->gpio, 1); 119 116 ndelay(100); 120 - gpio_set_value(i2c->gpio, 1); 117 + gpiod_set_value(i2c->gpio, 0); 121 118 } 122 119 123 120 static irqreturn_t i2c_pca_pf_handler(int this_irq, void *dev_id) ··· 139 136 struct resource *res; 140 137 struct i2c_pca9564_pf_platform_data *platform_data = 141 138 dev_get_platdata(&pdev->dev); 139 + struct device_node *np = pdev->dev.of_node; 142 140 int ret = 0; 143 141 int irq; 144 142 145 - res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 146 143 irq = platform_get_irq(pdev, 0); 147 144 /* If irq is 0, we do polling. */ 145 + if (irq < 0) 146 + irq = 0; 148 147 149 - if (res == NULL) { 150 - ret = -ENODEV; 151 - goto e_print; 152 - } 148 + i2c = devm_kzalloc(&pdev->dev, sizeof(*i2c), GFP_KERNEL); 149 + if (!i2c) 150 + return -ENOMEM; 153 151 154 - if (!request_mem_region(res->start, resource_size(res), res->name)) { 155 - ret = -ENOMEM; 156 - goto e_print; 157 - } 152 + res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 153 + i2c->reg_base = devm_ioremap_resource(&pdev->dev, res); 154 + if (IS_ERR(i2c->reg_base)) 155 + return PTR_ERR(i2c->reg_base); 158 156 159 - i2c = kzalloc(sizeof(struct i2c_pca_pf_data), GFP_KERNEL); 160 - if (!i2c) { 161 - ret = -ENOMEM; 162 - goto e_alloc; 163 - } 164 157 165 158 init_waitqueue_head(&i2c->wait); 166 159 167 - i2c->reg_base = ioremap(res->start, resource_size(res)); 168 - if (!i2c->reg_base) { 169 - ret = -ENOMEM; 170 - goto e_remap; 171 - } 172 160 i2c->io_base = res->start; 173 161 i2c->io_size = resource_size(res); 174 162 i2c->irq = irq; ··· 171 177 (unsigned long) res->start); 172 178 i2c->adap.algo_data = &i2c->algo_data; 173 179 i2c->adap.dev.parent = &pdev->dev; 180 + i2c->adap.dev.of_node = np; 174 181 175 182 if (platform_data) { 176 183 i2c->adap.timeout = platform_data->timeout; 177 184 i2c->algo_data.i2c_clock = platform_data->i2c_clock_speed; 178 - i2c->gpio = platform_data->gpio; 185 + if (gpio_is_valid(platform_data->gpio)) { 186 + ret = devm_gpio_request_one(&pdev->dev, 187 + platform_data->gpio, 188 + GPIOF_ACTIVE_LOW, 189 + i2c->adap.name); 190 + if (ret == 0) { 191 + i2c->gpio = gpio_to_desc(platform_data->gpio); 192 + gpiod_direction_output(i2c->gpio, 0); 193 + } else { 194 + dev_warn(&pdev->dev, "Registering gpio failed!\n"); 195 + i2c->gpio = NULL; 196 + } 197 + } 198 + } else if (np) { 199 + i2c->adap.timeout = HZ; 200 + i2c->gpio = devm_gpiod_get_optional(&pdev->dev, "reset-gpios", GPIOD_OUT_LOW); 201 + if (IS_ERR(i2c->gpio)) 202 + return PTR_ERR(i2c->gpio); 203 + of_property_read_u32_index(np, "clock-frequency", 0, 204 + &i2c->algo_data.i2c_clock); 179 205 } else { 180 206 i2c->adap.timeout = HZ; 181 207 i2c->algo_data.i2c_clock = 59000; 182 - i2c->gpio = -1; 208 + i2c->gpio = NULL; 183 209 } 184 210 185 211 i2c->algo_data.data = i2c; 186 212 i2c->algo_data.wait_for_completion = i2c_pca_pf_waitforcompletion; 187 - i2c->algo_data.reset_chip = i2c_pca_pf_dummyreset; 213 + if (i2c->gpio) 214 + i2c->algo_data.reset_chip = i2c_pca_pf_resetchip; 215 + else 216 + i2c->algo_data.reset_chip = i2c_pca_pf_dummyreset; 188 217 189 218 switch (res->flags & IORESOURCE_MEM_TYPE_MASK) { 190 219 case IORESOURCE_MEM_32BIT: ··· 225 208 break; 226 209 } 227 210 228 - /* Use gpio_is_valid() when in mainline */ 229 - if (i2c->gpio > -1) { 230 - ret = gpio_request(i2c->gpio, i2c->adap.name); 231 - if (ret == 0) { 232 - gpio_direction_output(i2c->gpio, 1); 233 - i2c->algo_data.reset_chip = i2c_pca_pf_resetchip; 234 - } else { 235 - printk(KERN_WARNING "%s: Registering gpio failed!\n", 236 - i2c->adap.name); 237 - i2c->gpio = ret; 238 - } 239 - } 240 - 241 211 if (irq) { 242 - ret = request_irq(irq, i2c_pca_pf_handler, 212 + ret = devm_request_irq(&pdev->dev, irq, i2c_pca_pf_handler, 243 213 IRQF_TRIGGER_FALLING, pdev->name, i2c); 244 214 if (ret) 245 - goto e_reqirq; 215 + return ret; 246 216 } 247 217 248 - if (i2c_pca_add_numbered_bus(&i2c->adap) < 0) { 249 - ret = -ENODEV; 250 - goto e_adapt; 251 - } 218 + ret = i2c_pca_add_numbered_bus(&i2c->adap); 219 + if (ret) 220 + return ret; 252 221 253 222 platform_set_drvdata(pdev, i2c); 254 223 255 - printk(KERN_INFO "%s registered.\n", i2c->adap.name); 224 + dev_info(&pdev->dev, "registered.\n"); 256 225 257 226 return 0; 258 - 259 - e_adapt: 260 - if (irq) 261 - free_irq(irq, i2c); 262 - e_reqirq: 263 - if (i2c->gpio > -1) 264 - gpio_free(i2c->gpio); 265 - 266 - iounmap(i2c->reg_base); 267 - e_remap: 268 - kfree(i2c); 269 - e_alloc: 270 - release_mem_region(res->start, resource_size(res)); 271 - e_print: 272 - printk(KERN_ERR "Registering PCA9564/PCA9665 FAILED! (%d)\n", ret); 273 - return ret; 274 227 } 275 228 276 229 static int i2c_pca_pf_remove(struct platform_device *pdev) ··· 249 262 250 263 i2c_del_adapter(&i2c->adap); 251 264 252 - if (i2c->irq) 253 - free_irq(i2c->irq, i2c); 254 - 255 - if (i2c->gpio > -1) 256 - gpio_free(i2c->gpio); 257 - 258 - iounmap(i2c->reg_base); 259 - release_mem_region(i2c->io_base, i2c->io_size); 260 - kfree(i2c); 261 - 262 265 return 0; 263 266 } 267 + 268 + #ifdef CONFIG_OF 269 + static const struct of_device_id i2c_pca_of_match_table[] = { 270 + { .compatible = "nxp,pca9564" }, 271 + { .compatible = "nxp,pca9665" }, 272 + {}, 273 + }; 274 + MODULE_DEVICE_TABLE(of, i2c_pca_of_match_table); 275 + #endif 264 276 265 277 static struct platform_driver i2c_pca_pf_driver = { 266 278 .probe = i2c_pca_pf_probe, 267 279 .remove = i2c_pca_pf_remove, 268 280 .driver = { 269 281 .name = "i2c-pca-platform", 282 + .of_match_table = of_match_ptr(i2c_pca_of_match_table), 270 283 }, 271 284 }; 272 285
+7 -2
drivers/i2c/busses/i2c-rcar.c
··· 1 1 /* 2 - * Driver for the Renesas RCar I2C unit 2 + * Driver for the Renesas R-Car I2C unit 3 3 * 4 4 * Copyright (C) 2014-15 Wolfram Sang <wsa@sang-engineering.com> 5 5 * Copyright (C) 2011-2015 Renesas Electronics Corporation ··· 783 783 784 784 static u32 rcar_i2c_func(struct i2c_adapter *adap) 785 785 { 786 - /* This HW can't do SMBUS_QUICK and NOSTART */ 786 + /* 787 + * This HW can't do: 788 + * I2C_SMBUS_QUICK (setting FSB during START didn't work) 789 + * I2C_M_NOSTART (automatically sends address after START) 790 + * I2C_M_IGNORE_NAK (automatically sends STOP after NAK) 791 + */ 787 792 return I2C_FUNC_I2C | I2C_FUNC_SLAVE | 788 793 (I2C_FUNC_SMBUS_EMUL & ~I2C_FUNC_SMBUS_QUICK); 789 794 }
+7 -20
drivers/i2c/busses/i2c-sh_mobile.c
··· 24 24 #include <linux/dma-mapping.h> 25 25 #include <linux/err.h> 26 26 #include <linux/i2c.h> 27 - #include <linux/i2c/i2c-sh_mobile.h> 28 27 #include <linux/init.h> 29 28 #include <linux/interrupt.h> 30 29 #include <linux/io.h> ··· 878 879 879 880 static int sh_mobile_i2c_probe(struct platform_device *dev) 880 881 { 881 - struct i2c_sh_mobile_platform_data *pdata = dev_get_platdata(&dev->dev); 882 882 struct sh_mobile_i2c_data *pd; 883 883 struct i2c_adapter *adap; 884 884 struct resource *res; 885 + const struct of_device_id *match; 885 886 int ret; 886 887 u32 bus_speed; 887 888 ··· 909 910 if (IS_ERR(pd->reg)) 910 911 return PTR_ERR(pd->reg); 911 912 912 - /* Use platform data bus speed or STANDARD_MODE */ 913 913 ret = of_property_read_u32(dev->dev.of_node, "clock-frequency", &bus_speed); 914 914 pd->bus_speed = ret ? STANDARD_MODE : bus_speed; 915 - 916 915 pd->clks_per_count = 1; 917 916 918 - if (dev->dev.of_node) { 919 - const struct of_device_id *match; 917 + match = of_match_device(sh_mobile_i2c_dt_ids, &dev->dev); 918 + if (match) { 919 + const struct sh_mobile_dt_config *config = match->data; 920 920 921 - match = of_match_device(sh_mobile_i2c_dt_ids, &dev->dev); 922 - if (match) { 923 - const struct sh_mobile_dt_config *config; 921 + pd->clks_per_count = config->clks_per_count; 924 922 925 - config = match->data; 926 - pd->clks_per_count = config->clks_per_count; 927 - 928 - if (config->setup) 929 - config->setup(pd); 930 - } 931 - } else { 932 - if (pdata && pdata->bus_speed) 933 - pd->bus_speed = pdata->bus_speed; 934 - if (pdata && pdata->clks_per_count) 935 - pd->clks_per_count = pdata->clks_per_count; 923 + if (config->setup) 924 + config->setup(pd); 936 925 } 937 926 938 927 /* The IIC blocks on SH-Mobile ARM processors
+188 -54
drivers/i2c/busses/i2c-xgene-slimpro.c
··· 22 22 * using the APM X-Gene SLIMpro mailbox driver. 23 23 * 24 24 */ 25 + #include <acpi/pcc.h> 25 26 #include <linux/acpi.h> 26 27 #include <linux/dma-mapping.h> 27 28 #include <linux/i2c.h> 28 29 #include <linux/interrupt.h> 30 + #include <linux/io.h> 29 31 #include <linux/mailbox_client.h> 30 32 #include <linux/module.h> 31 33 #include <linux/of.h> ··· 91 89 ((addrlen << SLIMPRO_IIC_ADDRLEN_SHIFT) & SLIMPRO_IIC_ADDRLEN_MASK) | \ 92 90 ((datalen << SLIMPRO_IIC_DATALEN_SHIFT) & SLIMPRO_IIC_DATALEN_MASK)) 93 91 92 + #define SLIMPRO_MSG_TYPE(v) (((v) & 0xF0000000) >> 28) 93 + 94 94 /* 95 95 * Encode for upper address for block data 96 96 */ ··· 103 99 & 0x3FF00000)) 104 100 #define SLIMPRO_IIC_ENCODE_ADDR(a) ((a) & 0x000FFFFF) 105 101 102 + #define SLIMPRO_IIC_MSG_DWORD_COUNT 3 103 + 104 + /* PCC related defines */ 105 + #define PCC_SIGNATURE 0x50424300 106 + #define PCC_STS_CMD_COMPLETE BIT(0) 107 + #define PCC_STS_SCI_DOORBELL BIT(1) 108 + #define PCC_STS_ERR BIT(2) 109 + #define PCC_STS_PLAT_NOTIFY BIT(3) 110 + #define PCC_CMD_GENERATE_DB_INT BIT(15) 111 + 106 112 struct slimpro_i2c_dev { 107 113 struct i2c_adapter adapter; 108 114 struct device *dev; 109 115 struct mbox_chan *mbox_chan; 110 116 struct mbox_client mbox_client; 117 + int mbox_idx; 111 118 struct completion rd_complete; 112 119 u8 dma_buffer[I2C_SMBUS_BLOCK_MAX + 1]; /* dma_buffer[0] is used for length */ 113 120 u32 *resp_msg; 121 + phys_addr_t comm_base_addr; 122 + void *pcc_comm_addr; 114 123 }; 115 124 116 125 #define to_slimpro_i2c_dev(cl) \ 117 126 container_of(cl, struct slimpro_i2c_dev, mbox_client) 127 + 128 + /* 129 + * This function tests and clears a bitmask then returns its old value 130 + */ 131 + static u16 xgene_word_tst_and_clr(u16 *addr, u16 mask) 132 + { 133 + u16 ret, val; 134 + 135 + val = le16_to_cpu(READ_ONCE(*addr)); 136 + ret = val & mask; 137 + val &= ~mask; 138 + WRITE_ONCE(*addr, cpu_to_le16(val)); 139 + 140 + return ret; 141 + } 118 142 119 143 static void slimpro_i2c_rx_cb(struct mbox_client *cl, void *mssg) 120 144 { ··· 161 129 complete(&ctx->rd_complete); 162 130 } 163 131 132 + static void slimpro_i2c_pcc_rx_cb(struct mbox_client *cl, void *msg) 133 + { 134 + struct slimpro_i2c_dev *ctx = to_slimpro_i2c_dev(cl); 135 + struct acpi_pcct_shared_memory *generic_comm_base = ctx->pcc_comm_addr; 136 + 137 + /* Check if platform sends interrupt */ 138 + if (!xgene_word_tst_and_clr(&generic_comm_base->status, 139 + PCC_STS_SCI_DOORBELL)) 140 + return; 141 + 142 + if (xgene_word_tst_and_clr(&generic_comm_base->status, 143 + PCC_STS_CMD_COMPLETE)) { 144 + msg = generic_comm_base + 1; 145 + 146 + /* Response message msg[1] contains the return value. */ 147 + if (ctx->resp_msg) 148 + *ctx->resp_msg = ((u32 *)msg)[1]; 149 + 150 + complete(&ctx->rd_complete); 151 + } 152 + } 153 + 154 + static void slimpro_i2c_pcc_tx_prepare(struct slimpro_i2c_dev *ctx, u32 *msg) 155 + { 156 + struct acpi_pcct_shared_memory *generic_comm_base = ctx->pcc_comm_addr; 157 + u32 *ptr = (void *)(generic_comm_base + 1); 158 + u16 status; 159 + int i; 160 + 161 + WRITE_ONCE(generic_comm_base->signature, 162 + cpu_to_le32(PCC_SIGNATURE | ctx->mbox_idx)); 163 + 164 + WRITE_ONCE(generic_comm_base->command, 165 + cpu_to_le16(SLIMPRO_MSG_TYPE(msg[0]) | PCC_CMD_GENERATE_DB_INT)); 166 + 167 + status = le16_to_cpu(READ_ONCE(generic_comm_base->status)); 168 + status &= ~PCC_STS_CMD_COMPLETE; 169 + WRITE_ONCE(generic_comm_base->status, cpu_to_le16(status)); 170 + 171 + /* Copy the message to the PCC comm space */ 172 + for (i = 0; i < SLIMPRO_IIC_MSG_DWORD_COUNT; i++) 173 + WRITE_ONCE(ptr[i], cpu_to_le32(msg[i])); 174 + } 175 + 164 176 static int start_i2c_msg_xfer(struct slimpro_i2c_dev *ctx) 165 177 { 166 - if (ctx->mbox_client.tx_block) { 178 + if (ctx->mbox_client.tx_block || !acpi_disabled) { 167 179 if (!wait_for_completion_timeout(&ctx->rd_complete, 168 180 msecs_to_jiffies(MAILBOX_OP_TIMEOUT))) 169 181 return -ETIMEDOUT; ··· 220 144 return 0; 221 145 } 222 146 147 + static int slimpro_i2c_send_msg(struct slimpro_i2c_dev *ctx, 148 + u32 *msg, 149 + u32 *data) 150 + { 151 + int rc; 152 + 153 + ctx->resp_msg = data; 154 + 155 + if (!acpi_disabled) { 156 + reinit_completion(&ctx->rd_complete); 157 + slimpro_i2c_pcc_tx_prepare(ctx, msg); 158 + } 159 + 160 + rc = mbox_send_message(ctx->mbox_chan, msg); 161 + if (rc < 0) 162 + goto err; 163 + 164 + rc = start_i2c_msg_xfer(ctx); 165 + 166 + err: 167 + if (!acpi_disabled) 168 + mbox_chan_txdone(ctx->mbox_chan, 0); 169 + 170 + ctx->resp_msg = NULL; 171 + 172 + return rc; 173 + } 174 + 223 175 static int slimpro_i2c_rd(struct slimpro_i2c_dev *ctx, u32 chip, 224 176 u32 addr, u32 addrlen, u32 protocol, 225 177 u32 readlen, u32 *data) 226 178 { 227 179 u32 msg[3]; 228 - int rc; 229 180 230 181 msg[0] = SLIMPRO_IIC_ENCODE_MSG(SLIMPRO_IIC_BUS, chip, 231 182 SLIMPRO_IIC_READ, protocol, addrlen, readlen); 232 183 msg[1] = SLIMPRO_IIC_ENCODE_ADDR(addr); 233 184 msg[2] = 0; 234 - ctx->resp_msg = data; 235 - rc = mbox_send_message(ctx->mbox_chan, &msg); 236 - if (rc < 0) 237 - goto err; 238 185 239 - rc = start_i2c_msg_xfer(ctx); 240 - err: 241 - ctx->resp_msg = NULL; 242 - return rc; 186 + return slimpro_i2c_send_msg(ctx, msg, data); 243 187 } 244 188 245 189 static int slimpro_i2c_wr(struct slimpro_i2c_dev *ctx, u32 chip, ··· 267 171 u32 data) 268 172 { 269 173 u32 msg[3]; 270 - int rc; 271 174 272 175 msg[0] = SLIMPRO_IIC_ENCODE_MSG(SLIMPRO_IIC_BUS, chip, 273 176 SLIMPRO_IIC_WRITE, protocol, addrlen, writelen); 274 177 msg[1] = SLIMPRO_IIC_ENCODE_ADDR(addr); 275 178 msg[2] = data; 276 - ctx->resp_msg = msg; 277 179 278 - rc = mbox_send_message(ctx->mbox_chan, &msg); 279 - if (rc < 0) 280 - goto err; 281 - 282 - rc = start_i2c_msg_xfer(ctx); 283 - err: 284 - ctx->resp_msg = NULL; 285 - return rc; 180 + return slimpro_i2c_send_msg(ctx, msg, msg); 286 181 } 287 182 288 183 static int slimpro_i2c_blkrd(struct slimpro_i2c_dev *ctx, u32 chip, u32 addr, ··· 288 201 if (dma_mapping_error(ctx->dev, paddr)) { 289 202 dev_err(&ctx->adapter.dev, "Error in mapping dma buffer %p\n", 290 203 ctx->dma_buffer); 291 - rc = -ENOMEM; 292 - goto err; 204 + return -ENOMEM; 293 205 } 294 206 295 207 msg[0] = SLIMPRO_IIC_ENCODE_MSG(SLIMPRO_IIC_BUS, chip, SLIMPRO_IIC_READ, ··· 298 212 SLIMPRO_IIC_ENCODE_UPPER_BUFADDR(paddr) | 299 213 SLIMPRO_IIC_ENCODE_ADDR(addr); 300 214 msg[2] = (u32)paddr; 301 - ctx->resp_msg = msg; 302 215 303 - rc = mbox_send_message(ctx->mbox_chan, &msg); 304 - if (rc < 0) 305 - goto err_unmap; 306 - 307 - rc = start_i2c_msg_xfer(ctx); 216 + rc = slimpro_i2c_send_msg(ctx, msg, msg); 308 217 309 218 /* Copy to destination */ 310 219 memcpy(data, ctx->dma_buffer, readlen); 311 220 312 - err_unmap: 313 221 dma_unmap_single(ctx->dev, paddr, readlen, DMA_FROM_DEVICE); 314 - err: 315 - ctx->resp_msg = NULL; 316 222 return rc; 317 223 } 318 224 ··· 322 244 if (dma_mapping_error(ctx->dev, paddr)) { 323 245 dev_err(&ctx->adapter.dev, "Error in mapping dma buffer %p\n", 324 246 ctx->dma_buffer); 325 - rc = -ENOMEM; 326 - goto err; 247 + return -ENOMEM; 327 248 } 328 249 329 250 msg[0] = SLIMPRO_IIC_ENCODE_MSG(SLIMPRO_IIC_BUS, chip, SLIMPRO_IIC_WRITE, ··· 331 254 SLIMPRO_IIC_ENCODE_UPPER_BUFADDR(paddr) | 332 255 SLIMPRO_IIC_ENCODE_ADDR(addr); 333 256 msg[2] = (u32)paddr; 334 - ctx->resp_msg = msg; 335 257 336 258 if (ctx->mbox_client.tx_block) 337 259 reinit_completion(&ctx->rd_complete); 338 260 339 - rc = mbox_send_message(ctx->mbox_chan, &msg); 340 - if (rc < 0) 341 - goto err_unmap; 261 + rc = slimpro_i2c_send_msg(ctx, msg, msg); 342 262 343 - rc = start_i2c_msg_xfer(ctx); 344 - 345 - err_unmap: 346 263 dma_unmap_single(ctx->dev, paddr, writelen, DMA_TO_DEVICE); 347 - err: 348 - ctx->resp_msg = NULL; 349 264 return rc; 350 265 } 351 266 ··· 463 394 464 395 /* Request mailbox channel */ 465 396 cl->dev = &pdev->dev; 466 - cl->rx_callback = slimpro_i2c_rx_cb; 467 - cl->tx_block = true; 468 397 init_completion(&ctx->rd_complete); 469 398 cl->tx_tout = MAILBOX_OP_TIMEOUT; 470 399 cl->knows_txdone = false; 471 - ctx->mbox_chan = mbox_request_channel(cl, MAILBOX_I2C_INDEX); 472 - if (IS_ERR(ctx->mbox_chan)) { 473 - dev_err(&pdev->dev, "i2c mailbox channel request failed\n"); 474 - return PTR_ERR(ctx->mbox_chan); 475 - } 400 + if (acpi_disabled) { 401 + cl->tx_block = true; 402 + cl->rx_callback = slimpro_i2c_rx_cb; 403 + ctx->mbox_chan = mbox_request_channel(cl, MAILBOX_I2C_INDEX); 404 + if (IS_ERR(ctx->mbox_chan)) { 405 + dev_err(&pdev->dev, "i2c mailbox channel request failed\n"); 406 + return PTR_ERR(ctx->mbox_chan); 407 + } 408 + } else { 409 + struct acpi_pcct_hw_reduced *cppc_ss; 476 410 411 + if (device_property_read_u32(&pdev->dev, "pcc-channel", 412 + &ctx->mbox_idx)) 413 + ctx->mbox_idx = MAILBOX_I2C_INDEX; 414 + 415 + cl->tx_block = false; 416 + cl->rx_callback = slimpro_i2c_pcc_rx_cb; 417 + ctx->mbox_chan = pcc_mbox_request_channel(cl, ctx->mbox_idx); 418 + if (IS_ERR(ctx->mbox_chan)) { 419 + dev_err(&pdev->dev, "PCC mailbox channel request failed\n"); 420 + return PTR_ERR(ctx->mbox_chan); 421 + } 422 + 423 + /* 424 + * The PCC mailbox controller driver should 425 + * have parsed the PCCT (global table of all 426 + * PCC channels) and stored pointers to the 427 + * subspace communication region in con_priv. 428 + */ 429 + cppc_ss = ctx->mbox_chan->con_priv; 430 + if (!cppc_ss) { 431 + dev_err(&pdev->dev, "PPC subspace not found\n"); 432 + rc = -ENOENT; 433 + goto mbox_err; 434 + } 435 + 436 + if (!ctx->mbox_chan->mbox->txdone_irq) { 437 + dev_err(&pdev->dev, "PCC IRQ not supported\n"); 438 + rc = -ENOENT; 439 + goto mbox_err; 440 + } 441 + 442 + /* 443 + * This is the shared communication region 444 + * for the OS and Platform to communicate over. 445 + */ 446 + ctx->comm_base_addr = cppc_ss->base_address; 447 + if (ctx->comm_base_addr) { 448 + ctx->pcc_comm_addr = memremap(ctx->comm_base_addr, 449 + cppc_ss->length, 450 + MEMREMAP_WB); 451 + } else { 452 + dev_err(&pdev->dev, "Failed to get PCC comm region\n"); 453 + rc = -ENOENT; 454 + goto mbox_err; 455 + } 456 + 457 + if (!ctx->pcc_comm_addr) { 458 + dev_err(&pdev->dev, 459 + "Failed to ioremap PCC comm region\n"); 460 + rc = -ENOMEM; 461 + goto mbox_err; 462 + } 463 + } 477 464 rc = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)); 478 465 if (rc) 479 466 dev_warn(&pdev->dev, "Unable to set dma mask\n"); ··· 544 419 ACPI_COMPANION_SET(&adapter->dev, ACPI_COMPANION(&pdev->dev)); 545 420 i2c_set_adapdata(adapter, ctx); 546 421 rc = i2c_add_adapter(adapter); 547 - if (rc) { 548 - mbox_free_channel(ctx->mbox_chan); 549 - return rc; 550 - } 422 + if (rc) 423 + goto mbox_err; 551 424 552 425 dev_info(&pdev->dev, "Mailbox I2C Adapter registered\n"); 553 426 return 0; 427 + 428 + mbox_err: 429 + if (acpi_disabled) 430 + mbox_free_channel(ctx->mbox_chan); 431 + else 432 + pcc_mbox_free_channel(ctx->mbox_chan); 433 + 434 + return rc; 554 435 } 555 436 556 437 static int xgene_slimpro_i2c_remove(struct platform_device *pdev) ··· 565 434 566 435 i2c_del_adapter(&ctx->adapter); 567 436 568 - mbox_free_channel(ctx->mbox_chan); 437 + if (acpi_disabled) 438 + mbox_free_channel(ctx->mbox_chan); 439 + else 440 + pcc_mbox_free_channel(ctx->mbox_chan); 569 441 570 442 return 0; 571 443 }
+1
drivers/i2c/busses/i2c-xlp9xx.c
··· 393 393 init_completion(&priv->msg_complete); 394 394 priv->adapter.dev.parent = &pdev->dev; 395 395 priv->adapter.algo = &xlp9xx_i2c_algo; 396 + priv->adapter.class = I2C_CLASS_HWMON; 396 397 ACPI_COMPANION_SET(&priv->adapter.dev, ACPI_COMPANION(&pdev->dev)); 397 398 priv->adapter.dev.of_node = pdev->dev.of_node; 398 399 priv->dev = &pdev->dev;
+609
drivers/i2c/busses/i2c-zx2967.c
··· 1 + /* 2 + * Copyright (C) 2017 Sanechips Technology Co., Ltd. 3 + * Copyright 2017 Linaro Ltd. 4 + * 5 + * Author: Baoyou Xie <baoyou.xie@linaro.org> 6 + * 7 + * This program is free software; you can redistribute it and/or modify 8 + * it under the terms of the GNU General Public License version 2 as 9 + * published by the Free Software Foundation. 10 + */ 11 + 12 + #include <linux/clk.h> 13 + #include <linux/i2c.h> 14 + #include <linux/interrupt.h> 15 + #include <linux/io.h> 16 + #include <linux/module.h> 17 + #include <linux/platform_device.h> 18 + 19 + #define REG_CMD 0x04 20 + #define REG_DEVADDR_H 0x0C 21 + #define REG_DEVADDR_L 0x10 22 + #define REG_CLK_DIV_FS 0x14 23 + #define REG_CLK_DIV_HS 0x18 24 + #define REG_WRCONF 0x1C 25 + #define REG_RDCONF 0x20 26 + #define REG_DATA 0x24 27 + #define REG_STAT 0x28 28 + 29 + #define I2C_STOP 0 30 + #define I2C_MASTER BIT(0) 31 + #define I2C_ADDR_MODE_TEN BIT(1) 32 + #define I2C_IRQ_MSK_ENABLE BIT(3) 33 + #define I2C_RW_READ BIT(4) 34 + #define I2C_CMB_RW_EN BIT(5) 35 + #define I2C_START BIT(6) 36 + 37 + #define I2C_ADDR_LOW_MASK GENMASK(6, 0) 38 + #define I2C_ADDR_LOW_SHIFT 0 39 + #define I2C_ADDR_HI_MASK GENMASK(2, 0) 40 + #define I2C_ADDR_HI_SHIFT 7 41 + 42 + #define I2C_WFIFO_RESET BIT(7) 43 + #define I2C_RFIFO_RESET BIT(7) 44 + 45 + #define I2C_IRQ_ACK_CLEAR BIT(7) 46 + #define I2C_INT_MASK GENMASK(6, 0) 47 + 48 + #define I2C_TRANS_DONE BIT(0) 49 + #define I2C_SR_EDEVICE BIT(1) 50 + #define I2C_SR_EDATA BIT(2) 51 + 52 + #define I2C_FIFO_MAX 16 53 + 54 + #define I2C_TIMEOUT msecs_to_jiffies(1000) 55 + 56 + #define DEV(i2c) ((i2c)->adap.dev.parent) 57 + 58 + struct zx2967_i2c { 59 + struct i2c_adapter adap; 60 + struct clk *clk; 61 + struct completion complete; 62 + u32 clk_freq; 63 + void __iomem *reg_base; 64 + size_t residue; 65 + int irq; 66 + int msg_rd; 67 + u8 *cur_trans; 68 + u8 access_cnt; 69 + bool is_suspended; 70 + int error; 71 + }; 72 + 73 + static void zx2967_i2c_writel(struct zx2967_i2c *i2c, 74 + u32 val, unsigned long reg) 75 + { 76 + writel_relaxed(val, i2c->reg_base + reg); 77 + } 78 + 79 + static u32 zx2967_i2c_readl(struct zx2967_i2c *i2c, unsigned long reg) 80 + { 81 + return readl_relaxed(i2c->reg_base + reg); 82 + } 83 + 84 + static void zx2967_i2c_writesb(struct zx2967_i2c *i2c, 85 + void *data, unsigned long reg, int len) 86 + { 87 + writesb(i2c->reg_base + reg, data, len); 88 + } 89 + 90 + static void zx2967_i2c_readsb(struct zx2967_i2c *i2c, 91 + void *data, unsigned long reg, int len) 92 + { 93 + readsb(i2c->reg_base + reg, data, len); 94 + } 95 + 96 + static void zx2967_i2c_start_ctrl(struct zx2967_i2c *i2c) 97 + { 98 + u32 status; 99 + u32 ctl; 100 + 101 + status = zx2967_i2c_readl(i2c, REG_STAT); 102 + status |= I2C_IRQ_ACK_CLEAR; 103 + zx2967_i2c_writel(i2c, status, REG_STAT); 104 + 105 + ctl = zx2967_i2c_readl(i2c, REG_CMD); 106 + if (i2c->msg_rd) 107 + ctl |= I2C_RW_READ; 108 + else 109 + ctl &= ~I2C_RW_READ; 110 + ctl &= ~I2C_CMB_RW_EN; 111 + ctl |= I2C_START; 112 + zx2967_i2c_writel(i2c, ctl, REG_CMD); 113 + } 114 + 115 + static void zx2967_i2c_flush_fifos(struct zx2967_i2c *i2c) 116 + { 117 + u32 offset; 118 + u32 val; 119 + 120 + if (i2c->msg_rd) { 121 + offset = REG_RDCONF; 122 + val = I2C_RFIFO_RESET; 123 + } else { 124 + offset = REG_WRCONF; 125 + val = I2C_WFIFO_RESET; 126 + } 127 + 128 + val |= zx2967_i2c_readl(i2c, offset); 129 + zx2967_i2c_writel(i2c, val, offset); 130 + } 131 + 132 + static int zx2967_i2c_empty_rx_fifo(struct zx2967_i2c *i2c, u32 size) 133 + { 134 + u8 val[I2C_FIFO_MAX] = {0}; 135 + int i; 136 + 137 + if (size > I2C_FIFO_MAX) { 138 + dev_err(DEV(i2c), "fifo size %d over the max value %d\n", 139 + size, I2C_FIFO_MAX); 140 + return -EINVAL; 141 + } 142 + 143 + zx2967_i2c_readsb(i2c, val, REG_DATA, size); 144 + for (i = 0; i < size; i++) { 145 + *i2c->cur_trans++ = val[i]; 146 + i2c->residue--; 147 + } 148 + 149 + barrier(); 150 + 151 + return 0; 152 + } 153 + 154 + static int zx2967_i2c_fill_tx_fifo(struct zx2967_i2c *i2c) 155 + { 156 + size_t residue = i2c->residue; 157 + u8 *buf = i2c->cur_trans; 158 + 159 + if (residue == 0) { 160 + dev_err(DEV(i2c), "residue is %d\n", (int)residue); 161 + return -EINVAL; 162 + } 163 + 164 + if (residue <= I2C_FIFO_MAX) { 165 + zx2967_i2c_writesb(i2c, buf, REG_DATA, residue); 166 + 167 + /* Again update before writing to FIFO to make sure isr sees. */ 168 + i2c->residue = 0; 169 + i2c->cur_trans = NULL; 170 + } else { 171 + zx2967_i2c_writesb(i2c, buf, REG_DATA, I2C_FIFO_MAX); 172 + i2c->residue -= I2C_FIFO_MAX; 173 + i2c->cur_trans += I2C_FIFO_MAX; 174 + } 175 + 176 + barrier(); 177 + 178 + return 0; 179 + } 180 + 181 + static int zx2967_i2c_reset_hardware(struct zx2967_i2c *i2c) 182 + { 183 + u32 val; 184 + u32 clk_div; 185 + 186 + val = I2C_MASTER | I2C_IRQ_MSK_ENABLE; 187 + zx2967_i2c_writel(i2c, val, REG_CMD); 188 + 189 + clk_div = clk_get_rate(i2c->clk) / i2c->clk_freq - 1; 190 + zx2967_i2c_writel(i2c, clk_div, REG_CLK_DIV_FS); 191 + zx2967_i2c_writel(i2c, clk_div, REG_CLK_DIV_HS); 192 + 193 + zx2967_i2c_writel(i2c, I2C_FIFO_MAX - 1, REG_WRCONF); 194 + zx2967_i2c_writel(i2c, I2C_FIFO_MAX - 1, REG_RDCONF); 195 + zx2967_i2c_writel(i2c, 1, REG_RDCONF); 196 + 197 + zx2967_i2c_flush_fifos(i2c); 198 + 199 + return 0; 200 + } 201 + 202 + static void zx2967_i2c_isr_clr(struct zx2967_i2c *i2c) 203 + { 204 + u32 status; 205 + 206 + status = zx2967_i2c_readl(i2c, REG_STAT); 207 + status |= I2C_IRQ_ACK_CLEAR; 208 + zx2967_i2c_writel(i2c, status, REG_STAT); 209 + } 210 + 211 + static irqreturn_t zx2967_i2c_isr(int irq, void *dev_id) 212 + { 213 + u32 status; 214 + struct zx2967_i2c *i2c = (struct zx2967_i2c *)dev_id; 215 + 216 + status = zx2967_i2c_readl(i2c, REG_STAT) & I2C_INT_MASK; 217 + zx2967_i2c_isr_clr(i2c); 218 + 219 + if (status & I2C_SR_EDEVICE) 220 + i2c->error = -ENXIO; 221 + else if (status & I2C_SR_EDATA) 222 + i2c->error = -EIO; 223 + else if (status & I2C_TRANS_DONE) 224 + i2c->error = 0; 225 + else 226 + goto done; 227 + 228 + complete(&i2c->complete); 229 + done: 230 + return IRQ_HANDLED; 231 + } 232 + 233 + static void zx2967_set_addr(struct zx2967_i2c *i2c, u16 addr) 234 + { 235 + u16 val; 236 + 237 + val = (addr >> I2C_ADDR_LOW_SHIFT) & I2C_ADDR_LOW_MASK; 238 + zx2967_i2c_writel(i2c, val, REG_DEVADDR_L); 239 + 240 + val = (addr >> I2C_ADDR_HI_SHIFT) & I2C_ADDR_HI_MASK; 241 + zx2967_i2c_writel(i2c, val, REG_DEVADDR_H); 242 + if (val) 243 + val = zx2967_i2c_readl(i2c, REG_CMD) | I2C_ADDR_MODE_TEN; 244 + else 245 + val = zx2967_i2c_readl(i2c, REG_CMD) & ~I2C_ADDR_MODE_TEN; 246 + zx2967_i2c_writel(i2c, val, REG_CMD); 247 + } 248 + 249 + static int zx2967_i2c_xfer_bytes(struct zx2967_i2c *i2c, u32 bytes) 250 + { 251 + unsigned long time_left; 252 + int rd = i2c->msg_rd; 253 + int ret; 254 + 255 + reinit_completion(&i2c->complete); 256 + 257 + if (rd) { 258 + zx2967_i2c_writel(i2c, bytes - 1, REG_RDCONF); 259 + } else { 260 + ret = zx2967_i2c_fill_tx_fifo(i2c); 261 + if (ret) 262 + return ret; 263 + } 264 + 265 + zx2967_i2c_start_ctrl(i2c); 266 + 267 + time_left = wait_for_completion_timeout(&i2c->complete, 268 + I2C_TIMEOUT); 269 + if (time_left == 0) 270 + return -ETIMEDOUT; 271 + 272 + if (i2c->error) 273 + return i2c->error; 274 + 275 + return rd ? zx2967_i2c_empty_rx_fifo(i2c, bytes) : 0; 276 + } 277 + 278 + static int zx2967_i2c_xfer_msg(struct zx2967_i2c *i2c, 279 + struct i2c_msg *msg) 280 + { 281 + int ret; 282 + int i; 283 + 284 + if (msg->len == 0) 285 + return -EINVAL; 286 + 287 + zx2967_i2c_flush_fifos(i2c); 288 + 289 + i2c->cur_trans = msg->buf; 290 + i2c->residue = msg->len; 291 + i2c->access_cnt = msg->len / I2C_FIFO_MAX; 292 + i2c->msg_rd = msg->flags & I2C_M_RD; 293 + 294 + for (i = 0; i < i2c->access_cnt; i++) { 295 + ret = zx2967_i2c_xfer_bytes(i2c, I2C_FIFO_MAX); 296 + if (ret) 297 + return ret; 298 + } 299 + 300 + if (i2c->residue > 0) { 301 + ret = zx2967_i2c_xfer_bytes(i2c, i2c->residue); 302 + if (ret) 303 + return ret; 304 + } 305 + 306 + i2c->residue = 0; 307 + i2c->access_cnt = 0; 308 + 309 + return 0; 310 + } 311 + 312 + static int zx2967_i2c_xfer(struct i2c_adapter *adap, 313 + struct i2c_msg *msgs, int num) 314 + { 315 + struct zx2967_i2c *i2c = i2c_get_adapdata(adap); 316 + int ret; 317 + int i; 318 + 319 + if (i2c->is_suspended) 320 + return -EBUSY; 321 + 322 + zx2967_set_addr(i2c, msgs->addr); 323 + 324 + for (i = 0; i < num; i++) { 325 + ret = zx2967_i2c_xfer_msg(i2c, &msgs[i]); 326 + if (ret) 327 + return ret; 328 + } 329 + 330 + return num; 331 + } 332 + 333 + static void 334 + zx2967_smbus_xfer_prepare(struct zx2967_i2c *i2c, u16 addr, 335 + char read_write, u8 command, int size, 336 + union i2c_smbus_data *data) 337 + { 338 + u32 val; 339 + 340 + val = zx2967_i2c_readl(i2c, REG_RDCONF); 341 + val |= I2C_RFIFO_RESET; 342 + zx2967_i2c_writel(i2c, val, REG_RDCONF); 343 + zx2967_set_addr(i2c, addr); 344 + val = zx2967_i2c_readl(i2c, REG_CMD); 345 + val &= ~I2C_RW_READ; 346 + zx2967_i2c_writel(i2c, val, REG_CMD); 347 + 348 + switch (size) { 349 + case I2C_SMBUS_BYTE: 350 + zx2967_i2c_writel(i2c, command, REG_DATA); 351 + break; 352 + case I2C_SMBUS_BYTE_DATA: 353 + zx2967_i2c_writel(i2c, command, REG_DATA); 354 + if (read_write == I2C_SMBUS_WRITE) 355 + zx2967_i2c_writel(i2c, data->byte, REG_DATA); 356 + break; 357 + case I2C_SMBUS_WORD_DATA: 358 + zx2967_i2c_writel(i2c, command, REG_DATA); 359 + if (read_write == I2C_SMBUS_WRITE) { 360 + zx2967_i2c_writel(i2c, (data->word >> 8), REG_DATA); 361 + zx2967_i2c_writel(i2c, (data->word & 0xff), 362 + REG_DATA); 363 + } 364 + break; 365 + } 366 + } 367 + 368 + static int zx2967_smbus_xfer_read(struct zx2967_i2c *i2c, int size, 369 + union i2c_smbus_data *data) 370 + { 371 + unsigned long time_left; 372 + u8 buf[2]; 373 + u32 val; 374 + 375 + reinit_completion(&i2c->complete); 376 + 377 + val = zx2967_i2c_readl(i2c, REG_CMD); 378 + val |= I2C_CMB_RW_EN; 379 + zx2967_i2c_writel(i2c, val, REG_CMD); 380 + 381 + val = zx2967_i2c_readl(i2c, REG_CMD); 382 + val |= I2C_START; 383 + zx2967_i2c_writel(i2c, val, REG_CMD); 384 + 385 + time_left = wait_for_completion_timeout(&i2c->complete, 386 + I2C_TIMEOUT); 387 + if (time_left == 0) 388 + return -ETIMEDOUT; 389 + 390 + if (i2c->error) 391 + return i2c->error; 392 + 393 + switch (size) { 394 + case I2C_SMBUS_BYTE: 395 + case I2C_SMBUS_BYTE_DATA: 396 + val = zx2967_i2c_readl(i2c, REG_DATA); 397 + data->byte = val; 398 + break; 399 + case I2C_SMBUS_WORD_DATA: 400 + case I2C_SMBUS_PROC_CALL: 401 + buf[0] = zx2967_i2c_readl(i2c, REG_DATA); 402 + buf[1] = zx2967_i2c_readl(i2c, REG_DATA); 403 + data->word = (buf[0] << 8) | buf[1]; 404 + break; 405 + default: 406 + return -EOPNOTSUPP; 407 + } 408 + 409 + return 0; 410 + } 411 + 412 + static int zx2967_smbus_xfer_write(struct zx2967_i2c *i2c) 413 + { 414 + unsigned long time_left; 415 + u32 val; 416 + 417 + reinit_completion(&i2c->complete); 418 + val = zx2967_i2c_readl(i2c, REG_CMD); 419 + val |= I2C_START; 420 + zx2967_i2c_writel(i2c, val, REG_CMD); 421 + 422 + time_left = wait_for_completion_timeout(&i2c->complete, 423 + I2C_TIMEOUT); 424 + if (time_left == 0) 425 + return -ETIMEDOUT; 426 + 427 + if (i2c->error) 428 + return i2c->error; 429 + 430 + return 0; 431 + } 432 + 433 + static int zx2967_smbus_xfer(struct i2c_adapter *adap, u16 addr, 434 + unsigned short flags, char read_write, 435 + u8 command, int size, union i2c_smbus_data *data) 436 + { 437 + struct zx2967_i2c *i2c = i2c_get_adapdata(adap); 438 + 439 + if (size == I2C_SMBUS_QUICK) 440 + read_write = I2C_SMBUS_WRITE; 441 + 442 + switch (size) { 443 + case I2C_SMBUS_QUICK: 444 + case I2C_SMBUS_BYTE: 445 + case I2C_SMBUS_BYTE_DATA: 446 + case I2C_SMBUS_WORD_DATA: 447 + zx2967_smbus_xfer_prepare(i2c, addr, read_write, 448 + command, size, data); 449 + break; 450 + default: 451 + return -EOPNOTSUPP; 452 + } 453 + 454 + if (read_write == I2C_SMBUS_READ) 455 + return zx2967_smbus_xfer_read(i2c, size, data); 456 + 457 + return zx2967_smbus_xfer_write(i2c); 458 + } 459 + 460 + static u32 zx2967_i2c_func(struct i2c_adapter *adap) 461 + { 462 + return I2C_FUNC_I2C | 463 + I2C_FUNC_SMBUS_QUICK | 464 + I2C_FUNC_SMBUS_BYTE | 465 + I2C_FUNC_SMBUS_BYTE_DATA | 466 + I2C_FUNC_SMBUS_WORD_DATA | 467 + I2C_FUNC_SMBUS_BLOCK_DATA | 468 + I2C_FUNC_SMBUS_PROC_CALL | 469 + I2C_FUNC_SMBUS_I2C_BLOCK; 470 + } 471 + 472 + static int __maybe_unused zx2967_i2c_suspend(struct device *dev) 473 + { 474 + struct zx2967_i2c *i2c = dev_get_drvdata(dev); 475 + 476 + i2c->is_suspended = true; 477 + clk_disable_unprepare(i2c->clk); 478 + 479 + return 0; 480 + } 481 + 482 + static int __maybe_unused zx2967_i2c_resume(struct device *dev) 483 + { 484 + struct zx2967_i2c *i2c = dev_get_drvdata(dev); 485 + 486 + i2c->is_suspended = false; 487 + clk_prepare_enable(i2c->clk); 488 + 489 + return 0; 490 + } 491 + 492 + static SIMPLE_DEV_PM_OPS(zx2967_i2c_dev_pm_ops, 493 + zx2967_i2c_suspend, zx2967_i2c_resume); 494 + 495 + static const struct i2c_algorithm zx2967_i2c_algo = { 496 + .master_xfer = zx2967_i2c_xfer, 497 + .smbus_xfer = zx2967_smbus_xfer, 498 + .functionality = zx2967_i2c_func, 499 + }; 500 + 501 + static const struct of_device_id zx2967_i2c_of_match[] = { 502 + { .compatible = "zte,zx296718-i2c", }, 503 + { }, 504 + }; 505 + MODULE_DEVICE_TABLE(of, zx2967_i2c_of_match); 506 + 507 + static int zx2967_i2c_probe(struct platform_device *pdev) 508 + { 509 + struct zx2967_i2c *i2c; 510 + void __iomem *reg_base; 511 + struct resource *res; 512 + struct clk *clk; 513 + int ret; 514 + 515 + i2c = devm_kzalloc(&pdev->dev, sizeof(*i2c), GFP_KERNEL); 516 + if (!i2c) 517 + return -ENOMEM; 518 + 519 + res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 520 + reg_base = devm_ioremap_resource(&pdev->dev, res); 521 + if (IS_ERR(reg_base)) 522 + return PTR_ERR(reg_base); 523 + 524 + clk = devm_clk_get(&pdev->dev, NULL); 525 + if (IS_ERR(clk)) { 526 + dev_err(&pdev->dev, "missing controller clock"); 527 + return PTR_ERR(clk); 528 + } 529 + 530 + ret = clk_prepare_enable(clk); 531 + if (ret) { 532 + dev_err(&pdev->dev, "failed to enable i2c_clk\n"); 533 + return ret; 534 + } 535 + 536 + ret = device_property_read_u32(&pdev->dev, "clock-frequency", 537 + &i2c->clk_freq); 538 + if (ret) { 539 + dev_err(&pdev->dev, "missing clock-frequency"); 540 + return ret; 541 + } 542 + 543 + ret = platform_get_irq(pdev, 0); 544 + if (ret < 0) 545 + return ret; 546 + 547 + i2c->irq = ret; 548 + i2c->reg_base = reg_base; 549 + i2c->clk = clk; 550 + 551 + init_completion(&i2c->complete); 552 + platform_set_drvdata(pdev, i2c); 553 + 554 + ret = zx2967_i2c_reset_hardware(i2c); 555 + if (ret) { 556 + dev_err(&pdev->dev, "failed to initialize i2c controller\n"); 557 + goto err_clk_unprepare; 558 + } 559 + 560 + ret = devm_request_irq(&pdev->dev, i2c->irq, 561 + zx2967_i2c_isr, 0, dev_name(&pdev->dev), i2c); 562 + if (ret) { 563 + dev_err(&pdev->dev, "failed to request irq %i\n", i2c->irq); 564 + goto err_clk_unprepare; 565 + } 566 + 567 + i2c_set_adapdata(&i2c->adap, i2c); 568 + strlcpy(i2c->adap.name, "zx2967 i2c adapter", 569 + sizeof(i2c->adap.name)); 570 + i2c->adap.algo = &zx2967_i2c_algo; 571 + i2c->adap.nr = pdev->id; 572 + i2c->adap.dev.parent = &pdev->dev; 573 + i2c->adap.dev.of_node = pdev->dev.of_node; 574 + 575 + ret = i2c_add_numbered_adapter(&i2c->adap); 576 + if (ret) 577 + goto err_clk_unprepare; 578 + 579 + return 0; 580 + 581 + err_clk_unprepare: 582 + clk_disable_unprepare(i2c->clk); 583 + return ret; 584 + } 585 + 586 + static int zx2967_i2c_remove(struct platform_device *pdev) 587 + { 588 + struct zx2967_i2c *i2c = platform_get_drvdata(pdev); 589 + 590 + i2c_del_adapter(&i2c->adap); 591 + clk_disable_unprepare(i2c->clk); 592 + 593 + return 0; 594 + } 595 + 596 + static struct platform_driver zx2967_i2c_driver = { 597 + .probe = zx2967_i2c_probe, 598 + .remove = zx2967_i2c_remove, 599 + .driver = { 600 + .name = "zx2967_i2c", 601 + .of_match_table = zx2967_i2c_of_match, 602 + .pm = &zx2967_i2c_dev_pm_ops, 603 + }, 604 + }; 605 + module_platform_driver(zx2967_i2c_driver); 606 + 607 + MODULE_AUTHOR("Baoyou Xie <baoyou.xie@linaro.org>"); 608 + MODULE_DESCRIPTION("ZTE ZX2967 I2C Bus Controller driver"); 609 + MODULE_LICENSE("GPL v2");
+665
drivers/i2c/i2c-core-acpi.c
··· 1 + /* 2 + * Linux I2C core ACPI support code 3 + * 4 + * Copyright (C) 2014 Intel Corp, Author: Lan Tianyu <tianyu.lan@intel.com> 5 + * 6 + * This program is free software; you can redistribute it and/or modify it 7 + * under the terms of the GNU General Public License as published by the Free 8 + * Software Foundation; either version 2 of the License, or (at your option) 9 + * any later version. 10 + */ 11 + 12 + #include <linux/acpi.h> 13 + #include <linux/device.h> 14 + #include <linux/err.h> 15 + #include <linux/i2c.h> 16 + #include <linux/list.h> 17 + #include <linux/module.h> 18 + #include <linux/slab.h> 19 + 20 + #include "i2c-core.h" 21 + 22 + struct i2c_acpi_handler_data { 23 + struct acpi_connection_info info; 24 + struct i2c_adapter *adapter; 25 + }; 26 + 27 + struct gsb_buffer { 28 + u8 status; 29 + u8 len; 30 + union { 31 + u16 wdata; 32 + u8 bdata; 33 + u8 data[0]; 34 + }; 35 + } __packed; 36 + 37 + struct i2c_acpi_lookup { 38 + struct i2c_board_info *info; 39 + acpi_handle adapter_handle; 40 + acpi_handle device_handle; 41 + acpi_handle search_handle; 42 + int n; 43 + int index; 44 + u32 speed; 45 + u32 min_speed; 46 + }; 47 + 48 + static int i2c_acpi_fill_info(struct acpi_resource *ares, void *data) 49 + { 50 + struct i2c_acpi_lookup *lookup = data; 51 + struct i2c_board_info *info = lookup->info; 52 + struct acpi_resource_i2c_serialbus *sb; 53 + acpi_status status; 54 + 55 + if (info->addr || ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS) 56 + return 1; 57 + 58 + sb = &ares->data.i2c_serial_bus; 59 + if (sb->type != ACPI_RESOURCE_SERIAL_TYPE_I2C) 60 + return 1; 61 + 62 + if (lookup->index != -1 && lookup->n++ != lookup->index) 63 + return 1; 64 + 65 + status = acpi_get_handle(lookup->device_handle, 66 + sb->resource_source.string_ptr, 67 + &lookup->adapter_handle); 68 + if (!ACPI_SUCCESS(status)) 69 + return 1; 70 + 71 + info->addr = sb->slave_address; 72 + lookup->speed = sb->connection_speed; 73 + if (sb->access_mode == ACPI_I2C_10BIT_MODE) 74 + info->flags |= I2C_CLIENT_TEN; 75 + 76 + return 1; 77 + } 78 + 79 + static const struct acpi_device_id i2c_acpi_ignored_device_ids[] = { 80 + /* 81 + * ACPI video acpi_devices, which are handled by the acpi-video driver 82 + * sometimes contain a SERIAL_TYPE_I2C ACPI resource, ignore these. 83 + */ 84 + { ACPI_VIDEO_HID, 0 }, 85 + {} 86 + }; 87 + 88 + static int i2c_acpi_do_lookup(struct acpi_device *adev, 89 + struct i2c_acpi_lookup *lookup) 90 + { 91 + struct i2c_board_info *info = lookup->info; 92 + struct list_head resource_list; 93 + int ret; 94 + 95 + if (acpi_bus_get_status(adev) || !adev->status.present || 96 + acpi_device_enumerated(adev)) 97 + return -EINVAL; 98 + 99 + if (acpi_match_device_ids(adev, i2c_acpi_ignored_device_ids) == 0) 100 + return -ENODEV; 101 + 102 + memset(info, 0, sizeof(*info)); 103 + lookup->device_handle = acpi_device_handle(adev); 104 + 105 + /* Look up for I2cSerialBus resource */ 106 + INIT_LIST_HEAD(&resource_list); 107 + ret = acpi_dev_get_resources(adev, &resource_list, 108 + i2c_acpi_fill_info, lookup); 109 + acpi_dev_free_resource_list(&resource_list); 110 + 111 + if (ret < 0 || !info->addr) 112 + return -EINVAL; 113 + 114 + return 0; 115 + } 116 + 117 + static int i2c_acpi_get_info(struct acpi_device *adev, 118 + struct i2c_board_info *info, 119 + struct i2c_adapter *adapter, 120 + acpi_handle *adapter_handle) 121 + { 122 + struct list_head resource_list; 123 + struct resource_entry *entry; 124 + struct i2c_acpi_lookup lookup; 125 + int ret; 126 + 127 + memset(&lookup, 0, sizeof(lookup)); 128 + lookup.info = info; 129 + lookup.index = -1; 130 + 131 + ret = i2c_acpi_do_lookup(adev, &lookup); 132 + if (ret) 133 + return ret; 134 + 135 + if (adapter) { 136 + /* The adapter must match the one in I2cSerialBus() connector */ 137 + if (ACPI_HANDLE(&adapter->dev) != lookup.adapter_handle) 138 + return -ENODEV; 139 + } else { 140 + struct acpi_device *adapter_adev; 141 + 142 + /* The adapter must be present */ 143 + if (acpi_bus_get_device(lookup.adapter_handle, &adapter_adev)) 144 + return -ENODEV; 145 + if (acpi_bus_get_status(adapter_adev) || 146 + !adapter_adev->status.present) 147 + return -ENODEV; 148 + } 149 + 150 + info->fwnode = acpi_fwnode_handle(adev); 151 + if (adapter_handle) 152 + *adapter_handle = lookup.adapter_handle; 153 + 154 + /* Then fill IRQ number if any */ 155 + INIT_LIST_HEAD(&resource_list); 156 + ret = acpi_dev_get_resources(adev, &resource_list, NULL, NULL); 157 + if (ret < 0) 158 + return -EINVAL; 159 + 160 + resource_list_for_each_entry(entry, &resource_list) { 161 + if (resource_type(entry->res) == IORESOURCE_IRQ) { 162 + info->irq = entry->res->start; 163 + break; 164 + } 165 + } 166 + 167 + acpi_dev_free_resource_list(&resource_list); 168 + 169 + acpi_set_modalias(adev, dev_name(&adev->dev), info->type, 170 + sizeof(info->type)); 171 + 172 + return 0; 173 + } 174 + 175 + static void i2c_acpi_register_device(struct i2c_adapter *adapter, 176 + struct acpi_device *adev, 177 + struct i2c_board_info *info) 178 + { 179 + adev->power.flags.ignore_parent = true; 180 + acpi_device_set_enumerated(adev); 181 + 182 + if (!i2c_new_device(adapter, info)) { 183 + adev->power.flags.ignore_parent = false; 184 + dev_err(&adapter->dev, 185 + "failed to add I2C device %s from ACPI\n", 186 + dev_name(&adev->dev)); 187 + } 188 + } 189 + 190 + static acpi_status i2c_acpi_add_device(acpi_handle handle, u32 level, 191 + void *data, void **return_value) 192 + { 193 + struct i2c_adapter *adapter = data; 194 + struct acpi_device *adev; 195 + struct i2c_board_info info; 196 + 197 + if (acpi_bus_get_device(handle, &adev)) 198 + return AE_OK; 199 + 200 + if (i2c_acpi_get_info(adev, &info, adapter, NULL)) 201 + return AE_OK; 202 + 203 + i2c_acpi_register_device(adapter, adev, &info); 204 + 205 + return AE_OK; 206 + } 207 + 208 + #define I2C_ACPI_MAX_SCAN_DEPTH 32 209 + 210 + /** 211 + * i2c_acpi_register_devices - enumerate I2C slave devices behind adapter 212 + * @adap: pointer to adapter 213 + * 214 + * Enumerate all I2C slave devices behind this adapter by walking the ACPI 215 + * namespace. When a device is found it will be added to the Linux device 216 + * model and bound to the corresponding ACPI handle. 217 + */ 218 + void i2c_acpi_register_devices(struct i2c_adapter *adap) 219 + { 220 + acpi_status status; 221 + 222 + if (!has_acpi_companion(&adap->dev)) 223 + return; 224 + 225 + status = acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT, 226 + I2C_ACPI_MAX_SCAN_DEPTH, 227 + i2c_acpi_add_device, NULL, 228 + adap, NULL); 229 + if (ACPI_FAILURE(status)) 230 + dev_warn(&adap->dev, "failed to enumerate I2C slaves\n"); 231 + } 232 + 233 + static acpi_status i2c_acpi_lookup_speed(acpi_handle handle, u32 level, 234 + void *data, void **return_value) 235 + { 236 + struct i2c_acpi_lookup *lookup = data; 237 + struct acpi_device *adev; 238 + 239 + if (acpi_bus_get_device(handle, &adev)) 240 + return AE_OK; 241 + 242 + if (i2c_acpi_do_lookup(adev, lookup)) 243 + return AE_OK; 244 + 245 + if (lookup->search_handle != lookup->adapter_handle) 246 + return AE_OK; 247 + 248 + if (lookup->speed <= lookup->min_speed) 249 + lookup->min_speed = lookup->speed; 250 + 251 + return AE_OK; 252 + } 253 + 254 + /** 255 + * i2c_acpi_find_bus_speed - find I2C bus speed from ACPI 256 + * @dev: The device owning the bus 257 + * 258 + * Find the I2C bus speed by walking the ACPI namespace for all I2C slaves 259 + * devices connected to this bus and use the speed of slowest device. 260 + * 261 + * Returns the speed in Hz or zero 262 + */ 263 + u32 i2c_acpi_find_bus_speed(struct device *dev) 264 + { 265 + struct i2c_acpi_lookup lookup; 266 + struct i2c_board_info dummy; 267 + acpi_status status; 268 + 269 + if (!has_acpi_companion(dev)) 270 + return 0; 271 + 272 + memset(&lookup, 0, sizeof(lookup)); 273 + lookup.search_handle = ACPI_HANDLE(dev); 274 + lookup.min_speed = UINT_MAX; 275 + lookup.info = &dummy; 276 + lookup.index = -1; 277 + 278 + status = acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT, 279 + I2C_ACPI_MAX_SCAN_DEPTH, 280 + i2c_acpi_lookup_speed, NULL, 281 + &lookup, NULL); 282 + 283 + if (ACPI_FAILURE(status)) { 284 + dev_warn(dev, "unable to find I2C bus speed from ACPI\n"); 285 + return 0; 286 + } 287 + 288 + return lookup.min_speed != UINT_MAX ? lookup.min_speed : 0; 289 + } 290 + EXPORT_SYMBOL_GPL(i2c_acpi_find_bus_speed); 291 + 292 + static int i2c_acpi_match_adapter(struct device *dev, void *data) 293 + { 294 + struct i2c_adapter *adapter = i2c_verify_adapter(dev); 295 + 296 + if (!adapter) 297 + return 0; 298 + 299 + return ACPI_HANDLE(dev) == (acpi_handle)data; 300 + } 301 + 302 + static int i2c_acpi_match_device(struct device *dev, void *data) 303 + { 304 + return ACPI_COMPANION(dev) == data; 305 + } 306 + 307 + static struct i2c_adapter *i2c_acpi_find_adapter_by_handle(acpi_handle handle) 308 + { 309 + struct device *dev; 310 + 311 + dev = bus_find_device(&i2c_bus_type, NULL, handle, 312 + i2c_acpi_match_adapter); 313 + return dev ? i2c_verify_adapter(dev) : NULL; 314 + } 315 + 316 + static struct i2c_client *i2c_acpi_find_client_by_adev(struct acpi_device *adev) 317 + { 318 + struct device *dev; 319 + 320 + dev = bus_find_device(&i2c_bus_type, NULL, adev, i2c_acpi_match_device); 321 + return dev ? i2c_verify_client(dev) : NULL; 322 + } 323 + 324 + static int i2c_acpi_notify(struct notifier_block *nb, unsigned long value, 325 + void *arg) 326 + { 327 + struct acpi_device *adev = arg; 328 + struct i2c_board_info info; 329 + acpi_handle adapter_handle; 330 + struct i2c_adapter *adapter; 331 + struct i2c_client *client; 332 + 333 + switch (value) { 334 + case ACPI_RECONFIG_DEVICE_ADD: 335 + if (i2c_acpi_get_info(adev, &info, NULL, &adapter_handle)) 336 + break; 337 + 338 + adapter = i2c_acpi_find_adapter_by_handle(adapter_handle); 339 + if (!adapter) 340 + break; 341 + 342 + i2c_acpi_register_device(adapter, adev, &info); 343 + break; 344 + case ACPI_RECONFIG_DEVICE_REMOVE: 345 + if (!acpi_device_enumerated(adev)) 346 + break; 347 + 348 + client = i2c_acpi_find_client_by_adev(adev); 349 + if (!client) 350 + break; 351 + 352 + i2c_unregister_device(client); 353 + put_device(&client->dev); 354 + break; 355 + } 356 + 357 + return NOTIFY_OK; 358 + } 359 + 360 + struct notifier_block i2c_acpi_notifier = { 361 + .notifier_call = i2c_acpi_notify, 362 + }; 363 + 364 + /** 365 + * i2c_acpi_new_device - Create i2c-client for the Nth I2cSerialBus resource 366 + * @dev: Device owning the ACPI resources to get the client from 367 + * @index: Index of ACPI resource to get 368 + * @info: describes the I2C device; note this is modified (addr gets set) 369 + * Context: can sleep 370 + * 371 + * By default the i2c subsys creates an i2c-client for the first I2cSerialBus 372 + * resource of an acpi_device, but some acpi_devices have multiple I2cSerialBus 373 + * resources, in that case this function can be used to create an i2c-client 374 + * for other I2cSerialBus resources in the Current Resource Settings table. 375 + * 376 + * Also see i2c_new_device, which this function calls to create the i2c-client. 377 + * 378 + * Returns a pointer to the new i2c-client, or NULL if the adapter is not found. 379 + */ 380 + struct i2c_client *i2c_acpi_new_device(struct device *dev, int index, 381 + struct i2c_board_info *info) 382 + { 383 + struct i2c_acpi_lookup lookup; 384 + struct i2c_adapter *adapter; 385 + struct acpi_device *adev; 386 + LIST_HEAD(resource_list); 387 + int ret; 388 + 389 + adev = ACPI_COMPANION(dev); 390 + if (!adev) 391 + return NULL; 392 + 393 + memset(&lookup, 0, sizeof(lookup)); 394 + lookup.info = info; 395 + lookup.device_handle = acpi_device_handle(adev); 396 + lookup.index = index; 397 + 398 + ret = acpi_dev_get_resources(adev, &resource_list, 399 + i2c_acpi_fill_info, &lookup); 400 + acpi_dev_free_resource_list(&resource_list); 401 + 402 + if (ret < 0 || !info->addr) 403 + return NULL; 404 + 405 + adapter = i2c_acpi_find_adapter_by_handle(lookup.adapter_handle); 406 + if (!adapter) 407 + return NULL; 408 + 409 + return i2c_new_device(adapter, info); 410 + } 411 + EXPORT_SYMBOL_GPL(i2c_acpi_new_device); 412 + 413 + #ifdef CONFIG_ACPI_I2C_OPREGION 414 + static int acpi_gsb_i2c_read_bytes(struct i2c_client *client, 415 + u8 cmd, u8 *data, u8 data_len) 416 + { 417 + 418 + struct i2c_msg msgs[2]; 419 + int ret; 420 + u8 *buffer; 421 + 422 + buffer = kzalloc(data_len, GFP_KERNEL); 423 + if (!buffer) 424 + return AE_NO_MEMORY; 425 + 426 + msgs[0].addr = client->addr; 427 + msgs[0].flags = client->flags; 428 + msgs[0].len = 1; 429 + msgs[0].buf = &cmd; 430 + 431 + msgs[1].addr = client->addr; 432 + msgs[1].flags = client->flags | I2C_M_RD; 433 + msgs[1].len = data_len; 434 + msgs[1].buf = buffer; 435 + 436 + ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs)); 437 + if (ret < 0) 438 + dev_err(&client->adapter->dev, "i2c read failed\n"); 439 + else 440 + memcpy(data, buffer, data_len); 441 + 442 + kfree(buffer); 443 + return ret; 444 + } 445 + 446 + static int acpi_gsb_i2c_write_bytes(struct i2c_client *client, 447 + u8 cmd, u8 *data, u8 data_len) 448 + { 449 + 450 + struct i2c_msg msgs[1]; 451 + u8 *buffer; 452 + int ret = AE_OK; 453 + 454 + buffer = kzalloc(data_len + 1, GFP_KERNEL); 455 + if (!buffer) 456 + return AE_NO_MEMORY; 457 + 458 + buffer[0] = cmd; 459 + memcpy(buffer + 1, data, data_len); 460 + 461 + msgs[0].addr = client->addr; 462 + msgs[0].flags = client->flags; 463 + msgs[0].len = data_len + 1; 464 + msgs[0].buf = buffer; 465 + 466 + ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs)); 467 + if (ret < 0) 468 + dev_err(&client->adapter->dev, "i2c write failed\n"); 469 + 470 + kfree(buffer); 471 + return ret; 472 + } 473 + 474 + static acpi_status 475 + i2c_acpi_space_handler(u32 function, acpi_physical_address command, 476 + u32 bits, u64 *value64, 477 + void *handler_context, void *region_context) 478 + { 479 + struct gsb_buffer *gsb = (struct gsb_buffer *)value64; 480 + struct i2c_acpi_handler_data *data = handler_context; 481 + struct acpi_connection_info *info = &data->info; 482 + struct acpi_resource_i2c_serialbus *sb; 483 + struct i2c_adapter *adapter = data->adapter; 484 + struct i2c_client *client; 485 + struct acpi_resource *ares; 486 + u32 accessor_type = function >> 16; 487 + u8 action = function & ACPI_IO_MASK; 488 + acpi_status ret; 489 + int status; 490 + 491 + ret = acpi_buffer_to_resource(info->connection, info->length, &ares); 492 + if (ACPI_FAILURE(ret)) 493 + return ret; 494 + 495 + client = kzalloc(sizeof(*client), GFP_KERNEL); 496 + if (!client) { 497 + ret = AE_NO_MEMORY; 498 + goto err; 499 + } 500 + 501 + if (!value64 || ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS) { 502 + ret = AE_BAD_PARAMETER; 503 + goto err; 504 + } 505 + 506 + sb = &ares->data.i2c_serial_bus; 507 + if (sb->type != ACPI_RESOURCE_SERIAL_TYPE_I2C) { 508 + ret = AE_BAD_PARAMETER; 509 + goto err; 510 + } 511 + 512 + client->adapter = adapter; 513 + client->addr = sb->slave_address; 514 + 515 + if (sb->access_mode == ACPI_I2C_10BIT_MODE) 516 + client->flags |= I2C_CLIENT_TEN; 517 + 518 + switch (accessor_type) { 519 + case ACPI_GSB_ACCESS_ATTRIB_SEND_RCV: 520 + if (action == ACPI_READ) { 521 + status = i2c_smbus_read_byte(client); 522 + if (status >= 0) { 523 + gsb->bdata = status; 524 + status = 0; 525 + } 526 + } else { 527 + status = i2c_smbus_write_byte(client, gsb->bdata); 528 + } 529 + break; 530 + 531 + case ACPI_GSB_ACCESS_ATTRIB_BYTE: 532 + if (action == ACPI_READ) { 533 + status = i2c_smbus_read_byte_data(client, command); 534 + if (status >= 0) { 535 + gsb->bdata = status; 536 + status = 0; 537 + } 538 + } else { 539 + status = i2c_smbus_write_byte_data(client, command, 540 + gsb->bdata); 541 + } 542 + break; 543 + 544 + case ACPI_GSB_ACCESS_ATTRIB_WORD: 545 + if (action == ACPI_READ) { 546 + status = i2c_smbus_read_word_data(client, command); 547 + if (status >= 0) { 548 + gsb->wdata = status; 549 + status = 0; 550 + } 551 + } else { 552 + status = i2c_smbus_write_word_data(client, command, 553 + gsb->wdata); 554 + } 555 + break; 556 + 557 + case ACPI_GSB_ACCESS_ATTRIB_BLOCK: 558 + if (action == ACPI_READ) { 559 + status = i2c_smbus_read_block_data(client, command, 560 + gsb->data); 561 + if (status >= 0) { 562 + gsb->len = status; 563 + status = 0; 564 + } 565 + } else { 566 + status = i2c_smbus_write_block_data(client, command, 567 + gsb->len, gsb->data); 568 + } 569 + break; 570 + 571 + case ACPI_GSB_ACCESS_ATTRIB_MULTIBYTE: 572 + if (action == ACPI_READ) { 573 + status = acpi_gsb_i2c_read_bytes(client, command, 574 + gsb->data, info->access_length); 575 + if (status > 0) 576 + status = 0; 577 + } else { 578 + status = acpi_gsb_i2c_write_bytes(client, command, 579 + gsb->data, info->access_length); 580 + } 581 + break; 582 + 583 + default: 584 + dev_warn(&adapter->dev, "protocol 0x%02x not supported for client 0x%02x\n", 585 + accessor_type, client->addr); 586 + ret = AE_BAD_PARAMETER; 587 + goto err; 588 + } 589 + 590 + gsb->status = status; 591 + 592 + err: 593 + kfree(client); 594 + ACPI_FREE(ares); 595 + return ret; 596 + } 597 + 598 + 599 + int i2c_acpi_install_space_handler(struct i2c_adapter *adapter) 600 + { 601 + acpi_handle handle; 602 + struct i2c_acpi_handler_data *data; 603 + acpi_status status; 604 + 605 + if (!adapter->dev.parent) 606 + return -ENODEV; 607 + 608 + handle = ACPI_HANDLE(adapter->dev.parent); 609 + 610 + if (!handle) 611 + return -ENODEV; 612 + 613 + data = kzalloc(sizeof(struct i2c_acpi_handler_data), 614 + GFP_KERNEL); 615 + if (!data) 616 + return -ENOMEM; 617 + 618 + data->adapter = adapter; 619 + status = acpi_bus_attach_private_data(handle, (void *)data); 620 + if (ACPI_FAILURE(status)) { 621 + kfree(data); 622 + return -ENOMEM; 623 + } 624 + 625 + status = acpi_install_address_space_handler(handle, 626 + ACPI_ADR_SPACE_GSBUS, 627 + &i2c_acpi_space_handler, 628 + NULL, 629 + data); 630 + if (ACPI_FAILURE(status)) { 631 + dev_err(&adapter->dev, "Error installing i2c space handler\n"); 632 + acpi_bus_detach_private_data(handle); 633 + kfree(data); 634 + return -ENOMEM; 635 + } 636 + 637 + acpi_walk_dep_device_list(handle); 638 + return 0; 639 + } 640 + 641 + void i2c_acpi_remove_space_handler(struct i2c_adapter *adapter) 642 + { 643 + acpi_handle handle; 644 + struct i2c_acpi_handler_data *data; 645 + acpi_status status; 646 + 647 + if (!adapter->dev.parent) 648 + return; 649 + 650 + handle = ACPI_HANDLE(adapter->dev.parent); 651 + 652 + if (!handle) 653 + return; 654 + 655 + acpi_remove_address_space_handler(handle, 656 + ACPI_ADR_SPACE_GSBUS, 657 + &i2c_acpi_space_handler); 658 + 659 + status = acpi_bus_get_private_data(handle, (void **)&data); 660 + if (ACPI_SUCCESS(status)) 661 + kfree(data); 662 + 663 + acpi_bus_detach_private_data(handle); 664 + } 665 + #endif /* CONFIG_ACPI_I2C_OPREGION */
+276
drivers/i2c/i2c-core-of.c
··· 1 + /* 2 + * Linux I2C core OF support code 3 + * 4 + * Copyright (C) 2008 Jochen Friedrich <jochen@scram.de> 5 + * based on a previous patch from Jon Smirl <jonsmirl@gmail.com> 6 + * 7 + * Copyright (C) 2013 Wolfram Sang <wsa@the-dreams.de> 8 + * 9 + * This program is free software; you can redistribute it and/or modify it 10 + * under the terms of the GNU General Public License as published by the Free 11 + * Software Foundation; either version 2 of the License, or (at your option) 12 + * any later version. 13 + */ 14 + 15 + #include <dt-bindings/i2c/i2c.h> 16 + #include <linux/device.h> 17 + #include <linux/err.h> 18 + #include <linux/i2c.h> 19 + #include <linux/module.h> 20 + #include <linux/of.h> 21 + #include <linux/of_device.h> 22 + 23 + #include "i2c-core.h" 24 + 25 + static struct i2c_client *of_i2c_register_device(struct i2c_adapter *adap, 26 + struct device_node *node) 27 + { 28 + struct i2c_client *result; 29 + struct i2c_board_info info = {}; 30 + struct dev_archdata dev_ad = {}; 31 + const __be32 *addr_be; 32 + u32 addr; 33 + int len; 34 + 35 + dev_dbg(&adap->dev, "of_i2c: register %s\n", node->full_name); 36 + 37 + if (of_modalias_node(node, info.type, sizeof(info.type)) < 0) { 38 + dev_err(&adap->dev, "of_i2c: modalias failure on %s\n", 39 + node->full_name); 40 + return ERR_PTR(-EINVAL); 41 + } 42 + 43 + addr_be = of_get_property(node, "reg", &len); 44 + if (!addr_be || (len < sizeof(*addr_be))) { 45 + dev_err(&adap->dev, "of_i2c: invalid reg on %s\n", 46 + node->full_name); 47 + return ERR_PTR(-EINVAL); 48 + } 49 + 50 + addr = be32_to_cpup(addr_be); 51 + if (addr & I2C_TEN_BIT_ADDRESS) { 52 + addr &= ~I2C_TEN_BIT_ADDRESS; 53 + info.flags |= I2C_CLIENT_TEN; 54 + } 55 + 56 + if (addr & I2C_OWN_SLAVE_ADDRESS) { 57 + addr &= ~I2C_OWN_SLAVE_ADDRESS; 58 + info.flags |= I2C_CLIENT_SLAVE; 59 + } 60 + 61 + if (i2c_check_addr_validity(addr, info.flags)) { 62 + dev_err(&adap->dev, "of_i2c: invalid addr=%x on %s\n", 63 + addr, node->full_name); 64 + return ERR_PTR(-EINVAL); 65 + } 66 + 67 + info.addr = addr; 68 + info.of_node = of_node_get(node); 69 + info.archdata = &dev_ad; 70 + 71 + if (of_property_read_bool(node, "host-notify")) 72 + info.flags |= I2C_CLIENT_HOST_NOTIFY; 73 + 74 + if (of_get_property(node, "wakeup-source", NULL)) 75 + info.flags |= I2C_CLIENT_WAKE; 76 + 77 + result = i2c_new_device(adap, &info); 78 + if (result == NULL) { 79 + dev_err(&adap->dev, "of_i2c: Failure registering %s\n", 80 + node->full_name); 81 + of_node_put(node); 82 + return ERR_PTR(-EINVAL); 83 + } 84 + return result; 85 + } 86 + 87 + void of_i2c_register_devices(struct i2c_adapter *adap) 88 + { 89 + struct device_node *bus, *node; 90 + struct i2c_client *client; 91 + 92 + /* Only register child devices if the adapter has a node pointer set */ 93 + if (!adap->dev.of_node) 94 + return; 95 + 96 + dev_dbg(&adap->dev, "of_i2c: walking child nodes\n"); 97 + 98 + bus = of_get_child_by_name(adap->dev.of_node, "i2c-bus"); 99 + if (!bus) 100 + bus = of_node_get(adap->dev.of_node); 101 + 102 + for_each_available_child_of_node(bus, node) { 103 + if (of_node_test_and_set_flag(node, OF_POPULATED)) 104 + continue; 105 + 106 + client = of_i2c_register_device(adap, node); 107 + if (IS_ERR(client)) { 108 + dev_warn(&adap->dev, 109 + "Failed to create I2C device for %s\n", 110 + node->full_name); 111 + of_node_clear_flag(node, OF_POPULATED); 112 + } 113 + } 114 + 115 + of_node_put(bus); 116 + } 117 + 118 + static int of_dev_node_match(struct device *dev, void *data) 119 + { 120 + return dev->of_node == data; 121 + } 122 + 123 + /* must call put_device() when done with returned i2c_client device */ 124 + struct i2c_client *of_find_i2c_device_by_node(struct device_node *node) 125 + { 126 + struct device *dev; 127 + struct i2c_client *client; 128 + 129 + dev = bus_find_device(&i2c_bus_type, NULL, node, of_dev_node_match); 130 + if (!dev) 131 + return NULL; 132 + 133 + client = i2c_verify_client(dev); 134 + if (!client) 135 + put_device(dev); 136 + 137 + return client; 138 + } 139 + EXPORT_SYMBOL(of_find_i2c_device_by_node); 140 + 141 + /* must call put_device() when done with returned i2c_adapter device */ 142 + struct i2c_adapter *of_find_i2c_adapter_by_node(struct device_node *node) 143 + { 144 + struct device *dev; 145 + struct i2c_adapter *adapter; 146 + 147 + dev = bus_find_device(&i2c_bus_type, NULL, node, of_dev_node_match); 148 + if (!dev) 149 + return NULL; 150 + 151 + adapter = i2c_verify_adapter(dev); 152 + if (!adapter) 153 + put_device(dev); 154 + 155 + return adapter; 156 + } 157 + EXPORT_SYMBOL(of_find_i2c_adapter_by_node); 158 + 159 + /* must call i2c_put_adapter() when done with returned i2c_adapter device */ 160 + struct i2c_adapter *of_get_i2c_adapter_by_node(struct device_node *node) 161 + { 162 + struct i2c_adapter *adapter; 163 + 164 + adapter = of_find_i2c_adapter_by_node(node); 165 + if (!adapter) 166 + return NULL; 167 + 168 + if (!try_module_get(adapter->owner)) { 169 + put_device(&adapter->dev); 170 + adapter = NULL; 171 + } 172 + 173 + return adapter; 174 + } 175 + EXPORT_SYMBOL(of_get_i2c_adapter_by_node); 176 + 177 + static const struct of_device_id* 178 + i2c_of_match_device_sysfs(const struct of_device_id *matches, 179 + struct i2c_client *client) 180 + { 181 + const char *name; 182 + 183 + for (; matches->compatible[0]; matches++) { 184 + /* 185 + * Adding devices through the i2c sysfs interface provides us 186 + * a string to match which may be compatible with the device 187 + * tree compatible strings, however with no actual of_node the 188 + * of_match_device() will not match 189 + */ 190 + if (sysfs_streq(client->name, matches->compatible)) 191 + return matches; 192 + 193 + name = strchr(matches->compatible, ','); 194 + if (!name) 195 + name = matches->compatible; 196 + else 197 + name++; 198 + 199 + if (sysfs_streq(client->name, name)) 200 + return matches; 201 + } 202 + 203 + return NULL; 204 + } 205 + 206 + const struct of_device_id 207 + *i2c_of_match_device(const struct of_device_id *matches, 208 + struct i2c_client *client) 209 + { 210 + const struct of_device_id *match; 211 + 212 + if (!(client && matches)) 213 + return NULL; 214 + 215 + match = of_match_device(matches, &client->dev); 216 + if (match) 217 + return match; 218 + 219 + return i2c_of_match_device_sysfs(matches, client); 220 + } 221 + EXPORT_SYMBOL_GPL(i2c_of_match_device); 222 + 223 + #if IS_ENABLED(CONFIG_OF_DYNAMIC) 224 + static int of_i2c_notify(struct notifier_block *nb, unsigned long action, 225 + void *arg) 226 + { 227 + struct of_reconfig_data *rd = arg; 228 + struct i2c_adapter *adap; 229 + struct i2c_client *client; 230 + 231 + switch (of_reconfig_get_state_change(action, rd)) { 232 + case OF_RECONFIG_CHANGE_ADD: 233 + adap = of_find_i2c_adapter_by_node(rd->dn->parent); 234 + if (adap == NULL) 235 + return NOTIFY_OK; /* not for us */ 236 + 237 + if (of_node_test_and_set_flag(rd->dn, OF_POPULATED)) { 238 + put_device(&adap->dev); 239 + return NOTIFY_OK; 240 + } 241 + 242 + client = of_i2c_register_device(adap, rd->dn); 243 + put_device(&adap->dev); 244 + 245 + if (IS_ERR(client)) { 246 + dev_err(&adap->dev, "failed to create client for '%s'\n", 247 + rd->dn->full_name); 248 + of_node_clear_flag(rd->dn, OF_POPULATED); 249 + return notifier_from_errno(PTR_ERR(client)); 250 + } 251 + break; 252 + case OF_RECONFIG_CHANGE_REMOVE: 253 + /* already depopulated? */ 254 + if (!of_node_check_flag(rd->dn, OF_POPULATED)) 255 + return NOTIFY_OK; 256 + 257 + /* find our device by node */ 258 + client = of_find_i2c_device_by_node(rd->dn); 259 + if (client == NULL) 260 + return NOTIFY_OK; /* no? not meant for us */ 261 + 262 + /* unregister takes one ref away */ 263 + i2c_unregister_device(client); 264 + 265 + /* and put the reference of the find */ 266 + put_device(&client->dev); 267 + break; 268 + } 269 + 270 + return NOTIFY_OK; 271 + } 272 + 273 + struct notifier_block i2c_of_notifier = { 274 + .notifier_call = of_i2c_notify, 275 + }; 276 + #endif /* CONFIG_OF_DYNAMIC */
+115
drivers/i2c/i2c-core-slave.c
··· 1 + /* 2 + * Linux I2C core slave support code 3 + * 4 + * Copyright (C) 2014 by Wolfram Sang <wsa@sang-engineering.com> 5 + * 6 + * This program is free software; you can redistribute it and/or modify it 7 + * under the terms of the GNU General Public License as published by the Free 8 + * Software Foundation; either version 2 of the License, or (at your option) 9 + * any later version. 10 + */ 11 + 12 + #include <dt-bindings/i2c/i2c.h> 13 + #include <linux/acpi.h> 14 + #include <linux/device.h> 15 + #include <linux/err.h> 16 + #include <linux/i2c.h> 17 + #include <linux/of.h> 18 + 19 + #include "i2c-core.h" 20 + 21 + int i2c_slave_register(struct i2c_client *client, i2c_slave_cb_t slave_cb) 22 + { 23 + int ret; 24 + 25 + if (!client || !slave_cb) { 26 + WARN(1, "insufficient data\n"); 27 + return -EINVAL; 28 + } 29 + 30 + if (!(client->flags & I2C_CLIENT_SLAVE)) 31 + dev_warn(&client->dev, "%s: client slave flag not set. You might see address collisions\n", 32 + __func__); 33 + 34 + if (!(client->flags & I2C_CLIENT_TEN)) { 35 + /* Enforce stricter address checking */ 36 + ret = i2c_check_7bit_addr_validity_strict(client->addr); 37 + if (ret) { 38 + dev_err(&client->dev, "%s: invalid address\n", __func__); 39 + return ret; 40 + } 41 + } 42 + 43 + if (!client->adapter->algo->reg_slave) { 44 + dev_err(&client->dev, "%s: not supported by adapter\n", __func__); 45 + return -EOPNOTSUPP; 46 + } 47 + 48 + client->slave_cb = slave_cb; 49 + 50 + i2c_lock_adapter(client->adapter); 51 + ret = client->adapter->algo->reg_slave(client); 52 + i2c_unlock_adapter(client->adapter); 53 + 54 + if (ret) { 55 + client->slave_cb = NULL; 56 + dev_err(&client->dev, "%s: adapter returned error %d\n", __func__, ret); 57 + } 58 + 59 + return ret; 60 + } 61 + EXPORT_SYMBOL_GPL(i2c_slave_register); 62 + 63 + int i2c_slave_unregister(struct i2c_client *client) 64 + { 65 + int ret; 66 + 67 + if (!client->adapter->algo->unreg_slave) { 68 + dev_err(&client->dev, "%s: not supported by adapter\n", __func__); 69 + return -EOPNOTSUPP; 70 + } 71 + 72 + i2c_lock_adapter(client->adapter); 73 + ret = client->adapter->algo->unreg_slave(client); 74 + i2c_unlock_adapter(client->adapter); 75 + 76 + if (ret == 0) 77 + client->slave_cb = NULL; 78 + else 79 + dev_err(&client->dev, "%s: adapter returned error %d\n", __func__, ret); 80 + 81 + return ret; 82 + } 83 + EXPORT_SYMBOL_GPL(i2c_slave_unregister); 84 + 85 + /** 86 + * i2c_detect_slave_mode - detect operation mode 87 + * @dev: The device owning the bus 88 + * 89 + * This checks the device nodes for an I2C slave by checking the address 90 + * used in the reg property. If the address match the I2C_OWN_SLAVE_ADDRESS 91 + * flag this means the device is configured to act as a I2C slave and it will 92 + * be listening at that address. 93 + * 94 + * Returns true if an I2C own slave address is detected, otherwise returns 95 + * false. 96 + */ 97 + bool i2c_detect_slave_mode(struct device *dev) 98 + { 99 + if (IS_BUILTIN(CONFIG_OF) && dev->of_node) { 100 + struct device_node *child; 101 + u32 reg; 102 + 103 + for_each_child_of_node(dev->of_node, child) { 104 + of_property_read_u32(child, "reg", &reg); 105 + if (reg & I2C_OWN_SLAVE_ADDRESS) { 106 + of_node_put(child); 107 + return true; 108 + } 109 + } 110 + } else if (IS_BUILTIN(CONFIG_ACPI) && ACPI_HANDLE(dev)) { 111 + dev_dbg(dev, "ACPI slave is not supported yet\n"); 112 + } 113 + return false; 114 + } 115 + EXPORT_SYMBOL_GPL(i2c_detect_slave_mode);
+594
drivers/i2c/i2c-core-smbus.c
··· 1 + /* 2 + * Linux I2C core SMBus and SMBus emulation code 3 + * 4 + * This file contains the SMBus functions which are always included in the I2C 5 + * core because they can be emulated via I2C. SMBus specific extensions 6 + * (e.g. smbalert) are handled in a seperate i2c-smbus module. 7 + * 8 + * All SMBus-related things are written by Frodo Looijaard <frodol@dds.nl> 9 + * SMBus 2.0 support by Mark Studebaker <mdsxyz123@yahoo.com> and 10 + * Jean Delvare <jdelvare@suse.de> 11 + * 12 + * This program is free software; you can redistribute it and/or modify it 13 + * under the terms of the GNU General Public License as published by the Free 14 + * Software Foundation; either version 2 of the License, or (at your option) 15 + * any later version. 16 + */ 17 + #include <linux/device.h> 18 + #include <linux/err.h> 19 + #include <linux/i2c.h> 20 + 21 + #define CREATE_TRACE_POINTS 22 + #include <trace/events/smbus.h> 23 + 24 + 25 + /* The SMBus parts */ 26 + 27 + #define POLY (0x1070U << 3) 28 + static u8 crc8(u16 data) 29 + { 30 + int i; 31 + 32 + for (i = 0; i < 8; i++) { 33 + if (data & 0x8000) 34 + data = data ^ POLY; 35 + data = data << 1; 36 + } 37 + return (u8)(data >> 8); 38 + } 39 + 40 + /* Incremental CRC8 over count bytes in the array pointed to by p */ 41 + static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count) 42 + { 43 + int i; 44 + 45 + for (i = 0; i < count; i++) 46 + crc = crc8((crc ^ p[i]) << 8); 47 + return crc; 48 + } 49 + 50 + /* Assume a 7-bit address, which is reasonable for SMBus */ 51 + static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg) 52 + { 53 + /* The address will be sent first */ 54 + u8 addr = i2c_8bit_addr_from_msg(msg); 55 + pec = i2c_smbus_pec(pec, &addr, 1); 56 + 57 + /* The data buffer follows */ 58 + return i2c_smbus_pec(pec, msg->buf, msg->len); 59 + } 60 + 61 + /* Used for write only transactions */ 62 + static inline void i2c_smbus_add_pec(struct i2c_msg *msg) 63 + { 64 + msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg); 65 + msg->len++; 66 + } 67 + 68 + /* Return <0 on CRC error 69 + If there was a write before this read (most cases) we need to take the 70 + partial CRC from the write part into account. 71 + Note that this function does modify the message (we need to decrease the 72 + message length to hide the CRC byte from the caller). */ 73 + static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg) 74 + { 75 + u8 rpec = msg->buf[--msg->len]; 76 + cpec = i2c_smbus_msg_pec(cpec, msg); 77 + 78 + if (rpec != cpec) { 79 + pr_debug("Bad PEC 0x%02x vs. 0x%02x\n", 80 + rpec, cpec); 81 + return -EBADMSG; 82 + } 83 + return 0; 84 + } 85 + 86 + /** 87 + * i2c_smbus_read_byte - SMBus "receive byte" protocol 88 + * @client: Handle to slave device 89 + * 90 + * This executes the SMBus "receive byte" protocol, returning negative errno 91 + * else the byte received from the device. 92 + */ 93 + s32 i2c_smbus_read_byte(const struct i2c_client *client) 94 + { 95 + union i2c_smbus_data data; 96 + int status; 97 + 98 + status = i2c_smbus_xfer(client->adapter, client->addr, client->flags, 99 + I2C_SMBUS_READ, 0, 100 + I2C_SMBUS_BYTE, &data); 101 + return (status < 0) ? status : data.byte; 102 + } 103 + EXPORT_SYMBOL(i2c_smbus_read_byte); 104 + 105 + /** 106 + * i2c_smbus_write_byte - SMBus "send byte" protocol 107 + * @client: Handle to slave device 108 + * @value: Byte to be sent 109 + * 110 + * This executes the SMBus "send byte" protocol, returning negative errno 111 + * else zero on success. 112 + */ 113 + s32 i2c_smbus_write_byte(const struct i2c_client *client, u8 value) 114 + { 115 + return i2c_smbus_xfer(client->adapter, client->addr, client->flags, 116 + I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL); 117 + } 118 + EXPORT_SYMBOL(i2c_smbus_write_byte); 119 + 120 + /** 121 + * i2c_smbus_read_byte_data - SMBus "read byte" protocol 122 + * @client: Handle to slave device 123 + * @command: Byte interpreted by slave 124 + * 125 + * This executes the SMBus "read byte" protocol, returning negative errno 126 + * else a data byte received from the device. 127 + */ 128 + s32 i2c_smbus_read_byte_data(const struct i2c_client *client, u8 command) 129 + { 130 + union i2c_smbus_data data; 131 + int status; 132 + 133 + status = i2c_smbus_xfer(client->adapter, client->addr, client->flags, 134 + I2C_SMBUS_READ, command, 135 + I2C_SMBUS_BYTE_DATA, &data); 136 + return (status < 0) ? status : data.byte; 137 + } 138 + EXPORT_SYMBOL(i2c_smbus_read_byte_data); 139 + 140 + /** 141 + * i2c_smbus_write_byte_data - SMBus "write byte" protocol 142 + * @client: Handle to slave device 143 + * @command: Byte interpreted by slave 144 + * @value: Byte being written 145 + * 146 + * This executes the SMBus "write byte" protocol, returning negative errno 147 + * else zero on success. 148 + */ 149 + s32 i2c_smbus_write_byte_data(const struct i2c_client *client, u8 command, 150 + u8 value) 151 + { 152 + union i2c_smbus_data data; 153 + data.byte = value; 154 + return i2c_smbus_xfer(client->adapter, client->addr, client->flags, 155 + I2C_SMBUS_WRITE, command, 156 + I2C_SMBUS_BYTE_DATA, &data); 157 + } 158 + EXPORT_SYMBOL(i2c_smbus_write_byte_data); 159 + 160 + /** 161 + * i2c_smbus_read_word_data - SMBus "read word" protocol 162 + * @client: Handle to slave device 163 + * @command: Byte interpreted by slave 164 + * 165 + * This executes the SMBus "read word" protocol, returning negative errno 166 + * else a 16-bit unsigned "word" received from the device. 167 + */ 168 + s32 i2c_smbus_read_word_data(const struct i2c_client *client, u8 command) 169 + { 170 + union i2c_smbus_data data; 171 + int status; 172 + 173 + status = i2c_smbus_xfer(client->adapter, client->addr, client->flags, 174 + I2C_SMBUS_READ, command, 175 + I2C_SMBUS_WORD_DATA, &data); 176 + return (status < 0) ? status : data.word; 177 + } 178 + EXPORT_SYMBOL(i2c_smbus_read_word_data); 179 + 180 + /** 181 + * i2c_smbus_write_word_data - SMBus "write word" protocol 182 + * @client: Handle to slave device 183 + * @command: Byte interpreted by slave 184 + * @value: 16-bit "word" being written 185 + * 186 + * This executes the SMBus "write word" protocol, returning negative errno 187 + * else zero on success. 188 + */ 189 + s32 i2c_smbus_write_word_data(const struct i2c_client *client, u8 command, 190 + u16 value) 191 + { 192 + union i2c_smbus_data data; 193 + data.word = value; 194 + return i2c_smbus_xfer(client->adapter, client->addr, client->flags, 195 + I2C_SMBUS_WRITE, command, 196 + I2C_SMBUS_WORD_DATA, &data); 197 + } 198 + EXPORT_SYMBOL(i2c_smbus_write_word_data); 199 + 200 + /** 201 + * i2c_smbus_read_block_data - SMBus "block read" protocol 202 + * @client: Handle to slave device 203 + * @command: Byte interpreted by slave 204 + * @values: Byte array into which data will be read; big enough to hold 205 + * the data returned by the slave. SMBus allows at most 32 bytes. 206 + * 207 + * This executes the SMBus "block read" protocol, returning negative errno 208 + * else the number of data bytes in the slave's response. 209 + * 210 + * Note that using this function requires that the client's adapter support 211 + * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers 212 + * support this; its emulation through I2C messaging relies on a specific 213 + * mechanism (I2C_M_RECV_LEN) which may not be implemented. 214 + */ 215 + s32 i2c_smbus_read_block_data(const struct i2c_client *client, u8 command, 216 + u8 *values) 217 + { 218 + union i2c_smbus_data data; 219 + int status; 220 + 221 + status = i2c_smbus_xfer(client->adapter, client->addr, client->flags, 222 + I2C_SMBUS_READ, command, 223 + I2C_SMBUS_BLOCK_DATA, &data); 224 + if (status) 225 + return status; 226 + 227 + memcpy(values, &data.block[1], data.block[0]); 228 + return data.block[0]; 229 + } 230 + EXPORT_SYMBOL(i2c_smbus_read_block_data); 231 + 232 + /** 233 + * i2c_smbus_write_block_data - SMBus "block write" protocol 234 + * @client: Handle to slave device 235 + * @command: Byte interpreted by slave 236 + * @length: Size of data block; SMBus allows at most 32 bytes 237 + * @values: Byte array which will be written. 238 + * 239 + * This executes the SMBus "block write" protocol, returning negative errno 240 + * else zero on success. 241 + */ 242 + s32 i2c_smbus_write_block_data(const struct i2c_client *client, u8 command, 243 + u8 length, const u8 *values) 244 + { 245 + union i2c_smbus_data data; 246 + 247 + if (length > I2C_SMBUS_BLOCK_MAX) 248 + length = I2C_SMBUS_BLOCK_MAX; 249 + data.block[0] = length; 250 + memcpy(&data.block[1], values, length); 251 + return i2c_smbus_xfer(client->adapter, client->addr, client->flags, 252 + I2C_SMBUS_WRITE, command, 253 + I2C_SMBUS_BLOCK_DATA, &data); 254 + } 255 + EXPORT_SYMBOL(i2c_smbus_write_block_data); 256 + 257 + /* Returns the number of read bytes */ 258 + s32 i2c_smbus_read_i2c_block_data(const struct i2c_client *client, u8 command, 259 + u8 length, u8 *values) 260 + { 261 + union i2c_smbus_data data; 262 + int status; 263 + 264 + if (length > I2C_SMBUS_BLOCK_MAX) 265 + length = I2C_SMBUS_BLOCK_MAX; 266 + data.block[0] = length; 267 + status = i2c_smbus_xfer(client->adapter, client->addr, client->flags, 268 + I2C_SMBUS_READ, command, 269 + I2C_SMBUS_I2C_BLOCK_DATA, &data); 270 + if (status < 0) 271 + return status; 272 + 273 + memcpy(values, &data.block[1], data.block[0]); 274 + return data.block[0]; 275 + } 276 + EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data); 277 + 278 + s32 i2c_smbus_write_i2c_block_data(const struct i2c_client *client, u8 command, 279 + u8 length, const u8 *values) 280 + { 281 + union i2c_smbus_data data; 282 + 283 + if (length > I2C_SMBUS_BLOCK_MAX) 284 + length = I2C_SMBUS_BLOCK_MAX; 285 + data.block[0] = length; 286 + memcpy(data.block + 1, values, length); 287 + return i2c_smbus_xfer(client->adapter, client->addr, client->flags, 288 + I2C_SMBUS_WRITE, command, 289 + I2C_SMBUS_I2C_BLOCK_DATA, &data); 290 + } 291 + EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data); 292 + 293 + /* Simulate a SMBus command using the i2c protocol 294 + No checking of parameters is done! */ 295 + static s32 i2c_smbus_xfer_emulated(struct i2c_adapter *adapter, u16 addr, 296 + unsigned short flags, 297 + char read_write, u8 command, int size, 298 + union i2c_smbus_data *data) 299 + { 300 + /* So we need to generate a series of msgs. In the case of writing, we 301 + need to use only one message; when reading, we need two. We initialize 302 + most things with sane defaults, to keep the code below somewhat 303 + simpler. */ 304 + unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3]; 305 + unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2]; 306 + int num = read_write == I2C_SMBUS_READ ? 2 : 1; 307 + int i; 308 + u8 partial_pec = 0; 309 + int status; 310 + struct i2c_msg msg[2] = { 311 + { 312 + .addr = addr, 313 + .flags = flags, 314 + .len = 1, 315 + .buf = msgbuf0, 316 + }, { 317 + .addr = addr, 318 + .flags = flags | I2C_M_RD, 319 + .len = 0, 320 + .buf = msgbuf1, 321 + }, 322 + }; 323 + 324 + msgbuf0[0] = command; 325 + switch (size) { 326 + case I2C_SMBUS_QUICK: 327 + msg[0].len = 0; 328 + /* Special case: The read/write field is used as data */ 329 + msg[0].flags = flags | (read_write == I2C_SMBUS_READ ? 330 + I2C_M_RD : 0); 331 + num = 1; 332 + break; 333 + case I2C_SMBUS_BYTE: 334 + if (read_write == I2C_SMBUS_READ) { 335 + /* Special case: only a read! */ 336 + msg[0].flags = I2C_M_RD | flags; 337 + num = 1; 338 + } 339 + break; 340 + case I2C_SMBUS_BYTE_DATA: 341 + if (read_write == I2C_SMBUS_READ) 342 + msg[1].len = 1; 343 + else { 344 + msg[0].len = 2; 345 + msgbuf0[1] = data->byte; 346 + } 347 + break; 348 + case I2C_SMBUS_WORD_DATA: 349 + if (read_write == I2C_SMBUS_READ) 350 + msg[1].len = 2; 351 + else { 352 + msg[0].len = 3; 353 + msgbuf0[1] = data->word & 0xff; 354 + msgbuf0[2] = data->word >> 8; 355 + } 356 + break; 357 + case I2C_SMBUS_PROC_CALL: 358 + num = 2; /* Special case */ 359 + read_write = I2C_SMBUS_READ; 360 + msg[0].len = 3; 361 + msg[1].len = 2; 362 + msgbuf0[1] = data->word & 0xff; 363 + msgbuf0[2] = data->word >> 8; 364 + break; 365 + case I2C_SMBUS_BLOCK_DATA: 366 + if (read_write == I2C_SMBUS_READ) { 367 + msg[1].flags |= I2C_M_RECV_LEN; 368 + msg[1].len = 1; /* block length will be added by 369 + the underlying bus driver */ 370 + } else { 371 + msg[0].len = data->block[0] + 2; 372 + if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) { 373 + dev_err(&adapter->dev, 374 + "Invalid block write size %d\n", 375 + data->block[0]); 376 + return -EINVAL; 377 + } 378 + for (i = 1; i < msg[0].len; i++) 379 + msgbuf0[i] = data->block[i-1]; 380 + } 381 + break; 382 + case I2C_SMBUS_BLOCK_PROC_CALL: 383 + num = 2; /* Another special case */ 384 + read_write = I2C_SMBUS_READ; 385 + if (data->block[0] > I2C_SMBUS_BLOCK_MAX) { 386 + dev_err(&adapter->dev, 387 + "Invalid block write size %d\n", 388 + data->block[0]); 389 + return -EINVAL; 390 + } 391 + msg[0].len = data->block[0] + 2; 392 + for (i = 1; i < msg[0].len; i++) 393 + msgbuf0[i] = data->block[i-1]; 394 + msg[1].flags |= I2C_M_RECV_LEN; 395 + msg[1].len = 1; /* block length will be added by 396 + the underlying bus driver */ 397 + break; 398 + case I2C_SMBUS_I2C_BLOCK_DATA: 399 + if (read_write == I2C_SMBUS_READ) { 400 + msg[1].len = data->block[0]; 401 + } else { 402 + msg[0].len = data->block[0] + 1; 403 + if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) { 404 + dev_err(&adapter->dev, 405 + "Invalid block write size %d\n", 406 + data->block[0]); 407 + return -EINVAL; 408 + } 409 + for (i = 1; i <= data->block[0]; i++) 410 + msgbuf0[i] = data->block[i]; 411 + } 412 + break; 413 + default: 414 + dev_err(&adapter->dev, "Unsupported transaction %d\n", size); 415 + return -EOPNOTSUPP; 416 + } 417 + 418 + i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK 419 + && size != I2C_SMBUS_I2C_BLOCK_DATA); 420 + if (i) { 421 + /* Compute PEC if first message is a write */ 422 + if (!(msg[0].flags & I2C_M_RD)) { 423 + if (num == 1) /* Write only */ 424 + i2c_smbus_add_pec(&msg[0]); 425 + else /* Write followed by read */ 426 + partial_pec = i2c_smbus_msg_pec(0, &msg[0]); 427 + } 428 + /* Ask for PEC if last message is a read */ 429 + if (msg[num-1].flags & I2C_M_RD) 430 + msg[num-1].len++; 431 + } 432 + 433 + status = i2c_transfer(adapter, msg, num); 434 + if (status < 0) 435 + return status; 436 + 437 + /* Check PEC if last message is a read */ 438 + if (i && (msg[num-1].flags & I2C_M_RD)) { 439 + status = i2c_smbus_check_pec(partial_pec, &msg[num-1]); 440 + if (status < 0) 441 + return status; 442 + } 443 + 444 + if (read_write == I2C_SMBUS_READ) 445 + switch (size) { 446 + case I2C_SMBUS_BYTE: 447 + data->byte = msgbuf0[0]; 448 + break; 449 + case I2C_SMBUS_BYTE_DATA: 450 + data->byte = msgbuf1[0]; 451 + break; 452 + case I2C_SMBUS_WORD_DATA: 453 + case I2C_SMBUS_PROC_CALL: 454 + data->word = msgbuf1[0] | (msgbuf1[1] << 8); 455 + break; 456 + case I2C_SMBUS_I2C_BLOCK_DATA: 457 + for (i = 0; i < data->block[0]; i++) 458 + data->block[i+1] = msgbuf1[i]; 459 + break; 460 + case I2C_SMBUS_BLOCK_DATA: 461 + case I2C_SMBUS_BLOCK_PROC_CALL: 462 + for (i = 0; i < msgbuf1[0] + 1; i++) 463 + data->block[i] = msgbuf1[i]; 464 + break; 465 + } 466 + return 0; 467 + } 468 + 469 + /** 470 + * i2c_smbus_xfer - execute SMBus protocol operations 471 + * @adapter: Handle to I2C bus 472 + * @addr: Address of SMBus slave on that bus 473 + * @flags: I2C_CLIENT_* flags (usually zero or I2C_CLIENT_PEC) 474 + * @read_write: I2C_SMBUS_READ or I2C_SMBUS_WRITE 475 + * @command: Byte interpreted by slave, for protocols which use such bytes 476 + * @protocol: SMBus protocol operation to execute, such as I2C_SMBUS_PROC_CALL 477 + * @data: Data to be read or written 478 + * 479 + * This executes an SMBus protocol operation, and returns a negative 480 + * errno code else zero on success. 481 + */ 482 + s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr, unsigned short flags, 483 + char read_write, u8 command, int protocol, 484 + union i2c_smbus_data *data) 485 + { 486 + unsigned long orig_jiffies; 487 + int try; 488 + s32 res; 489 + 490 + /* If enabled, the following two tracepoints are conditional on 491 + * read_write and protocol. 492 + */ 493 + trace_smbus_write(adapter, addr, flags, read_write, 494 + command, protocol, data); 495 + trace_smbus_read(adapter, addr, flags, read_write, 496 + command, protocol); 497 + 498 + flags &= I2C_M_TEN | I2C_CLIENT_PEC | I2C_CLIENT_SCCB; 499 + 500 + if (adapter->algo->smbus_xfer) { 501 + i2c_lock_bus(adapter, I2C_LOCK_SEGMENT); 502 + 503 + /* Retry automatically on arbitration loss */ 504 + orig_jiffies = jiffies; 505 + for (res = 0, try = 0; try <= adapter->retries; try++) { 506 + res = adapter->algo->smbus_xfer(adapter, addr, flags, 507 + read_write, command, 508 + protocol, data); 509 + if (res != -EAGAIN) 510 + break; 511 + if (time_after(jiffies, 512 + orig_jiffies + adapter->timeout)) 513 + break; 514 + } 515 + i2c_unlock_bus(adapter, I2C_LOCK_SEGMENT); 516 + 517 + if (res != -EOPNOTSUPP || !adapter->algo->master_xfer) 518 + goto trace; 519 + /* 520 + * Fall back to i2c_smbus_xfer_emulated if the adapter doesn't 521 + * implement native support for the SMBus operation. 522 + */ 523 + } 524 + 525 + res = i2c_smbus_xfer_emulated(adapter, addr, flags, read_write, 526 + command, protocol, data); 527 + 528 + trace: 529 + /* If enabled, the reply tracepoint is conditional on read_write. */ 530 + trace_smbus_reply(adapter, addr, flags, read_write, 531 + command, protocol, data); 532 + trace_smbus_result(adapter, addr, flags, read_write, 533 + command, protocol, res); 534 + 535 + return res; 536 + } 537 + EXPORT_SYMBOL(i2c_smbus_xfer); 538 + 539 + /** 540 + * i2c_smbus_read_i2c_block_data_or_emulated - read block or emulate 541 + * @client: Handle to slave device 542 + * @command: Byte interpreted by slave 543 + * @length: Size of data block; SMBus allows at most I2C_SMBUS_BLOCK_MAX bytes 544 + * @values: Byte array into which data will be read; big enough to hold 545 + * the data returned by the slave. SMBus allows at most 546 + * I2C_SMBUS_BLOCK_MAX bytes. 547 + * 548 + * This executes the SMBus "block read" protocol if supported by the adapter. 549 + * If block read is not supported, it emulates it using either word or byte 550 + * read protocols depending on availability. 551 + * 552 + * The addresses of the I2C slave device that are accessed with this function 553 + * must be mapped to a linear region, so that a block read will have the same 554 + * effect as a byte read. Before using this function you must double-check 555 + * if the I2C slave does support exchanging a block transfer with a byte 556 + * transfer. 557 + */ 558 + s32 i2c_smbus_read_i2c_block_data_or_emulated(const struct i2c_client *client, 559 + u8 command, u8 length, u8 *values) 560 + { 561 + u8 i = 0; 562 + int status; 563 + 564 + if (length > I2C_SMBUS_BLOCK_MAX) 565 + length = I2C_SMBUS_BLOCK_MAX; 566 + 567 + if (i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_READ_I2C_BLOCK)) 568 + return i2c_smbus_read_i2c_block_data(client, command, length, values); 569 + 570 + if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_READ_BYTE_DATA)) 571 + return -EOPNOTSUPP; 572 + 573 + if (i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_READ_WORD_DATA)) { 574 + while ((i + 2) <= length) { 575 + status = i2c_smbus_read_word_data(client, command + i); 576 + if (status < 0) 577 + return status; 578 + values[i] = status & 0xff; 579 + values[i + 1] = status >> 8; 580 + i += 2; 581 + } 582 + } 583 + 584 + while (i < length) { 585 + status = i2c_smbus_read_byte_data(client, command + i); 586 + if (status < 0) 587 + return status; 588 + values[i] = status; 589 + i++; 590 + } 591 + 592 + return i; 593 + } 594 + EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data_or_emulated);
+24 -1610
drivers/i2c/i2c-core.c drivers/i2c/i2c-core-base.c
··· 1 - /* i2c-core.c - a device driver for the iic-bus interface */ 2 - /* ------------------------------------------------------------------------- */ 3 - /* Copyright (C) 1995-99 Simon G. Vogl 4 - 5 - This program is free software; you can redistribute it and/or modify 6 - it under the terms of the GNU General Public License as published by 7 - the Free Software Foundation; either version 2 of the License, or 8 - (at your option) any later version. 9 - 10 - This program is distributed in the hope that it will be useful, 11 - but WITHOUT ANY WARRANTY; without even the implied warranty of 12 - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 - GNU General Public License for more details. */ 14 - /* ------------------------------------------------------------------------- */ 15 - 16 - /* With some changes from Kyösti Mälkki <kmalkki@cc.hut.fi>. 17 - All SMBus-related things are written by Frodo Looijaard <frodol@dds.nl> 18 - SMBus 2.0 support by Mark Studebaker <mdsxyz123@yahoo.com> and 19 - Jean Delvare <jdelvare@suse.de> 20 - Mux support by Rodolfo Giometti <giometti@enneenne.com> and 21 - Michael Lawnick <michael.lawnick.ext@nsn.com> 22 - OF support is copyright (c) 2008 Jochen Friedrich <jochen@scram.de> 23 - (based on a previous patch from Jon Smirl <jonsmirl@gmail.com>) and 24 - (c) 2013 Wolfram Sang <wsa@the-dreams.de> 25 - I2C ACPI code Copyright (C) 2014 Intel Corp 26 - Author: Lan Tianyu <tianyu.lan@intel.com> 27 - I2C slave support (c) 2014 by Wolfram Sang <wsa@sang-engineering.com> 1 + /* 2 + * Linux I2C core 3 + * 4 + * Copyright (C) 1995-99 Simon G. Vogl 5 + * With some changes from Kyösti Mälkki <kmalkki@cc.hut.fi> 6 + * Mux support by Rodolfo Giometti <giometti@enneenne.com> and 7 + * Michael Lawnick <michael.lawnick.ext@nsn.com> 8 + * 9 + * Copyright (C) 2013-2017 Wolfram Sang <wsa@the-dreams.de> 10 + * 11 + * This program is free software; you can redistribute it and/or modify it 12 + * under the terms of the GNU General Public License as published by the Free 13 + * Software Foundation; either version 2 of the License, or (at your option) 14 + * any later version. 15 + * 16 + * This program is distributed in the hope that it will be useful, but WITHOUT 17 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 18 + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. 28 19 */ 29 20 30 21 #define pr_fmt(fmt) "i2c-core: " fmt 31 22 32 23 #include <dt-bindings/i2c/i2c.h> 33 - #include <linux/uaccess.h> 34 24 #include <linux/acpi.h> 35 25 #include <linux/clk/clk-conf.h> 36 26 #include <linux/completion.h> ··· 28 38 #include <linux/err.h> 29 39 #include <linux/errno.h> 30 40 #include <linux/gpio.h> 31 - #include <linux/hardirq.h> 32 41 #include <linux/i2c.h> 33 42 #include <linux/idr.h> 34 43 #include <linux/init.h> ··· 57 68 #define I2C_ADDR_7BITS_MAX 0x77 58 69 #define I2C_ADDR_7BITS_COUNT (I2C_ADDR_7BITS_MAX + 1) 59 70 60 - /* core_lock protects i2c_adapter_idr, and guarantees 61 - that device detection, deletion of detected devices, and attach_adapter 62 - calls are serialized */ 71 + /* 72 + * core_lock protects i2c_adapter_idr, and guarantees that device detection, 73 + * deletion of detected devices, and attach_adapter calls are serialized 74 + */ 63 75 static DEFINE_MUTEX(core_lock); 64 76 static DEFINE_IDR(i2c_adapter_idr); 65 77 ··· 79 89 { 80 90 static_key_slow_dec(&i2c_trace_msg); 81 91 } 82 - 83 - #if defined(CONFIG_ACPI) 84 - struct i2c_acpi_handler_data { 85 - struct acpi_connection_info info; 86 - struct i2c_adapter *adapter; 87 - }; 88 - 89 - struct gsb_buffer { 90 - u8 status; 91 - u8 len; 92 - union { 93 - u16 wdata; 94 - u8 bdata; 95 - u8 data[0]; 96 - }; 97 - } __packed; 98 - 99 - struct i2c_acpi_lookup { 100 - struct i2c_board_info *info; 101 - acpi_handle adapter_handle; 102 - acpi_handle device_handle; 103 - acpi_handle search_handle; 104 - int n; 105 - int index; 106 - u32 speed; 107 - u32 min_speed; 108 - }; 109 - 110 - static int i2c_acpi_fill_info(struct acpi_resource *ares, void *data) 111 - { 112 - struct i2c_acpi_lookup *lookup = data; 113 - struct i2c_board_info *info = lookup->info; 114 - struct acpi_resource_i2c_serialbus *sb; 115 - acpi_status status; 116 - 117 - if (info->addr || ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS) 118 - return 1; 119 - 120 - sb = &ares->data.i2c_serial_bus; 121 - if (sb->type != ACPI_RESOURCE_SERIAL_TYPE_I2C) 122 - return 1; 123 - 124 - if (lookup->index != -1 && lookup->n++ != lookup->index) 125 - return 1; 126 - 127 - status = acpi_get_handle(lookup->device_handle, 128 - sb->resource_source.string_ptr, 129 - &lookup->adapter_handle); 130 - if (!ACPI_SUCCESS(status)) 131 - return 1; 132 - 133 - info->addr = sb->slave_address; 134 - lookup->speed = sb->connection_speed; 135 - if (sb->access_mode == ACPI_I2C_10BIT_MODE) 136 - info->flags |= I2C_CLIENT_TEN; 137 - 138 - return 1; 139 - } 140 - 141 - static int i2c_acpi_do_lookup(struct acpi_device *adev, 142 - struct i2c_acpi_lookup *lookup) 143 - { 144 - struct i2c_board_info *info = lookup->info; 145 - struct list_head resource_list; 146 - int ret; 147 - 148 - if (acpi_bus_get_status(adev) || !adev->status.present || 149 - acpi_device_enumerated(adev)) 150 - return -EINVAL; 151 - 152 - memset(info, 0, sizeof(*info)); 153 - lookup->device_handle = acpi_device_handle(adev); 154 - 155 - /* Look up for I2cSerialBus resource */ 156 - INIT_LIST_HEAD(&resource_list); 157 - ret = acpi_dev_get_resources(adev, &resource_list, 158 - i2c_acpi_fill_info, lookup); 159 - acpi_dev_free_resource_list(&resource_list); 160 - 161 - if (ret < 0 || !info->addr) 162 - return -EINVAL; 163 - 164 - return 0; 165 - } 166 - 167 - static int i2c_acpi_get_info(struct acpi_device *adev, 168 - struct i2c_board_info *info, 169 - struct i2c_adapter *adapter, 170 - acpi_handle *adapter_handle) 171 - { 172 - struct list_head resource_list; 173 - struct resource_entry *entry; 174 - struct i2c_acpi_lookup lookup; 175 - int ret; 176 - 177 - memset(&lookup, 0, sizeof(lookup)); 178 - lookup.info = info; 179 - lookup.index = -1; 180 - 181 - ret = i2c_acpi_do_lookup(adev, &lookup); 182 - if (ret) 183 - return ret; 184 - 185 - if (adapter) { 186 - /* The adapter must match the one in I2cSerialBus() connector */ 187 - if (ACPI_HANDLE(&adapter->dev) != lookup.adapter_handle) 188 - return -ENODEV; 189 - } else { 190 - struct acpi_device *adapter_adev; 191 - 192 - /* The adapter must be present */ 193 - if (acpi_bus_get_device(lookup.adapter_handle, &adapter_adev)) 194 - return -ENODEV; 195 - if (acpi_bus_get_status(adapter_adev) || 196 - !adapter_adev->status.present) 197 - return -ENODEV; 198 - } 199 - 200 - info->fwnode = acpi_fwnode_handle(adev); 201 - if (adapter_handle) 202 - *adapter_handle = lookup.adapter_handle; 203 - 204 - /* Then fill IRQ number if any */ 205 - INIT_LIST_HEAD(&resource_list); 206 - ret = acpi_dev_get_resources(adev, &resource_list, NULL, NULL); 207 - if (ret < 0) 208 - return -EINVAL; 209 - 210 - resource_list_for_each_entry(entry, &resource_list) { 211 - if (resource_type(entry->res) == IORESOURCE_IRQ) { 212 - info->irq = entry->res->start; 213 - break; 214 - } 215 - } 216 - 217 - acpi_dev_free_resource_list(&resource_list); 218 - 219 - acpi_set_modalias(adev, dev_name(&adev->dev), info->type, 220 - sizeof(info->type)); 221 - 222 - return 0; 223 - } 224 - 225 - static void i2c_acpi_register_device(struct i2c_adapter *adapter, 226 - struct acpi_device *adev, 227 - struct i2c_board_info *info) 228 - { 229 - adev->power.flags.ignore_parent = true; 230 - acpi_device_set_enumerated(adev); 231 - 232 - if (!i2c_new_device(adapter, info)) { 233 - adev->power.flags.ignore_parent = false; 234 - dev_err(&adapter->dev, 235 - "failed to add I2C device %s from ACPI\n", 236 - dev_name(&adev->dev)); 237 - } 238 - } 239 - 240 - static acpi_status i2c_acpi_add_device(acpi_handle handle, u32 level, 241 - void *data, void **return_value) 242 - { 243 - struct i2c_adapter *adapter = data; 244 - struct acpi_device *adev; 245 - struct i2c_board_info info; 246 - 247 - if (acpi_bus_get_device(handle, &adev)) 248 - return AE_OK; 249 - 250 - if (i2c_acpi_get_info(adev, &info, adapter, NULL)) 251 - return AE_OK; 252 - 253 - i2c_acpi_register_device(adapter, adev, &info); 254 - 255 - return AE_OK; 256 - } 257 - 258 - #define I2C_ACPI_MAX_SCAN_DEPTH 32 259 - 260 - /** 261 - * i2c_acpi_register_devices - enumerate I2C slave devices behind adapter 262 - * @adap: pointer to adapter 263 - * 264 - * Enumerate all I2C slave devices behind this adapter by walking the ACPI 265 - * namespace. When a device is found it will be added to the Linux device 266 - * model and bound to the corresponding ACPI handle. 267 - */ 268 - static void i2c_acpi_register_devices(struct i2c_adapter *adap) 269 - { 270 - acpi_status status; 271 - 272 - if (!has_acpi_companion(&adap->dev)) 273 - return; 274 - 275 - status = acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT, 276 - I2C_ACPI_MAX_SCAN_DEPTH, 277 - i2c_acpi_add_device, NULL, 278 - adap, NULL); 279 - if (ACPI_FAILURE(status)) 280 - dev_warn(&adap->dev, "failed to enumerate I2C slaves\n"); 281 - } 282 - 283 - static acpi_status i2c_acpi_lookup_speed(acpi_handle handle, u32 level, 284 - void *data, void **return_value) 285 - { 286 - struct i2c_acpi_lookup *lookup = data; 287 - struct acpi_device *adev; 288 - 289 - if (acpi_bus_get_device(handle, &adev)) 290 - return AE_OK; 291 - 292 - if (i2c_acpi_do_lookup(adev, lookup)) 293 - return AE_OK; 294 - 295 - if (lookup->search_handle != lookup->adapter_handle) 296 - return AE_OK; 297 - 298 - if (lookup->speed <= lookup->min_speed) 299 - lookup->min_speed = lookup->speed; 300 - 301 - return AE_OK; 302 - } 303 - 304 - /** 305 - * i2c_acpi_find_bus_speed - find I2C bus speed from ACPI 306 - * @dev: The device owning the bus 307 - * 308 - * Find the I2C bus speed by walking the ACPI namespace for all I2C slaves 309 - * devices connected to this bus and use the speed of slowest device. 310 - * 311 - * Returns the speed in Hz or zero 312 - */ 313 - u32 i2c_acpi_find_bus_speed(struct device *dev) 314 - { 315 - struct i2c_acpi_lookup lookup; 316 - struct i2c_board_info dummy; 317 - acpi_status status; 318 - 319 - if (!has_acpi_companion(dev)) 320 - return 0; 321 - 322 - memset(&lookup, 0, sizeof(lookup)); 323 - lookup.search_handle = ACPI_HANDLE(dev); 324 - lookup.min_speed = UINT_MAX; 325 - lookup.info = &dummy; 326 - lookup.index = -1; 327 - 328 - status = acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT, 329 - I2C_ACPI_MAX_SCAN_DEPTH, 330 - i2c_acpi_lookup_speed, NULL, 331 - &lookup, NULL); 332 - 333 - if (ACPI_FAILURE(status)) { 334 - dev_warn(dev, "unable to find I2C bus speed from ACPI\n"); 335 - return 0; 336 - } 337 - 338 - return lookup.min_speed != UINT_MAX ? lookup.min_speed : 0; 339 - } 340 - EXPORT_SYMBOL_GPL(i2c_acpi_find_bus_speed); 341 - 342 - static int i2c_acpi_match_adapter(struct device *dev, void *data) 343 - { 344 - struct i2c_adapter *adapter = i2c_verify_adapter(dev); 345 - 346 - if (!adapter) 347 - return 0; 348 - 349 - return ACPI_HANDLE(dev) == (acpi_handle)data; 350 - } 351 - 352 - static int i2c_acpi_match_device(struct device *dev, void *data) 353 - { 354 - return ACPI_COMPANION(dev) == data; 355 - } 356 - 357 - static struct i2c_adapter *i2c_acpi_find_adapter_by_handle(acpi_handle handle) 358 - { 359 - struct device *dev; 360 - 361 - dev = bus_find_device(&i2c_bus_type, NULL, handle, 362 - i2c_acpi_match_adapter); 363 - return dev ? i2c_verify_adapter(dev) : NULL; 364 - } 365 - 366 - static struct i2c_client *i2c_acpi_find_client_by_adev(struct acpi_device *adev) 367 - { 368 - struct device *dev; 369 - 370 - dev = bus_find_device(&i2c_bus_type, NULL, adev, i2c_acpi_match_device); 371 - return dev ? i2c_verify_client(dev) : NULL; 372 - } 373 - 374 - static int i2c_acpi_notify(struct notifier_block *nb, unsigned long value, 375 - void *arg) 376 - { 377 - struct acpi_device *adev = arg; 378 - struct i2c_board_info info; 379 - acpi_handle adapter_handle; 380 - struct i2c_adapter *adapter; 381 - struct i2c_client *client; 382 - 383 - switch (value) { 384 - case ACPI_RECONFIG_DEVICE_ADD: 385 - if (i2c_acpi_get_info(adev, &info, NULL, &adapter_handle)) 386 - break; 387 - 388 - adapter = i2c_acpi_find_adapter_by_handle(adapter_handle); 389 - if (!adapter) 390 - break; 391 - 392 - i2c_acpi_register_device(adapter, adev, &info); 393 - break; 394 - case ACPI_RECONFIG_DEVICE_REMOVE: 395 - if (!acpi_device_enumerated(adev)) 396 - break; 397 - 398 - client = i2c_acpi_find_client_by_adev(adev); 399 - if (!client) 400 - break; 401 - 402 - i2c_unregister_device(client); 403 - put_device(&client->dev); 404 - break; 405 - } 406 - 407 - return NOTIFY_OK; 408 - } 409 - 410 - static struct notifier_block i2c_acpi_notifier = { 411 - .notifier_call = i2c_acpi_notify, 412 - }; 413 - 414 - /** 415 - * i2c_acpi_new_device - Create i2c-client for the Nth I2cSerialBus resource 416 - * @dev: Device owning the ACPI resources to get the client from 417 - * @index: Index of ACPI resource to get 418 - * @info: describes the I2C device; note this is modified (addr gets set) 419 - * Context: can sleep 420 - * 421 - * By default the i2c subsys creates an i2c-client for the first I2cSerialBus 422 - * resource of an acpi_device, but some acpi_devices have multiple I2cSerialBus 423 - * resources, in that case this function can be used to create an i2c-client 424 - * for other I2cSerialBus resources in the Current Resource Settings table. 425 - * 426 - * Also see i2c_new_device, which this function calls to create the i2c-client. 427 - * 428 - * Returns a pointer to the new i2c-client, or NULL if the adapter is not found. 429 - */ 430 - struct i2c_client *i2c_acpi_new_device(struct device *dev, int index, 431 - struct i2c_board_info *info) 432 - { 433 - struct i2c_acpi_lookup lookup; 434 - struct i2c_adapter *adapter; 435 - struct acpi_device *adev; 436 - LIST_HEAD(resource_list); 437 - int ret; 438 - 439 - adev = ACPI_COMPANION(dev); 440 - if (!adev) 441 - return NULL; 442 - 443 - memset(&lookup, 0, sizeof(lookup)); 444 - lookup.info = info; 445 - lookup.device_handle = acpi_device_handle(adev); 446 - lookup.index = index; 447 - 448 - ret = acpi_dev_get_resources(adev, &resource_list, 449 - i2c_acpi_fill_info, &lookup); 450 - acpi_dev_free_resource_list(&resource_list); 451 - 452 - if (ret < 0 || !info->addr) 453 - return NULL; 454 - 455 - adapter = i2c_acpi_find_adapter_by_handle(lookup.adapter_handle); 456 - if (!adapter) 457 - return NULL; 458 - 459 - return i2c_new_device(adapter, info); 460 - } 461 - EXPORT_SYMBOL_GPL(i2c_acpi_new_device); 462 - #else /* CONFIG_ACPI */ 463 - static inline void i2c_acpi_register_devices(struct i2c_adapter *adap) { } 464 - extern struct notifier_block i2c_acpi_notifier; 465 - #endif /* CONFIG_ACPI */ 466 - 467 - #ifdef CONFIG_ACPI_I2C_OPREGION 468 - static int acpi_gsb_i2c_read_bytes(struct i2c_client *client, 469 - u8 cmd, u8 *data, u8 data_len) 470 - { 471 - 472 - struct i2c_msg msgs[2]; 473 - int ret; 474 - u8 *buffer; 475 - 476 - buffer = kzalloc(data_len, GFP_KERNEL); 477 - if (!buffer) 478 - return AE_NO_MEMORY; 479 - 480 - msgs[0].addr = client->addr; 481 - msgs[0].flags = client->flags; 482 - msgs[0].len = 1; 483 - msgs[0].buf = &cmd; 484 - 485 - msgs[1].addr = client->addr; 486 - msgs[1].flags = client->flags | I2C_M_RD; 487 - msgs[1].len = data_len; 488 - msgs[1].buf = buffer; 489 - 490 - ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs)); 491 - if (ret < 0) 492 - dev_err(&client->adapter->dev, "i2c read failed\n"); 493 - else 494 - memcpy(data, buffer, data_len); 495 - 496 - kfree(buffer); 497 - return ret; 498 - } 499 - 500 - static int acpi_gsb_i2c_write_bytes(struct i2c_client *client, 501 - u8 cmd, u8 *data, u8 data_len) 502 - { 503 - 504 - struct i2c_msg msgs[1]; 505 - u8 *buffer; 506 - int ret = AE_OK; 507 - 508 - buffer = kzalloc(data_len + 1, GFP_KERNEL); 509 - if (!buffer) 510 - return AE_NO_MEMORY; 511 - 512 - buffer[0] = cmd; 513 - memcpy(buffer + 1, data, data_len); 514 - 515 - msgs[0].addr = client->addr; 516 - msgs[0].flags = client->flags; 517 - msgs[0].len = data_len + 1; 518 - msgs[0].buf = buffer; 519 - 520 - ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs)); 521 - if (ret < 0) 522 - dev_err(&client->adapter->dev, "i2c write failed\n"); 523 - 524 - kfree(buffer); 525 - return ret; 526 - } 527 - 528 - static acpi_status 529 - i2c_acpi_space_handler(u32 function, acpi_physical_address command, 530 - u32 bits, u64 *value64, 531 - void *handler_context, void *region_context) 532 - { 533 - struct gsb_buffer *gsb = (struct gsb_buffer *)value64; 534 - struct i2c_acpi_handler_data *data = handler_context; 535 - struct acpi_connection_info *info = &data->info; 536 - struct acpi_resource_i2c_serialbus *sb; 537 - struct i2c_adapter *adapter = data->adapter; 538 - struct i2c_client *client; 539 - struct acpi_resource *ares; 540 - u32 accessor_type = function >> 16; 541 - u8 action = function & ACPI_IO_MASK; 542 - acpi_status ret; 543 - int status; 544 - 545 - ret = acpi_buffer_to_resource(info->connection, info->length, &ares); 546 - if (ACPI_FAILURE(ret)) 547 - return ret; 548 - 549 - client = kzalloc(sizeof(*client), GFP_KERNEL); 550 - if (!client) { 551 - ret = AE_NO_MEMORY; 552 - goto err; 553 - } 554 - 555 - if (!value64 || ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS) { 556 - ret = AE_BAD_PARAMETER; 557 - goto err; 558 - } 559 - 560 - sb = &ares->data.i2c_serial_bus; 561 - if (sb->type != ACPI_RESOURCE_SERIAL_TYPE_I2C) { 562 - ret = AE_BAD_PARAMETER; 563 - goto err; 564 - } 565 - 566 - client->adapter = adapter; 567 - client->addr = sb->slave_address; 568 - 569 - if (sb->access_mode == ACPI_I2C_10BIT_MODE) 570 - client->flags |= I2C_CLIENT_TEN; 571 - 572 - switch (accessor_type) { 573 - case ACPI_GSB_ACCESS_ATTRIB_SEND_RCV: 574 - if (action == ACPI_READ) { 575 - status = i2c_smbus_read_byte(client); 576 - if (status >= 0) { 577 - gsb->bdata = status; 578 - status = 0; 579 - } 580 - } else { 581 - status = i2c_smbus_write_byte(client, gsb->bdata); 582 - } 583 - break; 584 - 585 - case ACPI_GSB_ACCESS_ATTRIB_BYTE: 586 - if (action == ACPI_READ) { 587 - status = i2c_smbus_read_byte_data(client, command); 588 - if (status >= 0) { 589 - gsb->bdata = status; 590 - status = 0; 591 - } 592 - } else { 593 - status = i2c_smbus_write_byte_data(client, command, 594 - gsb->bdata); 595 - } 596 - break; 597 - 598 - case ACPI_GSB_ACCESS_ATTRIB_WORD: 599 - if (action == ACPI_READ) { 600 - status = i2c_smbus_read_word_data(client, command); 601 - if (status >= 0) { 602 - gsb->wdata = status; 603 - status = 0; 604 - } 605 - } else { 606 - status = i2c_smbus_write_word_data(client, command, 607 - gsb->wdata); 608 - } 609 - break; 610 - 611 - case ACPI_GSB_ACCESS_ATTRIB_BLOCK: 612 - if (action == ACPI_READ) { 613 - status = i2c_smbus_read_block_data(client, command, 614 - gsb->data); 615 - if (status >= 0) { 616 - gsb->len = status; 617 - status = 0; 618 - } 619 - } else { 620 - status = i2c_smbus_write_block_data(client, command, 621 - gsb->len, gsb->data); 622 - } 623 - break; 624 - 625 - case ACPI_GSB_ACCESS_ATTRIB_MULTIBYTE: 626 - if (action == ACPI_READ) { 627 - status = acpi_gsb_i2c_read_bytes(client, command, 628 - gsb->data, info->access_length); 629 - if (status > 0) 630 - status = 0; 631 - } else { 632 - status = acpi_gsb_i2c_write_bytes(client, command, 633 - gsb->data, info->access_length); 634 - } 635 - break; 636 - 637 - default: 638 - dev_warn(&adapter->dev, "protocol 0x%02x not supported for client 0x%02x\n", 639 - accessor_type, client->addr); 640 - ret = AE_BAD_PARAMETER; 641 - goto err; 642 - } 643 - 644 - gsb->status = status; 645 - 646 - err: 647 - kfree(client); 648 - ACPI_FREE(ares); 649 - return ret; 650 - } 651 - 652 - 653 - static int i2c_acpi_install_space_handler(struct i2c_adapter *adapter) 654 - { 655 - acpi_handle handle; 656 - struct i2c_acpi_handler_data *data; 657 - acpi_status status; 658 - 659 - if (!adapter->dev.parent) 660 - return -ENODEV; 661 - 662 - handle = ACPI_HANDLE(adapter->dev.parent); 663 - 664 - if (!handle) 665 - return -ENODEV; 666 - 667 - data = kzalloc(sizeof(struct i2c_acpi_handler_data), 668 - GFP_KERNEL); 669 - if (!data) 670 - return -ENOMEM; 671 - 672 - data->adapter = adapter; 673 - status = acpi_bus_attach_private_data(handle, (void *)data); 674 - if (ACPI_FAILURE(status)) { 675 - kfree(data); 676 - return -ENOMEM; 677 - } 678 - 679 - status = acpi_install_address_space_handler(handle, 680 - ACPI_ADR_SPACE_GSBUS, 681 - &i2c_acpi_space_handler, 682 - NULL, 683 - data); 684 - if (ACPI_FAILURE(status)) { 685 - dev_err(&adapter->dev, "Error installing i2c space handler\n"); 686 - acpi_bus_detach_private_data(handle); 687 - kfree(data); 688 - return -ENOMEM; 689 - } 690 - 691 - acpi_walk_dep_device_list(handle); 692 - return 0; 693 - } 694 - 695 - static void i2c_acpi_remove_space_handler(struct i2c_adapter *adapter) 696 - { 697 - acpi_handle handle; 698 - struct i2c_acpi_handler_data *data; 699 - acpi_status status; 700 - 701 - if (!adapter->dev.parent) 702 - return; 703 - 704 - handle = ACPI_HANDLE(adapter->dev.parent); 705 - 706 - if (!handle) 707 - return; 708 - 709 - acpi_remove_address_space_handler(handle, 710 - ACPI_ADR_SPACE_GSBUS, 711 - &i2c_acpi_space_handler); 712 - 713 - status = acpi_bus_get_private_data(handle, (void **)&data); 714 - if (ACPI_SUCCESS(status)) 715 - kfree(data); 716 - 717 - acpi_bus_detach_private_data(handle); 718 - } 719 - #else /* CONFIG_ACPI_I2C_OPREGION */ 720 - static inline void i2c_acpi_remove_space_handler(struct i2c_adapter *adapter) 721 - { } 722 - 723 - static inline int i2c_acpi_install_space_handler(struct i2c_adapter *adapter) 724 - { return 0; } 725 - #endif /* CONFIG_ACPI_I2C_OPREGION */ 726 - 727 - /* ------------------------------------------------------------------------- */ 728 92 729 93 const struct i2c_device_id *i2c_match_id(const struct i2c_device_id *id, 730 94 const struct i2c_client *client) ··· 539 1195 540 1196 /* This is a permissive address validity check, I2C address map constraints 541 1197 * are purposely not enforced, except for the general call address. */ 542 - static int i2c_check_addr_validity(unsigned addr, unsigned short flags) 1198 + int i2c_check_addr_validity(unsigned addr, unsigned short flags) 543 1199 { 544 1200 if (flags & I2C_CLIENT_TEN) { 545 1201 /* 10-bit address, all values are valid */ ··· 557 1213 * device uses a reserved address, then it shouldn't be probed. 7-bit 558 1214 * addressing is assumed, 10-bit address devices are rare and should be 559 1215 * explicitly enumerated. */ 560 - static int i2c_check_7bit_addr_validity_strict(unsigned short addr) 1216 + int i2c_check_7bit_addr_validity_strict(unsigned short addr) 561 1217 { 562 1218 /* 563 1219 * Reserved addresses per I2C specification: ··· 1107 1763 } 1108 1764 up_read(&__i2c_board_lock); 1109 1765 } 1110 - 1111 - /* OF support code */ 1112 - 1113 - #if IS_ENABLED(CONFIG_OF) 1114 - static struct i2c_client *of_i2c_register_device(struct i2c_adapter *adap, 1115 - struct device_node *node) 1116 - { 1117 - struct i2c_client *result; 1118 - struct i2c_board_info info = {}; 1119 - struct dev_archdata dev_ad = {}; 1120 - const __be32 *addr_be; 1121 - u32 addr; 1122 - int len; 1123 - 1124 - dev_dbg(&adap->dev, "of_i2c: register %s\n", node->full_name); 1125 - 1126 - if (of_modalias_node(node, info.type, sizeof(info.type)) < 0) { 1127 - dev_err(&adap->dev, "of_i2c: modalias failure on %s\n", 1128 - node->full_name); 1129 - return ERR_PTR(-EINVAL); 1130 - } 1131 - 1132 - addr_be = of_get_property(node, "reg", &len); 1133 - if (!addr_be || (len < sizeof(*addr_be))) { 1134 - dev_err(&adap->dev, "of_i2c: invalid reg on %s\n", 1135 - node->full_name); 1136 - return ERR_PTR(-EINVAL); 1137 - } 1138 - 1139 - addr = be32_to_cpup(addr_be); 1140 - if (addr & I2C_TEN_BIT_ADDRESS) { 1141 - addr &= ~I2C_TEN_BIT_ADDRESS; 1142 - info.flags |= I2C_CLIENT_TEN; 1143 - } 1144 - 1145 - if (addr & I2C_OWN_SLAVE_ADDRESS) { 1146 - addr &= ~I2C_OWN_SLAVE_ADDRESS; 1147 - info.flags |= I2C_CLIENT_SLAVE; 1148 - } 1149 - 1150 - if (i2c_check_addr_validity(addr, info.flags)) { 1151 - dev_err(&adap->dev, "of_i2c: invalid addr=%x on %s\n", 1152 - addr, node->full_name); 1153 - return ERR_PTR(-EINVAL); 1154 - } 1155 - 1156 - info.addr = addr; 1157 - info.of_node = of_node_get(node); 1158 - info.archdata = &dev_ad; 1159 - 1160 - if (of_property_read_bool(node, "host-notify")) 1161 - info.flags |= I2C_CLIENT_HOST_NOTIFY; 1162 - 1163 - if (of_get_property(node, "wakeup-source", NULL)) 1164 - info.flags |= I2C_CLIENT_WAKE; 1165 - 1166 - result = i2c_new_device(adap, &info); 1167 - if (result == NULL) { 1168 - dev_err(&adap->dev, "of_i2c: Failure registering %s\n", 1169 - node->full_name); 1170 - of_node_put(node); 1171 - return ERR_PTR(-EINVAL); 1172 - } 1173 - return result; 1174 - } 1175 - 1176 - static void of_i2c_register_devices(struct i2c_adapter *adap) 1177 - { 1178 - struct device_node *bus, *node; 1179 - struct i2c_client *client; 1180 - 1181 - /* Only register child devices if the adapter has a node pointer set */ 1182 - if (!adap->dev.of_node) 1183 - return; 1184 - 1185 - dev_dbg(&adap->dev, "of_i2c: walking child nodes\n"); 1186 - 1187 - bus = of_get_child_by_name(adap->dev.of_node, "i2c-bus"); 1188 - if (!bus) 1189 - bus = of_node_get(adap->dev.of_node); 1190 - 1191 - for_each_available_child_of_node(bus, node) { 1192 - if (of_node_test_and_set_flag(node, OF_POPULATED)) 1193 - continue; 1194 - 1195 - client = of_i2c_register_device(adap, node); 1196 - if (IS_ERR(client)) { 1197 - dev_warn(&adap->dev, 1198 - "Failed to create I2C device for %s\n", 1199 - node->full_name); 1200 - of_node_clear_flag(node, OF_POPULATED); 1201 - } 1202 - } 1203 - 1204 - of_node_put(bus); 1205 - } 1206 - 1207 - static int of_dev_node_match(struct device *dev, void *data) 1208 - { 1209 - return dev->of_node == data; 1210 - } 1211 - 1212 - /* must call put_device() when done with returned i2c_client device */ 1213 - struct i2c_client *of_find_i2c_device_by_node(struct device_node *node) 1214 - { 1215 - struct device *dev; 1216 - struct i2c_client *client; 1217 - 1218 - dev = bus_find_device(&i2c_bus_type, NULL, node, of_dev_node_match); 1219 - if (!dev) 1220 - return NULL; 1221 - 1222 - client = i2c_verify_client(dev); 1223 - if (!client) 1224 - put_device(dev); 1225 - 1226 - return client; 1227 - } 1228 - EXPORT_SYMBOL(of_find_i2c_device_by_node); 1229 - 1230 - /* must call put_device() when done with returned i2c_adapter device */ 1231 - struct i2c_adapter *of_find_i2c_adapter_by_node(struct device_node *node) 1232 - { 1233 - struct device *dev; 1234 - struct i2c_adapter *adapter; 1235 - 1236 - dev = bus_find_device(&i2c_bus_type, NULL, node, of_dev_node_match); 1237 - if (!dev) 1238 - return NULL; 1239 - 1240 - adapter = i2c_verify_adapter(dev); 1241 - if (!adapter) 1242 - put_device(dev); 1243 - 1244 - return adapter; 1245 - } 1246 - EXPORT_SYMBOL(of_find_i2c_adapter_by_node); 1247 - 1248 - /* must call i2c_put_adapter() when done with returned i2c_adapter device */ 1249 - struct i2c_adapter *of_get_i2c_adapter_by_node(struct device_node *node) 1250 - { 1251 - struct i2c_adapter *adapter; 1252 - 1253 - adapter = of_find_i2c_adapter_by_node(node); 1254 - if (!adapter) 1255 - return NULL; 1256 - 1257 - if (!try_module_get(adapter->owner)) { 1258 - put_device(&adapter->dev); 1259 - adapter = NULL; 1260 - } 1261 - 1262 - return adapter; 1263 - } 1264 - EXPORT_SYMBOL(of_get_i2c_adapter_by_node); 1265 - 1266 - static const struct of_device_id* 1267 - i2c_of_match_device_sysfs(const struct of_device_id *matches, 1268 - struct i2c_client *client) 1269 - { 1270 - const char *name; 1271 - 1272 - for (; matches->compatible[0]; matches++) { 1273 - /* 1274 - * Adding devices through the i2c sysfs interface provides us 1275 - * a string to match which may be compatible with the device 1276 - * tree compatible strings, however with no actual of_node the 1277 - * of_match_device() will not match 1278 - */ 1279 - if (sysfs_streq(client->name, matches->compatible)) 1280 - return matches; 1281 - 1282 - name = strchr(matches->compatible, ','); 1283 - if (!name) 1284 - name = matches->compatible; 1285 - else 1286 - name++; 1287 - 1288 - if (sysfs_streq(client->name, name)) 1289 - return matches; 1290 - } 1291 - 1292 - return NULL; 1293 - } 1294 - 1295 - const struct of_device_id 1296 - *i2c_of_match_device(const struct of_device_id *matches, 1297 - struct i2c_client *client) 1298 - { 1299 - const struct of_device_id *match; 1300 - 1301 - if (!(client && matches)) 1302 - return NULL; 1303 - 1304 - match = of_match_device(matches, &client->dev); 1305 - if (match) 1306 - return match; 1307 - 1308 - return i2c_of_match_device_sysfs(matches, client); 1309 - } 1310 - EXPORT_SYMBOL_GPL(i2c_of_match_device); 1311 - #else 1312 - static void of_i2c_register_devices(struct i2c_adapter *adap) { } 1313 - #endif /* CONFIG_OF */ 1314 1766 1315 1767 static int i2c_do_add_adapter(struct i2c_driver *driver, 1316 1768 struct i2c_adapter *adap) ··· 1702 2562 } 1703 2563 EXPORT_SYMBOL(i2c_clients_command); 1704 2564 1705 - #if IS_ENABLED(CONFIG_OF_DYNAMIC) 1706 - static int of_i2c_notify(struct notifier_block *nb, unsigned long action, 1707 - void *arg) 1708 - { 1709 - struct of_reconfig_data *rd = arg; 1710 - struct i2c_adapter *adap; 1711 - struct i2c_client *client; 1712 - 1713 - switch (of_reconfig_get_state_change(action, rd)) { 1714 - case OF_RECONFIG_CHANGE_ADD: 1715 - adap = of_find_i2c_adapter_by_node(rd->dn->parent); 1716 - if (adap == NULL) 1717 - return NOTIFY_OK; /* not for us */ 1718 - 1719 - if (of_node_test_and_set_flag(rd->dn, OF_POPULATED)) { 1720 - put_device(&adap->dev); 1721 - return NOTIFY_OK; 1722 - } 1723 - 1724 - client = of_i2c_register_device(adap, rd->dn); 1725 - put_device(&adap->dev); 1726 - 1727 - if (IS_ERR(client)) { 1728 - dev_err(&adap->dev, "failed to create client for '%s'\n", 1729 - rd->dn->full_name); 1730 - of_node_clear_flag(rd->dn, OF_POPULATED); 1731 - return notifier_from_errno(PTR_ERR(client)); 1732 - } 1733 - break; 1734 - case OF_RECONFIG_CHANGE_REMOVE: 1735 - /* already depopulated? */ 1736 - if (!of_node_check_flag(rd->dn, OF_POPULATED)) 1737 - return NOTIFY_OK; 1738 - 1739 - /* find our device by node */ 1740 - client = of_find_i2c_device_by_node(rd->dn); 1741 - if (client == NULL) 1742 - return NOTIFY_OK; /* no? not meant for us */ 1743 - 1744 - /* unregister takes one ref away */ 1745 - i2c_unregister_device(client); 1746 - 1747 - /* and put the reference of the find */ 1748 - put_device(&client->dev); 1749 - break; 1750 - } 1751 - 1752 - return NOTIFY_OK; 1753 - } 1754 - static struct notifier_block i2c_of_notifier = { 1755 - .notifier_call = of_i2c_notify, 1756 - }; 1757 - #else 1758 - extern struct notifier_block i2c_of_notifier; 1759 - #endif /* CONFIG_OF_DYNAMIC */ 1760 - 1761 2565 static int __init i2c_init(void) 1762 2566 { 1763 2567 int retval; ··· 2239 3155 module_put(adap->owner); 2240 3156 } 2241 3157 EXPORT_SYMBOL(i2c_put_adapter); 2242 - 2243 - /* The SMBus parts */ 2244 - 2245 - #define POLY (0x1070U << 3) 2246 - static u8 crc8(u16 data) 2247 - { 2248 - int i; 2249 - 2250 - for (i = 0; i < 8; i++) { 2251 - if (data & 0x8000) 2252 - data = data ^ POLY; 2253 - data = data << 1; 2254 - } 2255 - return (u8)(data >> 8); 2256 - } 2257 - 2258 - /* Incremental CRC8 over count bytes in the array pointed to by p */ 2259 - static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count) 2260 - { 2261 - int i; 2262 - 2263 - for (i = 0; i < count; i++) 2264 - crc = crc8((crc ^ p[i]) << 8); 2265 - return crc; 2266 - } 2267 - 2268 - /* Assume a 7-bit address, which is reasonable for SMBus */ 2269 - static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg) 2270 - { 2271 - /* The address will be sent first */ 2272 - u8 addr = i2c_8bit_addr_from_msg(msg); 2273 - pec = i2c_smbus_pec(pec, &addr, 1); 2274 - 2275 - /* The data buffer follows */ 2276 - return i2c_smbus_pec(pec, msg->buf, msg->len); 2277 - } 2278 - 2279 - /* Used for write only transactions */ 2280 - static inline void i2c_smbus_add_pec(struct i2c_msg *msg) 2281 - { 2282 - msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg); 2283 - msg->len++; 2284 - } 2285 - 2286 - /* Return <0 on CRC error 2287 - If there was a write before this read (most cases) we need to take the 2288 - partial CRC from the write part into account. 2289 - Note that this function does modify the message (we need to decrease the 2290 - message length to hide the CRC byte from the caller). */ 2291 - static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg) 2292 - { 2293 - u8 rpec = msg->buf[--msg->len]; 2294 - cpec = i2c_smbus_msg_pec(cpec, msg); 2295 - 2296 - if (rpec != cpec) { 2297 - pr_debug("Bad PEC 0x%02x vs. 0x%02x\n", 2298 - rpec, cpec); 2299 - return -EBADMSG; 2300 - } 2301 - return 0; 2302 - } 2303 - 2304 - /** 2305 - * i2c_smbus_read_byte - SMBus "receive byte" protocol 2306 - * @client: Handle to slave device 2307 - * 2308 - * This executes the SMBus "receive byte" protocol, returning negative errno 2309 - * else the byte received from the device. 2310 - */ 2311 - s32 i2c_smbus_read_byte(const struct i2c_client *client) 2312 - { 2313 - union i2c_smbus_data data; 2314 - int status; 2315 - 2316 - status = i2c_smbus_xfer(client->adapter, client->addr, client->flags, 2317 - I2C_SMBUS_READ, 0, 2318 - I2C_SMBUS_BYTE, &data); 2319 - return (status < 0) ? status : data.byte; 2320 - } 2321 - EXPORT_SYMBOL(i2c_smbus_read_byte); 2322 - 2323 - /** 2324 - * i2c_smbus_write_byte - SMBus "send byte" protocol 2325 - * @client: Handle to slave device 2326 - * @value: Byte to be sent 2327 - * 2328 - * This executes the SMBus "send byte" protocol, returning negative errno 2329 - * else zero on success. 2330 - */ 2331 - s32 i2c_smbus_write_byte(const struct i2c_client *client, u8 value) 2332 - { 2333 - return i2c_smbus_xfer(client->adapter, client->addr, client->flags, 2334 - I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL); 2335 - } 2336 - EXPORT_SYMBOL(i2c_smbus_write_byte); 2337 - 2338 - /** 2339 - * i2c_smbus_read_byte_data - SMBus "read byte" protocol 2340 - * @client: Handle to slave device 2341 - * @command: Byte interpreted by slave 2342 - * 2343 - * This executes the SMBus "read byte" protocol, returning negative errno 2344 - * else a data byte received from the device. 2345 - */ 2346 - s32 i2c_smbus_read_byte_data(const struct i2c_client *client, u8 command) 2347 - { 2348 - union i2c_smbus_data data; 2349 - int status; 2350 - 2351 - status = i2c_smbus_xfer(client->adapter, client->addr, client->flags, 2352 - I2C_SMBUS_READ, command, 2353 - I2C_SMBUS_BYTE_DATA, &data); 2354 - return (status < 0) ? status : data.byte; 2355 - } 2356 - EXPORT_SYMBOL(i2c_smbus_read_byte_data); 2357 - 2358 - /** 2359 - * i2c_smbus_write_byte_data - SMBus "write byte" protocol 2360 - * @client: Handle to slave device 2361 - * @command: Byte interpreted by slave 2362 - * @value: Byte being written 2363 - * 2364 - * This executes the SMBus "write byte" protocol, returning negative errno 2365 - * else zero on success. 2366 - */ 2367 - s32 i2c_smbus_write_byte_data(const struct i2c_client *client, u8 command, 2368 - u8 value) 2369 - { 2370 - union i2c_smbus_data data; 2371 - data.byte = value; 2372 - return i2c_smbus_xfer(client->adapter, client->addr, client->flags, 2373 - I2C_SMBUS_WRITE, command, 2374 - I2C_SMBUS_BYTE_DATA, &data); 2375 - } 2376 - EXPORT_SYMBOL(i2c_smbus_write_byte_data); 2377 - 2378 - /** 2379 - * i2c_smbus_read_word_data - SMBus "read word" protocol 2380 - * @client: Handle to slave device 2381 - * @command: Byte interpreted by slave 2382 - * 2383 - * This executes the SMBus "read word" protocol, returning negative errno 2384 - * else a 16-bit unsigned "word" received from the device. 2385 - */ 2386 - s32 i2c_smbus_read_word_data(const struct i2c_client *client, u8 command) 2387 - { 2388 - union i2c_smbus_data data; 2389 - int status; 2390 - 2391 - status = i2c_smbus_xfer(client->adapter, client->addr, client->flags, 2392 - I2C_SMBUS_READ, command, 2393 - I2C_SMBUS_WORD_DATA, &data); 2394 - return (status < 0) ? status : data.word; 2395 - } 2396 - EXPORT_SYMBOL(i2c_smbus_read_word_data); 2397 - 2398 - /** 2399 - * i2c_smbus_write_word_data - SMBus "write word" protocol 2400 - * @client: Handle to slave device 2401 - * @command: Byte interpreted by slave 2402 - * @value: 16-bit "word" being written 2403 - * 2404 - * This executes the SMBus "write word" protocol, returning negative errno 2405 - * else zero on success. 2406 - */ 2407 - s32 i2c_smbus_write_word_data(const struct i2c_client *client, u8 command, 2408 - u16 value) 2409 - { 2410 - union i2c_smbus_data data; 2411 - data.word = value; 2412 - return i2c_smbus_xfer(client->adapter, client->addr, client->flags, 2413 - I2C_SMBUS_WRITE, command, 2414 - I2C_SMBUS_WORD_DATA, &data); 2415 - } 2416 - EXPORT_SYMBOL(i2c_smbus_write_word_data); 2417 - 2418 - /** 2419 - * i2c_smbus_read_block_data - SMBus "block read" protocol 2420 - * @client: Handle to slave device 2421 - * @command: Byte interpreted by slave 2422 - * @values: Byte array into which data will be read; big enough to hold 2423 - * the data returned by the slave. SMBus allows at most 32 bytes. 2424 - * 2425 - * This executes the SMBus "block read" protocol, returning negative errno 2426 - * else the number of data bytes in the slave's response. 2427 - * 2428 - * Note that using this function requires that the client's adapter support 2429 - * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers 2430 - * support this; its emulation through I2C messaging relies on a specific 2431 - * mechanism (I2C_M_RECV_LEN) which may not be implemented. 2432 - */ 2433 - s32 i2c_smbus_read_block_data(const struct i2c_client *client, u8 command, 2434 - u8 *values) 2435 - { 2436 - union i2c_smbus_data data; 2437 - int status; 2438 - 2439 - status = i2c_smbus_xfer(client->adapter, client->addr, client->flags, 2440 - I2C_SMBUS_READ, command, 2441 - I2C_SMBUS_BLOCK_DATA, &data); 2442 - if (status) 2443 - return status; 2444 - 2445 - memcpy(values, &data.block[1], data.block[0]); 2446 - return data.block[0]; 2447 - } 2448 - EXPORT_SYMBOL(i2c_smbus_read_block_data); 2449 - 2450 - /** 2451 - * i2c_smbus_write_block_data - SMBus "block write" protocol 2452 - * @client: Handle to slave device 2453 - * @command: Byte interpreted by slave 2454 - * @length: Size of data block; SMBus allows at most 32 bytes 2455 - * @values: Byte array which will be written. 2456 - * 2457 - * This executes the SMBus "block write" protocol, returning negative errno 2458 - * else zero on success. 2459 - */ 2460 - s32 i2c_smbus_write_block_data(const struct i2c_client *client, u8 command, 2461 - u8 length, const u8 *values) 2462 - { 2463 - union i2c_smbus_data data; 2464 - 2465 - if (length > I2C_SMBUS_BLOCK_MAX) 2466 - length = I2C_SMBUS_BLOCK_MAX; 2467 - data.block[0] = length; 2468 - memcpy(&data.block[1], values, length); 2469 - return i2c_smbus_xfer(client->adapter, client->addr, client->flags, 2470 - I2C_SMBUS_WRITE, command, 2471 - I2C_SMBUS_BLOCK_DATA, &data); 2472 - } 2473 - EXPORT_SYMBOL(i2c_smbus_write_block_data); 2474 - 2475 - /* Returns the number of read bytes */ 2476 - s32 i2c_smbus_read_i2c_block_data(const struct i2c_client *client, u8 command, 2477 - u8 length, u8 *values) 2478 - { 2479 - union i2c_smbus_data data; 2480 - int status; 2481 - 2482 - if (length > I2C_SMBUS_BLOCK_MAX) 2483 - length = I2C_SMBUS_BLOCK_MAX; 2484 - data.block[0] = length; 2485 - status = i2c_smbus_xfer(client->adapter, client->addr, client->flags, 2486 - I2C_SMBUS_READ, command, 2487 - I2C_SMBUS_I2C_BLOCK_DATA, &data); 2488 - if (status < 0) 2489 - return status; 2490 - 2491 - memcpy(values, &data.block[1], data.block[0]); 2492 - return data.block[0]; 2493 - } 2494 - EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data); 2495 - 2496 - s32 i2c_smbus_write_i2c_block_data(const struct i2c_client *client, u8 command, 2497 - u8 length, const u8 *values) 2498 - { 2499 - union i2c_smbus_data data; 2500 - 2501 - if (length > I2C_SMBUS_BLOCK_MAX) 2502 - length = I2C_SMBUS_BLOCK_MAX; 2503 - data.block[0] = length; 2504 - memcpy(data.block + 1, values, length); 2505 - return i2c_smbus_xfer(client->adapter, client->addr, client->flags, 2506 - I2C_SMBUS_WRITE, command, 2507 - I2C_SMBUS_I2C_BLOCK_DATA, &data); 2508 - } 2509 - EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data); 2510 - 2511 - /* Simulate a SMBus command using the i2c protocol 2512 - No checking of parameters is done! */ 2513 - static s32 i2c_smbus_xfer_emulated(struct i2c_adapter *adapter, u16 addr, 2514 - unsigned short flags, 2515 - char read_write, u8 command, int size, 2516 - union i2c_smbus_data *data) 2517 - { 2518 - /* So we need to generate a series of msgs. In the case of writing, we 2519 - need to use only one message; when reading, we need two. We initialize 2520 - most things with sane defaults, to keep the code below somewhat 2521 - simpler. */ 2522 - unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3]; 2523 - unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2]; 2524 - int num = read_write == I2C_SMBUS_READ ? 2 : 1; 2525 - int i; 2526 - u8 partial_pec = 0; 2527 - int status; 2528 - struct i2c_msg msg[2] = { 2529 - { 2530 - .addr = addr, 2531 - .flags = flags, 2532 - .len = 1, 2533 - .buf = msgbuf0, 2534 - }, { 2535 - .addr = addr, 2536 - .flags = flags | I2C_M_RD, 2537 - .len = 0, 2538 - .buf = msgbuf1, 2539 - }, 2540 - }; 2541 - 2542 - msgbuf0[0] = command; 2543 - switch (size) { 2544 - case I2C_SMBUS_QUICK: 2545 - msg[0].len = 0; 2546 - /* Special case: The read/write field is used as data */ 2547 - msg[0].flags = flags | (read_write == I2C_SMBUS_READ ? 2548 - I2C_M_RD : 0); 2549 - num = 1; 2550 - break; 2551 - case I2C_SMBUS_BYTE: 2552 - if (read_write == I2C_SMBUS_READ) { 2553 - /* Special case: only a read! */ 2554 - msg[0].flags = I2C_M_RD | flags; 2555 - num = 1; 2556 - } 2557 - break; 2558 - case I2C_SMBUS_BYTE_DATA: 2559 - if (read_write == I2C_SMBUS_READ) 2560 - msg[1].len = 1; 2561 - else { 2562 - msg[0].len = 2; 2563 - msgbuf0[1] = data->byte; 2564 - } 2565 - break; 2566 - case I2C_SMBUS_WORD_DATA: 2567 - if (read_write == I2C_SMBUS_READ) 2568 - msg[1].len = 2; 2569 - else { 2570 - msg[0].len = 3; 2571 - msgbuf0[1] = data->word & 0xff; 2572 - msgbuf0[2] = data->word >> 8; 2573 - } 2574 - break; 2575 - case I2C_SMBUS_PROC_CALL: 2576 - num = 2; /* Special case */ 2577 - read_write = I2C_SMBUS_READ; 2578 - msg[0].len = 3; 2579 - msg[1].len = 2; 2580 - msgbuf0[1] = data->word & 0xff; 2581 - msgbuf0[2] = data->word >> 8; 2582 - break; 2583 - case I2C_SMBUS_BLOCK_DATA: 2584 - if (read_write == I2C_SMBUS_READ) { 2585 - msg[1].flags |= I2C_M_RECV_LEN; 2586 - msg[1].len = 1; /* block length will be added by 2587 - the underlying bus driver */ 2588 - } else { 2589 - msg[0].len = data->block[0] + 2; 2590 - if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) { 2591 - dev_err(&adapter->dev, 2592 - "Invalid block write size %d\n", 2593 - data->block[0]); 2594 - return -EINVAL; 2595 - } 2596 - for (i = 1; i < msg[0].len; i++) 2597 - msgbuf0[i] = data->block[i-1]; 2598 - } 2599 - break; 2600 - case I2C_SMBUS_BLOCK_PROC_CALL: 2601 - num = 2; /* Another special case */ 2602 - read_write = I2C_SMBUS_READ; 2603 - if (data->block[0] > I2C_SMBUS_BLOCK_MAX) { 2604 - dev_err(&adapter->dev, 2605 - "Invalid block write size %d\n", 2606 - data->block[0]); 2607 - return -EINVAL; 2608 - } 2609 - msg[0].len = data->block[0] + 2; 2610 - for (i = 1; i < msg[0].len; i++) 2611 - msgbuf0[i] = data->block[i-1]; 2612 - msg[1].flags |= I2C_M_RECV_LEN; 2613 - msg[1].len = 1; /* block length will be added by 2614 - the underlying bus driver */ 2615 - break; 2616 - case I2C_SMBUS_I2C_BLOCK_DATA: 2617 - if (read_write == I2C_SMBUS_READ) { 2618 - msg[1].len = data->block[0]; 2619 - } else { 2620 - msg[0].len = data->block[0] + 1; 2621 - if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) { 2622 - dev_err(&adapter->dev, 2623 - "Invalid block write size %d\n", 2624 - data->block[0]); 2625 - return -EINVAL; 2626 - } 2627 - for (i = 1; i <= data->block[0]; i++) 2628 - msgbuf0[i] = data->block[i]; 2629 - } 2630 - break; 2631 - default: 2632 - dev_err(&adapter->dev, "Unsupported transaction %d\n", size); 2633 - return -EOPNOTSUPP; 2634 - } 2635 - 2636 - i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK 2637 - && size != I2C_SMBUS_I2C_BLOCK_DATA); 2638 - if (i) { 2639 - /* Compute PEC if first message is a write */ 2640 - if (!(msg[0].flags & I2C_M_RD)) { 2641 - if (num == 1) /* Write only */ 2642 - i2c_smbus_add_pec(&msg[0]); 2643 - else /* Write followed by read */ 2644 - partial_pec = i2c_smbus_msg_pec(0, &msg[0]); 2645 - } 2646 - /* Ask for PEC if last message is a read */ 2647 - if (msg[num-1].flags & I2C_M_RD) 2648 - msg[num-1].len++; 2649 - } 2650 - 2651 - status = i2c_transfer(adapter, msg, num); 2652 - if (status < 0) 2653 - return status; 2654 - 2655 - /* Check PEC if last message is a read */ 2656 - if (i && (msg[num-1].flags & I2C_M_RD)) { 2657 - status = i2c_smbus_check_pec(partial_pec, &msg[num-1]); 2658 - if (status < 0) 2659 - return status; 2660 - } 2661 - 2662 - if (read_write == I2C_SMBUS_READ) 2663 - switch (size) { 2664 - case I2C_SMBUS_BYTE: 2665 - data->byte = msgbuf0[0]; 2666 - break; 2667 - case I2C_SMBUS_BYTE_DATA: 2668 - data->byte = msgbuf1[0]; 2669 - break; 2670 - case I2C_SMBUS_WORD_DATA: 2671 - case I2C_SMBUS_PROC_CALL: 2672 - data->word = msgbuf1[0] | (msgbuf1[1] << 8); 2673 - break; 2674 - case I2C_SMBUS_I2C_BLOCK_DATA: 2675 - for (i = 0; i < data->block[0]; i++) 2676 - data->block[i+1] = msgbuf1[i]; 2677 - break; 2678 - case I2C_SMBUS_BLOCK_DATA: 2679 - case I2C_SMBUS_BLOCK_PROC_CALL: 2680 - for (i = 0; i < msgbuf1[0] + 1; i++) 2681 - data->block[i] = msgbuf1[i]; 2682 - break; 2683 - } 2684 - return 0; 2685 - } 2686 - 2687 - /** 2688 - * i2c_smbus_xfer - execute SMBus protocol operations 2689 - * @adapter: Handle to I2C bus 2690 - * @addr: Address of SMBus slave on that bus 2691 - * @flags: I2C_CLIENT_* flags (usually zero or I2C_CLIENT_PEC) 2692 - * @read_write: I2C_SMBUS_READ or I2C_SMBUS_WRITE 2693 - * @command: Byte interpreted by slave, for protocols which use such bytes 2694 - * @protocol: SMBus protocol operation to execute, such as I2C_SMBUS_PROC_CALL 2695 - * @data: Data to be read or written 2696 - * 2697 - * This executes an SMBus protocol operation, and returns a negative 2698 - * errno code else zero on success. 2699 - */ 2700 - s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr, unsigned short flags, 2701 - char read_write, u8 command, int protocol, 2702 - union i2c_smbus_data *data) 2703 - { 2704 - unsigned long orig_jiffies; 2705 - int try; 2706 - s32 res; 2707 - 2708 - /* If enabled, the following two tracepoints are conditional on 2709 - * read_write and protocol. 2710 - */ 2711 - trace_smbus_write(adapter, addr, flags, read_write, 2712 - command, protocol, data); 2713 - trace_smbus_read(adapter, addr, flags, read_write, 2714 - command, protocol); 2715 - 2716 - flags &= I2C_M_TEN | I2C_CLIENT_PEC | I2C_CLIENT_SCCB; 2717 - 2718 - if (adapter->algo->smbus_xfer) { 2719 - i2c_lock_bus(adapter, I2C_LOCK_SEGMENT); 2720 - 2721 - /* Retry automatically on arbitration loss */ 2722 - orig_jiffies = jiffies; 2723 - for (res = 0, try = 0; try <= adapter->retries; try++) { 2724 - res = adapter->algo->smbus_xfer(adapter, addr, flags, 2725 - read_write, command, 2726 - protocol, data); 2727 - if (res != -EAGAIN) 2728 - break; 2729 - if (time_after(jiffies, 2730 - orig_jiffies + adapter->timeout)) 2731 - break; 2732 - } 2733 - i2c_unlock_bus(adapter, I2C_LOCK_SEGMENT); 2734 - 2735 - if (res != -EOPNOTSUPP || !adapter->algo->master_xfer) 2736 - goto trace; 2737 - /* 2738 - * Fall back to i2c_smbus_xfer_emulated if the adapter doesn't 2739 - * implement native support for the SMBus operation. 2740 - */ 2741 - } 2742 - 2743 - res = i2c_smbus_xfer_emulated(adapter, addr, flags, read_write, 2744 - command, protocol, data); 2745 - 2746 - trace: 2747 - /* If enabled, the reply tracepoint is conditional on read_write. */ 2748 - trace_smbus_reply(adapter, addr, flags, read_write, 2749 - command, protocol, data); 2750 - trace_smbus_result(adapter, addr, flags, read_write, 2751 - command, protocol, res); 2752 - 2753 - return res; 2754 - } 2755 - EXPORT_SYMBOL(i2c_smbus_xfer); 2756 - 2757 - /** 2758 - * i2c_smbus_read_i2c_block_data_or_emulated - read block or emulate 2759 - * @client: Handle to slave device 2760 - * @command: Byte interpreted by slave 2761 - * @length: Size of data block; SMBus allows at most I2C_SMBUS_BLOCK_MAX bytes 2762 - * @values: Byte array into which data will be read; big enough to hold 2763 - * the data returned by the slave. SMBus allows at most 2764 - * I2C_SMBUS_BLOCK_MAX bytes. 2765 - * 2766 - * This executes the SMBus "block read" protocol if supported by the adapter. 2767 - * If block read is not supported, it emulates it using either word or byte 2768 - * read protocols depending on availability. 2769 - * 2770 - * The addresses of the I2C slave device that are accessed with this function 2771 - * must be mapped to a linear region, so that a block read will have the same 2772 - * effect as a byte read. Before using this function you must double-check 2773 - * if the I2C slave does support exchanging a block transfer with a byte 2774 - * transfer. 2775 - */ 2776 - s32 i2c_smbus_read_i2c_block_data_or_emulated(const struct i2c_client *client, 2777 - u8 command, u8 length, u8 *values) 2778 - { 2779 - u8 i = 0; 2780 - int status; 2781 - 2782 - if (length > I2C_SMBUS_BLOCK_MAX) 2783 - length = I2C_SMBUS_BLOCK_MAX; 2784 - 2785 - if (i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_READ_I2C_BLOCK)) 2786 - return i2c_smbus_read_i2c_block_data(client, command, length, values); 2787 - 2788 - if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_READ_BYTE_DATA)) 2789 - return -EOPNOTSUPP; 2790 - 2791 - if (i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_READ_WORD_DATA)) { 2792 - while ((i + 2) <= length) { 2793 - status = i2c_smbus_read_word_data(client, command + i); 2794 - if (status < 0) 2795 - return status; 2796 - values[i] = status & 0xff; 2797 - values[i + 1] = status >> 8; 2798 - i += 2; 2799 - } 2800 - } 2801 - 2802 - while (i < length) { 2803 - status = i2c_smbus_read_byte_data(client, command + i); 2804 - if (status < 0) 2805 - return status; 2806 - values[i] = status; 2807 - i++; 2808 - } 2809 - 2810 - return i; 2811 - } 2812 - EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data_or_emulated); 2813 - 2814 - #if IS_ENABLED(CONFIG_I2C_SLAVE) 2815 - int i2c_slave_register(struct i2c_client *client, i2c_slave_cb_t slave_cb) 2816 - { 2817 - int ret; 2818 - 2819 - if (!client || !slave_cb) { 2820 - WARN(1, "insufficient data\n"); 2821 - return -EINVAL; 2822 - } 2823 - 2824 - if (!(client->flags & I2C_CLIENT_SLAVE)) 2825 - dev_warn(&client->dev, "%s: client slave flag not set. You might see address collisions\n", 2826 - __func__); 2827 - 2828 - if (!(client->flags & I2C_CLIENT_TEN)) { 2829 - /* Enforce stricter address checking */ 2830 - ret = i2c_check_7bit_addr_validity_strict(client->addr); 2831 - if (ret) { 2832 - dev_err(&client->dev, "%s: invalid address\n", __func__); 2833 - return ret; 2834 - } 2835 - } 2836 - 2837 - if (!client->adapter->algo->reg_slave) { 2838 - dev_err(&client->dev, "%s: not supported by adapter\n", __func__); 2839 - return -EOPNOTSUPP; 2840 - } 2841 - 2842 - client->slave_cb = slave_cb; 2843 - 2844 - i2c_lock_adapter(client->adapter); 2845 - ret = client->adapter->algo->reg_slave(client); 2846 - i2c_unlock_adapter(client->adapter); 2847 - 2848 - if (ret) { 2849 - client->slave_cb = NULL; 2850 - dev_err(&client->dev, "%s: adapter returned error %d\n", __func__, ret); 2851 - } 2852 - 2853 - return ret; 2854 - } 2855 - EXPORT_SYMBOL_GPL(i2c_slave_register); 2856 - 2857 - int i2c_slave_unregister(struct i2c_client *client) 2858 - { 2859 - int ret; 2860 - 2861 - if (!client->adapter->algo->unreg_slave) { 2862 - dev_err(&client->dev, "%s: not supported by adapter\n", __func__); 2863 - return -EOPNOTSUPP; 2864 - } 2865 - 2866 - i2c_lock_adapter(client->adapter); 2867 - ret = client->adapter->algo->unreg_slave(client); 2868 - i2c_unlock_adapter(client->adapter); 2869 - 2870 - if (ret == 0) 2871 - client->slave_cb = NULL; 2872 - else 2873 - dev_err(&client->dev, "%s: adapter returned error %d\n", __func__, ret); 2874 - 2875 - return ret; 2876 - } 2877 - EXPORT_SYMBOL_GPL(i2c_slave_unregister); 2878 - 2879 - /** 2880 - * i2c_detect_slave_mode - detect operation mode 2881 - * @dev: The device owning the bus 2882 - * 2883 - * This checks the device nodes for an I2C slave by checking the address 2884 - * used in the reg property. If the address match the I2C_OWN_SLAVE_ADDRESS 2885 - * flag this means the device is configured to act as a I2C slave and it will 2886 - * be listening at that address. 2887 - * 2888 - * Returns true if an I2C own slave address is detected, otherwise returns 2889 - * false. 2890 - */ 2891 - bool i2c_detect_slave_mode(struct device *dev) 2892 - { 2893 - if (IS_BUILTIN(CONFIG_OF) && dev->of_node) { 2894 - struct device_node *child; 2895 - u32 reg; 2896 - 2897 - for_each_child_of_node(dev->of_node, child) { 2898 - of_property_read_u32(child, "reg", &reg); 2899 - if (reg & I2C_OWN_SLAVE_ADDRESS) { 2900 - of_node_put(child); 2901 - return true; 2902 - } 2903 - } 2904 - } else if (IS_BUILTIN(CONFIG_ACPI) && ACPI_HANDLE(dev)) { 2905 - dev_dbg(dev, "ACPI slave is not supported yet\n"); 2906 - } 2907 - return false; 2908 - } 2909 - EXPORT_SYMBOL_GPL(i2c_detect_slave_mode); 2910 - 2911 - #endif 2912 3158 2913 3159 MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>"); 2914 3160 MODULE_DESCRIPTION("I2C-Bus main module");
+24
drivers/i2c/i2c-core.h
··· 27 27 extern struct list_head __i2c_board_list; 28 28 extern int __i2c_first_dynamic_bus_num; 29 29 30 + int i2c_check_addr_validity(unsigned addr, unsigned short flags); 31 + int i2c_check_7bit_addr_validity_strict(unsigned short addr); 32 + 33 + #ifdef CONFIG_ACPI 34 + void i2c_acpi_register_devices(struct i2c_adapter *adap); 35 + #else /* CONFIG_ACPI */ 36 + static inline void i2c_acpi_register_devices(struct i2c_adapter *adap) { } 37 + #endif /* CONFIG_ACPI */ 38 + extern struct notifier_block i2c_acpi_notifier; 39 + 40 + #ifdef CONFIG_ACPI_I2C_OPREGION 41 + int i2c_acpi_install_space_handler(struct i2c_adapter *adapter); 42 + void i2c_acpi_remove_space_handler(struct i2c_adapter *adapter); 43 + #else /* CONFIG_ACPI_I2C_OPREGION */ 44 + static inline int i2c_acpi_install_space_handler(struct i2c_adapter *adapter) { return 0; } 45 + static inline void i2c_acpi_remove_space_handler(struct i2c_adapter *adapter) { } 46 + #endif /* CONFIG_ACPI_I2C_OPREGION */ 47 + 48 + #ifdef CONFIG_OF 49 + void of_i2c_register_devices(struct i2c_adapter *adap); 50 + #else 51 + static inline void of_i2c_register_devices(struct i2c_adapter *adap) { } 52 + #endif 53 + extern struct notifier_block i2c_of_notifier;
+7 -7
drivers/i2c/i2c-stub.c
··· 16 16 */ 17 17 18 18 #define DEBUG 1 19 + #define pr_fmt(fmt) "i2c-stub: " fmt 19 20 20 21 #include <linux/errno.h> 21 22 #include <linux/i2c.h> ··· 343 342 if (!chip->bank_words) 344 343 return -ENOMEM; 345 344 346 - pr_debug("i2c-stub: Allocated %u banks of %u words each (registers 0x%02x to 0x%02x)\n", 345 + pr_debug("Allocated %u banks of %u words each (registers 0x%02x to 0x%02x)\n", 347 346 chip->bank_mask, chip->bank_size, chip->bank_start, 348 347 chip->bank_end); 349 348 ··· 364 363 int i, ret; 365 364 366 365 if (!chip_addr[0]) { 367 - pr_err("i2c-stub: Please specify a chip address\n"); 366 + pr_err("Please specify a chip address\n"); 368 367 return -ENODEV; 369 368 } 370 369 371 370 for (i = 0; i < MAX_CHIPS && chip_addr[i]; i++) { 372 371 if (chip_addr[i] < 0x03 || chip_addr[i] > 0x77) { 373 - pr_err("i2c-stub: Invalid chip address 0x%02x\n", 372 + pr_err("Invalid chip address 0x%02x\n", 374 373 chip_addr[i]); 375 374 return -EINVAL; 376 375 } 377 376 378 - pr_info("i2c-stub: Virtual chip at 0x%02x\n", chip_addr[i]); 377 + pr_info("Virtual chip at 0x%02x\n", chip_addr[i]); 379 378 } 380 379 381 380 /* Allocate memory for all chips at once */ 382 381 stub_chips_nr = i; 383 382 stub_chips = kcalloc(stub_chips_nr, sizeof(struct stub_chip), 384 383 GFP_KERNEL); 385 - if (!stub_chips) { 386 - pr_err("i2c-stub: Out of memory\n"); 384 + if (!stub_chips) 387 385 return -ENOMEM; 388 - } 386 + 389 387 for (i = 0; i < stub_chips_nr; i++) { 390 388 INIT_LIST_HEAD(&stub_chips[i].smbus_blocks); 391 389
+2
include/linux/i2c.h
··· 295 295 { 296 296 return client->slave_cb(client, event, val); 297 297 } 298 + #else 299 + static inline bool i2c_detect_slave_mode(struct device *dev) { return false; } 298 300 #endif 299 301 300 302 /**
-11
include/linux/i2c/i2c-sh_mobile.h
··· 1 - #ifndef __I2C_SH_MOBILE_H__ 2 - #define __I2C_SH_MOBILE_H__ 3 - 4 - #include <linux/platform_device.h> 5 - 6 - struct i2c_sh_mobile_platform_data { 7 - unsigned long bus_speed; 8 - unsigned int clks_per_count; 9 - }; 10 - 11 - #endif /* __I2C_SH_MOBILE_H__ */
+2 -224
include/trace/events/i2c.h
··· 1 - /* I2C and SMBUS message transfer tracepoints 1 + /* I2C message transfer tracepoints 2 2 * 3 3 * Copyright (C) 2013 Red Hat, Inc. All Rights Reserved. 4 4 * Written by David Howells (dhowells@redhat.com) ··· 18 18 #include <linux/tracepoint.h> 19 19 20 20 /* 21 - * drivers/i2c/i2c-core.c 21 + * drivers/i2c/i2c-core-base.c 22 22 */ 23 23 extern int i2c_transfer_trace_reg(void); 24 24 extern void i2c_transfer_trace_unreg(void); ··· 143 143 ), 144 144 i2c_transfer_trace_reg, 145 145 i2c_transfer_trace_unreg); 146 - 147 - /* 148 - * i2c_smbus_xfer() write data or procedure call request 149 - */ 150 - TRACE_EVENT_CONDITION(smbus_write, 151 - TP_PROTO(const struct i2c_adapter *adap, 152 - u16 addr, unsigned short flags, 153 - char read_write, u8 command, int protocol, 154 - const union i2c_smbus_data *data), 155 - TP_ARGS(adap, addr, flags, read_write, command, protocol, data), 156 - TP_CONDITION(read_write == I2C_SMBUS_WRITE || 157 - protocol == I2C_SMBUS_PROC_CALL || 158 - protocol == I2C_SMBUS_BLOCK_PROC_CALL), 159 - TP_STRUCT__entry( 160 - __field(int, adapter_nr ) 161 - __field(__u16, addr ) 162 - __field(__u16, flags ) 163 - __field(__u8, command ) 164 - __field(__u8, len ) 165 - __field(__u32, protocol ) 166 - __array(__u8, buf, I2C_SMBUS_BLOCK_MAX + 2) ), 167 - TP_fast_assign( 168 - __entry->adapter_nr = adap->nr; 169 - __entry->addr = addr; 170 - __entry->flags = flags; 171 - __entry->command = command; 172 - __entry->protocol = protocol; 173 - 174 - switch (protocol) { 175 - case I2C_SMBUS_BYTE_DATA: 176 - __entry->len = 1; 177 - goto copy; 178 - case I2C_SMBUS_WORD_DATA: 179 - case I2C_SMBUS_PROC_CALL: 180 - __entry->len = 2; 181 - goto copy; 182 - case I2C_SMBUS_BLOCK_DATA: 183 - case I2C_SMBUS_BLOCK_PROC_CALL: 184 - case I2C_SMBUS_I2C_BLOCK_DATA: 185 - __entry->len = data->block[0] + 1; 186 - copy: 187 - memcpy(__entry->buf, data->block, __entry->len); 188 - break; 189 - case I2C_SMBUS_QUICK: 190 - case I2C_SMBUS_BYTE: 191 - case I2C_SMBUS_I2C_BLOCK_BROKEN: 192 - default: 193 - __entry->len = 0; 194 - } 195 - ), 196 - TP_printk("i2c-%d a=%03x f=%04x c=%x %s l=%u [%*phD]", 197 - __entry->adapter_nr, 198 - __entry->addr, 199 - __entry->flags, 200 - __entry->command, 201 - __print_symbolic(__entry->protocol, 202 - { I2C_SMBUS_QUICK, "QUICK" }, 203 - { I2C_SMBUS_BYTE, "BYTE" }, 204 - { I2C_SMBUS_BYTE_DATA, "BYTE_DATA" }, 205 - { I2C_SMBUS_WORD_DATA, "WORD_DATA" }, 206 - { I2C_SMBUS_PROC_CALL, "PROC_CALL" }, 207 - { I2C_SMBUS_BLOCK_DATA, "BLOCK_DATA" }, 208 - { I2C_SMBUS_I2C_BLOCK_BROKEN, "I2C_BLOCK_BROKEN" }, 209 - { I2C_SMBUS_BLOCK_PROC_CALL, "BLOCK_PROC_CALL" }, 210 - { I2C_SMBUS_I2C_BLOCK_DATA, "I2C_BLOCK_DATA" }), 211 - __entry->len, 212 - __entry->len, __entry->buf 213 - )); 214 - 215 - /* 216 - * i2c_smbus_xfer() read data request 217 - */ 218 - TRACE_EVENT_CONDITION(smbus_read, 219 - TP_PROTO(const struct i2c_adapter *adap, 220 - u16 addr, unsigned short flags, 221 - char read_write, u8 command, int protocol), 222 - TP_ARGS(adap, addr, flags, read_write, command, protocol), 223 - TP_CONDITION(!(read_write == I2C_SMBUS_WRITE || 224 - protocol == I2C_SMBUS_PROC_CALL || 225 - protocol == I2C_SMBUS_BLOCK_PROC_CALL)), 226 - TP_STRUCT__entry( 227 - __field(int, adapter_nr ) 228 - __field(__u16, flags ) 229 - __field(__u16, addr ) 230 - __field(__u8, command ) 231 - __field(__u32, protocol ) 232 - __array(__u8, buf, I2C_SMBUS_BLOCK_MAX + 2) ), 233 - TP_fast_assign( 234 - __entry->adapter_nr = adap->nr; 235 - __entry->addr = addr; 236 - __entry->flags = flags; 237 - __entry->command = command; 238 - __entry->protocol = protocol; 239 - ), 240 - TP_printk("i2c-%d a=%03x f=%04x c=%x %s", 241 - __entry->adapter_nr, 242 - __entry->addr, 243 - __entry->flags, 244 - __entry->command, 245 - __print_symbolic(__entry->protocol, 246 - { I2C_SMBUS_QUICK, "QUICK" }, 247 - { I2C_SMBUS_BYTE, "BYTE" }, 248 - { I2C_SMBUS_BYTE_DATA, "BYTE_DATA" }, 249 - { I2C_SMBUS_WORD_DATA, "WORD_DATA" }, 250 - { I2C_SMBUS_PROC_CALL, "PROC_CALL" }, 251 - { I2C_SMBUS_BLOCK_DATA, "BLOCK_DATA" }, 252 - { I2C_SMBUS_I2C_BLOCK_BROKEN, "I2C_BLOCK_BROKEN" }, 253 - { I2C_SMBUS_BLOCK_PROC_CALL, "BLOCK_PROC_CALL" }, 254 - { I2C_SMBUS_I2C_BLOCK_DATA, "I2C_BLOCK_DATA" }) 255 - )); 256 - 257 - /* 258 - * i2c_smbus_xfer() read data or procedure call reply 259 - */ 260 - TRACE_EVENT_CONDITION(smbus_reply, 261 - TP_PROTO(const struct i2c_adapter *adap, 262 - u16 addr, unsigned short flags, 263 - char read_write, u8 command, int protocol, 264 - const union i2c_smbus_data *data), 265 - TP_ARGS(adap, addr, flags, read_write, command, protocol, data), 266 - TP_CONDITION(read_write == I2C_SMBUS_READ), 267 - TP_STRUCT__entry( 268 - __field(int, adapter_nr ) 269 - __field(__u16, addr ) 270 - __field(__u16, flags ) 271 - __field(__u8, command ) 272 - __field(__u8, len ) 273 - __field(__u32, protocol ) 274 - __array(__u8, buf, I2C_SMBUS_BLOCK_MAX + 2) ), 275 - TP_fast_assign( 276 - __entry->adapter_nr = adap->nr; 277 - __entry->addr = addr; 278 - __entry->flags = flags; 279 - __entry->command = command; 280 - __entry->protocol = protocol; 281 - 282 - switch (protocol) { 283 - case I2C_SMBUS_BYTE: 284 - case I2C_SMBUS_BYTE_DATA: 285 - __entry->len = 1; 286 - goto copy; 287 - case I2C_SMBUS_WORD_DATA: 288 - case I2C_SMBUS_PROC_CALL: 289 - __entry->len = 2; 290 - goto copy; 291 - case I2C_SMBUS_BLOCK_DATA: 292 - case I2C_SMBUS_BLOCK_PROC_CALL: 293 - case I2C_SMBUS_I2C_BLOCK_DATA: 294 - __entry->len = data->block[0] + 1; 295 - copy: 296 - memcpy(__entry->buf, data->block, __entry->len); 297 - break; 298 - case I2C_SMBUS_QUICK: 299 - case I2C_SMBUS_I2C_BLOCK_BROKEN: 300 - default: 301 - __entry->len = 0; 302 - } 303 - ), 304 - TP_printk("i2c-%d a=%03x f=%04x c=%x %s l=%u [%*phD]", 305 - __entry->adapter_nr, 306 - __entry->addr, 307 - __entry->flags, 308 - __entry->command, 309 - __print_symbolic(__entry->protocol, 310 - { I2C_SMBUS_QUICK, "QUICK" }, 311 - { I2C_SMBUS_BYTE, "BYTE" }, 312 - { I2C_SMBUS_BYTE_DATA, "BYTE_DATA" }, 313 - { I2C_SMBUS_WORD_DATA, "WORD_DATA" }, 314 - { I2C_SMBUS_PROC_CALL, "PROC_CALL" }, 315 - { I2C_SMBUS_BLOCK_DATA, "BLOCK_DATA" }, 316 - { I2C_SMBUS_I2C_BLOCK_BROKEN, "I2C_BLOCK_BROKEN" }, 317 - { I2C_SMBUS_BLOCK_PROC_CALL, "BLOCK_PROC_CALL" }, 318 - { I2C_SMBUS_I2C_BLOCK_DATA, "I2C_BLOCK_DATA" }), 319 - __entry->len, 320 - __entry->len, __entry->buf 321 - )); 322 - 323 - /* 324 - * i2c_smbus_xfer() result 325 - */ 326 - TRACE_EVENT(smbus_result, 327 - TP_PROTO(const struct i2c_adapter *adap, 328 - u16 addr, unsigned short flags, 329 - char read_write, u8 command, int protocol, 330 - int res), 331 - TP_ARGS(adap, addr, flags, read_write, command, protocol, res), 332 - TP_STRUCT__entry( 333 - __field(int, adapter_nr ) 334 - __field(__u16, addr ) 335 - __field(__u16, flags ) 336 - __field(__u8, read_write ) 337 - __field(__u8, command ) 338 - __field(__s16, res ) 339 - __field(__u32, protocol ) 340 - ), 341 - TP_fast_assign( 342 - __entry->adapter_nr = adap->nr; 343 - __entry->addr = addr; 344 - __entry->flags = flags; 345 - __entry->read_write = read_write; 346 - __entry->command = command; 347 - __entry->protocol = protocol; 348 - __entry->res = res; 349 - ), 350 - TP_printk("i2c-%d a=%03x f=%04x c=%x %s %s res=%d", 351 - __entry->adapter_nr, 352 - __entry->addr, 353 - __entry->flags, 354 - __entry->command, 355 - __print_symbolic(__entry->protocol, 356 - { I2C_SMBUS_QUICK, "QUICK" }, 357 - { I2C_SMBUS_BYTE, "BYTE" }, 358 - { I2C_SMBUS_BYTE_DATA, "BYTE_DATA" }, 359 - { I2C_SMBUS_WORD_DATA, "WORD_DATA" }, 360 - { I2C_SMBUS_PROC_CALL, "PROC_CALL" }, 361 - { I2C_SMBUS_BLOCK_DATA, "BLOCK_DATA" }, 362 - { I2C_SMBUS_I2C_BLOCK_BROKEN, "I2C_BLOCK_BROKEN" }, 363 - { I2C_SMBUS_BLOCK_PROC_CALL, "BLOCK_PROC_CALL" }, 364 - { I2C_SMBUS_I2C_BLOCK_DATA, "I2C_BLOCK_DATA" }), 365 - __entry->read_write == I2C_SMBUS_WRITE ? "wr" : "rd", 366 - __entry->res 367 - )); 368 146 369 147 #endif /* _TRACE_I2C_H */ 370 148
+249
include/trace/events/smbus.h
··· 1 + /* SMBUS message transfer tracepoints 2 + * 3 + * Copyright (C) 2013 Red Hat, Inc. All Rights Reserved. 4 + * Written by David Howells (dhowells@redhat.com) 5 + * 6 + * This program is free software; you can redistribute it and/or 7 + * modify it under the terms of the GNU General Public Licence 8 + * as published by the Free Software Foundation; either version 9 + * 2 of the Licence, or (at your option) any later version. 10 + */ 11 + #undef TRACE_SYSTEM 12 + #define TRACE_SYSTEM smbus 13 + 14 + #if !defined(_TRACE_SMBUS_H) || defined(TRACE_HEADER_MULTI_READ) 15 + #define _TRACE_SMBUS_H 16 + 17 + #include <linux/i2c.h> 18 + #include <linux/tracepoint.h> 19 + 20 + /* 21 + * drivers/i2c/i2c-core-smbus.c 22 + */ 23 + 24 + /* 25 + * i2c_smbus_xfer() write data or procedure call request 26 + */ 27 + TRACE_EVENT_CONDITION(smbus_write, 28 + TP_PROTO(const struct i2c_adapter *adap, 29 + u16 addr, unsigned short flags, 30 + char read_write, u8 command, int protocol, 31 + const union i2c_smbus_data *data), 32 + TP_ARGS(adap, addr, flags, read_write, command, protocol, data), 33 + TP_CONDITION(read_write == I2C_SMBUS_WRITE || 34 + protocol == I2C_SMBUS_PROC_CALL || 35 + protocol == I2C_SMBUS_BLOCK_PROC_CALL), 36 + TP_STRUCT__entry( 37 + __field(int, adapter_nr ) 38 + __field(__u16, addr ) 39 + __field(__u16, flags ) 40 + __field(__u8, command ) 41 + __field(__u8, len ) 42 + __field(__u32, protocol ) 43 + __array(__u8, buf, I2C_SMBUS_BLOCK_MAX + 2) ), 44 + TP_fast_assign( 45 + __entry->adapter_nr = adap->nr; 46 + __entry->addr = addr; 47 + __entry->flags = flags; 48 + __entry->command = command; 49 + __entry->protocol = protocol; 50 + 51 + switch (protocol) { 52 + case I2C_SMBUS_BYTE_DATA: 53 + __entry->len = 1; 54 + goto copy; 55 + case I2C_SMBUS_WORD_DATA: 56 + case I2C_SMBUS_PROC_CALL: 57 + __entry->len = 2; 58 + goto copy; 59 + case I2C_SMBUS_BLOCK_DATA: 60 + case I2C_SMBUS_BLOCK_PROC_CALL: 61 + case I2C_SMBUS_I2C_BLOCK_DATA: 62 + __entry->len = data->block[0] + 1; 63 + copy: 64 + memcpy(__entry->buf, data->block, __entry->len); 65 + break; 66 + case I2C_SMBUS_QUICK: 67 + case I2C_SMBUS_BYTE: 68 + case I2C_SMBUS_I2C_BLOCK_BROKEN: 69 + default: 70 + __entry->len = 0; 71 + } 72 + ), 73 + TP_printk("i2c-%d a=%03x f=%04x c=%x %s l=%u [%*phD]", 74 + __entry->adapter_nr, 75 + __entry->addr, 76 + __entry->flags, 77 + __entry->command, 78 + __print_symbolic(__entry->protocol, 79 + { I2C_SMBUS_QUICK, "QUICK" }, 80 + { I2C_SMBUS_BYTE, "BYTE" }, 81 + { I2C_SMBUS_BYTE_DATA, "BYTE_DATA" }, 82 + { I2C_SMBUS_WORD_DATA, "WORD_DATA" }, 83 + { I2C_SMBUS_PROC_CALL, "PROC_CALL" }, 84 + { I2C_SMBUS_BLOCK_DATA, "BLOCK_DATA" }, 85 + { I2C_SMBUS_I2C_BLOCK_BROKEN, "I2C_BLOCK_BROKEN" }, 86 + { I2C_SMBUS_BLOCK_PROC_CALL, "BLOCK_PROC_CALL" }, 87 + { I2C_SMBUS_I2C_BLOCK_DATA, "I2C_BLOCK_DATA" }), 88 + __entry->len, 89 + __entry->len, __entry->buf 90 + )); 91 + 92 + /* 93 + * i2c_smbus_xfer() read data request 94 + */ 95 + TRACE_EVENT_CONDITION(smbus_read, 96 + TP_PROTO(const struct i2c_adapter *adap, 97 + u16 addr, unsigned short flags, 98 + char read_write, u8 command, int protocol), 99 + TP_ARGS(adap, addr, flags, read_write, command, protocol), 100 + TP_CONDITION(!(read_write == I2C_SMBUS_WRITE || 101 + protocol == I2C_SMBUS_PROC_CALL || 102 + protocol == I2C_SMBUS_BLOCK_PROC_CALL)), 103 + TP_STRUCT__entry( 104 + __field(int, adapter_nr ) 105 + __field(__u16, flags ) 106 + __field(__u16, addr ) 107 + __field(__u8, command ) 108 + __field(__u32, protocol ) 109 + __array(__u8, buf, I2C_SMBUS_BLOCK_MAX + 2) ), 110 + TP_fast_assign( 111 + __entry->adapter_nr = adap->nr; 112 + __entry->addr = addr; 113 + __entry->flags = flags; 114 + __entry->command = command; 115 + __entry->protocol = protocol; 116 + ), 117 + TP_printk("i2c-%d a=%03x f=%04x c=%x %s", 118 + __entry->adapter_nr, 119 + __entry->addr, 120 + __entry->flags, 121 + __entry->command, 122 + __print_symbolic(__entry->protocol, 123 + { I2C_SMBUS_QUICK, "QUICK" }, 124 + { I2C_SMBUS_BYTE, "BYTE" }, 125 + { I2C_SMBUS_BYTE_DATA, "BYTE_DATA" }, 126 + { I2C_SMBUS_WORD_DATA, "WORD_DATA" }, 127 + { I2C_SMBUS_PROC_CALL, "PROC_CALL" }, 128 + { I2C_SMBUS_BLOCK_DATA, "BLOCK_DATA" }, 129 + { I2C_SMBUS_I2C_BLOCK_BROKEN, "I2C_BLOCK_BROKEN" }, 130 + { I2C_SMBUS_BLOCK_PROC_CALL, "BLOCK_PROC_CALL" }, 131 + { I2C_SMBUS_I2C_BLOCK_DATA, "I2C_BLOCK_DATA" }) 132 + )); 133 + 134 + /* 135 + * i2c_smbus_xfer() read data or procedure call reply 136 + */ 137 + TRACE_EVENT_CONDITION(smbus_reply, 138 + TP_PROTO(const struct i2c_adapter *adap, 139 + u16 addr, unsigned short flags, 140 + char read_write, u8 command, int protocol, 141 + const union i2c_smbus_data *data), 142 + TP_ARGS(adap, addr, flags, read_write, command, protocol, data), 143 + TP_CONDITION(read_write == I2C_SMBUS_READ), 144 + TP_STRUCT__entry( 145 + __field(int, adapter_nr ) 146 + __field(__u16, addr ) 147 + __field(__u16, flags ) 148 + __field(__u8, command ) 149 + __field(__u8, len ) 150 + __field(__u32, protocol ) 151 + __array(__u8, buf, I2C_SMBUS_BLOCK_MAX + 2) ), 152 + TP_fast_assign( 153 + __entry->adapter_nr = adap->nr; 154 + __entry->addr = addr; 155 + __entry->flags = flags; 156 + __entry->command = command; 157 + __entry->protocol = protocol; 158 + 159 + switch (protocol) { 160 + case I2C_SMBUS_BYTE: 161 + case I2C_SMBUS_BYTE_DATA: 162 + __entry->len = 1; 163 + goto copy; 164 + case I2C_SMBUS_WORD_DATA: 165 + case I2C_SMBUS_PROC_CALL: 166 + __entry->len = 2; 167 + goto copy; 168 + case I2C_SMBUS_BLOCK_DATA: 169 + case I2C_SMBUS_BLOCK_PROC_CALL: 170 + case I2C_SMBUS_I2C_BLOCK_DATA: 171 + __entry->len = data->block[0] + 1; 172 + copy: 173 + memcpy(__entry->buf, data->block, __entry->len); 174 + break; 175 + case I2C_SMBUS_QUICK: 176 + case I2C_SMBUS_I2C_BLOCK_BROKEN: 177 + default: 178 + __entry->len = 0; 179 + } 180 + ), 181 + TP_printk("i2c-%d a=%03x f=%04x c=%x %s l=%u [%*phD]", 182 + __entry->adapter_nr, 183 + __entry->addr, 184 + __entry->flags, 185 + __entry->command, 186 + __print_symbolic(__entry->protocol, 187 + { I2C_SMBUS_QUICK, "QUICK" }, 188 + { I2C_SMBUS_BYTE, "BYTE" }, 189 + { I2C_SMBUS_BYTE_DATA, "BYTE_DATA" }, 190 + { I2C_SMBUS_WORD_DATA, "WORD_DATA" }, 191 + { I2C_SMBUS_PROC_CALL, "PROC_CALL" }, 192 + { I2C_SMBUS_BLOCK_DATA, "BLOCK_DATA" }, 193 + { I2C_SMBUS_I2C_BLOCK_BROKEN, "I2C_BLOCK_BROKEN" }, 194 + { I2C_SMBUS_BLOCK_PROC_CALL, "BLOCK_PROC_CALL" }, 195 + { I2C_SMBUS_I2C_BLOCK_DATA, "I2C_BLOCK_DATA" }), 196 + __entry->len, 197 + __entry->len, __entry->buf 198 + )); 199 + 200 + /* 201 + * i2c_smbus_xfer() result 202 + */ 203 + TRACE_EVENT(smbus_result, 204 + TP_PROTO(const struct i2c_adapter *adap, 205 + u16 addr, unsigned short flags, 206 + char read_write, u8 command, int protocol, 207 + int res), 208 + TP_ARGS(adap, addr, flags, read_write, command, protocol, res), 209 + TP_STRUCT__entry( 210 + __field(int, adapter_nr ) 211 + __field(__u16, addr ) 212 + __field(__u16, flags ) 213 + __field(__u8, read_write ) 214 + __field(__u8, command ) 215 + __field(__s16, res ) 216 + __field(__u32, protocol ) 217 + ), 218 + TP_fast_assign( 219 + __entry->adapter_nr = adap->nr; 220 + __entry->addr = addr; 221 + __entry->flags = flags; 222 + __entry->read_write = read_write; 223 + __entry->command = command; 224 + __entry->protocol = protocol; 225 + __entry->res = res; 226 + ), 227 + TP_printk("i2c-%d a=%03x f=%04x c=%x %s %s res=%d", 228 + __entry->adapter_nr, 229 + __entry->addr, 230 + __entry->flags, 231 + __entry->command, 232 + __print_symbolic(__entry->protocol, 233 + { I2C_SMBUS_QUICK, "QUICK" }, 234 + { I2C_SMBUS_BYTE, "BYTE" }, 235 + { I2C_SMBUS_BYTE_DATA, "BYTE_DATA" }, 236 + { I2C_SMBUS_WORD_DATA, "WORD_DATA" }, 237 + { I2C_SMBUS_PROC_CALL, "PROC_CALL" }, 238 + { I2C_SMBUS_BLOCK_DATA, "BLOCK_DATA" }, 239 + { I2C_SMBUS_I2C_BLOCK_BROKEN, "I2C_BLOCK_BROKEN" }, 240 + { I2C_SMBUS_BLOCK_PROC_CALL, "BLOCK_PROC_CALL" }, 241 + { I2C_SMBUS_I2C_BLOCK_DATA, "I2C_BLOCK_DATA" }), 242 + __entry->read_write == I2C_SMBUS_WRITE ? "wr" : "rd", 243 + __entry->res 244 + )); 245 + 246 + #endif /* _TRACE_SMBUS_H */ 247 + 248 + /* This part must be outside protection */ 249 + #include <trace/define_trace.h>