q6afe.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504
  1. // SPDX-License-Identifier: GPL-2.0
  2. // Copyright (c) 2011-2017, The Linux Foundation. All rights reserved.
  3. // Copyright (c) 2018, Linaro Limited
  4. #include <linux/slab.h>
  5. #include <linux/kernel.h>
  6. #include <linux/uaccess.h>
  7. #include <linux/wait.h>
  8. #include <linux/jiffies.h>
  9. #include <linux/sched.h>
  10. #include <linux/module.h>
  11. #include <linux/kref.h>
  12. #include <linux/of.h>
  13. #include <linux/of_platform.h>
  14. #include <linux/spinlock.h>
  15. #include <linux/delay.h>
  16. #include <linux/soc/qcom/apr.h>
  17. #include <sound/soc.h>
  18. #include <sound/soc-dai.h>
  19. #include <sound/pcm.h>
  20. #include <sound/pcm_params.h>
  21. #include "q6dsp-errno.h"
  22. #include "q6core.h"
  23. #include "q6afe.h"
  24. /* AFE CMDs */
  25. #define AFE_PORT_CMD_DEVICE_START 0x000100E5
  26. #define AFE_PORT_CMD_DEVICE_STOP 0x000100E6
  27. #define AFE_PORT_CMD_SET_PARAM_V2 0x000100EF
  28. #define AFE_SVC_CMD_SET_PARAM 0x000100f3
  29. #define AFE_PORT_CMDRSP_GET_PARAM_V2 0x00010106
  30. #define AFE_PARAM_ID_HDMI_CONFIG 0x00010210
  31. #define AFE_MODULE_AUDIO_DEV_INTERFACE 0x0001020C
  32. #define AFE_MODULE_TDM 0x0001028A
  33. #define AFE_PARAM_ID_CDC_SLIMBUS_SLAVE_CFG 0x00010235
  34. #define AFE_PARAM_ID_LPAIF_CLK_CONFIG 0x00010238
  35. #define AFE_PARAM_ID_INT_DIGITAL_CDC_CLK_CONFIG 0x00010239
  36. #define AFE_PARAM_ID_SLIMBUS_CONFIG 0x00010212
  37. #define AFE_PARAM_ID_I2S_CONFIG 0x0001020D
  38. #define AFE_PARAM_ID_TDM_CONFIG 0x0001029D
  39. #define AFE_PARAM_ID_PORT_SLOT_MAPPING_CONFIG 0x00010297
  40. /* I2S config specific */
  41. #define AFE_API_VERSION_I2S_CONFIG 0x1
  42. #define AFE_PORT_I2S_SD0 0x1
  43. #define AFE_PORT_I2S_SD1 0x2
  44. #define AFE_PORT_I2S_SD2 0x3
  45. #define AFE_PORT_I2S_SD3 0x4
  46. #define AFE_PORT_I2S_SD0_MASK BIT(0x0)
  47. #define AFE_PORT_I2S_SD1_MASK BIT(0x1)
  48. #define AFE_PORT_I2S_SD2_MASK BIT(0x2)
  49. #define AFE_PORT_I2S_SD3_MASK BIT(0x3)
  50. #define AFE_PORT_I2S_SD0_1_MASK GENMASK(1, 0)
  51. #define AFE_PORT_I2S_SD2_3_MASK GENMASK(3, 2)
  52. #define AFE_PORT_I2S_SD0_1_2_MASK GENMASK(2, 0)
  53. #define AFE_PORT_I2S_SD0_1_2_3_MASK GENMASK(3, 0)
  54. #define AFE_PORT_I2S_QUAD01 0x5
  55. #define AFE_PORT_I2S_QUAD23 0x6
  56. #define AFE_PORT_I2S_6CHS 0x7
  57. #define AFE_PORT_I2S_8CHS 0x8
  58. #define AFE_PORT_I2S_MONO 0x0
  59. #define AFE_PORT_I2S_STEREO 0x1
  60. #define AFE_PORT_CONFIG_I2S_WS_SRC_EXTERNAL 0x0
  61. #define AFE_PORT_CONFIG_I2S_WS_SRC_INTERNAL 0x1
  62. #define AFE_LINEAR_PCM_DATA 0x0
  63. /* Port IDs */
  64. #define AFE_API_VERSION_HDMI_CONFIG 0x1
  65. #define AFE_PORT_ID_MULTICHAN_HDMI_RX 0x100E
  66. #define AFE_API_VERSION_SLIMBUS_CONFIG 0x1
  67. /* Clock set API version */
  68. #define AFE_API_VERSION_CLOCK_SET 1
  69. #define Q6AFE_LPASS_CLK_CONFIG_API_VERSION 0x1
  70. #define AFE_MODULE_CLOCK_SET 0x0001028F
  71. #define AFE_PARAM_ID_CLOCK_SET 0x00010290
  72. /* SLIMbus Rx port on channel 0. */
  73. #define AFE_PORT_ID_SLIMBUS_MULTI_CHAN_0_RX 0x4000
  74. /* SLIMbus Tx port on channel 0. */
  75. #define AFE_PORT_ID_SLIMBUS_MULTI_CHAN_0_TX 0x4001
  76. /* SLIMbus Rx port on channel 1. */
  77. #define AFE_PORT_ID_SLIMBUS_MULTI_CHAN_1_RX 0x4002
  78. /* SLIMbus Tx port on channel 1. */
  79. #define AFE_PORT_ID_SLIMBUS_MULTI_CHAN_1_TX 0x4003
  80. /* SLIMbus Rx port on channel 2. */
  81. #define AFE_PORT_ID_SLIMBUS_MULTI_CHAN_2_RX 0x4004
  82. /* SLIMbus Tx port on channel 2. */
  83. #define AFE_PORT_ID_SLIMBUS_MULTI_CHAN_2_TX 0x4005
  84. /* SLIMbus Rx port on channel 3. */
  85. #define AFE_PORT_ID_SLIMBUS_MULTI_CHAN_3_RX 0x4006
  86. /* SLIMbus Tx port on channel 3. */
  87. #define AFE_PORT_ID_SLIMBUS_MULTI_CHAN_3_TX 0x4007
  88. /* SLIMbus Rx port on channel 4. */
  89. #define AFE_PORT_ID_SLIMBUS_MULTI_CHAN_4_RX 0x4008
  90. /* SLIMbus Tx port on channel 4. */
  91. #define AFE_PORT_ID_SLIMBUS_MULTI_CHAN_4_TX 0x4009
  92. /* SLIMbus Rx port on channel 5. */
  93. #define AFE_PORT_ID_SLIMBUS_MULTI_CHAN_5_RX 0x400a
  94. /* SLIMbus Tx port on channel 5. */
  95. #define AFE_PORT_ID_SLIMBUS_MULTI_CHAN_5_TX 0x400b
  96. /* SLIMbus Rx port on channel 6. */
  97. #define AFE_PORT_ID_SLIMBUS_MULTI_CHAN_6_RX 0x400c
  98. /* SLIMbus Tx port on channel 6. */
  99. #define AFE_PORT_ID_SLIMBUS_MULTI_CHAN_6_TX 0x400d
  100. #define AFE_PORT_ID_PRIMARY_MI2S_RX 0x1000
  101. #define AFE_PORT_ID_PRIMARY_MI2S_TX 0x1001
  102. #define AFE_PORT_ID_SECONDARY_MI2S_RX 0x1002
  103. #define AFE_PORT_ID_SECONDARY_MI2S_TX 0x1003
  104. #define AFE_PORT_ID_TERTIARY_MI2S_RX 0x1004
  105. #define AFE_PORT_ID_TERTIARY_MI2S_TX 0x1005
  106. #define AFE_PORT_ID_QUATERNARY_MI2S_RX 0x1006
  107. #define AFE_PORT_ID_QUATERNARY_MI2S_TX 0x1007
  108. /* Start of the range of port IDs for TDM devices. */
  109. #define AFE_PORT_ID_TDM_PORT_RANGE_START 0x9000
  110. /* End of the range of port IDs for TDM devices. */
  111. #define AFE_PORT_ID_TDM_PORT_RANGE_END \
  112. (AFE_PORT_ID_TDM_PORT_RANGE_START+0x50-1)
  113. /* Size of the range of port IDs for TDM ports. */
  114. #define AFE_PORT_ID_TDM_PORT_RANGE_SIZE \
  115. (AFE_PORT_ID_TDM_PORT_RANGE_END - \
  116. AFE_PORT_ID_TDM_PORT_RANGE_START+1)
  117. #define AFE_PORT_ID_PRIMARY_TDM_RX \
  118. (AFE_PORT_ID_TDM_PORT_RANGE_START + 0x00)
  119. #define AFE_PORT_ID_PRIMARY_TDM_RX_1 \
  120. (AFE_PORT_ID_PRIMARY_TDM_RX + 0x02)
  121. #define AFE_PORT_ID_PRIMARY_TDM_RX_2 \
  122. (AFE_PORT_ID_PRIMARY_TDM_RX + 0x04)
  123. #define AFE_PORT_ID_PRIMARY_TDM_RX_3 \
  124. (AFE_PORT_ID_PRIMARY_TDM_RX + 0x06)
  125. #define AFE_PORT_ID_PRIMARY_TDM_RX_4 \
  126. (AFE_PORT_ID_PRIMARY_TDM_RX + 0x08)
  127. #define AFE_PORT_ID_PRIMARY_TDM_RX_5 \
  128. (AFE_PORT_ID_PRIMARY_TDM_RX + 0x0A)
  129. #define AFE_PORT_ID_PRIMARY_TDM_RX_6 \
  130. (AFE_PORT_ID_PRIMARY_TDM_RX + 0x0C)
  131. #define AFE_PORT_ID_PRIMARY_TDM_RX_7 \
  132. (AFE_PORT_ID_PRIMARY_TDM_RX + 0x0E)
  133. #define AFE_PORT_ID_PRIMARY_TDM_TX \
  134. (AFE_PORT_ID_TDM_PORT_RANGE_START + 0x01)
  135. #define AFE_PORT_ID_PRIMARY_TDM_TX_1 \
  136. (AFE_PORT_ID_PRIMARY_TDM_TX + 0x02)
  137. #define AFE_PORT_ID_PRIMARY_TDM_TX_2 \
  138. (AFE_PORT_ID_PRIMARY_TDM_TX + 0x04)
  139. #define AFE_PORT_ID_PRIMARY_TDM_TX_3 \
  140. (AFE_PORT_ID_PRIMARY_TDM_TX + 0x06)
  141. #define AFE_PORT_ID_PRIMARY_TDM_TX_4 \
  142. (AFE_PORT_ID_PRIMARY_TDM_TX + 0x08)
  143. #define AFE_PORT_ID_PRIMARY_TDM_TX_5 \
  144. (AFE_PORT_ID_PRIMARY_TDM_TX + 0x0A)
  145. #define AFE_PORT_ID_PRIMARY_TDM_TX_6 \
  146. (AFE_PORT_ID_PRIMARY_TDM_TX + 0x0C)
  147. #define AFE_PORT_ID_PRIMARY_TDM_TX_7 \
  148. (AFE_PORT_ID_PRIMARY_TDM_TX + 0x0E)
  149. #define AFE_PORT_ID_SECONDARY_TDM_RX \
  150. (AFE_PORT_ID_TDM_PORT_RANGE_START + 0x10)
  151. #define AFE_PORT_ID_SECONDARY_TDM_RX_1 \
  152. (AFE_PORT_ID_SECONDARY_TDM_RX + 0x02)
  153. #define AFE_PORT_ID_SECONDARY_TDM_RX_2 \
  154. (AFE_PORT_ID_SECONDARY_TDM_RX + 0x04)
  155. #define AFE_PORT_ID_SECONDARY_TDM_RX_3 \
  156. (AFE_PORT_ID_SECONDARY_TDM_RX + 0x06)
  157. #define AFE_PORT_ID_SECONDARY_TDM_RX_4 \
  158. (AFE_PORT_ID_SECONDARY_TDM_RX + 0x08)
  159. #define AFE_PORT_ID_SECONDARY_TDM_RX_5 \
  160. (AFE_PORT_ID_SECONDARY_TDM_RX + 0x0A)
  161. #define AFE_PORT_ID_SECONDARY_TDM_RX_6 \
  162. (AFE_PORT_ID_SECONDARY_TDM_RX + 0x0C)
  163. #define AFE_PORT_ID_SECONDARY_TDM_RX_7 \
  164. (AFE_PORT_ID_SECONDARY_TDM_RX + 0x0E)
  165. #define AFE_PORT_ID_SECONDARY_TDM_TX \
  166. (AFE_PORT_ID_TDM_PORT_RANGE_START + 0x11)
  167. #define AFE_PORT_ID_SECONDARY_TDM_TX_1 \
  168. (AFE_PORT_ID_SECONDARY_TDM_TX + 0x02)
  169. #define AFE_PORT_ID_SECONDARY_TDM_TX_2 \
  170. (AFE_PORT_ID_SECONDARY_TDM_TX + 0x04)
  171. #define AFE_PORT_ID_SECONDARY_TDM_TX_3 \
  172. (AFE_PORT_ID_SECONDARY_TDM_TX + 0x06)
  173. #define AFE_PORT_ID_SECONDARY_TDM_TX_4 \
  174. (AFE_PORT_ID_SECONDARY_TDM_TX + 0x08)
  175. #define AFE_PORT_ID_SECONDARY_TDM_TX_5 \
  176. (AFE_PORT_ID_SECONDARY_TDM_TX + 0x0A)
  177. #define AFE_PORT_ID_SECONDARY_TDM_TX_6 \
  178. (AFE_PORT_ID_SECONDARY_TDM_TX + 0x0C)
  179. #define AFE_PORT_ID_SECONDARY_TDM_TX_7 \
  180. (AFE_PORT_ID_SECONDARY_TDM_TX + 0x0E)
  181. #define AFE_PORT_ID_TERTIARY_TDM_RX \
  182. (AFE_PORT_ID_TDM_PORT_RANGE_START + 0x20)
  183. #define AFE_PORT_ID_TERTIARY_TDM_RX_1 \
  184. (AFE_PORT_ID_TERTIARY_TDM_RX + 0x02)
  185. #define AFE_PORT_ID_TERTIARY_TDM_RX_2 \
  186. (AFE_PORT_ID_TERTIARY_TDM_RX + 0x04)
  187. #define AFE_PORT_ID_TERTIARY_TDM_RX_3 \
  188. (AFE_PORT_ID_TERTIARY_TDM_RX + 0x06)
  189. #define AFE_PORT_ID_TERTIARY_TDM_RX_4 \
  190. (AFE_PORT_ID_TERTIARY_TDM_RX + 0x08)
  191. #define AFE_PORT_ID_TERTIARY_TDM_RX_5 \
  192. (AFE_PORT_ID_TERTIARY_TDM_RX + 0x0A)
  193. #define AFE_PORT_ID_TERTIARY_TDM_RX_6 \
  194. (AFE_PORT_ID_TERTIARY_TDM_RX + 0x0C)
  195. #define AFE_PORT_ID_TERTIARY_TDM_RX_7 \
  196. (AFE_PORT_ID_TERTIARY_TDM_RX + 0x0E)
  197. #define AFE_PORT_ID_TERTIARY_TDM_TX \
  198. (AFE_PORT_ID_TDM_PORT_RANGE_START + 0x21)
  199. #define AFE_PORT_ID_TERTIARY_TDM_TX_1 \
  200. (AFE_PORT_ID_TERTIARY_TDM_TX + 0x02)
  201. #define AFE_PORT_ID_TERTIARY_TDM_TX_2 \
  202. (AFE_PORT_ID_TERTIARY_TDM_TX + 0x04)
  203. #define AFE_PORT_ID_TERTIARY_TDM_TX_3 \
  204. (AFE_PORT_ID_TERTIARY_TDM_TX + 0x06)
  205. #define AFE_PORT_ID_TERTIARY_TDM_TX_4 \
  206. (AFE_PORT_ID_TERTIARY_TDM_TX + 0x08)
  207. #define AFE_PORT_ID_TERTIARY_TDM_TX_5 \
  208. (AFE_PORT_ID_TERTIARY_TDM_TX + 0x0A)
  209. #define AFE_PORT_ID_TERTIARY_TDM_TX_6 \
  210. (AFE_PORT_ID_TERTIARY_TDM_TX + 0x0C)
  211. #define AFE_PORT_ID_TERTIARY_TDM_TX_7 \
  212. (AFE_PORT_ID_TERTIARY_TDM_TX + 0x0E)
  213. #define AFE_PORT_ID_QUATERNARY_TDM_RX \
  214. (AFE_PORT_ID_TDM_PORT_RANGE_START + 0x30)
  215. #define AFE_PORT_ID_QUATERNARY_TDM_RX_1 \
  216. (AFE_PORT_ID_QUATERNARY_TDM_RX + 0x02)
  217. #define AFE_PORT_ID_QUATERNARY_TDM_RX_2 \
  218. (AFE_PORT_ID_QUATERNARY_TDM_RX + 0x04)
  219. #define AFE_PORT_ID_QUATERNARY_TDM_RX_3 \
  220. (AFE_PORT_ID_QUATERNARY_TDM_RX + 0x06)
  221. #define AFE_PORT_ID_QUATERNARY_TDM_RX_4 \
  222. (AFE_PORT_ID_QUATERNARY_TDM_RX + 0x08)
  223. #define AFE_PORT_ID_QUATERNARY_TDM_RX_5 \
  224. (AFE_PORT_ID_QUATERNARY_TDM_RX + 0x0A)
  225. #define AFE_PORT_ID_QUATERNARY_TDM_RX_6 \
  226. (AFE_PORT_ID_QUATERNARY_TDM_RX + 0x0C)
  227. #define AFE_PORT_ID_QUATERNARY_TDM_RX_7 \
  228. (AFE_PORT_ID_QUATERNARY_TDM_RX + 0x0E)
  229. #define AFE_PORT_ID_QUATERNARY_TDM_TX \
  230. (AFE_PORT_ID_TDM_PORT_RANGE_START + 0x31)
  231. #define AFE_PORT_ID_QUATERNARY_TDM_TX_1 \
  232. (AFE_PORT_ID_QUATERNARY_TDM_TX + 0x02)
  233. #define AFE_PORT_ID_QUATERNARY_TDM_TX_2 \
  234. (AFE_PORT_ID_QUATERNARY_TDM_TX + 0x04)
  235. #define AFE_PORT_ID_QUATERNARY_TDM_TX_3 \
  236. (AFE_PORT_ID_QUATERNARY_TDM_TX + 0x06)
  237. #define AFE_PORT_ID_QUATERNARY_TDM_TX_4 \
  238. (AFE_PORT_ID_QUATERNARY_TDM_TX + 0x08)
  239. #define AFE_PORT_ID_QUATERNARY_TDM_TX_5 \
  240. (AFE_PORT_ID_QUATERNARY_TDM_TX + 0x0A)
  241. #define AFE_PORT_ID_QUATERNARY_TDM_TX_6 \
  242. (AFE_PORT_ID_QUATERNARY_TDM_TX + 0x0C)
  243. #define AFE_PORT_ID_QUATERNARY_TDM_TX_7 \
  244. (AFE_PORT_ID_QUATERNARY_TDM_TX + 0x0E)
  245. #define AFE_PORT_ID_QUINARY_TDM_RX \
  246. (AFE_PORT_ID_TDM_PORT_RANGE_START + 0x40)
  247. #define AFE_PORT_ID_QUINARY_TDM_RX_1 \
  248. (AFE_PORT_ID_QUINARY_TDM_RX + 0x02)
  249. #define AFE_PORT_ID_QUINARY_TDM_RX_2 \
  250. (AFE_PORT_ID_QUINARY_TDM_RX + 0x04)
  251. #define AFE_PORT_ID_QUINARY_TDM_RX_3 \
  252. (AFE_PORT_ID_QUINARY_TDM_RX + 0x06)
  253. #define AFE_PORT_ID_QUINARY_TDM_RX_4 \
  254. (AFE_PORT_ID_QUINARY_TDM_RX + 0x08)
  255. #define AFE_PORT_ID_QUINARY_TDM_RX_5 \
  256. (AFE_PORT_ID_QUINARY_TDM_RX + 0x0A)
  257. #define AFE_PORT_ID_QUINARY_TDM_RX_6 \
  258. (AFE_PORT_ID_QUINARY_TDM_RX + 0x0C)
  259. #define AFE_PORT_ID_QUINARY_TDM_RX_7 \
  260. (AFE_PORT_ID_QUINARY_TDM_RX + 0x0E)
  261. #define AFE_PORT_ID_QUINARY_TDM_TX \
  262. (AFE_PORT_ID_TDM_PORT_RANGE_START + 0x41)
  263. #define AFE_PORT_ID_QUINARY_TDM_TX_1 \
  264. (AFE_PORT_ID_QUINARY_TDM_TX + 0x02)
  265. #define AFE_PORT_ID_QUINARY_TDM_TX_2 \
  266. (AFE_PORT_ID_QUINARY_TDM_TX + 0x04)
  267. #define AFE_PORT_ID_QUINARY_TDM_TX_3 \
  268. (AFE_PORT_ID_QUINARY_TDM_TX + 0x06)
  269. #define AFE_PORT_ID_QUINARY_TDM_TX_4 \
  270. (AFE_PORT_ID_QUINARY_TDM_TX + 0x08)
  271. #define AFE_PORT_ID_QUINARY_TDM_TX_5 \
  272. (AFE_PORT_ID_QUINARY_TDM_TX + 0x0A)
  273. #define AFE_PORT_ID_QUINARY_TDM_TX_6 \
  274. (AFE_PORT_ID_QUINARY_TDM_TX + 0x0C)
  275. #define AFE_PORT_ID_QUINARY_TDM_TX_7 \
  276. (AFE_PORT_ID_QUINARY_TDM_TX + 0x0E)
  277. #define Q6AFE_LPASS_MODE_CLK1_VALID 1
  278. #define Q6AFE_LPASS_MODE_CLK2_VALID 2
  279. #define Q6AFE_LPASS_CLK_SRC_INTERNAL 1
  280. #define Q6AFE_LPASS_CLK_ROOT_DEFAULT 0
  281. #define AFE_API_VERSION_TDM_CONFIG 1
  282. #define AFE_API_VERSION_SLOT_MAPPING_CONFIG 1
  283. #define TIMEOUT_MS 1000
  284. #define AFE_CMD_RESP_AVAIL 0
  285. #define AFE_CMD_RESP_NONE 1
  286. struct q6afe {
  287. struct apr_device *apr;
  288. struct device *dev;
  289. struct q6core_svc_api_info ainfo;
  290. struct mutex lock;
  291. struct list_head port_list;
  292. spinlock_t port_list_lock;
  293. };
  294. struct afe_port_cmd_device_start {
  295. u16 port_id;
  296. u16 reserved;
  297. } __packed;
  298. struct afe_port_cmd_device_stop {
  299. u16 port_id;
  300. u16 reserved;
  301. /* Reserved for 32-bit alignment. This field must be set to 0.*/
  302. } __packed;
  303. struct afe_port_param_data_v2 {
  304. u32 module_id;
  305. u32 param_id;
  306. u16 param_size;
  307. u16 reserved;
  308. } __packed;
  309. struct afe_svc_cmd_set_param {
  310. uint32_t payload_size;
  311. uint32_t payload_address_lsw;
  312. uint32_t payload_address_msw;
  313. uint32_t mem_map_handle;
  314. } __packed;
  315. struct afe_port_cmd_set_param_v2 {
  316. u16 port_id;
  317. u16 payload_size;
  318. u32 payload_address_lsw;
  319. u32 payload_address_msw;
  320. u32 mem_map_handle;
  321. } __packed;
  322. struct afe_param_id_hdmi_multi_chan_audio_cfg {
  323. u32 hdmi_cfg_minor_version;
  324. u16 datatype;
  325. u16 channel_allocation;
  326. u32 sample_rate;
  327. u16 bit_width;
  328. u16 reserved;
  329. } __packed;
  330. struct afe_param_id_slimbus_cfg {
  331. u32 sb_cfg_minor_version;
  332. /* Minor version used for tracking the version of the SLIMBUS
  333. * configuration interface.
  334. * Supported values: #AFE_API_VERSION_SLIMBUS_CONFIG
  335. */
  336. u16 slimbus_dev_id;
  337. /* SLIMbus hardware device ID, which is required to handle
  338. * multiple SLIMbus hardware blocks.
  339. * Supported values: - #AFE_SLIMBUS_DEVICE_1 - #AFE_SLIMBUS_DEVICE_2
  340. */
  341. u16 bit_width;
  342. /* Bit width of the sample.
  343. * Supported values: 16, 24
  344. */
  345. u16 data_format;
  346. /* Data format supported by the SLIMbus hardware. The default is
  347. * 0 (#AFE_SB_DATA_FORMAT_NOT_INDICATED), which indicates the
  348. * hardware does not perform any format conversions before the data
  349. * transfer.
  350. */
  351. u16 num_channels;
  352. /* Number of channels.
  353. * Supported values: 1 to #AFE_PORT_MAX_AUDIO_CHAN_CNT
  354. */
  355. u8 shared_ch_mapping[AFE_PORT_MAX_AUDIO_CHAN_CNT];
  356. /* Mapping of shared channel IDs (128 to 255) to which the
  357. * master port is to be connected.
  358. * Shared_channel_mapping[i] represents the shared channel assigned
  359. * for audio channel i in multichannel audio data.
  360. */
  361. u32 sample_rate;
  362. /* Sampling rate of the port.
  363. * Supported values:
  364. * - #AFE_PORT_SAMPLE_RATE_8K
  365. * - #AFE_PORT_SAMPLE_RATE_16K
  366. * - #AFE_PORT_SAMPLE_RATE_48K
  367. * - #AFE_PORT_SAMPLE_RATE_96K
  368. * - #AFE_PORT_SAMPLE_RATE_192K
  369. */
  370. } __packed;
  371. struct afe_clk_cfg {
  372. u32 i2s_cfg_minor_version;
  373. u32 clk_val1;
  374. u32 clk_val2;
  375. u16 clk_src;
  376. u16 clk_root;
  377. u16 clk_set_mode;
  378. u16 reserved;
  379. } __packed;
  380. struct afe_digital_clk_cfg {
  381. u32 i2s_cfg_minor_version;
  382. u32 clk_val;
  383. u16 clk_root;
  384. u16 reserved;
  385. } __packed;
  386. struct afe_param_id_i2s_cfg {
  387. u32 i2s_cfg_minor_version;
  388. u16 bit_width;
  389. u16 channel_mode;
  390. u16 mono_stereo;
  391. u16 ws_src;
  392. u32 sample_rate;
  393. u16 data_format;
  394. u16 reserved;
  395. } __packed;
  396. struct afe_param_id_tdm_cfg {
  397. u32 tdm_cfg_minor_version;
  398. u32 num_channels;
  399. u32 sample_rate;
  400. u32 bit_width;
  401. u16 data_format;
  402. u16 sync_mode;
  403. u16 sync_src;
  404. u16 nslots_per_frame;
  405. u16 ctrl_data_out_enable;
  406. u16 ctrl_invert_sync_pulse;
  407. u16 ctrl_sync_data_delay;
  408. u16 slot_width;
  409. u32 slot_mask;
  410. } __packed;
  411. union afe_port_config {
  412. struct afe_param_id_hdmi_multi_chan_audio_cfg hdmi_multi_ch;
  413. struct afe_param_id_slimbus_cfg slim_cfg;
  414. struct afe_param_id_i2s_cfg i2s_cfg;
  415. struct afe_param_id_tdm_cfg tdm_cfg;
  416. } __packed;
  417. struct afe_clk_set {
  418. uint32_t clk_set_minor_version;
  419. uint32_t clk_id;
  420. uint32_t clk_freq_in_hz;
  421. uint16_t clk_attri;
  422. uint16_t clk_root;
  423. uint32_t enable;
  424. };
  425. struct afe_param_id_slot_mapping_cfg {
  426. u32 minor_version;
  427. u16 num_channels;
  428. u16 bitwidth;
  429. u32 data_align_type;
  430. u16 ch_mapping[AFE_PORT_MAX_AUDIO_CHAN_CNT];
  431. } __packed;
  432. struct q6afe_port {
  433. wait_queue_head_t wait;
  434. union afe_port_config port_cfg;
  435. struct afe_param_id_slot_mapping_cfg *scfg;
  436. struct aprv2_ibasic_rsp_result_t result;
  437. int token;
  438. int id;
  439. int cfg_type;
  440. struct q6afe *afe;
  441. struct kref refcount;
  442. struct list_head node;
  443. };
  444. struct afe_port_map {
  445. int port_id;
  446. int token;
  447. int is_rx;
  448. int is_dig_pcm;
  449. };
  450. /*
  451. * Mapping between Virtual Port IDs to DSP AFE Port ID
  452. * On B Family SoCs DSP Port IDs are consistent across multiple SoCs
  453. * on A Family SoCs DSP port IDs are same as virtual Port IDs.
  454. */
  455. static struct afe_port_map port_maps[AFE_PORT_MAX] = {
  456. [HDMI_RX] = { AFE_PORT_ID_MULTICHAN_HDMI_RX, HDMI_RX, 1, 1},
  457. [SLIMBUS_0_RX] = { AFE_PORT_ID_SLIMBUS_MULTI_CHAN_0_RX,
  458. SLIMBUS_0_RX, 1, 1},
  459. [SLIMBUS_1_RX] = { AFE_PORT_ID_SLIMBUS_MULTI_CHAN_1_RX,
  460. SLIMBUS_1_RX, 1, 1},
  461. [SLIMBUS_2_RX] = { AFE_PORT_ID_SLIMBUS_MULTI_CHAN_2_RX,
  462. SLIMBUS_2_RX, 1, 1},
  463. [SLIMBUS_3_RX] = { AFE_PORT_ID_SLIMBUS_MULTI_CHAN_3_RX,
  464. SLIMBUS_3_RX, 1, 1},
  465. [SLIMBUS_4_RX] = { AFE_PORT_ID_SLIMBUS_MULTI_CHAN_4_RX,
  466. SLIMBUS_4_RX, 1, 1},
  467. [SLIMBUS_5_RX] = { AFE_PORT_ID_SLIMBUS_MULTI_CHAN_5_RX,
  468. SLIMBUS_5_RX, 1, 1},
  469. [SLIMBUS_6_RX] = { AFE_PORT_ID_SLIMBUS_MULTI_CHAN_6_RX,
  470. SLIMBUS_6_RX, 1, 1},
  471. [SLIMBUS_0_TX] = { AFE_PORT_ID_SLIMBUS_MULTI_CHAN_0_TX,
  472. SLIMBUS_0_TX, 0, 1},
  473. [SLIMBUS_1_TX] = { AFE_PORT_ID_SLIMBUS_MULTI_CHAN_1_TX,
  474. SLIMBUS_1_TX, 0, 1},
  475. [SLIMBUS_2_TX] = { AFE_PORT_ID_SLIMBUS_MULTI_CHAN_2_TX,
  476. SLIMBUS_2_TX, 0, 1},
  477. [SLIMBUS_3_TX] = { AFE_PORT_ID_SLIMBUS_MULTI_CHAN_3_TX,
  478. SLIMBUS_3_TX, 0, 1},
  479. [SLIMBUS_4_TX] = { AFE_PORT_ID_SLIMBUS_MULTI_CHAN_4_TX,
  480. SLIMBUS_4_TX, 0, 1},
  481. [SLIMBUS_5_TX] = { AFE_PORT_ID_SLIMBUS_MULTI_CHAN_5_TX,
  482. SLIMBUS_5_TX, 0, 1},
  483. [SLIMBUS_6_TX] = { AFE_PORT_ID_SLIMBUS_MULTI_CHAN_6_TX,
  484. SLIMBUS_6_TX, 0, 1},
  485. [PRIMARY_MI2S_RX] = { AFE_PORT_ID_PRIMARY_MI2S_RX,
  486. PRIMARY_MI2S_RX, 1, 1},
  487. [PRIMARY_MI2S_TX] = { AFE_PORT_ID_PRIMARY_MI2S_TX,
  488. PRIMARY_MI2S_RX, 0, 1},
  489. [SECONDARY_MI2S_RX] = { AFE_PORT_ID_SECONDARY_MI2S_RX,
  490. SECONDARY_MI2S_RX, 1, 1},
  491. [SECONDARY_MI2S_TX] = { AFE_PORT_ID_SECONDARY_MI2S_TX,
  492. SECONDARY_MI2S_TX, 0, 1},
  493. [TERTIARY_MI2S_RX] = { AFE_PORT_ID_TERTIARY_MI2S_RX,
  494. TERTIARY_MI2S_RX, 1, 1},
  495. [TERTIARY_MI2S_TX] = { AFE_PORT_ID_TERTIARY_MI2S_TX,
  496. TERTIARY_MI2S_TX, 0, 1},
  497. [QUATERNARY_MI2S_RX] = { AFE_PORT_ID_QUATERNARY_MI2S_RX,
  498. QUATERNARY_MI2S_RX, 1, 1},
  499. [QUATERNARY_MI2S_TX] = { AFE_PORT_ID_QUATERNARY_MI2S_TX,
  500. QUATERNARY_MI2S_TX, 0, 1},
  501. [PRIMARY_TDM_RX_0] = { AFE_PORT_ID_PRIMARY_TDM_RX,
  502. PRIMARY_TDM_RX_0, 1, 1},
  503. [PRIMARY_TDM_TX_0] = { AFE_PORT_ID_PRIMARY_TDM_TX,
  504. PRIMARY_TDM_TX_0, 0, 1},
  505. [PRIMARY_TDM_RX_1] = { AFE_PORT_ID_PRIMARY_TDM_RX_1,
  506. PRIMARY_TDM_RX_1, 1, 1},
  507. [PRIMARY_TDM_TX_1] = { AFE_PORT_ID_PRIMARY_TDM_TX_1,
  508. PRIMARY_TDM_TX_1, 0, 1},
  509. [PRIMARY_TDM_RX_2] = { AFE_PORT_ID_PRIMARY_TDM_RX_2,
  510. PRIMARY_TDM_RX_2, 1, 1},
  511. [PRIMARY_TDM_TX_2] = { AFE_PORT_ID_PRIMARY_TDM_TX_2,
  512. PRIMARY_TDM_TX_2, 0, 1},
  513. [PRIMARY_TDM_RX_3] = { AFE_PORT_ID_PRIMARY_TDM_RX_3,
  514. PRIMARY_TDM_RX_3, 1, 1},
  515. [PRIMARY_TDM_TX_3] = { AFE_PORT_ID_PRIMARY_TDM_TX_3,
  516. PRIMARY_TDM_TX_3, 0, 1},
  517. [PRIMARY_TDM_RX_4] = { AFE_PORT_ID_PRIMARY_TDM_RX_4,
  518. PRIMARY_TDM_RX_4, 1, 1},
  519. [PRIMARY_TDM_TX_4] = { AFE_PORT_ID_PRIMARY_TDM_TX_4,
  520. PRIMARY_TDM_TX_4, 0, 1},
  521. [PRIMARY_TDM_RX_5] = { AFE_PORT_ID_PRIMARY_TDM_RX_5,
  522. PRIMARY_TDM_RX_5, 1, 1},
  523. [PRIMARY_TDM_TX_5] = { AFE_PORT_ID_PRIMARY_TDM_TX_5,
  524. PRIMARY_TDM_TX_5, 0, 1},
  525. [PRIMARY_TDM_RX_6] = { AFE_PORT_ID_PRIMARY_TDM_RX_6,
  526. PRIMARY_TDM_RX_6, 1, 1},
  527. [PRIMARY_TDM_TX_6] = { AFE_PORT_ID_PRIMARY_TDM_TX_6,
  528. PRIMARY_TDM_TX_6, 0, 1},
  529. [PRIMARY_TDM_RX_7] = { AFE_PORT_ID_PRIMARY_TDM_RX_7,
  530. PRIMARY_TDM_RX_7, 1, 1},
  531. [PRIMARY_TDM_TX_7] = { AFE_PORT_ID_PRIMARY_TDM_TX_7,
  532. PRIMARY_TDM_TX_7, 0, 1},
  533. [SECONDARY_TDM_RX_0] = { AFE_PORT_ID_SECONDARY_TDM_RX,
  534. SECONDARY_TDM_RX_0, 1, 1},
  535. [SECONDARY_TDM_TX_0] = { AFE_PORT_ID_SECONDARY_TDM_TX,
  536. SECONDARY_TDM_TX_0, 0, 1},
  537. [SECONDARY_TDM_RX_1] = { AFE_PORT_ID_SECONDARY_TDM_RX_1,
  538. SECONDARY_TDM_RX_1, 1, 1},
  539. [SECONDARY_TDM_TX_1] = { AFE_PORT_ID_SECONDARY_TDM_TX_1,
  540. SECONDARY_TDM_TX_1, 0, 1},
  541. [SECONDARY_TDM_RX_2] = { AFE_PORT_ID_SECONDARY_TDM_RX_2,
  542. SECONDARY_TDM_RX_2, 1, 1},
  543. [SECONDARY_TDM_TX_2] = { AFE_PORT_ID_SECONDARY_TDM_TX_2,
  544. SECONDARY_TDM_TX_2, 0, 1},
  545. [SECONDARY_TDM_RX_3] = { AFE_PORT_ID_SECONDARY_TDM_RX_3,
  546. SECONDARY_TDM_RX_3, 1, 1},
  547. [SECONDARY_TDM_TX_3] = { AFE_PORT_ID_SECONDARY_TDM_TX_3,
  548. SECONDARY_TDM_TX_3, 0, 1},
  549. [SECONDARY_TDM_RX_4] = { AFE_PORT_ID_SECONDARY_TDM_RX_4,
  550. SECONDARY_TDM_RX_4, 1, 1},
  551. [SECONDARY_TDM_TX_4] = { AFE_PORT_ID_SECONDARY_TDM_TX_4,
  552. SECONDARY_TDM_TX_4, 0, 1},
  553. [SECONDARY_TDM_RX_5] = { AFE_PORT_ID_SECONDARY_TDM_RX_5,
  554. SECONDARY_TDM_RX_5, 1, 1},
  555. [SECONDARY_TDM_TX_5] = { AFE_PORT_ID_SECONDARY_TDM_TX_5,
  556. SECONDARY_TDM_TX_5, 0, 1},
  557. [SECONDARY_TDM_RX_6] = { AFE_PORT_ID_SECONDARY_TDM_RX_6,
  558. SECONDARY_TDM_RX_6, 1, 1},
  559. [SECONDARY_TDM_TX_6] = { AFE_PORT_ID_SECONDARY_TDM_TX_6,
  560. SECONDARY_TDM_TX_6, 0, 1},
  561. [SECONDARY_TDM_RX_7] = { AFE_PORT_ID_SECONDARY_TDM_RX_7,
  562. SECONDARY_TDM_RX_7, 1, 1},
  563. [SECONDARY_TDM_TX_7] = { AFE_PORT_ID_SECONDARY_TDM_TX_7,
  564. SECONDARY_TDM_TX_7, 0, 1},
  565. [TERTIARY_TDM_RX_0] = { AFE_PORT_ID_TERTIARY_TDM_RX,
  566. TERTIARY_TDM_RX_0, 1, 1},
  567. [TERTIARY_TDM_TX_0] = { AFE_PORT_ID_TERTIARY_TDM_TX,
  568. TERTIARY_TDM_TX_0, 0, 1},
  569. [TERTIARY_TDM_RX_1] = { AFE_PORT_ID_TERTIARY_TDM_RX_1,
  570. TERTIARY_TDM_RX_1, 1, 1},
  571. [TERTIARY_TDM_TX_1] = { AFE_PORT_ID_TERTIARY_TDM_TX_1,
  572. TERTIARY_TDM_TX_1, 0, 1},
  573. [TERTIARY_TDM_RX_2] = { AFE_PORT_ID_TERTIARY_TDM_RX_2,
  574. TERTIARY_TDM_RX_2, 1, 1},
  575. [TERTIARY_TDM_TX_2] = { AFE_PORT_ID_TERTIARY_TDM_TX_2,
  576. TERTIARY_TDM_TX_2, 0, 1},
  577. [TERTIARY_TDM_RX_3] = { AFE_PORT_ID_TERTIARY_TDM_RX_3,
  578. TERTIARY_TDM_RX_3, 1, 1},
  579. [TERTIARY_TDM_TX_3] = { AFE_PORT_ID_TERTIARY_TDM_TX_3,
  580. TERTIARY_TDM_TX_3, 0, 1},
  581. [TERTIARY_TDM_RX_4] = { AFE_PORT_ID_TERTIARY_TDM_RX_4,
  582. TERTIARY_TDM_RX_4, 1, 1},
  583. [TERTIARY_TDM_TX_4] = { AFE_PORT_ID_TERTIARY_TDM_TX_4,
  584. TERTIARY_TDM_TX_4, 0, 1},
  585. [TERTIARY_TDM_RX_5] = { AFE_PORT_ID_TERTIARY_TDM_RX_5,
  586. TERTIARY_TDM_RX_5, 1, 1},
  587. [TERTIARY_TDM_TX_5] = { AFE_PORT_ID_TERTIARY_TDM_TX_5,
  588. TERTIARY_TDM_TX_5, 0, 1},
  589. [TERTIARY_TDM_RX_6] = { AFE_PORT_ID_TERTIARY_TDM_RX_6,
  590. TERTIARY_TDM_RX_6, 1, 1},
  591. [TERTIARY_TDM_TX_6] = { AFE_PORT_ID_TERTIARY_TDM_TX_6,
  592. TERTIARY_TDM_TX_6, 0, 1},
  593. [TERTIARY_TDM_RX_7] = { AFE_PORT_ID_TERTIARY_TDM_RX_7,
  594. TERTIARY_TDM_RX_7, 1, 1},
  595. [TERTIARY_TDM_TX_7] = { AFE_PORT_ID_TERTIARY_TDM_TX_7,
  596. TERTIARY_TDM_TX_7, 0, 1},
  597. [QUATERNARY_TDM_RX_0] = { AFE_PORT_ID_QUATERNARY_TDM_RX,
  598. QUATERNARY_TDM_RX_0, 1, 1},
  599. [QUATERNARY_TDM_TX_0] = { AFE_PORT_ID_QUATERNARY_TDM_TX,
  600. QUATERNARY_TDM_TX_0, 0, 1},
  601. [QUATERNARY_TDM_RX_1] = { AFE_PORT_ID_QUATERNARY_TDM_RX_1,
  602. QUATERNARY_TDM_RX_1, 1, 1},
  603. [QUATERNARY_TDM_TX_1] = { AFE_PORT_ID_QUATERNARY_TDM_TX_1,
  604. QUATERNARY_TDM_TX_1, 0, 1},
  605. [QUATERNARY_TDM_RX_2] = { AFE_PORT_ID_QUATERNARY_TDM_RX_2,
  606. QUATERNARY_TDM_RX_2, 1, 1},
  607. [QUATERNARY_TDM_TX_2] = { AFE_PORT_ID_QUATERNARY_TDM_TX_2,
  608. QUATERNARY_TDM_TX_2, 0, 1},
  609. [QUATERNARY_TDM_RX_3] = { AFE_PORT_ID_QUATERNARY_TDM_RX_3,
  610. QUATERNARY_TDM_RX_3, 1, 1},
  611. [QUATERNARY_TDM_TX_3] = { AFE_PORT_ID_QUATERNARY_TDM_TX_3,
  612. QUATERNARY_TDM_TX_3, 0, 1},
  613. [QUATERNARY_TDM_RX_4] = { AFE_PORT_ID_QUATERNARY_TDM_RX_4,
  614. QUATERNARY_TDM_RX_4, 1, 1},
  615. [QUATERNARY_TDM_TX_4] = { AFE_PORT_ID_QUATERNARY_TDM_TX_4,
  616. QUATERNARY_TDM_TX_4, 0, 1},
  617. [QUATERNARY_TDM_RX_5] = { AFE_PORT_ID_QUATERNARY_TDM_RX_5,
  618. QUATERNARY_TDM_RX_5, 1, 1},
  619. [QUATERNARY_TDM_TX_5] = { AFE_PORT_ID_QUATERNARY_TDM_TX_5,
  620. QUATERNARY_TDM_TX_5, 0, 1},
  621. [QUATERNARY_TDM_RX_6] = { AFE_PORT_ID_QUATERNARY_TDM_RX_6,
  622. QUATERNARY_TDM_RX_6, 1, 1},
  623. [QUATERNARY_TDM_TX_6] = { AFE_PORT_ID_QUATERNARY_TDM_TX_6,
  624. QUATERNARY_TDM_TX_6, 0, 1},
  625. [QUATERNARY_TDM_RX_7] = { AFE_PORT_ID_QUATERNARY_TDM_RX_7,
  626. QUATERNARY_TDM_RX_7, 1, 1},
  627. [QUATERNARY_TDM_TX_7] = { AFE_PORT_ID_QUATERNARY_TDM_TX_7,
  628. QUATERNARY_TDM_TX_7, 0, 1},
  629. [QUINARY_TDM_RX_0] = { AFE_PORT_ID_QUINARY_TDM_RX,
  630. QUINARY_TDM_RX_0, 1, 1},
  631. [QUINARY_TDM_TX_0] = { AFE_PORT_ID_QUINARY_TDM_TX,
  632. QUINARY_TDM_TX_0, 0, 1},
  633. [QUINARY_TDM_RX_1] = { AFE_PORT_ID_QUINARY_TDM_RX_1,
  634. QUINARY_TDM_RX_1, 1, 1},
  635. [QUINARY_TDM_TX_1] = { AFE_PORT_ID_QUINARY_TDM_TX_1,
  636. QUINARY_TDM_TX_1, 0, 1},
  637. [QUINARY_TDM_RX_2] = { AFE_PORT_ID_QUINARY_TDM_RX_2,
  638. QUINARY_TDM_RX_2, 1, 1},
  639. [QUINARY_TDM_TX_2] = { AFE_PORT_ID_QUINARY_TDM_TX_2,
  640. QUINARY_TDM_TX_2, 0, 1},
  641. [QUINARY_TDM_RX_3] = { AFE_PORT_ID_QUINARY_TDM_RX_3,
  642. QUINARY_TDM_RX_3, 1, 1},
  643. [QUINARY_TDM_TX_3] = { AFE_PORT_ID_QUINARY_TDM_TX_3,
  644. QUINARY_TDM_TX_3, 0, 1},
  645. [QUINARY_TDM_RX_4] = { AFE_PORT_ID_QUINARY_TDM_RX_4,
  646. QUINARY_TDM_RX_4, 1, 1},
  647. [QUINARY_TDM_TX_4] = { AFE_PORT_ID_QUINARY_TDM_TX_4,
  648. QUINARY_TDM_TX_4, 0, 1},
  649. [QUINARY_TDM_RX_5] = { AFE_PORT_ID_QUINARY_TDM_RX_5,
  650. QUINARY_TDM_RX_5, 1, 1},
  651. [QUINARY_TDM_TX_5] = { AFE_PORT_ID_QUINARY_TDM_TX_5,
  652. QUINARY_TDM_TX_5, 0, 1},
  653. [QUINARY_TDM_RX_6] = { AFE_PORT_ID_QUINARY_TDM_RX_6,
  654. QUINARY_TDM_RX_6, 1, 1},
  655. [QUINARY_TDM_TX_6] = { AFE_PORT_ID_QUINARY_TDM_TX_6,
  656. QUINARY_TDM_TX_6, 0, 1},
  657. [QUINARY_TDM_RX_7] = { AFE_PORT_ID_QUINARY_TDM_RX_7,
  658. QUINARY_TDM_RX_7, 1, 1},
  659. [QUINARY_TDM_TX_7] = { AFE_PORT_ID_QUINARY_TDM_TX_7,
  660. QUINARY_TDM_TX_7, 0, 1},
  661. };
  662. static void q6afe_port_free(struct kref *ref)
  663. {
  664. struct q6afe_port *port;
  665. struct q6afe *afe;
  666. unsigned long flags;
  667. port = container_of(ref, struct q6afe_port, refcount);
  668. afe = port->afe;
  669. spin_lock_irqsave(&afe->port_list_lock, flags);
  670. list_del(&port->node);
  671. spin_unlock_irqrestore(&afe->port_list_lock, flags);
  672. kfree(port->scfg);
  673. kfree(port);
  674. }
  675. static struct q6afe_port *q6afe_find_port(struct q6afe *afe, int token)
  676. {
  677. struct q6afe_port *p = NULL;
  678. struct q6afe_port *ret = NULL;
  679. unsigned long flags;
  680. spin_lock_irqsave(&afe->port_list_lock, flags);
  681. list_for_each_entry(p, &afe->port_list, node)
  682. if (p->token == token) {
  683. ret = p;
  684. kref_get(&p->refcount);
  685. break;
  686. }
  687. spin_unlock_irqrestore(&afe->port_list_lock, flags);
  688. return ret;
  689. }
  690. static int q6afe_callback(struct apr_device *adev, struct apr_resp_pkt *data)
  691. {
  692. struct q6afe *afe = dev_get_drvdata(&adev->dev);
  693. struct aprv2_ibasic_rsp_result_t *res;
  694. struct apr_hdr *hdr = &data->hdr;
  695. struct q6afe_port *port;
  696. if (!data->payload_size)
  697. return 0;
  698. res = data->payload;
  699. switch (hdr->opcode) {
  700. case APR_BASIC_RSP_RESULT: {
  701. if (res->status) {
  702. dev_err(afe->dev, "cmd = 0x%x returned error = 0x%x\n",
  703. res->opcode, res->status);
  704. }
  705. switch (res->opcode) {
  706. case AFE_PORT_CMD_SET_PARAM_V2:
  707. case AFE_PORT_CMD_DEVICE_STOP:
  708. case AFE_PORT_CMD_DEVICE_START:
  709. case AFE_SVC_CMD_SET_PARAM:
  710. port = q6afe_find_port(afe, hdr->token);
  711. if (port) {
  712. port->result = *res;
  713. wake_up(&port->wait);
  714. kref_put(&port->refcount, q6afe_port_free);
  715. }
  716. break;
  717. default:
  718. dev_err(afe->dev, "Unknown cmd 0x%x\n", res->opcode);
  719. break;
  720. }
  721. }
  722. break;
  723. default:
  724. break;
  725. }
  726. return 0;
  727. }
  728. /**
  729. * q6afe_get_port_id() - Get port id from a given port index
  730. *
  731. * @index: port index
  732. *
  733. * Return: Will be an negative on error or valid port_id on success
  734. */
  735. int q6afe_get_port_id(int index)
  736. {
  737. if (index < 0 || index >= AFE_PORT_MAX)
  738. return -EINVAL;
  739. return port_maps[index].port_id;
  740. }
  741. EXPORT_SYMBOL_GPL(q6afe_get_port_id);
  742. static int afe_apr_send_pkt(struct q6afe *afe, struct apr_pkt *pkt,
  743. struct q6afe_port *port)
  744. {
  745. wait_queue_head_t *wait = &port->wait;
  746. struct apr_hdr *hdr = &pkt->hdr;
  747. int ret;
  748. mutex_lock(&afe->lock);
  749. port->result.opcode = 0;
  750. port->result.status = 0;
  751. ret = apr_send_pkt(afe->apr, pkt);
  752. if (ret < 0) {
  753. dev_err(afe->dev, "packet not transmitted (%d)\n", ret);
  754. ret = -EINVAL;
  755. goto err;
  756. }
  757. ret = wait_event_timeout(*wait, (port->result.opcode == hdr->opcode),
  758. msecs_to_jiffies(TIMEOUT_MS));
  759. if (!ret) {
  760. ret = -ETIMEDOUT;
  761. } else if (port->result.status > 0) {
  762. dev_err(afe->dev, "DSP returned error[%x]\n",
  763. port->result.status);
  764. ret = -EINVAL;
  765. } else {
  766. ret = 0;
  767. }
  768. err:
  769. mutex_unlock(&afe->lock);
  770. return ret;
  771. }
  772. static int q6afe_port_set_param(struct q6afe_port *port, void *data,
  773. int param_id, int module_id, int psize)
  774. {
  775. struct afe_svc_cmd_set_param *param;
  776. struct afe_port_param_data_v2 *pdata;
  777. struct q6afe *afe = port->afe;
  778. struct apr_pkt *pkt;
  779. u16 port_id = port->id;
  780. int ret, pkt_size;
  781. void *p, *pl;
  782. pkt_size = APR_HDR_SIZE + sizeof(*param) + sizeof(*pdata) + psize;
  783. p = kzalloc(pkt_size, GFP_KERNEL);
  784. if (!p)
  785. return -ENOMEM;
  786. pkt = p;
  787. param = p + APR_HDR_SIZE;
  788. pdata = p + APR_HDR_SIZE + sizeof(*param);
  789. pl = p + APR_HDR_SIZE + sizeof(*param) + sizeof(*pdata);
  790. memcpy(pl, data, psize);
  791. pkt->hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  792. APR_HDR_LEN(APR_HDR_SIZE),
  793. APR_PKT_VER);
  794. pkt->hdr.pkt_size = pkt_size;
  795. pkt->hdr.src_port = 0;
  796. pkt->hdr.dest_port = 0;
  797. pkt->hdr.token = port->token;
  798. pkt->hdr.opcode = AFE_SVC_CMD_SET_PARAM;
  799. param->payload_size = sizeof(*pdata) + psize;
  800. param->payload_address_lsw = 0x00;
  801. param->payload_address_msw = 0x00;
  802. param->mem_map_handle = 0x00;
  803. pdata->module_id = module_id;
  804. pdata->param_id = param_id;
  805. pdata->param_size = psize;
  806. ret = afe_apr_send_pkt(afe, pkt, port);
  807. if (ret)
  808. dev_err(afe->dev, "AFE enable for port 0x%x failed %d\n",
  809. port_id, ret);
  810. kfree(pkt);
  811. return ret;
  812. }
  813. static int q6afe_port_set_param_v2(struct q6afe_port *port, void *data,
  814. int param_id, int module_id, int psize)
  815. {
  816. struct afe_port_cmd_set_param_v2 *param;
  817. struct afe_port_param_data_v2 *pdata;
  818. struct q6afe *afe = port->afe;
  819. struct apr_pkt *pkt;
  820. u16 port_id = port->id;
  821. int ret, pkt_size;
  822. void *p, *pl;
  823. pkt_size = APR_HDR_SIZE + sizeof(*param) + sizeof(*pdata) + psize;
  824. p = kzalloc(pkt_size, GFP_KERNEL);
  825. if (!p)
  826. return -ENOMEM;
  827. pkt = p;
  828. param = p + APR_HDR_SIZE;
  829. pdata = p + APR_HDR_SIZE + sizeof(*param);
  830. pl = p + APR_HDR_SIZE + sizeof(*param) + sizeof(*pdata);
  831. memcpy(pl, data, psize);
  832. pkt->hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  833. APR_HDR_LEN(APR_HDR_SIZE),
  834. APR_PKT_VER);
  835. pkt->hdr.pkt_size = pkt_size;
  836. pkt->hdr.src_port = 0;
  837. pkt->hdr.dest_port = 0;
  838. pkt->hdr.token = port->token;
  839. pkt->hdr.opcode = AFE_PORT_CMD_SET_PARAM_V2;
  840. param->port_id = port_id;
  841. param->payload_size = sizeof(*pdata) + psize;
  842. param->payload_address_lsw = 0x00;
  843. param->payload_address_msw = 0x00;
  844. param->mem_map_handle = 0x00;
  845. pdata->module_id = module_id;
  846. pdata->param_id = param_id;
  847. pdata->param_size = psize;
  848. ret = afe_apr_send_pkt(afe, pkt, port);
  849. if (ret)
  850. dev_err(afe->dev, "AFE enable for port 0x%x failed %d\n",
  851. port_id, ret);
  852. kfree(pkt);
  853. return ret;
  854. }
  855. static int q6afe_set_lpass_clock(struct q6afe_port *port,
  856. struct afe_clk_cfg *cfg)
  857. {
  858. return q6afe_port_set_param_v2(port, cfg,
  859. AFE_PARAM_ID_LPAIF_CLK_CONFIG,
  860. AFE_MODULE_AUDIO_DEV_INTERFACE,
  861. sizeof(*cfg));
  862. }
  863. static int q6afe_set_lpass_clock_v2(struct q6afe_port *port,
  864. struct afe_clk_set *cfg)
  865. {
  866. return q6afe_port_set_param(port, cfg, AFE_PARAM_ID_CLOCK_SET,
  867. AFE_MODULE_CLOCK_SET, sizeof(*cfg));
  868. }
  869. static int q6afe_set_digital_codec_core_clock(struct q6afe_port *port,
  870. struct afe_digital_clk_cfg *cfg)
  871. {
  872. return q6afe_port_set_param_v2(port, cfg,
  873. AFE_PARAM_ID_INT_DIGITAL_CDC_CLK_CONFIG,
  874. AFE_MODULE_AUDIO_DEV_INTERFACE,
  875. sizeof(*cfg));
  876. }
  877. int q6afe_port_set_sysclk(struct q6afe_port *port, int clk_id,
  878. int clk_src, int clk_root,
  879. unsigned int freq, int dir)
  880. {
  881. struct afe_clk_cfg ccfg = {0,};
  882. struct afe_clk_set cset = {0,};
  883. struct afe_digital_clk_cfg dcfg = {0,};
  884. int ret;
  885. switch (clk_id) {
  886. case LPAIF_DIG_CLK:
  887. dcfg.i2s_cfg_minor_version = AFE_API_VERSION_I2S_CONFIG;
  888. dcfg.clk_val = freq;
  889. dcfg.clk_root = clk_root;
  890. ret = q6afe_set_digital_codec_core_clock(port, &dcfg);
  891. break;
  892. case LPAIF_BIT_CLK:
  893. ccfg.i2s_cfg_minor_version = AFE_API_VERSION_I2S_CONFIG;
  894. ccfg.clk_val1 = freq;
  895. ccfg.clk_src = clk_src;
  896. ccfg.clk_root = clk_root;
  897. ccfg.clk_set_mode = Q6AFE_LPASS_MODE_CLK1_VALID;
  898. ret = q6afe_set_lpass_clock(port, &ccfg);
  899. break;
  900. case LPAIF_OSR_CLK:
  901. ccfg.i2s_cfg_minor_version = AFE_API_VERSION_I2S_CONFIG;
  902. ccfg.clk_val2 = freq;
  903. ccfg.clk_src = clk_src;
  904. ccfg.clk_root = clk_root;
  905. ccfg.clk_set_mode = Q6AFE_LPASS_MODE_CLK2_VALID;
  906. ret = q6afe_set_lpass_clock(port, &ccfg);
  907. break;
  908. case Q6AFE_LPASS_CLK_ID_PRI_MI2S_IBIT ... Q6AFE_LPASS_CLK_ID_QUI_MI2S_OSR:
  909. case Q6AFE_LPASS_CLK_ID_MCLK_1 ... Q6AFE_LPASS_CLK_ID_INT_MCLK_1:
  910. case Q6AFE_LPASS_CLK_ID_PRI_TDM_IBIT ... Q6AFE_LPASS_CLK_ID_QUIN_TDM_EBIT:
  911. cset.clk_set_minor_version = AFE_API_VERSION_CLOCK_SET;
  912. cset.clk_id = clk_id;
  913. cset.clk_freq_in_hz = freq;
  914. cset.clk_attri = clk_src;
  915. cset.clk_root = clk_root;
  916. cset.enable = !!freq;
  917. ret = q6afe_set_lpass_clock_v2(port, &cset);
  918. break;
  919. default:
  920. ret = -EINVAL;
  921. break;
  922. }
  923. return ret;
  924. }
  925. EXPORT_SYMBOL_GPL(q6afe_port_set_sysclk);
  926. /**
  927. * q6afe_port_stop() - Stop a afe port
  928. *
  929. * @port: Instance of port to stop
  930. *
  931. * Return: Will be an negative on packet size on success.
  932. */
  933. int q6afe_port_stop(struct q6afe_port *port)
  934. {
  935. struct afe_port_cmd_device_stop *stop;
  936. struct q6afe *afe = port->afe;
  937. struct apr_pkt *pkt;
  938. int port_id = port->id;
  939. int ret = 0;
  940. int index, pkt_size;
  941. void *p;
  942. port_id = port->id;
  943. index = port->token;
  944. if (index < 0 || index >= AFE_PORT_MAX) {
  945. dev_err(afe->dev, "AFE port index[%d] invalid!\n", index);
  946. return -EINVAL;
  947. }
  948. pkt_size = APR_HDR_SIZE + sizeof(*stop);
  949. p = kzalloc(pkt_size, GFP_KERNEL);
  950. if (!p)
  951. return -ENOMEM;
  952. pkt = p;
  953. stop = p + APR_HDR_SIZE;
  954. pkt->hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  955. APR_HDR_LEN(APR_HDR_SIZE),
  956. APR_PKT_VER);
  957. pkt->hdr.pkt_size = pkt_size;
  958. pkt->hdr.src_port = 0;
  959. pkt->hdr.dest_port = 0;
  960. pkt->hdr.token = index;
  961. pkt->hdr.opcode = AFE_PORT_CMD_DEVICE_STOP;
  962. stop->port_id = port_id;
  963. stop->reserved = 0;
  964. ret = afe_apr_send_pkt(afe, pkt, port);
  965. if (ret)
  966. dev_err(afe->dev, "AFE close failed %d\n", ret);
  967. kfree(pkt);
  968. return ret;
  969. }
  970. EXPORT_SYMBOL_GPL(q6afe_port_stop);
  971. /**
  972. * q6afe_slim_port_prepare() - Prepare slim afe port.
  973. *
  974. * @port: Instance of afe port
  975. * @cfg: SLIM configuration for the afe port
  976. *
  977. */
  978. void q6afe_slim_port_prepare(struct q6afe_port *port,
  979. struct q6afe_slim_cfg *cfg)
  980. {
  981. union afe_port_config *pcfg = &port->port_cfg;
  982. pcfg->slim_cfg.sb_cfg_minor_version = AFE_API_VERSION_SLIMBUS_CONFIG;
  983. pcfg->slim_cfg.sample_rate = cfg->sample_rate;
  984. pcfg->slim_cfg.bit_width = cfg->bit_width;
  985. pcfg->slim_cfg.num_channels = cfg->num_channels;
  986. pcfg->slim_cfg.data_format = cfg->data_format;
  987. pcfg->slim_cfg.shared_ch_mapping[0] = cfg->ch_mapping[0];
  988. pcfg->slim_cfg.shared_ch_mapping[1] = cfg->ch_mapping[1];
  989. pcfg->slim_cfg.shared_ch_mapping[2] = cfg->ch_mapping[2];
  990. pcfg->slim_cfg.shared_ch_mapping[3] = cfg->ch_mapping[3];
  991. }
  992. EXPORT_SYMBOL_GPL(q6afe_slim_port_prepare);
  993. /**
  994. * q6afe_tdm_port_prepare() - Prepare tdm afe port.
  995. *
  996. * @port: Instance of afe port
  997. * @cfg: TDM configuration for the afe port
  998. *
  999. */
  1000. void q6afe_tdm_port_prepare(struct q6afe_port *port,
  1001. struct q6afe_tdm_cfg *cfg)
  1002. {
  1003. union afe_port_config *pcfg = &port->port_cfg;
  1004. pcfg->tdm_cfg.tdm_cfg_minor_version = AFE_API_VERSION_TDM_CONFIG;
  1005. pcfg->tdm_cfg.num_channels = cfg->num_channels;
  1006. pcfg->tdm_cfg.sample_rate = cfg->sample_rate;
  1007. pcfg->tdm_cfg.bit_width = cfg->bit_width;
  1008. pcfg->tdm_cfg.data_format = cfg->data_format;
  1009. pcfg->tdm_cfg.sync_mode = cfg->sync_mode;
  1010. pcfg->tdm_cfg.sync_src = cfg->sync_src;
  1011. pcfg->tdm_cfg.nslots_per_frame = cfg->nslots_per_frame;
  1012. pcfg->tdm_cfg.slot_width = cfg->slot_width;
  1013. pcfg->tdm_cfg.slot_mask = cfg->slot_mask;
  1014. port->scfg = kzalloc(sizeof(*port->scfg), GFP_KERNEL);
  1015. if (!port->scfg)
  1016. return;
  1017. port->scfg->minor_version = AFE_API_VERSION_SLOT_MAPPING_CONFIG;
  1018. port->scfg->num_channels = cfg->num_channels;
  1019. port->scfg->bitwidth = cfg->bit_width;
  1020. port->scfg->data_align_type = cfg->data_align_type;
  1021. memcpy(port->scfg->ch_mapping, cfg->ch_mapping,
  1022. sizeof(u16) * AFE_PORT_MAX_AUDIO_CHAN_CNT);
  1023. }
  1024. EXPORT_SYMBOL_GPL(q6afe_tdm_port_prepare);
  1025. /**
  1026. * q6afe_hdmi_port_prepare() - Prepare hdmi afe port.
  1027. *
  1028. * @port: Instance of afe port
  1029. * @cfg: HDMI configuration for the afe port
  1030. *
  1031. */
  1032. void q6afe_hdmi_port_prepare(struct q6afe_port *port,
  1033. struct q6afe_hdmi_cfg *cfg)
  1034. {
  1035. union afe_port_config *pcfg = &port->port_cfg;
  1036. pcfg->hdmi_multi_ch.hdmi_cfg_minor_version =
  1037. AFE_API_VERSION_HDMI_CONFIG;
  1038. pcfg->hdmi_multi_ch.datatype = cfg->datatype;
  1039. pcfg->hdmi_multi_ch.channel_allocation = cfg->channel_allocation;
  1040. pcfg->hdmi_multi_ch.sample_rate = cfg->sample_rate;
  1041. pcfg->hdmi_multi_ch.bit_width = cfg->bit_width;
  1042. }
  1043. EXPORT_SYMBOL_GPL(q6afe_hdmi_port_prepare);
  1044. /**
  1045. * q6afe_i2s_port_prepare() - Prepare i2s afe port.
  1046. *
  1047. * @port: Instance of afe port
  1048. * @cfg: I2S configuration for the afe port
  1049. * Return: Will be an negative on error and zero on success.
  1050. */
  1051. int q6afe_i2s_port_prepare(struct q6afe_port *port, struct q6afe_i2s_cfg *cfg)
  1052. {
  1053. union afe_port_config *pcfg = &port->port_cfg;
  1054. struct device *dev = port->afe->dev;
  1055. int num_sd_lines;
  1056. pcfg->i2s_cfg.i2s_cfg_minor_version = AFE_API_VERSION_I2S_CONFIG;
  1057. pcfg->i2s_cfg.sample_rate = cfg->sample_rate;
  1058. pcfg->i2s_cfg.bit_width = cfg->bit_width;
  1059. pcfg->i2s_cfg.data_format = AFE_LINEAR_PCM_DATA;
  1060. switch (cfg->fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  1061. case SND_SOC_DAIFMT_CBS_CFS:
  1062. pcfg->i2s_cfg.ws_src = AFE_PORT_CONFIG_I2S_WS_SRC_INTERNAL;
  1063. break;
  1064. case SND_SOC_DAIFMT_CBM_CFM:
  1065. /* CPU is slave */
  1066. pcfg->i2s_cfg.ws_src = AFE_PORT_CONFIG_I2S_WS_SRC_EXTERNAL;
  1067. break;
  1068. default:
  1069. break;
  1070. }
  1071. num_sd_lines = hweight_long(cfg->sd_line_mask);
  1072. switch (num_sd_lines) {
  1073. case 0:
  1074. dev_err(dev, "no line is assigned\n");
  1075. return -EINVAL;
  1076. case 1:
  1077. switch (cfg->sd_line_mask) {
  1078. case AFE_PORT_I2S_SD0_MASK:
  1079. pcfg->i2s_cfg.channel_mode = AFE_PORT_I2S_SD0;
  1080. break;
  1081. case AFE_PORT_I2S_SD1_MASK:
  1082. pcfg->i2s_cfg.channel_mode = AFE_PORT_I2S_SD1;
  1083. break;
  1084. case AFE_PORT_I2S_SD2_MASK:
  1085. pcfg->i2s_cfg.channel_mode = AFE_PORT_I2S_SD2;
  1086. break;
  1087. case AFE_PORT_I2S_SD3_MASK:
  1088. pcfg->i2s_cfg.channel_mode = AFE_PORT_I2S_SD3;
  1089. break;
  1090. default:
  1091. dev_err(dev, "Invalid SD lines\n");
  1092. return -EINVAL;
  1093. }
  1094. break;
  1095. case 2:
  1096. switch (cfg->sd_line_mask) {
  1097. case AFE_PORT_I2S_SD0_1_MASK:
  1098. pcfg->i2s_cfg.channel_mode = AFE_PORT_I2S_QUAD01;
  1099. break;
  1100. case AFE_PORT_I2S_SD2_3_MASK:
  1101. pcfg->i2s_cfg.channel_mode = AFE_PORT_I2S_QUAD23;
  1102. break;
  1103. default:
  1104. dev_err(dev, "Invalid SD lines\n");
  1105. return -EINVAL;
  1106. }
  1107. break;
  1108. case 3:
  1109. switch (cfg->sd_line_mask) {
  1110. case AFE_PORT_I2S_SD0_1_2_MASK:
  1111. pcfg->i2s_cfg.channel_mode = AFE_PORT_I2S_6CHS;
  1112. break;
  1113. default:
  1114. dev_err(dev, "Invalid SD lines\n");
  1115. return -EINVAL;
  1116. }
  1117. break;
  1118. case 4:
  1119. switch (cfg->sd_line_mask) {
  1120. case AFE_PORT_I2S_SD0_1_2_3_MASK:
  1121. pcfg->i2s_cfg.channel_mode = AFE_PORT_I2S_8CHS;
  1122. break;
  1123. default:
  1124. dev_err(dev, "Invalid SD lines\n");
  1125. return -EINVAL;
  1126. }
  1127. break;
  1128. default:
  1129. dev_err(dev, "Invalid SD lines\n");
  1130. return -EINVAL;
  1131. }
  1132. switch (cfg->num_channels) {
  1133. case 1:
  1134. case 2:
  1135. switch (pcfg->i2s_cfg.channel_mode) {
  1136. case AFE_PORT_I2S_QUAD01:
  1137. case AFE_PORT_I2S_6CHS:
  1138. case AFE_PORT_I2S_8CHS:
  1139. pcfg->i2s_cfg.channel_mode = AFE_PORT_I2S_SD0;
  1140. break;
  1141. case AFE_PORT_I2S_QUAD23:
  1142. pcfg->i2s_cfg.channel_mode = AFE_PORT_I2S_SD2;
  1143. break;
  1144. }
  1145. if (cfg->num_channels == 2)
  1146. pcfg->i2s_cfg.mono_stereo = AFE_PORT_I2S_STEREO;
  1147. else
  1148. pcfg->i2s_cfg.mono_stereo = AFE_PORT_I2S_MONO;
  1149. break;
  1150. case 3:
  1151. case 4:
  1152. if (pcfg->i2s_cfg.channel_mode < AFE_PORT_I2S_QUAD01) {
  1153. dev_err(dev, "Invalid Channel mode\n");
  1154. return -EINVAL;
  1155. }
  1156. break;
  1157. case 5:
  1158. case 6:
  1159. if (pcfg->i2s_cfg.channel_mode < AFE_PORT_I2S_6CHS) {
  1160. dev_err(dev, "Invalid Channel mode\n");
  1161. return -EINVAL;
  1162. }
  1163. break;
  1164. case 7:
  1165. case 8:
  1166. if (pcfg->i2s_cfg.channel_mode < AFE_PORT_I2S_8CHS) {
  1167. dev_err(dev, "Invalid Channel mode\n");
  1168. return -EINVAL;
  1169. }
  1170. break;
  1171. default:
  1172. break;
  1173. }
  1174. return 0;
  1175. }
  1176. EXPORT_SYMBOL_GPL(q6afe_i2s_port_prepare);
  1177. /**
  1178. * q6afe_port_start() - Start a afe port
  1179. *
  1180. * @port: Instance of port to start
  1181. *
  1182. * Return: Will be an negative on packet size on success.
  1183. */
  1184. int q6afe_port_start(struct q6afe_port *port)
  1185. {
  1186. struct afe_port_cmd_device_start *start;
  1187. struct q6afe *afe = port->afe;
  1188. int port_id = port->id;
  1189. int ret, param_id = port->cfg_type;
  1190. struct apr_pkt *pkt;
  1191. int pkt_size;
  1192. void *p;
  1193. ret = q6afe_port_set_param_v2(port, &port->port_cfg, param_id,
  1194. AFE_MODULE_AUDIO_DEV_INTERFACE,
  1195. sizeof(port->port_cfg));
  1196. if (ret) {
  1197. dev_err(afe->dev, "AFE enable for port 0x%x failed %d\n",
  1198. port_id, ret);
  1199. return ret;
  1200. }
  1201. if (port->scfg) {
  1202. ret = q6afe_port_set_param_v2(port, port->scfg,
  1203. AFE_PARAM_ID_PORT_SLOT_MAPPING_CONFIG,
  1204. AFE_MODULE_TDM, sizeof(*port->scfg));
  1205. if (ret) {
  1206. dev_err(afe->dev, "AFE enable for port 0x%x failed %d\n",
  1207. port_id, ret);
  1208. return ret;
  1209. }
  1210. }
  1211. pkt_size = APR_HDR_SIZE + sizeof(*start);
  1212. p = kzalloc(pkt_size, GFP_KERNEL);
  1213. if (!p)
  1214. return -ENOMEM;
  1215. pkt = p;
  1216. start = p + APR_HDR_SIZE;
  1217. pkt->hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  1218. APR_HDR_LEN(APR_HDR_SIZE),
  1219. APR_PKT_VER);
  1220. pkt->hdr.pkt_size = pkt_size;
  1221. pkt->hdr.src_port = 0;
  1222. pkt->hdr.dest_port = 0;
  1223. pkt->hdr.token = port->token;
  1224. pkt->hdr.opcode = AFE_PORT_CMD_DEVICE_START;
  1225. start->port_id = port_id;
  1226. ret = afe_apr_send_pkt(afe, pkt, port);
  1227. if (ret)
  1228. dev_err(afe->dev, "AFE enable for port 0x%x failed %d\n",
  1229. port_id, ret);
  1230. kfree(pkt);
  1231. return ret;
  1232. }
  1233. EXPORT_SYMBOL_GPL(q6afe_port_start);
  1234. /**
  1235. * q6afe_port_get_from_id() - Get port instance from a port id
  1236. *
  1237. * @dev: Pointer to afe child device.
  1238. * @id: port id
  1239. *
  1240. * Return: Will be an error pointer on error or a valid afe port
  1241. * on success.
  1242. */
  1243. struct q6afe_port *q6afe_port_get_from_id(struct device *dev, int id)
  1244. {
  1245. int port_id;
  1246. struct q6afe *afe = dev_get_drvdata(dev->parent);
  1247. struct q6afe_port *port;
  1248. unsigned long flags;
  1249. int cfg_type;
  1250. if (id < 0 || id >= AFE_PORT_MAX) {
  1251. dev_err(dev, "AFE port token[%d] invalid!\n", id);
  1252. return ERR_PTR(-EINVAL);
  1253. }
  1254. /* if port is multiple times bind/unbind before callback finishes */
  1255. port = q6afe_find_port(afe, id);
  1256. if (port) {
  1257. dev_err(dev, "AFE Port already open\n");
  1258. return port;
  1259. }
  1260. port_id = port_maps[id].port_id;
  1261. switch (port_id) {
  1262. case AFE_PORT_ID_MULTICHAN_HDMI_RX:
  1263. cfg_type = AFE_PARAM_ID_HDMI_CONFIG;
  1264. break;
  1265. case AFE_PORT_ID_SLIMBUS_MULTI_CHAN_0_TX:
  1266. case AFE_PORT_ID_SLIMBUS_MULTI_CHAN_1_TX:
  1267. case AFE_PORT_ID_SLIMBUS_MULTI_CHAN_2_TX:
  1268. case AFE_PORT_ID_SLIMBUS_MULTI_CHAN_3_TX:
  1269. case AFE_PORT_ID_SLIMBUS_MULTI_CHAN_4_TX:
  1270. case AFE_PORT_ID_SLIMBUS_MULTI_CHAN_5_TX:
  1271. case AFE_PORT_ID_SLIMBUS_MULTI_CHAN_6_TX:
  1272. case AFE_PORT_ID_SLIMBUS_MULTI_CHAN_0_RX:
  1273. case AFE_PORT_ID_SLIMBUS_MULTI_CHAN_1_RX:
  1274. case AFE_PORT_ID_SLIMBUS_MULTI_CHAN_2_RX:
  1275. case AFE_PORT_ID_SLIMBUS_MULTI_CHAN_3_RX:
  1276. case AFE_PORT_ID_SLIMBUS_MULTI_CHAN_4_RX:
  1277. case AFE_PORT_ID_SLIMBUS_MULTI_CHAN_5_RX:
  1278. case AFE_PORT_ID_SLIMBUS_MULTI_CHAN_6_RX:
  1279. cfg_type = AFE_PARAM_ID_SLIMBUS_CONFIG;
  1280. break;
  1281. case AFE_PORT_ID_PRIMARY_MI2S_RX:
  1282. case AFE_PORT_ID_PRIMARY_MI2S_TX:
  1283. case AFE_PORT_ID_SECONDARY_MI2S_RX:
  1284. case AFE_PORT_ID_SECONDARY_MI2S_TX:
  1285. case AFE_PORT_ID_TERTIARY_MI2S_RX:
  1286. case AFE_PORT_ID_TERTIARY_MI2S_TX:
  1287. case AFE_PORT_ID_QUATERNARY_MI2S_RX:
  1288. case AFE_PORT_ID_QUATERNARY_MI2S_TX:
  1289. cfg_type = AFE_PARAM_ID_I2S_CONFIG;
  1290. break;
  1291. case AFE_PORT_ID_PRIMARY_TDM_RX ... AFE_PORT_ID_QUINARY_TDM_TX_7:
  1292. cfg_type = AFE_PARAM_ID_TDM_CONFIG;
  1293. break;
  1294. default:
  1295. dev_err(dev, "Invalid port id 0x%x\n", port_id);
  1296. return ERR_PTR(-EINVAL);
  1297. }
  1298. port = kzalloc(sizeof(*port), GFP_KERNEL);
  1299. if (!port)
  1300. return ERR_PTR(-ENOMEM);
  1301. init_waitqueue_head(&port->wait);
  1302. port->token = id;
  1303. port->id = port_id;
  1304. port->afe = afe;
  1305. port->cfg_type = cfg_type;
  1306. kref_init(&port->refcount);
  1307. spin_lock_irqsave(&afe->port_list_lock, flags);
  1308. list_add_tail(&port->node, &afe->port_list);
  1309. spin_unlock_irqrestore(&afe->port_list_lock, flags);
  1310. return port;
  1311. }
  1312. EXPORT_SYMBOL_GPL(q6afe_port_get_from_id);
  1313. /**
  1314. * q6afe_port_put() - Release port reference
  1315. *
  1316. * @port: Instance of port to put
  1317. */
  1318. void q6afe_port_put(struct q6afe_port *port)
  1319. {
  1320. kref_put(&port->refcount, q6afe_port_free);
  1321. }
  1322. EXPORT_SYMBOL_GPL(q6afe_port_put);
  1323. static int q6afe_probe(struct apr_device *adev)
  1324. {
  1325. struct q6afe *afe;
  1326. struct device *dev = &adev->dev;
  1327. afe = devm_kzalloc(dev, sizeof(*afe), GFP_KERNEL);
  1328. if (!afe)
  1329. return -ENOMEM;
  1330. q6core_get_svc_api_info(adev->svc_id, &afe->ainfo);
  1331. afe->apr = adev;
  1332. mutex_init(&afe->lock);
  1333. afe->dev = dev;
  1334. INIT_LIST_HEAD(&afe->port_list);
  1335. spin_lock_init(&afe->port_list_lock);
  1336. dev_set_drvdata(dev, afe);
  1337. return of_platform_populate(dev->of_node, NULL, NULL, dev);
  1338. }
  1339. static int q6afe_remove(struct apr_device *adev)
  1340. {
  1341. of_platform_depopulate(&adev->dev);
  1342. return 0;
  1343. }
  1344. static const struct of_device_id q6afe_device_id[] = {
  1345. { .compatible = "qcom,q6afe" },
  1346. {},
  1347. };
  1348. MODULE_DEVICE_TABLE(of, q6afe_device_id);
  1349. static struct apr_driver qcom_q6afe_driver = {
  1350. .probe = q6afe_probe,
  1351. .remove = q6afe_remove,
  1352. .callback = q6afe_callback,
  1353. .driver = {
  1354. .name = "qcom-q6afe",
  1355. .of_match_table = of_match_ptr(q6afe_device_id),
  1356. },
  1357. };
  1358. module_apr_driver(qcom_q6afe_driver);
  1359. MODULE_DESCRIPTION("Q6 Audio Front End");
  1360. MODULE_LICENSE("GPL v2");