pcm1681.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. /*
  2. * PCM1681 ASoC codec driver
  3. *
  4. * Copyright (c) StreamUnlimited GmbH 2013
  5. * Marek Belisko <marek.belisko@streamunlimited.com>
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. */
  17. #include <linux/module.h>
  18. #include <linux/slab.h>
  19. #include <linux/delay.h>
  20. #include <linux/gpio.h>
  21. #include <linux/i2c.h>
  22. #include <linux/regmap.h>
  23. #include <linux/of.h>
  24. #include <linux/of_device.h>
  25. #include <linux/of_gpio.h>
  26. #include <sound/pcm.h>
  27. #include <sound/pcm_params.h>
  28. #include <sound/soc.h>
  29. #include <sound/tlv.h>
  30. #define PCM1681_PCM_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | \
  31. SNDRV_PCM_FMTBIT_S24_LE)
  32. #define PCM1681_PCM_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000 | \
  33. SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | \
  34. SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 | \
  35. SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_192000)
  36. #define PCM1681_SOFT_MUTE_ALL 0xff
  37. #define PCM1681_DEEMPH_RATE_MASK 0x18
  38. #define PCM1681_DEEMPH_MASK 0x01
  39. #define PCM1681_ATT_CONTROL(X) (X <= 6 ? X : X + 9) /* Attenuation level */
  40. #define PCM1681_SOFT_MUTE 0x07 /* Soft mute control register */
  41. #define PCM1681_DAC_CONTROL 0x08 /* DAC operation control */
  42. #define PCM1681_FMT_CONTROL 0x09 /* Audio interface data format */
  43. #define PCM1681_DEEMPH_CONTROL 0x0a /* De-emphasis control */
  44. #define PCM1681_ZERO_DETECT_STATUS 0x0e /* Zero detect status reg */
  45. static const struct reg_default pcm1681_reg_defaults[] = {
  46. { 0x01, 0xff },
  47. { 0x02, 0xff },
  48. { 0x03, 0xff },
  49. { 0x04, 0xff },
  50. { 0x05, 0xff },
  51. { 0x06, 0xff },
  52. { 0x07, 0x00 },
  53. { 0x08, 0x00 },
  54. { 0x09, 0x06 },
  55. { 0x0A, 0x00 },
  56. { 0x0B, 0xff },
  57. { 0x0C, 0x0f },
  58. { 0x0D, 0x00 },
  59. { 0x10, 0xff },
  60. { 0x11, 0xff },
  61. { 0x12, 0x00 },
  62. { 0x13, 0x00 },
  63. };
  64. static bool pcm1681_accessible_reg(struct device *dev, unsigned int reg)
  65. {
  66. return !((reg == 0x00) || (reg == 0x0f));
  67. }
  68. static bool pcm1681_writeable_reg(struct device *dev, unsigned int reg)
  69. {
  70. return pcm1681_accessible_reg(dev, reg) &&
  71. (reg != PCM1681_ZERO_DETECT_STATUS);
  72. }
  73. struct pcm1681_private {
  74. struct regmap *regmap;
  75. unsigned int format;
  76. /* Current deemphasis status */
  77. unsigned int deemph;
  78. /* Current rate for deemphasis control */
  79. unsigned int rate;
  80. };
  81. static const int pcm1681_deemph[] = { 44100, 48000, 32000 };
  82. static int pcm1681_set_deemph(struct snd_soc_codec *codec)
  83. {
  84. struct pcm1681_private *priv = snd_soc_codec_get_drvdata(codec);
  85. int i = 0, val = -1, enable = 0;
  86. if (priv->deemph) {
  87. for (i = 0; i < ARRAY_SIZE(pcm1681_deemph); i++) {
  88. if (pcm1681_deemph[i] == priv->rate) {
  89. val = i;
  90. break;
  91. }
  92. }
  93. }
  94. if (val != -1) {
  95. regmap_update_bits(priv->regmap, PCM1681_DEEMPH_CONTROL,
  96. PCM1681_DEEMPH_RATE_MASK, val << 3);
  97. enable = 1;
  98. } else {
  99. enable = 0;
  100. }
  101. /* enable/disable deemphasis functionality */
  102. return regmap_update_bits(priv->regmap, PCM1681_DEEMPH_CONTROL,
  103. PCM1681_DEEMPH_MASK, enable);
  104. }
  105. static int pcm1681_get_deemph(struct snd_kcontrol *kcontrol,
  106. struct snd_ctl_elem_value *ucontrol)
  107. {
  108. struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
  109. struct pcm1681_private *priv = snd_soc_codec_get_drvdata(codec);
  110. ucontrol->value.integer.value[0] = priv->deemph;
  111. return 0;
  112. }
  113. static int pcm1681_put_deemph(struct snd_kcontrol *kcontrol,
  114. struct snd_ctl_elem_value *ucontrol)
  115. {
  116. struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
  117. struct pcm1681_private *priv = snd_soc_codec_get_drvdata(codec);
  118. priv->deemph = ucontrol->value.integer.value[0];
  119. return pcm1681_set_deemph(codec);
  120. }
  121. static int pcm1681_set_dai_fmt(struct snd_soc_dai *codec_dai,
  122. unsigned int format)
  123. {
  124. struct snd_soc_codec *codec = codec_dai->codec;
  125. struct pcm1681_private *priv = snd_soc_codec_get_drvdata(codec);
  126. /* The PCM1681 can only be slave to all clocks */
  127. if ((format & SND_SOC_DAIFMT_MASTER_MASK) != SND_SOC_DAIFMT_CBS_CFS) {
  128. dev_err(codec->dev, "Invalid clocking mode\n");
  129. return -EINVAL;
  130. }
  131. priv->format = format;
  132. return 0;
  133. }
  134. static int pcm1681_digital_mute(struct snd_soc_dai *dai, int mute)
  135. {
  136. struct snd_soc_codec *codec = dai->codec;
  137. struct pcm1681_private *priv = snd_soc_codec_get_drvdata(codec);
  138. int val;
  139. if (mute)
  140. val = PCM1681_SOFT_MUTE_ALL;
  141. else
  142. val = 0;
  143. return regmap_write(priv->regmap, PCM1681_SOFT_MUTE, val);
  144. }
  145. static int pcm1681_hw_params(struct snd_pcm_substream *substream,
  146. struct snd_pcm_hw_params *params,
  147. struct snd_soc_dai *dai)
  148. {
  149. struct snd_soc_codec *codec = dai->codec;
  150. struct pcm1681_private *priv = snd_soc_codec_get_drvdata(codec);
  151. int val = 0, ret;
  152. priv->rate = params_rate(params);
  153. switch (priv->format & SND_SOC_DAIFMT_FORMAT_MASK) {
  154. case SND_SOC_DAIFMT_RIGHT_J:
  155. switch (params_width(params)) {
  156. case 24:
  157. val = 0;
  158. break;
  159. case 16:
  160. val = 3;
  161. break;
  162. default:
  163. return -EINVAL;
  164. }
  165. break;
  166. case SND_SOC_DAIFMT_I2S:
  167. val = 0x04;
  168. break;
  169. case SND_SOC_DAIFMT_LEFT_J:
  170. val = 0x05;
  171. break;
  172. default:
  173. dev_err(codec->dev, "Invalid DAI format\n");
  174. return -EINVAL;
  175. }
  176. ret = regmap_update_bits(priv->regmap, PCM1681_FMT_CONTROL, 0x0f, val);
  177. if (ret < 0)
  178. return ret;
  179. return pcm1681_set_deemph(codec);
  180. }
  181. static const struct snd_soc_dai_ops pcm1681_dai_ops = {
  182. .set_fmt = pcm1681_set_dai_fmt,
  183. .hw_params = pcm1681_hw_params,
  184. .digital_mute = pcm1681_digital_mute,
  185. };
  186. static const struct snd_soc_dapm_widget pcm1681_dapm_widgets[] = {
  187. SND_SOC_DAPM_OUTPUT("VOUT1"),
  188. SND_SOC_DAPM_OUTPUT("VOUT2"),
  189. SND_SOC_DAPM_OUTPUT("VOUT3"),
  190. SND_SOC_DAPM_OUTPUT("VOUT4"),
  191. SND_SOC_DAPM_OUTPUT("VOUT5"),
  192. SND_SOC_DAPM_OUTPUT("VOUT6"),
  193. SND_SOC_DAPM_OUTPUT("VOUT7"),
  194. SND_SOC_DAPM_OUTPUT("VOUT8"),
  195. };
  196. static const struct snd_soc_dapm_route pcm1681_dapm_routes[] = {
  197. { "VOUT1", NULL, "Playback" },
  198. { "VOUT2", NULL, "Playback" },
  199. { "VOUT3", NULL, "Playback" },
  200. { "VOUT4", NULL, "Playback" },
  201. { "VOUT5", NULL, "Playback" },
  202. { "VOUT6", NULL, "Playback" },
  203. { "VOUT7", NULL, "Playback" },
  204. { "VOUT8", NULL, "Playback" },
  205. };
  206. static const DECLARE_TLV_DB_SCALE(pcm1681_dac_tlv, -6350, 50, 1);
  207. static const struct snd_kcontrol_new pcm1681_controls[] = {
  208. SOC_DOUBLE_R_TLV("Channel 1/2 Playback Volume",
  209. PCM1681_ATT_CONTROL(1), PCM1681_ATT_CONTROL(2), 0,
  210. 0x7f, 0, pcm1681_dac_tlv),
  211. SOC_DOUBLE_R_TLV("Channel 3/4 Playback Volume",
  212. PCM1681_ATT_CONTROL(3), PCM1681_ATT_CONTROL(4), 0,
  213. 0x7f, 0, pcm1681_dac_tlv),
  214. SOC_DOUBLE_R_TLV("Channel 5/6 Playback Volume",
  215. PCM1681_ATT_CONTROL(5), PCM1681_ATT_CONTROL(6), 0,
  216. 0x7f, 0, pcm1681_dac_tlv),
  217. SOC_DOUBLE_R_TLV("Channel 7/8 Playback Volume",
  218. PCM1681_ATT_CONTROL(7), PCM1681_ATT_CONTROL(8), 0,
  219. 0x7f, 0, pcm1681_dac_tlv),
  220. SOC_SINGLE_BOOL_EXT("De-emphasis Switch", 0,
  221. pcm1681_get_deemph, pcm1681_put_deemph),
  222. };
  223. static struct snd_soc_dai_driver pcm1681_dai = {
  224. .name = "pcm1681-hifi",
  225. .playback = {
  226. .stream_name = "Playback",
  227. .channels_min = 2,
  228. .channels_max = 8,
  229. .rates = PCM1681_PCM_RATES,
  230. .formats = PCM1681_PCM_FORMATS,
  231. },
  232. .ops = &pcm1681_dai_ops,
  233. };
  234. #ifdef CONFIG_OF
  235. static const struct of_device_id pcm1681_dt_ids[] = {
  236. { .compatible = "ti,pcm1681", },
  237. { }
  238. };
  239. MODULE_DEVICE_TABLE(of, pcm1681_dt_ids);
  240. #endif
  241. static const struct regmap_config pcm1681_regmap = {
  242. .reg_bits = 8,
  243. .val_bits = 8,
  244. .max_register = 0x13,
  245. .reg_defaults = pcm1681_reg_defaults,
  246. .num_reg_defaults = ARRAY_SIZE(pcm1681_reg_defaults),
  247. .writeable_reg = pcm1681_writeable_reg,
  248. .readable_reg = pcm1681_accessible_reg,
  249. };
  250. static struct snd_soc_codec_driver soc_codec_dev_pcm1681 = {
  251. .component_driver = {
  252. .controls = pcm1681_controls,
  253. .num_controls = ARRAY_SIZE(pcm1681_controls),
  254. .dapm_widgets = pcm1681_dapm_widgets,
  255. .num_dapm_widgets = ARRAY_SIZE(pcm1681_dapm_widgets),
  256. .dapm_routes = pcm1681_dapm_routes,
  257. .num_dapm_routes = ARRAY_SIZE(pcm1681_dapm_routes),
  258. },
  259. };
  260. static const struct i2c_device_id pcm1681_i2c_id[] = {
  261. {"pcm1681", 0},
  262. {}
  263. };
  264. MODULE_DEVICE_TABLE(i2c, pcm1681_i2c_id);
  265. static int pcm1681_i2c_probe(struct i2c_client *client,
  266. const struct i2c_device_id *id)
  267. {
  268. int ret;
  269. struct pcm1681_private *priv;
  270. priv = devm_kzalloc(&client->dev, sizeof(*priv), GFP_KERNEL);
  271. if (!priv)
  272. return -ENOMEM;
  273. priv->regmap = devm_regmap_init_i2c(client, &pcm1681_regmap);
  274. if (IS_ERR(priv->regmap)) {
  275. ret = PTR_ERR(priv->regmap);
  276. dev_err(&client->dev, "Failed to create regmap: %d\n", ret);
  277. return ret;
  278. }
  279. i2c_set_clientdata(client, priv);
  280. return snd_soc_register_codec(&client->dev, &soc_codec_dev_pcm1681,
  281. &pcm1681_dai, 1);
  282. }
  283. static int pcm1681_i2c_remove(struct i2c_client *client)
  284. {
  285. snd_soc_unregister_codec(&client->dev);
  286. return 0;
  287. }
  288. static struct i2c_driver pcm1681_i2c_driver = {
  289. .driver = {
  290. .name = "pcm1681",
  291. .of_match_table = of_match_ptr(pcm1681_dt_ids),
  292. },
  293. .id_table = pcm1681_i2c_id,
  294. .probe = pcm1681_i2c_probe,
  295. .remove = pcm1681_i2c_remove,
  296. };
  297. module_i2c_driver(pcm1681_i2c_driver);
  298. MODULE_DESCRIPTION("Texas Instruments PCM1681 ALSA SoC Codec Driver");
  299. MODULE_AUTHOR("Marek Belisko <marek.belisko@streamunlimited.com>");
  300. MODULE_LICENSE("GPL");