aboutsummaryrefslogtreecommitdiff
path: root/drivers/i2c/chips/pcf50606.c
blob: 93dd4b869bf08db558787a455b5debf556001fb5 (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
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
/* Philips/NXP PCF50606 Power Management Unit (PMU) driver
 *
 * (C) 2006-2007 by Openmoko, Inc.
 * Authors: Harald Welte <laforge@openmoko.org>,
 *	    Matt Hsu <matt@openmoko.org>
 * All rights reserved.
 *
 * 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; either version 2 of
 * the License, or (at your option) any later version.
 *
 * 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.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 * MA 02111-1307 USA
 *
 * This driver is a monster ;) It provides the following features
 * - voltage control for a dozen different voltage domains
 * - charging control for main and backup battery
 * - rtc / alarm
 * - watchdog
 * - adc driver (hw_sensors like)
 * - pwm driver
 * - backlight
 *
 */

#include <linux/module.h>
#include <linux/init.h>
#include <linux/i2c.h>
#include <linux/types.h>
#include <linux/interrupt.h>
#include <linux/irq.h>
#include <linux/workqueue.h>
#include <linux/rtc.h>
#include <linux/bcd.h>
#include <linux/watchdog.h>
#include <linux/miscdevice.h>
#include <linux/input.h>
#include <linux/fb.h>
#include <linux/backlight.h>
#include <linux/sched.h>
#include <linux/platform_device.h>
#include <linux/pcf50606.h>
#include <linux/apm-emulation.h>

#include <asm/mach-types.h>
#include <asm/arch/gta01.h>

#include "pcf50606.h"

/* we use dev_dbg() throughout the code, but sometimes don't want to
 * write an entire line of debug related information.  This DEBUGPC
 * macro is a continuation for dev_dbg() */
#ifdef DEBUG
#define DEBUGPC(x, args ...) printk(x, ## args)
#else
#define DEBUGPC(x, args ...)
#endif

/***********************************************************************
 * Static data / structures
 ***********************************************************************/

static unsigned short normal_i2c[] = { 0x08, I2C_CLIENT_END };

I2C_CLIENT_INSMOD_1(pcf50606);

#define PCF50606_B_CHG_FAST	0	/* Charger Fast allowed */
#define PCF50606_B_CHG_PRESENT	1	/* Charger present */
#define PCF50606_B_CHG_FOK	2	/* Fast OK for battery */
#define PCF50606_B_CHG_ERR	3	/* Charger Error */
#define PCF50606_B_CHG_PROT	4	/* Charger Protection */
#define PCF50606_B_CHG_READY	5	/* Charging completed */

#define PCF50606_F_CHG_FAST	(1<<PCF50606_B_CHG_FAST)
#define PCF50606_F_CHG_PRESENT	(1<<PCF50606_B_CHG_PRESENT)
#define PCF50606_F_CHG_FOK	(1<<PCF50606_B_CHG_FOK)
#define PCF50606_F_CHG_ERR	(1<<PCF50606_B_CHG_ERR)
#define PCF50606_F_CHG_PROT	(1<<PCF50606_B_CHG_PROT)
#define PCF50606_F_CHG_READY	(1<<PCF50606_B_CHG_READY)
#define PCF50606_F_CHG_MASK	0x000000fc

#define PCF50606_F_PWR_PRESSED	0x00000100
#define PCF50606_F_RTC_SECOND	0x00000200

enum close_state {
	CLOSE_STATE_NOT,
	CLOSE_STATE_ALLOW = 0x2342,
};

struct pcf50606_data {
	struct i2c_client client;
	struct pcf50606_platform_data *pdata;
	struct backlight_device *backlight;
	struct mutex lock;
	unsigned int flags;
	unsigned int working;
	struct mutex working_lock;
	struct work_struct work;
	struct rtc_device *rtc;
	struct input_dev *input_dev;
	int allow_close;
	int onkey_seconds;
	int irq;
	int coldplug_done;
#ifdef CONFIG_PM
	struct {
		u_int8_t dcdc1, dcdc2;
		u_int8_t dcdec1;
		u_int8_t dcudc1;
		u_int8_t ioregc;
		u_int8_t d1regc1;
		u_int8_t d2regc1;
		u_int8_t d3regc1;
		u_int8_t lpregc1;
		u_int8_t adcc1, adcc2;
		u_int8_t pwmc1;
		u_int8_t int1m, int2m, int3m;
	} standby_regs;
#endif
};

static struct i2c_driver pcf50606_driver;

/* This is an ugly construct on how to access the (currently single/global)
 * pcf50606 handle from other code in the kernel.  I didn't really come up with
 * a more decent method of dynamically resolving this */
struct pcf50606_data *pcf50606_global;
EXPORT_SYMBOL_GPL(pcf50606_global);

static struct platform_device *pcf50606_pdev;

/* This is a 10k, B=3370 NTC Thermistor -10..79 centigrade */
/* Table entries are offset by +0.5C so a properly rounded value is generated */
static const u_int16_t ntc_table_10k_3370B[] = {
	/* -10 */
	43888, 41819, 39862, 38010, 36257, 34596, 33024, 31534, 30121, 28781,
	27510, 26304, 25159, 24071, 23038, 22056, 21122, 20234, 19390, 18586,
	17821, 17093, 16399, 15738, 15107, 14506, 13933, 13387, 12865, 12367,
	11891, 11437, 11003, 10588, 10192, 9813, 9450, 9103, 8771, 8453,
	8149, 7857, 7578, 7310, 7054, 6808, 6572, 6346, 6129, 5920,
	5720, 5528, 5344, 5167, 4996, 4833, 4675, 4524, 4379, 4239,
	4104, 3975, 3850, 3730, 3614, 3503, 3396, 3292, 3193, 3097,
	3004, 2915, 2829, 2745, 2665, 2588, 2513, 2441, 2371, 2304,
	2239, 2176, 2116, 2057, 2000, 1945, 1892, 1841, 1791, 1743,
};


/***********************************************************************
 * Low-Level routines
 ***********************************************************************/

static inline int __reg_write(struct pcf50606_data *pcf, u_int8_t reg,
			      u_int8_t val)
{
	return i2c_smbus_write_byte_data(&pcf->client, reg, val);
}

static int reg_write(struct pcf50606_data *pcf, u_int8_t reg, u_int8_t val)
{
	int ret;

	mutex_lock(&pcf->lock);
	ret = __reg_write(pcf, reg, val);
	mutex_unlock(&pcf->lock);

	return ret;
}

static inline int32_t __reg_read(struct pcf50606_data *pcf, u_int8_t reg)
{
	int32_t ret;

	ret = i2c_smbus_read_byte_data(&pcf->client, reg);

	return ret;
}

static u_int8_t reg_read(struct pcf50606_data *pcf, u_int8_t reg)
{
	int32_t ret;

	mutex_lock(&pcf->lock);
	ret = __reg_read(pcf, reg);
	mutex_unlock(&pcf->lock);

	return ret & 0xff;
}

static int reg_set_bit_mask(struct pcf50606_data *pcf,
			    u_int8_t reg, u_int8_t mask, u_int8_t val)
{
	int ret;
	u_int8_t tmp;

	val &= mask;

	mutex_lock(&pcf->lock);

	tmp = __reg_read(pcf, reg);
	tmp &= ~mask;
	tmp |= val;
	ret = __reg_write(pcf, reg, tmp);

	mutex_unlock(&pcf->lock);

	return ret;
}

static int reg_clear_bits(struct pcf50606_data *pcf, u_int8_t reg, u_int8_t val)
{
	int ret;
	u_int8_t tmp;

	mutex_lock(&pcf->lock);

	tmp = __reg_read(pcf, reg);
	tmp &= ~val;
	ret = __reg_write(pcf, reg, tmp);

	mutex_unlock(&pcf->lock);

	return ret;
}

/* synchronously read one ADC channel (busy-wait for result to be complete) */
static u_int16_t adc_read(struct pcf50606_data *pcf,  int channel,
			  u_int16_t *data2)
{
	u_int8_t adcs2, adcs1;
	u_int16_t ret;

	dev_dbg(&pcf->client.dev, "entering (pcf=%p, channel=%u, data2=%p)\n",
		pcf, channel, data2);

	channel &= PCF50606_ADCC2_ADCMUX_MASK;

	mutex_lock(&pcf->lock);

	/* start ADC conversion of selected channel */
	__reg_write(pcf, PCF50606_REG_ADCC2, channel |
		    PCF50606_ADCC2_ADCSTART | PCF50606_ADCC2_RES_10BIT);

	do {
		adcs2 = __reg_read(pcf, PCF50606_REG_ADCS2);
	} while (!(adcs2 & PCF50606_ADCS2_ADCRDY));

	adcs1 = __reg_read(pcf, PCF50606_REG_ADCS1);
	ret = (adcs1 << 2) | (adcs2 & 0x03);

	if (data2) {
		adcs1 = __reg_read(pcf, PCF50606_REG_ADCS3);
		*data2 = (adcs1 << 2) | ((adcs2 & 0x0c) >> 2);
	}

	mutex_unlock(&pcf->lock);

	dev_dbg(&pcf->client.dev, "returning %u %u\n", ret,
		data2 ? *data2 : 0);

	return ret;
}

/***********************************************************************
 * Voltage / ADC
 ***********************************************************************/

static u_int8_t dcudc_voltage(unsigned int millivolts)
{
	if (millivolts < 900)
		return 0;
	if (millivolts > 5500)
		return 0x1f;
	if (millivolts <= 3300) {
		millivolts -= 900;
		return millivolts/300;
	}
	if (millivolts < 4000)
		return 0x0f;
	else {
		millivolts -= 4000;
		return millivolts/100;
	}
}

static unsigned int dcudc_2voltage(u_int8_t bits)
{
	bits &= 0x1f;
	if (bits < 0x08)
		return 900 + bits * 300;
	else if (bits < 0x10)
		return 3300;
	else
		return 4000 + bits * 100;
}

static u_int8_t dcdec_voltage(unsigned int millivolts)
{
	if (millivolts < 900)
		return 0;
	else if (millivolts > 3300)
		return 0x0f;

	millivolts -= 900;
	return millivolts/300;
}

static unsigned int dcdec_2voltage(u_int8_t bits)
{
	bits &= 0x0f;
	return 900 + bits*300;
}

static u_int8_t dcdc_voltage(unsigned int millivolts)
{
	if (millivolts < 900)
		return 0;
	else if (millivolts > 3600)
		return 0x1f;

	if (millivolts < 1500) {
		millivolts -= 900;
		return millivolts/25;
	} else {
		millivolts -= 1500;
		return 0x18 + millivolts/300;
	}
}

static unsigned int dcdc_2voltage(u_int8_t bits)
{
	bits &= 0x1f;
	if ((bits & 0x18) == 0x18)
		return 1500 + ((bits & 0x7) * 300);
	else
		return 900 + (bits * 25);
}

static u_int8_t dx_voltage(unsigned int millivolts)
{
	if (millivolts < 900)
		return 0;
	else if (millivolts > 3300)
		return 0x18;

	millivolts -= 900;
	return millivolts/100;
}

static unsigned int dx_2voltage(u_int8_t bits)
{
	bits &= 0x1f;
	return 900 + (bits * 100);
}

static const u_int8_t regulator_registers[__NUM_PCF50606_REGULATORS] = {
	[PCF50606_REGULATOR_DCD]	= PCF50606_REG_DCDC1,
	[PCF50606_REGULATOR_DCDE]	= PCF50606_REG_DCDEC1,
	[PCF50606_REGULATOR_DCUD]	= PCF50606_REG_DCUDC1,
	[PCF50606_REGULATOR_D1REG]	= PCF50606_REG_D1REGC1,
	[PCF50606_REGULATOR_D2REG]	= PCF50606_REG_D2REGC1,
	[PCF50606_REGULATOR_D3REG]	= PCF50606_REG_D3REGC1,
	[PCF50606_REGULATOR_LPREG]	= PCF50606_REG_LPREGC1,
	[PCF50606_REGULATOR_IOREG]	= PCF50606_REG_IOREGC,
};

int pcf50606_onoff_set(struct pcf50606_data *pcf,
		       enum pcf50606_regulator_id reg, int on)
{
	u_int8_t addr;

	if (reg >= __NUM_PCF50606_REGULATORS)
		return -EINVAL;

	/* IOREG cannot be powered off since it powers the PMU I2C */
	if (reg == PCF50606_REGULATOR_IOREG)
		return -EIO;

	addr = regulator_registers[reg];

	if (on == 0)
		reg_set_bit_mask(pcf, addr, 0xe0, 0x00);
	else
		reg_set_bit_mask(pcf, addr, 0xe0, 0xe0);

	return 0;
}
EXPORT_SYMBOL_GPL(pcf50606_onoff_set);

int pcf50606_onoff_get(struct pcf50606_data *pcf,
		       enum pcf50606_regulator_id reg)
{
	u_int8_t val, addr;

	if (reg >= __NUM_PCF50606_REGULATORS)
		return -EINVAL;

	addr = regulator_registers[reg];
	val = (reg_read(pcf, addr) & 0xe0) >> 5;

	/* PWREN1 = 1, PWREN2 = 1, see table 16 of datasheet */
	switch (val) {
	case 0:
	case 5:
		return 0;
	default:
		return 1;
	}
}
EXPORT_SYMBOL_GPL(pcf50606_onoff_get);

int pcf50606_voltage_set(struct pcf50606_data *pcf,
			 enum pcf50606_regulator_id reg,
			 unsigned int millivolts)
{
	u_int8_t volt_bits;
	u_int8_t regnr;
	int rc;

	dev_dbg(&pcf->client.dev, "pcf=%p, reg=%d, mvolts=%d\n", pcf, reg,
		millivolts);

	if (reg >= __NUM_PCF50606_REGULATORS)
		return -EINVAL;

	if (millivolts > pcf->pdata->rails[reg].voltage.max)
		return -EINVAL;

	switch (reg) {
	case PCF50606_REGULATOR_DCD:
		volt_bits = dcdc_voltage(millivolts);
		rc = reg_set_bit_mask(pcf, PCF50606_REG_DCDC1, 0x1f,
				      volt_bits);
		break;
	case PCF50606_REGULATOR_DCDE:
		volt_bits = dcdec_voltage(millivolts);
		rc = reg_set_bit_mask(pcf, PCF50606_REG_DCDEC1, 0x0f,
				      volt_bits);
		break;
	case PCF50606_REGULATOR_DCUD:
		volt_bits = dcudc_voltage(millivolts);
		rc = reg_set_bit_mask(pcf, PCF50606_REG_DCUDC1, 0x1f,
				      volt_bits);
		break;
	case PCF50606_REGULATOR_D1REG:
	case PCF50606_REGULATOR_D2REG:
	case PCF50606_REGULATOR_D3REG:
		regnr = PCF50606_REG_D1REGC1 + (reg - PCF50606_REGULATOR_D1REG);
		volt_bits = dx_voltage(millivolts);
		rc = reg_set_bit_mask(pcf, regnr, 0x1f, volt_bits);
		break;
	case PCF50606_REGULATOR_LPREG:
		volt_bits = dx_voltage(millivolts);
		rc = reg_set_bit_mask(pcf, PCF50606_REG_LPREGC1, 0x1f,
				      volt_bits);
		break;
	case PCF50606_REGULATOR_IOREG:
		if (millivolts < 1800)
			return -EINVAL;
		volt_bits = dx_voltage(millivolts);
		rc = reg_set_bit_mask(pcf, PCF50606_REG_IOREGC, 0x1f,
				      volt_bits);
		break;
	default:
		return -EINVAL;
	}

	return rc;
}
EXPORT_SYMBOL_GPL(pcf50606_voltage_set);

unsigned int pcf50606_voltage_get(struct pcf50606_data *pcf,
			 enum pcf50606_regulator_id reg)
{
	u_int8_t volt_bits;
	u_int8_t regnr;
	unsigned int rc = 0;

	if (reg >= __NUM_PCF50606_REGULATORS)
		return -EINVAL;

	switch (reg) {
	case PCF50606_REGULATOR_DCD:
		volt_bits = reg_read(pcf, PCF50606_REG_DCDC1) & 0x1f;
		rc = dcdc_2voltage(volt_bits);
		break;
	case PCF50606_REGULATOR_DCDE:
		volt_bits = reg_read(pcf, PCF50606_REG_DCDEC1) & 0x0f;
		rc = dcdec_2voltage(volt_bits);
		break;
	case PCF50606_REGULATOR_DCUD:
		volt_bits = reg_read(pcf, PCF50606_REG_DCUDC1) & 0x1f;
		rc = dcudc_2voltage(volt_bits);
		break;
	case PCF50606_REGULATOR_D1REG:
	case PCF50606_REGULATOR_D2REG:
	case PCF50606_REGULATOR_D3REG:
		regnr = PCF50606_REG_D1REGC1 + (reg - PCF50606_REGULATOR_D1REG);
		volt_bits = reg_read(pcf, regnr) & 0x1f;
		if (volt_bits > 0x18)
			volt_bits = 0x18;
		rc = dx_2voltage(volt_bits);
		break;
	case PCF50606_REGULATOR_LPREG:
		volt_bits = reg_read(pcf, PCF50606_REG_LPREGC1) & 0x1f;
		if (volt_bits > 0x18)
			volt_bits = 0x18;
		rc = dx_2voltage(volt_bits);
		break;
	case PCF50606_REGULATOR_IOREG:
		volt_bits = reg_read(pcf, PCF50606_REG_IOREGC) & 0x1f;
		if (volt_bits > 0x18)
			volt_bits = 0x18;
		rc = dx_2voltage(volt_bits);
		break;
	default:
		return -EINVAL;
	}

	return rc;
}
EXPORT_SYMBOL_GPL(pcf50606_voltage_get);

/* go into 'STANDBY' mode, i.e. power off the main CPU and peripherals */
void pcf50606_go_standby(void)
{
	reg_write(pcf50606_global, PCF50606_REG_OOCC1,
		  PCF50606_OOCC1_GOSTDBY);
}
EXPORT_SYMBOL_GPL(pcf50606_go_standby);

void pcf50606_gpo0_set(struct pcf50606_data *pcf, int on)
{
	u_int8_t val;

	if (on)
		val = 0x07;
	else
		val = 0x0f;

	reg_set_bit_mask(pcf, PCF50606_REG_GPOC1, 0x0f, val);
}
EXPORT_SYMBOL_GPL(pcf50606_gpo0_set);

int pcf50606_gpo0_get(struct pcf50606_data *pcf)
{
	u_int8_t reg = reg_read(pcf, PCF50606_REG_GPOC1) & 0x0f;

	if (reg == 0x07 || reg == 0x08)
		return 1;

	return 0;
}
EXPORT_SYMBOL_GPL(pcf50606_gpo0_get);

static void pcf50606_work(struct work_struct *work)
{
	struct pcf50606_data *pcf =
			container_of(work, struct pcf50606_data, work);
	u_int8_t pcfirq[3];
	int ret;

	mutex_lock(&pcf->working_lock);
	pcf->working = 1;
	/*
	 * p35 pcf50606 datasheet rev 2.2:
	 * ''The system controller shall read all interrupt registers in
	 *   one I2C read action''
	 * because if you don't INT# gets stuck asserted forever after a
	 * while
	 */
	ret = i2c_smbus_read_i2c_block_data(&pcf->client, PCF50606_REG_INT1, 3,
					    pcfirq);
	if (ret != 3)
		DEBUGPC("Oh crap PMU IRQ register read failed %d\n", ret);

	if (!pcf->coldplug_done) {
		DEBUGPC("PMU Coldplug init\n");

		/* we used SECOND to kick ourselves started -- turn it off */
		pcfirq[0] &= ~PCF50606_INT1_SECOND;
		reg_set_bit_mask(pcf, PCF50606_REG_INT1M, PCF50606_INT1_SECOND,
				 PCF50606_INT1_SECOND);

		/* coldplug the USB if present */
		if (__reg_read(pcf, PCF50606_REG_OOCS) & PCF50606_OOCS_EXTON) {
			/* Charger inserted */
			DEBUGPC("COLD CHGINS ");
			input_report_key(pcf->input_dev, KEY_BATTERY, 1);
			apm_queue_event(APM_POWER_STATUS_CHANGE);
			pcf->flags |= PCF50606_F_CHG_PRESENT;
			if (pcf->pdata->cb)
				pcf->pdata->cb(&pcf->client.dev,
						PCF50606_FEAT_MBC,
						PMU_EVT_INSERT);
		}

		pcf->coldplug_done = 1;
	}


	dev_dbg(&pcf->client.dev, "INT1=0x%02x INT2=0x%02x INT3=0x%02x:",
		pcfirq[0], pcfirq[1], pcfirq[2]);

	if (pcfirq[0] & PCF50606_INT1_ONKEYF) {
		/* ONKEY falling edge (start of button press) */
		DEBUGPC("ONKEYF ");
		pcf->flags |= PCF50606_F_PWR_PRESSED;
		input_report_key(pcf->input_dev, KEY_POWER, 1);
	}
	if (pcfirq[0] & PCF50606_INT1_ONKEY1S) {
		/* ONKEY pressed for more than 1 second */
		pcf->onkey_seconds = 0;
		DEBUGPC("ONKEY1S ");
		/* Tell PMU we are taking care of this */
		reg_set_bit_mask(pcf, PCF50606_REG_OOCC1,
				 PCF50606_OOCC1_TOTRST,
				 PCF50606_OOCC1_TOTRST);
		/* enable SECOND interrupt (hz tick) */
		reg_clear_bits(pcf, PCF50606_REG_INT1M, PCF50606_INT1_SECOND);
	}
	if (pcfirq[0] & PCF50606_INT1_ONKEYR) {
		/* ONKEY rising edge (end of button press) */
		DEBUGPC("ONKEYR ");
		pcf->flags &= ~PCF50606_F_PWR_PRESSED;
		pcf->onkey_seconds = -1;
		input_report_key(pcf->input_dev, KEY_POWER, 0);
		/* disable SECOND interrupt in case RTC didn't
		 * request it */
		if (!(pcf->flags & PCF50606_F_RTC_SECOND))
			reg_set_bit_mask(pcf, PCF50606_REG_INT1M,
					 PCF50606_INT1_SECOND,
					 PCF50606_INT1_SECOND);
	}
	if (pcfirq[0] & PCF50606_INT1_EXTONR) {
		DEBUGPC("EXTONR ");
		input_report_key(pcf->input_dev, KEY_POWER2, 1);
	}
	if (pcfirq[0] & PCF50606_INT1_EXTONF) {
		DEBUGPC("EXTONF ");
		input_report_key(pcf->input_dev, KEY_POWER2, 0);
	}
	if (pcfirq[0] & PCF50606_INT1_SECOND) {
		DEBUGPC("SECOND ");
		if (pcf->flags & PCF50606_F_RTC_SECOND)
			rtc_update_irq(pcf->rtc, 1,
				       RTC_PF | RTC_IRQF);

		if (pcf->onkey_seconds >= 0 &&
		    pcf->flags & PCF50606_F_PWR_PRESSED) {
			DEBUGPC("ONKEY_SECONDS(%u, OOCC1=0x%02x) ",
				pcf->onkey_seconds,
				reg_read(pcf, PCF50606_REG_OOCC1));
			pcf->onkey_seconds++;
			if (pcf->onkey_seconds >=
			    pcf->pdata->onkey_seconds_required) {
				/* Ask init to do 'ctrlaltdel' */
				/*
				 * currently Linux reacts badly to issuing a
				 * signal to PID #1 before init is started.
				 * What happens is that the next kernel thread
				 * to start, which is the JFFS2 Garbage
				 * collector in our case, gets the signal
				 * instead and proceeds to fail to fork --
				 * which is very bad.  Therefore we confirm
				 * PID #1 exists before issuing the signal
				 */
				if (find_task_by_pid_ns(1, &init_pid_ns)) {
					kill_pid(task_pid(find_task_by_pid_ns(1, 
							&init_pid_ns)), SIGINT, 1);
					DEBUGPC("SIGINT(init) ");
				}
				/* FIXME: what to do if userspace doesn't
				 * shut down? Do we want to force it? */
			}
		}
	}
	if (pcfirq[0] & PCF50606_INT1_ALARM) {
		DEBUGPC("ALARM ");
		if (pcf->pdata->used_features & PCF50606_FEAT_RTC)
			rtc_update_irq(pcf->rtc, 1,
				       RTC_AF | RTC_IRQF);
	}

	if (pcfirq[1] & PCF50606_INT2_CHGINS) {
		/* Charger inserted */
		DEBUGPC("CHGINS ");
		input_report_key(pcf->input_dev, KEY_BATTERY, 1);
		apm_queue_event(APM_POWER_STATUS_CHANGE);
		pcf->flags |= PCF50606_F_CHG_PRESENT;
		if (pcf->pdata->cb)
			pcf->pdata->cb(&pcf->client.dev,
				       PCF50606_FEAT_MBC, PMU_EVT_INSERT);
		/* FIXME: how to signal this to userspace */
	}
	if (pcfirq[1] & PCF50606_INT2_CHGRM) {
		/* Charger removed */
		DEBUGPC("CHGRM ");
		input_report_key(pcf->input_dev, KEY_BATTERY, 0);
		apm_queue_event(APM_POWER_STATUS_CHANGE);
		pcf->flags &= ~(PCF50606_F_CHG_MASK|PCF50606_F_CHG_PRESENT);
		if (pcf->pdata->cb)
			pcf->pdata->cb(&pcf->client.dev,
				       PCF50606_FEAT_MBC, PMU_EVT_INSERT);
		/* FIXME: how signal this to userspace */
	}
	if (pcfirq[1] & PCF50606_INT2_CHGFOK) {
		/* Battery ready for fast charging */
		DEBUGPC("CHGFOK ");
		pcf->flags |= PCF50606_F_CHG_FOK;
		/* FIXME: how to signal this to userspace */
	}
	if (pcfirq[1] & PCF50606_INT2_CHGERR) {
		/* Error in charge mode */
		DEBUGPC("CHGERR ");
		pcf->flags |= PCF50606_F_CHG_ERR;
		pcf->flags &= ~(PCF50606_F_CHG_FOK|PCF50606_F_CHG_READY);
		/* FIXME: how to signal this to userspace */
	}
	if (pcfirq[1] & PCF50606_INT2_CHGFRDY) {
		/* Fast charge completed */
		DEBUGPC("CHGFRDY ");
		pcf->flags |= PCF50606_F_CHG_READY;
		pcf->flags &= ~PCF50606_F_CHG_FOK;
		/* FIXME: how to signal this to userspace */
	}
	if (pcfirq[1] & PCF50606_INT2_CHGPROT) {
		/* Charging protection interrupt */
		DEBUGPC("CHGPROT ");
		pcf->flags &= ~(PCF50606_F_CHG_FOK|PCF50606_F_CHG_READY);
		/* FIXME: signal this to userspace */
	}
	if (pcfirq[1] & PCF50606_INT2_CHGWD10S) {
		/* Charger watchdog will expire in 10 seconds */
		DEBUGPC("CHGWD10S ");
		reg_set_bit_mask(pcf, PCF50606_REG_OOCC1,
				 PCF50606_OOCC1_WDTRST,
				 PCF50606_OOCC1_WDTRST);
	}
	if (pcfirq[1] & PCF50606_INT2_CHGWDEXP) {
		/* Charger watchdog expires */
		DEBUGPC("CHGWDEXP ");
		/* FIXME: how to signal this to userspace */
	}

	if (pcfirq[2] & PCF50606_INT3_ADCRDY) {
		/* ADC result ready */
		DEBUGPC("ADCRDY ");
	}
	if (pcfirq[2] & PCF50606_INT3_ACDINS) {
		/* Accessory insertion detected */
		DEBUGPC("ACDINS ");
		if (pcf->pdata->cb)
			pcf->pdata->cb(&pcf->client.dev,
				       PCF50606_FEAT_ACD, PMU_EVT_INSERT);
	}
	if (pcfirq[2] & PCF50606_INT3_ACDREM) {
		/* Accessory removal detected */
		DEBUGPC("ACDREM ");
		if (pcf->pdata->cb)
			pcf->pdata->cb(&pcf->client.dev,
				       PCF50606_FEAT_ACD, PMU_EVT_REMOVE);
	}
	/* FIXME: TSCPRES */
	if (pcfirq[2] & PCF50606_INT3_LOWBAT) {
		if (__reg_read(pcf, PCF50606_REG_OOCS) & PCF50606_OOCS_EXTON) {
			/*
			 * hey no need to freak out, we have some kind of
			 * valid charger power
			 */
			DEBUGPC("(NO)BAT ");
		} else {
			/* Really low battery voltage, we have 8 seconds left */
			DEBUGPC("LOWBAT ");
			/*
			 * currently Linux reacts badly to issuing a signal to
			 * PID #1 before init is started.  What happens is that
			 * the next kernel thread to start, which is the JFFS2
			 * Garbage collector in our case, gets the signal
			 * instead and proceeds to fail to fork -- which is
			 * very bad.  Therefore we confirm PID #1 exists
			 * before issuing SPIGPWR
			 */
			if (find_task_by_pid_ns(1, &init_pid_ns)) {
				apm_queue_event(APM_LOW_BATTERY);
				DEBUGPC("SIGPWR(init) ");
				kill_pid(task_pid(find_task_by_pid_ns(1, &init_pid_ns)), SIGPWR, 1);
			} else
				/*
				 * well, our situation is like this:  we do not
				 * have any external power, we have a low
				 * battery and since PID #1 doesn't exist yet,
				 * we are early in the boot, likely before
				 * rootfs mount.  We should just call it a day
				 */
				apm_queue_event(APM_CRITICAL_SUSPEND);
		}
		/* Tell PMU we are taking care of this */
		reg_set_bit_mask(pcf, PCF50606_REG_OOCC1,
				 PCF50606_OOCC1_TOTRST,
				 PCF50606_OOCC1_TOTRST);
	}
	if (pcfirq[2] & PCF50606_INT3_HIGHTMP) {
		/* High temperature */
		DEBUGPC("HIGHTMP ");
		apm_queue_event(APM_CRITICAL_SUSPEND);
	}

	DEBUGPC("\n");

	pcf->working = 0;
	input_sync(pcf->input_dev);
	put_device(&pcf->client.dev);
	mutex_unlock(&pcf->working_lock);
}

static irqreturn_t pcf50606_irq(int irq, void *_pcf)
{
	struct pcf50606_data *pcf = _pcf;

	dev_dbg(&pcf->client.dev, "entering(irq=%u, pcf=%p): scheduling work\n",
		irq, _pcf);
	get_device(&pcf->client.dev);
	if (!schedule_work(&pcf->work) && !pcf->working)
		dev_dbg(&pcf->client.dev, "work item may be lost\n");

	return IRQ_HANDLED;
}

static u_int16_t adc_to_batt_millivolts(u_int16_t adc)
{
	u_int16_t mvolts;

	mvolts = (adc * 6000) / 1024;

	return mvolts;
}

#define BATTVOLT_SCALE_START 2800
#define BATTVOLT_SCALE_END 4200
#define BATTVOLT_SCALE_DIVIDER ((BATTVOLT_SCALE_END - BATTVOLT_SCALE_START)/100)

static u_int8_t battvolt_scale(u_int16_t battvolt)
{
	/* FIXME: this linear scale is completely bogus */
	u_int16_t battvolt_relative = battvolt - BATTVOLT_SCALE_START;
	unsigned int percent = battvolt_relative / BATTVOLT_SCALE_DIVIDER;

	return percent;
}

u_int16_t pcf50606_battvolt(struct pcf50606_data *pcf)
{
	u_int16_t adc;
	adc = adc_read(pcf, PCF50606_ADCMUX_BATVOLT_RES, NULL);

	return adc_to_batt_millivolts(adc);
}
EXPORT_SYMBOL_GPL(pcf50606_battvolt);

static ssize_t show_battvolt(struct device *dev, struct device_attribute *attr,
			     char *buf)
{
	struct i2c_client *client = to_i2c_client(dev);
	struct pcf50606_data *pcf = i2c_get_clientdata(client);

	return sprintf(buf, "%u\n", pcf50606_battvolt(pcf));
}
static DEVICE_ATTR(battvolt, S_IRUGO | S_IWUSR, show_battvolt, NULL);

static int reg_id_by_name(const char *name)
{
	int reg_id;

	if (!strcmp(name, "voltage_dcd"))
		reg_id = PCF50606_REGULATOR_DCD;
	else if (!strcmp(name, "voltage_dcde"))
		reg_id = PCF50606_REGULATOR_DCDE;
	else if (!strcmp(name, "voltage_dcud"))
		reg_id = PCF50606_REGULATOR_DCUD;
	else if (!strcmp(name, "voltage_d1reg"))
		reg_id = PCF50606_REGULATOR_D1REG;
	else if (!strcmp(name, "voltage_d2reg"))
		reg_id = PCF50606_REGULATOR_D2REG;
	else if (!strcmp(name, "voltage_d3reg"))
		reg_id = PCF50606_REGULATOR_D3REG;
	else if (!strcmp(name, "voltage_lpreg"))
		reg_id = PCF50606_REGULATOR_LPREG;
	else if (!strcmp(name, "voltage_ioreg"))
		reg_id = PCF50606_REGULATOR_IOREG;
	else
		reg_id = -1;

	return reg_id;
}

static ssize_t show_vreg(struct device *dev, struct device_attribute *attr,
			 char *buf)
{
	struct i2c_client *client = to_i2c_client(dev);
	struct pcf50606_data *pcf = i2c_get_clientdata(client);
	unsigned int reg_id;

	reg_id = reg_id_by_name(attr->attr.name);
	if (reg_id < 0)
		return 0;

	if (pcf50606_onoff_get(pcf, reg_id) > 0)
		return sprintf(buf, "%u\n", pcf50606_voltage_get(pcf, reg_id));
	else
		return strlcpy(buf, "0\n", PAGE_SIZE);
}

static ssize_t set_vreg(struct device *dev, struct device_attribute *attr,
			const char *buf, size_t count)
{
	struct i2c_client *client = to_i2c_client(dev);
	struct pcf50606_data *pcf = i2c_get_clientdata(client);
	unsigned long mvolts = simple_strtoul(buf, NULL, 10);
	unsigned int reg_id;

	reg_id = reg_id_by_name(attr->attr.name);
	if (reg_id < 0)
		return -EIO;

	dev_dbg(dev, "attempting to set %s(%d) to %lu mvolts\n",
		attr->attr.name, reg_id, mvolts);

	if (mvolts == 0) {
		pcf50606_onoff_set(pcf, reg_id, 0);
	} else {
		if (pcf50606_voltage_set(pcf, reg_id, mvolts) < 0) {
			dev_warn(dev, "refusing to set %s(%d) to %lu mvolts "
				 "(max=%u)\n", attr->attr.name, reg_id, mvolts,
				 pcf->pdata->rails[reg_id].voltage.max);
			return -EINVAL;
		}
		pcf50606_onoff_set(pcf, reg_id, 1);
	}

	return count;
}

static DEVICE_ATTR(voltage_dcd, S_IRUGO | S_IWUSR, show_vreg, set_vreg);
static DEVICE_ATTR(voltage_dcde, S_IRUGO | S_IWUSR, show_vreg, set_vreg);
static DEVICE_ATTR(voltage_dcud, S_IRUGO | S_IWUSR, show_vreg, set_vreg);
static DEVICE_ATTR(voltage_d1reg, S_IRUGO | S_IWUSR, show_vreg, set_vreg);
static DEVICE_ATTR(voltage_d2reg, S_IRUGO | S_IWUSR, show_vreg, set_vreg);
static DEVICE_ATTR(voltage_d3reg, S_IRUGO | S_IWUSR, show_vreg, set_vreg);
static DEVICE_ATTR(voltage_lpreg, S_IRUGO | S_IWUSR, show_vreg, set_vreg);
static DEVICE_ATTR(voltage_ioreg, S_IRUGO | S_IWUSR, show_vreg, set_vreg);

/***********************************************************************
 * Charger Control
 ***********************************************************************/

/* Enable/disable fast charging (500mA in the GTA01) */
void pcf50606_charge_fast(struct pcf50606_data *pcf, int on)
{
	if (!(pcf->pdata->used_features & PCF50606_FEAT_MBC))
		return;

	if (on) {
		/* We can allow PCF to automatically charge
		 * using Ifast */
		pcf->flags |= PCF50606_F_CHG_FAST;
		reg_set_bit_mask(pcf, PCF50606_REG_MBCC1,
				 PCF50606_MBCC1_AUTOFST,
				 PCF50606_MBCC1_AUTOFST);
	} else {
		pcf->flags &= ~PCF50606_F_CHG_FAST;
		/* disable automatic fast-charge */
		reg_clear_bits(pcf, PCF50606_REG_MBCC1,
				PCF50606_MBCC1_AUTOFST);
		/* switch to idle mode to abort existing charge
		 * process */
		reg_set_bit_mask(pcf, PCF50606_REG_MBCC1,
				 PCF50606_MBCC1_CHGMOD_MASK,
				 PCF50606_MBCC1_CHGMOD_IDLE);
	}
}
EXPORT_SYMBOL_GPL(pcf50606_charge_fast);

static inline u_int16_t adc_to_rntc(struct pcf50606_data *pcf, u_int16_t adc)
{
	u_int32_t r_ntc = (adc * (u_int32_t)pcf->pdata->r_fix_batt)
		/ (1023 - adc);

	return r_ntc;
}

static inline int16_t rntc_to_temp(u_int16_t rntc)
{
	int i;

	for (i = 0; i < ARRAY_SIZE(ntc_table_10k_3370B); i++) {
		if (rntc > ntc_table_10k_3370B[i])
			return i - 10;	/* First element is -10 */
	}
	return -99;	/* Below our range */
}

static ssize_t show_battemp(struct device *dev, struct device_attribute *attr,
			    char *buf)
{
	struct i2c_client *client = to_i2c_client(dev);
	struct pcf50606_data *pcf = i2c_get_clientdata(client);
	u_int16_t adc;

	adc = adc_read(pcf, PCF50606_ADCMUX_BATTEMP, NULL);

	return sprintf(buf, "%d\n", rntc_to_temp(adc_to_rntc(pcf, adc)));
}
static DEVICE_ATTR(battemp, S_IRUGO | S_IWUSR, show_battemp, NULL);

static inline int16_t adc_to_chg_milliamps(struct pcf50606_data *pcf,
					     u_int16_t adc_adcin1,
					     u_int16_t adc_batvolt)
{
	int32_t res = (adc_adcin1 - adc_batvolt) * 2400;
	return (res * 1000) / (pcf->pdata->r_sense_milli * 1024);
}

static ssize_t show_chgcur(struct device *dev, struct device_attribute *attr,
			   char *buf)
{
	struct i2c_client *client = to_i2c_client(dev);
	struct pcf50606_data *pcf = i2c_get_clientdata(client);
	u_int16_t adc_batvolt, adc_adcin1;
	int16_t ma;

	adc_batvolt = adc_read(pcf, PCF50606_ADCMUX_BATVOLT_ADCIN1,
			       &adc_adcin1);
	ma = adc_to_chg_milliamps(pcf, adc_adcin1, adc_batvolt);

	return sprintf(buf, "%d\n", ma);
}
static DEVICE_ATTR(chgcur, S_IRUGO | S_IWUSR, show_chgcur, NULL);

static const char *chgmode_names[] = {
	[PCF50606_MBCC1_CHGMOD_QUAL]		= "qualification",
	[PCF50606_MBCC1_CHGMOD_PRE]		= "pre",
	[PCF50606_MBCC1_CHGMOD_TRICKLE]		= "trickle",
	[PCF50606_MBCC1_CHGMOD_FAST_CCCV]	= "fast_cccv",
	[PCF50606_MBCC1_CHGMOD_FAST_NOCC]	= "fast_nocc",
	[PCF50606_MBCC1_CHGMOD_FAST_NOCV]	= "fast_nocv",
	[PCF50606_MBCC1_CHGMOD_FAST_SW]		= "fast_switch",
	[PCF50606_MBCC1_CHGMOD_IDLE]		= "idle",
};

static ssize_t show_chgmode(struct device *dev, struct device_attribute *attr,
			    char *buf)
{
	struct i2c_client *client = to_i2c_client(dev);
	struct pcf50606_data *pcf = i2c_get_clientdata(client);
	u_int8_t mbcc1 = reg_read(pcf, PCF50606_REG_MBCC1);
	u_int8_t chgmod = (mbcc1 & PCF50606_MBCC1_CHGMOD_MASK);

	return sprintf(buf, "%s\n", chgmode_names[chgmod]);
}

static ssize_t set_chgmode(struct device *dev, struct device_attribute *attr,
			   const char *buf, size_t count)
{
	struct i2c_client *client = to_i2c_client(dev);
	struct pcf50606_data *pcf = i2c_get_clientdata(client);
	u_int8_t mbcc1 = reg_read(pcf, PCF50606_REG_MBCC1);

	mbcc1 &= ~PCF50606_MBCC1_CHGMOD_MASK;

	if (!strcmp(buf, "qualification"))
		mbcc1 |= PCF50606_MBCC1_CHGMOD_QUAL;
	else if (!strcmp(buf, "pre"))
		mbcc1 |= PCF50606_MBCC1_CHGMOD_PRE;
	else if (!strcmp(buf, "trickle"))
		mbcc1 |= PCF50606_MBCC1_CHGMOD_TRICKLE;
	else if (!strcmp(buf, "fast_cccv"))
		mbcc1 |= PCF50606_MBCC1_CHGMOD_FAST_CCCV;
	/* We don't allow the other fast modes for security reasons */
	else if (!strcmp(buf, "idle"))
		mbcc1 |= PCF50606_MBCC1_CHGMOD_IDLE;
	else
		return -EINVAL;

	reg_write(pcf, PCF50606_REG_MBCC1, mbcc1);

	return count;
}

static DEVICE_ATTR(chgmode, S_IRUGO | S_IWUSR, show_chgmode, set_chgmode);

static const char *chgstate_names[] = {
	[PCF50606_B_CHG_FAST]			= "fast_enabled",
	[PCF50606_B_CHG_PRESENT] 		= "present",
	[PCF50606_B_CHG_FOK]			= "fast_ok",
	[PCF50606_B_CHG_ERR]			= "error",
	[PCF50606_B_CHG_PROT]			= "protection",
	[PCF50606_B_CHG_READY]			= "ready",
};

static ssize_t show_chgstate(struct device *dev, struct device_attribute *attr,
			    char *buf)
{
	struct i2c_client *client = to_i2c_client(dev);
	struct pcf50606_data *pcf = i2c_get_clientdata(client);
	char *b = buf;
	int i;

	for (i = 0; i < 32; i++)
		if (pcf->flags & (1 << i) && i < ARRAY_SIZE(chgstate_names))
			b += sprintf(b, "%s ", chgstate_names[i]);

	if (b > buf)
		b += sprintf(b, "\n");

	return b - buf;
}
static DEVICE_ATTR(chgstate, S_IRUGO | S_IWUSR, show_chgstate, NULL);

/***********************************************************************
 * APM emulation
 ***********************************************************************/

static void pcf50606_get_power_status(struct apm_power_info *info)
{
	struct pcf50606_data *pcf = pcf50606_global;
	u_int8_t mbcc1 = reg_read(pcf, PCF50606_REG_MBCC1);
	u_int8_t chgmod = mbcc1 & PCF50606_MBCC1_CHGMOD_MASK;
	u_int16_t battvolt = pcf50606_battvolt(pcf);

	if (reg_read(pcf, PCF50606_REG_OOCS) & PCF50606_OOCS_EXTON)
		info->ac_line_status = APM_AC_ONLINE;
	else
		info->ac_line_status = APM_AC_OFFLINE;

	switch (chgmod) {
	case PCF50606_MBCC1_CHGMOD_QUAL:
	case PCF50606_MBCC1_CHGMOD_PRE:
	case PCF50606_MBCC1_CHGMOD_IDLE:
		info->battery_life = battvolt_scale(battvolt);
		break;
	default:
		info->battery_status = APM_BATTERY_STATUS_CHARGING;
		info->battery_flag = APM_BATTERY_FLAG_CHARGING;
		break;
	}
}

/***********************************************************************
 * RTC
 ***********************************************************************/

struct pcf50606_time {
	u_int8_t sec;
	u_int8_t min;
	u_int8_t hour;
	u_int8_t wkday;
	u_int8_t day;
	u_int8_t month;
	u_int8_t year;
};

static void pcf2rtc_time(struct rtc_time *rtc, struct pcf50606_time *pcf)
{
	rtc->tm_sec = BCD2BIN(pcf->sec);
	rtc->tm_min = BCD2BIN(pcf->min);
	rtc->tm_hour = BCD2BIN(pcf->hour);
	rtc->tm_wday = BCD2BIN(pcf->wkday);
	rtc->tm_mday = BCD2BIN(pcf->day);
	rtc->tm_mon = BCD2BIN(pcf->month);
	rtc->tm_year = BCD2BIN(pcf->year) + 100;
}

static void rtc2pcf_time(struct pcf50606_time *pcf, struct rtc_time *rtc)
{
	pcf->sec = BIN2BCD(rtc->tm_sec);
	pcf->min = BIN2BCD(rtc->tm_min);
	pcf->hour = BIN2BCD(rtc->tm_hour);
	pcf->wkday = BIN2BCD(rtc->tm_wday);
	pcf->day = BIN2BCD(rtc->tm_mday);
	pcf->month = BIN2BCD(rtc->tm_mon);
	pcf->year = BIN2BCD(rtc->tm_year - 100);
}

static int pcf50606_rtc_ioctl(struct device *dev, unsigned int cmd,
			      unsigned long arg)
{
	struct i2c_client *client = to_i2c_client(dev);
	struct pcf50606_data *pcf = i2c_get_clientdata(client);

	switch (cmd) {
	case RTC_AIE_OFF:
		/* disable the alarm interrupt */
		reg_set_bit_mask(pcf, PCF50606_REG_INT1M,
				 PCF50606_INT1_ALARM, PCF50606_INT1_ALARM);
		return 0;
	case RTC_AIE_ON:
		/* enable the alarm interrupt */
		reg_clear_bits(pcf, PCF50606_REG_INT1M, PCF50606_INT1_ALARM);
		return 0;
	case RTC_PIE_OFF:
		/* disable periodic interrupt (hz tick) */
		pcf->flags &= ~PCF50606_F_RTC_SECOND;
		reg_set_bit_mask(pcf, PCF50606_REG_INT1M,
				 PCF50606_INT1_SECOND, PCF50606_INT1_SECOND);
		return 0;
	case RTC_PIE_ON:
		/* ensable periodic interrupt (hz tick) */
		pcf->flags |= PCF50606_F_RTC_SECOND;
		reg_clear_bits(pcf, PCF50606_REG_INT1M, PCF50606_INT1_SECOND);
		return 0;
	}
	return -ENOIOCTLCMD;
}

static int pcf50606_rtc_read_time(struct device *dev, struct rtc_time *tm)
{
	struct i2c_client *client = to_i2c_client(dev);
	struct pcf50606_data *pcf = i2c_get_clientdata(client);
	struct pcf50606_time pcf_tm;

	mutex_lock(&pcf->lock);
	pcf_tm.sec = __reg_read(pcf, PCF50606_REG_RTCSC);
	pcf_tm.min = __reg_read(pcf, PCF50606_REG_RTCMN);
	pcf_tm.hour = __reg_read(pcf, PCF50606_REG_RTCHR);
	pcf_tm.wkday = __reg_read(pcf, PCF50606_REG_RTCWD);
	pcf_tm.day = __reg_read(pcf, PCF50606_REG_RTCDT);
	pcf_tm.month = __reg_read(pcf, PCF50606_REG_RTCMT);
	pcf_tm.year = __reg_read(pcf, PCF50606_REG_RTCYR);
	mutex_unlock(&pcf->lock);

	dev_dbg(dev, "PCF_TIME: %02x.%02x.%02x %02x:%02x:%02x\n",
		pcf_tm.day, pcf_tm.month, pcf_tm.year,
		pcf_tm.hour, pcf_tm.min, pcf_tm.sec);

	pcf2rtc_time(tm, &pcf_tm);

	dev_dbg(dev, "RTC_TIME: %u.%u.%u %u:%u:%u\n",
		tm->tm_mday, tm->tm_mon, tm->tm_year,
		tm->tm_hour, tm->tm_min, tm->tm_sec);

	return 0;
}

static int pcf50606_rtc_set_time(struct device *dev, struct rtc_time *tm)
{
	struct i2c_client *client = to_i2c_client(dev);
	struct pcf50606_data *pcf = i2c_get_clientdata(client);
	struct pcf50606_time pcf_tm;
	u_int8_t int1m;

	dev_dbg(dev, "RTC_TIME: %u.%u.%u %u:%u:%u\n",
		tm->tm_mday, tm->tm_mon, tm->tm_year,
		tm->tm_hour, tm->tm_min, tm->tm_sec);
	rtc2pcf_time(&pcf_tm, tm);
	dev_dbg(dev, "PCF_TIME: %02x.%02x.%02x %02x:%02x:%02x\n",
		pcf_tm.day, pcf_tm.month, pcf_tm.year,
		pcf_tm.hour, pcf_tm.min, pcf_tm.sec);

	mutex_lock(&pcf->lock);

	/* disable SECOND interrupt */
	int1m = __reg_read(pcf, PCF50606_REG_INT1M);
	__reg_write(pcf, PCF50606_REG_INT1M, int1m | PCF50606_INT1_SECOND);

	__reg_write(pcf, PCF50606_REG_RTCSC, pcf_tm.sec);
	__reg_write(pcf, PCF50606_REG_RTCMN, pcf_tm.min);
	__reg_write(pcf, PCF50606_REG_RTCHR, pcf_tm.hour);
	__reg_write(pcf, PCF50606_REG_RTCWD, pcf_tm.wkday);
	__reg_write(pcf, PCF50606_REG_RTCDT, pcf_tm.day);
	__reg_write(pcf, PCF50606_REG_RTCMT, pcf_tm.month);
	__reg_write(pcf, PCF50606_REG_RTCYR, pcf_tm.year);

	/* restore INT1M, potentially re-enable SECOND interrupt */
	__reg_write(pcf, PCF50606_REG_INT1M, int1m);

	mutex_unlock(&pcf->lock);

	return 0;
}

static int pcf50606_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
{
	struct i2c_client *client = to_i2c_client(dev);
	struct pcf50606_data *pcf = i2c_get_clientdata(client);
	struct pcf50606_time pcf_tm;

	mutex_lock(&pcf->lock);
	alrm->enabled =
		__reg_read(pcf, PCF50606_REG_INT1M) & PCF50606_INT1_ALARM
		? 0 : 1;
	pcf_tm.sec = __reg_read(pcf, PCF50606_REG_RTCSCA);
	pcf_tm.min = __reg_read(pcf, PCF50606_REG_RTCMNA);
	pcf_tm.hour = __reg_read(pcf, PCF50606_REG_RTCHRA);
	pcf_tm.wkday = __reg_read(pcf, PCF50606_REG_RTCWDA);
	pcf_tm.day = __reg_read(pcf, PCF50606_REG_RTCDTA);
	pcf_tm.month = __reg_read(pcf, PCF50606_REG_RTCMTA);
	pcf_tm.year = __reg_read(pcf, PCF50606_REG_RTCYRA);
	mutex_unlock(&pcf->lock);

	pcf2rtc_time(&alrm->time, &pcf_tm);

	return 0;
}

static int pcf50606_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
{
	struct i2c_client *client = to_i2c_client(dev);
	struct pcf50606_data *pcf = i2c_get_clientdata(client);
	struct pcf50606_time pcf_tm;
	u_int8_t irqmask;

	rtc2pcf_time(&pcf_tm, &alrm->time);

	mutex_lock(&pcf->lock);

	/* disable alarm interrupt */
	irqmask = __reg_read(pcf, PCF50606_REG_INT1M);
	irqmask |= PCF50606_INT1_ALARM;
	__reg_write(pcf, PCF50606_REG_INT1M, irqmask);

	__reg_write(pcf, PCF50606_REG_RTCSCA, pcf_tm.sec);
	__reg_write(pcf, PCF50606_REG_RTCMNA, pcf_tm.min);
	__reg_write(pcf, PCF50606_REG_RTCHRA, pcf_tm.hour);
	__reg_write(pcf, PCF50606_REG_RTCWDA, pcf_tm.wkday);
	__reg_write(pcf, PCF50606_REG_RTCDTA, pcf_tm.day);
	__reg_write(pcf, PCF50606_REG_RTCMTA, pcf_tm.month);
	__reg_write(pcf, PCF50606_REG_RTCYRA, pcf_tm.year);

	if (alrm->enabled) {
		/* (re-)enaable alarm interrupt */
		irqmask = __reg_read(pcf, PCF50606_REG_INT1M);
		irqmask &= ~PCF50606_INT1_ALARM;
		__reg_write(pcf, PCF50606_REG_INT1M, irqmask);
	}

	mutex_unlock(&pcf->lock);

	/* FIXME */
	return 0;
}

static struct rtc_class_ops pcf50606_rtc_ops = {
	.ioctl		= pcf50606_rtc_ioctl,
	.read_time	= pcf50606_rtc_read_time,
	.set_time	= pcf50606_rtc_set_time,
	.read_alarm	= pcf50606_rtc_read_alarm,
	.set_alarm	= pcf50606_rtc_set_alarm,
};

/***********************************************************************
 * Watchdog
 ***********************************************************************/

static void pcf50606_wdt_start(struct pcf50606_data *pcf)
{
	reg_set_bit_mask(pcf, PCF50606_REG_OOCC1, PCF50606_OOCC1_WDTRST,
			 PCF50606_OOCC1_WDTRST);
}

static void pcf50606_wdt_stop(struct pcf50606_data *pcf)
{
	reg_clear_bits(pcf, PCF50606_REG_OOCS, PCF50606_OOCS_WDTEXP);
}

static void pcf50606_wdt_keepalive(struct pcf50606_data *pcf)
{
	pcf50606_wdt_start(pcf);
}

static int pcf50606_wdt_open(struct inode *inode, struct file *file)
{
	struct pcf50606_data *pcf = pcf50606_global;

	file->private_data = pcf;

	/* start the timer */
	pcf50606_wdt_start(pcf);

	return nonseekable_open(inode, file);
}

static int pcf50606_wdt_release(struct inode *inode, struct file *file)
{
	struct pcf50606_data *pcf = file->private_data;

	if (pcf->allow_close == CLOSE_STATE_ALLOW)
		pcf50606_wdt_stop(pcf);
	else {
		printk(KERN_CRIT "Unexpected close, not stopping watchdog!\n");
		pcf50606_wdt_keepalive(pcf);
	}

	pcf->allow_close = CLOSE_STATE_NOT;

	return 0;
}

static ssize_t pcf50606_wdt_write(struct file *file, const char __user *data,
				  size_t len, loff_t *ppos)
{
	struct pcf50606_data *pcf = file->private_data;
	if (len) {
		size_t i;

		for (i = 0; i != len; i++) {
			char c;
			if (get_user(c, data + i))
				return -EFAULT;
			if (c == 'V')
				pcf->allow_close = CLOSE_STATE_ALLOW;
		}
		pcf50606_wdt_keepalive(pcf);
	}

	return len;
}

static struct watchdog_info pcf50606_wdt_ident = {
	.options	= WDIOF_MAGICCLOSE,
	.firmware_version = 0,
	.identity	= "PCF50606 Watchdog",
};

static int pcf50606_wdt_ioctl(struct inode *inode, struct file *file,
			      unsigned int cmd, unsigned long arg)
{
	struct pcf50606_data *pcf = file->private_data;
	void __user *argp = (void __user *)arg;
	int __user *p = argp;

	switch (cmd) {
	case WDIOC_GETSUPPORT:
		return copy_to_user(argp, &pcf50606_wdt_ident,
				    sizeof(pcf50606_wdt_ident)) ? -EFAULT : 0;
		break;
	case WDIOC_GETSTATUS:
	case WDIOC_GETBOOTSTATUS:
		return put_user(0, p);
	case WDIOC_KEEPALIVE:
		pcf50606_wdt_keepalive(pcf);
		return 0;
	case WDIOC_GETTIMEOUT:
		return put_user(8, p);
	default:
		return -ENOIOCTLCMD;
	}
}

static struct file_operations pcf50606_wdt_fops = {
	.owner		= THIS_MODULE,
	.llseek		= no_llseek,
	.write		= &pcf50606_wdt_write,
	.ioctl		= &pcf50606_wdt_ioctl,
	.open		= &pcf50606_wdt_open,
	.release	= &pcf50606_wdt_release,
};

static struct miscdevice pcf50606_wdt_miscdev = {
	.minor		= WATCHDOG_MINOR,
	.name		= "watchdog",
	.fops		= &pcf50606_wdt_fops,
};

/***********************************************************************
 * PWM
 ***********************************************************************/

static const char *pwm_dc_table[] = {
	"0/16", "1/16", "2/16", "3/16",
	"4/16", "5/16", "6/16", "7/16",
	"8/16", "9/16", "10/16", "11/16",
	"12/16", "13/16", "14/16", "15/16",
};

static ssize_t show_pwm_dc(struct device *dev, struct device_attribute *attr,
			   char *buf)
{
	struct i2c_client *client = to_i2c_client(dev);
	struct pcf50606_data *pcf = i2c_get_clientdata(client);
	u_int8_t val;

	val = reg_read(pcf, PCF50606_REG_PWMC1) >> PCF50606_PWMC1_DC_SHIFT;
	val &= 0xf;

	return sprintf(buf, "%s\n", pwm_dc_table[val]);
}

static ssize_t set_pwm_dc(struct device *dev, struct device_attribute *attr,
			  const char *buf, size_t count)
{
	struct i2c_client *client = to_i2c_client(dev);
	struct pcf50606_data *pcf = i2c_get_clientdata(client);
	u_int8_t i;

	for (i = 0; i < ARRAY_SIZE(pwm_dc_table); i++) {
		if (!strncmp(buf, pwm_dc_table[i], strlen(pwm_dc_table[i]))) {
			dev_dbg(dev, "setting pwm dc %s\n\r", pwm_dc_table[i]);
			reg_set_bit_mask(pcf, PCF50606_REG_PWMC1, 0x1e,
					 (i << PCF50606_PWMC1_DC_SHIFT));
		}
	}
	return count;
}

static DEVICE_ATTR(pwm_dc, S_IRUGO | S_IWUSR, show_pwm_dc, set_pwm_dc);

static const char *pwm_clk_table[] = {
	"512", "256", "128", "64",
	"56300", "28100", "14100", "7000",
};

static ssize_t show_pwm_clk(struct device *dev, struct device_attribute *attr,
			    char *buf)
{
	struct i2c_client *client = to_i2c_client(dev);
	struct pcf50606_data *pcf = i2c_get_clientdata(client);
	u_int8_t val;

	val = reg_read(pcf, PCF50606_REG_PWMC1) >> PCF50606_PWMC1_CLK_SHIFT;
	val &= 0x7;

	return sprintf(buf, "%s\n", pwm_clk_table[val]);
}

static ssize_t set_pwm_clk(struct device *dev, struct device_attribute *attr,
			   const char *buf, size_t count)
{
	struct i2c_client *client = to_i2c_client(dev);
	struct pcf50606_data *pcf = i2c_get_clientdata(client);
	u_int8_t i;

	for (i = 0; i < ARRAY_SIZE(pwm_clk_table); i++) {
		if (!strncmp(buf, pwm_clk_table[i], strlen(pwm_clk_table[i]))) {
			dev_dbg(dev, "setting pwm clk %s\n\r",
				pwm_clk_table[i]);
			reg_set_bit_mask(pcf, PCF50606_REG_PWMC1, 0xe0,
					 (i << PCF50606_PWMC1_CLK_SHIFT));
		}
	}
	return count;
}

static DEVICE_ATTR(pwm_clk, S_IRUGO | S_IWUSR, show_pwm_clk, set_pwm_clk);

static int pcf50606bl_get_intensity(struct backlight_device *bd)
{
	struct pcf50606_data *pcf = bl_get_data(bd);
	int intensity = reg_read(pcf, PCF50606_REG_PWMC1);
	intensity = (intensity >> PCF50606_PWMC1_DC_SHIFT);

	return intensity & 0xf;
}

static int pcf50606bl_set_intensity(struct backlight_device *bd)
{
	struct pcf50606_data *pcf = bl_get_data(bd);
	int intensity = bd->props.brightness;

	if (bd->props.power != FB_BLANK_UNBLANK)
		intensity = 0;
	if (bd->props.fb_blank != FB_BLANK_UNBLANK)
		intensity = 0;

	return reg_set_bit_mask(pcf, PCF50606_REG_PWMC1, 0x1e,
				(intensity << PCF50606_PWMC1_DC_SHIFT));
}

static struct backlight_ops pcf50606bl_ops = {
	.get_brightness	= pcf50606bl_get_intensity,
	.update_status	= pcf50606bl_set_intensity,
};

/***********************************************************************
 * Driver initialization
 ***********************************************************************/

#ifdef CONFIG_MACH_NEO1973_GTA01
/* We currently place those platform devices here to make sure the device
 * suspend/resume order is correct */
static struct platform_device gta01_pm_gps_dev = {
	.name		= "neo1973-pm-gps",
};

static struct platform_device gta01_pm_bt_dev = {
	.name		= "neo1973-pm-bt",
};
#endif

static struct attribute *pcf_sysfs_entries[16] = {
	&dev_attr_voltage_dcd.attr,
	&dev_attr_voltage_dcde.attr,
	&dev_attr_voltage_dcud.attr,
	&dev_attr_voltage_d1reg.attr,
	&dev_attr_voltage_d2reg.attr,
	&dev_attr_voltage_d3reg.attr,
	&dev_attr_voltage_lpreg.attr,
	&dev_attr_voltage_ioreg.attr,
	NULL
};

static struct attribute_group pcf_attr_group = {
	.name	= NULL,			/* put in device directory */
	.attrs	= pcf_sysfs_entries,
};

static void populate_sysfs_group(struct pcf50606_data *pcf)
{
	int i = 0;
	struct attribute **attr;

	for (attr = pcf_sysfs_entries; *attr; attr++)
		i++;

	if (pcf->pdata->used_features & PCF50606_FEAT_MBC) {
		pcf_sysfs_entries[i++] = &dev_attr_chgstate.attr;
		pcf_sysfs_entries[i++] = &dev_attr_chgmode.attr;
	}

	if (pcf->pdata->used_features & PCF50606_FEAT_CHGCUR)
		pcf_sysfs_entries[i++] = &dev_attr_chgcur.attr;

	if (pcf->pdata->used_features & PCF50606_FEAT_BATVOLT)
		pcf_sysfs_entries[i++] = &dev_attr_battvolt.attr;

	if (pcf->pdata->used_features & PCF50606_FEAT_BATTEMP)
		pcf_sysfs_entries[i++] = &dev_attr_battemp.attr;

	if (pcf->pdata->used_features & PCF50606_FEAT_PWM) {
		pcf_sysfs_entries[i++] = &dev_attr_pwm_dc.attr;
		pcf_sysfs_entries[i++] = &dev_attr_pwm_clk.attr;
	}
}

static int pcf50606_detect(struct i2c_adapter *adapter, int address, int kind)
{
	struct i2c_client *new_client;
	struct pcf50606_data *data;
	int err = 0;
	int irq;

	if (!pcf50606_pdev) {
		printk(KERN_ERR "pcf50606: driver needs a platform_device!\n");
		return -EIO;
	}

	irq = platform_get_irq(pcf50606_pdev, 0);
	if (irq < 0) {
		dev_err(&pcf50606_pdev->dev, "no irq in platform resources!\n");
		return -EIO;
	}

	/* At the moment, we only support one PCF50606 in a system */
	if (pcf50606_global) {
		dev_err(&pcf50606_pdev->dev,
			"currently only one chip supported\n");
		return -EBUSY;
	}

	data = kzalloc(sizeof(*data), GFP_KERNEL);
	if (!data)
		return -ENOMEM;

	mutex_init(&data->lock);
	mutex_init(&data->working_lock);
	INIT_WORK(&data->work, pcf50606_work);
	data->irq = irq;
	data->working = 0;
	data->onkey_seconds = -1;
	data->pdata = pcf50606_pdev->dev.platform_data;

	new_client = &data->client;
	i2c_set_clientdata(new_client, data);
	new_client->addr = address;
	new_client->adapter = adapter;
	new_client->driver = &pcf50606_driver;
	new_client->flags = 0;
	strlcpy(new_client->name, "pcf50606", I2C_NAME_SIZE);

	/* now we try to detect the chip */

	/* register with i2c core */
	err = i2c_attach_client(new_client);
	if (err) {
		dev_err(&new_client->dev,
			"error during i2c_attach_client()\n");
		goto exit_free;
	}

	populate_sysfs_group(data);

	err = sysfs_create_group(&new_client->dev.kobj, &pcf_attr_group);
	if (err) {
		dev_err(&new_client->dev, "error creating sysfs group\n");
		goto exit_detach;
	}

	/* create virtual charger 'device' */

	/* input device registration */
	data->input_dev = input_allocate_device();
	if (!data->input_dev)
		goto exit_sysfs;

	data->input_dev->name = "FIC Neo1973 PMU events";
	data->input_dev->phys = "I2C";
	data->input_dev->id.bustype = BUS_I2C;

	data->input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_PWR);
	set_bit(KEY_POWER, data->input_dev->keybit);
	set_bit(KEY_POWER2, data->input_dev->keybit);
	set_bit(KEY_BATTERY, data->input_dev->keybit);

	err = input_register_device(data->input_dev);
	if (err)
		goto exit_sysfs;

	/* register power off handler with core power management */
	pm_power_off = &pcf50606_go_standby;

	/* configure interrupt mask */
	/* we don't mask SECOND here, because we want one to do coldplug with */
	reg_write(data, PCF50606_REG_INT1M, 0x00);
	reg_write(data, PCF50606_REG_INT2M, 0x00);
	reg_write(data, PCF50606_REG_INT3M, PCF50606_INT3_TSCPRES);

	err = request_irq(irq, pcf50606_irq, IRQF_TRIGGER_FALLING,
			  "pcf50606", data);
	if (err < 0)
		goto exit_input;

	if (enable_irq_wake(irq) < 0)
		dev_err(&new_client->dev, "IRQ %u cannot be enabled as wake-up"
			"source in this hardware revision!", irq);

	pcf50606_global = data;

	if (data->pdata->used_features & PCF50606_FEAT_RTC) {
		data->rtc = rtc_device_register("pcf50606", &new_client->dev,
						&pcf50606_rtc_ops, THIS_MODULE);
		if (IS_ERR(data->rtc)) {
			err = PTR_ERR(data->rtc);
			goto exit_irq;
		}
	}

	if (data->pdata->used_features & PCF50606_FEAT_WDT) {
		err = misc_register(&pcf50606_wdt_miscdev);
		if (err) {
			dev_err(&new_client->dev, "cannot register miscdev on "
			       "minor=%d (%d)\n", WATCHDOG_MINOR, err);
			goto exit_rtc;
		}
	}

	if (data->pdata->used_features & PCF50606_FEAT_PWM) {
		/* enable PWM controller */
		reg_set_bit_mask(data, PCF50606_REG_PWMC1,
				 PCF50606_PWMC1_ACTSET,
				 PCF50606_PWMC1_ACTSET);
	}

	if (data->pdata->used_features & PCF50606_FEAT_PWM_BL) {
		data->backlight = backlight_device_register("pcf50606-bl",
							    &new_client->dev,
							    data,
							    &pcf50606bl_ops);
		if (!data->backlight)
			goto exit_misc;
		data->backlight->props.max_brightness = 16;
		data->backlight->props.power = FB_BLANK_UNBLANK;
		data->backlight->props.brightness =
					data->pdata->init_brightness;
		backlight_update_status(data->backlight);
	}

	apm_get_power_status = pcf50606_get_power_status;

#ifdef CONFIG_MACH_NEO1973_GTA01
	if (machine_is_neo1973_gta01()) {
		/* FIXME: what is the device name we are symlinking to?
		 * symlink won't get created until this is fixed.
		 */
		struct device * busdev = bus_find_device_by_name(
							&platform_bus_type,
							NULL, "FIXME");
		gta01_pm_gps_dev.dev.parent = &new_client->dev;
		switch (system_rev) {
		case GTA01Bv2_SYSTEM_REV:
		case GTA01Bv3_SYSTEM_REV:
		case GTA01Bv4_SYSTEM_REV:
			gta01_pm_bt_dev.dev.parent = &new_client->dev;
			platform_device_register(&gta01_pm_bt_dev);
			break;
		}
		platform_device_register(&gta01_pm_gps_dev);
		/* a link for gllin compatibility */
		/* 2.6.25 tracking: platform_bus_type.devices that was here
		 * has gone away in mainline
		 */
		if (busdev) {
			err = sysfs_create_link(&busdev->kobj,
			&gta01_pm_gps_dev.dev.kobj, "gta01-pm-gps.0");
			if (err)
				printk(KERN_ERR
			    "sysfs_create_link (gta01-pm-gps.0): %d\n", err);
		}
	}
#endif

	if (data->pdata->used_features & PCF50606_FEAT_ACD)
		reg_set_bit_mask(data, PCF50606_REG_ACDC1,
				 PCF50606_ACDC1_ACDAPE, PCF50606_ACDC1_ACDAPE);
	else
		reg_clear_bits(data, PCF50606_REG_ACDC1,
			       PCF50606_ACDC1_ACDAPE);

	return 0;

exit_misc:
	if (data->pdata->used_features & PCF50606_FEAT_WDT)
		misc_deregister(&pcf50606_wdt_miscdev);
exit_rtc:
	if (data->pdata->used_features & PCF50606_FEAT_RTC)
		rtc_device_unregister(pcf50606_global->rtc);
exit_irq:
	free_irq(pcf50606_global->irq, pcf50606_global);
	pcf50606_global = NULL;
exit_input:
	pm_power_off = NULL;
	input_unregister_device(data->input_dev);
exit_sysfs:
	sysfs_remove_group(&new_client->dev.kobj, &pcf_attr_group);
exit_detach:
	i2c_detach_client(new_client);
exit_free:
	kfree(data);
	return err;
}

static int pcf50606_attach_adapter(struct i2c_adapter *adapter)
{
	return i2c_probe(adapter, &addr_data, &pcf50606_detect);
}

static int pcf50606_detach_client(struct i2c_client *client)
{
	struct pcf50606_data *pcf = i2c_get_clientdata(client);

	apm_get_power_status = NULL;
	input_unregister_device(pcf->input_dev);

	if (pcf->pdata->used_features & PCF50606_FEAT_PWM_BL)
		backlight_device_unregister(pcf->backlight);

	if (pcf->pdata->used_features & PCF50606_FEAT_WDT)
		misc_deregister(&pcf50606_wdt_miscdev);

	if (pcf->pdata->used_features & PCF50606_FEAT_RTC)
		rtc_device_unregister(pcf->rtc);

	free_irq(pcf->irq, pcf);

	sysfs_remove_group(&client->dev.kobj, &pcf_attr_group);

	pm_power_off = NULL;

	kfree(pcf);

	return 0;
}

#ifdef CONFIG_PM
#define INT1M_RESUMERS	(PCF50606_INT1_ALARM | \
			 PCF50606_INT1_ONKEYF | \
			 PCF50606_INT1_EXTONR)
#define INT2M_RESUMERS	(PCF50606_INT2_CHGWD10S | \
			 PCF50606_INT2_CHGPROT | \
			 PCF50606_INT2_CHGERR)
#define INT3M_RESUMERS	(PCF50606_INT3_LOWBAT | \
			 PCF50606_INT3_HIGHTMP | \
			 PCF50606_INT3_ACDINS)
static int pcf50606_suspend(struct device *dev, pm_message_t state)
{
	struct i2c_client *client = to_i2c_client(dev);
	struct pcf50606_data *pcf = i2c_get_clientdata(client);
	int i;

	/* The general idea is to power down all unused power supplies,
	 * and then mask all PCF50606 interrup sources but EXTONR, ONKEYF
	 * and ALARM */

	mutex_lock(&pcf->lock);

	/* Save all registers that don't "survive" standby state */
	pcf->standby_regs.dcdc1 = __reg_read(pcf, PCF50606_REG_DCDC1);
	pcf->standby_regs.dcdc2 = __reg_read(pcf, PCF50606_REG_DCDC2);
	pcf->standby_regs.dcdec1 = __reg_read(pcf, PCF50606_REG_DCDEC1);
	pcf->standby_regs.dcudc1 = __reg_read(pcf, PCF50606_REG_DCUDC1);
	pcf->standby_regs.ioregc = __reg_read(pcf, PCF50606_REG_IOREGC);
	pcf->standby_regs.d1regc1 = __reg_read(pcf, PCF50606_REG_D1REGC1);
	pcf->standby_regs.d2regc1 = __reg_read(pcf, PCF50606_REG_D2REGC1);
	pcf->standby_regs.d3regc1 = __reg_read(pcf, PCF50606_REG_D3REGC1);
	pcf->standby_regs.lpregc1 = __reg_read(pcf, PCF50606_REG_LPREGC1);
	pcf->standby_regs.adcc1 = __reg_read(pcf, PCF50606_REG_ADCC1);
	pcf->standby_regs.adcc2 = __reg_read(pcf, PCF50606_REG_ADCC2);
	pcf->standby_regs.pwmc1 = __reg_read(pcf, PCF50606_REG_PWMC1);

	/* switch off power supplies that are not needed during suspend */
	for (i = 0; i < __NUM_PCF50606_REGULATORS; i++) {
		if (!(pcf->pdata->rails[i].flags & PMU_VRAIL_F_SUSPEND_ON)) {
			u_int8_t tmp;

			/* IOREG powers the I@C interface so we cannot switch
			 * it off */
			if (i == PCF50606_REGULATOR_IOREG)
				continue;

			dev_dbg(dev, "disabling pcf50606 regulator %u\n", i);
			/* we cannot use pcf50606_onoff_set() because we're
			 * already under the mutex */
			tmp = __reg_read(pcf, regulator_registers[i]);
			tmp &= 0x1f;
			__reg_write(pcf, regulator_registers[i], tmp);
		}
	}

	pcf->standby_regs.int1m = __reg_read(pcf, PCF50606_REG_INT1M);
	pcf->standby_regs.int2m = __reg_read(pcf, PCF50606_REG_INT2M);
	pcf->standby_regs.int3m = __reg_read(pcf, PCF50606_REG_INT3M);
	__reg_write(pcf, PCF50606_REG_INT1M, ~INT1M_RESUMERS & 0xff);
	__reg_write(pcf, PCF50606_REG_INT2M, ~INT2M_RESUMERS & 0xff);
	__reg_write(pcf, PCF50606_REG_INT3M, ~INT3M_RESUMERS & 0xff);

	mutex_unlock(&pcf->lock);

	return 0;
}

static int pcf50606_resume(struct device *dev)
{
	struct i2c_client *client = to_i2c_client(dev);
	struct pcf50606_data *pcf = i2c_get_clientdata(client);

	mutex_lock(&pcf->lock);

	/* Resume all saved registers that don't "survive" standby state */
	__reg_write(pcf, PCF50606_REG_INT1M, pcf->standby_regs.int1m);
	__reg_write(pcf, PCF50606_REG_INT2M, pcf->standby_regs.int2m);
	__reg_write(pcf, PCF50606_REG_INT3M, pcf->standby_regs.int3m);

	__reg_write(pcf, PCF50606_REG_DCDC1, pcf->standby_regs.dcdc1);
	__reg_write(pcf, PCF50606_REG_DCDC2, pcf->standby_regs.dcdc2);
	__reg_write(pcf, PCF50606_REG_DCDEC1, pcf->standby_regs.dcdec1);
	__reg_write(pcf, PCF50606_REG_DCUDC1, pcf->standby_regs.dcudc1);
	__reg_write(pcf, PCF50606_REG_IOREGC, pcf->standby_regs.ioregc);
	__reg_write(pcf, PCF50606_REG_D1REGC1, pcf->standby_regs.d1regc1);
	__reg_write(pcf, PCF50606_REG_D2REGC1, pcf->standby_regs.d2regc1);
	__reg_write(pcf, PCF50606_REG_D3REGC1, pcf->standby_regs.d3regc1);
	__reg_write(pcf, PCF50606_REG_LPREGC1, pcf->standby_regs.lpregc1);
	__reg_write(pcf, PCF50606_REG_ADCC1, pcf->standby_regs.adcc1);
	__reg_write(pcf, PCF50606_REG_ADCC2, pcf->standby_regs.adcc2);
	__reg_write(pcf, PCF50606_REG_PWMC1, pcf->standby_regs.pwmc1);

	mutex_unlock(&pcf->lock);

	/* Hack to fix the gta01 power button problem on resume */
	pcf50606_irq(0, pcf);

	return 0;
}
#else
#define pcf50606_suspend NULL
#define pcf50606_resume NULL
#endif

static struct i2c_driver pcf50606_driver = {
	.driver = {
		.name	 = "pcf50606",
		.suspend = pcf50606_suspend,
		.resume	 = pcf50606_resume,
	},
	.id		= I2C_DRIVERID_PCF50606,
	.attach_adapter	= pcf50606_attach_adapter,
	.detach_client	= pcf50606_detach_client,
};

/* platform driver, since i2c devices don't have platform_data */
static int __init pcf50606_plat_probe(struct platform_device *pdev)
{
	struct pcf50606_platform_data *pdata = pdev->dev.platform_data;

	if (!pdata)
		return -ENODEV;

	pcf50606_pdev = pdev;

	return 0;
}

static int pcf50606_plat_remove(struct platform_device *pdev)
{
	return 0;
}

static struct platform_driver pcf50606_plat_driver = {
	.probe	= pcf50606_plat_probe,
	.remove	= pcf50606_plat_remove,
	.driver = {
		.owner	= THIS_MODULE,
		.name 	= "pcf50606",
	},
};

static int __init pcf50606_init(void)
{
	int rc;

	rc = platform_driver_register(&pcf50606_plat_driver);
	if (!rc)
		rc = i2c_add_driver(&pcf50606_driver);

	return rc;
}

static void pcf50606_exit(void)
{
	i2c_del_driver(&pcf50606_driver);
	platform_driver_unregister(&pcf50606_plat_driver);
}

MODULE_DESCRIPTION("I2C chip driver for NXP PCF50606 power management unit");
MODULE_AUTHOR("Harald Welte <laforge@openmoko.org>");
MODULE_LICENSE("GPL");

module_init(pcf50606_init);
module_exit(pcf50606_exit);