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