psc724.c 14 KB

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