2 * MFD driver for twl4030 audio submodule, which contains an audio codec, and
5 * Author: Peter Ujfalusi <peter.ujfalusi@ti.com>
7 * Copyright: (C) 2009 Nokia Corporation
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
25 #include <linux/module.h>
26 #include <linux/types.h>
27 #include <linux/slab.h>
28 #include <linux/kernel.h>
30 #include <linux/platform_device.h>
31 #include <linux/i2c/twl.h>
32 #include <linux/mfd/core.h>
33 #include <linux/mfd/twl4030-audio.h>
35 #define TWL4030_AUDIO_CELLS 2
37 static struct platform_device *twl4030_audio_dev;
39 struct twl4030_audio_resource {
45 struct twl4030_audio {
46 unsigned int audio_mclk;
48 struct twl4030_audio_resource resource[TWL4030_AUDIO_RES_MAX];
49 struct mfd_cell cells[TWL4030_AUDIO_CELLS];
53 * Modify the resource, the function returns the content of the register
54 * after the modification.
56 static int twl4030_audio_set_resource(enum twl4030_audio_res id, int enable)
58 struct twl4030_audio *audio = platform_get_drvdata(twl4030_audio_dev);
61 twl_i2c_read_u8(TWL4030_MODULE_AUDIO_VOICE, &val,
62 audio->resource[id].reg);
65 val |= audio->resource[id].mask;
67 val &= ~audio->resource[id].mask;
69 twl_i2c_write_u8(TWL4030_MODULE_AUDIO_VOICE,
70 val, audio->resource[id].reg);
75 static inline int twl4030_audio_get_resource(enum twl4030_audio_res id)
77 struct twl4030_audio *audio = platform_get_drvdata(twl4030_audio_dev);
80 twl_i2c_read_u8(TWL4030_MODULE_AUDIO_VOICE, &val,
81 audio->resource[id].reg);
87 * Enable the resource.
88 * The function returns with error or the content of the register
90 int twl4030_audio_enable_resource(enum twl4030_audio_res id)
92 struct twl4030_audio *audio = platform_get_drvdata(twl4030_audio_dev);
95 if (id >= TWL4030_AUDIO_RES_MAX) {
96 dev_err(&twl4030_audio_dev->dev,
97 "Invalid resource ID (%u)\n", id);
101 mutex_lock(&audio->mutex);
102 if (!audio->resource[id].request_count)
103 /* Resource was disabled, enable it */
104 val = twl4030_audio_set_resource(id, 1);
106 val = twl4030_audio_get_resource(id);
108 audio->resource[id].request_count++;
109 mutex_unlock(&audio->mutex);
113 EXPORT_SYMBOL_GPL(twl4030_audio_enable_resource);
116 * Disable the resource.
117 * The function returns with error or the content of the register
119 int twl4030_audio_disable_resource(unsigned id)
121 struct twl4030_audio *audio = platform_get_drvdata(twl4030_audio_dev);
124 if (id >= TWL4030_AUDIO_RES_MAX) {
125 dev_err(&twl4030_audio_dev->dev,
126 "Invalid resource ID (%u)\n", id);
130 mutex_lock(&audio->mutex);
131 if (!audio->resource[id].request_count) {
132 dev_err(&twl4030_audio_dev->dev,
133 "Resource has been disabled already (%u)\n", id);
134 mutex_unlock(&audio->mutex);
137 audio->resource[id].request_count--;
139 if (!audio->resource[id].request_count)
140 /* Resource can be disabled now */
141 val = twl4030_audio_set_resource(id, 0);
143 val = twl4030_audio_get_resource(id);
145 mutex_unlock(&audio->mutex);
149 EXPORT_SYMBOL_GPL(twl4030_audio_disable_resource);
151 unsigned int twl4030_audio_get_mclk(void)
153 struct twl4030_audio *audio = platform_get_drvdata(twl4030_audio_dev);
155 return audio->audio_mclk;
157 EXPORT_SYMBOL_GPL(twl4030_audio_get_mclk);
159 static int __devinit twl4030_audio_probe(struct platform_device *pdev)
161 struct twl4030_audio *audio;
162 struct twl4030_audio_data *pdata = pdev->dev.platform_data;
163 struct mfd_cell *cell = NULL;
168 dev_err(&pdev->dev, "Platform data is missing\n");
172 /* Configure APLL_INFREQ and disable APLL if enabled */
174 switch (pdata->audio_mclk) {
176 val |= TWL4030_APLL_INFREQ_19200KHZ;
179 val |= TWL4030_APLL_INFREQ_26000KHZ;
182 val |= TWL4030_APLL_INFREQ_38400KHZ;
185 dev_err(&pdev->dev, "Invalid audio_mclk\n");
188 twl_i2c_write_u8(TWL4030_MODULE_AUDIO_VOICE,
189 val, TWL4030_REG_APLL_CTL);
191 audio = kzalloc(sizeof(struct twl4030_audio), GFP_KERNEL);
195 platform_set_drvdata(pdev, audio);
197 twl4030_audio_dev = pdev;
198 mutex_init(&audio->mutex);
199 audio->audio_mclk = pdata->audio_mclk;
202 audio->resource[TWL4030_AUDIO_RES_POWER].reg = TWL4030_REG_CODEC_MODE;
203 audio->resource[TWL4030_AUDIO_RES_POWER].mask = TWL4030_CODECPDZ;
206 audio->resource[TWL4030_AUDIO_RES_APLL].reg = TWL4030_REG_APLL_CTL;
207 audio->resource[TWL4030_AUDIO_RES_APLL].mask = TWL4030_APLL_EN;
210 cell = &audio->cells[childs];
211 cell->name = "twl4030-codec";
212 cell->platform_data = pdata->codec;
213 cell->pdata_size = sizeof(*pdata->codec);
217 cell = &audio->cells[childs];
218 cell->name = "twl4030-vibra";
219 cell->platform_data = pdata->vibra;
220 cell->pdata_size = sizeof(*pdata->vibra);
225 ret = mfd_add_devices(&pdev->dev, pdev->id, audio->cells,
228 dev_err(&pdev->dev, "No platform data found for childs\n");
235 platform_set_drvdata(pdev, NULL);
237 twl4030_audio_dev = NULL;
241 static int __devexit twl4030_audio_remove(struct platform_device *pdev)
243 struct twl4030_audio *audio = platform_get_drvdata(pdev);
245 mfd_remove_devices(&pdev->dev);
246 platform_set_drvdata(pdev, NULL);
248 twl4030_audio_dev = NULL;
253 MODULE_ALIAS("platform:twl4030-audio");
255 static struct platform_driver twl4030_audio_driver = {
256 .probe = twl4030_audio_probe,
257 .remove = __devexit_p(twl4030_audio_remove),
259 .owner = THIS_MODULE,
260 .name = "twl4030-audio",
264 module_platform_driver(twl4030_audio_driver);
266 MODULE_AUTHOR("Peter Ujfalusi <peter.ujfalusi@ti.com>");
267 MODULE_LICENSE("GPL");