aboutsummaryrefslogtreecommitdiffstats
path: root/target/linux/ubicom32/files/sound/ubicom32/ubi32-cs4350.c
blob: 7e6f9acbb3367ef1790928266579fe7d49496894 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
/*
 * sound/ubicom32/ubi32-cs4350.c
 *	Interface to ubicom32 virtual audio peripheral - using CS4350 DAC
 *
 * (C) Copyright 2009, Ubicom, Inc.
 *
 * This file is part of the Ubicom32 Linux Kernel Port.
 *
 * The Ubicom32 Linux Kernel Port is free software: you can redistribute
 * it and/or modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation, either version 2 of the
 * License, or (at your option) any later version.
 *
 * The Ubicom32 Linux Kernel Port is distributed in the hope that it
 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
 * the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with the Ubicom32 Linux Kernel Port.  If not,
 * see <http://www.gnu.org/licenses/>.
 */

#include <linux/i2c.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <sound/core.h>
#include <sound/tlv.h>
#include <sound/control.h>
#include <sound/pcm.h>
#include <sound/initval.h>
#include "ubi32.h"

#define DRIVER_NAME "snd-ubi32-cs4350"

/*
 * Module properties
 */
static const struct i2c_device_id snd_ubi32_cs4350_id[] = {
	{"cs4350", 0 },
	{ }
};
MODULE_DEVICE_TABLE(i2c, ubicom32audio_id);

static int index = SNDRV_DEFAULT_IDX1; /* Index 0-MAX */

/*
 * The dB scale for the Cirrus Logic cs4350.  The output range is from
 * -127.5 dB to 0 dB.
 */
static const DECLARE_TLV_DB_SCALE(snd_ubi32_cs4350_db, -12750, 50, 0);

#define ubi32_cs4350_mute_info	snd_ctl_boolean_stereo_info

/*
 * Private data for cs4350 chip
 */
struct ubi32_cs4350_priv {
	/*
	 * The current volume settings
	 */
	uint8_t volume[2];

	/*
	 * Bitmask of mutes MSB (unused, ..., unused, right_ch, left_ch) LSB
	 */
	uint8_t mute;

	/*
	 * Lock to protect this struct because callbacks are not atomic.
	 */
	spinlock_t lock;
};

/*
 * The info for the cs4350.  The volume currently has one channel,
 * and 255 possible settings.
 */
static int ubi32_cs4350_volume_info(struct snd_kcontrol *kcontrol,
				    struct snd_ctl_elem_info *uinfo)
{
	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
	uinfo->count = 2;
	uinfo->value.integer.min = 0;
	uinfo->value.integer.max = 255; // 8 bits in cirrus logic cs4350 volume register
	return 0;
}

static int ubi32_cs4350_volume_get(struct snd_kcontrol *kcontrol,
				   struct snd_ctl_elem_value *ucontrol)
{
	struct ubi32_snd_priv *ubi32_priv = snd_kcontrol_chip(kcontrol);
	struct ubi32_cs4350_priv *cs4350_priv;
	unsigned long flags;

	cs4350_priv = snd_ubi32_priv_get_drv(ubi32_priv);

	spin_lock_irqsave(&cs4350_priv->lock, flags);

	ucontrol->value.integer.value[0] = cs4350_priv->volume[0];
	ucontrol->value.integer.value[1] = cs4350_priv->volume[1];

	spin_unlock_irqrestore(&cs4350_priv->lock, flags);

	return 0;
}

static int ubi32_cs4350_volume_put(struct snd_kcontrol *kcontrol,
				   struct snd_ctl_elem_value *ucontrol)
{
	struct ubi32_snd_priv *ubi32_priv = snd_kcontrol_chip(kcontrol);
	struct i2c_client *client = (struct i2c_client *)ubi32_priv->client;
	struct ubi32_cs4350_priv *cs4350_priv;
	unsigned long flags;
	int ret, changed;
	char send[2];
	uint8_t volume_reg_value_left, volume_reg_value_right;

	changed = 0;

	cs4350_priv = snd_ubi32_priv_get_drv(ubi32_priv);
	volume_reg_value_left = 255 - (ucontrol->value.integer.value[0] & 0xFF);
	volume_reg_value_right = 255 - (ucontrol->value.integer.value[1] & 0xFF);

#if SND_UBI32_DEBUG
	snd_printk(KERN_INFO "Setting volume: writing %d,%d to CS4350 volume registers\n", volume_reg_value_left, volume_reg_value_right);
#endif
	spin_lock_irqsave(&cs4350_priv->lock, flags);

	if (cs4350_priv->volume[0] != ucontrol->value.integer.value[0]) {
		send[0] = 0x05; // left channel
		send[1] = volume_reg_value_left;
		ret = i2c_master_send(client, send, 2);
		if (ret != 2) {
			snd_printk(KERN_ERR "Failed to set channel A volume on CS4350\n");
			return changed;
		}
		cs4350_priv->volume[0] = ucontrol->value.integer.value[0];
		changed = 1;
	}

	if (cs4350_priv->volume[1] != ucontrol->value.integer.value[1]) {
		send[0] = 0x06; // right channel
		send[1] = volume_reg_value_right;
		ret = i2c_master_send(client, send, 2);
		if (ret != 2) {
			snd_printk(KERN_ERR "Failed to set channel B volume on CS4350\n");
			return changed;
		}
		cs4350_priv->volume[1] = ucontrol->value.integer.value[1];
		changed = 1;
	}

	spin_unlock_irqrestore(&cs4350_priv->lock, flags);

	return changed;
}

static struct snd_kcontrol_new ubi32_cs4350_volume __devinitdata = {
	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
		  SNDRV_CTL_ELEM_ACCESS_TLV_READ,
	.name = "PCM Playback Volume",
	.info = ubi32_cs4350_volume_info,
	.get = ubi32_cs4350_volume_get,
	.put = ubi32_cs4350_volume_put,
	.tlv.p = snd_ubi32_cs4350_db,
};

static int ubi32_cs4350_mute_get(struct snd_kcontrol *kcontrol,
				 struct snd_ctl_elem_value *ucontrol)
{
	struct ubi32_snd_priv *ubi32_priv = snd_kcontrol_chip(kcontrol);
	struct ubi32_cs4350_priv *cs4350_priv;
	unsigned long flags;

	cs4350_priv = snd_ubi32_priv_get_drv(ubi32_priv);

	spin_lock_irqsave(&cs4350_priv->lock, flags);

	ucontrol->value.integer.value[0] = cs4350_priv->mute & 1;
	ucontrol->value.integer.value[1] = (cs4350_priv->mute & (1 << 1)) ? 1 : 0;

	spin_unlock_irqrestore(&cs4350_priv->lock, flags);

	return 0;
}

static int ubi32_cs4350_mute_put(struct snd_kcontrol *kcontrol,
				 struct snd_ctl_elem_value *ucontrol)
{
	struct ubi32_snd_priv *ubi32_priv = snd_kcontrol_chip(kcontrol);
	struct i2c_client *client = (struct i2c_client *)ubi32_priv->client;
	struct ubi32_cs4350_priv *cs4350_priv;
	unsigned long flags;
	int ret, changed;
	char send[2];
	char recv[1];
	uint8_t mute;

        changed = 0;

	cs4350_priv = snd_ubi32_priv_get_drv(ubi32_priv);

	spin_lock_irqsave(&cs4350_priv->lock, flags);

	if ((cs4350_priv->mute & 1) != ucontrol->value.integer.value[0]) {
		send[0] = 0x04;
		ret = i2c_master_send(client, send, 1);
		if (ret != 1) {
			snd_printk(KERN_ERR "Failed to write to mute register: channel 0\n");
			return changed;
		}

		ret = i2c_master_recv(client, recv, 1);
		if (ret != 1) {
			snd_printk(KERN_ERR "Failed to read mute register: channel 0\n");
			return changed;
		}

		mute = recv[0];

		if (ucontrol->value.integer.value[0]) {
			cs4350_priv->mute |= 1;
			mute &= ~(1 << 4);
#if SND_UBI32_DEBUG
			snd_printk(KERN_INFO "Unmuted channel A\n");
#endif
		} else {
			cs4350_priv->mute &= ~1;
			mute |= (1 << 4);
#if SND_UBI32_DEBUG
			snd_printk(KERN_INFO "Muted channel A\n");
#endif
		}

		send[0] = 0x04;
		send[1] = mute;
		ret = i2c_master_send(client, send, 2);
		if (ret != 2) {
			snd_printk(KERN_ERR "Failed to set channel A mute on CS4350\n");
			return changed;
		}
		changed = 1;
	}

	if (((cs4350_priv->mute & 2) >> 1) != ucontrol->value.integer.value[1]) {
		send[0] = 0x04;
		ret = i2c_master_send(client, send, 1);
		if (ret != 1) {
			snd_printk(KERN_ERR "Failed to write to mute register: channel 1\n");
			return changed;
		}

		ret = i2c_master_recv(client, recv, 1);
		if (ret != 1) {
			snd_printk(KERN_ERR "Failed to read mute register: channel 1\n");
			return changed;
		}

		mute = recv[0];

		if (ucontrol->value.integer.value[1]) {
			cs4350_priv->mute |= (1 << 1);
			mute &= ~(1 << 3);
#if SND_UBI32_DEBUG
			snd_printk(KERN_INFO "Unmuted channel B\n");
#endif
		} else {
			cs4350_priv->mute &= ~(1 << 1);
			mute |= (1 << 3);
#if SND_UBI32_DEBUG
			snd_printk(KERN_INFO "Muted channel B\n");
#endif
		}

		send[0] = 0x04;
		send[1] = mute;
		ret = i2c_master_send(client, send, 2);
		if (ret != 2) {
			snd_printk(KERN_ERR "Failed to set channel A mute on CS4350\n");
			return changed;
		}
		changed = 1;
	}

	spin_unlock_irqrestore(&cs4350_priv->lock, flags);

	return changed;
}

static struct snd_kcontrol_new ubi32_cs4350_mute __devinitdata = {
	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
	.name = "PCM Playback Switch",
	.info = ubi32_cs4350_mute_info,
	.get = ubi32_cs4350_mute_get,
	.put = ubi32_cs4350_mute_put,
};

/*
 * snd_ubi32_cs4350_free
 *	Card private data free function
 */
void snd_ubi32_cs4350_free(struct snd_card *card)
{
	struct ubi32_snd_priv *ubi32_priv;
	struct ubi32_cs4350_priv *cs4350_priv;

	ubi32_priv = card->private_data;
	cs4350_priv = snd_ubi32_priv_get_drv(ubi32_priv);
	if (cs4350_priv) {
		kfree(cs4350_priv);
	}
}

/*
 * snd_ubi32_cs4350_dac_init
 */
static int snd_ubi32_cs4350_dac_init(struct i2c_client *client, const struct i2c_device_id *id)
{
	int ret;
	char send[2];
	char recv[8];

	/*
	 * Initialize the CS4350 DAC over the I2C interface
	 */
	snd_printk(KERN_INFO "Initializing CS4350 DAC\n");

	/*
	 * Register 0x01: device/revid
	 */
	send[0] = 0x01;
	ret = i2c_master_send(client, send, 1);
	if (ret != 1) {
		snd_printk(KERN_ERR "Failed 1st attempt to write to CS4350 register 0x01\n");
		goto fail;
	}
	ret = i2c_master_recv(client, recv, 1);
	if (ret != 1) {
		snd_printk(KERN_ERR "Failed initial read of CS4350 registers\n");
		goto fail;
	}
	snd_printk(KERN_INFO "CS4350 DAC Device/Rev: %08x\n", recv[0]);

	/*
	 * Register 0x02: Mode control
	 *	I2S DIF[2:0] = 001, no De-Emphasis, Auto speed mode
	 */
	send[0] = 0x02;
	send[1] = 0x10;
	ret = i2c_master_send(client, send, 2);
	if (ret != 2) {
		snd_printk(KERN_ERR "Failed to set CS4350 to I2S mode\n");
		goto fail;
	}

	/*
	 * Register 0x05/0x06: Volume control
	 *	Channel A volume set to 0 dB
	 *	Channel B volume set to 0 dB
	 */
	send[0] = 0x05;
	send[1] = 0x00;
	ret = i2c_master_send(client, send, 2);
	if (ret != 2) {
		snd_printk(KERN_ERR "Failed to set channel A volume on CS4350\n");
		goto fail;
	}

	send[0] = 0x06;
	send[1] = 0x00;
	ret = i2c_master_send(client, send, 2);
	if (ret != 2) {
		snd_printk(KERN_ERR "Failed to set channel A volume on CS4350\n");
		goto fail;
	}

	/*
	 * Make sure the changes took place, this helps verify we are talking to
	 * the correct chip.
	 */
	send[0] = 0x81;
	ret = i2c_master_send(client, send, 1);
	if (ret != 1) {
		snd_printk(KERN_ERR "Failed to initiate readback\n");
		goto fail;
	}

	ret = i2c_master_recv(client, recv, 8);
	if (ret != 8) {
		snd_printk(KERN_ERR "Failed second read of CS4350 registers\n");
		goto fail;
	}

	if ((recv[1] != 0x10) || (recv[4] != 0x00) || (recv[5] != 0x00)) {
		snd_printk(KERN_ERR "Failed to initialize CS4350 DAC\n");
		goto fail;
	}

	snd_printk(KERN_INFO "CS4350 DAC Initialized\n");
	return 0;

fail:
	return -ENODEV;
}

/*
 * snd_ubi32_cs4350_i2c_probe
 */
static int snd_ubi32_cs4350_i2c_probe(struct i2c_client *client, const struct i2c_device_id *id)
{
	struct snd_card *card;
	struct ubi32_snd_priv *ubi32_priv;
	struct ubi32_cs4350_priv *cs4350_priv;
	int err, ret;
	struct platform_device *pdev;

	pdev = client->dev.platform_data;
	if (!pdev) {
		return -ENODEV;
	}

	/*
	 * Initialize the CS4350 DAC
	 */
	ret = snd_ubi32_cs4350_dac_init(client, id);
	if (ret < 0) {
		/*
		 * Initialization failed.  Propagate the error.
		 */
		return ret;
	}

	/*
	 * Create a snd_card structure
	 */
	card = snd_card_new(index, "Ubi32-CS4350", THIS_MODULE, sizeof(struct ubi32_snd_priv));
	if (card == NULL) {
		return -ENOMEM;
	}

	card->private_free = snd_ubi32_cs4350_free; /* Not sure if correct */
	ubi32_priv = card->private_data;

	/*
	 * CS4350 DAC has a minimum sample rate of 30khz and an
	 * upper limit of 216khz for it's auto-detect.
	 */
	ubi32_priv->min_sample_rate = 30000;
	ubi32_priv->max_sample_rate = 216000;

	/*
	 * Initialize the snd_card's private data structure
	 */
	ubi32_priv->card = card;
	ubi32_priv->client = client;

	/*
	 * Create our private data structure
	 */
	cs4350_priv = kzalloc(sizeof(struct ubi32_cs4350_priv), GFP_KERNEL);
	if (!cs4350_priv) {
		snd_card_free(card);
		return -ENOMEM;
	}
	snd_ubi32_priv_set_drv(ubi32_priv, cs4350_priv);
	spin_lock_init(&cs4350_priv->lock);

	/*
	 * Initial volume is set to max by probe function
	 */
	cs4350_priv->volume[0] = 0xFF;
	cs4350_priv->volume[1] = 0xFF;

	/*
	 * The CS4350 starts off unmuted (bit set = not muted)
	 */
	cs4350_priv->mute = 3;

	/*
	 * Create the new PCM instance
	 */
	err = snd_ubi32_pcm_probe(ubi32_priv, pdev);
	if (err < 0) {
		snd_card_free(card);
		return err; /* What is err?  Need to include correct file */
	}

	strcpy(card->driver, "Ubi32-CS4350");
	strcpy(card->shortname, "Ubi32-CS4350");
	snprintf(card->longname, sizeof(card->longname),
		"%s at sendirq=%d.%d recvirq=%d.%d regs=%p",
		card->shortname, ubi32_priv->tx_irq, ubi32_priv->irq_idx,
		ubi32_priv->rx_irq, ubi32_priv->irq_idx, ubi32_priv->adr);

	snd_card_set_dev(card, &client->dev);

	/*
	 * Set up the mixer components
	 */
	err = snd_ctl_add(card, snd_ctl_new1(&ubi32_cs4350_volume, ubi32_priv));
	if (err) {
		snd_printk(KERN_WARNING "Failed to add volume mixer control\n");
	}
	err = snd_ctl_add(card, snd_ctl_new1(&ubi32_cs4350_mute, ubi32_priv));
	if (err) {
		snd_printk(KERN_WARNING "Failed to add mute mixer control\n");
	}

	/*
	 * Register the sound card
	 */
	if ((err = snd_card_register(card)) != 0) {
		snd_printk(KERN_WARNING "snd_card_register error\n");
	}

	/*
	 * Store card for access from other methods
	 */
	i2c_set_clientdata(client, card);

	return 0;
}

/*
 * snd_ubi32_cs4350_i2c_remove
 */
static int __devexit snd_ubi32_cs4350_i2c_remove(struct i2c_client *client)
{
	struct snd_card *card;
	struct ubi32_snd_priv *ubi32_priv;

	card = i2c_get_clientdata(client);

	ubi32_priv = card->private_data;
	snd_ubi32_pcm_remove(ubi32_priv);

	snd_card_free(i2c_get_clientdata(client));
	i2c_set_clientdata(client, NULL);

	return 0;
}

/*
 * I2C driver description
 */
static struct i2c_driver snd_ubi32_cs4350_driver = {
	.driver = {
		.name = DRIVER_NAME,
		.owner = THIS_MODULE,
	},
	.id_table	= snd_ubi32_cs4350_id,
	.probe		= snd_ubi32_cs4350_i2c_probe,
	.remove		= __devexit_p(snd_ubi32_cs4350_i2c_remove),
};

/*
 * Driver init
 */
static int __init snd_ubi32_cs4350_init(void)
{
	return i2c_add_driver(&snd_ubi32_cs4350_driver);
}
module_init(snd_ubi32_cs4350_init);

/*
 * snd_ubi32_cs4350_exit
 */
static void __exit snd_ubi32_cs4350_exit(void)
{
	i2c_del_driver(&snd_ubi32_cs4350_driver);
}
module_exit(snd_ubi32_cs4350_exit);

/*
 * Module properties
 */
MODULE_ALIAS("i2c:" DRIVER_NAME);
MODULE_AUTHOR("Patrick Tjin");
MODULE_DESCRIPTION("Driver for Ubicom32 audio devices CS4350");
MODULE_LICENSE("GPL");