pcm1792a.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /*
  2. * PCM1792A ASoC codec driver
  3. *
  4. * Copyright (c) Amarula Solutions B.V. 2013
  5. *
  6. * Michael Trimarchi <michael@amarulasolutions.com>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version 2
  11. * of the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. */
  18. #include <linux/module.h>
  19. #include <linux/slab.h>
  20. #include <linux/kernel.h>
  21. #include <linux/device.h>
  22. #include <linux/spi/spi.h>
  23. #include <sound/core.h>
  24. #include <sound/pcm.h>
  25. #include <sound/pcm_params.h>
  26. #include <sound/initval.h>
  27. #include <sound/soc.h>
  28. #include <sound/tlv.h>
  29. #include <linux/of.h>
  30. #include <linux/of_device.h>
  31. #include "pcm1792a.h"
  32. #define PCM1792A_DAC_VOL_LEFT 0x10
  33. #define PCM1792A_DAC_VOL_RIGHT 0x11
  34. #define PCM1792A_FMT_CONTROL 0x12
  35. #define PCM1792A_SOFT_MUTE PCM1792A_FMT_CONTROL
  36. #define PCM1792A_FMT_MASK 0x70
  37. #define PCM1792A_FMT_SHIFT 4
  38. #define PCM1792A_MUTE_MASK 0x01
  39. #define PCM1792A_MUTE_SHIFT 0
  40. #define PCM1792A_ATLD_ENABLE (1 << 7)
  41. static const struct reg_default pcm1792a_reg_defaults[] = {
  42. { 0x10, 0xff },
  43. { 0x11, 0xff },
  44. { 0x12, 0x50 },
  45. { 0x13, 0x00 },
  46. { 0x14, 0x00 },
  47. { 0x15, 0x01 },
  48. { 0x16, 0x00 },
  49. { 0x17, 0x00 },
  50. };
  51. static bool pcm1792a_accessible_reg(struct device *dev, unsigned int reg)
  52. {
  53. return reg >= 0x10 && reg <= 0x17;
  54. }
  55. static bool pcm1792a_writeable_reg(struct device *dev, unsigned register reg)
  56. {
  57. bool accessible;
  58. accessible = pcm1792a_accessible_reg(dev, reg);
  59. return accessible && reg != 0x16 && reg != 0x17;
  60. }
  61. struct pcm1792a_private {
  62. struct regmap *regmap;
  63. unsigned int format;
  64. unsigned int rate;
  65. };
  66. static int pcm1792a_set_dai_fmt(struct snd_soc_dai *codec_dai,
  67. unsigned int format)
  68. {
  69. struct snd_soc_codec *codec = codec_dai->codec;
  70. struct pcm1792a_private *priv = snd_soc_codec_get_drvdata(codec);
  71. priv->format = format;
  72. return 0;
  73. }
  74. static int pcm1792a_digital_mute(struct snd_soc_dai *dai, int mute)
  75. {
  76. struct snd_soc_codec *codec = dai->codec;
  77. struct pcm1792a_private *priv = snd_soc_codec_get_drvdata(codec);
  78. int ret;
  79. ret = regmap_update_bits(priv->regmap, PCM1792A_SOFT_MUTE,
  80. PCM1792A_MUTE_MASK, !!mute);
  81. if (ret < 0)
  82. return ret;
  83. return 0;
  84. }
  85. static int pcm1792a_hw_params(struct snd_pcm_substream *substream,
  86. struct snd_pcm_hw_params *params,
  87. struct snd_soc_dai *dai)
  88. {
  89. struct snd_soc_codec *codec = dai->codec;
  90. struct pcm1792a_private *priv = snd_soc_codec_get_drvdata(codec);
  91. int val = 0, ret;
  92. int pcm_format = params_format(params);
  93. priv->rate = params_rate(params);
  94. switch (priv->format & SND_SOC_DAIFMT_FORMAT_MASK) {
  95. case SND_SOC_DAIFMT_RIGHT_J:
  96. if (pcm_format == SNDRV_PCM_FORMAT_S24_LE ||
  97. pcm_format == SNDRV_PCM_FORMAT_S32_LE)
  98. val = 0x02;
  99. else if (pcm_format == SNDRV_PCM_FORMAT_S16_LE)
  100. val = 0x00;
  101. break;
  102. case SND_SOC_DAIFMT_I2S:
  103. if (pcm_format == SNDRV_PCM_FORMAT_S24_LE ||
  104. pcm_format == SNDRV_PCM_FORMAT_S32_LE)
  105. val = 0x05;
  106. else if (pcm_format == SNDRV_PCM_FORMAT_S16_LE)
  107. val = 0x04;
  108. break;
  109. default:
  110. dev_err(codec->dev, "Invalid DAI format\n");
  111. return -EINVAL;
  112. }
  113. val = val << PCM1792A_FMT_SHIFT | PCM1792A_ATLD_ENABLE;
  114. ret = regmap_update_bits(priv->regmap, PCM1792A_FMT_CONTROL,
  115. PCM1792A_FMT_MASK | PCM1792A_ATLD_ENABLE, val);
  116. if (ret < 0)
  117. return ret;
  118. return 0;
  119. }
  120. static const struct snd_soc_dai_ops pcm1792a_dai_ops = {
  121. .set_fmt = pcm1792a_set_dai_fmt,
  122. .hw_params = pcm1792a_hw_params,
  123. .digital_mute = pcm1792a_digital_mute,
  124. };
  125. static const DECLARE_TLV_DB_SCALE(pcm1792a_dac_tlv, -12000, 50, 1);
  126. static const struct snd_kcontrol_new pcm1792a_controls[] = {
  127. SOC_DOUBLE_R_RANGE_TLV("DAC Playback Volume", PCM1792A_DAC_VOL_LEFT,
  128. PCM1792A_DAC_VOL_RIGHT, 0, 0xf, 0xff, 0,
  129. pcm1792a_dac_tlv),
  130. };
  131. static const struct snd_soc_dapm_widget pcm1792a_dapm_widgets[] = {
  132. SND_SOC_DAPM_OUTPUT("IOUTL+"),
  133. SND_SOC_DAPM_OUTPUT("IOUTL-"),
  134. SND_SOC_DAPM_OUTPUT("IOUTR+"),
  135. SND_SOC_DAPM_OUTPUT("IOUTR-"),
  136. };
  137. static const struct snd_soc_dapm_route pcm1792a_dapm_routes[] = {
  138. { "IOUTL+", NULL, "Playback" },
  139. { "IOUTL-", NULL, "Playback" },
  140. { "IOUTR+", NULL, "Playback" },
  141. { "IOUTR-", NULL, "Playback" },
  142. };
  143. static struct snd_soc_dai_driver pcm1792a_dai = {
  144. .name = "pcm1792a-hifi",
  145. .playback = {
  146. .stream_name = "Playback",
  147. .channels_min = 2,
  148. .channels_max = 2,
  149. .rates = PCM1792A_RATES,
  150. .formats = PCM1792A_FORMATS, },
  151. .ops = &pcm1792a_dai_ops,
  152. };
  153. static const struct of_device_id pcm1792a_of_match[] = {
  154. { .compatible = "ti,pcm1792a", },
  155. { }
  156. };
  157. MODULE_DEVICE_TABLE(of, pcm1792a_of_match);
  158. static const struct regmap_config pcm1792a_regmap = {
  159. .reg_bits = 8,
  160. .val_bits = 8,
  161. .max_register = 23,
  162. .reg_defaults = pcm1792a_reg_defaults,
  163. .num_reg_defaults = ARRAY_SIZE(pcm1792a_reg_defaults),
  164. .writeable_reg = pcm1792a_writeable_reg,
  165. .readable_reg = pcm1792a_accessible_reg,
  166. };
  167. static struct snd_soc_codec_driver soc_codec_dev_pcm1792a = {
  168. .controls = pcm1792a_controls,
  169. .num_controls = ARRAY_SIZE(pcm1792a_controls),
  170. .dapm_widgets = pcm1792a_dapm_widgets,
  171. .num_dapm_widgets = ARRAY_SIZE(pcm1792a_dapm_widgets),
  172. .dapm_routes = pcm1792a_dapm_routes,
  173. .num_dapm_routes = ARRAY_SIZE(pcm1792a_dapm_routes),
  174. };
  175. static int pcm1792a_spi_probe(struct spi_device *spi)
  176. {
  177. struct pcm1792a_private *pcm1792a;
  178. int ret;
  179. pcm1792a = devm_kzalloc(&spi->dev, sizeof(struct pcm1792a_private),
  180. GFP_KERNEL);
  181. if (!pcm1792a)
  182. return -ENOMEM;
  183. spi_set_drvdata(spi, pcm1792a);
  184. pcm1792a->regmap = devm_regmap_init_spi(spi, &pcm1792a_regmap);
  185. if (IS_ERR(pcm1792a->regmap)) {
  186. ret = PTR_ERR(pcm1792a->regmap);
  187. dev_err(&spi->dev, "Failed to register regmap: %d\n", ret);
  188. return ret;
  189. }
  190. return snd_soc_register_codec(&spi->dev,
  191. &soc_codec_dev_pcm1792a, &pcm1792a_dai, 1);
  192. }
  193. static int pcm1792a_spi_remove(struct spi_device *spi)
  194. {
  195. snd_soc_unregister_codec(&spi->dev);
  196. return 0;
  197. }
  198. static const struct spi_device_id pcm1792a_spi_ids[] = {
  199. { "pcm1792a", 0 },
  200. { },
  201. };
  202. MODULE_DEVICE_TABLE(spi, pcm1792a_spi_ids);
  203. static struct spi_driver pcm1792a_codec_driver = {
  204. .driver = {
  205. .name = "pcm1792a",
  206. .owner = THIS_MODULE,
  207. .of_match_table = of_match_ptr(pcm1792a_of_match),
  208. },
  209. .id_table = pcm1792a_spi_ids,
  210. .probe = pcm1792a_spi_probe,
  211. .remove = pcm1792a_spi_remove,
  212. };
  213. module_spi_driver(pcm1792a_codec_driver);
  214. MODULE_DESCRIPTION("ASoC PCM1792A driver");
  215. MODULE_AUTHOR("Michael Trimarchi <michael@amarulasolutions.com>");
  216. MODULE_LICENSE("GPL");