trf7970a.c 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245
  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 <[email protected]>
  7. * Author: Felipe Balbi <[email protected]>
  8. * Author: Mark A. Greer <[email protected]>
  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 <[email protected]>");
  1847. MODULE_LICENSE("GPL v2");
  1848. MODULE_DESCRIPTION("TI trf7970a RFID/NFC Transceiver Driver");