common.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004
  1. /******************************************************************************
  2. * Copyright (C) 2015, The Linux Foundation. All rights reserved.
  3. * Copyright (C) 2019-2021 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. #include <linux/gpio.h>
  21. #include <linux/of_gpio.h>
  22. #include <linux/of_platform.h>
  23. #include <linux/delay.h>
  24. #include <linux/version.h>
  25. #include <linux/module.h>
  26. #include <linux/uaccess.h>
  27. #if IS_ENABLED(CONFIG_SAMSUNG_NFC)
  28. #include <linux/regulator/consumer.h>
  29. #include <linux/pinctrl/consumer.h>
  30. #ifdef CONFIG_NFC_SN2XX_ESE_SUPPORT
  31. #include "p73.h"
  32. #endif
  33. #endif
  34. #include "common_ese.h"
  35. #if IS_ENABLED(CONFIG_SAMSUNG_NFC)
  36. static int nfc_param_lpcharge = LPM_NO_SUPPORT;
  37. module_param(nfc_param_lpcharge, int, 0440);
  38. struct nfc_dev *g_nfc_dev;
  39. static bool g_is_nfc_pvdd_enabled;
  40. #endif
  41. #if IS_ENABLED(CONFIG_SAMSUNG_NFC)
  42. enum lpm_status nfc_get_lpcharge(void)
  43. {
  44. return nfc_param_lpcharge;
  45. }
  46. void nfc_set_i2c_pinctrl(struct device *dev, char *pinctrl_name)
  47. {
  48. struct device_node *np = dev->of_node;
  49. struct device_node *i2c_np = of_get_parent(np);
  50. struct platform_device *i2c_pdev;
  51. struct pinctrl *pinctrl_i2c;
  52. i2c_pdev = of_find_device_by_node(i2c_np);
  53. if (!i2c_pdev) {
  54. NFC_LOG_ERR("i2c pdev not found\n");
  55. return;
  56. }
  57. pinctrl_i2c = devm_pinctrl_get_select(&i2c_pdev->dev, pinctrl_name);
  58. if (IS_ERR_OR_NULL(pinctrl_i2c)) {
  59. NFC_LOG_ERR("No %s pinctrl %ld\n", pinctrl_name, PTR_ERR(pinctrl_i2c));
  60. } else {
  61. devm_pinctrl_put(pinctrl_i2c);
  62. NFC_LOG_INFO("%s pinctrl done\n", pinctrl_name);
  63. }
  64. }
  65. #endif
  66. #ifdef CONFIG_MAKE_NODE_USING_PLATFORM_DEVICE
  67. struct nfc_dev g_nfc_dev_for_chrdev;
  68. void nfc_parse_dt_for_platform_device(struct device *dev)
  69. {
  70. struct device_node *np = dev->of_node;
  71. struct platform_configs *nfc_configs = &g_nfc_dev_for_chrdev.configs;
  72. struct platform_gpio *nfc_gpio = &nfc_configs->gpio;
  73. nfc_configs->late_pvdd_en = of_property_read_bool(np, "pn547,late_pvdd_en");
  74. NFC_LOG_INFO("late_pvdd_en :%d, lpcharge :%d\n", nfc_configs->late_pvdd_en, nfc_get_lpcharge());
  75. if (nfc_get_lpcharge() == LPM_FALSE)
  76. nfc_configs->late_pvdd_en = false;
  77. if (nfc_configs->late_pvdd_en) {
  78. nfc_gpio->ven = of_get_named_gpio(np, DTS_VEN_GPIO_STR, 0);
  79. if (!gpio_is_valid(nfc_gpio->ven))
  80. NFC_LOG_ERR("%s: ven gpio invalid %d\n", __func__, nfc_gpio->ven);
  81. else
  82. configure_gpio(nfc_gpio->ven, GPIO_OUTPUT);
  83. nfc_configs->nfc_pvdd = regulator_get(dev, "nfc_pvdd");
  84. if (IS_ERR(nfc_configs->nfc_pvdd))
  85. NFC_LOG_ERR("get nfc_pvdd error\n");
  86. else
  87. NFC_LOG_INFO("LDO nfc_pvdd: %pK, vol:%d\n",
  88. nfc_configs->nfc_pvdd, regulator_get_voltage(nfc_configs->nfc_pvdd));
  89. }
  90. }
  91. #endif
  92. int nfc_parse_dt(struct device *dev, struct platform_configs *nfc_configs,
  93. uint8_t interface)
  94. {
  95. struct device_node *np = dev->of_node;
  96. struct platform_gpio *nfc_gpio = &nfc_configs->gpio;
  97. #if IS_ENABLED(CONFIG_SAMSUNG_NFC)
  98. const char *ap_str;
  99. static int retry_count = 3;
  100. #endif
  101. if (!np) {
  102. NFC_LOG_ERR("%s: nfc of_node NULL\n", __func__);
  103. return -EINVAL;
  104. }
  105. nfc_gpio->irq = -EINVAL;
  106. nfc_gpio->dwl_req = -EINVAL;
  107. nfc_gpio->ven = -EINVAL;
  108. /* irq required for i2c based chips only */
  109. if (interface == PLATFORM_IF_I2C) {
  110. nfc_gpio->irq = of_get_named_gpio(np, DTS_IRQ_GPIO_STR, 0);
  111. if ((!gpio_is_valid(nfc_gpio->irq))) {
  112. NFC_LOG_ERR("%s: irq gpio invalid %d\n", __func__,
  113. nfc_gpio->irq);
  114. return -EINVAL;
  115. }
  116. NFC_LOG_INFO("%s: irq %d\n", __func__, nfc_gpio->irq);
  117. }
  118. nfc_gpio->ven = of_get_named_gpio(np, DTS_VEN_GPIO_STR, 0);
  119. if ((!gpio_is_valid(nfc_gpio->ven))) {
  120. NFC_LOG_ERR("%s: ven gpio invalid %d\n", __func__, nfc_gpio->ven);
  121. return -EINVAL;
  122. }
  123. /* some products like sn220 does not required fw dwl pin */
  124. nfc_gpio->dwl_req = of_get_named_gpio(np, DTS_FWDN_GPIO_STR, 0);
  125. if ((!gpio_is_valid(nfc_gpio->dwl_req)))
  126. NFC_LOG_ERR("%s: dwl_req gpio is not supported(%d)\n", __func__,
  127. nfc_gpio->dwl_req);
  128. #if IS_ENABLED(CONFIG_SAMSUNG_NFC)
  129. nfc_configs->late_pvdd_en = of_property_read_bool(np, "pn547,late_pvdd_en");
  130. nfc_configs->disable_clk_irq_during_wakeup =
  131. of_property_read_bool(np, "pn547,disable_clk_irq_during_wakeup");
  132. if (nfc_get_lpcharge() == LPM_FALSE)
  133. nfc_configs->late_pvdd_en = false;
  134. nfc_gpio->clk_req = of_get_named_gpio(np, "pn547,clk_req-gpio", 0);
  135. if ((!gpio_is_valid(nfc_gpio->clk_req)))
  136. NFC_LOG_ERR("nfc clk_req gpio invalid %d\n", nfc_gpio->clk_req);
  137. nfc_configs->clk_req_wake = of_property_read_bool(np, "pn547,clk_req_wake");
  138. if (of_property_read_bool(np, "pn547,clk_req_all_trigger")) {
  139. nfc_configs->clk_req_all_trigger = true;
  140. NFC_LOG_INFO("irq_all_trigger\n");
  141. }
  142. if (of_property_read_bool(np, "pn547,change_clkreq_for_acpm")) {
  143. nfc_configs->change_clkreq_for_acpm = true;
  144. NFC_LOG_INFO("change clkreq for acpm!!\n");
  145. }
  146. if (!of_property_read_string(np, "pn547,ap_vendor", &ap_str)) {
  147. if (!strcmp(ap_str, "slsi"))
  148. nfc_configs->ap_vendor = AP_VENDOR_SLSI;
  149. else if (!strcmp(ap_str, "qct") || !strcmp(ap_str, "qualcomm"))
  150. nfc_configs->ap_vendor = AP_VENDOR_QCT;
  151. else if (!strcmp(ap_str, "mtk"))
  152. nfc_configs->ap_vendor = AP_VENDOR_MTK;
  153. } else {
  154. NFC_LOG_INFO("AP vendor is not set\n");
  155. }
  156. #ifdef CONFIG_MAKE_NODE_USING_PLATFORM_DEVICE
  157. if (nfc_configs->late_pvdd_en) {
  158. if (!IS_ERR_OR_NULL(g_nfc_dev_for_chrdev.configs.nfc_pvdd)) {
  159. nfc_configs->nfc_pvdd = g_nfc_dev_for_chrdev.configs.nfc_pvdd;
  160. } else {
  161. nfc_configs->nfc_pvdd = regulator_get(dev, "nfc_pvdd");
  162. if (!IS_ERR(nfc_configs->nfc_pvdd))
  163. g_nfc_dev_for_chrdev.configs.nfc_pvdd = nfc_configs->nfc_pvdd;
  164. NFC_LOG_INFO("retry to get platform pvdd\n");
  165. }
  166. } else {
  167. nfc_configs->nfc_pvdd = regulator_get(dev, "nfc_pvdd");
  168. }
  169. #else
  170. nfc_configs->nfc_pvdd = regulator_get(dev, "nfc_pvdd");
  171. #endif
  172. if (IS_ERR(nfc_configs->nfc_pvdd)) {
  173. NFC_LOG_ERR("get nfc_pvdd error\n");
  174. if (--retry_count > 0)
  175. return -EPROBE_DEFER;
  176. else
  177. return -ENODEV;
  178. } else {
  179. NFC_LOG_INFO("LDO nfc_pvdd: %pK, vol:%d\n",
  180. nfc_configs->nfc_pvdd, regulator_get_voltage(nfc_configs->nfc_pvdd));
  181. }
  182. if (of_property_read_bool(np, "pn547,always_on_pvdd")) {
  183. /* set late_pvdd_en to true so that nfc_en reset can be called in nfc_power_control */
  184. nfc_configs->late_pvdd_en = true;
  185. nfc_set_i2c_pinctrl(dev, "i2c_pull_up");
  186. }
  187. if (of_find_property(np, "clocks", NULL)) {
  188. nfc_configs->nfc_clock = clk_get(dev, "oscclk_nfc");
  189. if (IS_ERR(nfc_configs->nfc_clock)) {
  190. NFC_LOG_ERR("probe() clk not found\n");
  191. nfc_configs->nfc_clock = NULL;
  192. } else {
  193. NFC_LOG_INFO("found oscclk_nfc\n");
  194. }
  195. }
  196. #endif
  197. NFC_LOG_INFO("%s: irq %d, ven %d, fw %d\n", __func__, nfc_gpio->irq, nfc_gpio->ven,
  198. nfc_gpio->dwl_req);
  199. return 0;
  200. }
  201. #if IS_ENABLED(CONFIG_SAMSUNG_NFC)
  202. static int nfc_ocp_notifier(struct notifier_block *nb, unsigned long event, void *data);
  203. int nfc_regulator_onoff(struct nfc_dev *nfc_dev, int onoff)
  204. {
  205. int rc = 0;
  206. struct platform_configs *nfc_configs = &nfc_dev->configs;
  207. struct regulator *regulator_nfc_pvdd;
  208. #ifdef CONFIG_MAKE_NODE_USING_PLATFORM_DEVICE
  209. /* if nfc probe is called before nfc platform device, nfc_configs->nfc_pvdd is NULL */
  210. if (!nfc_configs->nfc_pvdd)
  211. nfc_configs->nfc_pvdd = g_nfc_dev_for_chrdev.configs.nfc_pvdd;
  212. #endif
  213. /* if regulator is not ready, try to get that again */
  214. if (IS_ERR_OR_NULL(nfc_configs->nfc_pvdd) && nfc_dev->i2c_dev.client) {
  215. NFC_LOG_INFO("%s retry to get regulator\n", __func__);
  216. nfc_configs->nfc_pvdd = regulator_get(&nfc_dev->i2c_dev.client->dev, "nfc_pvdd");
  217. }
  218. regulator_nfc_pvdd = nfc_configs->nfc_pvdd;
  219. if (IS_ERR_OR_NULL(regulator_nfc_pvdd)) {
  220. NFC_LOG_ERR("error at regulator!\n");
  221. rc = -ENODEV;
  222. goto done;
  223. }
  224. if (nfc_configs->ap_vendor == AP_VENDOR_QCT && !nfc_configs->ldo_ocp_nb.notifier_call) {
  225. nfc_configs->ldo_ocp_nb.notifier_call = nfc_ocp_notifier;
  226. devm_regulator_register_notifier(nfc_configs->nfc_pvdd, &nfc_configs->ldo_ocp_nb);
  227. NFC_LOG_INFO("%s register nfc ocp notifier\n", __func__);
  228. }
  229. NFC_LOG_INFO("onoff = %d, g_is_nfc_pvdd_enabled = %d\n", onoff, g_is_nfc_pvdd_enabled);
  230. if (g_is_nfc_pvdd_enabled == onoff) {
  231. NFC_LOG_INFO("%s already pvdd %s\n", __func__, onoff ? "enabled" : "disabled");
  232. goto done;
  233. }
  234. if (onoff) {
  235. rc = regulator_set_load(regulator_nfc_pvdd, 300000);
  236. if (rc) {
  237. NFC_LOG_ERR("regulator_uwb_vdd set_load failed, rc=%d\n", rc);
  238. goto done;
  239. }
  240. rc = regulator_enable(regulator_nfc_pvdd);
  241. if (rc) {
  242. NFC_LOG_ERR("enable failed, rc=%d\n", rc);
  243. goto done;
  244. }
  245. } else {
  246. rc = regulator_disable(regulator_nfc_pvdd);
  247. if (rc) {
  248. NFC_LOG_ERR("disable failed, rc=%d\n", rc);
  249. goto done;
  250. }
  251. }
  252. g_is_nfc_pvdd_enabled = !!onoff;
  253. NFC_LOG_INFO("success\n");
  254. done:
  255. return rc;
  256. }
  257. bool nfc_check_pvdd_status(void)
  258. {
  259. return g_is_nfc_pvdd_enabled;
  260. }
  261. #endif
  262. void set_valid_gpio(int gpio, int value)
  263. {
  264. if (gpio_is_valid(gpio)) {
  265. NFC_LOG_DBG("%s: gpio %d value %d\n", __func__, gpio, value);
  266. gpio_set_value(gpio, value);
  267. /* hardware dependent delay */
  268. usleep_range(NFC_GPIO_SET_WAIT_TIME_US,
  269. NFC_GPIO_SET_WAIT_TIME_US + 100);
  270. }
  271. }
  272. int get_valid_gpio(int gpio)
  273. {
  274. int value = -EINVAL;
  275. if (gpio_is_valid(gpio)) {
  276. value = gpio_get_value(gpio);
  277. NFC_LOG_DBG("%s: gpio %d value %d\n", __func__, gpio, value);
  278. }
  279. return value;
  280. }
  281. void gpio_set_ven(struct nfc_dev *nfc_dev, int value)
  282. {
  283. struct platform_gpio *nfc_gpio = &nfc_dev->configs.gpio;
  284. if (gpio_get_value(nfc_gpio->ven) != value) {
  285. NFC_LOG_REC("%s: value %d\n", __func__, value);
  286. /* reset on change in level from high to low */
  287. if (value)
  288. ese_cold_reset_release(nfc_dev);
  289. gpio_set_value(nfc_gpio->ven, value);
  290. /* hardware dependent delay */
  291. usleep_range(NFC_GPIO_SET_WAIT_TIME_US,
  292. NFC_GPIO_SET_WAIT_TIME_US + 100);
  293. }
  294. }
  295. int configure_gpio(unsigned int gpio, int flag)
  296. {
  297. int ret;
  298. NFC_LOG_DBG("%s: nfc gpio [%d] flag [%01x]\n", __func__, gpio, flag);
  299. if (gpio_is_valid(gpio)) {
  300. ret = gpio_request(gpio, "nfc_gpio");
  301. if (ret) {
  302. NFC_LOG_ERR("%s: unable to request nfc gpio [%d]\n",
  303. __func__, gpio);
  304. return ret;
  305. }
  306. /* set direction and value for output pin */
  307. if (flag & GPIO_OUTPUT) {
  308. ret = gpio_direction_output(gpio, (GPIO_HIGH & flag));
  309. NFC_LOG_DBG("%s: nfc o/p gpio %d level %d\n", __func__,
  310. gpio, gpio_get_value(gpio));
  311. } else {
  312. ret = gpio_direction_input(gpio);
  313. NFC_LOG_DBG("%s: nfc i/p gpio %d\n", __func__, gpio);
  314. }
  315. if (ret) {
  316. NFC_LOG_ERR("%s: unable to set direction for nfc gpio [%d]\n",
  317. __func__, gpio);
  318. gpio_free(gpio);
  319. return ret;
  320. }
  321. /* Consider value as control for input IRQ pin */
  322. if (flag & GPIO_IRQ) {
  323. ret = gpio_to_irq(gpio);
  324. if (ret < 0) {
  325. NFC_LOG_ERR("%s: unable to set irq [%d]\n", __func__,
  326. gpio);
  327. gpio_free(gpio);
  328. return ret;
  329. }
  330. NFC_LOG_DBG("%s: gpio_to_irq successful [%d]\n", __func__,
  331. gpio);
  332. return ret;
  333. }
  334. } else {
  335. NFC_LOG_ERR("%s: invalid gpio\n", __func__);
  336. ret = -EINVAL;
  337. }
  338. return ret;
  339. }
  340. void gpio_free_all(struct nfc_dev *nfc_dev)
  341. {
  342. struct platform_gpio *nfc_gpio = &nfc_dev->configs.gpio;
  343. if (gpio_is_valid(nfc_gpio->dwl_req))
  344. gpio_free(nfc_gpio->dwl_req);
  345. if (gpio_is_valid(nfc_gpio->irq))
  346. gpio_free(nfc_gpio->irq);
  347. if (gpio_is_valid(nfc_gpio->ven))
  348. gpio_free(nfc_gpio->ven);
  349. }
  350. #if IS_ENABLED(CONFIG_SAMSUNG_NFC)
  351. void nfc_power_control(struct nfc_dev *nfc_dev)
  352. {
  353. int ret;
  354. ret = nfc_regulator_onoff(nfc_dev, 1);
  355. if (ret < 0)
  356. NFC_LOG_ERR("%s pn547 regulator_on fail err = %d\n", __func__, ret);
  357. nfc_set_i2c_pinctrl(&nfc_dev->i2c_dev.client->dev, "i2c_pull_up");
  358. #ifdef CONFIG_NFC_SN2XX_ESE_SUPPORT
  359. ese_set_spi_pinctrl_for_ese_off(NULL);
  360. #endif
  361. usleep_range(15000, 20000); /* spec : VDDIO high -> 15~20 ms -> VEN high*/
  362. gpio_set_ven(nfc_dev, 1);
  363. gpio_set_ven(nfc_dev, 0);
  364. gpio_set_ven(nfc_dev, 1);
  365. }
  366. static int nfc_ocp_notifier(struct notifier_block *nb, unsigned long event, void *data)
  367. {
  368. if (event == REGULATOR_EVENT_OVER_CURRENT)
  369. NFC_LOG_ERR("NFC power OCP\n");
  370. return NOTIFY_OK;
  371. }
  372. #if (KERNEL_VERSION(6, 3, 0) <= LINUX_VERSION_CODE)
  373. static ssize_t nfc_support_show(const struct class *class,
  374. const struct class_attribute *attr, char *buf)
  375. #else
  376. static ssize_t nfc_support_show(struct class *class,
  377. struct class_attribute *attr, char *buf)
  378. #endif
  379. {
  380. NFC_LOG_INFO("\n");
  381. return 0;
  382. }
  383. static CLASS_ATTR_RO(nfc_support);
  384. #if (KERNEL_VERSION(6, 3, 0) <= LINUX_VERSION_CODE)
  385. static ssize_t pvdd_store(const struct class *class,
  386. const struct class_attribute *attr, const char *buf, size_t size)
  387. #else
  388. static ssize_t pvdd_store(struct class *class,
  389. struct class_attribute *attr, const char *buf, size_t size)
  390. #endif
  391. {
  392. struct nfc_dev *nfc_dev = g_nfc_dev;
  393. struct platform_configs *nfc_configs;
  394. #ifdef CONFIG_MAKE_NODE_USING_PLATFORM_DEVICE
  395. if (!g_nfc_dev) {
  396. nfc_dev = &g_nfc_dev_for_chrdev;
  397. NFC_LOG_INFO("%s called before nfc probe\n", __func__);
  398. }
  399. #endif
  400. if (!nfc_dev) {
  401. NFC_LOG_ERR("%s nfc_dev is NULL!\n", __func__);
  402. return size;
  403. }
  404. nfc_configs = &nfc_dev->configs;
  405. NFC_LOG_INFO("%s val: %c, late_pvdd_en: %d\n", __func__, buf[0], nfc_configs->late_pvdd_en);
  406. if (buf[0] == '1' && nfc_configs->late_pvdd_en)
  407. nfc_power_control(nfc_dev);
  408. return size;
  409. }
  410. static CLASS_ATTR_WO(pvdd);
  411. #ifdef CONFIG_SAMSUNG_NFC_DEBUG
  412. #if (KERNEL_VERSION(6, 3, 0) <= LINUX_VERSION_CODE)
  413. static ssize_t check_show(const struct class *class,
  414. const struct class_attribute *attr, char *buf)
  415. #else
  416. static ssize_t check_show(struct class *class,
  417. struct class_attribute *attr, char *buf)
  418. #endif
  419. {
  420. struct nfc_dev *nfc_dev = g_nfc_dev;
  421. char *cmd = nfc_dev->write_kbuf;
  422. char *rsp = nfc_dev->read_kbuf;
  423. int timeout = NCI_CMD_RSP_TIMEOUT_MS;
  424. int size = 0;
  425. int ret;
  426. int cmd_length = 4;
  427. if (!nfc_check_pvdd_status()) {
  428. NFC_LOG_ERR("Turn on PVDD first\n");
  429. size = snprintf(buf, SZ_64, "Turn on PVDD first\n");
  430. goto end;
  431. }
  432. mutex_lock(&nfc_dev->write_mutex);
  433. *cmd++ = 0x20;
  434. *cmd++ = 0x00;
  435. *cmd++ = 0x01;
  436. *cmd++ = 0x00;
  437. ret = nfc_dev->nfc_write(nfc_dev, nfc_dev->write_kbuf, cmd_length, MAX_RETRY_COUNT);
  438. if (ret != cmd_length) {
  439. ret = -EIO;
  440. NFC_LOG_ERR("%s: nfc_write returned %d\n", __func__, ret);
  441. size = snprintf(buf, SZ_64, "nfc_write returned %d. count : %d\n",
  442. ret, cmd_length);
  443. mutex_unlock(&nfc_dev->write_mutex);
  444. goto end;
  445. }
  446. mutex_unlock(&nfc_dev->write_mutex);
  447. /* Read data */
  448. mutex_lock(&nfc_dev->read_mutex);
  449. cmd_length = 6;
  450. ret = nfc_dev->nfc_read(nfc_dev, rsp, cmd_length, timeout);
  451. if (ret < 0 || ret > cmd_length) {
  452. NFC_LOG_ERR("%s: nfc_read returned %d\n", __func__, ret);
  453. size = snprintf(buf, SZ_64, "nfc_read returned %d. count : %d\n",
  454. ret, cmd_length);
  455. mutex_unlock(&nfc_dev->read_mutex);
  456. goto end;
  457. }
  458. mutex_unlock(&nfc_dev->read_mutex);
  459. size = snprintf(buf, SZ_64, "test completed!! size: %d, data: %X %X %X %X %X %X\n",
  460. ret, rsp[0], rsp[1], rsp[2], rsp[3], rsp[4], rsp[5]);
  461. end:
  462. return size;
  463. }
  464. #ifdef FEATURE_SEC_NFC_TEST
  465. extern void nfc_check_is_core_reset_ntf(u8 *data, int len);
  466. #endif
  467. #if (KERNEL_VERSION(6, 3, 0) <= LINUX_VERSION_CODE)
  468. static ssize_t check_store(const struct class *class,
  469. const struct class_attribute *attr, const char *buf, size_t size)
  470. #else
  471. static ssize_t check_store(struct class *class,
  472. struct class_attribute *attr, const char *buf, size_t size)
  473. #endif
  474. {
  475. if (size > 0) {
  476. if (buf[0] == '1') {
  477. NFC_LOG_INFO("%s: test\n", __func__);
  478. nfc_print_status();
  479. }
  480. #ifdef FEATURE_SEC_NFC_TEST
  481. else if (buf[0] == '2') {
  482. u8 header[3] = {0x60, 0x00, 0x06};
  483. u8 data[10] = {0x0, };
  484. nfc_check_is_core_reset_ntf(header, 3);
  485. data[0] = 0xA0;
  486. nfc_check_is_core_reset_ntf(data, 6);
  487. }
  488. #endif
  489. }
  490. return size;
  491. }
  492. static CLASS_ATTR_RW(check);
  493. #endif
  494. #endif
  495. #if IS_ENABLED(CONFIG_SAMSUNG_NFC)
  496. void nfc_probe_done(struct nfc_dev *nfc_dev)
  497. {
  498. g_nfc_dev = nfc_dev;
  499. }
  500. #endif
  501. void nfc_misc_unregister(struct nfc_dev *nfc_dev, int count)
  502. {
  503. NFC_LOG_DBG("%s: entry\n", __func__);
  504. #ifdef CONFIG_MAKE_NODE_USING_PLATFORM_DEVICE
  505. if (nfc_dev == NULL)
  506. nfc_dev = &g_nfc_dev_for_chrdev;
  507. #endif
  508. #if IS_ENABLED(CONFIG_SAMSUNG_NFC)
  509. class_remove_file(nfc_dev->nfc_class, &class_attr_nfc_support);
  510. class_remove_file(nfc_dev->nfc_class, &class_attr_pvdd);
  511. #ifdef CONFIG_SAMSUNG_NFC_DEBUG
  512. class_remove_file(nfc_dev->nfc_class, &class_attr_check);
  513. #endif
  514. #endif
  515. device_destroy(nfc_dev->nfc_class, nfc_dev->devno);
  516. cdev_del(&nfc_dev->c_dev);
  517. class_destroy(nfc_dev->nfc_class);
  518. unregister_chrdev_region(nfc_dev->devno, count);
  519. }
  520. int nfc_misc_register(struct nfc_dev *nfc_dev,
  521. const struct file_operations *nfc_fops, int count,
  522. char *devname, char *classname)
  523. {
  524. int ret = 0;
  525. #ifdef CONFIG_MAKE_NODE_USING_PLATFORM_DEVICE
  526. if (nfc_dev == NULL)
  527. nfc_dev = &g_nfc_dev_for_chrdev;
  528. #endif
  529. ret = alloc_chrdev_region(&nfc_dev->devno, 0, count, devname);
  530. if (ret < 0) {
  531. NFC_LOG_ERR("%s: failed to alloc chrdev region ret %d\n", __func__,
  532. ret);
  533. return ret;
  534. }
  535. #if (KERNEL_VERSION(6, 3, 0) <= LINUX_VERSION_CODE)
  536. nfc_dev->nfc_class = class_create(classname);
  537. #else
  538. nfc_dev->nfc_class = class_create(THIS_MODULE, classname);
  539. #endif
  540. if (IS_ERR(nfc_dev->nfc_class)) {
  541. ret = PTR_ERR(nfc_dev->nfc_class);
  542. NFC_LOG_ERR("%s: failed to register device class ret %d\n", __func__,
  543. ret);
  544. unregister_chrdev_region(nfc_dev->devno, count);
  545. return ret;
  546. }
  547. #if IS_ENABLED(CONFIG_SAMSUNG_NFC)
  548. ret = class_create_file(nfc_dev->nfc_class, &class_attr_nfc_support);
  549. if (ret)
  550. NFC_LOG_ERR("failed to create nfc_support file\n");
  551. ret = class_create_file(nfc_dev->nfc_class, &class_attr_pvdd);
  552. if (ret)
  553. NFC_LOG_ERR("failed to create nfc_support file\n");
  554. #ifdef CONFIG_SAMSUNG_NFC_DEBUG
  555. ret = class_create_file(nfc_dev->nfc_class, &class_attr_check);
  556. if (ret)
  557. NFC_LOG_ERR("failed to create test file\n");
  558. #endif
  559. #endif
  560. cdev_init(&nfc_dev->c_dev, nfc_fops);
  561. ret = cdev_add(&nfc_dev->c_dev, nfc_dev->devno, count);
  562. if (ret < 0) {
  563. NFC_LOG_ERR("%s: failed to add cdev ret %d\n", __func__, ret);
  564. class_destroy(nfc_dev->nfc_class);
  565. unregister_chrdev_region(nfc_dev->devno, count);
  566. return ret;
  567. }
  568. nfc_dev->nfc_device = device_create(nfc_dev->nfc_class, NULL,
  569. nfc_dev->devno, nfc_dev, devname);
  570. if (IS_ERR(nfc_dev->nfc_device)) {
  571. ret = PTR_ERR(nfc_dev->nfc_device);
  572. NFC_LOG_ERR("%s: failed to create the device ret %d\n", __func__,
  573. ret);
  574. cdev_del(&nfc_dev->c_dev);
  575. class_destroy(nfc_dev->nfc_class);
  576. unregister_chrdev_region(nfc_dev->devno, count);
  577. return ret;
  578. }
  579. return 0;
  580. }
  581. #if IS_ENABLED(CONFIG_SAMSUNG_NFC)
  582. void nfc_print_status(void)
  583. {
  584. struct nfc_dev *nfc_dev = g_nfc_dev;
  585. struct platform_configs *nfc_configs;
  586. struct platform_gpio *nfc_gpio;
  587. int en, firm, irq, pvdd = 0;
  588. int clk_req_irq = -1;
  589. if (nfc_dev == NULL)
  590. return;
  591. nfc_configs = &nfc_dev->configs;
  592. nfc_gpio = &nfc_dev->configs.gpio;
  593. en = get_valid_gpio(nfc_gpio->ven);
  594. firm = get_valid_gpio(nfc_gpio->dwl_req);
  595. irq = get_valid_gpio(nfc_gpio->irq);
  596. if (!IS_ERR_OR_NULL(nfc_configs->nfc_pvdd))
  597. pvdd = regulator_is_enabled(nfc_configs->nfc_pvdd);
  598. else
  599. NFC_LOG_ERR("nfc_pvdd is null\n");
  600. clk_req_irq = get_valid_gpio(nfc_gpio->clk_req);
  601. NFC_LOG_INFO("en: %d, firm: %d, pvdd: %d, irq: %d, clk_req: %d\n",
  602. en, firm, pvdd, irq, clk_req_irq);
  603. #ifdef CONFIG_NFC_SN2XX_ESE_SUPPORT
  604. p61_print_status(__func__);
  605. #endif
  606. #ifdef CONFIG_SEC_NFC_LOGGER_ADD_ACPM_LOG
  607. nfc_logger_acpm_log_print();
  608. #endif
  609. }
  610. #endif
  611. /**
  612. ** nfc_gpio_info() - gets the status of nfc gpio pins and encodes into a byte.
  613. ** @nfc_dev: nfc device data structure
  614. ** @arg: userspace buffer
  615. **
  616. ** Encoding can be done in following manner
  617. ** 1) map the gpio value into INVALID(-2), SET(1), RESET(0).
  618. ** 2) mask the first 2 bits of gpio.
  619. ** 3) left shift the 2 bits as multiple of 2.
  620. ** 4) multiply factor can be defined as position of gpio pin in struct platform_gpio
  621. **
  622. ** Return: -EFAULT, if unable to copy the data from kernel space to userspace, 0
  623. ** if Success(or no issue)
  624. **/
  625. static int nfc_gpio_info(struct nfc_dev *nfc_dev, unsigned long arg)
  626. {
  627. unsigned int gpios_status = 0;
  628. int value = 0;
  629. int gpio_no = 0;
  630. int i;
  631. struct platform_gpio *nfc_gpio = &nfc_dev->configs.gpio;
  632. #if !IS_ENABLED(CONFIG_SAMSUNG_NFC)
  633. for (i = 0; i < sizeof(struct platform_gpio) / sizeof(unsigned int); i++)
  634. #else
  635. for (i = 0; i < PLATFORM_DEFAULT_GPIO_CNT; i++)
  636. #endif
  637. {
  638. gpio_no = *((unsigned int *)nfc_gpio + i);
  639. value = get_valid_gpio(gpio_no);
  640. if (value < 0)
  641. value = -2;
  642. gpios_status |= (value & GPIO_STATUS_MASK_BITS)<<(GPIO_POS_SHIFT_VAL*i);
  643. }
  644. #if IS_ENABLED(CONFIG_SAMSUNG_NFC)
  645. nfc_print_status();
  646. #endif
  647. if (copy_to_user((uint32_t *) arg, &gpios_status, sizeof(value))) {
  648. pr_err("%s : Unable to copy data from kernel space to user space\n", __func__);
  649. return -EFAULT;
  650. }
  651. return 0;
  652. }
  653. /**
  654. * nfc_ioctl_power_states() - power control
  655. * @nfc_dev: nfc device data structure
  656. * @arg: mode that we want to move to
  657. *
  658. * Device power control. Depending on the arg value, device moves to
  659. * different states, refer common.h for args
  660. *
  661. * Return: -ENOIOCTLCMD if arg is not supported, 0 if Success(or no issue)
  662. * and error ret code otherwise
  663. */
  664. static int nfc_ioctl_power_states(struct nfc_dev *nfc_dev, unsigned long arg)
  665. {
  666. int ret = 0;
  667. struct platform_gpio *nfc_gpio = &nfc_dev->configs.gpio;
  668. if (arg == NFC_POWER_OFF) {
  669. /*
  670. * We are attempting a hardware reset so let us disable
  671. * interrupts to avoid spurious notifications to upper
  672. * layers.
  673. */
  674. nfc_dev->nfc_disable_intr(nfc_dev);
  675. set_valid_gpio(nfc_gpio->dwl_req, 0);
  676. gpio_set_ven(nfc_dev, 0);
  677. nfc_dev->nfc_ven_enabled = false;
  678. } else if (arg == NFC_POWER_ON) {
  679. nfc_dev->nfc_enable_intr(nfc_dev);
  680. set_valid_gpio(nfc_gpio->dwl_req, 0);
  681. gpio_set_ven(nfc_dev, 1);
  682. nfc_dev->nfc_ven_enabled = true;
  683. } else if (arg == NFC_FW_DWL_VEN_TOGGLE) {
  684. /*
  685. * We are switching to download Mode, toggle the enable pin
  686. * in order to set the NFCC in the new mode
  687. */
  688. nfc_dev->nfc_disable_intr(nfc_dev);
  689. set_valid_gpio(nfc_gpio->dwl_req, 1);
  690. nfc_dev->nfc_state = NFC_STATE_FW_DWL;
  691. gpio_set_ven(nfc_dev, 0);
  692. gpio_set_ven(nfc_dev, 1);
  693. nfc_dev->nfc_enable_intr(nfc_dev);
  694. } else if (arg == NFC_FW_DWL_HIGH) {
  695. /*
  696. * Setting firmware download gpio to HIGH
  697. * before FW download start
  698. */
  699. set_valid_gpio(nfc_gpio->dwl_req, 1);
  700. nfc_dev->nfc_state = NFC_STATE_FW_DWL;
  701. } else if (arg == NFC_VEN_FORCED_HARD_RESET) {
  702. nfc_dev->nfc_disable_intr(nfc_dev);
  703. gpio_set_ven(nfc_dev, 0);
  704. gpio_set_ven(nfc_dev, 1);
  705. nfc_dev->nfc_enable_intr(nfc_dev);
  706. } else if (arg == NFC_FW_DWL_LOW) {
  707. /*
  708. * Setting firmware download gpio to LOW
  709. * FW download finished
  710. */
  711. set_valid_gpio(nfc_gpio->dwl_req, 0);
  712. nfc_dev->nfc_state = NFC_STATE_NCI;
  713. } else {
  714. NFC_LOG_ERR("%s: bad arg %lu\n", __func__, arg);
  715. ret = -ENOIOCTLCMD;
  716. }
  717. return ret;
  718. }
  719. #ifdef CONFIG_COMPAT
  720. /**
  721. * nfc_dev_compat_ioctl - used to set or get data from upper layer.
  722. * @pfile file node for opened device.
  723. * @cmd ioctl type from upper layer.
  724. * @arg ioctl arg from upper layer.
  725. *
  726. * NFC and ESE Device power control, based on the argument value
  727. *
  728. * Return: -ENOIOCTLCMD if arg is not supported
  729. * 0 if Success(or no issue)
  730. * 0 or 1 in case of arg is ESE_GET_PWR/ESE_POWER_STATE
  731. * and error ret code otherwise
  732. */
  733. long nfc_dev_compat_ioctl(struct file *pfile, unsigned int cmd,
  734. unsigned long arg)
  735. {
  736. int ret = 0;
  737. arg = (compat_u64)arg;
  738. NFC_LOG_DBG("%s: cmd = %x arg = %zx\n", __func__, cmd, arg);
  739. ret = nfc_dev_ioctl(pfile, cmd, arg);
  740. return ret;
  741. }
  742. #endif
  743. /**
  744. * nfc_dev_ioctl - used to set or get data from upper layer.
  745. * @pfile file node for opened device.
  746. * @cmd ioctl type from upper layer.
  747. * @arg ioctl arg from upper layer.
  748. *
  749. * NFC and ESE Device power control, based on the argument value
  750. *
  751. * Return: -ENOIOCTLCMD if arg is not supported
  752. * 0 if Success(or no issue)
  753. * 0 or 1 in case of arg is ESE_GET_PWR/ESE_POWER_STATE
  754. * and error ret code otherwise
  755. */
  756. long nfc_dev_ioctl(struct file *pfile, unsigned int cmd, unsigned long arg)
  757. {
  758. int ret = 0;
  759. struct nfc_dev *nfc_dev = pfile->private_data;
  760. if (!nfc_dev)
  761. return -ENODEV;
  762. NFC_LOG_INFO("%s: cmd = %x arg = %zx\n", __func__, cmd, arg);
  763. switch (cmd) {
  764. case NFC_SET_PWR:
  765. NFC_LOG_INFO("%s: NFC_SET_PWR\n", __func__);
  766. ret = nfc_ioctl_power_states(nfc_dev, arg);
  767. break;
  768. case ESE_SET_PWR:
  769. NFC_LOG_INFO("%s: ESE_SET_PWR\n", __func__);
  770. ret = nfc_ese_pwr(nfc_dev, arg);
  771. break;
  772. case ESE_GET_PWR:
  773. NFC_LOG_INFO("%s: ESE_GET_PWR\n", __func__);
  774. ret = nfc_ese_pwr(nfc_dev, ESE_POWER_STATE);
  775. break;
  776. case NFC_GET_GPIO_STATUS:
  777. NFC_LOG_INFO("%s: NFC_GET_GPIO_STATUS\n", __func__);
  778. ret = nfc_gpio_info(nfc_dev, arg);
  779. break;
  780. default:
  781. NFC_LOG_ERR("%s: bad cmd %lu\n", __func__, arg);
  782. ret = -ENOIOCTLCMD;
  783. };
  784. return ret;
  785. }
  786. int nfc_dev_open(struct inode *inode, struct file *filp)
  787. {
  788. struct nfc_dev *nfc_dev = NULL;
  789. #if IS_ENABLED(CONFIG_SAMSUNG_NFC)
  790. struct platform_configs *nfc_configs;
  791. #endif
  792. #ifdef CONFIG_MAKE_NODE_USING_PLATFORM_DEVICE
  793. nfc_dev = g_nfc_dev;
  794. #else
  795. nfc_dev = container_of(inode->i_cdev, struct nfc_dev, c_dev);
  796. #endif
  797. if (!nfc_dev)
  798. return -ENODEV;
  799. NFC_LOG_INFO("%s: %d, %d\n", __func__, imajor(inode), iminor(inode));
  800. mutex_lock(&nfc_dev->dev_ref_mutex);
  801. filp->private_data = nfc_dev;
  802. if (nfc_dev->dev_ref_count == 0) {
  803. set_valid_gpio(nfc_dev->configs.gpio.dwl_req, 0);
  804. nfc_dev->nfc_enable_intr(nfc_dev);
  805. #if IS_ENABLED(CONFIG_SAMSUNG_NFC)
  806. nfc_configs = &nfc_dev->configs;
  807. if (!nfc_configs->disable_clk_irq_during_wakeup)
  808. nfc_dev->nfc_enable_clk_intr(nfc_dev);
  809. #endif
  810. }
  811. nfc_dev->dev_ref_count = nfc_dev->dev_ref_count + 1;
  812. mutex_unlock(&nfc_dev->dev_ref_mutex);
  813. return 0;
  814. }
  815. int nfc_dev_flush(struct file *pfile, fl_owner_t id)
  816. {
  817. struct nfc_dev *nfc_dev = pfile->private_data;
  818. if (!nfc_dev)
  819. return -ENODEV;
  820. /*
  821. * release blocked user thread waiting for pending read during close
  822. */
  823. if (!mutex_trylock(&nfc_dev->read_mutex)) {
  824. nfc_dev->release_read = true;
  825. nfc_dev->nfc_disable_intr(nfc_dev);
  826. wake_up(&nfc_dev->read_wq);
  827. NFC_LOG_DBG("%s: waiting for release of blocked read\n", __func__);
  828. mutex_lock(&nfc_dev->read_mutex);
  829. nfc_dev->release_read = false;
  830. } else {
  831. NFC_LOG_DBG("%s: read thread already released\n", __func__);
  832. }
  833. mutex_unlock(&nfc_dev->read_mutex);
  834. return 0;
  835. }
  836. int nfc_dev_close(struct inode *inode, struct file *filp)
  837. {
  838. struct nfc_dev *nfc_dev = NULL;
  839. #ifdef CONFIG_MAKE_NODE_USING_PLATFORM_DEVICE
  840. nfc_dev = g_nfc_dev;
  841. #else
  842. nfc_dev = container_of(inode->i_cdev, struct nfc_dev, c_dev);
  843. #endif
  844. if (!nfc_dev)
  845. return -ENODEV;
  846. NFC_LOG_INFO("%s: %d, %d\n", __func__, imajor(inode), iminor(inode));
  847. mutex_lock(&nfc_dev->dev_ref_mutex);
  848. if (nfc_dev->dev_ref_count == 1) {
  849. nfc_dev->nfc_disable_intr(nfc_dev);
  850. set_valid_gpio(nfc_dev->configs.gpio.dwl_req, 0);
  851. #if IS_ENABLED(CONFIG_SAMSUNG_NFC)
  852. nfc_dev->nfc_disable_clk_intr(nfc_dev);
  853. #endif
  854. }
  855. if (nfc_dev->dev_ref_count > 0)
  856. nfc_dev->dev_ref_count = nfc_dev->dev_ref_count - 1;
  857. else {
  858. /*
  859. * Use "ESE_RST_PROT_DIS" as argument
  860. * if eSE calls flow is via NFC driver
  861. * i.e. direct calls from SPI HAL to NFC driver
  862. */
  863. nfc_ese_pwr(nfc_dev, ESE_RST_PROT_DIS_NFC);
  864. }
  865. filp->private_data = NULL;
  866. #if IS_ENABLED(CONFIG_SAMSUNG_NFC)
  867. nfc_print_status();
  868. #endif
  869. mutex_unlock(&nfc_dev->dev_ref_mutex);
  870. return 0;
  871. }
  872. int validate_nfc_state_nci(struct nfc_dev *nfc_dev)
  873. {
  874. struct platform_gpio *nfc_gpio = &nfc_dev->configs.gpio;
  875. if (!gpio_get_value(nfc_gpio->ven)) {
  876. NFC_LOG_ERR("%s: ven low - nfcc powered off\n", __func__);
  877. return -ENODEV;
  878. }
  879. if (get_valid_gpio(nfc_gpio->dwl_req) == 1) {
  880. NFC_LOG_ERR("%s: fw download in-progress\n", __func__);
  881. return -EBUSY;
  882. }
  883. if (nfc_dev->nfc_state != NFC_STATE_NCI) {
  884. NFC_LOG_ERR("%s: fw download state\n", __func__);
  885. return -EBUSY;
  886. }
  887. return 0;
  888. }