2 * HID over I2C Open Firmware Subclass
4 * Copyright (c) 2012 Benjamin Tissoires <benjamin.tissoires@gmail.com>
5 * Copyright (c) 2012 Ecole Nationale de l'Aviation Civile, France
6 * Copyright (c) 2012 Red Hat, Inc
8 * This code was forked out of the core code, which was partly based on
9 * "USB HID support for Linux":
11 * Copyright (c) 1999 Andreas Gal
12 * Copyright (c) 2000-2005 Vojtech Pavlik <vojtech@suse.cz>
13 * Copyright (c) 2005 Michael Haboustak <mike-@cinci.rr.com> for Concept2, Inc
14 * Copyright (c) 2007-2008 Oliver Neukum
15 * Copyright (c) 2006-2010 Jiri Kosina
17 * This file is subject to the terms and conditions of the GNU General Public
18 * License. See the file COPYING in the main directory of this archive for
22 #include <linux/delay.h>
23 #include <linux/device.h>
24 #include <linux/gpio/consumer.h>
25 #include <linux/hid.h>
26 #include <linux/i2c.h>
27 #include <linux/kernel.h>
28 #include <linux/module.h>
31 #include <linux/regulator/consumer.h>
36 struct i2chid_ops ops;
38 struct i2c_client *client;
39 struct gpio_desc *reset_gpio;
40 struct regulator_bulk_data supplies[2];
41 int post_power_delay_ms;
42 int post_reset_delay_ms;
45 static int i2c_hid_of_power_up(struct i2chid_ops *ops)
47 struct i2c_hid_of *ihid_of = container_of(ops, struct i2c_hid_of, ops);
48 struct device *dev = &ihid_of->client->dev;
51 ret = regulator_bulk_enable(ARRAY_SIZE(ihid_of->supplies),
54 dev_warn(dev, "Failed to enable supplies: %d\n", ret);
58 if (ihid_of->post_power_delay_ms)
59 msleep(ihid_of->post_power_delay_ms);
61 gpiod_set_value_cansleep(ihid_of->reset_gpio, 0);
62 if (ihid_of->post_reset_delay_ms)
63 msleep(ihid_of->post_reset_delay_ms);
68 static void i2c_hid_of_power_down(struct i2chid_ops *ops)
70 struct i2c_hid_of *ihid_of = container_of(ops, struct i2c_hid_of, ops);
72 gpiod_set_value_cansleep(ihid_of->reset_gpio, 1);
73 regulator_bulk_disable(ARRAY_SIZE(ihid_of->supplies),
77 static int i2c_hid_of_probe(struct i2c_client *client)
79 struct device *dev = &client->dev;
80 struct i2c_hid_of *ihid_of;
81 u16 hid_descriptor_address;
86 ihid_of = devm_kzalloc(dev, sizeof(*ihid_of), GFP_KERNEL);
90 ihid_of->client = client;
91 ihid_of->ops.power_up = i2c_hid_of_power_up;
92 ihid_of->ops.power_down = i2c_hid_of_power_down;
94 ret = device_property_read_u32(dev, "hid-descr-addr", &val);
96 dev_err(dev, "HID register address not provided\n");
100 dev_err(dev, "Bad HID register address: 0x%08x\n", val);
103 hid_descriptor_address = val;
105 if (!device_property_read_u32(dev, "post-power-on-delay-ms", &val))
106 ihid_of->post_power_delay_ms = val;
109 * Note this is a kernel internal device-property set by x86 platform code,
110 * this MUST not be used in devicetree files without first adding it to
113 if (!device_property_read_u32(dev, "post-reset-deassert-delay-ms", &val))
114 ihid_of->post_reset_delay_ms = val;
116 /* Start out with reset asserted */
117 ihid_of->reset_gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH);
118 if (IS_ERR(ihid_of->reset_gpio))
119 return PTR_ERR(ihid_of->reset_gpio);
121 ihid_of->supplies[0].supply = "vdd";
122 ihid_of->supplies[1].supply = "vddl";
123 ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(ihid_of->supplies),
128 if (device_property_read_bool(dev, "touchscreen-inverted-x"))
129 quirks |= HID_QUIRK_X_INVERT;
131 if (device_property_read_bool(dev, "touchscreen-inverted-y"))
132 quirks |= HID_QUIRK_Y_INVERT;
134 return i2c_hid_core_probe(client, &ihid_of->ops,
135 hid_descriptor_address, quirks);
139 static const struct of_device_id i2c_hid_of_match[] = {
140 { .compatible = "hid-over-i2c" },
143 MODULE_DEVICE_TABLE(of, i2c_hid_of_match);
146 static const struct i2c_device_id i2c_hid_of_id_table[] = {
148 { "hid-over-i2c", 0 },
151 MODULE_DEVICE_TABLE(i2c, i2c_hid_of_id_table);
153 static struct i2c_driver i2c_hid_of_driver = {
155 .name = "i2c_hid_of",
156 .pm = &i2c_hid_core_pm,
157 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
158 .of_match_table = of_match_ptr(i2c_hid_of_match),
161 .probe = i2c_hid_of_probe,
162 .remove = i2c_hid_core_remove,
163 .shutdown = i2c_hid_core_shutdown,
164 .id_table = i2c_hid_of_id_table,
167 module_i2c_driver(i2c_hid_of_driver);
169 MODULE_DESCRIPTION("HID over I2C OF driver");
170 MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
171 MODULE_LICENSE("GPL");