common.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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/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 "nfc"
  28. /* NFC character device name, this will be in /dev/ */
  29. #define NFC_CHAR_DEV_NAME "pn553"
  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 for IRQ low during write 5*3ms */
  58. #define NFC_WRITE_IRQ_WAIT_TIME_US (3000)
  59. /* Time to wait before retrying i2c/I3C writes */
  60. #define WRITE_RETRY_WAIT_TIME_US (1000)
  61. /* Time to wait before retrying read for some specific usecases */
  62. #define READ_RETRY_WAIT_TIME_US (3500)
  63. #define NFC_MAGIC (0xE9)
  64. /* Ioctls */
  65. /* The type should be aligned with MW HAL definitions */
  66. #define NFC_SET_PWR _IOW(NFC_MAGIC, 0x01, long)
  67. #define ESE_SET_PWR _IOW(NFC_MAGIC, 0x02, long)
  68. #define ESE_GET_PWR _IOR(NFC_MAGIC, 0x03, long)
  69. #define DTS_IRQ_GPIO_STR "nxp,pn544-irq"
  70. #define DTS_VEN_GPIO_STR "nxp,pn544-ven"
  71. #define DTS_FWDN_GPIO_STR "nxp,pn544-fw-dwnld"
  72. enum nfcc_ioctl_request {
  73. /* NFC disable request with VEN LOW */
  74. NFC_POWER_OFF = 0,
  75. /* NFC enable request with VEN Toggle */
  76. NFC_POWER_ON,
  77. /* firmware download request with VEN Toggle */
  78. NFC_FW_DWL_VEN_TOGGLE,
  79. /* ISO reset request */
  80. NFC_ISO_RESET,
  81. /* request for firmware download gpio HIGH */
  82. NFC_FW_DWL_HIGH,
  83. /* VEN hard reset request */
  84. NFC_VEN_FORCED_HARD_RESET,
  85. /* request for firmware download gpio LOW */
  86. NFC_FW_DWL_LOW,
  87. };
  88. /* nfc platform interface type */
  89. enum interface_flags {
  90. /* I2C physical IF for NFCC */
  91. PLATFORM_IF_I2C = 0,
  92. };
  93. /* nfc state flags */
  94. enum nfc_state_flags {
  95. /* nfc in unknown state */
  96. NFC_STATE_UNKNOWN = 0,
  97. /* nfc in download mode */
  98. NFC_STATE_FW_DWL = 0x1,
  99. /* nfc booted in NCI mode */
  100. NFC_STATE_NCI = 0x2,
  101. /* nfc booted in Fw teared mode */
  102. NFC_STATE_FW_TEARED = 0x4,
  103. };
  104. /*
  105. * Power state for IBI handing, mainly needed to defer the IBI handling
  106. * for the IBI received in suspend state to do it later in resume call
  107. */
  108. enum pm_state_flags {
  109. PM_STATE_NORMAL = 0,
  110. PM_STATE_SUSPEND,
  111. PM_STATE_IBI_BEFORE_RESUME,
  112. };
  113. /* Enum for GPIO values */
  114. enum gpio_values {
  115. GPIO_INPUT = 0x0,
  116. GPIO_OUTPUT = 0x1,
  117. GPIO_HIGH = 0x2,
  118. GPIO_OUTPUT_HIGH = 0x3,
  119. GPIO_IRQ = 0x4,
  120. };
  121. /* NFC GPIO variables */
  122. struct platform_gpio {
  123. unsigned int irq;
  124. unsigned int ven;
  125. unsigned int dwl_req;
  126. };
  127. /* NFC Struct to get all the required configs from DTS */
  128. struct platform_configs {
  129. struct platform_gpio gpio;
  130. };
  131. /* cold reset Features specific Parameters */
  132. struct cold_reset {
  133. bool rsp_pending; /* cmd rsp pending status */
  134. bool in_progress; /* for cold reset when gurad timer in progress */
  135. bool reset_protection; /* reset protection enabled/disabled */
  136. uint8_t status; /* status from response buffer */
  137. uint8_t rst_prot_src; /* reset protection source (SPI, NFC) */
  138. struct timer_list timer;
  139. wait_queue_head_t read_wq;
  140. };
  141. /* Device specific structure */
  142. struct nfc_dev {
  143. wait_queue_head_t read_wq;
  144. struct mutex read_mutex;
  145. struct mutex write_mutex;
  146. uint8_t *read_kbuf;
  147. uint8_t *write_kbuf;
  148. struct mutex dev_ref_mutex;
  149. unsigned int dev_ref_count;
  150. struct class *nfc_class;
  151. struct device *nfc_device;
  152. struct cdev c_dev;
  153. dev_t devno;
  154. /* Interface flag */
  155. uint8_t interface;
  156. /* nfc state flags */
  157. uint8_t nfc_state;
  158. /* NFC VEN pin state */
  159. bool nfc_ven_enabled;
  160. bool release_read;
  161. union {
  162. struct i2c_dev i2c_dev;
  163. };
  164. struct platform_configs configs;
  165. struct cold_reset cold_reset;
  166. /* function pointers for the common i2c functionality */
  167. int (*nfc_read)(struct nfc_dev *dev, char *buf, size_t count,
  168. int timeout);
  169. int (*nfc_write)(struct nfc_dev *dev, const char *buf,
  170. const size_t count, int max_retry_cnt);
  171. int (*nfc_enable_intr)(struct nfc_dev *dev);
  172. int (*nfc_disable_intr)(struct nfc_dev *dev);
  173. };
  174. int nfc_dev_open(struct inode *inode, struct file *filp);
  175. int nfc_dev_flush(struct file *pfile, fl_owner_t id);
  176. int nfc_dev_close(struct inode *inode, struct file *filp);
  177. long nfc_dev_ioctl(struct file *pfile, unsigned int cmd, unsigned long arg);
  178. int nfc_parse_dt(struct device *dev, struct platform_configs *nfc_configs,
  179. uint8_t interface);
  180. int nfc_misc_register(struct nfc_dev *nfc_dev,
  181. const struct file_operations *nfc_fops, int count,
  182. char *devname, char *classname);
  183. void nfc_misc_unregister(struct nfc_dev *nfc_dev, int count);
  184. int configure_gpio(unsigned int gpio, int flag);
  185. void gpio_set_ven(struct nfc_dev *nfc_dev, int value);
  186. void gpio_free_all(struct nfc_dev *nfc_dev);
  187. int validate_nfc_state_nci(struct nfc_dev *nfc_dev);
  188. #endif /* _COMMON_H_ */