uda1380.c 23 KB

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