swr-dmic.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2019-2020, The Linux Foundation. All rights reserved.
  4. */
  5. #include <linux/module.h>
  6. #include <linux/init.h>
  7. #include <linux/slab.h>
  8. #include <linux/platform_device.h>
  9. #include <linux/device.h>
  10. #include <linux/printk.h>
  11. #include <linux/bitops.h>
  12. #include <linux/regulator/consumer.h>
  13. #include <linux/pm_runtime.h>
  14. #include <linux/delay.h>
  15. #include <linux/kernel.h>
  16. #include <linux/gpio.h>
  17. #include <linux/of_gpio.h>
  18. #include <linux/of_platform.h>
  19. #include <linux/debugfs.h>
  20. #include <soc/soundwire.h>
  21. #include <sound/pcm.h>
  22. #include <sound/pcm_params.h>
  23. #include <sound/soc.h>
  24. #include <sound/soc-dapm.h>
  25. #include <sound/tlv.h>
  26. #include <asoc/msm-cdc-pinctrl.h>
  27. #include <asoc/msm-cdc-supply.h>
  28. #include <dt-bindings/sound/audio-codec-port-types.h>
  29. #include "wcd938x/wcd938x.h"
  30. #include "swr-dmic.h"
  31. static int swr_master_channel_map[] = {
  32. ZERO,
  33. SWRM_TX1_CH1,
  34. SWRM_TX1_CH2,
  35. SWRM_TX1_CH3,
  36. SWRM_TX1_CH4,
  37. SWRM_TX2_CH1,
  38. SWRM_TX2_CH2,
  39. SWRM_TX2_CH3,
  40. SWRM_TX2_CH4,
  41. SWRM_TX3_CH1,
  42. SWRM_TX3_CH2,
  43. SWRM_TX3_CH3,
  44. SWRM_TX3_CH4,
  45. SWRM_PCM_IN,
  46. };
  47. /*
  48. * Private data Structure for swr-dmic. All parameters related to
  49. * external mic codec needs to be defined here.
  50. */
  51. struct swr_dmic_priv {
  52. struct device *dev;
  53. struct swr_device *swr_slave;
  54. struct snd_soc_component *component;
  55. struct snd_soc_component_driver *driver;
  56. struct snd_soc_dai_driver *dai_driver;
  57. struct snd_soc_component *supply_component;
  58. u32 micb_num;
  59. struct device_node *wcd_handle;
  60. bool is_wcd_supply;
  61. int is_en_supply;
  62. int port_type;
  63. u8 tx_master_port_map[SWR_DMIC_MAX_PORTS];
  64. };
  65. const char *codec_name_list[] = {
  66. "swr-dmic.01",
  67. "swr-dmic.02",
  68. "swr-dmic.03",
  69. "swr-dmic.04",
  70. };
  71. const char *dai_name_list[] = {
  72. "swr_dmic_tx0",
  73. "swr_dmic_tx1",
  74. "swr_dmic_tx2",
  75. "swr_dmic_tx3",
  76. };
  77. const char *aif_name_list[] = {
  78. "SWR_DMIC_AIF0 Capture",
  79. "SWR_DMIC_AIF1 Capture",
  80. "SWR_DMIC_AIF2 Capture",
  81. "SWR_DMIC_AIF3 Capture",
  82. };
  83. static int swr_dmic_reset(struct swr_device *pdev);
  84. static int swr_dmic_up(struct swr_device *pdev);
  85. static int swr_dmic_down(struct swr_device *pdev);
  86. static inline int swr_dmic_tx_get_slave_port_type_idx(const char *wname,
  87. unsigned int *port_idx)
  88. {
  89. u8 port_type;
  90. if (strnstr(wname, "HIFI", strlen(wname)))
  91. port_type = SWR_DMIC_HIFI_PORT;
  92. else if (strnstr(wname, "LP", strlen(wname)))
  93. port_type = SWR_DMIC_LP_PORT;
  94. else
  95. return -EINVAL;
  96. *port_idx = port_type;
  97. return 0;
  98. }
  99. static inline int swr_dmic_get_master_port_val(int port)
  100. {
  101. int i;
  102. for (i = 0; i < ARRAY_SIZE(swr_master_channel_map); i++)
  103. if (port == swr_master_channel_map[i])
  104. return i;
  105. return 0;
  106. }
  107. static int swr_dmic_tx_master_port_get(struct snd_kcontrol *kcontrol,
  108. struct snd_ctl_elem_value *ucontrol)
  109. {
  110. struct snd_soc_component *component =
  111. snd_soc_kcontrol_component(kcontrol);
  112. struct swr_dmic_priv *swr_dmic = snd_soc_component_get_drvdata(component);
  113. int ret = 0;
  114. int slave_port_idx;
  115. ret = swr_dmic_tx_get_slave_port_type_idx(kcontrol->id.name,
  116. &slave_port_idx);
  117. if (ret) {
  118. dev_dbg(component->dev, "%s: invalid port string\n", __func__);
  119. return ret;
  120. }
  121. swr_dmic->port_type = slave_port_idx;
  122. ucontrol->value.integer.value[0] =
  123. swr_dmic_get_master_port_val(
  124. swr_dmic->tx_master_port_map[slave_port_idx]);
  125. dev_dbg(component->dev, "%s: ucontrol->value.integer.value[0] = %ld\n",
  126. __func__, ucontrol->value.integer.value[0]);
  127. return 0;
  128. }
  129. static int swr_dmic_tx_master_port_put(struct snd_kcontrol *kcontrol,
  130. struct snd_ctl_elem_value *ucontrol)
  131. {
  132. struct snd_soc_component *component =
  133. snd_soc_kcontrol_component(kcontrol);
  134. struct swr_dmic_priv *swr_dmic = snd_soc_component_get_drvdata(component);
  135. int ret = 0;
  136. int slave_port_idx;
  137. ret = swr_dmic_tx_get_slave_port_type_idx(kcontrol->id.name,
  138. &slave_port_idx);
  139. if (ret) {
  140. dev_dbg(component->dev, "%s: invalid port string\n", __func__);
  141. return ret;
  142. }
  143. swr_dmic->port_type = slave_port_idx;
  144. swr_dmic->tx_master_port_map[slave_port_idx] =
  145. swr_master_channel_map[ucontrol->value.enumerated.item[0]];
  146. dev_dbg(component->dev, "%s: slv port id: %d, master_port_type: %d\n",
  147. __func__, slave_port_idx,
  148. swr_dmic->tx_master_port_map[slave_port_idx]);
  149. return 0;
  150. }
  151. static int swr_dmic_port_enable(struct snd_soc_dapm_widget *w,
  152. struct snd_kcontrol *kcontrol, int event)
  153. {
  154. int ret = 0;
  155. struct snd_soc_component *component =
  156. snd_soc_dapm_to_component(w->dapm);
  157. struct swr_dmic_priv *swr_dmic =
  158. snd_soc_component_get_drvdata(component);
  159. u8 ch_mask = 0x01; // only DpnChannelEN1 register is available
  160. u8 num_port = 1;
  161. u8 port_id = swr_dmic->port_type;
  162. u8 port_type = swr_dmic->tx_master_port_map[port_id];
  163. switch (event) {
  164. case SND_SOC_DAPM_POST_PMU:
  165. ret = swr_slvdev_datapath_control(swr_dmic->swr_slave,
  166. swr_dmic->swr_slave->dev_num, true);
  167. break;
  168. case SND_SOC_DAPM_PRE_PMD:
  169. ret = swr_disconnect_port(swr_dmic->swr_slave,
  170. &port_id, num_port, &ch_mask, &port_type);
  171. break;
  172. };
  173. return ret;
  174. }
  175. static int dmic_swr_ctrl(struct snd_soc_dapm_widget *w,
  176. struct snd_kcontrol *kcontrol, int event)
  177. {
  178. int ret = 0;
  179. struct snd_soc_component *component =
  180. snd_soc_dapm_to_component(w->dapm);
  181. struct swr_dmic_priv *swr_dmic =
  182. snd_soc_component_get_drvdata(component);
  183. u8 num_ch = 1;
  184. u8 ch_mask = 0x01; // only DpnChannelEN1 register is available
  185. u32 ch_rate = SWR_CLK_RATE_4P8MHZ;
  186. u8 num_port = 1;
  187. u8 port_type = 0;
  188. u8 port_id = swr_dmic->port_type;
  189. /*
  190. * Port 1 is high quality / 2.4 or 3.072 Mbps
  191. * Port 2 is listen low power / 0.6 or 0.768 Mbps
  192. */
  193. if(swr_dmic->port_type == SWR_DMIC_HIFI_PORT)
  194. ch_rate = SWR_CLK_RATE_2P4MHZ;
  195. else
  196. ch_rate = SWR_CLK_RATE_0P6MHZ;
  197. port_type = swr_dmic->tx_master_port_map[port_id];
  198. dev_dbg(component->dev, "%s port_type: %d event: %d\n", __func__,
  199. port_type, event);
  200. switch (event) {
  201. case SND_SOC_DAPM_PRE_PMU:
  202. ret = swr_connect_port(swr_dmic->swr_slave, &port_id,
  203. num_port, &ch_mask, &ch_rate,
  204. &num_ch, &port_type);
  205. break;
  206. case SND_SOC_DAPM_POST_PMD:
  207. ret = swr_slvdev_datapath_control(swr_dmic->swr_slave,
  208. swr_dmic->swr_slave->dev_num, false);
  209. break;
  210. };
  211. return ret;
  212. }
  213. static int swr_dmic_enable_supply(struct snd_soc_dapm_widget *w,
  214. struct snd_kcontrol *kcontrol,
  215. int event)
  216. {
  217. struct snd_soc_component *component =
  218. snd_soc_dapm_to_component(w->dapm);
  219. struct swr_dmic_priv *swr_dmic =
  220. snd_soc_component_get_drvdata(component);
  221. int ret = 0;
  222. dev_dbg(component->dev, "%s wname: %s event: %d\n", __func__,
  223. w->name, event);
  224. switch (event) {
  225. case SND_SOC_DAPM_PRE_PMU:
  226. ret = swr_dmic_up(swr_dmic->swr_slave);
  227. break;
  228. case SND_SOC_DAPM_POST_PMU:
  229. ret = swr_dmic_reset(swr_dmic->swr_slave);
  230. break;
  231. case SND_SOC_DAPM_POST_PMD:
  232. ret = swr_dmic_down(swr_dmic->swr_slave);
  233. break;
  234. }
  235. if (ret)
  236. dev_dbg(component->dev, "%s wname: %s event: %d ret : %d\n",
  237. __func__, w->name, event, ret);
  238. return ret;
  239. }
  240. static const char * const tx_master_port_text[] = {
  241. "ZERO", "SWRM_TX1_CH1", "SWRM_TX1_CH2", "SWRM_TX1_CH3", "SWRM_TX1_CH4",
  242. "SWRM_TX2_CH1", "SWRM_TX2_CH2", "SWRM_TX2_CH3", "SWRM_TX2_CH4",
  243. "SWRM_TX3_CH1", "SWRM_TX3_CH2", "SWRM_TX3_CH3", "SWRM_TX3_CH4",
  244. "SWRM_PCM_IN",
  245. };
  246. static const struct soc_enum tx_master_port_enum =
  247. SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(tx_master_port_text),
  248. tx_master_port_text);
  249. static const struct snd_kcontrol_new swr_dmic_snd_controls[] = {
  250. SOC_ENUM_EXT("HIFI PortMap", tx_master_port_enum,
  251. swr_dmic_tx_master_port_get, swr_dmic_tx_master_port_put),
  252. SOC_ENUM_EXT("LP PortMap", tx_master_port_enum,
  253. swr_dmic_tx_master_port_get, swr_dmic_tx_master_port_put),
  254. };
  255. static const struct snd_kcontrol_new dmic_switch[] = {
  256. SOC_DAPM_SINGLE("Switch", SND_SOC_NOPM, 0, 1, 0)
  257. };
  258. static const struct snd_soc_dapm_widget swr_dmic_dapm_widgets[] = {
  259. SND_SOC_DAPM_MIXER_E("SWR_DMIC_MIXER", SND_SOC_NOPM, 0, 0,
  260. dmic_switch, ARRAY_SIZE(dmic_switch), dmic_swr_ctrl,
  261. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  262. SND_SOC_DAPM_INPUT("SWR_DMIC"),
  263. SND_SOC_DAPM_SUPPLY_S("SMIC_SUPPLY", 1, SND_SOC_NOPM, 0, 0,
  264. swr_dmic_enable_supply,
  265. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
  266. SND_SOC_DAPM_POST_PMD),
  267. SND_SOC_DAPM_OUT_DRV_E("SMIC_PORT_EN", SND_SOC_NOPM, 0, 0, NULL, 0,
  268. swr_dmic_port_enable,
  269. SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
  270. SND_SOC_DAPM_OUTPUT("SWR_DMIC_OUTPUT"),
  271. };
  272. static const struct snd_soc_dapm_route swr_dmic_audio_map[] = {
  273. {"SWR_DMIC", NULL, "SMIC_SUPPLY"},
  274. {"SWR_DMIC_MIXER", "Switch", "SWR_DMIC"},
  275. {"SMIC_PORT_EN", NULL, "SWR_DMIC_MIXER"},
  276. {"SWR_DMIC_OUTPUT", NULL, "SMIC_PORT_EN"},
  277. };
  278. static int swr_dmic_codec_probe(struct snd_soc_component *component)
  279. {
  280. struct swr_dmic_priv *swr_dmic =
  281. snd_soc_component_get_drvdata(component);
  282. struct snd_soc_dapm_context *dapm =
  283. snd_soc_component_get_dapm(component);
  284. char w_name[100];
  285. if (!swr_dmic)
  286. return -EINVAL;
  287. swr_dmic->component = component;
  288. snd_soc_dapm_ignore_suspend(dapm,
  289. swr_dmic->dai_driver->capture.stream_name);
  290. memset(w_name, 0, 100);
  291. strlcpy(w_name, component->name_prefix, 100);
  292. strlcat(w_name, " SWR_DMIC", 100);
  293. snd_soc_dapm_ignore_suspend(dapm, w_name);
  294. memset(w_name, 0, 100);
  295. strlcpy(w_name, component->name_prefix, 100);
  296. strlcat(w_name, " SMIC_SUPPLY", 100);
  297. snd_soc_dapm_ignore_suspend(dapm, w_name);
  298. memset(w_name, 0, 100);
  299. strlcpy(w_name, component->name_prefix, 100);
  300. strlcat(w_name, " SMIC_PORT_EN", 100);
  301. snd_soc_dapm_ignore_suspend(dapm, w_name);
  302. memset(w_name, 0, 100);
  303. strlcpy(w_name, component->name_prefix, 100);
  304. strlcat(w_name, " SWR_DMIC_OUTPUT", 100);
  305. snd_soc_dapm_ignore_suspend(dapm, w_name);
  306. snd_soc_dapm_sync(dapm);
  307. return 0;
  308. }
  309. static void swr_dmic_codec_remove(struct snd_soc_component *component)
  310. {
  311. struct swr_dmic_priv *swr_dmic =
  312. snd_soc_component_get_drvdata(component);
  313. swr_dmic->component = NULL;
  314. return;
  315. }
  316. static const struct snd_soc_component_driver soc_codec_dev_swr_dmic = {
  317. .name = NULL,
  318. .probe = swr_dmic_codec_probe,
  319. .remove = swr_dmic_codec_remove,
  320. .controls = swr_dmic_snd_controls,
  321. .num_controls = ARRAY_SIZE(swr_dmic_snd_controls),
  322. .dapm_widgets = swr_dmic_dapm_widgets,
  323. .num_dapm_widgets = ARRAY_SIZE(swr_dmic_dapm_widgets),
  324. .dapm_routes = swr_dmic_audio_map,
  325. .num_dapm_routes = ARRAY_SIZE(swr_dmic_audio_map),
  326. };
  327. static int enable_wcd_codec_supply(struct swr_dmic_priv *swr_dmic, bool enable)
  328. {
  329. int rc = 0;
  330. int micb_num = swr_dmic->micb_num;
  331. struct snd_soc_component *component = swr_dmic->supply_component;
  332. if (!component) {
  333. pr_err("%s: component is NULL\n", __func__);
  334. return -EINVAL;
  335. }
  336. if (enable)
  337. rc = wcd938x_codec_force_enable_micbias_v2(component,
  338. SND_SOC_DAPM_PRE_PMU, micb_num);
  339. else
  340. rc = wcd938x_codec_force_enable_micbias_v2(component,
  341. SND_SOC_DAPM_POST_PMD, micb_num);
  342. return rc;
  343. }
  344. static int swr_dmic_parse_supply(struct device_node *np,
  345. struct swr_dmic_priv *swr_dmic)
  346. {
  347. struct platform_device *pdev = NULL;
  348. if (!np || !swr_dmic)
  349. return -EINVAL;
  350. pdev = of_find_device_by_node(np);
  351. if (!pdev)
  352. return -EINVAL;
  353. swr_dmic->supply_component = snd_soc_lookup_component(&pdev->dev, NULL);
  354. return 0;
  355. }
  356. static struct snd_soc_dai_driver swr_dmic_dai[] = {
  357. {
  358. .name = "",
  359. .id = 0,
  360. .capture = {
  361. .stream_name = "",
  362. .rates = (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000 |
  363. SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_48000 |
  364. SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_192000),
  365. .formats = (SNDRV_PCM_FMTBIT_S16_LE |
  366. SNDRV_PCM_FMTBIT_S24_LE |
  367. SNDRV_PCM_FMTBIT_S32_LE),
  368. .rate_max = 192000,
  369. .rate_min = 8000,
  370. .channels_min = 1,
  371. .channels_max = 2,
  372. },
  373. },
  374. };
  375. static int swr_dmic_probe(struct swr_device *pdev)
  376. {
  377. int ret = 0;
  378. int i = 0;
  379. u8 swr_devnum = 0;
  380. int dev_index = -1;
  381. char* prefix_name = NULL;
  382. struct swr_dmic_priv *swr_dmic = NULL;
  383. const char *swr_dmic_name_prefix_of = NULL;
  384. const char *swr_dmic_codec_name_of = NULL;
  385. struct snd_soc_component *component = NULL;
  386. swr_dmic = devm_kzalloc(&pdev->dev, sizeof(struct swr_dmic_priv),
  387. GFP_KERNEL);
  388. if (!swr_dmic)
  389. return -ENOMEM;
  390. ret = of_property_read_u32(pdev->dev.of_node, "qcom,swr-dmic-supply",
  391. &swr_dmic->micb_num);
  392. if (ret) {
  393. dev_dbg(&pdev->dev, "%s: Looking up %s property in node %s failed\n",
  394. __func__, "qcom,swr-dmic-supply",
  395. pdev->dev.of_node->full_name);
  396. goto err;
  397. }
  398. swr_dmic->wcd_handle = of_parse_phandle(pdev->dev.of_node,
  399. "qcom,wcd-handle", 0);
  400. if (!swr_dmic->wcd_handle) {
  401. dev_dbg(&pdev->dev, "%s: no wcd handle listed\n",
  402. __func__);
  403. swr_dmic->is_wcd_supply = false;
  404. } else {
  405. swr_dmic_parse_supply(swr_dmic->wcd_handle, swr_dmic);
  406. swr_dmic->is_wcd_supply = true;
  407. }
  408. if (swr_dmic->is_wcd_supply) {
  409. ret = enable_wcd_codec_supply(swr_dmic, true);
  410. if (ret) {
  411. ret = -EPROBE_DEFER;
  412. goto err;
  413. }
  414. ++swr_dmic->is_en_supply;
  415. }
  416. swr_set_dev_data(pdev, swr_dmic);
  417. swr_dmic->swr_slave = pdev;
  418. ret = of_property_read_string(pdev->dev.of_node, "qcom,swr-dmic-prefix",
  419. &swr_dmic_name_prefix_of);
  420. if (ret) {
  421. dev_dbg(&pdev->dev, "%s: Looking up %s property in node %s failed\n",
  422. __func__, "qcom,swr-dmic-prefix",
  423. pdev->dev.of_node->full_name);
  424. goto dev_err;
  425. }
  426. ret = of_property_read_string(pdev->dev.of_node, "qcom,codec-name",
  427. &swr_dmic_codec_name_of);
  428. if (ret) {
  429. dev_dbg(&pdev->dev, "%s: Looking up %s property in node %s failed\n",
  430. __func__, "qcom,codec-name",
  431. pdev->dev.of_node->full_name);
  432. goto dev_err;
  433. }
  434. /*
  435. * Add 5msec delay to provide sufficient time for
  436. * soundwire auto enumeration of slave devices as
  437. * as per HW requirement.
  438. */
  439. usleep_range(5000, 5010);
  440. ret = swr_get_logical_dev_num(pdev, pdev->addr, &swr_devnum);
  441. if (ret) {
  442. dev_dbg(&pdev->dev,
  443. "%s get devnum %d for dev addr %lx failed\n",
  444. __func__, swr_devnum, pdev->addr);
  445. ret = -EPROBE_DEFER;
  446. goto err;
  447. }
  448. pdev->dev_num = swr_devnum;
  449. swr_dmic->driver = devm_kzalloc(&pdev->dev,
  450. sizeof(struct snd_soc_component_driver), GFP_KERNEL);
  451. if (!swr_dmic->driver) {
  452. ret = -ENOMEM;
  453. goto dev_err;
  454. }
  455. memcpy(swr_dmic->driver, &soc_codec_dev_swr_dmic,
  456. sizeof(struct snd_soc_component_driver));
  457. for (i = 0; i < ARRAY_SIZE(codec_name_list); i++) {
  458. if (!strcmp(swr_dmic_codec_name_of, codec_name_list[i])) {
  459. dev_index = i;
  460. break;
  461. }
  462. }
  463. if (dev_index < 0) {
  464. ret = -EINVAL;
  465. goto dev_err;
  466. }
  467. swr_dmic->driver->name = codec_name_list[dev_index];
  468. swr_dmic->dai_driver = devm_kzalloc(&pdev->dev,
  469. sizeof(struct snd_soc_dai_driver), GFP_KERNEL);
  470. if (!swr_dmic->dai_driver) {
  471. ret = -ENOMEM;
  472. goto dev_err;
  473. }
  474. memcpy(swr_dmic->dai_driver, swr_dmic_dai,
  475. sizeof(struct snd_soc_dai_driver));
  476. swr_dmic->dai_driver->id = dev_index;
  477. swr_dmic->dai_driver->name = dai_name_list[dev_index];
  478. swr_dmic->dai_driver->capture.stream_name = aif_name_list[dev_index];
  479. /* Number of DAI's used is 1 */
  480. ret = snd_soc_register_component(&pdev->dev, swr_dmic->driver,
  481. swr_dmic->dai_driver, 1);
  482. if (ret) {
  483. dev_err(&pdev->dev, "%s: Codec registration failed\n",
  484. __func__);
  485. goto dev_err;
  486. }
  487. component = snd_soc_lookup_component(&pdev->dev,
  488. swr_dmic->driver->name);
  489. if (!component) {
  490. dev_err(&pdev->dev, "%s: could not find swr_dmic component\n",
  491. __func__);
  492. goto dev_err;
  493. }
  494. swr_dmic->component = component;
  495. prefix_name = devm_kzalloc(&pdev->dev,
  496. strlen(swr_dmic_name_prefix_of) + 1,
  497. GFP_KERNEL);
  498. if (!prefix_name) {
  499. ret = -ENOMEM;
  500. goto dev_err;
  501. }
  502. strlcpy(prefix_name, swr_dmic_name_prefix_of,
  503. strlen(swr_dmic_name_prefix_of) + 1);
  504. component->name_prefix = prefix_name;
  505. if (swr_dmic->is_en_supply == 1) {
  506. enable_wcd_codec_supply(swr_dmic, false);
  507. --swr_dmic->is_en_supply;
  508. }
  509. return 0;
  510. dev_err:
  511. if (swr_dmic->is_en_supply == 1) {
  512. enable_wcd_codec_supply(swr_dmic, false);
  513. --swr_dmic->is_en_supply;
  514. }
  515. swr_dmic->is_wcd_supply = false;
  516. swr_dmic->wcd_handle = NULL;
  517. swr_remove_device(pdev);
  518. err:
  519. return ret;
  520. }
  521. static int swr_dmic_remove(struct swr_device *pdev)
  522. {
  523. struct swr_dmic_priv *swr_dmic;
  524. swr_dmic = swr_get_dev_data(pdev);
  525. if (!swr_dmic) {
  526. dev_err(&pdev->dev, "%s: swr_dmic is NULL\n", __func__);
  527. return -EINVAL;
  528. }
  529. snd_soc_unregister_component(&pdev->dev);
  530. swr_set_dev_data(pdev, NULL);
  531. return 0;
  532. }
  533. static int swr_dmic_up(struct swr_device *pdev)
  534. {
  535. int ret = 0;
  536. struct swr_dmic_priv *swr_dmic;
  537. swr_dmic = swr_get_dev_data(pdev);
  538. if (!swr_dmic) {
  539. dev_err(&pdev->dev, "%s: swr_dmic is NULL\n", __func__);
  540. return -EINVAL;
  541. }
  542. ++swr_dmic->is_en_supply;
  543. if (swr_dmic->is_en_supply == 1)
  544. ret = enable_wcd_codec_supply(swr_dmic, true);
  545. return ret;
  546. }
  547. static int swr_dmic_down(struct swr_device *pdev)
  548. {
  549. struct swr_dmic_priv *swr_dmic;
  550. int ret = 0;
  551. swr_dmic = swr_get_dev_data(pdev);
  552. if (!swr_dmic) {
  553. dev_err(&pdev->dev, "%s: swr_dmic is NULL\n", __func__);
  554. return -EINVAL;
  555. }
  556. dev_dbg(&pdev->dev, "%s: is_en_supply: %d\n",
  557. __func__, swr_dmic->is_en_supply);
  558. --swr_dmic->is_en_supply;
  559. if (swr_dmic->is_en_supply < 0) {
  560. dev_warn(&pdev->dev, "%s: mismatch in supply count %d\n",
  561. __func__, swr_dmic->is_en_supply);
  562. swr_dmic->is_en_supply = 0;
  563. goto done;
  564. }
  565. if (!swr_dmic->is_en_supply)
  566. enable_wcd_codec_supply(swr_dmic, false);
  567. done:
  568. return ret;
  569. }
  570. static int swr_dmic_reset(struct swr_device *pdev)
  571. {
  572. struct swr_dmic_priv *swr_dmic;
  573. u8 retry = 5;
  574. u8 devnum = 0;
  575. swr_dmic = swr_get_dev_data(pdev);
  576. if (!swr_dmic) {
  577. dev_err(&pdev->dev, "%s: swr_dmic is NULL\n", __func__);
  578. return -EINVAL;
  579. }
  580. while (swr_get_logical_dev_num(pdev, pdev->addr, &devnum) && retry--) {
  581. /* Retry after 1 msec delay */
  582. usleep_range(1000, 1100);
  583. }
  584. pdev->dev_num = devnum;
  585. dev_dbg(&pdev->dev, "%s: devnum: %d\n", __func__, devnum);
  586. return 0;
  587. }
  588. #ifdef CONFIG_PM_SLEEP
  589. static int swr_dmic_suspend(struct device *dev)
  590. {
  591. dev_dbg(dev, "%s: system suspend\n", __func__);
  592. return 0;
  593. }
  594. static int swr_dmic_resume(struct device *dev)
  595. {
  596. struct swr_dmic_priv *swr_dmic = swr_get_dev_data(to_swr_device(dev));
  597. if (!swr_dmic) {
  598. dev_err(dev, "%s: swr_dmic private data is NULL\n", __func__);
  599. return -EINVAL;
  600. }
  601. dev_dbg(dev, "%s: system resume\n", __func__);
  602. return 0;
  603. }
  604. #endif /* CONFIG_PM_SLEEP */
  605. static const struct dev_pm_ops swr_dmic_pm_ops = {
  606. SET_SYSTEM_SLEEP_PM_OPS(swr_dmic_suspend, swr_dmic_resume)
  607. };
  608. static const struct swr_device_id swr_dmic_id[] = {
  609. {"swr-dmic", 0},
  610. {}
  611. };
  612. static const struct of_device_id swr_dmic_dt_match[] = {
  613. {
  614. .compatible = "qcom,swr-dmic",
  615. },
  616. {}
  617. };
  618. static struct swr_driver swr_dmic_driver = {
  619. .driver = {
  620. .name = "swr-dmic",
  621. .owner = THIS_MODULE,
  622. .pm = &swr_dmic_pm_ops,
  623. .of_match_table = swr_dmic_dt_match,
  624. },
  625. .probe = swr_dmic_probe,
  626. .remove = swr_dmic_remove,
  627. .id_table = swr_dmic_id,
  628. .device_down = swr_dmic_down,
  629. };
  630. static int __init swr_dmic_init(void)
  631. {
  632. return swr_driver_register(&swr_dmic_driver);
  633. }
  634. static void __exit swr_dmic_exit(void)
  635. {
  636. swr_driver_unregister(&swr_dmic_driver);
  637. }
  638. module_init(swr_dmic_init);
  639. module_exit(swr_dmic_exit);
  640. MODULE_DESCRIPTION("SWR DMIC driver");
  641. MODULE_LICENSE("GPL v2");