2 * HID over I2C Open Firmware Subclass
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
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->ops.power_up = i2c_hid_of_power_up;
91 ihid_of->ops.power_down = i2c_hid_of_power_down;
93 ret = device_property_read_u32(dev, "hid-descr-addr", &val);
95 dev_err(dev, "HID register address not provided\n");
99 dev_err(dev, "Bad HID register address: 0x%08x\n", val);
102 hid_descriptor_address = val;
104 if (!device_property_read_u32(dev, "post-power-on-delay-ms", &val))
105 ihid_of->post_power_delay_ms = val;
108 * Note this is a kernel internal device-property set by x86 platform code,
109 * this MUST not be used in devicetree files without first adding it to
112 if (!device_property_read_u32(dev, "post-reset-deassert-delay-ms", &val))
113 ihid_of->post_reset_delay_ms = val;
115 /* Start out with reset asserted */
116 ihid_of->reset_gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH);
117 if (IS_ERR(ihid_of->reset_gpio))
118 return PTR_ERR(ihid_of->reset_gpio);
120 ihid_of->supplies[0].supply = "vdd";
121 ihid_of->supplies[1].supply = "vddl";
122 ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(ihid_of->supplies),
127 if (device_property_read_bool(dev, "touchscreen-inverted-x"))
128 quirks |= HID_QUIRK_X_INVERT;
130 if (device_property_read_bool(dev, "touchscreen-inverted-y"))
131 quirks |= HID_QUIRK_Y_INVERT;
133 return i2c_hid_core_probe(client, &ihid_of->ops,
134 hid_descriptor_address, quirks);
138 static const struct of_device_id i2c_hid_of_match[] = {
139 { .compatible = "hid-over-i2c" },
142 MODULE_DEVICE_TABLE(of, i2c_hid_of_match);
145 static const struct i2c_device_id i2c_hid_of_id_table[] = {
147 { "hid-over-i2c", 0 },
150 MODULE_DEVICE_TABLE(i2c, i2c_hid_of_id_table);
152 static struct i2c_driver i2c_hid_of_driver = {
154 .name = "i2c_hid_of",
155 .pm = &i2c_hid_core_pm,
156 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
157 .of_match_table = of_match_ptr(i2c_hid_of_match),
160 .probe = i2c_hid_of_probe,
161 .remove = i2c_hid_core_remove,
162 .shutdown = i2c_hid_core_shutdown,
163 .id_table = i2c_hid_of_id_table,
166 module_i2c_driver(i2c_hid_of_driver);
168 MODULE_DESCRIPTION("HID over I2C OF driver");
170 MODULE_LICENSE("GPL");