sgtl5000.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421
  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. struct sgtl5000_priv *sgtl = snd_soc_codec_get_drvdata(codec);
  683. int ret;
  684. switch (level) {
  685. case SND_SOC_BIAS_ON:
  686. case SND_SOC_BIAS_PREPARE:
  687. case SND_SOC_BIAS_STANDBY:
  688. regcache_cache_only(sgtl->regmap, false);
  689. ret = regcache_sync(sgtl->regmap);
  690. if (ret) {
  691. regcache_cache_only(sgtl->regmap, true);
  692. return ret;
  693. }
  694. snd_soc_update_bits(codec, SGTL5000_CHIP_ANA_POWER,
  695. SGTL5000_REFTOP_POWERUP,
  696. SGTL5000_REFTOP_POWERUP);
  697. break;
  698. case SND_SOC_BIAS_OFF:
  699. regcache_cache_only(sgtl->regmap, true);
  700. snd_soc_update_bits(codec, SGTL5000_CHIP_ANA_POWER,
  701. SGTL5000_REFTOP_POWERUP, 0);
  702. break;
  703. }
  704. return 0;
  705. }
  706. #define SGTL5000_FORMATS (SNDRV_PCM_FMTBIT_S16_LE |\
  707. SNDRV_PCM_FMTBIT_S20_3LE |\
  708. SNDRV_PCM_FMTBIT_S24_LE |\
  709. SNDRV_PCM_FMTBIT_S32_LE)
  710. static const struct snd_soc_dai_ops sgtl5000_ops = {
  711. .hw_params = sgtl5000_pcm_hw_params,
  712. .digital_mute = sgtl5000_digital_mute,
  713. .set_fmt = sgtl5000_set_dai_fmt,
  714. .set_sysclk = sgtl5000_set_dai_sysclk,
  715. };
  716. static struct snd_soc_dai_driver sgtl5000_dai = {
  717. .name = "sgtl5000",
  718. .playback = {
  719. .stream_name = "Playback",
  720. .channels_min = 1,
  721. .channels_max = 2,
  722. /*
  723. * only support 8~48K + 96K,
  724. * TODO modify hw_param to support more
  725. */
  726. .rates = SNDRV_PCM_RATE_8000_48000 | SNDRV_PCM_RATE_96000,
  727. .formats = SGTL5000_FORMATS,
  728. },
  729. .capture = {
  730. .stream_name = "Capture",
  731. .channels_min = 1,
  732. .channels_max = 2,
  733. .rates = SNDRV_PCM_RATE_8000_48000 | SNDRV_PCM_RATE_96000,
  734. .formats = SGTL5000_FORMATS,
  735. },
  736. .ops = &sgtl5000_ops,
  737. .symmetric_rates = 1,
  738. };
  739. static bool sgtl5000_volatile(struct device *dev, unsigned int reg)
  740. {
  741. switch (reg) {
  742. case SGTL5000_CHIP_ID:
  743. case SGTL5000_CHIP_ADCDAC_CTRL:
  744. case SGTL5000_CHIP_ANA_STATUS:
  745. return true;
  746. }
  747. return false;
  748. }
  749. static bool sgtl5000_readable(struct device *dev, unsigned int reg)
  750. {
  751. switch (reg) {
  752. case SGTL5000_CHIP_ID:
  753. case SGTL5000_CHIP_DIG_POWER:
  754. case SGTL5000_CHIP_CLK_CTRL:
  755. case SGTL5000_CHIP_I2S_CTRL:
  756. case SGTL5000_CHIP_SSS_CTRL:
  757. case SGTL5000_CHIP_ADCDAC_CTRL:
  758. case SGTL5000_CHIP_DAC_VOL:
  759. case SGTL5000_CHIP_PAD_STRENGTH:
  760. case SGTL5000_CHIP_ANA_ADC_CTRL:
  761. case SGTL5000_CHIP_ANA_HP_CTRL:
  762. case SGTL5000_CHIP_ANA_CTRL:
  763. case SGTL5000_CHIP_LINREG_CTRL:
  764. case SGTL5000_CHIP_REF_CTRL:
  765. case SGTL5000_CHIP_MIC_CTRL:
  766. case SGTL5000_CHIP_LINE_OUT_CTRL:
  767. case SGTL5000_CHIP_LINE_OUT_VOL:
  768. case SGTL5000_CHIP_ANA_POWER:
  769. case SGTL5000_CHIP_PLL_CTRL:
  770. case SGTL5000_CHIP_CLK_TOP_CTRL:
  771. case SGTL5000_CHIP_ANA_STATUS:
  772. case SGTL5000_CHIP_SHORT_CTRL:
  773. case SGTL5000_CHIP_ANA_TEST2:
  774. case SGTL5000_DAP_CTRL:
  775. case SGTL5000_DAP_PEQ:
  776. case SGTL5000_DAP_BASS_ENHANCE:
  777. case SGTL5000_DAP_BASS_ENHANCE_CTRL:
  778. case SGTL5000_DAP_AUDIO_EQ:
  779. case SGTL5000_DAP_SURROUND:
  780. case SGTL5000_DAP_FLT_COEF_ACCESS:
  781. case SGTL5000_DAP_COEF_WR_B0_MSB:
  782. case SGTL5000_DAP_COEF_WR_B0_LSB:
  783. case SGTL5000_DAP_EQ_BASS_BAND0:
  784. case SGTL5000_DAP_EQ_BASS_BAND1:
  785. case SGTL5000_DAP_EQ_BASS_BAND2:
  786. case SGTL5000_DAP_EQ_BASS_BAND3:
  787. case SGTL5000_DAP_EQ_BASS_BAND4:
  788. case SGTL5000_DAP_MAIN_CHAN:
  789. case SGTL5000_DAP_MIX_CHAN:
  790. case SGTL5000_DAP_AVC_CTRL:
  791. case SGTL5000_DAP_AVC_THRESHOLD:
  792. case SGTL5000_DAP_AVC_ATTACK:
  793. case SGTL5000_DAP_AVC_DECAY:
  794. case SGTL5000_DAP_COEF_WR_B1_MSB:
  795. case SGTL5000_DAP_COEF_WR_B1_LSB:
  796. case SGTL5000_DAP_COEF_WR_B2_MSB:
  797. case SGTL5000_DAP_COEF_WR_B2_LSB:
  798. case SGTL5000_DAP_COEF_WR_A1_MSB:
  799. case SGTL5000_DAP_COEF_WR_A1_LSB:
  800. case SGTL5000_DAP_COEF_WR_A2_MSB:
  801. case SGTL5000_DAP_COEF_WR_A2_LSB:
  802. return true;
  803. default:
  804. return false;
  805. }
  806. }
  807. /*
  808. * This precalculated table contains all (vag_val * 100 / lo_calcntrl) results
  809. * to select an appropriate lo_vol_* in SGTL5000_CHIP_LINE_OUT_VOL
  810. * The calculatation was done for all possible register values which
  811. * is the array index and the following formula: 10^((idx−15)/40) * 100
  812. */
  813. static const u8 vol_quot_table[] = {
  814. 42, 45, 47, 50, 53, 56, 60, 63,
  815. 67, 71, 75, 79, 84, 89, 94, 100,
  816. 106, 112, 119, 126, 133, 141, 150, 158,
  817. 168, 178, 188, 200, 211, 224, 237, 251
  818. };
  819. /*
  820. * sgtl5000 has 3 internal power supplies:
  821. * 1. VAG, normally set to vdda/2
  822. * 2. charge pump, set to different value
  823. * according to voltage of vdda and vddio
  824. * 3. line out VAG, normally set to vddio/2
  825. *
  826. * and should be set according to:
  827. * 1. vddd provided by external or not
  828. * 2. vdda and vddio voltage value. > 3.1v or not
  829. */
  830. static int sgtl5000_set_power_regs(struct snd_soc_codec *codec)
  831. {
  832. int vddd;
  833. int vdda;
  834. int vddio;
  835. u16 ana_pwr;
  836. u16 lreg_ctrl;
  837. int vag;
  838. int lo_vag;
  839. int vol_quot;
  840. int lo_vol;
  841. size_t i;
  842. struct sgtl5000_priv *sgtl5000 = snd_soc_codec_get_drvdata(codec);
  843. vdda = regulator_get_voltage(sgtl5000->supplies[VDDA].consumer);
  844. vddio = regulator_get_voltage(sgtl5000->supplies[VDDIO].consumer);
  845. vddd = (sgtl5000->num_supplies > VDDD)
  846. ? regulator_get_voltage(sgtl5000->supplies[VDDD].consumer)
  847. : LDO_VOLTAGE;
  848. vdda = vdda / 1000;
  849. vddio = vddio / 1000;
  850. vddd = vddd / 1000;
  851. if (vdda <= 0 || vddio <= 0 || vddd < 0) {
  852. dev_err(codec->dev, "regulator voltage not set correctly\n");
  853. return -EINVAL;
  854. }
  855. /* according to datasheet, maximum voltage of supplies */
  856. if (vdda > 3600 || vddio > 3600 || vddd > 1980) {
  857. dev_err(codec->dev,
  858. "exceed max voltage vdda %dmV vddio %dmV vddd %dmV\n",
  859. vdda, vddio, vddd);
  860. return -EINVAL;
  861. }
  862. /* reset value */
  863. ana_pwr = snd_soc_read(codec, SGTL5000_CHIP_ANA_POWER);
  864. ana_pwr |= SGTL5000_DAC_STEREO |
  865. SGTL5000_ADC_STEREO |
  866. SGTL5000_REFTOP_POWERUP;
  867. lreg_ctrl = snd_soc_read(codec, SGTL5000_CHIP_LINREG_CTRL);
  868. if (vddio < 3100 && vdda < 3100) {
  869. /* enable internal oscillator used for charge pump */
  870. snd_soc_update_bits(codec, SGTL5000_CHIP_CLK_TOP_CTRL,
  871. SGTL5000_INT_OSC_EN,
  872. SGTL5000_INT_OSC_EN);
  873. /* Enable VDDC charge pump */
  874. ana_pwr |= SGTL5000_VDDC_CHRGPMP_POWERUP;
  875. } else if (vddio >= 3100 && vdda >= 3100) {
  876. ana_pwr &= ~SGTL5000_VDDC_CHRGPMP_POWERUP;
  877. /* VDDC use VDDIO rail */
  878. lreg_ctrl |= SGTL5000_VDDC_ASSN_OVRD;
  879. lreg_ctrl |= SGTL5000_VDDC_MAN_ASSN_VDDIO <<
  880. SGTL5000_VDDC_MAN_ASSN_SHIFT;
  881. }
  882. snd_soc_write(codec, SGTL5000_CHIP_LINREG_CTRL, lreg_ctrl);
  883. snd_soc_write(codec, SGTL5000_CHIP_ANA_POWER, ana_pwr);
  884. /*
  885. * set ADC/DAC VAG to vdda / 2,
  886. * should stay in range (0.8v, 1.575v)
  887. */
  888. vag = vdda / 2;
  889. if (vag <= SGTL5000_ANA_GND_BASE)
  890. vag = 0;
  891. else if (vag >= SGTL5000_ANA_GND_BASE + SGTL5000_ANA_GND_STP *
  892. (SGTL5000_ANA_GND_MASK >> SGTL5000_ANA_GND_SHIFT))
  893. vag = SGTL5000_ANA_GND_MASK >> SGTL5000_ANA_GND_SHIFT;
  894. else
  895. vag = (vag - SGTL5000_ANA_GND_BASE) / SGTL5000_ANA_GND_STP;
  896. snd_soc_update_bits(codec, SGTL5000_CHIP_REF_CTRL,
  897. SGTL5000_ANA_GND_MASK, vag << SGTL5000_ANA_GND_SHIFT);
  898. /* set line out VAG to vddio / 2, in range (0.8v, 1.675v) */
  899. lo_vag = vddio / 2;
  900. if (lo_vag <= SGTL5000_LINE_OUT_GND_BASE)
  901. lo_vag = 0;
  902. else if (lo_vag >= SGTL5000_LINE_OUT_GND_BASE +
  903. SGTL5000_LINE_OUT_GND_STP * SGTL5000_LINE_OUT_GND_MAX)
  904. lo_vag = SGTL5000_LINE_OUT_GND_MAX;
  905. else
  906. lo_vag = (lo_vag - SGTL5000_LINE_OUT_GND_BASE) /
  907. SGTL5000_LINE_OUT_GND_STP;
  908. snd_soc_update_bits(codec, SGTL5000_CHIP_LINE_OUT_CTRL,
  909. SGTL5000_LINE_OUT_CURRENT_MASK |
  910. SGTL5000_LINE_OUT_GND_MASK,
  911. lo_vag << SGTL5000_LINE_OUT_GND_SHIFT |
  912. SGTL5000_LINE_OUT_CURRENT_360u <<
  913. SGTL5000_LINE_OUT_CURRENT_SHIFT);
  914. /*
  915. * Set lineout output level in range (0..31)
  916. * the same value is used for right and left channel
  917. *
  918. * Searching for a suitable index solving this formula:
  919. * idx = 40 * log10(vag_val / lo_cagcntrl) + 15
  920. */
  921. vol_quot = (vag * 100) / lo_vag;
  922. lo_vol = 0;
  923. for (i = 0; i < ARRAY_SIZE(vol_quot_table); i++) {
  924. if (vol_quot >= vol_quot_table[i])
  925. lo_vol = i;
  926. else
  927. break;
  928. }
  929. snd_soc_update_bits(codec, SGTL5000_CHIP_LINE_OUT_VOL,
  930. SGTL5000_LINE_OUT_VOL_RIGHT_MASK |
  931. SGTL5000_LINE_OUT_VOL_LEFT_MASK,
  932. lo_vol << SGTL5000_LINE_OUT_VOL_RIGHT_SHIFT |
  933. lo_vol << SGTL5000_LINE_OUT_VOL_LEFT_SHIFT);
  934. return 0;
  935. }
  936. static int sgtl5000_enable_regulators(struct i2c_client *client)
  937. {
  938. int ret;
  939. int i;
  940. int external_vddd = 0;
  941. struct regulator *vddd;
  942. struct sgtl5000_priv *sgtl5000 = i2c_get_clientdata(client);
  943. for (i = 0; i < ARRAY_SIZE(sgtl5000->supplies); i++)
  944. sgtl5000->supplies[i].supply = supply_names[i];
  945. vddd = regulator_get_optional(&client->dev, "VDDD");
  946. if (IS_ERR(vddd)) {
  947. /* See if it's just not registered yet */
  948. if (PTR_ERR(vddd) == -EPROBE_DEFER)
  949. return -EPROBE_DEFER;
  950. } else {
  951. external_vddd = 1;
  952. regulator_put(vddd);
  953. }
  954. sgtl5000->num_supplies = ARRAY_SIZE(sgtl5000->supplies)
  955. - 1 + external_vddd;
  956. ret = regulator_bulk_get(&client->dev, sgtl5000->num_supplies,
  957. sgtl5000->supplies);
  958. if (ret)
  959. return ret;
  960. ret = regulator_bulk_enable(sgtl5000->num_supplies,
  961. sgtl5000->supplies);
  962. if (!ret)
  963. usleep_range(10, 20);
  964. else
  965. regulator_bulk_free(sgtl5000->num_supplies,
  966. sgtl5000->supplies);
  967. return ret;
  968. }
  969. static int sgtl5000_probe(struct snd_soc_codec *codec)
  970. {
  971. int ret;
  972. struct sgtl5000_priv *sgtl5000 = snd_soc_codec_get_drvdata(codec);
  973. /* power up sgtl5000 */
  974. ret = sgtl5000_set_power_regs(codec);
  975. if (ret)
  976. goto err;
  977. /* enable small pop, introduce 400ms delay in turning off */
  978. snd_soc_update_bits(codec, SGTL5000_CHIP_REF_CTRL,
  979. SGTL5000_SMALL_POP, 1);
  980. /* disable short cut detector */
  981. snd_soc_write(codec, SGTL5000_CHIP_SHORT_CTRL, 0);
  982. /*
  983. * set i2s as default input of sound switch
  984. * TODO: add sound switch to control and dapm widge.
  985. */
  986. snd_soc_write(codec, SGTL5000_CHIP_SSS_CTRL,
  987. SGTL5000_DAC_SEL_I2S_IN << SGTL5000_DAC_SEL_SHIFT);
  988. snd_soc_write(codec, SGTL5000_CHIP_DIG_POWER,
  989. SGTL5000_ADC_EN | SGTL5000_DAC_EN);
  990. /* enable dac volume ramp by default */
  991. snd_soc_write(codec, SGTL5000_CHIP_ADCDAC_CTRL,
  992. SGTL5000_DAC_VOL_RAMP_EN |
  993. SGTL5000_DAC_MUTE_RIGHT |
  994. SGTL5000_DAC_MUTE_LEFT);
  995. snd_soc_write(codec, SGTL5000_CHIP_PAD_STRENGTH, 0x015f);
  996. snd_soc_write(codec, SGTL5000_CHIP_ANA_CTRL,
  997. SGTL5000_HP_ZCD_EN |
  998. SGTL5000_ADC_ZCD_EN);
  999. snd_soc_update_bits(codec, SGTL5000_CHIP_MIC_CTRL,
  1000. SGTL5000_BIAS_R_MASK,
  1001. sgtl5000->micbias_resistor << SGTL5000_BIAS_R_SHIFT);
  1002. snd_soc_update_bits(codec, SGTL5000_CHIP_MIC_CTRL,
  1003. SGTL5000_BIAS_VOLT_MASK,
  1004. sgtl5000->micbias_voltage << SGTL5000_BIAS_VOLT_SHIFT);
  1005. /*
  1006. * disable DAP
  1007. * TODO:
  1008. * Enable DAP in kcontrol and dapm.
  1009. */
  1010. snd_soc_write(codec, SGTL5000_DAP_CTRL, 0);
  1011. return 0;
  1012. err:
  1013. return ret;
  1014. }
  1015. static int sgtl5000_remove(struct snd_soc_codec *codec)
  1016. {
  1017. return 0;
  1018. }
  1019. static struct snd_soc_codec_driver sgtl5000_driver = {
  1020. .probe = sgtl5000_probe,
  1021. .remove = sgtl5000_remove,
  1022. .set_bias_level = sgtl5000_set_bias_level,
  1023. .suspend_bias_off = true,
  1024. .component_driver = {
  1025. .controls = sgtl5000_snd_controls,
  1026. .num_controls = ARRAY_SIZE(sgtl5000_snd_controls),
  1027. .dapm_widgets = sgtl5000_dapm_widgets,
  1028. .num_dapm_widgets = ARRAY_SIZE(sgtl5000_dapm_widgets),
  1029. .dapm_routes = sgtl5000_dapm_routes,
  1030. .num_dapm_routes = ARRAY_SIZE(sgtl5000_dapm_routes),
  1031. },
  1032. };
  1033. static const struct regmap_config sgtl5000_regmap = {
  1034. .reg_bits = 16,
  1035. .val_bits = 16,
  1036. .reg_stride = 2,
  1037. .max_register = SGTL5000_MAX_REG_OFFSET,
  1038. .volatile_reg = sgtl5000_volatile,
  1039. .readable_reg = sgtl5000_readable,
  1040. .cache_type = REGCACHE_RBTREE,
  1041. .reg_defaults = sgtl5000_reg_defaults,
  1042. .num_reg_defaults = ARRAY_SIZE(sgtl5000_reg_defaults),
  1043. };
  1044. /*
  1045. * Write all the default values from sgtl5000_reg_defaults[] array into the
  1046. * sgtl5000 registers, to make sure we always start with the sane registers
  1047. * values as stated in the datasheet.
  1048. *
  1049. * Since sgtl5000 does not have a reset line, nor a reset command in software,
  1050. * we follow this approach to guarantee we always start from the default values
  1051. * and avoid problems like, not being able to probe after an audio playback
  1052. * followed by a system reset or a 'reboot' command in Linux
  1053. */
  1054. static void sgtl5000_fill_defaults(struct i2c_client *client)
  1055. {
  1056. struct sgtl5000_priv *sgtl5000 = i2c_get_clientdata(client);
  1057. int i, ret, val, index;
  1058. for (i = 0; i < ARRAY_SIZE(sgtl5000_reg_defaults); i++) {
  1059. val = sgtl5000_reg_defaults[i].def;
  1060. index = sgtl5000_reg_defaults[i].reg;
  1061. ret = regmap_write(sgtl5000->regmap, index, val);
  1062. if (ret)
  1063. dev_err(&client->dev,
  1064. "%s: error %d setting reg 0x%02x to 0x%04x\n",
  1065. __func__, ret, index, val);
  1066. }
  1067. }
  1068. static int sgtl5000_i2c_probe(struct i2c_client *client,
  1069. const struct i2c_device_id *id)
  1070. {
  1071. struct sgtl5000_priv *sgtl5000;
  1072. int ret, reg, rev;
  1073. struct device_node *np = client->dev.of_node;
  1074. u32 value;
  1075. u16 ana_pwr;
  1076. sgtl5000 = devm_kzalloc(&client->dev, sizeof(*sgtl5000), GFP_KERNEL);
  1077. if (!sgtl5000)
  1078. return -ENOMEM;
  1079. i2c_set_clientdata(client, sgtl5000);
  1080. ret = sgtl5000_enable_regulators(client);
  1081. if (ret)
  1082. return ret;
  1083. sgtl5000->regmap = devm_regmap_init_i2c(client, &sgtl5000_regmap);
  1084. if (IS_ERR(sgtl5000->regmap)) {
  1085. ret = PTR_ERR(sgtl5000->regmap);
  1086. dev_err(&client->dev, "Failed to allocate regmap: %d\n", ret);
  1087. goto disable_regs;
  1088. }
  1089. sgtl5000->mclk = devm_clk_get(&client->dev, NULL);
  1090. if (IS_ERR(sgtl5000->mclk)) {
  1091. ret = PTR_ERR(sgtl5000->mclk);
  1092. dev_err(&client->dev, "Failed to get mclock: %d\n", ret);
  1093. /* Defer the probe to see if the clk will be provided later */
  1094. if (ret == -ENOENT)
  1095. ret = -EPROBE_DEFER;
  1096. goto disable_regs;
  1097. }
  1098. ret = clk_prepare_enable(sgtl5000->mclk);
  1099. if (ret) {
  1100. dev_err(&client->dev, "Error enabling clock %d\n", ret);
  1101. goto disable_regs;
  1102. }
  1103. /* Need 8 clocks before I2C accesses */
  1104. udelay(1);
  1105. /* read chip information */
  1106. ret = regmap_read(sgtl5000->regmap, SGTL5000_CHIP_ID, &reg);
  1107. if (ret) {
  1108. dev_err(&client->dev, "Error reading chip id %d\n", ret);
  1109. goto disable_clk;
  1110. }
  1111. if (((reg & SGTL5000_PARTID_MASK) >> SGTL5000_PARTID_SHIFT) !=
  1112. SGTL5000_PARTID_PART_ID) {
  1113. dev_err(&client->dev,
  1114. "Device with ID register %x is not a sgtl5000\n", reg);
  1115. ret = -ENODEV;
  1116. goto disable_clk;
  1117. }
  1118. rev = (reg & SGTL5000_REVID_MASK) >> SGTL5000_REVID_SHIFT;
  1119. dev_info(&client->dev, "sgtl5000 revision 0x%x\n", rev);
  1120. sgtl5000->revision = rev;
  1121. /* reconfigure the clocks in case we're using the PLL */
  1122. ret = regmap_write(sgtl5000->regmap,
  1123. SGTL5000_CHIP_CLK_CTRL,
  1124. SGTL5000_CHIP_CLK_CTRL_DEFAULT);
  1125. if (ret)
  1126. dev_err(&client->dev,
  1127. "Error %d initializing CHIP_CLK_CTRL\n", ret);
  1128. /* Follow section 2.2.1.1 of AN3663 */
  1129. ana_pwr = SGTL5000_ANA_POWER_DEFAULT;
  1130. if (sgtl5000->num_supplies <= VDDD) {
  1131. /* internal VDDD at 1.2V */
  1132. ret = regmap_update_bits(sgtl5000->regmap,
  1133. SGTL5000_CHIP_LINREG_CTRL,
  1134. SGTL5000_LINREG_VDDD_MASK,
  1135. LINREG_VDDD);
  1136. if (ret)
  1137. dev_err(&client->dev,
  1138. "Error %d setting LINREG_VDDD\n", ret);
  1139. ana_pwr |= SGTL5000_LINEREG_D_POWERUP;
  1140. dev_info(&client->dev,
  1141. "Using internal LDO instead of VDDD: check ER1\n");
  1142. } else {
  1143. /* using external LDO for VDDD
  1144. * Clear startup powerup and simple powerup
  1145. * bits to save power
  1146. */
  1147. ana_pwr &= ~(SGTL5000_STARTUP_POWERUP
  1148. | SGTL5000_LINREG_SIMPLE_POWERUP);
  1149. dev_dbg(&client->dev, "Using external VDDD\n");
  1150. }
  1151. ret = regmap_write(sgtl5000->regmap, SGTL5000_CHIP_ANA_POWER, ana_pwr);
  1152. if (ret)
  1153. dev_err(&client->dev,
  1154. "Error %d setting CHIP_ANA_POWER to %04x\n",
  1155. ret, ana_pwr);
  1156. if (np) {
  1157. if (!of_property_read_u32(np,
  1158. "micbias-resistor-k-ohms", &value)) {
  1159. switch (value) {
  1160. case SGTL5000_MICBIAS_OFF:
  1161. sgtl5000->micbias_resistor = 0;
  1162. break;
  1163. case SGTL5000_MICBIAS_2K:
  1164. sgtl5000->micbias_resistor = 1;
  1165. break;
  1166. case SGTL5000_MICBIAS_4K:
  1167. sgtl5000->micbias_resistor = 2;
  1168. break;
  1169. case SGTL5000_MICBIAS_8K:
  1170. sgtl5000->micbias_resistor = 3;
  1171. break;
  1172. default:
  1173. sgtl5000->micbias_resistor = 2;
  1174. dev_err(&client->dev,
  1175. "Unsuitable MicBias resistor\n");
  1176. }
  1177. } else {
  1178. /* default is 4Kohms */
  1179. sgtl5000->micbias_resistor = 2;
  1180. }
  1181. if (!of_property_read_u32(np,
  1182. "micbias-voltage-m-volts", &value)) {
  1183. /* 1250mV => 0 */
  1184. /* steps of 250mV */
  1185. if ((value >= 1250) && (value <= 3000))
  1186. sgtl5000->micbias_voltage = (value / 250) - 5;
  1187. else {
  1188. sgtl5000->micbias_voltage = 0;
  1189. dev_err(&client->dev,
  1190. "Unsuitable MicBias voltage\n");
  1191. }
  1192. } else {
  1193. sgtl5000->micbias_voltage = 0;
  1194. }
  1195. }
  1196. /* Ensure sgtl5000 will start with sane register values */
  1197. sgtl5000_fill_defaults(client);
  1198. ret = snd_soc_register_codec(&client->dev,
  1199. &sgtl5000_driver, &sgtl5000_dai, 1);
  1200. if (ret)
  1201. goto disable_clk;
  1202. return 0;
  1203. disable_clk:
  1204. clk_disable_unprepare(sgtl5000->mclk);
  1205. disable_regs:
  1206. regulator_bulk_disable(sgtl5000->num_supplies, sgtl5000->supplies);
  1207. regulator_bulk_free(sgtl5000->num_supplies, sgtl5000->supplies);
  1208. return ret;
  1209. }
  1210. static int sgtl5000_i2c_remove(struct i2c_client *client)
  1211. {
  1212. struct sgtl5000_priv *sgtl5000 = i2c_get_clientdata(client);
  1213. snd_soc_unregister_codec(&client->dev);
  1214. clk_disable_unprepare(sgtl5000->mclk);
  1215. regulator_bulk_disable(sgtl5000->num_supplies, sgtl5000->supplies);
  1216. regulator_bulk_free(sgtl5000->num_supplies, sgtl5000->supplies);
  1217. return 0;
  1218. }
  1219. static const struct i2c_device_id sgtl5000_id[] = {
  1220. {"sgtl5000", 0},
  1221. {},
  1222. };
  1223. MODULE_DEVICE_TABLE(i2c, sgtl5000_id);
  1224. static const struct of_device_id sgtl5000_dt_ids[] = {
  1225. { .compatible = "fsl,sgtl5000", },
  1226. { /* sentinel */ }
  1227. };
  1228. MODULE_DEVICE_TABLE(of, sgtl5000_dt_ids);
  1229. static struct i2c_driver sgtl5000_i2c_driver = {
  1230. .driver = {
  1231. .name = "sgtl5000",
  1232. .of_match_table = sgtl5000_dt_ids,
  1233. },
  1234. .probe = sgtl5000_i2c_probe,
  1235. .remove = sgtl5000_i2c_remove,
  1236. .id_table = sgtl5000_id,
  1237. };
  1238. module_i2c_driver(sgtl5000_i2c_driver);
  1239. MODULE_DESCRIPTION("Freescale SGTL5000 ALSA SoC Codec Driver");
  1240. MODULE_AUTHOR("Zeng Zhaoming <zengzm.kernel@gmail.com>");
  1241. MODULE_LICENSE("GPL");