burgundy.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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(struct snd_pmac *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(struct snd_pmac *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(struct snd_pmac *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(struct snd_pmac *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(struct snd_pmac *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(struct snd_pmac *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(struct snd_pmac *chip, unsigned int address,
  115. long *volume, int shift)
  116. {
  117. int hardvolume, lvolume, rvolume;
  118. lvolume = volume[0] ? volume[0] + BURGUNDY_VOLUME_OFFSET : 0;
  119. rvolume = volume[1] ? volume[1] + BURGUNDY_VOLUME_OFFSET : 0;
  120. hardvolume = lvolume + (rvolume << shift);
  121. if (shift == 8)
  122. hardvolume |= hardvolume << 16;
  123. snd_pmac_burgundy_wcw(chip, address, hardvolume);
  124. }
  125. static void
  126. snd_pmac_burgundy_read_volume(struct snd_pmac *chip, unsigned int address,
  127. long *volume, int shift)
  128. {
  129. int wvolume;
  130. wvolume = snd_pmac_burgundy_rcw(chip, address);
  131. volume[0] = wvolume & 0xff;
  132. if (volume[0] >= BURGUNDY_VOLUME_OFFSET)
  133. volume[0] -= BURGUNDY_VOLUME_OFFSET;
  134. else
  135. volume[0] = 0;
  136. volume[1] = (wvolume >> shift) & 0xff;
  137. if (volume[1] >= BURGUNDY_VOLUME_OFFSET)
  138. volume[1] -= BURGUNDY_VOLUME_OFFSET;
  139. else
  140. volume[1] = 0;
  141. }
  142. /*
  143. */
  144. #define BASE2ADDR(base) ((base) << 12)
  145. #define ADDR2BASE(addr) ((addr) >> 12)
  146. static int snd_pmac_burgundy_info_volume(struct snd_kcontrol *kcontrol,
  147. struct snd_ctl_elem_info *uinfo)
  148. {
  149. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  150. uinfo->count = 2;
  151. uinfo->value.integer.min = 0;
  152. uinfo->value.integer.max = 100;
  153. return 0;
  154. }
  155. static int snd_pmac_burgundy_get_volume(struct snd_kcontrol *kcontrol,
  156. struct snd_ctl_elem_value *ucontrol)
  157. {
  158. struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
  159. unsigned int addr = BASE2ADDR(kcontrol->private_value & 0xff);
  160. int shift = (kcontrol->private_value >> 8) & 0xff;
  161. snd_pmac_burgundy_read_volume(chip, addr, ucontrol->value.integer.value,
  162. shift);
  163. return 0;
  164. }
  165. static int snd_pmac_burgundy_put_volume(struct snd_kcontrol *kcontrol,
  166. struct snd_ctl_elem_value *ucontrol)
  167. {
  168. struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
  169. unsigned int addr = BASE2ADDR(kcontrol->private_value & 0xff);
  170. int shift = (kcontrol->private_value >> 8) & 0xff;
  171. long nvoices[2];
  172. snd_pmac_burgundy_write_volume(chip, addr, ucontrol->value.integer.value,
  173. shift);
  174. snd_pmac_burgundy_read_volume(chip, addr, nvoices, shift);
  175. return (nvoices[0] != ucontrol->value.integer.value[0] ||
  176. nvoices[1] != ucontrol->value.integer.value[1]);
  177. }
  178. #define BURGUNDY_VOLUME(xname, xindex, addr, shift) \
  179. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex,\
  180. .info = snd_pmac_burgundy_info_volume,\
  181. .get = snd_pmac_burgundy_get_volume,\
  182. .put = snd_pmac_burgundy_put_volume,\
  183. .private_value = ((ADDR2BASE(addr) & 0xff) | ((shift) << 8)) }
  184. /* lineout/speaker */
  185. static int snd_pmac_burgundy_info_switch_out(struct snd_kcontrol *kcontrol,
  186. struct snd_ctl_elem_info *uinfo)
  187. {
  188. int stereo = (kcontrol->private_value >> 24) & 1;
  189. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  190. uinfo->count = stereo + 1;
  191. uinfo->value.integer.min = 0;
  192. uinfo->value.integer.max = 1;
  193. return 0;
  194. }
  195. static int snd_pmac_burgundy_get_switch_out(struct snd_kcontrol *kcontrol,
  196. struct snd_ctl_elem_value *ucontrol)
  197. {
  198. struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
  199. int lmask = kcontrol->private_value & 0xff;
  200. int rmask = (kcontrol->private_value >> 8) & 0xff;
  201. int stereo = (kcontrol->private_value >> 24) & 1;
  202. int val = snd_pmac_burgundy_rcb(chip, MASK_ADDR_BURGUNDY_MORE_OUTPUTENABLES);
  203. ucontrol->value.integer.value[0] = (val & lmask) ? 1 : 0;
  204. if (stereo)
  205. ucontrol->value.integer.value[1] = (val & rmask) ? 1 : 0;
  206. return 0;
  207. }
  208. static int snd_pmac_burgundy_put_switch_out(struct snd_kcontrol *kcontrol,
  209. struct snd_ctl_elem_value *ucontrol)
  210. {
  211. struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
  212. int lmask = kcontrol->private_value & 0xff;
  213. int rmask = (kcontrol->private_value >> 8) & 0xff;
  214. int stereo = (kcontrol->private_value >> 24) & 1;
  215. int val, oval;
  216. oval = snd_pmac_burgundy_rcb(chip, MASK_ADDR_BURGUNDY_MORE_OUTPUTENABLES);
  217. val = oval & ~(lmask | rmask);
  218. if (ucontrol->value.integer.value[0])
  219. val |= lmask;
  220. if (stereo && ucontrol->value.integer.value[1])
  221. val |= rmask;
  222. snd_pmac_burgundy_wcb(chip, MASK_ADDR_BURGUNDY_MORE_OUTPUTENABLES, val);
  223. return val != oval;
  224. }
  225. #define BURGUNDY_OUTPUT_SWITCH(xname, xindex, lmask, rmask, stereo) \
  226. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex,\
  227. .info = snd_pmac_burgundy_info_switch_out,\
  228. .get = snd_pmac_burgundy_get_switch_out,\
  229. .put = snd_pmac_burgundy_put_switch_out,\
  230. .private_value = ((lmask) | ((rmask) << 8) | ((stereo) << 24)) }
  231. /* line/speaker output volume */
  232. static int snd_pmac_burgundy_info_volume_out(struct snd_kcontrol *kcontrol,
  233. struct snd_ctl_elem_info *uinfo)
  234. {
  235. int stereo = (kcontrol->private_value >> 24) & 1;
  236. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  237. uinfo->count = stereo + 1;
  238. uinfo->value.integer.min = 0;
  239. uinfo->value.integer.max = 15;
  240. return 0;
  241. }
  242. static int snd_pmac_burgundy_get_volume_out(struct snd_kcontrol *kcontrol,
  243. struct snd_ctl_elem_value *ucontrol)
  244. {
  245. struct snd_pmac *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;
  249. oval = ~snd_pmac_burgundy_rcb(chip, addr) & 0xff;
  250. ucontrol->value.integer.value[0] = oval & 0xf;
  251. if (stereo)
  252. ucontrol->value.integer.value[1] = (oval >> 4) & 0xf;
  253. return 0;
  254. }
  255. static int snd_pmac_burgundy_put_volume_out(struct snd_kcontrol *kcontrol,
  256. struct snd_ctl_elem_value *ucontrol)
  257. {
  258. struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
  259. unsigned int addr = BASE2ADDR(kcontrol->private_value & 0xff);
  260. int stereo = (kcontrol->private_value >> 24) & 1;
  261. int oval, val;
  262. oval = ~snd_pmac_burgundy_rcb(chip, addr) & 0xff;
  263. val = ucontrol->value.integer.value[0];
  264. if (stereo)
  265. val |= ucontrol->value.integer.value[1] << 4;
  266. else
  267. val |= ucontrol->value.integer.value[0] << 4;
  268. val = ~val & 0xff;
  269. snd_pmac_burgundy_wcb(chip, addr, val);
  270. return val != oval;
  271. }
  272. #define BURGUNDY_OUTPUT_VOLUME(xname, xindex, addr, stereo) \
  273. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex,\
  274. .info = snd_pmac_burgundy_info_volume_out,\
  275. .get = snd_pmac_burgundy_get_volume_out,\
  276. .put = snd_pmac_burgundy_put_volume_out,\
  277. .private_value = (ADDR2BASE(addr) | ((stereo) << 24)) }
  278. static struct snd_kcontrol_new snd_pmac_burgundy_mixers[] __initdata = {
  279. BURGUNDY_VOLUME("Master Playback Volume", 0, MASK_ADDR_BURGUNDY_MASTER_VOLUME, 8),
  280. BURGUNDY_VOLUME("Line Playback Volume", 0, MASK_ADDR_BURGUNDY_VOLLINE, 16),
  281. BURGUNDY_VOLUME("CD Playback Volume", 0, MASK_ADDR_BURGUNDY_VOLCD, 16),
  282. BURGUNDY_VOLUME("Mic Playback Volume", 0, MASK_ADDR_BURGUNDY_VOLMIC, 16),
  283. BURGUNDY_OUTPUT_VOLUME("PC Speaker Playback Volume", 0, MASK_ADDR_BURGUNDY_ATTENHP, 0),
  284. /*BURGUNDY_OUTPUT_VOLUME("PCM Playback Volume", 0, MASK_ADDR_BURGUNDY_ATTENLINEOUT, 1),*/
  285. BURGUNDY_OUTPUT_VOLUME("Headphone Playback Volume", 0, MASK_ADDR_BURGUNDY_ATTENSPEAKER, 1),
  286. };
  287. static struct snd_kcontrol_new snd_pmac_burgundy_master_sw __initdata =
  288. BURGUNDY_OUTPUT_SWITCH("Headphone Playback Switch", 0, BURGUNDY_OUTPUT_LEFT, BURGUNDY_OUTPUT_RIGHT, 1);
  289. static struct snd_kcontrol_new snd_pmac_burgundy_speaker_sw __initdata =
  290. BURGUNDY_OUTPUT_SWITCH("PC Speaker Playback Switch", 0, BURGUNDY_OUTPUT_INTERN, 0, 0);
  291. #ifdef PMAC_SUPPORT_AUTOMUTE
  292. /*
  293. * auto-mute stuffs
  294. */
  295. static int snd_pmac_burgundy_detect_headphone(struct snd_pmac *chip)
  296. {
  297. return (in_le32(&chip->awacs->codec_stat) & chip->hp_stat_mask) ? 1 : 0;
  298. }
  299. static void snd_pmac_burgundy_update_automute(struct snd_pmac *chip, int do_notify)
  300. {
  301. if (chip->auto_mute) {
  302. int reg, oreg;
  303. reg = oreg = snd_pmac_burgundy_rcb(chip, MASK_ADDR_BURGUNDY_MORE_OUTPUTENABLES);
  304. reg &= ~(BURGUNDY_OUTPUT_LEFT | BURGUNDY_OUTPUT_RIGHT | BURGUNDY_OUTPUT_INTERN);
  305. if (snd_pmac_burgundy_detect_headphone(chip))
  306. reg |= BURGUNDY_OUTPUT_LEFT | BURGUNDY_OUTPUT_RIGHT;
  307. else
  308. reg |= BURGUNDY_OUTPUT_INTERN;
  309. if (do_notify && reg == oreg)
  310. return;
  311. snd_pmac_burgundy_wcb(chip, MASK_ADDR_BURGUNDY_MORE_OUTPUTENABLES, reg);
  312. if (do_notify) {
  313. snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
  314. &chip->master_sw_ctl->id);
  315. snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
  316. &chip->speaker_sw_ctl->id);
  317. snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
  318. &chip->hp_detect_ctl->id);
  319. }
  320. }
  321. }
  322. #endif /* PMAC_SUPPORT_AUTOMUTE */
  323. /*
  324. * initialize burgundy
  325. */
  326. int __init snd_pmac_burgundy_init(struct snd_pmac *chip)
  327. {
  328. int i, err;
  329. /* Checks to see the chip is alive and kicking */
  330. if ((in_le32(&chip->awacs->codec_ctrl) & MASK_ERRCODE) == 0xf0000) {
  331. printk(KERN_WARNING "pmac burgundy: disabled by MacOS :-(\n");
  332. return 1;
  333. }
  334. snd_pmac_burgundy_wcb(chip, MASK_ADDR_BURGUNDY_OUTPUTENABLES,
  335. DEF_BURGUNDY_OUTPUTENABLES);
  336. snd_pmac_burgundy_wcb(chip, MASK_ADDR_BURGUNDY_MORE_OUTPUTENABLES,
  337. DEF_BURGUNDY_MORE_OUTPUTENABLES);
  338. snd_pmac_burgundy_wcw(chip, MASK_ADDR_BURGUNDY_OUTPUTSELECTS,
  339. DEF_BURGUNDY_OUTPUTSELECTS);
  340. snd_pmac_burgundy_wcb(chip, MASK_ADDR_BURGUNDY_INPSEL21,
  341. DEF_BURGUNDY_INPSEL21);
  342. snd_pmac_burgundy_wcb(chip, MASK_ADDR_BURGUNDY_INPSEL3,
  343. DEF_BURGUNDY_INPSEL3);
  344. snd_pmac_burgundy_wcb(chip, MASK_ADDR_BURGUNDY_GAINCD,
  345. DEF_BURGUNDY_GAINCD);
  346. snd_pmac_burgundy_wcb(chip, MASK_ADDR_BURGUNDY_GAINLINE,
  347. DEF_BURGUNDY_GAINLINE);
  348. snd_pmac_burgundy_wcb(chip, MASK_ADDR_BURGUNDY_GAINMIC,
  349. DEF_BURGUNDY_GAINMIC);
  350. snd_pmac_burgundy_wcb(chip, MASK_ADDR_BURGUNDY_GAINMODEM,
  351. DEF_BURGUNDY_GAINMODEM);
  352. snd_pmac_burgundy_wcb(chip, MASK_ADDR_BURGUNDY_ATTENSPEAKER,
  353. DEF_BURGUNDY_ATTENSPEAKER);
  354. snd_pmac_burgundy_wcb(chip, MASK_ADDR_BURGUNDY_ATTENLINEOUT,
  355. DEF_BURGUNDY_ATTENLINEOUT);
  356. snd_pmac_burgundy_wcb(chip, MASK_ADDR_BURGUNDY_ATTENHP,
  357. DEF_BURGUNDY_ATTENHP);
  358. snd_pmac_burgundy_wcw(chip, MASK_ADDR_BURGUNDY_MASTER_VOLUME,
  359. DEF_BURGUNDY_MASTER_VOLUME);
  360. snd_pmac_burgundy_wcw(chip, MASK_ADDR_BURGUNDY_VOLCD,
  361. DEF_BURGUNDY_VOLCD);
  362. snd_pmac_burgundy_wcw(chip, MASK_ADDR_BURGUNDY_VOLLINE,
  363. DEF_BURGUNDY_VOLLINE);
  364. snd_pmac_burgundy_wcw(chip, MASK_ADDR_BURGUNDY_VOLMIC,
  365. DEF_BURGUNDY_VOLMIC);
  366. if (chip->hp_stat_mask == 0)
  367. /* set headphone-jack detection bit */
  368. chip->hp_stat_mask = 0x04;
  369. /*
  370. * build burgundy mixers
  371. */
  372. strcpy(chip->card->mixername, "PowerMac Burgundy");
  373. for (i = 0; i < ARRAY_SIZE(snd_pmac_burgundy_mixers); i++) {
  374. if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_pmac_burgundy_mixers[i], chip))) < 0)
  375. return err;
  376. }
  377. chip->master_sw_ctl = snd_ctl_new1(&snd_pmac_burgundy_master_sw, chip);
  378. if ((err = snd_ctl_add(chip->card, chip->master_sw_ctl)) < 0)
  379. return err;
  380. chip->speaker_sw_ctl = snd_ctl_new1(&snd_pmac_burgundy_speaker_sw, chip);
  381. if ((err = snd_ctl_add(chip->card, chip->speaker_sw_ctl)) < 0)
  382. return err;
  383. #ifdef PMAC_SUPPORT_AUTOMUTE
  384. if ((err = snd_pmac_add_automute(chip)) < 0)
  385. return err;
  386. chip->detect_headphone = snd_pmac_burgundy_detect_headphone;
  387. chip->update_automute = snd_pmac_burgundy_update_automute;
  388. snd_pmac_burgundy_update_automute(chip, 0); /* update the status only */
  389. #endif
  390. return 0;
  391. }