2 * MFD driver for twl4030 codec submodule
4 * Author: Peter Ujfalusi <peter.ujfalusi@nokia.com>
6 * Copyright: (C) 2009 Nokia Corporation
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
24 #include <linux/module.h>
25 #include <linux/types.h>
26 #include <linux/kernel.h>
28 #include <linux/platform_device.h>
29 #include <linux/i2c/twl.h>
30 #include <linux/mfd/core.h>
31 #include <linux/mfd/twl4030-codec.h>
33 #define TWL4030_CODEC_CELLS 2
35 static struct platform_device *twl4030_codec_dev;
37 struct twl4030_codec_resource {
43 struct twl4030_codec {
44 unsigned int audio_mclk;
46 struct twl4030_codec_resource resource[TWL4030_CODEC_RES_MAX];
47 struct mfd_cell cells[TWL4030_CODEC_CELLS];
51 * Modify the resource, the function returns the content of the register
52 * after the modification.
54 static int twl4030_codec_set_resource(enum twl4030_codec_res id, int enable)
56 struct twl4030_codec *codec = platform_get_drvdata(twl4030_codec_dev);
59 twl_i2c_read_u8(TWL4030_MODULE_AUDIO_VOICE, &val,
60 codec->resource[id].reg);
63 val |= codec->resource[id].mask;
65 val &= ~codec->resource[id].mask;
67 twl_i2c_write_u8(TWL4030_MODULE_AUDIO_VOICE,
68 val, codec->resource[id].reg);
73 static inline int twl4030_codec_get_resource(enum twl4030_codec_res id)
75 struct twl4030_codec *codec = platform_get_drvdata(twl4030_codec_dev);
78 twl_i2c_read_u8(TWL4030_MODULE_AUDIO_VOICE, &val,
79 codec->resource[id].reg);
85 * Enable the resource.
86 * The function returns with error or the content of the register
88 int twl4030_codec_enable_resource(enum twl4030_codec_res id)
90 struct twl4030_codec *codec = platform_get_drvdata(twl4030_codec_dev);
93 if (id >= TWL4030_CODEC_RES_MAX) {
94 dev_err(&twl4030_codec_dev->dev,
95 "Invalid resource ID (%u)\n", id);
99 mutex_lock(&codec->mutex);
100 if (!codec->resource[id].request_count)
101 /* Resource was disabled, enable it */
102 val = twl4030_codec_set_resource(id, 1);
104 val = twl4030_codec_get_resource(id);
106 codec->resource[id].request_count++;
107 mutex_unlock(&codec->mutex);
111 EXPORT_SYMBOL_GPL(twl4030_codec_enable_resource);
114 * Disable the resource.
115 * The function returns with error or the content of the register
117 int twl4030_codec_disable_resource(unsigned id)
119 struct twl4030_codec *codec = platform_get_drvdata(twl4030_codec_dev);
122 if (id >= TWL4030_CODEC_RES_MAX) {
123 dev_err(&twl4030_codec_dev->dev,
124 "Invalid resource ID (%u)\n", id);
128 mutex_lock(&codec->mutex);
129 if (!codec->resource[id].request_count) {
130 dev_err(&twl4030_codec_dev->dev,
131 "Resource has been disabled already (%u)\n", id);
132 mutex_unlock(&codec->mutex);
135 codec->resource[id].request_count--;
137 if (!codec->resource[id].request_count)
138 /* Resource can be disabled now */
139 val = twl4030_codec_set_resource(id, 0);
141 val = twl4030_codec_get_resource(id);
143 mutex_unlock(&codec->mutex);
147 EXPORT_SYMBOL_GPL(twl4030_codec_disable_resource);
149 unsigned int twl4030_codec_get_mclk(void)
151 struct twl4030_codec *codec = platform_get_drvdata(twl4030_codec_dev);
153 return codec->audio_mclk;
155 EXPORT_SYMBOL_GPL(twl4030_codec_get_mclk);
157 static int __devinit twl4030_codec_probe(struct platform_device *pdev)
159 struct twl4030_codec *codec;
160 struct twl4030_codec_data *pdata = pdev->dev.platform_data;
161 struct mfd_cell *cell = NULL;
166 dev_err(&pdev->dev, "Platform data is missing\n");
170 /* Configure APLL_INFREQ and disable APLL if enabled */
172 switch (pdata->audio_mclk) {
174 val |= TWL4030_APLL_INFREQ_19200KHZ;
177 val |= TWL4030_APLL_INFREQ_26000KHZ;
180 val |= TWL4030_APLL_INFREQ_38400KHZ;
183 dev_err(&pdev->dev, "Invalid audio_mclk\n");
186 twl_i2c_write_u8(TWL4030_MODULE_AUDIO_VOICE,
187 val, TWL4030_REG_APLL_CTL);
189 codec = kzalloc(sizeof(struct twl4030_codec), GFP_KERNEL);
193 platform_set_drvdata(pdev, codec);
195 twl4030_codec_dev = pdev;
196 mutex_init(&codec->mutex);
197 codec->audio_mclk = pdata->audio_mclk;
200 codec->resource[TWL4030_CODEC_RES_POWER].reg = TWL4030_REG_CODEC_MODE;
201 codec->resource[TWL4030_CODEC_RES_POWER].mask = TWL4030_CODECPDZ;
204 codec->resource[TWL4030_CODEC_RES_APLL].reg = TWL4030_REG_APLL_CTL;
205 codec->resource[TWL4030_CODEC_RES_APLL].mask = TWL4030_APLL_EN;
208 cell = &codec->cells[childs];
209 cell->name = "twl4030_codec_audio";
210 cell->platform_data = pdata->audio;
211 cell->data_size = sizeof(*pdata->audio);
215 cell = &codec->cells[childs];
216 cell->name = "twl4030_codec_vibra";
217 cell->platform_data = pdata->vibra;
218 cell->data_size = sizeof(*pdata->vibra);
223 ret = mfd_add_devices(&pdev->dev, pdev->id, codec->cells,
226 dev_err(&pdev->dev, "No platform data found for childs\n");
233 platform_set_drvdata(pdev, NULL);
235 twl4030_codec_dev = NULL;
239 static int __devexit twl4030_codec_remove(struct platform_device *pdev)
241 struct twl4030_codec *codec = platform_get_drvdata(pdev);
243 mfd_remove_devices(&pdev->dev);
244 platform_set_drvdata(pdev, NULL);
246 twl4030_codec_dev = NULL;
251 MODULE_ALIAS("platform:twl4030_codec");
253 static struct platform_driver twl4030_codec_driver = {
254 .probe = twl4030_codec_probe,
255 .remove = __devexit_p(twl4030_codec_remove),
257 .owner = THIS_MODULE,
258 .name = "twl4030_codec",
262 static int __devinit twl4030_codec_init(void)
264 return platform_driver_register(&twl4030_codec_driver);
266 module_init(twl4030_codec_init);
268 static void __devexit twl4030_codec_exit(void)
270 platform_driver_unregister(&twl4030_codec_driver);
272 module_exit(twl4030_codec_exit);
274 MODULE_AUTHOR("Peter Ujfalusi <peter.ujfalusi@nokia.com>");
275 MODULE_LICENSE("GPL");