i2c: designware: enable SLAVE in platform module
[linux-block.git] / drivers / i2c / i2c-core-acpi.c
CommitLineData
53f8f7c5
WS
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
22struct i2c_acpi_handler_data {
23 struct acpi_connection_info info;
24 struct i2c_adapter *adapter;
25};
26
27struct gsb_buffer {
28 u8 status;
29 u8 len;
30 union {
31 u16 wdata;
32 u8 bdata;
33 u8 data[0];
34 };
35} __packed;
36
37struct 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
48static 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
79static int i2c_acpi_do_lookup(struct acpi_device *adev,
80 struct i2c_acpi_lookup *lookup)
81{
82 struct i2c_board_info *info = lookup->info;
83 struct list_head resource_list;
84 int ret;
85
86 if (acpi_bus_get_status(adev) || !adev->status.present ||
87 acpi_device_enumerated(adev))
88 return -EINVAL;
89
90 memset(info, 0, sizeof(*info));
91 lookup->device_handle = acpi_device_handle(adev);
92
93 /* Look up for I2cSerialBus resource */
94 INIT_LIST_HEAD(&resource_list);
95 ret = acpi_dev_get_resources(adev, &resource_list,
96 i2c_acpi_fill_info, lookup);
97 acpi_dev_free_resource_list(&resource_list);
98
99 if (ret < 0 || !info->addr)
100 return -EINVAL;
101
102 return 0;
103}
104
105static int i2c_acpi_get_info(struct acpi_device *adev,
106 struct i2c_board_info *info,
107 struct i2c_adapter *adapter,
108 acpi_handle *adapter_handle)
109{
110 struct list_head resource_list;
111 struct resource_entry *entry;
112 struct i2c_acpi_lookup lookup;
113 int ret;
114
115 memset(&lookup, 0, sizeof(lookup));
116 lookup.info = info;
117 lookup.index = -1;
118
119 ret = i2c_acpi_do_lookup(adev, &lookup);
120 if (ret)
121 return ret;
122
123 if (adapter) {
124 /* The adapter must match the one in I2cSerialBus() connector */
125 if (ACPI_HANDLE(&adapter->dev) != lookup.adapter_handle)
126 return -ENODEV;
127 } else {
128 struct acpi_device *adapter_adev;
129
130 /* The adapter must be present */
131 if (acpi_bus_get_device(lookup.adapter_handle, &adapter_adev))
132 return -ENODEV;
133 if (acpi_bus_get_status(adapter_adev) ||
134 !adapter_adev->status.present)
135 return -ENODEV;
136 }
137
138 info->fwnode = acpi_fwnode_handle(adev);
139 if (adapter_handle)
140 *adapter_handle = lookup.adapter_handle;
141
142 /* Then fill IRQ number if any */
143 INIT_LIST_HEAD(&resource_list);
144 ret = acpi_dev_get_resources(adev, &resource_list, NULL, NULL);
145 if (ret < 0)
146 return -EINVAL;
147
148 resource_list_for_each_entry(entry, &resource_list) {
149 if (resource_type(entry->res) == IORESOURCE_IRQ) {
150 info->irq = entry->res->start;
151 break;
152 }
153 }
154
155 acpi_dev_free_resource_list(&resource_list);
156
157 acpi_set_modalias(adev, dev_name(&adev->dev), info->type,
158 sizeof(info->type));
159
160 return 0;
161}
162
163static void i2c_acpi_register_device(struct i2c_adapter *adapter,
164 struct acpi_device *adev,
165 struct i2c_board_info *info)
166{
167 adev->power.flags.ignore_parent = true;
168 acpi_device_set_enumerated(adev);
169
170 if (!i2c_new_device(adapter, info)) {
171 adev->power.flags.ignore_parent = false;
172 dev_err(&adapter->dev,
173 "failed to add I2C device %s from ACPI\n",
174 dev_name(&adev->dev));
175 }
176}
177
178static acpi_status i2c_acpi_add_device(acpi_handle handle, u32 level,
179 void *data, void **return_value)
180{
181 struct i2c_adapter *adapter = data;
182 struct acpi_device *adev;
183 struct i2c_board_info info;
184
185 if (acpi_bus_get_device(handle, &adev))
186 return AE_OK;
187
188 if (i2c_acpi_get_info(adev, &info, adapter, NULL))
189 return AE_OK;
190
191 i2c_acpi_register_device(adapter, adev, &info);
192
193 return AE_OK;
194}
195
196#define I2C_ACPI_MAX_SCAN_DEPTH 32
197
198/**
199 * i2c_acpi_register_devices - enumerate I2C slave devices behind adapter
200 * @adap: pointer to adapter
201 *
202 * Enumerate all I2C slave devices behind this adapter by walking the ACPI
203 * namespace. When a device is found it will be added to the Linux device
204 * model and bound to the corresponding ACPI handle.
205 */
206void i2c_acpi_register_devices(struct i2c_adapter *adap)
207{
208 acpi_status status;
209
210 if (!has_acpi_companion(&adap->dev))
211 return;
212
213 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
214 I2C_ACPI_MAX_SCAN_DEPTH,
215 i2c_acpi_add_device, NULL,
216 adap, NULL);
217 if (ACPI_FAILURE(status))
218 dev_warn(&adap->dev, "failed to enumerate I2C slaves\n");
219}
220
221static acpi_status i2c_acpi_lookup_speed(acpi_handle handle, u32 level,
222 void *data, void **return_value)
223{
224 struct i2c_acpi_lookup *lookup = data;
225 struct acpi_device *adev;
226
227 if (acpi_bus_get_device(handle, &adev))
228 return AE_OK;
229
230 if (i2c_acpi_do_lookup(adev, lookup))
231 return AE_OK;
232
233 if (lookup->search_handle != lookup->adapter_handle)
234 return AE_OK;
235
236 if (lookup->speed <= lookup->min_speed)
237 lookup->min_speed = lookup->speed;
238
239 return AE_OK;
240}
241
242/**
243 * i2c_acpi_find_bus_speed - find I2C bus speed from ACPI
244 * @dev: The device owning the bus
245 *
246 * Find the I2C bus speed by walking the ACPI namespace for all I2C slaves
247 * devices connected to this bus and use the speed of slowest device.
248 *
249 * Returns the speed in Hz or zero
250 */
251u32 i2c_acpi_find_bus_speed(struct device *dev)
252{
253 struct i2c_acpi_lookup lookup;
254 struct i2c_board_info dummy;
255 acpi_status status;
256
257 if (!has_acpi_companion(dev))
258 return 0;
259
260 memset(&lookup, 0, sizeof(lookup));
261 lookup.search_handle = ACPI_HANDLE(dev);
262 lookup.min_speed = UINT_MAX;
263 lookup.info = &dummy;
264 lookup.index = -1;
265
266 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
267 I2C_ACPI_MAX_SCAN_DEPTH,
268 i2c_acpi_lookup_speed, NULL,
269 &lookup, NULL);
270
271 if (ACPI_FAILURE(status)) {
272 dev_warn(dev, "unable to find I2C bus speed from ACPI\n");
273 return 0;
274 }
275
276 return lookup.min_speed != UINT_MAX ? lookup.min_speed : 0;
277}
278EXPORT_SYMBOL_GPL(i2c_acpi_find_bus_speed);
279
280static int i2c_acpi_match_adapter(struct device *dev, void *data)
281{
282 struct i2c_adapter *adapter = i2c_verify_adapter(dev);
283
284 if (!adapter)
285 return 0;
286
287 return ACPI_HANDLE(dev) == (acpi_handle)data;
288}
289
290static int i2c_acpi_match_device(struct device *dev, void *data)
291{
292 return ACPI_COMPANION(dev) == data;
293}
294
295static struct i2c_adapter *i2c_acpi_find_adapter_by_handle(acpi_handle handle)
296{
297 struct device *dev;
298
299 dev = bus_find_device(&i2c_bus_type, NULL, handle,
300 i2c_acpi_match_adapter);
301 return dev ? i2c_verify_adapter(dev) : NULL;
302}
303
304static struct i2c_client *i2c_acpi_find_client_by_adev(struct acpi_device *adev)
305{
306 struct device *dev;
307
308 dev = bus_find_device(&i2c_bus_type, NULL, adev, i2c_acpi_match_device);
309 return dev ? i2c_verify_client(dev) : NULL;
310}
311
312static int i2c_acpi_notify(struct notifier_block *nb, unsigned long value,
313 void *arg)
314{
315 struct acpi_device *adev = arg;
316 struct i2c_board_info info;
317 acpi_handle adapter_handle;
318 struct i2c_adapter *adapter;
319 struct i2c_client *client;
320
321 switch (value) {
322 case ACPI_RECONFIG_DEVICE_ADD:
323 if (i2c_acpi_get_info(adev, &info, NULL, &adapter_handle))
324 break;
325
326 adapter = i2c_acpi_find_adapter_by_handle(adapter_handle);
327 if (!adapter)
328 break;
329
330 i2c_acpi_register_device(adapter, adev, &info);
331 break;
332 case ACPI_RECONFIG_DEVICE_REMOVE:
333 if (!acpi_device_enumerated(adev))
334 break;
335
336 client = i2c_acpi_find_client_by_adev(adev);
337 if (!client)
338 break;
339
340 i2c_unregister_device(client);
341 put_device(&client->dev);
342 break;
343 }
344
345 return NOTIFY_OK;
346}
347
348struct notifier_block i2c_acpi_notifier = {
349 .notifier_call = i2c_acpi_notify,
350};
351
352/**
353 * i2c_acpi_new_device - Create i2c-client for the Nth I2cSerialBus resource
354 * @dev: Device owning the ACPI resources to get the client from
355 * @index: Index of ACPI resource to get
356 * @info: describes the I2C device; note this is modified (addr gets set)
357 * Context: can sleep
358 *
359 * By default the i2c subsys creates an i2c-client for the first I2cSerialBus
360 * resource of an acpi_device, but some acpi_devices have multiple I2cSerialBus
361 * resources, in that case this function can be used to create an i2c-client
362 * for other I2cSerialBus resources in the Current Resource Settings table.
363 *
364 * Also see i2c_new_device, which this function calls to create the i2c-client.
365 *
366 * Returns a pointer to the new i2c-client, or NULL if the adapter is not found.
367 */
368struct i2c_client *i2c_acpi_new_device(struct device *dev, int index,
369 struct i2c_board_info *info)
370{
371 struct i2c_acpi_lookup lookup;
372 struct i2c_adapter *adapter;
373 struct acpi_device *adev;
374 LIST_HEAD(resource_list);
375 int ret;
376
377 adev = ACPI_COMPANION(dev);
378 if (!adev)
379 return NULL;
380
381 memset(&lookup, 0, sizeof(lookup));
382 lookup.info = info;
383 lookup.device_handle = acpi_device_handle(adev);
384 lookup.index = index;
385
386 ret = acpi_dev_get_resources(adev, &resource_list,
387 i2c_acpi_fill_info, &lookup);
388 acpi_dev_free_resource_list(&resource_list);
389
390 if (ret < 0 || !info->addr)
391 return NULL;
392
393 adapter = i2c_acpi_find_adapter_by_handle(lookup.adapter_handle);
394 if (!adapter)
395 return NULL;
396
397 return i2c_new_device(adapter, info);
398}
399EXPORT_SYMBOL_GPL(i2c_acpi_new_device);
400
401#ifdef CONFIG_ACPI_I2C_OPREGION
402static int acpi_gsb_i2c_read_bytes(struct i2c_client *client,
403 u8 cmd, u8 *data, u8 data_len)
404{
405
406 struct i2c_msg msgs[2];
407 int ret;
408 u8 *buffer;
409
410 buffer = kzalloc(data_len, GFP_KERNEL);
411 if (!buffer)
412 return AE_NO_MEMORY;
413
414 msgs[0].addr = client->addr;
415 msgs[0].flags = client->flags;
416 msgs[0].len = 1;
417 msgs[0].buf = &cmd;
418
419 msgs[1].addr = client->addr;
420 msgs[1].flags = client->flags | I2C_M_RD;
421 msgs[1].len = data_len;
422 msgs[1].buf = buffer;
423
424 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
425 if (ret < 0)
426 dev_err(&client->adapter->dev, "i2c read failed\n");
427 else
428 memcpy(data, buffer, data_len);
429
430 kfree(buffer);
431 return ret;
432}
433
434static int acpi_gsb_i2c_write_bytes(struct i2c_client *client,
435 u8 cmd, u8 *data, u8 data_len)
436{
437
438 struct i2c_msg msgs[1];
439 u8 *buffer;
440 int ret = AE_OK;
441
442 buffer = kzalloc(data_len + 1, GFP_KERNEL);
443 if (!buffer)
444 return AE_NO_MEMORY;
445
446 buffer[0] = cmd;
447 memcpy(buffer + 1, data, data_len);
448
449 msgs[0].addr = client->addr;
450 msgs[0].flags = client->flags;
451 msgs[0].len = data_len + 1;
452 msgs[0].buf = buffer;
453
454 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
455 if (ret < 0)
456 dev_err(&client->adapter->dev, "i2c write failed\n");
457
458 kfree(buffer);
459 return ret;
460}
461
462static acpi_status
463i2c_acpi_space_handler(u32 function, acpi_physical_address command,
464 u32 bits, u64 *value64,
465 void *handler_context, void *region_context)
466{
467 struct gsb_buffer *gsb = (struct gsb_buffer *)value64;
468 struct i2c_acpi_handler_data *data = handler_context;
469 struct acpi_connection_info *info = &data->info;
470 struct acpi_resource_i2c_serialbus *sb;
471 struct i2c_adapter *adapter = data->adapter;
472 struct i2c_client *client;
473 struct acpi_resource *ares;
474 u32 accessor_type = function >> 16;
475 u8 action = function & ACPI_IO_MASK;
476 acpi_status ret;
477 int status;
478
479 ret = acpi_buffer_to_resource(info->connection, info->length, &ares);
480 if (ACPI_FAILURE(ret))
481 return ret;
482
483 client = kzalloc(sizeof(*client), GFP_KERNEL);
484 if (!client) {
485 ret = AE_NO_MEMORY;
486 goto err;
487 }
488
489 if (!value64 || ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS) {
490 ret = AE_BAD_PARAMETER;
491 goto err;
492 }
493
494 sb = &ares->data.i2c_serial_bus;
495 if (sb->type != ACPI_RESOURCE_SERIAL_TYPE_I2C) {
496 ret = AE_BAD_PARAMETER;
497 goto err;
498 }
499
500 client->adapter = adapter;
501 client->addr = sb->slave_address;
502
503 if (sb->access_mode == ACPI_I2C_10BIT_MODE)
504 client->flags |= I2C_CLIENT_TEN;
505
506 switch (accessor_type) {
507 case ACPI_GSB_ACCESS_ATTRIB_SEND_RCV:
508 if (action == ACPI_READ) {
509 status = i2c_smbus_read_byte(client);
510 if (status >= 0) {
511 gsb->bdata = status;
512 status = 0;
513 }
514 } else {
515 status = i2c_smbus_write_byte(client, gsb->bdata);
516 }
517 break;
518
519 case ACPI_GSB_ACCESS_ATTRIB_BYTE:
520 if (action == ACPI_READ) {
521 status = i2c_smbus_read_byte_data(client, command);
522 if (status >= 0) {
523 gsb->bdata = status;
524 status = 0;
525 }
526 } else {
527 status = i2c_smbus_write_byte_data(client, command,
528 gsb->bdata);
529 }
530 break;
531
532 case ACPI_GSB_ACCESS_ATTRIB_WORD:
533 if (action == ACPI_READ) {
534 status = i2c_smbus_read_word_data(client, command);
535 if (status >= 0) {
536 gsb->wdata = status;
537 status = 0;
538 }
539 } else {
540 status = i2c_smbus_write_word_data(client, command,
541 gsb->wdata);
542 }
543 break;
544
545 case ACPI_GSB_ACCESS_ATTRIB_BLOCK:
546 if (action == ACPI_READ) {
547 status = i2c_smbus_read_block_data(client, command,
548 gsb->data);
549 if (status >= 0) {
550 gsb->len = status;
551 status = 0;
552 }
553 } else {
554 status = i2c_smbus_write_block_data(client, command,
555 gsb->len, gsb->data);
556 }
557 break;
558
559 case ACPI_GSB_ACCESS_ATTRIB_MULTIBYTE:
560 if (action == ACPI_READ) {
561 status = acpi_gsb_i2c_read_bytes(client, command,
562 gsb->data, info->access_length);
563 if (status > 0)
564 status = 0;
565 } else {
566 status = acpi_gsb_i2c_write_bytes(client, command,
567 gsb->data, info->access_length);
568 }
569 break;
570
571 default:
572 dev_warn(&adapter->dev, "protocol 0x%02x not supported for client 0x%02x\n",
573 accessor_type, client->addr);
574 ret = AE_BAD_PARAMETER;
575 goto err;
576 }
577
578 gsb->status = status;
579
580 err:
581 kfree(client);
582 ACPI_FREE(ares);
583 return ret;
584}
585
586
587int i2c_acpi_install_space_handler(struct i2c_adapter *adapter)
588{
589 acpi_handle handle;
590 struct i2c_acpi_handler_data *data;
591 acpi_status status;
592
593 if (!adapter->dev.parent)
594 return -ENODEV;
595
596 handle = ACPI_HANDLE(adapter->dev.parent);
597
598 if (!handle)
599 return -ENODEV;
600
601 data = kzalloc(sizeof(struct i2c_acpi_handler_data),
602 GFP_KERNEL);
603 if (!data)
604 return -ENOMEM;
605
606 data->adapter = adapter;
607 status = acpi_bus_attach_private_data(handle, (void *)data);
608 if (ACPI_FAILURE(status)) {
609 kfree(data);
610 return -ENOMEM;
611 }
612
613 status = acpi_install_address_space_handler(handle,
614 ACPI_ADR_SPACE_GSBUS,
615 &i2c_acpi_space_handler,
616 NULL,
617 data);
618 if (ACPI_FAILURE(status)) {
619 dev_err(&adapter->dev, "Error installing i2c space handler\n");
620 acpi_bus_detach_private_data(handle);
621 kfree(data);
622 return -ENOMEM;
623 }
624
625 acpi_walk_dep_device_list(handle);
626 return 0;
627}
628
629void i2c_acpi_remove_space_handler(struct i2c_adapter *adapter)
630{
631 acpi_handle handle;
632 struct i2c_acpi_handler_data *data;
633 acpi_status status;
634
635 if (!adapter->dev.parent)
636 return;
637
638 handle = ACPI_HANDLE(adapter->dev.parent);
639
640 if (!handle)
641 return;
642
643 acpi_remove_address_space_handler(handle,
644 ACPI_ADR_SPACE_GSBUS,
645 &i2c_acpi_space_handler);
646
647 status = acpi_bus_get_private_data(handle, (void **)&data);
648 if (ACPI_SUCCESS(status))
649 kfree(data);
650
651 acpi_bus_detach_private_data(handle);
652}
653#endif /* CONFIG_ACPI_I2C_OPREGION */