es7241.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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/gpio/consumer.h>
  6. #include <linux/of_platform.h>
  7. #include <linux/module.h>
  8. #include <sound/soc.h>
  9. struct es7241_clock_mode {
  10. unsigned int rate_min;
  11. unsigned int rate_max;
  12. unsigned int *slv_mfs;
  13. unsigned int slv_mfs_num;
  14. unsigned int mst_mfs;
  15. unsigned int mst_m0:1;
  16. unsigned int mst_m1:1;
  17. };
  18. struct es7241_chip {
  19. const struct es7241_clock_mode *modes;
  20. unsigned int mode_num;
  21. };
  22. struct es7241_data {
  23. struct gpio_desc *reset;
  24. struct gpio_desc *m0;
  25. struct gpio_desc *m1;
  26. unsigned int fmt;
  27. unsigned int mclk;
  28. bool is_slave;
  29. const struct es7241_chip *chip;
  30. };
  31. static void es7241_set_mode(struct es7241_data *priv, int m0, int m1)
  32. {
  33. /* put the device in reset */
  34. gpiod_set_value_cansleep(priv->reset, 0);
  35. /* set the mode */
  36. gpiod_set_value_cansleep(priv->m0, m0);
  37. gpiod_set_value_cansleep(priv->m1, m1);
  38. /* take the device out of reset - datasheet does not specify a delay */
  39. gpiod_set_value_cansleep(priv->reset, 1);
  40. }
  41. static int es7241_set_slave_mode(struct es7241_data *priv,
  42. const struct es7241_clock_mode *mode,
  43. unsigned int mfs)
  44. {
  45. int j;
  46. if (!mfs)
  47. goto out_ok;
  48. for (j = 0; j < mode->slv_mfs_num; j++) {
  49. if (mode->slv_mfs[j] == mfs)
  50. goto out_ok;
  51. }
  52. return -EINVAL;
  53. out_ok:
  54. es7241_set_mode(priv, 1, 1);
  55. return 0;
  56. }
  57. static int es7241_set_master_mode(struct es7241_data *priv,
  58. const struct es7241_clock_mode *mode,
  59. unsigned int mfs)
  60. {
  61. /*
  62. * We can't really set clock ratio, if the mclk/lrclk is different
  63. * from what we provide, then error out
  64. */
  65. if (mfs && mfs != mode->mst_mfs)
  66. return -EINVAL;
  67. es7241_set_mode(priv, mode->mst_m0, mode->mst_m1);
  68. return 0;
  69. }
  70. static int es7241_hw_params(struct snd_pcm_substream *substream,
  71. struct snd_pcm_hw_params *params,
  72. struct snd_soc_dai *dai)
  73. {
  74. struct es7241_data *priv = snd_soc_dai_get_drvdata(dai);
  75. unsigned int rate = params_rate(params);
  76. unsigned int mfs = priv->mclk / rate;
  77. int i;
  78. for (i = 0; i < priv->chip->mode_num; i++) {
  79. const struct es7241_clock_mode *mode = &priv->chip->modes[i];
  80. if (rate < mode->rate_min || rate >= mode->rate_max)
  81. continue;
  82. if (priv->is_slave)
  83. return es7241_set_slave_mode(priv, mode, mfs);
  84. else
  85. return es7241_set_master_mode(priv, mode, mfs);
  86. }
  87. /* should not happen */
  88. dev_err(dai->dev, "unsupported rate: %u\n", rate);
  89. return -EINVAL;
  90. }
  91. static int es7241_set_sysclk(struct snd_soc_dai *dai, int clk_id,
  92. unsigned int freq, int dir)
  93. {
  94. struct es7241_data *priv = snd_soc_dai_get_drvdata(dai);
  95. if (dir == SND_SOC_CLOCK_IN && clk_id == 0) {
  96. priv->mclk = freq;
  97. return 0;
  98. }
  99. return -ENOTSUPP;
  100. }
  101. static int es7241_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
  102. {
  103. struct es7241_data *priv = snd_soc_dai_get_drvdata(dai);
  104. if ((fmt & SND_SOC_DAIFMT_INV_MASK) != SND_SOC_DAIFMT_NB_NF) {
  105. dev_err(dai->dev, "Unsupported dai clock inversion\n");
  106. return -EINVAL;
  107. }
  108. if ((fmt & SND_SOC_DAIFMT_FORMAT_MASK) != priv->fmt) {
  109. dev_err(dai->dev, "Invalid dai format\n");
  110. return -EINVAL;
  111. }
  112. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  113. case SND_SOC_DAIFMT_CBS_CFS:
  114. priv->is_slave = true;
  115. break;
  116. case SND_SOC_DAIFMT_CBM_CFM:
  117. priv->is_slave = false;
  118. break;
  119. default:
  120. dev_err(dai->dev, "Unsupported clock configuration\n");
  121. return -EINVAL;
  122. }
  123. return 0;
  124. }
  125. static const struct snd_soc_dai_ops es7241_dai_ops = {
  126. .set_fmt = es7241_set_fmt,
  127. .hw_params = es7241_hw_params,
  128. .set_sysclk = es7241_set_sysclk,
  129. };
  130. static struct snd_soc_dai_driver es7241_dai = {
  131. .name = "es7241-hifi",
  132. .capture = {
  133. .stream_name = "Capture",
  134. .channels_min = 2,
  135. .channels_max = 2,
  136. .rates = SNDRV_PCM_RATE_8000_192000,
  137. .formats = (SNDRV_PCM_FMTBIT_S16_LE |
  138. SNDRV_PCM_FMTBIT_S24_3LE |
  139. SNDRV_PCM_FMTBIT_S24_LE),
  140. },
  141. .ops = &es7241_dai_ops,
  142. };
  143. static const struct es7241_clock_mode es7241_modes[] = {
  144. {
  145. /* Single speed mode */
  146. .rate_min = 8000,
  147. .rate_max = 50000,
  148. .slv_mfs = (unsigned int[]) { 256, 384, 512, 768, 1024 },
  149. .slv_mfs_num = 5,
  150. .mst_mfs = 256,
  151. .mst_m0 = 0,
  152. .mst_m1 = 0,
  153. }, {
  154. /* Double speed mode */
  155. .rate_min = 50000,
  156. .rate_max = 100000,
  157. .slv_mfs = (unsigned int[]) { 128, 192 },
  158. .slv_mfs_num = 2,
  159. .mst_mfs = 128,
  160. .mst_m0 = 1,
  161. .mst_m1 = 0,
  162. }, {
  163. /* Quad speed mode */
  164. .rate_min = 100000,
  165. .rate_max = 200000,
  166. .slv_mfs = (unsigned int[]) { 64 },
  167. .slv_mfs_num = 1,
  168. .mst_mfs = 64,
  169. .mst_m0 = 0,
  170. .mst_m1 = 1,
  171. },
  172. };
  173. static const struct es7241_chip es7241_chip = {
  174. .modes = es7241_modes,
  175. .mode_num = ARRAY_SIZE(es7241_modes),
  176. };
  177. static const struct snd_soc_dapm_widget es7241_dapm_widgets[] = {
  178. SND_SOC_DAPM_INPUT("AINL"),
  179. SND_SOC_DAPM_INPUT("AINR"),
  180. SND_SOC_DAPM_DAC("ADC", "Capture", SND_SOC_NOPM, 0, 0),
  181. SND_SOC_DAPM_REGULATOR_SUPPLY("VDDP", 0, 0),
  182. SND_SOC_DAPM_REGULATOR_SUPPLY("VDDD", 0, 0),
  183. SND_SOC_DAPM_REGULATOR_SUPPLY("VDDA", 0, 0),
  184. };
  185. static const struct snd_soc_dapm_route es7241_dapm_routes[] = {
  186. { "ADC", NULL, "AINL", },
  187. { "ADC", NULL, "AINR", },
  188. { "ADC", NULL, "VDDA", },
  189. { "Capture", NULL, "VDDP", },
  190. { "Capture", NULL, "VDDD", },
  191. };
  192. static const struct snd_soc_component_driver es7241_component_driver = {
  193. .dapm_widgets = es7241_dapm_widgets,
  194. .num_dapm_widgets = ARRAY_SIZE(es7241_dapm_widgets),
  195. .dapm_routes = es7241_dapm_routes,
  196. .num_dapm_routes = ARRAY_SIZE(es7241_dapm_routes),
  197. .idle_bias_on = 1,
  198. .endianness = 1,
  199. .non_legacy_dai_naming = 1,
  200. };
  201. static void es7241_parse_fmt(struct device *dev, struct es7241_data *priv)
  202. {
  203. bool is_leftj;
  204. /*
  205. * The format is given by a pull resistor on the SDOUT pin:
  206. * pull-up for i2s, pull-down for left justified.
  207. */
  208. is_leftj = of_property_read_bool(dev->of_node,
  209. "everest,sdout-pull-down");
  210. if (is_leftj)
  211. priv->fmt = SND_SOC_DAIFMT_LEFT_J;
  212. else
  213. priv->fmt = SND_SOC_DAIFMT_I2S;
  214. }
  215. static int es7241_probe(struct platform_device *pdev)
  216. {
  217. struct device *dev = &pdev->dev;
  218. struct es7241_data *priv;
  219. int err;
  220. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  221. if (!priv)
  222. return -ENOMEM;
  223. platform_set_drvdata(pdev, priv);
  224. priv->chip = of_device_get_match_data(dev);
  225. if (!priv->chip) {
  226. dev_err(dev, "failed to match device\n");
  227. return -ENODEV;
  228. }
  229. es7241_parse_fmt(dev, priv);
  230. priv->reset = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_LOW);
  231. if (IS_ERR(priv->reset)) {
  232. err = PTR_ERR(priv->reset);
  233. if (err != -EPROBE_DEFER)
  234. dev_err(dev, "Failed to get 'reset' gpio: %d", err);
  235. return err;
  236. }
  237. priv->m0 = devm_gpiod_get_optional(dev, "m0", GPIOD_OUT_LOW);
  238. if (IS_ERR(priv->m0)) {
  239. err = PTR_ERR(priv->m0);
  240. if (err != -EPROBE_DEFER)
  241. dev_err(dev, "Failed to get 'm0' gpio: %d", err);
  242. return err;
  243. }
  244. priv->m1 = devm_gpiod_get_optional(dev, "m1", GPIOD_OUT_LOW);
  245. if (IS_ERR(priv->m1)) {
  246. err = PTR_ERR(priv->m1);
  247. if (err != -EPROBE_DEFER)
  248. dev_err(dev, "Failed to get 'm1' gpio: %d", err);
  249. return err;
  250. }
  251. return devm_snd_soc_register_component(&pdev->dev,
  252. &es7241_component_driver,
  253. &es7241_dai, 1);
  254. }
  255. #ifdef CONFIG_OF
  256. static const struct of_device_id es7241_ids[] = {
  257. { .compatible = "everest,es7241", .data = &es7241_chip },
  258. { }
  259. };
  260. MODULE_DEVICE_TABLE(of, es7241_ids);
  261. #endif
  262. static struct platform_driver es7241_driver = {
  263. .driver = {
  264. .name = "es7241",
  265. .of_match_table = of_match_ptr(es7241_ids),
  266. },
  267. .probe = es7241_probe,
  268. };
  269. module_platform_driver(es7241_driver);
  270. MODULE_DESCRIPTION("ASoC ES7241 audio codec driver");
  271. MODULE_AUTHOR("Jerome Brunet <jbrunet@baylibre.com>");
  272. MODULE_LICENSE("GPL");