summaryrefslogtreecommitdiff
path: root/drivers/mfd/apalis-tk1-k20.c
blob: e794bda6fde7e08c87b450dc7077bdd525997afc (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
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
/*
 * Copyright 2016-2017 Toradex AG
 * Dominik Sliwa <dominik.sliwa@toradex.com>
 *
 * based on an driver for MC13xxx by:
 * Copyright 2009-2010 Pengutronix
 * Uwe Kleine-Koenig <u.kleine-koenig@pengutronix.de>
 *
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License version 2 as published by the
 * Free Software Foundation.
 */

#include <linux/slab.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/interrupt.h>
#include <linux/mfd/core.h>
#include <linux/mfd/apalis-tk1-k20.h>
#include <linux/of.h>
#include <linux/of_device.h>
#include <linux/of_gpio.h>
#include <linux/of_irq.h>
#include <linux/err.h>
#include <linux/firmware.h>
#include <linux/spi/spi.h>
#include <linux/delay.h>

#include "apalis-tk1-k20-ezp.h"
#undef CONFIG_EXPERIMENTAL_K20_HSMODE
#define CONFIG_V12_K20_HSMODE
#define APALIS_TK1_K20_MAX_MSG 4
static const struct spi_device_id apalis_tk1_k20_device_ids[] = {
	{
		.name = "apalis-tk1-k20",
	}, {
		/* sentinel */
	}
};
MODULE_DEVICE_TABLE(spi, apalis_tk1_k20_device_ids);

static const struct of_device_id apalis_tk1_k20_dt_ids[] = {
	{
		.compatible = "toradex,apalis-tk1-k20",
	}, {
		/* sentinel */
	}
};
MODULE_DEVICE_TABLE(of, apalis_tk1_k20_dt_ids);

static const struct regmap_config apalis_tk1_k20_regmap_spi_config = {
	.reg_bits = 8,
	.pad_bits = 0,
	.val_bits = 8,

	.max_register = APALIS_TK1_K20_NUMREGS,

	.cache_type = REGCACHE_NONE,
#if defined(CONFIG_EXPERIMENTAL_K20_HSMODE) || defined(CONFIG_V12_K20_HSMODE)
	.use_single_rw = 0,
#else
	.use_single_rw = 1,
#endif

};

static int apalis_tk1_k20_spi_read(void *context, const void *reg,
		size_t reg_size, void *val, size_t val_size)
{
	unsigned char w[APALIS_TK1_K20_MAX_BULK] = {APALIS_TK1_K20_READ_INST,
			 *((unsigned char *) reg), val_size, 0x00, 0x00, 0x00,
			0x00};
	unsigned char r[APALIS_TK1_K20_MAX_BULK];
	unsigned char *p = val;
	struct device *dev = context;
	struct spi_device *spi = to_spi_device(dev);
	struct spi_transfer t = {
		.tx_buf = w,
		.rx_buf = r,
		.len = 8,
		.cs_change = 0,
		.delay_usecs = 0,
	};

	struct spi_message m;
	int ret;
	spi->mode = SPI_MODE_1;

	if (reg_size != 1)
		return -ENOTSUPP;

	if (val_size == 1) {
		spi_message_init(&m);
		spi_message_add_tail(&t, &m);
		ret = spi_sync(spi, &m);

		for (int i = 3; i < 7; i++ )
		{
			if (((unsigned char *)t.rx_buf)[i] == 0x55) {
				*p = ((unsigned char *)t.rx_buf)[i + 1];
				return ret;
			}
		}

		for (int j = 0; j < APALIS_TK1_MAX_RETRY_CNT; j++) {
			udelay(250 * j * j);
			t.tx_buf = w;
			t.rx_buf = r;
			spi_message_init(&m);
			spi_message_add_tail(&t, &m);
			ret = spi_sync(spi, &m);
			for (int i = 3; i < 7; i++ )
			{
				if (((unsigned char *)t.rx_buf)[i] == 0x55) {
					*p = ((unsigned char *)t.rx_buf)[i + 1];
					return ret;
				}
			}
		}
		ret = -EIO;

#ifdef CONFIG_V12_K20_HSMODE
	} else if ((val_size > 1) && (val_size < APALIS_TK1_K20_MAX_BULK)) {
		t.len = 5;
		w[0] = APALIS_TK1_K20_BULK_READ_INST;
		spi_message_init(&m);
		spi_message_add_tail(&t, &m);
		ret = spi_sync(spi, &m);
		/* no need to reinit the message*/
		t.len = val_size;
		t.rx_buf = p;
		/* just use the same transfer */
		ret = spi_sync(spi, &m);

#endif
	} else {
		return -ENOTSUPP;
	}

	return ret;
}


static int apalis_tk1_k20_spi_write(void *context, const void *data,
		size_t count)
{
	struct device *dev = context;
	struct spi_device *spi = to_spi_device(dev);
	uint8_t out_data[APALIS_TK1_K20_MAX_BULK];
	int ret;


	spi->mode = SPI_MODE_1;

	if (count == 2) {
		out_data[0] = APALIS_TK1_K20_WRITE_INST;
		out_data[1] = ((uint8_t *)data)[0];
		out_data[2] = ((uint8_t *)data)[1];
		ret = spi_write(spi, out_data, 3);

#ifdef CONFIG_V12_K20_HSMODE
	} else if ((count > 2) && (count < APALIS_TK1_K20_MAX_BULK)) {
		out_data[0] = APALIS_TK1_K20_BULK_WRITE_INST;
		out_data[1] = ((uint8_t *)data)[0];
		out_data[2] = count - 1;
		memcpy(&out_data[3], &((uint8_t *)data)[1], count - 1);
		ret = spi_write(spi, out_data, count + 2);
#endif
	} else {
		dev_err(dev, "Apalis TK1 K20 MFD invalid write count = %d\n",
				count);
		return -ENOTSUPP;
	}
	return ret;
}

static struct regmap_bus regmap_apalis_tk1_k20_bus = {
	.write = apalis_tk1_k20_spi_write,
	.read = apalis_tk1_k20_spi_read,
};

void apalis_tk1_k20_lock(struct apalis_tk1_k20_regmap *apalis_tk1_k20)
{
	if (!mutex_trylock(&apalis_tk1_k20->lock)) {
		dev_dbg(apalis_tk1_k20->dev, "wait for %s from %ps\n",
				__func__, __builtin_return_address(0));

		mutex_lock(&apalis_tk1_k20->lock);
	}
	dev_dbg(apalis_tk1_k20->dev, "%s from %ps\n",
			__func__, __builtin_return_address(0));
}
EXPORT_SYMBOL(apalis_tk1_k20_lock);

void apalis_tk1_k20_unlock(struct apalis_tk1_k20_regmap *apalis_tk1_k20)
{
	dev_dbg(apalis_tk1_k20->dev, "%s from %ps\n",
			__func__, __builtin_return_address(0));
	mutex_unlock(&apalis_tk1_k20->lock);
}
EXPORT_SYMBOL(apalis_tk1_k20_unlock);

int apalis_tk1_k20_reg_read(struct apalis_tk1_k20_regmap *apalis_tk1_k20,
		unsigned int offset, u32 *val)
{
	int ret;

	ret = regmap_read(apalis_tk1_k20->regmap, offset, val);
	dev_vdbg(apalis_tk1_k20->dev, "[0x%02x] -> 0x%06x\n", offset, *val);

	return ret;
}
EXPORT_SYMBOL(apalis_tk1_k20_reg_read);

int apalis_tk1_k20_reg_write(struct apalis_tk1_k20_regmap *apalis_tk1_k20,
		unsigned int offset, u32 val)
{
	dev_vdbg(apalis_tk1_k20->dev, "[0x%02x] <- 0x%06x\n", offset, val);

	if (val >= BIT(24))
		return -EINVAL;

	return regmap_write(apalis_tk1_k20->regmap, offset, val);
}
EXPORT_SYMBOL(apalis_tk1_k20_reg_write);

int apalis_tk1_k20_reg_read_bulk(struct apalis_tk1_k20_regmap *apalis_tk1_k20,
		unsigned int offset,
		uint8_t *val, size_t size)
{
	int ret;

	if (size > APALIS_TK1_K20_MAX_BULK)
		return -EINVAL;

	ret = regmap_bulk_read(apalis_tk1_k20->regmap, offset, val, size);
	dev_vdbg(apalis_tk1_k20->dev, "bulk read %d bytes from [0x%02x]\n",
			size, offset);

	return ret;
}
EXPORT_SYMBOL(apalis_tk1_k20_reg_read_bulk);

int apalis_tk1_k20_reg_write_bulk(struct apalis_tk1_k20_regmap *apalis_tk1_k20,
		unsigned int offset, uint8_t *buf, size_t size)
{
	dev_vdbg(apalis_tk1_k20->dev, "bulk write %d bytes to [0x%02x]\n",
			(unsigned int)size, offset);

	if (size > APALIS_TK1_K20_MAX_BULK)
		return -EINVAL;
	return regmap_bulk_write(apalis_tk1_k20->regmap, offset, buf, size);
}
EXPORT_SYMBOL(apalis_tk1_k20_reg_write_bulk);

int apalis_tk1_k20_reg_rmw(struct apalis_tk1_k20_regmap *apalis_tk1_k20,
		unsigned int offset, u32 mask, u32 val)
{
	dev_vdbg(apalis_tk1_k20->dev, "[0x%02x] <- 0x%06x (mask: 0x%06x)\n",
			offset, val, mask);

	return regmap_update_bits(apalis_tk1_k20->regmap, offset, mask, val);
}
EXPORT_SYMBOL(apalis_tk1_k20_reg_rmw);

int apalis_tk1_k20_irq_mask(struct apalis_tk1_k20_regmap *apalis_tk1_k20,
		int irq)
{
	int virq = -1;
	if (irq != APALIS_TK1_K20_CAN1_IRQ && irq != APALIS_TK1_K20_CAN0_IRQ) {
		virq = regmap_irq_get_virq(apalis_tk1_k20->irq_data, irq);

	} else {
		virq = (irq == APALIS_TK1_K20_CAN0_IRQ) ?
			apalis_tk1_k20->can0_irq:apalis_tk1_k20->can1_irq;
	}

	disable_irq_nosync(virq);

	return 0;
}
EXPORT_SYMBOL(apalis_tk1_k20_irq_mask);

int apalis_tk1_k20_irq_unmask(struct apalis_tk1_k20_regmap *apalis_tk1_k20,
		int irq)
{
	int virq = -1;
	if (irq != APALIS_TK1_K20_CAN1_IRQ && irq != APALIS_TK1_K20_CAN0_IRQ) {
		virq = regmap_irq_get_virq(apalis_tk1_k20->irq_data, irq);

	} else {
		virq = (irq == APALIS_TK1_K20_CAN0_IRQ) ?
			apalis_tk1_k20->can0_irq:apalis_tk1_k20->can1_irq;
	}

	enable_irq(virq);

	return 0;
}
EXPORT_SYMBOL(apalis_tk1_k20_irq_unmask);

int apalis_tk1_k20_irq_status(struct apalis_tk1_k20_regmap *apalis_tk1_k20,
		int irq, int *enabled, int *pending)
{
	int ret;
	unsigned int offmask = APALIS_TK1_K20_MSQREG;
	unsigned int offstat = APALIS_TK1_K20_IRQREG;
	u32 irqbit = 1 << irq;

	if (irq < 0 || irq >= ARRAY_SIZE(apalis_tk1_k20->irqs))
		return -EINVAL;

	if (enabled) {
		u32 mask;

		ret = apalis_tk1_k20_reg_read(apalis_tk1_k20, offmask, &mask);
		if (ret)
			return ret;

		*enabled = mask & irqbit;
	}

	if (pending) {
		u32 stat;

		ret = apalis_tk1_k20_reg_read(apalis_tk1_k20, offstat, &stat);
		if (ret)
			return ret;

		*pending = stat & irqbit;
	}

	return 0;
}
EXPORT_SYMBOL(apalis_tk1_k20_irq_status);

int apalis_tk1_k20_irq_request(struct apalis_tk1_k20_regmap *apalis_tk1_k20,
		int irq, irq_handler_t handler, const char *name, void *dev)
{
	int virq = -1;
	int irq_flags = IRQF_ONESHOT;
	if (irq != APALIS_TK1_K20_CAN1_IRQ && irq != APALIS_TK1_K20_CAN0_IRQ) {
		virq = regmap_irq_get_virq(apalis_tk1_k20->irq_data, irq);
		irq_flags = IRQF_ONESHOT;
	} else {
		virq = (irq == APALIS_TK1_K20_CAN0_IRQ) ?
			apalis_tk1_k20->can0_irq:apalis_tk1_k20->can1_irq;
		irq_flags = IRQF_ONESHOT | IRQF_TRIGGER_FALLING |
				IRQF_TRIGGER_RISING;
	}
	return devm_request_threaded_irq(apalis_tk1_k20->dev, virq,
			 NULL, handler, irq_flags, name, dev);
}
EXPORT_SYMBOL(apalis_tk1_k20_irq_request);

int apalis_tk1_k20_irq_free(struct apalis_tk1_k20_regmap *apalis_tk1_k20,
		int irq, void *dev)
{
	int virq = -1;
	if (irq != APALIS_TK1_K20_CAN1_IRQ && irq != APALIS_TK1_K20_CAN0_IRQ) {
		virq = regmap_irq_get_virq(apalis_tk1_k20->irq_data, irq);

	} else {
		virq = (irq == APALIS_TK1_K20_CAN0_IRQ) ?
			apalis_tk1_k20->can0_irq:apalis_tk1_k20->can1_irq;
	}

	devm_free_irq(apalis_tk1_k20->dev, virq, dev);

	return 0;
}
EXPORT_SYMBOL(apalis_tk1_k20_irq_free);


static int apalis_tk1_k20_add_subdevice_pdata_id(
		struct apalis_tk1_k20_regmap *apalis_tk1_k20, const char *name,
		void *pdata, size_t pdata_size, int id)
{
	struct mfd_cell cell = {
		.platform_data = pdata,
		.pdata_size = pdata_size,
	};

	cell.name = kmemdup(name, strlen(name) + 1, GFP_KERNEL);
	if (!cell.name)
		return -ENOMEM;

	return mfd_add_devices(apalis_tk1_k20->dev, id, &cell, 1, NULL, 0,
			       regmap_irq_get_domain(apalis_tk1_k20->irq_data));
}

static int apalis_tk1_k20_add_subdevice_pdata(
		struct apalis_tk1_k20_regmap *apalis_tk1_k20, const char *name,
		void *pdata, size_t pdata_size)
{
	return apalis_tk1_k20_add_subdevice_pdata_id(apalis_tk1_k20, name,
			pdata, pdata_size, -1);
}

static int apalis_tk1_k20_add_subdevice(
		struct apalis_tk1_k20_regmap *apalis_tk1_k20, const char *name)
{
	return apalis_tk1_k20_add_subdevice_pdata(apalis_tk1_k20, name, NULL,
						  0);
}

static void apalis_tk1_k20_reset_chip(
		struct apalis_tk1_k20_regmap *apalis_tk1_k20)
{
	udelay(10);
	gpio_set_value(apalis_tk1_k20->reset_gpio, 0);
	msleep(10);
	gpio_set_value(apalis_tk1_k20->reset_gpio, 1);
	udelay(10);
}

#ifdef CONFIG_APALIS_TK1_K20_EZP
static int apalis_tk1_k20_read_ezport(
		struct apalis_tk1_k20_regmap *apalis_tk1_k20, uint8_t command,
		int addr, int count, uint8_t *buffer)
{
	uint8_t w[4 + APALIS_TK1_K20_EZP_MAX_DATA];
	uint8_t r[4 + APALIS_TK1_K20_EZP_MAX_DATA];
	uint8_t *out;
	struct spi_message m;
	struct spi_device *spi = to_spi_device(apalis_tk1_k20->dev);
	struct spi_transfer t = {
		.tx_buf = w,
		.rx_buf = r,
		.speed_hz = APALIS_TK1_K20_EZP_MAX_SPEED,
	};
	int ret;

	spi->mode = SPI_MODE_0;

	if (count > APALIS_TK1_K20_EZP_MAX_DATA)
		return -ENOSPC;

	memset(w, 0, 4 + count);

	switch (command) {
	case APALIS_TK1_K20_EZP_READ:
	case APALIS_TK1_K20_EZP_FREAD:
		t.len = 4 + count;
		w[1] = (addr & 0xFF0000) >> 16;
		w[2] = (addr & 0xFF00) >> 8;
		w[3] = (addr & 0xFC);
		out = &r[4];
		break;
	case APALIS_TK1_K20_EZP_RDSR:
	case APALIS_TK1_K20_EZP_FRDFCOOB:
		t.len = 1 + count;
		out = &r[1];
		break;
	default:
		return -EINVAL;
	}
	w[0] = command;

	gpio_set_value(apalis_tk1_k20->ezpcs_gpio, 0);
	spi_message_init(&m);
	spi_message_add_tail(&t, &m);
	ret = spi_sync(spi, &m);
	gpio_set_value(apalis_tk1_k20->ezpcs_gpio, 1);
	if (ret != 0)
		return ret;

	memcpy(buffer, out, count);

	return 0;
}

static int apalis_tk1_k20_write_ezport(
		struct apalis_tk1_k20_regmap *apalis_tk1_k20, uint8_t command,
		int addr, int count, const uint8_t *buffer)
{
	uint8_t w[4 + APALIS_TK1_K20_EZP_MAX_DATA];
	uint8_t r[4 + APALIS_TK1_K20_EZP_MAX_DATA];
	struct spi_message m;
	struct spi_device *spi = to_spi_device(apalis_tk1_k20->dev);
	struct spi_transfer t = {
		.tx_buf = w,
		.rx_buf = r,
		.speed_hz = APALIS_TK1_K20_EZP_MAX_SPEED,
	};
	int ret;

	spi->mode = SPI_MODE_0;

	if (count > APALIS_TK1_K20_EZP_MAX_DATA)
		return -ENOSPC;

	switch (command) {
	case APALIS_TK1_K20_EZP_SP:
	case APALIS_TK1_K20_EZP_SE:
		t.len = 4 + count;
		w[1] = (addr & 0xFF0000) >> 16;
		w[2] = (addr & 0xFF00) >> 8;
		w[3] = (addr & 0xF8);
		memcpy(&w[4], buffer, count);
		break;
	case APALIS_TK1_K20_EZP_WREN:
	case APALIS_TK1_K20_EZP_WRDI:
	case APALIS_TK1_K20_EZP_BE:
	case APALIS_TK1_K20_EZP_RESET:
	case APALIS_TK1_K20_EZP_WRFCCOB:
		t.len = 1 + count;
		memcpy(&w[1], buffer, count);
		break;
	default:
		return -EINVAL;
	}
	w[0] = command;

	gpio_set_value(apalis_tk1_k20->ezpcs_gpio, 0);
	spi_message_init(&m);
	spi_message_add_tail(&t, &m);
	ret = spi_sync(spi, &m);
	gpio_set_value(apalis_tk1_k20->ezpcs_gpio, 1);

	return ret;
}

static int apalis_tk1_k20_set_wren_ezport(
		struct apalis_tk1_k20_regmap *apalis_tk1_k20)
{
	uint8_t buffer;

	if (apalis_tk1_k20_write_ezport(apalis_tk1_k20, APALIS_TK1_K20_EZP_WREN,
			0, 0, NULL) < 0)
		return -EIO;
	if (apalis_tk1_k20_read_ezport(apalis_tk1_k20, APALIS_TK1_K20_EZP_RDSR,
			0, 1, &buffer) < 0)
		return -EIO;
	if ((buffer & APALIS_TK1_K20_EZP_STA_WEN))
		return 0;

	/* If it failed try one last time */
	if (apalis_tk1_k20_write_ezport(apalis_tk1_k20, APALIS_TK1_K20_EZP_WREN,
			0, 0, NULL) < 0)
		return -EIO;
	if (apalis_tk1_k20_read_ezport(apalis_tk1_k20, APALIS_TK1_K20_EZP_RDSR,
			0, 1, &buffer) < 0)
		return -EIO;
	if ((buffer & APALIS_TK1_K20_EZP_STA_WEN))
		return 0;

	return -EIO;

}

static int apalis_tk1_k20_enter_ezport(
		struct apalis_tk1_k20_regmap *apalis_tk1_k20)
{
	uint8_t status = 0x00;
	uint8_t buffer;

	gpio_set_value(apalis_tk1_k20->ezpcs_gpio, 0);
	msleep(10);
	apalis_tk1_k20_reset_chip(apalis_tk1_k20);
	msleep(10);
	gpio_set_value(apalis_tk1_k20->ezpcs_gpio, 1);
	if (apalis_tk1_k20_read_ezport(apalis_tk1_k20, APALIS_TK1_K20_EZP_RDSR,
			0, 1, &buffer) < 0)
		goto bad;
	status = buffer;
	if (apalis_tk1_k20_write_ezport(apalis_tk1_k20, APALIS_TK1_K20_EZP_WREN,
			0, 0, NULL) < 0)
		goto bad;
	if (apalis_tk1_k20_read_ezport(apalis_tk1_k20, APALIS_TK1_K20_EZP_RDSR,
			0, 1, &buffer) < 0)
		goto bad;
	if ((buffer & APALIS_TK1_K20_EZP_STA_WEN) && buffer != status)
		return 0;

bad:
	dev_err(apalis_tk1_k20->dev, "Error entering EZ Port mode.\n");
	return -EIO;
}

static int apalis_tk1_k20_erase_chip_ezport(
		struct apalis_tk1_k20_regmap *apalis_tk1_k20)
{
	uint8_t buffer[16];
	int i;

	if (apalis_tk1_k20_set_wren_ezport(apalis_tk1_k20))
		goto bad;
	if (apalis_tk1_k20_write_ezport(apalis_tk1_k20, APALIS_TK1_K20_EZP_BE,
			0, 0, NULL) < 0)
		goto bad;

	if (apalis_tk1_k20_read_ezport(apalis_tk1_k20, APALIS_TK1_K20_EZP_RDSR,
			0, 1, buffer) < 0)
		goto bad;

	i = 0;
	while (buffer[0] & APALIS_TK1_K20_EZP_STA_WIP) {
		msleep(20);
		if ((apalis_tk1_k20_read_ezport(apalis_tk1_k20,
		     APALIS_TK1_K20_EZP_RDSR, 0, 1, buffer) < 0) || (i > 50))
			goto bad;
		i++;
	}

	return 0;

bad:
	dev_err(apalis_tk1_k20->dev, "Error erasing the chip.\n");
	return -EIO;
}

static int apalis_tk1_k20_flash_chip_ezport(
		struct apalis_tk1_k20_regmap *apalis_tk1_k20)
{
	uint8_t buffer;
	const uint8_t *fw_chunk;
	int i, j, transfer_size;

	for (i = 0; i < fw_entry->size;) {
		if (apalis_tk1_k20_set_wren_ezport(apalis_tk1_k20))
			goto bad;

		fw_chunk = fw_entry->data + i;
		transfer_size = (i + APALIS_TK1_K20_EZP_WRITE_SIZE <
				 fw_entry->size) ? APALIS_TK1_K20_EZP_WRITE_SIZE
				 : (fw_entry->size - i - 1);
		dev_vdbg(apalis_tk1_k20->dev,
			 "Apalis TK1 K20 MFD transfer_size = %d addr = 0x%X\n",
				 transfer_size, i);
		if (apalis_tk1_k20_write_ezport(apalis_tk1_k20,
				APALIS_TK1_K20_EZP_SP, i,
			transfer_size, fw_chunk) < 0)
			goto bad;
		udelay(2000);
		if (apalis_tk1_k20_read_ezport(apalis_tk1_k20,
				APALIS_TK1_K20_EZP_RDSR, 0, 1, &buffer) < 0)
			goto bad;

		j = 0;
		while (buffer & APALIS_TK1_K20_EZP_STA_WIP) {
			msleep(10);
			if ((apalis_tk1_k20_read_ezport(apalis_tk1_k20,
					APALIS_TK1_K20_EZP_RDSR, 0, 1,
					&buffer) < 0) || (j > 10000))
				goto bad;
			j++;
		}
		i += APALIS_TK1_K20_EZP_WRITE_SIZE;
	}

	return 0;

bad:
	dev_err(apalis_tk1_k20->dev, "Error writing to the chip.\n");
	return -EIO;
}

static uint8_t apalis_tk1_k20_fw_ezport_status(void)
{
	return fw_entry->data[APALIS_TK1_K20_FW_FOPT_ADDR] &
			APALIS_TK1_K20_FOPT_EZP_ENA;
}

static uint8_t apalis_tk1_k20_get_fw_revision(void)
{
	if (fw_entry)
		if (fw_entry->size > APALIS_TK1_K20_FW_VER_ADDR)
			return fw_entry->data[APALIS_TK1_K20_FW_VER_ADDR];
	return 0;
}
#endif /* CONFIG_APALIS_TK1_K20_EZP */


#ifdef CONFIG_OF
static int apalis_tk1_k20_probe_flags_dt(
		struct apalis_tk1_k20_regmap *apalis_tk1_k20)
{
	struct device_node *np = apalis_tk1_k20->dev->of_node;

	if (!np)
		return -ENODEV;

	if (of_property_read_bool(np, "toradex,apalis-tk1-k20-uses-adc"))
		apalis_tk1_k20->flags |= APALIS_TK1_K20_USES_ADC;

	if (of_property_read_bool(np, "toradex,apalis-tk1-k20-uses-tsc"))
		apalis_tk1_k20->flags |= APALIS_TK1_K20_USES_TSC;

	if (of_property_read_bool(np, "toradex,apalis-tk1-k20-uses-can"))
		apalis_tk1_k20->flags |= APALIS_TK1_K20_USES_CAN;

	if (of_property_read_bool(np, "toradex,apalis-tk1-k20-uses-gpio"))
		apalis_tk1_k20->flags |= APALIS_TK1_K20_USES_GPIO;

	return 0;
}

static int apalis_tk1_k20_probe_gpios_dt(
		struct apalis_tk1_k20_regmap *apalis_tk1_k20)
{
	struct device_node *np = apalis_tk1_k20->dev->of_node;

	if (!np)
		return -ENODEV;

	apalis_tk1_k20->reset_gpio = of_get_named_gpio(np, "rst-gpio", 0);
	if (apalis_tk1_k20->reset_gpio < 0)
		return apalis_tk1_k20->reset_gpio;
	gpio_request(apalis_tk1_k20->reset_gpio, "apalis-tk1-k20-reset");
	gpio_direction_output(apalis_tk1_k20->reset_gpio, 1);

	apalis_tk1_k20->ezpcs_gpio = of_get_named_gpio(np, "ezport-cs-gpio", 0);
	if (apalis_tk1_k20->ezpcs_gpio < 0)
		return apalis_tk1_k20->ezpcs_gpio;
	gpio_request(apalis_tk1_k20->ezpcs_gpio, "apalis-tk1-k20-ezpcs");
	gpio_direction_output(apalis_tk1_k20->ezpcs_gpio, 1);

	apalis_tk1_k20->int2_gpio = of_get_named_gpio(np, "int2-gpio", 0);
	if (apalis_tk1_k20->int2_gpio < 0)
		return apalis_tk1_k20->int2_gpio;
	gpio_request(apalis_tk1_k20->int2_gpio, "apalis-tk1-k20-int2");
	gpio_direction_output(apalis_tk1_k20->int2_gpio, 1);

	return 0;
}
#else
static inline int apalis_tk1_k20_probe_flags_dt(
		struct apalis_tk1_k20_regmap *apalis_tk1_k20)
{
	return -ENODEV;
}
static inline int apalis_tk1_k20_probe_gpios_dt(
		struct apalis_tk1_k20_regmap *apalis_tk1_k20)
{
	return -ENODEV;
}
#endif

int apalis_tk1_k20_dev_init(struct device *dev)
{
	struct apalis_tk1_k20_platform_data *pdata = dev_get_platdata(dev);
	struct apalis_tk1_k20_regmap *apalis_tk1_k20 = dev_get_drvdata(dev);
	uint32_t revision = 0x00;
	int ret, i;
	int erase_only = 0;

	apalis_tk1_k20->dev = dev;

	ret = apalis_tk1_k20_probe_gpios_dt(apalis_tk1_k20);
	if ((ret < 0) && pdata) {
		if (pdata) {
			apalis_tk1_k20->ezpcs_gpio = pdata->ezpcs_gpio;
			apalis_tk1_k20->reset_gpio = pdata->reset_gpio;
			apalis_tk1_k20->int2_gpio = pdata->int2_gpio;
		} else {
			dev_err(dev, "Error claiming GPIOs\n");
			ret = -EINVAL;
			goto bad;
		}
	}
	apalis_tk1_k20_reset_chip(apalis_tk1_k20);
	msleep(10);
	ret = apalis_tk1_k20_reg_read(apalis_tk1_k20, APALIS_TK1_K20_REVREG,
			&revision);

#ifdef CONFIG_APALIS_TK1_K20_EZP
	if ((request_firmware(&fw_entry, "apalis-tk1-k20.bin", dev) < 0)
	    && (revision != APALIS_TK1_K20_FW_VER)) {
		dev_err(apalis_tk1_k20->dev,
			"Unsupported firmware version %d.%d and no local" \
			" firmware file available.\n",
			(revision & 0xF0 >> 8),
			(revision & 0x0F));
		ret = -ENOTSUPP;
		goto bad;
	}

	if ((fw_entry == NULL) && (revision != APALIS_TK1_K20_FW_VER)) {
		dev_err(apalis_tk1_k20->dev,
			"Unsupported firmware version %d.%d and no local" \
			" firmware file available.\n",
			(revision & 0xF0 >> 8),
			(revision & 0x0F));
		ret = -ENOTSUPP;
		goto bad;
	}

	if (fw_entry != NULL) {
		if (fw_entry->size == 1)
			erase_only = 1;
	}

	if ((apalis_tk1_k20_get_fw_revision() != APALIS_TK1_K20_FW_VER) &&
		(revision != APALIS_TK1_K20_FW_VER) && !erase_only &&
		(fw_entry != NULL)) {
		dev_err(apalis_tk1_k20->dev,
			"Unsupported firmware version in both the device " \
			"as well as the local firmware file.\n");
		release_firmware(fw_entry);
		ret = -ENOTSUPP;
		goto bad;
	}

	if ((revision != APALIS_TK1_K20_FW_VER) && !erase_only &&
			(!apalis_tk1_k20_fw_ezport_status()) &&
			(fw_entry != NULL)) {
		dev_err(apalis_tk1_k20->dev,
			"Unsupported firmware version in the device and the " \
			"local firmware file disables the EZ Port.\n");
		release_firmware(fw_entry);
		ret = -ENOTSUPP;
		goto bad;
	}

	if (((revision != APALIS_TK1_K20_FW_VER) || erase_only) &&
			(fw_entry != NULL)) {
		i = 0;
		while (apalis_tk1_k20_enter_ezport(apalis_tk1_k20) < 0
				&& i++ < 5) {
			msleep(50);
		}
		if (i >= 5) {
			dev_err(apalis_tk1_k20->dev,
				"Problem entering EZ port mode.\n");
			release_firmware(fw_entry);
			ret = -EIO;
			goto bad;
		}
		if (apalis_tk1_k20_erase_chip_ezport(apalis_tk1_k20) < 0) {
			dev_err(apalis_tk1_k20->dev,
				"Problem erasing the chip.\n");
			release_firmware(fw_entry);
			ret = -EIO;
			goto bad;
		}
		if (erase_only) {
			dev_err(apalis_tk1_k20->dev,
				"Chip fully erased.\n");
			release_firmware(fw_entry);
			ret = -EIO;
			goto bad;
		}
		if (apalis_tk1_k20_flash_chip_ezport(apalis_tk1_k20) < 0) {
			dev_err(apalis_tk1_k20->dev,
				"Problem flashing new firmware.\n");
			release_firmware(fw_entry);
			ret = -EIO;
			goto bad;
		}
	}
	if (fw_entry != NULL)
		release_firmware(fw_entry);

	msleep(10);
	apalis_tk1_k20_reset_chip(apalis_tk1_k20);
	msleep(10);

	ret = apalis_tk1_k20_reg_read(apalis_tk1_k20, APALIS_TK1_K20_REVREG,
			&revision);
#endif /* CONFIG_APALIS_TK1_K20_EZP */

	if (ret) {
		dev_err(apalis_tk1_k20->dev, "Device is not answering.\n");
		goto bad;
	}

	if (revision != APALIS_TK1_K20_FW_VER) {
		dev_err(apalis_tk1_k20->dev,
			"Unsupported firmware version %d.%d.\n",
			((revision & 0xF0) >> 4), (revision & 0x0F));
		ret = -ENOTSUPP;
		goto bad;
	}

	for (i = 0; i < ARRAY_SIZE(apalis_tk1_k20->irqs); i++) {
		apalis_tk1_k20->irqs[i].reg_offset = i /
						     APALIS_TK1_K20_IRQ_PER_REG;
		apalis_tk1_k20->irqs[i].mask = BIT(i %
						   APALIS_TK1_K20_IRQ_PER_REG);
	}

	apalis_tk1_k20->irq_chip.name		= dev_name(dev);
	apalis_tk1_k20->irq_chip.status_base	= APALIS_TK1_K20_IRQREG;
	apalis_tk1_k20->irq_chip.mask_base	= APALIS_TK1_K20_MSQREG;
	apalis_tk1_k20->irq_chip.ack_base	= 0;
	apalis_tk1_k20->irq_chip.irq_reg_stride	= 0;
	apalis_tk1_k20->irq_chip.num_regs	= APALIS_TK1_K20_IRQ_REG_CNT;
	apalis_tk1_k20->irq_chip.irqs		= apalis_tk1_k20->irqs;
	apalis_tk1_k20->irq_chip.num_irqs = ARRAY_SIZE(apalis_tk1_k20->irqs);

	ret = regmap_add_irq_chip(apalis_tk1_k20->regmap, apalis_tk1_k20->irq,
			IRQF_ONESHOT | IRQF_TRIGGER_FALLING |
			IRQF_TRIGGER_RISING, 0, &apalis_tk1_k20->irq_chip,
			&apalis_tk1_k20->irq_data);
	if (ret)
		goto bad;

	mutex_init(&apalis_tk1_k20->lock);

	if (apalis_tk1_k20_probe_flags_dt(apalis_tk1_k20) < 0 && pdata)
		apalis_tk1_k20->flags = pdata->flags;

	if (apalis_tk1_k20->flags & APALIS_TK1_K20_USES_CAN) {
		apalis_tk1_k20->can0_irq = irq_of_parse_and_map(
					apalis_tk1_k20->dev->of_node, 1);
		apalis_tk1_k20->can1_irq = irq_of_parse_and_map(
					apalis_tk1_k20->dev->of_node, 2);
		if (apalis_tk1_k20->can0_irq == 0 ||
				apalis_tk1_k20->can1_irq == 0) {
			apalis_tk1_k20->flags &= ~APALIS_TK1_K20_USES_CAN;
			dev_err(apalis_tk1_k20->dev,
					"Missing CAN interrupts.\n");
		}
	}

	if (pdata) {
		if (apalis_tk1_k20->flags & APALIS_TK1_K20_USES_TSC)
			apalis_tk1_k20_add_subdevice_pdata(apalis_tk1_k20,
					"apalis-tk1-k20-ts",
					&pdata->touch, sizeof(pdata->touch));

		if (apalis_tk1_k20->flags & APALIS_TK1_K20_USES_ADC)
			apalis_tk1_k20_add_subdevice_pdata(apalis_tk1_k20,
					"apalis-tk1-k20-adc",
					&pdata->adc, sizeof(pdata->adc));

		if (apalis_tk1_k20->flags & APALIS_TK1_K20_USES_CAN) {
			/* We have 2 CAN devices inside K20 */
			pdata->can0.id = 0;
			pdata->can1.id = 1;
			apalis_tk1_k20_add_subdevice_pdata(apalis_tk1_k20,
					"apalis-tk1-k20-can",
					&pdata->can0, sizeof(pdata->can0));
			apalis_tk1_k20_add_subdevice_pdata(apalis_tk1_k20,
					"apalis-tk1-k20-can",
					&pdata->can1, sizeof(pdata->can1));
		}
		if (apalis_tk1_k20->flags & APALIS_TK1_K20_USES_GPIO)
			apalis_tk1_k20_add_subdevice_pdata(apalis_tk1_k20,
					"apalis-tk1-k20-gpio",
					&pdata->gpio, sizeof(pdata->gpio));
	} else {
		if (apalis_tk1_k20->flags & APALIS_TK1_K20_USES_TSC)
			apalis_tk1_k20_add_subdevice(apalis_tk1_k20,
					"apalis-tk1-k20-ts");

		if (apalis_tk1_k20->flags & APALIS_TK1_K20_USES_ADC)
			apalis_tk1_k20_add_subdevice(apalis_tk1_k20,
					"apalis-tk1-k20-adc");

		if (apalis_tk1_k20->flags & APALIS_TK1_K20_USES_CAN) {
			/* We have 2 CAN devices inside K20 */
			apalis_tk1_k20_add_subdevice_pdata_id(apalis_tk1_k20,
					"apalis-tk1-k20-can",
					NULL, 0, 0);
			apalis_tk1_k20_add_subdevice_pdata_id(apalis_tk1_k20,
					"apalis-tk1-k20-can",
					NULL, 0, 1);
		}

		if (apalis_tk1_k20->flags & APALIS_TK1_K20_USES_GPIO)
			apalis_tk1_k20_add_subdevice(apalis_tk1_k20,
					"apalis-tk1-k20-gpio");
	}

	dev_info(apalis_tk1_k20->dev, "Apalis TK1 K20 MFD driver.\n"
			"Firmware version %d.%d.\n", FW_MAJOR, FW_MINOR);

	return 0;

bad:
	if (apalis_tk1_k20->ezpcs_gpio >= 0)
		gpio_free(apalis_tk1_k20->ezpcs_gpio);
	if (apalis_tk1_k20->reset_gpio >= 0)
		gpio_free(apalis_tk1_k20->reset_gpio);
	if (apalis_tk1_k20->int2_gpio >= 0)
		gpio_free(apalis_tk1_k20->int2_gpio);

	return ret;
}


static int apalis_tk1_k20_spi_probe(struct spi_device *spi)
{
	struct apalis_tk1_k20_regmap *apalis_tk1_k20;
	int ret;

	apalis_tk1_k20 = devm_kzalloc(&spi->dev, sizeof(*apalis_tk1_k20),
			GFP_KERNEL);
	if (!apalis_tk1_k20)
		return -ENOMEM;

	dev_set_drvdata(&spi->dev, apalis_tk1_k20);

	spi->mode = SPI_MODE_1;

	apalis_tk1_k20->irq = spi->irq;

	spi->max_speed_hz = (spi->max_speed_hz >= APALIS_TK1_K20_MAX_SPI_SPEED)
				? APALIS_TK1_K20_MAX_SPI_SPEED : spi->max_speed_hz;

	ret = spi_setup(spi);
	if (ret)
		return ret;

	apalis_tk1_k20->regmap = devm_regmap_init(&spi->dev,
			&regmap_apalis_tk1_k20_bus, &spi->dev,
			&apalis_tk1_k20_regmap_spi_config);
	if (IS_ERR(apalis_tk1_k20->regmap)) {
		ret = PTR_ERR(apalis_tk1_k20->regmap);
		dev_err(&spi->dev, "Failed to initialize regmap: %d\n", ret);
		return ret;
	}

	return apalis_tk1_k20_dev_init(&spi->dev);
}

static int apalis_tk1_k20_spi_remove(struct spi_device *spi)
{
	struct apalis_tk1_k20_regmap *apalis_tk1_k20 =
			dev_get_drvdata(&spi->dev);

	if (apalis_tk1_k20->ezpcs_gpio >= 0)
		gpio_free(apalis_tk1_k20->ezpcs_gpio);
	if (apalis_tk1_k20->reset_gpio >= 0)
		gpio_free(apalis_tk1_k20->reset_gpio);
	if (apalis_tk1_k20->int2_gpio >= 0)
		gpio_free(apalis_tk1_k20->int2_gpio);

	kfree(spi->controller_data);
	spi->controller_data = NULL;

	mfd_remove_devices(&spi->dev);
	regmap_del_irq_chip(apalis_tk1_k20->irq, apalis_tk1_k20->irq_data);
	mutex_destroy(&apalis_tk1_k20->lock);

	return 0;
}

static struct spi_driver apalis_tk1_k20_spi_driver = {
	.id_table = apalis_tk1_k20_device_ids,
	.driver = {
		.name = "apalis-tk1-k20",
		.of_match_table = apalis_tk1_k20_dt_ids,
	},
	.probe = apalis_tk1_k20_spi_probe,
	.remove = apalis_tk1_k20_spi_remove,
};

static int __init apalis_tk1_k20_init(void)
{
	return spi_register_driver(&apalis_tk1_k20_spi_driver);
}
subsys_initcall(apalis_tk1_k20_init);

static void __exit apalis_tk1_k20_exit(void)
{
	spi_unregister_driver(&apalis_tk1_k20_spi_driver);
}
module_exit(apalis_tk1_k20_exit);

MODULE_DESCRIPTION("MFD driver for Kinetis MK20DN512 MCU on Apalis TK1");
MODULE_AUTHOR("Dominik Sliwa <dominik.sliwa@toradex.com>");
MODULE_LICENSE("GPL v2");