ak4xxx-adda.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892
  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 <perex@perex.cz>,
  7. * Takashi Iwai <tiwai@suse.de>
  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 <perex@perex.cz>, Takashi Iwai <tiwai@suse.de>");
  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. snd_akm4xxx_write(ak, chip, addr, nval);
  366. return 1;
  367. }
  368. static int snd_akm4xxx_volume_put(struct snd_kcontrol *kcontrol,
  369. struct snd_ctl_elem_value *ucontrol)
  370. {
  371. unsigned int mask = AK_GET_MASK(kcontrol->private_value);
  372. unsigned int val = ucontrol->value.integer.value[0];
  373. if (val > mask)
  374. return -EINVAL;
  375. return put_ak_reg(kcontrol, AK_GET_ADDR(kcontrol->private_value), val);
  376. }
  377. static int snd_akm4xxx_stereo_volume_info(struct snd_kcontrol *kcontrol,
  378. struct snd_ctl_elem_info *uinfo)
  379. {
  380. unsigned int mask = AK_GET_MASK(kcontrol->private_value);
  381. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  382. uinfo->count = 2;
  383. uinfo->value.integer.min = 0;
  384. uinfo->value.integer.max = mask;
  385. return 0;
  386. }
  387. static int snd_akm4xxx_stereo_volume_get(struct snd_kcontrol *kcontrol,
  388. struct snd_ctl_elem_value *ucontrol)
  389. {
  390. struct snd_akm4xxx *ak = snd_kcontrol_chip(kcontrol);
  391. int chip = AK_GET_CHIP(kcontrol->private_value);
  392. int addr = AK_GET_ADDR(kcontrol->private_value);
  393. ucontrol->value.integer.value[0] = snd_akm4xxx_get_vol(ak, chip, addr);
  394. ucontrol->value.integer.value[1] = snd_akm4xxx_get_vol(ak, chip, addr+1);
  395. return 0;
  396. }
  397. static int snd_akm4xxx_stereo_volume_put(struct snd_kcontrol *kcontrol,
  398. struct snd_ctl_elem_value *ucontrol)
  399. {
  400. int addr = AK_GET_ADDR(kcontrol->private_value);
  401. unsigned int mask = AK_GET_MASK(kcontrol->private_value);
  402. unsigned int val[2];
  403. int change;
  404. val[0] = ucontrol->value.integer.value[0];
  405. val[1] = ucontrol->value.integer.value[1];
  406. if (val[0] > mask || val[1] > mask)
  407. return -EINVAL;
  408. change = put_ak_reg(kcontrol, addr, val[0]);
  409. change |= put_ak_reg(kcontrol, addr + 1, val[1]);
  410. return change;
  411. }
  412. static int snd_akm4xxx_deemphasis_info(struct snd_kcontrol *kcontrol,
  413. struct snd_ctl_elem_info *uinfo)
  414. {
  415. static const char * const texts[4] = {
  416. "44.1kHz", "Off", "48kHz", "32kHz",
  417. };
  418. return snd_ctl_enum_info(uinfo, 1, 4, texts);
  419. }
  420. static int snd_akm4xxx_deemphasis_get(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. ucontrol->value.enumerated.item[0] =
  428. (snd_akm4xxx_get(ak, chip, addr) >> shift) & 3;
  429. return 0;
  430. }
  431. static int snd_akm4xxx_deemphasis_put(struct snd_kcontrol *kcontrol,
  432. struct snd_ctl_elem_value *ucontrol)
  433. {
  434. struct snd_akm4xxx *ak = snd_kcontrol_chip(kcontrol);
  435. int chip = AK_GET_CHIP(kcontrol->private_value);
  436. int addr = AK_GET_ADDR(kcontrol->private_value);
  437. int shift = AK_GET_SHIFT(kcontrol->private_value);
  438. unsigned char nval = ucontrol->value.enumerated.item[0] & 3;
  439. int change;
  440. nval = (nval << shift) |
  441. (snd_akm4xxx_get(ak, chip, addr) & ~(3 << shift));
  442. change = snd_akm4xxx_get(ak, chip, addr) != nval;
  443. if (change)
  444. snd_akm4xxx_write(ak, chip, addr, nval);
  445. return change;
  446. }
  447. #define ak4xxx_switch_info snd_ctl_boolean_mono_info
  448. static int ak4xxx_switch_get(struct snd_kcontrol *kcontrol,
  449. struct snd_ctl_elem_value *ucontrol)
  450. {
  451. struct snd_akm4xxx *ak = snd_kcontrol_chip(kcontrol);
  452. int chip = AK_GET_CHIP(kcontrol->private_value);
  453. int addr = AK_GET_ADDR(kcontrol->private_value);
  454. int shift = AK_GET_SHIFT(kcontrol->private_value);
  455. int invert = AK_GET_INVERT(kcontrol->private_value);
  456. /* we observe the (1<<shift) bit only */
  457. unsigned char val = snd_akm4xxx_get(ak, chip, addr) & (1<<shift);
  458. if (invert)
  459. val = ! val;
  460. ucontrol->value.integer.value[0] = (val & (1<<shift)) != 0;
  461. return 0;
  462. }
  463. static int ak4xxx_switch_put(struct snd_kcontrol *kcontrol,
  464. struct snd_ctl_elem_value *ucontrol)
  465. {
  466. struct snd_akm4xxx *ak = snd_kcontrol_chip(kcontrol);
  467. int chip = AK_GET_CHIP(kcontrol->private_value);
  468. int addr = AK_GET_ADDR(kcontrol->private_value);
  469. int shift = AK_GET_SHIFT(kcontrol->private_value);
  470. int invert = AK_GET_INVERT(kcontrol->private_value);
  471. long flag = ucontrol->value.integer.value[0];
  472. unsigned char val, oval;
  473. int change;
  474. if (invert)
  475. flag = ! flag;
  476. oval = snd_akm4xxx_get(ak, chip, addr);
  477. if (flag)
  478. val = oval | (1<<shift);
  479. else
  480. val = oval & ~(1<<shift);
  481. change = (oval != val);
  482. if (change)
  483. snd_akm4xxx_write(ak, chip, addr, val);
  484. return change;
  485. }
  486. #define AK5365_NUM_INPUTS 5
  487. static int ak4xxx_capture_num_inputs(struct snd_akm4xxx *ak, int mixer_ch)
  488. {
  489. int num_names;
  490. const char **input_names;
  491. input_names = ak->adc_info[mixer_ch].input_names;
  492. num_names = 0;
  493. while (num_names < AK5365_NUM_INPUTS && input_names[num_names])
  494. ++num_names;
  495. return num_names;
  496. }
  497. static int ak4xxx_capture_source_info(struct snd_kcontrol *kcontrol,
  498. struct snd_ctl_elem_info *uinfo)
  499. {
  500. struct snd_akm4xxx *ak = snd_kcontrol_chip(kcontrol);
  501. int mixer_ch = AK_GET_SHIFT(kcontrol->private_value);
  502. unsigned int num_names;
  503. num_names = ak4xxx_capture_num_inputs(ak, mixer_ch);
  504. if (!num_names)
  505. return -EINVAL;
  506. return snd_ctl_enum_info(uinfo, 1, num_names,
  507. ak->adc_info[mixer_ch].input_names);
  508. }
  509. static int ak4xxx_capture_source_get(struct snd_kcontrol *kcontrol,
  510. struct snd_ctl_elem_value *ucontrol)
  511. {
  512. struct snd_akm4xxx *ak = snd_kcontrol_chip(kcontrol);
  513. int chip = AK_GET_CHIP(kcontrol->private_value);
  514. int addr = AK_GET_ADDR(kcontrol->private_value);
  515. int mask = AK_GET_MASK(kcontrol->private_value);
  516. unsigned char val;
  517. val = snd_akm4xxx_get(ak, chip, addr) & mask;
  518. ucontrol->value.enumerated.item[0] = val;
  519. return 0;
  520. }
  521. static int ak4xxx_capture_source_put(struct snd_kcontrol *kcontrol,
  522. struct snd_ctl_elem_value *ucontrol)
  523. {
  524. struct snd_akm4xxx *ak = snd_kcontrol_chip(kcontrol);
  525. int mixer_ch = AK_GET_SHIFT(kcontrol->private_value);
  526. int chip = AK_GET_CHIP(kcontrol->private_value);
  527. int addr = AK_GET_ADDR(kcontrol->private_value);
  528. int mask = AK_GET_MASK(kcontrol->private_value);
  529. unsigned char oval, val;
  530. int num_names = ak4xxx_capture_num_inputs(ak, mixer_ch);
  531. if (ucontrol->value.enumerated.item[0] >= num_names)
  532. return -EINVAL;
  533. oval = snd_akm4xxx_get(ak, chip, addr);
  534. val = oval & ~mask;
  535. val |= ucontrol->value.enumerated.item[0] & mask;
  536. if (val != oval) {
  537. snd_akm4xxx_write(ak, chip, addr, val);
  538. return 1;
  539. }
  540. return 0;
  541. }
  542. /*
  543. * build AK4xxx controls
  544. */
  545. static int build_dac_controls(struct snd_akm4xxx *ak)
  546. {
  547. int idx, err, mixer_ch, num_stereo;
  548. struct snd_kcontrol_new knew;
  549. mixer_ch = 0;
  550. for (idx = 0; idx < ak->num_dacs; ) {
  551. /* mute control for Revolution 7.1 - AK4381 */
  552. if (ak->type == SND_AK4381
  553. && ak->dac_info[mixer_ch].switch_name) {
  554. memset(&knew, 0, sizeof(knew));
  555. knew.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
  556. knew.count = 1;
  557. knew.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
  558. knew.name = ak->dac_info[mixer_ch].switch_name;
  559. knew.info = ak4xxx_switch_info;
  560. knew.get = ak4xxx_switch_get;
  561. knew.put = ak4xxx_switch_put;
  562. knew.access = 0;
  563. /* register 1, bit 0 (SMUTE): 0 = normal operation,
  564. 1 = mute */
  565. knew.private_value =
  566. AK_COMPOSE(idx/2, 1, 0, 0) | AK_INVERT;
  567. err = snd_ctl_add(ak->card, snd_ctl_new1(&knew, ak));
  568. if (err < 0)
  569. return err;
  570. }
  571. memset(&knew, 0, sizeof(knew));
  572. if (! ak->dac_info || ! ak->dac_info[mixer_ch].name) {
  573. knew.name = "DAC Volume";
  574. knew.index = mixer_ch + ak->idx_offset * 2;
  575. num_stereo = 1;
  576. } else {
  577. knew.name = ak->dac_info[mixer_ch].name;
  578. num_stereo = ak->dac_info[mixer_ch].num_channels;
  579. }
  580. knew.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
  581. knew.count = 1;
  582. knew.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
  583. SNDRV_CTL_ELEM_ACCESS_TLV_READ;
  584. if (num_stereo == 2) {
  585. knew.info = snd_akm4xxx_stereo_volume_info;
  586. knew.get = snd_akm4xxx_stereo_volume_get;
  587. knew.put = snd_akm4xxx_stereo_volume_put;
  588. } else {
  589. knew.info = snd_akm4xxx_volume_info;
  590. knew.get = snd_akm4xxx_volume_get;
  591. knew.put = snd_akm4xxx_volume_put;
  592. }
  593. switch (ak->type) {
  594. case SND_AK4524:
  595. /* register 6 & 7 */
  596. knew.private_value =
  597. AK_COMPOSE(idx/2, (idx%2) + 6, 0, 127) |
  598. AK_VOL_CVT;
  599. knew.tlv.p = db_scale_vol_datt;
  600. break;
  601. case SND_AK4528:
  602. /* register 4 & 5 */
  603. knew.private_value =
  604. AK_COMPOSE(idx/2, (idx%2) + 4, 0, 127) |
  605. AK_VOL_CVT;
  606. knew.tlv.p = db_scale_vol_datt;
  607. break;
  608. case SND_AK4529: {
  609. /* registers 2-7 and b,c */
  610. int val = idx < 6 ? idx + 2 : (idx - 6) + 0xb;
  611. knew.private_value =
  612. AK_COMPOSE(0, val, 0, 255) | AK_INVERT;
  613. knew.tlv.p = db_scale_8bit;
  614. break;
  615. }
  616. case SND_AK4355:
  617. /* register 4-9, chip #0 only */
  618. knew.private_value = AK_COMPOSE(0, idx + 4, 0, 255);
  619. knew.tlv.p = db_scale_8bit;
  620. break;
  621. case SND_AK4358: {
  622. /* register 4-9 and 11-12, chip #0 only */
  623. int addr = idx < 6 ? idx + 4 : idx + 5;
  624. knew.private_value =
  625. AK_COMPOSE(0, addr, 0, 127) | AK_NEEDSMSB;
  626. knew.tlv.p = db_scale_7bit;
  627. break;
  628. }
  629. case SND_AK4381:
  630. /* register 3 & 4 */
  631. knew.private_value =
  632. AK_COMPOSE(idx/2, (idx%2) + 3, 0, 255);
  633. knew.tlv.p = db_scale_linear;
  634. break;
  635. case SND_AK4620:
  636. /* register 6 & 7 */
  637. knew.private_value =
  638. AK_COMPOSE(idx/2, (idx%2) + 6, 0, 255);
  639. knew.tlv.p = db_scale_linear;
  640. break;
  641. default:
  642. return -EINVAL;
  643. }
  644. err = snd_ctl_add(ak->card, snd_ctl_new1(&knew, ak));
  645. if (err < 0)
  646. return err;
  647. idx += num_stereo;
  648. mixer_ch++;
  649. }
  650. return 0;
  651. }
  652. static int build_adc_controls(struct snd_akm4xxx *ak)
  653. {
  654. int idx, err, mixer_ch, num_stereo, max_steps;
  655. struct snd_kcontrol_new knew;
  656. mixer_ch = 0;
  657. if (ak->type == SND_AK4528)
  658. return 0; /* no controls */
  659. for (idx = 0; idx < ak->num_adcs;) {
  660. memset(&knew, 0, sizeof(knew));
  661. if (! ak->adc_info || ! ak->adc_info[mixer_ch].name) {
  662. knew.name = "ADC Volume";
  663. knew.index = mixer_ch + ak->idx_offset * 2;
  664. num_stereo = 1;
  665. } else {
  666. knew.name = ak->adc_info[mixer_ch].name;
  667. num_stereo = ak->adc_info[mixer_ch].num_channels;
  668. }
  669. knew.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
  670. knew.count = 1;
  671. knew.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
  672. SNDRV_CTL_ELEM_ACCESS_TLV_READ;
  673. if (num_stereo == 2) {
  674. knew.info = snd_akm4xxx_stereo_volume_info;
  675. knew.get = snd_akm4xxx_stereo_volume_get;
  676. knew.put = snd_akm4xxx_stereo_volume_put;
  677. } else {
  678. knew.info = snd_akm4xxx_volume_info;
  679. knew.get = snd_akm4xxx_volume_get;
  680. knew.put = snd_akm4xxx_volume_put;
  681. }
  682. /* register 4 & 5 */
  683. if (ak->type == SND_AK5365)
  684. max_steps = 152;
  685. else
  686. max_steps = 164;
  687. knew.private_value =
  688. AK_COMPOSE(idx/2, (idx%2) + 4, 0, max_steps) |
  689. AK_VOL_CVT | AK_IPGA;
  690. knew.tlv.p = db_scale_vol_datt;
  691. err = snd_ctl_add(ak->card, snd_ctl_new1(&knew, ak));
  692. if (err < 0)
  693. return err;
  694. if (ak->type == SND_AK5365 && (idx % 2) == 0) {
  695. if (! ak->adc_info ||
  696. ! ak->adc_info[mixer_ch].switch_name) {
  697. knew.name = "Capture Switch";
  698. knew.index = mixer_ch + ak->idx_offset * 2;
  699. } else
  700. knew.name = ak->adc_info[mixer_ch].switch_name;
  701. knew.info = ak4xxx_switch_info;
  702. knew.get = ak4xxx_switch_get;
  703. knew.put = ak4xxx_switch_put;
  704. knew.access = 0;
  705. /* register 2, bit 0 (SMUTE): 0 = normal operation,
  706. 1 = mute */
  707. knew.private_value =
  708. AK_COMPOSE(idx/2, 2, 0, 0) | AK_INVERT;
  709. err = snd_ctl_add(ak->card, snd_ctl_new1(&knew, ak));
  710. if (err < 0)
  711. return err;
  712. memset(&knew, 0, sizeof(knew));
  713. if (!ak->adc_info ||
  714. !ak->adc_info[mixer_ch].selector_name) {
  715. knew.name = "Capture Channel";
  716. knew.index = mixer_ch + ak->idx_offset * 2;
  717. } else
  718. knew.name = ak->adc_info[mixer_ch].selector_name;
  719. knew.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
  720. knew.info = ak4xxx_capture_source_info;
  721. knew.get = ak4xxx_capture_source_get;
  722. knew.put = ak4xxx_capture_source_put;
  723. knew.access = 0;
  724. /* input selector control: reg. 1, bits 0-2.
  725. * mis-use 'shift' to pass mixer_ch */
  726. knew.private_value
  727. = AK_COMPOSE(idx/2, 1, mixer_ch, 0x07);
  728. err = snd_ctl_add(ak->card, snd_ctl_new1(&knew, ak));
  729. if (err < 0)
  730. return err;
  731. }
  732. idx += num_stereo;
  733. mixer_ch++;
  734. }
  735. return 0;
  736. }
  737. static int build_deemphasis(struct snd_akm4xxx *ak, int num_emphs)
  738. {
  739. int idx, err;
  740. struct snd_kcontrol_new knew;
  741. for (idx = 0; idx < num_emphs; idx++) {
  742. memset(&knew, 0, sizeof(knew));
  743. knew.name = "Deemphasis";
  744. knew.index = idx + ak->idx_offset;
  745. knew.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
  746. knew.count = 1;
  747. knew.info = snd_akm4xxx_deemphasis_info;
  748. knew.get = snd_akm4xxx_deemphasis_get;
  749. knew.put = snd_akm4xxx_deemphasis_put;
  750. switch (ak->type) {
  751. case SND_AK4524:
  752. case SND_AK4528:
  753. case SND_AK4620:
  754. /* register 3 */
  755. knew.private_value = AK_COMPOSE(idx, 3, 0, 0);
  756. break;
  757. case SND_AK4529: {
  758. int shift = idx == 3 ? 6 : (2 - idx) * 2;
  759. /* register 8 with shift */
  760. knew.private_value = AK_COMPOSE(0, 8, shift, 0);
  761. break;
  762. }
  763. case SND_AK4355:
  764. case SND_AK4358:
  765. knew.private_value = AK_COMPOSE(idx, 3, 0, 0);
  766. break;
  767. case SND_AK4381:
  768. knew.private_value = AK_COMPOSE(idx, 1, 1, 0);
  769. break;
  770. default:
  771. return -EINVAL;
  772. }
  773. err = snd_ctl_add(ak->card, snd_ctl_new1(&knew, ak));
  774. if (err < 0)
  775. return err;
  776. }
  777. return 0;
  778. }
  779. static void proc_regs_read(struct snd_info_entry *entry,
  780. struct snd_info_buffer *buffer)
  781. {
  782. struct snd_akm4xxx *ak = entry->private_data;
  783. int reg, val, chip;
  784. for (chip = 0; chip < ak->num_chips; chip++) {
  785. for (reg = 0; reg < ak->total_regs; reg++) {
  786. val = snd_akm4xxx_get(ak, chip, reg);
  787. snd_iprintf(buffer, "chip %d: 0x%02x = 0x%02x\n", chip,
  788. reg, val);
  789. }
  790. }
  791. }
  792. static int proc_init(struct snd_akm4xxx *ak)
  793. {
  794. return snd_card_ro_proc_new(ak->card, ak->name, ak, proc_regs_read);
  795. }
  796. int snd_akm4xxx_build_controls(struct snd_akm4xxx *ak)
  797. {
  798. int err, num_emphs;
  799. err = build_dac_controls(ak);
  800. if (err < 0)
  801. return err;
  802. err = build_adc_controls(ak);
  803. if (err < 0)
  804. return err;
  805. if (ak->type == SND_AK4355 || ak->type == SND_AK4358)
  806. num_emphs = 1;
  807. else if (ak->type == SND_AK4620)
  808. num_emphs = 0;
  809. else
  810. num_emphs = ak->num_dacs / 2;
  811. err = build_deemphasis(ak, num_emphs);
  812. if (err < 0)
  813. return err;
  814. err = proc_init(ak);
  815. if (err < 0)
  816. return err;
  817. return 0;
  818. }
  819. EXPORT_SYMBOL(snd_akm4xxx_build_controls);