tumbler.c 38 KB

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