swr-dmic.c 23 KB

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