pn547.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784
  1. /*
  2. * Copyright (C) 2010 Trusted Logic S.A.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17. *
  18. */
  19. #include <linux/kernel.h>
  20. #include <linux/module.h>
  21. #include <linux/fs.h>
  22. #include <linux/slab.h>
  23. #include <linux/init.h>
  24. #include <linux/list.h>
  25. #include <linux/i2c.h>
  26. #include <linux/clk.h>
  27. #include <linux/irq.h>
  28. #include <linux/jiffies.h>
  29. #include <linux/uaccess.h>
  30. #include <linux/delay.h>
  31. #include <linux/interrupt.h>
  32. #include <linux/io.h>
  33. #include <linux/platform_device.h>
  34. #include <linux/gpio.h>
  35. #include <linux/miscdevice.h>
  36. #include <linux/spinlock.h>
  37. #include <linux/pn547.h>
  38. #include <linux/wakelock.h>
  39. #include <linux/of_gpio.h>
  40. #ifdef CONFIG_NFC_PN547_CLOCK_REQUEST
  41. #include <mach/msm_xo.h>
  42. #include <linux/workqueue.h>
  43. #endif
  44. #ifdef CONFIG_NFC_PN547_PMC8974_CLK_REQ
  45. #include <linux/clk.h>
  46. #endif
  47. #ifdef CONFIG_NFC_PN547_8226_USE_BBCLK2
  48. #include <linux/clk.h>
  49. #endif
  50. #define MAX_BUFFER_SIZE 512
  51. #undef pr_info
  52. #undef pr_debug
  53. #undef pr_err
  54. #define pr_info printk
  55. #define pr_debug printk
  56. #define pr_err printk
  57. #define NFC_DEBUG 0
  58. #define MAX_TRY_I2C_READ 10
  59. #define I2C_ADDR_READ_L 0x51
  60. #define I2C_ADDR_READ_H 0x57
  61. #ifdef CONFIG_MACH_VICTORLTE_CTC
  62. extern unsigned int system_rev;
  63. #endif
  64. struct pn547_dev {
  65. wait_queue_head_t read_wq;
  66. struct mutex read_mutex;
  67. struct i2c_client *client;
  68. struct miscdevice pn547_device;
  69. void (*conf_gpio) (void);
  70. unsigned int ven_gpio;
  71. unsigned int firm_gpio;
  72. unsigned int irq_gpio;
  73. atomic_t irq_enabled;
  74. atomic_t read_flag;
  75. bool cancel_read;
  76. struct wake_lock nfc_wake_lock;
  77. #ifdef CONFIG_NFC_PN547_PMC8974_CLK_REQ
  78. struct clk *nfc_clk;
  79. #endif
  80. #ifdef CONFIG_NFC_PN547_8226_USE_BBCLK2
  81. struct clk *nfc_clock;
  82. unsigned int clk_req_gpio;
  83. #endif
  84. #ifdef CONFIG_NFC_PN547_CLOCK_REQUEST
  85. unsigned int clk_req_gpio;
  86. unsigned int clk_req_irq;
  87. struct msm_xo_voter *nfc_clock;
  88. struct work_struct work_nfc_clock;
  89. struct workqueue_struct *wq_clock;
  90. bool clock_state;
  91. #endif
  92. };
  93. static irqreturn_t pn547_dev_irq_handler(int irq, void *dev_id)
  94. {
  95. struct pn547_dev *pn547_dev = dev_id;
  96. if (!gpio_get_value(pn547_dev->irq_gpio)) {
  97. #if NFC_DEBUG
  98. pr_err("%s, irq_gpio = %d\n", __func__,
  99. gpio_get_value(pn547_dev->irq_gpio));
  100. #endif
  101. return IRQ_HANDLED;
  102. }
  103. /* Wake up waiting readers */
  104. atomic_set(&pn547_dev->read_flag, 1);
  105. wake_up(&pn547_dev->read_wq);
  106. #if NFC_DEBUG
  107. pr_info("pn547 : call\n");
  108. #endif
  109. wake_lock_timeout(&pn547_dev->nfc_wake_lock, 2*HZ);
  110. return IRQ_HANDLED;
  111. }
  112. #ifdef CONFIG_NFC_PN547_CLOCK_REQUEST
  113. static void nfc_work_func_clock(struct work_struct *work)
  114. {
  115. struct pn547_dev *pn547_dev = container_of(work, struct pn547_dev,
  116. work_nfc_clock);
  117. int ret = 0;
  118. if (gpio_get_value(pn547_dev->clk_req_gpio)) {
  119. if (pn547_dev->clock_state == false) {
  120. ret = msm_xo_mode_vote(pn547_dev->nfc_clock,
  121. MSM_XO_MODE_ON);
  122. if (ret < 0) {
  123. pr_err("%s: Failed to vote for TCX0_A1 ON (%d)\n",
  124. __func__, ret);
  125. }
  126. pn547_dev->clock_state = true;
  127. }
  128. } else {
  129. if (pn547_dev->clock_state == true) {
  130. ret = msm_xo_mode_vote(pn547_dev->nfc_clock,
  131. MSM_XO_MODE_OFF);
  132. if (ret < 0) {
  133. pr_err("%s: Failed to vote for TCX0_A1 OFF (%d)\n",
  134. __func__, ret);
  135. }
  136. pn547_dev->clock_state = false;
  137. }
  138. }
  139. }
  140. static irqreturn_t pn547_dev_clk_req_irq_handler(int irq, void *dev_id)
  141. {
  142. struct pn547_dev *pn547_dev = dev_id;
  143. queue_work(pn547_dev->wq_clock, &pn547_dev->work_nfc_clock);
  144. return IRQ_HANDLED;
  145. }
  146. #endif
  147. static ssize_t pn547_dev_read(struct file *filp, char __user *buf,
  148. size_t count, loff_t *offset)
  149. {
  150. struct pn547_dev *pn547_dev = filp->private_data;
  151. char tmp[MAX_BUFFER_SIZE] = {0, };
  152. int ret = 0;
  153. #ifdef CONFIG_NFC_PN544
  154. int readingWatchdog = 0;
  155. #endif
  156. if (count > MAX_BUFFER_SIZE)
  157. count = MAX_BUFFER_SIZE;
  158. pr_debug("%s : reading %zu bytes. irq=%s\n", __func__, count,
  159. gpio_get_value(pn547_dev->irq_gpio) ? "1" : "0");
  160. #if NFC_DEBUG
  161. pr_info("pn547 : + r\n");
  162. #endif
  163. mutex_lock(&pn547_dev->read_mutex);
  164. #ifdef CONFIG_NFC_PN544
  165. wait_irq:
  166. #endif
  167. if (!gpio_get_value(pn547_dev->irq_gpio)) {
  168. atomic_set(&pn547_dev->read_flag, 0);
  169. if (filp->f_flags & O_NONBLOCK) {
  170. pr_info("%s : O_NONBLOCK\n", __func__);
  171. ret = -EAGAIN;
  172. goto fail;
  173. }
  174. #if NFC_DEBUG
  175. pr_info("pn547: wait_event_interruptible : in\n");
  176. #endif
  177. if (!gpio_get_value(pn547_dev->irq_gpio))
  178. ret = wait_event_interruptible(pn547_dev->read_wq,
  179. atomic_read(&pn547_dev->read_flag));
  180. #if NFC_DEBUG
  181. pr_info("pn547 : h\n");
  182. #endif
  183. if (pn547_dev->cancel_read) {
  184. pn547_dev->cancel_read = false;
  185. ret = -1;
  186. goto fail;
  187. }
  188. if (ret)
  189. goto fail;
  190. }
  191. /* Read data */
  192. ret = i2c_master_recv(pn547_dev->client, tmp, count);
  193. #ifdef CONFIG_NFC_PN544
  194. /* If bad frame(from 0x51 to 0x57) is received from pn65n,
  195. * we need to read again after waiting that IRQ is down.
  196. * if data is not ready, pn65n will send from 0x51 to 0x57. */
  197. if ((I2C_ADDR_READ_L <= tmp[0] && tmp[0] <= I2C_ADDR_READ_H)
  198. && readingWatchdog < MAX_TRY_I2C_READ) {
  199. pr_warn("%s: data is not ready yet.data = 0x%x, cnt=%d\n",
  200. __func__, tmp[0], readingWatchdog);
  201. usleep_range(2000, 2000); /* sleep 2ms to wait for IRQ */
  202. readingWatchdog++;
  203. goto wait_irq;
  204. }
  205. #endif
  206. #if NFC_DEBUG
  207. pr_info("pn547: i2c_master_recv\n");
  208. #endif
  209. mutex_unlock(&pn547_dev->read_mutex);
  210. if (ret < 0) {
  211. pr_err("%s: i2c_master_recv returned %d\n", __func__,
  212. ret);
  213. return ret;
  214. }
  215. if (ret > count) {
  216. pr_err("%s: received too many bytes from i2c (%d)\n",
  217. __func__, ret);
  218. return -EIO;
  219. }
  220. if (copy_to_user(buf, tmp, ret)) {
  221. pr_err("%s : failed to copy to user space\n", __func__);
  222. return -EFAULT;
  223. }
  224. return ret;
  225. fail:
  226. mutex_unlock(&pn547_dev->read_mutex);
  227. return ret;
  228. }
  229. static ssize_t pn547_dev_write(struct file *filp, const char __user *buf,
  230. size_t count, loff_t *offset)
  231. {
  232. struct pn547_dev *pn547_dev;
  233. char tmp[MAX_BUFFER_SIZE] = {0, };
  234. int ret = 0, retry = 2;
  235. pn547_dev = filp->private_data;
  236. #if NFC_DEBUG
  237. pr_info("pn547 : + w\n");
  238. #endif
  239. if (count > MAX_BUFFER_SIZE)
  240. count = MAX_BUFFER_SIZE;
  241. if (copy_from_user(tmp, buf, count)) {
  242. pr_err("%s : failed to copy from user space\n", __func__);
  243. return -EFAULT;
  244. }
  245. pr_debug("%s : writing %zu bytes.\n", __func__, count);
  246. /* Write data */
  247. do {
  248. retry--;
  249. ret = i2c_master_send(pn547_dev->client, tmp, count);
  250. if (ret == count)
  251. break;
  252. usleep_range(6000, 10000); /* Retry, chip was in standby */
  253. #if NFC_DEBUG
  254. pr_debug("%s : retry = %d\n", __func__, retry);
  255. #endif
  256. } while (retry);
  257. #if NFC_DEBUG
  258. pr_info("pn547 : - w\n");
  259. #endif
  260. if (ret != count) {
  261. pr_err("%s : i2c_master_send returned %d\n", __func__, ret);
  262. ret = -EIO;
  263. }
  264. return ret;
  265. }
  266. static int pn547_dev_open(struct inode *inode, struct file *filp)
  267. {
  268. struct pn547_dev *pn547_dev = container_of(filp->private_data,
  269. struct pn547_dev,
  270. pn547_device);
  271. filp->private_data = pn547_dev;
  272. pr_debug("%s : %d,%d\n", __func__, imajor(inode), iminor(inode));
  273. return 0;
  274. }
  275. static long pn547_dev_ioctl(struct file *filp,
  276. unsigned int cmd, unsigned long arg)
  277. {
  278. struct pn547_dev *pn547_dev = filp->private_data;
  279. switch (cmd) {
  280. case PN547_SET_PWR:
  281. if (arg == 2) {
  282. /* power on with firmware download (requires hw reset)
  283. */
  284. #if defined(CONFIG_SEC_MILLETWIFI_COMMON) || defined(CONFIG_SEC_RUBENSLTE_COMMON) || defined(CONFIG_SEC_RUBENSWIFI_COMMON)
  285. gpio_direction_output(pn547_dev->ven_gpio, 1);
  286. #endif
  287. gpio_set_value_cansleep(pn547_dev->ven_gpio, 1);
  288. gpio_set_value(pn547_dev->firm_gpio, 1);
  289. usleep_range(10000, 10050);
  290. gpio_set_value_cansleep(pn547_dev->ven_gpio, 0);
  291. usleep_range(10000, 10050);
  292. gpio_set_value_cansleep(pn547_dev->ven_gpio, 1);
  293. usleep_range(10000, 10050);
  294. if (atomic_read(&pn547_dev->irq_enabled) == 0) {
  295. atomic_set(&pn547_dev->irq_enabled, 1);
  296. enable_irq(pn547_dev->client->irq);
  297. enable_irq_wake(pn547_dev->client->irq);
  298. }
  299. pr_info("%s power on with firmware, irq=%d\n", __func__,
  300. atomic_read(&pn547_dev->irq_enabled));
  301. } else if (arg == 1) {
  302. /* power on */
  303. if (pn547_dev->conf_gpio)
  304. pn547_dev->conf_gpio();
  305. gpio_set_value(pn547_dev->firm_gpio, 0);
  306. #if defined(CONFIG_SEC_MILLETWIFI_COMMON) || defined(CONFIG_SEC_RUBENSLTE_COMMON) || defined(CONFIG_SEC_RUBENSWIFI_COMMON)
  307. gpio_direction_output(pn547_dev->ven_gpio, 1);
  308. #endif
  309. gpio_set_value_cansleep(pn547_dev->ven_gpio, 1);
  310. usleep_range(10000, 10050);
  311. if (atomic_read(&pn547_dev->irq_enabled) == 0) {
  312. atomic_set(&pn547_dev->irq_enabled, 1);
  313. enable_irq(pn547_dev->client->irq);
  314. enable_irq_wake(pn547_dev->client->irq);
  315. }
  316. pr_info("%s power on, irq=%d\n", __func__,
  317. atomic_read(&pn547_dev->irq_enabled));
  318. } else if (arg == 0) {
  319. /* power off */
  320. if (atomic_read(&pn547_dev->irq_enabled) == 1) {
  321. atomic_set(&pn547_dev->irq_enabled, 0);
  322. disable_irq_wake(pn547_dev->client->irq);
  323. disable_irq_nosync(pn547_dev->client->irq);
  324. }
  325. pr_info("%s power off, irq=%d\n", __func__,
  326. atomic_read(&pn547_dev->irq_enabled));
  327. gpio_set_value(pn547_dev->firm_gpio, 0);
  328. #if defined(CONFIG_SEC_MILLETWIFI_COMMON) || defined(CONFIG_SEC_RUBENSLTE_COMMON) || defined(CONFIG_SEC_RUBENSWIFI_COMMON)
  329. gpio_direction_output(pn547_dev->ven_gpio, 0);
  330. #endif
  331. gpio_set_value_cansleep(pn547_dev->ven_gpio, 0);
  332. usleep_range(10000, 10050);
  333. } else if (arg == 3) {
  334. pr_info("%s Read Cancel\n", __func__);
  335. pn547_dev->cancel_read = true;
  336. atomic_set(&pn547_dev->read_flag, 1);
  337. wake_up(&pn547_dev->read_wq);
  338. } else {
  339. pr_err("%s bad arg %lu\n", __func__, arg);
  340. return -EINVAL;
  341. }
  342. break;
  343. default:
  344. pr_err("%s bad ioctl %u\n", __func__, cmd);
  345. return -EINVAL;
  346. }
  347. return 0;
  348. }
  349. static const struct file_operations pn547_dev_fops = {
  350. .owner = THIS_MODULE,
  351. .llseek = no_llseek,
  352. .read = pn547_dev_read,
  353. .write = pn547_dev_write,
  354. .open = pn547_dev_open,
  355. .unlocked_ioctl = pn547_dev_ioctl,
  356. };
  357. #ifdef CONFIG_OF
  358. static int pn547_parse_dt(struct device *dev,
  359. struct pn547_i2c_platform_data *pdata)
  360. {
  361. struct device_node *np = dev->of_node;
  362. #ifdef CONFIG_NFC_PN547_8226_USE_BBCLK2
  363. u32 gpio_flags;
  364. #endif
  365. pdata->irq_gpio = of_get_named_gpio_flags(np, "pn547,irq-gpio",
  366. 0, &pdata->irq_gpio_flags);
  367. pdata->ven_gpio = of_get_named_gpio_flags(np, "pn547,ven-gpio",
  368. 0, &pdata->ven_gpio_flags);
  369. pdata->firm_gpio = of_get_named_gpio_flags(np, "pn547,firm-gpio",
  370. 0, &pdata->firm_gpio_flags);
  371. #ifdef CONFIG_NFC_PN547_8226_USE_BBCLK2
  372. pdata->clk_req_gpio = of_get_named_gpio_flags(np, "pn547,clk_req_gpio",
  373. 0, &gpio_flags);
  374. #endif
  375. if (pdata->firm_gpio < 0)
  376. of_property_read_u32(np, "pn547,firm-expander-gpio",
  377. &pdata->firm_gpio);
  378. pr_info("%s: irq : %d, ven : %d, firm : %d\n",
  379. __func__, pdata->irq_gpio, pdata->ven_gpio,
  380. pdata->firm_gpio);
  381. return 0;
  382. }
  383. #else
  384. static int pn547_parse_dt(struct device *dev,
  385. struct pn547_i2c_platform_data *pdata)
  386. {
  387. return -ENODEV;
  388. }
  389. #endif
  390. static int pn547_probe(struct i2c_client *client,
  391. const struct i2c_device_id *id)
  392. {
  393. int ret;
  394. int err;
  395. int addr;
  396. char tmp[4] = {0x20, 0x00, 0x01, 0x01};
  397. int addrcnt;
  398. struct pn547_i2c_platform_data *platform_data;
  399. struct pn547_dev *pn547_dev;
  400. #ifdef CONFIG_MACH_VICTORLTE_CTC
  401. pr_info("%s : start system_rev : %d\n", __func__,system_rev);
  402. if (system_rev < 3)
  403. {
  404. pr_info("%s : probe fail \n", __func__);
  405. return -ENODEV;
  406. }
  407. #endif
  408. if (client->dev.of_node) {
  409. platform_data = devm_kzalloc(&client->dev,
  410. sizeof(struct pn547_i2c_platform_data), GFP_KERNEL);
  411. if (!platform_data) {
  412. dev_err(&client->dev, "Failed to allocate memory\n");
  413. return -ENOMEM;
  414. }
  415. err = pn547_parse_dt(&client->dev, platform_data);
  416. if (err)
  417. return err;
  418. } else {
  419. platform_data = client->dev.platform_data;
  420. }
  421. if (platform_data == NULL) {
  422. pr_err("%s : nfc probe fail\n", __func__);
  423. return -ENODEV;
  424. }
  425. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  426. pr_err("%s : need I2C_FUNC_I2C\n", __func__);
  427. return -ENODEV;
  428. }
  429. ret = gpio_request(platform_data->irq_gpio, "nfc_int");
  430. if (ret)
  431. return -ENODEV;
  432. ret = gpio_request(platform_data->ven_gpio, "nfc_ven");
  433. if (ret)
  434. goto err_ven;
  435. ret = gpio_request(platform_data->firm_gpio, "nfc_firm");
  436. if (ret)
  437. goto err_firm;
  438. #ifdef CONFIG_NFC_PN547_CLOCK_REQUEST
  439. ret = gpio_request(platform_data->clk_req_gpio, "nfc_clk_req");
  440. if (ret)
  441. goto err_clk_req;
  442. #endif
  443. #ifdef CONFIG_NFC_PN547_8226_USE_BBCLK2
  444. ret = gpio_request(platform_data->clk_req_gpio, "nfc_clk_req");
  445. if (ret)
  446. goto err_clk_req;
  447. #endif
  448. pn547_dev = kzalloc(sizeof(*pn547_dev), GFP_KERNEL);
  449. if (pn547_dev == NULL) {
  450. dev_err(&client->dev,
  451. "failed to allocate memory for module data\n");
  452. ret = -ENOMEM;
  453. goto err_exit;
  454. }
  455. #ifdef CONFIG_NFC_PN547_CLOCK_REQUEST
  456. pn547_dev->nfc_clock = msm_xo_get(MSM_XO_TCXO_A1, "nfc");
  457. if (IS_ERR(pn547_dev->nfc_clock)) {
  458. ret = PTR_ERR(pn547_dev->nfc_clock);
  459. printk(KERN_ERR "%s: Couldn't get TCXO_A1 vote for NFC (%d)\n",
  460. __func__, ret);
  461. ret = -ENODEV;
  462. goto err_get_clock;
  463. }
  464. pn547_dev->clock_state = false;
  465. #endif
  466. #ifdef CONFIG_NFC_PN547_8226_USE_BBCLK2
  467. pn547_dev->nfc_clock = clk_get(NULL, "nfc_clock");
  468. if (IS_ERR(pn547_dev->nfc_clock)) {
  469. ret = PTR_ERR(pn547_dev->nfc_clock);
  470. printk(KERN_ERR "%s: Couldn't get D1 (%d)\n",
  471. __func__, ret);
  472. } else {
  473. if (clk_prepare_enable(pn547_dev->nfc_clock))
  474. printk(KERN_ERR "%s: Couldn't prepare D1\n",
  475. __func__);
  476. }
  477. #endif
  478. #ifdef CONFIG_NFC_PN547_PMC8974_CLK_REQ
  479. #ifdef CONFIG_NFC_I2C_OVERWRITE
  480. pn547_dev->nfc_clk = clk_get(NULL, "nfc_clk");
  481. #else
  482. pn547_dev->nfc_clk = clk_get(&client->dev, "nfc_clk");
  483. #endif
  484. if (IS_ERR(pn547_dev->nfc_clk)) {
  485. ret = PTR_ERR(pn547_dev->nfc_clk);
  486. printk(KERN_ERR "%s: Couldn't get D1 (%d)\n",
  487. __func__, ret);
  488. } else {
  489. if (clk_prepare_enable(pn547_dev->nfc_clk))
  490. printk(KERN_ERR "%s: Couldn't prepare D1\n",
  491. __func__);
  492. }
  493. #endif
  494. pr_info("%s : IRQ num %d\n", __func__, client->irq);
  495. pn547_dev->irq_gpio = platform_data->irq_gpio;
  496. pn547_dev->ven_gpio = platform_data->ven_gpio;
  497. pn547_dev->firm_gpio = platform_data->firm_gpio;
  498. pn547_dev->conf_gpio = platform_data->conf_gpio;
  499. #ifdef CONFIG_NFC_PN547_CLOCK_REQUEST
  500. pn547_dev->clk_req_gpio = platform_data->clk_req_gpio;
  501. pn547_dev->clk_req_irq = platform_data->clk_req_irq;
  502. #endif
  503. #ifdef CONFIG_NFC_PN547_8226_USE_BBCLK2
  504. pn547_dev->clk_req_gpio = platform_data->clk_req_gpio;
  505. #endif
  506. pn547_dev->client = client;
  507. /* init mutex and queues */
  508. init_waitqueue_head(&pn547_dev->read_wq);
  509. mutex_init(&pn547_dev->read_mutex);
  510. pn547_dev->pn547_device.minor = MISC_DYNAMIC_MINOR;
  511. #ifdef CONFIG_NFC_PN547
  512. pn547_dev->pn547_device.name = "pn547";
  513. #else
  514. pn547_dev->pn547_device.name = "pn544";
  515. #endif
  516. pn547_dev->pn547_device.fops = &pn547_dev_fops;
  517. ret = misc_register(&pn547_dev->pn547_device);
  518. if (ret) {
  519. pr_err("%s : misc_register failed\n", __FILE__);
  520. goto err_misc_register;
  521. }
  522. /* request irq. the irq is set whenever the chip has data available
  523. * for reading. it is cleared when all data has been read.
  524. */
  525. pr_info("%s : requesting IRQ %d\n", __func__, client->irq);
  526. gpio_direction_input(pn547_dev->irq_gpio);
  527. gpio_direction_output(pn547_dev->ven_gpio, 0);
  528. gpio_direction_output(pn547_dev->firm_gpio, 0);
  529. #if defined(CONFIG_NFC_PN547_CLOCK_REQUEST) || defined(CONFIG_NFC_PN547_8226_USE_BBCLK2)
  530. gpio_direction_input(pn547_dev->clk_req_gpio);
  531. #endif
  532. i2c_set_clientdata(client, pn547_dev);
  533. wake_lock_init(&pn547_dev->nfc_wake_lock,
  534. WAKE_LOCK_SUSPEND, "nfc_wake_lock");
  535. #ifdef CONFIG_NFC_PN547_CLOCK_REQUEST
  536. pn547_dev->wq_clock = create_singlethread_workqueue("nfc_wq");
  537. if (!pn547_dev->wq_clock) {
  538. ret = -ENOMEM;
  539. pr_err("%s: could not create workqueue\n", __func__);
  540. goto err_create_workqueue;
  541. }
  542. INIT_WORK(&pn547_dev->work_nfc_clock, nfc_work_func_clock);
  543. #endif
  544. if(client->irq <=0)
  545. {
  546. pr_info("%s : [Before] requesting IRQ %d\n", __func__, client->irq);
  547. client->irq = gpio_to_irq(pn547_dev->irq_gpio);
  548. pr_info("%s : [After] requesting IRQ %d\n", __func__, client->irq);
  549. }
  550. ret = request_irq(client->irq, pn547_dev_irq_handler,
  551. IRQF_TRIGGER_RISING, "pn547", pn547_dev);
  552. if (ret) {
  553. dev_err(&client->dev, "request_irq failed\n");
  554. goto err_request_irq_failed;
  555. }
  556. disable_irq_nosync(pn547_dev->client->irq);
  557. atomic_set(&pn547_dev->irq_enabled, 0);
  558. #ifdef CONFIG_NFC_PN547_CLOCK_REQUEST
  559. ret = request_irq(pn547_dev->clk_req_irq, pn547_dev_clk_req_irq_handler,
  560. IRQF_SHARED | IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING
  561. , "pn547_clk_req", pn547_dev);
  562. if (ret) {
  563. dev_err(&client->dev, "request_irq(clk_req) failed\n");
  564. goto err_request_irq_failed;
  565. }
  566. enable_irq_wake(pn547_dev->clk_req_irq);
  567. #endif
  568. gpio_set_value(pn547_dev->ven_gpio, 1);
  569. gpio_set_value(pn547_dev->firm_gpio, 1); /* add firmware pin */
  570. usleep_range(4900, 5000);
  571. gpio_set_value(pn547_dev->ven_gpio, 0);
  572. usleep_range(4900, 5000);
  573. gpio_set_value(pn547_dev->ven_gpio, 1);
  574. usleep_range(4900, 5000);
  575. for (addr = 0x2B; addr > 0x27; addr--) {
  576. client->addr = addr;
  577. addrcnt = 2;
  578. do {
  579. ret = i2c_master_send(client, tmp, 4);
  580. if (ret > 0) {
  581. pr_info("%s : i2c addr=0x%X\n",
  582. __func__, client->addr);
  583. break;
  584. }
  585. } while (addrcnt--);
  586. if (ret > 0)
  587. break;
  588. }
  589. if(ret <= 0)
  590. client->addr = 0x2B;
  591. gpio_set_value(pn547_dev->ven_gpio, 0);
  592. gpio_set_value(pn547_dev->firm_gpio, 0); /* add */
  593. if (ret < 0)
  594. pr_err("%s : fail to get i2c addr\n", __func__);
  595. /* goto err_request_irq_failed; */
  596. else
  597. pr_info("%s : success\n", __func__);
  598. return 0;
  599. err_request_irq_failed:
  600. #ifdef CONFIG_NFC_PN547_CLOCK_REQUEST
  601. err_create_workqueue:
  602. #endif
  603. misc_deregister(&pn547_dev->pn547_device);
  604. wake_lock_destroy(&pn547_dev->nfc_wake_lock);
  605. err_misc_register:
  606. mutex_destroy(&pn547_dev->read_mutex);
  607. #ifdef CONFIG_NFC_PN547_CLOCK_REQUEST
  608. msm_xo_put(pn547_dev->nfc_clock);
  609. err_get_clock:
  610. #endif
  611. kfree(pn547_dev);
  612. err_exit:
  613. #if defined(CONFIG_NFC_PN547_CLOCK_REQUEST) || defined(CONFIG_NFC_PN547_8226_USE_BBCLK2)
  614. gpio_free(platform_data->clk_req_gpio);
  615. err_clk_req:
  616. #endif
  617. gpio_free(platform_data->firm_gpio);
  618. err_firm:
  619. gpio_free(platform_data->ven_gpio);
  620. err_ven:
  621. gpio_free(platform_data->irq_gpio);
  622. pr_err("[pn547] pn547_probe fail!\n");
  623. return ret;
  624. }
  625. static int pn547_remove(struct i2c_client *client)
  626. {
  627. struct pn547_dev *pn547_dev;
  628. pn547_dev = i2c_get_clientdata(client);
  629. #ifdef CONFIG_NFC_PN547_8226_USE_BBCLK2
  630. if(pn547_dev->nfc_clock)
  631. clk_unprepare(pn547_dev->nfc_clock);
  632. #endif
  633. #ifdef CONFIG_NFC_PN547_PMC8974_CLK_REQ
  634. if (pn547_dev->nfc_clk)
  635. clk_unprepare(pn547_dev->nfc_clk);
  636. #endif
  637. wake_lock_destroy(&pn547_dev->nfc_wake_lock);
  638. free_irq(client->irq, pn547_dev);
  639. misc_deregister(&pn547_dev->pn547_device);
  640. mutex_destroy(&pn547_dev->read_mutex);
  641. gpio_free(pn547_dev->irq_gpio);
  642. gpio_free(pn547_dev->ven_gpio);
  643. gpio_free(pn547_dev->firm_gpio);
  644. #ifdef CONFIG_NFC_PN547_8226_USE_BBCLK2
  645. gpio_free(pn547_dev->clk_req_gpio);
  646. #endif
  647. #ifdef CONFIG_NFC_PN547_CLOCK_REQUEST
  648. gpio_free(pn547_dev->clk_req_gpio);
  649. msm_xo_put(pn547_dev->nfc_clock);
  650. #endif
  651. kfree(pn547_dev);
  652. return 0;
  653. }
  654. static const struct i2c_device_id pn547_id[] = {
  655. {"pn547", 0},
  656. {}
  657. };
  658. #ifdef CONFIG_OF
  659. static struct of_device_id nfc_match_table[] = {
  660. { .compatible = "pn547",},
  661. {},
  662. };
  663. #else
  664. #define nfc_match_table NULL
  665. #endif
  666. static struct i2c_driver pn547_driver = {
  667. .id_table = pn547_id,
  668. .probe = pn547_probe,
  669. .remove = pn547_remove,
  670. .driver = {
  671. .owner = THIS_MODULE,
  672. .name = "pn547",
  673. .of_match_table = nfc_match_table,
  674. },
  675. };
  676. /*
  677. * module load/unload record keeping
  678. */
  679. static int __init pn547_dev_init(void)
  680. {
  681. pr_info("Loading pn547 driver\n");
  682. return i2c_add_driver(&pn547_driver);
  683. }
  684. module_init(pn547_dev_init);
  685. static void __exit pn547_dev_exit(void)
  686. {
  687. pr_info("Unloading pn547 driver\n");
  688. i2c_del_driver(&pn547_driver);
  689. }
  690. module_exit(pn547_dev_exit);
  691. MODULE_AUTHOR("Sylvain Fonteneau");
  692. MODULE_DESCRIPTION("NFC PN547 driver");
  693. MODULE_LICENSE("GPL");