swr-dmic.c 20 KB

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