2 * PCM179X 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 PCM179X_DAC_VOL_LEFT 0x10
37 #define PCM179X_DAC_VOL_RIGHT 0x11
38 #define PCM179X_FMT_CONTROL 0x12
39 #define PCM179X_MODE_CONTROL 0x13
40 #define PCM179X_SOFT_MUTE PCM179X_FMT_CONTROL
42 #define PCM179X_FMT_MASK 0x70
43 #define PCM179X_FMT_SHIFT 4
44 #define PCM179X_MUTE_MASK 0x01
45 #define PCM179X_MUTE_SHIFT 0
46 #define PCM179X_ATLD_ENABLE (1 << 7)
48 static const struct reg_default pcm179x_reg_defaults[] = {
59 static bool pcm179x_accessible_reg(struct device *dev, unsigned int reg)
61 return reg >= 0x10 && reg <= 0x17;
64 static bool pcm179x_writeable_reg(struct device *dev, unsigned register reg)
68 accessible = pcm179x_accessible_reg(dev, reg);
70 return accessible && reg != 0x16 && reg != 0x17;
73 struct pcm179x_private {
74 struct regmap *regmap;
79 static int pcm179x_set_dai_fmt(struct snd_soc_dai *codec_dai,
82 struct snd_soc_codec *codec = codec_dai->codec;
83 struct pcm179x_private *priv = snd_soc_codec_get_drvdata(codec);
85 priv->format = format;
90 static int pcm179x_digital_mute(struct snd_soc_dai *dai, int mute)
92 struct snd_soc_codec *codec = dai->codec;
93 struct pcm179x_private *priv = snd_soc_codec_get_drvdata(codec);
96 ret = regmap_update_bits(priv->regmap, PCM179X_SOFT_MUTE,
97 PCM179X_MUTE_MASK, !!mute);
104 static int pcm179x_hw_params(struct snd_pcm_substream *substream,
105 struct snd_pcm_hw_params *params,
106 struct snd_soc_dai *dai)
108 struct snd_soc_codec *codec = dai->codec;
109 struct pcm179x_private *priv = snd_soc_codec_get_drvdata(codec);
112 priv->rate = params_rate(params);
114 switch (priv->format & SND_SOC_DAIFMT_FORMAT_MASK) {
115 case SND_SOC_DAIFMT_RIGHT_J:
116 switch (params_width(params)) {
128 case SND_SOC_DAIFMT_I2S:
129 switch (params_width(params)) {
142 dev_err(codec->dev, "Invalid DAI format\n");
146 val = val << PCM179X_FMT_SHIFT | PCM179X_ATLD_ENABLE;
148 ret = regmap_update_bits(priv->regmap, PCM179X_FMT_CONTROL,
149 PCM179X_FMT_MASK | PCM179X_ATLD_ENABLE, val);
156 static const struct snd_soc_dai_ops pcm179x_dai_ops = {
157 .set_fmt = pcm179x_set_dai_fmt,
158 .hw_params = pcm179x_hw_params,
159 .digital_mute = pcm179x_digital_mute,
162 static const DECLARE_TLV_DB_SCALE(pcm179x_dac_tlv, -12000, 50, 1);
164 static const struct snd_kcontrol_new pcm179x_controls[] = {
165 SOC_DOUBLE_R_RANGE_TLV("DAC Playback Volume", PCM179X_DAC_VOL_LEFT,
166 PCM179X_DAC_VOL_RIGHT, 0, 0xf, 0xff, 0,
168 SOC_SINGLE("DAC Invert Output Switch", PCM179X_MODE_CONTROL, 7, 1, 0),
169 SOC_SINGLE("DAC Rolloff Filter Switch", PCM179X_MODE_CONTROL, 1, 1, 0),
172 static const struct snd_soc_dapm_widget pcm179x_dapm_widgets[] = {
173 SND_SOC_DAPM_OUTPUT("IOUTL+"),
174 SND_SOC_DAPM_OUTPUT("IOUTL-"),
175 SND_SOC_DAPM_OUTPUT("IOUTR+"),
176 SND_SOC_DAPM_OUTPUT("IOUTR-"),
179 static const struct snd_soc_dapm_route pcm179x_dapm_routes[] = {
180 { "IOUTL+", NULL, "Playback" },
181 { "IOUTL-", NULL, "Playback" },
182 { "IOUTR+", NULL, "Playback" },
183 { "IOUTR-", NULL, "Playback" },
186 static struct snd_soc_dai_driver pcm179x_dai = {
187 .name = "pcm179x-hifi",
189 .stream_name = "Playback",
192 .rates = PCM1792A_RATES,
193 .formats = PCM1792A_FORMATS, },
194 .ops = &pcm179x_dai_ops,
197 static const struct of_device_id pcm179x_of_match[] = {
198 { .compatible = "ti,pcm1792a", },
201 MODULE_DEVICE_TABLE(of, pcm179x_of_match);
203 static const struct regmap_config pcm179x_regmap = {
207 .reg_defaults = pcm179x_reg_defaults,
208 .num_reg_defaults = ARRAY_SIZE(pcm179x_reg_defaults),
209 .writeable_reg = pcm179x_writeable_reg,
210 .readable_reg = pcm179x_accessible_reg,
213 static struct snd_soc_codec_driver soc_codec_dev_pcm179x = {
214 .controls = pcm179x_controls,
215 .num_controls = ARRAY_SIZE(pcm179x_controls),
216 .dapm_widgets = pcm179x_dapm_widgets,
217 .num_dapm_widgets = ARRAY_SIZE(pcm179x_dapm_widgets),
218 .dapm_routes = pcm179x_dapm_routes,
219 .num_dapm_routes = ARRAY_SIZE(pcm179x_dapm_routes),
222 static int pcm179x_spi_probe(struct spi_device *spi)
224 struct pcm179x_private *pcm179x;
227 pcm179x = devm_kzalloc(&spi->dev, sizeof(struct pcm179x_private),
232 spi_set_drvdata(spi, pcm179x);
234 pcm179x->regmap = devm_regmap_init_spi(spi, &pcm179x_regmap);
235 if (IS_ERR(pcm179x->regmap)) {
236 ret = PTR_ERR(pcm179x->regmap);
237 dev_err(&spi->dev, "Failed to register regmap: %d\n", ret);
241 return snd_soc_register_codec(&spi->dev,
242 &soc_codec_dev_pcm179x, &pcm179x_dai, 1);
245 static int pcm179x_spi_remove(struct spi_device *spi)
247 snd_soc_unregister_codec(&spi->dev);
251 static const struct spi_device_id pcm179x_spi_ids[] = {
255 MODULE_DEVICE_TABLE(spi, pcm179x_spi_ids);
257 static struct spi_driver pcm179x_codec_driver = {
260 .of_match_table = of_match_ptr(pcm179x_of_match),
262 .id_table = pcm179x_spi_ids,
263 .probe = pcm179x_spi_probe,
264 .remove = pcm179x_spi_remove,
267 module_spi_driver(pcm179x_codec_driver);
269 MODULE_DESCRIPTION("ASoC PCM179X driver");
270 MODULE_AUTHOR("Michael Trimarchi <michael@amarulasolutions.com>");
271 MODULE_LICENSE("GPL");