tumbler.c 38 KB

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