xref: /openbmc/linux/sound/soc/codecs/pcm3008.c (revision 98af278b)
1 /*
2  * ALSA Soc PCM3008 codec support
3  *
4  * Author:	Hugo Villeneuve
5  * Copyright (C) 2008 Lyrtech inc
6  *
7  * Based on AC97 Soc codec, original copyright follow:
8  * Copyright 2005 Wolfson Microelectronics PLC.
9  *
10  *  This program is free software; you can redistribute  it and/or modify it
11  *  under  the terms of  the GNU General  Public License as published by the
12  *  Free Software Foundation;  either version 2 of the  License, or (at your
13  *  option) any later version.
14  *
15  * Generic PCM3008 support.
16  */
17 
18 #include <linux/init.h>
19 #include <linux/kernel.h>
20 #include <linux/device.h>
21 #include <linux/gpio.h>
22 #include <linux/slab.h>
23 #include <linux/module.h>
24 #include <sound/core.h>
25 #include <sound/pcm.h>
26 #include <sound/initval.h>
27 #include <sound/soc.h>
28 
29 #include "pcm3008.h"
30 
31 static int pcm3008_dac_ev(struct snd_soc_dapm_widget *w,
32 			  struct snd_kcontrol *kcontrol,
33 			  int event)
34 {
35 	struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
36 	struct pcm3008_setup_data *setup = component->dev->platform_data;
37 
38 	gpio_set_value_cansleep(setup->pdda_pin,
39 				SND_SOC_DAPM_EVENT_ON(event));
40 
41 	return 0;
42 }
43 
44 static int pcm3008_adc_ev(struct snd_soc_dapm_widget *w,
45 			  struct snd_kcontrol *kcontrol,
46 			  int event)
47 {
48 	struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
49 	struct pcm3008_setup_data *setup = component->dev->platform_data;
50 
51 	gpio_set_value_cansleep(setup->pdad_pin,
52 				SND_SOC_DAPM_EVENT_ON(event));
53 
54 	return 0;
55 }
56 
57 static const struct snd_soc_dapm_widget pcm3008_dapm_widgets[] = {
58 SND_SOC_DAPM_INPUT("VINL"),
59 SND_SOC_DAPM_INPUT("VINR"),
60 
61 SND_SOC_DAPM_DAC_E("DAC", NULL, SND_SOC_NOPM, 0, 0, pcm3008_dac_ev,
62 		   SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
63 SND_SOC_DAPM_ADC_E("ADC", NULL, SND_SOC_NOPM, 0, 0, pcm3008_adc_ev,
64 		   SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
65 
66 SND_SOC_DAPM_OUTPUT("VOUTL"),
67 SND_SOC_DAPM_OUTPUT("VOUTR"),
68 };
69 
70 static const struct snd_soc_dapm_route pcm3008_dapm_routes[] = {
71 	{ "PCM3008 Capture", NULL, "ADC" },
72 	{ "ADC", NULL, "VINL" },
73 	{ "ADC", NULL, "VINR" },
74 
75 	{ "DAC", NULL, "PCM3008 Playback" },
76 	{ "VOUTL", NULL, "DAC" },
77 	{ "VOUTR", NULL, "DAC" },
78 };
79 
80 #define PCM3008_RATES (SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |	\
81 		       SNDRV_PCM_RATE_48000)
82 
83 static struct snd_soc_dai_driver pcm3008_dai = {
84 	.name = "pcm3008-hifi",
85 	.playback = {
86 		.stream_name = "PCM3008 Playback",
87 		.channels_min = 1,
88 		.channels_max = 2,
89 		.rates = PCM3008_RATES,
90 		.formats = SNDRV_PCM_FMTBIT_S16_LE,
91 	},
92 	.capture = {
93 		.stream_name = "PCM3008 Capture",
94 		.channels_min = 1,
95 		.channels_max = 2,
96 		.rates = PCM3008_RATES,
97 		.formats = SNDRV_PCM_FMTBIT_S16_LE,
98 	},
99 };
100 
101 static const struct snd_soc_component_driver soc_component_dev_pcm3008 = {
102 	.dapm_widgets		= pcm3008_dapm_widgets,
103 	.num_dapm_widgets	= ARRAY_SIZE(pcm3008_dapm_widgets),
104 	.dapm_routes		= pcm3008_dapm_routes,
105 	.num_dapm_routes	= ARRAY_SIZE(pcm3008_dapm_routes),
106 	.idle_bias_on		= 1,
107 	.use_pmdown_time	= 1,
108 	.endianness		= 1,
109 	.non_legacy_dai_naming	= 1,
110 };
111 
112 static int pcm3008_codec_probe(struct platform_device *pdev)
113 {
114 	struct pcm3008_setup_data *setup = pdev->dev.platform_data;
115 	int ret;
116 
117 	if (!setup)
118 		return -EINVAL;
119 
120 	/* DEM1  DEM0  DE-EMPHASIS_MODE
121 	 * Low   Low   De-emphasis 44.1 kHz ON
122 	 * Low   High  De-emphasis OFF
123 	 * High  Low   De-emphasis 48 kHz ON
124 	 * High  High  De-emphasis 32 kHz ON
125 	 */
126 
127 	/* Configure DEM0 GPIO (turning OFF DAC De-emphasis). */
128 	ret = devm_gpio_request_one(&pdev->dev, setup->dem0_pin,
129 				    GPIOF_OUT_INIT_HIGH, "codec_dem0");
130 	if (ret != 0)
131 		return ret;
132 
133 	/* Configure DEM1 GPIO (turning OFF DAC De-emphasis). */
134 	ret = devm_gpio_request_one(&pdev->dev, setup->dem1_pin,
135 				    GPIOF_OUT_INIT_LOW, "codec_dem1");
136 	if (ret != 0)
137 		return ret;
138 
139 	/* Configure PDAD GPIO. */
140 	ret = devm_gpio_request_one(&pdev->dev, setup->pdad_pin,
141 				    GPIOF_OUT_INIT_LOW, "codec_pdad");
142 	if (ret != 0)
143 		return ret;
144 
145 	/* Configure PDDA GPIO. */
146 	ret = devm_gpio_request_one(&pdev->dev, setup->pdda_pin,
147 				    GPIOF_OUT_INIT_LOW, "codec_pdda");
148 	if (ret != 0)
149 		return ret;
150 
151 	return devm_snd_soc_register_component(&pdev->dev,
152 			&soc_component_dev_pcm3008, &pcm3008_dai, 1);
153 }
154 
155 MODULE_ALIAS("platform:pcm3008-codec");
156 
157 static struct platform_driver pcm3008_codec_driver = {
158 	.probe		= pcm3008_codec_probe,
159 	.driver		= {
160 		.name	= "pcm3008-codec",
161 	},
162 };
163 
164 module_platform_driver(pcm3008_codec_driver);
165 
166 MODULE_DESCRIPTION("Soc PCM3008 driver");
167 MODULE_AUTHOR("Hugo Villeneuve");
168 MODULE_LICENSE("GPL");
169