2 * PCM1792A ASoC codec driver
4 * Copyright (c) Amarula Solutions B.V. 2013
6 * Michael Trimarchi <michael@amarulasolutions.com>
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
19 #include <linux/module.h>
20 #include <linux/slab.h>
21 #include <linux/kernel.h>
22 #include <linux/device.h>
23 #include <linux/spi/spi.h>
25 #include <sound/core.h>
26 #include <sound/pcm.h>
27 #include <sound/pcm_params.h>
28 #include <sound/initval.h>
29 #include <sound/soc.h>
30 #include <sound/tlv.h>
32 #include <linux/of_device.h>
36 #define PCM1792A_DAC_VOL_LEFT 0x10
37 #define PCM1792A_DAC_VOL_RIGHT 0x11
38 #define PCM1792A_FMT_CONTROL 0x12
39 #define PCM1792A_SOFT_MUTE PCM1792A_FMT_CONTROL
41 #define PCM1792A_FMT_MASK 0x70
42 #define PCM1792A_FMT_SHIFT 4
43 #define PCM1792A_MUTE_MASK 0x01
44 #define PCM1792A_MUTE_SHIFT 0
45 #define PCM1792A_ATLD_ENABLE (1 << 7)
47 static const struct reg_default pcm1792a_reg_defaults[] = {
58 static bool pcm1792a_accessible_reg(struct device *dev, unsigned int reg)
60 return reg >= 0x10 && reg <= 0x17;
63 static bool pcm1792a_writeable_reg(struct device *dev, unsigned register reg)
67 accessible = pcm1792a_accessible_reg(dev, reg);
69 return accessible && reg != 0x16 && reg != 0x17;
72 struct pcm1792a_private {
73 struct regmap *regmap;
78 static int pcm1792a_set_dai_fmt(struct snd_soc_dai *codec_dai,
81 struct snd_soc_codec *codec = codec_dai->codec;
82 struct pcm1792a_private *priv = snd_soc_codec_get_drvdata(codec);
84 priv->format = format;
89 static int pcm1792a_digital_mute(struct snd_soc_dai *dai, int mute)
91 struct snd_soc_codec *codec = dai->codec;
92 struct pcm1792a_private *priv = snd_soc_codec_get_drvdata(codec);
95 ret = regmap_update_bits(priv->regmap, PCM1792A_SOFT_MUTE,
96 PCM1792A_MUTE_MASK, !!mute);
103 static int pcm1792a_hw_params(struct snd_pcm_substream *substream,
104 struct snd_pcm_hw_params *params,
105 struct snd_soc_dai *dai)
107 struct snd_soc_codec *codec = dai->codec;
108 struct pcm1792a_private *priv = snd_soc_codec_get_drvdata(codec);
110 int pcm_format = params_format(params);
112 priv->rate = params_rate(params);
114 switch (priv->format & SND_SOC_DAIFMT_FORMAT_MASK) {
115 case SND_SOC_DAIFMT_RIGHT_J:
116 if (pcm_format == SNDRV_PCM_FORMAT_S24_LE ||
117 pcm_format == SNDRV_PCM_FORMAT_S32_LE)
119 else if (pcm_format == SNDRV_PCM_FORMAT_S16_LE)
122 case SND_SOC_DAIFMT_I2S:
123 if (pcm_format == SNDRV_PCM_FORMAT_S24_LE ||
124 pcm_format == SNDRV_PCM_FORMAT_S32_LE)
126 else if (pcm_format == SNDRV_PCM_FORMAT_S16_LE)
130 dev_err(codec->dev, "Invalid DAI format\n");
134 val = val << PCM1792A_FMT_SHIFT | PCM1792A_ATLD_ENABLE;
136 ret = regmap_update_bits(priv->regmap, PCM1792A_FMT_CONTROL,
137 PCM1792A_FMT_MASK | PCM1792A_ATLD_ENABLE, val);
144 static const struct snd_soc_dai_ops pcm1792a_dai_ops = {
145 .set_fmt = pcm1792a_set_dai_fmt,
146 .hw_params = pcm1792a_hw_params,
147 .digital_mute = pcm1792a_digital_mute,
150 static const DECLARE_TLV_DB_SCALE(pcm1792a_dac_tlv, -12000, 50, 1);
152 static const struct snd_kcontrol_new pcm1792a_controls[] = {
153 SOC_DOUBLE_R_RANGE_TLV("DAC Playback Volume", PCM1792A_DAC_VOL_LEFT,
154 PCM1792A_DAC_VOL_RIGHT, 0, 0xf, 0xff, 0,
158 static const struct snd_soc_dapm_widget pcm1792a_dapm_widgets[] = {
159 SND_SOC_DAPM_OUTPUT("IOUTL+"),
160 SND_SOC_DAPM_OUTPUT("IOUTL-"),
161 SND_SOC_DAPM_OUTPUT("IOUTR+"),
162 SND_SOC_DAPM_OUTPUT("IOUTR-"),
165 static const struct snd_soc_dapm_route pcm1792a_dapm_routes[] = {
166 { "IOUTL+", NULL, "Playback" },
167 { "IOUTL-", NULL, "Playback" },
168 { "IOUTR+", NULL, "Playback" },
169 { "IOUTR-", NULL, "Playback" },
172 static struct snd_soc_dai_driver pcm1792a_dai = {
173 .name = "pcm1792a-hifi",
175 .stream_name = "Playback",
178 .rates = PCM1792A_RATES,
179 .formats = PCM1792A_FORMATS, },
180 .ops = &pcm1792a_dai_ops,
183 static const struct of_device_id pcm1792a_of_match[] = {
184 { .compatible = "ti,pcm1792a", },
187 MODULE_DEVICE_TABLE(of, pcm1792a_of_match);
189 static const struct regmap_config pcm1792a_regmap = {
193 .reg_defaults = pcm1792a_reg_defaults,
194 .num_reg_defaults = ARRAY_SIZE(pcm1792a_reg_defaults),
195 .writeable_reg = pcm1792a_writeable_reg,
196 .readable_reg = pcm1792a_accessible_reg,
199 static struct snd_soc_codec_driver soc_codec_dev_pcm1792a = {
200 .controls = pcm1792a_controls,
201 .num_controls = ARRAY_SIZE(pcm1792a_controls),
202 .dapm_widgets = pcm1792a_dapm_widgets,
203 .num_dapm_widgets = ARRAY_SIZE(pcm1792a_dapm_widgets),
204 .dapm_routes = pcm1792a_dapm_routes,
205 .num_dapm_routes = ARRAY_SIZE(pcm1792a_dapm_routes),
208 static int pcm1792a_spi_probe(struct spi_device *spi)
210 struct pcm1792a_private *pcm1792a;
213 pcm1792a = devm_kzalloc(&spi->dev, sizeof(struct pcm1792a_private),
218 spi_set_drvdata(spi, pcm1792a);
220 pcm1792a->regmap = devm_regmap_init_spi(spi, &pcm1792a_regmap);
221 if (IS_ERR(pcm1792a->regmap)) {
222 ret = PTR_ERR(pcm1792a->regmap);
223 dev_err(&spi->dev, "Failed to register regmap: %d\n", ret);
227 return snd_soc_register_codec(&spi->dev,
228 &soc_codec_dev_pcm1792a, &pcm1792a_dai, 1);
231 static int pcm1792a_spi_remove(struct spi_device *spi)
233 snd_soc_unregister_codec(&spi->dev);
237 static const struct spi_device_id pcm1792a_spi_ids[] = {
241 MODULE_DEVICE_TABLE(spi, pcm1792a_spi_ids);
243 static struct spi_driver pcm1792a_codec_driver = {
246 .owner = THIS_MODULE,
247 .of_match_table = of_match_ptr(pcm1792a_of_match),
249 .id_table = pcm1792a_spi_ids,
250 .probe = pcm1792a_spi_probe,
251 .remove = pcm1792a_spi_remove,
254 module_spi_driver(pcm1792a_codec_driver);
256 MODULE_DESCRIPTION("ASoC PCM1792A driver");
257 MODULE_AUTHOR("Michael Trimarchi <michael@amarulasolutions.com>");
258 MODULE_LICENSE("GPL");