blob: 698289dc3e227b95c87d3dc829b3f62201dadc95 [file] [log] [blame]
Jerome Brunet9000b592017-02-27 16:47:23 +01001/*
2 * Copyright (c) 2017 BayLibre, SAS.
3 * Author: Jerome Brunet <jbrunet@baylibre.com>
4 *
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.
8 *
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.
13 *
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.
18 */
19
Jerome Bruneta016b112018-06-29 17:09:21 +020020#include <linux/of_platform.h>
Jerome Brunet9000b592017-02-27 16:47:23 +010021#include <linux/module.h>
22#include <sound/soc.h>
23
24/*
25 * The everest 7134 is a very simple DA converter with no register
26 */
27
Jerome Bruneta016b112018-06-29 17:09:21 +020028struct es7134_clock_mode {
29 unsigned int rate_min;
30 unsigned int rate_max;
31 unsigned int *mclk_fs;
32 unsigned int mclk_fs_num;
33};
34
35struct es7134_chip {
36 const struct es7134_clock_mode *modes;
37 unsigned int mode_num;
38};
39
40struct es7134_data {
41 unsigned int mclk;
42 const struct es7134_chip *chip;
43};
44
45static int es7134_check_mclk(struct snd_soc_dai *dai,
46 struct es7134_data *priv,
47 unsigned int rate)
48{
49 unsigned int mfs = priv->mclk / rate;
50 int i, j;
51
52 for (i = 0; i < priv->chip->mode_num; i++) {
53 const struct es7134_clock_mode *mode = &priv->chip->modes[i];
54
55 if (rate < mode->rate_min || rate > mode->rate_max)
56 continue;
57
58 for (j = 0; j < mode->mclk_fs_num; j++) {
59 if (mode->mclk_fs[j] == mfs)
60 return 0;
61 }
62
63 dev_err(dai->dev, "unsupported mclk_fs %u for rate %u\n",
64 mfs, rate);
65 return -EINVAL;
66 }
67
68 /* should not happen */
69 dev_err(dai->dev, "unsupported rate: %u\n", rate);
70 return -EINVAL;
71}
72
73static int es7134_hw_params(struct snd_pcm_substream *substream,
74 struct snd_pcm_hw_params *params,
75 struct snd_soc_dai *dai)
76{
77 struct es7134_data *priv = snd_soc_dai_get_drvdata(dai);
78
79 /* mclk has not been provided, assume it is OK */
80 if (!priv->mclk)
81 return 0;
82
83 return es7134_check_mclk(dai, priv, params_rate(params));
84}
85
86static int es7134_set_sysclk(struct snd_soc_dai *dai, int clk_id,
87 unsigned int freq, int dir)
88{
89 struct es7134_data *priv = snd_soc_dai_get_drvdata(dai);
90
91 if (dir == SND_SOC_CLOCK_IN && clk_id == 0) {
92 priv->mclk = freq;
93 return 0;
94 }
95
96 return -ENOTSUPP;
97}
98
Jerome Brunet9000b592017-02-27 16:47:23 +010099static int es7134_set_fmt(struct snd_soc_dai *codec_dai, unsigned int fmt)
100{
101 fmt &= (SND_SOC_DAIFMT_FORMAT_MASK | SND_SOC_DAIFMT_INV_MASK |
102 SND_SOC_DAIFMT_MASTER_MASK);
103
104 if (fmt != (SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
105 SND_SOC_DAIFMT_CBS_CFS)) {
106 dev_err(codec_dai->dev, "Invalid DAI format\n");
107 return -EINVAL;
108 }
109
110 return 0;
111}
112
113static const struct snd_soc_dai_ops es7134_dai_ops = {
114 .set_fmt = es7134_set_fmt,
Jerome Bruneta016b112018-06-29 17:09:21 +0200115 .hw_params = es7134_hw_params,
116 .set_sysclk = es7134_set_sysclk,
Jerome Brunet9000b592017-02-27 16:47:23 +0100117};
118
119static struct snd_soc_dai_driver es7134_dai = {
120 .name = "es7134-hifi",
121 .playback = {
122 .stream_name = "Playback",
123 .channels_min = 2,
124 .channels_max = 2,
Jerome Brunet56507292018-06-29 17:09:20 +0200125 .rates = (SNDRV_PCM_RATE_8000_48000 |
126 SNDRV_PCM_RATE_88200 |
127 SNDRV_PCM_RATE_96000 |
128 SNDRV_PCM_RATE_176400 |
129 SNDRV_PCM_RATE_192000),
Jerome Brunet9000b592017-02-27 16:47:23 +0100130 .formats = (SNDRV_PCM_FMTBIT_S16_LE |
131 SNDRV_PCM_FMTBIT_S18_3LE |
132 SNDRV_PCM_FMTBIT_S20_3LE |
133 SNDRV_PCM_FMTBIT_S24_3LE |
134 SNDRV_PCM_FMTBIT_S24_LE),
135 },
136 .ops = &es7134_dai_ops,
137};
138
Jerome Bruneta016b112018-06-29 17:09:21 +0200139static const struct es7134_clock_mode es7134_modes[] = {
140 {
141 /* Single speed mode */
142 .rate_min = 8000,
143 .rate_max = 50000,
144 .mclk_fs = (unsigned int[]) { 256, 384, 512, 768, 1024 },
145 .mclk_fs_num = 5,
146 }, {
147 /* Double speed mode */
148 .rate_min = 84000,
149 .rate_max = 100000,
150 .mclk_fs = (unsigned int[]) { 128, 192, 256, 384, 512 },
151 .mclk_fs_num = 5,
152 }, {
153 /* Quad speed mode */
154 .rate_min = 167000,
155 .rate_max = 192000,
156 .mclk_fs = (unsigned int[]) { 128, 192, 256 },
157 .mclk_fs_num = 3,
158 },
159};
160
161static const struct es7134_chip es7134_chip = {
162 .modes = es7134_modes,
163 .mode_num = ARRAY_SIZE(es7134_modes),
164};
165
Jerome Brunet9000b592017-02-27 16:47:23 +0100166static const struct snd_soc_dapm_widget es7134_dapm_widgets[] = {
167 SND_SOC_DAPM_OUTPUT("AOUTL"),
168 SND_SOC_DAPM_OUTPUT("AOUTR"),
169 SND_SOC_DAPM_DAC("DAC", "Playback", SND_SOC_NOPM, 0, 0),
170};
171
172static const struct snd_soc_dapm_route es7134_dapm_routes[] = {
173 { "AOUTL", NULL, "DAC" },
174 { "AOUTR", NULL, "DAC" },
175};
176
Kuninori Morimoto02009ee2018-01-29 04:32:37 +0000177static const struct snd_soc_component_driver es7134_component_driver = {
178 .dapm_widgets = es7134_dapm_widgets,
179 .num_dapm_widgets = ARRAY_SIZE(es7134_dapm_widgets),
180 .dapm_routes = es7134_dapm_routes,
181 .num_dapm_routes = ARRAY_SIZE(es7134_dapm_routes),
182 .idle_bias_on = 1,
183 .use_pmdown_time = 1,
184 .endianness = 1,
185 .non_legacy_dai_naming = 1,
Jerome Brunet9000b592017-02-27 16:47:23 +0100186};
187
188static int es7134_probe(struct platform_device *pdev)
189{
Jerome Bruneta016b112018-06-29 17:09:21 +0200190 struct device *dev = &pdev->dev;
191 struct es7134_data *priv;
192
193 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
194 if (!priv)
195 return -ENOMEM;
196 platform_set_drvdata(pdev, priv);
197
198 priv->chip = of_device_get_match_data(dev);
199 if (!priv->chip) {
200 dev_err(dev, "failed to match device\n");
201 return -ENODEV;
202 }
203
Kuninori Morimoto02009ee2018-01-29 04:32:37 +0000204 return devm_snd_soc_register_component(&pdev->dev,
205 &es7134_component_driver,
Jerome Brunet9000b592017-02-27 16:47:23 +0100206 &es7134_dai, 1);
207}
208
Jerome Brunet9000b592017-02-27 16:47:23 +0100209#ifdef CONFIG_OF
210static const struct of_device_id es7134_ids[] = {
Jerome Bruneta016b112018-06-29 17:09:21 +0200211 { .compatible = "everest,es7134", .data = &es7134_chip },
212 { .compatible = "everest,es7144", .data = &es7134_chip },
Jerome Brunet9000b592017-02-27 16:47:23 +0100213 { }
214};
215MODULE_DEVICE_TABLE(of, es7134_ids);
216#endif
217
218static struct platform_driver es7134_driver = {
219 .driver = {
220 .name = "es7134",
221 .of_match_table = of_match_ptr(es7134_ids),
222 },
223 .probe = es7134_probe,
Jerome Brunet9000b592017-02-27 16:47:23 +0100224};
225
226module_platform_driver(es7134_driver);
227
228MODULE_DESCRIPTION("ASoC ES7134 audio codec driver");
229MODULE_AUTHOR("Jerome Brunet <jbrunet@baylibre.com>");
230MODULE_LICENSE("GPL");