pn544.c 19 KB

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