Loading...
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Copyright (C) ST-Ericsson SA 2010
4 * Author: Naveen Kumar G <naveen.gaddipati@stericsson.com> for ST-Ericsson
5 */
6
7#include <linux/bitops.h>
8#include <linux/delay.h>
9#include <linux/gpio/consumer.h>
10#include <linux/i2c.h>
11#include <linux/input.h>
12#include <linux/input/mt.h>
13#include <linux/input/touchscreen.h>
14#include <linux/interrupt.h>
15#include <linux/kernel.h>
16#include <linux/module.h>
17#include <linux/property.h>
18#include <linux/regulator/consumer.h>
19#include <linux/slab.h>
20#include <linux/types.h>
21
22#define MAX_FINGERS 2
23#define RESET_DELAY 30
24#define PENUP_TIMEOUT (10)
25#define DELTA_MIN 16
26#define MASK_BITS 0x03
27#define SHIFT_8 8
28#define SHIFT_2 2
29#define LENGTH_OF_BUFFER 11
30#define I2C_RETRY_COUNT 5
31
32#define BU21013_SENSORS_BTN_0_7_REG 0x70
33#define BU21013_SENSORS_BTN_8_15_REG 0x71
34#define BU21013_SENSORS_BTN_16_23_REG 0x72
35#define BU21013_X1_POS_MSB_REG 0x73
36#define BU21013_X1_POS_LSB_REG 0x74
37#define BU21013_Y1_POS_MSB_REG 0x75
38#define BU21013_Y1_POS_LSB_REG 0x76
39#define BU21013_X2_POS_MSB_REG 0x77
40#define BU21013_X2_POS_LSB_REG 0x78
41#define BU21013_Y2_POS_MSB_REG 0x79
42#define BU21013_Y2_POS_LSB_REG 0x7A
43#define BU21013_INT_CLR_REG 0xE8
44#define BU21013_INT_MODE_REG 0xE9
45#define BU21013_GAIN_REG 0xEA
46#define BU21013_OFFSET_MODE_REG 0xEB
47#define BU21013_XY_EDGE_REG 0xEC
48#define BU21013_RESET_REG 0xED
49#define BU21013_CALIB_REG 0xEE
50#define BU21013_DONE_REG 0xEF
51#define BU21013_SENSOR_0_7_REG 0xF0
52#define BU21013_SENSOR_8_15_REG 0xF1
53#define BU21013_SENSOR_16_23_REG 0xF2
54#define BU21013_POS_MODE1_REG 0xF3
55#define BU21013_POS_MODE2_REG 0xF4
56#define BU21013_CLK_MODE_REG 0xF5
57#define BU21013_IDLE_REG 0xFA
58#define BU21013_FILTER_REG 0xFB
59#define BU21013_TH_ON_REG 0xFC
60#define BU21013_TH_OFF_REG 0xFD
61
62
63#define BU21013_RESET_ENABLE 0x01
64
65#define BU21013_SENSORS_EN_0_7 0x3F
66#define BU21013_SENSORS_EN_8_15 0xFC
67#define BU21013_SENSORS_EN_16_23 0x1F
68
69#define BU21013_POS_MODE1_0 0x02
70#define BU21013_POS_MODE1_1 0x04
71#define BU21013_POS_MODE1_2 0x08
72
73#define BU21013_POS_MODE2_ZERO 0x01
74#define BU21013_POS_MODE2_AVG1 0x02
75#define BU21013_POS_MODE2_AVG2 0x04
76#define BU21013_POS_MODE2_EN_XY 0x08
77#define BU21013_POS_MODE2_EN_RAW 0x10
78#define BU21013_POS_MODE2_MULTI 0x80
79
80#define BU21013_CLK_MODE_DIV 0x01
81#define BU21013_CLK_MODE_EXT 0x02
82#define BU21013_CLK_MODE_CALIB 0x80
83
84#define BU21013_IDLET_0 0x01
85#define BU21013_IDLET_1 0x02
86#define BU21013_IDLET_2 0x04
87#define BU21013_IDLET_3 0x08
88#define BU21013_IDLE_INTERMIT_EN 0x10
89
90#define BU21013_DELTA_0_6 0x7F
91#define BU21013_FILTER_EN 0x80
92
93#define BU21013_INT_MODE_LEVEL 0x00
94#define BU21013_INT_MODE_EDGE 0x01
95
96#define BU21013_GAIN_0 0x01
97#define BU21013_GAIN_1 0x02
98#define BU21013_GAIN_2 0x04
99
100#define BU21013_OFFSET_MODE_DEFAULT 0x00
101#define BU21013_OFFSET_MODE_MOVE 0x01
102#define BU21013_OFFSET_MODE_DISABLE 0x02
103
104#define BU21013_TH_ON_0 0x01
105#define BU21013_TH_ON_1 0x02
106#define BU21013_TH_ON_2 0x04
107#define BU21013_TH_ON_3 0x08
108#define BU21013_TH_ON_4 0x10
109#define BU21013_TH_ON_5 0x20
110#define BU21013_TH_ON_6 0x40
111#define BU21013_TH_ON_7 0x80
112#define BU21013_TH_ON_MAX 0xFF
113
114#define BU21013_TH_OFF_0 0x01
115#define BU21013_TH_OFF_1 0x02
116#define BU21013_TH_OFF_2 0x04
117#define BU21013_TH_OFF_3 0x08
118#define BU21013_TH_OFF_4 0x10
119#define BU21013_TH_OFF_5 0x20
120#define BU21013_TH_OFF_6 0x40
121#define BU21013_TH_OFF_7 0x80
122#define BU21013_TH_OFF_MAX 0xFF
123
124#define BU21013_X_EDGE_0 0x01
125#define BU21013_X_EDGE_1 0x02
126#define BU21013_X_EDGE_2 0x04
127#define BU21013_X_EDGE_3 0x08
128#define BU21013_Y_EDGE_0 0x10
129#define BU21013_Y_EDGE_1 0x20
130#define BU21013_Y_EDGE_2 0x40
131#define BU21013_Y_EDGE_3 0x80
132
133#define BU21013_DONE 0x01
134#define BU21013_NUMBER_OF_X_SENSORS (6)
135#define BU21013_NUMBER_OF_Y_SENSORS (11)
136
137#define DRIVER_TP "bu21013_tp"
138
139/**
140 * struct bu21013_ts - touch panel data structure
141 * @client: pointer to the i2c client
142 * @in_dev: pointer to the input device structure
143 * @props: the device coordinate transformation properties
144 * @regulator: pointer to the Regulator used for touch screen
145 * @cs_gpiod: chip select GPIO line
146 * @int_gpiod: touch interrupt GPIO line
147 * @touch_x_max: maximum X coordinate reported by the device
148 * @touch_y_max: maximum Y coordinate reported by the device
149 * @x_flip: indicates that the driver should invert X coordinate before
150 * reporting
151 * @y_flip: indicates that the driver should invert Y coordinate before
152 * reporting
153 * @touch_stopped: touch stop flag
154 *
155 * Touch panel device data structure
156 */
157struct bu21013_ts {
158 struct i2c_client *client;
159 struct input_dev *in_dev;
160 struct touchscreen_properties props;
161 struct regulator *regulator;
162 struct gpio_desc *cs_gpiod;
163 struct gpio_desc *int_gpiod;
164 u32 touch_x_max;
165 u32 touch_y_max;
166 bool x_flip;
167 bool y_flip;
168 bool touch_stopped;
169};
170
171static int bu21013_read_block_data(struct bu21013_ts *ts, u8 *buf)
172{
173 int ret, i;
174
175 for (i = 0; i < I2C_RETRY_COUNT; i++) {
176 ret = i2c_smbus_read_i2c_block_data(ts->client,
177 BU21013_SENSORS_BTN_0_7_REG,
178 LENGTH_OF_BUFFER, buf);
179 if (ret == LENGTH_OF_BUFFER)
180 return 0;
181 }
182
183 return -EINVAL;
184}
185
186static int bu21013_do_touch_report(struct bu21013_ts *ts)
187{
188 struct input_dev *input = ts->in_dev;
189 struct input_mt_pos pos[MAX_FINGERS];
190 int slots[MAX_FINGERS];
191 u8 buf[LENGTH_OF_BUFFER];
192 bool has_x_sensors, has_y_sensors;
193 int finger_down_count = 0;
194 int i;
195
196 if (bu21013_read_block_data(ts, buf) < 0)
197 return -EINVAL;
198
199 has_x_sensors = hweight32(buf[0] & BU21013_SENSORS_EN_0_7);
200 has_y_sensors = hweight32(((buf[1] & BU21013_SENSORS_EN_8_15) |
201 ((buf[2] & BU21013_SENSORS_EN_16_23) << SHIFT_8)) >> SHIFT_2);
202 if (!has_x_sensors || !has_y_sensors)
203 return 0;
204
205 for (i = 0; i < MAX_FINGERS; i++) {
206 const u8 *data = &buf[4 * i + 3];
207 unsigned int x, y;
208
209 x = data[0] << SHIFT_2 | (data[1] & MASK_BITS);
210 y = data[2] << SHIFT_2 | (data[3] & MASK_BITS);
211 if (x != 0 && y != 0)
212 touchscreen_set_mt_pos(&pos[finger_down_count++],
213 &ts->props, x, y);
214 }
215
216 if (finger_down_count == 2 &&
217 (abs(pos[0].x - pos[1].x) < DELTA_MIN ||
218 abs(pos[0].y - pos[1].y) < DELTA_MIN)) {
219 return 0;
220 }
221
222 input_mt_assign_slots(input, slots, pos, finger_down_count, DELTA_MIN);
223 for (i = 0; i < finger_down_count; i++) {
224 input_mt_slot(input, slots[i]);
225 input_mt_report_slot_state(input, MT_TOOL_FINGER, true);
226 input_report_abs(input, ABS_MT_POSITION_X, pos[i].x);
227 input_report_abs(input, ABS_MT_POSITION_Y, pos[i].y);
228 }
229
230 input_mt_sync_frame(input);
231 input_sync(input);
232
233 return 0;
234}
235
236static irqreturn_t bu21013_gpio_irq(int irq, void *device_data)
237{
238 struct bu21013_ts *ts = device_data;
239 int keep_polling;
240 int error;
241
242 do {
243 error = bu21013_do_touch_report(ts);
244 if (error) {
245 dev_err(&ts->client->dev, "%s failed\n", __func__);
246 break;
247 }
248
249 if (unlikely(ts->touch_stopped))
250 break;
251
252 keep_polling = ts->int_gpiod ?
253 gpiod_get_value(ts->int_gpiod) : false;
254 if (keep_polling)
255 usleep_range(2000, 2500);
256 } while (keep_polling);
257
258 return IRQ_HANDLED;
259}
260
261static int bu21013_init_chip(struct bu21013_ts *ts)
262{
263 struct i2c_client *client = ts->client;
264 int error;
265
266 error = i2c_smbus_write_byte_data(client, BU21013_RESET_REG,
267 BU21013_RESET_ENABLE);
268 if (error) {
269 dev_err(&client->dev, "BU21013_RESET reg write failed\n");
270 return error;
271 }
272 msleep(RESET_DELAY);
273
274 error = i2c_smbus_write_byte_data(client, BU21013_SENSOR_0_7_REG,
275 BU21013_SENSORS_EN_0_7);
276 if (error) {
277 dev_err(&client->dev, "BU21013_SENSOR_0_7 reg write failed\n");
278 return error;
279 }
280
281 error = i2c_smbus_write_byte_data(client, BU21013_SENSOR_8_15_REG,
282 BU21013_SENSORS_EN_8_15);
283 if (error) {
284 dev_err(&client->dev, "BU21013_SENSOR_8_15 reg write failed\n");
285 return error;
286 }
287
288 error = i2c_smbus_write_byte_data(client, BU21013_SENSOR_16_23_REG,
289 BU21013_SENSORS_EN_16_23);
290 if (error) {
291 dev_err(&client->dev, "BU21013_SENSOR_16_23 reg write failed\n");
292 return error;
293 }
294
295 error = i2c_smbus_write_byte_data(client, BU21013_POS_MODE1_REG,
296 BU21013_POS_MODE1_0 |
297 BU21013_POS_MODE1_1);
298 if (error) {
299 dev_err(&client->dev, "BU21013_POS_MODE1 reg write failed\n");
300 return error;
301 }
302
303 error = i2c_smbus_write_byte_data(client, BU21013_POS_MODE2_REG,
304 BU21013_POS_MODE2_ZERO |
305 BU21013_POS_MODE2_AVG1 |
306 BU21013_POS_MODE2_AVG2 |
307 BU21013_POS_MODE2_EN_RAW |
308 BU21013_POS_MODE2_MULTI);
309 if (error) {
310 dev_err(&client->dev, "BU21013_POS_MODE2 reg write failed\n");
311 return error;
312 }
313
314 error = i2c_smbus_write_byte_data(client, BU21013_CLK_MODE_REG,
315 BU21013_CLK_MODE_DIV |
316 BU21013_CLK_MODE_CALIB);
317 if (error) {
318 dev_err(&client->dev, "BU21013_CLK_MODE reg write failed\n");
319 return error;
320 }
321
322 error = i2c_smbus_write_byte_data(client, BU21013_IDLE_REG,
323 BU21013_IDLET_0 |
324 BU21013_IDLE_INTERMIT_EN);
325 if (error) {
326 dev_err(&client->dev, "BU21013_IDLE reg write failed\n");
327 return error;
328 }
329
330 error = i2c_smbus_write_byte_data(client, BU21013_INT_MODE_REG,
331 BU21013_INT_MODE_LEVEL);
332 if (error) {
333 dev_err(&client->dev, "BU21013_INT_MODE reg write failed\n");
334 return error;
335 }
336
337 error = i2c_smbus_write_byte_data(client, BU21013_FILTER_REG,
338 BU21013_DELTA_0_6 |
339 BU21013_FILTER_EN);
340 if (error) {
341 dev_err(&client->dev, "BU21013_FILTER reg write failed\n");
342 return error;
343 }
344
345 error = i2c_smbus_write_byte_data(client, BU21013_TH_ON_REG,
346 BU21013_TH_ON_5);
347 if (error) {
348 dev_err(&client->dev, "BU21013_TH_ON reg write failed\n");
349 return error;
350 }
351
352 error = i2c_smbus_write_byte_data(client, BU21013_TH_OFF_REG,
353 BU21013_TH_OFF_4 | BU21013_TH_OFF_3);
354 if (error) {
355 dev_err(&client->dev, "BU21013_TH_OFF reg write failed\n");
356 return error;
357 }
358
359 error = i2c_smbus_write_byte_data(client, BU21013_GAIN_REG,
360 BU21013_GAIN_0 | BU21013_GAIN_1);
361 if (error) {
362 dev_err(&client->dev, "BU21013_GAIN reg write failed\n");
363 return error;
364 }
365
366 error = i2c_smbus_write_byte_data(client, BU21013_OFFSET_MODE_REG,
367 BU21013_OFFSET_MODE_DEFAULT);
368 if (error) {
369 dev_err(&client->dev, "BU21013_OFFSET_MODE reg write failed\n");
370 return error;
371 }
372
373 error = i2c_smbus_write_byte_data(client, BU21013_XY_EDGE_REG,
374 BU21013_X_EDGE_0 |
375 BU21013_X_EDGE_2 |
376 BU21013_Y_EDGE_1 |
377 BU21013_Y_EDGE_3);
378 if (error) {
379 dev_err(&client->dev, "BU21013_XY_EDGE reg write failed\n");
380 return error;
381 }
382
383 error = i2c_smbus_write_byte_data(client, BU21013_DONE_REG,
384 BU21013_DONE);
385 if (error) {
386 dev_err(&client->dev, "BU21013_REG_DONE reg write failed\n");
387 return error;
388 }
389
390 return 0;
391}
392
393static void bu21013_power_off(void *_ts)
394{
395 struct bu21013_ts *ts = _ts;
396
397 regulator_disable(ts->regulator);
398}
399
400static void bu21013_disable_chip(void *_ts)
401{
402 struct bu21013_ts *ts = _ts;
403
404 gpiod_set_value(ts->cs_gpiod, 0);
405}
406
407static int bu21013_probe(struct i2c_client *client)
408{
409 struct bu21013_ts *ts;
410 struct input_dev *in_dev;
411 struct input_absinfo *info;
412 u32 max_x = 0, max_y = 0;
413 int error;
414
415 if (!i2c_check_functionality(client->adapter,
416 I2C_FUNC_SMBUS_BYTE_DATA)) {
417 dev_err(&client->dev, "i2c smbus byte data not supported\n");
418 return -EIO;
419 }
420
421 if (!client->irq) {
422 dev_err(&client->dev, "No IRQ set up\n");
423 return -EINVAL;
424 }
425
426 ts = devm_kzalloc(&client->dev, sizeof(*ts), GFP_KERNEL);
427 if (!ts)
428 return -ENOMEM;
429
430 ts->client = client;
431
432 ts->x_flip = device_property_read_bool(&client->dev, "rohm,flip-x");
433 ts->y_flip = device_property_read_bool(&client->dev, "rohm,flip-y");
434
435 in_dev = devm_input_allocate_device(&client->dev);
436 if (!in_dev) {
437 dev_err(&client->dev, "device memory alloc failed\n");
438 return -ENOMEM;
439 }
440 ts->in_dev = in_dev;
441 input_set_drvdata(in_dev, ts);
442
443 /* register the device to input subsystem */
444 in_dev->name = DRIVER_TP;
445 in_dev->id.bustype = BUS_I2C;
446
447 device_property_read_u32(&client->dev, "rohm,touch-max-x", &max_x);
448 device_property_read_u32(&client->dev, "rohm,touch-max-y", &max_y);
449
450 input_set_abs_params(in_dev, ABS_MT_POSITION_X, 0, max_x, 0, 0);
451 input_set_abs_params(in_dev, ABS_MT_POSITION_Y, 0, max_y, 0, 0);
452
453 touchscreen_parse_properties(in_dev, true, &ts->props);
454
455 /* Adjust for the legacy "flip" properties, if present */
456 if (!ts->props.invert_x &&
457 device_property_read_bool(&client->dev, "rohm,flip-x")) {
458 info = &in_dev->absinfo[ABS_MT_POSITION_X];
459 info->maximum -= info->minimum;
460 info->minimum = 0;
461 }
462
463 if (!ts->props.invert_y &&
464 device_property_read_bool(&client->dev, "rohm,flip-y")) {
465 info = &in_dev->absinfo[ABS_MT_POSITION_Y];
466 info->maximum -= info->minimum;
467 info->minimum = 0;
468 }
469
470 error = input_mt_init_slots(in_dev, MAX_FINGERS,
471 INPUT_MT_DIRECT | INPUT_MT_TRACK |
472 INPUT_MT_DROP_UNUSED);
473 if (error) {
474 dev_err(&client->dev, "failed to initialize MT slots");
475 return error;
476 }
477
478 ts->regulator = devm_regulator_get(&client->dev, "avdd");
479 if (IS_ERR(ts->regulator)) {
480 dev_err(&client->dev, "regulator_get failed\n");
481 return PTR_ERR(ts->regulator);
482 }
483
484 error = regulator_enable(ts->regulator);
485 if (error) {
486 dev_err(&client->dev, "regulator enable failed\n");
487 return error;
488 }
489
490 error = devm_add_action_or_reset(&client->dev, bu21013_power_off, ts);
491 if (error) {
492 dev_err(&client->dev, "failed to install power off handler\n");
493 return error;
494 }
495
496 /* Named "CS" on the chip, DT binding is "reset" */
497 ts->cs_gpiod = devm_gpiod_get(&client->dev, "reset", GPIOD_OUT_HIGH);
498 error = PTR_ERR_OR_ZERO(ts->cs_gpiod);
499 if (error) {
500 if (error != -EPROBE_DEFER)
501 dev_err(&client->dev, "failed to get CS GPIO\n");
502 return error;
503 }
504 gpiod_set_consumer_name(ts->cs_gpiod, "BU21013 CS");
505
506 error = devm_add_action_or_reset(&client->dev,
507 bu21013_disable_chip, ts);
508 if (error) {
509 dev_err(&client->dev,
510 "failed to install chip disable handler\n");
511 return error;
512 }
513
514 /* Named "INT" on the chip, DT binding is "touch" */
515 ts->int_gpiod = devm_gpiod_get_optional(&client->dev,
516 "touch", GPIOD_IN);
517 error = PTR_ERR_OR_ZERO(ts->int_gpiod);
518 if (error) {
519 if (error != -EPROBE_DEFER)
520 dev_err(&client->dev, "failed to get INT GPIO\n");
521 return error;
522 }
523
524 if (ts->int_gpiod)
525 gpiod_set_consumer_name(ts->int_gpiod, "BU21013 INT");
526
527 /* configure the touch panel controller */
528 error = bu21013_init_chip(ts);
529 if (error) {
530 dev_err(&client->dev, "error in bu21013 config\n");
531 return error;
532 }
533
534 error = devm_request_threaded_irq(&client->dev, client->irq,
535 NULL, bu21013_gpio_irq,
536 IRQF_ONESHOT, DRIVER_TP, ts);
537 if (error) {
538 dev_err(&client->dev, "request irq %d failed\n",
539 client->irq);
540 return error;
541 }
542
543 error = input_register_device(in_dev);
544 if (error) {
545 dev_err(&client->dev, "failed to register input device\n");
546 return error;
547 }
548
549 i2c_set_clientdata(client, ts);
550
551 return 0;
552}
553
554static void bu21013_remove(struct i2c_client *client)
555{
556 struct bu21013_ts *ts = i2c_get_clientdata(client);
557
558 /* Make sure IRQ will exit quickly even if there is contact */
559 ts->touch_stopped = true;
560 /* The resources will be freed by devm */
561}
562
563static int __maybe_unused bu21013_suspend(struct device *dev)
564{
565 struct i2c_client *client = to_i2c_client(dev);
566 struct bu21013_ts *ts = i2c_get_clientdata(client);
567
568 ts->touch_stopped = true;
569 mb();
570 disable_irq(client->irq);
571
572 if (!device_may_wakeup(&client->dev))
573 regulator_disable(ts->regulator);
574
575 return 0;
576}
577
578static int __maybe_unused bu21013_resume(struct device *dev)
579{
580 struct i2c_client *client = to_i2c_client(dev);
581 struct bu21013_ts *ts = i2c_get_clientdata(client);
582 int error;
583
584 if (!device_may_wakeup(&client->dev)) {
585 error = regulator_enable(ts->regulator);
586 if (error) {
587 dev_err(&client->dev,
588 "failed to re-enable regulator when resuming\n");
589 return error;
590 }
591
592 error = bu21013_init_chip(ts);
593 if (error) {
594 dev_err(&client->dev,
595 "failed to reinitialize chip when resuming\n");
596 return error;
597 }
598 }
599
600 ts->touch_stopped = false;
601 mb();
602 enable_irq(client->irq);
603
604 return 0;
605}
606
607static SIMPLE_DEV_PM_OPS(bu21013_dev_pm_ops, bu21013_suspend, bu21013_resume);
608
609static const struct i2c_device_id bu21013_id[] = {
610 { DRIVER_TP, 0 },
611 { }
612};
613MODULE_DEVICE_TABLE(i2c, bu21013_id);
614
615static struct i2c_driver bu21013_driver = {
616 .driver = {
617 .name = DRIVER_TP,
618 .pm = &bu21013_dev_pm_ops,
619 },
620 .probe_new = bu21013_probe,
621 .remove = bu21013_remove,
622 .id_table = bu21013_id,
623};
624
625module_i2c_driver(bu21013_driver);
626
627MODULE_LICENSE("GPL v2");
628MODULE_AUTHOR("Naveen Kumar G <naveen.gaddipati@stericsson.com>");
629MODULE_DESCRIPTION("bu21013 touch screen controller driver");
1/*
2 * Copyright (C) ST-Ericsson SA 2010
3 * Author: Naveen Kumar G <naveen.gaddipati@stericsson.com> for ST-Ericsson
4 * License terms:GNU General Public License (GPL) version 2
5 */
6
7#include <linux/kernel.h>
8#include <linux/delay.h>
9#include <linux/interrupt.h>
10#include <linux/i2c.h>
11#include <linux/workqueue.h>
12#include <linux/input.h>
13#include <linux/input/bu21013.h>
14#include <linux/slab.h>
15#include <linux/regulator/consumer.h>
16#include <linux/module.h>
17
18#define PEN_DOWN_INTR 0
19#define MAX_FINGERS 2
20#define RESET_DELAY 30
21#define PENUP_TIMEOUT (10)
22#define DELTA_MIN 16
23#define MASK_BITS 0x03
24#define SHIFT_8 8
25#define SHIFT_2 2
26#define LENGTH_OF_BUFFER 11
27#define I2C_RETRY_COUNT 5
28
29#define BU21013_SENSORS_BTN_0_7_REG 0x70
30#define BU21013_SENSORS_BTN_8_15_REG 0x71
31#define BU21013_SENSORS_BTN_16_23_REG 0x72
32#define BU21013_X1_POS_MSB_REG 0x73
33#define BU21013_X1_POS_LSB_REG 0x74
34#define BU21013_Y1_POS_MSB_REG 0x75
35#define BU21013_Y1_POS_LSB_REG 0x76
36#define BU21013_X2_POS_MSB_REG 0x77
37#define BU21013_X2_POS_LSB_REG 0x78
38#define BU21013_Y2_POS_MSB_REG 0x79
39#define BU21013_Y2_POS_LSB_REG 0x7A
40#define BU21013_INT_CLR_REG 0xE8
41#define BU21013_INT_MODE_REG 0xE9
42#define BU21013_GAIN_REG 0xEA
43#define BU21013_OFFSET_MODE_REG 0xEB
44#define BU21013_XY_EDGE_REG 0xEC
45#define BU21013_RESET_REG 0xED
46#define BU21013_CALIB_REG 0xEE
47#define BU21013_DONE_REG 0xEF
48#define BU21013_SENSOR_0_7_REG 0xF0
49#define BU21013_SENSOR_8_15_REG 0xF1
50#define BU21013_SENSOR_16_23_REG 0xF2
51#define BU21013_POS_MODE1_REG 0xF3
52#define BU21013_POS_MODE2_REG 0xF4
53#define BU21013_CLK_MODE_REG 0xF5
54#define BU21013_IDLE_REG 0xFA
55#define BU21013_FILTER_REG 0xFB
56#define BU21013_TH_ON_REG 0xFC
57#define BU21013_TH_OFF_REG 0xFD
58
59
60#define BU21013_RESET_ENABLE 0x01
61
62#define BU21013_SENSORS_EN_0_7 0x3F
63#define BU21013_SENSORS_EN_8_15 0xFC
64#define BU21013_SENSORS_EN_16_23 0x1F
65
66#define BU21013_POS_MODE1_0 0x02
67#define BU21013_POS_MODE1_1 0x04
68#define BU21013_POS_MODE1_2 0x08
69
70#define BU21013_POS_MODE2_ZERO 0x01
71#define BU21013_POS_MODE2_AVG1 0x02
72#define BU21013_POS_MODE2_AVG2 0x04
73#define BU21013_POS_MODE2_EN_XY 0x08
74#define BU21013_POS_MODE2_EN_RAW 0x10
75#define BU21013_POS_MODE2_MULTI 0x80
76
77#define BU21013_CLK_MODE_DIV 0x01
78#define BU21013_CLK_MODE_EXT 0x02
79#define BU21013_CLK_MODE_CALIB 0x80
80
81#define BU21013_IDLET_0 0x01
82#define BU21013_IDLET_1 0x02
83#define BU21013_IDLET_2 0x04
84#define BU21013_IDLET_3 0x08
85#define BU21013_IDLE_INTERMIT_EN 0x10
86
87#define BU21013_DELTA_0_6 0x7F
88#define BU21013_FILTER_EN 0x80
89
90#define BU21013_INT_MODE_LEVEL 0x00
91#define BU21013_INT_MODE_EDGE 0x01
92
93#define BU21013_GAIN_0 0x01
94#define BU21013_GAIN_1 0x02
95#define BU21013_GAIN_2 0x04
96
97#define BU21013_OFFSET_MODE_DEFAULT 0x00
98#define BU21013_OFFSET_MODE_MOVE 0x01
99#define BU21013_OFFSET_MODE_DISABLE 0x02
100
101#define BU21013_TH_ON_0 0x01
102#define BU21013_TH_ON_1 0x02
103#define BU21013_TH_ON_2 0x04
104#define BU21013_TH_ON_3 0x08
105#define BU21013_TH_ON_4 0x10
106#define BU21013_TH_ON_5 0x20
107#define BU21013_TH_ON_6 0x40
108#define BU21013_TH_ON_7 0x80
109#define BU21013_TH_ON_MAX 0xFF
110
111#define BU21013_TH_OFF_0 0x01
112#define BU21013_TH_OFF_1 0x02
113#define BU21013_TH_OFF_2 0x04
114#define BU21013_TH_OFF_3 0x08
115#define BU21013_TH_OFF_4 0x10
116#define BU21013_TH_OFF_5 0x20
117#define BU21013_TH_OFF_6 0x40
118#define BU21013_TH_OFF_7 0x80
119#define BU21013_TH_OFF_MAX 0xFF
120
121#define BU21013_X_EDGE_0 0x01
122#define BU21013_X_EDGE_1 0x02
123#define BU21013_X_EDGE_2 0x04
124#define BU21013_X_EDGE_3 0x08
125#define BU21013_Y_EDGE_0 0x10
126#define BU21013_Y_EDGE_1 0x20
127#define BU21013_Y_EDGE_2 0x40
128#define BU21013_Y_EDGE_3 0x80
129
130#define BU21013_DONE 0x01
131#define BU21013_NUMBER_OF_X_SENSORS (6)
132#define BU21013_NUMBER_OF_Y_SENSORS (11)
133
134#define DRIVER_TP "bu21013_tp"
135
136/**
137 * struct bu21013_ts_data - touch panel data structure
138 * @client: pointer to the i2c client
139 * @wait: variable to wait_queue_head_t structure
140 * @touch_stopped: touch stop flag
141 * @chip: pointer to the touch panel controller
142 * @in_dev: pointer to the input device structure
143 * @intr_pin: interrupt pin value
144 * @regulator: pointer to the Regulator used for touch screen
145 *
146 * Touch panel device data structure
147 */
148struct bu21013_ts_data {
149 struct i2c_client *client;
150 wait_queue_head_t wait;
151 bool touch_stopped;
152 const struct bu21013_platform_device *chip;
153 struct input_dev *in_dev;
154 unsigned int intr_pin;
155 struct regulator *regulator;
156};
157
158/**
159 * bu21013_read_block_data(): read the touch co-ordinates
160 * @data: bu21013_ts_data structure pointer
161 * @buf: byte pointer
162 *
163 * Read the touch co-ordinates using i2c read block into buffer
164 * and returns integer.
165 */
166static int bu21013_read_block_data(struct bu21013_ts_data *data, u8 *buf)
167{
168 int ret, i;
169
170 for (i = 0; i < I2C_RETRY_COUNT; i++) {
171 ret = i2c_smbus_read_i2c_block_data
172 (data->client, BU21013_SENSORS_BTN_0_7_REG,
173 LENGTH_OF_BUFFER, buf);
174 if (ret == LENGTH_OF_BUFFER)
175 return 0;
176 }
177 return -EINVAL;
178}
179
180/**
181 * bu21013_do_touch_report(): Get the touch co-ordinates
182 * @data: bu21013_ts_data structure pointer
183 *
184 * Get the touch co-ordinates from touch sensor registers and writes
185 * into device structure and returns integer.
186 */
187static int bu21013_do_touch_report(struct bu21013_ts_data *data)
188{
189 u8 buf[LENGTH_OF_BUFFER];
190 unsigned int pos_x[2], pos_y[2];
191 bool has_x_sensors, has_y_sensors;
192 int finger_down_count = 0;
193 int i;
194
195 if (data == NULL)
196 return -EINVAL;
197
198 if (bu21013_read_block_data(data, buf) < 0)
199 return -EINVAL;
200
201 has_x_sensors = hweight32(buf[0] & BU21013_SENSORS_EN_0_7);
202 has_y_sensors = hweight32(((buf[1] & BU21013_SENSORS_EN_8_15) |
203 ((buf[2] & BU21013_SENSORS_EN_16_23) << SHIFT_8)) >> SHIFT_2);
204 if (!has_x_sensors || !has_y_sensors)
205 return 0;
206
207 for (i = 0; i < MAX_FINGERS; i++) {
208 const u8 *p = &buf[4 * i + 3];
209 unsigned int x = p[0] << SHIFT_2 | (p[1] & MASK_BITS);
210 unsigned int y = p[2] << SHIFT_2 | (p[3] & MASK_BITS);
211 if (x == 0 || y == 0)
212 continue;
213 pos_x[finger_down_count] = x;
214 pos_y[finger_down_count] = y;
215 finger_down_count++;
216 }
217
218 if (finger_down_count) {
219 if (finger_down_count == 2 &&
220 (abs(pos_x[0] - pos_x[1]) < DELTA_MIN ||
221 abs(pos_y[0] - pos_y[1]) < DELTA_MIN)) {
222 return 0;
223 }
224
225 for (i = 0; i < finger_down_count; i++) {
226 if (data->chip->x_flip)
227 pos_x[i] = data->chip->touch_x_max - pos_x[i];
228 if (data->chip->y_flip)
229 pos_y[i] = data->chip->touch_y_max - pos_y[i];
230
231 input_report_abs(data->in_dev,
232 ABS_MT_POSITION_X, pos_x[i]);
233 input_report_abs(data->in_dev,
234 ABS_MT_POSITION_Y, pos_y[i]);
235 input_mt_sync(data->in_dev);
236 }
237 } else
238 input_mt_sync(data->in_dev);
239
240 input_sync(data->in_dev);
241
242 return 0;
243}
244/**
245 * bu21013_gpio_irq() - gpio thread function for touch interrupt
246 * @irq: irq value
247 * @device_data: void pointer
248 *
249 * This gpio thread function for touch interrupt
250 * and returns irqreturn_t.
251 */
252static irqreturn_t bu21013_gpio_irq(int irq, void *device_data)
253{
254 struct bu21013_ts_data *data = device_data;
255 struct i2c_client *i2c = data->client;
256 int retval;
257
258 do {
259 retval = bu21013_do_touch_report(data);
260 if (retval < 0) {
261 dev_err(&i2c->dev, "bu21013_do_touch_report failed\n");
262 return IRQ_NONE;
263 }
264
265 data->intr_pin = data->chip->irq_read_val();
266 if (data->intr_pin == PEN_DOWN_INTR)
267 wait_event_timeout(data->wait, data->touch_stopped,
268 msecs_to_jiffies(2));
269 } while (!data->intr_pin && !data->touch_stopped);
270
271 return IRQ_HANDLED;
272}
273
274/**
275 * bu21013_init_chip() - power on sequence for the bu21013 controller
276 * @data: device structure pointer
277 *
278 * This function is used to power on
279 * the bu21013 controller and returns integer.
280 */
281static int bu21013_init_chip(struct bu21013_ts_data *data)
282{
283 int retval;
284 struct i2c_client *i2c = data->client;
285
286 retval = i2c_smbus_write_byte_data(i2c, BU21013_RESET_REG,
287 BU21013_RESET_ENABLE);
288 if (retval < 0) {
289 dev_err(&i2c->dev, "BU21013_RESET reg write failed\n");
290 return retval;
291 }
292 msleep(RESET_DELAY);
293
294 retval = i2c_smbus_write_byte_data(i2c, BU21013_SENSOR_0_7_REG,
295 BU21013_SENSORS_EN_0_7);
296 if (retval < 0) {
297 dev_err(&i2c->dev, "BU21013_SENSOR_0_7 reg write failed\n");
298 return retval;
299 }
300
301 retval = i2c_smbus_write_byte_data(i2c, BU21013_SENSOR_8_15_REG,
302 BU21013_SENSORS_EN_8_15);
303 if (retval < 0) {
304 dev_err(&i2c->dev, "BU21013_SENSOR_8_15 reg write failed\n");
305 return retval;
306 }
307
308 retval = i2c_smbus_write_byte_data(i2c, BU21013_SENSOR_16_23_REG,
309 BU21013_SENSORS_EN_16_23);
310 if (retval < 0) {
311 dev_err(&i2c->dev, "BU21013_SENSOR_16_23 reg write failed\n");
312 return retval;
313 }
314
315 retval = i2c_smbus_write_byte_data(i2c, BU21013_POS_MODE1_REG,
316 (BU21013_POS_MODE1_0 | BU21013_POS_MODE1_1));
317 if (retval < 0) {
318 dev_err(&i2c->dev, "BU21013_POS_MODE1 reg write failed\n");
319 return retval;
320 }
321
322 retval = i2c_smbus_write_byte_data(i2c, BU21013_POS_MODE2_REG,
323 (BU21013_POS_MODE2_ZERO | BU21013_POS_MODE2_AVG1 |
324 BU21013_POS_MODE2_AVG2 | BU21013_POS_MODE2_EN_RAW |
325 BU21013_POS_MODE2_MULTI));
326 if (retval < 0) {
327 dev_err(&i2c->dev, "BU21013_POS_MODE2 reg write failed\n");
328 return retval;
329 }
330
331 if (data->chip->ext_clk)
332 retval = i2c_smbus_write_byte_data(i2c, BU21013_CLK_MODE_REG,
333 (BU21013_CLK_MODE_EXT | BU21013_CLK_MODE_CALIB));
334 else
335 retval = i2c_smbus_write_byte_data(i2c, BU21013_CLK_MODE_REG,
336 (BU21013_CLK_MODE_DIV | BU21013_CLK_MODE_CALIB));
337 if (retval < 0) {
338 dev_err(&i2c->dev, "BU21013_CLK_MODE reg write failed\n");
339 return retval;
340 }
341
342 retval = i2c_smbus_write_byte_data(i2c, BU21013_IDLE_REG,
343 (BU21013_IDLET_0 | BU21013_IDLE_INTERMIT_EN));
344 if (retval < 0) {
345 dev_err(&i2c->dev, "BU21013_IDLE reg write failed\n");
346 return retval;
347 }
348
349 retval = i2c_smbus_write_byte_data(i2c, BU21013_INT_MODE_REG,
350 BU21013_INT_MODE_LEVEL);
351 if (retval < 0) {
352 dev_err(&i2c->dev, "BU21013_INT_MODE reg write failed\n");
353 return retval;
354 }
355
356 retval = i2c_smbus_write_byte_data(i2c, BU21013_FILTER_REG,
357 (BU21013_DELTA_0_6 |
358 BU21013_FILTER_EN));
359 if (retval < 0) {
360 dev_err(&i2c->dev, "BU21013_FILTER reg write failed\n");
361 return retval;
362 }
363
364 retval = i2c_smbus_write_byte_data(i2c, BU21013_TH_ON_REG,
365 BU21013_TH_ON_5);
366 if (retval < 0) {
367 dev_err(&i2c->dev, "BU21013_TH_ON reg write failed\n");
368 return retval;
369 }
370
371 retval = i2c_smbus_write_byte_data(i2c, BU21013_TH_OFF_REG,
372 BU21013_TH_OFF_4 | BU21013_TH_OFF_3);
373 if (retval < 0) {
374 dev_err(&i2c->dev, "BU21013_TH_OFF reg write failed\n");
375 return retval;
376 }
377
378 retval = i2c_smbus_write_byte_data(i2c, BU21013_GAIN_REG,
379 (BU21013_GAIN_0 | BU21013_GAIN_1));
380 if (retval < 0) {
381 dev_err(&i2c->dev, "BU21013_GAIN reg write failed\n");
382 return retval;
383 }
384
385 retval = i2c_smbus_write_byte_data(i2c, BU21013_OFFSET_MODE_REG,
386 BU21013_OFFSET_MODE_DEFAULT);
387 if (retval < 0) {
388 dev_err(&i2c->dev, "BU21013_OFFSET_MODE reg write failed\n");
389 return retval;
390 }
391
392 retval = i2c_smbus_write_byte_data(i2c, BU21013_XY_EDGE_REG,
393 (BU21013_X_EDGE_0 | BU21013_X_EDGE_2 |
394 BU21013_Y_EDGE_1 | BU21013_Y_EDGE_3));
395 if (retval < 0) {
396 dev_err(&i2c->dev, "BU21013_XY_EDGE reg write failed\n");
397 return retval;
398 }
399
400 retval = i2c_smbus_write_byte_data(i2c, BU21013_DONE_REG,
401 BU21013_DONE);
402 if (retval < 0) {
403 dev_err(&i2c->dev, "BU21013_REG_DONE reg write failed\n");
404 return retval;
405 }
406
407 return 0;
408}
409
410/**
411 * bu21013_free_irq() - frees IRQ registered for touchscreen
412 * @bu21013_data: device structure pointer
413 *
414 * This function signals interrupt thread to stop processing and
415 * frees interrupt.
416 */
417static void bu21013_free_irq(struct bu21013_ts_data *bu21013_data)
418{
419 bu21013_data->touch_stopped = true;
420 wake_up(&bu21013_data->wait);
421 free_irq(bu21013_data->chip->irq, bu21013_data);
422}
423
424/**
425 * bu21013_probe() - initializes the i2c-client touchscreen driver
426 * @client: i2c client structure pointer
427 * @id: i2c device id pointer
428 *
429 * This function used to initializes the i2c-client touchscreen
430 * driver and returns integer.
431 */
432static int __devinit bu21013_probe(struct i2c_client *client,
433 const struct i2c_device_id *id)
434{
435 struct bu21013_ts_data *bu21013_data;
436 struct input_dev *in_dev;
437 const struct bu21013_platform_device *pdata =
438 client->dev.platform_data;
439 int error;
440
441 if (!i2c_check_functionality(client->adapter,
442 I2C_FUNC_SMBUS_BYTE_DATA)) {
443 dev_err(&client->dev, "i2c smbus byte data not supported\n");
444 return -EIO;
445 }
446
447 if (!pdata) {
448 dev_err(&client->dev, "platform data not defined\n");
449 return -EINVAL;
450 }
451
452 bu21013_data = kzalloc(sizeof(struct bu21013_ts_data), GFP_KERNEL);
453 in_dev = input_allocate_device();
454 if (!bu21013_data || !in_dev) {
455 dev_err(&client->dev, "device memory alloc failed\n");
456 error = -ENOMEM;
457 goto err_free_mem;
458 }
459
460 bu21013_data->in_dev = in_dev;
461 bu21013_data->chip = pdata;
462 bu21013_data->client = client;
463
464 bu21013_data->regulator = regulator_get(&client->dev, "V-TOUCH");
465 if (IS_ERR(bu21013_data->regulator)) {
466 dev_err(&client->dev, "regulator_get failed\n");
467 error = PTR_ERR(bu21013_data->regulator);
468 goto err_free_mem;
469 }
470
471 error = regulator_enable(bu21013_data->regulator);
472 if (error < 0) {
473 dev_err(&client->dev, "regulator enable failed\n");
474 goto err_put_regulator;
475 }
476
477 bu21013_data->touch_stopped = false;
478 init_waitqueue_head(&bu21013_data->wait);
479
480 /* configure the gpio pins */
481 if (pdata->cs_en) {
482 error = pdata->cs_en(pdata->cs_pin);
483 if (error < 0) {
484 dev_err(&client->dev, "chip init failed\n");
485 goto err_disable_regulator;
486 }
487 }
488
489 /* configure the touch panel controller */
490 error = bu21013_init_chip(bu21013_data);
491 if (error) {
492 dev_err(&client->dev, "error in bu21013 config\n");
493 goto err_cs_disable;
494 }
495
496 /* register the device to input subsystem */
497 in_dev->name = DRIVER_TP;
498 in_dev->id.bustype = BUS_I2C;
499 in_dev->dev.parent = &client->dev;
500
501 __set_bit(EV_SYN, in_dev->evbit);
502 __set_bit(EV_KEY, in_dev->evbit);
503 __set_bit(EV_ABS, in_dev->evbit);
504
505 input_set_abs_params(in_dev, ABS_MT_POSITION_X, 0,
506 pdata->touch_x_max, 0, 0);
507 input_set_abs_params(in_dev, ABS_MT_POSITION_Y, 0,
508 pdata->touch_y_max, 0, 0);
509 input_set_drvdata(in_dev, bu21013_data);
510
511 error = request_threaded_irq(pdata->irq, NULL, bu21013_gpio_irq,
512 IRQF_TRIGGER_FALLING | IRQF_SHARED |
513 IRQF_ONESHOT,
514 DRIVER_TP, bu21013_data);
515 if (error) {
516 dev_err(&client->dev, "request irq %d failed\n", pdata->irq);
517 goto err_cs_disable;
518 }
519
520 error = input_register_device(in_dev);
521 if (error) {
522 dev_err(&client->dev, "failed to register input device\n");
523 goto err_free_irq;
524 }
525
526 device_init_wakeup(&client->dev, pdata->wakeup);
527 i2c_set_clientdata(client, bu21013_data);
528
529 return 0;
530
531err_free_irq:
532 bu21013_free_irq(bu21013_data);
533err_cs_disable:
534 pdata->cs_dis(pdata->cs_pin);
535err_disable_regulator:
536 regulator_disable(bu21013_data->regulator);
537err_put_regulator:
538 regulator_put(bu21013_data->regulator);
539err_free_mem:
540 input_free_device(in_dev);
541 kfree(bu21013_data);
542
543 return error;
544}
545/**
546 * bu21013_remove() - removes the i2c-client touchscreen driver
547 * @client: i2c client structure pointer
548 *
549 * This function uses to remove the i2c-client
550 * touchscreen driver and returns integer.
551 */
552static int __devexit bu21013_remove(struct i2c_client *client)
553{
554 struct bu21013_ts_data *bu21013_data = i2c_get_clientdata(client);
555
556 bu21013_free_irq(bu21013_data);
557
558 bu21013_data->chip->cs_dis(bu21013_data->chip->cs_pin);
559
560 input_unregister_device(bu21013_data->in_dev);
561
562 regulator_disable(bu21013_data->regulator);
563 regulator_put(bu21013_data->regulator);
564
565 kfree(bu21013_data);
566
567 device_init_wakeup(&client->dev, false);
568
569 return 0;
570}
571
572#ifdef CONFIG_PM
573/**
574 * bu21013_suspend() - suspend the touch screen controller
575 * @dev: pointer to device structure
576 *
577 * This function is used to suspend the
578 * touch panel controller and returns integer
579 */
580static int bu21013_suspend(struct device *dev)
581{
582 struct bu21013_ts_data *bu21013_data = dev_get_drvdata(dev);
583 struct i2c_client *client = bu21013_data->client;
584
585 bu21013_data->touch_stopped = true;
586 if (device_may_wakeup(&client->dev))
587 enable_irq_wake(bu21013_data->chip->irq);
588 else
589 disable_irq(bu21013_data->chip->irq);
590
591 regulator_disable(bu21013_data->regulator);
592
593 return 0;
594}
595
596/**
597 * bu21013_resume() - resume the touch screen controller
598 * @dev: pointer to device structure
599 *
600 * This function is used to resume the touch panel
601 * controller and returns integer.
602 */
603static int bu21013_resume(struct device *dev)
604{
605 struct bu21013_ts_data *bu21013_data = dev_get_drvdata(dev);
606 struct i2c_client *client = bu21013_data->client;
607 int retval;
608
609 retval = regulator_enable(bu21013_data->regulator);
610 if (retval < 0) {
611 dev_err(&client->dev, "bu21013 regulator enable failed\n");
612 return retval;
613 }
614
615 retval = bu21013_init_chip(bu21013_data);
616 if (retval < 0) {
617 dev_err(&client->dev, "bu21013 controller config failed\n");
618 return retval;
619 }
620
621 bu21013_data->touch_stopped = false;
622
623 if (device_may_wakeup(&client->dev))
624 disable_irq_wake(bu21013_data->chip->irq);
625 else
626 enable_irq(bu21013_data->chip->irq);
627
628 return 0;
629}
630
631static const struct dev_pm_ops bu21013_dev_pm_ops = {
632 .suspend = bu21013_suspend,
633 .resume = bu21013_resume,
634};
635#endif
636
637static const struct i2c_device_id bu21013_id[] = {
638 { DRIVER_TP, 0 },
639 { }
640};
641MODULE_DEVICE_TABLE(i2c, bu21013_id);
642
643static struct i2c_driver bu21013_driver = {
644 .driver = {
645 .name = DRIVER_TP,
646 .owner = THIS_MODULE,
647#ifdef CONFIG_PM
648 .pm = &bu21013_dev_pm_ops,
649#endif
650 },
651 .probe = bu21013_probe,
652 .remove = __devexit_p(bu21013_remove),
653 .id_table = bu21013_id,
654};
655
656module_i2c_driver(bu21013_driver);
657
658MODULE_LICENSE("GPL v2");
659MODULE_AUTHOR("Naveen Kumar G <naveen.gaddipati@stericsson.com>");
660MODULE_DESCRIPTION("bu21013 touch screen controller driver");