blob: d212960b4ddaa07f9f266848f48741192604e453 [file] [log] [blame]
Daniel Mackcc289be2013-03-08 12:07:28 +01001/*
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>
Daniel Mackfb668e72014-03-27 21:42:14 +010017#include <linux/regulator/consumer.h>
Daniel Mackcc289be2013-03-08 12:07:28 +010018#include <sound/soc.h>
19#include <sound/pcm.h>
20#include <sound/initval.h>
21
Daniel Mack2ad76542014-03-28 19:05:04 +010022static const char * const supply_names[] = {
Daniel Mackfb668e72014-03-27 21:42:14 +010023 "va", "vd"
24};
25
Daniel Mackcc289be2013-03-08 12:07:28 +010026struct ak5386_priv {
27 int reset_gpio;
Daniel Mackfb668e72014-03-27 21:42:14 +010028 struct regulator_bulk_data supplies[ARRAY_SIZE(supply_names)];
Daniel Mackcc289be2013-03-08 12:07:28 +010029};
30
Mark Browndcf14392013-08-11 12:28:56 +010031static const struct snd_soc_dapm_widget ak5386_dapm_widgets[] = {
32SND_SOC_DAPM_INPUT("AINL"),
33SND_SOC_DAPM_INPUT("AINR"),
34};
35
36static const struct snd_soc_dapm_route ak5386_dapm_routes[] = {
37 { "Capture", NULL, "AINL" },
38 { "Capture", NULL, "AINR" },
39};
40
Kuninori Morimoto12132de2018-01-29 03:13:23 +000041static int ak5386_soc_probe(struct snd_soc_component *component)
Daniel Mackfb668e72014-03-27 21:42:14 +010042{
Kuninori Morimoto12132de2018-01-29 03:13:23 +000043 struct ak5386_priv *priv = snd_soc_component_get_drvdata(component);
Daniel Mackfb668e72014-03-27 21:42:14 +010044 return regulator_bulk_enable(ARRAY_SIZE(priv->supplies), priv->supplies);
45}
46
Kuninori Morimoto12132de2018-01-29 03:13:23 +000047static void ak5386_soc_remove(struct snd_soc_component *component)
Daniel Mackfb668e72014-03-27 21:42:14 +010048{
Kuninori Morimoto12132de2018-01-29 03:13:23 +000049 struct ak5386_priv *priv = snd_soc_component_get_drvdata(component);
Daniel Mackfb668e72014-03-27 21:42:14 +010050 regulator_bulk_disable(ARRAY_SIZE(priv->supplies), priv->supplies);
Daniel Mackfb668e72014-03-27 21:42:14 +010051}
52
53#ifdef CONFIG_PM
Kuninori Morimoto12132de2018-01-29 03:13:23 +000054static int ak5386_soc_suspend(struct snd_soc_component *component)
Daniel Mackfb668e72014-03-27 21:42:14 +010055{
Kuninori Morimoto12132de2018-01-29 03:13:23 +000056 struct ak5386_priv *priv = snd_soc_component_get_drvdata(component);
Daniel Mackfb668e72014-03-27 21:42:14 +010057 regulator_bulk_disable(ARRAY_SIZE(priv->supplies), priv->supplies);
58 return 0;
59}
60
Kuninori Morimoto12132de2018-01-29 03:13:23 +000061static int ak5386_soc_resume(struct snd_soc_component *component)
Daniel Mackfb668e72014-03-27 21:42:14 +010062{
Kuninori Morimoto12132de2018-01-29 03:13:23 +000063 struct ak5386_priv *priv = snd_soc_component_get_drvdata(component);
Daniel Mackfb668e72014-03-27 21:42:14 +010064 return regulator_bulk_enable(ARRAY_SIZE(priv->supplies), priv->supplies);
65}
66#else
67#define ak5386_soc_suspend NULL
68#define ak5386_soc_resume NULL
69#endif /* CONFIG_PM */
70
Kuninori Morimoto12132de2018-01-29 03:13:23 +000071static const struct snd_soc_component_driver soc_component_ak5386 = {
72 .probe = ak5386_soc_probe,
73 .remove = ak5386_soc_remove,
74 .suspend = ak5386_soc_suspend,
75 .resume = ak5386_soc_resume,
76 .dapm_widgets = ak5386_dapm_widgets,
77 .num_dapm_widgets = ARRAY_SIZE(ak5386_dapm_widgets),
78 .dapm_routes = ak5386_dapm_routes,
79 .num_dapm_routes = ARRAY_SIZE(ak5386_dapm_routes),
80 .idle_bias_on = 1,
81 .use_pmdown_time = 1,
82 .endianness = 1,
83 .non_legacy_dai_naming = 1,
Mark Browndcf14392013-08-11 12:28:56 +010084};
Daniel Mackcc289be2013-03-08 12:07:28 +010085
86static int ak5386_set_dai_fmt(struct snd_soc_dai *codec_dai,
87 unsigned int format)
88{
Kuninori Morimoto12132de2018-01-29 03:13:23 +000089 struct snd_soc_component *component = codec_dai->component;
Daniel Mackcc289be2013-03-08 12:07:28 +010090
91 format &= SND_SOC_DAIFMT_FORMAT_MASK;
92 if (format != SND_SOC_DAIFMT_LEFT_J &&
93 format != SND_SOC_DAIFMT_I2S) {
Kuninori Morimoto12132de2018-01-29 03:13:23 +000094 dev_err(component->dev, "Invalid DAI format\n");
Daniel Mackcc289be2013-03-08 12:07:28 +010095 return -EINVAL;
96 }
97
98 return 0;
99}
100
101static int ak5386_hw_params(struct snd_pcm_substream *substream,
102 struct snd_pcm_hw_params *params,
103 struct snd_soc_dai *dai)
104{
Kuninori Morimoto12132de2018-01-29 03:13:23 +0000105 struct snd_soc_component *component = dai->component;
106 struct ak5386_priv *priv = snd_soc_component_get_drvdata(component);
Daniel Mackcc289be2013-03-08 12:07:28 +0100107
108 /*
109 * From the datasheet:
110 *
111 * All external clocks (MCLK, SCLK and LRCK) must be present unless
112 * PDN pin = ā€œLā€. If these clocks are not provided, the AK5386 may
113 * draw excess current due to its use of internal dynamically
114 * refreshed logic. If the external clocks are not present, place
115 * the AK5386 in power-down mode (PDN pin = ā€œLā€).
116 */
117
118 if (gpio_is_valid(priv->reset_gpio))
119 gpio_set_value(priv->reset_gpio, 1);
120
121 return 0;
122}
123
124static int ak5386_hw_free(struct snd_pcm_substream *substream,
125 struct snd_soc_dai *dai)
126{
Kuninori Morimoto12132de2018-01-29 03:13:23 +0000127 struct snd_soc_component *component = dai->component;
128 struct ak5386_priv *priv = snd_soc_component_get_drvdata(component);
Daniel Mackcc289be2013-03-08 12:07:28 +0100129
130 if (gpio_is_valid(priv->reset_gpio))
131 gpio_set_value(priv->reset_gpio, 0);
132
133 return 0;
134}
135
136static const struct snd_soc_dai_ops ak5386_dai_ops = {
137 .set_fmt = ak5386_set_dai_fmt,
138 .hw_params = ak5386_hw_params,
139 .hw_free = ak5386_hw_free,
140};
141
142static struct snd_soc_dai_driver ak5386_dai = {
143 .name = "ak5386-hifi",
144 .capture = {
145 .stream_name = "Capture",
146 .channels_min = 1,
147 .channels_max = 2,
148 .rates = SNDRV_PCM_RATE_8000_192000,
149 .formats = SNDRV_PCM_FMTBIT_S8 |
150 SNDRV_PCM_FMTBIT_S16_LE |
151 SNDRV_PCM_FMTBIT_S24_LE |
152 SNDRV_PCM_FMTBIT_S24_3LE,
153 },
154 .ops = &ak5386_dai_ops,
155};
156
157#ifdef CONFIG_OF
158static const struct of_device_id ak5386_dt_ids[] = {
159 { .compatible = "asahi-kasei,ak5386", },
160 { }
161};
162MODULE_DEVICE_TABLE(of, ak5386_dt_ids);
163#endif
164
165static int ak5386_probe(struct platform_device *pdev)
166{
167 struct device *dev = &pdev->dev;
168 struct ak5386_priv *priv;
Daniel Mackfb668e72014-03-27 21:42:14 +0100169 int ret, i;
Daniel Mackcc289be2013-03-08 12:07:28 +0100170
171 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
172 if (!priv)
173 return -ENOMEM;
174
175 priv->reset_gpio = -EINVAL;
176 dev_set_drvdata(dev, priv);
177
Daniel Mackfb668e72014-03-27 21:42:14 +0100178 for (i = 0; i < ARRAY_SIZE(supply_names); i++)
179 priv->supplies[i].supply = supply_names[i];
180
181 ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(priv->supplies),
182 priv->supplies);
183 if (ret < 0)
184 return ret;
185
Daniel Mackcc289be2013-03-08 12:07:28 +0100186 if (of_match_device(of_match_ptr(ak5386_dt_ids), dev))
187 priv->reset_gpio = of_get_named_gpio(dev->of_node,
188 "reset-gpio", 0);
189
190 if (gpio_is_valid(priv->reset_gpio))
191 if (devm_gpio_request_one(dev, priv->reset_gpio,
192 GPIOF_OUT_INIT_LOW,
193 "AK5386 Reset"))
194 priv->reset_gpio = -EINVAL;
195
Kuninori Morimoto12132de2018-01-29 03:13:23 +0000196 return devm_snd_soc_register_component(dev, &soc_component_ak5386,
Daniel Mackcc289be2013-03-08 12:07:28 +0100197 &ak5386_dai, 1);
198}
199
Daniel Mackcc289be2013-03-08 12:07:28 +0100200static struct platform_driver ak5386_driver = {
201 .probe = ak5386_probe,
Daniel Mackcc289be2013-03-08 12:07:28 +0100202 .driver = {
203 .name = "ak5386",
Daniel Mackcc289be2013-03-08 12:07:28 +0100204 .of_match_table = of_match_ptr(ak5386_dt_ids),
205 },
206};
207
208module_platform_driver(ak5386_driver);
209
210MODULE_DESCRIPTION("ASoC driver for AK5386 ADC");
211MODULE_AUTHOR("Daniel Mack <zonque@gmail.com>");
212MODULE_LICENSE("GPL");