Root/target/linux/ubicom32/files/sound/ubicom32/ubi32-cs4350.c

1/*
2 * sound/ubicom32/ubi32-cs4350.c
3 * Interface to ubicom32 virtual audio peripheral - using CS4350 DAC
4 *
5 * (C) Copyright 2009, Ubicom, Inc.
6 *
7 * This file is part of the Ubicom32 Linux Kernel Port.
8 *
9 * The Ubicom32 Linux Kernel Port is free software: you can redistribute
10 * it and/or modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation, either version 2 of the
12 * License, or (at your option) any later version.
13 *
14 * The Ubicom32 Linux Kernel Port is distributed in the hope that it
15 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
16 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
17 * the GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with the Ubicom32 Linux Kernel Port. If not,
21 * see <http://www.gnu.org/licenses/>.
22 */
23
24#include <linux/i2c.h>
25#include <linux/module.h>
26#include <linux/moduleparam.h>
27#include <sound/core.h>
28#include <sound/tlv.h>
29#include <sound/control.h>
30#include <sound/pcm.h>
31#include <sound/initval.h>
32#include "ubi32.h"
33
34#define DRIVER_NAME "snd-ubi32-cs4350"
35
36/*
37 * Module properties
38 */
39static const struct i2c_device_id snd_ubi32_cs4350_id[] = {
40    {"cs4350", 0 },
41    { }
42};
43MODULE_DEVICE_TABLE(i2c, ubicom32audio_id);
44
45static int index = SNDRV_DEFAULT_IDX1; /* Index 0-MAX */
46
47/*
48 * The dB scale for the Cirrus Logic cs4350. The output range is from
49 * -127.5 dB to 0 dB.
50 */
51static const DECLARE_TLV_DB_SCALE(snd_ubi32_cs4350_db, -12750, 50, 0);
52
53#define ubi32_cs4350_mute_info snd_ctl_boolean_stereo_info
54
55/*
56 * Private data for cs4350 chip
57 */
58struct ubi32_cs4350_priv {
59    /*
60     * The current volume settings
61     */
62    uint8_t volume[2];
63
64    /*
65     * Bitmask of mutes MSB (unused, ..., unused, right_ch, left_ch) LSB
66     */
67    uint8_t mute;
68
69    /*
70     * Lock to protect this struct because callbacks are not atomic.
71     */
72    spinlock_t lock;
73};
74
75/*
76 * The info for the cs4350. The volume currently has one channel,
77 * and 255 possible settings.
78 */
79static int ubi32_cs4350_volume_info(struct snd_kcontrol *kcontrol,
80                    struct snd_ctl_elem_info *uinfo)
81{
82    uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
83    uinfo->count = 2;
84    uinfo->value.integer.min = 0;
85    uinfo->value.integer.max = 255; // 8 bits in cirrus logic cs4350 volume register
86    return 0;
87}
88
89static int ubi32_cs4350_volume_get(struct snd_kcontrol *kcontrol,
90                   struct snd_ctl_elem_value *ucontrol)
91{
92    struct ubi32_snd_priv *ubi32_priv = snd_kcontrol_chip(kcontrol);
93    struct ubi32_cs4350_priv *cs4350_priv;
94    unsigned long flags;
95
96    cs4350_priv = snd_ubi32_priv_get_drv(ubi32_priv);
97
98    spin_lock_irqsave(&cs4350_priv->lock, flags);
99
100    ucontrol->value.integer.value[0] = cs4350_priv->volume[0];
101    ucontrol->value.integer.value[1] = cs4350_priv->volume[1];
102
103    spin_unlock_irqrestore(&cs4350_priv->lock, flags);
104
105    return 0;
106}
107
108static int ubi32_cs4350_volume_put(struct snd_kcontrol *kcontrol,
109                   struct snd_ctl_elem_value *ucontrol)
110{
111    struct ubi32_snd_priv *ubi32_priv = snd_kcontrol_chip(kcontrol);
112    struct i2c_client *client = (struct i2c_client *)ubi32_priv->client;
113    struct ubi32_cs4350_priv *cs4350_priv;
114    unsigned long flags;
115    int ret, changed;
116    char send[2];
117    uint8_t volume_reg_value_left, volume_reg_value_right;
118
119    changed = 0;
120
121    cs4350_priv = snd_ubi32_priv_get_drv(ubi32_priv);
122    volume_reg_value_left = 255 - (ucontrol->value.integer.value[0] & 0xFF);
123    volume_reg_value_right = 255 - (ucontrol->value.integer.value[1] & 0xFF);
124
125#if SND_UBI32_DEBUG
126    snd_printk(KERN_INFO "Setting volume: writing %d,%d to CS4350 volume registers\n", volume_reg_value_left, volume_reg_value_right);
127#endif
128    spin_lock_irqsave(&cs4350_priv->lock, flags);
129
130    if (cs4350_priv->volume[0] != ucontrol->value.integer.value[0]) {
131        send[0] = 0x05; // left channel
132        send[1] = volume_reg_value_left;
133        ret = i2c_master_send(client, send, 2);
134        if (ret != 2) {
135            snd_printk(KERN_ERR "Failed to set channel A volume on CS4350\n");
136            return changed;
137        }
138        cs4350_priv->volume[0] = ucontrol->value.integer.value[0];
139        changed = 1;
140    }
141
142    if (cs4350_priv->volume[1] != ucontrol->value.integer.value[1]) {
143        send[0] = 0x06; // right channel
144        send[1] = volume_reg_value_right;
145        ret = i2c_master_send(client, send, 2);
146        if (ret != 2) {
147            snd_printk(KERN_ERR "Failed to set channel B volume on CS4350\n");
148            return changed;
149        }
150        cs4350_priv->volume[1] = ucontrol->value.integer.value[1];
151        changed = 1;
152    }
153
154    spin_unlock_irqrestore(&cs4350_priv->lock, flags);
155
156    return changed;
157}
158
159static struct snd_kcontrol_new ubi32_cs4350_volume __devinitdata = {
160    .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
161    .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
162          SNDRV_CTL_ELEM_ACCESS_TLV_READ,
163    .name = "PCM Playback Volume",
164    .info = ubi32_cs4350_volume_info,
165    .get = ubi32_cs4350_volume_get,
166    .put = ubi32_cs4350_volume_put,
167    .tlv.p = snd_ubi32_cs4350_db,
168};
169
170static int ubi32_cs4350_mute_get(struct snd_kcontrol *kcontrol,
171                 struct snd_ctl_elem_value *ucontrol)
172{
173    struct ubi32_snd_priv *ubi32_priv = snd_kcontrol_chip(kcontrol);
174    struct ubi32_cs4350_priv *cs4350_priv;
175    unsigned long flags;
176
177    cs4350_priv = snd_ubi32_priv_get_drv(ubi32_priv);
178
179    spin_lock_irqsave(&cs4350_priv->lock, flags);
180
181    ucontrol->value.integer.value[0] = cs4350_priv->mute & 1;
182    ucontrol->value.integer.value[1] = (cs4350_priv->mute & (1 << 1)) ? 1 : 0;
183
184    spin_unlock_irqrestore(&cs4350_priv->lock, flags);
185
186    return 0;
187}
188
189static int ubi32_cs4350_mute_put(struct snd_kcontrol *kcontrol,
190                 struct snd_ctl_elem_value *ucontrol)
191{
192    struct ubi32_snd_priv *ubi32_priv = snd_kcontrol_chip(kcontrol);
193    struct i2c_client *client = (struct i2c_client *)ubi32_priv->client;
194    struct ubi32_cs4350_priv *cs4350_priv;
195    unsigned long flags;
196    int ret, changed;
197    char send[2];
198    char recv[1];
199    uint8_t mute;
200
201        changed = 0;
202
203    cs4350_priv = snd_ubi32_priv_get_drv(ubi32_priv);
204
205    spin_lock_irqsave(&cs4350_priv->lock, flags);
206
207    if ((cs4350_priv->mute & 1) != ucontrol->value.integer.value[0]) {
208        send[0] = 0x04;
209        ret = i2c_master_send(client, send, 1);
210        if (ret != 1) {
211            snd_printk(KERN_ERR "Failed to write to mute register: channel 0\n");
212            return changed;
213        }
214
215        ret = i2c_master_recv(client, recv, 1);
216        if (ret != 1) {
217            snd_printk(KERN_ERR "Failed to read mute register: channel 0\n");
218            return changed;
219        }
220
221        mute = recv[0];
222
223        if (ucontrol->value.integer.value[0]) {
224            cs4350_priv->mute |= 1;
225            mute &= ~(1 << 4);
226#if SND_UBI32_DEBUG
227            snd_printk(KERN_INFO "Unmuted channel A\n");
228#endif
229        } else {
230            cs4350_priv->mute &= ~1;
231            mute |= (1 << 4);
232#if SND_UBI32_DEBUG
233            snd_printk(KERN_INFO "Muted channel A\n");
234#endif
235        }
236
237        send[0] = 0x04;
238        send[1] = mute;
239        ret = i2c_master_send(client, send, 2);
240        if (ret != 2) {
241            snd_printk(KERN_ERR "Failed to set channel A mute on CS4350\n");
242            return changed;
243        }
244        changed = 1;
245    }
246
247    if (((cs4350_priv->mute & 2) >> 1) != ucontrol->value.integer.value[1]) {
248        send[0] = 0x04;
249        ret = i2c_master_send(client, send, 1);
250        if (ret != 1) {
251            snd_printk(KERN_ERR "Failed to write to mute register: channel 1\n");
252            return changed;
253        }
254
255        ret = i2c_master_recv(client, recv, 1);
256        if (ret != 1) {
257            snd_printk(KERN_ERR "Failed to read mute register: channel 1\n");
258            return changed;
259        }
260
261        mute = recv[0];
262
263        if (ucontrol->value.integer.value[1]) {
264            cs4350_priv->mute |= (1 << 1);
265            mute &= ~(1 << 3);
266#if SND_UBI32_DEBUG
267            snd_printk(KERN_INFO "Unmuted channel B\n");
268#endif
269        } else {
270            cs4350_priv->mute &= ~(1 << 1);
271            mute |= (1 << 3);
272#if SND_UBI32_DEBUG
273            snd_printk(KERN_INFO "Muted channel B\n");
274#endif
275        }
276
277        send[0] = 0x04;
278        send[1] = mute;
279        ret = i2c_master_send(client, send, 2);
280        if (ret != 2) {
281            snd_printk(KERN_ERR "Failed to set channel A mute on CS4350\n");
282            return changed;
283        }
284        changed = 1;
285    }
286
287    spin_unlock_irqrestore(&cs4350_priv->lock, flags);
288
289    return changed;
290}
291
292static struct snd_kcontrol_new ubi32_cs4350_mute __devinitdata = {
293    .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
294    .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
295    .name = "PCM Playback Switch",
296    .info = ubi32_cs4350_mute_info,
297    .get = ubi32_cs4350_mute_get,
298    .put = ubi32_cs4350_mute_put,
299};
300
301/*
302 * snd_ubi32_cs4350_free
303 * Card private data free function
304 */
305void snd_ubi32_cs4350_free(struct snd_card *card)
306{
307    struct ubi32_snd_priv *ubi32_priv;
308    struct ubi32_cs4350_priv *cs4350_priv;
309
310    ubi32_priv = card->private_data;
311    cs4350_priv = snd_ubi32_priv_get_drv(ubi32_priv);
312    if (cs4350_priv) {
313        kfree(cs4350_priv);
314    }
315}
316
317/*
318 * snd_ubi32_cs4350_dac_init
319 */
320static int snd_ubi32_cs4350_dac_init(struct i2c_client *client, const struct i2c_device_id *id)
321{
322    int ret;
323    char send[2];
324    char recv[8];
325
326    /*
327     * Initialize the CS4350 DAC over the I2C interface
328     */
329    snd_printk(KERN_INFO "Initializing CS4350 DAC\n");
330
331    /*
332     * Register 0x01: device/revid
333     */
334    send[0] = 0x01;
335    ret = i2c_master_send(client, send, 1);
336    if (ret != 1) {
337        snd_printk(KERN_ERR "Failed 1st attempt to write to CS4350 register 0x01\n");
338        goto fail;
339    }
340    ret = i2c_master_recv(client, recv, 1);
341    if (ret != 1) {
342        snd_printk(KERN_ERR "Failed initial read of CS4350 registers\n");
343        goto fail;
344    }
345    snd_printk(KERN_INFO "CS4350 DAC Device/Rev: %08x\n", recv[0]);
346
347    /*
348     * Register 0x02: Mode control
349     * I2S DIF[2:0] = 001, no De-Emphasis, Auto speed mode
350     */
351    send[0] = 0x02;
352    send[1] = 0x10;
353    ret = i2c_master_send(client, send, 2);
354    if (ret != 2) {
355        snd_printk(KERN_ERR "Failed to set CS4350 to I2S mode\n");
356        goto fail;
357    }
358
359    /*
360     * Register 0x05/0x06: Volume control
361     * Channel A volume set to 0 dB
362     * Channel B volume set to 0 dB
363     */
364    send[0] = 0x05;
365    send[1] = 0x00;
366    ret = i2c_master_send(client, send, 2);
367    if (ret != 2) {
368        snd_printk(KERN_ERR "Failed to set channel A volume on CS4350\n");
369        goto fail;
370    }
371
372    send[0] = 0x06;
373    send[1] = 0x00;
374    ret = i2c_master_send(client, send, 2);
375    if (ret != 2) {
376        snd_printk(KERN_ERR "Failed to set channel A volume on CS4350\n");
377        goto fail;
378    }
379
380    /*
381     * Make sure the changes took place, this helps verify we are talking to
382     * the correct chip.
383     */
384    send[0] = 0x81;
385    ret = i2c_master_send(client, send, 1);
386    if (ret != 1) {
387        snd_printk(KERN_ERR "Failed to initiate readback\n");
388        goto fail;
389    }
390
391    ret = i2c_master_recv(client, recv, 8);
392    if (ret != 8) {
393        snd_printk(KERN_ERR "Failed second read of CS4350 registers\n");
394        goto fail;
395    }
396
397    if ((recv[1] != 0x10) || (recv[4] != 0x00) || (recv[5] != 0x00)) {
398        snd_printk(KERN_ERR "Failed to initialize CS4350 DAC\n");
399        goto fail;
400    }
401
402    snd_printk(KERN_INFO "CS4350 DAC Initialized\n");
403    return 0;
404
405fail:
406    return -ENODEV;
407}
408
409/*
410 * snd_ubi32_cs4350_i2c_probe
411 */
412static int snd_ubi32_cs4350_i2c_probe(struct i2c_client *client, const struct i2c_device_id *id)
413{
414    struct snd_card *card;
415    struct ubi32_snd_priv *ubi32_priv;
416    struct ubi32_cs4350_priv *cs4350_priv;
417    int err, ret;
418    struct platform_device *pdev;
419
420    pdev = client->dev.platform_data;
421    if (!pdev) {
422        return -ENODEV;
423    }
424
425    /*
426     * Initialize the CS4350 DAC
427     */
428    ret = snd_ubi32_cs4350_dac_init(client, id);
429    if (ret < 0) {
430        /*
431         * Initialization failed. Propagate the error.
432         */
433        return ret;
434    }
435
436    /*
437     * Create a snd_card structure
438     */
439    card = snd_card_new(index, "Ubi32-CS4350", THIS_MODULE, sizeof(struct ubi32_snd_priv));
440    if (card == NULL) {
441        return -ENOMEM;
442    }
443
444    card->private_free = snd_ubi32_cs4350_free; /* Not sure if correct */
445    ubi32_priv = card->private_data;
446
447    /*
448     * CS4350 DAC has a minimum sample rate of 30khz and an
449     * upper limit of 216khz for it's auto-detect.
450     */
451    ubi32_priv->min_sample_rate = 30000;
452    ubi32_priv->max_sample_rate = 216000;
453
454    /*
455     * Initialize the snd_card's private data structure
456     */
457    ubi32_priv->card = card;
458    ubi32_priv->client = client;
459
460    /*
461     * Create our private data structure
462     */
463    cs4350_priv = kzalloc(sizeof(struct ubi32_cs4350_priv), GFP_KERNEL);
464    if (!cs4350_priv) {
465        snd_card_free(card);
466        return -ENOMEM;
467    }
468    snd_ubi32_priv_set_drv(ubi32_priv, cs4350_priv);
469    spin_lock_init(&cs4350_priv->lock);
470
471    /*
472     * Initial volume is set to max by probe function
473     */
474    cs4350_priv->volume[0] = 0xFF;
475    cs4350_priv->volume[1] = 0xFF;
476
477    /*
478     * The CS4350 starts off unmuted (bit set = not muted)
479     */
480    cs4350_priv->mute = 3;
481
482    /*
483     * Create the new PCM instance
484     */
485    err = snd_ubi32_pcm_probe(ubi32_priv, pdev);
486    if (err < 0) {
487        snd_card_free(card);
488        return err; /* What is err? Need to include correct file */
489    }
490
491    strcpy(card->driver, "Ubi32-CS4350");
492    strcpy(card->shortname, "Ubi32-CS4350");
493    snprintf(card->longname, sizeof(card->longname),
494        "%s at sendirq=%d.%d recvirq=%d.%d regs=%p",
495        card->shortname, ubi32_priv->tx_irq, ubi32_priv->irq_idx,
496        ubi32_priv->rx_irq, ubi32_priv->irq_idx, ubi32_priv->adr);
497
498    snd_card_set_dev(card, &client->dev);
499
500    /*
501     * Set up the mixer components
502     */
503    err = snd_ctl_add(card, snd_ctl_new1(&ubi32_cs4350_volume, ubi32_priv));
504    if (err) {
505        snd_printk(KERN_WARNING "Failed to add volume mixer control\n");
506    }
507    err = snd_ctl_add(card, snd_ctl_new1(&ubi32_cs4350_mute, ubi32_priv));
508    if (err) {
509        snd_printk(KERN_WARNING "Failed to add mute mixer control\n");
510    }
511
512    /*
513     * Register the sound card
514     */
515    if ((err = snd_card_register(card)) != 0) {
516        snd_printk(KERN_WARNING "snd_card_register error\n");
517    }
518
519    /*
520     * Store card for access from other methods
521     */
522    i2c_set_clientdata(client, card);
523
524    return 0;
525}
526
527/*
528 * snd_ubi32_cs4350_i2c_remove
529 */
530static int __devexit snd_ubi32_cs4350_i2c_remove(struct i2c_client *client)
531{
532    struct snd_card *card;
533    struct ubi32_snd_priv *ubi32_priv;
534
535    card = i2c_get_clientdata(client);
536
537    ubi32_priv = card->private_data;
538    snd_ubi32_pcm_remove(ubi32_priv);
539
540    snd_card_free(i2c_get_clientdata(client));
541    i2c_set_clientdata(client, NULL);
542
543    return 0;
544}
545
546/*
547 * I2C driver description
548 */
549static struct i2c_driver snd_ubi32_cs4350_driver = {
550    .driver = {
551        .name = DRIVER_NAME,
552        .owner = THIS_MODULE,
553    },
554    .id_table = snd_ubi32_cs4350_id,
555    .probe = snd_ubi32_cs4350_i2c_probe,
556    .remove = __devexit_p(snd_ubi32_cs4350_i2c_remove),
557};
558
559/*
560 * Driver init
561 */
562static int __init snd_ubi32_cs4350_init(void)
563{
564    return i2c_add_driver(&snd_ubi32_cs4350_driver);
565}
566module_init(snd_ubi32_cs4350_init);
567
568/*
569 * snd_ubi32_cs4350_exit
570 */
571static void __exit snd_ubi32_cs4350_exit(void)
572{
573    i2c_del_driver(&snd_ubi32_cs4350_driver);
574}
575module_exit(snd_ubi32_cs4350_exit);
576
577/*
578 * Module properties
579 */
580MODULE_ALIAS("i2c:" DRIVER_NAME);
581MODULE_AUTHOR("Patrick Tjin");
582MODULE_DESCRIPTION("Driver for Ubicom32 audio devices CS4350");
583MODULE_LICENSE("GPL");
584

Archive Download this file



interactive