trf7970a.c 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * TI TRF7970a RFID/NFC Transceiver Driver
  4. *
  5. * Copyright (C) 2013 Texas Instruments Incorporated - http://www.ti.com
  6. *
  7. * Author: Erick Macias <emacias@ti.com>
  8. * Author: Felipe Balbi <balbi@ti.com>
  9. * Author: Mark A. Greer <mgreer@animalcreek.com>
  10. */
  11. #include <linux/module.h>
  12. #include <linux/device.h>
  13. #include <linux/netdevice.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/pm_runtime.h>
  16. #include <linux/nfc.h>
  17. #include <linux/skbuff.h>
  18. #include <linux/delay.h>
  19. #include <linux/gpio/consumer.h>
  20. #include <linux/of.h>
  21. #include <linux/spi/spi.h>
  22. #include <linux/regulator/consumer.h>
  23. #include <net/nfc/nfc.h>
  24. #include <net/nfc/digital.h>
  25. /* There are 3 ways the host can communicate with the trf7970a:
  26. * parallel mode, SPI with Slave Select (SS) mode, and SPI without
  27. * SS mode. The driver only supports the two SPI modes.
  28. *
  29. * The trf7970a is very timing sensitive and the VIN, EN2, and EN
  30. * pins must asserted in that order and with specific delays in between.
  31. * The delays used in the driver were provided by TI and have been
  32. * confirmed to work with this driver. There is a bug with the current
  33. * version of the trf7970a that requires that EN2 remain low no matter
  34. * what. If it goes high, it will generate an RF field even when in
  35. * passive target mode. TI has indicated that the chip will work okay
  36. * when EN2 is left low. The 'en2-rf-quirk' device tree property
  37. * indicates that trf7970a currently being used has the erratum and
  38. * that EN2 must be kept low.
  39. *
  40. * Timeouts are implemented using the delayed workqueue kernel facility.
  41. * Timeouts are required so things don't hang when there is no response
  42. * from the trf7970a (or tag). Using this mechanism creates a race with
  43. * interrupts, however. That is, an interrupt and a timeout could occur
  44. * closely enough together that one is blocked by the mutex while the other
  45. * executes. When the timeout handler executes first and blocks the
  46. * interrupt handler, it will eventually set the state to IDLE so the
  47. * interrupt handler will check the state and exit with no harm done.
  48. * When the interrupt handler executes first and blocks the timeout handler,
  49. * the cancel_delayed_work() call will know that it didn't cancel the
  50. * work item (i.e., timeout) and will return zero. That return code is
  51. * used by the timer handler to indicate that it should ignore the timeout
  52. * once its unblocked.
  53. *
  54. * Aborting an active command isn't as simple as it seems because the only
  55. * way to abort a command that's already been sent to the tag is so turn
  56. * off power to the tag. If we do that, though, we'd have to go through
  57. * the entire anticollision procedure again but the digital layer doesn't
  58. * support that. So, if an abort is received before trf7970a_send_cmd()
  59. * has sent the command to the tag, it simply returns -ECANCELED. If the
  60. * command has already been sent to the tag, then the driver continues
  61. * normally and recieves the response data (or error) but just before
  62. * sending the data upstream, it frees the rx_skb and sends -ECANCELED
  63. * upstream instead. If the command failed, that error will be sent
  64. * upstream.
  65. *
  66. * When recieving data from a tag and the interrupt status register has
  67. * only the SRX bit set, it means that all of the data has been received
  68. * (once what's in the fifo has been read). However, depending on timing
  69. * an interrupt status with only the SRX bit set may not be recived. In
  70. * those cases, the timeout mechanism is used to wait 20 ms in case more
  71. * data arrives. After 20 ms, it is assumed that all of the data has been
  72. * received and the accumulated rx data is sent upstream. The
  73. * 'TRF7970A_ST_WAIT_FOR_RX_DATA_CONT' state is used for this purpose
  74. * (i.e., it indicates that some data has been received but we're not sure
  75. * if there is more coming so a timeout in this state means all data has
  76. * been received and there isn't an error). The delay is 20 ms since delays
  77. * of ~16 ms have been observed during testing.
  78. *
  79. * When transmitting a frame larger than the FIFO size (127 bytes), the
  80. * driver will wait 20 ms for the FIFO to drain past the low-watermark
  81. * and generate an interrupt. The low-watermark set to 32 bytes so the
  82. * interrupt should fire after 127 - 32 = 95 bytes have been sent. At
  83. * the lowest possible bit rate (6.62 kbps for 15693), it will take up
  84. * to ~14.35 ms so 20 ms is used for the timeout.
  85. *
  86. * Type 2 write and sector select commands respond with a 4-bit ACK or NACK.
  87. * Having only 4 bits in the FIFO won't normally generate an interrupt so
  88. * driver enables the '4_bit_RX' bit of the Special Functions register 1
  89. * to cause an interrupt in that case. Leaving that bit for a read command
  90. * messes up the data returned so it is only enabled when the framing is
  91. * 'NFC_DIGITAL_FRAMING_NFCA_T2T' and the command is not a read command.
  92. * Unfortunately, that means that the driver has to peek into tx frames
  93. * when the framing is 'NFC_DIGITAL_FRAMING_NFCA_T2T'. This is done by
  94. * the trf7970a_per_cmd_config() routine.
  95. *
  96. * ISO/IEC 15693 frames specify whether to use single or double sub-carrier
  97. * frequencies and whether to use low or high data rates in the flags byte
  98. * of the frame. This means that the driver has to peek at all 15693 frames
  99. * to determine what speed to set the communication to. In addition, write
  100. * and lock commands use the OPTION flag to indicate that an EOF must be
  101. * sent to the tag before it will send its response. So the driver has to
  102. * examine all frames for that reason too.
  103. *
  104. * It is unclear how long to wait before sending the EOF. According to the
  105. * Note under Table 1-1 in section 1.6 of
  106. * http://www.ti.com/lit/ug/scbu011/scbu011.pdf, that wait should be at least
  107. * 10 ms for TI Tag-it HF-I tags; however testing has shown that is not long
  108. * enough so 20 ms is used. So the timer is set to 40 ms - 20 ms to drain
  109. * up to 127 bytes in the FIFO at the lowest bit rate plus another 20 ms to
  110. * ensure the wait is long enough before sending the EOF. This seems to work
  111. * reliably.
  112. */
  113. #define TRF7970A_SUPPORTED_PROTOCOLS \
  114. (NFC_PROTO_MIFARE_MASK | NFC_PROTO_ISO14443_MASK | \
  115. NFC_PROTO_ISO14443_B_MASK | NFC_PROTO_FELICA_MASK | \
  116. NFC_PROTO_ISO15693_MASK | NFC_PROTO_NFC_DEP_MASK)
  117. #define TRF7970A_AUTOSUSPEND_DELAY 30000 /* 30 seconds */
  118. #define TRF7970A_13MHZ_CLOCK_FREQUENCY 13560000
  119. #define TRF7970A_27MHZ_CLOCK_FREQUENCY 27120000
  120. #define TRF7970A_RX_SKB_ALLOC_SIZE 256
  121. #define TRF7970A_FIFO_SIZE 127
  122. /* TX length is 3 nibbles long ==> 4KB - 1 bytes max */
  123. #define TRF7970A_TX_MAX (4096 - 1)
  124. #define TRF7970A_WAIT_FOR_TX_IRQ 20
  125. #define TRF7970A_WAIT_FOR_RX_DATA_TIMEOUT 20
  126. #define TRF7970A_WAIT_FOR_FIFO_DRAIN_TIMEOUT 20
  127. #define TRF7970A_WAIT_TO_ISSUE_ISO15693_EOF 40
  128. /* Guard times for various RF technologies (in us) */
  129. #define TRF7970A_GUARD_TIME_NFCA 5000
  130. #define TRF7970A_GUARD_TIME_NFCB 5000
  131. #define TRF7970A_GUARD_TIME_NFCF 20000
  132. #define TRF7970A_GUARD_TIME_15693 1000
  133. /* Quirks */
  134. /* Erratum: When reading IRQ Status register on trf7970a, we must issue a
  135. * read continuous command for IRQ Status and Collision Position registers.
  136. */
  137. #define TRF7970A_QUIRK_IRQ_STATUS_READ BIT(0)
  138. #define TRF7970A_QUIRK_EN2_MUST_STAY_LOW BIT(1)
  139. /* Direct commands */
  140. #define TRF7970A_CMD_IDLE 0x00
  141. #define TRF7970A_CMD_SOFT_INIT 0x03
  142. #define TRF7970A_CMD_RF_COLLISION 0x04
  143. #define TRF7970A_CMD_RF_COLLISION_RESPONSE_N 0x05
  144. #define TRF7970A_CMD_RF_COLLISION_RESPONSE_0 0x06
  145. #define TRF7970A_CMD_FIFO_RESET 0x0f
  146. #define TRF7970A_CMD_TRANSMIT_NO_CRC 0x10
  147. #define TRF7970A_CMD_TRANSMIT 0x11
  148. #define TRF7970A_CMD_DELAY_TRANSMIT_NO_CRC 0x12
  149. #define TRF7970A_CMD_DELAY_TRANSMIT 0x13
  150. #define TRF7970A_CMD_EOF 0x14
  151. #define TRF7970A_CMD_CLOSE_SLOT 0x15
  152. #define TRF7970A_CMD_BLOCK_RX 0x16
  153. #define TRF7970A_CMD_ENABLE_RX 0x17
  154. #define TRF7970A_CMD_TEST_INT_RF 0x18
  155. #define TRF7970A_CMD_TEST_EXT_RF 0x19
  156. #define TRF7970A_CMD_RX_GAIN_ADJUST 0x1a
  157. /* Bits determining whether its a direct command or register R/W,
  158. * whether to use a continuous SPI transaction or not, and the actual
  159. * direct cmd opcode or register address.
  160. */
  161. #define TRF7970A_CMD_BIT_CTRL BIT(7)
  162. #define TRF7970A_CMD_BIT_RW BIT(6)
  163. #define TRF7970A_CMD_BIT_CONTINUOUS BIT(5)
  164. #define TRF7970A_CMD_BIT_OPCODE(opcode) ((opcode) & 0x1f)
  165. /* Registers addresses */
  166. #define TRF7970A_CHIP_STATUS_CTRL 0x00
  167. #define TRF7970A_ISO_CTRL 0x01
  168. #define TRF7970A_ISO14443B_TX_OPTIONS 0x02
  169. #define TRF7970A_ISO14443A_HIGH_BITRATE_OPTIONS 0x03
  170. #define TRF7970A_TX_TIMER_SETTING_H_BYTE 0x04
  171. #define TRF7970A_TX_TIMER_SETTING_L_BYTE 0x05
  172. #define TRF7970A_TX_PULSE_LENGTH_CTRL 0x06
  173. #define TRF7970A_RX_NO_RESPONSE_WAIT 0x07
  174. #define TRF7970A_RX_WAIT_TIME 0x08
  175. #define TRF7970A_MODULATOR_SYS_CLK_CTRL 0x09
  176. #define TRF7970A_RX_SPECIAL_SETTINGS 0x0a
  177. #define TRF7970A_REG_IO_CTRL 0x0b
  178. #define TRF7970A_IRQ_STATUS 0x0c
  179. #define TRF7970A_COLLISION_IRQ_MASK 0x0d
  180. #define TRF7970A_COLLISION_POSITION 0x0e
  181. #define TRF7970A_RSSI_OSC_STATUS 0x0f
  182. #define TRF7970A_SPECIAL_FCN_REG1 0x10
  183. #define TRF7970A_SPECIAL_FCN_REG2 0x11
  184. #define TRF7970A_RAM1 0x12
  185. #define TRF7970A_RAM2 0x13
  186. #define TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS 0x14
  187. #define TRF7970A_NFC_LOW_FIELD_LEVEL 0x16
  188. #define TRF7970A_NFCID1 0x17
  189. #define TRF7970A_NFC_TARGET_LEVEL 0x18
  190. #define TRF79070A_NFC_TARGET_PROTOCOL 0x19
  191. #define TRF7970A_TEST_REGISTER1 0x1a
  192. #define TRF7970A_TEST_REGISTER2 0x1b
  193. #define TRF7970A_FIFO_STATUS 0x1c
  194. #define TRF7970A_TX_LENGTH_BYTE1 0x1d
  195. #define TRF7970A_TX_LENGTH_BYTE2 0x1e
  196. #define TRF7970A_FIFO_IO_REGISTER 0x1f
  197. /* Chip Status Control Register Bits */
  198. #define TRF7970A_CHIP_STATUS_VRS5_3 BIT(0)
  199. #define TRF7970A_CHIP_STATUS_REC_ON BIT(1)
  200. #define TRF7970A_CHIP_STATUS_AGC_ON BIT(2)
  201. #define TRF7970A_CHIP_STATUS_PM_ON BIT(3)
  202. #define TRF7970A_CHIP_STATUS_RF_PWR BIT(4)
  203. #define TRF7970A_CHIP_STATUS_RF_ON BIT(5)
  204. #define TRF7970A_CHIP_STATUS_DIRECT BIT(6)
  205. #define TRF7970A_CHIP_STATUS_STBY BIT(7)
  206. /* ISO Control Register Bits */
  207. #define TRF7970A_ISO_CTRL_15693_SGL_1OF4_662 0x00
  208. #define TRF7970A_ISO_CTRL_15693_SGL_1OF256_662 0x01
  209. #define TRF7970A_ISO_CTRL_15693_SGL_1OF4_2648 0x02
  210. #define TRF7970A_ISO_CTRL_15693_SGL_1OF256_2648 0x03
  211. #define TRF7970A_ISO_CTRL_15693_DBL_1OF4_667a 0x04
  212. #define TRF7970A_ISO_CTRL_15693_DBL_1OF256_667 0x05
  213. #define TRF7970A_ISO_CTRL_15693_DBL_1OF4_2669 0x06
  214. #define TRF7970A_ISO_CTRL_15693_DBL_1OF256_2669 0x07
  215. #define TRF7970A_ISO_CTRL_14443A_106 0x08
  216. #define TRF7970A_ISO_CTRL_14443A_212 0x09
  217. #define TRF7970A_ISO_CTRL_14443A_424 0x0a
  218. #define TRF7970A_ISO_CTRL_14443A_848 0x0b
  219. #define TRF7970A_ISO_CTRL_14443B_106 0x0c
  220. #define TRF7970A_ISO_CTRL_14443B_212 0x0d
  221. #define TRF7970A_ISO_CTRL_14443B_424 0x0e
  222. #define TRF7970A_ISO_CTRL_14443B_848 0x0f
  223. #define TRF7970A_ISO_CTRL_FELICA_212 0x1a
  224. #define TRF7970A_ISO_CTRL_FELICA_424 0x1b
  225. #define TRF7970A_ISO_CTRL_NFC_NFCA_106 0x01
  226. #define TRF7970A_ISO_CTRL_NFC_NFCF_212 0x02
  227. #define TRF7970A_ISO_CTRL_NFC_NFCF_424 0x03
  228. #define TRF7970A_ISO_CTRL_NFC_CE_14443A 0x00
  229. #define TRF7970A_ISO_CTRL_NFC_CE_14443B 0x01
  230. #define TRF7970A_ISO_CTRL_NFC_CE BIT(2)
  231. #define TRF7970A_ISO_CTRL_NFC_ACTIVE BIT(3)
  232. #define TRF7970A_ISO_CTRL_NFC_INITIATOR BIT(4)
  233. #define TRF7970A_ISO_CTRL_NFC_NFC_CE_MODE BIT(5)
  234. #define TRF7970A_ISO_CTRL_RFID BIT(5)
  235. #define TRF7970A_ISO_CTRL_DIR_MODE BIT(6)
  236. #define TRF7970A_ISO_CTRL_RX_CRC_N BIT(7) /* true == No CRC */
  237. #define TRF7970A_ISO_CTRL_RFID_SPEED_MASK 0x1f
  238. /* Modulator and SYS_CLK Control Register Bits */
  239. #define TRF7970A_MODULATOR_DEPTH(n) ((n) & 0x7)
  240. #define TRF7970A_MODULATOR_DEPTH_ASK10 (TRF7970A_MODULATOR_DEPTH(0))
  241. #define TRF7970A_MODULATOR_DEPTH_OOK (TRF7970A_MODULATOR_DEPTH(1))
  242. #define TRF7970A_MODULATOR_DEPTH_ASK7 (TRF7970A_MODULATOR_DEPTH(2))
  243. #define TRF7970A_MODULATOR_DEPTH_ASK8_5 (TRF7970A_MODULATOR_DEPTH(3))
  244. #define TRF7970A_MODULATOR_DEPTH_ASK13 (TRF7970A_MODULATOR_DEPTH(4))
  245. #define TRF7970A_MODULATOR_DEPTH_ASK16 (TRF7970A_MODULATOR_DEPTH(5))
  246. #define TRF7970A_MODULATOR_DEPTH_ASK22 (TRF7970A_MODULATOR_DEPTH(6))
  247. #define TRF7970A_MODULATOR_DEPTH_ASK30 (TRF7970A_MODULATOR_DEPTH(7))
  248. #define TRF7970A_MODULATOR_EN_ANA BIT(3)
  249. #define TRF7970A_MODULATOR_CLK(n) (((n) & 0x3) << 4)
  250. #define TRF7970A_MODULATOR_CLK_DISABLED (TRF7970A_MODULATOR_CLK(0))
  251. #define TRF7970A_MODULATOR_CLK_3_6 (TRF7970A_MODULATOR_CLK(1))
  252. #define TRF7970A_MODULATOR_CLK_6_13 (TRF7970A_MODULATOR_CLK(2))
  253. #define TRF7970A_MODULATOR_CLK_13_27 (TRF7970A_MODULATOR_CLK(3))
  254. #define TRF7970A_MODULATOR_EN_OOK BIT(6)
  255. #define TRF7970A_MODULATOR_27MHZ BIT(7)
  256. #define TRF7970A_RX_SPECIAL_SETTINGS_NO_LIM BIT(0)
  257. #define TRF7970A_RX_SPECIAL_SETTINGS_AGCR BIT(1)
  258. #define TRF7970A_RX_SPECIAL_SETTINGS_GD_0DB (0x0 << 2)
  259. #define TRF7970A_RX_SPECIAL_SETTINGS_GD_5DB (0x1 << 2)
  260. #define TRF7970A_RX_SPECIAL_SETTINGS_GD_10DB (0x2 << 2)
  261. #define TRF7970A_RX_SPECIAL_SETTINGS_GD_15DB (0x3 << 2)
  262. #define TRF7970A_RX_SPECIAL_SETTINGS_HBT BIT(4)
  263. #define TRF7970A_RX_SPECIAL_SETTINGS_M848 BIT(5)
  264. #define TRF7970A_RX_SPECIAL_SETTINGS_C424 BIT(6)
  265. #define TRF7970A_RX_SPECIAL_SETTINGS_C212 BIT(7)
  266. #define TRF7970A_REG_IO_CTRL_VRS(v) ((v) & 0x07)
  267. #define TRF7970A_REG_IO_CTRL_IO_LOW BIT(5)
  268. #define TRF7970A_REG_IO_CTRL_EN_EXT_PA BIT(6)
  269. #define TRF7970A_REG_IO_CTRL_AUTO_REG BIT(7)
  270. /* IRQ Status Register Bits */
  271. #define TRF7970A_IRQ_STATUS_NORESP BIT(0) /* ISO15693 only */
  272. #define TRF7970A_IRQ_STATUS_NFC_COL_ERROR BIT(0)
  273. #define TRF7970A_IRQ_STATUS_COL BIT(1)
  274. #define TRF7970A_IRQ_STATUS_FRAMING_EOF_ERROR BIT(2)
  275. #define TRF7970A_IRQ_STATUS_NFC_RF BIT(2)
  276. #define TRF7970A_IRQ_STATUS_PARITY_ERROR BIT(3)
  277. #define TRF7970A_IRQ_STATUS_NFC_SDD BIT(3)
  278. #define TRF7970A_IRQ_STATUS_CRC_ERROR BIT(4)
  279. #define TRF7970A_IRQ_STATUS_NFC_PROTO_ERROR BIT(4)
  280. #define TRF7970A_IRQ_STATUS_FIFO BIT(5)
  281. #define TRF7970A_IRQ_STATUS_SRX BIT(6)
  282. #define TRF7970A_IRQ_STATUS_TX BIT(7)
  283. #define TRF7970A_IRQ_STATUS_ERROR \
  284. (TRF7970A_IRQ_STATUS_COL | \
  285. TRF7970A_IRQ_STATUS_FRAMING_EOF_ERROR | \
  286. TRF7970A_IRQ_STATUS_PARITY_ERROR | \
  287. TRF7970A_IRQ_STATUS_CRC_ERROR)
  288. #define TRF7970A_RSSI_OSC_STATUS_RSSI_MASK (BIT(2) | BIT(1) | BIT(0))
  289. #define TRF7970A_RSSI_OSC_STATUS_RSSI_X_MASK (BIT(5) | BIT(4) | BIT(3))
  290. #define TRF7970A_RSSI_OSC_STATUS_RSSI_OSC_OK BIT(6)
  291. #define TRF7970A_SPECIAL_FCN_REG1_COL_7_6 BIT(0)
  292. #define TRF7970A_SPECIAL_FCN_REG1_14_ANTICOLL BIT(1)
  293. #define TRF7970A_SPECIAL_FCN_REG1_4_BIT_RX BIT(2)
  294. #define TRF7970A_SPECIAL_FCN_REG1_SP_DIR_MODE BIT(3)
  295. #define TRF7970A_SPECIAL_FCN_REG1_NEXT_SLOT_37US BIT(4)
  296. #define TRF7970A_SPECIAL_FCN_REG1_PAR43 BIT(5)
  297. #define TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLH_124 (0x0 << 2)
  298. #define TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLH_120 (0x1 << 2)
  299. #define TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLH_112 (0x2 << 2)
  300. #define TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLH_96 (0x3 << 2)
  301. #define TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLL_4 0x0
  302. #define TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLL_8 0x1
  303. #define TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLL_16 0x2
  304. #define TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLL_32 0x3
  305. #define TRF7970A_NFC_LOW_FIELD_LEVEL_RFDET(v) ((v) & 0x07)
  306. #define TRF7970A_NFC_LOW_FIELD_LEVEL_CLEX_DIS BIT(7)
  307. #define TRF7970A_NFC_TARGET_LEVEL_RFDET(v) ((v) & 0x07)
  308. #define TRF7970A_NFC_TARGET_LEVEL_HI_RF BIT(3)
  309. #define TRF7970A_NFC_TARGET_LEVEL_SDD_EN BIT(5)
  310. #define TRF7970A_NFC_TARGET_LEVEL_LD_S_4BYTES (0x0 << 6)
  311. #define TRF7970A_NFC_TARGET_LEVEL_LD_S_7BYTES (0x1 << 6)
  312. #define TRF7970A_NFC_TARGET_LEVEL_LD_S_10BYTES (0x2 << 6)
  313. #define TRF79070A_NFC_TARGET_PROTOCOL_NFCBR_106 BIT(0)
  314. #define TRF79070A_NFC_TARGET_PROTOCOL_NFCBR_212 BIT(1)
  315. #define TRF79070A_NFC_TARGET_PROTOCOL_NFCBR_424 (BIT(0) | BIT(1))
  316. #define TRF79070A_NFC_TARGET_PROTOCOL_PAS_14443B BIT(2)
  317. #define TRF79070A_NFC_TARGET_PROTOCOL_PAS_106 BIT(3)
  318. #define TRF79070A_NFC_TARGET_PROTOCOL_FELICA BIT(4)
  319. #define TRF79070A_NFC_TARGET_PROTOCOL_RF_L BIT(6)
  320. #define TRF79070A_NFC_TARGET_PROTOCOL_RF_H BIT(7)
  321. #define TRF79070A_NFC_TARGET_PROTOCOL_106A \
  322. (TRF79070A_NFC_TARGET_PROTOCOL_RF_H | \
  323. TRF79070A_NFC_TARGET_PROTOCOL_RF_L | \
  324. TRF79070A_NFC_TARGET_PROTOCOL_PAS_106 | \
  325. TRF79070A_NFC_TARGET_PROTOCOL_NFCBR_106)
  326. #define TRF79070A_NFC_TARGET_PROTOCOL_106B \
  327. (TRF79070A_NFC_TARGET_PROTOCOL_RF_H | \
  328. TRF79070A_NFC_TARGET_PROTOCOL_RF_L | \
  329. TRF79070A_NFC_TARGET_PROTOCOL_PAS_14443B | \
  330. TRF79070A_NFC_TARGET_PROTOCOL_NFCBR_106)
  331. #define TRF79070A_NFC_TARGET_PROTOCOL_212F \
  332. (TRF79070A_NFC_TARGET_PROTOCOL_RF_H | \
  333. TRF79070A_NFC_TARGET_PROTOCOL_RF_L | \
  334. TRF79070A_NFC_TARGET_PROTOCOL_FELICA | \
  335. TRF79070A_NFC_TARGET_PROTOCOL_NFCBR_212)
  336. #define TRF79070A_NFC_TARGET_PROTOCOL_424F \
  337. (TRF79070A_NFC_TARGET_PROTOCOL_RF_H | \
  338. TRF79070A_NFC_TARGET_PROTOCOL_RF_L | \
  339. TRF79070A_NFC_TARGET_PROTOCOL_FELICA | \
  340. TRF79070A_NFC_TARGET_PROTOCOL_NFCBR_424)
  341. #define TRF7970A_FIFO_STATUS_OVERFLOW BIT(7)
  342. /* NFC (ISO/IEC 14443A) Type 2 Tag commands */
  343. #define NFC_T2T_CMD_READ 0x30
  344. /* ISO 15693 commands codes */
  345. #define ISO15693_CMD_INVENTORY 0x01
  346. #define ISO15693_CMD_READ_SINGLE_BLOCK 0x20
  347. #define ISO15693_CMD_WRITE_SINGLE_BLOCK 0x21
  348. #define ISO15693_CMD_LOCK_BLOCK 0x22
  349. #define ISO15693_CMD_READ_MULTIPLE_BLOCK 0x23
  350. #define ISO15693_CMD_WRITE_MULTIPLE_BLOCK 0x24
  351. #define ISO15693_CMD_SELECT 0x25
  352. #define ISO15693_CMD_RESET_TO_READY 0x26
  353. #define ISO15693_CMD_WRITE_AFI 0x27
  354. #define ISO15693_CMD_LOCK_AFI 0x28
  355. #define ISO15693_CMD_WRITE_DSFID 0x29
  356. #define ISO15693_CMD_LOCK_DSFID 0x2a
  357. #define ISO15693_CMD_GET_SYSTEM_INFO 0x2b
  358. #define ISO15693_CMD_GET_MULTIPLE_BLOCK_SECURITY_STATUS 0x2c
  359. /* ISO 15693 request and response flags */
  360. #define ISO15693_REQ_FLAG_SUB_CARRIER BIT(0)
  361. #define ISO15693_REQ_FLAG_DATA_RATE BIT(1)
  362. #define ISO15693_REQ_FLAG_INVENTORY BIT(2)
  363. #define ISO15693_REQ_FLAG_PROTOCOL_EXT BIT(3)
  364. #define ISO15693_REQ_FLAG_SELECT BIT(4)
  365. #define ISO15693_REQ_FLAG_AFI BIT(4)
  366. #define ISO15693_REQ_FLAG_ADDRESS BIT(5)
  367. #define ISO15693_REQ_FLAG_NB_SLOTS BIT(5)
  368. #define ISO15693_REQ_FLAG_OPTION BIT(6)
  369. #define ISO15693_REQ_FLAG_SPEED_MASK \
  370. (ISO15693_REQ_FLAG_SUB_CARRIER | ISO15693_REQ_FLAG_DATA_RATE)
  371. enum trf7970a_state {
  372. TRF7970A_ST_PWR_OFF,
  373. TRF7970A_ST_RF_OFF,
  374. TRF7970A_ST_IDLE,
  375. TRF7970A_ST_IDLE_RX_BLOCKED,
  376. TRF7970A_ST_WAIT_FOR_TX_FIFO,
  377. TRF7970A_ST_WAIT_FOR_RX_DATA,
  378. TRF7970A_ST_WAIT_FOR_RX_DATA_CONT,
  379. TRF7970A_ST_WAIT_TO_ISSUE_EOF,
  380. TRF7970A_ST_LISTENING,
  381. TRF7970A_ST_LISTENING_MD,
  382. TRF7970A_ST_MAX
  383. };
  384. struct trf7970a {
  385. enum trf7970a_state state;
  386. struct device *dev;
  387. struct spi_device *spi;
  388. struct regulator *vin_regulator;
  389. struct regulator *vddio_regulator;
  390. struct nfc_digital_dev *ddev;
  391. u32 quirks;
  392. bool is_initiator;
  393. bool aborting;
  394. struct sk_buff *tx_skb;
  395. struct sk_buff *rx_skb;
  396. nfc_digital_cmd_complete_t cb;
  397. void *cb_arg;
  398. u8 chip_status_ctrl;
  399. u8 iso_ctrl;
  400. u8 iso_ctrl_tech;
  401. u8 modulator_sys_clk_ctrl;
  402. u8 special_fcn_reg1;
  403. u8 io_ctrl;
  404. unsigned int guard_time;
  405. int technology;
  406. int framing;
  407. u8 md_rf_tech;
  408. u8 tx_cmd;
  409. bool issue_eof;
  410. struct gpio_desc *en_gpiod;
  411. struct gpio_desc *en2_gpiod;
  412. struct mutex lock;
  413. unsigned int timeout;
  414. bool ignore_timeout;
  415. struct delayed_work timeout_work;
  416. };
  417. static int trf7970a_cmd(struct trf7970a *trf, u8 opcode)
  418. {
  419. u8 cmd = TRF7970A_CMD_BIT_CTRL | TRF7970A_CMD_BIT_OPCODE(opcode);
  420. int ret;
  421. dev_dbg(trf->dev, "cmd: 0x%x\n", cmd);
  422. ret = spi_write(trf->spi, &cmd, 1);
  423. if (ret)
  424. dev_err(trf->dev, "%s - cmd: 0x%x, ret: %d\n", __func__, cmd,
  425. ret);
  426. return ret;
  427. }
  428. static int trf7970a_read(struct trf7970a *trf, u8 reg, u8 *val)
  429. {
  430. u8 addr = TRF7970A_CMD_BIT_RW | reg;
  431. int ret;
  432. ret = spi_write_then_read(trf->spi, &addr, 1, val, 1);
  433. if (ret)
  434. dev_err(trf->dev, "%s - addr: 0x%x, ret: %d\n", __func__, addr,
  435. ret);
  436. dev_dbg(trf->dev, "read(0x%x): 0x%x\n", addr, *val);
  437. return ret;
  438. }
  439. static int trf7970a_read_cont(struct trf7970a *trf, u8 reg, u8 *buf,
  440. size_t len)
  441. {
  442. u8 addr = reg | TRF7970A_CMD_BIT_RW | TRF7970A_CMD_BIT_CONTINUOUS;
  443. struct spi_transfer t[2];
  444. struct spi_message m;
  445. int ret;
  446. dev_dbg(trf->dev, "read_cont(0x%x, %zd)\n", addr, len);
  447. spi_message_init(&m);
  448. memset(&t, 0, sizeof(t));
  449. t[0].tx_buf = &addr;
  450. t[0].len = sizeof(addr);
  451. spi_message_add_tail(&t[0], &m);
  452. t[1].rx_buf = buf;
  453. t[1].len = len;
  454. spi_message_add_tail(&t[1], &m);
  455. ret = spi_sync(trf->spi, &m);
  456. if (ret)
  457. dev_err(trf->dev, "%s - addr: 0x%x, ret: %d\n", __func__, addr,
  458. ret);
  459. return ret;
  460. }
  461. static int trf7970a_write(struct trf7970a *trf, u8 reg, u8 val)
  462. {
  463. u8 buf[2] = { reg, val };
  464. int ret;
  465. dev_dbg(trf->dev, "write(0x%x): 0x%x\n", reg, val);
  466. ret = spi_write(trf->spi, buf, 2);
  467. if (ret)
  468. dev_err(trf->dev, "%s - write: 0x%x 0x%x, ret: %d\n", __func__,
  469. buf[0], buf[1], ret);
  470. return ret;
  471. }
  472. static int trf7970a_read_irqstatus(struct trf7970a *trf, u8 *status)
  473. {
  474. int ret;
  475. u8 buf[2];
  476. u8 addr;
  477. addr = TRF7970A_IRQ_STATUS | TRF7970A_CMD_BIT_RW;
  478. if (trf->quirks & TRF7970A_QUIRK_IRQ_STATUS_READ) {
  479. addr |= TRF7970A_CMD_BIT_CONTINUOUS;
  480. ret = spi_write_then_read(trf->spi, &addr, 1, buf, 2);
  481. } else {
  482. ret = spi_write_then_read(trf->spi, &addr, 1, buf, 1);
  483. }
  484. if (ret)
  485. dev_err(trf->dev, "%s - irqstatus: Status read failed: %d\n",
  486. __func__, ret);
  487. else
  488. *status = buf[0];
  489. return ret;
  490. }
  491. static int trf7970a_read_target_proto(struct trf7970a *trf, u8 *target_proto)
  492. {
  493. int ret;
  494. u8 buf[2];
  495. u8 addr;
  496. addr = TRF79070A_NFC_TARGET_PROTOCOL | TRF7970A_CMD_BIT_RW |
  497. TRF7970A_CMD_BIT_CONTINUOUS;
  498. ret = spi_write_then_read(trf->spi, &addr, 1, buf, 2);
  499. if (ret)
  500. dev_err(trf->dev, "%s - target_proto: Read failed: %d\n",
  501. __func__, ret);
  502. else
  503. *target_proto = buf[0];
  504. return ret;
  505. }
  506. static int trf7970a_mode_detect(struct trf7970a *trf, u8 *rf_tech)
  507. {
  508. int ret;
  509. u8 target_proto, tech;
  510. ret = trf7970a_read_target_proto(trf, &target_proto);
  511. if (ret)
  512. return ret;
  513. switch (target_proto) {
  514. case TRF79070A_NFC_TARGET_PROTOCOL_106A:
  515. tech = NFC_DIGITAL_RF_TECH_106A;
  516. break;
  517. case TRF79070A_NFC_TARGET_PROTOCOL_106B:
  518. tech = NFC_DIGITAL_RF_TECH_106B;
  519. break;
  520. case TRF79070A_NFC_TARGET_PROTOCOL_212F:
  521. tech = NFC_DIGITAL_RF_TECH_212F;
  522. break;
  523. case TRF79070A_NFC_TARGET_PROTOCOL_424F:
  524. tech = NFC_DIGITAL_RF_TECH_424F;
  525. break;
  526. default:
  527. dev_dbg(trf->dev, "%s - mode_detect: target_proto: 0x%x\n",
  528. __func__, target_proto);
  529. return -EIO;
  530. }
  531. *rf_tech = tech;
  532. return ret;
  533. }
  534. static void trf7970a_send_upstream(struct trf7970a *trf)
  535. {
  536. dev_kfree_skb_any(trf->tx_skb);
  537. trf->tx_skb = NULL;
  538. if (trf->rx_skb && !IS_ERR(trf->rx_skb) && !trf->aborting)
  539. print_hex_dump_debug("trf7970a rx data: ", DUMP_PREFIX_NONE,
  540. 16, 1, trf->rx_skb->data, trf->rx_skb->len,
  541. false);
  542. trf->state = TRF7970A_ST_IDLE;
  543. if (trf->aborting) {
  544. dev_dbg(trf->dev, "Abort process complete\n");
  545. if (!IS_ERR(trf->rx_skb)) {
  546. kfree_skb(trf->rx_skb);
  547. trf->rx_skb = ERR_PTR(-ECANCELED);
  548. }
  549. trf->aborting = false;
  550. }
  551. trf->cb(trf->ddev, trf->cb_arg, trf->rx_skb);
  552. trf->rx_skb = NULL;
  553. }
  554. static void trf7970a_send_err_upstream(struct trf7970a *trf, int errno)
  555. {
  556. dev_dbg(trf->dev, "Error - state: %d, errno: %d\n", trf->state, errno);
  557. cancel_delayed_work(&trf->timeout_work);
  558. kfree_skb(trf->rx_skb);
  559. trf->rx_skb = ERR_PTR(errno);
  560. trf7970a_send_upstream(trf);
  561. }
  562. static int trf7970a_transmit(struct trf7970a *trf, struct sk_buff *skb,
  563. unsigned int len, const u8 *prefix,
  564. unsigned int prefix_len)
  565. {
  566. struct spi_transfer t[2];
  567. struct spi_message m;
  568. unsigned int timeout;
  569. int ret;
  570. print_hex_dump_debug("trf7970a tx data: ", DUMP_PREFIX_NONE,
  571. 16, 1, skb->data, len, false);
  572. spi_message_init(&m);
  573. memset(&t, 0, sizeof(t));
  574. t[0].tx_buf = prefix;
  575. t[0].len = prefix_len;
  576. spi_message_add_tail(&t[0], &m);
  577. t[1].tx_buf = skb->data;
  578. t[1].len = len;
  579. spi_message_add_tail(&t[1], &m);
  580. ret = spi_sync(trf->spi, &m);
  581. if (ret) {
  582. dev_err(trf->dev, "%s - Can't send tx data: %d\n", __func__,
  583. ret);
  584. return ret;
  585. }
  586. skb_pull(skb, len);
  587. if (skb->len > 0) {
  588. trf->state = TRF7970A_ST_WAIT_FOR_TX_FIFO;
  589. timeout = TRF7970A_WAIT_FOR_FIFO_DRAIN_TIMEOUT;
  590. } else {
  591. if (trf->issue_eof) {
  592. trf->state = TRF7970A_ST_WAIT_TO_ISSUE_EOF;
  593. timeout = TRF7970A_WAIT_TO_ISSUE_ISO15693_EOF;
  594. } else {
  595. trf->state = TRF7970A_ST_WAIT_FOR_RX_DATA;
  596. if (!trf->timeout)
  597. timeout = TRF7970A_WAIT_FOR_TX_IRQ;
  598. else
  599. timeout = trf->timeout;
  600. }
  601. }
  602. dev_dbg(trf->dev, "Setting timeout for %d ms, state: %d\n", timeout,
  603. trf->state);
  604. schedule_delayed_work(&trf->timeout_work, msecs_to_jiffies(timeout));
  605. return 0;
  606. }
  607. static void trf7970a_fill_fifo(struct trf7970a *trf)
  608. {
  609. struct sk_buff *skb = trf->tx_skb;
  610. unsigned int len;
  611. int ret;
  612. u8 fifo_bytes;
  613. u8 prefix;
  614. ret = trf7970a_read(trf, TRF7970A_FIFO_STATUS, &fifo_bytes);
  615. if (ret) {
  616. trf7970a_send_err_upstream(trf, ret);
  617. return;
  618. }
  619. dev_dbg(trf->dev, "Filling FIFO - fifo_bytes: 0x%x\n", fifo_bytes);
  620. fifo_bytes &= ~TRF7970A_FIFO_STATUS_OVERFLOW;
  621. /* Calculate how much more data can be written to the fifo */
  622. len = TRF7970A_FIFO_SIZE - fifo_bytes;
  623. if (!len) {
  624. schedule_delayed_work(&trf->timeout_work,
  625. msecs_to_jiffies(TRF7970A_WAIT_FOR_FIFO_DRAIN_TIMEOUT));
  626. return;
  627. }
  628. len = min(skb->len, len);
  629. prefix = TRF7970A_CMD_BIT_CONTINUOUS | TRF7970A_FIFO_IO_REGISTER;
  630. ret = trf7970a_transmit(trf, skb, len, &prefix, sizeof(prefix));
  631. if (ret)
  632. trf7970a_send_err_upstream(trf, ret);
  633. }
  634. static void trf7970a_drain_fifo(struct trf7970a *trf, u8 status)
  635. {
  636. struct sk_buff *skb = trf->rx_skb;
  637. int ret;
  638. u8 fifo_bytes;
  639. if (status & TRF7970A_IRQ_STATUS_ERROR) {
  640. trf7970a_send_err_upstream(trf, -EIO);
  641. return;
  642. }
  643. ret = trf7970a_read(trf, TRF7970A_FIFO_STATUS, &fifo_bytes);
  644. if (ret) {
  645. trf7970a_send_err_upstream(trf, ret);
  646. return;
  647. }
  648. dev_dbg(trf->dev, "Draining FIFO - fifo_bytes: 0x%x\n", fifo_bytes);
  649. fifo_bytes &= ~TRF7970A_FIFO_STATUS_OVERFLOW;
  650. if (!fifo_bytes)
  651. goto no_rx_data;
  652. if (fifo_bytes > skb_tailroom(skb)) {
  653. skb = skb_copy_expand(skb, skb_headroom(skb),
  654. max_t(int, fifo_bytes,
  655. TRF7970A_RX_SKB_ALLOC_SIZE),
  656. GFP_KERNEL);
  657. if (!skb) {
  658. trf7970a_send_err_upstream(trf, -ENOMEM);
  659. return;
  660. }
  661. kfree_skb(trf->rx_skb);
  662. trf->rx_skb = skb;
  663. }
  664. ret = trf7970a_read_cont(trf, TRF7970A_FIFO_IO_REGISTER,
  665. skb_put(skb, fifo_bytes), fifo_bytes);
  666. if (ret) {
  667. trf7970a_send_err_upstream(trf, ret);
  668. return;
  669. }
  670. /* If received Type 2 ACK/NACK, shift right 4 bits and pass up */
  671. if ((trf->framing == NFC_DIGITAL_FRAMING_NFCA_T2T) && (skb->len == 1) &&
  672. (trf->special_fcn_reg1 == TRF7970A_SPECIAL_FCN_REG1_4_BIT_RX)) {
  673. skb->data[0] >>= 4;
  674. status = TRF7970A_IRQ_STATUS_SRX;
  675. } else {
  676. trf->state = TRF7970A_ST_WAIT_FOR_RX_DATA_CONT;
  677. ret = trf7970a_read(trf, TRF7970A_FIFO_STATUS, &fifo_bytes);
  678. if (ret) {
  679. trf7970a_send_err_upstream(trf, ret);
  680. return;
  681. }
  682. fifo_bytes &= ~TRF7970A_FIFO_STATUS_OVERFLOW;
  683. /* If there are bytes in the FIFO, set status to '0' so
  684. * the if stmt below doesn't fire and the driver will wait
  685. * for the trf7970a to generate another RX interrupt.
  686. */
  687. if (fifo_bytes)
  688. status = 0;
  689. }
  690. no_rx_data:
  691. if (status == TRF7970A_IRQ_STATUS_SRX) { /* Receive complete */
  692. trf7970a_send_upstream(trf);
  693. return;
  694. }
  695. dev_dbg(trf->dev, "Setting timeout for %d ms\n",
  696. TRF7970A_WAIT_FOR_RX_DATA_TIMEOUT);
  697. schedule_delayed_work(&trf->timeout_work,
  698. msecs_to_jiffies(TRF7970A_WAIT_FOR_RX_DATA_TIMEOUT));
  699. }
  700. static irqreturn_t trf7970a_irq(int irq, void *dev_id)
  701. {
  702. struct trf7970a *trf = dev_id;
  703. int ret;
  704. u8 status, fifo_bytes, iso_ctrl;
  705. mutex_lock(&trf->lock);
  706. if (trf->state == TRF7970A_ST_RF_OFF) {
  707. mutex_unlock(&trf->lock);
  708. return IRQ_NONE;
  709. }
  710. ret = trf7970a_read_irqstatus(trf, &status);
  711. if (ret) {
  712. mutex_unlock(&trf->lock);
  713. return IRQ_NONE;
  714. }
  715. dev_dbg(trf->dev, "IRQ - state: %d, status: 0x%x\n", trf->state,
  716. status);
  717. if (!status) {
  718. mutex_unlock(&trf->lock);
  719. return IRQ_NONE;
  720. }
  721. switch (trf->state) {
  722. case TRF7970A_ST_IDLE:
  723. case TRF7970A_ST_IDLE_RX_BLOCKED:
  724. /* If initiator and getting interrupts caused by RF noise,
  725. * turn off the receiver to avoid unnecessary interrupts.
  726. * It will be turned back on in trf7970a_send_cmd() when
  727. * the next command is issued.
  728. */
  729. if (trf->is_initiator && (status & TRF7970A_IRQ_STATUS_ERROR)) {
  730. trf7970a_cmd(trf, TRF7970A_CMD_BLOCK_RX);
  731. trf->state = TRF7970A_ST_IDLE_RX_BLOCKED;
  732. }
  733. trf7970a_cmd(trf, TRF7970A_CMD_FIFO_RESET);
  734. break;
  735. case TRF7970A_ST_WAIT_FOR_TX_FIFO:
  736. if (status & TRF7970A_IRQ_STATUS_TX) {
  737. trf->ignore_timeout =
  738. !cancel_delayed_work(&trf->timeout_work);
  739. trf7970a_fill_fifo(trf);
  740. } else {
  741. trf7970a_send_err_upstream(trf, -EIO);
  742. }
  743. break;
  744. case TRF7970A_ST_WAIT_FOR_RX_DATA:
  745. case TRF7970A_ST_WAIT_FOR_RX_DATA_CONT:
  746. if (status & TRF7970A_IRQ_STATUS_SRX) {
  747. trf->ignore_timeout =
  748. !cancel_delayed_work(&trf->timeout_work);
  749. trf7970a_drain_fifo(trf, status);
  750. } else if (status & TRF7970A_IRQ_STATUS_FIFO) {
  751. ret = trf7970a_read(trf, TRF7970A_FIFO_STATUS,
  752. &fifo_bytes);
  753. fifo_bytes &= ~TRF7970A_FIFO_STATUS_OVERFLOW;
  754. if (ret)
  755. trf7970a_send_err_upstream(trf, ret);
  756. else if (!fifo_bytes)
  757. trf7970a_cmd(trf, TRF7970A_CMD_FIFO_RESET);
  758. } else if ((status == TRF7970A_IRQ_STATUS_TX) ||
  759. (!trf->is_initiator &&
  760. (status == (TRF7970A_IRQ_STATUS_TX |
  761. TRF7970A_IRQ_STATUS_NFC_RF)))) {
  762. trf7970a_cmd(trf, TRF7970A_CMD_FIFO_RESET);
  763. if (!trf->timeout) {
  764. trf->ignore_timeout =
  765. !cancel_delayed_work(&trf->timeout_work);
  766. trf->rx_skb = ERR_PTR(0);
  767. trf7970a_send_upstream(trf);
  768. break;
  769. }
  770. if (trf->is_initiator)
  771. break;
  772. iso_ctrl = trf->iso_ctrl;
  773. switch (trf->framing) {
  774. case NFC_DIGITAL_FRAMING_NFCA_STANDARD:
  775. trf->tx_cmd = TRF7970A_CMD_TRANSMIT_NO_CRC;
  776. iso_ctrl |= TRF7970A_ISO_CTRL_RX_CRC_N;
  777. trf->iso_ctrl = 0xff; /* Force ISO_CTRL write */
  778. break;
  779. case NFC_DIGITAL_FRAMING_NFCA_STANDARD_WITH_CRC_A:
  780. trf->tx_cmd = TRF7970A_CMD_TRANSMIT;
  781. iso_ctrl &= ~TRF7970A_ISO_CTRL_RX_CRC_N;
  782. trf->iso_ctrl = 0xff; /* Force ISO_CTRL write */
  783. break;
  784. case NFC_DIGITAL_FRAMING_NFCA_ANTICOL_COMPLETE:
  785. ret = trf7970a_write(trf,
  786. TRF7970A_SPECIAL_FCN_REG1,
  787. TRF7970A_SPECIAL_FCN_REG1_14_ANTICOLL);
  788. if (ret)
  789. goto err_unlock_exit;
  790. trf->special_fcn_reg1 =
  791. TRF7970A_SPECIAL_FCN_REG1_14_ANTICOLL;
  792. break;
  793. default:
  794. break;
  795. }
  796. if (iso_ctrl != trf->iso_ctrl) {
  797. ret = trf7970a_write(trf, TRF7970A_ISO_CTRL,
  798. iso_ctrl);
  799. if (ret)
  800. goto err_unlock_exit;
  801. trf->iso_ctrl = iso_ctrl;
  802. }
  803. } else {
  804. trf7970a_send_err_upstream(trf, -EIO);
  805. }
  806. break;
  807. case TRF7970A_ST_WAIT_TO_ISSUE_EOF:
  808. if (status != TRF7970A_IRQ_STATUS_TX)
  809. trf7970a_send_err_upstream(trf, -EIO);
  810. break;
  811. case TRF7970A_ST_LISTENING:
  812. if (status & TRF7970A_IRQ_STATUS_SRX) {
  813. trf->ignore_timeout =
  814. !cancel_delayed_work(&trf->timeout_work);
  815. trf7970a_drain_fifo(trf, status);
  816. } else if (!(status & TRF7970A_IRQ_STATUS_NFC_RF)) {
  817. trf7970a_send_err_upstream(trf, -EIO);
  818. }
  819. break;
  820. case TRF7970A_ST_LISTENING_MD:
  821. if (status & TRF7970A_IRQ_STATUS_SRX) {
  822. trf->ignore_timeout =
  823. !cancel_delayed_work(&trf->timeout_work);
  824. ret = trf7970a_mode_detect(trf, &trf->md_rf_tech);
  825. if (ret) {
  826. trf7970a_send_err_upstream(trf, ret);
  827. } else {
  828. trf->state = TRF7970A_ST_LISTENING;
  829. trf7970a_drain_fifo(trf, status);
  830. }
  831. } else if (!(status & TRF7970A_IRQ_STATUS_NFC_RF)) {
  832. trf7970a_send_err_upstream(trf, -EIO);
  833. }
  834. break;
  835. default:
  836. dev_err(trf->dev, "%s - Driver in invalid state: %d\n",
  837. __func__, trf->state);
  838. }
  839. err_unlock_exit:
  840. mutex_unlock(&trf->lock);
  841. return IRQ_HANDLED;
  842. }
  843. static void trf7970a_issue_eof(struct trf7970a *trf)
  844. {
  845. int ret;
  846. dev_dbg(trf->dev, "Issuing EOF\n");
  847. ret = trf7970a_cmd(trf, TRF7970A_CMD_FIFO_RESET);
  848. if (ret)
  849. trf7970a_send_err_upstream(trf, ret);
  850. ret = trf7970a_cmd(trf, TRF7970A_CMD_EOF);
  851. if (ret)
  852. trf7970a_send_err_upstream(trf, ret);
  853. trf->state = TRF7970A_ST_WAIT_FOR_RX_DATA;
  854. dev_dbg(trf->dev, "Setting timeout for %d ms, state: %d\n",
  855. trf->timeout, trf->state);
  856. schedule_delayed_work(&trf->timeout_work,
  857. msecs_to_jiffies(trf->timeout));
  858. }
  859. static void trf7970a_timeout_work_handler(struct work_struct *work)
  860. {
  861. struct trf7970a *trf = container_of(work, struct trf7970a,
  862. timeout_work.work);
  863. dev_dbg(trf->dev, "Timeout - state: %d, ignore_timeout: %d\n",
  864. trf->state, trf->ignore_timeout);
  865. mutex_lock(&trf->lock);
  866. if (trf->ignore_timeout)
  867. trf->ignore_timeout = false;
  868. else if (trf->state == TRF7970A_ST_WAIT_FOR_RX_DATA_CONT)
  869. trf7970a_drain_fifo(trf, TRF7970A_IRQ_STATUS_SRX);
  870. else if (trf->state == TRF7970A_ST_WAIT_TO_ISSUE_EOF)
  871. trf7970a_issue_eof(trf);
  872. else
  873. trf7970a_send_err_upstream(trf, -ETIMEDOUT);
  874. mutex_unlock(&trf->lock);
  875. }
  876. static int trf7970a_init(struct trf7970a *trf)
  877. {
  878. int ret;
  879. dev_dbg(trf->dev, "Initializing device - state: %d\n", trf->state);
  880. ret = trf7970a_cmd(trf, TRF7970A_CMD_SOFT_INIT);
  881. if (ret)
  882. goto err_out;
  883. ret = trf7970a_cmd(trf, TRF7970A_CMD_IDLE);
  884. if (ret)
  885. goto err_out;
  886. ret = trf7970a_write(trf, TRF7970A_REG_IO_CTRL,
  887. trf->io_ctrl | TRF7970A_REG_IO_CTRL_VRS(0x1));
  888. if (ret)
  889. goto err_out;
  890. ret = trf7970a_write(trf, TRF7970A_NFC_TARGET_LEVEL, 0);
  891. if (ret)
  892. goto err_out;
  893. usleep_range(1000, 2000);
  894. trf->chip_status_ctrl &= ~TRF7970A_CHIP_STATUS_RF_ON;
  895. ret = trf7970a_write(trf, TRF7970A_MODULATOR_SYS_CLK_CTRL,
  896. trf->modulator_sys_clk_ctrl);
  897. if (ret)
  898. goto err_out;
  899. ret = trf7970a_write(trf, TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS,
  900. TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLH_96 |
  901. TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLL_32);
  902. if (ret)
  903. goto err_out;
  904. ret = trf7970a_write(trf, TRF7970A_SPECIAL_FCN_REG1, 0);
  905. if (ret)
  906. goto err_out;
  907. trf->special_fcn_reg1 = 0;
  908. trf->iso_ctrl = 0xff;
  909. return 0;
  910. err_out:
  911. dev_dbg(trf->dev, "Couldn't init device: %d\n", ret);
  912. return ret;
  913. }
  914. static void trf7970a_switch_rf_off(struct trf7970a *trf)
  915. {
  916. if ((trf->state == TRF7970A_ST_PWR_OFF) ||
  917. (trf->state == TRF7970A_ST_RF_OFF))
  918. return;
  919. dev_dbg(trf->dev, "Switching rf off\n");
  920. trf->chip_status_ctrl &= ~TRF7970A_CHIP_STATUS_RF_ON;
  921. trf7970a_write(trf, TRF7970A_CHIP_STATUS_CTRL, trf->chip_status_ctrl);
  922. trf->aborting = false;
  923. trf->state = TRF7970A_ST_RF_OFF;
  924. pm_runtime_mark_last_busy(trf->dev);
  925. pm_runtime_put_autosuspend(trf->dev);
  926. }
  927. static int trf7970a_switch_rf_on(struct trf7970a *trf)
  928. {
  929. int ret;
  930. dev_dbg(trf->dev, "Switching rf on\n");
  931. pm_runtime_get_sync(trf->dev);
  932. if (trf->state != TRF7970A_ST_RF_OFF) { /* Power on, RF off */
  933. dev_err(trf->dev, "%s - Incorrect state: %d\n", __func__,
  934. trf->state);
  935. return -EINVAL;
  936. }
  937. ret = trf7970a_init(trf);
  938. if (ret) {
  939. dev_err(trf->dev, "%s - Can't initialize: %d\n", __func__, ret);
  940. return ret;
  941. }
  942. trf->state = TRF7970A_ST_IDLE;
  943. return 0;
  944. }
  945. static int trf7970a_switch_rf(struct nfc_digital_dev *ddev, bool on)
  946. {
  947. struct trf7970a *trf = nfc_digital_get_drvdata(ddev);
  948. int ret = 0;
  949. dev_dbg(trf->dev, "Switching RF - state: %d, on: %d\n", trf->state, on);
  950. mutex_lock(&trf->lock);
  951. if (on) {
  952. switch (trf->state) {
  953. case TRF7970A_ST_PWR_OFF:
  954. case TRF7970A_ST_RF_OFF:
  955. ret = trf7970a_switch_rf_on(trf);
  956. break;
  957. case TRF7970A_ST_IDLE:
  958. case TRF7970A_ST_IDLE_RX_BLOCKED:
  959. break;
  960. default:
  961. dev_err(trf->dev, "%s - Invalid request: %d %d\n",
  962. __func__, trf->state, on);
  963. trf7970a_switch_rf_off(trf);
  964. ret = -EINVAL;
  965. }
  966. } else {
  967. switch (trf->state) {
  968. case TRF7970A_ST_PWR_OFF:
  969. case TRF7970A_ST_RF_OFF:
  970. break;
  971. default:
  972. dev_err(trf->dev, "%s - Invalid request: %d %d\n",
  973. __func__, trf->state, on);
  974. ret = -EINVAL;
  975. fallthrough;
  976. case TRF7970A_ST_IDLE:
  977. case TRF7970A_ST_IDLE_RX_BLOCKED:
  978. case TRF7970A_ST_WAIT_FOR_RX_DATA:
  979. case TRF7970A_ST_WAIT_FOR_RX_DATA_CONT:
  980. trf7970a_switch_rf_off(trf);
  981. }
  982. }
  983. mutex_unlock(&trf->lock);
  984. return ret;
  985. }
  986. static int trf7970a_in_config_rf_tech(struct trf7970a *trf, int tech)
  987. {
  988. int ret = 0;
  989. dev_dbg(trf->dev, "rf technology: %d\n", tech);
  990. switch (tech) {
  991. case NFC_DIGITAL_RF_TECH_106A:
  992. trf->iso_ctrl_tech = TRF7970A_ISO_CTRL_14443A_106;
  993. trf->modulator_sys_clk_ctrl =
  994. (trf->modulator_sys_clk_ctrl & 0xf8) |
  995. TRF7970A_MODULATOR_DEPTH_OOK;
  996. trf->guard_time = TRF7970A_GUARD_TIME_NFCA;
  997. break;
  998. case NFC_DIGITAL_RF_TECH_106B:
  999. trf->iso_ctrl_tech = TRF7970A_ISO_CTRL_14443B_106;
  1000. trf->modulator_sys_clk_ctrl =
  1001. (trf->modulator_sys_clk_ctrl & 0xf8) |
  1002. TRF7970A_MODULATOR_DEPTH_ASK10;
  1003. trf->guard_time = TRF7970A_GUARD_TIME_NFCB;
  1004. break;
  1005. case NFC_DIGITAL_RF_TECH_212F:
  1006. trf->iso_ctrl_tech = TRF7970A_ISO_CTRL_FELICA_212;
  1007. trf->modulator_sys_clk_ctrl =
  1008. (trf->modulator_sys_clk_ctrl & 0xf8) |
  1009. TRF7970A_MODULATOR_DEPTH_ASK10;
  1010. trf->guard_time = TRF7970A_GUARD_TIME_NFCF;
  1011. break;
  1012. case NFC_DIGITAL_RF_TECH_424F:
  1013. trf->iso_ctrl_tech = TRF7970A_ISO_CTRL_FELICA_424;
  1014. trf->modulator_sys_clk_ctrl =
  1015. (trf->modulator_sys_clk_ctrl & 0xf8) |
  1016. TRF7970A_MODULATOR_DEPTH_ASK10;
  1017. trf->guard_time = TRF7970A_GUARD_TIME_NFCF;
  1018. break;
  1019. case NFC_DIGITAL_RF_TECH_ISO15693:
  1020. trf->iso_ctrl_tech = TRF7970A_ISO_CTRL_15693_SGL_1OF4_2648;
  1021. trf->modulator_sys_clk_ctrl =
  1022. (trf->modulator_sys_clk_ctrl & 0xf8) |
  1023. TRF7970A_MODULATOR_DEPTH_OOK;
  1024. trf->guard_time = TRF7970A_GUARD_TIME_15693;
  1025. break;
  1026. default:
  1027. dev_dbg(trf->dev, "Unsupported rf technology: %d\n", tech);
  1028. return -EINVAL;
  1029. }
  1030. trf->technology = tech;
  1031. /* If in initiator mode and not changing the RF tech due to a
  1032. * PSL sequence (indicated by 'trf->iso_ctrl == 0xff' from
  1033. * trf7970a_init()), clear the NFC Target Detection Level register
  1034. * due to erratum.
  1035. */
  1036. if (trf->iso_ctrl == 0xff)
  1037. ret = trf7970a_write(trf, TRF7970A_NFC_TARGET_LEVEL, 0);
  1038. return ret;
  1039. }
  1040. static int trf7970a_is_rf_field(struct trf7970a *trf, bool *is_rf_field)
  1041. {
  1042. int ret;
  1043. u8 rssi;
  1044. ret = trf7970a_write(trf, TRF7970A_CHIP_STATUS_CTRL,
  1045. trf->chip_status_ctrl |
  1046. TRF7970A_CHIP_STATUS_REC_ON);
  1047. if (ret)
  1048. return ret;
  1049. ret = trf7970a_cmd(trf, TRF7970A_CMD_TEST_EXT_RF);
  1050. if (ret)
  1051. return ret;
  1052. usleep_range(50, 60);
  1053. ret = trf7970a_read(trf, TRF7970A_RSSI_OSC_STATUS, &rssi);
  1054. if (ret)
  1055. return ret;
  1056. ret = trf7970a_write(trf, TRF7970A_CHIP_STATUS_CTRL,
  1057. trf->chip_status_ctrl);
  1058. if (ret)
  1059. return ret;
  1060. if (rssi & TRF7970A_RSSI_OSC_STATUS_RSSI_MASK)
  1061. *is_rf_field = true;
  1062. else
  1063. *is_rf_field = false;
  1064. return 0;
  1065. }
  1066. static int trf7970a_in_config_framing(struct trf7970a *trf, int framing)
  1067. {
  1068. u8 iso_ctrl = trf->iso_ctrl_tech;
  1069. bool is_rf_field = false;
  1070. int ret;
  1071. dev_dbg(trf->dev, "framing: %d\n", framing);
  1072. switch (framing) {
  1073. case NFC_DIGITAL_FRAMING_NFCA_SHORT:
  1074. case NFC_DIGITAL_FRAMING_NFCA_STANDARD:
  1075. trf->tx_cmd = TRF7970A_CMD_TRANSMIT_NO_CRC;
  1076. iso_ctrl |= TRF7970A_ISO_CTRL_RX_CRC_N;
  1077. break;
  1078. case NFC_DIGITAL_FRAMING_NFCA_STANDARD_WITH_CRC_A:
  1079. case NFC_DIGITAL_FRAMING_NFCA_T4T:
  1080. case NFC_DIGITAL_FRAMING_NFCB:
  1081. case NFC_DIGITAL_FRAMING_NFCB_T4T:
  1082. case NFC_DIGITAL_FRAMING_NFCF:
  1083. case NFC_DIGITAL_FRAMING_NFCF_T3T:
  1084. case NFC_DIGITAL_FRAMING_ISO15693_INVENTORY:
  1085. case NFC_DIGITAL_FRAMING_ISO15693_T5T:
  1086. case NFC_DIGITAL_FRAMING_NFCA_NFC_DEP:
  1087. case NFC_DIGITAL_FRAMING_NFCF_NFC_DEP:
  1088. trf->tx_cmd = TRF7970A_CMD_TRANSMIT;
  1089. iso_ctrl &= ~TRF7970A_ISO_CTRL_RX_CRC_N;
  1090. break;
  1091. case NFC_DIGITAL_FRAMING_NFCA_T2T:
  1092. trf->tx_cmd = TRF7970A_CMD_TRANSMIT;
  1093. iso_ctrl |= TRF7970A_ISO_CTRL_RX_CRC_N;
  1094. break;
  1095. default:
  1096. dev_dbg(trf->dev, "Unsupported Framing: %d\n", framing);
  1097. return -EINVAL;
  1098. }
  1099. trf->framing = framing;
  1100. if (!(trf->chip_status_ctrl & TRF7970A_CHIP_STATUS_RF_ON)) {
  1101. ret = trf7970a_is_rf_field(trf, &is_rf_field);
  1102. if (ret)
  1103. return ret;
  1104. if (is_rf_field)
  1105. return -EBUSY;
  1106. }
  1107. if (iso_ctrl != trf->iso_ctrl) {
  1108. ret = trf7970a_write(trf, TRF7970A_ISO_CTRL, iso_ctrl);
  1109. if (ret)
  1110. return ret;
  1111. trf->iso_ctrl = iso_ctrl;
  1112. ret = trf7970a_write(trf, TRF7970A_MODULATOR_SYS_CLK_CTRL,
  1113. trf->modulator_sys_clk_ctrl);
  1114. if (ret)
  1115. return ret;
  1116. }
  1117. if (!(trf->chip_status_ctrl & TRF7970A_CHIP_STATUS_RF_ON)) {
  1118. ret = trf7970a_write(trf, TRF7970A_CHIP_STATUS_CTRL,
  1119. trf->chip_status_ctrl |
  1120. TRF7970A_CHIP_STATUS_RF_ON);
  1121. if (ret)
  1122. return ret;
  1123. trf->chip_status_ctrl |= TRF7970A_CHIP_STATUS_RF_ON;
  1124. usleep_range(trf->guard_time, trf->guard_time + 1000);
  1125. }
  1126. return 0;
  1127. }
  1128. static int trf7970a_in_configure_hw(struct nfc_digital_dev *ddev, int type,
  1129. int param)
  1130. {
  1131. struct trf7970a *trf = nfc_digital_get_drvdata(ddev);
  1132. int ret;
  1133. dev_dbg(trf->dev, "Configure hw - type: %d, param: %d\n", type, param);
  1134. mutex_lock(&trf->lock);
  1135. trf->is_initiator = true;
  1136. if ((trf->state == TRF7970A_ST_PWR_OFF) ||
  1137. (trf->state == TRF7970A_ST_RF_OFF)) {
  1138. ret = trf7970a_switch_rf_on(trf);
  1139. if (ret)
  1140. goto err_unlock;
  1141. }
  1142. switch (type) {
  1143. case NFC_DIGITAL_CONFIG_RF_TECH:
  1144. ret = trf7970a_in_config_rf_tech(trf, param);
  1145. break;
  1146. case NFC_DIGITAL_CONFIG_FRAMING:
  1147. ret = trf7970a_in_config_framing(trf, param);
  1148. break;
  1149. default:
  1150. dev_dbg(trf->dev, "Unknown type: %d\n", type);
  1151. ret = -EINVAL;
  1152. }
  1153. err_unlock:
  1154. mutex_unlock(&trf->lock);
  1155. return ret;
  1156. }
  1157. static int trf7970a_is_iso15693_write_or_lock(u8 cmd)
  1158. {
  1159. switch (cmd) {
  1160. case ISO15693_CMD_WRITE_SINGLE_BLOCK:
  1161. case ISO15693_CMD_LOCK_BLOCK:
  1162. case ISO15693_CMD_WRITE_MULTIPLE_BLOCK:
  1163. case ISO15693_CMD_WRITE_AFI:
  1164. case ISO15693_CMD_LOCK_AFI:
  1165. case ISO15693_CMD_WRITE_DSFID:
  1166. case ISO15693_CMD_LOCK_DSFID:
  1167. return 1;
  1168. default:
  1169. return 0;
  1170. }
  1171. }
  1172. static int trf7970a_per_cmd_config(struct trf7970a *trf,
  1173. const struct sk_buff *skb)
  1174. {
  1175. const u8 *req = skb->data;
  1176. u8 special_fcn_reg1, iso_ctrl;
  1177. int ret;
  1178. trf->issue_eof = false;
  1179. /* When issuing Type 2 read command, make sure the '4_bit_RX' bit in
  1180. * special functions register 1 is cleared; otherwise, its a write or
  1181. * sector select command and '4_bit_RX' must be set.
  1182. *
  1183. * When issuing an ISO 15693 command, inspect the flags byte to see
  1184. * what speed to use. Also, remember if the OPTION flag is set on
  1185. * a Type 5 write or lock command so the driver will know that it
  1186. * has to send an EOF in order to get a response.
  1187. */
  1188. if ((trf->technology == NFC_DIGITAL_RF_TECH_106A) &&
  1189. (trf->framing == NFC_DIGITAL_FRAMING_NFCA_T2T)) {
  1190. if (req[0] == NFC_T2T_CMD_READ)
  1191. special_fcn_reg1 = 0;
  1192. else
  1193. special_fcn_reg1 = TRF7970A_SPECIAL_FCN_REG1_4_BIT_RX;
  1194. if (special_fcn_reg1 != trf->special_fcn_reg1) {
  1195. ret = trf7970a_write(trf, TRF7970A_SPECIAL_FCN_REG1,
  1196. special_fcn_reg1);
  1197. if (ret)
  1198. return ret;
  1199. trf->special_fcn_reg1 = special_fcn_reg1;
  1200. }
  1201. } else if (trf->technology == NFC_DIGITAL_RF_TECH_ISO15693) {
  1202. iso_ctrl = trf->iso_ctrl & ~TRF7970A_ISO_CTRL_RFID_SPEED_MASK;
  1203. switch (req[0] & ISO15693_REQ_FLAG_SPEED_MASK) {
  1204. case 0x00:
  1205. iso_ctrl |= TRF7970A_ISO_CTRL_15693_SGL_1OF4_662;
  1206. break;
  1207. case ISO15693_REQ_FLAG_SUB_CARRIER:
  1208. iso_ctrl |= TRF7970A_ISO_CTRL_15693_DBL_1OF4_667a;
  1209. break;
  1210. case ISO15693_REQ_FLAG_DATA_RATE:
  1211. iso_ctrl |= TRF7970A_ISO_CTRL_15693_SGL_1OF4_2648;
  1212. break;
  1213. case (ISO15693_REQ_FLAG_SUB_CARRIER |
  1214. ISO15693_REQ_FLAG_DATA_RATE):
  1215. iso_ctrl |= TRF7970A_ISO_CTRL_15693_DBL_1OF4_2669;
  1216. break;
  1217. }
  1218. if (iso_ctrl != trf->iso_ctrl) {
  1219. ret = trf7970a_write(trf, TRF7970A_ISO_CTRL, iso_ctrl);
  1220. if (ret)
  1221. return ret;
  1222. trf->iso_ctrl = iso_ctrl;
  1223. }
  1224. if ((trf->framing == NFC_DIGITAL_FRAMING_ISO15693_T5T) &&
  1225. trf7970a_is_iso15693_write_or_lock(req[1]) &&
  1226. (req[0] & ISO15693_REQ_FLAG_OPTION))
  1227. trf->issue_eof = true;
  1228. }
  1229. return 0;
  1230. }
  1231. static int trf7970a_send_cmd(struct nfc_digital_dev *ddev,
  1232. struct sk_buff *skb, u16 timeout,
  1233. nfc_digital_cmd_complete_t cb, void *arg)
  1234. {
  1235. struct trf7970a *trf = nfc_digital_get_drvdata(ddev);
  1236. u8 prefix[5];
  1237. unsigned int len;
  1238. int ret;
  1239. u8 status;
  1240. dev_dbg(trf->dev, "New request - state: %d, timeout: %d ms, len: %d\n",
  1241. trf->state, timeout, skb->len);
  1242. if (skb->len > TRF7970A_TX_MAX)
  1243. return -EINVAL;
  1244. mutex_lock(&trf->lock);
  1245. if ((trf->state != TRF7970A_ST_IDLE) &&
  1246. (trf->state != TRF7970A_ST_IDLE_RX_BLOCKED)) {
  1247. dev_err(trf->dev, "%s - Bogus state: %d\n", __func__,
  1248. trf->state);
  1249. ret = -EIO;
  1250. goto out_err;
  1251. }
  1252. if (trf->aborting) {
  1253. dev_dbg(trf->dev, "Abort process complete\n");
  1254. trf->aborting = false;
  1255. ret = -ECANCELED;
  1256. goto out_err;
  1257. }
  1258. if (timeout) {
  1259. trf->rx_skb = nfc_alloc_recv_skb(TRF7970A_RX_SKB_ALLOC_SIZE,
  1260. GFP_KERNEL);
  1261. if (!trf->rx_skb) {
  1262. dev_dbg(trf->dev, "Can't alloc rx_skb\n");
  1263. ret = -ENOMEM;
  1264. goto out_err;
  1265. }
  1266. }
  1267. if (trf->state == TRF7970A_ST_IDLE_RX_BLOCKED) {
  1268. ret = trf7970a_cmd(trf, TRF7970A_CMD_ENABLE_RX);
  1269. if (ret)
  1270. goto out_err;
  1271. trf->state = TRF7970A_ST_IDLE;
  1272. }
  1273. if (trf->is_initiator) {
  1274. ret = trf7970a_per_cmd_config(trf, skb);
  1275. if (ret)
  1276. goto out_err;
  1277. }
  1278. trf->ddev = ddev;
  1279. trf->tx_skb = skb;
  1280. trf->cb = cb;
  1281. trf->cb_arg = arg;
  1282. trf->timeout = timeout;
  1283. trf->ignore_timeout = false;
  1284. len = skb->len;
  1285. /* TX data must be prefixed with a FIFO reset cmd, a cmd that depends
  1286. * on what the current framing is, the address of the TX length byte 1
  1287. * register (0x1d), and the 2 byte length of the data to be transmitted.
  1288. * That totals 5 bytes.
  1289. */
  1290. prefix[0] = TRF7970A_CMD_BIT_CTRL |
  1291. TRF7970A_CMD_BIT_OPCODE(TRF7970A_CMD_FIFO_RESET);
  1292. prefix[1] = TRF7970A_CMD_BIT_CTRL |
  1293. TRF7970A_CMD_BIT_OPCODE(trf->tx_cmd);
  1294. prefix[2] = TRF7970A_CMD_BIT_CONTINUOUS | TRF7970A_TX_LENGTH_BYTE1;
  1295. if (trf->framing == NFC_DIGITAL_FRAMING_NFCA_SHORT) {
  1296. prefix[3] = 0x00;
  1297. prefix[4] = 0x0f; /* 7 bits */
  1298. } else {
  1299. prefix[3] = (len & 0xf00) >> 4;
  1300. prefix[3] |= ((len & 0xf0) >> 4);
  1301. prefix[4] = ((len & 0x0f) << 4);
  1302. }
  1303. len = min_t(int, skb->len, TRF7970A_FIFO_SIZE);
  1304. /* Clear possible spurious interrupt */
  1305. ret = trf7970a_read_irqstatus(trf, &status);
  1306. if (ret)
  1307. goto out_err;
  1308. ret = trf7970a_transmit(trf, skb, len, prefix, sizeof(prefix));
  1309. if (ret) {
  1310. kfree_skb(trf->rx_skb);
  1311. trf->rx_skb = NULL;
  1312. }
  1313. out_err:
  1314. mutex_unlock(&trf->lock);
  1315. return ret;
  1316. }
  1317. static int trf7970a_tg_config_rf_tech(struct trf7970a *trf, int tech)
  1318. {
  1319. int ret = 0;
  1320. dev_dbg(trf->dev, "rf technology: %d\n", tech);
  1321. switch (tech) {
  1322. case NFC_DIGITAL_RF_TECH_106A:
  1323. trf->iso_ctrl_tech = TRF7970A_ISO_CTRL_NFC_NFC_CE_MODE |
  1324. TRF7970A_ISO_CTRL_NFC_CE | TRF7970A_ISO_CTRL_NFC_CE_14443A;
  1325. trf->modulator_sys_clk_ctrl =
  1326. (trf->modulator_sys_clk_ctrl & 0xf8) |
  1327. TRF7970A_MODULATOR_DEPTH_OOK;
  1328. break;
  1329. case NFC_DIGITAL_RF_TECH_212F:
  1330. trf->iso_ctrl_tech = TRF7970A_ISO_CTRL_NFC_NFC_CE_MODE |
  1331. TRF7970A_ISO_CTRL_NFC_NFCF_212;
  1332. trf->modulator_sys_clk_ctrl =
  1333. (trf->modulator_sys_clk_ctrl & 0xf8) |
  1334. TRF7970A_MODULATOR_DEPTH_ASK10;
  1335. break;
  1336. case NFC_DIGITAL_RF_TECH_424F:
  1337. trf->iso_ctrl_tech = TRF7970A_ISO_CTRL_NFC_NFC_CE_MODE |
  1338. TRF7970A_ISO_CTRL_NFC_NFCF_424;
  1339. trf->modulator_sys_clk_ctrl =
  1340. (trf->modulator_sys_clk_ctrl & 0xf8) |
  1341. TRF7970A_MODULATOR_DEPTH_ASK10;
  1342. break;
  1343. default:
  1344. dev_dbg(trf->dev, "Unsupported rf technology: %d\n", tech);
  1345. return -EINVAL;
  1346. }
  1347. trf->technology = tech;
  1348. /* Normally we write the ISO_CTRL register in
  1349. * trf7970a_tg_config_framing() because the framing can change
  1350. * the value written. However, when sending a PSL RES,
  1351. * digital_tg_send_psl_res_complete() doesn't call
  1352. * trf7970a_tg_config_framing() so we must write the register
  1353. * here.
  1354. */
  1355. if ((trf->framing == NFC_DIGITAL_FRAMING_NFC_DEP_ACTIVATED) &&
  1356. (trf->iso_ctrl_tech != trf->iso_ctrl)) {
  1357. ret = trf7970a_write(trf, TRF7970A_ISO_CTRL,
  1358. trf->iso_ctrl_tech);
  1359. trf->iso_ctrl = trf->iso_ctrl_tech;
  1360. }
  1361. return ret;
  1362. }
  1363. /* Since this is a target routine, several of the framing calls are
  1364. * made between receiving the request and sending the response so they
  1365. * should take effect until after the response is sent. This is accomplished
  1366. * by skipping the ISO_CTRL register write here and doing it in the interrupt
  1367. * handler.
  1368. */
  1369. static int trf7970a_tg_config_framing(struct trf7970a *trf, int framing)
  1370. {
  1371. u8 iso_ctrl = trf->iso_ctrl_tech;
  1372. int ret;
  1373. dev_dbg(trf->dev, "framing: %d\n", framing);
  1374. switch (framing) {
  1375. case NFC_DIGITAL_FRAMING_NFCA_NFC_DEP:
  1376. trf->tx_cmd = TRF7970A_CMD_TRANSMIT_NO_CRC;
  1377. iso_ctrl |= TRF7970A_ISO_CTRL_RX_CRC_N;
  1378. break;
  1379. case NFC_DIGITAL_FRAMING_NFCA_STANDARD:
  1380. case NFC_DIGITAL_FRAMING_NFCA_STANDARD_WITH_CRC_A:
  1381. case NFC_DIGITAL_FRAMING_NFCA_ANTICOL_COMPLETE:
  1382. /* These ones are applied in the interrupt handler */
  1383. iso_ctrl = trf->iso_ctrl; /* Don't write to ISO_CTRL yet */
  1384. break;
  1385. case NFC_DIGITAL_FRAMING_NFCF_NFC_DEP:
  1386. trf->tx_cmd = TRF7970A_CMD_TRANSMIT;
  1387. iso_ctrl &= ~TRF7970A_ISO_CTRL_RX_CRC_N;
  1388. break;
  1389. case NFC_DIGITAL_FRAMING_NFC_DEP_ACTIVATED:
  1390. trf->tx_cmd = TRF7970A_CMD_TRANSMIT;
  1391. iso_ctrl &= ~TRF7970A_ISO_CTRL_RX_CRC_N;
  1392. break;
  1393. default:
  1394. dev_dbg(trf->dev, "Unsupported Framing: %d\n", framing);
  1395. return -EINVAL;
  1396. }
  1397. trf->framing = framing;
  1398. if (iso_ctrl != trf->iso_ctrl) {
  1399. ret = trf7970a_write(trf, TRF7970A_ISO_CTRL, iso_ctrl);
  1400. if (ret)
  1401. return ret;
  1402. trf->iso_ctrl = iso_ctrl;
  1403. ret = trf7970a_write(trf, TRF7970A_MODULATOR_SYS_CLK_CTRL,
  1404. trf->modulator_sys_clk_ctrl);
  1405. if (ret)
  1406. return ret;
  1407. }
  1408. if (!(trf->chip_status_ctrl & TRF7970A_CHIP_STATUS_RF_ON)) {
  1409. ret = trf7970a_write(trf, TRF7970A_CHIP_STATUS_CTRL,
  1410. trf->chip_status_ctrl |
  1411. TRF7970A_CHIP_STATUS_RF_ON);
  1412. if (ret)
  1413. return ret;
  1414. trf->chip_status_ctrl |= TRF7970A_CHIP_STATUS_RF_ON;
  1415. }
  1416. return 0;
  1417. }
  1418. static int trf7970a_tg_configure_hw(struct nfc_digital_dev *ddev, int type,
  1419. int param)
  1420. {
  1421. struct trf7970a *trf = nfc_digital_get_drvdata(ddev);
  1422. int ret;
  1423. dev_dbg(trf->dev, "Configure hw - type: %d, param: %d\n", type, param);
  1424. mutex_lock(&trf->lock);
  1425. trf->is_initiator = false;
  1426. if ((trf->state == TRF7970A_ST_PWR_OFF) ||
  1427. (trf->state == TRF7970A_ST_RF_OFF)) {
  1428. ret = trf7970a_switch_rf_on(trf);
  1429. if (ret)
  1430. goto err_unlock;
  1431. }
  1432. switch (type) {
  1433. case NFC_DIGITAL_CONFIG_RF_TECH:
  1434. ret = trf7970a_tg_config_rf_tech(trf, param);
  1435. break;
  1436. case NFC_DIGITAL_CONFIG_FRAMING:
  1437. ret = trf7970a_tg_config_framing(trf, param);
  1438. break;
  1439. default:
  1440. dev_dbg(trf->dev, "Unknown type: %d\n", type);
  1441. ret = -EINVAL;
  1442. }
  1443. err_unlock:
  1444. mutex_unlock(&trf->lock);
  1445. return ret;
  1446. }
  1447. static int _trf7970a_tg_listen(struct nfc_digital_dev *ddev, u16 timeout,
  1448. nfc_digital_cmd_complete_t cb, void *arg,
  1449. bool mode_detect)
  1450. {
  1451. struct trf7970a *trf = nfc_digital_get_drvdata(ddev);
  1452. int ret;
  1453. mutex_lock(&trf->lock);
  1454. if ((trf->state != TRF7970A_ST_IDLE) &&
  1455. (trf->state != TRF7970A_ST_IDLE_RX_BLOCKED)) {
  1456. dev_err(trf->dev, "%s - Bogus state: %d\n", __func__,
  1457. trf->state);
  1458. ret = -EIO;
  1459. goto out_err;
  1460. }
  1461. if (trf->aborting) {
  1462. dev_dbg(trf->dev, "Abort process complete\n");
  1463. trf->aborting = false;
  1464. ret = -ECANCELED;
  1465. goto out_err;
  1466. }
  1467. trf->rx_skb = nfc_alloc_recv_skb(TRF7970A_RX_SKB_ALLOC_SIZE,
  1468. GFP_KERNEL);
  1469. if (!trf->rx_skb) {
  1470. dev_dbg(trf->dev, "Can't alloc rx_skb\n");
  1471. ret = -ENOMEM;
  1472. goto out_err;
  1473. }
  1474. ret = trf7970a_write(trf, TRF7970A_RX_SPECIAL_SETTINGS,
  1475. TRF7970A_RX_SPECIAL_SETTINGS_HBT |
  1476. TRF7970A_RX_SPECIAL_SETTINGS_M848 |
  1477. TRF7970A_RX_SPECIAL_SETTINGS_C424 |
  1478. TRF7970A_RX_SPECIAL_SETTINGS_C212);
  1479. if (ret)
  1480. goto out_err;
  1481. ret = trf7970a_write(trf, TRF7970A_REG_IO_CTRL,
  1482. trf->io_ctrl | TRF7970A_REG_IO_CTRL_VRS(0x1));
  1483. if (ret)
  1484. goto out_err;
  1485. ret = trf7970a_write(trf, TRF7970A_NFC_LOW_FIELD_LEVEL,
  1486. TRF7970A_NFC_LOW_FIELD_LEVEL_RFDET(0x3));
  1487. if (ret)
  1488. goto out_err;
  1489. ret = trf7970a_write(trf, TRF7970A_NFC_TARGET_LEVEL,
  1490. TRF7970A_NFC_TARGET_LEVEL_RFDET(0x7));
  1491. if (ret)
  1492. goto out_err;
  1493. trf->ddev = ddev;
  1494. trf->cb = cb;
  1495. trf->cb_arg = arg;
  1496. trf->timeout = timeout;
  1497. trf->ignore_timeout = false;
  1498. ret = trf7970a_cmd(trf, TRF7970A_CMD_ENABLE_RX);
  1499. if (ret)
  1500. goto out_err;
  1501. trf->state = mode_detect ? TRF7970A_ST_LISTENING_MD :
  1502. TRF7970A_ST_LISTENING;
  1503. schedule_delayed_work(&trf->timeout_work, msecs_to_jiffies(timeout));
  1504. out_err:
  1505. mutex_unlock(&trf->lock);
  1506. return ret;
  1507. }
  1508. static int trf7970a_tg_listen(struct nfc_digital_dev *ddev, u16 timeout,
  1509. nfc_digital_cmd_complete_t cb, void *arg)
  1510. {
  1511. const struct trf7970a *trf = nfc_digital_get_drvdata(ddev);
  1512. dev_dbg(trf->dev, "Listen - state: %d, timeout: %d ms\n",
  1513. trf->state, timeout);
  1514. return _trf7970a_tg_listen(ddev, timeout, cb, arg, false);
  1515. }
  1516. static int trf7970a_tg_listen_md(struct nfc_digital_dev *ddev,
  1517. u16 timeout, nfc_digital_cmd_complete_t cb,
  1518. void *arg)
  1519. {
  1520. const struct trf7970a *trf = nfc_digital_get_drvdata(ddev);
  1521. int ret;
  1522. dev_dbg(trf->dev, "Listen MD - state: %d, timeout: %d ms\n",
  1523. trf->state, timeout);
  1524. ret = trf7970a_tg_configure_hw(ddev, NFC_DIGITAL_CONFIG_RF_TECH,
  1525. NFC_DIGITAL_RF_TECH_106A);
  1526. if (ret)
  1527. return ret;
  1528. ret = trf7970a_tg_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING,
  1529. NFC_DIGITAL_FRAMING_NFCA_NFC_DEP);
  1530. if (ret)
  1531. return ret;
  1532. return _trf7970a_tg_listen(ddev, timeout, cb, arg, true);
  1533. }
  1534. static int trf7970a_tg_get_rf_tech(struct nfc_digital_dev *ddev, u8 *rf_tech)
  1535. {
  1536. const struct trf7970a *trf = nfc_digital_get_drvdata(ddev);
  1537. dev_dbg(trf->dev, "Get RF Tech - state: %d, rf_tech: %d\n",
  1538. trf->state, trf->md_rf_tech);
  1539. *rf_tech = trf->md_rf_tech;
  1540. return 0;
  1541. }
  1542. static void trf7970a_abort_cmd(struct nfc_digital_dev *ddev)
  1543. {
  1544. struct trf7970a *trf = nfc_digital_get_drvdata(ddev);
  1545. dev_dbg(trf->dev, "Abort process initiated\n");
  1546. mutex_lock(&trf->lock);
  1547. switch (trf->state) {
  1548. case TRF7970A_ST_WAIT_FOR_TX_FIFO:
  1549. case TRF7970A_ST_WAIT_FOR_RX_DATA:
  1550. case TRF7970A_ST_WAIT_FOR_RX_DATA_CONT:
  1551. case TRF7970A_ST_WAIT_TO_ISSUE_EOF:
  1552. trf->aborting = true;
  1553. break;
  1554. case TRF7970A_ST_LISTENING:
  1555. trf->ignore_timeout = !cancel_delayed_work(&trf->timeout_work);
  1556. trf7970a_send_err_upstream(trf, -ECANCELED);
  1557. dev_dbg(trf->dev, "Abort process complete\n");
  1558. break;
  1559. default:
  1560. break;
  1561. }
  1562. mutex_unlock(&trf->lock);
  1563. }
  1564. static const struct nfc_digital_ops trf7970a_nfc_ops = {
  1565. .in_configure_hw = trf7970a_in_configure_hw,
  1566. .in_send_cmd = trf7970a_send_cmd,
  1567. .tg_configure_hw = trf7970a_tg_configure_hw,
  1568. .tg_send_cmd = trf7970a_send_cmd,
  1569. .tg_listen = trf7970a_tg_listen,
  1570. .tg_listen_md = trf7970a_tg_listen_md,
  1571. .tg_get_rf_tech = trf7970a_tg_get_rf_tech,
  1572. .switch_rf = trf7970a_switch_rf,
  1573. .abort_cmd = trf7970a_abort_cmd,
  1574. };
  1575. static int trf7970a_power_up(struct trf7970a *trf)
  1576. {
  1577. int ret;
  1578. dev_dbg(trf->dev, "Powering up - state: %d\n", trf->state);
  1579. if (trf->state != TRF7970A_ST_PWR_OFF)
  1580. return 0;
  1581. ret = regulator_enable(trf->vin_regulator);
  1582. if (ret) {
  1583. dev_err(trf->dev, "%s - Can't enable VIN: %d\n", __func__, ret);
  1584. return ret;
  1585. }
  1586. usleep_range(5000, 6000);
  1587. if (trf->en2_gpiod &&
  1588. !(trf->quirks & TRF7970A_QUIRK_EN2_MUST_STAY_LOW)) {
  1589. gpiod_set_value_cansleep(trf->en2_gpiod, 1);
  1590. usleep_range(1000, 2000);
  1591. }
  1592. gpiod_set_value_cansleep(trf->en_gpiod, 1);
  1593. usleep_range(20000, 21000);
  1594. trf->state = TRF7970A_ST_RF_OFF;
  1595. return 0;
  1596. }
  1597. static int trf7970a_power_down(struct trf7970a *trf)
  1598. {
  1599. int ret;
  1600. dev_dbg(trf->dev, "Powering down - state: %d\n", trf->state);
  1601. if (trf->state == TRF7970A_ST_PWR_OFF)
  1602. return 0;
  1603. if (trf->state != TRF7970A_ST_RF_OFF) {
  1604. dev_dbg(trf->dev, "Can't power down - not RF_OFF state (%d)\n",
  1605. trf->state);
  1606. return -EBUSY;
  1607. }
  1608. gpiod_set_value_cansleep(trf->en_gpiod, 0);
  1609. if (trf->en2_gpiod && !(trf->quirks & TRF7970A_QUIRK_EN2_MUST_STAY_LOW))
  1610. gpiod_set_value_cansleep(trf->en2_gpiod, 0);
  1611. ret = regulator_disable(trf->vin_regulator);
  1612. if (ret)
  1613. dev_err(trf->dev, "%s - Can't disable VIN: %d\n", __func__,
  1614. ret);
  1615. trf->state = TRF7970A_ST_PWR_OFF;
  1616. return ret;
  1617. }
  1618. static int trf7970a_startup(struct trf7970a *trf)
  1619. {
  1620. int ret;
  1621. ret = trf7970a_power_up(trf);
  1622. if (ret)
  1623. return ret;
  1624. pm_runtime_set_active(trf->dev);
  1625. pm_runtime_enable(trf->dev);
  1626. pm_runtime_mark_last_busy(trf->dev);
  1627. return 0;
  1628. }
  1629. static void trf7970a_shutdown(struct trf7970a *trf)
  1630. {
  1631. switch (trf->state) {
  1632. case TRF7970A_ST_WAIT_FOR_TX_FIFO:
  1633. case TRF7970A_ST_WAIT_FOR_RX_DATA:
  1634. case TRF7970A_ST_WAIT_FOR_RX_DATA_CONT:
  1635. case TRF7970A_ST_WAIT_TO_ISSUE_EOF:
  1636. case TRF7970A_ST_LISTENING:
  1637. trf7970a_send_err_upstream(trf, -ECANCELED);
  1638. fallthrough;
  1639. case TRF7970A_ST_IDLE:
  1640. case TRF7970A_ST_IDLE_RX_BLOCKED:
  1641. trf7970a_switch_rf_off(trf);
  1642. break;
  1643. default:
  1644. break;
  1645. }
  1646. pm_runtime_disable(trf->dev);
  1647. pm_runtime_set_suspended(trf->dev);
  1648. trf7970a_power_down(trf);
  1649. }
  1650. static int trf7970a_get_autosuspend_delay(const struct device_node *np)
  1651. {
  1652. int autosuspend_delay, ret;
  1653. ret = of_property_read_u32(np, "autosuspend-delay", &autosuspend_delay);
  1654. if (ret)
  1655. autosuspend_delay = TRF7970A_AUTOSUSPEND_DELAY;
  1656. return autosuspend_delay;
  1657. }
  1658. static int trf7970a_probe(struct spi_device *spi)
  1659. {
  1660. const struct device_node *np = spi->dev.of_node;
  1661. struct trf7970a *trf;
  1662. int uvolts, autosuspend_delay, ret;
  1663. u32 clk_freq = TRF7970A_13MHZ_CLOCK_FREQUENCY;
  1664. if (!np) {
  1665. dev_err(&spi->dev, "No Device Tree entry\n");
  1666. return -EINVAL;
  1667. }
  1668. trf = devm_kzalloc(&spi->dev, sizeof(*trf), GFP_KERNEL);
  1669. if (!trf)
  1670. return -ENOMEM;
  1671. trf->state = TRF7970A_ST_PWR_OFF;
  1672. trf->dev = &spi->dev;
  1673. trf->spi = spi;
  1674. spi->mode = SPI_MODE_1;
  1675. spi->bits_per_word = 8;
  1676. ret = spi_setup(spi);
  1677. if (ret < 0) {
  1678. dev_err(trf->dev, "Can't set up SPI Communication\n");
  1679. return ret;
  1680. }
  1681. if (of_property_read_bool(np, "irq-status-read-quirk"))
  1682. trf->quirks |= TRF7970A_QUIRK_IRQ_STATUS_READ;
  1683. /* There are two enable pins - only EN must be present in the DT */
  1684. trf->en_gpiod = devm_gpiod_get_index(trf->dev, "ti,enable", 0,
  1685. GPIOD_OUT_LOW);
  1686. if (IS_ERR(trf->en_gpiod)) {
  1687. dev_err(trf->dev, "No EN GPIO property\n");
  1688. return PTR_ERR(trf->en_gpiod);
  1689. }
  1690. trf->en2_gpiod = devm_gpiod_get_index_optional(trf->dev, "ti,enable", 1,
  1691. GPIOD_OUT_LOW);
  1692. if (!trf->en2_gpiod) {
  1693. dev_info(trf->dev, "No EN2 GPIO property\n");
  1694. } else if (IS_ERR(trf->en2_gpiod)) {
  1695. dev_err(trf->dev, "Error getting EN2 GPIO property: %ld\n",
  1696. PTR_ERR(trf->en2_gpiod));
  1697. return PTR_ERR(trf->en2_gpiod);
  1698. } else if (of_property_read_bool(np, "en2-rf-quirk")) {
  1699. trf->quirks |= TRF7970A_QUIRK_EN2_MUST_STAY_LOW;
  1700. }
  1701. of_property_read_u32(np, "clock-frequency", &clk_freq);
  1702. if ((clk_freq != TRF7970A_27MHZ_CLOCK_FREQUENCY) &&
  1703. (clk_freq != TRF7970A_13MHZ_CLOCK_FREQUENCY)) {
  1704. dev_err(trf->dev,
  1705. "clock-frequency (%u Hz) unsupported\n", clk_freq);
  1706. return -EINVAL;
  1707. }
  1708. if (clk_freq == TRF7970A_27MHZ_CLOCK_FREQUENCY) {
  1709. trf->modulator_sys_clk_ctrl = TRF7970A_MODULATOR_27MHZ;
  1710. dev_dbg(trf->dev, "trf7970a configured for 27MHz crystal\n");
  1711. } else {
  1712. trf->modulator_sys_clk_ctrl = 0;
  1713. }
  1714. ret = devm_request_threaded_irq(trf->dev, spi->irq, NULL,
  1715. trf7970a_irq,
  1716. IRQF_TRIGGER_RISING | IRQF_ONESHOT,
  1717. "trf7970a", trf);
  1718. if (ret) {
  1719. dev_err(trf->dev, "Can't request IRQ#%d: %d\n", spi->irq, ret);
  1720. return ret;
  1721. }
  1722. mutex_init(&trf->lock);
  1723. INIT_DELAYED_WORK(&trf->timeout_work, trf7970a_timeout_work_handler);
  1724. trf->vin_regulator = devm_regulator_get(&spi->dev, "vin");
  1725. if (IS_ERR(trf->vin_regulator)) {
  1726. ret = PTR_ERR(trf->vin_regulator);
  1727. dev_err(trf->dev, "Can't get VIN regulator: %d\n", ret);
  1728. goto err_destroy_lock;
  1729. }
  1730. ret = regulator_enable(trf->vin_regulator);
  1731. if (ret) {
  1732. dev_err(trf->dev, "Can't enable VIN: %d\n", ret);
  1733. goto err_destroy_lock;
  1734. }
  1735. uvolts = regulator_get_voltage(trf->vin_regulator);
  1736. if (uvolts > 4000000)
  1737. trf->chip_status_ctrl = TRF7970A_CHIP_STATUS_VRS5_3;
  1738. trf->vddio_regulator = devm_regulator_get(&spi->dev, "vdd-io");
  1739. if (IS_ERR(trf->vddio_regulator)) {
  1740. ret = PTR_ERR(trf->vddio_regulator);
  1741. dev_err(trf->dev, "Can't get VDD_IO regulator: %d\n", ret);
  1742. goto err_disable_vin_regulator;
  1743. }
  1744. ret = regulator_enable(trf->vddio_regulator);
  1745. if (ret) {
  1746. dev_err(trf->dev, "Can't enable VDD_IO: %d\n", ret);
  1747. goto err_disable_vin_regulator;
  1748. }
  1749. if (regulator_get_voltage(trf->vddio_regulator) == 1800000) {
  1750. trf->io_ctrl = TRF7970A_REG_IO_CTRL_IO_LOW;
  1751. dev_dbg(trf->dev, "trf7970a config vdd_io to 1.8V\n");
  1752. }
  1753. trf->ddev = nfc_digital_allocate_device(&trf7970a_nfc_ops,
  1754. TRF7970A_SUPPORTED_PROTOCOLS,
  1755. NFC_DIGITAL_DRV_CAPS_IN_CRC |
  1756. NFC_DIGITAL_DRV_CAPS_TG_CRC, 0,
  1757. 0);
  1758. if (!trf->ddev) {
  1759. dev_err(trf->dev, "Can't allocate NFC digital device\n");
  1760. ret = -ENOMEM;
  1761. goto err_disable_vddio_regulator;
  1762. }
  1763. nfc_digital_set_parent_dev(trf->ddev, trf->dev);
  1764. nfc_digital_set_drvdata(trf->ddev, trf);
  1765. spi_set_drvdata(spi, trf);
  1766. autosuspend_delay = trf7970a_get_autosuspend_delay(np);
  1767. pm_runtime_set_autosuspend_delay(trf->dev, autosuspend_delay);
  1768. pm_runtime_use_autosuspend(trf->dev);
  1769. ret = trf7970a_startup(trf);
  1770. if (ret)
  1771. goto err_free_ddev;
  1772. ret = nfc_digital_register_device(trf->ddev);
  1773. if (ret) {
  1774. dev_err(trf->dev, "Can't register NFC digital device: %d\n",
  1775. ret);
  1776. goto err_shutdown;
  1777. }
  1778. return 0;
  1779. err_shutdown:
  1780. trf7970a_shutdown(trf);
  1781. err_free_ddev:
  1782. nfc_digital_free_device(trf->ddev);
  1783. err_disable_vddio_regulator:
  1784. regulator_disable(trf->vddio_regulator);
  1785. err_disable_vin_regulator:
  1786. regulator_disable(trf->vin_regulator);
  1787. err_destroy_lock:
  1788. mutex_destroy(&trf->lock);
  1789. return ret;
  1790. }
  1791. static void trf7970a_remove(struct spi_device *spi)
  1792. {
  1793. struct trf7970a *trf = spi_get_drvdata(spi);
  1794. mutex_lock(&trf->lock);
  1795. trf7970a_shutdown(trf);
  1796. mutex_unlock(&trf->lock);
  1797. nfc_digital_unregister_device(trf->ddev);
  1798. nfc_digital_free_device(trf->ddev);
  1799. regulator_disable(trf->vddio_regulator);
  1800. regulator_disable(trf->vin_regulator);
  1801. mutex_destroy(&trf->lock);
  1802. }
  1803. #ifdef CONFIG_PM_SLEEP
  1804. static int trf7970a_suspend(struct device *dev)
  1805. {
  1806. struct spi_device *spi = to_spi_device(dev);
  1807. struct trf7970a *trf = spi_get_drvdata(spi);
  1808. mutex_lock(&trf->lock);
  1809. trf7970a_shutdown(trf);
  1810. mutex_unlock(&trf->lock);
  1811. return 0;
  1812. }
  1813. static int trf7970a_resume(struct device *dev)
  1814. {
  1815. struct spi_device *spi = to_spi_device(dev);
  1816. struct trf7970a *trf = spi_get_drvdata(spi);
  1817. int ret;
  1818. mutex_lock(&trf->lock);
  1819. ret = trf7970a_startup(trf);
  1820. mutex_unlock(&trf->lock);
  1821. return ret;
  1822. }
  1823. #endif
  1824. #ifdef CONFIG_PM
  1825. static int trf7970a_pm_runtime_suspend(struct device *dev)
  1826. {
  1827. struct spi_device *spi = to_spi_device(dev);
  1828. struct trf7970a *trf = spi_get_drvdata(spi);
  1829. int ret;
  1830. mutex_lock(&trf->lock);
  1831. ret = trf7970a_power_down(trf);
  1832. mutex_unlock(&trf->lock);
  1833. return ret;
  1834. }
  1835. static int trf7970a_pm_runtime_resume(struct device *dev)
  1836. {
  1837. struct spi_device *spi = to_spi_device(dev);
  1838. struct trf7970a *trf = spi_get_drvdata(spi);
  1839. int ret;
  1840. ret = trf7970a_power_up(trf);
  1841. if (!ret)
  1842. pm_runtime_mark_last_busy(dev);
  1843. return ret;
  1844. }
  1845. #endif
  1846. static const struct dev_pm_ops trf7970a_pm_ops = {
  1847. SET_SYSTEM_SLEEP_PM_OPS(trf7970a_suspend, trf7970a_resume)
  1848. SET_RUNTIME_PM_OPS(trf7970a_pm_runtime_suspend,
  1849. trf7970a_pm_runtime_resume, NULL)
  1850. };
  1851. static const struct of_device_id trf7970a_of_match[] __maybe_unused = {
  1852. {.compatible = "ti,trf7970a",},
  1853. {},
  1854. };
  1855. MODULE_DEVICE_TABLE(of, trf7970a_of_match);
  1856. static const struct spi_device_id trf7970a_id_table[] = {
  1857. {"trf7970a", 0},
  1858. {}
  1859. };
  1860. MODULE_DEVICE_TABLE(spi, trf7970a_id_table);
  1861. static struct spi_driver trf7970a_spi_driver = {
  1862. .probe = trf7970a_probe,
  1863. .remove = trf7970a_remove,
  1864. .id_table = trf7970a_id_table,
  1865. .driver = {
  1866. .name = "trf7970a",
  1867. .of_match_table = of_match_ptr(trf7970a_of_match),
  1868. .pm = &trf7970a_pm_ops,
  1869. },
  1870. };
  1871. module_spi_driver(trf7970a_spi_driver);
  1872. MODULE_AUTHOR("Mark A. Greer <mgreer@animalcreek.com>");
  1873. MODULE_LICENSE("GPL v2");
  1874. MODULE_DESCRIPTION("TI trf7970a RFID/NFC Transceiver Driver");