Commit | Line | Data |
---|---|---|
dd4cae8b | 1 | /* |
021cbc1e | 2 | * Microchip AR1020 and AR1021 driver for I2C |
dd4cae8b CG |
3 | * |
4 | * Author: Christian Gmeiner <christian.gmeiner@gmail.com> | |
5 | * | |
6 | * License: GPLv2 as published by the FSF. | |
7 | */ | |
8 | ||
8a038b83 | 9 | #include <linux/bitops.h> |
dd4cae8b CG |
10 | #include <linux/module.h> |
11 | #include <linux/input.h> | |
12 | #include <linux/of.h> | |
13 | #include <linux/i2c.h> | |
14 | #include <linux/irq.h> | |
15 | #include <linux/interrupt.h> | |
16 | ||
17 | #define AR1021_TOCUH_PKG_SIZE 5 | |
18 | ||
19 | #define AR1021_MAX_X 4095 | |
20 | #define AR1021_MAX_Y 4095 | |
21 | ||
e55057e8 MK |
22 | #define AR1021_CMD 0x55 |
23 | ||
24 | #define AR1021_CMD_ENABLE_TOUCH 0x12 | |
25 | ||
dd4cae8b CG |
26 | struct ar1021_i2c { |
27 | struct i2c_client *client; | |
28 | struct input_dev *input; | |
29 | u8 data[AR1021_TOCUH_PKG_SIZE]; | |
30 | }; | |
31 | ||
32 | static irqreturn_t ar1021_i2c_irq(int irq, void *dev_id) | |
33 | { | |
34 | struct ar1021_i2c *ar1021 = dev_id; | |
35 | struct input_dev *input = ar1021->input; | |
36 | u8 *data = ar1021->data; | |
37 | unsigned int x, y, button; | |
38 | int retval; | |
39 | ||
40 | retval = i2c_master_recv(ar1021->client, | |
2274c987 | 41 | ar1021->data, sizeof(ar1021->data)); |
dd4cae8b CG |
42 | if (retval != sizeof(ar1021->data)) |
43 | goto out; | |
44 | ||
45 | /* sync bit set ? */ | |
8a038b83 | 46 | if (!(data[0] & BIT(7))) |
dd4cae8b CG |
47 | goto out; |
48 | ||
49 | button = data[0] & BIT(0); | |
50 | x = ((data[2] & 0x1f) << 7) | (data[1] & 0x7f); | |
51 | y = ((data[4] & 0x1f) << 7) | (data[3] & 0x7f); | |
52 | ||
53 | input_report_abs(input, ABS_X, x); | |
54 | input_report_abs(input, ABS_Y, y); | |
55 | input_report_key(input, BTN_TOUCH, button); | |
56 | input_sync(input); | |
57 | ||
58 | out: | |
59 | return IRQ_HANDLED; | |
60 | } | |
61 | ||
62 | static int ar1021_i2c_open(struct input_dev *dev) | |
63 | { | |
e55057e8 MK |
64 | static const u8 cmd_enable_touch[] = { |
65 | AR1021_CMD, | |
66 | 0x01, /* number of bytes after this */ | |
67 | AR1021_CMD_ENABLE_TOUCH | |
68 | }; | |
dd4cae8b CG |
69 | struct ar1021_i2c *ar1021 = input_get_drvdata(dev); |
70 | struct i2c_client *client = ar1021->client; | |
e55057e8 MK |
71 | int error; |
72 | ||
73 | error = i2c_master_send(ar1021->client, cmd_enable_touch, | |
74 | sizeof(cmd_enable_touch)); | |
75 | if (error < 0) | |
76 | return error; | |
dd4cae8b CG |
77 | |
78 | enable_irq(client->irq); | |
79 | ||
80 | return 0; | |
81 | } | |
82 | ||
83 | static void ar1021_i2c_close(struct input_dev *dev) | |
84 | { | |
85 | struct ar1021_i2c *ar1021 = input_get_drvdata(dev); | |
86 | struct i2c_client *client = ar1021->client; | |
87 | ||
88 | disable_irq(client->irq); | |
89 | } | |
90 | ||
91 | static int ar1021_i2c_probe(struct i2c_client *client, | |
2274c987 | 92 | const struct i2c_device_id *id) |
dd4cae8b CG |
93 | { |
94 | struct ar1021_i2c *ar1021; | |
95 | struct input_dev *input; | |
96 | int error; | |
97 | ||
98 | if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) { | |
99 | dev_err(&client->dev, "i2c_check_functionality error\n"); | |
100 | return -ENXIO; | |
101 | } | |
102 | ||
103 | ar1021 = devm_kzalloc(&client->dev, sizeof(*ar1021), GFP_KERNEL); | |
104 | if (!ar1021) | |
105 | return -ENOMEM; | |
106 | ||
107 | input = devm_input_allocate_device(&client->dev); | |
108 | if (!input) | |
109 | return -ENOMEM; | |
110 | ||
111 | ar1021->client = client; | |
112 | ar1021->input = input; | |
113 | ||
114 | input->name = "ar1021 I2C Touchscreen"; | |
115 | input->id.bustype = BUS_I2C; | |
116 | input->dev.parent = &client->dev; | |
117 | input->open = ar1021_i2c_open; | |
118 | input->close = ar1021_i2c_close; | |
119 | ||
120 | input_set_capability(input, EV_KEY, BTN_TOUCH); | |
121 | input_set_abs_params(input, ABS_X, 0, AR1021_MAX_X, 0, 0); | |
122 | input_set_abs_params(input, ABS_Y, 0, AR1021_MAX_Y, 0, 0); | |
123 | ||
124 | input_set_drvdata(input, ar1021); | |
125 | ||
126 | error = devm_request_threaded_irq(&client->dev, client->irq, | |
127 | NULL, ar1021_i2c_irq, | |
61e977b7 | 128 | IRQF_ONESHOT, |
dd4cae8b CG |
129 | "ar1021_i2c", ar1021); |
130 | if (error) { | |
131 | dev_err(&client->dev, | |
132 | "Failed to enable IRQ, error: %d\n", error); | |
133 | return error; | |
134 | } | |
135 | ||
136 | /* Disable the IRQ, we'll enable it in ar1021_i2c_open() */ | |
137 | disable_irq(client->irq); | |
138 | ||
139 | error = input_register_device(ar1021->input); | |
140 | if (error) { | |
141 | dev_err(&client->dev, | |
142 | "Failed to register input device, error: %d\n", error); | |
143 | return error; | |
144 | } | |
145 | ||
dd4cae8b CG |
146 | return 0; |
147 | } | |
148 | ||
149 | static int __maybe_unused ar1021_i2c_suspend(struct device *dev) | |
150 | { | |
151 | struct i2c_client *client = to_i2c_client(dev); | |
152 | ||
153 | disable_irq(client->irq); | |
154 | ||
155 | return 0; | |
156 | } | |
157 | ||
158 | static int __maybe_unused ar1021_i2c_resume(struct device *dev) | |
159 | { | |
160 | struct i2c_client *client = to_i2c_client(dev); | |
161 | ||
162 | enable_irq(client->irq); | |
163 | ||
164 | return 0; | |
165 | } | |
166 | ||
167 | static SIMPLE_DEV_PM_OPS(ar1021_i2c_pm, ar1021_i2c_suspend, ar1021_i2c_resume); | |
168 | ||
169 | static const struct i2c_device_id ar1021_i2c_id[] = { | |
95123fc4 | 170 | { "ar1021", 0 }, |
dd4cae8b CG |
171 | { }, |
172 | }; | |
173 | MODULE_DEVICE_TABLE(i2c, ar1021_i2c_id); | |
174 | ||
245165de | 175 | static const struct of_device_id ar1021_i2c_of_match[] = { |
dd4cae8b CG |
176 | { .compatible = "microchip,ar1021-i2c", }, |
177 | { } | |
178 | }; | |
179 | MODULE_DEVICE_TABLE(of, ar1021_i2c_of_match); | |
180 | ||
181 | static struct i2c_driver ar1021_i2c_driver = { | |
182 | .driver = { | |
183 | .name = "ar1021_i2c", | |
dd4cae8b CG |
184 | .pm = &ar1021_i2c_pm, |
185 | .of_match_table = ar1021_i2c_of_match, | |
186 | }, | |
187 | ||
188 | .probe = ar1021_i2c_probe, | |
189 | .id_table = ar1021_i2c_id, | |
190 | }; | |
191 | module_i2c_driver(ar1021_i2c_driver); | |
192 | ||
193 | MODULE_AUTHOR("Christian Gmeiner <christian.gmeiner@gmail.com>"); | |
021cbc1e | 194 | MODULE_DESCRIPTION("Microchip AR1020 and AR1021 I2C Driver"); |
dd4cae8b | 195 | MODULE_LICENSE("GPL"); |