uda1380.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888
  1. /*
  2. * uda1380.c - Philips UDA1380 ALSA SoC audio driver
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. *
  8. * Copyright (c) 2007-2009 Philipp Zabel <philipp.zabel@gmail.com>
  9. *
  10. * Modified by Richard Purdie <richard@openedhand.com> to fit into SoC
  11. * codec model.
  12. *
  13. * Copyright (c) 2005 Giorgio Padrin <giorgio@mandarinlogiq.org>
  14. * Copyright 2005 Openedhand Ltd.
  15. */
  16. #include <linux/module.h>
  17. #include <linux/init.h>
  18. #include <linux/types.h>
  19. #include <linux/slab.h>
  20. #include <linux/errno.h>
  21. #include <linux/gpio.h>
  22. #include <linux/delay.h>
  23. #include <linux/i2c.h>
  24. #include <linux/workqueue.h>
  25. #include <sound/core.h>
  26. #include <sound/control.h>
  27. #include <sound/initval.h>
  28. #include <sound/soc.h>
  29. #include <sound/soc-dapm.h>
  30. #include <sound/tlv.h>
  31. #include <sound/uda1380.h>
  32. #include "uda1380.h"
  33. /* codec private data */
  34. struct uda1380_priv {
  35. struct snd_soc_codec *codec;
  36. u16 reg_cache[UDA1380_CACHEREGNUM];
  37. unsigned int dac_clk;
  38. struct work_struct work;
  39. void *control_data;
  40. };
  41. /*
  42. * uda1380 register cache
  43. */
  44. static const u16 uda1380_reg[UDA1380_CACHEREGNUM] = {
  45. 0x0502, 0x0000, 0x0000, 0x3f3f,
  46. 0x0202, 0x0000, 0x0000, 0x0000,
  47. 0x0000, 0x0000, 0x0000, 0x0000,
  48. 0x0000, 0x0000, 0x0000, 0x0000,
  49. 0x0000, 0xff00, 0x0000, 0x4800,
  50. 0x0000, 0x0000, 0x0000, 0x0000,
  51. 0x0000, 0x0000, 0x0000, 0x0000,
  52. 0x0000, 0x0000, 0x0000, 0x0000,
  53. 0x0000, 0x8000, 0x0002, 0x0000,
  54. };
  55. static unsigned long uda1380_cache_dirty;
  56. /*
  57. * read uda1380 register cache
  58. */
  59. static inline unsigned int uda1380_read_reg_cache(struct snd_soc_codec *codec,
  60. unsigned int reg)
  61. {
  62. u16 *cache = codec->reg_cache;
  63. if (reg == UDA1380_RESET)
  64. return 0;
  65. if (reg >= UDA1380_CACHEREGNUM)
  66. return -1;
  67. return cache[reg];
  68. }
  69. /*
  70. * write uda1380 register cache
  71. */
  72. static inline void uda1380_write_reg_cache(struct snd_soc_codec *codec,
  73. u16 reg, unsigned int value)
  74. {
  75. u16 *cache = codec->reg_cache;
  76. if (reg >= UDA1380_CACHEREGNUM)
  77. return;
  78. if ((reg >= 0x10) && (cache[reg] != value))
  79. set_bit(reg - 0x10, &uda1380_cache_dirty);
  80. cache[reg] = value;
  81. }
  82. /*
  83. * write to the UDA1380 register space
  84. */
  85. static int uda1380_write(struct snd_soc_codec *codec, unsigned int reg,
  86. unsigned int value)
  87. {
  88. u8 data[3];
  89. /* data is
  90. * data[0] is register offset
  91. * data[1] is MS byte
  92. * data[2] is LS byte
  93. */
  94. data[0] = reg;
  95. data[1] = (value & 0xff00) >> 8;
  96. data[2] = value & 0x00ff;
  97. uda1380_write_reg_cache(codec, reg, value);
  98. /* the interpolator & decimator regs must only be written when the
  99. * codec DAI is active.
  100. */
  101. if (!codec->active && (reg >= UDA1380_MVOL))
  102. return 0;
  103. pr_debug("uda1380: hw write %x val %x\n", reg, value);
  104. if (codec->hw_write(codec->control_data, data, 3) == 3) {
  105. unsigned int val;
  106. i2c_master_send(codec->control_data, data, 1);
  107. i2c_master_recv(codec->control_data, data, 2);
  108. val = (data[0]<<8) | data[1];
  109. if (val != value) {
  110. pr_debug("uda1380: READ BACK VAL %x\n",
  111. (data[0]<<8) | data[1]);
  112. return -EIO;
  113. }
  114. if (reg >= 0x10)
  115. clear_bit(reg - 0x10, &uda1380_cache_dirty);
  116. return 0;
  117. } else
  118. return -EIO;
  119. }
  120. static void uda1380_sync_cache(struct snd_soc_codec *codec)
  121. {
  122. int reg;
  123. u8 data[3];
  124. u16 *cache = codec->reg_cache;
  125. /* Sync reg_cache with the hardware */
  126. for (reg = 0; reg < UDA1380_MVOL; reg++) {
  127. data[0] = reg;
  128. data[1] = (cache[reg] & 0xff00) >> 8;
  129. data[2] = cache[reg] & 0x00ff;
  130. if (codec->hw_write(codec->control_data, data, 3) != 3)
  131. dev_err(codec->dev, "%s: write to reg 0x%x failed\n",
  132. __func__, reg);
  133. }
  134. }
  135. static int uda1380_reset(struct snd_soc_codec *codec)
  136. {
  137. struct uda1380_platform_data *pdata = codec->dev->platform_data;
  138. if (gpio_is_valid(pdata->gpio_reset)) {
  139. gpio_set_value(pdata->gpio_reset, 1);
  140. mdelay(1);
  141. gpio_set_value(pdata->gpio_reset, 0);
  142. } else {
  143. u8 data[3];
  144. data[0] = UDA1380_RESET;
  145. data[1] = 0;
  146. data[2] = 0;
  147. if (codec->hw_write(codec->control_data, data, 3) != 3) {
  148. dev_err(codec->dev, "%s: failed\n", __func__);
  149. return -EIO;
  150. }
  151. }
  152. return 0;
  153. }
  154. static void uda1380_flush_work(struct work_struct *work)
  155. {
  156. struct uda1380_priv *uda1380 = container_of(work, struct uda1380_priv, work);
  157. struct snd_soc_codec *uda1380_codec = uda1380->codec;
  158. int bit, reg;
  159. for_each_set_bit(bit, &uda1380_cache_dirty, UDA1380_CACHEREGNUM - 0x10) {
  160. reg = 0x10 + bit;
  161. pr_debug("uda1380: flush reg %x val %x:\n", reg,
  162. uda1380_read_reg_cache(uda1380_codec, reg));
  163. uda1380_write(uda1380_codec, reg,
  164. uda1380_read_reg_cache(uda1380_codec, reg));
  165. clear_bit(bit, &uda1380_cache_dirty);
  166. }
  167. }
  168. /* declarations of ALSA reg_elem_REAL controls */
  169. static const char *uda1380_deemp[] = {
  170. "None",
  171. "32kHz",
  172. "44.1kHz",
  173. "48kHz",
  174. "96kHz",
  175. };
  176. static const char *uda1380_input_sel[] = {
  177. "Line",
  178. "Mic + Line R",
  179. "Line L",
  180. "Mic",
  181. };
  182. static const char *uda1380_output_sel[] = {
  183. "DAC",
  184. "Analog Mixer",
  185. };
  186. static const char *uda1380_spf_mode[] = {
  187. "Flat",
  188. "Minimum1",
  189. "Minimum2",
  190. "Maximum"
  191. };
  192. static const char *uda1380_capture_sel[] = {
  193. "ADC",
  194. "Digital Mixer"
  195. };
  196. static const char *uda1380_sel_ns[] = {
  197. "3rd-order",
  198. "5th-order"
  199. };
  200. static const char *uda1380_mix_control[] = {
  201. "off",
  202. "PCM only",
  203. "before sound processing",
  204. "after sound processing"
  205. };
  206. static const char *uda1380_sdet_setting[] = {
  207. "3200",
  208. "4800",
  209. "9600",
  210. "19200"
  211. };
  212. static const char *uda1380_os_setting[] = {
  213. "single-speed",
  214. "double-speed (no mixing)",
  215. "quad-speed (no mixing)"
  216. };
  217. static const struct soc_enum uda1380_deemp_enum[] = {
  218. SOC_ENUM_SINGLE(UDA1380_DEEMP, 8, 5, uda1380_deemp),
  219. SOC_ENUM_SINGLE(UDA1380_DEEMP, 0, 5, uda1380_deemp),
  220. };
  221. static const struct soc_enum uda1380_input_sel_enum =
  222. SOC_ENUM_SINGLE(UDA1380_ADC, 2, 4, uda1380_input_sel); /* SEL_MIC, SEL_LNA */
  223. static const struct soc_enum uda1380_output_sel_enum =
  224. SOC_ENUM_SINGLE(UDA1380_PM, 7, 2, uda1380_output_sel); /* R02_EN_AVC */
  225. static const struct soc_enum uda1380_spf_enum =
  226. SOC_ENUM_SINGLE(UDA1380_MODE, 14, 4, uda1380_spf_mode); /* M */
  227. static const struct soc_enum uda1380_capture_sel_enum =
  228. SOC_ENUM_SINGLE(UDA1380_IFACE, 6, 2, uda1380_capture_sel); /* SEL_SOURCE */
  229. static const struct soc_enum uda1380_sel_ns_enum =
  230. SOC_ENUM_SINGLE(UDA1380_MIXER, 14, 2, uda1380_sel_ns); /* SEL_NS */
  231. static const struct soc_enum uda1380_mix_enum =
  232. SOC_ENUM_SINGLE(UDA1380_MIXER, 12, 4, uda1380_mix_control); /* MIX, MIX_POS */
  233. static const struct soc_enum uda1380_sdet_enum =
  234. SOC_ENUM_SINGLE(UDA1380_MIXER, 4, 4, uda1380_sdet_setting); /* SD_VALUE */
  235. static const struct soc_enum uda1380_os_enum =
  236. SOC_ENUM_SINGLE(UDA1380_MIXER, 0, 3, uda1380_os_setting); /* OS */
  237. /*
  238. * from -48 dB in 1.5 dB steps (mute instead of -49.5 dB)
  239. */
  240. static DECLARE_TLV_DB_SCALE(amix_tlv, -4950, 150, 1);
  241. /*
  242. * from -78 dB in 1 dB steps (3 dB steps, really. LSB are ignored),
  243. * from -66 dB in 0.5 dB steps (2 dB steps, really) and
  244. * from -52 dB in 0.25 dB steps
  245. */
  246. static const unsigned int mvol_tlv[] = {
  247. TLV_DB_RANGE_HEAD(3),
  248. 0, 15, TLV_DB_SCALE_ITEM(-8200, 100, 1),
  249. 16, 43, TLV_DB_SCALE_ITEM(-6600, 50, 0),
  250. 44, 252, TLV_DB_SCALE_ITEM(-5200, 25, 0),
  251. };
  252. /*
  253. * from -72 dB in 1.5 dB steps (6 dB steps really),
  254. * from -66 dB in 0.75 dB steps (3 dB steps really),
  255. * from -60 dB in 0.5 dB steps (2 dB steps really) and
  256. * from -46 dB in 0.25 dB steps
  257. */
  258. static const unsigned int vc_tlv[] = {
  259. TLV_DB_RANGE_HEAD(4),
  260. 0, 7, TLV_DB_SCALE_ITEM(-7800, 150, 1),
  261. 8, 15, TLV_DB_SCALE_ITEM(-6600, 75, 0),
  262. 16, 43, TLV_DB_SCALE_ITEM(-6000, 50, 0),
  263. 44, 228, TLV_DB_SCALE_ITEM(-4600, 25, 0),
  264. };
  265. /* from 0 to 6 dB in 2 dB steps if SPF mode != flat */
  266. static DECLARE_TLV_DB_SCALE(tr_tlv, 0, 200, 0);
  267. /* from 0 to 24 dB in 2 dB steps, if SPF mode == maximum, otherwise cuts
  268. * off at 18 dB max) */
  269. static DECLARE_TLV_DB_SCALE(bb_tlv, 0, 200, 0);
  270. /* from -63 to 24 dB in 0.5 dB steps (-128...48) */
  271. static DECLARE_TLV_DB_SCALE(dec_tlv, -6400, 50, 1);
  272. /* from 0 to 24 dB in 3 dB steps */
  273. static DECLARE_TLV_DB_SCALE(pga_tlv, 0, 300, 0);
  274. /* from 0 to 30 dB in 2 dB steps */
  275. static DECLARE_TLV_DB_SCALE(vga_tlv, 0, 200, 0);
  276. static const struct snd_kcontrol_new uda1380_snd_controls[] = {
  277. SOC_DOUBLE_TLV("Analog Mixer Volume", UDA1380_AMIX, 0, 8, 44, 1, amix_tlv), /* AVCR, AVCL */
  278. SOC_DOUBLE_TLV("Master Playback Volume", UDA1380_MVOL, 0, 8, 252, 1, mvol_tlv), /* MVCL, MVCR */
  279. SOC_SINGLE_TLV("ADC Playback Volume", UDA1380_MIXVOL, 8, 228, 1, vc_tlv), /* VC2 */
  280. SOC_SINGLE_TLV("PCM Playback Volume", UDA1380_MIXVOL, 0, 228, 1, vc_tlv), /* VC1 */
  281. SOC_ENUM("Sound Processing Filter", uda1380_spf_enum), /* M */
  282. SOC_DOUBLE_TLV("Tone Control - Treble", UDA1380_MODE, 4, 12, 3, 0, tr_tlv), /* TRL, TRR */
  283. SOC_DOUBLE_TLV("Tone Control - Bass", UDA1380_MODE, 0, 8, 15, 0, bb_tlv), /* BBL, BBR */
  284. /**/ SOC_SINGLE("Master Playback Switch", UDA1380_DEEMP, 14, 1, 1), /* MTM */
  285. SOC_SINGLE("ADC Playback Switch", UDA1380_DEEMP, 11, 1, 1), /* MT2 from decimation filter */
  286. SOC_ENUM("ADC Playback De-emphasis", uda1380_deemp_enum[0]), /* DE2 */
  287. SOC_SINGLE("PCM Playback Switch", UDA1380_DEEMP, 3, 1, 1), /* MT1, from digital data input */
  288. SOC_ENUM("PCM Playback De-emphasis", uda1380_deemp_enum[1]), /* DE1 */
  289. SOC_SINGLE("DAC Polarity inverting Switch", UDA1380_MIXER, 15, 1, 0), /* DA_POL_INV */
  290. SOC_ENUM("Noise Shaper", uda1380_sel_ns_enum), /* SEL_NS */
  291. SOC_ENUM("Digital Mixer Signal Control", uda1380_mix_enum), /* MIX_POS, MIX */
  292. SOC_SINGLE("Silence Detector Switch", UDA1380_MIXER, 6, 1, 0), /* SDET_ON */
  293. SOC_ENUM("Silence Detector Setting", uda1380_sdet_enum), /* SD_VALUE */
  294. SOC_ENUM("Oversampling Input", uda1380_os_enum), /* OS */
  295. SOC_DOUBLE_S8_TLV("ADC Capture Volume", UDA1380_DEC, -128, 48, dec_tlv), /* ML_DEC, MR_DEC */
  296. /**/ SOC_SINGLE("ADC Capture Switch", UDA1380_PGA, 15, 1, 1), /* MT_ADC */
  297. SOC_DOUBLE_TLV("Line Capture Volume", UDA1380_PGA, 0, 8, 8, 0, pga_tlv), /* PGA_GAINCTRLL, PGA_GAINCTRLR */
  298. SOC_SINGLE("ADC Polarity inverting Switch", UDA1380_ADC, 12, 1, 0), /* ADCPOL_INV */
  299. SOC_SINGLE_TLV("Mic Capture Volume", UDA1380_ADC, 8, 15, 0, vga_tlv), /* VGA_CTRL */
  300. SOC_SINGLE("DC Filter Bypass Switch", UDA1380_ADC, 1, 1, 0), /* SKIP_DCFIL (before decimator) */
  301. SOC_SINGLE("DC Filter Enable Switch", UDA1380_ADC, 0, 1, 0), /* EN_DCFIL (at output of decimator) */
  302. SOC_SINGLE("AGC Timing", UDA1380_AGC, 8, 7, 0), /* TODO: enum, see table 62 */
  303. SOC_SINGLE("AGC Target level", UDA1380_AGC, 2, 3, 1), /* AGC_LEVEL */
  304. /* -5.5, -8, -11.5, -14 dBFS */
  305. SOC_SINGLE("AGC Switch", UDA1380_AGC, 0, 1, 0),
  306. };
  307. /* Input mux */
  308. static const struct snd_kcontrol_new uda1380_input_mux_control =
  309. SOC_DAPM_ENUM("Route", uda1380_input_sel_enum);
  310. /* Output mux */
  311. static const struct snd_kcontrol_new uda1380_output_mux_control =
  312. SOC_DAPM_ENUM("Route", uda1380_output_sel_enum);
  313. /* Capture mux */
  314. static const struct snd_kcontrol_new uda1380_capture_mux_control =
  315. SOC_DAPM_ENUM("Route", uda1380_capture_sel_enum);
  316. static const struct snd_soc_dapm_widget uda1380_dapm_widgets[] = {
  317. SND_SOC_DAPM_MUX("Input Mux", SND_SOC_NOPM, 0, 0,
  318. &uda1380_input_mux_control),
  319. SND_SOC_DAPM_MUX("Output Mux", SND_SOC_NOPM, 0, 0,
  320. &uda1380_output_mux_control),
  321. SND_SOC_DAPM_MUX("Capture Mux", SND_SOC_NOPM, 0, 0,
  322. &uda1380_capture_mux_control),
  323. SND_SOC_DAPM_PGA("Left PGA", UDA1380_PM, 3, 0, NULL, 0),
  324. SND_SOC_DAPM_PGA("Right PGA", UDA1380_PM, 1, 0, NULL, 0),
  325. SND_SOC_DAPM_PGA("Mic LNA", UDA1380_PM, 4, 0, NULL, 0),
  326. SND_SOC_DAPM_ADC("Left ADC", "Left Capture", UDA1380_PM, 2, 0),
  327. SND_SOC_DAPM_ADC("Right ADC", "Right Capture", UDA1380_PM, 0, 0),
  328. SND_SOC_DAPM_INPUT("VINM"),
  329. SND_SOC_DAPM_INPUT("VINL"),
  330. SND_SOC_DAPM_INPUT("VINR"),
  331. SND_SOC_DAPM_MIXER("Analog Mixer", UDA1380_PM, 6, 0, NULL, 0),
  332. SND_SOC_DAPM_OUTPUT("VOUTLHP"),
  333. SND_SOC_DAPM_OUTPUT("VOUTRHP"),
  334. SND_SOC_DAPM_OUTPUT("VOUTL"),
  335. SND_SOC_DAPM_OUTPUT("VOUTR"),
  336. SND_SOC_DAPM_DAC("DAC", "Playback", UDA1380_PM, 10, 0),
  337. SND_SOC_DAPM_PGA("HeadPhone Driver", UDA1380_PM, 13, 0, NULL, 0),
  338. };
  339. static const struct snd_soc_dapm_route audio_map[] = {
  340. /* output mux */
  341. {"HeadPhone Driver", NULL, "Output Mux"},
  342. {"VOUTR", NULL, "Output Mux"},
  343. {"VOUTL", NULL, "Output Mux"},
  344. {"Analog Mixer", NULL, "VINR"},
  345. {"Analog Mixer", NULL, "VINL"},
  346. {"Analog Mixer", NULL, "DAC"},
  347. {"Output Mux", "DAC", "DAC"},
  348. {"Output Mux", "Analog Mixer", "Analog Mixer"},
  349. /* {"DAC", "Digital Mixer", "I2S" } */
  350. /* headphone driver */
  351. {"VOUTLHP", NULL, "HeadPhone Driver"},
  352. {"VOUTRHP", NULL, "HeadPhone Driver"},
  353. /* input mux */
  354. {"Left ADC", NULL, "Input Mux"},
  355. {"Input Mux", "Mic", "Mic LNA"},
  356. {"Input Mux", "Mic + Line R", "Mic LNA"},
  357. {"Input Mux", "Line L", "Left PGA"},
  358. {"Input Mux", "Line", "Left PGA"},
  359. /* right input */
  360. {"Right ADC", "Mic + Line R", "Right PGA"},
  361. {"Right ADC", "Line", "Right PGA"},
  362. /* inputs */
  363. {"Mic LNA", NULL, "VINM"},
  364. {"Left PGA", NULL, "VINL"},
  365. {"Right PGA", NULL, "VINR"},
  366. };
  367. static int uda1380_add_widgets(struct snd_soc_codec *codec)
  368. {
  369. struct snd_soc_dapm_context *dapm = &codec->dapm;
  370. snd_soc_dapm_new_controls(dapm, uda1380_dapm_widgets,
  371. ARRAY_SIZE(uda1380_dapm_widgets));
  372. snd_soc_dapm_add_routes(dapm, audio_map, ARRAY_SIZE(audio_map));
  373. return 0;
  374. }
  375. static int uda1380_set_dai_fmt_both(struct snd_soc_dai *codec_dai,
  376. unsigned int fmt)
  377. {
  378. struct snd_soc_codec *codec = codec_dai->codec;
  379. int iface;
  380. /* set up DAI based upon fmt */
  381. iface = uda1380_read_reg_cache(codec, UDA1380_IFACE);
  382. iface &= ~(R01_SFORI_MASK | R01_SIM | R01_SFORO_MASK);
  383. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  384. case SND_SOC_DAIFMT_I2S:
  385. iface |= R01_SFORI_I2S | R01_SFORO_I2S;
  386. break;
  387. case SND_SOC_DAIFMT_LSB:
  388. iface |= R01_SFORI_LSB16 | R01_SFORO_LSB16;
  389. break;
  390. case SND_SOC_DAIFMT_MSB:
  391. iface |= R01_SFORI_MSB | R01_SFORO_MSB;
  392. }
  393. /* DATAI is slave only, so in single-link mode, this has to be slave */
  394. if ((fmt & SND_SOC_DAIFMT_MASTER_MASK) != SND_SOC_DAIFMT_CBS_CFS)
  395. return -EINVAL;
  396. uda1380_write(codec, UDA1380_IFACE, iface);
  397. return 0;
  398. }
  399. static int uda1380_set_dai_fmt_playback(struct snd_soc_dai *codec_dai,
  400. unsigned int fmt)
  401. {
  402. struct snd_soc_codec *codec = codec_dai->codec;
  403. int iface;
  404. /* set up DAI based upon fmt */
  405. iface = uda1380_read_reg_cache(codec, UDA1380_IFACE);
  406. iface &= ~R01_SFORI_MASK;
  407. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  408. case SND_SOC_DAIFMT_I2S:
  409. iface |= R01_SFORI_I2S;
  410. break;
  411. case SND_SOC_DAIFMT_LSB:
  412. iface |= R01_SFORI_LSB16;
  413. break;
  414. case SND_SOC_DAIFMT_MSB:
  415. iface |= R01_SFORI_MSB;
  416. }
  417. /* DATAI is slave only, so this has to be slave */
  418. if ((fmt & SND_SOC_DAIFMT_MASTER_MASK) != SND_SOC_DAIFMT_CBS_CFS)
  419. return -EINVAL;
  420. uda1380_write(codec, UDA1380_IFACE, iface);
  421. return 0;
  422. }
  423. static int uda1380_set_dai_fmt_capture(struct snd_soc_dai *codec_dai,
  424. unsigned int fmt)
  425. {
  426. struct snd_soc_codec *codec = codec_dai->codec;
  427. int iface;
  428. /* set up DAI based upon fmt */
  429. iface = uda1380_read_reg_cache(codec, UDA1380_IFACE);
  430. iface &= ~(R01_SIM | R01_SFORO_MASK);
  431. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  432. case SND_SOC_DAIFMT_I2S:
  433. iface |= R01_SFORO_I2S;
  434. break;
  435. case SND_SOC_DAIFMT_LSB:
  436. iface |= R01_SFORO_LSB16;
  437. break;
  438. case SND_SOC_DAIFMT_MSB:
  439. iface |= R01_SFORO_MSB;
  440. }
  441. if ((fmt & SND_SOC_DAIFMT_MASTER_MASK) == SND_SOC_DAIFMT_CBM_CFM)
  442. iface |= R01_SIM;
  443. uda1380_write(codec, UDA1380_IFACE, iface);
  444. return 0;
  445. }
  446. static int uda1380_trigger(struct snd_pcm_substream *substream, int cmd,
  447. struct snd_soc_dai *dai)
  448. {
  449. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  450. struct snd_soc_codec *codec = rtd->codec;
  451. struct uda1380_priv *uda1380 = snd_soc_codec_get_drvdata(codec);
  452. int mixer = uda1380_read_reg_cache(codec, UDA1380_MIXER);
  453. switch (cmd) {
  454. case SNDRV_PCM_TRIGGER_START:
  455. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  456. uda1380_write_reg_cache(codec, UDA1380_MIXER,
  457. mixer & ~R14_SILENCE);
  458. schedule_work(&uda1380->work);
  459. break;
  460. case SNDRV_PCM_TRIGGER_STOP:
  461. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  462. uda1380_write_reg_cache(codec, UDA1380_MIXER,
  463. mixer | R14_SILENCE);
  464. schedule_work(&uda1380->work);
  465. break;
  466. }
  467. return 0;
  468. }
  469. static int uda1380_pcm_hw_params(struct snd_pcm_substream *substream,
  470. struct snd_pcm_hw_params *params,
  471. struct snd_soc_dai *dai)
  472. {
  473. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  474. struct snd_soc_codec *codec = rtd->codec;
  475. u16 clk = uda1380_read_reg_cache(codec, UDA1380_CLK);
  476. /* set WSPLL power and divider if running from this clock */
  477. if (clk & R00_DAC_CLK) {
  478. int rate = params_rate(params);
  479. u16 pm = uda1380_read_reg_cache(codec, UDA1380_PM);
  480. clk &= ~0x3; /* clear SEL_LOOP_DIV */
  481. switch (rate) {
  482. case 6250 ... 12500:
  483. clk |= 0x0;
  484. break;
  485. case 12501 ... 25000:
  486. clk |= 0x1;
  487. break;
  488. case 25001 ... 50000:
  489. clk |= 0x2;
  490. break;
  491. case 50001 ... 100000:
  492. clk |= 0x3;
  493. break;
  494. }
  495. uda1380_write(codec, UDA1380_PM, R02_PON_PLL | pm);
  496. }
  497. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  498. clk |= R00_EN_DAC | R00_EN_INT;
  499. else
  500. clk |= R00_EN_ADC | R00_EN_DEC;
  501. uda1380_write(codec, UDA1380_CLK, clk);
  502. return 0;
  503. }
  504. static void uda1380_pcm_shutdown(struct snd_pcm_substream *substream,
  505. struct snd_soc_dai *dai)
  506. {
  507. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  508. struct snd_soc_codec *codec = rtd->codec;
  509. u16 clk = uda1380_read_reg_cache(codec, UDA1380_CLK);
  510. /* shut down WSPLL power if running from this clock */
  511. if (clk & R00_DAC_CLK) {
  512. u16 pm = uda1380_read_reg_cache(codec, UDA1380_PM);
  513. uda1380_write(codec, UDA1380_PM, ~R02_PON_PLL & pm);
  514. }
  515. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  516. clk &= ~(R00_EN_DAC | R00_EN_INT);
  517. else
  518. clk &= ~(R00_EN_ADC | R00_EN_DEC);
  519. uda1380_write(codec, UDA1380_CLK, clk);
  520. }
  521. static int uda1380_set_bias_level(struct snd_soc_codec *codec,
  522. enum snd_soc_bias_level level)
  523. {
  524. int pm = uda1380_read_reg_cache(codec, UDA1380_PM);
  525. int reg;
  526. struct uda1380_platform_data *pdata = codec->dev->platform_data;
  527. if (codec->dapm.bias_level == level)
  528. return 0;
  529. switch (level) {
  530. case SND_SOC_BIAS_ON:
  531. case SND_SOC_BIAS_PREPARE:
  532. /* ADC, DAC on */
  533. uda1380_write(codec, UDA1380_PM, R02_PON_BIAS | pm);
  534. break;
  535. case SND_SOC_BIAS_STANDBY:
  536. if (codec->dapm.bias_level == SND_SOC_BIAS_OFF) {
  537. if (gpio_is_valid(pdata->gpio_power)) {
  538. gpio_set_value(pdata->gpio_power, 1);
  539. mdelay(1);
  540. uda1380_reset(codec);
  541. }
  542. uda1380_sync_cache(codec);
  543. }
  544. uda1380_write(codec, UDA1380_PM, 0x0);
  545. break;
  546. case SND_SOC_BIAS_OFF:
  547. if (!gpio_is_valid(pdata->gpio_power))
  548. break;
  549. gpio_set_value(pdata->gpio_power, 0);
  550. /* Mark mixer regs cache dirty to sync them with
  551. * codec regs on power on.
  552. */
  553. for (reg = UDA1380_MVOL; reg < UDA1380_CACHEREGNUM; reg++)
  554. set_bit(reg - 0x10, &uda1380_cache_dirty);
  555. }
  556. codec->dapm.bias_level = level;
  557. return 0;
  558. }
  559. #define UDA1380_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |\
  560. SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 |\
  561. SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000)
  562. static struct snd_soc_dai_ops uda1380_dai_ops = {
  563. .hw_params = uda1380_pcm_hw_params,
  564. .shutdown = uda1380_pcm_shutdown,
  565. .trigger = uda1380_trigger,
  566. .set_fmt = uda1380_set_dai_fmt_both,
  567. };
  568. static struct snd_soc_dai_ops uda1380_dai_ops_playback = {
  569. .hw_params = uda1380_pcm_hw_params,
  570. .shutdown = uda1380_pcm_shutdown,
  571. .trigger = uda1380_trigger,
  572. .set_fmt = uda1380_set_dai_fmt_playback,
  573. };
  574. static struct snd_soc_dai_ops uda1380_dai_ops_capture = {
  575. .hw_params = uda1380_pcm_hw_params,
  576. .shutdown = uda1380_pcm_shutdown,
  577. .trigger = uda1380_trigger,
  578. .set_fmt = uda1380_set_dai_fmt_capture,
  579. };
  580. static struct snd_soc_dai_driver uda1380_dai[] = {
  581. {
  582. .name = "uda1380-hifi",
  583. .playback = {
  584. .stream_name = "Playback",
  585. .channels_min = 1,
  586. .channels_max = 2,
  587. .rates = UDA1380_RATES,
  588. .formats = SNDRV_PCM_FMTBIT_S16_LE,},
  589. .capture = {
  590. .stream_name = "Capture",
  591. .channels_min = 1,
  592. .channels_max = 2,
  593. .rates = UDA1380_RATES,
  594. .formats = SNDRV_PCM_FMTBIT_S16_LE,},
  595. .ops = &uda1380_dai_ops,
  596. },
  597. { /* playback only - dual interface */
  598. .name = "uda1380-hifi-playback",
  599. .playback = {
  600. .stream_name = "Playback",
  601. .channels_min = 1,
  602. .channels_max = 2,
  603. .rates = UDA1380_RATES,
  604. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  605. },
  606. .ops = &uda1380_dai_ops_playback,
  607. },
  608. { /* capture only - dual interface*/
  609. .name = "uda1380-hifi-capture",
  610. .capture = {
  611. .stream_name = "Capture",
  612. .channels_min = 1,
  613. .channels_max = 2,
  614. .rates = UDA1380_RATES,
  615. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  616. },
  617. .ops = &uda1380_dai_ops_capture,
  618. },
  619. };
  620. static int uda1380_suspend(struct snd_soc_codec *codec, pm_message_t state)
  621. {
  622. uda1380_set_bias_level(codec, SND_SOC_BIAS_OFF);
  623. return 0;
  624. }
  625. static int uda1380_resume(struct snd_soc_codec *codec)
  626. {
  627. uda1380_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  628. return 0;
  629. }
  630. static int uda1380_probe(struct snd_soc_codec *codec)
  631. {
  632. struct uda1380_platform_data *pdata =codec->dev->platform_data;
  633. struct uda1380_priv *uda1380 = snd_soc_codec_get_drvdata(codec);
  634. int ret;
  635. uda1380->codec = codec;
  636. codec->hw_write = (hw_write_t)i2c_master_send;
  637. codec->control_data = uda1380->control_data;
  638. if (!pdata)
  639. return -EINVAL;
  640. if (gpio_is_valid(pdata->gpio_reset)) {
  641. ret = gpio_request(pdata->gpio_reset, "uda1380 reset");
  642. if (ret)
  643. goto err_out;
  644. ret = gpio_direction_output(pdata->gpio_reset, 0);
  645. if (ret)
  646. goto err_gpio_reset_conf;
  647. }
  648. if (gpio_is_valid(pdata->gpio_power)) {
  649. ret = gpio_request(pdata->gpio_power, "uda1380 power");
  650. if (ret)
  651. goto err_gpio;
  652. ret = gpio_direction_output(pdata->gpio_power, 0);
  653. if (ret)
  654. goto err_gpio_power_conf;
  655. } else {
  656. ret = uda1380_reset(codec);
  657. if (ret) {
  658. dev_err(codec->dev, "Failed to issue reset\n");
  659. goto err_reset;
  660. }
  661. }
  662. INIT_WORK(&uda1380->work, uda1380_flush_work);
  663. /* power on device */
  664. uda1380_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  665. /* set clock input */
  666. switch (pdata->dac_clk) {
  667. case UDA1380_DAC_CLK_SYSCLK:
  668. uda1380_write_reg_cache(codec, UDA1380_CLK, 0);
  669. break;
  670. case UDA1380_DAC_CLK_WSPLL:
  671. uda1380_write_reg_cache(codec, UDA1380_CLK,
  672. R00_DAC_CLK);
  673. break;
  674. }
  675. snd_soc_add_controls(codec, uda1380_snd_controls,
  676. ARRAY_SIZE(uda1380_snd_controls));
  677. uda1380_add_widgets(codec);
  678. return 0;
  679. err_reset:
  680. err_gpio_power_conf:
  681. if (gpio_is_valid(pdata->gpio_power))
  682. gpio_free(pdata->gpio_power);
  683. err_gpio_reset_conf:
  684. err_gpio:
  685. if (gpio_is_valid(pdata->gpio_reset))
  686. gpio_free(pdata->gpio_reset);
  687. err_out:
  688. return ret;
  689. }
  690. /* power down chip */
  691. static int uda1380_remove(struct snd_soc_codec *codec)
  692. {
  693. struct uda1380_platform_data *pdata =codec->dev->platform_data;
  694. uda1380_set_bias_level(codec, SND_SOC_BIAS_OFF);
  695. gpio_free(pdata->gpio_reset);
  696. gpio_free(pdata->gpio_power);
  697. return 0;
  698. }
  699. static struct snd_soc_codec_driver soc_codec_dev_uda1380 = {
  700. .probe = uda1380_probe,
  701. .remove = uda1380_remove,
  702. .suspend = uda1380_suspend,
  703. .resume = uda1380_resume,
  704. .read = uda1380_read_reg_cache,
  705. .write = uda1380_write,
  706. .set_bias_level = uda1380_set_bias_level,
  707. .reg_cache_size = ARRAY_SIZE(uda1380_reg),
  708. .reg_word_size = sizeof(u16),
  709. .reg_cache_default = uda1380_reg,
  710. .reg_cache_step = 1,
  711. };
  712. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  713. static __devinit int uda1380_i2c_probe(struct i2c_client *i2c,
  714. const struct i2c_device_id *id)
  715. {
  716. struct uda1380_priv *uda1380;
  717. int ret;
  718. uda1380 = kzalloc(sizeof(struct uda1380_priv), GFP_KERNEL);
  719. if (uda1380 == NULL)
  720. return -ENOMEM;
  721. i2c_set_clientdata(i2c, uda1380);
  722. uda1380->control_data = i2c;
  723. ret = snd_soc_register_codec(&i2c->dev,
  724. &soc_codec_dev_uda1380, uda1380_dai, ARRAY_SIZE(uda1380_dai));
  725. if (ret < 0)
  726. kfree(uda1380);
  727. return ret;
  728. }
  729. static int __devexit uda1380_i2c_remove(struct i2c_client *i2c)
  730. {
  731. snd_soc_unregister_codec(&i2c->dev);
  732. kfree(i2c_get_clientdata(i2c));
  733. return 0;
  734. }
  735. static const struct i2c_device_id uda1380_i2c_id[] = {
  736. { "uda1380", 0 },
  737. { }
  738. };
  739. MODULE_DEVICE_TABLE(i2c, uda1380_i2c_id);
  740. static struct i2c_driver uda1380_i2c_driver = {
  741. .driver = {
  742. .name = "uda1380-codec",
  743. .owner = THIS_MODULE,
  744. },
  745. .probe = uda1380_i2c_probe,
  746. .remove = __devexit_p(uda1380_i2c_remove),
  747. .id_table = uda1380_i2c_id,
  748. };
  749. #endif
  750. static int __init uda1380_modinit(void)
  751. {
  752. int ret;
  753. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  754. ret = i2c_add_driver(&uda1380_i2c_driver);
  755. if (ret != 0)
  756. pr_err("Failed to register UDA1380 I2C driver: %d\n", ret);
  757. #endif
  758. return 0;
  759. }
  760. module_init(uda1380_modinit);
  761. static void __exit uda1380_exit(void)
  762. {
  763. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  764. i2c_del_driver(&uda1380_i2c_driver);
  765. #endif
  766. }
  767. module_exit(uda1380_exit);
  768. MODULE_AUTHOR("Giorgio Padrin");
  769. MODULE_DESCRIPTION("Audio support for codec Philips UDA1380");
  770. MODULE_LICENSE("GPL");