ak5386.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. #include <linux/module.h>
  12. #include <linux/slab.h>
  13. #include <linux/of.h>
  14. #include <linux/of_gpio.h>
  15. #include <linux/of_device.h>
  16. #include <sound/soc.h>
  17. #include <sound/pcm.h>
  18. #include <sound/initval.h>
  19. struct ak5386_priv {
  20. int reset_gpio;
  21. };
  22. static struct snd_soc_codec_driver soc_codec_ak5386;
  23. static int ak5386_set_dai_fmt(struct snd_soc_dai *codec_dai,
  24. unsigned int format)
  25. {
  26. struct snd_soc_codec *codec = codec_dai->codec;
  27. format &= SND_SOC_DAIFMT_FORMAT_MASK;
  28. if (format != SND_SOC_DAIFMT_LEFT_J &&
  29. format != SND_SOC_DAIFMT_I2S) {
  30. dev_err(codec->dev, "Invalid DAI format\n");
  31. return -EINVAL;
  32. }
  33. return 0;
  34. }
  35. static int ak5386_hw_params(struct snd_pcm_substream *substream,
  36. struct snd_pcm_hw_params *params,
  37. struct snd_soc_dai *dai)
  38. {
  39. struct snd_soc_codec *codec = dai->codec;
  40. struct ak5386_priv *priv = snd_soc_codec_get_drvdata(codec);
  41. /*
  42. * From the datasheet:
  43. *
  44. * All external clocks (MCLK, SCLK and LRCK) must be present unless
  45. * PDN pin = “L”. If these clocks are not provided, the AK5386 may
  46. * draw excess current due to its use of internal dynamically
  47. * refreshed logic. If the external clocks are not present, place
  48. * the AK5386 in power-down mode (PDN pin = “L”).
  49. */
  50. if (gpio_is_valid(priv->reset_gpio))
  51. gpio_set_value(priv->reset_gpio, 1);
  52. return 0;
  53. }
  54. static int ak5386_hw_free(struct snd_pcm_substream *substream,
  55. struct snd_soc_dai *dai)
  56. {
  57. struct snd_soc_codec *codec = dai->codec;
  58. struct ak5386_priv *priv = snd_soc_codec_get_drvdata(codec);
  59. if (gpio_is_valid(priv->reset_gpio))
  60. gpio_set_value(priv->reset_gpio, 0);
  61. return 0;
  62. }
  63. static const struct snd_soc_dai_ops ak5386_dai_ops = {
  64. .set_fmt = ak5386_set_dai_fmt,
  65. .hw_params = ak5386_hw_params,
  66. .hw_free = ak5386_hw_free,
  67. };
  68. static struct snd_soc_dai_driver ak5386_dai = {
  69. .name = "ak5386-hifi",
  70. .capture = {
  71. .stream_name = "Capture",
  72. .channels_min = 1,
  73. .channels_max = 2,
  74. .rates = SNDRV_PCM_RATE_8000_192000,
  75. .formats = SNDRV_PCM_FMTBIT_S8 |
  76. SNDRV_PCM_FMTBIT_S16_LE |
  77. SNDRV_PCM_FMTBIT_S24_LE |
  78. SNDRV_PCM_FMTBIT_S24_3LE,
  79. },
  80. .ops = &ak5386_dai_ops,
  81. };
  82. #ifdef CONFIG_OF
  83. static const struct of_device_id ak5386_dt_ids[] = {
  84. { .compatible = "asahi-kasei,ak5386", },
  85. { }
  86. };
  87. MODULE_DEVICE_TABLE(of, ak5386_dt_ids);
  88. #endif
  89. static int ak5386_probe(struct platform_device *pdev)
  90. {
  91. struct device *dev = &pdev->dev;
  92. struct ak5386_priv *priv;
  93. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  94. if (!priv)
  95. return -ENOMEM;
  96. priv->reset_gpio = -EINVAL;
  97. dev_set_drvdata(dev, priv);
  98. if (of_match_device(of_match_ptr(ak5386_dt_ids), dev))
  99. priv->reset_gpio = of_get_named_gpio(dev->of_node,
  100. "reset-gpio", 0);
  101. if (gpio_is_valid(priv->reset_gpio))
  102. if (devm_gpio_request_one(dev, priv->reset_gpio,
  103. GPIOF_OUT_INIT_LOW,
  104. "AK5386 Reset"))
  105. priv->reset_gpio = -EINVAL;
  106. return snd_soc_register_codec(dev, &soc_codec_ak5386,
  107. &ak5386_dai, 1);
  108. }
  109. static int ak5386_remove(struct platform_device *pdev)
  110. {
  111. snd_soc_unregister_codec(&pdev->dev);
  112. return 0;
  113. }
  114. static struct platform_driver ak5386_driver = {
  115. .probe = ak5386_probe,
  116. .remove = ak5386_remove,
  117. .driver = {
  118. .name = "ak5386",
  119. .owner = THIS_MODULE,
  120. .of_match_table = of_match_ptr(ak5386_dt_ids),
  121. },
  122. };
  123. module_platform_driver(ak5386_driver);
  124. MODULE_DESCRIPTION("ASoC driver for AK5386 ADC");
  125. MODULE_AUTHOR("Daniel Mack <zonque@gmail.com>");
  126. MODULE_LICENSE("GPL");