sgtl5000.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465
  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] = 0x0010,
  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_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_MICBIAS_E("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_pcm_runtime *rtd = substream->private_data;
  585. struct snd_soc_codec *codec = rtd->codec;
  586. struct sgtl5000_priv *sgtl5000 = snd_soc_codec_get_drvdata(codec);
  587. int channels = params_channels(params);
  588. int i2s_ctl = 0;
  589. int stereo;
  590. int ret;
  591. /* sysclk should already set */
  592. if (!sgtl5000->sysclk) {
  593. dev_err(codec->dev, "%s: set sysclk first!\n", __func__);
  594. return -EFAULT;
  595. }
  596. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  597. stereo = SGTL5000_DAC_STEREO;
  598. else
  599. stereo = SGTL5000_ADC_STEREO;
  600. /* set mono to save power */
  601. snd_soc_update_bits(codec, SGTL5000_CHIP_ANA_POWER, stereo,
  602. channels == 1 ? 0 : stereo);
  603. /* set codec clock base on lrclk */
  604. ret = sgtl5000_set_clock(codec, params_rate(params));
  605. if (ret)
  606. return ret;
  607. /* set i2s data format */
  608. switch (params_format(params)) {
  609. case SNDRV_PCM_FORMAT_S16_LE:
  610. if (sgtl5000->fmt == SND_SOC_DAIFMT_RIGHT_J)
  611. return -EINVAL;
  612. i2s_ctl |= SGTL5000_I2S_DLEN_16 << SGTL5000_I2S_DLEN_SHIFT;
  613. i2s_ctl |= SGTL5000_I2S_SCLKFREQ_32FS <<
  614. SGTL5000_I2S_SCLKFREQ_SHIFT;
  615. break;
  616. case SNDRV_PCM_FORMAT_S20_3LE:
  617. i2s_ctl |= SGTL5000_I2S_DLEN_20 << SGTL5000_I2S_DLEN_SHIFT;
  618. i2s_ctl |= SGTL5000_I2S_SCLKFREQ_64FS <<
  619. SGTL5000_I2S_SCLKFREQ_SHIFT;
  620. break;
  621. case SNDRV_PCM_FORMAT_S24_LE:
  622. i2s_ctl |= SGTL5000_I2S_DLEN_24 << SGTL5000_I2S_DLEN_SHIFT;
  623. i2s_ctl |= SGTL5000_I2S_SCLKFREQ_64FS <<
  624. SGTL5000_I2S_SCLKFREQ_SHIFT;
  625. break;
  626. case SNDRV_PCM_FORMAT_S32_LE:
  627. if (sgtl5000->fmt == SND_SOC_DAIFMT_RIGHT_J)
  628. return -EINVAL;
  629. i2s_ctl |= SGTL5000_I2S_DLEN_32 << SGTL5000_I2S_DLEN_SHIFT;
  630. i2s_ctl |= SGTL5000_I2S_SCLKFREQ_64FS <<
  631. SGTL5000_I2S_SCLKFREQ_SHIFT;
  632. break;
  633. default:
  634. return -EINVAL;
  635. }
  636. snd_soc_update_bits(codec, SGTL5000_CHIP_I2S_CTRL,
  637. SGTL5000_I2S_DLEN_MASK | SGTL5000_I2S_SCLKFREQ_MASK,
  638. i2s_ctl);
  639. return 0;
  640. }
  641. #ifdef CONFIG_REGULATOR
  642. static int ldo_regulator_is_enabled(struct regulator_dev *dev)
  643. {
  644. struct ldo_regulator *ldo = rdev_get_drvdata(dev);
  645. return ldo->enabled;
  646. }
  647. static int ldo_regulator_enable(struct regulator_dev *dev)
  648. {
  649. struct ldo_regulator *ldo = rdev_get_drvdata(dev);
  650. struct snd_soc_codec *codec = (struct snd_soc_codec *)ldo->codec_data;
  651. int reg;
  652. if (ldo_regulator_is_enabled(dev))
  653. return 0;
  654. /* set regulator value firstly */
  655. reg = (1600 - ldo->voltage / 1000) / 50;
  656. reg = clamp(reg, 0x0, 0xf);
  657. /* amend the voltage value, unit: uV */
  658. ldo->voltage = (1600 - reg * 50) * 1000;
  659. /* set voltage to register */
  660. snd_soc_update_bits(codec, SGTL5000_CHIP_LINREG_CTRL,
  661. SGTL5000_LINREG_VDDD_MASK, reg);
  662. snd_soc_update_bits(codec, SGTL5000_CHIP_ANA_POWER,
  663. SGTL5000_LINEREG_D_POWERUP,
  664. SGTL5000_LINEREG_D_POWERUP);
  665. /* when internal ldo enabled, simple digital power can be disabled */
  666. snd_soc_update_bits(codec, SGTL5000_CHIP_ANA_POWER,
  667. SGTL5000_LINREG_SIMPLE_POWERUP,
  668. 0);
  669. ldo->enabled = 1;
  670. return 0;
  671. }
  672. static int ldo_regulator_disable(struct regulator_dev *dev)
  673. {
  674. struct ldo_regulator *ldo = rdev_get_drvdata(dev);
  675. struct snd_soc_codec *codec = (struct snd_soc_codec *)ldo->codec_data;
  676. snd_soc_update_bits(codec, SGTL5000_CHIP_ANA_POWER,
  677. SGTL5000_LINEREG_D_POWERUP,
  678. 0);
  679. /* clear voltage info */
  680. snd_soc_update_bits(codec, SGTL5000_CHIP_LINREG_CTRL,
  681. SGTL5000_LINREG_VDDD_MASK, 0);
  682. ldo->enabled = 0;
  683. return 0;
  684. }
  685. static int ldo_regulator_get_voltage(struct regulator_dev *dev)
  686. {
  687. struct ldo_regulator *ldo = rdev_get_drvdata(dev);
  688. return ldo->voltage;
  689. }
  690. static struct regulator_ops ldo_regulator_ops = {
  691. .is_enabled = ldo_regulator_is_enabled,
  692. .enable = ldo_regulator_enable,
  693. .disable = ldo_regulator_disable,
  694. .get_voltage = ldo_regulator_get_voltage,
  695. };
  696. static int ldo_regulator_register(struct snd_soc_codec *codec,
  697. struct regulator_init_data *init_data,
  698. int voltage)
  699. {
  700. struct ldo_regulator *ldo;
  701. struct sgtl5000_priv *sgtl5000 = snd_soc_codec_get_drvdata(codec);
  702. 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. ldo->dev = regulator_register(&ldo->desc, codec->dev,
  720. init_data, ldo, NULL);
  721. if (IS_ERR(ldo->dev)) {
  722. int ret = PTR_ERR(ldo->dev);
  723. dev_err(codec->dev, "failed to register regulator\n");
  724. kfree(ldo->desc.name);
  725. kfree(ldo);
  726. return ret;
  727. }
  728. sgtl5000->ldo = ldo;
  729. return 0;
  730. }
  731. static int ldo_regulator_remove(struct snd_soc_codec *codec)
  732. {
  733. struct sgtl5000_priv *sgtl5000 = snd_soc_codec_get_drvdata(codec);
  734. struct ldo_regulator *ldo = sgtl5000->ldo;
  735. if (!ldo)
  736. return 0;
  737. regulator_unregister(ldo->dev);
  738. kfree(ldo->desc.name);
  739. kfree(ldo);
  740. return 0;
  741. }
  742. #else
  743. static int ldo_regulator_register(struct snd_soc_codec *codec,
  744. struct regulator_init_data *init_data,
  745. int voltage)
  746. {
  747. dev_err(codec->dev, "this setup needs regulator support in the kernel\n");
  748. return -EINVAL;
  749. }
  750. static int ldo_regulator_remove(struct snd_soc_codec *codec)
  751. {
  752. return 0;
  753. }
  754. #endif
  755. /*
  756. * set dac bias
  757. * common state changes:
  758. * startup:
  759. * off --> standby --> prepare --> on
  760. * standby --> prepare --> on
  761. *
  762. * stop:
  763. * on --> prepare --> standby
  764. */
  765. static int sgtl5000_set_bias_level(struct snd_soc_codec *codec,
  766. enum snd_soc_bias_level level)
  767. {
  768. int ret;
  769. struct sgtl5000_priv *sgtl5000 = snd_soc_codec_get_drvdata(codec);
  770. switch (level) {
  771. case SND_SOC_BIAS_ON:
  772. case SND_SOC_BIAS_PREPARE:
  773. break;
  774. case SND_SOC_BIAS_STANDBY:
  775. if (codec->dapm.bias_level == SND_SOC_BIAS_OFF) {
  776. ret = regulator_bulk_enable(
  777. ARRAY_SIZE(sgtl5000->supplies),
  778. sgtl5000->supplies);
  779. if (ret)
  780. return ret;
  781. udelay(10);
  782. }
  783. break;
  784. case SND_SOC_BIAS_OFF:
  785. regulator_bulk_disable(ARRAY_SIZE(sgtl5000->supplies),
  786. sgtl5000->supplies);
  787. break;
  788. }
  789. codec->dapm.bias_level = level;
  790. return 0;
  791. }
  792. #define SGTL5000_FORMATS (SNDRV_PCM_FMTBIT_S16_LE |\
  793. SNDRV_PCM_FMTBIT_S20_3LE |\
  794. SNDRV_PCM_FMTBIT_S24_LE |\
  795. SNDRV_PCM_FMTBIT_S32_LE)
  796. static const struct snd_soc_dai_ops sgtl5000_ops = {
  797. .hw_params = sgtl5000_pcm_hw_params,
  798. .digital_mute = sgtl5000_digital_mute,
  799. .set_fmt = sgtl5000_set_dai_fmt,
  800. .set_sysclk = sgtl5000_set_dai_sysclk,
  801. };
  802. static struct snd_soc_dai_driver sgtl5000_dai = {
  803. .name = "sgtl5000",
  804. .playback = {
  805. .stream_name = "Playback",
  806. .channels_min = 1,
  807. .channels_max = 2,
  808. /*
  809. * only support 8~48K + 96K,
  810. * TODO modify hw_param to support more
  811. */
  812. .rates = SNDRV_PCM_RATE_8000_48000 | SNDRV_PCM_RATE_96000,
  813. .formats = SGTL5000_FORMATS,
  814. },
  815. .capture = {
  816. .stream_name = "Capture",
  817. .channels_min = 1,
  818. .channels_max = 2,
  819. .rates = SNDRV_PCM_RATE_8000_48000 | SNDRV_PCM_RATE_96000,
  820. .formats = SGTL5000_FORMATS,
  821. },
  822. .ops = &sgtl5000_ops,
  823. .symmetric_rates = 1,
  824. };
  825. static int sgtl5000_volatile_register(struct snd_soc_codec *codec,
  826. unsigned int reg)
  827. {
  828. switch (reg) {
  829. case SGTL5000_CHIP_ID:
  830. case SGTL5000_CHIP_ADCDAC_CTRL:
  831. case SGTL5000_CHIP_ANA_STATUS:
  832. return 1;
  833. }
  834. return 0;
  835. }
  836. #ifdef CONFIG_SUSPEND
  837. static int sgtl5000_suspend(struct snd_soc_codec *codec)
  838. {
  839. sgtl5000_set_bias_level(codec, SND_SOC_BIAS_OFF);
  840. return 0;
  841. }
  842. /*
  843. * restore all sgtl5000 registers,
  844. * since a big hole between dap and regular registers,
  845. * we will restore them respectively.
  846. */
  847. static int sgtl5000_restore_regs(struct snd_soc_codec *codec)
  848. {
  849. u16 *cache = codec->reg_cache;
  850. u16 reg;
  851. /* restore regular registers */
  852. for (reg = 0; reg <= SGTL5000_CHIP_SHORT_CTRL; reg += 2) {
  853. /* These regs should restore in particular order */
  854. if (reg == SGTL5000_CHIP_ANA_POWER ||
  855. reg == SGTL5000_CHIP_CLK_CTRL ||
  856. reg == SGTL5000_CHIP_LINREG_CTRL ||
  857. reg == SGTL5000_CHIP_LINE_OUT_CTRL ||
  858. reg == SGTL5000_CHIP_REF_CTRL)
  859. continue;
  860. snd_soc_write(codec, reg, cache[reg]);
  861. }
  862. /* restore dap registers */
  863. for (reg = SGTL5000_DAP_REG_OFFSET; reg < SGTL5000_MAX_REG_OFFSET; reg += 2)
  864. snd_soc_write(codec, reg, cache[reg]);
  865. /*
  866. * restore these regs according to the power setting sequence in
  867. * sgtl5000_set_power_regs() and clock setting sequence in
  868. * sgtl5000_set_clock().
  869. *
  870. * The order of restore is:
  871. * 1. SGTL5000_CHIP_CLK_CTRL MCLK_FREQ bits (1:0) should be restore after
  872. * SGTL5000_CHIP_ANA_POWER PLL bits set
  873. * 2. SGTL5000_CHIP_LINREG_CTRL should be set before
  874. * SGTL5000_CHIP_ANA_POWER LINREG_D restored
  875. * 3. SGTL5000_CHIP_REF_CTRL controls Analog Ground Voltage,
  876. * prefer to resotre it after SGTL5000_CHIP_ANA_POWER restored
  877. */
  878. snd_soc_write(codec, SGTL5000_CHIP_LINREG_CTRL,
  879. cache[SGTL5000_CHIP_LINREG_CTRL]);
  880. snd_soc_write(codec, SGTL5000_CHIP_ANA_POWER,
  881. cache[SGTL5000_CHIP_ANA_POWER]);
  882. snd_soc_write(codec, SGTL5000_CHIP_CLK_CTRL,
  883. cache[SGTL5000_CHIP_CLK_CTRL]);
  884. snd_soc_write(codec, SGTL5000_CHIP_REF_CTRL,
  885. cache[SGTL5000_CHIP_REF_CTRL]);
  886. snd_soc_write(codec, SGTL5000_CHIP_LINE_OUT_CTRL,
  887. cache[SGTL5000_CHIP_LINE_OUT_CTRL]);
  888. return 0;
  889. }
  890. static int sgtl5000_resume(struct snd_soc_codec *codec)
  891. {
  892. /* Bring the codec back up to standby to enable regulators */
  893. sgtl5000_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  894. /* Restore registers by cached in memory */
  895. sgtl5000_restore_regs(codec);
  896. return 0;
  897. }
  898. #else
  899. #define sgtl5000_suspend NULL
  900. #define sgtl5000_resume NULL
  901. #endif /* CONFIG_SUSPEND */
  902. /*
  903. * sgtl5000 has 3 internal power supplies:
  904. * 1. VAG, normally set to vdda/2
  905. * 2. chargepump, set to different value
  906. * according to voltage of vdda and vddio
  907. * 3. line out VAG, normally set to vddio/2
  908. *
  909. * and should be set according to:
  910. * 1. vddd provided by external or not
  911. * 2. vdda and vddio voltage value. > 3.1v or not
  912. * 3. chip revision >=0x11 or not. If >=0x11, not use external vddd.
  913. */
  914. static int sgtl5000_set_power_regs(struct snd_soc_codec *codec)
  915. {
  916. int vddd;
  917. int vdda;
  918. int vddio;
  919. u16 ana_pwr;
  920. u16 lreg_ctrl;
  921. int vag;
  922. struct sgtl5000_priv *sgtl5000 = snd_soc_codec_get_drvdata(codec);
  923. vdda = regulator_get_voltage(sgtl5000->supplies[VDDA].consumer);
  924. vddio = regulator_get_voltage(sgtl5000->supplies[VDDIO].consumer);
  925. vddd = regulator_get_voltage(sgtl5000->supplies[VDDD].consumer);
  926. vdda = vdda / 1000;
  927. vddio = vddio / 1000;
  928. vddd = vddd / 1000;
  929. if (vdda <= 0 || vddio <= 0 || vddd < 0) {
  930. dev_err(codec->dev, "regulator voltage not set correctly\n");
  931. return -EINVAL;
  932. }
  933. /* according to datasheet, maximum voltage of supplies */
  934. if (vdda > 3600 || vddio > 3600 || vddd > 1980) {
  935. dev_err(codec->dev,
  936. "exceed max voltage vdda %dmV vddio %dmV vddd %dmV\n",
  937. vdda, vddio, vddd);
  938. return -EINVAL;
  939. }
  940. /* reset value */
  941. ana_pwr = snd_soc_read(codec, SGTL5000_CHIP_ANA_POWER);
  942. ana_pwr |= SGTL5000_DAC_STEREO |
  943. SGTL5000_ADC_STEREO |
  944. SGTL5000_REFTOP_POWERUP;
  945. lreg_ctrl = snd_soc_read(codec, SGTL5000_CHIP_LINREG_CTRL);
  946. if (vddio < 3100 && vdda < 3100) {
  947. /* enable internal oscillator used for charge pump */
  948. snd_soc_update_bits(codec, SGTL5000_CHIP_CLK_TOP_CTRL,
  949. SGTL5000_INT_OSC_EN,
  950. SGTL5000_INT_OSC_EN);
  951. /* Enable VDDC charge pump */
  952. ana_pwr |= SGTL5000_VDDC_CHRGPMP_POWERUP;
  953. } else if (vddio >= 3100 && vdda >= 3100) {
  954. ana_pwr &= ~SGTL5000_VDDC_CHRGPMP_POWERUP;
  955. /* VDDC use VDDIO rail */
  956. lreg_ctrl |= SGTL5000_VDDC_ASSN_OVRD;
  957. lreg_ctrl |= SGTL5000_VDDC_MAN_ASSN_VDDIO <<
  958. SGTL5000_VDDC_MAN_ASSN_SHIFT;
  959. }
  960. snd_soc_write(codec, SGTL5000_CHIP_LINREG_CTRL, lreg_ctrl);
  961. snd_soc_write(codec, SGTL5000_CHIP_ANA_POWER, ana_pwr);
  962. /* set voltage to register */
  963. snd_soc_update_bits(codec, SGTL5000_CHIP_LINREG_CTRL,
  964. SGTL5000_LINREG_VDDD_MASK, 0x8);
  965. /*
  966. * if vddd linear reg has been enabled,
  967. * simple digital supply should be clear to get
  968. * proper VDDD voltage.
  969. */
  970. if (ana_pwr & SGTL5000_LINEREG_D_POWERUP)
  971. snd_soc_update_bits(codec, SGTL5000_CHIP_ANA_POWER,
  972. SGTL5000_LINREG_SIMPLE_POWERUP,
  973. 0);
  974. else
  975. snd_soc_update_bits(codec, SGTL5000_CHIP_ANA_POWER,
  976. SGTL5000_LINREG_SIMPLE_POWERUP |
  977. SGTL5000_STARTUP_POWERUP,
  978. 0);
  979. /*
  980. * set ADC/DAC VAG to vdda / 2,
  981. * should stay in range (0.8v, 1.575v)
  982. */
  983. vag = vdda / 2;
  984. if (vag <= SGTL5000_ANA_GND_BASE)
  985. vag = 0;
  986. else if (vag >= SGTL5000_ANA_GND_BASE + SGTL5000_ANA_GND_STP *
  987. (SGTL5000_ANA_GND_MASK >> SGTL5000_ANA_GND_SHIFT))
  988. vag = SGTL5000_ANA_GND_MASK >> SGTL5000_ANA_GND_SHIFT;
  989. else
  990. vag = (vag - SGTL5000_ANA_GND_BASE) / SGTL5000_ANA_GND_STP;
  991. snd_soc_update_bits(codec, SGTL5000_CHIP_REF_CTRL,
  992. SGTL5000_ANA_GND_MASK, vag << SGTL5000_ANA_GND_SHIFT);
  993. /* set line out VAG to vddio / 2, in range (0.8v, 1.675v) */
  994. vag = vddio / 2;
  995. if (vag <= SGTL5000_LINE_OUT_GND_BASE)
  996. vag = 0;
  997. else if (vag >= SGTL5000_LINE_OUT_GND_BASE +
  998. SGTL5000_LINE_OUT_GND_STP * SGTL5000_LINE_OUT_GND_MAX)
  999. vag = SGTL5000_LINE_OUT_GND_MAX;
  1000. else
  1001. vag = (vag - SGTL5000_LINE_OUT_GND_BASE) /
  1002. SGTL5000_LINE_OUT_GND_STP;
  1003. snd_soc_update_bits(codec, SGTL5000_CHIP_LINE_OUT_CTRL,
  1004. SGTL5000_LINE_OUT_CURRENT_MASK |
  1005. SGTL5000_LINE_OUT_GND_MASK,
  1006. vag << SGTL5000_LINE_OUT_GND_SHIFT |
  1007. SGTL5000_LINE_OUT_CURRENT_360u <<
  1008. SGTL5000_LINE_OUT_CURRENT_SHIFT);
  1009. return 0;
  1010. }
  1011. static int sgtl5000_replace_vddd_with_ldo(struct snd_soc_codec *codec)
  1012. {
  1013. struct sgtl5000_priv *sgtl5000 = snd_soc_codec_get_drvdata(codec);
  1014. int ret;
  1015. /* set internal ldo to 1.2v */
  1016. ret = ldo_regulator_register(codec, &ldo_init_data, LDO_VOLTAGE);
  1017. if (ret) {
  1018. dev_err(codec->dev,
  1019. "Failed to register vddd internal supplies: %d\n", ret);
  1020. return ret;
  1021. }
  1022. sgtl5000->supplies[VDDD].supply = LDO_CONSUMER_NAME;
  1023. ret = regulator_bulk_get(codec->dev, ARRAY_SIZE(sgtl5000->supplies),
  1024. sgtl5000->supplies);
  1025. if (ret) {
  1026. ldo_regulator_remove(codec);
  1027. dev_err(codec->dev, "Failed to request supplies: %d\n", ret);
  1028. return ret;
  1029. }
  1030. dev_info(codec->dev, "Using internal LDO instead of VDDD\n");
  1031. return 0;
  1032. }
  1033. static int sgtl5000_enable_regulators(struct snd_soc_codec *codec)
  1034. {
  1035. u16 reg;
  1036. int ret;
  1037. int rev;
  1038. int i;
  1039. int external_vddd = 0;
  1040. struct sgtl5000_priv *sgtl5000 = snd_soc_codec_get_drvdata(codec);
  1041. for (i = 0; i < ARRAY_SIZE(sgtl5000->supplies); i++)
  1042. sgtl5000->supplies[i].supply = supply_names[i];
  1043. ret = regulator_bulk_get(codec->dev, ARRAY_SIZE(sgtl5000->supplies),
  1044. sgtl5000->supplies);
  1045. if (!ret)
  1046. external_vddd = 1;
  1047. else {
  1048. ret = sgtl5000_replace_vddd_with_ldo(codec);
  1049. if (ret)
  1050. return ret;
  1051. }
  1052. ret = regulator_bulk_enable(ARRAY_SIZE(sgtl5000->supplies),
  1053. sgtl5000->supplies);
  1054. if (ret)
  1055. goto err_regulator_free;
  1056. /* wait for all power rails bring up */
  1057. udelay(10);
  1058. /* Need 8 clocks before I2C accesses */
  1059. udelay(1);
  1060. /* read chip information */
  1061. reg = snd_soc_read(codec, SGTL5000_CHIP_ID);
  1062. if (((reg & SGTL5000_PARTID_MASK) >> SGTL5000_PARTID_SHIFT) !=
  1063. SGTL5000_PARTID_PART_ID) {
  1064. dev_err(codec->dev,
  1065. "Device with ID register %x is not a sgtl5000\n", reg);
  1066. ret = -ENODEV;
  1067. goto err_regulator_disable;
  1068. }
  1069. rev = (reg & SGTL5000_REVID_MASK) >> SGTL5000_REVID_SHIFT;
  1070. dev_info(codec->dev, "sgtl5000 revision 0x%x\n", rev);
  1071. /*
  1072. * workaround for revision 0x11 and later,
  1073. * roll back to use internal LDO
  1074. */
  1075. if (external_vddd && rev >= 0x11) {
  1076. /* disable all regulator first */
  1077. regulator_bulk_disable(ARRAY_SIZE(sgtl5000->supplies),
  1078. sgtl5000->supplies);
  1079. /* free VDDD regulator */
  1080. regulator_bulk_free(ARRAY_SIZE(sgtl5000->supplies),
  1081. sgtl5000->supplies);
  1082. ret = sgtl5000_replace_vddd_with_ldo(codec);
  1083. if (ret)
  1084. return ret;
  1085. ret = regulator_bulk_enable(ARRAY_SIZE(sgtl5000->supplies),
  1086. sgtl5000->supplies);
  1087. if (ret)
  1088. goto err_regulator_free;
  1089. /* wait for all power rails bring up */
  1090. udelay(10);
  1091. }
  1092. return 0;
  1093. err_regulator_disable:
  1094. regulator_bulk_disable(ARRAY_SIZE(sgtl5000->supplies),
  1095. sgtl5000->supplies);
  1096. err_regulator_free:
  1097. regulator_bulk_free(ARRAY_SIZE(sgtl5000->supplies),
  1098. sgtl5000->supplies);
  1099. if (external_vddd)
  1100. ldo_regulator_remove(codec);
  1101. return ret;
  1102. }
  1103. static int sgtl5000_probe(struct snd_soc_codec *codec)
  1104. {
  1105. int ret;
  1106. struct sgtl5000_priv *sgtl5000 = snd_soc_codec_get_drvdata(codec);
  1107. /* setup i2c data ops */
  1108. ret = snd_soc_codec_set_cache_io(codec, 16, 16, SND_SOC_I2C);
  1109. if (ret < 0) {
  1110. dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
  1111. return ret;
  1112. }
  1113. ret = sgtl5000_enable_regulators(codec);
  1114. if (ret)
  1115. return ret;
  1116. /* power up sgtl5000 */
  1117. ret = sgtl5000_set_power_regs(codec);
  1118. if (ret)
  1119. goto err;
  1120. /* enable small pop, introduce 400ms delay in turning off */
  1121. snd_soc_update_bits(codec, SGTL5000_CHIP_REF_CTRL,
  1122. SGTL5000_SMALL_POP, 1);
  1123. /* disable short cut detector */
  1124. snd_soc_write(codec, SGTL5000_CHIP_SHORT_CTRL, 0);
  1125. /*
  1126. * set i2s as default input of sound switch
  1127. * TODO: add sound switch to control and dapm widge.
  1128. */
  1129. snd_soc_write(codec, SGTL5000_CHIP_SSS_CTRL,
  1130. SGTL5000_DAC_SEL_I2S_IN << SGTL5000_DAC_SEL_SHIFT);
  1131. snd_soc_write(codec, SGTL5000_CHIP_DIG_POWER,
  1132. SGTL5000_ADC_EN | SGTL5000_DAC_EN);
  1133. /* enable dac volume ramp by default */
  1134. snd_soc_write(codec, SGTL5000_CHIP_ADCDAC_CTRL,
  1135. SGTL5000_DAC_VOL_RAMP_EN |
  1136. SGTL5000_DAC_MUTE_RIGHT |
  1137. SGTL5000_DAC_MUTE_LEFT);
  1138. snd_soc_write(codec, SGTL5000_CHIP_PAD_STRENGTH, 0x015f);
  1139. snd_soc_write(codec, SGTL5000_CHIP_ANA_CTRL,
  1140. SGTL5000_HP_ZCD_EN |
  1141. SGTL5000_ADC_ZCD_EN);
  1142. snd_soc_write(codec, SGTL5000_CHIP_MIC_CTRL, 0);
  1143. /*
  1144. * disable DAP
  1145. * TODO:
  1146. * Enable DAP in kcontrol and dapm.
  1147. */
  1148. snd_soc_write(codec, SGTL5000_DAP_CTRL, 0);
  1149. /* leading to standby state */
  1150. ret = sgtl5000_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  1151. if (ret)
  1152. goto err;
  1153. snd_soc_dapm_new_widgets(&codec->dapm);
  1154. return 0;
  1155. err:
  1156. regulator_bulk_disable(ARRAY_SIZE(sgtl5000->supplies),
  1157. sgtl5000->supplies);
  1158. regulator_bulk_free(ARRAY_SIZE(sgtl5000->supplies),
  1159. sgtl5000->supplies);
  1160. ldo_regulator_remove(codec);
  1161. return ret;
  1162. }
  1163. static int sgtl5000_remove(struct snd_soc_codec *codec)
  1164. {
  1165. struct sgtl5000_priv *sgtl5000 = snd_soc_codec_get_drvdata(codec);
  1166. sgtl5000_set_bias_level(codec, SND_SOC_BIAS_OFF);
  1167. regulator_bulk_disable(ARRAY_SIZE(sgtl5000->supplies),
  1168. sgtl5000->supplies);
  1169. regulator_bulk_free(ARRAY_SIZE(sgtl5000->supplies),
  1170. sgtl5000->supplies);
  1171. ldo_regulator_remove(codec);
  1172. return 0;
  1173. }
  1174. static struct snd_soc_codec_driver sgtl5000_driver = {
  1175. .probe = sgtl5000_probe,
  1176. .remove = sgtl5000_remove,
  1177. .suspend = sgtl5000_suspend,
  1178. .resume = sgtl5000_resume,
  1179. .set_bias_level = sgtl5000_set_bias_level,
  1180. .reg_cache_size = ARRAY_SIZE(sgtl5000_regs),
  1181. .reg_word_size = sizeof(u16),
  1182. .reg_cache_step = 2,
  1183. .reg_cache_default = sgtl5000_regs,
  1184. .volatile_register = sgtl5000_volatile_register,
  1185. .controls = sgtl5000_snd_controls,
  1186. .num_controls = ARRAY_SIZE(sgtl5000_snd_controls),
  1187. .dapm_widgets = sgtl5000_dapm_widgets,
  1188. .num_dapm_widgets = ARRAY_SIZE(sgtl5000_dapm_widgets),
  1189. .dapm_routes = sgtl5000_dapm_routes,
  1190. .num_dapm_routes = ARRAY_SIZE(sgtl5000_dapm_routes),
  1191. };
  1192. static __devinit int sgtl5000_i2c_probe(struct i2c_client *client,
  1193. const struct i2c_device_id *id)
  1194. {
  1195. struct sgtl5000_priv *sgtl5000;
  1196. int ret;
  1197. sgtl5000 = devm_kzalloc(&client->dev, sizeof(struct sgtl5000_priv),
  1198. GFP_KERNEL);
  1199. if (!sgtl5000)
  1200. return -ENOMEM;
  1201. i2c_set_clientdata(client, sgtl5000);
  1202. ret = snd_soc_register_codec(&client->dev,
  1203. &sgtl5000_driver, &sgtl5000_dai, 1);
  1204. return ret;
  1205. }
  1206. static __devexit int sgtl5000_i2c_remove(struct i2c_client *client)
  1207. {
  1208. snd_soc_unregister_codec(&client->dev);
  1209. return 0;
  1210. }
  1211. static const struct i2c_device_id sgtl5000_id[] = {
  1212. {"sgtl5000", 0},
  1213. {},
  1214. };
  1215. MODULE_DEVICE_TABLE(i2c, sgtl5000_id);
  1216. static const struct of_device_id sgtl5000_dt_ids[] = {
  1217. { .compatible = "fsl,sgtl5000", },
  1218. { /* sentinel */ }
  1219. };
  1220. MODULE_DEVICE_TABLE(of, sgtl5000_dt_ids);
  1221. static struct i2c_driver sgtl5000_i2c_driver = {
  1222. .driver = {
  1223. .name = "sgtl5000",
  1224. .owner = THIS_MODULE,
  1225. .of_match_table = sgtl5000_dt_ids,
  1226. },
  1227. .probe = sgtl5000_i2c_probe,
  1228. .remove = __devexit_p(sgtl5000_i2c_remove),
  1229. .id_table = sgtl5000_id,
  1230. };
  1231. static int __init sgtl5000_modinit(void)
  1232. {
  1233. return i2c_add_driver(&sgtl5000_i2c_driver);
  1234. }
  1235. module_init(sgtl5000_modinit);
  1236. static void __exit sgtl5000_exit(void)
  1237. {
  1238. i2c_del_driver(&sgtl5000_i2c_driver);
  1239. }
  1240. module_exit(sgtl5000_exit);
  1241. MODULE_DESCRIPTION("Freescale SGTL5000 ALSA SoC Codec Driver");
  1242. MODULE_AUTHOR("Zeng Zhaoming <zengzm.kernel@gmail.com>");
  1243. MODULE_LICENSE("GPL");