vt: vt_ioctl: fix VT_DISALLOCATE freeing in-use virtual console
[linux/fpc-iii.git] / drivers / i2c / i2c-core-acpi.c
blobeb056935938759f38026fcd62d460053e1cd7c4a
1 /*
2 * Linux I2C core ACPI support code
4 * Copyright (C) 2014 Intel Corp, Author: Lan Tianyu <tianyu.lan@intel.com>
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.
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>
20 #include "i2c-core.h"
22 struct i2c_acpi_handler_data {
23 struct acpi_connection_info info;
24 struct i2c_adapter *adapter;
27 struct gsb_buffer {
28 u8 status;
29 u8 len;
30 union {
31 u16 wdata;
32 u8 bdata;
33 u8 data[0];
35 } __packed;
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 u32 force_speed;
49 static int i2c_acpi_fill_info(struct acpi_resource *ares, void *data)
51 struct i2c_acpi_lookup *lookup = data;
52 struct i2c_board_info *info = lookup->info;
53 struct acpi_resource_i2c_serialbus *sb;
54 acpi_status status;
56 if (info->addr || ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS)
57 return 1;
59 sb = &ares->data.i2c_serial_bus;
60 if (sb->type != ACPI_RESOURCE_SERIAL_TYPE_I2C)
61 return 1;
63 if (lookup->index != -1 && lookup->n++ != lookup->index)
64 return 1;
66 status = acpi_get_handle(lookup->device_handle,
67 sb->resource_source.string_ptr,
68 &lookup->adapter_handle);
69 if (!ACPI_SUCCESS(status))
70 return 1;
72 info->addr = sb->slave_address;
73 lookup->speed = sb->connection_speed;
74 if (sb->access_mode == ACPI_I2C_10BIT_MODE)
75 info->flags |= I2C_CLIENT_TEN;
77 return 1;
80 static const struct acpi_device_id i2c_acpi_ignored_device_ids[] = {
82 * ACPI video acpi_devices, which are handled by the acpi-video driver
83 * sometimes contain a SERIAL_TYPE_I2C ACPI resource, ignore these.
85 { ACPI_VIDEO_HID, 0 },
89 static int i2c_acpi_do_lookup(struct acpi_device *adev,
90 struct i2c_acpi_lookup *lookup)
92 struct i2c_board_info *info = lookup->info;
93 struct list_head resource_list;
94 int ret;
96 if (acpi_bus_get_status(adev) || !adev->status.present ||
97 acpi_device_enumerated(adev))
98 return -EINVAL;
100 if (acpi_match_device_ids(adev, i2c_acpi_ignored_device_ids) == 0)
101 return -ENODEV;
103 memset(info, 0, sizeof(*info));
104 lookup->device_handle = acpi_device_handle(adev);
106 /* Look up for I2cSerialBus resource */
107 INIT_LIST_HEAD(&resource_list);
108 ret = acpi_dev_get_resources(adev, &resource_list,
109 i2c_acpi_fill_info, lookup);
110 acpi_dev_free_resource_list(&resource_list);
112 if (ret < 0 || !info->addr)
113 return -EINVAL;
115 return 0;
118 static int i2c_acpi_get_info(struct acpi_device *adev,
119 struct i2c_board_info *info,
120 struct i2c_adapter *adapter,
121 acpi_handle *adapter_handle)
123 struct list_head resource_list;
124 struct resource_entry *entry;
125 struct i2c_acpi_lookup lookup;
126 int ret;
128 memset(&lookup, 0, sizeof(lookup));
129 lookup.info = info;
130 lookup.index = -1;
132 ret = i2c_acpi_do_lookup(adev, &lookup);
133 if (ret)
134 return ret;
136 if (adapter) {
137 /* The adapter must match the one in I2cSerialBus() connector */
138 if (ACPI_HANDLE(&adapter->dev) != lookup.adapter_handle)
139 return -ENODEV;
140 } else {
141 struct acpi_device *adapter_adev;
143 /* The adapter must be present */
144 if (acpi_bus_get_device(lookup.adapter_handle, &adapter_adev))
145 return -ENODEV;
146 if (acpi_bus_get_status(adapter_adev) ||
147 !adapter_adev->status.present)
148 return -ENODEV;
151 info->fwnode = acpi_fwnode_handle(adev);
152 if (adapter_handle)
153 *adapter_handle = lookup.adapter_handle;
155 /* Then fill IRQ number if any */
156 INIT_LIST_HEAD(&resource_list);
157 ret = acpi_dev_get_resources(adev, &resource_list, NULL, NULL);
158 if (ret < 0)
159 return -EINVAL;
161 resource_list_for_each_entry(entry, &resource_list) {
162 if (resource_type(entry->res) == IORESOURCE_IRQ) {
163 info->irq = entry->res->start;
164 break;
168 acpi_dev_free_resource_list(&resource_list);
170 acpi_set_modalias(adev, dev_name(&adev->dev), info->type,
171 sizeof(info->type));
173 return 0;
176 static void i2c_acpi_register_device(struct i2c_adapter *adapter,
177 struct acpi_device *adev,
178 struct i2c_board_info *info)
180 adev->power.flags.ignore_parent = true;
181 acpi_device_set_enumerated(adev);
183 if (!i2c_new_device(adapter, info)) {
184 adev->power.flags.ignore_parent = false;
185 dev_err(&adapter->dev,
186 "failed to add I2C device %s from ACPI\n",
187 dev_name(&adev->dev));
191 static acpi_status i2c_acpi_add_device(acpi_handle handle, u32 level,
192 void *data, void **return_value)
194 struct i2c_adapter *adapter = data;
195 struct acpi_device *adev;
196 struct i2c_board_info info;
198 if (acpi_bus_get_device(handle, &adev))
199 return AE_OK;
201 if (i2c_acpi_get_info(adev, &info, adapter, NULL))
202 return AE_OK;
204 i2c_acpi_register_device(adapter, adev, &info);
206 return AE_OK;
209 #define I2C_ACPI_MAX_SCAN_DEPTH 32
212 * i2c_acpi_register_devices - enumerate I2C slave devices behind adapter
213 * @adap: pointer to adapter
215 * Enumerate all I2C slave devices behind this adapter by walking the ACPI
216 * namespace. When a device is found it will be added to the Linux device
217 * model and bound to the corresponding ACPI handle.
219 void i2c_acpi_register_devices(struct i2c_adapter *adap)
221 acpi_status status;
223 if (!has_acpi_companion(&adap->dev))
224 return;
226 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
227 I2C_ACPI_MAX_SCAN_DEPTH,
228 i2c_acpi_add_device, NULL,
229 adap, NULL);
230 if (ACPI_FAILURE(status))
231 dev_warn(&adap->dev, "failed to enumerate I2C slaves\n");
234 const struct acpi_device_id *
235 i2c_acpi_match_device(const struct acpi_device_id *matches,
236 struct i2c_client *client)
238 if (!(client && matches))
239 return NULL;
241 return acpi_match_device(matches, &client->dev);
244 static const struct acpi_device_id i2c_acpi_force_400khz_device_ids[] = {
246 * These Silead touchscreen controllers only work at 400KHz, for
247 * some reason they do not work at 100KHz. On some devices the ACPI
248 * tables list another device at their bus as only being capable
249 * of 100KHz, testing has shown that these other devices work fine
250 * at 400KHz (as can be expected of any recent i2c hw) so we force
251 * the speed of the bus to 400 KHz if a Silead device is present.
253 { "MSSL1680", 0 },
257 static acpi_status i2c_acpi_lookup_speed(acpi_handle handle, u32 level,
258 void *data, void **return_value)
260 struct i2c_acpi_lookup *lookup = data;
261 struct acpi_device *adev;
263 if (acpi_bus_get_device(handle, &adev))
264 return AE_OK;
266 if (i2c_acpi_do_lookup(adev, lookup))
267 return AE_OK;
269 if (lookup->search_handle != lookup->adapter_handle)
270 return AE_OK;
272 if (lookup->speed <= lookup->min_speed)
273 lookup->min_speed = lookup->speed;
275 if (acpi_match_device_ids(adev, i2c_acpi_force_400khz_device_ids) == 0)
276 lookup->force_speed = 400000;
278 return AE_OK;
282 * i2c_acpi_find_bus_speed - find I2C bus speed from ACPI
283 * @dev: The device owning the bus
285 * Find the I2C bus speed by walking the ACPI namespace for all I2C slaves
286 * devices connected to this bus and use the speed of slowest device.
288 * Returns the speed in Hz or zero
290 u32 i2c_acpi_find_bus_speed(struct device *dev)
292 struct i2c_acpi_lookup lookup;
293 struct i2c_board_info dummy;
294 acpi_status status;
296 if (!has_acpi_companion(dev))
297 return 0;
299 memset(&lookup, 0, sizeof(lookup));
300 lookup.search_handle = ACPI_HANDLE(dev);
301 lookup.min_speed = UINT_MAX;
302 lookup.info = &dummy;
303 lookup.index = -1;
305 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
306 I2C_ACPI_MAX_SCAN_DEPTH,
307 i2c_acpi_lookup_speed, NULL,
308 &lookup, NULL);
310 if (ACPI_FAILURE(status)) {
311 dev_warn(dev, "unable to find I2C bus speed from ACPI\n");
312 return 0;
315 if (lookup.force_speed) {
316 if (lookup.force_speed != lookup.min_speed)
317 dev_warn(dev, FW_BUG "DSDT uses known not-working I2C bus speed %d, forcing it to %d\n",
318 lookup.min_speed, lookup.force_speed);
319 return lookup.force_speed;
320 } else if (lookup.min_speed != UINT_MAX) {
321 return lookup.min_speed;
322 } else {
323 return 0;
326 EXPORT_SYMBOL_GPL(i2c_acpi_find_bus_speed);
328 static int i2c_acpi_find_match_adapter(struct device *dev, void *data)
330 struct i2c_adapter *adapter = i2c_verify_adapter(dev);
332 if (!adapter)
333 return 0;
335 return ACPI_HANDLE(dev) == (acpi_handle)data;
338 static int i2c_acpi_find_match_device(struct device *dev, void *data)
340 return ACPI_COMPANION(dev) == data;
343 static struct i2c_adapter *i2c_acpi_find_adapter_by_handle(acpi_handle handle)
345 struct device *dev;
347 dev = bus_find_device(&i2c_bus_type, NULL, handle,
348 i2c_acpi_find_match_adapter);
349 return dev ? i2c_verify_adapter(dev) : NULL;
352 static struct i2c_client *i2c_acpi_find_client_by_adev(struct acpi_device *adev)
354 struct device *dev;
355 struct i2c_client *client;
357 dev = bus_find_device(&i2c_bus_type, NULL, adev,
358 i2c_acpi_find_match_device);
359 if (!dev)
360 return NULL;
362 client = i2c_verify_client(dev);
363 if (!client)
364 put_device(dev);
366 return client;
369 static int i2c_acpi_notify(struct notifier_block *nb, unsigned long value,
370 void *arg)
372 struct acpi_device *adev = arg;
373 struct i2c_board_info info;
374 acpi_handle adapter_handle;
375 struct i2c_adapter *adapter;
376 struct i2c_client *client;
378 switch (value) {
379 case ACPI_RECONFIG_DEVICE_ADD:
380 if (i2c_acpi_get_info(adev, &info, NULL, &adapter_handle))
381 break;
383 adapter = i2c_acpi_find_adapter_by_handle(adapter_handle);
384 if (!adapter)
385 break;
387 i2c_acpi_register_device(adapter, adev, &info);
388 break;
389 case ACPI_RECONFIG_DEVICE_REMOVE:
390 if (!acpi_device_enumerated(adev))
391 break;
393 client = i2c_acpi_find_client_by_adev(adev);
394 if (!client)
395 break;
397 i2c_unregister_device(client);
398 put_device(&client->dev);
399 break;
402 return NOTIFY_OK;
405 struct notifier_block i2c_acpi_notifier = {
406 .notifier_call = i2c_acpi_notify,
410 * i2c_acpi_new_device - Create i2c-client for the Nth I2cSerialBus resource
411 * @dev: Device owning the ACPI resources to get the client from
412 * @index: Index of ACPI resource to get
413 * @info: describes the I2C device; note this is modified (addr gets set)
414 * Context: can sleep
416 * By default the i2c subsys creates an i2c-client for the first I2cSerialBus
417 * resource of an acpi_device, but some acpi_devices have multiple I2cSerialBus
418 * resources, in that case this function can be used to create an i2c-client
419 * for other I2cSerialBus resources in the Current Resource Settings table.
421 * Also see i2c_new_device, which this function calls to create the i2c-client.
423 * Returns a pointer to the new i2c-client, or NULL if the adapter is not found.
425 struct i2c_client *i2c_acpi_new_device(struct device *dev, int index,
426 struct i2c_board_info *info)
428 struct i2c_acpi_lookup lookup;
429 struct i2c_adapter *adapter;
430 struct acpi_device *adev;
431 LIST_HEAD(resource_list);
432 int ret;
434 adev = ACPI_COMPANION(dev);
435 if (!adev)
436 return NULL;
438 memset(&lookup, 0, sizeof(lookup));
439 lookup.info = info;
440 lookup.device_handle = acpi_device_handle(adev);
441 lookup.index = index;
443 ret = acpi_dev_get_resources(adev, &resource_list,
444 i2c_acpi_fill_info, &lookup);
445 acpi_dev_free_resource_list(&resource_list);
447 if (ret < 0 || !info->addr)
448 return NULL;
450 adapter = i2c_acpi_find_adapter_by_handle(lookup.adapter_handle);
451 if (!adapter)
452 return NULL;
454 return i2c_new_device(adapter, info);
456 EXPORT_SYMBOL_GPL(i2c_acpi_new_device);
458 #ifdef CONFIG_ACPI_I2C_OPREGION
459 static int acpi_gsb_i2c_read_bytes(struct i2c_client *client,
460 u8 cmd, u8 *data, u8 data_len)
463 struct i2c_msg msgs[2];
464 int ret;
465 u8 *buffer;
467 buffer = kzalloc(data_len, GFP_KERNEL);
468 if (!buffer)
469 return AE_NO_MEMORY;
471 msgs[0].addr = client->addr;
472 msgs[0].flags = client->flags;
473 msgs[0].len = 1;
474 msgs[0].buf = &cmd;
476 msgs[1].addr = client->addr;
477 msgs[1].flags = client->flags | I2C_M_RD;
478 msgs[1].len = data_len;
479 msgs[1].buf = buffer;
481 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
482 if (ret < 0) {
483 /* Getting a NACK is unfortunately normal with some DSTDs */
484 if (ret == -EREMOTEIO)
485 dev_dbg(&client->adapter->dev, "i2c read %d bytes from client@%#x starting at reg %#x failed, error: %d\n",
486 data_len, client->addr, cmd, ret);
487 else
488 dev_err(&client->adapter->dev, "i2c read %d bytes from client@%#x starting at reg %#x failed, error: %d\n",
489 data_len, client->addr, cmd, ret);
490 /* 2 transfers must have completed successfully */
491 } else if (ret == 2) {
492 memcpy(data, buffer, data_len);
493 ret = 0;
494 } else {
495 ret = -EIO;
498 kfree(buffer);
499 return ret;
502 static int acpi_gsb_i2c_write_bytes(struct i2c_client *client,
503 u8 cmd, u8 *data, u8 data_len)
506 struct i2c_msg msgs[1];
507 u8 *buffer;
508 int ret = AE_OK;
510 buffer = kzalloc(data_len + 1, GFP_KERNEL);
511 if (!buffer)
512 return AE_NO_MEMORY;
514 buffer[0] = cmd;
515 memcpy(buffer + 1, data, data_len);
517 msgs[0].addr = client->addr;
518 msgs[0].flags = client->flags;
519 msgs[0].len = data_len + 1;
520 msgs[0].buf = buffer;
522 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
524 kfree(buffer);
526 if (ret < 0) {
527 dev_err(&client->adapter->dev, "i2c write failed: %d\n", ret);
528 return ret;
531 /* 1 transfer must have completed successfully */
532 return (ret == 1) ? 0 : -EIO;
535 static acpi_status
536 i2c_acpi_space_handler(u32 function, acpi_physical_address command,
537 u32 bits, u64 *value64,
538 void *handler_context, void *region_context)
540 struct gsb_buffer *gsb = (struct gsb_buffer *)value64;
541 struct i2c_acpi_handler_data *data = handler_context;
542 struct acpi_connection_info *info = &data->info;
543 struct acpi_resource_i2c_serialbus *sb;
544 struct i2c_adapter *adapter = data->adapter;
545 struct i2c_client *client;
546 struct acpi_resource *ares;
547 u32 accessor_type = function >> 16;
548 u8 action = function & ACPI_IO_MASK;
549 acpi_status ret;
550 int status;
552 ret = acpi_buffer_to_resource(info->connection, info->length, &ares);
553 if (ACPI_FAILURE(ret))
554 return ret;
556 client = kzalloc(sizeof(*client), GFP_KERNEL);
557 if (!client) {
558 ret = AE_NO_MEMORY;
559 goto err;
562 if (!value64 || ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS) {
563 ret = AE_BAD_PARAMETER;
564 goto err;
567 sb = &ares->data.i2c_serial_bus;
568 if (sb->type != ACPI_RESOURCE_SERIAL_TYPE_I2C) {
569 ret = AE_BAD_PARAMETER;
570 goto err;
573 client->adapter = adapter;
574 client->addr = sb->slave_address;
576 if (sb->access_mode == ACPI_I2C_10BIT_MODE)
577 client->flags |= I2C_CLIENT_TEN;
579 switch (accessor_type) {
580 case ACPI_GSB_ACCESS_ATTRIB_SEND_RCV:
581 if (action == ACPI_READ) {
582 status = i2c_smbus_read_byte(client);
583 if (status >= 0) {
584 gsb->bdata = status;
585 status = 0;
587 } else {
588 status = i2c_smbus_write_byte(client, gsb->bdata);
590 break;
592 case ACPI_GSB_ACCESS_ATTRIB_BYTE:
593 if (action == ACPI_READ) {
594 status = i2c_smbus_read_byte_data(client, command);
595 if (status >= 0) {
596 gsb->bdata = status;
597 status = 0;
599 } else {
600 status = i2c_smbus_write_byte_data(client, command,
601 gsb->bdata);
603 break;
605 case ACPI_GSB_ACCESS_ATTRIB_WORD:
606 if (action == ACPI_READ) {
607 status = i2c_smbus_read_word_data(client, command);
608 if (status >= 0) {
609 gsb->wdata = status;
610 status = 0;
612 } else {
613 status = i2c_smbus_write_word_data(client, command,
614 gsb->wdata);
616 break;
618 case ACPI_GSB_ACCESS_ATTRIB_BLOCK:
619 if (action == ACPI_READ) {
620 status = i2c_smbus_read_block_data(client, command,
621 gsb->data);
622 if (status >= 0) {
623 gsb->len = status;
624 status = 0;
626 } else {
627 status = i2c_smbus_write_block_data(client, command,
628 gsb->len, gsb->data);
630 break;
632 case ACPI_GSB_ACCESS_ATTRIB_MULTIBYTE:
633 if (action == ACPI_READ) {
634 status = acpi_gsb_i2c_read_bytes(client, command,
635 gsb->data, info->access_length);
636 } else {
637 status = acpi_gsb_i2c_write_bytes(client, command,
638 gsb->data, info->access_length);
640 break;
642 default:
643 dev_warn(&adapter->dev, "protocol 0x%02x not supported for client 0x%02x\n",
644 accessor_type, client->addr);
645 ret = AE_BAD_PARAMETER;
646 goto err;
649 gsb->status = status;
651 err:
652 kfree(client);
653 ACPI_FREE(ares);
654 return ret;
658 int i2c_acpi_install_space_handler(struct i2c_adapter *adapter)
660 acpi_handle handle;
661 struct i2c_acpi_handler_data *data;
662 acpi_status status;
664 if (!adapter->dev.parent)
665 return -ENODEV;
667 handle = ACPI_HANDLE(adapter->dev.parent);
669 if (!handle)
670 return -ENODEV;
672 data = kzalloc(sizeof(struct i2c_acpi_handler_data),
673 GFP_KERNEL);
674 if (!data)
675 return -ENOMEM;
677 data->adapter = adapter;
678 status = acpi_bus_attach_private_data(handle, (void *)data);
679 if (ACPI_FAILURE(status)) {
680 kfree(data);
681 return -ENOMEM;
684 status = acpi_install_address_space_handler(handle,
685 ACPI_ADR_SPACE_GSBUS,
686 &i2c_acpi_space_handler,
687 NULL,
688 data);
689 if (ACPI_FAILURE(status)) {
690 dev_err(&adapter->dev, "Error installing i2c space handler\n");
691 acpi_bus_detach_private_data(handle);
692 kfree(data);
693 return -ENOMEM;
696 acpi_walk_dep_device_list(handle);
697 return 0;
700 void i2c_acpi_remove_space_handler(struct i2c_adapter *adapter)
702 acpi_handle handle;
703 struct i2c_acpi_handler_data *data;
704 acpi_status status;
706 if (!adapter->dev.parent)
707 return;
709 handle = ACPI_HANDLE(adapter->dev.parent);
711 if (!handle)
712 return;
714 acpi_remove_address_space_handler(handle,
715 ACPI_ADR_SPACE_GSBUS,
716 &i2c_acpi_space_handler);
718 status = acpi_bus_get_private_data(handle, (void **)&data);
719 if (ACPI_SUCCESS(status))
720 kfree(data);
722 acpi_bus_detach_private_data(handle);
724 #endif /* CONFIG_ACPI_I2C_OPREGION */