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