max9850.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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 DECLARE_TLV_DB_RANGE(max9850_tlv,
  63. 0x18, 0x1f, TLV_DB_SCALE_ITEM(-7450, 400, 0),
  64. 0x20, 0x33, TLV_DB_SCALE_ITEM(-4150, 200, 0),
  65. 0x34, 0x37, TLV_DB_SCALE_ITEM(-150, 100, 0),
  66. 0x38, 0x3f, TLV_DB_SCALE_ITEM(250, 50, 0)
  67. );
  68. static const struct snd_kcontrol_new max9850_controls[] = {
  69. SOC_SINGLE_TLV("Headphone Volume", MAX9850_VOLUME, 0, 0x3f, 1, max9850_tlv),
  70. SOC_SINGLE("Headphone Switch", MAX9850_VOLUME, 7, 1, 1),
  71. SOC_SINGLE("Mono Switch", MAX9850_GENERAL_PURPOSE, 2, 1, 0),
  72. };
  73. static const struct snd_kcontrol_new max9850_mixer_controls[] = {
  74. SOC_DAPM_SINGLE("Line In Switch", MAX9850_ENABLE, 1, 1, 0),
  75. };
  76. static const struct snd_soc_dapm_widget max9850_dapm_widgets[] = {
  77. SND_SOC_DAPM_SUPPLY("Charge Pump 1", MAX9850_ENABLE, 4, 0, NULL, 0),
  78. SND_SOC_DAPM_SUPPLY("Charge Pump 2", MAX9850_ENABLE, 5, 0, NULL, 0),
  79. SND_SOC_DAPM_SUPPLY("MCLK", MAX9850_ENABLE, 6, 0, NULL, 0),
  80. SND_SOC_DAPM_SUPPLY("SHDN", MAX9850_ENABLE, 7, 0, NULL, 0),
  81. SND_SOC_DAPM_MIXER_NAMED_CTL("Output Mixer", MAX9850_ENABLE, 2, 0,
  82. &max9850_mixer_controls[0],
  83. ARRAY_SIZE(max9850_mixer_controls)),
  84. SND_SOC_DAPM_PGA("Headphone Output", MAX9850_ENABLE, 3, 0, NULL, 0),
  85. SND_SOC_DAPM_DAC("DAC", "HiFi Playback", MAX9850_ENABLE, 0, 0),
  86. SND_SOC_DAPM_OUTPUT("OUTL"),
  87. SND_SOC_DAPM_OUTPUT("HPL"),
  88. SND_SOC_DAPM_OUTPUT("OUTR"),
  89. SND_SOC_DAPM_OUTPUT("HPR"),
  90. SND_SOC_DAPM_MIXER("Line Input", SND_SOC_NOPM, 0, 0, NULL, 0),
  91. SND_SOC_DAPM_INPUT("INL"),
  92. SND_SOC_DAPM_INPUT("INR"),
  93. };
  94. static const struct snd_soc_dapm_route max9850_dapm_routes[] = {
  95. /* output mixer */
  96. {"Output Mixer", NULL, "DAC"},
  97. {"Output Mixer", "Line In Switch", "Line Input"},
  98. /* outputs */
  99. {"Headphone Output", NULL, "Output Mixer"},
  100. {"HPL", NULL, "Headphone Output"},
  101. {"HPR", NULL, "Headphone Output"},
  102. {"OUTL", NULL, "Output Mixer"},
  103. {"OUTR", NULL, "Output Mixer"},
  104. /* inputs */
  105. {"Line Input", NULL, "INL"},
  106. {"Line Input", NULL, "INR"},
  107. /* supplies */
  108. {"Output Mixer", NULL, "Charge Pump 1"},
  109. {"Output Mixer", NULL, "Charge Pump 2"},
  110. {"Output Mixer", NULL, "SHDN"},
  111. {"DAC", NULL, "MCLK"},
  112. };
  113. static int max9850_hw_params(struct snd_pcm_substream *substream,
  114. struct snd_pcm_hw_params *params,
  115. struct snd_soc_dai *dai)
  116. {
  117. struct snd_soc_codec *codec = dai->codec;
  118. struct max9850_priv *max9850 = snd_soc_codec_get_drvdata(codec);
  119. u64 lrclk_div;
  120. u8 sf, da;
  121. if (!max9850->sysclk)
  122. return -EINVAL;
  123. /* lrclk_div = 2^22 * rate / iclk with iclk = mclk / sf */
  124. sf = (snd_soc_read(codec, MAX9850_CLOCK) >> 2) + 1;
  125. lrclk_div = (1 << 22);
  126. lrclk_div *= params_rate(params);
  127. lrclk_div *= sf;
  128. do_div(lrclk_div, max9850->sysclk);
  129. snd_soc_write(codec, MAX9850_LRCLK_MSB, (lrclk_div >> 8) & 0x7f);
  130. snd_soc_write(codec, MAX9850_LRCLK_LSB, lrclk_div & 0xff);
  131. switch (params_width(params)) {
  132. case 16:
  133. da = 0;
  134. break;
  135. case 20:
  136. da = 0x2;
  137. break;
  138. case 24:
  139. da = 0x3;
  140. break;
  141. default:
  142. return -EINVAL;
  143. }
  144. snd_soc_update_bits(codec, MAX9850_DIGITAL_AUDIO, 0x3, da);
  145. return 0;
  146. }
  147. static int max9850_set_dai_sysclk(struct snd_soc_dai *codec_dai,
  148. int clk_id, unsigned int freq, int dir)
  149. {
  150. struct snd_soc_codec *codec = codec_dai->codec;
  151. struct max9850_priv *max9850 = snd_soc_codec_get_drvdata(codec);
  152. /* calculate mclk -> iclk divider */
  153. if (freq <= 13000000)
  154. snd_soc_write(codec, MAX9850_CLOCK, 0x0);
  155. else if (freq <= 26000000)
  156. snd_soc_write(codec, MAX9850_CLOCK, 0x4);
  157. else if (freq <= 40000000)
  158. snd_soc_write(codec, MAX9850_CLOCK, 0x8);
  159. else
  160. return -EINVAL;
  161. max9850->sysclk = freq;
  162. return 0;
  163. }
  164. static int max9850_set_dai_fmt(struct snd_soc_dai *codec_dai, unsigned int fmt)
  165. {
  166. struct snd_soc_codec *codec = codec_dai->codec;
  167. u8 da = 0;
  168. /* set master/slave audio interface */
  169. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  170. case SND_SOC_DAIFMT_CBM_CFM:
  171. da |= MAX9850_MASTER;
  172. break;
  173. case SND_SOC_DAIFMT_CBS_CFS:
  174. break;
  175. default:
  176. return -EINVAL;
  177. }
  178. /* interface format */
  179. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  180. case SND_SOC_DAIFMT_I2S:
  181. da |= MAX9850_DLY;
  182. break;
  183. case SND_SOC_DAIFMT_RIGHT_J:
  184. da |= MAX9850_RTJ;
  185. break;
  186. case SND_SOC_DAIFMT_LEFT_J:
  187. break;
  188. default:
  189. return -EINVAL;
  190. }
  191. /* clock inversion */
  192. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  193. case SND_SOC_DAIFMT_NB_NF:
  194. break;
  195. case SND_SOC_DAIFMT_IB_IF:
  196. da |= MAX9850_BCINV | MAX9850_INV;
  197. break;
  198. case SND_SOC_DAIFMT_IB_NF:
  199. da |= MAX9850_BCINV;
  200. break;
  201. case SND_SOC_DAIFMT_NB_IF:
  202. da |= MAX9850_INV;
  203. break;
  204. default:
  205. return -EINVAL;
  206. }
  207. /* set da */
  208. snd_soc_write(codec, MAX9850_DIGITAL_AUDIO, da);
  209. return 0;
  210. }
  211. static int max9850_set_bias_level(struct snd_soc_codec *codec,
  212. enum snd_soc_bias_level level)
  213. {
  214. struct max9850_priv *max9850 = snd_soc_codec_get_drvdata(codec);
  215. int ret;
  216. switch (level) {
  217. case SND_SOC_BIAS_ON:
  218. break;
  219. case SND_SOC_BIAS_PREPARE:
  220. break;
  221. case SND_SOC_BIAS_STANDBY:
  222. if (snd_soc_codec_get_bias_level(codec) == SND_SOC_BIAS_OFF) {
  223. ret = regcache_sync(max9850->regmap);
  224. if (ret) {
  225. dev_err(codec->dev,
  226. "Failed to sync cache: %d\n", ret);
  227. return ret;
  228. }
  229. }
  230. break;
  231. case SND_SOC_BIAS_OFF:
  232. break;
  233. }
  234. return 0;
  235. }
  236. #define MAX9850_RATES SNDRV_PCM_RATE_8000_48000
  237. #define MAX9850_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
  238. SNDRV_PCM_FMTBIT_S24_LE)
  239. static const struct snd_soc_dai_ops max9850_dai_ops = {
  240. .hw_params = max9850_hw_params,
  241. .set_sysclk = max9850_set_dai_sysclk,
  242. .set_fmt = max9850_set_dai_fmt,
  243. };
  244. static struct snd_soc_dai_driver max9850_dai = {
  245. .name = "max9850-hifi",
  246. .playback = {
  247. .stream_name = "Playback",
  248. .channels_min = 1,
  249. .channels_max = 2,
  250. .rates = MAX9850_RATES,
  251. .formats = MAX9850_FORMATS
  252. },
  253. .ops = &max9850_dai_ops,
  254. };
  255. static int max9850_probe(struct snd_soc_codec *codec)
  256. {
  257. /* enable zero-detect */
  258. snd_soc_update_bits(codec, MAX9850_GENERAL_PURPOSE, 1, 1);
  259. /* enable slew-rate control */
  260. snd_soc_update_bits(codec, MAX9850_VOLUME, 0x40, 0x40);
  261. /* set slew-rate 125ms */
  262. snd_soc_update_bits(codec, MAX9850_CHARGE_PUMP, 0xff, 0xc0);
  263. return 0;
  264. }
  265. static struct snd_soc_codec_driver soc_codec_dev_max9850 = {
  266. .probe = max9850_probe,
  267. .set_bias_level = max9850_set_bias_level,
  268. .suspend_bias_off = true,
  269. .component_driver = {
  270. .controls = max9850_controls,
  271. .num_controls = ARRAY_SIZE(max9850_controls),
  272. .dapm_widgets = max9850_dapm_widgets,
  273. .num_dapm_widgets = ARRAY_SIZE(max9850_dapm_widgets),
  274. .dapm_routes = max9850_dapm_routes,
  275. .num_dapm_routes = ARRAY_SIZE(max9850_dapm_routes),
  276. },
  277. };
  278. static int max9850_i2c_probe(struct i2c_client *i2c,
  279. const struct i2c_device_id *id)
  280. {
  281. struct max9850_priv *max9850;
  282. int ret;
  283. max9850 = devm_kzalloc(&i2c->dev, sizeof(struct max9850_priv),
  284. GFP_KERNEL);
  285. if (max9850 == NULL)
  286. return -ENOMEM;
  287. max9850->regmap = devm_regmap_init_i2c(i2c, &max9850_regmap);
  288. if (IS_ERR(max9850->regmap))
  289. return PTR_ERR(max9850->regmap);
  290. i2c_set_clientdata(i2c, max9850);
  291. ret = snd_soc_register_codec(&i2c->dev,
  292. &soc_codec_dev_max9850, &max9850_dai, 1);
  293. return ret;
  294. }
  295. static int max9850_i2c_remove(struct i2c_client *client)
  296. {
  297. snd_soc_unregister_codec(&client->dev);
  298. return 0;
  299. }
  300. static const struct i2c_device_id max9850_i2c_id[] = {
  301. { "max9850", 0 },
  302. { }
  303. };
  304. MODULE_DEVICE_TABLE(i2c, max9850_i2c_id);
  305. static struct i2c_driver max9850_i2c_driver = {
  306. .driver = {
  307. .name = "max9850",
  308. },
  309. .probe = max9850_i2c_probe,
  310. .remove = max9850_i2c_remove,
  311. .id_table = max9850_i2c_id,
  312. };
  313. module_i2c_driver(max9850_i2c_driver);
  314. MODULE_AUTHOR("Christian Glindkamp <christian.glindkamp@taskit.de>");
  315. MODULE_DESCRIPTION("ASoC MAX9850 codec driver");
  316. MODULE_LICENSE("GPL");