1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 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> 7 */ 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> 14 #include <linux/regulator/consumer.h> 15 #include <sound/soc.h> 16 #include <sound/pcm.h> 17 #include <sound/initval.h> 18 19 static const char * const supply_names[] = { 20 "va", "vd" 21 }; 22 23 struct ak5386_priv { 24 int reset_gpio; 25 struct regulator_bulk_data supplies[ARRAY_SIZE(supply_names)]; 26 }; 27 28 static const struct snd_soc_dapm_widget ak5386_dapm_widgets[] = { 29 SND_SOC_DAPM_INPUT("AINL"), 30 SND_SOC_DAPM_INPUT("AINR"), 31 }; 32 33 static const struct snd_soc_dapm_route ak5386_dapm_routes[] = { 34 { "Capture", NULL, "AINL" }, 35 { "Capture", NULL, "AINR" }, 36 }; 37 38 static int ak5386_soc_probe(struct snd_soc_component *component) 39 { 40 struct ak5386_priv *priv = snd_soc_component_get_drvdata(component); 41 return regulator_bulk_enable(ARRAY_SIZE(priv->supplies), priv->supplies); 42 } 43 44 static void ak5386_soc_remove(struct snd_soc_component *component) 45 { 46 struct ak5386_priv *priv = snd_soc_component_get_drvdata(component); 47 regulator_bulk_disable(ARRAY_SIZE(priv->supplies), priv->supplies); 48 } 49 50 #ifdef CONFIG_PM 51 static int ak5386_soc_suspend(struct snd_soc_component *component) 52 { 53 struct ak5386_priv *priv = snd_soc_component_get_drvdata(component); 54 regulator_bulk_disable(ARRAY_SIZE(priv->supplies), priv->supplies); 55 return 0; 56 } 57 58 static int ak5386_soc_resume(struct snd_soc_component *component) 59 { 60 struct ak5386_priv *priv = snd_soc_component_get_drvdata(component); 61 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 68 static 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 }; 81 82 static int ak5386_set_dai_fmt(struct snd_soc_dai *codec_dai, 83 unsigned int format) 84 { 85 struct snd_soc_component *component = codec_dai->component; 86 87 format &= SND_SOC_DAIFMT_FORMAT_MASK; 88 if (format != SND_SOC_DAIFMT_LEFT_J && 89 format != SND_SOC_DAIFMT_I2S) { 90 dev_err(component->dev, "Invalid DAI format\n"); 91 return -EINVAL; 92 } 93 94 return 0; 95 } 96 97 static int ak5386_hw_params(struct snd_pcm_substream *substream, 98 struct snd_pcm_hw_params *params, 99 struct snd_soc_dai *dai) 100 { 101 struct snd_soc_component *component = dai->component; 102 struct ak5386_priv *priv = snd_soc_component_get_drvdata(component); 103 104 /* 105 * From the datasheet: 106 * 107 * All external clocks (MCLK, SCLK and LRCK) must be present unless 108 * PDN pin = “L”. If these clocks are not provided, the AK5386 may 109 * draw excess current due to its use of internal dynamically 110 * refreshed logic. If the external clocks are not present, place 111 * the AK5386 in power-down mode (PDN pin = “L”). 112 */ 113 114 if (gpio_is_valid(priv->reset_gpio)) 115 gpio_set_value(priv->reset_gpio, 1); 116 117 return 0; 118 } 119 120 static int ak5386_hw_free(struct snd_pcm_substream *substream, 121 struct snd_soc_dai *dai) 122 { 123 struct snd_soc_component *component = dai->component; 124 struct ak5386_priv *priv = snd_soc_component_get_drvdata(component); 125 126 if (gpio_is_valid(priv->reset_gpio)) 127 gpio_set_value(priv->reset_gpio, 0); 128 129 return 0; 130 } 131 132 static const struct snd_soc_dai_ops ak5386_dai_ops = { 133 .set_fmt = ak5386_set_dai_fmt, 134 .hw_params = ak5386_hw_params, 135 .hw_free = ak5386_hw_free, 136 }; 137 138 static struct snd_soc_dai_driver ak5386_dai = { 139 .name = "ak5386-hifi", 140 .capture = { 141 .stream_name = "Capture", 142 .channels_min = 1, 143 .channels_max = 2, 144 .rates = SNDRV_PCM_RATE_8000_192000, 145 .formats = SNDRV_PCM_FMTBIT_S8 | 146 SNDRV_PCM_FMTBIT_S16_LE | 147 SNDRV_PCM_FMTBIT_S24_LE | 148 SNDRV_PCM_FMTBIT_S24_3LE, 149 }, 150 .ops = &ak5386_dai_ops, 151 }; 152 153 #ifdef CONFIG_OF 154 static const struct of_device_id ak5386_dt_ids[] = { 155 { .compatible = "asahi-kasei,ak5386", }, 156 { } 157 }; 158 MODULE_DEVICE_TABLE(of, ak5386_dt_ids); 159 #endif 160 161 static int ak5386_probe(struct platform_device *pdev) 162 { 163 struct device *dev = &pdev->dev; 164 struct ak5386_priv *priv; 165 int ret, i; 166 167 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); 168 if (!priv) 169 return -ENOMEM; 170 171 priv->reset_gpio = -EINVAL; 172 dev_set_drvdata(dev, priv); 173 174 for (i = 0; i < ARRAY_SIZE(supply_names); i++) 175 priv->supplies[i].supply = supply_names[i]; 176 177 ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(priv->supplies), 178 priv->supplies); 179 if (ret < 0) 180 return ret; 181 182 if (of_match_device(of_match_ptr(ak5386_dt_ids), dev)) 183 priv->reset_gpio = of_get_named_gpio(dev->of_node, 184 "reset-gpio", 0); 185 186 if (gpio_is_valid(priv->reset_gpio)) 187 if (devm_gpio_request_one(dev, priv->reset_gpio, 188 GPIOF_OUT_INIT_LOW, 189 "AK5386 Reset")) 190 priv->reset_gpio = -EINVAL; 191 192 return devm_snd_soc_register_component(dev, &soc_component_ak5386, 193 &ak5386_dai, 1); 194 } 195 196 static struct platform_driver ak5386_driver = { 197 .probe = ak5386_probe, 198 .driver = { 199 .name = "ak5386", 200 .of_match_table = of_match_ptr(ak5386_dt_ids), 201 }, 202 }; 203 204 module_platform_driver(ak5386_driver); 205 206 MODULE_DESCRIPTION("ASoC driver for AK5386 ADC"); 207 MODULE_AUTHOR("Daniel Mack <zonque@gmail.com>"); 208 MODULE_LICENSE("GPL"); 209