uda1380.c 25 KB

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