summaryrefstree
path: root/kernel/drivers/gpio/gpio-pcal6524.c
blob: 8fc1d2e3d7c0275f2ec8e64a6914e29749ecbe13 (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
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
/*
 *  pcal6524.c - 16 bit I/O port
 *
 *  Copyright (C) 2005 Ben Gardner <bgardner@wabtec.com>
 *  Copyright (C) 2007 Marvell International Ltd.
 *  Copyright (C) 2013 NXP Semiconductors
 *
 *  Derived from drivers/i2c/chips/pca953x.c
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; version 2 of the License.
 */

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/init.h>
#include <linux/i2c.h>
#include <linux/gpio.h>
#include <linux/interrupt.h>
#include <linux/irq.h>
#include <linux/platform_device.h>
#include <linux/delay.h>
#include <linux/err.h>
#include <linux/regulator/consumer.h>
#include <linux/debugfs.h>
#include <linux/seq_file.h>

#if defined(CONFIG_OF)
#include <linux/of_device.h>
#include <linux/of_gpio.h>
#endif
#include <linux/i2c/pcal6524.h>
#include <linux/sec_sysfs.h>

struct pcal6524_chip {
	struct i2c_client *client;
	struct gpio_chip gpio_chip;
	struct dentry	*dentry;
	struct mutex lock;
	struct pcal6524_platform_data *pdata;
	unsigned int gpio_start;

	uint8_t reg_output[3];
	uint8_t reg_polarity[3];
	uint8_t reg_config[3];
	uint8_t reg_drive0[2];
	uint8_t reg_drive1[2];
	uint8_t reg_drive2[2];
	uint8_t reg_inputlatch[3];
	uint8_t reg_enpullupdown[3];
	uint8_t reg_selpullupdown[3];
	//	uint8_t reg_intmask[3];
	uint8_t reg_outputconfig;
};

struct pcal6524_chip *g_dev;

static struct device *pcal6524_dev;

/* read the 8-bit register from the PCAL6524
 *  reg: register address
 *  val: the value read back from the PCAL6524
 */
static int pcal6524_read_reg(struct pcal6524_chip *chip, int reg, uint8_t *val)
{
	int ret = i2c_smbus_read_byte_data(chip->client, reg);

	if (ret < 0) {
		dev_err(&chip->client->dev,
				"failed reading register %d\n", ret);
		return ret;
	}

	*val = (uint8_t)ret;
	return 0;
}

/* write a 8-bit value to the PCAL6524
 *  reg: register address
 *  val: the value read back from the PCAL6524
 */
static int pcal6524_write_reg(struct pcal6524_chip *chip, int reg, uint8_t val)
{
	int ret = i2c_smbus_write_byte_data(chip->client, reg, val);

	if (ret < 0) {
		dev_err(&chip->client->dev,
				"failed writing register %d\n", ret);
		return ret;
	}

	return 0;
}

/* read a port pin value (INPUT register) from the PCAL6524
 *  off: bit number (0..23)
 *  return: bit value 0 or 1
 */
static int pcal6524_gpio_get_value(struct gpio_chip *gc, unsigned int off)
{
	int ret;
	int quot = off / 8;
	int rest = off % 8;
	uint8_t reg_val;

	struct pcal6524_chip *chip
		= container_of(gc, struct pcal6524_chip, gpio_chip);

	mutex_lock(&chip->lock);
	ret = pcal6524_read_reg(chip, PCAL6524_INPUT + quot, &reg_val);
	if (ret < 0) {
		/* NOTE:  diagnostic already emitted; that's all we should
		 * do unless gpio_*_value_cansleep() calls become different
		 * from their nonsleeping siblings (and report faults).
		 */
		mutex_unlock(&chip->lock);
		return ret;
	}

	mutex_unlock(&chip->lock);
	return (reg_val & (1u << rest)) ? 1 : 0;
}

/* write a port pin value (INPUT register) from the PCAL6524
 *  off: bit number (0..23)
 *  val: 0 or 1
 *  return: none
 */
static void pcal6524_gpio_set_value(struct gpio_chip *gc,
		unsigned int off, int val)
{
	int quot = off / 8;
	int rest = off % 8;
	struct pcal6524_chip *chip
		= container_of(gc, struct pcal6524_chip, gpio_chip);

	mutex_lock(&chip->lock);
	if (val)
		chip->reg_output[quot] |= (1u << rest);
	else
		chip->reg_output[quot] &= ~(1u << rest);

	pcal6524_write_reg(chip, PCAL6524_DAT_OUT + quot, chip->reg_output[quot]);
	mutex_unlock(&chip->lock);
}

/* set the CONFIGURATION register of a port pin as an input
 *  off: bit number (0..23)
 */
static int pcal6524_gpio_direction_input(struct gpio_chip *gc, unsigned int off)
{
	int ret;
	int quot = off / 8;
	int rest = off % 8;

	struct pcal6524_chip *chip
		= container_of(gc, struct pcal6524_chip, gpio_chip);

	pr_info("%s(off=[%d])\n", __func__, off);
	mutex_lock(&chip->lock);
	/* input set bit 1  */
	chip->reg_config[quot] |= (1u << rest);
	ret = pcal6524_write_reg(chip, PCAL6524_CONFIG + quot, chip->reg_config[quot]);
	mutex_unlock(&chip->lock);
	return ret;
}

/* set the DIRECTION (CONFIGURATION register) of a port pin as an output
 *  off: bit number (0..23)
 *  val = 1 or 0
 *  return: 0 if successful
 */
static int pcal6524_gpio_direction_output(struct gpio_chip *gc,
		unsigned int off, int val)
{
	int ret;
	int quot = off / 8;
	int rest = off % 8;

	struct pcal6524_chip *chip
		= container_of(gc, struct pcal6524_chip, gpio_chip);

	pr_info("%s(off=[%d], val=[%d])\n", __func__, off, val);
	mutex_lock(&chip->lock);
	/* set output level */
	if (val)
		chip->reg_output[quot] |= (1u << rest);
	else
		chip->reg_output[quot] &= ~(1u << rest);
	ret = pcal6524_write_reg(chip, PCAL6524_DAT_OUT + quot, chip->reg_output[quot]);

	/* then direction */
	/* output set bit 0  */
	chip->reg_config[quot] &= ~(1u << rest);
	ret |= pcal6524_write_reg(chip, PCAL6524_CONFIG + quot, chip->reg_config[quot]);
	mutex_unlock(&chip->lock);

	return ret;
}

static int pcal6524_gpio_request(struct gpio_chip *gc, unsigned int off)
{
	struct pcal6524_chip *chip
		= container_of(gc, struct pcal6524_chip, gpio_chip);

	pr_info("%s gpio %d\n", __func__, off);
	if (off >= chip->gpio_chip.ngpio) {
		pr_err("[%s] offset over max = [%d]\n", __func__, off);
		return 1;
	}
	/* to do*/
	return 0;
}

static void pcal6524_gpio_free(struct gpio_chip *gc, unsigned int off)
{
	struct pcal6524_chip *chip
		= container_of(gc, struct pcal6524_chip, gpio_chip);

	pr_info("%s gpio %d\n", __func__, off);
	if (off >= chip->gpio_chip.ngpio)
		pr_err("[%s] offset over max = [%d]\n", __func__, off);
	/* to do*/
}

static int pcal6524_gpio_setup(struct pcal6524_chip *dev)
{
	int ret, i;

	pr_info("[%s] GPIO Expander Init setting\n", __func__);

	dev->reg_outputconfig = 0x00;
	ret = pcal6524_write_reg(dev, PCAL6524_OUTPUT_CONFIG, dev->reg_outputconfig);		/* push-pull */
	if (ret < 0) {
		pr_err("failed set output config\n");
		return ret;
	}

	for (i = 0; i < PCAL6524_PORT_CNT; i++) {
		ret = pcal6524_write_reg(dev, PCAL6524_DAT_OUT + i, dev->reg_output[i]);
		/* 1 : output high, 0 : output low */
		if (ret < 0) {
			pr_err("failed set data out\n");
			return ret;
		}
	}
	for (i = 0; i < PCAL6524_PORT_CNT; i++) {
		ret = pcal6524_write_reg(dev, PCAL6524_CONFIG + i, dev->reg_config[i]);
		/* 1 : input, 0 : output */
		if (ret < 0) {
			pr_err("failed set config\n");
			return ret;
		}
	}

	for (i = 0; i < PCAL6524_PORT_CNT; i++) {
		dev->reg_polarity[i] = 0x00;
		ret = pcal6524_write_reg(dev, PCAL6524_POLARITY + i, dev->reg_polarity[i]);
		if (ret < 0) {
			pr_err("failed set polarity\n");
			return ret;
		}
	}

	for (i = 0; i < 2; i++) {
		dev->reg_drive0[i] = 0x00;
		ret = pcal6524_write_reg(dev, PCAL6524_DRIVE0 + i, dev->reg_drive0[i]);		/* drive 0.25x */
		if (ret < 0) {
			pr_err("failed set drive0\n");
			return ret;
		}
	}
	for (i = 0; i < 2; i++) {
		dev->reg_drive1[i] = 0x00;
		ret = pcal6524_write_reg(dev, PCAL6524_DRIVE1 + i, dev->reg_drive1[i]);		/* drive 0.25x */
		if (ret < 0) {
			pr_err("failed set drive1\n");
			return ret;
		}
	}
	for (i = 0; i < 2; i++) {
		dev->reg_drive2[i] = 0x00;
		ret = pcal6524_write_reg(dev, PCAL6524_DRIVE2 + i, dev->reg_drive2[i]);		/* drive 0.25x */
		if (ret < 0) {
			pr_err("failed set drive1\n");
			return ret;
		}
	}

	for (i = 0; i < PCAL6524_PORT_CNT; i++) {
		dev->reg_inputlatch[i] = 0x00;
		ret = pcal6524_write_reg(dev, PCAL6524_INPUT_LATCH + i, dev->reg_inputlatch[i]);
		/* not use latch */
		if (ret < 0) {
			pr_err("failed set input latch\n");
			return ret;
		}
	}

	for (i = 0; i < PCAL6524_PORT_CNT; i++) {
		ret = pcal6524_write_reg(dev, PCAL6524_SEL_PULLUPDOWN + i, dev->reg_selpullupdown[i]);
		/* 1 : pull-up, 0 : pull-down */
		if (ret < 0) {
			pr_err("failed set select pull\n");
			return ret;
		}
	}
	for (i = 0; i < PCAL6524_PORT_CNT; i++) {
		ret = pcal6524_write_reg(dev, PCAL6524_EN_PULLUPDOWN + i, dev->reg_enpullupdown[i]);
		/* 1 : enable, 0 : disable */
		if (ret < 0) {
			pr_err("failed set enable pullupdown\n");
			return ret;
		}
	}

	return 0;
}

#ifdef CONFIG_OF
static int pcal6524_parse_dt(struct device *dev,
		struct pcal6524_platform_data *pdata)
{
	struct device_node *np = dev->of_node;
	struct pinctrl *reset_pinctrl;
	int ret, i, j;
	u32 pull_reg[3];

	ret = of_property_read_u32(np, "pcal6524,gpio_start", &pdata->gpio_start);
	if (ret < 0) {
		pr_err("[%s]: Unable to read pcal6524,gpio_start\n", __func__);
		return ret;
	}

	ret = of_property_read_u32(np, "pcal6524,ngpio", &pdata->ngpio);
	if (ret < 0) {
		pr_err("[%s]: Unable to read pcal6524,ngpio\n", __func__);
		return ret;
	}
	pdata->reset_gpio = of_get_named_gpio(np, "pcal6524,reset-gpio", 0);
	/* Get pinctrl if target uses pinctrl */
	reset_pinctrl = devm_pinctrl_get_select(dev, "expander_reset_setting");
	if (IS_ERR(reset_pinctrl)) {
		if (PTR_ERR(reset_pinctrl) == -EPROBE_DEFER)
			return -EPROBE_DEFER;

		pr_debug("Target does not use pinctrl\n");
		reset_pinctrl = NULL;
	}

	ret = of_property_read_u32(np, "pcal6524,support_initialize", (u32 *)&pdata->support_init);
	if (ret < 0) {
		pr_err("[%s]: Unable to read pcal6524,support_init\n", __func__);
		pdata->support_init = 0;
	}

	if (pdata->support_init) {
		ret = of_property_read_u32(np, "pcal6524,config", (u32 *)&pdata->init_config);
		if (ret < 0) {
			pr_err("[%s]: Unable to read pcal6524,support_init\n", __func__);
			pdata->init_config = 0x000000;
		}
		ret = of_property_read_u32(np, "pcal6524,data_out", (u32 *)&pdata->init_data_out);
		if (ret < 0) {
			pr_err("[%s]: Unable to read pcal6524,support_init\n", __func__);
			pdata->init_data_out = 0x000000;
		}
		ret = of_property_read_u32(np, "pcal6524,pull_reg_p0", &pull_reg[0]);
		if (ret < 0)
			pr_err("[%s]: Unable to read pcal6524,pull_reg_p0\n", __func__);

		ret = of_property_read_u32(np, "pcal6524,pull_reg_p1", &pull_reg[1]);
		if (ret < 0)
			pr_err("[%s]: Unable to read pcal6524,pull_reg_p1\n", __func__);

		ret = of_property_read_u32(np, "pcal6524,pull_reg_p2", &pull_reg[2]);
		if (ret < 0)
			pr_err("[%s]: Unable to read pcal6524,pull_reg_p2\n", __func__);

		pr_info("[%s] Pull reg P0[0x%04x] P1[0x%04x] P2[0x%04x]\n", __func__,
				pull_reg[0], pull_reg[1], pull_reg[2]);
		pdata->init_en_pull = 0x000000;
		pdata->init_sel_pull = 0x000000;
		for (j = 0; j < 3; j++) {
			for (i = 0; i < 8; i++) {
				if (((pull_reg[j]>>(i*2))&0x3) == NO_PULL) {
					pdata->init_en_pull &= ~(1 << (i + (8 * j)));
					pdata->init_sel_pull &= ~(1 << (i + (8 * j)));
				} else if (((pull_reg[j]>>(i*2))&0x3) == PULL_DOWN) {
					pdata->init_en_pull |= (1 << (i + (8 * j)));
					pdata->init_sel_pull &= ~(1 << (i + (8 * j)));
				} else if (((pull_reg[j]>>(i*2))&0x3) == PULL_UP) {
					pdata->init_en_pull |= (1 << (i + (8 * j)));
					pdata->init_sel_pull |= (1 << (i + (8 * j)));
				}
			}
		}
	} else {
		pdata->init_config = 0x000000;
		pdata->init_data_out = 0x000000;
		pdata->init_en_pull = 0x000000;
		pdata->init_sel_pull = 0x000000;
	}

	pr_info("[%s] initialize reg 0x%06x 0x%06x 0x%06x 0x%06x\n", __func__,
			pdata->init_config, pdata->init_data_out,
			pdata->init_en_pull, pdata->init_sel_pull);
	dev->platform_data = pdata;
	pr_info("[%s]gpio_start=[%d]ngpio=[%d]reset-gpio=[%d]\n",
			__func__, pdata->gpio_start, pdata->ngpio,
			pdata->reset_gpio);
	return 0;
}
#endif

static int pcal6524_reset_chip(struct pcal6524_platform_data *pdata)
{
	int retval;
	int reset_gpio = pdata->reset_gpio;

	if (gpio_is_valid(reset_gpio)) {
		retval = gpio_request(reset_gpio, "pcal6524_reset_gpio");
		if (retval) {
			pr_err("[%s]: unable to request gpio [%d]\n",
					__func__, reset_gpio);
			return retval;
		}

		retval = gpio_direction_output(reset_gpio, 1);
		if (retval) {
			pr_err("[%s]: unable to set direction for gpio [%d]\n",
					__func__, reset_gpio);
			gpio_free(reset_gpio);
			return retval;
		}

		usleep_range(100, 100);
		gpio_set_value(reset_gpio, 0);
		usleep_range(100, 100);
		gpio_set_value(reset_gpio, 1);
		pr_info("[%s]: gpio expander reset.\n", __func__);

		gpio_free(reset_gpio);
		return 0;
	} else {
		pr_err("[%s]: gpio_is_valid fail\n", __func__);
		return -EIO;
	}
	return 0;
}

static ssize_t store_pcal6524_gpio_inout(struct device *dev,
		struct device_attribute *devattr,
		const char *buf, size_t count)
{
	int retval, off, val, gpio_pcal6524;
	char in_out, msg[13];
	struct pcal6524_chip *data = dev_get_drvdata(dev);

	retval = sscanf(buf, "%1c %3d %1d", &in_out, &off, &val);
	if (retval == 0) {
		dev_err(&data->client->dev, "[%s] fail to pcal6524 out.\n", __func__);
		return count;
	}
	if (!(in_out == 'i' || in_out == 'o')) {
		pr_err("[%s] wrong in_out value [%c]\n", __func__,  in_out);
		return count;
	}
	if ((off < 0) || (off > PCAL6524_MAX_GPIO)) {
		pr_err("[%s] wrong offset value [%d]\n", __func__, off);
		return count;
	}
	if (!(val == 0 || val == 1)) {
		pr_err("[%s] wrong val value [%d]\n", __func__, val);
		return count;
	}

	gpio_pcal6524 = data->gpio_start + off;
	snprintf(msg, sizeof(msg)/sizeof(char), "exp-gpio%d\n", off);
	if (gpio_is_valid(gpio_pcal6524)) {
		retval = gpio_request(gpio_pcal6524, msg);
		if (retval) {
			pr_err("[%s] unable to request gpio=[%d] err=[%d]\n",
					__func__, gpio_pcal6524, retval);
			return count;
		}

		if (in_out == 'i') {
			retval = gpio_direction_input(gpio_pcal6524);
			val = gpio_get_value(gpio_pcal6524);
		} else
			retval = gpio_direction_output(gpio_pcal6524, val);

		if (retval)
			pr_err("%s: unable to set direction for gpio [%d]\n",
					__func__, gpio_pcal6524);

		gpio_free(gpio_pcal6524);
	}

	pr_info("pcal6524 mode set to dir[%c], offset[%d], val[%d]\n", in_out, off, val);

	return count;
}

static ssize_t show_pcal6524_gpio_state(struct device *dev,
		struct device_attribute *devattr, char *buf)
{
	struct pcal6524_chip *chip = dev_get_drvdata(dev);
	struct pcal6524_chip chip_state;
	int i, drv_str;
	int quot = 0, rest = 0;
	uint8_t read_input[3];
	size_t size = 0;

	for (i = 0; i < PCAL6524_PORT_CNT; i++) {
		pcal6524_read_reg(chip, PCAL6524_INPUT + i, &read_input[i]);
		pcal6524_read_reg(chip, PCAL6524_DAT_OUT + i, &chip_state.reg_output[i]);
		pcal6524_read_reg(chip, PCAL6524_CONFIG + i, &chip_state.reg_config[i]);
		if (i < 2) {
			pcal6524_read_reg(chip, PCAL6524_DRIVE0 + i, &chip_state.reg_drive0[i]);
			pcal6524_read_reg(chip, PCAL6524_DRIVE1 + i, &chip_state.reg_drive1[i]);
			pcal6524_read_reg(chip, PCAL6524_DRIVE2 + i, &chip_state.reg_drive2[i]);
		}
		pcal6524_read_reg(chip, PCAL6524_EN_PULLUPDOWN + i, &chip_state.reg_enpullupdown[i]);
		pcal6524_read_reg(chip, PCAL6524_SEL_PULLUPDOWN + i, &chip_state.reg_selpullupdown[i]);
	}

	for (i = 0; i <= PCAL6524_MAX_GPIO; i++) {
		size += sprintf(&buf[size], "Expander[3%02d]", i);
		quot = i / 8;
		rest = i % 8;

		if ((chip_state.reg_config[quot] >> rest) & 0x1)
			size += sprintf(&buf[size], " IN");
		else {
			if ((chip_state.reg_output[quot] >> rest) & 0x1)
				size += sprintf(&buf[size], " OUT_HIGH");
			else
				size += sprintf(&buf[size], " OUT_LOW");
		}

		if ((chip_state.reg_enpullupdown[quot] >> rest) & 0x1) {
			if ((chip_state.reg_selpullupdown[quot] >> rest) & 0x1)
				size += sprintf(&buf[size], " PULL_UP");
			else
				size += sprintf(&buf[size], " PULL_DOWN");
		} else
			size += sprintf(&buf[size], " PULL_NONE");

		if (quot == 2) {
			if (rest > 3)
				drv_str = (chip_state.reg_drive2[1] >> ((rest-4)*2)) & 0x3;
			else
				drv_str = (chip_state.reg_drive2[0] >> ((rest)*2)) & 0x3;
		} else if (quot == 1) {
			if (rest > 3)
				drv_str = (chip_state.reg_drive1[1] >> ((rest-4)*2)) & 0x3;
			else
				drv_str = (chip_state.reg_drive1[0] >> ((rest)*2)) & 0x3;
		} else {
			if (rest > 3)
				drv_str = (chip_state.reg_drive0[1] >> ((rest-4)*2)) & 0x3;
			else
				drv_str = (chip_state.reg_drive0[0] >> ((rest)*2)) & 0x3;
		}

		switch (drv_str) {
		case GPIO_CFG_6_25MA:
			size += sprintf(&buf[size], " DRV_6.25mA");
			break;
		case GPIO_CFG_12_5MA:
			size += sprintf(&buf[size], " DRV_12.5mA");
			break;
		case GPIO_CFG_18_75MA:
			size += sprintf(&buf[size], " DRV_18.75mA");
			break;
		case GPIO_CFG_25MA:
			size += sprintf(&buf[size], " DRV_25mA");
			break;
		}

		if ((read_input[quot] >> rest) & 0x1)
			size += sprintf(&buf[size], " VAL_HIGH\n");
		else
			size += sprintf(&buf[size], " VAL_LOW\n");
	}

	return size;
}

static DEVICE_ATTR(expgpio, 0644,
		show_pcal6524_gpio_state, store_pcal6524_gpio_inout);

#ifdef CONFIG_SEC_PM_DEBUG
int expander_print_all(void)
{
	struct pcal6524_chip chip_state;
	int i, drv_str;
	int quot = 0, rest = 0;
	uint8_t read_input[3];

	if (!g_dev)
		return -ENODEV;

	for (i = 0; i < PCAL6524_PORT_CNT; i++) {
		pcal6524_read_reg(g_dev, PCAL6524_INPUT + i, &read_input[i]);
		pcal6524_read_reg(g_dev, PCAL6524_DAT_OUT + i, &chip_state.reg_output[i]);
		pcal6524_read_reg(g_dev, PCAL6524_CONFIG + i, &chip_state.reg_config[i]);
		if (i < 2) {
			pcal6524_read_reg(g_dev, PCAL6524_DRIVE0 + i, &chip_state.reg_drive0[i]);
			pcal6524_read_reg(g_dev, PCAL6524_DRIVE1 + i, &chip_state.reg_drive1[i]);
			pcal6524_read_reg(g_dev, PCAL6524_DRIVE2 + i, &chip_state.reg_drive2[i]);
		}
		pcal6524_read_reg(g_dev, PCAL6524_EN_PULLUPDOWN + i, &chip_state.reg_enpullupdown[i]);
		pcal6524_read_reg(g_dev, PCAL6524_SEL_PULLUPDOWN + i, &chip_state.reg_selpullupdown[i]);
	}

	for (i = 0; i <= PCAL6524_MAX_GPIO; i++) {
		pr_info("Expander[3%02d]\n", i);
		quot = i / 8;
		rest = i % 8;

		if ((chip_state.reg_config[quot] >> rest) & 0x1)
			pr_info("\tIN\n");
		else {
			if ((chip_state.reg_output[quot] >> rest) & 0x1)
				pr_info("\tOUT_HIGH\n");
			else
				pr_info("\tOUT_LOW\n");
		}

		if ((chip_state.reg_enpullupdown[quot] >> rest) & 0x1) {
			if ((chip_state.reg_selpullupdown[quot] >> rest) & 0x1)
				pr_info("\tPULL_UP\n");
			else
				pr_info("\tPULL_DOWN\n");
		} else
			pr_info("\tPULL_NONE\n");

		if (quot == 2) {
			if (rest > 3)
				drv_str = (chip_state.reg_drive2[1] >> ((rest-4)*2)) & 0x3;
			else
				drv_str = (chip_state.reg_drive2[0] >> ((rest)*2)) & 0x3;
		} else if (quot == 1) {
			if (rest > 3)
				drv_str = (chip_state.reg_drive1[1] >> ((rest-4)*2)) & 0x3;
			else
				drv_str = (chip_state.reg_drive1[0] >> ((rest)*2)) & 0x3;
		} else {
			if (rest > 3)
				drv_str = (chip_state.reg_drive0[1] >> ((rest-4)*2)) & 0x3;
			else
				drv_str = (chip_state.reg_drive0[0] >> ((rest)*2)) & 0x3;
		}

		switch (drv_str) {
		case GPIO_CFG_6_25MA:
			pr_info("\tDRV_6.25mA\n");
			break;
		case GPIO_CFG_12_5MA:
			pr_info("\tDRV_12.5mA\n");
			break;
		case GPIO_CFG_18_75MA:
			pr_info("\tDRV_18.75mA\n");
			break;
		case GPIO_CFG_25MA:
			pr_info("\tDRV_25mA\n");
			break;
		}

		if ((read_input[quot] >> rest) & 0x1)
			pr_info("\tVAL_HIGH\n");
		else
			pr_info("\tVAL_LOW\n");
	}

	return 0;
}
#endif

static int expander_show(struct seq_file *s, void *unused)
{
	struct pcal6524_chip chip_state;
	int i, drv_str;
	int quot = 0, rest = 0;
	uint8_t read_input[3];

	for (i = 0; i < PCAL6524_PORT_CNT; i++) {
		pcal6524_read_reg(g_dev, PCAL6524_INPUT + i, &read_input[i]);
		pcal6524_read_reg(g_dev, PCAL6524_DAT_OUT + i, &chip_state.reg_output[i]);
		pcal6524_read_reg(g_dev, PCAL6524_CONFIG + i, &chip_state.reg_config[i]);
		if (i < 2) {
			pcal6524_read_reg(g_dev, PCAL6524_DRIVE0 + i, &chip_state.reg_drive0[i]);
			pcal6524_read_reg(g_dev, PCAL6524_DRIVE1 + i, &chip_state.reg_drive1[i]);
			pcal6524_read_reg(g_dev, PCAL6524_DRIVE2 + i, &chip_state.reg_drive2[i]);
		}
		pcal6524_read_reg(g_dev, PCAL6524_EN_PULLUPDOWN + i, &chip_state.reg_enpullupdown[i]);
		pcal6524_read_reg(g_dev, PCAL6524_SEL_PULLUPDOWN + i, &chip_state.reg_selpullupdown[i]);
	}

	for (i = 0; i <= PCAL6524_MAX_GPIO; i++) {
		seq_printf(s, "Expander[3%02d]", i);
		quot = i / 8;
		rest = i % 8;

		if ((chip_state.reg_config[quot] >> rest) & 0x1)
			seq_puts(s, " IN");
		else {
			if ((chip_state.reg_output[quot] >> rest) & 0x1)
				seq_puts(s, " OUT_HIGH");
			else
				seq_puts(s, " OUT_LOW");
		}

		if ((chip_state.reg_enpullupdown[quot] >> rest) & 0x1) {
			if ((chip_state.reg_selpullupdown[quot] >> rest) & 0x1)
				seq_puts(s, " PULL_UP");
			else
				seq_puts(s, " PULL_DOWN");
		} else
			seq_puts(s, " PULL_NONE");

		if (quot == 2) {
			if (rest > 3)
				drv_str = (chip_state.reg_drive2[1] >> ((rest-4)*2)) & 0x3;
			else
				drv_str = (chip_state.reg_drive2[0] >> ((rest)*2)) & 0x3;
		} else if (quot == 1) {
			if (rest > 3)
				drv_str = (chip_state.reg_drive1[1] >> ((rest-4)*2)) & 0x3;
			else
				drv_str = (chip_state.reg_drive1[0] >> ((rest)*2)) & 0x3;
		} else {
			if (rest > 3)
				drv_str = (chip_state.reg_drive0[1] >> ((rest-4)*2)) & 0x3;
			else
				drv_str = (chip_state.reg_drive0[0] >> ((rest)*2)) & 0x3;
		}

		switch (drv_str) {
		case GPIO_CFG_6_25MA:
			seq_puts(s, " DRV_6.25mA");
			break;
		case GPIO_CFG_12_5MA:
			seq_puts(s, " DRV_12.5mA");
			break;
		case GPIO_CFG_18_75MA:
			seq_puts(s, " DRV_18.75mA");
			break;
		case GPIO_CFG_25MA:
			seq_puts(s, " DRV_25mA");
			break;
		}

		if ((read_input[quot] >> rest) & 0x1)
			seq_puts(s, " VAL_HIGH\n");
		else
			seq_puts(s, " VAL_LOW\n");
	}

	return 0;
}
static int expander_debug_open(struct inode *inode, struct file *file)
{
	return single_open(file, expander_show, NULL);
}

static const struct file_operations expander_operations = {
	.open		= expander_debug_open,
	.read		= seq_read,
	.llseek		= seq_lseek,
	.release	= single_release,
};

static int pcal6524_gpio_probe(struct i2c_client *client,
		const struct i2c_device_id *id)
{
	struct device_node *np = client->dev.of_node;
	struct pcal6524_platform_data *pdata = NULL;
	struct pcal6524_chip *dev;
	struct gpio_chip *gc;
	struct dentry *debugfs_file;
	int ret, i;
	int retry;

	pr_info("[%s]\n", __func__);
	if (!i2c_check_functionality(client->adapter,
				I2C_FUNC_SMBUS_BYTE_DATA)) {
		dev_err(&client->dev, "SMBUS Byte Data not Supported\n");
		return -EIO;
	}
#ifdef CONFIG_OF
	if (np) {
		pdata = devm_kzalloc(&client->dev,
				sizeof(struct pcal6524_platform_data),
				GFP_KERNEL);
		if (!pdata) {
			dev_err(&client->dev, "Failed to allocate memory\n");
			return -ENOMEM;
		}

		ret = pcal6524_parse_dt(&client->dev, pdata);
		if (ret) {
			pr_err("[%s] pcal6524 parse dt failed\n", __func__);
			return ret;
		}

	} else {
		pdata = client->dev.platform_data;
		pr_info("GPIO Expender failed to align dtsi %s",
				__func__);
	}
#else
	pdata = client->dev.platform_data;
#endif
	if (pdata == NULL) {
		dev_err(&client->dev, "missing platform data\n");
		return -ENODEV;
	}

	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
	if (dev == NULL) {
		dev_err(&client->dev, "failed to alloc memory\n");
		return -ENOMEM;
	}

	dev->client = client;
	dev->pdata = pdata;
	dev->gpio_start = pdata->gpio_start;

	gc = &dev->gpio_chip;
	gc->direction_input  = pcal6524_gpio_direction_input;
	gc->direction_output = pcal6524_gpio_direction_output;
	gc->get = pcal6524_gpio_get_value;
	gc->set = pcal6524_gpio_set_value;
	gc->request = pcal6524_gpio_request;
	gc->free = pcal6524_gpio_free;
	gc->can_sleep = 0;
	gc->parent = &client->dev;

	gc->base = pdata->gpio_start;
	gc->ngpio = pdata->ngpio;
	gc->label = client->name;
	gc->owner = THIS_MODULE;

	mutex_init(&dev->lock);

	for (i = 0; i < PCAL6524_PORT_CNT; i++) {
		dev->reg_config[i] = (pdata->init_config >> (8*i)) & 0xFF;
		dev->reg_output[i] = (pdata->init_data_out >> (8*i)) & 0xFF;
		dev->reg_enpullupdown[i] = (pdata->init_en_pull >> (8*i)) & 0xFF;
		dev->reg_selpullupdown[i] = (pdata->init_sel_pull >> (8*i)) & 0xFF;
	}

	retry = 0;
	while (1) {
		ret = pcal6524_reset_chip(pdata);
		if (ret) {
			pr_err("[%s]reset control fail\n", __func__);
		} else {
			ret = pcal6524_gpio_setup(dev);
			if (ret) {
				dev_err(&client->dev,
						"expander setup i2c retry [%d]\n", retry);
			} else {
				pr_info("[%s]Expander setup success [%d]\n",
						__func__, retry);
				break;
			}
		}
		if (retry++ > 5) {
			dev_err(&client->dev,
					"Failed to expander retry[%d]\n", retry);

			panic("pcal6524 i2c fail, check HW!\n");

			goto err;

		}
		usleep_range(100, 200);
	}

	ret = gpiochip_add(&dev->gpio_chip);
	if (ret)
		goto err;

	dev_info(&client->dev, "gpios %d..%d on a %s\n",
			gc->base, gc->base + gc->ngpio - 1,
			client->name);

	pcal6524_dev = sec_device_create(dev, "expander");
	if (IS_ERR(pcal6524_dev)) {
		dev_err(&client->dev,
				"Failed to create device for expander\n");
		ret = -ENODEV;
		goto err;
	}

	ret = sysfs_create_file(&pcal6524_dev->kobj, &dev_attr_expgpio.attr);
	if (ret) {
		dev_err(&client->dev,
				"Failed to create sysfs group for expander\n");
		goto err_destroy;
	}

	dev->dentry = debugfs_create_dir("expander", NULL);
	if (IS_ERR_OR_NULL(dev->dentry)) {
		dev_err(&client->dev,
				"Failed to create debugfs dir for expander\n");
		goto err_debug_dir;

	}
	debugfs_file = debugfs_create_file("gpio", S_IFREG | 0444,
			dev->dentry, NULL, &expander_operations);
	if (IS_ERR_OR_NULL(debugfs_file)) {
		dev_err(&client->dev,
				"Failed to create debugfs file for gpio\n");
		goto err_debug_file;
	}

	i2c_set_clientdata(client, dev);
	g_dev = dev;

	return 0;

err_debug_file:
	debugfs_remove_recursive(dev->dentry);
err_debug_dir:
	sysfs_remove_file(&pcal6524_dev->kobj, &dev_attr_expgpio.attr);
err_destroy:
	sec_device_destroy(0);
	return ret;
err:
	mutex_destroy(&dev->lock);
	kfree(dev);
	return ret;
}

static int pcal6524_gpio_remove(struct i2c_client *client)
{
	struct pcal6524_chip *dev = i2c_get_clientdata(client);

	gpiochip_remove(&dev->gpio_chip);
	if (!IS_ERR_OR_NULL(dev->dentry))
		debugfs_remove_recursive(dev->dentry);
	sysfs_remove_file(&pcal6524_dev->kobj, &dev_attr_expgpio.attr);
	mutex_destroy(&dev->lock);
	kfree(dev);
	return 0;
}

#ifdef CONFIG_OF
static const struct of_device_id pcal6524_dt_ids[] = {
	{ .compatible = "pcal6524,gpio-expander",},
	{ /* sentinel */ },
};
#endif
static const struct i2c_device_id pcal6524_gpio_id[] = {
	{DRV_NAME, 0, },
	{ }
};
MODULE_DEVICE_TABLE(i2c, pcal6524_gpio_id);

static struct i2c_driver pcal6524_gpio_driver = {
	.driver = {
		.name = DRV_NAME,
#ifdef CONFIG_OF
		.of_match_table = of_match_ptr(pcal6524_dt_ids),
#endif
	},
	.probe = pcal6524_gpio_probe,
	.remove = pcal6524_gpio_remove,
	.id_table = pcal6524_gpio_id,
};

static int __init pcal6524_gpio_init(void)
{
	return i2c_add_driver(&pcal6524_gpio_driver);
}

subsys_initcall(pcal6524_gpio_init);

static void __exit pcal6524_gpio_exit(void)
{
	i2c_del_driver(&pcal6524_gpio_driver);
}

module_exit(pcal6524_gpio_exit);

MODULE_DESCRIPTION("GPIO expander driver for PCAL6524");
MODULE_LICENSE("GPL");