burgundy.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. /*
  2. * PMac Burgundy lowlevel functions
  3. *
  4. * Copyright (c) by Takashi Iwai <tiwai@suse.de>
  5. * code based on dmasound.c.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. #include <sound/driver.h>
  22. #include <asm/io.h>
  23. #include <linux/init.h>
  24. #include <linux/slab.h>
  25. #include <linux/delay.h>
  26. #include <sound/core.h>
  27. #include "pmac.h"
  28. #include "burgundy.h"
  29. /* Waits for busy flag to clear */
  30. static inline void
  31. snd_pmac_burgundy_busy_wait(pmac_t *chip)
  32. {
  33. int timeout = 50;
  34. while ((in_le32(&chip->awacs->codec_ctrl) & MASK_NEWECMD) && timeout--)
  35. udelay(1);
  36. if (! timeout)
  37. printk(KERN_DEBUG "burgundy_busy_wait: timeout\n");
  38. }
  39. static inline void
  40. snd_pmac_burgundy_extend_wait(pmac_t *chip)
  41. {
  42. int timeout;
  43. timeout = 50;
  44. while (!(in_le32(&chip->awacs->codec_stat) & MASK_EXTEND) && timeout--)
  45. udelay(1);
  46. if (! timeout)
  47. printk(KERN_DEBUG "burgundy_extend_wait: timeout #1\n");
  48. timeout = 50;
  49. while ((in_le32(&chip->awacs->codec_stat) & MASK_EXTEND) && timeout--)
  50. udelay(1);
  51. if (! timeout)
  52. printk(KERN_DEBUG "burgundy_extend_wait: timeout #2\n");
  53. }
  54. static void
  55. snd_pmac_burgundy_wcw(pmac_t *chip, unsigned addr, unsigned val)
  56. {
  57. out_le32(&chip->awacs->codec_ctrl, addr + 0x200c00 + (val & 0xff));
  58. snd_pmac_burgundy_busy_wait(chip);
  59. out_le32(&chip->awacs->codec_ctrl, addr + 0x200d00 +((val>>8) & 0xff));
  60. snd_pmac_burgundy_busy_wait(chip);
  61. out_le32(&chip->awacs->codec_ctrl, addr + 0x200e00 +((val>>16) & 0xff));
  62. snd_pmac_burgundy_busy_wait(chip);
  63. out_le32(&chip->awacs->codec_ctrl, addr + 0x200f00 +((val>>24) & 0xff));
  64. snd_pmac_burgundy_busy_wait(chip);
  65. }
  66. static unsigned
  67. snd_pmac_burgundy_rcw(pmac_t *chip, unsigned addr)
  68. {
  69. unsigned val = 0;
  70. unsigned long flags;
  71. spin_lock_irqsave(&chip->reg_lock, flags);
  72. out_le32(&chip->awacs->codec_ctrl, addr + 0x100000);
  73. snd_pmac_burgundy_busy_wait(chip);
  74. snd_pmac_burgundy_extend_wait(chip);
  75. val += (in_le32(&chip->awacs->codec_stat) >> 4) & 0xff;
  76. out_le32(&chip->awacs->codec_ctrl, addr + 0x100100);
  77. snd_pmac_burgundy_busy_wait(chip);
  78. snd_pmac_burgundy_extend_wait(chip);
  79. val += ((in_le32(&chip->awacs->codec_stat)>>4) & 0xff) <<8;
  80. out_le32(&chip->awacs->codec_ctrl, addr + 0x100200);
  81. snd_pmac_burgundy_busy_wait(chip);
  82. snd_pmac_burgundy_extend_wait(chip);
  83. val += ((in_le32(&chip->awacs->codec_stat)>>4) & 0xff) <<16;
  84. out_le32(&chip->awacs->codec_ctrl, addr + 0x100300);
  85. snd_pmac_burgundy_busy_wait(chip);
  86. snd_pmac_burgundy_extend_wait(chip);
  87. val += ((in_le32(&chip->awacs->codec_stat)>>4) & 0xff) <<24;
  88. spin_unlock_irqrestore(&chip->reg_lock, flags);
  89. return val;
  90. }
  91. static void
  92. snd_pmac_burgundy_wcb(pmac_t *chip, unsigned int addr, unsigned int val)
  93. {
  94. out_le32(&chip->awacs->codec_ctrl, addr + 0x300000 + (val & 0xff));
  95. snd_pmac_burgundy_busy_wait(chip);
  96. }
  97. static unsigned
  98. snd_pmac_burgundy_rcb(pmac_t *chip, unsigned int addr)
  99. {
  100. unsigned val = 0;
  101. unsigned long flags;
  102. spin_lock_irqsave(&chip->reg_lock, flags);
  103. out_le32(&chip->awacs->codec_ctrl, addr + 0x100000);
  104. snd_pmac_burgundy_busy_wait(chip);
  105. snd_pmac_burgundy_extend_wait(chip);
  106. val += (in_le32(&chip->awacs->codec_stat) >> 4) & 0xff;
  107. spin_unlock_irqrestore(&chip->reg_lock, flags);
  108. return val;
  109. }
  110. /*
  111. * Burgundy volume: 0 - 100, stereo
  112. */
  113. static void
  114. snd_pmac_burgundy_write_volume(pmac_t *chip, unsigned int address, long *volume, int shift)
  115. {
  116. int hardvolume, lvolume, rvolume;
  117. lvolume = volume[0] ? volume[0] + BURGUNDY_VOLUME_OFFSET : 0;
  118. rvolume = volume[1] ? volume[1] + BURGUNDY_VOLUME_OFFSET : 0;
  119. hardvolume = lvolume + (rvolume << shift);
  120. if (shift == 8)
  121. hardvolume |= hardvolume << 16;
  122. snd_pmac_burgundy_wcw(chip, address, hardvolume);
  123. }
  124. static void
  125. snd_pmac_burgundy_read_volume(pmac_t *chip, unsigned int address, long *volume, int shift)
  126. {
  127. int wvolume;
  128. wvolume = snd_pmac_burgundy_rcw(chip, address);
  129. volume[0] = wvolume & 0xff;
  130. if (volume[0] >= BURGUNDY_VOLUME_OFFSET)
  131. volume[0] -= BURGUNDY_VOLUME_OFFSET;
  132. else
  133. volume[0] = 0;
  134. volume[1] = (wvolume >> shift) & 0xff;
  135. if (volume[1] >= BURGUNDY_VOLUME_OFFSET)
  136. volume[1] -= BURGUNDY_VOLUME_OFFSET;
  137. else
  138. volume[1] = 0;
  139. }
  140. /*
  141. */
  142. #define BASE2ADDR(base) ((base) << 12)
  143. #define ADDR2BASE(addr) ((addr) >> 12)
  144. static int snd_pmac_burgundy_info_volume(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
  145. {
  146. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  147. uinfo->count = 2;
  148. uinfo->value.integer.min = 0;
  149. uinfo->value.integer.max = 100;
  150. return 0;
  151. }
  152. static int snd_pmac_burgundy_get_volume(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
  153. {
  154. pmac_t *chip = snd_kcontrol_chip(kcontrol);
  155. unsigned int addr = BASE2ADDR(kcontrol->private_value & 0xff);
  156. int shift = (kcontrol->private_value >> 8) & 0xff;
  157. snd_pmac_burgundy_read_volume(chip, addr, ucontrol->value.integer.value, shift);
  158. return 0;
  159. }
  160. static int snd_pmac_burgundy_put_volume(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
  161. {
  162. pmac_t *chip = snd_kcontrol_chip(kcontrol);
  163. unsigned int addr = BASE2ADDR(kcontrol->private_value & 0xff);
  164. int shift = (kcontrol->private_value >> 8) & 0xff;
  165. long nvoices[2];
  166. snd_pmac_burgundy_write_volume(chip, addr, ucontrol->value.integer.value, shift);
  167. snd_pmac_burgundy_read_volume(chip, addr, nvoices, shift);
  168. return (nvoices[0] != ucontrol->value.integer.value[0] ||
  169. nvoices[1] != ucontrol->value.integer.value[1]);
  170. }
  171. #define BURGUNDY_VOLUME(xname, xindex, addr, shift) \
  172. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex,\
  173. .info = snd_pmac_burgundy_info_volume,\
  174. .get = snd_pmac_burgundy_get_volume,\
  175. .put = snd_pmac_burgundy_put_volume,\
  176. .private_value = ((ADDR2BASE(addr) & 0xff) | ((shift) << 8)) }
  177. /* lineout/speaker */
  178. static int snd_pmac_burgundy_info_switch_out(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
  179. {
  180. int stereo = (kcontrol->private_value >> 24) & 1;
  181. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  182. uinfo->count = stereo + 1;
  183. uinfo->value.integer.min = 0;
  184. uinfo->value.integer.max = 1;
  185. return 0;
  186. }
  187. static int snd_pmac_burgundy_get_switch_out(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
  188. {
  189. pmac_t *chip = snd_kcontrol_chip(kcontrol);
  190. int lmask = kcontrol->private_value & 0xff;
  191. int rmask = (kcontrol->private_value >> 8) & 0xff;
  192. int stereo = (kcontrol->private_value >> 24) & 1;
  193. int val = snd_pmac_burgundy_rcb(chip, MASK_ADDR_BURGUNDY_MORE_OUTPUTENABLES);
  194. ucontrol->value.integer.value[0] = (val & lmask) ? 1 : 0;
  195. if (stereo)
  196. ucontrol->value.integer.value[1] = (val & rmask) ? 1 : 0;
  197. return 0;
  198. }
  199. static int snd_pmac_burgundy_put_switch_out(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
  200. {
  201. pmac_t *chip = snd_kcontrol_chip(kcontrol);
  202. int lmask = kcontrol->private_value & 0xff;
  203. int rmask = (kcontrol->private_value >> 8) & 0xff;
  204. int stereo = (kcontrol->private_value >> 24) & 1;
  205. int val, oval;
  206. oval = snd_pmac_burgundy_rcb(chip, MASK_ADDR_BURGUNDY_MORE_OUTPUTENABLES);
  207. val = oval & ~(lmask | rmask);
  208. if (ucontrol->value.integer.value[0])
  209. val |= lmask;
  210. if (stereo && ucontrol->value.integer.value[1])
  211. val |= rmask;
  212. snd_pmac_burgundy_wcb(chip, MASK_ADDR_BURGUNDY_MORE_OUTPUTENABLES, val);
  213. return val != oval;
  214. }
  215. #define BURGUNDY_OUTPUT_SWITCH(xname, xindex, lmask, rmask, stereo) \
  216. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex,\
  217. .info = snd_pmac_burgundy_info_switch_out,\
  218. .get = snd_pmac_burgundy_get_switch_out,\
  219. .put = snd_pmac_burgundy_put_switch_out,\
  220. .private_value = ((lmask) | ((rmask) << 8) | ((stereo) << 24)) }
  221. /* line/speaker output volume */
  222. static int snd_pmac_burgundy_info_volume_out(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
  223. {
  224. int stereo = (kcontrol->private_value >> 24) & 1;
  225. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  226. uinfo->count = stereo + 1;
  227. uinfo->value.integer.min = 0;
  228. uinfo->value.integer.max = 15;
  229. return 0;
  230. }
  231. static int snd_pmac_burgundy_get_volume_out(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
  232. {
  233. pmac_t *chip = snd_kcontrol_chip(kcontrol);
  234. unsigned int addr = BASE2ADDR(kcontrol->private_value & 0xff);
  235. int stereo = (kcontrol->private_value >> 24) & 1;
  236. int oval;
  237. oval = ~snd_pmac_burgundy_rcb(chip, addr) & 0xff;
  238. ucontrol->value.integer.value[0] = oval & 0xf;
  239. if (stereo)
  240. ucontrol->value.integer.value[1] = (oval >> 4) & 0xf;
  241. return 0;
  242. }
  243. static int snd_pmac_burgundy_put_volume_out(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
  244. {
  245. pmac_t *chip = snd_kcontrol_chip(kcontrol);
  246. unsigned int addr = BASE2ADDR(kcontrol->private_value & 0xff);
  247. int stereo = (kcontrol->private_value >> 24) & 1;
  248. int oval, val;
  249. oval = ~snd_pmac_burgundy_rcb(chip, addr) & 0xff;
  250. val = ucontrol->value.integer.value[0];
  251. if (stereo)
  252. val |= ucontrol->value.integer.value[1] << 4;
  253. else
  254. val |= ucontrol->value.integer.value[0] << 4;
  255. val = ~val & 0xff;
  256. snd_pmac_burgundy_wcb(chip, addr, val);
  257. return val != oval;
  258. }
  259. #define BURGUNDY_OUTPUT_VOLUME(xname, xindex, addr, stereo) \
  260. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex,\
  261. .info = snd_pmac_burgundy_info_volume_out,\
  262. .get = snd_pmac_burgundy_get_volume_out,\
  263. .put = snd_pmac_burgundy_put_volume_out,\
  264. .private_value = (ADDR2BASE(addr) | ((stereo) << 24)) }
  265. static snd_kcontrol_new_t snd_pmac_burgundy_mixers[] __initdata = {
  266. BURGUNDY_VOLUME("Master Playback Volume", 0, MASK_ADDR_BURGUNDY_MASTER_VOLUME, 8),
  267. BURGUNDY_VOLUME("Line Playback Volume", 0, MASK_ADDR_BURGUNDY_VOLLINE, 16),
  268. BURGUNDY_VOLUME("CD Playback Volume", 0, MASK_ADDR_BURGUNDY_VOLCD, 16),
  269. BURGUNDY_VOLUME("Mic Playback Volume", 0, MASK_ADDR_BURGUNDY_VOLMIC, 16),
  270. BURGUNDY_OUTPUT_VOLUME("PC Speaker Playback Volume", 0, MASK_ADDR_BURGUNDY_ATTENHP, 0),
  271. /*BURGUNDY_OUTPUT_VOLUME("PCM Playback Volume", 0, MASK_ADDR_BURGUNDY_ATTENLINEOUT, 1),*/
  272. BURGUNDY_OUTPUT_VOLUME("Headphone Playback Volume", 0, MASK_ADDR_BURGUNDY_ATTENSPEAKER, 1),
  273. };
  274. static snd_kcontrol_new_t snd_pmac_burgundy_master_sw __initdata =
  275. BURGUNDY_OUTPUT_SWITCH("Headphone Playback Switch", 0, BURGUNDY_OUTPUT_LEFT, BURGUNDY_OUTPUT_RIGHT, 1);
  276. static snd_kcontrol_new_t snd_pmac_burgundy_speaker_sw __initdata =
  277. BURGUNDY_OUTPUT_SWITCH("PC Speaker Playback Switch", 0, BURGUNDY_OUTPUT_INTERN, 0, 0);
  278. #ifdef PMAC_SUPPORT_AUTOMUTE
  279. /*
  280. * auto-mute stuffs
  281. */
  282. static int snd_pmac_burgundy_detect_headphone(pmac_t *chip)
  283. {
  284. return (in_le32(&chip->awacs->codec_stat) & chip->hp_stat_mask) ? 1 : 0;
  285. }
  286. static void snd_pmac_burgundy_update_automute(pmac_t *chip, int do_notify)
  287. {
  288. if (chip->auto_mute) {
  289. int reg, oreg;
  290. reg = oreg = snd_pmac_burgundy_rcb(chip, MASK_ADDR_BURGUNDY_MORE_OUTPUTENABLES);
  291. reg &= ~(BURGUNDY_OUTPUT_LEFT | BURGUNDY_OUTPUT_RIGHT | BURGUNDY_OUTPUT_INTERN);
  292. if (snd_pmac_burgundy_detect_headphone(chip))
  293. reg |= BURGUNDY_OUTPUT_LEFT | BURGUNDY_OUTPUT_RIGHT;
  294. else
  295. reg |= BURGUNDY_OUTPUT_INTERN;
  296. if (do_notify && reg == oreg)
  297. return;
  298. snd_pmac_burgundy_wcb(chip, MASK_ADDR_BURGUNDY_MORE_OUTPUTENABLES, reg);
  299. if (do_notify) {
  300. snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
  301. &chip->master_sw_ctl->id);
  302. snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
  303. &chip->speaker_sw_ctl->id);
  304. snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
  305. &chip->hp_detect_ctl->id);
  306. }
  307. }
  308. }
  309. #endif /* PMAC_SUPPORT_AUTOMUTE */
  310. /*
  311. * initialize burgundy
  312. */
  313. int __init snd_pmac_burgundy_init(pmac_t *chip)
  314. {
  315. int i, err;
  316. /* Checks to see the chip is alive and kicking */
  317. if ((in_le32(&chip->awacs->codec_ctrl) & MASK_ERRCODE) == 0xf0000) {
  318. printk(KERN_WARNING "pmac burgundy: disabled by MacOS :-(\n");
  319. return 1;
  320. }
  321. snd_pmac_burgundy_wcb(chip, MASK_ADDR_BURGUNDY_OUTPUTENABLES,
  322. DEF_BURGUNDY_OUTPUTENABLES);
  323. snd_pmac_burgundy_wcb(chip, MASK_ADDR_BURGUNDY_MORE_OUTPUTENABLES,
  324. DEF_BURGUNDY_MORE_OUTPUTENABLES);
  325. snd_pmac_burgundy_wcw(chip, MASK_ADDR_BURGUNDY_OUTPUTSELECTS,
  326. DEF_BURGUNDY_OUTPUTSELECTS);
  327. snd_pmac_burgundy_wcb(chip, MASK_ADDR_BURGUNDY_INPSEL21,
  328. DEF_BURGUNDY_INPSEL21);
  329. snd_pmac_burgundy_wcb(chip, MASK_ADDR_BURGUNDY_INPSEL3,
  330. DEF_BURGUNDY_INPSEL3);
  331. snd_pmac_burgundy_wcb(chip, MASK_ADDR_BURGUNDY_GAINCD,
  332. DEF_BURGUNDY_GAINCD);
  333. snd_pmac_burgundy_wcb(chip, MASK_ADDR_BURGUNDY_GAINLINE,
  334. DEF_BURGUNDY_GAINLINE);
  335. snd_pmac_burgundy_wcb(chip, MASK_ADDR_BURGUNDY_GAINMIC,
  336. DEF_BURGUNDY_GAINMIC);
  337. snd_pmac_burgundy_wcb(chip, MASK_ADDR_BURGUNDY_GAINMODEM,
  338. DEF_BURGUNDY_GAINMODEM);
  339. snd_pmac_burgundy_wcb(chip, MASK_ADDR_BURGUNDY_ATTENSPEAKER,
  340. DEF_BURGUNDY_ATTENSPEAKER);
  341. snd_pmac_burgundy_wcb(chip, MASK_ADDR_BURGUNDY_ATTENLINEOUT,
  342. DEF_BURGUNDY_ATTENLINEOUT);
  343. snd_pmac_burgundy_wcb(chip, MASK_ADDR_BURGUNDY_ATTENHP,
  344. DEF_BURGUNDY_ATTENHP);
  345. snd_pmac_burgundy_wcw(chip, MASK_ADDR_BURGUNDY_MASTER_VOLUME,
  346. DEF_BURGUNDY_MASTER_VOLUME);
  347. snd_pmac_burgundy_wcw(chip, MASK_ADDR_BURGUNDY_VOLCD,
  348. DEF_BURGUNDY_VOLCD);
  349. snd_pmac_burgundy_wcw(chip, MASK_ADDR_BURGUNDY_VOLLINE,
  350. DEF_BURGUNDY_VOLLINE);
  351. snd_pmac_burgundy_wcw(chip, MASK_ADDR_BURGUNDY_VOLMIC,
  352. DEF_BURGUNDY_VOLMIC);
  353. if (chip->hp_stat_mask == 0)
  354. /* set headphone-jack detection bit */
  355. chip->hp_stat_mask = 0x04;
  356. /*
  357. * build burgundy mixers
  358. */
  359. strcpy(chip->card->mixername, "PowerMac Burgundy");
  360. for (i = 0; i < ARRAY_SIZE(snd_pmac_burgundy_mixers); i++) {
  361. if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_pmac_burgundy_mixers[i], chip))) < 0)
  362. return err;
  363. }
  364. chip->master_sw_ctl = snd_ctl_new1(&snd_pmac_burgundy_master_sw, chip);
  365. if ((err = snd_ctl_add(chip->card, chip->master_sw_ctl)) < 0)
  366. return err;
  367. chip->speaker_sw_ctl = snd_ctl_new1(&snd_pmac_burgundy_speaker_sw, chip);
  368. if ((err = snd_ctl_add(chip->card, chip->speaker_sw_ctl)) < 0)
  369. return err;
  370. #ifdef PMAC_SUPPORT_AUTOMUTE
  371. if ((err = snd_pmac_add_automute(chip)) < 0)
  372. return err;
  373. chip->detect_headphone = snd_pmac_burgundy_detect_headphone;
  374. chip->update_automute = snd_pmac_burgundy_update_automute;
  375. snd_pmac_burgundy_update_automute(chip, 0); /* update the status only */
  376. #endif
  377. return 0;
  378. }