ak4xxx-adda.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751
  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/tlv.h>
  31. #include <sound/ak4xxx-adda.h>
  32. MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>, Takashi Iwai <tiwai@suse.de>");
  33. MODULE_DESCRIPTION("Routines for control of AK452x / AK43xx AD/DA converters");
  34. MODULE_LICENSE("GPL");
  35. /* write the given register and save the data to the cache */
  36. void snd_akm4xxx_write(struct snd_akm4xxx *ak, int chip, unsigned char reg,
  37. unsigned char val)
  38. {
  39. ak->ops.lock(ak, chip);
  40. ak->ops.write(ak, chip, reg, val);
  41. /* save the data */
  42. snd_akm4xxx_set(ak, chip, reg, val);
  43. ak->ops.unlock(ak, chip);
  44. }
  45. EXPORT_SYMBOL(snd_akm4xxx_write);
  46. /* reset procedure for AK4524 and AK4528 */
  47. static void ak4524_reset(struct snd_akm4xxx *ak, int state)
  48. {
  49. unsigned int chip;
  50. unsigned char reg, maxreg;
  51. if (ak->type == SND_AK4528)
  52. maxreg = 0x06;
  53. else
  54. maxreg = 0x08;
  55. for (chip = 0; chip < ak->num_dacs/2; chip++) {
  56. snd_akm4xxx_write(ak, chip, 0x01, state ? 0x00 : 0x03);
  57. if (state)
  58. continue;
  59. /* DAC volumes */
  60. for (reg = 0x04; reg < maxreg; reg++)
  61. snd_akm4xxx_write(ak, chip, reg,
  62. snd_akm4xxx_get(ak, chip, reg));
  63. }
  64. }
  65. /* reset procedure for AK4355 and AK4358 */
  66. static void ak4355_reset(struct snd_akm4xxx *ak, int state)
  67. {
  68. unsigned char reg;
  69. if (state) {
  70. snd_akm4xxx_write(ak, 0, 0x01, 0x02); /* reset and soft-mute */
  71. return;
  72. }
  73. for (reg = 0x00; reg < 0x0b; reg++)
  74. if (reg != 0x01)
  75. snd_akm4xxx_write(ak, 0, reg,
  76. snd_akm4xxx_get(ak, 0, reg));
  77. snd_akm4xxx_write(ak, 0, 0x01, 0x01); /* un-reset, unmute */
  78. }
  79. /* reset procedure for AK4381 */
  80. static void ak4381_reset(struct snd_akm4xxx *ak, int state)
  81. {
  82. unsigned int chip;
  83. unsigned char reg;
  84. for (chip = 0; chip < ak->num_dacs/2; chip++) {
  85. snd_akm4xxx_write(ak, chip, 0x00, state ? 0x0c : 0x0f);
  86. if (state)
  87. continue;
  88. for (reg = 0x01; reg < 0x05; reg++)
  89. snd_akm4xxx_write(ak, chip, reg,
  90. snd_akm4xxx_get(ak, chip, reg));
  91. }
  92. }
  93. /*
  94. * reset the AKM codecs
  95. * @state: 1 = reset codec, 0 = restore the registers
  96. *
  97. * assert the reset operation and restores the register values to the chips.
  98. */
  99. void snd_akm4xxx_reset(struct snd_akm4xxx *ak, int state)
  100. {
  101. switch (ak->type) {
  102. case SND_AK4524:
  103. case SND_AK4528:
  104. ak4524_reset(ak, state);
  105. break;
  106. case SND_AK4529:
  107. /* FIXME: needed for ak4529? */
  108. break;
  109. case SND_AK4355:
  110. case SND_AK4358:
  111. ak4355_reset(ak, state);
  112. break;
  113. case SND_AK4381:
  114. ak4381_reset(ak, state);
  115. break;
  116. default:
  117. break;
  118. }
  119. }
  120. EXPORT_SYMBOL(snd_akm4xxx_reset);
  121. /*
  122. * Volume conversion table for non-linear volumes
  123. * from -63.5dB (mute) to 0dB step 0.5dB
  124. *
  125. * Used for AK4524 input/ouput attenuation, AK4528, and
  126. * AK5365 input attenuation
  127. */
  128. static unsigned char vol_cvt_datt[128] = {
  129. 0x00, 0x01, 0x01, 0x02, 0x02, 0x03, 0x03, 0x04,
  130. 0x04, 0x04, 0x04, 0x05, 0x05, 0x05, 0x06, 0x06,
  131. 0x06, 0x07, 0x07, 0x08, 0x08, 0x08, 0x09, 0x0a,
  132. 0x0a, 0x0b, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x0f,
  133. 0x10, 0x10, 0x11, 0x12, 0x12, 0x13, 0x13, 0x14,
  134. 0x15, 0x16, 0x17, 0x17, 0x18, 0x19, 0x1a, 0x1c,
  135. 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x23,
  136. 0x24, 0x25, 0x26, 0x28, 0x29, 0x2a, 0x2b, 0x2d,
  137. 0x2e, 0x30, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35,
  138. 0x37, 0x38, 0x39, 0x3b, 0x3c, 0x3e, 0x3f, 0x40,
  139. 0x41, 0x42, 0x43, 0x44, 0x46, 0x47, 0x48, 0x4a,
  140. 0x4b, 0x4d, 0x4e, 0x50, 0x51, 0x52, 0x53, 0x54,
  141. 0x55, 0x56, 0x58, 0x59, 0x5b, 0x5c, 0x5e, 0x5f,
  142. 0x60, 0x61, 0x62, 0x64, 0x65, 0x66, 0x67, 0x69,
  143. 0x6a, 0x6c, 0x6d, 0x6f, 0x70, 0x71, 0x72, 0x73,
  144. 0x75, 0x76, 0x77, 0x79, 0x7a, 0x7c, 0x7d, 0x7f,
  145. };
  146. /*
  147. * dB tables
  148. */
  149. static DECLARE_TLV_DB_SCALE(db_scale_vol_datt, -6350, 50, 1);
  150. static DECLARE_TLV_DB_SCALE(db_scale_8bit, -12750, 50, 1);
  151. static DECLARE_TLV_DB_SCALE(db_scale_7bit, -6350, 50, 1);
  152. static DECLARE_TLV_DB_LINEAR(db_scale_linear, TLV_DB_GAIN_MUTE, 0);
  153. /*
  154. * initialize all the ak4xxx chips
  155. */
  156. void snd_akm4xxx_init(struct snd_akm4xxx *ak)
  157. {
  158. static unsigned char inits_ak4524[] = {
  159. 0x00, 0x07, /* 0: all power up */
  160. 0x01, 0x00, /* 1: ADC/DAC reset */
  161. 0x02, 0x60, /* 2: 24bit I2S */
  162. 0x03, 0x19, /* 3: deemphasis off */
  163. 0x01, 0x03, /* 1: ADC/DAC enable */
  164. 0x04, 0x00, /* 4: ADC left muted */
  165. 0x05, 0x00, /* 5: ADC right muted */
  166. 0x06, 0x00, /* 6: DAC left muted */
  167. 0x07, 0x00, /* 7: DAC right muted */
  168. 0xff, 0xff
  169. };
  170. static unsigned char inits_ak4528[] = {
  171. 0x00, 0x07, /* 0: all power up */
  172. 0x01, 0x00, /* 1: ADC/DAC reset */
  173. 0x02, 0x60, /* 2: 24bit I2S */
  174. 0x03, 0x0d, /* 3: deemphasis off, turn LR highpass filters on */
  175. 0x01, 0x03, /* 1: ADC/DAC enable */
  176. 0x04, 0x00, /* 4: ADC left muted */
  177. 0x05, 0x00, /* 5: ADC right muted */
  178. 0xff, 0xff
  179. };
  180. static unsigned char inits_ak4529[] = {
  181. 0x09, 0x01, /* 9: ATS=0, RSTN=1 */
  182. 0x0a, 0x3f, /* A: all power up, no zero/overflow detection */
  183. 0x00, 0x0c, /* 0: TDM=0, 24bit I2S, SMUTE=0 */
  184. 0x01, 0x00, /* 1: ACKS=0, ADC, loop off */
  185. 0x02, 0xff, /* 2: LOUT1 muted */
  186. 0x03, 0xff, /* 3: ROUT1 muted */
  187. 0x04, 0xff, /* 4: LOUT2 muted */
  188. 0x05, 0xff, /* 5: ROUT2 muted */
  189. 0x06, 0xff, /* 6: LOUT3 muted */
  190. 0x07, 0xff, /* 7: ROUT3 muted */
  191. 0x0b, 0xff, /* B: LOUT4 muted */
  192. 0x0c, 0xff, /* C: ROUT4 muted */
  193. 0x08, 0x55, /* 8: deemphasis all off */
  194. 0xff, 0xff
  195. };
  196. static unsigned char inits_ak4355[] = {
  197. 0x01, 0x02, /* 1: reset and soft-mute */
  198. 0x00, 0x06, /* 0: mode3(i2s), disable auto-clock detect,
  199. * disable DZF, sharp roll-off, RSTN#=0 */
  200. 0x02, 0x0e, /* 2: DA's power up, normal speed, RSTN#=0 */
  201. // 0x02, 0x2e, /* quad speed */
  202. 0x03, 0x01, /* 3: de-emphasis off */
  203. 0x04, 0x00, /* 4: LOUT1 volume muted */
  204. 0x05, 0x00, /* 5: ROUT1 volume muted */
  205. 0x06, 0x00, /* 6: LOUT2 volume muted */
  206. 0x07, 0x00, /* 7: ROUT2 volume muted */
  207. 0x08, 0x00, /* 8: LOUT3 volume muted */
  208. 0x09, 0x00, /* 9: ROUT3 volume muted */
  209. 0x0a, 0x00, /* a: DATT speed=0, ignore DZF */
  210. 0x01, 0x01, /* 1: un-reset, unmute */
  211. 0xff, 0xff
  212. };
  213. static unsigned char inits_ak4358[] = {
  214. 0x01, 0x02, /* 1: reset and soft-mute */
  215. 0x00, 0x06, /* 0: mode3(i2s), disable auto-clock detect,
  216. * disable DZF, sharp roll-off, RSTN#=0 */
  217. 0x02, 0x0e, /* 2: DA's power up, normal speed, RSTN#=0 */
  218. // 0x02, 0x2e, /* quad speed */
  219. 0x03, 0x01, /* 3: de-emphasis off */
  220. 0x04, 0x00, /* 4: LOUT1 volume muted */
  221. 0x05, 0x00, /* 5: ROUT1 volume muted */
  222. 0x06, 0x00, /* 6: LOUT2 volume muted */
  223. 0x07, 0x00, /* 7: ROUT2 volume muted */
  224. 0x08, 0x00, /* 8: LOUT3 volume muted */
  225. 0x09, 0x00, /* 9: ROUT3 volume muted */
  226. 0x0b, 0x00, /* b: LOUT4 volume muted */
  227. 0x0c, 0x00, /* c: ROUT4 volume muted */
  228. 0x0a, 0x00, /* a: DATT speed=0, ignore DZF */
  229. 0x01, 0x01, /* 1: un-reset, unmute */
  230. 0xff, 0xff
  231. };
  232. static unsigned char inits_ak4381[] = {
  233. 0x00, 0x0c, /* 0: mode3(i2s), disable auto-clock detect */
  234. 0x01, 0x02, /* 1: de-emphasis off, normal speed,
  235. * sharp roll-off, DZF off */
  236. // 0x01, 0x12, /* quad speed */
  237. 0x02, 0x00, /* 2: DZF disabled */
  238. 0x03, 0x00, /* 3: LATT 0 */
  239. 0x04, 0x00, /* 4: RATT 0 */
  240. 0x00, 0x0f, /* 0: power-up, un-reset */
  241. 0xff, 0xff
  242. };
  243. int chip, num_chips;
  244. unsigned char *ptr, reg, data, *inits;
  245. memset(ak->images, 0, sizeof(ak->images));
  246. memset(ak->volumes, 0, sizeof(ak->volumes));
  247. switch (ak->type) {
  248. case SND_AK4524:
  249. inits = inits_ak4524;
  250. num_chips = ak->num_dacs / 2;
  251. break;
  252. case SND_AK4528:
  253. inits = inits_ak4528;
  254. num_chips = ak->num_dacs / 2;
  255. break;
  256. case SND_AK4529:
  257. inits = inits_ak4529;
  258. num_chips = 1;
  259. break;
  260. case SND_AK4355:
  261. inits = inits_ak4355;
  262. num_chips = 1;
  263. break;
  264. case SND_AK4358:
  265. inits = inits_ak4358;
  266. num_chips = 1;
  267. break;
  268. case SND_AK4381:
  269. inits = inits_ak4381;
  270. num_chips = ak->num_dacs / 2;
  271. break;
  272. case SND_AK5365:
  273. /* FIXME: any init sequence? */
  274. return;
  275. default:
  276. snd_BUG();
  277. return;
  278. }
  279. for (chip = 0; chip < num_chips; chip++) {
  280. ptr = inits;
  281. while (*ptr != 0xff) {
  282. reg = *ptr++;
  283. data = *ptr++;
  284. snd_akm4xxx_write(ak, chip, reg, data);
  285. }
  286. }
  287. }
  288. EXPORT_SYMBOL(snd_akm4xxx_init);
  289. /*
  290. * Mixer callbacks
  291. */
  292. #define AK_IPGA (1<<20) /* including IPGA */
  293. #define AK_VOL_CVT (1<<21) /* need dB conversion */
  294. #define AK_NEEDSMSB (1<<22) /* need MSB update bit */
  295. #define AK_INVERT (1<<23) /* data is inverted */
  296. #define AK_GET_CHIP(val) (((val) >> 8) & 0xff)
  297. #define AK_GET_ADDR(val) ((val) & 0xff)
  298. #define AK_GET_SHIFT(val) (((val) >> 16) & 0x0f)
  299. #define AK_GET_VOL_CVT(val) (((val) >> 21) & 1)
  300. #define AK_GET_IPGA(val) (((val) >> 20) & 1)
  301. #define AK_GET_NEEDSMSB(val) (((val) >> 22) & 1)
  302. #define AK_GET_INVERT(val) (((val) >> 23) & 1)
  303. #define AK_GET_MASK(val) (((val) >> 24) & 0xff)
  304. #define AK_COMPOSE(chip,addr,shift,mask) \
  305. (((chip) << 8) | (addr) | ((shift) << 16) | ((mask) << 24))
  306. static int snd_akm4xxx_volume_info(struct snd_kcontrol *kcontrol,
  307. struct snd_ctl_elem_info *uinfo)
  308. {
  309. unsigned int mask = AK_GET_MASK(kcontrol->private_value);
  310. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  311. uinfo->count = 1;
  312. uinfo->value.integer.min = 0;
  313. uinfo->value.integer.max = mask;
  314. return 0;
  315. }
  316. static int snd_akm4xxx_volume_get(struct snd_kcontrol *kcontrol,
  317. struct snd_ctl_elem_value *ucontrol)
  318. {
  319. struct snd_akm4xxx *ak = snd_kcontrol_chip(kcontrol);
  320. int chip = AK_GET_CHIP(kcontrol->private_value);
  321. int addr = AK_GET_ADDR(kcontrol->private_value);
  322. ucontrol->value.integer.value[0] = snd_akm4xxx_get_vol(ak, chip, addr);
  323. return 0;
  324. }
  325. static int put_ak_reg(struct snd_kcontrol *kcontrol, int addr,
  326. unsigned char nval)
  327. {
  328. struct snd_akm4xxx *ak = snd_kcontrol_chip(kcontrol);
  329. unsigned int mask = AK_GET_MASK(kcontrol->private_value);
  330. int chip = AK_GET_CHIP(kcontrol->private_value);
  331. if (snd_akm4xxx_get_vol(ak, chip, addr) == nval)
  332. return 0;
  333. snd_akm4xxx_set_vol(ak, chip, addr, nval);
  334. if (AK_GET_VOL_CVT(kcontrol->private_value) && nval < 128)
  335. nval = vol_cvt_datt[nval];
  336. if (AK_GET_IPGA(kcontrol->private_value) && nval >= 128)
  337. nval++; /* need to correct + 1 since both 127 and 128 are 0dB */
  338. if (AK_GET_INVERT(kcontrol->private_value))
  339. nval = mask - nval;
  340. if (AK_GET_NEEDSMSB(kcontrol->private_value))
  341. nval |= 0x80;
  342. snd_akm4xxx_write(ak, chip, addr, nval);
  343. return 1;
  344. }
  345. static int snd_akm4xxx_volume_put(struct snd_kcontrol *kcontrol,
  346. struct snd_ctl_elem_value *ucontrol)
  347. {
  348. return put_ak_reg(kcontrol, AK_GET_ADDR(kcontrol->private_value),
  349. ucontrol->value.integer.value[0]);
  350. }
  351. static int snd_akm4xxx_stereo_volume_info(struct snd_kcontrol *kcontrol,
  352. struct snd_ctl_elem_info *uinfo)
  353. {
  354. unsigned int mask = AK_GET_MASK(kcontrol->private_value);
  355. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  356. uinfo->count = 2;
  357. uinfo->value.integer.min = 0;
  358. uinfo->value.integer.max = mask;
  359. return 0;
  360. }
  361. static int snd_akm4xxx_stereo_volume_get(struct snd_kcontrol *kcontrol,
  362. struct snd_ctl_elem_value *ucontrol)
  363. {
  364. struct snd_akm4xxx *ak = snd_kcontrol_chip(kcontrol);
  365. int chip = AK_GET_CHIP(kcontrol->private_value);
  366. int addr = AK_GET_ADDR(kcontrol->private_value);
  367. ucontrol->value.integer.value[0] = snd_akm4xxx_get_vol(ak, chip, addr);
  368. ucontrol->value.integer.value[1] = snd_akm4xxx_get_vol(ak, chip, addr+1);
  369. return 0;
  370. }
  371. static int snd_akm4xxx_stereo_volume_put(struct snd_kcontrol *kcontrol,
  372. struct snd_ctl_elem_value *ucontrol)
  373. {
  374. int addr = AK_GET_ADDR(kcontrol->private_value);
  375. int change;
  376. change = put_ak_reg(kcontrol, addr, ucontrol->value.integer.value[0]);
  377. change |= put_ak_reg(kcontrol, addr + 1,
  378. ucontrol->value.integer.value[1]);
  379. return change;
  380. }
  381. static int snd_akm4xxx_deemphasis_info(struct snd_kcontrol *kcontrol,
  382. struct snd_ctl_elem_info *uinfo)
  383. {
  384. static char *texts[4] = {
  385. "44.1kHz", "Off", "48kHz", "32kHz",
  386. };
  387. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  388. uinfo->count = 1;
  389. uinfo->value.enumerated.items = 4;
  390. if (uinfo->value.enumerated.item >= 4)
  391. uinfo->value.enumerated.item = 3;
  392. strcpy(uinfo->value.enumerated.name,
  393. texts[uinfo->value.enumerated.item]);
  394. return 0;
  395. }
  396. static int snd_akm4xxx_deemphasis_get(struct snd_kcontrol *kcontrol,
  397. struct snd_ctl_elem_value *ucontrol)
  398. {
  399. struct snd_akm4xxx *ak = snd_kcontrol_chip(kcontrol);
  400. int chip = AK_GET_CHIP(kcontrol->private_value);
  401. int addr = AK_GET_ADDR(kcontrol->private_value);
  402. int shift = AK_GET_SHIFT(kcontrol->private_value);
  403. ucontrol->value.enumerated.item[0] =
  404. (snd_akm4xxx_get(ak, chip, addr) >> shift) & 3;
  405. return 0;
  406. }
  407. static int snd_akm4xxx_deemphasis_put(struct snd_kcontrol *kcontrol,
  408. struct snd_ctl_elem_value *ucontrol)
  409. {
  410. struct snd_akm4xxx *ak = snd_kcontrol_chip(kcontrol);
  411. int chip = AK_GET_CHIP(kcontrol->private_value);
  412. int addr = AK_GET_ADDR(kcontrol->private_value);
  413. int shift = AK_GET_SHIFT(kcontrol->private_value);
  414. unsigned char nval = ucontrol->value.enumerated.item[0] & 3;
  415. int change;
  416. nval = (nval << shift) |
  417. (snd_akm4xxx_get(ak, chip, addr) & ~(3 << shift));
  418. change = snd_akm4xxx_get(ak, chip, addr) != nval;
  419. if (change)
  420. snd_akm4xxx_write(ak, chip, addr, nval);
  421. return change;
  422. }
  423. static int ak4xxx_switch_info(struct snd_kcontrol *kcontrol,
  424. struct snd_ctl_elem_info *uinfo)
  425. {
  426. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  427. uinfo->count = 1;
  428. uinfo->value.integer.min = 0;
  429. uinfo->value.integer.max = 1;
  430. return 0;
  431. }
  432. static int ak4xxx_switch_get(struct snd_kcontrol *kcontrol,
  433. struct snd_ctl_elem_value *ucontrol)
  434. {
  435. struct snd_akm4xxx *ak = snd_kcontrol_chip(kcontrol);
  436. int chip = AK_GET_CHIP(kcontrol->private_value);
  437. int addr = AK_GET_ADDR(kcontrol->private_value);
  438. int shift = AK_GET_SHIFT(kcontrol->private_value);
  439. int invert = AK_GET_INVERT(kcontrol->private_value);
  440. unsigned char val = snd_akm4xxx_get(ak, chip, addr);
  441. if (invert)
  442. val = ! val;
  443. ucontrol->value.integer.value[0] = (val & (1<<shift)) != 0;
  444. return 0;
  445. }
  446. static int ak4xxx_switch_put(struct snd_kcontrol *kcontrol,
  447. struct snd_ctl_elem_value *ucontrol)
  448. {
  449. struct snd_akm4xxx *ak = snd_kcontrol_chip(kcontrol);
  450. int chip = AK_GET_CHIP(kcontrol->private_value);
  451. int addr = AK_GET_ADDR(kcontrol->private_value);
  452. int shift = AK_GET_SHIFT(kcontrol->private_value);
  453. int invert = AK_GET_INVERT(kcontrol->private_value);
  454. long flag = ucontrol->value.integer.value[0];
  455. unsigned char val, oval;
  456. int change;
  457. if (invert)
  458. flag = ! flag;
  459. oval = snd_akm4xxx_get(ak, chip, addr);
  460. if (flag)
  461. val = oval | (1<<shift);
  462. else
  463. val = oval & ~(1<<shift);
  464. change = (oval != val);
  465. if (change)
  466. snd_akm4xxx_write(ak, chip, addr, val);
  467. return change;
  468. }
  469. /*
  470. * build AK4xxx controls
  471. */
  472. static int build_dac_controls(struct snd_akm4xxx *ak)
  473. {
  474. int idx, err, mixer_ch, num_stereo;
  475. struct snd_kcontrol_new knew;
  476. mixer_ch = 0;
  477. for (idx = 0; idx < ak->num_dacs; ) {
  478. memset(&knew, 0, sizeof(knew));
  479. if (! ak->dac_info || ! ak->dac_info[mixer_ch].name) {
  480. knew.name = "DAC Volume";
  481. knew.index = mixer_ch + ak->idx_offset * 2;
  482. num_stereo = 1;
  483. } else {
  484. knew.name = ak->dac_info[mixer_ch].name;
  485. num_stereo = ak->dac_info[mixer_ch].num_channels;
  486. }
  487. knew.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
  488. knew.count = 1;
  489. knew.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
  490. SNDRV_CTL_ELEM_ACCESS_TLV_READ;
  491. if (num_stereo == 2) {
  492. knew.info = snd_akm4xxx_stereo_volume_info;
  493. knew.get = snd_akm4xxx_stereo_volume_get;
  494. knew.put = snd_akm4xxx_stereo_volume_put;
  495. } else {
  496. knew.info = snd_akm4xxx_volume_info;
  497. knew.get = snd_akm4xxx_volume_get;
  498. knew.put = snd_akm4xxx_volume_put;
  499. }
  500. switch (ak->type) {
  501. case SND_AK4524:
  502. /* register 6 & 7 */
  503. knew.private_value =
  504. AK_COMPOSE(idx/2, (idx%2) + 6, 0, 127) |
  505. AK_VOL_CVT;
  506. knew.tlv.p = db_scale_vol_datt;
  507. break;
  508. case SND_AK4528:
  509. /* register 4 & 5 */
  510. knew.private_value =
  511. AK_COMPOSE(idx/2, (idx%2) + 4, 0, 127) |
  512. AK_VOL_CVT;
  513. knew.tlv.p = db_scale_vol_datt;
  514. break;
  515. case SND_AK4529: {
  516. /* registers 2-7 and b,c */
  517. int val = idx < 6 ? idx + 2 : (idx - 6) + 0xb;
  518. knew.private_value =
  519. AK_COMPOSE(0, val, 0, 255) | AK_INVERT;
  520. knew.tlv.p = db_scale_8bit;
  521. break;
  522. }
  523. case SND_AK4355:
  524. /* register 4-9, chip #0 only */
  525. knew.private_value = AK_COMPOSE(0, idx + 4, 0, 255);
  526. knew.tlv.p = db_scale_8bit;
  527. break;
  528. case SND_AK4358: {
  529. /* register 4-9 and 11-12, chip #0 only */
  530. int addr = idx < 6 ? idx + 4 : idx + 5;
  531. knew.private_value =
  532. AK_COMPOSE(0, addr, 0, 127) | AK_NEEDSMSB;
  533. knew.tlv.p = db_scale_7bit;
  534. break;
  535. }
  536. case SND_AK4381:
  537. /* register 3 & 4 */
  538. knew.private_value =
  539. AK_COMPOSE(idx/2, (idx%2) + 3, 0, 255);
  540. knew.tlv.p = db_scale_linear;
  541. break;
  542. default:
  543. return -EINVAL;
  544. }
  545. err = snd_ctl_add(ak->card, snd_ctl_new1(&knew, ak));
  546. if (err < 0)
  547. return err;
  548. idx += num_stereo;
  549. mixer_ch++;
  550. }
  551. return 0;
  552. }
  553. static int build_adc_controls(struct snd_akm4xxx *ak)
  554. {
  555. int idx, err, mixer_ch, num_stereo;
  556. struct snd_kcontrol_new knew;
  557. mixer_ch = 0;
  558. for (idx = 0; idx < ak->num_adcs;) {
  559. memset(&knew, 0, sizeof(knew));
  560. if (! ak->adc_info || ! ak->adc_info[mixer_ch].name) {
  561. knew.name = "ADC Volume";
  562. knew.index = mixer_ch + ak->idx_offset * 2;
  563. num_stereo = 1;
  564. } else {
  565. knew.name = ak->adc_info[mixer_ch].name;
  566. num_stereo = ak->adc_info[mixer_ch].num_channels;
  567. }
  568. knew.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
  569. knew.count = 1;
  570. knew.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
  571. SNDRV_CTL_ELEM_ACCESS_TLV_READ;
  572. if (num_stereo == 2) {
  573. knew.info = snd_akm4xxx_stereo_volume_info;
  574. knew.get = snd_akm4xxx_stereo_volume_get;
  575. knew.put = snd_akm4xxx_stereo_volume_put;
  576. } else {
  577. knew.info = snd_akm4xxx_volume_info;
  578. knew.get = snd_akm4xxx_volume_get;
  579. knew.put = snd_akm4xxx_volume_put;
  580. }
  581. /* register 4 & 5 */
  582. if (ak->type == SND_AK5365)
  583. knew.private_value =
  584. AK_COMPOSE(idx/2, (idx%2) + 4, 0, 151) |
  585. AK_VOL_CVT | AK_IPGA;
  586. else
  587. knew.private_value =
  588. AK_COMPOSE(idx/2, (idx%2) + 4, 0, 163) |
  589. AK_VOL_CVT | AK_IPGA;
  590. knew.tlv.p = db_scale_vol_datt;
  591. err = snd_ctl_add(ak->card, snd_ctl_new1(&knew, ak));
  592. if (err < 0)
  593. return err;
  594. if (ak->type == SND_AK5365 && (idx % 2) == 0) {
  595. if (! ak->adc_info ||
  596. ! ak->adc_info[mixer_ch].switch_name)
  597. knew.name = "Capture Switch";
  598. else
  599. knew.name = ak->adc_info[mixer_ch].switch_name;
  600. knew.info = ak4xxx_switch_info;
  601. knew.get = ak4xxx_switch_get;
  602. knew.put = ak4xxx_switch_put;
  603. knew.access = 0;
  604. /* register 2, bit 0 (SMUTE): 0 = normal operation,
  605. 1 = mute */
  606. knew.private_value =
  607. AK_COMPOSE(idx/2, 2, 0, 0) | AK_INVERT;
  608. err = snd_ctl_add(ak->card, snd_ctl_new1(&knew, ak));
  609. if (err < 0)
  610. return err;
  611. }
  612. idx += num_stereo;
  613. mixer_ch++;
  614. }
  615. return 0;
  616. }
  617. static int build_deemphasis(struct snd_akm4xxx *ak, int num_emphs)
  618. {
  619. int idx, err;
  620. struct snd_kcontrol_new knew;
  621. for (idx = 0; idx < num_emphs; idx++) {
  622. memset(&knew, 0, sizeof(knew));
  623. knew.name = "Deemphasis";
  624. knew.index = idx + ak->idx_offset;
  625. knew.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
  626. knew.count = 1;
  627. knew.info = snd_akm4xxx_deemphasis_info;
  628. knew.get = snd_akm4xxx_deemphasis_get;
  629. knew.put = snd_akm4xxx_deemphasis_put;
  630. switch (ak->type) {
  631. case SND_AK4524:
  632. case SND_AK4528:
  633. /* register 3 */
  634. knew.private_value = AK_COMPOSE(idx, 3, 0, 0);
  635. break;
  636. case SND_AK4529: {
  637. int shift = idx == 3 ? 6 : (2 - idx) * 2;
  638. /* register 8 with shift */
  639. knew.private_value = AK_COMPOSE(0, 8, shift, 0);
  640. break;
  641. }
  642. case SND_AK4355:
  643. case SND_AK4358:
  644. knew.private_value = AK_COMPOSE(idx, 3, 0, 0);
  645. break;
  646. case SND_AK4381:
  647. knew.private_value = AK_COMPOSE(idx, 1, 1, 0);
  648. break;
  649. default:
  650. return -EINVAL;
  651. }
  652. err = snd_ctl_add(ak->card, snd_ctl_new1(&knew, ak));
  653. if (err < 0)
  654. return err;
  655. }
  656. return 0;
  657. }
  658. int snd_akm4xxx_build_controls(struct snd_akm4xxx *ak)
  659. {
  660. int err, num_emphs;
  661. err = build_dac_controls(ak);
  662. if (err < 0)
  663. return err;
  664. err = build_adc_controls(ak);
  665. if (err < 0)
  666. return err;
  667. if (ak->type == SND_AK4355 || ak->type == SND_AK4358)
  668. num_emphs = 1;
  669. else
  670. num_emphs = ak->num_dacs / 2;
  671. err = build_deemphasis(ak, num_emphs);
  672. if (err < 0)
  673. return err;
  674. return 0;
  675. }
  676. EXPORT_SYMBOL(snd_akm4xxx_build_controls);
  677. static int __init alsa_akm4xxx_module_init(void)
  678. {
  679. return 0;
  680. }
  681. static void __exit alsa_akm4xxx_module_exit(void)
  682. {
  683. }
  684. module_init(alsa_akm4xxx_module_init)
  685. module_exit(alsa_akm4xxx_module_exit)