sgtl5000.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409
  1. /*
  2. * sgtl5000.c -- SGTL5000 ALSA SoC Audio driver
  3. *
  4. * Copyright 2010-2011 Freescale Semiconductor, Inc. All Rights Reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <linux/module.h>
  11. #include <linux/moduleparam.h>
  12. #include <linux/init.h>
  13. #include <linux/delay.h>
  14. #include <linux/slab.h>
  15. #include <linux/pm.h>
  16. #include <linux/i2c.h>
  17. #include <linux/clk.h>
  18. #include <linux/log2.h>
  19. #include <linux/regmap.h>
  20. #include <linux/regulator/driver.h>
  21. #include <linux/regulator/machine.h>
  22. #include <linux/regulator/consumer.h>
  23. #include <linux/of_device.h>
  24. #include <sound/core.h>
  25. #include <sound/tlv.h>
  26. #include <sound/pcm.h>
  27. #include <sound/pcm_params.h>
  28. #include <sound/soc.h>
  29. #include <sound/soc-dapm.h>
  30. #include <sound/initval.h>
  31. #include "sgtl5000.h"
  32. #define SGTL5000_DAP_REG_OFFSET 0x0100
  33. #define SGTL5000_MAX_REG_OFFSET 0x013A
  34. /* default value of sgtl5000 registers */
  35. static const struct reg_default sgtl5000_reg_defaults[] = {
  36. { SGTL5000_CHIP_DIG_POWER, 0x0000 },
  37. { SGTL5000_CHIP_I2S_CTRL, 0x0010 },
  38. { SGTL5000_CHIP_SSS_CTRL, 0x0010 },
  39. { SGTL5000_CHIP_ADCDAC_CTRL, 0x020c },
  40. { SGTL5000_CHIP_DAC_VOL, 0x3c3c },
  41. { SGTL5000_CHIP_PAD_STRENGTH, 0x015f },
  42. { SGTL5000_CHIP_ANA_ADC_CTRL, 0x0000 },
  43. { SGTL5000_CHIP_ANA_HP_CTRL, 0x1818 },
  44. { SGTL5000_CHIP_ANA_CTRL, 0x0111 },
  45. { SGTL5000_CHIP_REF_CTRL, 0x0000 },
  46. { SGTL5000_CHIP_MIC_CTRL, 0x0000 },
  47. { SGTL5000_CHIP_LINE_OUT_CTRL, 0x0000 },
  48. { SGTL5000_CHIP_LINE_OUT_VOL, 0x0404 },
  49. { SGTL5000_CHIP_PLL_CTRL, 0x5000 },
  50. { SGTL5000_CHIP_CLK_TOP_CTRL, 0x0000 },
  51. { SGTL5000_CHIP_ANA_STATUS, 0x0000 },
  52. { SGTL5000_CHIP_SHORT_CTRL, 0x0000 },
  53. { SGTL5000_CHIP_ANA_TEST2, 0x0000 },
  54. { SGTL5000_DAP_CTRL, 0x0000 },
  55. { SGTL5000_DAP_PEQ, 0x0000 },
  56. { SGTL5000_DAP_BASS_ENHANCE, 0x0040 },
  57. { SGTL5000_DAP_BASS_ENHANCE_CTRL, 0x051f },
  58. { SGTL5000_DAP_AUDIO_EQ, 0x0000 },
  59. { SGTL5000_DAP_SURROUND, 0x0040 },
  60. { SGTL5000_DAP_EQ_BASS_BAND0, 0x002f },
  61. { SGTL5000_DAP_EQ_BASS_BAND1, 0x002f },
  62. { SGTL5000_DAP_EQ_BASS_BAND2, 0x002f },
  63. { SGTL5000_DAP_EQ_BASS_BAND3, 0x002f },
  64. { SGTL5000_DAP_EQ_BASS_BAND4, 0x002f },
  65. { SGTL5000_DAP_MAIN_CHAN, 0x8000 },
  66. { SGTL5000_DAP_MIX_CHAN, 0x0000 },
  67. { SGTL5000_DAP_AVC_CTRL, 0x0510 },
  68. { SGTL5000_DAP_AVC_THRESHOLD, 0x1473 },
  69. { SGTL5000_DAP_AVC_ATTACK, 0x0028 },
  70. { SGTL5000_DAP_AVC_DECAY, 0x0050 },
  71. };
  72. /* regulator supplies for sgtl5000, VDDD is an optional external supply */
  73. enum sgtl5000_regulator_supplies {
  74. VDDA,
  75. VDDIO,
  76. VDDD,
  77. SGTL5000_SUPPLY_NUM
  78. };
  79. /* vddd is optional supply */
  80. static const char *supply_names[SGTL5000_SUPPLY_NUM] = {
  81. "VDDA",
  82. "VDDIO",
  83. "VDDD"
  84. };
  85. #define LDO_VOLTAGE 1200000
  86. #define LINREG_VDDD ((1600 - LDO_VOLTAGE / 1000) / 50)
  87. enum sgtl5000_micbias_resistor {
  88. SGTL5000_MICBIAS_OFF = 0,
  89. SGTL5000_MICBIAS_2K = 2,
  90. SGTL5000_MICBIAS_4K = 4,
  91. SGTL5000_MICBIAS_8K = 8,
  92. };
  93. /* sgtl5000 private structure in codec */
  94. struct sgtl5000_priv {
  95. int sysclk; /* sysclk rate */
  96. int master; /* i2s master or not */
  97. int fmt; /* i2s data format */
  98. struct regulator_bulk_data supplies[SGTL5000_SUPPLY_NUM];
  99. int num_supplies;
  100. struct regmap *regmap;
  101. struct clk *mclk;
  102. int revision;
  103. u8 micbias_resistor;
  104. u8 micbias_voltage;
  105. };
  106. /*
  107. * mic_bias power on/off share the same register bits with
  108. * output impedance of mic bias, when power on mic bias, we
  109. * need reclaim it to impedance value.
  110. * 0x0 = Powered off
  111. * 0x1 = 2Kohm
  112. * 0x2 = 4Kohm
  113. * 0x3 = 8Kohm
  114. */
  115. static int mic_bias_event(struct snd_soc_dapm_widget *w,
  116. struct snd_kcontrol *kcontrol, int event)
  117. {
  118. struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
  119. struct sgtl5000_priv *sgtl5000 = snd_soc_codec_get_drvdata(codec);
  120. switch (event) {
  121. case SND_SOC_DAPM_POST_PMU:
  122. /* change mic bias resistor */
  123. snd_soc_update_bits(codec, SGTL5000_CHIP_MIC_CTRL,
  124. SGTL5000_BIAS_R_MASK,
  125. sgtl5000->micbias_resistor << SGTL5000_BIAS_R_SHIFT);
  126. break;
  127. case SND_SOC_DAPM_PRE_PMD:
  128. snd_soc_update_bits(codec, SGTL5000_CHIP_MIC_CTRL,
  129. SGTL5000_BIAS_R_MASK, 0);
  130. break;
  131. }
  132. return 0;
  133. }
  134. /*
  135. * As manual described, ADC/DAC only works when VAG powerup,
  136. * So enabled VAG before ADC/DAC up.
  137. * In power down case, we need wait 400ms when vag fully ramped down.
  138. */
  139. static int power_vag_event(struct snd_soc_dapm_widget *w,
  140. struct snd_kcontrol *kcontrol, int event)
  141. {
  142. struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
  143. const u32 mask = SGTL5000_DAC_POWERUP | SGTL5000_ADC_POWERUP;
  144. switch (event) {
  145. case SND_SOC_DAPM_POST_PMU:
  146. snd_soc_update_bits(codec, SGTL5000_CHIP_ANA_POWER,
  147. SGTL5000_VAG_POWERUP, SGTL5000_VAG_POWERUP);
  148. msleep(400);
  149. break;
  150. case SND_SOC_DAPM_PRE_PMD:
  151. /*
  152. * Don't clear VAG_POWERUP, when both DAC and ADC are
  153. * operational to prevent inadvertently starving the
  154. * other one of them.
  155. */
  156. if ((snd_soc_read(codec, SGTL5000_CHIP_ANA_POWER) &
  157. mask) != mask) {
  158. snd_soc_update_bits(codec, SGTL5000_CHIP_ANA_POWER,
  159. SGTL5000_VAG_POWERUP, 0);
  160. msleep(400);
  161. }
  162. break;
  163. default:
  164. break;
  165. }
  166. return 0;
  167. }
  168. /* input sources for ADC */
  169. static const char *adc_mux_text[] = {
  170. "MIC_IN", "LINE_IN"
  171. };
  172. static SOC_ENUM_SINGLE_DECL(adc_enum,
  173. SGTL5000_CHIP_ANA_CTRL, 2,
  174. adc_mux_text);
  175. static const struct snd_kcontrol_new adc_mux =
  176. SOC_DAPM_ENUM("Capture Mux", adc_enum);
  177. /* input sources for DAC */
  178. static const char *dac_mux_text[] = {
  179. "DAC", "LINE_IN"
  180. };
  181. static SOC_ENUM_SINGLE_DECL(dac_enum,
  182. SGTL5000_CHIP_ANA_CTRL, 6,
  183. dac_mux_text);
  184. static const struct snd_kcontrol_new dac_mux =
  185. SOC_DAPM_ENUM("Headphone Mux", dac_enum);
  186. static const struct snd_soc_dapm_widget sgtl5000_dapm_widgets[] = {
  187. SND_SOC_DAPM_INPUT("LINE_IN"),
  188. SND_SOC_DAPM_INPUT("MIC_IN"),
  189. SND_SOC_DAPM_OUTPUT("HP_OUT"),
  190. SND_SOC_DAPM_OUTPUT("LINE_OUT"),
  191. SND_SOC_DAPM_SUPPLY("Mic Bias", SGTL5000_CHIP_MIC_CTRL, 8, 0,
  192. mic_bias_event,
  193. SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
  194. SND_SOC_DAPM_PGA("HP", SGTL5000_CHIP_ANA_POWER, 4, 0, NULL, 0),
  195. SND_SOC_DAPM_PGA("LO", SGTL5000_CHIP_ANA_POWER, 0, 0, NULL, 0),
  196. SND_SOC_DAPM_MUX("Capture Mux", SND_SOC_NOPM, 0, 0, &adc_mux),
  197. SND_SOC_DAPM_MUX("Headphone Mux", SND_SOC_NOPM, 0, 0, &dac_mux),
  198. /* aif for i2s input */
  199. SND_SOC_DAPM_AIF_IN("AIFIN", "Playback",
  200. 0, SGTL5000_CHIP_DIG_POWER,
  201. 0, 0),
  202. /* aif for i2s output */
  203. SND_SOC_DAPM_AIF_OUT("AIFOUT", "Capture",
  204. 0, SGTL5000_CHIP_DIG_POWER,
  205. 1, 0),
  206. SND_SOC_DAPM_ADC("ADC", "Capture", SGTL5000_CHIP_ANA_POWER, 1, 0),
  207. SND_SOC_DAPM_DAC("DAC", "Playback", SGTL5000_CHIP_ANA_POWER, 3, 0),
  208. SND_SOC_DAPM_PRE("VAG_POWER_PRE", power_vag_event),
  209. SND_SOC_DAPM_POST("VAG_POWER_POST", power_vag_event),
  210. };
  211. /* routes for sgtl5000 */
  212. static const struct snd_soc_dapm_route sgtl5000_dapm_routes[] = {
  213. {"Capture Mux", "LINE_IN", "LINE_IN"}, /* line_in --> adc_mux */
  214. {"Capture Mux", "MIC_IN", "MIC_IN"}, /* mic_in --> adc_mux */
  215. {"ADC", NULL, "Capture Mux"}, /* adc_mux --> adc */
  216. {"AIFOUT", NULL, "ADC"}, /* adc --> i2s_out */
  217. {"DAC", NULL, "AIFIN"}, /* i2s-->dac,skip audio mux */
  218. {"Headphone Mux", "DAC", "DAC"}, /* dac --> hp_mux */
  219. {"LO", NULL, "DAC"}, /* dac --> line_out */
  220. {"Headphone Mux", "LINE_IN", "LINE_IN"},/* line_in --> hp_mux */
  221. {"HP", NULL, "Headphone Mux"}, /* hp_mux --> hp */
  222. {"LINE_OUT", NULL, "LO"},
  223. {"HP_OUT", NULL, "HP"},
  224. };
  225. /* custom function to fetch info of PCM playback volume */
  226. static int dac_info_volsw(struct snd_kcontrol *kcontrol,
  227. struct snd_ctl_elem_info *uinfo)
  228. {
  229. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  230. uinfo->count = 2;
  231. uinfo->value.integer.min = 0;
  232. uinfo->value.integer.max = 0xfc - 0x3c;
  233. return 0;
  234. }
  235. /*
  236. * custom function to get of PCM playback volume
  237. *
  238. * dac volume register
  239. * 15-------------8-7--------------0
  240. * | R channel vol | L channel vol |
  241. * -------------------------------
  242. *
  243. * PCM volume with 0.5017 dB steps from 0 to -90 dB
  244. *
  245. * register values map to dB
  246. * 0x3B and less = Reserved
  247. * 0x3C = 0 dB
  248. * 0x3D = -0.5 dB
  249. * 0xF0 = -90 dB
  250. * 0xFC and greater = Muted
  251. *
  252. * register value map to userspace value
  253. *
  254. * register value 0x3c(0dB) 0xf0(-90dB)0xfc
  255. * ------------------------------
  256. * userspace value 0xc0 0
  257. */
  258. static int dac_get_volsw(struct snd_kcontrol *kcontrol,
  259. struct snd_ctl_elem_value *ucontrol)
  260. {
  261. struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
  262. int reg;
  263. int l;
  264. int r;
  265. reg = snd_soc_read(codec, SGTL5000_CHIP_DAC_VOL);
  266. /* get left channel volume */
  267. l = (reg & SGTL5000_DAC_VOL_LEFT_MASK) >> SGTL5000_DAC_VOL_LEFT_SHIFT;
  268. /* get right channel volume */
  269. r = (reg & SGTL5000_DAC_VOL_RIGHT_MASK) >> SGTL5000_DAC_VOL_RIGHT_SHIFT;
  270. /* make sure value fall in (0x3c,0xfc) */
  271. l = clamp(l, 0x3c, 0xfc);
  272. r = clamp(r, 0x3c, 0xfc);
  273. /* invert it and map to userspace value */
  274. l = 0xfc - l;
  275. r = 0xfc - r;
  276. ucontrol->value.integer.value[0] = l;
  277. ucontrol->value.integer.value[1] = r;
  278. return 0;
  279. }
  280. /*
  281. * custom function to put of PCM playback volume
  282. *
  283. * dac volume register
  284. * 15-------------8-7--------------0
  285. * | R channel vol | L channel vol |
  286. * -------------------------------
  287. *
  288. * PCM volume with 0.5017 dB steps from 0 to -90 dB
  289. *
  290. * register values map to dB
  291. * 0x3B and less = Reserved
  292. * 0x3C = 0 dB
  293. * 0x3D = -0.5 dB
  294. * 0xF0 = -90 dB
  295. * 0xFC and greater = Muted
  296. *
  297. * userspace value map to register value
  298. *
  299. * userspace value 0xc0 0
  300. * ------------------------------
  301. * register value 0x3c(0dB) 0xf0(-90dB)0xfc
  302. */
  303. static int dac_put_volsw(struct snd_kcontrol *kcontrol,
  304. struct snd_ctl_elem_value *ucontrol)
  305. {
  306. struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
  307. int reg;
  308. int l;
  309. int r;
  310. l = ucontrol->value.integer.value[0];
  311. r = ucontrol->value.integer.value[1];
  312. /* make sure userspace volume fall in (0, 0xfc-0x3c) */
  313. l = clamp(l, 0, 0xfc - 0x3c);
  314. r = clamp(r, 0, 0xfc - 0x3c);
  315. /* invert it, get the value can be set to register */
  316. l = 0xfc - l;
  317. r = 0xfc - r;
  318. /* shift to get the register value */
  319. reg = l << SGTL5000_DAC_VOL_LEFT_SHIFT |
  320. r << SGTL5000_DAC_VOL_RIGHT_SHIFT;
  321. snd_soc_write(codec, SGTL5000_CHIP_DAC_VOL, reg);
  322. return 0;
  323. }
  324. static const DECLARE_TLV_DB_SCALE(capture_6db_attenuate, -600, 600, 0);
  325. /* tlv for mic gain, 0db 20db 30db 40db */
  326. static const DECLARE_TLV_DB_RANGE(mic_gain_tlv,
  327. 0, 0, TLV_DB_SCALE_ITEM(0, 0, 0),
  328. 1, 3, TLV_DB_SCALE_ITEM(2000, 1000, 0)
  329. );
  330. /* tlv for hp volume, -51.5db to 12.0db, step .5db */
  331. static const DECLARE_TLV_DB_SCALE(headphone_volume, -5150, 50, 0);
  332. /* tlv for lineout volume, 31 steps of .5db each */
  333. static const DECLARE_TLV_DB_SCALE(lineout_volume, -1550, 50, 0);
  334. static const struct snd_kcontrol_new sgtl5000_snd_controls[] = {
  335. /* SOC_DOUBLE_S8_TLV with invert */
  336. {
  337. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  338. .name = "PCM Playback Volume",
  339. .access = SNDRV_CTL_ELEM_ACCESS_TLV_READ |
  340. SNDRV_CTL_ELEM_ACCESS_READWRITE,
  341. .info = dac_info_volsw,
  342. .get = dac_get_volsw,
  343. .put = dac_put_volsw,
  344. },
  345. SOC_DOUBLE("Capture Volume", SGTL5000_CHIP_ANA_ADC_CTRL, 0, 4, 0xf, 0),
  346. SOC_SINGLE_TLV("Capture Attenuate Switch (-6dB)",
  347. SGTL5000_CHIP_ANA_ADC_CTRL,
  348. 8, 1, 0, capture_6db_attenuate),
  349. SOC_SINGLE("Capture ZC Switch", SGTL5000_CHIP_ANA_CTRL, 1, 1, 0),
  350. SOC_DOUBLE_TLV("Headphone Playback Volume",
  351. SGTL5000_CHIP_ANA_HP_CTRL,
  352. 0, 8,
  353. 0x7f, 1,
  354. headphone_volume),
  355. SOC_SINGLE("Headphone Playback Switch", SGTL5000_CHIP_ANA_CTRL,
  356. 4, 1, 1),
  357. SOC_SINGLE("Headphone Playback ZC Switch", SGTL5000_CHIP_ANA_CTRL,
  358. 5, 1, 0),
  359. SOC_SINGLE_TLV("Mic Volume", SGTL5000_CHIP_MIC_CTRL,
  360. 0, 3, 0, mic_gain_tlv),
  361. SOC_DOUBLE_TLV("Lineout Playback Volume",
  362. SGTL5000_CHIP_LINE_OUT_VOL,
  363. SGTL5000_LINE_OUT_VOL_LEFT_SHIFT,
  364. SGTL5000_LINE_OUT_VOL_RIGHT_SHIFT,
  365. 0x1f, 1,
  366. lineout_volume),
  367. SOC_SINGLE("Lineout Playback Switch", SGTL5000_CHIP_ANA_CTRL, 8, 1, 1),
  368. };
  369. /* mute the codec used by alsa core */
  370. static int sgtl5000_digital_mute(struct snd_soc_dai *codec_dai, int mute)
  371. {
  372. struct snd_soc_codec *codec = codec_dai->codec;
  373. u16 adcdac_ctrl = SGTL5000_DAC_MUTE_LEFT | SGTL5000_DAC_MUTE_RIGHT;
  374. snd_soc_update_bits(codec, SGTL5000_CHIP_ADCDAC_CTRL,
  375. adcdac_ctrl, mute ? adcdac_ctrl : 0);
  376. return 0;
  377. }
  378. /* set codec format */
  379. static int sgtl5000_set_dai_fmt(struct snd_soc_dai *codec_dai, unsigned int fmt)
  380. {
  381. struct snd_soc_codec *codec = codec_dai->codec;
  382. struct sgtl5000_priv *sgtl5000 = snd_soc_codec_get_drvdata(codec);
  383. u16 i2sctl = 0;
  384. sgtl5000->master = 0;
  385. /*
  386. * i2s clock and frame master setting.
  387. * ONLY support:
  388. * - clock and frame slave,
  389. * - clock and frame master
  390. */
  391. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  392. case SND_SOC_DAIFMT_CBS_CFS:
  393. break;
  394. case SND_SOC_DAIFMT_CBM_CFM:
  395. i2sctl |= SGTL5000_I2S_MASTER;
  396. sgtl5000->master = 1;
  397. break;
  398. default:
  399. return -EINVAL;
  400. }
  401. /* setting i2s data format */
  402. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  403. case SND_SOC_DAIFMT_DSP_A:
  404. i2sctl |= SGTL5000_I2S_MODE_PCM << SGTL5000_I2S_MODE_SHIFT;
  405. break;
  406. case SND_SOC_DAIFMT_DSP_B:
  407. i2sctl |= SGTL5000_I2S_MODE_PCM << SGTL5000_I2S_MODE_SHIFT;
  408. i2sctl |= SGTL5000_I2S_LRALIGN;
  409. break;
  410. case SND_SOC_DAIFMT_I2S:
  411. i2sctl |= SGTL5000_I2S_MODE_I2S_LJ << SGTL5000_I2S_MODE_SHIFT;
  412. break;
  413. case SND_SOC_DAIFMT_RIGHT_J:
  414. i2sctl |= SGTL5000_I2S_MODE_RJ << SGTL5000_I2S_MODE_SHIFT;
  415. i2sctl |= SGTL5000_I2S_LRPOL;
  416. break;
  417. case SND_SOC_DAIFMT_LEFT_J:
  418. i2sctl |= SGTL5000_I2S_MODE_I2S_LJ << SGTL5000_I2S_MODE_SHIFT;
  419. i2sctl |= SGTL5000_I2S_LRALIGN;
  420. break;
  421. default:
  422. return -EINVAL;
  423. }
  424. sgtl5000->fmt = fmt & SND_SOC_DAIFMT_FORMAT_MASK;
  425. /* Clock inversion */
  426. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  427. case SND_SOC_DAIFMT_NB_NF:
  428. break;
  429. case SND_SOC_DAIFMT_IB_NF:
  430. i2sctl |= SGTL5000_I2S_SCLK_INV;
  431. break;
  432. default:
  433. return -EINVAL;
  434. }
  435. snd_soc_write(codec, SGTL5000_CHIP_I2S_CTRL, i2sctl);
  436. return 0;
  437. }
  438. /* set codec sysclk */
  439. static int sgtl5000_set_dai_sysclk(struct snd_soc_dai *codec_dai,
  440. int clk_id, unsigned int freq, int dir)
  441. {
  442. struct snd_soc_codec *codec = codec_dai->codec;
  443. struct sgtl5000_priv *sgtl5000 = snd_soc_codec_get_drvdata(codec);
  444. switch (clk_id) {
  445. case SGTL5000_SYSCLK:
  446. sgtl5000->sysclk = freq;
  447. break;
  448. default:
  449. return -EINVAL;
  450. }
  451. return 0;
  452. }
  453. /*
  454. * set clock according to i2s frame clock,
  455. * sgtl5000 provides 2 clock sources:
  456. * 1. sys_mclk: sample freq can only be configured to
  457. * 1/256, 1/384, 1/512 of sys_mclk.
  458. * 2. pll: can derive any audio clocks.
  459. *
  460. * clock setting rules:
  461. * 1. in slave mode, only sys_mclk can be used
  462. * 2. as constraint by sys_mclk, sample freq should be set to 32 kHz, 44.1 kHz
  463. * and above.
  464. * 3. usage of sys_mclk is preferred over pll to save power.
  465. */
  466. static int sgtl5000_set_clock(struct snd_soc_codec *codec, int frame_rate)
  467. {
  468. struct sgtl5000_priv *sgtl5000 = snd_soc_codec_get_drvdata(codec);
  469. int clk_ctl = 0;
  470. int sys_fs; /* sample freq */
  471. /*
  472. * sample freq should be divided by frame clock,
  473. * if frame clock is lower than 44.1 kHz, sample freq should be set to
  474. * 32 kHz or 44.1 kHz.
  475. */
  476. switch (frame_rate) {
  477. case 8000:
  478. case 16000:
  479. sys_fs = 32000;
  480. break;
  481. case 11025:
  482. case 22050:
  483. sys_fs = 44100;
  484. break;
  485. default:
  486. sys_fs = frame_rate;
  487. break;
  488. }
  489. /* set divided factor of frame clock */
  490. switch (sys_fs / frame_rate) {
  491. case 4:
  492. clk_ctl |= SGTL5000_RATE_MODE_DIV_4 << SGTL5000_RATE_MODE_SHIFT;
  493. break;
  494. case 2:
  495. clk_ctl |= SGTL5000_RATE_MODE_DIV_2 << SGTL5000_RATE_MODE_SHIFT;
  496. break;
  497. case 1:
  498. clk_ctl |= SGTL5000_RATE_MODE_DIV_1 << SGTL5000_RATE_MODE_SHIFT;
  499. break;
  500. default:
  501. return -EINVAL;
  502. }
  503. /* set the sys_fs according to frame rate */
  504. switch (sys_fs) {
  505. case 32000:
  506. clk_ctl |= SGTL5000_SYS_FS_32k << SGTL5000_SYS_FS_SHIFT;
  507. break;
  508. case 44100:
  509. clk_ctl |= SGTL5000_SYS_FS_44_1k << SGTL5000_SYS_FS_SHIFT;
  510. break;
  511. case 48000:
  512. clk_ctl |= SGTL5000_SYS_FS_48k << SGTL5000_SYS_FS_SHIFT;
  513. break;
  514. case 96000:
  515. clk_ctl |= SGTL5000_SYS_FS_96k << SGTL5000_SYS_FS_SHIFT;
  516. break;
  517. default:
  518. dev_err(codec->dev, "frame rate %d not supported\n",
  519. frame_rate);
  520. return -EINVAL;
  521. }
  522. /*
  523. * calculate the divider of mclk/sample_freq,
  524. * factor of freq = 96 kHz can only be 256, since mclk is in the range
  525. * of 8 MHz - 27 MHz
  526. */
  527. switch (sgtl5000->sysclk / frame_rate) {
  528. case 256:
  529. clk_ctl |= SGTL5000_MCLK_FREQ_256FS <<
  530. SGTL5000_MCLK_FREQ_SHIFT;
  531. break;
  532. case 384:
  533. clk_ctl |= SGTL5000_MCLK_FREQ_384FS <<
  534. SGTL5000_MCLK_FREQ_SHIFT;
  535. break;
  536. case 512:
  537. clk_ctl |= SGTL5000_MCLK_FREQ_512FS <<
  538. SGTL5000_MCLK_FREQ_SHIFT;
  539. break;
  540. default:
  541. /* if mclk does not satisfy the divider, use pll */
  542. if (sgtl5000->master) {
  543. clk_ctl |= SGTL5000_MCLK_FREQ_PLL <<
  544. SGTL5000_MCLK_FREQ_SHIFT;
  545. } else {
  546. dev_err(codec->dev,
  547. "PLL not supported in slave mode\n");
  548. dev_err(codec->dev, "%d ratio is not supported. "
  549. "SYS_MCLK needs to be 256, 384 or 512 * fs\n",
  550. sgtl5000->sysclk / frame_rate);
  551. return -EINVAL;
  552. }
  553. }
  554. /* if using pll, please check manual 6.4.2 for detail */
  555. if ((clk_ctl & SGTL5000_MCLK_FREQ_MASK) == SGTL5000_MCLK_FREQ_PLL) {
  556. u64 out, t;
  557. int div2;
  558. int pll_ctl;
  559. unsigned int in, int_div, frac_div;
  560. if (sgtl5000->sysclk > 17000000) {
  561. div2 = 1;
  562. in = sgtl5000->sysclk / 2;
  563. } else {
  564. div2 = 0;
  565. in = sgtl5000->sysclk;
  566. }
  567. if (sys_fs == 44100)
  568. out = 180633600;
  569. else
  570. out = 196608000;
  571. t = do_div(out, in);
  572. int_div = out;
  573. t *= 2048;
  574. do_div(t, in);
  575. frac_div = t;
  576. pll_ctl = int_div << SGTL5000_PLL_INT_DIV_SHIFT |
  577. frac_div << SGTL5000_PLL_FRAC_DIV_SHIFT;
  578. snd_soc_write(codec, SGTL5000_CHIP_PLL_CTRL, pll_ctl);
  579. if (div2)
  580. snd_soc_update_bits(codec,
  581. SGTL5000_CHIP_CLK_TOP_CTRL,
  582. SGTL5000_INPUT_FREQ_DIV2,
  583. SGTL5000_INPUT_FREQ_DIV2);
  584. else
  585. snd_soc_update_bits(codec,
  586. SGTL5000_CHIP_CLK_TOP_CTRL,
  587. SGTL5000_INPUT_FREQ_DIV2,
  588. 0);
  589. /* power up pll */
  590. snd_soc_update_bits(codec, SGTL5000_CHIP_ANA_POWER,
  591. SGTL5000_PLL_POWERUP | SGTL5000_VCOAMP_POWERUP,
  592. SGTL5000_PLL_POWERUP | SGTL5000_VCOAMP_POWERUP);
  593. /* if using pll, clk_ctrl must be set after pll power up */
  594. snd_soc_write(codec, SGTL5000_CHIP_CLK_CTRL, clk_ctl);
  595. } else {
  596. /* otherwise, clk_ctrl must be set before pll power down */
  597. snd_soc_write(codec, SGTL5000_CHIP_CLK_CTRL, clk_ctl);
  598. /* power down pll */
  599. snd_soc_update_bits(codec, SGTL5000_CHIP_ANA_POWER,
  600. SGTL5000_PLL_POWERUP | SGTL5000_VCOAMP_POWERUP,
  601. 0);
  602. }
  603. return 0;
  604. }
  605. /*
  606. * Set PCM DAI bit size and sample rate.
  607. * input: params_rate, params_fmt
  608. */
  609. static int sgtl5000_pcm_hw_params(struct snd_pcm_substream *substream,
  610. struct snd_pcm_hw_params *params,
  611. struct snd_soc_dai *dai)
  612. {
  613. struct snd_soc_codec *codec = dai->codec;
  614. struct sgtl5000_priv *sgtl5000 = snd_soc_codec_get_drvdata(codec);
  615. int channels = params_channels(params);
  616. int i2s_ctl = 0;
  617. int stereo;
  618. int ret;
  619. /* sysclk should already set */
  620. if (!sgtl5000->sysclk) {
  621. dev_err(codec->dev, "%s: set sysclk first!\n", __func__);
  622. return -EFAULT;
  623. }
  624. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  625. stereo = SGTL5000_DAC_STEREO;
  626. else
  627. stereo = SGTL5000_ADC_STEREO;
  628. /* set mono to save power */
  629. snd_soc_update_bits(codec, SGTL5000_CHIP_ANA_POWER, stereo,
  630. channels == 1 ? 0 : stereo);
  631. /* set codec clock base on lrclk */
  632. ret = sgtl5000_set_clock(codec, params_rate(params));
  633. if (ret)
  634. return ret;
  635. /* set i2s data format */
  636. switch (params_width(params)) {
  637. case 16:
  638. if (sgtl5000->fmt == SND_SOC_DAIFMT_RIGHT_J)
  639. return -EINVAL;
  640. i2s_ctl |= SGTL5000_I2S_DLEN_16 << SGTL5000_I2S_DLEN_SHIFT;
  641. i2s_ctl |= SGTL5000_I2S_SCLKFREQ_32FS <<
  642. SGTL5000_I2S_SCLKFREQ_SHIFT;
  643. break;
  644. case 20:
  645. i2s_ctl |= SGTL5000_I2S_DLEN_20 << SGTL5000_I2S_DLEN_SHIFT;
  646. i2s_ctl |= SGTL5000_I2S_SCLKFREQ_64FS <<
  647. SGTL5000_I2S_SCLKFREQ_SHIFT;
  648. break;
  649. case 24:
  650. i2s_ctl |= SGTL5000_I2S_DLEN_24 << SGTL5000_I2S_DLEN_SHIFT;
  651. i2s_ctl |= SGTL5000_I2S_SCLKFREQ_64FS <<
  652. SGTL5000_I2S_SCLKFREQ_SHIFT;
  653. break;
  654. case 32:
  655. if (sgtl5000->fmt == SND_SOC_DAIFMT_RIGHT_J)
  656. return -EINVAL;
  657. i2s_ctl |= SGTL5000_I2S_DLEN_32 << SGTL5000_I2S_DLEN_SHIFT;
  658. i2s_ctl |= SGTL5000_I2S_SCLKFREQ_64FS <<
  659. SGTL5000_I2S_SCLKFREQ_SHIFT;
  660. break;
  661. default:
  662. return -EINVAL;
  663. }
  664. snd_soc_update_bits(codec, SGTL5000_CHIP_I2S_CTRL,
  665. SGTL5000_I2S_DLEN_MASK | SGTL5000_I2S_SCLKFREQ_MASK,
  666. i2s_ctl);
  667. return 0;
  668. }
  669. /*
  670. * set dac bias
  671. * common state changes:
  672. * startup:
  673. * off --> standby --> prepare --> on
  674. * standby --> prepare --> on
  675. *
  676. * stop:
  677. * on --> prepare --> standby
  678. */
  679. static int sgtl5000_set_bias_level(struct snd_soc_codec *codec,
  680. enum snd_soc_bias_level level)
  681. {
  682. switch (level) {
  683. case SND_SOC_BIAS_ON:
  684. case SND_SOC_BIAS_PREPARE:
  685. case SND_SOC_BIAS_STANDBY:
  686. snd_soc_update_bits(codec, SGTL5000_CHIP_ANA_POWER,
  687. SGTL5000_REFTOP_POWERUP,
  688. SGTL5000_REFTOP_POWERUP);
  689. break;
  690. case SND_SOC_BIAS_OFF:
  691. snd_soc_update_bits(codec, SGTL5000_CHIP_ANA_POWER,
  692. SGTL5000_REFTOP_POWERUP, 0);
  693. break;
  694. }
  695. return 0;
  696. }
  697. #define SGTL5000_FORMATS (SNDRV_PCM_FMTBIT_S16_LE |\
  698. SNDRV_PCM_FMTBIT_S20_3LE |\
  699. SNDRV_PCM_FMTBIT_S24_LE |\
  700. SNDRV_PCM_FMTBIT_S32_LE)
  701. static const struct snd_soc_dai_ops sgtl5000_ops = {
  702. .hw_params = sgtl5000_pcm_hw_params,
  703. .digital_mute = sgtl5000_digital_mute,
  704. .set_fmt = sgtl5000_set_dai_fmt,
  705. .set_sysclk = sgtl5000_set_dai_sysclk,
  706. };
  707. static struct snd_soc_dai_driver sgtl5000_dai = {
  708. .name = "sgtl5000",
  709. .playback = {
  710. .stream_name = "Playback",
  711. .channels_min = 1,
  712. .channels_max = 2,
  713. /*
  714. * only support 8~48K + 96K,
  715. * TODO modify hw_param to support more
  716. */
  717. .rates = SNDRV_PCM_RATE_8000_48000 | SNDRV_PCM_RATE_96000,
  718. .formats = SGTL5000_FORMATS,
  719. },
  720. .capture = {
  721. .stream_name = "Capture",
  722. .channels_min = 1,
  723. .channels_max = 2,
  724. .rates = SNDRV_PCM_RATE_8000_48000 | SNDRV_PCM_RATE_96000,
  725. .formats = SGTL5000_FORMATS,
  726. },
  727. .ops = &sgtl5000_ops,
  728. .symmetric_rates = 1,
  729. };
  730. static bool sgtl5000_volatile(struct device *dev, unsigned int reg)
  731. {
  732. switch (reg) {
  733. case SGTL5000_CHIP_ID:
  734. case SGTL5000_CHIP_ADCDAC_CTRL:
  735. case SGTL5000_CHIP_ANA_STATUS:
  736. return true;
  737. }
  738. return false;
  739. }
  740. static bool sgtl5000_readable(struct device *dev, unsigned int reg)
  741. {
  742. switch (reg) {
  743. case SGTL5000_CHIP_ID:
  744. case SGTL5000_CHIP_DIG_POWER:
  745. case SGTL5000_CHIP_CLK_CTRL:
  746. case SGTL5000_CHIP_I2S_CTRL:
  747. case SGTL5000_CHIP_SSS_CTRL:
  748. case SGTL5000_CHIP_ADCDAC_CTRL:
  749. case SGTL5000_CHIP_DAC_VOL:
  750. case SGTL5000_CHIP_PAD_STRENGTH:
  751. case SGTL5000_CHIP_ANA_ADC_CTRL:
  752. case SGTL5000_CHIP_ANA_HP_CTRL:
  753. case SGTL5000_CHIP_ANA_CTRL:
  754. case SGTL5000_CHIP_LINREG_CTRL:
  755. case SGTL5000_CHIP_REF_CTRL:
  756. case SGTL5000_CHIP_MIC_CTRL:
  757. case SGTL5000_CHIP_LINE_OUT_CTRL:
  758. case SGTL5000_CHIP_LINE_OUT_VOL:
  759. case SGTL5000_CHIP_ANA_POWER:
  760. case SGTL5000_CHIP_PLL_CTRL:
  761. case SGTL5000_CHIP_CLK_TOP_CTRL:
  762. case SGTL5000_CHIP_ANA_STATUS:
  763. case SGTL5000_CHIP_SHORT_CTRL:
  764. case SGTL5000_CHIP_ANA_TEST2:
  765. case SGTL5000_DAP_CTRL:
  766. case SGTL5000_DAP_PEQ:
  767. case SGTL5000_DAP_BASS_ENHANCE:
  768. case SGTL5000_DAP_BASS_ENHANCE_CTRL:
  769. case SGTL5000_DAP_AUDIO_EQ:
  770. case SGTL5000_DAP_SURROUND:
  771. case SGTL5000_DAP_FLT_COEF_ACCESS:
  772. case SGTL5000_DAP_COEF_WR_B0_MSB:
  773. case SGTL5000_DAP_COEF_WR_B0_LSB:
  774. case SGTL5000_DAP_EQ_BASS_BAND0:
  775. case SGTL5000_DAP_EQ_BASS_BAND1:
  776. case SGTL5000_DAP_EQ_BASS_BAND2:
  777. case SGTL5000_DAP_EQ_BASS_BAND3:
  778. case SGTL5000_DAP_EQ_BASS_BAND4:
  779. case SGTL5000_DAP_MAIN_CHAN:
  780. case SGTL5000_DAP_MIX_CHAN:
  781. case SGTL5000_DAP_AVC_CTRL:
  782. case SGTL5000_DAP_AVC_THRESHOLD:
  783. case SGTL5000_DAP_AVC_ATTACK:
  784. case SGTL5000_DAP_AVC_DECAY:
  785. case SGTL5000_DAP_COEF_WR_B1_MSB:
  786. case SGTL5000_DAP_COEF_WR_B1_LSB:
  787. case SGTL5000_DAP_COEF_WR_B2_MSB:
  788. case SGTL5000_DAP_COEF_WR_B2_LSB:
  789. case SGTL5000_DAP_COEF_WR_A1_MSB:
  790. case SGTL5000_DAP_COEF_WR_A1_LSB:
  791. case SGTL5000_DAP_COEF_WR_A2_MSB:
  792. case SGTL5000_DAP_COEF_WR_A2_LSB:
  793. return true;
  794. default:
  795. return false;
  796. }
  797. }
  798. /*
  799. * This precalculated table contains all (vag_val * 100 / lo_calcntrl) results
  800. * to select an appropriate lo_vol_* in SGTL5000_CHIP_LINE_OUT_VOL
  801. * The calculatation was done for all possible register values which
  802. * is the array index and the following formula: 10^((idx−15)/40) * 100
  803. */
  804. static const u8 vol_quot_table[] = {
  805. 42, 45, 47, 50, 53, 56, 60, 63,
  806. 67, 71, 75, 79, 84, 89, 94, 100,
  807. 106, 112, 119, 126, 133, 141, 150, 158,
  808. 168, 178, 188, 200, 211, 224, 237, 251
  809. };
  810. /*
  811. * sgtl5000 has 3 internal power supplies:
  812. * 1. VAG, normally set to vdda/2
  813. * 2. charge pump, set to different value
  814. * according to voltage of vdda and vddio
  815. * 3. line out VAG, normally set to vddio/2
  816. *
  817. * and should be set according to:
  818. * 1. vddd provided by external or not
  819. * 2. vdda and vddio voltage value. > 3.1v or not
  820. */
  821. static int sgtl5000_set_power_regs(struct snd_soc_codec *codec)
  822. {
  823. int vddd;
  824. int vdda;
  825. int vddio;
  826. u16 ana_pwr;
  827. u16 lreg_ctrl;
  828. int vag;
  829. int lo_vag;
  830. int vol_quot;
  831. int lo_vol;
  832. size_t i;
  833. struct sgtl5000_priv *sgtl5000 = snd_soc_codec_get_drvdata(codec);
  834. vdda = regulator_get_voltage(sgtl5000->supplies[VDDA].consumer);
  835. vddio = regulator_get_voltage(sgtl5000->supplies[VDDIO].consumer);
  836. vddd = (sgtl5000->num_supplies > VDDD)
  837. ? regulator_get_voltage(sgtl5000->supplies[VDDD].consumer)
  838. : LDO_VOLTAGE;
  839. vdda = vdda / 1000;
  840. vddio = vddio / 1000;
  841. vddd = vddd / 1000;
  842. if (vdda <= 0 || vddio <= 0 || vddd < 0) {
  843. dev_err(codec->dev, "regulator voltage not set correctly\n");
  844. return -EINVAL;
  845. }
  846. /* according to datasheet, maximum voltage of supplies */
  847. if (vdda > 3600 || vddio > 3600 || vddd > 1980) {
  848. dev_err(codec->dev,
  849. "exceed max voltage vdda %dmV vddio %dmV vddd %dmV\n",
  850. vdda, vddio, vddd);
  851. return -EINVAL;
  852. }
  853. /* reset value */
  854. ana_pwr = snd_soc_read(codec, SGTL5000_CHIP_ANA_POWER);
  855. ana_pwr |= SGTL5000_DAC_STEREO |
  856. SGTL5000_ADC_STEREO |
  857. SGTL5000_REFTOP_POWERUP;
  858. lreg_ctrl = snd_soc_read(codec, SGTL5000_CHIP_LINREG_CTRL);
  859. if (vddio < 3100 && vdda < 3100) {
  860. /* enable internal oscillator used for charge pump */
  861. snd_soc_update_bits(codec, SGTL5000_CHIP_CLK_TOP_CTRL,
  862. SGTL5000_INT_OSC_EN,
  863. SGTL5000_INT_OSC_EN);
  864. /* Enable VDDC charge pump */
  865. ana_pwr |= SGTL5000_VDDC_CHRGPMP_POWERUP;
  866. } else if (vddio >= 3100 && vdda >= 3100) {
  867. ana_pwr &= ~SGTL5000_VDDC_CHRGPMP_POWERUP;
  868. /* VDDC use VDDIO rail */
  869. lreg_ctrl |= SGTL5000_VDDC_ASSN_OVRD;
  870. lreg_ctrl |= SGTL5000_VDDC_MAN_ASSN_VDDIO <<
  871. SGTL5000_VDDC_MAN_ASSN_SHIFT;
  872. }
  873. snd_soc_write(codec, SGTL5000_CHIP_LINREG_CTRL, lreg_ctrl);
  874. snd_soc_write(codec, SGTL5000_CHIP_ANA_POWER, ana_pwr);
  875. /*
  876. * set ADC/DAC VAG to vdda / 2,
  877. * should stay in range (0.8v, 1.575v)
  878. */
  879. vag = vdda / 2;
  880. if (vag <= SGTL5000_ANA_GND_BASE)
  881. vag = 0;
  882. else if (vag >= SGTL5000_ANA_GND_BASE + SGTL5000_ANA_GND_STP *
  883. (SGTL5000_ANA_GND_MASK >> SGTL5000_ANA_GND_SHIFT))
  884. vag = SGTL5000_ANA_GND_MASK >> SGTL5000_ANA_GND_SHIFT;
  885. else
  886. vag = (vag - SGTL5000_ANA_GND_BASE) / SGTL5000_ANA_GND_STP;
  887. snd_soc_update_bits(codec, SGTL5000_CHIP_REF_CTRL,
  888. SGTL5000_ANA_GND_MASK, vag << SGTL5000_ANA_GND_SHIFT);
  889. /* set line out VAG to vddio / 2, in range (0.8v, 1.675v) */
  890. lo_vag = vddio / 2;
  891. if (lo_vag <= SGTL5000_LINE_OUT_GND_BASE)
  892. lo_vag = 0;
  893. else if (lo_vag >= SGTL5000_LINE_OUT_GND_BASE +
  894. SGTL5000_LINE_OUT_GND_STP * SGTL5000_LINE_OUT_GND_MAX)
  895. lo_vag = SGTL5000_LINE_OUT_GND_MAX;
  896. else
  897. lo_vag = (lo_vag - SGTL5000_LINE_OUT_GND_BASE) /
  898. SGTL5000_LINE_OUT_GND_STP;
  899. snd_soc_update_bits(codec, SGTL5000_CHIP_LINE_OUT_CTRL,
  900. SGTL5000_LINE_OUT_CURRENT_MASK |
  901. SGTL5000_LINE_OUT_GND_MASK,
  902. lo_vag << SGTL5000_LINE_OUT_GND_SHIFT |
  903. SGTL5000_LINE_OUT_CURRENT_360u <<
  904. SGTL5000_LINE_OUT_CURRENT_SHIFT);
  905. /*
  906. * Set lineout output level in range (0..31)
  907. * the same value is used for right and left channel
  908. *
  909. * Searching for a suitable index solving this formula:
  910. * idx = 40 * log10(vag_val / lo_cagcntrl) + 15
  911. */
  912. vol_quot = (vag * 100) / lo_vag;
  913. lo_vol = 0;
  914. for (i = 0; i < ARRAY_SIZE(vol_quot_table); i++) {
  915. if (vol_quot >= vol_quot_table[i])
  916. lo_vol = i;
  917. else
  918. break;
  919. }
  920. snd_soc_update_bits(codec, SGTL5000_CHIP_LINE_OUT_VOL,
  921. SGTL5000_LINE_OUT_VOL_RIGHT_MASK |
  922. SGTL5000_LINE_OUT_VOL_LEFT_MASK,
  923. lo_vol << SGTL5000_LINE_OUT_VOL_RIGHT_SHIFT |
  924. lo_vol << SGTL5000_LINE_OUT_VOL_LEFT_SHIFT);
  925. return 0;
  926. }
  927. static int sgtl5000_enable_regulators(struct i2c_client *client)
  928. {
  929. int ret;
  930. int i;
  931. int external_vddd = 0;
  932. struct regulator *vddd;
  933. struct sgtl5000_priv *sgtl5000 = i2c_get_clientdata(client);
  934. for (i = 0; i < ARRAY_SIZE(sgtl5000->supplies); i++)
  935. sgtl5000->supplies[i].supply = supply_names[i];
  936. vddd = regulator_get_optional(&client->dev, "VDDD");
  937. if (IS_ERR(vddd)) {
  938. /* See if it's just not registered yet */
  939. if (PTR_ERR(vddd) == -EPROBE_DEFER)
  940. return -EPROBE_DEFER;
  941. } else {
  942. external_vddd = 1;
  943. regulator_put(vddd);
  944. }
  945. sgtl5000->num_supplies = ARRAY_SIZE(sgtl5000->supplies)
  946. - 1 + external_vddd;
  947. ret = regulator_bulk_get(&client->dev, sgtl5000->num_supplies,
  948. sgtl5000->supplies);
  949. if (ret)
  950. return ret;
  951. ret = regulator_bulk_enable(sgtl5000->num_supplies,
  952. sgtl5000->supplies);
  953. if (!ret)
  954. usleep_range(10, 20);
  955. else
  956. regulator_bulk_free(sgtl5000->num_supplies,
  957. sgtl5000->supplies);
  958. return ret;
  959. }
  960. static int sgtl5000_probe(struct snd_soc_codec *codec)
  961. {
  962. int ret;
  963. struct sgtl5000_priv *sgtl5000 = snd_soc_codec_get_drvdata(codec);
  964. /* power up sgtl5000 */
  965. ret = sgtl5000_set_power_regs(codec);
  966. if (ret)
  967. goto err;
  968. /* enable small pop, introduce 400ms delay in turning off */
  969. snd_soc_update_bits(codec, SGTL5000_CHIP_REF_CTRL,
  970. SGTL5000_SMALL_POP, 1);
  971. /* disable short cut detector */
  972. snd_soc_write(codec, SGTL5000_CHIP_SHORT_CTRL, 0);
  973. /*
  974. * set i2s as default input of sound switch
  975. * TODO: add sound switch to control and dapm widge.
  976. */
  977. snd_soc_write(codec, SGTL5000_CHIP_SSS_CTRL,
  978. SGTL5000_DAC_SEL_I2S_IN << SGTL5000_DAC_SEL_SHIFT);
  979. snd_soc_write(codec, SGTL5000_CHIP_DIG_POWER,
  980. SGTL5000_ADC_EN | SGTL5000_DAC_EN);
  981. /* enable dac volume ramp by default */
  982. snd_soc_write(codec, SGTL5000_CHIP_ADCDAC_CTRL,
  983. SGTL5000_DAC_VOL_RAMP_EN |
  984. SGTL5000_DAC_MUTE_RIGHT |
  985. SGTL5000_DAC_MUTE_LEFT);
  986. snd_soc_write(codec, SGTL5000_CHIP_PAD_STRENGTH, 0x015f);
  987. snd_soc_write(codec, SGTL5000_CHIP_ANA_CTRL,
  988. SGTL5000_HP_ZCD_EN |
  989. SGTL5000_ADC_ZCD_EN);
  990. snd_soc_update_bits(codec, SGTL5000_CHIP_MIC_CTRL,
  991. SGTL5000_BIAS_R_MASK,
  992. sgtl5000->micbias_resistor << SGTL5000_BIAS_R_SHIFT);
  993. snd_soc_update_bits(codec, SGTL5000_CHIP_MIC_CTRL,
  994. SGTL5000_BIAS_VOLT_MASK,
  995. sgtl5000->micbias_voltage << SGTL5000_BIAS_VOLT_SHIFT);
  996. /*
  997. * disable DAP
  998. * TODO:
  999. * Enable DAP in kcontrol and dapm.
  1000. */
  1001. snd_soc_write(codec, SGTL5000_DAP_CTRL, 0);
  1002. return 0;
  1003. err:
  1004. return ret;
  1005. }
  1006. static int sgtl5000_remove(struct snd_soc_codec *codec)
  1007. {
  1008. return 0;
  1009. }
  1010. static struct snd_soc_codec_driver sgtl5000_driver = {
  1011. .probe = sgtl5000_probe,
  1012. .remove = sgtl5000_remove,
  1013. .set_bias_level = sgtl5000_set_bias_level,
  1014. .suspend_bias_off = true,
  1015. .component_driver = {
  1016. .controls = sgtl5000_snd_controls,
  1017. .num_controls = ARRAY_SIZE(sgtl5000_snd_controls),
  1018. .dapm_widgets = sgtl5000_dapm_widgets,
  1019. .num_dapm_widgets = ARRAY_SIZE(sgtl5000_dapm_widgets),
  1020. .dapm_routes = sgtl5000_dapm_routes,
  1021. .num_dapm_routes = ARRAY_SIZE(sgtl5000_dapm_routes),
  1022. },
  1023. };
  1024. static const struct regmap_config sgtl5000_regmap = {
  1025. .reg_bits = 16,
  1026. .val_bits = 16,
  1027. .reg_stride = 2,
  1028. .max_register = SGTL5000_MAX_REG_OFFSET,
  1029. .volatile_reg = sgtl5000_volatile,
  1030. .readable_reg = sgtl5000_readable,
  1031. .cache_type = REGCACHE_RBTREE,
  1032. .reg_defaults = sgtl5000_reg_defaults,
  1033. .num_reg_defaults = ARRAY_SIZE(sgtl5000_reg_defaults),
  1034. };
  1035. /*
  1036. * Write all the default values from sgtl5000_reg_defaults[] array into the
  1037. * sgtl5000 registers, to make sure we always start with the sane registers
  1038. * values as stated in the datasheet.
  1039. *
  1040. * Since sgtl5000 does not have a reset line, nor a reset command in software,
  1041. * we follow this approach to guarantee we always start from the default values
  1042. * and avoid problems like, not being able to probe after an audio playback
  1043. * followed by a system reset or a 'reboot' command in Linux
  1044. */
  1045. static void sgtl5000_fill_defaults(struct i2c_client *client)
  1046. {
  1047. struct sgtl5000_priv *sgtl5000 = i2c_get_clientdata(client);
  1048. int i, ret, val, index;
  1049. for (i = 0; i < ARRAY_SIZE(sgtl5000_reg_defaults); i++) {
  1050. val = sgtl5000_reg_defaults[i].def;
  1051. index = sgtl5000_reg_defaults[i].reg;
  1052. ret = regmap_write(sgtl5000->regmap, index, val);
  1053. if (ret)
  1054. dev_err(&client->dev,
  1055. "%s: error %d setting reg 0x%02x to 0x%04x\n",
  1056. __func__, ret, index, val);
  1057. }
  1058. }
  1059. static int sgtl5000_i2c_probe(struct i2c_client *client,
  1060. const struct i2c_device_id *id)
  1061. {
  1062. struct sgtl5000_priv *sgtl5000;
  1063. int ret, reg, rev;
  1064. struct device_node *np = client->dev.of_node;
  1065. u32 value;
  1066. u16 ana_pwr;
  1067. sgtl5000 = devm_kzalloc(&client->dev, sizeof(*sgtl5000), GFP_KERNEL);
  1068. if (!sgtl5000)
  1069. return -ENOMEM;
  1070. i2c_set_clientdata(client, sgtl5000);
  1071. ret = sgtl5000_enable_regulators(client);
  1072. if (ret)
  1073. return ret;
  1074. sgtl5000->regmap = devm_regmap_init_i2c(client, &sgtl5000_regmap);
  1075. if (IS_ERR(sgtl5000->regmap)) {
  1076. ret = PTR_ERR(sgtl5000->regmap);
  1077. dev_err(&client->dev, "Failed to allocate regmap: %d\n", ret);
  1078. goto disable_regs;
  1079. }
  1080. sgtl5000->mclk = devm_clk_get(&client->dev, NULL);
  1081. if (IS_ERR(sgtl5000->mclk)) {
  1082. ret = PTR_ERR(sgtl5000->mclk);
  1083. dev_err(&client->dev, "Failed to get mclock: %d\n", ret);
  1084. /* Defer the probe to see if the clk will be provided later */
  1085. if (ret == -ENOENT)
  1086. ret = -EPROBE_DEFER;
  1087. goto disable_regs;
  1088. }
  1089. ret = clk_prepare_enable(sgtl5000->mclk);
  1090. if (ret) {
  1091. dev_err(&client->dev, "Error enabling clock %d\n", ret);
  1092. goto disable_regs;
  1093. }
  1094. /* Need 8 clocks before I2C accesses */
  1095. udelay(1);
  1096. /* read chip information */
  1097. ret = regmap_read(sgtl5000->regmap, SGTL5000_CHIP_ID, &reg);
  1098. if (ret) {
  1099. dev_err(&client->dev, "Error reading chip id %d\n", ret);
  1100. goto disable_clk;
  1101. }
  1102. if (((reg & SGTL5000_PARTID_MASK) >> SGTL5000_PARTID_SHIFT) !=
  1103. SGTL5000_PARTID_PART_ID) {
  1104. dev_err(&client->dev,
  1105. "Device with ID register %x is not a sgtl5000\n", reg);
  1106. ret = -ENODEV;
  1107. goto disable_clk;
  1108. }
  1109. rev = (reg & SGTL5000_REVID_MASK) >> SGTL5000_REVID_SHIFT;
  1110. dev_info(&client->dev, "sgtl5000 revision 0x%x\n", rev);
  1111. sgtl5000->revision = rev;
  1112. /* reconfigure the clocks in case we're using the PLL */
  1113. ret = regmap_write(sgtl5000->regmap,
  1114. SGTL5000_CHIP_CLK_CTRL,
  1115. SGTL5000_CHIP_CLK_CTRL_DEFAULT);
  1116. if (ret)
  1117. dev_err(&client->dev,
  1118. "Error %d initializing CHIP_CLK_CTRL\n", ret);
  1119. /* Follow section 2.2.1.1 of AN3663 */
  1120. ana_pwr = SGTL5000_ANA_POWER_DEFAULT;
  1121. if (sgtl5000->num_supplies <= VDDD) {
  1122. /* internal VDDD at 1.2V */
  1123. ret = regmap_update_bits(sgtl5000->regmap,
  1124. SGTL5000_CHIP_LINREG_CTRL,
  1125. SGTL5000_LINREG_VDDD_MASK,
  1126. LINREG_VDDD);
  1127. if (ret)
  1128. dev_err(&client->dev,
  1129. "Error %d setting LINREG_VDDD\n", ret);
  1130. ana_pwr |= SGTL5000_LINEREG_D_POWERUP;
  1131. dev_info(&client->dev,
  1132. "Using internal LDO instead of VDDD: check ER1\n");
  1133. } else {
  1134. /* using external LDO for VDDD
  1135. * Clear startup powerup and simple powerup
  1136. * bits to save power
  1137. */
  1138. ana_pwr &= ~(SGTL5000_STARTUP_POWERUP
  1139. | SGTL5000_LINREG_SIMPLE_POWERUP);
  1140. dev_dbg(&client->dev, "Using external VDDD\n");
  1141. }
  1142. ret = regmap_write(sgtl5000->regmap, SGTL5000_CHIP_ANA_POWER, ana_pwr);
  1143. if (ret)
  1144. dev_err(&client->dev,
  1145. "Error %d setting CHIP_ANA_POWER to %04x\n",
  1146. ret, ana_pwr);
  1147. if (np) {
  1148. if (!of_property_read_u32(np,
  1149. "micbias-resistor-k-ohms", &value)) {
  1150. switch (value) {
  1151. case SGTL5000_MICBIAS_OFF:
  1152. sgtl5000->micbias_resistor = 0;
  1153. break;
  1154. case SGTL5000_MICBIAS_2K:
  1155. sgtl5000->micbias_resistor = 1;
  1156. break;
  1157. case SGTL5000_MICBIAS_4K:
  1158. sgtl5000->micbias_resistor = 2;
  1159. break;
  1160. case SGTL5000_MICBIAS_8K:
  1161. sgtl5000->micbias_resistor = 3;
  1162. break;
  1163. default:
  1164. sgtl5000->micbias_resistor = 2;
  1165. dev_err(&client->dev,
  1166. "Unsuitable MicBias resistor\n");
  1167. }
  1168. } else {
  1169. /* default is 4Kohms */
  1170. sgtl5000->micbias_resistor = 2;
  1171. }
  1172. if (!of_property_read_u32(np,
  1173. "micbias-voltage-m-volts", &value)) {
  1174. /* 1250mV => 0 */
  1175. /* steps of 250mV */
  1176. if ((value >= 1250) && (value <= 3000))
  1177. sgtl5000->micbias_voltage = (value / 250) - 5;
  1178. else {
  1179. sgtl5000->micbias_voltage = 0;
  1180. dev_err(&client->dev,
  1181. "Unsuitable MicBias voltage\n");
  1182. }
  1183. } else {
  1184. sgtl5000->micbias_voltage = 0;
  1185. }
  1186. }
  1187. /* Ensure sgtl5000 will start with sane register values */
  1188. sgtl5000_fill_defaults(client);
  1189. ret = snd_soc_register_codec(&client->dev,
  1190. &sgtl5000_driver, &sgtl5000_dai, 1);
  1191. if (ret)
  1192. goto disable_clk;
  1193. return 0;
  1194. disable_clk:
  1195. clk_disable_unprepare(sgtl5000->mclk);
  1196. disable_regs:
  1197. regulator_bulk_disable(sgtl5000->num_supplies, sgtl5000->supplies);
  1198. regulator_bulk_free(sgtl5000->num_supplies, sgtl5000->supplies);
  1199. return ret;
  1200. }
  1201. static int sgtl5000_i2c_remove(struct i2c_client *client)
  1202. {
  1203. struct sgtl5000_priv *sgtl5000 = i2c_get_clientdata(client);
  1204. snd_soc_unregister_codec(&client->dev);
  1205. clk_disable_unprepare(sgtl5000->mclk);
  1206. regulator_bulk_disable(sgtl5000->num_supplies, sgtl5000->supplies);
  1207. regulator_bulk_free(sgtl5000->num_supplies, sgtl5000->supplies);
  1208. return 0;
  1209. }
  1210. static const struct i2c_device_id sgtl5000_id[] = {
  1211. {"sgtl5000", 0},
  1212. {},
  1213. };
  1214. MODULE_DEVICE_TABLE(i2c, sgtl5000_id);
  1215. static const struct of_device_id sgtl5000_dt_ids[] = {
  1216. { .compatible = "fsl,sgtl5000", },
  1217. { /* sentinel */ }
  1218. };
  1219. MODULE_DEVICE_TABLE(of, sgtl5000_dt_ids);
  1220. static struct i2c_driver sgtl5000_i2c_driver = {
  1221. .driver = {
  1222. .name = "sgtl5000",
  1223. .of_match_table = sgtl5000_dt_ids,
  1224. },
  1225. .probe = sgtl5000_i2c_probe,
  1226. .remove = sgtl5000_i2c_remove,
  1227. .id_table = sgtl5000_id,
  1228. };
  1229. module_i2c_driver(sgtl5000_i2c_driver);
  1230. MODULE_DESCRIPTION("Freescale SGTL5000 ALSA SoC Codec Driver");
  1231. MODULE_AUTHOR("Zeng Zhaoming <zengzm.kernel@gmail.com>");
  1232. MODULE_LICENSE("GPL");