mt6605.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330
  1. /***************************************************************************
  2. * Filename:
  3. * ---------
  4. * mt6605.c
  5. *
  6. * Project:
  7. * --------
  8. *
  9. * Description:
  10. * ------------
  11. *
  12. * Author:
  13. * -------
  14. * LiangChi Huang, ext 25609, LiangChi.Huang@mediatek.com, 2012-08-09
  15. *
  16. *****************************************************************************/
  17. #define pr_fmt(fmt) "["KBUILD_MODNAME"]" fmt
  18. /*****************************************************************************
  19. * Include
  20. *****************************************************************************/
  21. #include <linux/kernel.h>
  22. #include <linux/module.h>
  23. #include <linux/fs.h>
  24. #include <linux/slab.h>
  25. #include <linux/init.h>
  26. #include <linux/list.h>
  27. #include <linux/i2c.h>
  28. #include <linux/irq.h>
  29. #include <linux/jiffies.h>
  30. #include <linux/uaccess.h>
  31. #include <linux/delay.h>
  32. #include <linux/interrupt.h>
  33. #include <linux/io.h>
  34. #include <linux/platform_device.h>
  35. #include <linux/gpio.h>
  36. #include <linux/miscdevice.h>
  37. #include <linux/spinlock.h>
  38. #include <linux/dma-mapping.h>
  39. #include <linux/interrupt.h>
  40. #ifdef CONFIG_OF
  41. #include <linux/of.h>
  42. #include <linux/of_irq.h>
  43. #include <linux/of_address.h>
  44. #endif
  45. /* #include <mach/eint.h> */
  46. /* #include <mach/mt6605.h> */
  47. #ifndef CONFIG_MTK_FPGA
  48. #include <mt_clkbuf_ctl.h> /* for clock buffer */
  49. #endif
  50. /* #include <cust_eint.h> */
  51. /* #include <cust_i2c.h> */
  52. /*****************************************************************************
  53. * Define
  54. *****************************************************************************/
  55. /* #define NFC_I2C_BUSNUM I2C_NFC_CHANNEL */
  56. #define I2C_ID_NAME "mt6605"
  57. #define MAX_BUFFER_SIZE 255
  58. #define NFC_CLIENT_TIMING 400 /* I2C speed */
  59. enum {
  60. MTK_NFC_GPIO_EN_B = 0x0,
  61. MTK_NFC_GPIO_SYSRST_B,
  62. MTK_NFC_GPIO_EINT,
  63. MTK_NFC_GPIO_IRQ,
  64. MTK_NFC_GPIO_IOCTL,
  65. MTK_NFC_GPIO_MAX_NUM
  66. };
  67. enum {
  68. MTK_NFC_IOCTL_READ = 0x0,
  69. MTK_NFC_IOCTL_WRITE,
  70. MTK_NFC_IOCTL_MAX_NUM
  71. };
  72. enum {
  73. MTK_NFC_IOCTL_CMD_CLOCK_BUF_ENABLE = 0x0,
  74. MTK_NFC_IOCTL_CMD_CLOCK_BUF_DISABLE,
  75. MTK_NFC_IOCTL_CMD_EXIT_EINT,
  76. MTK_NFC_IOCTL_CMD_GET_CHIP_ID,
  77. MTK_NFC_IOCTL_CMD_READ_DATA,
  78. MTK_NFC_IOCTL_CMD_MAX_NUM
  79. };
  80. enum {
  81. MTK_NFC_PULL_LOW = 0x0,
  82. MTK_NFC_PULL_HIGH,
  83. MTK_NFC_PULL_INVALID,
  84. };
  85. enum {
  86. MTK_NFC_GPIO_DIR_IN = 0x0,
  87. MTK_NFC_GPIO_DIR_OUT,
  88. MTK_NFC_GPIO_DIR_INVALID,
  89. };
  90. /*****************************************************************************
  91. * Global Variable
  92. *****************************************************************************/
  93. struct mt6605_dev *mt6605_dev_ptr = NULL;
  94. struct mt6605_dev _gmt6605_dev;
  95. #if !defined(CONFIG_MTK_LEGACY)
  96. struct platform_device *nfc_plt_dev = NULL;
  97. struct pinctrl *gpctrl = NULL;
  98. struct pinctrl_state *st_ven_h = NULL;
  99. struct pinctrl_state *st_ven_l = NULL;
  100. struct pinctrl_state *st_rst_h = NULL;
  101. struct pinctrl_state *st_rst_l = NULL;
  102. struct pinctrl_state *st_eint_h = NULL;
  103. struct pinctrl_state *st_eint_l = NULL;
  104. struct pinctrl_state *st_irq_init = NULL;
  105. #endif
  106. /* static struct i2c_board_info nfc_board_info __initdata = */
  107. /* { I2C_BOARD_INFO(I2C_ID_NAME, I2C_NFC_SLAVE_7_BIT_ADDR) }; */
  108. /* For DMA */
  109. #ifdef CONFIG_MTK_I2C_EXTENSION
  110. static char *I2CDMAWriteBuf; /*= NULL;*//* unnecessary initialise */
  111. static unsigned int I2CDMAWriteBuf_pa; /* = NULL; */
  112. static char *I2CDMAReadBuf; /*= NULL;*//* unnecessary initialise */
  113. static unsigned int I2CDMAReadBuf_pa; /* = NULL; */
  114. #else
  115. static char I2CDMAWriteBuf[255];
  116. static char I2CDMAReadBuf[255];
  117. #endif
  118. static int fgNfcChip; /*= 0;*//* unnecessary initialise */
  119. int forceExitBlockingRead = 0;
  120. /* NFC IRQ */
  121. static u32 nfc_irq;
  122. static int nfc_irq_count; /*= 0;*//* unnecessary initialise */
  123. /*****************************************************************************
  124. * Function Prototype
  125. *****************************************************************************/
  126. static int mt6605_probe(struct i2c_client *client,
  127. const struct i2c_device_id *id);
  128. static int mt6605_remove(struct i2c_client *client);
  129. /* static int mt6605_detect(struct i2c_client *client, */
  130. /* int kind, struct i2c_board_info *info); */
  131. static int mt6605_dev_open(struct inode *inode, struct file *filp);
  132. static long mt6605_dev_unlocked_ioctl(struct file *filp, unsigned int cmd,
  133. unsigned long arg);
  134. static ssize_t mt6605_dev_read(struct file *filp, char __user *buf,
  135. size_t count, loff_t *offset);
  136. static ssize_t mt6605_dev_write(struct file *filp, const char __user *buf,
  137. size_t count, loff_t *offset);
  138. static int mt_nfc_probe(struct platform_device *pdev);
  139. static int mt_nfc_remove(struct platform_device *pdev);
  140. /* void mt6605_dev_irq_handler(void); */
  141. static irqreturn_t mt6605_dev_irq_handler(int irq, void *data); /*IRQ handler */
  142. /* static void mt6605_disable_irq(struct mt6605_dev *mt6605_dev); */
  143. #if !defined(CONFIG_MTK_LEGACY)
  144. static int mt_nfc_pinctrl_init(struct platform_device *pdev);
  145. static int mt_nfc_pinctrl_select(struct pinctrl *p, struct pinctrl_state *s);
  146. static int mt_nfc_gpio_init(void);
  147. #endif
  148. static int mt_nfc_get_gpio_value(int gpio_num);
  149. static int mt_nfc_get_gpio_dir(int gpio_num);
  150. /*****************************************************************************
  151. * Data Structure
  152. *****************************************************************************/
  153. struct mt6605_dev {
  154. wait_queue_head_t read_wq;
  155. struct mutex read_mutex;
  156. struct i2c_client *client;
  157. struct miscdevice mt6605_device;
  158. unsigned int ven_gpio;
  159. unsigned int sysrstb_gpio;
  160. unsigned int irq_gpio; /* Chip inform Host */
  161. unsigned int eint_gpio; /* Host inform Chip */
  162. bool irq_enabled;
  163. struct mutex irq_enabled_lock;
  164. /* spinlock_t irq_enabled_lock; */
  165. };
  166. struct mt6605_i2c_platform_data {
  167. unsigned int irq_gpio; /* Chip inform Host */
  168. unsigned int ven_gpio;
  169. unsigned int sysrstb_gpio;
  170. unsigned int eint_gpio; /* Host inform Chip */
  171. };
  172. static const struct i2c_device_id mt6605_id[] = {
  173. {I2C_ID_NAME, 0},
  174. {}
  175. };
  176. #ifdef CONFIG_OF
  177. static const struct of_device_id nfc_switch_of_match[] = {
  178. {.compatible = "mediatek,nfc"},
  179. {},
  180. };
  181. #endif
  182. /* #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,36)) */
  183. /* static struct i2c_client_address_data addr_data = { .forces = forces,}; */
  184. /* #endif */
  185. static struct i2c_driver mt6605_dev_driver = {
  186. .id_table = mt6605_id,
  187. .probe = mt6605_probe,
  188. .remove = mt6605_remove,
  189. /* .detect = mt6605_detect, */
  190. .driver = {
  191. .name = "mt6605",
  192. .owner = THIS_MODULE,
  193. #ifdef CONFIG_OF
  194. .of_match_table = nfc_switch_of_match,
  195. #endif
  196. },
  197. };
  198. /* platform driver */
  199. static const struct of_device_id nfc_dev_of_match[] = {
  200. {.compatible = "mediatek,nfc-gpio-v2",},
  201. {},
  202. };
  203. static struct platform_driver mtk_nfc_platform_driver = {
  204. .probe = mt_nfc_probe,
  205. .remove = mt_nfc_remove,
  206. .driver = {
  207. .name = I2C_ID_NAME,
  208. .owner = THIS_MODULE,
  209. #ifdef CONFIG_OF
  210. .of_match_table = nfc_dev_of_match,
  211. #endif
  212. },
  213. };
  214. #if !defined(CONFIG_MTK_LEGACY)
  215. struct mt6605_i2c_platform_data mt6605_platform_data;
  216. #else
  217. static struct mt6605_i2c_platform_data mt6605_platform_data = {
  218. .irq_gpio = GPIO_IRQ_NFC_PIN,
  219. .ven_gpio = GPIO_NFC_VENB_PIN,
  220. .sysrstb_gpio = GPIO_NFC_RST_PIN,
  221. .eint_gpio = GPIO_NFC_EINT_PIN,
  222. };
  223. #endif
  224. static const struct file_operations mt6605_dev_fops = {
  225. .owner = THIS_MODULE,
  226. .llseek = no_llseek,
  227. .read = mt6605_dev_read,
  228. .write = mt6605_dev_write,
  229. .open = mt6605_dev_open,
  230. #ifdef CONFIG_COMPAT
  231. .compat_ioctl = mt6605_dev_unlocked_ioctl,
  232. #endif
  233. .unlocked_ioctl = mt6605_dev_unlocked_ioctl,
  234. };
  235. /*****************************************************************************
  236. * Extern Area
  237. *****************************************************************************/
  238. /*****************************************************************************
  239. * Function
  240. *****************************************************************************/
  241. static void mt6605_enable_irq(u32 irq_line)
  242. {
  243. /* pr_debug("%s : irq_line=%d, nfc_irq_count=%d.\n", __func__,
  244. irq_line, nfc_irq_count); */
  245. nfc_irq_count++; /* It must set before call enable_irq */
  246. enable_irq(irq_line);
  247. /* pr_debug("%s : nfc_irq_count = %d.\n", __func__, nfc_irq_count); */
  248. }
  249. static void mt6605_disable_irq(u32 irq_line)
  250. {
  251. /* pr_debug("%s : irq_line=%d, nfc_irq_count=%d.\n", __func__,
  252. irq_line, nfc_irq_count); */
  253. if (nfc_irq_count >= 1) {
  254. disable_irq_nosync(irq_line);
  255. nfc_irq_count--;
  256. } else {
  257. pr_debug("%s : disable irq fail.\n", __func__);
  258. }
  259. /* pr_debug("%s : nfc_irq_count = %d.\n", __func__, nfc_irq_count); */
  260. }
  261. static int mt6605_probe(struct i2c_client *client,
  262. const struct i2c_device_id *id)
  263. {
  264. int ret = 0;
  265. struct mt6605_i2c_platform_data *platform_data;
  266. struct device_node *node;
  267. pr_debug("mt6605_dev_probe\n");
  268. platform_data = &mt6605_platform_data;
  269. if (platform_data == NULL) {
  270. pr_err("%s : nfc probe fail\n", __func__);
  271. return -ENODEV;
  272. }
  273. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  274. pr_err("%s : need I2C_FUNC_I2C\n", __func__);
  275. return -ENODEV;
  276. }
  277. #if defined(CONFIG_MTK_LEGACY)
  278. mt_set_gpio_mode(platform_data->irq_gpio, GPIO_IRQ_NFC_PIN_M_EINT);
  279. mt_set_gpio_dir(platform_data->irq_gpio, GPIO_DIR_IN);
  280. mt_set_gpio_pull_enable(platform_data->irq_gpio, GPIO_PULL_ENABLE);
  281. mt_set_gpio_pull_select(platform_data->irq_gpio, GPIO_PULL_DOWN);
  282. /* ven_gpio */
  283. mt_set_gpio_mode(platform_data->ven_gpio, GPIO_NFC_VENB_PIN_M_GPIO);
  284. mt_set_gpio_dir(platform_data->ven_gpio, GPIO_DIR_OUT);
  285. mt_set_gpio_out(platform_data->ven_gpio, GPIO_OUT_ONE);
  286. usleep_range(900, 1000); /* debug */
  287. /* firm_gpio */
  288. mt_set_gpio_mode(platform_data->sysrstb_gpio, GPIO_NFC_RST_PIN_M_GPIO);
  289. mt_set_gpio_dir(platform_data->sysrstb_gpio, GPIO_DIR_OUT);
  290. mt_set_gpio_out(platform_data->sysrstb_gpio, GPIO_OUT_ONE);
  291. usleep_range(900, 1000); /* debug */
  292. /* EINT_gpio */
  293. mt_set_gpio_mode(platform_data->eint_gpio, GPIO_NFC_EINT_PIN_M_GPIO);
  294. mt_set_gpio_dir(platform_data->eint_gpio, GPIO_DIR_OUT);
  295. /* Set output High */
  296. mt_set_gpio_out(platform_data->eint_gpio, GPIO_OUT_ZERO);
  297. _gmt6605_dev.irq_gpio = platform_data->irq_gpio;
  298. _gmt6605_dev.ven_gpio = platform_data->ven_gpio;
  299. _gmt6605_dev.sysrstb_gpio = platform_data->sysrstb_gpio;
  300. _gmt6605_dev.eint_gpio = platform_data->eint_gpio;
  301. #endif
  302. _gmt6605_dev.client = client;
  303. /* init mutex and queues */
  304. init_waitqueue_head(&_gmt6605_dev.read_wq);
  305. mutex_init(&_gmt6605_dev.read_mutex);
  306. /* spin_lock_init(&mt6605_dev->irq_enabled_lock); */
  307. mutex_init(&_gmt6605_dev.irq_enabled_lock);
  308. #if 0
  309. _gmt6605_dev.mt6605_device.minor = MISC_DYNAMIC_MINOR;
  310. _gmt6605_dev.mt6605_device.name = "mt6605";
  311. _gmt6605_dev.mt6605_device.fops = &mt6605_dev_fops;
  312. ret = misc_register(&_gmt6605_dev.mt6605_device);
  313. if (ret) {
  314. pr_err("%s : misc_register failed\n", __FILE__);
  315. goto err_misc_register;
  316. }
  317. #endif
  318. #ifdef CONFIG_MTK_I2C_EXTENSION
  319. #ifdef CONFIG_64BIT
  320. I2CDMAWriteBuf =
  321. (char *)dma_alloc_coherent(&client->dev, MAX_BUFFER_SIZE,
  322. (dma_addr_t *) &I2CDMAWriteBuf_pa,
  323. GFP_KERNEL);
  324. #else
  325. I2CDMAWriteBuf =
  326. (char *)dma_alloc_coherent(NULL, MAX_BUFFER_SIZE,
  327. (dma_addr_t *) &I2CDMAWriteBuf_pa,
  328. GFP_KERNEL);
  329. #endif
  330. if (I2CDMAWriteBuf == NULL) {
  331. pr_err("%s : failed to allocate dma buffer\n", __func__);
  332. mutex_destroy(&_gmt6605_dev.read_mutex);
  333. gpio_free(platform_data->sysrstb_gpio);
  334. return ret;
  335. }
  336. #ifdef CONFIG_64BIT
  337. I2CDMAReadBuf =
  338. (char *)dma_alloc_coherent(&client->dev, MAX_BUFFER_SIZE,
  339. (dma_addr_t *) &I2CDMAReadBuf_pa,
  340. GFP_KERNEL);
  341. #else
  342. I2CDMAReadBuf =
  343. (char *)dma_alloc_coherent(NULL, MAX_BUFFER_SIZE,
  344. (dma_addr_t *) &I2CDMAReadBuf_pa,
  345. GFP_KERNEL);
  346. #endif
  347. if (I2CDMAReadBuf == NULL) {
  348. pr_err("%s : failed to allocate dma buffer\n", __func__);
  349. mutex_destroy(&_gmt6605_dev.read_mutex);
  350. gpio_free(platform_data->sysrstb_gpio);
  351. return ret;
  352. }
  353. pr_debug("%s :I2CDMAWriteBuf_pa %d, I2CDMAReadBuf_pa,%d\n", __func__,
  354. I2CDMAWriteBuf_pa, I2CDMAReadBuf_pa);
  355. #else
  356. memset(I2CDMAWriteBuf, 0x00, sizeof(I2CDMAWriteBuf));
  357. memset(I2CDMAReadBuf, 0x00, sizeof(I2CDMAReadBuf));
  358. #endif
  359. /* request irq. the irq is set whenever the chip has data available
  360. * for reading. it is cleared when all data has been read.
  361. */
  362. /* client->irq = CUST_EINT_IRQ_NFC_NUM; */
  363. /* pr_debug("%s : requesting IRQ %d\n", __func__, client->irq); */
  364. /* pr_debug("mt6605_Prob2\n"); */
  365. /* mt_eint_set_hw_debounce(CUST_EINT_IRQ_NFC_NUM, */
  366. /* CUST_EINT_IRQ_NFC_DEBOUNCE_CN); */
  367. /* mt_eint_registration(CUST_EINT_IRQ_NFC_NUM, */
  368. /* CUST_EINT_IRQ_NFC_TYPE, mt6605_dev_irq_handler, */
  369. /* 0); */
  370. /* NFC IRQ settings */
  371. node = of_find_compatible_node(NULL, NULL, "mediatek,nfc-gpio-v2");
  372. if (node) {
  373. nfc_irq = irq_of_parse_and_map(node, 0);
  374. client->irq = nfc_irq;
  375. ret =
  376. request_irq(nfc_irq, mt6605_dev_irq_handler,
  377. IRQF_TRIGGER_NONE, "IRQ_NFC-eint", NULL);
  378. if (ret) {
  379. pr_err("%s : EINT IRQ LINE NOT AVAILABLE\n", __func__);
  380. } else {
  381. pr_debug("%s : set EINT finished, nfc_irq=%d", __func__,
  382. nfc_irq);
  383. nfc_irq_count++;
  384. mt6605_disable_irq(nfc_irq);
  385. }
  386. } else {
  387. pr_err("%s : can not find NFC eint compatible node\n",
  388. __func__);
  389. }
  390. /* mt_eint_unmask(CUST_EINT_IRQ_NFC_NUM); */
  391. /* mt6605_disable_irq(mt6605_dev); */
  392. /* mt_eint_mask(CUST_EINT_IRQ_NFC_NUM); */
  393. i2c_set_clientdata(client, &_gmt6605_dev);
  394. forceExitBlockingRead = 0;
  395. return 0;
  396. }
  397. static int mt6605_remove(struct i2c_client *client)
  398. {
  399. /* struct mt6605_dev *mt6605_dev; */
  400. pr_debug("mt6605_remove\n");
  401. #ifdef CONFIG_MTK_I2C_EXTENSION
  402. if (I2CDMAWriteBuf) {
  403. #ifdef CONFIG_64BIT
  404. dma_free_coherent(&client->dev, MAX_BUFFER_SIZE, I2CDMAWriteBuf,
  405. I2CDMAWriteBuf_pa);
  406. #else
  407. dma_free_coherent(NULL, MAX_BUFFER_SIZE, I2CDMAWriteBuf,
  408. I2CDMAWriteBuf_pa);
  409. #endif
  410. I2CDMAWriteBuf = NULL;
  411. I2CDMAWriteBuf_pa = 0;
  412. }
  413. if (I2CDMAReadBuf) {
  414. #ifdef CONFIG_64BIT
  415. dma_free_coherent(&client->dev, MAX_BUFFER_SIZE, I2CDMAReadBuf,
  416. I2CDMAReadBuf_pa);
  417. #else
  418. dma_free_coherent(NULL, MAX_BUFFER_SIZE, I2CDMAReadBuf,
  419. I2CDMAReadBuf_pa);
  420. #endif
  421. I2CDMAReadBuf = NULL;
  422. I2CDMAReadBuf_pa = 0;
  423. }
  424. #endif
  425. /* mt6605_dev = i2c_get_clientdata(client); */
  426. /* free_irq(client->irq, &_gmt6605_dev); */
  427. free_irq(nfc_irq, &_gmt6605_dev);
  428. misc_deregister(&_gmt6605_dev.mt6605_device);
  429. mutex_destroy(&_gmt6605_dev.read_mutex);
  430. #if defined(CONFIG_MTK_LEGACY)
  431. gpio_free(_gmt6605_dev.irq_gpio);
  432. gpio_free(_gmt6605_dev.ven_gpio);
  433. gpio_free(_gmt6605_dev.sysrstb_gpio);
  434. gpio_free(_gmt6605_dev.eint_gpio);
  435. #endif
  436. /* kfree(mt6605_dev); */
  437. return 0;
  438. }
  439. static int mt_nfc_probe(struct platform_device *pdev)
  440. {
  441. int ret = 0;
  442. #if !defined(CONFIG_MTK_LEGACY)
  443. nfc_plt_dev = pdev;
  444. pr_debug("%s : &nfc_plt_dev=%p\n", __func__, nfc_plt_dev);
  445. /* pinctrl init */
  446. ret = mt_nfc_pinctrl_init(pdev);
  447. /* gpio init */
  448. if (mt_nfc_gpio_init() != 0)
  449. pr_debug("%s : mt_nfc_gpio_init err.\n", __func__);
  450. #endif
  451. return 0;
  452. }
  453. static int mt_nfc_remove(struct platform_device *pdev)
  454. {
  455. pr_debug("%s : &pdev=%p\n", __func__, pdev);
  456. return 0;
  457. }
  458. /* void mt6605_dev_irq_handler(void) */
  459. irqreturn_t mt6605_dev_irq_handler(int irq, void *data)
  460. {
  461. struct mt6605_dev *mt6605_dev = mt6605_dev_ptr;
  462. /* 2015/08/25 reduce print_log
  463. pr_debug("%s : &mt6605_dev=%p\n", __func__, mt6605_dev);
  464. */
  465. if (NULL == mt6605_dev) {
  466. pr_debug("mt6605_dev NULL.\n");
  467. return IRQ_HANDLED;
  468. }
  469. /* mt6605_disable_irq(mt6605_dev); */
  470. mt6605_disable_irq(nfc_irq);
  471. wake_up(&mt6605_dev->read_wq);
  472. /* wake_up_interruptible(&mt6605_dev->read_wq); */
  473. /* pr_debug("%s : wake_up &read_wq=%p\n", __func__, &mt6605_dev->read_wq); */
  474. return IRQ_HANDLED;
  475. }
  476. static ssize_t mt6605_dev_read(struct file *filp, char __user *buf,
  477. size_t count, loff_t *offset)
  478. {
  479. struct mt6605_dev *mt6605_dev = filp->private_data;
  480. int ret = 0;
  481. int read_retry = 5;
  482. if (count > MAX_BUFFER_SIZE)
  483. count = MAX_BUFFER_SIZE;
  484. if (!mt_nfc_get_gpio_value(mt6605_dev->irq_gpio)) {
  485. if (filp->f_flags & O_NONBLOCK) {
  486. ret = -EAGAIN;
  487. pr_debug("%s : goto fail.\n", __func__);
  488. goto fail;
  489. }
  490. /* mutex_lock(&mt6605_dev->irq_enabled_lock); */
  491. /* mt6605_dev->irq_enabled = true; */
  492. /* mutex_unlock(&mt6605_dev->irq_enabled_lock); */
  493. mutex_lock(&mt6605_dev->read_mutex);
  494. if (forceExitBlockingRead == 1) {
  495. pr_debug("%s :forceExitBlockingRead.\n", __func__);
  496. forceExitBlockingRead = 0; /* clear flag */
  497. mutex_unlock(&mt6605_dev->read_mutex);
  498. goto fail;
  499. }
  500. mutex_unlock(&mt6605_dev->read_mutex);
  501. /* mt_eint_unmask(mt6605_dev->client->irq); */
  502. /* pr_debug("%s : mt_eint_unmask %d, IRQ, %d\n", */
  503. /* __func__, */
  504. /* mt6605_dev->client->irq, */
  505. /* mt_get_gpio_in(mt6605_dev->irq_gpio)); */
  506. mt6605_enable_irq(nfc_irq);
  507. /* 2015/06/11 reduce print
  508. pr_debug("%s : enable_irq %d, irq status=%d\n",
  509. __func__,
  510. nfc_irq, mt_nfc_get_gpio_value(mt6605_dev->irq_gpio));
  511. */
  512. ret = wait_event_interruptible(mt6605_dev->read_wq,
  513. (mt_nfc_get_gpio_value
  514. (mt6605_dev->irq_gpio)
  515. || forceExitBlockingRead));
  516. /* 2015/06/11 reduce print
  517. pr_debug("%s : wait_event_interruptible ret=%d,irq status=%d\n",
  518. __func__, ret,
  519. mt_nfc_get_gpio_value(mt6605_dev->irq_gpio));
  520. */
  521. if (ret || forceExitBlockingRead) {
  522. mt6605_disable_irq(nfc_irq);
  523. pr_debug("%s : goto fail\n", __func__);
  524. mutex_lock(&mt6605_dev->read_mutex);
  525. if (forceExitBlockingRead == 1) {
  526. pr_debug
  527. ("%s:clear flag,orceExitBlockingRead\n",
  528. __func__);
  529. forceExitBlockingRead = 0; /* clear flag */
  530. }
  531. mutex_unlock(&mt6605_dev->read_mutex);
  532. goto fail;
  533. }
  534. }
  535. #ifdef CONFIG_MTK_I2C_EXTENSION
  536. mt6605_dev->client->addr = (mt6605_dev->client->addr & I2C_MASK_FLAG);
  537. mt6605_dev->client->ext_flag |= I2C_DMA_FLAG;
  538. /* mt6605_dev->client->ext_flag |= I2C_DIRECTION_FLAG; */
  539. /* mt6605_dev->client->ext_flag |= I2C_A_FILTER_MSG; */
  540. mt6605_dev->client->timing = NFC_CLIENT_TIMING;
  541. /* Read data */
  542. ret =
  543. i2c_master_recv(mt6605_dev->client,
  544. (unsigned char *)(uintptr_t) I2CDMAReadBuf_pa,
  545. count);
  546. #else
  547. while (read_retry) {
  548. ret =
  549. i2c_master_recv(mt6605_dev->client,
  550. (unsigned char *)(uintptr_t) I2CDMAReadBuf,
  551. count);
  552. /* mutex_unlock(&mt6605_dev->read_mutex); */
  553. /*pr_debug("%s : i2c_master_recv returned=%d, irq status=%d\n", __func__,
  554. ret, mt_nfc_get_gpio_value(mt6605_dev->irq_gpio));*/
  555. if (ret < 0) {
  556. pr_debug("%s: i2c_master_recv failed: %d, read_retry: %d\n",
  557. __func__, ret, read_retry);
  558. read_retry--;
  559. usleep_range(900, 1000);
  560. continue;
  561. }
  562. break;
  563. }
  564. #endif
  565. if (ret < 0) {
  566. pr_err("%s: i2c_master_recv failed: %d, read_retry: %d\n",
  567. __func__, ret, read_retry);
  568. return ret;
  569. }
  570. if (ret > count) {
  571. pr_debug("%s: received too many bytes from i2c (%d)\n",
  572. __func__, ret);
  573. return -EIO;
  574. }
  575. if (copy_to_user(buf, I2CDMAReadBuf, ret)) {
  576. pr_debug("%s : failed to copy to user space\n", __func__);
  577. return -EFAULT;
  578. }
  579. /* pr_debug("%s: return,ret,%d\n", __func__, ret); */
  580. return ret;
  581. fail:
  582. /* mutex_unlock(&mt6605_dev->read_mutex); */
  583. pr_debug("%s: return,fail,%d\n", __func__, ret);
  584. return ret;
  585. }
  586. static ssize_t mt6605_dev_write(struct file *filp, const char __user *buf,
  587. size_t count, loff_t *offset)
  588. {
  589. struct mt6605_dev *mt6605_dev;
  590. int ret = 0, ret_tmp = 0, count_ori = 0, count_remain = 0, idx = 0;
  591. mt6605_dev = filp->private_data;
  592. count_ori = count;
  593. count_remain = count_ori;
  594. if (count > MAX_BUFFER_SIZE) {
  595. count = MAX_BUFFER_SIZE;
  596. count_remain -= count;
  597. }
  598. while (1) {
  599. if (copy_from_user
  600. (I2CDMAWriteBuf, &buf[(idx * MAX_BUFFER_SIZE)], count)) {
  601. pr_debug("%s : failed to copy from user space.\n",
  602. __func__);
  603. return -EFAULT;
  604. }
  605. /* 2015/06/11 reduce print_log
  606. pr_debug("%s : writing %zu bytes, remain bytes %d.\n", __func__,
  607. count, count_remain);
  608. */
  609. /* Write data */
  610. #ifdef CONFIG_MTK_I2C_EXTENSION
  611. mt6605_dev->client->addr =
  612. (mt6605_dev->client->addr & I2C_MASK_FLAG);
  613. mt6605_dev->client->ext_flag |= I2C_DMA_FLAG;
  614. /* mt6605_dev->client->ext_flag |= I2C_DIRECTION_FLAG; */
  615. /* mt6605_dev->client->ext_flag |= I2C_A_FILTER_MSG; */
  616. mt6605_dev->client->timing = NFC_CLIENT_TIMING;
  617. ret_tmp =
  618. i2c_master_send(mt6605_dev->client,
  619. (unsigned char *)(uintptr_t)
  620. I2CDMAWriteBuf_pa, count);
  621. #else
  622. ret_tmp =
  623. i2c_master_send(mt6605_dev->client,
  624. (unsigned char *)(uintptr_t)
  625. I2CDMAWriteBuf, count);
  626. #endif
  627. if (ret_tmp != count) {
  628. pr_debug("%s : i2c_master_send returned %d\n", __func__,
  629. ret);
  630. ret = -EIO;
  631. return ret;
  632. }
  633. ret += ret_tmp;
  634. /* pr_debug("%s : ret_tmp=%d,ret=%d,count_ori=%d\n", __func__,
  635. ret_tmp, ret, count_ori); */
  636. if (ret == count_ori) {
  637. /* pr_debug("%s : ret == count_ori\n", __func__); */
  638. break;
  639. } else {
  640. if (count_remain > MAX_BUFFER_SIZE) {
  641. count = MAX_BUFFER_SIZE;
  642. count_remain -= MAX_BUFFER_SIZE;
  643. } else {
  644. count = count_remain;
  645. count_remain = 0;
  646. }
  647. idx++;
  648. }
  649. }
  650. /* 2015/06/11 reduce print_log
  651. pr_debug("%s : writing %d bytes. Status %d\n", __func__, count_ori,
  652. ret);
  653. */
  654. return ret;
  655. }
  656. static int mt6605_dev_open(struct inode *inode, struct file *filp)
  657. {
  658. struct mt6605_dev *mt6605_dev =
  659. container_of(filp->private_data, struct mt6605_dev, mt6605_device);
  660. filp->private_data = mt6605_dev;
  661. mt6605_dev_ptr = mt6605_dev;
  662. pr_debug("mt6605_dev_open,%s : %d,%d, &mt6605_dev_open=%p\n", __func__,
  663. imajor(inode), iminor(inode), mt6605_dev_ptr);
  664. forceExitBlockingRead = 0;
  665. return 0;
  666. }
  667. static long mt6605_dev_unlocked_ioctl(struct file *filp, unsigned int cmd,
  668. unsigned long arg)
  669. {
  670. struct mt6605_dev *mt6605_dev = filp->private_data;
  671. int result = 0;
  672. int gpio_dir, tmp_gpio;
  673. int gpio_num = 0;
  674. pr_debug("mt6605_dev_unlocked_ioctl: cmd=0x%04x,arg=0x%04lx.\n", cmd,
  675. arg);
  676. if ((cmd & 0xFFFF) == 0xFE00) {
  677. mt6605_disable_irq(nfc_irq);
  678. pr_debug("mt6605_dev_unlocked_ioctl for disable_irq.\n");
  679. return 0;
  680. } else if ((cmd & 0xFFFF) == 0xFE01) {
  681. struct device_node *node;
  682. int ret;
  683. #if !defined(CONFIG_MTK_LEGACY)
  684. mt_nfc_pinctrl_select(gpctrl, st_irq_init);
  685. #else
  686. mt_set_gpio_mode(mt6605_dev->irq_gpio, GPIO_IRQ_NFC_PIN_M_EINT);
  687. mt_set_gpio_dir(mt6605_dev->irq_gpio, GPIO_DIR_IN);
  688. mt_set_gpio_pull_enable(mt6605_dev->irq_gpio, GPIO_PULL_ENABLE);
  689. mt_set_gpio_pull_select(mt6605_dev->irq_gpio, GPIO_PULL_DOWN);
  690. #endif
  691. /* mt_eint_set_hw_debounce(CUST_EINT_IRQ_NFC_NUM, */
  692. /* CUST_EINT_IRQ_NFC_DEBOUNCE_CN); */
  693. /* mt_eint_registration(CUST_EINT_IRQ_NFC_NUM, */
  694. /* CUST_EINT_IRQ_NFC_TYPE, */
  695. /* mt6605_dev_irq_handler, 0); */
  696. /* mt_eint_mask(CUST_EINT_IRQ_NFC_NUM); */
  697. /* NFC IRQ settings */
  698. node =
  699. of_find_compatible_node(NULL, NULL, "mediatek,nfc-gpio-v2");
  700. if (node) {
  701. nfc_irq = irq_of_parse_and_map(node, 0);
  702. /* client->irq = nfc_irq; */
  703. ret =
  704. request_irq(nfc_irq, mt6605_dev_irq_handler,
  705. IRQF_TRIGGER_NONE, "IRQ_NFC-eint",
  706. NULL);
  707. if (ret) {
  708. pr_err("%s : EINT IRQ LINE NOT AVAILABLE\n",
  709. __func__);
  710. } else {
  711. pr_debug
  712. ("%s : set EINT finished, nfc_irq=%d.\n",
  713. __func__, nfc_irq);
  714. nfc_irq_count++;
  715. mt6605_disable_irq(nfc_irq);
  716. }
  717. } else {
  718. pr_err("%s : can not find NFC eint compatible node.\n",
  719. __func__);
  720. }
  721. /* 2015/08/25 reduce print_log
  722. pr_debug("mt6605_dev_unlocked_ioctl,Re-registered IRQ.\n");
  723. */
  724. return 0;
  725. } else if ((cmd & 0xFFFF) == 0xFEFF) { /* EXIT EINT */
  726. mutex_lock(&mt6605_dev->read_mutex);
  727. forceExitBlockingRead = 1;
  728. mutex_unlock(&mt6605_dev->read_mutex);
  729. wake_up_interruptible(&mt6605_dev->read_wq);
  730. pr_debug("mt6605_dev_unlocked_ioctl,SW Release IRQ.\n");
  731. return 0;
  732. } else if ((cmd & 0xFFFF) == 0xFEFE) { /* Get ChipID */
  733. return fgNfcChip;
  734. } else if ((cmd & 0xFFFF) == 0xFEFD) {
  735. fgNfcChip = (arg & 0xFFFF);
  736. return 0;
  737. }
  738. tmp_gpio = (((arg & 0xFF00) >> 8) & 0x00FF);
  739. if (tmp_gpio == MTK_NFC_GPIO_EN_B) {
  740. gpio_num = mt6605_dev->ven_gpio;
  741. } else if (tmp_gpio == MTK_NFC_GPIO_SYSRST_B) {
  742. gpio_num = mt6605_dev->sysrstb_gpio;
  743. } else if (tmp_gpio == MTK_NFC_GPIO_EINT) {
  744. gpio_num = mt6605_dev->eint_gpio;
  745. } else if (tmp_gpio == MTK_NFC_GPIO_IRQ) {
  746. gpio_num = mt6605_dev->irq_gpio;
  747. } else if (tmp_gpio == MTK_NFC_GPIO_IOCTL) {
  748. /* IOCTL */
  749. /* int command = (arg & 0x00FF); */
  750. switch (arg & 0x00FF) {
  751. case MTK_NFC_IOCTL_CMD_CLOCK_BUF_ENABLE:
  752. pr_debug("%s, enable clock buffer.\n", __func__);
  753. #ifndef CONFIG_MTK_FPGA
  754. /* enable nfc clock buffer */
  755. clk_buf_ctrl(CLK_BUF_NFC, 1);
  756. #endif
  757. break;
  758. case MTK_NFC_IOCTL_CMD_CLOCK_BUF_DISABLE:
  759. pr_debug("%s, disable clock buffer.\n", __func__);
  760. #ifndef CONFIG_MTK_FPGA
  761. /* disable nfc clock buffer */
  762. clk_buf_ctrl(CLK_BUF_NFC, 0);
  763. #endif
  764. break;
  765. case MTK_NFC_IOCTL_CMD_EXIT_EINT:
  766. pr_debug("mt6605_dev_unlocked_ioctl, EXIT EINT.\n");
  767. mutex_lock(&mt6605_dev->read_mutex);
  768. forceExitBlockingRead = 1;
  769. mutex_unlock(&mt6605_dev->read_mutex);
  770. wake_up_interruptible(&mt6605_dev->read_wq);
  771. pr_debug("mt6605_dev_unlocked_ioctl,SW Release IRQ\n");
  772. break;
  773. case MTK_NFC_IOCTL_CMD_GET_CHIP_ID:
  774. return fgNfcChip;
  775. break;
  776. case MTK_NFC_IOCTL_CMD_READ_DATA:
  777. pr_debug("Call mt6605_dev_irq_handler. irq=%d.\n",
  778. mt_nfc_get_gpio_value(mt6605_dev->irq_gpio));
  779. if (mt_nfc_get_gpio_value(mt6605_dev->irq_gpio))
  780. mt6605_dev_irq_handler(nfc_irq, NULL);
  781. break;
  782. default:
  783. break;
  784. }
  785. return 0;
  786. } else {
  787. result = MTK_NFC_PULL_INVALID;
  788. pr_debug("%s, invalid ioctl.\n", __func__);
  789. return result;
  790. }
  791. if (cmd == MTK_NFC_IOCTL_READ) {
  792. result = mt_nfc_get_gpio_value(gpio_num);
  793. /*
  794. if ((gpio_dir == GPIO_DIR_IN)
  795. || (gpio_dir == GPIO_DIR_OUT)) {
  796. result = mt_get_gpio_in(gpio_num);
  797. } else {
  798. result = MTK_NFC_PULL_INVALID;
  799. } */
  800. pr_debug("%s : get gpio value. %d\n", __func__, result);
  801. /*error handler for eint_registration abnormal case */
  802. if (tmp_gpio == MTK_NFC_GPIO_IRQ && result == 0x01) {
  803. pr_debug
  804. ("%s,irq=igh call mt6605_dev_irq_handler\n",
  805. __func__);
  806. mt6605_dev_irq_handler(nfc_irq, NULL);
  807. }
  808. } else if (cmd == MTK_NFC_IOCTL_WRITE) {
  809. gpio_dir = mt_nfc_get_gpio_dir(gpio_num);
  810. pr_debug("test2 MTK_NFC_IOCTL_WRITE, gpio_dir %d, gpio_num %d, tmp_gpio %d\n",
  811. gpio_dir, gpio_num, tmp_gpio);
  812. if (gpio_dir == MTK_NFC_GPIO_DIR_OUT) {
  813. int gpio_pol = (arg & 0x00FF);
  814. pr_debug("test1 MTK_NFC_GPIO_DIR_OUT\n");
  815. if (gpio_pol == MTK_NFC_PULL_LOW) {
  816. #if !defined(CONFIG_MTK_LEGACY)
  817. switch (tmp_gpio) {
  818. case MTK_NFC_GPIO_EN_B:
  819. result =
  820. mt_nfc_pinctrl_select(gpctrl,
  821. st_ven_l);
  822. break;
  823. case MTK_NFC_GPIO_SYSRST_B:
  824. result =
  825. mt_nfc_pinctrl_select(gpctrl,
  826. st_rst_l);
  827. break;
  828. case MTK_NFC_GPIO_EINT:
  829. result =
  830. mt_nfc_pinctrl_select(gpctrl,
  831. st_eint_l);
  832. break;
  833. case MTK_NFC_GPIO_IRQ:
  834. pr_debug
  835. ("%s : IRQ cannot set.\n",
  836. __func__);
  837. break;
  838. default:
  839. pr_debug("%s : default case.\n",
  840. __func__);
  841. break;
  842. }
  843. #else
  844. result =
  845. mt_set_gpio_out(gpio_num, GPIO_OUT_ZERO);
  846. pr_debug("%s : call mtk legacy.\n", __func__);
  847. #endif
  848. } else if (gpio_pol == MTK_NFC_PULL_HIGH) {
  849. #if !defined(CONFIG_MTK_LEGACY)
  850. switch (tmp_gpio) {
  851. case MTK_NFC_GPIO_EN_B:
  852. result =
  853. mt_nfc_pinctrl_select(gpctrl,
  854. st_ven_h);
  855. break;
  856. case MTK_NFC_GPIO_SYSRST_B:
  857. result =
  858. mt_nfc_pinctrl_select(gpctrl,
  859. st_rst_h);
  860. break;
  861. case MTK_NFC_GPIO_EINT:
  862. result =
  863. mt_nfc_pinctrl_select(gpctrl,
  864. st_eint_h);
  865. break;
  866. case MTK_NFC_GPIO_IRQ:
  867. pr_debug
  868. ("%s : IRQ cannot pull.\n",
  869. __func__);
  870. break;
  871. default:
  872. pr_debug("%s : default case.\n",
  873. __func__);
  874. break;
  875. }
  876. #else
  877. result =
  878. mt_set_gpio_out(gpio_num, GPIO_OUT_ONE);
  879. pr_debug("%s : call mtk legacy.\n", __func__);
  880. #endif
  881. }
  882. } else {
  883. result = MTK_NFC_PULL_INVALID;
  884. pr_debug("test3\n");
  885. }
  886. } else {
  887. result = MTK_NFC_PULL_INVALID;
  888. pr_debug("test4\n");
  889. }
  890. pr_debug("mt6605_dev_unlocked_ioctl : result=%d\n", result);
  891. return result;
  892. }
  893. #if !defined(CONFIG_MTK_LEGACY)
  894. static int mt_nfc_pinctrl_select(struct pinctrl *p, struct pinctrl_state *s)
  895. {
  896. int ret = 0;
  897. if (p != NULL && s != NULL) {
  898. ret = pinctrl_select_state(p, s);
  899. } else {
  900. /* pr_debug("%s : pinctrl_select err\n", __func__); */
  901. ret = -1;
  902. }
  903. return ret;
  904. }
  905. static int mt_nfc_pinctrl_init(struct platform_device *pdev)
  906. {
  907. int ret = 0;
  908. gpctrl = devm_pinctrl_get(&pdev->dev);
  909. if (IS_ERR(gpctrl)) {
  910. dev_err(&pdev->dev, "Cannot find pinctrl!");
  911. ret = PTR_ERR(gpctrl);
  912. goto end;
  913. }
  914. st_ven_h = pinctrl_lookup_state(gpctrl, "ven_high");
  915. if (IS_ERR(st_ven_h)) {
  916. ret = PTR_ERR(st_ven_h);
  917. pr_debug("%s : pinctrl err, ven_high\n", __func__);
  918. goto end;
  919. }
  920. st_ven_l = pinctrl_lookup_state(gpctrl, "ven_low");
  921. if (IS_ERR(st_ven_l)) {
  922. ret = PTR_ERR(st_ven_l);
  923. pr_debug("%s : pinctrl err, ven_low\n", __func__);
  924. goto end;
  925. }
  926. st_rst_h = pinctrl_lookup_state(gpctrl, "rst_high");
  927. if (IS_ERR(st_rst_h)) {
  928. ret = PTR_ERR(st_rst_h);
  929. pr_debug("%s : pinctrl err, rst_high\n", __func__);
  930. goto end;
  931. }
  932. st_rst_l = pinctrl_lookup_state(gpctrl, "rst_low");
  933. if (IS_ERR(st_rst_l)) {
  934. ret = PTR_ERR(st_rst_l);
  935. pr_debug("%s : pinctrl err, rst_low\n", __func__);
  936. goto end;
  937. }
  938. st_eint_h = pinctrl_lookup_state(gpctrl, "eint_high");
  939. if (IS_ERR(st_eint_h)) {
  940. ret = PTR_ERR(st_eint_h);
  941. pr_debug("%s : pinctrl err, eint_high\n", __func__);
  942. goto end;
  943. }
  944. st_eint_l = pinctrl_lookup_state(gpctrl, "eint_low");
  945. if (IS_ERR(st_eint_l)) {
  946. ret = PTR_ERR(st_eint_l);
  947. pr_debug("%s : pinctrl err, eint_low\n", __func__);
  948. goto end;
  949. }
  950. st_irq_init = pinctrl_lookup_state(gpctrl, "irq_init");
  951. if (IS_ERR(st_irq_init)) {
  952. ret = PTR_ERR(st_irq_init);
  953. pr_debug("%s : pinctrl err, irq_init\n", __func__);
  954. goto end;
  955. }
  956. /* select state */
  957. ret = mt_nfc_pinctrl_select(gpctrl, st_irq_init);
  958. usleep_range(900, 1000);
  959. ret = mt_nfc_pinctrl_select(gpctrl, st_ven_h);
  960. usleep_range(900, 1000);
  961. ret = mt_nfc_pinctrl_select(gpctrl, st_rst_h);
  962. usleep_range(900, 1000);
  963. ret = mt_nfc_pinctrl_select(gpctrl, st_eint_l);
  964. end:
  965. return ret;
  966. }
  967. static int mt_nfc_gpio_init(void)
  968. {
  969. struct device_node *node;
  970. node = of_find_compatible_node(NULL, NULL, "mediatek,nfc-gpio-v2");
  971. if (node) {
  972. of_property_read_u32_array(node, "gpio-ven",
  973. &(mt6605_platform_data.ven_gpio), 1);
  974. of_property_read_u32_array(node, "gpio-rst",
  975. &(mt6605_platform_data.sysrstb_gpio),
  976. 1);
  977. of_property_read_u32_array(node, "gpio-eint",
  978. &(mt6605_platform_data.eint_gpio),
  979. 1);
  980. of_property_read_u32_array(node, "gpio-irq",
  981. &(mt6605_platform_data.irq_gpio), 1);
  982. } else {
  983. pr_debug("%s : get gpio num err.\n", __func__);
  984. return -1;
  985. }
  986. _gmt6605_dev.irq_gpio = mt6605_platform_data.irq_gpio;
  987. _gmt6605_dev.ven_gpio = mt6605_platform_data.ven_gpio;
  988. _gmt6605_dev.sysrstb_gpio = mt6605_platform_data.sysrstb_gpio;
  989. _gmt6605_dev.eint_gpio = mt6605_platform_data.eint_gpio;
  990. return 0;
  991. }
  992. #endif
  993. static int mt_nfc_get_gpio_value(int gpio_num)
  994. {
  995. int value = 0;
  996. if (mt_nfc_get_gpio_dir(gpio_num) != MTK_NFC_GPIO_DIR_INVALID) {
  997. #if !defined(CONFIG_MTK_LEGACY)
  998. value = __gpio_get_value(gpio_num);
  999. #else
  1000. value = mt_get_gpio_in(gpio_num);
  1001. #endif
  1002. }
  1003. return value;
  1004. }
  1005. static int mt_nfc_get_gpio_dir(int gpio_num)
  1006. {
  1007. if (gpio_num == mt6605_platform_data.irq_gpio) {
  1008. return MTK_NFC_GPIO_DIR_IN; /* input */
  1009. } else if ((gpio_num == mt6605_platform_data.ven_gpio) ||
  1010. (gpio_num == mt6605_platform_data.sysrstb_gpio) ||
  1011. (gpio_num == mt6605_platform_data.eint_gpio)) {
  1012. return MTK_NFC_GPIO_DIR_OUT; /* output */
  1013. } else {
  1014. return MTK_NFC_GPIO_DIR_INVALID;
  1015. }
  1016. }
  1017. /* return 0, success; return <0, fail
  1018. * md_id : modem id
  1019. * md_state : 0, on ; 1, off ;
  1020. * vsim_state : 0, on ; 1, off;
  1021. */
  1022. int inform_nfc_vsim_change(int md_id, int md_state, int vsim_state)
  1023. {
  1024. char send_data[] = {
  1025. 0xaa, 0x0f, 0x03, 0x00, 0x03,
  1026. 0x00, 0x00, 0xaa, 0xf0
  1027. };
  1028. int ret = 0;
  1029. int send_bytes = sizeof(send_data);
  1030. int retry;
  1031. /* AA 0F 03 00 03 00 xx AA F0 */
  1032. /* where xx is defined: */
  1033. /* Bit[7:4] : md_id */
  1034. /* Bit[3:2] : md_state */
  1035. /* Bit[1:0] : vsim_state */
  1036. send_data[6] |= (md_id << 4);
  1037. send_data[6] |= (md_state << 2);
  1038. send_data[6] |= (vsim_state);
  1039. pr_debug
  1040. ("%s, md_id,%d, md_state,%d, vsim_state,%d , send_data[6],0x%X.\n",
  1041. __func__, md_id, md_state, vsim_state, send_data[6]);
  1042. /* send to mt6605 */
  1043. _gmt6605_dev.client->addr = (_gmt6605_dev.client->addr & I2C_MASK_FLAG);
  1044. _gmt6605_dev.client->ext_flag |= I2C_DMA_FLAG;
  1045. _gmt6605_dev.client->timing = 400;
  1046. memcpy(I2CDMAWriteBuf, send_data, send_bytes);
  1047. /* eint pull high */
  1048. #if !defined(CONFIG_MTK_LEGACY)
  1049. mt_nfc_pinctrl_select(gpctrl, st_eint_h);
  1050. #else
  1051. ret = mt_set_gpio_out(mt6605_platform_data.eint_gpio, GPIO_OUT_ONE);
  1052. #endif
  1053. /* sleep 5ms */
  1054. /* msleep(5); */
  1055. usleep_range(1800, 2000);
  1056. for (retry = 0; retry < 10; retry++) {
  1057. /* ret = i2c_master_send(_gmt6605_dev.client, */
  1058. /* (unsigned char *)I2CDMAWriteBuf_pa, send_bytes); */
  1059. ret =
  1060. i2c_master_send(_gmt6605_dev.client,
  1061. (unsigned char *)(uintptr_t)
  1062. I2CDMAWriteBuf_pa, send_bytes);
  1063. pr_debug
  1064. ("%s, i2c_master_send return %d.\n",
  1065. __func__, ret);
  1066. if (ret == send_bytes) {
  1067. pr_debug
  1068. ("%s, send to mt6605 OK. retry %d.\n",
  1069. __func__, retry);
  1070. break;
  1071. } else {
  1072. pr_debug
  1073. ("%s, send to mt6605 fail. retry %d, ret %d.\n",
  1074. __func__, retry, ret);
  1075. }
  1076. /* sleep 2ms */
  1077. /* msleep(2); */
  1078. usleep_range(1800, 2000);
  1079. }
  1080. /* eint pull low */
  1081. #if !defined(CONFIG_MTK_LEGACY)
  1082. mt_nfc_pinctrl_select(gpctrl, st_eint_l);
  1083. #else
  1084. ret = mt_set_gpio_out(mt6605_platform_data.eint_gpio, GPIO_OUT_ZERO);
  1085. #endif
  1086. return 0;
  1087. }
  1088. /*
  1089. * module load/unload record keeping
  1090. */
  1091. static int __init mt6605_dev_init(void)
  1092. {
  1093. int ret;
  1094. pr_debug("mt6605_dev_init\n");
  1095. /* i2c_register_board_info(NFC_I2C_BUSNUM, &nfc_board_info, 1); */
  1096. /* pr_debug("mt6605_dev_init2\n"); */
  1097. platform_driver_register(&mtk_nfc_platform_driver);
  1098. i2c_add_driver(&mt6605_dev_driver);
  1099. _gmt6605_dev.mt6605_device.minor = MISC_DYNAMIC_MINOR;
  1100. _gmt6605_dev.mt6605_device.name = "mt6605";
  1101. _gmt6605_dev.mt6605_device.fops = &mt6605_dev_fops;
  1102. ret = misc_register(&_gmt6605_dev.mt6605_device);
  1103. if (ret) {
  1104. pr_debug("%s : misc_register failed\n", __FILE__);
  1105. return ret;
  1106. }
  1107. /* 2015/08/25 reduce print_log
  1108. pr_debug("mt6605_dev_init success\n");
  1109. */
  1110. return 0;
  1111. }
  1112. static void __exit mt6605_dev_exit(void)
  1113. {
  1114. pr_debug("mt6605_dev_exit\n");
  1115. i2c_del_driver(&mt6605_dev_driver);
  1116. }
  1117. module_init(mt6605_dev_init);
  1118. module_exit(mt6605_dev_exit);
  1119. MODULE_AUTHOR("LiangChi Huang");
  1120. MODULE_DESCRIPTION("MTK NFC driver");
  1121. MODULE_LICENSE("GPL");