wsa-macro.c 82 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803
  1. /* Copyright (c) 2018, The Linux Foundation. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. */
  12. #include <linux/module.h>
  13. #include <linux/init.h>
  14. #include <linux/io.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/clk.h>
  17. #include <sound/soc.h>
  18. #include <sound/soc-dapm.h>
  19. #include <sound/tlv.h>
  20. #include <soc/swr-wcd.h>
  21. #include "bolero-cdc.h"
  22. #include "bolero-cdc-registers.h"
  23. #include "wsa-macro.h"
  24. #include "../msm-cdc-pinctrl.h"
  25. #define WSA_MACRO_MAX_OFFSET 0x1000
  26. #define WSA_MACRO_RX_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000 |\
  27. SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_48000 |\
  28. SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_192000)
  29. #define WSA_MACRO_RX_MIX_RATES (SNDRV_PCM_RATE_48000 |\
  30. SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_192000)
  31. #define WSA_MACRO_RX_FORMATS (SNDRV_PCM_FMTBIT_S16_LE |\
  32. SNDRV_PCM_FMTBIT_S24_LE |\
  33. SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S32_LE)
  34. #define WSA_MACRO_ECHO_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000 |\
  35. SNDRV_PCM_RATE_48000)
  36. #define WSA_MACRO_ECHO_FORMATS (SNDRV_PCM_FMTBIT_S16_LE |\
  37. SNDRV_PCM_FMTBIT_S24_LE |\
  38. SNDRV_PCM_FMTBIT_S24_3LE)
  39. #define NUM_INTERPOLATORS 2
  40. #define WSA_MACRO_MUX_INP_SHFT 0x3
  41. #define WSA_MACRO_MUX_INP_MASK1 0x38
  42. #define WSA_MACRO_MUX_INP_MASK2 0x38
  43. #define WSA_MACRO_MUX_CFG_OFFSET 0x8
  44. #define WSA_MACRO_MUX_CFG1_OFFSET 0x4
  45. #define WSA_MACRO_RX_COMP_OFFSET 0x40
  46. #define WSA_MACRO_RX_SOFTCLIP_OFFSET 0x40
  47. #define WSA_MACRO_RX_PATH_OFFSET 0x80
  48. #define WSA_MACRO_RX_PATH_CFG3_OFFSET 0x10
  49. #define WSA_MACRO_RX_PATH_DSMDEM_OFFSET 0x4C
  50. #define WSA_MACRO_FS_RATE_MASK 0x0F
  51. enum {
  52. WSA_MACRO_RX0 = 0,
  53. WSA_MACRO_RX1,
  54. WSA_MACRO_RX_MIX,
  55. WSA_MACRO_RX_MIX0 = WSA_MACRO_RX_MIX,
  56. WSA_MACRO_RX_MIX1,
  57. WSA_MACRO_RX_MAX,
  58. };
  59. enum {
  60. WSA_MACRO_TX0 = 0,
  61. WSA_MACRO_TX1,
  62. WSA_MACRO_TX_MAX,
  63. };
  64. enum {
  65. WSA_MACRO_EC0_MUX = 0,
  66. WSA_MACRO_EC1_MUX,
  67. WSA_MACRO_EC_MUX_MAX,
  68. };
  69. enum {
  70. WSA_MACRO_COMP1, /* SPK_L */
  71. WSA_MACRO_COMP2, /* SPK_R */
  72. WSA_MACRO_COMP_MAX
  73. };
  74. enum {
  75. WSA_MACRO_SOFTCLIP0, /* RX0 */
  76. WSA_MACRO_SOFTCLIP1, /* RX1 */
  77. WSA_MACRO_SOFTCLIP_MAX
  78. };
  79. struct interp_sample_rate {
  80. int sample_rate;
  81. int rate_val;
  82. };
  83. /*
  84. * Structure used to update codec
  85. * register defaults after reset
  86. */
  87. struct wsa_macro_reg_mask_val {
  88. u16 reg;
  89. u8 mask;
  90. u8 val;
  91. };
  92. static struct interp_sample_rate int_prim_sample_rate_val[] = {
  93. {8000, 0x0}, /* 8K */
  94. {16000, 0x1}, /* 16K */
  95. {24000, -EINVAL},/* 24K */
  96. {32000, 0x3}, /* 32K */
  97. {48000, 0x4}, /* 48K */
  98. {96000, 0x5}, /* 96K */
  99. {192000, 0x6}, /* 192K */
  100. {384000, 0x7}, /* 384K */
  101. {44100, 0x8}, /* 44.1K */
  102. };
  103. static struct interp_sample_rate int_mix_sample_rate_val[] = {
  104. {48000, 0x4}, /* 48K */
  105. {96000, 0x5}, /* 96K */
  106. {192000, 0x6}, /* 192K */
  107. };
  108. #define WSA_MACRO_SWR_STRING_LEN 80
  109. static int wsa_macro_hw_params(struct snd_pcm_substream *substream,
  110. struct snd_pcm_hw_params *params,
  111. struct snd_soc_dai *dai);
  112. static int wsa_macro_get_channel_map(struct snd_soc_dai *dai,
  113. unsigned int *tx_num, unsigned int *tx_slot,
  114. unsigned int *rx_num, unsigned int *rx_slot);
  115. /* Hold instance to soundwire platform device */
  116. struct wsa_macro_swr_ctrl_data {
  117. struct platform_device *wsa_swr_pdev;
  118. };
  119. struct wsa_macro_swr_ctrl_platform_data {
  120. void *handle; /* holds codec private data */
  121. int (*read)(void *handle, int reg);
  122. int (*write)(void *handle, int reg, int val);
  123. int (*bulk_write)(void *handle, u32 *reg, u32 *val, size_t len);
  124. int (*clk)(void *handle, bool enable);
  125. int (*handle_irq)(void *handle,
  126. irqreturn_t (*swrm_irq_handler)(int irq,
  127. void *data),
  128. void *swrm_handle,
  129. int action);
  130. };
  131. struct wsa_macro_bcl_pmic_params {
  132. u8 id;
  133. u8 sid;
  134. u8 ppid;
  135. };
  136. enum {
  137. WSA_MACRO_AIF_INVALID = 0,
  138. WSA_MACRO_AIF1_PB,
  139. WSA_MACRO_AIF_MIX1_PB,
  140. WSA_MACRO_AIF_VI,
  141. WSA_MACRO_AIF_ECHO,
  142. WSA_MACRO_MAX_DAIS,
  143. };
  144. #define WSA_MACRO_CHILD_DEVICES_MAX 3
  145. /*
  146. * @dev: wsa macro device pointer
  147. * @comp_enabled: compander enable mixer value set
  148. * @ec_hq: echo HQ enable mixer value set
  149. * @prim_int_users: Users of interpolator
  150. * @wsa_mclk_users: WSA MCLK users count
  151. * @swr_clk_users: SWR clk users count
  152. * @vi_feed_value: VI sense mask
  153. * @mclk_lock: to lock mclk operations
  154. * @swr_clk_lock: to lock swr master clock operations
  155. * @swr_ctrl_data: SoundWire data structure
  156. * @swr_plat_data: Soundwire platform data
  157. * @wsa_macro_add_child_devices_work: work for adding child devices
  158. * @wsa_swr_gpio_p: used by pinctrl API
  159. * @wsa_core_clk: MCLK for wsa macro
  160. * @wsa_npl_clk: NPL clock for WSA soundwire
  161. * @codec: codec handle
  162. * @rx_0_count: RX0 interpolation users
  163. * @rx_1_count: RX1 interpolation users
  164. * @active_ch_mask: channel mask for all AIF DAIs
  165. * @active_ch_cnt: channel count of all AIF DAIs
  166. * @rx_port_value: mixer ctl value of WSA RX MUXes
  167. * @wsa_io_base: Base address of WSA macro addr space
  168. */
  169. struct wsa_macro_priv {
  170. struct device *dev;
  171. int comp_enabled[WSA_MACRO_COMP_MAX];
  172. int ec_hq[WSA_MACRO_RX1 + 1];
  173. u16 prim_int_users[WSA_MACRO_RX1 + 1];
  174. u16 wsa_mclk_users;
  175. u16 swr_clk_users;
  176. unsigned int vi_feed_value;
  177. struct mutex mclk_lock;
  178. struct mutex swr_clk_lock;
  179. struct wsa_macro_swr_ctrl_data *swr_ctrl_data;
  180. struct wsa_macro_swr_ctrl_platform_data swr_plat_data;
  181. struct work_struct wsa_macro_add_child_devices_work;
  182. struct device_node *wsa_swr_gpio_p;
  183. struct clk *wsa_core_clk;
  184. struct clk *wsa_npl_clk;
  185. struct snd_soc_codec *codec;
  186. int rx_0_count;
  187. int rx_1_count;
  188. unsigned long active_ch_mask[WSA_MACRO_MAX_DAIS];
  189. unsigned long active_ch_cnt[WSA_MACRO_MAX_DAIS];
  190. int rx_port_value[WSA_MACRO_RX_MAX];
  191. char __iomem *wsa_io_base;
  192. struct platform_device *pdev_child_devices
  193. [WSA_MACRO_CHILD_DEVICES_MAX];
  194. int child_count;
  195. int ear_spkr_gain;
  196. int spkr_gain_offset;
  197. int spkr_mode;
  198. int is_softclip_on[WSA_MACRO_SOFTCLIP_MAX];
  199. int softclip_clk_users[WSA_MACRO_SOFTCLIP_MAX];
  200. struct wsa_macro_bcl_pmic_params bcl_pmic_params;
  201. };
  202. static int wsa_macro_config_ear_spkr_gain(struct snd_soc_codec *codec,
  203. struct wsa_macro_priv *wsa_priv,
  204. int event, int gain_reg);
  205. static struct snd_soc_dai_driver wsa_macro_dai[];
  206. static const DECLARE_TLV_DB_SCALE(digital_gain, 0, 1, 0);
  207. static const char *const rx_text[] = {
  208. "ZERO", "RX0", "RX1", "RX_MIX0", "RX_MIX1", "DEC0", "DEC1"
  209. };
  210. static const char *const rx_mix_text[] = {
  211. "ZERO", "RX0", "RX1", "RX_MIX0", "RX_MIX1"
  212. };
  213. static const char *const rx_mix_ec_text[] = {
  214. "ZERO", "RX_MIX_TX0", "RX_MIX_TX1"
  215. };
  216. static const char *const rx_mux_text[] = {
  217. "ZERO", "AIF1_PB", "AIF_MIX1_PB"
  218. };
  219. static const char *const rx_sidetone_mix_text[] = {
  220. "ZERO", "SRC0"
  221. };
  222. static const char * const wsa_macro_ear_spkr_pa_gain_text[] = {
  223. "G_DEFAULT", "G_0_DB", "G_1_DB", "G_2_DB", "G_3_DB",
  224. "G_4_DB", "G_5_DB", "G_6_DB"
  225. };
  226. static const char * const wsa_macro_speaker_boost_stage_text[] = {
  227. "NO_MAX_STATE", "MAX_STATE_1", "MAX_STATE_2"
  228. };
  229. static const char * const wsa_macro_vbat_bcl_gsm_mode_text[] = {
  230. "OFF", "ON"
  231. };
  232. static const struct snd_kcontrol_new wsa_int0_vbat_mix_switch[] = {
  233. SOC_DAPM_SINGLE("WSA RX0 VBAT Enable", SND_SOC_NOPM, 0, 1, 0)
  234. };
  235. static const struct snd_kcontrol_new wsa_int1_vbat_mix_switch[] = {
  236. SOC_DAPM_SINGLE("WSA RX1 VBAT Enable", SND_SOC_NOPM, 0, 1, 0)
  237. };
  238. static SOC_ENUM_SINGLE_EXT_DECL(wsa_macro_ear_spkr_pa_gain_enum,
  239. wsa_macro_ear_spkr_pa_gain_text);
  240. static SOC_ENUM_SINGLE_EXT_DECL(wsa_macro_spkr_boost_stage_enum,
  241. wsa_macro_speaker_boost_stage_text);
  242. static SOC_ENUM_SINGLE_EXT_DECL(wsa_macro_vbat_bcl_gsm_mode_enum,
  243. wsa_macro_vbat_bcl_gsm_mode_text);
  244. /* RX INT0 */
  245. static const struct soc_enum rx0_prim_inp0_chain_enum =
  246. SOC_ENUM_SINGLE(BOLERO_CDC_WSA_RX_INP_MUX_RX_INT0_CFG0,
  247. 0, 7, rx_text);
  248. static const struct soc_enum rx0_prim_inp1_chain_enum =
  249. SOC_ENUM_SINGLE(BOLERO_CDC_WSA_RX_INP_MUX_RX_INT0_CFG0,
  250. 3, 7, rx_text);
  251. static const struct soc_enum rx0_prim_inp2_chain_enum =
  252. SOC_ENUM_SINGLE(BOLERO_CDC_WSA_RX_INP_MUX_RX_INT0_CFG1,
  253. 3, 7, rx_text);
  254. static const struct soc_enum rx0_mix_chain_enum =
  255. SOC_ENUM_SINGLE(BOLERO_CDC_WSA_RX_INP_MUX_RX_INT0_CFG1,
  256. 0, 5, rx_mix_text);
  257. static const struct soc_enum rx0_sidetone_mix_enum =
  258. SOC_ENUM_SINGLE(SND_SOC_NOPM, 0, 2, rx_sidetone_mix_text);
  259. static const struct snd_kcontrol_new rx0_prim_inp0_mux =
  260. SOC_DAPM_ENUM("WSA_RX0 INP0 Mux", rx0_prim_inp0_chain_enum);
  261. static const struct snd_kcontrol_new rx0_prim_inp1_mux =
  262. SOC_DAPM_ENUM("WSA_RX0 INP1 Mux", rx0_prim_inp1_chain_enum);
  263. static const struct snd_kcontrol_new rx0_prim_inp2_mux =
  264. SOC_DAPM_ENUM("WSA_RX0 INP2 Mux", rx0_prim_inp2_chain_enum);
  265. static const struct snd_kcontrol_new rx0_mix_mux =
  266. SOC_DAPM_ENUM("WSA_RX0 MIX Mux", rx0_mix_chain_enum);
  267. static const struct snd_kcontrol_new rx0_sidetone_mix_mux =
  268. SOC_DAPM_ENUM("WSA_RX0 SIDETONE MIX Mux", rx0_sidetone_mix_enum);
  269. /* RX INT1 */
  270. static const struct soc_enum rx1_prim_inp0_chain_enum =
  271. SOC_ENUM_SINGLE(BOLERO_CDC_WSA_RX_INP_MUX_RX_INT1_CFG0,
  272. 0, 7, rx_text);
  273. static const struct soc_enum rx1_prim_inp1_chain_enum =
  274. SOC_ENUM_SINGLE(BOLERO_CDC_WSA_RX_INP_MUX_RX_INT1_CFG0,
  275. 3, 7, rx_text);
  276. static const struct soc_enum rx1_prim_inp2_chain_enum =
  277. SOC_ENUM_SINGLE(BOLERO_CDC_WSA_RX_INP_MUX_RX_INT1_CFG1,
  278. 3, 7, rx_text);
  279. static const struct soc_enum rx1_mix_chain_enum =
  280. SOC_ENUM_SINGLE(BOLERO_CDC_WSA_RX_INP_MUX_RX_INT1_CFG1,
  281. 0, 5, rx_mix_text);
  282. static const struct snd_kcontrol_new rx1_prim_inp0_mux =
  283. SOC_DAPM_ENUM("WSA_RX1 INP0 Mux", rx1_prim_inp0_chain_enum);
  284. static const struct snd_kcontrol_new rx1_prim_inp1_mux =
  285. SOC_DAPM_ENUM("WSA_RX1 INP1 Mux", rx1_prim_inp1_chain_enum);
  286. static const struct snd_kcontrol_new rx1_prim_inp2_mux =
  287. SOC_DAPM_ENUM("WSA_RX1 INP2 Mux", rx1_prim_inp2_chain_enum);
  288. static const struct snd_kcontrol_new rx1_mix_mux =
  289. SOC_DAPM_ENUM("WSA_RX1 MIX Mux", rx1_mix_chain_enum);
  290. static const struct soc_enum rx_mix_ec0_enum =
  291. SOC_ENUM_SINGLE(BOLERO_CDC_WSA_RX_INP_MUX_RX_MIX_CFG0,
  292. 0, 3, rx_mix_ec_text);
  293. static const struct soc_enum rx_mix_ec1_enum =
  294. SOC_ENUM_SINGLE(BOLERO_CDC_WSA_RX_INP_MUX_RX_MIX_CFG0,
  295. 3, 3, rx_mix_ec_text);
  296. static const struct snd_kcontrol_new rx_mix_ec0_mux =
  297. SOC_DAPM_ENUM("WSA RX_MIX EC0_Mux", rx_mix_ec0_enum);
  298. static const struct snd_kcontrol_new rx_mix_ec1_mux =
  299. SOC_DAPM_ENUM("WSA RX_MIX EC1_Mux", rx_mix_ec1_enum);
  300. static struct snd_soc_dai_ops wsa_macro_dai_ops = {
  301. .hw_params = wsa_macro_hw_params,
  302. .get_channel_map = wsa_macro_get_channel_map,
  303. };
  304. static struct snd_soc_dai_driver wsa_macro_dai[] = {
  305. {
  306. .name = "wsa_macro_rx1",
  307. .id = WSA_MACRO_AIF1_PB,
  308. .playback = {
  309. .stream_name = "WSA_AIF1 Playback",
  310. .rates = WSA_MACRO_RX_RATES,
  311. .formats = WSA_MACRO_RX_FORMATS,
  312. .rate_max = 384000,
  313. .rate_min = 8000,
  314. .channels_min = 1,
  315. .channels_max = 2,
  316. },
  317. .ops = &wsa_macro_dai_ops,
  318. },
  319. {
  320. .name = "wsa_macro_rx_mix",
  321. .id = WSA_MACRO_AIF_MIX1_PB,
  322. .playback = {
  323. .stream_name = "WSA_AIF_MIX1 Playback",
  324. .rates = WSA_MACRO_RX_MIX_RATES,
  325. .formats = WSA_MACRO_RX_FORMATS,
  326. .rate_max = 192000,
  327. .rate_min = 48000,
  328. .channels_min = 1,
  329. .channels_max = 2,
  330. },
  331. .ops = &wsa_macro_dai_ops,
  332. },
  333. {
  334. .name = "wsa_macro_vifeedback",
  335. .id = WSA_MACRO_AIF_VI,
  336. .capture = {
  337. .stream_name = "WSA_AIF_VI Capture",
  338. .rates = SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_48000,
  339. .formats = WSA_MACRO_RX_FORMATS,
  340. .rate_max = 48000,
  341. .rate_min = 8000,
  342. .channels_min = 1,
  343. .channels_max = 4,
  344. },
  345. .ops = &wsa_macro_dai_ops,
  346. },
  347. {
  348. .name = "wsa_macro_echo",
  349. .id = WSA_MACRO_AIF_ECHO,
  350. .capture = {
  351. .stream_name = "WSA_AIF_ECHO Capture",
  352. .rates = WSA_MACRO_ECHO_RATES,
  353. .formats = WSA_MACRO_ECHO_FORMATS,
  354. .rate_max = 48000,
  355. .rate_min = 8000,
  356. .channels_min = 1,
  357. .channels_max = 2,
  358. },
  359. .ops = &wsa_macro_dai_ops,
  360. },
  361. };
  362. static const struct wsa_macro_reg_mask_val wsa_macro_spkr_default[] = {
  363. {BOLERO_CDC_WSA_COMPANDER0_CTL3, 0x80, 0x80},
  364. {BOLERO_CDC_WSA_COMPANDER1_CTL3, 0x80, 0x80},
  365. {BOLERO_CDC_WSA_COMPANDER0_CTL7, 0x01, 0x01},
  366. {BOLERO_CDC_WSA_COMPANDER1_CTL7, 0x01, 0x01},
  367. {BOLERO_CDC_WSA_BOOST0_BOOST_CTL, 0x7C, 0x58},
  368. {BOLERO_CDC_WSA_BOOST1_BOOST_CTL, 0x7C, 0x58},
  369. };
  370. static const struct wsa_macro_reg_mask_val wsa_macro_spkr_mode1[] = {
  371. {BOLERO_CDC_WSA_COMPANDER0_CTL3, 0x80, 0x00},
  372. {BOLERO_CDC_WSA_COMPANDER1_CTL3, 0x80, 0x00},
  373. {BOLERO_CDC_WSA_COMPANDER0_CTL7, 0x01, 0x00},
  374. {BOLERO_CDC_WSA_COMPANDER1_CTL7, 0x01, 0x00},
  375. {BOLERO_CDC_WSA_BOOST0_BOOST_CTL, 0x7C, 0x44},
  376. {BOLERO_CDC_WSA_BOOST1_BOOST_CTL, 0x7C, 0x44},
  377. };
  378. static bool wsa_macro_get_data(struct snd_soc_codec *codec,
  379. struct device **wsa_dev,
  380. struct wsa_macro_priv **wsa_priv,
  381. const char *func_name)
  382. {
  383. *wsa_dev = bolero_get_device_ptr(codec->dev, WSA_MACRO);
  384. if (!(*wsa_dev)) {
  385. dev_err(codec->dev,
  386. "%s: null device for macro!\n", func_name);
  387. return false;
  388. }
  389. *wsa_priv = dev_get_drvdata((*wsa_dev));
  390. if (!(*wsa_priv) || !(*wsa_priv)->codec) {
  391. dev_err(codec->dev,
  392. "%s: priv is null for macro!\n", func_name);
  393. return false;
  394. }
  395. return true;
  396. }
  397. /**
  398. * wsa_macro_set_spkr_gain_offset - offset the speaker path
  399. * gain with the given offset value.
  400. *
  401. * @codec: codec instance
  402. * @offset: Indicates speaker path gain offset value.
  403. *
  404. * Returns 0 on success or -EINVAL on error.
  405. */
  406. int wsa_macro_set_spkr_gain_offset(struct snd_soc_codec *codec, int offset)
  407. {
  408. struct device *wsa_dev = NULL;
  409. struct wsa_macro_priv *wsa_priv = NULL;
  410. if (!codec) {
  411. pr_err("%s: NULL codec pointer!\n", __func__);
  412. return -EINVAL;
  413. }
  414. if (!wsa_macro_get_data(codec, &wsa_dev, &wsa_priv, __func__))
  415. return -EINVAL;
  416. wsa_priv->spkr_gain_offset = offset;
  417. return 0;
  418. }
  419. EXPORT_SYMBOL(wsa_macro_set_spkr_gain_offset);
  420. /**
  421. * wsa_macro_set_spkr_mode - Configures speaker compander and smartboost
  422. * settings based on speaker mode.
  423. *
  424. * @codec: codec instance
  425. * @mode: Indicates speaker configuration mode.
  426. *
  427. * Returns 0 on success or -EINVAL on error.
  428. */
  429. int wsa_macro_set_spkr_mode(struct snd_soc_codec *codec, int mode)
  430. {
  431. int i;
  432. const struct wsa_macro_reg_mask_val *regs;
  433. int size;
  434. struct device *wsa_dev = NULL;
  435. struct wsa_macro_priv *wsa_priv = NULL;
  436. if (!codec) {
  437. pr_err("%s: NULL codec pointer!\n", __func__);
  438. return -EINVAL;
  439. }
  440. if (!wsa_macro_get_data(codec, &wsa_dev, &wsa_priv, __func__))
  441. return -EINVAL;
  442. switch (mode) {
  443. case WSA_MACRO_SPKR_MODE_1:
  444. regs = wsa_macro_spkr_mode1;
  445. size = ARRAY_SIZE(wsa_macro_spkr_mode1);
  446. break;
  447. default:
  448. regs = wsa_macro_spkr_default;
  449. size = ARRAY_SIZE(wsa_macro_spkr_default);
  450. break;
  451. }
  452. wsa_priv->spkr_mode = mode;
  453. for (i = 0; i < size; i++)
  454. snd_soc_update_bits(codec, regs[i].reg,
  455. regs[i].mask, regs[i].val);
  456. return 0;
  457. }
  458. EXPORT_SYMBOL(wsa_macro_set_spkr_mode);
  459. static int wsa_macro_set_prim_interpolator_rate(struct snd_soc_dai *dai,
  460. u8 int_prim_fs_rate_reg_val,
  461. u32 sample_rate)
  462. {
  463. u8 int_1_mix1_inp;
  464. u32 j, port;
  465. u16 int_mux_cfg0, int_mux_cfg1;
  466. u16 int_fs_reg;
  467. u8 int_mux_cfg0_val, int_mux_cfg1_val;
  468. u8 inp0_sel, inp1_sel, inp2_sel;
  469. struct snd_soc_codec *codec = dai->codec;
  470. struct device *wsa_dev = NULL;
  471. struct wsa_macro_priv *wsa_priv = NULL;
  472. if (!wsa_macro_get_data(codec, &wsa_dev, &wsa_priv, __func__))
  473. return -EINVAL;
  474. for_each_set_bit(port, &wsa_priv->active_ch_mask[dai->id],
  475. WSA_MACRO_RX_MAX) {
  476. int_1_mix1_inp = port;
  477. if ((int_1_mix1_inp < WSA_MACRO_RX0) ||
  478. (int_1_mix1_inp > WSA_MACRO_RX_MIX1)) {
  479. dev_err(wsa_dev,
  480. "%s: Invalid RX port, Dai ID is %d\n",
  481. __func__, dai->id);
  482. return -EINVAL;
  483. }
  484. int_mux_cfg0 = BOLERO_CDC_WSA_RX_INP_MUX_RX_INT0_CFG0;
  485. /*
  486. * Loop through all interpolator MUX inputs and find out
  487. * to which interpolator input, the cdc_dma rx port
  488. * is connected
  489. */
  490. for (j = 0; j < NUM_INTERPOLATORS; j++) {
  491. int_mux_cfg1 = int_mux_cfg0 + WSA_MACRO_MUX_CFG1_OFFSET;
  492. int_mux_cfg0_val = snd_soc_read(codec, int_mux_cfg0);
  493. int_mux_cfg1_val = snd_soc_read(codec, int_mux_cfg1);
  494. inp0_sel = int_mux_cfg0_val & WSA_MACRO_MUX_INP_MASK1;
  495. inp1_sel = (int_mux_cfg0_val >>
  496. WSA_MACRO_MUX_INP_SHFT) &
  497. WSA_MACRO_MUX_INP_MASK2;
  498. inp2_sel = (int_mux_cfg1_val >>
  499. WSA_MACRO_MUX_INP_SHFT) &
  500. WSA_MACRO_MUX_INP_MASK2;
  501. if ((inp0_sel == int_1_mix1_inp) ||
  502. (inp1_sel == int_1_mix1_inp) ||
  503. (inp2_sel == int_1_mix1_inp)) {
  504. int_fs_reg = BOLERO_CDC_WSA_RX0_RX_PATH_CTL +
  505. WSA_MACRO_RX_PATH_OFFSET * j;
  506. dev_dbg(wsa_dev,
  507. "%s: AIF_PB DAI(%d) connected to INT%u_1\n",
  508. __func__, dai->id, j);
  509. dev_dbg(wsa_dev,
  510. "%s: set INT%u_1 sample rate to %u\n",
  511. __func__, j, sample_rate);
  512. /* sample_rate is in Hz */
  513. snd_soc_update_bits(codec, int_fs_reg,
  514. WSA_MACRO_FS_RATE_MASK,
  515. int_prim_fs_rate_reg_val);
  516. }
  517. int_mux_cfg0 += WSA_MACRO_MUX_CFG_OFFSET;
  518. }
  519. }
  520. return 0;
  521. }
  522. static int wsa_macro_set_mix_interpolator_rate(struct snd_soc_dai *dai,
  523. u8 int_mix_fs_rate_reg_val,
  524. u32 sample_rate)
  525. {
  526. u8 int_2_inp;
  527. u32 j, port;
  528. u16 int_mux_cfg1, int_fs_reg;
  529. u8 int_mux_cfg1_val;
  530. struct snd_soc_codec *codec = dai->codec;
  531. struct device *wsa_dev = NULL;
  532. struct wsa_macro_priv *wsa_priv = NULL;
  533. if (!wsa_macro_get_data(codec, &wsa_dev, &wsa_priv, __func__))
  534. return -EINVAL;
  535. for_each_set_bit(port, &wsa_priv->active_ch_mask[dai->id],
  536. WSA_MACRO_RX_MAX) {
  537. int_2_inp = port;
  538. if ((int_2_inp < WSA_MACRO_RX0) ||
  539. (int_2_inp > WSA_MACRO_RX_MIX1)) {
  540. dev_err(wsa_dev,
  541. "%s: Invalid RX port, Dai ID is %d\n",
  542. __func__, dai->id);
  543. return -EINVAL;
  544. }
  545. int_mux_cfg1 = BOLERO_CDC_WSA_RX_INP_MUX_RX_INT0_CFG1;
  546. for (j = 0; j < NUM_INTERPOLATORS; j++) {
  547. int_mux_cfg1_val = snd_soc_read(codec, int_mux_cfg1) &
  548. WSA_MACRO_MUX_INP_MASK1;
  549. if (int_mux_cfg1_val == int_2_inp) {
  550. int_fs_reg =
  551. BOLERO_CDC_WSA_RX0_RX_PATH_MIX_CTL +
  552. WSA_MACRO_RX_PATH_OFFSET * j;
  553. dev_dbg(wsa_dev,
  554. "%s: AIF_PB DAI(%d) connected to INT%u_2\n",
  555. __func__, dai->id, j);
  556. dev_dbg(wsa_dev,
  557. "%s: set INT%u_2 sample rate to %u\n",
  558. __func__, j, sample_rate);
  559. snd_soc_update_bits(codec, int_fs_reg,
  560. WSA_MACRO_FS_RATE_MASK,
  561. int_mix_fs_rate_reg_val);
  562. }
  563. int_mux_cfg1 += WSA_MACRO_MUX_CFG_OFFSET;
  564. }
  565. }
  566. return 0;
  567. }
  568. static int wsa_macro_set_interpolator_rate(struct snd_soc_dai *dai,
  569. u32 sample_rate)
  570. {
  571. int rate_val = 0;
  572. int i, ret;
  573. /* set mixing path rate */
  574. for (i = 0; i < ARRAY_SIZE(int_mix_sample_rate_val); i++) {
  575. if (sample_rate ==
  576. int_mix_sample_rate_val[i].sample_rate) {
  577. rate_val =
  578. int_mix_sample_rate_val[i].rate_val;
  579. break;
  580. }
  581. }
  582. if ((i == ARRAY_SIZE(int_mix_sample_rate_val)) ||
  583. (rate_val < 0))
  584. goto prim_rate;
  585. ret = wsa_macro_set_mix_interpolator_rate(dai,
  586. (u8) rate_val, sample_rate);
  587. prim_rate:
  588. /* set primary path sample rate */
  589. for (i = 0; i < ARRAY_SIZE(int_prim_sample_rate_val); i++) {
  590. if (sample_rate ==
  591. int_prim_sample_rate_val[i].sample_rate) {
  592. rate_val =
  593. int_prim_sample_rate_val[i].rate_val;
  594. break;
  595. }
  596. }
  597. if ((i == ARRAY_SIZE(int_prim_sample_rate_val)) ||
  598. (rate_val < 0))
  599. return -EINVAL;
  600. ret = wsa_macro_set_prim_interpolator_rate(dai,
  601. (u8) rate_val, sample_rate);
  602. return ret;
  603. }
  604. static int wsa_macro_hw_params(struct snd_pcm_substream *substream,
  605. struct snd_pcm_hw_params *params,
  606. struct snd_soc_dai *dai)
  607. {
  608. struct snd_soc_codec *codec = dai->codec;
  609. int ret;
  610. dev_dbg(codec->dev,
  611. "%s: dai_name = %s DAI-ID %x rate %d num_ch %d\n", __func__,
  612. dai->name, dai->id, params_rate(params),
  613. params_channels(params));
  614. switch (substream->stream) {
  615. case SNDRV_PCM_STREAM_PLAYBACK:
  616. ret = wsa_macro_set_interpolator_rate(dai, params_rate(params));
  617. if (ret) {
  618. dev_err(codec->dev,
  619. "%s: cannot set sample rate: %u\n",
  620. __func__, params_rate(params));
  621. return ret;
  622. }
  623. break;
  624. case SNDRV_PCM_STREAM_CAPTURE:
  625. default:
  626. break;
  627. }
  628. return 0;
  629. }
  630. static int wsa_macro_get_channel_map(struct snd_soc_dai *dai,
  631. unsigned int *tx_num, unsigned int *tx_slot,
  632. unsigned int *rx_num, unsigned int *rx_slot)
  633. {
  634. struct snd_soc_codec *codec = dai->codec;
  635. struct device *wsa_dev = NULL;
  636. struct wsa_macro_priv *wsa_priv = NULL;
  637. if (!wsa_macro_get_data(codec, &wsa_dev, &wsa_priv, __func__))
  638. return -EINVAL;
  639. wsa_priv = dev_get_drvdata(wsa_dev);
  640. if (!wsa_priv)
  641. return -EINVAL;
  642. switch (dai->id) {
  643. case WSA_MACRO_AIF_VI:
  644. case WSA_MACRO_AIF_ECHO:
  645. *tx_slot = wsa_priv->active_ch_mask[dai->id];
  646. *tx_num = wsa_priv->active_ch_cnt[dai->id];
  647. break;
  648. case WSA_MACRO_AIF1_PB:
  649. case WSA_MACRO_AIF_MIX1_PB:
  650. *rx_slot = wsa_priv->active_ch_mask[dai->id];
  651. *rx_num = wsa_priv->active_ch_cnt[dai->id];
  652. break;
  653. default:
  654. dev_err(wsa_dev, "%s: Invalid AIF\n", __func__);
  655. break;
  656. }
  657. return 0;
  658. }
  659. static int wsa_macro_mclk_enable(struct wsa_macro_priv *wsa_priv,
  660. bool mclk_enable, bool dapm)
  661. {
  662. struct regmap *regmap = dev_get_regmap(wsa_priv->dev->parent, NULL);
  663. int ret = 0;
  664. if (regmap == NULL) {
  665. dev_err(wsa_priv->dev, "%s: regmap is NULL\n", __func__);
  666. return -EINVAL;
  667. }
  668. dev_dbg(wsa_priv->dev, "%s: mclk_enable = %u, dapm = %d clk_users= %d\n",
  669. __func__, mclk_enable, dapm, wsa_priv->wsa_mclk_users);
  670. mutex_lock(&wsa_priv->mclk_lock);
  671. if (mclk_enable) {
  672. wsa_priv->wsa_mclk_users++;
  673. if (wsa_priv->wsa_mclk_users == 1) {
  674. ret = bolero_request_clock(wsa_priv->dev,
  675. WSA_MACRO, MCLK_MUX0, true);
  676. if (ret < 0) {
  677. dev_err(wsa_priv->dev,
  678. "%s: wsa request clock enable failed\n",
  679. __func__);
  680. goto exit;
  681. }
  682. regcache_mark_dirty(regmap);
  683. regcache_sync_region(regmap,
  684. WSA_START_OFFSET,
  685. WSA_MAX_OFFSET);
  686. /* 9.6MHz MCLK, set value 0x00 if other frequency */
  687. regmap_update_bits(regmap,
  688. BOLERO_CDC_WSA_TOP_FREQ_MCLK, 0x01, 0x01);
  689. regmap_update_bits(regmap,
  690. BOLERO_CDC_WSA_CLK_RST_CTRL_MCLK_CONTROL,
  691. 0x01, 0x01);
  692. regmap_update_bits(regmap,
  693. BOLERO_CDC_WSA_CLK_RST_CTRL_FS_CNT_CONTROL,
  694. 0x01, 0x01);
  695. }
  696. } else {
  697. wsa_priv->wsa_mclk_users--;
  698. if (wsa_priv->wsa_mclk_users == 0) {
  699. regmap_update_bits(regmap,
  700. BOLERO_CDC_WSA_CLK_RST_CTRL_FS_CNT_CONTROL,
  701. 0x01, 0x00);
  702. regmap_update_bits(regmap,
  703. BOLERO_CDC_WSA_CLK_RST_CTRL_MCLK_CONTROL,
  704. 0x01, 0x00);
  705. bolero_request_clock(wsa_priv->dev,
  706. WSA_MACRO, MCLK_MUX0, false);
  707. }
  708. }
  709. exit:
  710. mutex_unlock(&wsa_priv->mclk_lock);
  711. return ret;
  712. }
  713. static int wsa_macro_mclk_event(struct snd_soc_dapm_widget *w,
  714. struct snd_kcontrol *kcontrol, int event)
  715. {
  716. struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
  717. int ret = 0;
  718. struct device *wsa_dev = NULL;
  719. struct wsa_macro_priv *wsa_priv = NULL;
  720. if (!wsa_macro_get_data(codec, &wsa_dev, &wsa_priv, __func__))
  721. return -EINVAL;
  722. dev_dbg(wsa_dev, "%s: event = %d\n", __func__, event);
  723. switch (event) {
  724. case SND_SOC_DAPM_PRE_PMU:
  725. ret = wsa_macro_mclk_enable(wsa_priv, 1, true);
  726. break;
  727. case SND_SOC_DAPM_POST_PMD:
  728. wsa_macro_mclk_enable(wsa_priv, 0, true);
  729. break;
  730. default:
  731. dev_err(wsa_priv->dev,
  732. "%s: invalid DAPM event %d\n", __func__, event);
  733. ret = -EINVAL;
  734. }
  735. return ret;
  736. }
  737. static int wsa_macro_mclk_ctrl(struct device *dev, bool enable)
  738. {
  739. struct wsa_macro_priv *wsa_priv = dev_get_drvdata(dev);
  740. int ret = 0;
  741. if (!wsa_priv)
  742. return -EINVAL;
  743. if (enable) {
  744. ret = clk_prepare_enable(wsa_priv->wsa_core_clk);
  745. if (ret < 0) {
  746. dev_err(dev, "%s:wsa mclk enable failed\n", __func__);
  747. goto exit;
  748. }
  749. ret = clk_prepare_enable(wsa_priv->wsa_npl_clk);
  750. if (ret < 0) {
  751. dev_err(dev, "%s:wsa npl_clk enable failed\n",
  752. __func__);
  753. clk_disable_unprepare(wsa_priv->wsa_core_clk);
  754. goto exit;
  755. }
  756. } else {
  757. clk_disable_unprepare(wsa_priv->wsa_npl_clk);
  758. clk_disable_unprepare(wsa_priv->wsa_core_clk);
  759. }
  760. exit:
  761. return ret;
  762. }
  763. static int wsa_macro_event_handler(struct snd_soc_codec *codec, u16 event,
  764. u32 data)
  765. {
  766. struct device *wsa_dev = NULL;
  767. struct wsa_macro_priv *wsa_priv = NULL;
  768. if (!wsa_macro_get_data(codec, &wsa_dev, &wsa_priv, __func__))
  769. return -EINVAL;
  770. switch (event) {
  771. case BOLERO_MACRO_EVT_SSR_DOWN:
  772. swrm_wcd_notify(
  773. wsa_priv->swr_ctrl_data[0].wsa_swr_pdev,
  774. SWR_DEVICE_SSR_DOWN, NULL);
  775. swrm_wcd_notify(
  776. wsa_priv->swr_ctrl_data[0].wsa_swr_pdev,
  777. SWR_DEVICE_DOWN, NULL);
  778. break;
  779. case BOLERO_MACRO_EVT_SSR_UP:
  780. swrm_wcd_notify(
  781. wsa_priv->swr_ctrl_data[0].wsa_swr_pdev,
  782. SWR_DEVICE_SSR_UP, NULL);
  783. break;
  784. }
  785. return 0;
  786. }
  787. static int wsa_macro_enable_vi_feedback(struct snd_soc_dapm_widget *w,
  788. struct snd_kcontrol *kcontrol,
  789. int event)
  790. {
  791. struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
  792. struct device *wsa_dev = NULL;
  793. struct wsa_macro_priv *wsa_priv = NULL;
  794. if (!wsa_macro_get_data(codec, &wsa_dev, &wsa_priv, __func__))
  795. return -EINVAL;
  796. switch (event) {
  797. case SND_SOC_DAPM_POST_PMU:
  798. if (test_bit(WSA_MACRO_TX0,
  799. &wsa_priv->active_ch_mask[WSA_MACRO_AIF_VI])) {
  800. dev_dbg(wsa_dev, "%s: spkr1 enabled\n", __func__);
  801. /* Enable V&I sensing */
  802. snd_soc_update_bits(codec,
  803. BOLERO_CDC_WSA_TX0_SPKR_PROT_PATH_CTL,
  804. 0x20, 0x20);
  805. snd_soc_update_bits(codec,
  806. BOLERO_CDC_WSA_TX1_SPKR_PROT_PATH_CTL,
  807. 0x20, 0x20);
  808. snd_soc_update_bits(codec,
  809. BOLERO_CDC_WSA_TX0_SPKR_PROT_PATH_CTL,
  810. 0x0F, 0x00);
  811. snd_soc_update_bits(codec,
  812. BOLERO_CDC_WSA_TX1_SPKR_PROT_PATH_CTL,
  813. 0x0F, 0x00);
  814. snd_soc_update_bits(codec,
  815. BOLERO_CDC_WSA_TX0_SPKR_PROT_PATH_CTL,
  816. 0x10, 0x10);
  817. snd_soc_update_bits(codec,
  818. BOLERO_CDC_WSA_TX1_SPKR_PROT_PATH_CTL,
  819. 0x10, 0x10);
  820. snd_soc_update_bits(codec,
  821. BOLERO_CDC_WSA_TX0_SPKR_PROT_PATH_CTL,
  822. 0x20, 0x00);
  823. snd_soc_update_bits(codec,
  824. BOLERO_CDC_WSA_TX1_SPKR_PROT_PATH_CTL,
  825. 0x20, 0x00);
  826. }
  827. if (test_bit(WSA_MACRO_TX1,
  828. &wsa_priv->active_ch_mask[WSA_MACRO_AIF_VI])) {
  829. dev_dbg(wsa_dev, "%s: spkr2 enabled\n", __func__);
  830. /* Enable V&I sensing */
  831. snd_soc_update_bits(codec,
  832. BOLERO_CDC_WSA_TX2_SPKR_PROT_PATH_CTL,
  833. 0x20, 0x20);
  834. snd_soc_update_bits(codec,
  835. BOLERO_CDC_WSA_TX3_SPKR_PROT_PATH_CTL,
  836. 0x20, 0x20);
  837. snd_soc_update_bits(codec,
  838. BOLERO_CDC_WSA_TX2_SPKR_PROT_PATH_CTL,
  839. 0x0F, 0x00);
  840. snd_soc_update_bits(codec,
  841. BOLERO_CDC_WSA_TX3_SPKR_PROT_PATH_CTL,
  842. 0x0F, 0x00);
  843. snd_soc_update_bits(codec,
  844. BOLERO_CDC_WSA_TX2_SPKR_PROT_PATH_CTL,
  845. 0x10, 0x10);
  846. snd_soc_update_bits(codec,
  847. BOLERO_CDC_WSA_TX3_SPKR_PROT_PATH_CTL,
  848. 0x10, 0x10);
  849. snd_soc_update_bits(codec,
  850. BOLERO_CDC_WSA_TX2_SPKR_PROT_PATH_CTL,
  851. 0x20, 0x00);
  852. snd_soc_update_bits(codec,
  853. BOLERO_CDC_WSA_TX3_SPKR_PROT_PATH_CTL,
  854. 0x20, 0x00);
  855. }
  856. break;
  857. case SND_SOC_DAPM_POST_PMD:
  858. if (test_bit(WSA_MACRO_TX0,
  859. &wsa_priv->active_ch_mask[WSA_MACRO_AIF_VI])) {
  860. /* Disable V&I sensing */
  861. snd_soc_update_bits(codec,
  862. BOLERO_CDC_WSA_TX0_SPKR_PROT_PATH_CTL,
  863. 0x20, 0x20);
  864. snd_soc_update_bits(codec,
  865. BOLERO_CDC_WSA_TX1_SPKR_PROT_PATH_CTL,
  866. 0x20, 0x20);
  867. dev_dbg(wsa_dev, "%s: spkr1 disabled\n", __func__);
  868. snd_soc_update_bits(codec,
  869. BOLERO_CDC_WSA_TX0_SPKR_PROT_PATH_CTL,
  870. 0x10, 0x00);
  871. snd_soc_update_bits(codec,
  872. BOLERO_CDC_WSA_TX1_SPKR_PROT_PATH_CTL,
  873. 0x10, 0x00);
  874. }
  875. if (test_bit(WSA_MACRO_TX1,
  876. &wsa_priv->active_ch_mask[WSA_MACRO_AIF_VI])) {
  877. /* Disable V&I sensing */
  878. dev_dbg(wsa_dev, "%s: spkr2 disabled\n", __func__);
  879. snd_soc_update_bits(codec,
  880. BOLERO_CDC_WSA_TX2_SPKR_PROT_PATH_CTL,
  881. 0x20, 0x20);
  882. snd_soc_update_bits(codec,
  883. BOLERO_CDC_WSA_TX3_SPKR_PROT_PATH_CTL,
  884. 0x20, 0x20);
  885. snd_soc_update_bits(codec,
  886. BOLERO_CDC_WSA_TX2_SPKR_PROT_PATH_CTL,
  887. 0x10, 0x00);
  888. snd_soc_update_bits(codec,
  889. BOLERO_CDC_WSA_TX3_SPKR_PROT_PATH_CTL,
  890. 0x10, 0x00);
  891. }
  892. break;
  893. }
  894. return 0;
  895. }
  896. static int wsa_macro_enable_mix_path(struct snd_soc_dapm_widget *w,
  897. struct snd_kcontrol *kcontrol, int event)
  898. {
  899. struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
  900. u16 gain_reg;
  901. int offset_val = 0;
  902. int val = 0;
  903. dev_dbg(codec->dev, "%s %d %s\n", __func__, event, w->name);
  904. switch (w->reg) {
  905. case BOLERO_CDC_WSA_RX0_RX_PATH_MIX_CTL:
  906. gain_reg = BOLERO_CDC_WSA_RX0_RX_VOL_MIX_CTL;
  907. break;
  908. case BOLERO_CDC_WSA_RX1_RX_PATH_MIX_CTL:
  909. gain_reg = BOLERO_CDC_WSA_RX1_RX_VOL_MIX_CTL;
  910. break;
  911. default:
  912. dev_err(codec->dev, "%s: No gain register avail for %s\n",
  913. __func__, w->name);
  914. return 0;
  915. }
  916. switch (event) {
  917. case SND_SOC_DAPM_POST_PMU:
  918. val = snd_soc_read(codec, gain_reg);
  919. val += offset_val;
  920. snd_soc_write(codec, gain_reg, val);
  921. break;
  922. case SND_SOC_DAPM_POST_PMD:
  923. break;
  924. }
  925. return 0;
  926. }
  927. static void wsa_macro_hd2_control(struct snd_soc_codec *codec,
  928. u16 reg, int event)
  929. {
  930. u16 hd2_scale_reg;
  931. u16 hd2_enable_reg = 0;
  932. if (reg == BOLERO_CDC_WSA_RX0_RX_PATH_CTL) {
  933. hd2_scale_reg = BOLERO_CDC_WSA_RX0_RX_PATH_SEC3;
  934. hd2_enable_reg = BOLERO_CDC_WSA_RX0_RX_PATH_CFG0;
  935. }
  936. if (reg == BOLERO_CDC_WSA_RX1_RX_PATH_CTL) {
  937. hd2_scale_reg = BOLERO_CDC_WSA_RX1_RX_PATH_SEC3;
  938. hd2_enable_reg = BOLERO_CDC_WSA_RX1_RX_PATH_CFG0;
  939. }
  940. if (hd2_enable_reg && SND_SOC_DAPM_EVENT_ON(event)) {
  941. snd_soc_update_bits(codec, hd2_scale_reg, 0x3C, 0x10);
  942. snd_soc_update_bits(codec, hd2_scale_reg, 0x03, 0x01);
  943. snd_soc_update_bits(codec, hd2_enable_reg, 0x04, 0x04);
  944. }
  945. if (hd2_enable_reg && SND_SOC_DAPM_EVENT_OFF(event)) {
  946. snd_soc_update_bits(codec, hd2_enable_reg, 0x04, 0x00);
  947. snd_soc_update_bits(codec, hd2_scale_reg, 0x03, 0x00);
  948. snd_soc_update_bits(codec, hd2_scale_reg, 0x3C, 0x00);
  949. }
  950. }
  951. static int wsa_macro_enable_swr(struct snd_soc_dapm_widget *w,
  952. struct snd_kcontrol *kcontrol, int event)
  953. {
  954. struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
  955. int ch_cnt;
  956. struct device *wsa_dev = NULL;
  957. struct wsa_macro_priv *wsa_priv = NULL;
  958. if (!wsa_macro_get_data(codec, &wsa_dev, &wsa_priv, __func__))
  959. return -EINVAL;
  960. switch (event) {
  961. case SND_SOC_DAPM_PRE_PMU:
  962. if (!(strnstr(w->name, "RX0", sizeof("WSA_RX0"))) &&
  963. !wsa_priv->rx_0_count)
  964. wsa_priv->rx_0_count++;
  965. if (!(strnstr(w->name, "RX1", sizeof("WSA_RX1"))) &&
  966. !wsa_priv->rx_1_count)
  967. wsa_priv->rx_1_count++;
  968. ch_cnt = wsa_priv->rx_0_count + wsa_priv->rx_1_count;
  969. swrm_wcd_notify(
  970. wsa_priv->swr_ctrl_data[0].wsa_swr_pdev,
  971. SWR_DEVICE_UP, NULL);
  972. swrm_wcd_notify(
  973. wsa_priv->swr_ctrl_data[0].wsa_swr_pdev,
  974. SWR_SET_NUM_RX_CH, &ch_cnt);
  975. break;
  976. case SND_SOC_DAPM_POST_PMD:
  977. if (!(strnstr(w->name, "RX0", sizeof("WSA_RX0"))) &&
  978. wsa_priv->rx_0_count)
  979. wsa_priv->rx_0_count--;
  980. if (!(strnstr(w->name, "RX1", sizeof("WSA_RX1"))) &&
  981. wsa_priv->rx_1_count)
  982. wsa_priv->rx_1_count--;
  983. ch_cnt = wsa_priv->rx_0_count + wsa_priv->rx_1_count;
  984. swrm_wcd_notify(
  985. wsa_priv->swr_ctrl_data[0].wsa_swr_pdev,
  986. SWR_SET_NUM_RX_CH, &ch_cnt);
  987. break;
  988. }
  989. dev_dbg(wsa_priv->dev, "%s: current swr ch cnt: %d\n",
  990. __func__, wsa_priv->rx_0_count + wsa_priv->rx_1_count);
  991. return 0;
  992. }
  993. static int wsa_macro_config_compander(struct snd_soc_codec *codec,
  994. int comp, int event)
  995. {
  996. u16 comp_ctl0_reg, rx_path_cfg0_reg;
  997. struct device *wsa_dev = NULL;
  998. struct wsa_macro_priv *wsa_priv = NULL;
  999. if (!wsa_macro_get_data(codec, &wsa_dev, &wsa_priv, __func__))
  1000. return -EINVAL;
  1001. dev_dbg(codec->dev, "%s: event %d compander %d, enabled %d\n",
  1002. __func__, event, comp + 1, wsa_priv->comp_enabled[comp]);
  1003. if (!wsa_priv->comp_enabled[comp])
  1004. return 0;
  1005. comp_ctl0_reg = BOLERO_CDC_WSA_COMPANDER0_CTL0 +
  1006. (comp * WSA_MACRO_RX_COMP_OFFSET);
  1007. rx_path_cfg0_reg = BOLERO_CDC_WSA_RX0_RX_PATH_CFG0 +
  1008. (comp * WSA_MACRO_RX_PATH_OFFSET);
  1009. if (SND_SOC_DAPM_EVENT_ON(event)) {
  1010. /* Enable Compander Clock */
  1011. snd_soc_update_bits(codec, comp_ctl0_reg, 0x01, 0x01);
  1012. snd_soc_update_bits(codec, comp_ctl0_reg, 0x02, 0x02);
  1013. snd_soc_update_bits(codec, comp_ctl0_reg, 0x02, 0x00);
  1014. snd_soc_update_bits(codec, rx_path_cfg0_reg, 0x02, 0x02);
  1015. }
  1016. if (SND_SOC_DAPM_EVENT_OFF(event)) {
  1017. snd_soc_update_bits(codec, comp_ctl0_reg, 0x04, 0x04);
  1018. snd_soc_update_bits(codec, rx_path_cfg0_reg, 0x02, 0x00);
  1019. snd_soc_update_bits(codec, comp_ctl0_reg, 0x02, 0x02);
  1020. snd_soc_update_bits(codec, comp_ctl0_reg, 0x02, 0x00);
  1021. snd_soc_update_bits(codec, comp_ctl0_reg, 0x01, 0x00);
  1022. snd_soc_update_bits(codec, comp_ctl0_reg, 0x04, 0x00);
  1023. }
  1024. return 0;
  1025. }
  1026. static void wsa_macro_enable_softclip_clk(struct snd_soc_codec *codec,
  1027. struct wsa_macro_priv *wsa_priv,
  1028. int path,
  1029. bool enable)
  1030. {
  1031. u16 softclip_clk_reg = BOLERO_CDC_WSA_SOFTCLIP0_CRC +
  1032. (path * WSA_MACRO_RX_SOFTCLIP_OFFSET);
  1033. u8 softclip_mux_mask = (1 << path);
  1034. u8 softclip_mux_value = (1 << path);
  1035. dev_dbg(codec->dev, "%s: path %d, enable %d\n",
  1036. __func__, path, enable);
  1037. if (enable) {
  1038. if (wsa_priv->softclip_clk_users[path] == 0) {
  1039. snd_soc_update_bits(codec,
  1040. softclip_clk_reg, 0x01, 0x01);
  1041. snd_soc_update_bits(codec,
  1042. BOLERO_CDC_WSA_RX_INP_MUX_SOFTCLIP_CFG0,
  1043. softclip_mux_mask, softclip_mux_value);
  1044. }
  1045. wsa_priv->softclip_clk_users[path]++;
  1046. } else {
  1047. wsa_priv->softclip_clk_users[path]--;
  1048. if (wsa_priv->softclip_clk_users[path] == 0) {
  1049. snd_soc_update_bits(codec,
  1050. softclip_clk_reg, 0x01, 0x00);
  1051. snd_soc_update_bits(codec,
  1052. BOLERO_CDC_WSA_RX_INP_MUX_SOFTCLIP_CFG0,
  1053. softclip_mux_mask, 0x00);
  1054. }
  1055. }
  1056. }
  1057. static int wsa_macro_config_softclip(struct snd_soc_codec *codec,
  1058. int path, int event)
  1059. {
  1060. u16 softclip_ctrl_reg = 0;
  1061. struct device *wsa_dev = NULL;
  1062. struct wsa_macro_priv *wsa_priv = NULL;
  1063. int softclip_path = 0;
  1064. if (!wsa_macro_get_data(codec, &wsa_dev, &wsa_priv, __func__))
  1065. return -EINVAL;
  1066. if (path == WSA_MACRO_COMP1)
  1067. softclip_path = WSA_MACRO_SOFTCLIP0;
  1068. else if (path == WSA_MACRO_COMP2)
  1069. softclip_path = WSA_MACRO_SOFTCLIP1;
  1070. dev_dbg(codec->dev, "%s: event %d path %d, enabled %d\n",
  1071. __func__, event, softclip_path,
  1072. wsa_priv->is_softclip_on[softclip_path]);
  1073. if (!wsa_priv->is_softclip_on[softclip_path])
  1074. return 0;
  1075. softclip_ctrl_reg = BOLERO_CDC_WSA_SOFTCLIP0_SOFTCLIP_CTRL +
  1076. (softclip_path * WSA_MACRO_RX_SOFTCLIP_OFFSET);
  1077. if (SND_SOC_DAPM_EVENT_ON(event)) {
  1078. /* Enable Softclip clock and mux */
  1079. wsa_macro_enable_softclip_clk(codec, wsa_priv, softclip_path,
  1080. true);
  1081. /* Enable Softclip control */
  1082. snd_soc_update_bits(codec, softclip_ctrl_reg, 0x01, 0x01);
  1083. }
  1084. if (SND_SOC_DAPM_EVENT_OFF(event)) {
  1085. snd_soc_update_bits(codec, softclip_ctrl_reg, 0x01, 0x00);
  1086. wsa_macro_enable_softclip_clk(codec, wsa_priv, softclip_path,
  1087. false);
  1088. }
  1089. return 0;
  1090. }
  1091. static int wsa_macro_interp_get_primary_reg(u16 reg, u16 *ind)
  1092. {
  1093. u16 prim_int_reg = 0;
  1094. switch (reg) {
  1095. case BOLERO_CDC_WSA_RX0_RX_PATH_CTL:
  1096. case BOLERO_CDC_WSA_RX0_RX_PATH_MIX_CTL:
  1097. prim_int_reg = BOLERO_CDC_WSA_RX0_RX_PATH_CTL;
  1098. *ind = 0;
  1099. break;
  1100. case BOLERO_CDC_WSA_RX1_RX_PATH_CTL:
  1101. case BOLERO_CDC_WSA_RX1_RX_PATH_MIX_CTL:
  1102. prim_int_reg = BOLERO_CDC_WSA_RX1_RX_PATH_CTL;
  1103. *ind = 1;
  1104. break;
  1105. }
  1106. return prim_int_reg;
  1107. }
  1108. static int wsa_macro_enable_prim_interpolator(
  1109. struct snd_soc_codec *codec,
  1110. u16 reg, int event)
  1111. {
  1112. u16 prim_int_reg;
  1113. u16 ind = 0;
  1114. struct device *wsa_dev = NULL;
  1115. struct wsa_macro_priv *wsa_priv = NULL;
  1116. if (!wsa_macro_get_data(codec, &wsa_dev, &wsa_priv, __func__))
  1117. return -EINVAL;
  1118. prim_int_reg = wsa_macro_interp_get_primary_reg(reg, &ind);
  1119. switch (event) {
  1120. case SND_SOC_DAPM_PRE_PMU:
  1121. wsa_priv->prim_int_users[ind]++;
  1122. if (wsa_priv->prim_int_users[ind] == 1) {
  1123. snd_soc_update_bits(codec,
  1124. prim_int_reg + WSA_MACRO_RX_PATH_CFG3_OFFSET,
  1125. 0x03, 0x03);
  1126. snd_soc_update_bits(codec, prim_int_reg,
  1127. 0x10, 0x10);
  1128. wsa_macro_hd2_control(codec, prim_int_reg, event);
  1129. snd_soc_update_bits(codec,
  1130. prim_int_reg + WSA_MACRO_RX_PATH_DSMDEM_OFFSET,
  1131. 0x1, 0x1);
  1132. snd_soc_update_bits(codec, prim_int_reg,
  1133. 1 << 0x5, 1 << 0x5);
  1134. }
  1135. if ((reg != prim_int_reg) &&
  1136. ((snd_soc_read(codec, prim_int_reg)) & 0x10))
  1137. snd_soc_update_bits(codec, reg, 0x10, 0x10);
  1138. break;
  1139. case SND_SOC_DAPM_POST_PMD:
  1140. wsa_priv->prim_int_users[ind]--;
  1141. if (wsa_priv->prim_int_users[ind] == 0) {
  1142. snd_soc_update_bits(codec, prim_int_reg,
  1143. 1 << 0x5, 0 << 0x5);
  1144. snd_soc_update_bits(codec, prim_int_reg,
  1145. 0x40, 0x40);
  1146. snd_soc_update_bits(codec, prim_int_reg,
  1147. 0x40, 0x00);
  1148. wsa_macro_hd2_control(codec, prim_int_reg, event);
  1149. }
  1150. break;
  1151. }
  1152. dev_dbg(codec->dev, "%s: primary interpolator: INT%d, users: %d\n",
  1153. __func__, ind, wsa_priv->prim_int_users[ind]);
  1154. return 0;
  1155. }
  1156. static int wsa_macro_enable_interpolator(struct snd_soc_dapm_widget *w,
  1157. struct snd_kcontrol *kcontrol,
  1158. int event)
  1159. {
  1160. struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
  1161. u16 gain_reg;
  1162. u16 reg;
  1163. int val;
  1164. int offset_val = 0;
  1165. struct device *wsa_dev = NULL;
  1166. struct wsa_macro_priv *wsa_priv = NULL;
  1167. if (!wsa_macro_get_data(codec, &wsa_dev, &wsa_priv, __func__))
  1168. return -EINVAL;
  1169. dev_dbg(codec->dev, "%s %d %s\n", __func__, event, w->name);
  1170. if (!(strcmp(w->name, "WSA_RX INT0 INTERP"))) {
  1171. reg = BOLERO_CDC_WSA_RX0_RX_PATH_CTL;
  1172. gain_reg = BOLERO_CDC_WSA_RX0_RX_VOL_CTL;
  1173. } else if (!(strcmp(w->name, "WSA_RX INT1 INTERP"))) {
  1174. reg = BOLERO_CDC_WSA_RX1_RX_PATH_CTL;
  1175. gain_reg = BOLERO_CDC_WSA_RX1_RX_VOL_CTL;
  1176. } else {
  1177. dev_err(codec->dev, "%s: Interpolator reg not found\n",
  1178. __func__);
  1179. return -EINVAL;
  1180. }
  1181. switch (event) {
  1182. case SND_SOC_DAPM_PRE_PMU:
  1183. /* Reset if needed */
  1184. wsa_macro_enable_prim_interpolator(codec, reg, event);
  1185. break;
  1186. case SND_SOC_DAPM_POST_PMU:
  1187. wsa_macro_config_compander(codec, w->shift, event);
  1188. wsa_macro_config_softclip(codec, w->shift, event);
  1189. /* apply gain after int clk is enabled */
  1190. if ((wsa_priv->spkr_gain_offset ==
  1191. WSA_MACRO_GAIN_OFFSET_M1P5_DB) &&
  1192. (wsa_priv->comp_enabled[WSA_MACRO_COMP1] ||
  1193. wsa_priv->comp_enabled[WSA_MACRO_COMP2]) &&
  1194. (gain_reg == BOLERO_CDC_WSA_RX0_RX_VOL_CTL ||
  1195. gain_reg == BOLERO_CDC_WSA_RX1_RX_VOL_CTL)) {
  1196. snd_soc_update_bits(codec, BOLERO_CDC_WSA_RX0_RX_PATH_SEC1,
  1197. 0x01, 0x01);
  1198. snd_soc_update_bits(codec,
  1199. BOLERO_CDC_WSA_RX0_RX_PATH_MIX_SEC0,
  1200. 0x01, 0x01);
  1201. snd_soc_update_bits(codec, BOLERO_CDC_WSA_RX1_RX_PATH_SEC1,
  1202. 0x01, 0x01);
  1203. snd_soc_update_bits(codec,
  1204. BOLERO_CDC_WSA_RX1_RX_PATH_MIX_SEC0,
  1205. 0x01, 0x01);
  1206. offset_val = -2;
  1207. }
  1208. val = snd_soc_read(codec, gain_reg);
  1209. val += offset_val;
  1210. snd_soc_write(codec, gain_reg, val);
  1211. wsa_macro_config_ear_spkr_gain(codec, wsa_priv,
  1212. event, gain_reg);
  1213. break;
  1214. case SND_SOC_DAPM_POST_PMD:
  1215. wsa_macro_config_compander(codec, w->shift, event);
  1216. wsa_macro_config_softclip(codec, w->shift, event);
  1217. wsa_macro_enable_prim_interpolator(codec, reg, event);
  1218. if ((wsa_priv->spkr_gain_offset ==
  1219. WSA_MACRO_GAIN_OFFSET_M1P5_DB) &&
  1220. (wsa_priv->comp_enabled[WSA_MACRO_COMP1] ||
  1221. wsa_priv->comp_enabled[WSA_MACRO_COMP2]) &&
  1222. (gain_reg == BOLERO_CDC_WSA_RX0_RX_VOL_CTL ||
  1223. gain_reg == BOLERO_CDC_WSA_RX1_RX_VOL_CTL)) {
  1224. snd_soc_update_bits(codec, BOLERO_CDC_WSA_RX0_RX_PATH_SEC1,
  1225. 0x01, 0x00);
  1226. snd_soc_update_bits(codec,
  1227. BOLERO_CDC_WSA_RX0_RX_PATH_MIX_SEC0,
  1228. 0x01, 0x00);
  1229. snd_soc_update_bits(codec, BOLERO_CDC_WSA_RX1_RX_PATH_SEC1,
  1230. 0x01, 0x00);
  1231. snd_soc_update_bits(codec,
  1232. BOLERO_CDC_WSA_RX1_RX_PATH_MIX_SEC0,
  1233. 0x01, 0x00);
  1234. offset_val = 2;
  1235. val = snd_soc_read(codec, gain_reg);
  1236. val += offset_val;
  1237. snd_soc_write(codec, gain_reg, val);
  1238. }
  1239. wsa_macro_config_ear_spkr_gain(codec, wsa_priv,
  1240. event, gain_reg);
  1241. break;
  1242. }
  1243. return 0;
  1244. }
  1245. static int wsa_macro_config_ear_spkr_gain(struct snd_soc_codec *codec,
  1246. struct wsa_macro_priv *wsa_priv,
  1247. int event, int gain_reg)
  1248. {
  1249. int comp_gain_offset, val;
  1250. switch (wsa_priv->spkr_mode) {
  1251. /* Compander gain in WSA_MACRO_SPKR_MODE1 case is 12 dB */
  1252. case WSA_MACRO_SPKR_MODE_1:
  1253. comp_gain_offset = -12;
  1254. break;
  1255. /* Default case compander gain is 15 dB */
  1256. default:
  1257. comp_gain_offset = -15;
  1258. break;
  1259. }
  1260. switch (event) {
  1261. case SND_SOC_DAPM_POST_PMU:
  1262. /* Apply ear spkr gain only if compander is enabled */
  1263. if (wsa_priv->comp_enabled[WSA_MACRO_COMP1] &&
  1264. (gain_reg == BOLERO_CDC_WSA_RX0_RX_VOL_CTL) &&
  1265. (wsa_priv->ear_spkr_gain != 0)) {
  1266. /* For example, val is -8(-12+5-1) for 4dB of gain */
  1267. val = comp_gain_offset + wsa_priv->ear_spkr_gain - 1;
  1268. snd_soc_write(codec, gain_reg, val);
  1269. dev_dbg(wsa_priv->dev, "%s: RX0 Volume %d dB\n",
  1270. __func__, val);
  1271. }
  1272. break;
  1273. case SND_SOC_DAPM_POST_PMD:
  1274. /*
  1275. * Reset RX0 volume to 0 dB if compander is enabled and
  1276. * ear_spkr_gain is non-zero.
  1277. */
  1278. if (wsa_priv->comp_enabled[WSA_MACRO_COMP1] &&
  1279. (gain_reg == BOLERO_CDC_WSA_RX0_RX_VOL_CTL) &&
  1280. (wsa_priv->ear_spkr_gain != 0)) {
  1281. snd_soc_write(codec, gain_reg, 0x0);
  1282. dev_dbg(wsa_priv->dev, "%s: Reset RX0 Volume to 0 dB\n",
  1283. __func__);
  1284. }
  1285. break;
  1286. }
  1287. return 0;
  1288. }
  1289. static int wsa_macro_spk_boost_event(struct snd_soc_dapm_widget *w,
  1290. struct snd_kcontrol *kcontrol,
  1291. int event)
  1292. {
  1293. struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
  1294. u16 boost_path_ctl, boost_path_cfg1;
  1295. u16 reg, reg_mix;
  1296. dev_dbg(codec->dev, "%s %s %d\n", __func__, w->name, event);
  1297. if (!strcmp(w->name, "WSA_RX INT0 CHAIN")) {
  1298. boost_path_ctl = BOLERO_CDC_WSA_BOOST0_BOOST_PATH_CTL;
  1299. boost_path_cfg1 = BOLERO_CDC_WSA_RX0_RX_PATH_CFG1;
  1300. reg = BOLERO_CDC_WSA_RX0_RX_PATH_CTL;
  1301. reg_mix = BOLERO_CDC_WSA_RX0_RX_PATH_MIX_CTL;
  1302. } else if (!strcmp(w->name, "WSA_RX INT1 CHAIN")) {
  1303. boost_path_ctl = BOLERO_CDC_WSA_BOOST1_BOOST_PATH_CTL;
  1304. boost_path_cfg1 = BOLERO_CDC_WSA_RX1_RX_PATH_CFG1;
  1305. reg = BOLERO_CDC_WSA_RX1_RX_PATH_CTL;
  1306. reg_mix = BOLERO_CDC_WSA_RX1_RX_PATH_MIX_CTL;
  1307. } else {
  1308. dev_err(codec->dev, "%s: unknown widget: %s\n",
  1309. __func__, w->name);
  1310. return -EINVAL;
  1311. }
  1312. switch (event) {
  1313. case SND_SOC_DAPM_PRE_PMU:
  1314. snd_soc_update_bits(codec, boost_path_cfg1, 0x01, 0x01);
  1315. snd_soc_update_bits(codec, boost_path_ctl, 0x10, 0x10);
  1316. if ((snd_soc_read(codec, reg_mix)) & 0x10)
  1317. snd_soc_update_bits(codec, reg_mix, 0x10, 0x00);
  1318. break;
  1319. case SND_SOC_DAPM_POST_PMU:
  1320. snd_soc_update_bits(codec, reg, 0x10, 0x00);
  1321. break;
  1322. case SND_SOC_DAPM_POST_PMD:
  1323. snd_soc_update_bits(codec, boost_path_ctl, 0x10, 0x00);
  1324. snd_soc_update_bits(codec, boost_path_cfg1, 0x01, 0x00);
  1325. break;
  1326. }
  1327. return 0;
  1328. }
  1329. static int wsa_macro_enable_vbat(struct snd_soc_dapm_widget *w,
  1330. struct snd_kcontrol *kcontrol,
  1331. int event)
  1332. {
  1333. struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
  1334. struct device *wsa_dev = NULL;
  1335. struct wsa_macro_priv *wsa_priv = NULL;
  1336. u16 vbat_path_cfg = 0;
  1337. int softclip_path = 0;
  1338. if (!wsa_macro_get_data(codec, &wsa_dev, &wsa_priv, __func__))
  1339. return -EINVAL;
  1340. dev_dbg(codec->dev, "%s %s %d\n", __func__, w->name, event);
  1341. if (!strcmp(w->name, "WSA_RX INT0 VBAT")) {
  1342. vbat_path_cfg = BOLERO_CDC_WSA_RX0_RX_PATH_CFG1;
  1343. softclip_path = WSA_MACRO_SOFTCLIP0;
  1344. } else if (!strcmp(w->name, "WSA_RX INT1 VBAT")) {
  1345. vbat_path_cfg = BOLERO_CDC_WSA_RX1_RX_PATH_CFG1;
  1346. softclip_path = WSA_MACRO_SOFTCLIP1;
  1347. }
  1348. switch (event) {
  1349. case SND_SOC_DAPM_PRE_PMU:
  1350. /* Enable clock for VBAT block */
  1351. snd_soc_update_bits(codec,
  1352. BOLERO_CDC_WSA_VBAT_BCL_VBAT_PATH_CTL, 0x10, 0x10);
  1353. /* Enable VBAT block */
  1354. snd_soc_update_bits(codec,
  1355. BOLERO_CDC_WSA_VBAT_BCL_VBAT_CFG, 0x01, 0x01);
  1356. /* Update interpolator with 384K path */
  1357. snd_soc_update_bits(codec, vbat_path_cfg, 0x80, 0x80);
  1358. /* Use attenuation mode */
  1359. snd_soc_update_bits(codec, BOLERO_CDC_WSA_VBAT_BCL_VBAT_CFG,
  1360. 0x02, 0x00);
  1361. /*
  1362. * BCL block needs softclip clock and mux config to be enabled
  1363. */
  1364. wsa_macro_enable_softclip_clk(codec, wsa_priv, softclip_path,
  1365. true);
  1366. /* Enable VBAT at channel level */
  1367. snd_soc_update_bits(codec, vbat_path_cfg, 0x02, 0x02);
  1368. /* Set the ATTK1 gain */
  1369. snd_soc_update_bits(codec,
  1370. BOLERO_CDC_WSA_VBAT_BCL_VBAT_BCL_GAIN_UPD1,
  1371. 0xFF, 0xFF);
  1372. snd_soc_update_bits(codec,
  1373. BOLERO_CDC_WSA_VBAT_BCL_VBAT_BCL_GAIN_UPD2,
  1374. 0xFF, 0x03);
  1375. snd_soc_update_bits(codec,
  1376. BOLERO_CDC_WSA_VBAT_BCL_VBAT_BCL_GAIN_UPD3,
  1377. 0xFF, 0x00);
  1378. /* Set the ATTK2 gain */
  1379. snd_soc_update_bits(codec,
  1380. BOLERO_CDC_WSA_VBAT_BCL_VBAT_BCL_GAIN_UPD4,
  1381. 0xFF, 0xFF);
  1382. snd_soc_update_bits(codec,
  1383. BOLERO_CDC_WSA_VBAT_BCL_VBAT_BCL_GAIN_UPD5,
  1384. 0xFF, 0x03);
  1385. snd_soc_update_bits(codec,
  1386. BOLERO_CDC_WSA_VBAT_BCL_VBAT_BCL_GAIN_UPD6,
  1387. 0xFF, 0x00);
  1388. /* Set the ATTK3 gain */
  1389. snd_soc_update_bits(codec,
  1390. BOLERO_CDC_WSA_VBAT_BCL_VBAT_BCL_GAIN_UPD7,
  1391. 0xFF, 0xFF);
  1392. snd_soc_update_bits(codec,
  1393. BOLERO_CDC_WSA_VBAT_BCL_VBAT_BCL_GAIN_UPD8,
  1394. 0xFF, 0x03);
  1395. snd_soc_update_bits(codec,
  1396. BOLERO_CDC_WSA_VBAT_BCL_VBAT_BCL_GAIN_UPD9,
  1397. 0xFF, 0x00);
  1398. break;
  1399. case SND_SOC_DAPM_POST_PMD:
  1400. snd_soc_update_bits(codec, vbat_path_cfg, 0x80, 0x00);
  1401. snd_soc_update_bits(codec, BOLERO_CDC_WSA_VBAT_BCL_VBAT_CFG,
  1402. 0x02, 0x02);
  1403. snd_soc_update_bits(codec, vbat_path_cfg, 0x02, 0x00);
  1404. snd_soc_update_bits(codec,
  1405. BOLERO_CDC_WSA_VBAT_BCL_VBAT_BCL_GAIN_UPD1,
  1406. 0xFF, 0x00);
  1407. snd_soc_update_bits(codec,
  1408. BOLERO_CDC_WSA_VBAT_BCL_VBAT_BCL_GAIN_UPD2,
  1409. 0xFF, 0x00);
  1410. snd_soc_update_bits(codec,
  1411. BOLERO_CDC_WSA_VBAT_BCL_VBAT_BCL_GAIN_UPD3,
  1412. 0xFF, 0x00);
  1413. snd_soc_update_bits(codec,
  1414. BOLERO_CDC_WSA_VBAT_BCL_VBAT_BCL_GAIN_UPD4,
  1415. 0xFF, 0x00);
  1416. snd_soc_update_bits(codec,
  1417. BOLERO_CDC_WSA_VBAT_BCL_VBAT_BCL_GAIN_UPD5,
  1418. 0xFF, 0x00);
  1419. snd_soc_update_bits(codec,
  1420. BOLERO_CDC_WSA_VBAT_BCL_VBAT_BCL_GAIN_UPD6,
  1421. 0xFF, 0x00);
  1422. snd_soc_update_bits(codec,
  1423. BOLERO_CDC_WSA_VBAT_BCL_VBAT_BCL_GAIN_UPD7,
  1424. 0xFF, 0x00);
  1425. snd_soc_update_bits(codec,
  1426. BOLERO_CDC_WSA_VBAT_BCL_VBAT_BCL_GAIN_UPD8,
  1427. 0xFF, 0x00);
  1428. snd_soc_update_bits(codec,
  1429. BOLERO_CDC_WSA_VBAT_BCL_VBAT_BCL_GAIN_UPD9,
  1430. 0xFF, 0x00);
  1431. wsa_macro_enable_softclip_clk(codec, wsa_priv, softclip_path,
  1432. false);
  1433. snd_soc_update_bits(codec,
  1434. BOLERO_CDC_WSA_VBAT_BCL_VBAT_CFG, 0x01, 0x00);
  1435. snd_soc_update_bits(codec,
  1436. BOLERO_CDC_WSA_VBAT_BCL_VBAT_PATH_CTL, 0x10, 0x00);
  1437. break;
  1438. default:
  1439. dev_err(wsa_dev, "%s: Invalid event %d\n", __func__, event);
  1440. break;
  1441. }
  1442. return 0;
  1443. }
  1444. static int wsa_macro_enable_echo(struct snd_soc_dapm_widget *w,
  1445. struct snd_kcontrol *kcontrol,
  1446. int event)
  1447. {
  1448. struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
  1449. struct device *wsa_dev = NULL;
  1450. struct wsa_macro_priv *wsa_priv = NULL;
  1451. u16 val, ec_tx = 0, ec_hq_reg;
  1452. if (!wsa_macro_get_data(codec, &wsa_dev, &wsa_priv, __func__))
  1453. return -EINVAL;
  1454. dev_dbg(wsa_dev, "%s %d %s\n", __func__, event, w->name);
  1455. val = snd_soc_read(codec, BOLERO_CDC_WSA_RX_INP_MUX_RX_MIX_CFG0);
  1456. if (!(strcmp(w->name, "WSA RX_MIX EC0_MUX")))
  1457. ec_tx = (val & 0x07) - 1;
  1458. else
  1459. ec_tx = ((val & 0x38) >> 0x3) - 1;
  1460. if (ec_tx < 0 || ec_tx >= (WSA_MACRO_RX1 + 1)) {
  1461. dev_err(wsa_dev, "%s: EC mix control not set correctly\n",
  1462. __func__);
  1463. return -EINVAL;
  1464. }
  1465. if (wsa_priv->ec_hq[ec_tx]) {
  1466. snd_soc_update_bits(codec,
  1467. BOLERO_CDC_WSA_RX_INP_MUX_RX_MIX_CFG0,
  1468. 0x1 << ec_tx, 0x1 << ec_tx);
  1469. ec_hq_reg = BOLERO_CDC_WSA_EC_HQ0_EC_REF_HQ_PATH_CTL +
  1470. 0x20 * ec_tx;
  1471. snd_soc_update_bits(codec, ec_hq_reg, 0x01, 0x01);
  1472. ec_hq_reg = BOLERO_CDC_WSA_EC_HQ0_EC_REF_HQ_CFG0 +
  1473. 0x20 * ec_tx;
  1474. /* default set to 48k */
  1475. snd_soc_update_bits(codec, ec_hq_reg, 0x1E, 0x08);
  1476. }
  1477. return 0;
  1478. }
  1479. static int wsa_macro_get_ec_hq(struct snd_kcontrol *kcontrol,
  1480. struct snd_ctl_elem_value *ucontrol)
  1481. {
  1482. struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
  1483. int ec_tx = ((struct soc_multi_mixer_control *)
  1484. kcontrol->private_value)->shift;
  1485. struct device *wsa_dev = NULL;
  1486. struct wsa_macro_priv *wsa_priv = NULL;
  1487. if (!wsa_macro_get_data(codec, &wsa_dev, &wsa_priv, __func__))
  1488. return -EINVAL;
  1489. ucontrol->value.integer.value[0] = wsa_priv->ec_hq[ec_tx];
  1490. return 0;
  1491. }
  1492. static int wsa_macro_set_ec_hq(struct snd_kcontrol *kcontrol,
  1493. struct snd_ctl_elem_value *ucontrol)
  1494. {
  1495. struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
  1496. int ec_tx = ((struct soc_multi_mixer_control *)
  1497. kcontrol->private_value)->shift;
  1498. int value = ucontrol->value.integer.value[0];
  1499. struct device *wsa_dev = NULL;
  1500. struct wsa_macro_priv *wsa_priv = NULL;
  1501. if (!wsa_macro_get_data(codec, &wsa_dev, &wsa_priv, __func__))
  1502. return -EINVAL;
  1503. dev_dbg(wsa_dev, "%s: enable current %d, new %d\n",
  1504. __func__, wsa_priv->ec_hq[ec_tx], value);
  1505. wsa_priv->ec_hq[ec_tx] = value;
  1506. return 0;
  1507. }
  1508. static int wsa_macro_get_compander(struct snd_kcontrol *kcontrol,
  1509. struct snd_ctl_elem_value *ucontrol)
  1510. {
  1511. struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
  1512. int comp = ((struct soc_multi_mixer_control *)
  1513. kcontrol->private_value)->shift;
  1514. struct device *wsa_dev = NULL;
  1515. struct wsa_macro_priv *wsa_priv = NULL;
  1516. if (!wsa_macro_get_data(codec, &wsa_dev, &wsa_priv, __func__))
  1517. return -EINVAL;
  1518. ucontrol->value.integer.value[0] = wsa_priv->comp_enabled[comp];
  1519. return 0;
  1520. }
  1521. static int wsa_macro_set_compander(struct snd_kcontrol *kcontrol,
  1522. struct snd_ctl_elem_value *ucontrol)
  1523. {
  1524. struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
  1525. int comp = ((struct soc_multi_mixer_control *)
  1526. kcontrol->private_value)->shift;
  1527. int value = ucontrol->value.integer.value[0];
  1528. struct device *wsa_dev = NULL;
  1529. struct wsa_macro_priv *wsa_priv = NULL;
  1530. if (!wsa_macro_get_data(codec, &wsa_dev, &wsa_priv, __func__))
  1531. return -EINVAL;
  1532. dev_dbg(codec->dev, "%s: Compander %d enable current %d, new %d\n",
  1533. __func__, comp + 1, wsa_priv->comp_enabled[comp], value);
  1534. wsa_priv->comp_enabled[comp] = value;
  1535. return 0;
  1536. }
  1537. static int wsa_macro_ear_spkr_pa_gain_get(struct snd_kcontrol *kcontrol,
  1538. struct snd_ctl_elem_value *ucontrol)
  1539. {
  1540. struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
  1541. struct device *wsa_dev = NULL;
  1542. struct wsa_macro_priv *wsa_priv = NULL;
  1543. if (!wsa_macro_get_data(codec, &wsa_dev, &wsa_priv, __func__))
  1544. return -EINVAL;
  1545. ucontrol->value.integer.value[0] = wsa_priv->ear_spkr_gain;
  1546. dev_dbg(codec->dev, "%s: ucontrol->value.integer.value[0] = %ld\n",
  1547. __func__, ucontrol->value.integer.value[0]);
  1548. return 0;
  1549. }
  1550. static int wsa_macro_ear_spkr_pa_gain_put(struct snd_kcontrol *kcontrol,
  1551. struct snd_ctl_elem_value *ucontrol)
  1552. {
  1553. struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
  1554. struct device *wsa_dev = NULL;
  1555. struct wsa_macro_priv *wsa_priv = NULL;
  1556. if (!wsa_macro_get_data(codec, &wsa_dev, &wsa_priv, __func__))
  1557. return -EINVAL;
  1558. wsa_priv->ear_spkr_gain = ucontrol->value.integer.value[0];
  1559. dev_dbg(codec->dev, "%s: gain = %d\n", __func__,
  1560. wsa_priv->ear_spkr_gain);
  1561. return 0;
  1562. }
  1563. static int wsa_macro_spkr_left_boost_stage_get(struct snd_kcontrol *kcontrol,
  1564. struct snd_ctl_elem_value *ucontrol)
  1565. {
  1566. u8 bst_state_max = 0;
  1567. struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
  1568. bst_state_max = snd_soc_read(codec, BOLERO_CDC_WSA_BOOST0_BOOST_CTL);
  1569. bst_state_max = (bst_state_max & 0x0c) >> 2;
  1570. ucontrol->value.integer.value[0] = bst_state_max;
  1571. dev_dbg(codec->dev, "%s: ucontrol->value.integer.value[0] = %ld\n",
  1572. __func__, ucontrol->value.integer.value[0]);
  1573. return 0;
  1574. }
  1575. static int wsa_macro_spkr_left_boost_stage_put(struct snd_kcontrol *kcontrol,
  1576. struct snd_ctl_elem_value *ucontrol)
  1577. {
  1578. u8 bst_state_max;
  1579. struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
  1580. dev_dbg(codec->dev, "%s: ucontrol->value.integer.value[0] = %ld\n",
  1581. __func__, ucontrol->value.integer.value[0]);
  1582. bst_state_max = ucontrol->value.integer.value[0] << 2;
  1583. snd_soc_update_bits(codec, BOLERO_CDC_WSA_BOOST0_BOOST_CTL,
  1584. 0x0c, bst_state_max);
  1585. return 0;
  1586. }
  1587. static int wsa_macro_spkr_right_boost_stage_get(struct snd_kcontrol *kcontrol,
  1588. struct snd_ctl_elem_value *ucontrol)
  1589. {
  1590. u8 bst_state_max = 0;
  1591. struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
  1592. bst_state_max = snd_soc_read(codec, BOLERO_CDC_WSA_BOOST1_BOOST_CTL);
  1593. bst_state_max = (bst_state_max & 0x0c) >> 2;
  1594. ucontrol->value.integer.value[0] = bst_state_max;
  1595. dev_dbg(codec->dev, "%s: ucontrol->value.integer.value[0] = %ld\n",
  1596. __func__, ucontrol->value.integer.value[0]);
  1597. return 0;
  1598. }
  1599. static int wsa_macro_spkr_right_boost_stage_put(struct snd_kcontrol *kcontrol,
  1600. struct snd_ctl_elem_value *ucontrol)
  1601. {
  1602. u8 bst_state_max;
  1603. struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
  1604. dev_dbg(codec->dev, "%s: ucontrol->value.integer.value[0] = %ld\n",
  1605. __func__, ucontrol->value.integer.value[0]);
  1606. bst_state_max = ucontrol->value.integer.value[0] << 2;
  1607. snd_soc_update_bits(codec, BOLERO_CDC_WSA_BOOST1_BOOST_CTL,
  1608. 0x0c, bst_state_max);
  1609. return 0;
  1610. }
  1611. static int wsa_macro_rx_mux_get(struct snd_kcontrol *kcontrol,
  1612. struct snd_ctl_elem_value *ucontrol)
  1613. {
  1614. struct snd_soc_dapm_widget *widget =
  1615. snd_soc_dapm_kcontrol_widget(kcontrol);
  1616. struct snd_soc_codec *codec = snd_soc_dapm_to_codec(widget->dapm);
  1617. struct device *wsa_dev = NULL;
  1618. struct wsa_macro_priv *wsa_priv = NULL;
  1619. if (!wsa_macro_get_data(codec, &wsa_dev, &wsa_priv, __func__))
  1620. return -EINVAL;
  1621. ucontrol->value.integer.value[0] =
  1622. wsa_priv->rx_port_value[widget->shift];
  1623. return 0;
  1624. }
  1625. static int wsa_macro_rx_mux_put(struct snd_kcontrol *kcontrol,
  1626. struct snd_ctl_elem_value *ucontrol)
  1627. {
  1628. struct snd_soc_dapm_widget *widget =
  1629. snd_soc_dapm_kcontrol_widget(kcontrol);
  1630. struct snd_soc_codec *codec = snd_soc_dapm_to_codec(widget->dapm);
  1631. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  1632. struct snd_soc_dapm_update *update = NULL;
  1633. u32 rx_port_value = ucontrol->value.integer.value[0];
  1634. u32 bit_input = 0;
  1635. u32 aif_rst;
  1636. struct device *wsa_dev = NULL;
  1637. struct wsa_macro_priv *wsa_priv = NULL;
  1638. if (!wsa_macro_get_data(codec, &wsa_dev, &wsa_priv, __func__))
  1639. return -EINVAL;
  1640. aif_rst = wsa_priv->rx_port_value[widget->shift];
  1641. if (!rx_port_value) {
  1642. if (aif_rst == 0) {
  1643. dev_err(wsa_dev, "%s: AIF reset already\n", __func__);
  1644. return 0;
  1645. }
  1646. }
  1647. wsa_priv->rx_port_value[widget->shift] = rx_port_value;
  1648. bit_input = widget->shift;
  1649. if (widget->shift >= WSA_MACRO_RX_MIX)
  1650. bit_input %= WSA_MACRO_RX_MIX;
  1651. switch (rx_port_value) {
  1652. case 0:
  1653. clear_bit(bit_input,
  1654. &wsa_priv->active_ch_mask[aif_rst]);
  1655. wsa_priv->active_ch_cnt[aif_rst]--;
  1656. break;
  1657. case 1:
  1658. case 2:
  1659. set_bit(bit_input,
  1660. &wsa_priv->active_ch_mask[rx_port_value]);
  1661. wsa_priv->active_ch_cnt[rx_port_value]++;
  1662. break;
  1663. default:
  1664. dev_err(wsa_dev,
  1665. "%s: Invalid AIF_ID for WSA RX MUX\n", __func__);
  1666. return -EINVAL;
  1667. }
  1668. snd_soc_dapm_mux_update_power(widget->dapm, kcontrol,
  1669. rx_port_value, e, update);
  1670. return 0;
  1671. }
  1672. static int wsa_macro_vbat_bcl_gsm_mode_func_get(struct snd_kcontrol *kcontrol,
  1673. struct snd_ctl_elem_value *ucontrol)
  1674. {
  1675. struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
  1676. ucontrol->value.integer.value[0] =
  1677. ((snd_soc_read(codec, BOLERO_CDC_WSA_VBAT_BCL_VBAT_CFG) & 0x04) ?
  1678. 1 : 0);
  1679. dev_dbg(codec->dev, "%s: value: %lu\n", __func__,
  1680. ucontrol->value.integer.value[0]);
  1681. return 0;
  1682. }
  1683. static int wsa_macro_vbat_bcl_gsm_mode_func_put(struct snd_kcontrol *kcontrol,
  1684. struct snd_ctl_elem_value *ucontrol)
  1685. {
  1686. struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
  1687. dev_dbg(codec->dev, "%s: value: %lu\n", __func__,
  1688. ucontrol->value.integer.value[0]);
  1689. /* Set Vbat register configuration for GSM mode bit based on value */
  1690. if (ucontrol->value.integer.value[0])
  1691. snd_soc_update_bits(codec, BOLERO_CDC_WSA_VBAT_BCL_VBAT_CFG,
  1692. 0x04, 0x04);
  1693. else
  1694. snd_soc_update_bits(codec, BOLERO_CDC_WSA_VBAT_BCL_VBAT_CFG,
  1695. 0x04, 0x00);
  1696. return 0;
  1697. }
  1698. static int wsa_macro_soft_clip_enable_get(struct snd_kcontrol *kcontrol,
  1699. struct snd_ctl_elem_value *ucontrol)
  1700. {
  1701. struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
  1702. struct device *wsa_dev = NULL;
  1703. struct wsa_macro_priv *wsa_priv = NULL;
  1704. int path = ((struct soc_multi_mixer_control *)
  1705. kcontrol->private_value)->shift;
  1706. if (!wsa_macro_get_data(codec, &wsa_dev, &wsa_priv, __func__))
  1707. return -EINVAL;
  1708. ucontrol->value.integer.value[0] = wsa_priv->is_softclip_on[path];
  1709. dev_dbg(codec->dev, "%s: ucontrol->value.integer.value[0] = %ld\n",
  1710. __func__, ucontrol->value.integer.value[0]);
  1711. return 0;
  1712. }
  1713. static int wsa_macro_soft_clip_enable_put(struct snd_kcontrol *kcontrol,
  1714. struct snd_ctl_elem_value *ucontrol)
  1715. {
  1716. struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
  1717. struct device *wsa_dev = NULL;
  1718. struct wsa_macro_priv *wsa_priv = NULL;
  1719. int path = ((struct soc_multi_mixer_control *)
  1720. kcontrol->private_value)->shift;
  1721. if (!wsa_macro_get_data(codec, &wsa_dev, &wsa_priv, __func__))
  1722. return -EINVAL;
  1723. wsa_priv->is_softclip_on[path] = ucontrol->value.integer.value[0];
  1724. dev_dbg(codec->dev, "%s: soft clip enable for %d: %d\n", __func__,
  1725. path, wsa_priv->is_softclip_on[path]);
  1726. return 0;
  1727. }
  1728. static const struct snd_kcontrol_new wsa_macro_snd_controls[] = {
  1729. SOC_ENUM_EXT("EAR SPKR PA Gain", wsa_macro_ear_spkr_pa_gain_enum,
  1730. wsa_macro_ear_spkr_pa_gain_get,
  1731. wsa_macro_ear_spkr_pa_gain_put),
  1732. SOC_ENUM_EXT("SPKR Left Boost Max State",
  1733. wsa_macro_spkr_boost_stage_enum,
  1734. wsa_macro_spkr_left_boost_stage_get,
  1735. wsa_macro_spkr_left_boost_stage_put),
  1736. SOC_ENUM_EXT("SPKR Right Boost Max State",
  1737. wsa_macro_spkr_boost_stage_enum,
  1738. wsa_macro_spkr_right_boost_stage_get,
  1739. wsa_macro_spkr_right_boost_stage_put),
  1740. SOC_ENUM_EXT("GSM mode Enable", wsa_macro_vbat_bcl_gsm_mode_enum,
  1741. wsa_macro_vbat_bcl_gsm_mode_func_get,
  1742. wsa_macro_vbat_bcl_gsm_mode_func_put),
  1743. SOC_SINGLE_EXT("WSA_Softclip0 Enable", SND_SOC_NOPM,
  1744. WSA_MACRO_SOFTCLIP0, 1, 0,
  1745. wsa_macro_soft_clip_enable_get,
  1746. wsa_macro_soft_clip_enable_put),
  1747. SOC_SINGLE_EXT("WSA_Softclip1 Enable", SND_SOC_NOPM,
  1748. WSA_MACRO_SOFTCLIP1, 1, 0,
  1749. wsa_macro_soft_clip_enable_get,
  1750. wsa_macro_soft_clip_enable_put),
  1751. SOC_SINGLE_SX_TLV("WSA_RX0 Digital Volume",
  1752. BOLERO_CDC_WSA_RX0_RX_VOL_CTL,
  1753. 0, -84, 40, digital_gain),
  1754. SOC_SINGLE_SX_TLV("WSA_RX1 Digital Volume",
  1755. BOLERO_CDC_WSA_RX1_RX_VOL_CTL,
  1756. 0, -84, 40, digital_gain),
  1757. SOC_SINGLE_EXT("WSA_COMP1 Switch", SND_SOC_NOPM, WSA_MACRO_COMP1, 1, 0,
  1758. wsa_macro_get_compander, wsa_macro_set_compander),
  1759. SOC_SINGLE_EXT("WSA_COMP2 Switch", SND_SOC_NOPM, WSA_MACRO_COMP2, 1, 0,
  1760. wsa_macro_get_compander, wsa_macro_set_compander),
  1761. SOC_SINGLE_EXT("WSA_RX0 EC_HQ Switch", SND_SOC_NOPM, WSA_MACRO_RX0,
  1762. 1, 0, wsa_macro_get_ec_hq, wsa_macro_set_ec_hq),
  1763. SOC_SINGLE_EXT("WSA_RX1 EC_HQ Switch", SND_SOC_NOPM, WSA_MACRO_RX1,
  1764. 1, 0, wsa_macro_get_ec_hq, wsa_macro_set_ec_hq),
  1765. };
  1766. static const struct soc_enum rx_mux_enum =
  1767. SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(rx_mux_text), rx_mux_text);
  1768. static const struct snd_kcontrol_new rx_mux[WSA_MACRO_RX_MAX] = {
  1769. SOC_DAPM_ENUM_EXT("WSA RX0 Mux", rx_mux_enum,
  1770. wsa_macro_rx_mux_get, wsa_macro_rx_mux_put),
  1771. SOC_DAPM_ENUM_EXT("WSA RX1 Mux", rx_mux_enum,
  1772. wsa_macro_rx_mux_get, wsa_macro_rx_mux_put),
  1773. SOC_DAPM_ENUM_EXT("WSA RX_MIX0 Mux", rx_mux_enum,
  1774. wsa_macro_rx_mux_get, wsa_macro_rx_mux_put),
  1775. SOC_DAPM_ENUM_EXT("WSA RX_MIX1 Mux", rx_mux_enum,
  1776. wsa_macro_rx_mux_get, wsa_macro_rx_mux_put),
  1777. };
  1778. static int wsa_macro_vi_feed_mixer_get(struct snd_kcontrol *kcontrol,
  1779. struct snd_ctl_elem_value *ucontrol)
  1780. {
  1781. struct snd_soc_dapm_widget *widget =
  1782. snd_soc_dapm_kcontrol_widget(kcontrol);
  1783. struct snd_soc_codec *codec = snd_soc_dapm_to_codec(widget->dapm);
  1784. struct soc_multi_mixer_control *mixer =
  1785. ((struct soc_multi_mixer_control *)kcontrol->private_value);
  1786. u32 dai_id = widget->shift;
  1787. u32 spk_tx_id = mixer->shift;
  1788. struct device *wsa_dev = NULL;
  1789. struct wsa_macro_priv *wsa_priv = NULL;
  1790. if (!wsa_macro_get_data(codec, &wsa_dev, &wsa_priv, __func__))
  1791. return -EINVAL;
  1792. if (test_bit(spk_tx_id, &wsa_priv->active_ch_mask[dai_id]))
  1793. ucontrol->value.integer.value[0] = 1;
  1794. else
  1795. ucontrol->value.integer.value[0] = 0;
  1796. return 0;
  1797. }
  1798. static int wsa_macro_vi_feed_mixer_put(struct snd_kcontrol *kcontrol,
  1799. struct snd_ctl_elem_value *ucontrol)
  1800. {
  1801. struct snd_soc_dapm_widget *widget =
  1802. snd_soc_dapm_kcontrol_widget(kcontrol);
  1803. struct snd_soc_codec *codec = snd_soc_dapm_to_codec(widget->dapm);
  1804. struct soc_multi_mixer_control *mixer =
  1805. ((struct soc_multi_mixer_control *)kcontrol->private_value);
  1806. u32 spk_tx_id = mixer->shift;
  1807. u32 enable = ucontrol->value.integer.value[0];
  1808. struct device *wsa_dev = NULL;
  1809. struct wsa_macro_priv *wsa_priv = NULL;
  1810. if (!wsa_macro_get_data(codec, &wsa_dev, &wsa_priv, __func__))
  1811. return -EINVAL;
  1812. wsa_priv->vi_feed_value = ucontrol->value.integer.value[0];
  1813. if (enable) {
  1814. if (spk_tx_id == WSA_MACRO_TX0 &&
  1815. !test_bit(WSA_MACRO_TX0,
  1816. &wsa_priv->active_ch_mask[WSA_MACRO_AIF_VI])) {
  1817. set_bit(WSA_MACRO_TX0,
  1818. &wsa_priv->active_ch_mask[WSA_MACRO_AIF_VI]);
  1819. wsa_priv->active_ch_cnt[WSA_MACRO_AIF_VI]++;
  1820. }
  1821. if (spk_tx_id == WSA_MACRO_TX1 &&
  1822. !test_bit(WSA_MACRO_TX1,
  1823. &wsa_priv->active_ch_mask[WSA_MACRO_AIF_VI])) {
  1824. set_bit(WSA_MACRO_TX1,
  1825. &wsa_priv->active_ch_mask[WSA_MACRO_AIF_VI]);
  1826. wsa_priv->active_ch_cnt[WSA_MACRO_AIF_VI]++;
  1827. }
  1828. } else {
  1829. if (spk_tx_id == WSA_MACRO_TX0 &&
  1830. test_bit(WSA_MACRO_TX0,
  1831. &wsa_priv->active_ch_mask[WSA_MACRO_AIF_VI])) {
  1832. clear_bit(WSA_MACRO_TX0,
  1833. &wsa_priv->active_ch_mask[WSA_MACRO_AIF_VI]);
  1834. wsa_priv->active_ch_cnt[WSA_MACRO_AIF_VI]--;
  1835. }
  1836. if (spk_tx_id == WSA_MACRO_TX1 &&
  1837. test_bit(WSA_MACRO_TX1,
  1838. &wsa_priv->active_ch_mask[WSA_MACRO_AIF_VI])) {
  1839. clear_bit(WSA_MACRO_TX1,
  1840. &wsa_priv->active_ch_mask[WSA_MACRO_AIF_VI]);
  1841. wsa_priv->active_ch_cnt[WSA_MACRO_AIF_VI]--;
  1842. }
  1843. }
  1844. snd_soc_dapm_mixer_update_power(widget->dapm, kcontrol, enable, NULL);
  1845. return 0;
  1846. }
  1847. static const struct snd_kcontrol_new aif_vi_mixer[] = {
  1848. SOC_SINGLE_EXT("WSA_SPKR_VI_1", SND_SOC_NOPM, WSA_MACRO_TX0, 1, 0,
  1849. wsa_macro_vi_feed_mixer_get,
  1850. wsa_macro_vi_feed_mixer_put),
  1851. SOC_SINGLE_EXT("WSA_SPKR_VI_2", SND_SOC_NOPM, WSA_MACRO_TX1, 1, 0,
  1852. wsa_macro_vi_feed_mixer_get,
  1853. wsa_macro_vi_feed_mixer_put),
  1854. };
  1855. static const struct snd_soc_dapm_widget wsa_macro_dapm_widgets[] = {
  1856. SND_SOC_DAPM_AIF_IN("WSA AIF1 PB", "WSA_AIF1 Playback", 0,
  1857. SND_SOC_NOPM, 0, 0),
  1858. SND_SOC_DAPM_AIF_IN("WSA AIF_MIX1 PB", "WSA_AIF_MIX1 Playback", 0,
  1859. SND_SOC_NOPM, 0, 0),
  1860. SND_SOC_DAPM_AIF_OUT_E("WSA AIF_VI", "WSA_AIF_VI Capture", 0,
  1861. SND_SOC_NOPM, WSA_MACRO_AIF_VI, 0,
  1862. wsa_macro_enable_vi_feedback,
  1863. SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
  1864. SND_SOC_DAPM_AIF_OUT("WSA AIF_ECHO", "WSA_AIF_ECHO Capture", 0,
  1865. SND_SOC_NOPM, 0, 0),
  1866. SND_SOC_DAPM_MIXER("WSA_AIF_VI Mixer", SND_SOC_NOPM, WSA_MACRO_AIF_VI,
  1867. 0, aif_vi_mixer, ARRAY_SIZE(aif_vi_mixer)),
  1868. SND_SOC_DAPM_MUX_E("WSA RX_MIX EC0_MUX", SND_SOC_NOPM,
  1869. WSA_MACRO_EC0_MUX, 0,
  1870. &rx_mix_ec0_mux, wsa_macro_enable_echo,
  1871. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  1872. SND_SOC_DAPM_MUX_E("WSA RX_MIX EC1_MUX", SND_SOC_NOPM,
  1873. WSA_MACRO_EC1_MUX, 0,
  1874. &rx_mix_ec1_mux, wsa_macro_enable_echo,
  1875. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  1876. SND_SOC_DAPM_MUX("WSA RX0 MUX", SND_SOC_NOPM, WSA_MACRO_RX0, 0,
  1877. &rx_mux[WSA_MACRO_RX0]),
  1878. SND_SOC_DAPM_MUX("WSA RX1 MUX", SND_SOC_NOPM, WSA_MACRO_RX1, 0,
  1879. &rx_mux[WSA_MACRO_RX1]),
  1880. SND_SOC_DAPM_MUX("WSA RX_MIX0 MUX", SND_SOC_NOPM, WSA_MACRO_RX_MIX0, 0,
  1881. &rx_mux[WSA_MACRO_RX_MIX0]),
  1882. SND_SOC_DAPM_MUX("WSA RX_MIX1 MUX", SND_SOC_NOPM, WSA_MACRO_RX_MIX1, 0,
  1883. &rx_mux[WSA_MACRO_RX_MIX1]),
  1884. SND_SOC_DAPM_MIXER("WSA RX0", SND_SOC_NOPM, 0, 0, NULL, 0),
  1885. SND_SOC_DAPM_MIXER("WSA RX1", SND_SOC_NOPM, 0, 0, NULL, 0),
  1886. SND_SOC_DAPM_MIXER("WSA RX_MIX0", SND_SOC_NOPM, 0, 0, NULL, 0),
  1887. SND_SOC_DAPM_MIXER("WSA RX_MIX1", SND_SOC_NOPM, 0, 0, NULL, 0),
  1888. SND_SOC_DAPM_MUX_E("WSA_RX0 INP0", SND_SOC_NOPM, 0, 0,
  1889. &rx0_prim_inp0_mux, wsa_macro_enable_swr,
  1890. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  1891. SND_SOC_DAPM_MUX_E("WSA_RX0 INP1", SND_SOC_NOPM, 0, 0,
  1892. &rx0_prim_inp1_mux, wsa_macro_enable_swr,
  1893. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  1894. SND_SOC_DAPM_MUX_E("WSA_RX0 INP2", SND_SOC_NOPM, 0, 0,
  1895. &rx0_prim_inp2_mux, wsa_macro_enable_swr,
  1896. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  1897. SND_SOC_DAPM_MUX_E("WSA_RX0 MIX INP", SND_SOC_NOPM, 0, 0,
  1898. &rx0_mix_mux, wsa_macro_enable_mix_path,
  1899. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  1900. SND_SOC_DAPM_MUX_E("WSA_RX1 INP0", SND_SOC_NOPM, 0, 0,
  1901. &rx1_prim_inp0_mux, wsa_macro_enable_swr,
  1902. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  1903. SND_SOC_DAPM_MUX_E("WSA_RX1 INP1", SND_SOC_NOPM, 0, 0,
  1904. &rx1_prim_inp1_mux, wsa_macro_enable_swr,
  1905. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  1906. SND_SOC_DAPM_MUX_E("WSA_RX1 INP2", SND_SOC_NOPM, 0, 0,
  1907. &rx1_prim_inp2_mux, wsa_macro_enable_swr,
  1908. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  1909. SND_SOC_DAPM_MUX_E("WSA_RX1 MIX INP", SND_SOC_NOPM, 0, 0,
  1910. &rx1_mix_mux, wsa_macro_enable_mix_path,
  1911. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  1912. SND_SOC_DAPM_MIXER("WSA_RX INT0 MIX", SND_SOC_NOPM, 0, 0, NULL, 0),
  1913. SND_SOC_DAPM_MIXER("WSA_RX INT1 MIX", SND_SOC_NOPM, 0, 0, NULL, 0),
  1914. SND_SOC_DAPM_MIXER("WSA_RX INT0 SEC MIX", SND_SOC_NOPM, 0, 0, NULL, 0),
  1915. SND_SOC_DAPM_MIXER("WSA_RX INT1 SEC MIX", SND_SOC_NOPM, 0, 0, NULL, 0),
  1916. SND_SOC_DAPM_MUX_E("WSA_RX0 INT0 SIDETONE MIX",
  1917. BOLERO_CDC_WSA_RX0_RX_PATH_CFG1, 4, 0,
  1918. &rx0_sidetone_mix_mux, wsa_macro_enable_swr,
  1919. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  1920. SND_SOC_DAPM_INPUT("WSA SRC0_INP"),
  1921. SND_SOC_DAPM_INPUT("WSA_TX DEC0_INP"),
  1922. SND_SOC_DAPM_INPUT("WSA_TX DEC1_INP"),
  1923. SND_SOC_DAPM_MIXER_E("WSA_RX INT0 INTERP", SND_SOC_NOPM,
  1924. WSA_MACRO_COMP1, 0, NULL, 0, wsa_macro_enable_interpolator,
  1925. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
  1926. SND_SOC_DAPM_POST_PMD),
  1927. SND_SOC_DAPM_MIXER_E("WSA_RX INT1 INTERP", SND_SOC_NOPM,
  1928. WSA_MACRO_COMP2, 0, NULL, 0, wsa_macro_enable_interpolator,
  1929. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
  1930. SND_SOC_DAPM_POST_PMD),
  1931. SND_SOC_DAPM_MIXER_E("WSA_RX INT0 CHAIN", SND_SOC_NOPM, 0, 0,
  1932. NULL, 0, wsa_macro_spk_boost_event,
  1933. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
  1934. SND_SOC_DAPM_POST_PMD),
  1935. SND_SOC_DAPM_MIXER_E("WSA_RX INT1 CHAIN", SND_SOC_NOPM, 0, 0,
  1936. NULL, 0, wsa_macro_spk_boost_event,
  1937. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
  1938. SND_SOC_DAPM_POST_PMD),
  1939. SND_SOC_DAPM_MIXER_E("WSA_RX INT0 VBAT", SND_SOC_NOPM,
  1940. 0, 0, wsa_int0_vbat_mix_switch,
  1941. ARRAY_SIZE(wsa_int0_vbat_mix_switch),
  1942. wsa_macro_enable_vbat,
  1943. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  1944. SND_SOC_DAPM_MIXER_E("WSA_RX INT1 VBAT", SND_SOC_NOPM,
  1945. 0, 0, wsa_int1_vbat_mix_switch,
  1946. ARRAY_SIZE(wsa_int1_vbat_mix_switch),
  1947. wsa_macro_enable_vbat,
  1948. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  1949. SND_SOC_DAPM_INPUT("VIINPUT_WSA"),
  1950. SND_SOC_DAPM_OUTPUT("WSA_SPK1 OUT"),
  1951. SND_SOC_DAPM_OUTPUT("WSA_SPK2 OUT"),
  1952. SND_SOC_DAPM_SUPPLY_S("WSA_MCLK", 0, SND_SOC_NOPM, 0, 0,
  1953. wsa_macro_mclk_event, SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  1954. };
  1955. static const struct snd_soc_dapm_route wsa_audio_map[] = {
  1956. /* VI Feedback */
  1957. {"WSA_AIF_VI Mixer", "WSA_SPKR_VI_1", "VIINPUT_WSA"},
  1958. {"WSA_AIF_VI Mixer", "WSA_SPKR_VI_2", "VIINPUT_WSA"},
  1959. {"WSA AIF_VI", NULL, "WSA_AIF_VI Mixer"},
  1960. {"WSA AIF_VI", NULL, "WSA_MCLK"},
  1961. {"WSA RX_MIX EC0_MUX", "RX_MIX_TX0", "WSA_RX INT0 SEC MIX"},
  1962. {"WSA RX_MIX EC1_MUX", "RX_MIX_TX0", "WSA_RX INT0 SEC MIX"},
  1963. {"WSA RX_MIX EC0_MUX", "RX_MIX_TX1", "WSA_RX INT1 SEC MIX"},
  1964. {"WSA RX_MIX EC1_MUX", "RX_MIX_TX1", "WSA_RX INT1 SEC MIX"},
  1965. {"WSA AIF_ECHO", NULL, "WSA RX_MIX EC0_MUX"},
  1966. {"WSA AIF_ECHO", NULL, "WSA RX_MIX EC1_MUX"},
  1967. {"WSA AIF_ECHO", NULL, "WSA_MCLK"},
  1968. {"WSA AIF1 PB", NULL, "WSA_MCLK"},
  1969. {"WSA AIF_MIX1 PB", NULL, "WSA_MCLK"},
  1970. {"WSA RX0 MUX", "AIF1_PB", "WSA AIF1 PB"},
  1971. {"WSA RX1 MUX", "AIF1_PB", "WSA AIF1 PB"},
  1972. {"WSA RX_MIX0 MUX", "AIF1_PB", "WSA AIF1 PB"},
  1973. {"WSA RX_MIX1 MUX", "AIF1_PB", "WSA AIF1 PB"},
  1974. {"WSA RX0 MUX", "AIF_MIX1_PB", "WSA AIF_MIX1 PB"},
  1975. {"WSA RX1 MUX", "AIF_MIX1_PB", "WSA AIF_MIX1 PB"},
  1976. {"WSA RX_MIX0 MUX", "AIF_MIX1_PB", "WSA AIF_MIX1 PB"},
  1977. {"WSA RX_MIX1 MUX", "AIF_MIX1_PB", "WSA AIF_MIX1 PB"},
  1978. {"WSA RX0", NULL, "WSA RX0 MUX"},
  1979. {"WSA RX1", NULL, "WSA RX1 MUX"},
  1980. {"WSA RX_MIX0", NULL, "WSA RX_MIX0 MUX"},
  1981. {"WSA RX_MIX1", NULL, "WSA RX_MIX1 MUX"},
  1982. {"WSA_RX0 INP0", "RX0", "WSA RX0"},
  1983. {"WSA_RX0 INP0", "RX1", "WSA RX1"},
  1984. {"WSA_RX0 INP0", "RX_MIX0", "WSA RX_MIX0"},
  1985. {"WSA_RX0 INP0", "RX_MIX1", "WSA RX_MIX1"},
  1986. {"WSA_RX0 INP0", "DEC0", "WSA_TX DEC0_INP"},
  1987. {"WSA_RX0 INP0", "DEC1", "WSA_TX DEC1_INP"},
  1988. {"WSA_RX INT0 MIX", NULL, "WSA_RX0 INP0"},
  1989. {"WSA_RX0 INP1", "RX0", "WSA RX0"},
  1990. {"WSA_RX0 INP1", "RX1", "WSA RX1"},
  1991. {"WSA_RX0 INP1", "RX_MIX0", "WSA RX_MIX0"},
  1992. {"WSA_RX0 INP1", "RX_MIX1", "WSA RX_MIX1"},
  1993. {"WSA_RX0 INP1", "DEC0", "WSA_TX DEC0_INP"},
  1994. {"WSA_RX0 INP1", "DEC1", "WSA_TX DEC1_INP"},
  1995. {"WSA_RX INT0 MIX", NULL, "WSA_RX0 INP1"},
  1996. {"WSA_RX0 INP2", "RX0", "WSA RX0"},
  1997. {"WSA_RX0 INP2", "RX1", "WSA RX1"},
  1998. {"WSA_RX0 INP2", "RX_MIX0", "WSA RX_MIX0"},
  1999. {"WSA_RX0 INP2", "RX_MIX1", "WSA RX_MIX1"},
  2000. {"WSA_RX0 INP2", "DEC0", "WSA_TX DEC0_INP"},
  2001. {"WSA_RX0 INP2", "DEC1", "WSA_TX DEC1_INP"},
  2002. {"WSA_RX INT0 MIX", NULL, "WSA_RX0 INP2"},
  2003. {"WSA_RX0 MIX INP", "RX0", "WSA RX0"},
  2004. {"WSA_RX0 MIX INP", "RX1", "WSA RX1"},
  2005. {"WSA_RX0 MIX INP", "RX_MIX0", "WSA RX_MIX0"},
  2006. {"WSA_RX0 MIX INP", "RX_MIX1", "WSA RX_MIX1"},
  2007. {"WSA_RX INT0 SEC MIX", NULL, "WSA_RX0 MIX INP"},
  2008. {"WSA_RX INT0 SEC MIX", NULL, "WSA_RX INT0 MIX"},
  2009. {"WSA_RX INT0 INTERP", NULL, "WSA_RX INT0 SEC MIX"},
  2010. {"WSA_RX0 INT0 SIDETONE MIX", "SRC0", "WSA SRC0_INP"},
  2011. {"WSA_RX INT0 INTERP", NULL, "WSA_RX0 INT0 SIDETONE MIX"},
  2012. {"WSA_RX INT0 CHAIN", NULL, "WSA_RX INT0 INTERP"},
  2013. {"WSA_RX INT0 VBAT", "WSA RX0 VBAT Enable", "WSA_RX INT0 INTERP"},
  2014. {"WSA_RX INT0 CHAIN", NULL, "WSA_RX INT0 VBAT"},
  2015. {"WSA_SPK1 OUT", NULL, "WSA_RX INT0 CHAIN"},
  2016. {"WSA_SPK1 OUT", NULL, "WSA_MCLK"},
  2017. {"WSA_RX1 INP0", "RX0", "WSA RX0"},
  2018. {"WSA_RX1 INP0", "RX1", "WSA RX1"},
  2019. {"WSA_RX1 INP0", "RX_MIX0", "WSA RX_MIX0"},
  2020. {"WSA_RX1 INP0", "RX_MIX1", "WSA RX_MIX1"},
  2021. {"WSA_RX1 INP0", "DEC0", "WSA_TX DEC0_INP"},
  2022. {"WSA_RX1 INP0", "DEC1", "WSA_TX DEC1_INP"},
  2023. {"WSA_RX INT1 MIX", NULL, "WSA_RX1 INP0"},
  2024. {"WSA_RX1 INP1", "RX0", "WSA RX0"},
  2025. {"WSA_RX1 INP1", "RX1", "WSA RX1"},
  2026. {"WSA_RX1 INP1", "RX_MIX0", "WSA RX_MIX0"},
  2027. {"WSA_RX1 INP1", "RX_MIX1", "WSA RX_MIX1"},
  2028. {"WSA_RX1 INP1", "DEC0", "WSA_TX DEC0_INP"},
  2029. {"WSA_RX1 INP1", "DEC1", "WSA_TX DEC1_INP"},
  2030. {"WSA_RX INT1 MIX", NULL, "WSA_RX1 INP1"},
  2031. {"WSA_RX1 INP2", "RX0", "WSA RX0"},
  2032. {"WSA_RX1 INP2", "RX1", "WSA RX1"},
  2033. {"WSA_RX1 INP2", "RX_MIX0", "WSA RX_MIX0"},
  2034. {"WSA_RX1 INP2", "RX_MIX1", "WSA RX_MIX1"},
  2035. {"WSA_RX1 INP2", "DEC0", "WSA_TX DEC0_INP"},
  2036. {"WSA_RX1 INP2", "DEC1", "WSA_TX DEC1_INP"},
  2037. {"WSA_RX INT1 MIX", NULL, "WSA_RX1 INP2"},
  2038. {"WSA_RX1 MIX INP", "RX0", "WSA RX0"},
  2039. {"WSA_RX1 MIX INP", "RX1", "WSA RX1"},
  2040. {"WSA_RX1 MIX INP", "RX_MIX0", "WSA RX_MIX0"},
  2041. {"WSA_RX1 MIX INP", "RX_MIX1", "WSA RX_MIX1"},
  2042. {"WSA_RX INT1 SEC MIX", NULL, "WSA_RX1 MIX INP"},
  2043. {"WSA_RX INT1 SEC MIX", NULL, "WSA_RX INT1 MIX"},
  2044. {"WSA_RX INT1 INTERP", NULL, "WSA_RX INT1 SEC MIX"},
  2045. {"WSA_RX INT1 VBAT", "WSA RX1 VBAT Enable", "WSA_RX INT1 INTERP"},
  2046. {"WSA_RX INT1 CHAIN", NULL, "WSA_RX INT1 VBAT"},
  2047. {"WSA_RX INT1 CHAIN", NULL, "WSA_RX INT1 INTERP"},
  2048. {"WSA_SPK2 OUT", NULL, "WSA_RX INT1 CHAIN"},
  2049. {"WSA_SPK2 OUT", NULL, "WSA_MCLK"},
  2050. };
  2051. static const struct wsa_macro_reg_mask_val wsa_macro_reg_init[] = {
  2052. {BOLERO_CDC_WSA_BOOST0_BOOST_CFG1, 0x3F, 0x12},
  2053. {BOLERO_CDC_WSA_BOOST0_BOOST_CFG2, 0x1C, 0x08},
  2054. {BOLERO_CDC_WSA_COMPANDER0_CTL7, 0x1E, 0x18},
  2055. {BOLERO_CDC_WSA_BOOST1_BOOST_CFG1, 0x3F, 0x12},
  2056. {BOLERO_CDC_WSA_BOOST1_BOOST_CFG2, 0x1C, 0x08},
  2057. {BOLERO_CDC_WSA_COMPANDER1_CTL7, 0x1E, 0x18},
  2058. {BOLERO_CDC_WSA_BOOST0_BOOST_CTL, 0x70, 0x58},
  2059. {BOLERO_CDC_WSA_BOOST1_BOOST_CTL, 0x70, 0x58},
  2060. {BOLERO_CDC_WSA_RX0_RX_PATH_CFG1, 0x08, 0x08},
  2061. {BOLERO_CDC_WSA_RX1_RX_PATH_CFG1, 0x08, 0x08},
  2062. {BOLERO_CDC_WSA_TOP_TOP_CFG1, 0x02, 0x02},
  2063. {BOLERO_CDC_WSA_TOP_TOP_CFG1, 0x01, 0x01},
  2064. {BOLERO_CDC_WSA_TX0_SPKR_PROT_PATH_CFG0, 0x01, 0x01},
  2065. {BOLERO_CDC_WSA_TX1_SPKR_PROT_PATH_CFG0, 0x01, 0x01},
  2066. {BOLERO_CDC_WSA_TX2_SPKR_PROT_PATH_CFG0, 0x01, 0x01},
  2067. {BOLERO_CDC_WSA_TX3_SPKR_PROT_PATH_CFG0, 0x01, 0x01},
  2068. {BOLERO_CDC_WSA_COMPANDER0_CTL3, 0x80, 0x80},
  2069. {BOLERO_CDC_WSA_COMPANDER1_CTL3, 0x80, 0x80},
  2070. {BOLERO_CDC_WSA_COMPANDER0_CTL7, 0x01, 0x01},
  2071. {BOLERO_CDC_WSA_COMPANDER1_CTL7, 0x01, 0x01},
  2072. {BOLERO_CDC_WSA_RX0_RX_PATH_CFG0, 0x01, 0x01},
  2073. {BOLERO_CDC_WSA_RX1_RX_PATH_CFG0, 0x01, 0x01},
  2074. {BOLERO_CDC_WSA_RX0_RX_PATH_MIX_CFG, 0x01, 0x01},
  2075. {BOLERO_CDC_WSA_RX1_RX_PATH_MIX_CFG, 0x01, 0x01},
  2076. };
  2077. static void wsa_macro_init_bcl_pmic_reg(struct snd_soc_codec *codec)
  2078. {
  2079. struct device *wsa_dev = NULL;
  2080. struct wsa_macro_priv *wsa_priv = NULL;
  2081. if (!codec) {
  2082. pr_err("%s: NULL codec pointer!\n", __func__);
  2083. return;
  2084. }
  2085. if (!wsa_macro_get_data(codec, &wsa_dev, &wsa_priv, __func__))
  2086. return;
  2087. switch (wsa_priv->bcl_pmic_params.id) {
  2088. case 0:
  2089. /* Enable ID0 to listen to respective PMIC group interrupts */
  2090. snd_soc_update_bits(codec,
  2091. BOLERO_CDC_WSA_VBAT_BCL_VBAT_DECODE_CTL1, 0x02, 0x02);
  2092. /* Update MC_SID0 */
  2093. snd_soc_update_bits(codec,
  2094. BOLERO_CDC_WSA_VBAT_BCL_VBAT_DECODE_CFG1, 0x0F,
  2095. wsa_priv->bcl_pmic_params.sid);
  2096. /* Update MC_PPID0 */
  2097. snd_soc_update_bits(codec,
  2098. BOLERO_CDC_WSA_VBAT_BCL_VBAT_DECODE_CFG2, 0xFF,
  2099. wsa_priv->bcl_pmic_params.ppid);
  2100. break;
  2101. case 1:
  2102. /* Enable ID1 to listen to respective PMIC group interrupts */
  2103. snd_soc_update_bits(codec,
  2104. BOLERO_CDC_WSA_VBAT_BCL_VBAT_DECODE_CTL1, 0x01, 0x01);
  2105. /* Update MC_SID1 */
  2106. snd_soc_update_bits(codec,
  2107. BOLERO_CDC_WSA_VBAT_BCL_VBAT_DECODE_CFG3, 0x0F,
  2108. wsa_priv->bcl_pmic_params.sid);
  2109. /* Update MC_PPID1 */
  2110. snd_soc_update_bits(codec,
  2111. BOLERO_CDC_WSA_VBAT_BCL_VBAT_DECODE_CFG4, 0xFF,
  2112. wsa_priv->bcl_pmic_params.ppid);
  2113. break;
  2114. default:
  2115. dev_err(wsa_dev, "%s: PMIC ID is invalid %d\n",
  2116. __func__, wsa_priv->bcl_pmic_params.id);
  2117. break;
  2118. }
  2119. }
  2120. static void wsa_macro_init_reg(struct snd_soc_codec *codec)
  2121. {
  2122. int i;
  2123. for (i = 0; i < ARRAY_SIZE(wsa_macro_reg_init); i++)
  2124. snd_soc_update_bits(codec,
  2125. wsa_macro_reg_init[i].reg,
  2126. wsa_macro_reg_init[i].mask,
  2127. wsa_macro_reg_init[i].val);
  2128. wsa_macro_init_bcl_pmic_reg(codec);
  2129. }
  2130. static int wsa_swrm_clock(void *handle, bool enable)
  2131. {
  2132. struct wsa_macro_priv *wsa_priv = (struct wsa_macro_priv *) handle;
  2133. struct regmap *regmap = dev_get_regmap(wsa_priv->dev->parent, NULL);
  2134. if (regmap == NULL) {
  2135. dev_err(wsa_priv->dev, "%s: regmap is NULL\n", __func__);
  2136. return -EINVAL;
  2137. }
  2138. mutex_lock(&wsa_priv->swr_clk_lock);
  2139. dev_dbg(wsa_priv->dev, "%s: swrm clock %s\n",
  2140. __func__, (enable ? "enable" : "disable"));
  2141. if (enable) {
  2142. wsa_priv->swr_clk_users++;
  2143. if (wsa_priv->swr_clk_users == 1) {
  2144. wsa_macro_mclk_enable(wsa_priv, 1, true);
  2145. regmap_update_bits(regmap,
  2146. BOLERO_CDC_WSA_CLK_RST_CTRL_SWR_CONTROL,
  2147. 0x01, 0x01);
  2148. regmap_update_bits(regmap,
  2149. BOLERO_CDC_WSA_CLK_RST_CTRL_SWR_CONTROL,
  2150. 0x1C, 0x0C);
  2151. msm_cdc_pinctrl_select_active_state(
  2152. wsa_priv->wsa_swr_gpio_p);
  2153. }
  2154. } else {
  2155. wsa_priv->swr_clk_users--;
  2156. if (wsa_priv->swr_clk_users == 0) {
  2157. regmap_update_bits(regmap,
  2158. BOLERO_CDC_WSA_CLK_RST_CTRL_SWR_CONTROL,
  2159. 0x01, 0x00);
  2160. msm_cdc_pinctrl_select_sleep_state(
  2161. wsa_priv->wsa_swr_gpio_p);
  2162. wsa_macro_mclk_enable(wsa_priv, 0, true);
  2163. }
  2164. }
  2165. dev_dbg(wsa_priv->dev, "%s: swrm clock users %d\n",
  2166. __func__, wsa_priv->swr_clk_users);
  2167. mutex_unlock(&wsa_priv->swr_clk_lock);
  2168. return 0;
  2169. }
  2170. static int wsa_macro_init(struct snd_soc_codec *codec)
  2171. {
  2172. struct snd_soc_dapm_context *dapm = snd_soc_codec_get_dapm(codec);
  2173. int ret;
  2174. struct device *wsa_dev = NULL;
  2175. struct wsa_macro_priv *wsa_priv = NULL;
  2176. wsa_dev = bolero_get_device_ptr(codec->dev, WSA_MACRO);
  2177. if (!wsa_dev) {
  2178. dev_err(codec->dev,
  2179. "%s: null device for macro!\n", __func__);
  2180. return -EINVAL;
  2181. }
  2182. wsa_priv = dev_get_drvdata(wsa_dev);
  2183. if (!wsa_priv) {
  2184. dev_err(codec->dev,
  2185. "%s: priv is null for macro!\n", __func__);
  2186. return -EINVAL;
  2187. }
  2188. ret = snd_soc_dapm_new_controls(dapm, wsa_macro_dapm_widgets,
  2189. ARRAY_SIZE(wsa_macro_dapm_widgets));
  2190. if (ret < 0) {
  2191. dev_err(wsa_dev, "%s: Failed to add controls\n", __func__);
  2192. return ret;
  2193. }
  2194. ret = snd_soc_dapm_add_routes(dapm, wsa_audio_map,
  2195. ARRAY_SIZE(wsa_audio_map));
  2196. if (ret < 0) {
  2197. dev_err(wsa_dev, "%s: Failed to add routes\n", __func__);
  2198. return ret;
  2199. }
  2200. ret = snd_soc_dapm_new_widgets(dapm->card);
  2201. if (ret < 0) {
  2202. dev_err(wsa_dev, "%s: Failed to add widgets\n", __func__);
  2203. return ret;
  2204. }
  2205. ret = snd_soc_add_codec_controls(codec, wsa_macro_snd_controls,
  2206. ARRAY_SIZE(wsa_macro_snd_controls));
  2207. if (ret < 0) {
  2208. dev_err(wsa_dev, "%s: Failed to add snd_ctls\n", __func__);
  2209. return ret;
  2210. }
  2211. snd_soc_dapm_ignore_suspend(dapm, "WSA_AIF1 Playback");
  2212. snd_soc_dapm_ignore_suspend(dapm, "WSA_AIF_MIX1 Playback");
  2213. snd_soc_dapm_ignore_suspend(dapm, "WSA_AIF_VI Capture");
  2214. snd_soc_dapm_ignore_suspend(dapm, "WSA_AIF_ECHO Capture");
  2215. snd_soc_dapm_ignore_suspend(dapm, "WSA_SPK1 OUT");
  2216. snd_soc_dapm_ignore_suspend(dapm, "WSA_SPK2 OUT");
  2217. snd_soc_dapm_ignore_suspend(dapm, "VIINPUT_WSA");
  2218. snd_soc_dapm_ignore_suspend(dapm, "WSA SRC0_INP");
  2219. snd_soc_dapm_ignore_suspend(dapm, "WSA_TX DEC0_INP");
  2220. snd_soc_dapm_ignore_suspend(dapm, "WSA_TX DEC1_INP");
  2221. snd_soc_dapm_sync(dapm);
  2222. wsa_priv->codec = codec;
  2223. wsa_priv->spkr_gain_offset = WSA_MACRO_GAIN_OFFSET_0_DB;
  2224. wsa_macro_init_reg(codec);
  2225. return 0;
  2226. }
  2227. static int wsa_macro_deinit(struct snd_soc_codec *codec)
  2228. {
  2229. struct device *wsa_dev = NULL;
  2230. struct wsa_macro_priv *wsa_priv = NULL;
  2231. if (!wsa_macro_get_data(codec, &wsa_dev, &wsa_priv, __func__))
  2232. return -EINVAL;
  2233. wsa_priv->codec = NULL;
  2234. return 0;
  2235. }
  2236. static void wsa_macro_add_child_devices(struct work_struct *work)
  2237. {
  2238. struct wsa_macro_priv *wsa_priv;
  2239. struct platform_device *pdev;
  2240. struct device_node *node;
  2241. struct wsa_macro_swr_ctrl_data *swr_ctrl_data = NULL, *temp;
  2242. int ret;
  2243. u16 count = 0, ctrl_num = 0;
  2244. struct wsa_macro_swr_ctrl_platform_data *platdata;
  2245. char plat_dev_name[WSA_MACRO_SWR_STRING_LEN];
  2246. wsa_priv = container_of(work, struct wsa_macro_priv,
  2247. wsa_macro_add_child_devices_work);
  2248. if (!wsa_priv) {
  2249. pr_err("%s: Memory for wsa_priv does not exist\n",
  2250. __func__);
  2251. return;
  2252. }
  2253. if (!wsa_priv->dev || !wsa_priv->dev->of_node) {
  2254. dev_err(wsa_priv->dev,
  2255. "%s: DT node for wsa_priv does not exist\n", __func__);
  2256. return;
  2257. }
  2258. platdata = &wsa_priv->swr_plat_data;
  2259. wsa_priv->child_count = 0;
  2260. for_each_available_child_of_node(wsa_priv->dev->of_node, node) {
  2261. if (strnstr(node->name, "wsa_swr_master",
  2262. strlen("wsa_swr_master")) != NULL)
  2263. strlcpy(plat_dev_name, "wsa_swr_ctrl",
  2264. (WSA_MACRO_SWR_STRING_LEN - 1));
  2265. else if (strnstr(node->name, "msm_cdc_pinctrl",
  2266. strlen("msm_cdc_pinctrl")) != NULL)
  2267. strlcpy(plat_dev_name, node->name,
  2268. (WSA_MACRO_SWR_STRING_LEN - 1));
  2269. else
  2270. continue;
  2271. pdev = platform_device_alloc(plat_dev_name, -1);
  2272. if (!pdev) {
  2273. dev_err(wsa_priv->dev, "%s: pdev memory alloc failed\n",
  2274. __func__);
  2275. ret = -ENOMEM;
  2276. goto err;
  2277. }
  2278. pdev->dev.parent = wsa_priv->dev;
  2279. pdev->dev.of_node = node;
  2280. if (strnstr(node->name, "wsa_swr_master",
  2281. strlen("wsa_swr_master")) != NULL) {
  2282. ret = platform_device_add_data(pdev, platdata,
  2283. sizeof(*platdata));
  2284. if (ret) {
  2285. dev_err(&pdev->dev,
  2286. "%s: cannot add plat data ctrl:%d\n",
  2287. __func__, ctrl_num);
  2288. goto fail_pdev_add;
  2289. }
  2290. }
  2291. ret = platform_device_add(pdev);
  2292. if (ret) {
  2293. dev_err(&pdev->dev,
  2294. "%s: Cannot add platform device\n",
  2295. __func__);
  2296. goto fail_pdev_add;
  2297. }
  2298. if (!strcmp(node->name, "wsa_swr_master")) {
  2299. temp = krealloc(swr_ctrl_data,
  2300. (ctrl_num + 1) * sizeof(
  2301. struct wsa_macro_swr_ctrl_data),
  2302. GFP_KERNEL);
  2303. if (!temp) {
  2304. dev_err(&pdev->dev, "out of memory\n");
  2305. ret = -ENOMEM;
  2306. goto err;
  2307. }
  2308. swr_ctrl_data = temp;
  2309. swr_ctrl_data[ctrl_num].wsa_swr_pdev = pdev;
  2310. ctrl_num++;
  2311. dev_dbg(&pdev->dev,
  2312. "%s: Added soundwire ctrl device(s)\n",
  2313. __func__);
  2314. wsa_priv->swr_ctrl_data = swr_ctrl_data;
  2315. }
  2316. if (wsa_priv->child_count < WSA_MACRO_CHILD_DEVICES_MAX)
  2317. wsa_priv->pdev_child_devices[
  2318. wsa_priv->child_count++] = pdev;
  2319. else
  2320. goto err;
  2321. }
  2322. return;
  2323. fail_pdev_add:
  2324. for (count = 0; count < wsa_priv->child_count; count++)
  2325. platform_device_put(wsa_priv->pdev_child_devices[count]);
  2326. err:
  2327. return;
  2328. }
  2329. static void wsa_macro_init_ops(struct macro_ops *ops,
  2330. char __iomem *wsa_io_base)
  2331. {
  2332. memset(ops, 0, sizeof(struct macro_ops));
  2333. ops->init = wsa_macro_init;
  2334. ops->exit = wsa_macro_deinit;
  2335. ops->io_base = wsa_io_base;
  2336. ops->dai_ptr = wsa_macro_dai;
  2337. ops->num_dais = ARRAY_SIZE(wsa_macro_dai);
  2338. ops->mclk_fn = wsa_macro_mclk_ctrl;
  2339. ops->event_handler = wsa_macro_event_handler;
  2340. }
  2341. static int wsa_macro_probe(struct platform_device *pdev)
  2342. {
  2343. struct macro_ops ops;
  2344. struct wsa_macro_priv *wsa_priv;
  2345. u32 wsa_base_addr;
  2346. char __iomem *wsa_io_base;
  2347. int ret = 0;
  2348. struct clk *wsa_core_clk, *wsa_npl_clk;
  2349. u8 bcl_pmic_params[3];
  2350. wsa_priv = devm_kzalloc(&pdev->dev, sizeof(struct wsa_macro_priv),
  2351. GFP_KERNEL);
  2352. if (!wsa_priv)
  2353. return -ENOMEM;
  2354. wsa_priv->dev = &pdev->dev;
  2355. ret = of_property_read_u32(pdev->dev.of_node, "reg",
  2356. &wsa_base_addr);
  2357. if (ret) {
  2358. dev_err(&pdev->dev, "%s: could not find %s entry in dt\n",
  2359. __func__, "reg");
  2360. return ret;
  2361. }
  2362. wsa_priv->wsa_swr_gpio_p = of_parse_phandle(pdev->dev.of_node,
  2363. "qcom,wsa-swr-gpios", 0);
  2364. if (!wsa_priv->wsa_swr_gpio_p) {
  2365. dev_err(&pdev->dev, "%s: swr_gpios handle not provided!\n",
  2366. __func__);
  2367. return -EINVAL;
  2368. }
  2369. wsa_io_base = devm_ioremap(&pdev->dev,
  2370. wsa_base_addr, WSA_MACRO_MAX_OFFSET);
  2371. if (!wsa_io_base) {
  2372. dev_err(&pdev->dev, "%s: ioremap failed\n", __func__);
  2373. return -EINVAL;
  2374. }
  2375. wsa_priv->wsa_io_base = wsa_io_base;
  2376. INIT_WORK(&wsa_priv->wsa_macro_add_child_devices_work,
  2377. wsa_macro_add_child_devices);
  2378. wsa_priv->swr_plat_data.handle = (void *) wsa_priv;
  2379. wsa_priv->swr_plat_data.read = NULL;
  2380. wsa_priv->swr_plat_data.write = NULL;
  2381. wsa_priv->swr_plat_data.bulk_write = NULL;
  2382. wsa_priv->swr_plat_data.clk = wsa_swrm_clock;
  2383. wsa_priv->swr_plat_data.handle_irq = NULL;
  2384. /* Register MCLK for wsa macro */
  2385. wsa_core_clk = devm_clk_get(&pdev->dev, "wsa_core_clk");
  2386. if (IS_ERR(wsa_core_clk)) {
  2387. ret = PTR_ERR(wsa_core_clk);
  2388. dev_err(&pdev->dev, "%s: clk get %s failed\n",
  2389. __func__, "wsa_core_clk");
  2390. return ret;
  2391. }
  2392. wsa_priv->wsa_core_clk = wsa_core_clk;
  2393. /* Register npl clk for soundwire */
  2394. wsa_npl_clk = devm_clk_get(&pdev->dev, "wsa_npl_clk");
  2395. if (IS_ERR(wsa_npl_clk)) {
  2396. ret = PTR_ERR(wsa_npl_clk);
  2397. dev_err(&pdev->dev, "%s: clk get %s failed\n",
  2398. __func__, "wsa_npl_clk");
  2399. return ret;
  2400. }
  2401. wsa_priv->wsa_npl_clk = wsa_npl_clk;
  2402. ret = of_property_read_u8_array(pdev->dev.of_node,
  2403. "qcom,wsa-bcl-pmic-params", bcl_pmic_params,
  2404. sizeof(bcl_pmic_params));
  2405. if (ret) {
  2406. dev_dbg(&pdev->dev, "%s: could not find %s entry in dt\n",
  2407. __func__, "qcom,wsa-bcl-pmic-params");
  2408. } else {
  2409. wsa_priv->bcl_pmic_params.id = bcl_pmic_params[0];
  2410. wsa_priv->bcl_pmic_params.sid = bcl_pmic_params[1];
  2411. wsa_priv->bcl_pmic_params.ppid = bcl_pmic_params[2];
  2412. }
  2413. dev_set_drvdata(&pdev->dev, wsa_priv);
  2414. mutex_init(&wsa_priv->mclk_lock);
  2415. mutex_init(&wsa_priv->swr_clk_lock);
  2416. wsa_macro_init_ops(&ops, wsa_io_base);
  2417. ret = bolero_register_macro(&pdev->dev, WSA_MACRO, &ops);
  2418. if (ret < 0) {
  2419. dev_err(&pdev->dev, "%s: register macro failed\n", __func__);
  2420. goto reg_macro_fail;
  2421. }
  2422. schedule_work(&wsa_priv->wsa_macro_add_child_devices_work);
  2423. return ret;
  2424. reg_macro_fail:
  2425. mutex_destroy(&wsa_priv->mclk_lock);
  2426. mutex_destroy(&wsa_priv->swr_clk_lock);
  2427. return ret;
  2428. }
  2429. static int wsa_macro_remove(struct platform_device *pdev)
  2430. {
  2431. struct wsa_macro_priv *wsa_priv;
  2432. u16 count = 0;
  2433. wsa_priv = dev_get_drvdata(&pdev->dev);
  2434. if (!wsa_priv)
  2435. return -EINVAL;
  2436. for (count = 0; count < wsa_priv->child_count &&
  2437. count < WSA_MACRO_CHILD_DEVICES_MAX; count++)
  2438. platform_device_unregister(wsa_priv->pdev_child_devices[count]);
  2439. bolero_unregister_macro(&pdev->dev, WSA_MACRO);
  2440. mutex_destroy(&wsa_priv->mclk_lock);
  2441. mutex_destroy(&wsa_priv->swr_clk_lock);
  2442. return 0;
  2443. }
  2444. static const struct of_device_id wsa_macro_dt_match[] = {
  2445. {.compatible = "qcom,wsa-macro"},
  2446. {}
  2447. };
  2448. static struct platform_driver wsa_macro_driver = {
  2449. .driver = {
  2450. .name = "wsa_macro",
  2451. .owner = THIS_MODULE,
  2452. .of_match_table = wsa_macro_dt_match,
  2453. },
  2454. .probe = wsa_macro_probe,
  2455. .remove = wsa_macro_remove,
  2456. };
  2457. module_platform_driver(wsa_macro_driver);
  2458. MODULE_DESCRIPTION("WSA macro driver");
  2459. MODULE_LICENSE("GPL v2");