st21nfc.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756
  1. /*
  2. * Copyright (C) 2016 ST Microelectronics S.A.
  3. * Copyright (C) 2010 Stollmann E+V GmbH
  4. * Copyright (C) 2010 Trusted Logic S.A.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms and conditions of the GNU General Public License,
  8. * version 2, as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <linux/kernel.h>
  19. #include <linux/module.h>
  20. #include <linux/fs.h>
  21. #include <linux/version.h>
  22. #include <linux/slab.h>
  23. #include <linux/init.h>
  24. #include <linux/list.h>
  25. #include <linux/i2c.h>
  26. #include <linux/irq.h>
  27. #include <linux/jiffies.h>
  28. #include <linux/uaccess.h>
  29. #include <linux/delay.h>
  30. #include <linux/interrupt.h>
  31. #include <linux/io.h>
  32. #include <linux/platform_device.h>
  33. #include <linux/gpio.h>
  34. #include <linux/poll.h>
  35. #include <linux/miscdevice.h>
  36. #include <linux/spinlock.h>
  37. #include "st21nfc.h"
  38. #include <linux/of_gpio.h>
  39. #define MAX_BUFFER_SIZE 260
  40. #define DRIVER_VERSION "2.0.0"
  41. /* define the active state of the WAKEUP pin */
  42. #define ST21_IRQ_ACTIVE_HIGH 1
  43. #define ST21_IRQ_ACTIVE_LOW 0
  44. /* prototypes */
  45. static irqreturn_t st21nfc_dev_irq_handler(int irq, void *dev_id);
  46. /*
  47. * The platform data member 'polarity_mode' defines
  48. * how the wakeup pin is configured and handled.
  49. * it can take the following values :
  50. * IRQF_TRIGGER_RISING
  51. * IRQF_TRIGGER_FALLING
  52. * IRQF_TRIGGER_HIGH
  53. * IRQF_TRIGGER_LOW
  54. */
  55. struct st21nfc_platform {
  56. struct mutex read_mutex;
  57. struct i2c_client *client;
  58. unsigned int irq_gpio;
  59. unsigned int reset_gpio;
  60. unsigned int ena_gpio;
  61. unsigned int polarity_mode;
  62. /* either 0 (low-active) or 1 (high-active) */
  63. unsigned int active_polarity;
  64. };
  65. static bool irqIsAttached;
  66. static bool device_open; /* Is device open? */
  67. struct st21nfc_dev {
  68. wait_queue_head_t read_wq;
  69. struct miscdevice st21nfc_device;
  70. bool irq_enabled;
  71. struct st21nfc_platform platform_data;
  72. spinlock_t irq_enabled_lock;
  73. };
  74. static int st21nfc_loc_set_polaritymode(struct st21nfc_dev *st21nfc_dev,
  75. int mode)
  76. {
  77. struct i2c_client *client = st21nfc_dev->platform_data.client;
  78. unsigned int irq_type;
  79. int ret;
  80. st21nfc_dev->platform_data.polarity_mode = mode;
  81. /* setup irq_flags */
  82. switch (mode) {
  83. case IRQF_TRIGGER_RISING:
  84. irq_type = IRQ_TYPE_EDGE_RISING;
  85. st21nfc_dev->platform_data.active_polarity = 1;
  86. break;
  87. case IRQF_TRIGGER_FALLING:
  88. irq_type = IRQ_TYPE_EDGE_FALLING;
  89. st21nfc_dev->platform_data.active_polarity = 0;
  90. break;
  91. case IRQF_TRIGGER_HIGH:
  92. irq_type = IRQ_TYPE_LEVEL_HIGH;
  93. st21nfc_dev->platform_data.active_polarity = 1;
  94. break;
  95. case IRQF_TRIGGER_LOW:
  96. irq_type = IRQ_TYPE_LEVEL_LOW;
  97. st21nfc_dev->platform_data.active_polarity = 0;
  98. break;
  99. default:
  100. irq_type = IRQF_TRIGGER_FALLING;
  101. st21nfc_dev->platform_data.active_polarity = 0;
  102. break;
  103. }
  104. if (irqIsAttached) {
  105. free_irq(client->irq, st21nfc_dev);
  106. irqIsAttached = false;
  107. }
  108. ret = irq_set_irq_type(client->irq, irq_type);
  109. if (ret) {
  110. pr_err("%s : set_irq_type failed\n", __FILE__);
  111. return -ENODEV;
  112. }
  113. /* request irq. the irq is set whenever the chip has data available
  114. * for reading. it is cleared when all data has been read.
  115. */
  116. pr_debug("%s : requesting IRQ %d\n", __func__, client->irq);
  117. st21nfc_dev->irq_enabled = true;
  118. ret = request_irq(client->irq, st21nfc_dev_irq_handler,
  119. st21nfc_dev->platform_data.polarity_mode,
  120. client->name, st21nfc_dev);
  121. if (!ret)
  122. irqIsAttached = true;
  123. return ret;
  124. }
  125. static void st21nfc_disable_irq(struct st21nfc_dev *st21nfc_dev)
  126. {
  127. unsigned long flags;
  128. spin_lock_irqsave(&st21nfc_dev->irq_enabled_lock, flags);
  129. if (st21nfc_dev->irq_enabled) {
  130. disable_irq_nosync(st21nfc_dev->platform_data.client->irq);
  131. st21nfc_dev->irq_enabled = false;
  132. }
  133. spin_unlock_irqrestore(&st21nfc_dev->irq_enabled_lock, flags);
  134. }
  135. static irqreturn_t st21nfc_dev_irq_handler(int irq, void *dev_id)
  136. {
  137. struct st21nfc_dev *st21nfc_dev = dev_id;
  138. st21nfc_disable_irq(st21nfc_dev);
  139. /* Wake up waiting readers */
  140. wake_up(&st21nfc_dev->read_wq);
  141. return IRQ_HANDLED;
  142. }
  143. static ssize_t st21nfc_dev_read(struct file *filp, char __user *buf,
  144. size_t count, loff_t *offset)
  145. {
  146. struct st21nfc_dev *st21nfc_dev = container_of(filp->private_data,
  147. struct st21nfc_dev,
  148. st21nfc_device);
  149. char tmp[MAX_BUFFER_SIZE];
  150. int ret;
  151. if (count > MAX_BUFFER_SIZE)
  152. count = MAX_BUFFER_SIZE;
  153. pr_debug("%s : reading %zu bytes.\n", __func__, count);
  154. mutex_lock(&st21nfc_dev->platform_data.read_mutex);
  155. /* Read data */
  156. ret = i2c_master_recv(st21nfc_dev->platform_data.client, tmp, count);
  157. mutex_unlock(&st21nfc_dev->platform_data.read_mutex);
  158. if (ret < 0) {
  159. pr_err("%s: i2c_master_recv returned %d\n", __func__, ret);
  160. return ret;
  161. }
  162. if (ret > count) {
  163. pr_err("%s: received too many bytes from i2c (%d)\n",
  164. __func__, ret);
  165. return -EIO;
  166. }
  167. if (copy_to_user(buf, tmp, ret)) {
  168. pr_warn("%s : failed to copy to user space\n", __func__);
  169. return -EFAULT;
  170. }
  171. return ret;
  172. }
  173. static ssize_t st21nfc_dev_write(struct file *filp, const char __user *buf,
  174. size_t count, loff_t *offset)
  175. {
  176. struct st21nfc_dev *st21nfc_dev;
  177. char tmp[MAX_BUFFER_SIZE];
  178. int ret = count;
  179. st21nfc_dev = container_of(filp->private_data,
  180. struct st21nfc_dev, st21nfc_device);
  181. pr_debug("%s: st21nfc_dev ptr %p\n", __func__, st21nfc_dev);
  182. if (count > MAX_BUFFER_SIZE)
  183. count = MAX_BUFFER_SIZE;
  184. if (copy_from_user(tmp, buf, count)) {
  185. pr_err("%s : failed to copy from user space\n", __func__);
  186. return -EFAULT;
  187. }
  188. pr_debug("%s : writing %zu bytes.\n", __func__, count);
  189. /* Write data */
  190. ret = i2c_master_send(st21nfc_dev->platform_data.client, tmp, count);
  191. if (ret != count) {
  192. pr_err("%s : i2c_master_send returned %d\n", __func__, ret);
  193. ret = -EIO;
  194. }
  195. return ret;
  196. }
  197. static int st21nfc_dev_open(struct inode *inode, struct file *filp)
  198. {
  199. int ret = 0;
  200. struct st21nfc_dev *st21nfc_dev = NULL;
  201. if (device_open) {
  202. ret = -EBUSY;
  203. pr_err("%s : device already opened ret= %d\n", __func__, ret);
  204. } else {
  205. device_open = true;
  206. st21nfc_dev = container_of(filp->private_data,
  207. struct st21nfc_dev,
  208. st21nfc_device);
  209. pr_debug("%s : device_open = %d", __func__, device_open);
  210. pr_debug("%s : %d,%d ", __func__, imajor(inode), iminor(inode));
  211. pr_debug("%s: st21nfc_dev ptr %p\n", __func__, st21nfc_dev);
  212. }
  213. return ret;
  214. }
  215. static int st21nfc_release(struct inode *inode, struct file *file)
  216. {
  217. device_open = false;
  218. pr_debug("%s : device_open = %d\n", __func__, device_open);
  219. return 0;
  220. }
  221. static long st21nfc_dev_ioctl(struct file *filp, unsigned int cmd,
  222. unsigned long arg)
  223. {
  224. struct st21nfc_dev *st21nfc_dev = container_of(filp->private_data,
  225. struct st21nfc_dev,
  226. st21nfc_device);
  227. int ret = 0;
  228. switch (cmd) {
  229. case ST21NFC_SET_POLARITY_FALLING:
  230. pr_info(" ### ST21NFC_SET_POLARITY_FALLING ###");
  231. st21nfc_loc_set_polaritymode(st21nfc_dev, IRQF_TRIGGER_FALLING);
  232. break;
  233. case ST21NFC_SET_POLARITY_RISING:
  234. pr_info(" ### ST21NFC_SET_POLARITY_RISING ###");
  235. st21nfc_loc_set_polaritymode(st21nfc_dev, IRQF_TRIGGER_RISING);
  236. break;
  237. case ST21NFC_SET_POLARITY_LOW:
  238. pr_info(" ### ST21NFC_SET_POLARITY_LOW ###");
  239. st21nfc_loc_set_polaritymode(st21nfc_dev, IRQF_TRIGGER_LOW);
  240. break;
  241. case ST21NFC_SET_POLARITY_HIGH:
  242. pr_info(" ### ST21NFC_SET_POLARITY_HIGH ###");
  243. st21nfc_loc_set_polaritymode(st21nfc_dev, IRQF_TRIGGER_HIGH);
  244. break;
  245. case ST21NFC_PULSE_RESET:
  246. #ifdef ST21_USES_SINGLE_RESET
  247. pr_info("%s Pulse Request\n", __func__);
  248. if (st21nfc_dev->platform_data.reset_gpio != 0) {
  249. /* pulse low for 20 millisecs */
  250. gpio_set_value(st21nfc_dev->platform_data.reset_gpio,
  251. 0);
  252. msleep(20);
  253. gpio_set_value(st21nfc_dev->platform_data.reset_gpio,
  254. 1);
  255. pr_info("%s done Pulse Request\n", __func__);
  256. }
  257. break;
  258. #else
  259. /* Double pulse is done to exit Quick boot mode.*/
  260. pr_info("%s Double Pulse Request\n", __func__);
  261. if (st21nfc_dev->platform_data.reset_gpio != 0) {
  262. /* pulse low for 20 millisecs */
  263. gpio_set_value(st21nfc_dev->platform_data.reset_gpio,
  264. 0);
  265. msleep(20);
  266. gpio_set_value(st21nfc_dev->platform_data.reset_gpio,
  267. 1);
  268. msleep(20);
  269. /* pulse low for 20 millisecs */
  270. gpio_set_value(st21nfc_dev->platform_data.reset_gpio,
  271. 0);
  272. msleep(20);
  273. gpio_set_value(st21nfc_dev->platform_data.reset_gpio,
  274. 1);
  275. pr_info("%s done Double Pulse Request\n", __func__);
  276. }
  277. break;
  278. #endif
  279. case ST21NFC_GET_WAKEUP:
  280. /* deliver state of Wake_up_pin as return value of ioctl */
  281. ret = gpio_get_value(st21nfc_dev->platform_data.irq_gpio);
  282. /*
  283. * ret shall be equal to 1 if gpio level equals to polarity.
  284. * Warning: depending on gpio_get_value implementation,
  285. * it can returns a value different than 1 in case of high level
  286. */
  287. if (((ret == 0)
  288. && (st21nfc_dev->platform_data.active_polarity == 0))
  289. || ((ret > 0)
  290. && (st21nfc_dev->platform_data.active_polarity == 1))) {
  291. ret = 1;
  292. } else {
  293. ret = 0;
  294. }
  295. pr_debug("%s get gpio result %d\n", __func__, ret);
  296. break;
  297. case ST21NFC_GET_POLARITY:
  298. ret = st21nfc_dev->platform_data.polarity_mode;
  299. pr_debug("%s get polarity %d\n", __func__, ret);
  300. break;
  301. case ST21NFC_RECOVERY:
  302. /* For ST21NFCD usage only */
  303. pr_info("%s Recovery Request\n", __func__);
  304. if (st21nfc_dev->platform_data.reset_gpio != 0) {
  305. /* pulse low for 20 millisecs */
  306. gpio_set_value(st21nfc_dev->platform_data.reset_gpio,
  307. 0);
  308. msleep(20);
  309. /* during the reset, force IRQ OUT as DH output instead of
  310. * input in normal usage
  311. */
  312. ret = gpio_direction_output(
  313. st21nfc_dev->platform_data.irq_gpio, 1);
  314. if (ret) {
  315. pr_err("%s : gpio_direction_output failed\n",
  316. __FILE__);
  317. ret = -ENODEV;
  318. break;
  319. }
  320. gpio_set_value(st21nfc_dev->platform_data.irq_gpio, 1);
  321. msleep(20);
  322. gpio_set_value(st21nfc_dev->platform_data.reset_gpio,
  323. 1);
  324. pr_info("%s done Pulse Request\n", __func__);
  325. }
  326. msleep(20);
  327. gpio_set_value(st21nfc_dev->platform_data.irq_gpio, 0);
  328. msleep(20);
  329. gpio_set_value(st21nfc_dev->platform_data.irq_gpio, 1);
  330. msleep(20);
  331. gpio_set_value(st21nfc_dev->platform_data.irq_gpio, 0);
  332. msleep(20);
  333. pr_info("%s Recovery procedure finished\n", __func__);
  334. ret = gpio_direction_input(
  335. st21nfc_dev->platform_data.irq_gpio);
  336. if (ret) {
  337. pr_err("%s : gpio_direction_input failed\n", __FILE__);
  338. ret = -ENODEV;
  339. }
  340. break;
  341. default:
  342. pr_err("%s bad ioctl %u\n", __func__, cmd);
  343. ret = -EINVAL;
  344. break;
  345. }
  346. return ret;
  347. }
  348. static unsigned int st21nfc_poll(struct file *file, poll_table *wait)
  349. {
  350. struct st21nfc_dev *st21nfc_dev = container_of(file->private_data,
  351. struct st21nfc_dev,
  352. st21nfc_device);
  353. unsigned int mask = 0;
  354. int pinlev = 0;
  355. /* wait for Wake_up_pin == high */
  356. pr_debug("%s call poll_Wait\n", __func__);
  357. poll_wait(file, &st21nfc_dev->read_wq, wait);
  358. pinlev = gpio_get_value(st21nfc_dev->platform_data.irq_gpio);
  359. if (((pinlev == 0) && (st21nfc_dev->platform_data.active_polarity == 0))
  360. || ((pinlev > 0)
  361. && (st21nfc_dev->platform_data.active_polarity == 1))) {
  362. pr_debug("%s return ready\n", __func__);
  363. mask = POLLIN | POLLRDNORM; /* signal data avail */
  364. st21nfc_disable_irq(st21nfc_dev);
  365. } else {
  366. /* Wake_up_pin is low. Activate ISR */
  367. if (!st21nfc_dev->irq_enabled) {
  368. pr_debug("%s enable irq\n", __func__);
  369. st21nfc_dev->irq_enabled = true;
  370. enable_irq(st21nfc_dev->platform_data.client->irq);
  371. } else {
  372. pr_debug("%s irq already enabled\n", __func__);
  373. }
  374. }
  375. return mask;
  376. }
  377. static const struct file_operations st21nfc_dev_fops = {
  378. .owner = THIS_MODULE,
  379. .llseek = no_llseek,
  380. .read = st21nfc_dev_read,
  381. .write = st21nfc_dev_write,
  382. .open = st21nfc_dev_open,
  383. .poll = st21nfc_poll,
  384. .release = st21nfc_release,
  385. .unlocked_ioctl = st21nfc_dev_ioctl,
  386. #ifdef CONFIG_COMPAT
  387. .compat_ioctl = st21nfc_dev_ioctl
  388. #endif
  389. };
  390. static ssize_t st21nfc_show_i2c_addr(struct device *dev,
  391. struct device_attribute *attr, char *buf)
  392. {
  393. struct i2c_client *client = to_i2c_client(dev);
  394. if (client != NULL)
  395. return snprintf(buf, PAGE_SIZE, "0x%.2x\n", client->addr);
  396. return 0;
  397. } /* st21nfc_show_i2c_addr() */
  398. static ssize_t st21nfc_change_i2c_addr(struct device *dev,
  399. struct device_attribute *attr,
  400. const char *buf, size_t count)
  401. {
  402. struct st21nfc_dev *data = dev_get_drvdata(dev);
  403. long new_addr = 0;
  404. if (data != NULL && data->platform_data.client != NULL) {
  405. if (!kstrtol(buf, 10, &new_addr)) {
  406. mutex_lock(&data->platform_data.read_mutex);
  407. data->platform_data.client->addr = new_addr;
  408. mutex_unlock(&data->platform_data.read_mutex);
  409. return count;
  410. }
  411. return -EINVAL;
  412. }
  413. return 0;
  414. } /* st21nfc_change_i2c_addr() */
  415. static ssize_t st21nfc_version(struct device *dev,
  416. struct device_attribute *attr, char *buf)
  417. {
  418. return snprintf(buf, PAGE_SIZE, "%s\n", DRIVER_VERSION);
  419. } /* st21nfc_version */
  420. static DEVICE_ATTR(i2c_addr, 0644, st21nfc_show_i2c_addr,
  421. st21nfc_change_i2c_addr);
  422. static DEVICE_ATTR(version, 0444, st21nfc_version, NULL);
  423. static struct attribute *st21nfc_attrs[] = {
  424. &dev_attr_i2c_addr.attr,
  425. &dev_attr_version.attr,
  426. NULL,
  427. };
  428. static struct attribute_group st21nfc_attr_grp = {
  429. .attrs = st21nfc_attrs,
  430. };
  431. static int nfc_parse_dt(struct device *dev, struct st21nfc_platform_data *pdata)
  432. {
  433. #ifdef CONFIG_OF
  434. struct device_node *np = dev->of_node;
  435. np = of_find_compatible_node(NULL, NULL, "st,st21nfc");
  436. if (IS_ERR_OR_NULL(np)) {
  437. pr_err("%s: cannot find compatible node \"%s\"",
  438. __func__, "st,st21nfc");
  439. return -ENODEV;
  440. }
  441. pdata->reset_gpio = of_get_named_gpio(np, "st,reset_gpio", 0);
  442. if ((!gpio_is_valid(pdata->reset_gpio))) {
  443. pr_err("%s: fail to get reset_gpio\n", __func__);
  444. return -EINVAL;
  445. }
  446. pdata->irq_gpio = of_get_named_gpio(np, "st,irq_gpio", 0);
  447. if ((!gpio_is_valid(pdata->irq_gpio))) {
  448. pr_err("%s: fail to get irq_gpio\n", __func__);
  449. return -EINVAL;
  450. }
  451. pdata->polarity_mode = 0;
  452. pr_err("%s : get reset_gpio[%d], irq_gpio[%d], polarity_mode[%d]\n",
  453. __func__, pdata->reset_gpio, pdata->irq_gpio,
  454. pdata->polarity_mode);
  455. #endif
  456. return 0;
  457. }
  458. static int st21nfc_probe(struct i2c_client *client,
  459. const struct i2c_device_id *id)
  460. {
  461. int ret;
  462. struct st21nfc_platform_data *platform_data;
  463. struct st21nfc_dev *st21nfc_dev;
  464. dev_info(&client->dev, "st21nfc_probe\n");
  465. if (client->dev.of_node) {
  466. platform_data = devm_kzalloc(&client->dev,
  467. sizeof(struct st21nfc_platform_data), GFP_KERNEL);
  468. if (!platform_data)
  469. return -ENOMEM;
  470. dev_info(&client->dev, "%s: Parse st21nfc DTS\n", __func__);
  471. ret = nfc_parse_dt(&client->dev, platform_data);
  472. if (ret)
  473. return ret;
  474. } else {
  475. platform_data = client->dev.platform_data;
  476. pr_info("%s : No st21nfc DTS\n", __func__);
  477. }
  478. if (!platform_data) {
  479. dev_err(&client->dev, "%s: no platform data\n", __func__);
  480. return -EINVAL;
  481. }
  482. dev_dbg(&client->dev,
  483. "nfc-nci probe: %s, inside nfc-nci flags = %x\n",
  484. __func__, client->flags);
  485. if (platform_data == NULL) {
  486. dev_err(&client->dev, "nfc-nci probe: failed\n");
  487. return -ENODEV;
  488. }
  489. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  490. pr_err("%s : need I2C_FUNC_I2C\n", __func__);
  491. return -ENODEV;
  492. }
  493. client->adapter->timeout = msecs_to_jiffies(3 * 10); /* 30ms */
  494. client->adapter->retries = 0;
  495. st21nfc_dev = kzalloc(sizeof(*st21nfc_dev), GFP_KERNEL);
  496. if (st21nfc_dev == NULL) {
  497. ret = -ENOMEM;
  498. goto err_exit;
  499. }
  500. dev_dbg(&client->dev, "%s : dev_cb_addr %p\n", __func__, st21nfc_dev);
  501. /* store for later use */
  502. st21nfc_dev->platform_data.irq_gpio = platform_data->irq_gpio;
  503. st21nfc_dev->platform_data.ena_gpio = platform_data->ena_gpio;
  504. st21nfc_dev->platform_data.reset_gpio = platform_data->reset_gpio;
  505. st21nfc_dev->platform_data.polarity_mode = platform_data->polarity_mode;
  506. st21nfc_dev->platform_data.client = client;
  507. ret = gpio_request(platform_data->irq_gpio, "irq_gpio");
  508. if (ret) {
  509. pr_err("%s : gpio_request failed\n", __FILE__);
  510. ret = -ENODEV;
  511. goto err_free_buffer;
  512. }
  513. ret = gpio_direction_input(platform_data->irq_gpio);
  514. if (ret) {
  515. pr_err("%s : gpio_direction_input failed\n", __FILE__);
  516. ret = -ENODEV;
  517. goto err_free_buffer;
  518. }
  519. /* initialize irqIsAttached variable */
  520. irqIsAttached = false;
  521. /* initialize device_open variable */
  522. device_open = 0;
  523. /* handle optional RESET */
  524. if (platform_data->reset_gpio != 0) {
  525. ret = gpio_request(platform_data->reset_gpio, "reset_gpio");
  526. if (ret) {
  527. pr_err("%s : reset gpio_request failed\n", __FILE__);
  528. ret = -ENODEV;
  529. goto err_free_buffer;
  530. }
  531. ret = gpio_direction_output(platform_data->reset_gpio, 1);
  532. if (ret) {
  533. pr_err("%s : reset gpio_direction_output failed\n",
  534. __FILE__);
  535. ret = -ENODEV;
  536. goto err_free_buffer;
  537. }
  538. /* low active */
  539. gpio_set_value(st21nfc_dev->platform_data.reset_gpio, 1);
  540. }
  541. client->irq = gpio_to_irq(platform_data->irq_gpio);
  542. enable_irq_wake(client->irq);
  543. /* init mutex and queues */
  544. init_waitqueue_head(&st21nfc_dev->read_wq);
  545. mutex_init(&st21nfc_dev->platform_data.read_mutex);
  546. spin_lock_init(&st21nfc_dev->irq_enabled_lock);
  547. dev_dbg(&client->dev, "%s : debug irq_gpio = %d, client-irq = %d\n",
  548. __func__, platform_data->irq_gpio, client->irq);
  549. st21nfc_dev->st21nfc_device.minor = MISC_DYNAMIC_MINOR;
  550. st21nfc_dev->st21nfc_device.name = "st21nfc";
  551. st21nfc_dev->st21nfc_device.fops = &st21nfc_dev_fops;
  552. st21nfc_dev->st21nfc_device.parent = &client->dev;
  553. i2c_set_clientdata(client, st21nfc_dev);
  554. ret = misc_register(&st21nfc_dev->st21nfc_device);
  555. if (ret) {
  556. pr_err("%s : misc_register failed\n", __FILE__);
  557. goto err_misc_register;
  558. }
  559. if (sysfs_create_group(&client->dev.kobj, &st21nfc_attr_grp)) {
  560. pr_err("%s : sysfs_create_group failed\n", __FILE__);
  561. goto err_request_irq_failed;
  562. }
  563. st21nfc_disable_irq(st21nfc_dev);
  564. return 0;
  565. err_request_irq_failed:
  566. misc_deregister(&st21nfc_dev->st21nfc_device);
  567. err_misc_register:
  568. mutex_destroy(&st21nfc_dev->platform_data.read_mutex);
  569. err_free_buffer:
  570. kfree(st21nfc_dev);
  571. err_exit:
  572. gpio_free(platform_data->irq_gpio);
  573. if (platform_data->ena_gpio != 0)
  574. gpio_free(platform_data->ena_gpio);
  575. return ret;
  576. }
  577. static int st21nfc_remove(struct i2c_client *client)
  578. {
  579. struct st21nfc_dev *st21nfc_dev;
  580. st21nfc_dev = i2c_get_clientdata(client);
  581. free_irq(client->irq, st21nfc_dev);
  582. misc_deregister(&st21nfc_dev->st21nfc_device);
  583. mutex_destroy(&st21nfc_dev->platform_data.read_mutex);
  584. gpio_free(st21nfc_dev->platform_data.irq_gpio);
  585. if (st21nfc_dev->platform_data.ena_gpio != 0)
  586. gpio_free(st21nfc_dev->platform_data.ena_gpio);
  587. kfree(st21nfc_dev);
  588. return 0;
  589. }
  590. static const struct i2c_device_id st21nfc_id[] = {
  591. {"st21nfc", 0},
  592. {}
  593. };
  594. static const struct of_device_id st21nfc_of_match[] = {
  595. /* it's same as the compatible of nfc in dts. */
  596. { .compatible = "st,st21nfc", },
  597. {}
  598. };
  599. /* MODULE_DEVICE_TABLE(of, st21nfc_of_match); */
  600. static struct i2c_driver st21nfc_driver = {
  601. .id_table = st21nfc_id,
  602. .driver = {
  603. .name = "st21nfc",
  604. .owner = THIS_MODULE,
  605. .of_match_table = st21nfc_of_match,
  606. },
  607. .probe = st21nfc_probe,
  608. .remove = st21nfc_remove,
  609. };
  610. static const struct of_device_id st21nfc_board_of_match[] = {
  611. { .compatible = "st,st21nfc", },
  612. {}
  613. };
  614. static int st21nfc_platform_probe(struct platform_device *pdev)
  615. {
  616. pr_err("st21nfc_platform_probe pr_err\n");
  617. return 0;
  618. }
  619. static int st21nfc_platform_remove(struct platform_device *pdev)
  620. {
  621. pr_err("st21nfc_platform_remove\n");
  622. return 0;
  623. }
  624. static struct platform_driver st21nfc_platform_driver = {
  625. .probe = st21nfc_platform_probe,
  626. .remove = st21nfc_platform_remove,
  627. .driver = {
  628. .name = "st21nfc",
  629. .owner = THIS_MODULE,
  630. .of_match_table = st21nfc_board_of_match,
  631. },
  632. };
  633. /*
  634. * module load/unload record keeping
  635. */
  636. static int __init st21nfc_dev_init(void)
  637. {
  638. pr_info("%s: Loading st21nfc driver\n", __func__);
  639. /* add by wuling to fix compilation error */
  640. platform_driver_register(&st21nfc_platform_driver);
  641. return i2c_add_driver(&st21nfc_driver);
  642. }
  643. module_init(st21nfc_dev_init);
  644. static void __exit st21nfc_dev_exit(void)
  645. {
  646. pr_debug("Unloading st21nfc driver\n");
  647. i2c_del_driver(&st21nfc_driver);
  648. }
  649. module_exit(st21nfc_dev_exit);
  650. MODULE_AUTHOR("Norbert Kawulski");
  651. MODULE_DESCRIPTION("NFC ST21NFC driver");
  652. MODULE_VERSION(DRIVER_VERSION);
  653. MODULE_LICENSE("GPL");