tumbler.c 38 KB

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