ak4535.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. /*
  2. * ak4535.c -- AK4535 ALSA Soc Audio driver
  3. *
  4. * Copyright 2005 Openedhand Ltd.
  5. *
  6. * Author: Richard Purdie <richard@openedhand.com>
  7. *
  8. * Based on wm8753.c by Liam Girdwood
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation.
  13. */
  14. #include <linux/module.h>
  15. #include <linux/moduleparam.h>
  16. #include <linux/init.h>
  17. #include <linux/delay.h>
  18. #include <linux/pm.h>
  19. #include <linux/i2c.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/initval.h>
  27. #include "ak4535.h"
  28. /* codec private data */
  29. struct ak4535_priv {
  30. struct regmap *regmap;
  31. unsigned int sysclk;
  32. };
  33. /*
  34. * ak4535 register cache
  35. */
  36. static const struct reg_default ak4535_reg_defaults[] = {
  37. { 0, 0x00 },
  38. { 1, 0x80 },
  39. { 2, 0x00 },
  40. { 3, 0x03 },
  41. { 4, 0x02 },
  42. { 5, 0x00 },
  43. { 6, 0x11 },
  44. { 7, 0x01 },
  45. { 8, 0x00 },
  46. { 9, 0x40 },
  47. { 10, 0x36 },
  48. { 11, 0x10 },
  49. { 12, 0x00 },
  50. { 13, 0x00 },
  51. { 14, 0x57 },
  52. };
  53. static bool ak4535_volatile(struct device *dev, unsigned int reg)
  54. {
  55. switch (reg) {
  56. case AK4535_STATUS:
  57. return true;
  58. default:
  59. return false;
  60. }
  61. }
  62. static const char *ak4535_mono_gain[] = {"+6dB", "-17dB"};
  63. static const char *ak4535_mono_out[] = {"(L + R)/2", "Hi-Z"};
  64. static const char *ak4535_hp_out[] = {"Stereo", "Mono"};
  65. static const char *ak4535_deemp[] = {"44.1kHz", "Off", "48kHz", "32kHz"};
  66. static const char *ak4535_mic_select[] = {"Internal", "External"};
  67. static const struct soc_enum ak4535_enum[] = {
  68. SOC_ENUM_SINGLE(AK4535_SIG1, 7, 2, ak4535_mono_gain),
  69. SOC_ENUM_SINGLE(AK4535_SIG1, 6, 2, ak4535_mono_out),
  70. SOC_ENUM_SINGLE(AK4535_MODE2, 2, 2, ak4535_hp_out),
  71. SOC_ENUM_SINGLE(AK4535_DAC, 0, 4, ak4535_deemp),
  72. SOC_ENUM_SINGLE(AK4535_MIC, 1, 2, ak4535_mic_select),
  73. };
  74. static const struct snd_kcontrol_new ak4535_snd_controls[] = {
  75. SOC_SINGLE("ALC2 Switch", AK4535_SIG1, 1, 1, 0),
  76. SOC_ENUM("Mono 1 Output", ak4535_enum[1]),
  77. SOC_ENUM("Mono 1 Gain", ak4535_enum[0]),
  78. SOC_ENUM("Headphone Output", ak4535_enum[2]),
  79. SOC_ENUM("Playback Deemphasis", ak4535_enum[3]),
  80. SOC_SINGLE("Bass Volume", AK4535_DAC, 2, 3, 0),
  81. SOC_SINGLE("Mic Boost (+20dB) Switch", AK4535_MIC, 0, 1, 0),
  82. SOC_ENUM("Mic Select", ak4535_enum[4]),
  83. SOC_SINGLE("ALC Operation Time", AK4535_TIMER, 0, 3, 0),
  84. SOC_SINGLE("ALC Recovery Time", AK4535_TIMER, 2, 3, 0),
  85. SOC_SINGLE("ALC ZC Time", AK4535_TIMER, 4, 3, 0),
  86. SOC_SINGLE("ALC 1 Switch", AK4535_ALC1, 5, 1, 0),
  87. SOC_SINGLE("ALC 2 Switch", AK4535_ALC1, 6, 1, 0),
  88. SOC_SINGLE("ALC Volume", AK4535_ALC2, 0, 127, 0),
  89. SOC_SINGLE("Capture Volume", AK4535_PGA, 0, 127, 0),
  90. SOC_SINGLE("Left Playback Volume", AK4535_LATT, 0, 127, 1),
  91. SOC_SINGLE("Right Playback Volume", AK4535_RATT, 0, 127, 1),
  92. SOC_SINGLE("AUX Bypass Volume", AK4535_VOL, 0, 15, 0),
  93. SOC_SINGLE("Mic Sidetone Volume", AK4535_VOL, 4, 7, 0),
  94. };
  95. /* Mono 1 Mixer */
  96. static const struct snd_kcontrol_new ak4535_mono1_mixer_controls[] = {
  97. SOC_DAPM_SINGLE("Mic Sidetone Switch", AK4535_SIG1, 4, 1, 0),
  98. SOC_DAPM_SINGLE("Mono Playback Switch", AK4535_SIG1, 5, 1, 0),
  99. };
  100. /* Stereo Mixer */
  101. static const struct snd_kcontrol_new ak4535_stereo_mixer_controls[] = {
  102. SOC_DAPM_SINGLE("Mic Sidetone Switch", AK4535_SIG2, 4, 1, 0),
  103. SOC_DAPM_SINGLE("Playback Switch", AK4535_SIG2, 7, 1, 0),
  104. SOC_DAPM_SINGLE("Aux Bypass Switch", AK4535_SIG2, 5, 1, 0),
  105. };
  106. /* Input Mixer */
  107. static const struct snd_kcontrol_new ak4535_input_mixer_controls[] = {
  108. SOC_DAPM_SINGLE("Mic Capture Switch", AK4535_MIC, 2, 1, 0),
  109. SOC_DAPM_SINGLE("Aux Capture Switch", AK4535_MIC, 5, 1, 0),
  110. };
  111. /* Input mux */
  112. static const struct snd_kcontrol_new ak4535_input_mux_control =
  113. SOC_DAPM_ENUM("Input Select", ak4535_enum[4]);
  114. /* HP L switch */
  115. static const struct snd_kcontrol_new ak4535_hpl_control =
  116. SOC_DAPM_SINGLE("Switch", AK4535_SIG2, 1, 1, 1);
  117. /* HP R switch */
  118. static const struct snd_kcontrol_new ak4535_hpr_control =
  119. SOC_DAPM_SINGLE("Switch", AK4535_SIG2, 0, 1, 1);
  120. /* mono 2 switch */
  121. static const struct snd_kcontrol_new ak4535_mono2_control =
  122. SOC_DAPM_SINGLE("Switch", AK4535_SIG1, 0, 1, 0);
  123. /* Line out switch */
  124. static const struct snd_kcontrol_new ak4535_line_control =
  125. SOC_DAPM_SINGLE("Switch", AK4535_SIG2, 6, 1, 0);
  126. /* ak4535 dapm widgets */
  127. static const struct snd_soc_dapm_widget ak4535_dapm_widgets[] = {
  128. SND_SOC_DAPM_MIXER("Stereo Mixer", SND_SOC_NOPM, 0, 0,
  129. &ak4535_stereo_mixer_controls[0],
  130. ARRAY_SIZE(ak4535_stereo_mixer_controls)),
  131. SND_SOC_DAPM_MIXER("Mono1 Mixer", SND_SOC_NOPM, 0, 0,
  132. &ak4535_mono1_mixer_controls[0],
  133. ARRAY_SIZE(ak4535_mono1_mixer_controls)),
  134. SND_SOC_DAPM_MIXER("Input Mixer", SND_SOC_NOPM, 0, 0,
  135. &ak4535_input_mixer_controls[0],
  136. ARRAY_SIZE(ak4535_input_mixer_controls)),
  137. SND_SOC_DAPM_MUX("Input Mux", SND_SOC_NOPM, 0, 0,
  138. &ak4535_input_mux_control),
  139. SND_SOC_DAPM_DAC("DAC", "Playback", AK4535_PM2, 0, 0),
  140. SND_SOC_DAPM_SWITCH("Mono 2 Enable", SND_SOC_NOPM, 0, 0,
  141. &ak4535_mono2_control),
  142. /* speaker powersave bit */
  143. SND_SOC_DAPM_PGA("Speaker Enable", AK4535_MODE2, 0, 0, NULL, 0),
  144. SND_SOC_DAPM_SWITCH("Line Out Enable", SND_SOC_NOPM, 0, 0,
  145. &ak4535_line_control),
  146. SND_SOC_DAPM_SWITCH("Left HP Enable", SND_SOC_NOPM, 0, 0,
  147. &ak4535_hpl_control),
  148. SND_SOC_DAPM_SWITCH("Right HP Enable", SND_SOC_NOPM, 0, 0,
  149. &ak4535_hpr_control),
  150. SND_SOC_DAPM_OUTPUT("LOUT"),
  151. SND_SOC_DAPM_OUTPUT("HPL"),
  152. SND_SOC_DAPM_OUTPUT("ROUT"),
  153. SND_SOC_DAPM_OUTPUT("HPR"),
  154. SND_SOC_DAPM_OUTPUT("SPP"),
  155. SND_SOC_DAPM_OUTPUT("SPN"),
  156. SND_SOC_DAPM_OUTPUT("MOUT1"),
  157. SND_SOC_DAPM_OUTPUT("MOUT2"),
  158. SND_SOC_DAPM_OUTPUT("MICOUT"),
  159. SND_SOC_DAPM_ADC("ADC", "Capture", AK4535_PM1, 0, 0),
  160. SND_SOC_DAPM_PGA("Spk Amp", AK4535_PM2, 3, 0, NULL, 0),
  161. SND_SOC_DAPM_PGA("HP R Amp", AK4535_PM2, 1, 0, NULL, 0),
  162. SND_SOC_DAPM_PGA("HP L Amp", AK4535_PM2, 2, 0, NULL, 0),
  163. SND_SOC_DAPM_PGA("Mic", AK4535_PM1, 1, 0, NULL, 0),
  164. SND_SOC_DAPM_PGA("Line Out", AK4535_PM1, 4, 0, NULL, 0),
  165. SND_SOC_DAPM_PGA("Mono Out", AK4535_PM1, 3, 0, NULL, 0),
  166. SND_SOC_DAPM_PGA("AUX In", AK4535_PM1, 2, 0, NULL, 0),
  167. SND_SOC_DAPM_MICBIAS("Mic Int Bias", AK4535_MIC, 3, 0),
  168. SND_SOC_DAPM_MICBIAS("Mic Ext Bias", AK4535_MIC, 4, 0),
  169. SND_SOC_DAPM_INPUT("MICIN"),
  170. SND_SOC_DAPM_INPUT("MICEXT"),
  171. SND_SOC_DAPM_INPUT("AUX"),
  172. SND_SOC_DAPM_INPUT("MIN"),
  173. SND_SOC_DAPM_INPUT("AIN"),
  174. };
  175. static const struct snd_soc_dapm_route ak4535_audio_map[] = {
  176. /*stereo mixer */
  177. {"Stereo Mixer", "Playback Switch", "DAC"},
  178. {"Stereo Mixer", "Mic Sidetone Switch", "Mic"},
  179. {"Stereo Mixer", "Aux Bypass Switch", "AUX In"},
  180. /* mono1 mixer */
  181. {"Mono1 Mixer", "Mic Sidetone Switch", "Mic"},
  182. {"Mono1 Mixer", "Mono Playback Switch", "DAC"},
  183. /* Mic */
  184. {"Mic", NULL, "AIN"},
  185. {"Input Mux", "Internal", "Mic Int Bias"},
  186. {"Input Mux", "External", "Mic Ext Bias"},
  187. {"Mic Int Bias", NULL, "MICIN"},
  188. {"Mic Ext Bias", NULL, "MICEXT"},
  189. {"MICOUT", NULL, "Input Mux"},
  190. /* line out */
  191. {"LOUT", NULL, "Line Out Enable"},
  192. {"ROUT", NULL, "Line Out Enable"},
  193. {"Line Out Enable", "Switch", "Line Out"},
  194. {"Line Out", NULL, "Stereo Mixer"},
  195. /* mono1 out */
  196. {"MOUT1", NULL, "Mono Out"},
  197. {"Mono Out", NULL, "Mono1 Mixer"},
  198. /* left HP */
  199. {"HPL", NULL, "Left HP Enable"},
  200. {"Left HP Enable", "Switch", "HP L Amp"},
  201. {"HP L Amp", NULL, "Stereo Mixer"},
  202. /* right HP */
  203. {"HPR", NULL, "Right HP Enable"},
  204. {"Right HP Enable", "Switch", "HP R Amp"},
  205. {"HP R Amp", NULL, "Stereo Mixer"},
  206. /* speaker */
  207. {"SPP", NULL, "Speaker Enable"},
  208. {"SPN", NULL, "Speaker Enable"},
  209. {"Speaker Enable", "Switch", "Spk Amp"},
  210. {"Spk Amp", NULL, "MIN"},
  211. /* mono 2 */
  212. {"MOUT2", NULL, "Mono 2 Enable"},
  213. {"Mono 2 Enable", "Switch", "Stereo Mixer"},
  214. /* Aux In */
  215. {"Aux In", NULL, "AUX"},
  216. /* ADC */
  217. {"ADC", NULL, "Input Mixer"},
  218. {"Input Mixer", "Mic Capture Switch", "Mic"},
  219. {"Input Mixer", "Aux Capture Switch", "Aux In"},
  220. };
  221. static int ak4535_set_dai_sysclk(struct snd_soc_dai *codec_dai,
  222. int clk_id, unsigned int freq, int dir)
  223. {
  224. struct snd_soc_codec *codec = codec_dai->codec;
  225. struct ak4535_priv *ak4535 = snd_soc_codec_get_drvdata(codec);
  226. ak4535->sysclk = freq;
  227. return 0;
  228. }
  229. static int ak4535_hw_params(struct snd_pcm_substream *substream,
  230. struct snd_pcm_hw_params *params,
  231. struct snd_soc_dai *dai)
  232. {
  233. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  234. struct snd_soc_codec *codec = rtd->codec;
  235. struct ak4535_priv *ak4535 = snd_soc_codec_get_drvdata(codec);
  236. u8 mode2 = snd_soc_read(codec, AK4535_MODE2) & ~(0x3 << 5);
  237. int rate = params_rate(params), fs = 256;
  238. if (rate)
  239. fs = ak4535->sysclk / rate;
  240. /* set fs */
  241. switch (fs) {
  242. case 1024:
  243. mode2 |= (0x2 << 5);
  244. break;
  245. case 512:
  246. mode2 |= (0x1 << 5);
  247. break;
  248. case 256:
  249. break;
  250. }
  251. /* set rate */
  252. snd_soc_write(codec, AK4535_MODE2, mode2);
  253. return 0;
  254. }
  255. static int ak4535_set_dai_fmt(struct snd_soc_dai *codec_dai,
  256. unsigned int fmt)
  257. {
  258. struct snd_soc_codec *codec = codec_dai->codec;
  259. u8 mode1 = 0;
  260. /* interface format */
  261. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  262. case SND_SOC_DAIFMT_I2S:
  263. mode1 = 0x0002;
  264. break;
  265. case SND_SOC_DAIFMT_LEFT_J:
  266. mode1 = 0x0001;
  267. break;
  268. default:
  269. return -EINVAL;
  270. }
  271. /* use 32 fs for BCLK to save power */
  272. mode1 |= 0x4;
  273. snd_soc_write(codec, AK4535_MODE1, mode1);
  274. return 0;
  275. }
  276. static int ak4535_mute(struct snd_soc_dai *dai, int mute)
  277. {
  278. struct snd_soc_codec *codec = dai->codec;
  279. u16 mute_reg = snd_soc_read(codec, AK4535_DAC);
  280. if (!mute)
  281. snd_soc_write(codec, AK4535_DAC, mute_reg & ~0x20);
  282. else
  283. snd_soc_write(codec, AK4535_DAC, mute_reg | 0x20);
  284. return 0;
  285. }
  286. static int ak4535_set_bias_level(struct snd_soc_codec *codec,
  287. enum snd_soc_bias_level level)
  288. {
  289. switch (level) {
  290. case SND_SOC_BIAS_ON:
  291. snd_soc_update_bits(codec, AK4535_DAC, 0x20, 0);
  292. break;
  293. case SND_SOC_BIAS_PREPARE:
  294. snd_soc_update_bits(codec, AK4535_DAC, 0x20, 0x20);
  295. break;
  296. case SND_SOC_BIAS_STANDBY:
  297. snd_soc_update_bits(codec, AK4535_PM1, 0x80, 0x80);
  298. snd_soc_update_bits(codec, AK4535_PM2, 0x80, 0);
  299. break;
  300. case SND_SOC_BIAS_OFF:
  301. snd_soc_update_bits(codec, AK4535_PM1, 0x80, 0);
  302. break;
  303. }
  304. codec->dapm.bias_level = level;
  305. return 0;
  306. }
  307. #define AK4535_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |\
  308. SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 |\
  309. SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000)
  310. static const struct snd_soc_dai_ops ak4535_dai_ops = {
  311. .hw_params = ak4535_hw_params,
  312. .set_fmt = ak4535_set_dai_fmt,
  313. .digital_mute = ak4535_mute,
  314. .set_sysclk = ak4535_set_dai_sysclk,
  315. };
  316. static struct snd_soc_dai_driver ak4535_dai = {
  317. .name = "ak4535-hifi",
  318. .playback = {
  319. .stream_name = "Playback",
  320. .channels_min = 1,
  321. .channels_max = 2,
  322. .rates = AK4535_RATES,
  323. .formats = SNDRV_PCM_FMTBIT_S16_LE,},
  324. .capture = {
  325. .stream_name = "Capture",
  326. .channels_min = 1,
  327. .channels_max = 2,
  328. .rates = AK4535_RATES,
  329. .formats = SNDRV_PCM_FMTBIT_S16_LE,},
  330. .ops = &ak4535_dai_ops,
  331. };
  332. static int ak4535_suspend(struct snd_soc_codec *codec)
  333. {
  334. ak4535_set_bias_level(codec, SND_SOC_BIAS_OFF);
  335. return 0;
  336. }
  337. static int ak4535_resume(struct snd_soc_codec *codec)
  338. {
  339. snd_soc_cache_sync(codec);
  340. ak4535_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  341. return 0;
  342. }
  343. static int ak4535_probe(struct snd_soc_codec *codec)
  344. {
  345. struct ak4535_priv *ak4535 = snd_soc_codec_get_drvdata(codec);
  346. int ret;
  347. codec->control_data = ak4535->regmap;
  348. ret = snd_soc_codec_set_cache_io(codec, 8, 8, SND_SOC_REGMAP);
  349. if (ret < 0) {
  350. dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
  351. return ret;
  352. }
  353. /* power on device */
  354. ak4535_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  355. snd_soc_add_codec_controls(codec, ak4535_snd_controls,
  356. ARRAY_SIZE(ak4535_snd_controls));
  357. return 0;
  358. }
  359. /* power down chip */
  360. static int ak4535_remove(struct snd_soc_codec *codec)
  361. {
  362. ak4535_set_bias_level(codec, SND_SOC_BIAS_OFF);
  363. return 0;
  364. }
  365. static const struct regmap_config ak4535_regmap = {
  366. .reg_bits = 8,
  367. .val_bits = 8,
  368. .max_register = AK4535_STATUS,
  369. .volatile_reg = ak4535_volatile,
  370. .cache_type = REGCACHE_RBTREE,
  371. .reg_defaults = ak4535_reg_defaults,
  372. .num_reg_defaults = ARRAY_SIZE(ak4535_reg_defaults),
  373. };
  374. static struct snd_soc_codec_driver soc_codec_dev_ak4535 = {
  375. .probe = ak4535_probe,
  376. .remove = ak4535_remove,
  377. .suspend = ak4535_suspend,
  378. .resume = ak4535_resume,
  379. .set_bias_level = ak4535_set_bias_level,
  380. .dapm_widgets = ak4535_dapm_widgets,
  381. .num_dapm_widgets = ARRAY_SIZE(ak4535_dapm_widgets),
  382. .dapm_routes = ak4535_audio_map,
  383. .num_dapm_routes = ARRAY_SIZE(ak4535_audio_map),
  384. };
  385. static __devinit int ak4535_i2c_probe(struct i2c_client *i2c,
  386. const struct i2c_device_id *id)
  387. {
  388. struct ak4535_priv *ak4535;
  389. int ret;
  390. ak4535 = devm_kzalloc(&i2c->dev, sizeof(struct ak4535_priv),
  391. GFP_KERNEL);
  392. if (ak4535 == NULL)
  393. return -ENOMEM;
  394. ak4535->regmap = regmap_init_i2c(i2c, &ak4535_regmap);
  395. if (IS_ERR(ak4535->regmap)) {
  396. ret = PTR_ERR(ak4535->regmap);
  397. dev_err(&i2c->dev, "Failed to init regmap: %d\n", ret);
  398. return ret;
  399. }
  400. i2c_set_clientdata(i2c, ak4535);
  401. ret = snd_soc_register_codec(&i2c->dev,
  402. &soc_codec_dev_ak4535, &ak4535_dai, 1);
  403. if (ret != 0)
  404. regmap_exit(ak4535->regmap);
  405. return ret;
  406. }
  407. static __devexit int ak4535_i2c_remove(struct i2c_client *client)
  408. {
  409. struct ak4535_priv *ak4535 = i2c_get_clientdata(client);
  410. snd_soc_unregister_codec(&client->dev);
  411. regmap_exit(ak4535->regmap);
  412. return 0;
  413. }
  414. static const struct i2c_device_id ak4535_i2c_id[] = {
  415. { "ak4535", 0 },
  416. { }
  417. };
  418. MODULE_DEVICE_TABLE(i2c, ak4535_i2c_id);
  419. static struct i2c_driver ak4535_i2c_driver = {
  420. .driver = {
  421. .name = "ak4535",
  422. .owner = THIS_MODULE,
  423. },
  424. .probe = ak4535_i2c_probe,
  425. .remove = __devexit_p(ak4535_i2c_remove),
  426. .id_table = ak4535_i2c_id,
  427. };
  428. module_i2c_driver(ak4535_i2c_driver);
  429. MODULE_DESCRIPTION("Soc AK4535 driver");
  430. MODULE_AUTHOR("Richard Purdie");
  431. MODULE_LICENSE("GPL");