sgtl5000.c 36 KB

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