alc5632.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199
  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 const 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. struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
  111. /* to power-on/off class-d amp generators/speaker */
  112. /* need to write to 'index-46h' register : */
  113. /* so write index num (here 0x46) to reg 0x6a */
  114. /* and then 0xffff/0 to reg 0x6c */
  115. snd_soc_write(codec, ALC5632_HID_CTRL_INDEX, 0x46);
  116. switch (event) {
  117. case SND_SOC_DAPM_PRE_PMU:
  118. snd_soc_write(codec, ALC5632_HID_CTRL_DATA, 0xFFFF);
  119. break;
  120. case SND_SOC_DAPM_POST_PMD:
  121. snd_soc_write(codec, ALC5632_HID_CTRL_DATA, 0);
  122. break;
  123. }
  124. return 0;
  125. }
  126. /*
  127. * ALC5632 Controls
  128. */
  129. /* -34.5db min scale, 1.5db steps, no mute */
  130. static const DECLARE_TLV_DB_SCALE(vol_tlv, -3450, 150, 0);
  131. /* -46.5db min scale, 1.5db steps, no mute */
  132. static const DECLARE_TLV_DB_SCALE(hp_tlv, -4650, 150, 0);
  133. /* -16.5db min scale, 1.5db steps, no mute */
  134. static const DECLARE_TLV_DB_SCALE(adc_rec_tlv, -1650, 150, 0);
  135. static const DECLARE_TLV_DB_RANGE(boost_tlv,
  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 SOC_ENUM_SINGLE_DECL(alc5632_aux_out_input_enum,
  272. ALC5632_OUTPUT_MIXER_CTRL, 6,
  273. alc5632_aux_out_input_sel);
  274. static const struct snd_kcontrol_new alc5632_auxout_mux_controls =
  275. SOC_DAPM_ENUM("AuxOut Mux", alc5632_aux_out_input_enum);
  276. /* speaker output mux */
  277. static SOC_ENUM_SINGLE_DECL(alc5632_spkout_input_enum,
  278. ALC5632_OUTPUT_MIXER_CTRL, 10,
  279. alc5632_spkout_input_sel);
  280. static const struct snd_kcontrol_new alc5632_spkout_mux_controls =
  281. SOC_DAPM_ENUM("SpeakerOut Mux", alc5632_spkout_input_enum);
  282. /* headphone left output mux */
  283. static SOC_ENUM_SINGLE_DECL(alc5632_hpl_out_input_enum,
  284. ALC5632_OUTPUT_MIXER_CTRL, 9,
  285. alc5632_hpl_out_input_sel);
  286. static const struct snd_kcontrol_new alc5632_hpl_out_mux_controls =
  287. SOC_DAPM_ENUM("Left Headphone Mux", alc5632_hpl_out_input_enum);
  288. /* headphone right output mux */
  289. static SOC_ENUM_SINGLE_DECL(alc5632_hpr_out_input_enum,
  290. ALC5632_OUTPUT_MIXER_CTRL, 8,
  291. alc5632_hpr_out_input_sel);
  292. static const struct snd_kcontrol_new alc5632_hpr_out_mux_controls =
  293. SOC_DAPM_ENUM("Right Headphone Mux", alc5632_hpr_out_input_enum);
  294. /* speaker output N select */
  295. static SOC_ENUM_SINGLE_DECL(alc5632_spk_n_sour_enum,
  296. ALC5632_OUTPUT_MIXER_CTRL, 14,
  297. alc5632_spk_n_sour_sel);
  298. static const struct snd_kcontrol_new alc5632_spkoutn_mux_controls =
  299. SOC_DAPM_ENUM("SpeakerOut N Mux", alc5632_spk_n_sour_enum);
  300. /* speaker amplifier */
  301. static const char *alc5632_amp_names[] = {"AB Amp", "D Amp"};
  302. static SOC_ENUM_SINGLE_DECL(alc5632_amp_enum,
  303. ALC5632_OUTPUT_MIXER_CTRL, 13,
  304. alc5632_amp_names);
  305. static const struct snd_kcontrol_new alc5632_amp_mux_controls =
  306. SOC_DAPM_ENUM("AB-D Amp Mux", alc5632_amp_enum);
  307. /* ADC output select */
  308. static SOC_ENUM_SINGLE_DECL(alc5632_adcr_func_enum,
  309. ALC5632_DAC_FUNC_SELECT, 5,
  310. alc5632_adcr_func_sel);
  311. static const struct snd_kcontrol_new alc5632_adcr_func_controls =
  312. SOC_DAPM_ENUM("ADCR Mux", alc5632_adcr_func_enum);
  313. /* I2S out select */
  314. static SOC_ENUM_SINGLE_DECL(alc5632_i2s_out_enum,
  315. ALC5632_I2S_OUT_CTL, 5,
  316. alc5632_i2s_out_sel);
  317. static const struct snd_kcontrol_new alc5632_i2s_out_controls =
  318. SOC_DAPM_ENUM("I2SOut Mux", alc5632_i2s_out_enum);
  319. static const struct snd_soc_dapm_widget alc5632_dapm_widgets[] = {
  320. /* Muxes */
  321. SND_SOC_DAPM_MUX("AuxOut Mux", SND_SOC_NOPM, 0, 0,
  322. &alc5632_auxout_mux_controls),
  323. SND_SOC_DAPM_MUX("SpeakerOut Mux", SND_SOC_NOPM, 0, 0,
  324. &alc5632_spkout_mux_controls),
  325. SND_SOC_DAPM_MUX("Left Headphone Mux", SND_SOC_NOPM, 0, 0,
  326. &alc5632_hpl_out_mux_controls),
  327. SND_SOC_DAPM_MUX("Right Headphone Mux", SND_SOC_NOPM, 0, 0,
  328. &alc5632_hpr_out_mux_controls),
  329. SND_SOC_DAPM_MUX("SpeakerOut N Mux", SND_SOC_NOPM, 0, 0,
  330. &alc5632_spkoutn_mux_controls),
  331. SND_SOC_DAPM_MUX("ADCR Mux", SND_SOC_NOPM, 0, 0,
  332. &alc5632_adcr_func_controls),
  333. SND_SOC_DAPM_MUX("I2SOut Mux", ALC5632_PWR_MANAG_ADD1, 11, 0,
  334. &alc5632_i2s_out_controls),
  335. /* output mixers */
  336. SND_SOC_DAPM_MIXER("HP Mix", SND_SOC_NOPM, 0, 0,
  337. &alc5632_hp_mixer_controls[0],
  338. ARRAY_SIZE(alc5632_hp_mixer_controls)),
  339. SND_SOC_DAPM_MIXER("HPR Mix", ALC5632_PWR_MANAG_ADD2, 4, 0,
  340. &alc5632_hpr_mixer_controls[0],
  341. ARRAY_SIZE(alc5632_hpr_mixer_controls)),
  342. SND_SOC_DAPM_MIXER("HPL Mix", ALC5632_PWR_MANAG_ADD2, 5, 0,
  343. &alc5632_hpl_mixer_controls[0],
  344. ARRAY_SIZE(alc5632_hpl_mixer_controls)),
  345. SND_SOC_DAPM_MIXER("HPOut Mix", SND_SOC_NOPM, 0, 0, NULL, 0),
  346. SND_SOC_DAPM_MIXER("Mono Mix", ALC5632_PWR_MANAG_ADD2, 2, 0,
  347. &alc5632_mono_mixer_controls[0],
  348. ARRAY_SIZE(alc5632_mono_mixer_controls)),
  349. SND_SOC_DAPM_MIXER("Speaker Mix", ALC5632_PWR_MANAG_ADD2, 3, 0,
  350. &alc5632_speaker_mixer_controls[0],
  351. ARRAY_SIZE(alc5632_speaker_mixer_controls)),
  352. SND_SOC_DAPM_MIXER("DMICL Mix", SND_SOC_NOPM, 0, 0,
  353. &alc5632_dmicl_mixer_controls[0],
  354. ARRAY_SIZE(alc5632_dmicl_mixer_controls)),
  355. SND_SOC_DAPM_MIXER("DMICR Mix", SND_SOC_NOPM, 0, 0,
  356. &alc5632_dmicr_mixer_controls[0],
  357. ARRAY_SIZE(alc5632_dmicr_mixer_controls)),
  358. /* input mixers */
  359. SND_SOC_DAPM_MIXER("Left Capture Mix", ALC5632_PWR_MANAG_ADD2, 1, 0,
  360. &alc5632_captureL_mixer_controls[0],
  361. ARRAY_SIZE(alc5632_captureL_mixer_controls)),
  362. SND_SOC_DAPM_MIXER("Right Capture Mix", ALC5632_PWR_MANAG_ADD2, 0, 0,
  363. &alc5632_captureR_mixer_controls[0],
  364. ARRAY_SIZE(alc5632_captureR_mixer_controls)),
  365. SND_SOC_DAPM_AIF_IN("AIFRXL", "Left HiFi Playback", 0, SND_SOC_NOPM, 0, 0),
  366. SND_SOC_DAPM_AIF_IN("AIFRXR", "Right HiFi Playback", 0, SND_SOC_NOPM, 0, 0),
  367. SND_SOC_DAPM_AIF_OUT("AIFTXL", "Left HiFi Capture", 0, SND_SOC_NOPM, 0, 0),
  368. SND_SOC_DAPM_AIF_OUT("AIFTXR", "Right HiFi Capture", 0, SND_SOC_NOPM, 0, 0),
  369. SND_SOC_DAPM_AIF_IN("VAIFRX", "Voice Playback", 0, SND_SOC_NOPM, 0, 0),
  370. SND_SOC_DAPM_AIF_OUT("VAIFTX", "Voice Capture", 0, SND_SOC_NOPM, 0, 0),
  371. SND_SOC_DAPM_DAC("Voice DAC", NULL, ALC5632_PWR_MANAG_ADD2, 10, 0),
  372. SND_SOC_DAPM_DAC("Left DAC", NULL, ALC5632_PWR_MANAG_ADD2, 9, 0),
  373. SND_SOC_DAPM_DAC("Right DAC", NULL, ALC5632_PWR_MANAG_ADD2, 8, 0),
  374. SND_SOC_DAPM_ADC("Left ADC", NULL, ALC5632_PWR_MANAG_ADD2, 7, 0),
  375. SND_SOC_DAPM_ADC("Right ADC", NULL, ALC5632_PWR_MANAG_ADD2, 6, 0),
  376. SND_SOC_DAPM_MIXER("DAC Left Channel", ALC5632_PWR_MANAG_ADD1, 15, 0, NULL, 0),
  377. SND_SOC_DAPM_MIXER("DAC Right Channel",
  378. ALC5632_PWR_MANAG_ADD1, 14, 0, NULL, 0),
  379. SND_SOC_DAPM_MIXER("I2S Mix", ALC5632_PWR_MANAG_ADD1, 11, 0, NULL, 0),
  380. SND_SOC_DAPM_MIXER("Phone Mix", SND_SOC_NOPM, 0, 0, NULL, 0),
  381. SND_SOC_DAPM_MIXER("Line Mix", SND_SOC_NOPM, 0, 0, NULL, 0),
  382. SND_SOC_DAPM_MIXER("Voice Mix", SND_SOC_NOPM, 0, 0, NULL, 0),
  383. SND_SOC_DAPM_MIXER("ADCLR", SND_SOC_NOPM, 0, 0, NULL, 0),
  384. SND_SOC_DAPM_PGA("Left Headphone", ALC5632_PWR_MANAG_ADD3, 11, 0, NULL, 0),
  385. SND_SOC_DAPM_PGA("Right Headphone", ALC5632_PWR_MANAG_ADD3, 10, 0, NULL, 0),
  386. SND_SOC_DAPM_PGA("Left Speaker", ALC5632_PWR_MANAG_ADD3, 13, 0, NULL, 0),
  387. SND_SOC_DAPM_PGA("Right Speaker", ALC5632_PWR_MANAG_ADD3, 12, 0, NULL, 0),
  388. SND_SOC_DAPM_PGA("Aux Out", ALC5632_PWR_MANAG_ADD3, 14, 0, NULL, 0),
  389. SND_SOC_DAPM_PGA("Left LineIn", ALC5632_PWR_MANAG_ADD3, 7, 0, NULL, 0),
  390. SND_SOC_DAPM_PGA("Right LineIn", ALC5632_PWR_MANAG_ADD3, 6, 0, NULL, 0),
  391. SND_SOC_DAPM_PGA("Phone", ALC5632_PWR_MANAG_ADD3, 5, 0, NULL, 0),
  392. SND_SOC_DAPM_PGA("Phone ADMix", ALC5632_PWR_MANAG_ADD3, 4, 0, NULL, 0),
  393. SND_SOC_DAPM_PGA("MIC1 PGA", ALC5632_PWR_MANAG_ADD3, 3, 0, NULL, 0),
  394. SND_SOC_DAPM_PGA("MIC2 PGA", ALC5632_PWR_MANAG_ADD3, 2, 0, NULL, 0),
  395. SND_SOC_DAPM_PGA("MIC1 Pre Amp", ALC5632_PWR_MANAG_ADD3, 1, 0, NULL, 0),
  396. SND_SOC_DAPM_PGA("MIC2 Pre Amp", ALC5632_PWR_MANAG_ADD3, 0, 0, NULL, 0),
  397. SND_SOC_DAPM_SUPPLY("MICBIAS1", ALC5632_PWR_MANAG_ADD1, 3, 0, NULL, 0),
  398. SND_SOC_DAPM_SUPPLY("MICBIAS2", ALC5632_PWR_MANAG_ADD1, 2, 0, NULL, 0),
  399. SND_SOC_DAPM_PGA_E("D Amp", ALC5632_PWR_MANAG_ADD2, 14, 0, NULL, 0,
  400. amp_mixer_event, SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  401. SND_SOC_DAPM_PGA("AB Amp", ALC5632_PWR_MANAG_ADD2, 15, 0, NULL, 0),
  402. SND_SOC_DAPM_MUX("AB-D Amp Mux", ALC5632_PWR_MANAG_ADD1, 10, 0,
  403. &alc5632_amp_mux_controls),
  404. SND_SOC_DAPM_OUTPUT("AUXOUT"),
  405. SND_SOC_DAPM_OUTPUT("HPL"),
  406. SND_SOC_DAPM_OUTPUT("HPR"),
  407. SND_SOC_DAPM_OUTPUT("SPKOUT"),
  408. SND_SOC_DAPM_OUTPUT("SPKOUTN"),
  409. SND_SOC_DAPM_INPUT("LINEINL"),
  410. SND_SOC_DAPM_INPUT("LINEINR"),
  411. SND_SOC_DAPM_INPUT("PHONEP"),
  412. SND_SOC_DAPM_INPUT("PHONEN"),
  413. SND_SOC_DAPM_INPUT("DMICDAT"),
  414. SND_SOC_DAPM_INPUT("MIC1"),
  415. SND_SOC_DAPM_INPUT("MIC2"),
  416. SND_SOC_DAPM_VMID("Vmid"),
  417. };
  418. static const struct snd_soc_dapm_route alc5632_dapm_routes[] = {
  419. /* Playback streams */
  420. {"Left DAC", NULL, "AIFRXL"},
  421. {"Right DAC", NULL, "AIFRXR"},
  422. /* virtual mixer - mixes left & right channels */
  423. {"I2S Mix", NULL, "Left DAC"},
  424. {"I2S Mix", NULL, "Right DAC"},
  425. {"Line Mix", NULL, "Right LineIn"},
  426. {"Line Mix", NULL, "Left LineIn"},
  427. {"Phone Mix", NULL, "Phone"},
  428. {"Phone Mix", NULL, "Phone ADMix"},
  429. {"AUXOUT", NULL, "Aux Out"},
  430. /* DAC */
  431. {"DAC Right Channel", NULL, "I2S Mix"},
  432. {"DAC Left Channel", NULL, "I2S Mix"},
  433. /* HP mixer */
  434. {"HPL Mix", "ADC2HP_L Playback Switch", "Left Capture Mix"},
  435. {"HPL Mix", NULL, "HP Mix"},
  436. {"HPR Mix", "ADC2HP_R Playback Switch", "Right Capture Mix"},
  437. {"HPR Mix", NULL, "HP Mix"},
  438. {"HP Mix", "LI2HP Playback Switch", "Line Mix"},
  439. {"HP Mix", "PHONE2HP Playback Switch", "Phone Mix"},
  440. {"HP Mix", "MIC12HP Playback Switch", "MIC1 PGA"},
  441. {"HP Mix", "MIC22HP Playback Switch", "MIC2 PGA"},
  442. {"HP Mix", "VOICE2HP Playback Switch", "Voice Mix"},
  443. {"HPR Mix", "DACR2HP Playback Switch", "DAC Right Channel"},
  444. {"HPL Mix", "DACL2HP Playback Switch", "DAC Left Channel"},
  445. {"HPOut Mix", NULL, "HP Mix"},
  446. {"HPOut Mix", NULL, "HPR Mix"},
  447. {"HPOut Mix", NULL, "HPL Mix"},
  448. /* speaker mixer */
  449. {"Speaker Mix", "LI2SPK Playback Switch", "Line Mix"},
  450. {"Speaker Mix", "PHONE2SPK Playback Switch", "Phone Mix"},
  451. {"Speaker Mix", "MIC12SPK Playback Switch", "MIC1 PGA"},
  452. {"Speaker Mix", "MIC22SPK Playback Switch", "MIC2 PGA"},
  453. {"Speaker Mix", "DAC2SPK Playback Switch", "DAC Left Channel"},
  454. {"Speaker Mix", "VOICE2SPK Playback Switch", "Voice Mix"},
  455. /* mono mixer */
  456. {"Mono Mix", "ADC2MONO_L Playback Switch", "Left Capture Mix"},
  457. {"Mono Mix", "ADC2MONO_R Playback Switch", "Right Capture Mix"},
  458. {"Mono Mix", "LI2MONO Playback Switch", "Line Mix"},
  459. {"Mono Mix", "MIC12MONO Playback Switch", "MIC1 PGA"},
  460. {"Mono Mix", "MIC22MONO Playback Switch", "MIC2 PGA"},
  461. {"Mono Mix", "DAC2MONO Playback Switch", "DAC Left Channel"},
  462. {"Mono Mix", "VOICE2MONO Playback Switch", "Voice Mix"},
  463. /* Left record mixer */
  464. {"Left Capture Mix", "LIL2REC Capture Switch", "LINEINL"},
  465. {"Left Capture Mix", "PH2REC_L Capture Switch", "PHONEN"},
  466. {"Left Capture Mix", "MIC12REC_L Capture Switch", "MIC1 Pre Amp"},
  467. {"Left Capture Mix", "MIC22REC_L Capture Switch", "MIC2 Pre Amp"},
  468. {"Left Capture Mix", "HPL2REC Capture Switch", "HPL Mix"},
  469. {"Left Capture Mix", "SPK2REC_L Capture Switch", "Speaker Mix"},
  470. {"Left Capture Mix", "MONO2REC_L Capture Switch", "Mono Mix"},
  471. /*Right record mixer */
  472. {"Right Capture Mix", "LIR2REC Capture Switch", "LINEINR"},
  473. {"Right Capture Mix", "PH2REC_R Capture Switch", "PHONEP"},
  474. {"Right Capture Mix", "MIC12REC_R Capture Switch", "MIC1 Pre Amp"},
  475. {"Right Capture Mix", "MIC22REC_R Capture Switch", "MIC2 Pre Amp"},
  476. {"Right Capture Mix", "HPR2REC Capture Switch", "HPR Mix"},
  477. {"Right Capture Mix", "SPK2REC_R Capture Switch", "Speaker Mix"},
  478. {"Right Capture Mix", "MONO2REC_R Capture Switch", "Mono Mix"},
  479. /* headphone left mux */
  480. {"Left Headphone Mux", "HP Left Mix", "HPL Mix"},
  481. {"Left Headphone Mux", "Vmid", "Vmid"},
  482. /* headphone right mux */
  483. {"Right Headphone Mux", "HP Right Mix", "HPR Mix"},
  484. {"Right Headphone Mux", "Vmid", "Vmid"},
  485. /* speaker out mux */
  486. {"SpeakerOut Mux", "Vmid", "Vmid"},
  487. {"SpeakerOut Mux", "HPOut Mix", "HPOut Mix"},
  488. {"SpeakerOut Mux", "Speaker Mix", "Speaker Mix"},
  489. {"SpeakerOut Mux", "Mono Mix", "Mono Mix"},
  490. /* Mono/Aux Out mux */
  491. {"AuxOut Mux", "Vmid", "Vmid"},
  492. {"AuxOut Mux", "HPOut Mix", "HPOut Mix"},
  493. {"AuxOut Mux", "Speaker Mix", "Speaker Mix"},
  494. {"AuxOut Mux", "Mono Mix", "Mono Mix"},
  495. /* output pga */
  496. {"HPL", NULL, "Left Headphone"},
  497. {"Left Headphone", NULL, "Left Headphone Mux"},
  498. {"HPR", NULL, "Right Headphone"},
  499. {"Right Headphone", NULL, "Right Headphone Mux"},
  500. {"Aux Out", NULL, "AuxOut Mux"},
  501. /* input pga */
  502. {"Left LineIn", NULL, "LINEINL"},
  503. {"Right LineIn", NULL, "LINEINR"},
  504. {"Phone", NULL, "PHONEP"},
  505. {"MIC1 Pre Amp", NULL, "MIC1"},
  506. {"MIC2 Pre Amp", NULL, "MIC2"},
  507. {"MIC1 PGA", NULL, "MIC1 Pre Amp"},
  508. {"MIC2 PGA", NULL, "MIC2 Pre Amp"},
  509. /* left ADC */
  510. {"Left ADC", NULL, "Left Capture Mix"},
  511. {"DMICL Mix", "DMICL2ADC Capture Switch", "DMICDAT"},
  512. {"Left ADC", NULL, "DMICL Mix"},
  513. {"ADCLR", NULL, "Left ADC"},
  514. /* right ADC */
  515. {"Right ADC", NULL, "Right Capture Mix"},
  516. {"DMICR Mix", "DMICR2ADC Capture Switch", "DMICDAT"},
  517. {"Right ADC", NULL, "DMICR Mix"},
  518. {"ADCR Mux", "Stereo ADC", "Right ADC"},
  519. {"ADCR Mux", "Voice ADC", "Right ADC"},
  520. {"ADCLR", NULL, "ADCR Mux"},
  521. {"VAIFTX", NULL, "ADCR Mux"},
  522. /* Digital I2S out */
  523. {"I2SOut Mux", "ADC LR", "ADCLR"},
  524. {"I2SOut Mux", "Voice Stereo Digital", "VAIFRX"},
  525. {"AIFTXL", NULL, "I2SOut Mux"},
  526. {"AIFTXR", NULL, "I2SOut Mux"},
  527. /* Voice Mix */
  528. {"Voice DAC", NULL, "VAIFRX"},
  529. {"Voice Mix", NULL, "Voice DAC"},
  530. /* Speaker Output */
  531. {"SpeakerOut N Mux", "RN/-R", "Left Speaker"},
  532. {"SpeakerOut N Mux", "RP/+R", "Left Speaker"},
  533. {"SpeakerOut N Mux", "LN/-R", "Left Speaker"},
  534. {"SpeakerOut N Mux", "Mute", "Vmid"},
  535. {"SpeakerOut N Mux", "RN/-R", "Right Speaker"},
  536. {"SpeakerOut N Mux", "RP/+R", "Right Speaker"},
  537. {"SpeakerOut N Mux", "LN/-R", "Right Speaker"},
  538. {"SpeakerOut N Mux", "Mute", "Vmid"},
  539. {"AB Amp", NULL, "SpeakerOut Mux"},
  540. {"D Amp", NULL, "SpeakerOut Mux"},
  541. {"AB-D Amp Mux", "AB Amp", "AB Amp"},
  542. {"AB-D Amp Mux", "D Amp", "D Amp"},
  543. {"Left Speaker", NULL, "AB-D Amp Mux"},
  544. {"Right Speaker", NULL, "AB-D Amp Mux"},
  545. {"SPKOUT", NULL, "Left Speaker"},
  546. {"SPKOUT", NULL, "Right Speaker"},
  547. {"SPKOUTN", NULL, "SpeakerOut N Mux"},
  548. };
  549. /* PLL divisors */
  550. struct _pll_div {
  551. u32 pll_in;
  552. u32 pll_out;
  553. u16 regvalue;
  554. };
  555. /* Note : pll code from original alc5632 driver. Not sure of how good it is */
  556. /* useful only for master mode */
  557. static const struct _pll_div codec_master_pll_div[] = {
  558. { 2048000, 8192000, 0x0ea0},
  559. { 3686400, 8192000, 0x4e27},
  560. { 12000000, 8192000, 0x456b},
  561. { 13000000, 8192000, 0x495f},
  562. { 13100000, 8192000, 0x0320},
  563. { 2048000, 11289600, 0xf637},
  564. { 3686400, 11289600, 0x2f22},
  565. { 12000000, 11289600, 0x3e2f},
  566. { 13000000, 11289600, 0x4d5b},
  567. { 13100000, 11289600, 0x363b},
  568. { 2048000, 16384000, 0x1ea0},
  569. { 3686400, 16384000, 0x9e27},
  570. { 12000000, 16384000, 0x452b},
  571. { 13000000, 16384000, 0x542f},
  572. { 13100000, 16384000, 0x03a0},
  573. { 2048000, 16934400, 0xe625},
  574. { 3686400, 16934400, 0x9126},
  575. { 12000000, 16934400, 0x4d2c},
  576. { 13000000, 16934400, 0x742f},
  577. { 13100000, 16934400, 0x3c27},
  578. { 2048000, 22579200, 0x2aa0},
  579. { 3686400, 22579200, 0x2f20},
  580. { 12000000, 22579200, 0x7e2f},
  581. { 13000000, 22579200, 0x742f},
  582. { 13100000, 22579200, 0x3c27},
  583. { 2048000, 24576000, 0x2ea0},
  584. { 3686400, 24576000, 0xee27},
  585. { 12000000, 24576000, 0x2915},
  586. { 13000000, 24576000, 0x772e},
  587. { 13100000, 24576000, 0x0d20},
  588. };
  589. /* FOUT = MCLK*(N+2)/((M+2)*(K+2))
  590. N: bit 15:8 (div 2 .. div 257)
  591. K: bit 6:4 typical 2
  592. M: bit 3:0 (div 2 .. div 17)
  593. same as for 5623 - thanks!
  594. */
  595. static const struct _pll_div codec_slave_pll_div[] = {
  596. { 1024000, 16384000, 0x3ea0},
  597. { 1411200, 22579200, 0x3ea0},
  598. { 1536000, 24576000, 0x3ea0},
  599. { 2048000, 16384000, 0x1ea0},
  600. { 2822400, 22579200, 0x1ea0},
  601. { 3072000, 24576000, 0x1ea0},
  602. };
  603. static int alc5632_set_dai_pll(struct snd_soc_dai *codec_dai, int pll_id,
  604. int source, unsigned int freq_in, unsigned int freq_out)
  605. {
  606. int i;
  607. struct snd_soc_codec *codec = codec_dai->codec;
  608. int gbl_clk = 0, pll_div = 0;
  609. u16 reg;
  610. if (pll_id < ALC5632_PLL_FR_MCLK || pll_id > ALC5632_PLL_FR_VBCLK)
  611. return -EINVAL;
  612. /* Disable PLL power */
  613. snd_soc_update_bits(codec, ALC5632_PWR_MANAG_ADD2,
  614. ALC5632_PWR_ADD2_PLL1,
  615. 0);
  616. snd_soc_update_bits(codec, ALC5632_PWR_MANAG_ADD2,
  617. ALC5632_PWR_ADD2_PLL2,
  618. 0);
  619. /* pll is not used in slave mode */
  620. reg = snd_soc_read(codec, ALC5632_DAI_CONTROL);
  621. if (reg & ALC5632_DAI_SDP_SLAVE_MODE)
  622. return 0;
  623. if (!freq_in || !freq_out)
  624. return 0;
  625. switch (pll_id) {
  626. case ALC5632_PLL_FR_MCLK:
  627. for (i = 0; i < ARRAY_SIZE(codec_master_pll_div); i++) {
  628. if (codec_master_pll_div[i].pll_in == freq_in
  629. && codec_master_pll_div[i].pll_out == freq_out) {
  630. /* PLL source from MCLK */
  631. pll_div = codec_master_pll_div[i].regvalue;
  632. break;
  633. }
  634. }
  635. break;
  636. case ALC5632_PLL_FR_BCLK:
  637. for (i = 0; i < ARRAY_SIZE(codec_slave_pll_div); i++) {
  638. if (codec_slave_pll_div[i].pll_in == freq_in
  639. && codec_slave_pll_div[i].pll_out == freq_out) {
  640. /* PLL source from Bitclk */
  641. gbl_clk = ALC5632_PLL_FR_BCLK;
  642. pll_div = codec_slave_pll_div[i].regvalue;
  643. break;
  644. }
  645. }
  646. break;
  647. case ALC5632_PLL_FR_VBCLK:
  648. for (i = 0; i < ARRAY_SIZE(codec_slave_pll_div); i++) {
  649. if (codec_slave_pll_div[i].pll_in == freq_in
  650. && codec_slave_pll_div[i].pll_out == freq_out) {
  651. /* PLL source from voice clock */
  652. gbl_clk = ALC5632_PLL_FR_VBCLK;
  653. pll_div = codec_slave_pll_div[i].regvalue;
  654. break;
  655. }
  656. }
  657. break;
  658. default:
  659. return -EINVAL;
  660. }
  661. if (!pll_div)
  662. return -EINVAL;
  663. /* choose MCLK/BCLK/VBCLK */
  664. snd_soc_write(codec, ALC5632_GPCR2, gbl_clk);
  665. /* choose PLL1 clock rate */
  666. snd_soc_write(codec, ALC5632_PLL1_CTRL, pll_div);
  667. /* enable PLL1 */
  668. snd_soc_update_bits(codec, ALC5632_PWR_MANAG_ADD2,
  669. ALC5632_PWR_ADD2_PLL1,
  670. ALC5632_PWR_ADD2_PLL1);
  671. /* enable PLL2 */
  672. snd_soc_update_bits(codec, ALC5632_PWR_MANAG_ADD2,
  673. ALC5632_PWR_ADD2_PLL2,
  674. ALC5632_PWR_ADD2_PLL2);
  675. /* use PLL1 as main SYSCLK */
  676. snd_soc_update_bits(codec, ALC5632_GPCR1,
  677. ALC5632_GPCR1_CLK_SYS_SRC_SEL_PLL1,
  678. ALC5632_GPCR1_CLK_SYS_SRC_SEL_PLL1);
  679. return 0;
  680. }
  681. struct _coeff_div {
  682. u16 fs;
  683. u16 regvalue;
  684. };
  685. /* codec hifi mclk (after PLL) clock divider coefficients */
  686. /* values inspired from column BCLK=32Fs of Appendix A table */
  687. static const struct _coeff_div coeff_div[] = {
  688. {512*1, 0x3075},
  689. };
  690. static int get_coeff(struct snd_soc_codec *codec, int rate)
  691. {
  692. struct alc5632_priv *alc5632 = snd_soc_codec_get_drvdata(codec);
  693. int i;
  694. for (i = 0; i < ARRAY_SIZE(coeff_div); i++) {
  695. if (coeff_div[i].fs * rate == alc5632->sysclk)
  696. return i;
  697. }
  698. return -EINVAL;
  699. }
  700. /*
  701. * Clock after PLL and dividers
  702. */
  703. static int alc5632_set_dai_sysclk(struct snd_soc_dai *codec_dai,
  704. int clk_id, unsigned int freq, int dir)
  705. {
  706. struct snd_soc_codec *codec = codec_dai->codec;
  707. struct alc5632_priv *alc5632 = snd_soc_codec_get_drvdata(codec);
  708. switch (freq) {
  709. case 4096000:
  710. case 8192000:
  711. case 11289600:
  712. case 12288000:
  713. case 16384000:
  714. case 16934400:
  715. case 18432000:
  716. case 22579200:
  717. case 24576000:
  718. alc5632->sysclk = freq;
  719. return 0;
  720. }
  721. return -EINVAL;
  722. }
  723. static int alc5632_set_dai_fmt(struct snd_soc_dai *codec_dai,
  724. unsigned int fmt)
  725. {
  726. struct snd_soc_codec *codec = codec_dai->codec;
  727. u16 iface = 0;
  728. /* set master/slave audio interface */
  729. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  730. case SND_SOC_DAIFMT_CBM_CFM:
  731. iface = ALC5632_DAI_SDP_MASTER_MODE;
  732. break;
  733. case SND_SOC_DAIFMT_CBS_CFS:
  734. iface = ALC5632_DAI_SDP_SLAVE_MODE;
  735. break;
  736. default:
  737. return -EINVAL;
  738. }
  739. /* interface format */
  740. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  741. case SND_SOC_DAIFMT_I2S:
  742. iface |= ALC5632_DAI_I2S_DF_I2S;
  743. break;
  744. case SND_SOC_DAIFMT_LEFT_J:
  745. iface |= ALC5632_DAI_I2S_DF_LEFT;
  746. break;
  747. case SND_SOC_DAIFMT_DSP_A:
  748. iface |= ALC5632_DAI_I2S_DF_PCM_A;
  749. break;
  750. case SND_SOC_DAIFMT_DSP_B:
  751. iface |= ALC5632_DAI_I2S_DF_PCM_B;
  752. break;
  753. default:
  754. return -EINVAL;
  755. }
  756. /* clock inversion */
  757. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  758. case SND_SOC_DAIFMT_NB_NF:
  759. break;
  760. case SND_SOC_DAIFMT_IB_IF:
  761. iface |= ALC5632_DAI_MAIN_I2S_BCLK_POL_CTRL;
  762. break;
  763. case SND_SOC_DAIFMT_IB_NF:
  764. iface |= ALC5632_DAI_MAIN_I2S_BCLK_POL_CTRL;
  765. break;
  766. case SND_SOC_DAIFMT_NB_IF:
  767. break;
  768. default:
  769. return -EINVAL;
  770. }
  771. return snd_soc_write(codec, ALC5632_DAI_CONTROL, iface);
  772. }
  773. static int alc5632_pcm_hw_params(struct snd_pcm_substream *substream,
  774. struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
  775. {
  776. struct snd_soc_codec *codec = dai->codec;
  777. int coeff, rate;
  778. u16 iface;
  779. iface = snd_soc_read(codec, ALC5632_DAI_CONTROL);
  780. iface &= ~ALC5632_DAI_I2S_DL_MASK;
  781. /* bit size */
  782. switch (params_width(params)) {
  783. case 16:
  784. iface |= ALC5632_DAI_I2S_DL_16;
  785. break;
  786. case 20:
  787. iface |= ALC5632_DAI_I2S_DL_20;
  788. break;
  789. case 24:
  790. iface |= ALC5632_DAI_I2S_DL_24;
  791. break;
  792. default:
  793. return -EINVAL;
  794. }
  795. /* set iface & srate */
  796. snd_soc_write(codec, ALC5632_DAI_CONTROL, iface);
  797. rate = params_rate(params);
  798. coeff = get_coeff(codec, rate);
  799. if (coeff < 0)
  800. return -EINVAL;
  801. coeff = coeff_div[coeff].regvalue;
  802. snd_soc_write(codec, ALC5632_DAC_CLK_CTRL1, coeff);
  803. return 0;
  804. }
  805. static int alc5632_mute(struct snd_soc_dai *dai, int mute)
  806. {
  807. struct snd_soc_codec *codec = dai->codec;
  808. u16 hp_mute = ALC5632_MISC_HP_DEPOP_MUTE_L
  809. |ALC5632_MISC_HP_DEPOP_MUTE_R;
  810. u16 mute_reg = snd_soc_read(codec, ALC5632_MISC_CTRL) & ~hp_mute;
  811. if (mute)
  812. mute_reg |= hp_mute;
  813. return snd_soc_write(codec, ALC5632_MISC_CTRL, mute_reg);
  814. }
  815. #define ALC5632_ADD2_POWER_EN (ALC5632_PWR_ADD2_VREF)
  816. #define ALC5632_ADD3_POWER_EN (ALC5632_PWR_ADD3_MIC1_BOOST_AD)
  817. #define ALC5632_ADD1_POWER_EN \
  818. (ALC5632_PWR_ADD1_DAC_REF \
  819. | ALC5632_PWR_ADD1_SOFTGEN_EN \
  820. | ALC5632_PWR_ADD1_HP_OUT_AMP \
  821. | ALC5632_PWR_ADD1_HP_OUT_ENH_AMP \
  822. | ALC5632_PWR_ADD1_MAIN_BIAS)
  823. static void enable_power_depop(struct snd_soc_codec *codec)
  824. {
  825. snd_soc_update_bits(codec, ALC5632_PWR_MANAG_ADD1,
  826. ALC5632_PWR_ADD1_SOFTGEN_EN,
  827. ALC5632_PWR_ADD1_SOFTGEN_EN);
  828. snd_soc_update_bits(codec, ALC5632_PWR_MANAG_ADD3,
  829. ALC5632_ADD3_POWER_EN,
  830. ALC5632_ADD3_POWER_EN);
  831. snd_soc_update_bits(codec, ALC5632_MISC_CTRL,
  832. ALC5632_MISC_HP_DEPOP_MODE2_EN,
  833. ALC5632_MISC_HP_DEPOP_MODE2_EN);
  834. /* "normal" mode: 0 @ 26 */
  835. /* set all PR0-7 mixers to 0 */
  836. snd_soc_update_bits(codec, ALC5632_PWR_DOWN_CTRL_STATUS,
  837. ALC5632_PWR_DOWN_CTRL_STATUS_MASK,
  838. 0);
  839. msleep(500);
  840. snd_soc_update_bits(codec, ALC5632_PWR_MANAG_ADD2,
  841. ALC5632_ADD2_POWER_EN,
  842. ALC5632_ADD2_POWER_EN);
  843. snd_soc_update_bits(codec, ALC5632_PWR_MANAG_ADD1,
  844. ALC5632_ADD1_POWER_EN,
  845. ALC5632_ADD1_POWER_EN);
  846. /* disable HP Depop2 */
  847. snd_soc_update_bits(codec, ALC5632_MISC_CTRL,
  848. ALC5632_MISC_HP_DEPOP_MODE2_EN,
  849. 0);
  850. }
  851. static int alc5632_set_bias_level(struct snd_soc_codec *codec,
  852. enum snd_soc_bias_level level)
  853. {
  854. switch (level) {
  855. case SND_SOC_BIAS_ON:
  856. enable_power_depop(codec);
  857. break;
  858. case SND_SOC_BIAS_PREPARE:
  859. break;
  860. case SND_SOC_BIAS_STANDBY:
  861. /* everything off except vref/vmid, */
  862. snd_soc_update_bits(codec, ALC5632_PWR_MANAG_ADD1,
  863. ALC5632_PWR_MANAG_ADD1_MASK,
  864. ALC5632_PWR_ADD1_MAIN_BIAS);
  865. snd_soc_update_bits(codec, ALC5632_PWR_MANAG_ADD2,
  866. ALC5632_PWR_MANAG_ADD2_MASK,
  867. ALC5632_PWR_ADD2_VREF);
  868. /* "normal" mode: 0 @ 26 */
  869. snd_soc_update_bits(codec, ALC5632_PWR_DOWN_CTRL_STATUS,
  870. ALC5632_PWR_DOWN_CTRL_STATUS_MASK,
  871. 0xffff ^ (ALC5632_PWR_VREF_PR3
  872. | ALC5632_PWR_VREF_PR2));
  873. break;
  874. case SND_SOC_BIAS_OFF:
  875. /* everything off, dac mute, inactive */
  876. snd_soc_update_bits(codec, ALC5632_PWR_MANAG_ADD2,
  877. ALC5632_PWR_MANAG_ADD2_MASK, 0);
  878. snd_soc_update_bits(codec, ALC5632_PWR_MANAG_ADD3,
  879. ALC5632_PWR_MANAG_ADD3_MASK, 0);
  880. snd_soc_update_bits(codec, ALC5632_PWR_MANAG_ADD1,
  881. ALC5632_PWR_MANAG_ADD1_MASK, 0);
  882. break;
  883. }
  884. return 0;
  885. }
  886. #define ALC5632_FORMATS (SNDRV_PCM_FMTBIT_S16_LE \
  887. | SNDRV_PCM_FMTBIT_S24_LE \
  888. | SNDRV_PCM_FMTBIT_S32_LE)
  889. static const struct snd_soc_dai_ops alc5632_dai_ops = {
  890. .hw_params = alc5632_pcm_hw_params,
  891. .digital_mute = alc5632_mute,
  892. .set_fmt = alc5632_set_dai_fmt,
  893. .set_sysclk = alc5632_set_dai_sysclk,
  894. .set_pll = alc5632_set_dai_pll,
  895. };
  896. static struct snd_soc_dai_driver alc5632_dai = {
  897. .name = "alc5632-hifi",
  898. .playback = {
  899. .stream_name = "HiFi Playback",
  900. .channels_min = 1,
  901. .channels_max = 2,
  902. .rate_min = 8000,
  903. .rate_max = 48000,
  904. .rates = SNDRV_PCM_RATE_8000_48000,
  905. .formats = ALC5632_FORMATS,},
  906. .capture = {
  907. .stream_name = "HiFi Capture",
  908. .channels_min = 1,
  909. .channels_max = 2,
  910. .rate_min = 8000,
  911. .rate_max = 48000,
  912. .rates = SNDRV_PCM_RATE_8000_48000,
  913. .formats = ALC5632_FORMATS,},
  914. .ops = &alc5632_dai_ops,
  915. .symmetric_rates = 1,
  916. };
  917. #ifdef CONFIG_PM
  918. static int alc5632_resume(struct snd_soc_codec *codec)
  919. {
  920. struct alc5632_priv *alc5632 = snd_soc_codec_get_drvdata(codec);
  921. regcache_sync(alc5632->regmap);
  922. return 0;
  923. }
  924. #else
  925. #define alc5632_resume NULL
  926. #endif
  927. static int alc5632_probe(struct snd_soc_codec *codec)
  928. {
  929. struct alc5632_priv *alc5632 = snd_soc_codec_get_drvdata(codec);
  930. switch (alc5632->id) {
  931. case 0x5c:
  932. snd_soc_add_codec_controls(codec, alc5632_vol_snd_controls,
  933. ARRAY_SIZE(alc5632_vol_snd_controls));
  934. break;
  935. default:
  936. return -EINVAL;
  937. }
  938. return 0;
  939. }
  940. static const struct snd_soc_codec_driver soc_codec_device_alc5632 = {
  941. .probe = alc5632_probe,
  942. .resume = alc5632_resume,
  943. .set_bias_level = alc5632_set_bias_level,
  944. .suspend_bias_off = true,
  945. .component_driver = {
  946. .controls = alc5632_snd_controls,
  947. .num_controls = ARRAY_SIZE(alc5632_snd_controls),
  948. .dapm_widgets = alc5632_dapm_widgets,
  949. .num_dapm_widgets = ARRAY_SIZE(alc5632_dapm_widgets),
  950. .dapm_routes = alc5632_dapm_routes,
  951. .num_dapm_routes = ARRAY_SIZE(alc5632_dapm_routes),
  952. },
  953. };
  954. static const struct regmap_config alc5632_regmap = {
  955. .reg_bits = 8,
  956. .val_bits = 16,
  957. .max_register = ALC5632_MAX_REGISTER,
  958. .reg_defaults = alc5632_reg_defaults,
  959. .num_reg_defaults = ARRAY_SIZE(alc5632_reg_defaults),
  960. .volatile_reg = alc5632_volatile_register,
  961. .cache_type = REGCACHE_RBTREE,
  962. };
  963. /*
  964. * alc5632 2 wire address is determined by A1 pin
  965. * state during powerup.
  966. * low = 0x1a
  967. * high = 0x1b
  968. */
  969. static int alc5632_i2c_probe(struct i2c_client *client,
  970. const struct i2c_device_id *id)
  971. {
  972. struct alc5632_priv *alc5632;
  973. int ret, ret1, ret2;
  974. unsigned int vid1, vid2;
  975. alc5632 = devm_kzalloc(&client->dev,
  976. sizeof(struct alc5632_priv), GFP_KERNEL);
  977. if (alc5632 == NULL)
  978. return -ENOMEM;
  979. i2c_set_clientdata(client, alc5632);
  980. alc5632->regmap = devm_regmap_init_i2c(client, &alc5632_regmap);
  981. if (IS_ERR(alc5632->regmap)) {
  982. ret = PTR_ERR(alc5632->regmap);
  983. dev_err(&client->dev, "regmap_init() failed: %d\n", ret);
  984. return ret;
  985. }
  986. ret1 = regmap_read(alc5632->regmap, ALC5632_VENDOR_ID1, &vid1);
  987. ret2 = regmap_read(alc5632->regmap, ALC5632_VENDOR_ID2, &vid2);
  988. if (ret1 != 0 || ret2 != 0) {
  989. dev_err(&client->dev,
  990. "Failed to read chip ID: ret1=%d, ret2=%d\n", ret1, ret2);
  991. return -EIO;
  992. }
  993. vid2 >>= 8;
  994. if ((vid1 != 0x10EC) || (vid2 != id->driver_data)) {
  995. dev_err(&client->dev,
  996. "Device is not a ALC5632: VID1=0x%x, VID2=0x%x\n", vid1, vid2);
  997. return -EINVAL;
  998. }
  999. ret = alc5632_reset(alc5632->regmap);
  1000. if (ret < 0) {
  1001. dev_err(&client->dev, "Failed to issue reset\n");
  1002. return ret;
  1003. }
  1004. alc5632->id = vid2;
  1005. switch (alc5632->id) {
  1006. case 0x5c:
  1007. alc5632_dai.name = "alc5632-hifi";
  1008. break;
  1009. default:
  1010. return -EINVAL;
  1011. }
  1012. ret = snd_soc_register_codec(&client->dev,
  1013. &soc_codec_device_alc5632, &alc5632_dai, 1);
  1014. if (ret < 0) {
  1015. dev_err(&client->dev, "Failed to register codec: %d\n", ret);
  1016. return ret;
  1017. }
  1018. return ret;
  1019. }
  1020. static int alc5632_i2c_remove(struct i2c_client *client)
  1021. {
  1022. snd_soc_unregister_codec(&client->dev);
  1023. return 0;
  1024. }
  1025. static const struct i2c_device_id alc5632_i2c_table[] = {
  1026. {"alc5632", 0x5c},
  1027. {}
  1028. };
  1029. MODULE_DEVICE_TABLE(i2c, alc5632_i2c_table);
  1030. static const struct of_device_id alc5632_of_match[] = {
  1031. { .compatible = "realtek,alc5632", },
  1032. { }
  1033. };
  1034. MODULE_DEVICE_TABLE(of, alc5632_of_match);
  1035. /* i2c codec control layer */
  1036. static struct i2c_driver alc5632_i2c_driver = {
  1037. .driver = {
  1038. .name = "alc5632",
  1039. .of_match_table = of_match_ptr(alc5632_of_match),
  1040. },
  1041. .probe = alc5632_i2c_probe,
  1042. .remove = alc5632_i2c_remove,
  1043. .id_table = alc5632_i2c_table,
  1044. };
  1045. module_i2c_driver(alc5632_i2c_driver);
  1046. MODULE_DESCRIPTION("ASoC ALC5632 driver");
  1047. MODULE_AUTHOR("Leon Romanovsky <leon@leon.nu>");
  1048. MODULE_LICENSE("GPL");