common.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /******************************************************************************
  2. * Copyright (C) 2019-2020 NXP
  3. * *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. *
  18. ******************************************************************************/
  19. #ifndef _COMMON_H_
  20. #define _COMMON_H_
  21. #include <linux/types.h>
  22. #include <linux/version.h>
  23. #include <linux/semaphore.h>
  24. #include <linux/completion.h>
  25. #include <linux/ioctl.h>
  26. #include <linux/cdev.h>
  27. #include <linux/spinlock.h>
  28. #include <linux/gpio.h>
  29. #include "i2c_drv.h"
  30. #define DEV_COUNT 1 /* Max device count for this driver */
  31. #define CLASS_NAME "nfc" /* i2c device class */
  32. // NFC character device name, this will be in /dev/
  33. #define NFC_CHAR_DEV_NAME "pn553"
  34. // NCI packet details
  35. #define NCI_MSG_CMD 0x20
  36. #define NCI_MSG_RSP 0x40
  37. #define NCI_HDR_LEN 3
  38. #define NCI_PAYLOAD_IDX 3
  39. #define NCI_PAYLOAD_LEN_IDX 2
  40. // FW DNLD packet details
  41. #define FW_HDR_LEN 2
  42. #define FW_PAYLOAD_LEN_IDX 1
  43. #define FW_CRC_LEN 2
  44. #define MIN_NFC_DL_FRAME_SIZE 3
  45. #define NCI_RESET_CMD_LEN (4)
  46. #define NCI_RESET_RSP_LEN (4)
  47. #define NCI_RESET_NTF_LEN (13)
  48. #define DL_GET_VERSION_CMD_LEN (8)
  49. #define DL_GET_VERSION_RSP_LEN_1 (12)
  50. #define DL_GET_VERSION_RSP_LEN_2 (20)
  51. #define DL_RESET_CMD_LEN (8)
  52. #define DL_GET_SESSION_STATE_CMD_LEN (8)
  53. #define DL_GET_SESSION_STATE_RSP_LEN (8)
  54. #define GET_SESSION_STS_OFF (3)
  55. #define NFCC_SESSION_STS_CLOSED (0x0)
  56. #define MAX_NCI_PAYLOAD_LEN (255)
  57. #define MAX_BUFFER_SIZE (NCI_HDR_LEN + MAX_NCI_PAYLOAD_LEN)
  58. #define MAX_DL_PAYLOAD_LEN (550)
  59. #define MAX_DL_BUFFER_SIZE (FW_HDR_LEN + FW_CRC_LEN + MAX_DL_PAYLOAD_LEN)
  60. // Maximum retry count for standby writes
  61. #define MAX_RETRY_COUNT (3)
  62. // Retry count for normal write
  63. #define NO_RETRY (1)
  64. #define MAX_IRQ_WAIT_TIME (90)
  65. #define WAKEUP_SRC_TIMEOUT (2000)
  66. /*command response timeout*/
  67. #define NCI_CMD_RSP_TIMEOUT (2000) //2s
  68. #define NFC_MAGIC 0xE9
  69. /*Ioctls*/
  70. // The type should be aligned with MW HAL definitions
  71. #define NFC_SET_PWR _IOW(NFC_MAGIC, 0x01, long)
  72. #define ESE_SET_PWR _IOW(NFC_MAGIC, 0x02, long)
  73. #define ESE_GET_PWR _IOR(NFC_MAGIC, 0x03, long)
  74. #define NFC_GET_PLATFORM_TYPE _IO(NFC_MAGIC, 0x04)
  75. #define NFC_GET_NFC_STATE _IO(NFC_MAGIC, 0x05)
  76. /* NFC HAL can call this ioctl to get the current IRQ state */
  77. #define NFC_GET_IRQ_STATE _IOW(NFC_MAGIC, 0x06, long)
  78. #define DTS_IRQ_GPIO_STR "nxp,pn544-irq"
  79. #define DTS_VEN_GPIO_STR "nxp,pn544-ven"
  80. #define DTS_FWDN_GPIO_STR "nxp,pn544-fw-dwnld"
  81. enum nfcc_ioctl_request {
  82. /* NFC disable request with VEN LOW */
  83. NFC_POWER_OFF = 0,
  84. /* NFC enable request with VEN Toggle */
  85. NFC_POWER_ON,
  86. /* firmware download request with VEN Toggle */
  87. NFC_FW_DWL_VEN_TOGGLE,
  88. /* ISO reset request */
  89. NFC_ISO_RESET,
  90. /* request for firmware download gpio HIGH */
  91. NFC_FW_DWL_HIGH,
  92. /* VEN hard reset request */
  93. NFC_VEN_FORCED_HARD_RESET,
  94. /* request for firmware download gpio LOW */
  95. NFC_FW_DWL_LOW,
  96. };
  97. /*nfc platform interface type*/
  98. enum interface_flags {
  99. /*I2C physical IF for NFCC */
  100. PLATFORM_IF_I2C = 0,
  101. };
  102. /*nfc state flags*/
  103. enum nfc_state_flags {
  104. /*nfc in unknown state */
  105. NFC_STATE_UNKNOWN = 0,
  106. /*nfc in download mode */
  107. NFC_STATE_FW_DWL = 0x1,
  108. /*nfc booted in NCI mode */
  109. NFC_STATE_NCI = 0x2,
  110. /*nfc booted in Fw teared mode */
  111. NFC_STATE_FW_TEARED = 0x4,
  112. };
  113. /*
  114. * Power state for IBI handing, mainly needed to defer the IBI handling
  115. * for the IBI received in suspend state to do it later in resume call
  116. */
  117. enum pm_state_flags {
  118. PM_STATE_NORMAL = 0,
  119. PM_STATE_SUSPEND,
  120. PM_STATE_IBI_BEFORE_RESUME,
  121. };
  122. /* Enum for GPIO values*/
  123. enum gpio_values {
  124. GPIO_INPUT = 0x0,
  125. GPIO_OUTPUT = 0x1,
  126. GPIO_HIGH = 0x2,
  127. GPIO_OUTPUT_HIGH = 0x3,
  128. GPIO_IRQ = 0x4,
  129. };
  130. // NFC GPIO variables
  131. typedef struct platform_gpio {
  132. unsigned int irq;
  133. unsigned int ven;
  134. unsigned int dwl_req;
  135. } platform_gpio_t;
  136. //cold reset Features specific Parameters
  137. typedef struct cold_reset {
  138. bool rsp_pending; /*cmd rsp pending status */
  139. bool in_progress; /*for cold reset when gurad timer in progress */
  140. bool reset_protection; /*reset protection enabled/disabled */
  141. uint8_t status; /*status from response buffer */
  142. uint8_t rst_prot_src; /*reset protection source (SPI, NFC) */
  143. struct mutex sync_mutex;
  144. struct timer_list timer;
  145. wait_queue_head_t read_wq;
  146. } cold_reset_t;
  147. /* Device specific structure */
  148. typedef struct nfc_dev {
  149. wait_queue_head_t read_wq;
  150. struct mutex read_mutex;
  151. struct mutex ese_access_mutex;
  152. struct mutex dev_ref_mutex;
  153. unsigned int dev_ref_count;
  154. struct class *nfc_class;
  155. struct device *nfc_device;
  156. struct cdev c_dev;
  157. dev_t devno;
  158. /* Interface flag */
  159. uint8_t interface;
  160. /* nfc state flags */
  161. uint8_t nfc_state;
  162. /* NFC VEN pin state */
  163. bool nfc_ven_enabled;
  164. union {
  165. i2c_dev_t i2c_dev;
  166. };
  167. platform_gpio_t gpio;
  168. cold_reset_t cold_reset;
  169. /*funtion pointers for the common i2c functionality */
  170. int (*nfc_read) (struct nfc_dev *dev, char *buf, size_t count);
  171. int (*nfc_write) (struct nfc_dev *dev, const char *buf, const size_t count,
  172. int max_retry_cnt);
  173. int (*nfc_enable_intr) (struct nfc_dev *dev);
  174. int (*nfc_disable_intr) (struct nfc_dev *dev);
  175. int (*nfc_flush_readq) (struct nfc_dev *dev);
  176. } nfc_dev_t;
  177. int nfc_dev_open(struct inode *inode, struct file *filp);
  178. int nfc_dev_close(struct inode *inode, struct file *filp);
  179. long nfc_dev_ioctl(struct file *pfile, unsigned int cmd, unsigned long arg);
  180. int nfc_parse_dt(struct device *dev, platform_gpio_t *nfc_gpio,
  181. uint8_t interface);
  182. int nfc_misc_register(nfc_dev_t *nfc_dev,
  183. const struct file_operations *nfc_fops, int count, char *devname,
  184. char *classname);
  185. void nfc_misc_unregister(nfc_dev_t *nfc_dev, int count);
  186. int configure_gpio(unsigned int gpio, int flag);
  187. void gpio_set_ven(nfc_dev_t *nfc_dev, int value);
  188. void gpio_free_all(nfc_dev_t *nfc_dev);
  189. int validate_nfc_state_nci(nfc_dev_t *nfc_dev);
  190. #endif //_COMMON_H_