xonar_pcm179x.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145
  1. /*
  2. * card driver for models with PCM1796 DACs (Xonar D2/D2X/HDAV1.3/ST/STX)
  3. *
  4. * Copyright (c) Clemens Ladisch <clemens@ladisch.de>
  5. *
  6. *
  7. * This driver is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License, version 2.
  9. *
  10. * This driver is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this driver; if not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /*
  19. * Xonar D2/D2X
  20. * ------------
  21. *
  22. * CMI8788:
  23. *
  24. * SPI 0 -> 1st PCM1796 (front)
  25. * SPI 1 -> 2nd PCM1796 (surround)
  26. * SPI 2 -> 3rd PCM1796 (center/LFE)
  27. * SPI 4 -> 4th PCM1796 (back)
  28. *
  29. * GPIO 2 -> M0 of CS5381
  30. * GPIO 3 -> M1 of CS5381
  31. * GPIO 5 <- external power present (D2X only)
  32. * GPIO 7 -> ALT
  33. * GPIO 8 -> enable output to speakers
  34. *
  35. * CM9780:
  36. *
  37. * LINE_OUT -> input of ADC
  38. *
  39. * AUX_IN <- aux
  40. * VIDEO_IN <- CD
  41. * FMIC_IN <- mic
  42. *
  43. * GPO 0 -> route line-in (0) or AC97 output (1) to CS5381 input
  44. */
  45. /*
  46. * Xonar HDAV1.3 (Deluxe)
  47. * ----------------------
  48. *
  49. * CMI8788:
  50. *
  51. * I²C <-> PCM1796 (addr 1001100) (front)
  52. *
  53. * GPI 0 <- external power present
  54. *
  55. * GPIO 0 -> enable HDMI (0) or speaker (1) output
  56. * GPIO 2 -> M0 of CS5381
  57. * GPIO 3 -> M1 of CS5381
  58. * GPIO 4 <- daughterboard detection
  59. * GPIO 5 <- daughterboard detection
  60. * GPIO 6 -> ?
  61. * GPIO 7 -> ?
  62. * GPIO 8 -> route input jack to line-in (0) or mic-in (1)
  63. *
  64. * UART <-> HDMI controller
  65. *
  66. * CM9780:
  67. *
  68. * LINE_OUT -> input of ADC
  69. *
  70. * AUX_IN <- aux
  71. * CD_IN <- CD
  72. * MIC_IN <- mic
  73. *
  74. * GPO 0 -> route line-in (0) or AC97 output (1) to CS5381 input
  75. *
  76. * no daughterboard
  77. * ----------------
  78. *
  79. * GPIO 4 <- 1
  80. *
  81. * H6 daughterboard
  82. * ----------------
  83. *
  84. * GPIO 4 <- 0
  85. * GPIO 5 <- 0
  86. *
  87. * I²C <-> PCM1796 (addr 1001101) (surround)
  88. * <-> PCM1796 (addr 1001110) (center/LFE)
  89. * <-> PCM1796 (addr 1001111) (back)
  90. *
  91. * unknown daughterboard
  92. * ---------------------
  93. *
  94. * GPIO 4 <- 0
  95. * GPIO 5 <- 1
  96. *
  97. * I²C <-> CS4362A (addr 0011000) (surround, center/LFE, back)
  98. */
  99. /*
  100. * Xonar Essence ST (Deluxe)/STX
  101. * -----------------------------
  102. *
  103. * CMI8788:
  104. *
  105. * I²C <-> PCM1792A (addr 1001100)
  106. * <-> CS2000 (addr 1001110) (ST only)
  107. *
  108. * ADC1 MCLK -> REF_CLK of CS2000 (ST only)
  109. *
  110. * GPI 0 <- external power present (STX only)
  111. *
  112. * GPIO 0 -> enable output to speakers
  113. * GPIO 1 -> route HP to front panel (0) or rear jack (1)
  114. * GPIO 2 -> M0 of CS5381
  115. * GPIO 3 -> M1 of CS5381
  116. * GPIO 4 <- daughterboard detection
  117. * GPIO 5 <- daughterboard detection
  118. * GPIO 6 -> ?
  119. * GPIO 7 -> route output to speaker jacks (0) or HP (1)
  120. * GPIO 8 -> route input jack to line-in (0) or mic-in (1)
  121. *
  122. * PCM1792A:
  123. *
  124. * SCK <- CLK_OUT of CS2000 (ST only)
  125. *
  126. * CM9780:
  127. *
  128. * LINE_OUT -> input of ADC
  129. *
  130. * AUX_IN <- aux
  131. * MIC_IN <- mic
  132. *
  133. * GPO 0 -> route line-in (0) or AC97 output (1) to CS5381 input
  134. *
  135. * H6 daughterboard
  136. * ----------------
  137. *
  138. * GPIO 4 <- 0
  139. * GPIO 5 <- 0
  140. */
  141. /*
  142. * Xonar Xense
  143. * -----------
  144. *
  145. * CMI8788:
  146. *
  147. * I²C <-> PCM1796 (addr 1001100) (front)
  148. * <-> CS4362A (addr 0011000) (surround, center/LFE, back)
  149. * <-> CS2000 (addr 1001110)
  150. *
  151. * ADC1 MCLK -> REF_CLK of CS2000
  152. *
  153. * GPI 0 <- external power present
  154. *
  155. * GPIO 0 -> enable output
  156. * GPIO 1 -> route HP to front panel (0) or rear jack (1)
  157. * GPIO 2 -> M0 of CS5381
  158. * GPIO 3 -> M1 of CS5381
  159. * GPIO 4 -> enable output
  160. * GPIO 5 -> enable output
  161. * GPIO 6 -> ?
  162. * GPIO 7 -> route output to HP (0) or speaker (1)
  163. * GPIO 8 -> route input jack to mic-in (0) or line-in (1)
  164. *
  165. * CM9780:
  166. *
  167. * LINE_OUT -> input of ADC
  168. *
  169. * AUX_IN <- aux
  170. * VIDEO_IN <- ?
  171. * FMIC_IN <- mic
  172. *
  173. * GPO 0 -> route line-in (0) or AC97 output (1) to CS5381 input
  174. * GPO 1 -> route mic-in from input jack (0) or front panel header (1)
  175. */
  176. #include <linux/pci.h>
  177. #include <linux/delay.h>
  178. #include <linux/mutex.h>
  179. #include <sound/ac97_codec.h>
  180. #include <sound/control.h>
  181. #include <sound/core.h>
  182. #include <sound/info.h>
  183. #include <sound/pcm.h>
  184. #include <sound/pcm_params.h>
  185. #include <sound/tlv.h>
  186. #include "xonar.h"
  187. #include "cm9780.h"
  188. #include "pcm1796.h"
  189. #include "cs2000.h"
  190. #define GPIO_D2X_EXT_POWER 0x0020
  191. #define GPIO_D2_ALT 0x0080
  192. #define GPIO_D2_OUTPUT_ENABLE 0x0100
  193. #define GPI_EXT_POWER 0x01
  194. #define GPIO_INPUT_ROUTE 0x0100
  195. #define GPIO_HDAV_OUTPUT_ENABLE 0x0001
  196. #define GPIO_HDAV_MAGIC 0x00c0
  197. #define GPIO_DB_MASK 0x0030
  198. #define GPIO_DB_H6 0x0000
  199. #define GPIO_ST_OUTPUT_ENABLE 0x0001
  200. #define GPIO_ST_HP_REAR 0x0002
  201. #define GPIO_ST_MAGIC 0x0040
  202. #define GPIO_ST_HP 0x0080
  203. #define I2C_DEVICE_PCM1796(i) (0x98 + ((i) << 1)) /* 10011, ii, /W=0 */
  204. #define I2C_DEVICE_CS2000 0x9c /* 100111, 0, /W=0 */
  205. #define PCM1796_REG_BASE 16
  206. struct xonar_pcm179x {
  207. struct xonar_generic generic;
  208. unsigned int dacs;
  209. u8 pcm1796_regs[4][5];
  210. unsigned int current_rate;
  211. bool h6;
  212. bool hp_active;
  213. s8 hp_gain_offset;
  214. bool has_cs2000;
  215. u8 cs2000_regs[0x1f];
  216. bool broken_i2c;
  217. };
  218. struct xonar_hdav {
  219. struct xonar_pcm179x pcm179x;
  220. struct xonar_hdmi hdmi;
  221. };
  222. static inline void pcm1796_write_spi(struct oxygen *chip, unsigned int codec,
  223. u8 reg, u8 value)
  224. {
  225. /* maps ALSA channel pair number to SPI output */
  226. static const u8 codec_map[4] = {
  227. 0, 1, 2, 4
  228. };
  229. oxygen_write_spi(chip, OXYGEN_SPI_TRIGGER |
  230. OXYGEN_SPI_DATA_LENGTH_2 |
  231. OXYGEN_SPI_CLOCK_160 |
  232. (codec_map[codec] << OXYGEN_SPI_CODEC_SHIFT) |
  233. OXYGEN_SPI_CEN_LATCH_CLOCK_HI,
  234. (reg << 8) | value);
  235. }
  236. static inline void pcm1796_write_i2c(struct oxygen *chip, unsigned int codec,
  237. u8 reg, u8 value)
  238. {
  239. oxygen_write_i2c(chip, I2C_DEVICE_PCM1796(codec), reg, value);
  240. }
  241. static void pcm1796_write(struct oxygen *chip, unsigned int codec,
  242. u8 reg, u8 value)
  243. {
  244. struct xonar_pcm179x *data = chip->model_data;
  245. if ((chip->model.function_flags & OXYGEN_FUNCTION_2WIRE_SPI_MASK) ==
  246. OXYGEN_FUNCTION_SPI)
  247. pcm1796_write_spi(chip, codec, reg, value);
  248. else
  249. pcm1796_write_i2c(chip, codec, reg, value);
  250. if ((unsigned int)(reg - PCM1796_REG_BASE)
  251. < ARRAY_SIZE(data->pcm1796_regs[codec]))
  252. data->pcm1796_regs[codec][reg - PCM1796_REG_BASE] = value;
  253. }
  254. static void pcm1796_write_cached(struct oxygen *chip, unsigned int codec,
  255. u8 reg, u8 value)
  256. {
  257. struct xonar_pcm179x *data = chip->model_data;
  258. if (value != data->pcm1796_regs[codec][reg - PCM1796_REG_BASE])
  259. pcm1796_write(chip, codec, reg, value);
  260. }
  261. static void cs2000_write(struct oxygen *chip, u8 reg, u8 value)
  262. {
  263. struct xonar_pcm179x *data = chip->model_data;
  264. oxygen_write_i2c(chip, I2C_DEVICE_CS2000, reg, value);
  265. data->cs2000_regs[reg] = value;
  266. }
  267. static void cs2000_write_cached(struct oxygen *chip, u8 reg, u8 value)
  268. {
  269. struct xonar_pcm179x *data = chip->model_data;
  270. if (value != data->cs2000_regs[reg])
  271. cs2000_write(chip, reg, value);
  272. }
  273. static void pcm1796_registers_init(struct oxygen *chip)
  274. {
  275. struct xonar_pcm179x *data = chip->model_data;
  276. unsigned int i;
  277. s8 gain_offset;
  278. msleep(1);
  279. gain_offset = data->hp_active ? data->hp_gain_offset : 0;
  280. for (i = 0; i < data->dacs; ++i) {
  281. /* set ATLD before ATL/ATR */
  282. pcm1796_write(chip, i, 18,
  283. data->pcm1796_regs[0][18 - PCM1796_REG_BASE]);
  284. pcm1796_write(chip, i, 16, chip->dac_volume[i * 2]
  285. + gain_offset);
  286. pcm1796_write(chip, i, 17, chip->dac_volume[i * 2 + 1]
  287. + gain_offset);
  288. pcm1796_write(chip, i, 19,
  289. data->pcm1796_regs[0][19 - PCM1796_REG_BASE]);
  290. pcm1796_write(chip, i, 20,
  291. data->pcm1796_regs[0][20 - PCM1796_REG_BASE]);
  292. pcm1796_write(chip, i, 21, 0);
  293. gain_offset = 0;
  294. }
  295. }
  296. static void pcm1796_init(struct oxygen *chip)
  297. {
  298. struct xonar_pcm179x *data = chip->model_data;
  299. data->pcm1796_regs[0][18 - PCM1796_REG_BASE] =
  300. PCM1796_DMF_DISABLED | PCM1796_FMT_24_I2S | PCM1796_ATLD;
  301. if (!data->broken_i2c)
  302. data->pcm1796_regs[0][18 - PCM1796_REG_BASE] |= PCM1796_MUTE;
  303. data->pcm1796_regs[0][19 - PCM1796_REG_BASE] =
  304. PCM1796_FLT_SHARP | PCM1796_ATS_1;
  305. data->pcm1796_regs[0][20 - PCM1796_REG_BASE] =
  306. data->h6 ? PCM1796_OS_64 : PCM1796_OS_128;
  307. pcm1796_registers_init(chip);
  308. data->current_rate = 48000;
  309. }
  310. static void xonar_d2_init(struct oxygen *chip)
  311. {
  312. struct xonar_pcm179x *data = chip->model_data;
  313. data->generic.anti_pop_delay = 300;
  314. data->generic.output_enable_bit = GPIO_D2_OUTPUT_ENABLE;
  315. data->dacs = 4;
  316. pcm1796_init(chip);
  317. oxygen_set_bits16(chip, OXYGEN_GPIO_CONTROL, GPIO_D2_ALT);
  318. oxygen_clear_bits16(chip, OXYGEN_GPIO_DATA, GPIO_D2_ALT);
  319. oxygen_ac97_set_bits(chip, 0, CM9780_JACK, CM9780_FMIC2MIC);
  320. xonar_init_cs53x1(chip);
  321. xonar_enable_output(chip);
  322. snd_component_add(chip->card, "PCM1796");
  323. snd_component_add(chip->card, "CS5381");
  324. }
  325. static void xonar_d2x_init(struct oxygen *chip)
  326. {
  327. struct xonar_pcm179x *data = chip->model_data;
  328. data->generic.ext_power_reg = OXYGEN_GPIO_DATA;
  329. data->generic.ext_power_int_reg = OXYGEN_GPIO_INTERRUPT_MASK;
  330. data->generic.ext_power_bit = GPIO_D2X_EXT_POWER;
  331. oxygen_clear_bits16(chip, OXYGEN_GPIO_CONTROL, GPIO_D2X_EXT_POWER);
  332. xonar_init_ext_power(chip);
  333. xonar_d2_init(chip);
  334. }
  335. static void xonar_hdav_init(struct oxygen *chip)
  336. {
  337. struct xonar_hdav *data = chip->model_data;
  338. oxygen_write16(chip, OXYGEN_2WIRE_BUS_STATUS,
  339. OXYGEN_2WIRE_LENGTH_8 |
  340. OXYGEN_2WIRE_INTERRUPT_MASK |
  341. OXYGEN_2WIRE_SPEED_STANDARD);
  342. data->pcm179x.generic.anti_pop_delay = 100;
  343. data->pcm179x.generic.output_enable_bit = GPIO_HDAV_OUTPUT_ENABLE;
  344. data->pcm179x.generic.ext_power_reg = OXYGEN_GPI_DATA;
  345. data->pcm179x.generic.ext_power_int_reg = OXYGEN_GPI_INTERRUPT_MASK;
  346. data->pcm179x.generic.ext_power_bit = GPI_EXT_POWER;
  347. data->pcm179x.dacs = chip->model.dac_channels_mixer / 2;
  348. data->pcm179x.h6 = chip->model.dac_channels_mixer > 2;
  349. pcm1796_init(chip);
  350. oxygen_set_bits16(chip, OXYGEN_GPIO_CONTROL,
  351. GPIO_HDAV_MAGIC | GPIO_INPUT_ROUTE);
  352. oxygen_clear_bits16(chip, OXYGEN_GPIO_DATA, GPIO_INPUT_ROUTE);
  353. xonar_init_cs53x1(chip);
  354. xonar_init_ext_power(chip);
  355. xonar_hdmi_init(chip, &data->hdmi);
  356. xonar_enable_output(chip);
  357. snd_component_add(chip->card, "PCM1796");
  358. snd_component_add(chip->card, "CS5381");
  359. }
  360. static void xonar_st_init_i2c(struct oxygen *chip)
  361. {
  362. oxygen_write16(chip, OXYGEN_2WIRE_BUS_STATUS,
  363. OXYGEN_2WIRE_LENGTH_8 |
  364. OXYGEN_2WIRE_INTERRUPT_MASK |
  365. OXYGEN_2WIRE_SPEED_STANDARD);
  366. }
  367. static void xonar_st_init_common(struct oxygen *chip)
  368. {
  369. struct xonar_pcm179x *data = chip->model_data;
  370. data->generic.output_enable_bit = GPIO_ST_OUTPUT_ENABLE;
  371. data->dacs = chip->model.dac_channels_mixer / 2;
  372. data->hp_gain_offset = 2*-18;
  373. pcm1796_init(chip);
  374. oxygen_set_bits16(chip, OXYGEN_GPIO_CONTROL,
  375. GPIO_INPUT_ROUTE | GPIO_ST_HP_REAR |
  376. GPIO_ST_MAGIC | GPIO_ST_HP);
  377. oxygen_clear_bits16(chip, OXYGEN_GPIO_DATA,
  378. GPIO_INPUT_ROUTE | GPIO_ST_HP_REAR | GPIO_ST_HP);
  379. xonar_init_cs53x1(chip);
  380. xonar_enable_output(chip);
  381. snd_component_add(chip->card, "PCM1792A");
  382. snd_component_add(chip->card, "CS5381");
  383. }
  384. static void cs2000_registers_init(struct oxygen *chip)
  385. {
  386. struct xonar_pcm179x *data = chip->model_data;
  387. cs2000_write(chip, CS2000_GLOBAL_CFG, CS2000_FREEZE);
  388. cs2000_write(chip, CS2000_DEV_CTRL, 0);
  389. cs2000_write(chip, CS2000_DEV_CFG_1,
  390. CS2000_R_MOD_SEL_1 |
  391. (0 << CS2000_R_SEL_SHIFT) |
  392. CS2000_AUX_OUT_SRC_REF_CLK |
  393. CS2000_EN_DEV_CFG_1);
  394. cs2000_write(chip, CS2000_DEV_CFG_2,
  395. (0 << CS2000_LOCK_CLK_SHIFT) |
  396. CS2000_FRAC_N_SRC_STATIC);
  397. cs2000_write(chip, CS2000_RATIO_0 + 0, 0x00); /* 1.0 */
  398. cs2000_write(chip, CS2000_RATIO_0 + 1, 0x10);
  399. cs2000_write(chip, CS2000_RATIO_0 + 2, 0x00);
  400. cs2000_write(chip, CS2000_RATIO_0 + 3, 0x00);
  401. cs2000_write(chip, CS2000_FUN_CFG_1,
  402. data->cs2000_regs[CS2000_FUN_CFG_1]);
  403. cs2000_write(chip, CS2000_FUN_CFG_2, 0);
  404. cs2000_write(chip, CS2000_GLOBAL_CFG, CS2000_EN_DEV_CFG_2);
  405. msleep(3); /* PLL lock delay */
  406. }
  407. static void xonar_st_init(struct oxygen *chip)
  408. {
  409. struct xonar_pcm179x *data = chip->model_data;
  410. data->generic.anti_pop_delay = 100;
  411. data->h6 = chip->model.dac_channels_mixer > 2;
  412. data->has_cs2000 = 1;
  413. data->cs2000_regs[CS2000_FUN_CFG_1] = CS2000_REF_CLK_DIV_1;
  414. data->broken_i2c = true;
  415. oxygen_write16(chip, OXYGEN_I2S_A_FORMAT,
  416. OXYGEN_RATE_48000 |
  417. OXYGEN_I2S_FORMAT_I2S |
  418. OXYGEN_I2S_MCLK(data->h6 ? MCLK_256 : MCLK_512) |
  419. OXYGEN_I2S_BITS_16 |
  420. OXYGEN_I2S_MASTER |
  421. OXYGEN_I2S_BCLK_64);
  422. xonar_st_init_i2c(chip);
  423. cs2000_registers_init(chip);
  424. xonar_st_init_common(chip);
  425. snd_component_add(chip->card, "CS2000");
  426. }
  427. static void xonar_stx_init(struct oxygen *chip)
  428. {
  429. struct xonar_pcm179x *data = chip->model_data;
  430. xonar_st_init_i2c(chip);
  431. data->generic.anti_pop_delay = 800;
  432. data->generic.ext_power_reg = OXYGEN_GPI_DATA;
  433. data->generic.ext_power_int_reg = OXYGEN_GPI_INTERRUPT_MASK;
  434. data->generic.ext_power_bit = GPI_EXT_POWER;
  435. xonar_init_ext_power(chip);
  436. xonar_st_init_common(chip);
  437. }
  438. static void xonar_d2_cleanup(struct oxygen *chip)
  439. {
  440. xonar_disable_output(chip);
  441. }
  442. static void xonar_hdav_cleanup(struct oxygen *chip)
  443. {
  444. xonar_hdmi_cleanup(chip);
  445. xonar_disable_output(chip);
  446. msleep(2);
  447. }
  448. static void xonar_st_cleanup(struct oxygen *chip)
  449. {
  450. xonar_disable_output(chip);
  451. }
  452. static void xonar_d2_suspend(struct oxygen *chip)
  453. {
  454. xonar_d2_cleanup(chip);
  455. }
  456. static void xonar_hdav_suspend(struct oxygen *chip)
  457. {
  458. xonar_hdav_cleanup(chip);
  459. }
  460. static void xonar_st_suspend(struct oxygen *chip)
  461. {
  462. xonar_st_cleanup(chip);
  463. }
  464. static void xonar_d2_resume(struct oxygen *chip)
  465. {
  466. pcm1796_registers_init(chip);
  467. xonar_enable_output(chip);
  468. }
  469. static void xonar_hdav_resume(struct oxygen *chip)
  470. {
  471. struct xonar_hdav *data = chip->model_data;
  472. pcm1796_registers_init(chip);
  473. xonar_hdmi_resume(chip, &data->hdmi);
  474. xonar_enable_output(chip);
  475. }
  476. static void xonar_stx_resume(struct oxygen *chip)
  477. {
  478. pcm1796_registers_init(chip);
  479. xonar_enable_output(chip);
  480. }
  481. static void xonar_st_resume(struct oxygen *chip)
  482. {
  483. cs2000_registers_init(chip);
  484. xonar_stx_resume(chip);
  485. }
  486. static void update_pcm1796_oversampling(struct oxygen *chip)
  487. {
  488. struct xonar_pcm179x *data = chip->model_data;
  489. unsigned int i;
  490. u8 reg;
  491. if (data->current_rate <= 48000 && !data->h6)
  492. reg = PCM1796_OS_128;
  493. else
  494. reg = PCM1796_OS_64;
  495. for (i = 0; i < data->dacs; ++i)
  496. pcm1796_write_cached(chip, i, 20, reg);
  497. }
  498. static void set_pcm1796_params(struct oxygen *chip,
  499. struct snd_pcm_hw_params *params)
  500. {
  501. struct xonar_pcm179x *data = chip->model_data;
  502. msleep(1);
  503. data->current_rate = params_rate(params);
  504. update_pcm1796_oversampling(chip);
  505. }
  506. static void update_pcm1796_volume(struct oxygen *chip)
  507. {
  508. struct xonar_pcm179x *data = chip->model_data;
  509. unsigned int i;
  510. s8 gain_offset;
  511. gain_offset = data->hp_active ? data->hp_gain_offset : 0;
  512. for (i = 0; i < data->dacs; ++i) {
  513. pcm1796_write_cached(chip, i, 16, chip->dac_volume[i * 2]
  514. + gain_offset);
  515. pcm1796_write_cached(chip, i, 17, chip->dac_volume[i * 2 + 1]
  516. + gain_offset);
  517. gain_offset = 0;
  518. }
  519. }
  520. static void update_pcm1796_mute(struct oxygen *chip)
  521. {
  522. struct xonar_pcm179x *data = chip->model_data;
  523. unsigned int i;
  524. u8 value;
  525. value = PCM1796_DMF_DISABLED | PCM1796_FMT_24_I2S | PCM1796_ATLD;
  526. if (chip->dac_mute)
  527. value |= PCM1796_MUTE;
  528. for (i = 0; i < data->dacs; ++i)
  529. pcm1796_write_cached(chip, i, 18, value);
  530. }
  531. static void update_cs2000_rate(struct oxygen *chip, unsigned int rate)
  532. {
  533. struct xonar_pcm179x *data = chip->model_data;
  534. u8 rate_mclk, reg;
  535. switch (rate) {
  536. case 32000:
  537. case 64000:
  538. rate_mclk = OXYGEN_RATE_32000;
  539. break;
  540. case 44100:
  541. case 88200:
  542. case 176400:
  543. rate_mclk = OXYGEN_RATE_44100;
  544. break;
  545. default:
  546. case 48000:
  547. case 96000:
  548. case 192000:
  549. rate_mclk = OXYGEN_RATE_48000;
  550. break;
  551. }
  552. if (rate <= 96000 && (rate > 48000 || data->h6)) {
  553. rate_mclk |= OXYGEN_I2S_MCLK(MCLK_256);
  554. reg = CS2000_REF_CLK_DIV_1;
  555. } else {
  556. rate_mclk |= OXYGEN_I2S_MCLK(MCLK_512);
  557. reg = CS2000_REF_CLK_DIV_2;
  558. }
  559. oxygen_write16_masked(chip, OXYGEN_I2S_A_FORMAT, rate_mclk,
  560. OXYGEN_I2S_RATE_MASK | OXYGEN_I2S_MCLK_MASK);
  561. cs2000_write_cached(chip, CS2000_FUN_CFG_1, reg);
  562. msleep(3); /* PLL lock delay */
  563. }
  564. static void set_st_params(struct oxygen *chip,
  565. struct snd_pcm_hw_params *params)
  566. {
  567. update_cs2000_rate(chip, params_rate(params));
  568. set_pcm1796_params(chip, params);
  569. }
  570. static void set_hdav_params(struct oxygen *chip,
  571. struct snd_pcm_hw_params *params)
  572. {
  573. struct xonar_hdav *data = chip->model_data;
  574. set_pcm1796_params(chip, params);
  575. xonar_set_hdmi_params(chip, &data->hdmi, params);
  576. }
  577. static const struct snd_kcontrol_new alt_switch = {
  578. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  579. .name = "Analog Loopback Switch",
  580. .info = snd_ctl_boolean_mono_info,
  581. .get = xonar_gpio_bit_switch_get,
  582. .put = xonar_gpio_bit_switch_put,
  583. .private_value = GPIO_D2_ALT,
  584. };
  585. static int rolloff_info(struct snd_kcontrol *ctl,
  586. struct snd_ctl_elem_info *info)
  587. {
  588. static const char *const names[2] = {
  589. "Sharp Roll-off", "Slow Roll-off"
  590. };
  591. return snd_ctl_enum_info(info, 1, 2, names);
  592. }
  593. static int rolloff_get(struct snd_kcontrol *ctl,
  594. struct snd_ctl_elem_value *value)
  595. {
  596. struct oxygen *chip = ctl->private_data;
  597. struct xonar_pcm179x *data = chip->model_data;
  598. value->value.enumerated.item[0] =
  599. (data->pcm1796_regs[0][19 - PCM1796_REG_BASE] &
  600. PCM1796_FLT_MASK) != PCM1796_FLT_SHARP;
  601. return 0;
  602. }
  603. static int rolloff_put(struct snd_kcontrol *ctl,
  604. struct snd_ctl_elem_value *value)
  605. {
  606. struct oxygen *chip = ctl->private_data;
  607. struct xonar_pcm179x *data = chip->model_data;
  608. unsigned int i;
  609. int changed;
  610. u8 reg;
  611. mutex_lock(&chip->mutex);
  612. reg = data->pcm1796_regs[0][19 - PCM1796_REG_BASE];
  613. reg &= ~PCM1796_FLT_MASK;
  614. if (!value->value.enumerated.item[0])
  615. reg |= PCM1796_FLT_SHARP;
  616. else
  617. reg |= PCM1796_FLT_SLOW;
  618. changed = reg != data->pcm1796_regs[0][19 - PCM1796_REG_BASE];
  619. if (changed) {
  620. for (i = 0; i < data->dacs; ++i)
  621. pcm1796_write(chip, i, 19, reg);
  622. }
  623. mutex_unlock(&chip->mutex);
  624. return changed;
  625. }
  626. static const struct snd_kcontrol_new rolloff_control = {
  627. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  628. .name = "DAC Filter Playback Enum",
  629. .info = rolloff_info,
  630. .get = rolloff_get,
  631. .put = rolloff_put,
  632. };
  633. static const struct snd_kcontrol_new hdav_hdmi_control = {
  634. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  635. .name = "HDMI Playback Switch",
  636. .info = snd_ctl_boolean_mono_info,
  637. .get = xonar_gpio_bit_switch_get,
  638. .put = xonar_gpio_bit_switch_put,
  639. .private_value = GPIO_HDAV_OUTPUT_ENABLE | XONAR_GPIO_BIT_INVERT,
  640. };
  641. static int st_output_switch_info(struct snd_kcontrol *ctl,
  642. struct snd_ctl_elem_info *info)
  643. {
  644. static const char *const names[3] = {
  645. "Speakers", "Headphones", "FP Headphones"
  646. };
  647. return snd_ctl_enum_info(info, 1, 3, names);
  648. }
  649. static int st_output_switch_get(struct snd_kcontrol *ctl,
  650. struct snd_ctl_elem_value *value)
  651. {
  652. struct oxygen *chip = ctl->private_data;
  653. u16 gpio;
  654. gpio = oxygen_read16(chip, OXYGEN_GPIO_DATA);
  655. if (!(gpio & GPIO_ST_HP))
  656. value->value.enumerated.item[0] = 0;
  657. else if (gpio & GPIO_ST_HP_REAR)
  658. value->value.enumerated.item[0] = 1;
  659. else
  660. value->value.enumerated.item[0] = 2;
  661. return 0;
  662. }
  663. static int st_output_switch_put(struct snd_kcontrol *ctl,
  664. struct snd_ctl_elem_value *value)
  665. {
  666. struct oxygen *chip = ctl->private_data;
  667. struct xonar_pcm179x *data = chip->model_data;
  668. u16 gpio_old, gpio;
  669. mutex_lock(&chip->mutex);
  670. gpio_old = oxygen_read16(chip, OXYGEN_GPIO_DATA);
  671. gpio = gpio_old;
  672. switch (value->value.enumerated.item[0]) {
  673. case 0:
  674. gpio &= ~(GPIO_ST_HP | GPIO_ST_HP_REAR);
  675. break;
  676. case 1:
  677. gpio |= GPIO_ST_HP | GPIO_ST_HP_REAR;
  678. break;
  679. case 2:
  680. gpio = (gpio | GPIO_ST_HP) & ~GPIO_ST_HP_REAR;
  681. break;
  682. }
  683. oxygen_write16(chip, OXYGEN_GPIO_DATA, gpio);
  684. data->hp_active = gpio & GPIO_ST_HP;
  685. update_pcm1796_volume(chip);
  686. mutex_unlock(&chip->mutex);
  687. return gpio != gpio_old;
  688. }
  689. static int st_hp_volume_offset_info(struct snd_kcontrol *ctl,
  690. struct snd_ctl_elem_info *info)
  691. {
  692. static const char *const names[3] = {
  693. "< 64 ohms", "64-300 ohms", "300-600 ohms"
  694. };
  695. return snd_ctl_enum_info(info, 1, 3, names);
  696. }
  697. static int st_hp_volume_offset_get(struct snd_kcontrol *ctl,
  698. struct snd_ctl_elem_value *value)
  699. {
  700. struct oxygen *chip = ctl->private_data;
  701. struct xonar_pcm179x *data = chip->model_data;
  702. mutex_lock(&chip->mutex);
  703. if (data->hp_gain_offset < 2*-6)
  704. value->value.enumerated.item[0] = 0;
  705. else if (data->hp_gain_offset < 0)
  706. value->value.enumerated.item[0] = 1;
  707. else
  708. value->value.enumerated.item[0] = 2;
  709. mutex_unlock(&chip->mutex);
  710. return 0;
  711. }
  712. static int st_hp_volume_offset_put(struct snd_kcontrol *ctl,
  713. struct snd_ctl_elem_value *value)
  714. {
  715. static const s8 offsets[] = { 2*-18, 2*-6, 0 };
  716. struct oxygen *chip = ctl->private_data;
  717. struct xonar_pcm179x *data = chip->model_data;
  718. s8 offset;
  719. int changed;
  720. if (value->value.enumerated.item[0] > 2)
  721. return -EINVAL;
  722. offset = offsets[value->value.enumerated.item[0]];
  723. mutex_lock(&chip->mutex);
  724. changed = offset != data->hp_gain_offset;
  725. if (changed) {
  726. data->hp_gain_offset = offset;
  727. update_pcm1796_volume(chip);
  728. }
  729. mutex_unlock(&chip->mutex);
  730. return changed;
  731. }
  732. static const struct snd_kcontrol_new st_controls[] = {
  733. {
  734. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  735. .name = "Analog Output",
  736. .info = st_output_switch_info,
  737. .get = st_output_switch_get,
  738. .put = st_output_switch_put,
  739. },
  740. {
  741. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  742. .name = "Headphones Impedance Playback Enum",
  743. .info = st_hp_volume_offset_info,
  744. .get = st_hp_volume_offset_get,
  745. .put = st_hp_volume_offset_put,
  746. },
  747. };
  748. static void xonar_line_mic_ac97_switch(struct oxygen *chip,
  749. unsigned int reg, unsigned int mute)
  750. {
  751. if (reg == AC97_LINE) {
  752. spin_lock_irq(&chip->reg_lock);
  753. oxygen_write16_masked(chip, OXYGEN_GPIO_DATA,
  754. mute ? GPIO_INPUT_ROUTE : 0,
  755. GPIO_INPUT_ROUTE);
  756. spin_unlock_irq(&chip->reg_lock);
  757. }
  758. }
  759. static const DECLARE_TLV_DB_SCALE(pcm1796_db_scale, -6000, 50, 0);
  760. static int xonar_d2_control_filter(struct snd_kcontrol_new *template)
  761. {
  762. if (!strncmp(template->name, "CD Capture ", 11))
  763. /* CD in is actually connected to the video in pin */
  764. template->private_value ^= AC97_CD ^ AC97_VIDEO;
  765. return 0;
  766. }
  767. static int xonar_st_h6_control_filter(struct snd_kcontrol_new *template)
  768. {
  769. if (!strncmp(template->name, "Master Playback ", 16))
  770. /* no volume/mute, as I²C to the third DAC does not work */
  771. return 1;
  772. return 0;
  773. }
  774. static int add_pcm1796_controls(struct oxygen *chip)
  775. {
  776. struct xonar_pcm179x *data = chip->model_data;
  777. int err;
  778. if (!data->broken_i2c) {
  779. err = snd_ctl_add(chip->card,
  780. snd_ctl_new1(&rolloff_control, chip));
  781. if (err < 0)
  782. return err;
  783. }
  784. return 0;
  785. }
  786. static int xonar_d2_mixer_init(struct oxygen *chip)
  787. {
  788. int err;
  789. err = snd_ctl_add(chip->card, snd_ctl_new1(&alt_switch, chip));
  790. if (err < 0)
  791. return err;
  792. err = add_pcm1796_controls(chip);
  793. if (err < 0)
  794. return err;
  795. return 0;
  796. }
  797. static int xonar_hdav_mixer_init(struct oxygen *chip)
  798. {
  799. int err;
  800. err = snd_ctl_add(chip->card, snd_ctl_new1(&hdav_hdmi_control, chip));
  801. if (err < 0)
  802. return err;
  803. err = add_pcm1796_controls(chip);
  804. if (err < 0)
  805. return err;
  806. return 0;
  807. }
  808. static int xonar_st_mixer_init(struct oxygen *chip)
  809. {
  810. unsigned int i;
  811. int err;
  812. for (i = 0; i < ARRAY_SIZE(st_controls); ++i) {
  813. err = snd_ctl_add(chip->card,
  814. snd_ctl_new1(&st_controls[i], chip));
  815. if (err < 0)
  816. return err;
  817. }
  818. err = add_pcm1796_controls(chip);
  819. if (err < 0)
  820. return err;
  821. return 0;
  822. }
  823. static void dump_pcm1796_registers(struct oxygen *chip,
  824. struct snd_info_buffer *buffer)
  825. {
  826. struct xonar_pcm179x *data = chip->model_data;
  827. unsigned int dac, i;
  828. for (dac = 0; dac < data->dacs; ++dac) {
  829. snd_iprintf(buffer, "\nPCM1796 %u:", dac + 1);
  830. for (i = 0; i < 5; ++i)
  831. snd_iprintf(buffer, " %02x",
  832. data->pcm1796_regs[dac][i]);
  833. }
  834. snd_iprintf(buffer, "\n");
  835. }
  836. static void dump_cs2000_registers(struct oxygen *chip,
  837. struct snd_info_buffer *buffer)
  838. {
  839. struct xonar_pcm179x *data = chip->model_data;
  840. unsigned int i;
  841. if (data->has_cs2000) {
  842. snd_iprintf(buffer, "\nCS2000:\n00: ");
  843. for (i = 1; i < 0x10; ++i)
  844. snd_iprintf(buffer, " %02x", data->cs2000_regs[i]);
  845. snd_iprintf(buffer, "\n10:");
  846. for (i = 0x10; i < 0x1f; ++i)
  847. snd_iprintf(buffer, " %02x", data->cs2000_regs[i]);
  848. snd_iprintf(buffer, "\n");
  849. }
  850. }
  851. static void dump_st_registers(struct oxygen *chip,
  852. struct snd_info_buffer *buffer)
  853. {
  854. dump_pcm1796_registers(chip, buffer);
  855. dump_cs2000_registers(chip, buffer);
  856. }
  857. static const struct oxygen_model model_xonar_d2 = {
  858. .longname = "Asus Virtuoso 200",
  859. .chip = "AV200",
  860. .init = xonar_d2_init,
  861. .control_filter = xonar_d2_control_filter,
  862. .mixer_init = xonar_d2_mixer_init,
  863. .cleanup = xonar_d2_cleanup,
  864. .suspend = xonar_d2_suspend,
  865. .resume = xonar_d2_resume,
  866. .set_dac_params = set_pcm1796_params,
  867. .set_adc_params = xonar_set_cs53x1_params,
  868. .update_dac_volume = update_pcm1796_volume,
  869. .update_dac_mute = update_pcm1796_mute,
  870. .dump_registers = dump_pcm1796_registers,
  871. .dac_tlv = pcm1796_db_scale,
  872. .model_data_size = sizeof(struct xonar_pcm179x),
  873. .device_config = PLAYBACK_0_TO_I2S |
  874. PLAYBACK_1_TO_SPDIF |
  875. CAPTURE_0_FROM_I2S_2 |
  876. CAPTURE_1_FROM_SPDIF |
  877. MIDI_OUTPUT |
  878. MIDI_INPUT |
  879. AC97_CD_INPUT,
  880. .dac_channels_pcm = 8,
  881. .dac_channels_mixer = 8,
  882. .dac_volume_min = 255 - 2*60,
  883. .dac_volume_max = 255,
  884. .misc_flags = OXYGEN_MISC_MIDI,
  885. .function_flags = OXYGEN_FUNCTION_SPI |
  886. OXYGEN_FUNCTION_ENABLE_SPI_4_5,
  887. .dac_mclks = OXYGEN_MCLKS(512, 128, 128),
  888. .adc_mclks = OXYGEN_MCLKS(256, 128, 128),
  889. .dac_i2s_format = OXYGEN_I2S_FORMAT_I2S,
  890. .adc_i2s_format = OXYGEN_I2S_FORMAT_LJUST,
  891. };
  892. static const struct oxygen_model model_xonar_hdav = {
  893. .longname = "Asus Virtuoso 200",
  894. .chip = "AV200",
  895. .init = xonar_hdav_init,
  896. .mixer_init = xonar_hdav_mixer_init,
  897. .cleanup = xonar_hdav_cleanup,
  898. .suspend = xonar_hdav_suspend,
  899. .resume = xonar_hdav_resume,
  900. .pcm_hardware_filter = xonar_hdmi_pcm_hardware_filter,
  901. .set_dac_params = set_hdav_params,
  902. .set_adc_params = xonar_set_cs53x1_params,
  903. .update_dac_volume = update_pcm1796_volume,
  904. .update_dac_mute = update_pcm1796_mute,
  905. .uart_input = xonar_hdmi_uart_input,
  906. .ac97_switch = xonar_line_mic_ac97_switch,
  907. .dump_registers = dump_pcm1796_registers,
  908. .dac_tlv = pcm1796_db_scale,
  909. .model_data_size = sizeof(struct xonar_hdav),
  910. .device_config = PLAYBACK_0_TO_I2S |
  911. PLAYBACK_1_TO_SPDIF |
  912. CAPTURE_0_FROM_I2S_2 |
  913. CAPTURE_1_FROM_SPDIF,
  914. .dac_channels_pcm = 8,
  915. .dac_channels_mixer = 2,
  916. .dac_volume_min = 255 - 2*60,
  917. .dac_volume_max = 255,
  918. .misc_flags = OXYGEN_MISC_MIDI,
  919. .function_flags = OXYGEN_FUNCTION_2WIRE,
  920. .dac_mclks = OXYGEN_MCLKS(512, 128, 128),
  921. .adc_mclks = OXYGEN_MCLKS(256, 128, 128),
  922. .dac_i2s_format = OXYGEN_I2S_FORMAT_I2S,
  923. .adc_i2s_format = OXYGEN_I2S_FORMAT_LJUST,
  924. };
  925. static const struct oxygen_model model_xonar_st = {
  926. .longname = "Asus Virtuoso 100",
  927. .chip = "AV200",
  928. .init = xonar_st_init,
  929. .mixer_init = xonar_st_mixer_init,
  930. .cleanup = xonar_st_cleanup,
  931. .suspend = xonar_st_suspend,
  932. .resume = xonar_st_resume,
  933. .set_dac_params = set_st_params,
  934. .set_adc_params = xonar_set_cs53x1_params,
  935. .update_dac_volume = update_pcm1796_volume,
  936. .update_dac_mute = update_pcm1796_mute,
  937. .ac97_switch = xonar_line_mic_ac97_switch,
  938. .dump_registers = dump_st_registers,
  939. .dac_tlv = pcm1796_db_scale,
  940. .model_data_size = sizeof(struct xonar_pcm179x),
  941. .device_config = PLAYBACK_0_TO_I2S |
  942. PLAYBACK_1_TO_SPDIF |
  943. CAPTURE_0_FROM_I2S_2 |
  944. AC97_FMIC_SWITCH,
  945. .dac_channels_pcm = 2,
  946. .dac_channels_mixer = 2,
  947. .dac_volume_min = 255 - 2*60,
  948. .dac_volume_max = 255,
  949. .function_flags = OXYGEN_FUNCTION_2WIRE,
  950. .dac_mclks = OXYGEN_MCLKS(512, 128, 128),
  951. .adc_mclks = OXYGEN_MCLKS(256, 128, 128),
  952. .dac_i2s_format = OXYGEN_I2S_FORMAT_I2S,
  953. .adc_i2s_format = OXYGEN_I2S_FORMAT_LJUST,
  954. };
  955. int __devinit get_xonar_pcm179x_model(struct oxygen *chip,
  956. const struct pci_device_id *id)
  957. {
  958. switch (id->subdevice) {
  959. case 0x8269:
  960. chip->model = model_xonar_d2;
  961. chip->model.shortname = "Xonar D2";
  962. break;
  963. case 0x82b7:
  964. chip->model = model_xonar_d2;
  965. chip->model.shortname = "Xonar D2X";
  966. chip->model.init = xonar_d2x_init;
  967. break;
  968. case 0x8314:
  969. chip->model = model_xonar_hdav;
  970. oxygen_clear_bits16(chip, OXYGEN_GPIO_CONTROL, GPIO_DB_MASK);
  971. switch (oxygen_read16(chip, OXYGEN_GPIO_DATA) & GPIO_DB_MASK) {
  972. default:
  973. chip->model.shortname = "Xonar HDAV1.3";
  974. break;
  975. case GPIO_DB_H6:
  976. chip->model.shortname = "Xonar HDAV1.3+H6";
  977. chip->model.dac_channels_mixer = 8;
  978. chip->model.dac_mclks = OXYGEN_MCLKS(256, 128, 128);
  979. break;
  980. }
  981. break;
  982. case 0x835d:
  983. chip->model = model_xonar_st;
  984. oxygen_clear_bits16(chip, OXYGEN_GPIO_CONTROL, GPIO_DB_MASK);
  985. switch (oxygen_read16(chip, OXYGEN_GPIO_DATA) & GPIO_DB_MASK) {
  986. default:
  987. chip->model.shortname = "Xonar ST";
  988. break;
  989. case GPIO_DB_H6:
  990. chip->model.shortname = "Xonar ST+H6";
  991. chip->model.control_filter = xonar_st_h6_control_filter;
  992. chip->model.dac_channels_pcm = 8;
  993. chip->model.dac_channels_mixer = 8;
  994. chip->model.dac_volume_min = 255;
  995. chip->model.dac_mclks = OXYGEN_MCLKS(256, 128, 128);
  996. break;
  997. }
  998. break;
  999. case 0x835c:
  1000. chip->model = model_xonar_st;
  1001. chip->model.shortname = "Xonar STX";
  1002. chip->model.init = xonar_stx_init;
  1003. chip->model.resume = xonar_stx_resume;
  1004. chip->model.set_dac_params = set_pcm1796_params;
  1005. break;
  1006. default:
  1007. return -EINVAL;
  1008. }
  1009. return 0;
  1010. }