i2c_drv.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212
  1. /******************************************************************************
  2. * Copyright (C) 2015, The Linux Foundation. All rights reserved.
  3. * Copyright (C) 2013-2022 NXP
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. *
  19. ******************************************************************************/
  20. /*
  21. * Copyright (C) 2010 Trusted Logic S.A.
  22. *
  23. * This program is free software; you can redistribute it and/or modify
  24. * it under the terms of the GNU General Public License as published by
  25. * the Free Software Foundation; either version 2 of the License, or
  26. * (at your option) any later version.
  27. *
  28. * This program is distributed in the hope that it will be useful,
  29. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  30. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  31. * GNU General Public License for more details.
  32. *
  33. * You should have received a copy of the GNU General Public License
  34. * along with this program; if not, write to the Free Software
  35. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  36. */
  37. #include <linux/module.h>
  38. #include <linux/interrupt.h>
  39. #include <linux/delay.h>
  40. #include <linux/uaccess.h>
  41. #include <linux/gpio.h>
  42. #ifdef CONFIG_COMPAT
  43. #include <linux/compat.h>
  44. #endif
  45. #ifdef CONFIG_MAKE_NODE_USING_PLATFORM_DEVICE
  46. #include <linux/platform_device.h>
  47. #endif
  48. #include "common_ese.h"
  49. #include "p73.h"
  50. #if IS_ENABLED(CONFIG_SAMSUNG_NFC)
  51. #include <linux/regulator/consumer.h>
  52. #include <linux/pinctrl/consumer.h>
  53. #include "common.h"
  54. static int nfc_reboot_cb(struct notifier_block *nb,
  55. unsigned long action, void *data);
  56. #endif
  57. #ifdef FEATURE_CORE_RESET_NTF_CHECK
  58. struct nfc_core_reset_type {
  59. char name[16];
  60. int data_len;
  61. u8 err_type;
  62. };
  63. /* if Power Reset comes after NFC on, it's normal operation */
  64. struct nfc_core_reset_type core_reset[] = {
  65. {"Unrecover", 0xA, 0x00},
  66. {"Power Reset", 0x9, 0x01}, /* at new version */
  67. {"Power Reset", 0xA, 0x01}, /* at old version */
  68. {"Assert", 0x6, 0xA0},
  69. {"Clock lost", 0x6, 0xA4}
  70. };
  71. static bool is_core_reset;
  72. bool nfc_check_core_reset_type(u8 *data, int len)
  73. {
  74. int arr_size = ARRAY_SIZE(core_reset);
  75. int i;
  76. bool found = false;
  77. for (i = 0; i < arr_size; i++) {
  78. if (core_reset[i].data_len == len &&
  79. core_reset[i].err_type == data[0]) {
  80. found = true;
  81. break;
  82. }
  83. }
  84. if (found) {
  85. char info[64] = {0x0, };
  86. int j, idx = 0;
  87. idx = snprintf(info, 64, "6000%02X", len);
  88. for (j = 0; j < len; j++)
  89. idx += snprintf(info + idx, 64 - idx, "%02X", data[j]);
  90. NFC_LOG_INFO("CORE_RESET: %s (%s)\n", core_reset[i].name, info);
  91. nfc_print_status();
  92. }
  93. return found;
  94. }
  95. void nfc_check_is_core_reset_ntf(u8 *data, int len)
  96. {
  97. if (is_core_reset) {
  98. /* check payload */
  99. nfc_check_core_reset_type(data, len);
  100. is_core_reset = false;
  101. } else if (len == 3) { /* check header */
  102. /* check if it's core reset ntf */
  103. if (data[0] == 0x60 && data[1] == 0x00) {
  104. switch (data[2]) {
  105. case 0x06:
  106. case 0x0A:
  107. is_core_reset = true;
  108. break;
  109. default:
  110. is_core_reset = false;
  111. }
  112. } else {
  113. is_core_reset = false;
  114. }
  115. }
  116. }
  117. #endif
  118. /**
  119. * i2c_disable_irq()
  120. *
  121. * Check if interrupt is disabled or not
  122. * and disable interrupt
  123. *
  124. * Return: int
  125. */
  126. int i2c_disable_irq(struct nfc_dev *dev)
  127. {
  128. unsigned long flags;
  129. spin_lock_irqsave(&dev->i2c_dev.irq_enabled_lock, flags);
  130. if (dev->i2c_dev.irq_enabled) {
  131. disable_irq_nosync(dev->i2c_dev.client->irq);
  132. dev->i2c_dev.irq_enabled = false;
  133. }
  134. spin_unlock_irqrestore(&dev->i2c_dev.irq_enabled_lock, flags);
  135. return 0;
  136. }
  137. /**
  138. * i2c_enable_irq()
  139. *
  140. * Check if interrupt is enabled or not
  141. * and enable interrupt
  142. *
  143. * Return: int
  144. */
  145. int i2c_enable_irq(struct nfc_dev *dev)
  146. {
  147. unsigned long flags;
  148. spin_lock_irqsave(&dev->i2c_dev.irq_enabled_lock, flags);
  149. if (!dev->i2c_dev.irq_enabled) {
  150. dev->i2c_dev.irq_enabled = true;
  151. enable_irq(dev->i2c_dev.client->irq);
  152. }
  153. spin_unlock_irqrestore(&dev->i2c_dev.irq_enabled_lock, flags);
  154. return 0;
  155. }
  156. #if IS_ENABLED(CONFIG_SAMSUNG_NFC)
  157. void i2c_disable_clk_irq(struct nfc_dev *dev)
  158. {
  159. struct platform_configs *nfc_configs = &dev->configs;
  160. struct platform_gpio *nfc_gpio = &dev->configs.gpio;
  161. if (nfc_configs->clk_req_wake || nfc_configs->clk_req_all_trigger) {
  162. if (nfc_gpio->clk_req_irq_enabled) {
  163. disable_irq_nosync(nfc_gpio->clk_req_irq);
  164. nfc_gpio->clk_req_irq_enabled = false;
  165. }
  166. dev->clk_req_wakelock = false;
  167. }
  168. }
  169. void i2c_enable_clk_irq(struct nfc_dev *dev)
  170. {
  171. struct platform_configs *nfc_configs = &dev->configs;
  172. struct platform_gpio *nfc_gpio = &dev->configs.gpio;
  173. if (nfc_configs->clk_req_wake || nfc_configs->clk_req_all_trigger) {
  174. if (!nfc_gpio->clk_req_irq_enabled) {
  175. enable_irq(nfc_gpio->clk_req_irq);
  176. nfc_gpio->clk_req_irq_enabled = true;
  177. }
  178. }
  179. }
  180. #endif
  181. static irqreturn_t i2c_irq_handler(int irq, void *dev_id)
  182. {
  183. struct nfc_dev *nfc_dev = dev_id;
  184. #if IS_ENABLED(CONFIG_SAMSUNG_NFC)
  185. wake_lock_timeout(&nfc_dev->nfc_wake_lock, 2*HZ);
  186. #else
  187. struct i2c_dev *i2c_dev = &nfc_dev->i2c_dev;
  188. if (device_may_wakeup(&i2c_dev->client->dev))
  189. pm_wakeup_event(&i2c_dev->client->dev, WAKEUP_SRC_TIMEOUT);
  190. #endif
  191. i2c_disable_irq(nfc_dev);
  192. wake_up(&nfc_dev->read_wq);
  193. NFC_LOG_REC("irq\n");
  194. return IRQ_HANDLED;
  195. }
  196. #if IS_ENABLED(CONFIG_SAMSUNG_NFC)
  197. static irqreturn_t nfc_clk_req_irq_handler(int irq, void *dev_id)
  198. {
  199. struct nfc_dev *nfc_dev = dev_id;
  200. struct platform_gpio *nfc_gpio = &nfc_dev->configs.gpio;
  201. struct platform_configs *nfc_configs = &nfc_dev->configs;
  202. static bool nfc_clk_status;
  203. if (nfc_configs->change_clkreq_for_acpm)
  204. return IRQ_HANDLED;
  205. if (nfc_configs->clk_req_all_trigger) {
  206. if (gpio_get_value(nfc_gpio->clk_req)) {
  207. NFC_LOG_REC("clk_req 1\n");
  208. if (!wake_lock_active(&nfc_dev->nfc_clk_wake_lock))
  209. wake_lock(&nfc_dev->nfc_clk_wake_lock);
  210. if (nfc_configs->nfc_clock && !nfc_clk_status) {
  211. if (clk_prepare_enable(nfc_configs->nfc_clock)) {
  212. NFC_LOG_REC("clock enable failed\n");
  213. return IRQ_HANDLED;
  214. }
  215. nfc_clk_status = true;
  216. }
  217. } else {
  218. NFC_LOG_REC("clk_req 0\n");
  219. if (wake_lock_active(&nfc_dev->nfc_clk_wake_lock))
  220. wake_unlock(&nfc_dev->nfc_clk_wake_lock);
  221. if (nfc_configs->nfc_clock && nfc_clk_status) {
  222. clk_disable_unprepare(nfc_configs->nfc_clock);
  223. nfc_clk_status = false;
  224. }
  225. }
  226. } else {
  227. NFC_LOG_REC("clk_req w:%d\n", nfc_dev->clk_req_wakelock);
  228. if (nfc_dev->clk_req_wakelock) {
  229. nfc_dev->clk_req_wakelock = false;
  230. wake_lock_timeout(&nfc_dev->nfc_clk_wake_lock, 2*HZ);
  231. }
  232. }
  233. return IRQ_HANDLED;
  234. }
  235. #define PRINT_NFC_BUF 0
  236. #if PRINT_NFC_BUF
  237. static void print_hex_buf(const char *tag, const char *data, int len)
  238. {
  239. pr_info("%s len: %d\n", tag, len);
  240. print_hex_dump(KERN_DEBUG, tag, DUMP_PREFIX_OFFSET, 16, 8,
  241. data, len, false);
  242. }
  243. #else
  244. static void print_hex_buf(const char *tag, const char *data, int len)
  245. {
  246. do {} while (0);
  247. }
  248. #endif/*PRINT_NFC_BUF*/
  249. static void secnfc_check_screen_on_rsp(struct nfc_dev *nfc_dev,
  250. const char *buf, size_t count)
  251. {
  252. if (nfc_dev->screen_on_cmd && nfc_dev->screen_cfg) {
  253. nfc_dev->screen_on_cmd = false;
  254. nfc_dev->screen_cfg = false;
  255. NFC_LOG_INFO("scrn_on\n");
  256. }
  257. }
  258. static void secnfc_check_screen_off_rsp(struct nfc_dev *nfc_dev,
  259. const char *buf, size_t count)
  260. {
  261. if (!nfc_dev->screen_off_cmd || !nfc_dev->screen_cfg) {
  262. nfc_dev->screen_off_rsp_count = 0;
  263. return;
  264. }
  265. if (!nfc_dev->screen_off_rsp_count && count == 3/*header*/) {
  266. nfc_dev->screen_off_rsp_count++;
  267. return;
  268. }
  269. if (nfc_dev->screen_off_rsp_count == 1/*payload*/) {
  270. if (!buf[0]) {//00 or 0000
  271. if (wake_lock_active(&nfc_dev->nfc_wake_lock))
  272. wake_unlock(&nfc_dev->nfc_wake_lock);
  273. NFC_LOG_INFO("scrn_off\n");
  274. nfc_dev->screen_cfg = false;
  275. nfc_dev->screen_off_cmd = false;
  276. }
  277. nfc_dev->screen_off_rsp_count = 0;
  278. return;
  279. }
  280. nfc_dev->screen_off_rsp_count = 0;
  281. }
  282. #endif
  283. int i2c_read(struct nfc_dev *nfc_dev, char *buf, size_t count, int timeout)
  284. {
  285. int ret;
  286. struct i2c_dev *i2c_dev = &nfc_dev->i2c_dev;
  287. struct platform_gpio *nfc_gpio = &nfc_dev->configs.gpio;
  288. NFC_LOG_REC("rd: %zu\n", count);
  289. if (timeout > NCI_CMD_RSP_TIMEOUT_MS)
  290. timeout = NCI_CMD_RSP_TIMEOUT_MS;
  291. if (count > MAX_NCI_BUFFER_SIZE)
  292. count = MAX_NCI_BUFFER_SIZE;
  293. if (!gpio_get_value(nfc_gpio->irq)) {
  294. while (1) {
  295. ret = 0;
  296. i2c_enable_irq(nfc_dev);
  297. if (!gpio_get_value(nfc_gpio->irq)) {
  298. if (timeout) {
  299. ret = wait_event_interruptible_timeout(
  300. nfc_dev->read_wq,
  301. !i2c_dev->irq_enabled,
  302. msecs_to_jiffies(timeout));
  303. if (ret <= 0) {
  304. NFC_LOG_ERR("%s: timeout error\n",
  305. __func__);
  306. goto err;
  307. }
  308. } else {
  309. ret = wait_event_interruptible(
  310. nfc_dev->read_wq,
  311. !i2c_dev->irq_enabled);
  312. if (ret) {
  313. NFC_LOG_ERR("%s: err wakeup of wq\n",
  314. __func__);
  315. goto err;
  316. }
  317. }
  318. }
  319. i2c_disable_irq(nfc_dev);
  320. if (gpio_get_value(nfc_gpio->irq))
  321. break;
  322. if (!gpio_get_value(nfc_gpio->ven)) {
  323. NFC_LOG_ERR("%s: releasing read\n", __func__);
  324. ret = -EIO;
  325. goto err;
  326. }
  327. /*
  328. * NFC service wanted to close the driver so,
  329. * release the calling reader thread asap.
  330. *
  331. * This can happen in case of nfc node close call from
  332. * eSE HAL in that case the NFC HAL reader thread
  333. * will again call read system call
  334. */
  335. if (nfc_dev->release_read) {
  336. NFC_LOG_REC("%s: releasing read\n", __func__);
  337. return 0;
  338. }
  339. NFC_LOG_ERR("%s: spurious interrupt detected\n", __func__);
  340. }
  341. }
  342. memset(buf, 0x00, count);
  343. /* Read data */
  344. ret = i2c_master_recv(nfc_dev->i2c_dev.client, buf, count);
  345. if (ret <= 0) {
  346. NFC_LOG_ERR("%s: returned %d\n", __func__, ret);
  347. goto err;
  348. }
  349. #if IS_ENABLED(CONFIG_SAMSUNG_NFC)
  350. print_hex_buf("nfc_rd: ", buf, count);
  351. secnfc_check_screen_on_rsp(nfc_dev, buf, ret);
  352. secnfc_check_screen_off_rsp(nfc_dev, buf, ret);
  353. #ifdef FEATURE_CORE_RESET_NTF_CHECK
  354. nfc_check_is_core_reset_ntf(buf, ret);
  355. #endif
  356. #endif
  357. /* check if it's response of cold reset command
  358. * NFC HAL process shouldn't receive this data as
  359. * command was sent by driver
  360. */
  361. if (nfc_dev->cold_reset.rsp_pending) {
  362. if (IS_PROP_CMD_RSP(buf)) {
  363. /* Read data */
  364. ret = i2c_master_recv(nfc_dev->i2c_dev.client,
  365. &buf[NCI_PAYLOAD_IDX],
  366. buf[NCI_PAYLOAD_LEN_IDX]);
  367. if (ret <= 0) {
  368. NFC_LOG_ERR("%s: error reading cold rst/prot rsp\n",
  369. __func__);
  370. goto err;
  371. }
  372. wakeup_on_prop_rsp(nfc_dev, buf);
  373. /*
  374. * NFC process doesn't know about cold reset command
  375. * being sent as it was initiated by eSE process
  376. * we shouldn't return any data to NFC process
  377. */
  378. return 0;
  379. }
  380. }
  381. err:
  382. return ret;
  383. }
  384. #if IS_ENABLED(CONFIG_SAMSUNG_NFC)
  385. #define SCREEN_ONOFF_CMD_SZ 4
  386. #define SCREEN_SET_CFG_SZ 7
  387. static bool secnfc_check_screen_on_cmd(struct nfc_dev *nfc_dev,
  388. const char *buf, size_t count)
  389. {
  390. if (count != SCREEN_ONOFF_CMD_SZ)
  391. return false;
  392. if (buf[0] == 0x20 && buf[1] == 0x09 && buf[2] == 0x01 && buf[3] == 0x2)
  393. return true;
  394. return false;
  395. }
  396. static bool secnfc_check_screen_off_cmd(struct nfc_dev *nfc_dev,
  397. const char *buf, size_t count)
  398. {
  399. if (count != SCREEN_ONOFF_CMD_SZ)
  400. return false;
  401. if (buf[0] == 0x20 && buf[1] == 0x09 && buf[2] == 0x01 &&
  402. (buf[3] == 0x1 || buf[3] == 0x3))
  403. return true;
  404. return false;
  405. }
  406. static bool secnfc_check_screen_cfg(struct nfc_dev *nfc_dev,
  407. const char *buf, size_t count)
  408. {
  409. if (count != SCREEN_SET_CFG_SZ)
  410. return false;
  411. if (buf[0] == 0x20 && buf[1] == 0x02 && buf[2] == 0x04 &&
  412. buf[3] == 0x01 && buf[4] == 0x02 &&
  413. buf[5] == 0x01 && buf[6] == 0x00)
  414. return true;
  415. return false;
  416. }
  417. static void secnfc_configure_clk_irq_based_on_screen_onoff(struct nfc_dev *nfc_dev, bool is_screen_on)
  418. {
  419. struct platform_configs *nfc_configs = &nfc_dev->configs;
  420. if (!nfc_configs->disable_clk_irq_during_wakeup)
  421. return;
  422. if (is_screen_on)
  423. i2c_disable_clk_irq(nfc_dev);
  424. else
  425. i2c_enable_clk_irq(nfc_dev);
  426. }
  427. static void secnfc_check_screen_onoff(struct nfc_dev *nfc_dev,
  428. const char *buf, size_t count)
  429. {
  430. if (secnfc_check_screen_cfg(nfc_dev, buf, count)) {
  431. nfc_dev->screen_cfg = true;
  432. } else if (secnfc_check_screen_on_cmd(nfc_dev, buf, count)) {
  433. nfc_dev->screen_on_cmd = true;
  434. secnfc_configure_clk_irq_based_on_screen_onoff(nfc_dev, true);
  435. } else if (secnfc_check_screen_off_cmd(nfc_dev, buf, count)) {
  436. nfc_dev->screen_off_cmd = true;
  437. secnfc_configure_clk_irq_based_on_screen_onoff(nfc_dev, false);
  438. } else {
  439. nfc_dev->screen_on_cmd = false;
  440. nfc_dev->screen_off_cmd = false;
  441. nfc_dev->screen_cfg = false;
  442. }
  443. }
  444. #endif
  445. int i2c_write(struct nfc_dev *nfc_dev, const char *buf, size_t count,
  446. int max_retry_cnt)
  447. {
  448. int ret = -EINVAL;
  449. int retry_cnt;
  450. struct platform_gpio *nfc_gpio = &nfc_dev->configs.gpio;
  451. if (count > MAX_DL_BUFFER_SIZE)
  452. count = MAX_DL_BUFFER_SIZE;
  453. NFC_LOG_REC("wr: %zu\n", count);
  454. #if IS_ENABLED(CONFIG_SAMSUNG_NFC)
  455. print_hex_buf("nfc_wr: ", buf, count);
  456. secnfc_check_screen_onoff(nfc_dev, buf, count);
  457. #endif
  458. /*
  459. * Wait for any pending read for max 15ms before write
  460. * This is to avoid any packet corruption during read, when
  461. * the host cmds resets NFCC during any parallel read operation
  462. */
  463. for (retry_cnt = 1; retry_cnt <= MAX_WRITE_IRQ_COUNT; retry_cnt++) {
  464. if (gpio_get_value(nfc_gpio->irq)) {
  465. NFC_LOG_ERR("%s: irq high during write, wait\n", __func__);
  466. usleep_range(WRITE_RETRY_WAIT_TIME_US,
  467. WRITE_RETRY_WAIT_TIME_US + 100);
  468. } else {
  469. break;
  470. }
  471. if (retry_cnt == MAX_WRITE_IRQ_COUNT &&
  472. gpio_get_value(nfc_gpio->irq)) {
  473. NFC_LOG_ERR("%s: allow after maximum wait\n", __func__);
  474. }
  475. }
  476. for (retry_cnt = 1; retry_cnt <= max_retry_cnt; retry_cnt++) {
  477. ret = i2c_master_send(nfc_dev->i2c_dev.client, buf, count);
  478. if (ret <= 0) {
  479. NFC_LOG_ERR("%s: write failed ret(%d), maybe in standby\n",
  480. __func__, ret);
  481. usleep_range(WRITE_RETRY_WAIT_TIME_US,
  482. WRITE_RETRY_WAIT_TIME_US + 100);
  483. } else if (ret != count) {
  484. NFC_LOG_ERR("%s: failed to write %d\n", __func__, ret);
  485. ret = -EIO;
  486. } else if (ret == count)
  487. break;
  488. }
  489. return ret;
  490. }
  491. ssize_t nfc_i2c_dev_read(struct file *filp, char __user *buf, size_t count,
  492. loff_t *offset)
  493. {
  494. int ret;
  495. struct nfc_dev *nfc_dev = (struct nfc_dev *)filp->private_data;
  496. if (!nfc_dev) {
  497. NFC_LOG_ERR("%s: device doesn't exist anymore\n", __func__);
  498. return -ENODEV;
  499. }
  500. #if IS_ENABLED(CONFIG_SAMSUNG_NFC)
  501. if (count > MAX_NCI_BUFFER_SIZE) {
  502. //NFC_LOG_ERR("%s: too big count %u\n", __func__, count);
  503. return -EINVAL;
  504. }
  505. #endif
  506. mutex_lock(&nfc_dev->read_mutex);
  507. if (filp->f_flags & O_NONBLOCK) {
  508. ret = i2c_master_recv(nfc_dev->i2c_dev.client, nfc_dev->read_kbuf, count);
  509. NFC_LOG_ERR("%s: NONBLOCK read ret = %d\n", __func__, ret);
  510. } else {
  511. ret = i2c_read(nfc_dev, nfc_dev->read_kbuf, count, 0);
  512. }
  513. if (ret > 0) {
  514. if (copy_to_user(buf, nfc_dev->read_kbuf, ret)) {
  515. NFC_LOG_ERR("%s: failed to copy to user space\n", __func__);
  516. ret = -EFAULT;
  517. }
  518. }
  519. mutex_unlock(&nfc_dev->read_mutex);
  520. return ret;
  521. }
  522. ssize_t nfc_i2c_dev_write(struct file *filp, const char __user *buf,
  523. size_t count, loff_t *offset)
  524. {
  525. int ret;
  526. struct nfc_dev *nfc_dev = (struct nfc_dev *)filp->private_data;
  527. if (count > MAX_DL_BUFFER_SIZE)
  528. count = MAX_DL_BUFFER_SIZE;
  529. if (!nfc_dev) {
  530. NFC_LOG_ERR("%s: device doesn't exist anymore\n", __func__);
  531. return -ENODEV;
  532. }
  533. mutex_lock(&nfc_dev->write_mutex);
  534. if (copy_from_user(nfc_dev->write_kbuf, buf, count)) {
  535. NFC_LOG_ERR("%s: failed to copy from user space\n", __func__);
  536. mutex_unlock(&nfc_dev->write_mutex);
  537. return -EFAULT;
  538. }
  539. ret = i2c_write(nfc_dev, nfc_dev->write_kbuf, count, NO_RETRY);
  540. mutex_unlock(&nfc_dev->write_mutex);
  541. return ret;
  542. }
  543. static const struct file_operations nfc_i2c_dev_fops = {
  544. .owner = THIS_MODULE,
  545. .llseek = no_llseek,
  546. .read = nfc_i2c_dev_read,
  547. .write = nfc_i2c_dev_write,
  548. .open = nfc_dev_open,
  549. .flush = nfc_dev_flush,
  550. .release = nfc_dev_close,
  551. .unlocked_ioctl = nfc_dev_ioctl,
  552. #ifdef CONFIG_COMPAT
  553. .compat_ioctl = nfc_dev_compat_ioctl,
  554. #endif
  555. };
  556. #if (KERNEL_VERSION(6, 3, 0) <= LINUX_VERSION_CODE)
  557. int nfc_i2c_dev_probe(struct i2c_client *client)
  558. #else
  559. int nfc_i2c_dev_probe(struct i2c_client *client,const struct i2c_device_id *id)
  560. #endif
  561. {
  562. int ret = 0;
  563. struct nfc_dev *nfc_dev = NULL;
  564. struct i2c_dev *i2c_dev = NULL;
  565. struct platform_configs *nfc_configs = NULL;
  566. struct platform_gpio *nfc_gpio = NULL;
  567. #ifdef CONFIG_SEC_NFC_LOGGER
  568. #ifdef CONFIG_SEC_NFC_LOGGER_ADD_ACPM_LOG
  569. nfc_logger_acpm_log_init(0x0);
  570. #endif
  571. nfc_logger_register_nfc_stauts_func(nfc_print_status);
  572. #endif
  573. NFC_LOG_INFO("%s: enter\n", __func__);
  574. nfc_dev = kzalloc(sizeof(struct nfc_dev), GFP_KERNEL);
  575. if (nfc_dev == NULL) {
  576. ret = -ENOMEM;
  577. goto err;
  578. }
  579. nfc_configs = &nfc_dev->configs;
  580. nfc_gpio = &nfc_configs->gpio;
  581. /* retrieve details of gpios from dt */
  582. ret = nfc_parse_dt(&client->dev, nfc_configs, PLATFORM_IF_I2C);
  583. if (ret) {
  584. NFC_LOG_ERR("%s: failed to parse dt\n", __func__);
  585. goto err_free_nfc_dev;
  586. }
  587. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  588. NFC_LOG_ERR("%s: need I2C_FUNC_I2C\n", __func__);
  589. ret = -ENODEV;
  590. goto err_free_nfc_dev;
  591. }
  592. nfc_dev->read_kbuf = kzalloc(MAX_NCI_BUFFER_SIZE, GFP_DMA | GFP_KERNEL);
  593. if (!nfc_dev->read_kbuf) {
  594. ret = -ENOMEM;
  595. goto err_free_nfc_dev;
  596. }
  597. nfc_dev->write_kbuf = kzalloc(MAX_DL_BUFFER_SIZE, GFP_DMA | GFP_KERNEL);
  598. if (!nfc_dev->write_kbuf) {
  599. ret = -ENOMEM;
  600. goto err_free_read_kbuf;
  601. }
  602. nfc_dev->interface = PLATFORM_IF_I2C;
  603. nfc_dev->nfc_state = NFC_STATE_NCI;
  604. nfc_dev->i2c_dev.client = client;
  605. i2c_dev = &nfc_dev->i2c_dev;
  606. nfc_dev->nfc_read = i2c_read;
  607. nfc_dev->nfc_write = i2c_write;
  608. nfc_dev->nfc_enable_intr = i2c_enable_irq;
  609. nfc_dev->nfc_disable_intr = i2c_disable_irq;
  610. #if IS_ENABLED(CONFIG_SAMSUNG_NFC)
  611. nfc_dev->nfc_enable_clk_intr = i2c_enable_clk_irq;
  612. nfc_dev->nfc_disable_clk_intr = i2c_disable_clk_irq;
  613. /* reboot notifier callback */
  614. nfc_dev->reboot_nb.notifier_call = nfc_reboot_cb;
  615. register_reboot_notifier(&nfc_dev->reboot_nb);
  616. #endif
  617. #ifdef CONFIG_MAKE_NODE_USING_PLATFORM_DEVICE
  618. if (!nfc_configs->late_pvdd_en) {
  619. ret = configure_gpio(nfc_gpio->ven, GPIO_OUTPUT);
  620. if (ret) {
  621. NFC_LOG_ERR("%s: unable to request nfc reset gpio [%d]\n", __func__,
  622. nfc_gpio->ven);
  623. goto err_free_write_kbuf;
  624. }
  625. }
  626. #else
  627. ret = configure_gpio(nfc_gpio->ven, GPIO_OUTPUT);
  628. if (ret) {
  629. NFC_LOG_ERR("%s: unable to request nfc reset gpio [%d]\n", __func__,
  630. nfc_gpio->ven);
  631. goto err_free_write_kbuf;
  632. }
  633. #endif
  634. ret = configure_gpio(nfc_gpio->irq, GPIO_IRQ);
  635. if (ret <= 0) {
  636. NFC_LOG_ERR("%s: unable to request nfc irq gpio [%d]\n", __func__,
  637. nfc_gpio->irq);
  638. goto err_free_gpio;
  639. }
  640. client->irq = ret;
  641. if (gpio_is_valid(nfc_gpio->dwl_req)) {
  642. ret = configure_gpio(nfc_gpio->dwl_req, GPIO_OUTPUT);
  643. if (ret) {
  644. NFC_LOG_ERR("%s: unable to request nfc firm downl gpio [%d]\n",
  645. __func__, nfc_gpio->dwl_req);
  646. }
  647. }
  648. /* init mutex and queues */
  649. init_waitqueue_head(&nfc_dev->read_wq);
  650. mutex_init(&nfc_dev->read_mutex);
  651. mutex_init(&nfc_dev->write_mutex);
  652. mutex_init(&nfc_dev->dev_ref_mutex);
  653. spin_lock_init(&i2c_dev->irq_enabled_lock);
  654. common_ese_init(nfc_dev);
  655. #if IS_ENABLED(CONFIG_SAMSUNG_NFC)
  656. wake_lock_init(&nfc_dev->nfc_wake_lock, WAKE_LOCK_SUSPEND, "nfc_wake_lock");
  657. #endif
  658. #ifndef CONFIG_MAKE_NODE_USING_PLATFORM_DEVICE
  659. ret = nfc_misc_register(nfc_dev, &nfc_i2c_dev_fops, DEV_COUNT,
  660. NFC_CHAR_DEV_NAME, CLASS_NAME);
  661. if (ret) {
  662. NFC_LOG_ERR("%s: nfc_misc_register failed\n", __func__);
  663. goto err_mutex_destroy;
  664. }
  665. #endif
  666. /* interrupt initializations */
  667. NFC_LOG_INFO("%s: requesting IRQ %d\n", __func__, client->irq);
  668. i2c_dev->irq_enabled = true;
  669. ret = request_irq(client->irq, i2c_irq_handler, IRQF_TRIGGER_HIGH,
  670. client->name, nfc_dev);
  671. if (ret) {
  672. NFC_LOG_ERR("%s: request_irq failed\n", __func__);
  673. goto err_nfc_misc_unregister;
  674. }
  675. #if IS_ENABLED(CONFIG_SAMSUNG_NFC)
  676. if (nfc_configs->clk_req_wake || nfc_configs->clk_req_all_trigger) {
  677. unsigned long irq_flag = IRQF_TRIGGER_RISING | IRQF_ONESHOT;
  678. wake_lock_init(&nfc_dev->nfc_clk_wake_lock, WAKE_LOCK_SUSPEND, "nfc_clk_wake_lock");
  679. if (nfc_configs->clk_req_all_trigger)
  680. irq_flag |= IRQF_TRIGGER_FALLING;
  681. ret = configure_gpio(nfc_gpio->clk_req, GPIO_IRQ);
  682. if (ret <= 0) {
  683. NFC_LOG_ERR("%s: unable to request nfc clk req irq gpio [%d]\n", __func__, nfc_gpio->irq);
  684. } else {
  685. nfc_gpio->clk_req_irq = ret;
  686. NFC_LOG_INFO("clk_req IRQ num %d\n", nfc_gpio->clk_req_irq);
  687. ret = request_threaded_irq(nfc_gpio->clk_req_irq, NULL, nfc_clk_req_irq_handler,
  688. irq_flag, "pn547_clk_req", nfc_dev);
  689. if (ret)
  690. NFC_LOG_ERR("clk_req_irq failed\n");
  691. else {
  692. nfc_gpio->clk_req_irq_enabled = true;
  693. i2c_disable_clk_irq(nfc_dev);
  694. }
  695. }
  696. }
  697. if (!nfc_configs->late_pvdd_en)
  698. nfc_power_control(nfc_dev);
  699. #else
  700. gpio_set_ven(nfc_dev, 1);
  701. gpio_set_ven(nfc_dev, 0);
  702. gpio_set_ven(nfc_dev, 1);
  703. #endif
  704. i2c_disable_irq(nfc_dev);
  705. i2c_set_clientdata(client, nfc_dev);
  706. i2c_dev->irq_wake_up = false;
  707. #if IS_ENABLED(CONFIG_SAMSUNG_NFC)
  708. #ifdef CONFIG_CLK_ACPM_INIT
  709. if (nfc_configs->change_clkreq_for_acpm) {
  710. NFC_LOG_INFO("acpm_init_nfc_clk_req clk_req : %d\n", nfc_gpio->clk_req);
  711. acpm_init_eint_nfc_clk_req(nfc_gpio->clk_req);
  712. }
  713. #endif
  714. #if IS_ENABLED(CONFIG_NFC_SN2XX_ESE_SUPPORT)
  715. store_nfc_i2c_device(&client->dev);
  716. #endif
  717. nfc_probe_done(nfc_dev);
  718. #endif
  719. NFC_LOG_INFO("%s: probing nfc i2c successfully\n", __func__);
  720. return 0;
  721. err_nfc_misc_unregister:
  722. #ifndef CONFIG_MAKE_NODE_USING_PLATFORM_DEVICE
  723. nfc_misc_unregister(nfc_dev, DEV_COUNT);
  724. err_mutex_destroy:
  725. #endif
  726. mutex_destroy(&nfc_dev->dev_ref_mutex);
  727. mutex_destroy(&nfc_dev->read_mutex);
  728. mutex_destroy(&nfc_dev->write_mutex);
  729. err_free_gpio:
  730. gpio_free_all(nfc_dev);
  731. err_free_write_kbuf:
  732. kfree(nfc_dev->write_kbuf);
  733. err_free_read_kbuf:
  734. kfree(nfc_dev->read_kbuf);
  735. err_free_nfc_dev:
  736. kfree(nfc_dev);
  737. err:
  738. NFC_LOG_ERR("%s: probing not successful, check hardware\n", __func__);
  739. return ret;
  740. }
  741. #if LINUX_VERSION_CODE < KERNEL_VERSION(6, 1, 0)
  742. int nfc_i2c_dev_remove(struct i2c_client *client)
  743. #else
  744. void nfc_i2c_dev_remove(struct i2c_client *client)
  745. #endif
  746. {
  747. int ret = 0;
  748. struct nfc_dev *nfc_dev = NULL;
  749. NFC_LOG_INFO("%s: remove device\n", __func__);
  750. nfc_dev = i2c_get_clientdata(client);
  751. if (!nfc_dev) {
  752. NFC_LOG_ERR("%s: device doesn't exist anymore\n", __func__);
  753. ret = -ENODEV;
  754. goto end;
  755. }
  756. if (nfc_dev->dev_ref_count > 0) {
  757. NFC_LOG_ERR("%s: device already in use\n", __func__);
  758. ret = -EBUSY;
  759. goto end;
  760. }
  761. free_irq(client->irq, nfc_dev);
  762. #ifdef CONFIG_MAKE_NODE_USING_PLATFORM_DEVICE
  763. nfc_misc_unregister(NULL, DEV_COUNT);
  764. #else
  765. nfc_misc_unregister(nfc_dev, DEV_COUNT);
  766. #endif
  767. #ifdef CONFIG_SEC_NFC_LOGGER
  768. nfc_logger_deinit();
  769. #endif
  770. wake_lock_destroy(&nfc_dev->nfc_wake_lock);
  771. mutex_destroy(&nfc_dev->read_mutex);
  772. mutex_destroy(&nfc_dev->write_mutex);
  773. gpio_free_all(nfc_dev);
  774. kfree(nfc_dev->read_kbuf);
  775. kfree(nfc_dev->write_kbuf);
  776. kfree(nfc_dev);
  777. end:
  778. NFC_LOG_INFO("%s: ret :%d\n", __func__, ret);
  779. #if LINUX_VERSION_CODE < KERNEL_VERSION(6, 1, 0)
  780. return ret;
  781. #endif
  782. }
  783. int nfc_i2c_dev_suspend(struct device *device)
  784. {
  785. struct i2c_client *client = to_i2c_client(device);
  786. struct nfc_dev *nfc_dev = i2c_get_clientdata(client);
  787. struct i2c_dev *i2c_dev = NULL;
  788. #if IS_ENABLED(CONFIG_SAMSUNG_NFC)
  789. struct platform_configs *nfc_configs = &nfc_dev->configs;
  790. struct platform_gpio *nfc_gpio = &nfc_dev->configs.gpio;
  791. #endif
  792. NFC_LOG_INFO_WITH_DATE("suspend\n");
  793. if (!nfc_dev) {
  794. NFC_LOG_ERR("%s: device doesn't exist anymore\n", __func__);
  795. return -ENODEV;
  796. }
  797. i2c_dev = &nfc_dev->i2c_dev;
  798. #if IS_ENABLED(CONFIG_SAMSUNG_NFC)
  799. if (i2c_dev->irq_enabled && !i2c_dev->irq_wake_up) {
  800. if (!enable_irq_wake(client->irq))
  801. i2c_dev->irq_wake_up = true;
  802. }
  803. if (nfc_configs->clk_req_wake || nfc_configs->clk_req_all_trigger) {
  804. enable_irq_wake(nfc_gpio->clk_req_irq);
  805. nfc_dev->clk_req_wakelock = true;
  806. }
  807. #else
  808. if (device_may_wakeup(&client->dev) && i2c_dev->irq_enabled) {
  809. if (!enable_irq_wake(client->irq))
  810. i2c_dev->irq_wake_up = true;
  811. }
  812. #endif
  813. NFC_LOG_DBG("%s: irq_wake_up = %d\n", __func__, i2c_dev->irq_wake_up);
  814. return 0;
  815. }
  816. int nfc_i2c_dev_resume(struct device *device)
  817. {
  818. struct i2c_client *client = to_i2c_client(device);
  819. struct nfc_dev *nfc_dev = i2c_get_clientdata(client);
  820. struct i2c_dev *i2c_dev = NULL;
  821. #if IS_ENABLED(CONFIG_SAMSUNG_NFC)
  822. struct platform_configs *nfc_configs = &nfc_dev->configs;
  823. struct platform_gpio *nfc_gpio = &nfc_dev->configs.gpio;
  824. #endif
  825. NFC_LOG_INFO_WITH_DATE("resume\n");
  826. if (!nfc_dev) {
  827. NFC_LOG_ERR("%s: device doesn't exist anymore\n", __func__);
  828. return -ENODEV;
  829. }
  830. i2c_dev = &nfc_dev->i2c_dev;
  831. #if IS_ENABLED(CONFIG_SAMSUNG_NFC)
  832. if (i2c_dev->irq_wake_up) {
  833. if (!disable_irq_wake(client->irq))
  834. i2c_dev->irq_wake_up = false;
  835. }
  836. if (nfc_configs->clk_req_wake || nfc_configs->clk_req_all_trigger)
  837. disable_irq_wake(nfc_gpio->clk_req_irq);
  838. #else
  839. if (device_may_wakeup(&client->dev) && i2c_dev->irq_wake_up) {
  840. if (!disable_irq_wake(client->irq))
  841. i2c_dev->irq_wake_up = false;
  842. }
  843. #endif
  844. NFC_LOG_DBG("%s: irq_wake_up = %d\n", __func__, i2c_dev->irq_wake_up);
  845. return 0;
  846. }
  847. #if IS_ENABLED(CONFIG_SAMSUNG_NFC)
  848. #define SN300_PVDD_MIN 1100000
  849. #define SN300_PVDD_MAX 1300000
  850. static int nfc_pvdd_off(struct nfc_dev *nfc_dev)
  851. {
  852. struct platform_configs *nfc_configs;
  853. int ret = 0, pvdd_val;
  854. // NFC_LOG_INFO("nfc_pvdd_off ++\n");
  855. nfc_configs = &nfc_dev->configs;
  856. pvdd_val = regulator_get_voltage(nfc_configs->nfc_pvdd);
  857. NFC_LOG_INFO("pvdd_val %d\n", pvdd_val);
  858. if (SN300_PVDD_MIN < pvdd_val && pvdd_val < SN300_PVDD_MAX) {
  859. // NFC_LOG_INFO("nfc IO force off\n");
  860. ret = nfc_regulator_onoff(nfc_dev, 0);
  861. if (ret < 0)
  862. NFC_LOG_ERR("%s pn547 regulator_on FAIL %d\n", __func__, ret);
  863. }
  864. // NFC_LOG_INFO("nfc_pvdd_off --\n");
  865. //exit:
  866. return ret;
  867. }
  868. static int nfc_reboot_cb(struct notifier_block *nb,
  869. unsigned long action, void *data)
  870. {
  871. struct nfc_dev *nfc_dev = container_of(nb, struct nfc_dev, reboot_nb);
  872. struct platform_gpio *nfc_gpio;
  873. struct platform_configs *nfc_configs;
  874. int ret;
  875. if (!nfc_dev) {
  876. NFC_LOG_INFO("no nfc dev\n");
  877. goto exit;
  878. }
  879. NFC_LOG_INFO("nfc_reboot_cb ++\n");
  880. nfc_gpio = &nfc_dev->configs.gpio;
  881. nfc_configs = &nfc_dev->configs;
  882. if (nfc_configs->ap_vendor == AP_VENDOR_QCT) {
  883. ret = gpio_direction_output(nfc_gpio->ven, 0);
  884. if (ret)
  885. NFC_LOG_ERR("VEN control fail %d\n", ret);
  886. usleep_range(300, 301);
  887. nfc_pvdd_off(nfc_dev);
  888. }
  889. NFC_LOG_INFO("nfc_reboot_cb --\n");
  890. exit:
  891. return NOTIFY_OK;
  892. }
  893. static void nfc_shutdown(struct i2c_client *client)
  894. {
  895. struct nfc_dev *nfc_dev = i2c_get_clientdata(client);
  896. int ret;
  897. NFC_LOG_INFO("nfc_shutdown ++\n");
  898. if (nfc_dev) {
  899. struct platform_gpio *nfc_gpio = &nfc_dev->configs.gpio;
  900. struct platform_configs *nfc_configs = &nfc_dev->configs;
  901. /*
  902. * In case of S.LSI, NFC card mode doesn't work in LPM.
  903. * so, it's skipped on S.LSI models using sn110. it need to be checked
  904. */
  905. if (nfc_configs->ap_vendor != AP_VENDOR_SLSI) {
  906. if (nfc_configs->ap_vendor == AP_VENDOR_MTK) {
  907. /*use internal pull-up case*/
  908. struct pinctrl *pinctrl = NULL;
  909. pinctrl = devm_pinctrl_get_select(&client->dev, "i2c_off");
  910. if (IS_ERR_OR_NULL(pinctrl))
  911. NFC_LOG_ERR("Failed to pinctrl i2c_off\n");
  912. else
  913. devm_pinctrl_put(pinctrl);
  914. NFC_LOG_ERR("i2c off pinctrl called\n");
  915. };
  916. ret = gpio_direction_output(nfc_gpio->ven, 0);
  917. if (ret)
  918. NFC_LOG_ERR("VEN control fail %d\n", ret);
  919. if (nfc_configs->ap_vendor == AP_VENDOR_QCT) {
  920. usleep_range(300, 301);
  921. nfc_pvdd_off(nfc_dev);
  922. }
  923. }
  924. }
  925. NFC_LOG_INFO("nfc_shutdown --\n");
  926. }
  927. #endif
  928. #ifdef CONFIG_MAKE_NODE_USING_PLATFORM_DEVICE
  929. static int nfc_platform_probe(struct platform_device *pdev)
  930. {
  931. int ret = -1;
  932. nfc_parse_dt_for_platform_device(&pdev->dev);
  933. ret = nfc_misc_register(NULL, &nfc_i2c_dev_fops, DEV_COUNT,
  934. NFC_CHAR_DEV_NAME, CLASS_NAME);
  935. if (ret)
  936. NFC_LOG_ERR("%s: nfc_misc_register failed\n", __func__);
  937. NFC_LOG_INFO("%s: finished...\n", __func__);
  938. return 0;
  939. }
  940. static int nfc_platform_remove(struct platform_device *pdev)
  941. {
  942. NFC_LOG_INFO("Entry : %s\n", __func__);
  943. return 0;
  944. }
  945. static const struct of_device_id nfc_platform_match_table[] = {
  946. { .compatible = "nfc_platform",},
  947. {},
  948. };
  949. static struct platform_driver nfc_platform_driver = {
  950. .driver = {
  951. .name = "nfc_platform",
  952. .owner = THIS_MODULE,
  953. #ifdef CONFIG_OF
  954. .of_match_table = nfc_platform_match_table,
  955. #endif
  956. },
  957. .probe = nfc_platform_probe,
  958. .remove = nfc_platform_remove,
  959. };
  960. #endif /* CONFIG_MAKE_NODE_USING_PLATFORM_DEVICE */
  961. static const struct i2c_device_id nfc_i2c_dev_id[] = {{NFC_I2C_DEV_ID, 0},
  962. {}};
  963. static const struct of_device_id nfc_i2c_dev_match_table[] = {
  964. {
  965. .compatible = NFC_I2C_DRV_STR,
  966. },
  967. {}
  968. };
  969. static const struct dev_pm_ops nfc_i2c_dev_pm_ops = { SET_SYSTEM_SLEEP_PM_OPS(
  970. nfc_i2c_dev_suspend, nfc_i2c_dev_resume) };
  971. static struct i2c_driver nfc_i2c_dev_driver = {
  972. .id_table = nfc_i2c_dev_id,
  973. .probe = nfc_i2c_dev_probe,
  974. .remove = nfc_i2c_dev_remove,
  975. #if IS_ENABLED(CONFIG_SAMSUNG_NFC)
  976. .shutdown = nfc_shutdown,
  977. #endif
  978. .driver = {
  979. .name = NFC_I2C_DRV_STR,
  980. .pm = &nfc_i2c_dev_pm_ops,
  981. .of_match_table = nfc_i2c_dev_match_table,
  982. .probe_type = PROBE_PREFER_ASYNCHRONOUS,
  983. },
  984. };
  985. MODULE_DEVICE_TABLE(of, nfc_i2c_dev_match_table);
  986. #if IS_MODULE(CONFIG_SAMSUNG_NFC)
  987. extern int p61_dev_init(void);
  988. extern void p61_dev_exit(void);
  989. static int __init nfc_i2c_dev_init(void)
  990. {
  991. int ret = 0;
  992. #ifdef CONFIG_SEC_NFC_LOGGER
  993. nfc_logger_init();
  994. nfc_logger_set_max_count(-1);
  995. #endif
  996. NFC_LOG_INFO("%s: Loading NXP NFC I2C driver\n", __func__);
  997. #if IS_ENABLED(CONFIG_SAMSUNG_NFC)
  998. if (nfc_get_lpcharge() == LPM_TRUE)
  999. return ret;
  1000. #endif
  1001. #ifdef CONFIG_MAKE_NODE_USING_PLATFORM_DEVICE
  1002. ret = platform_driver_register(&nfc_platform_driver);
  1003. NFC_LOG_INFO("%s: platform_driver_register, ret %d\n", __func__, ret);
  1004. #endif
  1005. ret = i2c_add_driver(&nfc_i2c_dev_driver);
  1006. if (ret != 0)
  1007. NFC_LOG_ERR("%s: NFC I2C add driver error ret %d\n", __func__, ret);
  1008. #if IS_ENABLED(CONFIG_NFC_SN2XX_ESE_SUPPORT)
  1009. ret = p61_dev_init();
  1010. #endif
  1011. return ret;
  1012. }
  1013. module_init(nfc_i2c_dev_init);
  1014. static void __exit nfc_i2c_dev_exit(void)
  1015. {
  1016. NFC_LOG_INFO("%s: Unloading NXP NFC I2C driver\n", __func__);
  1017. #if IS_ENABLED(CONFIG_SAMSUNG_NFC)
  1018. if (nfc_get_lpcharge() == LPM_TRUE)
  1019. return;
  1020. #endif
  1021. #if IS_ENABLED(CONFIG_NFC_SN2XX_ESE_SUPPORT)
  1022. p61_dev_exit();
  1023. #endif
  1024. i2c_del_driver(&nfc_i2c_dev_driver);
  1025. #ifdef CONFIG_MAKE_NODE_USING_PLATFORM_DEVICE
  1026. platform_driver_unregister(&nfc_platform_driver);
  1027. #endif
  1028. }
  1029. module_exit(nfc_i2c_dev_exit);
  1030. #else /* not IS_MODULE(CONFIG_SAMSUNG_NFC) */
  1031. static int __init nfc_i2c_dev_init(void)
  1032. {
  1033. int ret = 0;
  1034. #ifdef CONFIG_SEC_NFC_LOGGER
  1035. nfc_logger_init();
  1036. nfc_logger_set_max_count(-1);
  1037. #endif
  1038. NFC_LOG_INFO("%s: Loading NXP NFC I2C driver\n", __func__);
  1039. #if IS_ENABLED(CONFIG_SAMSUNG_NFC)
  1040. if (nfc_get_lpcharge() == LPM_TRUE)
  1041. return ret;
  1042. #endif
  1043. #ifdef CONFIG_MAKE_NODE_USING_PLATFORM_DEVICE
  1044. ret = platform_driver_register(&nfc_platform_driver);
  1045. NFC_LOG_INFO("%s: platform_driver_register, ret %d\n", __func__, ret);
  1046. #endif
  1047. ret = i2c_add_driver(&nfc_i2c_dev_driver);
  1048. if (ret != 0)
  1049. NFC_LOG_ERR("%s: NFC I2C add driver error ret %d\n", __func__, ret);
  1050. return ret;
  1051. }
  1052. module_init(nfc_i2c_dev_init);
  1053. static void __exit nfc_i2c_dev_exit(void)
  1054. {
  1055. NFC_LOG_INFO("%s: Unloading NXP NFC I2C driver\n", __func__);
  1056. #if IS_ENABLED(CONFIG_SAMSUNG_NFC)
  1057. if (nfc_get_lpcharge() == LPM_TRUE)
  1058. return;
  1059. #endif
  1060. i2c_del_driver(&nfc_i2c_dev_driver);
  1061. #ifdef CONFIG_MAKE_NODE_USING_PLATFORM_DEVICE
  1062. platform_driver_unregister(&nfc_platform_driver);
  1063. #endif
  1064. }
  1065. module_exit(nfc_i2c_dev_exit);
  1066. #endif /* IS_MODULE(CONFIG_SAMSUNG_NFC) */
  1067. MODULE_DESCRIPTION("NXP NFC I2C driver");
  1068. MODULE_LICENSE("GPL");