0001
0002
0003
0004
0005
0006
0007
0008
0009 #include <linux/module.h>
0010 #include <linux/slab.h>
0011 #include <linux/of.h>
0012 #include <linux/of_gpio.h>
0013 #include <linux/of_device.h>
0014 #include <linux/regulator/consumer.h>
0015 #include <sound/soc.h>
0016 #include <sound/pcm.h>
0017 #include <sound/initval.h>
0018
0019 static const char * const supply_names[] = {
0020 "va", "vd"
0021 };
0022
0023 struct ak5386_priv {
0024 int reset_gpio;
0025 struct regulator_bulk_data supplies[ARRAY_SIZE(supply_names)];
0026 };
0027
0028 static const struct snd_soc_dapm_widget ak5386_dapm_widgets[] = {
0029 SND_SOC_DAPM_INPUT("AINL"),
0030 SND_SOC_DAPM_INPUT("AINR"),
0031 };
0032
0033 static const struct snd_soc_dapm_route ak5386_dapm_routes[] = {
0034 { "Capture", NULL, "AINL" },
0035 { "Capture", NULL, "AINR" },
0036 };
0037
0038 static int ak5386_soc_probe(struct snd_soc_component *component)
0039 {
0040 struct ak5386_priv *priv = snd_soc_component_get_drvdata(component);
0041 return regulator_bulk_enable(ARRAY_SIZE(priv->supplies), priv->supplies);
0042 }
0043
0044 static void ak5386_soc_remove(struct snd_soc_component *component)
0045 {
0046 struct ak5386_priv *priv = snd_soc_component_get_drvdata(component);
0047 regulator_bulk_disable(ARRAY_SIZE(priv->supplies), priv->supplies);
0048 }
0049
0050 #ifdef CONFIG_PM
0051 static int ak5386_soc_suspend(struct snd_soc_component *component)
0052 {
0053 struct ak5386_priv *priv = snd_soc_component_get_drvdata(component);
0054 regulator_bulk_disable(ARRAY_SIZE(priv->supplies), priv->supplies);
0055 return 0;
0056 }
0057
0058 static int ak5386_soc_resume(struct snd_soc_component *component)
0059 {
0060 struct ak5386_priv *priv = snd_soc_component_get_drvdata(component);
0061 return regulator_bulk_enable(ARRAY_SIZE(priv->supplies), priv->supplies);
0062 }
0063 #else
0064 #define ak5386_soc_suspend NULL
0065 #define ak5386_soc_resume NULL
0066 #endif
0067
0068 static const struct snd_soc_component_driver soc_component_ak5386 = {
0069 .probe = ak5386_soc_probe,
0070 .remove = ak5386_soc_remove,
0071 .suspend = ak5386_soc_suspend,
0072 .resume = ak5386_soc_resume,
0073 .dapm_widgets = ak5386_dapm_widgets,
0074 .num_dapm_widgets = ARRAY_SIZE(ak5386_dapm_widgets),
0075 .dapm_routes = ak5386_dapm_routes,
0076 .num_dapm_routes = ARRAY_SIZE(ak5386_dapm_routes),
0077 .idle_bias_on = 1,
0078 .use_pmdown_time = 1,
0079 .endianness = 1,
0080 };
0081
0082 static int ak5386_set_dai_fmt(struct snd_soc_dai *codec_dai,
0083 unsigned int format)
0084 {
0085 struct snd_soc_component *component = codec_dai->component;
0086
0087 format &= SND_SOC_DAIFMT_FORMAT_MASK;
0088 if (format != SND_SOC_DAIFMT_LEFT_J &&
0089 format != SND_SOC_DAIFMT_I2S) {
0090 dev_err(component->dev, "Invalid DAI format\n");
0091 return -EINVAL;
0092 }
0093
0094 return 0;
0095 }
0096
0097 static int ak5386_hw_params(struct snd_pcm_substream *substream,
0098 struct snd_pcm_hw_params *params,
0099 struct snd_soc_dai *dai)
0100 {
0101 struct snd_soc_component *component = dai->component;
0102 struct ak5386_priv *priv = snd_soc_component_get_drvdata(component);
0103
0104
0105
0106
0107
0108
0109
0110
0111
0112
0113
0114 if (gpio_is_valid(priv->reset_gpio))
0115 gpio_set_value(priv->reset_gpio, 1);
0116
0117 return 0;
0118 }
0119
0120 static int ak5386_hw_free(struct snd_pcm_substream *substream,
0121 struct snd_soc_dai *dai)
0122 {
0123 struct snd_soc_component *component = dai->component;
0124 struct ak5386_priv *priv = snd_soc_component_get_drvdata(component);
0125
0126 if (gpio_is_valid(priv->reset_gpio))
0127 gpio_set_value(priv->reset_gpio, 0);
0128
0129 return 0;
0130 }
0131
0132 static const struct snd_soc_dai_ops ak5386_dai_ops = {
0133 .set_fmt = ak5386_set_dai_fmt,
0134 .hw_params = ak5386_hw_params,
0135 .hw_free = ak5386_hw_free,
0136 };
0137
0138 static struct snd_soc_dai_driver ak5386_dai = {
0139 .name = "ak5386-hifi",
0140 .capture = {
0141 .stream_name = "Capture",
0142 .channels_min = 1,
0143 .channels_max = 2,
0144 .rates = SNDRV_PCM_RATE_8000_192000,
0145 .formats = SNDRV_PCM_FMTBIT_S8 |
0146 SNDRV_PCM_FMTBIT_S16_LE |
0147 SNDRV_PCM_FMTBIT_S24_LE |
0148 SNDRV_PCM_FMTBIT_S24_3LE,
0149 },
0150 .ops = &ak5386_dai_ops,
0151 };
0152
0153 #ifdef CONFIG_OF
0154 static const struct of_device_id ak5386_dt_ids[] = {
0155 { .compatible = "asahi-kasei,ak5386", },
0156 { }
0157 };
0158 MODULE_DEVICE_TABLE(of, ak5386_dt_ids);
0159 #endif
0160
0161 static int ak5386_probe(struct platform_device *pdev)
0162 {
0163 struct device *dev = &pdev->dev;
0164 struct ak5386_priv *priv;
0165 int ret, i;
0166
0167 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
0168 if (!priv)
0169 return -ENOMEM;
0170
0171 priv->reset_gpio = -EINVAL;
0172 dev_set_drvdata(dev, priv);
0173
0174 for (i = 0; i < ARRAY_SIZE(supply_names); i++)
0175 priv->supplies[i].supply = supply_names[i];
0176
0177 ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(priv->supplies),
0178 priv->supplies);
0179 if (ret < 0)
0180 return ret;
0181
0182 if (of_match_device(of_match_ptr(ak5386_dt_ids), dev))
0183 priv->reset_gpio = of_get_named_gpio(dev->of_node,
0184 "reset-gpio", 0);
0185
0186 if (gpio_is_valid(priv->reset_gpio))
0187 if (devm_gpio_request_one(dev, priv->reset_gpio,
0188 GPIOF_OUT_INIT_LOW,
0189 "AK5386 Reset"))
0190 priv->reset_gpio = -EINVAL;
0191
0192 return devm_snd_soc_register_component(dev, &soc_component_ak5386,
0193 &ak5386_dai, 1);
0194 }
0195
0196 static struct platform_driver ak5386_driver = {
0197 .probe = ak5386_probe,
0198 .driver = {
0199 .name = "ak5386",
0200 .of_match_table = of_match_ptr(ak5386_dt_ids),
0201 },
0202 };
0203
0204 module_platform_driver(ak5386_driver);
0205
0206 MODULE_DESCRIPTION("ASoC driver for AK5386 ADC");
0207 MODULE_AUTHOR("Daniel Mack <zonque@gmail.com>");
0208 MODULE_LICENSE("GPL");