tumbler.c 38 KB

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