pcm179x.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*
  2. * PCM179X 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 <sound/core.h>
  23. #include <sound/pcm.h>
  24. #include <sound/pcm_params.h>
  25. #include <sound/initval.h>
  26. #include <sound/soc.h>
  27. #include <sound/tlv.h>
  28. #include <linux/of.h>
  29. #include "pcm179x.h"
  30. #define PCM179X_DAC_VOL_LEFT 0x10
  31. #define PCM179X_DAC_VOL_RIGHT 0x11
  32. #define PCM179X_FMT_CONTROL 0x12
  33. #define PCM179X_MODE_CONTROL 0x13
  34. #define PCM179X_SOFT_MUTE PCM179X_FMT_CONTROL
  35. #define PCM179X_FMT_MASK 0x70
  36. #define PCM179X_FMT_SHIFT 4
  37. #define PCM179X_MUTE_MASK 0x01
  38. #define PCM179X_MUTE_SHIFT 0
  39. #define PCM179X_ATLD_ENABLE (1 << 7)
  40. static const struct reg_default pcm179x_reg_defaults[] = {
  41. { 0x10, 0xff },
  42. { 0x11, 0xff },
  43. { 0x12, 0x50 },
  44. { 0x13, 0x00 },
  45. { 0x14, 0x00 },
  46. { 0x15, 0x01 },
  47. { 0x16, 0x00 },
  48. { 0x17, 0x00 },
  49. };
  50. static bool pcm179x_accessible_reg(struct device *dev, unsigned int reg)
  51. {
  52. return reg >= 0x10 && reg <= 0x17;
  53. }
  54. static bool pcm179x_writeable_reg(struct device *dev, unsigned int reg)
  55. {
  56. bool accessible;
  57. accessible = pcm179x_accessible_reg(dev, reg);
  58. return accessible && reg != 0x16 && reg != 0x17;
  59. }
  60. struct pcm179x_private {
  61. struct regmap *regmap;
  62. unsigned int format;
  63. unsigned int rate;
  64. };
  65. static int pcm179x_set_dai_fmt(struct snd_soc_dai *codec_dai,
  66. unsigned int format)
  67. {
  68. struct snd_soc_codec *codec = codec_dai->codec;
  69. struct pcm179x_private *priv = snd_soc_codec_get_drvdata(codec);
  70. priv->format = format;
  71. return 0;
  72. }
  73. static int pcm179x_digital_mute(struct snd_soc_dai *dai, int mute)
  74. {
  75. struct snd_soc_codec *codec = dai->codec;
  76. struct pcm179x_private *priv = snd_soc_codec_get_drvdata(codec);
  77. int ret;
  78. ret = regmap_update_bits(priv->regmap, PCM179X_SOFT_MUTE,
  79. PCM179X_MUTE_MASK, !!mute);
  80. if (ret < 0)
  81. return ret;
  82. return 0;
  83. }
  84. static int pcm179x_hw_params(struct snd_pcm_substream *substream,
  85. struct snd_pcm_hw_params *params,
  86. struct snd_soc_dai *dai)
  87. {
  88. struct snd_soc_codec *codec = dai->codec;
  89. struct pcm179x_private *priv = snd_soc_codec_get_drvdata(codec);
  90. int val = 0, ret;
  91. priv->rate = params_rate(params);
  92. switch (priv->format & SND_SOC_DAIFMT_FORMAT_MASK) {
  93. case SND_SOC_DAIFMT_RIGHT_J:
  94. switch (params_width(params)) {
  95. case 24:
  96. case 32:
  97. val = 2;
  98. break;
  99. case 16:
  100. val = 0;
  101. break;
  102. default:
  103. return -EINVAL;
  104. }
  105. break;
  106. case SND_SOC_DAIFMT_I2S:
  107. switch (params_width(params)) {
  108. case 24:
  109. case 32:
  110. val = 5;
  111. break;
  112. case 16:
  113. val = 4;
  114. break;
  115. default:
  116. return -EINVAL;
  117. }
  118. break;
  119. default:
  120. dev_err(codec->dev, "Invalid DAI format\n");
  121. return -EINVAL;
  122. }
  123. val = val << PCM179X_FMT_SHIFT | PCM179X_ATLD_ENABLE;
  124. ret = regmap_update_bits(priv->regmap, PCM179X_FMT_CONTROL,
  125. PCM179X_FMT_MASK | PCM179X_ATLD_ENABLE, val);
  126. if (ret < 0)
  127. return ret;
  128. return 0;
  129. }
  130. static const struct snd_soc_dai_ops pcm179x_dai_ops = {
  131. .set_fmt = pcm179x_set_dai_fmt,
  132. .hw_params = pcm179x_hw_params,
  133. .digital_mute = pcm179x_digital_mute,
  134. };
  135. static const DECLARE_TLV_DB_SCALE(pcm179x_dac_tlv, -12000, 50, 1);
  136. static const struct snd_kcontrol_new pcm179x_controls[] = {
  137. SOC_DOUBLE_R_RANGE_TLV("DAC Playback Volume", PCM179X_DAC_VOL_LEFT,
  138. PCM179X_DAC_VOL_RIGHT, 0, 0xf, 0xff, 0,
  139. pcm179x_dac_tlv),
  140. SOC_SINGLE("DAC Invert Output Switch", PCM179X_MODE_CONTROL, 7, 1, 0),
  141. SOC_SINGLE("DAC Rolloff Filter Switch", PCM179X_MODE_CONTROL, 1, 1, 0),
  142. };
  143. static const struct snd_soc_dapm_widget pcm179x_dapm_widgets[] = {
  144. SND_SOC_DAPM_OUTPUT("IOUTL+"),
  145. SND_SOC_DAPM_OUTPUT("IOUTL-"),
  146. SND_SOC_DAPM_OUTPUT("IOUTR+"),
  147. SND_SOC_DAPM_OUTPUT("IOUTR-"),
  148. };
  149. static const struct snd_soc_dapm_route pcm179x_dapm_routes[] = {
  150. { "IOUTL+", NULL, "Playback" },
  151. { "IOUTL-", NULL, "Playback" },
  152. { "IOUTR+", NULL, "Playback" },
  153. { "IOUTR-", NULL, "Playback" },
  154. };
  155. static struct snd_soc_dai_driver pcm179x_dai = {
  156. .name = "pcm179x-hifi",
  157. .playback = {
  158. .stream_name = "Playback",
  159. .channels_min = 2,
  160. .channels_max = 2,
  161. .rates = SNDRV_PCM_RATE_CONTINUOUS,
  162. .rate_min = 10000,
  163. .rate_max = 200000,
  164. .formats = PCM1792A_FORMATS, },
  165. .ops = &pcm179x_dai_ops,
  166. };
  167. const struct regmap_config pcm179x_regmap_config = {
  168. .reg_bits = 8,
  169. .val_bits = 8,
  170. .max_register = 23,
  171. .reg_defaults = pcm179x_reg_defaults,
  172. .num_reg_defaults = ARRAY_SIZE(pcm179x_reg_defaults),
  173. .writeable_reg = pcm179x_writeable_reg,
  174. .readable_reg = pcm179x_accessible_reg,
  175. };
  176. EXPORT_SYMBOL_GPL(pcm179x_regmap_config);
  177. static struct snd_soc_codec_driver soc_codec_dev_pcm179x = {
  178. .component_driver = {
  179. .controls = pcm179x_controls,
  180. .num_controls = ARRAY_SIZE(pcm179x_controls),
  181. .dapm_widgets = pcm179x_dapm_widgets,
  182. .num_dapm_widgets = ARRAY_SIZE(pcm179x_dapm_widgets),
  183. .dapm_routes = pcm179x_dapm_routes,
  184. .num_dapm_routes = ARRAY_SIZE(pcm179x_dapm_routes),
  185. },
  186. };
  187. int pcm179x_common_init(struct device *dev, struct regmap *regmap)
  188. {
  189. struct pcm179x_private *pcm179x;
  190. pcm179x = devm_kzalloc(dev, sizeof(struct pcm179x_private),
  191. GFP_KERNEL);
  192. if (!pcm179x)
  193. return -ENOMEM;
  194. pcm179x->regmap = regmap;
  195. dev_set_drvdata(dev, pcm179x);
  196. return snd_soc_register_codec(dev,
  197. &soc_codec_dev_pcm179x, &pcm179x_dai, 1);
  198. }
  199. EXPORT_SYMBOL_GPL(pcm179x_common_init);
  200. int pcm179x_common_exit(struct device *dev)
  201. {
  202. snd_soc_unregister_codec(dev);
  203. return 0;
  204. }
  205. EXPORT_SYMBOL_GPL(pcm179x_common_exit);
  206. MODULE_DESCRIPTION("ASoC PCM179X driver");
  207. MODULE_AUTHOR("Michael Trimarchi <michael@amarulasolutions.com>");
  208. MODULE_LICENSE("GPL");