sgtl5000.c 37 KB

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