1 // SPDX-License-Identifier: GPL-2.0
3 // PCM3060 codec driver
5 // Copyright (C) 2018 Kirill Marinushkin <kmarinushkin@birdec.com>
7 #include <linux/module.h>
8 #include <sound/pcm_params.h>
10 #include <sound/tlv.h>
16 static int pcm3060_set_sysclk(struct snd_soc_dai *dai, int clk_id,
17 unsigned int freq, int dir)
19 struct snd_soc_component *comp = dai->component;
20 struct pcm3060_priv *priv = snd_soc_component_get_drvdata(comp);
24 if (dir != SND_SOC_CLOCK_IN) {
25 dev_err(comp->dev, "unsupported sysclock dir: %d\n", dir);
35 val = (dai->id == PCM3060_DAI_ID_DAC ? PCM3060_REG_CSEL : 0);
39 val = (dai->id == PCM3060_DAI_ID_DAC ? 0 : PCM3060_REG_CSEL);
43 dev_err(comp->dev, "unsupported sysclock id: %d\n", clk_id);
47 if (dai->id == PCM3060_DAI_ID_DAC)
52 regmap_update_bits(priv->regmap, reg, PCM3060_REG_CSEL, val);
54 priv->dai[dai->id].sclk_freq = freq;
59 static int pcm3060_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
61 struct snd_soc_component *comp = dai->component;
62 struct pcm3060_priv *priv = snd_soc_component_get_drvdata(comp);
66 if ((fmt & SND_SOC_DAIFMT_INV_MASK) != SND_SOC_DAIFMT_NB_NF) {
67 dev_err(comp->dev, "unsupported DAI polarity: 0x%x\n", fmt);
71 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
72 case SND_SOC_DAIFMT_CBM_CFM:
73 priv->dai[dai->id].is_master = true;
75 case SND_SOC_DAIFMT_CBS_CFS:
76 priv->dai[dai->id].is_master = false;
79 dev_err(comp->dev, "unsupported DAI master mode: 0x%x\n", fmt);
83 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
84 case SND_SOC_DAIFMT_I2S:
85 val = PCM3060_REG_FMT_I2S;
87 case SND_SOC_DAIFMT_RIGHT_J:
88 val = PCM3060_REG_FMT_RJ;
90 case SND_SOC_DAIFMT_LEFT_J:
91 val = PCM3060_REG_FMT_LJ;
94 dev_err(comp->dev, "unsupported DAI format: 0x%x\n", fmt);
98 if (dai->id == PCM3060_DAI_ID_DAC)
103 regmap_update_bits(priv->regmap, reg, PCM3060_REG_MASK_FMT, val);
108 static int pcm3060_hw_params(struct snd_pcm_substream *substream,
109 struct snd_pcm_hw_params *params,
110 struct snd_soc_dai *dai)
112 struct snd_soc_component *comp = dai->component;
113 struct pcm3060_priv *priv = snd_soc_component_get_drvdata(comp);
119 if (!priv->dai[dai->id].is_master) {
120 val = PCM3060_REG_MS_S;
124 rate = params_rate(params);
126 dev_err(comp->dev, "rate is not configured\n");
130 ratio = priv->dai[dai->id].sclk_freq / rate;
134 val = PCM3060_REG_MS_M768;
137 val = PCM3060_REG_MS_M512;
140 val = PCM3060_REG_MS_M384;
143 val = PCM3060_REG_MS_M256;
146 val = PCM3060_REG_MS_M192;
149 val = PCM3060_REG_MS_M128;
152 dev_err(comp->dev, "unsupported ratio: %d\n", ratio);
157 if (dai->id == PCM3060_DAI_ID_DAC)
162 regmap_update_bits(priv->regmap, reg, PCM3060_REG_MASK_MS, val);
167 static const struct snd_soc_dai_ops pcm3060_dai_ops = {
168 .set_sysclk = pcm3060_set_sysclk,
169 .set_fmt = pcm3060_set_fmt,
170 .hw_params = pcm3060_hw_params,
173 #define PCM3060_DAI_RATES_ADC (SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_32000 | \
174 SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 | \
175 SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000)
177 #define PCM3060_DAI_RATES_DAC (PCM3060_DAI_RATES_ADC | \
178 SNDRV_PCM_RATE_176400 | SNDRV_PCM_RATE_192000)
180 static struct snd_soc_dai_driver pcm3060_dai[] = {
182 .name = "pcm3060-dac",
183 .id = PCM3060_DAI_ID_DAC,
185 .stream_name = "Playback",
188 .rates = PCM3060_DAI_RATES_DAC,
189 .formats = SNDRV_PCM_FMTBIT_S24_LE,
191 .ops = &pcm3060_dai_ops,
194 .name = "pcm3060-adc",
195 .id = PCM3060_DAI_ID_ADC,
197 .stream_name = "Capture",
200 .rates = PCM3060_DAI_RATES_ADC,
201 .formats = SNDRV_PCM_FMTBIT_S24_LE,
203 .ops = &pcm3060_dai_ops,
209 static DECLARE_TLV_DB_SCALE(pcm3060_dapm_tlv, -10050, 50, 1);
211 static const struct snd_kcontrol_new pcm3060_dapm_controls[] = {
212 SOC_DOUBLE_R_RANGE_TLV("Master Playback Volume",
213 PCM3060_REG65, PCM3060_REG66, 0,
214 PCM3060_REG_AT2_MIN, PCM3060_REG_AT2_MAX,
215 0, pcm3060_dapm_tlv),
216 SOC_DOUBLE("Master Playback Switch", PCM3060_REG68,
217 PCM3060_REG_SHIFT_MUT21, PCM3060_REG_SHIFT_MUT22, 1, 1),
219 SOC_DOUBLE_R_RANGE_TLV("Master Capture Volume",
220 PCM3060_REG70, PCM3060_REG71, 0,
221 PCM3060_REG_AT1_MIN, PCM3060_REG_AT1_MAX,
222 0, pcm3060_dapm_tlv),
223 SOC_DOUBLE("Master Capture Switch", PCM3060_REG73,
224 PCM3060_REG_SHIFT_MUT11, PCM3060_REG_SHIFT_MUT12, 1, 1),
227 static const struct snd_soc_dapm_widget pcm3060_dapm_widgets[] = {
228 SND_SOC_DAPM_DAC("DAC", "Playback", PCM3060_REG64,
229 PCM3060_REG_SHIFT_DAPSV, 1),
231 SND_SOC_DAPM_OUTPUT("OUTL"),
232 SND_SOC_DAPM_OUTPUT("OUTR"),
234 SND_SOC_DAPM_INPUT("INL"),
235 SND_SOC_DAPM_INPUT("INR"),
237 SND_SOC_DAPM_ADC("ADC", "Capture", PCM3060_REG64,
238 PCM3060_REG_SHIFT_ADPSV, 1),
241 static const struct snd_soc_dapm_route pcm3060_dapm_map[] = {
242 { "OUTL", NULL, "DAC" },
243 { "OUTR", NULL, "DAC" },
245 { "ADC", NULL, "INL" },
246 { "ADC", NULL, "INR" },
251 static const struct snd_soc_component_driver pcm3060_soc_comp_driver = {
252 .controls = pcm3060_dapm_controls,
253 .num_controls = ARRAY_SIZE(pcm3060_dapm_controls),
254 .dapm_widgets = pcm3060_dapm_widgets,
255 .num_dapm_widgets = ARRAY_SIZE(pcm3060_dapm_widgets),
256 .dapm_routes = pcm3060_dapm_map,
257 .num_dapm_routes = ARRAY_SIZE(pcm3060_dapm_map),
262 static bool pcm3060_reg_writeable(struct device *dev, unsigned int reg)
264 return (reg >= PCM3060_REG64);
267 static bool pcm3060_reg_readable(struct device *dev, unsigned int reg)
269 return (reg >= PCM3060_REG64);
272 static bool pcm3060_reg_volatile(struct device *dev, unsigned int reg)
274 /* PCM3060_REG64 is volatile */
275 return (reg == PCM3060_REG64);
278 static const struct reg_default pcm3060_reg_defaults[] = {
279 { PCM3060_REG64, 0xF0 },
280 { PCM3060_REG65, 0xFF },
281 { PCM3060_REG66, 0xFF },
282 { PCM3060_REG67, 0x00 },
283 { PCM3060_REG68, 0x00 },
284 { PCM3060_REG69, 0x00 },
285 { PCM3060_REG70, 0xD7 },
286 { PCM3060_REG71, 0xD7 },
287 { PCM3060_REG72, 0x00 },
288 { PCM3060_REG73, 0x00 },
291 const struct regmap_config pcm3060_regmap = {
294 .writeable_reg = pcm3060_reg_writeable,
295 .readable_reg = pcm3060_reg_readable,
296 .volatile_reg = pcm3060_reg_volatile,
297 .max_register = PCM3060_REG73,
298 .reg_defaults = pcm3060_reg_defaults,
299 .num_reg_defaults = ARRAY_SIZE(pcm3060_reg_defaults),
300 .cache_type = REGCACHE_RBTREE,
302 EXPORT_SYMBOL(pcm3060_regmap);
306 static void pcm3060_parse_dt(const struct device_node *np,
307 struct pcm3060_priv *priv)
309 priv->out_se = of_property_read_bool(np, "ti,out-single-ended");
312 int pcm3060_probe(struct device *dev)
315 struct pcm3060_priv *priv = dev_get_drvdata(dev);
318 rc = regmap_update_bits(priv->regmap, PCM3060_REG64,
319 PCM3060_REG_MRST, 0);
321 dev_err(dev, "failed to reset component, rc=%d\n", rc);
326 pcm3060_parse_dt(dev->of_node, priv);
329 regmap_update_bits(priv->regmap, PCM3060_REG64,
330 PCM3060_REG_SE, PCM3060_REG_SE);
332 rc = devm_snd_soc_register_component(dev, &pcm3060_soc_comp_driver,
334 ARRAY_SIZE(pcm3060_dai));
336 dev_err(dev, "failed to register component, rc=%d\n", rc);
342 EXPORT_SYMBOL(pcm3060_probe);
344 MODULE_DESCRIPTION("PCM3060 codec driver");
345 MODULE_AUTHOR("Kirill Marinushkin <kmarinushkin@birdec.com>");
346 MODULE_LICENSE("GPL v2");