msm_sdw_cdc.c 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084
  1. /* Copyright (c) 2016-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/device.h>
  13. #include <linux/module.h>
  14. #include <linux/io.h>
  15. #include <linux/init.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/printk.h>
  18. #include <linux/debugfs.h>
  19. #include <linux/bitops.h>
  20. #include <linux/regmap.h>
  21. #include <linux/delay.h>
  22. #include <linux/kernel.h>
  23. #include <sound/pcm.h>
  24. #include <sound/pcm_params.h>
  25. #include <sound/soc.h>
  26. #include <sound/soc-dapm.h>
  27. #include <sound/tlv.h>
  28. #include <ipc/apr.h>
  29. #include <soc/swr-wcd.h>
  30. #include <dsp/audio_notifier.h>
  31. #include <dsp/apr_audio-v2.h>
  32. #include <dsp/q6core.h>
  33. #include "msm_sdw.h"
  34. #include "msm_sdw_registers.h"
  35. #include "../msm-cdc-pinctrl.h"
  36. #define MSM_SDW_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000 |\
  37. SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_48000)
  38. #define MSM_SDW_FORMATS (SNDRV_PCM_FMTBIT_S16_LE |\
  39. SNDRV_PCM_FMTBIT_S24_LE |\
  40. SNDRV_PCM_FMTBIT_S24_3LE)
  41. #define MSM_SDW_STRING_LEN 80
  42. #define INT_MCLK1_FREQ 9600000
  43. #define SDW_NPL_FREQ 153600000
  44. #define MSM_SDW_VERSION_1_0 0x0001
  45. #define MSM_SDW_VERSION_ENTRY_SIZE 32
  46. /*
  47. * 200 Milliseconds sufficient for DSP bring up in the modem
  48. * after Sub System Restart
  49. */
  50. #define ADSP_STATE_READY_TIMEOUT_MS 200
  51. static const DECLARE_TLV_DB_SCALE(digital_gain, 0, 1, 0);
  52. static struct snd_soc_dai_driver msm_sdw_dai[];
  53. static bool skip_irq = true;
  54. static int msm_sdw_config_ear_spkr_gain(struct snd_soc_codec *codec,
  55. int event, int gain_reg);
  56. static int msm_sdw_config_compander(struct snd_soc_codec *, int, int);
  57. static int msm_sdw_mclk_enable(struct msm_sdw_priv *msm_sdw,
  58. int mclk_enable, bool dapm);
  59. static int msm_int_enable_sdw_cdc_clk(struct msm_sdw_priv *msm_sdw,
  60. int enable, bool dapm);
  61. enum {
  62. VI_SENSE_1,
  63. VI_SENSE_2,
  64. };
  65. enum {
  66. AIF1_SDW_PB = 0,
  67. AIF1_SDW_VIFEED,
  68. NUM_CODEC_DAIS,
  69. };
  70. static const struct msm_sdw_reg_mask_val msm_sdw_spkr_default[] = {
  71. {MSM_SDW_COMPANDER7_CTL3, 0x80, 0x80},
  72. {MSM_SDW_COMPANDER8_CTL3, 0x80, 0x80},
  73. {MSM_SDW_COMPANDER7_CTL7, 0x01, 0x01},
  74. {MSM_SDW_COMPANDER8_CTL7, 0x01, 0x01},
  75. {MSM_SDW_BOOST0_BOOST_CTL, 0x7C, 0x58},
  76. {MSM_SDW_BOOST1_BOOST_CTL, 0x7C, 0x58},
  77. };
  78. static const struct msm_sdw_reg_mask_val msm_sdw_spkr_mode1[] = {
  79. {MSM_SDW_COMPANDER7_CTL3, 0x80, 0x00},
  80. {MSM_SDW_COMPANDER8_CTL3, 0x80, 0x00},
  81. {MSM_SDW_COMPANDER7_CTL7, 0x01, 0x00},
  82. {MSM_SDW_COMPANDER8_CTL7, 0x01, 0x00},
  83. {MSM_SDW_BOOST0_BOOST_CTL, 0x7C, 0x44},
  84. {MSM_SDW_BOOST1_BOOST_CTL, 0x7C, 0x44},
  85. };
  86. /**
  87. * msm_sdw_set_spkr_gain_offset - offset the speaker path
  88. * gain with the given offset value.
  89. *
  90. * @codec: codec instance
  91. * @offset: Indicates speaker path gain offset value.
  92. *
  93. * Returns 0 on success or -EINVAL on error.
  94. */
  95. int msm_sdw_set_spkr_gain_offset(struct snd_soc_codec *codec, int offset)
  96. {
  97. struct msm_sdw_priv *priv;
  98. if (!codec) {
  99. pr_err("%s: NULL codec pointer!\n", __func__);
  100. return -EINVAL;
  101. }
  102. priv = snd_soc_codec_get_drvdata(codec);
  103. if (!priv)
  104. return -EINVAL;
  105. priv->spkr_gain_offset = offset;
  106. return 0;
  107. }
  108. EXPORT_SYMBOL(msm_sdw_set_spkr_gain_offset);
  109. /**
  110. * msm_sdw_set_spkr_mode - Configures speaker compander and smartboost
  111. * settings based on speaker mode.
  112. *
  113. * @codec: codec instance
  114. * @mode: Indicates speaker configuration mode.
  115. *
  116. * Returns 0 on success or -EINVAL on error.
  117. */
  118. int msm_sdw_set_spkr_mode(struct snd_soc_codec *codec, int mode)
  119. {
  120. struct msm_sdw_priv *priv;
  121. int i;
  122. const struct msm_sdw_reg_mask_val *regs;
  123. int size;
  124. if (!codec) {
  125. pr_err("%s: NULL codec pointer!\n", __func__);
  126. return -EINVAL;
  127. }
  128. priv = snd_soc_codec_get_drvdata(codec);
  129. if (!priv)
  130. return -EINVAL;
  131. switch (mode) {
  132. case SPKR_MODE_1:
  133. regs = msm_sdw_spkr_mode1;
  134. size = ARRAY_SIZE(msm_sdw_spkr_mode1);
  135. break;
  136. default:
  137. regs = msm_sdw_spkr_default;
  138. size = ARRAY_SIZE(msm_sdw_spkr_default);
  139. break;
  140. }
  141. priv->spkr_mode = mode;
  142. for (i = 0; i < size; i++)
  143. snd_soc_update_bits(codec, regs[i].reg,
  144. regs[i].mask, regs[i].val);
  145. return 0;
  146. }
  147. EXPORT_SYMBOL(msm_sdw_set_spkr_mode);
  148. static int msm_enable_sdw_npl_clk(struct msm_sdw_priv *msm_sdw, int enable)
  149. {
  150. int ret = 0;
  151. dev_dbg(msm_sdw->dev, "%s: enable %d\n", __func__, enable);
  152. mutex_lock(&msm_sdw->sdw_npl_clk_mutex);
  153. if (enable) {
  154. if (msm_sdw->sdw_npl_clk_enabled == false) {
  155. msm_sdw->sdw_npl_clk.enable = 1;
  156. ret = afe_set_lpass_clock_v2(
  157. AFE_PORT_ID_INT4_MI2S_RX,
  158. &msm_sdw->sdw_npl_clk);
  159. if (ret < 0) {
  160. dev_err(msm_sdw->dev,
  161. "%s: failed to enable SDW NPL CLK\n",
  162. __func__);
  163. mutex_unlock(&msm_sdw->sdw_npl_clk_mutex);
  164. return ret;
  165. }
  166. dev_dbg(msm_sdw->dev, "enabled sdw npl clk\n");
  167. msm_sdw->sdw_npl_clk_enabled = true;
  168. }
  169. } else {
  170. if (msm_sdw->sdw_npl_clk_enabled == true) {
  171. msm_sdw->sdw_npl_clk.enable = 0;
  172. ret = afe_set_lpass_clock_v2(
  173. AFE_PORT_ID_INT4_MI2S_RX,
  174. &msm_sdw->sdw_npl_clk);
  175. if (ret < 0)
  176. dev_err(msm_sdw->dev,
  177. "%s: failed to disable SDW NPL CLK\n",
  178. __func__);
  179. msm_sdw->sdw_npl_clk_enabled = false;
  180. }
  181. }
  182. mutex_unlock(&msm_sdw->sdw_npl_clk_mutex);
  183. return ret;
  184. }
  185. static int msm_int_enable_sdw_cdc_clk(struct msm_sdw_priv *msm_sdw,
  186. int enable, bool dapm)
  187. {
  188. int ret = 0;
  189. mutex_lock(&msm_sdw->cdc_int_mclk1_mutex);
  190. dev_dbg(msm_sdw->dev, "%s: enable %d mclk1 ref counter %d\n",
  191. __func__, enable, msm_sdw->int_mclk1_rsc_ref);
  192. if (enable) {
  193. if (msm_sdw->int_mclk1_rsc_ref == 0) {
  194. cancel_delayed_work_sync(
  195. &msm_sdw->disable_int_mclk1_work);
  196. if (msm_sdw->int_mclk1_enabled == false) {
  197. msm_sdw->sdw_cdc_core_clk.enable = 1;
  198. ret = afe_set_lpass_clock_v2(
  199. AFE_PORT_ID_INT4_MI2S_RX,
  200. &msm_sdw->sdw_cdc_core_clk);
  201. if (ret < 0) {
  202. dev_err(msm_sdw->dev,
  203. "%s: failed to enable SDW MCLK\n",
  204. __func__);
  205. goto rtn;
  206. }
  207. dev_dbg(msm_sdw->dev,
  208. "enabled sdw codec core mclk\n");
  209. msm_sdw->int_mclk1_enabled = true;
  210. }
  211. }
  212. msm_sdw->int_mclk1_rsc_ref++;
  213. } else {
  214. cancel_delayed_work_sync(&msm_sdw->disable_int_mclk1_work);
  215. if (msm_sdw->int_mclk1_rsc_ref > 0) {
  216. msm_sdw->int_mclk1_rsc_ref--;
  217. dev_dbg(msm_sdw->dev,
  218. "%s: decrementing mclk_res_ref %d\n",
  219. __func__, msm_sdw->int_mclk1_rsc_ref);
  220. }
  221. if (msm_sdw->int_mclk1_enabled == true &&
  222. msm_sdw->int_mclk1_rsc_ref == 0) {
  223. msm_sdw->sdw_cdc_core_clk.enable = 0;
  224. ret = afe_set_lpass_clock_v2(
  225. AFE_PORT_ID_INT4_MI2S_RX,
  226. &msm_sdw->sdw_cdc_core_clk);
  227. if (ret < 0)
  228. dev_err(msm_sdw->dev,
  229. "%s: failed to disable SDW MCLK\n",
  230. __func__);
  231. msm_sdw->int_mclk1_enabled = false;
  232. }
  233. }
  234. rtn:
  235. mutex_unlock(&msm_sdw->cdc_int_mclk1_mutex);
  236. return ret;
  237. }
  238. EXPORT_SYMBOL(msm_int_enable_sdw_cdc_clk);
  239. static void msm_disable_int_mclk1(struct work_struct *work)
  240. {
  241. struct msm_sdw_priv *msm_sdw = NULL;
  242. struct delayed_work *dwork;
  243. int ret = 0;
  244. dwork = to_delayed_work(work);
  245. msm_sdw = container_of(dwork, struct msm_sdw_priv,
  246. disable_int_mclk1_work);
  247. mutex_lock(&msm_sdw->cdc_int_mclk1_mutex);
  248. dev_dbg(msm_sdw->dev, "%s: mclk1_enabled %d mclk1_rsc_ref %d\n",
  249. __func__, msm_sdw->int_mclk1_enabled,
  250. msm_sdw->int_mclk1_rsc_ref);
  251. if (msm_sdw->int_mclk1_enabled == true
  252. && msm_sdw->int_mclk1_rsc_ref == 0) {
  253. dev_dbg(msm_sdw->dev, "Disable the mclk1\n");
  254. msm_sdw->sdw_cdc_core_clk.enable = 0;
  255. ret = afe_set_lpass_clock_v2(
  256. AFE_PORT_ID_INT4_MI2S_RX,
  257. &msm_sdw->sdw_cdc_core_clk);
  258. if (ret < 0)
  259. dev_err(msm_sdw->dev,
  260. "%s failed to disable the MCLK1\n",
  261. __func__);
  262. msm_sdw->int_mclk1_enabled = false;
  263. }
  264. mutex_unlock(&msm_sdw->cdc_int_mclk1_mutex);
  265. }
  266. static int msm_int_mclk1_event(struct snd_soc_dapm_widget *w,
  267. struct snd_kcontrol *kcontrol, int event)
  268. {
  269. struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
  270. struct msm_sdw_priv *msm_sdw = snd_soc_codec_get_drvdata(codec);
  271. int ret = 0;
  272. dev_dbg(msm_sdw->dev, "%s: event = %d\n", __func__, event);
  273. switch (event) {
  274. case SND_SOC_DAPM_PRE_PMU:
  275. /* enable the codec mclk config */
  276. msm_int_enable_sdw_cdc_clk(msm_sdw, 1, true);
  277. msm_sdw_mclk_enable(msm_sdw, 1, true);
  278. break;
  279. case SND_SOC_DAPM_POST_PMD:
  280. /* disable the codec mclk config */
  281. msm_sdw_mclk_enable(msm_sdw, 0, true);
  282. msm_int_enable_sdw_cdc_clk(msm_sdw, 0, true);
  283. break;
  284. default:
  285. dev_err(msm_sdw->dev,
  286. "%s: invalid DAPM event %d\n", __func__, event);
  287. ret = -EINVAL;
  288. }
  289. return ret;
  290. }
  291. static int msm_sdw_ahb_write_device(struct msm_sdw_priv *msm_sdw,
  292. u16 reg, u8 *value)
  293. {
  294. u32 temp = (u32)(*value) & 0x000000FF;
  295. if (!msm_sdw->dev_up) {
  296. dev_err_ratelimited(msm_sdw->dev, "%s: q6 not ready\n",
  297. __func__);
  298. return 0;
  299. }
  300. iowrite32(temp, msm_sdw->sdw_base + reg);
  301. return 0;
  302. }
  303. static int msm_sdw_ahb_read_device(struct msm_sdw_priv *msm_sdw,
  304. u16 reg, u8 *value)
  305. {
  306. u32 temp;
  307. if (!msm_sdw->dev_up) {
  308. dev_err_ratelimited(msm_sdw->dev, "%s: q6 not ready\n",
  309. __func__);
  310. return 0;
  311. }
  312. temp = ioread32(msm_sdw->sdw_base + reg);
  313. *value = (u8)temp;
  314. return 0;
  315. }
  316. static int __msm_sdw_reg_read(struct msm_sdw_priv *msm_sdw, unsigned short reg,
  317. int bytes, void *dest)
  318. {
  319. int ret = -EINVAL, i;
  320. u8 temp = 0;
  321. dev_dbg(msm_sdw->dev, "%s reg = %x\n", __func__, reg);
  322. mutex_lock(&msm_sdw->cdc_int_mclk1_mutex);
  323. if (msm_sdw->int_mclk1_enabled == false) {
  324. msm_sdw->sdw_cdc_core_clk.enable = 1;
  325. ret = afe_set_lpass_clock_v2(
  326. AFE_PORT_ID_INT4_MI2S_RX,
  327. &msm_sdw->sdw_cdc_core_clk);
  328. if (ret < 0) {
  329. dev_err(msm_sdw->dev,
  330. "%s:failed to enable the INT_MCLK1\n",
  331. __func__);
  332. goto unlock_exit;
  333. }
  334. dev_dbg(msm_sdw->dev, "%s:enabled sdw codec core clk\n",
  335. __func__);
  336. for (i = 0; i < bytes; i++) {
  337. ret = msm_sdw_ahb_read_device(
  338. msm_sdw, reg + (4 * i), &temp);
  339. ((u8 *)dest)[i] = temp;
  340. }
  341. msm_sdw->int_mclk1_enabled = true;
  342. schedule_delayed_work(&msm_sdw->disable_int_mclk1_work, 50);
  343. goto unlock_exit;
  344. }
  345. for (i = 0; i < bytes; i++) {
  346. ret = msm_sdw_ahb_read_device(
  347. msm_sdw, reg + (4 * i), &temp);
  348. ((u8 *)dest)[i] = temp;
  349. }
  350. unlock_exit:
  351. mutex_unlock(&msm_sdw->cdc_int_mclk1_mutex);
  352. if (ret < 0) {
  353. dev_err_ratelimited(msm_sdw->dev,
  354. "%s: codec read failed for reg 0x%x\n",
  355. __func__, reg);
  356. return ret;
  357. }
  358. dev_dbg(msm_sdw->dev, "Read 0x%02x from 0x%x\n", temp, reg);
  359. return 0;
  360. }
  361. static int __msm_sdw_reg_write(struct msm_sdw_priv *msm_sdw, unsigned short reg,
  362. int bytes, void *src)
  363. {
  364. int ret = -EINVAL, i;
  365. mutex_lock(&msm_sdw->cdc_int_mclk1_mutex);
  366. if (msm_sdw->int_mclk1_enabled == false) {
  367. msm_sdw->sdw_cdc_core_clk.enable = 1;
  368. ret = afe_set_lpass_clock_v2(AFE_PORT_ID_INT4_MI2S_RX,
  369. &msm_sdw->sdw_cdc_core_clk);
  370. if (ret < 0) {
  371. dev_err(msm_sdw->dev,
  372. "%s: failed to enable the INT_MCLK1\n",
  373. __func__);
  374. ret = 0;
  375. goto unlock_exit;
  376. }
  377. dev_dbg(msm_sdw->dev, "%s: enabled INT_MCLK1\n", __func__);
  378. for (i = 0; i < bytes; i++)
  379. ret = msm_sdw_ahb_write_device(msm_sdw, reg + (4 * i),
  380. &((u8 *)src)[i]);
  381. msm_sdw->int_mclk1_enabled = true;
  382. schedule_delayed_work(&msm_sdw->disable_int_mclk1_work, 50);
  383. goto unlock_exit;
  384. }
  385. for (i = 0; i < bytes; i++)
  386. ret = msm_sdw_ahb_write_device(msm_sdw, reg + (4 * i),
  387. &((u8 *)src)[i]);
  388. unlock_exit:
  389. mutex_unlock(&msm_sdw->cdc_int_mclk1_mutex);
  390. dev_dbg(msm_sdw->dev, "Write 0x%x val 0x%02x\n",
  391. reg, (u32)(*(u32 *)src));
  392. return ret;
  393. }
  394. static int msm_sdw_codec_enable_vi_feedback(struct snd_soc_dapm_widget *w,
  395. struct snd_kcontrol *kcontrol,
  396. int event)
  397. {
  398. struct snd_soc_codec *codec = NULL;
  399. struct msm_sdw_priv *msm_sdw_p = NULL;
  400. int ret = 0;
  401. if (!w) {
  402. pr_err("%s invalid params\n", __func__);
  403. return -EINVAL;
  404. }
  405. codec = snd_soc_dapm_to_codec(w->dapm);
  406. msm_sdw_p = snd_soc_codec_get_drvdata(codec);
  407. dev_dbg(codec->dev, "%s: num_dai %d stream name %s\n",
  408. __func__, codec->component.num_dai, w->sname);
  409. dev_dbg(codec->dev, "%s(): w->name %s event %d w->shift %d\n",
  410. __func__, w->name, event, w->shift);
  411. if (w->shift != AIF1_SDW_VIFEED) {
  412. dev_err(codec->dev,
  413. "%s:Error in enabling the vi feedback path\n",
  414. __func__);
  415. ret = -EINVAL;
  416. goto out_vi;
  417. }
  418. switch (event) {
  419. case SND_SOC_DAPM_POST_PMU:
  420. if (test_bit(VI_SENSE_1, &msm_sdw_p->status_mask)) {
  421. dev_dbg(codec->dev, "%s: spkr1 enabled\n", __func__);
  422. /* Enable V&I sensing */
  423. snd_soc_update_bits(codec,
  424. MSM_SDW_TX9_SPKR_PROT_PATH_CTL, 0x20, 0x20);
  425. snd_soc_update_bits(codec,
  426. MSM_SDW_TX10_SPKR_PROT_PATH_CTL, 0x20,
  427. 0x20);
  428. snd_soc_update_bits(codec,
  429. MSM_SDW_TX9_SPKR_PROT_PATH_CTL, 0x0F, 0x04);
  430. snd_soc_update_bits(codec,
  431. MSM_SDW_TX10_SPKR_PROT_PATH_CTL, 0x0F, 0x04);
  432. snd_soc_update_bits(codec,
  433. MSM_SDW_TX9_SPKR_PROT_PATH_CTL, 0x10, 0x10);
  434. snd_soc_update_bits(codec,
  435. MSM_SDW_TX10_SPKR_PROT_PATH_CTL, 0x10,
  436. 0x10);
  437. snd_soc_update_bits(codec,
  438. MSM_SDW_TX9_SPKR_PROT_PATH_CTL, 0x20, 0x00);
  439. snd_soc_update_bits(codec,
  440. MSM_SDW_TX10_SPKR_PROT_PATH_CTL, 0x20,
  441. 0x00);
  442. }
  443. if (test_bit(VI_SENSE_2, &msm_sdw_p->status_mask)) {
  444. dev_dbg(codec->dev, "%s: spkr2 enabled\n", __func__);
  445. /* Enable V&I sensing */
  446. snd_soc_update_bits(codec,
  447. MSM_SDW_TX11_SPKR_PROT_PATH_CTL, 0x20,
  448. 0x20);
  449. snd_soc_update_bits(codec,
  450. MSM_SDW_TX12_SPKR_PROT_PATH_CTL, 0x20,
  451. 0x20);
  452. snd_soc_update_bits(codec,
  453. MSM_SDW_TX11_SPKR_PROT_PATH_CTL, 0x0F,
  454. 0x04);
  455. snd_soc_update_bits(codec,
  456. MSM_SDW_TX12_SPKR_PROT_PATH_CTL, 0x0F,
  457. 0x04);
  458. snd_soc_update_bits(codec,
  459. MSM_SDW_TX11_SPKR_PROT_PATH_CTL, 0x10,
  460. 0x10);
  461. snd_soc_update_bits(codec,
  462. MSM_SDW_TX12_SPKR_PROT_PATH_CTL, 0x10,
  463. 0x10);
  464. snd_soc_update_bits(codec,
  465. MSM_SDW_TX11_SPKR_PROT_PATH_CTL, 0x20,
  466. 0x00);
  467. snd_soc_update_bits(codec,
  468. MSM_SDW_TX12_SPKR_PROT_PATH_CTL, 0x20,
  469. 0x00);
  470. }
  471. break;
  472. case SND_SOC_DAPM_POST_PMD:
  473. if (test_bit(VI_SENSE_1, &msm_sdw_p->status_mask)) {
  474. /* Disable V&I sensing */
  475. dev_dbg(codec->dev, "%s: spkr1 disabled\n", __func__);
  476. snd_soc_update_bits(codec,
  477. MSM_SDW_TX9_SPKR_PROT_PATH_CTL, 0x20, 0x20);
  478. snd_soc_update_bits(codec,
  479. MSM_SDW_TX10_SPKR_PROT_PATH_CTL, 0x20,
  480. 0x20);
  481. snd_soc_update_bits(codec,
  482. MSM_SDW_TX9_SPKR_PROT_PATH_CTL, 0x10, 0x00);
  483. snd_soc_update_bits(codec,
  484. MSM_SDW_TX10_SPKR_PROT_PATH_CTL, 0x10,
  485. 0x00);
  486. }
  487. if (test_bit(VI_SENSE_2, &msm_sdw_p->status_mask)) {
  488. /* Disable V&I sensing */
  489. dev_dbg(codec->dev, "%s: spkr2 disabled\n", __func__);
  490. snd_soc_update_bits(codec,
  491. MSM_SDW_TX11_SPKR_PROT_PATH_CTL, 0x20,
  492. 0x20);
  493. snd_soc_update_bits(codec,
  494. MSM_SDW_TX12_SPKR_PROT_PATH_CTL, 0x20,
  495. 0x20);
  496. snd_soc_update_bits(codec,
  497. MSM_SDW_TX11_SPKR_PROT_PATH_CTL, 0x10,
  498. 0x00);
  499. snd_soc_update_bits(codec,
  500. MSM_SDW_TX12_SPKR_PROT_PATH_CTL, 0x10,
  501. 0x00);
  502. }
  503. break;
  504. }
  505. out_vi:
  506. return ret;
  507. }
  508. static int msm_sdwm_handle_irq(void *handle,
  509. irqreturn_t (*swrm_irq_handler)(int irq,
  510. void *data),
  511. void *swrm_handle,
  512. int action)
  513. {
  514. struct msm_sdw_priv *msm_sdw;
  515. int ret = 0;
  516. if (!handle) {
  517. pr_err("%s: null handle received\n", __func__);
  518. return -EINVAL;
  519. }
  520. msm_sdw = (struct msm_sdw_priv *) handle;
  521. if (skip_irq)
  522. return ret;
  523. if (action) {
  524. ret = request_threaded_irq(msm_sdw->sdw_irq, NULL,
  525. swrm_irq_handler,
  526. IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
  527. "swr_master_irq", swrm_handle);
  528. if (ret)
  529. dev_err(msm_sdw->dev, "%s: Failed to request irq %d\n",
  530. __func__, ret);
  531. } else
  532. free_irq(msm_sdw->sdw_irq, swrm_handle);
  533. return ret;
  534. }
  535. static void msm_sdw_codec_hd2_control(struct snd_soc_codec *codec,
  536. u16 reg, int event)
  537. {
  538. u16 hd2_scale_reg;
  539. u16 hd2_enable_reg = 0;
  540. if (reg == MSM_SDW_RX7_RX_PATH_CTL) {
  541. hd2_scale_reg = MSM_SDW_RX7_RX_PATH_SEC3;
  542. hd2_enable_reg = MSM_SDW_RX7_RX_PATH_CFG0;
  543. }
  544. if (reg == MSM_SDW_RX8_RX_PATH_CTL) {
  545. hd2_scale_reg = MSM_SDW_RX8_RX_PATH_SEC3;
  546. hd2_enable_reg = MSM_SDW_RX8_RX_PATH_CFG0;
  547. }
  548. if (hd2_enable_reg && SND_SOC_DAPM_EVENT_ON(event)) {
  549. snd_soc_update_bits(codec, hd2_scale_reg, 0x3C, 0x10);
  550. snd_soc_update_bits(codec, hd2_scale_reg, 0x03, 0x01);
  551. snd_soc_update_bits(codec, hd2_enable_reg, 0x04, 0x04);
  552. }
  553. if (hd2_enable_reg && SND_SOC_DAPM_EVENT_OFF(event)) {
  554. snd_soc_update_bits(codec, hd2_enable_reg, 0x04, 0x00);
  555. snd_soc_update_bits(codec, hd2_scale_reg, 0x03, 0x00);
  556. snd_soc_update_bits(codec, hd2_scale_reg, 0x3C, 0x00);
  557. }
  558. }
  559. static int msm_sdw_enable_swr(struct snd_soc_dapm_widget *w,
  560. struct snd_kcontrol *kcontrol, int event)
  561. {
  562. struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
  563. struct msm_sdw_priv *msm_sdw;
  564. int i, ch_cnt;
  565. msm_sdw = snd_soc_codec_get_drvdata(codec);
  566. if (!msm_sdw->nr)
  567. return 0;
  568. switch (event) {
  569. case SND_SOC_DAPM_PRE_PMU:
  570. if (!(strnstr(w->name, "RX4", sizeof("RX4 MIX"))) &&
  571. !msm_sdw->rx_4_count)
  572. msm_sdw->rx_4_count++;
  573. if (!(strnstr(w->name, "RX5", sizeof("RX5 MIX"))) &&
  574. !msm_sdw->rx_5_count)
  575. msm_sdw->rx_5_count++;
  576. ch_cnt = msm_sdw->rx_4_count + msm_sdw->rx_5_count;
  577. for (i = 0; i < msm_sdw->nr; i++) {
  578. swrm_wcd_notify(msm_sdw->sdw_ctrl_data[i].sdw_pdev,
  579. SWR_DEVICE_UP, NULL);
  580. swrm_wcd_notify(msm_sdw->sdw_ctrl_data[i].sdw_pdev,
  581. SWR_SET_NUM_RX_CH, &ch_cnt);
  582. }
  583. break;
  584. case SND_SOC_DAPM_POST_PMD:
  585. if (!(strnstr(w->name, "RX4", sizeof("RX4 MIX"))) &&
  586. msm_sdw->rx_4_count)
  587. msm_sdw->rx_4_count--;
  588. if (!(strnstr(w->name, "RX5", sizeof("RX5 MIX"))) &&
  589. msm_sdw->rx_5_count)
  590. msm_sdw->rx_5_count--;
  591. ch_cnt = msm_sdw->rx_4_count + msm_sdw->rx_5_count;
  592. for (i = 0; i < msm_sdw->nr; i++)
  593. swrm_wcd_notify(msm_sdw->sdw_ctrl_data[i].sdw_pdev,
  594. SWR_SET_NUM_RX_CH, &ch_cnt);
  595. break;
  596. }
  597. dev_dbg(msm_sdw->dev, "%s: current swr ch cnt: %d\n",
  598. __func__, msm_sdw->rx_4_count + msm_sdw->rx_5_count);
  599. return 0;
  600. }
  601. static int msm_sdw_codec_enable_interpolator(struct snd_soc_dapm_widget *w,
  602. struct snd_kcontrol *kcontrol,
  603. int event)
  604. {
  605. struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
  606. struct msm_sdw_priv *msm_sdw = snd_soc_codec_get_drvdata(codec);
  607. u16 gain_reg;
  608. u16 reg;
  609. int val;
  610. int offset_val = 0;
  611. dev_dbg(codec->dev, "%s %d %s\n", __func__, event, w->name);
  612. if (!(strcmp(w->name, "RX INT4 INTERP"))) {
  613. reg = MSM_SDW_RX7_RX_PATH_CTL;
  614. gain_reg = MSM_SDW_RX7_RX_VOL_CTL;
  615. } else if (!(strcmp(w->name, "RX INT5 INTERP"))) {
  616. reg = MSM_SDW_RX8_RX_PATH_CTL;
  617. gain_reg = MSM_SDW_RX8_RX_VOL_CTL;
  618. } else {
  619. dev_err(codec->dev, "%s: Interpolator reg not found\n",
  620. __func__);
  621. return -EINVAL;
  622. }
  623. switch (event) {
  624. case SND_SOC_DAPM_PRE_PMU:
  625. snd_soc_update_bits(codec, reg, 0x10, 0x10);
  626. msm_sdw_codec_hd2_control(codec, reg, event);
  627. snd_soc_update_bits(codec, reg, 1 << 0x5, 1 << 0x5);
  628. break;
  629. case SND_SOC_DAPM_POST_PMU:
  630. msm_sdw_config_compander(codec, w->shift, event);
  631. /* apply gain after int clk is enabled */
  632. if ((msm_sdw->spkr_gain_offset == RX_GAIN_OFFSET_M1P5_DB) &&
  633. (msm_sdw->comp_enabled[COMP1] ||
  634. msm_sdw->comp_enabled[COMP2]) &&
  635. (gain_reg == MSM_SDW_RX7_RX_VOL_CTL ||
  636. gain_reg == MSM_SDW_RX8_RX_VOL_CTL)) {
  637. snd_soc_update_bits(codec, MSM_SDW_RX7_RX_PATH_SEC1,
  638. 0x01, 0x01);
  639. snd_soc_update_bits(codec,
  640. MSM_SDW_RX7_RX_PATH_MIX_SEC0,
  641. 0x01, 0x01);
  642. snd_soc_update_bits(codec, MSM_SDW_RX8_RX_PATH_SEC1,
  643. 0x01, 0x01);
  644. snd_soc_update_bits(codec,
  645. MSM_SDW_RX8_RX_PATH_MIX_SEC0,
  646. 0x01, 0x01);
  647. offset_val = -2;
  648. }
  649. val = snd_soc_read(codec, gain_reg);
  650. val += offset_val;
  651. snd_soc_write(codec, gain_reg, val);
  652. msm_sdw_config_ear_spkr_gain(codec, event, gain_reg);
  653. snd_soc_update_bits(codec, reg, 0x10, 0x00);
  654. break;
  655. case SND_SOC_DAPM_POST_PMD:
  656. snd_soc_update_bits(codec, reg, 1 << 0x5, 0 << 0x5);
  657. snd_soc_update_bits(codec, reg, 0x40, 0x40);
  658. snd_soc_update_bits(codec, reg, 0x40, 0x00);
  659. msm_sdw_codec_hd2_control(codec, reg, event);
  660. msm_sdw_config_compander(codec, w->shift, event);
  661. if ((msm_sdw->spkr_gain_offset == RX_GAIN_OFFSET_M1P5_DB) &&
  662. (msm_sdw->comp_enabled[COMP1] ||
  663. msm_sdw->comp_enabled[COMP2]) &&
  664. (gain_reg == MSM_SDW_RX7_RX_VOL_CTL ||
  665. gain_reg == MSM_SDW_RX8_RX_VOL_CTL)) {
  666. snd_soc_update_bits(codec, MSM_SDW_RX7_RX_PATH_SEC1,
  667. 0x01, 0x00);
  668. snd_soc_update_bits(codec,
  669. MSM_SDW_RX7_RX_PATH_MIX_SEC0,
  670. 0x01, 0x00);
  671. snd_soc_update_bits(codec, MSM_SDW_RX8_RX_PATH_SEC1,
  672. 0x01, 0x00);
  673. snd_soc_update_bits(codec,
  674. MSM_SDW_RX8_RX_PATH_MIX_SEC0,
  675. 0x01, 0x00);
  676. offset_val = 2;
  677. val = snd_soc_read(codec, gain_reg);
  678. val += offset_val;
  679. snd_soc_write(codec, gain_reg, val);
  680. }
  681. msm_sdw_config_ear_spkr_gain(codec, event, gain_reg);
  682. break;
  683. };
  684. return 0;
  685. }
  686. static int msm_sdw_config_ear_spkr_gain(struct snd_soc_codec *codec,
  687. int event, int gain_reg)
  688. {
  689. int comp_gain_offset, val;
  690. struct msm_sdw_priv *msm_sdw = snd_soc_codec_get_drvdata(codec);
  691. switch (msm_sdw->spkr_mode) {
  692. /* Compander gain in SPKR_MODE1 case is 12 dB */
  693. case SPKR_MODE_1:
  694. comp_gain_offset = -12;
  695. break;
  696. /* Default case compander gain is 15 dB */
  697. default:
  698. comp_gain_offset = -15;
  699. break;
  700. }
  701. switch (event) {
  702. case SND_SOC_DAPM_POST_PMU:
  703. /* Apply ear spkr gain only if compander is enabled */
  704. if (msm_sdw->comp_enabled[COMP1] &&
  705. (gain_reg == MSM_SDW_RX7_RX_VOL_CTL) &&
  706. (msm_sdw->ear_spkr_gain != 0)) {
  707. /* For example, val is -8(-12+5-1) for 4dB of gain */
  708. val = comp_gain_offset + msm_sdw->ear_spkr_gain - 1;
  709. snd_soc_write(codec, gain_reg, val);
  710. dev_dbg(codec->dev, "%s: RX4 Volume %d dB\n",
  711. __func__, val);
  712. }
  713. break;
  714. case SND_SOC_DAPM_POST_PMD:
  715. /*
  716. * Reset RX4 volume to 0 dB if compander is enabled and
  717. * ear_spkr_gain is non-zero.
  718. */
  719. if (msm_sdw->comp_enabled[COMP1] &&
  720. (gain_reg == MSM_SDW_RX7_RX_VOL_CTL) &&
  721. (msm_sdw->ear_spkr_gain != 0)) {
  722. snd_soc_write(codec, gain_reg, 0x0);
  723. dev_dbg(codec->dev, "%s: Reset RX4 Volume to 0 dB\n",
  724. __func__);
  725. }
  726. break;
  727. }
  728. return 0;
  729. }
  730. static int msm_sdw_codec_spk_boost_event(struct snd_soc_dapm_widget *w,
  731. struct snd_kcontrol *kcontrol,
  732. int event)
  733. {
  734. struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
  735. u16 boost_path_ctl, boost_path_cfg1;
  736. u16 reg;
  737. dev_dbg(codec->dev, "%s %s %d\n", __func__, w->name, event);
  738. if (!strcmp(w->name, "RX INT4 CHAIN")) {
  739. boost_path_ctl = MSM_SDW_BOOST0_BOOST_PATH_CTL;
  740. boost_path_cfg1 = MSM_SDW_RX7_RX_PATH_CFG1;
  741. reg = MSM_SDW_RX7_RX_PATH_CTL;
  742. } else if (!strcmp(w->name, "RX INT5 CHAIN")) {
  743. boost_path_ctl = MSM_SDW_BOOST1_BOOST_PATH_CTL;
  744. boost_path_cfg1 = MSM_SDW_RX8_RX_PATH_CFG1;
  745. reg = MSM_SDW_RX8_RX_PATH_CTL;
  746. } else {
  747. dev_err(codec->dev, "%s: boost reg not found\n",
  748. __func__);
  749. return -EINVAL;
  750. }
  751. switch (event) {
  752. case SND_SOC_DAPM_PRE_PMU:
  753. snd_soc_update_bits(codec, boost_path_ctl, 0x10, 0x10);
  754. snd_soc_update_bits(codec, boost_path_cfg1, 0x01, 0x01);
  755. snd_soc_update_bits(codec, reg, 0x10, 0x00);
  756. break;
  757. case SND_SOC_DAPM_POST_PMD:
  758. snd_soc_update_bits(codec, boost_path_cfg1, 0x01, 0x00);
  759. snd_soc_update_bits(codec, boost_path_ctl, 0x10, 0x00);
  760. break;
  761. };
  762. return 0;
  763. }
  764. static int msm_sdw_config_compander(struct snd_soc_codec *codec, int comp,
  765. int event)
  766. {
  767. struct msm_sdw_priv *msm_sdw = snd_soc_codec_get_drvdata(codec);
  768. u16 comp_ctl0_reg, rx_path_cfg0_reg;
  769. if (comp < COMP1 || comp >= COMP_MAX)
  770. return 0;
  771. dev_dbg(codec->dev, "%s: event %d compander %d, enabled %d\n",
  772. __func__, event, comp + 1, msm_sdw->comp_enabled[comp]);
  773. if (!msm_sdw->comp_enabled[comp])
  774. return 0;
  775. comp_ctl0_reg = MSM_SDW_COMPANDER7_CTL0 + (comp * 0x20);
  776. rx_path_cfg0_reg = MSM_SDW_RX7_RX_PATH_CFG0 + (comp * 0x1E0);
  777. if (SND_SOC_DAPM_EVENT_ON(event)) {
  778. /* Enable Compander Clock */
  779. snd_soc_update_bits(codec, comp_ctl0_reg, 0x01, 0x01);
  780. snd_soc_update_bits(codec, comp_ctl0_reg, 0x02, 0x02);
  781. snd_soc_update_bits(codec, comp_ctl0_reg, 0x02, 0x00);
  782. snd_soc_update_bits(codec, rx_path_cfg0_reg, 0x02, 0x02);
  783. }
  784. if (SND_SOC_DAPM_EVENT_OFF(event)) {
  785. snd_soc_update_bits(codec, comp_ctl0_reg, 0x04, 0x04);
  786. snd_soc_update_bits(codec, rx_path_cfg0_reg, 0x02, 0x00);
  787. snd_soc_update_bits(codec, comp_ctl0_reg, 0x02, 0x02);
  788. snd_soc_update_bits(codec, comp_ctl0_reg, 0x02, 0x00);
  789. snd_soc_update_bits(codec, comp_ctl0_reg, 0x01, 0x00);
  790. snd_soc_update_bits(codec, comp_ctl0_reg, 0x04, 0x00);
  791. }
  792. return 0;
  793. }
  794. static int msm_sdw_get_compander(struct snd_kcontrol *kcontrol,
  795. struct snd_ctl_elem_value *ucontrol)
  796. {
  797. struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
  798. int comp = ((struct soc_multi_mixer_control *)
  799. kcontrol->private_value)->shift;
  800. struct msm_sdw_priv *msm_sdw = snd_soc_codec_get_drvdata(codec);
  801. ucontrol->value.integer.value[0] = msm_sdw->comp_enabled[comp];
  802. return 0;
  803. }
  804. static int msm_sdw_set_compander(struct snd_kcontrol *kcontrol,
  805. struct snd_ctl_elem_value *ucontrol)
  806. {
  807. struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
  808. struct msm_sdw_priv *msm_sdw = snd_soc_codec_get_drvdata(codec);
  809. int comp = ((struct soc_multi_mixer_control *)
  810. kcontrol->private_value)->shift;
  811. int value = ucontrol->value.integer.value[0];
  812. dev_dbg(codec->dev, "%s: Compander %d enable current %d, new %d\n",
  813. __func__, comp + 1, msm_sdw->comp_enabled[comp], value);
  814. msm_sdw->comp_enabled[comp] = value;
  815. return 0;
  816. }
  817. static int msm_sdw_ear_spkr_pa_gain_get(struct snd_kcontrol *kcontrol,
  818. struct snd_ctl_elem_value *ucontrol)
  819. {
  820. struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
  821. struct msm_sdw_priv *msm_sdw = snd_soc_codec_get_drvdata(codec);
  822. ucontrol->value.integer.value[0] = msm_sdw->ear_spkr_gain;
  823. dev_dbg(codec->dev, "%s: ucontrol->value.integer.value[0] = %ld\n",
  824. __func__, ucontrol->value.integer.value[0]);
  825. return 0;
  826. }
  827. static int msm_sdw_ear_spkr_pa_gain_put(struct snd_kcontrol *kcontrol,
  828. struct snd_ctl_elem_value *ucontrol)
  829. {
  830. struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
  831. struct msm_sdw_priv *msm_sdw = snd_soc_codec_get_drvdata(codec);
  832. msm_sdw->ear_spkr_gain = ucontrol->value.integer.value[0];
  833. dev_dbg(codec->dev, "%s: gain = %d\n", __func__,
  834. msm_sdw->ear_spkr_gain);
  835. return 0;
  836. }
  837. static int msm_sdw_spkr_left_boost_stage_get(struct snd_kcontrol *kcontrol,
  838. struct snd_ctl_elem_value *ucontrol)
  839. {
  840. u8 bst_state_max = 0;
  841. struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
  842. bst_state_max = snd_soc_read(codec, MSM_SDW_BOOST0_BOOST_CTL);
  843. bst_state_max = (bst_state_max & 0x0c) >> 2;
  844. ucontrol->value.integer.value[0] = bst_state_max;
  845. dev_dbg(codec->dev, "%s: ucontrol->value.integer.value[0] = %ld\n",
  846. __func__, ucontrol->value.integer.value[0]);
  847. return 0;
  848. }
  849. static int msm_sdw_spkr_left_boost_stage_put(struct snd_kcontrol *kcontrol,
  850. struct snd_ctl_elem_value *ucontrol)
  851. {
  852. u8 bst_state_max;
  853. struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
  854. dev_dbg(codec->dev, "%s: ucontrol->value.integer.value[0] = %ld\n",
  855. __func__, ucontrol->value.integer.value[0]);
  856. bst_state_max = ucontrol->value.integer.value[0] << 2;
  857. snd_soc_update_bits(codec, MSM_SDW_BOOST0_BOOST_CTL,
  858. 0x0c, bst_state_max);
  859. return 0;
  860. }
  861. static int msm_sdw_spkr_right_boost_stage_get(struct snd_kcontrol *kcontrol,
  862. struct snd_ctl_elem_value *ucontrol)
  863. {
  864. u8 bst_state_max = 0;
  865. struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
  866. bst_state_max = snd_soc_read(codec, MSM_SDW_BOOST1_BOOST_CTL);
  867. bst_state_max = (bst_state_max & 0x0c) >> 2;
  868. ucontrol->value.integer.value[0] = bst_state_max;
  869. dev_dbg(codec->dev, "%s: ucontrol->value.integer.value[0] = %ld\n",
  870. __func__, ucontrol->value.integer.value[0]);
  871. return 0;
  872. }
  873. static int msm_sdw_spkr_right_boost_stage_put(struct snd_kcontrol *kcontrol,
  874. struct snd_ctl_elem_value *ucontrol)
  875. {
  876. u8 bst_state_max;
  877. struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
  878. dev_dbg(codec->dev, "%s: ucontrol->value.integer.value[0] = %ld\n",
  879. __func__, ucontrol->value.integer.value[0]);
  880. bst_state_max = ucontrol->value.integer.value[0] << 2;
  881. snd_soc_update_bits(codec, MSM_SDW_BOOST1_BOOST_CTL,
  882. 0x0c, bst_state_max);
  883. return 0;
  884. }
  885. static int msm_sdw_vi_feed_mixer_get(struct snd_kcontrol *kcontrol,
  886. struct snd_ctl_elem_value *ucontrol)
  887. {
  888. struct snd_soc_dapm_widget *widget =
  889. snd_soc_dapm_kcontrol_widget(kcontrol);
  890. struct snd_soc_codec *codec = snd_soc_dapm_to_codec(widget->dapm);
  891. struct msm_sdw_priv *msm_sdw_p = snd_soc_codec_get_drvdata(codec);
  892. ucontrol->value.integer.value[0] = msm_sdw_p->vi_feed_value;
  893. return 0;
  894. }
  895. static int msm_sdw_vi_feed_mixer_put(struct snd_kcontrol *kcontrol,
  896. struct snd_ctl_elem_value *ucontrol)
  897. {
  898. struct snd_soc_dapm_widget *widget =
  899. snd_soc_dapm_kcontrol_widget(kcontrol);
  900. struct snd_soc_codec *codec = snd_soc_dapm_to_codec(widget->dapm);
  901. struct msm_sdw_priv *msm_sdw_p = snd_soc_codec_get_drvdata(codec);
  902. struct soc_multi_mixer_control *mixer =
  903. ((struct soc_multi_mixer_control *)kcontrol->private_value);
  904. u32 dai_id = widget->shift;
  905. u32 port_id = mixer->shift;
  906. u32 enable = ucontrol->value.integer.value[0];
  907. dev_dbg(codec->dev, "%s: enable: %d, port_id:%d, dai_id: %d\n",
  908. __func__, enable, port_id, dai_id);
  909. msm_sdw_p->vi_feed_value = ucontrol->value.integer.value[0];
  910. mutex_lock(&msm_sdw_p->codec_mutex);
  911. if (enable) {
  912. if (port_id == MSM_SDW_TX0 && !test_bit(VI_SENSE_1,
  913. &msm_sdw_p->status_mask))
  914. set_bit(VI_SENSE_1, &msm_sdw_p->status_mask);
  915. if (port_id == MSM_SDW_TX1 && !test_bit(VI_SENSE_2,
  916. &msm_sdw_p->status_mask))
  917. set_bit(VI_SENSE_2, &msm_sdw_p->status_mask);
  918. } else {
  919. if (port_id == MSM_SDW_TX0 && test_bit(VI_SENSE_1,
  920. &msm_sdw_p->status_mask))
  921. clear_bit(VI_SENSE_1, &msm_sdw_p->status_mask);
  922. if (port_id == MSM_SDW_TX1 && test_bit(VI_SENSE_2,
  923. &msm_sdw_p->status_mask))
  924. clear_bit(VI_SENSE_2, &msm_sdw_p->status_mask);
  925. }
  926. mutex_unlock(&msm_sdw_p->codec_mutex);
  927. snd_soc_dapm_mixer_update_power(widget->dapm, kcontrol, enable, NULL);
  928. return 0;
  929. }
  930. static int msm_sdw_mclk_enable(struct msm_sdw_priv *msm_sdw,
  931. int mclk_enable, bool dapm)
  932. {
  933. dev_dbg(msm_sdw->dev, "%s: mclk_enable = %u, dapm = %d clk_users= %d\n",
  934. __func__, mclk_enable, dapm, msm_sdw->sdw_mclk_users);
  935. if (mclk_enable) {
  936. msm_sdw->sdw_mclk_users++;
  937. if (msm_sdw->sdw_mclk_users == 1) {
  938. regmap_update_bits(msm_sdw->regmap,
  939. MSM_SDW_CLK_RST_CTRL_FS_CNT_CONTROL,
  940. 0x01, 0x01);
  941. regmap_update_bits(msm_sdw->regmap,
  942. MSM_SDW_CLK_RST_CTRL_MCLK_CONTROL,
  943. 0x01, 0x01);
  944. /* 9.6MHz MCLK, set value 0x00 if other frequency */
  945. regmap_update_bits(msm_sdw->regmap,
  946. MSM_SDW_TOP_FREQ_MCLK, 0x01, 0x01);
  947. }
  948. } else {
  949. msm_sdw->sdw_mclk_users--;
  950. if (msm_sdw->sdw_mclk_users == 0) {
  951. regmap_update_bits(msm_sdw->regmap,
  952. MSM_SDW_CLK_RST_CTRL_FS_CNT_CONTROL,
  953. 0x01, 0x00);
  954. regmap_update_bits(msm_sdw->regmap,
  955. MSM_SDW_CLK_RST_CTRL_MCLK_CONTROL,
  956. 0x01, 0x00);
  957. }
  958. }
  959. return 0;
  960. }
  961. EXPORT_SYMBOL(msm_sdw_mclk_enable);
  962. static int msm_sdw_swrm_read(void *handle, int reg)
  963. {
  964. struct msm_sdw_priv *msm_sdw;
  965. unsigned short sdw_rd_addr_base;
  966. unsigned short sdw_rd_data_base;
  967. int val, ret;
  968. if (!handle) {
  969. pr_err("%s: NULL handle\n", __func__);
  970. return -EINVAL;
  971. }
  972. msm_sdw = (struct msm_sdw_priv *)handle;
  973. dev_dbg(msm_sdw->dev, "%s: Reading soundwire register, 0x%x\n",
  974. __func__, reg);
  975. sdw_rd_addr_base = MSM_SDW_AHB_BRIDGE_RD_ADDR_0;
  976. sdw_rd_data_base = MSM_SDW_AHB_BRIDGE_RD_DATA_0;
  977. /*
  978. * Add sleep as SWR slave access read takes time.
  979. * Allow for RD_DONE to complete for previous register if any.
  980. */
  981. usleep_range(100, 105);
  982. /* read_lock */
  983. mutex_lock(&msm_sdw->sdw_read_lock);
  984. ret = regmap_bulk_write(msm_sdw->regmap, sdw_rd_addr_base,
  985. (u8 *)&reg, 4);
  986. if (ret < 0) {
  987. dev_err(msm_sdw->dev, "%s: RD Addr Failure\n", __func__);
  988. goto err;
  989. }
  990. /* Add sleep for SWR register read value to get updated. */
  991. usleep_range(100, 105);
  992. /* Check for RD value */
  993. ret = regmap_bulk_read(msm_sdw->regmap, sdw_rd_data_base,
  994. (u8 *)&val, 4);
  995. if (ret < 0) {
  996. dev_err(msm_sdw->dev, "%s: RD Data Failure\n", __func__);
  997. goto err;
  998. }
  999. ret = val;
  1000. err:
  1001. /* read_unlock */
  1002. mutex_unlock(&msm_sdw->sdw_read_lock);
  1003. return ret;
  1004. }
  1005. static int msm_sdw_bulk_write(struct msm_sdw_priv *msm_sdw,
  1006. struct msm_sdw_reg_val *bulk_reg,
  1007. size_t len)
  1008. {
  1009. int i, ret = 0;
  1010. unsigned short sdw_wr_addr_base;
  1011. unsigned short sdw_wr_data_base;
  1012. sdw_wr_addr_base = MSM_SDW_AHB_BRIDGE_WR_ADDR_0;
  1013. sdw_wr_data_base = MSM_SDW_AHB_BRIDGE_WR_DATA_0;
  1014. for (i = 0; i < len; i += 2) {
  1015. /*
  1016. * Add sleep as SWR slave write takes time.
  1017. * Allow for any previous pending write to complete.
  1018. */
  1019. usleep_range(100, 105);
  1020. /* First Write the Data to register */
  1021. ret = regmap_bulk_write(msm_sdw->regmap,
  1022. sdw_wr_data_base, bulk_reg[i].buf, 4);
  1023. if (ret < 0) {
  1024. dev_err(msm_sdw->dev, "%s: WR Data Failure\n",
  1025. __func__);
  1026. break;
  1027. }
  1028. /* Next Write Address */
  1029. ret = regmap_bulk_write(msm_sdw->regmap,
  1030. sdw_wr_addr_base, bulk_reg[i+1].buf, 4);
  1031. if (ret < 0) {
  1032. dev_err(msm_sdw->dev,
  1033. "%s: WR Addr Failure: 0x%x\n",
  1034. __func__, (u32)(bulk_reg[i+1].buf[0]));
  1035. break;
  1036. }
  1037. }
  1038. return ret;
  1039. }
  1040. static int msm_sdw_swrm_bulk_write(void *handle, u32 *reg, u32 *val, size_t len)
  1041. {
  1042. struct msm_sdw_priv *msm_sdw;
  1043. struct msm_sdw_reg_val *bulk_reg;
  1044. unsigned short sdw_wr_addr_base;
  1045. unsigned short sdw_wr_data_base;
  1046. int i, j, ret;
  1047. if (!handle) {
  1048. pr_err("%s: NULL handle\n", __func__);
  1049. return -EINVAL;
  1050. }
  1051. msm_sdw = (struct msm_sdw_priv *)handle;
  1052. if (len <= 0) {
  1053. dev_err(msm_sdw->dev,
  1054. "%s: Invalid size: %zu\n", __func__, len);
  1055. return -EINVAL;
  1056. }
  1057. sdw_wr_addr_base = MSM_SDW_AHB_BRIDGE_WR_ADDR_0;
  1058. sdw_wr_data_base = MSM_SDW_AHB_BRIDGE_WR_DATA_0;
  1059. bulk_reg = kzalloc((2 * len * sizeof(struct msm_sdw_reg_val)),
  1060. GFP_KERNEL);
  1061. if (!bulk_reg)
  1062. return -ENOMEM;
  1063. for (i = 0, j = 0; i < (len * 2); i += 2, j++) {
  1064. bulk_reg[i].reg = sdw_wr_data_base;
  1065. bulk_reg[i].buf = (u8 *)(&val[j]);
  1066. bulk_reg[i].bytes = 4;
  1067. bulk_reg[i+1].reg = sdw_wr_addr_base;
  1068. bulk_reg[i+1].buf = (u8 *)(&reg[j]);
  1069. bulk_reg[i+1].bytes = 4;
  1070. }
  1071. mutex_lock(&msm_sdw->sdw_write_lock);
  1072. ret = msm_sdw_bulk_write(msm_sdw, bulk_reg, (len * 2));
  1073. if (ret)
  1074. dev_err(msm_sdw->dev, "%s: swrm bulk write failed, ret: %d\n",
  1075. __func__, ret);
  1076. mutex_unlock(&msm_sdw->sdw_write_lock);
  1077. kfree(bulk_reg);
  1078. return ret;
  1079. }
  1080. static int msm_sdw_swrm_write(void *handle, int reg, int val)
  1081. {
  1082. struct msm_sdw_priv *msm_sdw;
  1083. unsigned short sdw_wr_addr_base;
  1084. unsigned short sdw_wr_data_base;
  1085. struct msm_sdw_reg_val bulk_reg[2];
  1086. int ret;
  1087. if (!handle) {
  1088. pr_err("%s: NULL handle\n", __func__);
  1089. return -EINVAL;
  1090. }
  1091. msm_sdw = (struct msm_sdw_priv *)handle;
  1092. sdw_wr_addr_base = MSM_SDW_AHB_BRIDGE_WR_ADDR_0;
  1093. sdw_wr_data_base = MSM_SDW_AHB_BRIDGE_WR_DATA_0;
  1094. /* First Write the Data to register */
  1095. bulk_reg[0].reg = sdw_wr_data_base;
  1096. bulk_reg[0].buf = (u8 *)(&val);
  1097. bulk_reg[0].bytes = 4;
  1098. bulk_reg[1].reg = sdw_wr_addr_base;
  1099. bulk_reg[1].buf = (u8 *)(&reg);
  1100. bulk_reg[1].bytes = 4;
  1101. mutex_lock(&msm_sdw->sdw_write_lock);
  1102. ret = msm_sdw_bulk_write(msm_sdw, bulk_reg, 2);
  1103. if (ret < 0)
  1104. dev_err(msm_sdw->dev, "%s: WR Data Failure\n", __func__);
  1105. mutex_unlock(&msm_sdw->sdw_write_lock);
  1106. return ret;
  1107. }
  1108. static int msm_sdw_swrm_clock(void *handle, bool enable)
  1109. {
  1110. struct msm_sdw_priv *msm_sdw = (struct msm_sdw_priv *) handle;
  1111. mutex_lock(&msm_sdw->sdw_clk_lock);
  1112. dev_dbg(msm_sdw->dev, "%s: swrm clock %s\n",
  1113. __func__, (enable ? "enable" : "disable"));
  1114. if (enable) {
  1115. msm_sdw->sdw_clk_users++;
  1116. if (msm_sdw->sdw_clk_users == 1) {
  1117. msm_int_enable_sdw_cdc_clk(msm_sdw, 1, true);
  1118. msm_sdw_mclk_enable(msm_sdw, 1, true);
  1119. regmap_update_bits(msm_sdw->regmap,
  1120. MSM_SDW_CLK_RST_CTRL_SWR_CONTROL, 0x01, 0x01);
  1121. msm_enable_sdw_npl_clk(msm_sdw, true);
  1122. msm_cdc_pinctrl_select_active_state(
  1123. msm_sdw->sdw_gpio_p);
  1124. }
  1125. } else {
  1126. msm_sdw->sdw_clk_users--;
  1127. if (msm_sdw->sdw_clk_users == 0) {
  1128. regmap_update_bits(msm_sdw->regmap,
  1129. MSM_SDW_CLK_RST_CTRL_SWR_CONTROL,
  1130. 0x01, 0x00);
  1131. msm_sdw_mclk_enable(msm_sdw, 0, true);
  1132. msm_int_enable_sdw_cdc_clk(msm_sdw, 0, true);
  1133. msm_enable_sdw_npl_clk(msm_sdw, false);
  1134. msm_cdc_pinctrl_select_sleep_state(msm_sdw->sdw_gpio_p);
  1135. }
  1136. }
  1137. dev_dbg(msm_sdw->dev, "%s: swrm clock users %d\n",
  1138. __func__, msm_sdw->sdw_clk_users);
  1139. mutex_unlock(&msm_sdw->sdw_clk_lock);
  1140. return 0;
  1141. }
  1142. static int msm_sdw_startup(struct snd_pcm_substream *substream,
  1143. struct snd_soc_dai *dai)
  1144. {
  1145. dev_dbg(dai->codec->dev, "%s(): substream = %s stream = %d\n",
  1146. __func__,
  1147. substream->name, substream->stream);
  1148. return 0;
  1149. }
  1150. static int msm_sdw_hw_params(struct snd_pcm_substream *substream,
  1151. struct snd_pcm_hw_params *params,
  1152. struct snd_soc_dai *dai)
  1153. {
  1154. u8 clk_fs_rate, fs_rate;
  1155. dev_dbg(dai->codec->dev,
  1156. "%s: dai_name = %s DAI-ID %x rate %d num_ch %d format %d\n",
  1157. __func__, dai->name, dai->id, params_rate(params),
  1158. params_channels(params), params_format(params));
  1159. switch (params_rate(params)) {
  1160. case 8000:
  1161. clk_fs_rate = 0x00;
  1162. fs_rate = 0x00;
  1163. break;
  1164. case 16000:
  1165. clk_fs_rate = 0x01;
  1166. fs_rate = 0x01;
  1167. break;
  1168. case 32000:
  1169. clk_fs_rate = 0x02;
  1170. fs_rate = 0x03;
  1171. break;
  1172. case 48000:
  1173. clk_fs_rate = 0x03;
  1174. fs_rate = 0x04;
  1175. break;
  1176. case 96000:
  1177. clk_fs_rate = 0x04;
  1178. fs_rate = 0x05;
  1179. break;
  1180. case 192000:
  1181. clk_fs_rate = 0x05;
  1182. fs_rate = 0x06;
  1183. break;
  1184. default:
  1185. dev_err(dai->codec->dev,
  1186. "%s: Invalid sampling rate %d\n", __func__,
  1187. params_rate(params));
  1188. return -EINVAL;
  1189. }
  1190. if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
  1191. snd_soc_update_bits(dai->codec,
  1192. MSM_SDW_TOP_TX_I2S_CTL, 0x1C,
  1193. (clk_fs_rate << 2));
  1194. } else {
  1195. snd_soc_update_bits(dai->codec,
  1196. MSM_SDW_TOP_RX_I2S_CTL, 0x1C,
  1197. (clk_fs_rate << 2));
  1198. snd_soc_update_bits(dai->codec,
  1199. MSM_SDW_RX7_RX_PATH_CTL, 0x0F,
  1200. fs_rate);
  1201. snd_soc_update_bits(dai->codec,
  1202. MSM_SDW_RX8_RX_PATH_CTL, 0x0F,
  1203. fs_rate);
  1204. }
  1205. switch (params_format(params)) {
  1206. case SNDRV_PCM_FORMAT_S16_LE:
  1207. if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  1208. snd_soc_update_bits(dai->codec,
  1209. MSM_SDW_TOP_TX_I2S_CTL, 0x20, 0x20);
  1210. else
  1211. snd_soc_update_bits(dai->codec,
  1212. MSM_SDW_TOP_RX_I2S_CTL, 0x20, 0x20);
  1213. break;
  1214. case SNDRV_PCM_FORMAT_S24_LE:
  1215. case SNDRV_PCM_FORMAT_S24_3LE:
  1216. if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  1217. snd_soc_update_bits(dai->codec,
  1218. MSM_SDW_TOP_TX_I2S_CTL, 0x20, 0x00);
  1219. else
  1220. snd_soc_update_bits(dai->codec,
  1221. MSM_SDW_TOP_RX_I2S_CTL, 0x20, 0x00);
  1222. break;
  1223. default:
  1224. dev_err(dai->codec->dev, "%s: wrong format selected\n",
  1225. __func__);
  1226. return -EINVAL;
  1227. }
  1228. return 0;
  1229. }
  1230. static void msm_sdw_shutdown(struct snd_pcm_substream *substream,
  1231. struct snd_soc_dai *dai)
  1232. {
  1233. dev_dbg(dai->codec->dev,
  1234. "%s(): substream = %s stream = %d\n", __func__,
  1235. substream->name, substream->stream);
  1236. }
  1237. static ssize_t msm_sdw_codec_version_read(struct snd_info_entry *entry,
  1238. void *file_private_data,
  1239. struct file *file,
  1240. char __user *buf, size_t count,
  1241. loff_t pos)
  1242. {
  1243. struct msm_sdw_priv *msm_sdw;
  1244. char buffer[MSM_SDW_VERSION_ENTRY_SIZE];
  1245. int len = 0;
  1246. msm_sdw = (struct msm_sdw_priv *) entry->private_data;
  1247. if (!msm_sdw) {
  1248. pr_err("%s: msm_sdw priv is null\n", __func__);
  1249. return -EINVAL;
  1250. }
  1251. switch (msm_sdw->version) {
  1252. case MSM_SDW_VERSION_1_0:
  1253. len = snprintf(buffer, sizeof(buffer), "SDW-CDC_1_0\n");
  1254. break;
  1255. default:
  1256. len = snprintf(buffer, sizeof(buffer), "VER_UNDEFINED\n");
  1257. }
  1258. return simple_read_from_buffer(buf, count, &pos, buffer, len);
  1259. }
  1260. static struct snd_info_entry_ops msm_sdw_codec_info_ops = {
  1261. .read = msm_sdw_codec_version_read,
  1262. };
  1263. /*
  1264. * msm_sdw_codec_info_create_codec_entry - creates msm_sdw module
  1265. * @codec_root: The parent directory
  1266. * @codec: Codec instance
  1267. *
  1268. * Creates msm_sdw module and version entry under the given
  1269. * parent directory.
  1270. *
  1271. * Return: 0 on success or negative error code on failure.
  1272. */
  1273. int msm_sdw_codec_info_create_codec_entry(struct snd_info_entry *codec_root,
  1274. struct snd_soc_codec *codec)
  1275. {
  1276. struct snd_info_entry *version_entry;
  1277. struct msm_sdw_priv *msm_sdw;
  1278. struct snd_soc_card *card;
  1279. char name[80];
  1280. if (!codec_root || !codec)
  1281. return -EINVAL;
  1282. msm_sdw = snd_soc_codec_get_drvdata(codec);
  1283. card = codec->component.card;
  1284. snprintf(name, sizeof(name), "%x.%s", (u32)msm_sdw->sdw_base_addr,
  1285. "msm-sdw-codec");
  1286. msm_sdw->entry = snd_info_create_subdir(codec_root->module,
  1287. (const char *)name,
  1288. codec_root);
  1289. if (!msm_sdw->entry) {
  1290. dev_err(codec->dev, "%s: failed to create msm_sdw entry\n",
  1291. __func__);
  1292. return -ENOMEM;
  1293. }
  1294. version_entry = snd_info_create_card_entry(card->snd_card,
  1295. "version",
  1296. msm_sdw->entry);
  1297. if (!version_entry) {
  1298. dev_err(codec->dev, "%s: failed to create msm_sdw version entry\n",
  1299. __func__);
  1300. return -ENOMEM;
  1301. }
  1302. version_entry->private_data = msm_sdw;
  1303. version_entry->size = MSM_SDW_VERSION_ENTRY_SIZE;
  1304. version_entry->content = SNDRV_INFO_CONTENT_DATA;
  1305. version_entry->c.ops = &msm_sdw_codec_info_ops;
  1306. if (snd_info_register(version_entry) < 0) {
  1307. snd_info_free_entry(version_entry);
  1308. return -ENOMEM;
  1309. }
  1310. msm_sdw->version_entry = version_entry;
  1311. return 0;
  1312. }
  1313. EXPORT_SYMBOL(msm_sdw_codec_info_create_codec_entry);
  1314. static struct snd_soc_dai_ops msm_sdw_dai_ops = {
  1315. .startup = msm_sdw_startup,
  1316. .shutdown = msm_sdw_shutdown,
  1317. .hw_params = msm_sdw_hw_params,
  1318. };
  1319. static struct snd_soc_dai_driver msm_sdw_dai[] = {
  1320. {
  1321. .name = "msm_sdw_i2s_rx1",
  1322. .id = AIF1_SDW_PB,
  1323. .playback = {
  1324. .stream_name = "AIF1_SDW Playback",
  1325. .rates = MSM_SDW_RATES,
  1326. .formats = MSM_SDW_FORMATS,
  1327. .rate_max = 192000,
  1328. .rate_min = 8000,
  1329. .channels_min = 1,
  1330. .channels_max = 4,
  1331. },
  1332. .ops = &msm_sdw_dai_ops,
  1333. },
  1334. {
  1335. .name = "msm_sdw_vifeedback",
  1336. .id = AIF1_SDW_VIFEED,
  1337. .capture = {
  1338. .stream_name = "VIfeed_SDW",
  1339. .rates = MSM_SDW_RATES,
  1340. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  1341. .rate_max = 48000,
  1342. .rate_min = 8000,
  1343. .channels_min = 2,
  1344. .channels_max = 4,
  1345. },
  1346. .ops = &msm_sdw_dai_ops,
  1347. },
  1348. };
  1349. static const char * const rx_mix1_text[] = {
  1350. "ZERO", "RX4", "RX5"
  1351. };
  1352. static const char * const msm_sdw_ear_spkr_pa_gain_text[] = {
  1353. "G_DEFAULT", "G_0_DB", "G_1_DB", "G_2_DB", "G_3_DB",
  1354. "G_4_DB", "G_5_DB", "G_6_DB"
  1355. };
  1356. static const char * const msm_sdw_speaker_boost_stage_text[] = {
  1357. "NO_MAX_STATE", "MAX_STATE_1", "MAX_STATE_2"
  1358. };
  1359. static SOC_ENUM_SINGLE_EXT_DECL(msm_sdw_ear_spkr_pa_gain_enum,
  1360. msm_sdw_ear_spkr_pa_gain_text);
  1361. static SOC_ENUM_SINGLE_EXT_DECL(msm_sdw_spkr_boost_stage_enum,
  1362. msm_sdw_speaker_boost_stage_text);
  1363. /* RX4 MIX1 */
  1364. static const struct soc_enum rx4_mix1_inp1_chain_enum =
  1365. SOC_ENUM_SINGLE(MSM_SDW_TOP_RX7_PATH_INPUT0_MUX,
  1366. 0, 3, rx_mix1_text);
  1367. static const struct soc_enum rx4_mix1_inp2_chain_enum =
  1368. SOC_ENUM_SINGLE(MSM_SDW_TOP_RX7_PATH_INPUT1_MUX,
  1369. 0, 3, rx_mix1_text);
  1370. /* RX5 MIX1 */
  1371. static const struct soc_enum rx5_mix1_inp1_chain_enum =
  1372. SOC_ENUM_SINGLE(MSM_SDW_TOP_RX8_PATH_INPUT0_MUX,
  1373. 0, 3, rx_mix1_text);
  1374. static const struct soc_enum rx5_mix1_inp2_chain_enum =
  1375. SOC_ENUM_SINGLE(MSM_SDW_TOP_RX8_PATH_INPUT1_MUX,
  1376. 0, 3, rx_mix1_text);
  1377. static const struct snd_kcontrol_new rx4_mix1_inp1_mux =
  1378. SOC_DAPM_ENUM("RX4 MIX1 INP1 Mux", rx4_mix1_inp1_chain_enum);
  1379. static const struct snd_kcontrol_new rx4_mix1_inp2_mux =
  1380. SOC_DAPM_ENUM("RX4 MIX1 INP2 Mux", rx4_mix1_inp2_chain_enum);
  1381. static const struct snd_kcontrol_new rx5_mix1_inp1_mux =
  1382. SOC_DAPM_ENUM("RX5 MIX1 INP1 Mux", rx5_mix1_inp1_chain_enum);
  1383. static const struct snd_kcontrol_new rx5_mix1_inp2_mux =
  1384. SOC_DAPM_ENUM("RX5 MIX1 INP2 Mux", rx5_mix1_inp2_chain_enum);
  1385. static const struct snd_kcontrol_new aif1_vi_mixer[] = {
  1386. SOC_SINGLE_EXT("SPKR_VI_1", SND_SOC_NOPM, MSM_SDW_TX0, 1, 0,
  1387. msm_sdw_vi_feed_mixer_get, msm_sdw_vi_feed_mixer_put),
  1388. SOC_SINGLE_EXT("SPKR_VI_2", SND_SOC_NOPM, MSM_SDW_TX1, 1, 0,
  1389. msm_sdw_vi_feed_mixer_get, msm_sdw_vi_feed_mixer_put),
  1390. };
  1391. static const struct snd_soc_dapm_widget msm_sdw_dapm_widgets[] = {
  1392. SND_SOC_DAPM_AIF_IN("I2S RX4", "AIF1_SDW Playback", 0,
  1393. SND_SOC_NOPM, 0, 0),
  1394. SND_SOC_DAPM_AIF_IN("I2S RX5", "AIF1_SDW Playback", 0,
  1395. SND_SOC_NOPM, 0, 0),
  1396. SND_SOC_DAPM_AIF_OUT_E("AIF1_SDW VI", "VIfeed_SDW", 0, SND_SOC_NOPM,
  1397. AIF1_SDW_VIFEED, 0, msm_sdw_codec_enable_vi_feedback,
  1398. SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
  1399. SND_SOC_DAPM_MIXER("AIF1_VI_SDW Mixer", SND_SOC_NOPM, AIF1_SDW_VIFEED,
  1400. 0, aif1_vi_mixer, ARRAY_SIZE(aif1_vi_mixer)),
  1401. SND_SOC_DAPM_MUX_E("RX4 MIX1 INP1", SND_SOC_NOPM, 0, 0,
  1402. &rx4_mix1_inp1_mux, msm_sdw_enable_swr,
  1403. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  1404. SND_SOC_DAPM_MUX_E("RX4 MIX1 INP2", SND_SOC_NOPM, 0, 0,
  1405. &rx4_mix1_inp2_mux, msm_sdw_enable_swr,
  1406. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  1407. SND_SOC_DAPM_MUX_E("RX5 MIX1 INP1", SND_SOC_NOPM, 0, 0,
  1408. &rx5_mix1_inp1_mux, msm_sdw_enable_swr,
  1409. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  1410. SND_SOC_DAPM_MUX_E("RX5 MIX1 INP2", SND_SOC_NOPM, 0, 0,
  1411. &rx5_mix1_inp2_mux, msm_sdw_enable_swr,
  1412. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  1413. SND_SOC_DAPM_MIXER("RX4 MIX1", SND_SOC_NOPM, 0, 0, NULL, 0),
  1414. SND_SOC_DAPM_MIXER("RX5 MIX1", SND_SOC_NOPM, 0, 0, NULL, 0),
  1415. SND_SOC_DAPM_MIXER_E("RX INT4 INTERP", SND_SOC_NOPM,
  1416. COMP1, 0, NULL, 0, msm_sdw_codec_enable_interpolator,
  1417. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
  1418. SND_SOC_DAPM_POST_PMD),
  1419. SND_SOC_DAPM_MIXER_E("RX INT5 INTERP", SND_SOC_NOPM,
  1420. COMP2, 0, NULL, 0, msm_sdw_codec_enable_interpolator,
  1421. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
  1422. SND_SOC_DAPM_POST_PMD),
  1423. SND_SOC_DAPM_MIXER_E("RX INT4 CHAIN", SND_SOC_NOPM, 0, 0,
  1424. NULL, 0, msm_sdw_codec_spk_boost_event,
  1425. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  1426. SND_SOC_DAPM_MIXER_E("RX INT5 CHAIN", SND_SOC_NOPM, 0, 0,
  1427. NULL, 0, msm_sdw_codec_spk_boost_event,
  1428. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  1429. SND_SOC_DAPM_INPUT("VIINPUT_SDW"),
  1430. SND_SOC_DAPM_OUTPUT("SPK1 OUT"),
  1431. SND_SOC_DAPM_OUTPUT("SPK2 OUT"),
  1432. SND_SOC_DAPM_SUPPLY_S("SDW_CONN", -1, MSM_SDW_TOP_I2S_CLK,
  1433. 0, 0, NULL, 0),
  1434. SND_SOC_DAPM_SUPPLY_S("INT_MCLK1", -2, SND_SOC_NOPM, 0, 0,
  1435. msm_int_mclk1_event, SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  1436. SND_SOC_DAPM_SUPPLY("SDW_RX_I2S_CLK",
  1437. MSM_SDW_TOP_RX_I2S_CTL, 0, 0, NULL, 0),
  1438. SND_SOC_DAPM_SUPPLY("SDW_TX_I2S_CLK",
  1439. MSM_SDW_TOP_TX_I2S_CTL, 0, 0, NULL, 0),
  1440. };
  1441. static const struct snd_kcontrol_new msm_sdw_snd_controls[] = {
  1442. SOC_ENUM_EXT("EAR SPKR PA Gain", msm_sdw_ear_spkr_pa_gain_enum,
  1443. msm_sdw_ear_spkr_pa_gain_get,
  1444. msm_sdw_ear_spkr_pa_gain_put),
  1445. SOC_ENUM_EXT("SPKR Left Boost Max State",
  1446. msm_sdw_spkr_boost_stage_enum,
  1447. msm_sdw_spkr_left_boost_stage_get,
  1448. msm_sdw_spkr_left_boost_stage_put),
  1449. SOC_ENUM_EXT("SPKR Right Boost Max State",
  1450. msm_sdw_spkr_boost_stage_enum,
  1451. msm_sdw_spkr_right_boost_stage_get,
  1452. msm_sdw_spkr_right_boost_stage_put),
  1453. SOC_SINGLE_SX_TLV("RX4 Digital Volume", MSM_SDW_RX7_RX_VOL_CTL,
  1454. 0, -84, 40, digital_gain),
  1455. SOC_SINGLE_SX_TLV("RX5 Digital Volume", MSM_SDW_RX8_RX_VOL_CTL,
  1456. 0, -84, 40, digital_gain),
  1457. SOC_SINGLE_EXT("COMP1 Switch", SND_SOC_NOPM, COMP1, 1, 0,
  1458. msm_sdw_get_compander, msm_sdw_set_compander),
  1459. SOC_SINGLE_EXT("COMP2 Switch", SND_SOC_NOPM, COMP2, 1, 0,
  1460. msm_sdw_get_compander, msm_sdw_set_compander),
  1461. };
  1462. static const struct snd_soc_dapm_route audio_map[] = {
  1463. {"AIF1_SDW VI", NULL, "SDW_TX_I2S_CLK"},
  1464. {"SDW_TX_I2S_CLK", NULL, "INT_MCLK1"},
  1465. {"SDW_TX_I2S_CLK", NULL, "SDW_CONN"},
  1466. /* VI Feedback */
  1467. {"AIF1_VI_SDW Mixer", "SPKR_VI_1", "VIINPUT_SDW"},
  1468. {"AIF1_VI_SDW Mixer", "SPKR_VI_2", "VIINPUT_SDW"},
  1469. {"AIF1_SDW VI", NULL, "AIF1_VI_SDW Mixer"},
  1470. {"SDW_RX_I2S_CLK", NULL, "INT_MCLK1"},
  1471. {"SDW_RX_I2S_CLK", NULL, "SDW_CONN"},
  1472. {"I2S RX4", NULL, "SDW_RX_I2S_CLK"},
  1473. {"I2S RX5", NULL, "SDW_RX_I2S_CLK"},
  1474. {"RX4 MIX1 INP1", "RX4", "I2S RX4"},
  1475. {"RX4 MIX1 INP1", "RX5", "I2S RX5"},
  1476. {"RX4 MIX1 INP2", "RX4", "I2S RX4"},
  1477. {"RX4 MIX1 INP2", "RX5", "I2S RX5"},
  1478. {"RX5 MIX1 INP1", "RX4", "I2S RX4"},
  1479. {"RX5 MIX1 INP1", "RX5", "I2S RX5"},
  1480. {"RX5 MIX1 INP2", "RX4", "I2S RX4"},
  1481. {"RX5 MIX1 INP2", "RX5", "I2S RX5"},
  1482. {"RX4 MIX1", NULL, "RX4 MIX1 INP1"},
  1483. {"RX4 MIX1", NULL, "RX4 MIX1 INP2"},
  1484. {"RX5 MIX1", NULL, "RX5 MIX1 INP1"},
  1485. {"RX5 MIX1", NULL, "RX5 MIX1 INP2"},
  1486. {"RX INT4 INTERP", NULL, "RX4 MIX1"},
  1487. {"RX INT4 CHAIN", NULL, "RX INT4 INTERP"},
  1488. {"SPK1 OUT", NULL, "RX INT4 CHAIN"},
  1489. {"RX INT5 INTERP", NULL, "RX5 MIX1"},
  1490. {"RX INT5 CHAIN", NULL, "RX INT5 INTERP"},
  1491. {"SPK2 OUT", NULL, "RX INT5 CHAIN"},
  1492. };
  1493. static const struct msm_sdw_reg_mask_val msm_sdw_reg_init[] = {
  1494. {MSM_SDW_BOOST0_BOOST_CFG1, 0x3F, 0x12},
  1495. {MSM_SDW_BOOST0_BOOST_CFG2, 0x1C, 0x08},
  1496. {MSM_SDW_COMPANDER7_CTL7, 0x1E, 0x18},
  1497. {MSM_SDW_BOOST1_BOOST_CFG1, 0x3F, 0x12},
  1498. {MSM_SDW_BOOST1_BOOST_CFG2, 0x1C, 0x08},
  1499. {MSM_SDW_COMPANDER8_CTL7, 0x1E, 0x18},
  1500. {MSM_SDW_BOOST0_BOOST_CTL, 0x70, 0x58},
  1501. {MSM_SDW_BOOST1_BOOST_CTL, 0x70, 0x58},
  1502. {MSM_SDW_RX7_RX_PATH_CFG1, 0x08, 0x08},
  1503. {MSM_SDW_RX8_RX_PATH_CFG1, 0x08, 0x08},
  1504. {MSM_SDW_TOP_TOP_CFG1, 0x02, 0x02},
  1505. {MSM_SDW_TOP_TOP_CFG1, 0x01, 0x01},
  1506. {MSM_SDW_TX9_SPKR_PROT_PATH_CFG0, 0x01, 0x01},
  1507. {MSM_SDW_TX10_SPKR_PROT_PATH_CFG0, 0x01, 0x01},
  1508. {MSM_SDW_TX11_SPKR_PROT_PATH_CFG0, 0x01, 0x01},
  1509. {MSM_SDW_TX12_SPKR_PROT_PATH_CFG0, 0x01, 0x01},
  1510. {MSM_SDW_COMPANDER7_CTL3, 0x80, 0x80},
  1511. {MSM_SDW_COMPANDER8_CTL3, 0x80, 0x80},
  1512. {MSM_SDW_COMPANDER7_CTL7, 0x01, 0x01},
  1513. {MSM_SDW_COMPANDER8_CTL7, 0x01, 0x01},
  1514. {MSM_SDW_RX7_RX_PATH_CFG0, 0x01, 0x01},
  1515. {MSM_SDW_RX8_RX_PATH_CFG0, 0x01, 0x01},
  1516. {MSM_SDW_RX7_RX_PATH_MIX_CFG, 0x01, 0x01},
  1517. {MSM_SDW_RX8_RX_PATH_MIX_CFG, 0x01, 0x01},
  1518. };
  1519. static void msm_sdw_init_reg(struct snd_soc_codec *codec)
  1520. {
  1521. int i;
  1522. for (i = 0; i < ARRAY_SIZE(msm_sdw_reg_init); i++)
  1523. snd_soc_update_bits(codec,
  1524. msm_sdw_reg_init[i].reg,
  1525. msm_sdw_reg_init[i].mask,
  1526. msm_sdw_reg_init[i].val);
  1527. }
  1528. static int msm_sdw_notifier_service_cb(struct notifier_block *nb,
  1529. unsigned long opcode, void *ptr)
  1530. {
  1531. int i;
  1532. struct msm_sdw_priv *msm_sdw = container_of(nb,
  1533. struct msm_sdw_priv,
  1534. service_nb);
  1535. bool adsp_ready = false;
  1536. unsigned long timeout;
  1537. static bool initial_boot = true;
  1538. pr_debug("%s: Service opcode 0x%lx\n", __func__, opcode);
  1539. mutex_lock(&msm_sdw->codec_mutex);
  1540. switch (opcode) {
  1541. case AUDIO_NOTIFIER_SERVICE_DOWN:
  1542. if (initial_boot) {
  1543. initial_boot = false;
  1544. break;
  1545. }
  1546. msm_sdw->int_mclk1_enabled = false;
  1547. msm_sdw->dev_up = false;
  1548. for (i = 0; i < msm_sdw->nr; i++)
  1549. swrm_wcd_notify(msm_sdw->sdw_ctrl_data[i].sdw_pdev,
  1550. SWR_DEVICE_DOWN, NULL);
  1551. break;
  1552. case AUDIO_NOTIFIER_SERVICE_UP:
  1553. if (initial_boot)
  1554. initial_boot = false;
  1555. if (!q6core_is_adsp_ready()) {
  1556. dev_dbg(msm_sdw->dev, "ADSP isn't ready\n");
  1557. timeout = jiffies +
  1558. msecs_to_jiffies(ADSP_STATE_READY_TIMEOUT_MS);
  1559. while (!time_after(jiffies, timeout)) {
  1560. if (!q6core_is_adsp_ready()) {
  1561. dev_dbg(msm_sdw->dev,
  1562. "ADSP isn't ready\n");
  1563. } else {
  1564. dev_dbg(msm_sdw->dev,
  1565. "ADSP is ready\n");
  1566. adsp_ready = true;
  1567. goto powerup;
  1568. }
  1569. }
  1570. } else {
  1571. adsp_ready = true;
  1572. dev_dbg(msm_sdw->dev, "%s: DSP is ready\n", __func__);
  1573. }
  1574. powerup:
  1575. if (adsp_ready) {
  1576. msm_sdw->dev_up = true;
  1577. msm_sdw_init_reg(msm_sdw->codec);
  1578. regcache_mark_dirty(msm_sdw->regmap);
  1579. regcache_sync(msm_sdw->regmap);
  1580. msm_sdw_set_spkr_mode(msm_sdw->codec,
  1581. msm_sdw->spkr_mode);
  1582. }
  1583. break;
  1584. default:
  1585. break;
  1586. }
  1587. mutex_unlock(&msm_sdw->codec_mutex);
  1588. return NOTIFY_OK;
  1589. }
  1590. static int msm_sdw_codec_probe(struct snd_soc_codec *codec)
  1591. {
  1592. struct msm_sdw_priv *msm_sdw;
  1593. int i, ret;
  1594. msm_sdw = snd_soc_codec_get_drvdata(codec);
  1595. if (!msm_sdw) {
  1596. pr_err("%s:SDW priv data null\n", __func__);
  1597. return -EINVAL;
  1598. }
  1599. msm_sdw->codec = codec;
  1600. for (i = 0; i < COMP_MAX; i++)
  1601. msm_sdw->comp_enabled[i] = 0;
  1602. msm_sdw->spkr_gain_offset = RX_GAIN_OFFSET_0_DB;
  1603. msm_sdw_init_reg(codec);
  1604. msm_sdw->version = MSM_SDW_VERSION_1_0;
  1605. msm_sdw->service_nb.notifier_call = msm_sdw_notifier_service_cb;
  1606. ret = audio_notifier_register("msm_sdw",
  1607. AUDIO_NOTIFIER_ADSP_DOMAIN,
  1608. &msm_sdw->service_nb);
  1609. if (ret < 0)
  1610. dev_err(msm_sdw->dev,
  1611. "%s: Audio notifier register failed ret = %d\n",
  1612. __func__, ret);
  1613. return 0;
  1614. }
  1615. static int msm_sdw_codec_remove(struct snd_soc_codec *codec)
  1616. {
  1617. return 0;
  1618. }
  1619. static struct regmap *msm_sdw_get_regmap(struct device *dev)
  1620. {
  1621. struct msm_sdw_priv *msm_sdw = dev_get_drvdata(dev);
  1622. return msm_sdw->regmap;
  1623. }
  1624. static struct snd_soc_codec_driver soc_codec_dev_msm_sdw = {
  1625. .probe = msm_sdw_codec_probe,
  1626. .remove = msm_sdw_codec_remove,
  1627. .get_regmap = msm_sdw_get_regmap,
  1628. .component_driver = {
  1629. .controls = msm_sdw_snd_controls,
  1630. .num_controls = ARRAY_SIZE(msm_sdw_snd_controls),
  1631. .dapm_widgets = msm_sdw_dapm_widgets,
  1632. .num_dapm_widgets = ARRAY_SIZE(msm_sdw_dapm_widgets),
  1633. .dapm_routes = audio_map,
  1634. .num_dapm_routes = ARRAY_SIZE(audio_map),
  1635. },
  1636. };
  1637. static void msm_sdw_add_child_devices(struct work_struct *work)
  1638. {
  1639. struct msm_sdw_priv *msm_sdw;
  1640. struct platform_device *pdev;
  1641. struct device_node *node;
  1642. struct msm_sdw_ctrl_data *sdw_ctrl_data = NULL, *temp;
  1643. int ret, ctrl_num = 0;
  1644. struct wcd_sdw_ctrl_platform_data *platdata;
  1645. char plat_dev_name[MSM_SDW_STRING_LEN];
  1646. msm_sdw = container_of(work, struct msm_sdw_priv,
  1647. msm_sdw_add_child_devices_work);
  1648. if (!msm_sdw) {
  1649. pr_err("%s: Memory for msm_sdw does not exist\n",
  1650. __func__);
  1651. return;
  1652. }
  1653. if (!msm_sdw->dev->of_node) {
  1654. dev_err(msm_sdw->dev,
  1655. "%s: DT node for msm_sdw does not exist\n", __func__);
  1656. return;
  1657. }
  1658. platdata = &msm_sdw->sdw_plat_data;
  1659. for_each_available_child_of_node(msm_sdw->dev->of_node, node) {
  1660. if (!strcmp(node->name, "swr_master"))
  1661. strlcpy(plat_dev_name, "msm_sdw_swr_ctrl",
  1662. (MSM_SDW_STRING_LEN - 1));
  1663. else if (strnstr(node->name, "msm_cdc_pinctrl",
  1664. strlen("msm_cdc_pinctrl")) != NULL)
  1665. strlcpy(plat_dev_name, node->name,
  1666. (MSM_SDW_STRING_LEN - 1));
  1667. else
  1668. continue;
  1669. pdev = platform_device_alloc(plat_dev_name, -1);
  1670. if (!pdev) {
  1671. dev_err(msm_sdw->dev, "%s: pdev memory alloc failed\n",
  1672. __func__);
  1673. ret = -ENOMEM;
  1674. goto err;
  1675. }
  1676. pdev->dev.parent = msm_sdw->dev;
  1677. pdev->dev.of_node = node;
  1678. if (!strcmp(node->name, "swr_master")) {
  1679. ret = platform_device_add_data(pdev, platdata,
  1680. sizeof(*platdata));
  1681. if (ret) {
  1682. dev_err(&pdev->dev,
  1683. "%s: cannot add plat data ctrl:%d\n",
  1684. __func__, ctrl_num);
  1685. goto fail_pdev_add;
  1686. }
  1687. }
  1688. ret = platform_device_add(pdev);
  1689. if (ret) {
  1690. dev_err(&pdev->dev,
  1691. "%s: Cannot add platform device\n",
  1692. __func__);
  1693. goto fail_pdev_add;
  1694. }
  1695. if (!strcmp(node->name, "swr_master")) {
  1696. temp = krealloc(sdw_ctrl_data,
  1697. (ctrl_num + 1) * sizeof(
  1698. struct msm_sdw_ctrl_data),
  1699. GFP_KERNEL);
  1700. if (!temp) {
  1701. dev_err(&pdev->dev, "out of memory\n");
  1702. ret = -ENOMEM;
  1703. goto err;
  1704. }
  1705. sdw_ctrl_data = temp;
  1706. sdw_ctrl_data[ctrl_num].sdw_pdev = pdev;
  1707. ctrl_num++;
  1708. dev_dbg(&pdev->dev,
  1709. "%s: Added soundwire ctrl device(s)\n",
  1710. __func__);
  1711. msm_sdw->nr = ctrl_num;
  1712. msm_sdw->sdw_ctrl_data = sdw_ctrl_data;
  1713. }
  1714. }
  1715. return;
  1716. fail_pdev_add:
  1717. platform_device_put(pdev);
  1718. err:
  1719. return;
  1720. }
  1721. static int msm_sdw_probe(struct platform_device *pdev)
  1722. {
  1723. int ret = 0;
  1724. struct msm_sdw_priv *msm_sdw;
  1725. int adsp_state;
  1726. adsp_state = apr_get_subsys_state();
  1727. if (adsp_state != APR_SUBSYS_LOADED) {
  1728. dev_err(&pdev->dev, "Adsp is not loaded yet %d\n",
  1729. adsp_state);
  1730. return -EPROBE_DEFER;
  1731. }
  1732. msm_sdw = devm_kzalloc(&pdev->dev, sizeof(struct msm_sdw_priv),
  1733. GFP_KERNEL);
  1734. if (!msm_sdw)
  1735. return -ENOMEM;
  1736. dev_set_drvdata(&pdev->dev, msm_sdw);
  1737. msm_sdw->dev_up = true;
  1738. msm_sdw->dev = &pdev->dev;
  1739. INIT_WORK(&msm_sdw->msm_sdw_add_child_devices_work,
  1740. msm_sdw_add_child_devices);
  1741. msm_sdw->sdw_plat_data.handle = (void *) msm_sdw;
  1742. msm_sdw->sdw_plat_data.read = msm_sdw_swrm_read;
  1743. msm_sdw->sdw_plat_data.write = msm_sdw_swrm_write;
  1744. msm_sdw->sdw_plat_data.bulk_write = msm_sdw_swrm_bulk_write;
  1745. msm_sdw->sdw_plat_data.clk = msm_sdw_swrm_clock;
  1746. msm_sdw->sdw_plat_data.handle_irq = msm_sdwm_handle_irq;
  1747. ret = of_property_read_u32(pdev->dev.of_node, "reg",
  1748. &msm_sdw->sdw_base_addr);
  1749. if (ret) {
  1750. dev_err(&pdev->dev, "%s: could not find %s entry in dt\n",
  1751. __func__, "reg");
  1752. goto err_sdw_cdc;
  1753. }
  1754. msm_sdw->sdw_gpio_p = of_parse_phandle(pdev->dev.of_node,
  1755. "qcom,cdc-sdw-gpios", 0);
  1756. msm_sdw->sdw_base = ioremap(msm_sdw->sdw_base_addr,
  1757. MSM_SDW_MAX_REGISTER);
  1758. msm_sdw->read_dev = __msm_sdw_reg_read;
  1759. msm_sdw->write_dev = __msm_sdw_reg_write;
  1760. msm_sdw->regmap = msm_sdw_regmap_init(msm_sdw->dev,
  1761. &msm_sdw_regmap_config);
  1762. msm_sdw->sdw_irq = platform_get_irq_byname(pdev, "swr_master_irq");
  1763. if (msm_sdw->sdw_irq < 0) {
  1764. dev_err(msm_sdw->dev, "%s() error getting irq handle: %d\n",
  1765. __func__, msm_sdw->sdw_irq);
  1766. ret = -ENODEV;
  1767. goto err_sdw_cdc;
  1768. }
  1769. ret = snd_soc_register_codec(&pdev->dev, &soc_codec_dev_msm_sdw,
  1770. msm_sdw_dai, ARRAY_SIZE(msm_sdw_dai));
  1771. if (ret) {
  1772. dev_err(&pdev->dev, "%s: Codec registration failed, ret = %d\n",
  1773. __func__, ret);
  1774. goto err_sdw_cdc;
  1775. }
  1776. /* initialize the int_mclk1 */
  1777. msm_sdw->sdw_cdc_core_clk.clk_set_minor_version =
  1778. AFE_API_VERSION_I2S_CONFIG;
  1779. msm_sdw->sdw_cdc_core_clk.clk_id =
  1780. Q6AFE_LPASS_CLK_ID_INT_MCLK_1;
  1781. msm_sdw->sdw_cdc_core_clk.clk_freq_in_hz =
  1782. INT_MCLK1_FREQ;
  1783. msm_sdw->sdw_cdc_core_clk.clk_attri =
  1784. Q6AFE_LPASS_CLK_ATTRIBUTE_COUPLE_NO;
  1785. msm_sdw->sdw_cdc_core_clk.clk_root =
  1786. Q6AFE_LPASS_CLK_ROOT_DEFAULT;
  1787. msm_sdw->sdw_cdc_core_clk.enable = 0;
  1788. /* initialize the sdw_npl_clk */
  1789. msm_sdw->sdw_npl_clk.clk_set_minor_version =
  1790. AFE_API_VERSION_I2S_CONFIG;
  1791. msm_sdw->sdw_npl_clk.clk_id =
  1792. AFE_CLOCK_SET_CLOCK_ID_SWR_NPL_CLK;
  1793. msm_sdw->sdw_npl_clk.clk_freq_in_hz = SDW_NPL_FREQ;
  1794. msm_sdw->sdw_npl_clk.clk_attri =
  1795. Q6AFE_LPASS_CLK_ATTRIBUTE_COUPLE_NO;
  1796. msm_sdw->sdw_npl_clk.clk_root =
  1797. Q6AFE_LPASS_CLK_ROOT_DEFAULT;
  1798. msm_sdw->sdw_npl_clk.enable = 0;
  1799. INIT_DELAYED_WORK(&msm_sdw->disable_int_mclk1_work,
  1800. msm_disable_int_mclk1);
  1801. mutex_init(&msm_sdw->cdc_int_mclk1_mutex);
  1802. mutex_init(&msm_sdw->sdw_npl_clk_mutex);
  1803. mutex_init(&msm_sdw->io_lock);
  1804. mutex_init(&msm_sdw->sdw_read_lock);
  1805. mutex_init(&msm_sdw->sdw_write_lock);
  1806. mutex_init(&msm_sdw->sdw_clk_lock);
  1807. mutex_init(&msm_sdw->codec_mutex);
  1808. schedule_work(&msm_sdw->msm_sdw_add_child_devices_work);
  1809. dev_dbg(&pdev->dev, "%s: msm_sdw driver probe done\n", __func__);
  1810. return ret;
  1811. err_sdw_cdc:
  1812. devm_kfree(&pdev->dev, msm_sdw);
  1813. return ret;
  1814. }
  1815. static int msm_sdw_remove(struct platform_device *pdev)
  1816. {
  1817. struct msm_sdw_priv *msm_sdw;
  1818. msm_sdw = dev_get_drvdata(&pdev->dev);
  1819. mutex_destroy(&msm_sdw->io_lock);
  1820. mutex_destroy(&msm_sdw->sdw_read_lock);
  1821. mutex_destroy(&msm_sdw->sdw_write_lock);
  1822. mutex_destroy(&msm_sdw->sdw_clk_lock);
  1823. mutex_destroy(&msm_sdw->codec_mutex);
  1824. mutex_destroy(&msm_sdw->cdc_int_mclk1_mutex);
  1825. devm_kfree(&pdev->dev, msm_sdw);
  1826. snd_soc_unregister_codec(&pdev->dev);
  1827. return 0;
  1828. }
  1829. static const struct of_device_id msm_sdw_codec_dt_match[] = {
  1830. { .compatible = "qcom,msm-sdw-codec", },
  1831. {}
  1832. };
  1833. static struct platform_driver msm_sdw_codec_driver = {
  1834. .probe = msm_sdw_probe,
  1835. .remove = msm_sdw_remove,
  1836. .driver = {
  1837. .name = "msm_sdw_codec",
  1838. .owner = THIS_MODULE,
  1839. .of_match_table = msm_sdw_codec_dt_match,
  1840. },
  1841. };
  1842. module_platform_driver(msm_sdw_codec_driver);
  1843. MODULE_DESCRIPTION("MSM Soundwire Codec driver");
  1844. MODULE_LICENSE("GPL v2");