cs4349.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * cs4349.c -- CS4349 ALSA Soc Audio driver
  4. *
  5. * Copyright 2015 Cirrus Logic, Inc.
  6. *
  7. * Authors: Tim Howe <Tim.Howe@cirrus.com>
  8. */
  9. #include <linux/module.h>
  10. #include <linux/moduleparam.h>
  11. #include <linux/kernel.h>
  12. #include <linux/init.h>
  13. #include <linux/delay.h>
  14. #include <linux/gpio.h>
  15. #include <linux/gpio/consumer.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/pm.h>
  18. #include <linux/i2c.h>
  19. #include <linux/of_device.h>
  20. #include <linux/regmap.h>
  21. #include <linux/slab.h>
  22. #include <sound/core.h>
  23. #include <sound/pcm.h>
  24. #include <sound/pcm_params.h>
  25. #include <sound/soc.h>
  26. #include <sound/soc-dapm.h>
  27. #include <sound/initval.h>
  28. #include <sound/tlv.h>
  29. #include "cs4349.h"
  30. static const struct reg_default cs4349_reg_defaults[] = {
  31. { 2, 0x00 }, /* r02 - Mode Control */
  32. { 3, 0x09 }, /* r03 - Volume, Mixing and Inversion Control */
  33. { 4, 0x81 }, /* r04 - Mute Control */
  34. { 5, 0x00 }, /* r05 - Channel A Volume Control */
  35. { 6, 0x00 }, /* r06 - Channel B Volume Control */
  36. { 7, 0xB1 }, /* r07 - Ramp and Filter Control */
  37. { 8, 0x1C }, /* r08 - Misc. Control */
  38. };
  39. /* Private data for the CS4349 */
  40. struct cs4349_private {
  41. struct regmap *regmap;
  42. struct gpio_desc *reset_gpio;
  43. unsigned int mode;
  44. int rate;
  45. };
  46. static bool cs4349_readable_register(struct device *dev, unsigned int reg)
  47. {
  48. switch (reg) {
  49. case CS4349_CHIPID ... CS4349_MISC:
  50. return true;
  51. default:
  52. return false;
  53. }
  54. }
  55. static bool cs4349_writeable_register(struct device *dev, unsigned int reg)
  56. {
  57. switch (reg) {
  58. case CS4349_MODE ... CS4349_MISC:
  59. return true;
  60. default:
  61. return false;
  62. }
  63. }
  64. static int cs4349_set_dai_fmt(struct snd_soc_dai *codec_dai,
  65. unsigned int format)
  66. {
  67. struct snd_soc_component *component = codec_dai->component;
  68. struct cs4349_private *cs4349 = snd_soc_component_get_drvdata(component);
  69. unsigned int fmt;
  70. fmt = format & SND_SOC_DAIFMT_FORMAT_MASK;
  71. switch (fmt) {
  72. case SND_SOC_DAIFMT_I2S:
  73. case SND_SOC_DAIFMT_LEFT_J:
  74. case SND_SOC_DAIFMT_RIGHT_J:
  75. cs4349->mode = format & SND_SOC_DAIFMT_FORMAT_MASK;
  76. break;
  77. default:
  78. return -EINVAL;
  79. }
  80. return 0;
  81. }
  82. static int cs4349_pcm_hw_params(struct snd_pcm_substream *substream,
  83. struct snd_pcm_hw_params *params,
  84. struct snd_soc_dai *dai)
  85. {
  86. struct snd_soc_component *component = dai->component;
  87. struct cs4349_private *cs4349 = snd_soc_component_get_drvdata(component);
  88. int fmt, ret;
  89. cs4349->rate = params_rate(params);
  90. switch (cs4349->mode) {
  91. case SND_SOC_DAIFMT_I2S:
  92. fmt = DIF_I2S;
  93. break;
  94. case SND_SOC_DAIFMT_LEFT_J:
  95. fmt = DIF_LEFT_JST;
  96. break;
  97. case SND_SOC_DAIFMT_RIGHT_J:
  98. switch (params_width(params)) {
  99. case 16:
  100. fmt = DIF_RGHT_JST16;
  101. break;
  102. case 24:
  103. fmt = DIF_RGHT_JST24;
  104. break;
  105. default:
  106. return -EINVAL;
  107. }
  108. break;
  109. default:
  110. return -EINVAL;
  111. }
  112. ret = snd_soc_component_update_bits(component, CS4349_MODE, DIF_MASK,
  113. MODE_FORMAT(fmt));
  114. if (ret < 0)
  115. return ret;
  116. return 0;
  117. }
  118. static int cs4349_mute(struct snd_soc_dai *dai, int mute, int direction)
  119. {
  120. struct snd_soc_component *component = dai->component;
  121. int reg;
  122. reg = 0;
  123. if (mute)
  124. reg = MUTE_AB_MASK;
  125. return snd_soc_component_update_bits(component, CS4349_MUTE, MUTE_AB_MASK, reg);
  126. }
  127. static DECLARE_TLV_DB_SCALE(dig_tlv, -12750, 50, 0);
  128. static const char * const chan_mix_texts[] = {
  129. "Mute", "MuteA", "MuteA SwapB", "MuteA MonoB", "SwapA MuteB",
  130. "BothR", "Swap", "SwapA MonoB", "MuteB", "Normal", "BothL",
  131. "MonoB", "MonoA MuteB", "MonoA", "MonoA SwapB", "Mono",
  132. /*Normal == Channel A = Left, Channel B = Right*/
  133. };
  134. static const char * const fm_texts[] = {
  135. "Auto", "Single", "Double", "Quad",
  136. };
  137. static const char * const deemph_texts[] = {
  138. "None", "44.1k", "48k", "32k",
  139. };
  140. static const char * const softr_zeroc_texts[] = {
  141. "Immediate", "Zero Cross", "Soft Ramp", "SR on ZC",
  142. };
  143. static int deemph_values[] = {
  144. 0, 4, 8, 12,
  145. };
  146. static int softr_zeroc_values[] = {
  147. 0, 64, 128, 192,
  148. };
  149. static const struct soc_enum chan_mix_enum =
  150. SOC_ENUM_SINGLE(CS4349_VMI, 0,
  151. ARRAY_SIZE(chan_mix_texts),
  152. chan_mix_texts);
  153. static const struct soc_enum fm_mode_enum =
  154. SOC_ENUM_SINGLE(CS4349_MODE, 0,
  155. ARRAY_SIZE(fm_texts),
  156. fm_texts);
  157. static SOC_VALUE_ENUM_SINGLE_DECL(deemph_enum, CS4349_MODE, 0, DEM_MASK,
  158. deemph_texts, deemph_values);
  159. static SOC_VALUE_ENUM_SINGLE_DECL(softr_zeroc_enum, CS4349_RMPFLT, 0,
  160. SR_ZC_MASK, softr_zeroc_texts,
  161. softr_zeroc_values);
  162. static const struct snd_kcontrol_new cs4349_snd_controls[] = {
  163. SOC_DOUBLE_R_TLV("Master Playback Volume",
  164. CS4349_VOLA, CS4349_VOLB, 0, 0xFF, 1, dig_tlv),
  165. SOC_ENUM("Functional Mode", fm_mode_enum),
  166. SOC_ENUM("De-Emphasis Control", deemph_enum),
  167. SOC_ENUM("Soft Ramp Zero Cross Control", softr_zeroc_enum),
  168. SOC_ENUM("Channel Mixer", chan_mix_enum),
  169. SOC_SINGLE("VolA = VolB Switch", CS4349_VMI, 7, 1, 0),
  170. SOC_SINGLE("InvertA Switch", CS4349_VMI, 6, 1, 0),
  171. SOC_SINGLE("InvertB Switch", CS4349_VMI, 5, 1, 0),
  172. SOC_SINGLE("Auto-Mute Switch", CS4349_MUTE, 7, 1, 0),
  173. SOC_SINGLE("MUTEC A = B Switch", CS4349_MUTE, 5, 1, 0),
  174. SOC_SINGLE("Soft Ramp Up Switch", CS4349_RMPFLT, 5, 1, 0),
  175. SOC_SINGLE("Soft Ramp Down Switch", CS4349_RMPFLT, 4, 1, 0),
  176. SOC_SINGLE("Slow Roll Off Filter Switch", CS4349_RMPFLT, 2, 1, 0),
  177. SOC_SINGLE("Freeze Switch", CS4349_MISC, 5, 1, 0),
  178. SOC_SINGLE("Popguard Switch", CS4349_MISC, 4, 1, 0),
  179. };
  180. static const struct snd_soc_dapm_widget cs4349_dapm_widgets[] = {
  181. SND_SOC_DAPM_DAC("HiFi DAC", NULL, SND_SOC_NOPM, 0, 0),
  182. SND_SOC_DAPM_OUTPUT("OutputA"),
  183. SND_SOC_DAPM_OUTPUT("OutputB"),
  184. };
  185. static const struct snd_soc_dapm_route cs4349_routes[] = {
  186. {"DAC Playback", NULL, "OutputA"},
  187. {"DAC Playback", NULL, "OutputB"},
  188. {"OutputA", NULL, "HiFi DAC"},
  189. {"OutputB", NULL, "HiFi DAC"},
  190. };
  191. #define CS4349_PCM_FORMATS (SNDRV_PCM_FMTBIT_S8 | \
  192. SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE | \
  193. SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE | \
  194. SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE | \
  195. SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S24_3BE | \
  196. SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE | \
  197. SNDRV_PCM_FMTBIT_S32_LE)
  198. #define CS4349_PCM_RATES SNDRV_PCM_RATE_8000_192000
  199. static const struct snd_soc_dai_ops cs4349_dai_ops = {
  200. .hw_params = cs4349_pcm_hw_params,
  201. .set_fmt = cs4349_set_dai_fmt,
  202. .mute_stream = cs4349_mute,
  203. .no_capture_mute = 1,
  204. };
  205. static struct snd_soc_dai_driver cs4349_dai = {
  206. .name = "cs4349_hifi",
  207. .playback = {
  208. .stream_name = "DAC Playback",
  209. .channels_min = 1,
  210. .channels_max = 2,
  211. .rates = CS4349_PCM_RATES,
  212. .formats = CS4349_PCM_FORMATS,
  213. },
  214. .ops = &cs4349_dai_ops,
  215. .symmetric_rates = 1,
  216. };
  217. static const struct snd_soc_component_driver soc_component_dev_cs4349 = {
  218. .controls = cs4349_snd_controls,
  219. .num_controls = ARRAY_SIZE(cs4349_snd_controls),
  220. .dapm_widgets = cs4349_dapm_widgets,
  221. .num_dapm_widgets = ARRAY_SIZE(cs4349_dapm_widgets),
  222. .dapm_routes = cs4349_routes,
  223. .num_dapm_routes = ARRAY_SIZE(cs4349_routes),
  224. .idle_bias_on = 1,
  225. .use_pmdown_time = 1,
  226. .endianness = 1,
  227. .non_legacy_dai_naming = 1,
  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 devm_snd_soc_register_component(&client->dev,
  261. &soc_component_dev_cs4349,
  262. &cs4349_dai, 1);
  263. }
  264. static int cs4349_i2c_remove(struct i2c_client *client)
  265. {
  266. struct cs4349_private *cs4349 = i2c_get_clientdata(client);
  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. .pm = &cs4349_runtime_pm,
  316. },
  317. .id_table = cs4349_i2c_id,
  318. .probe = cs4349_i2c_probe,
  319. .remove = cs4349_i2c_remove,
  320. };
  321. module_i2c_driver(cs4349_i2c_driver);
  322. MODULE_AUTHOR("Tim Howe <tim.howe@cirrus.com>");
  323. MODULE_DESCRIPTION("Cirrus Logic CS4349 ALSA SoC Codec Driver");
  324. MODULE_LICENSE("GPL");