1b33752c3SDouglas Anderson /*
2b33752c3SDouglas Anderson * HID over I2C Open Firmware 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/delay.h>
23b33752c3SDouglas Anderson #include <linux/device.h>
242be40448SHans de Goede #include <linux/gpio/consumer.h>
25b60d3c80SAlistair Francis #include <linux/hid.h>
26b33752c3SDouglas Anderson #include <linux/i2c.h>
27b33752c3SDouglas Anderson #include <linux/kernel.h>
28b33752c3SDouglas Anderson #include <linux/module.h>
29b33752c3SDouglas Anderson #include <linux/of.h>
30b33752c3SDouglas Anderson #include <linux/pm.h>
31b33752c3SDouglas Anderson #include <linux/regulator/consumer.h>
32b33752c3SDouglas Anderson
33b33752c3SDouglas Anderson #include "i2c-hid.h"
34b33752c3SDouglas Anderson
35b33752c3SDouglas Anderson struct i2c_hid_of {
36b33752c3SDouglas Anderson struct i2chid_ops ops;
37b33752c3SDouglas Anderson
38b33752c3SDouglas Anderson struct i2c_client *client;
392be40448SHans de Goede struct gpio_desc *reset_gpio;
40b33752c3SDouglas Anderson struct regulator_bulk_data supplies[2];
41b33752c3SDouglas Anderson int post_power_delay_ms;
422be40448SHans de Goede int post_reset_delay_ms;
43b33752c3SDouglas Anderson };
44b33752c3SDouglas Anderson
i2c_hid_of_power_up(struct i2chid_ops * ops)45b33752c3SDouglas Anderson static int i2c_hid_of_power_up(struct i2chid_ops *ops)
46b33752c3SDouglas Anderson {
47b33752c3SDouglas Anderson struct i2c_hid_of *ihid_of = container_of(ops, struct i2c_hid_of, ops);
48b33752c3SDouglas Anderson struct device *dev = &ihid_of->client->dev;
49b33752c3SDouglas Anderson int ret;
50b33752c3SDouglas Anderson
51b33752c3SDouglas Anderson ret = regulator_bulk_enable(ARRAY_SIZE(ihid_of->supplies),
52b33752c3SDouglas Anderson ihid_of->supplies);
53b33752c3SDouglas Anderson if (ret) {
54b33752c3SDouglas Anderson dev_warn(dev, "Failed to enable supplies: %d\n", ret);
55b33752c3SDouglas Anderson return ret;
56b33752c3SDouglas Anderson }
57b33752c3SDouglas Anderson
58b33752c3SDouglas Anderson if (ihid_of->post_power_delay_ms)
59b33752c3SDouglas Anderson msleep(ihid_of->post_power_delay_ms);
60b33752c3SDouglas Anderson
612be40448SHans de Goede gpiod_set_value_cansleep(ihid_of->reset_gpio, 0);
622be40448SHans de Goede if (ihid_of->post_reset_delay_ms)
632be40448SHans de Goede msleep(ihid_of->post_reset_delay_ms);
642be40448SHans de Goede
65b33752c3SDouglas Anderson return 0;
66b33752c3SDouglas Anderson }
67b33752c3SDouglas Anderson
i2c_hid_of_power_down(struct i2chid_ops * ops)68b33752c3SDouglas Anderson static void i2c_hid_of_power_down(struct i2chid_ops *ops)
69b33752c3SDouglas Anderson {
70b33752c3SDouglas Anderson struct i2c_hid_of *ihid_of = container_of(ops, struct i2c_hid_of, ops);
71b33752c3SDouglas Anderson
722be40448SHans de Goede gpiod_set_value_cansleep(ihid_of->reset_gpio, 1);
73b33752c3SDouglas Anderson regulator_bulk_disable(ARRAY_SIZE(ihid_of->supplies),
74b33752c3SDouglas Anderson ihid_of->supplies);
75b33752c3SDouglas Anderson }
76b33752c3SDouglas Anderson
i2c_hid_of_probe(struct i2c_client * client)77baf34f3bSStephen Kitt static int i2c_hid_of_probe(struct i2c_client *client)
78b33752c3SDouglas Anderson {
79b33752c3SDouglas Anderson struct device *dev = &client->dev;
80b33752c3SDouglas Anderson struct i2c_hid_of *ihid_of;
81b33752c3SDouglas Anderson u16 hid_descriptor_address;
82b60d3c80SAlistair Francis u32 quirks = 0;
83b33752c3SDouglas Anderson int ret;
84b33752c3SDouglas Anderson u32 val;
85b33752c3SDouglas Anderson
869d793e7cSHans de Goede ihid_of = devm_kzalloc(dev, sizeof(*ihid_of), GFP_KERNEL);
87b33752c3SDouglas Anderson if (!ihid_of)
88b33752c3SDouglas Anderson return -ENOMEM;
89b33752c3SDouglas Anderson
90*00aab7dcSJohan Hovold ihid_of->client = client;
91b33752c3SDouglas Anderson ihid_of->ops.power_up = i2c_hid_of_power_up;
92b33752c3SDouglas Anderson ihid_of->ops.power_down = i2c_hid_of_power_down;
93b33752c3SDouglas Anderson
94728ec8b6SHans de Goede ret = device_property_read_u32(dev, "hid-descr-addr", &val);
95b33752c3SDouglas Anderson if (ret) {
969d793e7cSHans de Goede dev_err(dev, "HID register address not provided\n");
97b33752c3SDouglas Anderson return -ENODEV;
98b33752c3SDouglas Anderson }
99b33752c3SDouglas Anderson if (val >> 16) {
1009d793e7cSHans de Goede dev_err(dev, "Bad HID register address: 0x%08x\n", val);
101b33752c3SDouglas Anderson return -EINVAL;
102b33752c3SDouglas Anderson }
103b33752c3SDouglas Anderson hid_descriptor_address = val;
104b33752c3SDouglas Anderson
1059d793e7cSHans de Goede if (!device_property_read_u32(dev, "post-power-on-delay-ms", &val))
106b33752c3SDouglas Anderson ihid_of->post_power_delay_ms = val;
107b33752c3SDouglas Anderson
1082be40448SHans de Goede /*
1092be40448SHans de Goede * Note this is a kernel internal device-property set by x86 platform code,
1102be40448SHans de Goede * this MUST not be used in devicetree files without first adding it to
1112be40448SHans de Goede * the DT bindings.
1122be40448SHans de Goede */
1132be40448SHans de Goede if (!device_property_read_u32(dev, "post-reset-deassert-delay-ms", &val))
1142be40448SHans de Goede ihid_of->post_reset_delay_ms = val;
1152be40448SHans de Goede
1162be40448SHans de Goede /* Start out with reset asserted */
1172be40448SHans de Goede ihid_of->reset_gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH);
1182be40448SHans de Goede if (IS_ERR(ihid_of->reset_gpio))
1192be40448SHans de Goede return PTR_ERR(ihid_of->reset_gpio);
1202be40448SHans de Goede
121b33752c3SDouglas Anderson ihid_of->supplies[0].supply = "vdd";
122b33752c3SDouglas Anderson ihid_of->supplies[1].supply = "vddl";
1239d793e7cSHans de Goede ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(ihid_of->supplies),
124b33752c3SDouglas Anderson ihid_of->supplies);
125b33752c3SDouglas Anderson if (ret)
126b33752c3SDouglas Anderson return ret;
127b33752c3SDouglas Anderson
128b60d3c80SAlistair Francis if (device_property_read_bool(dev, "touchscreen-inverted-x"))
129b60d3c80SAlistair Francis quirks |= HID_QUIRK_X_INVERT;
130b60d3c80SAlistair Francis
131b60d3c80SAlistair Francis if (device_property_read_bool(dev, "touchscreen-inverted-y"))
132b60d3c80SAlistair Francis quirks |= HID_QUIRK_Y_INVERT;
133b60d3c80SAlistair Francis
134b33752c3SDouglas Anderson return i2c_hid_core_probe(client, &ihid_of->ops,
135b60d3c80SAlistair Francis hid_descriptor_address, quirks);
136b33752c3SDouglas Anderson }
137b33752c3SDouglas Anderson
138728ec8b6SHans de Goede #ifdef CONFIG_OF
139b33752c3SDouglas Anderson static const struct of_device_id i2c_hid_of_match[] = {
140b33752c3SDouglas Anderson { .compatible = "hid-over-i2c" },
141b33752c3SDouglas Anderson {},
142b33752c3SDouglas Anderson };
143b33752c3SDouglas Anderson MODULE_DEVICE_TABLE(of, i2c_hid_of_match);
144728ec8b6SHans de Goede #endif
145b33752c3SDouglas Anderson
146b33752c3SDouglas Anderson static const struct i2c_device_id i2c_hid_of_id_table[] = {
147b33752c3SDouglas Anderson { "hid", 0 },
148b33752c3SDouglas Anderson { "hid-over-i2c", 0 },
149b33752c3SDouglas Anderson { },
150b33752c3SDouglas Anderson };
151b33752c3SDouglas Anderson MODULE_DEVICE_TABLE(i2c, i2c_hid_of_id_table);
152b33752c3SDouglas Anderson
153b33752c3SDouglas Anderson static struct i2c_driver i2c_hid_of_driver = {
154b33752c3SDouglas Anderson .driver = {
155b33752c3SDouglas Anderson .name = "i2c_hid_of",
156b33752c3SDouglas Anderson .pm = &i2c_hid_core_pm,
157b33752c3SDouglas Anderson .probe_type = PROBE_PREFER_ASYNCHRONOUS,
158b33752c3SDouglas Anderson .of_match_table = of_match_ptr(i2c_hid_of_match),
159b33752c3SDouglas Anderson },
160b33752c3SDouglas Anderson
161e4b88075SUwe Kleine-König .probe = i2c_hid_of_probe,
162b33752c3SDouglas Anderson .remove = i2c_hid_core_remove,
163b33752c3SDouglas Anderson .shutdown = i2c_hid_core_shutdown,
164b33752c3SDouglas Anderson .id_table = i2c_hid_of_id_table,
165b33752c3SDouglas Anderson };
166b33752c3SDouglas Anderson
167b33752c3SDouglas Anderson module_i2c_driver(i2c_hid_of_driver);
168b33752c3SDouglas Anderson
169b33752c3SDouglas Anderson MODULE_DESCRIPTION("HID over I2C OF driver");
170b33752c3SDouglas Anderson MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
171b33752c3SDouglas Anderson MODULE_LICENSE("GPL");
172