pm8xxx-nfc.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /* Copyright (c) 2010,2011 The Linux Foundation. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. */
  13. /*
  14. * Qualcomm PMIC8XXX NFC driver
  15. *
  16. */
  17. #include <linux/module.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/err.h>
  20. #include <linux/debugfs.h>
  21. #include <linux/slab.h>
  22. #include <linux/mfd/pm8xxx/core.h>
  23. #include <linux/mfd/pm8xxx/nfc.h>
  24. /* PM8XXX NFC */
  25. #define SSBI_REG_NFC_CTRL 0x14D
  26. #define SSBI_REG_NFC_TEST 0x14E
  27. /* NFC_CTRL */
  28. #define PM8XXX_NFC_SUPPORT_EN 0x80
  29. #define PM8XXX_NFC_LDO_EN 0x40
  30. #define PM8XXX_NFC_EN 0x20
  31. #define PM8XXX_NFC_EXT_VDDLDO_EN 0x10
  32. #define PM8XXX_NFC_VPH_PWR_EN 0x08
  33. #define PM8XXX_NFC_RESERVED 0x04
  34. #define PM8XXX_NFC_VDDLDO_LEVEL 0x03
  35. /* NFC_TEST */
  36. #define PM8XXX_NFC_VDDLDO_MON_EN 0x80
  37. #define PM8XXX_NFC_ATEST_EN 0x40
  38. #define PM8XXX_NFC_DTEST1_EN 0x20
  39. #define PM8XXX_NFC_RESERVED2 0x18
  40. #define PM8XXX_NFC_VDDLDO_OK_S 0x04
  41. #define PM8XXX_NFC_MBG_EN_S 0x02
  42. #define PM8XXX_NFC_EXT_EN_S 0x01
  43. struct pm8xxx_nfc_device {
  44. struct device *dev;
  45. struct mutex nfc_mutex;
  46. #if defined(CONFIG_DEBUG_FS)
  47. struct dentry *dent;
  48. #endif
  49. };
  50. static struct pm8xxx_nfc_device *nfc_dev;
  51. /* APIs */
  52. /*
  53. * pm8xxx_nfc_request - request a handle to access NFC device
  54. */
  55. struct pm8xxx_nfc_device *pm8xxx_nfc_request(void)
  56. {
  57. return nfc_dev;
  58. }
  59. EXPORT_SYMBOL(pm8xxx_nfc_request);
  60. /*
  61. * pm8xxx_nfc_config - configure NFC signals
  62. *
  63. * @nfcdev: the NFC device
  64. * @mask: signal mask to configure
  65. * @flags: control flags
  66. */
  67. int pm8xxx_nfc_config(struct pm8xxx_nfc_device *nfcdev, u32 mask, u32 flags)
  68. {
  69. u8 nfc_ctrl, nfc_test, m, f;
  70. int rc;
  71. if (nfcdev == NULL || IS_ERR(nfcdev) || !mask)
  72. return -EINVAL;
  73. mutex_lock(&nfcdev->nfc_mutex);
  74. if (!(mask & PM_NFC_CTRL_REQ))
  75. goto config_test;
  76. rc = pm8xxx_readb(nfcdev->dev->parent, SSBI_REG_NFC_CTRL, &nfc_ctrl);
  77. if (rc) {
  78. pr_err("%s: FAIL pm8xxx_readb(): rc=%d (nfc_ctrl=0x%x)\n",
  79. __func__, rc, nfc_ctrl);
  80. goto config_done;
  81. }
  82. m = mask & 0x00ff;
  83. f = flags & 0x00ff;
  84. nfc_ctrl &= ~m;
  85. nfc_ctrl |= m & f;
  86. rc = pm8xxx_writeb(nfcdev->dev->parent, SSBI_REG_NFC_CTRL, nfc_ctrl);
  87. if (rc) {
  88. pr_err("%s: FAIL pm8xxx_writeb(): rc=%d (nfc_ctrl=0x%x)\n",
  89. __func__, rc, nfc_ctrl);
  90. goto config_done;
  91. }
  92. config_test:
  93. if (!(mask & PM_NFC_TEST_REQ))
  94. goto config_done;
  95. rc = pm8xxx_readb(nfcdev->dev->parent, SSBI_REG_NFC_TEST, &nfc_test);
  96. if (rc) {
  97. pr_err("%s: FAIL pm8xxx_readb(): rc=%d (nfc_test=0x%x)\n",
  98. __func__, rc, nfc_test);
  99. goto config_done;
  100. }
  101. m = (mask >> 8) & 0x00ff;
  102. f = (flags >> 8) & 0x00ff;
  103. nfc_test &= ~m;
  104. nfc_test |= m & f;
  105. rc = pm8xxx_writeb(nfcdev->dev->parent, SSBI_REG_NFC_TEST, nfc_test);
  106. if (rc) {
  107. pr_err("%s: FAIL pm8xxx_writeb(): rc=%d (nfc_test=0x%x)\n",
  108. __func__, rc, nfc_test);
  109. goto config_done;
  110. }
  111. config_done:
  112. mutex_unlock(&nfcdev->nfc_mutex);
  113. return 0;
  114. }
  115. EXPORT_SYMBOL(pm8xxx_nfc_config);
  116. /*
  117. * pm8xxx_nfc_get_status - get NFC status
  118. *
  119. * @nfcdev: the NFC device
  120. * @mask: of status mask to read
  121. * @status: pointer to the status variable
  122. */
  123. int pm8xxx_nfc_get_status(struct pm8xxx_nfc_device *nfcdev,
  124. u32 mask, u32 *status)
  125. {
  126. u8 nfc_ctrl, nfc_test;
  127. u32 st;
  128. int rc;
  129. if (nfcdev == NULL || IS_ERR(nfcdev) || status == NULL)
  130. return -EINVAL;
  131. st = 0;
  132. mutex_lock(&nfcdev->nfc_mutex);
  133. if (!(mask & PM_NFC_CTRL_REQ))
  134. goto read_test;
  135. rc = pm8xxx_readb(nfcdev->dev->parent, SSBI_REG_NFC_CTRL, &nfc_ctrl);
  136. if (rc) {
  137. pr_err("%s: FAIL pm8xxx_readb(): rc=%d (nfc_ctrl=0x%x)\n",
  138. __func__, rc, nfc_ctrl);
  139. goto get_status_done;
  140. }
  141. read_test:
  142. if (!(mask & (PM_NFC_TEST_REQ | PM_NFC_TEST_STATUS)))
  143. goto get_status_done;
  144. rc = pm8xxx_readb(nfcdev->dev->parent, SSBI_REG_NFC_TEST, &nfc_test);
  145. if (rc)
  146. pr_err("%s: FAIL pm8xxx_readb(): rc=%d (nfc_test=0x%x)\n",
  147. __func__, rc, nfc_test);
  148. get_status_done:
  149. st = nfc_ctrl;
  150. st |= nfc_test << 8;
  151. *status = st;
  152. mutex_unlock(&nfcdev->nfc_mutex);
  153. return 0;
  154. }
  155. EXPORT_SYMBOL(pm8xxx_nfc_get_status);
  156. /*
  157. * pm8xxx_nfc_free - free the NFC device
  158. */
  159. void pm8xxx_nfc_free(struct pm8xxx_nfc_device *nfcdev)
  160. {
  161. /* Disable all signals */
  162. pm8xxx_nfc_config(nfcdev, PM_NFC_CTRL_REQ, 0);
  163. }
  164. EXPORT_SYMBOL(pm8xxx_nfc_free);
  165. #if defined(CONFIG_DEBUG_FS)
  166. static int pm8xxx_nfc_debug_set(void *data, u64 val)
  167. {
  168. struct pm8xxx_nfc_device *nfcdev;
  169. u32 mask, control;
  170. int rc;
  171. nfcdev = (struct pm8xxx_nfc_device *)data;
  172. control = (u32)val & 0xffff;
  173. mask = ((u32)val >> 16) & 0xffff;
  174. rc = pm8xxx_nfc_config(nfcdev, mask, control);
  175. if (rc)
  176. pr_err("%s: ERR pm8xxx_nfc_config: rc=%d, "
  177. "[mask, control]=[0x%x, 0x%x]\n",
  178. __func__, rc, mask, control);
  179. return 0;
  180. }
  181. static int pm8xxx_nfc_debug_get(void *data, u64 *val)
  182. {
  183. struct pm8xxx_nfc_device *nfcdev;
  184. u32 status;
  185. int rc;
  186. nfcdev = (struct pm8xxx_nfc_device *)data;
  187. rc = pm8xxx_nfc_get_status(nfcdev, (u32)-1, &status);
  188. if (rc)
  189. pr_err("%s: ERR pm8xxx_nfc_get_status: rc=%d, status=0x%x\n",
  190. __func__, rc, status);
  191. if (val)
  192. *val = (u64)status;
  193. return 0;
  194. }
  195. DEFINE_SIMPLE_ATTRIBUTE(pm8xxx_nfc_fops, pm8xxx_nfc_debug_get,
  196. pm8xxx_nfc_debug_set, "%llu\n");
  197. static int pm8xxx_nfc_debug_init(struct pm8xxx_nfc_device *nfcdev)
  198. {
  199. struct dentry *dent;
  200. dent = debugfs_create_file("pm8xxx-nfc", 0644, NULL,
  201. (void *)nfcdev, &pm8xxx_nfc_fops);
  202. if (dent == NULL || IS_ERR(dent))
  203. pr_err("%s: ERR debugfs_create_file: dent=0x%x\n",
  204. __func__, (unsigned)dent);
  205. nfcdev->dent = dent;
  206. return 0;
  207. }
  208. #endif
  209. static int __devinit pm8xxx_nfc_probe(struct platform_device *pdev)
  210. {
  211. struct pm8xxx_nfc_device *nfcdev;
  212. nfcdev = kzalloc(sizeof *nfcdev, GFP_KERNEL);
  213. if (nfcdev == NULL) {
  214. pr_err("%s: kzalloc() failed.\n", __func__);
  215. return -ENOMEM;
  216. }
  217. mutex_init(&nfcdev->nfc_mutex);
  218. nfcdev->dev = &pdev->dev;
  219. nfc_dev = nfcdev;
  220. platform_set_drvdata(pdev, nfcdev);
  221. #if defined(CONFIG_DEBUG_FS)
  222. pm8xxx_nfc_debug_init(nfc_dev);
  223. #endif
  224. pr_notice("%s: OK\n", __func__);
  225. return 0;
  226. }
  227. static int __devexit pm8xxx_nfc_remove(struct platform_device *pdev)
  228. {
  229. struct pm8xxx_nfc_device *nfcdev = platform_get_drvdata(pdev);
  230. #if defined(CONFIG_DEBUG_FS)
  231. debugfs_remove(nfcdev->dent);
  232. #endif
  233. platform_set_drvdata(pdev, NULL);
  234. kfree(nfcdev);
  235. return 0;
  236. }
  237. static struct platform_driver pm8xxx_nfc_driver = {
  238. .probe = pm8xxx_nfc_probe,
  239. .remove = __devexit_p(pm8xxx_nfc_remove),
  240. .driver = {
  241. .name = PM8XXX_NFC_DEV_NAME,
  242. .owner = THIS_MODULE,
  243. },
  244. };
  245. static int __init pm8xxx_nfc_init(void)
  246. {
  247. return platform_driver_register(&pm8xxx_nfc_driver);
  248. }
  249. static void __exit pm8xxx_nfc_exit(void)
  250. {
  251. platform_driver_unregister(&pm8xxx_nfc_driver);
  252. }
  253. module_init(pm8xxx_nfc_init);
  254. module_exit(pm8xxx_nfc_exit);
  255. MODULE_LICENSE("GPL v2");
  256. MODULE_DESCRIPTION("PM8XXX NFC driver");
  257. MODULE_VERSION("1.0");
  258. MODULE_ALIAS("platform:" PM8XXX_NFC_DEV_NAME);