pn544.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891
  1. /*
  2. * Driver for the PN544 NFC chip.
  3. *
  4. * Copyright (C) Nokia Corporation
  5. *
  6. * Author: Jari Vanhala <ext-jari.vanhala@nokia.com>
  7. * Contact: Matti Aaltonen <matti.j.aaltonen@nokia.com>
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * version 2 as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. */
  22. #include <linux/completion.h>
  23. #include <linux/crc-ccitt.h>
  24. #include <linux/delay.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/kernel.h>
  27. #include <linux/miscdevice.h>
  28. #include <linux/module.h>
  29. #include <linux/mutex.h>
  30. #include <linux/nfc/pn544.h>
  31. #include <linux/poll.h>
  32. #include <linux/regulator/consumer.h>
  33. #include <linux/serial_core.h> /* for TCGETS */
  34. #include <linux/slab.h>
  35. #define DRIVER_CARD "PN544 NFC"
  36. #define DRIVER_DESC "NFC driver for PN544"
  37. static struct i2c_device_id pn544_id_table[] = {
  38. { PN544_DRIVER_NAME, 0 },
  39. { }
  40. };
  41. MODULE_DEVICE_TABLE(i2c, pn544_id_table);
  42. #define HCI_MODE 0
  43. #define FW_MODE 1
  44. enum pn544_state {
  45. PN544_ST_COLD,
  46. PN544_ST_FW_READY,
  47. PN544_ST_READY,
  48. };
  49. enum pn544_irq {
  50. PN544_NONE,
  51. PN544_INT,
  52. };
  53. struct pn544_info {
  54. struct miscdevice miscdev;
  55. struct i2c_client *i2c_dev;
  56. struct regulator_bulk_data regs[2];
  57. enum pn544_state state;
  58. wait_queue_head_t read_wait;
  59. loff_t read_offset;
  60. enum pn544_irq read_irq;
  61. struct mutex read_mutex; /* Serialize read_irq access */
  62. struct mutex mutex; /* Serialize info struct access */
  63. u8 *buf;
  64. size_t buflen;
  65. };
  66. static const char reg_vdd_io[] = "Vdd_IO";
  67. static const char reg_vbat[] = "VBat";
  68. /* sysfs interface */
  69. static ssize_t pn544_test(struct device *dev,
  70. struct device_attribute *attr, char *buf)
  71. {
  72. struct pn544_info *info = dev_get_drvdata(dev);
  73. struct i2c_client *client = info->i2c_dev;
  74. struct pn544_nfc_platform_data *pdata = client->dev.platform_data;
  75. return snprintf(buf, PAGE_SIZE, "%d\n", pdata->test());
  76. }
  77. static int pn544_enable(struct pn544_info *info, int mode)
  78. {
  79. struct pn544_nfc_platform_data *pdata;
  80. struct i2c_client *client = info->i2c_dev;
  81. int r;
  82. r = regulator_bulk_enable(ARRAY_SIZE(info->regs), info->regs);
  83. if (r < 0)
  84. return r;
  85. pdata = client->dev.platform_data;
  86. info->read_irq = PN544_NONE;
  87. if (pdata->enable)
  88. pdata->enable(mode);
  89. if (mode) {
  90. info->state = PN544_ST_FW_READY;
  91. dev_dbg(&client->dev, "now in FW-mode\n");
  92. } else {
  93. info->state = PN544_ST_READY;
  94. dev_dbg(&client->dev, "now in HCI-mode\n");
  95. }
  96. usleep_range(10000, 15000);
  97. return 0;
  98. }
  99. static void pn544_disable(struct pn544_info *info)
  100. {
  101. struct pn544_nfc_platform_data *pdata;
  102. struct i2c_client *client = info->i2c_dev;
  103. pdata = client->dev.platform_data;
  104. if (pdata->disable)
  105. pdata->disable();
  106. info->state = PN544_ST_COLD;
  107. dev_dbg(&client->dev, "Now in OFF-mode\n");
  108. msleep(PN544_RESETVEN_TIME);
  109. info->read_irq = PN544_NONE;
  110. regulator_bulk_disable(ARRAY_SIZE(info->regs), info->regs);
  111. }
  112. static int check_crc(u8 *buf, int buflen)
  113. {
  114. u8 len;
  115. u16 crc;
  116. len = buf[0] + 1;
  117. if (len < 4 || len != buflen || len > PN544_MSG_MAX_SIZE) {
  118. pr_err(PN544_DRIVER_NAME
  119. ": CRC; corrupt packet len %u (%d)\n", len, buflen);
  120. print_hex_dump(KERN_DEBUG, "crc: ", DUMP_PREFIX_NONE,
  121. 16, 2, buf, buflen, false);
  122. return -EPERM;
  123. }
  124. crc = crc_ccitt(0xffff, buf, len - 2);
  125. crc = ~crc;
  126. if (buf[len-2] != (crc & 0xff) || buf[len-1] != (crc >> 8)) {
  127. pr_err(PN544_DRIVER_NAME ": CRC error 0x%x != 0x%x 0x%x\n",
  128. crc, buf[len-1], buf[len-2]);
  129. print_hex_dump(KERN_DEBUG, "crc: ", DUMP_PREFIX_NONE,
  130. 16, 2, buf, buflen, false);
  131. return -EPERM;
  132. }
  133. return 0;
  134. }
  135. static int pn544_i2c_write(struct i2c_client *client, u8 *buf, int len)
  136. {
  137. int r;
  138. if (len < 4 || len != (buf[0] + 1)) {
  139. dev_err(&client->dev, "%s: Illegal message length: %d\n",
  140. __func__, len);
  141. return -EINVAL;
  142. }
  143. if (check_crc(buf, len))
  144. return -EINVAL;
  145. usleep_range(3000, 6000);
  146. r = i2c_master_send(client, buf, len);
  147. dev_dbg(&client->dev, "send: %d\n", r);
  148. if (r == -EREMOTEIO) { /* Retry, chip was in standby */
  149. usleep_range(6000, 10000);
  150. r = i2c_master_send(client, buf, len);
  151. dev_dbg(&client->dev, "send2: %d\n", r);
  152. }
  153. if (r != len)
  154. return -EREMOTEIO;
  155. return r;
  156. }
  157. static int pn544_i2c_read(struct i2c_client *client, u8 *buf, int buflen)
  158. {
  159. int r;
  160. u8 len;
  161. /*
  162. * You could read a packet in one go, but then you'd need to read
  163. * max size and rest would be 0xff fill, so we do split reads.
  164. */
  165. r = i2c_master_recv(client, &len, 1);
  166. dev_dbg(&client->dev, "recv1: %d\n", r);
  167. if (r != 1)
  168. return -EREMOTEIO;
  169. if (len < PN544_LLC_HCI_OVERHEAD)
  170. len = PN544_LLC_HCI_OVERHEAD;
  171. else if (len > (PN544_MSG_MAX_SIZE - 1))
  172. len = PN544_MSG_MAX_SIZE - 1;
  173. if (1 + len > buflen) /* len+(data+crc16) */
  174. return -EMSGSIZE;
  175. buf[0] = len;
  176. r = i2c_master_recv(client, buf + 1, len);
  177. dev_dbg(&client->dev, "recv2: %d\n", r);
  178. if (r != len)
  179. return -EREMOTEIO;
  180. usleep_range(3000, 6000);
  181. return r + 1;
  182. }
  183. static int pn544_fw_write(struct i2c_client *client, u8 *buf, int len)
  184. {
  185. int r;
  186. dev_dbg(&client->dev, "%s\n", __func__);
  187. if (len < PN544_FW_HEADER_SIZE ||
  188. (PN544_FW_HEADER_SIZE + (buf[1] << 8) + buf[2]) != len)
  189. return -EINVAL;
  190. r = i2c_master_send(client, buf, len);
  191. dev_dbg(&client->dev, "fw send: %d\n", r);
  192. if (r == -EREMOTEIO) { /* Retry, chip was in standby */
  193. usleep_range(6000, 10000);
  194. r = i2c_master_send(client, buf, len);
  195. dev_dbg(&client->dev, "fw send2: %d\n", r);
  196. }
  197. if (r != len)
  198. return -EREMOTEIO;
  199. return r;
  200. }
  201. static int pn544_fw_read(struct i2c_client *client, u8 *buf, int buflen)
  202. {
  203. int r, len;
  204. if (buflen < PN544_FW_HEADER_SIZE)
  205. return -EINVAL;
  206. r = i2c_master_recv(client, buf, PN544_FW_HEADER_SIZE);
  207. dev_dbg(&client->dev, "FW recv1: %d\n", r);
  208. if (r < 0)
  209. return r;
  210. if (r < PN544_FW_HEADER_SIZE)
  211. return -EINVAL;
  212. len = (buf[1] << 8) + buf[2];
  213. if (len == 0) /* just header, no additional data */
  214. return r;
  215. if (len > buflen - PN544_FW_HEADER_SIZE)
  216. return -EMSGSIZE;
  217. r = i2c_master_recv(client, buf + PN544_FW_HEADER_SIZE, len);
  218. dev_dbg(&client->dev, "fw recv2: %d\n", r);
  219. if (r != len)
  220. return -EINVAL;
  221. return r + PN544_FW_HEADER_SIZE;
  222. }
  223. static irqreturn_t pn544_irq_thread_fn(int irq, void *dev_id)
  224. {
  225. struct pn544_info *info = dev_id;
  226. struct i2c_client *client = info->i2c_dev;
  227. BUG_ON(!info);
  228. BUG_ON(irq != info->i2c_dev->irq);
  229. dev_dbg(&client->dev, "IRQ\n");
  230. mutex_lock(&info->read_mutex);
  231. info->read_irq = PN544_INT;
  232. mutex_unlock(&info->read_mutex);
  233. wake_up_interruptible(&info->read_wait);
  234. return IRQ_HANDLED;
  235. }
  236. static enum pn544_irq pn544_irq_state(struct pn544_info *info)
  237. {
  238. enum pn544_irq irq;
  239. mutex_lock(&info->read_mutex);
  240. irq = info->read_irq;
  241. mutex_unlock(&info->read_mutex);
  242. /*
  243. * XXX: should we check GPIO-line status directly?
  244. * return pdata->irq_status() ? PN544_INT : PN544_NONE;
  245. */
  246. return irq;
  247. }
  248. static ssize_t pn544_read(struct file *file, char __user *buf,
  249. size_t count, loff_t *offset)
  250. {
  251. struct pn544_info *info = container_of(file->private_data,
  252. struct pn544_info, miscdev);
  253. struct i2c_client *client = info->i2c_dev;
  254. enum pn544_irq irq;
  255. size_t len;
  256. int r = 0;
  257. dev_dbg(&client->dev, "%s: info: %p, count: %zu\n", __func__,
  258. info, count);
  259. mutex_lock(&info->mutex);
  260. if (info->state == PN544_ST_COLD) {
  261. r = -ENODEV;
  262. goto out;
  263. }
  264. irq = pn544_irq_state(info);
  265. if (irq == PN544_NONE) {
  266. if (file->f_flags & O_NONBLOCK) {
  267. r = -EAGAIN;
  268. goto out;
  269. }
  270. if (wait_event_interruptible(info->read_wait,
  271. (info->read_irq == PN544_INT))) {
  272. r = -ERESTARTSYS;
  273. goto out;
  274. }
  275. }
  276. if (info->state == PN544_ST_FW_READY) {
  277. len = min(count, info->buflen);
  278. mutex_lock(&info->read_mutex);
  279. r = pn544_fw_read(info->i2c_dev, info->buf, len);
  280. info->read_irq = PN544_NONE;
  281. mutex_unlock(&info->read_mutex);
  282. if (r < 0) {
  283. dev_err(&info->i2c_dev->dev, "FW read failed: %d\n", r);
  284. goto out;
  285. }
  286. print_hex_dump(KERN_DEBUG, "FW read: ", DUMP_PREFIX_NONE,
  287. 16, 2, info->buf, r, false);
  288. *offset += r;
  289. if (copy_to_user(buf, info->buf, r)) {
  290. r = -EFAULT;
  291. goto out;
  292. }
  293. } else {
  294. len = min(count, info->buflen);
  295. mutex_lock(&info->read_mutex);
  296. r = pn544_i2c_read(info->i2c_dev, info->buf, len);
  297. info->read_irq = PN544_NONE;
  298. mutex_unlock(&info->read_mutex);
  299. if (r < 0) {
  300. dev_err(&info->i2c_dev->dev, "read failed (%d)\n", r);
  301. goto out;
  302. }
  303. print_hex_dump(KERN_DEBUG, "read: ", DUMP_PREFIX_NONE,
  304. 16, 2, info->buf, r, false);
  305. *offset += r;
  306. if (copy_to_user(buf, info->buf, r)) {
  307. r = -EFAULT;
  308. goto out;
  309. }
  310. }
  311. out:
  312. mutex_unlock(&info->mutex);
  313. return r;
  314. }
  315. static unsigned int pn544_poll(struct file *file, poll_table *wait)
  316. {
  317. struct pn544_info *info = container_of(file->private_data,
  318. struct pn544_info, miscdev);
  319. struct i2c_client *client = info->i2c_dev;
  320. int r = 0;
  321. dev_dbg(&client->dev, "%s: info: %p\n", __func__, info);
  322. mutex_lock(&info->mutex);
  323. if (info->state == PN544_ST_COLD) {
  324. r = -ENODEV;
  325. goto out;
  326. }
  327. poll_wait(file, &info->read_wait, wait);
  328. if (pn544_irq_state(info) == PN544_INT) {
  329. r = POLLIN | POLLRDNORM;
  330. goto out;
  331. }
  332. out:
  333. mutex_unlock(&info->mutex);
  334. return r;
  335. }
  336. static ssize_t pn544_write(struct file *file, const char __user *buf,
  337. size_t count, loff_t *ppos)
  338. {
  339. struct pn544_info *info = container_of(file->private_data,
  340. struct pn544_info, miscdev);
  341. struct i2c_client *client = info->i2c_dev;
  342. ssize_t len;
  343. int r;
  344. dev_dbg(&client->dev, "%s: info: %p, count %zu\n", __func__,
  345. info, count);
  346. mutex_lock(&info->mutex);
  347. if (info->state == PN544_ST_COLD) {
  348. r = -ENODEV;
  349. goto out;
  350. }
  351. /*
  352. * XXX: should we detect rset-writes and clean possible
  353. * read_irq state
  354. */
  355. if (info->state == PN544_ST_FW_READY) {
  356. size_t fw_len;
  357. if (count < PN544_FW_HEADER_SIZE) {
  358. r = -EINVAL;
  359. goto out;
  360. }
  361. len = min(count, info->buflen);
  362. if (copy_from_user(info->buf, buf, len)) {
  363. r = -EFAULT;
  364. goto out;
  365. }
  366. print_hex_dump(KERN_DEBUG, "FW write: ", DUMP_PREFIX_NONE,
  367. 16, 2, info->buf, len, false);
  368. fw_len = PN544_FW_HEADER_SIZE + (info->buf[1] << 8) +
  369. info->buf[2];
  370. if (len > fw_len) /* 1 msg at a time */
  371. len = fw_len;
  372. r = pn544_fw_write(info->i2c_dev, info->buf, len);
  373. } else {
  374. if (count < PN544_LLC_MIN_SIZE) {
  375. r = -EINVAL;
  376. goto out;
  377. }
  378. len = min(count, info->buflen);
  379. if (copy_from_user(info->buf, buf, len)) {
  380. r = -EFAULT;
  381. goto out;
  382. }
  383. print_hex_dump(KERN_DEBUG, "write: ", DUMP_PREFIX_NONE,
  384. 16, 2, info->buf, len, false);
  385. if (len > (info->buf[0] + 1)) /* 1 msg at a time */
  386. len = info->buf[0] + 1;
  387. r = pn544_i2c_write(info->i2c_dev, info->buf, len);
  388. }
  389. out:
  390. mutex_unlock(&info->mutex);
  391. return r;
  392. }
  393. static long pn544_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  394. {
  395. struct pn544_info *info = container_of(file->private_data,
  396. struct pn544_info, miscdev);
  397. struct i2c_client *client = info->i2c_dev;
  398. struct pn544_nfc_platform_data *pdata;
  399. unsigned int val;
  400. int r = 0;
  401. dev_dbg(&client->dev, "%s: info: %p, cmd: 0x%x\n", __func__, info, cmd);
  402. mutex_lock(&info->mutex);
  403. if (info->state == PN544_ST_COLD) {
  404. r = -ENODEV;
  405. goto out;
  406. }
  407. pdata = info->i2c_dev->dev.platform_data;
  408. switch (cmd) {
  409. case PN544_GET_FW_MODE:
  410. dev_dbg(&client->dev, "%s: PN544_GET_FW_MODE\n", __func__);
  411. val = (info->state == PN544_ST_FW_READY);
  412. if (copy_to_user((void __user *)arg, &val, sizeof(val))) {
  413. r = -EFAULT;
  414. goto out;
  415. }
  416. break;
  417. case PN544_SET_FW_MODE:
  418. dev_dbg(&client->dev, "%s: PN544_SET_FW_MODE\n", __func__);
  419. if (copy_from_user(&val, (void __user *)arg, sizeof(val))) {
  420. r = -EFAULT;
  421. goto out;
  422. }
  423. if (val) {
  424. if (info->state == PN544_ST_FW_READY)
  425. break;
  426. pn544_disable(info);
  427. r = pn544_enable(info, FW_MODE);
  428. if (r < 0)
  429. goto out;
  430. } else {
  431. if (info->state == PN544_ST_READY)
  432. break;
  433. pn544_disable(info);
  434. r = pn544_enable(info, HCI_MODE);
  435. if (r < 0)
  436. goto out;
  437. }
  438. file->f_pos = info->read_offset;
  439. break;
  440. case TCGETS:
  441. dev_dbg(&client->dev, "%s: TCGETS\n", __func__);
  442. r = -ENOIOCTLCMD;
  443. break;
  444. default:
  445. dev_err(&client->dev, "Unknown ioctl 0x%x\n", cmd);
  446. r = -ENOIOCTLCMD;
  447. break;
  448. }
  449. out:
  450. mutex_unlock(&info->mutex);
  451. return r;
  452. }
  453. static int pn544_open(struct inode *inode, struct file *file)
  454. {
  455. struct pn544_info *info = container_of(file->private_data,
  456. struct pn544_info, miscdev);
  457. struct i2c_client *client = info->i2c_dev;
  458. int r = 0;
  459. dev_dbg(&client->dev, "%s: info: %p, client %p\n", __func__,
  460. info, info->i2c_dev);
  461. mutex_lock(&info->mutex);
  462. /*
  463. * Only 1 at a time.
  464. * XXX: maybe user (counter) would work better
  465. */
  466. if (info->state != PN544_ST_COLD) {
  467. r = -EBUSY;
  468. goto out;
  469. }
  470. file->f_pos = info->read_offset;
  471. r = pn544_enable(info, HCI_MODE);
  472. out:
  473. mutex_unlock(&info->mutex);
  474. return r;
  475. }
  476. static int pn544_close(struct inode *inode, struct file *file)
  477. {
  478. struct pn544_info *info = container_of(file->private_data,
  479. struct pn544_info, miscdev);
  480. struct i2c_client *client = info->i2c_dev;
  481. dev_dbg(&client->dev, "%s: info: %p, client %p\n",
  482. __func__, info, info->i2c_dev);
  483. mutex_lock(&info->mutex);
  484. pn544_disable(info);
  485. mutex_unlock(&info->mutex);
  486. return 0;
  487. }
  488. static const struct file_operations pn544_fops = {
  489. .owner = THIS_MODULE,
  490. .llseek = no_llseek,
  491. .read = pn544_read,
  492. .write = pn544_write,
  493. .poll = pn544_poll,
  494. .open = pn544_open,
  495. .release = pn544_close,
  496. .unlocked_ioctl = pn544_ioctl,
  497. };
  498. #ifdef CONFIG_PM
  499. static int pn544_suspend(struct device *dev)
  500. {
  501. struct i2c_client *client = to_i2c_client(dev);
  502. struct pn544_info *info;
  503. int r = 0;
  504. dev_info(&client->dev, "***\n%s: client %p\n***\n", __func__, client);
  505. info = i2c_get_clientdata(client);
  506. dev_info(&client->dev, "%s: info: %p, client %p\n", __func__,
  507. info, client);
  508. mutex_lock(&info->mutex);
  509. switch (info->state) {
  510. case PN544_ST_FW_READY:
  511. /* Do not suspend while upgrading FW, please! */
  512. r = -EPERM;
  513. break;
  514. case PN544_ST_READY:
  515. /*
  516. * CHECK: Device should be in standby-mode. No way to check?
  517. * Allowing low power mode for the regulator is potentially
  518. * dangerous if pn544 does not go to suspension.
  519. */
  520. break;
  521. case PN544_ST_COLD:
  522. break;
  523. };
  524. mutex_unlock(&info->mutex);
  525. return r;
  526. }
  527. static int pn544_resume(struct device *dev)
  528. {
  529. struct i2c_client *client = to_i2c_client(dev);
  530. struct pn544_info *info = i2c_get_clientdata(client);
  531. int r = 0;
  532. dev_dbg(&client->dev, "%s: info: %p, client %p\n", __func__,
  533. info, client);
  534. mutex_lock(&info->mutex);
  535. switch (info->state) {
  536. case PN544_ST_READY:
  537. /*
  538. * CHECK: If regulator low power mode is allowed in
  539. * pn544_suspend, we should go back to normal mode
  540. * here.
  541. */
  542. break;
  543. case PN544_ST_COLD:
  544. break;
  545. case PN544_ST_FW_READY:
  546. break;
  547. };
  548. mutex_unlock(&info->mutex);
  549. return r;
  550. }
  551. static SIMPLE_DEV_PM_OPS(pn544_pm_ops, pn544_suspend, pn544_resume);
  552. #endif
  553. static struct device_attribute pn544_attr =
  554. __ATTR(nfc_test, S_IRUGO, pn544_test, NULL);
  555. static int __devinit pn544_probe(struct i2c_client *client,
  556. const struct i2c_device_id *id)
  557. {
  558. struct pn544_info *info;
  559. struct pn544_nfc_platform_data *pdata;
  560. int r = 0;
  561. dev_dbg(&client->dev, "%s\n", __func__);
  562. dev_dbg(&client->dev, "IRQ: %d\n", client->irq);
  563. /* private data allocation */
  564. info = kzalloc(sizeof(struct pn544_info), GFP_KERNEL);
  565. if (!info) {
  566. dev_err(&client->dev,
  567. "Cannot allocate memory for pn544_info.\n");
  568. r = -ENOMEM;
  569. goto err_info_alloc;
  570. }
  571. info->buflen = max(PN544_MSG_MAX_SIZE, PN544_MAX_I2C_TRANSFER);
  572. info->buf = kzalloc(info->buflen, GFP_KERNEL);
  573. if (!info->buf) {
  574. dev_err(&client->dev,
  575. "Cannot allocate memory for pn544_info->buf.\n");
  576. r = -ENOMEM;
  577. goto err_buf_alloc;
  578. }
  579. info->regs[0].supply = reg_vdd_io;
  580. info->regs[1].supply = reg_vbat;
  581. r = regulator_bulk_get(&client->dev, ARRAY_SIZE(info->regs),
  582. info->regs);
  583. if (r < 0)
  584. goto err_kmalloc;
  585. info->i2c_dev = client;
  586. info->state = PN544_ST_COLD;
  587. info->read_irq = PN544_NONE;
  588. mutex_init(&info->read_mutex);
  589. mutex_init(&info->mutex);
  590. init_waitqueue_head(&info->read_wait);
  591. i2c_set_clientdata(client, info);
  592. pdata = client->dev.platform_data;
  593. if (!pdata) {
  594. dev_err(&client->dev, "No platform data\n");
  595. r = -EINVAL;
  596. goto err_reg;
  597. }
  598. if (!pdata->request_resources) {
  599. dev_err(&client->dev, "request_resources() missing\n");
  600. r = -EINVAL;
  601. goto err_reg;
  602. }
  603. r = pdata->request_resources(client);
  604. if (r) {
  605. dev_err(&client->dev, "Cannot get platform resources\n");
  606. goto err_reg;
  607. }
  608. r = request_threaded_irq(client->irq, NULL, pn544_irq_thread_fn,
  609. IRQF_TRIGGER_RISING, PN544_DRIVER_NAME,
  610. info);
  611. if (r < 0) {
  612. dev_err(&client->dev, "Unable to register IRQ handler\n");
  613. goto err_res;
  614. }
  615. /* If we don't have the test we don't need the sysfs file */
  616. if (pdata->test) {
  617. r = device_create_file(&client->dev, &pn544_attr);
  618. if (r) {
  619. dev_err(&client->dev,
  620. "sysfs registration failed, error %d\n", r);
  621. goto err_irq;
  622. }
  623. }
  624. info->miscdev.minor = MISC_DYNAMIC_MINOR;
  625. info->miscdev.name = PN544_DRIVER_NAME;
  626. info->miscdev.fops = &pn544_fops;
  627. info->miscdev.parent = &client->dev;
  628. r = misc_register(&info->miscdev);
  629. if (r < 0) {
  630. dev_err(&client->dev, "Device registration failed\n");
  631. goto err_sysfs;
  632. }
  633. dev_dbg(&client->dev, "%s: info: %p, pdata %p, client %p\n",
  634. __func__, info, pdata, client);
  635. return 0;
  636. err_sysfs:
  637. if (pdata->test)
  638. device_remove_file(&client->dev, &pn544_attr);
  639. err_irq:
  640. free_irq(client->irq, info);
  641. err_res:
  642. if (pdata->free_resources)
  643. pdata->free_resources();
  644. err_reg:
  645. regulator_bulk_free(ARRAY_SIZE(info->regs), info->regs);
  646. err_kmalloc:
  647. kfree(info->buf);
  648. err_buf_alloc:
  649. kfree(info);
  650. err_info_alloc:
  651. return r;
  652. }
  653. static __devexit int pn544_remove(struct i2c_client *client)
  654. {
  655. struct pn544_info *info = i2c_get_clientdata(client);
  656. struct pn544_nfc_platform_data *pdata = client->dev.platform_data;
  657. dev_dbg(&client->dev, "%s\n", __func__);
  658. misc_deregister(&info->miscdev);
  659. if (pdata->test)
  660. device_remove_file(&client->dev, &pn544_attr);
  661. if (info->state != PN544_ST_COLD) {
  662. if (pdata->disable)
  663. pdata->disable();
  664. info->read_irq = PN544_NONE;
  665. }
  666. free_irq(client->irq, info);
  667. if (pdata->free_resources)
  668. pdata->free_resources();
  669. regulator_bulk_free(ARRAY_SIZE(info->regs), info->regs);
  670. kfree(info->buf);
  671. kfree(info);
  672. return 0;
  673. }
  674. static struct i2c_driver pn544_driver = {
  675. .driver = {
  676. .name = PN544_DRIVER_NAME,
  677. #ifdef CONFIG_PM
  678. .pm = &pn544_pm_ops,
  679. #endif
  680. },
  681. .probe = pn544_probe,
  682. .id_table = pn544_id_table,
  683. .remove = __devexit_p(pn544_remove),
  684. };
  685. static int __init pn544_init(void)
  686. {
  687. int r;
  688. pr_debug(DRIVER_DESC ": %s\n", __func__);
  689. r = i2c_add_driver(&pn544_driver);
  690. if (r) {
  691. pr_err(PN544_DRIVER_NAME ": driver registration failed\n");
  692. return r;
  693. }
  694. return 0;
  695. }
  696. static void __exit pn544_exit(void)
  697. {
  698. i2c_del_driver(&pn544_driver);
  699. pr_info(DRIVER_DESC ", Exiting.\n");
  700. }
  701. module_init(pn544_init);
  702. module_exit(pn544_exit);
  703. MODULE_LICENSE("GPL");
  704. MODULE_DESCRIPTION(DRIVER_DESC);