tumbler.c 38 KB

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