tumbler.c 38 KB

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