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>
31 #include <linux/of_device.h>
35 #define PCM1792A_DAC_VOL_LEFT 0x10
36 #define PCM1792A_DAC_VOL_RIGHT 0x11
37 #define PCM1792A_FMT_CONTROL 0x12
38 #define PCM1792A_SOFT_MUTE PCM1792A_FMT_CONTROL
40 #define PCM1792A_FMT_MASK 0x70
41 #define PCM1792A_FMT_SHIFT 4
42 #define PCM1792A_MUTE_MASK 0x01
43 #define PCM1792A_MUTE_SHIFT 0
44 #define PCM1792A_ATLD_ENABLE (1 << 7)
46 static const struct reg_default pcm1792a_reg_defaults
[] = {
57 static bool pcm1792a_accessible_reg(struct device
*dev
, unsigned int reg
)
59 return reg
>= 0x10 && reg
<= 0x17;
62 static bool pcm1792a_writeable_reg(struct device
*dev
, unsigned register reg
)
66 accessible
= pcm1792a_accessible_reg(dev
, reg
);
68 return accessible
&& reg
!= 0x16 && reg
!= 0x17;
71 struct pcm1792a_private
{
72 struct regmap
*regmap
;
77 static int pcm1792a_set_dai_fmt(struct snd_soc_dai
*codec_dai
,
80 struct snd_soc_codec
*codec
= codec_dai
->codec
;
81 struct pcm1792a_private
*priv
= snd_soc_codec_get_drvdata(codec
);
83 priv
->format
= format
;
88 static int pcm1792a_digital_mute(struct snd_soc_dai
*dai
, int mute
)
90 struct snd_soc_codec
*codec
= dai
->codec
;
91 struct pcm1792a_private
*priv
= snd_soc_codec_get_drvdata(codec
);
94 ret
= regmap_update_bits(priv
->regmap
, PCM1792A_SOFT_MUTE
,
95 PCM1792A_MUTE_MASK
, !!mute
);
102 static int pcm1792a_hw_params(struct snd_pcm_substream
*substream
,
103 struct snd_pcm_hw_params
*params
,
104 struct snd_soc_dai
*dai
)
106 struct snd_soc_codec
*codec
= dai
->codec
;
107 struct pcm1792a_private
*priv
= snd_soc_codec_get_drvdata(codec
);
109 int pcm_format
= params_format(params
);
111 priv
->rate
= params_rate(params
);
113 switch (priv
->format
& SND_SOC_DAIFMT_FORMAT_MASK
) {
114 case SND_SOC_DAIFMT_RIGHT_J
:
115 if (pcm_format
== SNDRV_PCM_FORMAT_S24_LE
||
116 pcm_format
== SNDRV_PCM_FORMAT_S32_LE
)
118 else if (pcm_format
== SNDRV_PCM_FORMAT_S16_LE
)
121 case SND_SOC_DAIFMT_I2S
:
122 if (pcm_format
== SNDRV_PCM_FORMAT_S24_LE
||
123 pcm_format
== SNDRV_PCM_FORMAT_S32_LE
)
125 else if (pcm_format
== SNDRV_PCM_FORMAT_S16_LE
)
129 dev_err(codec
->dev
, "Invalid DAI format\n");
133 val
= val
<< PCM1792A_FMT_SHIFT
| PCM1792A_ATLD_ENABLE
;
135 ret
= regmap_update_bits(priv
->regmap
, PCM1792A_FMT_CONTROL
,
136 PCM1792A_FMT_MASK
| PCM1792A_ATLD_ENABLE
, val
);
143 static const struct snd_soc_dai_ops pcm1792a_dai_ops
= {
144 .set_fmt
= pcm1792a_set_dai_fmt
,
145 .hw_params
= pcm1792a_hw_params
,
146 .digital_mute
= pcm1792a_digital_mute
,
149 static const DECLARE_TLV_DB_SCALE(pcm1792a_dac_tlv
, -12000, 50, 1);
151 static const struct snd_kcontrol_new pcm1792a_controls
[] = {
152 SOC_DOUBLE_R_RANGE_TLV("DAC Playback Volume", PCM1792A_DAC_VOL_LEFT
,
153 PCM1792A_DAC_VOL_RIGHT
, 0, 0xf, 0xff, 0,
157 static const struct snd_soc_dapm_widget pcm1792a_dapm_widgets
[] = {
158 SND_SOC_DAPM_OUTPUT("IOUTL+"),
159 SND_SOC_DAPM_OUTPUT("IOUTL-"),
160 SND_SOC_DAPM_OUTPUT("IOUTR+"),
161 SND_SOC_DAPM_OUTPUT("IOUTR-"),
164 static const struct snd_soc_dapm_route pcm1792a_dapm_routes
[] = {
165 { "IOUTL+", NULL
, "Playback" },
166 { "IOUTL-", NULL
, "Playback" },
167 { "IOUTR+", NULL
, "Playback" },
168 { "IOUTR-", NULL
, "Playback" },
171 static struct snd_soc_dai_driver pcm1792a_dai
= {
172 .name
= "pcm1792a-hifi",
174 .stream_name
= "Playback",
177 .rates
= PCM1792A_RATES
,
178 .formats
= PCM1792A_FORMATS
, },
179 .ops
= &pcm1792a_dai_ops
,
182 static const struct of_device_id pcm1792a_of_match
[] = {
183 { .compatible
= "ti,pcm1792a", },
186 MODULE_DEVICE_TABLE(of
, pcm1792a_of_match
);
188 static const struct regmap_config pcm1792a_regmap
= {
192 .reg_defaults
= pcm1792a_reg_defaults
,
193 .num_reg_defaults
= ARRAY_SIZE(pcm1792a_reg_defaults
),
194 .writeable_reg
= pcm1792a_writeable_reg
,
195 .readable_reg
= pcm1792a_accessible_reg
,
198 static struct snd_soc_codec_driver soc_codec_dev_pcm1792a
= {
199 .controls
= pcm1792a_controls
,
200 .num_controls
= ARRAY_SIZE(pcm1792a_controls
),
201 .dapm_widgets
= pcm1792a_dapm_widgets
,
202 .num_dapm_widgets
= ARRAY_SIZE(pcm1792a_dapm_widgets
),
203 .dapm_routes
= pcm1792a_dapm_routes
,
204 .num_dapm_routes
= ARRAY_SIZE(pcm1792a_dapm_routes
),
207 static int pcm1792a_spi_probe(struct spi_device
*spi
)
209 struct pcm1792a_private
*pcm1792a
;
212 pcm1792a
= devm_kzalloc(&spi
->dev
, sizeof(struct pcm1792a_private
),
217 spi_set_drvdata(spi
, pcm1792a
);
219 pcm1792a
->regmap
= devm_regmap_init_spi(spi
, &pcm1792a_regmap
);
220 if (IS_ERR(pcm1792a
->regmap
)) {
221 ret
= PTR_ERR(pcm1792a
->regmap
);
222 dev_err(&spi
->dev
, "Failed to register regmap: %d\n", ret
);
226 return snd_soc_register_codec(&spi
->dev
,
227 &soc_codec_dev_pcm1792a
, &pcm1792a_dai
, 1);
230 static int pcm1792a_spi_remove(struct spi_device
*spi
)
232 snd_soc_unregister_codec(&spi
->dev
);
236 static const struct spi_device_id pcm1792a_spi_ids
[] = {
240 MODULE_DEVICE_TABLE(spi
, pcm1792a_spi_ids
);
242 static struct spi_driver pcm1792a_codec_driver
= {
245 .owner
= THIS_MODULE
,
246 .of_match_table
= of_match_ptr(pcm1792a_of_match
),
248 .id_table
= pcm1792a_spi_ids
,
249 .probe
= pcm1792a_spi_probe
,
250 .remove
= pcm1792a_spi_remove
,
253 module_spi_driver(pcm1792a_codec_driver
);
255 MODULE_DESCRIPTION("ASoC PCM1792A driver");
256 MODULE_AUTHOR("Michael Trimarchi <michael@amarulasolutions.com>");
257 MODULE_LICENSE("GPL");