tlv320aic32x4.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001
  1. /*
  2. * linux/sound/soc/codecs/tlv320aic32x4.c
  3. *
  4. * Copyright 2011 Vista Silicon S.L.
  5. *
  6. * Author: Javier Martin <javier.martin@vista-silicon.com>
  7. *
  8. * Based on sound/soc/codecs/wm8974 and TI driver for kernel 2.6.27.
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  23. * MA 02110-1301, USA.
  24. */
  25. #include <linux/module.h>
  26. #include <linux/moduleparam.h>
  27. #include <linux/init.h>
  28. #include <linux/delay.h>
  29. #include <linux/pm.h>
  30. #include <linux/gpio.h>
  31. #include <linux/of_gpio.h>
  32. #include <linux/cdev.h>
  33. #include <linux/slab.h>
  34. #include <linux/clk.h>
  35. #include <linux/regulator/consumer.h>
  36. #include <sound/tlv320aic32x4.h>
  37. #include <sound/core.h>
  38. #include <sound/pcm.h>
  39. #include <sound/pcm_params.h>
  40. #include <sound/soc.h>
  41. #include <sound/soc-dapm.h>
  42. #include <sound/initval.h>
  43. #include <sound/tlv.h>
  44. #include "tlv320aic32x4.h"
  45. struct aic32x4_rate_divs {
  46. u32 mclk;
  47. u32 rate;
  48. u8 p_val;
  49. u8 pll_j;
  50. u16 pll_d;
  51. u16 dosr;
  52. u8 ndac;
  53. u8 mdac;
  54. u8 aosr;
  55. u8 nadc;
  56. u8 madc;
  57. u8 blck_N;
  58. };
  59. struct aic32x4_priv {
  60. struct regmap *regmap;
  61. u32 sysclk;
  62. u32 power_cfg;
  63. u32 micpga_routing;
  64. bool swapdacs;
  65. int rstn_gpio;
  66. struct clk *mclk;
  67. struct regulator *supply_ldo;
  68. struct regulator *supply_iov;
  69. struct regulator *supply_dv;
  70. struct regulator *supply_av;
  71. };
  72. /* 0dB min, 0.5dB steps */
  73. static DECLARE_TLV_DB_SCALE(tlv_step_0_5, 0, 50, 0);
  74. /* -63.5dB min, 0.5dB steps */
  75. static DECLARE_TLV_DB_SCALE(tlv_pcm, -6350, 50, 0);
  76. /* -6dB min, 1dB steps */
  77. static DECLARE_TLV_DB_SCALE(tlv_driver_gain, -600, 100, 0);
  78. /* -12dB min, 0.5dB steps */
  79. static DECLARE_TLV_DB_SCALE(tlv_adc_vol, -1200, 50, 0);
  80. static const struct snd_kcontrol_new aic32x4_snd_controls[] = {
  81. SOC_DOUBLE_R_S_TLV("PCM Playback Volume", AIC32X4_LDACVOL,
  82. AIC32X4_RDACVOL, 0, -0x7f, 0x30, 7, 0, tlv_pcm),
  83. SOC_DOUBLE_R_S_TLV("HP Driver Gain Volume", AIC32X4_HPLGAIN,
  84. AIC32X4_HPRGAIN, 0, -0x6, 0x1d, 5, 0,
  85. tlv_driver_gain),
  86. SOC_DOUBLE_R_S_TLV("LO Driver Gain Volume", AIC32X4_LOLGAIN,
  87. AIC32X4_LORGAIN, 0, -0x6, 0x1d, 5, 0,
  88. tlv_driver_gain),
  89. SOC_DOUBLE_R("HP DAC Playback Switch", AIC32X4_HPLGAIN,
  90. AIC32X4_HPRGAIN, 6, 0x01, 1),
  91. SOC_DOUBLE_R("LO DAC Playback Switch", AIC32X4_LOLGAIN,
  92. AIC32X4_LORGAIN, 6, 0x01, 1),
  93. SOC_DOUBLE_R("Mic PGA Switch", AIC32X4_LMICPGAVOL,
  94. AIC32X4_RMICPGAVOL, 7, 0x01, 1),
  95. SOC_SINGLE("ADCFGA Left Mute Switch", AIC32X4_ADCFGA, 7, 1, 0),
  96. SOC_SINGLE("ADCFGA Right Mute Switch", AIC32X4_ADCFGA, 3, 1, 0),
  97. SOC_DOUBLE_R_S_TLV("ADC Level Volume", AIC32X4_LADCVOL,
  98. AIC32X4_RADCVOL, 0, -0x18, 0x28, 6, 0, tlv_adc_vol),
  99. SOC_DOUBLE_R_TLV("PGA Level Volume", AIC32X4_LMICPGAVOL,
  100. AIC32X4_RMICPGAVOL, 0, 0x5f, 0, tlv_step_0_5),
  101. SOC_SINGLE("Auto-mute Switch", AIC32X4_DACMUTE, 4, 7, 0),
  102. SOC_SINGLE("AGC Left Switch", AIC32X4_LAGC1, 7, 1, 0),
  103. SOC_SINGLE("AGC Right Switch", AIC32X4_RAGC1, 7, 1, 0),
  104. SOC_DOUBLE_R("AGC Target Level", AIC32X4_LAGC1, AIC32X4_RAGC1,
  105. 4, 0x07, 0),
  106. SOC_DOUBLE_R("AGC Gain Hysteresis", AIC32X4_LAGC1, AIC32X4_RAGC1,
  107. 0, 0x03, 0),
  108. SOC_DOUBLE_R("AGC Hysteresis", AIC32X4_LAGC2, AIC32X4_RAGC2,
  109. 6, 0x03, 0),
  110. SOC_DOUBLE_R("AGC Noise Threshold", AIC32X4_LAGC2, AIC32X4_RAGC2,
  111. 1, 0x1F, 0),
  112. SOC_DOUBLE_R("AGC Max PGA", AIC32X4_LAGC3, AIC32X4_RAGC3,
  113. 0, 0x7F, 0),
  114. SOC_DOUBLE_R("AGC Attack Time", AIC32X4_LAGC4, AIC32X4_RAGC4,
  115. 3, 0x1F, 0),
  116. SOC_DOUBLE_R("AGC Decay Time", AIC32X4_LAGC5, AIC32X4_RAGC5,
  117. 3, 0x1F, 0),
  118. SOC_DOUBLE_R("AGC Noise Debounce", AIC32X4_LAGC6, AIC32X4_RAGC6,
  119. 0, 0x1F, 0),
  120. SOC_DOUBLE_R("AGC Signal Debounce", AIC32X4_LAGC7, AIC32X4_RAGC7,
  121. 0, 0x0F, 0),
  122. };
  123. static const struct aic32x4_rate_divs aic32x4_divs[] = {
  124. /* 8k rate */
  125. {AIC32X4_FREQ_12000000, 8000, 1, 7, 6800, 768, 5, 3, 128, 5, 18, 24},
  126. {AIC32X4_FREQ_24000000, 8000, 2, 7, 6800, 768, 15, 1, 64, 45, 4, 24},
  127. {AIC32X4_FREQ_25000000, 8000, 2, 7, 3728, 768, 15, 1, 64, 45, 4, 24},
  128. /* 11.025k rate */
  129. {AIC32X4_FREQ_12000000, 11025, 1, 7, 5264, 512, 8, 2, 128, 8, 8, 16},
  130. {AIC32X4_FREQ_24000000, 11025, 2, 7, 5264, 512, 16, 1, 64, 32, 4, 16},
  131. /* 16k rate */
  132. {AIC32X4_FREQ_12000000, 16000, 1, 7, 6800, 384, 5, 3, 128, 5, 9, 12},
  133. {AIC32X4_FREQ_24000000, 16000, 2, 7, 6800, 384, 15, 1, 64, 18, 5, 12},
  134. {AIC32X4_FREQ_25000000, 16000, 2, 7, 3728, 384, 15, 1, 64, 18, 5, 12},
  135. /* 22.05k rate */
  136. {AIC32X4_FREQ_12000000, 22050, 1, 7, 5264, 256, 4, 4, 128, 4, 8, 8},
  137. {AIC32X4_FREQ_24000000, 22050, 2, 7, 5264, 256, 16, 1, 64, 16, 4, 8},
  138. {AIC32X4_FREQ_25000000, 22050, 2, 7, 2253, 256, 16, 1, 64, 16, 4, 8},
  139. /* 32k rate */
  140. {AIC32X4_FREQ_12000000, 32000, 1, 7, 1680, 192, 2, 7, 64, 2, 21, 6},
  141. {AIC32X4_FREQ_24000000, 32000, 2, 7, 1680, 192, 7, 2, 64, 7, 6, 6},
  142. /* 44.1k rate */
  143. {AIC32X4_FREQ_12000000, 44100, 1, 7, 5264, 128, 2, 8, 128, 2, 8, 4},
  144. {AIC32X4_FREQ_24000000, 44100, 2, 7, 5264, 128, 8, 2, 64, 8, 4, 4},
  145. {AIC32X4_FREQ_25000000, 44100, 2, 7, 2253, 128, 8, 2, 64, 8, 4, 4},
  146. /* 48k rate */
  147. {AIC32X4_FREQ_12000000, 48000, 1, 8, 1920, 128, 2, 8, 128, 2, 8, 4},
  148. {AIC32X4_FREQ_24000000, 48000, 2, 8, 1920, 128, 8, 2, 64, 8, 4, 4},
  149. {AIC32X4_FREQ_25000000, 48000, 2, 7, 8643, 128, 8, 2, 64, 8, 4, 4},
  150. /* 96k rate */
  151. {AIC32X4_FREQ_25000000, 96000, 2, 7, 8643, 64, 4, 4, 64, 4, 4, 1},
  152. };
  153. static const struct snd_kcontrol_new hpl_output_mixer_controls[] = {
  154. SOC_DAPM_SINGLE("L_DAC Switch", AIC32X4_HPLROUTE, 3, 1, 0),
  155. SOC_DAPM_SINGLE("IN1_L Switch", AIC32X4_HPLROUTE, 2, 1, 0),
  156. };
  157. static const struct snd_kcontrol_new hpr_output_mixer_controls[] = {
  158. SOC_DAPM_SINGLE("R_DAC Switch", AIC32X4_HPRROUTE, 3, 1, 0),
  159. SOC_DAPM_SINGLE("IN1_R Switch", AIC32X4_HPRROUTE, 2, 1, 0),
  160. };
  161. static const struct snd_kcontrol_new lol_output_mixer_controls[] = {
  162. SOC_DAPM_SINGLE("L_DAC Switch", AIC32X4_LOLROUTE, 3, 1, 0),
  163. };
  164. static const struct snd_kcontrol_new lor_output_mixer_controls[] = {
  165. SOC_DAPM_SINGLE("R_DAC Switch", AIC32X4_LORROUTE, 3, 1, 0),
  166. };
  167. static const char * const resistor_text[] = {
  168. "Off", "10 kOhm", "20 kOhm", "40 kOhm",
  169. };
  170. /* Left mixer pins */
  171. static SOC_ENUM_SINGLE_DECL(in1l_lpga_p_enum, AIC32X4_LMICPGAPIN, 6, resistor_text);
  172. static SOC_ENUM_SINGLE_DECL(in2l_lpga_p_enum, AIC32X4_LMICPGAPIN, 4, resistor_text);
  173. static SOC_ENUM_SINGLE_DECL(in3l_lpga_p_enum, AIC32X4_LMICPGAPIN, 2, resistor_text);
  174. static SOC_ENUM_SINGLE_DECL(in1r_lpga_p_enum, AIC32X4_LMICPGAPIN, 0, resistor_text);
  175. static SOC_ENUM_SINGLE_DECL(cml_lpga_n_enum, AIC32X4_LMICPGANIN, 6, resistor_text);
  176. static SOC_ENUM_SINGLE_DECL(in2r_lpga_n_enum, AIC32X4_LMICPGANIN, 4, resistor_text);
  177. static SOC_ENUM_SINGLE_DECL(in3r_lpga_n_enum, AIC32X4_LMICPGANIN, 2, resistor_text);
  178. static const struct snd_kcontrol_new in1l_to_lmixer_controls[] = {
  179. SOC_DAPM_ENUM("IN1_L L+ Switch", in1l_lpga_p_enum),
  180. };
  181. static const struct snd_kcontrol_new in2l_to_lmixer_controls[] = {
  182. SOC_DAPM_ENUM("IN2_L L+ Switch", in2l_lpga_p_enum),
  183. };
  184. static const struct snd_kcontrol_new in3l_to_lmixer_controls[] = {
  185. SOC_DAPM_ENUM("IN3_L L+ Switch", in3l_lpga_p_enum),
  186. };
  187. static const struct snd_kcontrol_new in1r_to_lmixer_controls[] = {
  188. SOC_DAPM_ENUM("IN1_R L+ Switch", in1r_lpga_p_enum),
  189. };
  190. static const struct snd_kcontrol_new cml_to_lmixer_controls[] = {
  191. SOC_DAPM_ENUM("CM_L L- Switch", cml_lpga_n_enum),
  192. };
  193. static const struct snd_kcontrol_new in2r_to_lmixer_controls[] = {
  194. SOC_DAPM_ENUM("IN2_R L- Switch", in2r_lpga_n_enum),
  195. };
  196. static const struct snd_kcontrol_new in3r_to_lmixer_controls[] = {
  197. SOC_DAPM_ENUM("IN3_R L- Switch", in3r_lpga_n_enum),
  198. };
  199. /* Right mixer pins */
  200. static SOC_ENUM_SINGLE_DECL(in1r_rpga_p_enum, AIC32X4_RMICPGAPIN, 6, resistor_text);
  201. static SOC_ENUM_SINGLE_DECL(in2r_rpga_p_enum, AIC32X4_RMICPGAPIN, 4, resistor_text);
  202. static SOC_ENUM_SINGLE_DECL(in3r_rpga_p_enum, AIC32X4_RMICPGAPIN, 2, resistor_text);
  203. static SOC_ENUM_SINGLE_DECL(in2l_rpga_p_enum, AIC32X4_RMICPGAPIN, 0, resistor_text);
  204. static SOC_ENUM_SINGLE_DECL(cmr_rpga_n_enum, AIC32X4_RMICPGANIN, 6, resistor_text);
  205. static SOC_ENUM_SINGLE_DECL(in1l_rpga_n_enum, AIC32X4_RMICPGANIN, 4, resistor_text);
  206. static SOC_ENUM_SINGLE_DECL(in3l_rpga_n_enum, AIC32X4_RMICPGANIN, 2, resistor_text);
  207. static const struct snd_kcontrol_new in1r_to_rmixer_controls[] = {
  208. SOC_DAPM_ENUM("IN1_R R+ Switch", in1r_rpga_p_enum),
  209. };
  210. static const struct snd_kcontrol_new in2r_to_rmixer_controls[] = {
  211. SOC_DAPM_ENUM("IN2_R R+ Switch", in2r_rpga_p_enum),
  212. };
  213. static const struct snd_kcontrol_new in3r_to_rmixer_controls[] = {
  214. SOC_DAPM_ENUM("IN3_R R+ Switch", in3r_rpga_p_enum),
  215. };
  216. static const struct snd_kcontrol_new in2l_to_rmixer_controls[] = {
  217. SOC_DAPM_ENUM("IN2_L R+ Switch", in2l_rpga_p_enum),
  218. };
  219. static const struct snd_kcontrol_new cmr_to_rmixer_controls[] = {
  220. SOC_DAPM_ENUM("CM_R R- Switch", cmr_rpga_n_enum),
  221. };
  222. static const struct snd_kcontrol_new in1l_to_rmixer_controls[] = {
  223. SOC_DAPM_ENUM("IN1_L R- Switch", in1l_rpga_n_enum),
  224. };
  225. static const struct snd_kcontrol_new in3l_to_rmixer_controls[] = {
  226. SOC_DAPM_ENUM("IN3_L R- Switch", in3l_rpga_n_enum),
  227. };
  228. static const struct snd_soc_dapm_widget aic32x4_dapm_widgets[] = {
  229. SND_SOC_DAPM_DAC("Left DAC", "Left Playback", AIC32X4_DACSETUP, 7, 0),
  230. SND_SOC_DAPM_MIXER("HPL Output Mixer", SND_SOC_NOPM, 0, 0,
  231. &hpl_output_mixer_controls[0],
  232. ARRAY_SIZE(hpl_output_mixer_controls)),
  233. SND_SOC_DAPM_PGA("HPL Power", AIC32X4_OUTPWRCTL, 5, 0, NULL, 0),
  234. SND_SOC_DAPM_MIXER("LOL Output Mixer", SND_SOC_NOPM, 0, 0,
  235. &lol_output_mixer_controls[0],
  236. ARRAY_SIZE(lol_output_mixer_controls)),
  237. SND_SOC_DAPM_PGA("LOL Power", AIC32X4_OUTPWRCTL, 3, 0, NULL, 0),
  238. SND_SOC_DAPM_DAC("Right DAC", "Right Playback", AIC32X4_DACSETUP, 6, 0),
  239. SND_SOC_DAPM_MIXER("HPR Output Mixer", SND_SOC_NOPM, 0, 0,
  240. &hpr_output_mixer_controls[0],
  241. ARRAY_SIZE(hpr_output_mixer_controls)),
  242. SND_SOC_DAPM_PGA("HPR Power", AIC32X4_OUTPWRCTL, 4, 0, NULL, 0),
  243. SND_SOC_DAPM_MIXER("LOR Output Mixer", SND_SOC_NOPM, 0, 0,
  244. &lor_output_mixer_controls[0],
  245. ARRAY_SIZE(lor_output_mixer_controls)),
  246. SND_SOC_DAPM_PGA("LOR Power", AIC32X4_OUTPWRCTL, 2, 0, NULL, 0),
  247. SND_SOC_DAPM_ADC("Right ADC", "Right Capture", AIC32X4_ADCSETUP, 6, 0),
  248. SND_SOC_DAPM_MUX("IN1_R to Right Mixer Positive Resistor", SND_SOC_NOPM, 0, 0,
  249. in1r_to_rmixer_controls),
  250. SND_SOC_DAPM_MUX("IN2_R to Right Mixer Positive Resistor", SND_SOC_NOPM, 0, 0,
  251. in2r_to_rmixer_controls),
  252. SND_SOC_DAPM_MUX("IN3_R to Right Mixer Positive Resistor", SND_SOC_NOPM, 0, 0,
  253. in3r_to_rmixer_controls),
  254. SND_SOC_DAPM_MUX("IN2_L to Right Mixer Positive Resistor", SND_SOC_NOPM, 0, 0,
  255. in2l_to_rmixer_controls),
  256. SND_SOC_DAPM_MUX("CM_R to Right Mixer Negative Resistor", SND_SOC_NOPM, 0, 0,
  257. cmr_to_rmixer_controls),
  258. SND_SOC_DAPM_MUX("IN1_L to Right Mixer Negative Resistor", SND_SOC_NOPM, 0, 0,
  259. in1l_to_rmixer_controls),
  260. SND_SOC_DAPM_MUX("IN3_L to Right Mixer Negative Resistor", SND_SOC_NOPM, 0, 0,
  261. in3l_to_rmixer_controls),
  262. SND_SOC_DAPM_ADC("Left ADC", "Left Capture", AIC32X4_ADCSETUP, 7, 0),
  263. SND_SOC_DAPM_MUX("IN1_L to Left Mixer Positive Resistor", SND_SOC_NOPM, 0, 0,
  264. in1l_to_lmixer_controls),
  265. SND_SOC_DAPM_MUX("IN2_L to Left Mixer Positive Resistor", SND_SOC_NOPM, 0, 0,
  266. in2l_to_lmixer_controls),
  267. SND_SOC_DAPM_MUX("IN3_L to Left Mixer Positive Resistor", SND_SOC_NOPM, 0, 0,
  268. in3l_to_lmixer_controls),
  269. SND_SOC_DAPM_MUX("IN1_R to Left Mixer Positive Resistor", SND_SOC_NOPM, 0, 0,
  270. in1r_to_lmixer_controls),
  271. SND_SOC_DAPM_MUX("CM_L to Left Mixer Negative Resistor", SND_SOC_NOPM, 0, 0,
  272. cml_to_lmixer_controls),
  273. SND_SOC_DAPM_MUX("IN2_R to Left Mixer Negative Resistor", SND_SOC_NOPM, 0, 0,
  274. in2r_to_lmixer_controls),
  275. SND_SOC_DAPM_MUX("IN3_R to Left Mixer Negative Resistor", SND_SOC_NOPM, 0, 0,
  276. in3r_to_lmixer_controls),
  277. SND_SOC_DAPM_MICBIAS("Mic Bias", AIC32X4_MICBIAS, 6, 0),
  278. SND_SOC_DAPM_OUTPUT("HPL"),
  279. SND_SOC_DAPM_OUTPUT("HPR"),
  280. SND_SOC_DAPM_OUTPUT("LOL"),
  281. SND_SOC_DAPM_OUTPUT("LOR"),
  282. SND_SOC_DAPM_INPUT("IN1_L"),
  283. SND_SOC_DAPM_INPUT("IN1_R"),
  284. SND_SOC_DAPM_INPUT("IN2_L"),
  285. SND_SOC_DAPM_INPUT("IN2_R"),
  286. SND_SOC_DAPM_INPUT("IN3_L"),
  287. SND_SOC_DAPM_INPUT("IN3_R"),
  288. };
  289. static const struct snd_soc_dapm_route aic32x4_dapm_routes[] = {
  290. /* Left Output */
  291. {"HPL Output Mixer", "L_DAC Switch", "Left DAC"},
  292. {"HPL Output Mixer", "IN1_L Switch", "IN1_L"},
  293. {"HPL Power", NULL, "HPL Output Mixer"},
  294. {"HPL", NULL, "HPL Power"},
  295. {"LOL Output Mixer", "L_DAC Switch", "Left DAC"},
  296. {"LOL Power", NULL, "LOL Output Mixer"},
  297. {"LOL", NULL, "LOL Power"},
  298. /* Right Output */
  299. {"HPR Output Mixer", "R_DAC Switch", "Right DAC"},
  300. {"HPR Output Mixer", "IN1_R Switch", "IN1_R"},
  301. {"HPR Power", NULL, "HPR Output Mixer"},
  302. {"HPR", NULL, "HPR Power"},
  303. {"LOR Output Mixer", "R_DAC Switch", "Right DAC"},
  304. {"LOR Power", NULL, "LOR Output Mixer"},
  305. {"LOR", NULL, "LOR Power"},
  306. /* Right Input */
  307. {"Right ADC", NULL, "IN1_R to Right Mixer Positive Resistor"},
  308. {"IN1_R to Right Mixer Positive Resistor", "10 kOhm", "IN1_R"},
  309. {"IN1_R to Right Mixer Positive Resistor", "20 kOhm", "IN1_R"},
  310. {"IN1_R to Right Mixer Positive Resistor", "40 kOhm", "IN1_R"},
  311. {"Right ADC", NULL, "IN2_R to Right Mixer Positive Resistor"},
  312. {"IN2_R to Right Mixer Positive Resistor", "10 kOhm", "IN2_R"},
  313. {"IN2_R to Right Mixer Positive Resistor", "20 kOhm", "IN2_R"},
  314. {"IN2_R to Right Mixer Positive Resistor", "40 kOhm", "IN2_R"},
  315. {"Right ADC", NULL, "IN3_R to Right Mixer Positive Resistor"},
  316. {"IN3_R to Right Mixer Positive Resistor", "10 kOhm", "IN3_R"},
  317. {"IN3_R to Right Mixer Positive Resistor", "20 kOhm", "IN3_R"},
  318. {"IN3_R to Right Mixer Positive Resistor", "40 kOhm", "IN3_R"},
  319. {"Right ADC", NULL, "IN2_L to Right Mixer Positive Resistor"},
  320. {"IN2_L to Right Mixer Positive Resistor", "10 kOhm", "IN2_L"},
  321. {"IN2_L to Right Mixer Positive Resistor", "20 kOhm", "IN2_L"},
  322. {"IN2_L to Right Mixer Positive Resistor", "40 kOhm", "IN2_L"},
  323. {"Right ADC", NULL, "CM_R to Right Mixer Negative Resistor"},
  324. {"CM_R to Right Mixer Negative Resistor", "10 kOhm", "CM_R"},
  325. {"CM_R to Right Mixer Negative Resistor", "20 kOhm", "CM_R"},
  326. {"CM_R to Right Mixer Negative Resistor", "40 kOhm", "CM_R"},
  327. {"Right ADC", NULL, "IN1_L to Right Mixer Negative Resistor"},
  328. {"IN1_L to Right Mixer Negative Resistor", "10 kOhm", "IN1_L"},
  329. {"IN1_L to Right Mixer Negative Resistor", "20 kOhm", "IN1_L"},
  330. {"IN1_L to Right Mixer Negative Resistor", "40 kOhm", "IN1_L"},
  331. {"Right ADC", NULL, "IN3_L to Right Mixer Negative Resistor"},
  332. {"IN3_L to Right Mixer Negative Resistor", "10 kOhm", "IN3_L"},
  333. {"IN3_L to Right Mixer Negative Resistor", "20 kOhm", "IN3_L"},
  334. {"IN3_L to Right Mixer Negative Resistor", "40 kOhm", "IN3_L"},
  335. /* Left Input */
  336. {"Left ADC", NULL, "IN1_L to Left Mixer Positive Resistor"},
  337. {"IN1_L to Left Mixer Positive Resistor", "10 kOhm", "IN1_L"},
  338. {"IN1_L to Left Mixer Positive Resistor", "20 kOhm", "IN1_L"},
  339. {"IN1_L to Left Mixer Positive Resistor", "40 kOhm", "IN1_L"},
  340. {"Left ADC", NULL, "IN2_L to Left Mixer Positive Resistor"},
  341. {"IN2_L to Left Mixer Positive Resistor", "10 kOhm", "IN2_L"},
  342. {"IN2_L to Left Mixer Positive Resistor", "20 kOhm", "IN2_L"},
  343. {"IN2_L to Left Mixer Positive Resistor", "40 kOhm", "IN2_L"},
  344. {"Left ADC", NULL, "IN3_L to Left Mixer Positive Resistor"},
  345. {"IN3_L to Left Mixer Positive Resistor", "10 kOhm", "IN3_L"},
  346. {"IN3_L to Left Mixer Positive Resistor", "20 kOhm", "IN3_L"},
  347. {"IN3_L to Left Mixer Positive Resistor", "40 kOhm", "IN3_L"},
  348. {"Left ADC", NULL, "IN1_R to Left Mixer Positive Resistor"},
  349. {"IN1_R to Left Mixer Positive Resistor", "10 kOhm", "IN1_R"},
  350. {"IN1_R to Left Mixer Positive Resistor", "20 kOhm", "IN1_R"},
  351. {"IN1_R to Left Mixer Positive Resistor", "40 kOhm", "IN1_R"},
  352. {"Left ADC", NULL, "CM_L to Left Mixer Negative Resistor"},
  353. {"CM_L to Left Mixer Negative Resistor", "10 kOhm", "CM_L"},
  354. {"CM_L to Left Mixer Negative Resistor", "20 kOhm", "CM_L"},
  355. {"CM_L to Left Mixer Negative Resistor", "40 kOhm", "CM_L"},
  356. {"Left ADC", NULL, "IN2_R to Left Mixer Negative Resistor"},
  357. {"IN2_R to Left Mixer Negative Resistor", "10 kOhm", "IN2_R"},
  358. {"IN2_R to Left Mixer Negative Resistor", "20 kOhm", "IN2_R"},
  359. {"IN2_R to Left Mixer Negative Resistor", "40 kOhm", "IN2_R"},
  360. {"Left ADC", NULL, "IN3_R to Left Mixer Negative Resistor"},
  361. {"IN3_R to Left Mixer Negative Resistor", "10 kOhm", "IN3_R"},
  362. {"IN3_R to Left Mixer Negative Resistor", "20 kOhm", "IN3_R"},
  363. {"IN3_R to Left Mixer Negative Resistor", "40 kOhm", "IN3_R"},
  364. };
  365. static const struct regmap_range_cfg aic32x4_regmap_pages[] = {
  366. {
  367. .selector_reg = 0,
  368. .selector_mask = 0xff,
  369. .window_start = 0,
  370. .window_len = 128,
  371. .range_min = 0,
  372. .range_max = AIC32X4_RMICPGAVOL,
  373. },
  374. };
  375. const struct regmap_config aic32x4_regmap_config = {
  376. .max_register = AIC32X4_RMICPGAVOL,
  377. .ranges = aic32x4_regmap_pages,
  378. .num_ranges = ARRAY_SIZE(aic32x4_regmap_pages),
  379. };
  380. EXPORT_SYMBOL(aic32x4_regmap_config);
  381. static inline int aic32x4_get_divs(int mclk, int rate)
  382. {
  383. int i;
  384. for (i = 0; i < ARRAY_SIZE(aic32x4_divs); i++) {
  385. if ((aic32x4_divs[i].rate == rate)
  386. && (aic32x4_divs[i].mclk == mclk)) {
  387. return i;
  388. }
  389. }
  390. printk(KERN_ERR "aic32x4: master clock and sample rate is not supported\n");
  391. return -EINVAL;
  392. }
  393. static int aic32x4_set_dai_sysclk(struct snd_soc_dai *codec_dai,
  394. int clk_id, unsigned int freq, int dir)
  395. {
  396. struct snd_soc_codec *codec = codec_dai->codec;
  397. struct aic32x4_priv *aic32x4 = snd_soc_codec_get_drvdata(codec);
  398. switch (freq) {
  399. case AIC32X4_FREQ_12000000:
  400. case AIC32X4_FREQ_24000000:
  401. case AIC32X4_FREQ_25000000:
  402. aic32x4->sysclk = freq;
  403. return 0;
  404. }
  405. printk(KERN_ERR "aic32x4: invalid frequency to set DAI system clock\n");
  406. return -EINVAL;
  407. }
  408. static int aic32x4_set_dai_fmt(struct snd_soc_dai *codec_dai, unsigned int fmt)
  409. {
  410. struct snd_soc_codec *codec = codec_dai->codec;
  411. u8 iface_reg_1;
  412. u8 iface_reg_2;
  413. u8 iface_reg_3;
  414. iface_reg_1 = snd_soc_read(codec, AIC32X4_IFACE1);
  415. iface_reg_1 = iface_reg_1 & ~(3 << 6 | 3 << 2);
  416. iface_reg_2 = snd_soc_read(codec, AIC32X4_IFACE2);
  417. iface_reg_2 = 0;
  418. iface_reg_3 = snd_soc_read(codec, AIC32X4_IFACE3);
  419. iface_reg_3 = iface_reg_3 & ~(1 << 3);
  420. /* set master/slave audio interface */
  421. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  422. case SND_SOC_DAIFMT_CBM_CFM:
  423. iface_reg_1 |= AIC32X4_BCLKMASTER | AIC32X4_WCLKMASTER;
  424. break;
  425. case SND_SOC_DAIFMT_CBS_CFS:
  426. break;
  427. default:
  428. printk(KERN_ERR "aic32x4: invalid DAI master/slave interface\n");
  429. return -EINVAL;
  430. }
  431. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  432. case SND_SOC_DAIFMT_I2S:
  433. break;
  434. case SND_SOC_DAIFMT_DSP_A:
  435. iface_reg_1 |= (AIC32X4_DSP_MODE << AIC32X4_PLLJ_SHIFT);
  436. iface_reg_3 |= (1 << 3); /* invert bit clock */
  437. iface_reg_2 = 0x01; /* add offset 1 */
  438. break;
  439. case SND_SOC_DAIFMT_DSP_B:
  440. iface_reg_1 |= (AIC32X4_DSP_MODE << AIC32X4_PLLJ_SHIFT);
  441. iface_reg_3 |= (1 << 3); /* invert bit clock */
  442. break;
  443. case SND_SOC_DAIFMT_RIGHT_J:
  444. iface_reg_1 |=
  445. (AIC32X4_RIGHT_JUSTIFIED_MODE << AIC32X4_PLLJ_SHIFT);
  446. break;
  447. case SND_SOC_DAIFMT_LEFT_J:
  448. iface_reg_1 |=
  449. (AIC32X4_LEFT_JUSTIFIED_MODE << AIC32X4_PLLJ_SHIFT);
  450. break;
  451. default:
  452. printk(KERN_ERR "aic32x4: invalid DAI interface format\n");
  453. return -EINVAL;
  454. }
  455. snd_soc_write(codec, AIC32X4_IFACE1, iface_reg_1);
  456. snd_soc_write(codec, AIC32X4_IFACE2, iface_reg_2);
  457. snd_soc_write(codec, AIC32X4_IFACE3, iface_reg_3);
  458. return 0;
  459. }
  460. static int aic32x4_hw_params(struct snd_pcm_substream *substream,
  461. struct snd_pcm_hw_params *params,
  462. struct snd_soc_dai *dai)
  463. {
  464. struct snd_soc_codec *codec = dai->codec;
  465. struct aic32x4_priv *aic32x4 = snd_soc_codec_get_drvdata(codec);
  466. u8 data;
  467. int i;
  468. i = aic32x4_get_divs(aic32x4->sysclk, params_rate(params));
  469. if (i < 0) {
  470. printk(KERN_ERR "aic32x4: sampling rate not supported\n");
  471. return i;
  472. }
  473. /* Use PLL as CODEC_CLKIN and DAC_MOD_CLK as BDIV_CLKIN */
  474. snd_soc_write(codec, AIC32X4_CLKMUX, AIC32X4_PLLCLKIN);
  475. snd_soc_write(codec, AIC32X4_IFACE3, AIC32X4_DACMOD2BCLK);
  476. /* We will fix R value to 1 and will make P & J=K.D as varialble */
  477. data = snd_soc_read(codec, AIC32X4_PLLPR);
  478. data &= ~(7 << 4);
  479. snd_soc_write(codec, AIC32X4_PLLPR,
  480. (data | (aic32x4_divs[i].p_val << 4) | 0x01));
  481. snd_soc_write(codec, AIC32X4_PLLJ, aic32x4_divs[i].pll_j);
  482. snd_soc_write(codec, AIC32X4_PLLDMSB, (aic32x4_divs[i].pll_d >> 8));
  483. snd_soc_write(codec, AIC32X4_PLLDLSB,
  484. (aic32x4_divs[i].pll_d & 0xff));
  485. /* NDAC divider value */
  486. data = snd_soc_read(codec, AIC32X4_NDAC);
  487. data &= ~(0x7f);
  488. snd_soc_write(codec, AIC32X4_NDAC, data | aic32x4_divs[i].ndac);
  489. /* MDAC divider value */
  490. data = snd_soc_read(codec, AIC32X4_MDAC);
  491. data &= ~(0x7f);
  492. snd_soc_write(codec, AIC32X4_MDAC, data | aic32x4_divs[i].mdac);
  493. /* DOSR MSB & LSB values */
  494. snd_soc_write(codec, AIC32X4_DOSRMSB, aic32x4_divs[i].dosr >> 8);
  495. snd_soc_write(codec, AIC32X4_DOSRLSB,
  496. (aic32x4_divs[i].dosr & 0xff));
  497. /* NADC divider value */
  498. data = snd_soc_read(codec, AIC32X4_NADC);
  499. data &= ~(0x7f);
  500. snd_soc_write(codec, AIC32X4_NADC, data | aic32x4_divs[i].nadc);
  501. /* MADC divider value */
  502. data = snd_soc_read(codec, AIC32X4_MADC);
  503. data &= ~(0x7f);
  504. snd_soc_write(codec, AIC32X4_MADC, data | aic32x4_divs[i].madc);
  505. /* AOSR value */
  506. snd_soc_write(codec, AIC32X4_AOSR, aic32x4_divs[i].aosr);
  507. /* BCLK N divider */
  508. data = snd_soc_read(codec, AIC32X4_BCLKN);
  509. data &= ~(0x7f);
  510. snd_soc_write(codec, AIC32X4_BCLKN, data | aic32x4_divs[i].blck_N);
  511. data = snd_soc_read(codec, AIC32X4_IFACE1);
  512. data = data & ~(3 << 4);
  513. switch (params_width(params)) {
  514. case 16:
  515. break;
  516. case 20:
  517. data |= (AIC32X4_WORD_LEN_20BITS << AIC32X4_DOSRMSB_SHIFT);
  518. break;
  519. case 24:
  520. data |= (AIC32X4_WORD_LEN_24BITS << AIC32X4_DOSRMSB_SHIFT);
  521. break;
  522. case 32:
  523. data |= (AIC32X4_WORD_LEN_32BITS << AIC32X4_DOSRMSB_SHIFT);
  524. break;
  525. }
  526. snd_soc_write(codec, AIC32X4_IFACE1, data);
  527. if (params_channels(params) == 1) {
  528. data = AIC32X4_RDAC2LCHN | AIC32X4_LDAC2LCHN;
  529. } else {
  530. if (aic32x4->swapdacs)
  531. data = AIC32X4_RDAC2LCHN | AIC32X4_LDAC2RCHN;
  532. else
  533. data = AIC32X4_LDAC2LCHN | AIC32X4_RDAC2RCHN;
  534. }
  535. snd_soc_update_bits(codec, AIC32X4_DACSETUP, AIC32X4_DAC_CHAN_MASK,
  536. data);
  537. return 0;
  538. }
  539. static int aic32x4_mute(struct snd_soc_dai *dai, int mute)
  540. {
  541. struct snd_soc_codec *codec = dai->codec;
  542. u8 dac_reg;
  543. dac_reg = snd_soc_read(codec, AIC32X4_DACMUTE) & ~AIC32X4_MUTEON;
  544. if (mute)
  545. snd_soc_write(codec, AIC32X4_DACMUTE, dac_reg | AIC32X4_MUTEON);
  546. else
  547. snd_soc_write(codec, AIC32X4_DACMUTE, dac_reg);
  548. return 0;
  549. }
  550. static int aic32x4_set_bias_level(struct snd_soc_codec *codec,
  551. enum snd_soc_bias_level level)
  552. {
  553. struct aic32x4_priv *aic32x4 = snd_soc_codec_get_drvdata(codec);
  554. int ret;
  555. switch (level) {
  556. case SND_SOC_BIAS_ON:
  557. /* Switch on master clock */
  558. ret = clk_prepare_enable(aic32x4->mclk);
  559. if (ret) {
  560. dev_err(codec->dev, "Failed to enable master clock\n");
  561. return ret;
  562. }
  563. /* Switch on PLL */
  564. snd_soc_update_bits(codec, AIC32X4_PLLPR,
  565. AIC32X4_PLLEN, AIC32X4_PLLEN);
  566. /* Switch on NDAC Divider */
  567. snd_soc_update_bits(codec, AIC32X4_NDAC,
  568. AIC32X4_NDACEN, AIC32X4_NDACEN);
  569. /* Switch on MDAC Divider */
  570. snd_soc_update_bits(codec, AIC32X4_MDAC,
  571. AIC32X4_MDACEN, AIC32X4_MDACEN);
  572. /* Switch on NADC Divider */
  573. snd_soc_update_bits(codec, AIC32X4_NADC,
  574. AIC32X4_NADCEN, AIC32X4_NADCEN);
  575. /* Switch on MADC Divider */
  576. snd_soc_update_bits(codec, AIC32X4_MADC,
  577. AIC32X4_MADCEN, AIC32X4_MADCEN);
  578. /* Switch on BCLK_N Divider */
  579. snd_soc_update_bits(codec, AIC32X4_BCLKN,
  580. AIC32X4_BCLKEN, AIC32X4_BCLKEN);
  581. break;
  582. case SND_SOC_BIAS_PREPARE:
  583. break;
  584. case SND_SOC_BIAS_STANDBY:
  585. /* Switch off BCLK_N Divider */
  586. snd_soc_update_bits(codec, AIC32X4_BCLKN,
  587. AIC32X4_BCLKEN, 0);
  588. /* Switch off MADC Divider */
  589. snd_soc_update_bits(codec, AIC32X4_MADC,
  590. AIC32X4_MADCEN, 0);
  591. /* Switch off NADC Divider */
  592. snd_soc_update_bits(codec, AIC32X4_NADC,
  593. AIC32X4_NADCEN, 0);
  594. /* Switch off MDAC Divider */
  595. snd_soc_update_bits(codec, AIC32X4_MDAC,
  596. AIC32X4_MDACEN, 0);
  597. /* Switch off NDAC Divider */
  598. snd_soc_update_bits(codec, AIC32X4_NDAC,
  599. AIC32X4_NDACEN, 0);
  600. /* Switch off PLL */
  601. snd_soc_update_bits(codec, AIC32X4_PLLPR,
  602. AIC32X4_PLLEN, 0);
  603. /* Switch off master clock */
  604. clk_disable_unprepare(aic32x4->mclk);
  605. break;
  606. case SND_SOC_BIAS_OFF:
  607. break;
  608. }
  609. return 0;
  610. }
  611. #define AIC32X4_RATES SNDRV_PCM_RATE_8000_96000
  612. #define AIC32X4_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE \
  613. | SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S32_LE)
  614. static const struct snd_soc_dai_ops aic32x4_ops = {
  615. .hw_params = aic32x4_hw_params,
  616. .digital_mute = aic32x4_mute,
  617. .set_fmt = aic32x4_set_dai_fmt,
  618. .set_sysclk = aic32x4_set_dai_sysclk,
  619. };
  620. static struct snd_soc_dai_driver aic32x4_dai = {
  621. .name = "tlv320aic32x4-hifi",
  622. .playback = {
  623. .stream_name = "Playback",
  624. .channels_min = 1,
  625. .channels_max = 2,
  626. .rates = AIC32X4_RATES,
  627. .formats = AIC32X4_FORMATS,},
  628. .capture = {
  629. .stream_name = "Capture",
  630. .channels_min = 1,
  631. .channels_max = 2,
  632. .rates = AIC32X4_RATES,
  633. .formats = AIC32X4_FORMATS,},
  634. .ops = &aic32x4_ops,
  635. .symmetric_rates = 1,
  636. };
  637. static int aic32x4_codec_probe(struct snd_soc_codec *codec)
  638. {
  639. struct aic32x4_priv *aic32x4 = snd_soc_codec_get_drvdata(codec);
  640. u32 tmp_reg;
  641. if (gpio_is_valid(aic32x4->rstn_gpio)) {
  642. ndelay(10);
  643. gpio_set_value(aic32x4->rstn_gpio, 1);
  644. }
  645. snd_soc_write(codec, AIC32X4_RESET, 0x01);
  646. /* Power platform configuration */
  647. if (aic32x4->power_cfg & AIC32X4_PWR_MICBIAS_2075_LDOIN) {
  648. snd_soc_write(codec, AIC32X4_MICBIAS, AIC32X4_MICBIAS_LDOIN |
  649. AIC32X4_MICBIAS_2075V);
  650. }
  651. if (aic32x4->power_cfg & AIC32X4_PWR_AVDD_DVDD_WEAK_DISABLE)
  652. snd_soc_write(codec, AIC32X4_PWRCFG, AIC32X4_AVDDWEAKDISABLE);
  653. tmp_reg = (aic32x4->power_cfg & AIC32X4_PWR_AIC32X4_LDO_ENABLE) ?
  654. AIC32X4_LDOCTLEN : 0;
  655. snd_soc_write(codec, AIC32X4_LDOCTL, tmp_reg);
  656. tmp_reg = snd_soc_read(codec, AIC32X4_CMMODE);
  657. if (aic32x4->power_cfg & AIC32X4_PWR_CMMODE_LDOIN_RANGE_18_36)
  658. tmp_reg |= AIC32X4_LDOIN_18_36;
  659. if (aic32x4->power_cfg & AIC32X4_PWR_CMMODE_HP_LDOIN_POWERED)
  660. tmp_reg |= AIC32X4_LDOIN2HP;
  661. snd_soc_write(codec, AIC32X4_CMMODE, tmp_reg);
  662. /* Mic PGA routing */
  663. if (aic32x4->micpga_routing & AIC32X4_MICPGA_ROUTE_LMIC_IN2R_10K)
  664. snd_soc_write(codec, AIC32X4_LMICPGANIN,
  665. AIC32X4_LMICPGANIN_IN2R_10K);
  666. else
  667. snd_soc_write(codec, AIC32X4_LMICPGANIN,
  668. AIC32X4_LMICPGANIN_CM1L_10K);
  669. if (aic32x4->micpga_routing & AIC32X4_MICPGA_ROUTE_RMIC_IN1L_10K)
  670. snd_soc_write(codec, AIC32X4_RMICPGANIN,
  671. AIC32X4_RMICPGANIN_IN1L_10K);
  672. else
  673. snd_soc_write(codec, AIC32X4_RMICPGANIN,
  674. AIC32X4_RMICPGANIN_CM1R_10K);
  675. /*
  676. * Workaround: for an unknown reason, the ADC needs to be powered up
  677. * and down for the first capture to work properly. It seems related to
  678. * a HW BUG or some kind of behavior not documented in the datasheet.
  679. */
  680. tmp_reg = snd_soc_read(codec, AIC32X4_ADCSETUP);
  681. snd_soc_write(codec, AIC32X4_ADCSETUP, tmp_reg |
  682. AIC32X4_LADC_EN | AIC32X4_RADC_EN);
  683. snd_soc_write(codec, AIC32X4_ADCSETUP, tmp_reg);
  684. return 0;
  685. }
  686. static struct snd_soc_codec_driver soc_codec_dev_aic32x4 = {
  687. .probe = aic32x4_codec_probe,
  688. .set_bias_level = aic32x4_set_bias_level,
  689. .suspend_bias_off = true,
  690. .component_driver = {
  691. .controls = aic32x4_snd_controls,
  692. .num_controls = ARRAY_SIZE(aic32x4_snd_controls),
  693. .dapm_widgets = aic32x4_dapm_widgets,
  694. .num_dapm_widgets = ARRAY_SIZE(aic32x4_dapm_widgets),
  695. .dapm_routes = aic32x4_dapm_routes,
  696. .num_dapm_routes = ARRAY_SIZE(aic32x4_dapm_routes),
  697. },
  698. };
  699. static int aic32x4_parse_dt(struct aic32x4_priv *aic32x4,
  700. struct device_node *np)
  701. {
  702. aic32x4->swapdacs = false;
  703. aic32x4->micpga_routing = 0;
  704. aic32x4->rstn_gpio = of_get_named_gpio(np, "reset-gpios", 0);
  705. return 0;
  706. }
  707. static void aic32x4_disable_regulators(struct aic32x4_priv *aic32x4)
  708. {
  709. regulator_disable(aic32x4->supply_iov);
  710. if (!IS_ERR(aic32x4->supply_ldo))
  711. regulator_disable(aic32x4->supply_ldo);
  712. if (!IS_ERR(aic32x4->supply_dv))
  713. regulator_disable(aic32x4->supply_dv);
  714. if (!IS_ERR(aic32x4->supply_av))
  715. regulator_disable(aic32x4->supply_av);
  716. }
  717. static int aic32x4_setup_regulators(struct device *dev,
  718. struct aic32x4_priv *aic32x4)
  719. {
  720. int ret = 0;
  721. aic32x4->supply_ldo = devm_regulator_get_optional(dev, "ldoin");
  722. aic32x4->supply_iov = devm_regulator_get(dev, "iov");
  723. aic32x4->supply_dv = devm_regulator_get_optional(dev, "dv");
  724. aic32x4->supply_av = devm_regulator_get_optional(dev, "av");
  725. /* Check if the regulator requirements are fulfilled */
  726. if (IS_ERR(aic32x4->supply_iov)) {
  727. dev_err(dev, "Missing supply 'iov'\n");
  728. return PTR_ERR(aic32x4->supply_iov);
  729. }
  730. if (IS_ERR(aic32x4->supply_ldo)) {
  731. if (PTR_ERR(aic32x4->supply_ldo) == -EPROBE_DEFER)
  732. return -EPROBE_DEFER;
  733. if (IS_ERR(aic32x4->supply_dv)) {
  734. dev_err(dev, "Missing supply 'dv' or 'ldoin'\n");
  735. return PTR_ERR(aic32x4->supply_dv);
  736. }
  737. if (IS_ERR(aic32x4->supply_av)) {
  738. dev_err(dev, "Missing supply 'av' or 'ldoin'\n");
  739. return PTR_ERR(aic32x4->supply_av);
  740. }
  741. } else {
  742. if (IS_ERR(aic32x4->supply_dv) &&
  743. PTR_ERR(aic32x4->supply_dv) == -EPROBE_DEFER)
  744. return -EPROBE_DEFER;
  745. if (IS_ERR(aic32x4->supply_av) &&
  746. PTR_ERR(aic32x4->supply_av) == -EPROBE_DEFER)
  747. return -EPROBE_DEFER;
  748. }
  749. ret = regulator_enable(aic32x4->supply_iov);
  750. if (ret) {
  751. dev_err(dev, "Failed to enable regulator iov\n");
  752. return ret;
  753. }
  754. if (!IS_ERR(aic32x4->supply_ldo)) {
  755. ret = regulator_enable(aic32x4->supply_ldo);
  756. if (ret) {
  757. dev_err(dev, "Failed to enable regulator ldo\n");
  758. goto error_ldo;
  759. }
  760. }
  761. if (!IS_ERR(aic32x4->supply_dv)) {
  762. ret = regulator_enable(aic32x4->supply_dv);
  763. if (ret) {
  764. dev_err(dev, "Failed to enable regulator dv\n");
  765. goto error_dv;
  766. }
  767. }
  768. if (!IS_ERR(aic32x4->supply_av)) {
  769. ret = regulator_enable(aic32x4->supply_av);
  770. if (ret) {
  771. dev_err(dev, "Failed to enable regulator av\n");
  772. goto error_av;
  773. }
  774. }
  775. if (!IS_ERR(aic32x4->supply_ldo) && IS_ERR(aic32x4->supply_av))
  776. aic32x4->power_cfg |= AIC32X4_PWR_AIC32X4_LDO_ENABLE;
  777. return 0;
  778. error_av:
  779. if (!IS_ERR(aic32x4->supply_dv))
  780. regulator_disable(aic32x4->supply_dv);
  781. error_dv:
  782. if (!IS_ERR(aic32x4->supply_ldo))
  783. regulator_disable(aic32x4->supply_ldo);
  784. error_ldo:
  785. regulator_disable(aic32x4->supply_iov);
  786. return ret;
  787. }
  788. int aic32x4_probe(struct device *dev, struct regmap *regmap)
  789. {
  790. struct aic32x4_priv *aic32x4;
  791. struct aic32x4_pdata *pdata = dev->platform_data;
  792. struct device_node *np = dev->of_node;
  793. int ret;
  794. if (IS_ERR(regmap))
  795. return PTR_ERR(regmap);
  796. aic32x4 = devm_kzalloc(dev, sizeof(struct aic32x4_priv),
  797. GFP_KERNEL);
  798. if (aic32x4 == NULL)
  799. return -ENOMEM;
  800. dev_set_drvdata(dev, aic32x4);
  801. if (pdata) {
  802. aic32x4->power_cfg = pdata->power_cfg;
  803. aic32x4->swapdacs = pdata->swapdacs;
  804. aic32x4->micpga_routing = pdata->micpga_routing;
  805. aic32x4->rstn_gpio = pdata->rstn_gpio;
  806. } else if (np) {
  807. ret = aic32x4_parse_dt(aic32x4, np);
  808. if (ret) {
  809. dev_err(dev, "Failed to parse DT node\n");
  810. return ret;
  811. }
  812. } else {
  813. aic32x4->power_cfg = 0;
  814. aic32x4->swapdacs = false;
  815. aic32x4->micpga_routing = 0;
  816. aic32x4->rstn_gpio = -1;
  817. }
  818. aic32x4->mclk = devm_clk_get(dev, "mclk");
  819. if (IS_ERR(aic32x4->mclk)) {
  820. dev_err(dev, "Failed getting the mclk. The current implementation does not support the usage of this codec without mclk\n");
  821. return PTR_ERR(aic32x4->mclk);
  822. }
  823. if (gpio_is_valid(aic32x4->rstn_gpio)) {
  824. ret = devm_gpio_request_one(dev, aic32x4->rstn_gpio,
  825. GPIOF_OUT_INIT_LOW, "tlv320aic32x4 rstn");
  826. if (ret != 0)
  827. return ret;
  828. }
  829. ret = aic32x4_setup_regulators(dev, aic32x4);
  830. if (ret) {
  831. dev_err(dev, "Failed to setup regulators\n");
  832. return ret;
  833. }
  834. ret = snd_soc_register_codec(dev,
  835. &soc_codec_dev_aic32x4, &aic32x4_dai, 1);
  836. if (ret) {
  837. dev_err(dev, "Failed to register codec\n");
  838. aic32x4_disable_regulators(aic32x4);
  839. return ret;
  840. }
  841. return 0;
  842. }
  843. EXPORT_SYMBOL(aic32x4_probe);
  844. int aic32x4_remove(struct device *dev)
  845. {
  846. struct aic32x4_priv *aic32x4 = dev_get_drvdata(dev);
  847. aic32x4_disable_regulators(aic32x4);
  848. snd_soc_unregister_codec(dev);
  849. return 0;
  850. }
  851. EXPORT_SYMBOL(aic32x4_remove);
  852. MODULE_DESCRIPTION("ASoC tlv320aic32x4 codec driver");
  853. MODULE_AUTHOR("Javier Martin <javier.martin@vista-silicon.com>");
  854. MODULE_LICENSE("GPL");