1b33752c3SDouglas Anderson /*
2b33752c3SDouglas Anderson  * HID over I2C ACPI Subclass
3b33752c3SDouglas Anderson  *
4b33752c3SDouglas Anderson  * Copyright (c) 2012 Benjamin Tissoires <benjamin.tissoires@gmail.com>
5b33752c3SDouglas Anderson  * Copyright (c) 2012 Ecole Nationale de l'Aviation Civile, France
6b33752c3SDouglas Anderson  * Copyright (c) 2012 Red Hat, Inc
7b33752c3SDouglas Anderson  *
8b33752c3SDouglas Anderson  * This code was forked out of the core code, which was partly based on
9b33752c3SDouglas Anderson  * "USB HID support for Linux":
10b33752c3SDouglas Anderson  *
11b33752c3SDouglas Anderson  *  Copyright (c) 1999 Andreas Gal
12b33752c3SDouglas Anderson  *  Copyright (c) 2000-2005 Vojtech Pavlik <vojtech@suse.cz>
13b33752c3SDouglas Anderson  *  Copyright (c) 2005 Michael Haboustak <mike-@cinci.rr.com> for Concept2, Inc
14b33752c3SDouglas Anderson  *  Copyright (c) 2007-2008 Oliver Neukum
15b33752c3SDouglas Anderson  *  Copyright (c) 2006-2010 Jiri Kosina
16b33752c3SDouglas Anderson  *
17b33752c3SDouglas Anderson  * This file is subject to the terms and conditions of the GNU General Public
18b33752c3SDouglas Anderson  * License.  See the file COPYING in the main directory of this archive for
19b33752c3SDouglas Anderson  * more details.
20b33752c3SDouglas Anderson  */
21b33752c3SDouglas Anderson 
22b33752c3SDouglas Anderson #include <linux/acpi.h>
23b33752c3SDouglas Anderson #include <linux/device.h>
24b33752c3SDouglas Anderson #include <linux/i2c.h>
25b33752c3SDouglas Anderson #include <linux/kernel.h>
26b33752c3SDouglas Anderson #include <linux/module.h>
27b33752c3SDouglas Anderson #include <linux/pm.h>
28a3836a02SAndy Shevchenko #include <linux/uuid.h>
29b33752c3SDouglas Anderson 
30b33752c3SDouglas Anderson #include "i2c-hid.h"
31b33752c3SDouglas Anderson 
32b33752c3SDouglas Anderson struct i2c_hid_acpi {
33b33752c3SDouglas Anderson 	struct i2chid_ops ops;
346d97010eSAndy Shevchenko 	struct acpi_device *adev;
35b33752c3SDouglas Anderson };
36b33752c3SDouglas Anderson 
37b33752c3SDouglas Anderson static const struct acpi_device_id i2c_hid_acpi_blacklist[] = {
38b33752c3SDouglas Anderson 	/*
39b33752c3SDouglas Anderson 	 * The CHPN0001 ACPI device, which is used to describe the Chipone
40b33752c3SDouglas Anderson 	 * ICN8505 controller, has a _CID of PNP0C50 but is not HID compatible.
41b33752c3SDouglas Anderson 	 */
424122abfeSAndy Shevchenko 	{ "CHPN0001" },
434122abfeSAndy Shevchenko 	{ }
44b33752c3SDouglas Anderson };
45b33752c3SDouglas Anderson 
46a3836a02SAndy Shevchenko /* HID I²C Device: 3cdff6f7-4267-4555-ad05-b30a3d8938de */
47b33752c3SDouglas Anderson static guid_t i2c_hid_guid =
48b33752c3SDouglas Anderson 	GUID_INIT(0x3CDFF6F7, 0x4267, 0x4555,
49b33752c3SDouglas Anderson 		  0xAD, 0x05, 0xB3, 0x0A, 0x3D, 0x89, 0x38, 0xDE);
50a3836a02SAndy Shevchenko 
51572eaeb7SAndy Shevchenko static int i2c_hid_acpi_get_descriptor(struct i2c_hid_acpi *ihid_acpi)
52a3836a02SAndy Shevchenko {
53572eaeb7SAndy Shevchenko 	struct acpi_device *adev = ihid_acpi->adev;
546d97010eSAndy Shevchenko 	acpi_handle handle = acpi_device_handle(adev);
55b33752c3SDouglas Anderson 	union acpi_object *obj;
56b33752c3SDouglas Anderson 	u16 hid_descriptor_address;
57b33752c3SDouglas Anderson 
58b33752c3SDouglas Anderson 	if (acpi_match_device_ids(adev, i2c_hid_acpi_blacklist) == 0)
59b33752c3SDouglas Anderson 		return -ENODEV;
60b33752c3SDouglas Anderson 
61b33752c3SDouglas Anderson 	obj = acpi_evaluate_dsm_typed(handle, &i2c_hid_guid, 1, 1, NULL,
62b33752c3SDouglas Anderson 				      ACPI_TYPE_INTEGER);
63b33752c3SDouglas Anderson 	if (!obj) {
646d97010eSAndy Shevchenko 		acpi_handle_err(handle, "Error _DSM call to get HID descriptor address failed\n");
65b33752c3SDouglas Anderson 		return -ENODEV;
66b33752c3SDouglas Anderson 	}
67b33752c3SDouglas Anderson 
68b33752c3SDouglas Anderson 	hid_descriptor_address = obj->integer.value;
69b33752c3SDouglas Anderson 	ACPI_FREE(obj);
70b33752c3SDouglas Anderson 
71b33752c3SDouglas Anderson 	return hid_descriptor_address;
72b33752c3SDouglas Anderson }
73b33752c3SDouglas Anderson 
74b33752c3SDouglas Anderson static void i2c_hid_acpi_shutdown_tail(struct i2chid_ops *ops)
75b33752c3SDouglas Anderson {
766d97010eSAndy Shevchenko 	struct i2c_hid_acpi *ihid_acpi = container_of(ops, struct i2c_hid_acpi, ops);
776d97010eSAndy Shevchenko 
786d97010eSAndy Shevchenko 	acpi_device_set_power(ihid_acpi->adev, ACPI_STATE_D3_COLD);
79b33752c3SDouglas Anderson }
80b33752c3SDouglas Anderson 
814cecff8fSAndy Shevchenko static int i2c_hid_acpi_probe(struct i2c_client *client)
82b33752c3SDouglas Anderson {
83b33752c3SDouglas Anderson 	struct device *dev = &client->dev;
84b33752c3SDouglas Anderson 	struct i2c_hid_acpi *ihid_acpi;
85b33752c3SDouglas Anderson 	u16 hid_descriptor_address;
86b33752c3SDouglas Anderson 	int ret;
87b33752c3SDouglas Anderson 
88b33752c3SDouglas Anderson 	ihid_acpi = devm_kzalloc(&client->dev, sizeof(*ihid_acpi), GFP_KERNEL);
89b33752c3SDouglas Anderson 	if (!ihid_acpi)
90b33752c3SDouglas Anderson 		return -ENOMEM;
91b33752c3SDouglas Anderson 
92572eaeb7SAndy Shevchenko 	ihid_acpi->adev = ACPI_COMPANION(dev);
93b33752c3SDouglas Anderson 	ihid_acpi->ops.shutdown_tail = i2c_hid_acpi_shutdown_tail;
94b33752c3SDouglas Anderson 
95572eaeb7SAndy Shevchenko 	ret = i2c_hid_acpi_get_descriptor(ihid_acpi);
96b33752c3SDouglas Anderson 	if (ret < 0)
97b33752c3SDouglas Anderson 		return ret;
98b33752c3SDouglas Anderson 	hid_descriptor_address = ret;
99b33752c3SDouglas Anderson 
100572eaeb7SAndy Shevchenko 	acpi_device_fix_up_power(ihid_acpi->adev);
101b33752c3SDouglas Anderson 
102b33752c3SDouglas Anderson 	return i2c_hid_core_probe(client, &ihid_acpi->ops,
103b60d3c80SAlistair Francis 				  hid_descriptor_address, 0);
104b33752c3SDouglas Anderson }
105b33752c3SDouglas Anderson 
106b33752c3SDouglas Anderson static const struct acpi_device_id i2c_hid_acpi_match[] = {
1074122abfeSAndy Shevchenko 	{ "ACPI0C50" },
1084122abfeSAndy Shevchenko 	{ "PNP0C50" },
1094122abfeSAndy Shevchenko 	{ }
110b33752c3SDouglas Anderson };
111b33752c3SDouglas Anderson MODULE_DEVICE_TABLE(acpi, i2c_hid_acpi_match);
112b33752c3SDouglas Anderson 
113b33752c3SDouglas Anderson static struct i2c_driver i2c_hid_acpi_driver = {
114b33752c3SDouglas Anderson 	.driver = {
115b33752c3SDouglas Anderson 		.name	= "i2c_hid_acpi",
116b33752c3SDouglas Anderson 		.pm	= &i2c_hid_core_pm,
117b33752c3SDouglas Anderson 		.probe_type = PROBE_PREFER_ASYNCHRONOUS,
11809609410SAndy Shevchenko 		.acpi_match_table = i2c_hid_acpi_match,
119b33752c3SDouglas Anderson 	},
120b33752c3SDouglas Anderson 
121*e4b88075SUwe Kleine-König 	.probe		= i2c_hid_acpi_probe,
122b33752c3SDouglas Anderson 	.remove		= i2c_hid_core_remove,
123b33752c3SDouglas Anderson 	.shutdown	= i2c_hid_core_shutdown,
124b33752c3SDouglas Anderson };
125b33752c3SDouglas Anderson 
126b33752c3SDouglas Anderson module_i2c_driver(i2c_hid_acpi_driver);
127b33752c3SDouglas Anderson 
128b33752c3SDouglas Anderson MODULE_DESCRIPTION("HID over I2C ACPI driver");
129b33752c3SDouglas Anderson MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
130b33752c3SDouglas Anderson MODULE_LICENSE("GPL");
131