ak4114.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  1. /*
  2. * Routines for control of the AK4114 via I2C and 4-wire serial interface
  3. * IEC958 (S/PDIF) receiver by Asahi Kasei
  4. * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
  5. *
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. *
  21. */
  22. #include <linux/slab.h>
  23. #include <linux/delay.h>
  24. #include <sound/core.h>
  25. #include <sound/control.h>
  26. #include <sound/pcm.h>
  27. #include <sound/ak4114.h>
  28. #include <sound/asoundef.h>
  29. #include <sound/info.h>
  30. MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
  31. MODULE_DESCRIPTION("AK4114 IEC958 (S/PDIF) receiver by Asahi Kasei");
  32. MODULE_LICENSE("GPL");
  33. #define AK4114_ADDR 0x00 /* fixed address */
  34. static void ak4114_stats(struct work_struct *work);
  35. static void ak4114_init_regs(struct ak4114 *chip);
  36. static void reg_write(struct ak4114 *ak4114, unsigned char reg, unsigned char val)
  37. {
  38. ak4114->write(ak4114->private_data, reg, val);
  39. if (reg <= AK4114_REG_INT1_MASK)
  40. ak4114->regmap[reg] = val;
  41. else if (reg >= AK4114_REG_TXCSB0 && reg <= AK4114_REG_TXCSB4)
  42. ak4114->txcsb[reg-AK4114_REG_TXCSB0] = val;
  43. }
  44. static inline unsigned char reg_read(struct ak4114 *ak4114, unsigned char reg)
  45. {
  46. return ak4114->read(ak4114->private_data, reg);
  47. }
  48. #if 0
  49. static void reg_dump(struct ak4114 *ak4114)
  50. {
  51. int i;
  52. printk(KERN_DEBUG "AK4114 REG DUMP:\n");
  53. for (i = 0; i < 0x20; i++)
  54. printk(KERN_DEBUG "reg[%02x] = %02x (%02x)\n", i, reg_read(ak4114, i), i < sizeof(ak4114->regmap) ? ak4114->regmap[i] : 0);
  55. }
  56. #endif
  57. static void snd_ak4114_free(struct ak4114 *chip)
  58. {
  59. chip->init = 1; /* don't schedule new work */
  60. mb();
  61. cancel_delayed_work_sync(&chip->work);
  62. kfree(chip);
  63. }
  64. static int snd_ak4114_dev_free(struct snd_device *device)
  65. {
  66. struct ak4114 *chip = device->device_data;
  67. snd_ak4114_free(chip);
  68. return 0;
  69. }
  70. int snd_ak4114_create(struct snd_card *card,
  71. ak4114_read_t *read, ak4114_write_t *write,
  72. const unsigned char pgm[7], const unsigned char txcsb[5],
  73. void *private_data, struct ak4114 **r_ak4114)
  74. {
  75. struct ak4114 *chip;
  76. int err = 0;
  77. unsigned char reg;
  78. static struct snd_device_ops ops = {
  79. .dev_free = snd_ak4114_dev_free,
  80. };
  81. chip = kzalloc(sizeof(*chip), GFP_KERNEL);
  82. if (chip == NULL)
  83. return -ENOMEM;
  84. spin_lock_init(&chip->lock);
  85. chip->card = card;
  86. chip->read = read;
  87. chip->write = write;
  88. chip->private_data = private_data;
  89. INIT_DELAYED_WORK(&chip->work, ak4114_stats);
  90. for (reg = 0; reg < 7; reg++)
  91. chip->regmap[reg] = pgm[reg];
  92. for (reg = 0; reg < 5; reg++)
  93. chip->txcsb[reg] = txcsb[reg];
  94. ak4114_init_regs(chip);
  95. chip->rcs0 = reg_read(chip, AK4114_REG_RCS0) & ~(AK4114_QINT | AK4114_CINT);
  96. chip->rcs1 = reg_read(chip, AK4114_REG_RCS1);
  97. if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0)
  98. goto __fail;
  99. if (r_ak4114)
  100. *r_ak4114 = chip;
  101. return 0;
  102. __fail:
  103. snd_ak4114_free(chip);
  104. return err < 0 ? err : -EIO;
  105. }
  106. void snd_ak4114_reg_write(struct ak4114 *chip, unsigned char reg, unsigned char mask, unsigned char val)
  107. {
  108. if (reg <= AK4114_REG_INT1_MASK)
  109. reg_write(chip, reg, (chip->regmap[reg] & ~mask) | val);
  110. else if (reg >= AK4114_REG_TXCSB0 && reg <= AK4114_REG_TXCSB4)
  111. reg_write(chip, reg,
  112. (chip->txcsb[reg-AK4114_REG_TXCSB0] & ~mask) | val);
  113. }
  114. static void ak4114_init_regs(struct ak4114 *chip)
  115. {
  116. unsigned char old = chip->regmap[AK4114_REG_PWRDN], reg;
  117. /* bring the chip to reset state and powerdown state */
  118. reg_write(chip, AK4114_REG_PWRDN, old & ~(AK4114_RST|AK4114_PWN));
  119. udelay(200);
  120. /* release reset, but leave powerdown */
  121. reg_write(chip, AK4114_REG_PWRDN, (old | AK4114_RST) & ~AK4114_PWN);
  122. udelay(200);
  123. for (reg = 1; reg < 7; reg++)
  124. reg_write(chip, reg, chip->regmap[reg]);
  125. for (reg = 0; reg < 5; reg++)
  126. reg_write(chip, reg + AK4114_REG_TXCSB0, chip->txcsb[reg]);
  127. /* release powerdown, everything is initialized now */
  128. reg_write(chip, AK4114_REG_PWRDN, old | AK4114_RST | AK4114_PWN);
  129. }
  130. void snd_ak4114_reinit(struct ak4114 *chip)
  131. {
  132. chip->init = 1;
  133. mb();
  134. flush_delayed_work_sync(&chip->work);
  135. ak4114_init_regs(chip);
  136. /* bring up statistics / event queing */
  137. chip->init = 0;
  138. if (chip->kctls[0])
  139. schedule_delayed_work(&chip->work, HZ / 10);
  140. }
  141. static unsigned int external_rate(unsigned char rcs1)
  142. {
  143. switch (rcs1 & (AK4114_FS0|AK4114_FS1|AK4114_FS2|AK4114_FS3)) {
  144. case AK4114_FS_32000HZ: return 32000;
  145. case AK4114_FS_44100HZ: return 44100;
  146. case AK4114_FS_48000HZ: return 48000;
  147. case AK4114_FS_88200HZ: return 88200;
  148. case AK4114_FS_96000HZ: return 96000;
  149. case AK4114_FS_176400HZ: return 176400;
  150. case AK4114_FS_192000HZ: return 192000;
  151. default: return 0;
  152. }
  153. }
  154. static int snd_ak4114_in_error_info(struct snd_kcontrol *kcontrol,
  155. struct snd_ctl_elem_info *uinfo)
  156. {
  157. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  158. uinfo->count = 1;
  159. uinfo->value.integer.min = 0;
  160. uinfo->value.integer.max = LONG_MAX;
  161. return 0;
  162. }
  163. static int snd_ak4114_in_error_get(struct snd_kcontrol *kcontrol,
  164. struct snd_ctl_elem_value *ucontrol)
  165. {
  166. struct ak4114 *chip = snd_kcontrol_chip(kcontrol);
  167. long *ptr;
  168. spin_lock_irq(&chip->lock);
  169. ptr = (long *)(((char *)chip) + kcontrol->private_value);
  170. ucontrol->value.integer.value[0] = *ptr;
  171. *ptr = 0;
  172. spin_unlock_irq(&chip->lock);
  173. return 0;
  174. }
  175. #define snd_ak4114_in_bit_info snd_ctl_boolean_mono_info
  176. static int snd_ak4114_in_bit_get(struct snd_kcontrol *kcontrol,
  177. struct snd_ctl_elem_value *ucontrol)
  178. {
  179. struct ak4114 *chip = snd_kcontrol_chip(kcontrol);
  180. unsigned char reg = kcontrol->private_value & 0xff;
  181. unsigned char bit = (kcontrol->private_value >> 8) & 0xff;
  182. unsigned char inv = (kcontrol->private_value >> 31) & 1;
  183. ucontrol->value.integer.value[0] = ((reg_read(chip, reg) & (1 << bit)) ? 1 : 0) ^ inv;
  184. return 0;
  185. }
  186. static int snd_ak4114_rate_info(struct snd_kcontrol *kcontrol,
  187. struct snd_ctl_elem_info *uinfo)
  188. {
  189. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  190. uinfo->count = 1;
  191. uinfo->value.integer.min = 0;
  192. uinfo->value.integer.max = 192000;
  193. return 0;
  194. }
  195. static int snd_ak4114_rate_get(struct snd_kcontrol *kcontrol,
  196. struct snd_ctl_elem_value *ucontrol)
  197. {
  198. struct ak4114 *chip = snd_kcontrol_chip(kcontrol);
  199. ucontrol->value.integer.value[0] = external_rate(reg_read(chip, AK4114_REG_RCS1));
  200. return 0;
  201. }
  202. static int snd_ak4114_spdif_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
  203. {
  204. uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
  205. uinfo->count = 1;
  206. return 0;
  207. }
  208. static int snd_ak4114_spdif_get(struct snd_kcontrol *kcontrol,
  209. struct snd_ctl_elem_value *ucontrol)
  210. {
  211. struct ak4114 *chip = snd_kcontrol_chip(kcontrol);
  212. unsigned i;
  213. for (i = 0; i < AK4114_REG_RXCSB_SIZE; i++)
  214. ucontrol->value.iec958.status[i] = reg_read(chip, AK4114_REG_RXCSB0 + i);
  215. return 0;
  216. }
  217. static int snd_ak4114_spdif_playback_get(struct snd_kcontrol *kcontrol,
  218. struct snd_ctl_elem_value *ucontrol)
  219. {
  220. struct ak4114 *chip = snd_kcontrol_chip(kcontrol);
  221. unsigned i;
  222. for (i = 0; i < AK4114_REG_TXCSB_SIZE; i++)
  223. ucontrol->value.iec958.status[i] = chip->txcsb[i];
  224. return 0;
  225. }
  226. static int snd_ak4114_spdif_playback_put(struct snd_kcontrol *kcontrol,
  227. struct snd_ctl_elem_value *ucontrol)
  228. {
  229. struct ak4114 *chip = snd_kcontrol_chip(kcontrol);
  230. unsigned i;
  231. for (i = 0; i < AK4114_REG_TXCSB_SIZE; i++)
  232. reg_write(chip, AK4114_REG_TXCSB0 + i, ucontrol->value.iec958.status[i]);
  233. return 0;
  234. }
  235. static int snd_ak4114_spdif_mask_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
  236. {
  237. uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
  238. uinfo->count = 1;
  239. return 0;
  240. }
  241. static int snd_ak4114_spdif_mask_get(struct snd_kcontrol *kcontrol,
  242. struct snd_ctl_elem_value *ucontrol)
  243. {
  244. memset(ucontrol->value.iec958.status, 0xff, AK4114_REG_RXCSB_SIZE);
  245. return 0;
  246. }
  247. static int snd_ak4114_spdif_pinfo(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
  248. {
  249. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  250. uinfo->value.integer.min = 0;
  251. uinfo->value.integer.max = 0xffff;
  252. uinfo->count = 4;
  253. return 0;
  254. }
  255. static int snd_ak4114_spdif_pget(struct snd_kcontrol *kcontrol,
  256. struct snd_ctl_elem_value *ucontrol)
  257. {
  258. struct ak4114 *chip = snd_kcontrol_chip(kcontrol);
  259. unsigned short tmp;
  260. ucontrol->value.integer.value[0] = 0xf8f2;
  261. ucontrol->value.integer.value[1] = 0x4e1f;
  262. tmp = reg_read(chip, AK4114_REG_Pc0) | (reg_read(chip, AK4114_REG_Pc1) << 8);
  263. ucontrol->value.integer.value[2] = tmp;
  264. tmp = reg_read(chip, AK4114_REG_Pd0) | (reg_read(chip, AK4114_REG_Pd1) << 8);
  265. ucontrol->value.integer.value[3] = tmp;
  266. return 0;
  267. }
  268. static int snd_ak4114_spdif_qinfo(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
  269. {
  270. uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
  271. uinfo->count = AK4114_REG_QSUB_SIZE;
  272. return 0;
  273. }
  274. static int snd_ak4114_spdif_qget(struct snd_kcontrol *kcontrol,
  275. struct snd_ctl_elem_value *ucontrol)
  276. {
  277. struct ak4114 *chip = snd_kcontrol_chip(kcontrol);
  278. unsigned i;
  279. for (i = 0; i < AK4114_REG_QSUB_SIZE; i++)
  280. ucontrol->value.bytes.data[i] = reg_read(chip, AK4114_REG_QSUB_ADDR + i);
  281. return 0;
  282. }
  283. /* Don't forget to change AK4114_CONTROLS define!!! */
  284. static struct snd_kcontrol_new snd_ak4114_iec958_controls[] = {
  285. {
  286. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  287. .name = "IEC958 Parity Errors",
  288. .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
  289. .info = snd_ak4114_in_error_info,
  290. .get = snd_ak4114_in_error_get,
  291. .private_value = offsetof(struct ak4114, parity_errors),
  292. },
  293. {
  294. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  295. .name = "IEC958 V-Bit Errors",
  296. .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
  297. .info = snd_ak4114_in_error_info,
  298. .get = snd_ak4114_in_error_get,
  299. .private_value = offsetof(struct ak4114, v_bit_errors),
  300. },
  301. {
  302. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  303. .name = "IEC958 C-CRC Errors",
  304. .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
  305. .info = snd_ak4114_in_error_info,
  306. .get = snd_ak4114_in_error_get,
  307. .private_value = offsetof(struct ak4114, ccrc_errors),
  308. },
  309. {
  310. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  311. .name = "IEC958 Q-CRC Errors",
  312. .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
  313. .info = snd_ak4114_in_error_info,
  314. .get = snd_ak4114_in_error_get,
  315. .private_value = offsetof(struct ak4114, qcrc_errors),
  316. },
  317. {
  318. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  319. .name = "IEC958 External Rate",
  320. .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
  321. .info = snd_ak4114_rate_info,
  322. .get = snd_ak4114_rate_get,
  323. },
  324. {
  325. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  326. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,MASK),
  327. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  328. .info = snd_ak4114_spdif_mask_info,
  329. .get = snd_ak4114_spdif_mask_get,
  330. },
  331. {
  332. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  333. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
  334. .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
  335. .info = snd_ak4114_spdif_info,
  336. .get = snd_ak4114_spdif_playback_get,
  337. .put = snd_ak4114_spdif_playback_put,
  338. },
  339. {
  340. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  341. .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,MASK),
  342. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  343. .info = snd_ak4114_spdif_mask_info,
  344. .get = snd_ak4114_spdif_mask_get,
  345. },
  346. {
  347. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  348. .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,DEFAULT),
  349. .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
  350. .info = snd_ak4114_spdif_info,
  351. .get = snd_ak4114_spdif_get,
  352. },
  353. {
  354. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  355. .name = "IEC958 Preample Capture Default",
  356. .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
  357. .info = snd_ak4114_spdif_pinfo,
  358. .get = snd_ak4114_spdif_pget,
  359. },
  360. {
  361. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  362. .name = "IEC958 Q-subcode Capture Default",
  363. .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
  364. .info = snd_ak4114_spdif_qinfo,
  365. .get = snd_ak4114_spdif_qget,
  366. },
  367. {
  368. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  369. .name = "IEC958 Audio",
  370. .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
  371. .info = snd_ak4114_in_bit_info,
  372. .get = snd_ak4114_in_bit_get,
  373. .private_value = (1<<31) | (1<<8) | AK4114_REG_RCS0,
  374. },
  375. {
  376. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  377. .name = "IEC958 Non-PCM Bitstream",
  378. .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
  379. .info = snd_ak4114_in_bit_info,
  380. .get = snd_ak4114_in_bit_get,
  381. .private_value = (6<<8) | AK4114_REG_RCS0,
  382. },
  383. {
  384. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  385. .name = "IEC958 DTS Bitstream",
  386. .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
  387. .info = snd_ak4114_in_bit_info,
  388. .get = snd_ak4114_in_bit_get,
  389. .private_value = (3<<8) | AK4114_REG_RCS0,
  390. },
  391. {
  392. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  393. .name = "IEC958 PPL Lock Status",
  394. .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
  395. .info = snd_ak4114_in_bit_info,
  396. .get = snd_ak4114_in_bit_get,
  397. .private_value = (1<<31) | (4<<8) | AK4114_REG_RCS0,
  398. }
  399. };
  400. static void snd_ak4114_proc_regs_read(struct snd_info_entry *entry,
  401. struct snd_info_buffer *buffer)
  402. {
  403. struct ak4114 *ak4114 = entry->private_data;
  404. int reg, val;
  405. /* all ak4114 registers 0x00 - 0x1f */
  406. for (reg = 0; reg < 0x20; reg++) {
  407. val = reg_read(ak4114, reg);
  408. snd_iprintf(buffer, "0x%02x = 0x%02x\n", reg, val);
  409. }
  410. }
  411. static void snd_ak4114_proc_init(struct ak4114 *ak4114)
  412. {
  413. struct snd_info_entry *entry;
  414. if (!snd_card_proc_new(ak4114->card, "ak4114", &entry))
  415. snd_info_set_text_ops(entry, ak4114, snd_ak4114_proc_regs_read);
  416. }
  417. int snd_ak4114_build(struct ak4114 *ak4114,
  418. struct snd_pcm_substream *ply_substream,
  419. struct snd_pcm_substream *cap_substream)
  420. {
  421. struct snd_kcontrol *kctl;
  422. unsigned int idx;
  423. int err;
  424. if (snd_BUG_ON(!cap_substream))
  425. return -EINVAL;
  426. ak4114->playback_substream = ply_substream;
  427. ak4114->capture_substream = cap_substream;
  428. for (idx = 0; idx < AK4114_CONTROLS; idx++) {
  429. kctl = snd_ctl_new1(&snd_ak4114_iec958_controls[idx], ak4114);
  430. if (kctl == NULL)
  431. return -ENOMEM;
  432. if (strstr(kctl->id.name, "Playback")) {
  433. if (ply_substream == NULL) {
  434. snd_ctl_free_one(kctl);
  435. ak4114->kctls[idx] = NULL;
  436. continue;
  437. }
  438. kctl->id.device = ply_substream->pcm->device;
  439. kctl->id.subdevice = ply_substream->number;
  440. } else {
  441. kctl->id.device = cap_substream->pcm->device;
  442. kctl->id.subdevice = cap_substream->number;
  443. }
  444. err = snd_ctl_add(ak4114->card, kctl);
  445. if (err < 0)
  446. return err;
  447. ak4114->kctls[idx] = kctl;
  448. }
  449. snd_ak4114_proc_init(ak4114);
  450. /* trigger workq */
  451. schedule_delayed_work(&ak4114->work, HZ / 10);
  452. return 0;
  453. }
  454. /* notify kcontrols if any parameters are changed */
  455. static void ak4114_notify(struct ak4114 *ak4114,
  456. unsigned char rcs0, unsigned char rcs1,
  457. unsigned char c0, unsigned char c1)
  458. {
  459. if (!ak4114->kctls[0])
  460. return;
  461. if (rcs0 & AK4114_PAR)
  462. snd_ctl_notify(ak4114->card, SNDRV_CTL_EVENT_MASK_VALUE,
  463. &ak4114->kctls[0]->id);
  464. if (rcs0 & AK4114_V)
  465. snd_ctl_notify(ak4114->card, SNDRV_CTL_EVENT_MASK_VALUE,
  466. &ak4114->kctls[1]->id);
  467. if (rcs1 & AK4114_CCRC)
  468. snd_ctl_notify(ak4114->card, SNDRV_CTL_EVENT_MASK_VALUE,
  469. &ak4114->kctls[2]->id);
  470. if (rcs1 & AK4114_QCRC)
  471. snd_ctl_notify(ak4114->card, SNDRV_CTL_EVENT_MASK_VALUE,
  472. &ak4114->kctls[3]->id);
  473. /* rate change */
  474. if (c1 & 0xf0)
  475. snd_ctl_notify(ak4114->card, SNDRV_CTL_EVENT_MASK_VALUE,
  476. &ak4114->kctls[4]->id);
  477. if ((c0 & AK4114_PEM) | (c0 & AK4114_CINT))
  478. snd_ctl_notify(ak4114->card, SNDRV_CTL_EVENT_MASK_VALUE,
  479. &ak4114->kctls[9]->id);
  480. if (c0 & AK4114_QINT)
  481. snd_ctl_notify(ak4114->card, SNDRV_CTL_EVENT_MASK_VALUE,
  482. &ak4114->kctls[10]->id);
  483. if (c0 & AK4114_AUDION)
  484. snd_ctl_notify(ak4114->card, SNDRV_CTL_EVENT_MASK_VALUE,
  485. &ak4114->kctls[11]->id);
  486. if (c0 & AK4114_AUTO)
  487. snd_ctl_notify(ak4114->card, SNDRV_CTL_EVENT_MASK_VALUE,
  488. &ak4114->kctls[12]->id);
  489. if (c0 & AK4114_DTSCD)
  490. snd_ctl_notify(ak4114->card, SNDRV_CTL_EVENT_MASK_VALUE,
  491. &ak4114->kctls[13]->id);
  492. if (c0 & AK4114_UNLCK)
  493. snd_ctl_notify(ak4114->card, SNDRV_CTL_EVENT_MASK_VALUE,
  494. &ak4114->kctls[14]->id);
  495. }
  496. int snd_ak4114_external_rate(struct ak4114 *ak4114)
  497. {
  498. unsigned char rcs1;
  499. rcs1 = reg_read(ak4114, AK4114_REG_RCS1);
  500. return external_rate(rcs1);
  501. }
  502. int snd_ak4114_check_rate_and_errors(struct ak4114 *ak4114, unsigned int flags)
  503. {
  504. struct snd_pcm_runtime *runtime = ak4114->capture_substream ? ak4114->capture_substream->runtime : NULL;
  505. unsigned long _flags;
  506. int res = 0;
  507. unsigned char rcs0, rcs1;
  508. unsigned char c0, c1;
  509. rcs1 = reg_read(ak4114, AK4114_REG_RCS1);
  510. if (flags & AK4114_CHECK_NO_STAT)
  511. goto __rate;
  512. rcs0 = reg_read(ak4114, AK4114_REG_RCS0);
  513. spin_lock_irqsave(&ak4114->lock, _flags);
  514. if (rcs0 & AK4114_PAR)
  515. ak4114->parity_errors++;
  516. if (rcs1 & AK4114_V)
  517. ak4114->v_bit_errors++;
  518. if (rcs1 & AK4114_CCRC)
  519. ak4114->ccrc_errors++;
  520. if (rcs1 & AK4114_QCRC)
  521. ak4114->qcrc_errors++;
  522. c0 = (ak4114->rcs0 & (AK4114_QINT | AK4114_CINT | AK4114_PEM | AK4114_AUDION | AK4114_AUTO | AK4114_UNLCK)) ^
  523. (rcs0 & (AK4114_QINT | AK4114_CINT | AK4114_PEM | AK4114_AUDION | AK4114_AUTO | AK4114_UNLCK));
  524. c1 = (ak4114->rcs1 & 0xf0) ^ (rcs1 & 0xf0);
  525. ak4114->rcs0 = rcs0 & ~(AK4114_QINT | AK4114_CINT);
  526. ak4114->rcs1 = rcs1;
  527. spin_unlock_irqrestore(&ak4114->lock, _flags);
  528. ak4114_notify(ak4114, rcs0, rcs1, c0, c1);
  529. if (ak4114->change_callback && (c0 | c1) != 0)
  530. ak4114->change_callback(ak4114, c0, c1);
  531. __rate:
  532. /* compare rate */
  533. res = external_rate(rcs1);
  534. if (!(flags & AK4114_CHECK_NO_RATE) && runtime && runtime->rate != res) {
  535. snd_pcm_stream_lock_irqsave(ak4114->capture_substream, _flags);
  536. if (snd_pcm_running(ak4114->capture_substream)) {
  537. // printk(KERN_DEBUG "rate changed (%i <- %i)\n", runtime->rate, res);
  538. snd_pcm_stop(ak4114->capture_substream, SNDRV_PCM_STATE_DRAINING);
  539. res = 1;
  540. }
  541. snd_pcm_stream_unlock_irqrestore(ak4114->capture_substream, _flags);
  542. }
  543. return res;
  544. }
  545. static void ak4114_stats(struct work_struct *work)
  546. {
  547. struct ak4114 *chip = container_of(work, struct ak4114, work.work);
  548. if (!chip->init)
  549. snd_ak4114_check_rate_and_errors(chip, chip->check_flags);
  550. schedule_delayed_work(&chip->work, HZ / 10);
  551. }
  552. EXPORT_SYMBOL(snd_ak4114_create);
  553. EXPORT_SYMBOL(snd_ak4114_reg_write);
  554. EXPORT_SYMBOL(snd_ak4114_reinit);
  555. EXPORT_SYMBOL(snd_ak4114_build);
  556. EXPORT_SYMBOL(snd_ak4114_external_rate);
  557. EXPORT_SYMBOL(snd_ak4114_check_rate_and_errors);