tumbler.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450
  1. /*
  2. * PMac Tumbler/Snapper lowlevel functions
  3. *
  4. * Copyright (c) by Takashi Iwai <tiwai@suse.de>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. *
  20. * Rene Rebe <rene.rebe@gmx.net>:
  21. * * update from shadow registers on wakeup and headphone plug
  22. * * automatically toggle DRC on headphone plug
  23. *
  24. */
  25. #include <sound/driver.h>
  26. #include <linux/init.h>
  27. #include <linux/delay.h>
  28. #include <linux/i2c.h>
  29. #include <linux/kmod.h>
  30. #include <linux/slab.h>
  31. #include <linux/interrupt.h>
  32. #include <sound/core.h>
  33. #include <asm/io.h>
  34. #include <asm/irq.h>
  35. #include <asm/machdep.h>
  36. #include <asm/pmac_feature.h>
  37. #include "pmac.h"
  38. #include "tumbler_volume.h"
  39. #undef DEBUG
  40. #ifdef DEBUG
  41. #define DBG(fmt...) printk(fmt)
  42. #else
  43. #define DBG(fmt...)
  44. #endif
  45. /* i2c address for tumbler */
  46. #define TAS_I2C_ADDR 0x34
  47. /* registers */
  48. #define TAS_REG_MCS 0x01 /* main control */
  49. #define TAS_REG_DRC 0x02
  50. #define TAS_REG_VOL 0x04
  51. #define TAS_REG_TREBLE 0x05
  52. #define TAS_REG_BASS 0x06
  53. #define TAS_REG_INPUT1 0x07
  54. #define TAS_REG_INPUT2 0x08
  55. /* tas3001c */
  56. #define TAS_REG_PCM TAS_REG_INPUT1
  57. /* tas3004 */
  58. #define TAS_REG_LMIX TAS_REG_INPUT1
  59. #define TAS_REG_RMIX TAS_REG_INPUT2
  60. #define TAS_REG_MCS2 0x43 /* main control 2 */
  61. #define TAS_REG_ACS 0x40 /* analog control */
  62. /* mono volumes for tas3001c/tas3004 */
  63. enum {
  64. VOL_IDX_PCM_MONO, /* tas3001c only */
  65. VOL_IDX_BASS, VOL_IDX_TREBLE,
  66. VOL_IDX_LAST_MONO
  67. };
  68. /* stereo volumes for tas3004 */
  69. enum {
  70. VOL_IDX_PCM, VOL_IDX_PCM2, VOL_IDX_ADC,
  71. VOL_IDX_LAST_MIX
  72. };
  73. struct pmac_gpio {
  74. unsigned int addr;
  75. u8 active_val;
  76. u8 inactive_val;
  77. u8 active_state;
  78. };
  79. struct pmac_tumbler {
  80. struct pmac_keywest i2c;
  81. struct pmac_gpio audio_reset;
  82. struct pmac_gpio amp_mute;
  83. struct pmac_gpio line_mute;
  84. struct pmac_gpio line_detect;
  85. struct pmac_gpio hp_mute;
  86. struct pmac_gpio hp_detect;
  87. int headphone_irq;
  88. int lineout_irq;
  89. unsigned int save_master_vol[2];
  90. unsigned int master_vol[2];
  91. unsigned int save_master_switch[2];
  92. unsigned int master_switch[2];
  93. unsigned int mono_vol[VOL_IDX_LAST_MONO];
  94. unsigned int mix_vol[VOL_IDX_LAST_MIX][2]; /* stereo volumes for tas3004 */
  95. int drc_range;
  96. int drc_enable;
  97. int capture_source;
  98. int anded_reset;
  99. int auto_mute_notify;
  100. int reset_on_sleep;
  101. u8 acs;
  102. };
  103. /*
  104. */
  105. static int send_init_client(struct pmac_keywest *i2c, unsigned int *regs)
  106. {
  107. while (*regs > 0) {
  108. int err, count = 10;
  109. do {
  110. err = i2c_smbus_write_byte_data(i2c->client,
  111. regs[0], regs[1]);
  112. if (err >= 0)
  113. break;
  114. DBG("(W) i2c error %d\n", err);
  115. mdelay(10);
  116. } while (count--);
  117. if (err < 0)
  118. return -ENXIO;
  119. regs += 2;
  120. }
  121. return 0;
  122. }
  123. static int tumbler_init_client(struct pmac_keywest *i2c)
  124. {
  125. static unsigned int regs[] = {
  126. /* normal operation, SCLK=64fps, i2s output, i2s input, 16bit width */
  127. TAS_REG_MCS, (1<<6)|(2<<4)|(2<<2)|0,
  128. 0, /* terminator */
  129. };
  130. DBG("(I) tumbler init client\n");
  131. return send_init_client(i2c, regs);
  132. }
  133. static int snapper_init_client(struct pmac_keywest *i2c)
  134. {
  135. static unsigned int regs[] = {
  136. /* normal operation, SCLK=64fps, i2s output, 16bit width */
  137. TAS_REG_MCS, (1<<6)|(2<<4)|0,
  138. /* normal operation, all-pass mode */
  139. TAS_REG_MCS2, (1<<1),
  140. /* normal output, no deemphasis, A input, power-up, line-in */
  141. TAS_REG_ACS, 0,
  142. 0, /* terminator */
  143. };
  144. DBG("(I) snapper init client\n");
  145. return send_init_client(i2c, regs);
  146. }
  147. /*
  148. * gpio access
  149. */
  150. #define do_gpio_write(gp, val) \
  151. pmac_call_feature(PMAC_FTR_WRITE_GPIO, NULL, (gp)->addr, val)
  152. #define do_gpio_read(gp) \
  153. pmac_call_feature(PMAC_FTR_READ_GPIO, NULL, (gp)->addr, 0)
  154. #define tumbler_gpio_free(gp) /* NOP */
  155. static void write_audio_gpio(struct pmac_gpio *gp, int active)
  156. {
  157. if (! gp->addr)
  158. return;
  159. active = active ? gp->active_val : gp->inactive_val;
  160. do_gpio_write(gp, active);
  161. DBG("(I) gpio %x write %d\n", gp->addr, active);
  162. }
  163. static int check_audio_gpio(struct pmac_gpio *gp)
  164. {
  165. int ret;
  166. if (! gp->addr)
  167. return 0;
  168. ret = do_gpio_read(gp);
  169. return (ret & 0xd) == (gp->active_val & 0xd);
  170. }
  171. static int read_audio_gpio(struct pmac_gpio *gp)
  172. {
  173. int ret;
  174. if (! gp->addr)
  175. return 0;
  176. ret = ((do_gpio_read(gp) & 0x02) !=0);
  177. return ret == gp->active_state;
  178. }
  179. /*
  180. * update master volume
  181. */
  182. static int tumbler_set_master_volume(struct pmac_tumbler *mix)
  183. {
  184. unsigned char block[6];
  185. unsigned int left_vol, right_vol;
  186. if (! mix->i2c.client)
  187. return -ENODEV;
  188. if (! mix->master_switch[0])
  189. left_vol = 0;
  190. else {
  191. left_vol = mix->master_vol[0];
  192. if (left_vol >= ARRAY_SIZE(master_volume_table))
  193. left_vol = ARRAY_SIZE(master_volume_table) - 1;
  194. left_vol = master_volume_table[left_vol];
  195. }
  196. if (! mix->master_switch[1])
  197. right_vol = 0;
  198. else {
  199. right_vol = mix->master_vol[1];
  200. if (right_vol >= ARRAY_SIZE(master_volume_table))
  201. right_vol = ARRAY_SIZE(master_volume_table) - 1;
  202. right_vol = master_volume_table[right_vol];
  203. }
  204. block[0] = (left_vol >> 16) & 0xff;
  205. block[1] = (left_vol >> 8) & 0xff;
  206. block[2] = (left_vol >> 0) & 0xff;
  207. block[3] = (right_vol >> 16) & 0xff;
  208. block[4] = (right_vol >> 8) & 0xff;
  209. block[5] = (right_vol >> 0) & 0xff;
  210. if (i2c_smbus_write_i2c_block_data(mix->i2c.client, TAS_REG_VOL, 6,
  211. block) < 0) {
  212. snd_printk("failed to set volume \n");
  213. return -EINVAL;
  214. }
  215. return 0;
  216. }
  217. /* output volume */
  218. static int tumbler_info_master_volume(struct snd_kcontrol *kcontrol,
  219. struct snd_ctl_elem_info *uinfo)
  220. {
  221. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  222. uinfo->count = 2;
  223. uinfo->value.integer.min = 0;
  224. uinfo->value.integer.max = ARRAY_SIZE(master_volume_table) - 1;
  225. return 0;
  226. }
  227. static int tumbler_get_master_volume(struct snd_kcontrol *kcontrol,
  228. struct snd_ctl_elem_value *ucontrol)
  229. {
  230. struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
  231. struct pmac_tumbler *mix = chip->mixer_data;
  232. snd_assert(mix, return -ENODEV);
  233. ucontrol->value.integer.value[0] = mix->master_vol[0];
  234. ucontrol->value.integer.value[1] = mix->master_vol[1];
  235. return 0;
  236. }
  237. static int tumbler_put_master_volume(struct snd_kcontrol *kcontrol,
  238. struct snd_ctl_elem_value *ucontrol)
  239. {
  240. struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
  241. struct pmac_tumbler *mix = chip->mixer_data;
  242. int change;
  243. snd_assert(mix, return -ENODEV);
  244. change = mix->master_vol[0] != ucontrol->value.integer.value[0] ||
  245. mix->master_vol[1] != ucontrol->value.integer.value[1];
  246. if (change) {
  247. mix->master_vol[0] = ucontrol->value.integer.value[0];
  248. mix->master_vol[1] = ucontrol->value.integer.value[1];
  249. tumbler_set_master_volume(mix);
  250. }
  251. return change;
  252. }
  253. /* output switch */
  254. static int tumbler_get_master_switch(struct snd_kcontrol *kcontrol,
  255. struct snd_ctl_elem_value *ucontrol)
  256. {
  257. struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
  258. struct pmac_tumbler *mix = chip->mixer_data;
  259. snd_assert(mix, return -ENODEV);
  260. ucontrol->value.integer.value[0] = mix->master_switch[0];
  261. ucontrol->value.integer.value[1] = mix->master_switch[1];
  262. return 0;
  263. }
  264. static int tumbler_put_master_switch(struct snd_kcontrol *kcontrol,
  265. struct snd_ctl_elem_value *ucontrol)
  266. {
  267. struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
  268. struct pmac_tumbler *mix = chip->mixer_data;
  269. int change;
  270. snd_assert(mix, return -ENODEV);
  271. change = mix->master_switch[0] != ucontrol->value.integer.value[0] ||
  272. mix->master_switch[1] != ucontrol->value.integer.value[1];
  273. if (change) {
  274. mix->master_switch[0] = !!ucontrol->value.integer.value[0];
  275. mix->master_switch[1] = !!ucontrol->value.integer.value[1];
  276. tumbler_set_master_volume(mix);
  277. }
  278. return change;
  279. }
  280. /*
  281. * TAS3001c dynamic range compression
  282. */
  283. #define TAS3001_DRC_MAX 0x5f
  284. static int tumbler_set_drc(struct pmac_tumbler *mix)
  285. {
  286. unsigned char val[2];
  287. if (! mix->i2c.client)
  288. return -ENODEV;
  289. if (mix->drc_enable) {
  290. val[0] = 0xc1; /* enable, 3:1 compression */
  291. if (mix->drc_range > TAS3001_DRC_MAX)
  292. val[1] = 0xf0;
  293. else if (mix->drc_range < 0)
  294. val[1] = 0x91;
  295. else
  296. val[1] = mix->drc_range + 0x91;
  297. } else {
  298. val[0] = 0;
  299. val[1] = 0;
  300. }
  301. if (i2c_smbus_write_i2c_block_data(mix->i2c.client, TAS_REG_DRC,
  302. 2, val) < 0) {
  303. snd_printk("failed to set DRC\n");
  304. return -EINVAL;
  305. }
  306. return 0;
  307. }
  308. /*
  309. * TAS3004
  310. */
  311. #define TAS3004_DRC_MAX 0xef
  312. static int snapper_set_drc(struct pmac_tumbler *mix)
  313. {
  314. unsigned char val[6];
  315. if (! mix->i2c.client)
  316. return -ENODEV;
  317. if (mix->drc_enable)
  318. val[0] = 0x50; /* 3:1 above threshold */
  319. else
  320. val[0] = 0x51; /* disabled */
  321. val[1] = 0x02; /* 1:1 below threshold */
  322. if (mix->drc_range > 0xef)
  323. val[2] = 0xef;
  324. else if (mix->drc_range < 0)
  325. val[2] = 0x00;
  326. else
  327. val[2] = mix->drc_range;
  328. val[3] = 0xb0;
  329. val[4] = 0x60;
  330. val[5] = 0xa0;
  331. if (i2c_smbus_write_i2c_block_data(mix->i2c.client, TAS_REG_DRC,
  332. 6, val) < 0) {
  333. snd_printk("failed to set DRC\n");
  334. return -EINVAL;
  335. }
  336. return 0;
  337. }
  338. static int tumbler_info_drc_value(struct snd_kcontrol *kcontrol,
  339. struct snd_ctl_elem_info *uinfo)
  340. {
  341. struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
  342. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  343. uinfo->count = 1;
  344. uinfo->value.integer.min = 0;
  345. uinfo->value.integer.max =
  346. chip->model == PMAC_TUMBLER ? TAS3001_DRC_MAX : TAS3004_DRC_MAX;
  347. return 0;
  348. }
  349. static int tumbler_get_drc_value(struct snd_kcontrol *kcontrol,
  350. struct snd_ctl_elem_value *ucontrol)
  351. {
  352. struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
  353. struct pmac_tumbler *mix;
  354. if (! (mix = chip->mixer_data))
  355. return -ENODEV;
  356. ucontrol->value.integer.value[0] = mix->drc_range;
  357. return 0;
  358. }
  359. static int tumbler_put_drc_value(struct snd_kcontrol *kcontrol,
  360. struct snd_ctl_elem_value *ucontrol)
  361. {
  362. struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
  363. struct pmac_tumbler *mix;
  364. int change;
  365. if (! (mix = chip->mixer_data))
  366. return -ENODEV;
  367. change = mix->drc_range != ucontrol->value.integer.value[0];
  368. if (change) {
  369. mix->drc_range = ucontrol->value.integer.value[0];
  370. if (chip->model == PMAC_TUMBLER)
  371. tumbler_set_drc(mix);
  372. else
  373. snapper_set_drc(mix);
  374. }
  375. return change;
  376. }
  377. static int tumbler_get_drc_switch(struct snd_kcontrol *kcontrol,
  378. struct snd_ctl_elem_value *ucontrol)
  379. {
  380. struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
  381. struct pmac_tumbler *mix;
  382. if (! (mix = chip->mixer_data))
  383. return -ENODEV;
  384. ucontrol->value.integer.value[0] = mix->drc_enable;
  385. return 0;
  386. }
  387. static int tumbler_put_drc_switch(struct snd_kcontrol *kcontrol,
  388. struct snd_ctl_elem_value *ucontrol)
  389. {
  390. struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
  391. struct pmac_tumbler *mix;
  392. int change;
  393. if (! (mix = chip->mixer_data))
  394. return -ENODEV;
  395. change = mix->drc_enable != ucontrol->value.integer.value[0];
  396. if (change) {
  397. mix->drc_enable = !!ucontrol->value.integer.value[0];
  398. if (chip->model == PMAC_TUMBLER)
  399. tumbler_set_drc(mix);
  400. else
  401. snapper_set_drc(mix);
  402. }
  403. return change;
  404. }
  405. /*
  406. * mono volumes
  407. */
  408. struct tumbler_mono_vol {
  409. int index;
  410. int reg;
  411. int bytes;
  412. unsigned int max;
  413. unsigned int *table;
  414. };
  415. static int tumbler_set_mono_volume(struct pmac_tumbler *mix,
  416. struct tumbler_mono_vol *info)
  417. {
  418. unsigned char block[4];
  419. unsigned int vol;
  420. int i;
  421. if (! mix->i2c.client)
  422. return -ENODEV;
  423. vol = mix->mono_vol[info->index];
  424. if (vol >= info->max)
  425. vol = info->max - 1;
  426. vol = info->table[vol];
  427. for (i = 0; i < info->bytes; i++)
  428. block[i] = (vol >> ((info->bytes - i - 1) * 8)) & 0xff;
  429. if (i2c_smbus_write_i2c_block_data(mix->i2c.client, info->reg,
  430. info->bytes, block) < 0) {
  431. snd_printk("failed to set mono volume %d\n", info->index);
  432. return -EINVAL;
  433. }
  434. return 0;
  435. }
  436. static int tumbler_info_mono(struct snd_kcontrol *kcontrol,
  437. struct snd_ctl_elem_info *uinfo)
  438. {
  439. struct tumbler_mono_vol *info = (struct tumbler_mono_vol *)kcontrol->private_value;
  440. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  441. uinfo->count = 1;
  442. uinfo->value.integer.min = 0;
  443. uinfo->value.integer.max = info->max - 1;
  444. return 0;
  445. }
  446. static int tumbler_get_mono(struct snd_kcontrol *kcontrol,
  447. struct snd_ctl_elem_value *ucontrol)
  448. {
  449. struct tumbler_mono_vol *info = (struct tumbler_mono_vol *)kcontrol->private_value;
  450. struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
  451. struct pmac_tumbler *mix;
  452. if (! (mix = chip->mixer_data))
  453. return -ENODEV;
  454. ucontrol->value.integer.value[0] = mix->mono_vol[info->index];
  455. return 0;
  456. }
  457. static int tumbler_put_mono(struct snd_kcontrol *kcontrol,
  458. struct snd_ctl_elem_value *ucontrol)
  459. {
  460. struct tumbler_mono_vol *info = (struct tumbler_mono_vol *)kcontrol->private_value;
  461. struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
  462. struct pmac_tumbler *mix;
  463. int change;
  464. if (! (mix = chip->mixer_data))
  465. return -ENODEV;
  466. change = mix->mono_vol[info->index] != ucontrol->value.integer.value[0];
  467. if (change) {
  468. mix->mono_vol[info->index] = ucontrol->value.integer.value[0];
  469. tumbler_set_mono_volume(mix, info);
  470. }
  471. return change;
  472. }
  473. /* TAS3001c mono volumes */
  474. static struct tumbler_mono_vol tumbler_pcm_vol_info = {
  475. .index = VOL_IDX_PCM_MONO,
  476. .reg = TAS_REG_PCM,
  477. .bytes = 3,
  478. .max = ARRAY_SIZE(mixer_volume_table),
  479. .table = mixer_volume_table,
  480. };
  481. static struct tumbler_mono_vol tumbler_bass_vol_info = {
  482. .index = VOL_IDX_BASS,
  483. .reg = TAS_REG_BASS,
  484. .bytes = 1,
  485. .max = ARRAY_SIZE(bass_volume_table),
  486. .table = bass_volume_table,
  487. };
  488. static struct tumbler_mono_vol tumbler_treble_vol_info = {
  489. .index = VOL_IDX_TREBLE,
  490. .reg = TAS_REG_TREBLE,
  491. .bytes = 1,
  492. .max = ARRAY_SIZE(treble_volume_table),
  493. .table = treble_volume_table,
  494. };
  495. /* TAS3004 mono volumes */
  496. static struct tumbler_mono_vol snapper_bass_vol_info = {
  497. .index = VOL_IDX_BASS,
  498. .reg = TAS_REG_BASS,
  499. .bytes = 1,
  500. .max = ARRAY_SIZE(snapper_bass_volume_table),
  501. .table = snapper_bass_volume_table,
  502. };
  503. static struct tumbler_mono_vol snapper_treble_vol_info = {
  504. .index = VOL_IDX_TREBLE,
  505. .reg = TAS_REG_TREBLE,
  506. .bytes = 1,
  507. .max = ARRAY_SIZE(snapper_treble_volume_table),
  508. .table = snapper_treble_volume_table,
  509. };
  510. #define DEFINE_MONO(xname,type) { \
  511. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,\
  512. .name = xname, \
  513. .info = tumbler_info_mono, \
  514. .get = tumbler_get_mono, \
  515. .put = tumbler_put_mono, \
  516. .private_value = (unsigned long)(&tumbler_##type##_vol_info), \
  517. }
  518. #define DEFINE_SNAPPER_MONO(xname,type) { \
  519. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,\
  520. .name = xname, \
  521. .info = tumbler_info_mono, \
  522. .get = tumbler_get_mono, \
  523. .put = tumbler_put_mono, \
  524. .private_value = (unsigned long)(&snapper_##type##_vol_info), \
  525. }
  526. /*
  527. * snapper mixer volumes
  528. */
  529. static int snapper_set_mix_vol1(struct pmac_tumbler *mix, int idx, int ch, int reg)
  530. {
  531. int i, j, vol;
  532. unsigned char block[9];
  533. vol = mix->mix_vol[idx][ch];
  534. if (vol >= ARRAY_SIZE(mixer_volume_table)) {
  535. vol = ARRAY_SIZE(mixer_volume_table) - 1;
  536. mix->mix_vol[idx][ch] = vol;
  537. }
  538. for (i = 0; i < 3; i++) {
  539. vol = mix->mix_vol[i][ch];
  540. vol = mixer_volume_table[vol];
  541. for (j = 0; j < 3; j++)
  542. block[i * 3 + j] = (vol >> ((2 - j) * 8)) & 0xff;
  543. }
  544. if (i2c_smbus_write_i2c_block_data(mix->i2c.client, reg,
  545. 9, block) < 0) {
  546. snd_printk("failed to set mono volume %d\n", reg);
  547. return -EINVAL;
  548. }
  549. return 0;
  550. }
  551. static int snapper_set_mix_vol(struct pmac_tumbler *mix, int idx)
  552. {
  553. if (! mix->i2c.client)
  554. return -ENODEV;
  555. if (snapper_set_mix_vol1(mix, idx, 0, TAS_REG_LMIX) < 0 ||
  556. snapper_set_mix_vol1(mix, idx, 1, TAS_REG_RMIX) < 0)
  557. return -EINVAL;
  558. return 0;
  559. }
  560. static int snapper_info_mix(struct snd_kcontrol *kcontrol,
  561. struct snd_ctl_elem_info *uinfo)
  562. {
  563. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  564. uinfo->count = 2;
  565. uinfo->value.integer.min = 0;
  566. uinfo->value.integer.max = ARRAY_SIZE(mixer_volume_table) - 1;
  567. return 0;
  568. }
  569. static int snapper_get_mix(struct snd_kcontrol *kcontrol,
  570. struct snd_ctl_elem_value *ucontrol)
  571. {
  572. int idx = (int)kcontrol->private_value;
  573. struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
  574. struct pmac_tumbler *mix;
  575. if (! (mix = chip->mixer_data))
  576. return -ENODEV;
  577. ucontrol->value.integer.value[0] = mix->mix_vol[idx][0];
  578. ucontrol->value.integer.value[1] = mix->mix_vol[idx][1];
  579. return 0;
  580. }
  581. static int snapper_put_mix(struct snd_kcontrol *kcontrol,
  582. struct snd_ctl_elem_value *ucontrol)
  583. {
  584. int idx = (int)kcontrol->private_value;
  585. struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
  586. struct pmac_tumbler *mix;
  587. int change;
  588. if (! (mix = chip->mixer_data))
  589. return -ENODEV;
  590. change = mix->mix_vol[idx][0] != ucontrol->value.integer.value[0] ||
  591. mix->mix_vol[idx][1] != ucontrol->value.integer.value[1];
  592. if (change) {
  593. mix->mix_vol[idx][0] = ucontrol->value.integer.value[0];
  594. mix->mix_vol[idx][1] = ucontrol->value.integer.value[1];
  595. snapper_set_mix_vol(mix, idx);
  596. }
  597. return change;
  598. }
  599. /*
  600. * mute switches. FIXME: Turn that into software mute when both outputs are muted
  601. * to avoid codec reset on ibook M7
  602. */
  603. enum { TUMBLER_MUTE_HP, TUMBLER_MUTE_AMP, TUMBLER_MUTE_LINE };
  604. static int tumbler_get_mute_switch(struct snd_kcontrol *kcontrol,
  605. struct snd_ctl_elem_value *ucontrol)
  606. {
  607. struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
  608. struct pmac_tumbler *mix;
  609. struct pmac_gpio *gp;
  610. if (! (mix = chip->mixer_data))
  611. return -ENODEV;
  612. switch(kcontrol->private_value) {
  613. case TUMBLER_MUTE_HP:
  614. gp = &mix->hp_mute; break;
  615. case TUMBLER_MUTE_AMP:
  616. gp = &mix->amp_mute; break;
  617. case TUMBLER_MUTE_LINE:
  618. gp = &mix->line_mute; break;
  619. default:
  620. gp = NULL;
  621. }
  622. if (gp == NULL)
  623. return -EINVAL;
  624. ucontrol->value.integer.value[0] = !check_audio_gpio(gp);
  625. return 0;
  626. }
  627. static int tumbler_put_mute_switch(struct snd_kcontrol *kcontrol,
  628. struct snd_ctl_elem_value *ucontrol)
  629. {
  630. struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
  631. struct pmac_tumbler *mix;
  632. struct pmac_gpio *gp;
  633. int val;
  634. #ifdef PMAC_SUPPORT_AUTOMUTE
  635. if (chip->update_automute && chip->auto_mute)
  636. return 0; /* don't touch in the auto-mute mode */
  637. #endif
  638. if (! (mix = chip->mixer_data))
  639. return -ENODEV;
  640. switch(kcontrol->private_value) {
  641. case TUMBLER_MUTE_HP:
  642. gp = &mix->hp_mute; break;
  643. case TUMBLER_MUTE_AMP:
  644. gp = &mix->amp_mute; break;
  645. case TUMBLER_MUTE_LINE:
  646. gp = &mix->line_mute; break;
  647. default:
  648. gp = NULL;
  649. }
  650. if (gp == NULL)
  651. return -EINVAL;
  652. val = ! check_audio_gpio(gp);
  653. if (val != ucontrol->value.integer.value[0]) {
  654. write_audio_gpio(gp, ! ucontrol->value.integer.value[0]);
  655. return 1;
  656. }
  657. return 0;
  658. }
  659. static int snapper_set_capture_source(struct pmac_tumbler *mix)
  660. {
  661. if (! mix->i2c.client)
  662. return -ENODEV;
  663. if (mix->capture_source)
  664. mix->acs = mix->acs |= 2;
  665. else
  666. mix->acs &= ~2;
  667. return i2c_smbus_write_byte_data(mix->i2c.client, TAS_REG_ACS, mix->acs);
  668. }
  669. static int snapper_info_capture_source(struct snd_kcontrol *kcontrol,
  670. struct snd_ctl_elem_info *uinfo)
  671. {
  672. static char *texts[2] = {
  673. "Line", "Mic"
  674. };
  675. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  676. uinfo->count = 1;
  677. uinfo->value.enumerated.items = 2;
  678. if (uinfo->value.enumerated.item > 1)
  679. uinfo->value.enumerated.item = 1;
  680. strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
  681. return 0;
  682. }
  683. static int snapper_get_capture_source(struct snd_kcontrol *kcontrol,
  684. struct snd_ctl_elem_value *ucontrol)
  685. {
  686. struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
  687. struct pmac_tumbler *mix = chip->mixer_data;
  688. snd_assert(mix, return -ENODEV);
  689. ucontrol->value.integer.value[0] = mix->capture_source;
  690. return 0;
  691. }
  692. static int snapper_put_capture_source(struct snd_kcontrol *kcontrol,
  693. struct snd_ctl_elem_value *ucontrol)
  694. {
  695. struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
  696. struct pmac_tumbler *mix = chip->mixer_data;
  697. int change;
  698. snd_assert(mix, return -ENODEV);
  699. change = ucontrol->value.integer.value[0] != mix->capture_source;
  700. if (change) {
  701. mix->capture_source = !!ucontrol->value.integer.value[0];
  702. snapper_set_capture_source(mix);
  703. }
  704. return change;
  705. }
  706. #define DEFINE_SNAPPER_MIX(xname,idx,ofs) { \
  707. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,\
  708. .name = xname, \
  709. .info = snapper_info_mix, \
  710. .get = snapper_get_mix, \
  711. .put = snapper_put_mix, \
  712. .index = idx,\
  713. .private_value = ofs, \
  714. }
  715. /*
  716. */
  717. static struct snd_kcontrol_new tumbler_mixers[] __initdata = {
  718. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  719. .name = "Master Playback Volume",
  720. .info = tumbler_info_master_volume,
  721. .get = tumbler_get_master_volume,
  722. .put = tumbler_put_master_volume
  723. },
  724. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  725. .name = "Master Playback Switch",
  726. .info = snd_pmac_boolean_stereo_info,
  727. .get = tumbler_get_master_switch,
  728. .put = tumbler_put_master_switch
  729. },
  730. DEFINE_MONO("Tone Control - Bass", bass),
  731. DEFINE_MONO("Tone Control - Treble", treble),
  732. DEFINE_MONO("PCM Playback Volume", pcm),
  733. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  734. .name = "DRC Range",
  735. .info = tumbler_info_drc_value,
  736. .get = tumbler_get_drc_value,
  737. .put = tumbler_put_drc_value
  738. },
  739. };
  740. static struct snd_kcontrol_new snapper_mixers[] __initdata = {
  741. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  742. .name = "Master Playback Volume",
  743. .info = tumbler_info_master_volume,
  744. .get = tumbler_get_master_volume,
  745. .put = tumbler_put_master_volume
  746. },
  747. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  748. .name = "Master Playback Switch",
  749. .info = snd_pmac_boolean_stereo_info,
  750. .get = tumbler_get_master_switch,
  751. .put = tumbler_put_master_switch
  752. },
  753. DEFINE_SNAPPER_MIX("PCM Playback Volume", 0, VOL_IDX_PCM),
  754. DEFINE_SNAPPER_MIX("PCM Playback Volume", 1, VOL_IDX_PCM2),
  755. DEFINE_SNAPPER_MIX("Monitor Mix Volume", 0, VOL_IDX_ADC),
  756. DEFINE_SNAPPER_MONO("Tone Control - Bass", bass),
  757. DEFINE_SNAPPER_MONO("Tone Control - Treble", treble),
  758. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  759. .name = "DRC Range",
  760. .info = tumbler_info_drc_value,
  761. .get = tumbler_get_drc_value,
  762. .put = tumbler_put_drc_value
  763. },
  764. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  765. .name = "Input Source", /* FIXME: "Capture Source" doesn't work properly */
  766. .info = snapper_info_capture_source,
  767. .get = snapper_get_capture_source,
  768. .put = snapper_put_capture_source
  769. },
  770. };
  771. static struct snd_kcontrol_new tumbler_hp_sw __initdata = {
  772. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  773. .name = "Headphone Playback Switch",
  774. .info = snd_pmac_boolean_mono_info,
  775. .get = tumbler_get_mute_switch,
  776. .put = tumbler_put_mute_switch,
  777. .private_value = TUMBLER_MUTE_HP,
  778. };
  779. static struct snd_kcontrol_new tumbler_speaker_sw __initdata = {
  780. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  781. .name = "PC Speaker Playback Switch",
  782. .info = snd_pmac_boolean_mono_info,
  783. .get = tumbler_get_mute_switch,
  784. .put = tumbler_put_mute_switch,
  785. .private_value = TUMBLER_MUTE_AMP,
  786. };
  787. static struct snd_kcontrol_new tumbler_lineout_sw __initdata = {
  788. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  789. .name = "Line Out Playback Switch",
  790. .info = snd_pmac_boolean_mono_info,
  791. .get = tumbler_get_mute_switch,
  792. .put = tumbler_put_mute_switch,
  793. .private_value = TUMBLER_MUTE_LINE,
  794. };
  795. static struct snd_kcontrol_new tumbler_drc_sw __initdata = {
  796. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  797. .name = "DRC Switch",
  798. .info = snd_pmac_boolean_mono_info,
  799. .get = tumbler_get_drc_switch,
  800. .put = tumbler_put_drc_switch
  801. };
  802. #ifdef PMAC_SUPPORT_AUTOMUTE
  803. /*
  804. * auto-mute stuffs
  805. */
  806. static int tumbler_detect_headphone(struct snd_pmac *chip)
  807. {
  808. struct pmac_tumbler *mix = chip->mixer_data;
  809. int detect = 0;
  810. if (mix->hp_detect.addr)
  811. detect |= read_audio_gpio(&mix->hp_detect);
  812. return detect;
  813. }
  814. static int tumbler_detect_lineout(struct snd_pmac *chip)
  815. {
  816. struct pmac_tumbler *mix = chip->mixer_data;
  817. int detect = 0;
  818. if (mix->line_detect.addr)
  819. detect |= read_audio_gpio(&mix->line_detect);
  820. return detect;
  821. }
  822. static void check_mute(struct snd_pmac *chip, struct pmac_gpio *gp, int val, int do_notify,
  823. struct snd_kcontrol *sw)
  824. {
  825. if (check_audio_gpio(gp) != val) {
  826. write_audio_gpio(gp, val);
  827. if (do_notify)
  828. snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
  829. &sw->id);
  830. }
  831. }
  832. static struct work_struct device_change;
  833. static void device_change_handler(void *self)
  834. {
  835. struct snd_pmac *chip = self;
  836. struct pmac_tumbler *mix;
  837. int headphone, lineout;
  838. if (!chip)
  839. return;
  840. mix = chip->mixer_data;
  841. snd_assert(mix, return);
  842. headphone = tumbler_detect_headphone(chip);
  843. lineout = tumbler_detect_lineout(chip);
  844. DBG("headphone: %d, lineout: %d\n", headphone, lineout);
  845. if (headphone || lineout) {
  846. /* unmute headphone/lineout & mute speaker */
  847. if (headphone)
  848. check_mute(chip, &mix->hp_mute, 0, mix->auto_mute_notify,
  849. chip->master_sw_ctl);
  850. if (lineout && mix->line_mute.addr != 0)
  851. check_mute(chip, &mix->line_mute, 0, mix->auto_mute_notify,
  852. chip->lineout_sw_ctl);
  853. if (mix->anded_reset)
  854. msleep(10);
  855. check_mute(chip, &mix->amp_mute, 1, mix->auto_mute_notify,
  856. chip->speaker_sw_ctl);
  857. } else {
  858. /* unmute speaker, mute others */
  859. check_mute(chip, &mix->amp_mute, 0, mix->auto_mute_notify,
  860. chip->speaker_sw_ctl);
  861. if (mix->anded_reset)
  862. msleep(10);
  863. check_mute(chip, &mix->hp_mute, 1, mix->auto_mute_notify,
  864. chip->master_sw_ctl);
  865. if (mix->line_mute.addr != 0)
  866. check_mute(chip, &mix->line_mute, 1, mix->auto_mute_notify,
  867. chip->lineout_sw_ctl);
  868. }
  869. if (mix->auto_mute_notify)
  870. snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
  871. &chip->hp_detect_ctl->id);
  872. #ifdef CONFIG_SND_POWERMAC_AUTO_DRC
  873. mix->drc_enable = ! (headphone || lineout);
  874. if (mix->auto_mute_notify)
  875. snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
  876. &chip->drc_sw_ctl->id);
  877. if (chip->model == PMAC_TUMBLER)
  878. tumbler_set_drc(mix);
  879. else
  880. snapper_set_drc(mix);
  881. #endif
  882. /* reset the master volume so the correct amplification is applied */
  883. tumbler_set_master_volume(mix);
  884. }
  885. static void tumbler_update_automute(struct snd_pmac *chip, int do_notify)
  886. {
  887. if (chip->auto_mute) {
  888. struct pmac_tumbler *mix;
  889. mix = chip->mixer_data;
  890. snd_assert(mix, return);
  891. mix->auto_mute_notify = do_notify;
  892. schedule_work(&device_change);
  893. }
  894. }
  895. #endif /* PMAC_SUPPORT_AUTOMUTE */
  896. /* interrupt - headphone plug changed */
  897. static irqreturn_t headphone_intr(int irq, void *devid, struct pt_regs *regs)
  898. {
  899. struct snd_pmac *chip = devid;
  900. if (chip->update_automute && chip->initialized) {
  901. chip->update_automute(chip, 1);
  902. return IRQ_HANDLED;
  903. }
  904. return IRQ_NONE;
  905. }
  906. /* look for audio-gpio device */
  907. static struct device_node *find_audio_device(const char *name)
  908. {
  909. struct device_node *np;
  910. if (! (np = find_devices("gpio")))
  911. return NULL;
  912. for (np = np->child; np; np = np->sibling) {
  913. char *property = get_property(np, "audio-gpio", NULL);
  914. if (property && strcmp(property, name) == 0)
  915. return np;
  916. }
  917. return NULL;
  918. }
  919. /* look for audio-gpio device */
  920. static struct device_node *find_compatible_audio_device(const char *name)
  921. {
  922. struct device_node *np;
  923. if (! (np = find_devices("gpio")))
  924. return NULL;
  925. for (np = np->child; np; np = np->sibling) {
  926. if (device_is_compatible(np, name))
  927. return np;
  928. }
  929. return NULL;
  930. }
  931. /* find an audio device and get its address */
  932. static long tumbler_find_device(const char *device, const char *platform,
  933. struct pmac_gpio *gp, int is_compatible)
  934. {
  935. struct device_node *node;
  936. u32 *base, addr;
  937. if (is_compatible)
  938. node = find_compatible_audio_device(device);
  939. else
  940. node = find_audio_device(device);
  941. if (! node) {
  942. DBG("(W) cannot find audio device %s !\n", device);
  943. snd_printdd("cannot find device %s\n", device);
  944. return -ENODEV;
  945. }
  946. base = (u32 *)get_property(node, "AAPL,address", NULL);
  947. if (! base) {
  948. base = (u32 *)get_property(node, "reg", NULL);
  949. if (!base) {
  950. DBG("(E) cannot find address for device %s !\n", device);
  951. snd_printd("cannot find address for device %s\n", device);
  952. return -ENODEV;
  953. }
  954. addr = *base;
  955. if (addr < 0x50)
  956. addr += 0x50;
  957. } else
  958. addr = *base;
  959. gp->addr = addr & 0x0000ffff;
  960. /* Try to find the active state, default to 0 ! */
  961. base = (u32 *)get_property(node, "audio-gpio-active-state", NULL);
  962. if (base) {
  963. gp->active_state = *base;
  964. gp->active_val = (*base) ? 0x5 : 0x4;
  965. gp->inactive_val = (*base) ? 0x4 : 0x5;
  966. } else {
  967. u32 *prop = NULL;
  968. gp->active_state = 0;
  969. gp->active_val = 0x4;
  970. gp->inactive_val = 0x5;
  971. /* Here are some crude hacks to extract the GPIO polarity and
  972. * open collector informations out of the do-platform script
  973. * as we don't yet have an interpreter for these things
  974. */
  975. if (platform)
  976. prop = (u32 *)get_property(node, platform, NULL);
  977. if (prop) {
  978. if (prop[3] == 0x9 && prop[4] == 0x9) {
  979. gp->active_val = 0xd;
  980. gp->inactive_val = 0xc;
  981. }
  982. if (prop[3] == 0x1 && prop[4] == 0x1) {
  983. gp->active_val = 0x5;
  984. gp->inactive_val = 0x4;
  985. }
  986. }
  987. }
  988. DBG("(I) GPIO device %s found, offset: %x, active state: %d !\n",
  989. device, gp->addr, gp->active_state);
  990. return irq_of_parse_and_map(node, 0);
  991. }
  992. /* reset audio */
  993. static void tumbler_reset_audio(struct snd_pmac *chip)
  994. {
  995. struct pmac_tumbler *mix = chip->mixer_data;
  996. if (mix->anded_reset) {
  997. DBG("(I) codec anded reset !\n");
  998. write_audio_gpio(&mix->hp_mute, 0);
  999. write_audio_gpio(&mix->amp_mute, 0);
  1000. msleep(200);
  1001. write_audio_gpio(&mix->hp_mute, 1);
  1002. write_audio_gpio(&mix->amp_mute, 1);
  1003. msleep(100);
  1004. write_audio_gpio(&mix->hp_mute, 0);
  1005. write_audio_gpio(&mix->amp_mute, 0);
  1006. msleep(100);
  1007. } else {
  1008. DBG("(I) codec normal reset !\n");
  1009. write_audio_gpio(&mix->audio_reset, 0);
  1010. msleep(200);
  1011. write_audio_gpio(&mix->audio_reset, 1);
  1012. msleep(100);
  1013. write_audio_gpio(&mix->audio_reset, 0);
  1014. msleep(100);
  1015. }
  1016. }
  1017. #ifdef CONFIG_PM
  1018. /* suspend mixer */
  1019. static void tumbler_suspend(struct snd_pmac *chip)
  1020. {
  1021. struct pmac_tumbler *mix = chip->mixer_data;
  1022. if (mix->headphone_irq >= 0)
  1023. disable_irq(mix->headphone_irq);
  1024. if (mix->lineout_irq >= 0)
  1025. disable_irq(mix->lineout_irq);
  1026. mix->save_master_switch[0] = mix->master_switch[0];
  1027. mix->save_master_switch[1] = mix->master_switch[1];
  1028. mix->save_master_vol[0] = mix->master_vol[0];
  1029. mix->save_master_vol[1] = mix->master_vol[1];
  1030. mix->master_switch[0] = mix->master_switch[1] = 0;
  1031. tumbler_set_master_volume(mix);
  1032. if (!mix->anded_reset) {
  1033. write_audio_gpio(&mix->amp_mute, 1);
  1034. write_audio_gpio(&mix->hp_mute, 1);
  1035. }
  1036. if (chip->model == PMAC_SNAPPER) {
  1037. mix->acs |= 1;
  1038. i2c_smbus_write_byte_data(mix->i2c.client, TAS_REG_ACS, mix->acs);
  1039. }
  1040. if (mix->anded_reset) {
  1041. write_audio_gpio(&mix->amp_mute, 1);
  1042. write_audio_gpio(&mix->hp_mute, 1);
  1043. } else
  1044. write_audio_gpio(&mix->audio_reset, 1);
  1045. }
  1046. /* resume mixer */
  1047. static void tumbler_resume(struct snd_pmac *chip)
  1048. {
  1049. struct pmac_tumbler *mix = chip->mixer_data;
  1050. snd_assert(mix, return);
  1051. mix->acs &= ~1;
  1052. mix->master_switch[0] = mix->save_master_switch[0];
  1053. mix->master_switch[1] = mix->save_master_switch[1];
  1054. mix->master_vol[0] = mix->save_master_vol[0];
  1055. mix->master_vol[1] = mix->save_master_vol[1];
  1056. tumbler_reset_audio(chip);
  1057. if (mix->i2c.client && mix->i2c.init_client) {
  1058. if (mix->i2c.init_client(&mix->i2c) < 0)
  1059. printk(KERN_ERR "tumbler_init_client error\n");
  1060. } else
  1061. printk(KERN_ERR "tumbler: i2c is not initialized\n");
  1062. if (chip->model == PMAC_TUMBLER) {
  1063. tumbler_set_mono_volume(mix, &tumbler_pcm_vol_info);
  1064. tumbler_set_mono_volume(mix, &tumbler_bass_vol_info);
  1065. tumbler_set_mono_volume(mix, &tumbler_treble_vol_info);
  1066. tumbler_set_drc(mix);
  1067. } else {
  1068. snapper_set_mix_vol(mix, VOL_IDX_PCM);
  1069. snapper_set_mix_vol(mix, VOL_IDX_PCM2);
  1070. snapper_set_mix_vol(mix, VOL_IDX_ADC);
  1071. tumbler_set_mono_volume(mix, &snapper_bass_vol_info);
  1072. tumbler_set_mono_volume(mix, &snapper_treble_vol_info);
  1073. snapper_set_drc(mix);
  1074. snapper_set_capture_source(mix);
  1075. }
  1076. tumbler_set_master_volume(mix);
  1077. if (chip->update_automute)
  1078. chip->update_automute(chip, 0);
  1079. if (mix->headphone_irq >= 0) {
  1080. unsigned char val;
  1081. enable_irq(mix->headphone_irq);
  1082. /* activate headphone status interrupts */
  1083. val = do_gpio_read(&mix->hp_detect);
  1084. do_gpio_write(&mix->hp_detect, val | 0x80);
  1085. }
  1086. if (mix->lineout_irq >= 0)
  1087. enable_irq(mix->lineout_irq);
  1088. }
  1089. #endif
  1090. /* initialize tumbler */
  1091. static int __init tumbler_init(struct snd_pmac *chip)
  1092. {
  1093. int irq;
  1094. struct pmac_tumbler *mix = chip->mixer_data;
  1095. snd_assert(mix, return -EINVAL);
  1096. if (tumbler_find_device("audio-hw-reset",
  1097. "platform-do-hw-reset",
  1098. &mix->audio_reset, 0) < 0)
  1099. tumbler_find_device("hw-reset",
  1100. "platform-do-hw-reset",
  1101. &mix->audio_reset, 1);
  1102. if (tumbler_find_device("amp-mute",
  1103. "platform-do-amp-mute",
  1104. &mix->amp_mute, 0) < 0)
  1105. tumbler_find_device("amp-mute",
  1106. "platform-do-amp-mute",
  1107. &mix->amp_mute, 1);
  1108. if (tumbler_find_device("headphone-mute",
  1109. "platform-do-headphone-mute",
  1110. &mix->hp_mute, 0) < 0)
  1111. tumbler_find_device("headphone-mute",
  1112. "platform-do-headphone-mute",
  1113. &mix->hp_mute, 1);
  1114. if (tumbler_find_device("line-output-mute",
  1115. "platform-do-lineout-mute",
  1116. &mix->line_mute, 0) < 0)
  1117. tumbler_find_device("line-output-mute",
  1118. "platform-do-lineout-mute",
  1119. &mix->line_mute, 1);
  1120. irq = tumbler_find_device("headphone-detect",
  1121. NULL, &mix->hp_detect, 0);
  1122. if (irq <= NO_IRQ)
  1123. irq = tumbler_find_device("headphone-detect",
  1124. NULL, &mix->hp_detect, 1);
  1125. if (irq <= NO_IRQ)
  1126. irq = tumbler_find_device("keywest-gpio15",
  1127. NULL, &mix->hp_detect, 1);
  1128. mix->headphone_irq = irq;
  1129. irq = tumbler_find_device("line-output-detect",
  1130. NULL, &mix->line_detect, 0);
  1131. if (irq <= NO_IRQ)
  1132. irq = tumbler_find_device("line-output-detect",
  1133. NULL, &mix->line_detect, 1);
  1134. mix->lineout_irq = irq;
  1135. tumbler_reset_audio(chip);
  1136. return 0;
  1137. }
  1138. static void tumbler_cleanup(struct snd_pmac *chip)
  1139. {
  1140. struct pmac_tumbler *mix = chip->mixer_data;
  1141. if (! mix)
  1142. return;
  1143. if (mix->headphone_irq >= 0)
  1144. free_irq(mix->headphone_irq, chip);
  1145. if (mix->lineout_irq >= 0)
  1146. free_irq(mix->lineout_irq, chip);
  1147. tumbler_gpio_free(&mix->audio_reset);
  1148. tumbler_gpio_free(&mix->amp_mute);
  1149. tumbler_gpio_free(&mix->hp_mute);
  1150. tumbler_gpio_free(&mix->hp_detect);
  1151. snd_pmac_keywest_cleanup(&mix->i2c);
  1152. kfree(mix);
  1153. chip->mixer_data = NULL;
  1154. }
  1155. /* exported */
  1156. int __init snd_pmac_tumbler_init(struct snd_pmac *chip)
  1157. {
  1158. int i, err;
  1159. struct pmac_tumbler *mix;
  1160. u32 *paddr;
  1161. struct device_node *tas_node, *np;
  1162. char *chipname;
  1163. #ifdef CONFIG_KMOD
  1164. if (current->fs->root)
  1165. request_module("i2c-powermac");
  1166. #endif /* CONFIG_KMOD */
  1167. mix = kzalloc(sizeof(*mix), GFP_KERNEL);
  1168. if (! mix)
  1169. return -ENOMEM;
  1170. mix->headphone_irq = -1;
  1171. chip->mixer_data = mix;
  1172. chip->mixer_free = tumbler_cleanup;
  1173. mix->anded_reset = 0;
  1174. mix->reset_on_sleep = 1;
  1175. for (np = chip->node->child; np; np = np->sibling) {
  1176. if (!strcmp(np->name, "sound")) {
  1177. if (get_property(np, "has-anded-reset", NULL))
  1178. mix->anded_reset = 1;
  1179. if (get_property(np, "layout-id", NULL))
  1180. mix->reset_on_sleep = 0;
  1181. break;
  1182. }
  1183. }
  1184. if ((err = tumbler_init(chip)) < 0)
  1185. return err;
  1186. /* set up TAS */
  1187. tas_node = find_devices("deq");
  1188. if (tas_node == NULL)
  1189. tas_node = find_devices("codec");
  1190. if (tas_node == NULL)
  1191. return -ENODEV;
  1192. paddr = (u32 *)get_property(tas_node, "i2c-address", NULL);
  1193. if (paddr == NULL)
  1194. paddr = (u32 *)get_property(tas_node, "reg", NULL);
  1195. if (paddr)
  1196. mix->i2c.addr = (*paddr) >> 1;
  1197. else
  1198. mix->i2c.addr = TAS_I2C_ADDR;
  1199. DBG("(I) TAS i2c address is: %x\n", mix->i2c.addr);
  1200. if (chip->model == PMAC_TUMBLER) {
  1201. mix->i2c.init_client = tumbler_init_client;
  1202. mix->i2c.name = "TAS3001c";
  1203. chipname = "Tumbler";
  1204. } else {
  1205. mix->i2c.init_client = snapper_init_client;
  1206. mix->i2c.name = "TAS3004";
  1207. chipname = "Snapper";
  1208. }
  1209. if ((err = snd_pmac_keywest_init(&mix->i2c)) < 0)
  1210. return err;
  1211. /*
  1212. * build mixers
  1213. */
  1214. sprintf(chip->card->mixername, "PowerMac %s", chipname);
  1215. if (chip->model == PMAC_TUMBLER) {
  1216. for (i = 0; i < ARRAY_SIZE(tumbler_mixers); i++) {
  1217. if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&tumbler_mixers[i], chip))) < 0)
  1218. return err;
  1219. }
  1220. } else {
  1221. for (i = 0; i < ARRAY_SIZE(snapper_mixers); i++) {
  1222. if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snapper_mixers[i], chip))) < 0)
  1223. return err;
  1224. }
  1225. }
  1226. chip->master_sw_ctl = snd_ctl_new1(&tumbler_hp_sw, chip);
  1227. if ((err = snd_ctl_add(chip->card, chip->master_sw_ctl)) < 0)
  1228. return err;
  1229. chip->speaker_sw_ctl = snd_ctl_new1(&tumbler_speaker_sw, chip);
  1230. if ((err = snd_ctl_add(chip->card, chip->speaker_sw_ctl)) < 0)
  1231. return err;
  1232. if (mix->line_mute.addr != 0) {
  1233. chip->lineout_sw_ctl = snd_ctl_new1(&tumbler_lineout_sw, chip);
  1234. if ((err = snd_ctl_add(chip->card, chip->lineout_sw_ctl)) < 0)
  1235. return err;
  1236. }
  1237. chip->drc_sw_ctl = snd_ctl_new1(&tumbler_drc_sw, chip);
  1238. if ((err = snd_ctl_add(chip->card, chip->drc_sw_ctl)) < 0)
  1239. return err;
  1240. /* set initial DRC range to 60% */
  1241. if (chip->model == PMAC_TUMBLER)
  1242. mix->drc_range = (TAS3001_DRC_MAX * 6) / 10;
  1243. else
  1244. mix->drc_range = (TAS3004_DRC_MAX * 6) / 10;
  1245. mix->drc_enable = 1; /* will be changed later if AUTO_DRC is set */
  1246. if (chip->model == PMAC_TUMBLER)
  1247. tumbler_set_drc(mix);
  1248. else
  1249. snapper_set_drc(mix);
  1250. #ifdef CONFIG_PM
  1251. chip->suspend = tumbler_suspend;
  1252. chip->resume = tumbler_resume;
  1253. #endif
  1254. INIT_WORK(&device_change, device_change_handler, (void *)chip);
  1255. #ifdef PMAC_SUPPORT_AUTOMUTE
  1256. if ((mix->headphone_irq >=0 || mix->lineout_irq >= 0)
  1257. && (err = snd_pmac_add_automute(chip)) < 0)
  1258. return err;
  1259. chip->detect_headphone = tumbler_detect_headphone;
  1260. chip->update_automute = tumbler_update_automute;
  1261. tumbler_update_automute(chip, 0); /* update the status only */
  1262. /* activate headphone status interrupts */
  1263. if (mix->headphone_irq >= 0) {
  1264. unsigned char val;
  1265. if ((err = request_irq(mix->headphone_irq, headphone_intr, 0,
  1266. "Sound Headphone Detection", chip)) < 0)
  1267. return 0;
  1268. /* activate headphone status interrupts */
  1269. val = do_gpio_read(&mix->hp_detect);
  1270. do_gpio_write(&mix->hp_detect, val | 0x80);
  1271. }
  1272. if (mix->lineout_irq >= 0) {
  1273. unsigned char val;
  1274. if ((err = request_irq(mix->lineout_irq, headphone_intr, 0,
  1275. "Sound Lineout Detection", chip)) < 0)
  1276. return 0;
  1277. /* activate headphone status interrupts */
  1278. val = do_gpio_read(&mix->line_detect);
  1279. do_gpio_write(&mix->line_detect, val | 0x80);
  1280. }
  1281. #endif
  1282. return 0;
  1283. }