Loading...
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Copyright (C) 2016, Jelle van der Waa <jelle@vdwaa.nl>
4 */
5
6#include <linux/delay.h>
7#include <linux/i2c.h>
8#include <linux/input.h>
9#include <linux/input/mt.h>
10#include <linux/input/touchscreen.h>
11#include <linux/interrupt.h>
12#include <linux/module.h>
13#include <linux/regulator/consumer.h>
14#include <asm/unaligned.h>
15
16#define ZET6223_MAX_FINGERS 16
17#define ZET6223_MAX_PKT_SIZE (3 + 4 * ZET6223_MAX_FINGERS)
18
19#define ZET6223_CMD_INFO 0xB2
20#define ZET6223_CMD_INFO_LENGTH 17
21#define ZET6223_VALID_PACKET 0x3c
22
23#define ZET6223_POWER_ON_DELAY_MSEC 30
24
25struct zet6223_ts {
26 struct i2c_client *client;
27 struct input_dev *input;
28 struct regulator *vcc;
29 struct regulator *vio;
30 struct touchscreen_properties prop;
31 struct regulator_bulk_data supplies[2];
32 u16 max_x;
33 u16 max_y;
34 u8 fingernum;
35};
36
37static int zet6223_start(struct input_dev *dev)
38{
39 struct zet6223_ts *ts = input_get_drvdata(dev);
40
41 enable_irq(ts->client->irq);
42
43 return 0;
44}
45
46static void zet6223_stop(struct input_dev *dev)
47{
48 struct zet6223_ts *ts = input_get_drvdata(dev);
49
50 disable_irq(ts->client->irq);
51}
52
53static irqreturn_t zet6223_irq(int irq, void *dev_id)
54{
55 struct zet6223_ts *ts = dev_id;
56 u16 finger_bits;
57
58 /*
59 * First 3 bytes are an identifier, two bytes of finger data.
60 * X, Y data per finger is 4 bytes.
61 */
62 u8 bufsize = 3 + 4 * ts->fingernum;
63 u8 buf[ZET6223_MAX_PKT_SIZE];
64 int i;
65 int ret;
66 int error;
67
68 ret = i2c_master_recv(ts->client, buf, bufsize);
69 if (ret != bufsize) {
70 error = ret < 0 ? ret : -EIO;
71 dev_err_ratelimited(&ts->client->dev,
72 "Error reading input data: %d\n", error);
73 return IRQ_HANDLED;
74 }
75
76 if (buf[0] != ZET6223_VALID_PACKET)
77 return IRQ_HANDLED;
78
79 finger_bits = get_unaligned_be16(buf + 1);
80 for (i = 0; i < ts->fingernum; i++) {
81 if (!(finger_bits & BIT(15 - i)))
82 continue;
83
84 input_mt_slot(ts->input, i);
85 input_mt_report_slot_state(ts->input, MT_TOOL_FINGER, true);
86 input_event(ts->input, EV_ABS, ABS_MT_POSITION_X,
87 ((buf[i + 3] >> 4) << 8) + buf[i + 4]);
88 input_event(ts->input, EV_ABS, ABS_MT_POSITION_Y,
89 ((buf[i + 3] & 0xF) << 8) + buf[i + 5]);
90 }
91
92 input_mt_sync_frame(ts->input);
93 input_sync(ts->input);
94
95 return IRQ_HANDLED;
96}
97
98static void zet6223_power_off(void *_ts)
99{
100 struct zet6223_ts *ts = _ts;
101
102 regulator_bulk_disable(ARRAY_SIZE(ts->supplies), ts->supplies);
103}
104
105static int zet6223_power_on(struct zet6223_ts *ts)
106{
107 struct device *dev = &ts->client->dev;
108 int error;
109
110 ts->supplies[0].supply = "vio";
111 ts->supplies[1].supply = "vcc";
112
113 error = devm_regulator_bulk_get(dev, ARRAY_SIZE(ts->supplies),
114 ts->supplies);
115 if (error)
116 return error;
117
118 error = regulator_bulk_enable(ARRAY_SIZE(ts->supplies), ts->supplies);
119 if (error)
120 return error;
121
122 msleep(ZET6223_POWER_ON_DELAY_MSEC);
123
124 error = devm_add_action_or_reset(dev, zet6223_power_off, ts);
125 if (error) {
126 dev_err(dev, "failed to install poweroff action: %d\n", error);
127 return error;
128 }
129
130 return 0;
131}
132
133static int zet6223_query_device(struct zet6223_ts *ts)
134{
135 u8 buf[ZET6223_CMD_INFO_LENGTH];
136 u8 cmd = ZET6223_CMD_INFO;
137 int ret;
138 int error;
139
140 ret = i2c_master_send(ts->client, &cmd, sizeof(cmd));
141 if (ret != sizeof(cmd)) {
142 error = ret < 0 ? ret : -EIO;
143 dev_err(&ts->client->dev,
144 "touchpanel info cmd failed: %d\n", error);
145 return error;
146 }
147
148 ret = i2c_master_recv(ts->client, buf, sizeof(buf));
149 if (ret != sizeof(buf)) {
150 error = ret < 0 ? ret : -EIO;
151 dev_err(&ts->client->dev,
152 "failed to retrieve touchpanel info: %d\n", error);
153 return error;
154 }
155
156 ts->fingernum = buf[15] & 0x7F;
157 if (ts->fingernum > ZET6223_MAX_FINGERS) {
158 dev_warn(&ts->client->dev,
159 "touchpanel reports %d fingers, limiting to %d\n",
160 ts->fingernum, ZET6223_MAX_FINGERS);
161 ts->fingernum = ZET6223_MAX_FINGERS;
162 }
163
164 ts->max_x = get_unaligned_le16(&buf[8]);
165 ts->max_y = get_unaligned_le16(&buf[10]);
166
167 return 0;
168}
169
170static int zet6223_probe(struct i2c_client *client)
171{
172 struct device *dev = &client->dev;
173 struct zet6223_ts *ts;
174 struct input_dev *input;
175 int error;
176
177 if (!client->irq) {
178 dev_err(dev, "no irq specified\n");
179 return -EINVAL;
180 }
181
182 ts = devm_kzalloc(dev, sizeof(*ts), GFP_KERNEL);
183 if (!ts)
184 return -ENOMEM;
185
186 ts->client = client;
187
188 error = zet6223_power_on(ts);
189 if (error)
190 return error;
191
192 error = zet6223_query_device(ts);
193 if (error)
194 return error;
195
196 ts->input = input = devm_input_allocate_device(dev);
197 if (!input)
198 return -ENOMEM;
199
200 input_set_drvdata(input, ts);
201
202 input->name = client->name;
203 input->id.bustype = BUS_I2C;
204 input->open = zet6223_start;
205 input->close = zet6223_stop;
206
207 input_set_abs_params(input, ABS_MT_POSITION_X, 0, ts->max_x, 0, 0);
208 input_set_abs_params(input, ABS_MT_POSITION_Y, 0, ts->max_y, 0, 0);
209
210 touchscreen_parse_properties(input, true, &ts->prop);
211
212 error = input_mt_init_slots(input, ts->fingernum,
213 INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED);
214 if (error)
215 return error;
216
217 error = devm_request_threaded_irq(dev, client->irq, NULL, zet6223_irq,
218 IRQF_ONESHOT, client->name, ts);
219 if (error) {
220 dev_err(dev, "failed to request irq %d: %d\n",
221 client->irq, error);
222 return error;
223 }
224
225 zet6223_stop(input);
226
227 error = input_register_device(input);
228 if (error)
229 return error;
230
231 return 0;
232}
233
234static const struct of_device_id zet6223_of_match[] = {
235 { .compatible = "zeitec,zet6223" },
236 { }
237};
238MODULE_DEVICE_TABLE(of, zet6223_of_match);
239
240static const struct i2c_device_id zet6223_id[] = {
241 { "zet6223", 0},
242 { }
243};
244MODULE_DEVICE_TABLE(i2c, zet6223_id);
245
246static struct i2c_driver zet6223_driver = {
247 .driver = {
248 .name = "zet6223",
249 .of_match_table = zet6223_of_match,
250 },
251 .probe_new = zet6223_probe,
252 .id_table = zet6223_id
253};
254module_i2c_driver(zet6223_driver);
255
256MODULE_AUTHOR("Jelle van der Waa <jelle@vdwaa.nl>");
257MODULE_DESCRIPTION("ZEITEC zet622x I2C touchscreen driver");
258MODULE_LICENSE("GPL");
1/*
2 * Copyright (C) 2016, Jelle van der Waa <jelle@vdwaa.nl>
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the Free
6 * Software Foundation; either version 2 of the License, or (at your option)
7 * any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 */
14
15#include <linux/delay.h>
16#include <linux/i2c.h>
17#include <linux/input.h>
18#include <linux/input/mt.h>
19#include <linux/input/touchscreen.h>
20#include <linux/interrupt.h>
21#include <linux/module.h>
22#include <linux/regulator/consumer.h>
23#include <asm/unaligned.h>
24
25#define ZET6223_MAX_FINGERS 16
26#define ZET6223_MAX_PKT_SIZE (3 + 4 * ZET6223_MAX_FINGERS)
27
28#define ZET6223_CMD_INFO 0xB2
29#define ZET6223_CMD_INFO_LENGTH 17
30#define ZET6223_VALID_PACKET 0x3c
31
32#define ZET6223_POWER_ON_DELAY_MSEC 30
33
34struct zet6223_ts {
35 struct i2c_client *client;
36 struct input_dev *input;
37 struct regulator *vcc;
38 struct regulator *vio;
39 struct touchscreen_properties prop;
40 struct regulator_bulk_data supplies[2];
41 u16 max_x;
42 u16 max_y;
43 u8 fingernum;
44};
45
46static int zet6223_start(struct input_dev *dev)
47{
48 struct zet6223_ts *ts = input_get_drvdata(dev);
49
50 enable_irq(ts->client->irq);
51
52 return 0;
53}
54
55static void zet6223_stop(struct input_dev *dev)
56{
57 struct zet6223_ts *ts = input_get_drvdata(dev);
58
59 disable_irq(ts->client->irq);
60}
61
62static irqreturn_t zet6223_irq(int irq, void *dev_id)
63{
64 struct zet6223_ts *ts = dev_id;
65 u16 finger_bits;
66
67 /*
68 * First 3 bytes are an identifier, two bytes of finger data.
69 * X, Y data per finger is 4 bytes.
70 */
71 u8 bufsize = 3 + 4 * ts->fingernum;
72 u8 buf[ZET6223_MAX_PKT_SIZE];
73 int i;
74 int ret;
75 int error;
76
77 ret = i2c_master_recv(ts->client, buf, bufsize);
78 if (ret != bufsize) {
79 error = ret < 0 ? ret : -EIO;
80 dev_err_ratelimited(&ts->client->dev,
81 "Error reading input data: %d\n", error);
82 return IRQ_HANDLED;
83 }
84
85 if (buf[0] != ZET6223_VALID_PACKET)
86 return IRQ_HANDLED;
87
88 finger_bits = get_unaligned_be16(buf + 1);
89 for (i = 0; i < ts->fingernum; i++) {
90 if (!(finger_bits & BIT(15 - i)))
91 continue;
92
93 input_mt_slot(ts->input, i);
94 input_mt_report_slot_state(ts->input, MT_TOOL_FINGER, true);
95 input_event(ts->input, EV_ABS, ABS_MT_POSITION_X,
96 ((buf[i + 3] >> 4) << 8) + buf[i + 4]);
97 input_event(ts->input, EV_ABS, ABS_MT_POSITION_Y,
98 ((buf[i + 3] & 0xF) << 8) + buf[i + 5]);
99 }
100
101 input_mt_sync_frame(ts->input);
102 input_sync(ts->input);
103
104 return IRQ_HANDLED;
105}
106
107static void zet6223_power_off(void *_ts)
108{
109 struct zet6223_ts *ts = _ts;
110
111 regulator_bulk_disable(ARRAY_SIZE(ts->supplies), ts->supplies);
112}
113
114static int zet6223_power_on(struct zet6223_ts *ts)
115{
116 struct device *dev = &ts->client->dev;
117 int error;
118
119 ts->supplies[0].supply = "vio";
120 ts->supplies[1].supply = "vcc";
121
122 error = devm_regulator_bulk_get(dev, ARRAY_SIZE(ts->supplies),
123 ts->supplies);
124 if (error)
125 return error;
126
127 error = regulator_bulk_enable(ARRAY_SIZE(ts->supplies), ts->supplies);
128 if (error)
129 return error;
130
131 msleep(ZET6223_POWER_ON_DELAY_MSEC);
132
133 error = devm_add_action_or_reset(dev, zet6223_power_off, ts);
134 if (error) {
135 dev_err(dev, "failed to install poweroff action: %d\n", error);
136 return error;
137 }
138
139 return 0;
140}
141
142static int zet6223_query_device(struct zet6223_ts *ts)
143{
144 u8 buf[ZET6223_CMD_INFO_LENGTH];
145 u8 cmd = ZET6223_CMD_INFO;
146 int ret;
147 int error;
148
149 ret = i2c_master_send(ts->client, &cmd, sizeof(cmd));
150 if (ret != sizeof(cmd)) {
151 error = ret < 0 ? ret : -EIO;
152 dev_err(&ts->client->dev,
153 "touchpanel info cmd failed: %d\n", error);
154 return error;
155 }
156
157 ret = i2c_master_recv(ts->client, buf, sizeof(buf));
158 if (ret != sizeof(buf)) {
159 error = ret < 0 ? ret : -EIO;
160 dev_err(&ts->client->dev,
161 "failed to retrieve touchpanel info: %d\n", error);
162 return error;
163 }
164
165 ts->fingernum = buf[15] & 0x7F;
166 if (ts->fingernum > ZET6223_MAX_FINGERS) {
167 dev_warn(&ts->client->dev,
168 "touchpanel reports %d fingers, limiting to %d\n",
169 ts->fingernum, ZET6223_MAX_FINGERS);
170 ts->fingernum = ZET6223_MAX_FINGERS;
171 }
172
173 ts->max_x = get_unaligned_le16(&buf[8]);
174 ts->max_y = get_unaligned_le16(&buf[10]);
175
176 return 0;
177}
178
179static int zet6223_probe(struct i2c_client *client,
180 const struct i2c_device_id *id)
181{
182 struct device *dev = &client->dev;
183 struct zet6223_ts *ts;
184 struct input_dev *input;
185 int error;
186
187 if (!client->irq) {
188 dev_err(dev, "no irq specified\n");
189 return -EINVAL;
190 }
191
192 ts = devm_kzalloc(dev, sizeof(*ts), GFP_KERNEL);
193 if (!ts)
194 return -ENOMEM;
195
196 ts->client = client;
197
198 error = zet6223_power_on(ts);
199 if (error)
200 return error;
201
202 error = zet6223_query_device(ts);
203 if (error)
204 return error;
205
206 ts->input = input = devm_input_allocate_device(dev);
207 if (!input)
208 return -ENOMEM;
209
210 input_set_drvdata(input, ts);
211
212 input->name = client->name;
213 input->id.bustype = BUS_I2C;
214 input->open = zet6223_start;
215 input->close = zet6223_stop;
216
217 input_set_abs_params(input, ABS_MT_POSITION_X, 0, ts->max_x, 0, 0);
218 input_set_abs_params(input, ABS_MT_POSITION_Y, 0, ts->max_y, 0, 0);
219
220 touchscreen_parse_properties(input, true, &ts->prop);
221
222 error = input_mt_init_slots(input, ts->fingernum,
223 INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED);
224 if (error)
225 return error;
226
227 error = devm_request_threaded_irq(dev, client->irq, NULL, zet6223_irq,
228 IRQF_ONESHOT, client->name, ts);
229 if (error) {
230 dev_err(dev, "failed to request irq %d: %d\n",
231 client->irq, error);
232 return error;
233 }
234
235 zet6223_stop(input);
236
237 error = input_register_device(input);
238 if (error)
239 return error;
240
241 return 0;
242}
243
244static const struct of_device_id zet6223_of_match[] = {
245 { .compatible = "zeitec,zet6223" },
246 { }
247};
248MODULE_DEVICE_TABLE(of, zet6223_of_match);
249
250static const struct i2c_device_id zet6223_id[] = {
251 { "zet6223", 0},
252 { }
253};
254MODULE_DEVICE_TABLE(i2c, zet6223_id);
255
256static struct i2c_driver zet6223_driver = {
257 .driver = {
258 .name = "zet6223",
259 .of_match_table = zet6223_of_match,
260 },
261 .probe = zet6223_probe,
262 .id_table = zet6223_id
263};
264module_i2c_driver(zet6223_driver);
265
266MODULE_AUTHOR("Jelle van der Waa <jelle@vdwaa.nl>");
267MODULE_DESCRIPTION("ZEITEC zet622x I2C touchscreen driver");
268MODULE_LICENSE("GPL");