blob: c02fded316c9f1ce1ee8fd2343b1bfb476399a9f [file] [log] [blame]
Peter Ujfalusi0b83dde2009-10-22 13:26:45 +03001/*
2 * MFD driver for twl4030 codec submodule
3 *
4 * Author: Peter Ujfalusi <peter.ujfalusi@nokia.com>
5 *
6 * Copyright: (C) 2009 Nokia Corporation
7 *
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.
11 *
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.
16 *
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
20 * 02110-1301 USA
21 *
22 */
23
24#include <linux/module.h>
25#include <linux/types.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
Peter Ujfalusi0b83dde2009-10-22 13:26:45 +030027#include <linux/kernel.h>
28#include <linux/fs.h>
29#include <linux/platform_device.h>
Stephen Rothwell8bea8672009-12-15 16:33:10 +110030#include <linux/i2c/twl.h>
Peter Ujfalusi0b83dde2009-10-22 13:26:45 +030031#include <linux/mfd/core.h>
32#include <linux/mfd/twl4030-codec.h>
33
34#define TWL4030_CODEC_CELLS 2
35
36static struct platform_device *twl4030_codec_dev;
37
38struct twl4030_codec_resource {
39 int request_count;
40 u8 reg;
41 u8 mask;
42};
43
44struct twl4030_codec {
Peter Ujfalusif9b46392009-11-04 09:58:19 +020045 unsigned int audio_mclk;
Peter Ujfalusi0b83dde2009-10-22 13:26:45 +030046 struct mutex mutex;
47 struct twl4030_codec_resource resource[TWL4030_CODEC_RES_MAX];
48 struct mfd_cell cells[TWL4030_CODEC_CELLS];
49};
50
51/*
52 * Modify the resource, the function returns the content of the register
53 * after the modification.
54 */
55static int twl4030_codec_set_resource(enum twl4030_codec_res id, int enable)
56{
57 struct twl4030_codec *codec = platform_get_drvdata(twl4030_codec_dev);
58 u8 val;
59
Stephen Rothwell8bea8672009-12-15 16:33:10 +110060 twl_i2c_read_u8(TWL4030_MODULE_AUDIO_VOICE, &val,
Peter Ujfalusi0b83dde2009-10-22 13:26:45 +030061 codec->resource[id].reg);
62
63 if (enable)
64 val |= codec->resource[id].mask;
65 else
66 val &= ~codec->resource[id].mask;
67
Stephen Rothwell8bea8672009-12-15 16:33:10 +110068 twl_i2c_write_u8(TWL4030_MODULE_AUDIO_VOICE,
Peter Ujfalusi0b83dde2009-10-22 13:26:45 +030069 val, codec->resource[id].reg);
70
71 return val;
72}
73
74static inline int twl4030_codec_get_resource(enum twl4030_codec_res id)
75{
76 struct twl4030_codec *codec = platform_get_drvdata(twl4030_codec_dev);
77 u8 val;
78
Stephen Rothwell8bea8672009-12-15 16:33:10 +110079 twl_i2c_read_u8(TWL4030_MODULE_AUDIO_VOICE, &val,
Peter Ujfalusi0b83dde2009-10-22 13:26:45 +030080 codec->resource[id].reg);
81
82 return val;
83}
84
85/*
86 * Enable the resource.
87 * The function returns with error or the content of the register
88 */
89int twl4030_codec_enable_resource(enum twl4030_codec_res id)
90{
91 struct twl4030_codec *codec = platform_get_drvdata(twl4030_codec_dev);
92 int val;
93
94 if (id >= TWL4030_CODEC_RES_MAX) {
95 dev_err(&twl4030_codec_dev->dev,
96 "Invalid resource ID (%u)\n", id);
97 return -EINVAL;
98 }
99
100 mutex_lock(&codec->mutex);
101 if (!codec->resource[id].request_count)
102 /* Resource was disabled, enable it */
103 val = twl4030_codec_set_resource(id, 1);
104 else
105 val = twl4030_codec_get_resource(id);
106
107 codec->resource[id].request_count++;
108 mutex_unlock(&codec->mutex);
109
110 return val;
111}
112EXPORT_SYMBOL_GPL(twl4030_codec_enable_resource);
113
114/*
115 * Disable the resource.
116 * The function returns with error or the content of the register
117 */
118int twl4030_codec_disable_resource(unsigned id)
119{
120 struct twl4030_codec *codec = platform_get_drvdata(twl4030_codec_dev);
121 int val;
122
123 if (id >= TWL4030_CODEC_RES_MAX) {
124 dev_err(&twl4030_codec_dev->dev,
125 "Invalid resource ID (%u)\n", id);
126 return -EINVAL;
127 }
128
129 mutex_lock(&codec->mutex);
130 if (!codec->resource[id].request_count) {
131 dev_err(&twl4030_codec_dev->dev,
132 "Resource has been disabled already (%u)\n", id);
133 mutex_unlock(&codec->mutex);
134 return -EPERM;
135 }
136 codec->resource[id].request_count--;
137
138 if (!codec->resource[id].request_count)
139 /* Resource can be disabled now */
140 val = twl4030_codec_set_resource(id, 0);
141 else
142 val = twl4030_codec_get_resource(id);
143
144 mutex_unlock(&codec->mutex);
145
146 return val;
147}
148EXPORT_SYMBOL_GPL(twl4030_codec_disable_resource);
149
Peter Ujfalusif9b46392009-11-04 09:58:19 +0200150unsigned int twl4030_codec_get_mclk(void)
151{
152 struct twl4030_codec *codec = platform_get_drvdata(twl4030_codec_dev);
153
154 return codec->audio_mclk;
155}
156EXPORT_SYMBOL_GPL(twl4030_codec_get_mclk);
157
Peter Ujfalusi0b83dde2009-10-22 13:26:45 +0300158static int __devinit twl4030_codec_probe(struct platform_device *pdev)
159{
160 struct twl4030_codec *codec;
161 struct twl4030_codec_data *pdata = pdev->dev.platform_data;
162 struct mfd_cell *cell = NULL;
163 int ret, childs = 0;
Peter Ujfalusif9b46392009-11-04 09:58:19 +0200164 u8 val;
165
166 if (!pdata) {
167 dev_err(&pdev->dev, "Platform data is missing\n");
168 return -EINVAL;
169 }
170
171 /* Configure APLL_INFREQ and disable APLL if enabled */
172 val = 0;
173 switch (pdata->audio_mclk) {
174 case 19200000:
175 val |= TWL4030_APLL_INFREQ_19200KHZ;
176 break;
177 case 26000000:
178 val |= TWL4030_APLL_INFREQ_26000KHZ;
179 break;
180 case 38400000:
181 val |= TWL4030_APLL_INFREQ_38400KHZ;
182 break;
183 default:
184 dev_err(&pdev->dev, "Invalid audio_mclk\n");
185 return -EINVAL;
186 }
Stephen Rothwell8bea8672009-12-15 16:33:10 +1100187 twl_i2c_write_u8(TWL4030_MODULE_AUDIO_VOICE,
Peter Ujfalusif9b46392009-11-04 09:58:19 +0200188 val, TWL4030_REG_APLL_CTL);
Peter Ujfalusi0b83dde2009-10-22 13:26:45 +0300189
190 codec = kzalloc(sizeof(struct twl4030_codec), GFP_KERNEL);
191 if (!codec)
192 return -ENOMEM;
193
194 platform_set_drvdata(pdev, codec);
195
196 twl4030_codec_dev = pdev;
197 mutex_init(&codec->mutex);
Peter Ujfalusif9b46392009-11-04 09:58:19 +0200198 codec->audio_mclk = pdata->audio_mclk;
Peter Ujfalusi0b83dde2009-10-22 13:26:45 +0300199
200 /* Codec power */
201 codec->resource[TWL4030_CODEC_RES_POWER].reg = TWL4030_REG_CODEC_MODE;
202 codec->resource[TWL4030_CODEC_RES_POWER].mask = TWL4030_CODECPDZ;
203
204 /* PLL */
205 codec->resource[TWL4030_CODEC_RES_APLL].reg = TWL4030_REG_APLL_CTL;
206 codec->resource[TWL4030_CODEC_RES_APLL].mask = TWL4030_APLL_EN;
207
208 if (pdata->audio) {
209 cell = &codec->cells[childs];
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000210 cell->name = "twl4030-codec";
Andres Salomon65e52352011-02-17 19:07:25 -0800211 cell->mfd_data = pdata->audio;
Peter Ujfalusi0b83dde2009-10-22 13:26:45 +0300212 childs++;
213 }
214 if (pdata->vibra) {
215 cell = &codec->cells[childs];
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000216 cell->name = "twl4030-vibra";
Andres Salomon65e52352011-02-17 19:07:25 -0800217 cell->mfd_data = pdata->vibra;
Peter Ujfalusi0b83dde2009-10-22 13:26:45 +0300218 childs++;
219 }
220
221 if (childs)
222 ret = mfd_add_devices(&pdev->dev, pdev->id, codec->cells,
223 childs, NULL, 0);
224 else {
225 dev_err(&pdev->dev, "No platform data found for childs\n");
226 ret = -ENODEV;
227 }
228
229 if (!ret)
230 return 0;
231
232 platform_set_drvdata(pdev, NULL);
233 kfree(codec);
234 twl4030_codec_dev = NULL;
235 return ret;
236}
237
238static int __devexit twl4030_codec_remove(struct platform_device *pdev)
239{
240 struct twl4030_codec *codec = platform_get_drvdata(pdev);
241
242 mfd_remove_devices(&pdev->dev);
243 platform_set_drvdata(pdev, NULL);
244 kfree(codec);
245 twl4030_codec_dev = NULL;
246
247 return 0;
248}
249
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000250MODULE_ALIAS("platform:twl4030-audio");
Peter Ujfalusi0b83dde2009-10-22 13:26:45 +0300251
252static struct platform_driver twl4030_codec_driver = {
253 .probe = twl4030_codec_probe,
254 .remove = __devexit_p(twl4030_codec_remove),
255 .driver = {
256 .owner = THIS_MODULE,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000257 .name = "twl4030-audio",
Peter Ujfalusi0b83dde2009-10-22 13:26:45 +0300258 },
259};
260
261static int __devinit twl4030_codec_init(void)
262{
263 return platform_driver_register(&twl4030_codec_driver);
264}
265module_init(twl4030_codec_init);
266
267static void __devexit twl4030_codec_exit(void)
268{
269 platform_driver_unregister(&twl4030_codec_driver);
270}
271module_exit(twl4030_codec_exit);
272
273MODULE_AUTHOR("Peter Ujfalusi <peter.ujfalusi@nokia.com>");
274MODULE_LICENSE("GPL");
275