trf7970a.c 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246
  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. ret = trf7970a_write(trf, TRF7970A_NFC_TARGET_LEVEL, 0);
  893. if (ret)
  894. goto err_out;
  895. usleep_range(1000, 2000);
  896. trf->chip_status_ctrl &= ~TRF7970A_CHIP_STATUS_RF_ON;
  897. ret = trf7970a_write(trf, TRF7970A_MODULATOR_SYS_CLK_CTRL, 0);
  898. if (ret)
  899. goto err_out;
  900. trf->modulator_sys_clk_ctrl = 0;
  901. ret = trf7970a_write(trf, TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS,
  902. TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLH_96 |
  903. TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLL_32);
  904. if (ret)
  905. goto err_out;
  906. ret = trf7970a_write(trf, TRF7970A_SPECIAL_FCN_REG1, 0);
  907. if (ret)
  908. goto err_out;
  909. trf->special_fcn_reg1 = 0;
  910. trf->iso_ctrl = 0xff;
  911. return 0;
  912. err_out:
  913. dev_dbg(trf->dev, "Couldn't init device: %d\n", ret);
  914. return ret;
  915. }
  916. static void trf7970a_switch_rf_off(struct trf7970a *trf)
  917. {
  918. if ((trf->state == TRF7970A_ST_PWR_OFF) ||
  919. (trf->state == TRF7970A_ST_RF_OFF))
  920. return;
  921. dev_dbg(trf->dev, "Switching rf off\n");
  922. trf->chip_status_ctrl &= ~TRF7970A_CHIP_STATUS_RF_ON;
  923. trf7970a_write(trf, TRF7970A_CHIP_STATUS_CTRL, trf->chip_status_ctrl);
  924. trf->aborting = false;
  925. trf->state = TRF7970A_ST_RF_OFF;
  926. pm_runtime_mark_last_busy(trf->dev);
  927. pm_runtime_put_autosuspend(trf->dev);
  928. }
  929. static int trf7970a_switch_rf_on(struct trf7970a *trf)
  930. {
  931. int ret;
  932. dev_dbg(trf->dev, "Switching rf on\n");
  933. pm_runtime_get_sync(trf->dev);
  934. if (trf->state != TRF7970A_ST_RF_OFF) { /* Power on, RF off */
  935. dev_err(trf->dev, "%s - Incorrect state: %d\n", __func__,
  936. trf->state);
  937. return -EINVAL;
  938. }
  939. ret = trf7970a_init(trf);
  940. if (ret) {
  941. dev_err(trf->dev, "%s - Can't initialize: %d\n", __func__, ret);
  942. return ret;
  943. }
  944. trf->state = TRF7970A_ST_IDLE;
  945. return 0;
  946. }
  947. static int trf7970a_switch_rf(struct nfc_digital_dev *ddev, bool on)
  948. {
  949. struct trf7970a *trf = nfc_digital_get_drvdata(ddev);
  950. int ret = 0;
  951. dev_dbg(trf->dev, "Switching RF - state: %d, on: %d\n", trf->state, on);
  952. mutex_lock(&trf->lock);
  953. if (on) {
  954. switch (trf->state) {
  955. case TRF7970A_ST_PWR_OFF:
  956. case TRF7970A_ST_RF_OFF:
  957. ret = trf7970a_switch_rf_on(trf);
  958. break;
  959. case TRF7970A_ST_IDLE:
  960. case TRF7970A_ST_IDLE_RX_BLOCKED:
  961. break;
  962. default:
  963. dev_err(trf->dev, "%s - Invalid request: %d %d\n",
  964. __func__, trf->state, on);
  965. trf7970a_switch_rf_off(trf);
  966. ret = -EINVAL;
  967. }
  968. } else {
  969. switch (trf->state) {
  970. case TRF7970A_ST_PWR_OFF:
  971. case TRF7970A_ST_RF_OFF:
  972. break;
  973. default:
  974. dev_err(trf->dev, "%s - Invalid request: %d %d\n",
  975. __func__, trf->state, on);
  976. ret = -EINVAL;
  977. /* FALLTHROUGH */
  978. case TRF7970A_ST_IDLE:
  979. case TRF7970A_ST_IDLE_RX_BLOCKED:
  980. case TRF7970A_ST_WAIT_FOR_RX_DATA:
  981. case TRF7970A_ST_WAIT_FOR_RX_DATA_CONT:
  982. trf7970a_switch_rf_off(trf);
  983. }
  984. }
  985. mutex_unlock(&trf->lock);
  986. return ret;
  987. }
  988. static int trf7970a_in_config_rf_tech(struct trf7970a *trf, int tech)
  989. {
  990. int ret = 0;
  991. dev_dbg(trf->dev, "rf technology: %d\n", tech);
  992. switch (tech) {
  993. case NFC_DIGITAL_RF_TECH_106A:
  994. trf->iso_ctrl_tech = TRF7970A_ISO_CTRL_14443A_106;
  995. trf->modulator_sys_clk_ctrl = 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 = TRF7970A_MODULATOR_DEPTH_ASK10;
  1001. trf->guard_time = TRF7970A_GUARD_TIME_NFCB;
  1002. break;
  1003. case NFC_DIGITAL_RF_TECH_212F:
  1004. trf->iso_ctrl_tech = TRF7970A_ISO_CTRL_FELICA_212;
  1005. trf->modulator_sys_clk_ctrl = TRF7970A_MODULATOR_DEPTH_ASK10;
  1006. trf->guard_time = TRF7970A_GUARD_TIME_NFCF;
  1007. break;
  1008. case NFC_DIGITAL_RF_TECH_424F:
  1009. trf->iso_ctrl_tech = TRF7970A_ISO_CTRL_FELICA_424;
  1010. trf->modulator_sys_clk_ctrl = TRF7970A_MODULATOR_DEPTH_ASK10;
  1011. trf->guard_time = TRF7970A_GUARD_TIME_NFCF;
  1012. break;
  1013. case NFC_DIGITAL_RF_TECH_ISO15693:
  1014. trf->iso_ctrl_tech = TRF7970A_ISO_CTRL_15693_SGL_1OF4_2648;
  1015. trf->modulator_sys_clk_ctrl = TRF7970A_MODULATOR_DEPTH_OOK;
  1016. trf->guard_time = TRF7970A_GUARD_TIME_15693;
  1017. break;
  1018. default:
  1019. dev_dbg(trf->dev, "Unsupported rf technology: %d\n", tech);
  1020. return -EINVAL;
  1021. }
  1022. trf->technology = tech;
  1023. /* If in initiator mode and not changing the RF tech due to a
  1024. * PSL sequence (indicated by 'trf->iso_ctrl == 0xff' from
  1025. * trf7970a_init()), clear the NFC Target Detection Level register
  1026. * due to erratum.
  1027. */
  1028. if (trf->iso_ctrl == 0xff)
  1029. ret = trf7970a_write(trf, TRF7970A_NFC_TARGET_LEVEL, 0);
  1030. return ret;
  1031. }
  1032. static int trf7970a_is_rf_field(struct trf7970a *trf, bool *is_rf_field)
  1033. {
  1034. int ret;
  1035. u8 rssi;
  1036. ret = trf7970a_write(trf, TRF7970A_CHIP_STATUS_CTRL,
  1037. trf->chip_status_ctrl | TRF7970A_CHIP_STATUS_REC_ON);
  1038. if (ret)
  1039. return ret;
  1040. ret = trf7970a_cmd(trf, TRF7970A_CMD_TEST_EXT_RF);
  1041. if (ret)
  1042. return ret;
  1043. usleep_range(50, 60);
  1044. ret = trf7970a_read(trf, TRF7970A_RSSI_OSC_STATUS, &rssi);
  1045. if (ret)
  1046. return ret;
  1047. ret = trf7970a_write(trf, TRF7970A_CHIP_STATUS_CTRL,
  1048. trf->chip_status_ctrl);
  1049. if (ret)
  1050. return ret;
  1051. if (rssi & TRF7970A_RSSI_OSC_STATUS_RSSI_MASK)
  1052. *is_rf_field = true;
  1053. else
  1054. *is_rf_field = false;
  1055. return 0;
  1056. }
  1057. static int trf7970a_in_config_framing(struct trf7970a *trf, int framing)
  1058. {
  1059. u8 iso_ctrl = trf->iso_ctrl_tech;
  1060. bool is_rf_field = false;
  1061. int ret;
  1062. dev_dbg(trf->dev, "framing: %d\n", framing);
  1063. switch (framing) {
  1064. case NFC_DIGITAL_FRAMING_NFCA_SHORT:
  1065. case NFC_DIGITAL_FRAMING_NFCA_STANDARD:
  1066. trf->tx_cmd = TRF7970A_CMD_TRANSMIT_NO_CRC;
  1067. iso_ctrl |= TRF7970A_ISO_CTRL_RX_CRC_N;
  1068. break;
  1069. case NFC_DIGITAL_FRAMING_NFCA_STANDARD_WITH_CRC_A:
  1070. case NFC_DIGITAL_FRAMING_NFCA_T4T:
  1071. case NFC_DIGITAL_FRAMING_NFCB:
  1072. case NFC_DIGITAL_FRAMING_NFCB_T4T:
  1073. case NFC_DIGITAL_FRAMING_NFCF:
  1074. case NFC_DIGITAL_FRAMING_NFCF_T3T:
  1075. case NFC_DIGITAL_FRAMING_ISO15693_INVENTORY:
  1076. case NFC_DIGITAL_FRAMING_ISO15693_T5T:
  1077. case NFC_DIGITAL_FRAMING_NFCA_NFC_DEP:
  1078. case NFC_DIGITAL_FRAMING_NFCF_NFC_DEP:
  1079. trf->tx_cmd = TRF7970A_CMD_TRANSMIT;
  1080. iso_ctrl &= ~TRF7970A_ISO_CTRL_RX_CRC_N;
  1081. break;
  1082. case NFC_DIGITAL_FRAMING_NFCA_T2T:
  1083. trf->tx_cmd = TRF7970A_CMD_TRANSMIT;
  1084. iso_ctrl |= TRF7970A_ISO_CTRL_RX_CRC_N;
  1085. break;
  1086. default:
  1087. dev_dbg(trf->dev, "Unsupported Framing: %d\n", framing);
  1088. return -EINVAL;
  1089. }
  1090. trf->framing = framing;
  1091. if (!(trf->chip_status_ctrl & TRF7970A_CHIP_STATUS_RF_ON)) {
  1092. ret = trf7970a_is_rf_field(trf, &is_rf_field);
  1093. if (ret)
  1094. return ret;
  1095. if (is_rf_field)
  1096. return -EBUSY;
  1097. }
  1098. if (iso_ctrl != trf->iso_ctrl) {
  1099. ret = trf7970a_write(trf, TRF7970A_ISO_CTRL, iso_ctrl);
  1100. if (ret)
  1101. return ret;
  1102. trf->iso_ctrl = iso_ctrl;
  1103. ret = trf7970a_write(trf, TRF7970A_MODULATOR_SYS_CLK_CTRL,
  1104. trf->modulator_sys_clk_ctrl);
  1105. if (ret)
  1106. return ret;
  1107. }
  1108. if (!(trf->chip_status_ctrl & TRF7970A_CHIP_STATUS_RF_ON)) {
  1109. ret = trf7970a_write(trf, TRF7970A_CHIP_STATUS_CTRL,
  1110. trf->chip_status_ctrl |
  1111. TRF7970A_CHIP_STATUS_RF_ON);
  1112. if (ret)
  1113. return ret;
  1114. trf->chip_status_ctrl |= TRF7970A_CHIP_STATUS_RF_ON;
  1115. usleep_range(trf->guard_time, trf->guard_time + 1000);
  1116. }
  1117. return 0;
  1118. }
  1119. static int trf7970a_in_configure_hw(struct nfc_digital_dev *ddev, int type,
  1120. int param)
  1121. {
  1122. struct trf7970a *trf = nfc_digital_get_drvdata(ddev);
  1123. int ret;
  1124. dev_dbg(trf->dev, "Configure hw - type: %d, param: %d\n", type, param);
  1125. mutex_lock(&trf->lock);
  1126. trf->is_initiator = true;
  1127. if ((trf->state == TRF7970A_ST_PWR_OFF) ||
  1128. (trf->state == TRF7970A_ST_RF_OFF)) {
  1129. ret = trf7970a_switch_rf_on(trf);
  1130. if (ret)
  1131. goto err_unlock;
  1132. }
  1133. switch (type) {
  1134. case NFC_DIGITAL_CONFIG_RF_TECH:
  1135. ret = trf7970a_in_config_rf_tech(trf, param);
  1136. break;
  1137. case NFC_DIGITAL_CONFIG_FRAMING:
  1138. ret = trf7970a_in_config_framing(trf, param);
  1139. break;
  1140. default:
  1141. dev_dbg(trf->dev, "Unknown type: %d\n", type);
  1142. ret = -EINVAL;
  1143. }
  1144. err_unlock:
  1145. mutex_unlock(&trf->lock);
  1146. return ret;
  1147. }
  1148. static int trf7970a_is_iso15693_write_or_lock(u8 cmd)
  1149. {
  1150. switch (cmd) {
  1151. case ISO15693_CMD_WRITE_SINGLE_BLOCK:
  1152. case ISO15693_CMD_LOCK_BLOCK:
  1153. case ISO15693_CMD_WRITE_MULTIPLE_BLOCK:
  1154. case ISO15693_CMD_WRITE_AFI:
  1155. case ISO15693_CMD_LOCK_AFI:
  1156. case ISO15693_CMD_WRITE_DSFID:
  1157. case ISO15693_CMD_LOCK_DSFID:
  1158. return 1;
  1159. break;
  1160. default:
  1161. return 0;
  1162. }
  1163. }
  1164. static int trf7970a_per_cmd_config(struct trf7970a *trf, struct sk_buff *skb)
  1165. {
  1166. u8 *req = skb->data;
  1167. u8 special_fcn_reg1, iso_ctrl;
  1168. int ret;
  1169. trf->issue_eof = false;
  1170. /* When issuing Type 2 read command, make sure the '4_bit_RX' bit in
  1171. * special functions register 1 is cleared; otherwise, its a write or
  1172. * sector select command and '4_bit_RX' must be set.
  1173. *
  1174. * When issuing an ISO 15693 command, inspect the flags byte to see
  1175. * what speed to use. Also, remember if the OPTION flag is set on
  1176. * a Type 5 write or lock command so the driver will know that it
  1177. * has to send an EOF in order to get a response.
  1178. */
  1179. if ((trf->technology == NFC_DIGITAL_RF_TECH_106A) &&
  1180. (trf->framing == NFC_DIGITAL_FRAMING_NFCA_T2T)) {
  1181. if (req[0] == NFC_T2T_CMD_READ)
  1182. special_fcn_reg1 = 0;
  1183. else
  1184. special_fcn_reg1 = TRF7970A_SPECIAL_FCN_REG1_4_BIT_RX;
  1185. if (special_fcn_reg1 != trf->special_fcn_reg1) {
  1186. ret = trf7970a_write(trf, TRF7970A_SPECIAL_FCN_REG1,
  1187. special_fcn_reg1);
  1188. if (ret)
  1189. return ret;
  1190. trf->special_fcn_reg1 = special_fcn_reg1;
  1191. }
  1192. } else if (trf->technology == NFC_DIGITAL_RF_TECH_ISO15693) {
  1193. iso_ctrl = trf->iso_ctrl & ~TRF7970A_ISO_CTRL_RFID_SPEED_MASK;
  1194. switch (req[0] & ISO15693_REQ_FLAG_SPEED_MASK) {
  1195. case 0x00:
  1196. iso_ctrl |= TRF7970A_ISO_CTRL_15693_SGL_1OF4_662;
  1197. break;
  1198. case ISO15693_REQ_FLAG_SUB_CARRIER:
  1199. iso_ctrl |= TRF7970A_ISO_CTRL_15693_DBL_1OF4_667a;
  1200. break;
  1201. case ISO15693_REQ_FLAG_DATA_RATE:
  1202. iso_ctrl |= TRF7970A_ISO_CTRL_15693_SGL_1OF4_2648;
  1203. break;
  1204. case (ISO15693_REQ_FLAG_SUB_CARRIER |
  1205. ISO15693_REQ_FLAG_DATA_RATE):
  1206. iso_ctrl |= TRF7970A_ISO_CTRL_15693_DBL_1OF4_2669;
  1207. break;
  1208. }
  1209. if (iso_ctrl != trf->iso_ctrl) {
  1210. ret = trf7970a_write(trf, TRF7970A_ISO_CTRL, iso_ctrl);
  1211. if (ret)
  1212. return ret;
  1213. trf->iso_ctrl = iso_ctrl;
  1214. }
  1215. if (trf->framing == NFC_DIGITAL_FRAMING_ISO15693_T5T) {
  1216. if (trf7970a_is_iso15693_write_or_lock(req[1]) &&
  1217. (req[0] & ISO15693_REQ_FLAG_OPTION))
  1218. trf->issue_eof = true;
  1219. else if ((trf->quirks &
  1220. TRF7970A_QUIRK_T5T_RMB_EXTRA_BYTE) &&
  1221. (req[1] == ISO15693_CMD_READ_MULTIPLE_BLOCK))
  1222. trf->adjust_resp_len = true;
  1223. }
  1224. }
  1225. return 0;
  1226. }
  1227. static int trf7970a_send_cmd(struct nfc_digital_dev *ddev,
  1228. struct sk_buff *skb, u16 timeout,
  1229. nfc_digital_cmd_complete_t cb, void *arg)
  1230. {
  1231. struct trf7970a *trf = nfc_digital_get_drvdata(ddev);
  1232. u8 prefix[5];
  1233. unsigned int len;
  1234. int ret;
  1235. u8 status;
  1236. dev_dbg(trf->dev, "New request - state: %d, timeout: %d ms, len: %d\n",
  1237. trf->state, timeout, skb->len);
  1238. if (skb->len > TRF7970A_TX_MAX)
  1239. return -EINVAL;
  1240. mutex_lock(&trf->lock);
  1241. if ((trf->state != TRF7970A_ST_IDLE) &&
  1242. (trf->state != TRF7970A_ST_IDLE_RX_BLOCKED)) {
  1243. dev_err(trf->dev, "%s - Bogus state: %d\n", __func__,
  1244. trf->state);
  1245. ret = -EIO;
  1246. goto out_err;
  1247. }
  1248. if (trf->aborting) {
  1249. dev_dbg(trf->dev, "Abort process complete\n");
  1250. trf->aborting = false;
  1251. ret = -ECANCELED;
  1252. goto out_err;
  1253. }
  1254. if (timeout) {
  1255. trf->rx_skb = nfc_alloc_recv_skb(TRF7970A_RX_SKB_ALLOC_SIZE,
  1256. GFP_KERNEL);
  1257. if (!trf->rx_skb) {
  1258. dev_dbg(trf->dev, "Can't alloc rx_skb\n");
  1259. ret = -ENOMEM;
  1260. goto out_err;
  1261. }
  1262. }
  1263. if (trf->state == TRF7970A_ST_IDLE_RX_BLOCKED) {
  1264. ret = trf7970a_cmd(trf, TRF7970A_CMD_ENABLE_RX);
  1265. if (ret)
  1266. goto out_err;
  1267. trf->state = TRF7970A_ST_IDLE;
  1268. }
  1269. if (trf->is_initiator) {
  1270. ret = trf7970a_per_cmd_config(trf, skb);
  1271. if (ret)
  1272. goto out_err;
  1273. }
  1274. trf->ddev = ddev;
  1275. trf->tx_skb = skb;
  1276. trf->cb = cb;
  1277. trf->cb_arg = arg;
  1278. trf->timeout = timeout;
  1279. trf->ignore_timeout = false;
  1280. len = skb->len;
  1281. /* TX data must be prefixed with a FIFO reset cmd, a cmd that depends
  1282. * on what the current framing is, the address of the TX length byte 1
  1283. * register (0x1d), and the 2 byte length of the data to be transmitted.
  1284. * That totals 5 bytes.
  1285. */
  1286. prefix[0] = TRF7970A_CMD_BIT_CTRL |
  1287. TRF7970A_CMD_BIT_OPCODE(TRF7970A_CMD_FIFO_RESET);
  1288. prefix[1] = TRF7970A_CMD_BIT_CTRL |
  1289. TRF7970A_CMD_BIT_OPCODE(trf->tx_cmd);
  1290. prefix[2] = TRF7970A_CMD_BIT_CONTINUOUS | TRF7970A_TX_LENGTH_BYTE1;
  1291. if (trf->framing == NFC_DIGITAL_FRAMING_NFCA_SHORT) {
  1292. prefix[3] = 0x00;
  1293. prefix[4] = 0x0f; /* 7 bits */
  1294. } else {
  1295. prefix[3] = (len & 0xf00) >> 4;
  1296. prefix[3] |= ((len & 0xf0) >> 4);
  1297. prefix[4] = ((len & 0x0f) << 4);
  1298. }
  1299. len = min_t(int, skb->len, TRF7970A_FIFO_SIZE);
  1300. /* Clear possible spurious interrupt */
  1301. ret = trf7970a_read_irqstatus(trf, &status);
  1302. if (ret)
  1303. goto out_err;
  1304. ret = trf7970a_transmit(trf, skb, len, prefix, sizeof(prefix));
  1305. if (ret) {
  1306. kfree_skb(trf->rx_skb);
  1307. trf->rx_skb = NULL;
  1308. }
  1309. out_err:
  1310. mutex_unlock(&trf->lock);
  1311. return ret;
  1312. }
  1313. static int trf7970a_tg_config_rf_tech(struct trf7970a *trf, int tech)
  1314. {
  1315. int ret = 0;
  1316. dev_dbg(trf->dev, "rf technology: %d\n", tech);
  1317. switch (tech) {
  1318. case NFC_DIGITAL_RF_TECH_106A:
  1319. trf->iso_ctrl_tech = TRF7970A_ISO_CTRL_NFC_NFC_CE_MODE |
  1320. TRF7970A_ISO_CTRL_NFC_CE |
  1321. TRF7970A_ISO_CTRL_NFC_CE_14443A;
  1322. trf->modulator_sys_clk_ctrl = TRF7970A_MODULATOR_DEPTH_OOK;
  1323. break;
  1324. case NFC_DIGITAL_RF_TECH_212F:
  1325. trf->iso_ctrl_tech = TRF7970A_ISO_CTRL_NFC_NFC_CE_MODE |
  1326. TRF7970A_ISO_CTRL_NFC_NFCF_212;
  1327. trf->modulator_sys_clk_ctrl = TRF7970A_MODULATOR_DEPTH_ASK10;
  1328. break;
  1329. case NFC_DIGITAL_RF_TECH_424F:
  1330. trf->iso_ctrl_tech = TRF7970A_ISO_CTRL_NFC_NFC_CE_MODE |
  1331. TRF7970A_ISO_CTRL_NFC_NFCF_424;
  1332. trf->modulator_sys_clk_ctrl = TRF7970A_MODULATOR_DEPTH_ASK10;
  1333. break;
  1334. default:
  1335. dev_dbg(trf->dev, "Unsupported rf technology: %d\n", tech);
  1336. return -EINVAL;
  1337. }
  1338. trf->technology = tech;
  1339. /* Normally we write the ISO_CTRL register in
  1340. * trf7970a_tg_config_framing() because the framing can change
  1341. * the value written. However, when sending a PSL RES,
  1342. * digital_tg_send_psl_res_complete() doesn't call
  1343. * trf7970a_tg_config_framing() so we must write the register
  1344. * here.
  1345. */
  1346. if ((trf->framing == NFC_DIGITAL_FRAMING_NFC_DEP_ACTIVATED) &&
  1347. (trf->iso_ctrl_tech != trf->iso_ctrl)) {
  1348. ret = trf7970a_write(trf, TRF7970A_ISO_CTRL,
  1349. trf->iso_ctrl_tech);
  1350. trf->iso_ctrl = trf->iso_ctrl_tech;
  1351. }
  1352. return ret;
  1353. }
  1354. /* Since this is a target routine, several of the framing calls are
  1355. * made between receiving the request and sending the response so they
  1356. * should take effect until after the response is sent. This is accomplished
  1357. * by skipping the ISO_CTRL register write here and doing it in the interrupt
  1358. * handler.
  1359. */
  1360. static int trf7970a_tg_config_framing(struct trf7970a *trf, int framing)
  1361. {
  1362. u8 iso_ctrl = trf->iso_ctrl_tech;
  1363. int ret;
  1364. dev_dbg(trf->dev, "framing: %d\n", framing);
  1365. switch (framing) {
  1366. case NFC_DIGITAL_FRAMING_NFCA_NFC_DEP:
  1367. trf->tx_cmd = TRF7970A_CMD_TRANSMIT_NO_CRC;
  1368. iso_ctrl |= TRF7970A_ISO_CTRL_RX_CRC_N;
  1369. break;
  1370. case NFC_DIGITAL_FRAMING_NFCA_STANDARD:
  1371. case NFC_DIGITAL_FRAMING_NFCA_STANDARD_WITH_CRC_A:
  1372. case NFC_DIGITAL_FRAMING_NFCA_ANTICOL_COMPLETE:
  1373. /* These ones are applied in the interrupt handler */
  1374. iso_ctrl = trf->iso_ctrl; /* Don't write to ISO_CTRL yet */
  1375. break;
  1376. case NFC_DIGITAL_FRAMING_NFCF_NFC_DEP:
  1377. trf->tx_cmd = TRF7970A_CMD_TRANSMIT;
  1378. iso_ctrl &= ~TRF7970A_ISO_CTRL_RX_CRC_N;
  1379. break;
  1380. case NFC_DIGITAL_FRAMING_NFC_DEP_ACTIVATED:
  1381. trf->tx_cmd = TRF7970A_CMD_TRANSMIT;
  1382. iso_ctrl &= ~TRF7970A_ISO_CTRL_RX_CRC_N;
  1383. break;
  1384. default:
  1385. dev_dbg(trf->dev, "Unsupported Framing: %d\n", framing);
  1386. return -EINVAL;
  1387. }
  1388. trf->framing = framing;
  1389. if (iso_ctrl != trf->iso_ctrl) {
  1390. ret = trf7970a_write(trf, TRF7970A_ISO_CTRL, iso_ctrl);
  1391. if (ret)
  1392. return ret;
  1393. trf->iso_ctrl = iso_ctrl;
  1394. ret = trf7970a_write(trf, TRF7970A_MODULATOR_SYS_CLK_CTRL,
  1395. trf->modulator_sys_clk_ctrl);
  1396. if (ret)
  1397. return ret;
  1398. }
  1399. if (!(trf->chip_status_ctrl & TRF7970A_CHIP_STATUS_RF_ON)) {
  1400. ret = trf7970a_write(trf, TRF7970A_CHIP_STATUS_CTRL,
  1401. trf->chip_status_ctrl |
  1402. TRF7970A_CHIP_STATUS_RF_ON);
  1403. if (ret)
  1404. return ret;
  1405. trf->chip_status_ctrl |= TRF7970A_CHIP_STATUS_RF_ON;
  1406. }
  1407. return 0;
  1408. }
  1409. static int trf7970a_tg_configure_hw(struct nfc_digital_dev *ddev, int type,
  1410. int param)
  1411. {
  1412. struct trf7970a *trf = nfc_digital_get_drvdata(ddev);
  1413. int ret;
  1414. dev_dbg(trf->dev, "Configure hw - type: %d, param: %d\n", type, param);
  1415. mutex_lock(&trf->lock);
  1416. trf->is_initiator = false;
  1417. if ((trf->state == TRF7970A_ST_PWR_OFF) ||
  1418. (trf->state == TRF7970A_ST_RF_OFF)) {
  1419. ret = trf7970a_switch_rf_on(trf);
  1420. if (ret)
  1421. goto err_unlock;
  1422. }
  1423. switch (type) {
  1424. case NFC_DIGITAL_CONFIG_RF_TECH:
  1425. ret = trf7970a_tg_config_rf_tech(trf, param);
  1426. break;
  1427. case NFC_DIGITAL_CONFIG_FRAMING:
  1428. ret = trf7970a_tg_config_framing(trf, param);
  1429. break;
  1430. default:
  1431. dev_dbg(trf->dev, "Unknown type: %d\n", type);
  1432. ret = -EINVAL;
  1433. }
  1434. err_unlock:
  1435. mutex_unlock(&trf->lock);
  1436. return ret;
  1437. }
  1438. static int _trf7970a_tg_listen(struct nfc_digital_dev *ddev, u16 timeout,
  1439. nfc_digital_cmd_complete_t cb, void *arg, bool mode_detect)
  1440. {
  1441. struct trf7970a *trf = nfc_digital_get_drvdata(ddev);
  1442. int ret;
  1443. mutex_lock(&trf->lock);
  1444. if ((trf->state != TRF7970A_ST_IDLE) &&
  1445. (trf->state != TRF7970A_ST_IDLE_RX_BLOCKED)) {
  1446. dev_err(trf->dev, "%s - Bogus state: %d\n", __func__,
  1447. trf->state);
  1448. ret = -EIO;
  1449. goto out_err;
  1450. }
  1451. if (trf->aborting) {
  1452. dev_dbg(trf->dev, "Abort process complete\n");
  1453. trf->aborting = false;
  1454. ret = -ECANCELED;
  1455. goto out_err;
  1456. }
  1457. trf->rx_skb = nfc_alloc_recv_skb(TRF7970A_RX_SKB_ALLOC_SIZE,
  1458. GFP_KERNEL);
  1459. if (!trf->rx_skb) {
  1460. dev_dbg(trf->dev, "Can't alloc rx_skb\n");
  1461. ret = -ENOMEM;
  1462. goto out_err;
  1463. }
  1464. ret = trf7970a_write(trf, TRF7970A_RX_SPECIAL_SETTINGS,
  1465. TRF7970A_RX_SPECIAL_SETTINGS_HBT |
  1466. TRF7970A_RX_SPECIAL_SETTINGS_M848 |
  1467. TRF7970A_RX_SPECIAL_SETTINGS_C424 |
  1468. TRF7970A_RX_SPECIAL_SETTINGS_C212);
  1469. if (ret)
  1470. goto out_err;
  1471. ret = trf7970a_write(trf, TRF7970A_REG_IO_CTRL,
  1472. TRF7970A_REG_IO_CTRL_VRS(0x1));
  1473. if (ret)
  1474. goto out_err;
  1475. ret = trf7970a_write(trf, TRF7970A_NFC_LOW_FIELD_LEVEL,
  1476. TRF7970A_NFC_LOW_FIELD_LEVEL_RFDET(0x3));
  1477. if (ret)
  1478. goto out_err;
  1479. ret = trf7970a_write(trf, TRF7970A_NFC_TARGET_LEVEL,
  1480. TRF7970A_NFC_TARGET_LEVEL_RFDET(0x7));
  1481. if (ret)
  1482. goto out_err;
  1483. trf->ddev = ddev;
  1484. trf->cb = cb;
  1485. trf->cb_arg = arg;
  1486. trf->timeout = timeout;
  1487. trf->ignore_timeout = false;
  1488. ret = trf7970a_cmd(trf, TRF7970A_CMD_ENABLE_RX);
  1489. if (ret)
  1490. goto out_err;
  1491. trf->state = mode_detect ? TRF7970A_ST_LISTENING_MD :
  1492. TRF7970A_ST_LISTENING;
  1493. schedule_delayed_work(&trf->timeout_work, msecs_to_jiffies(timeout));
  1494. out_err:
  1495. mutex_unlock(&trf->lock);
  1496. return ret;
  1497. }
  1498. static int trf7970a_tg_listen(struct nfc_digital_dev *ddev, u16 timeout,
  1499. nfc_digital_cmd_complete_t cb, void *arg)
  1500. {
  1501. struct trf7970a *trf = nfc_digital_get_drvdata(ddev);
  1502. dev_dbg(trf->dev, "Listen - state: %d, timeout: %d ms\n",
  1503. trf->state, timeout);
  1504. return _trf7970a_tg_listen(ddev, timeout, cb, arg, false);
  1505. }
  1506. static int trf7970a_tg_listen_md(struct nfc_digital_dev *ddev,
  1507. u16 timeout, nfc_digital_cmd_complete_t cb, void *arg)
  1508. {
  1509. struct trf7970a *trf = nfc_digital_get_drvdata(ddev);
  1510. int ret;
  1511. dev_dbg(trf->dev, "Listen MD - state: %d, timeout: %d ms\n",
  1512. trf->state, timeout);
  1513. ret = trf7970a_tg_configure_hw(ddev, NFC_DIGITAL_CONFIG_RF_TECH,
  1514. NFC_DIGITAL_RF_TECH_106A);
  1515. if (ret)
  1516. return ret;
  1517. ret = trf7970a_tg_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING,
  1518. NFC_DIGITAL_FRAMING_NFCA_NFC_DEP);
  1519. if (ret)
  1520. return ret;
  1521. return _trf7970a_tg_listen(ddev, timeout, cb, arg, true);
  1522. }
  1523. static int trf7970a_tg_get_rf_tech(struct nfc_digital_dev *ddev, u8 *rf_tech)
  1524. {
  1525. struct trf7970a *trf = nfc_digital_get_drvdata(ddev);
  1526. dev_dbg(trf->dev, "Get RF Tech - state: %d, rf_tech: %d\n",
  1527. trf->state, trf->md_rf_tech);
  1528. *rf_tech = trf->md_rf_tech;
  1529. return 0;
  1530. }
  1531. static void trf7970a_abort_cmd(struct nfc_digital_dev *ddev)
  1532. {
  1533. struct trf7970a *trf = nfc_digital_get_drvdata(ddev);
  1534. dev_dbg(trf->dev, "Abort process initiated\n");
  1535. mutex_lock(&trf->lock);
  1536. switch (trf->state) {
  1537. case TRF7970A_ST_WAIT_FOR_TX_FIFO:
  1538. case TRF7970A_ST_WAIT_FOR_RX_DATA:
  1539. case TRF7970A_ST_WAIT_FOR_RX_DATA_CONT:
  1540. case TRF7970A_ST_WAIT_TO_ISSUE_EOF:
  1541. trf->aborting = true;
  1542. break;
  1543. case TRF7970A_ST_LISTENING:
  1544. trf->ignore_timeout = !cancel_delayed_work(&trf->timeout_work);
  1545. trf7970a_send_err_upstream(trf, -ECANCELED);
  1546. dev_dbg(trf->dev, "Abort process complete\n");
  1547. break;
  1548. default:
  1549. break;
  1550. }
  1551. mutex_unlock(&trf->lock);
  1552. }
  1553. static struct nfc_digital_ops trf7970a_nfc_ops = {
  1554. .in_configure_hw = trf7970a_in_configure_hw,
  1555. .in_send_cmd = trf7970a_send_cmd,
  1556. .tg_configure_hw = trf7970a_tg_configure_hw,
  1557. .tg_send_cmd = trf7970a_send_cmd,
  1558. .tg_listen = trf7970a_tg_listen,
  1559. .tg_listen_md = trf7970a_tg_listen_md,
  1560. .tg_get_rf_tech = trf7970a_tg_get_rf_tech,
  1561. .switch_rf = trf7970a_switch_rf,
  1562. .abort_cmd = trf7970a_abort_cmd,
  1563. };
  1564. static int trf7970a_power_up(struct trf7970a *trf)
  1565. {
  1566. int ret;
  1567. dev_dbg(trf->dev, "Powering up - state: %d\n", trf->state);
  1568. if (trf->state != TRF7970A_ST_PWR_OFF)
  1569. return 0;
  1570. ret = regulator_enable(trf->regulator);
  1571. if (ret) {
  1572. dev_err(trf->dev, "%s - Can't enable VIN: %d\n", __func__, ret);
  1573. return ret;
  1574. }
  1575. usleep_range(5000, 6000);
  1576. if (!(trf->quirks & TRF7970A_QUIRK_EN2_MUST_STAY_LOW)) {
  1577. gpio_set_value(trf->en2_gpio, 1);
  1578. usleep_range(1000, 2000);
  1579. }
  1580. gpio_set_value(trf->en_gpio, 1);
  1581. usleep_range(20000, 21000);
  1582. trf->state = TRF7970A_ST_RF_OFF;
  1583. return 0;
  1584. }
  1585. static int trf7970a_power_down(struct trf7970a *trf)
  1586. {
  1587. int ret;
  1588. dev_dbg(trf->dev, "Powering down - state: %d\n", trf->state);
  1589. if (trf->state == TRF7970A_ST_PWR_OFF)
  1590. return 0;
  1591. if (trf->state != TRF7970A_ST_RF_OFF) {
  1592. dev_dbg(trf->dev, "Can't power down - not RF_OFF state (%d)\n",
  1593. trf->state);
  1594. return -EBUSY;
  1595. }
  1596. gpio_set_value(trf->en_gpio, 0);
  1597. gpio_set_value(trf->en2_gpio, 0);
  1598. ret = regulator_disable(trf->regulator);
  1599. if (ret)
  1600. dev_err(trf->dev, "%s - Can't disable VIN: %d\n", __func__,
  1601. ret);
  1602. trf->state = TRF7970A_ST_PWR_OFF;
  1603. return ret;
  1604. }
  1605. static int trf7970a_startup(struct trf7970a *trf)
  1606. {
  1607. int ret;
  1608. ret = trf7970a_power_up(trf);
  1609. if (ret)
  1610. return ret;
  1611. pm_runtime_set_active(trf->dev);
  1612. pm_runtime_enable(trf->dev);
  1613. pm_runtime_mark_last_busy(trf->dev);
  1614. return 0;
  1615. }
  1616. static void trf7970a_shutdown(struct trf7970a *trf)
  1617. {
  1618. switch (trf->state) {
  1619. case TRF7970A_ST_WAIT_FOR_TX_FIFO:
  1620. case TRF7970A_ST_WAIT_FOR_RX_DATA:
  1621. case TRF7970A_ST_WAIT_FOR_RX_DATA_CONT:
  1622. case TRF7970A_ST_WAIT_TO_ISSUE_EOF:
  1623. case TRF7970A_ST_LISTENING:
  1624. trf7970a_send_err_upstream(trf, -ECANCELED);
  1625. /* FALLTHROUGH */
  1626. case TRF7970A_ST_IDLE:
  1627. case TRF7970A_ST_IDLE_RX_BLOCKED:
  1628. trf7970a_switch_rf_off(trf);
  1629. break;
  1630. default:
  1631. break;
  1632. }
  1633. pm_runtime_disable(trf->dev);
  1634. pm_runtime_set_suspended(trf->dev);
  1635. trf7970a_power_down(trf);
  1636. }
  1637. static int trf7970a_get_autosuspend_delay(struct device_node *np)
  1638. {
  1639. int autosuspend_delay, ret;
  1640. ret = of_property_read_u32(np, "autosuspend-delay", &autosuspend_delay);
  1641. if (ret)
  1642. autosuspend_delay = TRF7970A_AUTOSUSPEND_DELAY;
  1643. return autosuspend_delay;
  1644. }
  1645. static int trf7970a_get_vin_voltage_override(struct device_node *np,
  1646. u32 *vin_uvolts)
  1647. {
  1648. return of_property_read_u32(np, "vin-voltage-override", vin_uvolts);
  1649. }
  1650. static int trf7970a_probe(struct spi_device *spi)
  1651. {
  1652. struct device_node *np = spi->dev.of_node;
  1653. struct trf7970a *trf;
  1654. int uvolts, autosuspend_delay, ret;
  1655. if (!np) {
  1656. dev_err(&spi->dev, "No Device Tree entry\n");
  1657. return -EINVAL;
  1658. }
  1659. trf = devm_kzalloc(&spi->dev, sizeof(*trf), GFP_KERNEL);
  1660. if (!trf)
  1661. return -ENOMEM;
  1662. trf->state = TRF7970A_ST_PWR_OFF;
  1663. trf->dev = &spi->dev;
  1664. trf->spi = spi;
  1665. spi->mode = SPI_MODE_1;
  1666. spi->bits_per_word = 8;
  1667. ret = spi_setup(spi);
  1668. if (ret < 0) {
  1669. dev_err(trf->dev, "Can't set up SPI Communication\n");
  1670. return ret;
  1671. }
  1672. if (of_property_read_bool(np, "t5t-rmb-extra-byte-quirk"))
  1673. trf->quirks |= TRF7970A_QUIRK_T5T_RMB_EXTRA_BYTE;
  1674. if (of_property_read_bool(np, "irq-status-read-quirk"))
  1675. trf->quirks |= TRF7970A_QUIRK_IRQ_STATUS_READ;
  1676. /* There are two enable pins - both must be present */
  1677. trf->en_gpio = of_get_named_gpio(np, "ti,enable-gpios", 0);
  1678. if (!gpio_is_valid(trf->en_gpio)) {
  1679. dev_err(trf->dev, "No EN GPIO property\n");
  1680. return trf->en_gpio;
  1681. }
  1682. ret = devm_gpio_request_one(trf->dev, trf->en_gpio,
  1683. GPIOF_DIR_OUT | GPIOF_INIT_LOW, "trf7970a EN");
  1684. if (ret) {
  1685. dev_err(trf->dev, "Can't request EN GPIO: %d\n", ret);
  1686. return ret;
  1687. }
  1688. trf->en2_gpio = of_get_named_gpio(np, "ti,enable-gpios", 1);
  1689. if (!gpio_is_valid(trf->en2_gpio)) {
  1690. dev_err(trf->dev, "No EN2 GPIO property\n");
  1691. return trf->en2_gpio;
  1692. }
  1693. ret = devm_gpio_request_one(trf->dev, trf->en2_gpio,
  1694. GPIOF_DIR_OUT | GPIOF_INIT_LOW, "trf7970a EN2");
  1695. if (ret) {
  1696. dev_err(trf->dev, "Can't request EN2 GPIO: %d\n", ret);
  1697. return ret;
  1698. }
  1699. if (of_property_read_bool(np, "en2-rf-quirk"))
  1700. trf->quirks |= TRF7970A_QUIRK_EN2_MUST_STAY_LOW;
  1701. ret = devm_request_threaded_irq(trf->dev, spi->irq, NULL,
  1702. trf7970a_irq, IRQF_TRIGGER_RISING | IRQF_ONESHOT,
  1703. "trf7970a", trf);
  1704. if (ret) {
  1705. dev_err(trf->dev, "Can't request IRQ#%d: %d\n", spi->irq, ret);
  1706. return ret;
  1707. }
  1708. mutex_init(&trf->lock);
  1709. INIT_DELAYED_WORK(&trf->timeout_work, trf7970a_timeout_work_handler);
  1710. trf->regulator = devm_regulator_get(&spi->dev, "vin");
  1711. if (IS_ERR(trf->regulator)) {
  1712. ret = PTR_ERR(trf->regulator);
  1713. dev_err(trf->dev, "Can't get VIN regulator: %d\n", ret);
  1714. goto err_destroy_lock;
  1715. }
  1716. ret = regulator_enable(trf->regulator);
  1717. if (ret) {
  1718. dev_err(trf->dev, "Can't enable VIN: %d\n", ret);
  1719. goto err_destroy_lock;
  1720. }
  1721. ret = trf7970a_get_vin_voltage_override(np, &uvolts);
  1722. if (ret)
  1723. uvolts = regulator_get_voltage(trf->regulator);
  1724. if (uvolts > 4000000)
  1725. trf->chip_status_ctrl = TRF7970A_CHIP_STATUS_VRS5_3;
  1726. trf->ddev = nfc_digital_allocate_device(&trf7970a_nfc_ops,
  1727. TRF7970A_SUPPORTED_PROTOCOLS,
  1728. NFC_DIGITAL_DRV_CAPS_IN_CRC |
  1729. NFC_DIGITAL_DRV_CAPS_TG_CRC, 0, 0);
  1730. if (!trf->ddev) {
  1731. dev_err(trf->dev, "Can't allocate NFC digital device\n");
  1732. ret = -ENOMEM;
  1733. goto err_disable_regulator;
  1734. }
  1735. nfc_digital_set_parent_dev(trf->ddev, trf->dev);
  1736. nfc_digital_set_drvdata(trf->ddev, trf);
  1737. spi_set_drvdata(spi, trf);
  1738. autosuspend_delay = trf7970a_get_autosuspend_delay(np);
  1739. pm_runtime_set_autosuspend_delay(trf->dev, autosuspend_delay);
  1740. pm_runtime_use_autosuspend(trf->dev);
  1741. ret = trf7970a_startup(trf);
  1742. if (ret)
  1743. goto err_free_ddev;
  1744. ret = nfc_digital_register_device(trf->ddev);
  1745. if (ret) {
  1746. dev_err(trf->dev, "Can't register NFC digital device: %d\n",
  1747. ret);
  1748. goto err_shutdown;
  1749. }
  1750. return 0;
  1751. err_shutdown:
  1752. trf7970a_shutdown(trf);
  1753. err_free_ddev:
  1754. nfc_digital_free_device(trf->ddev);
  1755. err_disable_regulator:
  1756. regulator_disable(trf->regulator);
  1757. err_destroy_lock:
  1758. mutex_destroy(&trf->lock);
  1759. return ret;
  1760. }
  1761. static int trf7970a_remove(struct spi_device *spi)
  1762. {
  1763. struct trf7970a *trf = spi_get_drvdata(spi);
  1764. mutex_lock(&trf->lock);
  1765. trf7970a_shutdown(trf);
  1766. mutex_unlock(&trf->lock);
  1767. nfc_digital_unregister_device(trf->ddev);
  1768. nfc_digital_free_device(trf->ddev);
  1769. regulator_disable(trf->regulator);
  1770. mutex_destroy(&trf->lock);
  1771. return 0;
  1772. }
  1773. #ifdef CONFIG_PM_SLEEP
  1774. static int trf7970a_suspend(struct device *dev)
  1775. {
  1776. struct spi_device *spi = to_spi_device(dev);
  1777. struct trf7970a *trf = spi_get_drvdata(spi);
  1778. dev_dbg(dev, "Suspend\n");
  1779. mutex_lock(&trf->lock);
  1780. trf7970a_shutdown(trf);
  1781. mutex_unlock(&trf->lock);
  1782. return 0;
  1783. }
  1784. static int trf7970a_resume(struct device *dev)
  1785. {
  1786. struct spi_device *spi = to_spi_device(dev);
  1787. struct trf7970a *trf = spi_get_drvdata(spi);
  1788. int ret;
  1789. dev_dbg(dev, "Resume\n");
  1790. mutex_lock(&trf->lock);
  1791. ret = trf7970a_startup(trf);
  1792. mutex_unlock(&trf->lock);
  1793. return ret;
  1794. }
  1795. #endif
  1796. #ifdef CONFIG_PM
  1797. static int trf7970a_pm_runtime_suspend(struct device *dev)
  1798. {
  1799. struct spi_device *spi = to_spi_device(dev);
  1800. struct trf7970a *trf = spi_get_drvdata(spi);
  1801. int ret;
  1802. dev_dbg(dev, "Runtime suspend\n");
  1803. mutex_lock(&trf->lock);
  1804. ret = trf7970a_power_down(trf);
  1805. mutex_unlock(&trf->lock);
  1806. return ret;
  1807. }
  1808. static int trf7970a_pm_runtime_resume(struct device *dev)
  1809. {
  1810. struct spi_device *spi = to_spi_device(dev);
  1811. struct trf7970a *trf = spi_get_drvdata(spi);
  1812. int ret;
  1813. dev_dbg(dev, "Runtime resume\n");
  1814. ret = trf7970a_power_up(trf);
  1815. if (!ret)
  1816. pm_runtime_mark_last_busy(dev);
  1817. return ret;
  1818. }
  1819. #endif
  1820. static const struct dev_pm_ops trf7970a_pm_ops = {
  1821. SET_SYSTEM_SLEEP_PM_OPS(trf7970a_suspend, trf7970a_resume)
  1822. SET_RUNTIME_PM_OPS(trf7970a_pm_runtime_suspend,
  1823. trf7970a_pm_runtime_resume, NULL)
  1824. };
  1825. static const struct of_device_id trf7970a_of_match[] = {
  1826. { .compatible = "ti,trf7970a", },
  1827. { /* sentinel */ },
  1828. };
  1829. MODULE_DEVICE_TABLE(of, trf7970a_of_match);
  1830. static const struct spi_device_id trf7970a_id_table[] = {
  1831. { "trf7970a", 0 },
  1832. { }
  1833. };
  1834. MODULE_DEVICE_TABLE(spi, trf7970a_id_table);
  1835. static struct spi_driver trf7970a_spi_driver = {
  1836. .probe = trf7970a_probe,
  1837. .remove = trf7970a_remove,
  1838. .id_table = trf7970a_id_table,
  1839. .driver = {
  1840. .name = "trf7970a",
  1841. .of_match_table = of_match_ptr(trf7970a_of_match),
  1842. .pm = &trf7970a_pm_ops,
  1843. },
  1844. };
  1845. module_spi_driver(trf7970a_spi_driver);
  1846. MODULE_AUTHOR("Mark A. Greer <mgreer@animalcreek.com>");
  1847. MODULE_LICENSE("GPL v2");
  1848. MODULE_DESCRIPTION("TI trf7970a RFID/NFC Transceiver Driver");