swr-dmic.c 23 KB

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