tumbler.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491
  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 <linux/string.h>
  32. #include <linux/of_irq.h>
  33. #include <linux/io.h>
  34. #include <sound/core.h>
  35. #include <asm/irq.h>
  36. #include <asm/machdep.h>
  37. #include <asm/pmac_feature.h>
  38. #include "pmac.h"
  39. #include "tumbler_volume.h"
  40. #undef DEBUG
  41. #ifdef DEBUG
  42. #define DBG(fmt...) printk(KERN_DEBUG fmt)
  43. #else
  44. #define DBG(fmt...)
  45. #endif
  46. #define IS_G4DA (of_machine_is_compatible("PowerMac3,4"))
  47. /* i2c address for tumbler */
  48. #define TAS_I2C_ADDR 0x34
  49. /* registers */
  50. #define TAS_REG_MCS 0x01 /* main control */
  51. #define TAS_REG_DRC 0x02
  52. #define TAS_REG_VOL 0x04
  53. #define TAS_REG_TREBLE 0x05
  54. #define TAS_REG_BASS 0x06
  55. #define TAS_REG_INPUT1 0x07
  56. #define TAS_REG_INPUT2 0x08
  57. /* tas3001c */
  58. #define TAS_REG_PCM TAS_REG_INPUT1
  59. /* tas3004 */
  60. #define TAS_REG_LMIX TAS_REG_INPUT1
  61. #define TAS_REG_RMIX TAS_REG_INPUT2
  62. #define TAS_REG_MCS2 0x43 /* main control 2 */
  63. #define TAS_REG_ACS 0x40 /* analog control */
  64. /* mono volumes for tas3001c/tas3004 */
  65. enum {
  66. VOL_IDX_PCM_MONO, /* tas3001c only */
  67. VOL_IDX_BASS, VOL_IDX_TREBLE,
  68. VOL_IDX_LAST_MONO
  69. };
  70. /* stereo volumes for tas3004 */
  71. enum {
  72. VOL_IDX_PCM, VOL_IDX_PCM2, VOL_IDX_ADC,
  73. VOL_IDX_LAST_MIX
  74. };
  75. struct pmac_gpio {
  76. unsigned int addr;
  77. u8 active_val;
  78. u8 inactive_val;
  79. u8 active_state;
  80. };
  81. struct pmac_tumbler {
  82. struct pmac_keywest i2c;
  83. struct pmac_gpio audio_reset;
  84. struct pmac_gpio amp_mute;
  85. struct pmac_gpio line_mute;
  86. struct pmac_gpio line_detect;
  87. struct pmac_gpio hp_mute;
  88. struct pmac_gpio hp_detect;
  89. int headphone_irq;
  90. int lineout_irq;
  91. unsigned int save_master_vol[2];
  92. unsigned int master_vol[2];
  93. unsigned int save_master_switch[2];
  94. unsigned int master_switch[2];
  95. unsigned int mono_vol[VOL_IDX_LAST_MONO];
  96. unsigned int mix_vol[VOL_IDX_LAST_MIX][2]; /* stereo volumes for tas3004 */
  97. int drc_range;
  98. int drc_enable;
  99. int capture_source;
  100. int anded_reset;
  101. int auto_mute_notify;
  102. int reset_on_sleep;
  103. u8 acs;
  104. };
  105. /*
  106. */
  107. static int send_init_client(struct pmac_keywest *i2c, unsigned int *regs)
  108. {
  109. while (*regs > 0) {
  110. int err, count = 10;
  111. do {
  112. err = i2c_smbus_write_byte_data(i2c->client,
  113. regs[0], regs[1]);
  114. if (err >= 0)
  115. break;
  116. DBG("(W) i2c error %d\n", err);
  117. mdelay(10);
  118. } while (count--);
  119. if (err < 0)
  120. return -ENXIO;
  121. regs += 2;
  122. }
  123. return 0;
  124. }
  125. static int tumbler_init_client(struct pmac_keywest *i2c)
  126. {
  127. static unsigned int regs[] = {
  128. /* normal operation, SCLK=64fps, i2s output, i2s input, 16bit width */
  129. TAS_REG_MCS, (1<<6)|(2<<4)|(2<<2)|0,
  130. 0, /* terminator */
  131. };
  132. DBG("(I) tumbler init client\n");
  133. return send_init_client(i2c, regs);
  134. }
  135. static int snapper_init_client(struct pmac_keywest *i2c)
  136. {
  137. static unsigned int regs[] = {
  138. /* normal operation, SCLK=64fps, i2s output, 16bit width */
  139. TAS_REG_MCS, (1<<6)|(2<<4)|0,
  140. /* normal operation, all-pass mode */
  141. TAS_REG_MCS2, (1<<1),
  142. /* normal output, no deemphasis, A input, power-up, line-in */
  143. TAS_REG_ACS, 0,
  144. 0, /* terminator */
  145. };
  146. DBG("(I) snapper init client\n");
  147. return send_init_client(i2c, regs);
  148. }
  149. /*
  150. * gpio access
  151. */
  152. #define do_gpio_write(gp, val) \
  153. pmac_call_feature(PMAC_FTR_WRITE_GPIO, NULL, (gp)->addr, val)
  154. #define do_gpio_read(gp) \
  155. pmac_call_feature(PMAC_FTR_READ_GPIO, NULL, (gp)->addr, 0)
  156. #define tumbler_gpio_free(gp) /* NOP */
  157. static void write_audio_gpio(struct pmac_gpio *gp, int active)
  158. {
  159. if (! gp->addr)
  160. return;
  161. active = active ? gp->active_val : gp->inactive_val;
  162. do_gpio_write(gp, active);
  163. DBG("(I) gpio %x write %d\n", gp->addr, active);
  164. }
  165. static int check_audio_gpio(struct pmac_gpio *gp)
  166. {
  167. int ret;
  168. if (! gp->addr)
  169. return 0;
  170. ret = do_gpio_read(gp);
  171. return (ret & 0x1) == (gp->active_val & 0x1);
  172. }
  173. static int read_audio_gpio(struct pmac_gpio *gp)
  174. {
  175. int ret;
  176. if (! gp->addr)
  177. return 0;
  178. ret = do_gpio_read(gp);
  179. ret = (ret & 0x02) !=0;
  180. return ret == gp->active_state;
  181. }
  182. /*
  183. * update master volume
  184. */
  185. static int tumbler_set_master_volume(struct pmac_tumbler *mix)
  186. {
  187. unsigned char block[6];
  188. unsigned int left_vol, right_vol;
  189. if (! mix->i2c.client)
  190. return -ENODEV;
  191. if (! mix->master_switch[0])
  192. left_vol = 0;
  193. else {
  194. left_vol = mix->master_vol[0];
  195. if (left_vol >= ARRAY_SIZE(master_volume_table))
  196. left_vol = ARRAY_SIZE(master_volume_table) - 1;
  197. left_vol = master_volume_table[left_vol];
  198. }
  199. if (! mix->master_switch[1])
  200. right_vol = 0;
  201. else {
  202. right_vol = mix->master_vol[1];
  203. if (right_vol >= ARRAY_SIZE(master_volume_table))
  204. right_vol = ARRAY_SIZE(master_volume_table) - 1;
  205. right_vol = master_volume_table[right_vol];
  206. }
  207. block[0] = (left_vol >> 16) & 0xff;
  208. block[1] = (left_vol >> 8) & 0xff;
  209. block[2] = (left_vol >> 0) & 0xff;
  210. block[3] = (right_vol >> 16) & 0xff;
  211. block[4] = (right_vol >> 8) & 0xff;
  212. block[5] = (right_vol >> 0) & 0xff;
  213. if (i2c_smbus_write_i2c_block_data(mix->i2c.client, TAS_REG_VOL, 6,
  214. block) < 0) {
  215. snd_printk(KERN_ERR "failed to set volume \n");
  216. return -EINVAL;
  217. }
  218. DBG("(I) succeeded to set volume (%u, %u)\n", left_vol, right_vol);
  219. return 0;
  220. }
  221. /* output volume */
  222. static int tumbler_info_master_volume(struct snd_kcontrol *kcontrol,
  223. struct snd_ctl_elem_info *uinfo)
  224. {
  225. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  226. uinfo->count = 2;
  227. uinfo->value.integer.min = 0;
  228. uinfo->value.integer.max = ARRAY_SIZE(master_volume_table) - 1;
  229. return 0;
  230. }
  231. static int tumbler_get_master_volume(struct snd_kcontrol *kcontrol,
  232. struct snd_ctl_elem_value *ucontrol)
  233. {
  234. struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
  235. struct pmac_tumbler *mix = chip->mixer_data;
  236. ucontrol->value.integer.value[0] = mix->master_vol[0];
  237. ucontrol->value.integer.value[1] = mix->master_vol[1];
  238. return 0;
  239. }
  240. static int tumbler_put_master_volume(struct snd_kcontrol *kcontrol,
  241. struct snd_ctl_elem_value *ucontrol)
  242. {
  243. struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
  244. struct pmac_tumbler *mix = chip->mixer_data;
  245. unsigned int vol[2];
  246. int change;
  247. vol[0] = ucontrol->value.integer.value[0];
  248. vol[1] = ucontrol->value.integer.value[1];
  249. if (vol[0] >= ARRAY_SIZE(master_volume_table) ||
  250. vol[1] >= ARRAY_SIZE(master_volume_table))
  251. return -EINVAL;
  252. change = mix->master_vol[0] != vol[0] ||
  253. mix->master_vol[1] != vol[1];
  254. if (change) {
  255. mix->master_vol[0] = vol[0];
  256. mix->master_vol[1] = vol[1];
  257. tumbler_set_master_volume(mix);
  258. }
  259. return change;
  260. }
  261. /* output switch */
  262. static int tumbler_get_master_switch(struct snd_kcontrol *kcontrol,
  263. struct snd_ctl_elem_value *ucontrol)
  264. {
  265. struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
  266. struct pmac_tumbler *mix = chip->mixer_data;
  267. ucontrol->value.integer.value[0] = mix->master_switch[0];
  268. ucontrol->value.integer.value[1] = mix->master_switch[1];
  269. return 0;
  270. }
  271. static int tumbler_put_master_switch(struct snd_kcontrol *kcontrol,
  272. struct snd_ctl_elem_value *ucontrol)
  273. {
  274. struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
  275. struct pmac_tumbler *mix = chip->mixer_data;
  276. int change;
  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(KERN_ERR "failed to set DRC\n");
  310. return -EINVAL;
  311. }
  312. DBG("(I) succeeded to set DRC (%u, %u)\n", val[0], val[1]);
  313. return 0;
  314. }
  315. /*
  316. * TAS3004
  317. */
  318. #define TAS3004_DRC_MAX 0xef
  319. static int snapper_set_drc(struct pmac_tumbler *mix)
  320. {
  321. unsigned char val[6];
  322. if (! mix->i2c.client)
  323. return -ENODEV;
  324. if (mix->drc_enable)
  325. val[0] = 0x50; /* 3:1 above threshold */
  326. else
  327. val[0] = 0x51; /* disabled */
  328. val[1] = 0x02; /* 1:1 below threshold */
  329. if (mix->drc_range > 0xef)
  330. val[2] = 0xef;
  331. else if (mix->drc_range < 0)
  332. val[2] = 0x00;
  333. else
  334. val[2] = mix->drc_range;
  335. val[3] = 0xb0;
  336. val[4] = 0x60;
  337. val[5] = 0xa0;
  338. if (i2c_smbus_write_i2c_block_data(mix->i2c.client, TAS_REG_DRC,
  339. 6, val) < 0) {
  340. snd_printk(KERN_ERR "failed to set DRC\n");
  341. return -EINVAL;
  342. }
  343. DBG("(I) succeeded to set DRC (%u, %u)\n", val[0], val[1]);
  344. return 0;
  345. }
  346. static int tumbler_info_drc_value(struct snd_kcontrol *kcontrol,
  347. struct snd_ctl_elem_info *uinfo)
  348. {
  349. struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
  350. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  351. uinfo->count = 1;
  352. uinfo->value.integer.min = 0;
  353. uinfo->value.integer.max =
  354. chip->model == PMAC_TUMBLER ? TAS3001_DRC_MAX : TAS3004_DRC_MAX;
  355. return 0;
  356. }
  357. static int tumbler_get_drc_value(struct snd_kcontrol *kcontrol,
  358. struct snd_ctl_elem_value *ucontrol)
  359. {
  360. struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
  361. struct pmac_tumbler *mix;
  362. if (! (mix = chip->mixer_data))
  363. return -ENODEV;
  364. ucontrol->value.integer.value[0] = mix->drc_range;
  365. return 0;
  366. }
  367. static int tumbler_put_drc_value(struct snd_kcontrol *kcontrol,
  368. struct snd_ctl_elem_value *ucontrol)
  369. {
  370. struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
  371. struct pmac_tumbler *mix;
  372. unsigned int val;
  373. int change;
  374. if (! (mix = chip->mixer_data))
  375. return -ENODEV;
  376. val = ucontrol->value.integer.value[0];
  377. if (chip->model == PMAC_TUMBLER) {
  378. if (val > TAS3001_DRC_MAX)
  379. return -EINVAL;
  380. } else {
  381. if (val > TAS3004_DRC_MAX)
  382. return -EINVAL;
  383. }
  384. change = mix->drc_range != val;
  385. if (change) {
  386. mix->drc_range = val;
  387. if (chip->model == PMAC_TUMBLER)
  388. tumbler_set_drc(mix);
  389. else
  390. snapper_set_drc(mix);
  391. }
  392. return change;
  393. }
  394. static int tumbler_get_drc_switch(struct snd_kcontrol *kcontrol,
  395. struct snd_ctl_elem_value *ucontrol)
  396. {
  397. struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
  398. struct pmac_tumbler *mix;
  399. if (! (mix = chip->mixer_data))
  400. return -ENODEV;
  401. ucontrol->value.integer.value[0] = mix->drc_enable;
  402. return 0;
  403. }
  404. static int tumbler_put_drc_switch(struct snd_kcontrol *kcontrol,
  405. struct snd_ctl_elem_value *ucontrol)
  406. {
  407. struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
  408. struct pmac_tumbler *mix;
  409. int change;
  410. if (! (mix = chip->mixer_data))
  411. return -ENODEV;
  412. change = mix->drc_enable != ucontrol->value.integer.value[0];
  413. if (change) {
  414. mix->drc_enable = !!ucontrol->value.integer.value[0];
  415. if (chip->model == PMAC_TUMBLER)
  416. tumbler_set_drc(mix);
  417. else
  418. snapper_set_drc(mix);
  419. }
  420. return change;
  421. }
  422. /*
  423. * mono volumes
  424. */
  425. struct tumbler_mono_vol {
  426. int index;
  427. int reg;
  428. int bytes;
  429. unsigned int max;
  430. unsigned int *table;
  431. };
  432. static int tumbler_set_mono_volume(struct pmac_tumbler *mix,
  433. struct tumbler_mono_vol *info)
  434. {
  435. unsigned char block[4];
  436. unsigned int vol;
  437. int i;
  438. if (! mix->i2c.client)
  439. return -ENODEV;
  440. vol = mix->mono_vol[info->index];
  441. if (vol >= info->max)
  442. vol = info->max - 1;
  443. vol = info->table[vol];
  444. for (i = 0; i < info->bytes; i++)
  445. block[i] = (vol >> ((info->bytes - i - 1) * 8)) & 0xff;
  446. if (i2c_smbus_write_i2c_block_data(mix->i2c.client, info->reg,
  447. info->bytes, block) < 0) {
  448. snd_printk(KERN_ERR "failed to set mono volume %d\n",
  449. info->index);
  450. return -EINVAL;
  451. }
  452. return 0;
  453. }
  454. static int tumbler_info_mono(struct snd_kcontrol *kcontrol,
  455. struct snd_ctl_elem_info *uinfo)
  456. {
  457. struct tumbler_mono_vol *info = (struct tumbler_mono_vol *)kcontrol->private_value;
  458. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  459. uinfo->count = 1;
  460. uinfo->value.integer.min = 0;
  461. uinfo->value.integer.max = info->max - 1;
  462. return 0;
  463. }
  464. static int tumbler_get_mono(struct snd_kcontrol *kcontrol,
  465. struct snd_ctl_elem_value *ucontrol)
  466. {
  467. struct tumbler_mono_vol *info = (struct tumbler_mono_vol *)kcontrol->private_value;
  468. struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
  469. struct pmac_tumbler *mix;
  470. if (! (mix = chip->mixer_data))
  471. return -ENODEV;
  472. ucontrol->value.integer.value[0] = mix->mono_vol[info->index];
  473. return 0;
  474. }
  475. static int tumbler_put_mono(struct snd_kcontrol *kcontrol,
  476. struct snd_ctl_elem_value *ucontrol)
  477. {
  478. struct tumbler_mono_vol *info = (struct tumbler_mono_vol *)kcontrol->private_value;
  479. struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
  480. struct pmac_tumbler *mix;
  481. unsigned int vol;
  482. int change;
  483. if (! (mix = chip->mixer_data))
  484. return -ENODEV;
  485. vol = ucontrol->value.integer.value[0];
  486. if (vol >= info->max)
  487. return -EINVAL;
  488. change = mix->mono_vol[info->index] != vol;
  489. if (change) {
  490. mix->mono_vol[info->index] = vol;
  491. tumbler_set_mono_volume(mix, info);
  492. }
  493. return change;
  494. }
  495. /* TAS3001c mono volumes */
  496. static struct tumbler_mono_vol tumbler_pcm_vol_info = {
  497. .index = VOL_IDX_PCM_MONO,
  498. .reg = TAS_REG_PCM,
  499. .bytes = 3,
  500. .max = ARRAY_SIZE(mixer_volume_table),
  501. .table = mixer_volume_table,
  502. };
  503. static struct tumbler_mono_vol tumbler_bass_vol_info = {
  504. .index = VOL_IDX_BASS,
  505. .reg = TAS_REG_BASS,
  506. .bytes = 1,
  507. .max = ARRAY_SIZE(bass_volume_table),
  508. .table = bass_volume_table,
  509. };
  510. static struct tumbler_mono_vol tumbler_treble_vol_info = {
  511. .index = VOL_IDX_TREBLE,
  512. .reg = TAS_REG_TREBLE,
  513. .bytes = 1,
  514. .max = ARRAY_SIZE(treble_volume_table),
  515. .table = treble_volume_table,
  516. };
  517. /* TAS3004 mono volumes */
  518. static struct tumbler_mono_vol snapper_bass_vol_info = {
  519. .index = VOL_IDX_BASS,
  520. .reg = TAS_REG_BASS,
  521. .bytes = 1,
  522. .max = ARRAY_SIZE(snapper_bass_volume_table),
  523. .table = snapper_bass_volume_table,
  524. };
  525. static struct tumbler_mono_vol snapper_treble_vol_info = {
  526. .index = VOL_IDX_TREBLE,
  527. .reg = TAS_REG_TREBLE,
  528. .bytes = 1,
  529. .max = ARRAY_SIZE(snapper_treble_volume_table),
  530. .table = snapper_treble_volume_table,
  531. };
  532. #define DEFINE_MONO(xname,type) { \
  533. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,\
  534. .name = xname, \
  535. .info = tumbler_info_mono, \
  536. .get = tumbler_get_mono, \
  537. .put = tumbler_put_mono, \
  538. .private_value = (unsigned long)(&tumbler_##type##_vol_info), \
  539. }
  540. #define DEFINE_SNAPPER_MONO(xname,type) { \
  541. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,\
  542. .name = xname, \
  543. .info = tumbler_info_mono, \
  544. .get = tumbler_get_mono, \
  545. .put = tumbler_put_mono, \
  546. .private_value = (unsigned long)(&snapper_##type##_vol_info), \
  547. }
  548. /*
  549. * snapper mixer volumes
  550. */
  551. static int snapper_set_mix_vol1(struct pmac_tumbler *mix, int idx, int ch, int reg)
  552. {
  553. int i, j, vol;
  554. unsigned char block[9];
  555. vol = mix->mix_vol[idx][ch];
  556. if (vol >= ARRAY_SIZE(mixer_volume_table)) {
  557. vol = ARRAY_SIZE(mixer_volume_table) - 1;
  558. mix->mix_vol[idx][ch] = vol;
  559. }
  560. for (i = 0; i < 3; i++) {
  561. vol = mix->mix_vol[i][ch];
  562. vol = mixer_volume_table[vol];
  563. for (j = 0; j < 3; j++)
  564. block[i * 3 + j] = (vol >> ((2 - j) * 8)) & 0xff;
  565. }
  566. if (i2c_smbus_write_i2c_block_data(mix->i2c.client, reg,
  567. 9, block) < 0) {
  568. snd_printk(KERN_ERR "failed to set mono volume %d\n", reg);
  569. return -EINVAL;
  570. }
  571. return 0;
  572. }
  573. static int snapper_set_mix_vol(struct pmac_tumbler *mix, int idx)
  574. {
  575. if (! mix->i2c.client)
  576. return -ENODEV;
  577. if (snapper_set_mix_vol1(mix, idx, 0, TAS_REG_LMIX) < 0 ||
  578. snapper_set_mix_vol1(mix, idx, 1, TAS_REG_RMIX) < 0)
  579. return -EINVAL;
  580. return 0;
  581. }
  582. static int snapper_info_mix(struct snd_kcontrol *kcontrol,
  583. struct snd_ctl_elem_info *uinfo)
  584. {
  585. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  586. uinfo->count = 2;
  587. uinfo->value.integer.min = 0;
  588. uinfo->value.integer.max = ARRAY_SIZE(mixer_volume_table) - 1;
  589. return 0;
  590. }
  591. static int snapper_get_mix(struct snd_kcontrol *kcontrol,
  592. struct snd_ctl_elem_value *ucontrol)
  593. {
  594. int idx = (int)kcontrol->private_value;
  595. struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
  596. struct pmac_tumbler *mix;
  597. if (! (mix = chip->mixer_data))
  598. return -ENODEV;
  599. ucontrol->value.integer.value[0] = mix->mix_vol[idx][0];
  600. ucontrol->value.integer.value[1] = mix->mix_vol[idx][1];
  601. return 0;
  602. }
  603. static int snapper_put_mix(struct snd_kcontrol *kcontrol,
  604. struct snd_ctl_elem_value *ucontrol)
  605. {
  606. int idx = (int)kcontrol->private_value;
  607. struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
  608. struct pmac_tumbler *mix;
  609. unsigned int vol[2];
  610. int change;
  611. if (! (mix = chip->mixer_data))
  612. return -ENODEV;
  613. vol[0] = ucontrol->value.integer.value[0];
  614. vol[1] = ucontrol->value.integer.value[1];
  615. if (vol[0] >= ARRAY_SIZE(mixer_volume_table) ||
  616. vol[1] >= ARRAY_SIZE(mixer_volume_table))
  617. return -EINVAL;
  618. change = mix->mix_vol[idx][0] != vol[0] ||
  619. mix->mix_vol[idx][1] != vol[1];
  620. if (change) {
  621. mix->mix_vol[idx][0] = vol[0];
  622. mix->mix_vol[idx][1] = vol[1];
  623. snapper_set_mix_vol(mix, idx);
  624. }
  625. return change;
  626. }
  627. /*
  628. * mute switches. FIXME: Turn that into software mute when both outputs are muted
  629. * to avoid codec reset on ibook M7
  630. */
  631. enum { TUMBLER_MUTE_HP, TUMBLER_MUTE_AMP, TUMBLER_MUTE_LINE };
  632. static int tumbler_get_mute_switch(struct snd_kcontrol *kcontrol,
  633. struct snd_ctl_elem_value *ucontrol)
  634. {
  635. struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
  636. struct pmac_tumbler *mix;
  637. struct pmac_gpio *gp;
  638. if (! (mix = chip->mixer_data))
  639. return -ENODEV;
  640. switch(kcontrol->private_value) {
  641. case TUMBLER_MUTE_HP:
  642. gp = &mix->hp_mute; break;
  643. case TUMBLER_MUTE_AMP:
  644. gp = &mix->amp_mute; break;
  645. case TUMBLER_MUTE_LINE:
  646. gp = &mix->line_mute; break;
  647. default:
  648. gp = NULL;
  649. }
  650. if (gp == NULL)
  651. return -EINVAL;
  652. ucontrol->value.integer.value[0] = !check_audio_gpio(gp);
  653. return 0;
  654. }
  655. static int tumbler_put_mute_switch(struct snd_kcontrol *kcontrol,
  656. struct snd_ctl_elem_value *ucontrol)
  657. {
  658. struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
  659. struct pmac_tumbler *mix;
  660. struct pmac_gpio *gp;
  661. int val;
  662. #ifdef PMAC_SUPPORT_AUTOMUTE
  663. if (chip->update_automute && chip->auto_mute)
  664. return 0; /* don't touch in the auto-mute mode */
  665. #endif
  666. if (! (mix = chip->mixer_data))
  667. return -ENODEV;
  668. switch(kcontrol->private_value) {
  669. case TUMBLER_MUTE_HP:
  670. gp = &mix->hp_mute; break;
  671. case TUMBLER_MUTE_AMP:
  672. gp = &mix->amp_mute; break;
  673. case TUMBLER_MUTE_LINE:
  674. gp = &mix->line_mute; break;
  675. default:
  676. gp = NULL;
  677. }
  678. if (gp == NULL)
  679. return -EINVAL;
  680. val = ! check_audio_gpio(gp);
  681. if (val != ucontrol->value.integer.value[0]) {
  682. write_audio_gpio(gp, ! ucontrol->value.integer.value[0]);
  683. return 1;
  684. }
  685. return 0;
  686. }
  687. static int snapper_set_capture_source(struct pmac_tumbler *mix)
  688. {
  689. if (! mix->i2c.client)
  690. return -ENODEV;
  691. if (mix->capture_source)
  692. mix->acs |= 2;
  693. else
  694. mix->acs &= ~2;
  695. return i2c_smbus_write_byte_data(mix->i2c.client, TAS_REG_ACS, mix->acs);
  696. }
  697. static int snapper_info_capture_source(struct snd_kcontrol *kcontrol,
  698. struct snd_ctl_elem_info *uinfo)
  699. {
  700. static const char * const texts[2] = {
  701. "Line", "Mic"
  702. };
  703. return snd_ctl_enum_info(uinfo, 1, 2, texts);
  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[] = {
  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[] = {
  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 = {
  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 = {
  801. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  802. .name = "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 = {
  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 = {
  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, !IS_G4DA, 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 = IS_G4DA
  1004. && !strncmp(device, "keywest-gpio1", 13);
  1005. gp->active_val = 0x4;
  1006. gp->inactive_val = 0x5;
  1007. /* Here are some crude hacks to extract the GPIO polarity and
  1008. * open collector informations out of the do-platform script
  1009. * as we don't yet have an interpreter for these things
  1010. */
  1011. if (platform)
  1012. prop = of_get_property(node, platform, NULL);
  1013. if (prop) {
  1014. if (prop[3] == 0x9 && prop[4] == 0x9) {
  1015. gp->active_val = 0xd;
  1016. gp->inactive_val = 0xc;
  1017. }
  1018. if (prop[3] == 0x1 && prop[4] == 0x1) {
  1019. gp->active_val = 0x5;
  1020. gp->inactive_val = 0x4;
  1021. }
  1022. }
  1023. }
  1024. DBG("(I) GPIO device %s found, offset: %x, active state: %d !\n",
  1025. device, gp->addr, gp->active_state);
  1026. ret = irq_of_parse_and_map(node, 0);
  1027. of_node_put(node);
  1028. return ret;
  1029. }
  1030. /* reset audio */
  1031. static void tumbler_reset_audio(struct snd_pmac *chip)
  1032. {
  1033. struct pmac_tumbler *mix = chip->mixer_data;
  1034. if (mix->anded_reset) {
  1035. DBG("(I) codec anded reset !\n");
  1036. write_audio_gpio(&mix->hp_mute, 0);
  1037. write_audio_gpio(&mix->amp_mute, 0);
  1038. msleep(200);
  1039. write_audio_gpio(&mix->hp_mute, 1);
  1040. write_audio_gpio(&mix->amp_mute, 1);
  1041. msleep(100);
  1042. write_audio_gpio(&mix->hp_mute, 0);
  1043. write_audio_gpio(&mix->amp_mute, 0);
  1044. msleep(100);
  1045. } else {
  1046. DBG("(I) codec normal reset !\n");
  1047. write_audio_gpio(&mix->audio_reset, 0);
  1048. msleep(200);
  1049. write_audio_gpio(&mix->audio_reset, 1);
  1050. msleep(100);
  1051. write_audio_gpio(&mix->audio_reset, 0);
  1052. msleep(100);
  1053. }
  1054. }
  1055. #ifdef CONFIG_PM
  1056. /* suspend mixer */
  1057. static void tumbler_suspend(struct snd_pmac *chip)
  1058. {
  1059. struct pmac_tumbler *mix = chip->mixer_data;
  1060. if (mix->headphone_irq >= 0)
  1061. disable_irq(mix->headphone_irq);
  1062. if (mix->lineout_irq >= 0)
  1063. disable_irq(mix->lineout_irq);
  1064. mix->save_master_switch[0] = mix->master_switch[0];
  1065. mix->save_master_switch[1] = mix->master_switch[1];
  1066. mix->save_master_vol[0] = mix->master_vol[0];
  1067. mix->save_master_vol[1] = mix->master_vol[1];
  1068. mix->master_switch[0] = mix->master_switch[1] = 0;
  1069. tumbler_set_master_volume(mix);
  1070. if (!mix->anded_reset) {
  1071. write_audio_gpio(&mix->amp_mute, 1);
  1072. write_audio_gpio(&mix->hp_mute, 1);
  1073. }
  1074. if (chip->model == PMAC_SNAPPER) {
  1075. mix->acs |= 1;
  1076. i2c_smbus_write_byte_data(mix->i2c.client, TAS_REG_ACS, mix->acs);
  1077. }
  1078. if (mix->anded_reset) {
  1079. write_audio_gpio(&mix->amp_mute, 1);
  1080. write_audio_gpio(&mix->hp_mute, 1);
  1081. } else
  1082. write_audio_gpio(&mix->audio_reset, 1);
  1083. }
  1084. /* resume mixer */
  1085. static void tumbler_resume(struct snd_pmac *chip)
  1086. {
  1087. struct pmac_tumbler *mix = chip->mixer_data;
  1088. mix->acs &= ~1;
  1089. mix->master_switch[0] = mix->save_master_switch[0];
  1090. mix->master_switch[1] = mix->save_master_switch[1];
  1091. mix->master_vol[0] = mix->save_master_vol[0];
  1092. mix->master_vol[1] = mix->save_master_vol[1];
  1093. tumbler_reset_audio(chip);
  1094. if (mix->i2c.client && mix->i2c.init_client) {
  1095. if (mix->i2c.init_client(&mix->i2c) < 0)
  1096. printk(KERN_ERR "tumbler_init_client error\n");
  1097. } else
  1098. printk(KERN_ERR "tumbler: i2c is not initialized\n");
  1099. if (chip->model == PMAC_TUMBLER) {
  1100. tumbler_set_mono_volume(mix, &tumbler_pcm_vol_info);
  1101. tumbler_set_mono_volume(mix, &tumbler_bass_vol_info);
  1102. tumbler_set_mono_volume(mix, &tumbler_treble_vol_info);
  1103. tumbler_set_drc(mix);
  1104. } else {
  1105. snapper_set_mix_vol(mix, VOL_IDX_PCM);
  1106. snapper_set_mix_vol(mix, VOL_IDX_PCM2);
  1107. snapper_set_mix_vol(mix, VOL_IDX_ADC);
  1108. tumbler_set_mono_volume(mix, &snapper_bass_vol_info);
  1109. tumbler_set_mono_volume(mix, &snapper_treble_vol_info);
  1110. snapper_set_drc(mix);
  1111. snapper_set_capture_source(mix);
  1112. }
  1113. tumbler_set_master_volume(mix);
  1114. if (chip->update_automute)
  1115. chip->update_automute(chip, 0);
  1116. if (mix->headphone_irq >= 0) {
  1117. unsigned char val;
  1118. enable_irq(mix->headphone_irq);
  1119. /* activate headphone status interrupts */
  1120. val = do_gpio_read(&mix->hp_detect);
  1121. do_gpio_write(&mix->hp_detect, val | 0x80);
  1122. }
  1123. if (mix->lineout_irq >= 0)
  1124. enable_irq(mix->lineout_irq);
  1125. }
  1126. #endif
  1127. /* initialize tumbler */
  1128. static int tumbler_init(struct snd_pmac *chip)
  1129. {
  1130. int irq;
  1131. struct pmac_tumbler *mix = chip->mixer_data;
  1132. if (tumbler_find_device("audio-hw-reset",
  1133. "platform-do-hw-reset",
  1134. &mix->audio_reset, 0) < 0)
  1135. tumbler_find_device("hw-reset",
  1136. "platform-do-hw-reset",
  1137. &mix->audio_reset, 1);
  1138. if (tumbler_find_device("amp-mute",
  1139. "platform-do-amp-mute",
  1140. &mix->amp_mute, 0) < 0)
  1141. tumbler_find_device("amp-mute",
  1142. "platform-do-amp-mute",
  1143. &mix->amp_mute, 1);
  1144. if (tumbler_find_device("headphone-mute",
  1145. "platform-do-headphone-mute",
  1146. &mix->hp_mute, 0) < 0)
  1147. tumbler_find_device("headphone-mute",
  1148. "platform-do-headphone-mute",
  1149. &mix->hp_mute, 1);
  1150. if (tumbler_find_device("line-output-mute",
  1151. "platform-do-lineout-mute",
  1152. &mix->line_mute, 0) < 0)
  1153. tumbler_find_device("line-output-mute",
  1154. "platform-do-lineout-mute",
  1155. &mix->line_mute, 1);
  1156. irq = tumbler_find_device("headphone-detect",
  1157. NULL, &mix->hp_detect, 0);
  1158. if (irq <= NO_IRQ)
  1159. irq = tumbler_find_device("headphone-detect",
  1160. NULL, &mix->hp_detect, 1);
  1161. if (irq <= NO_IRQ)
  1162. irq = tumbler_find_device("keywest-gpio15",
  1163. NULL, &mix->hp_detect, 1);
  1164. mix->headphone_irq = irq;
  1165. irq = tumbler_find_device("line-output-detect",
  1166. NULL, &mix->line_detect, 0);
  1167. if (irq <= NO_IRQ)
  1168. irq = tumbler_find_device("line-output-detect",
  1169. NULL, &mix->line_detect, 1);
  1170. if (IS_G4DA && irq <= NO_IRQ)
  1171. irq = tumbler_find_device("keywest-gpio16",
  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 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. }