tumbler.c 39 KB

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