alc5632.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235
  1. /*
  2. * alc5632.c -- ALC5632 ALSA SoC Audio Codec
  3. *
  4. * Copyright (C) 2011 The AC100 Kernel Team <ac100@lists.lauchpad.net>
  5. *
  6. * Authors: Leon Romanovsky <leon@leon.nu>
  7. * Andrey Danin <danindrey@mail.ru>
  8. * Ilya Petrov <ilya.muromec@gmail.com>
  9. * Marc Dietrich <marvin24@gmx.de>
  10. *
  11. * Based on alc5623.c by Arnaud Patard
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License version 2 as
  15. * published by the Free Software Foundation.
  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/regmap.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 "alc5632.h"
  32. /*
  33. * ALC5632 register cache
  34. */
  35. static struct reg_default alc5632_reg_defaults[] = {
  36. { 2, 0x8080 }, /* R2 - Speaker Output Volume */
  37. { 4, 0x8080 }, /* R4 - Headphone Output Volume */
  38. { 6, 0x8080 }, /* R6 - AUXOUT Volume */
  39. { 8, 0xC800 }, /* R8 - Phone Input */
  40. { 10, 0xE808 }, /* R10 - LINE_IN Volume */
  41. { 12, 0x1010 }, /* R12 - STEREO DAC Input Volume */
  42. { 14, 0x0808 }, /* R14 - MIC Input Volume */
  43. { 16, 0xEE0F }, /* R16 - Stereo DAC and MIC Routing Control */
  44. { 18, 0xCBCB }, /* R18 - ADC Record Gain */
  45. { 20, 0x7F7F }, /* R20 - ADC Record Mixer Control */
  46. { 24, 0xE010 }, /* R24 - Voice DAC Volume */
  47. { 28, 0x8008 }, /* R28 - Output Mixer Control */
  48. { 34, 0x0000 }, /* R34 - Microphone Control */
  49. { 36, 0x00C0 }, /* R36 - Codec Digital MIC/Digital Boost
  50. Control */
  51. { 46, 0x0000 }, /* R46 - Stereo DAC/Voice DAC/Stereo ADC
  52. Function Select */
  53. { 52, 0x8000 }, /* R52 - Main Serial Data Port Control
  54. (Stereo I2S) */
  55. { 54, 0x0000 }, /* R54 - Extend Serial Data Port Control
  56. (VoDAC_I2S/PCM) */
  57. { 58, 0x0000 }, /* R58 - Power Management Addition 1 */
  58. { 60, 0x0000 }, /* R60 - Power Management Addition 2 */
  59. { 62, 0x8000 }, /* R62 - Power Management Addition 3 */
  60. { 64, 0x0C0A }, /* R64 - General Purpose Control Register 1 */
  61. { 66, 0x0000 }, /* R66 - General Purpose Control Register 2 */
  62. { 68, 0x0000 }, /* R68 - PLL1 Control */
  63. { 70, 0x0000 }, /* R70 - PLL2 Control */
  64. { 76, 0xBE3E }, /* R76 - GPIO Pin Configuration */
  65. { 78, 0xBE3E }, /* R78 - GPIO Pin Polarity */
  66. { 80, 0x0000 }, /* R80 - GPIO Pin Sticky */
  67. { 82, 0x0000 }, /* R82 - GPIO Pin Wake Up */
  68. { 86, 0x0000 }, /* R86 - Pin Sharing */
  69. { 90, 0x0009 }, /* R90 - Soft Volume Control Setting */
  70. { 92, 0x0000 }, /* R92 - GPIO_Output Pin Control */
  71. { 94, 0x3000 }, /* R94 - MISC Control */
  72. { 96, 0x3075 }, /* R96 - Stereo DAC Clock Control_1 */
  73. { 98, 0x1010 }, /* R98 - Stereo DAC Clock Control_2 */
  74. { 100, 0x3110 }, /* R100 - VoDAC_PCM Clock Control_1 */
  75. { 104, 0x0553 }, /* R104 - Pseudo Stereo and Spatial Effect
  76. Block Control */
  77. { 106, 0x0000 }, /* R106 - Private Register Address */
  78. };
  79. /* codec private data */
  80. struct alc5632_priv {
  81. struct regmap *regmap;
  82. u8 id;
  83. unsigned int sysclk;
  84. };
  85. static bool alc5632_volatile_register(struct device *dev,
  86. unsigned int reg)
  87. {
  88. switch (reg) {
  89. case ALC5632_RESET:
  90. case ALC5632_PWR_DOWN_CTRL_STATUS:
  91. case ALC5632_GPIO_PIN_STATUS:
  92. case ALC5632_OVER_CURR_STATUS:
  93. case ALC5632_HID_CTRL_DATA:
  94. case ALC5632_EQ_CTRL:
  95. case ALC5632_VENDOR_ID1:
  96. case ALC5632_VENDOR_ID2:
  97. return true;
  98. default:
  99. break;
  100. }
  101. return false;
  102. }
  103. static inline int alc5632_reset(struct regmap *map)
  104. {
  105. return regmap_write(map, ALC5632_RESET, 0x59B4);
  106. }
  107. static int amp_mixer_event(struct snd_soc_dapm_widget *w,
  108. struct snd_kcontrol *kcontrol, int event)
  109. {
  110. /* to power-on/off class-d amp generators/speaker */
  111. /* need to write to 'index-46h' register : */
  112. /* so write index num (here 0x46) to reg 0x6a */
  113. /* and then 0xffff/0 to reg 0x6c */
  114. snd_soc_write(w->codec, ALC5632_HID_CTRL_INDEX, 0x46);
  115. switch (event) {
  116. case SND_SOC_DAPM_PRE_PMU:
  117. snd_soc_write(w->codec, ALC5632_HID_CTRL_DATA, 0xFFFF);
  118. break;
  119. case SND_SOC_DAPM_POST_PMD:
  120. snd_soc_write(w->codec, ALC5632_HID_CTRL_DATA, 0);
  121. break;
  122. }
  123. return 0;
  124. }
  125. /*
  126. * ALC5632 Controls
  127. */
  128. /* -34.5db min scale, 1.5db steps, no mute */
  129. static const DECLARE_TLV_DB_SCALE(vol_tlv, -3450, 150, 0);
  130. /* -46.5db min scale, 1.5db steps, no mute */
  131. static const DECLARE_TLV_DB_SCALE(hp_tlv, -4650, 150, 0);
  132. /* -16.5db min scale, 1.5db steps, no mute */
  133. static const DECLARE_TLV_DB_SCALE(adc_rec_tlv, -1650, 150, 0);
  134. static const unsigned int boost_tlv[] = {
  135. TLV_DB_RANGE_HEAD(2),
  136. 0, 1, TLV_DB_SCALE_ITEM(0, 2000, 0),
  137. 1, 3, TLV_DB_SCALE_ITEM(2000, 1000, 0),
  138. };
  139. /* 0db min scale, 6 db steps, no mute */
  140. static const DECLARE_TLV_DB_SCALE(dig_tlv, 0, 600, 0);
  141. /* 0db min scalem 0.75db steps, no mute */
  142. static const DECLARE_TLV_DB_SCALE(vdac_tlv, -3525, 75, 0);
  143. static const struct snd_kcontrol_new alc5632_vol_snd_controls[] = {
  144. /* left starts at bit 8, right at bit 0 */
  145. /* 31 steps (5 bit), -46.5db scale */
  146. SOC_DOUBLE_TLV("Speaker Playback Volume",
  147. ALC5632_SPK_OUT_VOL, 8, 0, 31, 1, hp_tlv),
  148. /* bit 15 mutes left, bit 7 right */
  149. SOC_DOUBLE("Speaker Playback Switch",
  150. ALC5632_SPK_OUT_VOL, 15, 7, 1, 1),
  151. SOC_DOUBLE_TLV("Headphone Playback Volume",
  152. ALC5632_HP_OUT_VOL, 8, 0, 31, 1, hp_tlv),
  153. SOC_DOUBLE("Headphone Playback Switch",
  154. ALC5632_HP_OUT_VOL, 15, 7, 1, 1),
  155. };
  156. static const struct snd_kcontrol_new alc5632_snd_controls[] = {
  157. SOC_DOUBLE_TLV("Auxout Playback Volume",
  158. ALC5632_AUX_OUT_VOL, 8, 0, 31, 1, hp_tlv),
  159. SOC_DOUBLE("Auxout Playback Switch",
  160. ALC5632_AUX_OUT_VOL, 15, 7, 1, 1),
  161. SOC_SINGLE_TLV("Voice DAC Playback Volume",
  162. ALC5632_VOICE_DAC_VOL, 0, 63, 0, vdac_tlv),
  163. SOC_SINGLE("Voice DAC Playback Switch",
  164. ALC5632_VOICE_DAC_VOL, 12, 1, 1),
  165. SOC_SINGLE_TLV("Phone Playback Volume",
  166. ALC5632_PHONE_IN_VOL, 8, 31, 1, vol_tlv),
  167. SOC_DOUBLE_TLV("LineIn Playback Volume",
  168. ALC5632_LINE_IN_VOL, 8, 0, 31, 1, vol_tlv),
  169. SOC_DOUBLE_TLV("Master Playback Volume",
  170. ALC5632_STEREO_DAC_IN_VOL, 8, 0, 63, 1, vdac_tlv),
  171. SOC_DOUBLE("Master Playback Switch",
  172. ALC5632_STEREO_DAC_IN_VOL, 15, 7, 1, 1),
  173. SOC_SINGLE_TLV("Mic1 Playback Volume",
  174. ALC5632_MIC_VOL, 8, 31, 1, vol_tlv),
  175. SOC_SINGLE_TLV("Mic2 Playback Volume",
  176. ALC5632_MIC_VOL, 0, 31, 1, vol_tlv),
  177. SOC_DOUBLE_TLV("Rec Capture Volume",
  178. ALC5632_ADC_REC_GAIN, 8, 0, 31, 0, adc_rec_tlv),
  179. SOC_SINGLE_TLV("Mic 1 Boost Volume",
  180. ALC5632_MIC_CTRL, 10, 3, 0, boost_tlv),
  181. SOC_SINGLE_TLV("Mic 2 Boost Volume",
  182. ALC5632_MIC_CTRL, 8, 3, 0, boost_tlv),
  183. SOC_SINGLE_TLV("DMIC Boost Capture Volume",
  184. ALC5632_DIGI_BOOST_CTRL, 0, 7, 0, dig_tlv),
  185. SOC_SINGLE("DMIC En Capture Switch",
  186. ALC5632_DIGI_BOOST_CTRL, 15, 1, 0),
  187. SOC_SINGLE("DMIC PreFilter Capture Switch",
  188. ALC5632_DIGI_BOOST_CTRL, 12, 1, 0),
  189. };
  190. /*
  191. * DAPM Controls
  192. */
  193. static const struct snd_kcontrol_new alc5632_hp_mixer_controls[] = {
  194. SOC_DAPM_SINGLE("LI2HP Playback Switch", ALC5632_LINE_IN_VOL, 15, 1, 1),
  195. SOC_DAPM_SINGLE("PHONE2HP Playback Switch", ALC5632_PHONE_IN_VOL, 15, 1, 1),
  196. SOC_DAPM_SINGLE("MIC12HP Playback Switch", ALC5632_MIC_ROUTING_CTRL, 15, 1, 1),
  197. SOC_DAPM_SINGLE("MIC22HP Playback Switch", ALC5632_MIC_ROUTING_CTRL, 11, 1, 1),
  198. SOC_DAPM_SINGLE("VOICE2HP Playback Switch", ALC5632_VOICE_DAC_VOL, 15, 1, 1),
  199. };
  200. static const struct snd_kcontrol_new alc5632_hpl_mixer_controls[] = {
  201. SOC_DAPM_SINGLE("ADC2HP_L Playback Switch", ALC5632_ADC_REC_GAIN, 15, 1, 1),
  202. SOC_DAPM_SINGLE("DACL2HP Playback Switch", ALC5632_MIC_ROUTING_CTRL, 3, 1, 1),
  203. };
  204. static const struct snd_kcontrol_new alc5632_hpr_mixer_controls[] = {
  205. SOC_DAPM_SINGLE("ADC2HP_R Playback Switch", ALC5632_ADC_REC_GAIN, 7, 1, 1),
  206. SOC_DAPM_SINGLE("DACR2HP Playback Switch", ALC5632_MIC_ROUTING_CTRL, 2, 1, 1),
  207. };
  208. static const struct snd_kcontrol_new alc5632_mono_mixer_controls[] = {
  209. SOC_DAPM_SINGLE("ADC2MONO_L Playback Switch", ALC5632_ADC_REC_GAIN, 14, 1, 1),
  210. SOC_DAPM_SINGLE("ADC2MONO_R Playback Switch", ALC5632_ADC_REC_GAIN, 6, 1, 1),
  211. SOC_DAPM_SINGLE("LI2MONO Playback Switch", ALC5632_LINE_IN_VOL, 13, 1, 1),
  212. SOC_DAPM_SINGLE("MIC12MONO Playback Switch",
  213. ALC5632_MIC_ROUTING_CTRL, 13, 1, 1),
  214. SOC_DAPM_SINGLE("MIC22MONO Playback Switch",
  215. ALC5632_MIC_ROUTING_CTRL, 9, 1, 1),
  216. SOC_DAPM_SINGLE("DAC2MONO Playback Switch", ALC5632_MIC_ROUTING_CTRL, 0, 1, 1),
  217. SOC_DAPM_SINGLE("VOICE2MONO Playback Switch", ALC5632_VOICE_DAC_VOL, 13, 1, 1),
  218. };
  219. static const struct snd_kcontrol_new alc5632_speaker_mixer_controls[] = {
  220. SOC_DAPM_SINGLE("LI2SPK Playback Switch", ALC5632_LINE_IN_VOL, 14, 1, 1),
  221. SOC_DAPM_SINGLE("PHONE2SPK Playback Switch", ALC5632_PHONE_IN_VOL, 14, 1, 1),
  222. SOC_DAPM_SINGLE("MIC12SPK Playback Switch",
  223. ALC5632_MIC_ROUTING_CTRL, 14, 1, 1),
  224. SOC_DAPM_SINGLE("MIC22SPK Playback Switch",
  225. ALC5632_MIC_ROUTING_CTRL, 10, 1, 1),
  226. SOC_DAPM_SINGLE("DAC2SPK Playback Switch", ALC5632_MIC_ROUTING_CTRL, 1, 1, 1),
  227. SOC_DAPM_SINGLE("VOICE2SPK Playback Switch", ALC5632_VOICE_DAC_VOL, 14, 1, 1),
  228. };
  229. /* Left Record Mixer */
  230. static const struct snd_kcontrol_new alc5632_captureL_mixer_controls[] = {
  231. SOC_DAPM_SINGLE("MIC12REC_L Capture Switch", ALC5632_ADC_REC_MIXER, 14, 1, 1),
  232. SOC_DAPM_SINGLE("MIC22REC_L Capture Switch", ALC5632_ADC_REC_MIXER, 13, 1, 1),
  233. SOC_DAPM_SINGLE("LIL2REC Capture Switch", ALC5632_ADC_REC_MIXER, 12, 1, 1),
  234. SOC_DAPM_SINGLE("PH2REC_L Capture Switch", ALC5632_ADC_REC_MIXER, 11, 1, 1),
  235. SOC_DAPM_SINGLE("HPL2REC Capture Switch", ALC5632_ADC_REC_MIXER, 10, 1, 1),
  236. SOC_DAPM_SINGLE("SPK2REC_L Capture Switch", ALC5632_ADC_REC_MIXER, 9, 1, 1),
  237. SOC_DAPM_SINGLE("MONO2REC_L Capture Switch", ALC5632_ADC_REC_MIXER, 8, 1, 1),
  238. };
  239. /* Right Record Mixer */
  240. static const struct snd_kcontrol_new alc5632_captureR_mixer_controls[] = {
  241. SOC_DAPM_SINGLE("MIC12REC_R Capture Switch", ALC5632_ADC_REC_MIXER, 6, 1, 1),
  242. SOC_DAPM_SINGLE("MIC22REC_R Capture Switch", ALC5632_ADC_REC_MIXER, 5, 1, 1),
  243. SOC_DAPM_SINGLE("LIR2REC Capture Switch", ALC5632_ADC_REC_MIXER, 4, 1, 1),
  244. SOC_DAPM_SINGLE("PH2REC_R Capture Switch", ALC5632_ADC_REC_MIXER, 3, 1, 1),
  245. SOC_DAPM_SINGLE("HPR2REC Capture Switch", ALC5632_ADC_REC_MIXER, 2, 1, 1),
  246. SOC_DAPM_SINGLE("SPK2REC_R Capture Switch", ALC5632_ADC_REC_MIXER, 1, 1, 1),
  247. SOC_DAPM_SINGLE("MONO2REC_R Capture Switch", ALC5632_ADC_REC_MIXER, 0, 1, 1),
  248. };
  249. /* Dmic Mixer */
  250. static const struct snd_kcontrol_new alc5632_dmicl_mixer_controls[] = {
  251. SOC_DAPM_SINGLE("DMICL2ADC Capture Switch", ALC5632_DIGI_BOOST_CTRL, 7, 1, 1),
  252. };
  253. static const struct snd_kcontrol_new alc5632_dmicr_mixer_controls[] = {
  254. SOC_DAPM_SINGLE("DMICR2ADC Capture Switch", ALC5632_DIGI_BOOST_CTRL, 6, 1, 1),
  255. };
  256. static const char * const alc5632_spk_n_sour_sel[] = {
  257. "RN/-R", "RP/+R", "LN/-R", "Mute"};
  258. static const char * const alc5632_hpl_out_input_sel[] = {
  259. "Vmid", "HP Left Mix"};
  260. static const char * const alc5632_hpr_out_input_sel[] = {
  261. "Vmid", "HP Right Mix"};
  262. static const char * const alc5632_spkout_input_sel[] = {
  263. "Vmid", "HPOut Mix", "Speaker Mix", "Mono Mix"};
  264. static const char * const alc5632_aux_out_input_sel[] = {
  265. "Vmid", "HPOut Mix", "Speaker Mix", "Mono Mix"};
  266. static const char * const alc5632_adcr_func_sel[] = {
  267. "Stereo ADC", "Voice ADC"};
  268. static const char * const alc5632_i2s_out_sel[] = {
  269. "ADC LR", "Voice Stereo Digital"};
  270. /* auxout output mux */
  271. static const struct soc_enum alc5632_aux_out_input_enum =
  272. SOC_ENUM_SINGLE(ALC5632_OUTPUT_MIXER_CTRL, 6, 4, alc5632_aux_out_input_sel);
  273. static const struct snd_kcontrol_new alc5632_auxout_mux_controls =
  274. SOC_DAPM_ENUM("AuxOut Mux", alc5632_aux_out_input_enum);
  275. /* speaker output mux */
  276. static const struct soc_enum alc5632_spkout_input_enum =
  277. SOC_ENUM_SINGLE(ALC5632_OUTPUT_MIXER_CTRL, 10, 4, alc5632_spkout_input_sel);
  278. static const struct snd_kcontrol_new alc5632_spkout_mux_controls =
  279. SOC_DAPM_ENUM("SpeakerOut Mux", alc5632_spkout_input_enum);
  280. /* headphone left output mux */
  281. static const struct soc_enum alc5632_hpl_out_input_enum =
  282. SOC_ENUM_SINGLE(ALC5632_OUTPUT_MIXER_CTRL, 9, 2, alc5632_hpl_out_input_sel);
  283. static const struct snd_kcontrol_new alc5632_hpl_out_mux_controls =
  284. SOC_DAPM_ENUM("Left Headphone Mux", alc5632_hpl_out_input_enum);
  285. /* headphone right output mux */
  286. static const struct soc_enum alc5632_hpr_out_input_enum =
  287. SOC_ENUM_SINGLE(ALC5632_OUTPUT_MIXER_CTRL, 8, 2, alc5632_hpr_out_input_sel);
  288. static const struct snd_kcontrol_new alc5632_hpr_out_mux_controls =
  289. SOC_DAPM_ENUM("Right Headphone Mux", alc5632_hpr_out_input_enum);
  290. /* speaker output N select */
  291. static const struct soc_enum alc5632_spk_n_sour_enum =
  292. SOC_ENUM_SINGLE(ALC5632_OUTPUT_MIXER_CTRL, 14, 4, alc5632_spk_n_sour_sel);
  293. static const struct snd_kcontrol_new alc5632_spkoutn_mux_controls =
  294. SOC_DAPM_ENUM("SpeakerOut N Mux", alc5632_spk_n_sour_enum);
  295. /* speaker amplifier */
  296. static const char *alc5632_amp_names[] = {"AB Amp", "D Amp"};
  297. static const struct soc_enum alc5632_amp_enum =
  298. SOC_ENUM_SINGLE(ALC5632_OUTPUT_MIXER_CTRL, 13, 2, alc5632_amp_names);
  299. static const struct snd_kcontrol_new alc5632_amp_mux_controls =
  300. SOC_DAPM_ENUM("AB-D Amp Mux", alc5632_amp_enum);
  301. /* ADC output select */
  302. static const struct soc_enum alc5632_adcr_func_enum =
  303. SOC_ENUM_SINGLE(ALC5632_DAC_FUNC_SELECT, 5, 2, alc5632_adcr_func_sel);
  304. static const struct snd_kcontrol_new alc5632_adcr_func_controls =
  305. SOC_DAPM_ENUM("ADCR Mux", alc5632_adcr_func_enum);
  306. /* I2S out select */
  307. static const struct soc_enum alc5632_i2s_out_enum =
  308. SOC_ENUM_SINGLE(ALC5632_I2S_OUT_CTL, 5, 2, alc5632_i2s_out_sel);
  309. static const struct snd_kcontrol_new alc5632_i2s_out_controls =
  310. SOC_DAPM_ENUM("I2SOut Mux", alc5632_i2s_out_enum);
  311. static const struct snd_soc_dapm_widget alc5632_dapm_widgets[] = {
  312. /* Muxes */
  313. SND_SOC_DAPM_MUX("AuxOut Mux", SND_SOC_NOPM, 0, 0,
  314. &alc5632_auxout_mux_controls),
  315. SND_SOC_DAPM_MUX("SpeakerOut Mux", SND_SOC_NOPM, 0, 0,
  316. &alc5632_spkout_mux_controls),
  317. SND_SOC_DAPM_MUX("Left Headphone Mux", SND_SOC_NOPM, 0, 0,
  318. &alc5632_hpl_out_mux_controls),
  319. SND_SOC_DAPM_MUX("Right Headphone Mux", SND_SOC_NOPM, 0, 0,
  320. &alc5632_hpr_out_mux_controls),
  321. SND_SOC_DAPM_MUX("SpeakerOut N Mux", SND_SOC_NOPM, 0, 0,
  322. &alc5632_spkoutn_mux_controls),
  323. SND_SOC_DAPM_MUX("ADCR Mux", SND_SOC_NOPM, 0, 0,
  324. &alc5632_adcr_func_controls),
  325. SND_SOC_DAPM_MUX("I2SOut Mux", ALC5632_PWR_MANAG_ADD1, 11, 0,
  326. &alc5632_i2s_out_controls),
  327. /* output mixers */
  328. SND_SOC_DAPM_MIXER("HP Mix", SND_SOC_NOPM, 0, 0,
  329. &alc5632_hp_mixer_controls[0],
  330. ARRAY_SIZE(alc5632_hp_mixer_controls)),
  331. SND_SOC_DAPM_MIXER("HPR Mix", ALC5632_PWR_MANAG_ADD2, 4, 0,
  332. &alc5632_hpr_mixer_controls[0],
  333. ARRAY_SIZE(alc5632_hpr_mixer_controls)),
  334. SND_SOC_DAPM_MIXER("HPL Mix", ALC5632_PWR_MANAG_ADD2, 5, 0,
  335. &alc5632_hpl_mixer_controls[0],
  336. ARRAY_SIZE(alc5632_hpl_mixer_controls)),
  337. SND_SOC_DAPM_MIXER("HPOut Mix", SND_SOC_NOPM, 0, 0, NULL, 0),
  338. SND_SOC_DAPM_MIXER("Mono Mix", ALC5632_PWR_MANAG_ADD2, 2, 0,
  339. &alc5632_mono_mixer_controls[0],
  340. ARRAY_SIZE(alc5632_mono_mixer_controls)),
  341. SND_SOC_DAPM_MIXER("Speaker Mix", ALC5632_PWR_MANAG_ADD2, 3, 0,
  342. &alc5632_speaker_mixer_controls[0],
  343. ARRAY_SIZE(alc5632_speaker_mixer_controls)),
  344. SND_SOC_DAPM_MIXER("DMICL Mix", SND_SOC_NOPM, 0, 0,
  345. &alc5632_dmicl_mixer_controls[0],
  346. ARRAY_SIZE(alc5632_dmicl_mixer_controls)),
  347. SND_SOC_DAPM_MIXER("DMICR Mix", SND_SOC_NOPM, 0, 0,
  348. &alc5632_dmicr_mixer_controls[0],
  349. ARRAY_SIZE(alc5632_dmicr_mixer_controls)),
  350. /* input mixers */
  351. SND_SOC_DAPM_MIXER("Left Capture Mix", ALC5632_PWR_MANAG_ADD2, 1, 0,
  352. &alc5632_captureL_mixer_controls[0],
  353. ARRAY_SIZE(alc5632_captureL_mixer_controls)),
  354. SND_SOC_DAPM_MIXER("Right Capture Mix", ALC5632_PWR_MANAG_ADD2, 0, 0,
  355. &alc5632_captureR_mixer_controls[0],
  356. ARRAY_SIZE(alc5632_captureR_mixer_controls)),
  357. SND_SOC_DAPM_AIF_IN("AIFRXL", "Left HiFi Playback", 0, SND_SOC_NOPM, 0, 0),
  358. SND_SOC_DAPM_AIF_IN("AIFRXR", "Right HiFi Playback", 0, SND_SOC_NOPM, 0, 0),
  359. SND_SOC_DAPM_AIF_OUT("AIFTXL", "Left HiFi Capture", 0, SND_SOC_NOPM, 0, 0),
  360. SND_SOC_DAPM_AIF_OUT("AIFTXR", "Right HiFi Capture", 0, SND_SOC_NOPM, 0, 0),
  361. SND_SOC_DAPM_AIF_IN("VAIFRX", "Voice Playback", 0, SND_SOC_NOPM, 0, 0),
  362. SND_SOC_DAPM_AIF_OUT("VAIFTX", "Voice Capture", 0, SND_SOC_NOPM, 0, 0),
  363. SND_SOC_DAPM_DAC("Voice DAC", NULL, ALC5632_PWR_MANAG_ADD2, 10, 0),
  364. SND_SOC_DAPM_DAC("Left DAC", NULL, ALC5632_PWR_MANAG_ADD2, 9, 0),
  365. SND_SOC_DAPM_DAC("Right DAC", NULL, ALC5632_PWR_MANAG_ADD2, 8, 0),
  366. SND_SOC_DAPM_ADC("Left ADC", NULL, ALC5632_PWR_MANAG_ADD2, 7, 0),
  367. SND_SOC_DAPM_ADC("Right ADC", NULL, ALC5632_PWR_MANAG_ADD2, 6, 0),
  368. SND_SOC_DAPM_MIXER("DAC Left Channel", ALC5632_PWR_MANAG_ADD1, 15, 0, NULL, 0),
  369. SND_SOC_DAPM_MIXER("DAC Right Channel",
  370. ALC5632_PWR_MANAG_ADD1, 14, 0, NULL, 0),
  371. SND_SOC_DAPM_MIXER("I2S Mix", ALC5632_PWR_MANAG_ADD1, 11, 0, NULL, 0),
  372. SND_SOC_DAPM_MIXER("Phone Mix", SND_SOC_NOPM, 0, 0, NULL, 0),
  373. SND_SOC_DAPM_MIXER("Line Mix", SND_SOC_NOPM, 0, 0, NULL, 0),
  374. SND_SOC_DAPM_MIXER("Voice Mix", SND_SOC_NOPM, 0, 0, NULL, 0),
  375. SND_SOC_DAPM_MIXER("ADCLR", SND_SOC_NOPM, 0, 0, NULL, 0),
  376. SND_SOC_DAPM_PGA("Left Headphone", ALC5632_PWR_MANAG_ADD3, 11, 0, NULL, 0),
  377. SND_SOC_DAPM_PGA("Right Headphone", ALC5632_PWR_MANAG_ADD3, 10, 0, NULL, 0),
  378. SND_SOC_DAPM_PGA("Left Speaker", ALC5632_PWR_MANAG_ADD3, 13, 0, NULL, 0),
  379. SND_SOC_DAPM_PGA("Right Speaker", ALC5632_PWR_MANAG_ADD3, 12, 0, NULL, 0),
  380. SND_SOC_DAPM_PGA("Aux Out", ALC5632_PWR_MANAG_ADD3, 14, 0, NULL, 0),
  381. SND_SOC_DAPM_PGA("Left LineIn", ALC5632_PWR_MANAG_ADD3, 7, 0, NULL, 0),
  382. SND_SOC_DAPM_PGA("Right LineIn", ALC5632_PWR_MANAG_ADD3, 6, 0, NULL, 0),
  383. SND_SOC_DAPM_PGA("Phone", ALC5632_PWR_MANAG_ADD3, 5, 0, NULL, 0),
  384. SND_SOC_DAPM_PGA("Phone ADMix", ALC5632_PWR_MANAG_ADD3, 4, 0, NULL, 0),
  385. SND_SOC_DAPM_PGA("MIC1 PGA", ALC5632_PWR_MANAG_ADD3, 3, 0, NULL, 0),
  386. SND_SOC_DAPM_PGA("MIC2 PGA", ALC5632_PWR_MANAG_ADD3, 2, 0, NULL, 0),
  387. SND_SOC_DAPM_PGA("MIC1 Pre Amp", ALC5632_PWR_MANAG_ADD3, 1, 0, NULL, 0),
  388. SND_SOC_DAPM_PGA("MIC2 Pre Amp", ALC5632_PWR_MANAG_ADD3, 0, 0, NULL, 0),
  389. SND_SOC_DAPM_SUPPLY("MICBIAS1", ALC5632_PWR_MANAG_ADD1, 3, 0, NULL, 0),
  390. SND_SOC_DAPM_SUPPLY("MICBIAS2", ALC5632_PWR_MANAG_ADD1, 2, 0, NULL, 0),
  391. SND_SOC_DAPM_PGA_E("D Amp", ALC5632_PWR_MANAG_ADD2, 14, 0, NULL, 0,
  392. amp_mixer_event, SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  393. SND_SOC_DAPM_PGA("AB Amp", ALC5632_PWR_MANAG_ADD2, 15, 0, NULL, 0),
  394. SND_SOC_DAPM_MUX("AB-D Amp Mux", ALC5632_PWR_MANAG_ADD1, 10, 0,
  395. &alc5632_amp_mux_controls),
  396. SND_SOC_DAPM_OUTPUT("AUXOUT"),
  397. SND_SOC_DAPM_OUTPUT("HPL"),
  398. SND_SOC_DAPM_OUTPUT("HPR"),
  399. SND_SOC_DAPM_OUTPUT("SPKOUT"),
  400. SND_SOC_DAPM_OUTPUT("SPKOUTN"),
  401. SND_SOC_DAPM_INPUT("LINEINL"),
  402. SND_SOC_DAPM_INPUT("LINEINR"),
  403. SND_SOC_DAPM_INPUT("PHONEP"),
  404. SND_SOC_DAPM_INPUT("PHONEN"),
  405. SND_SOC_DAPM_INPUT("DMICDAT"),
  406. SND_SOC_DAPM_INPUT("MIC1"),
  407. SND_SOC_DAPM_INPUT("MIC2"),
  408. SND_SOC_DAPM_VMID("Vmid"),
  409. };
  410. static const struct snd_soc_dapm_route alc5632_dapm_routes[] = {
  411. /* Playback streams */
  412. {"Left DAC", NULL, "AIFRXL"},
  413. {"Right DAC", NULL, "AIFRXR"},
  414. /* virtual mixer - mixes left & right channels */
  415. {"I2S Mix", NULL, "Left DAC"},
  416. {"I2S Mix", NULL, "Right DAC"},
  417. {"Line Mix", NULL, "Right LineIn"},
  418. {"Line Mix", NULL, "Left LineIn"},
  419. {"Phone Mix", NULL, "Phone"},
  420. {"Phone Mix", NULL, "Phone ADMix"},
  421. {"AUXOUT", NULL, "Aux Out"},
  422. /* DAC */
  423. {"DAC Right Channel", NULL, "I2S Mix"},
  424. {"DAC Left Channel", NULL, "I2S Mix"},
  425. /* HP mixer */
  426. {"HPL Mix", "ADC2HP_L Playback Switch", "Left Capture Mix"},
  427. {"HPL Mix", NULL, "HP Mix"},
  428. {"HPR Mix", "ADC2HP_R Playback Switch", "Right Capture Mix"},
  429. {"HPR Mix", NULL, "HP Mix"},
  430. {"HP Mix", "LI2HP Playback Switch", "Line Mix"},
  431. {"HP Mix", "PHONE2HP Playback Switch", "Phone Mix"},
  432. {"HP Mix", "MIC12HP Playback Switch", "MIC1 PGA"},
  433. {"HP Mix", "MIC22HP Playback Switch", "MIC2 PGA"},
  434. {"HP Mix", "VOICE2HP Playback Switch", "Voice Mix"},
  435. {"HPR Mix", "DACR2HP Playback Switch", "DAC Right Channel"},
  436. {"HPL Mix", "DACL2HP Playback Switch", "DAC Left Channel"},
  437. {"HPOut Mix", NULL, "HP Mix"},
  438. {"HPOut Mix", NULL, "HPR Mix"},
  439. {"HPOut Mix", NULL, "HPL Mix"},
  440. /* speaker mixer */
  441. {"Speaker Mix", "LI2SPK Playback Switch", "Line Mix"},
  442. {"Speaker Mix", "PHONE2SPK Playback Switch", "Phone Mix"},
  443. {"Speaker Mix", "MIC12SPK Playback Switch", "MIC1 PGA"},
  444. {"Speaker Mix", "MIC22SPK Playback Switch", "MIC2 PGA"},
  445. {"Speaker Mix", "DAC2SPK Playback Switch", "DAC Left Channel"},
  446. {"Speaker Mix", "VOICE2SPK Playback Switch", "Voice Mix"},
  447. /* mono mixer */
  448. {"Mono Mix", "ADC2MONO_L Playback Switch", "Left Capture Mix"},
  449. {"Mono Mix", "ADC2MONO_R Playback Switch", "Right Capture Mix"},
  450. {"Mono Mix", "LI2MONO Playback Switch", "Line Mix"},
  451. {"Mono Mix", "MIC12MONO Playback Switch", "MIC1 PGA"},
  452. {"Mono Mix", "MIC22MONO Playback Switch", "MIC2 PGA"},
  453. {"Mono Mix", "DAC2MONO Playback Switch", "DAC Left Channel"},
  454. {"Mono Mix", "VOICE2MONO Playback Switch", "Voice Mix"},
  455. /* Left record mixer */
  456. {"Left Capture Mix", "LIL2REC Capture Switch", "LINEINL"},
  457. {"Left Capture Mix", "PH2REC_L Capture Switch", "PHONEN"},
  458. {"Left Capture Mix", "MIC12REC_L Capture Switch", "MIC1 Pre Amp"},
  459. {"Left Capture Mix", "MIC22REC_L Capture Switch", "MIC2 Pre Amp"},
  460. {"Left Capture Mix", "HPL2REC Capture Switch", "HPL Mix"},
  461. {"Left Capture Mix", "SPK2REC_L Capture Switch", "Speaker Mix"},
  462. {"Left Capture Mix", "MONO2REC_L Capture Switch", "Mono Mix"},
  463. /*Right record mixer */
  464. {"Right Capture Mix", "LIR2REC Capture Switch", "LINEINR"},
  465. {"Right Capture Mix", "PH2REC_R Capture Switch", "PHONEP"},
  466. {"Right Capture Mix", "MIC12REC_R Capture Switch", "MIC1 Pre Amp"},
  467. {"Right Capture Mix", "MIC22REC_R Capture Switch", "MIC2 Pre Amp"},
  468. {"Right Capture Mix", "HPR2REC Capture Switch", "HPR Mix"},
  469. {"Right Capture Mix", "SPK2REC_R Capture Switch", "Speaker Mix"},
  470. {"Right Capture Mix", "MONO2REC_R Capture Switch", "Mono Mix"},
  471. /* headphone left mux */
  472. {"Left Headphone Mux", "HP Left Mix", "HPL Mix"},
  473. {"Left Headphone Mux", "Vmid", "Vmid"},
  474. /* headphone right mux */
  475. {"Right Headphone Mux", "HP Right Mix", "HPR Mix"},
  476. {"Right Headphone Mux", "Vmid", "Vmid"},
  477. /* speaker out mux */
  478. {"SpeakerOut Mux", "Vmid", "Vmid"},
  479. {"SpeakerOut Mux", "HPOut Mix", "HPOut Mix"},
  480. {"SpeakerOut Mux", "Speaker Mix", "Speaker Mix"},
  481. {"SpeakerOut Mux", "Mono Mix", "Mono Mix"},
  482. /* Mono/Aux Out mux */
  483. {"AuxOut Mux", "Vmid", "Vmid"},
  484. {"AuxOut Mux", "HPOut Mix", "HPOut Mix"},
  485. {"AuxOut Mux", "Speaker Mix", "Speaker Mix"},
  486. {"AuxOut Mux", "Mono Mix", "Mono Mix"},
  487. /* output pga */
  488. {"HPL", NULL, "Left Headphone"},
  489. {"Left Headphone", NULL, "Left Headphone Mux"},
  490. {"HPR", NULL, "Right Headphone"},
  491. {"Right Headphone", NULL, "Right Headphone Mux"},
  492. {"Aux Out", NULL, "AuxOut Mux"},
  493. /* input pga */
  494. {"Left LineIn", NULL, "LINEINL"},
  495. {"Right LineIn", NULL, "LINEINR"},
  496. {"Phone", NULL, "PHONEP"},
  497. {"MIC1 Pre Amp", NULL, "MIC1"},
  498. {"MIC2 Pre Amp", NULL, "MIC2"},
  499. {"MIC1 PGA", NULL, "MIC1 Pre Amp"},
  500. {"MIC2 PGA", NULL, "MIC2 Pre Amp"},
  501. /* left ADC */
  502. {"Left ADC", NULL, "Left Capture Mix"},
  503. {"DMICL Mix", "DMICL2ADC Capture Switch", "DMICDAT"},
  504. {"Left ADC", NULL, "DMICL Mix"},
  505. {"ADCLR", NULL, "Left ADC"},
  506. /* right ADC */
  507. {"Right ADC", NULL, "Right Capture Mix"},
  508. {"DMICR Mix", "DMICR2ADC Capture Switch", "DMICDAT"},
  509. {"Right ADC", NULL, "DMICR Mix"},
  510. {"ADCR Mux", "Stereo ADC", "Right ADC"},
  511. {"ADCR Mux", "Voice ADC", "Right ADC"},
  512. {"ADCLR", NULL, "ADCR Mux"},
  513. {"VAIFTX", NULL, "ADCR Mux"},
  514. /* Digital I2S out */
  515. {"I2SOut Mux", "ADC LR", "ADCLR"},
  516. {"I2SOut Mux", "Voice Stereo Digital", "VAIFRX"},
  517. {"AIFTXL", NULL, "I2SOut Mux"},
  518. {"AIFTXR", NULL, "I2SOut Mux"},
  519. /* Voice Mix */
  520. {"Voice DAC", NULL, "VAIFRX"},
  521. {"Voice Mix", NULL, "Voice DAC"},
  522. /* Speaker Output */
  523. {"SpeakerOut N Mux", "RN/-R", "Left Speaker"},
  524. {"SpeakerOut N Mux", "RP/+R", "Left Speaker"},
  525. {"SpeakerOut N Mux", "LN/-R", "Left Speaker"},
  526. {"SpeakerOut N Mux", "Mute", "Vmid"},
  527. {"SpeakerOut N Mux", "RN/-R", "Right Speaker"},
  528. {"SpeakerOut N Mux", "RP/+R", "Right Speaker"},
  529. {"SpeakerOut N Mux", "LN/-R", "Right Speaker"},
  530. {"SpeakerOut N Mux", "Mute", "Vmid"},
  531. {"AB Amp", NULL, "SpeakerOut Mux"},
  532. {"D Amp", NULL, "SpeakerOut Mux"},
  533. {"AB-D Amp Mux", "AB Amp", "AB Amp"},
  534. {"AB-D Amp Mux", "D Amp", "D Amp"},
  535. {"Left Speaker", NULL, "AB-D Amp Mux"},
  536. {"Right Speaker", NULL, "AB-D Amp Mux"},
  537. {"SPKOUT", NULL, "Left Speaker"},
  538. {"SPKOUT", NULL, "Right Speaker"},
  539. {"SPKOUTN", NULL, "SpeakerOut N Mux"},
  540. };
  541. /* PLL divisors */
  542. struct _pll_div {
  543. u32 pll_in;
  544. u32 pll_out;
  545. u16 regvalue;
  546. };
  547. /* Note : pll code from original alc5632 driver. Not sure of how good it is */
  548. /* usefull only for master mode */
  549. static const struct _pll_div codec_master_pll_div[] = {
  550. { 2048000, 8192000, 0x0ea0},
  551. { 3686400, 8192000, 0x4e27},
  552. { 12000000, 8192000, 0x456b},
  553. { 13000000, 8192000, 0x495f},
  554. { 13100000, 8192000, 0x0320},
  555. { 2048000, 11289600, 0xf637},
  556. { 3686400, 11289600, 0x2f22},
  557. { 12000000, 11289600, 0x3e2f},
  558. { 13000000, 11289600, 0x4d5b},
  559. { 13100000, 11289600, 0x363b},
  560. { 2048000, 16384000, 0x1ea0},
  561. { 3686400, 16384000, 0x9e27},
  562. { 12000000, 16384000, 0x452b},
  563. { 13000000, 16384000, 0x542f},
  564. { 13100000, 16384000, 0x03a0},
  565. { 2048000, 16934400, 0xe625},
  566. { 3686400, 16934400, 0x9126},
  567. { 12000000, 16934400, 0x4d2c},
  568. { 13000000, 16934400, 0x742f},
  569. { 13100000, 16934400, 0x3c27},
  570. { 2048000, 22579200, 0x2aa0},
  571. { 3686400, 22579200, 0x2f20},
  572. { 12000000, 22579200, 0x7e2f},
  573. { 13000000, 22579200, 0x742f},
  574. { 13100000, 22579200, 0x3c27},
  575. { 2048000, 24576000, 0x2ea0},
  576. { 3686400, 24576000, 0xee27},
  577. { 12000000, 24576000, 0x2915},
  578. { 13000000, 24576000, 0x772e},
  579. { 13100000, 24576000, 0x0d20},
  580. };
  581. /* FOUT = MCLK*(N+2)/((M+2)*(K+2))
  582. N: bit 15:8 (div 2 .. div 257)
  583. K: bit 6:4 typical 2
  584. M: bit 3:0 (div 2 .. div 17)
  585. same as for 5623 - thanks!
  586. */
  587. static const struct _pll_div codec_slave_pll_div[] = {
  588. { 1024000, 16384000, 0x3ea0},
  589. { 1411200, 22579200, 0x3ea0},
  590. { 1536000, 24576000, 0x3ea0},
  591. { 2048000, 16384000, 0x1ea0},
  592. { 2822400, 22579200, 0x1ea0},
  593. { 3072000, 24576000, 0x1ea0},
  594. };
  595. static int alc5632_set_dai_pll(struct snd_soc_dai *codec_dai, int pll_id,
  596. int source, unsigned int freq_in, unsigned int freq_out)
  597. {
  598. int i;
  599. struct snd_soc_codec *codec = codec_dai->codec;
  600. int gbl_clk = 0, pll_div = 0;
  601. u16 reg;
  602. if (pll_id < ALC5632_PLL_FR_MCLK || pll_id > ALC5632_PLL_FR_VBCLK)
  603. return -EINVAL;
  604. /* Disable PLL power */
  605. snd_soc_update_bits(codec, ALC5632_PWR_MANAG_ADD2,
  606. ALC5632_PWR_ADD2_PLL1,
  607. 0);
  608. snd_soc_update_bits(codec, ALC5632_PWR_MANAG_ADD2,
  609. ALC5632_PWR_ADD2_PLL2,
  610. 0);
  611. /* pll is not used in slave mode */
  612. reg = snd_soc_read(codec, ALC5632_DAI_CONTROL);
  613. if (reg & ALC5632_DAI_SDP_SLAVE_MODE)
  614. return 0;
  615. if (!freq_in || !freq_out)
  616. return 0;
  617. switch (pll_id) {
  618. case ALC5632_PLL_FR_MCLK:
  619. for (i = 0; i < ARRAY_SIZE(codec_master_pll_div); i++) {
  620. if (codec_master_pll_div[i].pll_in == freq_in
  621. && codec_master_pll_div[i].pll_out == freq_out) {
  622. /* PLL source from MCLK */
  623. pll_div = codec_master_pll_div[i].regvalue;
  624. break;
  625. }
  626. }
  627. break;
  628. case ALC5632_PLL_FR_BCLK:
  629. for (i = 0; i < ARRAY_SIZE(codec_slave_pll_div); i++) {
  630. if (codec_slave_pll_div[i].pll_in == freq_in
  631. && codec_slave_pll_div[i].pll_out == freq_out) {
  632. /* PLL source from Bitclk */
  633. gbl_clk = ALC5632_PLL_FR_BCLK;
  634. pll_div = codec_slave_pll_div[i].regvalue;
  635. break;
  636. }
  637. }
  638. break;
  639. case ALC5632_PLL_FR_VBCLK:
  640. for (i = 0; i < ARRAY_SIZE(codec_slave_pll_div); i++) {
  641. if (codec_slave_pll_div[i].pll_in == freq_in
  642. && codec_slave_pll_div[i].pll_out == freq_out) {
  643. /* PLL source from voice clock */
  644. gbl_clk = ALC5632_PLL_FR_VBCLK;
  645. pll_div = codec_slave_pll_div[i].regvalue;
  646. break;
  647. }
  648. }
  649. break;
  650. default:
  651. return -EINVAL;
  652. }
  653. if (!pll_div)
  654. return -EINVAL;
  655. /* choose MCLK/BCLK/VBCLK */
  656. snd_soc_write(codec, ALC5632_GPCR2, gbl_clk);
  657. /* choose PLL1 clock rate */
  658. snd_soc_write(codec, ALC5632_PLL1_CTRL, pll_div);
  659. /* enable PLL1 */
  660. snd_soc_update_bits(codec, ALC5632_PWR_MANAG_ADD2,
  661. ALC5632_PWR_ADD2_PLL1,
  662. ALC5632_PWR_ADD2_PLL1);
  663. /* enable PLL2 */
  664. snd_soc_update_bits(codec, ALC5632_PWR_MANAG_ADD2,
  665. ALC5632_PWR_ADD2_PLL2,
  666. ALC5632_PWR_ADD2_PLL2);
  667. /* use PLL1 as main SYSCLK */
  668. snd_soc_update_bits(codec, ALC5632_GPCR1,
  669. ALC5632_GPCR1_CLK_SYS_SRC_SEL_PLL1,
  670. ALC5632_GPCR1_CLK_SYS_SRC_SEL_PLL1);
  671. return 0;
  672. }
  673. struct _coeff_div {
  674. u16 fs;
  675. u16 regvalue;
  676. };
  677. /* codec hifi mclk (after PLL) clock divider coefficients */
  678. /* values inspired from column BCLK=32Fs of Appendix A table */
  679. static const struct _coeff_div coeff_div[] = {
  680. {512*1, 0x3075},
  681. };
  682. static int get_coeff(struct snd_soc_codec *codec, int rate)
  683. {
  684. struct alc5632_priv *alc5632 = snd_soc_codec_get_drvdata(codec);
  685. int i;
  686. for (i = 0; i < ARRAY_SIZE(coeff_div); i++) {
  687. if (coeff_div[i].fs * rate == alc5632->sysclk)
  688. return i;
  689. }
  690. return -EINVAL;
  691. }
  692. /*
  693. * Clock after PLL and dividers
  694. */
  695. static int alc5632_set_dai_sysclk(struct snd_soc_dai *codec_dai,
  696. int clk_id, unsigned int freq, int dir)
  697. {
  698. struct snd_soc_codec *codec = codec_dai->codec;
  699. struct alc5632_priv *alc5632 = snd_soc_codec_get_drvdata(codec);
  700. switch (freq) {
  701. case 4096000:
  702. case 8192000:
  703. case 11289600:
  704. case 12288000:
  705. case 16384000:
  706. case 16934400:
  707. case 18432000:
  708. case 22579200:
  709. case 24576000:
  710. alc5632->sysclk = freq;
  711. return 0;
  712. }
  713. return -EINVAL;
  714. }
  715. static int alc5632_set_dai_fmt(struct snd_soc_dai *codec_dai,
  716. unsigned int fmt)
  717. {
  718. struct snd_soc_codec *codec = codec_dai->codec;
  719. u16 iface = 0;
  720. /* set master/slave audio interface */
  721. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  722. case SND_SOC_DAIFMT_CBM_CFM:
  723. iface = ALC5632_DAI_SDP_MASTER_MODE;
  724. break;
  725. case SND_SOC_DAIFMT_CBS_CFS:
  726. iface = ALC5632_DAI_SDP_SLAVE_MODE;
  727. break;
  728. default:
  729. return -EINVAL;
  730. }
  731. /* interface format */
  732. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  733. case SND_SOC_DAIFMT_I2S:
  734. iface |= ALC5632_DAI_I2S_DF_I2S;
  735. break;
  736. case SND_SOC_DAIFMT_LEFT_J:
  737. iface |= ALC5632_DAI_I2S_DF_LEFT;
  738. break;
  739. case SND_SOC_DAIFMT_DSP_A:
  740. iface |= ALC5632_DAI_I2S_DF_PCM_A;
  741. break;
  742. case SND_SOC_DAIFMT_DSP_B:
  743. iface |= ALC5632_DAI_I2S_DF_PCM_B;
  744. break;
  745. default:
  746. return -EINVAL;
  747. }
  748. /* clock inversion */
  749. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  750. case SND_SOC_DAIFMT_NB_NF:
  751. break;
  752. case SND_SOC_DAIFMT_IB_IF:
  753. iface |= ALC5632_DAI_MAIN_I2S_BCLK_POL_CTRL;
  754. break;
  755. case SND_SOC_DAIFMT_IB_NF:
  756. iface |= ALC5632_DAI_MAIN_I2S_BCLK_POL_CTRL;
  757. break;
  758. case SND_SOC_DAIFMT_NB_IF:
  759. break;
  760. default:
  761. return -EINVAL;
  762. }
  763. return snd_soc_write(codec, ALC5632_DAI_CONTROL, iface);
  764. }
  765. static int alc5632_pcm_hw_params(struct snd_pcm_substream *substream,
  766. struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
  767. {
  768. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  769. struct snd_soc_codec *codec = rtd->codec;
  770. int coeff, rate;
  771. u16 iface;
  772. iface = snd_soc_read(codec, ALC5632_DAI_CONTROL);
  773. iface &= ~ALC5632_DAI_I2S_DL_MASK;
  774. /* bit size */
  775. switch (params_format(params)) {
  776. case SNDRV_PCM_FORMAT_S16_LE:
  777. iface |= ALC5632_DAI_I2S_DL_16;
  778. break;
  779. case SNDRV_PCM_FORMAT_S20_3LE:
  780. iface |= ALC5632_DAI_I2S_DL_20;
  781. break;
  782. case SNDRV_PCM_FORMAT_S24_LE:
  783. iface |= ALC5632_DAI_I2S_DL_24;
  784. break;
  785. default:
  786. return -EINVAL;
  787. }
  788. /* set iface & srate */
  789. snd_soc_write(codec, ALC5632_DAI_CONTROL, iface);
  790. rate = params_rate(params);
  791. coeff = get_coeff(codec, rate);
  792. if (coeff < 0)
  793. return -EINVAL;
  794. coeff = coeff_div[coeff].regvalue;
  795. snd_soc_write(codec, ALC5632_DAC_CLK_CTRL1, coeff);
  796. return 0;
  797. }
  798. static int alc5632_mute(struct snd_soc_dai *dai, int mute)
  799. {
  800. struct snd_soc_codec *codec = dai->codec;
  801. u16 hp_mute = ALC5632_MISC_HP_DEPOP_MUTE_L
  802. |ALC5632_MISC_HP_DEPOP_MUTE_R;
  803. u16 mute_reg = snd_soc_read(codec, ALC5632_MISC_CTRL) & ~hp_mute;
  804. if (mute)
  805. mute_reg |= hp_mute;
  806. return snd_soc_write(codec, ALC5632_MISC_CTRL, mute_reg);
  807. }
  808. #define ALC5632_ADD2_POWER_EN (ALC5632_PWR_ADD2_VREF)
  809. #define ALC5632_ADD3_POWER_EN (ALC5632_PWR_ADD3_MIC1_BOOST_AD)
  810. #define ALC5632_ADD1_POWER_EN \
  811. (ALC5632_PWR_ADD1_DAC_REF \
  812. | ALC5632_PWR_ADD1_SOFTGEN_EN \
  813. | ALC5632_PWR_ADD1_HP_OUT_AMP \
  814. | ALC5632_PWR_ADD1_HP_OUT_ENH_AMP \
  815. | ALC5632_PWR_ADD1_MAIN_BIAS)
  816. static void enable_power_depop(struct snd_soc_codec *codec)
  817. {
  818. snd_soc_update_bits(codec, ALC5632_PWR_MANAG_ADD1,
  819. ALC5632_PWR_ADD1_SOFTGEN_EN,
  820. ALC5632_PWR_ADD1_SOFTGEN_EN);
  821. snd_soc_update_bits(codec, ALC5632_PWR_MANAG_ADD3,
  822. ALC5632_ADD3_POWER_EN,
  823. ALC5632_ADD3_POWER_EN);
  824. snd_soc_update_bits(codec, ALC5632_MISC_CTRL,
  825. ALC5632_MISC_HP_DEPOP_MODE2_EN,
  826. ALC5632_MISC_HP_DEPOP_MODE2_EN);
  827. /* "normal" mode: 0 @ 26 */
  828. /* set all PR0-7 mixers to 0 */
  829. snd_soc_update_bits(codec, ALC5632_PWR_DOWN_CTRL_STATUS,
  830. ALC5632_PWR_DOWN_CTRL_STATUS_MASK,
  831. 0);
  832. msleep(500);
  833. snd_soc_update_bits(codec, ALC5632_PWR_MANAG_ADD2,
  834. ALC5632_ADD2_POWER_EN,
  835. ALC5632_ADD2_POWER_EN);
  836. snd_soc_update_bits(codec, ALC5632_PWR_MANAG_ADD1,
  837. ALC5632_ADD1_POWER_EN,
  838. ALC5632_ADD1_POWER_EN);
  839. /* disable HP Depop2 */
  840. snd_soc_update_bits(codec, ALC5632_MISC_CTRL,
  841. ALC5632_MISC_HP_DEPOP_MODE2_EN,
  842. 0);
  843. }
  844. static int alc5632_set_bias_level(struct snd_soc_codec *codec,
  845. enum snd_soc_bias_level level)
  846. {
  847. switch (level) {
  848. case SND_SOC_BIAS_ON:
  849. enable_power_depop(codec);
  850. break;
  851. case SND_SOC_BIAS_PREPARE:
  852. break;
  853. case SND_SOC_BIAS_STANDBY:
  854. /* everything off except vref/vmid, */
  855. snd_soc_update_bits(codec, ALC5632_PWR_MANAG_ADD1,
  856. ALC5632_PWR_MANAG_ADD1_MASK,
  857. ALC5632_PWR_ADD1_MAIN_BIAS);
  858. snd_soc_update_bits(codec, ALC5632_PWR_MANAG_ADD2,
  859. ALC5632_PWR_MANAG_ADD2_MASK,
  860. ALC5632_PWR_ADD2_VREF);
  861. /* "normal" mode: 0 @ 26 */
  862. snd_soc_update_bits(codec, ALC5632_PWR_DOWN_CTRL_STATUS,
  863. ALC5632_PWR_DOWN_CTRL_STATUS_MASK,
  864. 0xffff ^ (ALC5632_PWR_VREF_PR3
  865. | ALC5632_PWR_VREF_PR2));
  866. break;
  867. case SND_SOC_BIAS_OFF:
  868. /* everything off, dac mute, inactive */
  869. snd_soc_update_bits(codec, ALC5632_PWR_MANAG_ADD2,
  870. ALC5632_PWR_MANAG_ADD2_MASK, 0);
  871. snd_soc_update_bits(codec, ALC5632_PWR_MANAG_ADD3,
  872. ALC5632_PWR_MANAG_ADD3_MASK, 0);
  873. snd_soc_update_bits(codec, ALC5632_PWR_MANAG_ADD1,
  874. ALC5632_PWR_MANAG_ADD1_MASK, 0);
  875. break;
  876. }
  877. codec->dapm.bias_level = level;
  878. return 0;
  879. }
  880. #define ALC5632_FORMATS (SNDRV_PCM_FMTBIT_S16_LE \
  881. | SNDRV_PCM_FMTBIT_S24_LE \
  882. | SNDRV_PCM_FMTBIT_S32_LE)
  883. static const struct snd_soc_dai_ops alc5632_dai_ops = {
  884. .hw_params = alc5632_pcm_hw_params,
  885. .digital_mute = alc5632_mute,
  886. .set_fmt = alc5632_set_dai_fmt,
  887. .set_sysclk = alc5632_set_dai_sysclk,
  888. .set_pll = alc5632_set_dai_pll,
  889. };
  890. static struct snd_soc_dai_driver alc5632_dai = {
  891. .name = "alc5632-hifi",
  892. .playback = {
  893. .stream_name = "HiFi Playback",
  894. .channels_min = 1,
  895. .channels_max = 2,
  896. .rate_min = 8000,
  897. .rate_max = 48000,
  898. .rates = SNDRV_PCM_RATE_8000_48000,
  899. .formats = ALC5632_FORMATS,},
  900. .capture = {
  901. .stream_name = "HiFi Capture",
  902. .channels_min = 1,
  903. .channels_max = 2,
  904. .rate_min = 8000,
  905. .rate_max = 48000,
  906. .rates = SNDRV_PCM_RATE_8000_48000,
  907. .formats = ALC5632_FORMATS,},
  908. .ops = &alc5632_dai_ops,
  909. .symmetric_rates = 1,
  910. };
  911. #ifdef CONFIG_PM
  912. static int alc5632_suspend(struct snd_soc_codec *codec)
  913. {
  914. alc5632_set_bias_level(codec, SND_SOC_BIAS_OFF);
  915. return 0;
  916. }
  917. static int alc5632_resume(struct snd_soc_codec *codec)
  918. {
  919. struct alc5632_priv *alc5632 = snd_soc_codec_get_drvdata(codec);
  920. regcache_sync(alc5632->regmap);
  921. alc5632_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  922. return 0;
  923. }
  924. #else
  925. #define alc5632_suspend NULL
  926. #define alc5632_resume NULL
  927. #endif
  928. static int alc5632_probe(struct snd_soc_codec *codec)
  929. {
  930. struct alc5632_priv *alc5632 = snd_soc_codec_get_drvdata(codec);
  931. int ret;
  932. codec->control_data = alc5632->regmap;
  933. ret = snd_soc_codec_set_cache_io(codec, 8, 16, SND_SOC_REGMAP);
  934. if (ret != 0) {
  935. dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
  936. return ret;
  937. }
  938. /* power on device */
  939. alc5632_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  940. switch (alc5632->id) {
  941. case 0x5c:
  942. snd_soc_add_codec_controls(codec, alc5632_vol_snd_controls,
  943. ARRAY_SIZE(alc5632_vol_snd_controls));
  944. break;
  945. default:
  946. return -EINVAL;
  947. }
  948. return ret;
  949. }
  950. /* power down chip */
  951. static int alc5632_remove(struct snd_soc_codec *codec)
  952. {
  953. alc5632_set_bias_level(codec, SND_SOC_BIAS_OFF);
  954. return 0;
  955. }
  956. static struct snd_soc_codec_driver soc_codec_device_alc5632 = {
  957. .probe = alc5632_probe,
  958. .remove = alc5632_remove,
  959. .suspend = alc5632_suspend,
  960. .resume = alc5632_resume,
  961. .set_bias_level = alc5632_set_bias_level,
  962. .controls = alc5632_snd_controls,
  963. .num_controls = ARRAY_SIZE(alc5632_snd_controls),
  964. .dapm_widgets = alc5632_dapm_widgets,
  965. .num_dapm_widgets = ARRAY_SIZE(alc5632_dapm_widgets),
  966. .dapm_routes = alc5632_dapm_routes,
  967. .num_dapm_routes = ARRAY_SIZE(alc5632_dapm_routes),
  968. };
  969. static struct regmap_config alc5632_regmap = {
  970. .reg_bits = 8,
  971. .val_bits = 16,
  972. .max_register = ALC5632_MAX_REGISTER,
  973. .reg_defaults = alc5632_reg_defaults,
  974. .num_reg_defaults = ARRAY_SIZE(alc5632_reg_defaults),
  975. .volatile_reg = alc5632_volatile_register,
  976. .cache_type = REGCACHE_RBTREE,
  977. };
  978. /*
  979. * alc5632 2 wire address is determined by A1 pin
  980. * state during powerup.
  981. * low = 0x1a
  982. * high = 0x1b
  983. */
  984. static __devinit int alc5632_i2c_probe(struct i2c_client *client,
  985. const struct i2c_device_id *id)
  986. {
  987. struct alc5632_priv *alc5632;
  988. int ret, ret1, ret2;
  989. unsigned int vid1, vid2;
  990. alc5632 = devm_kzalloc(&client->dev,
  991. sizeof(struct alc5632_priv), GFP_KERNEL);
  992. if (alc5632 == NULL)
  993. return -ENOMEM;
  994. i2c_set_clientdata(client, alc5632);
  995. alc5632->regmap = regmap_init_i2c(client, &alc5632_regmap);
  996. if (IS_ERR(alc5632->regmap)) {
  997. ret = PTR_ERR(alc5632->regmap);
  998. dev_err(&client->dev, "regmap_init() failed: %d\n", ret);
  999. return ret;
  1000. }
  1001. ret1 = regmap_read(alc5632->regmap, ALC5632_VENDOR_ID1, &vid1);
  1002. ret2 = regmap_read(alc5632->regmap, ALC5632_VENDOR_ID2, &vid2);
  1003. if (ret1 != 0 || ret2 != 0) {
  1004. dev_err(&client->dev,
  1005. "Failed to read chip ID: ret1=%d, ret2=%d\n", ret1, ret2);
  1006. regmap_exit(alc5632->regmap);
  1007. return -EIO;
  1008. }
  1009. vid2 >>= 8;
  1010. if ((vid1 != 0x10EC) || (vid2 != id->driver_data)) {
  1011. dev_err(&client->dev,
  1012. "Device is not a ALC5632: VID1=0x%x, VID2=0x%x\n", vid1, vid2);
  1013. regmap_exit(alc5632->regmap);
  1014. return -EINVAL;
  1015. }
  1016. ret = alc5632_reset(alc5632->regmap);
  1017. if (ret < 0) {
  1018. dev_err(&client->dev, "Failed to issue reset\n");
  1019. regmap_exit(alc5632->regmap);
  1020. return ret;
  1021. }
  1022. alc5632->id = vid2;
  1023. switch (alc5632->id) {
  1024. case 0x5c:
  1025. alc5632_dai.name = "alc5632-hifi";
  1026. break;
  1027. default:
  1028. return -EINVAL;
  1029. }
  1030. ret = snd_soc_register_codec(&client->dev,
  1031. &soc_codec_device_alc5632, &alc5632_dai, 1);
  1032. if (ret < 0) {
  1033. dev_err(&client->dev, "Failed to register codec: %d\n", ret);
  1034. regmap_exit(alc5632->regmap);
  1035. return ret;
  1036. }
  1037. return ret;
  1038. }
  1039. static __devexit int alc5632_i2c_remove(struct i2c_client *client)
  1040. {
  1041. struct alc5632_priv *alc5632 = i2c_get_clientdata(client);
  1042. snd_soc_unregister_codec(&client->dev);
  1043. regmap_exit(alc5632->regmap);
  1044. return 0;
  1045. }
  1046. static const struct i2c_device_id alc5632_i2c_table[] = {
  1047. {"alc5632", 0x5c},
  1048. {}
  1049. };
  1050. MODULE_DEVICE_TABLE(i2c, alc5632_i2c_table);
  1051. /* i2c codec control layer */
  1052. static struct i2c_driver alc5632_i2c_driver = {
  1053. .driver = {
  1054. .name = "alc5632",
  1055. .owner = THIS_MODULE,
  1056. },
  1057. .probe = alc5632_i2c_probe,
  1058. .remove = __devexit_p(alc5632_i2c_remove),
  1059. .id_table = alc5632_i2c_table,
  1060. };
  1061. static int __init alc5632_modinit(void)
  1062. {
  1063. int ret;
  1064. ret = i2c_add_driver(&alc5632_i2c_driver);
  1065. if (ret != 0) {
  1066. printk(KERN_ERR "%s: can't add i2c driver", __func__);
  1067. return ret;
  1068. }
  1069. return ret;
  1070. }
  1071. module_init(alc5632_modinit);
  1072. static void __exit alc5632_modexit(void)
  1073. {
  1074. i2c_del_driver(&alc5632_i2c_driver);
  1075. }
  1076. module_exit(alc5632_modexit);
  1077. MODULE_DESCRIPTION("ASoC ALC5632 driver");
  1078. MODULE_AUTHOR("Leon Romanovsky <leon@leon.nu>");
  1079. MODULE_LICENSE("GPL");