2 * Copyright (c) 2017 BayLibre, SAS.
3 * Author: Jerome Brunet <jbrunet@baylibre.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of version 2 of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see <http://www.gnu.org/licenses/>.
16 * The full GNU General Public License is included in this distribution
17 * in the file called COPYING.
20 #include <linux/of_platform.h>
21 #include <linux/module.h>
22 #include <sound/soc.h>
25 * The everest 7134 is a very simple DA converter with no register
28 struct es7134_clock_mode {
29 unsigned int rate_min;
30 unsigned int rate_max;
31 unsigned int *mclk_fs;
32 unsigned int mclk_fs_num;
36 struct snd_soc_dai_driver *dai_drv;
37 const struct es7134_clock_mode *modes;
38 unsigned int mode_num;
39 const struct snd_soc_dapm_widget *extra_widgets;
40 unsigned int extra_widget_num;
41 const struct snd_soc_dapm_route *extra_routes;
42 unsigned int extra_route_num;
47 const struct es7134_chip *chip;
50 static int es7134_check_mclk(struct snd_soc_dai *dai,
51 struct es7134_data *priv,
54 unsigned int mfs = priv->mclk / rate;
57 for (i = 0; i < priv->chip->mode_num; i++) {
58 const struct es7134_clock_mode *mode = &priv->chip->modes[i];
60 if (rate < mode->rate_min || rate > mode->rate_max)
63 for (j = 0; j < mode->mclk_fs_num; j++) {
64 if (mode->mclk_fs[j] == mfs)
68 dev_err(dai->dev, "unsupported mclk_fs %u for rate %u\n",
73 /* should not happen */
74 dev_err(dai->dev, "unsupported rate: %u\n", rate);
78 static int es7134_hw_params(struct snd_pcm_substream *substream,
79 struct snd_pcm_hw_params *params,
80 struct snd_soc_dai *dai)
82 struct es7134_data *priv = snd_soc_dai_get_drvdata(dai);
84 /* mclk has not been provided, assume it is OK */
88 return es7134_check_mclk(dai, priv, params_rate(params));
91 static int es7134_set_sysclk(struct snd_soc_dai *dai, int clk_id,
92 unsigned int freq, int dir)
94 struct es7134_data *priv = snd_soc_dai_get_drvdata(dai);
96 if (dir == SND_SOC_CLOCK_IN && clk_id == 0) {
104 static int es7134_set_fmt(struct snd_soc_dai *codec_dai, unsigned int fmt)
106 fmt &= (SND_SOC_DAIFMT_FORMAT_MASK | SND_SOC_DAIFMT_INV_MASK |
107 SND_SOC_DAIFMT_MASTER_MASK);
109 if (fmt != (SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
110 SND_SOC_DAIFMT_CBS_CFS)) {
111 dev_err(codec_dai->dev, "Invalid DAI format\n");
118 static int es7134_component_probe(struct snd_soc_component *c)
120 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(c);
121 struct es7134_data *priv = snd_soc_component_get_drvdata(c);
122 const struct es7134_chip *chip = priv->chip;
125 if (chip->extra_widget_num) {
126 ret = snd_soc_dapm_new_controls(dapm, chip->extra_widgets,
127 chip->extra_widget_num);
129 dev_err(c->dev, "failed to add extra widgets\n");
134 if (chip->extra_route_num) {
135 ret = snd_soc_dapm_add_routes(dapm, chip->extra_routes,
136 chip->extra_route_num);
138 dev_err(c->dev, "failed to add extra routes\n");
146 static const struct snd_soc_dai_ops es7134_dai_ops = {
147 .set_fmt = es7134_set_fmt,
148 .hw_params = es7134_hw_params,
149 .set_sysclk = es7134_set_sysclk,
152 static struct snd_soc_dai_driver es7134_dai = {
153 .name = "es7134-hifi",
155 .stream_name = "Playback",
158 .rates = (SNDRV_PCM_RATE_8000_48000 |
159 SNDRV_PCM_RATE_88200 |
160 SNDRV_PCM_RATE_96000 |
161 SNDRV_PCM_RATE_176400 |
162 SNDRV_PCM_RATE_192000),
163 .formats = (SNDRV_PCM_FMTBIT_S16_LE |
164 SNDRV_PCM_FMTBIT_S18_3LE |
165 SNDRV_PCM_FMTBIT_S20_3LE |
166 SNDRV_PCM_FMTBIT_S24_3LE |
167 SNDRV_PCM_FMTBIT_S24_LE),
169 .ops = &es7134_dai_ops,
172 static const struct es7134_clock_mode es7134_modes[] = {
174 /* Single speed mode */
177 .mclk_fs = (unsigned int[]) { 256, 384, 512, 768, 1024 },
180 /* Double speed mode */
183 .mclk_fs = (unsigned int[]) { 128, 192, 256, 384, 512 },
186 /* Quad speed mode */
189 .mclk_fs = (unsigned int[]) { 128, 192, 256 },
194 /* Digital I/O are also supplied by VDD on the es7134 */
195 static const struct snd_soc_dapm_route es7134_extra_routes[] = {
196 { "Playback", NULL, "VDD", }
199 static const struct es7134_chip es7134_chip = {
200 .dai_drv = &es7134_dai,
201 .modes = es7134_modes,
202 .mode_num = ARRAY_SIZE(es7134_modes),
203 .extra_routes = es7134_extra_routes,
204 .extra_route_num = ARRAY_SIZE(es7134_extra_routes),
207 static const struct snd_soc_dapm_widget es7134_dapm_widgets[] = {
208 SND_SOC_DAPM_OUTPUT("AOUTL"),
209 SND_SOC_DAPM_OUTPUT("AOUTR"),
210 SND_SOC_DAPM_DAC("DAC", "Playback", SND_SOC_NOPM, 0, 0),
211 SND_SOC_DAPM_REGULATOR_SUPPLY("VDD", 0, 0),
214 static const struct snd_soc_dapm_route es7134_dapm_routes[] = {
215 { "AOUTL", NULL, "DAC" },
216 { "AOUTR", NULL, "DAC" },
217 { "DAC", NULL, "VDD" },
220 static const struct snd_soc_component_driver es7134_component_driver = {
221 .probe = es7134_component_probe,
222 .dapm_widgets = es7134_dapm_widgets,
223 .num_dapm_widgets = ARRAY_SIZE(es7134_dapm_widgets),
224 .dapm_routes = es7134_dapm_routes,
225 .num_dapm_routes = ARRAY_SIZE(es7134_dapm_routes),
227 .use_pmdown_time = 1,
229 .non_legacy_dai_naming = 1,
232 static struct snd_soc_dai_driver es7154_dai = {
233 .name = "es7154-hifi",
235 .stream_name = "Playback",
238 .rates = (SNDRV_PCM_RATE_8000_48000 |
239 SNDRV_PCM_RATE_88200 |
240 SNDRV_PCM_RATE_96000),
241 .formats = (SNDRV_PCM_FMTBIT_S16_LE |
242 SNDRV_PCM_FMTBIT_S18_3LE |
243 SNDRV_PCM_FMTBIT_S20_3LE |
244 SNDRV_PCM_FMTBIT_S24_3LE |
245 SNDRV_PCM_FMTBIT_S24_LE),
247 .ops = &es7134_dai_ops,
250 static const struct es7134_clock_mode es7154_modes[] = {
252 /* Single speed mode */
255 .mclk_fs = (unsigned int[]) { 32, 64, 128, 192, 256,
256 384, 512, 768, 1024 },
259 /* Double speed mode */
262 .mclk_fs = (unsigned int[]) { 128, 192, 256, 384, 512,
268 /* Es7154 has a separate supply for digital I/O */
269 static const struct snd_soc_dapm_widget es7154_extra_widgets[] = {
270 SND_SOC_DAPM_REGULATOR_SUPPLY("PVDD", 0, 0),
273 static const struct snd_soc_dapm_route es7154_extra_routes[] = {
274 { "Playback", NULL, "PVDD", }
277 static const struct es7134_chip es7154_chip = {
278 .dai_drv = &es7154_dai,
279 .modes = es7154_modes,
280 .mode_num = ARRAY_SIZE(es7154_modes),
281 .extra_routes = es7154_extra_routes,
282 .extra_route_num = ARRAY_SIZE(es7154_extra_routes),
283 .extra_widgets = es7154_extra_widgets,
284 .extra_widget_num = ARRAY_SIZE(es7154_extra_widgets),
287 static int es7134_probe(struct platform_device *pdev)
289 struct device *dev = &pdev->dev;
290 struct es7134_data *priv;
292 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
295 platform_set_drvdata(pdev, priv);
297 priv->chip = of_device_get_match_data(dev);
299 dev_err(dev, "failed to match device\n");
303 return devm_snd_soc_register_component(&pdev->dev,
304 &es7134_component_driver,
305 priv->chip->dai_drv, 1);
309 static const struct of_device_id es7134_ids[] = {
310 { .compatible = "everest,es7134", .data = &es7134_chip },
311 { .compatible = "everest,es7144", .data = &es7134_chip },
312 { .compatible = "everest,es7154", .data = &es7154_chip },
315 MODULE_DEVICE_TABLE(of, es7134_ids);
318 static struct platform_driver es7134_driver = {
321 .of_match_table = of_match_ptr(es7134_ids),
323 .probe = es7134_probe,
326 module_platform_driver(es7134_driver);
328 MODULE_DESCRIPTION("ASoC ES7134 audio codec driver");
329 MODULE_AUTHOR("Jerome Brunet <jbrunet@baylibre.com>");
330 MODULE_LICENSE("GPL");