twl4030-audio.c 6.5 KB
Newer Older
1
/*
2 3
 * MFD driver for twl4030 audio submodule, which contains an audio codec, and
 * the vibra control.
4
 *
5
 * Author: Peter Ujfalusi <peter.ujfalusi@ti.com>
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
 *
 * Copyright:   (C) 2009 Nokia Corporation
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * This program 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 this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 *
 */

#include <linux/module.h>
#include <linux/types.h>
27
#include <linux/slab.h>
28 29 30
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/platform_device.h>
31
#include <linux/i2c/twl.h>
32
#include <linux/mfd/core.h>
33
#include <linux/mfd/twl4030-audio.h>
34

35
#define TWL4030_AUDIO_CELLS	2
36

37
static struct platform_device *twl4030_audio_dev;
38

39
struct twl4030_audio_resource {
40 41 42 43 44
	int request_count;
	u8 reg;
	u8 mask;
};

45
struct twl4030_audio {
46
	unsigned int audio_mclk;
47
	struct mutex mutex;
48
	struct twl4030_audio_resource resource[TWL4030_AUDIO_RES_MAX];
49
	struct mfd_cell cells[TWL4030_AUDIO_CELLS];
50 51 52 53 54 55
};

/*
 * Modify the resource, the function returns the content of the register
 * after the modification.
 */
56
static int twl4030_audio_set_resource(enum twl4030_audio_res id, int enable)
57
{
58
	struct twl4030_audio *audio = platform_get_drvdata(twl4030_audio_dev);
59 60
	u8 val;

61
	twl_i2c_read_u8(TWL4030_MODULE_AUDIO_VOICE, &val,
62
			audio->resource[id].reg);
63 64

	if (enable)
65
		val |= audio->resource[id].mask;
66
	else
67
		val &= ~audio->resource[id].mask;
68

69
	twl_i2c_write_u8(TWL4030_MODULE_AUDIO_VOICE,
70
					val, audio->resource[id].reg);
71 72 73 74

	return val;
}

75
static inline int twl4030_audio_get_resource(enum twl4030_audio_res id)
76
{
77
	struct twl4030_audio *audio = platform_get_drvdata(twl4030_audio_dev);
78 79
	u8 val;

80
	twl_i2c_read_u8(TWL4030_MODULE_AUDIO_VOICE, &val,
81
			audio->resource[id].reg);
82 83 84 85 86 87 88 89

	return val;
}

/*
 * Enable the resource.
 * The function returns with error or the content of the register
 */
90
int twl4030_audio_enable_resource(enum twl4030_audio_res id)
91
{
92
	struct twl4030_audio *audio = platform_get_drvdata(twl4030_audio_dev);
93 94
	int val;

95
	if (id >= TWL4030_AUDIO_RES_MAX) {
96
		dev_err(&twl4030_audio_dev->dev,
97 98 99 100
				"Invalid resource ID (%u)\n", id);
		return -EINVAL;
	}

101 102
	mutex_lock(&audio->mutex);
	if (!audio->resource[id].request_count)
103
		/* Resource was disabled, enable it */
104
		val = twl4030_audio_set_resource(id, 1);
105
	else
106
		val = twl4030_audio_get_resource(id);
107

108 109
	audio->resource[id].request_count++;
	mutex_unlock(&audio->mutex);
110 111 112

	return val;
}
113
EXPORT_SYMBOL_GPL(twl4030_audio_enable_resource);
114 115 116 117 118

/*
 * Disable the resource.
 * The function returns with error or the content of the register
 */
119
int twl4030_audio_disable_resource(unsigned id)
120
{
121
	struct twl4030_audio *audio = platform_get_drvdata(twl4030_audio_dev);
122 123
	int val;

124
	if (id >= TWL4030_AUDIO_RES_MAX) {
125
		dev_err(&twl4030_audio_dev->dev,
126 127 128 129
				"Invalid resource ID (%u)\n", id);
		return -EINVAL;
	}

130 131 132
	mutex_lock(&audio->mutex);
	if (!audio->resource[id].request_count) {
		dev_err(&twl4030_audio_dev->dev,
133
			"Resource has been disabled already (%u)\n", id);
134
		mutex_unlock(&audio->mutex);
135 136
		return -EPERM;
	}
137
	audio->resource[id].request_count--;
138

139
	if (!audio->resource[id].request_count)
140
		/* Resource can be disabled now */
141
		val = twl4030_audio_set_resource(id, 0);
142
	else
143
		val = twl4030_audio_get_resource(id);
144

145
	mutex_unlock(&audio->mutex);
146 147 148

	return val;
}
149
EXPORT_SYMBOL_GPL(twl4030_audio_disable_resource);
150

151
unsigned int twl4030_audio_get_mclk(void)
152
{
153
	struct twl4030_audio *audio = platform_get_drvdata(twl4030_audio_dev);
154

155
	return audio->audio_mclk;
156
}
157
EXPORT_SYMBOL_GPL(twl4030_audio_get_mclk);
158

159
static int __devinit twl4030_audio_probe(struct platform_device *pdev)
160
{
161
	struct twl4030_audio *audio;
162
	struct twl4030_audio_data *pdata = pdev->dev.platform_data;
163 164
	struct mfd_cell *cell = NULL;
	int ret, childs = 0;
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
	u8 val;

	if (!pdata) {
		dev_err(&pdev->dev, "Platform data is missing\n");
		return -EINVAL;
	}

	/* Configure APLL_INFREQ and disable APLL if enabled */
	val = 0;
	switch (pdata->audio_mclk) {
	case 19200000:
		val |= TWL4030_APLL_INFREQ_19200KHZ;
		break;
	case 26000000:
		val |= TWL4030_APLL_INFREQ_26000KHZ;
		break;
	case 38400000:
		val |= TWL4030_APLL_INFREQ_38400KHZ;
		break;
	default:
		dev_err(&pdev->dev, "Invalid audio_mclk\n");
		return -EINVAL;
	}
188
	twl_i2c_write_u8(TWL4030_MODULE_AUDIO_VOICE,
189
					val, TWL4030_REG_APLL_CTL);
190

191 192
	audio = kzalloc(sizeof(struct twl4030_audio), GFP_KERNEL);
	if (!audio)
193 194
		return -ENOMEM;

195
	platform_set_drvdata(pdev, audio);
196

197 198 199
	twl4030_audio_dev = pdev;
	mutex_init(&audio->mutex);
	audio->audio_mclk = pdata->audio_mclk;
200 201

	/* Codec power */
202 203
	audio->resource[TWL4030_AUDIO_RES_POWER].reg = TWL4030_REG_CODEC_MODE;
	audio->resource[TWL4030_AUDIO_RES_POWER].mask = TWL4030_CODECPDZ;
204 205

	/* PLL */
206 207
	audio->resource[TWL4030_AUDIO_RES_APLL].reg = TWL4030_REG_APLL_CTL;
	audio->resource[TWL4030_AUDIO_RES_APLL].mask = TWL4030_APLL_EN;
208

209
	if (pdata->codec) {
210
		cell = &audio->cells[childs];
211
		cell->name = "twl4030-codec";
212 213
		cell->platform_data = pdata->codec;
		cell->pdata_size = sizeof(*pdata->codec);
214 215 216
		childs++;
	}
	if (pdata->vibra) {
217
		cell = &audio->cells[childs];
218
		cell->name = "twl4030-vibra";
219 220
		cell->platform_data = pdata->vibra;
		cell->pdata_size = sizeof(*pdata->vibra);
221 222 223 224
		childs++;
	}

	if (childs)
225
		ret = mfd_add_devices(&pdev->dev, pdev->id, audio->cells,
226
				      childs, NULL, 0, NULL);
227 228 229 230 231 232 233 234 235
	else {
		dev_err(&pdev->dev, "No platform data found for childs\n");
		ret = -ENODEV;
	}

	if (!ret)
		return 0;

	platform_set_drvdata(pdev, NULL);
236 237
	kfree(audio);
	twl4030_audio_dev = NULL;
238 239 240
	return ret;
}

241
static int __devexit twl4030_audio_remove(struct platform_device *pdev)
242
{
243
	struct twl4030_audio *audio = platform_get_drvdata(pdev);
244 245 246

	mfd_remove_devices(&pdev->dev);
	platform_set_drvdata(pdev, NULL);
247 248
	kfree(audio);
	twl4030_audio_dev = NULL;
249 250 251 252

	return 0;
}

253
MODULE_ALIAS("platform:twl4030-audio");
254

255 256 257
static struct platform_driver twl4030_audio_driver = {
	.probe		= twl4030_audio_probe,
	.remove		= __devexit_p(twl4030_audio_remove),
258 259
	.driver		= {
		.owner	= THIS_MODULE,
260
		.name	= "twl4030-audio",
261 262 263
	},
};

264
module_platform_driver(twl4030_audio_driver);
265

266
MODULE_AUTHOR("Peter Ujfalusi <peter.ujfalusi@ti.com>");
267
MODULE_LICENSE("GPL");