1
2
3
4
5
6
7
8
9
10
11
12#include <linux/delay.h>
13#include <linux/gpio/consumer.h>
14#include <linux/i2c.h>
15#include <linux/input.h>
16#include <linux/input/touchscreen.h>
17#include <linux/interrupt.h>
18#include <linux/irq.h>
19#include <linux/module.h>
20#include <linux/regulator/consumer.h>
21#include <linux/timer.h>
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39#define BU21029_HWID_REG (0x0E << 3)
40#define SUPPORTED_HWID 0x0229
41
42
43
44
45
46
47
48
49
50
51
52
53
54#define BU21029_CFR0_REG (0x00 << 3)
55#define CFR0_VALUE 0x00
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70#define BU21029_CFR1_REG (0x01 << 3)
71#define CFR1_VALUE 0xA6
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86#define BU21029_CFR2_REG (0x02 << 3)
87#define CFR2_VALUE 0xC9
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107#define BU21029_CFR3_REG (0x0B << 3)
108#define CFR3_VALUE 0x42
109
110
111
112
113
114
115
116
117
118
119
120#define BU21029_LDO_REG (0x0C << 3)
121#define LDO_VALUE 0x77
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137#define BU21029_AUTOSCAN 0x80
138
139
140
141
142
143
144
145#define PEN_UP_TIMEOUT_MS 50
146
147#define STOP_DELAY_MIN_US 50
148#define STOP_DELAY_MAX_US 1000
149#define START_DELAY_MS 2
150#define BUF_LEN 8
151#define SCALE_12BIT (1 << 12)
152#define MAX_12BIT ((1 << 12) - 1)
153#define DRIVER_NAME "bu21029"
154
155struct bu21029_ts_data {
156 struct i2c_client *client;
157 struct input_dev *in_dev;
158 struct timer_list timer;
159 struct regulator *vdd;
160 struct gpio_desc *reset_gpios;
161 u32 x_plate_ohms;
162 struct touchscreen_properties prop;
163};
164
165static void bu21029_touch_report(struct bu21029_ts_data *bu21029, const u8 *buf)
166{
167 u16 x, y, z1, z2;
168 u32 rz;
169 s32 max_pressure = input_abs_get_max(bu21029->in_dev, ABS_PRESSURE);
170
171
172
173
174
175
176
177
178
179
180
181 x = (buf[0] << 4) | (buf[1] >> 4);
182 y = (buf[2] << 4) | (buf[3] >> 4);
183 z1 = (buf[4] << 4) | (buf[5] >> 4);
184 z2 = (buf[6] << 4) | (buf[7] >> 4);
185
186 if (z1 && z2) {
187
188
189
190
191
192
193
194 rz = z2 - z1;
195 rz *= x;
196 rz *= bu21029->x_plate_ohms;
197 rz /= z1;
198 rz = DIV_ROUND_CLOSEST(rz, SCALE_12BIT);
199 if (rz <= max_pressure) {
200 touchscreen_report_pos(bu21029->in_dev, &bu21029->prop,
201 x, y, false);
202 input_report_abs(bu21029->in_dev, ABS_PRESSURE,
203 max_pressure - rz);
204 input_report_key(bu21029->in_dev, BTN_TOUCH, 1);
205 input_sync(bu21029->in_dev);
206 }
207 }
208}
209
210static void bu21029_touch_release(struct timer_list *t)
211{
212 struct bu21029_ts_data *bu21029 = from_timer(bu21029, t, timer);
213
214 input_report_abs(bu21029->in_dev, ABS_PRESSURE, 0);
215 input_report_key(bu21029->in_dev, BTN_TOUCH, 0);
216 input_sync(bu21029->in_dev);
217}
218
219static irqreturn_t bu21029_touch_soft_irq(int irq, void *data)
220{
221 struct bu21029_ts_data *bu21029 = data;
222 u8 buf[BUF_LEN];
223 int error;
224
225
226
227
228
229 error = i2c_smbus_read_i2c_block_data(bu21029->client, BU21029_AUTOSCAN,
230 sizeof(buf), buf);
231 if (error < 0)
232 goto out;
233
234 bu21029_touch_report(bu21029, buf);
235
236
237 mod_timer(&bu21029->timer,
238 jiffies + msecs_to_jiffies(PEN_UP_TIMEOUT_MS));
239
240out:
241 return IRQ_HANDLED;
242}
243
244static void bu21029_put_chip_in_reset(struct bu21029_ts_data *bu21029)
245{
246 if (bu21029->reset_gpios) {
247 gpiod_set_value_cansleep(bu21029->reset_gpios, 1);
248 usleep_range(STOP_DELAY_MIN_US, STOP_DELAY_MAX_US);
249 }
250}
251
252static int bu21029_start_chip(struct input_dev *dev)
253{
254 struct bu21029_ts_data *bu21029 = input_get_drvdata(dev);
255 struct i2c_client *i2c = bu21029->client;
256 struct {
257 u8 reg;
258 u8 value;
259 } init_table[] = {
260 {BU21029_CFR0_REG, CFR0_VALUE},
261 {BU21029_CFR1_REG, CFR1_VALUE},
262 {BU21029_CFR2_REG, CFR2_VALUE},
263 {BU21029_CFR3_REG, CFR3_VALUE},
264 {BU21029_LDO_REG, LDO_VALUE}
265 };
266 int error, i;
267 __be16 hwid;
268
269 error = regulator_enable(bu21029->vdd);
270 if (error) {
271 dev_err(&i2c->dev, "failed to power up chip: %d", error);
272 return error;
273 }
274
275
276 if (bu21029->reset_gpios) {
277 gpiod_set_value_cansleep(bu21029->reset_gpios, 0);
278 msleep(START_DELAY_MS);
279 }
280
281 error = i2c_smbus_read_i2c_block_data(i2c, BU21029_HWID_REG,
282 sizeof(hwid), (u8 *)&hwid);
283 if (error < 0) {
284 dev_err(&i2c->dev, "failed to read HW ID\n");
285 goto err_out;
286 }
287
288 if (be16_to_cpu(hwid) != SUPPORTED_HWID) {
289 dev_err(&i2c->dev,
290 "unsupported HW ID 0x%x\n", be16_to_cpu(hwid));
291 error = -ENODEV;
292 goto err_out;
293 }
294
295 for (i = 0; i < ARRAY_SIZE(init_table); ++i) {
296 error = i2c_smbus_write_byte_data(i2c,
297 init_table[i].reg,
298 init_table[i].value);
299 if (error < 0) {
300 dev_err(&i2c->dev,
301 "failed to write %#02x to register %#02x: %d\n",
302 init_table[i].value, init_table[i].reg,
303 error);
304 goto err_out;
305 }
306 }
307
308 error = i2c_smbus_write_byte(i2c, BU21029_AUTOSCAN);
309 if (error < 0) {
310 dev_err(&i2c->dev, "failed to start autoscan\n");
311 goto err_out;
312 }
313
314 enable_irq(bu21029->client->irq);
315 return 0;
316
317err_out:
318 bu21029_put_chip_in_reset(bu21029);
319 regulator_disable(bu21029->vdd);
320 return error;
321}
322
323static void bu21029_stop_chip(struct input_dev *dev)
324{
325 struct bu21029_ts_data *bu21029 = input_get_drvdata(dev);
326
327 disable_irq(bu21029->client->irq);
328 del_timer_sync(&bu21029->timer);
329
330 bu21029_put_chip_in_reset(bu21029);
331 regulator_disable(bu21029->vdd);
332}
333
334static int bu21029_probe(struct i2c_client *client,
335 const struct i2c_device_id *id)
336{
337 struct bu21029_ts_data *bu21029;
338 struct input_dev *in_dev;
339 int error;
340
341 if (!i2c_check_functionality(client->adapter,
342 I2C_FUNC_SMBUS_WRITE_BYTE |
343 I2C_FUNC_SMBUS_WRITE_BYTE_DATA |
344 I2C_FUNC_SMBUS_READ_I2C_BLOCK)) {
345 dev_err(&client->dev,
346 "i2c functionality support is not sufficient\n");
347 return -EIO;
348 }
349
350 bu21029 = devm_kzalloc(&client->dev, sizeof(*bu21029), GFP_KERNEL);
351 if (!bu21029)
352 return -ENOMEM;
353
354 error = device_property_read_u32(&client->dev, "rohm,x-plate-ohms",
355 &bu21029->x_plate_ohms);
356 if (error) {
357 dev_err(&client->dev,
358 "invalid 'x-plate-ohms' supplied: %d\n", error);
359 return error;
360 }
361
362 bu21029->vdd = devm_regulator_get(&client->dev, "vdd");
363 if (IS_ERR(bu21029->vdd)) {
364 error = PTR_ERR(bu21029->vdd);
365 if (error != -EPROBE_DEFER)
366 dev_err(&client->dev,
367 "failed to acquire 'vdd' supply: %d\n", error);
368 return error;
369 }
370
371 bu21029->reset_gpios = devm_gpiod_get_optional(&client->dev,
372 "reset", GPIOD_OUT_HIGH);
373 if (IS_ERR(bu21029->reset_gpios)) {
374 error = PTR_ERR(bu21029->reset_gpios);
375 if (error != -EPROBE_DEFER)
376 dev_err(&client->dev,
377 "failed to acquire 'reset' gpio: %d\n", error);
378 return error;
379 }
380
381 in_dev = devm_input_allocate_device(&client->dev);
382 if (!in_dev) {
383 dev_err(&client->dev, "unable to allocate input device\n");
384 return -ENOMEM;
385 }
386
387 bu21029->client = client;
388 bu21029->in_dev = in_dev;
389 timer_setup(&bu21029->timer, bu21029_touch_release, 0);
390
391 in_dev->name = DRIVER_NAME;
392 in_dev->id.bustype = BUS_I2C;
393 in_dev->open = bu21029_start_chip;
394 in_dev->close = bu21029_stop_chip;
395
396 input_set_capability(in_dev, EV_KEY, BTN_TOUCH);
397 input_set_abs_params(in_dev, ABS_X, 0, MAX_12BIT, 0, 0);
398 input_set_abs_params(in_dev, ABS_Y, 0, MAX_12BIT, 0, 0);
399 input_set_abs_params(in_dev, ABS_PRESSURE, 0, MAX_12BIT, 0, 0);
400 touchscreen_parse_properties(in_dev, false, &bu21029->prop);
401
402 input_set_drvdata(in_dev, bu21029);
403
404 irq_set_status_flags(client->irq, IRQ_NOAUTOEN);
405 error = devm_request_threaded_irq(&client->dev, client->irq,
406 NULL, bu21029_touch_soft_irq,
407 IRQF_ONESHOT, DRIVER_NAME, bu21029);
408 if (error) {
409 dev_err(&client->dev,
410 "unable to request touch irq: %d\n", error);
411 return error;
412 }
413
414 error = input_register_device(in_dev);
415 if (error) {
416 dev_err(&client->dev,
417 "unable to register input device: %d\n", error);
418 return error;
419 }
420
421 i2c_set_clientdata(client, bu21029);
422
423 return 0;
424}
425
426static int __maybe_unused bu21029_suspend(struct device *dev)
427{
428 struct i2c_client *i2c = to_i2c_client(dev);
429 struct bu21029_ts_data *bu21029 = i2c_get_clientdata(i2c);
430
431 if (!device_may_wakeup(dev)) {
432 mutex_lock(&bu21029->in_dev->mutex);
433 if (bu21029->in_dev->users)
434 bu21029_stop_chip(bu21029->in_dev);
435 mutex_unlock(&bu21029->in_dev->mutex);
436 }
437
438 return 0;
439}
440
441static int __maybe_unused bu21029_resume(struct device *dev)
442{
443 struct i2c_client *i2c = to_i2c_client(dev);
444 struct bu21029_ts_data *bu21029 = i2c_get_clientdata(i2c);
445
446 if (!device_may_wakeup(dev)) {
447 mutex_lock(&bu21029->in_dev->mutex);
448 if (bu21029->in_dev->users)
449 bu21029_start_chip(bu21029->in_dev);
450 mutex_unlock(&bu21029->in_dev->mutex);
451 }
452
453 return 0;
454}
455static SIMPLE_DEV_PM_OPS(bu21029_pm_ops, bu21029_suspend, bu21029_resume);
456
457static const struct i2c_device_id bu21029_ids[] = {
458 { DRIVER_NAME, 0 },
459 { }
460};
461MODULE_DEVICE_TABLE(i2c, bu21029_ids);
462
463#ifdef CONFIG_OF
464static const struct of_device_id bu21029_of_ids[] = {
465 { .compatible = "rohm,bu21029" },
466 { }
467};
468MODULE_DEVICE_TABLE(of, bu21029_of_ids);
469#endif
470
471static struct i2c_driver bu21029_driver = {
472 .driver = {
473 .name = DRIVER_NAME,
474 .of_match_table = of_match_ptr(bu21029_of_ids),
475 .pm = &bu21029_pm_ops,
476 },
477 .id_table = bu21029_ids,
478 .probe = bu21029_probe,
479};
480module_i2c_driver(bu21029_driver);
481
482MODULE_AUTHOR("Zhu Yi <yi.zhu5@cn.bosch.com>");
483MODULE_DESCRIPTION("Rohm BU21029 touchscreen controller driver");
484MODULE_LICENSE("GPL v2");
485