p73.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881
  1. /******************************************************************************
  2. *
  3. * Copyright 2012-2023 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. * \addtogroup spi_driver
  22. *
  23. * @{ */
  24. #include <linux/kernel.h>
  25. #include <linux/module.h>
  26. #include <linux/fs.h>
  27. #include <linux/slab.h>
  28. #include <linux/init.h>
  29. #include <linux/list.h>
  30. #include <linux/irq.h>
  31. #include <linux/jiffies.h>
  32. #include <linux/uaccess.h>
  33. #include <linux/delay.h>
  34. #include <linux/interrupt.h>
  35. #include <linux/io.h>
  36. #include <linux/platform_device.h>
  37. #include <linux/gpio.h>
  38. #include <linux/of_gpio.h>
  39. #include <linux/miscdevice.h>
  40. #include <linux/spinlock.h>
  41. #include <linux/spi/spi.h>
  42. #include <linux/sched.h>
  43. #include <linux/poll.h>
  44. #include <linux/ktime.h>
  45. #include <linux/regulator/consumer.h>
  46. #include <linux/pinctrl/consumer.h>
  47. #include <linux/spi/spidev.h>
  48. #include <linux/of_platform.h>
  49. #if IS_ENABLED(CONFIG_SAMSUNG_NFC)
  50. #include <linux/rcupdate.h>
  51. #if IS_ENABLED(CONFIG_SPI_MSM_GENI)
  52. #include "nfc_wakelock.h"
  53. #if IS_ENABLED(CONFIG_MSM_GENI_SE)
  54. #include <linux/msm-geni-se.h>
  55. #include <linux/msm_gpi.h>
  56. #endif
  57. #endif
  58. #endif
  59. #include "p73.h"
  60. #include "common_ese.h"
  61. #include "ese_reset.h"
  62. #define DRAGON_P61 0
  63. /* Device driver's configuration macro */
  64. /* Macro to configure poll/interrupt based req*/
  65. #undef P61_IRQ_ENABLE
  66. //#define P61_IRQ_ENABLE
  67. /* Macro to configure Hard/Soft reset to P61 */
  68. //#define P61_HARD_RESET
  69. #undef P61_HARD_RESET
  70. #ifdef P61_HARD_RESET
  71. static struct regulator *p61_regulator = NULL;
  72. #else
  73. #endif
  74. #define P61_IRQ 33 /* this is the same used in omap3beagle.c */
  75. #define P61_RST 138
  76. /* Macro to define SPI clock frequency */
  77. //#define P61_SPI_CLOCK_7Mzh
  78. #undef P61_SPI_CLOCK_7Mzh
  79. #undef P61_SPI_CLOCK_13_3_Mzh
  80. #undef P61_SPI_CLOCK_8Mzh
  81. #undef P61_SPI_CLOCK_20Mzh
  82. #define P61_SPI_CLOCK_25Mzh
  83. #ifdef P61_SPI_CLOCK_13_3_Mzh
  84. //#define P61_SPI_CLOCK 13300000L;Further debug needed
  85. #define P61_SPI_CLOCK 19000000L;
  86. #else
  87. #ifdef P61_SPI_CLOCK_7Mzh
  88. #define P61_SPI_CLOCK 7000000L;
  89. #else
  90. #ifdef P61_SPI_CLOCK_8Mzh
  91. #define P61_SPI_CLOCK 8000000L;
  92. #else
  93. #ifdef P61_SPI_CLOCK_20Mzh
  94. #define P61_SPI_CLOCK 20000000L;
  95. #else
  96. #ifdef P61_SPI_CLOCK_25Mzh
  97. #define P61_SPI_CLOCK 25000000L;
  98. #else
  99. #define P61_SPI_CLOCK 4000000L;
  100. #endif
  101. #endif
  102. #endif
  103. #endif
  104. #endif
  105. /* size of maximum read/write buffer supported by driver */
  106. #ifdef MAX_BUFFER_SIZE
  107. #undef MAX_BUFFER_SIZE
  108. #endif //MAX_BUFFER_SIZE
  109. #define MAX_BUFFER_SIZE 780U
  110. /* Different driver debug lever */
  111. enum P61_DEBUG_LEVEL {
  112. P61_DEBUG_OFF,
  113. P61_FULL_DEBUG
  114. };
  115. #if IS_ENABLED(CONFIG_SAMSUNG_NFC)
  116. static char *p61_pinctrl_name[] = {"ese_off", "ese_on", "lpm", "default", "active", "suspend"};
  117. enum p61_pin_ctrl {
  118. P61_PIN_CTRL_ESE_OFF,
  119. P61_PIN_CTRL_ESE_ON,
  120. P61_PIN_CTRL_LPM,
  121. P61_PIN_CTRL_DEFAULT,
  122. P61_PIN_CTRL_ACTIVE,
  123. P61_PIN_CTRL_SUSPEND,
  124. P61_PIN_CTRL_MAX
  125. };
  126. #endif
  127. /* Variable to store current debug level request by ioctl */
  128. static unsigned char debug_level;
  129. static DEFINE_MUTEX(open_close_mutex);
  130. #define P61_DBG_MSG(msg...) \
  131. switch(debug_level) \
  132. { \
  133. case P61_DEBUG_OFF: \
  134. break; \
  135. case P61_FULL_DEBUG: \
  136. printk(KERN_INFO "[NXP-P61] : " msg); \
  137. break; \
  138. default: \
  139. printk(KERN_ERR "[NXP-P61] : Wrong debug level %d", debug_level); \
  140. break; \
  141. } \
  142. #define P61_ERR_MSG(msg...) printk(KERN_ERR "[NFC-P61] : " msg );
  143. /* Device specific macro and structure */
  144. struct p61_dev {
  145. wait_queue_head_t read_wq; /* wait queue for read interrupt */
  146. struct mutex read_mutex; /* read mutex */
  147. struct mutex write_mutex; /* write mutex */
  148. struct spi_device *spi; /* spi device structure */
  149. struct miscdevice p61_device; /* char device as misc driver */
  150. int rst_gpio; /* SW Reset gpio */
  151. int irq_gpio; /* P61 will interrupt DH for any ntf */
  152. bool irq_enabled; /* flag to indicate irq is used */
  153. unsigned char enable_poll_mode; /* enable the poll mode */
  154. spinlock_t irq_enabled_lock; /*spin lock for read irq */
  155. int trusted_ese_gpio; /* i/p to eSE to distunguish trusted Session */
  156. ese_spi_transition_state_t ese_spi_transition_state; /* State of the driver */
  157. struct device *nfcc_device; /*nfcc driver handle for driver to driver comm */
  158. struct nfc_dev *nfcc_data;
  159. const char *nfcc_name;
  160. bool gpio_coldreset;
  161. #if IS_ENABLED(CONFIG_SAMSUNG_NFC)
  162. int ap_vendor;
  163. struct device_node *nfc_node;
  164. struct pinctrl *pinctrl;
  165. struct pinctrl_state *pinctrl_state[P61_PIN_CTRL_MAX];
  166. struct platform_device *spi_pdev;
  167. struct nfc_wake_lock ese_lock;
  168. int open_pid;
  169. int release_pid;
  170. char open_task_name[TASK_COMM_LEN];
  171. char release_task_name[TASK_COMM_LEN];
  172. #if IS_ENABLED(CONFIG_SPI_MSM_GENI)
  173. struct delayed_work spi_release_work;
  174. struct nfc_wake_lock spi_release_wakelock;
  175. #endif
  176. #endif
  177. unsigned char *r_buf;
  178. unsigned char *w_buf;
  179. };
  180. #if IS_ENABLED(CONFIG_SAMSUNG_NFC)
  181. struct device *g_nfc_device;
  182. struct p61_dev *g_p61_dev;
  183. #endif
  184. /* T==1 protocol specific global data */
  185. const unsigned char SOF = 0xA5u;
  186. #if IS_ENABLED(CONFIG_SAMSUNG_NFC)
  187. static void p61_get_task_info(struct p61_dev *p61_dev, char *task_name, int *pid)
  188. {
  189. struct task_struct *task;
  190. if (!p61_dev)
  191. return;
  192. rcu_read_lock();
  193. *pid = task_pid_nr(current);
  194. task = pid_task(find_vpid(*pid), PIDTYPE_PID);
  195. if (task) {
  196. memcpy(task_name, task->comm, TASK_COMM_LEN);
  197. task_name[TASK_COMM_LEN - 1] = '\0';
  198. }
  199. rcu_read_unlock();
  200. }
  201. static void p61_init_task_info(struct p61_dev *p61_dev)
  202. {
  203. p61_dev->open_pid = 0;
  204. p61_dev->release_pid = 0;
  205. memset(p61_dev->open_task_name, 0, TASK_COMM_LEN);
  206. memset(p61_dev->release_task_name, 0, TASK_COMM_LEN);
  207. }
  208. void p61_print_status(const char *func_name)
  209. {
  210. struct p61_dev *p61_dev = g_p61_dev;
  211. if (!p61_dev)
  212. return;
  213. NFC_LOG_INFO("%s: state=%d o_pid=%d rel_pid=%d o_task=%s rel_task=%s\n",
  214. func_name, p61_dev->ese_spi_transition_state,
  215. p61_dev->open_pid,
  216. p61_dev->release_pid,
  217. p61_dev->open_task_name,
  218. p61_dev->release_task_name);
  219. }
  220. static void p61_pinctrl_select(struct p61_dev *p61_dev, enum p61_pin_ctrl stat)
  221. {
  222. int ret;
  223. NFC_LOG_INFO("pinctrl[%s]\n", p61_pinctrl_name[stat]);
  224. if (!p61_dev->pinctrl || !p61_dev->pinctrl_state[stat])
  225. return;
  226. ret = pinctrl_select_state(p61_dev->pinctrl, p61_dev->pinctrl_state[stat]);
  227. if (ret < 0)
  228. NFC_LOG_INFO("pinctrl[%d] failed\n", stat);
  229. }
  230. #if IS_ENABLED(CONFIG_SPI_MSM_GENI)
  231. static void p61_spi_release_work(struct work_struct *work)
  232. {
  233. struct p61_dev *p61_dev = g_p61_dev;
  234. if (p61_dev == NULL) {
  235. NFC_LOG_ERR("%s: spi probe is not called\n", __func__);
  236. return;
  237. }
  238. NFC_LOG_INFO("release ese spi\n");
  239. p61_pinctrl_select(p61_dev, P61_PIN_CTRL_SUSPEND); /* for QC AP */
  240. }
  241. #endif
  242. #endif
  243. /**
  244. * \ingroup spi_driver
  245. * \brief Will be called on device close to release resources
  246. *
  247. * \param[in] struct inode *
  248. * \param[in] struct file *
  249. *
  250. * \retval 0 if ok.
  251. *
  252. */
  253. static int p61_dev_release(struct inode *inode, struct file *filp)
  254. {
  255. struct p61_dev *p61_dev = NULL;
  256. NFC_LOG_INFO("Enter %s: ESE driver release\n", __func__);
  257. mutex_lock(&open_close_mutex);
  258. p61_dev = filp->private_data;
  259. p61_dev->ese_spi_transition_state = ESE_SPI_IDLE;
  260. #if !IS_ENABLED(CONFIG_SAMSUNG_NFC)
  261. gpio_set_value(p61_dev->trusted_ese_gpio, 0);
  262. #endif
  263. nfc_ese_pwr(p61_dev->nfcc_data, ESE_RST_PROT_DIS);
  264. #if IS_ENABLED(CONFIG_SAMSUNG_NFC)
  265. p61_pinctrl_select(p61_dev, P61_PIN_CTRL_ESE_OFF); /* for LSI AP */
  266. #if IS_ENABLED(CONFIG_SPI_MSM_GENI)
  267. schedule_delayed_work(&p61_dev->spi_release_work,
  268. msecs_to_jiffies(2000));
  269. wake_lock_timeout(&p61_dev->spi_release_wakelock,
  270. msecs_to_jiffies(2100));
  271. #endif
  272. if (wake_lock_active(&p61_dev->ese_lock))
  273. wake_unlock(&p61_dev->ese_lock);
  274. #endif
  275. mutex_unlock(&open_close_mutex);
  276. #if IS_ENABLED(CONFIG_SAMSUNG_NFC)
  277. p61_get_task_info(p61_dev, p61_dev->release_task_name, &p61_dev->release_pid);
  278. p61_print_status(__func__);
  279. #endif
  280. NFC_LOG_INFO("Exit %s: ESE driver release\n", __func__);
  281. return 0;
  282. }
  283. static int p61_xfer(struct p61_dev *p61_dev,
  284. struct p61_ioctl_transfer *tr)
  285. {
  286. int status = 0;
  287. struct spi_message m;
  288. struct spi_transfer t;
  289. static u32 read_try_cnt;
  290. /*For SDM845 & linux4.9: need to change spi buffer
  291. * from stack to dynamic memory
  292. */
  293. if (p61_dev == NULL || tr == NULL)
  294. return -EFAULT;
  295. if (tr->len > MAX_BUFFER_SIZE || !tr->len)
  296. return -EMSGSIZE;
  297. spi_message_init(&m);
  298. memset(&t, 0, sizeof(t));
  299. memset(p61_dev->w_buf, 0, tr->len); /*memset 0 for write */
  300. memset(p61_dev->r_buf, 0, tr->len); /*memset 0 for read */
  301. if (tr->tx_buffer != NULL) { /*write */
  302. if (copy_from_user(p61_dev->w_buf, tr->tx_buffer, tr->len) != 0) {
  303. NFC_LOG_ERR("p61_wr: copy_from_user fail %d\n", tr->len);
  304. status = -EFAULT;
  305. goto xfer_exit;
  306. }
  307. }
  308. t.rx_buf = p61_dev->r_buf;
  309. t.tx_buf = p61_dev->w_buf;
  310. t.len = tr->len;
  311. spi_message_add_tail(&t, &m);
  312. status = spi_sync(p61_dev->spi, &m);
  313. if (status == 0) {
  314. if (tr->rx_buffer != NULL) { /*read */
  315. unsigned long missing = 0;
  316. missing = copy_to_user(tr->rx_buffer, p61_dev->r_buf, tr->len);
  317. if (missing != 0)
  318. tr->len = tr->len - (unsigned int)missing;
  319. }
  320. }
  321. if (tr->tx_buffer != NULL) {
  322. if (read_try_cnt)
  323. NFC_LOG_REC("p61w%d try%u\n", tr->len, read_try_cnt);
  324. else
  325. NFC_LOG_REC("p61w%d\n", tr->len);
  326. }
  327. if (tr->rx_buffer != NULL) {
  328. if (tr->len == 2 && ((p61_dev->r_buf[0] == 0x0 && p61_dev->r_buf[2] == 0x0) ||
  329. (p61_dev->r_buf[0] == 0xff && p61_dev->r_buf[2] == 0xff))) {
  330. read_try_cnt++;
  331. } else {
  332. if (read_try_cnt)
  333. NFC_LOG_REC("p61r%d try%u\n", tr->len, read_try_cnt);
  334. else
  335. NFC_LOG_REC("p61r%d\n", tr->len);
  336. read_try_cnt = 0;
  337. }
  338. }
  339. xfer_exit:
  340. return status;
  341. } /* vfsspi_xfer */
  342. static int p61_rw_spi_message(struct p61_dev *p61_dev,
  343. unsigned long arg)
  344. {
  345. struct p61_ioctl_transfer *dup = NULL;
  346. int err = 0;
  347. dup = kmalloc(sizeof(struct p61_ioctl_transfer), GFP_KERNEL);
  348. if (dup == NULL)
  349. return -ENOMEM;
  350. if (copy_from_user(dup, (void *)arg,
  351. sizeof(struct p61_ioctl_transfer)) != 0) {
  352. kfree(dup);
  353. return -EFAULT;
  354. }
  355. err = p61_xfer(p61_dev, dup);
  356. if (err != 0) {
  357. kfree(dup);
  358. NFC_LOG_ERR("%s: p61_xfer failed, %d\n", __func__, err);
  359. return err;
  360. }
  361. if (copy_to_user((void *)arg, dup,
  362. sizeof(struct p61_ioctl_transfer)) != 0) {
  363. kfree(dup);
  364. return -EFAULT;
  365. }
  366. kfree(dup);
  367. return 0;
  368. }
  369. #if IS_ENABLED(CONFIG_SAMSUNG_NFC)
  370. void store_nfc_i2c_device(struct device *nfc_i2c_dev)
  371. {
  372. g_nfc_device = nfc_i2c_dev;
  373. }
  374. #endif
  375. #ifdef CONFIG_COMPAT
  376. static int p61_rw_spi_message_compat(struct p61_dev *p61_dev,
  377. unsigned long arg)
  378. {
  379. struct p61_ioctl_transfer32 __user *argp = compat_ptr(arg);
  380. struct p61_ioctl_transfer32 it32;
  381. struct p61_ioctl_transfer *dup = NULL;
  382. int err = 0;
  383. dup = kmalloc(sizeof(struct p61_ioctl_transfer), GFP_KERNEL);
  384. if (dup == NULL)
  385. return -ENOMEM;
  386. if (copy_from_user(&it32, argp, sizeof(it32))) {
  387. kfree(dup);
  388. return -EFAULT;
  389. }
  390. dup->rx_buffer = (__u8 *)(uintptr_t)it32.rx_buffer;
  391. dup->tx_buffer = (__u8 *)(uintptr_t)it32.tx_buffer;
  392. dup->len = it32.len;
  393. err = p61_xfer(p61_dev, dup);
  394. if (err != 0) {
  395. kfree(dup);
  396. NFC_LOG_ERR("%s: p61_xfer failed, %d\n", __func__, err);
  397. return err;
  398. }
  399. if (it32.rx_buffer) {
  400. if (__put_user(dup->len, &argp->len)) {
  401. kfree(dup);
  402. return -EFAULT;
  403. }
  404. }
  405. kfree(dup);
  406. return 0;
  407. }
  408. #endif /*CONFIG_COMPAT */
  409. /**
  410. * \ingroup spi_driver
  411. * \brief Called from SPI LibEse to initilaize the P61 device
  412. *
  413. * \param[in] struct inode *
  414. * \param[in] struct file *
  415. *
  416. * \retval 0 if ok.
  417. *
  418. */
  419. static int p61_dev_open(struct inode *inode, struct file *filp)
  420. {
  421. #ifdef CONFIG_MAKE_NODE_USING_PLATFORM_DEVICE
  422. struct p61_dev *p61_dev = g_p61_dev;
  423. if (p61_dev == NULL) {
  424. NFC_LOG_ERR("%s: spi probe is not called\n", __func__);
  425. return -EAGAIN;
  426. }
  427. #else
  428. struct p61_dev
  429. *p61_dev = container_of(filp->private_data,
  430. struct p61_dev,
  431. p61_device);
  432. #endif
  433. /* Find the NFC parent device if it exists. */
  434. if (p61_dev != NULL && p61_dev->nfcc_data == NULL) {
  435. #if IS_ENABLED(CONFIG_SAMSUNG_NFC)
  436. struct device *nfc_dev = g_nfc_device;
  437. if (!nfc_dev) {
  438. NFC_LOG_ERR("%s: cannot find NFC controller\n", __func__);
  439. return -ENODEV;
  440. }
  441. #else
  442. struct device *nfc_dev = bus_find_device_by_name(&i2c_bus_type, NULL,
  443. p61_dev->nfcc_name);
  444. if (!nfc_dev) {
  445. NFC_LOG_ERR("%s: cannot find NFC controller '%s'\n", __func__,
  446. p61_dev->nfcc_name);
  447. return -ENODEV;
  448. }
  449. #endif
  450. p61_dev->nfcc_data = dev_get_drvdata(nfc_dev);
  451. if (!p61_dev->nfcc_data) {
  452. NFC_LOG_ERR("%s: cannot find NFC controller device data\n", __func__);
  453. put_device(nfc_dev);
  454. return -ENODEV;
  455. }
  456. P61_DBG_MSG("%s: NFC controller found\n", __func__);
  457. p61_dev->nfcc_device = nfc_dev;
  458. }
  459. filp->private_data = p61_dev;
  460. if(p61_dev->ese_spi_transition_state == ESE_SPI_BUSY) {
  461. NFC_LOG_ERR("%s : ESE is busy\n", __func__);
  462. return -EBUSY;
  463. }
  464. mutex_lock(&open_close_mutex);
  465. p61_dev->ese_spi_transition_state = ESE_SPI_BUSY;
  466. #if IS_ENABLED(CONFIG_SAMSUNG_NFC)
  467. /* for checking previous open/close tasks */
  468. p61_print_status("p61_dev_open pre");
  469. #if IS_ENABLED(CONFIG_SPI_MSM_GENI)
  470. cancel_delayed_work_sync(&p61_dev->spi_release_work);
  471. #endif
  472. if (!wake_lock_active(&p61_dev->ese_lock))
  473. wake_lock(&p61_dev->ese_lock);
  474. p61_pinctrl_select(p61_dev, P61_PIN_CTRL_ESE_ON);
  475. if (p61_dev->ap_vendor != AP_VENDOR_QCT)
  476. msleep(60);
  477. #endif
  478. mutex_unlock(&open_close_mutex);
  479. #if IS_ENABLED(CONFIG_SAMSUNG_NFC)
  480. p61_init_task_info(p61_dev);
  481. p61_get_task_info(p61_dev, p61_dev->open_task_name, &p61_dev->open_pid);
  482. p61_print_status(__func__);
  483. #endif
  484. return 0;
  485. }
  486. /**
  487. * \ingroup spi_driver
  488. * \brief To configure the P61_SET_PWR/P61_SET_DBG/P61_SET_POLL
  489. * \n P61_SET_PWR - hard reset (arg=2), soft reset (arg=1)
  490. * \n P61_SET_DBG - Enable/Disable (based on arg value) the driver logs
  491. * \n P61_SET_POLL - Configure the driver in poll (arg = 1), interrupt (arg = 0) based read operation
  492. * \param[in] struct file *
  493. * \param[in] unsigned int
  494. * \param[in] unsigned long
  495. *
  496. * \retval 0 if ok.
  497. *
  498. */
  499. static long p61_dev_ioctl(struct file *filp, unsigned int cmd,
  500. unsigned long arg)
  501. {
  502. int ret = 0;
  503. struct p61_dev *p61_dev = NULL;
  504. #if !IS_ENABLED(CONFIG_SAMSUNG_NFC)
  505. unsigned char buf[100];
  506. #endif
  507. P61_DBG_MSG(KERN_ALERT "p61_dev_ioctl-Enter %u arg = %ld\n", cmd, arg);
  508. p61_dev = filp->private_data;
  509. switch (cmd) {
  510. case P61_SET_PWR:
  511. #if IS_ENABLED(CONFIG_SAMSUNG_NFC)
  512. if (arg == 2)
  513. NFC_LOG_INFO("[NXP-P61] - P61_SET_PWR. No Action.\n");
  514. #else
  515. if (arg == 2) {
  516. #ifdef P61_HARD_RESET
  517. P61_DBG_MSG(KERN_ALERT " Disabling p61_regulator");
  518. if (p61_regulator != NULL) {
  519. regulator_disable(p61_regulator);
  520. msleep(50);
  521. regulator_enable(p61_regulator);
  522. P61_DBG_MSG(KERN_ALERT " Enabling p61_regulator");
  523. } else {
  524. NFC_LOG_ERR(KERN_ALERT " ERROR : p61_regulator is not enabled\n");
  525. }
  526. #endif
  527. } else if (arg == 1) {
  528. P61_DBG_MSG(KERN_ALERT " Soft Reset");
  529. //gpio_set_value(p61_dev->rst_gpio, 1);
  530. //msleep(20);
  531. gpio_set_value(p61_dev->rst_gpio, 0);
  532. msleep(50);
  533. ret = spi_read(p61_dev->spi, (void *)buf, sizeof(buf));
  534. msleep(50);
  535. gpio_set_value(p61_dev->rst_gpio, 1);
  536. msleep(20);
  537. }
  538. #endif
  539. break;
  540. case P61_SET_DBG:
  541. debug_level = (unsigned char)arg;
  542. NFC_LOG_INFO("[NXP-P61] - Debug level %d\n",
  543. debug_level);
  544. break;
  545. case P61_SET_POLL:
  546. p61_dev->enable_poll_mode = (unsigned char)arg;
  547. if (p61_dev->enable_poll_mode == 0) {
  548. NFC_LOG_INFO("[NXP-P61] - IRQ Mode is set\n");
  549. } else {
  550. NFC_LOG_INFO("[NXP-P61] - Poll Mode is set\n");
  551. p61_dev->enable_poll_mode = 1;
  552. }
  553. break;
  554. case P61_RW_SPI_DATA:
  555. ret = p61_rw_spi_message(p61_dev, arg);
  556. break;
  557. case P61_SET_SPM_PWR:
  558. NFC_LOG_INFO("P61_SET_SPM_PWR: enter\n");
  559. ret = nfc_ese_pwr(p61_dev->nfcc_data, arg);
  560. NFC_LOG_INFO("P61_SET_SPM_PWR: exit\n");
  561. break;
  562. case P61_GET_SPM_STATUS:
  563. NFC_LOG_INFO("P61_GET_SPM_STATUS: enter\n");
  564. ret = nfc_ese_pwr(p61_dev->nfcc_data, ESE_POWER_STATE);
  565. NFC_LOG_INFO("P61_GET_SPM_STATUS: exiti\n");
  566. break;
  567. case P61_SET_DWNLD_STATUS:
  568. NFC_LOG_INFO("P61_SET_DWNLD_STATUS: enter\n");
  569. //ret = nfc_dev_ioctl(filp, PN544_SET_DWNLD_STATUS, arg);
  570. NFC_LOG_INFO("P61_SET_DWNLD_STATUS: =%lu exit\n", arg);
  571. break;
  572. case P61_GET_ESE_ACCESS:
  573. NFC_LOG_INFO("P61_GET_ESE_ACCESS: enter\n");
  574. //ret = nfc_dev_ioctl(filp, P544_GET_ESE_ACCESS, arg);
  575. NFC_LOG_INFO("P61_GET_ESE_ACCESS ret: %d exit\n", ret);
  576. break;
  577. case P61_SET_POWER_SCHEME:
  578. NFC_LOG_INFO("P61_SET_POWER_SCHEME: enter\n");
  579. //ret = nfc_dev_ioctl(filp, P544_SET_POWER_SCHEME, arg);
  580. NFC_LOG_INFO("P61_SET_POWER_SCHEME ret: %d exit\n",
  581. ret);
  582. break;
  583. case P61_INHIBIT_PWR_CNTRL:
  584. NFC_LOG_INFO("P61_INHIBIT_PWR_CNTRL: enter\n");
  585. //ret = nfc_dev_ioctl(filp, P544_SECURE_TIMER_SESSION, arg);
  586. NFC_LOG_INFO("P61_INHIBIT_PWR_CNTRL ret: %d exit\n",
  587. ret);
  588. break;
  589. case ESE_PERFORM_COLD_RESET:
  590. NFC_LOG_INFO("ESE_PERFORM_COLD_RESET: enter\n");
  591. if (p61_dev->gpio_coldreset)
  592. ret = perform_ese_gpio_reset(p61_dev->rst_gpio);
  593. else
  594. ret = nfc_ese_pwr(p61_dev->nfcc_data, ESE_CLD_RST);
  595. NFC_LOG_INFO("ESE_PERFORM_COLD_RESET ret: %d exit\n", ret);
  596. break;
  597. case PERFORM_RESET_PROTECTION:
  598. if (p61_dev->gpio_coldreset) {
  599. NFC_LOG_INFO("PERFORM_RESET_PROTECTION is not required and not supported\n");
  600. } else {
  601. NFC_LOG_INFO("PERFORM_RESET_PROTECTION: enter\n");
  602. ret = nfc_ese_pwr(p61_dev->nfcc_data,
  603. (arg == 1 ? ESE_RST_PROT_EN : ESE_RST_PROT_DIS));
  604. NFC_LOG_INFO("PERFORM_RESET_PROTECTION ret: %d exit\n", ret);
  605. }
  606. break;
  607. case ESE_SET_TRUSTED_ACCESS:
  608. NFC_LOG_INFO("Enter %s: TRUSTED access enabled=%lu\n", __func__, arg);
  609. #if !IS_ENABLED(CONFIG_SAMSUNG_NFC)
  610. if(arg == 1) {
  611. NFC_LOG_INFO("ESE_SET_TRUSTED_ACCESS: enter Enabling\n");
  612. gpio_set_value(p61_dev->trusted_ese_gpio, 1);
  613. NFC_LOG_INFO("ESE_SET_TRUSTED_ACCESS ret: exit\n");
  614. } else if (arg == 0) {
  615. NFC_LOG_INFO("ESE_SET_TRUSTED_ACCESS: enter Disabling\n");
  616. gpio_set_value(p61_dev->trusted_ese_gpio, 0);
  617. NFC_LOG_INFO("ESE_SET_TRUSTED_ACCESS ret: exit\n");
  618. }
  619. #endif
  620. break;
  621. default:
  622. NFC_LOG_ERR("Error case\n");
  623. ret = -EINVAL;
  624. }
  625. P61_DBG_MSG(KERN_ALERT "p61_dev_ioctl-exit %u arg = %lu\n", cmd, arg);
  626. return ret;
  627. }
  628. #ifdef CONFIG_COMPAT
  629. /**
  630. * \ingroup spi_driver
  631. * \brief To configure the P61_SET_PWR/P61_SET_DBG/P61_SET_POLL
  632. * \n P61_SET_PWR - hard reset (arg=2), soft reset (arg=1)
  633. * \n P61_SET_DBG - Enable/Disable (based on arg value) the driver logs
  634. * \n P61_SET_POLL - Configure the driver in poll (arg = 1), interrupt (arg = 0) based read operation
  635. * \param[in] struct file *
  636. * \param[in] unsigned int
  637. * \param[in] unsigned long
  638. *
  639. * \retval 0 if ok.
  640. *
  641. */
  642. #if IS_ENABLED(CONFIG_SAMSUNG_NFC)
  643. static long p61_dev_compat_ioctl(struct file *filp, unsigned int cmd,
  644. unsigned long arg)
  645. {
  646. int ret = 0;
  647. struct p61_dev *p61_dev = NULL;
  648. if (_IOC_TYPE(cmd) != P61_MAGIC)
  649. return -ENOTTY;
  650. p61_dev = filp->private_data;
  651. switch (cmd) {
  652. case P61_SET_PWR_COMPAT:
  653. if (arg == 2)
  654. NFC_LOG_INFO("%s: P61_SET_PWR. No Action.\n", __func__);
  655. break;
  656. case P61_SET_DBG_COMPAT:
  657. debug_level = (unsigned char)arg;
  658. P61_DBG_MSG(KERN_INFO"[NXP-P61] - Debug level %d",
  659. debug_level);
  660. break;
  661. case P61_SET_POLL_COMPAT:
  662. p61_dev->enable_poll_mode = (unsigned char)arg;
  663. if (p61_dev->enable_poll_mode == 0) {
  664. P61_DBG_MSG(KERN_INFO"[NXP-P61] - IRQ Mode is set\n");
  665. } else {
  666. P61_DBG_MSG(KERN_INFO"[NXP-P61] - Poll Mode is set\n");
  667. p61_dev->enable_poll_mode = 1;
  668. }
  669. break;
  670. case P61_RW_SPI_DATA_COMPAT:
  671. ret = p61_rw_spi_message_compat(p61_dev, arg);
  672. break;
  673. case P61_SET_SPM_PWR_COMPAT:
  674. NFC_LOG_INFO("%s: P61_SET_SPM_PWR: enter\n", __func__);
  675. ret = nfc_ese_pwr(p61_dev->nfcc_data, arg);
  676. NFC_LOG_INFO("%s: P61_SET_SPM_PWR: exit\n", __func__);
  677. break;
  678. case P61_GET_SPM_STATUS_COMPAT:
  679. NFC_LOG_INFO("%s: P61_GET_SPM_STATUS: enter\n", __func__);
  680. ret = nfc_ese_pwr(p61_dev->nfcc_data, ESE_POWER_STATE);
  681. NFC_LOG_INFO("%s: P61_GET_SPM_STATUS: exit\n", __func__);
  682. break;
  683. case P61_SET_DWNLD_STATUS_COMPAT:
  684. NFC_LOG_INFO("P61_SET_DWNLD_STATUS: enter\n");
  685. //ret = pn547_dev_ioctl(filp, PN547_SET_DWNLD_STATUS, arg);
  686. NFC_LOG_INFO("%s: P61_SET_DWNLD_STATUS: =%lu exit\n", __func__, arg);
  687. break;
  688. case P61_GET_ESE_ACCESS_COMPAT:
  689. NFC_LOG_INFO("P61_GET_ESE_ACCESS: enter\n");
  690. //ret = pn547_dev_ioctl(filp, P547_GET_ESE_ACCESS, arg);
  691. NFC_LOG_INFO("P61_GET_ESE_ACCESS ret: %d exit\n", ret);
  692. break;
  693. case P61_SET_POWER_SCHEME_COMPAT:
  694. NFC_LOG_INFO("P61_SET_POWER_SCHEME: enter\n");
  695. //ret = pn547_dev_ioctl(filp, P544_SET_POWER_SCHEME, arg);
  696. NFC_LOG_INFO("P61_SET_POWER_SCHEME ret: %d exit\n",
  697. ret);
  698. break;
  699. case P61_INHIBIT_PWR_CNTRL_COMPAT:
  700. NFC_LOG_INFO("P61_INHIBIT_PWR_CNTRL: enter\n");
  701. //ret = pn547_dev_ioctl(filp, P544_SECURE_TIMER_SESSION, arg);
  702. NFC_LOG_INFO("P61_INHIBIT_PWR_CNTRL ret: %d exit\n",
  703. ret);
  704. break;
  705. case ESE_PERFORM_COLD_RESET_COMPAT:
  706. NFC_LOG_INFO("ESE_PERFORM_COLD_RESET: enter\n");
  707. if (p61_dev->gpio_coldreset)
  708. ret = perform_ese_gpio_reset(p61_dev->rst_gpio);
  709. else
  710. ret = nfc_ese_pwr(p61_dev->nfcc_data, ESE_CLD_RST);
  711. NFC_LOG_INFO("ESE_PERFORM_COLD_RESET ret: %d exit\n", ret);
  712. break;
  713. case PERFORM_RESET_PROTECTION_COMPAT:
  714. if (p61_dev->gpio_coldreset) {
  715. NFC_LOG_INFO("PERFORM_RESET_PROTECTION is not required and not supported\n");
  716. } else {
  717. NFC_LOG_INFO("PERFORM_RESET_PROTECTION: enter\n");
  718. ret = nfc_ese_pwr(p61_dev->nfcc_data,
  719. (arg == 1 ? ESE_RST_PROT_EN : ESE_RST_PROT_DIS));
  720. NFC_LOG_INFO("PERFORM_RESET_PROTECTION ret: %d exit\n", ret);
  721. }
  722. break;
  723. case ESE_SET_TRUSTED_ACCESS:
  724. NFC_LOG_INFO("Enter %s: TRUSTED access enabled=%lu\n", __func__, arg);
  725. #if !IS_ENABLED(CONFIG_SAMSUNG_NFC)
  726. if (arg == 1) {
  727. NFC_LOG_INFO("ESE_SET_TRUSTED_ACCESS: enter Enabling\n");
  728. gpio_set_value(p61_dev->trusted_ese_gpio, 1);
  729. NFC_LOG_INFO("ESE_SET_TRUSTED_ACCESS ret: exit\n");
  730. } else if (arg == 0) {
  731. NFC_LOG_INFO("ESE_SET_TRUSTED_ACCESS: enter Disabling\n");
  732. gpio_set_value(p61_dev->trusted_ese_gpio, 0);
  733. NFC_LOG_INFO("ESE_SET_TRUSTED_ACCESS ret: exit\n");
  734. }
  735. #endif
  736. break;
  737. default:
  738. NFC_LOG_INFO("%s: no matching ioctl!\n", __func__);
  739. ret = -EINVAL;
  740. }
  741. P61_DBG_MSG(KERN_ALERT "%s %u arg = %lu\n", __func__, cmd, arg);
  742. return ret;
  743. }
  744. #else
  745. static long p61_dev_compat_ioctl(struct file *filp, unsigned int cmd,
  746. unsigned long arg)
  747. {
  748. int ret = 0;
  749. arg = (compat_u64)arg;
  750. NFC_LOG_INFO(KERN_ALERT "%s-Enter %u arg = %ld\n", __func__, cmd, arg);
  751. NFC_LOG_DBG("%s: cmd = %x arg = %zx\n", __func__, cmd, arg);
  752. ret = p61_dev_ioctl(filp, cmd, arg);
  753. return ret;
  754. }
  755. #endif
  756. #endif
  757. /**
  758. * \ingroup spi_driver
  759. * \brief Write data to P61 on SPI
  760. *
  761. * \param[in] struct file *
  762. * \param[in] const char *
  763. * \param[in] size_t
  764. * \param[in] loff_t *
  765. *
  766. * \retval data size
  767. *
  768. */
  769. static ssize_t p61_dev_write(struct file *filp, const char *buf, size_t count,
  770. loff_t * offset)
  771. {
  772. int ret = -1;
  773. struct p61_dev *p61_dev;
  774. #if IS_ENABLED(CONFIG_SAMSUNG_NFC)
  775. unsigned char *tx_buffer;
  776. #else
  777. unsigned char tx_buffer[MAX_BUFFER_SIZE];
  778. #endif
  779. P61_DBG_MSG(KERN_ALERT "p61_dev_write -Enter count %zu\n", count);
  780. p61_dev = filp->private_data;
  781. mutex_lock(&p61_dev->write_mutex);
  782. if (count > MAX_BUFFER_SIZE)
  783. count = MAX_BUFFER_SIZE;
  784. #if IS_ENABLED(CONFIG_SAMSUNG_NFC)
  785. tx_buffer = p61_dev->w_buf;
  786. memset(&tx_buffer[0], 0, MAX_BUFFER_SIZE);
  787. #else
  788. memset(&tx_buffer[0], 0, sizeof(tx_buffer));
  789. #endif
  790. if (copy_from_user(&tx_buffer[0], &buf[0], count)) {
  791. NFC_LOG_ERR("%s: failed to copy from user space\n", __func__);
  792. mutex_unlock(&p61_dev->write_mutex);
  793. return -EFAULT;
  794. }
  795. /* Write data */
  796. ret = spi_write(p61_dev->spi, &tx_buffer[0], count);
  797. if (ret < 0) {
  798. NFC_LOG_ERR("%s: spi_write fail %d\n", __func__, ret);
  799. ret = -EIO;
  800. } else {
  801. ret = count;
  802. }
  803. mutex_unlock(&p61_dev->write_mutex);
  804. NFC_LOG_REC("%s ret %d- Exit\n", __func__, ret);
  805. return ret;
  806. }
  807. #ifdef P61_IRQ_ENABLE
  808. /**
  809. * \ingroup spi_driver
  810. * \brief To disable IRQ
  811. *
  812. * \param[in] struct p61_dev *
  813. *
  814. * \retval void
  815. *
  816. */
  817. static void p61_disable_irq(struct p61_dev *p61_dev)
  818. {
  819. unsigned long flags;
  820. P61_DBG_MSG("Entry : %s\n", __func__);
  821. spin_lock_irqsave(&p61_dev->irq_enabled_lock, flags);
  822. if (p61_dev->irq_enabled) {
  823. disable_irq_nosync(p61_dev->spi->irq);
  824. p61_dev->irq_enabled = false;
  825. }
  826. spin_unlock_irqrestore(&p61_dev->irq_enabled_lock, flags);
  827. P61_DBG_MSG("Exit : %s\n", __func__);
  828. }
  829. /**
  830. * \ingroup spi_driver
  831. * \brief Will get called when interrupt line asserted from P61
  832. *
  833. * \param[in] int
  834. * \param[in] void *
  835. *
  836. * \retval IRQ handle
  837. *
  838. */
  839. static irqreturn_t p61_dev_irq_handler(int irq, void *dev_id)
  840. {
  841. struct p61_dev *p61_dev = dev_id;
  842. P61_DBG_MSG("Entry : %s\n", __func__);
  843. p61_disable_irq(p61_dev);
  844. /* Wake up waiting readers */
  845. wake_up(&p61_dev->read_wq);
  846. P61_DBG_MSG("Exit : %s\n", __func__);
  847. return IRQ_HANDLED;
  848. }
  849. #endif
  850. /**
  851. * \ingroup spi_driver
  852. * \brief Used to read data from P61 in Poll/interrupt mode configured using ioctl call
  853. *
  854. * \param[in] struct file *
  855. * \param[in] char *
  856. * \param[in] size_t
  857. * \param[in] loff_t *
  858. *
  859. * \retval read size
  860. *
  861. */
  862. static ssize_t p61_dev_read(struct file *filp, char *buf, size_t count,
  863. loff_t * offset)
  864. {
  865. int ret = -EIO;
  866. struct p61_dev *p61_dev = filp->private_data;
  867. #if IS_ENABLED(CONFIG_SAMSUNG_NFC)
  868. unsigned char *rx_buffer = p61_dev->r_buf;
  869. #else
  870. unsigned char rx_buffer[MAX_BUFFER_SIZE];
  871. #endif
  872. NFC_LOG_REC("%s count %zu - Enter\n", __func__, count);
  873. mutex_lock(&p61_dev->read_mutex);
  874. if (count > MAX_BUFFER_SIZE) {
  875. count = MAX_BUFFER_SIZE;
  876. }
  877. #if IS_ENABLED(CONFIG_SAMSUNG_NFC)
  878. memset(&rx_buffer[0], 0x00, MAX_BUFFER_SIZE);
  879. #else
  880. memset(&rx_buffer[0], 0x00, sizeof(rx_buffer));
  881. #endif
  882. if (p61_dev->enable_poll_mode) {
  883. NFC_LOG_REC("%s Poll Mode Enabled\n", __func__);
  884. NFC_LOG_REC("SPI_READ returned %zu\n", count);
  885. ret = spi_read(p61_dev->spi, (void *)&rx_buffer[0], count);
  886. if (0 > ret) {
  887. NFC_LOG_ERR("spi_read failed [SOF]\n");
  888. goto fail;
  889. }
  890. } else {
  891. #ifdef P61_IRQ_ENABLE
  892. NFC_LOG_REC("%s Interrupt Mode Enabled\n", __func__);
  893. if (!gpio_get_value(p61_dev->irq_gpio)) {
  894. if (filp->f_flags & O_NONBLOCK) {
  895. ret = -EAGAIN;
  896. goto fail;
  897. }
  898. while (1) {
  899. NFC_LOG_REC("%s waiting for interrupt\n", __func__);
  900. p61_dev->irq_enabled = true;
  901. enable_irq(p61_dev->spi->irq);
  902. ret = wait_event_interruptible(p61_dev->read_wq, !p61_dev->irq_enabled);
  903. p61_disable_irq(p61_dev);
  904. if (ret) {
  905. NFC_LOG_ERR("wait_event_interruptible() : Failed\n");
  906. goto fail;
  907. }
  908. if (gpio_get_value(p61_dev->irq_gpio))
  909. break;
  910. NFC_LOG_ERR("%s: spurious interrupt detected\n", __func__);
  911. }
  912. }
  913. #else
  914. NFC_LOG_REC(" %s P61_IRQ_ENABLE not Enabled\n", __func__);
  915. #endif
  916. ret = spi_read(p61_dev->spi, (void *)&rx_buffer[0], count);
  917. if (0 > ret) {
  918. NFC_LOG_ERR("SPI_READ returned 0x%x\n", ret);
  919. ret = -EIO;
  920. goto fail;
  921. }
  922. }
  923. NFC_LOG_REC("total_count = %zu\n", count);
  924. if (copy_to_user(buf, &rx_buffer[0], count)) {
  925. NFC_LOG_ERR("%s : failed to copy to user space\n", __func__);
  926. ret = -EFAULT;
  927. goto fail;
  928. }
  929. NFC_LOG_REC("%s ret %d Exit\n", __func__, ret);
  930. NFC_LOG_REC("%s ret %d Exit\n", __func__, rx_buffer[0]);
  931. mutex_unlock(&p61_dev->read_mutex);
  932. return ret;
  933. fail:
  934. NFC_LOG_ERR("Error %s ret %d Exit\n", __func__, ret);
  935. mutex_unlock(&p61_dev->read_mutex);
  936. return ret;
  937. }
  938. /**
  939. * \ingroup spi_driver
  940. * \brief It will configure the GPIOs required for soft reset, read interrupt & regulated power supply to P61.
  941. *
  942. * \param[in] struct p61_spi_platform_data *
  943. * \param[in] struct p61_dev *
  944. * \param[in] struct spi_device *
  945. *
  946. * \retval 0 if ok.
  947. *
  948. */
  949. #if !IS_ENABLED(CONFIG_SAMSUNG_NFC)
  950. static int p61_hw_setup(struct p61_spi_platform_data *platform_data,
  951. struct p61_dev *p61_dev, struct spi_device *spi)
  952. {
  953. int ret = -1;
  954. NFC_LOG_INFO("Entry : %s\n", __func__);
  955. #ifdef P61_IRQ_ENABLE
  956. ret = gpio_request(platform_data->irq_gpio, "p61 irq");
  957. if (ret < 0) {
  958. NFC_LOG_ERR("gpio request failed gpio = 0x%x\n", platform_data->irq_gpio);
  959. goto fail;
  960. }
  961. ret = gpio_direction_input(platform_data->irq_gpio);
  962. if (ret < 0) {
  963. NFC_LOG_ERR("gpio request failed gpio = 0x%x\n", platform_data->irq_gpio);
  964. goto fail_irq;
  965. }
  966. #endif
  967. #ifdef P61_HARD_RESET
  968. /* RC : platform specific settings need to be declare */
  969. #if !DRAGON_P61
  970. p61_regulator = regulator_get(&spi->dev, "vaux3");
  971. #else
  972. p61_regulator = regulator_get(&spi->dev, "8941_l18");
  973. #endif
  974. if (IS_ERR(p61_regulator)) {
  975. ret = PTR_ERR(p61_regulator);
  976. #if !DRAGON_P61
  977. NFC_LOG_ERR(" Error to get vaux3 (error code) = %d\n", ret);
  978. #else
  979. NFC_LOG_ERR(" Error to get 8941_l18 (error code) = %d\n", ret);
  980. #endif
  981. return -ENODEV;
  982. } else {
  983. NFC_LOG_INFO("successfully got regulator\n");
  984. }
  985. ret = regulator_set_voltage(p61_regulator, 1800000, 1800000);
  986. if (ret != 0) {
  987. NFC_LOG_ERR("Error setting the regulator voltage %d\n", ret);
  988. regulator_put(p61_regulator);
  989. return ret;
  990. } else {
  991. regulator_enable(p61_regulator);
  992. NFC_LOG_INFO("successfully set regulator voltage\n");
  993. }
  994. ret = gpio_request(platform_data->rst_gpio, "p61 reset");
  995. if (ret < 0) {
  996. NFC_LOG_ERR("gpio reset request failed = 0x%x\n", platform_data->rst_gpio);
  997. goto fail_gpio;
  998. }
  999. /*soft reset gpio is set to default high */
  1000. ret = gpio_direction_output(platform_data->rst_gpio, 1);
  1001. if (ret < 0) {
  1002. NFC_LOG_ERR("gpio rst request failed gpio = 0x%x\n", platform_data->rst_gpio);
  1003. goto fail_gpio;
  1004. }
  1005. #endif
  1006. ret = gpio_request( platform_data->trusted_ese_gpio, "Trusted SE switch");
  1007. if (ret < 0) {
  1008. NFC_LOG_ERR("gpio reset request failed = 0x%x\n",
  1009. platform_data->trusted_ese_gpio);
  1010. gpio_free(platform_data->trusted_ese_gpio);
  1011. NFC_LOG_ERR("%s failed\n", __func__);
  1012. return ret;
  1013. }
  1014. ret = gpio_direction_output(platform_data->trusted_ese_gpio,0);
  1015. if (ret < 0) {
  1016. NFC_LOG_ERR("gpio rst request failed gpio = 0x%x\n",
  1017. platform_data->trusted_ese_gpio);
  1018. gpio_free(platform_data->trusted_ese_gpio);
  1019. NFC_LOG_ERR("%s failed\n, __func__");
  1020. return ret;
  1021. }
  1022. ret = ese_reset_gpio_setup(platform_data);
  1023. if (ret < 0) {
  1024. P61_ERR_MSG("Failed to setup ese reset gpio");
  1025. goto fail;
  1026. }
  1027. ret = 0;
  1028. NFC_LOG_INFO("Exit : %s\n", __func__);
  1029. return ret;
  1030. #ifdef P61_IRQ_ENABLE
  1031. fail_irq:
  1032. gpio_free(platform_data->irq_gpio);
  1033. #endif
  1034. fail:
  1035. NFC_LOG_ERR("p61_hw_setup failed\n");
  1036. return ret;
  1037. }
  1038. #endif
  1039. /**
  1040. * \ingroup spi_driver
  1041. * \brief Set the P61 device specific context for future use.
  1042. *
  1043. * \param[in] struct spi_device *
  1044. * \param[in] void *
  1045. *
  1046. * \retval void
  1047. *
  1048. */
  1049. static inline void p61_set_data(struct spi_device *spi, void *data)
  1050. {
  1051. dev_set_drvdata(&spi->dev, data);
  1052. }
  1053. /**
  1054. * \ingroup spi_driver
  1055. * \brief Get the P61 device specific context.
  1056. *
  1057. * \param[in] const struct spi_device *
  1058. *
  1059. * \retval Device Parameters
  1060. *
  1061. */
  1062. static inline void *p61_get_data(const struct spi_device *spi)
  1063. {
  1064. return dev_get_drvdata(&spi->dev);
  1065. }
  1066. /* possible fops on the p61 device */
  1067. static const struct file_operations p61_dev_fops = {
  1068. .owner = THIS_MODULE,
  1069. .read = p61_dev_read,
  1070. .write = p61_dev_write,
  1071. .open = p61_dev_open,
  1072. .release = p61_dev_release,
  1073. .unlocked_ioctl = p61_dev_ioctl,
  1074. #ifdef CONFIG_COMPAT
  1075. .compat_ioctl = p61_dev_compat_ioctl,
  1076. #endif
  1077. };
  1078. #if DRAGON_P61 || IS_ENABLED(CONFIG_SAMSUNG_NFC)
  1079. static int p61_parse_dt(struct device *dev, struct p61_spi_platform_data *data)
  1080. {
  1081. struct device_node *np = dev->of_node;
  1082. int errorno = 0;
  1083. #if IS_ENABLED(CONFIG_SAMSUNG_NFC)
  1084. int ese_det_gpio;
  1085. const char *ap_str;
  1086. int ret;
  1087. ese_det_gpio = of_get_named_gpio(np, "ese-det-gpio", 0);
  1088. if (!gpio_is_valid(ese_det_gpio)) {
  1089. NFC_LOG_INFO("%s: ese-det-gpio is not set\n", __func__);
  1090. } else {
  1091. ret = gpio_request(ese_det_gpio, "ese_det_gpio");
  1092. if (ret < 0)
  1093. NFC_LOG_ERR("%s failed to get gpio ese_det_gpio\n", __func__);
  1094. gpio_direction_input(ese_det_gpio);
  1095. if (!gpio_get_value(ese_det_gpio)) {
  1096. NFC_LOG_INFO("%s: ese is not supported\n", __func__);
  1097. return -ENODEV;
  1098. }
  1099. NFC_LOG_INFO("%s: ese is supported\n", __func__);
  1100. }
  1101. if (!of_property_read_string(np, "p61,ap_vendor", &ap_str)) {
  1102. if (!strcmp(ap_str, "slsi"))
  1103. data->ap_vendor = AP_VENDOR_SLSI;
  1104. else if (!strcmp(ap_str, "qct") || !strcmp(ap_str, "qualcomm"))
  1105. data->ap_vendor = AP_VENDOR_QCT;
  1106. else if (!strcmp(ap_str, "mtk"))
  1107. data->ap_vendor = AP_VENDOR_MTK;
  1108. NFC_LOG_INFO("AP vendor is %d\n", data->ap_vendor);
  1109. } else {
  1110. NFC_LOG_INFO("AP vendor is not set\n");
  1111. }
  1112. data->gpio_coldreset = of_property_read_bool(np, "p61,gpio_coldreset_support");
  1113. if (data->gpio_coldreset) {
  1114. NFC_LOG_INFO("gpio coldreset supports\n");
  1115. data->rst_gpio = of_get_named_gpio(np, "p61,gpio-rst", 0);
  1116. if ((!gpio_is_valid(data->rst_gpio)))
  1117. return -EINVAL;
  1118. }
  1119. #else
  1120. data->irq_gpio = of_get_named_gpio(np, "nxp,p61-irq", 0);
  1121. if ((!gpio_is_valid(data->irq_gpio)))
  1122. return -EINVAL;
  1123. data->rst_gpio = of_get_named_gpio(np, "nxp,p61-rst", 0);
  1124. if ((!gpio_is_valid(data->rst_gpio)))
  1125. return -EINVAL;
  1126. data->trusted_ese_gpio = of_get_named_gpio(np, "nxp,trusted-se", 0);
  1127. if ((!gpio_is_valid(data->trusted_ese_gpio)))
  1128. return -EINVAL;
  1129. NFC_LOG_INFO("%s: %d, %d, %d %d\n", __func__, data->irq_gpio, data->rst_gpio,
  1130. data->trusted_ese_gpio, errorno);
  1131. #endif
  1132. return errorno;
  1133. }
  1134. #endif
  1135. #if IS_ENABLED(CONFIG_SPI_MSM_GENI)
  1136. #if IS_ENABLED(CONFIG_MSM_GENI_SE)
  1137. /*
  1138. * eSE driver can't access spi_geni_master structure because it's defined in drivers/spi/spi-msm-geni.c file.
  1139. * so, we need a logic to search se_geni_rsc in "void *spi_geni_master".
  1140. */
  1141. struct se_geni_rsc *p61_find_spi_src(struct p61_dev *p61_dev, void *spi_geni_master)
  1142. {
  1143. char *offset = spi_geni_master;
  1144. struct se_geni_rsc *rsc;
  1145. int i;
  1146. int max_addr_cnt = 250;
  1147. for (i = 0; i < max_addr_cnt; i++) {
  1148. rsc = (struct se_geni_rsc *)offset;
  1149. if (rsc->geni_gpio_active == p61_dev->pinctrl_state[P61_PIN_CTRL_DEFAULT]) {
  1150. NFC_LOG_INFO("%s, found se_geni_rsc!\n", __func__);
  1151. return rsc;
  1152. }
  1153. offset++;
  1154. }
  1155. /* Check if spi_geni_master structure member which defined in spi-msm-geni.c file is changed or not when failed to find se_geni_src. */
  1156. NFC_LOG_ERR("%s, failed to find se_geni_rsc!\n", __func__);
  1157. return 0;
  1158. }
  1159. #else
  1160. /* CONFIG_QCOM_GENI_SE */
  1161. struct qc_spi_pinctrl {
  1162. struct pinctrl *geni_pinctrl;
  1163. struct pinctrl_state *geni_gpio_active;
  1164. struct pinctrl_state *geni_gpio_sleep;
  1165. };
  1166. struct qc_spi_pinctrl *p61_find_spi_src(struct p61_dev *p61_dev, void *spi_geni_master)
  1167. {
  1168. char *offset = spi_geni_master;
  1169. struct qc_spi_pinctrl *spi_pinctrl;
  1170. int i;
  1171. int max_addr_cnt = 250;
  1172. for (i = 0; i < max_addr_cnt; i++) {
  1173. spi_pinctrl = (struct qc_spi_pinctrl *)offset;
  1174. if (spi_pinctrl->geni_pinctrl == p61_dev->pinctrl &&
  1175. spi_pinctrl->geni_gpio_active == p61_dev->pinctrl_state[P61_PIN_CTRL_DEFAULT]) {
  1176. NFC_LOG_INFO("%s, found pinctrl in spi master!\n", __func__);
  1177. return spi_pinctrl;
  1178. }
  1179. offset++;
  1180. }
  1181. NFC_LOG_ERR("%s, failed to find spi pinctrl!\n", __func__);
  1182. return 0;
  1183. }
  1184. #endif
  1185. static void p61_set_spi_bus_pincontrol(struct p61_dev *p61_dev)
  1186. {
  1187. struct spi_master *master;
  1188. void *geni_mas;
  1189. #if IS_ENABLED(CONFIG_MSM_GENI_SE)
  1190. struct se_geni_rsc *spi_pinctrl;
  1191. #else
  1192. struct qc_spi_pinctrl *spi_pinctrl;
  1193. #endif
  1194. static bool called;
  1195. if (!p61_dev || called)
  1196. return;
  1197. if (!p61_dev->pinctrl_state[P61_PIN_CTRL_ACTIVE] || !p61_dev->pinctrl_state[P61_PIN_CTRL_SUSPEND])
  1198. return;
  1199. NFC_LOG_INFO("%s\n", __func__);
  1200. called = true;
  1201. master = platform_get_drvdata(p61_dev->spi_pdev);
  1202. geni_mas = spi_master_get_devdata(master);
  1203. spi_pinctrl = p61_find_spi_src(p61_dev, geni_mas);
  1204. if (spi_pinctrl) {
  1205. spi_pinctrl->geni_gpio_sleep =
  1206. pinctrl_lookup_state(spi_pinctrl->geni_pinctrl,
  1207. p61_pinctrl_name[P61_PIN_CTRL_SUSPEND]);
  1208. spi_pinctrl->geni_gpio_active =
  1209. pinctrl_lookup_state(spi_pinctrl->geni_pinctrl,
  1210. p61_pinctrl_name[P61_PIN_CTRL_ACTIVE]);
  1211. }
  1212. }
  1213. #endif
  1214. #if IS_ENABLED(CONFIG_SAMSUNG_NFC)
  1215. static void p61_parse_pinctrl_dt(struct device *dev, struct p61_dev *p61_dev)
  1216. {
  1217. struct device_node *spi_dev_node;
  1218. struct device_node *np = dev->of_node;
  1219. struct platform_device *spi_pdev;
  1220. struct pinctrl_state *tmp_pinctrl;
  1221. int i;
  1222. spi_dev_node = of_get_parent(np);
  1223. if (IS_ERR_OR_NULL(spi_dev_node)) {
  1224. NFC_LOG_INFO("no spi pinctrl\n");
  1225. return;
  1226. }
  1227. spi_pdev = of_find_device_by_node(spi_dev_node);
  1228. if (!spi_pdev) {
  1229. NFC_LOG_ERR("finding spi_dev failed\n");
  1230. return;
  1231. }
  1232. p61_dev->pinctrl = devm_pinctrl_get(&spi_pdev->dev);
  1233. if (IS_ERR(p61_dev->pinctrl)) {
  1234. NFC_LOG_ERR("pinctrl_get failed\n");
  1235. return;
  1236. }
  1237. for (i = 0; i < P61_PIN_CTRL_MAX; i++) {
  1238. tmp_pinctrl = pinctrl_lookup_state(p61_dev->pinctrl, p61_pinctrl_name[i]);
  1239. if (!IS_ERR(tmp_pinctrl)) {
  1240. NFC_LOG_INFO("pinctrl[%s] found\n", p61_pinctrl_name[i]);
  1241. p61_dev->pinctrl_state[i] = tmp_pinctrl;
  1242. }
  1243. }
  1244. p61_dev->spi_pdev = spi_pdev;
  1245. #if IS_ENABLED(CONFIG_SPI_MSM_GENI)
  1246. p61_set_spi_bus_pincontrol(p61_dev);
  1247. #endif
  1248. if (nfc_check_pvdd_status())
  1249. ese_set_spi_pinctrl_for_ese_off(p61_dev);
  1250. }
  1251. void ese_set_spi_pinctrl_for_ese_off(void *p61)
  1252. {
  1253. struct p61_dev *p61_dev = (struct p61_dev *)p61;
  1254. if (!p61_dev)
  1255. p61_dev = g_p61_dev;
  1256. if (!p61_dev) {
  1257. pr_err("%s: spi probe is not called\n", __func__);
  1258. return;
  1259. }
  1260. p61_pinctrl_select(p61_dev, P61_PIN_CTRL_ESE_OFF); /* for LSI AP */
  1261. p61_pinctrl_select(p61_dev, P61_PIN_CTRL_SUSPEND); /* for QC AP */
  1262. }
  1263. #endif
  1264. /**
  1265. * \ingroup spi_driver
  1266. * \brief To probe for P61 SPI interface. If found initialize the SPI clock, bit rate & SPI mode.
  1267. * It will create the dev entry (P61) for user space.
  1268. *
  1269. * \param[in] struct spi_device *
  1270. *
  1271. * \retval 0 if ok.
  1272. *
  1273. */
  1274. static int p61_probe(struct spi_device *spi)
  1275. {
  1276. int ret = -1;
  1277. struct p61_spi_platform_data *platform_data = NULL;
  1278. struct p61_spi_platform_data platform_data1;
  1279. struct p61_dev *p61_dev = NULL;
  1280. unsigned int max_speed_hz;
  1281. struct device_node *np = spi->dev.of_node;
  1282. #ifdef P61_IRQ_ENABLE
  1283. unsigned int irq_flags;
  1284. #endif
  1285. #ifdef CONFIG_SEC_NFC_LOGGER
  1286. nfc_logger_init();
  1287. nfc_logger_set_max_count(-1);
  1288. #endif
  1289. NFC_LOG_INFO("%s chip select : %d , bus number = %d\n", __func__,
  1290. spi->chip_select, spi->master->bus_num);
  1291. memset(&platform_data1, 0x00, sizeof(struct p61_spi_platform_data));
  1292. #if !DRAGON_P61 && !IS_ENABLED(CONFIG_SAMSUNG_NFC)
  1293. platform_data = spi->dev.platform_data;
  1294. if (platform_data == NULL) {
  1295. /* RC : rename the platformdata1 name */
  1296. /* TBD: This is only for Panda as we are passing NULL for platform data */
  1297. NFC_LOG_ERR("%s : p61 probe fail\n", __func__);
  1298. platform_data1.irq_gpio = P61_IRQ;
  1299. platform_data1.rst_gpio = P61_RST;
  1300. platform_data = &platform_data1;
  1301. NFC_LOG_ERR("%s : p61 probe fail1\n", __func__);
  1302. //return -ENODEV;
  1303. }
  1304. #else
  1305. ret = p61_parse_dt(&spi->dev, &platform_data1);
  1306. if (ret) {
  1307. NFC_LOG_ERR("%s - Failed to parse DT\n", __func__);
  1308. goto err_exit;
  1309. }
  1310. platform_data = &platform_data1;
  1311. #endif
  1312. p61_dev = kzalloc(sizeof(*p61_dev), GFP_KERNEL);
  1313. if (p61_dev == NULL) {
  1314. NFC_LOG_ERR("failed to allocate memory for module data\n");
  1315. ret = -ENOMEM;
  1316. goto err_exit;
  1317. }
  1318. #if !IS_ENABLED(CONFIG_SAMSUNG_NFC)
  1319. ret = p61_hw_setup(platform_data, p61_dev, spi);
  1320. if (ret < 0) {
  1321. NFC_LOG_ERR("Failed to p61_enable_P61_IRQ_ENABLE\n");
  1322. goto err_exit0;
  1323. }
  1324. #endif
  1325. if (platform_data->gpio_coldreset) {
  1326. ret = ese_reset_gpio_setup(platform_data);
  1327. if (ret < 0) {
  1328. P61_ERR_MSG("Failed to setup ese reset gpio");
  1329. goto err_exit0;
  1330. }
  1331. }
  1332. /* gpio cold reset doesn't work. so, set gpio coldreset to false to use i2c cold reset */
  1333. platform_data->gpio_coldreset = false;
  1334. spi->bits_per_word = 8;
  1335. spi->mode = SPI_MODE_0;
  1336. ret = of_property_read_u32(np, "spi-max-frequency", &max_speed_hz);
  1337. if (ret < 0) {
  1338. NFC_LOG_ERR("%s: There's no spi-max-frequency property\n", __func__);
  1339. goto err_exit0;
  1340. }
  1341. spi->max_speed_hz = max_speed_hz;
  1342. NFC_LOG_INFO("%s : spi max_speed_hz = %d\n", __func__, spi->max_speed_hz);
  1343. //spi->chip_select = SPI_NO_CS;
  1344. ret = spi_setup(spi);
  1345. if (ret < 0) {
  1346. NFC_LOG_ERR("failed to do spi_setup()\n");
  1347. goto err_exit0;
  1348. }
  1349. p61_dev->spi = spi;
  1350. #if IS_ENABLED(CONFIG_SAMSUNG_NFC)
  1351. p61_parse_pinctrl_dt(&spi->dev, p61_dev);
  1352. #endif
  1353. #ifndef CONFIG_MAKE_NODE_USING_PLATFORM_DEVICE
  1354. p61_dev->p61_device.minor = MISC_DYNAMIC_MINOR;
  1355. p61_dev->p61_device.name = "p61";
  1356. p61_dev->p61_device.fops = &p61_dev_fops;
  1357. p61_dev->p61_device.parent = &spi->dev;
  1358. #endif
  1359. p61_dev->irq_gpio = platform_data->irq_gpio;
  1360. p61_dev->rst_gpio = platform_data->rst_gpio;
  1361. p61_dev->trusted_ese_gpio = platform_data->trusted_ese_gpio;
  1362. p61_dev->gpio_coldreset = platform_data->gpio_coldreset;
  1363. #if IS_ENABLED(CONFIG_SAMSUNG_NFC)
  1364. p61_dev->ap_vendor = platform_data->ap_vendor;
  1365. #else
  1366. p61_dev->trusted_ese_gpio = platform_data->trusted_ese_gpio;
  1367. #endif
  1368. p61_dev->ese_spi_transition_state = ESE_SPI_IDLE;
  1369. dev_set_drvdata(&spi->dev, p61_dev);
  1370. /* init mutex and queues */
  1371. init_waitqueue_head(&p61_dev->read_wq);
  1372. mutex_init(&p61_dev->read_mutex);
  1373. mutex_init(&p61_dev->write_mutex);
  1374. if (p61_dev->gpio_coldreset)
  1375. ese_reset_init();
  1376. #ifdef P61_IRQ_ENABLE
  1377. spin_lock_init(&p61_dev->irq_enabled_lock);
  1378. #endif
  1379. #if IS_ENABLED(CONFIG_SAMSUNG_NFC)
  1380. p61_dev->nfc_node = of_parse_phandle(np, "nxp,nfcc", 0);
  1381. if (!p61_dev->nfc_node) {
  1382. NFC_LOG_ERR("%s: nxp,nfcc invalid or missing in device tree\n", __func__);
  1383. goto err_exit0;
  1384. }
  1385. #else
  1386. ret = of_property_read_string(np, "nxp,nfcc", &p61_dev->nfcc_name);
  1387. if (ret < 0) {
  1388. NFC_LOG_ERR("%s: nxp,nfcc invalid or missing in device tree (%d)\n",
  1389. __func__, ret);
  1390. goto err_exit0;
  1391. }
  1392. NFC_LOG_INFO("%s: device tree set '%s' as eSE power controller\n",
  1393. __func__, p61_dev->nfcc_name);
  1394. #endif
  1395. #ifndef CONFIG_MAKE_NODE_USING_PLATFORM_DEVICE
  1396. ret = misc_register(&p61_dev->p61_device);
  1397. if (ret < 0) {
  1398. NFC_LOG_ERR("misc_register failed! %d\n", ret);
  1399. goto err_exit0;
  1400. }
  1401. #endif
  1402. #ifdef P61_IRQ_ENABLE
  1403. p61_dev->spi->irq = gpio_to_irq(platform_data->irq_gpio);
  1404. if (p61_dev->spi->irq < 0) {
  1405. NFC_LOG_ERR("gpio_to_irq request failed gpio = 0x%x\n",
  1406. platform_data->irq_gpio);
  1407. goto err_exit1;
  1408. }
  1409. /* request irq. the irq is set whenever the chip has data available
  1410. * for reading. it is cleared when all data has been read.
  1411. */
  1412. p61_dev->irq_enabled = true;
  1413. irq_flags = IRQF_TRIGGER_RISING | IRQF_ONESHOT;
  1414. ret = request_irq(p61_dev->spi->irq, p61_dev_irq_handler, irq_flags,
  1415. p61_dev->p61_device.name, p61_dev);
  1416. if (ret) {
  1417. NFC_LOG_ERR("request_irq failed\n");
  1418. goto err_exit1;
  1419. }
  1420. p61_disable_irq(p61_dev);
  1421. #endif
  1422. #if IS_ENABLED(CONFIG_SAMSUNG_NFC)
  1423. p61_dev->r_buf = kzalloc(sizeof(unsigned char) * MAX_BUFFER_SIZE, GFP_KERNEL);
  1424. if (p61_dev->r_buf == NULL) {
  1425. NFC_LOG_ERR("failed to allocate for spi read buffer\n");
  1426. ret = -ENOMEM;
  1427. goto err_exit2;
  1428. }
  1429. p61_dev->w_buf = kzalloc(sizeof(unsigned char) * MAX_BUFFER_SIZE, GFP_KERNEL);
  1430. if (p61_dev->w_buf == NULL) {
  1431. NFC_LOG_ERR("failed to allocate for spi write buffer\n");
  1432. ret = -ENOMEM;
  1433. goto err_exit3;
  1434. }
  1435. wake_lock_init(&p61_dev->ese_lock, WAKE_LOCK_SUSPEND, "ese_lock");
  1436. #if IS_ENABLED(CONFIG_SPI_MSM_GENI)
  1437. INIT_DELAYED_WORK(&p61_dev->spi_release_work, p61_spi_release_work);
  1438. wake_lock_init(&p61_dev->spi_release_wakelock, WAKE_LOCK_SUSPEND, "ese_spi_wake_lock");
  1439. #endif
  1440. #endif
  1441. p61_dev->enable_poll_mode = 0; /* Default IRQ read mode */
  1442. #if IS_ENABLED(CONFIG_SAMSUNG_NFC)
  1443. g_p61_dev = p61_dev;
  1444. #endif
  1445. NFC_LOG_INFO("Exit : %s\n", __func__);
  1446. return ret;
  1447. #if IS_ENABLED(CONFIG_SAMSUNG_NFC)
  1448. err_exit3:
  1449. kfree(p61_dev->r_buf);
  1450. err_exit2:
  1451. #endif
  1452. #ifdef P61_IRQ_ENABLE
  1453. err_exit1:
  1454. #ifndef CONFIG_MAKE_NODE_USING_PLATFORM_DEVICE
  1455. misc_deregister(&p61_dev->p61_device);
  1456. #endif
  1457. #endif
  1458. err_exit0:
  1459. mutex_destroy(&p61_dev->read_mutex);
  1460. mutex_destroy(&p61_dev->write_mutex);
  1461. if (p61_dev->gpio_coldreset)
  1462. ese_reset_deinit();
  1463. if (p61_dev != NULL)
  1464. kfree(p61_dev);
  1465. err_exit:
  1466. NFC_LOG_ERR("ERROR: Exit : %s ret %d\n", __func__, ret);
  1467. return ret;
  1468. }
  1469. #ifdef CONFIG_MAKE_NODE_USING_PLATFORM_DEVICE
  1470. static struct miscdevice p61_misc_device = {
  1471. .minor = MISC_DYNAMIC_MINOR,
  1472. .name = "p61",
  1473. .fops = &p61_dev_fops,
  1474. };
  1475. static int p61_platform_probe(struct platform_device *pdev)
  1476. {
  1477. int ret = -1;
  1478. ret = misc_register(&p61_misc_device);
  1479. if (ret < 0)
  1480. NFC_LOG_ERR("misc_register failed! %d\n", ret);
  1481. NFC_LOG_INFO("%s: finished...\n", __func__);
  1482. return 0;
  1483. }
  1484. static int p61_platform_remove(struct platform_device *pdev)
  1485. {
  1486. NFC_LOG_INFO("Entry : %s\n", __func__);
  1487. return 0;
  1488. }
  1489. static const struct of_device_id p61_secure_match_table[] = {
  1490. { .compatible = "p61_platform",},
  1491. {},
  1492. };
  1493. static struct platform_driver p61_platform_driver = {
  1494. .driver = {
  1495. .name = "p61_platform",
  1496. .owner = THIS_MODULE,
  1497. #ifdef CONFIG_OF
  1498. .of_match_table = p61_secure_match_table,
  1499. #endif
  1500. },
  1501. .probe = p61_platform_probe,
  1502. .remove = p61_platform_remove,
  1503. };
  1504. #endif /* CONFIG_MAKE_NODE_USING_PLATFORM_DEVICE */
  1505. /**
  1506. * \ingroup spi_driver
  1507. * \brief Will get called when the device is removed to release the resources.
  1508. *
  1509. * \param[in] struct spi_device
  1510. *
  1511. * \retval 0 if ok.
  1512. *
  1513. */
  1514. #if LINUX_VERSION_CODE < KERNEL_VERSION(6, 1, 0)
  1515. static int p61_remove(struct spi_device *spi)
  1516. #else
  1517. static void p61_remove(struct spi_device *spi)
  1518. #endif
  1519. {
  1520. struct p61_dev *p61_dev = p61_get_data(spi);
  1521. P61_DBG_MSG("Entry : %s\n", __func__);
  1522. #ifdef P61_HARD_RESET
  1523. if (p61_regulator != NULL) {
  1524. regulator_disable(p61_regulator);
  1525. regulator_put(p61_regulator);
  1526. } else {
  1527. NFC_LOG_ERR("ERROR %s p61_regulator not enabled\n", __func__);
  1528. }
  1529. #endif
  1530. if (p61_dev != NULL) {
  1531. gpio_free(p61_dev->rst_gpio);
  1532. #ifdef P61_IRQ_ENABLE
  1533. free_irq(p61_dev->spi->irq, p61_dev);
  1534. gpio_free(p61_dev->irq_gpio);
  1535. #endif
  1536. #if !IS_ENABLED(CONFIG_SAMSUNG_NFC)
  1537. if (gpio_is_valid(p61_dev->trusted_ese_gpio)) {
  1538. gpio_free(p61_dev->trusted_ese_gpio);
  1539. }
  1540. #endif
  1541. mutex_destroy(&p61_dev->read_mutex);
  1542. #ifdef CONFIG_MAKE_NODE_USING_PLATFORM_DEVICE
  1543. misc_deregister(&p61_misc_device);
  1544. #else
  1545. misc_deregister(&p61_dev->p61_device);
  1546. #endif
  1547. if (p61_dev->gpio_coldreset)
  1548. ese_reset_deinit();
  1549. #if IS_ENABLED(CONFIG_SAMSUNG_NFC)
  1550. wake_lock_destroy(&p61_dev->ese_lock);
  1551. #endif
  1552. kfree(p61_dev);
  1553. }
  1554. P61_DBG_MSG("Exit : %s\n", __func__);
  1555. #if LINUX_VERSION_CODE < KERNEL_VERSION(6, 1, 0)
  1556. return 0;
  1557. #endif
  1558. }
  1559. #if DRAGON_P61
  1560. static struct of_device_id p61_dt_match[] = {
  1561. {
  1562. .compatible = "p61",
  1563. },
  1564. {}
  1565. };
  1566. #endif
  1567. static struct spi_driver p61_driver = {
  1568. .driver = {
  1569. .name = "p61",
  1570. .bus = &spi_bus_type,
  1571. .owner = THIS_MODULE,
  1572. #if DRAGON_P61
  1573. .of_match_table = p61_dt_match,
  1574. #endif
  1575. },
  1576. .probe = p61_probe,
  1577. .remove = (p61_remove),
  1578. };
  1579. /**
  1580. * \ingroup spi_driver
  1581. * \brief Module init interface
  1582. *
  1583. * \param[in] void
  1584. *
  1585. * \retval handle
  1586. *
  1587. */
  1588. #if IS_MODULE(CONFIG_SAMSUNG_NFC)
  1589. int p61_dev_init(void)
  1590. {
  1591. int ret;
  1592. debug_level = P61_DEBUG_OFF;
  1593. P61_DBG_MSG("Entry : %s\n", __func__);
  1594. #ifdef CONFIG_MAKE_NODE_USING_PLATFORM_DEVICE
  1595. ret = platform_driver_register(&p61_platform_driver);
  1596. NFC_LOG_INFO("%s: platform_driver_register, ret %d\n", __func__, ret);
  1597. #endif
  1598. ret = spi_register_driver(&p61_driver);
  1599. return ret;
  1600. }
  1601. EXPORT_SYMBOL(p61_dev_init);
  1602. void p61_dev_exit(void)
  1603. {
  1604. P61_DBG_MSG("Entry : %s\n", __func__);
  1605. #ifdef CONFIG_MAKE_NODE_USING_PLATFORM_DEVICE
  1606. platform_driver_unregister(&p61_platform_driver);
  1607. #endif
  1608. spi_unregister_driver(&p61_driver);
  1609. }
  1610. EXPORT_SYMBOL(p61_dev_exit);
  1611. #else
  1612. static int __init p61_dev_init(void)
  1613. {
  1614. int ret;
  1615. debug_level = P61_DEBUG_OFF;
  1616. P61_DBG_MSG("Entry : %s\n", __func__);
  1617. #if IS_ENABLED(CONFIG_SAMSUNG_NFC)
  1618. if (nfc_get_lpcharge() == LPM_TRUE)
  1619. return 0;
  1620. #endif
  1621. #ifdef CONFIG_MAKE_NODE_USING_PLATFORM_DEVICE
  1622. ret = platform_driver_register(&p61_platform_driver);
  1623. NFC_LOG_INFO("%s: platform_driver_register, ret %d\n", __func__, ret);
  1624. #endif
  1625. ret = spi_register_driver(&p61_driver);
  1626. return ret;
  1627. }
  1628. module_init(p61_dev_init);
  1629. /**
  1630. * \ingroup spi_driver
  1631. * \brief Module exit interface
  1632. *
  1633. * \param[in] void
  1634. *
  1635. * \retval void
  1636. *
  1637. */
  1638. static void __exit p61_dev_exit(void)
  1639. {
  1640. P61_DBG_MSG("Entry : %s\n", __func__);
  1641. #if IS_ENABLED(CONFIG_SAMSUNG_NFC)
  1642. if (nfc_get_lpcharge() == LPM_TRUE)
  1643. return;
  1644. #endif
  1645. #ifdef CONFIG_MAKE_NODE_USING_PLATFORM_DEVICE
  1646. platform_driver_unregister(&p61_platform_driver);
  1647. #endif
  1648. spi_unregister_driver(&p61_driver);
  1649. }
  1650. module_exit(p61_dev_exit);
  1651. MODULE_ALIAS("spi:p61");
  1652. MODULE_AUTHOR("BHUPENDRA PAWAR");
  1653. MODULE_DESCRIPTION("NXP P61 SPI driver");
  1654. MODULE_LICENSE("GPL");
  1655. #endif
  1656. /** @} */