max9850.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. /*
  2. * max9850.c -- codec driver for max9850
  3. *
  4. * Copyright (C) 2011 taskit GmbH
  5. *
  6. * Author: Christian Glindkamp <christian.glindkamp@taskit.de>
  7. *
  8. * Initial development of this code was funded by
  9. * MICRONIC Computer Systeme GmbH, http://www.mcsberlin.de/
  10. *
  11. * This program is free software; you can redistribute it and/or modify it
  12. * under the terms of the GNU General Public License as published by the
  13. * Free Software Foundation; either version 2 of the License, or (at your
  14. * option) any later version.
  15. *
  16. */
  17. #include <linux/module.h>
  18. #include <linux/init.h>
  19. #include <linux/i2c.h>
  20. #include <linux/regmap.h>
  21. #include <linux/slab.h>
  22. #include <sound/pcm.h>
  23. #include <sound/pcm_params.h>
  24. #include <sound/soc.h>
  25. #include <sound/tlv.h>
  26. #include "max9850.h"
  27. struct max9850_priv {
  28. struct regmap *regmap;
  29. unsigned int sysclk;
  30. };
  31. /* max9850 register cache */
  32. static const struct reg_default max9850_reg[] = {
  33. { 2, 0x0c },
  34. { 3, 0x00 },
  35. { 4, 0x00 },
  36. { 5, 0x00 },
  37. { 6, 0x00 },
  38. { 7, 0x00 },
  39. { 8, 0x00 },
  40. { 9, 0x00 },
  41. { 10, 0x00 },
  42. };
  43. /* these registers are not used at the moment but provided for the sake of
  44. * completeness */
  45. static bool max9850_volatile_register(struct device *dev, unsigned int reg)
  46. {
  47. switch (reg) {
  48. case MAX9850_STATUSA:
  49. case MAX9850_STATUSB:
  50. return 1;
  51. default:
  52. return 0;
  53. }
  54. }
  55. static const struct regmap_config max9850_regmap = {
  56. .reg_bits = 8,
  57. .val_bits = 8,
  58. .max_register = MAX9850_DIGITAL_AUDIO,
  59. .volatile_reg = max9850_volatile_register,
  60. .cache_type = REGCACHE_RBTREE,
  61. };
  62. static const unsigned int max9850_tlv[] = {
  63. TLV_DB_RANGE_HEAD(4),
  64. 0x18, 0x1f, TLV_DB_SCALE_ITEM(-7450, 400, 0),
  65. 0x20, 0x33, TLV_DB_SCALE_ITEM(-4150, 200, 0),
  66. 0x34, 0x37, TLV_DB_SCALE_ITEM(-150, 100, 0),
  67. 0x38, 0x3f, TLV_DB_SCALE_ITEM(250, 50, 0),
  68. };
  69. static const struct snd_kcontrol_new max9850_controls[] = {
  70. SOC_SINGLE_TLV("Headphone Volume", MAX9850_VOLUME, 0, 0x3f, 1, max9850_tlv),
  71. SOC_SINGLE("Headphone Switch", MAX9850_VOLUME, 7, 1, 1),
  72. SOC_SINGLE("Mono Switch", MAX9850_GENERAL_PURPOSE, 2, 1, 0),
  73. };
  74. static const struct snd_kcontrol_new max9850_mixer_controls[] = {
  75. SOC_DAPM_SINGLE("Line In Switch", MAX9850_ENABLE, 1, 1, 0),
  76. };
  77. static const struct snd_soc_dapm_widget max9850_dapm_widgets[] = {
  78. SND_SOC_DAPM_SUPPLY("Charge Pump 1", MAX9850_ENABLE, 4, 0, NULL, 0),
  79. SND_SOC_DAPM_SUPPLY("Charge Pump 2", MAX9850_ENABLE, 5, 0, NULL, 0),
  80. SND_SOC_DAPM_SUPPLY("MCLK", MAX9850_ENABLE, 6, 0, NULL, 0),
  81. SND_SOC_DAPM_SUPPLY("SHDN", MAX9850_ENABLE, 7, 0, NULL, 0),
  82. SND_SOC_DAPM_MIXER_NAMED_CTL("Output Mixer", MAX9850_ENABLE, 2, 0,
  83. &max9850_mixer_controls[0],
  84. ARRAY_SIZE(max9850_mixer_controls)),
  85. SND_SOC_DAPM_PGA("Headphone Output", MAX9850_ENABLE, 3, 0, NULL, 0),
  86. SND_SOC_DAPM_DAC("DAC", "HiFi Playback", MAX9850_ENABLE, 0, 0),
  87. SND_SOC_DAPM_OUTPUT("OUTL"),
  88. SND_SOC_DAPM_OUTPUT("HPL"),
  89. SND_SOC_DAPM_OUTPUT("OUTR"),
  90. SND_SOC_DAPM_OUTPUT("HPR"),
  91. SND_SOC_DAPM_MIXER("Line Input", SND_SOC_NOPM, 0, 0, NULL, 0),
  92. SND_SOC_DAPM_INPUT("INL"),
  93. SND_SOC_DAPM_INPUT("INR"),
  94. };
  95. static const struct snd_soc_dapm_route max9850_dapm_routes[] = {
  96. /* output mixer */
  97. {"Output Mixer", NULL, "DAC"},
  98. {"Output Mixer", "Line In Switch", "Line Input"},
  99. /* outputs */
  100. {"Headphone Output", NULL, "Output Mixer"},
  101. {"HPL", NULL, "Headphone Output"},
  102. {"HPR", NULL, "Headphone Output"},
  103. {"OUTL", NULL, "Output Mixer"},
  104. {"OUTR", NULL, "Output Mixer"},
  105. /* inputs */
  106. {"Line Input", NULL, "INL"},
  107. {"Line Input", NULL, "INR"},
  108. /* supplies */
  109. {"Output Mixer", NULL, "Charge Pump 1"},
  110. {"Output Mixer", NULL, "Charge Pump 2"},
  111. {"Output Mixer", NULL, "SHDN"},
  112. {"DAC", NULL, "MCLK"},
  113. };
  114. static int max9850_hw_params(struct snd_pcm_substream *substream,
  115. struct snd_pcm_hw_params *params,
  116. struct snd_soc_dai *dai)
  117. {
  118. struct snd_soc_codec *codec = dai->codec;
  119. struct max9850_priv *max9850 = snd_soc_codec_get_drvdata(codec);
  120. u64 lrclk_div;
  121. u8 sf, da;
  122. if (!max9850->sysclk)
  123. return -EINVAL;
  124. /* lrclk_div = 2^22 * rate / iclk with iclk = mclk / sf */
  125. sf = (snd_soc_read(codec, MAX9850_CLOCK) >> 2) + 1;
  126. lrclk_div = (1 << 22);
  127. lrclk_div *= params_rate(params);
  128. lrclk_div *= sf;
  129. do_div(lrclk_div, max9850->sysclk);
  130. snd_soc_write(codec, MAX9850_LRCLK_MSB, (lrclk_div >> 8) & 0x7f);
  131. snd_soc_write(codec, MAX9850_LRCLK_LSB, lrclk_div & 0xff);
  132. switch (params_format(params)) {
  133. case SNDRV_PCM_FORMAT_S16_LE:
  134. da = 0;
  135. break;
  136. case SNDRV_PCM_FORMAT_S20_3LE:
  137. da = 0x2;
  138. break;
  139. case SNDRV_PCM_FORMAT_S24_LE:
  140. da = 0x3;
  141. break;
  142. default:
  143. return -EINVAL;
  144. }
  145. snd_soc_update_bits(codec, MAX9850_DIGITAL_AUDIO, 0x3, da);
  146. return 0;
  147. }
  148. static int max9850_set_dai_sysclk(struct snd_soc_dai *codec_dai,
  149. int clk_id, unsigned int freq, int dir)
  150. {
  151. struct snd_soc_codec *codec = codec_dai->codec;
  152. struct max9850_priv *max9850 = snd_soc_codec_get_drvdata(codec);
  153. /* calculate mclk -> iclk divider */
  154. if (freq <= 13000000)
  155. snd_soc_write(codec, MAX9850_CLOCK, 0x0);
  156. else if (freq <= 26000000)
  157. snd_soc_write(codec, MAX9850_CLOCK, 0x4);
  158. else if (freq <= 40000000)
  159. snd_soc_write(codec, MAX9850_CLOCK, 0x8);
  160. else
  161. return -EINVAL;
  162. max9850->sysclk = freq;
  163. return 0;
  164. }
  165. static int max9850_set_dai_fmt(struct snd_soc_dai *codec_dai, unsigned int fmt)
  166. {
  167. struct snd_soc_codec *codec = codec_dai->codec;
  168. u8 da = 0;
  169. /* set master/slave audio interface */
  170. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  171. case SND_SOC_DAIFMT_CBM_CFM:
  172. da |= MAX9850_MASTER;
  173. break;
  174. case SND_SOC_DAIFMT_CBS_CFS:
  175. break;
  176. default:
  177. return -EINVAL;
  178. }
  179. /* interface format */
  180. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  181. case SND_SOC_DAIFMT_I2S:
  182. da |= MAX9850_DLY;
  183. break;
  184. case SND_SOC_DAIFMT_RIGHT_J:
  185. da |= MAX9850_RTJ;
  186. break;
  187. case SND_SOC_DAIFMT_LEFT_J:
  188. break;
  189. default:
  190. return -EINVAL;
  191. }
  192. /* clock inversion */
  193. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  194. case SND_SOC_DAIFMT_NB_NF:
  195. break;
  196. case SND_SOC_DAIFMT_IB_IF:
  197. da |= MAX9850_BCINV | MAX9850_INV;
  198. break;
  199. case SND_SOC_DAIFMT_IB_NF:
  200. da |= MAX9850_BCINV;
  201. break;
  202. case SND_SOC_DAIFMT_NB_IF:
  203. da |= MAX9850_INV;
  204. break;
  205. default:
  206. return -EINVAL;
  207. }
  208. /* set da */
  209. snd_soc_write(codec, MAX9850_DIGITAL_AUDIO, da);
  210. return 0;
  211. }
  212. static int max9850_set_bias_level(struct snd_soc_codec *codec,
  213. enum snd_soc_bias_level level)
  214. {
  215. struct max9850_priv *max9850 = snd_soc_codec_get_drvdata(codec);
  216. int ret;
  217. switch (level) {
  218. case SND_SOC_BIAS_ON:
  219. break;
  220. case SND_SOC_BIAS_PREPARE:
  221. break;
  222. case SND_SOC_BIAS_STANDBY:
  223. if (codec->dapm.bias_level == SND_SOC_BIAS_OFF) {
  224. ret = regcache_sync(max9850->regmap);
  225. if (ret) {
  226. dev_err(codec->dev,
  227. "Failed to sync cache: %d\n", ret);
  228. return ret;
  229. }
  230. }
  231. break;
  232. case SND_SOC_BIAS_OFF:
  233. break;
  234. }
  235. codec->dapm.bias_level = level;
  236. return 0;
  237. }
  238. #define MAX9850_RATES SNDRV_PCM_RATE_8000_48000
  239. #define MAX9850_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
  240. SNDRV_PCM_FMTBIT_S24_LE)
  241. static const struct snd_soc_dai_ops max9850_dai_ops = {
  242. .hw_params = max9850_hw_params,
  243. .set_sysclk = max9850_set_dai_sysclk,
  244. .set_fmt = max9850_set_dai_fmt,
  245. };
  246. static struct snd_soc_dai_driver max9850_dai = {
  247. .name = "max9850-hifi",
  248. .playback = {
  249. .stream_name = "Playback",
  250. .channels_min = 1,
  251. .channels_max = 2,
  252. .rates = MAX9850_RATES,
  253. .formats = MAX9850_FORMATS
  254. },
  255. .ops = &max9850_dai_ops,
  256. };
  257. #ifdef CONFIG_PM
  258. static int max9850_suspend(struct snd_soc_codec *codec)
  259. {
  260. max9850_set_bias_level(codec, SND_SOC_BIAS_OFF);
  261. return 0;
  262. }
  263. static int max9850_resume(struct snd_soc_codec *codec)
  264. {
  265. max9850_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  266. return 0;
  267. }
  268. #else
  269. #define max9850_suspend NULL
  270. #define max9850_resume NULL
  271. #endif
  272. static int max9850_probe(struct snd_soc_codec *codec)
  273. {
  274. int ret;
  275. ret = snd_soc_codec_set_cache_io(codec, 8, 8, SND_SOC_REGMAP);
  276. if (ret < 0) {
  277. dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
  278. return ret;
  279. }
  280. /* enable zero-detect */
  281. snd_soc_update_bits(codec, MAX9850_GENERAL_PURPOSE, 1, 1);
  282. /* enable slew-rate control */
  283. snd_soc_update_bits(codec, MAX9850_VOLUME, 0x40, 0x40);
  284. /* set slew-rate 125ms */
  285. snd_soc_update_bits(codec, MAX9850_CHARGE_PUMP, 0xff, 0xc0);
  286. return 0;
  287. }
  288. static struct snd_soc_codec_driver soc_codec_dev_max9850 = {
  289. .probe = max9850_probe,
  290. .suspend = max9850_suspend,
  291. .resume = max9850_resume,
  292. .set_bias_level = max9850_set_bias_level,
  293. .controls = max9850_controls,
  294. .num_controls = ARRAY_SIZE(max9850_controls),
  295. .dapm_widgets = max9850_dapm_widgets,
  296. .num_dapm_widgets = ARRAY_SIZE(max9850_dapm_widgets),
  297. .dapm_routes = max9850_dapm_routes,
  298. .num_dapm_routes = ARRAY_SIZE(max9850_dapm_routes),
  299. };
  300. static int max9850_i2c_probe(struct i2c_client *i2c,
  301. const struct i2c_device_id *id)
  302. {
  303. struct max9850_priv *max9850;
  304. int ret;
  305. max9850 = devm_kzalloc(&i2c->dev, sizeof(struct max9850_priv),
  306. GFP_KERNEL);
  307. if (max9850 == NULL)
  308. return -ENOMEM;
  309. max9850->regmap = devm_regmap_init_i2c(i2c, &max9850_regmap);
  310. if (IS_ERR(max9850->regmap))
  311. return PTR_ERR(max9850->regmap);
  312. i2c_set_clientdata(i2c, max9850);
  313. ret = snd_soc_register_codec(&i2c->dev,
  314. &soc_codec_dev_max9850, &max9850_dai, 1);
  315. return ret;
  316. }
  317. static int max9850_i2c_remove(struct i2c_client *client)
  318. {
  319. snd_soc_unregister_codec(&client->dev);
  320. return 0;
  321. }
  322. static const struct i2c_device_id max9850_i2c_id[] = {
  323. { "max9850", 0 },
  324. { }
  325. };
  326. MODULE_DEVICE_TABLE(i2c, max9850_i2c_id);
  327. static struct i2c_driver max9850_i2c_driver = {
  328. .driver = {
  329. .name = "max9850",
  330. .owner = THIS_MODULE,
  331. },
  332. .probe = max9850_i2c_probe,
  333. .remove = max9850_i2c_remove,
  334. .id_table = max9850_i2c_id,
  335. };
  336. module_i2c_driver(max9850_i2c_driver);
  337. MODULE_AUTHOR("Christian Glindkamp <christian.glindkamp@taskit.de>");
  338. MODULE_DESCRIPTION("ASoC MAX9850 codec driver");
  339. MODULE_LICENSE("GPL");