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/twl4030.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 {
45 struct twl4030_codec_resource resource[TWL4030_CODEC_RES_MAX];
46 struct mfd_cell cells[TWL4030_CODEC_CELLS];
50 * Modify the resource, the function returns the content of the register
51 * after the modification.
53 static int twl4030_codec_set_resource(enum twl4030_codec_res id, int enable)
55 struct twl4030_codec *codec = platform_get_drvdata(twl4030_codec_dev);
58 twl4030_i2c_read_u8(TWL4030_MODULE_AUDIO_VOICE, &val,
59 codec->resource[id].reg);
62 val |= codec->resource[id].mask;
64 val &= ~codec->resource[id].mask;
66 twl4030_i2c_write_u8(TWL4030_MODULE_AUDIO_VOICE,
67 val, codec->resource[id].reg);
72 static inline int twl4030_codec_get_resource(enum twl4030_codec_res id)
74 struct twl4030_codec *codec = platform_get_drvdata(twl4030_codec_dev);
77 twl4030_i2c_read_u8(TWL4030_MODULE_AUDIO_VOICE, &val,
78 codec->resource[id].reg);
84 * Enable the resource.
85 * The function returns with error or the content of the register
87 int twl4030_codec_enable_resource(enum twl4030_codec_res id)
89 struct twl4030_codec *codec = platform_get_drvdata(twl4030_codec_dev);
92 if (id >= TWL4030_CODEC_RES_MAX) {
93 dev_err(&twl4030_codec_dev->dev,
94 "Invalid resource ID (%u)\n", id);
98 mutex_lock(&codec->mutex);
99 if (!codec->resource[id].request_count)
100 /* Resource was disabled, enable it */
101 val = twl4030_codec_set_resource(id, 1);
103 val = twl4030_codec_get_resource(id);
105 codec->resource[id].request_count++;
106 mutex_unlock(&codec->mutex);
110 EXPORT_SYMBOL_GPL(twl4030_codec_enable_resource);
113 * Disable the resource.
114 * The function returns with error or the content of the register
116 int twl4030_codec_disable_resource(unsigned id)
118 struct twl4030_codec *codec = platform_get_drvdata(twl4030_codec_dev);
121 if (id >= TWL4030_CODEC_RES_MAX) {
122 dev_err(&twl4030_codec_dev->dev,
123 "Invalid resource ID (%u)\n", id);
127 mutex_lock(&codec->mutex);
128 if (!codec->resource[id].request_count) {
129 dev_err(&twl4030_codec_dev->dev,
130 "Resource has been disabled already (%u)\n", id);
131 mutex_unlock(&codec->mutex);
134 codec->resource[id].request_count--;
136 if (!codec->resource[id].request_count)
137 /* Resource can be disabled now */
138 val = twl4030_codec_set_resource(id, 0);
140 val = twl4030_codec_get_resource(id);
142 mutex_unlock(&codec->mutex);
146 EXPORT_SYMBOL_GPL(twl4030_codec_disable_resource);
148 static int __devinit twl4030_codec_probe(struct platform_device *pdev)
150 struct twl4030_codec *codec;
151 struct twl4030_codec_data *pdata = pdev->dev.platform_data;
152 struct mfd_cell *cell = NULL;
155 codec = kzalloc(sizeof(struct twl4030_codec), GFP_KERNEL);
159 platform_set_drvdata(pdev, codec);
161 twl4030_codec_dev = pdev;
162 mutex_init(&codec->mutex);
165 codec->resource[TWL4030_CODEC_RES_POWER].reg = TWL4030_REG_CODEC_MODE;
166 codec->resource[TWL4030_CODEC_RES_POWER].mask = TWL4030_CODECPDZ;
169 codec->resource[TWL4030_CODEC_RES_APLL].reg = TWL4030_REG_APLL_CTL;
170 codec->resource[TWL4030_CODEC_RES_APLL].mask = TWL4030_APLL_EN;
173 cell = &codec->cells[childs];
174 cell->name = "twl4030_codec_audio";
175 cell->platform_data = pdata->audio;
176 cell->data_size = sizeof(*pdata->audio);
180 cell = &codec->cells[childs];
181 cell->name = "twl4030_codec_vibra";
182 cell->platform_data = pdata->vibra;
183 cell->data_size = sizeof(*pdata->vibra);
188 ret = mfd_add_devices(&pdev->dev, pdev->id, codec->cells,
191 dev_err(&pdev->dev, "No platform data found for childs\n");
198 platform_set_drvdata(pdev, NULL);
200 twl4030_codec_dev = NULL;
204 static int __devexit twl4030_codec_remove(struct platform_device *pdev)
206 struct twl4030_codec *codec = platform_get_drvdata(pdev);
208 mfd_remove_devices(&pdev->dev);
209 platform_set_drvdata(pdev, NULL);
211 twl4030_codec_dev = NULL;
216 MODULE_ALIAS("platform:twl4030_codec");
218 static struct platform_driver twl4030_codec_driver = {
219 .probe = twl4030_codec_probe,
220 .remove = __devexit_p(twl4030_codec_remove),
222 .owner = THIS_MODULE,
223 .name = "twl4030_codec",
227 static int __devinit twl4030_codec_init(void)
229 return platform_driver_register(&twl4030_codec_driver);
231 module_init(twl4030_codec_init);
233 static void __devexit twl4030_codec_exit(void)
235 platform_driver_unregister(&twl4030_codec_driver);
237 module_exit(twl4030_codec_exit);
239 MODULE_AUTHOR("Peter Ujfalusi <peter.ujfalusi@nokia.com>");
240 MODULE_LICENSE("GPL");