max9850.c 8.7 KB

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