trf7970a.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493
  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.
  37. *
  38. * Timeouts are implemented using the delayed workqueue kernel facility.
  39. * Timeouts are required so things don't hang when there is no response
  40. * from the trf7970a (or tag). Using this mechanism creates a race with
  41. * interrupts, however. That is, an interrupt and a timeout could occur
  42. * closely enough together that one is blocked by the mutex while the other
  43. * executes. When the timeout handler executes first and blocks the
  44. * interrupt handler, it will eventually set the state to IDLE so the
  45. * interrupt handler will check the state and exit with no harm done.
  46. * When the interrupt handler executes first and blocks the timeout handler,
  47. * the cancel_delayed_work() call will know that it didn't cancel the
  48. * work item (i.e., timeout) and will return zero. That return code is
  49. * used by the timer handler to indicate that it should ignore the timeout
  50. * once its unblocked.
  51. *
  52. * Aborting an active command isn't as simple as it seems because the only
  53. * way to abort a command that's already been sent to the tag is so turn
  54. * off power to the tag. If we do that, though, we'd have to go through
  55. * the entire anticollision procedure again but the digital layer doesn't
  56. * support that. So, if an abort is received before trf7970a_in_send_cmd()
  57. * has sent the command to the tag, it simply returns -ECANCELED. If the
  58. * command has already been sent to the tag, then the driver continues
  59. * normally and recieves the response data (or error) but just before
  60. * sending the data upstream, it frees the rx_skb and sends -ECANCELED
  61. * upstream instead. If the command failed, that error will be sent
  62. * upstream.
  63. *
  64. * When recieving data from a tag and the interrupt status register has
  65. * only the SRX bit set, it means that all of the data has been received
  66. * (once what's in the fifo has been read). However, depending on timing
  67. * an interrupt status with only the SRX bit set may not be recived. In
  68. * those cases, the timeout mechanism is used to wait 20 ms in case more
  69. * data arrives. After 20 ms, it is assumed that all of the data has been
  70. * received and the accumulated rx data is sent upstream. The
  71. * 'TRF7970A_ST_WAIT_FOR_RX_DATA_CONT' state is used for this purpose
  72. * (i.e., it indicates that some data has been received but we're not sure
  73. * if there is more coming so a timeout in this state means all data has
  74. * been received and there isn't an error). The delay is 20 ms since delays
  75. * of ~16 ms have been observed during testing.
  76. *
  77. * Type 2 write and sector select commands respond with a 4-bit ACK or NACK.
  78. * Having only 4 bits in the FIFO won't normally generate an interrupt so
  79. * driver enables the '4_bit_RX' bit of the Special Functions register 1
  80. * to cause an interrupt in that case. Leaving that bit for a read command
  81. * messes up the data returned so it is only enabled when the framing is
  82. * 'NFC_DIGITAL_FRAMING_NFCA_T2T' and the command is not a read command.
  83. * Unfortunately, that means that the driver has to peek into tx frames
  84. * when the framing is 'NFC_DIGITAL_FRAMING_NFCA_T2T'. This is done by
  85. * the trf7970a_per_cmd_config() routine.
  86. *
  87. * ISO/IEC 15693 frames specify whether to use single or double sub-carrier
  88. * frequencies and whether to use low or high data rates in the flags byte
  89. * of the frame. This means that the driver has to peek at all 15693 frames
  90. * to determine what speed to set the communication to. In addition, write
  91. * and lock commands use the OPTION flag to indicate that an EOF must be
  92. * sent to the tag before it will send its response. So the driver has to
  93. * examine all frames for that reason too.
  94. *
  95. * It is unclear how long to wait before sending the EOF. According to the
  96. * Note under Table 1-1 in section 1.6 of
  97. * http://www.ti.com/lit/ug/scbu011/scbu011.pdf, that wait should be at least
  98. * 10 ms for TI Tag-it HF-I tags; however testing has shown that is not long
  99. * enough. For this reason, the driver waits 20 ms which seems to work
  100. * reliably.
  101. */
  102. #define TRF7970A_SUPPORTED_PROTOCOLS \
  103. (NFC_PROTO_MIFARE_MASK | NFC_PROTO_ISO14443_MASK | \
  104. NFC_PROTO_ISO14443_B_MASK | NFC_PROTO_FELICA_MASK | \
  105. NFC_PROTO_ISO15693_MASK)
  106. #define TRF7970A_AUTOSUSPEND_DELAY 30000 /* 30 seconds */
  107. /* TX data must be prefixed with a FIFO reset cmd, a cmd that depends
  108. * on what the current framing is, the address of the TX length byte 1
  109. * register (0x1d), and the 2 byte length of the data to be transmitted.
  110. * That totals 5 bytes.
  111. */
  112. #define TRF7970A_TX_SKB_HEADROOM 5
  113. #define TRF7970A_RX_SKB_ALLOC_SIZE 256
  114. #define TRF7970A_FIFO_SIZE 128
  115. /* TX length is 3 nibbles long ==> 4KB - 1 bytes max */
  116. #define TRF7970A_TX_MAX (4096 - 1)
  117. #define TRF7970A_WAIT_FOR_RX_DATA_TIMEOUT 20
  118. #define TRF7970A_WAIT_FOR_FIFO_DRAIN_TIMEOUT 3
  119. #define TRF7970A_WAIT_TO_ISSUE_ISO15693_EOF 20
  120. /* Quirks */
  121. /* Erratum: When reading IRQ Status register on trf7970a, we must issue a
  122. * read continuous command for IRQ Status and Collision Position registers.
  123. */
  124. #define TRF7970A_QUIRK_IRQ_STATUS_READ_ERRATA BIT(0)
  125. /* Direct commands */
  126. #define TRF7970A_CMD_IDLE 0x00
  127. #define TRF7970A_CMD_SOFT_INIT 0x03
  128. #define TRF7970A_CMD_RF_COLLISION 0x04
  129. #define TRF7970A_CMD_RF_COLLISION_RESPONSE_N 0x05
  130. #define TRF7970A_CMD_RF_COLLISION_RESPONSE_0 0x06
  131. #define TRF7970A_CMD_FIFO_RESET 0x0f
  132. #define TRF7970A_CMD_TRANSMIT_NO_CRC 0x10
  133. #define TRF7970A_CMD_TRANSMIT 0x11
  134. #define TRF7970A_CMD_DELAY_TRANSMIT_NO_CRC 0x12
  135. #define TRF7970A_CMD_DELAY_TRANSMIT 0x13
  136. #define TRF7970A_CMD_EOF 0x14
  137. #define TRF7970A_CMD_CLOSE_SLOT 0x15
  138. #define TRF7970A_CMD_BLOCK_RX 0x16
  139. #define TRF7970A_CMD_ENABLE_RX 0x17
  140. #define TRF7970A_CMD_TEST_EXT_RF 0x18
  141. #define TRF7970A_CMD_TEST_INT_RF 0x19
  142. #define TRF7970A_CMD_RX_GAIN_ADJUST 0x1a
  143. /* Bits determining whether its a direct command or register R/W,
  144. * whether to use a continuous SPI transaction or not, and the actual
  145. * direct cmd opcode or regster address.
  146. */
  147. #define TRF7970A_CMD_BIT_CTRL BIT(7)
  148. #define TRF7970A_CMD_BIT_RW BIT(6)
  149. #define TRF7970A_CMD_BIT_CONTINUOUS BIT(5)
  150. #define TRF7970A_CMD_BIT_OPCODE(opcode) ((opcode) & 0x1f)
  151. /* Registers addresses */
  152. #define TRF7970A_CHIP_STATUS_CTRL 0x00
  153. #define TRF7970A_ISO_CTRL 0x01
  154. #define TRF7970A_ISO14443B_TX_OPTIONS 0x02
  155. #define TRF7970A_ISO14443A_HIGH_BITRATE_OPTIONS 0x03
  156. #define TRF7970A_TX_TIMER_SETTING_H_BYTE 0x04
  157. #define TRF7970A_TX_TIMER_SETTING_L_BYTE 0x05
  158. #define TRF7970A_TX_PULSE_LENGTH_CTRL 0x06
  159. #define TRF7970A_RX_NO_RESPONSE_WAIT 0x07
  160. #define TRF7970A_RX_WAIT_TIME 0x08
  161. #define TRF7970A_MODULATOR_SYS_CLK_CTRL 0x09
  162. #define TRF7970A_RX_SPECIAL_SETTINGS 0x0a
  163. #define TRF7970A_REG_IO_CTRL 0x0b
  164. #define TRF7970A_IRQ_STATUS 0x0c
  165. #define TRF7970A_COLLISION_IRQ_MASK 0x0d
  166. #define TRF7970A_COLLISION_POSITION 0x0e
  167. #define TRF7970A_RSSI_OSC_STATUS 0x0f
  168. #define TRF7970A_SPECIAL_FCN_REG1 0x10
  169. #define TRF7970A_SPECIAL_FCN_REG2 0x11
  170. #define TRF7970A_RAM1 0x12
  171. #define TRF7970A_RAM2 0x13
  172. #define TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS 0x14
  173. #define TRF7970A_NFC_LOW_FIELD_LEVEL 0x16
  174. #define TRF7970A_NFCID1 0x17
  175. #define TRF7970A_NFC_TARGET_LEVEL 0x18
  176. #define TRF79070A_NFC_TARGET_PROTOCOL 0x19
  177. #define TRF7970A_TEST_REGISTER1 0x1a
  178. #define TRF7970A_TEST_REGISTER2 0x1b
  179. #define TRF7970A_FIFO_STATUS 0x1c
  180. #define TRF7970A_TX_LENGTH_BYTE1 0x1d
  181. #define TRF7970A_TX_LENGTH_BYTE2 0x1e
  182. #define TRF7970A_FIFO_IO_REGISTER 0x1f
  183. /* Chip Status Control Register Bits */
  184. #define TRF7970A_CHIP_STATUS_VRS5_3 BIT(0)
  185. #define TRF7970A_CHIP_STATUS_REC_ON BIT(1)
  186. #define TRF7970A_CHIP_STATUS_AGC_ON BIT(2)
  187. #define TRF7970A_CHIP_STATUS_PM_ON BIT(3)
  188. #define TRF7970A_CHIP_STATUS_RF_PWR BIT(4)
  189. #define TRF7970A_CHIP_STATUS_RF_ON BIT(5)
  190. #define TRF7970A_CHIP_STATUS_DIRECT BIT(6)
  191. #define TRF7970A_CHIP_STATUS_STBY BIT(7)
  192. /* ISO Control Register Bits */
  193. #define TRF7970A_ISO_CTRL_15693_SGL_1OF4_662 0x00
  194. #define TRF7970A_ISO_CTRL_15693_SGL_1OF256_662 0x01
  195. #define TRF7970A_ISO_CTRL_15693_SGL_1OF4_2648 0x02
  196. #define TRF7970A_ISO_CTRL_15693_SGL_1OF256_2648 0x03
  197. #define TRF7970A_ISO_CTRL_15693_DBL_1OF4_667a 0x04
  198. #define TRF7970A_ISO_CTRL_15693_DBL_1OF256_667 0x05
  199. #define TRF7970A_ISO_CTRL_15693_DBL_1OF4_2669 0x06
  200. #define TRF7970A_ISO_CTRL_15693_DBL_1OF256_2669 0x07
  201. #define TRF7970A_ISO_CTRL_14443A_106 0x08
  202. #define TRF7970A_ISO_CTRL_14443A_212 0x09
  203. #define TRF7970A_ISO_CTRL_14443A_424 0x0a
  204. #define TRF7970A_ISO_CTRL_14443A_848 0x0b
  205. #define TRF7970A_ISO_CTRL_14443B_106 0x0c
  206. #define TRF7970A_ISO_CTRL_14443B_212 0x0d
  207. #define TRF7970A_ISO_CTRL_14443B_424 0x0e
  208. #define TRF7970A_ISO_CTRL_14443B_848 0x0f
  209. #define TRF7970A_ISO_CTRL_FELICA_212 0x1a
  210. #define TRF7970A_ISO_CTRL_FELICA_424 0x1b
  211. #define TRF7970A_ISO_CTRL_RFID BIT(5)
  212. #define TRF7970A_ISO_CTRL_DIR_MODE BIT(6)
  213. #define TRF7970A_ISO_CTRL_RX_CRC_N BIT(7) /* true == No CRC */
  214. #define TRF7970A_ISO_CTRL_RFID_SPEED_MASK 0x1f
  215. /* Modulator and SYS_CLK Control Register Bits */
  216. #define TRF7970A_MODULATOR_DEPTH(n) ((n) & 0x7)
  217. #define TRF7970A_MODULATOR_DEPTH_ASK10 (TRF7970A_MODULATOR_DEPTH(0))
  218. #define TRF7970A_MODULATOR_DEPTH_OOK (TRF7970A_MODULATOR_DEPTH(1))
  219. #define TRF7970A_MODULATOR_DEPTH_ASK7 (TRF7970A_MODULATOR_DEPTH(2))
  220. #define TRF7970A_MODULATOR_DEPTH_ASK8_5 (TRF7970A_MODULATOR_DEPTH(3))
  221. #define TRF7970A_MODULATOR_DEPTH_ASK13 (TRF7970A_MODULATOR_DEPTH(4))
  222. #define TRF7970A_MODULATOR_DEPTH_ASK16 (TRF7970A_MODULATOR_DEPTH(5))
  223. #define TRF7970A_MODULATOR_DEPTH_ASK22 (TRF7970A_MODULATOR_DEPTH(6))
  224. #define TRF7970A_MODULATOR_DEPTH_ASK30 (TRF7970A_MODULATOR_DEPTH(7))
  225. #define TRF7970A_MODULATOR_EN_ANA BIT(3)
  226. #define TRF7970A_MODULATOR_CLK(n) (((n) & 0x3) << 4)
  227. #define TRF7970A_MODULATOR_CLK_DISABLED (TRF7970A_MODULATOR_CLK(0))
  228. #define TRF7970A_MODULATOR_CLK_3_6 (TRF7970A_MODULATOR_CLK(1))
  229. #define TRF7970A_MODULATOR_CLK_6_13 (TRF7970A_MODULATOR_CLK(2))
  230. #define TRF7970A_MODULATOR_CLK_13_27 (TRF7970A_MODULATOR_CLK(3))
  231. #define TRF7970A_MODULATOR_EN_OOK BIT(6)
  232. #define TRF7970A_MODULATOR_27MHZ BIT(7)
  233. /* IRQ Status Register Bits */
  234. #define TRF7970A_IRQ_STATUS_NORESP BIT(0) /* ISO15693 only */
  235. #define TRF7970A_IRQ_STATUS_COL BIT(1)
  236. #define TRF7970A_IRQ_STATUS_FRAMING_EOF_ERROR BIT(2)
  237. #define TRF7970A_IRQ_STATUS_PARITY_ERROR BIT(3)
  238. #define TRF7970A_IRQ_STATUS_CRC_ERROR BIT(4)
  239. #define TRF7970A_IRQ_STATUS_FIFO BIT(5)
  240. #define TRF7970A_IRQ_STATUS_SRX BIT(6)
  241. #define TRF7970A_IRQ_STATUS_TX BIT(7)
  242. #define TRF7970A_IRQ_STATUS_ERROR \
  243. (TRF7970A_IRQ_STATUS_COL | \
  244. TRF7970A_IRQ_STATUS_FRAMING_EOF_ERROR | \
  245. TRF7970A_IRQ_STATUS_PARITY_ERROR | \
  246. TRF7970A_IRQ_STATUS_CRC_ERROR)
  247. #define TRF7970A_SPECIAL_FCN_REG1_COL_7_6 BIT(0)
  248. #define TRF7970A_SPECIAL_FCN_REG1_14_ANTICOLL BIT(1)
  249. #define TRF7970A_SPECIAL_FCN_REG1_4_BIT_RX BIT(2)
  250. #define TRF7970A_SPECIAL_FCN_REG1_SP_DIR_MODE BIT(3)
  251. #define TRF7970A_SPECIAL_FCN_REG1_NEXT_SLOT_37US BIT(4)
  252. #define TRF7970A_SPECIAL_FCN_REG1_PAR43 BIT(5)
  253. #define TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLH_124 (0x0 << 2)
  254. #define TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLH_120 (0x1 << 2)
  255. #define TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLH_112 (0x2 << 2)
  256. #define TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLH_96 (0x3 << 2)
  257. #define TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLL_4 0x0
  258. #define TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLL_8 0x1
  259. #define TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLL_16 0x2
  260. #define TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLL_32 0x3
  261. #define TRF7970A_FIFO_STATUS_OVERFLOW BIT(7)
  262. /* NFC (ISO/IEC 14443A) Type 2 Tag commands */
  263. #define NFC_T2T_CMD_READ 0x30
  264. /* ISO 15693 commands codes */
  265. #define ISO15693_CMD_INVENTORY 0x01
  266. #define ISO15693_CMD_READ_SINGLE_BLOCK 0x20
  267. #define ISO15693_CMD_WRITE_SINGLE_BLOCK 0x21
  268. #define ISO15693_CMD_LOCK_BLOCK 0x22
  269. #define ISO15693_CMD_READ_MULTIPLE_BLOCK 0x23
  270. #define ISO15693_CMD_WRITE_MULTIPLE_BLOCK 0x24
  271. #define ISO15693_CMD_SELECT 0x25
  272. #define ISO15693_CMD_RESET_TO_READY 0x26
  273. #define ISO15693_CMD_WRITE_AFI 0x27
  274. #define ISO15693_CMD_LOCK_AFI 0x28
  275. #define ISO15693_CMD_WRITE_DSFID 0x29
  276. #define ISO15693_CMD_LOCK_DSFID 0x2a
  277. #define ISO15693_CMD_GET_SYSTEM_INFO 0x2b
  278. #define ISO15693_CMD_GET_MULTIPLE_BLOCK_SECURITY_STATUS 0x2c
  279. /* ISO 15693 request and response flags */
  280. #define ISO15693_REQ_FLAG_SUB_CARRIER BIT(0)
  281. #define ISO15693_REQ_FLAG_DATA_RATE BIT(1)
  282. #define ISO15693_REQ_FLAG_INVENTORY BIT(2)
  283. #define ISO15693_REQ_FLAG_PROTOCOL_EXT BIT(3)
  284. #define ISO15693_REQ_FLAG_SELECT BIT(4)
  285. #define ISO15693_REQ_FLAG_AFI BIT(4)
  286. #define ISO15693_REQ_FLAG_ADDRESS BIT(5)
  287. #define ISO15693_REQ_FLAG_NB_SLOTS BIT(5)
  288. #define ISO15693_REQ_FLAG_OPTION BIT(6)
  289. #define ISO15693_REQ_FLAG_SPEED_MASK \
  290. (ISO15693_REQ_FLAG_SUB_CARRIER | ISO15693_REQ_FLAG_DATA_RATE)
  291. enum trf7970a_state {
  292. TRF7970A_ST_OFF,
  293. TRF7970A_ST_IDLE,
  294. TRF7970A_ST_IDLE_RX_BLOCKED,
  295. TRF7970A_ST_WAIT_FOR_TX_FIFO,
  296. TRF7970A_ST_WAIT_FOR_RX_DATA,
  297. TRF7970A_ST_WAIT_FOR_RX_DATA_CONT,
  298. TRF7970A_ST_WAIT_TO_ISSUE_EOF,
  299. TRF7970A_ST_MAX
  300. };
  301. struct trf7970a {
  302. enum trf7970a_state state;
  303. struct device *dev;
  304. struct spi_device *spi;
  305. struct regulator *regulator;
  306. struct nfc_digital_dev *ddev;
  307. u32 quirks;
  308. bool aborting;
  309. struct sk_buff *tx_skb;
  310. struct sk_buff *rx_skb;
  311. nfc_digital_cmd_complete_t cb;
  312. void *cb_arg;
  313. u8 chip_status_ctrl;
  314. u8 iso_ctrl;
  315. u8 iso_ctrl_tech;
  316. u8 modulator_sys_clk_ctrl;
  317. u8 special_fcn_reg1;
  318. int technology;
  319. int framing;
  320. u8 tx_cmd;
  321. bool issue_eof;
  322. int en2_gpio;
  323. int en_gpio;
  324. struct mutex lock;
  325. unsigned int timeout;
  326. bool ignore_timeout;
  327. struct delayed_work timeout_work;
  328. };
  329. static int trf7970a_cmd(struct trf7970a *trf, u8 opcode)
  330. {
  331. u8 cmd = TRF7970A_CMD_BIT_CTRL | TRF7970A_CMD_BIT_OPCODE(opcode);
  332. int ret;
  333. dev_dbg(trf->dev, "cmd: 0x%x\n", cmd);
  334. ret = spi_write(trf->spi, &cmd, 1);
  335. if (ret)
  336. dev_err(trf->dev, "%s - cmd: 0x%x, ret: %d\n", __func__, cmd,
  337. ret);
  338. return ret;
  339. }
  340. static int trf7970a_read(struct trf7970a *trf, u8 reg, u8 *val)
  341. {
  342. u8 addr = TRF7970A_CMD_BIT_RW | reg;
  343. int ret;
  344. ret = spi_write_then_read(trf->spi, &addr, 1, val, 1);
  345. if (ret)
  346. dev_err(trf->dev, "%s - addr: 0x%x, ret: %d\n", __func__, addr,
  347. ret);
  348. dev_dbg(trf->dev, "read(0x%x): 0x%x\n", addr, *val);
  349. return ret;
  350. }
  351. static int trf7970a_read_cont(struct trf7970a *trf, u8 reg,
  352. u8 *buf, size_t len)
  353. {
  354. u8 addr = reg | TRF7970A_CMD_BIT_RW | TRF7970A_CMD_BIT_CONTINUOUS;
  355. int ret;
  356. dev_dbg(trf->dev, "read_cont(0x%x, %zd)\n", addr, len);
  357. ret = spi_write_then_read(trf->spi, &addr, 1, buf, len);
  358. if (ret)
  359. dev_err(trf->dev, "%s - addr: 0x%x, ret: %d\n", __func__, addr,
  360. ret);
  361. return ret;
  362. }
  363. static int trf7970a_write(struct trf7970a *trf, u8 reg, u8 val)
  364. {
  365. u8 buf[2] = { reg, val };
  366. int ret;
  367. dev_dbg(trf->dev, "write(0x%x): 0x%x\n", reg, val);
  368. ret = spi_write(trf->spi, buf, 2);
  369. if (ret)
  370. dev_err(trf->dev, "%s - write: 0x%x 0x%x, ret: %d\n", __func__,
  371. buf[0], buf[1], ret);
  372. return ret;
  373. }
  374. static int trf7970a_read_irqstatus(struct trf7970a *trf, u8 *status)
  375. {
  376. int ret;
  377. u8 buf[2];
  378. u8 addr;
  379. addr = TRF7970A_IRQ_STATUS | TRF7970A_CMD_BIT_RW;
  380. if (trf->quirks & TRF7970A_QUIRK_IRQ_STATUS_READ_ERRATA) {
  381. addr |= TRF7970A_CMD_BIT_CONTINUOUS;
  382. ret = spi_write_then_read(trf->spi, &addr, 1, buf, 2);
  383. } else {
  384. ret = spi_write_then_read(trf->spi, &addr, 1, buf, 1);
  385. }
  386. if (ret)
  387. dev_err(trf->dev, "%s - irqstatus: Status read failed: %d\n",
  388. __func__, ret);
  389. else
  390. *status = buf[0];
  391. return ret;
  392. }
  393. static void trf7970a_send_upstream(struct trf7970a *trf)
  394. {
  395. u8 rssi;
  396. dev_kfree_skb_any(trf->tx_skb);
  397. trf->tx_skb = NULL;
  398. if (trf->rx_skb && !IS_ERR(trf->rx_skb) && !trf->aborting)
  399. print_hex_dump_debug("trf7970a rx data: ", DUMP_PREFIX_NONE,
  400. 16, 1, trf->rx_skb->data, trf->rx_skb->len,
  401. false);
  402. /* According to the manual it is "good form" to reset the fifo and
  403. * read the RSSI levels & oscillator status register here. It doesn't
  404. * explain why.
  405. */
  406. trf7970a_cmd(trf, TRF7970A_CMD_FIFO_RESET);
  407. trf7970a_read(trf, TRF7970A_RSSI_OSC_STATUS, &rssi);
  408. trf->state = TRF7970A_ST_IDLE;
  409. if (trf->aborting) {
  410. dev_dbg(trf->dev, "Abort process complete\n");
  411. if (!IS_ERR(trf->rx_skb)) {
  412. kfree_skb(trf->rx_skb);
  413. trf->rx_skb = ERR_PTR(-ECANCELED);
  414. }
  415. trf->aborting = false;
  416. }
  417. trf->cb(trf->ddev, trf->cb_arg, trf->rx_skb);
  418. trf->rx_skb = NULL;
  419. }
  420. static void trf7970a_send_err_upstream(struct trf7970a *trf, int errno)
  421. {
  422. dev_dbg(trf->dev, "Error - state: %d, errno: %d\n", trf->state, errno);
  423. kfree_skb(trf->rx_skb);
  424. trf->rx_skb = ERR_PTR(errno);
  425. trf7970a_send_upstream(trf);
  426. }
  427. static int trf7970a_transmit(struct trf7970a *trf, struct sk_buff *skb,
  428. unsigned int len)
  429. {
  430. unsigned int timeout;
  431. int ret;
  432. print_hex_dump_debug("trf7970a tx data: ", DUMP_PREFIX_NONE,
  433. 16, 1, skb->data, len, false);
  434. ret = spi_write(trf->spi, skb->data, len);
  435. if (ret) {
  436. dev_err(trf->dev, "%s - Can't send tx data: %d\n", __func__,
  437. ret);
  438. return ret;
  439. }
  440. skb_pull(skb, len);
  441. if (skb->len > 0) {
  442. trf->state = TRF7970A_ST_WAIT_FOR_TX_FIFO;
  443. timeout = TRF7970A_WAIT_FOR_FIFO_DRAIN_TIMEOUT;
  444. } else {
  445. if (trf->issue_eof) {
  446. trf->state = TRF7970A_ST_WAIT_TO_ISSUE_EOF;
  447. timeout = TRF7970A_WAIT_TO_ISSUE_ISO15693_EOF;
  448. } else {
  449. trf->state = TRF7970A_ST_WAIT_FOR_RX_DATA;
  450. timeout = trf->timeout;
  451. }
  452. }
  453. dev_dbg(trf->dev, "Setting timeout for %d ms, state: %d\n", timeout,
  454. trf->state);
  455. schedule_delayed_work(&trf->timeout_work, msecs_to_jiffies(timeout));
  456. return 0;
  457. }
  458. static void trf7970a_fill_fifo(struct trf7970a *trf)
  459. {
  460. struct sk_buff *skb = trf->tx_skb;
  461. unsigned int len;
  462. int ret;
  463. u8 fifo_bytes;
  464. ret = trf7970a_read(trf, TRF7970A_FIFO_STATUS, &fifo_bytes);
  465. if (ret) {
  466. trf7970a_send_err_upstream(trf, ret);
  467. return;
  468. }
  469. dev_dbg(trf->dev, "Filling FIFO - fifo_bytes: 0x%x\n", fifo_bytes);
  470. if (fifo_bytes & TRF7970A_FIFO_STATUS_OVERFLOW) {
  471. dev_err(trf->dev, "%s - fifo overflow: 0x%x\n", __func__,
  472. fifo_bytes);
  473. trf7970a_send_err_upstream(trf, -EIO);
  474. return;
  475. }
  476. /* Calculate how much more data can be written to the fifo */
  477. len = TRF7970A_FIFO_SIZE - fifo_bytes;
  478. len = min(skb->len, len);
  479. ret = trf7970a_transmit(trf, skb, len);
  480. if (ret)
  481. trf7970a_send_err_upstream(trf, ret);
  482. }
  483. static void trf7970a_drain_fifo(struct trf7970a *trf, u8 status)
  484. {
  485. struct sk_buff *skb = trf->rx_skb;
  486. int ret;
  487. u8 fifo_bytes;
  488. if (status & TRF7970A_IRQ_STATUS_ERROR) {
  489. trf7970a_send_err_upstream(trf, -EIO);
  490. return;
  491. }
  492. ret = trf7970a_read(trf, TRF7970A_FIFO_STATUS, &fifo_bytes);
  493. if (ret) {
  494. trf7970a_send_err_upstream(trf, ret);
  495. return;
  496. }
  497. dev_dbg(trf->dev, "Draining FIFO - fifo_bytes: 0x%x\n", fifo_bytes);
  498. if (!fifo_bytes)
  499. goto no_rx_data;
  500. if (fifo_bytes & TRF7970A_FIFO_STATUS_OVERFLOW) {
  501. dev_err(trf->dev, "%s - fifo overflow: 0x%x\n", __func__,
  502. fifo_bytes);
  503. trf7970a_send_err_upstream(trf, -EIO);
  504. return;
  505. }
  506. if (fifo_bytes > skb_tailroom(skb)) {
  507. skb = skb_copy_expand(skb, skb_headroom(skb),
  508. max_t(int, fifo_bytes,
  509. TRF7970A_RX_SKB_ALLOC_SIZE),
  510. GFP_KERNEL);
  511. if (!skb) {
  512. trf7970a_send_err_upstream(trf, -ENOMEM);
  513. return;
  514. }
  515. kfree_skb(trf->rx_skb);
  516. trf->rx_skb = skb;
  517. }
  518. ret = trf7970a_read_cont(trf, TRF7970A_FIFO_IO_REGISTER,
  519. skb_put(skb, fifo_bytes), fifo_bytes);
  520. if (ret) {
  521. trf7970a_send_err_upstream(trf, ret);
  522. return;
  523. }
  524. /* If received Type 2 ACK/NACK, shift right 4 bits and pass up */
  525. if ((trf->framing == NFC_DIGITAL_FRAMING_NFCA_T2T) && (skb->len == 1) &&
  526. (trf->special_fcn_reg1 ==
  527. TRF7970A_SPECIAL_FCN_REG1_4_BIT_RX)) {
  528. skb->data[0] >>= 4;
  529. status = TRF7970A_IRQ_STATUS_SRX;
  530. } else {
  531. trf->state = TRF7970A_ST_WAIT_FOR_RX_DATA_CONT;
  532. }
  533. no_rx_data:
  534. if (status == TRF7970A_IRQ_STATUS_SRX) { /* Receive complete */
  535. trf7970a_send_upstream(trf);
  536. return;
  537. }
  538. dev_dbg(trf->dev, "Setting timeout for %d ms\n",
  539. TRF7970A_WAIT_FOR_RX_DATA_TIMEOUT);
  540. schedule_delayed_work(&trf->timeout_work,
  541. msecs_to_jiffies(TRF7970A_WAIT_FOR_RX_DATA_TIMEOUT));
  542. }
  543. static irqreturn_t trf7970a_irq(int irq, void *dev_id)
  544. {
  545. struct trf7970a *trf = dev_id;
  546. int ret;
  547. u8 status;
  548. mutex_lock(&trf->lock);
  549. if (trf->state == TRF7970A_ST_OFF) {
  550. mutex_unlock(&trf->lock);
  551. return IRQ_NONE;
  552. }
  553. ret = trf7970a_read_irqstatus(trf, &status);
  554. if (ret) {
  555. mutex_unlock(&trf->lock);
  556. return IRQ_NONE;
  557. }
  558. dev_dbg(trf->dev, "IRQ - state: %d, status: 0x%x\n", trf->state,
  559. status);
  560. if (!status) {
  561. mutex_unlock(&trf->lock);
  562. return IRQ_NONE;
  563. }
  564. switch (trf->state) {
  565. case TRF7970A_ST_IDLE:
  566. case TRF7970A_ST_IDLE_RX_BLOCKED:
  567. /* If getting interrupts caused by RF noise, turn off the
  568. * receiver to avoid unnecessary interrupts. It will be
  569. * turned back on in trf7970a_in_send_cmd() when the next
  570. * command is issued.
  571. */
  572. if (status & TRF7970A_IRQ_STATUS_ERROR) {
  573. trf7970a_cmd(trf, TRF7970A_CMD_BLOCK_RX);
  574. trf->state = TRF7970A_ST_IDLE_RX_BLOCKED;
  575. }
  576. trf7970a_cmd(trf, TRF7970A_CMD_FIFO_RESET);
  577. break;
  578. case TRF7970A_ST_WAIT_FOR_TX_FIFO:
  579. if (status & TRF7970A_IRQ_STATUS_TX) {
  580. trf->ignore_timeout =
  581. !cancel_delayed_work(&trf->timeout_work);
  582. trf7970a_fill_fifo(trf);
  583. } else {
  584. trf7970a_send_err_upstream(trf, -EIO);
  585. }
  586. break;
  587. case TRF7970A_ST_WAIT_FOR_RX_DATA:
  588. case TRF7970A_ST_WAIT_FOR_RX_DATA_CONT:
  589. if (status & TRF7970A_IRQ_STATUS_SRX) {
  590. trf->ignore_timeout =
  591. !cancel_delayed_work(&trf->timeout_work);
  592. trf7970a_drain_fifo(trf, status);
  593. } else if (status == TRF7970A_IRQ_STATUS_TX) {
  594. trf7970a_cmd(trf, TRF7970A_CMD_FIFO_RESET);
  595. } else {
  596. trf7970a_send_err_upstream(trf, -EIO);
  597. }
  598. break;
  599. case TRF7970A_ST_WAIT_TO_ISSUE_EOF:
  600. if (status != TRF7970A_IRQ_STATUS_TX)
  601. trf7970a_send_err_upstream(trf, -EIO);
  602. break;
  603. default:
  604. dev_err(trf->dev, "%s - Driver in invalid state: %d\n",
  605. __func__, trf->state);
  606. }
  607. mutex_unlock(&trf->lock);
  608. return IRQ_HANDLED;
  609. }
  610. static void trf7970a_issue_eof(struct trf7970a *trf)
  611. {
  612. int ret;
  613. dev_dbg(trf->dev, "Issuing EOF\n");
  614. ret = trf7970a_cmd(trf, TRF7970A_CMD_FIFO_RESET);
  615. if (ret)
  616. trf7970a_send_err_upstream(trf, ret);
  617. ret = trf7970a_cmd(trf, TRF7970A_CMD_EOF);
  618. if (ret)
  619. trf7970a_send_err_upstream(trf, ret);
  620. trf->state = TRF7970A_ST_WAIT_FOR_RX_DATA;
  621. dev_dbg(trf->dev, "Setting timeout for %d ms, state: %d\n",
  622. trf->timeout, trf->state);
  623. schedule_delayed_work(&trf->timeout_work,
  624. msecs_to_jiffies(trf->timeout));
  625. }
  626. static void trf7970a_timeout_work_handler(struct work_struct *work)
  627. {
  628. struct trf7970a *trf = container_of(work, struct trf7970a,
  629. timeout_work.work);
  630. dev_dbg(trf->dev, "Timeout - state: %d, ignore_timeout: %d\n",
  631. trf->state, trf->ignore_timeout);
  632. mutex_lock(&trf->lock);
  633. if (trf->ignore_timeout)
  634. trf->ignore_timeout = false;
  635. else if (trf->state == TRF7970A_ST_WAIT_FOR_RX_DATA_CONT)
  636. trf7970a_send_upstream(trf); /* No more rx data so send up */
  637. else if (trf->state == TRF7970A_ST_WAIT_TO_ISSUE_EOF)
  638. trf7970a_issue_eof(trf);
  639. else
  640. trf7970a_send_err_upstream(trf, -ETIMEDOUT);
  641. mutex_unlock(&trf->lock);
  642. }
  643. static int trf7970a_init(struct trf7970a *trf)
  644. {
  645. int ret;
  646. dev_dbg(trf->dev, "Initializing device - state: %d\n", trf->state);
  647. ret = trf7970a_cmd(trf, TRF7970A_CMD_SOFT_INIT);
  648. if (ret)
  649. goto err_out;
  650. ret = trf7970a_cmd(trf, TRF7970A_CMD_IDLE);
  651. if (ret)
  652. goto err_out;
  653. /* Must clear NFC Target Detection Level reg due to erratum */
  654. ret = trf7970a_write(trf, TRF7970A_NFC_TARGET_LEVEL, 0);
  655. if (ret)
  656. goto err_out;
  657. ret = trf7970a_write(trf, TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS,
  658. TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLH_96 |
  659. TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLL_32);
  660. if (ret)
  661. goto err_out;
  662. ret = trf7970a_write(trf, TRF7970A_SPECIAL_FCN_REG1, 0);
  663. if (ret)
  664. goto err_out;
  665. trf->special_fcn_reg1 = 0;
  666. trf->iso_ctrl = 0xff;
  667. return 0;
  668. err_out:
  669. dev_dbg(trf->dev, "Couldn't init device: %d\n", ret);
  670. return ret;
  671. }
  672. static void trf7970a_switch_rf_off(struct trf7970a *trf)
  673. {
  674. dev_dbg(trf->dev, "Switching rf off\n");
  675. trf->chip_status_ctrl &= ~TRF7970A_CHIP_STATUS_RF_ON;
  676. trf7970a_write(trf, TRF7970A_CHIP_STATUS_CTRL, trf->chip_status_ctrl);
  677. trf->aborting = false;
  678. trf->state = TRF7970A_ST_OFF;
  679. pm_runtime_mark_last_busy(trf->dev);
  680. pm_runtime_put_autosuspend(trf->dev);
  681. }
  682. static void trf7970a_switch_rf_on(struct trf7970a *trf)
  683. {
  684. dev_dbg(trf->dev, "Switching rf on\n");
  685. pm_runtime_get_sync(trf->dev);
  686. trf->state = TRF7970A_ST_IDLE;
  687. }
  688. static int trf7970a_switch_rf(struct nfc_digital_dev *ddev, bool on)
  689. {
  690. struct trf7970a *trf = nfc_digital_get_drvdata(ddev);
  691. dev_dbg(trf->dev, "Switching RF - state: %d, on: %d\n", trf->state, on);
  692. mutex_lock(&trf->lock);
  693. if (on) {
  694. switch (trf->state) {
  695. case TRF7970A_ST_OFF:
  696. trf7970a_switch_rf_on(trf);
  697. break;
  698. case TRF7970A_ST_IDLE:
  699. case TRF7970A_ST_IDLE_RX_BLOCKED:
  700. break;
  701. default:
  702. dev_err(trf->dev, "%s - Invalid request: %d %d\n",
  703. __func__, trf->state, on);
  704. trf7970a_switch_rf_off(trf);
  705. }
  706. } else {
  707. switch (trf->state) {
  708. case TRF7970A_ST_OFF:
  709. break;
  710. default:
  711. dev_err(trf->dev, "%s - Invalid request: %d %d\n",
  712. __func__, trf->state, on);
  713. /* FALLTHROUGH */
  714. case TRF7970A_ST_IDLE:
  715. case TRF7970A_ST_IDLE_RX_BLOCKED:
  716. trf7970a_switch_rf_off(trf);
  717. }
  718. }
  719. mutex_unlock(&trf->lock);
  720. return 0;
  721. }
  722. static int trf7970a_config_rf_tech(struct trf7970a *trf, int tech)
  723. {
  724. int ret = 0;
  725. dev_dbg(trf->dev, "rf technology: %d\n", tech);
  726. switch (tech) {
  727. case NFC_DIGITAL_RF_TECH_106A:
  728. trf->iso_ctrl_tech = TRF7970A_ISO_CTRL_14443A_106;
  729. trf->modulator_sys_clk_ctrl = TRF7970A_MODULATOR_DEPTH_OOK;
  730. break;
  731. case NFC_DIGITAL_RF_TECH_106B:
  732. trf->iso_ctrl_tech = TRF7970A_ISO_CTRL_14443B_106;
  733. trf->modulator_sys_clk_ctrl = TRF7970A_MODULATOR_DEPTH_ASK10;
  734. break;
  735. case NFC_DIGITAL_RF_TECH_212F:
  736. trf->iso_ctrl_tech = TRF7970A_ISO_CTRL_FELICA_212;
  737. trf->modulator_sys_clk_ctrl = TRF7970A_MODULATOR_DEPTH_ASK10;
  738. break;
  739. case NFC_DIGITAL_RF_TECH_424F:
  740. trf->iso_ctrl_tech = TRF7970A_ISO_CTRL_FELICA_424;
  741. trf->modulator_sys_clk_ctrl = TRF7970A_MODULATOR_DEPTH_ASK10;
  742. break;
  743. case NFC_DIGITAL_RF_TECH_ISO15693:
  744. trf->iso_ctrl_tech = TRF7970A_ISO_CTRL_15693_SGL_1OF4_2648;
  745. trf->modulator_sys_clk_ctrl = TRF7970A_MODULATOR_DEPTH_OOK;
  746. break;
  747. default:
  748. dev_dbg(trf->dev, "Unsupported rf technology: %d\n", tech);
  749. return -EINVAL;
  750. }
  751. trf->technology = tech;
  752. return ret;
  753. }
  754. static int trf7970a_config_framing(struct trf7970a *trf, int framing)
  755. {
  756. u8 iso_ctrl = trf->iso_ctrl_tech;
  757. int ret;
  758. dev_dbg(trf->dev, "framing: %d\n", framing);
  759. switch (framing) {
  760. case NFC_DIGITAL_FRAMING_NFCA_SHORT:
  761. case NFC_DIGITAL_FRAMING_NFCA_STANDARD:
  762. trf->tx_cmd = TRF7970A_CMD_TRANSMIT_NO_CRC;
  763. iso_ctrl |= TRF7970A_ISO_CTRL_RX_CRC_N;
  764. break;
  765. case NFC_DIGITAL_FRAMING_NFCA_STANDARD_WITH_CRC_A:
  766. case NFC_DIGITAL_FRAMING_NFCA_T4T:
  767. case NFC_DIGITAL_FRAMING_NFCB:
  768. case NFC_DIGITAL_FRAMING_NFCB_T4T:
  769. case NFC_DIGITAL_FRAMING_NFCF:
  770. case NFC_DIGITAL_FRAMING_NFCF_T3T:
  771. case NFC_DIGITAL_FRAMING_ISO15693_INVENTORY:
  772. case NFC_DIGITAL_FRAMING_ISO15693_T5T:
  773. trf->tx_cmd = TRF7970A_CMD_TRANSMIT;
  774. iso_ctrl &= ~TRF7970A_ISO_CTRL_RX_CRC_N;
  775. break;
  776. case NFC_DIGITAL_FRAMING_NFCA_T2T:
  777. trf->tx_cmd = TRF7970A_CMD_TRANSMIT;
  778. iso_ctrl |= TRF7970A_ISO_CTRL_RX_CRC_N;
  779. break;
  780. default:
  781. dev_dbg(trf->dev, "Unsupported Framing: %d\n", framing);
  782. return -EINVAL;
  783. }
  784. trf->framing = framing;
  785. if (iso_ctrl != trf->iso_ctrl) {
  786. ret = trf7970a_write(trf, TRF7970A_ISO_CTRL, iso_ctrl);
  787. if (ret)
  788. return ret;
  789. trf->iso_ctrl = iso_ctrl;
  790. ret = trf7970a_write(trf, TRF7970A_MODULATOR_SYS_CLK_CTRL,
  791. trf->modulator_sys_clk_ctrl);
  792. if (ret)
  793. return ret;
  794. }
  795. if (!(trf->chip_status_ctrl & TRF7970A_CHIP_STATUS_RF_ON)) {
  796. ret = trf7970a_write(trf, TRF7970A_CHIP_STATUS_CTRL,
  797. trf->chip_status_ctrl |
  798. TRF7970A_CHIP_STATUS_RF_ON);
  799. if (ret)
  800. return ret;
  801. trf->chip_status_ctrl |= TRF7970A_CHIP_STATUS_RF_ON;
  802. usleep_range(5000, 6000);
  803. }
  804. return 0;
  805. }
  806. static int trf7970a_in_configure_hw(struct nfc_digital_dev *ddev, int type,
  807. int param)
  808. {
  809. struct trf7970a *trf = nfc_digital_get_drvdata(ddev);
  810. int ret;
  811. dev_dbg(trf->dev, "Configure hw - type: %d, param: %d\n", type, param);
  812. mutex_lock(&trf->lock);
  813. if (trf->state == TRF7970A_ST_OFF)
  814. trf7970a_switch_rf_on(trf);
  815. switch (type) {
  816. case NFC_DIGITAL_CONFIG_RF_TECH:
  817. ret = trf7970a_config_rf_tech(trf, param);
  818. break;
  819. case NFC_DIGITAL_CONFIG_FRAMING:
  820. ret = trf7970a_config_framing(trf, param);
  821. break;
  822. default:
  823. dev_dbg(trf->dev, "Unknown type: %d\n", type);
  824. ret = -EINVAL;
  825. }
  826. mutex_unlock(&trf->lock);
  827. return ret;
  828. }
  829. static int trf7970a_is_iso15693_write_or_lock(u8 cmd)
  830. {
  831. switch (cmd) {
  832. case ISO15693_CMD_WRITE_SINGLE_BLOCK:
  833. case ISO15693_CMD_LOCK_BLOCK:
  834. case ISO15693_CMD_WRITE_MULTIPLE_BLOCK:
  835. case ISO15693_CMD_WRITE_AFI:
  836. case ISO15693_CMD_LOCK_AFI:
  837. case ISO15693_CMD_WRITE_DSFID:
  838. case ISO15693_CMD_LOCK_DSFID:
  839. return 1;
  840. break;
  841. default:
  842. return 0;
  843. }
  844. }
  845. static int trf7970a_per_cmd_config(struct trf7970a *trf, struct sk_buff *skb)
  846. {
  847. u8 *req = skb->data;
  848. u8 special_fcn_reg1, iso_ctrl;
  849. int ret;
  850. trf->issue_eof = false;
  851. /* When issuing Type 2 read command, make sure the '4_bit_RX' bit in
  852. * special functions register 1 is cleared; otherwise, its a write or
  853. * sector select command and '4_bit_RX' must be set.
  854. *
  855. * When issuing an ISO 15693 command, inspect the flags byte to see
  856. * what speed to use. Also, remember if the OPTION flag is set on
  857. * a Type 5 write or lock command so the driver will know that it
  858. * has to send an EOF in order to get a response.
  859. */
  860. if ((trf->technology == NFC_DIGITAL_RF_TECH_106A) &&
  861. (trf->framing == NFC_DIGITAL_FRAMING_NFCA_T2T)) {
  862. if (req[0] == NFC_T2T_CMD_READ)
  863. special_fcn_reg1 = 0;
  864. else
  865. special_fcn_reg1 = TRF7970A_SPECIAL_FCN_REG1_4_BIT_RX;
  866. if (special_fcn_reg1 != trf->special_fcn_reg1) {
  867. ret = trf7970a_write(trf, TRF7970A_SPECIAL_FCN_REG1,
  868. special_fcn_reg1);
  869. if (ret)
  870. return ret;
  871. trf->special_fcn_reg1 = special_fcn_reg1;
  872. }
  873. } else if (trf->technology == NFC_DIGITAL_RF_TECH_ISO15693) {
  874. iso_ctrl = trf->iso_ctrl & ~TRF7970A_ISO_CTRL_RFID_SPEED_MASK;
  875. switch (req[0] & ISO15693_REQ_FLAG_SPEED_MASK) {
  876. case 0x00:
  877. iso_ctrl |= TRF7970A_ISO_CTRL_15693_SGL_1OF4_662;
  878. break;
  879. case ISO15693_REQ_FLAG_SUB_CARRIER:
  880. iso_ctrl |= TRF7970A_ISO_CTRL_15693_DBL_1OF4_667a;
  881. break;
  882. case ISO15693_REQ_FLAG_DATA_RATE:
  883. iso_ctrl |= TRF7970A_ISO_CTRL_15693_SGL_1OF4_2648;
  884. break;
  885. case (ISO15693_REQ_FLAG_SUB_CARRIER |
  886. ISO15693_REQ_FLAG_DATA_RATE):
  887. iso_ctrl |= TRF7970A_ISO_CTRL_15693_DBL_1OF4_2669;
  888. break;
  889. }
  890. if (iso_ctrl != trf->iso_ctrl) {
  891. ret = trf7970a_write(trf, TRF7970A_ISO_CTRL, iso_ctrl);
  892. if (ret)
  893. return ret;
  894. trf->iso_ctrl = iso_ctrl;
  895. }
  896. if ((trf->framing == NFC_DIGITAL_FRAMING_ISO15693_T5T) &&
  897. trf7970a_is_iso15693_write_or_lock(req[1]) &&
  898. (req[0] & ISO15693_REQ_FLAG_OPTION))
  899. trf->issue_eof = true;
  900. }
  901. return 0;
  902. }
  903. static int trf7970a_in_send_cmd(struct nfc_digital_dev *ddev,
  904. struct sk_buff *skb, u16 timeout,
  905. nfc_digital_cmd_complete_t cb, void *arg)
  906. {
  907. struct trf7970a *trf = nfc_digital_get_drvdata(ddev);
  908. char *prefix;
  909. unsigned int len;
  910. int ret;
  911. dev_dbg(trf->dev, "New request - state: %d, timeout: %d ms, len: %d\n",
  912. trf->state, timeout, skb->len);
  913. if (skb->len > TRF7970A_TX_MAX)
  914. return -EINVAL;
  915. mutex_lock(&trf->lock);
  916. if ((trf->state != TRF7970A_ST_IDLE) &&
  917. (trf->state != TRF7970A_ST_IDLE_RX_BLOCKED)) {
  918. dev_err(trf->dev, "%s - Bogus state: %d\n", __func__,
  919. trf->state);
  920. ret = -EIO;
  921. goto out_err;
  922. }
  923. if (trf->aborting) {
  924. dev_dbg(trf->dev, "Abort process complete\n");
  925. trf->aborting = false;
  926. ret = -ECANCELED;
  927. goto out_err;
  928. }
  929. trf->rx_skb = nfc_alloc_recv_skb(TRF7970A_RX_SKB_ALLOC_SIZE,
  930. GFP_KERNEL);
  931. if (!trf->rx_skb) {
  932. dev_dbg(trf->dev, "Can't alloc rx_skb\n");
  933. ret = -ENOMEM;
  934. goto out_err;
  935. }
  936. if (trf->state == TRF7970A_ST_IDLE_RX_BLOCKED) {
  937. ret = trf7970a_cmd(trf, TRF7970A_CMD_ENABLE_RX);
  938. if (ret)
  939. goto out_err;
  940. trf->state = TRF7970A_ST_IDLE;
  941. }
  942. ret = trf7970a_per_cmd_config(trf, skb);
  943. if (ret)
  944. goto out_err;
  945. trf->ddev = ddev;
  946. trf->tx_skb = skb;
  947. trf->cb = cb;
  948. trf->cb_arg = arg;
  949. trf->timeout = timeout;
  950. trf->ignore_timeout = false;
  951. len = skb->len;
  952. prefix = skb_push(skb, TRF7970A_TX_SKB_HEADROOM);
  953. /* TX data must be prefixed with a FIFO reset cmd, a cmd that depends
  954. * on what the current framing is, the address of the TX length byte 1
  955. * register (0x1d), and the 2 byte length of the data to be transmitted.
  956. */
  957. prefix[0] = TRF7970A_CMD_BIT_CTRL |
  958. TRF7970A_CMD_BIT_OPCODE(TRF7970A_CMD_FIFO_RESET);
  959. prefix[1] = TRF7970A_CMD_BIT_CTRL |
  960. TRF7970A_CMD_BIT_OPCODE(trf->tx_cmd);
  961. prefix[2] = TRF7970A_CMD_BIT_CONTINUOUS | TRF7970A_TX_LENGTH_BYTE1;
  962. if (trf->framing == NFC_DIGITAL_FRAMING_NFCA_SHORT) {
  963. prefix[3] = 0x00;
  964. prefix[4] = 0x0f; /* 7 bits */
  965. } else {
  966. prefix[3] = (len & 0xf00) >> 4;
  967. prefix[3] |= ((len & 0xf0) >> 4);
  968. prefix[4] = ((len & 0x0f) << 4);
  969. }
  970. len = min_t(int, skb->len, TRF7970A_FIFO_SIZE);
  971. usleep_range(1000, 2000);
  972. ret = trf7970a_transmit(trf, skb, len);
  973. if (ret) {
  974. kfree_skb(trf->rx_skb);
  975. trf->rx_skb = NULL;
  976. }
  977. out_err:
  978. mutex_unlock(&trf->lock);
  979. return ret;
  980. }
  981. static int trf7970a_tg_configure_hw(struct nfc_digital_dev *ddev,
  982. int type, int param)
  983. {
  984. struct trf7970a *trf = nfc_digital_get_drvdata(ddev);
  985. dev_dbg(trf->dev, "Unsupported interface\n");
  986. return -EINVAL;
  987. }
  988. static int trf7970a_tg_send_cmd(struct nfc_digital_dev *ddev,
  989. struct sk_buff *skb, u16 timeout,
  990. nfc_digital_cmd_complete_t cb, void *arg)
  991. {
  992. struct trf7970a *trf = nfc_digital_get_drvdata(ddev);
  993. dev_dbg(trf->dev, "Unsupported interface\n");
  994. return -EINVAL;
  995. }
  996. static int trf7970a_tg_listen(struct nfc_digital_dev *ddev,
  997. u16 timeout, nfc_digital_cmd_complete_t cb, void *arg)
  998. {
  999. struct trf7970a *trf = nfc_digital_get_drvdata(ddev);
  1000. dev_dbg(trf->dev, "Unsupported interface\n");
  1001. return -EINVAL;
  1002. }
  1003. static int trf7970a_tg_listen_mdaa(struct nfc_digital_dev *ddev,
  1004. struct digital_tg_mdaa_params *mdaa_params,
  1005. u16 timeout, nfc_digital_cmd_complete_t cb, void *arg)
  1006. {
  1007. struct trf7970a *trf = nfc_digital_get_drvdata(ddev);
  1008. dev_dbg(trf->dev, "Unsupported interface\n");
  1009. return -EINVAL;
  1010. }
  1011. static void trf7970a_abort_cmd(struct nfc_digital_dev *ddev)
  1012. {
  1013. struct trf7970a *trf = nfc_digital_get_drvdata(ddev);
  1014. dev_dbg(trf->dev, "Abort process initiated\n");
  1015. mutex_lock(&trf->lock);
  1016. switch (trf->state) {
  1017. case TRF7970A_ST_WAIT_FOR_TX_FIFO:
  1018. case TRF7970A_ST_WAIT_FOR_RX_DATA:
  1019. case TRF7970A_ST_WAIT_FOR_RX_DATA_CONT:
  1020. case TRF7970A_ST_WAIT_TO_ISSUE_EOF:
  1021. trf->aborting = true;
  1022. break;
  1023. default:
  1024. break;
  1025. }
  1026. mutex_unlock(&trf->lock);
  1027. }
  1028. static struct nfc_digital_ops trf7970a_nfc_ops = {
  1029. .in_configure_hw = trf7970a_in_configure_hw,
  1030. .in_send_cmd = trf7970a_in_send_cmd,
  1031. .tg_configure_hw = trf7970a_tg_configure_hw,
  1032. .tg_send_cmd = trf7970a_tg_send_cmd,
  1033. .tg_listen = trf7970a_tg_listen,
  1034. .tg_listen_mdaa = trf7970a_tg_listen_mdaa,
  1035. .switch_rf = trf7970a_switch_rf,
  1036. .abort_cmd = trf7970a_abort_cmd,
  1037. };
  1038. static int trf7970a_get_autosuspend_delay(struct device_node *np)
  1039. {
  1040. int autosuspend_delay, ret;
  1041. ret = of_property_read_u32(np, "autosuspend-delay", &autosuspend_delay);
  1042. if (ret)
  1043. autosuspend_delay = TRF7970A_AUTOSUSPEND_DELAY;
  1044. of_node_put(np);
  1045. return autosuspend_delay;
  1046. }
  1047. static int trf7970a_probe(struct spi_device *spi)
  1048. {
  1049. struct device_node *np = spi->dev.of_node;
  1050. const struct spi_device_id *id = spi_get_device_id(spi);
  1051. struct trf7970a *trf;
  1052. int uvolts, autosuspend_delay, ret;
  1053. if (!np) {
  1054. dev_err(&spi->dev, "No Device Tree entry\n");
  1055. return -EINVAL;
  1056. }
  1057. trf = devm_kzalloc(&spi->dev, sizeof(*trf), GFP_KERNEL);
  1058. if (!trf)
  1059. return -ENOMEM;
  1060. trf->state = TRF7970A_ST_OFF;
  1061. trf->dev = &spi->dev;
  1062. trf->spi = spi;
  1063. trf->quirks = id->driver_data;
  1064. spi->mode = SPI_MODE_1;
  1065. spi->bits_per_word = 8;
  1066. /* There are two enable pins - both must be present */
  1067. trf->en_gpio = of_get_named_gpio(np, "ti,enable-gpios", 0);
  1068. if (!gpio_is_valid(trf->en_gpio)) {
  1069. dev_err(trf->dev, "No EN GPIO property\n");
  1070. return trf->en_gpio;
  1071. }
  1072. ret = devm_gpio_request_one(trf->dev, trf->en_gpio,
  1073. GPIOF_DIR_OUT | GPIOF_INIT_LOW, "EN");
  1074. if (ret) {
  1075. dev_err(trf->dev, "Can't request EN GPIO: %d\n", ret);
  1076. return ret;
  1077. }
  1078. trf->en2_gpio = of_get_named_gpio(np, "ti,enable-gpios", 1);
  1079. if (!gpio_is_valid(trf->en2_gpio)) {
  1080. dev_err(trf->dev, "No EN2 GPIO property\n");
  1081. return trf->en2_gpio;
  1082. }
  1083. ret = devm_gpio_request_one(trf->dev, trf->en2_gpio,
  1084. GPIOF_DIR_OUT | GPIOF_INIT_LOW, "EN2");
  1085. if (ret) {
  1086. dev_err(trf->dev, "Can't request EN2 GPIO: %d\n", ret);
  1087. return ret;
  1088. }
  1089. ret = devm_request_threaded_irq(trf->dev, spi->irq, NULL,
  1090. trf7970a_irq, IRQF_TRIGGER_RISING | IRQF_ONESHOT,
  1091. "trf7970a", trf);
  1092. if (ret) {
  1093. dev_err(trf->dev, "Can't request IRQ#%d: %d\n", spi->irq, ret);
  1094. return ret;
  1095. }
  1096. mutex_init(&trf->lock);
  1097. INIT_DELAYED_WORK(&trf->timeout_work, trf7970a_timeout_work_handler);
  1098. trf->regulator = devm_regulator_get(&spi->dev, "vin");
  1099. if (IS_ERR(trf->regulator)) {
  1100. ret = PTR_ERR(trf->regulator);
  1101. dev_err(trf->dev, "Can't get VIN regulator: %d\n", ret);
  1102. goto err_destroy_lock;
  1103. }
  1104. ret = regulator_enable(trf->regulator);
  1105. if (ret) {
  1106. dev_err(trf->dev, "Can't enable VIN: %d\n", ret);
  1107. goto err_destroy_lock;
  1108. }
  1109. uvolts = regulator_get_voltage(trf->regulator);
  1110. if (uvolts > 4000000)
  1111. trf->chip_status_ctrl = TRF7970A_CHIP_STATUS_VRS5_3;
  1112. trf->ddev = nfc_digital_allocate_device(&trf7970a_nfc_ops,
  1113. TRF7970A_SUPPORTED_PROTOCOLS,
  1114. NFC_DIGITAL_DRV_CAPS_IN_CRC, TRF7970A_TX_SKB_HEADROOM,
  1115. 0);
  1116. if (!trf->ddev) {
  1117. dev_err(trf->dev, "Can't allocate NFC digital device\n");
  1118. ret = -ENOMEM;
  1119. goto err_disable_regulator;
  1120. }
  1121. nfc_digital_set_parent_dev(trf->ddev, trf->dev);
  1122. nfc_digital_set_drvdata(trf->ddev, trf);
  1123. spi_set_drvdata(spi, trf);
  1124. autosuspend_delay = trf7970a_get_autosuspend_delay(np);
  1125. pm_runtime_set_autosuspend_delay(trf->dev, autosuspend_delay);
  1126. pm_runtime_use_autosuspend(trf->dev);
  1127. pm_runtime_enable(trf->dev);
  1128. ret = nfc_digital_register_device(trf->ddev);
  1129. if (ret) {
  1130. dev_err(trf->dev, "Can't register NFC digital device: %d\n",
  1131. ret);
  1132. goto err_free_ddev;
  1133. }
  1134. return 0;
  1135. err_free_ddev:
  1136. pm_runtime_disable(trf->dev);
  1137. nfc_digital_free_device(trf->ddev);
  1138. err_disable_regulator:
  1139. regulator_disable(trf->regulator);
  1140. err_destroy_lock:
  1141. mutex_destroy(&trf->lock);
  1142. return ret;
  1143. }
  1144. static int trf7970a_remove(struct spi_device *spi)
  1145. {
  1146. struct trf7970a *trf = spi_get_drvdata(spi);
  1147. mutex_lock(&trf->lock);
  1148. switch (trf->state) {
  1149. case TRF7970A_ST_WAIT_FOR_TX_FIFO:
  1150. case TRF7970A_ST_WAIT_FOR_RX_DATA:
  1151. case TRF7970A_ST_WAIT_FOR_RX_DATA_CONT:
  1152. case TRF7970A_ST_WAIT_TO_ISSUE_EOF:
  1153. trf7970a_send_err_upstream(trf, -ECANCELED);
  1154. /* FALLTHROUGH */
  1155. case TRF7970A_ST_IDLE:
  1156. case TRF7970A_ST_IDLE_RX_BLOCKED:
  1157. pm_runtime_put_sync(trf->dev);
  1158. break;
  1159. default:
  1160. break;
  1161. }
  1162. mutex_unlock(&trf->lock);
  1163. pm_runtime_disable(trf->dev);
  1164. nfc_digital_unregister_device(trf->ddev);
  1165. nfc_digital_free_device(trf->ddev);
  1166. regulator_disable(trf->regulator);
  1167. mutex_destroy(&trf->lock);
  1168. return 0;
  1169. }
  1170. #ifdef CONFIG_PM_RUNTIME
  1171. static int trf7970a_pm_runtime_suspend(struct device *dev)
  1172. {
  1173. struct spi_device *spi = container_of(dev, struct spi_device, dev);
  1174. struct trf7970a *trf = spi_get_drvdata(spi);
  1175. int ret;
  1176. dev_dbg(dev, "Runtime suspend\n");
  1177. if (trf->state != TRF7970A_ST_OFF) {
  1178. dev_dbg(dev, "Can't suspend - not in OFF state (%d)\n",
  1179. trf->state);
  1180. return -EBUSY;
  1181. }
  1182. gpio_set_value(trf->en_gpio, 0);
  1183. gpio_set_value(trf->en2_gpio, 0);
  1184. ret = regulator_disable(trf->regulator);
  1185. if (ret)
  1186. dev_err(dev, "%s - Can't disable VIN: %d\n", __func__, ret);
  1187. return ret;
  1188. }
  1189. static int trf7970a_pm_runtime_resume(struct device *dev)
  1190. {
  1191. struct spi_device *spi = container_of(dev, struct spi_device, dev);
  1192. struct trf7970a *trf = spi_get_drvdata(spi);
  1193. int ret;
  1194. dev_dbg(dev, "Runtime resume\n");
  1195. ret = regulator_enable(trf->regulator);
  1196. if (ret) {
  1197. dev_err(dev, "%s - Can't enable VIN: %d\n", __func__, ret);
  1198. return ret;
  1199. }
  1200. usleep_range(5000, 6000);
  1201. gpio_set_value(trf->en2_gpio, 1);
  1202. usleep_range(1000, 2000);
  1203. gpio_set_value(trf->en_gpio, 1);
  1204. usleep_range(20000, 21000);
  1205. ret = trf7970a_init(trf);
  1206. if (ret) {
  1207. dev_err(dev, "%s - Can't initialize: %d\n", __func__, ret);
  1208. return ret;
  1209. }
  1210. pm_runtime_mark_last_busy(dev);
  1211. return 0;
  1212. }
  1213. #endif
  1214. static const struct dev_pm_ops trf7970a_pm_ops = {
  1215. SET_RUNTIME_PM_OPS(trf7970a_pm_runtime_suspend,
  1216. trf7970a_pm_runtime_resume, NULL)
  1217. };
  1218. static const struct spi_device_id trf7970a_id_table[] = {
  1219. { "trf7970a", TRF7970A_QUIRK_IRQ_STATUS_READ_ERRATA },
  1220. { }
  1221. };
  1222. MODULE_DEVICE_TABLE(spi, trf7970a_id_table);
  1223. static struct spi_driver trf7970a_spi_driver = {
  1224. .probe = trf7970a_probe,
  1225. .remove = trf7970a_remove,
  1226. .id_table = trf7970a_id_table,
  1227. .driver = {
  1228. .name = "trf7970a",
  1229. .owner = THIS_MODULE,
  1230. .pm = &trf7970a_pm_ops,
  1231. },
  1232. };
  1233. module_spi_driver(trf7970a_spi_driver);
  1234. MODULE_AUTHOR("Mark A. Greer <mgreer@animalcreek.com>");
  1235. MODULE_LICENSE("GPL v2");
  1236. MODULE_DESCRIPTION("TI trf7970a RFID/NFC Transceiver Driver");