xref: /openbmc/linux/drivers/i2c/i2c-core-acpi.c (revision 590b516e)
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 const struct acpi_device_id *
234 i2c_acpi_match_device(const struct acpi_device_id *matches,
235 		      struct i2c_client *client)
236 {
237 	if (!(client && matches))
238 		return NULL;
239 
240 	return acpi_match_device(matches, &client->dev);
241 }
242 
243 static acpi_status i2c_acpi_lookup_speed(acpi_handle handle, u32 level,
244 					   void *data, void **return_value)
245 {
246 	struct i2c_acpi_lookup *lookup = data;
247 	struct acpi_device *adev;
248 
249 	if (acpi_bus_get_device(handle, &adev))
250 		return AE_OK;
251 
252 	if (i2c_acpi_do_lookup(adev, lookup))
253 		return AE_OK;
254 
255 	if (lookup->search_handle != lookup->adapter_handle)
256 		return AE_OK;
257 
258 	if (lookup->speed <= lookup->min_speed)
259 		lookup->min_speed = lookup->speed;
260 
261 	return AE_OK;
262 }
263 
264 /**
265  * i2c_acpi_find_bus_speed - find I2C bus speed from ACPI
266  * @dev: The device owning the bus
267  *
268  * Find the I2C bus speed by walking the ACPI namespace for all I2C slaves
269  * devices connected to this bus and use the speed of slowest device.
270  *
271  * Returns the speed in Hz or zero
272  */
273 u32 i2c_acpi_find_bus_speed(struct device *dev)
274 {
275 	struct i2c_acpi_lookup lookup;
276 	struct i2c_board_info dummy;
277 	acpi_status status;
278 
279 	if (!has_acpi_companion(dev))
280 		return 0;
281 
282 	memset(&lookup, 0, sizeof(lookup));
283 	lookup.search_handle = ACPI_HANDLE(dev);
284 	lookup.min_speed = UINT_MAX;
285 	lookup.info = &dummy;
286 	lookup.index = -1;
287 
288 	status = acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
289 				     I2C_ACPI_MAX_SCAN_DEPTH,
290 				     i2c_acpi_lookup_speed, NULL,
291 				     &lookup, NULL);
292 
293 	if (ACPI_FAILURE(status)) {
294 		dev_warn(dev, "unable to find I2C bus speed from ACPI\n");
295 		return 0;
296 	}
297 
298 	return lookup.min_speed != UINT_MAX ? lookup.min_speed : 0;
299 }
300 EXPORT_SYMBOL_GPL(i2c_acpi_find_bus_speed);
301 
302 static int i2c_acpi_find_match_adapter(struct device *dev, void *data)
303 {
304 	struct i2c_adapter *adapter = i2c_verify_adapter(dev);
305 
306 	if (!adapter)
307 		return 0;
308 
309 	return ACPI_HANDLE(dev) == (acpi_handle)data;
310 }
311 
312 static int i2c_acpi_find_match_device(struct device *dev, void *data)
313 {
314 	return ACPI_COMPANION(dev) == data;
315 }
316 
317 static struct i2c_adapter *i2c_acpi_find_adapter_by_handle(acpi_handle handle)
318 {
319 	struct device *dev;
320 
321 	dev = bus_find_device(&i2c_bus_type, NULL, handle,
322 			      i2c_acpi_find_match_adapter);
323 	return dev ? i2c_verify_adapter(dev) : NULL;
324 }
325 
326 static struct i2c_client *i2c_acpi_find_client_by_adev(struct acpi_device *adev)
327 {
328 	struct device *dev;
329 
330 	dev = bus_find_device(&i2c_bus_type, NULL, adev,
331 			      i2c_acpi_find_match_device);
332 	return dev ? i2c_verify_client(dev) : NULL;
333 }
334 
335 static int i2c_acpi_notify(struct notifier_block *nb, unsigned long value,
336 			   void *arg)
337 {
338 	struct acpi_device *adev = arg;
339 	struct i2c_board_info info;
340 	acpi_handle adapter_handle;
341 	struct i2c_adapter *adapter;
342 	struct i2c_client *client;
343 
344 	switch (value) {
345 	case ACPI_RECONFIG_DEVICE_ADD:
346 		if (i2c_acpi_get_info(adev, &info, NULL, &adapter_handle))
347 			break;
348 
349 		adapter = i2c_acpi_find_adapter_by_handle(adapter_handle);
350 		if (!adapter)
351 			break;
352 
353 		i2c_acpi_register_device(adapter, adev, &info);
354 		break;
355 	case ACPI_RECONFIG_DEVICE_REMOVE:
356 		if (!acpi_device_enumerated(adev))
357 			break;
358 
359 		client = i2c_acpi_find_client_by_adev(adev);
360 		if (!client)
361 			break;
362 
363 		i2c_unregister_device(client);
364 		put_device(&client->dev);
365 		break;
366 	}
367 
368 	return NOTIFY_OK;
369 }
370 
371 struct notifier_block i2c_acpi_notifier = {
372 	.notifier_call = i2c_acpi_notify,
373 };
374 
375 /**
376  * i2c_acpi_new_device - Create i2c-client for the Nth I2cSerialBus resource
377  * @dev:     Device owning the ACPI resources to get the client from
378  * @index:   Index of ACPI resource to get
379  * @info:    describes the I2C device; note this is modified (addr gets set)
380  * Context: can sleep
381  *
382  * By default the i2c subsys creates an i2c-client for the first I2cSerialBus
383  * resource of an acpi_device, but some acpi_devices have multiple I2cSerialBus
384  * resources, in that case this function can be used to create an i2c-client
385  * for other I2cSerialBus resources in the Current Resource Settings table.
386  *
387  * Also see i2c_new_device, which this function calls to create the i2c-client.
388  *
389  * Returns a pointer to the new i2c-client, or NULL if the adapter is not found.
390  */
391 struct i2c_client *i2c_acpi_new_device(struct device *dev, int index,
392 				       struct i2c_board_info *info)
393 {
394 	struct i2c_acpi_lookup lookup;
395 	struct i2c_adapter *adapter;
396 	struct acpi_device *adev;
397 	LIST_HEAD(resource_list);
398 	int ret;
399 
400 	adev = ACPI_COMPANION(dev);
401 	if (!adev)
402 		return NULL;
403 
404 	memset(&lookup, 0, sizeof(lookup));
405 	lookup.info = info;
406 	lookup.device_handle = acpi_device_handle(adev);
407 	lookup.index = index;
408 
409 	ret = acpi_dev_get_resources(adev, &resource_list,
410 				     i2c_acpi_fill_info, &lookup);
411 	acpi_dev_free_resource_list(&resource_list);
412 
413 	if (ret < 0 || !info->addr)
414 		return NULL;
415 
416 	adapter = i2c_acpi_find_adapter_by_handle(lookup.adapter_handle);
417 	if (!adapter)
418 		return NULL;
419 
420 	return i2c_new_device(adapter, info);
421 }
422 EXPORT_SYMBOL_GPL(i2c_acpi_new_device);
423 
424 #ifdef CONFIG_ACPI_I2C_OPREGION
425 static int acpi_gsb_i2c_read_bytes(struct i2c_client *client,
426 		u8 cmd, u8 *data, u8 data_len)
427 {
428 
429 	struct i2c_msg msgs[2];
430 	int ret;
431 	u8 *buffer;
432 
433 	buffer = kzalloc(data_len, GFP_KERNEL);
434 	if (!buffer)
435 		return AE_NO_MEMORY;
436 
437 	msgs[0].addr = client->addr;
438 	msgs[0].flags = client->flags;
439 	msgs[0].len = 1;
440 	msgs[0].buf = &cmd;
441 
442 	msgs[1].addr = client->addr;
443 	msgs[1].flags = client->flags | I2C_M_RD;
444 	msgs[1].len = data_len;
445 	msgs[1].buf = buffer;
446 
447 	ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
448 	if (ret < 0) {
449 		/* Getting a NACK is unfortunately normal with some DSTDs */
450 		if (ret == -EREMOTEIO)
451 			dev_dbg(&client->adapter->dev, "i2c read %d bytes from client@%#x starting at reg %#x failed, error: %d\n",
452 				data_len, client->addr, cmd, ret);
453 		else
454 			dev_err(&client->adapter->dev, "i2c read %d bytes from client@%#x starting at reg %#x failed, error: %d\n",
455 				data_len, client->addr, cmd, ret);
456 	/* 2 transfers must have completed successfully */
457 	} else if (ret == 2) {
458 		memcpy(data, buffer, data_len);
459 		ret = 0;
460 	} else {
461 		ret = -EIO;
462 	}
463 
464 	kfree(buffer);
465 	return ret;
466 }
467 
468 static int acpi_gsb_i2c_write_bytes(struct i2c_client *client,
469 		u8 cmd, u8 *data, u8 data_len)
470 {
471 
472 	struct i2c_msg msgs[1];
473 	u8 *buffer;
474 	int ret = AE_OK;
475 
476 	buffer = kzalloc(data_len + 1, GFP_KERNEL);
477 	if (!buffer)
478 		return AE_NO_MEMORY;
479 
480 	buffer[0] = cmd;
481 	memcpy(buffer + 1, data, data_len);
482 
483 	msgs[0].addr = client->addr;
484 	msgs[0].flags = client->flags;
485 	msgs[0].len = data_len + 1;
486 	msgs[0].buf = buffer;
487 
488 	ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
489 
490 	kfree(buffer);
491 
492 	if (ret < 0) {
493 		dev_err(&client->adapter->dev, "i2c write failed: %d\n", ret);
494 		return ret;
495 	}
496 
497 	/* 1 transfer must have completed successfully */
498 	return (ret == 1) ? 0 : -EIO;
499 }
500 
501 static acpi_status
502 i2c_acpi_space_handler(u32 function, acpi_physical_address command,
503 			u32 bits, u64 *value64,
504 			void *handler_context, void *region_context)
505 {
506 	struct gsb_buffer *gsb = (struct gsb_buffer *)value64;
507 	struct i2c_acpi_handler_data *data = handler_context;
508 	struct acpi_connection_info *info = &data->info;
509 	struct acpi_resource_i2c_serialbus *sb;
510 	struct i2c_adapter *adapter = data->adapter;
511 	struct i2c_client *client;
512 	struct acpi_resource *ares;
513 	u32 accessor_type = function >> 16;
514 	u8 action = function & ACPI_IO_MASK;
515 	acpi_status ret;
516 	int status;
517 
518 	ret = acpi_buffer_to_resource(info->connection, info->length, &ares);
519 	if (ACPI_FAILURE(ret))
520 		return ret;
521 
522 	client = kzalloc(sizeof(*client), GFP_KERNEL);
523 	if (!client) {
524 		ret = AE_NO_MEMORY;
525 		goto err;
526 	}
527 
528 	if (!value64 || ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS) {
529 		ret = AE_BAD_PARAMETER;
530 		goto err;
531 	}
532 
533 	sb = &ares->data.i2c_serial_bus;
534 	if (sb->type != ACPI_RESOURCE_SERIAL_TYPE_I2C) {
535 		ret = AE_BAD_PARAMETER;
536 		goto err;
537 	}
538 
539 	client->adapter = adapter;
540 	client->addr = sb->slave_address;
541 
542 	if (sb->access_mode == ACPI_I2C_10BIT_MODE)
543 		client->flags |= I2C_CLIENT_TEN;
544 
545 	switch (accessor_type) {
546 	case ACPI_GSB_ACCESS_ATTRIB_SEND_RCV:
547 		if (action == ACPI_READ) {
548 			status = i2c_smbus_read_byte(client);
549 			if (status >= 0) {
550 				gsb->bdata = status;
551 				status = 0;
552 			}
553 		} else {
554 			status = i2c_smbus_write_byte(client, gsb->bdata);
555 		}
556 		break;
557 
558 	case ACPI_GSB_ACCESS_ATTRIB_BYTE:
559 		if (action == ACPI_READ) {
560 			status = i2c_smbus_read_byte_data(client, command);
561 			if (status >= 0) {
562 				gsb->bdata = status;
563 				status = 0;
564 			}
565 		} else {
566 			status = i2c_smbus_write_byte_data(client, command,
567 					gsb->bdata);
568 		}
569 		break;
570 
571 	case ACPI_GSB_ACCESS_ATTRIB_WORD:
572 		if (action == ACPI_READ) {
573 			status = i2c_smbus_read_word_data(client, command);
574 			if (status >= 0) {
575 				gsb->wdata = status;
576 				status = 0;
577 			}
578 		} else {
579 			status = i2c_smbus_write_word_data(client, command,
580 					gsb->wdata);
581 		}
582 		break;
583 
584 	case ACPI_GSB_ACCESS_ATTRIB_BLOCK:
585 		if (action == ACPI_READ) {
586 			status = i2c_smbus_read_block_data(client, command,
587 					gsb->data);
588 			if (status >= 0) {
589 				gsb->len = status;
590 				status = 0;
591 			}
592 		} else {
593 			status = i2c_smbus_write_block_data(client, command,
594 					gsb->len, gsb->data);
595 		}
596 		break;
597 
598 	case ACPI_GSB_ACCESS_ATTRIB_MULTIBYTE:
599 		if (action == ACPI_READ) {
600 			status = acpi_gsb_i2c_read_bytes(client, command,
601 					gsb->data, info->access_length);
602 		} else {
603 			status = acpi_gsb_i2c_write_bytes(client, command,
604 					gsb->data, info->access_length);
605 		}
606 		break;
607 
608 	default:
609 		dev_warn(&adapter->dev, "protocol 0x%02x not supported for client 0x%02x\n",
610 			 accessor_type, client->addr);
611 		ret = AE_BAD_PARAMETER;
612 		goto err;
613 	}
614 
615 	gsb->status = status;
616 
617  err:
618 	kfree(client);
619 	ACPI_FREE(ares);
620 	return ret;
621 }
622 
623 
624 int i2c_acpi_install_space_handler(struct i2c_adapter *adapter)
625 {
626 	acpi_handle handle;
627 	struct i2c_acpi_handler_data *data;
628 	acpi_status status;
629 
630 	if (!adapter->dev.parent)
631 		return -ENODEV;
632 
633 	handle = ACPI_HANDLE(adapter->dev.parent);
634 
635 	if (!handle)
636 		return -ENODEV;
637 
638 	data = kzalloc(sizeof(struct i2c_acpi_handler_data),
639 			    GFP_KERNEL);
640 	if (!data)
641 		return -ENOMEM;
642 
643 	data->adapter = adapter;
644 	status = acpi_bus_attach_private_data(handle, (void *)data);
645 	if (ACPI_FAILURE(status)) {
646 		kfree(data);
647 		return -ENOMEM;
648 	}
649 
650 	status = acpi_install_address_space_handler(handle,
651 				ACPI_ADR_SPACE_GSBUS,
652 				&i2c_acpi_space_handler,
653 				NULL,
654 				data);
655 	if (ACPI_FAILURE(status)) {
656 		dev_err(&adapter->dev, "Error installing i2c space handler\n");
657 		acpi_bus_detach_private_data(handle);
658 		kfree(data);
659 		return -ENOMEM;
660 	}
661 
662 	acpi_walk_dep_device_list(handle);
663 	return 0;
664 }
665 
666 void i2c_acpi_remove_space_handler(struct i2c_adapter *adapter)
667 {
668 	acpi_handle handle;
669 	struct i2c_acpi_handler_data *data;
670 	acpi_status status;
671 
672 	if (!adapter->dev.parent)
673 		return;
674 
675 	handle = ACPI_HANDLE(adapter->dev.parent);
676 
677 	if (!handle)
678 		return;
679 
680 	acpi_remove_address_space_handler(handle,
681 				ACPI_ADR_SPACE_GSBUS,
682 				&i2c_acpi_space_handler);
683 
684 	status = acpi_bus_get_private_data(handle, (void **)&data);
685 	if (ACPI_SUCCESS(status))
686 		kfree(data);
687 
688 	acpi_bus_detach_private_data(handle);
689 }
690 #endif /* CONFIG_ACPI_I2C_OPREGION */
691