aboutsummaryrefslogtreecommitdiff
path: root/drivers/iio/magnetometer/mz_mag.c
blob: a018c604c4c4691054838bdb153c1405dca41b25 (plain) (blame)
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
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
/*
 * Copyright (C) 2015 Meizu.
 *
 * This software is licensed under the terms of the GNU General Public
 * License version 2, as published by the Free Software Foundation, and
 * may be copied, distributed, and modified under those terms.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 */

#include <linux/i2c.h>
#include <linux/slab.h>
#include <linux/delay.h>
#include <linux/input.h>
#include <linux/delay.h>
#include <linux/module.h>
#include <linux/uaccess.h>
#include <linux/workqueue.h>
#include <linux/dma-mapping.h>

#include <linux/iio/iio.h>
#include <linux/iio/sysfs.h>
#include <linux/iio/trigger.h>
#include <linux/iio/buffer.h>
#include <linux/iio/trigger_consumer.h>
#include <linux/iio/triggered_buffer.h>
#include <asm/unaligned.h>

#include <linux/meizu-sys.h>
#include <linux/meizu-sensors.h>

#define MZ_MAG_DEV_NAME "mz_mag"
#define MZ_MAG_I2C_NAME "mz_mag_i2c"
#define MZ_MAG_DEFAULT_MIN_POLLRATE 10
#define MZ_MAG_DEFAULT_POLLRATE 100
#define MZ_MAG_DEFAULT_RANGE 2
#define ABSMIN_MAG	-32767
#define ABSMAX_MAG	32767

#define	I2C_RETRY_DELAY     5      /* Waiting for signals [ms] */
#define	I2C_RETRIES         5      /* Number of retries */
#define	I2C_AUTO_INCREMENT  0x80   /* Autoincrement i2c address */

//#define MZ_MAG_DEBUG 1
#ifdef MZ_MAG_DEBUG
#define LOG_TAG_MZ_MAG "[mz_mag]"
#define pr_info(format, arg...)         printk(KERN_EMERG LOG_TAG_MZ_MAG format , ## arg)
#define dev_err(dev, format, arg...)    printk(KERN_EMERG LOG_TAG_MZ_MAG format , ## arg)
#define dev_info(dev, format, arg...)   printk(KERN_EMERG LOG_TAG_MZ_MAG format , ## arg)
#define dev_dbg(dev, format, arg...)    printk(KERN_EMERG LOG_TAG_MZ_MAG format , ## arg)
#define dev_warn(dev, format, arg...)   printk(KERN_EMERG LOG_TAG_MZ_MAG format , ## arg)
#define dev_notice(dev, format, arg...) printk(KERN_EMERG LOG_TAG_MZ_MAG format , ## arg)
#endif

/*
	axis_map_x, axis_map_y, axis_map_z can
	be used to swap x, y and z axes.
	If axis_map_x is 0 then it will be mapped to x-axis in android,
	if it is 1 then it will be mapped to y axis in Android and so on.

	To change the axes direction we can set negate_x,
	negate_y and negate_z value to 1.
	If negate_x, negate_y and negate_z values are set to 1
	then driver will negate accelerometer axes readings
	before passing to user space (Android).
*/
struct mz_mag_rotation {
	u8 axis_map_x;
	u8 axis_map_y;
	u8 axis_map_z;

	u8 negate_x;
	u8 negate_y;
	u8 negate_z;
};

struct mz_mag_data;

struct mz_mag_ops
{
	int (*enable_device)(struct mz_mag_data *mz_mag);
	int (*disable_device)(struct mz_mag_data *mz_mag);
	int (*set_range)(struct mz_mag_data *mz_mag, int range);
	int (*get_range)(struct mz_mag_data *mz_mag);
	int (*set_pollrate)(struct mz_mag_data *mz_mag, int pollrate);
	int (*get_pollrate)(struct mz_mag_data *mz_mag);
	int (*get_mag_data)(struct mz_mag_data *mz_mag, int16_t *mag_buf);
	int (*self_test)(struct mz_mag_data *mz_mag);
};

struct mz_mag_data {

	struct i2c_client *client;
	struct input_dev *input_dev;
	struct delayed_work input_work;

	struct iio_dev *indio_dev;
	struct iio_trigger *trig;
	u8    *iio_buffer_data;
	struct workqueue_struct *iio_workq;
	struct delayed_work iio_work;

	struct mutex lock;

	struct mz_mag_rotation *rota;
	struct mz_mag_ops *ops;

	uint16_t device_id;
	char    *device_name;
	uint8_t  sens[3];
	atomic_t enabled;
	int      range;
	int      pollrate;
	int      odr;
	int      min_pollrate;
};



/* ====================================================
	st480 Driver
   ==================================================== */

#define ST480_DEVICE_ID 0x7C
#define ST480_DEVICE_NAME "st480"
/*
 * register shift
 */
#define ST480_REG_DRR_SHIFT 2

/*
 * BURST MODE(INT)
 */
#define ST480_BURST_MODE 0
#define BURST_MODE_CMD 0x1F
#define BURST_MODE_DATA_LOW 0x01

/*
 * SINGLE MODE
 */
#define ST480_SINGLE_MODE 1
#define SINGLE_MEASUREMENT_MODE_CMD 0x3F

/*
 * register
 */
#define READ_MEASUREMENT_CMD 0x4F
#define WRITE_REGISTER_CMD 0x60
#define READ_REGISTER_CMD 0x50
#define EXIT_REGISTER_CMD 0x80
#define MEMORY_RECALL_CMD 0xD0
#define MEMORY_STORE_CMD 0xE0
#define RESET_CMD 0xF0

#define CALIBRATION_REG (0x02 << ST480_REG_DRR_SHIFT)
#define CALIBRATION_DATA_LOW 0x1C
#define CALIBRATION_DATA_HIGH 0x00

#define ONE_INIT_DATA_LOW 0x7C
#define ONE_INIT_DATA_HIGH 0x00
#define ONE_INIT_REG (0x00 << ST480_REG_DRR_SHIFT)

#define TWO_INIT_DATA_LOW 0x00
#define TWO_INIT_DATA_HIGH 0x00
#define TWO_INIT_REG (0x02 << ST480_REG_DRR_SHIFT)

#define TEMP_DATA_LOW 0x00
#define TEMP_DATA_HIGH 0x00
#define TEMP_REG (0x01 << ST480_REG_DRR_SHIFT)

static int st480_i2c_transfer_data(struct i2c_client *client,
							int len, char *buf, int length)
{

	int ret;
	int tries = 0;

	struct i2c_msg msgs[] = {
		{
			.addr  =  client->addr,
			.flags  =  0,
			.len  =  len,
			.buf  =  buf,
		},
		{
			.addr  =  client->addr,
			.flags  = I2C_M_RD,
			.len  =  length,
			.buf  =  buf,
		},
	};

	do {
		ret = i2c_transfer(client->adapter, msgs, 2);
		if (ret != 2)
			msleep_interruptible(I2C_RETRY_DELAY);
	} while ((ret != 2) && (++tries < I2C_RETRIES));

	if (ret != 2) {
		dev_err(&client->dev, "read transfer error\n");
		ret = -EIO;
	} else {
		ret = 0;
	}

	return ret;

}

/* the MTK platform can only use dma to transfer more tha 8 bytes data */
static u8 *gpDMABuf_va;
static dma_addr_t gpDMABuf_pa;

static char * st480_read_measurement(struct i2c_client *client)
{
	int ret = 0;
	char cmd[1] = {READ_MEASUREMENT_CMD};

	struct i2c_msg msgs[] = {
		{
			.addr  =  client->addr,
			.flags  =  0,
			.len  =  1,
			.buf  =  cmd,
			//.scl_rate = 400 * 1000,
		},
		{
			.addr  =  client->addr,
			/* the ext_flag is mtk specific */
			.ext_flag = (client->ext_flag | I2C_ENEXT_FLAG | I2C_DMA_FLAG),
			.flags  = I2C_M_RD,
			.len  =  9,
			.buf  =  (u8 *)gpDMABuf_pa,
			//.scl_rate = 400 * 1000,
		},
	};

	ret = i2c_transfer(client->adapter, msgs, 2);
	if(ret < 0){
		printk(KERN_EMERG "st480_i2c_transfer_data i2c_transfer error, ret: %d\n", ret);
		return NULL;
	}

	return gpDMABuf_va;

}


static int st480_self_test(struct mz_mag_data *mz_mag)
{
	int    ret;
	char * buffer;
	char * xyz_data;
	s16    hw_data_z0;
	s16    hw_data_z1;
	s16    z_bist;
	unsigned char buf[5];

	buf[0] = SINGLE_MEASUREMENT_MODE_CMD;
	st480_i2c_transfer_data(mz_mag->client, 1, buf, 1);

	msleep(500);

	/* read the normal z data */
	buffer = st480_read_measurement(mz_mag->client);
	if (NULL == buffer) {
		dev_err(&mz_mag->client->dev, "Failed to read measurement\n");
		ret = -1;
		goto err1;
	}

	xyz_data   = buffer + 3;
	hw_data_z0 = (xyz_data[4]<<8)|xyz_data[5];

	/* enter self test mode */
	buf[0] = WRITE_REGISTER_CMD;
	buf[1] = 0x01;
	buf[2] = 0x7C;
	buf[3] = ONE_INIT_REG;
	st480_i2c_transfer_data(mz_mag->client, 4, buf, 1);

	msleep(250);

	buf[0] = SINGLE_MEASUREMENT_MODE_CMD;
	st480_i2c_transfer_data(mz_mag->client, 1, buf, 1);

	msleep(500);

	/* read the self test z data */
	buffer = st480_read_measurement(mz_mag->client);
	if (NULL == buffer) {
		dev_err(&mz_mag->client->dev, "Failed to read measurement\n");
		ret = -1;
		goto err2;
	}

	xyz_data   = buffer + 3;
	hw_data_z1 = (xyz_data[4]<<8)|xyz_data[5];

	/* calculate z bist */
	z_bist = hw_data_z1 - hw_data_z0;
	dev_dbg(&mz_mag->client->dev, "z1: %d, z0: %d, z_bist: %d\n",
		hw_data_z1, hw_data_z0, z_bist);
	if (z_bist >= -180 && z_bist <= -90) {
		dev_dbg(&mz_mag->client->dev, "st480 self test success!\n");
		ret = 0;
	} else {
		dev_err(&mz_mag->client->dev, "self test failed\n");
		ret = -1;
	}

err2:
	buf[0] = WRITE_REGISTER_CMD;
	buf[1] = 0x00;
	buf[2] = 0x7C;
	buf[3] = ONE_INIT_REG;
	st480_i2c_transfer_data(mz_mag->client, 4, buf, 1);
err1:
	return ret;

}

#define READ_MEASUREMENT_XYZ_CMD        (0x4E)
#define READ_MEASUREMENT_T_CMD          (0x41)
static int st480_get_mag_data(struct mz_mag_data *mz_mag, int16_t * mag_data)
{
	int    ret;
	char   cmd[1];
	char * buffer;
	char * temp_data;
	char * xyz_data;
	s16    hw_data[3];
	int16_t mag_x, mag_y, mag_z;

	buffer = st480_read_measurement(mz_mag->client);
	if (NULL == buffer) {
		dev_err(&mz_mag->client->dev, "Failed to read measurement\n");
		ret = -1;
		goto out;
	}

	temp_data = buffer + 1;
	xyz_data  = buffer + 3;

	if(!((buffer[0]>>4) & 0X01)) {

		hw_data[0] = (xyz_data[0]<<8)|xyz_data[1];
		hw_data[1] = (xyz_data[2]<<8)|xyz_data[3];
		hw_data[2] = (xyz_data[4]<<8)|xyz_data[5];

		mag_x = ((mz_mag->rota->negate_x)?(-hw_data[mz_mag->rota->axis_map_x])
				: (hw_data[mz_mag->rota->axis_map_x]));
		mag_y = ((mz_mag->rota->negate_y)?(-hw_data[mz_mag->rota->axis_map_y])
				: (hw_data[mz_mag->rota->axis_map_y]));
		mag_z = ((mz_mag->rota->negate_z)?(-hw_data[mz_mag->rota->axis_map_z])
				: (hw_data[mz_mag->rota->axis_map_z]));

		if( ((temp_data[0]<<8)|(temp_data[1])) > 46244) {

			mag_x = mag_x * (1 + (70/128/4096)
						  * (((temp_data[0]<<8)|(temp_data[1])) - 46244));
			mag_y = mag_y * (1 + (70/128/4096)
						  * (((temp_data[0]<<8)|(temp_data[1])) - 46244));
			mag_z = mag_z * (1 + (70/128/4096)
						  * (((temp_data[0]<<8)|(temp_data[1])) - 46244));

		} else if( ((temp_data[0]<<8)|(temp_data[1])) < 46244) {

			mag_x = mag_x * (1 + (60/128/4096)
						  * (((temp_data[0]<<8)|(temp_data[1])) - 46244));
			mag_y = mag_y * (1 + (60/128/4096)
						  * (((temp_data[0]<<8)|(temp_data[1])) - 46244));
			mag_z = mag_z * (1 + (60/128/4096)
						  * (((temp_data[0]<<8)|(temp_data[1])) - 46244));
		}
		dev_dbg(&mz_mag->client->dev,
				"st480 raw data: x = %d, y = %d, z = %d \n",
				mag_x,mag_y,mag_z);

		mag_data[0] = mag_x;
		mag_data[1] = mag_y;
		mag_data[2] = mag_z;
		ret = 0;

	} else {

		dev_dbg(&mz_mag->client->dev,
			"get data state: 0x%X \n", buffer[0]);
		dev_dbg(&mz_mag->client->dev,
			"temp data: 0x%02X 0x%02X\n", temp_data[0], temp_data[1]);
		dev_dbg(&mz_mag->client->dev,
			"xyz  data: 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X \n",
			xyz_data[0], xyz_data[1], xyz_data[2],
			xyz_data[3], xyz_data[4], xyz_data[5]);

		ret = -1;
	}

out:
	cmd[0] = SINGLE_MEASUREMENT_MODE_CMD;
	st480_i2c_transfer_data(mz_mag->client, 1, cmd, 1);
	return ret;
}

struct mz_mag_ops st480_ops = {
	.get_mag_data = st480_get_mag_data,
	.self_test    = st480_self_test,
};

struct mz_mag_rotation st480_rota = {
	.axis_map_x = 0,
	.axis_map_y = 1,
	.axis_map_z = 2,
	.negate_x   = 1,
	.negate_y   = 1,
	.negate_z   = 0,
};
static int st480_hw_init(struct mz_mag_data *mz_mag)
{
	unsigned char buf[5];

#define ST480_DMA_MAX_TRANSACTION_LENGTH  255
	gpDMABuf_va = (u8 *)dma_alloc_coherent(&mz_mag->client->dev,
		ST480_DMA_MAX_TRANSACTION_LENGTH, &gpDMABuf_pa, GFP_KERNEL);
	if(!gpDMABuf_va){
		printk(KERN_EMERG "Allocate DMA I2C Buffer failed!\n");
		return -1;
	}

	memset(buf, 0, 5);

	buf[0] = READ_REGISTER_CMD;
	buf[1] = 0x00;
	st480_i2c_transfer_data(mz_mag->client, 2, buf, 3);

	if(buf[2] != ST480_DEVICE_ID) {
		dev_dbg(&mz_mag->client->dev, "mag device is not st480\n");
		return -1;
	}

	dev_dbg(&mz_mag->client->dev, "mag device is st480\n");
	dev_dbg(&mz_mag->client->dev, "device id: 0x00%02X\n", buf[2]);

	mz_mag->device_id    = ST480_DEVICE_ID;
	mz_mag->device_name  = ST480_DEVICE_NAME;
	mz_mag->rota         = &st480_rota;
	mz_mag->ops          = &st480_ops;
	mz_mag->min_pollrate = 10;

	//init register step 1
	buf[0] = WRITE_REGISTER_CMD;
	buf[1] = ONE_INIT_DATA_HIGH;
	buf[2] = ONE_INIT_DATA_LOW;
	buf[3] = ONE_INIT_REG;
	st480_i2c_transfer_data(mz_mag->client, 4, buf, 1);

	//init register step 2
	buf[0] = WRITE_REGISTER_CMD;
	buf[1] = TWO_INIT_DATA_HIGH;
	buf[2] = TWO_INIT_DATA_LOW;
	buf[3] = TWO_INIT_REG;
	st480_i2c_transfer_data(mz_mag->client, 4, buf, 1);

	//disable temperature compensation register
	buf[0] = WRITE_REGISTER_CMD;
	buf[1] = TEMP_DATA_HIGH;
	buf[2] = TEMP_DATA_LOW;
	buf[3] = TEMP_REG;
	st480_i2c_transfer_data(mz_mag->client, 4, buf, 1);

	//set calibration register
	buf[0] = WRITE_REGISTER_CMD;
	buf[1] = CALIBRATION_DATA_HIGH;
	buf[2] = CALIBRATION_DATA_LOW;
	buf[3] = CALIBRATION_REG;
	st480_i2c_transfer_data(mz_mag->client, 4, buf, 1);

	//set mode config
	buf[0] = SINGLE_MEASUREMENT_MODE_CMD;
	st480_i2c_transfer_data(mz_mag->client, 1, buf, 1);

	return 0;
}


/* ====================================================
	akm09911 Driver
   ==================================================== */



#define AKM09911_DEVICE_ID    0x0548
#define AKM09911_DEVICE_NAME  "ak09911"
#define AKM09911_REG_WIA1     0x00
#define AKM09911_REG_WIA2     0x01
#define AKM09911_REG_INFO1    0x02
#define AKM09911_REG_INFO2    0x03
#define AKM09911_REG_ST1      0x10
#define AKM09911_REG_DATA     AKM09911_REG_HXL
#define AKM09911_REG_HXL      0x11
#define AKM09911_REG_HXH      0x12
#define AKM09911_REG_HYL      0x13
#define AKM09911_REG_HYH      0x14
#define AKM09911_REG_HZL      0x15
#define AKM09911_REG_HZH      0x16
#define AKM09911_REG_TMPS     0x17
#define AKM09911_REG_ST2      0x18
#define AKM09911_REG_CNTL1    0x30
#define AKM09911_REG_CNTL2    0x31
#define AKM09911_REG_CNTL3    0x32

#define AKM09911_FUSE_ASAX     0x60
#define AKM09911_FUSE_ASAY     0x61
#define AKM09911_FUSE_ASAZ     0x62

#define AKM09911_MODE_SNG_MEASURE   0x01
#define AKM09911_MODE_SELF_TEST     0x10
#define AKM09911_MODE_FUSE_ACCESS   0x1F
#define AKM09911_MODE_POWERDOWN     0x00
#define AKM09911_RESET_DATA         0x01


static int akm09911_i2c_read(struct i2c_client *client,
				u8 * buf, int len)
{
	int ret;
	int tries = 0;

	struct i2c_msg	msgs[] = {
		{
			.addr = client->addr,
			.flags = client->flags & I2C_M_TEN,
			.len = 1,
			.buf = buf,
		},
		{
			.addr = client->addr,
			.flags = (client->flags & I2C_M_TEN) | I2C_M_RD,
			.len = len,
			.buf = buf,
		},
	};

	do {
		ret = i2c_transfer(client->adapter, msgs, 2);
		if (ret != 2)
			msleep_interruptible(I2C_RETRY_DELAY);
	} while ((ret != 2) && (++tries < I2C_RETRIES));

	if (ret != 2) {
		dev_err(&client->dev, "read transfer error\n");
		ret = -EIO;
	} else {
		ret = 0;
	}

	return ret;
}


static int akm09911_i2c_write(struct i2c_client *client, u8 * buf,
								int len)
{
	int ret;
	int tries = 0;

	struct i2c_msg msgs[] = {
		{
			.addr = client->addr,
			.flags = client->flags & I2C_M_TEN,
			.len = len + 1,
			.buf = buf,
		},
	};

	do {
		ret = i2c_transfer(client->adapter, msgs, 1);
		if (ret != 1)
			msleep_interruptible(I2C_RETRY_DELAY);
	} while ((ret != 1) && (++tries < I2C_RETRIES));

	if (ret != 1) {
		dev_err(&client->dev, "write transfer error\n");
		ret = -EIO;
	} else {
		ret = 0;
	}

	return ret;
}


static int akm09911_get_mag_data(struct mz_mag_data *mz_mag, int16_t * mag_data)
{
	int ret;
	u8 buf[9];
	int hw_data[3];
	u8 *sens;
	int16_t mag_x, mag_y, mag_z;

	sens = mz_mag->sens;

	buf[0] = AKM09911_REG_ST1;
	ret = akm09911_i2c_read(mz_mag->client, buf, 1);
	if (ret < 0) {
		goto out;
	}

	if (!(buf[0] & 0x01)) {
		dev_dbg(&mz_mag->client->dev, "data not ready\n");
		ret = -1;
		goto out;
	}

	buf[0] = AKM09911_REG_DATA;
	ret = akm09911_i2c_read(mz_mag->client, buf, 8);
	if (ret < 0) {
		ret = -1;
		goto out;
	}

	if (buf[7] & 0x08) {
		dev_dbg(&mz_mag->client->dev, "magnetic sensor overflow\n");
		ret = -1;
		goto out;
	}

	hw_data[0] = (int)((int16_t)(buf[1]<<8)+((int16_t)buf[0]));
	hw_data[1] = (int)((int16_t)(buf[3]<<8)+((int16_t)buf[2]));
	hw_data[2] = (int)((int16_t)(buf[5]<<8)+((int16_t)buf[4]));

	hw_data[0] = ((hw_data[0] * (sens[0] + 128)) >> 7);
	hw_data[1] = ((hw_data[1] * (sens[1] + 128)) >> 7);
	hw_data[2] = ((hw_data[2] * (sens[2] + 128)) >> 7);

#if 0
	mag_x = -hw_data[0];
	mag_y =  hw_data[1];
	mag_z = -hw_data[2];
#else
	mag_x = ((mz_mag->rota->negate_x)
			? (-hw_data[mz_mag->rota->axis_map_x])
			: (hw_data[mz_mag->rota->axis_map_x]));
	mag_y = ((mz_mag->rota->negate_y)
			? (-hw_data[mz_mag->rota->axis_map_y])
			: (hw_data[mz_mag->rota->axis_map_y]));
	mag_z = ((mz_mag->rota->negate_z)
			? (-hw_data[mz_mag->rota->axis_map_z])
			: (hw_data[mz_mag->rota->axis_map_z]));
#endif

#if 0
	dev_dbg(&mz_mag->client->dev, "mag data: %d %d %d\n",
		mag_x, mag_y, mag_z);
#endif

	mag_data[0] = mag_x;
	mag_data[1] = mag_y;
	mag_data[2] = mag_z;
	ret = 0;

out:
	buf[0] = 0x31;
	buf[1] = 0x01;
	akm09911_i2c_write(mz_mag->client, buf, 1);

	return ret;
}

static int akm09911_self_test(struct mz_mag_data *mz_mag)
{
	int i;
	int ret;
	int retry;
	int hw_data[3];
	unsigned char buf[8];
	int ak09911_st_lower[3] = {-50, -50, -400};
	int ak09911_st_upper[3] = {50, 50, -100};

	/* power down mode */
	buf[0] = AKM09911_REG_CNTL2;
	buf[1] = AKM09911_MODE_POWERDOWN;
	akm09911_i2c_write(mz_mag->client, buf, 1);

	/* self test mode */
	buf[0] = AKM09911_REG_CNTL2;
	buf[1] = AKM09911_MODE_SELF_TEST;
	akm09911_i2c_write(mz_mag->client, buf, 1);

	/* wait for data ready */
	retry = 10;
	while (retry--) {
		msleep(100);
		buf[0] = AKM09911_REG_ST1;
		ret = akm09911_i2c_read(mz_mag->client, buf, 1);
		if (ret < 0) {
			goto err1;
		}

		if (!(buf[0] & 0x01)) {
			dev_dbg(&mz_mag->client->dev, "data not ready\n");
			continue;
		} else {
			dev_dbg(&mz_mag->client->dev, "data is ready\n");
			break;
		}
	}

	if (!(buf[0] & 0x01)) {
		dev_err(&mz_mag->client->dev, "failed to wait data ready\n");
		ret = -1;
		goto err2;
	}

	/* read sensor data */
	buf[0] = AKM09911_REG_DATA;
	ret = akm09911_i2c_read(mz_mag->client, buf, 8);
	if (ret < 0) {
		ret = -1;
		goto err2;
	}

	if (buf[7] & 0x08) {
		dev_dbg(&mz_mag->client->dev, "magnetic sensor overflow\n");
		ret = -1;
		goto err2;
	}

	hw_data[0] = (int)((int16_t)(buf[1]<<8)+((int16_t)buf[0]));
	hw_data[1] = (int)((int16_t)(buf[3]<<8)+((int16_t)buf[2]));
	hw_data[2] = (int)((int16_t)(buf[5]<<8)+((int16_t)buf[4]));

	hw_data[0] = ((hw_data[0] * (mz_mag->sens[0] + 128)) >> 7);
	hw_data[1] = ((hw_data[1] * (mz_mag->sens[1] + 128)) >> 7);
	hw_data[2] = ((hw_data[2] * (mz_mag->sens[2] + 128)) >> 7);

	dev_dbg(&mz_mag->client->dev, "data:  %d %d %d\n",
		hw_data[0], hw_data[1], hw_data[2]);
	dev_dbg(&mz_mag->client->dev, "upper: %d %d %d\n",
		ak09911_st_upper[0], ak09911_st_upper[1], ak09911_st_upper[2]);
	dev_dbg(&mz_mag->client->dev, "lower: %d %d %d\n",
		ak09911_st_lower[0], ak09911_st_lower[1], ak09911_st_lower[2]);

	ret = 0;
	for (i=0; i<3; i++) {
		if (hw_data[i]>ak09911_st_upper[i]
		 || hw_data[i]<ak09911_st_lower[i]) {
			dev_err(&mz_mag->client->dev, "self test failed\n");
			ret = -1;
			break;
		}
	}

	if (ret == 0) {
		dev_dbg(&mz_mag->client->dev, "self test success\n");
	}

err2:
err1:
	/* revert to power down mode */
	buf[0] = AKM09911_REG_CNTL2;
	buf[1] = AKM09911_MODE_POWERDOWN;
	akm09911_i2c_write(mz_mag->client, buf, 1);
	return ret;
}

struct mz_mag_ops akm09911_ops = {
	.get_mag_data = akm09911_get_mag_data,
	.self_test    = akm09911_self_test,
};

struct mz_mag_rotation akm09911_rota = {
	.axis_map_x = 1,
	.axis_map_y = 0,
	.axis_map_z = 2,
	.negate_x   = 0,
	.negate_y   = 0,
	.negate_z   = 1,
};

static int akm09911_hw_init(struct mz_mag_data *mz_mag)
{
	unsigned char buf[4];

	memset(buf, 0, 4);

	/* get device id of magnetometer */
	buf[0] = AKM09911_REG_WIA1;
	akm09911_i2c_read(mz_mag->client, buf, 2);

	if (AKM09911_DEVICE_ID != (buf[1]<<8|buf[0])) {
		dev_dbg(&mz_mag->client->dev, "mag device is not akm09911\n");
		return -1;
	}

	dev_dbg(&mz_mag->client->dev, "mag device is akm09911\n");
	dev_dbg(&mz_mag->client->dev, "device id: 0x%02X%02X\n", buf[1], buf[0]);

	mz_mag->device_id = AKM09911_DEVICE_ID;
	mz_mag->device_name = AKM09911_DEVICE_NAME;
	mz_mag->rota      = &akm09911_rota;
	mz_mag->ops       = &akm09911_ops;

	/* set AKM to Fuse ROM access mode */
	buf[0] = AKM09911_REG_CNTL2;
	buf[1] = AKM09911_MODE_FUSE_ACCESS;
	akm09911_i2c_write(mz_mag->client, buf, 1);

	/* read sens */
	buf[0] = AKM09911_FUSE_ASAX;
	akm09911_i2c_read(mz_mag->client, buf, 3);

	dev_dbg(&mz_mag->client->dev, "mag sens: 0x%X 0x%X 0x%X\n",
		buf[0], buf[1], buf[2]);

	mz_mag->sens[0] = buf[0];
	mz_mag->sens[1] = buf[1];
	mz_mag->sens[2] = buf[2];

	/* revert to power down mode */
	buf[0] = AKM09911_REG_CNTL2;
	buf[1] = AKM09911_MODE_POWERDOWN;
	akm09911_i2c_write(mz_mag->client, buf, 1);

	return 0;
}


/* ====================================================
	af8133 Driver
   ==================================================== */

/*AF8133 register address*/
#define AF8133_REG_PCODE	0x00
#define AF8133_REG_STATUS1	0x02
#define AF8133_REG_DATA  	0x03
#define AF8133_REG_STATUS	0x0A
#define AF8133_REG_RANGE1	0x0B
#define AF8133_REG_SWR		0x11
#define AF8133_DEVICE_ID	0x50

#define FIND_SW_OFFSET_LOOP    5
#define FIND_SW_OFFSET_INDEX   2

static int mag_pos[3][FIND_SW_OFFSET_LOOP];
static int mag_neg[3][FIND_SW_OFFSET_LOOP];
static int mag_offset[3];
static int mag_cnt=0;

static int af8133_i2c_read(struct i2c_client *client,
				u8 * buf, int len)
{
	int ret;
	int tries = 0;

	struct i2c_msg	msgs[] = {
		{
			.addr = client->addr,
			.flags = client->flags & I2C_M_TEN,
			.len = 1,
			.buf = buf,
		},
		{
			.addr = client->addr,
			.flags = (client->flags & I2C_M_TEN) | I2C_M_RD,
			.len = len,
			.buf = buf,
		},
	};

	do {
		ret = i2c_transfer(client->adapter, msgs, 2);
		if (ret != 2)
			msleep_interruptible(I2C_RETRY_DELAY);
	} while ((ret != 2) && (++tries < I2C_RETRIES));

	if (ret != 2) {
		dev_err(&client->dev, "read transfer error\n");
		ret = -EIO;
	} else {
		ret = 0;
	}

	return ret;
}


static int af8133_i2c_write(struct i2c_client *client, u8 * buf,
								int len)
{
	int ret;
	int tries = 0;

	struct i2c_msg msgs[] = {
		{
			.addr = client->addr,
			.flags = client->flags & I2C_M_TEN,
			.len = len + 1,
			.buf = buf,
		},
	};

	do {
		ret = i2c_transfer(client->adapter, msgs, 1);
		if (ret != 1)
			msleep_interruptible(I2C_RETRY_DELAY);
	} while ((ret != 1) && (++tries < I2C_RETRIES));

	if (ret != 1) {
		dev_err(&client->dev, "write transfer error\n");
		ret = -EIO;
	} else {
		ret = 0;
	}

	return ret;
}


static int af8133_get_mag_data(struct mz_mag_data *mz_mag, int16_t * mag_data)
{
	int ret;
	u8 buf[9];
	int hw_data[3];
	u8 *sens;
	int i, j, k;
	int16_t mag_x, mag_y, mag_z;

	sens = mz_mag->sens;

	buf[0] = AF8133_REG_STATUS1;
	ret = af8133_i2c_read(mz_mag->client, buf, 1);
	if (ret < 0) {
		dev_dbg(&mz_mag->client->dev, "read status1 failed\n");
		goto out;
	}

	if (!(buf[0] & 0x01)) {
		dev_dbg(&mz_mag->client->dev, "data not ready\n");
		ret = -1;
		goto out;
	}

	buf[0] = AF8133_REG_DATA;
	ret = af8133_i2c_read(mz_mag->client, buf, 6);
	if (ret < 0) {
		dev_dbg(&mz_mag->client->dev, "read data failed\n");
		ret = -1;
		goto out;
	}

	hw_data[0] = (int)((int16_t)(buf[1]<<8)+((int16_t)buf[0]));
	hw_data[1] = (int)((int16_t)(buf[3]<<8)+((int16_t)buf[2]));
	hw_data[2] = (int)((int16_t)(buf[5]<<8)+((int16_t)buf[4]));


	for(i = 0; i < 3; i++)
		hw_data[i] = (hw_data[i] > 32767) ? (hw_data[i] - 65536) : hw_data[i];

	if(mag_cnt >= 0 && mag_cnt <= (FIND_SW_OFFSET_LOOP*2)) {

		/*
		   Using the set and reset function to calculate the offset.
		   offset = (value_set + value_reset) / 2
		 */
		if(mag_cnt >= 1 && mag_cnt <= FIND_SW_OFFSET_LOOP) {
			for(i = 0; i < 3; i++)
				mag_neg[i][mag_cnt-1] = hw_data[i];
		} else if (mag_cnt > FIND_SW_OFFSET_LOOP && mag_cnt <= (FIND_SW_OFFSET_LOOP*2)) {
			for(i = 0; i < 3; i++)
				mag_pos[i][mag_cnt-FIND_SW_OFFSET_LOOP-1] = hw_data[i];
		}

		mag_cnt++;

		if(mag_cnt >= 1 && mag_cnt <= FIND_SW_OFFSET_LOOP) {
			buf[0] = 0x14;
			buf[1] = 0x34;
			ret = af8133_i2c_write(mz_mag->client, buf, 1);
		}
		else if(mag_cnt > FIND_SW_OFFSET_LOOP && mag_cnt <= (FIND_SW_OFFSET_LOOP*2))
		{
			buf[0] = 0x14;
			buf[1] = 0x38;
			ret = af8133_i2c_write(mz_mag->client, buf, 1);
		}

		if(mag_cnt > (FIND_SW_OFFSET_LOOP*2)) {
			for(i=0;i<3;i++) {
				for(j=0;j<(FIND_SW_OFFSET_LOOP-1);j++) {
					for(k=0;k<(FIND_SW_OFFSET_LOOP-1);k++){
						if(mag_neg[i][k] < mag_neg[i][k+1]) {
							int tmp = mag_neg[i][k];
							mag_neg[i][k] = mag_neg[i][k+1];
							mag_neg[i][k+1] = tmp;
						}

						if(mag_pos[i][k] < mag_pos[i][k+1]) {
							int tmp = mag_pos[i][k];
							mag_pos[i][k] = mag_pos[i][k+1];
							mag_pos[i][k+1] = tmp;
						}
					}
				}
				mag_offset[i] = (mag_pos[i][(FIND_SW_OFFSET_INDEX)] + mag_neg[i][FIND_SW_OFFSET_INDEX]) / 2;
			}
		}

	} else {

		buf[0] = 0x14;
		buf[1] = 0x38;
		ret = af8133_i2c_write(mz_mag->client, buf, 1);
		if(ret < 0) return ret;
	}

	for(i = 0; i < 3; i++)
		hw_data[i] -= mag_offset[i];

	//dev_dbg(&mz_mag->client->dev, "hw_data: %d %d %d\n", hw_data[0], hw_data[1], hw_data[2]);
#if 1
	/* the rotation is in the hal layer */
	mag_x = hw_data[0];
	mag_y = hw_data[1];
	mag_z = hw_data[2];
#else
	mag_x = ((mz_mag->rota->negate_x)
			? (-hw_data[mz_mag->rota->axis_map_x])
			: (hw_data[mz_mag->rota->axis_map_x]));
	mag_y = ((mz_mag->rota->negate_y)
			? (-hw_data[mz_mag->rota->axis_map_y])
			: (hw_data[mz_mag->rota->axis_map_y]));
	mag_z = ((mz_mag->rota->negate_z)
			? (-hw_data[mz_mag->rota->axis_map_z])
			: (hw_data[mz_mag->rota->axis_map_z]));
#endif

#if 0
	dev_dbg(&mz_mag->client->dev, "mag data: %d %d %d\n",
		mag_x, mag_y, mag_z);
#endif

	mag_data[0] = mag_x;
	mag_data[1] = mag_y;
	mag_data[2] = mag_z;
	ret = 0;

out:
	buf[0] = AF8133_REG_STATUS;
	buf[1] = 0x01;
	af8133_i2c_write(mz_mag->client, buf, 1);

	return ret;
}

struct mz_mag_ops af8133_ops = {
	.get_mag_data = af8133_get_mag_data,
};

struct mz_mag_rotation af8133_rota = {
	.axis_map_x = 0,
	.axis_map_y = 1,
	.axis_map_z = 2,
	.negate_x   = 0,
	.negate_y   = 0,
	.negate_z   = 0,
};

static int af8133_hw_init(struct mz_mag_data *mz_mag)
{
	unsigned char buf[4];

	memset(buf, 0, 4);

	/* get device id of magnetometer */
	buf[0] = AF8133_REG_PCODE;
	af8133_i2c_read(mz_mag->client, buf, 1);

	if (AF8133_DEVICE_ID != buf[0]) {
		dev_dbg(&mz_mag->client->dev, "mag device is not af8133, id:0x%x\n", buf[0]);
		return -1;
	}

	dev_dbg(&mz_mag->client->dev, "mag device is af8133\n");
	dev_dbg(&mz_mag->client->dev, "device id: 0x%02X\n", buf[0]);

	mz_mag->device_id = AF8133_DEVICE_ID;
	mz_mag->rota      = &af8133_rota;
	mz_mag->ops       = &af8133_ops;

	mz_mag->sens[0] = 0;
	mz_mag->sens[1] = 0;
	mz_mag->sens[2] = 0;

	buf[0] = 0x10;
	buf[1] = 0x55;
	af8133_i2c_write(mz_mag->client, buf, 1);

	buf[0] = 0x14;
	buf[1] = 0x34;
	af8133_i2c_write(mz_mag->client, buf, 1);

	buf[0] = 0x33;
	buf[1] = 0x16;
	af8133_i2c_write(mz_mag->client, buf, 1);

	buf[0] = 0x0B;
	buf[1] = 0x3C;
	af8133_i2c_write(mz_mag->client, buf, 1);

	buf[0] = 0x13;
	buf[1] = 0x00;
	af8133_i2c_write(mz_mag->client, buf, 1);

	buf[0] = 0x0A;
	buf[1] = 0x01;
	af8133_i2c_write(mz_mag->client, buf, 1);

	mag_cnt       = 0;
	mag_offset[0] = 0;
	mag_offset[1] = 0;
	mag_offset[2] = 0;

	return 0;
}



/* ====================================================
	Meizu Magnetometer Driver
   ==================================================== */

/* sysfs interfaces */

static inline int mz_mag_enable(struct mz_mag_data *mz_mag)
{
	int ret = 0;

	if (!atomic_cmpxchg(&mz_mag->enabled, 0, 1)) {

		if (mz_mag->ops->enable_device)
			ret = mz_mag->ops->enable_device(mz_mag);

		if (ret < 0) {
			atomic_set(&mz_mag->enabled, 0);
			return ret;
		}

		dev_dbg(&mz_mag->client->dev, "mag input work start\n");
		schedule_delayed_work(&mz_mag->input_work,
			msecs_to_jiffies(mz_mag->pollrate));
	}

	return 0;
}

static inline int mz_mag_disable(struct mz_mag_data *mz_mag)
{
	if (atomic_cmpxchg(&mz_mag->enabled, 1, 0)) {

		dev_dbg(&mz_mag->client->dev, "mag input work stop\n");
		cancel_delayed_work_sync(&mz_mag->input_work);

		if (mz_mag->ops->disable_device)
			mz_mag->ops->disable_device(mz_mag);
	}

	return 0;
}


static inline int mz_mag_iio_enable(struct mz_mag_data *mz_mag)
{
	int ret = 0;

	if (!atomic_cmpxchg(&mz_mag->enabled, 0, 1)) {

		if (mz_mag->ops->enable_device)
			ret = mz_mag->ops->enable_device(mz_mag);

		if (ret < 0) {
			atomic_set(&mz_mag->enabled, 0);
			return ret;
		}

		dev_dbg(&mz_mag->client->dev, "mag iio work start\n");
		queue_delayed_work(mz_mag->iio_workq, &mz_mag->iio_work,
			msecs_to_jiffies(mz_mag->pollrate));
	}

	return 0;
}

static inline int mz_mag_iio_disable(struct mz_mag_data *mz_mag)
{
	if (atomic_cmpxchg(&mz_mag->enabled, 1, 0)) {

		dev_dbg(&mz_mag->client->dev, "mag iio work stop\n");
		cancel_delayed_work_sync(&mz_mag->iio_work);

		if (mz_mag->ops->disable_device)
			mz_mag->ops->disable_device(mz_mag);
	}

	return 0;
}


static ssize_t attr_pollrate_ms_show(struct device *dev,
					struct device_attribute *attr,
								char *buf)
{
	int val;
	struct mz_mag_data *mz_mag = dev_get_drvdata(dev);

	mutex_lock(&mz_mag->lock);
	val = mz_mag->pollrate;
	mutex_unlock(&mz_mag->lock);

	dev_dbg(&mz_mag->client->dev, "get mag pollrate: %d\n", val);
	return sprintf(buf, "%d\n", val);
}

static ssize_t attr_pollrate_ms_store(struct device *dev,
					struct device_attribute *attr,
						const char *buf, size_t size)
{
	int ret = 0;
	struct mz_mag_data *mz_mag = dev_get_drvdata(dev);
	unsigned long rate;

	dev_dbg(&mz_mag->client->dev, "set mag pollrate: %s\n", buf);

	if (strict_strtoul(buf, 10, &rate))
		return -EINVAL;
	if (!rate)
		return -EINVAL;

	rate = rate > mz_mag->min_pollrate ? rate : mz_mag->min_pollrate;

	mutex_lock(&mz_mag->lock);

	if (mz_mag->ops->set_pollrate)
		ret = mz_mag->ops->set_pollrate(mz_mag, rate);

	if(ret >= 0) {
		mz_mag->pollrate = rate;
	}

	mutex_unlock(&mz_mag->lock);

	return size;
}


static ssize_t attr_full_scale_show(struct device *dev,
				struct device_attribute *attr, char *buf)
{
	struct mz_mag_data *mz_mag = dev_get_drvdata(dev);
	int range = 2;

	mutex_lock(&mz_mag->lock);

	range = mz_mag->range;

	mutex_unlock(&mz_mag->lock);

	dev_dbg(&mz_mag->client->dev, "get mag range: %d\n", range);
	return sprintf(buf, "%d\n", range);
}

static ssize_t attr_full_scale_store(struct device *dev,
				struct device_attribute *attr,
						const char *buf, size_t size)
{
	struct mz_mag_data *mz_mag = dev_get_drvdata(dev);
	unsigned long range;

	dev_dbg(&mz_mag->client->dev, "set mag range: %s\n", buf);

	if (strict_strtoul(buf, 10, &range))
		return -EINVAL;

	mutex_lock(&mz_mag->lock);

	if (mz_mag->ops->set_range)
		mz_mag->ops->set_range(mz_mag, range);

	mz_mag->range = range;
	mutex_unlock(&mz_mag->lock);

	return size;
}

static ssize_t attr_enable_device_show(struct device *dev,
				struct device_attribute *attr, char *buf)
{
	struct mz_mag_data *mz_mag = dev_get_drvdata(dev);
	int val = atomic_read(&mz_mag->enabled);
	return sprintf(buf, "%d\n", val);
}

static ssize_t attr_enable_device_store(struct device *dev,
				struct device_attribute *attr,
						const char *buf, size_t size)
{
	struct mz_mag_data *mz_mag = dev_get_drvdata(dev);
	unsigned long val;

	if (strict_strtoul(buf, 10, &val))
		return -EINVAL;

	if (val)
		mz_mag_enable(mz_mag);
	else
		mz_mag_disable(mz_mag);

	return size;
}

static ssize_t attr_self_test_show(struct device *dev,
				struct device_attribute *attr, char *buf)
{
	int ret = 1;
	struct mz_mag_data *mz_mag = dev_get_drvdata(dev);

	mutex_lock(&mz_mag->lock);

	if (mz_mag->ops->self_test)
		ret = mz_mag->ops->self_test(mz_mag);

	mutex_unlock(&mz_mag->lock);

	return sprintf(buf, "%d\n", ret);
}

static struct device_attribute attributes[] = {
	__ATTR(pollrate_ms, 0664, attr_pollrate_ms_show, attr_pollrate_ms_store),
	__ATTR(full_scale, 0664, attr_full_scale_show, attr_full_scale_store),
	__ATTR(enable_device, 0664, attr_enable_device_show,
		attr_enable_device_store),
	__ATTR(compass_self_test, S_IRUGO, attr_self_test_show, NULL),
};

static int create_sysfs_interfaces(struct device *dev)
{
	int i;
	for (i = 0; i < ARRAY_SIZE(attributes); i++)
		if (device_create_file(dev, attributes + i))
			goto error;
	return 0;

error:
	for ( ; i >= 0; i--)
		device_remove_file(dev, attributes + i);
	dev_err(dev, "%s:Unable to create interface\n", __func__);
	return -1;
}
#if 0
static int remove_sysfs_interfaces(struct device *dev)
{
	int i;
	for (i = 0; i < ARRAY_SIZE(attributes); i++)
		device_remove_file(dev, attributes + i);
	return 0;
}
#endif

/* input device interfaces */

static void mz_mag_input_func(struct work_struct *work)
{
	int ret;
	struct mz_mag_data *mz_mag;
	int16_t mag_data[3];

	mz_mag = container_of((struct delayed_work *)work,
					struct mz_mag_data, input_work);

	if (mz_mag->ops->get_mag_data)
		ret = mz_mag->ops->get_mag_data(mz_mag, mag_data);
	else
		ret = -1;

	if (!ret) {
		input_report_abs(mz_mag->input_dev, ABS_X, mag_data[0]);
		input_report_abs(mz_mag->input_dev, ABS_Y, mag_data[1]);
		input_report_abs(mz_mag->input_dev, ABS_Z, mag_data[2]);
		input_event(mz_mag->input_dev, EV_SYN, SYN_REPORT, mz_mag->device_id);
	}

	schedule_delayed_work(&mz_mag->input_work,
					msecs_to_jiffies(mz_mag->pollrate));
}

static int mz_mag_input_init(struct mz_mag_data *mz_mag)
{
	int ret;

	INIT_DELAYED_WORK(&mz_mag->input_work, mz_mag_input_func);

	mz_mag->input_dev = input_allocate_device();
	if (!mz_mag->input_dev) {
		ret = -ENOMEM;
		dev_err(&mz_mag->client->dev, "Failed to allocate input device\n");
		goto err0;
	}

	/* Setup input device */
	set_bit(EV_ABS, mz_mag->input_dev->evbit);

	input_set_capability(mz_mag->input_dev, EV_ABS, ABS_X);
	input_set_capability(mz_mag->input_dev, EV_ABS, ABS_Y);
	input_set_capability(mz_mag->input_dev, EV_ABS, ABS_Z);

	/* x-axis of raw magnetic vector (-32768, 32767) */
	input_set_abs_params(mz_mag->input_dev, ABS_X, ABSMIN_MAG, ABSMAX_MAG, 0, 0);
	/* y-axis of raw magnetic vector (-32768, 32767) */
	input_set_abs_params(mz_mag->input_dev, ABS_Y, ABSMIN_MAG, ABSMAX_MAG, 0, 0);
	/* z-axis of raw magnetic vector (-32768, 32767) */
	input_set_abs_params(mz_mag->input_dev, ABS_Z, ABSMIN_MAG, ABSMAX_MAG, 0, 0);

	/* Set name */
	mz_mag->input_dev->name       = MZ_MAG_DEV_NAME;
	mz_mag->input_dev->dev.parent = &mz_mag->client->dev;

	/* Register */
	ret = input_register_device(mz_mag->input_dev);
	if (ret) {
		dev_err(&mz_mag->client->dev, "Failed to register input device\n");
		goto err1;
	}
	input_set_drvdata(mz_mag->input_dev, mz_mag);

	return 0;

err1:
	input_free_device(mz_mag->input_dev);
	mz_mag->input_dev = NULL;

err0:
	return ret;
}

/* iio device interfaces */

#define MZ_MAG_IIO_NUMBER_DATA_CHANNELS		3
#define MZ_MAG_IIO_BYTE_FOR_CHANNEL		2
static void mz_mag_iio_func(struct work_struct *work)
{
	int ret;
	int i, n = 0;
	struct mz_mag_data *mz_mag;
	int16_t mag_data[3];
	int64_t timestamp;
	struct timespec ts;

	mz_mag = container_of((struct delayed_work *)work,
					struct mz_mag_data, iio_work);

	//dev_dbg(&mz_mag->client->dev, "%s: enter\n", __func__);

	if (mz_mag->ops->get_mag_data)
		ret = mz_mag->ops->get_mag_data(mz_mag, mag_data);
	else
		ret = -1;

	if (!ret) {
		//dev_dbg(&mz_mag->client->dev, "iio_buffer_data: ================\n");
		for (i = 0; i < MZ_MAG_IIO_NUMBER_DATA_CHANNELS; i++) {

			if (mz_mag->indio_dev->active_scan_mask == NULL) {
				printk(KERN_EMERG "mag bug: active_scan_mask: %p\n",
					mz_mag->indio_dev->active_scan_mask);
				return;
			}

			if (test_bit(i, mz_mag->indio_dev->active_scan_mask)) {
				memcpy(&mz_mag->iio_buffer_data[n * MZ_MAG_IIO_BYTE_FOR_CHANNEL],
						&mag_data[i],
						MZ_MAG_IIO_BYTE_FOR_CHANNEL);
				#if 0
				dev_dbg(&mz_mag->client->dev, "%d ",
					*(int16_t *)&mz_mag->iio_buffer_data[n * MZ_MAG_IIO_BYTE_FOR_CHANNEL]);
				#endif
				n++;
			}
		}

		//timestamp = iio_get_time_ns();
		get_monotonic_boottime(&ts);
		timestamp = timespec_to_ns(&ts);
		if (mz_mag->indio_dev->scan_timestamp) {
			*(s64 *)((u8 *)mz_mag->iio_buffer_data +
				ALIGN(n * MZ_MAG_IIO_BYTE_FOR_CHANNEL,
						sizeof(s64))) = timestamp;
				#if 0
				dev_dbg(&mz_mag->client->dev, "%lld ",
					*(s64 *)((u8 *)mz_mag->iio_buffer_data +
					ALIGN(n * MZ_MAG_IIO_BYTE_FOR_CHANNEL,
						sizeof(s64))));
				#endif
		}
		//dev_dbg(&mz_mag->client->dev, "\niio_buffer_data ============= \n");
		//dev_dbg(&mz_mag->client->dev, "%s: push buffer\n", __func__);
		iio_push_to_buffers(mz_mag->indio_dev, mz_mag->iio_buffer_data);
	}

	queue_delayed_work(mz_mag->iio_workq, &mz_mag->iio_work,
					msecs_to_jiffies(mz_mag->pollrate));
	//dev_dbg(&mz_mag->client->dev, "%s: leave\n", __func__);
}


int mz_mag_trig_set_state(struct iio_trigger *trig, bool state)
{
	int ret;
	struct mz_mag_data *mz_mag = iio_device_get_drvdata(
		iio_trigger_get_drvdata(trig));

	dev_dbg(&mz_mag->client->dev, "%s: enter\n", __func__);
	if (state) {
		ret = mz_mag_iio_enable(mz_mag);
	} else {
		ret = mz_mag_iio_disable(mz_mag);
	}

	return ret;
}


static const struct iio_trigger_ops mz_mag_trigger_ops = {
	.owner = THIS_MODULE,
	.set_trigger_state = &mz_mag_trig_set_state,
};

static inline irqreturn_t mz_mag_iio_handler_empty(int irq, void *p)
{
	return IRQ_HANDLED;
}


static int mz_mag_iio_buffer_preenable(struct iio_dev *indio_dev)
{
	return iio_sw_buffer_preenable(indio_dev);
}

static int mz_mag_iio_buffer_postenable(struct iio_dev *indio_dev)
{
	int err;
	struct mz_mag_data *mz_mag = iio_device_get_drvdata(indio_dev);
	dev_dbg(&mz_mag->client->dev, "%s: enter\n", __func__);

	err = iio_triggered_buffer_postenable(indio_dev);
	if (err < 0) {
		dev_err(&mz_mag->client->dev,
			"failed to call iio_triggered_buffer_postenable\n");
		return err;
	}

	dev_dbg(&mz_mag->client->dev, "%s: leave\n", __func__);
	return 0;
}

static int mz_mag_iio_buffer_predisable(struct iio_dev *indio_dev)
{
	int err;
	struct mz_mag_data *mz_mag = iio_device_get_drvdata(indio_dev);
	dev_dbg(&mz_mag->client->dev, "%s: enter\n", __func__);
	err = iio_triggered_buffer_predisable(indio_dev);
	if (err < 0) {
		dev_err(&mz_mag->client->dev,
			"failed to call iio_triggered_buffer_predisable\n");
		return err;
	}

	dev_dbg(&mz_mag->client->dev, "%s: leave\n", __func__);
	return 0;
}

static int mz_mag_iio_buffer_postdisable(struct iio_dev *indio_dev)
{
	int err;
	struct mz_mag_data *mz_mag = iio_device_get_drvdata(indio_dev);
	dev_dbg(&mz_mag->client->dev, "%s: enter\n", __func__);
	mz_mag_iio_disable(mz_mag);
	dev_dbg(&mz_mag->client->dev, "%s: leave\n", __func__);
	return 0;
}

static const struct iio_buffer_setup_ops mz_mag_iio_buffer_setup_ops = {
	.preenable  = &mz_mag_iio_buffer_preenable,
	.postenable = &mz_mag_iio_buffer_postenable,
	.predisable = &mz_mag_iio_buffer_predisable,
	.postdisable = &mz_mag_iio_buffer_postdisable,
};


static int mz_mag_iio_read_raw(struct iio_dev *indio_dev,
		struct iio_chan_spec const *ch, int *val, int *val2, long mask)
{
	u8 outdata[2];
	struct mz_mag_data *mz_mag = iio_device_get_drvdata(indio_dev);
	dev_dbg(&mz_mag->client->dev, "%s: enter\n", __func__);
	switch (mask) {
	case IIO_CHAN_INFO_RAW:
		mutex_lock(&indio_dev->mlock);

		if (indio_dev->currentmode == INDIO_BUFFER_TRIGGERED) {
			mutex_unlock(&indio_dev->mlock);
			dev_dbg(&mz_mag->client->dev, "%s: leave\n", __func__);
			return -EBUSY;
		}

		/* get data from device */
		/* outdata = xxx */

		*val = (s16)get_unaligned_le16(outdata);
		*val = *val >> ch->scan_type.shift;

		mutex_unlock(&indio_dev->mlock);
		dev_dbg(&mz_mag->client->dev, "%s: leave\n", __func__);
		return IIO_VAL_INT;

	case IIO_CHAN_INFO_SCALE:
		/* the scale is always 1 */
		*val = 1;
		*val2 = 0;

		return IIO_VAL_INT_PLUS_MICRO;
	default:
		return -EINVAL;
	}
	dev_dbg(&mz_mag->client->dev, "%s: leave\n", __func__);
	return 0;
}


#define MZ_MAG_IIO_CHANNELS(device_type, index, mod, endian, bits, addr) \
{ \
	.type = device_type, \
	.modified = 1, \
	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
			BIT(IIO_CHAN_INFO_SCALE), \
	.scan_index = index, \
	.channel2 = mod, \
	.address = addr, \
	.scan_type = { \
		.sign = 's', \
		.realbits = bits, \
		.shift = 16 - bits, \
		.storagebits = 16, \
		.endianness = endian, \
	}, \
}

#define MZ_MAG_X_L_ADDR		0x28
#define MZ_MAG_Y_L_ADDR		0x2a
#define MZ_MAG_Z_L_ADDR		0x2c

static const struct iio_chan_spec mz_mag_iio_ch[] = {
	MZ_MAG_IIO_CHANNELS(IIO_MAGN, 0, IIO_MOD_X, IIO_LE,
					16, MZ_MAG_X_L_ADDR),
	MZ_MAG_IIO_CHANNELS(IIO_MAGN, 1, IIO_MOD_Y, IIO_LE,
					16, MZ_MAG_Y_L_ADDR),
	MZ_MAG_IIO_CHANNELS(IIO_MAGN, 2, IIO_MOD_Z, IIO_LE,
					16, MZ_MAG_Z_L_ADDR),
	IIO_CHAN_SOFT_TIMESTAMP(3)
};

/* we use the hw_fifo_len to identify which ic we are using */
#define ST480_HW_FIFO_LEN   (1)
#define AK09911_HW_FIFO_LEN (2)
#define AF8133_HW_FIFO_LEN  (3)
ssize_t mz_mag_iio_sysfs_get_hw_fifo_lenght(struct device *dev,
				struct device_attribute *attr, char *buf)
{
	int hw_fifo_lenght;
	struct mz_mag_data *mz_mag = dev_get_drvdata(dev);
	if (mz_mag->device_id == ST480_DEVICE_ID) {
		hw_fifo_lenght = ST480_HW_FIFO_LEN;
	} else if (mz_mag->device_id == AKM09911_DEVICE_ID) {
		hw_fifo_lenght = AK09911_HW_FIFO_LEN;
	} else if (mz_mag->device_id == AF8133_DEVICE_ID) {
		hw_fifo_lenght = AF8133_HW_FIFO_LEN;
	} else {
		hw_fifo_lenght = 0;
	}
	return sprintf(buf, "%d\n", hw_fifo_lenght);
}

ssize_t mz_mag_iio_sysfs_flush_fifo(struct device *dev,
		struct device_attribute *attr, const char *buf, size_t size)
{
	return size;
}

static ssize_t mz_mag_iio_sysfs_sampling_frequency_avail(
		struct device *dev, struct device_attribute *attr, char *buf)
{
	return scnprintf(buf, PAGE_SIZE,
				"%d %d %d %d %d\n", 26, 52, 104, 208, 416);
}

static ssize_t mz_mag_iio_sysfs_get_sampling_frequency(
		struct device *dev, struct device_attribute *attr, char *buf)
{
	struct mz_mag_data *mz_mag = dev_get_drvdata(dev);
	dev_dbg(&mz_mag->client->dev, "%s: enter\n", __func__);
	return sprintf(buf, "%d\n", mz_mag->odr);
}

static ssize_t mz_mag_iio_sysfs_set_sampling_frequency(
			struct device *dev, struct device_attribute *attr,
						const char *buf, size_t size)
{
	int err, err2 = -EINVAL;
	int ret = 0;
	unsigned int odr;
	int      rate;
	struct mz_mag_data *mz_mag = dev_get_drvdata(dev);

	dev_dbg(&mz_mag->client->dev, "%s: enter\n", __func__);

	err = kstrtoint(buf, 10, &odr);
	if (err < 0) {
		dev_err(&mz_mag->client->dev, "kstrtoint error\n");
		return err;
	}

	dev_dbg(&mz_mag->client->dev, "set up odr: %d\n", odr);
#if 1
	mutex_lock(&mz_mag->indio_dev->mlock);

	switch (odr) {
	case 26:
	case 52:
	case 104:
	case 208:
	case 416:
		rate = 1000 / odr;
		rate = rate > mz_mag->min_pollrate ? rate : mz_mag->min_pollrate;

		if (mz_mag->ops->set_pollrate)
			ret = mz_mag->ops->set_pollrate(mz_mag, rate);

		if(ret >= 0) {
			mz_mag->pollrate = rate;
			mz_mag->odr      = odr;
			err2 = 0;
		}

		break;
	default:
		err2 = -EINVAL;
		break;
	}

	mutex_unlock(&mz_mag->indio_dev->mlock);
#endif
	dev_dbg(&mz_mag->client->dev, "%s: leave\n", __func__);
	return err2 < 0 ? err2 : size;
}


static ssize_t mz_mag_iio_sysfs_get_selftest_status(struct device *dev,
				struct device_attribute *attr, char *buf)
{
	int ret = 1;
	struct mz_mag_data *mz_mag = dev_get_drvdata(dev);

	mutex_lock(&mz_mag->indio_dev->mlock);

	if (mz_mag->ops->self_test)
		ret = mz_mag->ops->self_test(mz_mag);

	mutex_unlock(&mz_mag->indio_dev->mlock);

	return sprintf(buf, "%d\n", ret);
}

static ssize_t mz_mag_iio_sysfs_set_selftest_status(struct device *dev,
		struct device_attribute *attr, const char *buf, size_t size)
{
	/* we do nothing when writing to the self_test */
	return size;
}

static IIO_DEV_ATTR_SAMP_FREQ(S_IWUSR | S_IRUGO,
			mz_mag_iio_sysfs_get_sampling_frequency,
			mz_mag_iio_sysfs_set_sampling_frequency);

static IIO_DEV_ATTR_SAMP_FREQ_AVAIL(mz_mag_iio_sysfs_sampling_frequency_avail);
static IIO_DEVICE_ATTR(hw_fifo_lenght, S_IRUGO, mz_mag_iio_sysfs_get_hw_fifo_lenght, NULL, 0);
static IIO_DEVICE_ATTR(flush, S_IWUSR, NULL, mz_mag_iio_sysfs_flush_fifo, 0);
static IIO_DEVICE_ATTR(compass_self_test, S_IWUSR | S_IRUGO,
				mz_mag_iio_sysfs_get_selftest_status,
				mz_mag_iio_sysfs_set_selftest_status, 0);

static struct attribute *mz_mag_iio_attributes[] = {
	&iio_dev_attr_sampling_frequency_available.dev_attr.attr,
	&iio_dev_attr_sampling_frequency.dev_attr.attr,
	&iio_dev_attr_hw_fifo_lenght.dev_attr.attr,
	&iio_dev_attr_compass_self_test.dev_attr.attr,
	&iio_dev_attr_flush.dev_attr.attr,
	NULL,
};

static const struct attribute_group mz_mag_iio_attribute_group = {
	.attrs = mz_mag_iio_attributes,
};


static const struct iio_info mz_mag_iio_info = {
	.driver_module = THIS_MODULE,
	.attrs = &mz_mag_iio_attribute_group,
	.read_raw = &mz_mag_iio_read_raw,
};


static int mz_sensors_mag_get_name(struct device *dev, char **name)
{
	struct mz_mag_data *mz_mag = dev_get_drvdata(dev);

	*name = mz_mag->device_name;

	dev_info(dev, "ltr559_als_get_name succeeded\n");
	return 0;
}

struct meizu_sensors_ops meizu_mag_ops = {
	.get_name = &mz_sensors_mag_get_name,
};

static int mz_mag_iio_init(struct mz_mag_data *mz_mag)
{
	int ret = -1;

	mz_mag->iio_buffer_data = kmalloc(32, GFP_KERNEL);
	if (mz_mag->iio_buffer_data == NULL) {
		dev_err(&mz_mag->client->dev, "failed to call kmalloc\n");
		return -ENOMEM;
	}

	INIT_DELAYED_WORK(&mz_mag->iio_work, mz_mag_iio_func);

	mz_mag->iio_workq = create_singlethread_workqueue("mz_mag_iio");
	if (!mz_mag->iio_workq) {
		dev_err(&mz_mag->client->dev,
			"failed to call create_singlethread_workqueue\n");
		goto free_iio_buffer_data;
	}

	mz_mag->indio_dev = iio_device_alloc(0);
	if (!mz_mag->indio_dev) {
		dev_err(&mz_mag->client->dev, "failed to call iio_device_alloc\n");
		goto wq_destory;
	}

	mz_mag->indio_dev->name = kasprintf(GFP_KERNEL, "%s_%s", "lsm6ds3", "magn");
	mz_mag->indio_dev->info = &mz_mag_iio_info;
	mz_mag->indio_dev->channels = (struct iio_chan_spec const	*)&mz_mag_iio_ch;
	mz_mag->indio_dev->num_channels = ARRAY_SIZE(mz_mag_iio_ch);
	mz_mag->indio_dev->modes = INDIO_DIRECT_MODE;
	iio_device_set_drvdata(mz_mag->indio_dev, mz_mag);

	ret = iio_triggered_buffer_setup(mz_mag->indio_dev,
				&mz_mag_iio_handler_empty, NULL,
				&mz_mag_iio_buffer_setup_ops);
	if (ret < 0) {
		dev_err(&mz_mag->client->dev, "failed to setup triggered buffer\n");
		goto iio_device_free;
	}

	mz_mag->trig = iio_trigger_alloc("%s-trigger", "magn");
	if (!mz_mag->trig) {
		dev_err(&mz_mag->client->dev, "failed to call iio_trigger_alloc\n");
		goto iio_deallocate_buffer;
	}

	mz_mag->trig->ops = &mz_mag_trigger_ops;
	mz_mag->trig->dev.parent = &mz_mag->client->dev;
	iio_trigger_set_drvdata(mz_mag->trig, mz_mag->indio_dev);

	ret = iio_trigger_register(mz_mag->trig);
	if (ret < 0) {
		dev_err(&mz_mag->client->dev, "failed to register iio trigger.\n");
		goto iio_deallocate_trigger;
	}
	mz_mag->indio_dev->trig = mz_mag->trig;

	ret = iio_device_register(mz_mag->indio_dev);
	if (ret < 0) {
		dev_err(&mz_mag->client->dev, "failed to register iio device.\n");
		goto iio_deallocate_trigger;
	}
#if 0
	ret = meizu_sysfslink_register_name(&mz_mag->indio_dev->dev, "magnetometer");
	if (ret < 0) {
		dev_err(&mz_mag->client->dev, "failed to meizu_sysfslink_register_name.\n");
		goto iio_deallocate_trigger;
	}
#endif
	ret = meizu_sensor_register(MEIZU_SENSOR_ID_COMPASS,
		&mz_mag->indio_dev->dev, &meizu_mag_ops);
	if (ret < 0) {
		dev_err(&mz_mag->client->dev, "failed to meizu_sensor_register.\n");
		goto iio_deallocate_trigger;
	}
	return 0;


iio_deallocate_trigger:
	iio_trigger_free(mz_mag->trig);
iio_deallocate_buffer:
	iio_triggered_buffer_cleanup(mz_mag->indio_dev);
iio_device_free:
	iio_device_free(mz_mag->indio_dev);
wq_destory:
	destroy_workqueue(mz_mag->iio_workq);
free_iio_buffer_data:
	kfree(mz_mag->iio_buffer_data);
	return ret;
}

static int mz_mag_setup(struct mz_mag_data *mz_mag)
{
	int ret;

	dev_dbg(&mz_mag->client->dev, "setup magnetometer sensor, mz_mag: %llu!!\n", (uint64_t)mz_mag);

	ret = akm09911_hw_init(mz_mag);
	if (ret == 0)
		return 0;

	ret = st480_hw_init(mz_mag);
	if (ret == 0)
		return 0;

	ret = af8133_hw_init(mz_mag);
	if (ret == 0)
		return 0;

	dev_err(&mz_mag->client->dev, "Failed to setup magnetometer sensor\n");

	return ret;
}


int mz_mag_probe(struct i2c_client *client, const struct i2c_device_id *id)
{
	int ret = 0;
	struct mz_mag_data *mz_mag;

	pr_info("%s mz_mag_probe\n", MZ_MAG_DEV_NAME);

	mz_mag = kzalloc(sizeof(struct mz_mag_data), GFP_KERNEL);
	if (!mz_mag) {
		pr_err("%s mz_mag_probe: memory allocation failed.\n", MZ_MAG_DEV_NAME);
		ret = -ENOMEM;
		goto err0;
	}

	mz_mag->client       = client;
	mz_mag->range        = MZ_MAG_DEFAULT_RANGE;
	mz_mag->pollrate     = MZ_MAG_DEFAULT_POLLRATE;
	mz_mag->min_pollrate = MZ_MAG_DEFAULT_MIN_POLLRATE;
	atomic_set(&mz_mag->enabled, 0);
	mutex_init(&mz_mag->lock);

	i2c_set_clientdata(client, mz_mag);

	ret = mz_mag_setup(mz_mag);
	if(ret < 0)
		goto err1;
#if 0
	ret = mz_mag_input_init(mz_mag);
	if (ret < 0)
		goto err2;

	ret = create_sysfs_interfaces(&client->dev);
	if (ret < 0)
		goto err3;
#endif
	ret = mz_mag_iio_init(mz_mag);
	if (ret < 0)
		goto err3;

	dev_dbg(&mz_mag->client->dev, "mz mag probe done\n");

	return 0;

err3:
err2:
err1:
	kfree(mz_mag);
err0:
	pr_err("%s mz_mag_probe failed.\n", MZ_MAG_DEV_NAME);
	return ret;

}

static const struct i2c_device_id mz_mag_id[] = {
	{MZ_MAG_DEV_NAME, 0 },
	{ }
};

static struct i2c_driver mz_mag_driver = {
	.probe     = mz_mag_probe,
	.id_table  = mz_mag_id,
	.driver = {
		.name  = MZ_MAG_DEV_NAME,
		.owner = THIS_MODULE,
	},
};

static struct i2c_board_info mz_mag_i2c[] = {
	{
		//I2C_BOARD_INFO(MZ_MAG_DEV_NAME, 0x1e), /* af8133 */
		I2C_BOARD_INFO(MZ_MAG_DEV_NAME, 0x0c), /* st480 & akm09911 */
	},
};

static int __init mz_mag_init(void)
{
	int ret;

	pr_info("%s mz_mag_init\n", MZ_MAG_DEV_NAME);
#if 1
	ret = i2c_register_board_info(2, mz_mag_i2c, 1);
	if (ret < 0) {
		pr_err("%s i2c_register_board_info failed\n", MZ_MAG_DEV_NAME);
	}

	pr_info("%s i2c_register_board_info success\n", MZ_MAG_DEV_NAME);
#endif
	return 0;
}

static void __exit mz_mag_exit(void)
{
	i2c_del_driver(&mz_mag_driver);
}

//module_init(mz_mag_init);
//module_exit(mz_mag_exit);

postcore_initcall(mz_mag_init);
module_i2c_driver(mz_mag_driver);

MODULE_AUTHOR("ZhangJiajing <zhangjiajing@meizu.com>");
MODULE_DESCRIPTION("Meizu magnetometer linux driver");
MODULE_LICENSE("GPL");
MODULE_VERSION("1.0.1");