cx20442.c 10.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * cx20442.c  --  CX20442 ALSA Soc Audio driver
 *
 * Copyright 2009 Janusz Krzysztofik <jkrzyszt@tis.icnet.pl>
 *
 * Initially based on sound/soc/codecs/wm8400.c
 * Copyright 2008, 2009 Wolfson Microelectronics PLC.
 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
 *
 *  This program 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.
 */

16
#include <linux/tty.h>
17
#include <linux/slab.h>
18
#include <linux/module.h>
19
#include <linux/regulator/consumer.h>
20

21 22
#include <sound/core.h>
#include <sound/initval.h>
L
Liam Girdwood 已提交
23
#include <sound/soc.h>
24 25 26 27 28

#include "cx20442.h"


struct cx20442_priv {
29
	struct tty_struct *tty;
30
	struct regulator *por;
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
};

#define CX20442_PM		0x0

#define CX20442_TELIN		0
#define CX20442_TELOUT		1
#define CX20442_MIC		2
#define CX20442_SPKOUT		3
#define CX20442_AGC		4

static const struct snd_soc_dapm_widget cx20442_dapm_widgets[] = {
	SND_SOC_DAPM_OUTPUT("TELOUT"),
	SND_SOC_DAPM_OUTPUT("SPKOUT"),
	SND_SOC_DAPM_OUTPUT("AGCOUT"),

	SND_SOC_DAPM_MIXER("SPKOUT Mixer", SND_SOC_NOPM, 0, 0, NULL, 0),

	SND_SOC_DAPM_PGA("TELOUT Amp", CX20442_PM, CX20442_TELOUT, 0, NULL, 0),
	SND_SOC_DAPM_PGA("SPKOUT Amp", CX20442_PM, CX20442_SPKOUT, 0, NULL, 0),
	SND_SOC_DAPM_PGA("SPKOUT AGC", CX20442_PM, CX20442_AGC, 0, NULL, 0),

	SND_SOC_DAPM_DAC("DAC", "Playback", SND_SOC_NOPM, 0, 0),
	SND_SOC_DAPM_ADC("ADC", "Capture", SND_SOC_NOPM, 0, 0),

	SND_SOC_DAPM_MIXER("Input Mixer", SND_SOC_NOPM, 0, 0, NULL, 0),

	SND_SOC_DAPM_MICBIAS("TELIN Bias", CX20442_PM, CX20442_TELIN, 0),
	SND_SOC_DAPM_MICBIAS("MIC Bias", CX20442_PM, CX20442_MIC, 0),

	SND_SOC_DAPM_PGA("MIC AGC", CX20442_PM, CX20442_AGC, 0, NULL, 0),

	SND_SOC_DAPM_INPUT("TELIN"),
	SND_SOC_DAPM_INPUT("MIC"),
	SND_SOC_DAPM_INPUT("AGCIN"),
};

static const struct snd_soc_dapm_route cx20442_audio_map[] = {
	{"TELOUT", NULL, "TELOUT Amp"},

	{"SPKOUT", NULL, "SPKOUT Mixer"},
	{"SPKOUT Mixer", NULL, "SPKOUT Amp"},

	{"TELOUT Amp", NULL, "DAC"},
	{"SPKOUT Amp", NULL, "DAC"},

	{"SPKOUT Mixer", NULL, "SPKOUT AGC"},
	{"SPKOUT AGC", NULL, "AGCIN"},

	{"AGCOUT", NULL, "MIC AGC"},
	{"MIC AGC", NULL, "MIC"},

	{"MIC Bias", NULL, "MIC"},
	{"Input Mixer", NULL, "MIC Bias"},

	{"TELIN Bias", NULL, "TELIN"},
	{"Input Mixer", NULL, "TELIN Bias"},

	{"ADC", NULL, "Input Mixer"},
};

enum v253_vls {
	V253_VLS_NONE = 0,
	V253_VLS_T,
	V253_VLS_L,
	V253_VLS_LT,
	V253_VLS_S,
	V253_VLS_ST,
	V253_VLS_M,
	V253_VLS_MST,
	V253_VLS_S1,
	V253_VLS_S1T,
	V253_VLS_MS1T,
	V253_VLS_M1,
	V253_VLS_M1ST,
	V253_VLS_M1S1T,
	V253_VLS_H,
	V253_VLS_HT,
	V253_VLS_MS,
	V253_VLS_MS1,
	V253_VLS_M1S,
	V253_VLS_M1S1,
	V253_VLS_TEST,
};

115 116
#if 0
/* FIXME : these function will be re-used */
117 118
static int cx20442_pm_to_v253_vls(u8 value)
{
119
	switch (value & ~(1 << CX20442_AGC)) {
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
	case 0:
		return V253_VLS_T;
	case (1 << CX20442_SPKOUT):
	case (1 << CX20442_MIC):
	case (1 << CX20442_SPKOUT) | (1 << CX20442_MIC):
		return V253_VLS_M1S1;
	case (1 << CX20442_TELOUT):
	case (1 << CX20442_TELIN):
	case (1 << CX20442_TELOUT) | (1 << CX20442_TELIN):
		return V253_VLS_L;
	case (1 << CX20442_TELOUT) | (1 << CX20442_MIC):
		return V253_VLS_NONE;
	}
	return -EINVAL;
}
static int cx20442_pm_to_v253_vsp(u8 value)
{
137
	switch (value & ~(1 << CX20442_AGC)) {
138 139 140 141 142 143 144 145 146 147 148
	case (1 << CX20442_SPKOUT):
	case (1 << CX20442_MIC):
	case (1 << CX20442_SPKOUT) | (1 << CX20442_MIC):
		return (bool)(value & (1 << CX20442_AGC));
	}
	return (value & (1 << CX20442_AGC)) ? -EINVAL : 0;
}

static int cx20442_write(struct snd_soc_codec *codec, unsigned int reg,
							unsigned int value)
{
149
	struct cx20442_priv *cx20442 = snd_soc_codec_get_drvdata(codec);
150 151 152 153
	u8 *reg_cache = codec->reg_cache;
	int vls, vsp, old, len;
	char buf[18];

154
	if (reg >= codec->driver->reg_cache_size)
155 156
		return -EINVAL;

157
	/* tty and write pointers required for talking to the modem
158
	 * are expected to be set by the line discipline initialization code */
159
	if (!cx20442->tty || !cx20442->tty->ops->write)
160 161 162 163 164 165 166 167 168 169
		return -EIO;

	old = reg_cache[reg];
	reg_cache[reg] = value;

	vls = cx20442_pm_to_v253_vls(value);
	if (vls < 0)
		return vls;

	vsp = cx20442_pm_to_v253_vsp(value);
170
	if (vsp < 0)
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
		return vsp;

	if ((vls == V253_VLS_T) ||
			(vls == cx20442_pm_to_v253_vls(old))) {
		if (vsp == cx20442_pm_to_v253_vsp(old))
			return 0;
		len = snprintf(buf, ARRAY_SIZE(buf), "at+vsp=%d\r", vsp);
	} else if (vsp == cx20442_pm_to_v253_vsp(old))
		len = snprintf(buf, ARRAY_SIZE(buf), "at+vls=%d\r", vls);
	else
		len = snprintf(buf, ARRAY_SIZE(buf),
					"at+vls=%d;+vsp=%d\r", vls, vsp);

	if (unlikely(len > (ARRAY_SIZE(buf) - 1)))
		return -ENOMEM;

187
	dev_dbg(codec->dev, "%s: %s\n", __func__, buf);
188
	if (cx20442->tty->ops->write(cx20442->tty, buf, len) != len)
189 190 191 192
		return -EIO;

	return 0;
}
193
#endif
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215

/*
 * Line discpline related code
 *
 * Any of the callback functions below can be used in two ways:
 * 1) registerd by a machine driver as one of line discipline operations,
 * 2) called from a machine's provided line discipline callback function
 *    in case when extra machine specific code must be run as well.
 */

/* Modem init: echo off, digital speaker off, quiet off, voice mode */
static const char *v253_init = "ate0m0q0+fclass=8\r";

/* Line discipline .open() */
static int v253_open(struct tty_struct *tty)
{
	int ret, len = strlen(v253_init);

	/* Doesn't make sense without write callback */
	if (!tty->ops->write)
		return -EINVAL;

216 217 218
	/* Won't work if no codec pointer has been passed by a card driver */
	if (!tty->disc_data)
		return -ENODEV;
219

220
	tty->receive_room = 16;
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
	if (tty->ops->write(tty, v253_init, len) != len) {
		ret = -EIO;
		goto err;
	}
	/* Actual setup will be performed after the modem responds. */
	return 0;
err:
	tty->disc_data = NULL;
	return ret;
}

/* Line discipline .close() */
static void v253_close(struct tty_struct *tty)
{
	struct snd_soc_codec *codec = tty->disc_data;
236
	struct cx20442_priv *cx20442;
237 238 239 240 241 242

	tty->disc_data = NULL;

	if (!codec)
		return;

243 244
	cx20442 = snd_soc_codec_get_drvdata(codec);

245
	/* Prevent the codec driver from further accessing the modem */
246
	cx20442->tty = NULL;
247
	codec->component.card->pop_time = 0;
248 249 250 251 252 253 254 255 256 257
}

/* Line discipline .hangup() */
static int v253_hangup(struct tty_struct *tty)
{
	v253_close(tty);
	return 0;
}

/* Line discipline .receive_buf() */
258 259
static void v253_receive(struct tty_struct *tty,
				const unsigned char *cp, char *fp, int count)
260 261
{
	struct snd_soc_codec *codec = tty->disc_data;
262
	struct cx20442_priv *cx20442;
263 264

	if (!codec)
265
		return;
266

267 268
	cx20442 = snd_soc_codec_get_drvdata(codec);

269
	if (!cx20442->tty) {
270 271 272
		/* First modem response, complete setup procedure */

		/* Set up codec driver access to modem controls */
273
		cx20442->tty = tty;
274
		codec->component.card->pop_time = 1;
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
	}
}

/* Line discipline .write_wakeup() */
static void v253_wakeup(struct tty_struct *tty)
{
}

struct tty_ldisc_ops v253_ops = {
	.magic = TTY_LDISC_MAGIC,
	.name = "cx20442",
	.owner = THIS_MODULE,
	.open = v253_open,
	.close = v253_close,
	.hangup = v253_hangup,
	.receive_buf = v253_receive,
	.write_wakeup = v253_wakeup,
};
EXPORT_SYMBOL_GPL(v253_ops);


/*
 * Codec DAI
 */

300
static struct snd_soc_dai_driver cx20442_dai = {
301
	.name = "cx20442-voice",
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
	.playback = {
		.stream_name = "Playback",
		.channels_min = 1,
		.channels_max = 1,
		.rates = SNDRV_PCM_RATE_8000,
		.formats = SNDRV_PCM_FMTBIT_S16_LE,
	},
	.capture = {
		.stream_name = "Capture",
		.channels_min = 1,
		.channels_max = 1,
		.rates = SNDRV_PCM_RATE_8000,
		.formats = SNDRV_PCM_FMTBIT_S16_LE,
	},
};

318 319 320 321 322 323 324 325
static int cx20442_set_bias_level(struct snd_soc_codec *codec,
		enum snd_soc_bias_level level)
{
	struct cx20442_priv *cx20442 = snd_soc_codec_get_drvdata(codec);
	int err = 0;

	switch (level) {
	case SND_SOC_BIAS_PREPARE:
326
		if (snd_soc_codec_get_bias_level(codec) != SND_SOC_BIAS_STANDBY)
327 328 329 330 331 332 333
			break;
		if (IS_ERR(cx20442->por))
			err = PTR_ERR(cx20442->por);
		else
			err = regulator_enable(cx20442->por);
		break;
	case SND_SOC_BIAS_STANDBY:
334
		if (snd_soc_codec_get_bias_level(codec) != SND_SOC_BIAS_PREPARE)
335 336 337 338 339 340 341 342 343 344 345 346 347
			break;
		if (IS_ERR(cx20442->por))
			err = PTR_ERR(cx20442->por);
		else
			err = regulator_disable(cx20442->por);
		break;
	default:
		break;
	}

	return err;
}

348
static int cx20442_codec_probe(struct snd_soc_codec *codec)
349
{
350
	struct cx20442_priv *cx20442;
351

352 353 354
	cx20442 = kzalloc(sizeof(struct cx20442_priv), GFP_KERNEL);
	if (cx20442 == NULL)
		return -ENOMEM;
355

356 357 358
	cx20442->por = regulator_get(codec->dev, "POR");
	if (IS_ERR(cx20442->por))
		dev_warn(codec->dev, "failed to get the regulator");
359
	cx20442->tty = NULL;
360 361

	snd_soc_codec_set_drvdata(codec, cx20442);
362
	codec->component.card->pop_time = 0;
363 364

	return 0;
365 366 367
}

/* power down chip */
368
static int cx20442_codec_remove(struct snd_soc_codec *codec)
369
{
370
	struct cx20442_priv *cx20442 = snd_soc_codec_get_drvdata(codec);
371

372 373
	if (cx20442->tty) {
		struct tty_struct *tty = cx20442->tty;
374
		tty_hangup(tty);
375
	}
376

377 378 379 380 381 382
	if (!IS_ERR(cx20442->por)) {
		/* should be already in STANDBY, hence disabled */
		regulator_put(cx20442->por);
	}

	snd_soc_codec_set_drvdata(codec, NULL);
383
	kfree(cx20442);
384 385 386
	return 0;
}

387
static const u8 cx20442_reg;
388

389
static const struct snd_soc_codec_driver cx20442_codec_dev = {
390 391
	.probe = 	cx20442_codec_probe,
	.remove = 	cx20442_codec_remove,
392
	.set_bias_level = cx20442_set_bias_level,
393

394 395 396 397 398 399
	.component_driver = {
		.dapm_widgets		= cx20442_dapm_widgets,
		.num_dapm_widgets	= ARRAY_SIZE(cx20442_dapm_widgets),
		.dapm_routes		= cx20442_audio_map,
		.num_dapm_routes	= ARRAY_SIZE(cx20442_audio_map),
	},
400 401 402 403
};

static int cx20442_platform_probe(struct platform_device *pdev)
{
404 405
	return snd_soc_register_codec(&pdev->dev,
			&cx20442_codec_dev, &cx20442_dai, 1);
406 407
}

408
static int cx20442_platform_remove(struct platform_device *pdev)
409
{
410
	snd_soc_unregister_codec(&pdev->dev);
411 412 413 414 415
	return 0;
}

static struct platform_driver cx20442_platform_driver = {
	.driver = {
416
		.name = "cx20442-codec",
417 418
		},
	.probe = cx20442_platform_probe,
419
	.remove = cx20442_platform_remove,
420 421
};

422
module_platform_driver(cx20442_platform_driver);
423 424 425 426

MODULE_DESCRIPTION("ASoC CX20442-11 voice modem codec driver");
MODULE_AUTHOR("Janusz Krzysztofik");
MODULE_LICENSE("GPL");
427
MODULE_ALIAS("platform:cx20442-codec");