sdm660-external.c 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882
  1. /* Copyright (c) 2015-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/delay.h>
  13. #include <linux/of_gpio.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/module.h>
  16. #include <sound/soc.h>
  17. #include <sound/pcm.h>
  18. #include <sound/pcm_params.h>
  19. #include <dsp/q6core.h>
  20. #include <dsp/audio_notifier.h>
  21. #include "msm-pcm-routing-v2.h"
  22. #include "sdm660-common.h"
  23. #include "sdm660-external.h"
  24. #include "codecs/wcd9335.h"
  25. #include "codecs/wcd934x/wcd934x.h"
  26. #include "codecs/wcd934x/wcd934x-mbhc.h"
  27. #define SDM660_SPK_ON 1
  28. #define SDM660_SPK_OFF 0
  29. #define WCD9XXX_MBHC_DEF_BUTTONS 8
  30. #define WCD9XXX_MBHC_DEF_RLOADS 5
  31. #define CODEC_EXT_CLK_RATE 9600000
  32. #define ADSP_STATE_READY_TIMEOUT_MS 3000
  33. #define WSA8810_NAME_1 "wsa881x.20170211"
  34. #define WSA8810_NAME_2 "wsa881x.20170212"
  35. static int msm_ext_spk_control = 1;
  36. static struct wcd_mbhc_config *wcd_mbhc_cfg_ptr;
  37. struct msm_asoc_wcd93xx_codec {
  38. void* (*get_afe_config_fn)(struct snd_soc_codec *codec,
  39. enum afe_config_type config_type);
  40. void (*mbhc_hs_detect_exit)(struct snd_soc_codec *codec);
  41. };
  42. static struct msm_asoc_wcd93xx_codec msm_codec_fn;
  43. static struct platform_device *spdev;
  44. static bool is_initial_boot;
  45. static void *def_ext_mbhc_cal(void);
  46. enum {
  47. SLIM_RX_0 = 0,
  48. SLIM_RX_1,
  49. SLIM_RX_2,
  50. SLIM_RX_3,
  51. SLIM_RX_4,
  52. SLIM_RX_5,
  53. SLIM_RX_6,
  54. SLIM_RX_7,
  55. SLIM_RX_MAX,
  56. };
  57. enum {
  58. SLIM_TX_0 = 0,
  59. SLIM_TX_1,
  60. SLIM_TX_2,
  61. SLIM_TX_3,
  62. SLIM_TX_4,
  63. SLIM_TX_5,
  64. SLIM_TX_6,
  65. SLIM_TX_7,
  66. SLIM_TX_8,
  67. SLIM_TX_MAX,
  68. };
  69. struct dev_config {
  70. u32 sample_rate;
  71. u32 bit_format;
  72. u32 channels;
  73. };
  74. /* Default configuration of slimbus channels */
  75. static struct dev_config slim_rx_cfg[] = {
  76. [SLIM_RX_0] = {SAMPLING_RATE_48KHZ, SNDRV_PCM_FORMAT_S16_LE, 1},
  77. [SLIM_RX_1] = {SAMPLING_RATE_48KHZ, SNDRV_PCM_FORMAT_S16_LE, 1},
  78. [SLIM_RX_2] = {SAMPLING_RATE_48KHZ, SNDRV_PCM_FORMAT_S16_LE, 1},
  79. [SLIM_RX_3] = {SAMPLING_RATE_48KHZ, SNDRV_PCM_FORMAT_S16_LE, 1},
  80. [SLIM_RX_4] = {SAMPLING_RATE_48KHZ, SNDRV_PCM_FORMAT_S16_LE, 1},
  81. [SLIM_RX_5] = {SAMPLING_RATE_48KHZ, SNDRV_PCM_FORMAT_S16_LE, 1},
  82. [SLIM_RX_6] = {SAMPLING_RATE_48KHZ, SNDRV_PCM_FORMAT_S16_LE, 1},
  83. [SLIM_RX_7] = {SAMPLING_RATE_8KHZ, SNDRV_PCM_FORMAT_S16_LE, 1},
  84. };
  85. static struct dev_config slim_tx_cfg[] = {
  86. [SLIM_TX_0] = {SAMPLING_RATE_48KHZ, SNDRV_PCM_FORMAT_S16_LE, 1},
  87. [SLIM_TX_1] = {SAMPLING_RATE_48KHZ, SNDRV_PCM_FORMAT_S16_LE, 1},
  88. [SLIM_TX_2] = {SAMPLING_RATE_48KHZ, SNDRV_PCM_FORMAT_S16_LE, 1},
  89. [SLIM_TX_3] = {SAMPLING_RATE_48KHZ, SNDRV_PCM_FORMAT_S16_LE, 1},
  90. [SLIM_TX_4] = {SAMPLING_RATE_48KHZ, SNDRV_PCM_FORMAT_S16_LE, 1},
  91. [SLIM_TX_5] = {SAMPLING_RATE_48KHZ, SNDRV_PCM_FORMAT_S16_LE, 1},
  92. [SLIM_TX_6] = {SAMPLING_RATE_48KHZ, SNDRV_PCM_FORMAT_S16_LE, 1},
  93. [SLIM_TX_7] = {SAMPLING_RATE_8KHZ, SNDRV_PCM_FORMAT_S16_LE, 1},
  94. [SLIM_TX_8] = {SAMPLING_RATE_48KHZ, SNDRV_PCM_FORMAT_S16_LE, 2},
  95. };
  96. static int msm_vi_feed_tx_ch = 2;
  97. static const char *const slim_rx_ch_text[] = {"One", "Two"};
  98. static const char *const slim_tx_ch_text[] = {"One", "Two", "Three", "Four",
  99. "Five", "Six", "Seven",
  100. "Eight"};
  101. static const char *const vi_feed_ch_text[] = {"One", "Two"};
  102. static char const *bit_format_text[] = {"S16_LE", "S24_LE", "S24_3LE",
  103. "S32_LE"};
  104. static char const *slim_sample_rate_text[] = {"KHZ_8", "KHZ_16",
  105. "KHZ_32", "KHZ_44P1", "KHZ_48",
  106. "KHZ_88P2", "KHZ_96", "KHZ_176P4",
  107. "KHZ_192", "KHZ_352P8", "KHZ_384"};
  108. static const char *const spk_function_text[] = {"Off", "On"};
  109. static char const *bt_sample_rate_text[] = {"KHZ_8", "KHZ_16",
  110. "KHZ_44P1", "KHZ_48",
  111. "KHZ_88P2", "KHZ_96"};
  112. static SOC_ENUM_SINGLE_EXT_DECL(spk_func_en, spk_function_text);
  113. static SOC_ENUM_SINGLE_EXT_DECL(slim_0_rx_chs, slim_rx_ch_text);
  114. static SOC_ENUM_SINGLE_EXT_DECL(slim_2_rx_chs, slim_rx_ch_text);
  115. static SOC_ENUM_SINGLE_EXT_DECL(slim_0_tx_chs, slim_tx_ch_text);
  116. static SOC_ENUM_SINGLE_EXT_DECL(slim_1_tx_chs, slim_tx_ch_text);
  117. static SOC_ENUM_SINGLE_EXT_DECL(slim_5_rx_chs, slim_rx_ch_text);
  118. static SOC_ENUM_SINGLE_EXT_DECL(slim_6_rx_chs, slim_rx_ch_text);
  119. static SOC_ENUM_SINGLE_EXT_DECL(vi_feed_tx_chs, vi_feed_ch_text);
  120. static SOC_ENUM_SINGLE_EXT_DECL(slim_0_rx_format, bit_format_text);
  121. static SOC_ENUM_SINGLE_EXT_DECL(slim_5_rx_format, bit_format_text);
  122. static SOC_ENUM_SINGLE_EXT_DECL(slim_6_rx_format, bit_format_text);
  123. static SOC_ENUM_SINGLE_EXT_DECL(slim_0_tx_format, bit_format_text);
  124. static SOC_ENUM_SINGLE_EXT_DECL(slim_0_rx_sample_rate, slim_sample_rate_text);
  125. static SOC_ENUM_SINGLE_EXT_DECL(slim_2_rx_sample_rate, slim_sample_rate_text);
  126. static SOC_ENUM_SINGLE_EXT_DECL(slim_0_tx_sample_rate, slim_sample_rate_text);
  127. static SOC_ENUM_SINGLE_EXT_DECL(slim_5_rx_sample_rate, slim_sample_rate_text);
  128. static SOC_ENUM_SINGLE_EXT_DECL(slim_6_rx_sample_rate, slim_sample_rate_text);
  129. static SOC_ENUM_SINGLE_EXT_DECL(bt_sample_rate, bt_sample_rate_text);
  130. static int slim_get_sample_rate_val(int sample_rate)
  131. {
  132. int sample_rate_val = 0;
  133. switch (sample_rate) {
  134. case SAMPLING_RATE_8KHZ:
  135. sample_rate_val = 0;
  136. break;
  137. case SAMPLING_RATE_16KHZ:
  138. sample_rate_val = 1;
  139. break;
  140. case SAMPLING_RATE_32KHZ:
  141. sample_rate_val = 2;
  142. break;
  143. case SAMPLING_RATE_44P1KHZ:
  144. sample_rate_val = 3;
  145. break;
  146. case SAMPLING_RATE_48KHZ:
  147. sample_rate_val = 4;
  148. break;
  149. case SAMPLING_RATE_88P2KHZ:
  150. sample_rate_val = 5;
  151. break;
  152. case SAMPLING_RATE_96KHZ:
  153. sample_rate_val = 6;
  154. break;
  155. case SAMPLING_RATE_176P4KHZ:
  156. sample_rate_val = 7;
  157. break;
  158. case SAMPLING_RATE_192KHZ:
  159. sample_rate_val = 8;
  160. break;
  161. case SAMPLING_RATE_352P8KHZ:
  162. sample_rate_val = 9;
  163. break;
  164. case SAMPLING_RATE_384KHZ:
  165. sample_rate_val = 10;
  166. break;
  167. default:
  168. sample_rate_val = 4;
  169. break;
  170. }
  171. return sample_rate_val;
  172. }
  173. static int slim_get_sample_rate(int value)
  174. {
  175. int sample_rate = 0;
  176. switch (value) {
  177. case 0:
  178. sample_rate = SAMPLING_RATE_8KHZ;
  179. break;
  180. case 1:
  181. sample_rate = SAMPLING_RATE_16KHZ;
  182. break;
  183. case 2:
  184. sample_rate = SAMPLING_RATE_32KHZ;
  185. break;
  186. case 3:
  187. sample_rate = SAMPLING_RATE_44P1KHZ;
  188. break;
  189. case 4:
  190. sample_rate = SAMPLING_RATE_48KHZ;
  191. break;
  192. case 5:
  193. sample_rate = SAMPLING_RATE_88P2KHZ;
  194. break;
  195. case 6:
  196. sample_rate = SAMPLING_RATE_96KHZ;
  197. break;
  198. case 7:
  199. sample_rate = SAMPLING_RATE_176P4KHZ;
  200. break;
  201. case 8:
  202. sample_rate = SAMPLING_RATE_192KHZ;
  203. break;
  204. case 9:
  205. sample_rate = SAMPLING_RATE_352P8KHZ;
  206. break;
  207. case 10:
  208. sample_rate = SAMPLING_RATE_384KHZ;
  209. break;
  210. default:
  211. sample_rate = SAMPLING_RATE_48KHZ;
  212. break;
  213. }
  214. return sample_rate;
  215. }
  216. static int slim_get_bit_format_val(int bit_format)
  217. {
  218. int val = 0;
  219. switch (bit_format) {
  220. case SNDRV_PCM_FORMAT_S32_LE:
  221. val = 3;
  222. break;
  223. case SNDRV_PCM_FORMAT_S24_3LE:
  224. val = 2;
  225. break;
  226. case SNDRV_PCM_FORMAT_S24_LE:
  227. val = 1;
  228. break;
  229. case SNDRV_PCM_FORMAT_S16_LE:
  230. default:
  231. val = 0;
  232. break;
  233. }
  234. return val;
  235. }
  236. static int slim_get_bit_format(int val)
  237. {
  238. int bit_fmt = SNDRV_PCM_FORMAT_S16_LE;
  239. switch (val) {
  240. case 0:
  241. bit_fmt = SNDRV_PCM_FORMAT_S16_LE;
  242. break;
  243. case 1:
  244. bit_fmt = SNDRV_PCM_FORMAT_S24_LE;
  245. break;
  246. case 2:
  247. bit_fmt = SNDRV_PCM_FORMAT_S24_3LE;
  248. break;
  249. case 3:
  250. bit_fmt = SNDRV_PCM_FORMAT_S32_LE;
  251. break;
  252. default:
  253. bit_fmt = SNDRV_PCM_FORMAT_S16_LE;
  254. break;
  255. }
  256. return bit_fmt;
  257. }
  258. static int slim_get_port_idx(struct snd_kcontrol *kcontrol)
  259. {
  260. int port_id = 0;
  261. if (strnstr(kcontrol->id.name, "SLIM_0_RX", sizeof("SLIM_0_RX")))
  262. port_id = SLIM_RX_0;
  263. else if (strnstr(kcontrol->id.name, "SLIM_2_RX", sizeof("SLIM_2_RX")))
  264. port_id = SLIM_RX_2;
  265. else if (strnstr(kcontrol->id.name, "SLIM_5_RX", sizeof("SLIM_5_RX")))
  266. port_id = SLIM_RX_5;
  267. else if (strnstr(kcontrol->id.name, "SLIM_6_RX", sizeof("SLIM_6_RX")))
  268. port_id = SLIM_RX_6;
  269. else if (strnstr(kcontrol->id.name, "SLIM_0_TX", sizeof("SLIM_0_TX")))
  270. port_id = SLIM_TX_0;
  271. else if (strnstr(kcontrol->id.name, "SLIM_1_TX", sizeof("SLIM_1_TX")))
  272. port_id = SLIM_TX_1;
  273. else {
  274. pr_err("%s: unsupported channel: %s",
  275. __func__, kcontrol->id.name);
  276. return -EINVAL;
  277. }
  278. return port_id;
  279. }
  280. static int msm_bt_sample_rate_get(struct snd_kcontrol *kcontrol,
  281. struct snd_ctl_elem_value *ucontrol)
  282. {
  283. /*
  284. * Slimbus_7_Rx/Tx sample rate values should always be in sync (same)
  285. * when used for BT_SCO use case. Return either Rx or Tx sample rate
  286. * value.
  287. */
  288. switch (slim_rx_cfg[SLIM_RX_7].sample_rate) {
  289. case SAMPLING_RATE_96KHZ:
  290. ucontrol->value.integer.value[0] = 5;
  291. break;
  292. case SAMPLING_RATE_88P2KHZ:
  293. ucontrol->value.integer.value[0] = 4;
  294. break;
  295. case SAMPLING_RATE_48KHZ:
  296. ucontrol->value.integer.value[0] = 3;
  297. break;
  298. case SAMPLING_RATE_44P1KHZ:
  299. ucontrol->value.integer.value[0] = 2;
  300. break;
  301. case SAMPLING_RATE_16KHZ:
  302. ucontrol->value.integer.value[0] = 1;
  303. break;
  304. case SAMPLING_RATE_8KHZ:
  305. default:
  306. ucontrol->value.integer.value[0] = 0;
  307. break;
  308. }
  309. pr_debug("%s: sample rate = %d", __func__,
  310. slim_rx_cfg[SLIM_RX_7].sample_rate);
  311. return 0;
  312. }
  313. static int msm_bt_sample_rate_put(struct snd_kcontrol *kcontrol,
  314. struct snd_ctl_elem_value *ucontrol)
  315. {
  316. switch (ucontrol->value.integer.value[0]) {
  317. case 1:
  318. slim_rx_cfg[SLIM_RX_7].sample_rate = SAMPLING_RATE_16KHZ;
  319. slim_tx_cfg[SLIM_TX_7].sample_rate = SAMPLING_RATE_16KHZ;
  320. break;
  321. case 2:
  322. slim_rx_cfg[SLIM_RX_7].sample_rate = SAMPLING_RATE_44P1KHZ;
  323. slim_tx_cfg[SLIM_TX_7].sample_rate = SAMPLING_RATE_44P1KHZ;
  324. break;
  325. case 3:
  326. slim_rx_cfg[SLIM_RX_7].sample_rate = SAMPLING_RATE_48KHZ;
  327. slim_tx_cfg[SLIM_TX_7].sample_rate = SAMPLING_RATE_48KHZ;
  328. break;
  329. case 4:
  330. slim_rx_cfg[SLIM_RX_7].sample_rate = SAMPLING_RATE_88P2KHZ;
  331. slim_tx_cfg[SLIM_TX_7].sample_rate = SAMPLING_RATE_88P2KHZ;
  332. break;
  333. case 5:
  334. slim_rx_cfg[SLIM_RX_7].sample_rate = SAMPLING_RATE_96KHZ;
  335. slim_tx_cfg[SLIM_TX_7].sample_rate = SAMPLING_RATE_96KHZ;
  336. break;
  337. case 0:
  338. default:
  339. slim_rx_cfg[SLIM_RX_7].sample_rate = SAMPLING_RATE_8KHZ;
  340. slim_tx_cfg[SLIM_TX_7].sample_rate = SAMPLING_RATE_8KHZ;
  341. break;
  342. }
  343. pr_debug("%s: sample rates: slim7_rx = %d, slim7_tx = %d, value = %d\n",
  344. __func__,
  345. slim_rx_cfg[SLIM_RX_7].sample_rate,
  346. slim_tx_cfg[SLIM_TX_7].sample_rate,
  347. ucontrol->value.enumerated.item[0]);
  348. return 0;
  349. }
  350. static int slim_rx_sample_rate_get(struct snd_kcontrol *kcontrol,
  351. struct snd_ctl_elem_value *ucontrol)
  352. {
  353. int ch_num = slim_get_port_idx(kcontrol);
  354. if (ch_num < 0)
  355. return ch_num;
  356. ucontrol->value.enumerated.item[0] =
  357. slim_get_sample_rate_val(slim_rx_cfg[ch_num].sample_rate);
  358. pr_debug("%s: slim[%d]_rx_sample_rate = %d, item = %d\n", __func__,
  359. ch_num, slim_rx_cfg[ch_num].sample_rate,
  360. ucontrol->value.enumerated.item[0]);
  361. return 0;
  362. }
  363. static int slim_rx_sample_rate_put(struct snd_kcontrol *kcontrol,
  364. struct snd_ctl_elem_value *ucontrol)
  365. {
  366. int ch_num = slim_get_port_idx(kcontrol);
  367. if (ch_num < 0)
  368. return ch_num;
  369. slim_rx_cfg[ch_num].sample_rate =
  370. slim_get_sample_rate(ucontrol->value.enumerated.item[0]);
  371. pr_debug("%s: slim[%d]_rx_sample_rate = %d, item = %d\n", __func__,
  372. ch_num, slim_rx_cfg[ch_num].sample_rate,
  373. ucontrol->value.enumerated.item[0]);
  374. return 0;
  375. }
  376. static int slim_tx_sample_rate_get(struct snd_kcontrol *kcontrol,
  377. struct snd_ctl_elem_value *ucontrol)
  378. {
  379. int ch_num = slim_get_port_idx(kcontrol);
  380. if (ch_num < 0)
  381. return ch_num;
  382. ucontrol->value.enumerated.item[0] =
  383. slim_get_sample_rate_val(slim_tx_cfg[ch_num].sample_rate);
  384. pr_debug("%s: slim[%d]_tx_sample_rate = %d, item = %d\n", __func__,
  385. ch_num, slim_tx_cfg[ch_num].sample_rate,
  386. ucontrol->value.enumerated.item[0]);
  387. return 0;
  388. }
  389. static int slim_tx_sample_rate_put(struct snd_kcontrol *kcontrol,
  390. struct snd_ctl_elem_value *ucontrol)
  391. {
  392. int sample_rate = 0;
  393. int ch_num = slim_get_port_idx(kcontrol);
  394. if (ch_num < 0)
  395. return ch_num;
  396. sample_rate = slim_get_sample_rate(ucontrol->value.enumerated.item[0]);
  397. if (sample_rate == SAMPLING_RATE_44P1KHZ) {
  398. pr_err("%s: Unsupported sample rate %d: for Tx path\n",
  399. __func__, sample_rate);
  400. return -EINVAL;
  401. }
  402. slim_tx_cfg[ch_num].sample_rate = sample_rate;
  403. pr_debug("%s: slim[%d]_tx_sample_rate = %d, value = %d\n", __func__,
  404. ch_num, slim_tx_cfg[ch_num].sample_rate,
  405. ucontrol->value.enumerated.item[0]);
  406. return 0;
  407. }
  408. static int slim_rx_bit_format_get(struct snd_kcontrol *kcontrol,
  409. struct snd_ctl_elem_value *ucontrol)
  410. {
  411. int ch_num = slim_get_port_idx(kcontrol);
  412. if (ch_num < 0)
  413. return ch_num;
  414. ucontrol->value.enumerated.item[0] =
  415. slim_get_bit_format_val(slim_rx_cfg[ch_num].bit_format);
  416. pr_debug("%s: slim[%d]_rx_bit_format = %d, ucontrol value = %d\n",
  417. __func__, ch_num, slim_rx_cfg[ch_num].bit_format,
  418. ucontrol->value.enumerated.item[0]);
  419. return 0;
  420. }
  421. static int slim_rx_bit_format_put(struct snd_kcontrol *kcontrol,
  422. struct snd_ctl_elem_value *ucontrol)
  423. {
  424. int ch_num = slim_get_port_idx(kcontrol);
  425. if (ch_num < 0)
  426. return ch_num;
  427. slim_rx_cfg[ch_num].bit_format =
  428. slim_get_bit_format(ucontrol->value.enumerated.item[0]);
  429. pr_debug("%s: slim[%d]_rx_bit_format = %d, ucontrol value = %d\n",
  430. __func__, ch_num, slim_rx_cfg[ch_num].bit_format,
  431. ucontrol->value.enumerated.item[0]);
  432. return 0;
  433. }
  434. static int slim_tx_bit_format_get(struct snd_kcontrol *kcontrol,
  435. struct snd_ctl_elem_value *ucontrol)
  436. {
  437. int ch_num = slim_get_port_idx(kcontrol);
  438. if (ch_num < 0)
  439. return ch_num;
  440. ucontrol->value.enumerated.item[0] =
  441. slim_get_bit_format_val(slim_tx_cfg[ch_num].bit_format);
  442. pr_debug("%s: slim[%d]_tx_bit_format = %d, ucontrol value = %d\n",
  443. __func__, ch_num, slim_tx_cfg[ch_num].bit_format,
  444. ucontrol->value.enumerated.item[0]);
  445. return 0;
  446. }
  447. static int slim_tx_bit_format_put(struct snd_kcontrol *kcontrol,
  448. struct snd_ctl_elem_value *ucontrol)
  449. {
  450. int ch_num = slim_get_port_idx(kcontrol);
  451. if (ch_num < 0)
  452. return ch_num;
  453. slim_tx_cfg[ch_num].bit_format =
  454. slim_get_bit_format(ucontrol->value.enumerated.item[0]);
  455. pr_debug("%s: slim[%d]_tx_bit_format = %d, ucontrol value = %d\n",
  456. __func__, ch_num, slim_tx_cfg[ch_num].bit_format,
  457. ucontrol->value.enumerated.item[0]);
  458. return 0;
  459. }
  460. static int msm_slim_rx_ch_get(struct snd_kcontrol *kcontrol,
  461. struct snd_ctl_elem_value *ucontrol)
  462. {
  463. int ch_num = slim_get_port_idx(kcontrol);
  464. if (ch_num < 0)
  465. return ch_num;
  466. pr_debug("%s: msm_slim_[%d]_rx_ch = %d\n", __func__,
  467. ch_num, slim_rx_cfg[ch_num].channels);
  468. ucontrol->value.enumerated.item[0] = slim_rx_cfg[ch_num].channels - 1;
  469. return 0;
  470. }
  471. static int msm_slim_rx_ch_put(struct snd_kcontrol *kcontrol,
  472. struct snd_ctl_elem_value *ucontrol)
  473. {
  474. int ch_num = slim_get_port_idx(kcontrol);
  475. if (ch_num < 0)
  476. return ch_num;
  477. slim_rx_cfg[ch_num].channels = ucontrol->value.enumerated.item[0] + 1;
  478. pr_debug("%s: msm_slim_[%d]_rx_ch = %d\n", __func__,
  479. ch_num, slim_rx_cfg[ch_num].channels);
  480. return 1;
  481. }
  482. static int msm_slim_tx_ch_get(struct snd_kcontrol *kcontrol,
  483. struct snd_ctl_elem_value *ucontrol)
  484. {
  485. int ch_num = slim_get_port_idx(kcontrol);
  486. if (ch_num < 0)
  487. return ch_num;
  488. pr_debug("%s: msm_slim_[%d]_tx_ch = %d\n", __func__,
  489. ch_num, slim_tx_cfg[ch_num].channels);
  490. ucontrol->value.enumerated.item[0] = slim_tx_cfg[ch_num].channels - 1;
  491. return 0;
  492. }
  493. static int msm_slim_tx_ch_put(struct snd_kcontrol *kcontrol,
  494. struct snd_ctl_elem_value *ucontrol)
  495. {
  496. int ch_num = slim_get_port_idx(kcontrol);
  497. if (ch_num < 0)
  498. return ch_num;
  499. slim_tx_cfg[ch_num].channels = ucontrol->value.enumerated.item[0] + 1;
  500. pr_debug("%s: msm_slim_[%d]_tx_ch = %d\n", __func__,
  501. ch_num, slim_tx_cfg[ch_num].channels);
  502. return 1;
  503. }
  504. static int msm_vi_feed_tx_ch_get(struct snd_kcontrol *kcontrol,
  505. struct snd_ctl_elem_value *ucontrol)
  506. {
  507. ucontrol->value.integer.value[0] = msm_vi_feed_tx_ch - 1;
  508. pr_debug("%s: msm_vi_feed_tx_ch = %ld\n", __func__,
  509. ucontrol->value.integer.value[0]);
  510. return 0;
  511. }
  512. static int msm_vi_feed_tx_ch_put(struct snd_kcontrol *kcontrol,
  513. struct snd_ctl_elem_value *ucontrol)
  514. {
  515. msm_vi_feed_tx_ch = ucontrol->value.integer.value[0] + 1;
  516. pr_debug("%s: msm_vi_feed_tx_ch = %d\n", __func__, msm_vi_feed_tx_ch);
  517. return 1;
  518. }
  519. static void *def_ext_mbhc_cal(void)
  520. {
  521. void *wcd_mbhc_cal;
  522. struct wcd_mbhc_btn_detect_cfg *btn_cfg;
  523. u16 *btn_high;
  524. wcd_mbhc_cal = kzalloc(WCD_MBHC_CAL_SIZE(WCD_MBHC_DEF_BUTTONS,
  525. WCD9XXX_MBHC_DEF_RLOADS), GFP_KERNEL);
  526. if (!wcd_mbhc_cal)
  527. return NULL;
  528. #define S(X, Y) ((WCD_MBHC_CAL_PLUG_TYPE_PTR(wcd_mbhc_cal)->X) = (Y))
  529. S(v_hs_max, 1600);
  530. #undef S
  531. #define S(X, Y) ((WCD_MBHC_CAL_BTN_DET_PTR(wcd_mbhc_cal)->X) = (Y))
  532. S(num_btn, WCD_MBHC_DEF_BUTTONS);
  533. #undef S
  534. btn_cfg = WCD_MBHC_CAL_BTN_DET_PTR(wcd_mbhc_cal);
  535. btn_high = ((void *)&btn_cfg->_v_btn_low) +
  536. (sizeof(btn_cfg->_v_btn_low[0]) * btn_cfg->num_btn);
  537. btn_high[0] = 75;
  538. btn_high[1] = 150;
  539. btn_high[2] = 237;
  540. btn_high[3] = 500;
  541. btn_high[4] = 500;
  542. btn_high[5] = 500;
  543. btn_high[6] = 500;
  544. btn_high[7] = 500;
  545. return wcd_mbhc_cal;
  546. }
  547. static inline int param_is_mask(int p)
  548. {
  549. return (p >= SNDRV_PCM_HW_PARAM_FIRST_MASK) &&
  550. (p <= SNDRV_PCM_HW_PARAM_LAST_MASK);
  551. }
  552. static inline struct snd_mask *param_to_mask(struct snd_pcm_hw_params *p, int n)
  553. {
  554. return &(p->masks[n - SNDRV_PCM_HW_PARAM_FIRST_MASK]);
  555. }
  556. static void msm_ext_control(struct snd_soc_codec *codec)
  557. {
  558. struct snd_soc_dapm_context *dapm =
  559. snd_soc_codec_get_dapm(codec);
  560. pr_debug("%s: msm_ext_spk_control = %d", __func__, msm_ext_spk_control);
  561. if (msm_ext_spk_control == SDM660_SPK_ON) {
  562. snd_soc_dapm_enable_pin(dapm, "Lineout_1 amp");
  563. snd_soc_dapm_enable_pin(dapm, "Lineout_3 amp");
  564. } else {
  565. snd_soc_dapm_disable_pin(dapm, "Lineout_1 amp");
  566. snd_soc_dapm_disable_pin(dapm, "Lineout_3 amp");
  567. }
  568. snd_soc_dapm_sync(dapm);
  569. }
  570. static int msm_ext_get_spk(struct snd_kcontrol *kcontrol,
  571. struct snd_ctl_elem_value *ucontrol)
  572. {
  573. pr_debug("%s: msm_ext_spk_control = %d\n",
  574. __func__, msm_ext_spk_control);
  575. ucontrol->value.integer.value[0] = msm_ext_spk_control;
  576. return 0;
  577. }
  578. static int msm_ext_set_spk(struct snd_kcontrol *kcontrol,
  579. struct snd_ctl_elem_value *ucontrol)
  580. {
  581. struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
  582. pr_debug("%s()\n", __func__);
  583. if (msm_ext_spk_control == ucontrol->value.integer.value[0])
  584. return 0;
  585. msm_ext_spk_control = ucontrol->value.integer.value[0];
  586. msm_ext_control(codec);
  587. return 1;
  588. }
  589. int msm_ext_enable_codec_mclk(struct snd_soc_codec *codec, int enable,
  590. bool dapm)
  591. {
  592. int ret;
  593. pr_debug("%s: enable = %d\n", __func__, enable);
  594. if (!strcmp(dev_name(codec->dev), "tasha_codec"))
  595. ret = tasha_cdc_mclk_enable(codec, enable, dapm);
  596. else if (!strcmp(dev_name(codec->dev), "tavil_codec"))
  597. ret = tavil_cdc_mclk_enable(codec, enable);
  598. else {
  599. dev_err(codec->dev, "%s: unknown codec to enable ext clk\n",
  600. __func__);
  601. ret = -EINVAL;
  602. }
  603. return ret;
  604. }
  605. static const struct snd_kcontrol_new msm_snd_controls[] = {
  606. SOC_ENUM_EXT("Speaker Function", spk_func_en, msm_ext_get_spk,
  607. msm_ext_set_spk),
  608. SOC_ENUM_EXT("SLIM_0_RX Channels", slim_0_rx_chs,
  609. msm_slim_rx_ch_get, msm_slim_rx_ch_put),
  610. SOC_ENUM_EXT("SLIM_2_RX Channels", slim_2_rx_chs,
  611. msm_slim_rx_ch_get, msm_slim_rx_ch_put),
  612. SOC_ENUM_EXT("SLIM_0_TX Channels", slim_0_tx_chs,
  613. msm_slim_tx_ch_get, msm_slim_tx_ch_put),
  614. SOC_ENUM_EXT("SLIM_1_TX Channels", slim_1_tx_chs,
  615. msm_slim_tx_ch_get, msm_slim_tx_ch_put),
  616. SOC_ENUM_EXT("SLIM_5_RX Channels", slim_5_rx_chs,
  617. msm_slim_rx_ch_get, msm_slim_rx_ch_put),
  618. SOC_ENUM_EXT("SLIM_6_RX Channels", slim_6_rx_chs,
  619. msm_slim_rx_ch_get, msm_slim_rx_ch_put),
  620. SOC_ENUM_EXT("VI_FEED_TX Channels", vi_feed_tx_chs,
  621. msm_vi_feed_tx_ch_get, msm_vi_feed_tx_ch_put),
  622. SOC_ENUM_EXT("SLIM_0_RX Format", slim_0_rx_format,
  623. slim_rx_bit_format_get, slim_rx_bit_format_put),
  624. SOC_ENUM_EXT("SLIM_5_RX Format", slim_5_rx_format,
  625. slim_rx_bit_format_get, slim_rx_bit_format_put),
  626. SOC_ENUM_EXT("SLIM_6_RX Format", slim_6_rx_format,
  627. slim_rx_bit_format_get, slim_rx_bit_format_put),
  628. SOC_ENUM_EXT("SLIM_0_TX Format", slim_0_tx_format,
  629. slim_tx_bit_format_get, slim_tx_bit_format_put),
  630. SOC_ENUM_EXT("SLIM_0_RX SampleRate", slim_0_rx_sample_rate,
  631. slim_rx_sample_rate_get, slim_rx_sample_rate_put),
  632. SOC_ENUM_EXT("SLIM_2_RX SampleRate", slim_2_rx_sample_rate,
  633. slim_rx_sample_rate_get, slim_rx_sample_rate_put),
  634. SOC_ENUM_EXT("SLIM_0_TX SampleRate", slim_0_tx_sample_rate,
  635. slim_tx_sample_rate_get, slim_tx_sample_rate_put),
  636. SOC_ENUM_EXT("SLIM_5_RX SampleRate", slim_5_rx_sample_rate,
  637. slim_rx_sample_rate_get, slim_rx_sample_rate_put),
  638. SOC_ENUM_EXT("SLIM_6_RX SampleRate", slim_6_rx_sample_rate,
  639. slim_rx_sample_rate_get, slim_rx_sample_rate_put),
  640. SOC_ENUM_EXT("BT SampleRate", bt_sample_rate,
  641. msm_bt_sample_rate_get,
  642. msm_bt_sample_rate_put),
  643. };
  644. static int msm_slim_get_ch_from_beid(int32_t id)
  645. {
  646. int ch_id = 0;
  647. switch (id) {
  648. case MSM_BACKEND_DAI_SLIMBUS_0_RX:
  649. ch_id = SLIM_RX_0;
  650. break;
  651. case MSM_BACKEND_DAI_SLIMBUS_1_RX:
  652. ch_id = SLIM_RX_1;
  653. break;
  654. case MSM_BACKEND_DAI_SLIMBUS_2_RX:
  655. ch_id = SLIM_RX_2;
  656. break;
  657. case MSM_BACKEND_DAI_SLIMBUS_3_RX:
  658. ch_id = SLIM_RX_3;
  659. break;
  660. case MSM_BACKEND_DAI_SLIMBUS_4_RX:
  661. ch_id = SLIM_RX_4;
  662. break;
  663. case MSM_BACKEND_DAI_SLIMBUS_6_RX:
  664. ch_id = SLIM_RX_6;
  665. break;
  666. case MSM_BACKEND_DAI_SLIMBUS_0_TX:
  667. ch_id = SLIM_TX_0;
  668. break;
  669. case MSM_BACKEND_DAI_SLIMBUS_3_TX:
  670. ch_id = SLIM_TX_3;
  671. break;
  672. default:
  673. ch_id = SLIM_RX_0;
  674. break;
  675. }
  676. return ch_id;
  677. }
  678. static void param_set_mask(struct snd_pcm_hw_params *p, int n, unsigned int bit)
  679. {
  680. if (bit >= SNDRV_MASK_MAX)
  681. return;
  682. if (param_is_mask(n)) {
  683. struct snd_mask *m = param_to_mask(p, n);
  684. m->bits[0] = 0;
  685. m->bits[1] = 0;
  686. m->bits[bit >> 5] |= (1 << (bit & 31));
  687. }
  688. }
  689. /**
  690. * msm_ext_be_hw_params_fixup - updates settings of ALSA BE hw params.
  691. *
  692. * @rtd: runtime dailink instance
  693. * @params: HW params of associated backend dailink.
  694. *
  695. * Returns 0 on success or rc on failure.
  696. */
  697. int msm_ext_be_hw_params_fixup(struct snd_soc_pcm_runtime *rtd,
  698. struct snd_pcm_hw_params *params)
  699. {
  700. struct snd_soc_dai_link *dai_link = rtd->dai_link;
  701. struct snd_interval *rate = hw_param_interval(params,
  702. SNDRV_PCM_HW_PARAM_RATE);
  703. struct snd_interval *channels = hw_param_interval(params,
  704. SNDRV_PCM_HW_PARAM_CHANNELS);
  705. int rc = 0;
  706. int idx;
  707. void *config = NULL;
  708. struct snd_soc_codec *codec = rtd->codec;
  709. pr_debug("%s: format = %d, rate = %d\n",
  710. __func__, params_format(params), params_rate(params));
  711. switch (dai_link->id) {
  712. case MSM_BACKEND_DAI_SLIMBUS_0_RX:
  713. case MSM_BACKEND_DAI_SLIMBUS_1_RX:
  714. case MSM_BACKEND_DAI_SLIMBUS_2_RX:
  715. case MSM_BACKEND_DAI_SLIMBUS_3_RX:
  716. case MSM_BACKEND_DAI_SLIMBUS_4_RX:
  717. case MSM_BACKEND_DAI_SLIMBUS_6_RX:
  718. idx = msm_slim_get_ch_from_beid(dai_link->id);
  719. param_set_mask(params, SNDRV_PCM_HW_PARAM_FORMAT,
  720. slim_rx_cfg[idx].bit_format);
  721. rate->min = rate->max = slim_rx_cfg[idx].sample_rate;
  722. channels->min = channels->max = slim_rx_cfg[idx].channels;
  723. break;
  724. case MSM_BACKEND_DAI_SLIMBUS_0_TX:
  725. case MSM_BACKEND_DAI_SLIMBUS_3_TX:
  726. idx = msm_slim_get_ch_from_beid(dai_link->id);
  727. param_set_mask(params, SNDRV_PCM_HW_PARAM_FORMAT,
  728. slim_tx_cfg[idx].bit_format);
  729. rate->min = rate->max = slim_tx_cfg[idx].sample_rate;
  730. channels->min = channels->max = slim_tx_cfg[idx].channels;
  731. break;
  732. case MSM_BACKEND_DAI_SLIMBUS_1_TX:
  733. param_set_mask(params, SNDRV_PCM_HW_PARAM_FORMAT,
  734. slim_tx_cfg[1].bit_format);
  735. rate->min = rate->max = slim_tx_cfg[1].sample_rate;
  736. channels->min = channels->max = slim_tx_cfg[1].channels;
  737. break;
  738. case MSM_BACKEND_DAI_SLIMBUS_4_TX:
  739. param_set_mask(params, SNDRV_PCM_HW_PARAM_FORMAT,
  740. SNDRV_PCM_FORMAT_S32_LE);
  741. rate->min = rate->max = SAMPLING_RATE_8KHZ;
  742. channels->min = channels->max = msm_vi_feed_tx_ch;
  743. break;
  744. case MSM_BACKEND_DAI_SLIMBUS_5_RX:
  745. param_set_mask(params, SNDRV_PCM_HW_PARAM_FORMAT,
  746. slim_rx_cfg[5].bit_format);
  747. rate->min = rate->max = slim_rx_cfg[5].sample_rate;
  748. channels->min = channels->max = slim_rx_cfg[5].channels;
  749. break;
  750. case MSM_BACKEND_DAI_SLIMBUS_5_TX:
  751. rate->min = rate->max = SAMPLING_RATE_16KHZ;
  752. channels->min = channels->max = 1;
  753. config = msm_codec_fn.get_afe_config_fn(codec,
  754. AFE_SLIMBUS_SLAVE_PORT_CONFIG);
  755. if (config) {
  756. rc = afe_set_config(AFE_SLIMBUS_SLAVE_PORT_CONFIG,
  757. config, SLIMBUS_5_TX);
  758. if (rc)
  759. pr_err("%s: Failed to set slimbus slave port config %d\n",
  760. __func__, rc);
  761. }
  762. break;
  763. case MSM_BACKEND_DAI_SLIMBUS_7_RX:
  764. param_set_mask(params, SNDRV_PCM_HW_PARAM_FORMAT,
  765. slim_rx_cfg[SLIM_RX_7].bit_format);
  766. rate->min = rate->max = slim_rx_cfg[SLIM_RX_7].sample_rate;
  767. channels->min = channels->max =
  768. slim_rx_cfg[SLIM_RX_7].channels;
  769. break;
  770. case MSM_BACKEND_DAI_SLIMBUS_7_TX:
  771. rate->min = rate->max = slim_tx_cfg[SLIM_TX_7].sample_rate;
  772. channels->min = channels->max =
  773. slim_tx_cfg[SLIM_TX_7].channels;
  774. break;
  775. case MSM_BACKEND_DAI_SLIMBUS_8_TX:
  776. rate->min = rate->max = slim_tx_cfg[SLIM_TX_8].sample_rate;
  777. channels->min = channels->max =
  778. slim_tx_cfg[SLIM_TX_8].channels;
  779. break;
  780. default:
  781. rate->min = rate->max = SAMPLING_RATE_48KHZ;
  782. break;
  783. }
  784. return rc;
  785. }
  786. EXPORT_SYMBOL(msm_ext_be_hw_params_fixup);
  787. /**
  788. * msm_snd_hw_params - hw params ops of backend dailink.
  789. *
  790. * @substream: PCM stream of associated backend dailink.
  791. * @params: HW params of associated backend dailink.
  792. *
  793. * Returns 0 on success or ret on failure.
  794. */
  795. int msm_snd_hw_params(struct snd_pcm_substream *substream,
  796. struct snd_pcm_hw_params *params)
  797. {
  798. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  799. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  800. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  801. struct snd_soc_dai_link *dai_link = rtd->dai_link;
  802. int ret = 0;
  803. u32 rx_ch[SLIM_MAX_RX_PORTS], tx_ch[SLIM_MAX_TX_PORTS];
  804. u32 rx_ch_cnt = 0, tx_ch_cnt = 0;
  805. u32 user_set_tx_ch = 0;
  806. u32 rx_ch_count;
  807. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  808. ret = snd_soc_dai_get_channel_map(codec_dai,
  809. &tx_ch_cnt, tx_ch, &rx_ch_cnt, rx_ch);
  810. if (ret < 0) {
  811. pr_err("%s: failed to get codec chan map, err:%d\n",
  812. __func__, ret);
  813. goto err_ch_map;
  814. }
  815. if (dai_link->id == MSM_BACKEND_DAI_SLIMBUS_5_RX) {
  816. pr_debug("%s: rx_5_ch=%d\n", __func__,
  817. slim_rx_cfg[5].channels);
  818. rx_ch_count = slim_rx_cfg[5].channels;
  819. } else if (dai_link->id == MSM_BACKEND_DAI_SLIMBUS_2_RX) {
  820. pr_debug("%s: rx_2_ch=%d\n", __func__,
  821. slim_rx_cfg[2].channels);
  822. rx_ch_count = slim_rx_cfg[2].channels;
  823. } else if (dai_link->id == MSM_BACKEND_DAI_SLIMBUS_6_RX) {
  824. pr_debug("%s: rx_6_ch=%d\n", __func__,
  825. slim_rx_cfg[6].channels);
  826. rx_ch_count = slim_rx_cfg[6].channels;
  827. } else {
  828. pr_debug("%s: rx_0_ch=%d\n", __func__,
  829. slim_rx_cfg[0].channels);
  830. rx_ch_count = slim_rx_cfg[0].channels;
  831. }
  832. ret = snd_soc_dai_set_channel_map(cpu_dai, 0, 0,
  833. rx_ch_count, rx_ch);
  834. if (ret < 0) {
  835. pr_err("%s: failed to set cpu chan map, err:%d\n",
  836. __func__, ret);
  837. goto err_ch_map;
  838. }
  839. } else {
  840. pr_debug("%s: %s_tx_dai_id_%d_ch=%d\n", __func__,
  841. codec_dai->name, codec_dai->id, user_set_tx_ch);
  842. ret = snd_soc_dai_get_channel_map(codec_dai,
  843. &tx_ch_cnt, tx_ch, &rx_ch_cnt, rx_ch);
  844. if (ret < 0) {
  845. pr_err("%s: failed to get codec chan map\n, err:%d\n",
  846. __func__, ret);
  847. goto err_ch_map;
  848. }
  849. /* For <codec>_tx1 case */
  850. if (dai_link->id == MSM_BACKEND_DAI_SLIMBUS_0_TX)
  851. user_set_tx_ch = slim_tx_cfg[0].channels;
  852. /* For <codec>_tx3 case */
  853. else if (dai_link->id == MSM_BACKEND_DAI_SLIMBUS_1_TX)
  854. user_set_tx_ch = slim_tx_cfg[1].channels;
  855. else if (dai_link->id == MSM_BACKEND_DAI_SLIMBUS_4_TX)
  856. user_set_tx_ch = msm_vi_feed_tx_ch;
  857. else
  858. user_set_tx_ch = tx_ch_cnt;
  859. pr_debug("%s: msm_slim_0_tx_ch(%d) user_set_tx_ch(%d) tx_ch_cnt(%d), id (%d)\n",
  860. __func__, slim_tx_cfg[0].channels, user_set_tx_ch,
  861. tx_ch_cnt, dai_link->id);
  862. ret = snd_soc_dai_set_channel_map(cpu_dai,
  863. user_set_tx_ch, tx_ch, 0, 0);
  864. if (ret < 0)
  865. pr_err("%s: failed to set cpu chan map, err:%d\n",
  866. __func__, ret);
  867. }
  868. err_ch_map:
  869. return ret;
  870. }
  871. EXPORT_SYMBOL(msm_snd_hw_params);
  872. /**
  873. * msm_ext_slimbus_2_hw_params - hw params ops of slimbus_2 BE.
  874. *
  875. * @substream: PCM stream of associated backend dailink.
  876. * @params: HW params of associated backend dailink.
  877. *
  878. * Returns 0 on success or ret on failure.
  879. */
  880. int msm_ext_slimbus_2_hw_params(struct snd_pcm_substream *substream,
  881. struct snd_pcm_hw_params *params)
  882. {
  883. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  884. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  885. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  886. int ret = 0;
  887. unsigned int rx_ch[SLIM_MAX_RX_PORTS], tx_ch[SLIM_MAX_TX_PORTS];
  888. unsigned int rx_ch_cnt = 0, tx_ch_cnt = 0;
  889. unsigned int num_tx_ch = 0;
  890. unsigned int num_rx_ch = 0;
  891. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  892. num_rx_ch = params_channels(params);
  893. pr_debug("%s: %s rx_dai_id = %d num_ch = %d\n", __func__,
  894. codec_dai->name, codec_dai->id, num_rx_ch);
  895. ret = snd_soc_dai_get_channel_map(codec_dai,
  896. &tx_ch_cnt, tx_ch, &rx_ch_cnt, rx_ch);
  897. if (ret < 0) {
  898. pr_err("%s: failed to get codec chan map, err:%d\n",
  899. __func__, ret);
  900. goto end;
  901. }
  902. ret = snd_soc_dai_set_channel_map(cpu_dai, 0, 0,
  903. num_rx_ch, rx_ch);
  904. if (ret < 0) {
  905. pr_err("%s: failed to set cpu chan map, err:%d\n",
  906. __func__, ret);
  907. goto end;
  908. }
  909. } else {
  910. num_tx_ch = params_channels(params);
  911. pr_debug("%s: %s tx_dai_id = %d num_ch = %d\n", __func__,
  912. codec_dai->name, codec_dai->id, num_tx_ch);
  913. ret = snd_soc_dai_get_channel_map(codec_dai,
  914. &tx_ch_cnt, tx_ch, &rx_ch_cnt, rx_ch);
  915. if (ret < 0) {
  916. pr_err("%s: failed to get codec chan map, err:%d\n",
  917. __func__, ret);
  918. goto end;
  919. }
  920. ret = snd_soc_dai_set_channel_map(cpu_dai,
  921. num_tx_ch, tx_ch, 0, 0);
  922. if (ret < 0) {
  923. pr_err("%s: failed to set cpu chan map, err:%d\n",
  924. __func__, ret);
  925. goto end;
  926. }
  927. }
  928. end:
  929. return ret;
  930. }
  931. EXPORT_SYMBOL(msm_ext_slimbus_2_hw_params);
  932. /**
  933. * msm_snd_cpe_hw_params - hw params ops of CPE backend.
  934. *
  935. * @substream: PCM stream of associated backend dailink.
  936. * @params: HW params of associated backend dailink.
  937. *
  938. * Returns 0 on success or ret on failure.
  939. */
  940. int msm_snd_cpe_hw_params(struct snd_pcm_substream *substream,
  941. struct snd_pcm_hw_params *params)
  942. {
  943. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  944. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  945. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  946. struct snd_soc_dai_link *dai_link = rtd->dai_link;
  947. int ret = 0;
  948. u32 tx_ch[SLIM_MAX_TX_PORTS];
  949. u32 tx_ch_cnt = 0;
  950. if (substream->stream != SNDRV_PCM_STREAM_CAPTURE) {
  951. pr_err("%s: Invalid stream type %d\n",
  952. __func__, substream->stream);
  953. ret = -EINVAL;
  954. goto end;
  955. }
  956. pr_debug("%s: %s_tx_dai_id_%d\n", __func__,
  957. codec_dai->name, codec_dai->id);
  958. ret = snd_soc_dai_get_channel_map(codec_dai,
  959. &tx_ch_cnt, tx_ch, NULL, NULL);
  960. if (ret < 0) {
  961. pr_err("%s: failed to get codec chan map\n, err:%d\n",
  962. __func__, ret);
  963. goto end;
  964. }
  965. pr_debug("%s: tx_ch_cnt(%d) id %d\n",
  966. __func__, tx_ch_cnt, dai_link->id);
  967. ret = snd_soc_dai_set_channel_map(cpu_dai,
  968. tx_ch_cnt, tx_ch, 0, 0);
  969. if (ret < 0) {
  970. pr_err("%s: failed to set cpu chan map, err:%d\n",
  971. __func__, ret);
  972. goto end;
  973. }
  974. end:
  975. return ret;
  976. }
  977. EXPORT_SYMBOL(msm_snd_cpe_hw_params);
  978. static int msm_afe_set_config(struct snd_soc_codec *codec)
  979. {
  980. int rc;
  981. void *config_data;
  982. pr_debug("%s: enter\n", __func__);
  983. if (!msm_codec_fn.get_afe_config_fn) {
  984. dev_err(codec->dev, "%s: codec get afe config not init'ed\n",
  985. __func__);
  986. return -EINVAL;
  987. }
  988. config_data = msm_codec_fn.get_afe_config_fn(codec,
  989. AFE_CDC_REGISTERS_CONFIG);
  990. if (config_data) {
  991. rc = afe_set_config(AFE_CDC_REGISTERS_CONFIG, config_data, 0);
  992. if (rc) {
  993. pr_err("%s: Failed to set codec registers config %d\n",
  994. __func__, rc);
  995. return rc;
  996. }
  997. }
  998. config_data = msm_codec_fn.get_afe_config_fn(codec,
  999. AFE_CDC_REGISTER_PAGE_CONFIG);
  1000. if (config_data) {
  1001. rc = afe_set_config(AFE_CDC_REGISTER_PAGE_CONFIG, config_data,
  1002. 0);
  1003. if (rc)
  1004. pr_err("%s: Failed to set cdc register page config\n",
  1005. __func__);
  1006. }
  1007. config_data = msm_codec_fn.get_afe_config_fn(codec,
  1008. AFE_SLIMBUS_SLAVE_CONFIG);
  1009. if (config_data) {
  1010. rc = afe_set_config(AFE_SLIMBUS_SLAVE_CONFIG, config_data, 0);
  1011. if (rc) {
  1012. pr_err("%s: Failed to set slimbus slave config %d\n",
  1013. __func__, rc);
  1014. return rc;
  1015. }
  1016. }
  1017. config_data = msm_codec_fn.get_afe_config_fn(codec,
  1018. AFE_AANC_VERSION);
  1019. if (config_data) {
  1020. rc = afe_set_config(AFE_AANC_VERSION, config_data, 0);
  1021. if (rc) {
  1022. pr_err("%s: Failed to set AANC version %d\n",
  1023. __func__, rc);
  1024. return rc;
  1025. }
  1026. }
  1027. config_data = msm_codec_fn.get_afe_config_fn(codec,
  1028. AFE_CDC_CLIP_REGISTERS_CONFIG);
  1029. if (config_data) {
  1030. rc = afe_set_config(AFE_CDC_CLIP_REGISTERS_CONFIG,
  1031. config_data, 0);
  1032. if (rc) {
  1033. pr_err("%s: Failed to set clip registers %d\n",
  1034. __func__, rc);
  1035. return rc;
  1036. }
  1037. }
  1038. config_data = msm_codec_fn.get_afe_config_fn(codec,
  1039. AFE_CLIP_BANK_SEL);
  1040. if (config_data) {
  1041. rc = afe_set_config(AFE_CLIP_BANK_SEL,
  1042. config_data, 0);
  1043. if (rc) {
  1044. pr_err("%s: Failed to set AFE bank selection %d\n",
  1045. __func__, rc);
  1046. return rc;
  1047. }
  1048. }
  1049. config_data = msm_codec_fn.get_afe_config_fn(codec,
  1050. AFE_CDC_REGISTER_PAGE_CONFIG);
  1051. if (config_data) {
  1052. rc = afe_set_config(AFE_CDC_REGISTER_PAGE_CONFIG, config_data,
  1053. 0);
  1054. if (rc)
  1055. pr_err("%s: Failed to set cdc register page config\n",
  1056. __func__);
  1057. }
  1058. return 0;
  1059. }
  1060. static void msm_afe_clear_config(void)
  1061. {
  1062. afe_clear_config(AFE_CDC_REGISTERS_CONFIG);
  1063. afe_clear_config(AFE_SLIMBUS_SLAVE_CONFIG);
  1064. }
  1065. static int msm_adsp_power_up_config(struct snd_soc_codec *codec,
  1066. struct snd_card *card)
  1067. {
  1068. int ret = 0;
  1069. unsigned long timeout;
  1070. int adsp_ready = 0;
  1071. bool snd_card_online = 0;
  1072. timeout = jiffies +
  1073. msecs_to_jiffies(ADSP_STATE_READY_TIMEOUT_MS);
  1074. do {
  1075. if (!snd_card_online) {
  1076. snd_card_online = snd_card_is_online_state(card);
  1077. pr_debug("%s: Sound card is %s\n", __func__,
  1078. snd_card_online ? "Online" : "Offline");
  1079. }
  1080. if (!adsp_ready) {
  1081. adsp_ready = q6core_is_adsp_ready();
  1082. pr_debug("%s: ADSP Audio is %s\n", __func__,
  1083. adsp_ready ? "ready" : "not ready");
  1084. }
  1085. if (snd_card_online && adsp_ready)
  1086. break;
  1087. /*
  1088. * Sound card/ADSP will be coming up after subsystem restart and
  1089. * it might not be fully up when the control reaches
  1090. * here. So, wait for 50msec before checking ADSP state
  1091. */
  1092. msleep(50);
  1093. } while (time_after(timeout, jiffies));
  1094. if (!snd_card_online || !adsp_ready) {
  1095. pr_err("%s: Timeout. Sound card is %s, ADSP Audio is %s\n",
  1096. __func__,
  1097. snd_card_online ? "Online" : "Offline",
  1098. adsp_ready ? "ready" : "not ready");
  1099. ret = -ETIMEDOUT;
  1100. goto err_fail;
  1101. }
  1102. ret = msm_afe_set_config(codec);
  1103. if (ret)
  1104. pr_err("%s: Failed to set AFE config. err %d\n",
  1105. __func__, ret);
  1106. return 0;
  1107. err_fail:
  1108. return ret;
  1109. }
  1110. static int sdm660_notifier_service_cb(struct notifier_block *this,
  1111. unsigned long opcode, void *ptr)
  1112. {
  1113. int ret;
  1114. struct snd_soc_card *card = NULL;
  1115. const char *be_dl_name = LPASS_BE_SLIMBUS_0_RX;
  1116. struct snd_soc_pcm_runtime *rtd;
  1117. struct snd_soc_codec *codec;
  1118. pr_debug("%s: Service opcode 0x%lx\n", __func__, opcode);
  1119. switch (opcode) {
  1120. case AUDIO_NOTIFIER_SERVICE_DOWN:
  1121. /*
  1122. * Use flag to ignore initial boot notifications
  1123. * On initial boot msm_adsp_power_up_config is
  1124. * called on init. There is no need to clear
  1125. * and set the config again on initial boot.
  1126. */
  1127. if (is_initial_boot)
  1128. break;
  1129. msm_afe_clear_config();
  1130. break;
  1131. case AUDIO_NOTIFIER_SERVICE_UP:
  1132. if (is_initial_boot) {
  1133. is_initial_boot = false;
  1134. break;
  1135. }
  1136. if (!spdev)
  1137. return -EINVAL;
  1138. card = platform_get_drvdata(spdev);
  1139. rtd = snd_soc_get_pcm_runtime(card, be_dl_name);
  1140. if (!rtd) {
  1141. dev_err(card->dev,
  1142. "%s: snd_soc_get_pcm_runtime for %s failed!\n",
  1143. __func__, be_dl_name);
  1144. ret = -EINVAL;
  1145. goto done;
  1146. }
  1147. codec = rtd->codec;
  1148. ret = msm_adsp_power_up_config(codec, card->snd_card);
  1149. if (ret < 0) {
  1150. dev_err(card->dev,
  1151. "%s: msm_adsp_power_up_config failed ret = %d!\n",
  1152. __func__, ret);
  1153. goto done;
  1154. }
  1155. break;
  1156. default:
  1157. break;
  1158. }
  1159. done:
  1160. return NOTIFY_OK;
  1161. }
  1162. static struct notifier_block service_nb = {
  1163. .notifier_call = sdm660_notifier_service_cb,
  1164. .priority = -INT_MAX,
  1165. };
  1166. static int msm_config_hph_en0_gpio(struct snd_soc_codec *codec, bool high)
  1167. {
  1168. struct snd_soc_card *card = codec->component.card;
  1169. struct msm_asoc_mach_data *pdata;
  1170. int val;
  1171. if (!card)
  1172. return 0;
  1173. pdata = snd_soc_card_get_drvdata(card);
  1174. if (!pdata || !gpio_is_valid(pdata->hph_en0_gpio))
  1175. return 0;
  1176. val = gpio_get_value_cansleep(pdata->hph_en0_gpio);
  1177. if ((!!val) == high)
  1178. return 0;
  1179. gpio_direction_output(pdata->hph_en0_gpio, (int)high);
  1180. return 1;
  1181. }
  1182. static int msm_snd_enable_codec_ext_tx_clk(struct snd_soc_codec *codec,
  1183. int enable, bool dapm)
  1184. {
  1185. int ret = 0;
  1186. if (!strcmp(dev_name(codec->dev), "tasha_codec"))
  1187. ret = tasha_cdc_mclk_tx_enable(codec, enable, dapm);
  1188. else {
  1189. dev_err(codec->dev, "%s: unknown codec to enable ext clk\n",
  1190. __func__);
  1191. ret = -EINVAL;
  1192. }
  1193. return ret;
  1194. }
  1195. static int msm_ext_mclk_tx_event(struct snd_soc_dapm_widget *w,
  1196. struct snd_kcontrol *kcontrol, int event)
  1197. {
  1198. struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
  1199. pr_debug("%s: event = %d\n", __func__, event);
  1200. switch (event) {
  1201. case SND_SOC_DAPM_PRE_PMU:
  1202. return msm_snd_enable_codec_ext_tx_clk(codec, 1, true);
  1203. case SND_SOC_DAPM_POST_PMD:
  1204. return msm_snd_enable_codec_ext_tx_clk(codec, 0, true);
  1205. }
  1206. return 0;
  1207. }
  1208. static int msm_ext_mclk_event(struct snd_soc_dapm_widget *w,
  1209. struct snd_kcontrol *kcontrol, int event)
  1210. {
  1211. struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
  1212. pr_debug("%s: event = %d\n", __func__, event);
  1213. switch (event) {
  1214. case SND_SOC_DAPM_PRE_PMU:
  1215. return msm_ext_enable_codec_mclk(codec, 1, true);
  1216. case SND_SOC_DAPM_POST_PMD:
  1217. return msm_ext_enable_codec_mclk(codec, 0, true);
  1218. }
  1219. return 0;
  1220. }
  1221. static int msm_ext_prepare_hifi(struct msm_asoc_mach_data *pdata)
  1222. {
  1223. int ret = 0;
  1224. if (gpio_is_valid(pdata->hph_en1_gpio)) {
  1225. pr_debug("%s: hph_en1_gpio request %d\n", __func__,
  1226. pdata->hph_en1_gpio);
  1227. ret = gpio_request(pdata->hph_en1_gpio, "hph_en1_gpio");
  1228. if (ret) {
  1229. pr_err("%s: hph_en1_gpio request failed, ret:%d\n",
  1230. __func__, ret);
  1231. goto err;
  1232. }
  1233. }
  1234. if (gpio_is_valid(pdata->hph_en0_gpio)) {
  1235. pr_debug("%s: hph_en0_gpio request %d\n", __func__,
  1236. pdata->hph_en0_gpio);
  1237. ret = gpio_request(pdata->hph_en0_gpio, "hph_en0_gpio");
  1238. if (ret)
  1239. pr_err("%s: hph_en0_gpio request failed, ret:%d\n",
  1240. __func__, ret);
  1241. }
  1242. err:
  1243. return ret;
  1244. }
  1245. static const struct snd_soc_dapm_widget msm_dapm_widgets[] = {
  1246. SND_SOC_DAPM_SUPPLY_S("MCLK", -1, SND_SOC_NOPM, 0, 0,
  1247. msm_ext_mclk_event, SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  1248. SND_SOC_DAPM_SUPPLY_S("MCLK TX", -1, SND_SOC_NOPM, 0, 0,
  1249. msm_ext_mclk_tx_event, SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  1250. SND_SOC_DAPM_SPK("Lineout_1 amp", NULL),
  1251. SND_SOC_DAPM_SPK("Lineout_3 amp", NULL),
  1252. SND_SOC_DAPM_SPK("Lineout_2 amp", NULL),
  1253. SND_SOC_DAPM_SPK("Lineout_4 amp", NULL),
  1254. SND_SOC_DAPM_MIC("Handset Mic", NULL),
  1255. SND_SOC_DAPM_MIC("Headset Mic", NULL),
  1256. SND_SOC_DAPM_MIC("Secondary Mic", NULL),
  1257. SND_SOC_DAPM_MIC("ANCRight Headset Mic", NULL),
  1258. SND_SOC_DAPM_MIC("ANCLeft Headset Mic", NULL),
  1259. SND_SOC_DAPM_MIC("Analog Mic4", NULL),
  1260. SND_SOC_DAPM_MIC("Analog Mic6", NULL),
  1261. SND_SOC_DAPM_MIC("Analog Mic7", NULL),
  1262. SND_SOC_DAPM_MIC("Analog Mic8", NULL),
  1263. SND_SOC_DAPM_MIC("Digital Mic0", NULL),
  1264. SND_SOC_DAPM_MIC("Digital Mic1", NULL),
  1265. SND_SOC_DAPM_MIC("Digital Mic2", NULL),
  1266. SND_SOC_DAPM_MIC("Digital Mic3", NULL),
  1267. SND_SOC_DAPM_MIC("Digital Mic4", NULL),
  1268. SND_SOC_DAPM_MIC("Digital Mic5", NULL),
  1269. SND_SOC_DAPM_MIC("Digital Mic6", NULL),
  1270. };
  1271. static struct snd_soc_dapm_route wcd_audio_paths_tasha[] = {
  1272. {"MIC BIAS1", NULL, "MCLK TX"},
  1273. {"MIC BIAS2", NULL, "MCLK TX"},
  1274. {"MIC BIAS3", NULL, "MCLK TX"},
  1275. {"MIC BIAS4", NULL, "MCLK TX"},
  1276. };
  1277. static struct snd_soc_dapm_route wcd_audio_paths[] = {
  1278. {"MIC BIAS1", NULL, "MCLK"},
  1279. {"MIC BIAS2", NULL, "MCLK"},
  1280. {"MIC BIAS3", NULL, "MCLK"},
  1281. {"MIC BIAS4", NULL, "MCLK"},
  1282. };
  1283. int msm_snd_card_tasha_late_probe(struct snd_soc_card *card)
  1284. {
  1285. const char *be_dl_name = LPASS_BE_SLIMBUS_0_RX;
  1286. struct snd_soc_pcm_runtime *rtd;
  1287. int ret = 0;
  1288. void *mbhc_calibration;
  1289. rtd = snd_soc_get_pcm_runtime(card, be_dl_name);
  1290. if (!rtd) {
  1291. dev_err(card->dev,
  1292. "%s: snd_soc_get_pcm_runtime for %s failed!\n",
  1293. __func__, be_dl_name);
  1294. ret = -EINVAL;
  1295. goto err_pcm_runtime;
  1296. }
  1297. mbhc_calibration = def_ext_mbhc_cal();
  1298. if (!mbhc_calibration) {
  1299. ret = -ENOMEM;
  1300. goto err_mbhc_cal;
  1301. }
  1302. wcd_mbhc_cfg_ptr->calibration = mbhc_calibration;
  1303. ret = tasha_mbhc_hs_detect(rtd->codec, wcd_mbhc_cfg_ptr);
  1304. if (ret) {
  1305. dev_err(card->dev, "%s: mbhc hs detect failed, err:%d\n",
  1306. __func__, ret);
  1307. goto err_hs_detect;
  1308. }
  1309. return 0;
  1310. err_hs_detect:
  1311. kfree(mbhc_calibration);
  1312. err_mbhc_cal:
  1313. err_pcm_runtime:
  1314. return ret;
  1315. }
  1316. int msm_snd_card_tavil_late_probe(struct snd_soc_card *card)
  1317. {
  1318. const char *be_dl_name = LPASS_BE_SLIMBUS_0_RX;
  1319. struct snd_soc_pcm_runtime *rtd;
  1320. int ret = 0;
  1321. void *mbhc_calibration;
  1322. rtd = snd_soc_get_pcm_runtime(card, be_dl_name);
  1323. if (!rtd) {
  1324. dev_err(card->dev,
  1325. "%s: snd_soc_get_pcm_runtime for %s failed!\n",
  1326. __func__, be_dl_name);
  1327. ret = -EINVAL;
  1328. goto err;
  1329. }
  1330. mbhc_calibration = def_ext_mbhc_cal();
  1331. if (!mbhc_calibration) {
  1332. ret = -ENOMEM;
  1333. goto err;
  1334. }
  1335. wcd_mbhc_cfg_ptr->calibration = mbhc_calibration;
  1336. ret = tavil_mbhc_hs_detect(rtd->codec, wcd_mbhc_cfg_ptr);
  1337. if (ret) {
  1338. dev_err(card->dev, "%s: mbhc hs detect failed, err:%d\n",
  1339. __func__, ret);
  1340. goto err_free_mbhc_cal;
  1341. }
  1342. return 0;
  1343. err_free_mbhc_cal:
  1344. kfree(mbhc_calibration);
  1345. err:
  1346. return ret;
  1347. }
  1348. /**
  1349. * msm_audrx_init - Audio init function of sound card instantiate.
  1350. *
  1351. * @rtd: runtime dailink instance
  1352. *
  1353. * Returns 0 on success or ret on failure.
  1354. */
  1355. int msm_audrx_init(struct snd_soc_pcm_runtime *rtd)
  1356. {
  1357. int ret;
  1358. void *config_data;
  1359. struct snd_soc_codec *codec = rtd->codec;
  1360. struct snd_soc_dapm_context *dapm =
  1361. snd_soc_codec_get_dapm(codec);
  1362. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  1363. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  1364. struct snd_soc_component *aux_comp;
  1365. struct snd_card *card;
  1366. struct snd_info_entry *entry;
  1367. struct msm_asoc_mach_data *pdata =
  1368. snd_soc_card_get_drvdata(rtd->card);
  1369. /* Codec SLIMBUS configuration
  1370. * RX1, RX2, RX3, RX4, RX5, RX6, RX7, RX8, RX9, RX10, RX11, RX12, RX13
  1371. * TX1, TX2, TX3, TX4, TX5, TX6, TX7, TX8, TX9, TX10, TX11, TX12, TX13
  1372. * TX14, TX15, TX16
  1373. */
  1374. unsigned int rx_ch[TASHA_RX_MAX] = {144, 145, 146, 147, 148, 149, 150,
  1375. 151, 152, 153, 154, 155, 156};
  1376. unsigned int tx_ch[TASHA_TX_MAX] = {128, 129, 130, 131, 132, 133,
  1377. 134, 135, 136, 137, 138, 139,
  1378. 140, 141, 142, 143};
  1379. /* Tavil Codec SLIMBUS configuration
  1380. * RX1, RX2, RX3, RX4, RX5, RX6, RX7, RX8
  1381. * TX1, TX2, TX3, TX4, TX5, TX6, TX7, TX8, TX9, TX10, TX11, TX12, TX13
  1382. * TX14, TX15, TX16
  1383. */
  1384. unsigned int rx_ch_tavil[WCD934X_RX_MAX] = {144, 145, 146, 147, 148,
  1385. 149, 150, 151};
  1386. unsigned int tx_ch_tavil[WCD934X_TX_MAX] = {128, 129, 130, 131, 132,
  1387. 133, 134, 135, 136, 137, 138,
  1388. 139, 140, 141, 142, 143};
  1389. pr_debug("%s: dev_name%s\n", __func__, dev_name(cpu_dai->dev));
  1390. rtd->pmdown_time = 0;
  1391. ret = snd_soc_add_codec_controls(codec, msm_snd_controls,
  1392. ARRAY_SIZE(msm_snd_controls));
  1393. if (ret < 0) {
  1394. pr_err("%s: add_codec_controls failed: %d\n",
  1395. __func__, ret);
  1396. return ret;
  1397. }
  1398. ret = snd_soc_add_codec_controls(codec, msm_common_snd_controls,
  1399. msm_common_snd_controls_size());
  1400. if (ret < 0) {
  1401. pr_err("%s: add_common_snd_controls failed: %d\n",
  1402. __func__, ret);
  1403. return ret;
  1404. }
  1405. snd_soc_dapm_new_controls(dapm, msm_dapm_widgets,
  1406. ARRAY_SIZE(msm_dapm_widgets));
  1407. if (!strcmp(dev_name(codec_dai->dev), "tasha_codec"))
  1408. snd_soc_dapm_add_routes(dapm, wcd_audio_paths_tasha,
  1409. ARRAY_SIZE(wcd_audio_paths_tasha));
  1410. else
  1411. snd_soc_dapm_add_routes(dapm, wcd_audio_paths,
  1412. ARRAY_SIZE(wcd_audio_paths));
  1413. snd_soc_dapm_enable_pin(dapm, "Lineout_1 amp");
  1414. snd_soc_dapm_enable_pin(dapm, "Lineout_3 amp");
  1415. snd_soc_dapm_enable_pin(dapm, "Lineout_2 amp");
  1416. snd_soc_dapm_enable_pin(dapm, "Lineout_4 amp");
  1417. snd_soc_dapm_ignore_suspend(dapm, "MADINPUT");
  1418. snd_soc_dapm_ignore_suspend(dapm, "MAD_CPE_INPUT");
  1419. snd_soc_dapm_ignore_suspend(dapm, "Handset Mic");
  1420. snd_soc_dapm_ignore_suspend(dapm, "Headset Mic");
  1421. snd_soc_dapm_ignore_suspend(dapm, "Secondary Mic");
  1422. snd_soc_dapm_ignore_suspend(dapm, "Lineout_1 amp");
  1423. snd_soc_dapm_ignore_suspend(dapm, "Lineout_3 amp");
  1424. snd_soc_dapm_ignore_suspend(dapm, "Lineout_2 amp");
  1425. snd_soc_dapm_ignore_suspend(dapm, "Lineout_4 amp");
  1426. snd_soc_dapm_ignore_suspend(dapm, "ANCRight Headset Mic");
  1427. snd_soc_dapm_ignore_suspend(dapm, "ANCLeft Headset Mic");
  1428. snd_soc_dapm_ignore_suspend(dapm, "Digital Mic0");
  1429. snd_soc_dapm_ignore_suspend(dapm, "Digital Mic1");
  1430. snd_soc_dapm_ignore_suspend(dapm, "Digital Mic2");
  1431. snd_soc_dapm_ignore_suspend(dapm, "Digital Mic3");
  1432. snd_soc_dapm_ignore_suspend(dapm, "Digital Mic4");
  1433. snd_soc_dapm_ignore_suspend(dapm, "Digital Mic5");
  1434. snd_soc_dapm_ignore_suspend(dapm, "Analog Mic4");
  1435. snd_soc_dapm_ignore_suspend(dapm, "Analog Mic6");
  1436. snd_soc_dapm_ignore_suspend(dapm, "Analog Mic7");
  1437. snd_soc_dapm_ignore_suspend(dapm, "Analog Mic8");
  1438. snd_soc_dapm_ignore_suspend(dapm, "EAR");
  1439. snd_soc_dapm_ignore_suspend(dapm, "LINEOUT1");
  1440. snd_soc_dapm_ignore_suspend(dapm, "LINEOUT2");
  1441. snd_soc_dapm_ignore_suspend(dapm, "AMIC1");
  1442. snd_soc_dapm_ignore_suspend(dapm, "AMIC2");
  1443. snd_soc_dapm_ignore_suspend(dapm, "AMIC3");
  1444. snd_soc_dapm_ignore_suspend(dapm, "AMIC4");
  1445. snd_soc_dapm_ignore_suspend(dapm, "AMIC5");
  1446. snd_soc_dapm_ignore_suspend(dapm, "DMIC0");
  1447. snd_soc_dapm_ignore_suspend(dapm, "DMIC1");
  1448. snd_soc_dapm_ignore_suspend(dapm, "DMIC2");
  1449. snd_soc_dapm_ignore_suspend(dapm, "DMIC3");
  1450. snd_soc_dapm_ignore_suspend(dapm, "DMIC4");
  1451. snd_soc_dapm_ignore_suspend(dapm, "DMIC5");
  1452. snd_soc_dapm_ignore_suspend(dapm, "ANC EAR");
  1453. snd_soc_dapm_ignore_suspend(dapm, "SPK1 OUT");
  1454. snd_soc_dapm_ignore_suspend(dapm, "SPK2 OUT");
  1455. snd_soc_dapm_ignore_suspend(dapm, "HPHL");
  1456. snd_soc_dapm_ignore_suspend(dapm, "HPHR");
  1457. snd_soc_dapm_ignore_suspend(dapm, "AIF4 VI");
  1458. snd_soc_dapm_ignore_suspend(dapm, "VIINPUT");
  1459. if (!strcmp(dev_name(codec_dai->dev), "tasha_codec")) {
  1460. snd_soc_dapm_ignore_suspend(dapm, "LINEOUT3");
  1461. snd_soc_dapm_ignore_suspend(dapm, "LINEOUT4");
  1462. snd_soc_dapm_ignore_suspend(dapm, "ANC HPHL");
  1463. snd_soc_dapm_ignore_suspend(dapm, "ANC HPHR");
  1464. snd_soc_dapm_ignore_suspend(dapm, "ANC LINEOUT1");
  1465. snd_soc_dapm_ignore_suspend(dapm, "ANC LINEOUT2");
  1466. } else {
  1467. snd_soc_dapm_ignore_suspend(dapm, "MAD_CPE_OUT1");
  1468. snd_soc_dapm_ignore_suspend(dapm, "MAD_CPE_OUT2");
  1469. }
  1470. snd_soc_dapm_sync(dapm);
  1471. if (!strcmp(dev_name(codec_dai->dev), "tavil_codec")) {
  1472. snd_soc_dai_set_channel_map(codec_dai, ARRAY_SIZE(tx_ch_tavil),
  1473. tx_ch_tavil, ARRAY_SIZE(rx_ch_tavil),
  1474. rx_ch_tavil);
  1475. } else {
  1476. snd_soc_dai_set_channel_map(codec_dai, ARRAY_SIZE(tx_ch),
  1477. tx_ch, ARRAY_SIZE(rx_ch),
  1478. rx_ch);
  1479. }
  1480. if (!strcmp(dev_name(codec_dai->dev), "tavil_codec")) {
  1481. msm_codec_fn.get_afe_config_fn = tavil_get_afe_config;
  1482. } else {
  1483. msm_codec_fn.get_afe_config_fn = tasha_get_afe_config;
  1484. msm_codec_fn.mbhc_hs_detect_exit = tasha_mbhc_hs_detect_exit;
  1485. }
  1486. ret = msm_adsp_power_up_config(codec, rtd->card->snd_card);
  1487. if (ret) {
  1488. pr_err("%s: Failed to set AFE config %d\n", __func__, ret);
  1489. goto err_afe_cfg;
  1490. }
  1491. config_data = msm_codec_fn.get_afe_config_fn(codec,
  1492. AFE_AANC_VERSION);
  1493. if (config_data) {
  1494. ret = afe_set_config(AFE_AANC_VERSION, config_data, 0);
  1495. if (ret) {
  1496. pr_err("%s: Failed to set aanc version %d\n",
  1497. __func__, ret);
  1498. goto err_afe_cfg;
  1499. }
  1500. }
  1501. if (!strcmp(dev_name(codec_dai->dev), "tasha_codec")) {
  1502. config_data = msm_codec_fn.get_afe_config_fn(codec,
  1503. AFE_CDC_CLIP_REGISTERS_CONFIG);
  1504. if (config_data) {
  1505. ret = afe_set_config(AFE_CDC_CLIP_REGISTERS_CONFIG,
  1506. config_data, 0);
  1507. if (ret) {
  1508. pr_err("%s: Failed to set clip registers %d\n",
  1509. __func__, ret);
  1510. goto err_afe_cfg;
  1511. }
  1512. }
  1513. config_data = msm_codec_fn.get_afe_config_fn(codec,
  1514. AFE_CLIP_BANK_SEL);
  1515. if (config_data) {
  1516. ret = afe_set_config(AFE_CLIP_BANK_SEL, config_data, 0);
  1517. if (ret) {
  1518. pr_err("%s: Failed to set AFE bank selection %d\n",
  1519. __func__, ret);
  1520. goto err_afe_cfg;
  1521. }
  1522. }
  1523. }
  1524. /*
  1525. * Send speaker configuration only for WSA8810.
  1526. * Defalut configuration is for WSA8815.
  1527. */
  1528. pr_debug("%s: Number of aux devices: %d\n",
  1529. __func__, rtd->card->num_aux_devs);
  1530. if (!strcmp(dev_name(codec_dai->dev), "tavil_codec")) {
  1531. if (rtd->card->num_aux_devs &&
  1532. !list_empty(&rtd->card->aux_comp_list)) {
  1533. aux_comp = list_first_entry(&rtd->card->aux_comp_list,
  1534. struct snd_soc_component, list_aux);
  1535. if (!strcmp(aux_comp->name, WSA8810_NAME_1) ||
  1536. !strcmp(aux_comp->name, WSA8810_NAME_2)) {
  1537. tavil_set_spkr_mode(rtd->codec, SPKR_MODE_1);
  1538. tavil_set_spkr_gain_offset(rtd->codec,
  1539. RX_GAIN_OFFSET_M1P5_DB);
  1540. }
  1541. }
  1542. card = rtd->card->snd_card;
  1543. entry = snd_info_create_subdir(card->module, "codecs",
  1544. card->proc_root);
  1545. if (!entry) {
  1546. pr_debug("%s: Cannot create codecs module entry\n",
  1547. __func__);
  1548. goto done;
  1549. }
  1550. pdata->codec_root = entry;
  1551. tavil_codec_info_create_codec_entry(pdata->codec_root, codec);
  1552. } else {
  1553. if (rtd->card->num_aux_devs &&
  1554. !list_empty(&rtd->card->aux_comp_list)) {
  1555. aux_comp = list_first_entry(&rtd->card->aux_comp_list,
  1556. struct snd_soc_component, list_aux);
  1557. if (!strcmp(aux_comp->name, WSA8810_NAME_1) ||
  1558. !strcmp(aux_comp->name, WSA8810_NAME_2)) {
  1559. tasha_set_spkr_mode(rtd->codec, SPKR_MODE_1);
  1560. tasha_set_spkr_gain_offset(rtd->codec,
  1561. RX_GAIN_OFFSET_M1P5_DB);
  1562. }
  1563. }
  1564. card = rtd->card->snd_card;
  1565. entry = snd_info_create_subdir(card->module, "codecs",
  1566. card->proc_root);
  1567. if (!entry) {
  1568. pr_debug("%s: Cannot create codecs module entry\n",
  1569. __func__);
  1570. goto done;
  1571. }
  1572. pdata->codec_root = entry;
  1573. tasha_codec_info_create_codec_entry(pdata->codec_root, codec);
  1574. tasha_mbhc_zdet_gpio_ctrl(msm_config_hph_en0_gpio, rtd->codec);
  1575. }
  1576. done:
  1577. msm_set_codec_reg_done(true);
  1578. return 0;
  1579. err_afe_cfg:
  1580. return ret;
  1581. }
  1582. EXPORT_SYMBOL(msm_audrx_init);
  1583. /**
  1584. * msm_ext_register_audio_notifier - register SSR notifier.
  1585. */
  1586. void msm_ext_register_audio_notifier(struct platform_device *pdev)
  1587. {
  1588. int ret;
  1589. is_initial_boot = true;
  1590. spdev = pdev;
  1591. ret = audio_notifier_register("sdm660", AUDIO_NOTIFIER_ADSP_DOMAIN,
  1592. &service_nb);
  1593. if (ret < 0)
  1594. pr_err("%s: Audio notifier register failed ret = %d\n",
  1595. __func__, ret);
  1596. }
  1597. EXPORT_SYMBOL(msm_ext_register_audio_notifier);
  1598. /**
  1599. * msm_ext_cdc_init - external codec machine specific init.
  1600. *
  1601. * @pdev: platform device handle
  1602. * @pdata: private data of machine driver
  1603. * @card: sound card pointer reference
  1604. * @mbhc_cfg: MBHC config reference
  1605. *
  1606. * Returns 0 on success or ret on failure.
  1607. */
  1608. int msm_ext_cdc_init(struct platform_device *pdev,
  1609. struct msm_asoc_mach_data *pdata,
  1610. struct snd_soc_card **card,
  1611. struct wcd_mbhc_config *wcd_mbhc_cfg_ptr1)
  1612. {
  1613. int ret = 0;
  1614. wcd_mbhc_cfg_ptr = wcd_mbhc_cfg_ptr1;
  1615. pdev->id = 0;
  1616. wcd_mbhc_cfg_ptr->moisture_en = true;
  1617. wcd_mbhc_cfg_ptr->mbhc_micbias = MIC_BIAS_2;
  1618. wcd_mbhc_cfg_ptr->anc_micbias = MIC_BIAS_2;
  1619. wcd_mbhc_cfg_ptr->enable_anc_mic_detect = false;
  1620. *card = populate_snd_card_dailinks(&pdev->dev, pdata->snd_card_val);
  1621. if (!(*card)) {
  1622. dev_err(&pdev->dev, "%s: Card uninitialized\n", __func__);
  1623. ret = -EPROBE_DEFER;
  1624. goto err;
  1625. }
  1626. platform_set_drvdata(pdev, *card);
  1627. snd_soc_card_set_drvdata(*card, pdata);
  1628. pdata->hph_en1_gpio = of_get_named_gpio(pdev->dev.of_node,
  1629. "qcom,hph-en1-gpio", 0);
  1630. if (!gpio_is_valid(pdata->hph_en1_gpio))
  1631. pdata->hph_en1_gpio_p = of_parse_phandle(pdev->dev.of_node,
  1632. "qcom,hph-en1-gpio", 0);
  1633. if (!gpio_is_valid(pdata->hph_en1_gpio) && (!pdata->hph_en1_gpio_p)) {
  1634. dev_dbg(&pdev->dev, "property %s not detected in node %s",
  1635. "qcom,hph-en1-gpio", pdev->dev.of_node->full_name);
  1636. }
  1637. pdata->hph_en0_gpio = of_get_named_gpio(pdev->dev.of_node,
  1638. "qcom,hph-en0-gpio", 0);
  1639. if (!gpio_is_valid(pdata->hph_en0_gpio))
  1640. pdata->hph_en0_gpio_p = of_parse_phandle(pdev->dev.of_node,
  1641. "qcom,hph-en0-gpio", 0);
  1642. if (!gpio_is_valid(pdata->hph_en0_gpio) && (!pdata->hph_en0_gpio_p)) {
  1643. dev_dbg(&pdev->dev, "property %s not detected in node %s",
  1644. "qcom,hph-en0-gpio", pdev->dev.of_node->full_name);
  1645. }
  1646. ret = msm_ext_prepare_hifi(pdata);
  1647. if (ret) {
  1648. dev_dbg(&pdev->dev, "msm_ext_prepare_hifi failed (%d)\n",
  1649. ret);
  1650. ret = 0;
  1651. }
  1652. err:
  1653. return ret;
  1654. }
  1655. EXPORT_SYMBOL(msm_ext_cdc_init);