common.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 "i2c_drv.h"
  24. /* Max device count for this driver */
  25. #define DEV_COUNT 1
  26. /* i2c device class */
  27. #define CLASS_NAME "nxpnfc"
  28. /* NFC character device name, this will be in /dev/ */
  29. #define NFC_CHAR_DEV_NAME "nxp-nci"
  30. /* NCI packet details */
  31. #define NCI_CMD (0x20)
  32. #define NCI_RSP (0x40)
  33. #define NCI_HDR_LEN (3)
  34. #define NCI_HDR_IDX (0)
  35. #define NCI_HDR_OID_IDX (1)
  36. #define NCI_PAYLOAD_IDX (3)
  37. #define NCI_PAYLOAD_LEN_IDX (2)
  38. /* FW DNLD packet details */
  39. #define DL_HDR_LEN (2)
  40. #define DL_CRC_LEN (2)
  41. #define MAX_NCI_PAYLOAD_LEN (255)
  42. #define MAX_NCI_BUFFER_SIZE (NCI_HDR_LEN + MAX_NCI_PAYLOAD_LEN)
  43. #define MAX_DL_PAYLOAD_LEN (550)
  44. #define MAX_DL_BUFFER_SIZE (DL_HDR_LEN + DL_CRC_LEN + \
  45. MAX_DL_PAYLOAD_LEN)
  46. /* Retry count for normal write */
  47. #define NO_RETRY (1)
  48. /* Maximum retry count for standby writes */
  49. #define MAX_RETRY_COUNT (3)
  50. #define MAX_WRITE_IRQ_COUNT (5)
  51. #define MAX_IRQ_WAIT_TIME (90)
  52. #define WAKEUP_SRC_TIMEOUT (2000)
  53. /* command response timeout */
  54. #define NCI_CMD_RSP_TIMEOUT_MS (2000)
  55. /* Time to wait for NFCC to be ready again after any change in the GPIO */
  56. #define NFC_GPIO_SET_WAIT_TIME_US (10000)
  57. /* Time to wait before retrying writes */
  58. #define WRITE_RETRY_WAIT_TIME_US (3000)
  59. /* Time to wait before retrying read for some specific usecases */
  60. #define READ_RETRY_WAIT_TIME_US (3500)
  61. #define NFC_MAGIC (0xE9)
  62. /* Ioctls */
  63. /* The type should be aligned with MW HAL definitions */
  64. #define NFC_SET_PWR _IOW(NFC_MAGIC, 0x01, uint32_t)
  65. #define ESE_SET_PWR _IOW(NFC_MAGIC, 0x02, uint32_t)
  66. #define ESE_GET_PWR _IOR(NFC_MAGIC, 0x03, uint32_t)
  67. #define NFC_SET_RESET_READ_PENDING _IOW(NFC_MAGIC, 0x04, uint32_t)
  68. #define DTS_IRQ_GPIO_STR "nxp,sn-irq"
  69. #define DTS_VEN_GPIO_STR "nxp,sn-ven-rstn"
  70. #define DTS_FWDN_GPIO_STR "nxp,sn-dwl-req"
  71. enum nfcc_ioctl_request {
  72. /* NFC disable request with VEN LOW */
  73. NFC_POWER_OFF = 0,
  74. /* NFC enable request with VEN Toggle */
  75. NFC_POWER_ON,
  76. /* firmware download request with VEN Toggle */
  77. NFC_FW_DWL_VEN_TOGGLE,
  78. /* ISO reset request */
  79. NFC_ISO_RESET,
  80. /* request for firmware download gpio HIGH */
  81. NFC_FW_DWL_HIGH,
  82. /* VEN hard reset request */
  83. NFC_VEN_FORCED_HARD_RESET,
  84. /* request for firmware download gpio LOW */
  85. NFC_FW_DWL_LOW,
  86. };
  87. enum nfc_read_pending {
  88. NFC_RESET_READ_PENDING,
  89. NFC_SET_READ_PENDING,
  90. };
  91. /* nfc platform interface type */
  92. enum interface_flags {
  93. /* I2C physical IF for NFCC */
  94. PLATFORM_IF_I2C = 0,
  95. };
  96. /* nfc state flags */
  97. enum nfc_state_flags {
  98. /* nfc in unknown state */
  99. NFC_STATE_UNKNOWN = 0,
  100. /* nfc in download mode */
  101. NFC_STATE_FW_DWL = 0x1,
  102. /* nfc booted in NCI mode */
  103. NFC_STATE_NCI = 0x2,
  104. /* nfc booted in Fw teared mode */
  105. NFC_STATE_FW_TEARED = 0x4,
  106. };
  107. /*
  108. * Power state for IBI handing, mainly needed to defer the IBI handling
  109. * for the IBI received in suspend state to do it later in resume call
  110. */
  111. enum pm_state_flags {
  112. PM_STATE_NORMAL = 0,
  113. PM_STATE_SUSPEND,
  114. PM_STATE_IBI_BEFORE_RESUME,
  115. };
  116. /* Enum for GPIO values */
  117. enum gpio_values {
  118. GPIO_INPUT = 0x0,
  119. GPIO_OUTPUT = 0x1,
  120. GPIO_HIGH = 0x2,
  121. GPIO_OUTPUT_HIGH = 0x3,
  122. GPIO_IRQ = 0x4,
  123. };
  124. /* NFC GPIO variables */
  125. struct platform_gpio {
  126. unsigned int irq;
  127. unsigned int ven;
  128. unsigned int dwl_req;
  129. };
  130. /* NFC Struct to get all the required configs from DTS */
  131. struct platform_configs {
  132. struct platform_gpio gpio;
  133. };
  134. /* cold reset Features specific Parameters */
  135. struct cold_reset {
  136. bool rsp_pending; /* cmd rsp pending status */
  137. bool in_progress; /* for cold reset when gurad timer in progress */
  138. bool reset_protection; /* reset protection enabled/disabled */
  139. uint8_t status; /* status from response buffer */
  140. uint8_t rst_prot_src; /* reset protection source (SPI, NFC) */
  141. struct timer_list timer;
  142. wait_queue_head_t read_wq;
  143. bool is_nfc_read_pending;
  144. };
  145. /* Device specific structure */
  146. struct nfc_dev {
  147. wait_queue_head_t read_wq;
  148. struct mutex read_mutex;
  149. struct mutex write_mutex;
  150. uint8_t *read_kbuf;
  151. uint8_t *write_kbuf;
  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. bool release_read;
  165. union {
  166. struct i2c_dev i2c_dev;
  167. };
  168. struct platform_configs configs;
  169. struct cold_reset cold_reset;
  170. /* function pointers for the common i2c functionality */
  171. int (*nfc_read)(struct nfc_dev *dev, char *buf, size_t count,
  172. int timeout);
  173. int (*nfc_write)(struct nfc_dev *dev, const char *buf,
  174. const size_t count, int max_retry_cnt);
  175. int (*nfc_enable_intr)(struct nfc_dev *dev);
  176. int (*nfc_disable_intr)(struct nfc_dev *dev);
  177. };
  178. int nfc_dev_open(struct inode *inode, struct file *filp);
  179. int nfc_dev_flush(struct file *pfile, fl_owner_t id);
  180. int nfc_dev_close(struct inode *inode, struct file *filp);
  181. long nfc_dev_compat_ioctl(struct file *pfile, unsigned int cmd,
  182. unsigned long arg);
  183. long nfc_dev_ioctl(struct file *pfile, unsigned int cmd, unsigned long arg);
  184. int nfc_parse_dt(struct device *dev, struct platform_configs *nfc_configs,
  185. uint8_t interface);
  186. int nfc_misc_register(struct nfc_dev *nfc_dev,
  187. const struct file_operations *nfc_fops, int count,
  188. char *devname, char *classname);
  189. void nfc_misc_unregister(struct nfc_dev *nfc_dev, int count);
  190. int configure_gpio(unsigned int gpio, int flag);
  191. void gpio_set_ven(struct nfc_dev *nfc_dev, int value);
  192. void gpio_free_all(struct nfc_dev *nfc_dev);
  193. int validate_nfc_state_nci(struct nfc_dev *nfc_dev);
  194. #endif /* _COMMON_H_ */