awacs.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932
  1. /*
  2. * PMac AWACS 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 <asm/io.h>
  22. #include <asm/nvram.h>
  23. #include <linux/init.h>
  24. #include <linux/delay.h>
  25. #include <linux/slab.h>
  26. #include <sound/core.h>
  27. #include "pmac.h"
  28. #ifdef CONFIG_ADB_CUDA
  29. #define PMAC_AMP_AVAIL
  30. #endif
  31. #ifdef PMAC_AMP_AVAIL
  32. struct awacs_amp {
  33. unsigned char amp_master;
  34. unsigned char amp_vol[2][2];
  35. unsigned char amp_tone[2];
  36. };
  37. #define CHECK_CUDA_AMP() (sys_ctrler == SYS_CTRLER_CUDA)
  38. #endif /* PMAC_AMP_AVAIL */
  39. static void snd_pmac_screamer_wait(struct snd_pmac *chip)
  40. {
  41. long timeout = 2000;
  42. while (!(in_le32(&chip->awacs->codec_stat) & MASK_VALID)) {
  43. mdelay(1);
  44. if (! --timeout) {
  45. snd_printd("snd_pmac_screamer_wait timeout\n");
  46. break;
  47. }
  48. }
  49. }
  50. /*
  51. * write AWACS register
  52. */
  53. static void
  54. snd_pmac_awacs_write(struct snd_pmac *chip, int val)
  55. {
  56. long timeout = 5000000;
  57. if (chip->model == PMAC_SCREAMER)
  58. snd_pmac_screamer_wait(chip);
  59. out_le32(&chip->awacs->codec_ctrl, val | (chip->subframe << 22));
  60. while (in_le32(&chip->awacs->codec_ctrl) & MASK_NEWECMD) {
  61. if (! --timeout) {
  62. snd_printd("snd_pmac_awacs_write timeout\n");
  63. break;
  64. }
  65. }
  66. }
  67. static void
  68. snd_pmac_awacs_write_reg(struct snd_pmac *chip, int reg, int val)
  69. {
  70. snd_pmac_awacs_write(chip, val | (reg << 12));
  71. chip->awacs_reg[reg] = val;
  72. }
  73. static void
  74. snd_pmac_awacs_write_noreg(struct snd_pmac *chip, int reg, int val)
  75. {
  76. snd_pmac_awacs_write(chip, val | (reg << 12));
  77. }
  78. #ifdef CONFIG_PM
  79. /* Recalibrate chip */
  80. static void screamer_recalibrate(struct snd_pmac *chip)
  81. {
  82. if (chip->model != PMAC_SCREAMER)
  83. return;
  84. /* Sorry for the horrible delays... I hope to get that improved
  85. * by making the whole PM process asynchronous in a future version
  86. */
  87. snd_pmac_awacs_write_noreg(chip, 1, chip->awacs_reg[1]);
  88. if (chip->manufacturer == 0x1)
  89. /* delay for broken crystal part */
  90. msleep(750);
  91. snd_pmac_awacs_write_noreg(chip, 1,
  92. chip->awacs_reg[1] | MASK_RECALIBRATE |
  93. MASK_CMUTE | MASK_AMUTE);
  94. snd_pmac_awacs_write_noreg(chip, 1, chip->awacs_reg[1]);
  95. snd_pmac_awacs_write_noreg(chip, 6, chip->awacs_reg[6]);
  96. }
  97. #else
  98. #define screamer_recalibrate(chip) /* NOP */
  99. #endif
  100. /*
  101. * additional callback to set the pcm format
  102. */
  103. static void snd_pmac_awacs_set_format(struct snd_pmac *chip)
  104. {
  105. chip->awacs_reg[1] &= ~MASK_SAMPLERATE;
  106. chip->awacs_reg[1] |= chip->rate_index << 3;
  107. snd_pmac_awacs_write_reg(chip, 1, chip->awacs_reg[1]);
  108. }
  109. /*
  110. * AWACS volume callbacks
  111. */
  112. /*
  113. * volumes: 0-15 stereo
  114. */
  115. static int snd_pmac_awacs_info_volume(struct snd_kcontrol *kcontrol,
  116. struct snd_ctl_elem_info *uinfo)
  117. {
  118. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  119. uinfo->count = 2;
  120. uinfo->value.integer.min = 0;
  121. uinfo->value.integer.max = 15;
  122. return 0;
  123. }
  124. static int snd_pmac_awacs_get_volume(struct snd_kcontrol *kcontrol,
  125. struct snd_ctl_elem_value *ucontrol)
  126. {
  127. struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
  128. int reg = kcontrol->private_value & 0xff;
  129. int lshift = (kcontrol->private_value >> 8) & 0xff;
  130. int inverted = (kcontrol->private_value >> 16) & 1;
  131. unsigned long flags;
  132. int vol[2];
  133. spin_lock_irqsave(&chip->reg_lock, flags);
  134. vol[0] = (chip->awacs_reg[reg] >> lshift) & 0xf;
  135. vol[1] = chip->awacs_reg[reg] & 0xf;
  136. spin_unlock_irqrestore(&chip->reg_lock, flags);
  137. if (inverted) {
  138. vol[0] = 0x0f - vol[0];
  139. vol[1] = 0x0f - vol[1];
  140. }
  141. ucontrol->value.integer.value[0] = vol[0];
  142. ucontrol->value.integer.value[1] = vol[1];
  143. return 0;
  144. }
  145. static int snd_pmac_awacs_put_volume(struct snd_kcontrol *kcontrol,
  146. struct snd_ctl_elem_value *ucontrol)
  147. {
  148. struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
  149. int reg = kcontrol->private_value & 0xff;
  150. int lshift = (kcontrol->private_value >> 8) & 0xff;
  151. int inverted = (kcontrol->private_value >> 16) & 1;
  152. int val, oldval;
  153. unsigned long flags;
  154. unsigned int vol[2];
  155. vol[0] = ucontrol->value.integer.value[0];
  156. vol[1] = ucontrol->value.integer.value[1];
  157. if (vol[0] > 0x0f || vol[1] > 0x0f)
  158. return -EINVAL;
  159. if (inverted) {
  160. vol[0] = 0x0f - vol[0];
  161. vol[1] = 0x0f - vol[1];
  162. }
  163. vol[0] &= 0x0f;
  164. vol[1] &= 0x0f;
  165. spin_lock_irqsave(&chip->reg_lock, flags);
  166. oldval = chip->awacs_reg[reg];
  167. val = oldval & ~(0xf | (0xf << lshift));
  168. val |= vol[0] << lshift;
  169. val |= vol[1];
  170. if (oldval != val)
  171. snd_pmac_awacs_write_reg(chip, reg, val);
  172. spin_unlock_irqrestore(&chip->reg_lock, flags);
  173. return oldval != reg;
  174. }
  175. #define AWACS_VOLUME(xname, xreg, xshift, xinverted) \
  176. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = 0, \
  177. .info = snd_pmac_awacs_info_volume, \
  178. .get = snd_pmac_awacs_get_volume, \
  179. .put = snd_pmac_awacs_put_volume, \
  180. .private_value = (xreg) | ((xshift) << 8) | ((xinverted) << 16) }
  181. /*
  182. * mute master/ogain for AWACS: mono
  183. */
  184. static int snd_pmac_awacs_get_switch(struct snd_kcontrol *kcontrol,
  185. struct snd_ctl_elem_value *ucontrol)
  186. {
  187. struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
  188. int reg = kcontrol->private_value & 0xff;
  189. int shift = (kcontrol->private_value >> 8) & 0xff;
  190. int invert = (kcontrol->private_value >> 16) & 1;
  191. int val;
  192. unsigned long flags;
  193. spin_lock_irqsave(&chip->reg_lock, flags);
  194. val = (chip->awacs_reg[reg] >> shift) & 1;
  195. spin_unlock_irqrestore(&chip->reg_lock, flags);
  196. if (invert)
  197. val = 1 - val;
  198. ucontrol->value.integer.value[0] = val;
  199. return 0;
  200. }
  201. static int snd_pmac_awacs_put_switch(struct snd_kcontrol *kcontrol,
  202. struct snd_ctl_elem_value *ucontrol)
  203. {
  204. struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
  205. int reg = kcontrol->private_value & 0xff;
  206. int shift = (kcontrol->private_value >> 8) & 0xff;
  207. int invert = (kcontrol->private_value >> 16) & 1;
  208. int mask = 1 << shift;
  209. int val, changed;
  210. unsigned long flags;
  211. spin_lock_irqsave(&chip->reg_lock, flags);
  212. val = chip->awacs_reg[reg] & ~mask;
  213. if (ucontrol->value.integer.value[0] != invert)
  214. val |= mask;
  215. changed = chip->awacs_reg[reg] != val;
  216. if (changed)
  217. snd_pmac_awacs_write_reg(chip, reg, val);
  218. spin_unlock_irqrestore(&chip->reg_lock, flags);
  219. return changed;
  220. }
  221. #define AWACS_SWITCH(xname, xreg, xshift, xinvert) \
  222. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = 0, \
  223. .info = snd_pmac_boolean_mono_info, \
  224. .get = snd_pmac_awacs_get_switch, \
  225. .put = snd_pmac_awacs_put_switch, \
  226. .private_value = (xreg) | ((xshift) << 8) | ((xinvert) << 16) }
  227. #ifdef PMAC_AMP_AVAIL
  228. /*
  229. * controls for perch/whisper extension cards, e.g. G3 desktop
  230. *
  231. * TDA7433 connected via i2c address 0x45 (= 0x8a),
  232. * accessed through cuda
  233. */
  234. static void awacs_set_cuda(int reg, int val)
  235. {
  236. struct adb_request req;
  237. cuda_request(&req, NULL, 5, CUDA_PACKET, CUDA_GET_SET_IIC, 0x8a, reg, val);
  238. while (! req.complete)
  239. cuda_poll();
  240. }
  241. /*
  242. * level = 0 - 14, 7 = 0 dB
  243. */
  244. static void awacs_amp_set_tone(struct awacs_amp *amp, int bass, int treble)
  245. {
  246. amp->amp_tone[0] = bass;
  247. amp->amp_tone[1] = treble;
  248. if (bass > 7)
  249. bass = (14 - bass) + 8;
  250. if (treble > 7)
  251. treble = (14 - treble) + 8;
  252. awacs_set_cuda(2, (bass << 4) | treble);
  253. }
  254. /*
  255. * vol = 0 - 31 (attenuation), 32 = mute bit, stereo
  256. */
  257. static int awacs_amp_set_vol(struct awacs_amp *amp, int index, int lvol, int rvol,
  258. int do_check)
  259. {
  260. if (do_check && amp->amp_vol[index][0] == lvol &&
  261. amp->amp_vol[index][1] == rvol)
  262. return 0;
  263. awacs_set_cuda(3 + index, lvol);
  264. awacs_set_cuda(5 + index, rvol);
  265. amp->amp_vol[index][0] = lvol;
  266. amp->amp_vol[index][1] = rvol;
  267. return 1;
  268. }
  269. /*
  270. * 0 = -79 dB, 79 = 0 dB, 99 = +20 dB
  271. */
  272. static void awacs_amp_set_master(struct awacs_amp *amp, int vol)
  273. {
  274. amp->amp_master = vol;
  275. if (vol <= 79)
  276. vol = 32 + (79 - vol);
  277. else
  278. vol = 32 - (vol - 79);
  279. awacs_set_cuda(1, vol);
  280. }
  281. static void awacs_amp_free(struct snd_pmac *chip)
  282. {
  283. struct awacs_amp *amp = chip->mixer_data;
  284. snd_assert(amp, return);
  285. kfree(amp);
  286. chip->mixer_data = NULL;
  287. chip->mixer_free = NULL;
  288. }
  289. /*
  290. * mixer controls
  291. */
  292. static int snd_pmac_awacs_info_volume_amp(struct snd_kcontrol *kcontrol,
  293. struct snd_ctl_elem_info *uinfo)
  294. {
  295. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  296. uinfo->count = 2;
  297. uinfo->value.integer.min = 0;
  298. uinfo->value.integer.max = 31;
  299. return 0;
  300. }
  301. static int snd_pmac_awacs_get_volume_amp(struct snd_kcontrol *kcontrol,
  302. struct snd_ctl_elem_value *ucontrol)
  303. {
  304. struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
  305. int index = kcontrol->private_value;
  306. struct awacs_amp *amp = chip->mixer_data;
  307. snd_assert(amp, return -EINVAL);
  308. snd_assert(index >= 0 && index <= 1, return -EINVAL);
  309. ucontrol->value.integer.value[0] = 31 - (amp->amp_vol[index][0] & 31);
  310. ucontrol->value.integer.value[1] = 31 - (amp->amp_vol[index][1] & 31);
  311. return 0;
  312. }
  313. static int snd_pmac_awacs_put_volume_amp(struct snd_kcontrol *kcontrol,
  314. struct snd_ctl_elem_value *ucontrol)
  315. {
  316. struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
  317. int index = kcontrol->private_value;
  318. int vol[2];
  319. struct awacs_amp *amp = chip->mixer_data;
  320. snd_assert(amp, return -EINVAL);
  321. snd_assert(index >= 0 && index <= 1, return -EINVAL);
  322. vol[0] = (31 - (ucontrol->value.integer.value[0] & 31)) | (amp->amp_vol[index][0] & 32);
  323. vol[1] = (31 - (ucontrol->value.integer.value[1] & 31)) | (amp->amp_vol[index][1] & 32);
  324. return awacs_amp_set_vol(amp, index, vol[0], vol[1], 1);
  325. }
  326. static int snd_pmac_awacs_get_switch_amp(struct snd_kcontrol *kcontrol,
  327. struct snd_ctl_elem_value *ucontrol)
  328. {
  329. struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
  330. int index = kcontrol->private_value;
  331. struct awacs_amp *amp = chip->mixer_data;
  332. snd_assert(amp, return -EINVAL);
  333. snd_assert(index >= 0 && index <= 1, return -EINVAL);
  334. ucontrol->value.integer.value[0] = (amp->amp_vol[index][0] & 32) ? 0 : 1;
  335. ucontrol->value.integer.value[1] = (amp->amp_vol[index][1] & 32) ? 0 : 1;
  336. return 0;
  337. }
  338. static int snd_pmac_awacs_put_switch_amp(struct snd_kcontrol *kcontrol,
  339. struct snd_ctl_elem_value *ucontrol)
  340. {
  341. struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
  342. int index = kcontrol->private_value;
  343. int vol[2];
  344. struct awacs_amp *amp = chip->mixer_data;
  345. snd_assert(amp, return -EINVAL);
  346. snd_assert(index >= 0 && index <= 1, return -EINVAL);
  347. vol[0] = (ucontrol->value.integer.value[0] ? 0 : 32) | (amp->amp_vol[index][0] & 31);
  348. vol[1] = (ucontrol->value.integer.value[1] ? 0 : 32) | (amp->amp_vol[index][1] & 31);
  349. return awacs_amp_set_vol(amp, index, vol[0], vol[1], 1);
  350. }
  351. static int snd_pmac_awacs_info_tone_amp(struct snd_kcontrol *kcontrol,
  352. struct snd_ctl_elem_info *uinfo)
  353. {
  354. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  355. uinfo->count = 1;
  356. uinfo->value.integer.min = 0;
  357. uinfo->value.integer.max = 14;
  358. return 0;
  359. }
  360. static int snd_pmac_awacs_get_tone_amp(struct snd_kcontrol *kcontrol,
  361. struct snd_ctl_elem_value *ucontrol)
  362. {
  363. struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
  364. int index = kcontrol->private_value;
  365. struct awacs_amp *amp = chip->mixer_data;
  366. snd_assert(amp, return -EINVAL);
  367. snd_assert(index >= 0 && index <= 1, return -EINVAL);
  368. ucontrol->value.integer.value[0] = amp->amp_tone[index];
  369. return 0;
  370. }
  371. static int snd_pmac_awacs_put_tone_amp(struct snd_kcontrol *kcontrol,
  372. struct snd_ctl_elem_value *ucontrol)
  373. {
  374. struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
  375. int index = kcontrol->private_value;
  376. struct awacs_amp *amp = chip->mixer_data;
  377. unsigned int val;
  378. snd_assert(amp, return -EINVAL);
  379. snd_assert(index >= 0 && index <= 1, return -EINVAL);
  380. val = ucontrol->value.integer.value[0];
  381. if (val > 14)
  382. return -EINVAL;
  383. if (val != amp->amp_tone[index]) {
  384. amp->amp_tone[index] = val;
  385. awacs_amp_set_tone(amp, amp->amp_tone[0], amp->amp_tone[1]);
  386. return 1;
  387. }
  388. return 0;
  389. }
  390. static int snd_pmac_awacs_info_master_amp(struct snd_kcontrol *kcontrol,
  391. struct snd_ctl_elem_info *uinfo)
  392. {
  393. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  394. uinfo->count = 1;
  395. uinfo->value.integer.min = 0;
  396. uinfo->value.integer.max = 99;
  397. return 0;
  398. }
  399. static int snd_pmac_awacs_get_master_amp(struct snd_kcontrol *kcontrol,
  400. struct snd_ctl_elem_value *ucontrol)
  401. {
  402. struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
  403. struct awacs_amp *amp = chip->mixer_data;
  404. snd_assert(amp, return -EINVAL);
  405. ucontrol->value.integer.value[0] = amp->amp_master;
  406. return 0;
  407. }
  408. static int snd_pmac_awacs_put_master_amp(struct snd_kcontrol *kcontrol,
  409. struct snd_ctl_elem_value *ucontrol)
  410. {
  411. struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
  412. struct awacs_amp *amp = chip->mixer_data;
  413. unsigned int val;
  414. snd_assert(amp, return -EINVAL);
  415. val = ucontrol->value.integer.value[0];
  416. if (val > 99)
  417. return -EINVAL;
  418. if (val != amp->amp_master) {
  419. amp->amp_master = val;
  420. awacs_amp_set_master(amp, amp->amp_master);
  421. return 1;
  422. }
  423. return 0;
  424. }
  425. #define AMP_CH_SPK 0
  426. #define AMP_CH_HD 1
  427. static struct snd_kcontrol_new snd_pmac_awacs_amp_vol[] __initdata = {
  428. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  429. .name = "PC Speaker Playback Volume",
  430. .info = snd_pmac_awacs_info_volume_amp,
  431. .get = snd_pmac_awacs_get_volume_amp,
  432. .put = snd_pmac_awacs_put_volume_amp,
  433. .private_value = AMP_CH_SPK,
  434. },
  435. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  436. .name = "Headphone Playback Volume",
  437. .info = snd_pmac_awacs_info_volume_amp,
  438. .get = snd_pmac_awacs_get_volume_amp,
  439. .put = snd_pmac_awacs_put_volume_amp,
  440. .private_value = AMP_CH_HD,
  441. },
  442. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  443. .name = "Tone Control - Bass",
  444. .info = snd_pmac_awacs_info_tone_amp,
  445. .get = snd_pmac_awacs_get_tone_amp,
  446. .put = snd_pmac_awacs_put_tone_amp,
  447. .private_value = 0,
  448. },
  449. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  450. .name = "Tone Control - Treble",
  451. .info = snd_pmac_awacs_info_tone_amp,
  452. .get = snd_pmac_awacs_get_tone_amp,
  453. .put = snd_pmac_awacs_put_tone_amp,
  454. .private_value = 1,
  455. },
  456. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  457. .name = "Amp Master Playback Volume",
  458. .info = snd_pmac_awacs_info_master_amp,
  459. .get = snd_pmac_awacs_get_master_amp,
  460. .put = snd_pmac_awacs_put_master_amp,
  461. },
  462. };
  463. static struct snd_kcontrol_new snd_pmac_awacs_amp_hp_sw __initdata = {
  464. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  465. .name = "Headphone Playback Switch",
  466. .info = snd_pmac_boolean_stereo_info,
  467. .get = snd_pmac_awacs_get_switch_amp,
  468. .put = snd_pmac_awacs_put_switch_amp,
  469. .private_value = AMP_CH_HD,
  470. };
  471. static struct snd_kcontrol_new snd_pmac_awacs_amp_spk_sw __initdata = {
  472. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  473. .name = "PC Speaker Playback Switch",
  474. .info = snd_pmac_boolean_stereo_info,
  475. .get = snd_pmac_awacs_get_switch_amp,
  476. .put = snd_pmac_awacs_put_switch_amp,
  477. .private_value = AMP_CH_SPK,
  478. };
  479. #endif /* PMAC_AMP_AVAIL */
  480. /*
  481. * mic boost for screamer
  482. */
  483. static int snd_pmac_screamer_mic_boost_info(struct snd_kcontrol *kcontrol,
  484. struct snd_ctl_elem_info *uinfo)
  485. {
  486. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  487. uinfo->count = 1;
  488. uinfo->value.integer.min = 0;
  489. uinfo->value.integer.max = 2;
  490. return 0;
  491. }
  492. static int snd_pmac_screamer_mic_boost_get(struct snd_kcontrol *kcontrol,
  493. struct snd_ctl_elem_value *ucontrol)
  494. {
  495. struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
  496. int val;
  497. unsigned long flags;
  498. spin_lock_irqsave(&chip->reg_lock, flags);
  499. if (chip->awacs_reg[6] & MASK_MIC_BOOST)
  500. val = 2;
  501. else if (chip->awacs_reg[0] & MASK_GAINLINE)
  502. val = 1;
  503. else
  504. val = 0;
  505. spin_unlock_irqrestore(&chip->reg_lock, flags);
  506. ucontrol->value.integer.value[0] = val;
  507. return 0;
  508. }
  509. static int snd_pmac_screamer_mic_boost_put(struct snd_kcontrol *kcontrol,
  510. struct snd_ctl_elem_value *ucontrol)
  511. {
  512. struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
  513. int changed = 0;
  514. int val0, val6;
  515. unsigned long flags;
  516. spin_lock_irqsave(&chip->reg_lock, flags);
  517. val0 = chip->awacs_reg[0] & ~MASK_GAINLINE;
  518. val6 = chip->awacs_reg[6] & ~MASK_MIC_BOOST;
  519. if (ucontrol->value.integer.value[0] > 0) {
  520. val0 |= MASK_GAINLINE;
  521. if (ucontrol->value.integer.value[0] > 1)
  522. val6 |= MASK_MIC_BOOST;
  523. }
  524. if (val0 != chip->awacs_reg[0]) {
  525. snd_pmac_awacs_write_reg(chip, 0, val0);
  526. changed = 1;
  527. }
  528. if (val6 != chip->awacs_reg[6]) {
  529. snd_pmac_awacs_write_reg(chip, 6, val6);
  530. changed = 1;
  531. }
  532. spin_unlock_irqrestore(&chip->reg_lock, flags);
  533. return changed;
  534. }
  535. /*
  536. * lists of mixer elements
  537. */
  538. static struct snd_kcontrol_new snd_pmac_awacs_mixers[] __initdata = {
  539. AWACS_VOLUME("Master Playback Volume", 2, 6, 1),
  540. AWACS_SWITCH("Master Capture Switch", 1, SHIFT_LOOPTHRU, 0),
  541. AWACS_VOLUME("Capture Volume", 0, 4, 0),
  542. AWACS_SWITCH("CD Capture Switch", 0, SHIFT_MUX_CD, 0),
  543. };
  544. /* FIXME: is this correct order?
  545. * screamer (powerbook G3 pismo) seems to have different bits...
  546. */
  547. static struct snd_kcontrol_new snd_pmac_awacs_mixers2[] __initdata = {
  548. AWACS_SWITCH("Line Capture Switch", 0, SHIFT_MUX_LINE, 0),
  549. AWACS_SWITCH("Mic Capture Switch", 0, SHIFT_MUX_MIC, 0),
  550. };
  551. static struct snd_kcontrol_new snd_pmac_screamer_mixers2[] __initdata = {
  552. AWACS_SWITCH("Line Capture Switch", 0, SHIFT_MUX_MIC, 0),
  553. AWACS_SWITCH("Mic Capture Switch", 0, SHIFT_MUX_LINE, 0),
  554. };
  555. static struct snd_kcontrol_new snd_pmac_awacs_master_sw __initdata =
  556. AWACS_SWITCH("Master Playback Switch", 1, SHIFT_HDMUTE, 1);
  557. static struct snd_kcontrol_new snd_pmac_awacs_mic_boost[] __initdata = {
  558. AWACS_SWITCH("Mic Boost", 0, SHIFT_GAINLINE, 0),
  559. };
  560. static struct snd_kcontrol_new snd_pmac_screamer_mic_boost[] __initdata = {
  561. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  562. .name = "Mic Boost",
  563. .info = snd_pmac_screamer_mic_boost_info,
  564. .get = snd_pmac_screamer_mic_boost_get,
  565. .put = snd_pmac_screamer_mic_boost_put,
  566. },
  567. };
  568. static struct snd_kcontrol_new snd_pmac_awacs_speaker_vol[] __initdata = {
  569. AWACS_VOLUME("PC Speaker Playback Volume", 4, 6, 1),
  570. };
  571. static struct snd_kcontrol_new snd_pmac_awacs_speaker_sw __initdata =
  572. AWACS_SWITCH("PC Speaker Playback Switch", 1, SHIFT_SPKMUTE, 1);
  573. /*
  574. * add new mixer elements to the card
  575. */
  576. static int build_mixers(struct snd_pmac *chip, int nums, struct snd_kcontrol_new *mixers)
  577. {
  578. int i, err;
  579. for (i = 0; i < nums; i++) {
  580. if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&mixers[i], chip))) < 0)
  581. return err;
  582. }
  583. return 0;
  584. }
  585. /*
  586. * restore all registers
  587. */
  588. static void awacs_restore_all_regs(struct snd_pmac *chip)
  589. {
  590. snd_pmac_awacs_write_noreg(chip, 0, chip->awacs_reg[0]);
  591. snd_pmac_awacs_write_noreg(chip, 1, chip->awacs_reg[1]);
  592. snd_pmac_awacs_write_noreg(chip, 2, chip->awacs_reg[2]);
  593. snd_pmac_awacs_write_noreg(chip, 4, chip->awacs_reg[4]);
  594. if (chip->model == PMAC_SCREAMER) {
  595. snd_pmac_awacs_write_noreg(chip, 5, chip->awacs_reg[5]);
  596. snd_pmac_awacs_write_noreg(chip, 6, chip->awacs_reg[6]);
  597. snd_pmac_awacs_write_noreg(chip, 7, chip->awacs_reg[7]);
  598. }
  599. }
  600. #ifdef CONFIG_PM
  601. static void snd_pmac_awacs_suspend(struct snd_pmac *chip)
  602. {
  603. snd_pmac_awacs_write_noreg(chip, 1, (chip->awacs_reg[1]
  604. | MASK_AMUTE | MASK_CMUTE));
  605. }
  606. static void snd_pmac_awacs_resume(struct snd_pmac *chip)
  607. {
  608. if (machine_is_compatible("PowerBook3,1")
  609. || machine_is_compatible("PowerBook3,2")) {
  610. msleep(100);
  611. snd_pmac_awacs_write_reg(chip, 1,
  612. chip->awacs_reg[1] & ~MASK_PAROUT);
  613. msleep(300);
  614. }
  615. awacs_restore_all_regs(chip);
  616. if (chip->model == PMAC_SCREAMER) {
  617. /* reset power bits in reg 6 */
  618. mdelay(5);
  619. snd_pmac_awacs_write_noreg(chip, 6, chip->awacs_reg[6]);
  620. }
  621. screamer_recalibrate(chip);
  622. #ifdef PMAC_AMP_AVAIL
  623. if (chip->mixer_data) {
  624. struct awacs_amp *amp = chip->mixer_data;
  625. awacs_amp_set_vol(amp, 0, amp->amp_vol[0][0], amp->amp_vol[0][1], 0);
  626. awacs_amp_set_vol(amp, 1, amp->amp_vol[1][0], amp->amp_vol[1][1], 0);
  627. awacs_amp_set_tone(amp, amp->amp_tone[0], amp->amp_tone[1]);
  628. awacs_amp_set_master(amp, amp->amp_master);
  629. }
  630. #endif
  631. }
  632. #endif /* CONFIG_PM */
  633. #ifdef PMAC_SUPPORT_AUTOMUTE
  634. /*
  635. * auto-mute stuffs
  636. */
  637. static int snd_pmac_awacs_detect_headphone(struct snd_pmac *chip)
  638. {
  639. return (in_le32(&chip->awacs->codec_stat) & chip->hp_stat_mask) ? 1 : 0;
  640. }
  641. #ifdef PMAC_AMP_AVAIL
  642. static int toggle_amp_mute(struct awacs_amp *amp, int index, int mute)
  643. {
  644. int vol[2];
  645. vol[0] = amp->amp_vol[index][0] & 31;
  646. vol[1] = amp->amp_vol[index][1] & 31;
  647. if (mute) {
  648. vol[0] |= 32;
  649. vol[1] |= 32;
  650. }
  651. return awacs_amp_set_vol(amp, index, vol[0], vol[1], 1);
  652. }
  653. #endif
  654. static void snd_pmac_awacs_update_automute(struct snd_pmac *chip, int do_notify)
  655. {
  656. if (chip->auto_mute) {
  657. #ifdef PMAC_AMP_AVAIL
  658. if (chip->mixer_data) {
  659. struct awacs_amp *amp = chip->mixer_data;
  660. int changed;
  661. if (snd_pmac_awacs_detect_headphone(chip)) {
  662. changed = toggle_amp_mute(amp, AMP_CH_HD, 0);
  663. changed |= toggle_amp_mute(amp, AMP_CH_SPK, 1);
  664. } else {
  665. changed = toggle_amp_mute(amp, AMP_CH_HD, 1);
  666. changed |= toggle_amp_mute(amp, AMP_CH_SPK, 0);
  667. }
  668. if (do_notify && ! changed)
  669. return;
  670. } else
  671. #endif
  672. {
  673. int reg = chip->awacs_reg[1] | (MASK_HDMUTE|MASK_SPKMUTE);
  674. if (snd_pmac_awacs_detect_headphone(chip))
  675. reg &= ~MASK_HDMUTE;
  676. else
  677. reg &= ~MASK_SPKMUTE;
  678. if (do_notify && reg == chip->awacs_reg[1])
  679. return;
  680. snd_pmac_awacs_write_reg(chip, 1, reg);
  681. }
  682. if (do_notify) {
  683. snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
  684. &chip->master_sw_ctl->id);
  685. snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
  686. &chip->speaker_sw_ctl->id);
  687. snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
  688. &chip->hp_detect_ctl->id);
  689. }
  690. }
  691. }
  692. #endif /* PMAC_SUPPORT_AUTOMUTE */
  693. /*
  694. * initialize chip
  695. */
  696. int __init
  697. snd_pmac_awacs_init(struct snd_pmac *chip)
  698. {
  699. int err, vol;
  700. /* looks like MASK_GAINLINE triggers something, so we set here
  701. * as start-up
  702. */
  703. chip->awacs_reg[0] = MASK_MUX_CD | 0xff | MASK_GAINLINE;
  704. chip->awacs_reg[1] = MASK_CMUTE | MASK_AMUTE;
  705. /* FIXME: Only machines with external SRS module need MASK_PAROUT */
  706. if (chip->has_iic || chip->device_id == 0x5 ||
  707. /*chip->_device_id == 0x8 || */
  708. chip->device_id == 0xb)
  709. chip->awacs_reg[1] |= MASK_PAROUT;
  710. /* get default volume from nvram */
  711. // vol = (~nvram_read_byte(0x1308) & 7) << 1;
  712. // vol = ((pmac_xpram_read( 8 ) & 7 ) << 1 );
  713. vol = 0x0f; /* no, on alsa, muted as default */
  714. vol = vol + (vol << 6);
  715. chip->awacs_reg[2] = vol;
  716. chip->awacs_reg[4] = vol;
  717. if (chip->model == PMAC_SCREAMER) {
  718. chip->awacs_reg[5] = vol; /* FIXME: screamer has loopthru vol control */
  719. chip->awacs_reg[6] = MASK_MIC_BOOST; /* FIXME: maybe should be vol << 3 for PCMCIA speaker */
  720. chip->awacs_reg[7] = 0;
  721. }
  722. awacs_restore_all_regs(chip);
  723. chip->manufacturer = (in_le32(&chip->awacs->codec_stat) >> 8) & 0xf;
  724. screamer_recalibrate(chip);
  725. chip->revision = (in_le32(&chip->awacs->codec_stat) >> 12) & 0xf;
  726. #ifdef PMAC_AMP_AVAIL
  727. if (chip->revision == 3 && chip->has_iic && CHECK_CUDA_AMP()) {
  728. struct awacs_amp *amp = kzalloc(sizeof(*amp), GFP_KERNEL);
  729. if (! amp)
  730. return -ENOMEM;
  731. chip->mixer_data = amp;
  732. chip->mixer_free = awacs_amp_free;
  733. awacs_amp_set_vol(amp, 0, 63, 63, 0); /* mute and zero vol */
  734. awacs_amp_set_vol(amp, 1, 63, 63, 0);
  735. awacs_amp_set_tone(amp, 7, 7); /* 0 dB */
  736. awacs_amp_set_master(amp, 79); /* 0 dB */
  737. }
  738. #endif /* PMAC_AMP_AVAIL */
  739. if (chip->hp_stat_mask == 0) {
  740. /* set headphone-jack detection bit */
  741. switch (chip->model) {
  742. case PMAC_AWACS:
  743. chip->hp_stat_mask = 0x04;
  744. break;
  745. case PMAC_SCREAMER:
  746. switch (chip->device_id) {
  747. case 0x08:
  748. /* 1 = side jack, 2 = front jack */
  749. chip->hp_stat_mask = 0x03;
  750. break;
  751. case 0x00:
  752. case 0x05:
  753. chip->hp_stat_mask = 0x04;
  754. break;
  755. default:
  756. chip->hp_stat_mask = 0x08;
  757. break;
  758. }
  759. break;
  760. default:
  761. snd_BUG();
  762. break;
  763. }
  764. }
  765. /*
  766. * build mixers
  767. */
  768. strcpy(chip->card->mixername, "PowerMac AWACS");
  769. if ((err = build_mixers(chip, ARRAY_SIZE(snd_pmac_awacs_mixers),
  770. snd_pmac_awacs_mixers)) < 0)
  771. return err;
  772. if (chip->model == PMAC_SCREAMER)
  773. err = build_mixers(chip, ARRAY_SIZE(snd_pmac_screamer_mixers2),
  774. snd_pmac_screamer_mixers2);
  775. else
  776. err = build_mixers(chip, ARRAY_SIZE(snd_pmac_awacs_mixers2),
  777. snd_pmac_awacs_mixers2);
  778. if (err < 0)
  779. return err;
  780. chip->master_sw_ctl = snd_ctl_new1(&snd_pmac_awacs_master_sw, chip);
  781. if ((err = snd_ctl_add(chip->card, chip->master_sw_ctl)) < 0)
  782. return err;
  783. #ifdef PMAC_AMP_AVAIL
  784. if (chip->mixer_data) {
  785. /* use amplifier. the signal is connected from route A
  786. * to the amp. the amp has its headphone and speaker
  787. * volumes and mute switches, so we use them instead of
  788. * screamer registers.
  789. * in this case, it seems the route C is not used.
  790. */
  791. if ((err = build_mixers(chip, ARRAY_SIZE(snd_pmac_awacs_amp_vol),
  792. snd_pmac_awacs_amp_vol)) < 0)
  793. return err;
  794. /* overwrite */
  795. chip->master_sw_ctl = snd_ctl_new1(&snd_pmac_awacs_amp_hp_sw, chip);
  796. if ((err = snd_ctl_add(chip->card, chip->master_sw_ctl)) < 0)
  797. return err;
  798. chip->speaker_sw_ctl = snd_ctl_new1(&snd_pmac_awacs_amp_spk_sw, chip);
  799. if ((err = snd_ctl_add(chip->card, chip->speaker_sw_ctl)) < 0)
  800. return err;
  801. } else
  802. #endif /* PMAC_AMP_AVAIL */
  803. {
  804. /* route A = headphone, route C = speaker */
  805. if ((err = build_mixers(chip, ARRAY_SIZE(snd_pmac_awacs_speaker_vol),
  806. snd_pmac_awacs_speaker_vol)) < 0)
  807. return err;
  808. chip->speaker_sw_ctl = snd_ctl_new1(&snd_pmac_awacs_speaker_sw, chip);
  809. if ((err = snd_ctl_add(chip->card, chip->speaker_sw_ctl)) < 0)
  810. return err;
  811. }
  812. if (chip->model == PMAC_SCREAMER) {
  813. if ((err = build_mixers(chip, ARRAY_SIZE(snd_pmac_screamer_mic_boost),
  814. snd_pmac_screamer_mic_boost)) < 0)
  815. return err;
  816. } else {
  817. if ((err = build_mixers(chip, ARRAY_SIZE(snd_pmac_awacs_mic_boost),
  818. snd_pmac_awacs_mic_boost)) < 0)
  819. return err;
  820. }
  821. /*
  822. * set lowlevel callbacks
  823. */
  824. chip->set_format = snd_pmac_awacs_set_format;
  825. #ifdef CONFIG_PM
  826. chip->suspend = snd_pmac_awacs_suspend;
  827. chip->resume = snd_pmac_awacs_resume;
  828. #endif
  829. #ifdef PMAC_SUPPORT_AUTOMUTE
  830. if ((err = snd_pmac_add_automute(chip)) < 0)
  831. return err;
  832. chip->detect_headphone = snd_pmac_awacs_detect_headphone;
  833. chip->update_automute = snd_pmac_awacs_update_automute;
  834. snd_pmac_awacs_update_automute(chip, 0); /* update the status only */
  835. #endif
  836. if (chip->model == PMAC_SCREAMER) {
  837. snd_pmac_awacs_write_noreg(chip, 6, chip->awacs_reg[6]);
  838. snd_pmac_awacs_write_noreg(chip, 0, chip->awacs_reg[0]);
  839. }
  840. return 0;
  841. }