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