ak4114.c 19 KB

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