ak4xxx-adda.c 24 KB

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