]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - drivers/i2c/i2c-core-acpi.c
UBUNTU: Ubuntu-4.15.0-96.97
[mirror_ubuntu-bionic-kernel.git] / 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 u32 force_speed;
47 };
48
49 static int i2c_acpi_fill_info(struct acpi_resource *ares, void *data)
50 {
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;
55
56 if (info->addr || ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS)
57 return 1;
58
59 sb = &ares->data.i2c_serial_bus;
60 if (sb->type != ACPI_RESOURCE_SERIAL_TYPE_I2C)
61 return 1;
62
63 if (lookup->index != -1 && lookup->n++ != lookup->index)
64 return 1;
65
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;
71
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;
76
77 return 1;
78 }
79
80 static const struct acpi_device_id i2c_acpi_ignored_device_ids[] = {
81 /*
82 * ACPI video acpi_devices, which are handled by the acpi-video driver
83 * sometimes contain a SERIAL_TYPE_I2C ACPI resource, ignore these.
84 */
85 { ACPI_VIDEO_HID, 0 },
86 {}
87 };
88
89 static int i2c_acpi_do_lookup(struct acpi_device *adev,
90 struct i2c_acpi_lookup *lookup)
91 {
92 struct i2c_board_info *info = lookup->info;
93 struct list_head resource_list;
94 int ret;
95
96 if (acpi_bus_get_status(adev) || !adev->status.present ||
97 acpi_device_enumerated(adev))
98 return -EINVAL;
99
100 if (acpi_match_device_ids(adev, i2c_acpi_ignored_device_ids) == 0)
101 return -ENODEV;
102
103 memset(info, 0, sizeof(*info));
104 lookup->device_handle = acpi_device_handle(adev);
105
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);
111
112 if (ret < 0 || !info->addr)
113 return -EINVAL;
114
115 return 0;
116 }
117
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)
122 {
123 struct list_head resource_list;
124 struct resource_entry *entry;
125 struct i2c_acpi_lookup lookup;
126 int ret;
127
128 memset(&lookup, 0, sizeof(lookup));
129 lookup.info = info;
130 lookup.index = -1;
131
132 ret = i2c_acpi_do_lookup(adev, &lookup);
133 if (ret)
134 return ret;
135
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;
142
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;
149 }
150
151 info->fwnode = acpi_fwnode_handle(adev);
152 if (adapter_handle)
153 *adapter_handle = lookup.adapter_handle;
154
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;
160
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;
165 }
166 }
167
168 acpi_dev_free_resource_list(&resource_list);
169
170 acpi_set_modalias(adev, dev_name(&adev->dev), info->type,
171 sizeof(info->type));
172
173 return 0;
174 }
175
176 static void i2c_acpi_register_device(struct i2c_adapter *adapter,
177 struct acpi_device *adev,
178 struct i2c_board_info *info)
179 {
180 adev->power.flags.ignore_parent = true;
181 acpi_device_set_enumerated(adev);
182
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));
188 }
189 }
190
191 static acpi_status i2c_acpi_add_device(acpi_handle handle, u32 level,
192 void *data, void **return_value)
193 {
194 struct i2c_adapter *adapter = data;
195 struct acpi_device *adev;
196 struct i2c_board_info info;
197
198 if (acpi_bus_get_device(handle, &adev))
199 return AE_OK;
200
201 if (i2c_acpi_get_info(adev, &info, adapter, NULL))
202 return AE_OK;
203
204 i2c_acpi_register_device(adapter, adev, &info);
205
206 return AE_OK;
207 }
208
209 #define I2C_ACPI_MAX_SCAN_DEPTH 32
210
211 /**
212 * i2c_acpi_register_devices - enumerate I2C slave devices behind adapter
213 * @adap: pointer to adapter
214 *
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.
218 */
219 void i2c_acpi_register_devices(struct i2c_adapter *adap)
220 {
221 acpi_status status;
222
223 if (!has_acpi_companion(&adap->dev))
224 return;
225
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");
232 }
233
234 const struct acpi_device_id *
235 i2c_acpi_match_device(const struct acpi_device_id *matches,
236 struct i2c_client *client)
237 {
238 if (!(client && matches))
239 return NULL;
240
241 return acpi_match_device(matches, &client->dev);
242 }
243
244 static const struct acpi_device_id i2c_acpi_force_400khz_device_ids[] = {
245 /*
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.
252 */
253 { "MSSL1680", 0 },
254 {}
255 };
256
257 static acpi_status i2c_acpi_lookup_speed(acpi_handle handle, u32 level,
258 void *data, void **return_value)
259 {
260 struct i2c_acpi_lookup *lookup = data;
261 struct acpi_device *adev;
262
263 if (acpi_bus_get_device(handle, &adev))
264 return AE_OK;
265
266 if (i2c_acpi_do_lookup(adev, lookup))
267 return AE_OK;
268
269 if (lookup->search_handle != lookup->adapter_handle)
270 return AE_OK;
271
272 if (lookup->speed <= lookup->min_speed)
273 lookup->min_speed = lookup->speed;
274
275 if (acpi_match_device_ids(adev, i2c_acpi_force_400khz_device_ids) == 0)
276 lookup->force_speed = 400000;
277
278 return AE_OK;
279 }
280
281 /**
282 * i2c_acpi_find_bus_speed - find I2C bus speed from ACPI
283 * @dev: The device owning the bus
284 *
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.
287 *
288 * Returns the speed in Hz or zero
289 */
290 u32 i2c_acpi_find_bus_speed(struct device *dev)
291 {
292 struct i2c_acpi_lookup lookup;
293 struct i2c_board_info dummy;
294 acpi_status status;
295
296 if (!has_acpi_companion(dev))
297 return 0;
298
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;
304
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);
309
310 if (ACPI_FAILURE(status)) {
311 dev_warn(dev, "unable to find I2C bus speed from ACPI\n");
312 return 0;
313 }
314
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;
324 }
325 }
326 EXPORT_SYMBOL_GPL(i2c_acpi_find_bus_speed);
327
328 static int i2c_acpi_find_match_adapter(struct device *dev, void *data)
329 {
330 struct i2c_adapter *adapter = i2c_verify_adapter(dev);
331
332 if (!adapter)
333 return 0;
334
335 return ACPI_HANDLE(dev) == (acpi_handle)data;
336 }
337
338 static int i2c_acpi_find_match_device(struct device *dev, void *data)
339 {
340 return ACPI_COMPANION(dev) == data;
341 }
342
343 static struct i2c_adapter *i2c_acpi_find_adapter_by_handle(acpi_handle handle)
344 {
345 struct device *dev;
346
347 dev = bus_find_device(&i2c_bus_type, NULL, handle,
348 i2c_acpi_find_match_adapter);
349 return dev ? i2c_verify_adapter(dev) : NULL;
350 }
351
352 static struct i2c_client *i2c_acpi_find_client_by_adev(struct acpi_device *adev)
353 {
354 struct device *dev;
355
356 dev = bus_find_device(&i2c_bus_type, NULL, adev,
357 i2c_acpi_find_match_device);
358 return dev ? i2c_verify_client(dev) : NULL;
359 }
360
361 static int i2c_acpi_notify(struct notifier_block *nb, unsigned long value,
362 void *arg)
363 {
364 struct acpi_device *adev = arg;
365 struct i2c_board_info info;
366 acpi_handle adapter_handle;
367 struct i2c_adapter *adapter;
368 struct i2c_client *client;
369
370 switch (value) {
371 case ACPI_RECONFIG_DEVICE_ADD:
372 if (i2c_acpi_get_info(adev, &info, NULL, &adapter_handle))
373 break;
374
375 adapter = i2c_acpi_find_adapter_by_handle(adapter_handle);
376 if (!adapter)
377 break;
378
379 i2c_acpi_register_device(adapter, adev, &info);
380 break;
381 case ACPI_RECONFIG_DEVICE_REMOVE:
382 if (!acpi_device_enumerated(adev))
383 break;
384
385 client = i2c_acpi_find_client_by_adev(adev);
386 if (!client)
387 break;
388
389 i2c_unregister_device(client);
390 put_device(&client->dev);
391 break;
392 }
393
394 return NOTIFY_OK;
395 }
396
397 struct notifier_block i2c_acpi_notifier = {
398 .notifier_call = i2c_acpi_notify,
399 };
400
401 /**
402 * i2c_acpi_new_device - Create i2c-client for the Nth I2cSerialBus resource
403 * @dev: Device owning the ACPI resources to get the client from
404 * @index: Index of ACPI resource to get
405 * @info: describes the I2C device; note this is modified (addr gets set)
406 * Context: can sleep
407 *
408 * By default the i2c subsys creates an i2c-client for the first I2cSerialBus
409 * resource of an acpi_device, but some acpi_devices have multiple I2cSerialBus
410 * resources, in that case this function can be used to create an i2c-client
411 * for other I2cSerialBus resources in the Current Resource Settings table.
412 *
413 * Also see i2c_new_device, which this function calls to create the i2c-client.
414 *
415 * Returns a pointer to the new i2c-client, or NULL if the adapter is not found.
416 */
417 struct i2c_client *i2c_acpi_new_device(struct device *dev, int index,
418 struct i2c_board_info *info)
419 {
420 struct i2c_acpi_lookup lookup;
421 struct i2c_adapter *adapter;
422 struct acpi_device *adev;
423 LIST_HEAD(resource_list);
424 int ret;
425
426 adev = ACPI_COMPANION(dev);
427 if (!adev)
428 return NULL;
429
430 memset(&lookup, 0, sizeof(lookup));
431 lookup.info = info;
432 lookup.device_handle = acpi_device_handle(adev);
433 lookup.index = index;
434
435 ret = acpi_dev_get_resources(adev, &resource_list,
436 i2c_acpi_fill_info, &lookup);
437 acpi_dev_free_resource_list(&resource_list);
438
439 if (ret < 0 || !info->addr)
440 return NULL;
441
442 adapter = i2c_acpi_find_adapter_by_handle(lookup.adapter_handle);
443 if (!adapter)
444 return NULL;
445
446 return i2c_new_device(adapter, info);
447 }
448 EXPORT_SYMBOL_GPL(i2c_acpi_new_device);
449
450 #ifdef CONFIG_ACPI_I2C_OPREGION
451 static int acpi_gsb_i2c_read_bytes(struct i2c_client *client,
452 u8 cmd, u8 *data, u8 data_len)
453 {
454
455 struct i2c_msg msgs[2];
456 int ret;
457 u8 *buffer;
458
459 buffer = kzalloc(data_len, GFP_KERNEL);
460 if (!buffer)
461 return AE_NO_MEMORY;
462
463 msgs[0].addr = client->addr;
464 msgs[0].flags = client->flags;
465 msgs[0].len = 1;
466 msgs[0].buf = &cmd;
467
468 msgs[1].addr = client->addr;
469 msgs[1].flags = client->flags | I2C_M_RD;
470 msgs[1].len = data_len;
471 msgs[1].buf = buffer;
472
473 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
474 if (ret < 0)
475 dev_err(&client->adapter->dev, "i2c read failed\n");
476 else
477 memcpy(data, buffer, data_len);
478
479 kfree(buffer);
480 return ret;
481 }
482
483 static int acpi_gsb_i2c_write_bytes(struct i2c_client *client,
484 u8 cmd, u8 *data, u8 data_len)
485 {
486
487 struct i2c_msg msgs[1];
488 u8 *buffer;
489 int ret = AE_OK;
490
491 buffer = kzalloc(data_len + 1, GFP_KERNEL);
492 if (!buffer)
493 return AE_NO_MEMORY;
494
495 buffer[0] = cmd;
496 memcpy(buffer + 1, data, data_len);
497
498 msgs[0].addr = client->addr;
499 msgs[0].flags = client->flags;
500 msgs[0].len = data_len + 1;
501 msgs[0].buf = buffer;
502
503 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
504
505 kfree(buffer);
506
507 if (ret < 0) {
508 dev_err(&client->adapter->dev, "i2c write failed: %d\n", ret);
509 return ret;
510 }
511
512 /* 1 transfer must have completed successfully */
513 return (ret == 1) ? 0 : -EIO;
514 }
515
516 static acpi_status
517 i2c_acpi_space_handler(u32 function, acpi_physical_address command,
518 u32 bits, u64 *value64,
519 void *handler_context, void *region_context)
520 {
521 struct gsb_buffer *gsb = (struct gsb_buffer *)value64;
522 struct i2c_acpi_handler_data *data = handler_context;
523 struct acpi_connection_info *info = &data->info;
524 struct acpi_resource_i2c_serialbus *sb;
525 struct i2c_adapter *adapter = data->adapter;
526 struct i2c_client *client;
527 struct acpi_resource *ares;
528 u32 accessor_type = function >> 16;
529 u8 action = function & ACPI_IO_MASK;
530 acpi_status ret;
531 int status;
532
533 ret = acpi_buffer_to_resource(info->connection, info->length, &ares);
534 if (ACPI_FAILURE(ret))
535 return ret;
536
537 client = kzalloc(sizeof(*client), GFP_KERNEL);
538 if (!client) {
539 ret = AE_NO_MEMORY;
540 goto err;
541 }
542
543 if (!value64 || ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS) {
544 ret = AE_BAD_PARAMETER;
545 goto err;
546 }
547
548 sb = &ares->data.i2c_serial_bus;
549 if (sb->type != ACPI_RESOURCE_SERIAL_TYPE_I2C) {
550 ret = AE_BAD_PARAMETER;
551 goto err;
552 }
553
554 client->adapter = adapter;
555 client->addr = sb->slave_address;
556
557 if (sb->access_mode == ACPI_I2C_10BIT_MODE)
558 client->flags |= I2C_CLIENT_TEN;
559
560 switch (accessor_type) {
561 case ACPI_GSB_ACCESS_ATTRIB_SEND_RCV:
562 if (action == ACPI_READ) {
563 status = i2c_smbus_read_byte(client);
564 if (status >= 0) {
565 gsb->bdata = status;
566 status = 0;
567 }
568 } else {
569 status = i2c_smbus_write_byte(client, gsb->bdata);
570 }
571 break;
572
573 case ACPI_GSB_ACCESS_ATTRIB_BYTE:
574 if (action == ACPI_READ) {
575 status = i2c_smbus_read_byte_data(client, command);
576 if (status >= 0) {
577 gsb->bdata = status;
578 status = 0;
579 }
580 } else {
581 status = i2c_smbus_write_byte_data(client, command,
582 gsb->bdata);
583 }
584 break;
585
586 case ACPI_GSB_ACCESS_ATTRIB_WORD:
587 if (action == ACPI_READ) {
588 status = i2c_smbus_read_word_data(client, command);
589 if (status >= 0) {
590 gsb->wdata = status;
591 status = 0;
592 }
593 } else {
594 status = i2c_smbus_write_word_data(client, command,
595 gsb->wdata);
596 }
597 break;
598
599 case ACPI_GSB_ACCESS_ATTRIB_BLOCK:
600 if (action == ACPI_READ) {
601 status = i2c_smbus_read_block_data(client, command,
602 gsb->data);
603 if (status >= 0) {
604 gsb->len = status;
605 status = 0;
606 }
607 } else {
608 status = i2c_smbus_write_block_data(client, command,
609 gsb->len, gsb->data);
610 }
611 break;
612
613 case ACPI_GSB_ACCESS_ATTRIB_MULTIBYTE:
614 if (action == ACPI_READ) {
615 status = acpi_gsb_i2c_read_bytes(client, command,
616 gsb->data, info->access_length);
617 if (status > 0)
618 status = 0;
619 } else {
620 status = acpi_gsb_i2c_write_bytes(client, command,
621 gsb->data, info->access_length);
622 }
623 break;
624
625 default:
626 dev_warn(&adapter->dev, "protocol 0x%02x not supported for client 0x%02x\n",
627 accessor_type, client->addr);
628 ret = AE_BAD_PARAMETER;
629 goto err;
630 }
631
632 gsb->status = status;
633
634 err:
635 kfree(client);
636 ACPI_FREE(ares);
637 return ret;
638 }
639
640
641 int i2c_acpi_install_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 -ENODEV;
649
650 handle = ACPI_HANDLE(adapter->dev.parent);
651
652 if (!handle)
653 return -ENODEV;
654
655 data = kzalloc(sizeof(struct i2c_acpi_handler_data),
656 GFP_KERNEL);
657 if (!data)
658 return -ENOMEM;
659
660 data->adapter = adapter;
661 status = acpi_bus_attach_private_data(handle, (void *)data);
662 if (ACPI_FAILURE(status)) {
663 kfree(data);
664 return -ENOMEM;
665 }
666
667 status = acpi_install_address_space_handler(handle,
668 ACPI_ADR_SPACE_GSBUS,
669 &i2c_acpi_space_handler,
670 NULL,
671 data);
672 if (ACPI_FAILURE(status)) {
673 dev_err(&adapter->dev, "Error installing i2c space handler\n");
674 acpi_bus_detach_private_data(handle);
675 kfree(data);
676 return -ENOMEM;
677 }
678
679 acpi_walk_dep_device_list(handle);
680 return 0;
681 }
682
683 void i2c_acpi_remove_space_handler(struct i2c_adapter *adapter)
684 {
685 acpi_handle handle;
686 struct i2c_acpi_handler_data *data;
687 acpi_status status;
688
689 if (!adapter->dev.parent)
690 return;
691
692 handle = ACPI_HANDLE(adapter->dev.parent);
693
694 if (!handle)
695 return;
696
697 acpi_remove_address_space_handler(handle,
698 ACPI_ADR_SPACE_GSBUS,
699 &i2c_acpi_space_handler);
700
701 status = acpi_bus_get_private_data(handle, (void **)&data);
702 if (ACPI_SUCCESS(status))
703 kfree(data);
704
705 acpi_bus_detach_private_data(handle);
706 }
707 #endif /* CONFIG_ACPI_I2C_OPREGION */