tumbler.c 36 KB

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