swr-dmic.c 17 KB

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