sgtl5000.c 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839
  1. // SPDX-License-Identifier: GPL-2.0
  2. //
  3. // sgtl5000.c -- SGTL5000 ALSA SoC Audio driver
  4. //
  5. // Copyright 2010-2011 Freescale Semiconductor, Inc. All Rights Reserved.
  6. #include <linux/module.h>
  7. #include <linux/moduleparam.h>
  8. #include <linux/init.h>
  9. #include <linux/delay.h>
  10. #include <linux/slab.h>
  11. #include <linux/pm.h>
  12. #include <linux/i2c.h>
  13. #include <linux/clk.h>
  14. #include <linux/log2.h>
  15. #include <linux/regmap.h>
  16. #include <linux/regulator/driver.h>
  17. #include <linux/regulator/machine.h>
  18. #include <linux/regulator/consumer.h>
  19. #include <linux/of_device.h>
  20. #include <sound/core.h>
  21. #include <sound/tlv.h>
  22. #include <sound/pcm.h>
  23. #include <sound/pcm_params.h>
  24. #include <sound/soc.h>
  25. #include <sound/soc-dapm.h>
  26. #include <sound/initval.h>
  27. #include "sgtl5000.h"
  28. #define SGTL5000_DAP_REG_OFFSET 0x0100
  29. #define SGTL5000_MAX_REG_OFFSET 0x013A
  30. /* Delay for the VAG ramp up */
  31. #define SGTL5000_VAG_POWERUP_DELAY 500 /* ms */
  32. /* Delay for the VAG ramp down */
  33. #define SGTL5000_VAG_POWERDOWN_DELAY 500 /* ms */
  34. #define SGTL5000_OUTPUTS_MUTE (SGTL5000_HP_MUTE | SGTL5000_LINE_OUT_MUTE)
  35. /* default value of sgtl5000 registers */
  36. static const struct reg_default sgtl5000_reg_defaults[] = {
  37. { SGTL5000_CHIP_DIG_POWER, 0x0000 },
  38. { SGTL5000_CHIP_I2S_CTRL, 0x0010 },
  39. { SGTL5000_CHIP_SSS_CTRL, 0x0010 },
  40. { SGTL5000_CHIP_ADCDAC_CTRL, 0x020c },
  41. { SGTL5000_CHIP_DAC_VOL, 0x3c3c },
  42. { SGTL5000_CHIP_PAD_STRENGTH, 0x015f },
  43. { SGTL5000_CHIP_ANA_ADC_CTRL, 0x0000 },
  44. { SGTL5000_CHIP_ANA_HP_CTRL, 0x1818 },
  45. { SGTL5000_CHIP_ANA_CTRL, 0x0111 },
  46. { SGTL5000_CHIP_REF_CTRL, 0x0000 },
  47. { SGTL5000_CHIP_MIC_CTRL, 0x0000 },
  48. { SGTL5000_CHIP_LINE_OUT_CTRL, 0x0000 },
  49. { SGTL5000_CHIP_LINE_OUT_VOL, 0x0404 },
  50. { SGTL5000_CHIP_PLL_CTRL, 0x5000 },
  51. { SGTL5000_CHIP_CLK_TOP_CTRL, 0x0000 },
  52. { SGTL5000_CHIP_ANA_STATUS, 0x0000 },
  53. { SGTL5000_CHIP_SHORT_CTRL, 0x0000 },
  54. { SGTL5000_CHIP_ANA_TEST2, 0x0000 },
  55. { SGTL5000_DAP_CTRL, 0x0000 },
  56. { SGTL5000_DAP_PEQ, 0x0000 },
  57. { SGTL5000_DAP_BASS_ENHANCE, 0x0040 },
  58. { SGTL5000_DAP_BASS_ENHANCE_CTRL, 0x051f },
  59. { SGTL5000_DAP_AUDIO_EQ, 0x0000 },
  60. { SGTL5000_DAP_SURROUND, 0x0040 },
  61. { SGTL5000_DAP_EQ_BASS_BAND0, 0x002f },
  62. { SGTL5000_DAP_EQ_BASS_BAND1, 0x002f },
  63. { SGTL5000_DAP_EQ_BASS_BAND2, 0x002f },
  64. { SGTL5000_DAP_EQ_BASS_BAND3, 0x002f },
  65. { SGTL5000_DAP_EQ_BASS_BAND4, 0x002f },
  66. { SGTL5000_DAP_MAIN_CHAN, 0x8000 },
  67. { SGTL5000_DAP_MIX_CHAN, 0x0000 },
  68. { SGTL5000_DAP_AVC_CTRL, 0x5100 },
  69. { SGTL5000_DAP_AVC_THRESHOLD, 0x1473 },
  70. { SGTL5000_DAP_AVC_ATTACK, 0x0028 },
  71. { SGTL5000_DAP_AVC_DECAY, 0x0050 },
  72. };
  73. /* AVC: Threshold dB -> register: pre-calculated values */
  74. static const u16 avc_thr_db2reg[97] = {
  75. 0x5168, 0x488E, 0x40AA, 0x39A1, 0x335D, 0x2DC7, 0x28CC, 0x245D, 0x2068,
  76. 0x1CE2, 0x19BE, 0x16F1, 0x1472, 0x1239, 0x103E, 0x0E7A, 0x0CE6, 0x0B7F,
  77. 0x0A3F, 0x0922, 0x0824, 0x0741, 0x0677, 0x05C3, 0x0522, 0x0493, 0x0414,
  78. 0x03A2, 0x033D, 0x02E3, 0x0293, 0x024B, 0x020B, 0x01D2, 0x019F, 0x0172,
  79. 0x014A, 0x0126, 0x0106, 0x00E9, 0x00D0, 0x00B9, 0x00A5, 0x0093, 0x0083,
  80. 0x0075, 0x0068, 0x005D, 0x0052, 0x0049, 0x0041, 0x003A, 0x0034, 0x002E,
  81. 0x0029, 0x0025, 0x0021, 0x001D, 0x001A, 0x0017, 0x0014, 0x0012, 0x0010,
  82. 0x000E, 0x000D, 0x000B, 0x000A, 0x0009, 0x0008, 0x0007, 0x0006, 0x0005,
  83. 0x0005, 0x0004, 0x0004, 0x0003, 0x0003, 0x0002, 0x0002, 0x0002, 0x0002,
  84. 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0000, 0x0000, 0x0000,
  85. 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000};
  86. /* regulator supplies for sgtl5000, VDDD is an optional external supply */
  87. enum sgtl5000_regulator_supplies {
  88. VDDA,
  89. VDDIO,
  90. VDDD,
  91. SGTL5000_SUPPLY_NUM
  92. };
  93. /* vddd is optional supply */
  94. static const char *supply_names[SGTL5000_SUPPLY_NUM] = {
  95. "VDDA",
  96. "VDDIO",
  97. "VDDD"
  98. };
  99. #define LDO_VOLTAGE 1200000
  100. #define LINREG_VDDD ((1600 - LDO_VOLTAGE / 1000) / 50)
  101. enum sgtl5000_micbias_resistor {
  102. SGTL5000_MICBIAS_OFF = 0,
  103. SGTL5000_MICBIAS_2K = 2,
  104. SGTL5000_MICBIAS_4K = 4,
  105. SGTL5000_MICBIAS_8K = 8,
  106. };
  107. enum {
  108. I2S_LRCLK_STRENGTH_DISABLE,
  109. I2S_LRCLK_STRENGTH_LOW,
  110. I2S_LRCLK_STRENGTH_MEDIUM,
  111. I2S_LRCLK_STRENGTH_HIGH,
  112. };
  113. enum {
  114. I2S_SCLK_STRENGTH_DISABLE,
  115. I2S_SCLK_STRENGTH_LOW,
  116. I2S_SCLK_STRENGTH_MEDIUM,
  117. I2S_SCLK_STRENGTH_HIGH,
  118. };
  119. enum {
  120. HP_POWER_EVENT,
  121. DAC_POWER_EVENT,
  122. ADC_POWER_EVENT,
  123. LAST_POWER_EVENT = ADC_POWER_EVENT
  124. };
  125. /* sgtl5000 private structure in codec */
  126. struct sgtl5000_priv {
  127. int sysclk; /* sysclk rate */
  128. int master; /* i2s master or not */
  129. int fmt; /* i2s data format */
  130. struct regulator_bulk_data supplies[SGTL5000_SUPPLY_NUM];
  131. int num_supplies;
  132. struct regmap *regmap;
  133. struct clk *mclk;
  134. int revision;
  135. u8 micbias_resistor;
  136. u8 micbias_voltage;
  137. u8 lrclk_strength;
  138. u8 sclk_strength;
  139. u16 mute_state[LAST_POWER_EVENT + 1];
  140. };
  141. static inline int hp_sel_input(struct snd_soc_component *component)
  142. {
  143. return (snd_soc_component_read(component, SGTL5000_CHIP_ANA_CTRL) &
  144. SGTL5000_HP_SEL_MASK) >> SGTL5000_HP_SEL_SHIFT;
  145. }
  146. static inline u16 mute_output(struct snd_soc_component *component,
  147. u16 mute_mask)
  148. {
  149. u16 mute_reg = snd_soc_component_read(component,
  150. SGTL5000_CHIP_ANA_CTRL);
  151. snd_soc_component_update_bits(component, SGTL5000_CHIP_ANA_CTRL,
  152. mute_mask, mute_mask);
  153. return mute_reg;
  154. }
  155. static inline void restore_output(struct snd_soc_component *component,
  156. u16 mute_mask, u16 mute_reg)
  157. {
  158. snd_soc_component_update_bits(component, SGTL5000_CHIP_ANA_CTRL,
  159. mute_mask, mute_reg);
  160. }
  161. static void vag_power_on(struct snd_soc_component *component, u32 source)
  162. {
  163. if (snd_soc_component_read(component, SGTL5000_CHIP_ANA_POWER) &
  164. SGTL5000_VAG_POWERUP)
  165. return;
  166. snd_soc_component_update_bits(component, SGTL5000_CHIP_ANA_POWER,
  167. SGTL5000_VAG_POWERUP, SGTL5000_VAG_POWERUP);
  168. /* When VAG powering on to get local loop from Line-In, the sleep
  169. * is required to avoid loud pop.
  170. */
  171. if (hp_sel_input(component) == SGTL5000_HP_SEL_LINE_IN &&
  172. source == HP_POWER_EVENT)
  173. msleep(SGTL5000_VAG_POWERUP_DELAY);
  174. }
  175. static int vag_power_consumers(struct snd_soc_component *component,
  176. u16 ana_pwr_reg, u32 source)
  177. {
  178. int consumers = 0;
  179. /* count dac/adc consumers unconditional */
  180. if (ana_pwr_reg & SGTL5000_DAC_POWERUP)
  181. consumers++;
  182. if (ana_pwr_reg & SGTL5000_ADC_POWERUP)
  183. consumers++;
  184. /*
  185. * If the event comes from HP and Line-In is selected,
  186. * current action is 'DAC to be powered down'.
  187. * As HP_POWERUP is not set when HP muxed to line-in,
  188. * we need to keep VAG power ON.
  189. */
  190. if (source == HP_POWER_EVENT) {
  191. if (hp_sel_input(component) == SGTL5000_HP_SEL_LINE_IN)
  192. consumers++;
  193. } else {
  194. if (ana_pwr_reg & SGTL5000_HP_POWERUP)
  195. consumers++;
  196. }
  197. return consumers;
  198. }
  199. static void vag_power_off(struct snd_soc_component *component, u32 source)
  200. {
  201. u16 ana_pwr = snd_soc_component_read(component,
  202. SGTL5000_CHIP_ANA_POWER);
  203. if (!(ana_pwr & SGTL5000_VAG_POWERUP))
  204. return;
  205. /*
  206. * This function calls when any of VAG power consumers is disappearing.
  207. * Thus, if there is more than one consumer at the moment, as minimum
  208. * one consumer will definitely stay after the end of the current
  209. * event.
  210. * Don't clear VAG_POWERUP if 2 or more consumers of VAG present:
  211. * - LINE_IN (for HP events) / HP (for DAC/ADC events)
  212. * - DAC
  213. * - ADC
  214. * (the current consumer is disappearing right now)
  215. */
  216. if (vag_power_consumers(component, ana_pwr, source) >= 2)
  217. return;
  218. snd_soc_component_update_bits(component, SGTL5000_CHIP_ANA_POWER,
  219. SGTL5000_VAG_POWERUP, 0);
  220. /* In power down case, we need wait 400-1000 ms
  221. * when VAG fully ramped down.
  222. * As longer we wait, as smaller pop we've got.
  223. */
  224. msleep(SGTL5000_VAG_POWERDOWN_DELAY);
  225. }
  226. /*
  227. * mic_bias power on/off share the same register bits with
  228. * output impedance of mic bias, when power on mic bias, we
  229. * need reclaim it to impedance value.
  230. * 0x0 = Powered off
  231. * 0x1 = 2Kohm
  232. * 0x2 = 4Kohm
  233. * 0x3 = 8Kohm
  234. */
  235. static int mic_bias_event(struct snd_soc_dapm_widget *w,
  236. struct snd_kcontrol *kcontrol, int event)
  237. {
  238. struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
  239. struct sgtl5000_priv *sgtl5000 = snd_soc_component_get_drvdata(component);
  240. switch (event) {
  241. case SND_SOC_DAPM_POST_PMU:
  242. /* change mic bias resistor */
  243. snd_soc_component_update_bits(component, SGTL5000_CHIP_MIC_CTRL,
  244. SGTL5000_BIAS_R_MASK,
  245. sgtl5000->micbias_resistor << SGTL5000_BIAS_R_SHIFT);
  246. break;
  247. case SND_SOC_DAPM_PRE_PMD:
  248. snd_soc_component_update_bits(component, SGTL5000_CHIP_MIC_CTRL,
  249. SGTL5000_BIAS_R_MASK, 0);
  250. break;
  251. }
  252. return 0;
  253. }
  254. static int vag_and_mute_control(struct snd_soc_component *component,
  255. int event, int event_source)
  256. {
  257. static const u16 mute_mask[] = {
  258. /*
  259. * Mask for HP_POWER_EVENT.
  260. * Muxing Headphones have to be wrapped with mute/unmute
  261. * headphones only.
  262. */
  263. SGTL5000_HP_MUTE,
  264. /*
  265. * Masks for DAC_POWER_EVENT/ADC_POWER_EVENT.
  266. * Muxing DAC or ADC block have to wrapped with mute/unmute
  267. * both headphones and line-out.
  268. */
  269. SGTL5000_OUTPUTS_MUTE,
  270. SGTL5000_OUTPUTS_MUTE
  271. };
  272. struct sgtl5000_priv *sgtl5000 =
  273. snd_soc_component_get_drvdata(component);
  274. switch (event) {
  275. case SND_SOC_DAPM_PRE_PMU:
  276. sgtl5000->mute_state[event_source] =
  277. mute_output(component, mute_mask[event_source]);
  278. break;
  279. case SND_SOC_DAPM_POST_PMU:
  280. vag_power_on(component, event_source);
  281. restore_output(component, mute_mask[event_source],
  282. sgtl5000->mute_state[event_source]);
  283. break;
  284. case SND_SOC_DAPM_PRE_PMD:
  285. sgtl5000->mute_state[event_source] =
  286. mute_output(component, mute_mask[event_source]);
  287. vag_power_off(component, event_source);
  288. break;
  289. case SND_SOC_DAPM_POST_PMD:
  290. restore_output(component, mute_mask[event_source],
  291. sgtl5000->mute_state[event_source]);
  292. break;
  293. default:
  294. break;
  295. }
  296. return 0;
  297. }
  298. /*
  299. * Mute Headphone when power it up/down.
  300. * Control VAG power on HP power path.
  301. */
  302. static int headphone_pga_event(struct snd_soc_dapm_widget *w,
  303. struct snd_kcontrol *kcontrol, int event)
  304. {
  305. struct snd_soc_component *component =
  306. snd_soc_dapm_to_component(w->dapm);
  307. return vag_and_mute_control(component, event, HP_POWER_EVENT);
  308. }
  309. /* As manual describes, ADC/DAC powering up/down requires
  310. * to mute outputs to avoid pops.
  311. * Control VAG power on ADC/DAC power path.
  312. */
  313. static int adc_updown_depop(struct snd_soc_dapm_widget *w,
  314. struct snd_kcontrol *kcontrol, int event)
  315. {
  316. struct snd_soc_component *component =
  317. snd_soc_dapm_to_component(w->dapm);
  318. return vag_and_mute_control(component, event, ADC_POWER_EVENT);
  319. }
  320. static int dac_updown_depop(struct snd_soc_dapm_widget *w,
  321. struct snd_kcontrol *kcontrol, int event)
  322. {
  323. struct snd_soc_component *component =
  324. snd_soc_dapm_to_component(w->dapm);
  325. return vag_and_mute_control(component, event, DAC_POWER_EVENT);
  326. }
  327. /* input sources for ADC */
  328. static const char *adc_mux_text[] = {
  329. "MIC_IN", "LINE_IN"
  330. };
  331. static SOC_ENUM_SINGLE_DECL(adc_enum,
  332. SGTL5000_CHIP_ANA_CTRL, 2,
  333. adc_mux_text);
  334. static const struct snd_kcontrol_new adc_mux =
  335. SOC_DAPM_ENUM("Capture Mux", adc_enum);
  336. /* input sources for headphone */
  337. static const char *hp_mux_text[] = {
  338. "DAC", "LINE_IN"
  339. };
  340. static SOC_ENUM_SINGLE_DECL(hp_enum,
  341. SGTL5000_CHIP_ANA_CTRL, 6,
  342. hp_mux_text);
  343. static const struct snd_kcontrol_new hp_mux =
  344. SOC_DAPM_ENUM("Headphone Mux", hp_enum);
  345. /* input sources for DAC */
  346. static const char *dac_mux_text[] = {
  347. "ADC", "I2S", "Rsvrd", "DAP"
  348. };
  349. static SOC_ENUM_SINGLE_DECL(dac_enum,
  350. SGTL5000_CHIP_SSS_CTRL, SGTL5000_DAC_SEL_SHIFT,
  351. dac_mux_text);
  352. static const struct snd_kcontrol_new dac_mux =
  353. SOC_DAPM_ENUM("Digital Input Mux", dac_enum);
  354. /* input sources for DAP */
  355. static const char *dap_mux_text[] = {
  356. "ADC", "I2S"
  357. };
  358. static SOC_ENUM_SINGLE_DECL(dap_enum,
  359. SGTL5000_CHIP_SSS_CTRL, SGTL5000_DAP_SEL_SHIFT,
  360. dap_mux_text);
  361. static const struct snd_kcontrol_new dap_mux =
  362. SOC_DAPM_ENUM("DAP Mux", dap_enum);
  363. /* input sources for DAP mix */
  364. static const char *dapmix_mux_text[] = {
  365. "ADC", "I2S"
  366. };
  367. static SOC_ENUM_SINGLE_DECL(dapmix_enum,
  368. SGTL5000_CHIP_SSS_CTRL, SGTL5000_DAP_MIX_SEL_SHIFT,
  369. dapmix_mux_text);
  370. static const struct snd_kcontrol_new dapmix_mux =
  371. SOC_DAPM_ENUM("DAP MIX Mux", dapmix_enum);
  372. static const struct snd_soc_dapm_widget sgtl5000_dapm_widgets[] = {
  373. SND_SOC_DAPM_INPUT("LINE_IN"),
  374. SND_SOC_DAPM_INPUT("MIC_IN"),
  375. SND_SOC_DAPM_OUTPUT("HP_OUT"),
  376. SND_SOC_DAPM_OUTPUT("LINE_OUT"),
  377. SND_SOC_DAPM_SUPPLY("Mic Bias", SGTL5000_CHIP_MIC_CTRL, 8, 0,
  378. mic_bias_event,
  379. SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
  380. SND_SOC_DAPM_PGA_E("HP", SGTL5000_CHIP_ANA_POWER, 4, 0, NULL, 0,
  381. headphone_pga_event,
  382. SND_SOC_DAPM_PRE_POST_PMU |
  383. SND_SOC_DAPM_PRE_POST_PMD),
  384. SND_SOC_DAPM_PGA("LO", SGTL5000_CHIP_ANA_POWER, 0, 0, NULL, 0),
  385. SND_SOC_DAPM_MUX("Capture Mux", SND_SOC_NOPM, 0, 0, &adc_mux),
  386. SND_SOC_DAPM_MUX("Headphone Mux", SND_SOC_NOPM, 0, 0, &hp_mux),
  387. SND_SOC_DAPM_MUX("Digital Input Mux", SND_SOC_NOPM, 0, 0, &dac_mux),
  388. SND_SOC_DAPM_MUX("DAP Mux", SGTL5000_DAP_CTRL, 0, 0, &dap_mux),
  389. SND_SOC_DAPM_MUX("DAP MIX Mux", SGTL5000_DAP_CTRL, 4, 0, &dapmix_mux),
  390. SND_SOC_DAPM_MIXER("DAP", SGTL5000_CHIP_DIG_POWER, 4, 0, NULL, 0),
  391. /* aif for i2s input */
  392. SND_SOC_DAPM_AIF_IN("AIFIN", "Playback",
  393. 0, SGTL5000_CHIP_DIG_POWER,
  394. 0, 0),
  395. /* aif for i2s output */
  396. SND_SOC_DAPM_AIF_OUT("AIFOUT", "Capture",
  397. 0, SGTL5000_CHIP_DIG_POWER,
  398. 1, 0),
  399. SND_SOC_DAPM_ADC_E("ADC", "Capture", SGTL5000_CHIP_ANA_POWER, 1, 0,
  400. adc_updown_depop, SND_SOC_DAPM_PRE_POST_PMU |
  401. SND_SOC_DAPM_PRE_POST_PMD),
  402. SND_SOC_DAPM_DAC_E("DAC", "Playback", SGTL5000_CHIP_ANA_POWER, 3, 0,
  403. dac_updown_depop, SND_SOC_DAPM_PRE_POST_PMU |
  404. SND_SOC_DAPM_PRE_POST_PMD),
  405. };
  406. /* routes for sgtl5000 */
  407. static const struct snd_soc_dapm_route sgtl5000_dapm_routes[] = {
  408. {"Capture Mux", "LINE_IN", "LINE_IN"}, /* line_in --> adc_mux */
  409. {"Capture Mux", "MIC_IN", "MIC_IN"}, /* mic_in --> adc_mux */
  410. {"ADC", NULL, "Capture Mux"}, /* adc_mux --> adc */
  411. {"AIFOUT", NULL, "ADC"}, /* adc --> i2s_out */
  412. {"DAP Mux", "ADC", "ADC"}, /* adc --> DAP mux */
  413. {"DAP Mux", NULL, "AIFIN"}, /* i2s --> DAP mux */
  414. {"DAP", NULL, "DAP Mux"}, /* DAP mux --> dap */
  415. {"DAP MIX Mux", "ADC", "ADC"}, /* adc --> DAP MIX mux */
  416. {"DAP MIX Mux", NULL, "AIFIN"}, /* i2s --> DAP MIX mux */
  417. {"DAP", NULL, "DAP MIX Mux"}, /* DAP MIX mux --> dap */
  418. {"Digital Input Mux", "ADC", "ADC"}, /* adc --> audio mux */
  419. {"Digital Input Mux", NULL, "AIFIN"}, /* i2s --> audio mux */
  420. {"Digital Input Mux", NULL, "DAP"}, /* dap --> audio mux */
  421. {"DAC", NULL, "Digital Input Mux"}, /* audio mux --> dac */
  422. {"Headphone Mux", "DAC", "DAC"}, /* dac --> hp_mux */
  423. {"LO", NULL, "DAC"}, /* dac --> line_out */
  424. {"Headphone Mux", "LINE_IN", "LINE_IN"},/* line_in --> hp_mux */
  425. {"HP", NULL, "Headphone Mux"}, /* hp_mux --> hp */
  426. {"LINE_OUT", NULL, "LO"},
  427. {"HP_OUT", NULL, "HP"},
  428. };
  429. /* custom function to fetch info of PCM playback volume */
  430. static int dac_info_volsw(struct snd_kcontrol *kcontrol,
  431. struct snd_ctl_elem_info *uinfo)
  432. {
  433. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  434. uinfo->count = 2;
  435. uinfo->value.integer.min = 0;
  436. uinfo->value.integer.max = 0xfc - 0x3c;
  437. return 0;
  438. }
  439. /*
  440. * custom function to get of PCM playback volume
  441. *
  442. * dac volume register
  443. * 15-------------8-7--------------0
  444. * | R channel vol | L channel vol |
  445. * -------------------------------
  446. *
  447. * PCM volume with 0.5017 dB steps from 0 to -90 dB
  448. *
  449. * register values map to dB
  450. * 0x3B and less = Reserved
  451. * 0x3C = 0 dB
  452. * 0x3D = -0.5 dB
  453. * 0xF0 = -90 dB
  454. * 0xFC and greater = Muted
  455. *
  456. * register value map to userspace value
  457. *
  458. * register value 0x3c(0dB) 0xf0(-90dB)0xfc
  459. * ------------------------------
  460. * userspace value 0xc0 0
  461. */
  462. static int dac_get_volsw(struct snd_kcontrol *kcontrol,
  463. struct snd_ctl_elem_value *ucontrol)
  464. {
  465. struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
  466. int reg;
  467. int l;
  468. int r;
  469. reg = snd_soc_component_read(component, SGTL5000_CHIP_DAC_VOL);
  470. /* get left channel volume */
  471. l = (reg & SGTL5000_DAC_VOL_LEFT_MASK) >> SGTL5000_DAC_VOL_LEFT_SHIFT;
  472. /* get right channel volume */
  473. r = (reg & SGTL5000_DAC_VOL_RIGHT_MASK) >> SGTL5000_DAC_VOL_RIGHT_SHIFT;
  474. /* make sure value fall in (0x3c,0xfc) */
  475. l = clamp(l, 0x3c, 0xfc);
  476. r = clamp(r, 0x3c, 0xfc);
  477. /* invert it and map to userspace value */
  478. l = 0xfc - l;
  479. r = 0xfc - r;
  480. ucontrol->value.integer.value[0] = l;
  481. ucontrol->value.integer.value[1] = r;
  482. return 0;
  483. }
  484. /*
  485. * custom function to put of PCM playback volume
  486. *
  487. * dac volume register
  488. * 15-------------8-7--------------0
  489. * | R channel vol | L channel vol |
  490. * -------------------------------
  491. *
  492. * PCM volume with 0.5017 dB steps from 0 to -90 dB
  493. *
  494. * register values map to dB
  495. * 0x3B and less = Reserved
  496. * 0x3C = 0 dB
  497. * 0x3D = -0.5 dB
  498. * 0xF0 = -90 dB
  499. * 0xFC and greater = Muted
  500. *
  501. * userspace value map to register value
  502. *
  503. * userspace value 0xc0 0
  504. * ------------------------------
  505. * register value 0x3c(0dB) 0xf0(-90dB)0xfc
  506. */
  507. static int dac_put_volsw(struct snd_kcontrol *kcontrol,
  508. struct snd_ctl_elem_value *ucontrol)
  509. {
  510. struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
  511. int reg;
  512. int l;
  513. int r;
  514. l = ucontrol->value.integer.value[0];
  515. r = ucontrol->value.integer.value[1];
  516. /* make sure userspace volume fall in (0, 0xfc-0x3c) */
  517. l = clamp(l, 0, 0xfc - 0x3c);
  518. r = clamp(r, 0, 0xfc - 0x3c);
  519. /* invert it, get the value can be set to register */
  520. l = 0xfc - l;
  521. r = 0xfc - r;
  522. /* shift to get the register value */
  523. reg = l << SGTL5000_DAC_VOL_LEFT_SHIFT |
  524. r << SGTL5000_DAC_VOL_RIGHT_SHIFT;
  525. snd_soc_component_write(component, SGTL5000_CHIP_DAC_VOL, reg);
  526. return 0;
  527. }
  528. /*
  529. * custom function to get AVC threshold
  530. *
  531. * The threshold dB is calculated by rearranging the calculation from the
  532. * avc_put_threshold function: register_value = 10^(dB/20) * 0.636 * 2^15 ==>
  533. * dB = ( fls(register_value) - 14.347 ) * 6.02
  534. *
  535. * As this calculation is expensive and the threshold dB values may not exceed
  536. * 0 to 96 we use pre-calculated values.
  537. */
  538. static int avc_get_threshold(struct snd_kcontrol *kcontrol,
  539. struct snd_ctl_elem_value *ucontrol)
  540. {
  541. struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
  542. int db, i;
  543. u16 reg = snd_soc_component_read(component, SGTL5000_DAP_AVC_THRESHOLD);
  544. /* register value 0 => -96dB */
  545. if (!reg) {
  546. ucontrol->value.integer.value[0] = 96;
  547. ucontrol->value.integer.value[1] = 96;
  548. return 0;
  549. }
  550. /* get dB from register value (rounded down) */
  551. for (i = 0; avc_thr_db2reg[i] > reg; i++)
  552. ;
  553. db = i;
  554. ucontrol->value.integer.value[0] = db;
  555. ucontrol->value.integer.value[1] = db;
  556. return 0;
  557. }
  558. /*
  559. * custom function to put AVC threshold
  560. *
  561. * The register value is calculated by following formula:
  562. * register_value = 10^(dB/20) * 0.636 * 2^15
  563. * As this calculation is expensive and the threshold dB values may not exceed
  564. * 0 to 96 we use pre-calculated values.
  565. */
  566. static int avc_put_threshold(struct snd_kcontrol *kcontrol,
  567. struct snd_ctl_elem_value *ucontrol)
  568. {
  569. struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
  570. int db;
  571. u16 reg;
  572. db = (int)ucontrol->value.integer.value[0];
  573. if (db < 0 || db > 96)
  574. return -EINVAL;
  575. reg = avc_thr_db2reg[db];
  576. snd_soc_component_write(component, SGTL5000_DAP_AVC_THRESHOLD, reg);
  577. return 0;
  578. }
  579. static const DECLARE_TLV_DB_SCALE(capture_6db_attenuate, -600, 600, 0);
  580. /* tlv for mic gain, 0db 20db 30db 40db */
  581. static const DECLARE_TLV_DB_RANGE(mic_gain_tlv,
  582. 0, 0, TLV_DB_SCALE_ITEM(0, 0, 0),
  583. 1, 3, TLV_DB_SCALE_ITEM(2000, 1000, 0)
  584. );
  585. /* tlv for DAP channels, 0% - 100% - 200% */
  586. static const DECLARE_TLV_DB_SCALE(dap_volume, 0, 1, 0);
  587. /* tlv for bass bands, -11.75db to 12.0db, step .25db */
  588. static const DECLARE_TLV_DB_SCALE(bass_band, -1175, 25, 0);
  589. /* tlv for hp volume, -51.5db to 12.0db, step .5db */
  590. static const DECLARE_TLV_DB_SCALE(headphone_volume, -5150, 50, 0);
  591. /* tlv for lineout volume, 31 steps of .5db each */
  592. static const DECLARE_TLV_DB_SCALE(lineout_volume, -1550, 50, 0);
  593. /* tlv for dap avc max gain, 0db, 6db, 12db */
  594. static const DECLARE_TLV_DB_SCALE(avc_max_gain, 0, 600, 0);
  595. /* tlv for dap avc threshold, */
  596. static const DECLARE_TLV_DB_MINMAX(avc_threshold, 0, 9600);
  597. static const struct snd_kcontrol_new sgtl5000_snd_controls[] = {
  598. /* SOC_DOUBLE_S8_TLV with invert */
  599. {
  600. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  601. .name = "PCM Playback Volume",
  602. .access = SNDRV_CTL_ELEM_ACCESS_TLV_READ |
  603. SNDRV_CTL_ELEM_ACCESS_READWRITE,
  604. .info = dac_info_volsw,
  605. .get = dac_get_volsw,
  606. .put = dac_put_volsw,
  607. },
  608. SOC_DOUBLE("Capture Volume", SGTL5000_CHIP_ANA_ADC_CTRL, 0, 4, 0xf, 0),
  609. SOC_SINGLE_TLV("Capture Attenuate Switch (-6dB)",
  610. SGTL5000_CHIP_ANA_ADC_CTRL,
  611. 8, 1, 0, capture_6db_attenuate),
  612. SOC_SINGLE("Capture ZC Switch", SGTL5000_CHIP_ANA_CTRL, 1, 1, 0),
  613. SOC_SINGLE("Capture Switch", SGTL5000_CHIP_ANA_CTRL, 0, 1, 1),
  614. SOC_DOUBLE_TLV("Headphone Playback Volume",
  615. SGTL5000_CHIP_ANA_HP_CTRL,
  616. 0, 8,
  617. 0x7f, 1,
  618. headphone_volume),
  619. SOC_SINGLE("Headphone Playback Switch", SGTL5000_CHIP_ANA_CTRL,
  620. 4, 1, 1),
  621. SOC_SINGLE("Headphone Playback ZC Switch", SGTL5000_CHIP_ANA_CTRL,
  622. 5, 1, 0),
  623. SOC_SINGLE_TLV("Mic Volume", SGTL5000_CHIP_MIC_CTRL,
  624. 0, 3, 0, mic_gain_tlv),
  625. SOC_DOUBLE_TLV("Lineout Playback Volume",
  626. SGTL5000_CHIP_LINE_OUT_VOL,
  627. SGTL5000_LINE_OUT_VOL_LEFT_SHIFT,
  628. SGTL5000_LINE_OUT_VOL_RIGHT_SHIFT,
  629. 0x1f, 1,
  630. lineout_volume),
  631. SOC_SINGLE("Lineout Playback Switch", SGTL5000_CHIP_ANA_CTRL, 8, 1, 1),
  632. SOC_SINGLE_TLV("DAP Main channel", SGTL5000_DAP_MAIN_CHAN,
  633. 0, 0xffff, 0, dap_volume),
  634. SOC_SINGLE_TLV("DAP Mix channel", SGTL5000_DAP_MIX_CHAN,
  635. 0, 0xffff, 0, dap_volume),
  636. /* Automatic Volume Control (DAP AVC) */
  637. SOC_SINGLE("AVC Switch", SGTL5000_DAP_AVC_CTRL, 0, 1, 0),
  638. SOC_SINGLE("AVC Hard Limiter Switch", SGTL5000_DAP_AVC_CTRL, 5, 1, 0),
  639. SOC_SINGLE_TLV("AVC Max Gain Volume", SGTL5000_DAP_AVC_CTRL, 12, 2, 0,
  640. avc_max_gain),
  641. SOC_SINGLE("AVC Integrator Response", SGTL5000_DAP_AVC_CTRL, 8, 3, 0),
  642. SOC_SINGLE_EXT_TLV("AVC Threshold Volume", SGTL5000_DAP_AVC_THRESHOLD,
  643. 0, 96, 0, avc_get_threshold, avc_put_threshold,
  644. avc_threshold),
  645. SOC_SINGLE_TLV("BASS 0", SGTL5000_DAP_EQ_BASS_BAND0,
  646. 0, 0x5F, 0, bass_band),
  647. SOC_SINGLE_TLV("BASS 1", SGTL5000_DAP_EQ_BASS_BAND1,
  648. 0, 0x5F, 0, bass_band),
  649. SOC_SINGLE_TLV("BASS 2", SGTL5000_DAP_EQ_BASS_BAND2,
  650. 0, 0x5F, 0, bass_band),
  651. SOC_SINGLE_TLV("BASS 3", SGTL5000_DAP_EQ_BASS_BAND3,
  652. 0, 0x5F, 0, bass_band),
  653. SOC_SINGLE_TLV("BASS 4", SGTL5000_DAP_EQ_BASS_BAND4,
  654. 0, 0x5F, 0, bass_band),
  655. };
  656. /* mute the codec used by alsa core */
  657. static int sgtl5000_mute_stream(struct snd_soc_dai *codec_dai, int mute, int direction)
  658. {
  659. struct snd_soc_component *component = codec_dai->component;
  660. u16 i2s_pwr = SGTL5000_I2S_IN_POWERUP;
  661. /*
  662. * During 'digital mute' do not mute DAC
  663. * because LINE_IN would be muted aswell. We want to mute
  664. * only I2S block - this can be done by powering it off
  665. */
  666. snd_soc_component_update_bits(component, SGTL5000_CHIP_DIG_POWER,
  667. i2s_pwr, mute ? 0 : i2s_pwr);
  668. return 0;
  669. }
  670. /* set codec format */
  671. static int sgtl5000_set_dai_fmt(struct snd_soc_dai *codec_dai, unsigned int fmt)
  672. {
  673. struct snd_soc_component *component = codec_dai->component;
  674. struct sgtl5000_priv *sgtl5000 = snd_soc_component_get_drvdata(component);
  675. u16 i2sctl = 0;
  676. sgtl5000->master = 0;
  677. /*
  678. * i2s clock and frame master setting.
  679. * ONLY support:
  680. * - clock and frame slave,
  681. * - clock and frame master
  682. */
  683. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  684. case SND_SOC_DAIFMT_CBS_CFS:
  685. break;
  686. case SND_SOC_DAIFMT_CBM_CFM:
  687. i2sctl |= SGTL5000_I2S_MASTER;
  688. sgtl5000->master = 1;
  689. break;
  690. default:
  691. return -EINVAL;
  692. }
  693. /* setting i2s data format */
  694. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  695. case SND_SOC_DAIFMT_DSP_A:
  696. i2sctl |= SGTL5000_I2S_MODE_PCM << SGTL5000_I2S_MODE_SHIFT;
  697. break;
  698. case SND_SOC_DAIFMT_DSP_B:
  699. i2sctl |= SGTL5000_I2S_MODE_PCM << SGTL5000_I2S_MODE_SHIFT;
  700. i2sctl |= SGTL5000_I2S_LRALIGN;
  701. break;
  702. case SND_SOC_DAIFMT_I2S:
  703. i2sctl |= SGTL5000_I2S_MODE_I2S_LJ << SGTL5000_I2S_MODE_SHIFT;
  704. break;
  705. case SND_SOC_DAIFMT_RIGHT_J:
  706. i2sctl |= SGTL5000_I2S_MODE_RJ << SGTL5000_I2S_MODE_SHIFT;
  707. i2sctl |= SGTL5000_I2S_LRPOL;
  708. break;
  709. case SND_SOC_DAIFMT_LEFT_J:
  710. i2sctl |= SGTL5000_I2S_MODE_I2S_LJ << SGTL5000_I2S_MODE_SHIFT;
  711. i2sctl |= SGTL5000_I2S_LRALIGN;
  712. break;
  713. default:
  714. return -EINVAL;
  715. }
  716. sgtl5000->fmt = fmt & SND_SOC_DAIFMT_FORMAT_MASK;
  717. /* Clock inversion */
  718. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  719. case SND_SOC_DAIFMT_NB_NF:
  720. break;
  721. case SND_SOC_DAIFMT_IB_NF:
  722. i2sctl |= SGTL5000_I2S_SCLK_INV;
  723. break;
  724. default:
  725. return -EINVAL;
  726. }
  727. snd_soc_component_write(component, SGTL5000_CHIP_I2S_CTRL, i2sctl);
  728. return 0;
  729. }
  730. /* set codec sysclk */
  731. static int sgtl5000_set_dai_sysclk(struct snd_soc_dai *codec_dai,
  732. int clk_id, unsigned int freq, int dir)
  733. {
  734. struct snd_soc_component *component = codec_dai->component;
  735. struct sgtl5000_priv *sgtl5000 = snd_soc_component_get_drvdata(component);
  736. switch (clk_id) {
  737. case SGTL5000_SYSCLK:
  738. sgtl5000->sysclk = freq;
  739. break;
  740. default:
  741. return -EINVAL;
  742. }
  743. return 0;
  744. }
  745. /*
  746. * set clock according to i2s frame clock,
  747. * sgtl5000 provides 2 clock sources:
  748. * 1. sys_mclk: sample freq can only be configured to
  749. * 1/256, 1/384, 1/512 of sys_mclk.
  750. * 2. pll: can derive any audio clocks.
  751. *
  752. * clock setting rules:
  753. * 1. in slave mode, only sys_mclk can be used
  754. * 2. as constraint by sys_mclk, sample freq should be set to 32 kHz, 44.1 kHz
  755. * and above.
  756. * 3. usage of sys_mclk is preferred over pll to save power.
  757. */
  758. static int sgtl5000_set_clock(struct snd_soc_component *component, int frame_rate)
  759. {
  760. struct sgtl5000_priv *sgtl5000 = snd_soc_component_get_drvdata(component);
  761. int clk_ctl = 0;
  762. int sys_fs; /* sample freq */
  763. /*
  764. * sample freq should be divided by frame clock,
  765. * if frame clock is lower than 44.1 kHz, sample freq should be set to
  766. * 32 kHz or 44.1 kHz.
  767. */
  768. switch (frame_rate) {
  769. case 8000:
  770. case 16000:
  771. sys_fs = 32000;
  772. break;
  773. case 11025:
  774. case 22050:
  775. sys_fs = 44100;
  776. break;
  777. default:
  778. sys_fs = frame_rate;
  779. break;
  780. }
  781. /* set divided factor of frame clock */
  782. switch (sys_fs / frame_rate) {
  783. case 4:
  784. clk_ctl |= SGTL5000_RATE_MODE_DIV_4 << SGTL5000_RATE_MODE_SHIFT;
  785. break;
  786. case 2:
  787. clk_ctl |= SGTL5000_RATE_MODE_DIV_2 << SGTL5000_RATE_MODE_SHIFT;
  788. break;
  789. case 1:
  790. clk_ctl |= SGTL5000_RATE_MODE_DIV_1 << SGTL5000_RATE_MODE_SHIFT;
  791. break;
  792. default:
  793. return -EINVAL;
  794. }
  795. /* set the sys_fs according to frame rate */
  796. switch (sys_fs) {
  797. case 32000:
  798. clk_ctl |= SGTL5000_SYS_FS_32k << SGTL5000_SYS_FS_SHIFT;
  799. break;
  800. case 44100:
  801. clk_ctl |= SGTL5000_SYS_FS_44_1k << SGTL5000_SYS_FS_SHIFT;
  802. break;
  803. case 48000:
  804. clk_ctl |= SGTL5000_SYS_FS_48k << SGTL5000_SYS_FS_SHIFT;
  805. break;
  806. case 96000:
  807. clk_ctl |= SGTL5000_SYS_FS_96k << SGTL5000_SYS_FS_SHIFT;
  808. break;
  809. default:
  810. dev_err(component->dev, "frame rate %d not supported\n",
  811. frame_rate);
  812. return -EINVAL;
  813. }
  814. /*
  815. * calculate the divider of mclk/sample_freq,
  816. * factor of freq = 96 kHz can only be 256, since mclk is in the range
  817. * of 8 MHz - 27 MHz
  818. */
  819. switch (sgtl5000->sysclk / frame_rate) {
  820. case 256:
  821. clk_ctl |= SGTL5000_MCLK_FREQ_256FS <<
  822. SGTL5000_MCLK_FREQ_SHIFT;
  823. break;
  824. case 384:
  825. clk_ctl |= SGTL5000_MCLK_FREQ_384FS <<
  826. SGTL5000_MCLK_FREQ_SHIFT;
  827. break;
  828. case 512:
  829. clk_ctl |= SGTL5000_MCLK_FREQ_512FS <<
  830. SGTL5000_MCLK_FREQ_SHIFT;
  831. break;
  832. default:
  833. /* if mclk does not satisfy the divider, use pll */
  834. if (sgtl5000->master) {
  835. clk_ctl |= SGTL5000_MCLK_FREQ_PLL <<
  836. SGTL5000_MCLK_FREQ_SHIFT;
  837. } else {
  838. dev_err(component->dev,
  839. "PLL not supported in slave mode\n");
  840. dev_err(component->dev, "%d ratio is not supported. "
  841. "SYS_MCLK needs to be 256, 384 or 512 * fs\n",
  842. sgtl5000->sysclk / frame_rate);
  843. return -EINVAL;
  844. }
  845. }
  846. /* if using pll, please check manual 6.4.2 for detail */
  847. if ((clk_ctl & SGTL5000_MCLK_FREQ_MASK) == SGTL5000_MCLK_FREQ_PLL) {
  848. u64 out, t;
  849. int div2;
  850. int pll_ctl;
  851. unsigned int in, int_div, frac_div;
  852. if (sgtl5000->sysclk > 17000000) {
  853. div2 = 1;
  854. in = sgtl5000->sysclk / 2;
  855. } else {
  856. div2 = 0;
  857. in = sgtl5000->sysclk;
  858. }
  859. if (sys_fs == 44100)
  860. out = 180633600;
  861. else
  862. out = 196608000;
  863. t = do_div(out, in);
  864. int_div = out;
  865. t *= 2048;
  866. do_div(t, in);
  867. frac_div = t;
  868. pll_ctl = int_div << SGTL5000_PLL_INT_DIV_SHIFT |
  869. frac_div << SGTL5000_PLL_FRAC_DIV_SHIFT;
  870. snd_soc_component_write(component, SGTL5000_CHIP_PLL_CTRL, pll_ctl);
  871. if (div2)
  872. snd_soc_component_update_bits(component,
  873. SGTL5000_CHIP_CLK_TOP_CTRL,
  874. SGTL5000_INPUT_FREQ_DIV2,
  875. SGTL5000_INPUT_FREQ_DIV2);
  876. else
  877. snd_soc_component_update_bits(component,
  878. SGTL5000_CHIP_CLK_TOP_CTRL,
  879. SGTL5000_INPUT_FREQ_DIV2,
  880. 0);
  881. /* power up pll */
  882. snd_soc_component_update_bits(component, SGTL5000_CHIP_ANA_POWER,
  883. SGTL5000_PLL_POWERUP | SGTL5000_VCOAMP_POWERUP,
  884. SGTL5000_PLL_POWERUP | SGTL5000_VCOAMP_POWERUP);
  885. /* if using pll, clk_ctrl must be set after pll power up */
  886. snd_soc_component_write(component, SGTL5000_CHIP_CLK_CTRL, clk_ctl);
  887. } else {
  888. /* otherwise, clk_ctrl must be set before pll power down */
  889. snd_soc_component_write(component, SGTL5000_CHIP_CLK_CTRL, clk_ctl);
  890. /* power down pll */
  891. snd_soc_component_update_bits(component, SGTL5000_CHIP_ANA_POWER,
  892. SGTL5000_PLL_POWERUP | SGTL5000_VCOAMP_POWERUP,
  893. 0);
  894. }
  895. return 0;
  896. }
  897. /*
  898. * Set PCM DAI bit size and sample rate.
  899. * input: params_rate, params_fmt
  900. */
  901. static int sgtl5000_pcm_hw_params(struct snd_pcm_substream *substream,
  902. struct snd_pcm_hw_params *params,
  903. struct snd_soc_dai *dai)
  904. {
  905. struct snd_soc_component *component = dai->component;
  906. struct sgtl5000_priv *sgtl5000 = snd_soc_component_get_drvdata(component);
  907. int channels = params_channels(params);
  908. int i2s_ctl = 0;
  909. int stereo;
  910. int ret;
  911. /* sysclk should already set */
  912. if (!sgtl5000->sysclk) {
  913. dev_err(component->dev, "%s: set sysclk first!\n", __func__);
  914. return -EFAULT;
  915. }
  916. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  917. stereo = SGTL5000_DAC_STEREO;
  918. else
  919. stereo = SGTL5000_ADC_STEREO;
  920. /* set mono to save power */
  921. snd_soc_component_update_bits(component, SGTL5000_CHIP_ANA_POWER, stereo,
  922. channels == 1 ? 0 : stereo);
  923. /* set codec clock base on lrclk */
  924. ret = sgtl5000_set_clock(component, params_rate(params));
  925. if (ret)
  926. return ret;
  927. /* set i2s data format */
  928. switch (params_width(params)) {
  929. case 16:
  930. if (sgtl5000->fmt == SND_SOC_DAIFMT_RIGHT_J)
  931. return -EINVAL;
  932. i2s_ctl |= SGTL5000_I2S_DLEN_16 << SGTL5000_I2S_DLEN_SHIFT;
  933. i2s_ctl |= SGTL5000_I2S_SCLKFREQ_32FS <<
  934. SGTL5000_I2S_SCLKFREQ_SHIFT;
  935. break;
  936. case 20:
  937. i2s_ctl |= SGTL5000_I2S_DLEN_20 << SGTL5000_I2S_DLEN_SHIFT;
  938. i2s_ctl |= SGTL5000_I2S_SCLKFREQ_64FS <<
  939. SGTL5000_I2S_SCLKFREQ_SHIFT;
  940. break;
  941. case 24:
  942. i2s_ctl |= SGTL5000_I2S_DLEN_24 << SGTL5000_I2S_DLEN_SHIFT;
  943. i2s_ctl |= SGTL5000_I2S_SCLKFREQ_64FS <<
  944. SGTL5000_I2S_SCLKFREQ_SHIFT;
  945. break;
  946. case 32:
  947. if (sgtl5000->fmt == SND_SOC_DAIFMT_RIGHT_J)
  948. return -EINVAL;
  949. i2s_ctl |= SGTL5000_I2S_DLEN_32 << SGTL5000_I2S_DLEN_SHIFT;
  950. i2s_ctl |= SGTL5000_I2S_SCLKFREQ_64FS <<
  951. SGTL5000_I2S_SCLKFREQ_SHIFT;
  952. break;
  953. default:
  954. return -EINVAL;
  955. }
  956. snd_soc_component_update_bits(component, SGTL5000_CHIP_I2S_CTRL,
  957. SGTL5000_I2S_DLEN_MASK | SGTL5000_I2S_SCLKFREQ_MASK,
  958. i2s_ctl);
  959. return 0;
  960. }
  961. /*
  962. * set dac bias
  963. * common state changes:
  964. * startup:
  965. * off --> standby --> prepare --> on
  966. * standby --> prepare --> on
  967. *
  968. * stop:
  969. * on --> prepare --> standby
  970. */
  971. static int sgtl5000_set_bias_level(struct snd_soc_component *component,
  972. enum snd_soc_bias_level level)
  973. {
  974. struct sgtl5000_priv *sgtl = snd_soc_component_get_drvdata(component);
  975. int ret;
  976. switch (level) {
  977. case SND_SOC_BIAS_ON:
  978. case SND_SOC_BIAS_PREPARE:
  979. case SND_SOC_BIAS_STANDBY:
  980. regcache_cache_only(sgtl->regmap, false);
  981. ret = regcache_sync(sgtl->regmap);
  982. if (ret) {
  983. regcache_cache_only(sgtl->regmap, true);
  984. return ret;
  985. }
  986. snd_soc_component_update_bits(component, SGTL5000_CHIP_ANA_POWER,
  987. SGTL5000_REFTOP_POWERUP,
  988. SGTL5000_REFTOP_POWERUP);
  989. break;
  990. case SND_SOC_BIAS_OFF:
  991. regcache_cache_only(sgtl->regmap, true);
  992. snd_soc_component_update_bits(component, SGTL5000_CHIP_ANA_POWER,
  993. SGTL5000_REFTOP_POWERUP, 0);
  994. break;
  995. }
  996. return 0;
  997. }
  998. #define SGTL5000_FORMATS (SNDRV_PCM_FMTBIT_S16_LE |\
  999. SNDRV_PCM_FMTBIT_S20_3LE |\
  1000. SNDRV_PCM_FMTBIT_S24_LE |\
  1001. SNDRV_PCM_FMTBIT_S32_LE)
  1002. static const struct snd_soc_dai_ops sgtl5000_ops = {
  1003. .hw_params = sgtl5000_pcm_hw_params,
  1004. .mute_stream = sgtl5000_mute_stream,
  1005. .set_fmt = sgtl5000_set_dai_fmt,
  1006. .set_sysclk = sgtl5000_set_dai_sysclk,
  1007. .no_capture_mute = 1,
  1008. };
  1009. static struct snd_soc_dai_driver sgtl5000_dai = {
  1010. .name = "sgtl5000",
  1011. .playback = {
  1012. .stream_name = "Playback",
  1013. .channels_min = 1,
  1014. .channels_max = 2,
  1015. /*
  1016. * only support 8~48K + 96K,
  1017. * TODO modify hw_param to support more
  1018. */
  1019. .rates = SNDRV_PCM_RATE_8000_48000 | SNDRV_PCM_RATE_96000,
  1020. .formats = SGTL5000_FORMATS,
  1021. },
  1022. .capture = {
  1023. .stream_name = "Capture",
  1024. .channels_min = 1,
  1025. .channels_max = 2,
  1026. .rates = SNDRV_PCM_RATE_8000_48000 | SNDRV_PCM_RATE_96000,
  1027. .formats = SGTL5000_FORMATS,
  1028. },
  1029. .ops = &sgtl5000_ops,
  1030. .symmetric_rate = 1,
  1031. };
  1032. static bool sgtl5000_volatile(struct device *dev, unsigned int reg)
  1033. {
  1034. switch (reg) {
  1035. case SGTL5000_CHIP_ID:
  1036. case SGTL5000_CHIP_ADCDAC_CTRL:
  1037. case SGTL5000_CHIP_ANA_STATUS:
  1038. return true;
  1039. }
  1040. return false;
  1041. }
  1042. static bool sgtl5000_readable(struct device *dev, unsigned int reg)
  1043. {
  1044. switch (reg) {
  1045. case SGTL5000_CHIP_ID:
  1046. case SGTL5000_CHIP_DIG_POWER:
  1047. case SGTL5000_CHIP_CLK_CTRL:
  1048. case SGTL5000_CHIP_I2S_CTRL:
  1049. case SGTL5000_CHIP_SSS_CTRL:
  1050. case SGTL5000_CHIP_ADCDAC_CTRL:
  1051. case SGTL5000_CHIP_DAC_VOL:
  1052. case SGTL5000_CHIP_PAD_STRENGTH:
  1053. case SGTL5000_CHIP_ANA_ADC_CTRL:
  1054. case SGTL5000_CHIP_ANA_HP_CTRL:
  1055. case SGTL5000_CHIP_ANA_CTRL:
  1056. case SGTL5000_CHIP_LINREG_CTRL:
  1057. case SGTL5000_CHIP_REF_CTRL:
  1058. case SGTL5000_CHIP_MIC_CTRL:
  1059. case SGTL5000_CHIP_LINE_OUT_CTRL:
  1060. case SGTL5000_CHIP_LINE_OUT_VOL:
  1061. case SGTL5000_CHIP_ANA_POWER:
  1062. case SGTL5000_CHIP_PLL_CTRL:
  1063. case SGTL5000_CHIP_CLK_TOP_CTRL:
  1064. case SGTL5000_CHIP_ANA_STATUS:
  1065. case SGTL5000_CHIP_SHORT_CTRL:
  1066. case SGTL5000_CHIP_ANA_TEST2:
  1067. case SGTL5000_DAP_CTRL:
  1068. case SGTL5000_DAP_PEQ:
  1069. case SGTL5000_DAP_BASS_ENHANCE:
  1070. case SGTL5000_DAP_BASS_ENHANCE_CTRL:
  1071. case SGTL5000_DAP_AUDIO_EQ:
  1072. case SGTL5000_DAP_SURROUND:
  1073. case SGTL5000_DAP_FLT_COEF_ACCESS:
  1074. case SGTL5000_DAP_COEF_WR_B0_MSB:
  1075. case SGTL5000_DAP_COEF_WR_B0_LSB:
  1076. case SGTL5000_DAP_EQ_BASS_BAND0:
  1077. case SGTL5000_DAP_EQ_BASS_BAND1:
  1078. case SGTL5000_DAP_EQ_BASS_BAND2:
  1079. case SGTL5000_DAP_EQ_BASS_BAND3:
  1080. case SGTL5000_DAP_EQ_BASS_BAND4:
  1081. case SGTL5000_DAP_MAIN_CHAN:
  1082. case SGTL5000_DAP_MIX_CHAN:
  1083. case SGTL5000_DAP_AVC_CTRL:
  1084. case SGTL5000_DAP_AVC_THRESHOLD:
  1085. case SGTL5000_DAP_AVC_ATTACK:
  1086. case SGTL5000_DAP_AVC_DECAY:
  1087. case SGTL5000_DAP_COEF_WR_B1_MSB:
  1088. case SGTL5000_DAP_COEF_WR_B1_LSB:
  1089. case SGTL5000_DAP_COEF_WR_B2_MSB:
  1090. case SGTL5000_DAP_COEF_WR_B2_LSB:
  1091. case SGTL5000_DAP_COEF_WR_A1_MSB:
  1092. case SGTL5000_DAP_COEF_WR_A1_LSB:
  1093. case SGTL5000_DAP_COEF_WR_A2_MSB:
  1094. case SGTL5000_DAP_COEF_WR_A2_LSB:
  1095. return true;
  1096. default:
  1097. return false;
  1098. }
  1099. }
  1100. /*
  1101. * This precalculated table contains all (vag_val * 100 / lo_calcntrl) results
  1102. * to select an appropriate lo_vol_* in SGTL5000_CHIP_LINE_OUT_VOL
  1103. * The calculatation was done for all possible register values which
  1104. * is the array index and the following formula: 10^((idx−15)/40) * 100
  1105. */
  1106. static const u8 vol_quot_table[] = {
  1107. 42, 45, 47, 50, 53, 56, 60, 63,
  1108. 67, 71, 75, 79, 84, 89, 94, 100,
  1109. 106, 112, 119, 126, 133, 141, 150, 158,
  1110. 168, 178, 188, 200, 211, 224, 237, 251
  1111. };
  1112. /*
  1113. * sgtl5000 has 3 internal power supplies:
  1114. * 1. VAG, normally set to vdda/2
  1115. * 2. charge pump, set to different value
  1116. * according to voltage of vdda and vddio
  1117. * 3. line out VAG, normally set to vddio/2
  1118. *
  1119. * and should be set according to:
  1120. * 1. vddd provided by external or not
  1121. * 2. vdda and vddio voltage value. > 3.1v or not
  1122. */
  1123. static int sgtl5000_set_power_regs(struct snd_soc_component *component)
  1124. {
  1125. int vddd;
  1126. int vdda;
  1127. int vddio;
  1128. u16 ana_pwr;
  1129. u16 lreg_ctrl;
  1130. int vag;
  1131. int lo_vag;
  1132. int vol_quot;
  1133. int lo_vol;
  1134. size_t i;
  1135. struct sgtl5000_priv *sgtl5000 = snd_soc_component_get_drvdata(component);
  1136. vdda = regulator_get_voltage(sgtl5000->supplies[VDDA].consumer);
  1137. vddio = regulator_get_voltage(sgtl5000->supplies[VDDIO].consumer);
  1138. vddd = (sgtl5000->num_supplies > VDDD)
  1139. ? regulator_get_voltage(sgtl5000->supplies[VDDD].consumer)
  1140. : LDO_VOLTAGE;
  1141. vdda = vdda / 1000;
  1142. vddio = vddio / 1000;
  1143. vddd = vddd / 1000;
  1144. if (vdda <= 0 || vddio <= 0 || vddd < 0) {
  1145. dev_err(component->dev, "regulator voltage not set correctly\n");
  1146. return -EINVAL;
  1147. }
  1148. /* according to datasheet, maximum voltage of supplies */
  1149. if (vdda > 3600 || vddio > 3600 || vddd > 1980) {
  1150. dev_err(component->dev,
  1151. "exceed max voltage vdda %dmV vddio %dmV vddd %dmV\n",
  1152. vdda, vddio, vddd);
  1153. return -EINVAL;
  1154. }
  1155. /* reset value */
  1156. ana_pwr = snd_soc_component_read(component, SGTL5000_CHIP_ANA_POWER);
  1157. ana_pwr |= SGTL5000_DAC_STEREO |
  1158. SGTL5000_ADC_STEREO |
  1159. SGTL5000_REFTOP_POWERUP;
  1160. lreg_ctrl = snd_soc_component_read(component, SGTL5000_CHIP_LINREG_CTRL);
  1161. if (vddio < 3100 && vdda < 3100) {
  1162. /* enable internal oscillator used for charge pump */
  1163. snd_soc_component_update_bits(component, SGTL5000_CHIP_CLK_TOP_CTRL,
  1164. SGTL5000_INT_OSC_EN,
  1165. SGTL5000_INT_OSC_EN);
  1166. /* Enable VDDC charge pump */
  1167. ana_pwr |= SGTL5000_VDDC_CHRGPMP_POWERUP;
  1168. } else {
  1169. ana_pwr &= ~SGTL5000_VDDC_CHRGPMP_POWERUP;
  1170. /*
  1171. * if vddio == vdda the source of charge pump should be
  1172. * assigned manually to VDDIO
  1173. */
  1174. if (regulator_is_equal(sgtl5000->supplies[VDDA].consumer,
  1175. sgtl5000->supplies[VDDIO].consumer)) {
  1176. lreg_ctrl |= SGTL5000_VDDC_ASSN_OVRD;
  1177. lreg_ctrl |= SGTL5000_VDDC_MAN_ASSN_VDDIO <<
  1178. SGTL5000_VDDC_MAN_ASSN_SHIFT;
  1179. }
  1180. }
  1181. snd_soc_component_write(component, SGTL5000_CHIP_LINREG_CTRL, lreg_ctrl);
  1182. snd_soc_component_write(component, SGTL5000_CHIP_ANA_POWER, ana_pwr);
  1183. /*
  1184. * set ADC/DAC VAG to vdda / 2,
  1185. * should stay in range (0.8v, 1.575v)
  1186. */
  1187. vag = vdda / 2;
  1188. if (vag <= SGTL5000_ANA_GND_BASE)
  1189. vag = 0;
  1190. else if (vag >= SGTL5000_ANA_GND_BASE + SGTL5000_ANA_GND_STP *
  1191. (SGTL5000_ANA_GND_MASK >> SGTL5000_ANA_GND_SHIFT))
  1192. vag = SGTL5000_ANA_GND_MASK >> SGTL5000_ANA_GND_SHIFT;
  1193. else
  1194. vag = (vag - SGTL5000_ANA_GND_BASE) / SGTL5000_ANA_GND_STP;
  1195. snd_soc_component_update_bits(component, SGTL5000_CHIP_REF_CTRL,
  1196. SGTL5000_ANA_GND_MASK, vag << SGTL5000_ANA_GND_SHIFT);
  1197. /* set line out VAG to vddio / 2, in range (0.8v, 1.675v) */
  1198. lo_vag = vddio / 2;
  1199. if (lo_vag <= SGTL5000_LINE_OUT_GND_BASE)
  1200. lo_vag = 0;
  1201. else if (lo_vag >= SGTL5000_LINE_OUT_GND_BASE +
  1202. SGTL5000_LINE_OUT_GND_STP * SGTL5000_LINE_OUT_GND_MAX)
  1203. lo_vag = SGTL5000_LINE_OUT_GND_MAX;
  1204. else
  1205. lo_vag = (lo_vag - SGTL5000_LINE_OUT_GND_BASE) /
  1206. SGTL5000_LINE_OUT_GND_STP;
  1207. snd_soc_component_update_bits(component, SGTL5000_CHIP_LINE_OUT_CTRL,
  1208. SGTL5000_LINE_OUT_CURRENT_MASK |
  1209. SGTL5000_LINE_OUT_GND_MASK,
  1210. lo_vag << SGTL5000_LINE_OUT_GND_SHIFT |
  1211. SGTL5000_LINE_OUT_CURRENT_360u <<
  1212. SGTL5000_LINE_OUT_CURRENT_SHIFT);
  1213. /*
  1214. * Set lineout output level in range (0..31)
  1215. * the same value is used for right and left channel
  1216. *
  1217. * Searching for a suitable index solving this formula:
  1218. * idx = 40 * log10(vag_val / lo_cagcntrl) + 15
  1219. */
  1220. vol_quot = lo_vag ? (vag * 100) / lo_vag : 0;
  1221. lo_vol = 0;
  1222. for (i = 0; i < ARRAY_SIZE(vol_quot_table); i++) {
  1223. if (vol_quot >= vol_quot_table[i])
  1224. lo_vol = i;
  1225. else
  1226. break;
  1227. }
  1228. snd_soc_component_update_bits(component, SGTL5000_CHIP_LINE_OUT_VOL,
  1229. SGTL5000_LINE_OUT_VOL_RIGHT_MASK |
  1230. SGTL5000_LINE_OUT_VOL_LEFT_MASK,
  1231. lo_vol << SGTL5000_LINE_OUT_VOL_RIGHT_SHIFT |
  1232. lo_vol << SGTL5000_LINE_OUT_VOL_LEFT_SHIFT);
  1233. return 0;
  1234. }
  1235. static int sgtl5000_enable_regulators(struct i2c_client *client)
  1236. {
  1237. int ret;
  1238. int i;
  1239. int external_vddd = 0;
  1240. struct regulator *vddd;
  1241. struct sgtl5000_priv *sgtl5000 = i2c_get_clientdata(client);
  1242. for (i = 0; i < ARRAY_SIZE(sgtl5000->supplies); i++)
  1243. sgtl5000->supplies[i].supply = supply_names[i];
  1244. vddd = regulator_get_optional(&client->dev, "VDDD");
  1245. if (IS_ERR(vddd)) {
  1246. /* See if it's just not registered yet */
  1247. if (PTR_ERR(vddd) == -EPROBE_DEFER)
  1248. return -EPROBE_DEFER;
  1249. } else {
  1250. external_vddd = 1;
  1251. regulator_put(vddd);
  1252. }
  1253. sgtl5000->num_supplies = ARRAY_SIZE(sgtl5000->supplies)
  1254. - 1 + external_vddd;
  1255. ret = regulator_bulk_get(&client->dev, sgtl5000->num_supplies,
  1256. sgtl5000->supplies);
  1257. if (ret)
  1258. return ret;
  1259. ret = regulator_bulk_enable(sgtl5000->num_supplies,
  1260. sgtl5000->supplies);
  1261. if (!ret)
  1262. usleep_range(10, 20);
  1263. else
  1264. regulator_bulk_free(sgtl5000->num_supplies,
  1265. sgtl5000->supplies);
  1266. return ret;
  1267. }
  1268. static int sgtl5000_probe(struct snd_soc_component *component)
  1269. {
  1270. int ret;
  1271. u16 reg;
  1272. struct sgtl5000_priv *sgtl5000 = snd_soc_component_get_drvdata(component);
  1273. unsigned int zcd_mask = SGTL5000_HP_ZCD_EN | SGTL5000_ADC_ZCD_EN;
  1274. /* power up sgtl5000 */
  1275. ret = sgtl5000_set_power_regs(component);
  1276. if (ret)
  1277. goto err;
  1278. /* enable small pop, introduce 400ms delay in turning off */
  1279. snd_soc_component_update_bits(component, SGTL5000_CHIP_REF_CTRL,
  1280. SGTL5000_SMALL_POP, SGTL5000_SMALL_POP);
  1281. /* disable short cut detector */
  1282. snd_soc_component_write(component, SGTL5000_CHIP_SHORT_CTRL, 0);
  1283. snd_soc_component_write(component, SGTL5000_CHIP_DIG_POWER,
  1284. SGTL5000_ADC_EN | SGTL5000_DAC_EN);
  1285. /* enable dac volume ramp by default */
  1286. snd_soc_component_write(component, SGTL5000_CHIP_ADCDAC_CTRL,
  1287. SGTL5000_DAC_VOL_RAMP_EN |
  1288. SGTL5000_DAC_MUTE_RIGHT |
  1289. SGTL5000_DAC_MUTE_LEFT);
  1290. reg = ((sgtl5000->lrclk_strength) << SGTL5000_PAD_I2S_LRCLK_SHIFT |
  1291. (sgtl5000->sclk_strength) << SGTL5000_PAD_I2S_SCLK_SHIFT |
  1292. 0x1f);
  1293. snd_soc_component_write(component, SGTL5000_CHIP_PAD_STRENGTH, reg);
  1294. snd_soc_component_update_bits(component, SGTL5000_CHIP_ANA_CTRL,
  1295. zcd_mask, zcd_mask);
  1296. snd_soc_component_update_bits(component, SGTL5000_CHIP_MIC_CTRL,
  1297. SGTL5000_BIAS_R_MASK,
  1298. sgtl5000->micbias_resistor << SGTL5000_BIAS_R_SHIFT);
  1299. snd_soc_component_update_bits(component, SGTL5000_CHIP_MIC_CTRL,
  1300. SGTL5000_BIAS_VOLT_MASK,
  1301. sgtl5000->micbias_voltage << SGTL5000_BIAS_VOLT_SHIFT);
  1302. /*
  1303. * enable DAP Graphic EQ
  1304. * TODO:
  1305. * Add control for changing between PEQ/Tone Control/GEQ
  1306. */
  1307. snd_soc_component_write(component, SGTL5000_DAP_AUDIO_EQ, SGTL5000_DAP_SEL_GEQ);
  1308. /* Unmute DAC after start */
  1309. snd_soc_component_update_bits(component, SGTL5000_CHIP_ADCDAC_CTRL,
  1310. SGTL5000_DAC_MUTE_LEFT | SGTL5000_DAC_MUTE_RIGHT, 0);
  1311. return 0;
  1312. err:
  1313. return ret;
  1314. }
  1315. static int sgtl5000_of_xlate_dai_id(struct snd_soc_component *component,
  1316. struct device_node *endpoint)
  1317. {
  1318. /* return dai id 0, whatever the endpoint index */
  1319. return 0;
  1320. }
  1321. static const struct snd_soc_component_driver sgtl5000_driver = {
  1322. .probe = sgtl5000_probe,
  1323. .set_bias_level = sgtl5000_set_bias_level,
  1324. .controls = sgtl5000_snd_controls,
  1325. .num_controls = ARRAY_SIZE(sgtl5000_snd_controls),
  1326. .dapm_widgets = sgtl5000_dapm_widgets,
  1327. .num_dapm_widgets = ARRAY_SIZE(sgtl5000_dapm_widgets),
  1328. .dapm_routes = sgtl5000_dapm_routes,
  1329. .num_dapm_routes = ARRAY_SIZE(sgtl5000_dapm_routes),
  1330. .of_xlate_dai_id = sgtl5000_of_xlate_dai_id,
  1331. .suspend_bias_off = 1,
  1332. .idle_bias_on = 1,
  1333. .use_pmdown_time = 1,
  1334. .endianness = 1,
  1335. };
  1336. static const struct regmap_config sgtl5000_regmap = {
  1337. .reg_bits = 16,
  1338. .val_bits = 16,
  1339. .reg_stride = 2,
  1340. .max_register = SGTL5000_MAX_REG_OFFSET,
  1341. .volatile_reg = sgtl5000_volatile,
  1342. .readable_reg = sgtl5000_readable,
  1343. .cache_type = REGCACHE_RBTREE,
  1344. .reg_defaults = sgtl5000_reg_defaults,
  1345. .num_reg_defaults = ARRAY_SIZE(sgtl5000_reg_defaults),
  1346. };
  1347. /*
  1348. * Write all the default values from sgtl5000_reg_defaults[] array into the
  1349. * sgtl5000 registers, to make sure we always start with the sane registers
  1350. * values as stated in the datasheet.
  1351. *
  1352. * Since sgtl5000 does not have a reset line, nor a reset command in software,
  1353. * we follow this approach to guarantee we always start from the default values
  1354. * and avoid problems like, not being able to probe after an audio playback
  1355. * followed by a system reset or a 'reboot' command in Linux
  1356. */
  1357. static void sgtl5000_fill_defaults(struct i2c_client *client)
  1358. {
  1359. struct sgtl5000_priv *sgtl5000 = i2c_get_clientdata(client);
  1360. int i, ret, val, index;
  1361. for (i = 0; i < ARRAY_SIZE(sgtl5000_reg_defaults); i++) {
  1362. val = sgtl5000_reg_defaults[i].def;
  1363. index = sgtl5000_reg_defaults[i].reg;
  1364. ret = regmap_write(sgtl5000->regmap, index, val);
  1365. if (ret)
  1366. dev_err(&client->dev,
  1367. "%s: error %d setting reg 0x%02x to 0x%04x\n",
  1368. __func__, ret, index, val);
  1369. }
  1370. }
  1371. static int sgtl5000_i2c_probe(struct i2c_client *client)
  1372. {
  1373. struct sgtl5000_priv *sgtl5000;
  1374. int ret, reg, rev;
  1375. struct device_node *np = client->dev.of_node;
  1376. u32 value;
  1377. u16 ana_pwr;
  1378. sgtl5000 = devm_kzalloc(&client->dev, sizeof(*sgtl5000), GFP_KERNEL);
  1379. if (!sgtl5000)
  1380. return -ENOMEM;
  1381. i2c_set_clientdata(client, sgtl5000);
  1382. ret = sgtl5000_enable_regulators(client);
  1383. if (ret)
  1384. return ret;
  1385. sgtl5000->regmap = devm_regmap_init_i2c(client, &sgtl5000_regmap);
  1386. if (IS_ERR(sgtl5000->regmap)) {
  1387. ret = PTR_ERR(sgtl5000->regmap);
  1388. dev_err(&client->dev, "Failed to allocate regmap: %d\n", ret);
  1389. goto disable_regs;
  1390. }
  1391. sgtl5000->mclk = devm_clk_get(&client->dev, NULL);
  1392. if (IS_ERR(sgtl5000->mclk)) {
  1393. ret = PTR_ERR(sgtl5000->mclk);
  1394. /* Defer the probe to see if the clk will be provided later */
  1395. if (ret == -ENOENT)
  1396. ret = -EPROBE_DEFER;
  1397. dev_err_probe(&client->dev, ret, "Failed to get mclock\n");
  1398. goto disable_regs;
  1399. }
  1400. ret = clk_prepare_enable(sgtl5000->mclk);
  1401. if (ret) {
  1402. dev_err(&client->dev, "Error enabling clock %d\n", ret);
  1403. goto disable_regs;
  1404. }
  1405. /* Need 8 clocks before I2C accesses */
  1406. udelay(1);
  1407. /* read chip information */
  1408. ret = regmap_read(sgtl5000->regmap, SGTL5000_CHIP_ID, &reg);
  1409. if (ret) {
  1410. dev_err(&client->dev, "Error reading chip id %d\n", ret);
  1411. goto disable_clk;
  1412. }
  1413. if (((reg & SGTL5000_PARTID_MASK) >> SGTL5000_PARTID_SHIFT) !=
  1414. SGTL5000_PARTID_PART_ID) {
  1415. dev_err(&client->dev,
  1416. "Device with ID register %x is not a sgtl5000\n", reg);
  1417. ret = -ENODEV;
  1418. goto disable_clk;
  1419. }
  1420. rev = (reg & SGTL5000_REVID_MASK) >> SGTL5000_REVID_SHIFT;
  1421. dev_info(&client->dev, "sgtl5000 revision 0x%x\n", rev);
  1422. sgtl5000->revision = rev;
  1423. /* reconfigure the clocks in case we're using the PLL */
  1424. ret = regmap_write(sgtl5000->regmap,
  1425. SGTL5000_CHIP_CLK_CTRL,
  1426. SGTL5000_CHIP_CLK_CTRL_DEFAULT);
  1427. if (ret)
  1428. dev_err(&client->dev,
  1429. "Error %d initializing CHIP_CLK_CTRL\n", ret);
  1430. /* Mute everything to avoid pop from the following power-up */
  1431. ret = regmap_write(sgtl5000->regmap, SGTL5000_CHIP_ANA_CTRL,
  1432. SGTL5000_CHIP_ANA_CTRL_DEFAULT);
  1433. if (ret) {
  1434. dev_err(&client->dev,
  1435. "Error %d muting outputs via CHIP_ANA_CTRL\n", ret);
  1436. goto disable_clk;
  1437. }
  1438. /*
  1439. * If VAG is powered-on (e.g. from previous boot), it would be disabled
  1440. * by the write to ANA_POWER in later steps of the probe code. This
  1441. * may create a loud pop even with all outputs muted. The proper way
  1442. * to circumvent this is disabling the bit first and waiting the proper
  1443. * cool-down time.
  1444. */
  1445. ret = regmap_read(sgtl5000->regmap, SGTL5000_CHIP_ANA_POWER, &value);
  1446. if (ret) {
  1447. dev_err(&client->dev, "Failed to read ANA_POWER: %d\n", ret);
  1448. goto disable_clk;
  1449. }
  1450. if (value & SGTL5000_VAG_POWERUP) {
  1451. ret = regmap_update_bits(sgtl5000->regmap,
  1452. SGTL5000_CHIP_ANA_POWER,
  1453. SGTL5000_VAG_POWERUP,
  1454. 0);
  1455. if (ret) {
  1456. dev_err(&client->dev, "Error %d disabling VAG\n", ret);
  1457. goto disable_clk;
  1458. }
  1459. msleep(SGTL5000_VAG_POWERDOWN_DELAY);
  1460. }
  1461. /* Follow section 2.2.1.1 of AN3663 */
  1462. ana_pwr = SGTL5000_ANA_POWER_DEFAULT;
  1463. if (sgtl5000->num_supplies <= VDDD) {
  1464. /* internal VDDD at 1.2V */
  1465. ret = regmap_update_bits(sgtl5000->regmap,
  1466. SGTL5000_CHIP_LINREG_CTRL,
  1467. SGTL5000_LINREG_VDDD_MASK,
  1468. LINREG_VDDD);
  1469. if (ret)
  1470. dev_err(&client->dev,
  1471. "Error %d setting LINREG_VDDD\n", ret);
  1472. ana_pwr |= SGTL5000_LINEREG_D_POWERUP;
  1473. dev_info(&client->dev,
  1474. "Using internal LDO instead of VDDD: check ER1 erratum\n");
  1475. } else {
  1476. /* using external LDO for VDDD
  1477. * Clear startup powerup and simple powerup
  1478. * bits to save power
  1479. */
  1480. ana_pwr &= ~(SGTL5000_STARTUP_POWERUP
  1481. | SGTL5000_LINREG_SIMPLE_POWERUP);
  1482. dev_dbg(&client->dev, "Using external VDDD\n");
  1483. }
  1484. ret = regmap_write(sgtl5000->regmap, SGTL5000_CHIP_ANA_POWER, ana_pwr);
  1485. if (ret)
  1486. dev_err(&client->dev,
  1487. "Error %d setting CHIP_ANA_POWER to %04x\n",
  1488. ret, ana_pwr);
  1489. if (np) {
  1490. if (!of_property_read_u32(np,
  1491. "micbias-resistor-k-ohms", &value)) {
  1492. switch (value) {
  1493. case SGTL5000_MICBIAS_OFF:
  1494. sgtl5000->micbias_resistor = 0;
  1495. break;
  1496. case SGTL5000_MICBIAS_2K:
  1497. sgtl5000->micbias_resistor = 1;
  1498. break;
  1499. case SGTL5000_MICBIAS_4K:
  1500. sgtl5000->micbias_resistor = 2;
  1501. break;
  1502. case SGTL5000_MICBIAS_8K:
  1503. sgtl5000->micbias_resistor = 3;
  1504. break;
  1505. default:
  1506. sgtl5000->micbias_resistor = 2;
  1507. dev_err(&client->dev,
  1508. "Unsuitable MicBias resistor\n");
  1509. }
  1510. } else {
  1511. /* default is 4Kohms */
  1512. sgtl5000->micbias_resistor = 2;
  1513. }
  1514. if (!of_property_read_u32(np,
  1515. "micbias-voltage-m-volts", &value)) {
  1516. /* 1250mV => 0 */
  1517. /* steps of 250mV */
  1518. if ((value >= 1250) && (value <= 3000))
  1519. sgtl5000->micbias_voltage = (value / 250) - 5;
  1520. else {
  1521. sgtl5000->micbias_voltage = 0;
  1522. dev_err(&client->dev,
  1523. "Unsuitable MicBias voltage\n");
  1524. }
  1525. } else {
  1526. sgtl5000->micbias_voltage = 0;
  1527. }
  1528. }
  1529. sgtl5000->lrclk_strength = I2S_LRCLK_STRENGTH_LOW;
  1530. if (!of_property_read_u32(np, "lrclk-strength", &value)) {
  1531. if (value > I2S_LRCLK_STRENGTH_HIGH)
  1532. value = I2S_LRCLK_STRENGTH_LOW;
  1533. sgtl5000->lrclk_strength = value;
  1534. }
  1535. sgtl5000->sclk_strength = I2S_SCLK_STRENGTH_LOW;
  1536. if (!of_property_read_u32(np, "sclk-strength", &value)) {
  1537. if (value > I2S_SCLK_STRENGTH_HIGH)
  1538. value = I2S_SCLK_STRENGTH_LOW;
  1539. sgtl5000->sclk_strength = value;
  1540. }
  1541. /* Ensure sgtl5000 will start with sane register values */
  1542. sgtl5000_fill_defaults(client);
  1543. ret = devm_snd_soc_register_component(&client->dev,
  1544. &sgtl5000_driver, &sgtl5000_dai, 1);
  1545. if (ret)
  1546. goto disable_clk;
  1547. return 0;
  1548. disable_clk:
  1549. clk_disable_unprepare(sgtl5000->mclk);
  1550. disable_regs:
  1551. regulator_bulk_disable(sgtl5000->num_supplies, sgtl5000->supplies);
  1552. regulator_bulk_free(sgtl5000->num_supplies, sgtl5000->supplies);
  1553. return ret;
  1554. }
  1555. static void sgtl5000_i2c_remove(struct i2c_client *client)
  1556. {
  1557. struct sgtl5000_priv *sgtl5000 = i2c_get_clientdata(client);
  1558. regmap_write(sgtl5000->regmap, SGTL5000_CHIP_CLK_CTRL, SGTL5000_CHIP_CLK_CTRL_DEFAULT);
  1559. regmap_write(sgtl5000->regmap, SGTL5000_CHIP_DIG_POWER, SGTL5000_DIG_POWER_DEFAULT);
  1560. regmap_write(sgtl5000->regmap, SGTL5000_CHIP_ANA_POWER, SGTL5000_ANA_POWER_DEFAULT);
  1561. clk_disable_unprepare(sgtl5000->mclk);
  1562. regulator_bulk_disable(sgtl5000->num_supplies, sgtl5000->supplies);
  1563. regulator_bulk_free(sgtl5000->num_supplies, sgtl5000->supplies);
  1564. }
  1565. static void sgtl5000_i2c_shutdown(struct i2c_client *client)
  1566. {
  1567. sgtl5000_i2c_remove(client);
  1568. }
  1569. static const struct i2c_device_id sgtl5000_id[] = {
  1570. {"sgtl5000", 0},
  1571. {},
  1572. };
  1573. MODULE_DEVICE_TABLE(i2c, sgtl5000_id);
  1574. static const struct of_device_id sgtl5000_dt_ids[] = {
  1575. { .compatible = "fsl,sgtl5000", },
  1576. { /* sentinel */ }
  1577. };
  1578. MODULE_DEVICE_TABLE(of, sgtl5000_dt_ids);
  1579. static struct i2c_driver sgtl5000_i2c_driver = {
  1580. .driver = {
  1581. .name = "sgtl5000",
  1582. .of_match_table = sgtl5000_dt_ids,
  1583. },
  1584. .probe_new = sgtl5000_i2c_probe,
  1585. .remove = sgtl5000_i2c_remove,
  1586. .shutdown = sgtl5000_i2c_shutdown,
  1587. .id_table = sgtl5000_id,
  1588. };
  1589. module_i2c_driver(sgtl5000_i2c_driver);
  1590. MODULE_DESCRIPTION("Freescale SGTL5000 ALSA SoC Codec Driver");
  1591. MODULE_AUTHOR("Zeng Zhaoming <[email protected]>");
  1592. MODULE_LICENSE("GPL");