cs8427.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990
  1. /*
  2. * Routines for control of the CS8427 via i2c bus
  3. * IEC958 (S/PDIF) receiver & transmitter by Cirrus Logic
  4. * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
  5. * Copyright (c) 2012, The Linux Foundation. All rights reserved.
  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 version 2 and
  9. * only version 2 as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. */
  17. #include <linux/slab.h>
  18. #include <linux/delay.h>
  19. #include <linux/init.h>
  20. #include <linux/bitrev.h>
  21. #include <linux/bitops.h>
  22. #include <linux/module.h>
  23. //#include <linux/export.h>
  24. #include <linux/i2c.h>
  25. #include <linux/gpio.h>
  26. #include <asm/unaligned.h>
  27. #include <sound/core.h>
  28. #include <sound/control.h>
  29. #include <sound/pcm.h>
  30. #include <sound/cs8427.h>
  31. #include <sound/asoundef.h>
  32. #include <sound/pcm.h>
  33. #include <sound/pcm_params.h>
  34. #include <sound/soc.h>
  35. #include <sound/soc-dapm.h>
  36. #include <sound/tlv.h>
  37. #define CS8427_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000 |\
  38. SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_48000 |\
  39. SNDRV_PCM_RATE_96000)
  40. #define CS8427_FORMATS (SNDRV_PCM_FMTBIT_S24_LE |\
  41. SNDRV_PCM_FORMAT_S16_LE |\
  42. SNDRV_PCM_FORMAT_S20_3LE)
  43. struct cs8427_stream {
  44. struct snd_pcm_substream *substream;
  45. char hw_status[CHANNEL_STATUS_SIZE]; /* hardware status */
  46. char def_status[CHANNEL_STATUS_SIZE]; /* default status */
  47. char pcm_status[CHANNEL_STATUS_SIZE]; /* PCM private status */
  48. char hw_udata[32];
  49. struct snd_kcontrol *pcm_ctl;
  50. };
  51. struct cs8427 {
  52. struct i2c_client *client;
  53. struct i2c_msg xfer_msg[2];
  54. unsigned char regmap[0x14]; /* map of first 1 + 13 registers */
  55. unsigned int reset_timeout;
  56. struct cs8427_stream playback;
  57. };
  58. static int cs8427_i2c_write_device(struct cs8427 *cs8427_i2c,
  59. u16 reg, u8 *value, u32 bytes)
  60. {
  61. struct i2c_msg *msg;
  62. int ret = 0;
  63. u8 reg_addr = 0;
  64. u8 data[bytes + 1];
  65. if (cs8427_i2c->client == NULL) {
  66. pr_err("%s: failed to get device info\n", __func__);
  67. return -ENODEV;
  68. }
  69. reg_addr = (u8)reg;
  70. msg = &cs8427_i2c->xfer_msg[0];
  71. msg->addr = cs8427_i2c->client->addr;
  72. msg->len = bytes + 1;
  73. msg->flags = 0;
  74. data[0] = reg_addr;
  75. data[1] = *value;
  76. msg->buf = data;
  77. ret = i2c_transfer(cs8427_i2c->client->adapter,
  78. cs8427_i2c->xfer_msg, 1);
  79. /* Try again if the write fails
  80. * checking with ebusy and number of bytes executed
  81. * for write ret value should be 1
  82. */
  83. if ((ret != 1) || (ret == -EBUSY)) {
  84. ret = i2c_transfer(
  85. cs8427_i2c->client->adapter,
  86. cs8427_i2c->xfer_msg, 1);
  87. if ((ret != 1) || (ret < 0)) {
  88. dev_err(&cs8427_i2c->client->dev,
  89. "failed to write the"
  90. " device reg %d\n", reg);
  91. return ret;
  92. }
  93. }
  94. return 0;
  95. }
  96. static int cs8427_i2c_write(struct cs8427 *chip, unsigned short reg,
  97. int bytes, void *src)
  98. {
  99. int ret = 0, err = 0;
  100. struct cs8427_platform_data *pdata = chip->client->dev.platform_data;
  101. /*
  102. * enable the 100KHz level shifter to communicate
  103. * with CS8427 chip
  104. */
  105. if (pdata->enable) {
  106. err = pdata->enable(1, pdata->ls_gpio);
  107. if (err < 0) {
  108. dev_err(&chip->client->dev,
  109. "failed to enable the level shifter\n");
  110. return err;
  111. }
  112. }
  113. ret = cs8427_i2c_write_device(chip, reg, src, bytes);
  114. /*
  115. * Disable the 100KHz level shifter to communicate
  116. * with CS8427 chip
  117. */
  118. if (pdata->enable) {
  119. err = pdata->enable(0, pdata->ls_gpio);
  120. if (err < 0) {
  121. dev_err(&chip->client->dev,
  122. "failed to disable the level shifter\n");
  123. return err;
  124. }
  125. }
  126. return ret;
  127. }
  128. static int cs8427_i2c_read_device(struct cs8427 *cs8427_i2c,
  129. unsigned short reg,
  130. int bytes, unsigned char *dest)
  131. {
  132. struct i2c_msg *msg;
  133. int ret = 0;
  134. u8 reg_addr = 0;
  135. u8 i = 0;
  136. if (cs8427_i2c->client == NULL) {
  137. pr_err("%s: failed to get device info\n", __func__);
  138. return -ENODEV;
  139. }
  140. for (i = 0; i < bytes; i++) {
  141. reg_addr = (u8)reg++;
  142. msg = &cs8427_i2c->xfer_msg[0];
  143. msg->addr = cs8427_i2c->client->addr;
  144. msg->len = 1;
  145. msg->flags = 0;
  146. msg->buf = &reg_addr;
  147. msg = &cs8427_i2c->xfer_msg[1];
  148. msg->addr = cs8427_i2c->client->addr;
  149. msg->len = 1;
  150. msg->flags = I2C_M_RD;
  151. msg->buf = dest++;
  152. ret = i2c_transfer(cs8427_i2c->client->adapter,
  153. cs8427_i2c->xfer_msg, 2);
  154. /* Try again if read fails first time
  155. checking with ebusy and number of bytes executed
  156. for read ret value should be 2*/
  157. if ((ret != 2) || (ret == -EBUSY)) {
  158. ret = i2c_transfer(
  159. cs8427_i2c->client->adapter,
  160. cs8427_i2c->xfer_msg, 2);
  161. if ((ret != 2) || (ret < 0)) {
  162. dev_err(&cs8427_i2c->client->dev,
  163. "failed to read cs8427"
  164. " register %d\n", reg);
  165. return ret;
  166. }
  167. }
  168. }
  169. return 0;
  170. }
  171. static int cs8427_i2c_read(struct cs8427 *chip,
  172. unsigned short reg,
  173. int bytes, void *dest)
  174. {
  175. u32 err = 0, ret = 0;
  176. struct cs8427_platform_data *pdata = chip->client->dev.platform_data;
  177. /*
  178. * enable the 100KHz level shifter to communicate
  179. * with CS8427 chip
  180. */
  181. if (pdata->enable) {
  182. err = pdata->enable(1, pdata->ls_gpio);
  183. if (err < 0) {
  184. dev_err(&chip->client->dev,
  185. "failed to enable the level shifter\n");
  186. return err;
  187. }
  188. }
  189. ret = cs8427_i2c_read_device(chip, reg,
  190. bytes, dest);
  191. /*
  192. * Disable the 100KHz level shifter to communicate
  193. * with CS8427 chip
  194. */
  195. if (pdata->enable) {
  196. err = pdata->enable(0, pdata->ls_gpio);
  197. if (err < 0) {
  198. dev_err(&chip->client->dev,
  199. "failed to disable the level shifter\n");
  200. return err;
  201. }
  202. }
  203. return ret;
  204. }
  205. static int cs8427_i2c_sendbytes(struct cs8427 *chip,
  206. char *reg_addr, char *data,
  207. int bytes)
  208. {
  209. u32 ret = 0, err = 0;
  210. u8 i = 0;
  211. struct cs8427_platform_data *pdata = chip->client->dev.platform_data;
  212. if (!chip) {
  213. pr_err("%s, invalid device info\n", __func__);
  214. return -ENODEV;
  215. }
  216. if (!data) {
  217. dev_err(&chip->client->dev, "%s:"
  218. "invalid data pointer\n", __func__);
  219. return -EINVAL;
  220. }
  221. /*
  222. * enable the 100KHz level shifter to communicate
  223. * with CS8427 chip
  224. */
  225. if (pdata->enable) {
  226. err = pdata->enable(1, pdata->ls_gpio);
  227. if (err < 0) {
  228. dev_err(&chip->client->dev,
  229. "failed to enable the level shifter\n");
  230. return err;
  231. }
  232. }
  233. for (i = 0; i < bytes; i++) {
  234. ret = cs8427_i2c_write_device(chip, (*reg_addr + i),
  235. &data[i], 1);
  236. if (ret < 0) {
  237. dev_err(&chip->client->dev,
  238. "%s: failed to send the data to"
  239. " cs8427 chip\n", __func__);
  240. break;
  241. }
  242. }
  243. /*
  244. * Disable the 100KHz level shifter to communicate
  245. * with CS8427 chip
  246. */
  247. if (pdata->enable) {
  248. err = pdata->enable(0, pdata->ls_gpio);
  249. if (err < 0) {
  250. dev_err(&chip->client->dev,
  251. "failed to disable the level shifter\n");
  252. return err;
  253. }
  254. }
  255. return i;
  256. }
  257. /*
  258. * Reset the chip using run bit, also lock PLL using ILRCK and
  259. * put back AES3INPUT. This workaround is described in latest
  260. * CS8427 datasheet, otherwise TXDSERIAL will not work.
  261. */
  262. static void snd_cs8427_reset(struct cs8427 *chip)
  263. {
  264. unsigned long end_time;
  265. int data, aes3input = 0;
  266. unsigned char val = 0;
  267. if (snd_BUG_ON(!chip))
  268. return;
  269. if ((chip->regmap[CS8427_REG_CLOCKSOURCE] & CS8427_RXDAES3INPUT) ==
  270. CS8427_RXDAES3INPUT) /* AES3 bit is set */
  271. aes3input = 1;
  272. chip->regmap[CS8427_REG_CLOCKSOURCE] &= ~(CS8427_RUN | CS8427_RXDMASK);
  273. cs8427_i2c_write(chip, CS8427_REG_CLOCKSOURCE,
  274. 1, &chip->regmap[CS8427_REG_CLOCKSOURCE]);
  275. udelay(200);
  276. chip->regmap[CS8427_REG_CLOCKSOURCE] |= CS8427_RUN | CS8427_RXDILRCK;
  277. cs8427_i2c_write(chip, CS8427_REG_CLOCKSOURCE,
  278. 1, &chip->regmap[CS8427_REG_CLOCKSOURCE]);
  279. udelay(200);
  280. end_time = jiffies + chip->reset_timeout;
  281. while (time_after_eq(end_time, jiffies)) {
  282. data = cs8427_i2c_read(chip, CS8427_REG_RECVERRORS,
  283. 1, &val);
  284. if (!(val & CS8427_UNLOCK))
  285. break;
  286. schedule_timeout_uninterruptible(1);
  287. }
  288. chip->regmap[CS8427_REG_CLOCKSOURCE] &= ~CS8427_RXDMASK;
  289. if (aes3input)
  290. chip->regmap[CS8427_REG_CLOCKSOURCE] |= CS8427_RXDAES3INPUT;
  291. cs8427_i2c_write(chip, CS8427_REG_CLOCKSOURCE,
  292. 1, &chip->regmap[CS8427_REG_CLOCKSOURCE]);
  293. }
  294. static int snd_cs8427_in_status_info(struct snd_kcontrol *kcontrol,
  295. struct snd_ctl_elem_info *uinfo)
  296. {
  297. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  298. uinfo->count = 1;
  299. uinfo->value.integer.min = 0;
  300. uinfo->value.integer.max = 255;
  301. return 0;
  302. }
  303. static int snd_cs8427_in_status_get(struct snd_kcontrol *kcontrol,
  304. struct snd_ctl_elem_value *ucontrol)
  305. {
  306. struct cs8427 *chip = kcontrol->private_data;
  307. unsigned char val = 0;
  308. int err = 0;
  309. err = cs8427_i2c_read(chip, kcontrol->private_value, 1, &val);
  310. if (err < 0)
  311. return err;
  312. ucontrol->value.integer.value[0] = val;
  313. return 0;
  314. }
  315. static int snd_cs8427_qsubcode_info(struct snd_kcontrol *kcontrol,
  316. struct snd_ctl_elem_info *uinfo)
  317. {
  318. uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
  319. uinfo->count = 10;
  320. return 0;
  321. }
  322. static int snd_cs8427_qsubcode_get(struct snd_kcontrol *kcontrol,
  323. struct snd_ctl_elem_value *ucontrol)
  324. {
  325. struct cs8427 *chip = kcontrol->private_data;
  326. unsigned char reg = CS8427_REG_QSUBCODE;
  327. int err;
  328. unsigned char val[20];
  329. if (!chip) {
  330. pr_err("%s: invalid device info\n", __func__);
  331. return -ENODEV;
  332. }
  333. err = cs8427_i2c_write(chip, reg, 1, &val[0]);
  334. if (err != 1) {
  335. dev_err(&chip->client->dev, "unable to send register"
  336. " 0x%x byte to CS8427\n", reg);
  337. return err < 0 ? err : -EIO;
  338. }
  339. err = cs8427_i2c_read(chip, *ucontrol->value.bytes.data, 10, &val);
  340. if (err != 10) {
  341. dev_err(&chip->client->dev, "unable to read"
  342. " Q-subcode bytes from CS8427\n");
  343. return err < 0 ? err : -EIO;
  344. }
  345. return 0;
  346. }
  347. static int snd_cs8427_spdif_info(struct snd_kcontrol *kcontrol,
  348. struct snd_ctl_elem_info *uinfo)
  349. {
  350. uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
  351. uinfo->count = 1;
  352. return 0;
  353. }
  354. static int snd_cs8427_select_corudata(struct cs8427 *cs8427_i2c, int udata)
  355. {
  356. struct cs8427 *chip = cs8427_i2c;
  357. int err;
  358. udata = udata ? CS8427_BSEL : 0;
  359. if (udata != (chip->regmap[CS8427_REG_CSDATABUF] & udata)) {
  360. chip->regmap[CS8427_REG_CSDATABUF] &= ~CS8427_BSEL;
  361. chip->regmap[CS8427_REG_CSDATABUF] |= udata;
  362. err = cs8427_i2c_write(cs8427_i2c, CS8427_REG_CSDATABUF,
  363. 1, &chip->regmap[CS8427_REG_CSDATABUF]);
  364. if (err < 0)
  365. return err;
  366. }
  367. return 0;
  368. }
  369. static int snd_cs8427_send_corudata(struct cs8427 *obj,
  370. int udata,
  371. unsigned char *ndata,
  372. int count)
  373. {
  374. struct cs8427 *chip = obj;
  375. char *hw_data = udata ?
  376. chip->playback.hw_udata : chip->playback.hw_status;
  377. char data[32];
  378. int err, idx;
  379. unsigned char addr = 0;
  380. int ret = 0;
  381. if (!memcmp(hw_data, ndata, count))
  382. return 0;
  383. err = snd_cs8427_select_corudata(chip, udata);
  384. if (err < 0)
  385. return err;
  386. memcpy(hw_data, ndata, count);
  387. if (udata) {
  388. memset(data, 0, sizeof(data));
  389. if (memcmp(hw_data, data, count) == 0) {
  390. chip->regmap[CS8427_REG_UDATABUF] &= ~CS8427_UBMMASK;
  391. chip->regmap[CS8427_REG_UDATABUF] |= CS8427_UBMZEROS |
  392. CS8427_EFTUI;
  393. err = cs8427_i2c_write(chip, CS8427_REG_UDATABUF,
  394. 1, &chip->regmap[CS8427_REG_UDATABUF]);
  395. return err < 0 ? err : 0;
  396. }
  397. }
  398. idx = 0;
  399. memcpy(data, ndata, CHANNEL_STATUS_SIZE);
  400. /* address from where the bufferhas to write*/
  401. addr = 0x20;
  402. ret = cs8427_i2c_sendbytes(chip, &addr, data, count);
  403. if (ret != count)
  404. return -EIO;
  405. return 1;
  406. }
  407. static int snd_cs8427_spdif_get(struct snd_kcontrol *kcontrol,
  408. struct snd_ctl_elem_value *ucontrol)
  409. {
  410. struct cs8427 *chip = kcontrol->private_data;
  411. if (!chip) {
  412. pr_err("%s: invalid device info\n", __func__);
  413. return -ENODEV;
  414. }
  415. memcpy(ucontrol->value.iec958.status,
  416. chip->playback.def_status, CHANNEL_STATUS_SIZE);
  417. return 0;
  418. }
  419. static int snd_cs8427_spdif_put(struct snd_kcontrol *kcontrol,
  420. struct snd_ctl_elem_value *ucontrol)
  421. {
  422. struct cs8427 *chip = kcontrol->private_data;
  423. unsigned char *status;
  424. int err, change;
  425. if (!chip) {
  426. pr_err("%s: invalid device info\n", __func__);
  427. return -ENODEV;
  428. }
  429. status = kcontrol->private_value ?
  430. chip->playback.pcm_status : chip->playback.def_status;
  431. change = memcmp(ucontrol->value.iec958.status, status,
  432. CHANNEL_STATUS_SIZE) != 0;
  433. if (!change) {
  434. memcpy(status, ucontrol->value.iec958.status,
  435. CHANNEL_STATUS_SIZE);
  436. err = snd_cs8427_send_corudata(chip, 0, status,
  437. CHANNEL_STATUS_SIZE);
  438. if (err < 0)
  439. change = err;
  440. }
  441. return change;
  442. }
  443. static int snd_cs8427_spdif_mask_info(struct snd_kcontrol *kcontrol,
  444. struct snd_ctl_elem_info *uinfo)
  445. {
  446. uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
  447. uinfo->count = 1;
  448. return 0;
  449. }
  450. static int snd_cs8427_spdif_mask_get(struct snd_kcontrol *kcontrol,
  451. struct snd_ctl_elem_value *ucontrol)
  452. {
  453. memset(ucontrol->value.iec958.status, 0xff, CHANNEL_STATUS_SIZE);
  454. return 0;
  455. }
  456. static struct snd_kcontrol_new snd_cs8427_iec958_controls[] = {
  457. {
  458. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  459. .info = snd_cs8427_in_status_info,
  460. .name = "IEC958 CS8427 Input Status",
  461. .access = (SNDRV_CTL_ELEM_ACCESS_READ |
  462. SNDRV_CTL_ELEM_ACCESS_VOLATILE),
  463. .get = snd_cs8427_in_status_get,
  464. .private_value = 15,
  465. },
  466. {
  467. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  468. .info = snd_cs8427_in_status_info,
  469. .name = "IEC958 CS8427 Error Status",
  470. .access = (SNDRV_CTL_ELEM_ACCESS_READ |
  471. SNDRV_CTL_ELEM_ACCESS_VOLATILE),
  472. .get = snd_cs8427_in_status_get,
  473. .private_value = 16,
  474. },
  475. {
  476. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  477. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  478. .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, MASK),
  479. .info = snd_cs8427_spdif_mask_info,
  480. .get = snd_cs8427_spdif_mask_get,
  481. },
  482. {
  483. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  484. .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK,
  485. DEFAULT),
  486. .info = snd_cs8427_spdif_info,
  487. .get = snd_cs8427_spdif_get,
  488. .put = snd_cs8427_spdif_put,
  489. .private_value = 0
  490. },
  491. {
  492. .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
  493. SNDRV_CTL_ELEM_ACCESS_INACTIVE),
  494. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  495. .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PCM_STREAM),
  496. .info = snd_cs8427_spdif_info,
  497. .get = snd_cs8427_spdif_get,
  498. .put = snd_cs8427_spdif_put,
  499. .private_value = 1
  500. },
  501. {
  502. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  503. .info = snd_cs8427_qsubcode_info,
  504. .name = "IEC958 Q-subcode Capture Default",
  505. .access = (SNDRV_CTL_ELEM_ACCESS_READ |
  506. SNDRV_CTL_ELEM_ACCESS_VOLATILE),
  507. .get = snd_cs8427_qsubcode_get
  508. }
  509. };
  510. static int cs8427_hw_params(struct snd_pcm_substream *substream,
  511. struct snd_pcm_hw_params *params,
  512. struct snd_soc_dai *dai)
  513. {
  514. struct snd_soc_codec *codec = dai->codec;
  515. struct cs8427 *chip = dev_get_drvdata(codec->dev);
  516. int ret = 0;
  517. if (chip == NULL) {
  518. pr_err("invalid device private data\n");
  519. return -ENODEV;
  520. }
  521. chip->regmap[CS8427_REG_SERIALINPUT] &= CS8427_BITWIDTH_MASK;
  522. switch (params_format(params)) {
  523. case SNDRV_PCM_FORMAT_S16_LE:
  524. chip->regmap[CS8427_REG_SERIALINPUT] |= CS8427_SIRES16;
  525. ret = cs8427_i2c_write(chip, CS8427_REG_SERIALINPUT, 1,
  526. &chip->regmap[CS8427_REG_SERIALINPUT]);
  527. break;
  528. case SNDRV_PCM_FORMAT_S20_3LE:
  529. chip->regmap[CS8427_REG_SERIALINPUT] |= CS8427_SIRES20;
  530. ret = cs8427_i2c_write(chip, CS8427_REG_SERIALINPUT, 1,
  531. &chip->regmap[CS8427_REG_SERIALINPUT]);
  532. break;
  533. case SNDRV_PCM_FORMAT_S24_LE:
  534. chip->regmap[CS8427_REG_SERIALINPUT] |= CS8427_SIRES24;
  535. ret = cs8427_i2c_write(chip, CS8427_REG_SERIALINPUT, 1,
  536. &chip->regmap[CS8427_REG_SERIALINPUT]);
  537. break;
  538. default:
  539. pr_err("invalid format\n");
  540. break;
  541. }
  542. dev_dbg(&chip->client->dev,
  543. "%s(): substream = %s stream = %d\n" , __func__,
  544. substream->name, substream->stream);
  545. return ret;
  546. }
  547. static int snd_cs8427_iec958_register_kcontrol(struct cs8427 *cs8427,
  548. struct snd_card *card)
  549. {
  550. struct cs8427 *chip = cs8427;
  551. struct snd_kcontrol *kctl;
  552. unsigned int idx;
  553. int err;
  554. for (idx = 0; idx < ARRAY_SIZE(snd_cs8427_iec958_controls); idx++) {
  555. kctl = snd_ctl_new1(&snd_cs8427_iec958_controls[idx], chip);
  556. if (kctl == NULL)
  557. return -ENOMEM;
  558. err = snd_ctl_add(card, kctl);
  559. if (err < 0) {
  560. dev_err(&chip->client->dev,
  561. "failed to add the kcontrol\n");
  562. return err;
  563. }
  564. }
  565. return err;
  566. }
  567. static int cs8427_startup(struct snd_pcm_substream *substream,
  568. struct snd_soc_dai *dai)
  569. {
  570. struct cs8427 *chip = dev_get_drvdata(dai->codec->dev);
  571. if (chip == NULL) {
  572. pr_err("invalid device private data\n");
  573. return -ENODEV;
  574. }
  575. /*
  576. * we need to make the pll lock for the I2S tranfers
  577. * reset the cs8427 chip for this.
  578. */
  579. snd_cs8427_reset(chip);
  580. dev_dbg(&chip->client->dev,
  581. "%s(): substream = %s stream = %d\n" , __func__,
  582. substream->name, substream->stream);
  583. return 0;
  584. }
  585. static void cs8427_shutdown(struct snd_pcm_substream *substream,
  586. struct snd_soc_dai *dai)
  587. {
  588. struct cs8427 *chip = dev_get_drvdata(dai->codec->dev);
  589. if (chip == NULL) {
  590. pr_err("invalid device private data\n");
  591. return;
  592. }
  593. dev_dbg(&chip->client->dev,
  594. "%s(): substream = %s stream = %d\n" , __func__,
  595. substream->name, substream->stream);
  596. }
  597. static int cs8427_set_dai_fmt(struct snd_soc_dai *dai, unsigned int fmt)
  598. {
  599. struct cs8427 *chip = dev_get_drvdata(dai->codec->dev);
  600. if (chip == NULL) {
  601. pr_err("invalid device private data\n");
  602. return -ENODEV;
  603. }
  604. dev_dbg(&chip->client->dev, "%s\n", __func__);
  605. return 0;
  606. }
  607. static struct snd_soc_dai_ops cs8427_dai_ops = {
  608. .startup = cs8427_startup,
  609. .shutdown = cs8427_shutdown,
  610. .hw_params = cs8427_hw_params,
  611. .set_fmt = cs8427_set_dai_fmt,
  612. };
  613. static struct snd_soc_dai_driver cs8427_dai[] = {
  614. {
  615. .name = "spdif_rx",
  616. .id = 1,
  617. .playback = {
  618. .stream_name = "AIF1 Playback",
  619. .rates = CS8427_RATES,
  620. .formats = CS8427_FORMATS,
  621. .rate_max = 192000,
  622. .rate_min = 8000,
  623. .channels_min = 1,
  624. .channels_max = 2,
  625. },
  626. .ops = &cs8427_dai_ops,
  627. },
  628. };
  629. static unsigned int cs8427_soc_i2c_read(struct snd_soc_codec *codec,
  630. unsigned int reg)
  631. {
  632. struct cs8427 *chip = dev_get_drvdata(codec->dev);
  633. if (chip == NULL) {
  634. pr_err("invalid device private data\n");
  635. return -ENODEV;
  636. }
  637. dev_dbg(&chip->client->dev, "cs8427 soc i2c read\n");
  638. return 0;
  639. }
  640. static int cs8427_soc_i2c_write(struct snd_soc_codec *codec,
  641. unsigned int reg, unsigned int value)
  642. {
  643. struct cs8427 *chip = dev_get_drvdata(codec->dev);
  644. if (chip == NULL) {
  645. pr_err("invalid device private data\n");
  646. return -ENODEV;
  647. }
  648. dev_dbg(&chip->client->dev, "cs8427 soc i2c write\n");
  649. return 0;
  650. }
  651. static int cs8427_soc_probe(struct snd_soc_codec *codec)
  652. {
  653. int ret = 0;
  654. struct cs8427 *chip;
  655. codec->control_data = dev_get_drvdata(codec->dev);
  656. chip = codec->control_data;
  657. if (chip == NULL) {
  658. pr_err("invalid device private data\n");
  659. return -ENODEV;
  660. }
  661. snd_cs8427_iec958_register_kcontrol(chip, codec->card->snd_card);
  662. dev_set_drvdata(codec->dev, chip);
  663. return ret;
  664. }
  665. static struct snd_soc_codec_driver soc_codec_dev_cs8427 = {
  666. .read = cs8427_soc_i2c_read,
  667. .write = cs8427_soc_i2c_write,
  668. .probe = cs8427_soc_probe,
  669. };
  670. int poweron_cs8427(struct cs8427 *chip)
  671. {
  672. struct cs8427_platform_data *pdata = chip->client->dev.platform_data;
  673. int ret = 0;
  674. if (pdata->enable) {
  675. ret = gpio_request(pdata->ls_gpio, "cs8427 ls");
  676. if (ret < 0) {
  677. dev_err(&chip->client->dev,
  678. "failed to request the gpio %d\n",
  679. pdata->reset_gpio);
  680. return ret;
  681. }
  682. }
  683. ret = gpio_request(pdata->reset_gpio, "cs8427 reset");
  684. if (ret < 0) {
  685. dev_err(&chip->client->dev,
  686. "failed to request the gpio %d\n",
  687. pdata->reset_gpio);
  688. return ret;
  689. }
  690. /*bring the chip out of reset*/
  691. gpio_direction_output(pdata->reset_gpio, 1);
  692. msleep(20);
  693. gpio_direction_output(pdata->reset_gpio, 0);
  694. msleep(20);
  695. gpio_direction_output(pdata->reset_gpio, 1);
  696. msleep(20);
  697. return ret;
  698. }
  699. static __devinit int cs8427_i2c_probe(struct i2c_client *client,
  700. const struct i2c_device_id *id)
  701. {
  702. static unsigned char initvals1[] = {
  703. CS8427_REG_CONTROL1 | CS8427_REG_AUTOINC,
  704. /* CS8427_REG_CONTROL1: RMCK to OMCK, valid PCM audio, disable mutes,
  705. * TCBL=output
  706. */
  707. CS8427_SWCLK | CS8427_TCBLDIR,
  708. /* CS8427_REG_CONTROL2: hold last valid audio sample, RMCK=256*Fs,
  709. * normal stereo operation
  710. */
  711. 0x08,
  712. /* CS8427_REG_DATAFLOW:
  713. * AES3 Transmitter data source => Serial Audio input port
  714. * Serial audio output port data source => reserved
  715. */
  716. CS8427_TXDSERIAL,
  717. /* CS8427_REG_CLOCKSOURCE: Run off, CMCK=256*Fs,
  718. * output time base = OMCK, input time base = recovered input clock,
  719. * recovered input clock source is ILRCK changed to AES3INPUT
  720. * (workaround, see snd_cs8427_reset)
  721. */
  722. CS8427_RXDILRCK | CS8427_OUTC,
  723. /* CS8427_REG_SERIALINPUT: Serial audio input port data format = I2S,
  724. * 24-bit, 64*Fsi
  725. */
  726. CS8427_SIDEL | CS8427_SILRPOL | CS8427_SORES16,
  727. /* CS8427_REG_SERIALOUTPUT: Serial audio output port data format
  728. * = I2S, 24-bit, 64*Fsi
  729. */
  730. CS8427_SODEL | CS8427_SOLRPOL | CS8427_SIRES16,
  731. };
  732. static unsigned char initvals2[] = {
  733. CS8427_REG_RECVERRMASK | CS8427_REG_AUTOINC,
  734. /* CS8427_REG_RECVERRMASK: unmask the input PLL clock, V, confidence,
  735. * biphase, parity status bits
  736. * CS8427_UNLOCK | CS8427_V | CS8427_CONF | CS8427_BIP | CS8427_PAR,
  737. */
  738. 0xff, /* set everything */
  739. /* CS8427_REG_CSDATABUF:
  740. * Registers 32-55 window to CS buffer
  741. * Inhibit D->E transfers from overwriting first 5 bytes of CS data.
  742. * Inhibit D->E transfers (all) of CS data.
  743. * Allow E->F transfer of CS data.
  744. * One byte mode; both A/B channels get same written CB data.
  745. * A channel info is output to chip's EMPH* pin.
  746. */
  747. CS8427_CBMR | CS8427_DETCI,
  748. /* CS8427_REG_UDATABUF:
  749. * Use internal buffer to transmit User (U) data.
  750. * Chip's U pin is an output.
  751. * Transmit all O's for user data.
  752. * Inhibit D->E transfers.
  753. * Inhibit E->F transfers.
  754. */
  755. CS8427_UD | CS8427_EFTUI | CS8427_DETUI,
  756. };
  757. int err;
  758. unsigned char buf[CHANNEL_STATUS_SIZE];
  759. unsigned char val = 0;
  760. char addr = 0;
  761. unsigned int reset_timeout = 1;
  762. int ret = 0;
  763. struct cs8427 *chip;
  764. if (!client) {
  765. pr_err("%s: invalid device info\n", __func__);
  766. return -EINVAL;
  767. }
  768. chip = kzalloc(sizeof(struct cs8427), GFP_KERNEL);
  769. if (chip == NULL) {
  770. dev_err(&client->dev,
  771. "%s: error, allocation failed\n", __func__);
  772. return -ENOMEM;
  773. }
  774. chip->client = client;
  775. dev_set_drvdata(&chip->client->dev, chip);
  776. ret = poweron_cs8427(chip);
  777. if (ret) {
  778. dev_err(&chip->client->dev,
  779. "failed to bring chip out of reset\n");
  780. return -ENODEV;
  781. }
  782. err = cs8427_i2c_read(chip, CS8427_REG_ID_AND_VER, 1, &val);
  783. if (err < 0) {
  784. /* give second chance */
  785. dev_err(&chip->client->dev,
  786. "failed to read cs8427 trying once again\n");
  787. err = cs8427_i2c_read(chip, CS8427_REG_ID_AND_VER,
  788. 1, &val);
  789. if (err < 0) {
  790. dev_err(&chip->client->dev,
  791. "failed to read version number\n");
  792. return -ENODEV;
  793. }
  794. dev_dbg(&chip->client->dev,
  795. "version number read = %x\n", val);
  796. }
  797. if (val != CS8427_VER8427A) {
  798. dev_err(&chip->client->dev,
  799. "unable to find CS8427 signature "
  800. "(expected 0x%x, read 0x%x),\n",
  801. CS8427_VER8427A, val);
  802. dev_err(&chip->client->dev,
  803. " initialization is not completed\n");
  804. return -EFAULT;
  805. }
  806. val = 0;
  807. /* turn off run bit while making changes to configuration */
  808. err = cs8427_i2c_write(chip, CS8427_REG_CLOCKSOURCE, 1, &val);
  809. if (err < 0)
  810. goto __fail;
  811. /* send initial values */
  812. memcpy(chip->regmap + (initvals1[0] & 0x7f), initvals1 + 1, 6);
  813. addr = 1;
  814. err = cs8427_i2c_sendbytes(chip, &addr, &initvals1[1], 6);
  815. if (err != 6) {
  816. err = err < 0 ? err : -EIO;
  817. goto __fail;
  818. }
  819. /* Turn off CS8427 interrupt stuff that is not used in hardware */
  820. memset(buf, 0, 7);
  821. /* from address 9 to 15 */
  822. addr = 9;
  823. err = cs8427_i2c_sendbytes(chip, &addr, buf, 7);
  824. if (err != 7)
  825. goto __fail;
  826. /* send transfer initialization sequence */
  827. addr = 0x11;
  828. memcpy(chip->regmap + (initvals2[0] & 0x7f), initvals2 + 1, 3);
  829. err = cs8427_i2c_sendbytes(chip, &addr, &initvals2[1], 3);
  830. if (err != 3) {
  831. err = err < 0 ? err : -EIO;
  832. goto __fail;
  833. }
  834. /* write default channel status bytes */
  835. put_unaligned_le32(SNDRV_PCM_DEFAULT_CON_SPDIF, buf);
  836. memset(buf + 4, 0, CHANNEL_STATUS_SIZE - 4);
  837. if (snd_cs8427_send_corudata(chip, 0, buf, CHANNEL_STATUS_SIZE) < 0)
  838. goto __fail;
  839. memcpy(chip->playback.def_status, buf, CHANNEL_STATUS_SIZE);
  840. memcpy(chip->playback.pcm_status, buf, CHANNEL_STATUS_SIZE);
  841. /* turn on run bit and rock'n'roll */
  842. if (reset_timeout < 1)
  843. reset_timeout = 1;
  844. chip->reset_timeout = reset_timeout;
  845. snd_cs8427_reset(chip);
  846. ret = snd_soc_register_codec(&chip->client->dev, &soc_codec_dev_cs8427,
  847. cs8427_dai, ARRAY_SIZE(cs8427_dai));
  848. return 0;
  849. __fail:
  850. kfree(chip);
  851. return err < 0 ? err : -EIO;
  852. }
  853. static int __devexit cs8427_remove(struct i2c_client *client)
  854. {
  855. struct cs8427 *chip;
  856. struct cs8427_platform_data *pdata;
  857. chip = dev_get_drvdata(&client->dev);
  858. if (!chip) {
  859. pr_err("invalid device info\n");
  860. return -ENODEV;
  861. }
  862. pdata = chip->client->dev.platform_data;
  863. gpio_free(pdata->reset_gpio);
  864. if (pdata->enable) {
  865. pdata->enable(0, pdata->ls_gpio);
  866. gpio_free(pdata->ls_gpio);
  867. }
  868. kfree(chip);
  869. return 0;
  870. }
  871. static struct i2c_device_id cs8427_id_table[] = {
  872. {"cs8427", CS8427_ADDR0},
  873. {"cs8427", CS8427_ADDR2},
  874. {"cs8427", CS8427_ADDR3},
  875. {"cs8427", CS8427_ADDR4},
  876. {"cs8427", CS8427_ADDR5},
  877. {"cs8427", CS8427_ADDR6},
  878. {"cs8427", CS8427_ADDR7},
  879. {}
  880. };
  881. MODULE_DEVICE_TABLE(i2c, cs8427_id_table);
  882. static struct i2c_driver cs8427_i2c_driver = {
  883. .driver = {
  884. .owner = THIS_MODULE,
  885. .name = "cs8427-spdif",
  886. },
  887. .id_table = cs8427_id_table,
  888. .probe = cs8427_i2c_probe,
  889. .remove = __devexit_p(cs8427_remove),
  890. };
  891. static int __init cs8427_module_init(void)
  892. {
  893. int ret = 0;
  894. ret = i2c_add_driver(&cs8427_i2c_driver);
  895. if (ret != 0)
  896. pr_err("failed to add the I2C driver\n");
  897. return ret;
  898. }
  899. static void __exit cs8427_module_exit(void)
  900. {
  901. pr_info("module exit\n");
  902. }
  903. module_init(cs8427_module_init)
  904. module_exit(cs8427_module_exit)
  905. MODULE_DESCRIPTION("CS8427 interface driver");
  906. MODULE_LICENSE("GPL v2");