cs8427.c 18 KB

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