trf7970a.c 60 KB

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