common.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. #ifndef _COMMON_H_
  21. #define _COMMON_H_
  22. #include <linux/types.h>
  23. #include <linux/version.h>
  24. #include <linux/semaphore.h>
  25. #include <linux/completion.h>
  26. #include <linux/ioctl.h>
  27. #include <linux/cdev.h>
  28. #include <linux/spinlock.h>
  29. #include <linux/gpio.h>
  30. #include "i2c_drv.h"
  31. #define DEV_COUNT 1 /* Max device count for this driver */
  32. #define CLASS_NAME "nfc" /* i2c device class */
  33. // NFC character device name, this will be in /dev/
  34. #define NFC_CHAR_DEV_NAME "pn553"
  35. // NCI packet details
  36. #define NCI_MSG_CMD 0x20
  37. #define NCI_MSG_RSP 0x40
  38. #define NCI_MSG_NTF 0x60
  39. #define NCI_HDR_LEN 3
  40. #define NCI_PAYLOAD_IDX 3
  41. #define NCI_PAYLOAD_LEN_IDX 2
  42. /*Time to wait for first NCI rest response*/
  43. #define NCI_RESET_RESP_READ_DELAY (10000) // 10ms
  44. #define NCI_RESET_RESP_TIMEOUT (500) // 500ms
  45. // FW DNLD packet details
  46. #define FW_MSG_CMD_RSP 0x00
  47. #define FW_HDR_LEN 2
  48. #define FW_PAYLOAD_LEN_IDX 1
  49. #define FW_CRC_LEN 2
  50. #define FW_MIN_PAYLOAD_LEN 4
  51. #define MIN_NFC_DL_FRAME_SIZE 3
  52. #define NCI_RESET_CMD_LEN (4)
  53. #define NCI_RESET_RSP_LEN (4)
  54. #define NCI_RESET_NTF_LEN (13)
  55. #define SN1XX_ROM_VER 0x01
  56. #define SN1xx_MAJOR_VER 0x10
  57. #define SN220_ROM_VER 0x01
  58. #define SN220_MAJOR_VER 0x01
  59. #define FW_ROM_CODE_VER_OFFSET 4
  60. #define FW_MAJOR_VER_OFFSET 7
  61. #define DL_GET_VERSION_CMD_LEN (8)
  62. #define DL_GET_VERSION_RSP_LEN_1 (12)
  63. #define DL_GET_VERSION_RSP_LEN_2 (20)
  64. #define DL_RESET_CMD_LEN (8)
  65. #define DL_GET_SESSION_STATE_CMD_LEN (8)
  66. #define DL_GET_SESSION_STATE_RSP_LEN (8)
  67. #define GET_SESSION_STS_OFF (3)
  68. #define NFCC_SESSION_STS_CLOSED (0x0)
  69. #define MAX_NCI_PAYLOAD_LEN (255)
  70. #define MAX_BUFFER_SIZE (NCI_HDR_LEN + MAX_NCI_PAYLOAD_LEN)
  71. #define MAX_DL_PAYLOAD_LEN (550)
  72. #define MAX_DL_BUFFER_SIZE (FW_HDR_LEN + FW_CRC_LEN + MAX_DL_PAYLOAD_LEN)
  73. // Maximum retry count for standby writes
  74. #define MAX_RETRY_COUNT (3)
  75. // Retry count for normal write
  76. #define NO_RETRY (1)
  77. #define MAX_IRQ_WAIT_TIME (90)
  78. #define WAKEUP_SRC_TIMEOUT (2000)
  79. /*command response timeout*/
  80. #define NCI_CMD_RSP_TIMEOUT (2000) //2s
  81. /*Time to wait for NFCC to be ready again after any change in the GPIO*/
  82. #define NFC_GPIO_SET_WAIT_TIME_USEC (10000)
  83. /*Time to wait after soft reset via any NCI/DL cmd*/
  84. #define NFC_SOFT_RESET_WAIT_TIME_USEC (5000)
  85. /*Time to wait before retrying i2c/I3C writes*/
  86. #define WRITE_RETRY_WAIT_TIME_USEC (1000)
  87. /*Time to wait before retrying read for some specific usecases*/
  88. #define READ_RETRY_WAIT_TIME_USEC (3500)
  89. #define NFC_MAGIC 0xE9
  90. /*Ioctls*/
  91. // The type should be aligned with MW HAL definitions
  92. #define NFC_SET_PWR _IOW(NFC_MAGIC, 0x01, long)
  93. #define ESE_SET_PWR _IOW(NFC_MAGIC, 0x02, long)
  94. #define ESE_GET_PWR _IOR(NFC_MAGIC, 0x03, long)
  95. /* NFC HAL can call this ioctl to get the current IRQ state */
  96. #define NFC_GET_IRQ_STATE _IO(NFC_MAGIC, 0x06)
  97. #define DTS_IRQ_GPIO_STR "nxp,pn544-irq"
  98. #define DTS_VEN_GPIO_STR "nxp,pn544-ven"
  99. #define DTS_FWDN_GPIO_STR "nxp,pn544-fw-dwnld"
  100. enum nfcc_ioctl_request {
  101. /* NFC disable request with VEN LOW */
  102. NFC_POWER_OFF = 0,
  103. /* NFC enable request with VEN Toggle */
  104. NFC_POWER_ON,
  105. /* firmware download request with VEN Toggle */
  106. NFC_FW_DWL_VEN_TOGGLE,
  107. /* ISO reset request */
  108. NFC_ISO_RESET,
  109. /* request for firmware download gpio HIGH */
  110. NFC_FW_DWL_HIGH,
  111. /* VEN hard reset request */
  112. NFC_VEN_FORCED_HARD_RESET,
  113. /* request for firmware download gpio LOW */
  114. NFC_FW_DWL_LOW,
  115. };
  116. /*nfc platform interface type*/
  117. enum interface_flags {
  118. /*I2C physical IF for NFCC */
  119. PLATFORM_IF_I2C = 0,
  120. };
  121. /*nfc state flags*/
  122. enum nfc_state_flags {
  123. /*nfc in unknown state */
  124. NFC_STATE_UNKNOWN = 0,
  125. /*nfc in download mode */
  126. NFC_STATE_FW_DWL = 0x1,
  127. /*nfc booted in NCI mode */
  128. NFC_STATE_NCI = 0x2,
  129. /*nfc booted in Fw teared mode */
  130. NFC_STATE_FW_TEARED = 0x4,
  131. };
  132. /*
  133. * Power state for IBI handing, mainly needed to defer the IBI handling
  134. * for the IBI received in suspend state to do it later in resume call
  135. */
  136. enum pm_state_flags {
  137. PM_STATE_NORMAL = 0,
  138. PM_STATE_SUSPEND,
  139. PM_STATE_IBI_BEFORE_RESUME,
  140. };
  141. /* Enum for GPIO values*/
  142. enum gpio_values {
  143. GPIO_INPUT = 0x0,
  144. GPIO_OUTPUT = 0x1,
  145. GPIO_HIGH = 0x2,
  146. GPIO_OUTPUT_HIGH = 0x3,
  147. GPIO_IRQ = 0x4,
  148. };
  149. // NFC GPIO variables
  150. struct platform_gpio {
  151. unsigned int irq;
  152. unsigned int ven;
  153. unsigned int dwl_req;
  154. };
  155. // NFC Struct to get all the required configs from DTS
  156. struct platform_configs {
  157. struct platform_gpio gpio;
  158. };
  159. //cold reset Features specific Parameters
  160. struct cold_reset {
  161. bool rsp_pending; /*cmd rsp pending status */
  162. bool in_progress; /*for cold reset when gurad timer in progress */
  163. bool reset_protection; /*reset protection enabled/disabled */
  164. uint8_t status; /*status from response buffer */
  165. uint8_t rst_prot_src; /*reset protection source (SPI, NFC) */
  166. struct timer_list timer;
  167. wait_queue_head_t read_wq;
  168. };
  169. enum chip_types {
  170. CHIP_SN1XX = 0x01,
  171. CHIP_SN220 = 0x02,
  172. CHIP_UNKNOWN = 0xFF,
  173. };
  174. /* Device specific structure */
  175. struct nfc_dev {
  176. wait_queue_head_t read_wq;
  177. struct mutex read_mutex;
  178. struct mutex write_mutex;
  179. uint8_t *read_kbuf;
  180. uint8_t *write_kbuf;
  181. struct mutex dev_ref_mutex;
  182. unsigned int dev_ref_count;
  183. struct class *nfc_class;
  184. struct device *nfc_device;
  185. struct cdev c_dev;
  186. dev_t devno;
  187. /* Interface flag */
  188. uint8_t interface;
  189. /* nfc state flags */
  190. uint8_t nfc_state;
  191. /* NFC VEN pin state */
  192. bool nfc_ven_enabled;
  193. /* current firmware major version */
  194. uint8_t fw_major_version;
  195. union {
  196. struct i2c_dev i2c_dev;
  197. };
  198. struct platform_configs configs;
  199. struct cold_reset cold_reset;
  200. /*function pointers for the common i2c functionality */
  201. int (*nfc_read)(struct nfc_dev *dev, char *buf, size_t count, int timeout);
  202. int (*nfc_write)(struct nfc_dev *dev, const char *buf, const size_t count,
  203. int max_retry_cnt);
  204. int (*nfc_enable_intr)(struct nfc_dev *dev);
  205. int (*nfc_disable_intr)(struct nfc_dev *dev);
  206. };
  207. int nfc_dev_open(struct inode *inode, struct file *filp);
  208. int nfc_dev_close(struct inode *inode, struct file *filp);
  209. long nfc_dev_ioctl(struct file *pfile, unsigned int cmd, unsigned long arg);
  210. int nfc_parse_dt(struct device *dev, struct platform_configs *nfc_configs,
  211. uint8_t interface);
  212. int nfc_misc_register(struct nfc_dev *nfc_dev,
  213. const struct file_operations *nfc_fops, int count, char *devname,
  214. char *classname);
  215. void nfc_misc_unregister(struct nfc_dev *nfc_dev, int count);
  216. int configure_gpio(unsigned int gpio, int flag);
  217. void gpio_set_ven(struct nfc_dev *nfc_dev, int value);
  218. void gpio_free_all(struct nfc_dev *nfc_dev);
  219. int validate_nfc_state_nci(struct nfc_dev *nfc_dev);
  220. int nfcc_hw_check(struct nfc_dev *nfc_dev);
  221. #endif //_COMMON_H_