swr-dmic.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726
  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. if (!swr_dmic)
  283. return -EINVAL;
  284. swr_dmic->component = component;
  285. return 0;
  286. }
  287. static void swr_dmic_codec_remove(struct snd_soc_component *component)
  288. {
  289. struct swr_dmic_priv *swr_dmic =
  290. snd_soc_component_get_drvdata(component);
  291. swr_dmic->component = NULL;
  292. return;
  293. }
  294. static const struct snd_soc_component_driver soc_codec_dev_swr_dmic = {
  295. .name = NULL,
  296. .probe = swr_dmic_codec_probe,
  297. .remove = swr_dmic_codec_remove,
  298. .controls = swr_dmic_snd_controls,
  299. .num_controls = ARRAY_SIZE(swr_dmic_snd_controls),
  300. .dapm_widgets = swr_dmic_dapm_widgets,
  301. .num_dapm_widgets = ARRAY_SIZE(swr_dmic_dapm_widgets),
  302. .dapm_routes = swr_dmic_audio_map,
  303. .num_dapm_routes = ARRAY_SIZE(swr_dmic_audio_map),
  304. };
  305. static int enable_wcd_codec_supply(struct swr_dmic_priv *swr_dmic, bool enable)
  306. {
  307. int rc = 0;
  308. int micb_num = swr_dmic->micb_num;
  309. struct snd_soc_component *component = swr_dmic->supply_component;
  310. if (!component) {
  311. pr_err("%s: component is NULL\n", __func__);
  312. return -EINVAL;
  313. }
  314. if (enable)
  315. rc = wcd938x_codec_force_enable_micbias_v2(component,
  316. SND_SOC_DAPM_PRE_PMU, micb_num);
  317. else
  318. rc = wcd938x_codec_force_enable_micbias_v2(component,
  319. SND_SOC_DAPM_POST_PMD, micb_num);
  320. return rc;
  321. }
  322. static int swr_dmic_parse_supply(struct device_node *np,
  323. struct swr_dmic_priv *swr_dmic)
  324. {
  325. struct platform_device *pdev = NULL;
  326. if (!np || !swr_dmic)
  327. return -EINVAL;
  328. pdev = of_find_device_by_node(np);
  329. if (!pdev)
  330. return -EINVAL;
  331. swr_dmic->supply_component = snd_soc_lookup_component(&pdev->dev, NULL);
  332. return 0;
  333. }
  334. static struct snd_soc_dai_driver swr_dmic_dai[] = {
  335. {
  336. .name = "",
  337. .id = 0,
  338. .capture = {
  339. .stream_name = "",
  340. .rates = (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000 |
  341. SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_48000 |
  342. SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_192000),
  343. .formats = (SNDRV_PCM_FMTBIT_S16_LE |
  344. SNDRV_PCM_FMTBIT_S24_LE |
  345. SNDRV_PCM_FMTBIT_S32_LE),
  346. .rate_max = 192000,
  347. .rate_min = 8000,
  348. .channels_min = 1,
  349. .channels_max = 2,
  350. },
  351. },
  352. };
  353. static int swr_dmic_probe(struct swr_device *pdev)
  354. {
  355. int ret = 0;
  356. int i = 0;
  357. u8 swr_devnum = 0;
  358. int dev_index = -1;
  359. char* prefix_name = NULL;
  360. struct swr_dmic_priv *swr_dmic = NULL;
  361. const char *swr_dmic_name_prefix_of = NULL;
  362. const char *swr_dmic_codec_name_of = NULL;
  363. struct snd_soc_component *component = NULL;
  364. swr_dmic = devm_kzalloc(&pdev->dev, sizeof(struct swr_dmic_priv),
  365. GFP_KERNEL);
  366. if (!swr_dmic)
  367. return -ENOMEM;
  368. ret = of_property_read_u32(pdev->dev.of_node, "qcom,swr-dmic-supply",
  369. &swr_dmic->micb_num);
  370. if (ret) {
  371. dev_dbg(&pdev->dev, "%s: Looking up %s property in node %s failed\n",
  372. __func__, "qcom,swr-dmic-supply",
  373. pdev->dev.of_node->full_name);
  374. goto err;
  375. }
  376. swr_dmic->wcd_handle = of_parse_phandle(pdev->dev.of_node,
  377. "qcom,wcd-handle", 0);
  378. if (!swr_dmic->wcd_handle) {
  379. dev_dbg(&pdev->dev, "%s: no wcd handle listed\n",
  380. __func__);
  381. swr_dmic->is_wcd_supply = false;
  382. } else {
  383. swr_dmic_parse_supply(swr_dmic->wcd_handle, swr_dmic);
  384. swr_dmic->is_wcd_supply = true;
  385. }
  386. if (swr_dmic->is_wcd_supply) {
  387. ret = enable_wcd_codec_supply(swr_dmic, true);
  388. if (ret) {
  389. ret = -EPROBE_DEFER;
  390. goto err;
  391. }
  392. ++swr_dmic->is_en_supply;
  393. }
  394. swr_set_dev_data(pdev, swr_dmic);
  395. swr_dmic->swr_slave = pdev;
  396. ret = of_property_read_string(pdev->dev.of_node, "qcom,swr-dmic-prefix",
  397. &swr_dmic_name_prefix_of);
  398. if (ret) {
  399. dev_dbg(&pdev->dev, "%s: Looking up %s property in node %s failed\n",
  400. __func__, "qcom,swr-dmic-prefix",
  401. pdev->dev.of_node->full_name);
  402. goto dev_err;
  403. }
  404. ret = of_property_read_string(pdev->dev.of_node, "qcom,codec-name",
  405. &swr_dmic_codec_name_of);
  406. if (ret) {
  407. dev_dbg(&pdev->dev, "%s: Looking up %s property in node %s failed\n",
  408. __func__, "qcom,codec-name",
  409. pdev->dev.of_node->full_name);
  410. goto dev_err;
  411. }
  412. /*
  413. * Add 5msec delay to provide sufficient time for
  414. * soundwire auto enumeration of slave devices as
  415. * as per HW requirement.
  416. */
  417. usleep_range(5000, 5010);
  418. ret = swr_get_logical_dev_num(pdev, pdev->addr, &swr_devnum);
  419. if (ret) {
  420. dev_dbg(&pdev->dev,
  421. "%s get devnum %d for dev addr %lx failed\n",
  422. __func__, swr_devnum, pdev->addr);
  423. ret = -EPROBE_DEFER;
  424. goto err;
  425. }
  426. pdev->dev_num = swr_devnum;
  427. swr_dmic->driver = devm_kzalloc(&pdev->dev,
  428. sizeof(struct snd_soc_component_driver), GFP_KERNEL);
  429. if (!swr_dmic->driver) {
  430. ret = -ENOMEM;
  431. goto dev_err;
  432. }
  433. memcpy(swr_dmic->driver, &soc_codec_dev_swr_dmic,
  434. sizeof(struct snd_soc_component_driver));
  435. for (i = 0; i < ARRAY_SIZE(codec_name_list); i++) {
  436. if (!strcmp(swr_dmic_codec_name_of, codec_name_list[i])) {
  437. dev_index = i;
  438. break;
  439. }
  440. }
  441. if (dev_index < 0) {
  442. ret = -EINVAL;
  443. goto dev_err;
  444. }
  445. swr_dmic->driver->name = codec_name_list[dev_index];
  446. swr_dmic->dai_driver = devm_kzalloc(&pdev->dev,
  447. sizeof(struct snd_soc_dai_driver), GFP_KERNEL);
  448. if (!swr_dmic->dai_driver) {
  449. ret = -ENOMEM;
  450. goto dev_err;
  451. }
  452. memcpy(swr_dmic->dai_driver, swr_dmic_dai,
  453. sizeof(struct snd_soc_dai_driver));
  454. swr_dmic->dai_driver->id = dev_index;
  455. swr_dmic->dai_driver->name = dai_name_list[dev_index];
  456. swr_dmic->dai_driver->capture.stream_name = aif_name_list[dev_index];
  457. /* Number of DAI's used is 1 */
  458. ret = snd_soc_register_component(&pdev->dev, swr_dmic->driver,
  459. swr_dmic->dai_driver, 1);
  460. if (ret) {
  461. dev_err(&pdev->dev, "%s: Codec registration failed\n",
  462. __func__);
  463. goto dev_err;
  464. }
  465. component = snd_soc_lookup_component(&pdev->dev,
  466. swr_dmic->driver->name);
  467. if (!component) {
  468. dev_err(&pdev->dev, "%s: could not find swr_dmic component\n",
  469. __func__);
  470. goto dev_err;
  471. }
  472. swr_dmic->component = component;
  473. prefix_name = devm_kzalloc(&pdev->dev,
  474. strlen(swr_dmic_name_prefix_of) + 1,
  475. GFP_KERNEL);
  476. if (!prefix_name) {
  477. ret = -ENOMEM;
  478. goto dev_err;
  479. }
  480. strlcpy(prefix_name, swr_dmic_name_prefix_of,
  481. strlen(swr_dmic_name_prefix_of) + 1);
  482. component->name_prefix = prefix_name;
  483. if (swr_dmic->is_en_supply == 1) {
  484. enable_wcd_codec_supply(swr_dmic, false);
  485. --swr_dmic->is_en_supply;
  486. }
  487. return 0;
  488. dev_err:
  489. if (swr_dmic->is_en_supply == 1) {
  490. enable_wcd_codec_supply(swr_dmic, false);
  491. --swr_dmic->is_en_supply;
  492. }
  493. swr_dmic->is_wcd_supply = false;
  494. swr_dmic->wcd_handle = NULL;
  495. swr_remove_device(pdev);
  496. err:
  497. return ret;
  498. }
  499. static int swr_dmic_remove(struct swr_device *pdev)
  500. {
  501. struct swr_dmic_priv *swr_dmic;
  502. swr_dmic = swr_get_dev_data(pdev);
  503. if (!swr_dmic) {
  504. dev_err(&pdev->dev, "%s: swr_dmic is NULL\n", __func__);
  505. return -EINVAL;
  506. }
  507. snd_soc_unregister_component(&pdev->dev);
  508. swr_set_dev_data(pdev, NULL);
  509. return 0;
  510. }
  511. static int swr_dmic_up(struct swr_device *pdev)
  512. {
  513. int ret = 0;
  514. struct swr_dmic_priv *swr_dmic;
  515. swr_dmic = swr_get_dev_data(pdev);
  516. if (!swr_dmic) {
  517. dev_err(&pdev->dev, "%s: swr_dmic is NULL\n", __func__);
  518. return -EINVAL;
  519. }
  520. ++swr_dmic->is_en_supply;
  521. if (swr_dmic->is_en_supply == 1)
  522. ret = enable_wcd_codec_supply(swr_dmic, true);
  523. return ret;
  524. }
  525. static int swr_dmic_down(struct swr_device *pdev)
  526. {
  527. struct swr_dmic_priv *swr_dmic;
  528. int ret = 0;
  529. swr_dmic = swr_get_dev_data(pdev);
  530. if (!swr_dmic) {
  531. dev_err(&pdev->dev, "%s: swr_dmic is NULL\n", __func__);
  532. return -EINVAL;
  533. }
  534. --swr_dmic->is_en_supply;
  535. if (swr_dmic->is_en_supply < 0) {
  536. dev_warn(&pdev->dev, "%s: mismatch in supply count %d\n",
  537. __func__, swr_dmic->is_en_supply);
  538. swr_dmic->is_en_supply = 0;
  539. goto done;
  540. }
  541. if (!swr_dmic->is_en_supply)
  542. enable_wcd_codec_supply(swr_dmic, false);
  543. done:
  544. return ret;
  545. }
  546. static int swr_dmic_reset(struct swr_device *pdev)
  547. {
  548. struct swr_dmic_priv *swr_dmic;
  549. u8 retry = 5;
  550. u8 devnum = 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. while (swr_get_logical_dev_num(pdev, pdev->addr, &devnum) && retry--) {
  557. /* Retry after 1 msec delay */
  558. usleep_range(1000, 1100);
  559. }
  560. pdev->dev_num = devnum;
  561. dev_dbg(&pdev->dev, "%s: devnum: %d\n", __func__, devnum);
  562. return 0;
  563. }
  564. #ifdef CONFIG_PM_SLEEP
  565. static int swr_dmic_suspend(struct device *dev)
  566. {
  567. dev_dbg(dev, "%s: system suspend\n", __func__);
  568. return 0;
  569. }
  570. static int swr_dmic_resume(struct device *dev)
  571. {
  572. struct swr_dmic_priv *swr_dmic = swr_get_dev_data(to_swr_device(dev));
  573. if (!swr_dmic) {
  574. dev_err(dev, "%s: swr_dmic private data is NULL\n", __func__);
  575. return -EINVAL;
  576. }
  577. dev_dbg(dev, "%s: system resume\n", __func__);
  578. return 0;
  579. }
  580. #endif /* CONFIG_PM_SLEEP */
  581. static const struct dev_pm_ops swr_dmic_pm_ops = {
  582. SET_SYSTEM_SLEEP_PM_OPS(swr_dmic_suspend, swr_dmic_resume)
  583. };
  584. static const struct swr_device_id swr_dmic_id[] = {
  585. {"swr-dmic", 0},
  586. {}
  587. };
  588. static const struct of_device_id swr_dmic_dt_match[] = {
  589. {
  590. .compatible = "qcom,swr-dmic",
  591. },
  592. {}
  593. };
  594. static struct swr_driver swr_dmic_driver = {
  595. .driver = {
  596. .name = "swr-dmic",
  597. .owner = THIS_MODULE,
  598. .pm = &swr_dmic_pm_ops,
  599. .of_match_table = swr_dmic_dt_match,
  600. },
  601. .probe = swr_dmic_probe,
  602. .remove = swr_dmic_remove,
  603. .id_table = swr_dmic_id,
  604. .device_down = swr_dmic_down,
  605. };
  606. static int __init swr_dmic_init(void)
  607. {
  608. return swr_driver_register(&swr_dmic_driver);
  609. }
  610. static void __exit swr_dmic_exit(void)
  611. {
  612. swr_driver_unregister(&swr_dmic_driver);
  613. }
  614. module_init(swr_dmic_init);
  615. module_exit(swr_dmic_exit);
  616. MODULE_DESCRIPTION("SWR DMIC driver");
  617. MODULE_LICENSE("GPL v2");