swr-dmic.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2019-2021, The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2022 Qualcomm Innovation Center, Inc. All rights reserved.
  5. */
  6. #include <linux/module.h>
  7. #include <linux/init.h>
  8. #include <linux/slab.h>
  9. #include <linux/platform_device.h>
  10. #include <linux/device.h>
  11. #include <linux/printk.h>
  12. #include <linux/bitops.h>
  13. #include <linux/regulator/consumer.h>
  14. #include <linux/pm_runtime.h>
  15. #include <linux/delay.h>
  16. #include <linux/kernel.h>
  17. #include <linux/gpio.h>
  18. #include <linux/of_gpio.h>
  19. #include <linux/of_platform.h>
  20. #include <linux/debugfs.h>
  21. #include <soc/soundwire.h>
  22. #include <sound/pcm.h>
  23. #include <sound/pcm_params.h>
  24. #include <sound/soc.h>
  25. #include <sound/soc-dapm.h>
  26. #include <sound/tlv.h>
  27. #include <asoc/msm-cdc-pinctrl.h>
  28. #include <asoc/msm-cdc-supply.h>
  29. #include <bindings/audio-codec-port-types.h>
  30. #ifdef CONFIG_SND_SOC_WCD939X
  31. #include "wcd939x/wcd939x.h"
  32. #else
  33. #include "wcd938x/wcd938x.h"
  34. #endif
  35. #include "swr-dmic.h"
  36. #define NUM_ATTEMPTS 5
  37. #define SWRS_SCP_CONTROL 0x44
  38. #define MAX_NAME_LEN 40
  39. static int swr_master_channel_map[] = {
  40. ZERO,
  41. SWRM_TX_PCM_OUT,
  42. SWRM_TX1_CH1,
  43. SWRM_TX1_CH2,
  44. SWRM_TX1_CH3,
  45. SWRM_TX1_CH4,
  46. SWRM_TX2_CH1,
  47. SWRM_TX2_CH2,
  48. SWRM_TX2_CH3,
  49. SWRM_TX2_CH4,
  50. SWRM_TX3_CH1,
  51. SWRM_TX3_CH2,
  52. SWRM_TX3_CH3,
  53. SWRM_TX3_CH4,
  54. SWRM_TX_PCM_IN,
  55. };
  56. /*
  57. * Private data Structure for swr-dmic. All parameters related to
  58. * external mic codec needs to be defined here.
  59. */
  60. struct swr_dmic_priv {
  61. struct device *dev;
  62. struct swr_device *swr_slave;
  63. struct snd_soc_component *component;
  64. struct snd_soc_component_driver *driver;
  65. struct snd_soc_dai_driver *dai_driver;
  66. struct snd_soc_component *supply_component;
  67. u32 micb_num;
  68. struct device_node *wcd_handle;
  69. bool is_wcd_supply;
  70. int is_en_supply;
  71. u8 tx_master_port_map[SWR_DMIC_MAX_PORTS];
  72. struct swr_port_params tx_port_params[SWR_UC_MAX][SWR_DMIC_MAX_PORTS];
  73. struct swr_dev_frame_config swr_tx_port_params[SWR_UC_MAX];
  74. struct notifier_block nblock;
  75. };
  76. const char *codec_name_list[] = {
  77. "swr-dmic.01",
  78. "swr-dmic.02",
  79. "swr-dmic.03",
  80. "swr-dmic.04",
  81. };
  82. const char *dai_name_list[] = {
  83. "swr_dmic_tx0",
  84. "swr_dmic_tx1",
  85. "swr_dmic_tx2",
  86. "swr_dmic_tx3",
  87. };
  88. const char *aif_name_list[] = {
  89. "SWR_DMIC_AIF0 Capture",
  90. "SWR_DMIC_AIF1 Capture",
  91. "SWR_DMIC_AIF2 Capture",
  92. "SWR_DMIC_AIF3 Capture",
  93. };
  94. static int swr_dmic_reset(struct swr_device *pdev);
  95. static int swr_dmic_up(struct swr_device *pdev);
  96. static int swr_dmic_down(struct swr_device *pdev);
  97. static int swr_dmic_event_notify(struct notifier_block *block,
  98. unsigned long val,
  99. void *data);
  100. static inline int swr_dmic_tx_get_slave_port_type_idx(const char *wname,
  101. unsigned int *port_idx)
  102. {
  103. u8 port_type;
  104. if (strnstr(wname, "HIFI", strlen(wname)))
  105. port_type = SWR_DMIC_HIFI_PORT;
  106. else if (strnstr(wname, "LP", strlen(wname)))
  107. port_type = SWR_DMIC_LP_PORT;
  108. else
  109. return -EINVAL;
  110. *port_idx = port_type;
  111. return 0;
  112. }
  113. static inline int swr_dmic_get_master_port_val(int port)
  114. {
  115. int i;
  116. for (i = 0; i < ARRAY_SIZE(swr_master_channel_map); i++)
  117. if (port == swr_master_channel_map[i])
  118. return i;
  119. return 0;
  120. }
  121. static int swr_dmic_tx_master_port_get(struct snd_kcontrol *kcontrol,
  122. struct snd_ctl_elem_value *ucontrol)
  123. {
  124. struct snd_soc_component *component =
  125. snd_soc_kcontrol_component(kcontrol);
  126. struct swr_dmic_priv *swr_dmic = NULL;
  127. int ret = 0;
  128. unsigned int slave_port_idx = SWR_DMIC_MAX_PORTS;
  129. if (NULL == component) {
  130. pr_err_ratelimited("%s: swr dmic component is NULL\n", __func__);
  131. return -EINVAL;
  132. }
  133. swr_dmic = snd_soc_component_get_drvdata(component);
  134. if (NULL == swr_dmic) {
  135. pr_err_ratelimited("%s: swr_dmic_priv is NULL\n", __func__);
  136. return -EINVAL;
  137. }
  138. ret = swr_dmic_tx_get_slave_port_type_idx(kcontrol->id.name,
  139. &slave_port_idx);
  140. if (ret) {
  141. dev_dbg(component->dev, "%s: invalid port string\n", __func__);
  142. return ret;
  143. }
  144. if (slave_port_idx >= SWR_DMIC_MAX_PORTS) {
  145. pr_err_ratelimited("%s: invalid slave port id\n", __func__);
  146. return -EINVAL;
  147. }
  148. ucontrol->value.integer.value[0] =
  149. swr_dmic_get_master_port_val(
  150. swr_dmic->tx_master_port_map[slave_port_idx]);
  151. dev_dbg(component->dev, "%s: ucontrol->value.integer.value[0] = %ld\n",
  152. __func__, ucontrol->value.integer.value[0]);
  153. return 0;
  154. }
  155. static int swr_dmic_tx_master_port_put(struct snd_kcontrol *kcontrol,
  156. struct snd_ctl_elem_value *ucontrol)
  157. {
  158. struct snd_soc_component *component =
  159. snd_soc_kcontrol_component(kcontrol);
  160. struct swr_dmic_priv *swr_dmic = NULL;
  161. int ret = 0;
  162. unsigned int slave_port_idx = SWR_DMIC_MAX_PORTS;
  163. unsigned int idx = 0;
  164. if (NULL == component) {
  165. pr_err_ratelimited("%s: swr dmic component is NULL\n", __func__);
  166. return -EINVAL;
  167. }
  168. swr_dmic = snd_soc_component_get_drvdata(component);
  169. if (NULL == swr_dmic) {
  170. pr_err_ratelimited("%s: swr_dmic_priv is NULL\n", __func__);
  171. return -EINVAL;
  172. }
  173. ret = swr_dmic_tx_get_slave_port_type_idx(kcontrol->id.name,
  174. &slave_port_idx);
  175. if (ret) {
  176. dev_dbg(component->dev, "%s: invalid port string\n", __func__);
  177. return ret;
  178. }
  179. if (slave_port_idx >= SWR_DMIC_MAX_PORTS) {
  180. pr_err_ratelimited("%s: invalid slave port id\n", __func__);
  181. return -EINVAL;
  182. }
  183. idx = ucontrol->value.enumerated.item[0];
  184. if (idx < 0 || idx >= ARRAY_SIZE(swr_master_channel_map))
  185. return -EINVAL;
  186. swr_dmic->tx_master_port_map[slave_port_idx] =
  187. swr_master_channel_map[idx];
  188. dev_dbg(component->dev, "%s: slv port id: %d, master_port_type: %d\n",
  189. __func__, slave_port_idx,
  190. swr_dmic->tx_master_port_map[slave_port_idx]);
  191. return 0;
  192. }
  193. static int swr_dmic_port_enable(struct snd_soc_dapm_widget *w,
  194. struct snd_kcontrol *kcontrol, int event)
  195. {
  196. int ret = 0;
  197. struct snd_soc_component *component =
  198. snd_soc_dapm_to_component(w->dapm);
  199. struct swr_dmic_priv *swr_dmic =
  200. snd_soc_component_get_drvdata(component);
  201. u8 ch_mask = 0x01; /* only DpnChannelEN1 register is available */
  202. u8 num_port = 1;
  203. u8 port_id = w->shift;
  204. u8 port_type = swr_dmic->tx_master_port_map[port_id];
  205. switch (event) {
  206. case SND_SOC_DAPM_POST_PMU:
  207. ret = swr_slvdev_datapath_control(swr_dmic->swr_slave,
  208. swr_dmic->swr_slave->dev_num, true);
  209. break;
  210. case SND_SOC_DAPM_PRE_PMD:
  211. ret = swr_disconnect_port(swr_dmic->swr_slave,
  212. &port_id, num_port, &ch_mask, &port_type);
  213. break;
  214. };
  215. return ret;
  216. }
  217. static int dmic_swr_ctrl(struct snd_soc_dapm_widget *w,
  218. struct snd_kcontrol *kcontrol, int event)
  219. {
  220. int ret = 0;
  221. struct snd_soc_component *component =
  222. snd_soc_dapm_to_component(w->dapm);
  223. struct swr_dmic_priv *swr_dmic =
  224. snd_soc_component_get_drvdata(component);
  225. u8 num_ch = 1;
  226. u8 ch_mask = 0x01; /* only DpnChannelEN1 register is available */
  227. u32 ch_rate = SWR_CLK_RATE_4P8MHZ;
  228. u8 num_port = 1;
  229. u8 port_type = 0;
  230. u8 port_id = w->shift;
  231. if (port_id >= SWR_DMIC_MAX_PORTS)
  232. {
  233. dev_err_ratelimited(component->dev, "%s: invalid port id: %d\n",
  234. __func__, port_id);
  235. return -EINVAL;
  236. }
  237. /*
  238. * Port 1 is high quality / 2.4 or 3.072 Mbps
  239. * Port 2 is listen low power / 0.6 or 0.768 Mbps
  240. */
  241. if (port_id == SWR_DMIC_HIFI_PORT)
  242. ch_rate = SWR_CLK_RATE_2P4MHZ;
  243. else
  244. ch_rate = SWR_CLK_RATE_0P6MHZ;
  245. port_type = swr_dmic->tx_master_port_map[port_id];
  246. dev_dbg(component->dev, "%s port_type: %d event: %d\n", __func__,
  247. port_type, event);
  248. switch (event) {
  249. case SND_SOC_DAPM_PRE_PMU:
  250. ret = swr_connect_port(swr_dmic->swr_slave, &port_id,
  251. num_port, &ch_mask, &ch_rate,
  252. &num_ch, &port_type);
  253. break;
  254. case SND_SOC_DAPM_POST_PMD:
  255. ret = swr_slvdev_datapath_control(swr_dmic->swr_slave,
  256. swr_dmic->swr_slave->dev_num, false);
  257. break;
  258. };
  259. return ret;
  260. }
  261. /* qcom,swr-tx-port-params = <OFFSET1_VAL0 LANE1>, <OFFSET1_VAL5 LANE0>, *UC0*
  262. <OFFSET1_VAL0 LANE1>, <OFFSET1_VAL2 LANE0>, *UC1*
  263. <OFFSET1_VAL1 LANE0>, <OFFSET1_VAL1 LANE0>, *UC2*
  264. <OFFSET1_VAL1 LANE0>, <OFFSET1_VAL1 LANE0>, *UC3 */
  265. static int swr_dmic_parse_port_params(struct device *dev,
  266. char *prop)
  267. {
  268. int i, j;
  269. u32 *dt_array, map_size, max_uc;
  270. int ret = 0;
  271. u32 cnt = 0;
  272. struct swr_port_params (*map)[SWR_UC_MAX][SWR_DMIC_MAX_PORTS];
  273. struct swr_dev_frame_config (*map_uc)[SWR_UC_MAX];
  274. struct swr_dmic_priv *swr_dmic = dev_get_drvdata(dev);
  275. map = &swr_dmic->tx_port_params;
  276. map_uc = &swr_dmic->swr_tx_port_params;
  277. if (!of_find_property(dev->of_node, prop,
  278. &map_size)) {
  279. dev_err(dev, "missing port mapping prop %s\n", prop);
  280. ret = -EINVAL;
  281. goto err_port_map;
  282. }
  283. max_uc = map_size / (SWR_DMIC_MAX_PORTS * SWR_PORT_PARAMS * sizeof(u32));
  284. if (max_uc != SWR_UC_MAX) {
  285. dev_err(dev,
  286. "%s:port params not provided for all usecases\n",
  287. __func__);
  288. ret = -EINVAL;
  289. goto err_port_map;
  290. }
  291. dt_array = kzalloc(map_size, GFP_KERNEL);
  292. if (!dt_array) {
  293. ret = -ENOMEM;
  294. goto err_alloc;
  295. }
  296. ret = of_property_read_u32_array(dev->of_node, prop, dt_array,
  297. SWR_DMIC_MAX_PORTS * SWR_PORT_PARAMS * max_uc);
  298. if (ret) {
  299. dev_err(dev, "%s: Failed to read port mapping from prop %s\n",
  300. __func__, prop);
  301. goto err_pdata_fail;
  302. }
  303. for (i = 0; i < max_uc; i++) {
  304. for (j = 0; j < SWR_DMIC_MAX_PORTS; j++) {
  305. cnt = (i * SWR_DMIC_MAX_PORTS + j) * SWR_PORT_PARAMS;
  306. (*map)[i][j].offset1 = dt_array[cnt];
  307. (*map)[i][j].lane_ctrl = dt_array[cnt + 1];
  308. dev_err(dev, "%s: port %d, uc: %d, offset1:%d, lane: %d\n",
  309. __func__, j, i, dt_array[cnt], dt_array[cnt + 1]);
  310. }
  311. (*map_uc)[i].pp = &(*map)[i][0];
  312. }
  313. kfree(dt_array);
  314. return 0;
  315. err_pdata_fail:
  316. kfree(dt_array);
  317. err_alloc:
  318. err_port_map:
  319. return ret;
  320. }
  321. static const char * const tx_master_port_text[] = {
  322. "ZERO", "SWRM_PCM_OUT", "SWRM_TX1_CH1", "SWRM_TX1_CH2", "SWRM_TX1_CH3",
  323. "SWRM_TX1_CH4", "SWRM_TX2_CH1", "SWRM_TX2_CH2", "SWRM_TX2_CH3",
  324. "SWRM_TX2_CH4", "SWRM_TX3_CH1", "SWRM_TX3_CH2", "SWRM_TX3_CH3",
  325. "SWRM_TX3_CH4", "SWRM_PCM_IN",
  326. };
  327. static const struct soc_enum tx_master_port_enum =
  328. SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(tx_master_port_text),
  329. tx_master_port_text);
  330. static const struct snd_kcontrol_new swr_dmic_snd_controls[] = {
  331. SOC_ENUM_EXT("HIFI PortMap", tx_master_port_enum,
  332. swr_dmic_tx_master_port_get, swr_dmic_tx_master_port_put),
  333. SOC_ENUM_EXT("LP PortMap", tx_master_port_enum,
  334. swr_dmic_tx_master_port_get, swr_dmic_tx_master_port_put),
  335. };
  336. static const struct snd_kcontrol_new dmic_switch[] = {
  337. SOC_DAPM_SINGLE("Switch", SND_SOC_NOPM, 0, 1, 0)
  338. };
  339. static const struct snd_kcontrol_new va_dmic_switch[] = {
  340. SOC_DAPM_SINGLE("Switch", SND_SOC_NOPM, 0, 1, 0)
  341. };
  342. static const struct snd_soc_dapm_widget swr_dmic_dapm_widgets[] = {
  343. SND_SOC_DAPM_MIXER_E("SWR_DMIC_MIXER", SND_SOC_NOPM,
  344. SWR_DMIC_HIFI_PORT, 0,
  345. dmic_switch, ARRAY_SIZE(dmic_switch), dmic_swr_ctrl,
  346. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  347. SND_SOC_DAPM_MIXER_E("SWR_DMIC_VA_MIXER", SND_SOC_NOPM,
  348. SWR_DMIC_LP_PORT, 0,
  349. va_dmic_switch, ARRAY_SIZE(va_dmic_switch), dmic_swr_ctrl,
  350. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  351. SND_SOC_DAPM_INPUT("SWR_DMIC"),
  352. SND_SOC_DAPM_INPUT("VA_SWR_DMIC"),
  353. SND_SOC_DAPM_OUT_DRV_E("SMIC_PORT_EN", SND_SOC_NOPM,
  354. SWR_DMIC_HIFI_PORT, 0, NULL, 0,
  355. swr_dmic_port_enable,
  356. SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
  357. SND_SOC_DAPM_OUT_DRV_E("SMIC_VA_PORT_EN", SND_SOC_NOPM,
  358. SWR_DMIC_LP_PORT, 0, NULL, 0,
  359. swr_dmic_port_enable,
  360. SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
  361. SND_SOC_DAPM_OUTPUT("SWR_DMIC_OUTPUT"),
  362. SND_SOC_DAPM_OUTPUT("SWR_DMIC_VA_OUTPUT"),
  363. };
  364. static const struct snd_soc_dapm_route swr_dmic_audio_map[] = {
  365. {"SWR_DMIC_MIXER", "Switch", "SWR_DMIC"},
  366. {"SMIC_PORT_EN", NULL, "SWR_DMIC_MIXER"},
  367. {"SWR_DMIC_OUTPUT", NULL, "SMIC_PORT_EN"},
  368. {"SWR_DMIC_VA_MIXER", "Switch", "VA_SWR_DMIC"},
  369. {"SMIC_VA_PORT_EN", NULL, "SWR_DMIC_VA_MIXER"},
  370. {"SWR_DMIC_VA_OUTPUT", NULL, "SMIC_VA_PORT_EN"},
  371. };
  372. static int swr_dmic_codec_probe(struct snd_soc_component *component)
  373. {
  374. struct swr_dmic_priv *swr_dmic =
  375. snd_soc_component_get_drvdata(component);
  376. struct snd_soc_dapm_context *dapm =
  377. snd_soc_component_get_dapm(component);
  378. char w_name[MAX_NAME_LEN];
  379. if (!swr_dmic)
  380. return -EINVAL;
  381. swr_dmic->component = component;
  382. if (!component->name_prefix) {
  383. dev_err(component->dev, "%s: component prefix is NULL\n", __func__);
  384. return -EPROBE_DEFER;
  385. }
  386. memset(w_name, 0, sizeof(w_name));
  387. strlcpy(w_name, component->name_prefix, sizeof(w_name));
  388. strlcat(w_name, " ", sizeof(w_name));
  389. strlcat(w_name, swr_dmic->dai_driver->capture.stream_name,
  390. sizeof(w_name));
  391. snd_soc_dapm_ignore_suspend(dapm, w_name);
  392. memset(w_name, 0, sizeof(w_name));
  393. strlcpy(w_name, component->name_prefix, sizeof(w_name));
  394. strlcat(w_name, " SWR_DMIC", sizeof(w_name));
  395. snd_soc_dapm_ignore_suspend(dapm, w_name);
  396. memset(w_name, 0, sizeof(w_name));
  397. strlcpy(w_name, component->name_prefix, sizeof(w_name));
  398. strlcat(w_name, " SMIC_PORT_EN", sizeof(w_name));
  399. snd_soc_dapm_ignore_suspend(dapm, w_name);
  400. memset(w_name, 0, sizeof(w_name));
  401. strlcpy(w_name, component->name_prefix, sizeof(w_name));
  402. strlcat(w_name, " SWR_DMIC_OUTPUT", sizeof(w_name));
  403. snd_soc_dapm_ignore_suspend(dapm, w_name);
  404. memset(w_name, 0, sizeof(w_name));
  405. strlcpy(w_name, component->name_prefix, sizeof(w_name));
  406. strlcat(w_name, " VA_SWR_DMIC", sizeof(w_name));
  407. snd_soc_dapm_ignore_suspend(dapm, w_name);
  408. memset(w_name, 0, sizeof(w_name));
  409. strlcpy(w_name, component->name_prefix, sizeof(w_name));
  410. strlcat(w_name, " SMIC_VA_PORT_EN", sizeof(w_name));
  411. snd_soc_dapm_ignore_suspend(dapm, w_name);
  412. memset(w_name, 0, sizeof(w_name));
  413. strlcpy(w_name, component->name_prefix, sizeof(w_name));
  414. strlcat(w_name, " SWR_DMIC_VA_OUTPUT", sizeof(w_name));
  415. snd_soc_dapm_ignore_suspend(dapm, w_name);
  416. snd_soc_dapm_sync(dapm);
  417. swr_dmic->nblock.notifier_call = swr_dmic_event_notify;
  418. #ifdef CONFIG_SND_SOC_WCD939X
  419. wcd939x_swr_dmic_register_notifier(swr_dmic->supply_component,
  420. &swr_dmic->nblock, true);
  421. #else
  422. wcd938x_swr_dmic_register_notifier(swr_dmic->supply_component,
  423. &swr_dmic->nblock, true);
  424. #endif
  425. return 0;
  426. }
  427. static void swr_dmic_codec_remove(struct snd_soc_component *component)
  428. {
  429. struct swr_dmic_priv *swr_dmic =
  430. snd_soc_component_get_drvdata(component);
  431. swr_dmic->component = NULL;
  432. return;
  433. }
  434. static const struct snd_soc_component_driver soc_codec_dev_swr_dmic = {
  435. .name = NULL,
  436. .probe = swr_dmic_codec_probe,
  437. .remove = swr_dmic_codec_remove,
  438. .controls = swr_dmic_snd_controls,
  439. .num_controls = ARRAY_SIZE(swr_dmic_snd_controls),
  440. .dapm_widgets = swr_dmic_dapm_widgets,
  441. .num_dapm_widgets = ARRAY_SIZE(swr_dmic_dapm_widgets),
  442. .dapm_routes = swr_dmic_audio_map,
  443. .num_dapm_routes = ARRAY_SIZE(swr_dmic_audio_map),
  444. };
  445. static int enable_wcd_codec_supply(struct swr_dmic_priv *swr_dmic, bool enable)
  446. {
  447. int rc = 0;
  448. int micb_num = swr_dmic->micb_num;
  449. struct snd_soc_component *component = swr_dmic->supply_component;
  450. if (!component) {
  451. pr_err_ratelimited("%s: component is NULL\n", __func__);
  452. return -EINVAL;
  453. }
  454. dev_dbg(component->dev, "%s: supply %d micbias: %d enable: %d\n",
  455. __func__, swr_dmic->is_en_supply, micb_num, enable);
  456. if (enable)
  457. #ifdef CONFIG_SND_SOC_WCD939X
  458. rc = wcd939x_codec_force_enable_micbias_v2(component,
  459. SND_SOC_DAPM_PRE_PMU, micb_num);
  460. #else
  461. rc = wcd938x_codec_force_enable_micbias_v2(component,
  462. SND_SOC_DAPM_PRE_PMU, micb_num);
  463. #endif
  464. else
  465. #ifdef CONFIG_SND_SOC_WCD939X
  466. rc = wcd939x_codec_force_enable_micbias_v2(component,
  467. SND_SOC_DAPM_POST_PMD, micb_num);
  468. #else
  469. rc = wcd938x_codec_force_enable_micbias_v2(component,
  470. SND_SOC_DAPM_POST_PMD, micb_num);
  471. #endif
  472. return rc;
  473. }
  474. static int swr_dmic_parse_supply(struct device_node *np,
  475. struct swr_dmic_priv *swr_dmic)
  476. {
  477. struct platform_device *pdev = NULL;
  478. if (!np || !swr_dmic)
  479. return -EINVAL;
  480. pdev = of_find_device_by_node(np);
  481. if (!pdev)
  482. return -EINVAL;
  483. swr_dmic->supply_component = snd_soc_lookup_component(&pdev->dev, NULL);
  484. return 0;
  485. }
  486. static struct snd_soc_dai_driver swr_dmic_dai[] = {
  487. {
  488. .name = "",
  489. .id = 0,
  490. .capture = {
  491. .stream_name = "",
  492. .rates = (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000 |
  493. SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_48000 |
  494. SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_192000),
  495. .formats = (SNDRV_PCM_FMTBIT_S16_LE |
  496. SNDRV_PCM_FMTBIT_S24_LE |
  497. SNDRV_PCM_FMTBIT_S32_LE),
  498. .rate_max = 192000,
  499. .rate_min = 8000,
  500. .channels_min = 1,
  501. .channels_max = 2,
  502. },
  503. },
  504. };
  505. static int swr_dmic_event_notify(struct notifier_block *block,
  506. unsigned long val,
  507. void *data)
  508. {
  509. u16 event = (val & 0xffff);
  510. int ret = 0;
  511. struct swr_dmic_priv *swr_dmic = container_of(block,
  512. struct swr_dmic_priv,
  513. nblock);
  514. switch (event) {
  515. #ifdef CONFIG_SND_SOC_WCD939X
  516. case WCD939X_EVT_SSR_DOWN:
  517. #else
  518. case WCD938X_EVT_SSR_DOWN:
  519. #endif
  520. ret = swr_dmic_down(swr_dmic->swr_slave);
  521. break;
  522. #ifdef CONFIG_SND_SOC_WCD939X
  523. case WCD939X_EVT_SSR_UP:
  524. #else
  525. case WCD938X_EVT_SSR_UP:
  526. #endif
  527. ret = swr_dmic_up(swr_dmic->swr_slave);
  528. if (!ret)
  529. ret = swr_dmic_reset(swr_dmic->swr_slave);
  530. break;
  531. }
  532. return ret;
  533. }
  534. static int swr_dmic_probe(struct swr_device *pdev)
  535. {
  536. int ret = 0;
  537. int i = 0;
  538. u8 swr_devnum = 0;
  539. int dev_index = -1;
  540. struct swr_dmic_priv *swr_dmic = NULL;
  541. const char *swr_dmic_codec_name_of = NULL;
  542. struct snd_soc_component *component = NULL;
  543. int num_retry = NUM_ATTEMPTS;
  544. swr_dmic = devm_kzalloc(&pdev->dev, sizeof(struct swr_dmic_priv),
  545. GFP_KERNEL);
  546. if (!swr_dmic)
  547. return -ENOMEM;
  548. ret = of_property_read_u32(pdev->dev.of_node, "qcom,swr-dmic-supply",
  549. &swr_dmic->micb_num);
  550. if (ret) {
  551. dev_dbg(&pdev->dev, "%s: Looking up %s property in node %s failed\n",
  552. __func__, "qcom,swr-dmic-supply",
  553. pdev->dev.of_node->full_name);
  554. goto err;
  555. }
  556. swr_dmic->wcd_handle = of_parse_phandle(pdev->dev.of_node,
  557. "qcom,wcd-handle", 0);
  558. if (!swr_dmic->wcd_handle) {
  559. dev_dbg(&pdev->dev, "%s: no wcd handle listed\n",
  560. __func__);
  561. swr_dmic->is_wcd_supply = false;
  562. } else {
  563. swr_dmic_parse_supply(swr_dmic->wcd_handle, swr_dmic);
  564. swr_dmic->is_wcd_supply = true;
  565. }
  566. if (swr_dmic->is_wcd_supply) {
  567. ret = enable_wcd_codec_supply(swr_dmic, true);
  568. if (ret) {
  569. ret = -EPROBE_DEFER;
  570. swr_dmic->is_wcd_supply = false;
  571. swr_dmic->wcd_handle = NULL;
  572. goto err;
  573. }
  574. ++swr_dmic->is_en_supply;
  575. }
  576. swr_set_dev_data(pdev, swr_dmic);
  577. swr_dmic->swr_slave = pdev;
  578. ret = of_property_read_string(pdev->dev.of_node, "qcom,codec-name",
  579. &swr_dmic_codec_name_of);
  580. if (ret) {
  581. dev_dbg(&pdev->dev, "%s: Looking up %s property in node %s failed\n",
  582. __func__, "qcom,codec-name",
  583. pdev->dev.of_node->full_name);
  584. goto dev_err;
  585. }
  586. ret = swr_dmic_parse_port_params(&pdev->dev, "qcom,swr-tx-port-params");
  587. if (ret) {
  588. dev_err(&pdev->dev, "%s: Parsing %s failed in node %s\n",
  589. __func__, "qcom,swr-tx-port-params",
  590. pdev->dev.of_node->full_name);
  591. goto dev_err;
  592. }
  593. /*
  594. * Add 5msec delay to provide sufficient time for
  595. * soundwire auto enumeration of slave devices as
  596. * as per HW requirement.
  597. */
  598. usleep_range(5000, 5010);
  599. do {
  600. /* Add delay for soundwire enumeration */
  601. usleep_range(100, 110);
  602. ret = swr_get_logical_dev_num(pdev, pdev->addr, &swr_devnum);
  603. } while (ret && --num_retry);
  604. if (ret) {
  605. dev_info(&pdev->dev,
  606. "%s get devnum %d for dev addr %llx failed\n",
  607. __func__, swr_devnum, pdev->addr);
  608. ret = -EPROBE_DEFER;
  609. if (swr_dmic->is_en_supply == 1) {
  610. enable_wcd_codec_supply(swr_dmic, false);
  611. --swr_dmic->is_en_supply;
  612. }
  613. swr_dmic->is_wcd_supply = false;
  614. swr_dmic->wcd_handle = NULL;
  615. goto err;
  616. }
  617. pdev->dev_num = swr_devnum;
  618. swr_init_port_params(pdev, SWR_DMIC_MAX_PORTS,
  619. swr_dmic->swr_tx_port_params);
  620. swr_dmic->driver = devm_kzalloc(&pdev->dev,
  621. sizeof(struct snd_soc_component_driver), GFP_KERNEL);
  622. if (!swr_dmic->driver) {
  623. ret = -ENOMEM;
  624. goto dev_err;
  625. }
  626. memcpy(swr_dmic->driver, &soc_codec_dev_swr_dmic,
  627. sizeof(struct snd_soc_component_driver));
  628. for (i = 0; i < ARRAY_SIZE(codec_name_list); i++) {
  629. if (!strcmp(swr_dmic_codec_name_of, codec_name_list[i])) {
  630. dev_index = i;
  631. break;
  632. }
  633. }
  634. if (dev_index < 0) {
  635. ret = -EINVAL;
  636. goto dev_err;
  637. }
  638. swr_dmic->driver->name = codec_name_list[dev_index];
  639. swr_dmic->dai_driver = devm_kzalloc(&pdev->dev,
  640. sizeof(struct snd_soc_dai_driver), GFP_KERNEL);
  641. if (!swr_dmic->dai_driver) {
  642. ret = -ENOMEM;
  643. goto dev_err;
  644. }
  645. memcpy(swr_dmic->dai_driver, swr_dmic_dai,
  646. sizeof(struct snd_soc_dai_driver));
  647. swr_dmic->dai_driver->id = dev_index;
  648. swr_dmic->dai_driver->name = dai_name_list[dev_index];
  649. swr_dmic->dai_driver->capture.stream_name = aif_name_list[dev_index];
  650. /* Number of DAI's used is 1 */
  651. ret = snd_soc_register_component(&pdev->dev, swr_dmic->driver,
  652. swr_dmic->dai_driver, 1);
  653. if (ret) {
  654. dev_err(&pdev->dev, "%s: Codec registration failed\n",
  655. __func__);
  656. goto dev_err;
  657. }
  658. component = snd_soc_lookup_component(&pdev->dev,
  659. swr_dmic->driver->name);
  660. if (!component) {
  661. dev_err(&pdev->dev, "%s: could not find swr_dmic component\n",
  662. __func__);
  663. goto dev_err;
  664. }
  665. swr_dmic->component = component;
  666. return 0;
  667. dev_err:
  668. if (swr_dmic->is_en_supply == 1) {
  669. enable_wcd_codec_supply(swr_dmic, false);
  670. --swr_dmic->is_en_supply;
  671. }
  672. swr_dmic->is_wcd_supply = false;
  673. swr_dmic->wcd_handle = NULL;
  674. swr_remove_device(pdev);
  675. err:
  676. return ret;
  677. }
  678. static int swr_dmic_remove(struct swr_device *pdev)
  679. {
  680. struct swr_dmic_priv *swr_dmic;
  681. swr_dmic = swr_get_dev_data(pdev);
  682. if (!swr_dmic) {
  683. dev_err(&pdev->dev, "%s: swr_dmic is NULL\n", __func__);
  684. return -EINVAL;
  685. }
  686. if (swr_dmic->is_en_supply == 1) {
  687. enable_wcd_codec_supply(swr_dmic, false);
  688. --swr_dmic->is_en_supply;
  689. }
  690. snd_soc_unregister_component(&pdev->dev);
  691. swr_set_dev_data(pdev, NULL);
  692. return 0;
  693. }
  694. static int swr_dmic_up(struct swr_device *pdev)
  695. {
  696. int ret = 0;
  697. struct swr_dmic_priv *swr_dmic;
  698. swr_dmic = swr_get_dev_data(pdev);
  699. if (!swr_dmic) {
  700. dev_err_ratelimited(&pdev->dev, "%s: swr_dmic is NULL\n", __func__);
  701. return -EINVAL;
  702. }
  703. ++swr_dmic->is_en_supply;
  704. if (swr_dmic->is_en_supply == 1)
  705. ret = enable_wcd_codec_supply(swr_dmic, true);
  706. return ret;
  707. }
  708. static int swr_dmic_down(struct swr_device *pdev)
  709. {
  710. struct swr_dmic_priv *swr_dmic;
  711. int ret = 0;
  712. swr_dmic = swr_get_dev_data(pdev);
  713. if (!swr_dmic) {
  714. dev_err_ratelimited(&pdev->dev, "%s: swr_dmic is NULL\n", __func__);
  715. return -EINVAL;
  716. }
  717. dev_dbg(&pdev->dev, "%s: is_en_supply: %d\n",
  718. __func__, swr_dmic->is_en_supply);
  719. --swr_dmic->is_en_supply;
  720. if (swr_dmic->is_en_supply < 0) {
  721. dev_warn(&pdev->dev, "%s: mismatch in supply count %d\n",
  722. __func__, swr_dmic->is_en_supply);
  723. swr_dmic->is_en_supply = 0;
  724. goto done;
  725. }
  726. if (!swr_dmic->is_en_supply)
  727. enable_wcd_codec_supply(swr_dmic, false);
  728. done:
  729. return ret;
  730. }
  731. static int swr_dmic_reset(struct swr_device *pdev)
  732. {
  733. struct swr_dmic_priv *swr_dmic;
  734. u8 retry = NUM_ATTEMPTS;
  735. u8 devnum = 0;
  736. swr_dmic = swr_get_dev_data(pdev);
  737. if (!swr_dmic) {
  738. dev_err_ratelimited(&pdev->dev, "%s: swr_dmic is NULL\n", __func__);
  739. return -EINVAL;
  740. }
  741. while (swr_get_logical_dev_num(pdev, pdev->addr, &devnum) && retry--) {
  742. /* Retry after 1 msec delay */
  743. usleep_range(1000, 1100);
  744. }
  745. pdev->dev_num = devnum;
  746. dev_dbg(&pdev->dev, "%s: devnum: %d\n", __func__, devnum);
  747. return 0;
  748. }
  749. #ifdef CONFIG_PM_SLEEP
  750. static int swr_dmic_suspend(struct device *dev)
  751. {
  752. dev_dbg(dev, "%s: system suspend\n", __func__);
  753. return 0;
  754. }
  755. static int swr_dmic_resume(struct device *dev)
  756. {
  757. struct swr_dmic_priv *swr_dmic = swr_get_dev_data(to_swr_device(dev));
  758. if (!swr_dmic) {
  759. dev_err_ratelimited(dev, "%s: swr_dmic private data is NULL\n", __func__);
  760. return -EINVAL;
  761. }
  762. dev_dbg(dev, "%s: system resume\n", __func__);
  763. return 0;
  764. }
  765. #endif /* CONFIG_PM_SLEEP */
  766. static const struct dev_pm_ops swr_dmic_pm_ops = {
  767. SET_SYSTEM_SLEEP_PM_OPS(swr_dmic_suspend, swr_dmic_resume)
  768. };
  769. static const struct swr_device_id swr_dmic_id[] = {
  770. {"swr-dmic", 0},
  771. {}
  772. };
  773. static const struct of_device_id swr_dmic_dt_match[] = {
  774. {
  775. .compatible = "qcom,swr-dmic",
  776. },
  777. {}
  778. };
  779. static struct swr_driver swr_dmic_driver = {
  780. .driver = {
  781. .name = "swr-dmic",
  782. .owner = THIS_MODULE,
  783. .pm = &swr_dmic_pm_ops,
  784. .of_match_table = swr_dmic_dt_match,
  785. },
  786. .probe = swr_dmic_probe,
  787. .remove = swr_dmic_remove,
  788. .id_table = swr_dmic_id,
  789. };
  790. static int __init swr_dmic_init(void)
  791. {
  792. return swr_driver_register(&swr_dmic_driver);
  793. }
  794. static void __exit swr_dmic_exit(void)
  795. {
  796. swr_driver_unregister(&swr_dmic_driver);
  797. }
  798. module_init(swr_dmic_init);
  799. module_exit(swr_dmic_exit);
  800. MODULE_DESCRIPTION("SWR DMIC driver");
  801. MODULE_LICENSE("GPL v2");