axg-tdm-interface.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  1. // SPDX-License-Identifier: (GPL-2.0 OR MIT)
  2. //
  3. // Copyright (c) 2018 BayLibre, SAS.
  4. // Author: Jerome Brunet <jbrunet@baylibre.com>
  5. #include <linux/clk.h>
  6. #include <linux/module.h>
  7. #include <linux/of_platform.h>
  8. #include <sound/pcm_params.h>
  9. #include <sound/soc.h>
  10. #include <sound/soc-dai.h>
  11. #include "axg-tdm.h"
  12. enum {
  13. TDM_IFACE_PAD,
  14. TDM_IFACE_LOOPBACK,
  15. };
  16. static unsigned int axg_tdm_slots_total(u32 *mask)
  17. {
  18. unsigned int slots = 0;
  19. int i;
  20. if (!mask)
  21. return 0;
  22. /* Count the total number of slots provided by all 4 lanes */
  23. for (i = 0; i < AXG_TDM_NUM_LANES; i++)
  24. slots += hweight32(mask[i]);
  25. return slots;
  26. }
  27. int axg_tdm_set_tdm_slots(struct snd_soc_dai *dai, u32 *tx_mask,
  28. u32 *rx_mask, unsigned int slots,
  29. unsigned int slot_width)
  30. {
  31. struct axg_tdm_iface *iface = snd_soc_dai_get_drvdata(dai);
  32. struct axg_tdm_stream *tx = (struct axg_tdm_stream *)
  33. dai->playback_dma_data;
  34. struct axg_tdm_stream *rx = (struct axg_tdm_stream *)
  35. dai->capture_dma_data;
  36. unsigned int tx_slots, rx_slots;
  37. tx_slots = axg_tdm_slots_total(tx_mask);
  38. rx_slots = axg_tdm_slots_total(rx_mask);
  39. /* We should at least have a slot for a valid interface */
  40. if (!tx_slots && !rx_slots) {
  41. dev_err(dai->dev, "interface has no slot\n");
  42. return -EINVAL;
  43. }
  44. /*
  45. * Amend the dai driver channel number and let dpcm channel merge do
  46. * its job
  47. */
  48. if (tx) {
  49. tx->mask = tx_mask;
  50. dai->driver->playback.channels_max = tx_slots;
  51. }
  52. if (rx) {
  53. rx->mask = rx_mask;
  54. dai->driver->capture.channels_max = rx_slots;
  55. }
  56. iface->slots = slots;
  57. switch (slot_width) {
  58. case 0:
  59. /* defaults width to 32 if not provided */
  60. iface->slot_width = 32;
  61. break;
  62. case 8:
  63. case 16:
  64. case 24:
  65. case 32:
  66. iface->slot_width = slot_width;
  67. break;
  68. default:
  69. dev_err(dai->dev, "unsupported slot width: %d\n", slot_width);
  70. return -EINVAL;
  71. }
  72. return 0;
  73. }
  74. EXPORT_SYMBOL_GPL(axg_tdm_set_tdm_slots);
  75. static int axg_tdm_iface_set_sysclk(struct snd_soc_dai *dai, int clk_id,
  76. unsigned int freq, int dir)
  77. {
  78. struct axg_tdm_iface *iface = snd_soc_dai_get_drvdata(dai);
  79. int ret = -ENOTSUPP;
  80. if (dir == SND_SOC_CLOCK_OUT && clk_id == 0) {
  81. if (!iface->mclk) {
  82. dev_warn(dai->dev, "master clock not provided\n");
  83. } else {
  84. ret = clk_set_rate(iface->mclk, freq);
  85. if (!ret)
  86. iface->mclk_rate = freq;
  87. }
  88. }
  89. return ret;
  90. }
  91. static int axg_tdm_iface_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
  92. {
  93. struct axg_tdm_iface *iface = snd_soc_dai_get_drvdata(dai);
  94. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  95. case SND_SOC_DAIFMT_CBS_CFS:
  96. if (!iface->mclk) {
  97. dev_err(dai->dev, "cpu clock master: mclk missing\n");
  98. return -ENODEV;
  99. }
  100. break;
  101. case SND_SOC_DAIFMT_CBM_CFM:
  102. break;
  103. case SND_SOC_DAIFMT_CBS_CFM:
  104. case SND_SOC_DAIFMT_CBM_CFS:
  105. dev_err(dai->dev, "only CBS_CFS and CBM_CFM are supported\n");
  106. /* Fall-through */
  107. default:
  108. return -EINVAL;
  109. }
  110. iface->fmt = fmt;
  111. return 0;
  112. }
  113. static int axg_tdm_iface_startup(struct snd_pcm_substream *substream,
  114. struct snd_soc_dai *dai)
  115. {
  116. struct axg_tdm_iface *iface = snd_soc_dai_get_drvdata(dai);
  117. struct axg_tdm_stream *ts =
  118. snd_soc_dai_get_dma_data(dai, substream);
  119. int ret;
  120. if (!axg_tdm_slots_total(ts->mask)) {
  121. dev_err(dai->dev, "interface has not slots\n");
  122. return -EINVAL;
  123. }
  124. /* Apply component wide rate symmetry */
  125. if (dai->component->active) {
  126. ret = snd_pcm_hw_constraint_single(substream->runtime,
  127. SNDRV_PCM_HW_PARAM_RATE,
  128. iface->rate);
  129. if (ret < 0) {
  130. dev_err(dai->dev,
  131. "can't set iface rate constraint\n");
  132. return ret;
  133. }
  134. }
  135. return 0;
  136. }
  137. static int axg_tdm_iface_set_stream(struct snd_pcm_substream *substream,
  138. struct snd_pcm_hw_params *params,
  139. struct snd_soc_dai *dai)
  140. {
  141. struct axg_tdm_iface *iface = snd_soc_dai_get_drvdata(dai);
  142. struct axg_tdm_stream *ts = snd_soc_dai_get_dma_data(dai, substream);
  143. unsigned int channels = params_channels(params);
  144. unsigned int width = params_width(params);
  145. /* Save rate and sample_bits for component symmetry */
  146. iface->rate = params_rate(params);
  147. /* Make sure this interface can cope with the stream */
  148. if (axg_tdm_slots_total(ts->mask) < channels) {
  149. dev_err(dai->dev, "not enough slots for channels\n");
  150. return -EINVAL;
  151. }
  152. if (iface->slot_width < width) {
  153. dev_err(dai->dev, "incompatible slots width for stream\n");
  154. return -EINVAL;
  155. }
  156. /* Save the parameter for tdmout/tdmin widgets */
  157. ts->physical_width = params_physical_width(params);
  158. ts->width = params_width(params);
  159. ts->channels = params_channels(params);
  160. return 0;
  161. }
  162. static int axg_tdm_iface_set_lrclk(struct snd_soc_dai *dai,
  163. struct snd_pcm_hw_params *params)
  164. {
  165. struct axg_tdm_iface *iface = snd_soc_dai_get_drvdata(dai);
  166. unsigned int ratio_num;
  167. int ret;
  168. ret = clk_set_rate(iface->lrclk, params_rate(params));
  169. if (ret) {
  170. dev_err(dai->dev, "setting sample clock failed: %d\n", ret);
  171. return ret;
  172. }
  173. switch (iface->fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  174. case SND_SOC_DAIFMT_I2S:
  175. case SND_SOC_DAIFMT_LEFT_J:
  176. case SND_SOC_DAIFMT_RIGHT_J:
  177. /* 50% duty cycle ratio */
  178. ratio_num = 1;
  179. break;
  180. case SND_SOC_DAIFMT_DSP_A:
  181. case SND_SOC_DAIFMT_DSP_B:
  182. /*
  183. * A zero duty cycle ratio will result in setting the mininum
  184. * ratio possible which, for this clock, is 1 cycle of the
  185. * parent bclk clock high and the rest low, This is exactly
  186. * what we want here.
  187. */
  188. ratio_num = 0;
  189. break;
  190. default:
  191. return -EINVAL;
  192. }
  193. ret = clk_set_duty_cycle(iface->lrclk, ratio_num, 2);
  194. if (ret) {
  195. dev_err(dai->dev,
  196. "setting sample clock duty cycle failed: %d\n", ret);
  197. return ret;
  198. }
  199. /* Set sample clock inversion */
  200. ret = clk_set_phase(iface->lrclk,
  201. axg_tdm_lrclk_invert(iface->fmt) ? 180 : 0);
  202. if (ret) {
  203. dev_err(dai->dev,
  204. "setting sample clock phase failed: %d\n", ret);
  205. return ret;
  206. }
  207. return 0;
  208. }
  209. static int axg_tdm_iface_set_sclk(struct snd_soc_dai *dai,
  210. struct snd_pcm_hw_params *params)
  211. {
  212. struct axg_tdm_iface *iface = snd_soc_dai_get_drvdata(dai);
  213. unsigned long srate;
  214. int ret;
  215. srate = iface->slots * iface->slot_width * params_rate(params);
  216. if (!iface->mclk_rate) {
  217. /* If no specific mclk is requested, default to bit clock * 4 */
  218. clk_set_rate(iface->mclk, 4 * srate);
  219. } else {
  220. /* Check if we can actually get the bit clock from mclk */
  221. if (iface->mclk_rate % srate) {
  222. dev_err(dai->dev,
  223. "can't derive sclk %lu from mclk %lu\n",
  224. srate, iface->mclk_rate);
  225. return -EINVAL;
  226. }
  227. }
  228. ret = clk_set_rate(iface->sclk, srate);
  229. if (ret) {
  230. dev_err(dai->dev, "setting bit clock failed: %d\n", ret);
  231. return ret;
  232. }
  233. /* Set the bit clock inversion */
  234. ret = clk_set_phase(iface->sclk,
  235. axg_tdm_sclk_invert(iface->fmt) ? 0 : 180);
  236. if (ret) {
  237. dev_err(dai->dev, "setting bit clock phase failed: %d\n", ret);
  238. return ret;
  239. }
  240. return ret;
  241. }
  242. static int axg_tdm_iface_hw_params(struct snd_pcm_substream *substream,
  243. struct snd_pcm_hw_params *params,
  244. struct snd_soc_dai *dai)
  245. {
  246. struct axg_tdm_iface *iface = snd_soc_dai_get_drvdata(dai);
  247. int ret;
  248. switch (iface->fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  249. case SND_SOC_DAIFMT_I2S:
  250. case SND_SOC_DAIFMT_LEFT_J:
  251. case SND_SOC_DAIFMT_RIGHT_J:
  252. if (iface->slots > 2) {
  253. dev_err(dai->dev, "bad slot number for format: %d\n",
  254. iface->slots);
  255. return -EINVAL;
  256. }
  257. break;
  258. case SND_SOC_DAI_FORMAT_DSP_A:
  259. case SND_SOC_DAI_FORMAT_DSP_B:
  260. break;
  261. default:
  262. dev_err(dai->dev, "unsupported dai format\n");
  263. return -EINVAL;
  264. }
  265. ret = axg_tdm_iface_set_stream(substream, params, dai);
  266. if (ret)
  267. return ret;
  268. if ((iface->fmt & SND_SOC_DAIFMT_MASTER_MASK) ==
  269. SND_SOC_DAIFMT_CBS_CFS) {
  270. ret = axg_tdm_iface_set_sclk(dai, params);
  271. if (ret)
  272. return ret;
  273. ret = axg_tdm_iface_set_lrclk(dai, params);
  274. if (ret)
  275. return ret;
  276. }
  277. return 0;
  278. }
  279. static int axg_tdm_iface_hw_free(struct snd_pcm_substream *substream,
  280. struct snd_soc_dai *dai)
  281. {
  282. struct axg_tdm_stream *ts = snd_soc_dai_get_dma_data(dai, substream);
  283. /* Stop all attached formatters */
  284. axg_tdm_stream_stop(ts);
  285. return 0;
  286. }
  287. static int axg_tdm_iface_prepare(struct snd_pcm_substream *substream,
  288. struct snd_soc_dai *dai)
  289. {
  290. struct axg_tdm_stream *ts = snd_soc_dai_get_dma_data(dai, substream);
  291. /* Force all attached formatters to update */
  292. return axg_tdm_stream_reset(ts);
  293. }
  294. static int axg_tdm_iface_remove_dai(struct snd_soc_dai *dai)
  295. {
  296. if (dai->capture_dma_data)
  297. axg_tdm_stream_free(dai->capture_dma_data);
  298. if (dai->playback_dma_data)
  299. axg_tdm_stream_free(dai->playback_dma_data);
  300. return 0;
  301. }
  302. static int axg_tdm_iface_probe_dai(struct snd_soc_dai *dai)
  303. {
  304. struct axg_tdm_iface *iface = snd_soc_dai_get_drvdata(dai);
  305. if (dai->capture_widget) {
  306. dai->capture_dma_data = axg_tdm_stream_alloc(iface);
  307. if (!dai->capture_dma_data)
  308. return -ENOMEM;
  309. }
  310. if (dai->playback_widget) {
  311. dai->playback_dma_data = axg_tdm_stream_alloc(iface);
  312. if (!dai->playback_dma_data) {
  313. axg_tdm_iface_remove_dai(dai);
  314. return -ENOMEM;
  315. }
  316. }
  317. return 0;
  318. }
  319. static const struct snd_soc_dai_ops axg_tdm_iface_ops = {
  320. .set_sysclk = axg_tdm_iface_set_sysclk,
  321. .set_fmt = axg_tdm_iface_set_fmt,
  322. .startup = axg_tdm_iface_startup,
  323. .hw_params = axg_tdm_iface_hw_params,
  324. .prepare = axg_tdm_iface_prepare,
  325. .hw_free = axg_tdm_iface_hw_free,
  326. };
  327. /* TDM Backend DAIs */
  328. static const struct snd_soc_dai_driver axg_tdm_iface_dai_drv[] = {
  329. [TDM_IFACE_PAD] = {
  330. .name = "TDM Pad",
  331. .playback = {
  332. .stream_name = "Playback",
  333. .channels_min = 1,
  334. .channels_max = AXG_TDM_CHANNEL_MAX,
  335. .rates = AXG_TDM_RATES,
  336. .formats = AXG_TDM_FORMATS,
  337. },
  338. .capture = {
  339. .stream_name = "Capture",
  340. .channels_min = 1,
  341. .channels_max = AXG_TDM_CHANNEL_MAX,
  342. .rates = AXG_TDM_RATES,
  343. .formats = AXG_TDM_FORMATS,
  344. },
  345. .id = TDM_IFACE_PAD,
  346. .ops = &axg_tdm_iface_ops,
  347. .probe = axg_tdm_iface_probe_dai,
  348. .remove = axg_tdm_iface_remove_dai,
  349. },
  350. [TDM_IFACE_LOOPBACK] = {
  351. .name = "TDM Loopback",
  352. .capture = {
  353. .stream_name = "Loopback",
  354. .channels_min = 1,
  355. .channels_max = AXG_TDM_CHANNEL_MAX,
  356. .rates = AXG_TDM_RATES,
  357. .formats = AXG_TDM_FORMATS,
  358. },
  359. .id = TDM_IFACE_LOOPBACK,
  360. .ops = &axg_tdm_iface_ops,
  361. .probe = axg_tdm_iface_probe_dai,
  362. .remove = axg_tdm_iface_remove_dai,
  363. },
  364. };
  365. static int axg_tdm_iface_set_bias_level(struct snd_soc_component *component,
  366. enum snd_soc_bias_level level)
  367. {
  368. struct axg_tdm_iface *iface = snd_soc_component_get_drvdata(component);
  369. enum snd_soc_bias_level now =
  370. snd_soc_component_get_bias_level(component);
  371. int ret = 0;
  372. switch (level) {
  373. case SND_SOC_BIAS_PREPARE:
  374. if (now == SND_SOC_BIAS_STANDBY)
  375. ret = clk_prepare_enable(iface->mclk);
  376. break;
  377. case SND_SOC_BIAS_STANDBY:
  378. if (now == SND_SOC_BIAS_PREPARE)
  379. clk_disable_unprepare(iface->mclk);
  380. break;
  381. case SND_SOC_BIAS_OFF:
  382. case SND_SOC_BIAS_ON:
  383. break;
  384. }
  385. return ret;
  386. }
  387. static const struct snd_soc_dapm_widget axg_tdm_iface_dapm_widgets[] = {
  388. SND_SOC_DAPM_SIGGEN("Playback Signal"),
  389. };
  390. static const struct snd_soc_dapm_route axg_tdm_iface_dapm_routes[] = {
  391. { "Loopback", NULL, "Playback Signal" },
  392. };
  393. static const struct snd_soc_component_driver axg_tdm_iface_component_drv = {
  394. .dapm_widgets = axg_tdm_iface_dapm_widgets,
  395. .num_dapm_widgets = ARRAY_SIZE(axg_tdm_iface_dapm_widgets),
  396. .dapm_routes = axg_tdm_iface_dapm_routes,
  397. .num_dapm_routes = ARRAY_SIZE(axg_tdm_iface_dapm_routes),
  398. .set_bias_level = axg_tdm_iface_set_bias_level,
  399. };
  400. static const struct of_device_id axg_tdm_iface_of_match[] = {
  401. { .compatible = "amlogic,axg-tdm-iface", },
  402. {}
  403. };
  404. MODULE_DEVICE_TABLE(of, axg_tdm_iface_of_match);
  405. static int axg_tdm_iface_probe(struct platform_device *pdev)
  406. {
  407. struct device *dev = &pdev->dev;
  408. struct snd_soc_dai_driver *dai_drv;
  409. struct axg_tdm_iface *iface;
  410. int ret, i;
  411. iface = devm_kzalloc(dev, sizeof(*iface), GFP_KERNEL);
  412. if (!iface)
  413. return -ENOMEM;
  414. platform_set_drvdata(pdev, iface);
  415. /*
  416. * Duplicate dai driver: depending on the slot masks configuration
  417. * We'll change the number of channel provided by DAI stream, so dpcm
  418. * channel merge can be done properly
  419. */
  420. dai_drv = devm_kcalloc(dev, ARRAY_SIZE(axg_tdm_iface_dai_drv),
  421. sizeof(*dai_drv), GFP_KERNEL);
  422. if (!dai_drv)
  423. return -ENOMEM;
  424. for (i = 0; i < ARRAY_SIZE(axg_tdm_iface_dai_drv); i++)
  425. memcpy(&dai_drv[i], &axg_tdm_iface_dai_drv[i],
  426. sizeof(*dai_drv));
  427. /* Bit clock provided on the pad */
  428. iface->sclk = devm_clk_get(dev, "sclk");
  429. if (IS_ERR(iface->sclk)) {
  430. ret = PTR_ERR(iface->sclk);
  431. if (ret != -EPROBE_DEFER)
  432. dev_err(dev, "failed to get sclk: %d\n", ret);
  433. return ret;
  434. }
  435. /* Sample clock provided on the pad */
  436. iface->lrclk = devm_clk_get(dev, "lrclk");
  437. if (IS_ERR(iface->lrclk)) {
  438. ret = PTR_ERR(iface->lrclk);
  439. if (ret != -EPROBE_DEFER)
  440. dev_err(dev, "failed to get lrclk: %d\n", ret);
  441. return ret;
  442. }
  443. /*
  444. * mclk maybe be missing when the cpu dai is in slave mode and
  445. * the codec does not require it to provide a master clock.
  446. * At this point, ignore the error if mclk is missing. We'll
  447. * throw an error if the cpu dai is master and mclk is missing
  448. */
  449. iface->mclk = devm_clk_get(dev, "mclk");
  450. if (IS_ERR(iface->mclk)) {
  451. ret = PTR_ERR(iface->mclk);
  452. if (ret == -ENOENT) {
  453. iface->mclk = NULL;
  454. } else {
  455. if (ret != -EPROBE_DEFER)
  456. dev_err(dev, "failed to get mclk: %d\n", ret);
  457. return ret;
  458. }
  459. }
  460. return devm_snd_soc_register_component(dev,
  461. &axg_tdm_iface_component_drv, dai_drv,
  462. ARRAY_SIZE(axg_tdm_iface_dai_drv));
  463. }
  464. static struct platform_driver axg_tdm_iface_pdrv = {
  465. .probe = axg_tdm_iface_probe,
  466. .driver = {
  467. .name = "axg-tdm-iface",
  468. .of_match_table = axg_tdm_iface_of_match,
  469. },
  470. };
  471. module_platform_driver(axg_tdm_iface_pdrv);
  472. MODULE_DESCRIPTION("Amlogic AXG TDM interface driver");
  473. MODULE_AUTHOR("Jerome Brunet <jbrunet@baylibre.com>");
  474. MODULE_LICENSE("GPL v2");