cs8427.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Routines for control of the CS8427 via i2c bus
  4. * IEC958 (S/PDIF) receiver & transmitter by Cirrus Logic
  5. * Copyright (c) by Jaroslav Kysela <[email protected]>
  6. */
  7. #include <linux/slab.h>
  8. #include <linux/delay.h>
  9. #include <linux/init.h>
  10. #include <linux/bitrev.h>
  11. #include <linux/module.h>
  12. #include <asm/unaligned.h>
  13. #include <sound/core.h>
  14. #include <sound/control.h>
  15. #include <sound/pcm.h>
  16. #include <sound/cs8427.h>
  17. #include <sound/asoundef.h>
  18. static void snd_cs8427_reset(struct snd_i2c_device *cs8427);
  19. MODULE_AUTHOR("Jaroslav Kysela <[email protected]>");
  20. MODULE_DESCRIPTION("IEC958 (S/PDIF) receiver & transmitter by Cirrus Logic");
  21. MODULE_LICENSE("GPL");
  22. #define CS8427_ADDR (0x20>>1) /* fixed address */
  23. struct cs8427_stream {
  24. struct snd_pcm_substream *substream;
  25. char hw_status[24]; /* hardware status */
  26. char def_status[24]; /* default status */
  27. char pcm_status[24]; /* PCM private status */
  28. char hw_udata[32];
  29. struct snd_kcontrol *pcm_ctl;
  30. };
  31. struct cs8427 {
  32. unsigned char regmap[0x14]; /* map of first 1 + 13 registers */
  33. unsigned int rate;
  34. unsigned int reset_timeout;
  35. struct cs8427_stream playback;
  36. struct cs8427_stream capture;
  37. };
  38. int snd_cs8427_reg_write(struct snd_i2c_device *device, unsigned char reg,
  39. unsigned char val)
  40. {
  41. int err;
  42. unsigned char buf[2];
  43. buf[0] = reg & 0x7f;
  44. buf[1] = val;
  45. err = snd_i2c_sendbytes(device, buf, 2);
  46. if (err != 2) {
  47. snd_printk(KERN_ERR "unable to send bytes 0x%02x:0x%02x "
  48. "to CS8427 (%i)\n", buf[0], buf[1], err);
  49. return err < 0 ? err : -EIO;
  50. }
  51. return 0;
  52. }
  53. EXPORT_SYMBOL(snd_cs8427_reg_write);
  54. static int snd_cs8427_reg_read(struct snd_i2c_device *device, unsigned char reg)
  55. {
  56. int err;
  57. unsigned char buf;
  58. err = snd_i2c_sendbytes(device, &reg, 1);
  59. if (err != 1) {
  60. snd_printk(KERN_ERR "unable to send register 0x%x byte "
  61. "to CS8427\n", reg);
  62. return err < 0 ? err : -EIO;
  63. }
  64. err = snd_i2c_readbytes(device, &buf, 1);
  65. if (err != 1) {
  66. snd_printk(KERN_ERR "unable to read register 0x%x byte "
  67. "from CS8427\n", reg);
  68. return err < 0 ? err : -EIO;
  69. }
  70. return buf;
  71. }
  72. static int snd_cs8427_select_corudata(struct snd_i2c_device *device, int udata)
  73. {
  74. struct cs8427 *chip = device->private_data;
  75. int err;
  76. udata = udata ? CS8427_BSEL : 0;
  77. if (udata != (chip->regmap[CS8427_REG_CSDATABUF] & udata)) {
  78. chip->regmap[CS8427_REG_CSDATABUF] &= ~CS8427_BSEL;
  79. chip->regmap[CS8427_REG_CSDATABUF] |= udata;
  80. err = snd_cs8427_reg_write(device, CS8427_REG_CSDATABUF,
  81. chip->regmap[CS8427_REG_CSDATABUF]);
  82. if (err < 0)
  83. return err;
  84. }
  85. return 0;
  86. }
  87. static int snd_cs8427_send_corudata(struct snd_i2c_device *device,
  88. int udata,
  89. unsigned char *ndata,
  90. int count)
  91. {
  92. struct cs8427 *chip = device->private_data;
  93. char *hw_data = udata ?
  94. chip->playback.hw_udata : chip->playback.hw_status;
  95. unsigned char data[32];
  96. int err, idx;
  97. if (!memcmp(hw_data, ndata, count))
  98. return 0;
  99. err = snd_cs8427_select_corudata(device, udata);
  100. if (err < 0)
  101. return err;
  102. memcpy(hw_data, ndata, count);
  103. if (udata) {
  104. memset(data, 0, sizeof(data));
  105. if (memcmp(hw_data, data, count) == 0) {
  106. chip->regmap[CS8427_REG_UDATABUF] &= ~CS8427_UBMMASK;
  107. chip->regmap[CS8427_REG_UDATABUF] |= CS8427_UBMZEROS |
  108. CS8427_EFTUI;
  109. err = snd_cs8427_reg_write(device, CS8427_REG_UDATABUF,
  110. chip->regmap[CS8427_REG_UDATABUF]);
  111. return err < 0 ? err : 0;
  112. }
  113. }
  114. data[0] = CS8427_REG_AUTOINC | CS8427_REG_CORU_DATABUF;
  115. for (idx = 0; idx < count; idx++)
  116. data[idx + 1] = bitrev8(ndata[idx]);
  117. if (snd_i2c_sendbytes(device, data, count + 1) != count + 1)
  118. return -EIO;
  119. return 1;
  120. }
  121. static void snd_cs8427_free(struct snd_i2c_device *device)
  122. {
  123. kfree(device->private_data);
  124. }
  125. int snd_cs8427_init(struct snd_i2c_bus *bus,
  126. struct snd_i2c_device *device)
  127. {
  128. static unsigned char initvals1[] = {
  129. CS8427_REG_CONTROL1 | CS8427_REG_AUTOINC,
  130. /* CS8427_REG_CONTROL1: RMCK to OMCK, valid PCM audio, disable mutes,
  131. TCBL=output */
  132. CS8427_SWCLK | CS8427_TCBLDIR,
  133. /* CS8427_REG_CONTROL2: hold last valid audio sample, RMCK=256*Fs,
  134. normal stereo operation */
  135. 0x00,
  136. /* CS8427_REG_DATAFLOW: output drivers normal operation, Tx<=serial,
  137. Rx=>serial */
  138. CS8427_TXDSERIAL | CS8427_SPDAES3RECEIVER,
  139. /* CS8427_REG_CLOCKSOURCE: Run off, CMCK=256*Fs,
  140. output time base = OMCK, input time base = recovered input clock,
  141. recovered input clock source is ILRCK changed to AES3INPUT
  142. (workaround, see snd_cs8427_reset) */
  143. CS8427_RXDILRCK,
  144. /* CS8427_REG_SERIALINPUT: Serial audio input port data format = I2S,
  145. 24-bit, 64*Fsi */
  146. CS8427_SIDEL | CS8427_SILRPOL,
  147. /* CS8427_REG_SERIALOUTPUT: Serial audio output port data format
  148. = I2S, 24-bit, 64*Fsi */
  149. CS8427_SODEL | CS8427_SOLRPOL,
  150. };
  151. static unsigned char initvals2[] = {
  152. CS8427_REG_RECVERRMASK | CS8427_REG_AUTOINC,
  153. /* CS8427_REG_RECVERRMASK: unmask the input PLL clock, V, confidence,
  154. biphase, parity status bits */
  155. /* CS8427_UNLOCK | CS8427_V | CS8427_CONF | CS8427_BIP | CS8427_PAR,*/
  156. 0xff, /* set everything */
  157. /* CS8427_REG_CSDATABUF:
  158. Registers 32-55 window to CS buffer
  159. Inhibit D->E transfers from overwriting first 5 bytes of CS data.
  160. Inhibit D->E transfers (all) of CS data.
  161. Allow E->F transfer of CS data.
  162. One byte mode; both A/B channels get same written CB data.
  163. A channel info is output to chip's EMPH* pin. */
  164. CS8427_CBMR | CS8427_DETCI,
  165. /* CS8427_REG_UDATABUF:
  166. Use internal buffer to transmit User (U) data.
  167. Chip's U pin is an output.
  168. Transmit all O's for user data.
  169. Inhibit D->E transfers.
  170. Inhibit E->F transfers. */
  171. CS8427_UD | CS8427_EFTUI | CS8427_DETUI,
  172. };
  173. struct cs8427 *chip = device->private_data;
  174. int err;
  175. unsigned char buf[24];
  176. snd_i2c_lock(bus);
  177. err = snd_cs8427_reg_read(device, CS8427_REG_ID_AND_VER);
  178. if (err != CS8427_VER8427A) {
  179. /* give second chance */
  180. snd_printk(KERN_WARNING "invalid CS8427 signature 0x%x: "
  181. "let me try again...\n", err);
  182. err = snd_cs8427_reg_read(device, CS8427_REG_ID_AND_VER);
  183. }
  184. if (err != CS8427_VER8427A) {
  185. snd_i2c_unlock(bus);
  186. snd_printk(KERN_ERR "unable to find CS8427 signature "
  187. "(expected 0x%x, read 0x%x),\n",
  188. CS8427_VER8427A, err);
  189. snd_printk(KERN_ERR " initialization is not completed\n");
  190. return -EFAULT;
  191. }
  192. /* turn off run bit while making changes to configuration */
  193. err = snd_cs8427_reg_write(device, CS8427_REG_CLOCKSOURCE, 0x00);
  194. if (err < 0)
  195. goto __fail;
  196. /* send initial values */
  197. memcpy(chip->regmap + (initvals1[0] & 0x7f), initvals1 + 1, 6);
  198. err = snd_i2c_sendbytes(device, initvals1, 7);
  199. if (err != 7) {
  200. err = err < 0 ? err : -EIO;
  201. goto __fail;
  202. }
  203. /* Turn off CS8427 interrupt stuff that is not used in hardware */
  204. memset(buf, 0, 7);
  205. /* from address 9 to 15 */
  206. buf[0] = 9; /* register */
  207. err = snd_i2c_sendbytes(device, buf, 7);
  208. if (err != 7)
  209. goto __fail;
  210. /* send transfer initialization sequence */
  211. memcpy(chip->regmap + (initvals2[0] & 0x7f), initvals2 + 1, 3);
  212. err = snd_i2c_sendbytes(device, initvals2, 4);
  213. if (err != 4) {
  214. err = err < 0 ? err : -EIO;
  215. goto __fail;
  216. }
  217. /* write default channel status bytes */
  218. put_unaligned_le32(SNDRV_PCM_DEFAULT_CON_SPDIF, buf);
  219. memset(buf + 4, 0, 24 - 4);
  220. if (snd_cs8427_send_corudata(device, 0, buf, 24) < 0)
  221. goto __fail;
  222. memcpy(chip->playback.def_status, buf, 24);
  223. memcpy(chip->playback.pcm_status, buf, 24);
  224. snd_i2c_unlock(bus);
  225. /* turn on run bit and rock'n'roll */
  226. snd_cs8427_reset(device);
  227. return 0;
  228. __fail:
  229. snd_i2c_unlock(bus);
  230. return err;
  231. }
  232. EXPORT_SYMBOL(snd_cs8427_init);
  233. int snd_cs8427_create(struct snd_i2c_bus *bus,
  234. unsigned char addr,
  235. unsigned int reset_timeout,
  236. struct snd_i2c_device **r_cs8427)
  237. {
  238. int err;
  239. struct cs8427 *chip;
  240. struct snd_i2c_device *device;
  241. err = snd_i2c_device_create(bus, "CS8427", CS8427_ADDR | (addr & 7),
  242. &device);
  243. if (err < 0)
  244. return err;
  245. chip = device->private_data = kzalloc(sizeof(*chip), GFP_KERNEL);
  246. if (chip == NULL) {
  247. snd_i2c_device_free(device);
  248. return -ENOMEM;
  249. }
  250. device->private_free = snd_cs8427_free;
  251. if (reset_timeout < 1)
  252. reset_timeout = 1;
  253. chip->reset_timeout = reset_timeout;
  254. err = snd_cs8427_init(bus, device);
  255. if (err)
  256. goto __fail;
  257. #if 0 // it's nice for read tests
  258. {
  259. char buf[128];
  260. int xx;
  261. buf[0] = 0x81;
  262. snd_i2c_sendbytes(device, buf, 1);
  263. snd_i2c_readbytes(device, buf, 127);
  264. for (xx = 0; xx < 127; xx++)
  265. printk(KERN_DEBUG "reg[0x%x] = 0x%x\n", xx+1, buf[xx]);
  266. }
  267. #endif
  268. if (r_cs8427)
  269. *r_cs8427 = device;
  270. return 0;
  271. __fail:
  272. snd_i2c_device_free(device);
  273. return err < 0 ? err : -EIO;
  274. }
  275. EXPORT_SYMBOL(snd_cs8427_create);
  276. /*
  277. * Reset the chip using run bit, also lock PLL using ILRCK and
  278. * put back AES3INPUT. This workaround is described in latest
  279. * CS8427 datasheet, otherwise TXDSERIAL will not work.
  280. */
  281. static void snd_cs8427_reset(struct snd_i2c_device *cs8427)
  282. {
  283. struct cs8427 *chip;
  284. unsigned long end_time;
  285. int data, aes3input = 0;
  286. if (snd_BUG_ON(!cs8427))
  287. return;
  288. chip = cs8427->private_data;
  289. snd_i2c_lock(cs8427->bus);
  290. if ((chip->regmap[CS8427_REG_CLOCKSOURCE] & CS8427_RXDAES3INPUT) ==
  291. CS8427_RXDAES3INPUT) /* AES3 bit is set */
  292. aes3input = 1;
  293. chip->regmap[CS8427_REG_CLOCKSOURCE] &= ~(CS8427_RUN | CS8427_RXDMASK);
  294. snd_cs8427_reg_write(cs8427, CS8427_REG_CLOCKSOURCE,
  295. chip->regmap[CS8427_REG_CLOCKSOURCE]);
  296. udelay(200);
  297. chip->regmap[CS8427_REG_CLOCKSOURCE] |= CS8427_RUN | CS8427_RXDILRCK;
  298. snd_cs8427_reg_write(cs8427, CS8427_REG_CLOCKSOURCE,
  299. chip->regmap[CS8427_REG_CLOCKSOURCE]);
  300. udelay(200);
  301. snd_i2c_unlock(cs8427->bus);
  302. end_time = jiffies + chip->reset_timeout;
  303. while (time_after_eq(end_time, jiffies)) {
  304. snd_i2c_lock(cs8427->bus);
  305. data = snd_cs8427_reg_read(cs8427, CS8427_REG_RECVERRORS);
  306. snd_i2c_unlock(cs8427->bus);
  307. if (!(data & CS8427_UNLOCK))
  308. break;
  309. schedule_timeout_uninterruptible(1);
  310. }
  311. snd_i2c_lock(cs8427->bus);
  312. chip->regmap[CS8427_REG_CLOCKSOURCE] &= ~CS8427_RXDMASK;
  313. if (aes3input)
  314. chip->regmap[CS8427_REG_CLOCKSOURCE] |= CS8427_RXDAES3INPUT;
  315. snd_cs8427_reg_write(cs8427, CS8427_REG_CLOCKSOURCE,
  316. chip->regmap[CS8427_REG_CLOCKSOURCE]);
  317. snd_i2c_unlock(cs8427->bus);
  318. }
  319. static int snd_cs8427_in_status_info(struct snd_kcontrol *kcontrol,
  320. struct snd_ctl_elem_info *uinfo)
  321. {
  322. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  323. uinfo->count = 1;
  324. uinfo->value.integer.min = 0;
  325. uinfo->value.integer.max = 255;
  326. return 0;
  327. }
  328. static int snd_cs8427_in_status_get(struct snd_kcontrol *kcontrol,
  329. struct snd_ctl_elem_value *ucontrol)
  330. {
  331. struct snd_i2c_device *device = snd_kcontrol_chip(kcontrol);
  332. int data;
  333. snd_i2c_lock(device->bus);
  334. data = snd_cs8427_reg_read(device, kcontrol->private_value);
  335. snd_i2c_unlock(device->bus);
  336. if (data < 0)
  337. return data;
  338. ucontrol->value.integer.value[0] = data;
  339. return 0;
  340. }
  341. static int snd_cs8427_qsubcode_info(struct snd_kcontrol *kcontrol,
  342. struct snd_ctl_elem_info *uinfo)
  343. {
  344. uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
  345. uinfo->count = 10;
  346. return 0;
  347. }
  348. static int snd_cs8427_qsubcode_get(struct snd_kcontrol *kcontrol,
  349. struct snd_ctl_elem_value *ucontrol)
  350. {
  351. struct snd_i2c_device *device = snd_kcontrol_chip(kcontrol);
  352. unsigned char reg = CS8427_REG_QSUBCODE;
  353. int err;
  354. snd_i2c_lock(device->bus);
  355. err = snd_i2c_sendbytes(device, &reg, 1);
  356. if (err != 1) {
  357. snd_printk(KERN_ERR "unable to send register 0x%x byte "
  358. "to CS8427\n", reg);
  359. snd_i2c_unlock(device->bus);
  360. return err < 0 ? err : -EIO;
  361. }
  362. err = snd_i2c_readbytes(device, ucontrol->value.bytes.data, 10);
  363. if (err != 10) {
  364. snd_printk(KERN_ERR "unable to read Q-subcode bytes "
  365. "from CS8427\n");
  366. snd_i2c_unlock(device->bus);
  367. return err < 0 ? err : -EIO;
  368. }
  369. snd_i2c_unlock(device->bus);
  370. return 0;
  371. }
  372. static int snd_cs8427_spdif_info(struct snd_kcontrol *kcontrol,
  373. struct snd_ctl_elem_info *uinfo)
  374. {
  375. uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
  376. uinfo->count = 1;
  377. return 0;
  378. }
  379. static int snd_cs8427_spdif_get(struct snd_kcontrol *kcontrol,
  380. struct snd_ctl_elem_value *ucontrol)
  381. {
  382. struct snd_i2c_device *device = snd_kcontrol_chip(kcontrol);
  383. struct cs8427 *chip = device->private_data;
  384. snd_i2c_lock(device->bus);
  385. memcpy(ucontrol->value.iec958.status, chip->playback.def_status, 24);
  386. snd_i2c_unlock(device->bus);
  387. return 0;
  388. }
  389. static int snd_cs8427_spdif_put(struct snd_kcontrol *kcontrol,
  390. struct snd_ctl_elem_value *ucontrol)
  391. {
  392. struct snd_i2c_device *device = snd_kcontrol_chip(kcontrol);
  393. struct cs8427 *chip = device->private_data;
  394. unsigned char *status = kcontrol->private_value ?
  395. chip->playback.pcm_status : chip->playback.def_status;
  396. struct snd_pcm_runtime *runtime = chip->playback.substream ?
  397. chip->playback.substream->runtime : NULL;
  398. int err, change;
  399. snd_i2c_lock(device->bus);
  400. change = memcmp(ucontrol->value.iec958.status, status, 24) != 0;
  401. memcpy(status, ucontrol->value.iec958.status, 24);
  402. if (change && (kcontrol->private_value ?
  403. runtime != NULL : runtime == NULL)) {
  404. err = snd_cs8427_send_corudata(device, 0, status, 24);
  405. if (err < 0)
  406. change = err;
  407. }
  408. snd_i2c_unlock(device->bus);
  409. return change;
  410. }
  411. static int snd_cs8427_spdif_mask_info(struct snd_kcontrol *kcontrol,
  412. struct snd_ctl_elem_info *uinfo)
  413. {
  414. uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
  415. uinfo->count = 1;
  416. return 0;
  417. }
  418. static int snd_cs8427_spdif_mask_get(struct snd_kcontrol *kcontrol,
  419. struct snd_ctl_elem_value *ucontrol)
  420. {
  421. memset(ucontrol->value.iec958.status, 0xff, 24);
  422. return 0;
  423. }
  424. static const struct snd_kcontrol_new snd_cs8427_iec958_controls[] = {
  425. {
  426. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  427. .info = snd_cs8427_in_status_info,
  428. .name = "IEC958 CS8427 Input Status",
  429. .access = (SNDRV_CTL_ELEM_ACCESS_READ |
  430. SNDRV_CTL_ELEM_ACCESS_VOLATILE),
  431. .get = snd_cs8427_in_status_get,
  432. .private_value = 15,
  433. },
  434. {
  435. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  436. .info = snd_cs8427_in_status_info,
  437. .name = "IEC958 CS8427 Error Status",
  438. .access = (SNDRV_CTL_ELEM_ACCESS_READ |
  439. SNDRV_CTL_ELEM_ACCESS_VOLATILE),
  440. .get = snd_cs8427_in_status_get,
  441. .private_value = 16,
  442. },
  443. {
  444. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  445. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  446. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,MASK),
  447. .info = snd_cs8427_spdif_mask_info,
  448. .get = snd_cs8427_spdif_mask_get,
  449. },
  450. {
  451. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  452. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
  453. .info = snd_cs8427_spdif_info,
  454. .get = snd_cs8427_spdif_get,
  455. .put = snd_cs8427_spdif_put,
  456. .private_value = 0
  457. },
  458. {
  459. .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
  460. SNDRV_CTL_ELEM_ACCESS_INACTIVE),
  461. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  462. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PCM_STREAM),
  463. .info = snd_cs8427_spdif_info,
  464. .get = snd_cs8427_spdif_get,
  465. .put = snd_cs8427_spdif_put,
  466. .private_value = 1
  467. },
  468. {
  469. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  470. .info = snd_cs8427_qsubcode_info,
  471. .name = "IEC958 Q-subcode Capture Default",
  472. .access = (SNDRV_CTL_ELEM_ACCESS_READ |
  473. SNDRV_CTL_ELEM_ACCESS_VOLATILE),
  474. .get = snd_cs8427_qsubcode_get
  475. }};
  476. int snd_cs8427_iec958_build(struct snd_i2c_device *cs8427,
  477. struct snd_pcm_substream *play_substream,
  478. struct snd_pcm_substream *cap_substream)
  479. {
  480. struct cs8427 *chip = cs8427->private_data;
  481. struct snd_kcontrol *kctl;
  482. unsigned int idx;
  483. int err;
  484. if (snd_BUG_ON(!play_substream || !cap_substream))
  485. return -EINVAL;
  486. for (idx = 0; idx < ARRAY_SIZE(snd_cs8427_iec958_controls); idx++) {
  487. kctl = snd_ctl_new1(&snd_cs8427_iec958_controls[idx], cs8427);
  488. if (kctl == NULL)
  489. return -ENOMEM;
  490. kctl->id.device = play_substream->pcm->device;
  491. kctl->id.subdevice = play_substream->number;
  492. err = snd_ctl_add(cs8427->bus->card, kctl);
  493. if (err < 0)
  494. return err;
  495. if (! strcmp(kctl->id.name,
  496. SNDRV_CTL_NAME_IEC958("",PLAYBACK,PCM_STREAM)))
  497. chip->playback.pcm_ctl = kctl;
  498. }
  499. chip->playback.substream = play_substream;
  500. chip->capture.substream = cap_substream;
  501. if (snd_BUG_ON(!chip->playback.pcm_ctl))
  502. return -EIO;
  503. return 0;
  504. }
  505. EXPORT_SYMBOL(snd_cs8427_iec958_build);
  506. int snd_cs8427_iec958_active(struct snd_i2c_device *cs8427, int active)
  507. {
  508. struct cs8427 *chip;
  509. if (snd_BUG_ON(!cs8427))
  510. return -ENXIO;
  511. chip = cs8427->private_data;
  512. if (active) {
  513. memcpy(chip->playback.pcm_status,
  514. chip->playback.def_status, 24);
  515. chip->playback.pcm_ctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
  516. } else {
  517. chip->playback.pcm_ctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
  518. }
  519. snd_ctl_notify(cs8427->bus->card,
  520. SNDRV_CTL_EVENT_MASK_VALUE | SNDRV_CTL_EVENT_MASK_INFO,
  521. &chip->playback.pcm_ctl->id);
  522. return 0;
  523. }
  524. EXPORT_SYMBOL(snd_cs8427_iec958_active);
  525. int snd_cs8427_iec958_pcm(struct snd_i2c_device *cs8427, unsigned int rate)
  526. {
  527. struct cs8427 *chip;
  528. char *status;
  529. int err, reset;
  530. if (snd_BUG_ON(!cs8427))
  531. return -ENXIO;
  532. chip = cs8427->private_data;
  533. status = chip->playback.pcm_status;
  534. snd_i2c_lock(cs8427->bus);
  535. if (status[0] & IEC958_AES0_PROFESSIONAL) {
  536. status[0] &= ~IEC958_AES0_PRO_FS;
  537. switch (rate) {
  538. case 32000: status[0] |= IEC958_AES0_PRO_FS_32000; break;
  539. case 44100: status[0] |= IEC958_AES0_PRO_FS_44100; break;
  540. case 48000: status[0] |= IEC958_AES0_PRO_FS_48000; break;
  541. default: status[0] |= IEC958_AES0_PRO_FS_NOTID; break;
  542. }
  543. } else {
  544. status[3] &= ~IEC958_AES3_CON_FS;
  545. switch (rate) {
  546. case 32000: status[3] |= IEC958_AES3_CON_FS_32000; break;
  547. case 44100: status[3] |= IEC958_AES3_CON_FS_44100; break;
  548. case 48000: status[3] |= IEC958_AES3_CON_FS_48000; break;
  549. }
  550. }
  551. err = snd_cs8427_send_corudata(cs8427, 0, status, 24);
  552. if (err > 0)
  553. snd_ctl_notify(cs8427->bus->card,
  554. SNDRV_CTL_EVENT_MASK_VALUE,
  555. &chip->playback.pcm_ctl->id);
  556. reset = chip->rate != rate;
  557. chip->rate = rate;
  558. snd_i2c_unlock(cs8427->bus);
  559. if (reset)
  560. snd_cs8427_reset(cs8427);
  561. return err < 0 ? err : 0;
  562. }
  563. EXPORT_SYMBOL(snd_cs8427_iec958_pcm);