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