trf7970a.c 61 KB

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