tumbler.c 38 KB

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