1 // SPDX-License-Identifier: (GPL-2.0 OR MIT)
3 // Copyright (c) 2018 BayLibre, SAS.
6 #include <linux/gpio/consumer.h>
7 #include <linux/of_platform.h>
8 #include <linux/module.h>
11 struct es7241_clock_mode {
12 unsigned int rate_min;
13 unsigned int rate_max;
14 unsigned int *slv_mfs;
15 unsigned int slv_mfs_num;
17 unsigned int mst_m0:1;
18 unsigned int mst_m1:1;
22 const struct es7241_clock_mode *modes;
23 unsigned int mode_num;
27 struct gpio_desc *reset;
33 const struct es7241_chip *chip;
36 static void es7241_set_mode(struct es7241_data *priv, int m0, int m1)
38 /* put the device in reset */
39 gpiod_set_value_cansleep(priv->reset, 0);
42 gpiod_set_value_cansleep(priv->m0, m0);
43 gpiod_set_value_cansleep(priv->m1, m1);
45 /* take the device out of reset - datasheet does not specify a delay */
46 gpiod_set_value_cansleep(priv->reset, 1);
49 static int es7241_set_consumer_mode(struct es7241_data *priv,
50 const struct es7241_clock_mode *mode,
58 for (j = 0; j < mode->slv_mfs_num; j++) {
59 if (mode->slv_mfs[j] == mfs)
66 es7241_set_mode(priv, 1, 1);
70 static int es7241_set_provider_mode(struct es7241_data *priv,
71 const struct es7241_clock_mode *mode,
75 * We can't really set clock ratio, if the mclk/lrclk is different
76 * from what we provide, then error out
78 if (mfs && mfs != mode->mst_mfs)
81 es7241_set_mode(priv, mode->mst_m0, mode->mst_m1);
86 static int es7241_hw_params(struct snd_pcm_substream *substream,
87 struct snd_pcm_hw_params *params,
88 struct snd_soc_dai *dai)
90 struct es7241_data *priv = snd_soc_dai_get_drvdata(dai);
91 unsigned int rate = params_rate(params);
92 unsigned int mfs = priv->mclk / rate;
95 for (i = 0; i < priv->chip->mode_num; i++) {
96 const struct es7241_clock_mode *mode = &priv->chip->modes[i];
98 if (rate < mode->rate_min || rate >= mode->rate_max)
101 if (priv->is_consumer)
102 return es7241_set_consumer_mode(priv, mode, mfs);
104 return es7241_set_provider_mode(priv, mode, mfs);
107 /* should not happen */
108 dev_err(dai->dev, "unsupported rate: %u\n", rate);
112 static int es7241_set_sysclk(struct snd_soc_dai *dai, int clk_id,
113 unsigned int freq, int dir)
115 struct es7241_data *priv = snd_soc_dai_get_drvdata(dai);
117 if (dir == SND_SOC_CLOCK_IN && clk_id == 0) {
125 static int es7241_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
127 struct es7241_data *priv = snd_soc_dai_get_drvdata(dai);
129 if ((fmt & SND_SOC_DAIFMT_INV_MASK) != SND_SOC_DAIFMT_NB_NF) {
130 dev_err(dai->dev, "Unsupported dai clock inversion\n");
134 if ((fmt & SND_SOC_DAIFMT_FORMAT_MASK) != priv->fmt) {
135 dev_err(dai->dev, "Invalid dai format\n");
139 switch (fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) {
140 case SND_SOC_DAIFMT_CBC_CFC:
141 priv->is_consumer = true;
143 case SND_SOC_DAIFMT_CBP_CFP:
144 priv->is_consumer = false;
148 dev_err(dai->dev, "Unsupported clock configuration\n");
155 static const struct snd_soc_dai_ops es7241_dai_ops = {
156 .set_fmt = es7241_set_fmt,
157 .hw_params = es7241_hw_params,
158 .set_sysclk = es7241_set_sysclk,
161 static struct snd_soc_dai_driver es7241_dai = {
162 .name = "es7241-hifi",
164 .stream_name = "Capture",
167 .rates = SNDRV_PCM_RATE_8000_192000,
168 .formats = (SNDRV_PCM_FMTBIT_S16_LE |
169 SNDRV_PCM_FMTBIT_S24_3LE |
170 SNDRV_PCM_FMTBIT_S24_LE),
172 .ops = &es7241_dai_ops,
175 static const struct es7241_clock_mode es7241_modes[] = {
177 /* Single speed mode */
180 .slv_mfs = (unsigned int[]) { 256, 384, 512, 768, 1024 },
186 /* Double speed mode */
189 .slv_mfs = (unsigned int[]) { 128, 192 },
195 /* Quad speed mode */
198 .slv_mfs = (unsigned int[]) { 64 },
206 static const struct es7241_chip es7241_chip __maybe_unused = {
207 .modes = es7241_modes,
208 .mode_num = ARRAY_SIZE(es7241_modes),
211 static const struct snd_soc_dapm_widget es7241_dapm_widgets[] = {
212 SND_SOC_DAPM_INPUT("AINL"),
213 SND_SOC_DAPM_INPUT("AINR"),
214 SND_SOC_DAPM_DAC("ADC", "Capture", SND_SOC_NOPM, 0, 0),
215 SND_SOC_DAPM_REGULATOR_SUPPLY("VDDP", 0, 0),
216 SND_SOC_DAPM_REGULATOR_SUPPLY("VDDD", 0, 0),
217 SND_SOC_DAPM_REGULATOR_SUPPLY("VDDA", 0, 0),
220 static const struct snd_soc_dapm_route es7241_dapm_routes[] = {
221 { "ADC", NULL, "AINL", },
222 { "ADC", NULL, "AINR", },
223 { "ADC", NULL, "VDDA", },
224 { "Capture", NULL, "VDDP", },
225 { "Capture", NULL, "VDDD", },
228 static const struct snd_soc_component_driver es7241_component_driver = {
229 .dapm_widgets = es7241_dapm_widgets,
230 .num_dapm_widgets = ARRAY_SIZE(es7241_dapm_widgets),
231 .dapm_routes = es7241_dapm_routes,
232 .num_dapm_routes = ARRAY_SIZE(es7241_dapm_routes),
237 static void es7241_parse_fmt(struct device *dev, struct es7241_data *priv)
242 * The format is given by a pull resistor on the SDOUT pin:
243 * pull-up for i2s, pull-down for left justified.
245 is_leftj = of_property_read_bool(dev->of_node,
246 "everest,sdout-pull-down");
248 priv->fmt = SND_SOC_DAIFMT_LEFT_J;
250 priv->fmt = SND_SOC_DAIFMT_I2S;
253 static int es7241_probe(struct platform_device *pdev)
255 struct device *dev = &pdev->dev;
256 struct es7241_data *priv;
258 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
261 platform_set_drvdata(pdev, priv);
263 priv->chip = of_device_get_match_data(dev);
265 dev_err(dev, "failed to match device\n");
269 es7241_parse_fmt(dev, priv);
271 priv->reset = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_LOW);
272 if (IS_ERR(priv->reset))
273 return dev_err_probe(dev, PTR_ERR(priv->reset),
274 "Failed to get 'reset' gpio");
276 priv->m0 = devm_gpiod_get_optional(dev, "m0", GPIOD_OUT_LOW);
277 if (IS_ERR(priv->m0))
278 return dev_err_probe(dev, PTR_ERR(priv->m0),
279 "Failed to get 'm0' gpio");
281 priv->m1 = devm_gpiod_get_optional(dev, "m1", GPIOD_OUT_LOW);
282 if (IS_ERR(priv->m1))
283 return dev_err_probe(dev, PTR_ERR(priv->m1),
284 "Failed to get 'm1' gpio");
286 return devm_snd_soc_register_component(&pdev->dev,
287 &es7241_component_driver,
292 static const struct of_device_id es7241_ids[] = {
293 { .compatible = "everest,es7241", .data = &es7241_chip },
296 MODULE_DEVICE_TABLE(of, es7241_ids);
299 static struct platform_driver es7241_driver = {
302 .of_match_table = of_match_ptr(es7241_ids),
304 .probe = es7241_probe,
307 module_platform_driver(es7241_driver);
309 MODULE_DESCRIPTION("ASoC ES7241 audio codec driver");
311 MODULE_LICENSE("GPL");