adav80x.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953
  1. /*
  2. * ADAV80X Audio Codec driver supporting ADAV801, ADAV803
  3. *
  4. * Copyright 2011 Analog Devices Inc.
  5. * Author: Yi Li <yi.li@analog.com>
  6. * Author: Lars-Peter Clausen <lars@metafoo.de>
  7. *
  8. * Licensed under the GPL-2 or later.
  9. */
  10. #include <linux/init.h>
  11. #include <linux/module.h>
  12. #include <linux/kernel.h>
  13. #include <linux/i2c.h>
  14. #include <linux/spi/spi.h>
  15. #include <linux/slab.h>
  16. #include <sound/core.h>
  17. #include <sound/pcm.h>
  18. #include <sound/pcm_params.h>
  19. #include <sound/tlv.h>
  20. #include <sound/soc.h>
  21. #include "adav80x.h"
  22. #define ADAV80X_PLAYBACK_CTRL 0x04
  23. #define ADAV80X_AUX_IN_CTRL 0x05
  24. #define ADAV80X_REC_CTRL 0x06
  25. #define ADAV80X_AUX_OUT_CTRL 0x07
  26. #define ADAV80X_DPATH_CTRL1 0x62
  27. #define ADAV80X_DPATH_CTRL2 0x63
  28. #define ADAV80X_DAC_CTRL1 0x64
  29. #define ADAV80X_DAC_CTRL2 0x65
  30. #define ADAV80X_DAC_CTRL3 0x66
  31. #define ADAV80X_DAC_L_VOL 0x68
  32. #define ADAV80X_DAC_R_VOL 0x69
  33. #define ADAV80X_PGA_L_VOL 0x6c
  34. #define ADAV80X_PGA_R_VOL 0x6d
  35. #define ADAV80X_ADC_CTRL1 0x6e
  36. #define ADAV80X_ADC_CTRL2 0x6f
  37. #define ADAV80X_ADC_L_VOL 0x70
  38. #define ADAV80X_ADC_R_VOL 0x71
  39. #define ADAV80X_PLL_CTRL1 0x74
  40. #define ADAV80X_PLL_CTRL2 0x75
  41. #define ADAV80X_ICLK_CTRL1 0x76
  42. #define ADAV80X_ICLK_CTRL2 0x77
  43. #define ADAV80X_PLL_CLK_SRC 0x78
  44. #define ADAV80X_PLL_OUTE 0x7a
  45. #define ADAV80X_PLL_CLK_SRC_PLL_XIN(pll) 0x00
  46. #define ADAV80X_PLL_CLK_SRC_PLL_MCLKI(pll) (0x40 << (pll))
  47. #define ADAV80X_PLL_CLK_SRC_PLL_MASK(pll) (0x40 << (pll))
  48. #define ADAV80X_ICLK_CTRL1_DAC_SRC(src) ((src) << 5)
  49. #define ADAV80X_ICLK_CTRL1_ADC_SRC(src) ((src) << 2)
  50. #define ADAV80X_ICLK_CTRL1_ICLK2_SRC(src) (src)
  51. #define ADAV80X_ICLK_CTRL2_ICLK1_SRC(src) ((src) << 3)
  52. #define ADAV80X_PLL_CTRL1_PLLDIV 0x10
  53. #define ADAV80X_PLL_CTRL1_PLLPD(pll) (0x04 << (pll))
  54. #define ADAV80X_PLL_CTRL1_XTLPD 0x02
  55. #define ADAV80X_PLL_CTRL2_FIELD(pll, x) ((x) << ((pll) * 4))
  56. #define ADAV80X_PLL_CTRL2_FS_48(pll) ADAV80X_PLL_CTRL2_FIELD((pll), 0x00)
  57. #define ADAV80X_PLL_CTRL2_FS_32(pll) ADAV80X_PLL_CTRL2_FIELD((pll), 0x08)
  58. #define ADAV80X_PLL_CTRL2_FS_44(pll) ADAV80X_PLL_CTRL2_FIELD((pll), 0x0c)
  59. #define ADAV80X_PLL_CTRL2_SEL(pll) ADAV80X_PLL_CTRL2_FIELD((pll), 0x02)
  60. #define ADAV80X_PLL_CTRL2_DOUB(pll) ADAV80X_PLL_CTRL2_FIELD((pll), 0x01)
  61. #define ADAV80X_PLL_CTRL2_PLL_MASK(pll) ADAV80X_PLL_CTRL2_FIELD((pll), 0x0f)
  62. #define ADAV80X_ADC_CTRL1_MODULATOR_MASK 0x80
  63. #define ADAV80X_ADC_CTRL1_MODULATOR_128FS 0x00
  64. #define ADAV80X_ADC_CTRL1_MODULATOR_64FS 0x80
  65. #define ADAV80X_DAC_CTRL1_PD 0x80
  66. #define ADAV80X_DAC_CTRL2_DIV1 0x00
  67. #define ADAV80X_DAC_CTRL2_DIV1_5 0x10
  68. #define ADAV80X_DAC_CTRL2_DIV2 0x20
  69. #define ADAV80X_DAC_CTRL2_DIV3 0x30
  70. #define ADAV80X_DAC_CTRL2_DIV_MASK 0x30
  71. #define ADAV80X_DAC_CTRL2_INTERPOL_256FS 0x00
  72. #define ADAV80X_DAC_CTRL2_INTERPOL_128FS 0x40
  73. #define ADAV80X_DAC_CTRL2_INTERPOL_64FS 0x80
  74. #define ADAV80X_DAC_CTRL2_INTERPOL_MASK 0xc0
  75. #define ADAV80X_DAC_CTRL2_DEEMPH_NONE 0x00
  76. #define ADAV80X_DAC_CTRL2_DEEMPH_44 0x01
  77. #define ADAV80X_DAC_CTRL2_DEEMPH_32 0x02
  78. #define ADAV80X_DAC_CTRL2_DEEMPH_48 0x03
  79. #define ADAV80X_DAC_CTRL2_DEEMPH_MASK 0x01
  80. #define ADAV80X_CAPTURE_MODE_MASTER 0x20
  81. #define ADAV80X_CAPTURE_WORD_LEN24 0x00
  82. #define ADAV80X_CAPTURE_WORD_LEN20 0x04
  83. #define ADAV80X_CAPTRUE_WORD_LEN18 0x08
  84. #define ADAV80X_CAPTURE_WORD_LEN16 0x0c
  85. #define ADAV80X_CAPTURE_WORD_LEN_MASK 0x0c
  86. #define ADAV80X_CAPTURE_MODE_LEFT_J 0x00
  87. #define ADAV80X_CAPTURE_MODE_I2S 0x01
  88. #define ADAV80X_CAPTURE_MODE_RIGHT_J 0x03
  89. #define ADAV80X_CAPTURE_MODE_MASK 0x03
  90. #define ADAV80X_PLAYBACK_MODE_MASTER 0x10
  91. #define ADAV80X_PLAYBACK_MODE_LEFT_J 0x00
  92. #define ADAV80X_PLAYBACK_MODE_I2S 0x01
  93. #define ADAV80X_PLAYBACK_MODE_RIGHT_J_24 0x04
  94. #define ADAV80X_PLAYBACK_MODE_RIGHT_J_20 0x05
  95. #define ADAV80X_PLAYBACK_MODE_RIGHT_J_18 0x06
  96. #define ADAV80X_PLAYBACK_MODE_RIGHT_J_16 0x07
  97. #define ADAV80X_PLAYBACK_MODE_MASK 0x07
  98. #define ADAV80X_PLL_OUTE_SYSCLKPD(x) BIT(2 - (x))
  99. static u8 adav80x_default_regs[] = {
  100. 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x02, 0x01, 0x80, 0x26, 0x00, 0x00,
  101. 0x02, 0x40, 0x20, 0x00, 0x09, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  102. 0x04, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0xd1, 0x92, 0xb1, 0x37,
  103. 0x48, 0xd2, 0xfb, 0xca, 0xd2, 0x15, 0xe8, 0x29, 0xb9, 0x6a, 0xda, 0x2b,
  104. 0xb7, 0xc0, 0x11, 0x65, 0x5c, 0xf6, 0xff, 0x8d, 0x00, 0x00, 0x00, 0x00,
  105. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  106. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xa5, 0x00, 0x00,
  107. 0x00, 0xe8, 0x46, 0xe1, 0x5b, 0xd3, 0x43, 0x77, 0x93, 0xa7, 0x44, 0xee,
  108. 0x32, 0x12, 0xc0, 0x11, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0x3f, 0x3f,
  109. 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0x00, 0x1d, 0x00, 0x00, 0x00, 0x00,
  110. 0x00, 0x00, 0x00, 0x00, 0x52, 0x00,
  111. };
  112. struct adav80x {
  113. enum snd_soc_control_type control_type;
  114. enum adav80x_clk_src clk_src;
  115. unsigned int sysclk;
  116. enum adav80x_pll_src pll_src;
  117. unsigned int dai_fmt[2];
  118. unsigned int rate;
  119. bool deemph;
  120. bool sysclk_pd[3];
  121. };
  122. static const char *adav80x_mux_text[] = {
  123. "ADC",
  124. "Playback",
  125. "Aux Playback",
  126. };
  127. static const unsigned int adav80x_mux_values[] = {
  128. 0, 2, 3,
  129. };
  130. #define ADAV80X_MUX_ENUM_DECL(name, reg, shift) \
  131. SOC_VALUE_ENUM_DOUBLE_DECL(name, reg, shift, 7, \
  132. ARRAY_SIZE(adav80x_mux_text), adav80x_mux_text, \
  133. adav80x_mux_values)
  134. static ADAV80X_MUX_ENUM_DECL(adav80x_aux_capture_enum, ADAV80X_DPATH_CTRL1, 0);
  135. static ADAV80X_MUX_ENUM_DECL(adav80x_capture_enum, ADAV80X_DPATH_CTRL1, 3);
  136. static ADAV80X_MUX_ENUM_DECL(adav80x_dac_enum, ADAV80X_DPATH_CTRL2, 3);
  137. static const struct snd_kcontrol_new adav80x_aux_capture_mux_ctrl =
  138. SOC_DAPM_VALUE_ENUM("Route", adav80x_aux_capture_enum);
  139. static const struct snd_kcontrol_new adav80x_capture_mux_ctrl =
  140. SOC_DAPM_VALUE_ENUM("Route", adav80x_capture_enum);
  141. static const struct snd_kcontrol_new adav80x_dac_mux_ctrl =
  142. SOC_DAPM_VALUE_ENUM("Route", adav80x_dac_enum);
  143. #define ADAV80X_MUX(name, ctrl) \
  144. SND_SOC_DAPM_VALUE_MUX(name, SND_SOC_NOPM, 0, 0, ctrl)
  145. static const struct snd_soc_dapm_widget adav80x_dapm_widgets[] = {
  146. SND_SOC_DAPM_DAC("DAC", NULL, ADAV80X_DAC_CTRL1, 7, 1),
  147. SND_SOC_DAPM_ADC("ADC", NULL, ADAV80X_ADC_CTRL1, 5, 1),
  148. SND_SOC_DAPM_PGA("Right PGA", ADAV80X_ADC_CTRL1, 0, 1, NULL, 0),
  149. SND_SOC_DAPM_PGA("Left PGA", ADAV80X_ADC_CTRL1, 1, 1, NULL, 0),
  150. SND_SOC_DAPM_AIF_OUT("AIFOUT", "HiFi Capture", 0, SND_SOC_NOPM, 0, 0),
  151. SND_SOC_DAPM_AIF_IN("AIFIN", "HiFi Playback", 0, SND_SOC_NOPM, 0, 0),
  152. SND_SOC_DAPM_AIF_OUT("AIFAUXOUT", "Aux Capture", 0, SND_SOC_NOPM, 0, 0),
  153. SND_SOC_DAPM_AIF_IN("AIFAUXIN", "Aux Playback", 0, SND_SOC_NOPM, 0, 0),
  154. ADAV80X_MUX("Aux Capture Select", &adav80x_aux_capture_mux_ctrl),
  155. ADAV80X_MUX("Capture Select", &adav80x_capture_mux_ctrl),
  156. ADAV80X_MUX("DAC Select", &adav80x_dac_mux_ctrl),
  157. SND_SOC_DAPM_INPUT("VINR"),
  158. SND_SOC_DAPM_INPUT("VINL"),
  159. SND_SOC_DAPM_OUTPUT("VOUTR"),
  160. SND_SOC_DAPM_OUTPUT("VOUTL"),
  161. SND_SOC_DAPM_SUPPLY("SYSCLK", SND_SOC_NOPM, 0, 0, NULL, 0),
  162. SND_SOC_DAPM_SUPPLY("PLL1", ADAV80X_PLL_CTRL1, 2, 1, NULL, 0),
  163. SND_SOC_DAPM_SUPPLY("PLL2", ADAV80X_PLL_CTRL1, 3, 1, NULL, 0),
  164. SND_SOC_DAPM_SUPPLY("OSC", ADAV80X_PLL_CTRL1, 1, 1, NULL, 0),
  165. };
  166. static int adav80x_dapm_sysclk_check(struct snd_soc_dapm_widget *source,
  167. struct snd_soc_dapm_widget *sink)
  168. {
  169. struct snd_soc_codec *codec = source->codec;
  170. struct adav80x *adav80x = snd_soc_codec_get_drvdata(codec);
  171. const char *clk;
  172. switch (adav80x->clk_src) {
  173. case ADAV80X_CLK_PLL1:
  174. clk = "PLL1";
  175. break;
  176. case ADAV80X_CLK_PLL2:
  177. clk = "PLL2";
  178. break;
  179. case ADAV80X_CLK_XTAL:
  180. clk = "OSC";
  181. break;
  182. default:
  183. return 0;
  184. }
  185. return strcmp(source->name, clk) == 0;
  186. }
  187. static int adav80x_dapm_pll_check(struct snd_soc_dapm_widget *source,
  188. struct snd_soc_dapm_widget *sink)
  189. {
  190. struct snd_soc_codec *codec = source->codec;
  191. struct adav80x *adav80x = snd_soc_codec_get_drvdata(codec);
  192. return adav80x->pll_src == ADAV80X_PLL_SRC_XTAL;
  193. }
  194. static const struct snd_soc_dapm_route adav80x_dapm_routes[] = {
  195. { "DAC Select", "ADC", "ADC" },
  196. { "DAC Select", "Playback", "AIFIN" },
  197. { "DAC Select", "Aux Playback", "AIFAUXIN" },
  198. { "DAC", NULL, "DAC Select" },
  199. { "Capture Select", "ADC", "ADC" },
  200. { "Capture Select", "Playback", "AIFIN" },
  201. { "Capture Select", "Aux Playback", "AIFAUXIN" },
  202. { "AIFOUT", NULL, "Capture Select" },
  203. { "Aux Capture Select", "ADC", "ADC" },
  204. { "Aux Capture Select", "Playback", "AIFIN" },
  205. { "Aux Capture Select", "Aux Playback", "AIFAUXIN" },
  206. { "AIFAUXOUT", NULL, "Aux Capture Select" },
  207. { "VOUTR", NULL, "DAC" },
  208. { "VOUTL", NULL, "DAC" },
  209. { "Left PGA", NULL, "VINL" },
  210. { "Right PGA", NULL, "VINR" },
  211. { "ADC", NULL, "Left PGA" },
  212. { "ADC", NULL, "Right PGA" },
  213. { "SYSCLK", NULL, "PLL1", adav80x_dapm_sysclk_check },
  214. { "SYSCLK", NULL, "PLL2", adav80x_dapm_sysclk_check },
  215. { "SYSCLK", NULL, "OSC", adav80x_dapm_sysclk_check },
  216. { "PLL1", NULL, "OSC", adav80x_dapm_pll_check },
  217. { "PLL2", NULL, "OSC", adav80x_dapm_pll_check },
  218. { "ADC", NULL, "SYSCLK" },
  219. { "DAC", NULL, "SYSCLK" },
  220. { "AIFOUT", NULL, "SYSCLK" },
  221. { "AIFAUXOUT", NULL, "SYSCLK" },
  222. { "AIFIN", NULL, "SYSCLK" },
  223. { "AIFAUXIN", NULL, "SYSCLK" },
  224. };
  225. static int adav80x_set_deemph(struct snd_soc_codec *codec)
  226. {
  227. struct adav80x *adav80x = snd_soc_codec_get_drvdata(codec);
  228. unsigned int val;
  229. if (adav80x->deemph) {
  230. switch (adav80x->rate) {
  231. case 32000:
  232. val = ADAV80X_DAC_CTRL2_DEEMPH_32;
  233. break;
  234. case 44100:
  235. val = ADAV80X_DAC_CTRL2_DEEMPH_44;
  236. break;
  237. case 48000:
  238. case 64000:
  239. case 88200:
  240. case 96000:
  241. val = ADAV80X_DAC_CTRL2_DEEMPH_48;
  242. break;
  243. default:
  244. val = ADAV80X_DAC_CTRL2_DEEMPH_NONE;
  245. break;
  246. }
  247. } else {
  248. val = ADAV80X_DAC_CTRL2_DEEMPH_NONE;
  249. }
  250. return snd_soc_update_bits(codec, ADAV80X_DAC_CTRL2,
  251. ADAV80X_DAC_CTRL2_DEEMPH_MASK, val);
  252. }
  253. static int adav80x_put_deemph(struct snd_kcontrol *kcontrol,
  254. struct snd_ctl_elem_value *ucontrol)
  255. {
  256. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  257. struct adav80x *adav80x = snd_soc_codec_get_drvdata(codec);
  258. unsigned int deemph = ucontrol->value.integer.value[0];
  259. if (deemph > 1)
  260. return -EINVAL;
  261. adav80x->deemph = deemph;
  262. return adav80x_set_deemph(codec);
  263. }
  264. static int adav80x_get_deemph(struct snd_kcontrol *kcontrol,
  265. struct snd_ctl_elem_value *ucontrol)
  266. {
  267. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  268. struct adav80x *adav80x = snd_soc_codec_get_drvdata(codec);
  269. ucontrol->value.integer.value[0] = adav80x->deemph;
  270. return 0;
  271. };
  272. static const DECLARE_TLV_DB_SCALE(adav80x_inpga_tlv, 0, 50, 0);
  273. static const DECLARE_TLV_DB_MINMAX(adav80x_digital_tlv, -9563, 0);
  274. static const struct snd_kcontrol_new adav80x_controls[] = {
  275. SOC_DOUBLE_R_TLV("Master Playback Volume", ADAV80X_DAC_L_VOL,
  276. ADAV80X_DAC_R_VOL, 0, 0xff, 0, adav80x_digital_tlv),
  277. SOC_DOUBLE_R_TLV("Master Capture Volume", ADAV80X_ADC_L_VOL,
  278. ADAV80X_ADC_R_VOL, 0, 0xff, 0, adav80x_digital_tlv),
  279. SOC_DOUBLE_R_TLV("PGA Capture Volume", ADAV80X_PGA_L_VOL,
  280. ADAV80X_PGA_R_VOL, 0, 0x30, 0, adav80x_inpga_tlv),
  281. SOC_DOUBLE("Master Playback Switch", ADAV80X_DAC_CTRL1, 0, 1, 1, 0),
  282. SOC_DOUBLE("Master Capture Switch", ADAV80X_ADC_CTRL1, 2, 3, 1, 1),
  283. SOC_SINGLE("ADC High Pass Filter Switch", ADAV80X_ADC_CTRL1, 6, 1, 0),
  284. SOC_SINGLE_BOOL_EXT("Playback De-emphasis Switch", 0,
  285. adav80x_get_deemph, adav80x_put_deemph),
  286. };
  287. static unsigned int adav80x_port_ctrl_regs[2][2] = {
  288. { ADAV80X_REC_CTRL, ADAV80X_PLAYBACK_CTRL, },
  289. { ADAV80X_AUX_OUT_CTRL, ADAV80X_AUX_IN_CTRL },
  290. };
  291. static int adav80x_set_dai_fmt(struct snd_soc_dai *dai, unsigned int fmt)
  292. {
  293. struct snd_soc_codec *codec = dai->codec;
  294. struct adav80x *adav80x = snd_soc_codec_get_drvdata(codec);
  295. unsigned int capture = 0x00;
  296. unsigned int playback = 0x00;
  297. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  298. case SND_SOC_DAIFMT_CBM_CFM:
  299. capture |= ADAV80X_CAPTURE_MODE_MASTER;
  300. playback |= ADAV80X_PLAYBACK_MODE_MASTER;
  301. case SND_SOC_DAIFMT_CBS_CFS:
  302. break;
  303. default:
  304. return -EINVAL;
  305. }
  306. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  307. case SND_SOC_DAIFMT_I2S:
  308. capture |= ADAV80X_CAPTURE_MODE_I2S;
  309. playback |= ADAV80X_PLAYBACK_MODE_I2S;
  310. break;
  311. case SND_SOC_DAIFMT_LEFT_J:
  312. capture |= ADAV80X_CAPTURE_MODE_LEFT_J;
  313. playback |= ADAV80X_PLAYBACK_MODE_LEFT_J;
  314. break;
  315. case SND_SOC_DAIFMT_RIGHT_J:
  316. capture |= ADAV80X_CAPTURE_MODE_RIGHT_J;
  317. playback |= ADAV80X_PLAYBACK_MODE_RIGHT_J_24;
  318. break;
  319. default:
  320. return -EINVAL;
  321. }
  322. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  323. case SND_SOC_DAIFMT_NB_NF:
  324. break;
  325. default:
  326. return -EINVAL;
  327. }
  328. snd_soc_update_bits(codec, adav80x_port_ctrl_regs[dai->id][0],
  329. ADAV80X_CAPTURE_MODE_MASK | ADAV80X_CAPTURE_MODE_MASTER,
  330. capture);
  331. snd_soc_write(codec, adav80x_port_ctrl_regs[dai->id][1], playback);
  332. adav80x->dai_fmt[dai->id] = fmt & SND_SOC_DAIFMT_FORMAT_MASK;
  333. return 0;
  334. }
  335. static int adav80x_set_adc_clock(struct snd_soc_codec *codec,
  336. unsigned int sample_rate)
  337. {
  338. unsigned int val;
  339. if (sample_rate <= 48000)
  340. val = ADAV80X_ADC_CTRL1_MODULATOR_128FS;
  341. else
  342. val = ADAV80X_ADC_CTRL1_MODULATOR_64FS;
  343. snd_soc_update_bits(codec, ADAV80X_ADC_CTRL1,
  344. ADAV80X_ADC_CTRL1_MODULATOR_MASK, val);
  345. return 0;
  346. }
  347. static int adav80x_set_dac_clock(struct snd_soc_codec *codec,
  348. unsigned int sample_rate)
  349. {
  350. unsigned int val;
  351. if (sample_rate <= 48000)
  352. val = ADAV80X_DAC_CTRL2_DIV1 | ADAV80X_DAC_CTRL2_INTERPOL_256FS;
  353. else
  354. val = ADAV80X_DAC_CTRL2_DIV2 | ADAV80X_DAC_CTRL2_INTERPOL_128FS;
  355. snd_soc_update_bits(codec, ADAV80X_DAC_CTRL2,
  356. ADAV80X_DAC_CTRL2_DIV_MASK | ADAV80X_DAC_CTRL2_INTERPOL_MASK,
  357. val);
  358. return 0;
  359. }
  360. static int adav80x_set_capture_pcm_format(struct snd_soc_codec *codec,
  361. struct snd_soc_dai *dai, snd_pcm_format_t format)
  362. {
  363. unsigned int val;
  364. switch (format) {
  365. case SNDRV_PCM_FORMAT_S16_LE:
  366. val = ADAV80X_CAPTURE_WORD_LEN16;
  367. break;
  368. case SNDRV_PCM_FORMAT_S18_3LE:
  369. val = ADAV80X_CAPTRUE_WORD_LEN18;
  370. break;
  371. case SNDRV_PCM_FORMAT_S20_3LE:
  372. val = ADAV80X_CAPTURE_WORD_LEN20;
  373. break;
  374. case SNDRV_PCM_FORMAT_S24_LE:
  375. val = ADAV80X_CAPTURE_WORD_LEN24;
  376. break;
  377. default:
  378. return -EINVAL;
  379. }
  380. snd_soc_update_bits(codec, adav80x_port_ctrl_regs[dai->id][0],
  381. ADAV80X_CAPTURE_WORD_LEN_MASK, val);
  382. return 0;
  383. }
  384. static int adav80x_set_playback_pcm_format(struct snd_soc_codec *codec,
  385. struct snd_soc_dai *dai, snd_pcm_format_t format)
  386. {
  387. struct adav80x *adav80x = snd_soc_codec_get_drvdata(codec);
  388. unsigned int val;
  389. if (adav80x->dai_fmt[dai->id] != SND_SOC_DAIFMT_RIGHT_J)
  390. return 0;
  391. switch (format) {
  392. case SNDRV_PCM_FORMAT_S16_LE:
  393. val = ADAV80X_PLAYBACK_MODE_RIGHT_J_16;
  394. break;
  395. case SNDRV_PCM_FORMAT_S18_3LE:
  396. val = ADAV80X_PLAYBACK_MODE_RIGHT_J_18;
  397. break;
  398. case SNDRV_PCM_FORMAT_S20_3LE:
  399. val = ADAV80X_PLAYBACK_MODE_RIGHT_J_20;
  400. break;
  401. case SNDRV_PCM_FORMAT_S24_LE:
  402. val = ADAV80X_PLAYBACK_MODE_RIGHT_J_24;
  403. break;
  404. default:
  405. return -EINVAL;
  406. }
  407. snd_soc_update_bits(codec, adav80x_port_ctrl_regs[dai->id][1],
  408. ADAV80X_PLAYBACK_MODE_MASK, val);
  409. return 0;
  410. }
  411. static int adav80x_hw_params(struct snd_pcm_substream *substream,
  412. struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
  413. {
  414. struct snd_soc_codec *codec = dai->codec;
  415. struct adav80x *adav80x = snd_soc_codec_get_drvdata(codec);
  416. unsigned int rate = params_rate(params);
  417. if (rate * 256 != adav80x->sysclk)
  418. return -EINVAL;
  419. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  420. adav80x_set_playback_pcm_format(codec, dai,
  421. params_format(params));
  422. adav80x_set_dac_clock(codec, rate);
  423. } else {
  424. adav80x_set_capture_pcm_format(codec, dai,
  425. params_format(params));
  426. adav80x_set_adc_clock(codec, rate);
  427. }
  428. adav80x->rate = rate;
  429. adav80x_set_deemph(codec);
  430. return 0;
  431. }
  432. static int adav80x_set_sysclk(struct snd_soc_codec *codec,
  433. int clk_id, int source,
  434. unsigned int freq, int dir)
  435. {
  436. struct adav80x *adav80x = snd_soc_codec_get_drvdata(codec);
  437. if (dir == SND_SOC_CLOCK_IN) {
  438. switch (clk_id) {
  439. case ADAV80X_CLK_XIN:
  440. case ADAV80X_CLK_XTAL:
  441. case ADAV80X_CLK_MCLKI:
  442. case ADAV80X_CLK_PLL1:
  443. case ADAV80X_CLK_PLL2:
  444. break;
  445. default:
  446. return -EINVAL;
  447. }
  448. adav80x->sysclk = freq;
  449. if (adav80x->clk_src != clk_id) {
  450. unsigned int iclk_ctrl1, iclk_ctrl2;
  451. adav80x->clk_src = clk_id;
  452. if (clk_id == ADAV80X_CLK_XTAL)
  453. clk_id = ADAV80X_CLK_XIN;
  454. iclk_ctrl1 = ADAV80X_ICLK_CTRL1_DAC_SRC(clk_id) |
  455. ADAV80X_ICLK_CTRL1_ADC_SRC(clk_id) |
  456. ADAV80X_ICLK_CTRL1_ICLK2_SRC(clk_id);
  457. iclk_ctrl2 = ADAV80X_ICLK_CTRL2_ICLK1_SRC(clk_id);
  458. snd_soc_write(codec, ADAV80X_ICLK_CTRL1, iclk_ctrl1);
  459. snd_soc_write(codec, ADAV80X_ICLK_CTRL2, iclk_ctrl2);
  460. snd_soc_dapm_sync(&codec->dapm);
  461. }
  462. } else {
  463. unsigned int mask;
  464. switch (clk_id) {
  465. case ADAV80X_CLK_SYSCLK1:
  466. case ADAV80X_CLK_SYSCLK2:
  467. case ADAV80X_CLK_SYSCLK3:
  468. break;
  469. default:
  470. return -EINVAL;
  471. }
  472. clk_id -= ADAV80X_CLK_SYSCLK1;
  473. mask = ADAV80X_PLL_OUTE_SYSCLKPD(clk_id);
  474. if (freq == 0) {
  475. snd_soc_update_bits(codec, ADAV80X_PLL_OUTE, mask, mask);
  476. adav80x->sysclk_pd[clk_id] = true;
  477. } else {
  478. snd_soc_update_bits(codec, ADAV80X_PLL_OUTE, mask, 0);
  479. adav80x->sysclk_pd[clk_id] = false;
  480. }
  481. if (adav80x->sysclk_pd[0])
  482. snd_soc_dapm_disable_pin(&codec->dapm, "PLL1");
  483. else
  484. snd_soc_dapm_force_enable_pin(&codec->dapm, "PLL1");
  485. if (adav80x->sysclk_pd[1] || adav80x->sysclk_pd[2])
  486. snd_soc_dapm_disable_pin(&codec->dapm, "PLL2");
  487. else
  488. snd_soc_dapm_force_enable_pin(&codec->dapm, "PLL2");
  489. snd_soc_dapm_sync(&codec->dapm);
  490. }
  491. return 0;
  492. }
  493. static int adav80x_set_pll(struct snd_soc_codec *codec, int pll_id,
  494. int source, unsigned int freq_in, unsigned int freq_out)
  495. {
  496. struct adav80x *adav80x = snd_soc_codec_get_drvdata(codec);
  497. unsigned int pll_ctrl1 = 0;
  498. unsigned int pll_ctrl2 = 0;
  499. unsigned int pll_src;
  500. switch (source) {
  501. case ADAV80X_PLL_SRC_XTAL:
  502. case ADAV80X_PLL_SRC_XIN:
  503. case ADAV80X_PLL_SRC_MCLKI:
  504. break;
  505. default:
  506. return -EINVAL;
  507. }
  508. if (!freq_out)
  509. return 0;
  510. switch (freq_in) {
  511. case 27000000:
  512. break;
  513. case 54000000:
  514. if (source == ADAV80X_PLL_SRC_XIN) {
  515. pll_ctrl1 |= ADAV80X_PLL_CTRL1_PLLDIV;
  516. break;
  517. }
  518. default:
  519. return -EINVAL;
  520. }
  521. if (freq_out > 12288000) {
  522. pll_ctrl2 |= ADAV80X_PLL_CTRL2_DOUB(pll_id);
  523. freq_out /= 2;
  524. }
  525. /* freq_out = sample_rate * 256 */
  526. switch (freq_out) {
  527. case 8192000:
  528. pll_ctrl2 |= ADAV80X_PLL_CTRL2_FS_32(pll_id);
  529. break;
  530. case 11289600:
  531. pll_ctrl2 |= ADAV80X_PLL_CTRL2_FS_44(pll_id);
  532. break;
  533. case 12288000:
  534. pll_ctrl2 |= ADAV80X_PLL_CTRL2_FS_48(pll_id);
  535. break;
  536. default:
  537. return -EINVAL;
  538. }
  539. snd_soc_update_bits(codec, ADAV80X_PLL_CTRL1, ADAV80X_PLL_CTRL1_PLLDIV,
  540. pll_ctrl1);
  541. snd_soc_update_bits(codec, ADAV80X_PLL_CTRL2,
  542. ADAV80X_PLL_CTRL2_PLL_MASK(pll_id), pll_ctrl2);
  543. if (source != adav80x->pll_src) {
  544. if (source == ADAV80X_PLL_SRC_MCLKI)
  545. pll_src = ADAV80X_PLL_CLK_SRC_PLL_MCLKI(pll_id);
  546. else
  547. pll_src = ADAV80X_PLL_CLK_SRC_PLL_XIN(pll_id);
  548. snd_soc_update_bits(codec, ADAV80X_PLL_CLK_SRC,
  549. ADAV80X_PLL_CLK_SRC_PLL_MASK(pll_id), pll_src);
  550. adav80x->pll_src = source;
  551. snd_soc_dapm_sync(&codec->dapm);
  552. }
  553. return 0;
  554. }
  555. static int adav80x_set_bias_level(struct snd_soc_codec *codec,
  556. enum snd_soc_bias_level level)
  557. {
  558. unsigned int mask = ADAV80X_DAC_CTRL1_PD;
  559. switch (level) {
  560. case SND_SOC_BIAS_ON:
  561. break;
  562. case SND_SOC_BIAS_PREPARE:
  563. break;
  564. case SND_SOC_BIAS_STANDBY:
  565. snd_soc_update_bits(codec, ADAV80X_DAC_CTRL1, mask, 0x00);
  566. break;
  567. case SND_SOC_BIAS_OFF:
  568. snd_soc_update_bits(codec, ADAV80X_DAC_CTRL1, mask, mask);
  569. break;
  570. }
  571. codec->dapm.bias_level = level;
  572. return 0;
  573. }
  574. /* Enforce the same sample rate on all audio interfaces */
  575. static int adav80x_dai_startup(struct snd_pcm_substream *substream,
  576. struct snd_soc_dai *dai)
  577. {
  578. struct snd_soc_codec *codec = dai->codec;
  579. struct adav80x *adav80x = snd_soc_codec_get_drvdata(codec);
  580. if (!codec->active || !adav80x->rate)
  581. return 0;
  582. return snd_pcm_hw_constraint_minmax(substream->runtime,
  583. SNDRV_PCM_HW_PARAM_RATE, adav80x->rate, adav80x->rate);
  584. }
  585. static void adav80x_dai_shutdown(struct snd_pcm_substream *substream,
  586. struct snd_soc_dai *dai)
  587. {
  588. struct snd_soc_codec *codec = dai->codec;
  589. struct adav80x *adav80x = snd_soc_codec_get_drvdata(codec);
  590. if (!codec->active)
  591. adav80x->rate = 0;
  592. }
  593. static const struct snd_soc_dai_ops adav80x_dai_ops = {
  594. .set_fmt = adav80x_set_dai_fmt,
  595. .hw_params = adav80x_hw_params,
  596. .startup = adav80x_dai_startup,
  597. .shutdown = adav80x_dai_shutdown,
  598. };
  599. #define ADAV80X_PLAYBACK_RATES (SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | \
  600. SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_64000 | SNDRV_PCM_RATE_88200 | \
  601. SNDRV_PCM_RATE_96000)
  602. #define ADAV80X_CAPTURE_RATES (SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_96000)
  603. #define ADAV80X_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S18_3LE | \
  604. SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S24_LE)
  605. static struct snd_soc_dai_driver adav80x_dais[] = {
  606. {
  607. .name = "adav80x-hifi",
  608. .id = 0,
  609. .playback = {
  610. .stream_name = "HiFi Playback",
  611. .channels_min = 2,
  612. .channels_max = 2,
  613. .rates = ADAV80X_PLAYBACK_RATES,
  614. .formats = ADAV80X_FORMATS,
  615. },
  616. .capture = {
  617. .stream_name = "HiFi Capture",
  618. .channels_min = 2,
  619. .channels_max = 2,
  620. .rates = ADAV80X_CAPTURE_RATES,
  621. .formats = ADAV80X_FORMATS,
  622. },
  623. .ops = &adav80x_dai_ops,
  624. },
  625. {
  626. .name = "adav80x-aux",
  627. .id = 1,
  628. .playback = {
  629. .stream_name = "Aux Playback",
  630. .channels_min = 2,
  631. .channels_max = 2,
  632. .rates = ADAV80X_PLAYBACK_RATES,
  633. .formats = ADAV80X_FORMATS,
  634. },
  635. .capture = {
  636. .stream_name = "Aux Capture",
  637. .channels_min = 2,
  638. .channels_max = 2,
  639. .rates = ADAV80X_CAPTURE_RATES,
  640. .formats = ADAV80X_FORMATS,
  641. },
  642. .ops = &adav80x_dai_ops,
  643. },
  644. };
  645. static int adav80x_probe(struct snd_soc_codec *codec)
  646. {
  647. int ret;
  648. struct adav80x *adav80x = snd_soc_codec_get_drvdata(codec);
  649. ret = snd_soc_codec_set_cache_io(codec, 7, 9, adav80x->control_type);
  650. if (ret) {
  651. dev_err(codec->dev, "failed to set cache I/O: %d\n", ret);
  652. return ret;
  653. }
  654. /* Force PLLs on for SYSCLK output */
  655. snd_soc_dapm_force_enable_pin(&codec->dapm, "PLL1");
  656. snd_soc_dapm_force_enable_pin(&codec->dapm, "PLL2");
  657. /* Power down S/PDIF receiver, since it is currently not supported */
  658. snd_soc_write(codec, ADAV80X_PLL_OUTE, 0x20);
  659. /* Disable DAC zero flag */
  660. snd_soc_write(codec, ADAV80X_DAC_CTRL3, 0x6);
  661. return adav80x_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  662. }
  663. static int adav80x_suspend(struct snd_soc_codec *codec)
  664. {
  665. return adav80x_set_bias_level(codec, SND_SOC_BIAS_OFF);
  666. }
  667. static int adav80x_resume(struct snd_soc_codec *codec)
  668. {
  669. adav80x_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  670. codec->cache_sync = 1;
  671. snd_soc_cache_sync(codec);
  672. return 0;
  673. }
  674. static int adav80x_remove(struct snd_soc_codec *codec)
  675. {
  676. return adav80x_set_bias_level(codec, SND_SOC_BIAS_OFF);
  677. }
  678. static struct snd_soc_codec_driver adav80x_codec_driver = {
  679. .probe = adav80x_probe,
  680. .remove = adav80x_remove,
  681. .suspend = adav80x_suspend,
  682. .resume = adav80x_resume,
  683. .set_bias_level = adav80x_set_bias_level,
  684. .set_pll = adav80x_set_pll,
  685. .set_sysclk = adav80x_set_sysclk,
  686. .reg_word_size = sizeof(u8),
  687. .reg_cache_size = ARRAY_SIZE(adav80x_default_regs),
  688. .reg_cache_default = adav80x_default_regs,
  689. .controls = adav80x_controls,
  690. .num_controls = ARRAY_SIZE(adav80x_controls),
  691. .dapm_widgets = adav80x_dapm_widgets,
  692. .num_dapm_widgets = ARRAY_SIZE(adav80x_dapm_widgets),
  693. .dapm_routes = adav80x_dapm_routes,
  694. .num_dapm_routes = ARRAY_SIZE(adav80x_dapm_routes),
  695. };
  696. static int __devinit adav80x_bus_probe(struct device *dev,
  697. enum snd_soc_control_type control_type)
  698. {
  699. struct adav80x *adav80x;
  700. int ret;
  701. adav80x = kzalloc(sizeof(*adav80x), GFP_KERNEL);
  702. if (!adav80x)
  703. return -ENOMEM;
  704. dev_set_drvdata(dev, adav80x);
  705. adav80x->control_type = control_type;
  706. ret = snd_soc_register_codec(dev, &adav80x_codec_driver,
  707. adav80x_dais, ARRAY_SIZE(adav80x_dais));
  708. if (ret)
  709. kfree(adav80x);
  710. return ret;
  711. }
  712. static int __devexit adav80x_bus_remove(struct device *dev)
  713. {
  714. snd_soc_unregister_codec(dev);
  715. kfree(dev_get_drvdata(dev));
  716. return 0;
  717. }
  718. #if defined(CONFIG_SPI_MASTER)
  719. static int __devinit adav80x_spi_probe(struct spi_device *spi)
  720. {
  721. return adav80x_bus_probe(&spi->dev, SND_SOC_SPI);
  722. }
  723. static int __devexit adav80x_spi_remove(struct spi_device *spi)
  724. {
  725. return adav80x_bus_remove(&spi->dev);
  726. }
  727. static struct spi_driver adav80x_spi_driver = {
  728. .driver = {
  729. .name = "adav801",
  730. .owner = THIS_MODULE,
  731. },
  732. .probe = adav80x_spi_probe,
  733. .remove = __devexit_p(adav80x_spi_remove),
  734. };
  735. #endif
  736. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  737. static const struct i2c_device_id adav80x_id[] = {
  738. { "adav803", 0 },
  739. { }
  740. };
  741. MODULE_DEVICE_TABLE(i2c, adav80x_id);
  742. static int __devinit adav80x_i2c_probe(struct i2c_client *client,
  743. const struct i2c_device_id *id)
  744. {
  745. return adav80x_bus_probe(&client->dev, SND_SOC_I2C);
  746. }
  747. static int __devexit adav80x_i2c_remove(struct i2c_client *client)
  748. {
  749. return adav80x_bus_remove(&client->dev);
  750. }
  751. static struct i2c_driver adav80x_i2c_driver = {
  752. .driver = {
  753. .name = "adav803",
  754. .owner = THIS_MODULE,
  755. },
  756. .probe = adav80x_i2c_probe,
  757. .remove = __devexit_p(adav80x_i2c_remove),
  758. .id_table = adav80x_id,
  759. };
  760. #endif
  761. static int __init adav80x_init(void)
  762. {
  763. int ret = 0;
  764. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  765. ret = i2c_add_driver(&adav80x_i2c_driver);
  766. if (ret)
  767. return ret;
  768. #endif
  769. #if defined(CONFIG_SPI_MASTER)
  770. ret = spi_register_driver(&adav80x_spi_driver);
  771. #endif
  772. return ret;
  773. }
  774. module_init(adav80x_init);
  775. static void __exit adav80x_exit(void)
  776. {
  777. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  778. i2c_del_driver(&adav80x_i2c_driver);
  779. #endif
  780. #if defined(CONFIG_SPI_MASTER)
  781. spi_unregister_driver(&adav80x_spi_driver);
  782. #endif
  783. }
  784. module_exit(adav80x_exit);
  785. MODULE_DESCRIPTION("ASoC ADAV80x driver");
  786. MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
  787. MODULE_AUTHOR("Yi Li <yi.li@analog.com>>");
  788. MODULE_LICENSE("GPL");