psc724.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. /*
  2. * ALSA driver for ICEnsemble VT1724 (Envy24HT)
  3. *
  4. * Lowlevel functions for Philips PSC724 Ultimate Edge
  5. *
  6. * Copyright (c) 2012 Ondrej Zary <linux@rainbow-software.org>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. *
  22. */
  23. #include <linux/delay.h>
  24. #include <linux/init.h>
  25. #include <linux/slab.h>
  26. #include <sound/core.h>
  27. #include "ice1712.h"
  28. #include "envy24ht.h"
  29. #include "psc724.h"
  30. #include "wm8766.h"
  31. #include "wm8776.h"
  32. struct psc724_spec {
  33. struct snd_wm8766 wm8766;
  34. struct snd_wm8776 wm8776;
  35. bool mute_all, jack_detect;
  36. struct snd_ice1712 *ice;
  37. struct delayed_work hp_work;
  38. bool hp_connected;
  39. };
  40. /****************************************************************************/
  41. /* PHILIPS PSC724 ULTIMATE EDGE */
  42. /****************************************************************************/
  43. /*
  44. * VT1722 (Envy24GT) - 6 outputs, 4 inputs (only 2 used), 24-bit/96kHz
  45. *
  46. * system configuration ICE_EEP2_SYSCONF=0x42
  47. * XIN1 49.152MHz
  48. * no MPU401
  49. * one stereo ADC, no S/PDIF receiver
  50. * three stereo DACs (FRONT, REAR, CENTER+LFE)
  51. *
  52. * AC-Link configuration ICE_EEP2_ACLINK=0x80
  53. * use I2S, not AC97
  54. *
  55. * I2S converters feature ICE_EEP2_I2S=0x30
  56. * I2S codec has no volume/mute control feature (bug!)
  57. * I2S codec does not support 96KHz or 192KHz (bug!)
  58. * I2S codec 24bits
  59. *
  60. * S/PDIF configuration ICE_EEP2_SPDIF=0xc1
  61. * Enable integrated S/PDIF transmitter
  62. * internal S/PDIF out implemented
  63. * No S/PDIF input
  64. * External S/PDIF out implemented
  65. *
  66. *
  67. * ** connected chips **
  68. *
  69. * WM8776
  70. * 2-channel DAC used for main output and stereo ADC (with 10-channel MUX)
  71. * AIN1: LINE IN, AIN2: CD/VIDEO, AIN3: AUX, AIN4: Front MIC, AIN5: Rear MIC
  72. * Controlled by I2C using VT1722 I2C interface:
  73. * MODE (pin16) -- GND
  74. * CE (pin17) -- GND I2C mode (address=0x34)
  75. * DI (pin18) -- SDA (VT1722 pin70)
  76. * CL (pin19) -- SCLK (VT1722 pin71)
  77. *
  78. * WM8766
  79. * 6-channel DAC used for rear & center/LFE outputs (only 4 channels used)
  80. * Controlled by SPI using VT1722 GPIO pins:
  81. * MODE (pin 1) -- GPIO19 (VT1722 pin99)
  82. * ML/I2S (pin11) -- GPIO18 (VT1722 pin98)
  83. * MC/IWL (pin12) -- GPIO17 (VT1722 pin97)
  84. * MD/DM (pin13) -- GPIO16 (VT1722 pin96)
  85. * MUTE (pin14) -- GPIO20 (VT1722 pin101)
  86. *
  87. * GPIO14 is used as input for headphone jack detection (1 = connected)
  88. * GPIO22 is used as MUTE ALL output, grounding all 6 channels
  89. *
  90. * ** output pins and device names **
  91. *
  92. * 5.1ch name -- output connector color -- device (-D option)
  93. *
  94. * FRONT 2ch -- green -- plughw:0,0
  95. * CENTER(Lch) SUBWOOFER(Rch) -- orange -- plughw:0,2,0
  96. * REAR 2ch -- black -- plughw:0,2,1
  97. */
  98. /* codec access low-level functions */
  99. #define GPIO_HP_JACK (1 << 14)
  100. #define GPIO_MUTE_SUR (1 << 20)
  101. #define GPIO_MUTE_ALL (1 << 22)
  102. #define JACK_INTERVAL 1000
  103. #define PSC724_SPI_DELAY 1
  104. #define PSC724_SPI_DATA (1 << 16)
  105. #define PSC724_SPI_CLK (1 << 17)
  106. #define PSC724_SPI_LOAD (1 << 18)
  107. #define PSC724_SPI_MASK (PSC724_SPI_DATA | PSC724_SPI_CLK | PSC724_SPI_LOAD)
  108. static void psc724_wm8766_write(struct snd_wm8766 *wm, u16 addr, u16 data)
  109. {
  110. struct psc724_spec *spec = container_of(wm, struct psc724_spec, wm8766);
  111. struct snd_ice1712 *ice = spec->ice;
  112. u32 st, bits;
  113. int i;
  114. snd_ice1712_save_gpio_status(ice);
  115. st = ((addr & 0x7f) << 9) | (data & 0x1ff);
  116. snd_ice1712_gpio_set_dir(ice, ice->gpio.direction | PSC724_SPI_MASK);
  117. snd_ice1712_gpio_set_mask(ice, ice->gpio.write_mask & ~PSC724_SPI_MASK);
  118. bits = snd_ice1712_gpio_read(ice) & ~PSC724_SPI_MASK;
  119. snd_ice1712_gpio_write(ice, bits);
  120. for (i = 0; i < 16; i++) {
  121. udelay(PSC724_SPI_DELAY);
  122. bits &= ~PSC724_SPI_CLK;
  123. /* MSB first */
  124. st <<= 1;
  125. if (st & 0x10000)
  126. bits |= PSC724_SPI_DATA;
  127. else
  128. bits &= ~PSC724_SPI_DATA;
  129. snd_ice1712_gpio_write(ice, bits);
  130. /* CLOCK high */
  131. udelay(PSC724_SPI_DELAY);
  132. bits |= PSC724_SPI_CLK;
  133. snd_ice1712_gpio_write(ice, bits);
  134. }
  135. /* LOAD high */
  136. udelay(PSC724_SPI_DELAY);
  137. bits |= PSC724_SPI_LOAD;
  138. snd_ice1712_gpio_write(ice, bits);
  139. /* LOAD low, DATA and CLOCK high */
  140. udelay(PSC724_SPI_DELAY);
  141. bits |= (PSC724_SPI_DATA | PSC724_SPI_CLK);
  142. snd_ice1712_gpio_write(ice, bits);
  143. snd_ice1712_restore_gpio_status(ice);
  144. }
  145. static void psc724_wm8776_write(struct snd_wm8776 *wm, u8 addr, u8 data)
  146. {
  147. struct psc724_spec *spec = container_of(wm, struct psc724_spec, wm8776);
  148. snd_vt1724_write_i2c(spec->ice, 0x34, addr, data);
  149. }
  150. /* mute all */
  151. static void psc724_set_master_switch(struct snd_ice1712 *ice, bool on)
  152. {
  153. unsigned int bits = snd_ice1712_gpio_read(ice);
  154. struct psc724_spec *spec = ice->spec;
  155. spec->mute_all = !on;
  156. if (on)
  157. bits &= ~(GPIO_MUTE_ALL | GPIO_MUTE_SUR);
  158. else
  159. bits |= GPIO_MUTE_ALL | GPIO_MUTE_SUR;
  160. snd_ice1712_gpio_write(ice, bits);
  161. }
  162. static bool psc724_get_master_switch(struct snd_ice1712 *ice)
  163. {
  164. struct psc724_spec *spec = ice->spec;
  165. return !spec->mute_all;
  166. }
  167. /* jack detection */
  168. static void psc724_set_jack_state(struct snd_ice1712 *ice, bool hp_connected)
  169. {
  170. struct psc724_spec *spec = ice->spec;
  171. struct snd_ctl_elem_id elem_id;
  172. struct snd_kcontrol *kctl;
  173. u16 power = spec->wm8776.regs[WM8776_REG_PWRDOWN] & ~WM8776_PWR_HPPD;
  174. psc724_set_master_switch(ice, !hp_connected);
  175. if (!hp_connected)
  176. power |= WM8776_PWR_HPPD;
  177. snd_wm8776_set_power(&spec->wm8776, power);
  178. spec->hp_connected = hp_connected;
  179. /* notify about master speaker mute change */
  180. memset(&elem_id, 0, sizeof(elem_id));
  181. elem_id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
  182. strlcpy(elem_id.name, "Master Speakers Playback Switch",
  183. sizeof(elem_id.name));
  184. kctl = snd_ctl_find_id(ice->card, &elem_id);
  185. snd_ctl_notify(ice->card, SNDRV_CTL_EVENT_MASK_VALUE, &kctl->id);
  186. /* and headphone mute change */
  187. strlcpy(elem_id.name, spec->wm8776.ctl[WM8776_CTL_HP_SW].name,
  188. sizeof(elem_id.name));
  189. kctl = snd_ctl_find_id(ice->card, &elem_id);
  190. snd_ctl_notify(ice->card, SNDRV_CTL_EVENT_MASK_VALUE, &kctl->id);
  191. }
  192. static void psc724_update_hp_jack_state(struct work_struct *work)
  193. {
  194. struct psc724_spec *spec = container_of(work, struct psc724_spec,
  195. hp_work.work);
  196. struct snd_ice1712 *ice = spec->ice;
  197. bool hp_connected = snd_ice1712_gpio_read(ice) & GPIO_HP_JACK;
  198. schedule_delayed_work(&spec->hp_work, msecs_to_jiffies(JACK_INTERVAL));
  199. if (hp_connected == spec->hp_connected)
  200. return;
  201. psc724_set_jack_state(ice, hp_connected);
  202. }
  203. static void psc724_set_jack_detection(struct snd_ice1712 *ice, bool on)
  204. {
  205. struct psc724_spec *spec = ice->spec;
  206. if (spec->jack_detect == on)
  207. return;
  208. spec->jack_detect = on;
  209. if (on) {
  210. bool hp_connected = snd_ice1712_gpio_read(ice) & GPIO_HP_JACK;
  211. psc724_set_jack_state(ice, hp_connected);
  212. schedule_delayed_work(&spec->hp_work,
  213. msecs_to_jiffies(JACK_INTERVAL));
  214. } else
  215. cancel_delayed_work_sync(&spec->hp_work);
  216. }
  217. static bool psc724_get_jack_detection(struct snd_ice1712 *ice)
  218. {
  219. struct psc724_spec *spec = ice->spec;
  220. return spec->jack_detect;
  221. }
  222. /* mixer controls */
  223. struct psc724_control {
  224. const char *name;
  225. void (*set)(struct snd_ice1712 *ice, bool on);
  226. bool (*get)(struct snd_ice1712 *ice);
  227. };
  228. static const struct psc724_control psc724_cont[] = {
  229. {
  230. .name = "Master Speakers Playback Switch",
  231. .set = psc724_set_master_switch,
  232. .get = psc724_get_master_switch,
  233. },
  234. {
  235. .name = "Headphone Jack Detection Playback Switch",
  236. .set = psc724_set_jack_detection,
  237. .get = psc724_get_jack_detection,
  238. },
  239. };
  240. static int psc724_ctl_get(struct snd_kcontrol *kcontrol,
  241. struct snd_ctl_elem_value *ucontrol)
  242. {
  243. struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
  244. int n = kcontrol->private_value;
  245. ucontrol->value.integer.value[0] = psc724_cont[n].get(ice);
  246. return 0;
  247. }
  248. static int psc724_ctl_put(struct snd_kcontrol *kcontrol,
  249. struct snd_ctl_elem_value *ucontrol)
  250. {
  251. struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
  252. int n = kcontrol->private_value;
  253. psc724_cont[n].set(ice, ucontrol->value.integer.value[0]);
  254. return 0;
  255. }
  256. static const char *front_volume = "Front Playback Volume";
  257. static const char *front_switch = "Front Playback Switch";
  258. static const char *front_zc = "Front Zero Cross Detect Playback Switch";
  259. static const char *front_izd = "Front Infinite Zero Detect Playback Switch";
  260. static const char *front_phase = "Front Phase Invert Playback Switch";
  261. static const char *front_deemph = "Front Deemphasis Playback Switch";
  262. static const char *ain1_switch = "Line Capture Switch";
  263. static const char *ain2_switch = "CD Capture Switch";
  264. static const char *ain3_switch = "AUX Capture Switch";
  265. static const char *ain4_switch = "Front Mic Capture Switch";
  266. static const char *ain5_switch = "Rear Mic Capture Switch";
  267. static const char *rear_volume = "Surround Playback Volume";
  268. static const char *clfe_volume = "CLFE Playback Volume";
  269. static const char *rear_switch = "Surround Playback Switch";
  270. static const char *clfe_switch = "CLFE Playback Switch";
  271. static const char *rear_phase = "Surround Phase Invert Playback Switch";
  272. static const char *clfe_phase = "CLFE Phase Invert Playback Switch";
  273. static const char *rear_deemph = "Surround Deemphasis Playback Switch";
  274. static const char *clfe_deemph = "CLFE Deemphasis Playback Switch";
  275. static const char *rear_clfe_izd = "Rear Infinite Zero Detect Playback Switch";
  276. static const char *rear_clfe_zc = "Rear Zero Cross Detect Playback Switch";
  277. static int psc724_add_controls(struct snd_ice1712 *ice)
  278. {
  279. struct snd_kcontrol_new cont;
  280. struct snd_kcontrol *ctl;
  281. int err, i;
  282. struct psc724_spec *spec = ice->spec;
  283. spec->wm8776.ctl[WM8776_CTL_DAC_VOL].name = front_volume;
  284. spec->wm8776.ctl[WM8776_CTL_DAC_SW].name = front_switch;
  285. spec->wm8776.ctl[WM8776_CTL_DAC_ZC_SW].name = front_zc;
  286. spec->wm8776.ctl[WM8776_CTL_AUX_SW].name = NULL;
  287. spec->wm8776.ctl[WM8776_CTL_DAC_IZD_SW].name = front_izd;
  288. spec->wm8776.ctl[WM8776_CTL_PHASE_SW].name = front_phase;
  289. spec->wm8776.ctl[WM8776_CTL_DEEMPH_SW].name = front_deemph;
  290. spec->wm8776.ctl[WM8776_CTL_INPUT1_SW].name = ain1_switch;
  291. spec->wm8776.ctl[WM8776_CTL_INPUT2_SW].name = ain2_switch;
  292. spec->wm8776.ctl[WM8776_CTL_INPUT3_SW].name = ain3_switch;
  293. spec->wm8776.ctl[WM8776_CTL_INPUT4_SW].name = ain4_switch;
  294. spec->wm8776.ctl[WM8776_CTL_INPUT5_SW].name = ain5_switch;
  295. snd_wm8776_build_controls(&spec->wm8776);
  296. spec->wm8766.ctl[WM8766_CTL_CH1_VOL].name = rear_volume;
  297. spec->wm8766.ctl[WM8766_CTL_CH2_VOL].name = clfe_volume;
  298. spec->wm8766.ctl[WM8766_CTL_CH3_VOL].name = NULL;
  299. spec->wm8766.ctl[WM8766_CTL_CH1_SW].name = rear_switch;
  300. spec->wm8766.ctl[WM8766_CTL_CH2_SW].name = clfe_switch;
  301. spec->wm8766.ctl[WM8766_CTL_CH3_SW].name = NULL;
  302. spec->wm8766.ctl[WM8766_CTL_PHASE1_SW].name = rear_phase;
  303. spec->wm8766.ctl[WM8766_CTL_PHASE2_SW].name = clfe_phase;
  304. spec->wm8766.ctl[WM8766_CTL_PHASE3_SW].name = NULL;
  305. spec->wm8766.ctl[WM8766_CTL_DEEMPH1_SW].name = rear_deemph;
  306. spec->wm8766.ctl[WM8766_CTL_DEEMPH2_SW].name = clfe_deemph;
  307. spec->wm8766.ctl[WM8766_CTL_DEEMPH3_SW].name = NULL;
  308. spec->wm8766.ctl[WM8766_CTL_IZD_SW].name = rear_clfe_izd;
  309. spec->wm8766.ctl[WM8766_CTL_ZC_SW].name = rear_clfe_zc;
  310. snd_wm8766_build_controls(&spec->wm8766);
  311. memset(&cont, 0, sizeof(cont));
  312. cont.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
  313. for (i = 0; i < ARRAY_SIZE(psc724_cont); i++) {
  314. cont.private_value = i;
  315. cont.name = psc724_cont[i].name;
  316. cont.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
  317. cont.info = snd_ctl_boolean_mono_info;
  318. cont.get = psc724_ctl_get;
  319. cont.put = psc724_ctl_put;
  320. ctl = snd_ctl_new1(&cont, ice);
  321. if (!ctl)
  322. return -ENOMEM;
  323. err = snd_ctl_add(ice->card, ctl);
  324. if (err < 0)
  325. return err;
  326. }
  327. return 0;
  328. }
  329. static void psc724_set_pro_rate(struct snd_ice1712 *ice, unsigned int rate)
  330. {
  331. struct psc724_spec *spec = ice->spec;
  332. /* restore codec volume settings after rate change (PMCLK stop) */
  333. snd_wm8776_volume_restore(&spec->wm8776);
  334. snd_wm8766_volume_restore(&spec->wm8766);
  335. }
  336. /* power management */
  337. #ifdef CONFIG_PM_SLEEP
  338. static int psc724_resume(struct snd_ice1712 *ice)
  339. {
  340. struct psc724_spec *spec = ice->spec;
  341. snd_wm8776_resume(&spec->wm8776);
  342. snd_wm8766_resume(&spec->wm8766);
  343. return 0;
  344. }
  345. #endif
  346. /* init */
  347. static int psc724_init(struct snd_ice1712 *ice)
  348. {
  349. struct psc724_spec *spec;
  350. spec = kzalloc(sizeof(*spec), GFP_KERNEL);
  351. if (!spec)
  352. return -ENOMEM;
  353. ice->spec = spec;
  354. spec->ice = ice;
  355. ice->num_total_dacs = 6;
  356. ice->num_total_adcs = 2;
  357. spec->wm8776.ops.write = psc724_wm8776_write;
  358. spec->wm8776.card = ice->card;
  359. snd_wm8776_init(&spec->wm8776);
  360. spec->wm8766.ops.write = psc724_wm8766_write;
  361. spec->wm8766.card = ice->card;
  362. #ifdef CONFIG_PM_SLEEP
  363. ice->pm_resume = psc724_resume;
  364. ice->pm_suspend_enabled = 1;
  365. #endif
  366. snd_wm8766_init(&spec->wm8766);
  367. snd_wm8766_set_if(&spec->wm8766,
  368. WM8766_IF_FMT_I2S | WM8766_IF_IWL_24BIT);
  369. ice->gpio.set_pro_rate = psc724_set_pro_rate;
  370. INIT_DELAYED_WORK(&spec->hp_work, psc724_update_hp_jack_state);
  371. psc724_set_jack_detection(ice, true);
  372. return 0;
  373. }
  374. static void psc724_exit(struct snd_ice1712 *ice)
  375. {
  376. struct psc724_spec *spec = ice->spec;
  377. cancel_delayed_work_sync(&spec->hp_work);
  378. }
  379. /* PSC724 has buggy EEPROM (no 96&192kHz, all FFh GPIOs), so override it here */
  380. static unsigned char psc724_eeprom[] = {
  381. [ICE_EEP2_SYSCONF] = 0x42, /* 49.152MHz, 1 ADC, 3 DACs */
  382. [ICE_EEP2_ACLINK] = 0x80, /* I2S */
  383. [ICE_EEP2_I2S] = 0xf0, /* I2S volume, 96kHz, 24bit */
  384. [ICE_EEP2_SPDIF] = 0xc1, /* spdif out-en, out-int, no input */
  385. /* GPIO outputs */
  386. [ICE_EEP2_GPIO_DIR2] = 0x5f, /* MUTE_ALL,WM8766 MUTE/MODE/ML/MC/MD */
  387. /* GPIO write enable */
  388. [ICE_EEP2_GPIO_MASK] = 0xff, /* read-only */
  389. [ICE_EEP2_GPIO_MASK1] = 0xff, /* read-only */
  390. [ICE_EEP2_GPIO_MASK2] = 0xa0, /* MUTE_ALL,WM8766 MUTE/MODE/ML/MC/MD */
  391. /* GPIO initial state */
  392. [ICE_EEP2_GPIO_STATE2] = 0x20, /* unmuted, all WM8766 pins low */
  393. };
  394. struct snd_ice1712_card_info snd_vt1724_psc724_cards[] = {
  395. {
  396. .subvendor = VT1724_SUBDEVICE_PSC724,
  397. .name = "Philips PSC724 Ultimate Edge",
  398. .model = "psc724",
  399. .chip_init = psc724_init,
  400. .chip_exit = psc724_exit,
  401. .build_controls = psc724_add_controls,
  402. .eeprom_size = sizeof(psc724_eeprom),
  403. .eeprom_data = psc724_eeprom,
  404. },
  405. {} /*terminator*/
  406. };