awacs.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059
  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,
  238. reg, val);
  239. while (! req.complete)
  240. cuda_poll();
  241. }
  242. /*
  243. * level = 0 - 14, 7 = 0 dB
  244. */
  245. static void awacs_amp_set_tone(struct awacs_amp *amp, int bass, int treble)
  246. {
  247. amp->amp_tone[0] = bass;
  248. amp->amp_tone[1] = treble;
  249. if (bass > 7)
  250. bass = (14 - bass) + 8;
  251. if (treble > 7)
  252. treble = (14 - treble) + 8;
  253. awacs_set_cuda(2, (bass << 4) | treble);
  254. }
  255. /*
  256. * vol = 0 - 31 (attenuation), 32 = mute bit, stereo
  257. */
  258. static int awacs_amp_set_vol(struct awacs_amp *amp, int index,
  259. int lvol, int rvol, int do_check)
  260. {
  261. if (do_check && amp->amp_vol[index][0] == lvol &&
  262. amp->amp_vol[index][1] == rvol)
  263. return 0;
  264. awacs_set_cuda(3 + index, lvol);
  265. awacs_set_cuda(5 + index, rvol);
  266. amp->amp_vol[index][0] = lvol;
  267. amp->amp_vol[index][1] = rvol;
  268. return 1;
  269. }
  270. /*
  271. * 0 = -79 dB, 79 = 0 dB, 99 = +20 dB
  272. */
  273. static void awacs_amp_set_master(struct awacs_amp *amp, int vol)
  274. {
  275. amp->amp_master = vol;
  276. if (vol <= 79)
  277. vol = 32 + (79 - vol);
  278. else
  279. vol = 32 - (vol - 79);
  280. awacs_set_cuda(1, vol);
  281. }
  282. static void awacs_amp_free(struct snd_pmac *chip)
  283. {
  284. struct awacs_amp *amp = chip->mixer_data;
  285. snd_assert(amp, return);
  286. kfree(amp);
  287. chip->mixer_data = NULL;
  288. chip->mixer_free = NULL;
  289. }
  290. /*
  291. * mixer controls
  292. */
  293. static int snd_pmac_awacs_info_volume_amp(struct snd_kcontrol *kcontrol,
  294. struct snd_ctl_elem_info *uinfo)
  295. {
  296. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  297. uinfo->count = 2;
  298. uinfo->value.integer.min = 0;
  299. uinfo->value.integer.max = 31;
  300. return 0;
  301. }
  302. static int snd_pmac_awacs_get_volume_amp(struct snd_kcontrol *kcontrol,
  303. struct snd_ctl_elem_value *ucontrol)
  304. {
  305. struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
  306. int index = kcontrol->private_value;
  307. struct awacs_amp *amp = chip->mixer_data;
  308. snd_assert(amp, return -EINVAL);
  309. snd_assert(index >= 0 && index <= 1, return -EINVAL);
  310. ucontrol->value.integer.value[0] = 31 - (amp->amp_vol[index][0] & 31);
  311. ucontrol->value.integer.value[1] = 31 - (amp->amp_vol[index][1] & 31);
  312. return 0;
  313. }
  314. static int snd_pmac_awacs_put_volume_amp(struct snd_kcontrol *kcontrol,
  315. struct snd_ctl_elem_value *ucontrol)
  316. {
  317. struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
  318. int index = kcontrol->private_value;
  319. int vol[2];
  320. struct awacs_amp *amp = chip->mixer_data;
  321. snd_assert(amp, return -EINVAL);
  322. snd_assert(index >= 0 && index <= 1, return -EINVAL);
  323. vol[0] = (31 - (ucontrol->value.integer.value[0] & 31))
  324. | (amp->amp_vol[index][0] & 32);
  325. vol[1] = (31 - (ucontrol->value.integer.value[1] & 31))
  326. | (amp->amp_vol[index][1] & 32);
  327. return awacs_amp_set_vol(amp, index, vol[0], vol[1], 1);
  328. }
  329. static int snd_pmac_awacs_get_switch_amp(struct snd_kcontrol *kcontrol,
  330. struct snd_ctl_elem_value *ucontrol)
  331. {
  332. struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
  333. int index = kcontrol->private_value;
  334. struct awacs_amp *amp = chip->mixer_data;
  335. snd_assert(amp, return -EINVAL);
  336. snd_assert(index >= 0 && index <= 1, return -EINVAL);
  337. ucontrol->value.integer.value[0] = (amp->amp_vol[index][0] & 32)
  338. ? 0 : 1;
  339. ucontrol->value.integer.value[1] = (amp->amp_vol[index][1] & 32)
  340. ? 0 : 1;
  341. return 0;
  342. }
  343. static int snd_pmac_awacs_put_switch_amp(struct snd_kcontrol *kcontrol,
  344. struct snd_ctl_elem_value *ucontrol)
  345. {
  346. struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
  347. int index = kcontrol->private_value;
  348. int vol[2];
  349. struct awacs_amp *amp = chip->mixer_data;
  350. snd_assert(amp, return -EINVAL);
  351. snd_assert(index >= 0 && index <= 1, return -EINVAL);
  352. vol[0] = (ucontrol->value.integer.value[0] ? 0 : 32)
  353. | (amp->amp_vol[index][0] & 31);
  354. vol[1] = (ucontrol->value.integer.value[1] ? 0 : 32)
  355. | (amp->amp_vol[index][1] & 31);
  356. return awacs_amp_set_vol(amp, index, vol[0], vol[1], 1);
  357. }
  358. static int snd_pmac_awacs_info_tone_amp(struct snd_kcontrol *kcontrol,
  359. struct snd_ctl_elem_info *uinfo)
  360. {
  361. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  362. uinfo->count = 1;
  363. uinfo->value.integer.min = 0;
  364. uinfo->value.integer.max = 14;
  365. return 0;
  366. }
  367. static int snd_pmac_awacs_get_tone_amp(struct snd_kcontrol *kcontrol,
  368. struct snd_ctl_elem_value *ucontrol)
  369. {
  370. struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
  371. int index = kcontrol->private_value;
  372. struct awacs_amp *amp = chip->mixer_data;
  373. snd_assert(amp, return -EINVAL);
  374. snd_assert(index >= 0 && index <= 1, return -EINVAL);
  375. ucontrol->value.integer.value[0] = amp->amp_tone[index];
  376. return 0;
  377. }
  378. static int snd_pmac_awacs_put_tone_amp(struct snd_kcontrol *kcontrol,
  379. struct snd_ctl_elem_value *ucontrol)
  380. {
  381. struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
  382. int index = kcontrol->private_value;
  383. struct awacs_amp *amp = chip->mixer_data;
  384. unsigned int val;
  385. snd_assert(amp, return -EINVAL);
  386. snd_assert(index >= 0 && index <= 1, return -EINVAL);
  387. val = ucontrol->value.integer.value[0];
  388. if (val > 14)
  389. return -EINVAL;
  390. if (val != amp->amp_tone[index]) {
  391. amp->amp_tone[index] = val;
  392. awacs_amp_set_tone(amp, amp->amp_tone[0], amp->amp_tone[1]);
  393. return 1;
  394. }
  395. return 0;
  396. }
  397. static int snd_pmac_awacs_info_master_amp(struct snd_kcontrol *kcontrol,
  398. struct snd_ctl_elem_info *uinfo)
  399. {
  400. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  401. uinfo->count = 1;
  402. uinfo->value.integer.min = 0;
  403. uinfo->value.integer.max = 99;
  404. return 0;
  405. }
  406. static int snd_pmac_awacs_get_master_amp(struct snd_kcontrol *kcontrol,
  407. struct snd_ctl_elem_value *ucontrol)
  408. {
  409. struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
  410. struct awacs_amp *amp = chip->mixer_data;
  411. snd_assert(amp, return -EINVAL);
  412. ucontrol->value.integer.value[0] = amp->amp_master;
  413. return 0;
  414. }
  415. static int snd_pmac_awacs_put_master_amp(struct snd_kcontrol *kcontrol,
  416. struct snd_ctl_elem_value *ucontrol)
  417. {
  418. struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
  419. struct awacs_amp *amp = chip->mixer_data;
  420. unsigned int val;
  421. snd_assert(amp, return -EINVAL);
  422. val = ucontrol->value.integer.value[0];
  423. if (val > 99)
  424. return -EINVAL;
  425. if (val != amp->amp_master) {
  426. amp->amp_master = val;
  427. awacs_amp_set_master(amp, amp->amp_master);
  428. return 1;
  429. }
  430. return 0;
  431. }
  432. #define AMP_CH_SPK 0
  433. #define AMP_CH_HD 1
  434. static struct snd_kcontrol_new snd_pmac_awacs_amp_vol[] __initdata = {
  435. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  436. .name = "PC Speaker 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_SPK,
  441. },
  442. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  443. .name = "Headphone Playback Volume",
  444. .info = snd_pmac_awacs_info_volume_amp,
  445. .get = snd_pmac_awacs_get_volume_amp,
  446. .put = snd_pmac_awacs_put_volume_amp,
  447. .private_value = AMP_CH_HD,
  448. },
  449. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  450. .name = "Tone Control - Bass",
  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 = 0,
  455. },
  456. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  457. .name = "Tone Control - Treble",
  458. .info = snd_pmac_awacs_info_tone_amp,
  459. .get = snd_pmac_awacs_get_tone_amp,
  460. .put = snd_pmac_awacs_put_tone_amp,
  461. .private_value = 1,
  462. },
  463. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  464. .name = "Amp Master Playback Volume",
  465. .info = snd_pmac_awacs_info_master_amp,
  466. .get = snd_pmac_awacs_get_master_amp,
  467. .put = snd_pmac_awacs_put_master_amp,
  468. },
  469. };
  470. static struct snd_kcontrol_new snd_pmac_awacs_amp_hp_sw __initdata = {
  471. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  472. .name = "Headphone Playback Switch",
  473. .info = snd_pmac_boolean_stereo_info,
  474. .get = snd_pmac_awacs_get_switch_amp,
  475. .put = snd_pmac_awacs_put_switch_amp,
  476. .private_value = AMP_CH_HD,
  477. };
  478. static struct snd_kcontrol_new snd_pmac_awacs_amp_spk_sw __initdata = {
  479. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  480. .name = "PC Speaker Playback Switch",
  481. .info = snd_pmac_boolean_stereo_info,
  482. .get = snd_pmac_awacs_get_switch_amp,
  483. .put = snd_pmac_awacs_put_switch_amp,
  484. .private_value = AMP_CH_SPK,
  485. };
  486. #endif /* PMAC_AMP_AVAIL */
  487. /*
  488. * mic boost for screamer
  489. */
  490. static int snd_pmac_screamer_mic_boost_info(struct snd_kcontrol *kcontrol,
  491. struct snd_ctl_elem_info *uinfo)
  492. {
  493. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  494. uinfo->count = 1;
  495. uinfo->value.integer.min = 0;
  496. uinfo->value.integer.max = 3;
  497. return 0;
  498. }
  499. static int snd_pmac_screamer_mic_boost_get(struct snd_kcontrol *kcontrol,
  500. struct snd_ctl_elem_value *ucontrol)
  501. {
  502. struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
  503. int val = 0;
  504. unsigned long flags;
  505. spin_lock_irqsave(&chip->reg_lock, flags);
  506. if (chip->awacs_reg[6] & MASK_MIC_BOOST)
  507. val |= 2;
  508. if (chip->awacs_reg[0] & MASK_GAINLINE)
  509. val |= 1;
  510. spin_unlock_irqrestore(&chip->reg_lock, flags);
  511. ucontrol->value.integer.value[0] = val;
  512. return 0;
  513. }
  514. static int snd_pmac_screamer_mic_boost_put(struct snd_kcontrol *kcontrol,
  515. struct snd_ctl_elem_value *ucontrol)
  516. {
  517. struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
  518. int changed = 0;
  519. int val0, val6;
  520. unsigned long flags;
  521. spin_lock_irqsave(&chip->reg_lock, flags);
  522. val0 = chip->awacs_reg[0] & ~MASK_GAINLINE;
  523. val6 = chip->awacs_reg[6] & ~MASK_MIC_BOOST;
  524. if (ucontrol->value.integer.value[0] & 1)
  525. val0 |= MASK_GAINLINE;
  526. if (ucontrol->value.integer.value[0] & 2)
  527. val6 |= MASK_MIC_BOOST;
  528. if (val0 != chip->awacs_reg[0]) {
  529. snd_pmac_awacs_write_reg(chip, 0, val0);
  530. changed = 1;
  531. }
  532. if (val6 != chip->awacs_reg[6]) {
  533. snd_pmac_awacs_write_reg(chip, 6, val6);
  534. changed = 1;
  535. }
  536. spin_unlock_irqrestore(&chip->reg_lock, flags);
  537. return changed;
  538. }
  539. /*
  540. * lists of mixer elements
  541. */
  542. static struct snd_kcontrol_new snd_pmac_awacs_mixers[] __initdata = {
  543. AWACS_SWITCH("Master Capture Switch", 1, SHIFT_LOOPTHRU, 0),
  544. AWACS_VOLUME("Master Capture Volume", 0, 4, 0),
  545. /* AWACS_SWITCH("Unknown Playback Switch", 6, SHIFT_PAROUT0, 0), */
  546. };
  547. static struct snd_kcontrol_new snd_pmac_screamer_mixers_beige[] __initdata = {
  548. AWACS_VOLUME("Master Playback Volume", 2, 6, 1),
  549. AWACS_VOLUME("Play-through Playback Volume", 5, 6, 1),
  550. AWACS_SWITCH("Line Capture Switch", 0, SHIFT_MUX_MIC, 0),
  551. AWACS_SWITCH("CD Capture Switch", 0, SHIFT_MUX_LINE, 0),
  552. };
  553. static struct snd_kcontrol_new snd_pmac_screamer_mixers_imac[] __initdata = {
  554. AWACS_VOLUME("Line out Playback Volume", 2, 6, 1),
  555. AWACS_VOLUME("Master Playback Volume", 5, 6, 1),
  556. AWACS_SWITCH("CD Capture Switch", 0, SHIFT_MUX_CD, 0),
  557. };
  558. static struct snd_kcontrol_new snd_pmac_awacs_mixers_pmac7500[] __initdata = {
  559. AWACS_VOLUME("Line out Playback Volume", 2, 6, 1),
  560. AWACS_SWITCH("CD Capture Switch", 0, SHIFT_MUX_CD, 0),
  561. AWACS_SWITCH("Line Capture Switch", 0, SHIFT_MUX_MIC, 0),
  562. };
  563. static struct snd_kcontrol_new snd_pmac_awacs_mixers_pmac[] __initdata = {
  564. AWACS_VOLUME("Master Playback Volume", 2, 6, 1),
  565. AWACS_SWITCH("CD Capture Switch", 0, SHIFT_MUX_CD, 0),
  566. };
  567. /* FIXME: is this correct order?
  568. * screamer (powerbook G3 pismo) seems to have different bits...
  569. */
  570. static struct snd_kcontrol_new snd_pmac_awacs_mixers2[] __initdata = {
  571. AWACS_SWITCH("Line Capture Switch", 0, SHIFT_MUX_LINE, 0),
  572. AWACS_SWITCH("Mic Capture Switch", 0, SHIFT_MUX_MIC, 0),
  573. };
  574. static struct snd_kcontrol_new snd_pmac_screamer_mixers2[] __initdata = {
  575. AWACS_SWITCH("Line Capture Switch", 0, SHIFT_MUX_MIC, 0),
  576. AWACS_SWITCH("Mic Capture Switch", 0, SHIFT_MUX_LINE, 0),
  577. };
  578. static struct snd_kcontrol_new snd_pmac_awacs_master_sw __initdata =
  579. AWACS_SWITCH("Master Playback Switch", 1, SHIFT_HDMUTE, 1);
  580. static struct snd_kcontrol_new snd_pmac_awacs_master_sw_imac __initdata =
  581. AWACS_SWITCH("Line out Playback Switch", 1, SHIFT_HDMUTE, 1);
  582. static struct snd_kcontrol_new snd_pmac_awacs_mic_boost[] __initdata = {
  583. AWACS_SWITCH("Mic Boost Capture Switch", 0, SHIFT_GAINLINE, 0),
  584. };
  585. static struct snd_kcontrol_new snd_pmac_screamer_mic_boost[] __initdata = {
  586. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  587. .name = "Mic Boost Capture Volume",
  588. .info = snd_pmac_screamer_mic_boost_info,
  589. .get = snd_pmac_screamer_mic_boost_get,
  590. .put = snd_pmac_screamer_mic_boost_put,
  591. },
  592. };
  593. static struct snd_kcontrol_new snd_pmac_awacs_mic_boost_pmac7500[] __initdata =
  594. {
  595. AWACS_SWITCH("Line Boost Capture Switch", 0, SHIFT_GAINLINE, 0),
  596. };
  597. static struct snd_kcontrol_new snd_pmac_screamer_mic_boost_beige[] __initdata =
  598. {
  599. AWACS_SWITCH("Line Boost Capture Switch", 0, SHIFT_GAINLINE, 0),
  600. AWACS_SWITCH("CD Boost Capture Switch", 6, SHIFT_MIC_BOOST, 0),
  601. };
  602. static struct snd_kcontrol_new snd_pmac_screamer_mic_boost_imac[] __initdata =
  603. {
  604. AWACS_SWITCH("Line Boost Capture Switch", 0, SHIFT_GAINLINE, 0),
  605. AWACS_SWITCH("Mic Boost Capture Switch", 6, SHIFT_MIC_BOOST, 0),
  606. };
  607. static struct snd_kcontrol_new snd_pmac_awacs_speaker_vol[] __initdata = {
  608. AWACS_VOLUME("PC Speaker Playback Volume", 4, 6, 1),
  609. };
  610. static struct snd_kcontrol_new snd_pmac_awacs_speaker_sw __initdata =
  611. AWACS_SWITCH("PC Speaker Playback Switch", 1, SHIFT_SPKMUTE, 1);
  612. static struct snd_kcontrol_new snd_pmac_awacs_speaker_sw_imac __initdata =
  613. AWACS_SWITCH("PC Speaker Playback Switch", 1, SHIFT_PAROUT1, 0);
  614. /*
  615. * add new mixer elements to the card
  616. */
  617. static int build_mixers(struct snd_pmac *chip, int nums,
  618. struct snd_kcontrol_new *mixers)
  619. {
  620. int i, err;
  621. for (i = 0; i < nums; i++) {
  622. err = snd_ctl_add(chip->card, snd_ctl_new1(&mixers[i], chip));
  623. if (err < 0)
  624. return err;
  625. }
  626. return 0;
  627. }
  628. /*
  629. * restore all registers
  630. */
  631. static void awacs_restore_all_regs(struct snd_pmac *chip)
  632. {
  633. snd_pmac_awacs_write_noreg(chip, 0, chip->awacs_reg[0]);
  634. snd_pmac_awacs_write_noreg(chip, 1, chip->awacs_reg[1]);
  635. snd_pmac_awacs_write_noreg(chip, 2, chip->awacs_reg[2]);
  636. snd_pmac_awacs_write_noreg(chip, 4, chip->awacs_reg[4]);
  637. if (chip->model == PMAC_SCREAMER) {
  638. snd_pmac_awacs_write_noreg(chip, 5, chip->awacs_reg[5]);
  639. snd_pmac_awacs_write_noreg(chip, 6, chip->awacs_reg[6]);
  640. snd_pmac_awacs_write_noreg(chip, 7, chip->awacs_reg[7]);
  641. }
  642. }
  643. #ifdef CONFIG_PM
  644. static void snd_pmac_awacs_suspend(struct snd_pmac *chip)
  645. {
  646. snd_pmac_awacs_write_noreg(chip, 1, (chip->awacs_reg[1]
  647. | MASK_AMUTE | MASK_CMUTE));
  648. }
  649. static void snd_pmac_awacs_resume(struct snd_pmac *chip)
  650. {
  651. if (machine_is_compatible("PowerBook3,1")
  652. || machine_is_compatible("PowerBook3,2")) {
  653. msleep(100);
  654. snd_pmac_awacs_write_reg(chip, 1,
  655. chip->awacs_reg[1] & ~MASK_PAROUT);
  656. msleep(300);
  657. }
  658. awacs_restore_all_regs(chip);
  659. if (chip->model == PMAC_SCREAMER) {
  660. /* reset power bits in reg 6 */
  661. mdelay(5);
  662. snd_pmac_awacs_write_noreg(chip, 6, chip->awacs_reg[6]);
  663. }
  664. screamer_recalibrate(chip);
  665. #ifdef PMAC_AMP_AVAIL
  666. if (chip->mixer_data) {
  667. struct awacs_amp *amp = chip->mixer_data;
  668. awacs_amp_set_vol(amp, 0,
  669. amp->amp_vol[0][0], amp->amp_vol[0][1], 0);
  670. awacs_amp_set_vol(amp, 1,
  671. amp->amp_vol[1][0], amp->amp_vol[1][1], 0);
  672. awacs_amp_set_tone(amp, amp->amp_tone[0], amp->amp_tone[1]);
  673. awacs_amp_set_master(amp, amp->amp_master);
  674. }
  675. #endif
  676. }
  677. #endif /* CONFIG_PM */
  678. #define IS_PM7500 (machine_is_compatible("AAPL,7500"))
  679. #define IS_BEIGE (machine_is_compatible("AAPL,Gossamer"))
  680. #define IS_IMAC (machine_is_compatible("PowerMac2,1") \
  681. || machine_is_compatible("PowerMac2,2") \
  682. || machine_is_compatible("PowerMac4,1"))
  683. static int imac;
  684. #ifdef PMAC_SUPPORT_AUTOMUTE
  685. /*
  686. * auto-mute stuffs
  687. */
  688. static int snd_pmac_awacs_detect_headphone(struct snd_pmac *chip)
  689. {
  690. return (in_le32(&chip->awacs->codec_stat) & chip->hp_stat_mask) ? 1 : 0;
  691. }
  692. #ifdef PMAC_AMP_AVAIL
  693. static int toggle_amp_mute(struct awacs_amp *amp, int index, int mute)
  694. {
  695. int vol[2];
  696. vol[0] = amp->amp_vol[index][0] & 31;
  697. vol[1] = amp->amp_vol[index][1] & 31;
  698. if (mute) {
  699. vol[0] |= 32;
  700. vol[1] |= 32;
  701. }
  702. return awacs_amp_set_vol(amp, index, vol[0], vol[1], 1);
  703. }
  704. #endif
  705. static void snd_pmac_awacs_update_automute(struct snd_pmac *chip, int do_notify)
  706. {
  707. if (chip->auto_mute) {
  708. #ifdef PMAC_AMP_AVAIL
  709. if (chip->mixer_data) {
  710. struct awacs_amp *amp = chip->mixer_data;
  711. int changed;
  712. if (snd_pmac_awacs_detect_headphone(chip)) {
  713. changed = toggle_amp_mute(amp, AMP_CH_HD, 0);
  714. changed |= toggle_amp_mute(amp, AMP_CH_SPK, 1);
  715. } else {
  716. changed = toggle_amp_mute(amp, AMP_CH_HD, 1);
  717. changed |= toggle_amp_mute(amp, AMP_CH_SPK, 0);
  718. }
  719. if (do_notify && ! changed)
  720. return;
  721. } else
  722. #endif
  723. {
  724. int reg = chip->awacs_reg[1]
  725. | (MASK_HDMUTE | MASK_SPKMUTE);
  726. if (imac) {
  727. reg &= ~MASK_SPKMUTE;
  728. reg &= ~MASK_PAROUT1;
  729. }
  730. if (snd_pmac_awacs_detect_headphone(chip))
  731. reg &= ~MASK_HDMUTE;
  732. else if (imac)
  733. reg |= MASK_PAROUT1;
  734. else
  735. reg &= ~MASK_SPKMUTE;
  736. if (do_notify && reg == chip->awacs_reg[1])
  737. return;
  738. snd_pmac_awacs_write_reg(chip, 1, reg);
  739. }
  740. if (do_notify) {
  741. snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
  742. &chip->master_sw_ctl->id);
  743. snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
  744. &chip->speaker_sw_ctl->id);
  745. snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
  746. &chip->hp_detect_ctl->id);
  747. }
  748. }
  749. }
  750. #endif /* PMAC_SUPPORT_AUTOMUTE */
  751. /*
  752. * initialize chip
  753. */
  754. int __init
  755. snd_pmac_awacs_init(struct snd_pmac *chip)
  756. {
  757. int pm7500 = IS_PM7500;
  758. int beige = IS_BEIGE;
  759. int err, vol;
  760. imac = IS_IMAC;
  761. /* looks like MASK_GAINLINE triggers something, so we set here
  762. * as start-up
  763. */
  764. chip->awacs_reg[0] = MASK_MUX_CD | 0xff | MASK_GAINLINE;
  765. chip->awacs_reg[1] = MASK_CMUTE | MASK_AMUTE;
  766. /* FIXME: Only machines with external SRS module need MASK_PAROUT */
  767. if (chip->has_iic || chip->device_id == 0x5 ||
  768. /* chip->_device_id == 0x8 || */
  769. chip->device_id == 0xb)
  770. chip->awacs_reg[1] |= MASK_PAROUT;
  771. /* get default volume from nvram */
  772. // vol = (~nvram_read_byte(0x1308) & 7) << 1;
  773. // vol = ((pmac_xpram_read( 8 ) & 7 ) << 1 );
  774. vol = 0x0f; /* no, on alsa, muted as default */
  775. vol = vol + (vol << 6);
  776. chip->awacs_reg[2] = vol;
  777. chip->awacs_reg[4] = vol;
  778. if (chip->model == PMAC_SCREAMER) {
  779. /* FIXME: screamer has loopthru vol control */
  780. chip->awacs_reg[5] = vol;
  781. /* FIXME: maybe should be vol << 3 for PCMCIA speaker */
  782. chip->awacs_reg[6] = MASK_MIC_BOOST;
  783. chip->awacs_reg[7] = 0;
  784. }
  785. awacs_restore_all_regs(chip);
  786. chip->manufacturer = (in_le32(&chip->awacs->codec_stat) >> 8) & 0xf;
  787. screamer_recalibrate(chip);
  788. chip->revision = (in_le32(&chip->awacs->codec_stat) >> 12) & 0xf;
  789. #ifdef PMAC_AMP_AVAIL
  790. if (chip->revision == 3 && chip->has_iic && CHECK_CUDA_AMP()) {
  791. struct awacs_amp *amp = kzalloc(sizeof(*amp), GFP_KERNEL);
  792. if (! amp)
  793. return -ENOMEM;
  794. chip->mixer_data = amp;
  795. chip->mixer_free = awacs_amp_free;
  796. /* mute and zero vol */
  797. awacs_amp_set_vol(amp, 0, 63, 63, 0);
  798. awacs_amp_set_vol(amp, 1, 63, 63, 0);
  799. awacs_amp_set_tone(amp, 7, 7); /* 0 dB */
  800. awacs_amp_set_master(amp, 79); /* 0 dB */
  801. }
  802. #endif /* PMAC_AMP_AVAIL */
  803. if (chip->hp_stat_mask == 0) {
  804. /* set headphone-jack detection bit */
  805. switch (chip->model) {
  806. case PMAC_AWACS:
  807. chip->hp_stat_mask = pm7500 ? MASK_HDPCONN
  808. : MASK_LOCONN;
  809. break;
  810. case PMAC_SCREAMER:
  811. switch (chip->device_id) {
  812. case 0x08:
  813. case 0x0B:
  814. chip->hp_stat_mask = imac
  815. ? MASK_LOCONN_IMAC |
  816. MASK_HDPLCONN_IMAC |
  817. MASK_HDPRCONN_IMAC
  818. : MASK_HDPCONN;
  819. break;
  820. case 0x00:
  821. case 0x05:
  822. chip->hp_stat_mask = MASK_LOCONN;
  823. break;
  824. default:
  825. chip->hp_stat_mask = MASK_HDPCONN;
  826. break;
  827. }
  828. break;
  829. default:
  830. snd_BUG();
  831. break;
  832. }
  833. }
  834. /*
  835. * build mixers
  836. */
  837. strcpy(chip->card->mixername, "PowerMac AWACS");
  838. err = build_mixers(chip, ARRAY_SIZE(snd_pmac_awacs_mixers),
  839. snd_pmac_awacs_mixers);
  840. if (err < 0)
  841. return err;
  842. if (beige)
  843. ;
  844. else if (chip->model == PMAC_SCREAMER)
  845. err = build_mixers(chip, ARRAY_SIZE(snd_pmac_screamer_mixers2),
  846. snd_pmac_screamer_mixers2);
  847. else if (!pm7500)
  848. err = build_mixers(chip, ARRAY_SIZE(snd_pmac_awacs_mixers2),
  849. snd_pmac_awacs_mixers2);
  850. if (err < 0)
  851. return err;
  852. if (pm7500)
  853. err = build_mixers(chip,
  854. ARRAY_SIZE(snd_pmac_awacs_mixers_pmac7500),
  855. snd_pmac_awacs_mixers_pmac7500);
  856. else if (beige)
  857. err = build_mixers(chip,
  858. ARRAY_SIZE(snd_pmac_screamer_mixers_beige),
  859. snd_pmac_screamer_mixers_beige);
  860. else if (imac)
  861. err = build_mixers(chip,
  862. ARRAY_SIZE(snd_pmac_screamer_mixers_imac),
  863. snd_pmac_screamer_mixers_imac);
  864. else
  865. err = build_mixers(chip,
  866. ARRAY_SIZE(snd_pmac_awacs_mixers_pmac),
  867. snd_pmac_awacs_mixers_pmac);
  868. if (err < 0)
  869. return err;
  870. chip->master_sw_ctl = snd_ctl_new1((pm7500 || imac)
  871. ? &snd_pmac_awacs_master_sw_imac
  872. : &snd_pmac_awacs_master_sw, chip);
  873. err = snd_ctl_add(chip->card, chip->master_sw_ctl);
  874. if (err < 0)
  875. return err;
  876. #ifdef PMAC_AMP_AVAIL
  877. if (chip->mixer_data) {
  878. /* use amplifier. the signal is connected from route A
  879. * to the amp. the amp has its headphone and speaker
  880. * volumes and mute switches, so we use them instead of
  881. * screamer registers.
  882. * in this case, it seems the route C is not used.
  883. */
  884. err = build_mixers(chip, ARRAY_SIZE(snd_pmac_awacs_amp_vol),
  885. snd_pmac_awacs_amp_vol);
  886. if (err < 0)
  887. return err;
  888. /* overwrite */
  889. chip->master_sw_ctl = snd_ctl_new1(&snd_pmac_awacs_amp_hp_sw,
  890. chip);
  891. err = snd_ctl_add(chip->card, chip->master_sw_ctl);
  892. if (err < 0)
  893. return err;
  894. chip->speaker_sw_ctl = snd_ctl_new1(&snd_pmac_awacs_amp_spk_sw,
  895. chip);
  896. err = snd_ctl_add(chip->card, chip->speaker_sw_ctl);
  897. if (err < 0)
  898. return err;
  899. } else
  900. #endif /* PMAC_AMP_AVAIL */
  901. {
  902. /* route A = headphone, route C = speaker */
  903. err = build_mixers(chip, ARRAY_SIZE(snd_pmac_awacs_speaker_vol),
  904. snd_pmac_awacs_speaker_vol);
  905. if (err < 0)
  906. return err;
  907. chip->speaker_sw_ctl = snd_ctl_new1(imac
  908. ? &snd_pmac_awacs_speaker_sw_imac
  909. : &snd_pmac_awacs_speaker_sw, chip);
  910. err = snd_ctl_add(chip->card, chip->speaker_sw_ctl);
  911. if (err < 0)
  912. return err;
  913. }
  914. if (beige)
  915. err = build_mixers(chip,
  916. ARRAY_SIZE(snd_pmac_screamer_mic_boost_beige),
  917. snd_pmac_screamer_mic_boost_beige);
  918. else if (imac)
  919. err = build_mixers(chip,
  920. ARRAY_SIZE(snd_pmac_screamer_mic_boost_imac),
  921. snd_pmac_screamer_mic_boost_imac);
  922. else if (chip->model == PMAC_SCREAMER)
  923. err = build_mixers(chip,
  924. ARRAY_SIZE(snd_pmac_screamer_mic_boost),
  925. snd_pmac_screamer_mic_boost);
  926. else if (pm7500)
  927. err = build_mixers(chip,
  928. ARRAY_SIZE(snd_pmac_awacs_mic_boost_pmac7500),
  929. snd_pmac_awacs_mic_boost_pmac7500);
  930. else
  931. err = build_mixers(chip, ARRAY_SIZE(snd_pmac_awacs_mic_boost),
  932. snd_pmac_awacs_mic_boost);
  933. if (err < 0)
  934. return err;
  935. /*
  936. * set lowlevel callbacks
  937. */
  938. chip->set_format = snd_pmac_awacs_set_format;
  939. #ifdef CONFIG_PM
  940. chip->suspend = snd_pmac_awacs_suspend;
  941. chip->resume = snd_pmac_awacs_resume;
  942. #endif
  943. #ifdef PMAC_SUPPORT_AUTOMUTE
  944. err = snd_pmac_add_automute(chip);
  945. if (err < 0)
  946. return err;
  947. chip->detect_headphone = snd_pmac_awacs_detect_headphone;
  948. chip->update_automute = snd_pmac_awacs_update_automute;
  949. snd_pmac_awacs_update_automute(chip, 0); /* update the status only */
  950. #endif
  951. if (chip->model == PMAC_SCREAMER) {
  952. snd_pmac_awacs_write_noreg(chip, 6, chip->awacs_reg[6]);
  953. snd_pmac_awacs_write_noreg(chip, 0, chip->awacs_reg[0]);
  954. }
  955. return 0;
  956. }