xref: /openbmc/linux/drivers/hid/i2c-hid/i2c-hid-of.c (revision 476ec641)
1 /*
2  * HID over I2C Open Firmware Subclass
3  *
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
7  *
8  * This code was forked out of the core code, which was partly based on
9  * "USB HID support for Linux":
10  *
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
16  *
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
19  * more details.
20  */
21 
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>
29 #include <linux/of.h>
30 #include <linux/pm.h>
31 #include <linux/regulator/consumer.h>
32 
33 #include "i2c-hid.h"
34 
35 struct i2c_hid_of {
36 	struct i2chid_ops ops;
37 
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;
43 };
44 
45 static int i2c_hid_of_power_up(struct i2chid_ops *ops)
46 {
47 	struct i2c_hid_of *ihid_of = container_of(ops, struct i2c_hid_of, ops);
48 	struct device *dev = &ihid_of->client->dev;
49 	int ret;
50 
51 	ret = regulator_bulk_enable(ARRAY_SIZE(ihid_of->supplies),
52 				    ihid_of->supplies);
53 	if (ret) {
54 		dev_warn(dev, "Failed to enable supplies: %d\n", ret);
55 		return ret;
56 	}
57 
58 	if (ihid_of->post_power_delay_ms)
59 		msleep(ihid_of->post_power_delay_ms);
60 
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);
64 
65 	return 0;
66 }
67 
68 static void i2c_hid_of_power_down(struct i2chid_ops *ops)
69 {
70 	struct i2c_hid_of *ihid_of = container_of(ops, struct i2c_hid_of, ops);
71 
72 	gpiod_set_value_cansleep(ihid_of->reset_gpio, 1);
73 	regulator_bulk_disable(ARRAY_SIZE(ihid_of->supplies),
74 			       ihid_of->supplies);
75 }
76 
77 static int i2c_hid_of_probe(struct i2c_client *client)
78 {
79 	struct device *dev = &client->dev;
80 	struct i2c_hid_of *ihid_of;
81 	u16 hid_descriptor_address;
82 	u32 quirks = 0;
83 	int ret;
84 	u32 val;
85 
86 	ihid_of = devm_kzalloc(dev, sizeof(*ihid_of), GFP_KERNEL);
87 	if (!ihid_of)
88 		return -ENOMEM;
89 
90 	ihid_of->ops.power_up = i2c_hid_of_power_up;
91 	ihid_of->ops.power_down = i2c_hid_of_power_down;
92 
93 	ret = device_property_read_u32(dev, "hid-descr-addr", &val);
94 	if (ret) {
95 		dev_err(dev, "HID register address not provided\n");
96 		return -ENODEV;
97 	}
98 	if (val >> 16) {
99 		dev_err(dev, "Bad HID register address: 0x%08x\n", val);
100 		return -EINVAL;
101 	}
102 	hid_descriptor_address = val;
103 
104 	if (!device_property_read_u32(dev, "post-power-on-delay-ms", &val))
105 		ihid_of->post_power_delay_ms = val;
106 
107 	/*
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
110 	 * the DT bindings.
111 	 */
112 	if (!device_property_read_u32(dev, "post-reset-deassert-delay-ms", &val))
113 		ihid_of->post_reset_delay_ms = val;
114 
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);
119 
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),
123 				      ihid_of->supplies);
124 	if (ret)
125 		return ret;
126 
127 	if (device_property_read_bool(dev, "touchscreen-inverted-x"))
128 		quirks |= HID_QUIRK_X_INVERT;
129 
130 	if (device_property_read_bool(dev, "touchscreen-inverted-y"))
131 		quirks |= HID_QUIRK_Y_INVERT;
132 
133 	return i2c_hid_core_probe(client, &ihid_of->ops,
134 				  hid_descriptor_address, quirks);
135 }
136 
137 #ifdef CONFIG_OF
138 static const struct of_device_id i2c_hid_of_match[] = {
139 	{ .compatible = "hid-over-i2c" },
140 	{},
141 };
142 MODULE_DEVICE_TABLE(of, i2c_hid_of_match);
143 #endif
144 
145 static const struct i2c_device_id i2c_hid_of_id_table[] = {
146 	{ "hid", 0 },
147 	{ "hid-over-i2c", 0 },
148 	{ },
149 };
150 MODULE_DEVICE_TABLE(i2c, i2c_hid_of_id_table);
151 
152 static struct i2c_driver i2c_hid_of_driver = {
153 	.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),
158 	},
159 
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,
164 };
165 
166 module_i2c_driver(i2c_hid_of_driver);
167 
168 MODULE_DESCRIPTION("HID over I2C OF driver");
169 MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
170 MODULE_LICENSE("GPL");
171