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