blob: c76bfff246028e161afb517175f3d24ac2760a56 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Daniel Mackcc289be2013-03-08 12:07:28 +01002/*
3 * ALSA SoC driver for
4 * Asahi Kasei AK5386 Single-ended 24-Bit 192kHz delta-sigma ADC
5 *
6 * (c) 2013 Daniel Mack <zonque@gmail.com>
Daniel Mackcc289be2013-03-08 12:07:28 +01007 */
8
9#include <linux/module.h>
10#include <linux/slab.h>
11#include <linux/of.h>
12#include <linux/of_gpio.h>
13#include <linux/of_device.h>
Daniel Mackfb668e72014-03-27 21:42:14 +010014#include <linux/regulator/consumer.h>
Daniel Mackcc289be2013-03-08 12:07:28 +010015#include <sound/soc.h>
16#include <sound/pcm.h>
17#include <sound/initval.h>
18
Daniel Mack2ad76542014-03-28 19:05:04 +010019static const char * const supply_names[] = {
Daniel Mackfb668e72014-03-27 21:42:14 +010020 "va", "vd"
21};
22
Daniel Mackcc289be2013-03-08 12:07:28 +010023struct ak5386_priv {
24 int reset_gpio;
Daniel Mackfb668e72014-03-27 21:42:14 +010025 struct regulator_bulk_data supplies[ARRAY_SIZE(supply_names)];
Daniel Mackcc289be2013-03-08 12:07:28 +010026};
27
Mark Browndcf14392013-08-11 12:28:56 +010028static const struct snd_soc_dapm_widget ak5386_dapm_widgets[] = {
29SND_SOC_DAPM_INPUT("AINL"),
30SND_SOC_DAPM_INPUT("AINR"),
31};
32
33static const struct snd_soc_dapm_route ak5386_dapm_routes[] = {
34 { "Capture", NULL, "AINL" },
35 { "Capture", NULL, "AINR" },
36};
37
Kuninori Morimoto12132de2018-01-29 03:13:23 +000038static int ak5386_soc_probe(struct snd_soc_component *component)
Daniel Mackfb668e72014-03-27 21:42:14 +010039{
Kuninori Morimoto12132de2018-01-29 03:13:23 +000040 struct ak5386_priv *priv = snd_soc_component_get_drvdata(component);
Daniel Mackfb668e72014-03-27 21:42:14 +010041 return regulator_bulk_enable(ARRAY_SIZE(priv->supplies), priv->supplies);
42}
43
Kuninori Morimoto12132de2018-01-29 03:13:23 +000044static void ak5386_soc_remove(struct snd_soc_component *component)
Daniel Mackfb668e72014-03-27 21:42:14 +010045{
Kuninori Morimoto12132de2018-01-29 03:13:23 +000046 struct ak5386_priv *priv = snd_soc_component_get_drvdata(component);
Daniel Mackfb668e72014-03-27 21:42:14 +010047 regulator_bulk_disable(ARRAY_SIZE(priv->supplies), priv->supplies);
Daniel Mackfb668e72014-03-27 21:42:14 +010048}
49
50#ifdef CONFIG_PM
Kuninori Morimoto12132de2018-01-29 03:13:23 +000051static int ak5386_soc_suspend(struct snd_soc_component *component)
Daniel Mackfb668e72014-03-27 21:42:14 +010052{
Kuninori Morimoto12132de2018-01-29 03:13:23 +000053 struct ak5386_priv *priv = snd_soc_component_get_drvdata(component);
Daniel Mackfb668e72014-03-27 21:42:14 +010054 regulator_bulk_disable(ARRAY_SIZE(priv->supplies), priv->supplies);
55 return 0;
56}
57
Kuninori Morimoto12132de2018-01-29 03:13:23 +000058static int ak5386_soc_resume(struct snd_soc_component *component)
Daniel Mackfb668e72014-03-27 21:42:14 +010059{
Kuninori Morimoto12132de2018-01-29 03:13:23 +000060 struct ak5386_priv *priv = snd_soc_component_get_drvdata(component);
Daniel Mackfb668e72014-03-27 21:42:14 +010061 return regulator_bulk_enable(ARRAY_SIZE(priv->supplies), priv->supplies);
62}
63#else
64#define ak5386_soc_suspend NULL
65#define ak5386_soc_resume NULL
66#endif /* CONFIG_PM */
67
Kuninori Morimoto12132de2018-01-29 03:13:23 +000068static const struct snd_soc_component_driver soc_component_ak5386 = {
69 .probe = ak5386_soc_probe,
70 .remove = ak5386_soc_remove,
71 .suspend = ak5386_soc_suspend,
72 .resume = ak5386_soc_resume,
73 .dapm_widgets = ak5386_dapm_widgets,
74 .num_dapm_widgets = ARRAY_SIZE(ak5386_dapm_widgets),
75 .dapm_routes = ak5386_dapm_routes,
76 .num_dapm_routes = ARRAY_SIZE(ak5386_dapm_routes),
77 .idle_bias_on = 1,
78 .use_pmdown_time = 1,
79 .endianness = 1,
80 .non_legacy_dai_naming = 1,
Mark Browndcf14392013-08-11 12:28:56 +010081};
Daniel Mackcc289be2013-03-08 12:07:28 +010082
83static int ak5386_set_dai_fmt(struct snd_soc_dai *codec_dai,
84 unsigned int format)
85{
Kuninori Morimoto12132de2018-01-29 03:13:23 +000086 struct snd_soc_component *component = codec_dai->component;
Daniel Mackcc289be2013-03-08 12:07:28 +010087
88 format &= SND_SOC_DAIFMT_FORMAT_MASK;
89 if (format != SND_SOC_DAIFMT_LEFT_J &&
90 format != SND_SOC_DAIFMT_I2S) {
Kuninori Morimoto12132de2018-01-29 03:13:23 +000091 dev_err(component->dev, "Invalid DAI format\n");
Daniel Mackcc289be2013-03-08 12:07:28 +010092 return -EINVAL;
93 }
94
95 return 0;
96}
97
98static int ak5386_hw_params(struct snd_pcm_substream *substream,
99 struct snd_pcm_hw_params *params,
100 struct snd_soc_dai *dai)
101{
Kuninori Morimoto12132de2018-01-29 03:13:23 +0000102 struct snd_soc_component *component = dai->component;
103 struct ak5386_priv *priv = snd_soc_component_get_drvdata(component);
Daniel Mackcc289be2013-03-08 12:07:28 +0100104
105 /*
106 * From the datasheet:
107 *
108 * All external clocks (MCLK, SCLK and LRCK) must be present unless
109 * PDN pin = ā€œLā€. If these clocks are not provided, the AK5386 may
110 * draw excess current due to its use of internal dynamically
111 * refreshed logic. If the external clocks are not present, place
112 * the AK5386 in power-down mode (PDN pin = ā€œLā€).
113 */
114
115 if (gpio_is_valid(priv->reset_gpio))
116 gpio_set_value(priv->reset_gpio, 1);
117
118 return 0;
119}
120
121static int ak5386_hw_free(struct snd_pcm_substream *substream,
122 struct snd_soc_dai *dai)
123{
Kuninori Morimoto12132de2018-01-29 03:13:23 +0000124 struct snd_soc_component *component = dai->component;
125 struct ak5386_priv *priv = snd_soc_component_get_drvdata(component);
Daniel Mackcc289be2013-03-08 12:07:28 +0100126
127 if (gpio_is_valid(priv->reset_gpio))
128 gpio_set_value(priv->reset_gpio, 0);
129
130 return 0;
131}
132
133static const struct snd_soc_dai_ops ak5386_dai_ops = {
134 .set_fmt = ak5386_set_dai_fmt,
135 .hw_params = ak5386_hw_params,
136 .hw_free = ak5386_hw_free,
137};
138
139static struct snd_soc_dai_driver ak5386_dai = {
140 .name = "ak5386-hifi",
141 .capture = {
142 .stream_name = "Capture",
143 .channels_min = 1,
144 .channels_max = 2,
145 .rates = SNDRV_PCM_RATE_8000_192000,
146 .formats = SNDRV_PCM_FMTBIT_S8 |
147 SNDRV_PCM_FMTBIT_S16_LE |
148 SNDRV_PCM_FMTBIT_S24_LE |
149 SNDRV_PCM_FMTBIT_S24_3LE,
150 },
151 .ops = &ak5386_dai_ops,
152};
153
154#ifdef CONFIG_OF
155static const struct of_device_id ak5386_dt_ids[] = {
156 { .compatible = "asahi-kasei,ak5386", },
157 { }
158};
159MODULE_DEVICE_TABLE(of, ak5386_dt_ids);
160#endif
161
162static int ak5386_probe(struct platform_device *pdev)
163{
164 struct device *dev = &pdev->dev;
165 struct ak5386_priv *priv;
Daniel Mackfb668e72014-03-27 21:42:14 +0100166 int ret, i;
Daniel Mackcc289be2013-03-08 12:07:28 +0100167
168 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
169 if (!priv)
170 return -ENOMEM;
171
172 priv->reset_gpio = -EINVAL;
173 dev_set_drvdata(dev, priv);
174
Daniel Mackfb668e72014-03-27 21:42:14 +0100175 for (i = 0; i < ARRAY_SIZE(supply_names); i++)
176 priv->supplies[i].supply = supply_names[i];
177
178 ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(priv->supplies),
179 priv->supplies);
180 if (ret < 0)
181 return ret;
182
Daniel Mackcc289be2013-03-08 12:07:28 +0100183 if (of_match_device(of_match_ptr(ak5386_dt_ids), dev))
184 priv->reset_gpio = of_get_named_gpio(dev->of_node,
185 "reset-gpio", 0);
186
187 if (gpio_is_valid(priv->reset_gpio))
188 if (devm_gpio_request_one(dev, priv->reset_gpio,
189 GPIOF_OUT_INIT_LOW,
190 "AK5386 Reset"))
191 priv->reset_gpio = -EINVAL;
192
Kuninori Morimoto12132de2018-01-29 03:13:23 +0000193 return devm_snd_soc_register_component(dev, &soc_component_ak5386,
Daniel Mackcc289be2013-03-08 12:07:28 +0100194 &ak5386_dai, 1);
195}
196
Daniel Mackcc289be2013-03-08 12:07:28 +0100197static struct platform_driver ak5386_driver = {
198 .probe = ak5386_probe,
Daniel Mackcc289be2013-03-08 12:07:28 +0100199 .driver = {
200 .name = "ak5386",
Daniel Mackcc289be2013-03-08 12:07:28 +0100201 .of_match_table = of_match_ptr(ak5386_dt_ids),
202 },
203};
204
205module_platform_driver(ak5386_driver);
206
207MODULE_DESCRIPTION("ASoC driver for AK5386 ADC");
208MODULE_AUTHOR("Daniel Mack <zonque@gmail.com>");
209MODULE_LICENSE("GPL");