blob: ea4bedf7803e5d41cdee288f9bf9940c0ccaa71d [file] [log] [blame]
Gaurav Singhalc53bc292017-02-16 16:35:39 +05301/* Copyright (c) 2015-2017, The Linux Foundation. All rights reserved.
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 */
12
13#include <linux/kernel.h>
14#include <linux/module.h>
15#include <linux/fs.h>
16#include <linux/reboot.h>
17#include <linux/slab.h>
18#include <linux/irq.h>
19#include <linux/delay.h>
20#include <linux/interrupt.h>
21#include <linux/gpio.h>
22#include <linux/spinlock.h>
23#include <linux/of_gpio.h>
24#include <linux/of_device.h>
25#include <linux/uaccess.h>
26#include "nq-nci.h"
27#include <linux/clk.h>
28#ifdef CONFIG_COMPAT
29#include <linux/compat.h>
30#endif
31
32struct nqx_platform_data {
33 unsigned int irq_gpio;
34 unsigned int en_gpio;
35 unsigned int clkreq_gpio;
36 unsigned int firm_gpio;
37 unsigned int ese_gpio;
38 const char *clk_src_name;
39};
40
41static const struct of_device_id msm_match_table[] = {
42 {.compatible = "qcom,nq-nci"},
43 {}
44};
45
46MODULE_DEVICE_TABLE(of, msm_match_table);
47
48#define MAX_BUFFER_SIZE (320)
49#define WAKEUP_SRC_TIMEOUT (2000)
50#define MAX_RETRY_COUNT 3
51
52struct nqx_dev {
53 wait_queue_head_t read_wq;
54 struct mutex read_mutex;
55 struct i2c_client *client;
56 struct miscdevice nqx_device;
57 union nqx_uinfo nqx_info;
58 /* NFC GPIO variables */
59 unsigned int irq_gpio;
60 unsigned int en_gpio;
61 unsigned int firm_gpio;
62 unsigned int clkreq_gpio;
63 unsigned int ese_gpio;
64 /* NFC VEN pin state powered by Nfc */
65 bool nfc_ven_enabled;
66 /* NFC_IRQ state */
67 bool irq_enabled;
68 /* NFC_IRQ wake-up state */
69 bool irq_wake_up;
70 spinlock_t irq_enabled_lock;
71 unsigned int count_irq;
72 /* Initial CORE RESET notification */
73 unsigned int core_reset_ntf;
74 /* CLK control */
75 bool clk_run;
76 struct clk *s_clk;
77 /* read buffer*/
78 size_t kbuflen;
79 u8 *kbuf;
80 struct nqx_platform_data *pdata;
81};
82
83static int nfcc_reboot(struct notifier_block *notifier, unsigned long val,
84 void *v);
85/*clock enable function*/
86static int nqx_clock_select(struct nqx_dev *nqx_dev);
87/*clock disable function*/
88static int nqx_clock_deselect(struct nqx_dev *nqx_dev);
89static struct notifier_block nfcc_notifier = {
90 .notifier_call = nfcc_reboot,
91 .next = NULL,
92 .priority = 0
93};
94
95unsigned int disable_ctrl;
96
97static void nqx_init_stat(struct nqx_dev *nqx_dev)
98{
99 nqx_dev->count_irq = 0;
100}
101
102static void nqx_disable_irq(struct nqx_dev *nqx_dev)
103{
104 unsigned long flags;
105
106 spin_lock_irqsave(&nqx_dev->irq_enabled_lock, flags);
107 if (nqx_dev->irq_enabled) {
108 disable_irq_nosync(nqx_dev->client->irq);
109 nqx_dev->irq_enabled = false;
110 }
111 spin_unlock_irqrestore(&nqx_dev->irq_enabled_lock, flags);
112}
113
114/**
115 * nqx_enable_irq()
116 *
117 * Check if interrupt is enabled or not
118 * and enable interrupt
119 *
120 * Return: void
121 */
122static void nqx_enable_irq(struct nqx_dev *nqx_dev)
123{
124 unsigned long flags;
125
126 spin_lock_irqsave(&nqx_dev->irq_enabled_lock, flags);
127 if (!nqx_dev->irq_enabled) {
128 nqx_dev->irq_enabled = true;
129 enable_irq(nqx_dev->client->irq);
130 }
131 spin_unlock_irqrestore(&nqx_dev->irq_enabled_lock, flags);
132}
133
134static irqreturn_t nqx_dev_irq_handler(int irq, void *dev_id)
135{
136 struct nqx_dev *nqx_dev = dev_id;
137 unsigned long flags;
138
139 if (device_may_wakeup(&nqx_dev->client->dev))
140 pm_wakeup_event(&nqx_dev->client->dev, WAKEUP_SRC_TIMEOUT);
141
142 nqx_disable_irq(nqx_dev);
143 spin_lock_irqsave(&nqx_dev->irq_enabled_lock, flags);
144 nqx_dev->count_irq++;
145 spin_unlock_irqrestore(&nqx_dev->irq_enabled_lock, flags);
146 wake_up(&nqx_dev->read_wq);
147
148 return IRQ_HANDLED;
149}
150
151static ssize_t nfc_read(struct file *filp, char __user *buf,
152 size_t count, loff_t *offset)
153{
154 struct nqx_dev *nqx_dev = filp->private_data;
155 unsigned char *tmp = NULL;
156 int ret;
157 int irq_gpio_val = 0;
158
159 if (!nqx_dev) {
160 ret = -ENODEV;
161 goto out;
162 }
163
164 if (count > nqx_dev->kbuflen)
165 count = nqx_dev->kbuflen;
166
167 dev_dbg(&nqx_dev->client->dev, "%s : reading %zu bytes.\n",
168 __func__, count);
169
170 mutex_lock(&nqx_dev->read_mutex);
171
172 irq_gpio_val = gpio_get_value(nqx_dev->irq_gpio);
173 if (irq_gpio_val == 0) {
174 if (filp->f_flags & O_NONBLOCK) {
175 dev_err(&nqx_dev->client->dev,
176 ":f_falg has O_NONBLOCK. EAGAIN\n");
177 ret = -EAGAIN;
178 goto err;
179 }
180 while (1) {
181 ret = 0;
182 if (!nqx_dev->irq_enabled) {
183 nqx_dev->irq_enabled = true;
184 enable_irq(nqx_dev->client->irq);
185 }
186 if (!gpio_get_value(nqx_dev->irq_gpio)) {
187 ret = wait_event_interruptible(nqx_dev->read_wq,
188 !nqx_dev->irq_enabled);
189 }
190 if (ret)
191 goto err;
192 nqx_disable_irq(nqx_dev);
193
194 if (gpio_get_value(nqx_dev->irq_gpio))
195 break;
196 dev_err_ratelimited(&nqx_dev->client->dev,
197 "gpio is low, no need to read data\n");
198 }
199 }
200
201 tmp = nqx_dev->kbuf;
202 if (!tmp) {
203 dev_err(&nqx_dev->client->dev,
204 "%s: device doesn't exist anymore\n", __func__);
205 ret = -ENODEV;
206 goto err;
207 }
208 memset(tmp, 0x00, count);
209
210 /* Read data */
211 ret = i2c_master_recv(nqx_dev->client, tmp, count);
212 if (ret < 0) {
213 dev_err(&nqx_dev->client->dev,
214 "%s: i2c_master_recv returned %d\n", __func__, ret);
215 goto err;
216 }
217 if (ret > count) {
218 dev_err(&nqx_dev->client->dev,
219 "%s: received too many bytes from i2c (%d)\n",
220 __func__, ret);
221 ret = -EIO;
222 goto err;
223 }
224#ifdef NFC_KERNEL_BU
225 dev_dbg(&nqx_dev->client->dev, "%s : NfcNciRx %x %x %x\n",
226 __func__, tmp[0], tmp[1], tmp[2]);
227#endif
228 if (copy_to_user(buf, tmp, ret)) {
229 dev_warn(&nqx_dev->client->dev,
230 "%s : failed to copy to user space\n", __func__);
231 ret = -EFAULT;
232 goto err;
233 }
234 mutex_unlock(&nqx_dev->read_mutex);
235 return ret;
236
237err:
238 mutex_unlock(&nqx_dev->read_mutex);
239out:
240 return ret;
241}
242
243static ssize_t nfc_write(struct file *filp, const char __user *buf,
244 size_t count, loff_t *offset)
245{
246 struct nqx_dev *nqx_dev = filp->private_data;
247 char *tmp = NULL;
248 int ret = 0;
249
250 if (!nqx_dev) {
251 ret = -ENODEV;
252 goto out;
253 }
254 if (count > nqx_dev->kbuflen) {
255 dev_err(&nqx_dev->client->dev, "%s: out of memory\n",
256 __func__);
257 ret = -ENOMEM;
258 goto out;
259 }
260
261 tmp = memdup_user(buf, count);
262 if (IS_ERR(tmp)) {
263 dev_err(&nqx_dev->client->dev, "%s: memdup_user failed\n",
264 __func__);
265 ret = PTR_ERR(tmp);
266 goto out;
267 }
268
269 ret = i2c_master_send(nqx_dev->client, tmp, count);
270 if (ret != count) {
271 dev_err(&nqx_dev->client->dev,
272 "%s: failed to write %d\n", __func__, ret);
273 ret = -EIO;
274 goto out_free;
275 }
276#ifdef NFC_KERNEL_BU
277 dev_dbg(&nqx_dev->client->dev,
278 "%s : i2c-%d: NfcNciTx %x %x %x\n",
279 __func__, iminor(file_inode(filp)),
280 tmp[0], tmp[1], tmp[2]);
281#endif
282 usleep_range(1000, 1100);
283out_free:
284 kfree(tmp);
285out:
286 return ret;
287}
288
289/**
290 * nqx_standby_write()
291 * @buf: pointer to data buffer
292 * @len: # of bytes need to transfer
293 *
294 * write data buffer over I2C and retry
295 * if NFCC is in stand by mode
296 *
297 * Return: # of bytes written or -ve value in case of error
298 */
299static int nqx_standby_write(struct nqx_dev *nqx_dev,
300 const unsigned char *buf, size_t len)
301{
302 int ret = -EINVAL;
303 int retry_cnt;
304
305 for (retry_cnt = 1; retry_cnt <= MAX_RETRY_COUNT; retry_cnt++) {
306 ret = i2c_master_send(nqx_dev->client, buf, len);
307 if (ret < 0) {
308 dev_err(&nqx_dev->client->dev,
309 "%s: write failed, Maybe in Standby Mode - Retry(%d)\n",
310 __func__, retry_cnt);
311 usleep_range(1000, 1100);
312 } else if (ret == len)
313 break;
314 }
315 return ret;
316}
317
318/*
319 * Power management of the eSE
320 * NFC & eSE ON : NFC_EN high and eSE_pwr_req high.
321 * NFC OFF & eSE ON : NFC_EN high and eSE_pwr_req high.
322 * NFC OFF & eSE OFF : NFC_EN low and eSE_pwr_req low.
323 */
324static int nqx_ese_pwr(struct nqx_dev *nqx_dev, unsigned long int arg)
325{
326 int r = -1;
327 const unsigned char svdd_off_cmd_warn[] = {0x2F, 0x31, 0x01, 0x01};
328 const unsigned char svdd_off_cmd_done[] = {0x2F, 0x31, 0x01, 0x00};
329
330 if (!gpio_is_valid(nqx_dev->ese_gpio)) {
331 dev_err(&nqx_dev->client->dev,
332 "%s: ese_gpio is not valid\n", __func__);
333 return -EINVAL;
334 }
335
336 if (arg == 0) {
337 /*
338 * We want to power on the eSE and to do so we need the
339 * eSE_pwr_req pin and the NFC_EN pin to be high
340 */
341 if (gpio_get_value(nqx_dev->ese_gpio)) {
342 dev_dbg(&nqx_dev->client->dev, "ese_gpio is already high\n");
343 r = 0;
344 } else {
345 /**
346 * Let's store the NFC_EN pin state
347 * only if the eSE is not yet on
348 */
349 nqx_dev->nfc_ven_enabled =
350 gpio_get_value(nqx_dev->en_gpio);
351 if (!nqx_dev->nfc_ven_enabled) {
352 gpio_set_value(nqx_dev->en_gpio, 1);
353 /* hardware dependent delay */
354 usleep_range(1000, 1100);
355 }
356 gpio_set_value(nqx_dev->ese_gpio, 1);
Gaurav Singhalcfb64802017-08-30 17:03:58 +0530357 usleep_range(1000, 1100);
Gaurav Singhalc53bc292017-02-16 16:35:39 +0530358 if (gpio_get_value(nqx_dev->ese_gpio)) {
359 dev_dbg(&nqx_dev->client->dev, "ese_gpio is enabled\n");
360 r = 0;
361 }
362 }
363 } else if (arg == 1) {
364 if (nqx_dev->nfc_ven_enabled &&
365 ((nqx_dev->nqx_info.info.chip_type == NFCC_NQ_220) ||
366 (nqx_dev->nqx_info.info.chip_type == NFCC_PN66T))) {
367 /**
368 * Let's inform the CLF we're
369 * powering off the eSE
370 */
371 r = nqx_standby_write(nqx_dev, svdd_off_cmd_warn,
372 sizeof(svdd_off_cmd_warn));
373 if (r < 0) {
374 dev_err(&nqx_dev->client->dev,
375 "%s: write failed after max retry\n",
376 __func__);
377 return -ENXIO;
378 }
379 dev_dbg(&nqx_dev->client->dev,
380 "%s: svdd_off_cmd_warn sent\n", __func__);
381
382 /* let's power down the eSE */
383 gpio_set_value(nqx_dev->ese_gpio, 0);
384 dev_dbg(&nqx_dev->client->dev,
385 "%s: nqx_dev->ese_gpio set to 0\n", __func__);
386
387 /**
388 * Time needed for the SVDD capacitor
389 * to get discharged
390 */
391 usleep_range(8000, 8100);
392
393 /* Let's inform the CLF the eSE is now off */
394 r = nqx_standby_write(nqx_dev, svdd_off_cmd_done,
395 sizeof(svdd_off_cmd_done));
396 if (r < 0) {
397 dev_err(&nqx_dev->client->dev,
398 "%s: write failed after max retry\n",
399 __func__);
400 return -ENXIO;
401 }
402 dev_dbg(&nqx_dev->client->dev,
403 "%s: svdd_off_cmd_done sent\n", __func__);
404 } else {
405 /**
406 * In case the NFC is off,
407 * there's no need to send the i2c commands
408 */
409 gpio_set_value(nqx_dev->ese_gpio, 0);
Gaurav Singhalcfb64802017-08-30 17:03:58 +0530410 usleep_range(1000, 1100);
Gaurav Singhalc53bc292017-02-16 16:35:39 +0530411 }
412
413 if (!gpio_get_value(nqx_dev->ese_gpio)) {
414 dev_dbg(&nqx_dev->client->dev, "ese_gpio is disabled\n");
415 r = 0;
416 }
417
418 if (!nqx_dev->nfc_ven_enabled) {
419 /* hardware dependent delay */
420 usleep_range(1000, 1100);
421 dev_dbg(&nqx_dev->client->dev, "disabling en_gpio\n");
422 gpio_set_value(nqx_dev->en_gpio, 0);
423 }
424 } else if (arg == 3) {
425 r = gpio_get_value(nqx_dev->ese_gpio);
426 }
427 return r;
428}
429
430static int nfc_open(struct inode *inode, struct file *filp)
431{
432 int ret = 0;
433 struct nqx_dev *nqx_dev = container_of(filp->private_data,
434 struct nqx_dev, nqx_device);
435
436 filp->private_data = nqx_dev;
437 nqx_init_stat(nqx_dev);
438
439 dev_dbg(&nqx_dev->client->dev,
440 "%s: %d,%d\n", __func__, imajor(inode), iminor(inode));
441 return ret;
442}
443
444/*
445 * nfc_ioctl_power_states() - power control
446 * @filp: pointer to the file descriptor
447 * @arg: mode that we want to move to
448 *
449 * Device power control. Depending on the arg value, device moves to
450 * different states
451 * (arg = 0): NFC_ENABLE GPIO = 0, FW_DL GPIO = 0
452 * (arg = 1): NFC_ENABLE GPIO = 1, FW_DL GPIO = 0
453 * (arg = 2): FW_DL GPIO = 1
454 *
455 * Return: -ENOIOCTLCMD if arg is not supported, 0 in any other case
456 */
457int nfc_ioctl_power_states(struct file *filp, unsigned long arg)
458{
459 int r = 0;
460 struct nqx_dev *nqx_dev = filp->private_data;
461
462 if (arg == 0) {
463 /*
464 * We are attempting a hardware reset so let us disable
465 * interrupts to avoid spurious notifications to upper
466 * layers.
467 */
468 nqx_disable_irq(nqx_dev);
469 dev_dbg(&nqx_dev->client->dev,
470 "gpio_set_value disable: %s: info: %p\n",
471 __func__, nqx_dev);
472 if (gpio_is_valid(nqx_dev->firm_gpio))
473 gpio_set_value(nqx_dev->firm_gpio, 0);
474
475 if (gpio_is_valid(nqx_dev->ese_gpio)) {
476 if (!gpio_get_value(nqx_dev->ese_gpio)) {
477 dev_dbg(&nqx_dev->client->dev, "disabling en_gpio\n");
478 gpio_set_value(nqx_dev->en_gpio, 0);
479 } else {
480 dev_dbg(&nqx_dev->client->dev, "keeping en_gpio high\n");
481 }
482 } else {
483 dev_dbg(&nqx_dev->client->dev, "ese_gpio invalid, set en_gpio to low\n");
484 gpio_set_value(nqx_dev->en_gpio, 0);
485 }
486 r = nqx_clock_deselect(nqx_dev);
487 if (r < 0)
488 dev_err(&nqx_dev->client->dev, "unable to disable clock\n");
489 nqx_dev->nfc_ven_enabled = false;
490 /* hardware dependent delay */
491 msleep(100);
492 } else if (arg == 1) {
493 nqx_enable_irq(nqx_dev);
494 dev_dbg(&nqx_dev->client->dev,
495 "gpio_set_value enable: %s: info: %p\n",
496 __func__, nqx_dev);
497 if (gpio_is_valid(nqx_dev->firm_gpio))
498 gpio_set_value(nqx_dev->firm_gpio, 0);
499 gpio_set_value(nqx_dev->en_gpio, 1);
500 r = nqx_clock_select(nqx_dev);
501 if (r < 0)
502 dev_err(&nqx_dev->client->dev, "unable to enable clock\n");
503 nqx_dev->nfc_ven_enabled = true;
504 msleep(20);
505 } else if (arg == 2) {
506 /*
507 * We are switching to Dowload Mode, toggle the enable pin
508 * in order to set the NFCC in the new mode
509 */
510 if (gpio_is_valid(nqx_dev->ese_gpio)) {
511 if (gpio_get_value(nqx_dev->ese_gpio)) {
512 dev_err(&nqx_dev->client->dev,
513 "FW download forbidden while ese is on\n");
514 return -EBUSY; /* Device or resource busy */
515 }
516 }
517 gpio_set_value(nqx_dev->en_gpio, 1);
518 msleep(20);
519 if (gpio_is_valid(nqx_dev->firm_gpio))
520 gpio_set_value(nqx_dev->firm_gpio, 1);
521 msleep(20);
522 gpio_set_value(nqx_dev->en_gpio, 0);
523 msleep(100);
524 gpio_set_value(nqx_dev->en_gpio, 1);
525 msleep(20);
526 } else {
527 r = -ENOIOCTLCMD;
528 }
529
530 return r;
531}
532
533#ifdef CONFIG_COMPAT
534static long nfc_compat_ioctl(struct file *pfile, unsigned int cmd,
535 unsigned long arg)
536{
537 long r = 0;
538
539 arg = (compat_u64)arg;
540 switch (cmd) {
541 case NFC_SET_PWR:
542 nfc_ioctl_power_states(pfile, arg);
543 break;
544 case ESE_SET_PWR:
545 nqx_ese_pwr(pfile->private_data, arg);
546 break;
547 case ESE_GET_PWR:
548 nqx_ese_pwr(pfile->private_data, 3);
549 break;
550 case SET_RX_BLOCK:
551 break;
552 case SET_EMULATOR_TEST_POINT:
553 break;
554 default:
555 r = -ENOTTY;
556 }
557 return r;
558}
559#endif
560
561/*
562 * nfc_ioctl_core_reset_ntf()
563 * @filp: pointer to the file descriptor
564 *
565 * Allows callers to determine if a CORE_RESET_NTF has arrived
566 *
567 * Return: the value of variable core_reset_ntf
568 */
569int nfc_ioctl_core_reset_ntf(struct file *filp)
570{
571 struct nqx_dev *nqx_dev = filp->private_data;
572
573 dev_dbg(&nqx_dev->client->dev, "%s: returning = %d\n", __func__,
574 nqx_dev->core_reset_ntf);
575 return nqx_dev->core_reset_ntf;
576}
577
578/*
579 * Inside nfc_ioctl_nfcc_info
580 *
581 * @brief nfc_ioctl_nfcc_info
582 *
583 * Check the NQ Chipset and firmware version details
584 */
585unsigned int nfc_ioctl_nfcc_info(struct file *filp, unsigned long arg)
586{
587 unsigned int r = 0;
588 struct nqx_dev *nqx_dev = filp->private_data;
589
590 r = nqx_dev->nqx_info.i;
591 dev_dbg(&nqx_dev->client->dev,
592 "nqx nfc : nfc_ioctl_nfcc_info r = %d\n", r);
593
594 return r;
595}
596
597static long nfc_ioctl(struct file *pfile, unsigned int cmd,
598 unsigned long arg)
599{
600 int r = 0;
601
602 switch (cmd) {
603 case NFC_SET_PWR:
604 r = nfc_ioctl_power_states(pfile, arg);
605 break;
606 case ESE_SET_PWR:
607 r = nqx_ese_pwr(pfile->private_data, arg);
608 break;
609 case ESE_GET_PWR:
610 r = nqx_ese_pwr(pfile->private_data, 3);
611 break;
612 case SET_RX_BLOCK:
613 break;
614 case SET_EMULATOR_TEST_POINT:
615 break;
616 case NFCC_INITIAL_CORE_RESET_NTF:
617 r = nfc_ioctl_core_reset_ntf(pfile);
618 break;
619 case NFCC_GET_INFO:
620 r = nfc_ioctl_nfcc_info(pfile, arg);
621 break;
622 default:
623 r = -ENOIOCTLCMD;
624 }
625 return r;
626}
627
628static const struct file_operations nfc_dev_fops = {
629 .owner = THIS_MODULE,
630 .llseek = no_llseek,
631 .read = nfc_read,
632 .write = nfc_write,
633 .open = nfc_open,
634 .unlocked_ioctl = nfc_ioctl,
635#ifdef CONFIG_COMPAT
636 .compat_ioctl = nfc_compat_ioctl
637#endif
638};
639
640/* Check for availability of NQ_ NFC controller hardware */
641static int nfcc_hw_check(struct i2c_client *client, struct nqx_dev *nqx_dev)
642{
643 int ret = 0;
644
645 unsigned char raw_nci_reset_cmd[] = {0x20, 0x00, 0x01, 0x00};
646 unsigned char raw_nci_init_cmd[] = {0x20, 0x01, 0x00};
647 unsigned char nci_init_rsp[28];
648 unsigned char nci_reset_rsp[6];
649 unsigned char init_rsp_len = 0;
650 unsigned int enable_gpio = nqx_dev->en_gpio;
651 /* making sure that the NFCC starts in a clean state. */
652 gpio_set_value(enable_gpio, 0);/* ULPM: Disable */
653 /* hardware dependent delay */
654 msleep(20);
655 gpio_set_value(enable_gpio, 1);/* HPD : Enable*/
656 /* hardware dependent delay */
657 msleep(20);
658
659 /* send NCI CORE RESET CMD with Keep Config parameters */
660 ret = i2c_master_send(client, raw_nci_reset_cmd,
661 sizeof(raw_nci_reset_cmd));
662 if (ret < 0) {
663 dev_err(&client->dev,
664 "%s: - i2c_master_send Error\n", __func__);
665 goto err_nfcc_hw_check;
666 }
667 /* hardware dependent delay */
668 msleep(30);
669
670 /* Read Response of RESET command */
671 ret = i2c_master_recv(client, nci_reset_rsp,
672 sizeof(nci_reset_rsp));
673 dev_err(&client->dev,
674 "%s: - nq - reset cmd answer : NfcNciRx %x %x %x\n",
675 __func__, nci_reset_rsp[0],
676 nci_reset_rsp[1], nci_reset_rsp[2]);
677 if (ret < 0) {
678 dev_err(&client->dev,
679 "%s: - i2c_master_recv Error\n", __func__);
680 goto err_nfcc_hw_check;
681 }
682 ret = i2c_master_send(client, raw_nci_init_cmd,
683 sizeof(raw_nci_init_cmd));
684 if (ret < 0) {
685 dev_err(&client->dev,
686 "%s: - i2c_master_send Error\n", __func__);
687 goto err_nfcc_hw_check;
688 }
689 /* hardware dependent delay */
690 msleep(30);
691 /* Read Response of INIT command */
692 ret = i2c_master_recv(client, nci_init_rsp,
693 sizeof(nci_init_rsp));
694 if (ret < 0) {
695 dev_err(&client->dev,
696 "%s: - i2c_master_recv Error\n", __func__);
697 goto err_nfcc_hw_check;
698 }
699 init_rsp_len = 2 + nci_init_rsp[2]; /*payload + len*/
700 if (init_rsp_len > PAYLOAD_HEADER_LENGTH) {
701 nqx_dev->nqx_info.info.chip_type =
702 nci_init_rsp[init_rsp_len - 3];
703 nqx_dev->nqx_info.info.rom_version =
704 nci_init_rsp[init_rsp_len - 2];
705 nqx_dev->nqx_info.info.fw_major =
706 nci_init_rsp[init_rsp_len - 1];
707 nqx_dev->nqx_info.info.fw_minor =
708 nci_init_rsp[init_rsp_len];
709 }
710 dev_dbg(&nqx_dev->client->dev, "NQ NFCC chip_type = %x\n",
711 nqx_dev->nqx_info.info.chip_type);
712 dev_dbg(&nqx_dev->client->dev, "NQ fw version = %x.%x.%x\n",
713 nqx_dev->nqx_info.info.rom_version,
714 nqx_dev->nqx_info.info.fw_major,
715 nqx_dev->nqx_info.info.fw_minor);
716
717 switch (nqx_dev->nqx_info.info.chip_type) {
718 case NFCC_NQ_210:
719 dev_dbg(&client->dev,
720 "%s: ## NFCC == NQ210 ##\n", __func__);
721 break;
722 case NFCC_NQ_220:
723 dev_dbg(&client->dev,
724 "%s: ## NFCC == NQ220 ##\n", __func__);
725 break;
726 case NFCC_NQ_310:
727 dev_dbg(&client->dev,
728 "%s: ## NFCC == NQ310 ##\n", __func__);
729 break;
730 case NFCC_NQ_330:
731 dev_dbg(&client->dev,
732 "%s: ## NFCC == NQ330 ##\n", __func__);
733 break;
734 case NFCC_PN66T:
735 dev_dbg(&client->dev,
736 "%s: ## NFCC == PN66T ##\n", __func__);
737 break;
738 default:
739 dev_err(&client->dev,
740 "%s: - NFCC HW not Supported\n", __func__);
741 break;
742 }
743
744 /*Disable NFC by default to save power on boot*/
745 gpio_set_value(enable_gpio, 0);/* ULPM: Disable */
746 ret = 0;
747 goto done;
748
749err_nfcc_hw_check:
750 ret = -ENXIO;
751 dev_err(&client->dev,
752 "%s: - NFCC HW not available\n", __func__);
753done:
754 return ret;
755}
756
757/*
758 * Routine to enable clock.
759 * this routine can be extended to select from multiple
760 * sources based on clk_src_name.
761 */
762static int nqx_clock_select(struct nqx_dev *nqx_dev)
763{
764 int r = 0;
765
766 nqx_dev->s_clk = clk_get(&nqx_dev->client->dev, "ref_clk");
767
768 if (nqx_dev->s_clk == NULL)
769 goto err_clk;
770
771 if (nqx_dev->clk_run == false)
772 r = clk_prepare_enable(nqx_dev->s_clk);
773
774 if (r)
775 goto err_clk;
776
777 nqx_dev->clk_run = true;
778
779 return r;
780
781err_clk:
782 r = -1;
783 return r;
784}
785
786/*
787 * Routine to disable clocks
788 */
789static int nqx_clock_deselect(struct nqx_dev *nqx_dev)
790{
791 int r = -1;
792
793 if (nqx_dev->s_clk != NULL) {
794 if (nqx_dev->clk_run == true) {
795 clk_disable_unprepare(nqx_dev->s_clk);
796 nqx_dev->clk_run = false;
797 }
798 return 0;
799 }
800 return r;
801}
802
803static int nfc_parse_dt(struct device *dev, struct nqx_platform_data *pdata)
804{
805 int r = 0;
806 struct device_node *np = dev->of_node;
807
808 pdata->en_gpio = of_get_named_gpio(np, "qcom,nq-ven", 0);
809 if ((!gpio_is_valid(pdata->en_gpio)))
810 return -EINVAL;
811 disable_ctrl = pdata->en_gpio;
812
813 pdata->irq_gpio = of_get_named_gpio(np, "qcom,nq-irq", 0);
814 if ((!gpio_is_valid(pdata->irq_gpio)))
815 return -EINVAL;
816
817 pdata->firm_gpio = of_get_named_gpio(np, "qcom,nq-firm", 0);
818 if (!gpio_is_valid(pdata->firm_gpio)) {
819 dev_warn(dev,
820 "FIRM GPIO <OPTIONAL> error getting from OF node\n");
821 pdata->firm_gpio = -EINVAL;
822 }
823
824 pdata->ese_gpio = of_get_named_gpio(np, "qcom,nq-esepwr", 0);
825 if (!gpio_is_valid(pdata->ese_gpio)) {
826 dev_warn(dev,
827 "ese GPIO <OPTIONAL> error getting from OF node\n");
828 pdata->ese_gpio = -EINVAL;
829 }
830
831 r = of_property_read_string(np, "qcom,clk-src", &pdata->clk_src_name);
832
833 pdata->clkreq_gpio = of_get_named_gpio(np, "qcom,nq-clkreq", 0);
834
835 if (r)
836 return -EINVAL;
837 return r;
838}
839
840static inline int gpio_input_init(const struct device * const dev,
841 const int gpio, const char * const gpio_name)
842{
843 int r = gpio_request(gpio, gpio_name);
844
845 if (r) {
846 dev_err(dev, "unable to request gpio [%d]\n", gpio);
847 return r;
848 }
849
850 r = gpio_direction_input(gpio);
851 if (r)
852 dev_err(dev, "unable to set direction for gpio [%d]\n", gpio);
853
854 return r;
855}
856
857static int nqx_probe(struct i2c_client *client,
858 const struct i2c_device_id *id)
859{
860 int r = 0;
861 int irqn = 0;
862 struct nqx_platform_data *platform_data;
863 struct nqx_dev *nqx_dev;
864
865 dev_dbg(&client->dev, "%s: enter\n", __func__);
866 if (client->dev.of_node) {
867 platform_data = devm_kzalloc(&client->dev,
868 sizeof(struct nqx_platform_data), GFP_KERNEL);
869 if (!platform_data) {
870 r = -ENOMEM;
871 goto err_platform_data;
872 }
873 r = nfc_parse_dt(&client->dev, platform_data);
874 if (r)
875 goto err_free_data;
876 } else
877 platform_data = client->dev.platform_data;
878
879 dev_dbg(&client->dev,
880 "%s, inside nfc-nci flags = %x\n",
881 __func__, client->flags);
882
883 if (platform_data == NULL) {
884 dev_err(&client->dev, "%s: failed\n", __func__);
885 r = -ENODEV;
886 goto err_platform_data;
887 }
888 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
889 dev_err(&client->dev, "%s: need I2C_FUNC_I2C\n", __func__);
890 r = -ENODEV;
891 goto err_free_data;
892 }
893 nqx_dev = kzalloc(sizeof(*nqx_dev), GFP_KERNEL);
894 if (nqx_dev == NULL) {
895 r = -ENOMEM;
896 goto err_free_data;
897 }
898 nqx_dev->client = client;
899 nqx_dev->kbuflen = MAX_BUFFER_SIZE;
900 nqx_dev->kbuf = kzalloc(MAX_BUFFER_SIZE, GFP_KERNEL);
901 if (!nqx_dev->kbuf) {
902 dev_err(&client->dev,
903 "failed to allocate memory for nqx_dev->kbuf\n");
904 r = -ENOMEM;
905 goto err_free_dev;
906 }
907
908 if (gpio_is_valid(platform_data->en_gpio)) {
909 r = gpio_request(platform_data->en_gpio, "nfc_reset_gpio");
910 if (r) {
911 dev_err(&client->dev,
912 "%s: unable to request nfc reset gpio [%d]\n",
913 __func__,
914 platform_data->en_gpio);
915 goto err_mem;
916 }
917 r = gpio_direction_output(platform_data->en_gpio, 0);
918 if (r) {
919 dev_err(&client->dev,
920 "%s: unable to set direction for nfc reset gpio [%d]\n",
921 __func__,
922 platform_data->en_gpio);
923 goto err_en_gpio;
924 }
925 } else {
926 dev_err(&client->dev,
927 "%s: nfc reset gpio not provided\n", __func__);
928 goto err_mem;
929 }
930
931 if (gpio_is_valid(platform_data->irq_gpio)) {
932 r = gpio_request(platform_data->irq_gpio, "nfc_irq_gpio");
933 if (r) {
934 dev_err(&client->dev, "%s: unable to request nfc irq gpio [%d]\n",
935 __func__, platform_data->irq_gpio);
936 goto err_en_gpio;
937 }
938 r = gpio_direction_input(platform_data->irq_gpio);
939 if (r) {
940 dev_err(&client->dev,
941 "%s: unable to set direction for nfc irq gpio [%d]\n",
942 __func__,
943 platform_data->irq_gpio);
944 goto err_irq_gpio;
945 }
946 irqn = gpio_to_irq(platform_data->irq_gpio);
947 if (irqn < 0) {
948 r = irqn;
949 goto err_irq_gpio;
950 }
951 client->irq = irqn;
952 } else {
953 dev_err(&client->dev, "%s: irq gpio not provided\n", __func__);
954 goto err_en_gpio;
955 }
956 if (gpio_is_valid(platform_data->firm_gpio)) {
957 r = gpio_request(platform_data->firm_gpio,
958 "nfc_firm_gpio");
959 if (r) {
960 dev_err(&client->dev,
961 "%s: unable to request nfc firmware gpio [%d]\n",
962 __func__, platform_data->firm_gpio);
963 goto err_irq_gpio;
964 }
965 r = gpio_direction_output(platform_data->firm_gpio, 0);
966 if (r) {
967 dev_err(&client->dev,
968 "%s: cannot set direction for nfc firmware gpio [%d]\n",
969 __func__, platform_data->firm_gpio);
970 goto err_firm_gpio;
971 }
972 } else {
973 dev_err(&client->dev,
974 "%s: firm gpio not provided\n", __func__);
975 goto err_irq_gpio;
976 }
977 if (gpio_is_valid(platform_data->ese_gpio)) {
978 r = gpio_request(platform_data->ese_gpio,
979 "nfc-ese_pwr");
980 if (r) {
981 nqx_dev->ese_gpio = -EINVAL;
982 dev_err(&client->dev,
983 "%s: unable to request nfc ese gpio [%d]\n",
984 __func__, platform_data->ese_gpio);
985 /* ese gpio optional so we should continue */
986 } else {
987 nqx_dev->ese_gpio = platform_data->ese_gpio;
988 r = gpio_direction_output(platform_data->ese_gpio, 0);
989 if (r) {
990 /*
991 * free ese gpio and set invalid
992 * to avoid further use
993 */
994 gpio_free(platform_data->ese_gpio);
995 nqx_dev->ese_gpio = -EINVAL;
996 dev_err(&client->dev,
997 "%s: cannot set direction for nfc ese gpio [%d]\n",
998 __func__, platform_data->ese_gpio);
999 /* ese gpio optional so we should continue */
1000 }
1001 }
1002 } else {
1003 nqx_dev->ese_gpio = -EINVAL;
1004 dev_err(&client->dev,
1005 "%s: ese gpio not provided\n", __func__);
1006 /* ese gpio optional so we should continue */
1007 }
1008 if (gpio_is_valid(platform_data->clkreq_gpio)) {
1009 r = gpio_request(platform_data->clkreq_gpio,
1010 "nfc_clkreq_gpio");
1011 if (r) {
1012 dev_err(&client->dev,
1013 "%s: unable to request nfc clkreq gpio [%d]\n",
1014 __func__, platform_data->clkreq_gpio);
1015 goto err_ese_gpio;
1016 }
1017 r = gpio_direction_input(platform_data->clkreq_gpio);
1018 if (r) {
1019 dev_err(&client->dev,
1020 "%s: cannot set direction for nfc clkreq gpio [%d]\n",
1021 __func__, platform_data->clkreq_gpio);
1022 goto err_clkreq_gpio;
1023 }
1024 } else {
1025 dev_err(&client->dev,
1026 "%s: clkreq gpio not provided\n", __func__);
1027 goto err_ese_gpio;
1028 }
1029
1030 nqx_dev->en_gpio = platform_data->en_gpio;
1031 nqx_dev->irq_gpio = platform_data->irq_gpio;
1032 nqx_dev->firm_gpio = platform_data->firm_gpio;
1033 nqx_dev->clkreq_gpio = platform_data->clkreq_gpio;
1034 nqx_dev->pdata = platform_data;
1035
1036 /* init mutex and queues */
1037 init_waitqueue_head(&nqx_dev->read_wq);
1038 mutex_init(&nqx_dev->read_mutex);
1039 spin_lock_init(&nqx_dev->irq_enabled_lock);
1040
1041 nqx_dev->nqx_device.minor = MISC_DYNAMIC_MINOR;
1042 nqx_dev->nqx_device.name = "nq-nci";
1043 nqx_dev->nqx_device.fops = &nfc_dev_fops;
1044
1045 r = misc_register(&nqx_dev->nqx_device);
1046 if (r) {
1047 dev_err(&client->dev, "%s: misc_register failed\n", __func__);
1048 goto err_misc_register;
1049 }
1050
1051 /* NFC_INT IRQ */
1052 nqx_dev->irq_enabled = true;
1053 r = request_irq(client->irq, nqx_dev_irq_handler,
1054 IRQF_TRIGGER_HIGH, client->name, nqx_dev);
1055 if (r) {
1056 dev_err(&client->dev, "%s: request_irq failed\n", __func__);
1057 goto err_request_irq_failed;
1058 }
1059 nqx_disable_irq(nqx_dev);
1060
1061 /*
1062 * To be efficient we need to test whether nfcc hardware is physically
1063 * present before attempting further hardware initialisation.
1064 *
1065 */
1066 r = nfcc_hw_check(client, nqx_dev);
1067 if (r) {
1068 /* make sure NFCC is not enabled */
1069 gpio_set_value(platform_data->en_gpio, 0);
1070 /* We don't think there is hardware switch NFC OFF */
1071 goto err_request_hw_check_failed;
1072 }
1073
1074 /* Register reboot notifier here */
1075 r = register_reboot_notifier(&nfcc_notifier);
1076 if (r) {
1077 dev_err(&client->dev,
1078 "%s: cannot register reboot notifier(err = %d)\n",
1079 __func__, r);
1080 /*
1081 * nfcc_hw_check function not doing memory
1082 * allocation so using same goto target here
1083 */
1084 goto err_request_hw_check_failed;
1085 }
1086
1087#ifdef NFC_KERNEL_BU
1088 r = nqx_clock_select(nqx_dev);
1089 if (r < 0) {
1090 dev_err(&client->dev,
1091 "%s: nqx_clock_select failed\n", __func__);
1092 goto err_clock_en_failed;
1093 }
1094 gpio_set_value(platform_data->en_gpio, 1);
1095#endif
1096 device_init_wakeup(&client->dev, true);
1097 device_set_wakeup_capable(&client->dev, true);
1098 i2c_set_clientdata(client, nqx_dev);
1099 nqx_dev->irq_wake_up = false;
1100
1101 dev_err(&client->dev,
1102 "%s: probing NFCC NQxxx exited successfully\n",
1103 __func__);
1104 return 0;
1105
1106#ifdef NFC_KERNEL_BU
1107err_clock_en_failed:
1108 unregister_reboot_notifier(&nfcc_notifier);
1109#endif
1110err_request_hw_check_failed:
1111 free_irq(client->irq, nqx_dev);
1112err_request_irq_failed:
1113 misc_deregister(&nqx_dev->nqx_device);
1114err_misc_register:
1115 mutex_destroy(&nqx_dev->read_mutex);
1116err_clkreq_gpio:
1117 gpio_free(platform_data->clkreq_gpio);
1118err_ese_gpio:
1119 /* optional gpio, not sure was configured in probe */
1120 if (nqx_dev->ese_gpio > 0)
1121 gpio_free(platform_data->ese_gpio);
1122err_firm_gpio:
1123 gpio_free(platform_data->firm_gpio);
1124err_irq_gpio:
1125 gpio_free(platform_data->irq_gpio);
1126err_en_gpio:
1127 gpio_free(platform_data->en_gpio);
1128err_mem:
1129 kfree(nqx_dev->kbuf);
1130err_free_dev:
1131 kfree(nqx_dev);
1132err_free_data:
1133 if (client->dev.of_node)
1134 devm_kfree(&client->dev, platform_data);
1135err_platform_data:
1136 dev_err(&client->dev,
1137 "%s: probing nqxx failed, check hardware\n",
1138 __func__);
1139 return r;
1140}
1141
1142static int nqx_remove(struct i2c_client *client)
1143{
1144 int ret = 0;
1145 struct nqx_dev *nqx_dev;
1146
1147 nqx_dev = i2c_get_clientdata(client);
1148 if (!nqx_dev) {
1149 dev_err(&client->dev,
1150 "%s: device doesn't exist anymore\n", __func__);
1151 ret = -ENODEV;
1152 goto err;
1153 }
1154
1155 unregister_reboot_notifier(&nfcc_notifier);
1156 free_irq(client->irq, nqx_dev);
1157 misc_deregister(&nqx_dev->nqx_device);
1158 mutex_destroy(&nqx_dev->read_mutex);
1159 gpio_free(nqx_dev->clkreq_gpio);
1160 /* optional gpio, not sure was configured in probe */
1161 if (nqx_dev->ese_gpio > 0)
1162 gpio_free(nqx_dev->ese_gpio);
1163 gpio_free(nqx_dev->firm_gpio);
1164 gpio_free(nqx_dev->irq_gpio);
1165 gpio_free(nqx_dev->en_gpio);
1166 kfree(nqx_dev->kbuf);
1167 if (client->dev.of_node)
1168 devm_kfree(&client->dev, nqx_dev->pdata);
1169
1170 kfree(nqx_dev);
1171err:
1172 return ret;
1173}
1174
1175static int nqx_suspend(struct device *device)
1176{
1177 struct i2c_client *client = to_i2c_client(device);
1178 struct nqx_dev *nqx_dev = i2c_get_clientdata(client);
1179
1180 if (device_may_wakeup(&client->dev) && nqx_dev->irq_enabled) {
1181 if (!enable_irq_wake(client->irq))
1182 nqx_dev->irq_wake_up = true;
1183 }
1184 return 0;
1185}
1186
1187static int nqx_resume(struct device *device)
1188{
1189 struct i2c_client *client = to_i2c_client(device);
1190 struct nqx_dev *nqx_dev = i2c_get_clientdata(client);
1191
1192 if (device_may_wakeup(&client->dev) && nqx_dev->irq_wake_up) {
1193 if (!disable_irq_wake(client->irq))
1194 nqx_dev->irq_wake_up = false;
1195 }
1196 return 0;
1197}
1198
1199static const struct i2c_device_id nqx_id[] = {
1200 {"nqx-i2c", 0},
1201 {}
1202};
1203
1204static const struct dev_pm_ops nfc_pm_ops = {
1205 SET_SYSTEM_SLEEP_PM_OPS(nqx_suspend, nqx_resume)
1206};
1207
1208static struct i2c_driver nqx = {
1209 .id_table = nqx_id,
1210 .probe = nqx_probe,
1211 .remove = nqx_remove,
1212 .driver = {
1213 .owner = THIS_MODULE,
1214 .name = "nq-nci",
1215 .of_match_table = msm_match_table,
1216 .pm = &nfc_pm_ops,
1217 },
1218};
1219
1220static int nfcc_reboot(struct notifier_block *notifier, unsigned long val,
1221 void *v)
1222{
1223 gpio_set_value(disable_ctrl, 1);
1224 return NOTIFY_OK;
1225}
1226
1227/*
1228 * module load/unload record keeping
1229 */
1230static int __init nqx_dev_init(void)
1231{
1232 return i2c_add_driver(&nqx);
1233}
1234module_init(nqx_dev_init);
1235
1236static void __exit nqx_dev_exit(void)
1237{
1238 unregister_reboot_notifier(&nfcc_notifier);
1239 i2c_del_driver(&nqx);
1240}
1241module_exit(nqx_dev_exit);
1242
1243MODULE_DESCRIPTION("NFC nqx");
1244MODULE_LICENSE("GPL v2");