sgtl5000.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461
  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. {"Headphone Mux", "LINE_IN", "LINE_IN"},/* line_in --> hp_mux */
  208. {"HP", NULL, "Headphone Mux"}, /* hp_mux --> hp */
  209. {"LINE_OUT", NULL, "LO"},
  210. {"HP_OUT", NULL, "HP"},
  211. };
  212. /* custom function to fetch info of PCM playback volume */
  213. static int dac_info_volsw(struct snd_kcontrol *kcontrol,
  214. struct snd_ctl_elem_info *uinfo)
  215. {
  216. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  217. uinfo->count = 2;
  218. uinfo->value.integer.min = 0;
  219. uinfo->value.integer.max = 0xfc - 0x3c;
  220. return 0;
  221. }
  222. /*
  223. * custom function to get of PCM playback volume
  224. *
  225. * dac volume register
  226. * 15-------------8-7--------------0
  227. * | R channel vol | L channel vol |
  228. * -------------------------------
  229. *
  230. * PCM volume with 0.5017 dB steps from 0 to -90 dB
  231. *
  232. * register values map to dB
  233. * 0x3B and less = Reserved
  234. * 0x3C = 0 dB
  235. * 0x3D = -0.5 dB
  236. * 0xF0 = -90 dB
  237. * 0xFC and greater = Muted
  238. *
  239. * register value map to userspace value
  240. *
  241. * register value 0x3c(0dB) 0xf0(-90dB)0xfc
  242. * ------------------------------
  243. * userspace value 0xc0 0
  244. */
  245. static int dac_get_volsw(struct snd_kcontrol *kcontrol,
  246. struct snd_ctl_elem_value *ucontrol)
  247. {
  248. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  249. int reg;
  250. int l;
  251. int r;
  252. reg = snd_soc_read(codec, SGTL5000_CHIP_DAC_VOL);
  253. /* get left channel volume */
  254. l = (reg & SGTL5000_DAC_VOL_LEFT_MASK) >> SGTL5000_DAC_VOL_LEFT_SHIFT;
  255. /* get right channel volume */
  256. r = (reg & SGTL5000_DAC_VOL_RIGHT_MASK) >> SGTL5000_DAC_VOL_RIGHT_SHIFT;
  257. /* make sure value fall in (0x3c,0xfc) */
  258. l = clamp(l, 0x3c, 0xfc);
  259. r = clamp(r, 0x3c, 0xfc);
  260. /* invert it and map to userspace value */
  261. l = 0xfc - l;
  262. r = 0xfc - r;
  263. ucontrol->value.integer.value[0] = l;
  264. ucontrol->value.integer.value[1] = r;
  265. return 0;
  266. }
  267. /*
  268. * custom function to put of PCM playback volume
  269. *
  270. * dac volume register
  271. * 15-------------8-7--------------0
  272. * | R channel vol | L channel vol |
  273. * -------------------------------
  274. *
  275. * PCM volume with 0.5017 dB steps from 0 to -90 dB
  276. *
  277. * register values map to dB
  278. * 0x3B and less = Reserved
  279. * 0x3C = 0 dB
  280. * 0x3D = -0.5 dB
  281. * 0xF0 = -90 dB
  282. * 0xFC and greater = Muted
  283. *
  284. * userspace value map to register value
  285. *
  286. * userspace value 0xc0 0
  287. * ------------------------------
  288. * register value 0x3c(0dB) 0xf0(-90dB)0xfc
  289. */
  290. static int dac_put_volsw(struct snd_kcontrol *kcontrol,
  291. struct snd_ctl_elem_value *ucontrol)
  292. {
  293. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  294. int reg;
  295. int l;
  296. int r;
  297. l = ucontrol->value.integer.value[0];
  298. r = ucontrol->value.integer.value[1];
  299. /* make sure userspace volume fall in (0, 0xfc-0x3c) */
  300. l = clamp(l, 0, 0xfc - 0x3c);
  301. r = clamp(r, 0, 0xfc - 0x3c);
  302. /* invert it, get the value can be set to register */
  303. l = 0xfc - l;
  304. r = 0xfc - r;
  305. /* shift to get the register value */
  306. reg = l << SGTL5000_DAC_VOL_LEFT_SHIFT |
  307. r << SGTL5000_DAC_VOL_RIGHT_SHIFT;
  308. snd_soc_write(codec, SGTL5000_CHIP_DAC_VOL, reg);
  309. return 0;
  310. }
  311. static const DECLARE_TLV_DB_SCALE(capture_6db_attenuate, -600, 600, 0);
  312. /* tlv for mic gain, 0db 20db 30db 40db */
  313. static const unsigned int mic_gain_tlv[] = {
  314. TLV_DB_RANGE_HEAD(2),
  315. 0, 0, TLV_DB_SCALE_ITEM(0, 0, 0),
  316. 1, 3, TLV_DB_SCALE_ITEM(2000, 1000, 0),
  317. };
  318. /* tlv for hp volume, -51.5db to 12.0db, step .5db */
  319. static const DECLARE_TLV_DB_SCALE(headphone_volume, -5150, 50, 0);
  320. static const struct snd_kcontrol_new sgtl5000_snd_controls[] = {
  321. /* SOC_DOUBLE_S8_TLV with invert */
  322. {
  323. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  324. .name = "PCM Playback Volume",
  325. .access = SNDRV_CTL_ELEM_ACCESS_TLV_READ |
  326. SNDRV_CTL_ELEM_ACCESS_READWRITE,
  327. .info = dac_info_volsw,
  328. .get = dac_get_volsw,
  329. .put = dac_put_volsw,
  330. },
  331. SOC_DOUBLE("Capture Volume", SGTL5000_CHIP_ANA_ADC_CTRL, 0, 4, 0xf, 0),
  332. SOC_SINGLE_TLV("Capture Attenuate Switch (-6dB)",
  333. SGTL5000_CHIP_ANA_ADC_CTRL,
  334. 8, 2, 0, capture_6db_attenuate),
  335. SOC_SINGLE("Capture ZC Switch", SGTL5000_CHIP_ANA_CTRL, 1, 1, 0),
  336. SOC_DOUBLE_TLV("Headphone Playback Volume",
  337. SGTL5000_CHIP_ANA_HP_CTRL,
  338. 0, 8,
  339. 0x7f, 1,
  340. headphone_volume),
  341. SOC_SINGLE("Headphone Playback ZC Switch", SGTL5000_CHIP_ANA_CTRL,
  342. 5, 1, 0),
  343. SOC_SINGLE_TLV("Mic Volume", SGTL5000_CHIP_MIC_CTRL,
  344. 0, 4, 0, mic_gain_tlv),
  345. };
  346. /* mute the codec used by alsa core */
  347. static int sgtl5000_digital_mute(struct snd_soc_dai *codec_dai, int mute)
  348. {
  349. struct snd_soc_codec *codec = codec_dai->codec;
  350. u16 adcdac_ctrl = SGTL5000_DAC_MUTE_LEFT | SGTL5000_DAC_MUTE_RIGHT;
  351. snd_soc_update_bits(codec, SGTL5000_CHIP_ADCDAC_CTRL,
  352. adcdac_ctrl, mute ? adcdac_ctrl : 0);
  353. return 0;
  354. }
  355. /* set codec format */
  356. static int sgtl5000_set_dai_fmt(struct snd_soc_dai *codec_dai, unsigned int fmt)
  357. {
  358. struct snd_soc_codec *codec = codec_dai->codec;
  359. struct sgtl5000_priv *sgtl5000 = snd_soc_codec_get_drvdata(codec);
  360. u16 i2sctl = 0;
  361. sgtl5000->master = 0;
  362. /*
  363. * i2s clock and frame master setting.
  364. * ONLY support:
  365. * - clock and frame slave,
  366. * - clock and frame master
  367. */
  368. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  369. case SND_SOC_DAIFMT_CBS_CFS:
  370. break;
  371. case SND_SOC_DAIFMT_CBM_CFM:
  372. i2sctl |= SGTL5000_I2S_MASTER;
  373. sgtl5000->master = 1;
  374. break;
  375. default:
  376. return -EINVAL;
  377. }
  378. /* setting i2s data format */
  379. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  380. case SND_SOC_DAIFMT_DSP_A:
  381. i2sctl |= SGTL5000_I2S_MODE_PCM;
  382. break;
  383. case SND_SOC_DAIFMT_DSP_B:
  384. i2sctl |= SGTL5000_I2S_MODE_PCM;
  385. i2sctl |= SGTL5000_I2S_LRALIGN;
  386. break;
  387. case SND_SOC_DAIFMT_I2S:
  388. i2sctl |= SGTL5000_I2S_MODE_I2S_LJ;
  389. break;
  390. case SND_SOC_DAIFMT_RIGHT_J:
  391. i2sctl |= SGTL5000_I2S_MODE_RJ;
  392. i2sctl |= SGTL5000_I2S_LRPOL;
  393. break;
  394. case SND_SOC_DAIFMT_LEFT_J:
  395. i2sctl |= SGTL5000_I2S_MODE_I2S_LJ;
  396. i2sctl |= SGTL5000_I2S_LRALIGN;
  397. break;
  398. default:
  399. return -EINVAL;
  400. }
  401. sgtl5000->fmt = fmt & SND_SOC_DAIFMT_FORMAT_MASK;
  402. /* Clock inversion */
  403. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  404. case SND_SOC_DAIFMT_NB_NF:
  405. break;
  406. case SND_SOC_DAIFMT_IB_NF:
  407. i2sctl |= SGTL5000_I2S_SCLK_INV;
  408. break;
  409. default:
  410. return -EINVAL;
  411. }
  412. snd_soc_write(codec, SGTL5000_CHIP_I2S_CTRL, i2sctl);
  413. return 0;
  414. }
  415. /* set codec sysclk */
  416. static int sgtl5000_set_dai_sysclk(struct snd_soc_dai *codec_dai,
  417. int clk_id, unsigned int freq, int dir)
  418. {
  419. struct snd_soc_codec *codec = codec_dai->codec;
  420. struct sgtl5000_priv *sgtl5000 = snd_soc_codec_get_drvdata(codec);
  421. switch (clk_id) {
  422. case SGTL5000_SYSCLK:
  423. sgtl5000->sysclk = freq;
  424. break;
  425. default:
  426. return -EINVAL;
  427. }
  428. return 0;
  429. }
  430. /*
  431. * set clock according to i2s frame clock,
  432. * sgtl5000 provide 2 clock sources.
  433. * 1. sys_mclk. sample freq can only configure to
  434. * 1/256, 1/384, 1/512 of sys_mclk.
  435. * 2. pll. can derive any audio clocks.
  436. *
  437. * clock setting rules:
  438. * 1. in slave mode, only sys_mclk can use.
  439. * 2. as constraint by sys_mclk, sample freq should
  440. * set to 32k, 44.1k and above.
  441. * 3. using sys_mclk prefer to pll to save power.
  442. */
  443. static int sgtl5000_set_clock(struct snd_soc_codec *codec, int frame_rate)
  444. {
  445. struct sgtl5000_priv *sgtl5000 = snd_soc_codec_get_drvdata(codec);
  446. int clk_ctl = 0;
  447. int sys_fs; /* sample freq */
  448. /*
  449. * sample freq should be divided by frame clock,
  450. * if frame clock lower than 44.1khz, sample feq should set to
  451. * 32khz or 44.1khz.
  452. */
  453. switch (frame_rate) {
  454. case 8000:
  455. case 16000:
  456. sys_fs = 32000;
  457. break;
  458. case 11025:
  459. case 22050:
  460. sys_fs = 44100;
  461. break;
  462. default:
  463. sys_fs = frame_rate;
  464. break;
  465. }
  466. /* set divided factor of frame clock */
  467. switch (sys_fs / frame_rate) {
  468. case 4:
  469. clk_ctl |= SGTL5000_RATE_MODE_DIV_4 << SGTL5000_RATE_MODE_SHIFT;
  470. break;
  471. case 2:
  472. clk_ctl |= SGTL5000_RATE_MODE_DIV_2 << SGTL5000_RATE_MODE_SHIFT;
  473. break;
  474. case 1:
  475. clk_ctl |= SGTL5000_RATE_MODE_DIV_1 << SGTL5000_RATE_MODE_SHIFT;
  476. break;
  477. default:
  478. return -EINVAL;
  479. }
  480. /* set the sys_fs according to frame rate */
  481. switch (sys_fs) {
  482. case 32000:
  483. clk_ctl |= SGTL5000_SYS_FS_32k << SGTL5000_SYS_FS_SHIFT;
  484. break;
  485. case 44100:
  486. clk_ctl |= SGTL5000_SYS_FS_44_1k << SGTL5000_SYS_FS_SHIFT;
  487. break;
  488. case 48000:
  489. clk_ctl |= SGTL5000_SYS_FS_48k << SGTL5000_SYS_FS_SHIFT;
  490. break;
  491. case 96000:
  492. clk_ctl |= SGTL5000_SYS_FS_96k << SGTL5000_SYS_FS_SHIFT;
  493. break;
  494. default:
  495. dev_err(codec->dev, "frame rate %d not supported\n",
  496. frame_rate);
  497. return -EINVAL;
  498. }
  499. /*
  500. * calculate the divider of mclk/sample_freq,
  501. * factor of freq =96k can only be 256, since mclk in range (12m,27m)
  502. */
  503. switch (sgtl5000->sysclk / sys_fs) {
  504. case 256:
  505. clk_ctl |= SGTL5000_MCLK_FREQ_256FS <<
  506. SGTL5000_MCLK_FREQ_SHIFT;
  507. break;
  508. case 384:
  509. clk_ctl |= SGTL5000_MCLK_FREQ_384FS <<
  510. SGTL5000_MCLK_FREQ_SHIFT;
  511. break;
  512. case 512:
  513. clk_ctl |= SGTL5000_MCLK_FREQ_512FS <<
  514. SGTL5000_MCLK_FREQ_SHIFT;
  515. break;
  516. default:
  517. /* if mclk not satisify the divider, use pll */
  518. if (sgtl5000->master) {
  519. clk_ctl |= SGTL5000_MCLK_FREQ_PLL <<
  520. SGTL5000_MCLK_FREQ_SHIFT;
  521. } else {
  522. dev_err(codec->dev,
  523. "PLL not supported in slave mode\n");
  524. return -EINVAL;
  525. }
  526. }
  527. /* if using pll, please check manual 6.4.2 for detail */
  528. if ((clk_ctl & SGTL5000_MCLK_FREQ_MASK) == SGTL5000_MCLK_FREQ_PLL) {
  529. u64 out, t;
  530. int div2;
  531. int pll_ctl;
  532. unsigned int in, int_div, frac_div;
  533. if (sgtl5000->sysclk > 17000000) {
  534. div2 = 1;
  535. in = sgtl5000->sysclk / 2;
  536. } else {
  537. div2 = 0;
  538. in = sgtl5000->sysclk;
  539. }
  540. if (sys_fs == 44100)
  541. out = 180633600;
  542. else
  543. out = 196608000;
  544. t = do_div(out, in);
  545. int_div = out;
  546. t *= 2048;
  547. do_div(t, in);
  548. frac_div = t;
  549. pll_ctl = int_div << SGTL5000_PLL_INT_DIV_SHIFT |
  550. frac_div << SGTL5000_PLL_FRAC_DIV_SHIFT;
  551. snd_soc_write(codec, SGTL5000_CHIP_PLL_CTRL, pll_ctl);
  552. if (div2)
  553. snd_soc_update_bits(codec,
  554. SGTL5000_CHIP_CLK_TOP_CTRL,
  555. SGTL5000_INPUT_FREQ_DIV2,
  556. SGTL5000_INPUT_FREQ_DIV2);
  557. else
  558. snd_soc_update_bits(codec,
  559. SGTL5000_CHIP_CLK_TOP_CTRL,
  560. SGTL5000_INPUT_FREQ_DIV2,
  561. 0);
  562. /* power up pll */
  563. snd_soc_update_bits(codec, SGTL5000_CHIP_ANA_POWER,
  564. SGTL5000_PLL_POWERUP | SGTL5000_VCOAMP_POWERUP,
  565. SGTL5000_PLL_POWERUP | SGTL5000_VCOAMP_POWERUP);
  566. } else {
  567. /* power down pll */
  568. snd_soc_update_bits(codec, SGTL5000_CHIP_ANA_POWER,
  569. SGTL5000_PLL_POWERUP | SGTL5000_VCOAMP_POWERUP,
  570. 0);
  571. }
  572. /* if using pll, clk_ctrl must be set after pll power up */
  573. snd_soc_write(codec, SGTL5000_CHIP_CLK_CTRL, clk_ctl);
  574. return 0;
  575. }
  576. /*
  577. * Set PCM DAI bit size and sample rate.
  578. * input: params_rate, params_fmt
  579. */
  580. static int sgtl5000_pcm_hw_params(struct snd_pcm_substream *substream,
  581. struct snd_pcm_hw_params *params,
  582. struct snd_soc_dai *dai)
  583. {
  584. struct snd_soc_codec *codec = dai->codec;
  585. struct sgtl5000_priv *sgtl5000 = snd_soc_codec_get_drvdata(codec);
  586. int channels = params_channels(params);
  587. int i2s_ctl = 0;
  588. int stereo;
  589. int ret;
  590. /* sysclk should already set */
  591. if (!sgtl5000->sysclk) {
  592. dev_err(codec->dev, "%s: set sysclk first!\n", __func__);
  593. return -EFAULT;
  594. }
  595. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  596. stereo = SGTL5000_DAC_STEREO;
  597. else
  598. stereo = SGTL5000_ADC_STEREO;
  599. /* set mono to save power */
  600. snd_soc_update_bits(codec, SGTL5000_CHIP_ANA_POWER, stereo,
  601. channels == 1 ? 0 : stereo);
  602. /* set codec clock base on lrclk */
  603. ret = sgtl5000_set_clock(codec, params_rate(params));
  604. if (ret)
  605. return ret;
  606. /* set i2s data format */
  607. switch (params_format(params)) {
  608. case SNDRV_PCM_FORMAT_S16_LE:
  609. if (sgtl5000->fmt == SND_SOC_DAIFMT_RIGHT_J)
  610. return -EINVAL;
  611. i2s_ctl |= SGTL5000_I2S_DLEN_16 << SGTL5000_I2S_DLEN_SHIFT;
  612. i2s_ctl |= SGTL5000_I2S_SCLKFREQ_32FS <<
  613. SGTL5000_I2S_SCLKFREQ_SHIFT;
  614. break;
  615. case SNDRV_PCM_FORMAT_S20_3LE:
  616. i2s_ctl |= SGTL5000_I2S_DLEN_20 << SGTL5000_I2S_DLEN_SHIFT;
  617. i2s_ctl |= SGTL5000_I2S_SCLKFREQ_64FS <<
  618. SGTL5000_I2S_SCLKFREQ_SHIFT;
  619. break;
  620. case SNDRV_PCM_FORMAT_S24_LE:
  621. i2s_ctl |= SGTL5000_I2S_DLEN_24 << SGTL5000_I2S_DLEN_SHIFT;
  622. i2s_ctl |= SGTL5000_I2S_SCLKFREQ_64FS <<
  623. SGTL5000_I2S_SCLKFREQ_SHIFT;
  624. break;
  625. case SNDRV_PCM_FORMAT_S32_LE:
  626. if (sgtl5000->fmt == SND_SOC_DAIFMT_RIGHT_J)
  627. return -EINVAL;
  628. i2s_ctl |= SGTL5000_I2S_DLEN_32 << SGTL5000_I2S_DLEN_SHIFT;
  629. i2s_ctl |= SGTL5000_I2S_SCLKFREQ_64FS <<
  630. SGTL5000_I2S_SCLKFREQ_SHIFT;
  631. break;
  632. default:
  633. return -EINVAL;
  634. }
  635. snd_soc_update_bits(codec, SGTL5000_CHIP_I2S_CTRL,
  636. SGTL5000_I2S_DLEN_MASK | SGTL5000_I2S_SCLKFREQ_MASK,
  637. i2s_ctl);
  638. return 0;
  639. }
  640. #ifdef CONFIG_REGULATOR
  641. static int ldo_regulator_is_enabled(struct regulator_dev *dev)
  642. {
  643. struct ldo_regulator *ldo = rdev_get_drvdata(dev);
  644. return ldo->enabled;
  645. }
  646. static int ldo_regulator_enable(struct regulator_dev *dev)
  647. {
  648. struct ldo_regulator *ldo = rdev_get_drvdata(dev);
  649. struct snd_soc_codec *codec = (struct snd_soc_codec *)ldo->codec_data;
  650. int reg;
  651. if (ldo_regulator_is_enabled(dev))
  652. return 0;
  653. /* set regulator value firstly */
  654. reg = (1600 - ldo->voltage / 1000) / 50;
  655. reg = clamp(reg, 0x0, 0xf);
  656. /* amend the voltage value, unit: uV */
  657. ldo->voltage = (1600 - reg * 50) * 1000;
  658. /* set voltage to register */
  659. snd_soc_update_bits(codec, SGTL5000_CHIP_LINREG_CTRL,
  660. SGTL5000_LINREG_VDDD_MASK, reg);
  661. snd_soc_update_bits(codec, SGTL5000_CHIP_ANA_POWER,
  662. SGTL5000_LINEREG_D_POWERUP,
  663. SGTL5000_LINEREG_D_POWERUP);
  664. /* when internal ldo enabled, simple digital power can be disabled */
  665. snd_soc_update_bits(codec, SGTL5000_CHIP_ANA_POWER,
  666. SGTL5000_LINREG_SIMPLE_POWERUP,
  667. 0);
  668. ldo->enabled = 1;
  669. return 0;
  670. }
  671. static int ldo_regulator_disable(struct regulator_dev *dev)
  672. {
  673. struct ldo_regulator *ldo = rdev_get_drvdata(dev);
  674. struct snd_soc_codec *codec = (struct snd_soc_codec *)ldo->codec_data;
  675. snd_soc_update_bits(codec, SGTL5000_CHIP_ANA_POWER,
  676. SGTL5000_LINEREG_D_POWERUP,
  677. 0);
  678. /* clear voltage info */
  679. snd_soc_update_bits(codec, SGTL5000_CHIP_LINREG_CTRL,
  680. SGTL5000_LINREG_VDDD_MASK, 0);
  681. ldo->enabled = 0;
  682. return 0;
  683. }
  684. static int ldo_regulator_get_voltage(struct regulator_dev *dev)
  685. {
  686. struct ldo_regulator *ldo = rdev_get_drvdata(dev);
  687. return ldo->voltage;
  688. }
  689. static struct regulator_ops ldo_regulator_ops = {
  690. .is_enabled = ldo_regulator_is_enabled,
  691. .enable = ldo_regulator_enable,
  692. .disable = ldo_regulator_disable,
  693. .get_voltage = ldo_regulator_get_voltage,
  694. };
  695. static int ldo_regulator_register(struct snd_soc_codec *codec,
  696. struct regulator_init_data *init_data,
  697. int voltage)
  698. {
  699. struct ldo_regulator *ldo;
  700. struct sgtl5000_priv *sgtl5000 = snd_soc_codec_get_drvdata(codec);
  701. struct regulator_config config = { };
  702. ldo = kzalloc(sizeof(struct ldo_regulator), GFP_KERNEL);
  703. if (!ldo) {
  704. dev_err(codec->dev, "failed to allocate ldo_regulator\n");
  705. return -ENOMEM;
  706. }
  707. ldo->desc.name = kstrdup(dev_name(codec->dev), GFP_KERNEL);
  708. if (!ldo->desc.name) {
  709. kfree(ldo);
  710. dev_err(codec->dev, "failed to allocate decs name memory\n");
  711. return -ENOMEM;
  712. }
  713. ldo->desc.type = REGULATOR_VOLTAGE;
  714. ldo->desc.owner = THIS_MODULE;
  715. ldo->desc.ops = &ldo_regulator_ops;
  716. ldo->desc.n_voltages = 1;
  717. ldo->codec_data = codec;
  718. ldo->voltage = voltage;
  719. config.dev = codec->dev;
  720. config.driver_data = ldo;
  721. config.init_data = init_data;
  722. ldo->dev = regulator_register(&ldo->desc, &config);
  723. if (IS_ERR(ldo->dev)) {
  724. int ret = PTR_ERR(ldo->dev);
  725. dev_err(codec->dev, "failed to register regulator\n");
  726. kfree(ldo->desc.name);
  727. kfree(ldo);
  728. return ret;
  729. }
  730. sgtl5000->ldo = ldo;
  731. return 0;
  732. }
  733. static int ldo_regulator_remove(struct snd_soc_codec *codec)
  734. {
  735. struct sgtl5000_priv *sgtl5000 = snd_soc_codec_get_drvdata(codec);
  736. struct ldo_regulator *ldo = sgtl5000->ldo;
  737. if (!ldo)
  738. return 0;
  739. regulator_unregister(ldo->dev);
  740. kfree(ldo->desc.name);
  741. kfree(ldo);
  742. return 0;
  743. }
  744. #else
  745. static int ldo_regulator_register(struct snd_soc_codec *codec,
  746. struct regulator_init_data *init_data,
  747. int voltage)
  748. {
  749. dev_err(codec->dev, "this setup needs regulator support in the kernel\n");
  750. return -EINVAL;
  751. }
  752. static int ldo_regulator_remove(struct snd_soc_codec *codec)
  753. {
  754. return 0;
  755. }
  756. #endif
  757. /*
  758. * set dac bias
  759. * common state changes:
  760. * startup:
  761. * off --> standby --> prepare --> on
  762. * standby --> prepare --> on
  763. *
  764. * stop:
  765. * on --> prepare --> standby
  766. */
  767. static int sgtl5000_set_bias_level(struct snd_soc_codec *codec,
  768. enum snd_soc_bias_level level)
  769. {
  770. int ret;
  771. struct sgtl5000_priv *sgtl5000 = snd_soc_codec_get_drvdata(codec);
  772. switch (level) {
  773. case SND_SOC_BIAS_ON:
  774. case SND_SOC_BIAS_PREPARE:
  775. break;
  776. case SND_SOC_BIAS_STANDBY:
  777. if (codec->dapm.bias_level == SND_SOC_BIAS_OFF) {
  778. ret = regulator_bulk_enable(
  779. ARRAY_SIZE(sgtl5000->supplies),
  780. sgtl5000->supplies);
  781. if (ret)
  782. return ret;
  783. udelay(10);
  784. }
  785. break;
  786. case SND_SOC_BIAS_OFF:
  787. regulator_bulk_disable(ARRAY_SIZE(sgtl5000->supplies),
  788. sgtl5000->supplies);
  789. break;
  790. }
  791. codec->dapm.bias_level = level;
  792. return 0;
  793. }
  794. #define SGTL5000_FORMATS (SNDRV_PCM_FMTBIT_S16_LE |\
  795. SNDRV_PCM_FMTBIT_S20_3LE |\
  796. SNDRV_PCM_FMTBIT_S24_LE |\
  797. SNDRV_PCM_FMTBIT_S32_LE)
  798. static const struct snd_soc_dai_ops sgtl5000_ops = {
  799. .hw_params = sgtl5000_pcm_hw_params,
  800. .digital_mute = sgtl5000_digital_mute,
  801. .set_fmt = sgtl5000_set_dai_fmt,
  802. .set_sysclk = sgtl5000_set_dai_sysclk,
  803. };
  804. static struct snd_soc_dai_driver sgtl5000_dai = {
  805. .name = "sgtl5000",
  806. .playback = {
  807. .stream_name = "Playback",
  808. .channels_min = 1,
  809. .channels_max = 2,
  810. /*
  811. * only support 8~48K + 96K,
  812. * TODO modify hw_param to support more
  813. */
  814. .rates = SNDRV_PCM_RATE_8000_48000 | SNDRV_PCM_RATE_96000,
  815. .formats = SGTL5000_FORMATS,
  816. },
  817. .capture = {
  818. .stream_name = "Capture",
  819. .channels_min = 1,
  820. .channels_max = 2,
  821. .rates = SNDRV_PCM_RATE_8000_48000 | SNDRV_PCM_RATE_96000,
  822. .formats = SGTL5000_FORMATS,
  823. },
  824. .ops = &sgtl5000_ops,
  825. .symmetric_rates = 1,
  826. };
  827. static int sgtl5000_volatile_register(struct snd_soc_codec *codec,
  828. unsigned int reg)
  829. {
  830. switch (reg) {
  831. case SGTL5000_CHIP_ID:
  832. case SGTL5000_CHIP_ADCDAC_CTRL:
  833. case SGTL5000_CHIP_ANA_STATUS:
  834. return 1;
  835. }
  836. return 0;
  837. }
  838. #ifdef CONFIG_SUSPEND
  839. static int sgtl5000_suspend(struct snd_soc_codec *codec)
  840. {
  841. sgtl5000_set_bias_level(codec, SND_SOC_BIAS_OFF);
  842. return 0;
  843. }
  844. /*
  845. * restore all sgtl5000 registers,
  846. * since a big hole between dap and regular registers,
  847. * we will restore them respectively.
  848. */
  849. static int sgtl5000_restore_regs(struct snd_soc_codec *codec)
  850. {
  851. u16 *cache = codec->reg_cache;
  852. u16 reg;
  853. /* restore regular registers */
  854. for (reg = 0; reg <= SGTL5000_CHIP_SHORT_CTRL; reg += 2) {
  855. /* These regs should restore in particular order */
  856. if (reg == SGTL5000_CHIP_ANA_POWER ||
  857. reg == SGTL5000_CHIP_CLK_CTRL ||
  858. reg == SGTL5000_CHIP_LINREG_CTRL ||
  859. reg == SGTL5000_CHIP_LINE_OUT_CTRL ||
  860. reg == SGTL5000_CHIP_REF_CTRL)
  861. continue;
  862. snd_soc_write(codec, reg, cache[reg]);
  863. }
  864. /* restore dap registers */
  865. for (reg = SGTL5000_DAP_REG_OFFSET; reg < SGTL5000_MAX_REG_OFFSET; reg += 2)
  866. snd_soc_write(codec, reg, cache[reg]);
  867. /*
  868. * restore these regs according to the power setting sequence in
  869. * sgtl5000_set_power_regs() and clock setting sequence in
  870. * sgtl5000_set_clock().
  871. *
  872. * The order of restore is:
  873. * 1. SGTL5000_CHIP_CLK_CTRL MCLK_FREQ bits (1:0) should be restore after
  874. * SGTL5000_CHIP_ANA_POWER PLL bits set
  875. * 2. SGTL5000_CHIP_LINREG_CTRL should be set before
  876. * SGTL5000_CHIP_ANA_POWER LINREG_D restored
  877. * 3. SGTL5000_CHIP_REF_CTRL controls Analog Ground Voltage,
  878. * prefer to resotre it after SGTL5000_CHIP_ANA_POWER restored
  879. */
  880. snd_soc_write(codec, SGTL5000_CHIP_LINREG_CTRL,
  881. cache[SGTL5000_CHIP_LINREG_CTRL]);
  882. snd_soc_write(codec, SGTL5000_CHIP_ANA_POWER,
  883. cache[SGTL5000_CHIP_ANA_POWER]);
  884. snd_soc_write(codec, SGTL5000_CHIP_CLK_CTRL,
  885. cache[SGTL5000_CHIP_CLK_CTRL]);
  886. snd_soc_write(codec, SGTL5000_CHIP_REF_CTRL,
  887. cache[SGTL5000_CHIP_REF_CTRL]);
  888. snd_soc_write(codec, SGTL5000_CHIP_LINE_OUT_CTRL,
  889. cache[SGTL5000_CHIP_LINE_OUT_CTRL]);
  890. return 0;
  891. }
  892. static int sgtl5000_resume(struct snd_soc_codec *codec)
  893. {
  894. /* Bring the codec back up to standby to enable regulators */
  895. sgtl5000_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  896. /* Restore registers by cached in memory */
  897. sgtl5000_restore_regs(codec);
  898. return 0;
  899. }
  900. #else
  901. #define sgtl5000_suspend NULL
  902. #define sgtl5000_resume NULL
  903. #endif /* CONFIG_SUSPEND */
  904. /*
  905. * sgtl5000 has 3 internal power supplies:
  906. * 1. VAG, normally set to vdda/2
  907. * 2. chargepump, set to different value
  908. * according to voltage of vdda and vddio
  909. * 3. line out VAG, normally set to vddio/2
  910. *
  911. * and should be set according to:
  912. * 1. vddd provided by external or not
  913. * 2. vdda and vddio voltage value. > 3.1v or not
  914. * 3. chip revision >=0x11 or not. If >=0x11, not use external vddd.
  915. */
  916. static int sgtl5000_set_power_regs(struct snd_soc_codec *codec)
  917. {
  918. int vddd;
  919. int vdda;
  920. int vddio;
  921. u16 ana_pwr;
  922. u16 lreg_ctrl;
  923. int vag;
  924. struct sgtl5000_priv *sgtl5000 = snd_soc_codec_get_drvdata(codec);
  925. vdda = regulator_get_voltage(sgtl5000->supplies[VDDA].consumer);
  926. vddio = regulator_get_voltage(sgtl5000->supplies[VDDIO].consumer);
  927. vddd = regulator_get_voltage(sgtl5000->supplies[VDDD].consumer);
  928. vdda = vdda / 1000;
  929. vddio = vddio / 1000;
  930. vddd = vddd / 1000;
  931. if (vdda <= 0 || vddio <= 0 || vddd < 0) {
  932. dev_err(codec->dev, "regulator voltage not set correctly\n");
  933. return -EINVAL;
  934. }
  935. /* according to datasheet, maximum voltage of supplies */
  936. if (vdda > 3600 || vddio > 3600 || vddd > 1980) {
  937. dev_err(codec->dev,
  938. "exceed max voltage vdda %dmV vddio %dmV vddd %dmV\n",
  939. vdda, vddio, vddd);
  940. return -EINVAL;
  941. }
  942. /* reset value */
  943. ana_pwr = snd_soc_read(codec, SGTL5000_CHIP_ANA_POWER);
  944. ana_pwr |= SGTL5000_DAC_STEREO |
  945. SGTL5000_ADC_STEREO |
  946. SGTL5000_REFTOP_POWERUP;
  947. lreg_ctrl = snd_soc_read(codec, SGTL5000_CHIP_LINREG_CTRL);
  948. if (vddio < 3100 && vdda < 3100) {
  949. /* enable internal oscillator used for charge pump */
  950. snd_soc_update_bits(codec, SGTL5000_CHIP_CLK_TOP_CTRL,
  951. SGTL5000_INT_OSC_EN,
  952. SGTL5000_INT_OSC_EN);
  953. /* Enable VDDC charge pump */
  954. ana_pwr |= SGTL5000_VDDC_CHRGPMP_POWERUP;
  955. } else if (vddio >= 3100 && vdda >= 3100) {
  956. /*
  957. * if vddio and vddd > 3.1v,
  958. * charge pump should be clean before set ana_pwr
  959. */
  960. snd_soc_update_bits(codec, SGTL5000_CHIP_ANA_POWER,
  961. SGTL5000_VDDC_CHRGPMP_POWERUP, 0);
  962. /* VDDC use VDDIO rail */
  963. lreg_ctrl |= SGTL5000_VDDC_ASSN_OVRD;
  964. lreg_ctrl |= SGTL5000_VDDC_MAN_ASSN_VDDIO <<
  965. SGTL5000_VDDC_MAN_ASSN_SHIFT;
  966. }
  967. snd_soc_write(codec, SGTL5000_CHIP_LINREG_CTRL, lreg_ctrl);
  968. snd_soc_write(codec, SGTL5000_CHIP_ANA_POWER, ana_pwr);
  969. /* set voltage to register */
  970. snd_soc_update_bits(codec, SGTL5000_CHIP_LINREG_CTRL,
  971. SGTL5000_LINREG_VDDD_MASK, 0x8);
  972. /*
  973. * if vddd linear reg has been enabled,
  974. * simple digital supply should be clear to get
  975. * proper VDDD voltage.
  976. */
  977. if (ana_pwr & SGTL5000_LINEREG_D_POWERUP)
  978. snd_soc_update_bits(codec, SGTL5000_CHIP_ANA_POWER,
  979. SGTL5000_LINREG_SIMPLE_POWERUP,
  980. 0);
  981. else
  982. snd_soc_update_bits(codec, SGTL5000_CHIP_ANA_POWER,
  983. SGTL5000_LINREG_SIMPLE_POWERUP |
  984. SGTL5000_STARTUP_POWERUP,
  985. 0);
  986. /*
  987. * set ADC/DAC VAG to vdda / 2,
  988. * should stay in range (0.8v, 1.575v)
  989. */
  990. vag = vdda / 2;
  991. if (vag <= SGTL5000_ANA_GND_BASE)
  992. vag = 0;
  993. else if (vag >= SGTL5000_ANA_GND_BASE + SGTL5000_ANA_GND_STP *
  994. (SGTL5000_ANA_GND_MASK >> SGTL5000_ANA_GND_SHIFT))
  995. vag = SGTL5000_ANA_GND_MASK >> SGTL5000_ANA_GND_SHIFT;
  996. else
  997. vag = (vag - SGTL5000_ANA_GND_BASE) / SGTL5000_ANA_GND_STP;
  998. snd_soc_update_bits(codec, SGTL5000_CHIP_REF_CTRL,
  999. SGTL5000_ANA_GND_MASK, vag << SGTL5000_ANA_GND_SHIFT);
  1000. /* set line out VAG to vddio / 2, in range (0.8v, 1.675v) */
  1001. vag = vddio / 2;
  1002. if (vag <= SGTL5000_LINE_OUT_GND_BASE)
  1003. vag = 0;
  1004. else if (vag >= SGTL5000_LINE_OUT_GND_BASE +
  1005. SGTL5000_LINE_OUT_GND_STP * SGTL5000_LINE_OUT_GND_MAX)
  1006. vag = SGTL5000_LINE_OUT_GND_MAX;
  1007. else
  1008. vag = (vag - SGTL5000_LINE_OUT_GND_BASE) /
  1009. SGTL5000_LINE_OUT_GND_STP;
  1010. snd_soc_update_bits(codec, SGTL5000_CHIP_LINE_OUT_CTRL,
  1011. SGTL5000_LINE_OUT_CURRENT_MASK |
  1012. SGTL5000_LINE_OUT_GND_MASK,
  1013. vag << SGTL5000_LINE_OUT_GND_SHIFT |
  1014. SGTL5000_LINE_OUT_CURRENT_360u <<
  1015. SGTL5000_LINE_OUT_CURRENT_SHIFT);
  1016. return 0;
  1017. }
  1018. static int sgtl5000_replace_vddd_with_ldo(struct snd_soc_codec *codec)
  1019. {
  1020. struct sgtl5000_priv *sgtl5000 = snd_soc_codec_get_drvdata(codec);
  1021. int ret;
  1022. /* set internal ldo to 1.2v */
  1023. ret = ldo_regulator_register(codec, &ldo_init_data, LDO_VOLTAGE);
  1024. if (ret) {
  1025. dev_err(codec->dev,
  1026. "Failed to register vddd internal supplies: %d\n", ret);
  1027. return ret;
  1028. }
  1029. sgtl5000->supplies[VDDD].supply = LDO_CONSUMER_NAME;
  1030. ret = regulator_bulk_get(codec->dev, ARRAY_SIZE(sgtl5000->supplies),
  1031. sgtl5000->supplies);
  1032. if (ret) {
  1033. ldo_regulator_remove(codec);
  1034. dev_err(codec->dev, "Failed to request supplies: %d\n", ret);
  1035. return ret;
  1036. }
  1037. dev_info(codec->dev, "Using internal LDO instead of VDDD\n");
  1038. return 0;
  1039. }
  1040. static int sgtl5000_enable_regulators(struct snd_soc_codec *codec)
  1041. {
  1042. u16 reg;
  1043. int ret;
  1044. int rev;
  1045. int i;
  1046. int external_vddd = 0;
  1047. struct sgtl5000_priv *sgtl5000 = snd_soc_codec_get_drvdata(codec);
  1048. for (i = 0; i < ARRAY_SIZE(sgtl5000->supplies); i++)
  1049. sgtl5000->supplies[i].supply = supply_names[i];
  1050. ret = regulator_bulk_get(codec->dev, ARRAY_SIZE(sgtl5000->supplies),
  1051. sgtl5000->supplies);
  1052. if (!ret)
  1053. external_vddd = 1;
  1054. else {
  1055. ret = sgtl5000_replace_vddd_with_ldo(codec);
  1056. if (ret)
  1057. return ret;
  1058. }
  1059. ret = regulator_bulk_enable(ARRAY_SIZE(sgtl5000->supplies),
  1060. sgtl5000->supplies);
  1061. if (ret)
  1062. goto err_regulator_free;
  1063. /* wait for all power rails bring up */
  1064. udelay(10);
  1065. /* read chip information */
  1066. reg = snd_soc_read(codec, SGTL5000_CHIP_ID);
  1067. if (((reg & SGTL5000_PARTID_MASK) >> SGTL5000_PARTID_SHIFT) !=
  1068. SGTL5000_PARTID_PART_ID) {
  1069. dev_err(codec->dev,
  1070. "Device with ID register %x is not a sgtl5000\n", reg);
  1071. ret = -ENODEV;
  1072. goto err_regulator_disable;
  1073. }
  1074. rev = (reg & SGTL5000_REVID_MASK) >> SGTL5000_REVID_SHIFT;
  1075. dev_info(codec->dev, "sgtl5000 revision 0x%x\n", rev);
  1076. /*
  1077. * workaround for revision 0x11 and later,
  1078. * roll back to use internal LDO
  1079. */
  1080. if (external_vddd && rev >= 0x11) {
  1081. /* disable all regulator first */
  1082. regulator_bulk_disable(ARRAY_SIZE(sgtl5000->supplies),
  1083. sgtl5000->supplies);
  1084. /* free VDDD regulator */
  1085. regulator_bulk_free(ARRAY_SIZE(sgtl5000->supplies),
  1086. sgtl5000->supplies);
  1087. ret = sgtl5000_replace_vddd_with_ldo(codec);
  1088. if (ret)
  1089. return ret;
  1090. ret = regulator_bulk_enable(ARRAY_SIZE(sgtl5000->supplies),
  1091. sgtl5000->supplies);
  1092. if (ret)
  1093. goto err_regulator_free;
  1094. /* wait for all power rails bring up */
  1095. udelay(10);
  1096. }
  1097. return 0;
  1098. err_regulator_disable:
  1099. regulator_bulk_disable(ARRAY_SIZE(sgtl5000->supplies),
  1100. sgtl5000->supplies);
  1101. err_regulator_free:
  1102. regulator_bulk_free(ARRAY_SIZE(sgtl5000->supplies),
  1103. sgtl5000->supplies);
  1104. if (external_vddd)
  1105. ldo_regulator_remove(codec);
  1106. return ret;
  1107. }
  1108. static int sgtl5000_probe(struct snd_soc_codec *codec)
  1109. {
  1110. int ret;
  1111. struct sgtl5000_priv *sgtl5000 = snd_soc_codec_get_drvdata(codec);
  1112. /* setup i2c data ops */
  1113. ret = snd_soc_codec_set_cache_io(codec, 16, 16, SND_SOC_I2C);
  1114. if (ret < 0) {
  1115. dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
  1116. return ret;
  1117. }
  1118. ret = sgtl5000_enable_regulators(codec);
  1119. if (ret)
  1120. return ret;
  1121. /* power up sgtl5000 */
  1122. ret = sgtl5000_set_power_regs(codec);
  1123. if (ret)
  1124. goto err;
  1125. /* enable small pop, introduce 400ms delay in turning off */
  1126. snd_soc_update_bits(codec, SGTL5000_CHIP_REF_CTRL,
  1127. SGTL5000_SMALL_POP,
  1128. SGTL5000_SMALL_POP);
  1129. /* disable short cut detector */
  1130. snd_soc_write(codec, SGTL5000_CHIP_SHORT_CTRL, 0);
  1131. /*
  1132. * set i2s as default input of sound switch
  1133. * TODO: add sound switch to control and dapm widge.
  1134. */
  1135. snd_soc_write(codec, SGTL5000_CHIP_SSS_CTRL,
  1136. SGTL5000_DAC_SEL_I2S_IN << SGTL5000_DAC_SEL_SHIFT);
  1137. snd_soc_write(codec, SGTL5000_CHIP_DIG_POWER,
  1138. SGTL5000_ADC_EN | SGTL5000_DAC_EN);
  1139. /* enable dac volume ramp by default */
  1140. snd_soc_write(codec, SGTL5000_CHIP_ADCDAC_CTRL,
  1141. SGTL5000_DAC_VOL_RAMP_EN |
  1142. SGTL5000_DAC_MUTE_RIGHT |
  1143. SGTL5000_DAC_MUTE_LEFT);
  1144. snd_soc_write(codec, SGTL5000_CHIP_PAD_STRENGTH, 0x015f);
  1145. snd_soc_write(codec, SGTL5000_CHIP_ANA_CTRL,
  1146. SGTL5000_HP_ZCD_EN |
  1147. SGTL5000_ADC_ZCD_EN);
  1148. snd_soc_write(codec, SGTL5000_CHIP_MIC_CTRL, 0);
  1149. /*
  1150. * disable DAP
  1151. * TODO:
  1152. * Enable DAP in kcontrol and dapm.
  1153. */
  1154. snd_soc_write(codec, SGTL5000_DAP_CTRL, 0);
  1155. /* leading to standby state */
  1156. ret = sgtl5000_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  1157. if (ret)
  1158. goto err;
  1159. snd_soc_dapm_new_widgets(&codec->dapm);
  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 __devinit 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 __devexit 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 = __devexit_p(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");