2 * sound/ubicom32/ubi32-cs4350.c
3 * Interface to ubicom32 virtual audio peripheral - using CS4350 DAC
5 * (C) Copyright 2009, Ubicom, Inc.
7 * This file is part of the Ubicom32 Linux Kernel Port.
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.
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.
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/>.
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>
34 #define DRIVER_NAME "snd-ubi32-cs4350"
39 static const struct i2c_device_id snd_ubi32_cs4350_id
[] = {
43 MODULE_DEVICE_TABLE(i2c
, ubicom32audio_id
);
45 static int index
= SNDRV_DEFAULT_IDX1
; /* Index 0-MAX */
48 * The dB scale for the Cirrus Logic cs4350. The output range is from
51 static const DECLARE_TLV_DB_SCALE(snd_ubi32_cs4350_db
, -12750, 50, 0);
53 #define ubi32_cs4350_mute_info snd_ctl_boolean_stereo_info
56 * Private data for cs4350 chip
58 struct ubi32_cs4350_priv
{
60 * The current volume settings
65 * Bitmask of mutes MSB (unused, ..., unused, right_ch, left_ch) LSB
70 * Lock to protect this struct because callbacks are not atomic.
76 * The info for the cs4350. The volume currently has one channel,
77 * and 255 possible settings.
79 static int ubi32_cs4350_volume_info(struct snd_kcontrol
*kcontrol
,
80 struct snd_ctl_elem_info
*uinfo
)
82 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
84 uinfo
->value
.integer
.min
= 0;
85 uinfo
->value
.integer
.max
= 255; // 8 bits in cirrus logic cs4350 volume register
89 static int ubi32_cs4350_volume_get(struct snd_kcontrol
*kcontrol
,
90 struct snd_ctl_elem_value
*ucontrol
)
92 struct ubi32_snd_priv
*ubi32_priv
= snd_kcontrol_chip(kcontrol
);
93 struct ubi32_cs4350_priv
*cs4350_priv
;
96 cs4350_priv
= snd_ubi32_priv_get_drv(ubi32_priv
);
98 spin_lock_irqsave(&cs4350_priv
->lock
, flags
);
100 ucontrol
->value
.integer
.value
[0] = cs4350_priv
->volume
[0];
101 ucontrol
->value
.integer
.value
[1] = cs4350_priv
->volume
[1];
103 spin_unlock_irqrestore(&cs4350_priv
->lock
, flags
);
108 static int ubi32_cs4350_volume_put(struct snd_kcontrol
*kcontrol
,
109 struct snd_ctl_elem_value
*ucontrol
)
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
;
117 uint8_t volume_reg_value_left
, volume_reg_value_right
;
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);
126 snd_printk(KERN_INFO
"Setting volume: writing %d,%d to CS4350 volume registers\n", volume_reg_value_left
, volume_reg_value_right
);
128 spin_lock_irqsave(&cs4350_priv
->lock
, flags
);
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);
135 snd_printk(KERN_ERR
"Failed to set channel A volume on CS4350\n");
138 cs4350_priv
->volume
[0] = ucontrol
->value
.integer
.value
[0];
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);
147 snd_printk(KERN_ERR
"Failed to set channel B volume on CS4350\n");
150 cs4350_priv
->volume
[1] = ucontrol
->value
.integer
.value
[1];
154 spin_unlock_irqrestore(&cs4350_priv
->lock
, flags
);
159 static 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
,
170 static int ubi32_cs4350_mute_get(struct snd_kcontrol
*kcontrol
,
171 struct snd_ctl_elem_value
*ucontrol
)
173 struct ubi32_snd_priv
*ubi32_priv
= snd_kcontrol_chip(kcontrol
);
174 struct ubi32_cs4350_priv
*cs4350_priv
;
177 cs4350_priv
= snd_ubi32_priv_get_drv(ubi32_priv
);
179 spin_lock_irqsave(&cs4350_priv
->lock
, flags
);
181 ucontrol
->value
.integer
.value
[0] = cs4350_priv
->mute
& 1;
182 ucontrol
->value
.integer
.value
[1] = (cs4350_priv
->mute
& (1 << 1)) ? 1 : 0;
184 spin_unlock_irqrestore(&cs4350_priv
->lock
, flags
);
189 static int ubi32_cs4350_mute_put(struct snd_kcontrol
*kcontrol
,
190 struct snd_ctl_elem_value
*ucontrol
)
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
;
203 cs4350_priv
= snd_ubi32_priv_get_drv(ubi32_priv
);
205 spin_lock_irqsave(&cs4350_priv
->lock
, flags
);
207 if ((cs4350_priv
->mute
& 1) != ucontrol
->value
.integer
.value
[0]) {
209 ret
= i2c_master_send(client
, send
, 1);
211 snd_printk(KERN_ERR
"Failed to write to mute register: channel 0\n");
215 ret
= i2c_master_recv(client
, recv
, 1);
217 snd_printk(KERN_ERR
"Failed to read mute register: channel 0\n");
223 if (ucontrol
->value
.integer
.value
[0]) {
224 cs4350_priv
->mute
|= 1;
227 snd_printk(KERN_INFO
"Unmuted channel A\n");
230 cs4350_priv
->mute
&= ~1;
233 snd_printk(KERN_INFO
"Muted channel A\n");
239 ret
= i2c_master_send(client
, send
, 2);
241 snd_printk(KERN_ERR
"Failed to set channel A mute on CS4350\n");
247 if (((cs4350_priv
->mute
& 2) >> 1) != ucontrol
->value
.integer
.value
[1]) {
249 ret
= i2c_master_send(client
, send
, 1);
251 snd_printk(KERN_ERR
"Failed to write to mute register: channel 1\n");
255 ret
= i2c_master_recv(client
, recv
, 1);
257 snd_printk(KERN_ERR
"Failed to read mute register: channel 1\n");
263 if (ucontrol
->value
.integer
.value
[1]) {
264 cs4350_priv
->mute
|= (1 << 1);
267 snd_printk(KERN_INFO
"Unmuted channel B\n");
270 cs4350_priv
->mute
&= ~(1 << 1);
273 snd_printk(KERN_INFO
"Muted channel B\n");
279 ret
= i2c_master_send(client
, send
, 2);
281 snd_printk(KERN_ERR
"Failed to set channel A mute on CS4350\n");
287 spin_unlock_irqrestore(&cs4350_priv
->lock
, flags
);
292 static 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
,
302 * snd_ubi32_cs4350_free
303 * Card private data free function
305 void snd_ubi32_cs4350_free(struct snd_card
*card
)
307 struct ubi32_snd_priv
*ubi32_priv
;
308 struct ubi32_cs4350_priv
*cs4350_priv
;
310 ubi32_priv
= card
->private_data
;
311 cs4350_priv
= snd_ubi32_priv_get_drv(ubi32_priv
);
318 * snd_ubi32_cs4350_dac_init
320 static int snd_ubi32_cs4350_dac_init(struct i2c_client
*client
, const struct i2c_device_id
*id
)
327 * Initialize the CS4350 DAC over the I2C interface
329 snd_printk(KERN_INFO
"Initializing CS4350 DAC\n");
332 * Register 0x01: device/revid
335 ret
= i2c_master_send(client
, send
, 1);
337 snd_printk(KERN_ERR
"Failed 1st attempt to write to CS4350 register 0x01\n");
340 ret
= i2c_master_recv(client
, recv
, 1);
342 snd_printk(KERN_ERR
"Failed initial read of CS4350 registers\n");
345 snd_printk(KERN_INFO
"CS4350 DAC Device/Rev: %08x\n", recv
[0]);
348 * Register 0x02: Mode control
349 * I2S DIF[2:0] = 001, no De-Emphasis, Auto speed mode
353 ret
= i2c_master_send(client
, send
, 2);
355 snd_printk(KERN_ERR
"Failed to set CS4350 to I2S mode\n");
360 * Register 0x05/0x06: Volume control
361 * Channel A volume set to 0 dB
362 * Channel B volume set to 0 dB
366 ret
= i2c_master_send(client
, send
, 2);
368 snd_printk(KERN_ERR
"Failed to set channel A volume on CS4350\n");
374 ret
= i2c_master_send(client
, send
, 2);
376 snd_printk(KERN_ERR
"Failed to set channel A volume on CS4350\n");
381 * Make sure the changes took place, this helps verify we are talking to
385 ret
= i2c_master_send(client
, send
, 1);
387 snd_printk(KERN_ERR
"Failed to initiate readback\n");
391 ret
= i2c_master_recv(client
, recv
, 8);
393 snd_printk(KERN_ERR
"Failed second read of CS4350 registers\n");
397 if ((recv
[1] != 0x10) || (recv
[4] != 0x00) || (recv
[5] != 0x00)) {
398 snd_printk(KERN_ERR
"Failed to initialize CS4350 DAC\n");
402 snd_printk(KERN_INFO
"CS4350 DAC Initialized\n");
410 * snd_ubi32_cs4350_i2c_probe
412 static int snd_ubi32_cs4350_i2c_probe(struct i2c_client
*client
, const struct i2c_device_id
*id
)
414 struct snd_card
*card
;
415 struct ubi32_snd_priv
*ubi32_priv
;
416 struct ubi32_cs4350_priv
*cs4350_priv
;
418 struct platform_device
*pdev
;
420 pdev
= client
->dev
.platform_data
;
426 * Initialize the CS4350 DAC
428 ret
= snd_ubi32_cs4350_dac_init(client
, id
);
431 * Initialization failed. Propagate the error.
437 * Create a snd_card structure
439 card
= snd_card_new(index
, "Ubi32-CS4350", THIS_MODULE
, sizeof(struct ubi32_snd_priv
));
444 card
->private_free
= snd_ubi32_cs4350_free
; /* Not sure if correct */
445 ubi32_priv
= card
->private_data
;
448 * CS4350 DAC has a minimum sample rate of 30khz and an
449 * upper limit of 216khz for it's auto-detect.
451 ubi32_priv
->min_sample_rate
= 30000;
452 ubi32_priv
->max_sample_rate
= 216000;
455 * Initialize the snd_card's private data structure
457 ubi32_priv
->card
= card
;
458 ubi32_priv
->client
= client
;
461 * Create our private data structure
463 cs4350_priv
= kzalloc(sizeof(struct ubi32_cs4350_priv
), GFP_KERNEL
);
468 snd_ubi32_priv_set_drv(ubi32_priv
, cs4350_priv
);
469 spin_lock_init(&cs4350_priv
->lock
);
472 * Initial volume is set to max by probe function
474 cs4350_priv
->volume
[0] = 0xFF;
475 cs4350_priv
->volume
[1] = 0xFF;
478 * The CS4350 starts off unmuted (bit set = not muted)
480 cs4350_priv
->mute
= 3;
483 * Create the new PCM instance
485 err
= snd_ubi32_pcm_probe(ubi32_priv
, pdev
);
488 return err
; /* What is err? Need to include correct file */
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
);
498 snd_card_set_dev(card
, &client
->dev
);
501 * Set up the mixer components
503 err
= snd_ctl_add(card
, snd_ctl_new1(&ubi32_cs4350_volume
, ubi32_priv
));
505 snd_printk(KERN_WARNING
"Failed to add volume mixer control\n");
507 err
= snd_ctl_add(card
, snd_ctl_new1(&ubi32_cs4350_mute
, ubi32_priv
));
509 snd_printk(KERN_WARNING
"Failed to add mute mixer control\n");
513 * Register the sound card
515 if ((err
= snd_card_register(card
)) != 0) {
516 snd_printk(KERN_WARNING
"snd_card_register error\n");
520 * Store card for access from other methods
522 i2c_set_clientdata(client
, card
);
528 * snd_ubi32_cs4350_i2c_remove
530 static int __devexit
snd_ubi32_cs4350_i2c_remove(struct i2c_client
*client
)
532 struct snd_card
*card
;
533 struct ubi32_snd_priv
*ubi32_priv
;
535 card
= i2c_get_clientdata(client
);
537 ubi32_priv
= card
->private_data
;
538 snd_ubi32_pcm_remove(ubi32_priv
);
540 snd_card_free(i2c_get_clientdata(client
));
541 i2c_set_clientdata(client
, NULL
);
547 * I2C driver description
549 static struct i2c_driver snd_ubi32_cs4350_driver
= {
552 .owner
= THIS_MODULE
,
554 .id_table
= snd_ubi32_cs4350_id
,
555 .probe
= snd_ubi32_cs4350_i2c_probe
,
556 .remove
= __devexit_p(snd_ubi32_cs4350_i2c_remove
),
562 static int __init
snd_ubi32_cs4350_init(void)
564 return i2c_add_driver(&snd_ubi32_cs4350_driver
);
566 module_init(snd_ubi32_cs4350_init
);
569 * snd_ubi32_cs4350_exit
571 static void __exit
snd_ubi32_cs4350_exit(void)
573 i2c_del_driver(&snd_ubi32_cs4350_driver
);
575 module_exit(snd_ubi32_cs4350_exit
);
580 MODULE_ALIAS("i2c:" DRIVER_NAME
);
581 MODULE_AUTHOR("Patrick Tjin");
582 MODULE_DESCRIPTION("Driver for Ubicom32 audio devices CS4350");
583 MODULE_LICENSE("GPL");