common.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. /******************************************************************************
  2. * Copyright (C) 2015, The Linux Foundation. All rights reserved.
  3. * Copyright (C) 2019-2022 NXP
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. *
  19. ******************************************************************************/
  20. #ifndef _COMMON_H_
  21. #define _COMMON_H_
  22. #include <linux/cdev.h>
  23. #include <linux/fs.h>
  24. #if IS_ENABLED(CONFIG_SAMSUNG_NFC)
  25. #include <linux/clk.h>
  26. #include <linux/reboot.h>
  27. #include "nfc_wakelock.h"
  28. #ifdef CONFIG_SEC_NFC_LOGGER
  29. #ifdef CONFIG_NFC_NXP_COMBINED
  30. #include "../nfc_logger/nfc_logger.h"
  31. #else
  32. #include "nfc_logger/nfc_logger.h"
  33. #endif
  34. #endif
  35. #endif
  36. #include "i2c_drv.h"
  37. /* Max device count for this driver */
  38. #define DEV_COUNT 1
  39. /* i2c device class */
  40. #if IS_ENABLED(CONFIG_SAMSUNG_NFC)
  41. #define CLASS_NAME "nfc_sec"
  42. #else
  43. #define CLASS_NAME "nfc"
  44. #endif
  45. /* NFC character device name, this will be in /dev/ */
  46. #define NFC_CHAR_DEV_NAME "pn547"
  47. /* NCI packet details */
  48. #define NCI_CMD (0x20)
  49. #define NCI_RSP (0x40)
  50. #define NCI_HDR_LEN (3)
  51. #define NCI_HDR_IDX (0)
  52. #define NCI_HDR_OID_IDX (1)
  53. #define NCI_PAYLOAD_IDX (3)
  54. #define NCI_PAYLOAD_LEN_IDX (2)
  55. /* FW DNLD packet details */
  56. #define DL_HDR_LEN (2)
  57. #define DL_CRC_LEN (2)
  58. #define MAX_NCI_PAYLOAD_LEN (255)
  59. #define MAX_NCI_BUFFER_SIZE (NCI_HDR_LEN + MAX_NCI_PAYLOAD_LEN)
  60. /*
  61. * Compile time option to select maximum writer buffer of either 4K or 550 bytes.
  62. * Default value is set as 4K. This value shall be chosen based on Hal flag "HDLL_4K_WRITE_SUPPORTED".
  63. * undef or comment HDLL_4K_WRITE_SUPPORTED to fallback to 550 bytes write frame buffer.
  64. */
  65. #define HDLL_4K_WRITE_SUPPORTED
  66. #ifdef HDLL_4K_WRITE_SUPPORTED
  67. #define MAX_DL_PAYLOAD_LEN (4096)
  68. #else
  69. #define MAX_DL_PAYLOAD_LEN (550)
  70. #endif
  71. #define MAX_DL_BUFFER_SIZE (DL_HDR_LEN + DL_CRC_LEN + \
  72. MAX_DL_PAYLOAD_LEN)
  73. /* Retry count for normal write */
  74. #define NO_RETRY (1)
  75. /* Maximum retry count for standby writes */
  76. #define MAX_RETRY_COUNT (3)
  77. #define MAX_WRITE_IRQ_COUNT (5)
  78. #define MAX_IRQ_WAIT_TIME (90)
  79. #define WAKEUP_SRC_TIMEOUT (2000)
  80. /* command response timeout */
  81. #define NCI_CMD_RSP_TIMEOUT_MS (2000)
  82. /* Time to wait for NFCC to be ready again after any change in the GPIO */
  83. #define NFC_GPIO_SET_WAIT_TIME_US (15000)
  84. /* Time to wait before retrying writes */
  85. #define WRITE_RETRY_WAIT_TIME_US (3000)
  86. /* Time to wait before retrying read for some specific usecases */
  87. #define READ_RETRY_WAIT_TIME_US (3500)
  88. #define NFC_MAGIC (0xE9)
  89. /* Ioctls */
  90. /* The type should be aligned with MW HAL definitions */
  91. #define NFC_SET_PWR _IOW(NFC_MAGIC, 0x01, uint64_t)
  92. #define ESE_SET_PWR _IOW(NFC_MAGIC, 0x02, uint64_t)
  93. #define ESE_GET_PWR _IOR(NFC_MAGIC, 0x03, uint64_t)
  94. #define NFC_GET_GPIO_STATUS _IOR(NFC_MAGIC, 0x05, uint64_t)
  95. #if IS_ENABLED(CONFIG_SAMSUNG_NFC)
  96. #define CONFIG_SAMSUNG_NFC_DEBUG
  97. #define FEATURE_CORE_RESET_NTF_CHECK
  98. #define DTS_IRQ_GPIO_STR "pn547,irq-gpio"
  99. #define DTS_VEN_GPIO_STR "pn547,ven-gpio"
  100. #define DTS_FWDN_GPIO_STR "pn547,firm-gpio"
  101. enum ap_vendors {
  102. AP_VENDOR_NONE,
  103. AP_VENDOR_SLSI,
  104. AP_VENDOR_QCT,
  105. AP_VENDOR_MTK,
  106. AP_VENDOR_ERR
  107. };
  108. enum lpm_status {
  109. LPM_NO_SUPPORT = -1,
  110. LPM_FALSE,
  111. LPM_TRUE
  112. };
  113. #else
  114. #define DTS_IRQ_GPIO_STR "nxp,sn-irq"
  115. #define DTS_VEN_GPIO_STR "nxp,sn-ven-rstn"
  116. #define DTS_FWDN_GPIO_STR "nxp,sn-dwl-req"
  117. #endif
  118. /* Each GPIO occupies consecutive two bits */
  119. #define GPIO_POS_SHIFT_VAL 2
  120. /* Two bits to indicate GPIO status (Invalid(-2), Set(1) or Reset(0)) */
  121. #define GPIO_STATUS_MASK_BITS 3
  122. #ifndef CONFIG_SEC_NFC_LOGGER
  123. #define NFC_LOG_ERR(fmt, ...) pr_err("sec_nfc: "fmt, ##__VA_ARGS__)
  124. #define NFC_LOG_INFO(fmt, ...) pr_info("sec_nfc: "fmt, ##__VA_ARGS__)
  125. #define NFC_LOG_INFO_WITH_DATE(fmt, ...) pr_info("sec_nfc: "fmt, ##__VA_ARGS__)
  126. #define NFC_LOG_DBG(fmt, ...) pr_debug("sec_nfc: "fmt, ##__VA_ARGS__)
  127. #define NFC_LOG_REC(fmt, ...) do { } while (0)
  128. #define nfc_print_hex_dump(a, b, c) do { } while (0)
  129. #define nfc_logger_init() do { } while (0)
  130. #define nfc_logger_deinit() do { } while (0)
  131. #define nfc_logger_set_max_count(a) do { } while (0)
  132. #define nfc_logger_register_nfc_stauts_func(a) do { } while (0)
  133. #endif /* CONFIG_SEC_NFC_LOGGER */
  134. enum nfcc_ioctl_request {
  135. /* NFC disable request with VEN LOW */
  136. NFC_POWER_OFF = 0,
  137. /* NFC enable request with VEN Toggle */
  138. NFC_POWER_ON,
  139. /* firmware download request with VEN Toggle */
  140. NFC_FW_DWL_VEN_TOGGLE,
  141. /* ISO reset request */
  142. NFC_ISO_RESET,
  143. /* request for firmware download gpio HIGH */
  144. NFC_FW_DWL_HIGH,
  145. /* VEN hard reset request */
  146. NFC_VEN_FORCED_HARD_RESET,
  147. /* request for firmware download gpio LOW */
  148. NFC_FW_DWL_LOW,
  149. };
  150. /* nfc platform interface type */
  151. enum interface_flags {
  152. /* I2C physical IF for NFCC */
  153. PLATFORM_IF_I2C = 0,
  154. };
  155. /* nfc state flags */
  156. enum nfc_state_flags {
  157. /* nfc in unknown state */
  158. NFC_STATE_UNKNOWN = 0,
  159. /* nfc in download mode */
  160. NFC_STATE_FW_DWL = 0x1,
  161. /* nfc booted in NCI mode */
  162. NFC_STATE_NCI = 0x2,
  163. /* nfc booted in Fw teared mode */
  164. NFC_STATE_FW_TEARED = 0x4,
  165. };
  166. /*
  167. * Power state for IBI handing, mainly needed to defer the IBI handling
  168. * for the IBI received in suspend state to do it later in resume call
  169. */
  170. enum pm_state_flags {
  171. PM_STATE_NORMAL = 0,
  172. PM_STATE_SUSPEND,
  173. PM_STATE_IBI_BEFORE_RESUME,
  174. };
  175. /* Enum for GPIO values */
  176. enum gpio_values {
  177. GPIO_INPUT = 0x0,
  178. GPIO_OUTPUT = 0x1,
  179. GPIO_HIGH = 0x2,
  180. GPIO_OUTPUT_HIGH = 0x3,
  181. GPIO_IRQ = 0x4,
  182. };
  183. #if IS_ENABLED(CONFIG_SAMSUNG_NFC)
  184. #define PLATFORM_DEFAULT_GPIO_CNT 3
  185. #endif
  186. /* NFC GPIO variables */
  187. struct platform_gpio {
  188. int irq;
  189. int ven;
  190. int dwl_req;
  191. #if IS_ENABLED(CONFIG_SAMSUNG_NFC)
  192. int clk_req;
  193. int clk_req_irq;
  194. bool clk_req_irq_enabled;
  195. #endif
  196. };
  197. /* NFC Struct to get all the required configs from DTS */
  198. struct platform_configs {
  199. struct platform_gpio gpio;
  200. #if IS_ENABLED(CONFIG_SAMSUNG_NFC)
  201. bool clk_req_wake;
  202. bool clk_req_all_trigger;
  203. bool change_clkreq_for_acpm;
  204. int ap_vendor;
  205. struct regulator *nfc_pvdd;
  206. struct clk *nfc_clock;
  207. bool late_pvdd_en;
  208. bool disable_clk_irq_during_wakeup;
  209. struct notifier_block ldo_ocp_nb;
  210. #endif
  211. };
  212. /* cold reset Features specific Parameters */
  213. struct cold_reset {
  214. bool rsp_pending; /* cmd rsp pending status */
  215. bool in_progress; /* for cold reset when gurad timer in progress */
  216. bool reset_protection; /* reset protection enabled/disabled */
  217. uint8_t status; /* status from response buffer */
  218. uint8_t rst_prot_src; /* reset protection source (SPI, NFC) */
  219. struct timer_list timer;
  220. wait_queue_head_t read_wq;
  221. };
  222. /* Device specific structure */
  223. struct nfc_dev {
  224. wait_queue_head_t read_wq;
  225. struct mutex read_mutex;
  226. struct mutex write_mutex;
  227. uint8_t *read_kbuf;
  228. uint8_t *write_kbuf;
  229. struct mutex dev_ref_mutex;
  230. unsigned int dev_ref_count;
  231. struct class *nfc_class;
  232. struct device *nfc_device;
  233. struct cdev c_dev;
  234. dev_t devno;
  235. /* Interface flag */
  236. uint8_t interface;
  237. /* nfc state flags */
  238. uint8_t nfc_state;
  239. /* NFC VEN pin state */
  240. bool nfc_ven_enabled;
  241. bool release_read;
  242. union {
  243. struct i2c_dev i2c_dev;
  244. };
  245. struct platform_configs configs;
  246. struct cold_reset cold_reset;
  247. #if IS_ENABLED(CONFIG_SAMSUNG_NFC)
  248. struct nfc_wake_lock nfc_wake_lock;
  249. struct nfc_wake_lock nfc_clk_wake_lock;
  250. bool clk_req_wakelock;
  251. bool screen_cfg;
  252. bool screen_on_cmd;
  253. bool screen_off_cmd;
  254. int screen_off_rsp_count;
  255. struct notifier_block reboot_nb;
  256. #endif
  257. /* function pointers for the common i2c functionality */
  258. int (*nfc_read)(struct nfc_dev *dev, char *buf, size_t count,
  259. int timeout);
  260. int (*nfc_write)(struct nfc_dev *dev, const char *buf,
  261. const size_t count, int max_retry_cnt);
  262. int (*nfc_enable_intr)(struct nfc_dev *dev);
  263. int (*nfc_disable_intr)(struct nfc_dev *dev);
  264. #if IS_ENABLED(CONFIG_SAMSUNG_NFC)
  265. void (*nfc_enable_clk_intr)(struct nfc_dev *dev);
  266. void (*nfc_disable_clk_intr)(struct nfc_dev *dev);
  267. #endif
  268. };
  269. int nfc_dev_open(struct inode *inode, struct file *filp);
  270. int nfc_dev_flush(struct file *pfile, fl_owner_t id);
  271. int nfc_dev_close(struct inode *inode, struct file *filp);
  272. long nfc_dev_compat_ioctl(struct file *pfile, unsigned int cmd,
  273. unsigned long arg);
  274. long nfc_dev_ioctl(struct file *pfile, unsigned int cmd, unsigned long arg);
  275. int nfc_parse_dt(struct device *dev, struct platform_configs *nfc_configs,
  276. uint8_t interface);
  277. int nfc_misc_register(struct nfc_dev *nfc_dev,
  278. const struct file_operations *nfc_fops, int count,
  279. char *devname, char *classname);
  280. void nfc_misc_unregister(struct nfc_dev *nfc_dev, int count);
  281. int configure_gpio(unsigned int gpio, int flag);
  282. void gpio_set_ven(struct nfc_dev *nfc_dev, int value);
  283. void gpio_free_all(struct nfc_dev *nfc_dev);
  284. int validate_nfc_state_nci(struct nfc_dev *nfc_dev);
  285. #if IS_ENABLED(CONFIG_SAMSUNG_NFC)
  286. int nfc_regulator_onoff(struct nfc_dev *nfc_dev, int onoff);
  287. void nfc_power_control(struct nfc_dev *nfc_dev);
  288. void nfc_print_status(void);
  289. void nfc_probe_done(struct nfc_dev *nfc_dev);
  290. bool nfc_check_pvdd_status(void);
  291. enum lpm_status nfc_get_lpcharge(void);
  292. #ifdef CONFIG_MAKE_NODE_USING_PLATFORM_DEVICE
  293. void nfc_parse_dt_for_platform_device(struct device *dev);
  294. #endif
  295. #endif
  296. #endif /* _COMMON_H_ */