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 & 0x1) == (gp->active_val & 0x1);
  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);
  177. ret = (ret & 0x02) !=0;
  178. return ret == gp->active_state;
  179. }
  180. /*
  181. * update master volume
  182. */
  183. static int tumbler_set_master_volume(struct pmac_tumbler *mix)
  184. {
  185. unsigned char block[6];
  186. unsigned int left_vol, right_vol;
  187. if (! mix->i2c.client)
  188. return -ENODEV;
  189. if (! mix->master_switch[0])
  190. left_vol = 0;
  191. else {
  192. left_vol = mix->master_vol[0];
  193. if (left_vol >= ARRAY_SIZE(master_volume_table))
  194. left_vol = ARRAY_SIZE(master_volume_table) - 1;
  195. left_vol = master_volume_table[left_vol];
  196. }
  197. if (! mix->master_switch[1])
  198. right_vol = 0;
  199. else {
  200. right_vol = mix->master_vol[1];
  201. if (right_vol >= ARRAY_SIZE(master_volume_table))
  202. right_vol = ARRAY_SIZE(master_volume_table) - 1;
  203. right_vol = master_volume_table[right_vol];
  204. }
  205. block[0] = (left_vol >> 16) & 0xff;
  206. block[1] = (left_vol >> 8) & 0xff;
  207. block[2] = (left_vol >> 0) & 0xff;
  208. block[3] = (right_vol >> 16) & 0xff;
  209. block[4] = (right_vol >> 8) & 0xff;
  210. block[5] = (right_vol >> 0) & 0xff;
  211. if (i2c_smbus_write_i2c_block_data(mix->i2c.client, TAS_REG_VOL, 6,
  212. block) < 0) {
  213. snd_printk("failed to set volume \n");
  214. return -EINVAL;
  215. }
  216. return 0;
  217. }
  218. /* output volume */
  219. static int tumbler_info_master_volume(struct snd_kcontrol *kcontrol,
  220. struct snd_ctl_elem_info *uinfo)
  221. {
  222. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  223. uinfo->count = 2;
  224. uinfo->value.integer.min = 0;
  225. uinfo->value.integer.max = ARRAY_SIZE(master_volume_table) - 1;
  226. return 0;
  227. }
  228. static int tumbler_get_master_volume(struct snd_kcontrol *kcontrol,
  229. struct snd_ctl_elem_value *ucontrol)
  230. {
  231. struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
  232. struct pmac_tumbler *mix = chip->mixer_data;
  233. snd_assert(mix, return -ENODEV);
  234. ucontrol->value.integer.value[0] = mix->master_vol[0];
  235. ucontrol->value.integer.value[1] = mix->master_vol[1];
  236. return 0;
  237. }
  238. static int tumbler_put_master_volume(struct snd_kcontrol *kcontrol,
  239. struct snd_ctl_elem_value *ucontrol)
  240. {
  241. struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
  242. struct pmac_tumbler *mix = chip->mixer_data;
  243. int change;
  244. snd_assert(mix, return -ENODEV);
  245. change = mix->master_vol[0] != ucontrol->value.integer.value[0] ||
  246. mix->master_vol[1] != ucontrol->value.integer.value[1];
  247. if (change) {
  248. mix->master_vol[0] = ucontrol->value.integer.value[0];
  249. mix->master_vol[1] = ucontrol->value.integer.value[1];
  250. tumbler_set_master_volume(mix);
  251. }
  252. return change;
  253. }
  254. /* output switch */
  255. static int tumbler_get_master_switch(struct snd_kcontrol *kcontrol,
  256. struct snd_ctl_elem_value *ucontrol)
  257. {
  258. struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
  259. struct pmac_tumbler *mix = chip->mixer_data;
  260. snd_assert(mix, return -ENODEV);
  261. ucontrol->value.integer.value[0] = mix->master_switch[0];
  262. ucontrol->value.integer.value[1] = mix->master_switch[1];
  263. return 0;
  264. }
  265. static int tumbler_put_master_switch(struct snd_kcontrol *kcontrol,
  266. struct snd_ctl_elem_value *ucontrol)
  267. {
  268. struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
  269. struct pmac_tumbler *mix = chip->mixer_data;
  270. int change;
  271. snd_assert(mix, return -ENODEV);
  272. change = mix->master_switch[0] != ucontrol->value.integer.value[0] ||
  273. mix->master_switch[1] != ucontrol->value.integer.value[1];
  274. if (change) {
  275. mix->master_switch[0] = !!ucontrol->value.integer.value[0];
  276. mix->master_switch[1] = !!ucontrol->value.integer.value[1];
  277. tumbler_set_master_volume(mix);
  278. }
  279. return change;
  280. }
  281. /*
  282. * TAS3001c dynamic range compression
  283. */
  284. #define TAS3001_DRC_MAX 0x5f
  285. static int tumbler_set_drc(struct pmac_tumbler *mix)
  286. {
  287. unsigned char val[2];
  288. if (! mix->i2c.client)
  289. return -ENODEV;
  290. if (mix->drc_enable) {
  291. val[0] = 0xc1; /* enable, 3:1 compression */
  292. if (mix->drc_range > TAS3001_DRC_MAX)
  293. val[1] = 0xf0;
  294. else if (mix->drc_range < 0)
  295. val[1] = 0x91;
  296. else
  297. val[1] = mix->drc_range + 0x91;
  298. } else {
  299. val[0] = 0;
  300. val[1] = 0;
  301. }
  302. if (i2c_smbus_write_i2c_block_data(mix->i2c.client, TAS_REG_DRC,
  303. 2, val) < 0) {
  304. snd_printk("failed to set DRC\n");
  305. return -EINVAL;
  306. }
  307. return 0;
  308. }
  309. /*
  310. * TAS3004
  311. */
  312. #define TAS3004_DRC_MAX 0xef
  313. static int snapper_set_drc(struct pmac_tumbler *mix)
  314. {
  315. unsigned char val[6];
  316. if (! mix->i2c.client)
  317. return -ENODEV;
  318. if (mix->drc_enable)
  319. val[0] = 0x50; /* 3:1 above threshold */
  320. else
  321. val[0] = 0x51; /* disabled */
  322. val[1] = 0x02; /* 1:1 below threshold */
  323. if (mix->drc_range > 0xef)
  324. val[2] = 0xef;
  325. else if (mix->drc_range < 0)
  326. val[2] = 0x00;
  327. else
  328. val[2] = mix->drc_range;
  329. val[3] = 0xb0;
  330. val[4] = 0x60;
  331. val[5] = 0xa0;
  332. if (i2c_smbus_write_i2c_block_data(mix->i2c.client, TAS_REG_DRC,
  333. 6, val) < 0) {
  334. snd_printk("failed to set DRC\n");
  335. return -EINVAL;
  336. }
  337. return 0;
  338. }
  339. static int tumbler_info_drc_value(struct snd_kcontrol *kcontrol,
  340. struct snd_ctl_elem_info *uinfo)
  341. {
  342. struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
  343. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  344. uinfo->count = 1;
  345. uinfo->value.integer.min = 0;
  346. uinfo->value.integer.max =
  347. chip->model == PMAC_TUMBLER ? TAS3001_DRC_MAX : TAS3004_DRC_MAX;
  348. return 0;
  349. }
  350. static int tumbler_get_drc_value(struct snd_kcontrol *kcontrol,
  351. struct snd_ctl_elem_value *ucontrol)
  352. {
  353. struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
  354. struct pmac_tumbler *mix;
  355. if (! (mix = chip->mixer_data))
  356. return -ENODEV;
  357. ucontrol->value.integer.value[0] = mix->drc_range;
  358. return 0;
  359. }
  360. static int tumbler_put_drc_value(struct snd_kcontrol *kcontrol,
  361. struct snd_ctl_elem_value *ucontrol)
  362. {
  363. struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
  364. struct pmac_tumbler *mix;
  365. int change;
  366. if (! (mix = chip->mixer_data))
  367. return -ENODEV;
  368. change = mix->drc_range != ucontrol->value.integer.value[0];
  369. if (change) {
  370. mix->drc_range = ucontrol->value.integer.value[0];
  371. if (chip->model == PMAC_TUMBLER)
  372. tumbler_set_drc(mix);
  373. else
  374. snapper_set_drc(mix);
  375. }
  376. return change;
  377. }
  378. static int tumbler_get_drc_switch(struct snd_kcontrol *kcontrol,
  379. struct snd_ctl_elem_value *ucontrol)
  380. {
  381. struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
  382. struct pmac_tumbler *mix;
  383. if (! (mix = chip->mixer_data))
  384. return -ENODEV;
  385. ucontrol->value.integer.value[0] = mix->drc_enable;
  386. return 0;
  387. }
  388. static int tumbler_put_drc_switch(struct snd_kcontrol *kcontrol,
  389. struct snd_ctl_elem_value *ucontrol)
  390. {
  391. struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
  392. struct pmac_tumbler *mix;
  393. int change;
  394. if (! (mix = chip->mixer_data))
  395. return -ENODEV;
  396. change = mix->drc_enable != ucontrol->value.integer.value[0];
  397. if (change) {
  398. mix->drc_enable = !!ucontrol->value.integer.value[0];
  399. if (chip->model == PMAC_TUMBLER)
  400. tumbler_set_drc(mix);
  401. else
  402. snapper_set_drc(mix);
  403. }
  404. return change;
  405. }
  406. /*
  407. * mono volumes
  408. */
  409. struct tumbler_mono_vol {
  410. int index;
  411. int reg;
  412. int bytes;
  413. unsigned int max;
  414. unsigned int *table;
  415. };
  416. static int tumbler_set_mono_volume(struct pmac_tumbler *mix,
  417. struct tumbler_mono_vol *info)
  418. {
  419. unsigned char block[4];
  420. unsigned int vol;
  421. int i;
  422. if (! mix->i2c.client)
  423. return -ENODEV;
  424. vol = mix->mono_vol[info->index];
  425. if (vol >= info->max)
  426. vol = info->max - 1;
  427. vol = info->table[vol];
  428. for (i = 0; i < info->bytes; i++)
  429. block[i] = (vol >> ((info->bytes - i - 1) * 8)) & 0xff;
  430. if (i2c_smbus_write_i2c_block_data(mix->i2c.client, info->reg,
  431. info->bytes, block) < 0) {
  432. snd_printk("failed to set mono volume %d\n", info->index);
  433. return -EINVAL;
  434. }
  435. return 0;
  436. }
  437. static int tumbler_info_mono(struct snd_kcontrol *kcontrol,
  438. struct snd_ctl_elem_info *uinfo)
  439. {
  440. struct tumbler_mono_vol *info = (struct tumbler_mono_vol *)kcontrol->private_value;
  441. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  442. uinfo->count = 1;
  443. uinfo->value.integer.min = 0;
  444. uinfo->value.integer.max = info->max - 1;
  445. return 0;
  446. }
  447. static int tumbler_get_mono(struct snd_kcontrol *kcontrol,
  448. struct snd_ctl_elem_value *ucontrol)
  449. {
  450. struct tumbler_mono_vol *info = (struct tumbler_mono_vol *)kcontrol->private_value;
  451. struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
  452. struct pmac_tumbler *mix;
  453. if (! (mix = chip->mixer_data))
  454. return -ENODEV;
  455. ucontrol->value.integer.value[0] = mix->mono_vol[info->index];
  456. return 0;
  457. }
  458. static int tumbler_put_mono(struct snd_kcontrol *kcontrol,
  459. struct snd_ctl_elem_value *ucontrol)
  460. {
  461. struct tumbler_mono_vol *info = (struct tumbler_mono_vol *)kcontrol->private_value;
  462. struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
  463. struct pmac_tumbler *mix;
  464. int change;
  465. if (! (mix = chip->mixer_data))
  466. return -ENODEV;
  467. change = mix->mono_vol[info->index] != ucontrol->value.integer.value[0];
  468. if (change) {
  469. mix->mono_vol[info->index] = ucontrol->value.integer.value[0];
  470. tumbler_set_mono_volume(mix, info);
  471. }
  472. return change;
  473. }
  474. /* TAS3001c mono volumes */
  475. static struct tumbler_mono_vol tumbler_pcm_vol_info = {
  476. .index = VOL_IDX_PCM_MONO,
  477. .reg = TAS_REG_PCM,
  478. .bytes = 3,
  479. .max = ARRAY_SIZE(mixer_volume_table),
  480. .table = mixer_volume_table,
  481. };
  482. static struct tumbler_mono_vol tumbler_bass_vol_info = {
  483. .index = VOL_IDX_BASS,
  484. .reg = TAS_REG_BASS,
  485. .bytes = 1,
  486. .max = ARRAY_SIZE(bass_volume_table),
  487. .table = bass_volume_table,
  488. };
  489. static struct tumbler_mono_vol tumbler_treble_vol_info = {
  490. .index = VOL_IDX_TREBLE,
  491. .reg = TAS_REG_TREBLE,
  492. .bytes = 1,
  493. .max = ARRAY_SIZE(treble_volume_table),
  494. .table = treble_volume_table,
  495. };
  496. /* TAS3004 mono volumes */
  497. static struct tumbler_mono_vol snapper_bass_vol_info = {
  498. .index = VOL_IDX_BASS,
  499. .reg = TAS_REG_BASS,
  500. .bytes = 1,
  501. .max = ARRAY_SIZE(snapper_bass_volume_table),
  502. .table = snapper_bass_volume_table,
  503. };
  504. static struct tumbler_mono_vol snapper_treble_vol_info = {
  505. .index = VOL_IDX_TREBLE,
  506. .reg = TAS_REG_TREBLE,
  507. .bytes = 1,
  508. .max = ARRAY_SIZE(snapper_treble_volume_table),
  509. .table = snapper_treble_volume_table,
  510. };
  511. #define DEFINE_MONO(xname,type) { \
  512. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,\
  513. .name = xname, \
  514. .info = tumbler_info_mono, \
  515. .get = tumbler_get_mono, \
  516. .put = tumbler_put_mono, \
  517. .private_value = (unsigned long)(&tumbler_##type##_vol_info), \
  518. }
  519. #define DEFINE_SNAPPER_MONO(xname,type) { \
  520. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,\
  521. .name = xname, \
  522. .info = tumbler_info_mono, \
  523. .get = tumbler_get_mono, \
  524. .put = tumbler_put_mono, \
  525. .private_value = (unsigned long)(&snapper_##type##_vol_info), \
  526. }
  527. /*
  528. * snapper mixer volumes
  529. */
  530. static int snapper_set_mix_vol1(struct pmac_tumbler *mix, int idx, int ch, int reg)
  531. {
  532. int i, j, vol;
  533. unsigned char block[9];
  534. vol = mix->mix_vol[idx][ch];
  535. if (vol >= ARRAY_SIZE(mixer_volume_table)) {
  536. vol = ARRAY_SIZE(mixer_volume_table) - 1;
  537. mix->mix_vol[idx][ch] = vol;
  538. }
  539. for (i = 0; i < 3; i++) {
  540. vol = mix->mix_vol[i][ch];
  541. vol = mixer_volume_table[vol];
  542. for (j = 0; j < 3; j++)
  543. block[i * 3 + j] = (vol >> ((2 - j) * 8)) & 0xff;
  544. }
  545. if (i2c_smbus_write_i2c_block_data(mix->i2c.client, reg,
  546. 9, block) < 0) {
  547. snd_printk("failed to set mono volume %d\n", reg);
  548. return -EINVAL;
  549. }
  550. return 0;
  551. }
  552. static int snapper_set_mix_vol(struct pmac_tumbler *mix, int idx)
  553. {
  554. if (! mix->i2c.client)
  555. return -ENODEV;
  556. if (snapper_set_mix_vol1(mix, idx, 0, TAS_REG_LMIX) < 0 ||
  557. snapper_set_mix_vol1(mix, idx, 1, TAS_REG_RMIX) < 0)
  558. return -EINVAL;
  559. return 0;
  560. }
  561. static int snapper_info_mix(struct snd_kcontrol *kcontrol,
  562. struct snd_ctl_elem_info *uinfo)
  563. {
  564. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  565. uinfo->count = 2;
  566. uinfo->value.integer.min = 0;
  567. uinfo->value.integer.max = ARRAY_SIZE(mixer_volume_table) - 1;
  568. return 0;
  569. }
  570. static int snapper_get_mix(struct snd_kcontrol *kcontrol,
  571. struct snd_ctl_elem_value *ucontrol)
  572. {
  573. int idx = (int)kcontrol->private_value;
  574. struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
  575. struct pmac_tumbler *mix;
  576. if (! (mix = chip->mixer_data))
  577. return -ENODEV;
  578. ucontrol->value.integer.value[0] = mix->mix_vol[idx][0];
  579. ucontrol->value.integer.value[1] = mix->mix_vol[idx][1];
  580. return 0;
  581. }
  582. static int snapper_put_mix(struct snd_kcontrol *kcontrol,
  583. struct snd_ctl_elem_value *ucontrol)
  584. {
  585. int idx = (int)kcontrol->private_value;
  586. struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
  587. struct pmac_tumbler *mix;
  588. int change;
  589. if (! (mix = chip->mixer_data))
  590. return -ENODEV;
  591. change = mix->mix_vol[idx][0] != ucontrol->value.integer.value[0] ||
  592. mix->mix_vol[idx][1] != ucontrol->value.integer.value[1];
  593. if (change) {
  594. mix->mix_vol[idx][0] = ucontrol->value.integer.value[0];
  595. mix->mix_vol[idx][1] = ucontrol->value.integer.value[1];
  596. snapper_set_mix_vol(mix, idx);
  597. }
  598. return change;
  599. }
  600. /*
  601. * mute switches. FIXME: Turn that into software mute when both outputs are muted
  602. * to avoid codec reset on ibook M7
  603. */
  604. enum { TUMBLER_MUTE_HP, TUMBLER_MUTE_AMP, TUMBLER_MUTE_LINE };
  605. static int tumbler_get_mute_switch(struct snd_kcontrol *kcontrol,
  606. struct snd_ctl_elem_value *ucontrol)
  607. {
  608. struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
  609. struct pmac_tumbler *mix;
  610. struct pmac_gpio *gp;
  611. if (! (mix = chip->mixer_data))
  612. return -ENODEV;
  613. switch(kcontrol->private_value) {
  614. case TUMBLER_MUTE_HP:
  615. gp = &mix->hp_mute; break;
  616. case TUMBLER_MUTE_AMP:
  617. gp = &mix->amp_mute; break;
  618. case TUMBLER_MUTE_LINE:
  619. gp = &mix->line_mute; break;
  620. default:
  621. gp = NULL;
  622. }
  623. if (gp == NULL)
  624. return -EINVAL;
  625. ucontrol->value.integer.value[0] = !check_audio_gpio(gp);
  626. return 0;
  627. }
  628. static int tumbler_put_mute_switch(struct snd_kcontrol *kcontrol,
  629. struct snd_ctl_elem_value *ucontrol)
  630. {
  631. struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
  632. struct pmac_tumbler *mix;
  633. struct pmac_gpio *gp;
  634. int val;
  635. #ifdef PMAC_SUPPORT_AUTOMUTE
  636. if (chip->update_automute && chip->auto_mute)
  637. return 0; /* don't touch in the auto-mute mode */
  638. #endif
  639. if (! (mix = chip->mixer_data))
  640. return -ENODEV;
  641. switch(kcontrol->private_value) {
  642. case TUMBLER_MUTE_HP:
  643. gp = &mix->hp_mute; break;
  644. case TUMBLER_MUTE_AMP:
  645. gp = &mix->amp_mute; break;
  646. case TUMBLER_MUTE_LINE:
  647. gp = &mix->line_mute; break;
  648. default:
  649. gp = NULL;
  650. }
  651. if (gp == NULL)
  652. return -EINVAL;
  653. val = ! check_audio_gpio(gp);
  654. if (val != ucontrol->value.integer.value[0]) {
  655. write_audio_gpio(gp, ! ucontrol->value.integer.value[0]);
  656. return 1;
  657. }
  658. return 0;
  659. }
  660. static int snapper_set_capture_source(struct pmac_tumbler *mix)
  661. {
  662. if (! mix->i2c.client)
  663. return -ENODEV;
  664. if (mix->capture_source)
  665. mix->acs = mix->acs |= 2;
  666. else
  667. mix->acs &= ~2;
  668. return i2c_smbus_write_byte_data(mix->i2c.client, TAS_REG_ACS, mix->acs);
  669. }
  670. static int snapper_info_capture_source(struct snd_kcontrol *kcontrol,
  671. struct snd_ctl_elem_info *uinfo)
  672. {
  673. static char *texts[2] = {
  674. "Line", "Mic"
  675. };
  676. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  677. uinfo->count = 1;
  678. uinfo->value.enumerated.items = 2;
  679. if (uinfo->value.enumerated.item > 1)
  680. uinfo->value.enumerated.item = 1;
  681. strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
  682. return 0;
  683. }
  684. static int snapper_get_capture_source(struct snd_kcontrol *kcontrol,
  685. struct snd_ctl_elem_value *ucontrol)
  686. {
  687. struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
  688. struct pmac_tumbler *mix = chip->mixer_data;
  689. snd_assert(mix, return -ENODEV);
  690. ucontrol->value.integer.value[0] = mix->capture_source;
  691. return 0;
  692. }
  693. static int snapper_put_capture_source(struct snd_kcontrol *kcontrol,
  694. struct snd_ctl_elem_value *ucontrol)
  695. {
  696. struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
  697. struct pmac_tumbler *mix = chip->mixer_data;
  698. int change;
  699. snd_assert(mix, return -ENODEV);
  700. change = ucontrol->value.integer.value[0] != mix->capture_source;
  701. if (change) {
  702. mix->capture_source = !!ucontrol->value.integer.value[0];
  703. snapper_set_capture_source(mix);
  704. }
  705. return change;
  706. }
  707. #define DEFINE_SNAPPER_MIX(xname,idx,ofs) { \
  708. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,\
  709. .name = xname, \
  710. .info = snapper_info_mix, \
  711. .get = snapper_get_mix, \
  712. .put = snapper_put_mix, \
  713. .index = idx,\
  714. .private_value = ofs, \
  715. }
  716. /*
  717. */
  718. static struct snd_kcontrol_new tumbler_mixers[] __initdata = {
  719. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  720. .name = "Master Playback Volume",
  721. .info = tumbler_info_master_volume,
  722. .get = tumbler_get_master_volume,
  723. .put = tumbler_put_master_volume
  724. },
  725. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  726. .name = "Master Playback Switch",
  727. .info = snd_pmac_boolean_stereo_info,
  728. .get = tumbler_get_master_switch,
  729. .put = tumbler_put_master_switch
  730. },
  731. DEFINE_MONO("Tone Control - Bass", bass),
  732. DEFINE_MONO("Tone Control - Treble", treble),
  733. DEFINE_MONO("PCM Playback Volume", pcm),
  734. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  735. .name = "DRC Range",
  736. .info = tumbler_info_drc_value,
  737. .get = tumbler_get_drc_value,
  738. .put = tumbler_put_drc_value
  739. },
  740. };
  741. static struct snd_kcontrol_new snapper_mixers[] __initdata = {
  742. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  743. .name = "Master Playback Volume",
  744. .info = tumbler_info_master_volume,
  745. .get = tumbler_get_master_volume,
  746. .put = tumbler_put_master_volume
  747. },
  748. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  749. .name = "Master Playback Switch",
  750. .info = snd_pmac_boolean_stereo_info,
  751. .get = tumbler_get_master_switch,
  752. .put = tumbler_put_master_switch
  753. },
  754. DEFINE_SNAPPER_MIX("PCM Playback Volume", 0, VOL_IDX_PCM),
  755. DEFINE_SNAPPER_MIX("PCM Playback Volume", 1, VOL_IDX_PCM2),
  756. DEFINE_SNAPPER_MIX("Monitor Mix Volume", 0, VOL_IDX_ADC),
  757. DEFINE_SNAPPER_MONO("Tone Control - Bass", bass),
  758. DEFINE_SNAPPER_MONO("Tone Control - Treble", treble),
  759. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  760. .name = "DRC Range",
  761. .info = tumbler_info_drc_value,
  762. .get = tumbler_get_drc_value,
  763. .put = tumbler_put_drc_value
  764. },
  765. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  766. .name = "Input Source", /* FIXME: "Capture Source" doesn't work properly */
  767. .info = snapper_info_capture_source,
  768. .get = snapper_get_capture_source,
  769. .put = snapper_put_capture_source
  770. },
  771. };
  772. static struct snd_kcontrol_new tumbler_hp_sw __initdata = {
  773. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  774. .name = "Headphone Playback Switch",
  775. .info = snd_pmac_boolean_mono_info,
  776. .get = tumbler_get_mute_switch,
  777. .put = tumbler_put_mute_switch,
  778. .private_value = TUMBLER_MUTE_HP,
  779. };
  780. static struct snd_kcontrol_new tumbler_speaker_sw __initdata = {
  781. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  782. .name = "PC Speaker Playback Switch",
  783. .info = snd_pmac_boolean_mono_info,
  784. .get = tumbler_get_mute_switch,
  785. .put = tumbler_put_mute_switch,
  786. .private_value = TUMBLER_MUTE_AMP,
  787. };
  788. static struct snd_kcontrol_new tumbler_lineout_sw __initdata = {
  789. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  790. .name = "Line Out 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_LINE,
  795. };
  796. static struct snd_kcontrol_new tumbler_drc_sw __initdata = {
  797. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  798. .name = "DRC Switch",
  799. .info = snd_pmac_boolean_mono_info,
  800. .get = tumbler_get_drc_switch,
  801. .put = tumbler_put_drc_switch
  802. };
  803. #ifdef PMAC_SUPPORT_AUTOMUTE
  804. /*
  805. * auto-mute stuffs
  806. */
  807. static int tumbler_detect_headphone(struct snd_pmac *chip)
  808. {
  809. struct pmac_tumbler *mix = chip->mixer_data;
  810. int detect = 0;
  811. if (mix->hp_detect.addr)
  812. detect |= read_audio_gpio(&mix->hp_detect);
  813. return detect;
  814. }
  815. static int tumbler_detect_lineout(struct snd_pmac *chip)
  816. {
  817. struct pmac_tumbler *mix = chip->mixer_data;
  818. int detect = 0;
  819. if (mix->line_detect.addr)
  820. detect |= read_audio_gpio(&mix->line_detect);
  821. return detect;
  822. }
  823. static void check_mute(struct snd_pmac *chip, struct pmac_gpio *gp, int val, int do_notify,
  824. struct snd_kcontrol *sw)
  825. {
  826. if (check_audio_gpio(gp) != val) {
  827. write_audio_gpio(gp, val);
  828. if (do_notify)
  829. snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
  830. &sw->id);
  831. }
  832. }
  833. static struct work_struct device_change;
  834. static void device_change_handler(void *self)
  835. {
  836. struct snd_pmac *chip = self;
  837. struct pmac_tumbler *mix;
  838. int headphone, lineout;
  839. if (!chip)
  840. return;
  841. mix = chip->mixer_data;
  842. snd_assert(mix, return);
  843. headphone = tumbler_detect_headphone(chip);
  844. lineout = tumbler_detect_lineout(chip);
  845. DBG("headphone: %d, lineout: %d\n", headphone, lineout);
  846. if (headphone || lineout) {
  847. /* unmute headphone/lineout & mute speaker */
  848. if (headphone)
  849. check_mute(chip, &mix->hp_mute, 0, mix->auto_mute_notify,
  850. chip->master_sw_ctl);
  851. if (lineout && mix->line_mute.addr != 0)
  852. check_mute(chip, &mix->line_mute, 0, mix->auto_mute_notify,
  853. chip->lineout_sw_ctl);
  854. if (mix->anded_reset)
  855. msleep(10);
  856. check_mute(chip, &mix->amp_mute, 1, mix->auto_mute_notify,
  857. chip->speaker_sw_ctl);
  858. } else {
  859. /* unmute speaker, mute others */
  860. check_mute(chip, &mix->amp_mute, 0, mix->auto_mute_notify,
  861. chip->speaker_sw_ctl);
  862. if (mix->anded_reset)
  863. msleep(10);
  864. check_mute(chip, &mix->hp_mute, 1, mix->auto_mute_notify,
  865. chip->master_sw_ctl);
  866. if (mix->line_mute.addr != 0)
  867. check_mute(chip, &mix->line_mute, 1, mix->auto_mute_notify,
  868. chip->lineout_sw_ctl);
  869. }
  870. if (mix->auto_mute_notify)
  871. snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
  872. &chip->hp_detect_ctl->id);
  873. #ifdef CONFIG_SND_POWERMAC_AUTO_DRC
  874. mix->drc_enable = ! (headphone || lineout);
  875. if (mix->auto_mute_notify)
  876. snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
  877. &chip->drc_sw_ctl->id);
  878. if (chip->model == PMAC_TUMBLER)
  879. tumbler_set_drc(mix);
  880. else
  881. snapper_set_drc(mix);
  882. #endif
  883. /* reset the master volume so the correct amplification is applied */
  884. tumbler_set_master_volume(mix);
  885. }
  886. static void tumbler_update_automute(struct snd_pmac *chip, int do_notify)
  887. {
  888. if (chip->auto_mute) {
  889. struct pmac_tumbler *mix;
  890. mix = chip->mixer_data;
  891. snd_assert(mix, return);
  892. mix->auto_mute_notify = do_notify;
  893. schedule_work(&device_change);
  894. }
  895. }
  896. #endif /* PMAC_SUPPORT_AUTOMUTE */
  897. /* interrupt - headphone plug changed */
  898. static irqreturn_t headphone_intr(int irq, void *devid)
  899. {
  900. struct snd_pmac *chip = devid;
  901. if (chip->update_automute && chip->initialized) {
  902. chip->update_automute(chip, 1);
  903. return IRQ_HANDLED;
  904. }
  905. return IRQ_NONE;
  906. }
  907. /* look for audio-gpio device */
  908. static struct device_node *find_audio_device(const char *name)
  909. {
  910. struct device_node *np;
  911. if (! (np = find_devices("gpio")))
  912. return NULL;
  913. for (np = np->child; np; np = np->sibling) {
  914. const char *property = get_property(np, "audio-gpio", NULL);
  915. if (property && strcmp(property, name) == 0)
  916. return np;
  917. }
  918. return NULL;
  919. }
  920. /* look for audio-gpio device */
  921. static struct device_node *find_compatible_audio_device(const char *name)
  922. {
  923. struct device_node *np;
  924. if (! (np = find_devices("gpio")))
  925. return NULL;
  926. for (np = np->child; np; np = np->sibling) {
  927. if (device_is_compatible(np, name))
  928. return np;
  929. }
  930. return NULL;
  931. }
  932. /* find an audio device and get its address */
  933. static long tumbler_find_device(const char *device, const char *platform,
  934. struct pmac_gpio *gp, int is_compatible)
  935. {
  936. struct device_node *node;
  937. const u32 *base;
  938. u32 addr;
  939. if (is_compatible)
  940. node = find_compatible_audio_device(device);
  941. else
  942. node = find_audio_device(device);
  943. if (! node) {
  944. DBG("(W) cannot find audio device %s !\n", device);
  945. snd_printdd("cannot find device %s\n", device);
  946. return -ENODEV;
  947. }
  948. base = get_property(node, "AAPL,address", NULL);
  949. if (! base) {
  950. base = get_property(node, "reg", NULL);
  951. if (!base) {
  952. DBG("(E) cannot find address for device %s !\n", device);
  953. snd_printd("cannot find address for device %s\n", device);
  954. return -ENODEV;
  955. }
  956. addr = *base;
  957. if (addr < 0x50)
  958. addr += 0x50;
  959. } else
  960. addr = *base;
  961. gp->addr = addr & 0x0000ffff;
  962. /* Try to find the active state, default to 0 ! */
  963. base = get_property(node, "audio-gpio-active-state", NULL);
  964. if (base) {
  965. gp->active_state = *base;
  966. gp->active_val = (*base) ? 0x5 : 0x4;
  967. gp->inactive_val = (*base) ? 0x4 : 0x5;
  968. } else {
  969. const u32 *prop = NULL;
  970. gp->active_state = 0;
  971. gp->active_val = 0x4;
  972. gp->inactive_val = 0x5;
  973. /* Here are some crude hacks to extract the GPIO polarity and
  974. * open collector informations out of the do-platform script
  975. * as we don't yet have an interpreter for these things
  976. */
  977. if (platform)
  978. prop = get_property(node, platform, NULL);
  979. if (prop) {
  980. if (prop[3] == 0x9 && prop[4] == 0x9) {
  981. gp->active_val = 0xd;
  982. gp->inactive_val = 0xc;
  983. }
  984. if (prop[3] == 0x1 && prop[4] == 0x1) {
  985. gp->active_val = 0x5;
  986. gp->inactive_val = 0x4;
  987. }
  988. }
  989. }
  990. DBG("(I) GPIO device %s found, offset: %x, active state: %d !\n",
  991. device, gp->addr, gp->active_state);
  992. return irq_of_parse_and_map(node, 0);
  993. }
  994. /* reset audio */
  995. static void tumbler_reset_audio(struct snd_pmac *chip)
  996. {
  997. struct pmac_tumbler *mix = chip->mixer_data;
  998. if (mix->anded_reset) {
  999. DBG("(I) codec anded reset !\n");
  1000. write_audio_gpio(&mix->hp_mute, 0);
  1001. write_audio_gpio(&mix->amp_mute, 0);
  1002. msleep(200);
  1003. write_audio_gpio(&mix->hp_mute, 1);
  1004. write_audio_gpio(&mix->amp_mute, 1);
  1005. msleep(100);
  1006. write_audio_gpio(&mix->hp_mute, 0);
  1007. write_audio_gpio(&mix->amp_mute, 0);
  1008. msleep(100);
  1009. } else {
  1010. DBG("(I) codec normal reset !\n");
  1011. write_audio_gpio(&mix->audio_reset, 0);
  1012. msleep(200);
  1013. write_audio_gpio(&mix->audio_reset, 1);
  1014. msleep(100);
  1015. write_audio_gpio(&mix->audio_reset, 0);
  1016. msleep(100);
  1017. }
  1018. }
  1019. #ifdef CONFIG_PM
  1020. /* suspend mixer */
  1021. static void tumbler_suspend(struct snd_pmac *chip)
  1022. {
  1023. struct pmac_tumbler *mix = chip->mixer_data;
  1024. if (mix->headphone_irq >= 0)
  1025. disable_irq(mix->headphone_irq);
  1026. if (mix->lineout_irq >= 0)
  1027. disable_irq(mix->lineout_irq);
  1028. mix->save_master_switch[0] = mix->master_switch[0];
  1029. mix->save_master_switch[1] = mix->master_switch[1];
  1030. mix->save_master_vol[0] = mix->master_vol[0];
  1031. mix->save_master_vol[1] = mix->master_vol[1];
  1032. mix->master_switch[0] = mix->master_switch[1] = 0;
  1033. tumbler_set_master_volume(mix);
  1034. if (!mix->anded_reset) {
  1035. write_audio_gpio(&mix->amp_mute, 1);
  1036. write_audio_gpio(&mix->hp_mute, 1);
  1037. }
  1038. if (chip->model == PMAC_SNAPPER) {
  1039. mix->acs |= 1;
  1040. i2c_smbus_write_byte_data(mix->i2c.client, TAS_REG_ACS, mix->acs);
  1041. }
  1042. if (mix->anded_reset) {
  1043. write_audio_gpio(&mix->amp_mute, 1);
  1044. write_audio_gpio(&mix->hp_mute, 1);
  1045. } else
  1046. write_audio_gpio(&mix->audio_reset, 1);
  1047. }
  1048. /* resume mixer */
  1049. static void tumbler_resume(struct snd_pmac *chip)
  1050. {
  1051. struct pmac_tumbler *mix = chip->mixer_data;
  1052. snd_assert(mix, return);
  1053. mix->acs &= ~1;
  1054. mix->master_switch[0] = mix->save_master_switch[0];
  1055. mix->master_switch[1] = mix->save_master_switch[1];
  1056. mix->master_vol[0] = mix->save_master_vol[0];
  1057. mix->master_vol[1] = mix->save_master_vol[1];
  1058. tumbler_reset_audio(chip);
  1059. if (mix->i2c.client && mix->i2c.init_client) {
  1060. if (mix->i2c.init_client(&mix->i2c) < 0)
  1061. printk(KERN_ERR "tumbler_init_client error\n");
  1062. } else
  1063. printk(KERN_ERR "tumbler: i2c is not initialized\n");
  1064. if (chip->model == PMAC_TUMBLER) {
  1065. tumbler_set_mono_volume(mix, &tumbler_pcm_vol_info);
  1066. tumbler_set_mono_volume(mix, &tumbler_bass_vol_info);
  1067. tumbler_set_mono_volume(mix, &tumbler_treble_vol_info);
  1068. tumbler_set_drc(mix);
  1069. } else {
  1070. snapper_set_mix_vol(mix, VOL_IDX_PCM);
  1071. snapper_set_mix_vol(mix, VOL_IDX_PCM2);
  1072. snapper_set_mix_vol(mix, VOL_IDX_ADC);
  1073. tumbler_set_mono_volume(mix, &snapper_bass_vol_info);
  1074. tumbler_set_mono_volume(mix, &snapper_treble_vol_info);
  1075. snapper_set_drc(mix);
  1076. snapper_set_capture_source(mix);
  1077. }
  1078. tumbler_set_master_volume(mix);
  1079. if (chip->update_automute)
  1080. chip->update_automute(chip, 0);
  1081. if (mix->headphone_irq >= 0) {
  1082. unsigned char val;
  1083. enable_irq(mix->headphone_irq);
  1084. /* activate headphone status interrupts */
  1085. val = do_gpio_read(&mix->hp_detect);
  1086. do_gpio_write(&mix->hp_detect, val | 0x80);
  1087. }
  1088. if (mix->lineout_irq >= 0)
  1089. enable_irq(mix->lineout_irq);
  1090. }
  1091. #endif
  1092. /* initialize tumbler */
  1093. static int __init tumbler_init(struct snd_pmac *chip)
  1094. {
  1095. int irq;
  1096. struct pmac_tumbler *mix = chip->mixer_data;
  1097. snd_assert(mix, return -EINVAL);
  1098. if (tumbler_find_device("audio-hw-reset",
  1099. "platform-do-hw-reset",
  1100. &mix->audio_reset, 0) < 0)
  1101. tumbler_find_device("hw-reset",
  1102. "platform-do-hw-reset",
  1103. &mix->audio_reset, 1);
  1104. if (tumbler_find_device("amp-mute",
  1105. "platform-do-amp-mute",
  1106. &mix->amp_mute, 0) < 0)
  1107. tumbler_find_device("amp-mute",
  1108. "platform-do-amp-mute",
  1109. &mix->amp_mute, 1);
  1110. if (tumbler_find_device("headphone-mute",
  1111. "platform-do-headphone-mute",
  1112. &mix->hp_mute, 0) < 0)
  1113. tumbler_find_device("headphone-mute",
  1114. "platform-do-headphone-mute",
  1115. &mix->hp_mute, 1);
  1116. if (tumbler_find_device("line-output-mute",
  1117. "platform-do-lineout-mute",
  1118. &mix->line_mute, 0) < 0)
  1119. tumbler_find_device("line-output-mute",
  1120. "platform-do-lineout-mute",
  1121. &mix->line_mute, 1);
  1122. irq = tumbler_find_device("headphone-detect",
  1123. NULL, &mix->hp_detect, 0);
  1124. if (irq <= NO_IRQ)
  1125. irq = tumbler_find_device("headphone-detect",
  1126. NULL, &mix->hp_detect, 1);
  1127. if (irq <= NO_IRQ)
  1128. irq = tumbler_find_device("keywest-gpio15",
  1129. NULL, &mix->hp_detect, 1);
  1130. mix->headphone_irq = irq;
  1131. irq = tumbler_find_device("line-output-detect",
  1132. NULL, &mix->line_detect, 0);
  1133. if (irq <= NO_IRQ)
  1134. irq = tumbler_find_device("line-output-detect",
  1135. NULL, &mix->line_detect, 1);
  1136. mix->lineout_irq = irq;
  1137. tumbler_reset_audio(chip);
  1138. return 0;
  1139. }
  1140. static void tumbler_cleanup(struct snd_pmac *chip)
  1141. {
  1142. struct pmac_tumbler *mix = chip->mixer_data;
  1143. if (! mix)
  1144. return;
  1145. if (mix->headphone_irq >= 0)
  1146. free_irq(mix->headphone_irq, chip);
  1147. if (mix->lineout_irq >= 0)
  1148. free_irq(mix->lineout_irq, chip);
  1149. tumbler_gpio_free(&mix->audio_reset);
  1150. tumbler_gpio_free(&mix->amp_mute);
  1151. tumbler_gpio_free(&mix->hp_mute);
  1152. tumbler_gpio_free(&mix->hp_detect);
  1153. snd_pmac_keywest_cleanup(&mix->i2c);
  1154. kfree(mix);
  1155. chip->mixer_data = NULL;
  1156. }
  1157. /* exported */
  1158. int __init snd_pmac_tumbler_init(struct snd_pmac *chip)
  1159. {
  1160. int i, err;
  1161. struct pmac_tumbler *mix;
  1162. u32 *paddr;
  1163. struct device_node *tas_node, *np;
  1164. char *chipname;
  1165. #ifdef CONFIG_KMOD
  1166. if (current->fs->root)
  1167. request_module("i2c-powermac");
  1168. #endif /* CONFIG_KMOD */
  1169. mix = kzalloc(sizeof(*mix), GFP_KERNEL);
  1170. if (! mix)
  1171. return -ENOMEM;
  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. }