g12a-toacodec.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. // SPDX-License-Identifier: GPL-2.0
  2. //
  3. // Copyright (c) 2020 BayLibre, SAS.
  4. // Author: Jerome Brunet <jbrunet@baylibre.com>
  5. #include <linux/bitfield.h>
  6. #include <linux/clk.h>
  7. #include <linux/module.h>
  8. #include <sound/pcm_params.h>
  9. #include <linux/regmap.h>
  10. #include <linux/regulator/consumer.h>
  11. #include <linux/reset.h>
  12. #include <sound/soc.h>
  13. #include <sound/soc-dai.h>
  14. #include <dt-bindings/sound/meson-g12a-toacodec.h>
  15. #include "axg-tdm.h"
  16. #include "meson-codec-glue.h"
  17. #define G12A_TOACODEC_DRV_NAME "g12a-toacodec"
  18. #define TOACODEC_CTRL0 0x0
  19. #define CTRL0_ENABLE_SHIFT 31
  20. #define CTRL0_DAT_SEL_SHIFT 14
  21. #define CTRL0_DAT_SEL (0x3 << CTRL0_DAT_SEL_SHIFT)
  22. #define CTRL0_LANE_SEL 12
  23. #define CTRL0_LRCLK_SEL GENMASK(9, 8)
  24. #define CTRL0_BLK_CAP_INV BIT(7)
  25. #define CTRL0_BCLK_O_INV BIT(6)
  26. #define CTRL0_BCLK_SEL GENMASK(5, 4)
  27. #define CTRL0_MCLK_SEL GENMASK(2, 0)
  28. #define TOACODEC_OUT_CHMAX 2
  29. static const char * const g12a_toacodec_mux_texts[] = {
  30. "I2S A", "I2S B", "I2S C",
  31. };
  32. static int g12a_toacodec_mux_put_enum(struct snd_kcontrol *kcontrol,
  33. struct snd_ctl_elem_value *ucontrol)
  34. {
  35. struct snd_soc_component *component =
  36. snd_soc_dapm_kcontrol_component(kcontrol);
  37. struct snd_soc_dapm_context *dapm =
  38. snd_soc_dapm_kcontrol_dapm(kcontrol);
  39. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  40. unsigned int mux, changed;
  41. mux = snd_soc_enum_item_to_val(e, ucontrol->value.enumerated.item[0]);
  42. changed = snd_soc_component_test_bits(component, e->reg,
  43. CTRL0_DAT_SEL,
  44. FIELD_PREP(CTRL0_DAT_SEL, mux));
  45. if (!changed)
  46. return 0;
  47. /* Force disconnect of the mux while updating */
  48. snd_soc_dapm_mux_update_power(dapm, kcontrol, 0, NULL, NULL);
  49. snd_soc_component_update_bits(component, e->reg,
  50. CTRL0_DAT_SEL |
  51. CTRL0_LRCLK_SEL |
  52. CTRL0_BCLK_SEL,
  53. FIELD_PREP(CTRL0_DAT_SEL, mux) |
  54. FIELD_PREP(CTRL0_LRCLK_SEL, mux) |
  55. FIELD_PREP(CTRL0_BCLK_SEL, mux));
  56. /*
  57. * FIXME:
  58. * On this soc, the glue gets the MCLK directly from the clock
  59. * controller instead of going the through the TDM interface.
  60. *
  61. * Here we assume interface A uses clock A, etc ... While it is
  62. * true for now, it could be different. Instead the glue should
  63. * find out the clock used by the interface and select the same
  64. * source. For that, we will need regmap backed clock mux which
  65. * is a work in progress
  66. */
  67. snd_soc_component_update_bits(component, e->reg,
  68. CTRL0_MCLK_SEL,
  69. FIELD_PREP(CTRL0_MCLK_SEL, mux));
  70. snd_soc_dapm_mux_update_power(dapm, kcontrol, mux, e, NULL);
  71. return 0;
  72. }
  73. static SOC_ENUM_SINGLE_DECL(g12a_toacodec_mux_enum, TOACODEC_CTRL0,
  74. CTRL0_DAT_SEL_SHIFT,
  75. g12a_toacodec_mux_texts);
  76. static const struct snd_kcontrol_new g12a_toacodec_mux =
  77. SOC_DAPM_ENUM_EXT("Source", g12a_toacodec_mux_enum,
  78. snd_soc_dapm_get_enum_double,
  79. g12a_toacodec_mux_put_enum);
  80. static const struct snd_kcontrol_new g12a_toacodec_out_enable =
  81. SOC_DAPM_SINGLE_AUTODISABLE("Switch", TOACODEC_CTRL0,
  82. CTRL0_ENABLE_SHIFT, 1, 0);
  83. static const struct snd_soc_dapm_widget g12a_toacodec_widgets[] = {
  84. SND_SOC_DAPM_MUX("SRC", SND_SOC_NOPM, 0, 0,
  85. &g12a_toacodec_mux),
  86. SND_SOC_DAPM_SWITCH("OUT EN", SND_SOC_NOPM, 0, 0,
  87. &g12a_toacodec_out_enable),
  88. };
  89. static int g12a_toacodec_input_hw_params(struct snd_pcm_substream *substream,
  90. struct snd_pcm_hw_params *params,
  91. struct snd_soc_dai *dai)
  92. {
  93. struct meson_codec_glue_input *data;
  94. int ret;
  95. ret = meson_codec_glue_input_hw_params(substream, params, dai);
  96. if (ret)
  97. return ret;
  98. /* The glue will provide 1 lane out of the 4 to the output */
  99. data = meson_codec_glue_input_get_data(dai);
  100. data->params.channels_min = min_t(unsigned int, TOACODEC_OUT_CHMAX,
  101. data->params.channels_min);
  102. data->params.channels_max = min_t(unsigned int, TOACODEC_OUT_CHMAX,
  103. data->params.channels_max);
  104. return 0;
  105. }
  106. static const struct snd_soc_dai_ops g12a_toacodec_input_ops = {
  107. .hw_params = g12a_toacodec_input_hw_params,
  108. .set_fmt = meson_codec_glue_input_set_fmt,
  109. };
  110. static const struct snd_soc_dai_ops g12a_toacodec_output_ops = {
  111. .startup = meson_codec_glue_output_startup,
  112. };
  113. #define TOACODEC_STREAM(xname, xsuffix, xchmax) \
  114. { \
  115. .stream_name = xname " " xsuffix, \
  116. .channels_min = 1, \
  117. .channels_max = (xchmax), \
  118. .rate_min = 5512, \
  119. .rate_max = 192000, \
  120. .formats = AXG_TDM_FORMATS, \
  121. }
  122. #define TOACODEC_INPUT(xname, xid) { \
  123. .name = xname, \
  124. .id = (xid), \
  125. .playback = TOACODEC_STREAM(xname, "Playback", 8), \
  126. .ops = &g12a_toacodec_input_ops, \
  127. .probe = meson_codec_glue_input_dai_probe, \
  128. .remove = meson_codec_glue_input_dai_remove, \
  129. }
  130. #define TOACODEC_OUTPUT(xname, xid) { \
  131. .name = xname, \
  132. .id = (xid), \
  133. .capture = TOACODEC_STREAM(xname, "Capture", TOACODEC_OUT_CHMAX), \
  134. .ops = &g12a_toacodec_output_ops, \
  135. }
  136. static struct snd_soc_dai_driver g12a_toacodec_dai_drv[] = {
  137. TOACODEC_INPUT("IN A", TOACODEC_IN_A),
  138. TOACODEC_INPUT("IN B", TOACODEC_IN_B),
  139. TOACODEC_INPUT("IN C", TOACODEC_IN_C),
  140. TOACODEC_OUTPUT("OUT", TOACODEC_OUT),
  141. };
  142. static int g12a_toacodec_component_probe(struct snd_soc_component *c)
  143. {
  144. /* Initialize the static clock parameters */
  145. return snd_soc_component_write(c, TOACODEC_CTRL0,
  146. CTRL0_BLK_CAP_INV);
  147. }
  148. static const struct snd_soc_dapm_route g12a_toacodec_routes[] = {
  149. { "SRC", "I2S A", "IN A Playback" },
  150. { "SRC", "I2S B", "IN B Playback" },
  151. { "SRC", "I2S C", "IN C Playback" },
  152. { "OUT EN", "Switch", "SRC" },
  153. { "OUT Capture", NULL, "OUT EN" },
  154. };
  155. static const struct snd_kcontrol_new g12a_toacodec_controls[] = {
  156. SOC_SINGLE("Lane Select", TOACODEC_CTRL0, CTRL0_LANE_SEL, 3, 0),
  157. };
  158. static const struct snd_soc_component_driver g12a_toacodec_component_drv = {
  159. .probe = g12a_toacodec_component_probe,
  160. .controls = g12a_toacodec_controls,
  161. .num_controls = ARRAY_SIZE(g12a_toacodec_controls),
  162. .dapm_widgets = g12a_toacodec_widgets,
  163. .num_dapm_widgets = ARRAY_SIZE(g12a_toacodec_widgets),
  164. .dapm_routes = g12a_toacodec_routes,
  165. .num_dapm_routes = ARRAY_SIZE(g12a_toacodec_routes),
  166. .endianness = 1,
  167. .non_legacy_dai_naming = 1,
  168. };
  169. static const struct regmap_config g12a_toacodec_regmap_cfg = {
  170. .reg_bits = 32,
  171. .val_bits = 32,
  172. .reg_stride = 4,
  173. };
  174. static const struct of_device_id g12a_toacodec_of_match[] = {
  175. { .compatible = "amlogic,g12a-toacodec", },
  176. {}
  177. };
  178. MODULE_DEVICE_TABLE(of, g12a_toacodec_of_match);
  179. static int g12a_toacodec_probe(struct platform_device *pdev)
  180. {
  181. struct device *dev = &pdev->dev;
  182. void __iomem *regs;
  183. struct regmap *map;
  184. int ret;
  185. ret = device_reset(dev);
  186. if (ret)
  187. return ret;
  188. regs = devm_platform_ioremap_resource(pdev, 0);
  189. if (IS_ERR(regs))
  190. return PTR_ERR(regs);
  191. map = devm_regmap_init_mmio(dev, regs, &g12a_toacodec_regmap_cfg);
  192. if (IS_ERR(map)) {
  193. dev_err(dev, "failed to init regmap: %ld\n",
  194. PTR_ERR(map));
  195. return PTR_ERR(map);
  196. }
  197. return devm_snd_soc_register_component(dev,
  198. &g12a_toacodec_component_drv, g12a_toacodec_dai_drv,
  199. ARRAY_SIZE(g12a_toacodec_dai_drv));
  200. }
  201. static struct platform_driver g12a_toacodec_pdrv = {
  202. .driver = {
  203. .name = G12A_TOACODEC_DRV_NAME,
  204. .of_match_table = g12a_toacodec_of_match,
  205. },
  206. .probe = g12a_toacodec_probe,
  207. };
  208. module_platform_driver(g12a_toacodec_pdrv);
  209. MODULE_AUTHOR("Jerome Brunet <jbrunet@baylibre.com>");
  210. MODULE_DESCRIPTION("Amlogic G12a To Internal DAC Codec Driver");
  211. MODULE_LICENSE("GPL v2");