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>
32 #include <linux/of_platform.h>
33 #include <linux/mfd/twl.h>
34 #include <linux/mfd/core.h>
35 #include <linux/mfd/twl4030-audio.h>
37 #define TWL4030_AUDIO_CELLS 2
39 static struct platform_device
*twl4030_audio_dev
;
41 struct twl4030_audio_resource
{
47 struct twl4030_audio
{
48 unsigned int audio_mclk
;
50 struct twl4030_audio_resource resource
[TWL4030_AUDIO_RES_MAX
];
51 struct mfd_cell cells
[TWL4030_AUDIO_CELLS
];
55 * Modify the resource, the function returns the content of the register
56 * after the modification.
58 static int twl4030_audio_set_resource(enum twl4030_audio_res id
, int enable
)
60 struct twl4030_audio
*audio
= platform_get_drvdata(twl4030_audio_dev
);
63 twl_i2c_read_u8(TWL4030_MODULE_AUDIO_VOICE
, &val
,
64 audio
->resource
[id
].reg
);
67 val
|= audio
->resource
[id
].mask
;
69 val
&= ~audio
->resource
[id
].mask
;
71 twl_i2c_write_u8(TWL4030_MODULE_AUDIO_VOICE
,
72 val
, audio
->resource
[id
].reg
);
77 static inline int twl4030_audio_get_resource(enum twl4030_audio_res id
)
79 struct twl4030_audio
*audio
= platform_get_drvdata(twl4030_audio_dev
);
82 twl_i2c_read_u8(TWL4030_MODULE_AUDIO_VOICE
, &val
,
83 audio
->resource
[id
].reg
);
89 * Enable the resource.
90 * The function returns with error or the content of the register
92 int twl4030_audio_enable_resource(enum twl4030_audio_res id
)
94 struct twl4030_audio
*audio
= platform_get_drvdata(twl4030_audio_dev
);
97 if (id
>= TWL4030_AUDIO_RES_MAX
) {
98 dev_err(&twl4030_audio_dev
->dev
,
99 "Invalid resource ID (%u)\n", id
);
103 mutex_lock(&audio
->mutex
);
104 if (!audio
->resource
[id
].request_count
)
105 /* Resource was disabled, enable it */
106 val
= twl4030_audio_set_resource(id
, 1);
108 val
= twl4030_audio_get_resource(id
);
110 audio
->resource
[id
].request_count
++;
111 mutex_unlock(&audio
->mutex
);
115 EXPORT_SYMBOL_GPL(twl4030_audio_enable_resource
);
118 * Disable the resource.
119 * The function returns with error or the content of the register
121 int twl4030_audio_disable_resource(enum twl4030_audio_res id
)
123 struct twl4030_audio
*audio
= platform_get_drvdata(twl4030_audio_dev
);
126 if (id
>= TWL4030_AUDIO_RES_MAX
) {
127 dev_err(&twl4030_audio_dev
->dev
,
128 "Invalid resource ID (%u)\n", id
);
132 mutex_lock(&audio
->mutex
);
133 if (!audio
->resource
[id
].request_count
) {
134 dev_err(&twl4030_audio_dev
->dev
,
135 "Resource has been disabled already (%u)\n", id
);
136 mutex_unlock(&audio
->mutex
);
139 audio
->resource
[id
].request_count
--;
141 if (!audio
->resource
[id
].request_count
)
142 /* Resource can be disabled now */
143 val
= twl4030_audio_set_resource(id
, 0);
145 val
= twl4030_audio_get_resource(id
);
147 mutex_unlock(&audio
->mutex
);
151 EXPORT_SYMBOL_GPL(twl4030_audio_disable_resource
);
153 unsigned int twl4030_audio_get_mclk(void)
155 struct twl4030_audio
*audio
= platform_get_drvdata(twl4030_audio_dev
);
157 return audio
->audio_mclk
;
159 EXPORT_SYMBOL_GPL(twl4030_audio_get_mclk
);
161 static bool twl4030_audio_has_codec(struct twl4030_audio_data
*pdata
,
162 struct device_node
*parent
)
164 struct device_node
*node
;
166 if (pdata
&& pdata
->codec
)
169 node
= of_get_child_by_name(parent
, "codec");
178 static bool twl4030_audio_has_vibra(struct twl4030_audio_data
*pdata
,
179 struct device_node
*node
)
183 if (pdata
&& pdata
->vibra
)
186 if (!of_property_read_u32(node
, "ti,enable-vibra", &vibra
) && vibra
)
192 static int twl4030_audio_probe(struct platform_device
*pdev
)
194 struct twl4030_audio
*audio
;
195 struct twl4030_audio_data
*pdata
= dev_get_platdata(&pdev
->dev
);
196 struct device_node
*node
= pdev
->dev
.of_node
;
197 struct mfd_cell
*cell
= NULL
;
201 if (!pdata
&& !node
) {
202 dev_err(&pdev
->dev
, "Platform data is missing\n");
206 audio
= devm_kzalloc(&pdev
->dev
, sizeof(struct twl4030_audio
),
211 mutex_init(&audio
->mutex
);
212 audio
->audio_mclk
= twl_get_hfclk_rate();
214 /* Configure APLL_INFREQ and disable APLL if enabled */
215 switch (audio
->audio_mclk
) {
217 val
= TWL4030_APLL_INFREQ_19200KHZ
;
220 val
= TWL4030_APLL_INFREQ_26000KHZ
;
223 val
= TWL4030_APLL_INFREQ_38400KHZ
;
226 dev_err(&pdev
->dev
, "Invalid audio_mclk\n");
229 twl_i2c_write_u8(TWL4030_MODULE_AUDIO_VOICE
, val
, TWL4030_REG_APLL_CTL
);
232 audio
->resource
[TWL4030_AUDIO_RES_POWER
].reg
= TWL4030_REG_CODEC_MODE
;
233 audio
->resource
[TWL4030_AUDIO_RES_POWER
].mask
= TWL4030_CODECPDZ
;
236 audio
->resource
[TWL4030_AUDIO_RES_APLL
].reg
= TWL4030_REG_APLL_CTL
;
237 audio
->resource
[TWL4030_AUDIO_RES_APLL
].mask
= TWL4030_APLL_EN
;
239 if (twl4030_audio_has_codec(pdata
, node
)) {
240 cell
= &audio
->cells
[childs
];
241 cell
->name
= "twl4030-codec";
243 cell
->platform_data
= pdata
->codec
;
244 cell
->pdata_size
= sizeof(*pdata
->codec
);
248 if (twl4030_audio_has_vibra(pdata
, node
)) {
249 cell
= &audio
->cells
[childs
];
250 cell
->name
= "twl4030-vibra";
252 cell
->platform_data
= pdata
->vibra
;
253 cell
->pdata_size
= sizeof(*pdata
->vibra
);
258 platform_set_drvdata(pdev
, audio
);
259 twl4030_audio_dev
= pdev
;
262 ret
= mfd_add_devices(&pdev
->dev
, pdev
->id
, audio
->cells
,
263 childs
, NULL
, 0, NULL
);
265 dev_err(&pdev
->dev
, "No platform data found for childs\n");
270 twl4030_audio_dev
= NULL
;
275 static int twl4030_audio_remove(struct platform_device
*pdev
)
277 mfd_remove_devices(&pdev
->dev
);
278 twl4030_audio_dev
= NULL
;
283 static const struct of_device_id twl4030_audio_of_match
[] = {
284 {.compatible
= "ti,twl4030-audio", },
287 MODULE_DEVICE_TABLE(of
, twl4030_audio_of_match
);
289 static struct platform_driver twl4030_audio_driver
= {
291 .name
= "twl4030-audio",
292 .of_match_table
= twl4030_audio_of_match
,
294 .probe
= twl4030_audio_probe
,
295 .remove
= twl4030_audio_remove
,
298 module_platform_driver(twl4030_audio_driver
);
300 MODULE_AUTHOR("Peter Ujfalusi <peter.ujfalusi@ti.com>");
301 MODULE_DESCRIPTION("TWL4030 audio block MFD driver");
302 MODULE_LICENSE("GPL");
303 MODULE_ALIAS("platform:twl4030-audio");