blob: baa4f9498a846bc8595f6a9c43325e1f4a467c3c [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);
357 if (gpio_get_value(nqx_dev->ese_gpio)) {
358 dev_dbg(&nqx_dev->client->dev, "ese_gpio is enabled\n");
359 r = 0;
360 }
361 }
362 } else if (arg == 1) {
363 if (nqx_dev->nfc_ven_enabled &&
364 ((nqx_dev->nqx_info.info.chip_type == NFCC_NQ_220) ||
365 (nqx_dev->nqx_info.info.chip_type == NFCC_PN66T))) {
366 /**
367 * Let's inform the CLF we're
368 * powering off the eSE
369 */
370 r = nqx_standby_write(nqx_dev, svdd_off_cmd_warn,
371 sizeof(svdd_off_cmd_warn));
372 if (r < 0) {
373 dev_err(&nqx_dev->client->dev,
374 "%s: write failed after max retry\n",
375 __func__);
376 return -ENXIO;
377 }
378 dev_dbg(&nqx_dev->client->dev,
379 "%s: svdd_off_cmd_warn sent\n", __func__);
380
381 /* let's power down the eSE */
382 gpio_set_value(nqx_dev->ese_gpio, 0);
383 dev_dbg(&nqx_dev->client->dev,
384 "%s: nqx_dev->ese_gpio set to 0\n", __func__);
385
386 /**
387 * Time needed for the SVDD capacitor
388 * to get discharged
389 */
390 usleep_range(8000, 8100);
391
392 /* Let's inform the CLF the eSE is now off */
393 r = nqx_standby_write(nqx_dev, svdd_off_cmd_done,
394 sizeof(svdd_off_cmd_done));
395 if (r < 0) {
396 dev_err(&nqx_dev->client->dev,
397 "%s: write failed after max retry\n",
398 __func__);
399 return -ENXIO;
400 }
401 dev_dbg(&nqx_dev->client->dev,
402 "%s: svdd_off_cmd_done sent\n", __func__);
403 } else {
404 /**
405 * In case the NFC is off,
406 * there's no need to send the i2c commands
407 */
408 gpio_set_value(nqx_dev->ese_gpio, 0);
409 }
410
411 if (!gpio_get_value(nqx_dev->ese_gpio)) {
412 dev_dbg(&nqx_dev->client->dev, "ese_gpio is disabled\n");
413 r = 0;
414 }
415
416 if (!nqx_dev->nfc_ven_enabled) {
417 /* hardware dependent delay */
418 usleep_range(1000, 1100);
419 dev_dbg(&nqx_dev->client->dev, "disabling en_gpio\n");
420 gpio_set_value(nqx_dev->en_gpio, 0);
421 }
422 } else if (arg == 3) {
423 r = gpio_get_value(nqx_dev->ese_gpio);
424 }
425 return r;
426}
427
428static int nfc_open(struct inode *inode, struct file *filp)
429{
430 int ret = 0;
431 struct nqx_dev *nqx_dev = container_of(filp->private_data,
432 struct nqx_dev, nqx_device);
433
434 filp->private_data = nqx_dev;
435 nqx_init_stat(nqx_dev);
436
437 dev_dbg(&nqx_dev->client->dev,
438 "%s: %d,%d\n", __func__, imajor(inode), iminor(inode));
439 return ret;
440}
441
442/*
443 * nfc_ioctl_power_states() - power control
444 * @filp: pointer to the file descriptor
445 * @arg: mode that we want to move to
446 *
447 * Device power control. Depending on the arg value, device moves to
448 * different states
449 * (arg = 0): NFC_ENABLE GPIO = 0, FW_DL GPIO = 0
450 * (arg = 1): NFC_ENABLE GPIO = 1, FW_DL GPIO = 0
451 * (arg = 2): FW_DL GPIO = 1
452 *
453 * Return: -ENOIOCTLCMD if arg is not supported, 0 in any other case
454 */
455int nfc_ioctl_power_states(struct file *filp, unsigned long arg)
456{
457 int r = 0;
458 struct nqx_dev *nqx_dev = filp->private_data;
459
460 if (arg == 0) {
461 /*
462 * We are attempting a hardware reset so let us disable
463 * interrupts to avoid spurious notifications to upper
464 * layers.
465 */
466 nqx_disable_irq(nqx_dev);
467 dev_dbg(&nqx_dev->client->dev,
468 "gpio_set_value disable: %s: info: %p\n",
469 __func__, nqx_dev);
470 if (gpio_is_valid(nqx_dev->firm_gpio))
471 gpio_set_value(nqx_dev->firm_gpio, 0);
472
473 if (gpio_is_valid(nqx_dev->ese_gpio)) {
474 if (!gpio_get_value(nqx_dev->ese_gpio)) {
475 dev_dbg(&nqx_dev->client->dev, "disabling en_gpio\n");
476 gpio_set_value(nqx_dev->en_gpio, 0);
477 } else {
478 dev_dbg(&nqx_dev->client->dev, "keeping en_gpio high\n");
479 }
480 } else {
481 dev_dbg(&nqx_dev->client->dev, "ese_gpio invalid, set en_gpio to low\n");
482 gpio_set_value(nqx_dev->en_gpio, 0);
483 }
484 r = nqx_clock_deselect(nqx_dev);
485 if (r < 0)
486 dev_err(&nqx_dev->client->dev, "unable to disable clock\n");
487 nqx_dev->nfc_ven_enabled = false;
488 /* hardware dependent delay */
489 msleep(100);
490 } else if (arg == 1) {
491 nqx_enable_irq(nqx_dev);
492 dev_dbg(&nqx_dev->client->dev,
493 "gpio_set_value enable: %s: info: %p\n",
494 __func__, nqx_dev);
495 if (gpio_is_valid(nqx_dev->firm_gpio))
496 gpio_set_value(nqx_dev->firm_gpio, 0);
497 gpio_set_value(nqx_dev->en_gpio, 1);
498 r = nqx_clock_select(nqx_dev);
499 if (r < 0)
500 dev_err(&nqx_dev->client->dev, "unable to enable clock\n");
501 nqx_dev->nfc_ven_enabled = true;
502 msleep(20);
503 } else if (arg == 2) {
504 /*
505 * We are switching to Dowload Mode, toggle the enable pin
506 * in order to set the NFCC in the new mode
507 */
508 if (gpio_is_valid(nqx_dev->ese_gpio)) {
509 if (gpio_get_value(nqx_dev->ese_gpio)) {
510 dev_err(&nqx_dev->client->dev,
511 "FW download forbidden while ese is on\n");
512 return -EBUSY; /* Device or resource busy */
513 }
514 }
515 gpio_set_value(nqx_dev->en_gpio, 1);
516 msleep(20);
517 if (gpio_is_valid(nqx_dev->firm_gpio))
518 gpio_set_value(nqx_dev->firm_gpio, 1);
519 msleep(20);
520 gpio_set_value(nqx_dev->en_gpio, 0);
521 msleep(100);
522 gpio_set_value(nqx_dev->en_gpio, 1);
523 msleep(20);
524 } else {
525 r = -ENOIOCTLCMD;
526 }
527
528 return r;
529}
530
531#ifdef CONFIG_COMPAT
532static long nfc_compat_ioctl(struct file *pfile, unsigned int cmd,
533 unsigned long arg)
534{
535 long r = 0;
536
537 arg = (compat_u64)arg;
538 switch (cmd) {
539 case NFC_SET_PWR:
540 nfc_ioctl_power_states(pfile, arg);
541 break;
542 case ESE_SET_PWR:
543 nqx_ese_pwr(pfile->private_data, arg);
544 break;
545 case ESE_GET_PWR:
546 nqx_ese_pwr(pfile->private_data, 3);
547 break;
548 case SET_RX_BLOCK:
549 break;
550 case SET_EMULATOR_TEST_POINT:
551 break;
552 default:
553 r = -ENOTTY;
554 }
555 return r;
556}
557#endif
558
559/*
560 * nfc_ioctl_core_reset_ntf()
561 * @filp: pointer to the file descriptor
562 *
563 * Allows callers to determine if a CORE_RESET_NTF has arrived
564 *
565 * Return: the value of variable core_reset_ntf
566 */
567int nfc_ioctl_core_reset_ntf(struct file *filp)
568{
569 struct nqx_dev *nqx_dev = filp->private_data;
570
571 dev_dbg(&nqx_dev->client->dev, "%s: returning = %d\n", __func__,
572 nqx_dev->core_reset_ntf);
573 return nqx_dev->core_reset_ntf;
574}
575
576/*
577 * Inside nfc_ioctl_nfcc_info
578 *
579 * @brief nfc_ioctl_nfcc_info
580 *
581 * Check the NQ Chipset and firmware version details
582 */
583unsigned int nfc_ioctl_nfcc_info(struct file *filp, unsigned long arg)
584{
585 unsigned int r = 0;
586 struct nqx_dev *nqx_dev = filp->private_data;
587
588 r = nqx_dev->nqx_info.i;
589 dev_dbg(&nqx_dev->client->dev,
590 "nqx nfc : nfc_ioctl_nfcc_info r = %d\n", r);
591
592 return r;
593}
594
595static long nfc_ioctl(struct file *pfile, unsigned int cmd,
596 unsigned long arg)
597{
598 int r = 0;
599
600 switch (cmd) {
601 case NFC_SET_PWR:
602 r = nfc_ioctl_power_states(pfile, arg);
603 break;
604 case ESE_SET_PWR:
605 r = nqx_ese_pwr(pfile->private_data, arg);
606 break;
607 case ESE_GET_PWR:
608 r = nqx_ese_pwr(pfile->private_data, 3);
609 break;
610 case SET_RX_BLOCK:
611 break;
612 case SET_EMULATOR_TEST_POINT:
613 break;
614 case NFCC_INITIAL_CORE_RESET_NTF:
615 r = nfc_ioctl_core_reset_ntf(pfile);
616 break;
617 case NFCC_GET_INFO:
618 r = nfc_ioctl_nfcc_info(pfile, arg);
619 break;
620 default:
621 r = -ENOIOCTLCMD;
622 }
623 return r;
624}
625
626static const struct file_operations nfc_dev_fops = {
627 .owner = THIS_MODULE,
628 .llseek = no_llseek,
629 .read = nfc_read,
630 .write = nfc_write,
631 .open = nfc_open,
632 .unlocked_ioctl = nfc_ioctl,
633#ifdef CONFIG_COMPAT
634 .compat_ioctl = nfc_compat_ioctl
635#endif
636};
637
638/* Check for availability of NQ_ NFC controller hardware */
639static int nfcc_hw_check(struct i2c_client *client, struct nqx_dev *nqx_dev)
640{
641 int ret = 0;
642
643 unsigned char raw_nci_reset_cmd[] = {0x20, 0x00, 0x01, 0x00};
644 unsigned char raw_nci_init_cmd[] = {0x20, 0x01, 0x00};
645 unsigned char nci_init_rsp[28];
646 unsigned char nci_reset_rsp[6];
647 unsigned char init_rsp_len = 0;
648 unsigned int enable_gpio = nqx_dev->en_gpio;
649 /* making sure that the NFCC starts in a clean state. */
650 gpio_set_value(enable_gpio, 0);/* ULPM: Disable */
651 /* hardware dependent delay */
652 msleep(20);
653 gpio_set_value(enable_gpio, 1);/* HPD : Enable*/
654 /* hardware dependent delay */
655 msleep(20);
656
657 /* send NCI CORE RESET CMD with Keep Config parameters */
658 ret = i2c_master_send(client, raw_nci_reset_cmd,
659 sizeof(raw_nci_reset_cmd));
660 if (ret < 0) {
661 dev_err(&client->dev,
662 "%s: - i2c_master_send Error\n", __func__);
663 goto err_nfcc_hw_check;
664 }
665 /* hardware dependent delay */
666 msleep(30);
667
668 /* Read Response of RESET command */
669 ret = i2c_master_recv(client, nci_reset_rsp,
670 sizeof(nci_reset_rsp));
671 dev_err(&client->dev,
672 "%s: - nq - reset cmd answer : NfcNciRx %x %x %x\n",
673 __func__, nci_reset_rsp[0],
674 nci_reset_rsp[1], nci_reset_rsp[2]);
675 if (ret < 0) {
676 dev_err(&client->dev,
677 "%s: - i2c_master_recv Error\n", __func__);
678 goto err_nfcc_hw_check;
679 }
680 ret = i2c_master_send(client, raw_nci_init_cmd,
681 sizeof(raw_nci_init_cmd));
682 if (ret < 0) {
683 dev_err(&client->dev,
684 "%s: - i2c_master_send Error\n", __func__);
685 goto err_nfcc_hw_check;
686 }
687 /* hardware dependent delay */
688 msleep(30);
689 /* Read Response of INIT command */
690 ret = i2c_master_recv(client, nci_init_rsp,
691 sizeof(nci_init_rsp));
692 if (ret < 0) {
693 dev_err(&client->dev,
694 "%s: - i2c_master_recv Error\n", __func__);
695 goto err_nfcc_hw_check;
696 }
697 init_rsp_len = 2 + nci_init_rsp[2]; /*payload + len*/
698 if (init_rsp_len > PAYLOAD_HEADER_LENGTH) {
699 nqx_dev->nqx_info.info.chip_type =
700 nci_init_rsp[init_rsp_len - 3];
701 nqx_dev->nqx_info.info.rom_version =
702 nci_init_rsp[init_rsp_len - 2];
703 nqx_dev->nqx_info.info.fw_major =
704 nci_init_rsp[init_rsp_len - 1];
705 nqx_dev->nqx_info.info.fw_minor =
706 nci_init_rsp[init_rsp_len];
707 }
708 dev_dbg(&nqx_dev->client->dev, "NQ NFCC chip_type = %x\n",
709 nqx_dev->nqx_info.info.chip_type);
710 dev_dbg(&nqx_dev->client->dev, "NQ fw version = %x.%x.%x\n",
711 nqx_dev->nqx_info.info.rom_version,
712 nqx_dev->nqx_info.info.fw_major,
713 nqx_dev->nqx_info.info.fw_minor);
714
715 switch (nqx_dev->nqx_info.info.chip_type) {
716 case NFCC_NQ_210:
717 dev_dbg(&client->dev,
718 "%s: ## NFCC == NQ210 ##\n", __func__);
719 break;
720 case NFCC_NQ_220:
721 dev_dbg(&client->dev,
722 "%s: ## NFCC == NQ220 ##\n", __func__);
723 break;
724 case NFCC_NQ_310:
725 dev_dbg(&client->dev,
726 "%s: ## NFCC == NQ310 ##\n", __func__);
727 break;
728 case NFCC_NQ_330:
729 dev_dbg(&client->dev,
730 "%s: ## NFCC == NQ330 ##\n", __func__);
731 break;
732 case NFCC_PN66T:
733 dev_dbg(&client->dev,
734 "%s: ## NFCC == PN66T ##\n", __func__);
735 break;
736 default:
737 dev_err(&client->dev,
738 "%s: - NFCC HW not Supported\n", __func__);
739 break;
740 }
741
742 /*Disable NFC by default to save power on boot*/
743 gpio_set_value(enable_gpio, 0);/* ULPM: Disable */
744 ret = 0;
745 goto done;
746
747err_nfcc_hw_check:
748 ret = -ENXIO;
749 dev_err(&client->dev,
750 "%s: - NFCC HW not available\n", __func__);
751done:
752 return ret;
753}
754
755/*
756 * Routine to enable clock.
757 * this routine can be extended to select from multiple
758 * sources based on clk_src_name.
759 */
760static int nqx_clock_select(struct nqx_dev *nqx_dev)
761{
762 int r = 0;
763
764 nqx_dev->s_clk = clk_get(&nqx_dev->client->dev, "ref_clk");
765
766 if (nqx_dev->s_clk == NULL)
767 goto err_clk;
768
769 if (nqx_dev->clk_run == false)
770 r = clk_prepare_enable(nqx_dev->s_clk);
771
772 if (r)
773 goto err_clk;
774
775 nqx_dev->clk_run = true;
776
777 return r;
778
779err_clk:
780 r = -1;
781 return r;
782}
783
784/*
785 * Routine to disable clocks
786 */
787static int nqx_clock_deselect(struct nqx_dev *nqx_dev)
788{
789 int r = -1;
790
791 if (nqx_dev->s_clk != NULL) {
792 if (nqx_dev->clk_run == true) {
793 clk_disable_unprepare(nqx_dev->s_clk);
794 nqx_dev->clk_run = false;
795 }
796 return 0;
797 }
798 return r;
799}
800
801static int nfc_parse_dt(struct device *dev, struct nqx_platform_data *pdata)
802{
803 int r = 0;
804 struct device_node *np = dev->of_node;
805
806 pdata->en_gpio = of_get_named_gpio(np, "qcom,nq-ven", 0);
807 if ((!gpio_is_valid(pdata->en_gpio)))
808 return -EINVAL;
809 disable_ctrl = pdata->en_gpio;
810
811 pdata->irq_gpio = of_get_named_gpio(np, "qcom,nq-irq", 0);
812 if ((!gpio_is_valid(pdata->irq_gpio)))
813 return -EINVAL;
814
815 pdata->firm_gpio = of_get_named_gpio(np, "qcom,nq-firm", 0);
816 if (!gpio_is_valid(pdata->firm_gpio)) {
817 dev_warn(dev,
818 "FIRM GPIO <OPTIONAL> error getting from OF node\n");
819 pdata->firm_gpio = -EINVAL;
820 }
821
822 pdata->ese_gpio = of_get_named_gpio(np, "qcom,nq-esepwr", 0);
823 if (!gpio_is_valid(pdata->ese_gpio)) {
824 dev_warn(dev,
825 "ese GPIO <OPTIONAL> error getting from OF node\n");
826 pdata->ese_gpio = -EINVAL;
827 }
828
829 r = of_property_read_string(np, "qcom,clk-src", &pdata->clk_src_name);
830
831 pdata->clkreq_gpio = of_get_named_gpio(np, "qcom,nq-clkreq", 0);
832
833 if (r)
834 return -EINVAL;
835 return r;
836}
837
838static inline int gpio_input_init(const struct device * const dev,
839 const int gpio, const char * const gpio_name)
840{
841 int r = gpio_request(gpio, gpio_name);
842
843 if (r) {
844 dev_err(dev, "unable to request gpio [%d]\n", gpio);
845 return r;
846 }
847
848 r = gpio_direction_input(gpio);
849 if (r)
850 dev_err(dev, "unable to set direction for gpio [%d]\n", gpio);
851
852 return r;
853}
854
855static int nqx_probe(struct i2c_client *client,
856 const struct i2c_device_id *id)
857{
858 int r = 0;
859 int irqn = 0;
860 struct nqx_platform_data *platform_data;
861 struct nqx_dev *nqx_dev;
862
863 dev_dbg(&client->dev, "%s: enter\n", __func__);
864 if (client->dev.of_node) {
865 platform_data = devm_kzalloc(&client->dev,
866 sizeof(struct nqx_platform_data), GFP_KERNEL);
867 if (!platform_data) {
868 r = -ENOMEM;
869 goto err_platform_data;
870 }
871 r = nfc_parse_dt(&client->dev, platform_data);
872 if (r)
873 goto err_free_data;
874 } else
875 platform_data = client->dev.platform_data;
876
877 dev_dbg(&client->dev,
878 "%s, inside nfc-nci flags = %x\n",
879 __func__, client->flags);
880
881 if (platform_data == NULL) {
882 dev_err(&client->dev, "%s: failed\n", __func__);
883 r = -ENODEV;
884 goto err_platform_data;
885 }
886 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
887 dev_err(&client->dev, "%s: need I2C_FUNC_I2C\n", __func__);
888 r = -ENODEV;
889 goto err_free_data;
890 }
891 nqx_dev = kzalloc(sizeof(*nqx_dev), GFP_KERNEL);
892 if (nqx_dev == NULL) {
893 r = -ENOMEM;
894 goto err_free_data;
895 }
896 nqx_dev->client = client;
897 nqx_dev->kbuflen = MAX_BUFFER_SIZE;
898 nqx_dev->kbuf = kzalloc(MAX_BUFFER_SIZE, GFP_KERNEL);
899 if (!nqx_dev->kbuf) {
900 dev_err(&client->dev,
901 "failed to allocate memory for nqx_dev->kbuf\n");
902 r = -ENOMEM;
903 goto err_free_dev;
904 }
905
906 if (gpio_is_valid(platform_data->en_gpio)) {
907 r = gpio_request(platform_data->en_gpio, "nfc_reset_gpio");
908 if (r) {
909 dev_err(&client->dev,
910 "%s: unable to request nfc reset gpio [%d]\n",
911 __func__,
912 platform_data->en_gpio);
913 goto err_mem;
914 }
915 r = gpio_direction_output(platform_data->en_gpio, 0);
916 if (r) {
917 dev_err(&client->dev,
918 "%s: unable to set direction for nfc reset gpio [%d]\n",
919 __func__,
920 platform_data->en_gpio);
921 goto err_en_gpio;
922 }
923 } else {
924 dev_err(&client->dev,
925 "%s: nfc reset gpio not provided\n", __func__);
926 goto err_mem;
927 }
928
929 if (gpio_is_valid(platform_data->irq_gpio)) {
930 r = gpio_request(platform_data->irq_gpio, "nfc_irq_gpio");
931 if (r) {
932 dev_err(&client->dev, "%s: unable to request nfc irq gpio [%d]\n",
933 __func__, platform_data->irq_gpio);
934 goto err_en_gpio;
935 }
936 r = gpio_direction_input(platform_data->irq_gpio);
937 if (r) {
938 dev_err(&client->dev,
939 "%s: unable to set direction for nfc irq gpio [%d]\n",
940 __func__,
941 platform_data->irq_gpio);
942 goto err_irq_gpio;
943 }
944 irqn = gpio_to_irq(platform_data->irq_gpio);
945 if (irqn < 0) {
946 r = irqn;
947 goto err_irq_gpio;
948 }
949 client->irq = irqn;
950 } else {
951 dev_err(&client->dev, "%s: irq gpio not provided\n", __func__);
952 goto err_en_gpio;
953 }
954 if (gpio_is_valid(platform_data->firm_gpio)) {
955 r = gpio_request(platform_data->firm_gpio,
956 "nfc_firm_gpio");
957 if (r) {
958 dev_err(&client->dev,
959 "%s: unable to request nfc firmware gpio [%d]\n",
960 __func__, platform_data->firm_gpio);
961 goto err_irq_gpio;
962 }
963 r = gpio_direction_output(platform_data->firm_gpio, 0);
964 if (r) {
965 dev_err(&client->dev,
966 "%s: cannot set direction for nfc firmware gpio [%d]\n",
967 __func__, platform_data->firm_gpio);
968 goto err_firm_gpio;
969 }
970 } else {
971 dev_err(&client->dev,
972 "%s: firm gpio not provided\n", __func__);
973 goto err_irq_gpio;
974 }
975 if (gpio_is_valid(platform_data->ese_gpio)) {
976 r = gpio_request(platform_data->ese_gpio,
977 "nfc-ese_pwr");
978 if (r) {
979 nqx_dev->ese_gpio = -EINVAL;
980 dev_err(&client->dev,
981 "%s: unable to request nfc ese gpio [%d]\n",
982 __func__, platform_data->ese_gpio);
983 /* ese gpio optional so we should continue */
984 } else {
985 nqx_dev->ese_gpio = platform_data->ese_gpio;
986 r = gpio_direction_output(platform_data->ese_gpio, 0);
987 if (r) {
988 /*
989 * free ese gpio and set invalid
990 * to avoid further use
991 */
992 gpio_free(platform_data->ese_gpio);
993 nqx_dev->ese_gpio = -EINVAL;
994 dev_err(&client->dev,
995 "%s: cannot set direction for nfc ese gpio [%d]\n",
996 __func__, platform_data->ese_gpio);
997 /* ese gpio optional so we should continue */
998 }
999 }
1000 } else {
1001 nqx_dev->ese_gpio = -EINVAL;
1002 dev_err(&client->dev,
1003 "%s: ese gpio not provided\n", __func__);
1004 /* ese gpio optional so we should continue */
1005 }
1006 if (gpio_is_valid(platform_data->clkreq_gpio)) {
1007 r = gpio_request(platform_data->clkreq_gpio,
1008 "nfc_clkreq_gpio");
1009 if (r) {
1010 dev_err(&client->dev,
1011 "%s: unable to request nfc clkreq gpio [%d]\n",
1012 __func__, platform_data->clkreq_gpio);
1013 goto err_ese_gpio;
1014 }
1015 r = gpio_direction_input(platform_data->clkreq_gpio);
1016 if (r) {
1017 dev_err(&client->dev,
1018 "%s: cannot set direction for nfc clkreq gpio [%d]\n",
1019 __func__, platform_data->clkreq_gpio);
1020 goto err_clkreq_gpio;
1021 }
1022 } else {
1023 dev_err(&client->dev,
1024 "%s: clkreq gpio not provided\n", __func__);
1025 goto err_ese_gpio;
1026 }
1027
1028 nqx_dev->en_gpio = platform_data->en_gpio;
1029 nqx_dev->irq_gpio = platform_data->irq_gpio;
1030 nqx_dev->firm_gpio = platform_data->firm_gpio;
1031 nqx_dev->clkreq_gpio = platform_data->clkreq_gpio;
1032 nqx_dev->pdata = platform_data;
1033
1034 /* init mutex and queues */
1035 init_waitqueue_head(&nqx_dev->read_wq);
1036 mutex_init(&nqx_dev->read_mutex);
1037 spin_lock_init(&nqx_dev->irq_enabled_lock);
1038
1039 nqx_dev->nqx_device.minor = MISC_DYNAMIC_MINOR;
1040 nqx_dev->nqx_device.name = "nq-nci";
1041 nqx_dev->nqx_device.fops = &nfc_dev_fops;
1042
1043 r = misc_register(&nqx_dev->nqx_device);
1044 if (r) {
1045 dev_err(&client->dev, "%s: misc_register failed\n", __func__);
1046 goto err_misc_register;
1047 }
1048
1049 /* NFC_INT IRQ */
1050 nqx_dev->irq_enabled = true;
1051 r = request_irq(client->irq, nqx_dev_irq_handler,
1052 IRQF_TRIGGER_HIGH, client->name, nqx_dev);
1053 if (r) {
1054 dev_err(&client->dev, "%s: request_irq failed\n", __func__);
1055 goto err_request_irq_failed;
1056 }
1057 nqx_disable_irq(nqx_dev);
1058
1059 /*
1060 * To be efficient we need to test whether nfcc hardware is physically
1061 * present before attempting further hardware initialisation.
1062 *
1063 */
1064 r = nfcc_hw_check(client, nqx_dev);
1065 if (r) {
1066 /* make sure NFCC is not enabled */
1067 gpio_set_value(platform_data->en_gpio, 0);
1068 /* We don't think there is hardware switch NFC OFF */
1069 goto err_request_hw_check_failed;
1070 }
1071
1072 /* Register reboot notifier here */
1073 r = register_reboot_notifier(&nfcc_notifier);
1074 if (r) {
1075 dev_err(&client->dev,
1076 "%s: cannot register reboot notifier(err = %d)\n",
1077 __func__, r);
1078 /*
1079 * nfcc_hw_check function not doing memory
1080 * allocation so using same goto target here
1081 */
1082 goto err_request_hw_check_failed;
1083 }
1084
1085#ifdef NFC_KERNEL_BU
1086 r = nqx_clock_select(nqx_dev);
1087 if (r < 0) {
1088 dev_err(&client->dev,
1089 "%s: nqx_clock_select failed\n", __func__);
1090 goto err_clock_en_failed;
1091 }
1092 gpio_set_value(platform_data->en_gpio, 1);
1093#endif
1094 device_init_wakeup(&client->dev, true);
1095 device_set_wakeup_capable(&client->dev, true);
1096 i2c_set_clientdata(client, nqx_dev);
1097 nqx_dev->irq_wake_up = false;
1098
1099 dev_err(&client->dev,
1100 "%s: probing NFCC NQxxx exited successfully\n",
1101 __func__);
1102 return 0;
1103
1104#ifdef NFC_KERNEL_BU
1105err_clock_en_failed:
1106 unregister_reboot_notifier(&nfcc_notifier);
1107#endif
1108err_request_hw_check_failed:
1109 free_irq(client->irq, nqx_dev);
1110err_request_irq_failed:
1111 misc_deregister(&nqx_dev->nqx_device);
1112err_misc_register:
1113 mutex_destroy(&nqx_dev->read_mutex);
1114err_clkreq_gpio:
1115 gpio_free(platform_data->clkreq_gpio);
1116err_ese_gpio:
1117 /* optional gpio, not sure was configured in probe */
1118 if (nqx_dev->ese_gpio > 0)
1119 gpio_free(platform_data->ese_gpio);
1120err_firm_gpio:
1121 gpio_free(platform_data->firm_gpio);
1122err_irq_gpio:
1123 gpio_free(platform_data->irq_gpio);
1124err_en_gpio:
1125 gpio_free(platform_data->en_gpio);
1126err_mem:
1127 kfree(nqx_dev->kbuf);
1128err_free_dev:
1129 kfree(nqx_dev);
1130err_free_data:
1131 if (client->dev.of_node)
1132 devm_kfree(&client->dev, platform_data);
1133err_platform_data:
1134 dev_err(&client->dev,
1135 "%s: probing nqxx failed, check hardware\n",
1136 __func__);
1137 return r;
1138}
1139
1140static int nqx_remove(struct i2c_client *client)
1141{
1142 int ret = 0;
1143 struct nqx_dev *nqx_dev;
1144
1145 nqx_dev = i2c_get_clientdata(client);
1146 if (!nqx_dev) {
1147 dev_err(&client->dev,
1148 "%s: device doesn't exist anymore\n", __func__);
1149 ret = -ENODEV;
1150 goto err;
1151 }
1152
1153 unregister_reboot_notifier(&nfcc_notifier);
1154 free_irq(client->irq, nqx_dev);
1155 misc_deregister(&nqx_dev->nqx_device);
1156 mutex_destroy(&nqx_dev->read_mutex);
1157 gpio_free(nqx_dev->clkreq_gpio);
1158 /* optional gpio, not sure was configured in probe */
1159 if (nqx_dev->ese_gpio > 0)
1160 gpio_free(nqx_dev->ese_gpio);
1161 gpio_free(nqx_dev->firm_gpio);
1162 gpio_free(nqx_dev->irq_gpio);
1163 gpio_free(nqx_dev->en_gpio);
1164 kfree(nqx_dev->kbuf);
1165 if (client->dev.of_node)
1166 devm_kfree(&client->dev, nqx_dev->pdata);
1167
1168 kfree(nqx_dev);
1169err:
1170 return ret;
1171}
1172
1173static int nqx_suspend(struct device *device)
1174{
1175 struct i2c_client *client = to_i2c_client(device);
1176 struct nqx_dev *nqx_dev = i2c_get_clientdata(client);
1177
1178 if (device_may_wakeup(&client->dev) && nqx_dev->irq_enabled) {
1179 if (!enable_irq_wake(client->irq))
1180 nqx_dev->irq_wake_up = true;
1181 }
1182 return 0;
1183}
1184
1185static int nqx_resume(struct device *device)
1186{
1187 struct i2c_client *client = to_i2c_client(device);
1188 struct nqx_dev *nqx_dev = i2c_get_clientdata(client);
1189
1190 if (device_may_wakeup(&client->dev) && nqx_dev->irq_wake_up) {
1191 if (!disable_irq_wake(client->irq))
1192 nqx_dev->irq_wake_up = false;
1193 }
1194 return 0;
1195}
1196
1197static const struct i2c_device_id nqx_id[] = {
1198 {"nqx-i2c", 0},
1199 {}
1200};
1201
1202static const struct dev_pm_ops nfc_pm_ops = {
1203 SET_SYSTEM_SLEEP_PM_OPS(nqx_suspend, nqx_resume)
1204};
1205
1206static struct i2c_driver nqx = {
1207 .id_table = nqx_id,
1208 .probe = nqx_probe,
1209 .remove = nqx_remove,
1210 .driver = {
1211 .owner = THIS_MODULE,
1212 .name = "nq-nci",
1213 .of_match_table = msm_match_table,
1214 .pm = &nfc_pm_ops,
1215 },
1216};
1217
1218static int nfcc_reboot(struct notifier_block *notifier, unsigned long val,
1219 void *v)
1220{
1221 gpio_set_value(disable_ctrl, 1);
1222 return NOTIFY_OK;
1223}
1224
1225/*
1226 * module load/unload record keeping
1227 */
1228static int __init nqx_dev_init(void)
1229{
1230 return i2c_add_driver(&nqx);
1231}
1232module_init(nqx_dev_init);
1233
1234static void __exit nqx_dev_exit(void)
1235{
1236 unregister_reboot_notifier(&nfcc_notifier);
1237 i2c_del_driver(&nqx);
1238}
1239module_exit(nqx_dev_exit);
1240
1241MODULE_DESCRIPTION("NFC nqx");
1242MODULE_LICENSE("GPL v2");