tumbler.c 38 KB

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