alc5623.c 34 KB

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