twl4030-audio.c 7.2 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 32
#include <linux/of.h>
#include <linux/of_platform.h>
33
#include <linux/i2c/twl.h>
34
#include <linux/mfd/core.h>
35
#include <linux/mfd/twl4030-audio.h>
36

37
#define TWL4030_AUDIO_CELLS	2
38

39
static struct platform_device *twl4030_audio_dev;
40

41
struct twl4030_audio_resource {
42 43 44 45 46
	int request_count;
	u8 reg;
	u8 mask;
};

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

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

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

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

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

	return val;
}

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

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

	return val;
}

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

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

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

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

	return val;
}
115
EXPORT_SYMBOL_GPL(twl4030_audio_enable_resource);
116 117 118 119 120

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

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

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

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

147
	mutex_unlock(&audio->mutex);
148 149 150

	return val;
}
151
EXPORT_SYMBOL_GPL(twl4030_audio_disable_resource);
152

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

157
	return audio->audio_mclk;
158
}
159
EXPORT_SYMBOL_GPL(twl4030_audio_get_mclk);
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
static bool twl4030_audio_has_codec(struct twl4030_audio_data *pdata,
			      struct device_node *node)
{
	if (pdata && pdata->codec)
		return true;

	if (of_find_node_by_name(node, "codec"))
		return true;

	return false;
}

static bool twl4030_audio_has_vibra(struct twl4030_audio_data *pdata,
			      struct device_node *node)
{
	int vibra;

	if (pdata && pdata->vibra)
		return true;

	if (!of_property_read_u32(node, "ti,enable-vibra", &vibra) && vibra)
		return true;

	return false;
}

B
Bill Pemberton 已提交
187
static int twl4030_audio_probe(struct platform_device *pdev)
188
{
189
	struct twl4030_audio *audio;
J
Jingoo Han 已提交
190
	struct twl4030_audio_data *pdata = dev_get_platdata(&pdev->dev);
191
	struct device_node *node = pdev->dev.of_node;
192 193
	struct mfd_cell *cell = NULL;
	int ret, childs = 0;
194 195
	u8 val;

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

201 202 203 204 205 206
	audio = devm_kzalloc(&pdev->dev, sizeof(struct twl4030_audio),
			     GFP_KERNEL);
	if (!audio)
		return -ENOMEM;

	mutex_init(&audio->mutex);
207
	audio->audio_mclk = twl_get_hfclk_rate();
208

209
	/* Configure APLL_INFREQ and disable APLL if enabled */
210
	switch (audio->audio_mclk) {
211
	case 19200000:
212
		val = TWL4030_APLL_INFREQ_19200KHZ;
213 214
		break;
	case 26000000:
215
		val = TWL4030_APLL_INFREQ_26000KHZ;
216 217
		break;
	case 38400000:
218
		val = TWL4030_APLL_INFREQ_38400KHZ;
219 220 221 222 223
		break;
	default:
		dev_err(&pdev->dev, "Invalid audio_mclk\n");
		return -EINVAL;
	}
224
	twl_i2c_write_u8(TWL4030_MODULE_AUDIO_VOICE, val, TWL4030_REG_APLL_CTL);
225 226

	/* Codec power */
227 228
	audio->resource[TWL4030_AUDIO_RES_POWER].reg = TWL4030_REG_CODEC_MODE;
	audio->resource[TWL4030_AUDIO_RES_POWER].mask = TWL4030_CODECPDZ;
229 230

	/* PLL */
231 232
	audio->resource[TWL4030_AUDIO_RES_APLL].reg = TWL4030_REG_APLL_CTL;
	audio->resource[TWL4030_AUDIO_RES_APLL].mask = TWL4030_APLL_EN;
233

234
	if (twl4030_audio_has_codec(pdata, node)) {
235
		cell = &audio->cells[childs];
236
		cell->name = "twl4030-codec";
237 238 239 240
		if (pdata) {
			cell->platform_data = pdata->codec;
			cell->pdata_size = sizeof(*pdata->codec);
		}
241 242
		childs++;
	}
243
	if (twl4030_audio_has_vibra(pdata, node)) {
244
		cell = &audio->cells[childs];
245
		cell->name = "twl4030-vibra";
246 247 248 249
		if (pdata) {
			cell->platform_data = pdata->vibra;
			cell->pdata_size = sizeof(*pdata->vibra);
		}
250 251 252
		childs++;
	}

253 254 255
	platform_set_drvdata(pdev, audio);
	twl4030_audio_dev = pdev;

256
	if (childs)
257
		ret = mfd_add_devices(&pdev->dev, pdev->id, audio->cells,
258
				      childs, NULL, 0, NULL);
259 260 261 262 263
	else {
		dev_err(&pdev->dev, "No platform data found for childs\n");
		ret = -ENODEV;
	}

264
	if (ret)
265
		twl4030_audio_dev = NULL;
266 267 268 269

	return ret;
}

B
Bill Pemberton 已提交
270
static int twl4030_audio_remove(struct platform_device *pdev)
271 272
{
	mfd_remove_devices(&pdev->dev);
273
	twl4030_audio_dev = NULL;
274 275 276 277

	return 0;
}

278 279 280 281 282 283
static const struct of_device_id twl4030_audio_of_match[] = {
	{.compatible = "ti,twl4030-audio", },
	{ },
};
MODULE_DEVICE_TABLE(of, twl4030_audio_of_match);

284
static struct platform_driver twl4030_audio_driver = {
285 286
	.driver		= {
		.owner	= THIS_MODULE,
287
		.name	= "twl4030-audio",
288
		.of_match_table = twl4030_audio_of_match,
289
	},
290
	.probe		= twl4030_audio_probe,
B
Bill Pemberton 已提交
291
	.remove		= twl4030_audio_remove,
292 293
};

294
module_platform_driver(twl4030_audio_driver);
295

296
MODULE_AUTHOR("Peter Ujfalusi <peter.ujfalusi@ti.com>");
297
MODULE_DESCRIPTION("TWL4030 audio block MFD driver");
298
MODULE_LICENSE("GPL");
299
MODULE_ALIAS("platform:twl4030-audio");