ak4xxx-adda.c 25 KB

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