swr-dmic.c 20 KB

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