ak4xxx-adda.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946
  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 <linux/module.h>
  28. #include <sound/core.h>
  29. #include <sound/control.h>
  30. #include <sound/tlv.h>
  31. #include <sound/ak4xxx-adda.h>
  32. #include <sound/info.h>
  33. MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>, Takashi Iwai <tiwai@suse.de>");
  34. MODULE_DESCRIPTION("Routines for control of AK452x / AK43xx AD/DA converters");
  35. MODULE_LICENSE("GPL");
  36. /* write the given register and save the data to the cache */
  37. void snd_akm4xxx_write(struct snd_akm4xxx *ak, int chip, unsigned char reg,
  38. unsigned char val)
  39. {
  40. ak->ops.lock(ak, chip);
  41. ak->ops.write(ak, chip, reg, val);
  42. /* save the data */
  43. snd_akm4xxx_set(ak, chip, reg, val);
  44. ak->ops.unlock(ak, chip);
  45. }
  46. EXPORT_SYMBOL(snd_akm4xxx_write);
  47. /* reset procedure for AK4524 and AK4528 */
  48. static void ak4524_reset(struct snd_akm4xxx *ak, int state)
  49. {
  50. unsigned int chip;
  51. unsigned char reg;
  52. for (chip = 0; chip < ak->num_dacs/2; chip++) {
  53. snd_akm4xxx_write(ak, chip, 0x01, state ? 0x00 : 0x03);
  54. if (state)
  55. continue;
  56. /* DAC volumes */
  57. for (reg = 0x04; reg < ak->total_regs; reg++)
  58. snd_akm4xxx_write(ak, chip, reg,
  59. snd_akm4xxx_get(ak, chip, reg));
  60. }
  61. }
  62. /* reset procedure for AK4355 and AK4358 */
  63. static void ak435X_reset(struct snd_akm4xxx *ak, int state)
  64. {
  65. unsigned char reg;
  66. if (state) {
  67. snd_akm4xxx_write(ak, 0, 0x01, 0x02); /* reset and soft-mute */
  68. return;
  69. }
  70. for (reg = 0x00; reg < ak->total_regs; reg++)
  71. if (reg != 0x01)
  72. snd_akm4xxx_write(ak, 0, reg,
  73. snd_akm4xxx_get(ak, 0, reg));
  74. snd_akm4xxx_write(ak, 0, 0x01, 0x01); /* un-reset, unmute */
  75. }
  76. /* reset procedure for AK4381 */
  77. static void ak4381_reset(struct snd_akm4xxx *ak, int state)
  78. {
  79. unsigned int chip;
  80. unsigned char reg;
  81. for (chip = 0; chip < ak->num_dacs/2; chip++) {
  82. snd_akm4xxx_write(ak, chip, 0x00, state ? 0x0c : 0x0f);
  83. if (state)
  84. continue;
  85. for (reg = 0x01; reg < ak->total_regs; reg++)
  86. snd_akm4xxx_write(ak, chip, reg,
  87. snd_akm4xxx_get(ak, chip, reg));
  88. }
  89. }
  90. /*
  91. * reset the AKM codecs
  92. * @state: 1 = reset codec, 0 = restore the registers
  93. *
  94. * assert the reset operation and restores the register values to the chips.
  95. */
  96. void snd_akm4xxx_reset(struct snd_akm4xxx *ak, int state)
  97. {
  98. switch (ak->type) {
  99. case SND_AK4524:
  100. case SND_AK4528:
  101. case SND_AK4620:
  102. ak4524_reset(ak, state);
  103. break;
  104. case SND_AK4529:
  105. /* FIXME: needed for ak4529? */
  106. break;
  107. case SND_AK4355:
  108. ak435X_reset(ak, state);
  109. break;
  110. case SND_AK4358:
  111. ak435X_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/AK4620 input/ouput attenuation, AK4528, and
  126. * AK5365 input attenuation
  127. */
  128. static const 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 const DECLARE_TLV_DB_SCALE(db_scale_vol_datt, -6350, 50, 1);
  150. static const DECLARE_TLV_DB_SCALE(db_scale_8bit, -12750, 50, 1);
  151. static const DECLARE_TLV_DB_SCALE(db_scale_7bit, -6350, 50, 1);
  152. static const 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 const 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 const 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 const 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 const 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 const 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, 0x4e, /* 2: DA's power up, normal speed, RSTN#=0 */
  218. /* 0x02, 0x6e,*/ /* 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 const 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. static const unsigned char inits_ak4620[] = {
  244. 0x00, 0x07, /* 0: normal */
  245. 0x01, 0x00, /* 0: reset */
  246. 0x01, 0x02, /* 1: RSTAD */
  247. 0x01, 0x03, /* 1: RSTDA */
  248. 0x01, 0x0f, /* 1: normal */
  249. 0x02, 0x60, /* 2: 24bit I2S */
  250. 0x03, 0x01, /* 3: deemphasis off */
  251. 0x04, 0x00, /* 4: LIN muted */
  252. 0x05, 0x00, /* 5: RIN muted */
  253. 0x06, 0x00, /* 6: LOUT muted */
  254. 0x07, 0x00, /* 7: ROUT muted */
  255. 0xff, 0xff
  256. };
  257. int chip;
  258. const unsigned char *ptr, *inits;
  259. unsigned char reg, data;
  260. memset(ak->images, 0, sizeof(ak->images));
  261. memset(ak->volumes, 0, sizeof(ak->volumes));
  262. switch (ak->type) {
  263. case SND_AK4524:
  264. inits = inits_ak4524;
  265. ak->num_chips = ak->num_dacs / 2;
  266. ak->name = "ak4524";
  267. ak->total_regs = 0x08;
  268. break;
  269. case SND_AK4528:
  270. inits = inits_ak4528;
  271. ak->num_chips = ak->num_dacs / 2;
  272. ak->name = "ak4528";
  273. ak->total_regs = 0x06;
  274. break;
  275. case SND_AK4529:
  276. inits = inits_ak4529;
  277. ak->num_chips = 1;
  278. ak->name = "ak4529";
  279. ak->total_regs = 0x0d;
  280. break;
  281. case SND_AK4355:
  282. inits = inits_ak4355;
  283. ak->num_chips = 1;
  284. ak->name = "ak4355";
  285. ak->total_regs = 0x0b;
  286. break;
  287. case SND_AK4358:
  288. inits = inits_ak4358;
  289. ak->num_chips = 1;
  290. ak->name = "ak4358";
  291. ak->total_regs = 0x10;
  292. break;
  293. case SND_AK4381:
  294. inits = inits_ak4381;
  295. ak->num_chips = ak->num_dacs / 2;
  296. ak->name = "ak4381";
  297. ak->total_regs = 0x05;
  298. break;
  299. case SND_AK5365:
  300. /* FIXME: any init sequence? */
  301. ak->num_chips = 1;
  302. ak->name = "ak5365";
  303. ak->total_regs = 0x08;
  304. return;
  305. case SND_AK4620:
  306. inits = inits_ak4620;
  307. ak->num_chips = ak->num_dacs / 2;
  308. ak->name = "ak4620";
  309. ak->total_regs = 0x08;
  310. break;
  311. default:
  312. snd_BUG();
  313. return;
  314. }
  315. for (chip = 0; chip < ak->num_chips; chip++) {
  316. ptr = inits;
  317. while (*ptr != 0xff) {
  318. reg = *ptr++;
  319. data = *ptr++;
  320. snd_akm4xxx_write(ak, chip, reg, data);
  321. udelay(10);
  322. }
  323. }
  324. }
  325. EXPORT_SYMBOL(snd_akm4xxx_init);
  326. /*
  327. * Mixer callbacks
  328. */
  329. #define AK_IPGA (1<<20) /* including IPGA */
  330. #define AK_VOL_CVT (1<<21) /* need dB conversion */
  331. #define AK_NEEDSMSB (1<<22) /* need MSB update bit */
  332. #define AK_INVERT (1<<23) /* data is inverted */
  333. #define AK_GET_CHIP(val) (((val) >> 8) & 0xff)
  334. #define AK_GET_ADDR(val) ((val) & 0xff)
  335. #define AK_GET_SHIFT(val) (((val) >> 16) & 0x0f)
  336. #define AK_GET_VOL_CVT(val) (((val) >> 21) & 1)
  337. #define AK_GET_IPGA(val) (((val) >> 20) & 1)
  338. #define AK_GET_NEEDSMSB(val) (((val) >> 22) & 1)
  339. #define AK_GET_INVERT(val) (((val) >> 23) & 1)
  340. #define AK_GET_MASK(val) (((val) >> 24) & 0xff)
  341. #define AK_COMPOSE(chip,addr,shift,mask) \
  342. (((chip) << 8) | (addr) | ((shift) << 16) | ((mask) << 24))
  343. static int snd_akm4xxx_volume_info(struct snd_kcontrol *kcontrol,
  344. struct snd_ctl_elem_info *uinfo)
  345. {
  346. unsigned int mask = AK_GET_MASK(kcontrol->private_value);
  347. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  348. uinfo->count = 1;
  349. uinfo->value.integer.min = 0;
  350. uinfo->value.integer.max = mask;
  351. return 0;
  352. }
  353. static int snd_akm4xxx_volume_get(struct snd_kcontrol *kcontrol,
  354. struct snd_ctl_elem_value *ucontrol)
  355. {
  356. struct snd_akm4xxx *ak = snd_kcontrol_chip(kcontrol);
  357. int chip = AK_GET_CHIP(kcontrol->private_value);
  358. int addr = AK_GET_ADDR(kcontrol->private_value);
  359. ucontrol->value.integer.value[0] = snd_akm4xxx_get_vol(ak, chip, addr);
  360. return 0;
  361. }
  362. static int put_ak_reg(struct snd_kcontrol *kcontrol, int addr,
  363. unsigned char nval)
  364. {
  365. struct snd_akm4xxx *ak = snd_kcontrol_chip(kcontrol);
  366. unsigned int mask = AK_GET_MASK(kcontrol->private_value);
  367. int chip = AK_GET_CHIP(kcontrol->private_value);
  368. if (snd_akm4xxx_get_vol(ak, chip, addr) == nval)
  369. return 0;
  370. snd_akm4xxx_set_vol(ak, chip, addr, nval);
  371. if (AK_GET_VOL_CVT(kcontrol->private_value) && nval < 128)
  372. nval = vol_cvt_datt[nval];
  373. if (AK_GET_IPGA(kcontrol->private_value) && nval >= 128)
  374. nval++; /* need to correct + 1 since both 127 and 128 are 0dB */
  375. if (AK_GET_INVERT(kcontrol->private_value))
  376. nval = mask - nval;
  377. if (AK_GET_NEEDSMSB(kcontrol->private_value))
  378. nval |= 0x80;
  379. /* printk(KERN_DEBUG "DEBUG - AK writing reg: chip %x addr %x,
  380. nval %x\n", chip, addr, nval); */
  381. snd_akm4xxx_write(ak, chip, addr, nval);
  382. return 1;
  383. }
  384. static int snd_akm4xxx_volume_put(struct snd_kcontrol *kcontrol,
  385. struct snd_ctl_elem_value *ucontrol)
  386. {
  387. unsigned int mask = AK_GET_MASK(kcontrol->private_value);
  388. unsigned int val = ucontrol->value.integer.value[0];
  389. if (val > mask)
  390. return -EINVAL;
  391. return put_ak_reg(kcontrol, AK_GET_ADDR(kcontrol->private_value), val);
  392. }
  393. static int snd_akm4xxx_stereo_volume_info(struct snd_kcontrol *kcontrol,
  394. struct snd_ctl_elem_info *uinfo)
  395. {
  396. unsigned int mask = AK_GET_MASK(kcontrol->private_value);
  397. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  398. uinfo->count = 2;
  399. uinfo->value.integer.min = 0;
  400. uinfo->value.integer.max = mask;
  401. return 0;
  402. }
  403. static int snd_akm4xxx_stereo_volume_get(struct snd_kcontrol *kcontrol,
  404. struct snd_ctl_elem_value *ucontrol)
  405. {
  406. struct snd_akm4xxx *ak = snd_kcontrol_chip(kcontrol);
  407. int chip = AK_GET_CHIP(kcontrol->private_value);
  408. int addr = AK_GET_ADDR(kcontrol->private_value);
  409. ucontrol->value.integer.value[0] = snd_akm4xxx_get_vol(ak, chip, addr);
  410. ucontrol->value.integer.value[1] = snd_akm4xxx_get_vol(ak, chip, addr+1);
  411. return 0;
  412. }
  413. static int snd_akm4xxx_stereo_volume_put(struct snd_kcontrol *kcontrol,
  414. struct snd_ctl_elem_value *ucontrol)
  415. {
  416. int addr = AK_GET_ADDR(kcontrol->private_value);
  417. unsigned int mask = AK_GET_MASK(kcontrol->private_value);
  418. unsigned int val[2];
  419. int change;
  420. val[0] = ucontrol->value.integer.value[0];
  421. val[1] = ucontrol->value.integer.value[1];
  422. if (val[0] > mask || val[1] > mask)
  423. return -EINVAL;
  424. change = put_ak_reg(kcontrol, addr, val[0]);
  425. change |= put_ak_reg(kcontrol, addr + 1, val[1]);
  426. return change;
  427. }
  428. static int snd_akm4xxx_deemphasis_info(struct snd_kcontrol *kcontrol,
  429. struct snd_ctl_elem_info *uinfo)
  430. {
  431. static char *texts[4] = {
  432. "44.1kHz", "Off", "48kHz", "32kHz",
  433. };
  434. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  435. uinfo->count = 1;
  436. uinfo->value.enumerated.items = 4;
  437. if (uinfo->value.enumerated.item >= 4)
  438. uinfo->value.enumerated.item = 3;
  439. strcpy(uinfo->value.enumerated.name,
  440. texts[uinfo->value.enumerated.item]);
  441. return 0;
  442. }
  443. static int snd_akm4xxx_deemphasis_get(struct snd_kcontrol *kcontrol,
  444. struct snd_ctl_elem_value *ucontrol)
  445. {
  446. struct snd_akm4xxx *ak = snd_kcontrol_chip(kcontrol);
  447. int chip = AK_GET_CHIP(kcontrol->private_value);
  448. int addr = AK_GET_ADDR(kcontrol->private_value);
  449. int shift = AK_GET_SHIFT(kcontrol->private_value);
  450. ucontrol->value.enumerated.item[0] =
  451. (snd_akm4xxx_get(ak, chip, addr) >> shift) & 3;
  452. return 0;
  453. }
  454. static int snd_akm4xxx_deemphasis_put(struct snd_kcontrol *kcontrol,
  455. struct snd_ctl_elem_value *ucontrol)
  456. {
  457. struct snd_akm4xxx *ak = snd_kcontrol_chip(kcontrol);
  458. int chip = AK_GET_CHIP(kcontrol->private_value);
  459. int addr = AK_GET_ADDR(kcontrol->private_value);
  460. int shift = AK_GET_SHIFT(kcontrol->private_value);
  461. unsigned char nval = ucontrol->value.enumerated.item[0] & 3;
  462. int change;
  463. nval = (nval << shift) |
  464. (snd_akm4xxx_get(ak, chip, addr) & ~(3 << shift));
  465. change = snd_akm4xxx_get(ak, chip, addr) != nval;
  466. if (change)
  467. snd_akm4xxx_write(ak, chip, addr, nval);
  468. return change;
  469. }
  470. #define ak4xxx_switch_info snd_ctl_boolean_mono_info
  471. static int ak4xxx_switch_get(struct snd_kcontrol *kcontrol,
  472. struct snd_ctl_elem_value *ucontrol)
  473. {
  474. struct snd_akm4xxx *ak = snd_kcontrol_chip(kcontrol);
  475. int chip = AK_GET_CHIP(kcontrol->private_value);
  476. int addr = AK_GET_ADDR(kcontrol->private_value);
  477. int shift = AK_GET_SHIFT(kcontrol->private_value);
  478. int invert = AK_GET_INVERT(kcontrol->private_value);
  479. /* we observe the (1<<shift) bit only */
  480. unsigned char val = snd_akm4xxx_get(ak, chip, addr) & (1<<shift);
  481. if (invert)
  482. val = ! val;
  483. ucontrol->value.integer.value[0] = (val & (1<<shift)) != 0;
  484. return 0;
  485. }
  486. static int ak4xxx_switch_put(struct snd_kcontrol *kcontrol,
  487. struct snd_ctl_elem_value *ucontrol)
  488. {
  489. struct snd_akm4xxx *ak = snd_kcontrol_chip(kcontrol);
  490. int chip = AK_GET_CHIP(kcontrol->private_value);
  491. int addr = AK_GET_ADDR(kcontrol->private_value);
  492. int shift = AK_GET_SHIFT(kcontrol->private_value);
  493. int invert = AK_GET_INVERT(kcontrol->private_value);
  494. long flag = ucontrol->value.integer.value[0];
  495. unsigned char val, oval;
  496. int change;
  497. if (invert)
  498. flag = ! flag;
  499. oval = snd_akm4xxx_get(ak, chip, addr);
  500. if (flag)
  501. val = oval | (1<<shift);
  502. else
  503. val = oval & ~(1<<shift);
  504. change = (oval != val);
  505. if (change)
  506. snd_akm4xxx_write(ak, chip, addr, val);
  507. return change;
  508. }
  509. #define AK5365_NUM_INPUTS 5
  510. static int ak4xxx_capture_num_inputs(struct snd_akm4xxx *ak, int mixer_ch)
  511. {
  512. int num_names;
  513. const char **input_names;
  514. input_names = ak->adc_info[mixer_ch].input_names;
  515. num_names = 0;
  516. while (num_names < AK5365_NUM_INPUTS && input_names[num_names])
  517. ++num_names;
  518. return num_names;
  519. }
  520. static int ak4xxx_capture_source_info(struct snd_kcontrol *kcontrol,
  521. struct snd_ctl_elem_info *uinfo)
  522. {
  523. struct snd_akm4xxx *ak = snd_kcontrol_chip(kcontrol);
  524. int mixer_ch = AK_GET_SHIFT(kcontrol->private_value);
  525. const char **input_names;
  526. unsigned int num_names, idx;
  527. num_names = ak4xxx_capture_num_inputs(ak, mixer_ch);
  528. if (!num_names)
  529. return -EINVAL;
  530. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  531. uinfo->count = 1;
  532. uinfo->value.enumerated.items = num_names;
  533. idx = uinfo->value.enumerated.item;
  534. if (idx >= num_names)
  535. return -EINVAL;
  536. input_names = ak->adc_info[mixer_ch].input_names;
  537. strncpy(uinfo->value.enumerated.name, input_names[idx],
  538. sizeof(uinfo->value.enumerated.name));
  539. return 0;
  540. }
  541. static int ak4xxx_capture_source_get(struct snd_kcontrol *kcontrol,
  542. struct snd_ctl_elem_value *ucontrol)
  543. {
  544. struct snd_akm4xxx *ak = snd_kcontrol_chip(kcontrol);
  545. int chip = AK_GET_CHIP(kcontrol->private_value);
  546. int addr = AK_GET_ADDR(kcontrol->private_value);
  547. int mask = AK_GET_MASK(kcontrol->private_value);
  548. unsigned char val;
  549. val = snd_akm4xxx_get(ak, chip, addr) & mask;
  550. ucontrol->value.enumerated.item[0] = val;
  551. return 0;
  552. }
  553. static int ak4xxx_capture_source_put(struct snd_kcontrol *kcontrol,
  554. struct snd_ctl_elem_value *ucontrol)
  555. {
  556. struct snd_akm4xxx *ak = snd_kcontrol_chip(kcontrol);
  557. int mixer_ch = AK_GET_SHIFT(kcontrol->private_value);
  558. int chip = AK_GET_CHIP(kcontrol->private_value);
  559. int addr = AK_GET_ADDR(kcontrol->private_value);
  560. int mask = AK_GET_MASK(kcontrol->private_value);
  561. unsigned char oval, val;
  562. int num_names = ak4xxx_capture_num_inputs(ak, mixer_ch);
  563. if (ucontrol->value.enumerated.item[0] >= num_names)
  564. return -EINVAL;
  565. oval = snd_akm4xxx_get(ak, chip, addr);
  566. val = oval & ~mask;
  567. val |= ucontrol->value.enumerated.item[0] & mask;
  568. if (val != oval) {
  569. snd_akm4xxx_write(ak, chip, addr, val);
  570. return 1;
  571. }
  572. return 0;
  573. }
  574. /*
  575. * build AK4xxx controls
  576. */
  577. static int build_dac_controls(struct snd_akm4xxx *ak)
  578. {
  579. int idx, err, mixer_ch, num_stereo;
  580. struct snd_kcontrol_new knew;
  581. mixer_ch = 0;
  582. for (idx = 0; idx < ak->num_dacs; ) {
  583. /* mute control for Revolution 7.1 - AK4381 */
  584. if (ak->type == SND_AK4381
  585. && ak->dac_info[mixer_ch].switch_name) {
  586. memset(&knew, 0, sizeof(knew));
  587. knew.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
  588. knew.count = 1;
  589. knew.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
  590. knew.name = ak->dac_info[mixer_ch].switch_name;
  591. knew.info = ak4xxx_switch_info;
  592. knew.get = ak4xxx_switch_get;
  593. knew.put = ak4xxx_switch_put;
  594. knew.access = 0;
  595. /* register 1, bit 0 (SMUTE): 0 = normal operation,
  596. 1 = mute */
  597. knew.private_value =
  598. AK_COMPOSE(idx/2, 1, 0, 0) | AK_INVERT;
  599. err = snd_ctl_add(ak->card, snd_ctl_new1(&knew, ak));
  600. if (err < 0)
  601. return err;
  602. }
  603. memset(&knew, 0, sizeof(knew));
  604. if (! ak->dac_info || ! ak->dac_info[mixer_ch].name) {
  605. knew.name = "DAC Volume";
  606. knew.index = mixer_ch + ak->idx_offset * 2;
  607. num_stereo = 1;
  608. } else {
  609. knew.name = ak->dac_info[mixer_ch].name;
  610. num_stereo = ak->dac_info[mixer_ch].num_channels;
  611. }
  612. knew.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
  613. knew.count = 1;
  614. knew.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
  615. SNDRV_CTL_ELEM_ACCESS_TLV_READ;
  616. if (num_stereo == 2) {
  617. knew.info = snd_akm4xxx_stereo_volume_info;
  618. knew.get = snd_akm4xxx_stereo_volume_get;
  619. knew.put = snd_akm4xxx_stereo_volume_put;
  620. } else {
  621. knew.info = snd_akm4xxx_volume_info;
  622. knew.get = snd_akm4xxx_volume_get;
  623. knew.put = snd_akm4xxx_volume_put;
  624. }
  625. switch (ak->type) {
  626. case SND_AK4524:
  627. /* register 6 & 7 */
  628. knew.private_value =
  629. AK_COMPOSE(idx/2, (idx%2) + 6, 0, 127) |
  630. AK_VOL_CVT;
  631. knew.tlv.p = db_scale_vol_datt;
  632. break;
  633. case SND_AK4528:
  634. /* register 4 & 5 */
  635. knew.private_value =
  636. AK_COMPOSE(idx/2, (idx%2) + 4, 0, 127) |
  637. AK_VOL_CVT;
  638. knew.tlv.p = db_scale_vol_datt;
  639. break;
  640. case SND_AK4529: {
  641. /* registers 2-7 and b,c */
  642. int val = idx < 6 ? idx + 2 : (idx - 6) + 0xb;
  643. knew.private_value =
  644. AK_COMPOSE(0, val, 0, 255) | AK_INVERT;
  645. knew.tlv.p = db_scale_8bit;
  646. break;
  647. }
  648. case SND_AK4355:
  649. /* register 4-9, chip #0 only */
  650. knew.private_value = AK_COMPOSE(0, idx + 4, 0, 255);
  651. knew.tlv.p = db_scale_8bit;
  652. break;
  653. case SND_AK4358: {
  654. /* register 4-9 and 11-12, chip #0 only */
  655. int addr = idx < 6 ? idx + 4 : idx + 5;
  656. knew.private_value =
  657. AK_COMPOSE(0, addr, 0, 127) | AK_NEEDSMSB;
  658. knew.tlv.p = db_scale_7bit;
  659. break;
  660. }
  661. case SND_AK4381:
  662. /* register 3 & 4 */
  663. knew.private_value =
  664. AK_COMPOSE(idx/2, (idx%2) + 3, 0, 255);
  665. knew.tlv.p = db_scale_linear;
  666. break;
  667. case SND_AK4620:
  668. /* register 6 & 7 */
  669. knew.private_value =
  670. AK_COMPOSE(idx/2, (idx%2) + 6, 0, 255);
  671. knew.tlv.p = db_scale_linear;
  672. break;
  673. default:
  674. return -EINVAL;
  675. }
  676. err = snd_ctl_add(ak->card, snd_ctl_new1(&knew, ak));
  677. if (err < 0)
  678. return err;
  679. idx += num_stereo;
  680. mixer_ch++;
  681. }
  682. return 0;
  683. }
  684. static int build_adc_controls(struct snd_akm4xxx *ak)
  685. {
  686. int idx, err, mixer_ch, num_stereo, max_steps;
  687. struct snd_kcontrol_new knew;
  688. mixer_ch = 0;
  689. if (ak->type == SND_AK4528)
  690. return 0; /* no controls */
  691. for (idx = 0; idx < ak->num_adcs;) {
  692. memset(&knew, 0, sizeof(knew));
  693. if (! ak->adc_info || ! ak->adc_info[mixer_ch].name) {
  694. knew.name = "ADC Volume";
  695. knew.index = mixer_ch + ak->idx_offset * 2;
  696. num_stereo = 1;
  697. } else {
  698. knew.name = ak->adc_info[mixer_ch].name;
  699. num_stereo = ak->adc_info[mixer_ch].num_channels;
  700. }
  701. knew.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
  702. knew.count = 1;
  703. knew.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
  704. SNDRV_CTL_ELEM_ACCESS_TLV_READ;
  705. if (num_stereo == 2) {
  706. knew.info = snd_akm4xxx_stereo_volume_info;
  707. knew.get = snd_akm4xxx_stereo_volume_get;
  708. knew.put = snd_akm4xxx_stereo_volume_put;
  709. } else {
  710. knew.info = snd_akm4xxx_volume_info;
  711. knew.get = snd_akm4xxx_volume_get;
  712. knew.put = snd_akm4xxx_volume_put;
  713. }
  714. /* register 4 & 5 */
  715. if (ak->type == SND_AK5365)
  716. max_steps = 152;
  717. else
  718. max_steps = 164;
  719. knew.private_value =
  720. AK_COMPOSE(idx/2, (idx%2) + 4, 0, max_steps) |
  721. AK_VOL_CVT | AK_IPGA;
  722. knew.tlv.p = db_scale_vol_datt;
  723. err = snd_ctl_add(ak->card, snd_ctl_new1(&knew, ak));
  724. if (err < 0)
  725. return err;
  726. if (ak->type == SND_AK5365 && (idx % 2) == 0) {
  727. if (! ak->adc_info ||
  728. ! ak->adc_info[mixer_ch].switch_name) {
  729. knew.name = "Capture Switch";
  730. knew.index = mixer_ch + ak->idx_offset * 2;
  731. } else
  732. knew.name = ak->adc_info[mixer_ch].switch_name;
  733. knew.info = ak4xxx_switch_info;
  734. knew.get = ak4xxx_switch_get;
  735. knew.put = ak4xxx_switch_put;
  736. knew.access = 0;
  737. /* register 2, bit 0 (SMUTE): 0 = normal operation,
  738. 1 = mute */
  739. knew.private_value =
  740. AK_COMPOSE(idx/2, 2, 0, 0) | AK_INVERT;
  741. err = snd_ctl_add(ak->card, snd_ctl_new1(&knew, ak));
  742. if (err < 0)
  743. return err;
  744. memset(&knew, 0, sizeof(knew));
  745. knew.name = ak->adc_info[mixer_ch].selector_name;
  746. if (!knew.name) {
  747. knew.name = "Capture Channel";
  748. knew.index = mixer_ch + ak->idx_offset * 2;
  749. }
  750. knew.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
  751. knew.info = ak4xxx_capture_source_info;
  752. knew.get = ak4xxx_capture_source_get;
  753. knew.put = ak4xxx_capture_source_put;
  754. knew.access = 0;
  755. /* input selector control: reg. 1, bits 0-2.
  756. * mis-use 'shift' to pass mixer_ch */
  757. knew.private_value
  758. = AK_COMPOSE(idx/2, 1, mixer_ch, 0x07);
  759. err = snd_ctl_add(ak->card, snd_ctl_new1(&knew, ak));
  760. if (err < 0)
  761. return err;
  762. }
  763. idx += num_stereo;
  764. mixer_ch++;
  765. }
  766. return 0;
  767. }
  768. static int build_deemphasis(struct snd_akm4xxx *ak, int num_emphs)
  769. {
  770. int idx, err;
  771. struct snd_kcontrol_new knew;
  772. for (idx = 0; idx < num_emphs; idx++) {
  773. memset(&knew, 0, sizeof(knew));
  774. knew.name = "Deemphasis";
  775. knew.index = idx + ak->idx_offset;
  776. knew.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
  777. knew.count = 1;
  778. knew.info = snd_akm4xxx_deemphasis_info;
  779. knew.get = snd_akm4xxx_deemphasis_get;
  780. knew.put = snd_akm4xxx_deemphasis_put;
  781. switch (ak->type) {
  782. case SND_AK4524:
  783. case SND_AK4528:
  784. case SND_AK4620:
  785. /* register 3 */
  786. knew.private_value = AK_COMPOSE(idx, 3, 0, 0);
  787. break;
  788. case SND_AK4529: {
  789. int shift = idx == 3 ? 6 : (2 - idx) * 2;
  790. /* register 8 with shift */
  791. knew.private_value = AK_COMPOSE(0, 8, shift, 0);
  792. break;
  793. }
  794. case SND_AK4355:
  795. case SND_AK4358:
  796. knew.private_value = AK_COMPOSE(idx, 3, 0, 0);
  797. break;
  798. case SND_AK4381:
  799. knew.private_value = AK_COMPOSE(idx, 1, 1, 0);
  800. break;
  801. default:
  802. return -EINVAL;
  803. }
  804. err = snd_ctl_add(ak->card, snd_ctl_new1(&knew, ak));
  805. if (err < 0)
  806. return err;
  807. }
  808. return 0;
  809. }
  810. #ifdef CONFIG_PROC_FS
  811. static void proc_regs_read(struct snd_info_entry *entry,
  812. struct snd_info_buffer *buffer)
  813. {
  814. struct snd_akm4xxx *ak = entry->private_data;
  815. int reg, val, chip;
  816. for (chip = 0; chip < ak->num_chips; chip++) {
  817. for (reg = 0; reg < ak->total_regs; reg++) {
  818. val = snd_akm4xxx_get(ak, chip, reg);
  819. snd_iprintf(buffer, "chip %d: 0x%02x = 0x%02x\n", chip,
  820. reg, val);
  821. }
  822. }
  823. }
  824. static int proc_init(struct snd_akm4xxx *ak)
  825. {
  826. struct snd_info_entry *entry;
  827. int err;
  828. err = snd_card_proc_new(ak->card, ak->name, &entry);
  829. if (err < 0)
  830. return err;
  831. snd_info_set_text_ops(entry, ak, proc_regs_read);
  832. return 0;
  833. }
  834. #else /* !CONFIG_PROC_FS */
  835. static int proc_init(struct snd_akm4xxx *ak) { return 0; }
  836. #endif
  837. int snd_akm4xxx_build_controls(struct snd_akm4xxx *ak)
  838. {
  839. int err, num_emphs;
  840. err = build_dac_controls(ak);
  841. if (err < 0)
  842. return err;
  843. err = build_adc_controls(ak);
  844. if (err < 0)
  845. return err;
  846. if (ak->type == SND_AK4355 || ak->type == SND_AK4358)
  847. num_emphs = 1;
  848. else if (ak->type == SND_AK4620)
  849. num_emphs = 0;
  850. else
  851. num_emphs = ak->num_dacs / 2;
  852. err = build_deemphasis(ak, num_emphs);
  853. if (err < 0)
  854. return err;
  855. err = proc_init(ak);
  856. if (err < 0)
  857. return err;
  858. return 0;
  859. }
  860. EXPORT_SYMBOL(snd_akm4xxx_build_controls);
  861. static int __init alsa_akm4xxx_module_init(void)
  862. {
  863. return 0;
  864. }
  865. static void __exit alsa_akm4xxx_module_exit(void)
  866. {
  867. }
  868. module_init(alsa_akm4xxx_module_init)
  869. module_exit(alsa_akm4xxx_module_exit)