ak4xxx-adda.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778
  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) & 0x3f)
  269. #define AK_GET_NEEDSMSB(val) (((val) >> 22) & 1)
  270. #define AK_GET_INVERT(val) (((val) >> 23) & 1)
  271. #define AK_GET_MASK(val) (((val) >> 24) & 0xff)
  272. #define AK_COMPOSE(chip,addr,shift,mask) \
  273. (((chip) << 8) | (addr) | ((shift) << 16) | ((mask) << 24))
  274. #define AK_NEEDSMSB (1<<22)
  275. #define AK_INVERT (1<<23)
  276. static int snd_akm4xxx_volume_info(struct snd_kcontrol *kcontrol,
  277. struct snd_ctl_elem_info *uinfo)
  278. {
  279. unsigned int mask = AK_GET_MASK(kcontrol->private_value);
  280. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  281. uinfo->count = 1;
  282. uinfo->value.integer.min = 0;
  283. uinfo->value.integer.max = mask;
  284. return 0;
  285. }
  286. static int snd_akm4xxx_volume_get(struct snd_kcontrol *kcontrol,
  287. struct snd_ctl_elem_value *ucontrol)
  288. {
  289. struct snd_akm4xxx *ak = snd_kcontrol_chip(kcontrol);
  290. int chip = AK_GET_CHIP(kcontrol->private_value);
  291. int addr = AK_GET_ADDR(kcontrol->private_value);
  292. int needsmsb = AK_GET_NEEDSMSB(kcontrol->private_value);
  293. int invert = AK_GET_INVERT(kcontrol->private_value);
  294. unsigned int mask = AK_GET_MASK(kcontrol->private_value);
  295. unsigned char val = snd_akm4xxx_get(ak, chip, addr);
  296. if (needsmsb)
  297. val &= 0x7f;
  298. ucontrol->value.integer.value[0] = invert ? mask - val : val;
  299. return 0;
  300. }
  301. static int snd_akm4xxx_volume_put(struct snd_kcontrol *kcontrol,
  302. struct snd_ctl_elem_value *ucontrol)
  303. {
  304. struct snd_akm4xxx *ak = snd_kcontrol_chip(kcontrol);
  305. int chip = AK_GET_CHIP(kcontrol->private_value);
  306. int addr = AK_GET_ADDR(kcontrol->private_value);
  307. int needsmsb = AK_GET_NEEDSMSB(kcontrol->private_value);
  308. int invert = AK_GET_INVERT(kcontrol->private_value);
  309. unsigned int mask = AK_GET_MASK(kcontrol->private_value);
  310. unsigned char nval = ucontrol->value.integer.value[0] % (mask+1);
  311. int change;
  312. if (invert)
  313. nval = mask - nval;
  314. if (needsmsb)
  315. nval |= 0x80;
  316. change = snd_akm4xxx_get(ak, chip, addr) != nval;
  317. if (change)
  318. snd_akm4xxx_write(ak, chip, addr, nval);
  319. return change;
  320. }
  321. static int snd_akm4xxx_stereo_volume_info(struct snd_kcontrol *kcontrol,
  322. struct snd_ctl_elem_info *uinfo)
  323. {
  324. unsigned int mask = AK_GET_MASK(kcontrol->private_value);
  325. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  326. uinfo->count = 2;
  327. uinfo->value.integer.min = 0;
  328. uinfo->value.integer.max = mask;
  329. return 0;
  330. }
  331. static int snd_akm4xxx_stereo_volume_get(struct snd_kcontrol *kcontrol,
  332. struct snd_ctl_elem_value *ucontrol)
  333. {
  334. struct snd_akm4xxx *ak = snd_kcontrol_chip(kcontrol);
  335. int chip = AK_GET_CHIP(kcontrol->private_value);
  336. int addr = AK_GET_ADDR(kcontrol->private_value);
  337. int needsmsb = AK_GET_NEEDSMSB(kcontrol->private_value);
  338. int invert = AK_GET_INVERT(kcontrol->private_value);
  339. unsigned int mask = AK_GET_MASK(kcontrol->private_value);
  340. unsigned char val;
  341. val = snd_akm4xxx_get(ak, chip, addr);
  342. if (needsmsb)
  343. val &= 0x7f;
  344. ucontrol->value.integer.value[0] = invert ? mask - val : val;
  345. val = snd_akm4xxx_get(ak, chip, addr+1);
  346. if (needsmsb)
  347. val &= 0x7f;
  348. ucontrol->value.integer.value[1] = invert ? mask - val : val;
  349. return 0;
  350. }
  351. static int snd_akm4xxx_stereo_volume_put(struct snd_kcontrol *kcontrol,
  352. struct snd_ctl_elem_value *ucontrol)
  353. {
  354. struct snd_akm4xxx *ak = snd_kcontrol_chip(kcontrol);
  355. int chip = AK_GET_CHIP(kcontrol->private_value);
  356. int addr = AK_GET_ADDR(kcontrol->private_value);
  357. int needsmsb = AK_GET_NEEDSMSB(kcontrol->private_value);
  358. int invert = AK_GET_INVERT(kcontrol->private_value);
  359. unsigned int mask = AK_GET_MASK(kcontrol->private_value);
  360. unsigned char nval = ucontrol->value.integer.value[0] % (mask+1);
  361. int change0, change1;
  362. if (invert)
  363. nval = mask - nval;
  364. if (needsmsb)
  365. nval |= 0x80;
  366. change0 = snd_akm4xxx_get(ak, chip, addr) != nval;
  367. if (change0)
  368. snd_akm4xxx_write(ak, chip, addr, nval);
  369. nval = ucontrol->value.integer.value[1] % (mask+1);
  370. if (invert)
  371. nval = mask - nval;
  372. if (needsmsb)
  373. nval |= 0x80;
  374. change1 = snd_akm4xxx_get(ak, chip, addr+1) != nval;
  375. if (change1)
  376. snd_akm4xxx_write(ak, chip, addr+1, nval);
  377. return change0 || change1;
  378. }
  379. static int snd_akm4xxx_ipga_gain_info(struct snd_kcontrol *kcontrol,
  380. struct snd_ctl_elem_info *uinfo)
  381. {
  382. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  383. uinfo->count = 1;
  384. uinfo->value.integer.min = 0;
  385. uinfo->value.integer.max = 36;
  386. return 0;
  387. }
  388. static int snd_akm4xxx_ipga_gain_get(struct snd_kcontrol *kcontrol,
  389. struct snd_ctl_elem_value *ucontrol)
  390. {
  391. struct snd_akm4xxx *ak = snd_kcontrol_chip(kcontrol);
  392. int chip = AK_GET_CHIP(kcontrol->private_value);
  393. int addr = AK_GET_ADDR(kcontrol->private_value);
  394. ucontrol->value.integer.value[0] =
  395. snd_akm4xxx_get_ipga(ak, chip, addr) & 0x7f;
  396. return 0;
  397. }
  398. static int snd_akm4xxx_ipga_gain_put(struct snd_kcontrol *kcontrol,
  399. struct snd_ctl_elem_value *ucontrol)
  400. {
  401. struct snd_akm4xxx *ak = snd_kcontrol_chip(kcontrol);
  402. int chip = AK_GET_CHIP(kcontrol->private_value);
  403. int addr = AK_GET_ADDR(kcontrol->private_value);
  404. unsigned char nval = (ucontrol->value.integer.value[0] % 37) | 0x80;
  405. int change = snd_akm4xxx_get_ipga(ak, chip, addr) != nval;
  406. if (change)
  407. snd_akm4xxx_write(ak, chip, addr, nval);
  408. return change;
  409. }
  410. static int snd_akm4xxx_deemphasis_info(struct snd_kcontrol *kcontrol,
  411. struct snd_ctl_elem_info *uinfo)
  412. {
  413. static char *texts[4] = {
  414. "44.1kHz", "Off", "48kHz", "32kHz",
  415. };
  416. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  417. uinfo->count = 1;
  418. uinfo->value.enumerated.items = 4;
  419. if (uinfo->value.enumerated.item >= 4)
  420. uinfo->value.enumerated.item = 3;
  421. strcpy(uinfo->value.enumerated.name,
  422. texts[uinfo->value.enumerated.item]);
  423. return 0;
  424. }
  425. static int snd_akm4xxx_deemphasis_get(struct snd_kcontrol *kcontrol,
  426. struct snd_ctl_elem_value *ucontrol)
  427. {
  428. struct snd_akm4xxx *ak = snd_kcontrol_chip(kcontrol);
  429. int chip = AK_GET_CHIP(kcontrol->private_value);
  430. int addr = AK_GET_ADDR(kcontrol->private_value);
  431. int shift = AK_GET_SHIFT(kcontrol->private_value);
  432. ucontrol->value.enumerated.item[0] =
  433. (snd_akm4xxx_get(ak, chip, addr) >> shift) & 3;
  434. return 0;
  435. }
  436. static int snd_akm4xxx_deemphasis_put(struct snd_kcontrol *kcontrol,
  437. struct snd_ctl_elem_value *ucontrol)
  438. {
  439. struct snd_akm4xxx *ak = snd_kcontrol_chip(kcontrol);
  440. int chip = AK_GET_CHIP(kcontrol->private_value);
  441. int addr = AK_GET_ADDR(kcontrol->private_value);
  442. int shift = AK_GET_SHIFT(kcontrol->private_value);
  443. unsigned char nval = ucontrol->value.enumerated.item[0] & 3;
  444. int change;
  445. nval = (nval << shift) |
  446. (snd_akm4xxx_get(ak, chip, addr) & ~(3 << shift));
  447. change = snd_akm4xxx_get(ak, chip, addr) != nval;
  448. if (change)
  449. snd_akm4xxx_write(ak, chip, addr, nval);
  450. return change;
  451. }
  452. static int ak4xxx_switch_info(struct snd_kcontrol *kcontrol,
  453. struct snd_ctl_elem_info *uinfo)
  454. {
  455. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  456. uinfo->count = 1;
  457. uinfo->value.integer.min = 0;
  458. uinfo->value.integer.max = 1;
  459. return 0;
  460. }
  461. static int ak4xxx_switch_get(struct snd_kcontrol *kcontrol,
  462. struct snd_ctl_elem_value *ucontrol)
  463. {
  464. struct snd_akm4xxx *ak = snd_kcontrol_chip(kcontrol);
  465. int chip = AK_GET_CHIP(kcontrol->private_value);
  466. int addr = AK_GET_ADDR(kcontrol->private_value);
  467. int shift = AK_GET_SHIFT(kcontrol->private_value);
  468. int invert = AK_GET_INVERT(kcontrol->private_value);
  469. unsigned char val = snd_akm4xxx_get(ak, chip, addr);
  470. if (invert)
  471. val = ! val;
  472. ucontrol->value.integer.value[0] = (val & (1<<shift)) != 0;
  473. return 0;
  474. }
  475. static int ak4xxx_switch_put(struct snd_kcontrol *kcontrol,
  476. struct snd_ctl_elem_value *ucontrol)
  477. {
  478. struct snd_akm4xxx *ak = snd_kcontrol_chip(kcontrol);
  479. int chip = AK_GET_CHIP(kcontrol->private_value);
  480. int addr = AK_GET_ADDR(kcontrol->private_value);
  481. int shift = AK_GET_SHIFT(kcontrol->private_value);
  482. int invert = AK_GET_INVERT(kcontrol->private_value);
  483. long flag = ucontrol->value.integer.value[0];
  484. unsigned char val, oval;
  485. int change;
  486. if (invert)
  487. flag = ! flag;
  488. oval = snd_akm4xxx_get(ak, chip, addr);
  489. if (flag)
  490. val = oval | (1<<shift);
  491. else
  492. val = oval & ~(1<<shift);
  493. change = (oval != val);
  494. if (change)
  495. snd_akm4xxx_write(ak, chip, addr, val);
  496. return change;
  497. }
  498. /*
  499. * build AK4xxx controls
  500. */
  501. int snd_akm4xxx_build_controls(struct snd_akm4xxx *ak)
  502. {
  503. unsigned int idx, num_emphs;
  504. struct snd_kcontrol *ctl;
  505. int err;
  506. int mixer_ch = 0;
  507. int num_stereo;
  508. ctl = kmalloc(sizeof(*ctl), GFP_KERNEL);
  509. if (! ctl)
  510. return -ENOMEM;
  511. for (idx = 0; idx < ak->num_dacs; ) {
  512. memset(ctl, 0, sizeof(*ctl));
  513. if (ak->channel_names == NULL) {
  514. strcpy(ctl->id.name, "DAC Volume");
  515. num_stereo = 1;
  516. ctl->id.index = mixer_ch + ak->idx_offset * 2;
  517. } else {
  518. strcpy(ctl->id.name, ak->channel_names[mixer_ch]);
  519. num_stereo = ak->num_stereo[mixer_ch];
  520. ctl->id.index = 0;
  521. }
  522. ctl->id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
  523. ctl->count = 1;
  524. if (num_stereo == 2) {
  525. ctl->info = snd_akm4xxx_stereo_volume_info;
  526. ctl->get = snd_akm4xxx_stereo_volume_get;
  527. ctl->put = snd_akm4xxx_stereo_volume_put;
  528. } else {
  529. ctl->info = snd_akm4xxx_volume_info;
  530. ctl->get = snd_akm4xxx_volume_get;
  531. ctl->put = snd_akm4xxx_volume_put;
  532. }
  533. switch (ak->type) {
  534. case SND_AK4524:
  535. /* register 6 & 7 */
  536. ctl->private_value =
  537. AK_COMPOSE(idx/2, (idx%2) + 6, 0, 127);
  538. break;
  539. case SND_AK4528:
  540. /* register 4 & 5 */
  541. ctl->private_value =
  542. AK_COMPOSE(idx/2, (idx%2) + 4, 0, 127);
  543. break;
  544. case SND_AK4529: {
  545. /* registers 2-7 and b,c */
  546. int val = idx < 6 ? idx + 2 : (idx - 6) + 0xb;
  547. ctl->private_value =
  548. AK_COMPOSE(0, val, 0, 255) | AK_INVERT;
  549. break;
  550. }
  551. case SND_AK4355:
  552. /* register 4-9, chip #0 only */
  553. ctl->private_value = AK_COMPOSE(0, idx + 4, 0, 255);
  554. break;
  555. case SND_AK4358: {
  556. /* register 4-9 and 11-12, chip #0 only */
  557. int addr = idx < 6 ? idx + 4 : idx + 5;
  558. ctl->private_value =
  559. AK_COMPOSE(0, addr, 0, 127) | AK_NEEDSMSB;
  560. break;
  561. }
  562. case SND_AK4381:
  563. /* register 3 & 4 */
  564. ctl->private_value =
  565. AK_COMPOSE(idx/2, (idx%2) + 3, 0, 255);
  566. break;
  567. default:
  568. err = -EINVAL;
  569. goto __error;
  570. }
  571. ctl->private_data = ak;
  572. err = snd_ctl_add(ak->card,
  573. snd_ctl_new(ctl, SNDRV_CTL_ELEM_ACCESS_READ|
  574. SNDRV_CTL_ELEM_ACCESS_WRITE));
  575. if (err < 0)
  576. goto __error;
  577. idx += num_stereo;
  578. mixer_ch++;
  579. }
  580. for (idx = 0; idx < ak->num_adcs && ak->type == SND_AK4524; ++idx) {
  581. memset(ctl, 0, sizeof(*ctl));
  582. strcpy(ctl->id.name, "ADC Volume");
  583. ctl->id.index = idx + ak->idx_offset * 2;
  584. ctl->id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
  585. ctl->count = 1;
  586. ctl->info = snd_akm4xxx_volume_info;
  587. ctl->get = snd_akm4xxx_volume_get;
  588. ctl->put = snd_akm4xxx_volume_put;
  589. /* register 4 & 5 */
  590. ctl->private_value =
  591. AK_COMPOSE(idx/2, (idx%2) + 4, 0, 127);
  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. memset(ctl, 0, sizeof(*ctl));
  599. strcpy(ctl->id.name, "IPGA Analog Capture Volume");
  600. ctl->id.index = idx + ak->idx_offset * 2;
  601. ctl->id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
  602. ctl->count = 1;
  603. ctl->info = snd_akm4xxx_ipga_gain_info;
  604. ctl->get = snd_akm4xxx_ipga_gain_get;
  605. ctl->put = snd_akm4xxx_ipga_gain_put;
  606. /* register 4 & 5 */
  607. ctl->private_value = AK_COMPOSE(idx/2, (idx%2) + 4, 0, 0);
  608. ctl->private_data = ak;
  609. err = snd_ctl_add(ak->card,
  610. snd_ctl_new(ctl, SNDRV_CTL_ELEM_ACCESS_READ|
  611. SNDRV_CTL_ELEM_ACCESS_WRITE));
  612. if (err < 0)
  613. goto __error;
  614. }
  615. if (ak->type == SND_AK5365) {
  616. memset(ctl, 0, sizeof(*ctl));
  617. if (ak->channel_names == NULL)
  618. strcpy(ctl->id.name, "Capture Volume");
  619. else
  620. strcpy(ctl->id.name, ak->channel_names[0]);
  621. ctl->id.index = ak->idx_offset * 2;
  622. ctl->id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
  623. ctl->count = 1;
  624. ctl->info = snd_akm4xxx_stereo_volume_info;
  625. ctl->get = snd_akm4xxx_stereo_volume_get;
  626. ctl->put = snd_akm4xxx_stereo_volume_put;
  627. /* Registers 4 & 5 (see AK5365 data sheet, pages 34 and 35):
  628. * valid values are from 0x00 (mute) to 0x98 (+12dB). */
  629. ctl->private_value =
  630. AK_COMPOSE(0, 4, 0, 0x98);
  631. ctl->private_data = ak;
  632. err = snd_ctl_add(ak->card,
  633. snd_ctl_new(ctl, SNDRV_CTL_ELEM_ACCESS_READ|
  634. SNDRV_CTL_ELEM_ACCESS_WRITE));
  635. if (err < 0)
  636. goto __error;
  637. memset(ctl, 0, sizeof(*ctl));
  638. if (ak->channel_names == NULL)
  639. strcpy(ctl->id.name, "Capture Switch");
  640. else
  641. strcpy(ctl->id.name, ak->channel_names[1]);
  642. ctl->id.index = ak->idx_offset * 2;
  643. ctl->id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
  644. ctl->count = 1;
  645. ctl->info = ak4xxx_switch_info;
  646. ctl->get = ak4xxx_switch_get;
  647. ctl->put = ak4xxx_switch_put;
  648. /* register 2, bit 0 (SMUTE): 0 = normal operation, 1 = mute */
  649. ctl->private_value =
  650. AK_COMPOSE(0, 2, 0, 0) | AK_INVERT;
  651. ctl->private_data = ak;
  652. err = snd_ctl_add(ak->card,
  653. snd_ctl_new(ctl, SNDRV_CTL_ELEM_ACCESS_READ|
  654. SNDRV_CTL_ELEM_ACCESS_WRITE));
  655. if (err < 0)
  656. goto __error;
  657. }
  658. if (ak->type == SND_AK4355 || ak->type == SND_AK4358)
  659. num_emphs = 1;
  660. else
  661. num_emphs = ak->num_dacs / 2;
  662. for (idx = 0; idx < num_emphs; idx++) {
  663. memset(ctl, 0, sizeof(*ctl));
  664. strcpy(ctl->id.name, "Deemphasis");
  665. ctl->id.index = idx + ak->idx_offset;
  666. ctl->id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
  667. ctl->count = 1;
  668. ctl->info = snd_akm4xxx_deemphasis_info;
  669. ctl->get = snd_akm4xxx_deemphasis_get;
  670. ctl->put = snd_akm4xxx_deemphasis_put;
  671. switch (ak->type) {
  672. case SND_AK4524:
  673. case SND_AK4528:
  674. /* register 3 */
  675. ctl->private_value = AK_COMPOSE(idx, 3, 0, 0);
  676. break;
  677. case SND_AK4529: {
  678. int shift = idx == 3 ? 6 : (2 - idx) * 2;
  679. /* register 8 with shift */
  680. ctl->private_value = AK_COMPOSE(0, 8, shift, 0);
  681. break;
  682. }
  683. case SND_AK4355:
  684. case SND_AK4358:
  685. ctl->private_value = AK_COMPOSE(idx, 3, 0, 0);
  686. break;
  687. case SND_AK4381:
  688. ctl->private_value = AK_COMPOSE(idx, 1, 1, 0);
  689. break;
  690. default:
  691. err = -EINVAL;
  692. goto __error;
  693. }
  694. ctl->private_data = ak;
  695. err = snd_ctl_add(ak->card,
  696. snd_ctl_new(ctl, SNDRV_CTL_ELEM_ACCESS_READ|
  697. SNDRV_CTL_ELEM_ACCESS_WRITE));
  698. if (err < 0)
  699. goto __error;
  700. }
  701. err = 0;
  702. __error:
  703. kfree(ctl);
  704. return err;
  705. }
  706. EXPORT_SYMBOL(snd_akm4xxx_build_controls);
  707. static int __init alsa_akm4xxx_module_init(void)
  708. {
  709. return 0;
  710. }
  711. static void __exit alsa_akm4xxx_module_exit(void)
  712. {
  713. }
  714. module_init(alsa_akm4xxx_module_init)
  715. module_exit(alsa_akm4xxx_module_exit)