tumbler.c 38 KB

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