tumbler.c 36 KB

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