Linux Audio

Check our new training course

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