pcm1681.c 8.9 KB

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