tumbler.c 38 KB

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