uda1380.c 23 KB

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