sec_nfc.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981
  1. /*
  2. * SAMSUNG NFC Controller
  3. *
  4. * Copyright (C) 2013 Samsung Electronics Co.Ltd
  5. * Author: Woonki Lee <woonki84.lee@samsung.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the
  9. * Free Software Foundation; either version 2 of the License, or (at your
  10. * option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. *
  21. * Last update: 2014-01-20
  22. *
  23. */
  24. #ifdef CONFIG_SEC_NFC_IF_I2C_GPIO
  25. #define CONFIG_SEC_NFC_IF_I2C
  26. #endif
  27. #include <linux/wait.h>
  28. #include <linux/delay.h>
  29. #include <linux/kernel.h>
  30. #include <linux/platform_device.h>
  31. #include <linux/mutex.h>
  32. #include <linux/module.h>
  33. #include <linux/miscdevice.h>
  34. #include <linux/gpio.h>
  35. #include <linux/slab.h>
  36. #include <linux/fs.h>
  37. #include <asm/uaccess.h>
  38. #include <linux/nfc/sec_nfc.h>
  39. #include <linux/wakelock.h>
  40. #include <linux/of_gpio.h>
  41. #include <linux/regulator/consumer.h>
  42. #ifdef CONFIG_SEC_NFC_CLK_REQ
  43. #include <linux/interrupt.h>
  44. #include <linux/clk.h>
  45. #endif
  46. #if defined(CONFIG_NFC_N5_PMC8974_CLK_REQ) || defined(CONFIG_SEC_NFC_USE_8226_BBCLK2)
  47. #include <linux/clk.h>
  48. #endif
  49. #ifndef CONFIG_SEC_NFC_IF_I2C
  50. struct sec_nfc_i2c_info {};
  51. #define sec_nfc_read NULL
  52. #define sec_nfc_write NULL
  53. #define sec_nfc_poll NULL
  54. #define sec_nfc_i2c_irq_clear(x)
  55. #define SEC_NFC_GET_INFO(dev) platform_get_drvdata(to_platform_device(dev))
  56. #else /* CONFIG_SEC_NFC_IF_I2C */
  57. #include <linux/interrupt.h>
  58. #include <linux/poll.h>
  59. #include <linux/sched.h>
  60. #include <linux/i2c.h>
  61. #define SEC_NFC_GET_INFO(dev) i2c_get_clientdata(to_i2c_client(dev))
  62. enum sec_nfc_irq {
  63. SEC_NFC_NONE,
  64. SEC_NFC_INT,
  65. SEC_NFC_SKIP,
  66. };
  67. struct sec_nfc_i2c_info {
  68. struct i2c_client *i2c_dev;
  69. struct mutex read_mutex;
  70. enum sec_nfc_irq read_irq;
  71. wait_queue_head_t read_wait;
  72. size_t buflen;
  73. u8 *buf;
  74. };
  75. #endif
  76. struct sec_nfc_info {
  77. struct miscdevice miscdev;
  78. struct mutex mutex;
  79. enum sec_nfc_mode mode;
  80. struct device *dev;
  81. struct sec_nfc_platform_data *pdata;
  82. struct sec_nfc_i2c_info i2c_info;
  83. #ifdef CONFIG_SEC_NFC_CLK_REQ
  84. bool clk_ctl;
  85. bool clk_state;
  86. #endif
  87. struct wake_lock wake_lock;
  88. struct regulator *L22;
  89. struct regulator *L6;
  90. };
  91. #ifndef CONFIG_SEC_NFC_NO_POWER_CONTROL
  92. int nfc_power_onoff(struct sec_nfc_info *info, bool onoff);
  93. #endif
  94. #ifdef CONFIG_SEC_NFC_IF_I2C
  95. static irqreturn_t sec_nfc_irq_thread_fn(int irq, void *dev_id)
  96. {
  97. struct sec_nfc_info *info = dev_id;
  98. struct sec_nfc_platform_data *pdata = info->pdata;
  99. dev_dbg(info->dev, "[NFC] Read Interrupt is occurred!\n");
  100. if(gpio_get_value(pdata->irq) == 0) {
  101. dev_err(info->dev, "[NFC] Warning,irq-gpio state is low!\n");
  102. return IRQ_HANDLED;
  103. }
  104. mutex_lock(&info->i2c_info.read_mutex);
  105. /* Skip during power switching */
  106. if (info->i2c_info.read_irq == SEC_NFC_SKIP) {
  107. mutex_unlock(&info->i2c_info.read_mutex);
  108. return IRQ_HANDLED;
  109. }
  110. info->i2c_info.read_irq = SEC_NFC_INT;
  111. mutex_unlock(&info->i2c_info.read_mutex);
  112. wake_up_interruptible(&info->i2c_info.read_wait);
  113. if(!wake_lock_active(&info->wake_lock))
  114. {
  115. dev_dbg(info->dev, "%s: Set wake_lock_timeout for 2 sec. !!!\n", __func__);
  116. wake_lock_timeout(&info->wake_lock, 2 * HZ);
  117. }
  118. return IRQ_HANDLED;
  119. }
  120. static ssize_t sec_nfc_read(struct file *file, char __user *buf,
  121. size_t count, loff_t *ppos)
  122. {
  123. struct sec_nfc_info *info = container_of(file->private_data,
  124. struct sec_nfc_info, miscdev);
  125. enum sec_nfc_irq irq;
  126. int ret = 0;
  127. dev_dbg(info->dev, "%s: info: %p, count: %zu\n", __func__,
  128. info, count);
  129. mutex_lock(&info->mutex);
  130. if (info->mode == SEC_NFC_MODE_OFF) {
  131. dev_err(info->dev, "sec_nfc is not enabled\n");
  132. ret = -ENODEV;
  133. goto out;
  134. }
  135. mutex_lock(&info->i2c_info.read_mutex);
  136. irq = info->i2c_info.read_irq;
  137. mutex_unlock(&info->i2c_info.read_mutex);
  138. if (irq == SEC_NFC_NONE) {
  139. if (file->f_flags & O_NONBLOCK) {
  140. dev_err(info->dev, "it is nonblock\n");
  141. ret = -EAGAIN;
  142. goto out;
  143. }
  144. }
  145. /* i2c recv */
  146. if (count > info->i2c_info.buflen)
  147. count = info->i2c_info.buflen;
  148. if (count > SEC_NFC_MSG_MAX_SIZE) {
  149. dev_err(info->dev, "user required wrong size :%d\n", count);
  150. ret = -EINVAL;
  151. goto out;
  152. }
  153. mutex_lock(&info->i2c_info.read_mutex);
  154. memset(info->i2c_info.buf, 0, count);
  155. ret = i2c_master_recv(info->i2c_info.i2c_dev, info->i2c_info.buf, count);
  156. dev_dbg(info->dev, "recv size : %d\n", ret);
  157. if (ret == -EREMOTEIO) {
  158. ret = -ERESTART;
  159. goto read_error;
  160. } else if (ret != count) {
  161. dev_err(info->dev, "read failed: return: %d count: %d\n",
  162. ret, count);
  163. //ret = -EREMOTEIO;
  164. goto read_error;
  165. }
  166. info->i2c_info.read_irq = SEC_NFC_NONE;
  167. mutex_unlock(&info->i2c_info.read_mutex);
  168. if (copy_to_user(buf, info->i2c_info.buf, ret)) {
  169. dev_err(info->dev, "copy failed to user\n");
  170. ret = -EFAULT;
  171. }
  172. goto out;
  173. read_error:
  174. info->i2c_info.read_irq = SEC_NFC_NONE;
  175. mutex_unlock(&info->i2c_info.read_mutex);
  176. out:
  177. mutex_unlock(&info->mutex);
  178. return ret;
  179. }
  180. static ssize_t sec_nfc_write(struct file *file, const char __user *buf,
  181. size_t count, loff_t *ppos)
  182. {
  183. struct sec_nfc_info *info = container_of(file->private_data,
  184. struct sec_nfc_info, miscdev);
  185. int ret = 0;
  186. dev_dbg(info->dev, "%s: info: %p, count %zu\n", __func__,
  187. info, count);
  188. mutex_lock(&info->mutex);
  189. if (info->mode == SEC_NFC_MODE_OFF) {
  190. dev_err(info->dev, "sec_nfc is not enabled\n");
  191. ret = -ENODEV;
  192. goto out;
  193. }
  194. if (count > info->i2c_info.buflen)
  195. count = info->i2c_info.buflen;
  196. if (count > SEC_NFC_MSG_MAX_SIZE) {
  197. dev_err(info->dev, "user required wrong size :%d\n", count);
  198. ret = -EINVAL;
  199. goto out;
  200. }
  201. if (copy_from_user(info->i2c_info.buf, buf, count)) {
  202. dev_err(info->dev, "copy failed from user\n");
  203. ret = -EFAULT;
  204. goto out;
  205. }
  206. /* Skip interrupt during power switching
  207. * It is released after first write */
  208. mutex_lock(&info->i2c_info.read_mutex);
  209. ret = i2c_master_send(info->i2c_info.i2c_dev, info->i2c_info.buf, count);
  210. if (info->i2c_info.read_irq == SEC_NFC_SKIP)
  211. info->i2c_info.read_irq = SEC_NFC_NONE;
  212. mutex_unlock(&info->i2c_info.read_mutex);
  213. if (ret == -EREMOTEIO) {
  214. dev_err(info->dev, "send failed: return: %d count: %d\n",
  215. ret, count);
  216. ret = -ERESTART;
  217. goto out;
  218. }
  219. if (ret != count) {
  220. dev_err(info->dev, "send failed: return: %d count: %d\n",
  221. ret, count);
  222. ret = -EREMOTEIO;
  223. }
  224. out:
  225. mutex_unlock(&info->mutex);
  226. return ret;
  227. }
  228. static unsigned int sec_nfc_poll(struct file *file, poll_table *wait)
  229. {
  230. struct sec_nfc_info *info = container_of(file->private_data,
  231. struct sec_nfc_info, miscdev);
  232. enum sec_nfc_irq irq;
  233. int ret = 0;
  234. dev_dbg(info->dev, "%s: info: %p\n", __func__, info);
  235. mutex_lock(&info->mutex);
  236. if (info->mode == SEC_NFC_MODE_OFF) {
  237. dev_err(info->dev, "sec_nfc is not enabled\n");
  238. ret = -ENODEV;
  239. goto out;
  240. }
  241. poll_wait(file, &info->i2c_info.read_wait, wait);
  242. mutex_lock(&info->i2c_info.read_mutex);
  243. irq = info->i2c_info.read_irq;
  244. if (irq == SEC_NFC_INT)
  245. ret = (POLLIN | POLLRDNORM);
  246. mutex_unlock(&info->i2c_info.read_mutex);
  247. out:
  248. mutex_unlock(&info->mutex);
  249. return ret;
  250. }
  251. void sec_nfc_i2c_irq_clear(struct sec_nfc_info *info)
  252. {
  253. /* clear interrupt. Interrupt will be occured at power off */
  254. mutex_lock(&info->i2c_info.read_mutex);
  255. info->i2c_info.read_irq = SEC_NFC_NONE;
  256. mutex_unlock(&info->i2c_info.read_mutex);
  257. }
  258. int sec_nfc_i2c_probe(struct i2c_client *client)
  259. {
  260. struct device *dev = &client->dev;
  261. struct sec_nfc_info *info = dev_get_drvdata(dev);
  262. struct sec_nfc_platform_data *pdata = info->pdata;
  263. int ret;
  264. pr_err("\n %s start", __func__);
  265. pr_err("info : %p", info);
  266. pr_err("pdata : %p", pdata);
  267. pr_err("pdata->irq: %d", pdata->irq);
  268. pr_err("client->irq:%d", client->irq);
  269. //pdata->irq = client->irq;
  270. info->i2c_info.buflen = SEC_NFC_MAX_BUFFER_SIZE;
  271. info->i2c_info.buf = kzalloc(SEC_NFC_MAX_BUFFER_SIZE, GFP_KERNEL);
  272. if (!info->i2c_info.buf) {
  273. dev_err(dev,
  274. "failed to allocate memory for sec_nfc_info->buf\n");
  275. return -ENOMEM;
  276. }
  277. info->i2c_info.i2c_dev = client;
  278. info->i2c_info.read_irq = SEC_NFC_NONE;
  279. mutex_init(&info->i2c_info.read_mutex);
  280. init_waitqueue_head(&info->i2c_info.read_wait);
  281. i2c_set_clientdata(client, info);
  282. #ifndef CONFIG_SEC_NFC_NO_POWER_CONTROL
  283. #if !defined(CONFIG_MACH_KSPORTSLTE_SPR)&& !defined(CONFIG_MACH_SLTE_SPR)
  284. nfc_power_onoff(info,1);
  285. #endif
  286. #endif
  287. ret = request_threaded_irq(client->irq, NULL, sec_nfc_irq_thread_fn,
  288. IRQF_TRIGGER_RISING | IRQF_ONESHOT, SEC_NFC_DRIVER_NAME,
  289. info);
  290. if (ret < 0) {
  291. dev_err(dev, "failed to register IRQ handler\n");
  292. kfree(info->i2c_info.buf);
  293. return ret;
  294. }
  295. pr_err("\n %s success", __func__);
  296. return 0;
  297. }
  298. void sec_nfc_i2c_remove(struct device *dev)
  299. {
  300. struct sec_nfc_info *info = dev_get_drvdata(dev);
  301. struct i2c_client *client = info->i2c_info.i2c_dev;
  302. struct sec_nfc_platform_data *pdata = info->pdata;
  303. free_irq(client->irq, info);
  304. gpio_free(pdata->irq);
  305. }
  306. #endif /* CONFIG_SEC_NFC_IF_I2C */
  307. #ifdef CONFIG_SEC_NFC_CLK_REQ
  308. #ifdef CONFIG_SEC_NFC_USE_8226_RFCLK2
  309. /* PMIC */
  310. #define sec_nfc_clk_on(clk) clk_prepare_enable(clk)
  311. #define sec_nfc_clk_off(clk) clk_disable_unprepare(clk)
  312. #else
  313. /* default GPIO */
  314. #define sec_nfc_clk_on(clk) gpio_set_value(clk, 1)
  315. #define sec_nfc_clk_off(clk) gpio_set_value(clk, 0)
  316. #endif
  317. static irqreturn_t sec_nfc_clk_irq_thread(int irq, void *dev_id)
  318. {
  319. struct sec_nfc_info *info = dev_id;
  320. struct sec_nfc_platform_data *pdata = info->pdata;
  321. bool value;
  322. value = gpio_get_value(pdata->clk_req) > 0 ? false : true;
  323. if (value == info->clk_state)
  324. return IRQ_HANDLED;
  325. //pr_err("\n %s: %s", __func__, value ? "True" : "False");
  326. if (value)
  327. sec_nfc_clk_on(pdata->clk_enable);
  328. else
  329. sec_nfc_clk_off(pdata->clk_enable);
  330. info->clk_state = value;
  331. return IRQ_HANDLED;
  332. }
  333. void sec_nfc_clk_ctl_enable(struct sec_nfc_info *info)
  334. {
  335. struct sec_nfc_platform_data *pdata = info->pdata;
  336. unsigned int irq = gpio_to_irq(pdata->clk_req);
  337. int ret;
  338. pr_err("\n %s start", __func__);
  339. if (info->clk_ctl)
  340. return;
  341. ret = gpio_request(pdata->clk_req, "nfc-ex-clk");
  342. if (ret) {
  343. dev_err(info->dev, "failed to get gpio ven\n");
  344. return;
  345. }
  346. info->clk_state = false;
  347. ret = request_threaded_irq(irq, NULL, sec_nfc_clk_irq_thread,
  348. IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
  349. SEC_NFC_DRIVER_NAME, info);
  350. if (ret < 0) {
  351. dev_err(info->dev, "failed to register CLK REQ IRQ handler\n");
  352. }
  353. info->clk_ctl = true;
  354. }
  355. void sec_nfc_clk_ctl_disable(struct sec_nfc_info *info)
  356. {
  357. struct sec_nfc_platform_data *pdata = info->pdata;
  358. unsigned int irq = gpio_to_irq(pdata->clk_req);
  359. pr_err("\n %s start", __func__);
  360. if (!info->clk_ctl)
  361. return;
  362. free_irq(irq, info);
  363. gpio_free(pdata->clk_req);
  364. if (info->clk_state)
  365. sec_nfc_clk_off(pdata->clk_enable);
  366. info->clk_state = false;
  367. info->clk_ctl = false;
  368. }
  369. #else
  370. #define sec_nfc_clk_ctl_enable(x)
  371. #define sec_nfc_clk_ctl_disable(x)
  372. #endif /* CONFIG_SEC_NFC_CLK_REQ */
  373. static void sec_nfc_set_mode(struct sec_nfc_info *info,
  374. enum sec_nfc_mode mode)
  375. {
  376. struct sec_nfc_platform_data *pdata = info->pdata;
  377. /* intfo lock is aleady gotten before calling this function */
  378. if (info->mode == mode) {
  379. dev_dbg(info->dev, "Power mode is already %d", mode);
  380. return;
  381. }
  382. pr_err("\n %s start %d", __func__, mode);
  383. /* intfo lock is aleady getten before calling this function */
  384. info->mode = mode;
  385. #ifdef CONFIG_SEC_NFC_IF_I2C
  386. /* Skip during power switching */
  387. mutex_lock(&info->i2c_info.read_mutex);
  388. info->i2c_info.read_irq = SEC_NFC_SKIP;
  389. mutex_unlock(&info->i2c_info.read_mutex);
  390. #endif
  391. gpio_set_value_cansleep(pdata->ven, 0);
  392. gpio_set_value(pdata->ven, SEC_NFC_PW_OFF);
  393. if (pdata->firm) gpio_set_value(pdata->firm, SEC_NFC_FW_OFF);
  394. if (mode == SEC_NFC_MODE_BOOTLOADER)
  395. if (pdata->firm) gpio_set_value(pdata->firm, SEC_NFC_FW_ON);
  396. if (mode != SEC_NFC_MODE_OFF) {
  397. msleep(SEC_NFC_VEN_WAIT_TIME);
  398. gpio_set_value_cansleep(pdata->ven, 1);
  399. gpio_set_value(pdata->ven, SEC_NFC_PW_ON);
  400. sec_nfc_clk_ctl_enable(info);
  401. #ifdef CONFIG_SEC_NFC_IF_I2C
  402. enable_irq_wake(info->i2c_info.i2c_dev->irq);
  403. #endif
  404. msleep(SEC_NFC_VEN_WAIT_TIME/2);
  405. } else {
  406. sec_nfc_clk_ctl_disable(info);
  407. #ifdef CONFIG_SEC_NFC_IF_I2C
  408. disable_irq_wake(info->i2c_info.i2c_dev->irq);
  409. #endif
  410. }
  411. dev_dbg(info->dev, "Power mode is : %d\n", mode);
  412. }
  413. static long sec_nfc_ioctl(struct file *file, unsigned int cmd,
  414. unsigned long arg)
  415. {
  416. struct sec_nfc_info *info = container_of(file->private_data,
  417. struct sec_nfc_info, miscdev);
  418. #ifdef CONFIG_SEC_NFC_PRODUCT_N5
  419. struct sec_nfc_platform_data *pdata = info->pdata;
  420. #endif
  421. unsigned int new = (unsigned int)arg;
  422. int ret = 0;
  423. dev_dbg(info->dev, "%s: info: %p, cmd: 0x%x\n",
  424. __func__, info, cmd);
  425. mutex_lock(&info->mutex);
  426. switch (cmd) {
  427. case SEC_NFC_SET_MODE:
  428. dev_dbg(info->dev, "%s: SEC_NFC_SET_MODE\n", __func__);
  429. if (info->mode == new)
  430. break;
  431. if (new >= SEC_NFC_MODE_COUNT) {
  432. dev_err(info->dev, "wrong mode (%d)\n", new);
  433. ret = -EFAULT;
  434. break;
  435. }
  436. sec_nfc_set_mode(info, new);
  437. break;
  438. #ifdef CONFIG_SEC_NFC_PRODUCT_N3
  439. case SEC_NFC_SLEEP:
  440. case SEC_NFC_WAKEUP:
  441. break;
  442. #elif defined(CONFIG_SEC_NFC_PRODUCT_N5)
  443. case SEC_NFC_SLEEP:
  444. if (info->mode != SEC_NFC_MODE_BOOTLOADER) {
  445. if(wake_lock_active(&info->wake_lock))
  446. wake_unlock(&info->wake_lock);
  447. gpio_set_value(pdata->wake, SEC_NFC_WAKE_SLEEP);
  448. }
  449. break;
  450. case SEC_NFC_WAKEUP:
  451. if (info->mode != SEC_NFC_MODE_BOOTLOADER) {
  452. gpio_set_value(pdata->wake, SEC_NFC_WAKE_UP);
  453. if(!wake_lock_active(&info->wake_lock))
  454. wake_lock(&info->wake_lock);
  455. }
  456. break;
  457. #endif
  458. default:
  459. dev_err(info->dev, "Unknow ioctl 0x%x\n", cmd);
  460. ret = -ENOIOCTLCMD;
  461. break;
  462. }
  463. mutex_unlock(&info->mutex);
  464. return ret;
  465. }
  466. static int sec_nfc_open(struct inode *inode, struct file *file)
  467. {
  468. struct sec_nfc_info *info = container_of(file->private_data,
  469. struct sec_nfc_info, miscdev);
  470. int ret = 0;
  471. pr_err("\n %s start", __func__);
  472. dev_dbg(info->dev, "%s: info : %p" , __func__, info);
  473. mutex_lock(&info->mutex);
  474. if (info->mode != SEC_NFC_MODE_OFF) {
  475. dev_err(info->dev, "sec_nfc is busy\n");
  476. ret = -EBUSY;
  477. goto out;
  478. }
  479. sec_nfc_set_mode(info, SEC_NFC_MODE_OFF);
  480. out:
  481. mutex_unlock(&info->mutex);
  482. return ret;
  483. }
  484. static int sec_nfc_close(struct inode *inode, struct file *file)
  485. {
  486. struct sec_nfc_info *info = container_of(file->private_data,
  487. struct sec_nfc_info, miscdev);
  488. pr_err("\n %s start", __func__);
  489. dev_dbg(info->dev, "%s: info : %p" , __func__, info);
  490. mutex_lock(&info->mutex);
  491. sec_nfc_set_mode(info, SEC_NFC_MODE_OFF);
  492. mutex_unlock(&info->mutex);
  493. return 0;
  494. }
  495. static const struct file_operations sec_nfc_fops = {
  496. .owner = THIS_MODULE,
  497. .read = sec_nfc_read,
  498. .write = sec_nfc_write,
  499. .poll = sec_nfc_poll,
  500. .open = sec_nfc_open,
  501. .release = sec_nfc_close,
  502. .unlocked_ioctl = sec_nfc_ioctl,
  503. };
  504. #ifndef CONFIG_SEC_NFC_NO_POWER_CONTROL
  505. int nfc_power_onoff(struct sec_nfc_info *data, bool onoff)
  506. {
  507. int ret = -1;
  508. if (!data->L22) {
  509. data->L22 = devm_regulator_get(data->dev, "nfc_ldo");
  510. if (!data->L22) {
  511. pr_err("%s: regulator pointer null 8941_L22, rc=%d\n",
  512. __func__, ret);
  513. return ret;
  514. }
  515. ret = regulator_set_voltage(data->L22, 3000000, 3000000);
  516. if (ret) {
  517. pr_err("%s: set voltage failed on 8941_L22, rc=%d\n",
  518. __func__, ret);
  519. return ret;
  520. }
  521. }
  522. if (!data->L6) {
  523. data->L6 = devm_regulator_get(data->dev, "vdd_nfc");
  524. if (!data->L6) {
  525. pr_err("%s: regulator pointer null 8941_l6, rc=%d\n",
  526. __func__, ret);
  527. return ret;
  528. }
  529. /*
  530. ret = regulator_set_voltage(data->L6, 1800000, 1800000);
  531. if (ret) {
  532. pr_err("%s: set voltage failed on 8941_l6, rc=%d\n",
  533. __func__, ret);
  534. return ret;
  535. }
  536. */
  537. }
  538. if(onoff){
  539. #if !defined(CONFIG_MACH_ATLANTICLTE_VZW) && !defined(CONFIG_MACH_ATLANTICLTE_ATT) && !defined(CONFIG_MACH_ATLANTICLTE_USC)
  540. ret = regulator_enable(data->L22);
  541. regulator_set_mode(data->L22, REGULATOR_MODE_NORMAL);
  542. if (ret) {
  543. pr_err("%s: Failed to enable regulator L22.\n",
  544. __func__);
  545. return ret;
  546. }
  547. #endif
  548. ret = regulator_enable(data->L6);
  549. if (ret) {
  550. pr_err("%s: Failed to enable regulator L6.\n",
  551. __func__);
  552. return ret;
  553. }
  554. }
  555. else {
  556. #if !defined(CONFIG_MACH_ATLANTICLTE_VZW) && !defined(CONFIG_MACH_ATLANTICLTE_ATT) && !defined(CONFIG_MACH_ATLANTICLTE_USC)
  557. ret = regulator_disable(data->L22);
  558. if (ret) {
  559. pr_err("%s: Failed to disable regulatorL22.\n",
  560. __func__);
  561. return ret;
  562. }
  563. #endif
  564. ret = regulator_disable(data->L6);
  565. if (ret) {
  566. pr_err("%s: Failed to disable regulator L6.\n",
  567. __func__);
  568. return ret;
  569. }
  570. }
  571. return 0;
  572. }
  573. #endif
  574. #ifdef CONFIG_PM
  575. static int sec_nfc_suspend(struct device *dev)
  576. {
  577. struct sec_nfc_info *info = SEC_NFC_GET_INFO(dev);
  578. int ret = 0;
  579. mutex_lock(&info->mutex);
  580. if (info->mode == SEC_NFC_MODE_BOOTLOADER)
  581. ret = -EPERM;
  582. mutex_unlock(&info->mutex);
  583. #ifndef CONFIG_SEC_NFC_NO_POWER_CONTROL
  584. #if !defined(CONFIG_MACH_KSPORTSLTE_SPR)&& !defined(CONFIG_MACH_SLTE_SPR)
  585. nfc_power_onoff(info,0);
  586. #endif
  587. #endif
  588. return ret;
  589. }
  590. static int sec_nfc_resume(struct device *dev)
  591. {
  592. #ifndef CONFIG_SEC_NFC_NO_POWER_CONTROL
  593. #if !defined(CONFIG_MACH_KSPORTSLTE_SPR)&& !defined(CONFIG_MACH_SLTE_SPR)
  594. struct sec_nfc_info *info = SEC_NFC_GET_INFO(dev);
  595. nfc_power_onoff(info,1);
  596. #endif
  597. #endif
  598. return 0;
  599. }
  600. static SIMPLE_DEV_PM_OPS(sec_nfc_pm_ops, sec_nfc_suspend, sec_nfc_resume);
  601. #endif
  602. #ifdef CONFIG_OF
  603. /*device tree parsing*/
  604. static int sec_nfc_parse_dt(struct device *dev,
  605. struct sec_nfc_platform_data *pdata)
  606. {
  607. struct device_node *np = dev->of_node;
  608. pdata->ven = of_get_named_gpio_flags(np, "sec-nfc,ven-gpio",
  609. 0, &pdata->ven_gpio_flags);
  610. pdata->firm = of_get_named_gpio_flags(np, "sec-nfc,firm-gpio",
  611. 0, &pdata->firm_gpio_flags);
  612. pdata->wake = pdata->firm;
  613. pdata->irq = of_get_named_gpio_flags(np, "sec-nfc,irq-gpio",
  614. 0, &pdata->irq_gpio_flags);
  615. #ifdef CONFIG_SEC_NFC_CLK_REQ
  616. pdata->clk_req = of_get_named_gpio_flags(np, "sec-nfc,clk_req-gpio",
  617. 0, &pdata->irq_gpio_flags);
  618. #ifdef CONFIG_SEC_NFC_USE_8226_RFCLK2
  619. pdata->clk_enable = clk_get(dev, "nfc_clock");
  620. #else
  621. pdata->clk_enable = of_get_named_gpio_flags(np, "sec-nfc,ext_clk-gpio",
  622. 0, &pdata->irq_gpio_flags);
  623. #endif
  624. #endif
  625. if (pdata->firm < 0)
  626. of_property_read_u32(np, "sec-nfc,firm-expander-gpio", &pdata->firm);
  627. pdata->wake = pdata->firm;
  628. pr_info("%s: irq : %d, ven : %d, firm : %d\n",
  629. __func__, pdata->irq, pdata->ven, pdata->firm);
  630. return 0;
  631. }
  632. #else
  633. static int sec_nfc_parse_dt(struct device *dev,
  634. struct sec_nfc_platform_data *pdata)
  635. {
  636. return -ENODEV;
  637. }
  638. #endif
  639. static int __sec_nfc_probe(struct device *dev)
  640. {
  641. struct sec_nfc_info *info;
  642. struct sec_nfc_platform_data *pdata = NULL;
  643. int ret = 0;
  644. pr_err("\n %s start", __func__);
  645. if (dev->of_node) {
  646. pdata = devm_kzalloc(dev,
  647. sizeof(struct sec_nfc_platform_data), GFP_KERNEL);
  648. if (!pdata) {
  649. dev_err(dev, "Failed to allocate memory\n");
  650. return -ENOMEM;
  651. }
  652. ret = sec_nfc_parse_dt(dev, pdata);
  653. if (ret)
  654. return ret;
  655. } else {
  656. pdata = dev->platform_data;
  657. }
  658. if (!pdata) {
  659. dev_err(dev, "No platform data\n");
  660. ret = -ENOMEM;
  661. goto err_pdata;
  662. }
  663. #ifdef CONFIG_SEC_NFC_USE_8226_BBCLK2
  664. pdata->nfc_clk = clk_get(NULL, "nfc_clock");
  665. if (IS_ERR(pdata->nfc_clk)) {
  666. ret = PTR_ERR(pdata->nfc_clk);
  667. printk(KERN_ERR "%s: Couldn't get D1 (%d)\n",
  668. __func__, ret);
  669. } else {
  670. if (clk_prepare_enable(pdata->nfc_clk))
  671. printk(KERN_ERR "%s: Couldn't prepare D1\n",
  672. __func__);
  673. }
  674. #endif
  675. #ifdef CONFIG_NFC_N5_PMC8974_CLK_REQ
  676. pdata->nfc_clk = clk_get(NULL, "nfc_clk");
  677. if (IS_ERR(pdata->nfc_clk)) {
  678. ret = PTR_ERR(pdata->nfc_clk);
  679. printk(KERN_ERR "%s: Couldn't get D1 (%d)\n",
  680. __func__, ret);
  681. } else {
  682. if (clk_prepare_enable(pdata->nfc_clk))
  683. printk(KERN_ERR "%s: Couldn't prepare D1\n",
  684. __func__);
  685. }
  686. #endif
  687. info = kzalloc(sizeof(struct sec_nfc_info), GFP_KERNEL);
  688. if (!info) {
  689. dev_err(dev, "failed to allocate memory for sec_nfc_info\n");
  690. ret = -ENOMEM;
  691. goto err_info_alloc;
  692. }
  693. info->dev = dev;
  694. info->pdata = pdata;
  695. info->mode = SEC_NFC_MODE_OFF;
  696. mutex_init(&info->mutex);
  697. dev_set_drvdata(dev, info);
  698. info->miscdev.minor = MISC_DYNAMIC_MINOR;
  699. info->miscdev.name = SEC_NFC_DRIVER_NAME;
  700. info->miscdev.fops = &sec_nfc_fops;
  701. info->miscdev.parent = dev;
  702. ret = misc_register(&info->miscdev);
  703. if (ret < 0) {
  704. dev_err(dev, "failed to register Device\n");
  705. goto err_dev_reg;
  706. }
  707. ret = gpio_request(pdata->ven, "ven-gpio");
  708. if (ret) {
  709. dev_err(dev, "failed to get gpio ven\n");
  710. goto err_gpio_ven;
  711. }
  712. gpio_direction_output(pdata->ven, SEC_NFC_PW_OFF);
  713. if (pdata->firm)
  714. {
  715. ret = gpio_request(pdata->firm, "firm-gpio");
  716. if (ret) {
  717. dev_err(dev, "failed to get gpio firm\n");
  718. goto err_gpio_firm;
  719. }
  720. gpio_direction_output(pdata->firm, SEC_NFC_FW_OFF);
  721. }
  722. wake_lock_init(&info->wake_lock, WAKE_LOCK_SUSPEND, "NFCWAKE");
  723. dev_dbg(dev, "%s: info: %p, pdata %p\n", __func__, info, pdata);
  724. pr_err("\n %s success", __func__);
  725. return 0;
  726. err_gpio_firm:
  727. gpio_free(pdata->ven);
  728. err_gpio_ven:
  729. err_dev_reg:
  730. err_info_alloc:
  731. kfree(info);
  732. err_pdata:
  733. return ret;
  734. }
  735. static int __sec_nfc_remove(struct device *dev)
  736. {
  737. struct sec_nfc_info *info = dev_get_drvdata(dev);
  738. struct sec_nfc_platform_data *pdata = info->pdata;
  739. dev_dbg(info->dev, "%s\n", __func__);
  740. #if defined(CONFIG_NFC_N5_PMC8974_CLK_REQ) || defined(CONFIG_SEC_NFC_USE_8226_BBCLK2)
  741. if (pdata->nfc_clk)
  742. clk_unprepare(pdata->nfc_clk);
  743. #endif
  744. misc_deregister(&info->miscdev);
  745. sec_nfc_set_mode(info, SEC_NFC_MODE_OFF);
  746. gpio_set_value(pdata->firm, 0);
  747. gpio_set_value_cansleep(pdata->ven, 0);
  748. gpio_free(pdata->ven);
  749. if (pdata->firm) gpio_free(pdata->firm);
  750. wake_lock_destroy(&info->wake_lock);
  751. kfree(info);
  752. return 0;
  753. }
  754. #ifdef CONFIG_SEC_NFC_IF_I2C
  755. MODULE_DEVICE_TABLE(i2c, sec_nfc_id_table);
  756. typedef struct i2c_driver sec_nfc_driver_type;
  757. #define SEC_NFC_INIT(driver) i2c_add_driver(driver);
  758. #define SEC_NFC_EXIT(driver) i2c_del_driver(driver);
  759. static int __devinit sec_nfc_probe(struct i2c_client *client,
  760. const struct i2c_device_id *id)
  761. {
  762. int ret = 0;
  763. ret = __sec_nfc_probe(&client->dev);
  764. if (ret)
  765. return ret;
  766. if (sec_nfc_i2c_probe(client))
  767. __sec_nfc_remove(&client->dev);
  768. return ret;
  769. }
  770. static int __devexit sec_nfc_remove(struct i2c_client *client)
  771. {
  772. sec_nfc_i2c_remove(&client->dev);
  773. return __sec_nfc_remove(&client->dev);
  774. }
  775. static struct i2c_device_id sec_nfc_id_table[] = {
  776. { SEC_NFC_DRIVER_NAME, 0 },
  777. { }
  778. };
  779. #else /* CONFIG_SEC_NFC_IF_I2C */
  780. MODULE_DEVICE_TABLE(platform, sec_nfc_id_table);
  781. typedef struct platform_driver sec_nfc_driver_type;
  782. #define SEC_NFC_INIT(driver) platform_driver_register(driver);
  783. #define SEC_NFC_EXIT(driver) platform_driver_unregister(driver);
  784. static int __devinit sec_nfc_probe(struct platform_device *pdev)
  785. {
  786. return __sec_nfc_probe(&pdev->dev);
  787. }
  788. static int __devexit sec_nfc_remove(struct platform_device *pdev)
  789. {
  790. return __sec_nfc_remove(&pdev->dev);
  791. }
  792. static struct platform_device_id sec_nfc_id_table[] = {
  793. { SEC_NFC_DRIVER_NAME, 0 },
  794. { }
  795. };
  796. #endif /* CONFIG_SEC_NFC_IF_I2C */
  797. #ifdef CONFIG_OF
  798. static struct of_device_id nfc_match_table[] = {
  799. { .compatible = SEC_NFC_DRIVER_NAME,},
  800. {},
  801. };
  802. #else
  803. #define nfc_match_table NULL
  804. #endif
  805. static sec_nfc_driver_type sec_nfc_driver = {
  806. .probe = sec_nfc_probe,
  807. .id_table = sec_nfc_id_table,
  808. .remove = sec_nfc_remove,
  809. .driver = {
  810. .name = SEC_NFC_DRIVER_NAME,
  811. #ifdef CONFIG_PM
  812. .pm = &sec_nfc_pm_ops,
  813. #endif
  814. .of_match_table = nfc_match_table,
  815. },
  816. };
  817. static int __init sec_nfc_init(void)
  818. {
  819. return SEC_NFC_INIT(&sec_nfc_driver);
  820. }
  821. static void __exit sec_nfc_exit(void)
  822. {
  823. SEC_NFC_EXIT(&sec_nfc_driver);
  824. }
  825. module_init(sec_nfc_init);
  826. module_exit(sec_nfc_exit);
  827. MODULE_DESCRIPTION("Samsung sec_nfc driver");
  828. MODULE_LICENSE("GPL");