1 // SPDX-License-Identifier: GPL-2.0
2 // Audio driver for PCM1789
3 // Copyright (C) 2018 Bootlin
4 // Mylène Josserand <mylene.josserand@bootlin.com>
6 #include <linux/gpio/consumer.h>
7 #include <linux/module.h>
8 #include <linux/workqueue.h>
10 #include <sound/pcm_params.h>
11 #include <sound/soc.h>
12 #include <sound/tlv.h>
16 #define PCM1789_MUTE_CONTROL 0x10
17 #define PCM1789_FMT_CONTROL 0x11
18 #define PCM1789_SOFT_MUTE 0x14
19 #define PCM1789_DAC_VOL_LEFT 0x18
20 #define PCM1789_DAC_VOL_RIGHT 0x19
22 #define PCM1789_FMT_MASK 0x07
23 #define PCM1789_MUTE_MASK 0x03
24 #define PCM1789_MUTE_SRET 0x06
26 struct pcm1789_private
{
27 struct regmap
*regmap
;
30 struct gpio_desc
*reset
;
31 struct work_struct work
;
35 static const struct reg_default pcm1789_reg_defaults
[] = {
36 { PCM1789_FMT_CONTROL
, 0x00 },
37 { PCM1789_SOFT_MUTE
, 0x00 },
38 { PCM1789_DAC_VOL_LEFT
, 0xff },
39 { PCM1789_DAC_VOL_RIGHT
, 0xff },
42 static bool pcm1789_accessible_reg(struct device
*dev
, unsigned int reg
)
44 return reg
>= PCM1789_MUTE_CONTROL
&& reg
<= PCM1789_DAC_VOL_RIGHT
;
47 static bool pcm1789_writeable_reg(struct device
*dev
, unsigned int reg
)
49 return pcm1789_accessible_reg(dev
, reg
);
52 static int pcm1789_set_dai_fmt(struct snd_soc_dai
*codec_dai
,
55 struct snd_soc_component
*component
= codec_dai
->component
;
56 struct pcm1789_private
*priv
= snd_soc_component_get_drvdata(component
);
58 priv
->format
= format
;
63 static int pcm1789_mute(struct snd_soc_dai
*codec_dai
, int mute
, int direction
)
65 struct snd_soc_component
*component
= codec_dai
->component
;
66 struct pcm1789_private
*priv
= snd_soc_component_get_drvdata(component
);
68 return regmap_update_bits(priv
->regmap
, PCM1789_SOFT_MUTE
,
70 mute
? 0 : PCM1789_MUTE_MASK
);
73 static int pcm1789_hw_params(struct snd_pcm_substream
*substream
,
74 struct snd_pcm_hw_params
*params
,
75 struct snd_soc_dai
*codec_dai
)
77 struct snd_soc_component
*component
= codec_dai
->component
;
78 struct pcm1789_private
*priv
= snd_soc_component_get_drvdata(component
);
81 priv
->rate
= params_rate(params
);
83 switch (priv
->format
& SND_SOC_DAIFMT_FORMAT_MASK
) {
84 case SND_SOC_DAIFMT_RIGHT_J
:
85 switch (params_width(params
)) {
96 case SND_SOC_DAIFMT_I2S
:
97 switch (params_width(params
)) {
107 case SND_SOC_DAIFMT_LEFT_J
:
108 switch (params_width(params
)) {
119 dev_err(component
->dev
, "Invalid DAI format\n");
123 ret
= regmap_update_bits(priv
->regmap
, PCM1789_FMT_CONTROL
,
124 PCM1789_FMT_MASK
, val
);
131 static void pcm1789_work_queue(struct work_struct
*work
)
133 struct pcm1789_private
*priv
= container_of(work
,
134 struct pcm1789_private
,
137 /* Perform a software reset to remove codec from desynchronized state */
138 if (regmap_update_bits(priv
->regmap
, PCM1789_MUTE_CONTROL
,
139 0x3 << PCM1789_MUTE_SRET
, 0) < 0)
140 dev_err(priv
->dev
, "Error while setting SRET");
143 static int pcm1789_trigger(struct snd_pcm_substream
*substream
, int cmd
,
144 struct snd_soc_dai
*dai
)
146 struct snd_soc_component
*component
= dai
->component
;
147 struct pcm1789_private
*priv
= snd_soc_component_get_drvdata(component
);
151 case SNDRV_PCM_TRIGGER_START
:
152 case SNDRV_PCM_TRIGGER_RESUME
:
153 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE
:
154 schedule_work(&priv
->work
);
156 case SNDRV_PCM_TRIGGER_STOP
:
157 case SNDRV_PCM_TRIGGER_SUSPEND
:
158 case SNDRV_PCM_TRIGGER_PAUSE_PUSH
:
167 static const struct snd_soc_dai_ops pcm1789_dai_ops
= {
168 .set_fmt
= pcm1789_set_dai_fmt
,
169 .hw_params
= pcm1789_hw_params
,
170 .mute_stream
= pcm1789_mute
,
171 .trigger
= pcm1789_trigger
,
172 .no_capture_mute
= 1,
175 static const DECLARE_TLV_DB_SCALE(pcm1789_dac_tlv
, -12000, 50, 1);
177 static const struct snd_kcontrol_new pcm1789_controls
[] = {
178 SOC_DOUBLE_R_RANGE_TLV("DAC Playback Volume", PCM1789_DAC_VOL_LEFT
,
179 PCM1789_DAC_VOL_RIGHT
, 0, 0xf, 0xff, 0,
183 static const struct snd_soc_dapm_widget pcm1789_dapm_widgets
[] = {
184 SND_SOC_DAPM_OUTPUT("IOUTL+"),
185 SND_SOC_DAPM_OUTPUT("IOUTL-"),
186 SND_SOC_DAPM_OUTPUT("IOUTR+"),
187 SND_SOC_DAPM_OUTPUT("IOUTR-"),
190 static const struct snd_soc_dapm_route pcm1789_dapm_routes
[] = {
191 { "IOUTL+", NULL
, "Playback" },
192 { "IOUTL-", NULL
, "Playback" },
193 { "IOUTR+", NULL
, "Playback" },
194 { "IOUTR-", NULL
, "Playback" },
197 static struct snd_soc_dai_driver pcm1789_dai
= {
198 .name
= "pcm1789-hifi",
200 .stream_name
= "Playback",
203 .rates
= SNDRV_PCM_RATE_CONTINUOUS
,
206 .formats
= PCM1789_FORMATS
,
208 .ops
= &pcm1789_dai_ops
,
211 const struct regmap_config pcm1789_regmap_config
= {
214 .max_register
= PCM1789_DAC_VOL_RIGHT
,
215 .reg_defaults
= pcm1789_reg_defaults
,
216 .num_reg_defaults
= ARRAY_SIZE(pcm1789_reg_defaults
),
217 .writeable_reg
= pcm1789_writeable_reg
,
218 .readable_reg
= pcm1789_accessible_reg
,
220 EXPORT_SYMBOL_GPL(pcm1789_regmap_config
);
222 static const struct snd_soc_component_driver soc_component_dev_pcm1789
= {
223 .controls
= pcm1789_controls
,
224 .num_controls
= ARRAY_SIZE(pcm1789_controls
),
225 .dapm_widgets
= pcm1789_dapm_widgets
,
226 .num_dapm_widgets
= ARRAY_SIZE(pcm1789_dapm_widgets
),
227 .dapm_routes
= pcm1789_dapm_routes
,
228 .num_dapm_routes
= ARRAY_SIZE(pcm1789_dapm_routes
),
230 .use_pmdown_time
= 1,
232 .non_legacy_dai_naming
= 1,
235 int pcm1789_common_init(struct device
*dev
, struct regmap
*regmap
)
237 struct pcm1789_private
*pcm1789
;
239 pcm1789
= devm_kzalloc(dev
, sizeof(struct pcm1789_private
),
244 pcm1789
->regmap
= regmap
;
246 dev_set_drvdata(dev
, pcm1789
);
248 pcm1789
->reset
= devm_gpiod_get_optional(dev
, "reset", GPIOD_OUT_HIGH
);
249 if (IS_ERR(pcm1789
->reset
))
250 return PTR_ERR(pcm1789
->reset
);
252 gpiod_set_value_cansleep(pcm1789
->reset
, 0);
255 INIT_WORK(&pcm1789
->work
, pcm1789_work_queue
);
257 return devm_snd_soc_register_component(dev
, &soc_component_dev_pcm1789
,
260 EXPORT_SYMBOL_GPL(pcm1789_common_init
);
262 int pcm1789_common_exit(struct device
*dev
)
264 struct pcm1789_private
*priv
= dev_get_drvdata(dev
);
266 flush_work(&priv
->work
);
270 EXPORT_SYMBOL_GPL(pcm1789_common_exit
);
272 MODULE_DESCRIPTION("ASoC PCM1789 driver");
273 MODULE_AUTHOR("Mylène Josserand <mylene.josserand@free-electrons.com>");
274 MODULE_LICENSE("GPL");