alc5623.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110
  1. /*
  2. * alc5623.c -- alc562[123] ALSA Soc Audio driver
  3. *
  4. * Copyright 2008 Realtek Microelectronics
  5. * Author: flove <flove@realtek.com> Ethan <eku@marvell.com>
  6. *
  7. * Copyright 2010 Arnaud Patard <arnaud.patard@rtp-net.org>
  8. *
  9. *
  10. * Based on WM8753.c
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License version 2 as
  14. * published by the Free Software Foundation.
  15. *
  16. */
  17. #include <linux/module.h>
  18. #include <linux/kernel.h>
  19. #include <linux/init.h>
  20. #include <linux/delay.h>
  21. #include <linux/pm.h>
  22. #include <linux/i2c.h>
  23. #include <linux/slab.h>
  24. #include <sound/core.h>
  25. #include <sound/pcm.h>
  26. #include <sound/pcm_params.h>
  27. #include <sound/tlv.h>
  28. #include <sound/soc.h>
  29. #include <sound/initval.h>
  30. #include <sound/alc5623.h>
  31. #include "alc5623.h"
  32. static int caps_charge = 2000;
  33. module_param(caps_charge, int, 0);
  34. MODULE_PARM_DESC(caps_charge, "ALC5623 cap charge time (msecs)");
  35. /* codec private data */
  36. struct alc5623_priv {
  37. enum snd_soc_control_type control_type;
  38. u8 id;
  39. unsigned int sysclk;
  40. u16 reg_cache[ALC5623_VENDOR_ID2+2];
  41. unsigned int add_ctrl;
  42. unsigned int jack_det_ctrl;
  43. };
  44. static void alc5623_fill_cache(struct snd_soc_codec *codec)
  45. {
  46. int i, step = codec->driver->reg_cache_step;
  47. u16 *cache = codec->reg_cache;
  48. /* not really efficient ... */
  49. codec->cache_bypass = 1;
  50. for (i = 0 ; i < codec->driver->reg_cache_size ; i += step)
  51. cache[i] = snd_soc_read(codec, i);
  52. codec->cache_bypass = 0;
  53. }
  54. static inline int alc5623_reset(struct snd_soc_codec *codec)
  55. {
  56. return snd_soc_write(codec, ALC5623_RESET, 0);
  57. }
  58. static int amp_mixer_event(struct snd_soc_dapm_widget *w,
  59. struct snd_kcontrol *kcontrol, int event)
  60. {
  61. /* to power-on/off class-d amp generators/speaker */
  62. /* need to write to 'index-46h' register : */
  63. /* so write index num (here 0x46) to reg 0x6a */
  64. /* and then 0xffff/0 to reg 0x6c */
  65. snd_soc_write(w->codec, ALC5623_HID_CTRL_INDEX, 0x46);
  66. switch (event) {
  67. case SND_SOC_DAPM_PRE_PMU:
  68. snd_soc_write(w->codec, ALC5623_HID_CTRL_DATA, 0xFFFF);
  69. break;
  70. case SND_SOC_DAPM_POST_PMD:
  71. snd_soc_write(w->codec, ALC5623_HID_CTRL_DATA, 0);
  72. break;
  73. }
  74. return 0;
  75. }
  76. /*
  77. * ALC5623 Controls
  78. */
  79. static const DECLARE_TLV_DB_SCALE(vol_tlv, -3450, 150, 0);
  80. static const DECLARE_TLV_DB_SCALE(hp_tlv, -4650, 150, 0);
  81. static const DECLARE_TLV_DB_SCALE(adc_rec_tlv, -1650, 150, 0);
  82. static const unsigned int boost_tlv[] = {
  83. TLV_DB_RANGE_HEAD(3),
  84. 0, 0, TLV_DB_SCALE_ITEM(0, 0, 0),
  85. 1, 1, TLV_DB_SCALE_ITEM(2000, 0, 0),
  86. 2, 2, TLV_DB_SCALE_ITEM(3000, 0, 0),
  87. };
  88. static const DECLARE_TLV_DB_SCALE(dig_tlv, 0, 600, 0);
  89. static const struct snd_kcontrol_new alc5621_vol_snd_controls[] = {
  90. SOC_DOUBLE_TLV("Speaker Playback Volume",
  91. ALC5623_SPK_OUT_VOL, 8, 0, 31, 1, hp_tlv),
  92. SOC_DOUBLE("Speaker Playback Switch",
  93. ALC5623_SPK_OUT_VOL, 15, 7, 1, 1),
  94. SOC_DOUBLE_TLV("Headphone Playback Volume",
  95. ALC5623_HP_OUT_VOL, 8, 0, 31, 1, hp_tlv),
  96. SOC_DOUBLE("Headphone Playback Switch",
  97. ALC5623_HP_OUT_VOL, 15, 7, 1, 1),
  98. };
  99. static const struct snd_kcontrol_new alc5622_vol_snd_controls[] = {
  100. SOC_DOUBLE_TLV("Speaker Playback Volume",
  101. ALC5623_SPK_OUT_VOL, 8, 0, 31, 1, hp_tlv),
  102. SOC_DOUBLE("Speaker Playback Switch",
  103. ALC5623_SPK_OUT_VOL, 15, 7, 1, 1),
  104. SOC_DOUBLE_TLV("Line Playback Volume",
  105. ALC5623_HP_OUT_VOL, 8, 0, 31, 1, hp_tlv),
  106. SOC_DOUBLE("Line Playback Switch",
  107. ALC5623_HP_OUT_VOL, 15, 7, 1, 1),
  108. };
  109. static const struct snd_kcontrol_new alc5623_vol_snd_controls[] = {
  110. SOC_DOUBLE_TLV("Line Playback Volume",
  111. ALC5623_SPK_OUT_VOL, 8, 0, 31, 1, hp_tlv),
  112. SOC_DOUBLE("Line Playback Switch",
  113. ALC5623_SPK_OUT_VOL, 15, 7, 1, 1),
  114. SOC_DOUBLE_TLV("Headphone Playback Volume",
  115. ALC5623_HP_OUT_VOL, 8, 0, 31, 1, hp_tlv),
  116. SOC_DOUBLE("Headphone Playback Switch",
  117. ALC5623_HP_OUT_VOL, 15, 7, 1, 1),
  118. };
  119. static const struct snd_kcontrol_new alc5623_snd_controls[] = {
  120. SOC_DOUBLE_TLV("Auxout Playback Volume",
  121. ALC5623_MONO_AUX_OUT_VOL, 8, 0, 31, 1, hp_tlv),
  122. SOC_DOUBLE("Auxout Playback Switch",
  123. ALC5623_MONO_AUX_OUT_VOL, 15, 7, 1, 1),
  124. SOC_DOUBLE_TLV("PCM Playback Volume",
  125. ALC5623_STEREO_DAC_VOL, 8, 0, 31, 1, vol_tlv),
  126. SOC_DOUBLE_TLV("AuxI Capture Volume",
  127. ALC5623_AUXIN_VOL, 8, 0, 31, 1, vol_tlv),
  128. SOC_DOUBLE_TLV("LineIn Capture Volume",
  129. ALC5623_LINE_IN_VOL, 8, 0, 31, 1, vol_tlv),
  130. SOC_SINGLE_TLV("Mic1 Capture Volume",
  131. ALC5623_MIC_VOL, 8, 31, 1, vol_tlv),
  132. SOC_SINGLE_TLV("Mic2 Capture Volume",
  133. ALC5623_MIC_VOL, 0, 31, 1, vol_tlv),
  134. SOC_DOUBLE_TLV("Rec Capture Volume",
  135. ALC5623_ADC_REC_GAIN, 7, 0, 31, 0, adc_rec_tlv),
  136. SOC_SINGLE_TLV("Mic 1 Boost Volume",
  137. ALC5623_MIC_CTRL, 10, 2, 0, boost_tlv),
  138. SOC_SINGLE_TLV("Mic 2 Boost Volume",
  139. ALC5623_MIC_CTRL, 8, 2, 0, boost_tlv),
  140. SOC_SINGLE_TLV("Digital Boost Volume",
  141. ALC5623_ADD_CTRL_REG, 4, 3, 0, dig_tlv),
  142. };
  143. /*
  144. * DAPM Controls
  145. */
  146. static const struct snd_kcontrol_new alc5623_hp_mixer_controls[] = {
  147. SOC_DAPM_SINGLE("LI2HP Playback Switch", ALC5623_LINE_IN_VOL, 15, 1, 1),
  148. SOC_DAPM_SINGLE("AUXI2HP Playback Switch", ALC5623_AUXIN_VOL, 15, 1, 1),
  149. SOC_DAPM_SINGLE("MIC12HP Playback Switch", ALC5623_MIC_ROUTING_CTRL, 15, 1, 1),
  150. SOC_DAPM_SINGLE("MIC22HP Playback Switch", ALC5623_MIC_ROUTING_CTRL, 7, 1, 1),
  151. SOC_DAPM_SINGLE("DAC2HP Playback Switch", ALC5623_STEREO_DAC_VOL, 15, 1, 1),
  152. };
  153. static const struct snd_kcontrol_new alc5623_hpl_mixer_controls[] = {
  154. SOC_DAPM_SINGLE("ADC2HP_L Playback Switch", ALC5623_ADC_REC_GAIN, 15, 1, 1),
  155. };
  156. static const struct snd_kcontrol_new alc5623_hpr_mixer_controls[] = {
  157. SOC_DAPM_SINGLE("ADC2HP_R Playback Switch", ALC5623_ADC_REC_GAIN, 14, 1, 1),
  158. };
  159. static const struct snd_kcontrol_new alc5623_mono_mixer_controls[] = {
  160. SOC_DAPM_SINGLE("ADC2MONO_L Playback Switch", ALC5623_ADC_REC_GAIN, 13, 1, 1),
  161. SOC_DAPM_SINGLE("ADC2MONO_R Playback Switch", ALC5623_ADC_REC_GAIN, 12, 1, 1),
  162. SOC_DAPM_SINGLE("LI2MONO Playback Switch", ALC5623_LINE_IN_VOL, 13, 1, 1),
  163. SOC_DAPM_SINGLE("AUXI2MONO Playback Switch", ALC5623_AUXIN_VOL, 13, 1, 1),
  164. SOC_DAPM_SINGLE("MIC12MONO Playback Switch", ALC5623_MIC_ROUTING_CTRL, 13, 1, 1),
  165. SOC_DAPM_SINGLE("MIC22MONO Playback Switch", ALC5623_MIC_ROUTING_CTRL, 5, 1, 1),
  166. SOC_DAPM_SINGLE("DAC2MONO Playback Switch", ALC5623_STEREO_DAC_VOL, 13, 1, 1),
  167. };
  168. static const struct snd_kcontrol_new alc5623_speaker_mixer_controls[] = {
  169. SOC_DAPM_SINGLE("LI2SPK Playback Switch", ALC5623_LINE_IN_VOL, 14, 1, 1),
  170. SOC_DAPM_SINGLE("AUXI2SPK Playback Switch", ALC5623_AUXIN_VOL, 14, 1, 1),
  171. SOC_DAPM_SINGLE("MIC12SPK Playback Switch", ALC5623_MIC_ROUTING_CTRL, 14, 1, 1),
  172. SOC_DAPM_SINGLE("MIC22SPK Playback Switch", ALC5623_MIC_ROUTING_CTRL, 6, 1, 1),
  173. SOC_DAPM_SINGLE("DAC2SPK Playback Switch", ALC5623_STEREO_DAC_VOL, 14, 1, 1),
  174. };
  175. /* Left Record Mixer */
  176. static const struct snd_kcontrol_new alc5623_captureL_mixer_controls[] = {
  177. SOC_DAPM_SINGLE("Mic1 Capture Switch", ALC5623_ADC_REC_MIXER, 14, 1, 1),
  178. SOC_DAPM_SINGLE("Mic2 Capture Switch", ALC5623_ADC_REC_MIXER, 13, 1, 1),
  179. SOC_DAPM_SINGLE("LineInL Capture Switch", ALC5623_ADC_REC_MIXER, 12, 1, 1),
  180. SOC_DAPM_SINGLE("Left AuxI Capture Switch", ALC5623_ADC_REC_MIXER, 11, 1, 1),
  181. SOC_DAPM_SINGLE("HPMixerL Capture Switch", ALC5623_ADC_REC_MIXER, 10, 1, 1),
  182. SOC_DAPM_SINGLE("SPKMixer Capture Switch", ALC5623_ADC_REC_MIXER, 9, 1, 1),
  183. SOC_DAPM_SINGLE("MonoMixer Capture Switch", ALC5623_ADC_REC_MIXER, 8, 1, 1),
  184. };
  185. /* Right Record Mixer */
  186. static const struct snd_kcontrol_new alc5623_captureR_mixer_controls[] = {
  187. SOC_DAPM_SINGLE("Mic1 Capture Switch", ALC5623_ADC_REC_MIXER, 6, 1, 1),
  188. SOC_DAPM_SINGLE("Mic2 Capture Switch", ALC5623_ADC_REC_MIXER, 5, 1, 1),
  189. SOC_DAPM_SINGLE("LineInR Capture Switch", ALC5623_ADC_REC_MIXER, 4, 1, 1),
  190. SOC_DAPM_SINGLE("Right AuxI Capture Switch", ALC5623_ADC_REC_MIXER, 3, 1, 1),
  191. SOC_DAPM_SINGLE("HPMixerR Capture Switch", ALC5623_ADC_REC_MIXER, 2, 1, 1),
  192. SOC_DAPM_SINGLE("SPKMixer Capture Switch", ALC5623_ADC_REC_MIXER, 1, 1, 1),
  193. SOC_DAPM_SINGLE("MonoMixer Capture Switch", ALC5623_ADC_REC_MIXER, 0, 1, 1),
  194. };
  195. static const char *alc5623_spk_n_sour_sel[] = {
  196. "RN/-R", "RP/+R", "LN/-R", "Vmid" };
  197. static const char *alc5623_hpl_out_input_sel[] = {
  198. "Vmid", "HP Left Mix"};
  199. static const char *alc5623_hpr_out_input_sel[] = {
  200. "Vmid", "HP Right Mix"};
  201. static const char *alc5623_spkout_input_sel[] = {
  202. "Vmid", "HPOut Mix", "Speaker Mix", "Mono Mix"};
  203. static const char *alc5623_aux_out_input_sel[] = {
  204. "Vmid", "HPOut Mix", "Speaker Mix", "Mono Mix"};
  205. /* auxout output mux */
  206. static const struct soc_enum alc5623_aux_out_input_enum =
  207. SOC_ENUM_SINGLE(ALC5623_OUTPUT_MIXER_CTRL, 6, 4, alc5623_aux_out_input_sel);
  208. static const struct snd_kcontrol_new alc5623_auxout_mux_controls =
  209. SOC_DAPM_ENUM("Route", alc5623_aux_out_input_enum);
  210. /* speaker output mux */
  211. static const struct soc_enum alc5623_spkout_input_enum =
  212. SOC_ENUM_SINGLE(ALC5623_OUTPUT_MIXER_CTRL, 10, 4, alc5623_spkout_input_sel);
  213. static const struct snd_kcontrol_new alc5623_spkout_mux_controls =
  214. SOC_DAPM_ENUM("Route", alc5623_spkout_input_enum);
  215. /* headphone left output mux */
  216. static const struct soc_enum alc5623_hpl_out_input_enum =
  217. SOC_ENUM_SINGLE(ALC5623_OUTPUT_MIXER_CTRL, 9, 2, alc5623_hpl_out_input_sel);
  218. static const struct snd_kcontrol_new alc5623_hpl_out_mux_controls =
  219. SOC_DAPM_ENUM("Route", alc5623_hpl_out_input_enum);
  220. /* headphone right output mux */
  221. static const struct soc_enum alc5623_hpr_out_input_enum =
  222. SOC_ENUM_SINGLE(ALC5623_OUTPUT_MIXER_CTRL, 8, 2, alc5623_hpr_out_input_sel);
  223. static const struct snd_kcontrol_new alc5623_hpr_out_mux_controls =
  224. SOC_DAPM_ENUM("Route", alc5623_hpr_out_input_enum);
  225. /* speaker output N select */
  226. static const struct soc_enum alc5623_spk_n_sour_enum =
  227. SOC_ENUM_SINGLE(ALC5623_OUTPUT_MIXER_CTRL, 14, 4, alc5623_spk_n_sour_sel);
  228. static const struct snd_kcontrol_new alc5623_spkoutn_mux_controls =
  229. SOC_DAPM_ENUM("Route", alc5623_spk_n_sour_enum);
  230. static const struct snd_soc_dapm_widget alc5623_dapm_widgets[] = {
  231. /* Muxes */
  232. SND_SOC_DAPM_MUX("AuxOut Mux", SND_SOC_NOPM, 0, 0,
  233. &alc5623_auxout_mux_controls),
  234. SND_SOC_DAPM_MUX("SpeakerOut Mux", SND_SOC_NOPM, 0, 0,
  235. &alc5623_spkout_mux_controls),
  236. SND_SOC_DAPM_MUX("Left Headphone Mux", SND_SOC_NOPM, 0, 0,
  237. &alc5623_hpl_out_mux_controls),
  238. SND_SOC_DAPM_MUX("Right Headphone Mux", SND_SOC_NOPM, 0, 0,
  239. &alc5623_hpr_out_mux_controls),
  240. SND_SOC_DAPM_MUX("SpeakerOut N Mux", SND_SOC_NOPM, 0, 0,
  241. &alc5623_spkoutn_mux_controls),
  242. /* output mixers */
  243. SND_SOC_DAPM_MIXER("HP Mix", SND_SOC_NOPM, 0, 0,
  244. &alc5623_hp_mixer_controls[0],
  245. ARRAY_SIZE(alc5623_hp_mixer_controls)),
  246. SND_SOC_DAPM_MIXER("HPR Mix", ALC5623_PWR_MANAG_ADD2, 4, 0,
  247. &alc5623_hpr_mixer_controls[0],
  248. ARRAY_SIZE(alc5623_hpr_mixer_controls)),
  249. SND_SOC_DAPM_MIXER("HPL Mix", ALC5623_PWR_MANAG_ADD2, 5, 0,
  250. &alc5623_hpl_mixer_controls[0],
  251. ARRAY_SIZE(alc5623_hpl_mixer_controls)),
  252. SND_SOC_DAPM_MIXER("HPOut Mix", SND_SOC_NOPM, 0, 0, NULL, 0),
  253. SND_SOC_DAPM_MIXER("Mono Mix", ALC5623_PWR_MANAG_ADD2, 2, 0,
  254. &alc5623_mono_mixer_controls[0],
  255. ARRAY_SIZE(alc5623_mono_mixer_controls)),
  256. SND_SOC_DAPM_MIXER("Speaker Mix", ALC5623_PWR_MANAG_ADD2, 3, 0,
  257. &alc5623_speaker_mixer_controls[0],
  258. ARRAY_SIZE(alc5623_speaker_mixer_controls)),
  259. /* input mixers */
  260. SND_SOC_DAPM_MIXER("Left Capture Mix", ALC5623_PWR_MANAG_ADD2, 1, 0,
  261. &alc5623_captureL_mixer_controls[0],
  262. ARRAY_SIZE(alc5623_captureL_mixer_controls)),
  263. SND_SOC_DAPM_MIXER("Right Capture Mix", ALC5623_PWR_MANAG_ADD2, 0, 0,
  264. &alc5623_captureR_mixer_controls[0],
  265. ARRAY_SIZE(alc5623_captureR_mixer_controls)),
  266. SND_SOC_DAPM_DAC("Left DAC", "Left HiFi Playback",
  267. ALC5623_PWR_MANAG_ADD2, 9, 0),
  268. SND_SOC_DAPM_DAC("Right DAC", "Right HiFi Playback",
  269. ALC5623_PWR_MANAG_ADD2, 8, 0),
  270. SND_SOC_DAPM_MIXER("I2S Mix", ALC5623_PWR_MANAG_ADD1, 15, 0, NULL, 0),
  271. SND_SOC_DAPM_MIXER("AuxI Mix", SND_SOC_NOPM, 0, 0, NULL, 0),
  272. SND_SOC_DAPM_MIXER("Line Mix", SND_SOC_NOPM, 0, 0, NULL, 0),
  273. SND_SOC_DAPM_ADC("Left ADC", "Left HiFi Capture",
  274. ALC5623_PWR_MANAG_ADD2, 7, 0),
  275. SND_SOC_DAPM_ADC("Right ADC", "Right HiFi Capture",
  276. ALC5623_PWR_MANAG_ADD2, 6, 0),
  277. SND_SOC_DAPM_PGA("Left Headphone", ALC5623_PWR_MANAG_ADD3, 10, 0, NULL, 0),
  278. SND_SOC_DAPM_PGA("Right Headphone", ALC5623_PWR_MANAG_ADD3, 9, 0, NULL, 0),
  279. SND_SOC_DAPM_PGA("SpeakerOut", ALC5623_PWR_MANAG_ADD3, 12, 0, NULL, 0),
  280. SND_SOC_DAPM_PGA("Left AuxOut", ALC5623_PWR_MANAG_ADD3, 14, 0, NULL, 0),
  281. SND_SOC_DAPM_PGA("Right AuxOut", ALC5623_PWR_MANAG_ADD3, 13, 0, NULL, 0),
  282. SND_SOC_DAPM_PGA("Left LineIn", ALC5623_PWR_MANAG_ADD3, 7, 0, NULL, 0),
  283. SND_SOC_DAPM_PGA("Right LineIn", ALC5623_PWR_MANAG_ADD3, 6, 0, NULL, 0),
  284. SND_SOC_DAPM_PGA("Left AuxI", ALC5623_PWR_MANAG_ADD3, 5, 0, NULL, 0),
  285. SND_SOC_DAPM_PGA("Right AuxI", ALC5623_PWR_MANAG_ADD3, 4, 0, NULL, 0),
  286. SND_SOC_DAPM_PGA("MIC1 PGA", ALC5623_PWR_MANAG_ADD3, 3, 0, NULL, 0),
  287. SND_SOC_DAPM_PGA("MIC2 PGA", ALC5623_PWR_MANAG_ADD3, 2, 0, NULL, 0),
  288. SND_SOC_DAPM_PGA("MIC1 Pre Amp", ALC5623_PWR_MANAG_ADD3, 1, 0, NULL, 0),
  289. SND_SOC_DAPM_PGA("MIC2 Pre Amp", ALC5623_PWR_MANAG_ADD3, 0, 0, NULL, 0),
  290. SND_SOC_DAPM_MICBIAS("Mic Bias1", ALC5623_PWR_MANAG_ADD1, 11, 0),
  291. SND_SOC_DAPM_OUTPUT("AUXOUTL"),
  292. SND_SOC_DAPM_OUTPUT("AUXOUTR"),
  293. SND_SOC_DAPM_OUTPUT("HPL"),
  294. SND_SOC_DAPM_OUTPUT("HPR"),
  295. SND_SOC_DAPM_OUTPUT("SPKOUT"),
  296. SND_SOC_DAPM_OUTPUT("SPKOUTN"),
  297. SND_SOC_DAPM_INPUT("LINEINL"),
  298. SND_SOC_DAPM_INPUT("LINEINR"),
  299. SND_SOC_DAPM_INPUT("AUXINL"),
  300. SND_SOC_DAPM_INPUT("AUXINR"),
  301. SND_SOC_DAPM_INPUT("MIC1"),
  302. SND_SOC_DAPM_INPUT("MIC2"),
  303. SND_SOC_DAPM_VMID("Vmid"),
  304. };
  305. static const char *alc5623_amp_names[] = {"AB Amp", "D Amp"};
  306. static const struct soc_enum alc5623_amp_enum =
  307. SOC_ENUM_SINGLE(ALC5623_OUTPUT_MIXER_CTRL, 13, 2, alc5623_amp_names);
  308. static const struct snd_kcontrol_new alc5623_amp_mux_controls =
  309. SOC_DAPM_ENUM("Route", alc5623_amp_enum);
  310. static const struct snd_soc_dapm_widget alc5623_dapm_amp_widgets[] = {
  311. SND_SOC_DAPM_PGA_E("D Amp", ALC5623_PWR_MANAG_ADD2, 14, 0, NULL, 0,
  312. amp_mixer_event, SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  313. SND_SOC_DAPM_PGA("AB Amp", ALC5623_PWR_MANAG_ADD2, 15, 0, NULL, 0),
  314. SND_SOC_DAPM_MUX("AB-D Amp Mux", SND_SOC_NOPM, 0, 0,
  315. &alc5623_amp_mux_controls),
  316. };
  317. static const struct snd_soc_dapm_route intercon[] = {
  318. /* virtual mixer - mixes left & right channels */
  319. {"I2S Mix", NULL, "Left DAC"},
  320. {"I2S Mix", NULL, "Right DAC"},
  321. {"Line Mix", NULL, "Right LineIn"},
  322. {"Line Mix", NULL, "Left LineIn"},
  323. {"AuxI Mix", NULL, "Left AuxI"},
  324. {"AuxI Mix", NULL, "Right AuxI"},
  325. {"AUXOUTL", NULL, "Left AuxOut"},
  326. {"AUXOUTR", NULL, "Right AuxOut"},
  327. /* HP mixer */
  328. {"HPL Mix", "ADC2HP_L Playback Switch", "Left Capture Mix"},
  329. {"HPL Mix", NULL, "HP Mix"},
  330. {"HPR Mix", "ADC2HP_R Playback Switch", "Right Capture Mix"},
  331. {"HPR Mix", NULL, "HP Mix"},
  332. {"HP Mix", "LI2HP Playback Switch", "Line Mix"},
  333. {"HP Mix", "AUXI2HP Playback Switch", "AuxI Mix"},
  334. {"HP Mix", "MIC12HP Playback Switch", "MIC1 PGA"},
  335. {"HP Mix", "MIC22HP Playback Switch", "MIC2 PGA"},
  336. {"HP Mix", "DAC2HP Playback Switch", "I2S Mix"},
  337. /* speaker mixer */
  338. {"Speaker Mix", "LI2SPK Playback Switch", "Line Mix"},
  339. {"Speaker Mix", "AUXI2SPK Playback Switch", "AuxI Mix"},
  340. {"Speaker Mix", "MIC12SPK Playback Switch", "MIC1 PGA"},
  341. {"Speaker Mix", "MIC22SPK Playback Switch", "MIC2 PGA"},
  342. {"Speaker Mix", "DAC2SPK Playback Switch", "I2S Mix"},
  343. /* mono mixer */
  344. {"Mono Mix", "ADC2MONO_L Playback Switch", "Left Capture Mix"},
  345. {"Mono Mix", "ADC2MONO_R Playback Switch", "Right Capture Mix"},
  346. {"Mono Mix", "LI2MONO Playback Switch", "Line Mix"},
  347. {"Mono Mix", "AUXI2MONO Playback Switch", "AuxI Mix"},
  348. {"Mono Mix", "MIC12MONO Playback Switch", "MIC1 PGA"},
  349. {"Mono Mix", "MIC22MONO Playback Switch", "MIC2 PGA"},
  350. {"Mono Mix", "DAC2MONO Playback Switch", "I2S Mix"},
  351. /* Left record mixer */
  352. {"Left Capture Mix", "LineInL Capture Switch", "LINEINL"},
  353. {"Left Capture Mix", "Left AuxI Capture Switch", "AUXINL"},
  354. {"Left Capture Mix", "Mic1 Capture Switch", "MIC1 Pre Amp"},
  355. {"Left Capture Mix", "Mic2 Capture Switch", "MIC2 Pre Amp"},
  356. {"Left Capture Mix", "HPMixerL Capture Switch", "HPL Mix"},
  357. {"Left Capture Mix", "SPKMixer Capture Switch", "Speaker Mix"},
  358. {"Left Capture Mix", "MonoMixer Capture Switch", "Mono Mix"},
  359. /*Right record mixer */
  360. {"Right Capture Mix", "LineInR Capture Switch", "LINEINR"},
  361. {"Right Capture Mix", "Right AuxI Capture Switch", "AUXINR"},
  362. {"Right Capture Mix", "Mic1 Capture Switch", "MIC1 Pre Amp"},
  363. {"Right Capture Mix", "Mic2 Capture Switch", "MIC2 Pre Amp"},
  364. {"Right Capture Mix", "HPMixerR Capture Switch", "HPR Mix"},
  365. {"Right Capture Mix", "SPKMixer Capture Switch", "Speaker Mix"},
  366. {"Right Capture Mix", "MonoMixer Capture Switch", "Mono Mix"},
  367. /* headphone left mux */
  368. {"Left Headphone Mux", "HP Left Mix", "HPL Mix"},
  369. {"Left Headphone Mux", "Vmid", "Vmid"},
  370. /* headphone right mux */
  371. {"Right Headphone Mux", "HP Right Mix", "HPR Mix"},
  372. {"Right Headphone Mux", "Vmid", "Vmid"},
  373. /* speaker out mux */
  374. {"SpeakerOut Mux", "Vmid", "Vmid"},
  375. {"SpeakerOut Mux", "HPOut Mix", "HPOut Mix"},
  376. {"SpeakerOut Mux", "Speaker Mix", "Speaker Mix"},
  377. {"SpeakerOut Mux", "Mono Mix", "Mono Mix"},
  378. /* Mono/Aux Out mux */
  379. {"AuxOut Mux", "Vmid", "Vmid"},
  380. {"AuxOut Mux", "HPOut Mix", "HPOut Mix"},
  381. {"AuxOut Mux", "Speaker Mix", "Speaker Mix"},
  382. {"AuxOut Mux", "Mono Mix", "Mono Mix"},
  383. /* output pga */
  384. {"HPL", NULL, "Left Headphone"},
  385. {"Left Headphone", NULL, "Left Headphone Mux"},
  386. {"HPR", NULL, "Right Headphone"},
  387. {"Right Headphone", NULL, "Right Headphone Mux"},
  388. {"Left AuxOut", NULL, "AuxOut Mux"},
  389. {"Right AuxOut", NULL, "AuxOut Mux"},
  390. /* input pga */
  391. {"Left LineIn", NULL, "LINEINL"},
  392. {"Right LineIn", NULL, "LINEINR"},
  393. {"Left AuxI", NULL, "AUXINL"},
  394. {"Right AuxI", NULL, "AUXINR"},
  395. {"MIC1 Pre Amp", NULL, "MIC1"},
  396. {"MIC2 Pre Amp", NULL, "MIC2"},
  397. {"MIC1 PGA", NULL, "MIC1 Pre Amp"},
  398. {"MIC2 PGA", NULL, "MIC2 Pre Amp"},
  399. /* left ADC */
  400. {"Left ADC", NULL, "Left Capture Mix"},
  401. /* right ADC */
  402. {"Right ADC", NULL, "Right Capture Mix"},
  403. {"SpeakerOut N Mux", "RN/-R", "SpeakerOut"},
  404. {"SpeakerOut N Mux", "RP/+R", "SpeakerOut"},
  405. {"SpeakerOut N Mux", "LN/-R", "SpeakerOut"},
  406. {"SpeakerOut N Mux", "Vmid", "Vmid"},
  407. {"SPKOUT", NULL, "SpeakerOut"},
  408. {"SPKOUTN", NULL, "SpeakerOut N Mux"},
  409. };
  410. static const struct snd_soc_dapm_route intercon_spk[] = {
  411. {"SpeakerOut", NULL, "SpeakerOut Mux"},
  412. };
  413. static const struct snd_soc_dapm_route intercon_amp_spk[] = {
  414. {"AB Amp", NULL, "SpeakerOut Mux"},
  415. {"D Amp", NULL, "SpeakerOut Mux"},
  416. {"AB-D Amp Mux", "AB Amp", "AB Amp"},
  417. {"AB-D Amp Mux", "D Amp", "D Amp"},
  418. {"SpeakerOut", NULL, "AB-D Amp Mux"},
  419. };
  420. /* PLL divisors */
  421. struct _pll_div {
  422. u32 pll_in;
  423. u32 pll_out;
  424. u16 regvalue;
  425. };
  426. /* Note : pll code from original alc5623 driver. Not sure of how good it is */
  427. /* useful only for master mode */
  428. static const struct _pll_div codec_master_pll_div[] = {
  429. { 2048000, 8192000, 0x0ea0},
  430. { 3686400, 8192000, 0x4e27},
  431. { 12000000, 8192000, 0x456b},
  432. { 13000000, 8192000, 0x495f},
  433. { 13100000, 8192000, 0x0320},
  434. { 2048000, 11289600, 0xf637},
  435. { 3686400, 11289600, 0x2f22},
  436. { 12000000, 11289600, 0x3e2f},
  437. { 13000000, 11289600, 0x4d5b},
  438. { 13100000, 11289600, 0x363b},
  439. { 2048000, 16384000, 0x1ea0},
  440. { 3686400, 16384000, 0x9e27},
  441. { 12000000, 16384000, 0x452b},
  442. { 13000000, 16384000, 0x542f},
  443. { 13100000, 16384000, 0x03a0},
  444. { 2048000, 16934400, 0xe625},
  445. { 3686400, 16934400, 0x9126},
  446. { 12000000, 16934400, 0x4d2c},
  447. { 13000000, 16934400, 0x742f},
  448. { 13100000, 16934400, 0x3c27},
  449. { 2048000, 22579200, 0x2aa0},
  450. { 3686400, 22579200, 0x2f20},
  451. { 12000000, 22579200, 0x7e2f},
  452. { 13000000, 22579200, 0x742f},
  453. { 13100000, 22579200, 0x3c27},
  454. { 2048000, 24576000, 0x2ea0},
  455. { 3686400, 24576000, 0xee27},
  456. { 12000000, 24576000, 0x2915},
  457. { 13000000, 24576000, 0x772e},
  458. { 13100000, 24576000, 0x0d20},
  459. };
  460. static const struct _pll_div codec_slave_pll_div[] = {
  461. { 1024000, 16384000, 0x3ea0},
  462. { 1411200, 22579200, 0x3ea0},
  463. { 1536000, 24576000, 0x3ea0},
  464. { 2048000, 16384000, 0x1ea0},
  465. { 2822400, 22579200, 0x1ea0},
  466. { 3072000, 24576000, 0x1ea0},
  467. };
  468. static int alc5623_set_dai_pll(struct snd_soc_dai *codec_dai, int pll_id,
  469. int source, unsigned int freq_in, unsigned int freq_out)
  470. {
  471. int i;
  472. struct snd_soc_codec *codec = codec_dai->codec;
  473. int gbl_clk = 0, pll_div = 0;
  474. u16 reg;
  475. if (pll_id < ALC5623_PLL_FR_MCLK || pll_id > ALC5623_PLL_FR_BCK)
  476. return -ENODEV;
  477. /* Disable PLL power */
  478. snd_soc_update_bits(codec, ALC5623_PWR_MANAG_ADD2,
  479. ALC5623_PWR_ADD2_PLL,
  480. 0);
  481. /* pll is not used in slave mode */
  482. reg = snd_soc_read(codec, ALC5623_DAI_CONTROL);
  483. if (reg & ALC5623_DAI_SDP_SLAVE_MODE)
  484. return 0;
  485. if (!freq_in || !freq_out)
  486. return 0;
  487. switch (pll_id) {
  488. case ALC5623_PLL_FR_MCLK:
  489. for (i = 0; i < ARRAY_SIZE(codec_master_pll_div); i++) {
  490. if (codec_master_pll_div[i].pll_in == freq_in
  491. && codec_master_pll_div[i].pll_out == freq_out) {
  492. /* PLL source from MCLK */
  493. pll_div = codec_master_pll_div[i].regvalue;
  494. break;
  495. }
  496. }
  497. break;
  498. case ALC5623_PLL_FR_BCK:
  499. for (i = 0; i < ARRAY_SIZE(codec_slave_pll_div); i++) {
  500. if (codec_slave_pll_div[i].pll_in == freq_in
  501. && codec_slave_pll_div[i].pll_out == freq_out) {
  502. /* PLL source from Bitclk */
  503. gbl_clk = ALC5623_GBL_CLK_PLL_SOUR_SEL_BITCLK;
  504. pll_div = codec_slave_pll_div[i].regvalue;
  505. break;
  506. }
  507. }
  508. break;
  509. default:
  510. return -EINVAL;
  511. }
  512. if (!pll_div)
  513. return -EINVAL;
  514. snd_soc_write(codec, ALC5623_GLOBAL_CLK_CTRL_REG, gbl_clk);
  515. snd_soc_write(codec, ALC5623_PLL_CTRL, pll_div);
  516. snd_soc_update_bits(codec, ALC5623_PWR_MANAG_ADD2,
  517. ALC5623_PWR_ADD2_PLL,
  518. ALC5623_PWR_ADD2_PLL);
  519. gbl_clk |= ALC5623_GBL_CLK_SYS_SOUR_SEL_PLL;
  520. snd_soc_write(codec, ALC5623_GLOBAL_CLK_CTRL_REG, gbl_clk);
  521. return 0;
  522. }
  523. struct _coeff_div {
  524. u16 fs;
  525. u16 regvalue;
  526. };
  527. /* codec hifi mclk (after PLL) clock divider coefficients */
  528. /* values inspired from column BCLK=32Fs of Appendix A table */
  529. static const struct _coeff_div coeff_div[] = {
  530. {256*8, 0x3a69},
  531. {384*8, 0x3c6b},
  532. {256*4, 0x2a69},
  533. {384*4, 0x2c6b},
  534. {256*2, 0x1a69},
  535. {384*2, 0x1c6b},
  536. {256*1, 0x0a69},
  537. {384*1, 0x0c6b},
  538. };
  539. static int get_coeff(struct snd_soc_codec *codec, int rate)
  540. {
  541. struct alc5623_priv *alc5623 = snd_soc_codec_get_drvdata(codec);
  542. int i;
  543. for (i = 0; i < ARRAY_SIZE(coeff_div); i++) {
  544. if (coeff_div[i].fs * rate == alc5623->sysclk)
  545. return i;
  546. }
  547. return -EINVAL;
  548. }
  549. /*
  550. * Clock after PLL and dividers
  551. */
  552. static int alc5623_set_dai_sysclk(struct snd_soc_dai *codec_dai,
  553. int clk_id, unsigned int freq, int dir)
  554. {
  555. struct snd_soc_codec *codec = codec_dai->codec;
  556. struct alc5623_priv *alc5623 = snd_soc_codec_get_drvdata(codec);
  557. switch (freq) {
  558. case 8192000:
  559. case 11289600:
  560. case 12288000:
  561. case 16384000:
  562. case 16934400:
  563. case 18432000:
  564. case 22579200:
  565. case 24576000:
  566. alc5623->sysclk = freq;
  567. return 0;
  568. }
  569. return -EINVAL;
  570. }
  571. static int alc5623_set_dai_fmt(struct snd_soc_dai *codec_dai,
  572. unsigned int fmt)
  573. {
  574. struct snd_soc_codec *codec = codec_dai->codec;
  575. u16 iface = 0;
  576. /* set master/slave audio interface */
  577. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  578. case SND_SOC_DAIFMT_CBM_CFM:
  579. iface = ALC5623_DAI_SDP_MASTER_MODE;
  580. break;
  581. case SND_SOC_DAIFMT_CBS_CFS:
  582. iface = ALC5623_DAI_SDP_SLAVE_MODE;
  583. break;
  584. default:
  585. return -EINVAL;
  586. }
  587. /* interface format */
  588. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  589. case SND_SOC_DAIFMT_I2S:
  590. iface |= ALC5623_DAI_I2S_DF_I2S;
  591. break;
  592. case SND_SOC_DAIFMT_RIGHT_J:
  593. iface |= ALC5623_DAI_I2S_DF_RIGHT;
  594. break;
  595. case SND_SOC_DAIFMT_LEFT_J:
  596. iface |= ALC5623_DAI_I2S_DF_LEFT;
  597. break;
  598. case SND_SOC_DAIFMT_DSP_A:
  599. iface |= ALC5623_DAI_I2S_DF_PCM;
  600. break;
  601. case SND_SOC_DAIFMT_DSP_B:
  602. iface |= ALC5623_DAI_I2S_DF_PCM | ALC5623_DAI_I2S_PCM_MODE;
  603. break;
  604. default:
  605. return -EINVAL;
  606. }
  607. /* clock inversion */
  608. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  609. case SND_SOC_DAIFMT_NB_NF:
  610. break;
  611. case SND_SOC_DAIFMT_IB_IF:
  612. iface |= ALC5623_DAI_MAIN_I2S_BCLK_POL_CTRL;
  613. break;
  614. case SND_SOC_DAIFMT_IB_NF:
  615. iface |= ALC5623_DAI_MAIN_I2S_BCLK_POL_CTRL;
  616. break;
  617. case SND_SOC_DAIFMT_NB_IF:
  618. break;
  619. default:
  620. return -EINVAL;
  621. }
  622. return snd_soc_write(codec, ALC5623_DAI_CONTROL, iface);
  623. }
  624. static int alc5623_pcm_hw_params(struct snd_pcm_substream *substream,
  625. struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
  626. {
  627. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  628. struct snd_soc_codec *codec = rtd->codec;
  629. struct alc5623_priv *alc5623 = snd_soc_codec_get_drvdata(codec);
  630. int coeff, rate;
  631. u16 iface;
  632. iface = snd_soc_read(codec, ALC5623_DAI_CONTROL);
  633. iface &= ~ALC5623_DAI_I2S_DL_MASK;
  634. /* bit size */
  635. switch (params_format(params)) {
  636. case SNDRV_PCM_FORMAT_S16_LE:
  637. iface |= ALC5623_DAI_I2S_DL_16;
  638. break;
  639. case SNDRV_PCM_FORMAT_S20_3LE:
  640. iface |= ALC5623_DAI_I2S_DL_20;
  641. break;
  642. case SNDRV_PCM_FORMAT_S24_LE:
  643. iface |= ALC5623_DAI_I2S_DL_24;
  644. break;
  645. case SNDRV_PCM_FORMAT_S32_LE:
  646. iface |= ALC5623_DAI_I2S_DL_32;
  647. break;
  648. default:
  649. return -EINVAL;
  650. }
  651. /* set iface & srate */
  652. snd_soc_write(codec, ALC5623_DAI_CONTROL, iface);
  653. rate = params_rate(params);
  654. coeff = get_coeff(codec, rate);
  655. if (coeff < 0)
  656. return -EINVAL;
  657. coeff = coeff_div[coeff].regvalue;
  658. dev_dbg(codec->dev, "%s: sysclk=%d,rate=%d,coeff=0x%04x\n",
  659. __func__, alc5623->sysclk, rate, coeff);
  660. snd_soc_write(codec, ALC5623_STEREO_AD_DA_CLK_CTRL, coeff);
  661. return 0;
  662. }
  663. static int alc5623_mute(struct snd_soc_dai *dai, int mute)
  664. {
  665. struct snd_soc_codec *codec = dai->codec;
  666. u16 hp_mute = ALC5623_MISC_M_DAC_L_INPUT | ALC5623_MISC_M_DAC_R_INPUT;
  667. u16 mute_reg = snd_soc_read(codec, ALC5623_MISC_CTRL) & ~hp_mute;
  668. if (mute)
  669. mute_reg |= hp_mute;
  670. return snd_soc_write(codec, ALC5623_MISC_CTRL, mute_reg);
  671. }
  672. #define ALC5623_ADD2_POWER_EN (ALC5623_PWR_ADD2_VREF \
  673. | ALC5623_PWR_ADD2_DAC_REF_CIR)
  674. #define ALC5623_ADD3_POWER_EN (ALC5623_PWR_ADD3_MAIN_BIAS \
  675. | ALC5623_PWR_ADD3_MIC1_BOOST_AD)
  676. #define ALC5623_ADD1_POWER_EN \
  677. (ALC5623_PWR_ADD1_SHORT_CURR_DET_EN | ALC5623_PWR_ADD1_SOFTGEN_EN \
  678. | ALC5623_PWR_ADD1_DEPOP_BUF_HP | ALC5623_PWR_ADD1_HP_OUT_AMP \
  679. | ALC5623_PWR_ADD1_HP_OUT_ENH_AMP)
  680. #define ALC5623_ADD1_POWER_EN_5622 \
  681. (ALC5623_PWR_ADD1_SHORT_CURR_DET_EN \
  682. | ALC5623_PWR_ADD1_HP_OUT_AMP)
  683. static void enable_power_depop(struct snd_soc_codec *codec)
  684. {
  685. struct alc5623_priv *alc5623 = snd_soc_codec_get_drvdata(codec);
  686. snd_soc_update_bits(codec, ALC5623_PWR_MANAG_ADD1,
  687. ALC5623_PWR_ADD1_SOFTGEN_EN,
  688. ALC5623_PWR_ADD1_SOFTGEN_EN);
  689. snd_soc_write(codec, ALC5623_PWR_MANAG_ADD3, ALC5623_ADD3_POWER_EN);
  690. snd_soc_update_bits(codec, ALC5623_MISC_CTRL,
  691. ALC5623_MISC_HP_DEPOP_MODE2_EN,
  692. ALC5623_MISC_HP_DEPOP_MODE2_EN);
  693. msleep(500);
  694. snd_soc_write(codec, ALC5623_PWR_MANAG_ADD2, ALC5623_ADD2_POWER_EN);
  695. /* avoid writing '1' into 5622 reserved bits */
  696. if (alc5623->id == 0x22)
  697. snd_soc_write(codec, ALC5623_PWR_MANAG_ADD1,
  698. ALC5623_ADD1_POWER_EN_5622);
  699. else
  700. snd_soc_write(codec, ALC5623_PWR_MANAG_ADD1,
  701. ALC5623_ADD1_POWER_EN);
  702. /* disable HP Depop2 */
  703. snd_soc_update_bits(codec, ALC5623_MISC_CTRL,
  704. ALC5623_MISC_HP_DEPOP_MODE2_EN,
  705. 0);
  706. }
  707. static int alc5623_set_bias_level(struct snd_soc_codec *codec,
  708. enum snd_soc_bias_level level)
  709. {
  710. switch (level) {
  711. case SND_SOC_BIAS_ON:
  712. enable_power_depop(codec);
  713. break;
  714. case SND_SOC_BIAS_PREPARE:
  715. break;
  716. case SND_SOC_BIAS_STANDBY:
  717. /* everything off except vref/vmid, */
  718. snd_soc_write(codec, ALC5623_PWR_MANAG_ADD2,
  719. ALC5623_PWR_ADD2_VREF);
  720. snd_soc_write(codec, ALC5623_PWR_MANAG_ADD3,
  721. ALC5623_PWR_ADD3_MAIN_BIAS);
  722. break;
  723. case SND_SOC_BIAS_OFF:
  724. /* everything off, dac mute, inactive */
  725. snd_soc_write(codec, ALC5623_PWR_MANAG_ADD2, 0);
  726. snd_soc_write(codec, ALC5623_PWR_MANAG_ADD3, 0);
  727. snd_soc_write(codec, ALC5623_PWR_MANAG_ADD1, 0);
  728. break;
  729. }
  730. codec->dapm.bias_level = level;
  731. return 0;
  732. }
  733. #define ALC5623_FORMATS (SNDRV_PCM_FMTBIT_S16_LE \
  734. | SNDRV_PCM_FMTBIT_S24_LE \
  735. | SNDRV_PCM_FMTBIT_S32_LE)
  736. static const struct snd_soc_dai_ops alc5623_dai_ops = {
  737. .hw_params = alc5623_pcm_hw_params,
  738. .digital_mute = alc5623_mute,
  739. .set_fmt = alc5623_set_dai_fmt,
  740. .set_sysclk = alc5623_set_dai_sysclk,
  741. .set_pll = alc5623_set_dai_pll,
  742. };
  743. static struct snd_soc_dai_driver alc5623_dai = {
  744. .name = "alc5623-hifi",
  745. .playback = {
  746. .stream_name = "Playback",
  747. .channels_min = 1,
  748. .channels_max = 2,
  749. .rate_min = 8000,
  750. .rate_max = 48000,
  751. .rates = SNDRV_PCM_RATE_8000_48000,
  752. .formats = ALC5623_FORMATS,},
  753. .capture = {
  754. .stream_name = "Capture",
  755. .channels_min = 1,
  756. .channels_max = 2,
  757. .rate_min = 8000,
  758. .rate_max = 48000,
  759. .rates = SNDRV_PCM_RATE_8000_48000,
  760. .formats = ALC5623_FORMATS,},
  761. .ops = &alc5623_dai_ops,
  762. };
  763. static int alc5623_suspend(struct snd_soc_codec *codec)
  764. {
  765. alc5623_set_bias_level(codec, SND_SOC_BIAS_OFF);
  766. return 0;
  767. }
  768. static int alc5623_resume(struct snd_soc_codec *codec)
  769. {
  770. int i, step = codec->driver->reg_cache_step;
  771. u16 *cache = codec->reg_cache;
  772. /* Sync reg_cache with the hardware */
  773. for (i = 2 ; i < codec->driver->reg_cache_size ; i += step)
  774. snd_soc_write(codec, i, cache[i]);
  775. alc5623_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  776. /* charge alc5623 caps */
  777. if (codec->dapm.suspend_bias_level == SND_SOC_BIAS_ON) {
  778. alc5623_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  779. codec->dapm.bias_level = SND_SOC_BIAS_ON;
  780. alc5623_set_bias_level(codec, codec->dapm.bias_level);
  781. }
  782. return 0;
  783. }
  784. static int alc5623_probe(struct snd_soc_codec *codec)
  785. {
  786. struct alc5623_priv *alc5623 = snd_soc_codec_get_drvdata(codec);
  787. struct snd_soc_dapm_context *dapm = &codec->dapm;
  788. int ret;
  789. ret = snd_soc_codec_set_cache_io(codec, 8, 16, alc5623->control_type);
  790. if (ret < 0) {
  791. dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
  792. return ret;
  793. }
  794. alc5623_reset(codec);
  795. alc5623_fill_cache(codec);
  796. /* power on device */
  797. alc5623_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  798. if (alc5623->add_ctrl) {
  799. snd_soc_write(codec, ALC5623_ADD_CTRL_REG,
  800. alc5623->add_ctrl);
  801. }
  802. if (alc5623->jack_det_ctrl) {
  803. snd_soc_write(codec, ALC5623_JACK_DET_CTRL,
  804. alc5623->jack_det_ctrl);
  805. }
  806. switch (alc5623->id) {
  807. case 0x21:
  808. snd_soc_add_codec_controls(codec, alc5621_vol_snd_controls,
  809. ARRAY_SIZE(alc5621_vol_snd_controls));
  810. break;
  811. case 0x22:
  812. snd_soc_add_codec_controls(codec, alc5622_vol_snd_controls,
  813. ARRAY_SIZE(alc5622_vol_snd_controls));
  814. break;
  815. case 0x23:
  816. snd_soc_add_codec_controls(codec, alc5623_vol_snd_controls,
  817. ARRAY_SIZE(alc5623_vol_snd_controls));
  818. break;
  819. default:
  820. return -EINVAL;
  821. }
  822. snd_soc_add_codec_controls(codec, alc5623_snd_controls,
  823. ARRAY_SIZE(alc5623_snd_controls));
  824. snd_soc_dapm_new_controls(dapm, alc5623_dapm_widgets,
  825. ARRAY_SIZE(alc5623_dapm_widgets));
  826. /* set up audio path interconnects */
  827. snd_soc_dapm_add_routes(dapm, intercon, ARRAY_SIZE(intercon));
  828. switch (alc5623->id) {
  829. case 0x21:
  830. case 0x22:
  831. snd_soc_dapm_new_controls(dapm, alc5623_dapm_amp_widgets,
  832. ARRAY_SIZE(alc5623_dapm_amp_widgets));
  833. snd_soc_dapm_add_routes(dapm, intercon_amp_spk,
  834. ARRAY_SIZE(intercon_amp_spk));
  835. break;
  836. case 0x23:
  837. snd_soc_dapm_add_routes(dapm, intercon_spk,
  838. ARRAY_SIZE(intercon_spk));
  839. break;
  840. default:
  841. return -EINVAL;
  842. }
  843. return ret;
  844. }
  845. /* power down chip */
  846. static int alc5623_remove(struct snd_soc_codec *codec)
  847. {
  848. alc5623_set_bias_level(codec, SND_SOC_BIAS_OFF);
  849. return 0;
  850. }
  851. static struct snd_soc_codec_driver soc_codec_device_alc5623 = {
  852. .probe = alc5623_probe,
  853. .remove = alc5623_remove,
  854. .suspend = alc5623_suspend,
  855. .resume = alc5623_resume,
  856. .set_bias_level = alc5623_set_bias_level,
  857. .reg_cache_size = ALC5623_VENDOR_ID2+2,
  858. .reg_word_size = sizeof(u16),
  859. .reg_cache_step = 2,
  860. };
  861. /*
  862. * ALC5623 2 wire address is determined by A1 pin
  863. * state during powerup.
  864. * low = 0x1a
  865. * high = 0x1b
  866. */
  867. static __devinit int alc5623_i2c_probe(struct i2c_client *client,
  868. const struct i2c_device_id *id)
  869. {
  870. struct alc5623_platform_data *pdata;
  871. struct alc5623_priv *alc5623;
  872. int ret, vid1, vid2;
  873. vid1 = i2c_smbus_read_word_data(client, ALC5623_VENDOR_ID1);
  874. if (vid1 < 0) {
  875. dev_err(&client->dev, "failed to read I2C\n");
  876. return -EIO;
  877. }
  878. vid1 = ((vid1 & 0xff) << 8) | (vid1 >> 8);
  879. vid2 = i2c_smbus_read_byte_data(client, ALC5623_VENDOR_ID2);
  880. if (vid2 < 0) {
  881. dev_err(&client->dev, "failed to read I2C\n");
  882. return -EIO;
  883. }
  884. if ((vid1 != 0x10ec) || (vid2 != id->driver_data)) {
  885. dev_err(&client->dev, "unknown or wrong codec\n");
  886. dev_err(&client->dev, "Expected %x:%lx, got %x:%x\n",
  887. 0x10ec, id->driver_data,
  888. vid1, vid2);
  889. return -ENODEV;
  890. }
  891. dev_dbg(&client->dev, "Found codec id : alc56%02x\n", vid2);
  892. alc5623 = devm_kzalloc(&client->dev, sizeof(struct alc5623_priv),
  893. GFP_KERNEL);
  894. if (alc5623 == NULL)
  895. return -ENOMEM;
  896. pdata = client->dev.platform_data;
  897. if (pdata) {
  898. alc5623->add_ctrl = pdata->add_ctrl;
  899. alc5623->jack_det_ctrl = pdata->jack_det_ctrl;
  900. }
  901. alc5623->id = vid2;
  902. switch (alc5623->id) {
  903. case 0x21:
  904. alc5623_dai.name = "alc5621-hifi";
  905. break;
  906. case 0x22:
  907. alc5623_dai.name = "alc5622-hifi";
  908. break;
  909. case 0x23:
  910. alc5623_dai.name = "alc5623-hifi";
  911. break;
  912. default:
  913. return -EINVAL;
  914. }
  915. i2c_set_clientdata(client, alc5623);
  916. alc5623->control_type = SND_SOC_I2C;
  917. ret = snd_soc_register_codec(&client->dev,
  918. &soc_codec_device_alc5623, &alc5623_dai, 1);
  919. if (ret != 0)
  920. dev_err(&client->dev, "Failed to register codec: %d\n", ret);
  921. return ret;
  922. }
  923. static __devexit int alc5623_i2c_remove(struct i2c_client *client)
  924. {
  925. snd_soc_unregister_codec(&client->dev);
  926. return 0;
  927. }
  928. static const struct i2c_device_id alc5623_i2c_table[] = {
  929. {"alc5621", 0x21},
  930. {"alc5622", 0x22},
  931. {"alc5623", 0x23},
  932. {}
  933. };
  934. MODULE_DEVICE_TABLE(i2c, alc5623_i2c_table);
  935. /* i2c codec control layer */
  936. static struct i2c_driver alc5623_i2c_driver = {
  937. .driver = {
  938. .name = "alc562x-codec",
  939. .owner = THIS_MODULE,
  940. },
  941. .probe = alc5623_i2c_probe,
  942. .remove = __devexit_p(alc5623_i2c_remove),
  943. .id_table = alc5623_i2c_table,
  944. };
  945. static int __init alc5623_modinit(void)
  946. {
  947. int ret;
  948. ret = i2c_add_driver(&alc5623_i2c_driver);
  949. if (ret != 0) {
  950. printk(KERN_ERR "%s: can't add i2c driver", __func__);
  951. return ret;
  952. }
  953. return ret;
  954. }
  955. module_init(alc5623_modinit);
  956. static void __exit alc5623_modexit(void)
  957. {
  958. i2c_del_driver(&alc5623_i2c_driver);
  959. }
  960. module_exit(alc5623_modexit);
  961. MODULE_DESCRIPTION("ASoC alc5621/2/3 driver");
  962. MODULE_AUTHOR("Arnaud Patard <arnaud.patard@rtp-net.org>");
  963. MODULE_LICENSE("GPL");