summaryrefslogtreecommitdiff
path: root/drivers/staging/iio/light/iqs253.c
blob: ea7480105a3407c5a146b451a4129fe43347c9bf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
/*
 * A iio driver for the capacitive sensor IQS253.
 *
 * IIO Light driver for monitoring proximity.
 *
 * Copyright (c) 2014, NVIDIA CORPORATION. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms and conditions of the GNU General Public License,
 * version 2, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 */

#include <linux/module.h>
#include <linux/i2c.h>
#include <linux/err.h>
#include <linux/mutex.h>
#include <linux/pm_runtime.h>
#include <linux/regulator/consumer.h>
#include <linux/platform_device.h>
#include <linux/gpio.h>
#include <linux/of_gpio.h>
#include <linux/slab.h>
#include <linux/delay.h>
#include <linux/irqchip/tegra.h>
#include <linux/input.h>
#include <linux/iio/iio.h>
#include <linux/iio/sysfs.h>
#include <linux/iio/light/ls_sysfs.h>
#include <linux/iio/light/ls_dt.h>

/* registers */
#define SYSFLAGS		0x10
#define PROX_STATUS		0x31
#define TOUCH_STATUS		0x35
#define TARGET			0xC4
#define COMP0			0xC5
#define CH0_ATI_BASE		0xC8
#define CH1_ATI_BASE		0xC9
#define CH2_ATI_BASE		0xCA
#define CH0_PTH			0xCB
#define CH1_PTH			0xCC
#define CH2_PTH			0xCD
#define PROX_SETTINGS0		0xD1
#define PROX_SETTINGS1		0xD2
#define PROX_SETTINGS2		0xD3
#define PROX_SETTINGS3		0xD4
#define ACTIVE_CHAN		0xD5
#define LOW_POWER		0xD6
#define DYCAL_CHANS		0xD8
#define EVENT_MODE_MASK		0xD9
#define DEFAULT_COMMS_POINTER	0xDD

#define IQS253_PROD_ID		41

#define PROX_CH0		0x01
#define PROX_CH1		0x02
#define PROX_CH2		0x04

#define STYLUS_ONLY		PROX_CH0
#define PROXIMITY_ONLY		(PROX_CH1 | PROX_CH2)

#define CH0_COMPENSATION	0x55

#define PROX_TH_CH0		0x04 /* proximity threshold = 0.5 cm */
#define PROX_TH_CH1		0x01 /* proximity threshold = 2 cm */
#define PROX_TH_CH2		0x01 /* proximity threshold = 2 cm */

#define DISABLE_DYCAL		0x00

#define CH0_ATI_TH		0x17
#define CH1_ATI_TH		0x17
#define CH2_ATI_TH		0x19

#define EVENT_PROX_ONLY		0x01

#define PROX_SETTING_NORMAL	0x25
#define PROX_SETTING_STYLUS	0x26

#define AUTO_ATI_DISABLE	BIT(7)
#define ATI_IN_PROGRESS		0x04

/* initial values */

#define EVENT_MODE_DISABLE_MASK	0x04
#define LTA_DISABLE		BIT(5)
#define ACF_DISABLE		BIT(4)
/* LTA always halt */
#define LTA_HALT_11		(BIT(0) | BIT(1))

#define ATI_ENABLED_MASK	0x80

#define NUM_REG 17

struct iqs253_chip {
	struct i2c_client	*client;
	const struct i2c_device_id	*id;
	u32			rdy_gpio;
	u32			wake_gpio;
	u32			sar_gpio;
	u32			mode;
	u32			value;
	struct regulator	*vddhi;
	u32			using_regulator;
#if defined(CONFIG_SENSORS_IQS253_AS_PROXIMITY)
	struct lightsensor_spec	*ls_spec;
#endif
	struct workqueue_struct	*wq;
	struct delayed_work	dw;
#if !defined(CONFIG_SENSORS_IQS253_AS_PROXIMITY)
	struct workqueue_struct	*sar_wq;
	struct delayed_work	sar_dw;
#endif
	struct input_dev	*idev;
	u32			stylus_inserted;
};

enum mode {
	MODE_NONE = -1,
	NORMAL_MODE,
	STYLUS_MODE,
	INIT_MODE,
	FORCE_ATI_MODE,
	POST_INIT_MODE,
	NUM_MODE
};

struct reg_val_pair {
	u8 reg;
	u8 val;
};

struct reg_val_pair reg_val_map[NUM_MODE][NUM_REG] = {
	{
		{ COMP0, CH0_COMPENSATION},
		{ CH0_ATI_BASE, CH0_ATI_TH},
		{ CH1_ATI_BASE, CH1_ATI_TH},
		{ CH2_ATI_BASE, CH2_ATI_TH},
		{ CH0_PTH, PROX_TH_CH0},
		{ CH1_PTH, PROX_TH_CH1},
		{ PROX_SETTINGS0, PROX_SETTING_NORMAL},
		{ ACTIVE_CHAN, PROXIMITY_ONLY | STYLUS_ONLY},
		{ DYCAL_CHANS, DISABLE_DYCAL},
		{ EVENT_MODE_MASK, EVENT_PROX_ONLY}
	},
	{
		{ COMP0, CH0_COMPENSATION},
		{ CH0_ATI_BASE, CH0_ATI_TH},
		{ CH1_ATI_BASE, CH1_ATI_TH},
		{ CH2_ATI_BASE, CH2_ATI_TH},
		{ CH2_PTH,  PROX_TH_CH2},
		{ PROX_SETTINGS0, PROX_SETTING_STYLUS},
		{ ACTIVE_CHAN, STYLUS_ONLY},
		{ DYCAL_CHANS, DISABLE_DYCAL},
		{ EVENT_MODE_MASK, EVENT_PROX_ONLY}
	},
	{	/* init settings */
		{ PROX_SETTINGS2, ACF_DISABLE | EVENT_MODE_DISABLE_MASK},
		{ ACTIVE_CHAN, PROXIMITY_ONLY},
		{ PROX_SETTINGS0, AUTO_ATI_DISABLE},
		{ CH0_PTH, 0x04},
		{ CH1_PTH, 0x04},
		{ CH2_PTH, 0x04},
		{ TARGET, 0x20},
	},
	{	/* force on ATI */
		{ PROX_SETTINGS0, 0x50}, /* enable ATI and force auto ATI */
	},
	{
		{ PROX_SETTINGS0, AUTO_ATI_DISABLE}, /* turn off ATI*/
		{ PROX_SETTINGS2, ACF_DISABLE |
				  EVENT_MODE_DISABLE_MASK | LTA_HALT_11},
		{ DYCAL_CHANS, DISABLE_DYCAL},
		/* turning on ATI is recommended but it has side effects */
	},
};

static void iqs253_i2c_hand_shake(struct iqs253_chip *iqs253_chip)
{
	int retry_count = 10;
	do {
		gpio_direction_output(iqs253_chip->rdy_gpio, 0);
		usleep_range(10 * 1000, 10 * 1000);
		/* put to tristate */
		gpio_direction_input(iqs253_chip->rdy_gpio);
	} while (gpio_get_value(iqs253_chip->rdy_gpio) && retry_count--);
}

static int iqs253_i2c_read_byte(struct iqs253_chip *chip, int reg)
{
	int ret = 0, retry_count = 10;
	do {
		iqs253_i2c_hand_shake(chip);
		ret = i2c_smbus_read_byte_data(chip->client, reg);
	} while (ret && retry_count--);
	return ret;
}

static int iqs253_i2c_write_byte(struct iqs253_chip *chip, int reg, int val)
{
	int ret = 0, retry_count = 10;
	do {
		iqs253_i2c_hand_shake(chip);
		ret = i2c_smbus_write_byte_data(chip->client, reg, val);
	} while (ret && retry_count--);
	return ret;
}

/* must call holding lock */
static int iqs253_set(struct iqs253_chip *iqs253_chip, int mode)
{
	int ret = 0, i;
	struct reg_val_pair *reg_val_pair_map;

	if ((mode != NORMAL_MODE) && (mode != STYLUS_MODE))
		return -EINVAL;

	reg_val_pair_map = reg_val_map[INIT_MODE];

	for (i = 0; i < NUM_REG; i++) {
		if (!reg_val_pair_map[i].reg && !reg_val_pair_map[i].val)
			continue;

		ret = iqs253_i2c_write_byte(iqs253_chip,
					reg_val_pair_map[i].reg,
					reg_val_pair_map[i].val);
		if (ret) {
			dev_err(&iqs253_chip->client->dev,
				"iqs253 write val:%x to reg:%x failed\n",
				reg_val_pair_map[i].val,
				reg_val_pair_map[i].reg);
			return ret;
		}
	}

	reg_val_pair_map = reg_val_map[FORCE_ATI_MODE];

	for (i = 0; i < NUM_REG; i++) {
		if (!reg_val_pair_map[i].reg && !reg_val_pair_map[i].val)
			continue;

		ret = iqs253_i2c_write_byte(iqs253_chip,
					reg_val_pair_map[i].reg,
					reg_val_pair_map[i].val);
		if (ret) {
			dev_err(&iqs253_chip->client->dev,
				"iqs253 write val:%x to reg:%x failed\n",
				reg_val_pair_map[i].val,
				reg_val_pair_map[i].reg);
			return ret;
		}
	}
	/* wait for ATI to finish */
	do {
		usleep_range(10 * 1000, 10 * 1000);
		ret = iqs253_i2c_read_byte(iqs253_chip, SYSFLAGS);
	} while (ret & ATI_IN_PROGRESS);

	reg_val_pair_map = reg_val_map[POST_INIT_MODE];

	for (i = 0; i < NUM_REG; i++) {
		if (!reg_val_pair_map[i].reg && !reg_val_pair_map[i].val)
			continue;

		ret = iqs253_i2c_write_byte(iqs253_chip,
					reg_val_pair_map[i].reg,
					reg_val_pair_map[i].val);
		if (ret) {
			dev_err(&iqs253_chip->client->dev,
				"iqs253 write val:%x to reg:%x failed\n",
				reg_val_pair_map[i].val,
				reg_val_pair_map[i].reg);
			return ret;
		}
	}
	iqs253_chip->mode = mode;
	return 0;
}

#if defined(CONFIG_SENSORS_IQS253_AS_PROXIMITY)
/* device's registration with iio to facilitate user operations */
static ssize_t iqs253_chan_regulator_enable(
		struct iio_dev *indio_dev, uintptr_t private,
		struct iio_chan_spec const *chan,
		const char *buf, size_t len)
{
	int ret = 0;
	u8 enable;
	struct iqs253_chip *chip = iio_priv(indio_dev);

	if (chip->mode == STYLUS_MODE)
		return -EINVAL;

	if (kstrtou8(buf, 10, &enable))
		return -EINVAL;

	if ((enable != 0) && (enable != 1))
		return -EINVAL;

	if (chan->type != IIO_PROXIMITY)
		return -EINVAL;

	if (enable == chip->using_regulator)
		goto success;

	if (enable)
		ret = regulator_enable(chip->vddhi);
	else
		ret = regulator_disable(chip->vddhi);

	if (ret) {
		dev_err(&chip->client->dev,
		"idname:%s func:%s line:%d enable:%d regulator logic failed\n",
		chip->id->name, __func__, __LINE__, enable);
		goto fail;
	}

success:
	chip->using_regulator = enable;
	chip->mode = MODE_NONE;
fail:
	return ret ? ret : 1;
}

static ssize_t iqs253_chan_normal_mode_enable(
		struct iio_dev *indio_dev, uintptr_t private,
		struct iio_chan_spec const *chan,
		const char *buf, size_t len)
{
	int ret = 0;
	u8 enable;
	struct iqs253_chip *chip = iio_priv(indio_dev);

	if (chip->mode == STYLUS_MODE)
		return -EINVAL;

	if (kstrtou8(buf, 10, &enable))
		return -EINVAL;

	if ((enable != 0) && (enable != 1))
		return -EINVAL;

	if (chan->type != IIO_PROXIMITY)
		return -EINVAL;

	if (!chip->using_regulator)
		return -EINVAL;

	if (enable)
		ret = iqs253_set(chip, NORMAL_MODE);
	else
		chip->mode = MODE_NONE;

	return ret ? ret : 1;
}

/*
 * chan_regulator_enable is used to enable regulators used by
 * particular channel.
 * chan_enable actually configures various registers to activate
 * a particular channel.
 */
static const struct iio_chan_spec_ext_info iqs253_ext_info[] = {
	{
		.name = "regulator_enable",
		.write = iqs253_chan_regulator_enable,
	},
	{
		.name = "enable",
		.write = iqs253_chan_normal_mode_enable,
	},
	{
	},
};

static const struct iio_chan_spec iqs253_channels[] = {
	{
		.type = IIO_PROXIMITY,
		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
		.ext_info = iqs253_ext_info,
	},
};

static int iqs253_read_raw(struct iio_dev *indio_dev,
	struct iio_chan_spec const *chan, int *val, int *val2, long mask)
{
	struct iqs253_chip *chip = iio_priv(indio_dev);
	int ret;

	if (chip->mode != NORMAL_MODE)
		return -EINVAL;

	if (chan->type != IIO_PROXIMITY)
		return -EINVAL;

	ret = iqs253_i2c_read_byte(chip, PROX_STATUS);
	chip->value = -1;
	if (ret >= 0) {
		if ((ret >= 0) && (chip->mode == NORMAL_MODE)) {
			ret = ret & PROXIMITY_ONLY;
			/*
			 * if both channel detect proximity => distance = 0;
			 * if one channel detects proximity => distance = 1;
			 * if no channel detects proximity => distance = 2;
			 */
			chip->value = (ret == (PROX_CH1 | PROX_CH2)) ? 0 :
								ret ? 1 : 2;
		}
	}
	if (chip->value == -1)
		return -EINVAL;

	*val = chip->value; /* cm */

	/* provide input to SAR */
	if (chip->value/2)
		gpio_direction_output(chip->sar_gpio, 0);
	else
		gpio_direction_output(chip->sar_gpio, 1);

	return IIO_VAL_INT;
}

static IIO_CONST_ATTR(vendor, "Azoteq");
static IIO_CONST_ATTR(in_proximity_integration_time,
			"16000000"); /* 16 msec */
static IIO_CONST_ATTR(in_proximity_max_range, "2"); /* cm */
static IIO_CONST_ATTR(in_proximity_power_consumed, "1.67"); /* mA */

static struct attribute *iqs253_attrs[] = {
	&iio_const_attr_vendor.dev_attr.attr,
	&iio_const_attr_in_proximity_integration_time.dev_attr.attr,
	&iio_const_attr_in_proximity_max_range.dev_attr.attr,
	&iio_const_attr_in_proximity_power_consumed.dev_attr.attr,
	NULL
};

static struct attribute_group iqs253_attr_group = {
	.name = "iqs253",
	.attrs = iqs253_attrs
};

static struct iio_info iqs253_iio_info = {
	.driver_module = THIS_MODULE,
	.read_raw = &iqs253_read_raw,
	.attrs = &iqs253_attr_group,
};

#else
static void iqs253_sar_proximity_detect_work(struct work_struct *ws)
{
	int ret;
	struct iqs253_chip *chip;

	chip = container_of(ws, struct iqs253_chip, sar_dw.work);

	if (!chip->using_regulator) {
		ret = regulator_enable(chip->vddhi);
		if (ret)
			goto finish;
		chip->using_regulator = true;
	}

	if (chip->mode != NORMAL_MODE) {
		ret = iqs253_set(chip, NORMAL_MODE);
		if (ret)
			goto finish;
	}

	ret = iqs253_i2c_read_byte(chip, PROX_STATUS);
	chip->value = -1;
	if (ret >= 0) {
		if ((ret >= 0) && (chip->mode == NORMAL_MODE)) {
			ret = ret & PROXIMITY_ONLY;
			/*
			 * if both channel detect proximity => distance = 0;
			 * if one channel detects proximity => distance = 1;
			 * if no channel detects proximity => distance = 2;
			 */
			chip->value = (ret == (PROX_CH1 | PROX_CH2)) ? 0 :
								ret ? 1 : 2;
		}
	}
	if (chip->value == -1)
		goto finish;
	/* provide input to SAR */
	if (chip->value/2)
		gpio_direction_output(chip->sar_gpio, 0);
	else
		gpio_direction_output(chip->sar_gpio, 1);

	ret = regulator_disable(chip->vddhi);
	if (ret)
		goto finish;
	chip->using_regulator = false;

finish:
	queue_delayed_work(chip->sar_wq, &chip->sar_dw, msecs_to_jiffies(1000));
}

#endif /* CONFIG_SENSORS_IQS253_AS_PROXIMITY */

#ifdef CONFIG_PM_SLEEP
static int iqs253_suspend(struct device *dev)
{
	struct i2c_client *client = to_i2c_client(dev);
	struct iio_dev *indio_dev = i2c_get_clientdata(client);
	struct iqs253_chip *chip = iio_priv(indio_dev);
	int ret = 0;

	if (!chip->using_regulator)
		ret = regulator_enable(chip->vddhi);

	if (ret) {
		dev_err(&chip->client->dev,
		"idname:%s func:%s line:%d regulator enable fails\n",
		chip->id->name, __func__, __LINE__);
		return ret;
	}

	ret = iqs253_set(chip, STYLUS_MODE);
	if (ret) {
		dev_err(&chip->client->dev,
		"idname:%s func:%s line:%d can not enable stylus mode\n",
		chip->id->name, __func__, __LINE__);
		return ret;
	}
	return tegra_pm_irq_set_wake(tegra_gpio_to_wake(chip->wake_gpio), 1);
}

static int iqs253_resume(struct device *dev)
{
	struct i2c_client *client = to_i2c_client(dev);
	struct iio_dev *indio_dev = i2c_get_clientdata(client);
	struct iqs253_chip *chip = iio_priv(indio_dev);
	int ret = 0;

	if (chip->using_regulator) {
		ret = iqs253_set(chip, NORMAL_MODE);
	} else {
		chip->mode = MODE_NONE;
		ret = regulator_disable(chip->vddhi);
	}

	if (ret) {
		dev_err(&chip->client->dev,
		"idname:%s func:%s line:%d regulator enable fails\n",
		chip->id->name, __func__, __LINE__);
		return ret;
	}

	return ret;
}

static SIMPLE_DEV_PM_OPS(iqs253_pm_ops, iqs253_suspend, iqs253_resume);
#define IQS253_PM_OPS (&iqs253_pm_ops)
#else
#define IQS253_PM_OPS NULL
#endif

static void iqs253_stylus_detect_work(struct work_struct *ws)
{
	int ret;
	struct iqs253_chip *chip;

	chip = container_of(ws, struct iqs253_chip, dw.work);

	if (!chip->using_regulator) {
		ret = regulator_enable(chip->vddhi);
		if (ret)
			goto finish;
		chip->using_regulator = true;
	}

	if (chip->mode != NORMAL_MODE) {
		ret = iqs253_set(chip, NORMAL_MODE);
		if (ret)
			goto finish;

		ret = iqs253_i2c_read_byte(chip, PROX_STATUS);
		chip->stylus_inserted = (ret & STYLUS_ONLY);
		input_report_switch(chip->idev, SW_TABLET_MODE,
					!chip->stylus_inserted);
		input_sync(chip->idev);
	}

	ret = iqs253_i2c_read_byte(chip, PROX_STATUS);
	chip->value = -1;
	if (ret >= 0) {
		ret &= STYLUS_ONLY;
		if (ret && !chip->stylus_inserted) {
			chip->stylus_inserted = true;
			input_report_switch(chip->idev, SW_TABLET_MODE, false);
			input_sync(chip->idev);
		} else if (!ret && chip->stylus_inserted) {
			chip->stylus_inserted = false;
			input_report_switch(chip->idev, SW_TABLET_MODE, true);
			input_sync(chip->idev);
		}
	}

finish:
	queue_delayed_work(chip->wq, &chip->dw, msecs_to_jiffies(2000));
}

static struct input_dev *iqs253_stylus_input_init(struct iqs253_chip *chip)
{
	int ret;
	struct input_dev *idev = input_allocate_device();
	if (!idev)
		return NULL;

	idev->name = "stylus_detect";
	set_bit(EV_SW, idev->evbit);
	input_set_capability(idev, EV_SW, SW_TABLET_MODE);
	ret = input_register_device(idev);
	if (ret) {
		input_free_device(idev);
		return ERR_PTR(ret);
	}

	chip->wq = create_freezable_workqueue("iqs253");
	if (!chip->wq) {
		dev_err(&chip->client->dev, "unable to create work queue\n");
		input_unregister_device(idev);
		input_free_device(idev);
		return ERR_PTR(-ENOMEM);
	}

	INIT_DELAYED_WORK(&chip->dw, iqs253_stylus_detect_work);

	queue_delayed_work(chip->wq, &chip->dw, 0);

	return idev;
}

static int iqs253_probe(struct i2c_client *client,
			const struct i2c_device_id *id)
{
	int ret;
	struct iqs253_chip *iqs253_chip;
	struct iio_dev *indio_dev;
	struct input_dev *idev;
	int rdy_gpio = -1, wake_gpio = -1, sar_gpio = -1;
	struct property *stylus_detect = NULL;

	rdy_gpio = of_get_named_gpio(client->dev.of_node, "rdy-gpio", 0);
	if (rdy_gpio == -EPROBE_DEFER)
		return -EPROBE_DEFER;

	if (!gpio_is_valid(rdy_gpio))
		return -EINVAL;

	wake_gpio = of_get_named_gpio(client->dev.of_node, "wake-gpio", 0);
	if (wake_gpio == -EPROBE_DEFER)
		return -EPROBE_DEFER;

	if (!gpio_is_valid(wake_gpio))
		return -EINVAL;

	sar_gpio = of_get_named_gpio(client->dev.of_node, "sar-gpio", 0);
	if (sar_gpio == -EPROBE_DEFER)
		return -EPROBE_DEFER;

	ret = gpio_request_one(sar_gpio, GPIOF_OUT_INIT_LOW, NULL);
	if (ret < 0)
		return -EINVAL;

	indio_dev = iio_device_alloc(sizeof(*iqs253_chip));
	if (!indio_dev)
		return -ENOMEM;

	i2c_set_clientdata(client, indio_dev);
	iqs253_chip = iio_priv(indio_dev);

#if defined(CONFIG_SENSORS_IQS253_AS_PROXIMITY)
	iqs253_chip->ls_spec = of_get_ls_spec(&client->dev);
	if (!iqs253_chip->ls_spec) {
		dev_err(&client->dev,
			"devname:%s func:%s line:%d invalid meta data\n",
			id->name, __func__, __LINE__);
		return -ENODATA;
	}

	fill_ls_attrs(iqs253_chip->ls_spec, iqs253_attrs);
	indio_dev->channels = iqs253_channels;
	indio_dev->info = &iqs253_iio_info;
	indio_dev->num_channels = 1;
	indio_dev->name = id->name;
	indio_dev->dev.parent = &client->dev;
	indio_dev->modes = INDIO_DIRECT_MODE;
	ret = iio_device_register(indio_dev);
	if (ret) {
		dev_err(&client->dev,
			"devname:%s func:%s line:%d iio_device_register fail\n",
			id->name, __func__, __LINE__);
		goto err_iio_register;
	}
#endif

	iqs253_chip->client = client;
	iqs253_chip->id = id;
	iqs253_chip->mode = MODE_NONE;
	iqs253_chip->vddhi = devm_regulator_get(&client->dev, "vddhi");
	if (IS_ERR(iqs253_chip->vddhi)) {
		dev_err(&client->dev,
			"devname:%s func:%s regulator vddhi not found\n",
			id->name, __func__);
		goto err_regulator_get;
	}

	ret = gpio_request(rdy_gpio, "iqs253");
	if (ret) {
			dev_err(&client->dev,
			"devname:%s func:%s regulator vddhi not found\n",
			id->name, __func__);
		goto err_gpio_request;
	}
	iqs253_chip->rdy_gpio = rdy_gpio;
	iqs253_chip->wake_gpio = wake_gpio;
	iqs253_chip->sar_gpio = sar_gpio;

	ret = regulator_enable(iqs253_chip->vddhi);
	if (ret) {
			dev_err(&client->dev,
			"devname:%s func:%s regulator enable failed\n",
			id->name, __func__);
		goto err_gpio_request;
	}

	ret = iqs253_i2c_read_byte(iqs253_chip, 0);
	if (ret != IQS253_PROD_ID) {
			dev_err(&client->dev,
			"devname:%s func:%s device not present\n",
			id->name, __func__);
		goto err_gpio_request;

	}

	ret = regulator_disable(iqs253_chip->vddhi);
	if (ret) {
			dev_err(&client->dev,
			"devname:%s func:%s regulator disable failed\n",
			id->name, __func__);
		goto err_gpio_request;
	}

	stylus_detect = of_find_property(client->dev.of_node,
						"stylus-detect", NULL);
	if (!stylus_detect)
		goto finish;

	idev = iqs253_stylus_input_init(iqs253_chip);
	if (IS_ERR_OR_NULL(idev))
		goto err_gpio_request;
	iqs253_chip->idev = idev;

finish:

#if !defined(CONFIG_SENSORS_IQS253_AS_PROXIMITY)
	iqs253_chip->sar_wq = create_freezable_workqueue("iqs253_sar");
	if (!iqs253_chip->sar_wq) {
		dev_err(&iqs253_chip->client->dev, "unable to create work queue\n");
		goto err_gpio_request;
	}

	INIT_DELAYED_WORK(&iqs253_chip->sar_dw,
				iqs253_sar_proximity_detect_work);

	queue_delayed_work(iqs253_chip->sar_wq, &iqs253_chip->sar_dw, 0);
#endif

	dev_info(&client->dev, "devname:%s func:%s line:%d probe success\n",
			id->name, __func__, __LINE__);

	return 0;

err_gpio_request:
#if !defined(CONFIG_SENSORS_IQS253_AS_PROXIMITY)
	if (iqs253_chip->sar_wq)
		destroy_workqueue(iqs253_chip->sar_wq);
#endif
err_regulator_get:
#if defined(CONFIG_SENSORS_IQS253_AS_PROXIMITY)
	iio_device_unregister(indio_dev);
err_iio_register:
#endif
	iio_device_free(indio_dev);

	dev_err(&client->dev, "devname:%s func:%s line:%d probe failed\n",
			id->name, __func__, __LINE__);
	return ret;
}

static int iqs253_remove(struct i2c_client *client)
{
	struct iio_dev *indio_dev = i2c_get_clientdata(client);
	struct iqs253_chip *chip = iio_priv(indio_dev);
	gpio_free(chip->rdy_gpio);
#if !defined(CONFIG_SENSORS_IQS253_AS_PROXIMITY)
	if (chip->sar_wq)
		destroy_workqueue(chip->sar_wq);
#endif
	if (chip->wq)
		destroy_workqueue(chip->wq);
	if (chip->idev) {
		input_unregister_device(chip->idev);
		input_free_device(chip->idev);
	}
#if defined(CONFIG_SENSORS_IQS253_AS_PROXIMITY)
	iio_device_unregister(indio_dev);
#endif
	iio_device_free(indio_dev);
	return 0;
}

static void iqs253_shutdown(struct i2c_client *client)
{

}

static const struct i2c_device_id iqs253_id[] = {
	{"iqs253", 0},
	{}
};

MODULE_DEVICE_TABLE(i2c, iqs253_id);

static const struct of_device_id iqs253_of_match[] = {
	{ .compatible = "azoteq,iqs253", },
	{ },
};
MODULE_DEVICE_TABLE(of, iqs253_of_match);

static struct i2c_driver iqs253_driver = {
	.class = I2C_CLASS_HWMON,
	.driver = {
		.name = "iqs253",
		.owner = THIS_MODULE,
		.of_match_table = of_match_ptr(iqs253_of_match),
	},
	.probe = iqs253_probe,
	.remove = iqs253_remove,
	.shutdown = iqs253_shutdown,
	.id_table = iqs253_id,
};

module_i2c_driver(iqs253_driver);

MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("IQS253 Driver");
MODULE_AUTHOR("Sri Krishna chowdary <schowdary@nvidia.com>");