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