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/slab.h>
27 #include <linux/kernel.h>
29 #include <linux/platform_device.h>
30 #include <linux/i2c/twl.h>
31 #include <linux/mfd/core.h>
32 #include <linux/mfd/twl4030-codec.h>
34 #define TWL4030_CODEC_CELLS 2
36 static struct platform_device
*twl4030_codec_dev
;
38 struct twl4030_codec_resource
{
44 struct twl4030_codec
{
45 unsigned int audio_mclk
;
47 struct twl4030_codec_resource resource
[TWL4030_CODEC_RES_MAX
];
48 struct mfd_cell cells
[TWL4030_CODEC_CELLS
];
52 * Modify the resource, the function returns the content of the register
53 * after the modification.
55 static int twl4030_codec_set_resource(enum twl4030_codec_res id
, int enable
)
57 struct twl4030_codec
*codec
= platform_get_drvdata(twl4030_codec_dev
);
60 twl_i2c_read_u8(TWL4030_MODULE_AUDIO_VOICE
, &val
,
61 codec
->resource
[id
].reg
);
64 val
|= codec
->resource
[id
].mask
;
66 val
&= ~codec
->resource
[id
].mask
;
68 twl_i2c_write_u8(TWL4030_MODULE_AUDIO_VOICE
,
69 val
, codec
->resource
[id
].reg
);
74 static inline int twl4030_codec_get_resource(enum twl4030_codec_res id
)
76 struct twl4030_codec
*codec
= platform_get_drvdata(twl4030_codec_dev
);
79 twl_i2c_read_u8(TWL4030_MODULE_AUDIO_VOICE
, &val
,
80 codec
->resource
[id
].reg
);
86 * Enable the resource.
87 * The function returns with error or the content of the register
89 int twl4030_codec_enable_resource(enum twl4030_codec_res id
)
91 struct twl4030_codec
*codec
= platform_get_drvdata(twl4030_codec_dev
);
94 if (id
>= TWL4030_CODEC_RES_MAX
) {
95 dev_err(&twl4030_codec_dev
->dev
,
96 "Invalid resource ID (%u)\n", id
);
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);
105 val
= twl4030_codec_get_resource(id
);
107 codec
->resource
[id
].request_count
++;
108 mutex_unlock(&codec
->mutex
);
112 EXPORT_SYMBOL_GPL(twl4030_codec_enable_resource
);
115 * Disable the resource.
116 * The function returns with error or the content of the register
118 int twl4030_codec_disable_resource(unsigned id
)
120 struct twl4030_codec
*codec
= platform_get_drvdata(twl4030_codec_dev
);
123 if (id
>= TWL4030_CODEC_RES_MAX
) {
124 dev_err(&twl4030_codec_dev
->dev
,
125 "Invalid resource ID (%u)\n", id
);
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
);
136 codec
->resource
[id
].request_count
--;
138 if (!codec
->resource
[id
].request_count
)
139 /* Resource can be disabled now */
140 val
= twl4030_codec_set_resource(id
, 0);
142 val
= twl4030_codec_get_resource(id
);
144 mutex_unlock(&codec
->mutex
);
148 EXPORT_SYMBOL_GPL(twl4030_codec_disable_resource
);
150 unsigned int twl4030_codec_get_mclk(void)
152 struct twl4030_codec
*codec
= platform_get_drvdata(twl4030_codec_dev
);
154 return codec
->audio_mclk
;
156 EXPORT_SYMBOL_GPL(twl4030_codec_get_mclk
);
158 static int __devinit
twl4030_codec_probe(struct platform_device
*pdev
)
160 struct twl4030_codec
*codec
;
161 struct twl4030_codec_data
*pdata
= pdev
->dev
.platform_data
;
162 struct mfd_cell
*cell
= NULL
;
167 dev_err(&pdev
->dev
, "Platform data is missing\n");
171 /* Configure APLL_INFREQ and disable APLL if enabled */
173 switch (pdata
->audio_mclk
) {
175 val
|= TWL4030_APLL_INFREQ_19200KHZ
;
178 val
|= TWL4030_APLL_INFREQ_26000KHZ
;
181 val
|= TWL4030_APLL_INFREQ_38400KHZ
;
184 dev_err(&pdev
->dev
, "Invalid audio_mclk\n");
187 twl_i2c_write_u8(TWL4030_MODULE_AUDIO_VOICE
,
188 val
, TWL4030_REG_APLL_CTL
);
190 codec
= kzalloc(sizeof(struct twl4030_codec
), GFP_KERNEL
);
194 platform_set_drvdata(pdev
, codec
);
196 twl4030_codec_dev
= pdev
;
197 mutex_init(&codec
->mutex
);
198 codec
->audio_mclk
= pdata
->audio_mclk
;
201 codec
->resource
[TWL4030_CODEC_RES_POWER
].reg
= TWL4030_REG_CODEC_MODE
;
202 codec
->resource
[TWL4030_CODEC_RES_POWER
].mask
= TWL4030_CODECPDZ
;
205 codec
->resource
[TWL4030_CODEC_RES_APLL
].reg
= TWL4030_REG_APLL_CTL
;
206 codec
->resource
[TWL4030_CODEC_RES_APLL
].mask
= TWL4030_APLL_EN
;
209 cell
= &codec
->cells
[childs
];
210 cell
->name
= "twl4030-codec";
211 cell
->platform_data
= pdata
->audio
;
212 cell
->data_size
= sizeof(*pdata
->audio
);
216 cell
= &codec
->cells
[childs
];
217 cell
->name
= "twl4030-vibra";
218 cell
->platform_data
= pdata
->vibra
;
219 cell
->data_size
= sizeof(*pdata
->vibra
);
224 ret
= mfd_add_devices(&pdev
->dev
, pdev
->id
, codec
->cells
,
227 dev_err(&pdev
->dev
, "No platform data found for childs\n");
234 platform_set_drvdata(pdev
, NULL
);
236 twl4030_codec_dev
= NULL
;
240 static int __devexit
twl4030_codec_remove(struct platform_device
*pdev
)
242 struct twl4030_codec
*codec
= platform_get_drvdata(pdev
);
244 mfd_remove_devices(&pdev
->dev
);
245 platform_set_drvdata(pdev
, NULL
);
247 twl4030_codec_dev
= NULL
;
252 MODULE_ALIAS("platform:twl4030-audio");
254 static struct platform_driver twl4030_codec_driver
= {
255 .probe
= twl4030_codec_probe
,
256 .remove
= __devexit_p(twl4030_codec_remove
),
258 .owner
= THIS_MODULE
,
259 .name
= "twl4030-audio",
263 static int __devinit
twl4030_codec_init(void)
265 return platform_driver_register(&twl4030_codec_driver
);
267 module_init(twl4030_codec_init
);
269 static void __devexit
twl4030_codec_exit(void)
271 platform_driver_unregister(&twl4030_codec_driver
);
273 module_exit(twl4030_codec_exit
);
275 MODULE_AUTHOR("Peter Ujfalusi <peter.ujfalusi@nokia.com>");
276 MODULE_LICENSE("GPL");