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

Configure Feed

Select the types of activity you want to include in your feed.

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

Pull i2c fixes from Wolfram Sang:
"This is probably not the kind of pull request you want to see that
late in the cycle. Yet, the ACPI refactorization was problematic
again and caused another two issues which need fixing. My holidays
with limited internet (plus travelling) and the developer's illness
didn't help either :(

The details:

- ACPI code was refactored out into a seperate file and as a
side-effect, the i2c-core module got renamed. Jean Delvare
rightfully complained about the rename being problematic for
distributions. So, Mika and I thought the least problematic way to
deal with it is to move all the code back into the main i2c core
source file. This is mainly a huge code move with some #ifdeffery
applied. No functional code changes. Our personal tests and the
testbots did not find problems. (I was thinking about reverting,
too, yet that would also have ~800 lines changed)

- The new ACPI code also had a NULL pointer exception, thanks to
Peter for finding and fixing it.

- Mikko fixed a locking problem by decoupling clock_prepare and
clock_enable.

- Addy learnt that the datasheet was wrong and reimplemented the
frequency setup according to the new algorithm.

- Fan fixed an off-by-one error when copying data

- Janusz fixed a copy'n'paste bug which gave a wrong error message

- Sergei made sure that "don't touch" bits are not accessed"

* 'i2c/for-current' of git://git.kernel.org/pub/scm/linux/kernel/git/wsa/linux:
i2c: acpi: Fix NULL Pointer dereference
i2c: move acpi code back into the core
i2c: rk3x: fix divisor calculation for SCL frequency
i2c: mxs: fix error message in pio transfer
i2c: ismt: use correct length when copy buffer
i2c: rcar: fix RCAR_IRQ_ACK_{RECV|SEND}
i2c: tegra: Move clk_prepare/clk_set_rate to probe

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