tumbler.c 38 KB

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