axg-spdifin.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. // SPDX-License-Identifier: (GPL-2.0 OR MIT)
  2. //
  3. // Copyright (c) 2018 BayLibre, SAS.
  4. // Author: Jerome Brunet <[email protected]>
  5. #include <linux/bitfield.h>
  6. #include <linux/clk.h>
  7. #include <linux/module.h>
  8. #include <linux/of_platform.h>
  9. #include <linux/regmap.h>
  10. #include <sound/soc.h>
  11. #include <sound/soc-dai.h>
  12. #include <sound/pcm_params.h>
  13. #define SPDIFIN_CTRL0 0x00
  14. #define SPDIFIN_CTRL0_EN BIT(31)
  15. #define SPDIFIN_CTRL0_RST_OUT BIT(29)
  16. #define SPDIFIN_CTRL0_RST_IN BIT(28)
  17. #define SPDIFIN_CTRL0_WIDTH_SEL BIT(24)
  18. #define SPDIFIN_CTRL0_STATUS_CH_SHIFT 11
  19. #define SPDIFIN_CTRL0_STATUS_SEL GENMASK(10, 8)
  20. #define SPDIFIN_CTRL0_SRC_SEL GENMASK(5, 4)
  21. #define SPDIFIN_CTRL0_CHK_VALID BIT(3)
  22. #define SPDIFIN_CTRL1 0x04
  23. #define SPDIFIN_CTRL1_BASE_TIMER GENMASK(19, 0)
  24. #define SPDIFIN_CTRL1_IRQ_MASK GENMASK(27, 20)
  25. #define SPDIFIN_CTRL2 0x08
  26. #define SPDIFIN_THRES_PER_REG 3
  27. #define SPDIFIN_THRES_WIDTH 10
  28. #define SPDIFIN_CTRL3 0x0c
  29. #define SPDIFIN_CTRL4 0x10
  30. #define SPDIFIN_TIMER_PER_REG 4
  31. #define SPDIFIN_TIMER_WIDTH 8
  32. #define SPDIFIN_CTRL5 0x14
  33. #define SPDIFIN_CTRL6 0x18
  34. #define SPDIFIN_STAT0 0x1c
  35. #define SPDIFIN_STAT0_MODE GENMASK(30, 28)
  36. #define SPDIFIN_STAT0_MAXW GENMASK(17, 8)
  37. #define SPDIFIN_STAT0_IRQ GENMASK(7, 0)
  38. #define SPDIFIN_IRQ_MODE_CHANGED BIT(2)
  39. #define SPDIFIN_STAT1 0x20
  40. #define SPDIFIN_STAT2 0x24
  41. #define SPDIFIN_MUTE_VAL 0x28
  42. #define SPDIFIN_MODE_NUM 7
  43. struct axg_spdifin_cfg {
  44. const unsigned int *mode_rates;
  45. unsigned int ref_rate;
  46. };
  47. struct axg_spdifin {
  48. const struct axg_spdifin_cfg *conf;
  49. struct regmap *map;
  50. struct clk *refclk;
  51. struct clk *pclk;
  52. };
  53. /*
  54. * TODO:
  55. * It would have been nice to check the actual rate against the sample rate
  56. * requested in hw_params(). Unfortunately, I was not able to make the mode
  57. * detection and IRQ work reliably:
  58. *
  59. * 1. IRQs are generated on mode change only, so there is no notification
  60. * on transition between no signal and mode 0 (32kHz).
  61. * 2. Mode detection very often has glitches, and may detects the
  62. * lowest or the highest mode before zeroing in on the actual mode.
  63. *
  64. * This makes calling snd_pcm_stop() difficult to get right. Even notifying
  65. * the kcontrol would be very unreliable at this point.
  66. * Let's keep things simple until the magic spell that makes this work is
  67. * found.
  68. */
  69. static unsigned int axg_spdifin_get_rate(struct axg_spdifin *priv)
  70. {
  71. unsigned int stat, mode, rate = 0;
  72. regmap_read(priv->map, SPDIFIN_STAT0, &stat);
  73. mode = FIELD_GET(SPDIFIN_STAT0_MODE, stat);
  74. /*
  75. * If max width is zero, we are not capturing anything.
  76. * Also Sometimes, when the capture is on but there is no data,
  77. * mode is SPDIFIN_MODE_NUM, but not always ...
  78. */
  79. if (FIELD_GET(SPDIFIN_STAT0_MAXW, stat) &&
  80. mode < SPDIFIN_MODE_NUM)
  81. rate = priv->conf->mode_rates[mode];
  82. return rate;
  83. }
  84. static int axg_spdifin_prepare(struct snd_pcm_substream *substream,
  85. struct snd_soc_dai *dai)
  86. {
  87. struct axg_spdifin *priv = snd_soc_dai_get_drvdata(dai);
  88. /* Apply both reset */
  89. regmap_update_bits(priv->map, SPDIFIN_CTRL0,
  90. SPDIFIN_CTRL0_RST_OUT |
  91. SPDIFIN_CTRL0_RST_IN,
  92. 0);
  93. /* Clear out reset before in reset */
  94. regmap_update_bits(priv->map, SPDIFIN_CTRL0,
  95. SPDIFIN_CTRL0_RST_OUT, SPDIFIN_CTRL0_RST_OUT);
  96. regmap_update_bits(priv->map, SPDIFIN_CTRL0,
  97. SPDIFIN_CTRL0_RST_IN, SPDIFIN_CTRL0_RST_IN);
  98. return 0;
  99. }
  100. static void axg_spdifin_write_mode_param(struct regmap *map, int mode,
  101. unsigned int val,
  102. unsigned int num_per_reg,
  103. unsigned int base_reg,
  104. unsigned int width)
  105. {
  106. uint64_t offset = mode;
  107. unsigned int reg, shift, rem;
  108. rem = do_div(offset, num_per_reg);
  109. reg = offset * regmap_get_reg_stride(map) + base_reg;
  110. shift = width * (num_per_reg - 1 - rem);
  111. regmap_update_bits(map, reg, GENMASK(width - 1, 0) << shift,
  112. val << shift);
  113. }
  114. static void axg_spdifin_write_timer(struct regmap *map, int mode,
  115. unsigned int val)
  116. {
  117. axg_spdifin_write_mode_param(map, mode, val, SPDIFIN_TIMER_PER_REG,
  118. SPDIFIN_CTRL4, SPDIFIN_TIMER_WIDTH);
  119. }
  120. static void axg_spdifin_write_threshold(struct regmap *map, int mode,
  121. unsigned int val)
  122. {
  123. axg_spdifin_write_mode_param(map, mode, val, SPDIFIN_THRES_PER_REG,
  124. SPDIFIN_CTRL2, SPDIFIN_THRES_WIDTH);
  125. }
  126. static unsigned int axg_spdifin_mode_timer(struct axg_spdifin *priv,
  127. int mode,
  128. unsigned int rate)
  129. {
  130. /*
  131. * Number of period of the reference clock during a period of the
  132. * input signal reference clock
  133. */
  134. return rate / (128 * priv->conf->mode_rates[mode]);
  135. }
  136. static int axg_spdifin_sample_mode_config(struct snd_soc_dai *dai,
  137. struct axg_spdifin *priv)
  138. {
  139. unsigned int rate, t_next;
  140. int ret, i = SPDIFIN_MODE_NUM - 1;
  141. /* Set spdif input reference clock */
  142. ret = clk_set_rate(priv->refclk, priv->conf->ref_rate);
  143. if (ret) {
  144. dev_err(dai->dev, "reference clock rate set failed\n");
  145. return ret;
  146. }
  147. /*
  148. * The rate actually set might be slightly different, get
  149. * the actual rate for the following mode calculation
  150. */
  151. rate = clk_get_rate(priv->refclk);
  152. /* HW will update mode every 1ms */
  153. regmap_update_bits(priv->map, SPDIFIN_CTRL1,
  154. SPDIFIN_CTRL1_BASE_TIMER,
  155. FIELD_PREP(SPDIFIN_CTRL1_BASE_TIMER, rate / 1000));
  156. /* Threshold based on the minimum width between two edges */
  157. regmap_update_bits(priv->map, SPDIFIN_CTRL0,
  158. SPDIFIN_CTRL0_WIDTH_SEL, SPDIFIN_CTRL0_WIDTH_SEL);
  159. /* Calculate the last timer which has no threshold */
  160. t_next = axg_spdifin_mode_timer(priv, i, rate);
  161. axg_spdifin_write_timer(priv->map, i, t_next);
  162. do {
  163. unsigned int t;
  164. i -= 1;
  165. /* Calculate the timer */
  166. t = axg_spdifin_mode_timer(priv, i, rate);
  167. /* Set the timer value */
  168. axg_spdifin_write_timer(priv->map, i, t);
  169. /* Set the threshold value */
  170. axg_spdifin_write_threshold(priv->map, i, t + t_next);
  171. /* Save the current timer for the next threshold calculation */
  172. t_next = t;
  173. } while (i > 0);
  174. return 0;
  175. }
  176. static int axg_spdifin_dai_probe(struct snd_soc_dai *dai)
  177. {
  178. struct axg_spdifin *priv = snd_soc_dai_get_drvdata(dai);
  179. int ret;
  180. ret = clk_prepare_enable(priv->pclk);
  181. if (ret) {
  182. dev_err(dai->dev, "failed to enable pclk\n");
  183. return ret;
  184. }
  185. ret = axg_spdifin_sample_mode_config(dai, priv);
  186. if (ret) {
  187. dev_err(dai->dev, "mode configuration failed\n");
  188. goto pclk_err;
  189. }
  190. ret = clk_prepare_enable(priv->refclk);
  191. if (ret) {
  192. dev_err(dai->dev,
  193. "failed to enable spdifin reference clock\n");
  194. goto pclk_err;
  195. }
  196. regmap_update_bits(priv->map, SPDIFIN_CTRL0, SPDIFIN_CTRL0_EN,
  197. SPDIFIN_CTRL0_EN);
  198. return 0;
  199. pclk_err:
  200. clk_disable_unprepare(priv->pclk);
  201. return ret;
  202. }
  203. static int axg_spdifin_dai_remove(struct snd_soc_dai *dai)
  204. {
  205. struct axg_spdifin *priv = snd_soc_dai_get_drvdata(dai);
  206. regmap_update_bits(priv->map, SPDIFIN_CTRL0, SPDIFIN_CTRL0_EN, 0);
  207. clk_disable_unprepare(priv->refclk);
  208. clk_disable_unprepare(priv->pclk);
  209. return 0;
  210. }
  211. static const struct snd_soc_dai_ops axg_spdifin_ops = {
  212. .prepare = axg_spdifin_prepare,
  213. };
  214. static int axg_spdifin_iec958_info(struct snd_kcontrol *kcontrol,
  215. struct snd_ctl_elem_info *uinfo)
  216. {
  217. uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
  218. uinfo->count = 1;
  219. return 0;
  220. }
  221. static int axg_spdifin_get_status_mask(struct snd_kcontrol *kcontrol,
  222. struct snd_ctl_elem_value *ucontrol)
  223. {
  224. int i;
  225. for (i = 0; i < 24; i++)
  226. ucontrol->value.iec958.status[i] = 0xff;
  227. return 0;
  228. }
  229. static int axg_spdifin_get_status(struct snd_kcontrol *kcontrol,
  230. struct snd_ctl_elem_value *ucontrol)
  231. {
  232. struct snd_soc_component *c = snd_kcontrol_chip(kcontrol);
  233. struct axg_spdifin *priv = snd_soc_component_get_drvdata(c);
  234. int i, j;
  235. for (i = 0; i < 6; i++) {
  236. unsigned int val;
  237. regmap_update_bits(priv->map, SPDIFIN_CTRL0,
  238. SPDIFIN_CTRL0_STATUS_SEL,
  239. FIELD_PREP(SPDIFIN_CTRL0_STATUS_SEL, i));
  240. regmap_read(priv->map, SPDIFIN_STAT1, &val);
  241. for (j = 0; j < 4; j++) {
  242. unsigned int offset = i * 4 + j;
  243. ucontrol->value.iec958.status[offset] =
  244. (val >> (j * 8)) & 0xff;
  245. }
  246. }
  247. return 0;
  248. }
  249. #define AXG_SPDIFIN_IEC958_MASK \
  250. { \
  251. .access = SNDRV_CTL_ELEM_ACCESS_READ, \
  252. .iface = SNDRV_CTL_ELEM_IFACE_PCM, \
  253. .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, MASK), \
  254. .info = axg_spdifin_iec958_info, \
  255. .get = axg_spdifin_get_status_mask, \
  256. }
  257. #define AXG_SPDIFIN_IEC958_STATUS \
  258. { \
  259. .access = (SNDRV_CTL_ELEM_ACCESS_READ | \
  260. SNDRV_CTL_ELEM_ACCESS_VOLATILE), \
  261. .iface = SNDRV_CTL_ELEM_IFACE_PCM, \
  262. .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, NONE), \
  263. .info = axg_spdifin_iec958_info, \
  264. .get = axg_spdifin_get_status, \
  265. }
  266. static const char * const spdifin_chsts_src_texts[] = {
  267. "A", "B",
  268. };
  269. static SOC_ENUM_SINGLE_DECL(axg_spdifin_chsts_src_enum, SPDIFIN_CTRL0,
  270. SPDIFIN_CTRL0_STATUS_CH_SHIFT,
  271. spdifin_chsts_src_texts);
  272. static int axg_spdifin_rate_lock_info(struct snd_kcontrol *kcontrol,
  273. struct snd_ctl_elem_info *uinfo)
  274. {
  275. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  276. uinfo->count = 1;
  277. uinfo->value.integer.min = 0;
  278. uinfo->value.integer.max = 192000;
  279. return 0;
  280. }
  281. static int axg_spdifin_rate_lock_get(struct snd_kcontrol *kcontrol,
  282. struct snd_ctl_elem_value *ucontrol)
  283. {
  284. struct snd_soc_component *c = snd_kcontrol_chip(kcontrol);
  285. struct axg_spdifin *priv = snd_soc_component_get_drvdata(c);
  286. ucontrol->value.integer.value[0] = axg_spdifin_get_rate(priv);
  287. return 0;
  288. }
  289. #define AXG_SPDIFIN_LOCK_RATE(xname) \
  290. { \
  291. .iface = SNDRV_CTL_ELEM_IFACE_PCM, \
  292. .access = (SNDRV_CTL_ELEM_ACCESS_READ | \
  293. SNDRV_CTL_ELEM_ACCESS_VOLATILE), \
  294. .get = axg_spdifin_rate_lock_get, \
  295. .info = axg_spdifin_rate_lock_info, \
  296. .name = xname, \
  297. }
  298. static const struct snd_kcontrol_new axg_spdifin_controls[] = {
  299. AXG_SPDIFIN_LOCK_RATE("Capture Rate Lock"),
  300. SOC_DOUBLE("Capture Switch", SPDIFIN_CTRL0, 7, 6, 1, 1),
  301. SOC_ENUM(SNDRV_CTL_NAME_IEC958("", CAPTURE, NONE) "Src",
  302. axg_spdifin_chsts_src_enum),
  303. AXG_SPDIFIN_IEC958_MASK,
  304. AXG_SPDIFIN_IEC958_STATUS,
  305. };
  306. static const struct snd_soc_component_driver axg_spdifin_component_drv = {
  307. .controls = axg_spdifin_controls,
  308. .num_controls = ARRAY_SIZE(axg_spdifin_controls),
  309. .legacy_dai_naming = 1,
  310. };
  311. static const struct regmap_config axg_spdifin_regmap_cfg = {
  312. .reg_bits = 32,
  313. .val_bits = 32,
  314. .reg_stride = 4,
  315. .max_register = SPDIFIN_MUTE_VAL,
  316. };
  317. static const unsigned int axg_spdifin_mode_rates[SPDIFIN_MODE_NUM] = {
  318. 32000, 44100, 48000, 88200, 96000, 176400, 192000,
  319. };
  320. static const struct axg_spdifin_cfg axg_cfg = {
  321. .mode_rates = axg_spdifin_mode_rates,
  322. .ref_rate = 333333333,
  323. };
  324. static const struct of_device_id axg_spdifin_of_match[] = {
  325. {
  326. .compatible = "amlogic,axg-spdifin",
  327. .data = &axg_cfg,
  328. }, {}
  329. };
  330. MODULE_DEVICE_TABLE(of, axg_spdifin_of_match);
  331. static struct snd_soc_dai_driver *
  332. axg_spdifin_get_dai_drv(struct device *dev, struct axg_spdifin *priv)
  333. {
  334. struct snd_soc_dai_driver *drv;
  335. int i;
  336. drv = devm_kzalloc(dev, sizeof(*drv), GFP_KERNEL);
  337. if (!drv)
  338. return ERR_PTR(-ENOMEM);
  339. drv->name = "SPDIF Input";
  340. drv->ops = &axg_spdifin_ops;
  341. drv->probe = axg_spdifin_dai_probe;
  342. drv->remove = axg_spdifin_dai_remove;
  343. drv->capture.stream_name = "Capture";
  344. drv->capture.channels_min = 1;
  345. drv->capture.channels_max = 2;
  346. drv->capture.formats = SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
  347. for (i = 0; i < SPDIFIN_MODE_NUM; i++) {
  348. unsigned int rb =
  349. snd_pcm_rate_to_rate_bit(priv->conf->mode_rates[i]);
  350. if (rb == SNDRV_PCM_RATE_KNOT)
  351. return ERR_PTR(-EINVAL);
  352. drv->capture.rates |= rb;
  353. }
  354. return drv;
  355. }
  356. static int axg_spdifin_probe(struct platform_device *pdev)
  357. {
  358. struct device *dev = &pdev->dev;
  359. struct axg_spdifin *priv;
  360. struct snd_soc_dai_driver *dai_drv;
  361. void __iomem *regs;
  362. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  363. if (!priv)
  364. return -ENOMEM;
  365. platform_set_drvdata(pdev, priv);
  366. priv->conf = of_device_get_match_data(dev);
  367. if (!priv->conf) {
  368. dev_err(dev, "failed to match device\n");
  369. return -ENODEV;
  370. }
  371. regs = devm_platform_ioremap_resource(pdev, 0);
  372. if (IS_ERR(regs))
  373. return PTR_ERR(regs);
  374. priv->map = devm_regmap_init_mmio(dev, regs, &axg_spdifin_regmap_cfg);
  375. if (IS_ERR(priv->map)) {
  376. dev_err(dev, "failed to init regmap: %ld\n",
  377. PTR_ERR(priv->map));
  378. return PTR_ERR(priv->map);
  379. }
  380. priv->pclk = devm_clk_get(dev, "pclk");
  381. if (IS_ERR(priv->pclk))
  382. return dev_err_probe(dev, PTR_ERR(priv->pclk), "failed to get pclk\n");
  383. priv->refclk = devm_clk_get(dev, "refclk");
  384. if (IS_ERR(priv->refclk))
  385. return dev_err_probe(dev, PTR_ERR(priv->refclk), "failed to get mclk\n");
  386. dai_drv = axg_spdifin_get_dai_drv(dev, priv);
  387. if (IS_ERR(dai_drv)) {
  388. dev_err(dev, "failed to get dai driver: %ld\n",
  389. PTR_ERR(dai_drv));
  390. return PTR_ERR(dai_drv);
  391. }
  392. return devm_snd_soc_register_component(dev, &axg_spdifin_component_drv,
  393. dai_drv, 1);
  394. }
  395. static struct platform_driver axg_spdifin_pdrv = {
  396. .probe = axg_spdifin_probe,
  397. .driver = {
  398. .name = "axg-spdifin",
  399. .of_match_table = axg_spdifin_of_match,
  400. },
  401. };
  402. module_platform_driver(axg_spdifin_pdrv);
  403. MODULE_DESCRIPTION("Amlogic AXG SPDIF Input driver");
  404. MODULE_AUTHOR("Jerome Brunet <[email protected]>");
  405. MODULE_LICENSE("GPL v2");