ak4xxx-adda.c 25 KB

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