alc5623.c 33 KB

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