cs4349.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. /*
  2. * cs4349.c -- CS4349 ALSA Soc Audio driver
  3. *
  4. * Copyright 2015 Cirrus Logic, Inc.
  5. *
  6. * Authors: Tim Howe <Tim.Howe@cirrus.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #include <linux/module.h>
  13. #include <linux/moduleparam.h>
  14. #include <linux/kernel.h>
  15. #include <linux/init.h>
  16. #include <linux/delay.h>
  17. #include <linux/gpio.h>
  18. #include <linux/gpio/consumer.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/pm.h>
  21. #include <linux/i2c.h>
  22. #include <linux/of_device.h>
  23. #include <linux/regmap.h>
  24. #include <linux/slab.h>
  25. #include <sound/core.h>
  26. #include <sound/pcm.h>
  27. #include <sound/pcm_params.h>
  28. #include <sound/soc.h>
  29. #include <sound/soc-dapm.h>
  30. #include <sound/initval.h>
  31. #include <sound/tlv.h>
  32. #include "cs4349.h"
  33. static const struct reg_default cs4349_reg_defaults[] = {
  34. { 2, 0x00 }, /* r02 - Mode Control */
  35. { 3, 0x09 }, /* r03 - Volume, Mixing and Inversion Control */
  36. { 4, 0x81 }, /* r04 - Mute Control */
  37. { 5, 0x00 }, /* r05 - Channel A Volume Control */
  38. { 6, 0x00 }, /* r06 - Channel B Volume Control */
  39. { 7, 0xB1 }, /* r07 - Ramp and Filter Control */
  40. { 8, 0x1C }, /* r08 - Misc. Control */
  41. };
  42. /* Private data for the CS4349 */
  43. struct cs4349_private {
  44. struct regmap *regmap;
  45. struct gpio_desc *reset_gpio;
  46. unsigned int mode;
  47. int rate;
  48. };
  49. static bool cs4349_readable_register(struct device *dev, unsigned int reg)
  50. {
  51. switch (reg) {
  52. case CS4349_CHIPID ... CS4349_MISC:
  53. return true;
  54. default:
  55. return false;
  56. }
  57. }
  58. static bool cs4349_writeable_register(struct device *dev, unsigned int reg)
  59. {
  60. switch (reg) {
  61. case CS4349_MODE ... CS4349_MISC:
  62. return true;
  63. default:
  64. return false;
  65. }
  66. }
  67. static int cs4349_set_dai_fmt(struct snd_soc_dai *codec_dai,
  68. unsigned int format)
  69. {
  70. struct snd_soc_codec *codec = codec_dai->codec;
  71. struct cs4349_private *cs4349 = snd_soc_codec_get_drvdata(codec);
  72. unsigned int fmt;
  73. fmt = format & SND_SOC_DAIFMT_FORMAT_MASK;
  74. switch (fmt) {
  75. case SND_SOC_DAIFMT_I2S:
  76. case SND_SOC_DAIFMT_LEFT_J:
  77. case SND_SOC_DAIFMT_RIGHT_J:
  78. cs4349->mode = format & SND_SOC_DAIFMT_FORMAT_MASK;
  79. break;
  80. default:
  81. return -EINVAL;
  82. }
  83. return 0;
  84. }
  85. static int cs4349_pcm_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 cs4349_private *cs4349 = snd_soc_codec_get_drvdata(codec);
  91. int fmt, ret;
  92. cs4349->rate = params_rate(params);
  93. switch (cs4349->mode) {
  94. case SND_SOC_DAIFMT_I2S:
  95. fmt = DIF_I2S;
  96. break;
  97. case SND_SOC_DAIFMT_LEFT_J:
  98. fmt = DIF_LEFT_JST;
  99. break;
  100. case SND_SOC_DAIFMT_RIGHT_J:
  101. switch (params_width(params)) {
  102. case 16:
  103. fmt = DIF_RGHT_JST16;
  104. break;
  105. case 24:
  106. fmt = DIF_RGHT_JST24;
  107. break;
  108. default:
  109. return -EINVAL;
  110. }
  111. break;
  112. default:
  113. return -EINVAL;
  114. }
  115. ret = snd_soc_update_bits(codec, CS4349_MODE, DIF_MASK,
  116. MODE_FORMAT(fmt));
  117. if (ret < 0)
  118. return ret;
  119. return 0;
  120. }
  121. static int cs4349_digital_mute(struct snd_soc_dai *dai, int mute)
  122. {
  123. struct snd_soc_codec *codec = dai->codec;
  124. int reg;
  125. reg = 0;
  126. if (mute)
  127. reg = MUTE_AB_MASK;
  128. return snd_soc_update_bits(codec, CS4349_MUTE, MUTE_AB_MASK, reg);
  129. }
  130. static DECLARE_TLV_DB_SCALE(dig_tlv, -12750, 50, 0);
  131. static const char * const chan_mix_texts[] = {
  132. "Mute", "MuteA", "MuteA SwapB", "MuteA MonoB", "SwapA MuteB",
  133. "BothR", "Swap", "SwapA MonoB", "MuteB", "Normal", "BothL",
  134. "MonoB", "MonoA MuteB", "MonoA", "MonoA SwapB", "Mono",
  135. /*Normal == Channel A = Left, Channel B = Right*/
  136. };
  137. static const char * const fm_texts[] = {
  138. "Auto", "Single", "Double", "Quad",
  139. };
  140. static const char * const deemph_texts[] = {
  141. "None", "44.1k", "48k", "32k",
  142. };
  143. static const char * const softr_zeroc_texts[] = {
  144. "Immediate", "Zero Cross", "Soft Ramp", "SR on ZC",
  145. };
  146. static int deemph_values[] = {
  147. 0, 4, 8, 12,
  148. };
  149. static int softr_zeroc_values[] = {
  150. 0, 64, 128, 192,
  151. };
  152. static const struct soc_enum chan_mix_enum =
  153. SOC_ENUM_SINGLE(CS4349_VMI, 0,
  154. ARRAY_SIZE(chan_mix_texts),
  155. chan_mix_texts);
  156. static const struct soc_enum fm_mode_enum =
  157. SOC_ENUM_SINGLE(CS4349_MODE, 0,
  158. ARRAY_SIZE(fm_texts),
  159. fm_texts);
  160. static SOC_VALUE_ENUM_SINGLE_DECL(deemph_enum, CS4349_MODE, 0, DEM_MASK,
  161. deemph_texts, deemph_values);
  162. static SOC_VALUE_ENUM_SINGLE_DECL(softr_zeroc_enum, CS4349_RMPFLT, 0,
  163. SR_ZC_MASK, softr_zeroc_texts,
  164. softr_zeroc_values);
  165. static const struct snd_kcontrol_new cs4349_snd_controls[] = {
  166. SOC_DOUBLE_R_TLV("Master Playback Volume",
  167. CS4349_VOLA, CS4349_VOLB, 0, 0xFF, 1, dig_tlv),
  168. SOC_ENUM("Functional Mode", fm_mode_enum),
  169. SOC_ENUM("De-Emphasis Control", deemph_enum),
  170. SOC_ENUM("Soft Ramp Zero Cross Control", softr_zeroc_enum),
  171. SOC_ENUM("Channel Mixer", chan_mix_enum),
  172. SOC_SINGLE("VolA = VolB Switch", CS4349_VMI, 7, 1, 0),
  173. SOC_SINGLE("InvertA Switch", CS4349_VMI, 6, 1, 0),
  174. SOC_SINGLE("InvertB Switch", CS4349_VMI, 5, 1, 0),
  175. SOC_SINGLE("Auto-Mute Switch", CS4349_MUTE, 7, 1, 0),
  176. SOC_SINGLE("MUTEC A = B Switch", CS4349_MUTE, 5, 1, 0),
  177. SOC_SINGLE("Soft Ramp Up Switch", CS4349_RMPFLT, 5, 1, 0),
  178. SOC_SINGLE("Soft Ramp Down Switch", CS4349_RMPFLT, 4, 1, 0),
  179. SOC_SINGLE("Slow Roll Off Filter Switch", CS4349_RMPFLT, 2, 1, 0),
  180. SOC_SINGLE("Freeze Switch", CS4349_MISC, 5, 1, 0),
  181. SOC_SINGLE("Popguard Switch", CS4349_MISC, 4, 1, 0),
  182. };
  183. static const struct snd_soc_dapm_widget cs4349_dapm_widgets[] = {
  184. SND_SOC_DAPM_DAC("HiFi DAC", NULL, SND_SOC_NOPM, 0, 0),
  185. SND_SOC_DAPM_OUTPUT("OutputA"),
  186. SND_SOC_DAPM_OUTPUT("OutputB"),
  187. };
  188. static const struct snd_soc_dapm_route cs4349_routes[] = {
  189. {"DAC Playback", NULL, "OutputA"},
  190. {"DAC Playback", NULL, "OutputB"},
  191. {"OutputA", NULL, "HiFi DAC"},
  192. {"OutputB", NULL, "HiFi DAC"},
  193. };
  194. #define CS4349_PCM_FORMATS (SNDRV_PCM_FMTBIT_S8 | \
  195. SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE | \
  196. SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE | \
  197. SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE | \
  198. SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S24_3BE | \
  199. SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE | \
  200. SNDRV_PCM_FMTBIT_S32_LE)
  201. #define CS4349_PCM_RATES SNDRV_PCM_RATE_8000_192000
  202. static const struct snd_soc_dai_ops cs4349_dai_ops = {
  203. .hw_params = cs4349_pcm_hw_params,
  204. .set_fmt = cs4349_set_dai_fmt,
  205. .digital_mute = cs4349_digital_mute,
  206. };
  207. static struct snd_soc_dai_driver cs4349_dai = {
  208. .name = "cs4349_hifi",
  209. .playback = {
  210. .stream_name = "DAC Playback",
  211. .channels_min = 1,
  212. .channels_max = 2,
  213. .rates = CS4349_PCM_RATES,
  214. .formats = CS4349_PCM_FORMATS,
  215. },
  216. .ops = &cs4349_dai_ops,
  217. .symmetric_rates = 1,
  218. };
  219. static struct snd_soc_codec_driver soc_codec_dev_cs4349 = {
  220. .component_driver = {
  221. .controls = cs4349_snd_controls,
  222. .num_controls = ARRAY_SIZE(cs4349_snd_controls),
  223. .dapm_widgets = cs4349_dapm_widgets,
  224. .num_dapm_widgets = ARRAY_SIZE(cs4349_dapm_widgets),
  225. .dapm_routes = cs4349_routes,
  226. .num_dapm_routes = ARRAY_SIZE(cs4349_routes),
  227. },
  228. };
  229. static const struct regmap_config cs4349_regmap = {
  230. .reg_bits = 8,
  231. .val_bits = 8,
  232. .max_register = CS4349_MISC,
  233. .reg_defaults = cs4349_reg_defaults,
  234. .num_reg_defaults = ARRAY_SIZE(cs4349_reg_defaults),
  235. .readable_reg = cs4349_readable_register,
  236. .writeable_reg = cs4349_writeable_register,
  237. .cache_type = REGCACHE_RBTREE,
  238. };
  239. static int cs4349_i2c_probe(struct i2c_client *client,
  240. const struct i2c_device_id *id)
  241. {
  242. struct cs4349_private *cs4349;
  243. int ret;
  244. cs4349 = devm_kzalloc(&client->dev, sizeof(*cs4349), GFP_KERNEL);
  245. if (!cs4349)
  246. return -ENOMEM;
  247. cs4349->regmap = devm_regmap_init_i2c(client, &cs4349_regmap);
  248. if (IS_ERR(cs4349->regmap)) {
  249. ret = PTR_ERR(cs4349->regmap);
  250. dev_err(&client->dev, "regmap_init() failed: %d\n", ret);
  251. return ret;
  252. }
  253. /* Reset the Device */
  254. cs4349->reset_gpio = devm_gpiod_get_optional(&client->dev,
  255. "reset", GPIOD_OUT_LOW);
  256. if (IS_ERR(cs4349->reset_gpio))
  257. return PTR_ERR(cs4349->reset_gpio);
  258. gpiod_set_value_cansleep(cs4349->reset_gpio, 1);
  259. i2c_set_clientdata(client, cs4349);
  260. return snd_soc_register_codec(&client->dev, &soc_codec_dev_cs4349,
  261. &cs4349_dai, 1);
  262. }
  263. static int cs4349_i2c_remove(struct i2c_client *client)
  264. {
  265. struct cs4349_private *cs4349 = i2c_get_clientdata(client);
  266. snd_soc_unregister_codec(&client->dev);
  267. /* Hold down reset */
  268. gpiod_set_value_cansleep(cs4349->reset_gpio, 0);
  269. return 0;
  270. }
  271. #ifdef CONFIG_PM
  272. static int cs4349_runtime_suspend(struct device *dev)
  273. {
  274. struct cs4349_private *cs4349 = dev_get_drvdata(dev);
  275. int ret;
  276. ret = regmap_update_bits(cs4349->regmap, CS4349_MISC, PWR_DWN, PWR_DWN);
  277. if (ret < 0)
  278. return ret;
  279. regcache_cache_only(cs4349->regmap, true);
  280. /* Hold down reset */
  281. gpiod_set_value_cansleep(cs4349->reset_gpio, 0);
  282. return 0;
  283. }
  284. static int cs4349_runtime_resume(struct device *dev)
  285. {
  286. struct cs4349_private *cs4349 = dev_get_drvdata(dev);
  287. int ret;
  288. ret = regmap_update_bits(cs4349->regmap, CS4349_MISC, PWR_DWN, 0);
  289. if (ret < 0)
  290. return ret;
  291. gpiod_set_value_cansleep(cs4349->reset_gpio, 1);
  292. regcache_cache_only(cs4349->regmap, false);
  293. regcache_sync(cs4349->regmap);
  294. return 0;
  295. }
  296. #endif
  297. static const struct dev_pm_ops cs4349_runtime_pm = {
  298. SET_RUNTIME_PM_OPS(cs4349_runtime_suspend, cs4349_runtime_resume,
  299. NULL)
  300. };
  301. static const struct of_device_id cs4349_of_match[] = {
  302. { .compatible = "cirrus,cs4349", },
  303. {},
  304. };
  305. MODULE_DEVICE_TABLE(of, cs4349_of_match);
  306. static const struct i2c_device_id cs4349_i2c_id[] = {
  307. {"cs4349", 0},
  308. {}
  309. };
  310. MODULE_DEVICE_TABLE(i2c, cs4349_i2c_id);
  311. static struct i2c_driver cs4349_i2c_driver = {
  312. .driver = {
  313. .name = "cs4349",
  314. .of_match_table = cs4349_of_match,
  315. },
  316. .id_table = cs4349_i2c_id,
  317. .probe = cs4349_i2c_probe,
  318. .remove = cs4349_i2c_remove,
  319. };
  320. module_i2c_driver(cs4349_i2c_driver);
  321. MODULE_AUTHOR("Tim Howe <tim.howe@cirrus.com>");
  322. MODULE_DESCRIPTION("Cirrus Logic CS4349 ALSA SoC Codec Driver");
  323. MODULE_LICENSE("GPL");