blob: 1d49640b6ddd04c8a64795c551918f9521ee641e [file] [log] [blame]
Tapas Dey367bf992019-01-17 16:54:23 +05301// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Copyright (c) 2015-2019, The Linux Foundation. All rights reserved.
4 */
5
6#include <linux/kernel.h>
7#include <linux/module.h>
8#include <linux/fs.h>
9#include <linux/reboot.h>
10#include <linux/slab.h>
11#include <linux/irq.h>
12#include <linux/delay.h>
13#include <linux/interrupt.h>
14#include <linux/gpio.h>
15#include <linux/spinlock.h>
16#include <linux/of_gpio.h>
17#include <linux/of_device.h>
18#include <linux/uaccess.h>
19#include "nq-nci.h"
20#include <linux/clk.h>
21#ifdef CONFIG_COMPAT
22#include <linux/compat.h>
23#endif
Karthik Poosa9681a3d2019-07-17 11:59:30 +053024#include <linux/jiffies.h>
Tapas Dey367bf992019-01-17 16:54:23 +053025
26struct nqx_platform_data {
27 unsigned int irq_gpio;
28 unsigned int en_gpio;
29 unsigned int clkreq_gpio;
30 unsigned int firm_gpio;
31 unsigned int ese_gpio;
32 const char *clk_src_name;
33 /* NFC_CLK pin voting state */
34 bool clk_pin_voting;
35};
36
37static const struct of_device_id msm_match_table[] = {
38 {.compatible = "qcom,nq-nci"},
39 {}
40};
41
42MODULE_DEVICE_TABLE(of, msm_match_table);
43
Tapas Dey367bf992019-01-17 16:54:23 +053044struct nqx_dev {
45 wait_queue_head_t read_wq;
Bhuvan Varshneydea45e32019-09-03 11:15:22 +053046 wait_queue_head_t cold_reset_read_wq;
Tapas Dey367bf992019-01-17 16:54:23 +053047 struct mutex read_mutex;
Bhuvan Varshney31df1f52019-05-20 15:57:08 +053048 struct mutex dev_ref_mutex;
Tapas Dey367bf992019-01-17 16:54:23 +053049 struct i2c_client *client;
50 dev_t devno;
51 struct class *nqx_class;
52 struct device *nqx_device;
53 struct cdev c_dev;
54 union nqx_uinfo nqx_info;
55 /* NFC GPIO variables */
56 unsigned int irq_gpio;
57 unsigned int en_gpio;
58 unsigned int firm_gpio;
59 unsigned int clkreq_gpio;
60 unsigned int ese_gpio;
61 /* NFC VEN pin state powered by Nfc */
62 bool nfc_ven_enabled;
Bhuvan Varshneydea45e32019-09-03 11:15:22 +053063 /* NFC state reflected from MW */
64 bool nfc_enabled;
Tapas Dey367bf992019-01-17 16:54:23 +053065 /* NFC_IRQ state */
66 bool irq_enabled;
67 /* NFC_IRQ wake-up state */
68 bool irq_wake_up;
Bhuvan Varshneydea45e32019-09-03 11:15:22 +053069 bool cold_reset_rsp_pending;
70 uint8_t cold_reset_status;
Tapas Dey367bf992019-01-17 16:54:23 +053071 spinlock_t irq_enabled_lock;
72 unsigned int count_irq;
Bhuvan Varshney31df1f52019-05-20 15:57:08 +053073 /* NFC_IRQ Count */
74 unsigned int dev_ref_count;
Tapas Dey367bf992019-01-17 16:54:23 +053075 /* Initial CORE RESET notification */
76 unsigned int core_reset_ntf;
77 /* CLK control */
78 bool clk_run;
79 struct clk *s_clk;
80 /* read buffer*/
81 size_t kbuflen;
82 u8 *kbuf;
83 struct nqx_platform_data *pdata;
84};
85
86static int nfcc_reboot(struct notifier_block *notifier, unsigned long val,
87 void *v);
88/*clock enable function*/
89static int nqx_clock_select(struct nqx_dev *nqx_dev);
90/*clock disable function*/
91static int nqx_clock_deselect(struct nqx_dev *nqx_dev);
Bhuvan Varshneydea45e32019-09-03 11:15:22 +053092static int nqx_standby_write(struct nqx_dev *nqx_dev,
93 const unsigned char *buf, size_t len);
94
Tapas Dey367bf992019-01-17 16:54:23 +053095static struct notifier_block nfcc_notifier = {
96 .notifier_call = nfcc_reboot,
97 .next = NULL,
98 .priority = 0
99};
100
101unsigned int disable_ctrl;
102
103static void nqx_init_stat(struct nqx_dev *nqx_dev)
104{
105 nqx_dev->count_irq = 0;
106}
107
108static void nqx_disable_irq(struct nqx_dev *nqx_dev)
109{
110 unsigned long flags;
111
112 spin_lock_irqsave(&nqx_dev->irq_enabled_lock, flags);
113 if (nqx_dev->irq_enabled) {
114 disable_irq_nosync(nqx_dev->client->irq);
115 nqx_dev->irq_enabled = false;
116 }
117 spin_unlock_irqrestore(&nqx_dev->irq_enabled_lock, flags);
118}
119
120/**
121 * nqx_enable_irq()
122 *
123 * Check if interrupt is enabled or not
124 * and enable interrupt
125 *
126 * Return: void
127 */
128static void nqx_enable_irq(struct nqx_dev *nqx_dev)
129{
130 unsigned long flags;
131
132 spin_lock_irqsave(&nqx_dev->irq_enabled_lock, flags);
133 if (!nqx_dev->irq_enabled) {
134 nqx_dev->irq_enabled = true;
135 enable_irq(nqx_dev->client->irq);
136 }
137 spin_unlock_irqrestore(&nqx_dev->irq_enabled_lock, flags);
138}
139
140static irqreturn_t nqx_dev_irq_handler(int irq, void *dev_id)
141{
142 struct nqx_dev *nqx_dev = dev_id;
143 unsigned long flags;
144
145 if (device_may_wakeup(&nqx_dev->client->dev))
146 pm_wakeup_event(&nqx_dev->client->dev, WAKEUP_SRC_TIMEOUT);
147
148 nqx_disable_irq(nqx_dev);
149 spin_lock_irqsave(&nqx_dev->irq_enabled_lock, flags);
150 nqx_dev->count_irq++;
151 spin_unlock_irqrestore(&nqx_dev->irq_enabled_lock, flags);
152 wake_up(&nqx_dev->read_wq);
153
154 return IRQ_HANDLED;
155}
156
Bhuvan Varshneydea45e32019-09-03 11:15:22 +0530157static int is_data_available_for_read(struct nqx_dev *nqx_dev)
158{
159 int ret;
160
161 nqx_enable_irq(nqx_dev);
162 ret = wait_event_interruptible_timeout(nqx_dev->read_wq,
163 !nqx_dev->irq_enabled, msecs_to_jiffies(MAX_IRQ_WAIT_TIME));
164 return ret;
165}
166
167static int send_cold_reset_cmd(struct nqx_dev *nqx_dev)
168{
169 int ret;
170 char *cold_reset_cmd = NULL;
171
172 if (gpio_get_value(nqx_dev->firm_gpio)) {
173 dev_err(&nqx_dev->client->dev, "FW download in-progress\n");
174 return -EBUSY;
175 }
176 if (!gpio_get_value(nqx_dev->en_gpio)) {
177 dev_err(&nqx_dev->client->dev, "VEN LOW - NFCC powered off\n");
178 return -ENODEV;
179 }
180 cold_reset_cmd = kzalloc(COLD_RESET_CMD_LEN, GFP_DMA | GFP_KERNEL);
181 if (!cold_reset_cmd)
182 return -ENOMEM;
183
184 cold_reset_cmd[0] = COLD_RESET_CMD_GID;
185 cold_reset_cmd[1] = COLD_RESET_OID;
186 cold_reset_cmd[2] = COLD_RESET_CMD_PAYLOAD_LEN;
187
188 ret = nqx_standby_write(nqx_dev, cold_reset_cmd, COLD_RESET_CMD_LEN);
189 if (ret < 0) {
190 dev_err(&nqx_dev->client->dev,
191 "%s: write failed after max retry\n", __func__);
192 }
193 kfree(cold_reset_cmd);
194 return ret;
195}
196
197static void read_cold_reset_rsp(struct nqx_dev *nqx_dev, bool isNfcEnabled,
198 char *header)
199{
200 int ret = -1;
201 char *cold_reset_rsp = NULL;
202
203 cold_reset_rsp = kzalloc(COLD_RESET_RSP_LEN, GFP_DMA | GFP_KERNEL);
204 if (!cold_reset_rsp)
205 return;
206
207 /*
208 * read header also if NFC is disabled
209 * for enable case, will be taken care by nfc_read thread
210 */
211 if (!isNfcEnabled) {
212 ret = i2c_master_recv(nqx_dev->client, cold_reset_rsp,
213 NCI_HEADER_LEN);
214 if (ret != NCI_HEADER_LEN) {
215 dev_err(&nqx_dev->client->dev,
216 "%s: failure to read cold reset rsp header\n",
217 __func__);
218 goto error;
219 }
220 } else {
221 memcpy(cold_reset_rsp, header, NCI_HEADER_LEN);
222 }
223
224 if ((NCI_HEADER_LEN + cold_reset_rsp[2]) > COLD_RESET_RSP_LEN) {
225 dev_err(&nqx_dev->client->dev,
226 "%s: - invalid response for cold_reset\n", __func__);
227 ret = -EINVAL;
228 goto error;
229 }
230 ret = i2c_master_recv(nqx_dev->client, &cold_reset_rsp[NCI_PAYLOAD_IDX],
231 cold_reset_rsp[2]);
232 if (ret != cold_reset_rsp[2]) {
233 dev_err(&nqx_dev->client->dev,
234 "%s: failure to read cold reset rsp status\n",
235 __func__);
236 goto error;
237 }
238 nqx_dev->cold_reset_status = cold_reset_rsp[NCI_PAYLOAD_IDX];
239error:
240 kfree(cold_reset_rsp);
241}
242
Tapas Dey367bf992019-01-17 16:54:23 +0530243static ssize_t nfc_read(struct file *filp, char __user *buf,
244 size_t count, loff_t *offset)
245{
246 struct nqx_dev *nqx_dev = filp->private_data;
247 unsigned char *tmp = NULL;
248 int ret;
249 int irq_gpio_val = 0;
250
251 if (!nqx_dev) {
252 ret = -ENODEV;
253 goto out;
254 }
255
256 if (count > nqx_dev->kbuflen)
257 count = nqx_dev->kbuflen;
258
259 dev_dbg(&nqx_dev->client->dev, "%s : reading %zu bytes.\n",
260 __func__, count);
261
262 mutex_lock(&nqx_dev->read_mutex);
263
264 irq_gpio_val = gpio_get_value(nqx_dev->irq_gpio);
265 if (irq_gpio_val == 0) {
266 if (filp->f_flags & O_NONBLOCK) {
267 dev_err(&nqx_dev->client->dev,
268 ":f_falg has O_NONBLOCK. EAGAIN\n");
269 ret = -EAGAIN;
270 goto err;
271 }
272 while (1) {
273 ret = 0;
274 if (!nqx_dev->irq_enabled) {
275 nqx_dev->irq_enabled = true;
276 enable_irq(nqx_dev->client->irq);
277 }
278 if (!gpio_get_value(nqx_dev->irq_gpio)) {
279 ret = wait_event_interruptible(nqx_dev->read_wq,
280 !nqx_dev->irq_enabled);
281 }
282 if (ret)
283 goto err;
284 nqx_disable_irq(nqx_dev);
285
286 if (gpio_get_value(nqx_dev->irq_gpio))
287 break;
288 dev_err_ratelimited(&nqx_dev->client->dev,
289 "gpio is low, no need to read data\n");
290 }
291 }
292
293 tmp = nqx_dev->kbuf;
294 if (!tmp) {
295 dev_err(&nqx_dev->client->dev,
296 "%s: device doesn't exist anymore\n", __func__);
297 ret = -ENODEV;
298 goto err;
299 }
300 memset(tmp, 0x00, count);
301
302 /* Read data */
303 ret = i2c_master_recv(nqx_dev->client, tmp, count);
304 if (ret < 0) {
305 dev_err(&nqx_dev->client->dev,
306 "%s: i2c_master_recv returned %d\n", __func__, ret);
307 goto err;
308 }
309 if (ret > count) {
310 dev_err(&nqx_dev->client->dev,
311 "%s: received too many bytes from i2c (%d)\n",
312 __func__, ret);
313 ret = -EIO;
314 goto err;
315 }
Bhuvan Varshneydea45e32019-09-03 11:15:22 +0530316 /* check if it's response of cold reset command
317 * NFC HAL process shouldn't receive this data as
318 * command was sent by eSE HAL
319 */
320 if (nqx_dev->cold_reset_rsp_pending
321 && (tmp[0] == COLD_RESET_RSP_GID)
322 && (tmp[1] == COLD_RESET_OID)) {
323 read_cold_reset_rsp(nqx_dev, true, tmp);
324 nqx_dev->cold_reset_rsp_pending = false;
325 wake_up_interruptible(&nqx_dev->cold_reset_read_wq);
326 mutex_unlock(&nqx_dev->read_mutex);
327 /*
328 * NFC process doesn't know about cold reset command
329 * being sent as it was initiated by eSE process
330 * we shouldn't return any data to NFC process
331 */
332 return 0;
333 }
Tapas Dey367bf992019-01-17 16:54:23 +0530334#ifdef NFC_KERNEL_BU
335 dev_dbg(&nqx_dev->client->dev, "%s : NfcNciRx %x %x %x\n",
336 __func__, tmp[0], tmp[1], tmp[2]);
337#endif
338 if (copy_to_user(buf, tmp, ret)) {
339 dev_warn(&nqx_dev->client->dev,
340 "%s : failed to copy to user space\n", __func__);
341 ret = -EFAULT;
342 goto err;
343 }
344 mutex_unlock(&nqx_dev->read_mutex);
345 return ret;
346
347err:
348 mutex_unlock(&nqx_dev->read_mutex);
349out:
350 return ret;
351}
352
353static ssize_t nfc_write(struct file *filp, const char __user *buf,
354 size_t count, loff_t *offset)
355{
356 struct nqx_dev *nqx_dev = filp->private_data;
357 char *tmp = NULL;
358 int ret = 0;
359
360 if (!nqx_dev) {
361 ret = -ENODEV;
362 goto out;
363 }
364 if (count > nqx_dev->kbuflen) {
365 dev_err(&nqx_dev->client->dev, "%s: out of memory\n",
366 __func__);
367 ret = -ENOMEM;
368 goto out;
369 }
370
371 tmp = memdup_user(buf, count);
372 if (IS_ERR(tmp)) {
373 dev_err(&nqx_dev->client->dev, "%s: memdup_user failed\n",
374 __func__);
375 ret = PTR_ERR(tmp);
376 goto out;
377 }
378
379 ret = i2c_master_send(nqx_dev->client, tmp, count);
380 if (ret != count) {
381 dev_err(&nqx_dev->client->dev,
382 "%s: failed to write %d\n", __func__, ret);
383 ret = -EIO;
384 goto out_free;
385 }
386#ifdef NFC_KERNEL_BU
387 dev_dbg(&nqx_dev->client->dev,
388 "%s : i2c-%d: NfcNciTx %x %x %x\n",
389 __func__, iminor(file_inode(filp)),
390 tmp[0], tmp[1], tmp[2]);
391#endif
392 usleep_range(1000, 1100);
393out_free:
394 kfree(tmp);
395out:
396 return ret;
397}
398
399/**
400 * nqx_standby_write()
401 * @buf: pointer to data buffer
402 * @len: # of bytes need to transfer
403 *
404 * write data buffer over I2C and retry
405 * if NFCC is in stand by mode
406 *
407 * Return: # of bytes written or -ve value in case of error
408 */
409static int nqx_standby_write(struct nqx_dev *nqx_dev,
410 const unsigned char *buf, size_t len)
411{
412 int ret = -EINVAL;
413 int retry_cnt;
414
415 for (retry_cnt = 1; retry_cnt <= MAX_RETRY_COUNT; retry_cnt++) {
416 ret = i2c_master_send(nqx_dev->client, buf, len);
417 if (ret < 0) {
418 dev_err(&nqx_dev->client->dev,
419 "%s: write failed, Maybe in Standby Mode - Retry(%d)\n",
420 __func__, retry_cnt);
421 usleep_range(1000, 1100);
422 } else if (ret == len)
423 break;
424 }
425 return ret;
426}
427
Bhuvan Varshneydea45e32019-09-03 11:15:22 +0530428
Tapas Dey367bf992019-01-17 16:54:23 +0530429/*
Bhuvan Varshneyf1684bb2019-01-03 21:17:45 +0530430 * Power management of the SN100 eSE
431 * eSE and NFCC both are powered using VEN gpio in SN100,
432 * VEN HIGH - eSE and NFCC both are powered on
433 * VEN LOW - eSE and NFCC both are power down
434 */
Bhuvan Varshneydea45e32019-09-03 11:15:22 +0530435
Bhuvan Varshneyf1684bb2019-01-03 21:17:45 +0530436static int sn100_ese_pwr(struct nqx_dev *nqx_dev, unsigned long arg)
437{
438 int r = -1;
439
Bhuvan Varshneydea45e32019-09-03 11:15:22 +0530440 if (arg == ESE_POWER_ON) {
Bhuvan Varshneyf1684bb2019-01-03 21:17:45 +0530441 /**
442 * Let's store the NFC VEN pin state
443 * will check stored value in case of eSE power off request,
444 * to find out if NFC MW also sent request to set VEN HIGH
445 * VEN state will remain HIGH if NFC is enabled otherwise
446 * it will be set as LOW
447 */
448 nqx_dev->nfc_ven_enabled =
449 gpio_get_value(nqx_dev->en_gpio);
450 if (!nqx_dev->nfc_ven_enabled) {
451 dev_dbg(&nqx_dev->client->dev, "eSE HAL service setting en_gpio HIGH\n");
452 gpio_set_value(nqx_dev->en_gpio, 1);
453 /* hardware dependent delay */
454 usleep_range(1000, 1100);
455 } else {
456 dev_dbg(&nqx_dev->client->dev, "en_gpio already HIGH\n");
457 }
458 r = 0;
Bhuvan Varshneydea45e32019-09-03 11:15:22 +0530459 } else if (arg == ESE_POWER_OFF) {
Bhuvan Varshneyf1684bb2019-01-03 21:17:45 +0530460 if (!nqx_dev->nfc_ven_enabled) {
461 dev_dbg(&nqx_dev->client->dev, "NFC not enabled, disabling en_gpio\n");
462 gpio_set_value(nqx_dev->en_gpio, 0);
463 /* hardware dependent delay */
464 usleep_range(1000, 1100);
465 } else {
466 dev_dbg(&nqx_dev->client->dev, "keep en_gpio high as NFC is enabled\n");
467 }
468 r = 0;
Bhuvan Varshneydea45e32019-09-03 11:15:22 +0530469 } else if (arg == ESE_COLD_RESET) {
470 // set default value for status as failure
471 nqx_dev->cold_reset_status = EIO;
472
473 r = send_cold_reset_cmd(nqx_dev);
474 if (r <= 0) {
475 dev_err(&nqx_dev->client->dev,
476 "failed to send cold reset command\n");
477 return nqx_dev->cold_reset_status;
478 }
479 nqx_dev->cold_reset_rsp_pending = true;
480 // check if NFC is enabled
481 if (nqx_dev->nfc_enabled) {
482 /*
483 * nfc_read thread will initiate cold reset response
484 * and it will signal for data available
485 */
486 wait_event_interruptible(nqx_dev->cold_reset_read_wq,
487 !nqx_dev->cold_reset_rsp_pending);
488 } else {
489 /*
490 * Read data as NFC thread is not active
491 */
492 r = is_data_available_for_read(nqx_dev);
493 if (r <= 0) {
494 nqx_disable_irq(nqx_dev);
495 nqx_dev->cold_reset_rsp_pending = false;
496 return nqx_dev->cold_reset_status;
497 }
498 read_cold_reset_rsp(nqx_dev, false, NULL);
499 nqx_dev->cold_reset_rsp_pending = false;
500 }
501 r = nqx_dev->cold_reset_status;
502 } else if (arg == ESE_POWER_STATE) {
Bhuvan Varshneyf1684bb2019-01-03 21:17:45 +0530503 // eSE power state
504 r = gpio_get_value(nqx_dev->en_gpio);
505 }
506 return r;
507}
508
509/*
Tapas Dey367bf992019-01-17 16:54:23 +0530510 * Power management of the eSE
511 * NFC & eSE ON : NFC_EN high and eSE_pwr_req high.
512 * NFC OFF & eSE ON : NFC_EN high and eSE_pwr_req high.
513 * NFC OFF & eSE OFF : NFC_EN low and eSE_pwr_req low.
514 */
515static int nqx_ese_pwr(struct nqx_dev *nqx_dev, unsigned long arg)
516{
517 int r = -1;
518 const unsigned char svdd_off_cmd_warn[] = {0x2F, 0x31, 0x01, 0x01};
519 const unsigned char svdd_off_cmd_done[] = {0x2F, 0x31, 0x01, 0x00};
520
521 if (!gpio_is_valid(nqx_dev->ese_gpio)) {
522 dev_err(&nqx_dev->client->dev,
523 "%s: ese_gpio is not valid\n", __func__);
524 return -EINVAL;
525 }
526
527 if (arg == 0) {
528 /*
529 * We want to power on the eSE and to do so we need the
530 * eSE_pwr_req pin and the NFC_EN pin to be high
531 */
532 if (gpio_get_value(nqx_dev->ese_gpio)) {
533 dev_dbg(&nqx_dev->client->dev, "ese_gpio is already high\n");
534 r = 0;
535 } else {
536 /**
537 * Let's store the NFC_EN pin state
538 * only if the eSE is not yet on
539 */
540 nqx_dev->nfc_ven_enabled =
541 gpio_get_value(nqx_dev->en_gpio);
542 if (!nqx_dev->nfc_ven_enabled) {
543 gpio_set_value(nqx_dev->en_gpio, 1);
544 /* hardware dependent delay */
545 usleep_range(1000, 1100);
546 }
547 gpio_set_value(nqx_dev->ese_gpio, 1);
548 usleep_range(1000, 1100);
549 if (gpio_get_value(nqx_dev->ese_gpio)) {
550 dev_dbg(&nqx_dev->client->dev, "ese_gpio is enabled\n");
551 r = 0;
552 }
553 }
554 } else if (arg == 1) {
555 if (nqx_dev->nfc_ven_enabled &&
556 ((nqx_dev->nqx_info.info.chip_type == NFCC_NQ_220) ||
557 (nqx_dev->nqx_info.info.chip_type == NFCC_PN66T))) {
558 /**
559 * Let's inform the CLF we're
560 * powering off the eSE
561 */
562 r = nqx_standby_write(nqx_dev, svdd_off_cmd_warn,
563 sizeof(svdd_off_cmd_warn));
564 if (r < 0) {
565 dev_err(&nqx_dev->client->dev,
566 "%s: write failed after max retry\n",
567 __func__);
568 return -ENXIO;
569 }
570 dev_dbg(&nqx_dev->client->dev,
571 "%s: svdd_off_cmd_warn sent\n", __func__);
572
573 /* let's power down the eSE */
574 gpio_set_value(nqx_dev->ese_gpio, 0);
575 dev_dbg(&nqx_dev->client->dev,
576 "%s: nqx_dev->ese_gpio set to 0\n", __func__);
577
578 /**
579 * Time needed for the SVDD capacitor
580 * to get discharged
581 */
582 usleep_range(8000, 8100);
583
584 /* Let's inform the CLF the eSE is now off */
585 r = nqx_standby_write(nqx_dev, svdd_off_cmd_done,
586 sizeof(svdd_off_cmd_done));
587 if (r < 0) {
588 dev_err(&nqx_dev->client->dev,
589 "%s: write failed after max retry\n",
590 __func__);
591 return -ENXIO;
592 }
593 dev_dbg(&nqx_dev->client->dev,
594 "%s: svdd_off_cmd_done sent\n", __func__);
595 } else {
596 /**
597 * In case the NFC is off,
598 * there's no need to send the i2c commands
599 */
600 gpio_set_value(nqx_dev->ese_gpio, 0);
601 usleep_range(1000, 1100);
602 }
603
604 if (!gpio_get_value(nqx_dev->ese_gpio)) {
605 dev_dbg(&nqx_dev->client->dev, "ese_gpio is disabled\n");
606 r = 0;
607 }
608
609 if (!nqx_dev->nfc_ven_enabled) {
610 /* hardware dependent delay */
611 usleep_range(1000, 1100);
612 dev_dbg(&nqx_dev->client->dev, "disabling en_gpio\n");
613 gpio_set_value(nqx_dev->en_gpio, 0);
614 }
615 } else if (arg == 3) {
616 r = gpio_get_value(nqx_dev->ese_gpio);
617 }
618 return r;
619}
620
621static int nfc_open(struct inode *inode, struct file *filp)
622{
623 struct nqx_dev *nqx_dev = container_of(inode->i_cdev,
624 struct nqx_dev, c_dev);
625
626 filp->private_data = nqx_dev;
627 nqx_init_stat(nqx_dev);
628
Bhuvan Varshney31df1f52019-05-20 15:57:08 +0530629 mutex_lock(&nqx_dev->dev_ref_mutex);
630
631 if (nqx_dev->dev_ref_count == 0) {
632 nqx_enable_irq(nqx_dev);
633
634 if (gpio_is_valid(nqx_dev->firm_gpio)) {
635 gpio_set_value(nqx_dev->firm_gpio, 0);
636 usleep_range(10000, 10100);
637 }
638 }
639
640 nqx_dev->dev_ref_count = nqx_dev->dev_ref_count + 1;
641
642 mutex_unlock(&nqx_dev->dev_ref_mutex);
643
Tapas Dey367bf992019-01-17 16:54:23 +0530644 dev_dbg(&nqx_dev->client->dev,
645 "%s: %d,%d\n", __func__, imajor(inode), iminor(inode));
646 return 0;
647}
648
Bhuvan Varshney31df1f52019-05-20 15:57:08 +0530649static int nfc_close(struct inode *inode, struct file *filp)
650{
651 struct nqx_dev *nqx_dev = container_of(inode->i_cdev,
652 struct nqx_dev, c_dev);
653
654 mutex_lock(&nqx_dev->dev_ref_mutex);
655
656 if (nqx_dev->dev_ref_count == 1) {
657 nqx_disable_irq(nqx_dev);
658
659 if (gpio_is_valid(nqx_dev->firm_gpio)) {
660 gpio_set_value(nqx_dev->firm_gpio, 0);
661 usleep_range(10000, 10100);
662 }
663 }
664
665 if (nqx_dev->dev_ref_count > 0)
666 nqx_dev->dev_ref_count = nqx_dev->dev_ref_count - 1;
667
668 mutex_unlock(&nqx_dev->dev_ref_mutex);
669
670 filp->private_data = NULL;
671
672 return 0;
673}
674
Tapas Dey367bf992019-01-17 16:54:23 +0530675/*
676 * nfc_ioctl_power_states() - power control
677 * @filp: pointer to the file descriptor
678 * @arg: mode that we want to move to
679 *
680 * Device power control. Depending on the arg value, device moves to
681 * different states
682 * (arg = 0): NFC_ENABLE GPIO = 0, FW_DL GPIO = 0
683 * (arg = 1): NFC_ENABLE GPIO = 1, FW_DL GPIO = 0
684 * (arg = 2): FW_DL GPIO = 1
685 *
686 * Return: -ENOIOCTLCMD if arg is not supported, 0 in any other case
687 */
688int nfc_ioctl_power_states(struct file *filp, unsigned long arg)
689{
690 int r = 0;
691 struct nqx_dev *nqx_dev = filp->private_data;
692
Bhuvan Varshneydea45e32019-09-03 11:15:22 +0530693 if (arg == NFC_POWER_OFF) {
Tapas Dey367bf992019-01-17 16:54:23 +0530694 /*
695 * We are attempting a hardware reset so let us disable
696 * interrupts to avoid spurious notifications to upper
697 * layers.
698 */
699 nqx_disable_irq(nqx_dev);
700 dev_dbg(&nqx_dev->client->dev,
701 "gpio_set_value disable: %s: info: %p\n",
702 __func__, nqx_dev);
703 if (gpio_is_valid(nqx_dev->firm_gpio)) {
704 gpio_set_value(nqx_dev->firm_gpio, 0);
705 usleep_range(10000, 10100);
706 }
707
708 if (gpio_is_valid(nqx_dev->ese_gpio)) {
709 if (!gpio_get_value(nqx_dev->ese_gpio)) {
710 dev_dbg(&nqx_dev->client->dev, "disabling en_gpio\n");
711 gpio_set_value(nqx_dev->en_gpio, 0);
712 usleep_range(10000, 10100);
713 } else {
714 dev_dbg(&nqx_dev->client->dev, "keeping en_gpio high\n");
715 }
716 } else {
717 dev_dbg(&nqx_dev->client->dev, "ese_gpio invalid, set en_gpio to low\n");
718 gpio_set_value(nqx_dev->en_gpio, 0);
719 usleep_range(10000, 10100);
720 }
721 if (nqx_dev->pdata->clk_pin_voting) {
722 r = nqx_clock_deselect(nqx_dev);
723 if (r < 0)
724 dev_err(&nqx_dev->client->dev, "unable to disable clock\n");
725 }
726 nqx_dev->nfc_ven_enabled = false;
Bhuvan Varshneydea45e32019-09-03 11:15:22 +0530727 } else if (arg == NFC_POWER_ON) {
Tapas Dey367bf992019-01-17 16:54:23 +0530728 nqx_enable_irq(nqx_dev);
729 dev_dbg(&nqx_dev->client->dev,
730 "gpio_set_value enable: %s: info: %p\n",
731 __func__, nqx_dev);
732 if (gpio_is_valid(nqx_dev->firm_gpio)) {
733 gpio_set_value(nqx_dev->firm_gpio, 0);
734 usleep_range(10000, 10100);
735 }
736 gpio_set_value(nqx_dev->en_gpio, 1);
737 usleep_range(10000, 10100);
738 if (nqx_dev->pdata->clk_pin_voting) {
739 r = nqx_clock_select(nqx_dev);
740 if (r < 0)
741 dev_err(&nqx_dev->client->dev, "unable to enable clock\n");
742 }
743 nqx_dev->nfc_ven_enabled = true;
Bhuvan Varshneydea45e32019-09-03 11:15:22 +0530744 } else if (arg == NFC_FW_DWL_VEN_TOGGLE) {
Tapas Dey367bf992019-01-17 16:54:23 +0530745 /*
746 * We are switching to Dowload Mode, toggle the enable pin
747 * in order to set the NFCC in the new mode
748 */
749 if (gpio_is_valid(nqx_dev->ese_gpio)) {
750 if (gpio_get_value(nqx_dev->ese_gpio)) {
751 dev_err(&nqx_dev->client->dev,
752 "FW download forbidden while ese is on\n");
753 return -EBUSY; /* Device or resource busy */
754 }
755 }
756 gpio_set_value(nqx_dev->en_gpio, 1);
757 usleep_range(10000, 10100);
758 if (gpio_is_valid(nqx_dev->firm_gpio)) {
759 gpio_set_value(nqx_dev->firm_gpio, 1);
760 usleep_range(10000, 10100);
761 }
762 gpio_set_value(nqx_dev->en_gpio, 0);
763 usleep_range(10000, 10100);
764 gpio_set_value(nqx_dev->en_gpio, 1);
765 usleep_range(10000, 10100);
Bhuvan Varshneydea45e32019-09-03 11:15:22 +0530766 } else if (arg == NFC_FW_DWL_HIGH) {
Tapas Dey367bf992019-01-17 16:54:23 +0530767 /*
768 * Setting firmware download gpio to HIGH for SN100U
769 * before FW download start
770 */
771 dev_dbg(&nqx_dev->client->dev, "SN100 fw gpio HIGH\n");
772 if (gpio_is_valid(nqx_dev->firm_gpio)) {
773 gpio_set_value(nqx_dev->firm_gpio, 1);
774 usleep_range(10000, 10100);
775 } else
776 dev_err(&nqx_dev->client->dev,
777 "firm_gpio is invalid\n");
Bhuvan Varshneydea45e32019-09-03 11:15:22 +0530778 } else if (arg == NFC_FW_DWL_LOW) {
Tapas Dey367bf992019-01-17 16:54:23 +0530779 /*
780 * Setting firmware download gpio to LOW for SN100U
781 * FW download finished
782 */
783 dev_dbg(&nqx_dev->client->dev, "SN100 fw gpio LOW\n");
784 if (gpio_is_valid(nqx_dev->firm_gpio)) {
785 gpio_set_value(nqx_dev->firm_gpio, 0);
786 usleep_range(10000, 10100);
787 } else {
788 dev_err(&nqx_dev->client->dev,
789 "firm_gpio is invalid\n");
790 }
Bhuvan Varshneydea45e32019-09-03 11:15:22 +0530791 } else if (arg == NFC_ENABLE) {
792 /*
793 * Setting flag true when NFC is enabled
794 */
795 nqx_dev->nfc_enabled = true;
796 } else if (arg == NFC_DISABLE) {
797 /*
798 * Setting flag false when NFC is disabled
799 */
800 nqx_dev->nfc_enabled = false;
Tapas Dey367bf992019-01-17 16:54:23 +0530801 } else {
802 r = -ENOIOCTLCMD;
803 }
804
805 return r;
806}
807
808#ifdef CONFIG_COMPAT
809static long nfc_compat_ioctl(struct file *pfile, unsigned int cmd,
810 unsigned long arg)
811{
812 long r = 0;
813
814 arg = (compat_u64)arg;
815 switch (cmd) {
816 case NFC_SET_PWR:
817 nfc_ioctl_power_states(pfile, arg);
818 break;
819 case ESE_SET_PWR:
820 nqx_ese_pwr(pfile->private_data, arg);
821 break;
822 case ESE_GET_PWR:
823 nqx_ese_pwr(pfile->private_data, 3);
824 break;
825 case SET_RX_BLOCK:
826 break;
827 case SET_EMULATOR_TEST_POINT:
828 break;
829 default:
830 r = -ENOTTY;
831 }
832 return r;
833}
834#endif
835
836/*
837 * nfc_ioctl_core_reset_ntf()
838 * @filp: pointer to the file descriptor
839 *
840 * Allows callers to determine if a CORE_RESET_NTF has arrived
841 *
842 * Return: the value of variable core_reset_ntf
843 */
844int nfc_ioctl_core_reset_ntf(struct file *filp)
845{
846 struct nqx_dev *nqx_dev = filp->private_data;
847
848 dev_dbg(&nqx_dev->client->dev, "%s: returning = %d\n", __func__,
849 nqx_dev->core_reset_ntf);
850 return nqx_dev->core_reset_ntf;
851}
852
853/*
854 * Inside nfc_ioctl_nfcc_info
855 *
856 * @brief nfc_ioctl_nfcc_info
857 *
858 * Check the NQ Chipset and firmware version details
859 */
860unsigned int nfc_ioctl_nfcc_info(struct file *filp, unsigned long arg)
861{
862 unsigned int r = 0;
863 struct nqx_dev *nqx_dev = filp->private_data;
864
865 r = nqx_dev->nqx_info.i;
866 dev_dbg(&nqx_dev->client->dev,
867 "nqx nfc : %s r = %d\n", __func__, r);
868
869 return r;
870}
871
872static long nfc_ioctl(struct file *pfile, unsigned int cmd,
873 unsigned long arg)
874{
875 int r = 0;
Bhuvan Varshneyf1684bb2019-01-03 21:17:45 +0530876 struct nqx_dev *nqx_dev = pfile->private_data;
877
878 if (!nqx_dev)
879 return -ENODEV;
Tapas Dey367bf992019-01-17 16:54:23 +0530880
881 switch (cmd) {
882 case NFC_SET_PWR:
883 r = nfc_ioctl_power_states(pfile, arg);
884 break;
885 case ESE_SET_PWR:
Bhuvan Varshneyf1684bb2019-01-03 21:17:45 +0530886 if ((nqx_dev->nqx_info.info.chip_type == NFCC_SN100_A) ||
887 (nqx_dev->nqx_info.info.chip_type == NFCC_SN100_B))
888 r = sn100_ese_pwr(nqx_dev, arg);
889 else
890 r = nqx_ese_pwr(nqx_dev, arg);
Tapas Dey367bf992019-01-17 16:54:23 +0530891 break;
892 case ESE_GET_PWR:
Bhuvan Varshneyf1684bb2019-01-03 21:17:45 +0530893 if ((nqx_dev->nqx_info.info.chip_type == NFCC_SN100_A) ||
894 (nqx_dev->nqx_info.info.chip_type == NFCC_SN100_B))
895 r = sn100_ese_pwr(nqx_dev, 3);
896 else
897 r = nqx_ese_pwr(nqx_dev, 3);
Tapas Dey367bf992019-01-17 16:54:23 +0530898 break;
899 case SET_RX_BLOCK:
900 break;
901 case SET_EMULATOR_TEST_POINT:
902 break;
903 case NFCC_INITIAL_CORE_RESET_NTF:
904 r = nfc_ioctl_core_reset_ntf(pfile);
905 break;
906 case NFCC_GET_INFO:
907 r = nfc_ioctl_nfcc_info(pfile, arg);
908 break;
909 default:
910 r = -ENOIOCTLCMD;
911 }
912 return r;
913}
914
915static const struct file_operations nfc_dev_fops = {
916 .owner = THIS_MODULE,
917 .llseek = no_llseek,
918 .read = nfc_read,
919 .write = nfc_write,
920 .open = nfc_open,
Bhuvan Varshney31df1f52019-05-20 15:57:08 +0530921 .release = nfc_close,
Tapas Dey367bf992019-01-17 16:54:23 +0530922 .unlocked_ioctl = nfc_ioctl,
923#ifdef CONFIG_COMPAT
924 .compat_ioctl = nfc_compat_ioctl
925#endif
926};
927
928/* Check for availability of NQ_ NFC controller hardware */
929static int nfcc_hw_check(struct i2c_client *client, struct nqx_dev *nqx_dev)
930{
931 int ret = 0;
932
933 int gpio_retry_count = 0;
Bhuvan Varshney31df1f52019-05-20 15:57:08 +0530934 unsigned char reset_ntf_len = 0;
Tapas Dey367bf992019-01-17 16:54:23 +0530935 unsigned int enable_gpio = nqx_dev->en_gpio;
936 char *nci_reset_cmd = NULL;
Tapas Dey367bf992019-01-17 16:54:23 +0530937 char *nci_reset_rsp = NULL;
Bhuvan Varshney31df1f52019-05-20 15:57:08 +0530938 char *nci_reset_ntf = NULL;
Tapas Dey367bf992019-01-17 16:54:23 +0530939 char *nci_get_version_cmd = NULL;
940 char *nci_get_version_rsp = NULL;
941
942 nci_reset_cmd = kzalloc(NCI_RESET_CMD_LEN + 1, GFP_DMA | GFP_KERNEL);
943 if (!nci_reset_cmd) {
944 ret = -ENOMEM;
945 goto done;
946 }
947
948 nci_reset_rsp = kzalloc(NCI_RESET_RSP_LEN + 1, GFP_DMA | GFP_KERNEL);
949 if (!nci_reset_rsp) {
950 ret = -ENOMEM;
951 goto done;
952 }
953
Bhuvan Varshney31df1f52019-05-20 15:57:08 +0530954 nci_reset_ntf = kzalloc(NCI_RESET_NTF_LEN + 1, GFP_DMA | GFP_KERNEL);
Tapas Dey5a881e22019-08-26 12:48:53 +0530955 if (!nci_reset_ntf) {
Tapas Dey367bf992019-01-17 16:54:23 +0530956 ret = -ENOMEM;
957 goto done;
958 }
959
960 nci_get_version_cmd = kzalloc(NCI_GET_VERSION_CMD_LEN + 1,
961 GFP_DMA | GFP_KERNEL);
962 if (!nci_get_version_cmd) {
963 ret = -ENOMEM;
964 goto done;
965 }
966
967 nci_get_version_rsp = kzalloc(NCI_GET_VERSION_RSP_LEN + 1,
968 GFP_DMA | GFP_KERNEL);
969 if (!nci_get_version_rsp) {
970 ret = -ENOMEM;
971 goto done;
972 }
973
974reset_enable_gpio:
975 /* making sure that the NFCC starts in a clean state. */
Bhuvan Varshney0421fe112019-06-17 18:41:54 +0530976 gpio_set_value(enable_gpio, 1);/* HPD : Enable*/
977 /* hardware dependent delay */
978 usleep_range(10000, 10100);
Tapas Dey367bf992019-01-17 16:54:23 +0530979 gpio_set_value(enable_gpio, 0);/* ULPM: Disable */
980 /* hardware dependent delay */
981 usleep_range(10000, 10100);
982 gpio_set_value(enable_gpio, 1);/* HPD : Enable*/
983 /* hardware dependent delay */
984 usleep_range(10000, 10100);
985
986 nci_reset_cmd[0] = 0x20;
987 nci_reset_cmd[1] = 0x00;
988 nci_reset_cmd[2] = 0x01;
989 nci_reset_cmd[3] = 0x00;
990 /* send NCI CORE RESET CMD with Keep Config parameters */
991 ret = i2c_master_send(client, nci_reset_cmd, NCI_RESET_CMD_LEN);
992 if (ret < 0) {
993 dev_err(&client->dev,
994 "%s: - i2c_master_send core reset Error\n", __func__);
995
996 if (gpio_is_valid(nqx_dev->firm_gpio)) {
997 gpio_set_value(nqx_dev->firm_gpio, 1);
998 usleep_range(10000, 10100);
999 }
1000 gpio_set_value(nqx_dev->en_gpio, 0);
1001 usleep_range(10000, 10100);
1002 gpio_set_value(nqx_dev->en_gpio, 1);
1003 usleep_range(10000, 10100);
1004
1005 nci_get_version_cmd[0] = 0x00;
1006 nci_get_version_cmd[1] = 0x04;
1007 nci_get_version_cmd[2] = 0xF1;
1008 nci_get_version_cmd[3] = 0x00;
1009 nci_get_version_cmd[4] = 0x00;
1010 nci_get_version_cmd[5] = 0x00;
1011 nci_get_version_cmd[6] = 0x6E;
1012 nci_get_version_cmd[7] = 0xEF;
1013 ret = i2c_master_send(client, nci_get_version_cmd,
1014 NCI_GET_VERSION_CMD_LEN);
1015
1016 if (ret < 0) {
1017 dev_err(&client->dev,
1018 "%s: - i2c_master_send get version cmd Error\n",
1019 __func__);
1020 goto err_nfcc_hw_check;
1021 }
1022 /* hardware dependent delay */
1023 usleep_range(10000, 10100);
1024
1025 ret = i2c_master_recv(client, nci_get_version_rsp,
1026 NCI_GET_VERSION_RSP_LEN);
1027 if (ret < 0) {
1028 dev_err(&client->dev,
1029 "%s: - i2c_master_recv get version rsp Error\n",
1030 __func__);
1031 goto err_nfcc_hw_check;
1032 } else {
1033 nqx_dev->nqx_info.info.chip_type =
1034 nci_get_version_rsp[3];
1035 nqx_dev->nqx_info.info.rom_version =
1036 nci_get_version_rsp[4];
1037 nqx_dev->nqx_info.info.fw_minor =
1038 nci_get_version_rsp[6];
1039 nqx_dev->nqx_info.info.fw_major =
1040 nci_get_version_rsp[7];
1041 }
1042 goto err_nfcc_reset_failed;
1043 }
Bhuvan Varshneydea45e32019-09-03 11:15:22 +05301044
1045 ret = is_data_available_for_read(nqx_dev);
Karthik Poosa9681a3d2019-07-17 11:59:30 +05301046 if (ret <= 0) {
Bhuvan Varshney0421fe112019-06-17 18:41:54 +05301047 nqx_disable_irq(nqx_dev);
1048 goto err_nfcc_hw_check;
1049 }
Tapas Dey367bf992019-01-17 16:54:23 +05301050
1051 /* Read Response of RESET command */
1052 ret = i2c_master_recv(client, nci_reset_rsp, NCI_RESET_RSP_LEN);
1053 if (ret < 0) {
1054 dev_err(&client->dev,
1055 "%s: - i2c_master_recv Error\n", __func__);
1056 gpio_retry_count = gpio_retry_count + 1;
1057 if (gpio_retry_count < MAX_RETRY_COUNT)
1058 goto reset_enable_gpio;
1059 goto err_nfcc_hw_check;
1060 }
Bhuvan Varshneydea45e32019-09-03 11:15:22 +05301061
1062 ret = is_data_available_for_read(nqx_dev);
Karthik Poosa9681a3d2019-07-17 11:59:30 +05301063 if (ret <= 0) {
Bhuvan Varshney0421fe112019-06-17 18:41:54 +05301064 nqx_disable_irq(nqx_dev);
1065 goto err_nfcc_hw_check;
1066 }
Bhuvan Varshney31df1f52019-05-20 15:57:08 +05301067
1068 /* Read Notification of RESET command */
1069 ret = i2c_master_recv(client, nci_reset_ntf, NCI_RESET_NTF_LEN);
Tapas Dey367bf992019-01-17 16:54:23 +05301070 if (ret < 0) {
1071 dev_err(&client->dev,
1072 "%s: - i2c_master_recv Error\n", __func__);
Bhuvan Varshney31df1f52019-05-20 15:57:08 +05301073 goto err_nfcc_hw_check;
Tapas Dey367bf992019-01-17 16:54:23 +05301074 }
Bhuvan Varshney31df1f52019-05-20 15:57:08 +05301075
1076 reset_ntf_len = 2 + nci_reset_ntf[2]; /*payload + len*/
1077 if (reset_ntf_len > PAYLOAD_HEADER_LENGTH) {
Tapas Dey367bf992019-01-17 16:54:23 +05301078 nqx_dev->nqx_info.info.chip_type =
Bhuvan Varshney31df1f52019-05-20 15:57:08 +05301079 nci_reset_ntf[reset_ntf_len - 3];
Tapas Dey367bf992019-01-17 16:54:23 +05301080 nqx_dev->nqx_info.info.rom_version =
Bhuvan Varshney31df1f52019-05-20 15:57:08 +05301081 nci_reset_ntf[reset_ntf_len - 2];
Tapas Dey367bf992019-01-17 16:54:23 +05301082 nqx_dev->nqx_info.info.fw_major =
Bhuvan Varshney31df1f52019-05-20 15:57:08 +05301083 nci_reset_ntf[reset_ntf_len - 1];
Tapas Dey367bf992019-01-17 16:54:23 +05301084 nqx_dev->nqx_info.info.fw_minor =
Bhuvan Varshney31df1f52019-05-20 15:57:08 +05301085 nci_reset_ntf[reset_ntf_len];
Tapas Dey367bf992019-01-17 16:54:23 +05301086 }
1087 dev_dbg(&client->dev,
1088 "%s: - nq - reset cmd answer : NfcNciRx %x %x %x\n",
1089 __func__, nci_reset_rsp[0],
1090 nci_reset_rsp[1], nci_reset_rsp[2]);
1091
1092err_nfcc_reset_failed:
1093 dev_dbg(&nqx_dev->client->dev, "NQ NFCC chip_type = %x\n",
1094 nqx_dev->nqx_info.info.chip_type);
1095 dev_dbg(&nqx_dev->client->dev, "NQ fw version = %x.%x.%x\n",
1096 nqx_dev->nqx_info.info.rom_version,
1097 nqx_dev->nqx_info.info.fw_major,
1098 nqx_dev->nqx_info.info.fw_minor);
1099
1100 switch (nqx_dev->nqx_info.info.chip_type) {
Tapas Dey367bf992019-01-17 16:54:23 +05301101 case NFCC_NQ_310:
1102 dev_dbg(&client->dev,
1103 "%s: ## NFCC == NQ310 ##\n", __func__);
1104 break;
1105 case NFCC_NQ_330:
1106 dev_dbg(&client->dev,
1107 "%s: ## NFCC == NQ330 ##\n", __func__);
1108 break;
1109 case NFCC_PN66T:
1110 dev_dbg(&client->dev,
1111 "%s: ## NFCC == PN66T ##\n", __func__);
1112 break;
Bhuvan Varshney31df1f52019-05-20 15:57:08 +05301113 case NFCC_SN100_A:
1114 case NFCC_SN100_B:
1115 dev_dbg(&client->dev,
1116 "%s: ## NFCC == SN100x ##\n", __func__);
1117 break;
Tapas Dey367bf992019-01-17 16:54:23 +05301118 default:
1119 dev_err(&client->dev,
1120 "%s: - NFCC HW not Supported\n", __func__);
1121 break;
1122 }
1123
Tapas Dey367bf992019-01-17 16:54:23 +05301124 ret = 0;
Bhuvan Varshney31df1f52019-05-20 15:57:08 +05301125 nqx_dev->nfc_ven_enabled = true;
Tapas Dey367bf992019-01-17 16:54:23 +05301126 goto done;
1127
Tapas Dey367bf992019-01-17 16:54:23 +05301128err_nfcc_hw_check:
1129 ret = -ENXIO;
1130 dev_err(&client->dev,
1131 "%s: - NFCC HW not available\n", __func__);
1132
1133done:
1134 kfree(nci_reset_rsp);
Bhuvan Varshney31df1f52019-05-20 15:57:08 +05301135 kfree(nci_reset_ntf);
Tapas Dey367bf992019-01-17 16:54:23 +05301136 kfree(nci_reset_cmd);
1137 kfree(nci_get_version_cmd);
1138 kfree(nci_get_version_rsp);
1139
1140 return ret;
1141}
1142
1143/*
1144 * Routine to enable clock.
1145 * this routine can be extended to select from multiple
1146 * sources based on clk_src_name.
1147 */
1148static int nqx_clock_select(struct nqx_dev *nqx_dev)
1149{
1150 int r = 0;
1151
1152 nqx_dev->s_clk = clk_get(&nqx_dev->client->dev, "ref_clk");
1153
1154 if (nqx_dev->s_clk == NULL)
1155 goto err_clk;
1156
1157 if (!nqx_dev->clk_run)
1158 r = clk_prepare_enable(nqx_dev->s_clk);
1159
1160 if (r)
1161 goto err_clk;
1162
1163 nqx_dev->clk_run = true;
1164
1165 return r;
1166
1167err_clk:
1168 r = -1;
1169 return r;
1170}
1171
1172/*
1173 * Routine to disable clocks
1174 */
1175static int nqx_clock_deselect(struct nqx_dev *nqx_dev)
1176{
1177 int r = -1;
1178
1179 if (nqx_dev->s_clk != NULL) {
1180 if (nqx_dev->clk_run) {
1181 clk_disable_unprepare(nqx_dev->s_clk);
1182 nqx_dev->clk_run = false;
1183 }
1184 return 0;
1185 }
1186 return r;
1187}
1188
1189static int nfc_parse_dt(struct device *dev, struct nqx_platform_data *pdata)
1190{
1191 int r = 0;
1192 struct device_node *np = dev->of_node;
1193
1194 pdata->en_gpio = of_get_named_gpio(np, "qcom,nq-ven", 0);
1195 if ((!gpio_is_valid(pdata->en_gpio)))
1196 return -EINVAL;
1197 disable_ctrl = pdata->en_gpio;
1198
1199 pdata->irq_gpio = of_get_named_gpio(np, "qcom,nq-irq", 0);
1200 if ((!gpio_is_valid(pdata->irq_gpio)))
1201 return -EINVAL;
1202
1203 pdata->firm_gpio = of_get_named_gpio(np, "qcom,nq-firm", 0);
1204 if (!gpio_is_valid(pdata->firm_gpio)) {
1205 dev_warn(dev,
1206 "FIRM GPIO <OPTIONAL> error getting from OF node\n");
1207 pdata->firm_gpio = -EINVAL;
1208 }
1209
1210 pdata->ese_gpio = of_get_named_gpio(np, "qcom,nq-esepwr", 0);
1211 if (!gpio_is_valid(pdata->ese_gpio)) {
1212 dev_warn(dev,
1213 "ese GPIO <OPTIONAL> error getting from OF node\n");
1214 pdata->ese_gpio = -EINVAL;
1215 }
1216
1217 if (of_property_read_string(np, "qcom,clk-src", &pdata->clk_src_name))
1218 pdata->clk_pin_voting = false;
1219 else
1220 pdata->clk_pin_voting = true;
1221
1222 pdata->clkreq_gpio = of_get_named_gpio(np, "qcom,nq-clkreq", 0);
1223
1224 return r;
1225}
1226
1227static inline int gpio_input_init(const struct device * const dev,
1228 const int gpio, const char * const gpio_name)
1229{
1230 int r = gpio_request(gpio, gpio_name);
1231
1232 if (r) {
1233 dev_err(dev, "unable to request gpio [%d]\n", gpio);
1234 return r;
1235 }
1236
1237 r = gpio_direction_input(gpio);
1238 if (r)
1239 dev_err(dev, "unable to set direction for gpio [%d]\n", gpio);
1240
1241 return r;
1242}
1243
1244static int nqx_probe(struct i2c_client *client,
1245 const struct i2c_device_id *id)
1246{
1247 int r = 0;
1248 int irqn = 0;
1249 struct nqx_platform_data *platform_data;
1250 struct nqx_dev *nqx_dev;
1251
1252 dev_dbg(&client->dev, "%s: enter\n", __func__);
1253 if (client->dev.of_node) {
1254 platform_data = devm_kzalloc(&client->dev,
1255 sizeof(struct nqx_platform_data), GFP_KERNEL);
1256 if (!platform_data) {
1257 r = -ENOMEM;
1258 goto err_platform_data;
1259 }
1260 r = nfc_parse_dt(&client->dev, platform_data);
1261 if (r)
1262 goto err_free_data;
1263 } else
1264 platform_data = client->dev.platform_data;
1265
1266 dev_dbg(&client->dev,
1267 "%s, inside nfc-nci flags = %x\n",
1268 __func__, client->flags);
1269
1270 if (platform_data == NULL) {
1271 dev_err(&client->dev, "%s: failed\n", __func__);
1272 r = -ENODEV;
1273 goto err_platform_data;
1274 }
1275 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
1276 dev_err(&client->dev, "%s: need I2C_FUNC_I2C\n", __func__);
1277 r = -ENODEV;
1278 goto err_free_data;
1279 }
1280 nqx_dev = kzalloc(sizeof(*nqx_dev), GFP_KERNEL);
1281 if (nqx_dev == NULL) {
1282 r = -ENOMEM;
1283 goto err_free_data;
1284 }
1285 nqx_dev->client = client;
1286 nqx_dev->kbuflen = MAX_BUFFER_SIZE;
1287 nqx_dev->kbuf = kzalloc(MAX_BUFFER_SIZE, GFP_KERNEL);
1288 if (!nqx_dev->kbuf) {
1289 dev_err(&client->dev,
1290 "failed to allocate memory for nqx_dev->kbuf\n");
1291 r = -ENOMEM;
1292 goto err_free_dev;
1293 }
1294
1295 if (gpio_is_valid(platform_data->en_gpio)) {
1296 r = gpio_request(platform_data->en_gpio, "nfc_reset_gpio");
1297 if (r) {
1298 dev_err(&client->dev,
1299 "%s: unable to request nfc reset gpio [%d]\n",
1300 __func__,
1301 platform_data->en_gpio);
1302 goto err_mem;
1303 }
1304 r = gpio_direction_output(platform_data->en_gpio, 0);
1305 if (r) {
1306 dev_err(&client->dev,
1307 "%s: unable to set direction for nfc reset gpio [%d]\n",
1308 __func__,
1309 platform_data->en_gpio);
1310 goto err_en_gpio;
1311 }
1312 } else {
1313 dev_err(&client->dev,
1314 "%s: nfc reset gpio not provided\n", __func__);
1315 goto err_mem;
1316 }
1317
1318 if (gpio_is_valid(platform_data->irq_gpio)) {
1319 r = gpio_request(platform_data->irq_gpio, "nfc_irq_gpio");
1320 if (r) {
1321 dev_err(&client->dev, "%s: unable to request nfc irq gpio [%d]\n",
1322 __func__, platform_data->irq_gpio);
1323 goto err_en_gpio;
1324 }
1325 r = gpio_direction_input(platform_data->irq_gpio);
1326 if (r) {
1327 dev_err(&client->dev,
1328 "%s: unable to set direction for nfc irq gpio [%d]\n",
1329 __func__,
1330 platform_data->irq_gpio);
1331 goto err_irq_gpio;
1332 }
1333 irqn = gpio_to_irq(platform_data->irq_gpio);
1334 if (irqn < 0) {
1335 r = irqn;
1336 goto err_irq_gpio;
1337 }
1338 client->irq = irqn;
1339 } else {
1340 dev_err(&client->dev, "%s: irq gpio not provided\n", __func__);
1341 goto err_en_gpio;
1342 }
1343 if (gpio_is_valid(platform_data->firm_gpio)) {
1344 r = gpio_request(platform_data->firm_gpio,
1345 "nfc_firm_gpio");
1346 if (r) {
1347 dev_err(&client->dev,
1348 "%s: unable to request nfc firmware gpio [%d]\n",
1349 __func__, platform_data->firm_gpio);
1350 goto err_irq_gpio;
1351 }
1352 r = gpio_direction_output(platform_data->firm_gpio, 0);
1353 if (r) {
1354 dev_err(&client->dev,
1355 "%s: cannot set direction for nfc firmware gpio [%d]\n",
1356 __func__, platform_data->firm_gpio);
1357 goto err_firm_gpio;
1358 }
1359 } else {
1360 dev_err(&client->dev,
1361 "%s: firm gpio not provided\n", __func__);
1362 goto err_irq_gpio;
1363 }
1364 if (gpio_is_valid(platform_data->ese_gpio)) {
1365 r = gpio_request(platform_data->ese_gpio,
1366 "nfc-ese_pwr");
1367 if (r) {
1368 nqx_dev->ese_gpio = -EINVAL;
1369 dev_err(&client->dev,
1370 "%s: unable to request nfc ese gpio [%d]\n",
1371 __func__, platform_data->ese_gpio);
1372 /* ese gpio optional so we should continue */
1373 } else {
1374 nqx_dev->ese_gpio = platform_data->ese_gpio;
1375 r = gpio_direction_output(platform_data->ese_gpio, 0);
1376 if (r) {
1377 /*
1378 * free ese gpio and set invalid
1379 * to avoid further use
1380 */
1381 gpio_free(platform_data->ese_gpio);
1382 nqx_dev->ese_gpio = -EINVAL;
1383 dev_err(&client->dev,
1384 "%s: cannot set direction for nfc ese gpio [%d]\n",
1385 __func__, platform_data->ese_gpio);
1386 /* ese gpio optional so we should continue */
1387 }
1388 }
1389 } else {
1390 nqx_dev->ese_gpio = -EINVAL;
1391 dev_err(&client->dev,
1392 "%s: ese gpio not provided\n", __func__);
1393 /* ese gpio optional so we should continue */
1394 }
1395 if (gpio_is_valid(platform_data->clkreq_gpio)) {
1396 r = gpio_request(platform_data->clkreq_gpio,
1397 "nfc_clkreq_gpio");
1398 if (r) {
1399 dev_err(&client->dev,
1400 "%s: unable to request nfc clkreq gpio [%d]\n",
1401 __func__, platform_data->clkreq_gpio);
1402 goto err_ese_gpio;
1403 }
1404 r = gpio_direction_input(platform_data->clkreq_gpio);
1405 if (r) {
1406 dev_err(&client->dev,
1407 "%s: cannot set direction for nfc clkreq gpio [%d]\n",
1408 __func__, platform_data->clkreq_gpio);
1409 goto err_clkreq_gpio;
1410 }
1411 } else {
1412 dev_err(&client->dev,
1413 "%s: clkreq gpio not provided\n", __func__);
1414 goto err_ese_gpio;
1415 }
1416
1417 nqx_dev->en_gpio = platform_data->en_gpio;
1418 nqx_dev->irq_gpio = platform_data->irq_gpio;
1419 nqx_dev->firm_gpio = platform_data->firm_gpio;
1420 nqx_dev->clkreq_gpio = platform_data->clkreq_gpio;
1421 nqx_dev->pdata = platform_data;
1422
1423 /* init mutex and queues */
1424 init_waitqueue_head(&nqx_dev->read_wq);
Bhuvan Varshneydea45e32019-09-03 11:15:22 +05301425 init_waitqueue_head(&nqx_dev->cold_reset_read_wq);
Tapas Dey367bf992019-01-17 16:54:23 +05301426 mutex_init(&nqx_dev->read_mutex);
Bhuvan Varshney31df1f52019-05-20 15:57:08 +05301427 mutex_init(&nqx_dev->dev_ref_mutex);
Tapas Dey367bf992019-01-17 16:54:23 +05301428 spin_lock_init(&nqx_dev->irq_enabled_lock);
1429
1430 r = alloc_chrdev_region(&nqx_dev->devno, 0, DEV_COUNT, DEVICE_NAME);
1431 if (r < 0) {
1432 dev_err(&client->dev,
1433 "%s: failed to alloc chrdev region\n", __func__);
1434 goto err_char_dev_register;
1435 }
1436
1437 nqx_dev->nqx_class = class_create(THIS_MODULE, CLASS_NAME);
1438 if (IS_ERR(nqx_dev->nqx_class)) {
1439 dev_err(&client->dev,
1440 "%s: failed to register device class\n", __func__);
1441 goto err_class_create;
1442 }
1443
1444 cdev_init(&nqx_dev->c_dev, &nfc_dev_fops);
1445 r = cdev_add(&nqx_dev->c_dev, nqx_dev->devno, DEV_COUNT);
1446 if (r < 0) {
1447 dev_err(&client->dev, "%s: failed to add cdev\n", __func__);
1448 goto err_cdev_add;
1449 }
1450
1451 nqx_dev->nqx_device = device_create(nqx_dev->nqx_class, NULL,
1452 nqx_dev->devno, nqx_dev, DEVICE_NAME);
1453 if (IS_ERR(nqx_dev->nqx_device)) {
1454 dev_err(&client->dev,
1455 "%s: failed to create the device\n", __func__);
1456 goto err_device_create;
1457 }
1458
1459 /* NFC_INT IRQ */
1460 nqx_dev->irq_enabled = true;
1461 r = request_irq(client->irq, nqx_dev_irq_handler,
1462 IRQF_TRIGGER_HIGH, client->name, nqx_dev);
1463 if (r) {
1464 dev_err(&client->dev, "%s: request_irq failed\n", __func__);
1465 goto err_request_irq_failed;
1466 }
1467 nqx_disable_irq(nqx_dev);
1468
1469 /*
1470 * To be efficient we need to test whether nfcc hardware is physically
1471 * present before attempting further hardware initialisation.
1472 *
1473 */
1474 r = nfcc_hw_check(client, nqx_dev);
1475 if (r) {
1476 /* make sure NFCC is not enabled */
1477 gpio_set_value(platform_data->en_gpio, 0);
1478 /* We don't think there is hardware switch NFC OFF */
1479 goto err_request_hw_check_failed;
1480 }
1481
1482 /* Register reboot notifier here */
1483 r = register_reboot_notifier(&nfcc_notifier);
1484 if (r) {
1485 dev_err(&client->dev,
1486 "%s: cannot register reboot notifier(err = %d)\n",
1487 __func__, r);
1488 /*
1489 * nfcc_hw_check function not doing memory
1490 * allocation so using same goto target here
1491 */
1492 goto err_request_hw_check_failed;
1493 }
1494
1495#ifdef NFC_KERNEL_BU
1496 r = nqx_clock_select(nqx_dev);
1497 if (r < 0) {
1498 dev_err(&client->dev,
1499 "%s: nqx_clock_select failed\n", __func__);
1500 goto err_clock_en_failed;
1501 }
1502 gpio_set_value(platform_data->en_gpio, 1);
1503#endif
1504 device_init_wakeup(&client->dev, true);
1505 device_set_wakeup_capable(&client->dev, true);
1506 i2c_set_clientdata(client, nqx_dev);
1507 nqx_dev->irq_wake_up = false;
Bhuvan Varshneydea45e32019-09-03 11:15:22 +05301508 nqx_dev->cold_reset_rsp_pending = false;
1509 nqx_dev->nfc_enabled = false;
Tapas Dey367bf992019-01-17 16:54:23 +05301510
1511 dev_err(&client->dev,
1512 "%s: probing NFCC NQxxx exited successfully\n",
1513 __func__);
1514 return 0;
1515
1516#ifdef NFC_KERNEL_BU
1517err_clock_en_failed:
1518 unregister_reboot_notifier(&nfcc_notifier);
1519#endif
1520err_request_hw_check_failed:
1521 free_irq(client->irq, nqx_dev);
1522err_request_irq_failed:
1523 device_destroy(nqx_dev->nqx_class, nqx_dev->devno);
1524err_device_create:
1525 cdev_del(&nqx_dev->c_dev);
1526err_cdev_add:
1527 class_destroy(nqx_dev->nqx_class);
1528err_class_create:
1529 unregister_chrdev_region(nqx_dev->devno, DEV_COUNT);
1530err_char_dev_register:
1531 mutex_destroy(&nqx_dev->read_mutex);
1532err_clkreq_gpio:
1533 gpio_free(platform_data->clkreq_gpio);
1534err_ese_gpio:
1535 /* optional gpio, not sure was configured in probe */
Bhuvan Varshney0bf56a42019-03-20 13:26:27 +05301536 if (gpio_is_valid(platform_data->ese_gpio))
Tapas Dey367bf992019-01-17 16:54:23 +05301537 gpio_free(platform_data->ese_gpio);
1538err_firm_gpio:
1539 gpio_free(platform_data->firm_gpio);
1540err_irq_gpio:
1541 gpio_free(platform_data->irq_gpio);
1542err_en_gpio:
1543 gpio_free(platform_data->en_gpio);
1544err_mem:
1545 kfree(nqx_dev->kbuf);
1546err_free_dev:
1547 kfree(nqx_dev);
1548err_free_data:
1549 if (client->dev.of_node)
1550 devm_kfree(&client->dev, platform_data);
1551err_platform_data:
1552 dev_err(&client->dev,
1553 "%s: probing nqxx failed, check hardware\n",
1554 __func__);
1555 return r;
1556}
1557
1558static int nqx_remove(struct i2c_client *client)
1559{
1560 int ret = 0;
1561 struct nqx_dev *nqx_dev;
1562
1563 nqx_dev = i2c_get_clientdata(client);
1564 if (!nqx_dev) {
1565 dev_err(&client->dev,
1566 "%s: device doesn't exist anymore\n", __func__);
1567 ret = -ENODEV;
1568 goto err;
1569 }
1570
1571 unregister_reboot_notifier(&nfcc_notifier);
1572 free_irq(client->irq, nqx_dev);
1573 cdev_del(&nqx_dev->c_dev);
1574 device_destroy(nqx_dev->nqx_class, nqx_dev->devno);
1575 class_destroy(nqx_dev->nqx_class);
1576 unregister_chrdev_region(nqx_dev->devno, DEV_COUNT);
1577 mutex_destroy(&nqx_dev->read_mutex);
Bhuvan Varshney31df1f52019-05-20 15:57:08 +05301578 mutex_destroy(&nqx_dev->dev_ref_mutex);
Tapas Dey367bf992019-01-17 16:54:23 +05301579 gpio_free(nqx_dev->clkreq_gpio);
1580 /* optional gpio, not sure was configured in probe */
1581 if (nqx_dev->ese_gpio > 0)
1582 gpio_free(nqx_dev->ese_gpio);
1583 gpio_free(nqx_dev->firm_gpio);
1584 gpio_free(nqx_dev->irq_gpio);
1585 gpio_free(nqx_dev->en_gpio);
1586 kfree(nqx_dev->kbuf);
1587 if (client->dev.of_node)
1588 devm_kfree(&client->dev, nqx_dev->pdata);
1589
1590 kfree(nqx_dev);
1591err:
1592 return ret;
1593}
1594
1595static int nqx_suspend(struct device *device)
1596{
1597 struct i2c_client *client = to_i2c_client(device);
1598 struct nqx_dev *nqx_dev = i2c_get_clientdata(client);
1599
1600 if (device_may_wakeup(&client->dev) && nqx_dev->irq_enabled) {
1601 if (!enable_irq_wake(client->irq))
1602 nqx_dev->irq_wake_up = true;
1603 }
1604 return 0;
1605}
1606
1607static int nqx_resume(struct device *device)
1608{
1609 struct i2c_client *client = to_i2c_client(device);
1610 struct nqx_dev *nqx_dev = i2c_get_clientdata(client);
1611
1612 if (device_may_wakeup(&client->dev) && nqx_dev->irq_wake_up) {
1613 if (!disable_irq_wake(client->irq))
1614 nqx_dev->irq_wake_up = false;
1615 }
1616 return 0;
1617}
1618
1619static const struct i2c_device_id nqx_id[] = {
1620 {"nqx-i2c", 0},
1621 {}
1622};
1623
1624static const struct dev_pm_ops nfc_pm_ops = {
1625 SET_SYSTEM_SLEEP_PM_OPS(nqx_suspend, nqx_resume)
1626};
1627
1628static struct i2c_driver nqx = {
1629 .id_table = nqx_id,
1630 .probe = nqx_probe,
1631 .remove = nqx_remove,
1632 .driver = {
1633 .name = "nq-nci",
1634 .of_match_table = msm_match_table,
1635 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
1636 .pm = &nfc_pm_ops,
1637 },
1638};
1639
1640static int nfcc_reboot(struct notifier_block *notifier, unsigned long val,
1641 void *v)
1642{
1643 gpio_set_value(disable_ctrl, 1);
1644 return NOTIFY_OK;
1645}
1646
1647/*
1648 * module load/unload record keeping
1649 */
1650static int __init nqx_dev_init(void)
1651{
1652 return i2c_add_driver(&nqx);
1653}
1654module_init(nqx_dev_init);
1655
1656static void __exit nqx_dev_exit(void)
1657{
1658 unregister_reboot_notifier(&nfcc_notifier);
1659 i2c_del_driver(&nqx);
1660}
1661module_exit(nqx_dev_exit);
1662
1663MODULE_DESCRIPTION("NFC nqx");
1664MODULE_LICENSE("GPL v2");