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