xref: /openbmc/linux/sound/soc/codecs/ak5386.c (revision 4f6cce39)
1 /*
2  * ALSA SoC driver for
3  *    Asahi Kasei AK5386 Single-ended 24-Bit 192kHz delta-sigma ADC
4  *
5  * (c) 2013 Daniel Mack <zonque@gmail.com>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation.
10  */
11 
12 #include <linux/module.h>
13 #include <linux/slab.h>
14 #include <linux/of.h>
15 #include <linux/of_gpio.h>
16 #include <linux/of_device.h>
17 #include <linux/regulator/consumer.h>
18 #include <sound/soc.h>
19 #include <sound/pcm.h>
20 #include <sound/initval.h>
21 
22 static const char * const supply_names[] = {
23 	"va", "vd"
24 };
25 
26 struct ak5386_priv {
27 	int reset_gpio;
28 	struct regulator_bulk_data supplies[ARRAY_SIZE(supply_names)];
29 };
30 
31 static const struct snd_soc_dapm_widget ak5386_dapm_widgets[] = {
32 SND_SOC_DAPM_INPUT("AINL"),
33 SND_SOC_DAPM_INPUT("AINR"),
34 };
35 
36 static const struct snd_soc_dapm_route ak5386_dapm_routes[] = {
37 	{ "Capture", NULL, "AINL" },
38 	{ "Capture", NULL, "AINR" },
39 };
40 
41 static int ak5386_soc_probe(struct snd_soc_codec *codec)
42 {
43 	struct ak5386_priv *priv = snd_soc_codec_get_drvdata(codec);
44 	return regulator_bulk_enable(ARRAY_SIZE(priv->supplies), priv->supplies);
45 }
46 
47 static int ak5386_soc_remove(struct snd_soc_codec *codec)
48 {
49 	struct ak5386_priv *priv = snd_soc_codec_get_drvdata(codec);
50 	regulator_bulk_disable(ARRAY_SIZE(priv->supplies), priv->supplies);
51 	return 0;
52 }
53 
54 #ifdef CONFIG_PM
55 static int ak5386_soc_suspend(struct snd_soc_codec *codec)
56 {
57 	struct ak5386_priv *priv = snd_soc_codec_get_drvdata(codec);
58 	regulator_bulk_disable(ARRAY_SIZE(priv->supplies), priv->supplies);
59 	return 0;
60 }
61 
62 static int ak5386_soc_resume(struct snd_soc_codec *codec)
63 {
64 	struct ak5386_priv *priv = snd_soc_codec_get_drvdata(codec);
65 	return regulator_bulk_enable(ARRAY_SIZE(priv->supplies), priv->supplies);
66 }
67 #else
68 #define ak5386_soc_suspend	NULL
69 #define ak5386_soc_resume	NULL
70 #endif /* CONFIG_PM */
71 
72 static struct snd_soc_codec_driver soc_codec_ak5386 = {
73 	.probe = ak5386_soc_probe,
74 	.remove = ak5386_soc_remove,
75 	.suspend = ak5386_soc_suspend,
76 	.resume = ak5386_soc_resume,
77 	.component_driver = {
78 		.dapm_widgets		= ak5386_dapm_widgets,
79 		.num_dapm_widgets	= ARRAY_SIZE(ak5386_dapm_widgets),
80 		.dapm_routes		= ak5386_dapm_routes,
81 		.num_dapm_routes	= ARRAY_SIZE(ak5386_dapm_routes),
82 	},
83 };
84 
85 static int ak5386_set_dai_fmt(struct snd_soc_dai *codec_dai,
86 			      unsigned int format)
87 {
88 	struct snd_soc_codec *codec = codec_dai->codec;
89 
90 	format &= SND_SOC_DAIFMT_FORMAT_MASK;
91 	if (format != SND_SOC_DAIFMT_LEFT_J &&
92 	    format != SND_SOC_DAIFMT_I2S) {
93 		dev_err(codec->dev, "Invalid DAI format\n");
94 		return -EINVAL;
95 	}
96 
97 	return 0;
98 }
99 
100 static int ak5386_hw_params(struct snd_pcm_substream *substream,
101 			    struct snd_pcm_hw_params *params,
102 			    struct snd_soc_dai *dai)
103 {
104 	struct snd_soc_codec *codec = dai->codec;
105 	struct ak5386_priv *priv = snd_soc_codec_get_drvdata(codec);
106 
107 	/*
108 	 * From the datasheet:
109 	 *
110 	 * All external clocks (MCLK, SCLK and LRCK) must be present unless
111 	 * PDN pin = “L”. If these clocks are not provided, the AK5386 may
112 	 * draw excess current due to its use of internal dynamically
113 	 * refreshed logic. If the external clocks are not present, place
114 	 * the AK5386 in power-down mode (PDN pin = “L”).
115 	 */
116 
117 	if (gpio_is_valid(priv->reset_gpio))
118 		gpio_set_value(priv->reset_gpio, 1);
119 
120 	return 0;
121 }
122 
123 static int ak5386_hw_free(struct snd_pcm_substream *substream,
124 			  struct snd_soc_dai *dai)
125 {
126 	struct snd_soc_codec *codec = dai->codec;
127 	struct ak5386_priv *priv = snd_soc_codec_get_drvdata(codec);
128 
129 	if (gpio_is_valid(priv->reset_gpio))
130 		gpio_set_value(priv->reset_gpio, 0);
131 
132 	return 0;
133 }
134 
135 static const struct snd_soc_dai_ops ak5386_dai_ops = {
136 	.set_fmt	= ak5386_set_dai_fmt,
137 	.hw_params	= ak5386_hw_params,
138 	.hw_free	= ak5386_hw_free,
139 };
140 
141 static struct snd_soc_dai_driver ak5386_dai = {
142 	.name		= "ak5386-hifi",
143 	.capture	= {
144 		.stream_name	= "Capture",
145 		.channels_min	= 1,
146 		.channels_max	= 2,
147 		.rates		= SNDRV_PCM_RATE_8000_192000,
148 		.formats	= SNDRV_PCM_FMTBIT_S8     |
149 				  SNDRV_PCM_FMTBIT_S16_LE |
150 				  SNDRV_PCM_FMTBIT_S24_LE |
151 				  SNDRV_PCM_FMTBIT_S24_3LE,
152 	},
153 	.ops	= &ak5386_dai_ops,
154 };
155 
156 #ifdef CONFIG_OF
157 static const struct of_device_id ak5386_dt_ids[] = {
158 	{ .compatible = "asahi-kasei,ak5386", },
159 	{ }
160 };
161 MODULE_DEVICE_TABLE(of, ak5386_dt_ids);
162 #endif
163 
164 static int ak5386_probe(struct platform_device *pdev)
165 {
166 	struct device *dev = &pdev->dev;
167 	struct ak5386_priv *priv;
168 	int ret, i;
169 
170 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
171 	if (!priv)
172 		return -ENOMEM;
173 
174 	priv->reset_gpio = -EINVAL;
175 	dev_set_drvdata(dev, priv);
176 
177 	for (i = 0; i < ARRAY_SIZE(supply_names); i++)
178 		priv->supplies[i].supply = supply_names[i];
179 
180 	ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(priv->supplies),
181 				      priv->supplies);
182 	if (ret < 0)
183 		return ret;
184 
185 	if (of_match_device(of_match_ptr(ak5386_dt_ids), dev))
186 		priv->reset_gpio = of_get_named_gpio(dev->of_node,
187 						      "reset-gpio", 0);
188 
189 	if (gpio_is_valid(priv->reset_gpio))
190 		if (devm_gpio_request_one(dev, priv->reset_gpio,
191 					  GPIOF_OUT_INIT_LOW,
192 					  "AK5386 Reset"))
193 			priv->reset_gpio = -EINVAL;
194 
195 	return snd_soc_register_codec(dev, &soc_codec_ak5386,
196 				      &ak5386_dai, 1);
197 }
198 
199 static int ak5386_remove(struct platform_device *pdev)
200 {
201 	snd_soc_unregister_codec(&pdev->dev);
202 	return 0;
203 }
204 
205 static struct platform_driver ak5386_driver = {
206 	.probe		= ak5386_probe,
207 	.remove		= ak5386_remove,
208 	.driver		= {
209 		.name	= "ak5386",
210 		.of_match_table = of_match_ptr(ak5386_dt_ids),
211 	},
212 };
213 
214 module_platform_driver(ak5386_driver);
215 
216 MODULE_DESCRIPTION("ASoC driver for AK5386 ADC");
217 MODULE_AUTHOR("Daniel Mack <zonque@gmail.com>");
218 MODULE_LICENSE("GPL");
219