uda1380.c 24 KB

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