swr-dmic.c 17 KB

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