ak4xxx-adda.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762
  1. /*
  2. * ALSA driver for AK4524 / AK4528 / AK4529 / AK4355 / AK4358 / AK4381
  3. * AD and DA converters
  4. *
  5. * Copyright (c) 2000-2004 Jaroslav Kysela <perex@suse.cz>,
  6. * Takashi Iwai <tiwai@suse.de>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. *
  22. */
  23. #include <sound/driver.h>
  24. #include <asm/io.h>
  25. #include <linux/delay.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/init.h>
  28. #include <sound/core.h>
  29. #include <sound/control.h>
  30. #include <sound/ak4xxx-adda.h>
  31. MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>, Takashi Iwai <tiwai@suse.de>");
  32. MODULE_DESCRIPTION("Routines for control of AK452x / AK43xx AD/DA converters");
  33. MODULE_LICENSE("GPL");
  34. void snd_akm4xxx_write(struct snd_akm4xxx *ak, int chip, unsigned char reg,
  35. unsigned char val)
  36. {
  37. ak->ops.lock(ak, chip);
  38. ak->ops.write(ak, chip, reg, val);
  39. /* save the data */
  40. if (ak->type == SND_AK4524 || ak->type == SND_AK4528) {
  41. if ((reg != 0x04 && reg != 0x05) || (val & 0x80) == 0)
  42. snd_akm4xxx_set(ak, chip, reg, val);
  43. else
  44. snd_akm4xxx_set_ipga(ak, chip, reg, val);
  45. } else {
  46. /* AK4529, or else */
  47. snd_akm4xxx_set(ak, chip, reg, val);
  48. }
  49. ak->ops.unlock(ak, chip);
  50. }
  51. EXPORT_SYMBOL(snd_akm4xxx_write);
  52. /* reset procedure for AK4524 and AK4528 */
  53. static void ak4524_reset(struct snd_akm4xxx *ak, int state)
  54. {
  55. unsigned int chip;
  56. unsigned char reg, maxreg;
  57. if (ak->type == SND_AK4528)
  58. maxreg = 0x06;
  59. else
  60. maxreg = 0x08;
  61. for (chip = 0; chip < ak->num_dacs/2; chip++) {
  62. snd_akm4xxx_write(ak, chip, 0x01, state ? 0x00 : 0x03);
  63. if (state)
  64. continue;
  65. /* DAC volumes */
  66. for (reg = 0x04; reg < maxreg; reg++)
  67. snd_akm4xxx_write(ak, chip, reg,
  68. snd_akm4xxx_get(ak, chip, reg));
  69. if (ak->type == SND_AK4528)
  70. continue;
  71. /* IPGA */
  72. for (reg = 0x04; reg < 0x06; reg++)
  73. snd_akm4xxx_write(ak, chip, reg,
  74. snd_akm4xxx_get_ipga(ak, chip, reg));
  75. }
  76. }
  77. /* reset procedure for AK4355 and AK4358 */
  78. static void ak4355_reset(struct snd_akm4xxx *ak, int state)
  79. {
  80. unsigned char reg;
  81. if (state) {
  82. snd_akm4xxx_write(ak, 0, 0x01, 0x02); /* reset and soft-mute */
  83. return;
  84. }
  85. for (reg = 0x00; reg < 0x0b; reg++)
  86. if (reg != 0x01)
  87. snd_akm4xxx_write(ak, 0, reg,
  88. snd_akm4xxx_get(ak, 0, reg));
  89. snd_akm4xxx_write(ak, 0, 0x01, 0x01); /* un-reset, unmute */
  90. }
  91. /* reset procedure for AK4381 */
  92. static void ak4381_reset(struct snd_akm4xxx *ak, int state)
  93. {
  94. unsigned int chip;
  95. unsigned char reg;
  96. for (chip = 0; chip < ak->num_dacs/2; chip++) {
  97. snd_akm4xxx_write(ak, chip, 0x00, state ? 0x0c : 0x0f);
  98. if (state)
  99. continue;
  100. for (reg = 0x01; reg < 0x05; reg++)
  101. snd_akm4xxx_write(ak, chip, reg,
  102. snd_akm4xxx_get(ak, chip, reg));
  103. }
  104. }
  105. /*
  106. * reset the AKM codecs
  107. * @state: 1 = reset codec, 0 = restore the registers
  108. *
  109. * assert the reset operation and restores the register values to the chips.
  110. */
  111. void snd_akm4xxx_reset(struct snd_akm4xxx *ak, int state)
  112. {
  113. switch (ak->type) {
  114. case SND_AK4524:
  115. case SND_AK4528:
  116. ak4524_reset(ak, state);
  117. break;
  118. case SND_AK4529:
  119. /* FIXME: needed for ak4529? */
  120. break;
  121. case SND_AK4355:
  122. case SND_AK4358:
  123. ak4355_reset(ak, state);
  124. break;
  125. case SND_AK4381:
  126. ak4381_reset(ak, state);
  127. break;
  128. default:
  129. break;
  130. }
  131. }
  132. EXPORT_SYMBOL(snd_akm4xxx_reset);
  133. /*
  134. * initialize all the ak4xxx chips
  135. */
  136. void snd_akm4xxx_init(struct snd_akm4xxx *ak)
  137. {
  138. static unsigned char inits_ak4524[] = {
  139. 0x00, 0x07, /* 0: all power up */
  140. 0x01, 0x00, /* 1: ADC/DAC reset */
  141. 0x02, 0x60, /* 2: 24bit I2S */
  142. 0x03, 0x19, /* 3: deemphasis off */
  143. 0x01, 0x03, /* 1: ADC/DAC enable */
  144. 0x04, 0x00, /* 4: ADC left muted */
  145. 0x05, 0x00, /* 5: ADC right muted */
  146. 0x04, 0x80, /* 4: ADC IPGA gain 0dB */
  147. 0x05, 0x80, /* 5: ADC IPGA gain 0dB */
  148. 0x06, 0x00, /* 6: DAC left muted */
  149. 0x07, 0x00, /* 7: DAC right muted */
  150. 0xff, 0xff
  151. };
  152. static unsigned char inits_ak4528[] = {
  153. 0x00, 0x07, /* 0: all power up */
  154. 0x01, 0x00, /* 1: ADC/DAC reset */
  155. 0x02, 0x60, /* 2: 24bit I2S */
  156. 0x03, 0x0d, /* 3: deemphasis off, turn LR highpass filters on */
  157. 0x01, 0x03, /* 1: ADC/DAC enable */
  158. 0x04, 0x00, /* 4: ADC left muted */
  159. 0x05, 0x00, /* 5: ADC right muted */
  160. 0xff, 0xff
  161. };
  162. static unsigned char inits_ak4529[] = {
  163. 0x09, 0x01, /* 9: ATS=0, RSTN=1 */
  164. 0x0a, 0x3f, /* A: all power up, no zero/overflow detection */
  165. 0x00, 0x0c, /* 0: TDM=0, 24bit I2S, SMUTE=0 */
  166. 0x01, 0x00, /* 1: ACKS=0, ADC, loop off */
  167. 0x02, 0xff, /* 2: LOUT1 muted */
  168. 0x03, 0xff, /* 3: ROUT1 muted */
  169. 0x04, 0xff, /* 4: LOUT2 muted */
  170. 0x05, 0xff, /* 5: ROUT2 muted */
  171. 0x06, 0xff, /* 6: LOUT3 muted */
  172. 0x07, 0xff, /* 7: ROUT3 muted */
  173. 0x0b, 0xff, /* B: LOUT4 muted */
  174. 0x0c, 0xff, /* C: ROUT4 muted */
  175. 0x08, 0x55, /* 8: deemphasis all off */
  176. 0xff, 0xff
  177. };
  178. static unsigned char inits_ak4355[] = {
  179. 0x01, 0x02, /* 1: reset and soft-mute */
  180. 0x00, 0x06, /* 0: mode3(i2s), disable auto-clock detect,
  181. * disable DZF, sharp roll-off, RSTN#=0 */
  182. 0x02, 0x0e, /* 2: DA's power up, normal speed, RSTN#=0 */
  183. // 0x02, 0x2e, /* quad speed */
  184. 0x03, 0x01, /* 3: de-emphasis off */
  185. 0x04, 0x00, /* 4: LOUT1 volume muted */
  186. 0x05, 0x00, /* 5: ROUT1 volume muted */
  187. 0x06, 0x00, /* 6: LOUT2 volume muted */
  188. 0x07, 0x00, /* 7: ROUT2 volume muted */
  189. 0x08, 0x00, /* 8: LOUT3 volume muted */
  190. 0x09, 0x00, /* 9: ROUT3 volume muted */
  191. 0x0a, 0x00, /* a: DATT speed=0, ignore DZF */
  192. 0x01, 0x01, /* 1: un-reset, unmute */
  193. 0xff, 0xff
  194. };
  195. static unsigned char inits_ak4358[] = {
  196. 0x01, 0x02, /* 1: reset and soft-mute */
  197. 0x00, 0x06, /* 0: mode3(i2s), disable auto-clock detect,
  198. * disable DZF, sharp roll-off, RSTN#=0 */
  199. 0x02, 0x0e, /* 2: DA's power up, normal speed, RSTN#=0 */
  200. // 0x02, 0x2e, /* quad speed */
  201. 0x03, 0x01, /* 3: de-emphasis off */
  202. 0x04, 0x00, /* 4: LOUT1 volume muted */
  203. 0x05, 0x00, /* 5: ROUT1 volume muted */
  204. 0x06, 0x00, /* 6: LOUT2 volume muted */
  205. 0x07, 0x00, /* 7: ROUT2 volume muted */
  206. 0x08, 0x00, /* 8: LOUT3 volume muted */
  207. 0x09, 0x00, /* 9: ROUT3 volume muted */
  208. 0x0b, 0x00, /* b: LOUT4 volume muted */
  209. 0x0c, 0x00, /* c: ROUT4 volume muted */
  210. 0x0a, 0x00, /* a: DATT speed=0, ignore DZF */
  211. 0x01, 0x01, /* 1: un-reset, unmute */
  212. 0xff, 0xff
  213. };
  214. static unsigned char inits_ak4381[] = {
  215. 0x00, 0x0c, /* 0: mode3(i2s), disable auto-clock detect */
  216. 0x01, 0x02, /* 1: de-emphasis off, normal speed,
  217. * sharp roll-off, DZF off */
  218. // 0x01, 0x12, /* quad speed */
  219. 0x02, 0x00, /* 2: DZF disabled */
  220. 0x03, 0x00, /* 3: LATT 0 */
  221. 0x04, 0x00, /* 4: RATT 0 */
  222. 0x00, 0x0f, /* 0: power-up, un-reset */
  223. 0xff, 0xff
  224. };
  225. int chip, num_chips;
  226. unsigned char *ptr, reg, data, *inits;
  227. switch (ak->type) {
  228. case SND_AK4524:
  229. inits = inits_ak4524;
  230. num_chips = ak->num_dacs / 2;
  231. break;
  232. case SND_AK4528:
  233. inits = inits_ak4528;
  234. num_chips = ak->num_dacs / 2;
  235. break;
  236. case SND_AK4529:
  237. inits = inits_ak4529;
  238. num_chips = 1;
  239. break;
  240. case SND_AK4355:
  241. inits = inits_ak4355;
  242. num_chips = 1;
  243. break;
  244. case SND_AK4358:
  245. inits = inits_ak4358;
  246. num_chips = 1;
  247. break;
  248. case SND_AK4381:
  249. inits = inits_ak4381;
  250. num_chips = ak->num_dacs / 2;
  251. break;
  252. default:
  253. snd_BUG();
  254. return;
  255. }
  256. for (chip = 0; chip < num_chips; chip++) {
  257. ptr = inits;
  258. while (*ptr != 0xff) {
  259. reg = *ptr++;
  260. data = *ptr++;
  261. snd_akm4xxx_write(ak, chip, reg, data);
  262. }
  263. }
  264. }
  265. EXPORT_SYMBOL(snd_akm4xxx_init);
  266. #define AK_GET_CHIP(val) (((val) >> 8) & 0xff)
  267. #define AK_GET_ADDR(val) ((val) & 0xff)
  268. #define AK_GET_SHIFT(val) (((val) >> 16) & 0x7f)
  269. #define AK_GET_INVERT(val) (((val) >> 23) & 1)
  270. #define AK_GET_MASK(val) (((val) >> 24) & 0xff)
  271. #define AK_COMPOSE(chip,addr,shift,mask) \
  272. (((chip) << 8) | (addr) | ((shift) << 16) | ((mask) << 24))
  273. #define AK_INVERT (1<<23)
  274. static int snd_akm4xxx_volume_info(struct snd_kcontrol *kcontrol,
  275. struct snd_ctl_elem_info *uinfo)
  276. {
  277. unsigned int mask = AK_GET_MASK(kcontrol->private_value);
  278. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  279. uinfo->count = 1;
  280. uinfo->value.integer.min = 0;
  281. uinfo->value.integer.max = mask;
  282. return 0;
  283. }
  284. static int snd_akm4xxx_volume_get(struct snd_kcontrol *kcontrol,
  285. struct snd_ctl_elem_value *ucontrol)
  286. {
  287. struct snd_akm4xxx *ak = snd_kcontrol_chip(kcontrol);
  288. int chip = AK_GET_CHIP(kcontrol->private_value);
  289. int addr = AK_GET_ADDR(kcontrol->private_value);
  290. int invert = AK_GET_INVERT(kcontrol->private_value);
  291. unsigned int mask = AK_GET_MASK(kcontrol->private_value);
  292. unsigned char val = snd_akm4xxx_get(ak, chip, addr);
  293. ucontrol->value.integer.value[0] = invert ? mask - val : val;
  294. return 0;
  295. }
  296. static int snd_akm4xxx_volume_put(struct snd_kcontrol *kcontrol,
  297. struct snd_ctl_elem_value *ucontrol)
  298. {
  299. struct snd_akm4xxx *ak = snd_kcontrol_chip(kcontrol);
  300. int chip = AK_GET_CHIP(kcontrol->private_value);
  301. int addr = AK_GET_ADDR(kcontrol->private_value);
  302. int invert = AK_GET_INVERT(kcontrol->private_value);
  303. unsigned int mask = AK_GET_MASK(kcontrol->private_value);
  304. unsigned char nval = ucontrol->value.integer.value[0] % (mask+1);
  305. int change;
  306. if (invert)
  307. nval = mask - nval;
  308. change = snd_akm4xxx_get(ak, chip, addr) != nval;
  309. if (change)
  310. snd_akm4xxx_write(ak, chip, addr, nval);
  311. return change;
  312. }
  313. static int snd_akm4xxx_stereo_volume_info(struct snd_kcontrol *kcontrol,
  314. struct snd_ctl_elem_info *uinfo)
  315. {
  316. unsigned int mask = AK_GET_MASK(kcontrol->private_value);
  317. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  318. uinfo->count = 2;
  319. uinfo->value.integer.min = 0;
  320. uinfo->value.integer.max = mask;
  321. return 0;
  322. }
  323. static int snd_akm4xxx_stereo_volume_get(struct snd_kcontrol *kcontrol,
  324. struct snd_ctl_elem_value *ucontrol)
  325. {
  326. struct snd_akm4xxx *ak = snd_kcontrol_chip(kcontrol);
  327. int chip = AK_GET_CHIP(kcontrol->private_value);
  328. int addr = AK_GET_ADDR(kcontrol->private_value);
  329. int invert = AK_GET_INVERT(kcontrol->private_value);
  330. unsigned int mask = AK_GET_MASK(kcontrol->private_value);
  331. unsigned char val = snd_akm4xxx_get(ak, chip, addr);
  332. ucontrol->value.integer.value[0] = invert ? mask - val : val;
  333. val = snd_akm4xxx_get(ak, chip, addr+1);
  334. ucontrol->value.integer.value[1] = invert ? mask - val : val;
  335. return 0;
  336. }
  337. static int snd_akm4xxx_stereo_volume_put(struct snd_kcontrol *kcontrol,
  338. struct snd_ctl_elem_value *ucontrol)
  339. {
  340. struct snd_akm4xxx *ak = snd_kcontrol_chip(kcontrol);
  341. int chip = AK_GET_CHIP(kcontrol->private_value);
  342. int addr = AK_GET_ADDR(kcontrol->private_value);
  343. int invert = AK_GET_INVERT(kcontrol->private_value);
  344. unsigned int mask = AK_GET_MASK(kcontrol->private_value);
  345. unsigned char nval = ucontrol->value.integer.value[0] % (mask+1);
  346. int change0, change1;
  347. if (invert)
  348. nval = mask - nval;
  349. change0 = snd_akm4xxx_get(ak, chip, addr) != nval;
  350. if (change0)
  351. snd_akm4xxx_write(ak, chip, addr, nval);
  352. nval = ucontrol->value.integer.value[1] % (mask+1);
  353. if (invert)
  354. nval = mask - nval;
  355. change1 = snd_akm4xxx_get(ak, chip, addr+1) != nval;
  356. if (change1)
  357. snd_akm4xxx_write(ak, chip, addr+1, nval);
  358. return change0 || change1;
  359. }
  360. static int snd_akm4xxx_ipga_gain_info(struct snd_kcontrol *kcontrol,
  361. struct snd_ctl_elem_info *uinfo)
  362. {
  363. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  364. uinfo->count = 1;
  365. uinfo->value.integer.min = 0;
  366. uinfo->value.integer.max = 36;
  367. return 0;
  368. }
  369. static int snd_akm4xxx_ipga_gain_get(struct snd_kcontrol *kcontrol,
  370. struct snd_ctl_elem_value *ucontrol)
  371. {
  372. struct snd_akm4xxx *ak = snd_kcontrol_chip(kcontrol);
  373. int chip = AK_GET_CHIP(kcontrol->private_value);
  374. int addr = AK_GET_ADDR(kcontrol->private_value);
  375. ucontrol->value.integer.value[0] =
  376. snd_akm4xxx_get_ipga(ak, chip, addr) & 0x7f;
  377. return 0;
  378. }
  379. static int snd_akm4xxx_ipga_gain_put(struct snd_kcontrol *kcontrol,
  380. struct snd_ctl_elem_value *ucontrol)
  381. {
  382. struct snd_akm4xxx *ak = snd_kcontrol_chip(kcontrol);
  383. int chip = AK_GET_CHIP(kcontrol->private_value);
  384. int addr = AK_GET_ADDR(kcontrol->private_value);
  385. unsigned char nval = (ucontrol->value.integer.value[0] % 37) | 0x80;
  386. int change = snd_akm4xxx_get_ipga(ak, chip, addr) != nval;
  387. if (change)
  388. snd_akm4xxx_write(ak, chip, addr, nval);
  389. return change;
  390. }
  391. static int snd_akm4xxx_deemphasis_info(struct snd_kcontrol *kcontrol,
  392. struct snd_ctl_elem_info *uinfo)
  393. {
  394. static char *texts[4] = {
  395. "44.1kHz", "Off", "48kHz", "32kHz",
  396. };
  397. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  398. uinfo->count = 1;
  399. uinfo->value.enumerated.items = 4;
  400. if (uinfo->value.enumerated.item >= 4)
  401. uinfo->value.enumerated.item = 3;
  402. strcpy(uinfo->value.enumerated.name,
  403. texts[uinfo->value.enumerated.item]);
  404. return 0;
  405. }
  406. static int snd_akm4xxx_deemphasis_get(struct snd_kcontrol *kcontrol,
  407. struct snd_ctl_elem_value *ucontrol)
  408. {
  409. struct snd_akm4xxx *ak = snd_kcontrol_chip(kcontrol);
  410. int chip = AK_GET_CHIP(kcontrol->private_value);
  411. int addr = AK_GET_ADDR(kcontrol->private_value);
  412. int shift = AK_GET_SHIFT(kcontrol->private_value);
  413. ucontrol->value.enumerated.item[0] =
  414. (snd_akm4xxx_get(ak, chip, addr) >> shift) & 3;
  415. return 0;
  416. }
  417. static int snd_akm4xxx_deemphasis_put(struct snd_kcontrol *kcontrol,
  418. struct snd_ctl_elem_value *ucontrol)
  419. {
  420. struct snd_akm4xxx *ak = snd_kcontrol_chip(kcontrol);
  421. int chip = AK_GET_CHIP(kcontrol->private_value);
  422. int addr = AK_GET_ADDR(kcontrol->private_value);
  423. int shift = AK_GET_SHIFT(kcontrol->private_value);
  424. unsigned char nval = ucontrol->value.enumerated.item[0] & 3;
  425. int change;
  426. nval = (nval << shift) |
  427. (snd_akm4xxx_get(ak, chip, addr) & ~(3 << shift));
  428. change = snd_akm4xxx_get(ak, chip, addr) != nval;
  429. if (change)
  430. snd_akm4xxx_write(ak, chip, addr, nval);
  431. return change;
  432. }
  433. static int ak4xxx_switch_info(struct snd_kcontrol *kcontrol,
  434. struct snd_ctl_elem_info *uinfo)
  435. {
  436. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  437. uinfo->count = 1;
  438. uinfo->value.integer.min = 0;
  439. uinfo->value.integer.max = 1;
  440. return 0;
  441. }
  442. static int ak4xxx_switch_get(struct snd_kcontrol *kcontrol,
  443. struct snd_ctl_elem_value *ucontrol)
  444. {
  445. struct snd_akm4xxx *ak = snd_kcontrol_chip(kcontrol);
  446. int chip = AK_GET_CHIP(kcontrol->private_value);
  447. int addr = AK_GET_ADDR(kcontrol->private_value);
  448. int shift = AK_GET_SHIFT(kcontrol->private_value);
  449. int invert = AK_GET_INVERT(kcontrol->private_value);
  450. unsigned char val = snd_akm4xxx_get(ak, chip, addr);
  451. if (invert)
  452. val = ! val;
  453. ucontrol->value.integer.value[0] = (val & (1<<shift)) != 0;
  454. return 0;
  455. }
  456. static int ak4xxx_switch_put(struct snd_kcontrol *kcontrol,
  457. struct snd_ctl_elem_value *ucontrol)
  458. {
  459. struct snd_akm4xxx *ak = snd_kcontrol_chip(kcontrol);
  460. int chip = AK_GET_CHIP(kcontrol->private_value);
  461. int addr = AK_GET_ADDR(kcontrol->private_value);
  462. int shift = AK_GET_SHIFT(kcontrol->private_value);
  463. int invert = AK_GET_INVERT(kcontrol->private_value);
  464. long flag = ucontrol->value.integer.value[0];
  465. unsigned char val, oval;
  466. int change;
  467. if (invert)
  468. flag = ! flag;
  469. oval = snd_akm4xxx_get(ak, chip, addr);
  470. if (flag)
  471. val = oval | (1<<shift);
  472. else
  473. val = oval & ~(1<<shift);
  474. change = (oval != val);
  475. if (change)
  476. snd_akm4xxx_write(ak, chip, addr, val);
  477. return change;
  478. }
  479. /*
  480. * build AK4xxx controls
  481. */
  482. int snd_akm4xxx_build_controls(struct snd_akm4xxx *ak)
  483. {
  484. unsigned int idx, num_emphs;
  485. struct snd_kcontrol *ctl;
  486. int err;
  487. int mixer_ch = 0;
  488. int num_stereo;
  489. ctl = kmalloc(sizeof(*ctl), GFP_KERNEL);
  490. if (! ctl)
  491. return -ENOMEM;
  492. for (idx = 0; idx < ak->num_dacs; ) {
  493. memset(ctl, 0, sizeof(*ctl));
  494. if (ak->channel_names == NULL) {
  495. strcpy(ctl->id.name, "DAC Volume");
  496. num_stereo = 1;
  497. ctl->id.index = mixer_ch + ak->idx_offset * 2;
  498. } else {
  499. strcpy(ctl->id.name, ak->channel_names[mixer_ch]);
  500. num_stereo = ak->num_stereo[mixer_ch];
  501. ctl->id.index = 0;
  502. }
  503. ctl->id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
  504. ctl->count = 1;
  505. if (num_stereo == 2) {
  506. ctl->info = snd_akm4xxx_stereo_volume_info;
  507. ctl->get = snd_akm4xxx_stereo_volume_get;
  508. ctl->put = snd_akm4xxx_stereo_volume_put;
  509. } else {
  510. ctl->info = snd_akm4xxx_volume_info;
  511. ctl->get = snd_akm4xxx_volume_get;
  512. ctl->put = snd_akm4xxx_volume_put;
  513. }
  514. switch (ak->type) {
  515. case SND_AK4524:
  516. /* register 6 & 7 */
  517. ctl->private_value =
  518. AK_COMPOSE(idx/2, (idx%2) + 6, 0, 127);
  519. break;
  520. case SND_AK4528:
  521. /* register 4 & 5 */
  522. ctl->private_value =
  523. AK_COMPOSE(idx/2, (idx%2) + 4, 0, 127);
  524. break;
  525. case SND_AK4529: {
  526. /* registers 2-7 and b,c */
  527. int val = idx < 6 ? idx + 2 : (idx - 6) + 0xb;
  528. ctl->private_value =
  529. AK_COMPOSE(0, val, 0, 255) | AK_INVERT;
  530. break;
  531. }
  532. case SND_AK4355:
  533. /* register 4-9, chip #0 only */
  534. ctl->private_value = AK_COMPOSE(0, idx + 4, 0, 255);
  535. break;
  536. case SND_AK4358:
  537. if (idx >= 6)
  538. /* register 4-9, chip #0 only */
  539. ctl->private_value =
  540. AK_COMPOSE(0, idx + 5, 0, 255);
  541. else
  542. /* register 4-9, chip #0 only */
  543. ctl->private_value =
  544. AK_COMPOSE(0, idx + 4, 0, 255);
  545. break;
  546. case SND_AK4381:
  547. /* register 3 & 4 */
  548. ctl->private_value =
  549. AK_COMPOSE(idx/2, (idx%2) + 3, 0, 255);
  550. break;
  551. default:
  552. err = -EINVAL;
  553. goto __error;
  554. }
  555. ctl->private_data = ak;
  556. err = snd_ctl_add(ak->card,
  557. snd_ctl_new(ctl, SNDRV_CTL_ELEM_ACCESS_READ|
  558. SNDRV_CTL_ELEM_ACCESS_WRITE));
  559. if (err < 0)
  560. goto __error;
  561. idx += num_stereo;
  562. mixer_ch++;
  563. }
  564. for (idx = 0; idx < ak->num_adcs && ak->type == SND_AK4524; ++idx) {
  565. memset(ctl, 0, sizeof(*ctl));
  566. strcpy(ctl->id.name, "ADC Volume");
  567. ctl->id.index = idx + ak->idx_offset * 2;
  568. ctl->id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
  569. ctl->count = 1;
  570. ctl->info = snd_akm4xxx_volume_info;
  571. ctl->get = snd_akm4xxx_volume_get;
  572. ctl->put = snd_akm4xxx_volume_put;
  573. /* register 4 & 5 */
  574. ctl->private_value =
  575. AK_COMPOSE(idx/2, (idx%2) + 4, 0, 127);
  576. ctl->private_data = ak;
  577. err = snd_ctl_add(ak->card,
  578. snd_ctl_new(ctl, SNDRV_CTL_ELEM_ACCESS_READ|
  579. SNDRV_CTL_ELEM_ACCESS_WRITE));
  580. if (err < 0)
  581. goto __error;
  582. memset(ctl, 0, sizeof(*ctl));
  583. strcpy(ctl->id.name, "IPGA Analog Capture Volume");
  584. ctl->id.index = idx + ak->idx_offset * 2;
  585. ctl->id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
  586. ctl->count = 1;
  587. ctl->info = snd_akm4xxx_ipga_gain_info;
  588. ctl->get = snd_akm4xxx_ipga_gain_get;
  589. ctl->put = snd_akm4xxx_ipga_gain_put;
  590. /* register 4 & 5 */
  591. ctl->private_value = AK_COMPOSE(idx/2, (idx%2) + 4, 0, 0);
  592. ctl->private_data = ak;
  593. err = snd_ctl_add(ak->card,
  594. snd_ctl_new(ctl, SNDRV_CTL_ELEM_ACCESS_READ|
  595. SNDRV_CTL_ELEM_ACCESS_WRITE));
  596. if (err < 0)
  597. goto __error;
  598. }
  599. if (ak->type == SND_AK5365) {
  600. memset(ctl, 0, sizeof(*ctl));
  601. if (ak->channel_names == NULL)
  602. strcpy(ctl->id.name, "Capture Volume");
  603. else
  604. strcpy(ctl->id.name, ak->channel_names[0]);
  605. ctl->id.index = ak->idx_offset * 2;
  606. ctl->id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
  607. ctl->count = 1;
  608. ctl->info = snd_akm4xxx_stereo_volume_info;
  609. ctl->get = snd_akm4xxx_stereo_volume_get;
  610. ctl->put = snd_akm4xxx_stereo_volume_put;
  611. /* Registers 4 & 5 (see AK5365 data sheet, pages 34 and 35):
  612. * valid values are from 0x00 (mute) to 0x98 (+12dB). */
  613. ctl->private_value =
  614. AK_COMPOSE(0, 4, 0, 0x98);
  615. ctl->private_data = ak;
  616. err = snd_ctl_add(ak->card,
  617. snd_ctl_new(ctl, SNDRV_CTL_ELEM_ACCESS_READ|
  618. SNDRV_CTL_ELEM_ACCESS_WRITE));
  619. if (err < 0)
  620. goto __error;
  621. memset(ctl, 0, sizeof(*ctl));
  622. if (ak->channel_names == NULL)
  623. strcpy(ctl->id.name, "Capture Switch");
  624. else
  625. strcpy(ctl->id.name, ak->channel_names[1]);
  626. ctl->id.index = ak->idx_offset * 2;
  627. ctl->id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
  628. ctl->count = 1;
  629. ctl->info = ak4xxx_switch_info;
  630. ctl->get = ak4xxx_switch_get;
  631. ctl->put = ak4xxx_switch_put;
  632. /* register 2, bit 0 (SMUTE): 0 = normal operation, 1 = mute */
  633. ctl->private_value =
  634. AK_COMPOSE(0, 2, 0, 0) | AK_INVERT;
  635. ctl->private_data = ak;
  636. err = snd_ctl_add(ak->card,
  637. snd_ctl_new(ctl, SNDRV_CTL_ELEM_ACCESS_READ|
  638. SNDRV_CTL_ELEM_ACCESS_WRITE));
  639. if (err < 0)
  640. goto __error;
  641. }
  642. if (ak->type == SND_AK4355 || ak->type == SND_AK4358)
  643. num_emphs = 1;
  644. else
  645. num_emphs = ak->num_dacs / 2;
  646. for (idx = 0; idx < num_emphs; idx++) {
  647. memset(ctl, 0, sizeof(*ctl));
  648. strcpy(ctl->id.name, "Deemphasis");
  649. ctl->id.index = idx + ak->idx_offset;
  650. ctl->id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
  651. ctl->count = 1;
  652. ctl->info = snd_akm4xxx_deemphasis_info;
  653. ctl->get = snd_akm4xxx_deemphasis_get;
  654. ctl->put = snd_akm4xxx_deemphasis_put;
  655. switch (ak->type) {
  656. case SND_AK4524:
  657. case SND_AK4528:
  658. /* register 3 */
  659. ctl->private_value = AK_COMPOSE(idx, 3, 0, 0);
  660. break;
  661. case SND_AK4529: {
  662. int shift = idx == 3 ? 6 : (2 - idx) * 2;
  663. /* register 8 with shift */
  664. ctl->private_value = AK_COMPOSE(0, 8, shift, 0);
  665. break;
  666. }
  667. case SND_AK4355:
  668. case SND_AK4358:
  669. ctl->private_value = AK_COMPOSE(idx, 3, 0, 0);
  670. break;
  671. case SND_AK4381:
  672. ctl->private_value = AK_COMPOSE(idx, 1, 1, 0);
  673. break;
  674. default:
  675. err = -EINVAL;
  676. goto __error;
  677. }
  678. ctl->private_data = ak;
  679. err = snd_ctl_add(ak->card,
  680. snd_ctl_new(ctl, SNDRV_CTL_ELEM_ACCESS_READ|
  681. SNDRV_CTL_ELEM_ACCESS_WRITE));
  682. if (err < 0)
  683. goto __error;
  684. }
  685. err = 0;
  686. __error:
  687. kfree(ctl);
  688. return err;
  689. }
  690. EXPORT_SYMBOL(snd_akm4xxx_build_controls);
  691. static int __init alsa_akm4xxx_module_init(void)
  692. {
  693. return 0;
  694. }
  695. static void __exit alsa_akm4xxx_module_exit(void)
  696. {
  697. }
  698. module_init(alsa_akm4xxx_module_init)
  699. module_exit(alsa_akm4xxx_module_exit)