uda1380.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887
  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. snd_soc_dapm_new_controls(codec, uda1380_dapm_widgets,
  370. ARRAY_SIZE(uda1380_dapm_widgets));
  371. snd_soc_dapm_add_routes(codec, audio_map, ARRAY_SIZE(audio_map));
  372. return 0;
  373. }
  374. static int uda1380_set_dai_fmt_both(struct snd_soc_dai *codec_dai,
  375. unsigned int fmt)
  376. {
  377. struct snd_soc_codec *codec = codec_dai->codec;
  378. int iface;
  379. /* set up DAI based upon fmt */
  380. iface = uda1380_read_reg_cache(codec, UDA1380_IFACE);
  381. iface &= ~(R01_SFORI_MASK | R01_SIM | R01_SFORO_MASK);
  382. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  383. case SND_SOC_DAIFMT_I2S:
  384. iface |= R01_SFORI_I2S | R01_SFORO_I2S;
  385. break;
  386. case SND_SOC_DAIFMT_LSB:
  387. iface |= R01_SFORI_LSB16 | R01_SFORO_LSB16;
  388. break;
  389. case SND_SOC_DAIFMT_MSB:
  390. iface |= R01_SFORI_MSB | R01_SFORO_MSB;
  391. }
  392. /* DATAI is slave only, so in single-link mode, this has to be slave */
  393. if ((fmt & SND_SOC_DAIFMT_MASTER_MASK) != SND_SOC_DAIFMT_CBS_CFS)
  394. return -EINVAL;
  395. uda1380_write(codec, UDA1380_IFACE, iface);
  396. return 0;
  397. }
  398. static int uda1380_set_dai_fmt_playback(struct snd_soc_dai *codec_dai,
  399. unsigned int fmt)
  400. {
  401. struct snd_soc_codec *codec = codec_dai->codec;
  402. int iface;
  403. /* set up DAI based upon fmt */
  404. iface = uda1380_read_reg_cache(codec, UDA1380_IFACE);
  405. iface &= ~R01_SFORI_MASK;
  406. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  407. case SND_SOC_DAIFMT_I2S:
  408. iface |= R01_SFORI_I2S;
  409. break;
  410. case SND_SOC_DAIFMT_LSB:
  411. iface |= R01_SFORI_LSB16;
  412. break;
  413. case SND_SOC_DAIFMT_MSB:
  414. iface |= R01_SFORI_MSB;
  415. }
  416. /* DATAI is slave only, so this has to be slave */
  417. if ((fmt & SND_SOC_DAIFMT_MASTER_MASK) != SND_SOC_DAIFMT_CBS_CFS)
  418. return -EINVAL;
  419. uda1380_write(codec, UDA1380_IFACE, iface);
  420. return 0;
  421. }
  422. static int uda1380_set_dai_fmt_capture(struct snd_soc_dai *codec_dai,
  423. unsigned int fmt)
  424. {
  425. struct snd_soc_codec *codec = codec_dai->codec;
  426. int iface;
  427. /* set up DAI based upon fmt */
  428. iface = uda1380_read_reg_cache(codec, UDA1380_IFACE);
  429. iface &= ~(R01_SIM | R01_SFORO_MASK);
  430. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  431. case SND_SOC_DAIFMT_I2S:
  432. iface |= R01_SFORO_I2S;
  433. break;
  434. case SND_SOC_DAIFMT_LSB:
  435. iface |= R01_SFORO_LSB16;
  436. break;
  437. case SND_SOC_DAIFMT_MSB:
  438. iface |= R01_SFORO_MSB;
  439. }
  440. if ((fmt & SND_SOC_DAIFMT_MASTER_MASK) == SND_SOC_DAIFMT_CBM_CFM)
  441. iface |= R01_SIM;
  442. uda1380_write(codec, UDA1380_IFACE, iface);
  443. return 0;
  444. }
  445. static int uda1380_trigger(struct snd_pcm_substream *substream, int cmd,
  446. struct snd_soc_dai *dai)
  447. {
  448. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  449. struct snd_soc_codec *codec = rtd->codec;
  450. struct uda1380_priv *uda1380 = snd_soc_codec_get_drvdata(codec);
  451. int mixer = uda1380_read_reg_cache(codec, UDA1380_MIXER);
  452. switch (cmd) {
  453. case SNDRV_PCM_TRIGGER_START:
  454. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  455. uda1380_write_reg_cache(codec, UDA1380_MIXER,
  456. mixer & ~R14_SILENCE);
  457. schedule_work(&uda1380->work);
  458. break;
  459. case SNDRV_PCM_TRIGGER_STOP:
  460. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  461. uda1380_write_reg_cache(codec, UDA1380_MIXER,
  462. mixer | R14_SILENCE);
  463. schedule_work(&uda1380->work);
  464. break;
  465. }
  466. return 0;
  467. }
  468. static int uda1380_pcm_hw_params(struct snd_pcm_substream *substream,
  469. struct snd_pcm_hw_params *params,
  470. struct snd_soc_dai *dai)
  471. {
  472. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  473. struct snd_soc_codec *codec = rtd->codec;
  474. u16 clk = uda1380_read_reg_cache(codec, UDA1380_CLK);
  475. /* set WSPLL power and divider if running from this clock */
  476. if (clk & R00_DAC_CLK) {
  477. int rate = params_rate(params);
  478. u16 pm = uda1380_read_reg_cache(codec, UDA1380_PM);
  479. clk &= ~0x3; /* clear SEL_LOOP_DIV */
  480. switch (rate) {
  481. case 6250 ... 12500:
  482. clk |= 0x0;
  483. break;
  484. case 12501 ... 25000:
  485. clk |= 0x1;
  486. break;
  487. case 25001 ... 50000:
  488. clk |= 0x2;
  489. break;
  490. case 50001 ... 100000:
  491. clk |= 0x3;
  492. break;
  493. }
  494. uda1380_write(codec, UDA1380_PM, R02_PON_PLL | pm);
  495. }
  496. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  497. clk |= R00_EN_DAC | R00_EN_INT;
  498. else
  499. clk |= R00_EN_ADC | R00_EN_DEC;
  500. uda1380_write(codec, UDA1380_CLK, clk);
  501. return 0;
  502. }
  503. static void uda1380_pcm_shutdown(struct snd_pcm_substream *substream,
  504. struct snd_soc_dai *dai)
  505. {
  506. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  507. struct snd_soc_codec *codec = rtd->codec;
  508. u16 clk = uda1380_read_reg_cache(codec, UDA1380_CLK);
  509. /* shut down WSPLL power if running from this clock */
  510. if (clk & R00_DAC_CLK) {
  511. u16 pm = uda1380_read_reg_cache(codec, UDA1380_PM);
  512. uda1380_write(codec, UDA1380_PM, ~R02_PON_PLL & pm);
  513. }
  514. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  515. clk &= ~(R00_EN_DAC | R00_EN_INT);
  516. else
  517. clk &= ~(R00_EN_ADC | R00_EN_DEC);
  518. uda1380_write(codec, UDA1380_CLK, clk);
  519. }
  520. static int uda1380_set_bias_level(struct snd_soc_codec *codec,
  521. enum snd_soc_bias_level level)
  522. {
  523. int pm = uda1380_read_reg_cache(codec, UDA1380_PM);
  524. int reg;
  525. struct uda1380_platform_data *pdata = codec->dev->platform_data;
  526. if (codec->bias_level == level)
  527. return 0;
  528. switch (level) {
  529. case SND_SOC_BIAS_ON:
  530. case SND_SOC_BIAS_PREPARE:
  531. /* ADC, DAC on */
  532. uda1380_write(codec, UDA1380_PM, R02_PON_BIAS | pm);
  533. break;
  534. case SND_SOC_BIAS_STANDBY:
  535. if (codec->bias_level == SND_SOC_BIAS_OFF) {
  536. if (gpio_is_valid(pdata->gpio_power)) {
  537. gpio_set_value(pdata->gpio_power, 1);
  538. mdelay(1);
  539. uda1380_reset(codec);
  540. }
  541. uda1380_sync_cache(codec);
  542. }
  543. uda1380_write(codec, UDA1380_PM, 0x0);
  544. break;
  545. case SND_SOC_BIAS_OFF:
  546. if (!gpio_is_valid(pdata->gpio_power))
  547. break;
  548. gpio_set_value(pdata->gpio_power, 0);
  549. /* Mark mixer regs cache dirty to sync them with
  550. * codec regs on power on.
  551. */
  552. for (reg = UDA1380_MVOL; reg < UDA1380_CACHEREGNUM; reg++)
  553. set_bit(reg - 0x10, &uda1380_cache_dirty);
  554. }
  555. codec->bias_level = level;
  556. return 0;
  557. }
  558. #define UDA1380_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |\
  559. SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 |\
  560. SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000)
  561. static struct snd_soc_dai_ops uda1380_dai_ops = {
  562. .hw_params = uda1380_pcm_hw_params,
  563. .shutdown = uda1380_pcm_shutdown,
  564. .trigger = uda1380_trigger,
  565. .set_fmt = uda1380_set_dai_fmt_both,
  566. };
  567. static struct snd_soc_dai_ops uda1380_dai_ops_playback = {
  568. .hw_params = uda1380_pcm_hw_params,
  569. .shutdown = uda1380_pcm_shutdown,
  570. .trigger = uda1380_trigger,
  571. .set_fmt = uda1380_set_dai_fmt_playback,
  572. };
  573. static struct snd_soc_dai_ops uda1380_dai_ops_capture = {
  574. .hw_params = uda1380_pcm_hw_params,
  575. .shutdown = uda1380_pcm_shutdown,
  576. .trigger = uda1380_trigger,
  577. .set_fmt = uda1380_set_dai_fmt_capture,
  578. };
  579. static struct snd_soc_dai_driver uda1380_dai[] = {
  580. {
  581. .name = "uda1380-hifi",
  582. .playback = {
  583. .stream_name = "Playback",
  584. .channels_min = 1,
  585. .channels_max = 2,
  586. .rates = UDA1380_RATES,
  587. .formats = SNDRV_PCM_FMTBIT_S16_LE,},
  588. .capture = {
  589. .stream_name = "Capture",
  590. .channels_min = 1,
  591. .channels_max = 2,
  592. .rates = UDA1380_RATES,
  593. .formats = SNDRV_PCM_FMTBIT_S16_LE,},
  594. .ops = &uda1380_dai_ops,
  595. },
  596. { /* playback only - dual interface */
  597. .name = "uda1380-hifi-playback",
  598. .playback = {
  599. .stream_name = "Playback",
  600. .channels_min = 1,
  601. .channels_max = 2,
  602. .rates = UDA1380_RATES,
  603. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  604. },
  605. .ops = &uda1380_dai_ops_playback,
  606. },
  607. { /* capture only - dual interface*/
  608. .name = "uda1380-hifi-capture",
  609. .capture = {
  610. .stream_name = "Capture",
  611. .channels_min = 1,
  612. .channels_max = 2,
  613. .rates = UDA1380_RATES,
  614. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  615. },
  616. .ops = &uda1380_dai_ops_capture,
  617. },
  618. };
  619. static int uda1380_suspend(struct snd_soc_codec *codec, pm_message_t state)
  620. {
  621. uda1380_set_bias_level(codec, SND_SOC_BIAS_OFF);
  622. return 0;
  623. }
  624. static int uda1380_resume(struct snd_soc_codec *codec)
  625. {
  626. uda1380_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  627. return 0;
  628. }
  629. static int uda1380_probe(struct snd_soc_codec *codec)
  630. {
  631. struct uda1380_platform_data *pdata =codec->dev->platform_data;
  632. struct uda1380_priv *uda1380 = snd_soc_codec_get_drvdata(codec);
  633. int ret;
  634. uda1380->codec = codec;
  635. codec->hw_write = (hw_write_t)i2c_master_send;
  636. codec->control_data = uda1380->control_data;
  637. if (!pdata)
  638. return -EINVAL;
  639. if (gpio_is_valid(pdata->gpio_reset)) {
  640. ret = gpio_request(pdata->gpio_reset, "uda1380 reset");
  641. if (ret)
  642. goto err_out;
  643. ret = gpio_direction_output(pdata->gpio_reset, 0);
  644. if (ret)
  645. goto err_gpio_reset_conf;
  646. }
  647. if (gpio_is_valid(pdata->gpio_power)) {
  648. ret = gpio_request(pdata->gpio_power, "uda1380 power");
  649. if (ret)
  650. goto err_gpio;
  651. ret = gpio_direction_output(pdata->gpio_power, 0);
  652. if (ret)
  653. goto err_gpio_power_conf;
  654. } else {
  655. ret = uda1380_reset(codec);
  656. if (ret) {
  657. dev_err(codec->dev, "Failed to issue reset\n");
  658. goto err_reset;
  659. }
  660. }
  661. INIT_WORK(&uda1380->work, uda1380_flush_work);
  662. /* power on device */
  663. uda1380_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  664. /* set clock input */
  665. switch (pdata->dac_clk) {
  666. case UDA1380_DAC_CLK_SYSCLK:
  667. uda1380_write_reg_cache(codec, UDA1380_CLK, 0);
  668. break;
  669. case UDA1380_DAC_CLK_WSPLL:
  670. uda1380_write_reg_cache(codec, UDA1380_CLK,
  671. R00_DAC_CLK);
  672. break;
  673. }
  674. snd_soc_add_controls(codec, uda1380_snd_controls,
  675. ARRAY_SIZE(uda1380_snd_controls));
  676. uda1380_add_widgets(codec);
  677. return 0;
  678. err_reset:
  679. err_gpio_power_conf:
  680. if (gpio_is_valid(pdata->gpio_power))
  681. gpio_free(pdata->gpio_power);
  682. err_gpio_reset_conf:
  683. err_gpio:
  684. if (gpio_is_valid(pdata->gpio_reset))
  685. gpio_free(pdata->gpio_reset);
  686. err_out:
  687. return ret;
  688. }
  689. /* power down chip */
  690. static int uda1380_remove(struct snd_soc_codec *codec)
  691. {
  692. struct uda1380_platform_data *pdata =codec->dev->platform_data;
  693. uda1380_set_bias_level(codec, SND_SOC_BIAS_OFF);
  694. gpio_free(pdata->gpio_reset);
  695. gpio_free(pdata->gpio_power);
  696. return 0;
  697. }
  698. static struct snd_soc_codec_driver soc_codec_dev_uda1380 = {
  699. .probe = uda1380_probe,
  700. .remove = uda1380_remove,
  701. .suspend = uda1380_suspend,
  702. .resume = uda1380_resume,
  703. .read = uda1380_read_reg_cache,
  704. .write = uda1380_write,
  705. .set_bias_level = uda1380_set_bias_level,
  706. .reg_cache_size = ARRAY_SIZE(uda1380_reg),
  707. .reg_word_size = sizeof(u16),
  708. .reg_cache_default = uda1380_reg,
  709. .reg_cache_step = 1,
  710. };
  711. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  712. static __devinit int uda1380_i2c_probe(struct i2c_client *i2c,
  713. const struct i2c_device_id *id)
  714. {
  715. struct uda1380_priv *uda1380;
  716. int ret;
  717. uda1380 = kzalloc(sizeof(struct uda1380_priv), GFP_KERNEL);
  718. if (uda1380 == NULL)
  719. return -ENOMEM;
  720. i2c_set_clientdata(i2c, uda1380);
  721. uda1380->control_data = i2c;
  722. ret = snd_soc_register_codec(&i2c->dev,
  723. &soc_codec_dev_uda1380, uda1380_dai, ARRAY_SIZE(uda1380_dai));
  724. if (ret < 0)
  725. kfree(uda1380);
  726. return ret;
  727. }
  728. static int __devexit uda1380_i2c_remove(struct i2c_client *i2c)
  729. {
  730. snd_soc_unregister_codec(&i2c->dev);
  731. kfree(i2c_get_clientdata(i2c));
  732. return 0;
  733. }
  734. static const struct i2c_device_id uda1380_i2c_id[] = {
  735. { "uda1380", 0 },
  736. { }
  737. };
  738. MODULE_DEVICE_TABLE(i2c, uda1380_i2c_id);
  739. static struct i2c_driver uda1380_i2c_driver = {
  740. .driver = {
  741. .name = "uda1380-codec",
  742. .owner = THIS_MODULE,
  743. },
  744. .probe = uda1380_i2c_probe,
  745. .remove = __devexit_p(uda1380_i2c_remove),
  746. .id_table = uda1380_i2c_id,
  747. };
  748. #endif
  749. static int __init uda1380_modinit(void)
  750. {
  751. int ret;
  752. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  753. ret = i2c_add_driver(&uda1380_i2c_driver);
  754. if (ret != 0)
  755. pr_err("Failed to register UDA1380 I2C driver: %d\n", ret);
  756. #endif
  757. return 0;
  758. }
  759. module_init(uda1380_modinit);
  760. static void __exit uda1380_exit(void)
  761. {
  762. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  763. i2c_del_driver(&uda1380_i2c_driver);
  764. #endif
  765. }
  766. module_exit(uda1380_exit);
  767. MODULE_AUTHOR("Giorgio Padrin");
  768. MODULE_DESCRIPTION("Audio support for codec Philips UDA1380");
  769. MODULE_LICENSE("GPL");