]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/mfd/twl4030-audio.c
Merge tag 'drm-misc-fixes-2018-01-08' of git://anongit.freedesktop.org/drm/drm-misc...
[mirror_ubuntu-bionic-kernel.git] / drivers / mfd / twl4030-audio.c
CommitLineData
0b83ddeb 1/*
4fe5668b
PU
2 * MFD driver for twl4030 audio submodule, which contains an audio codec, and
3 * the vibra control.
0b83ddeb 4 *
4a7c00cd 5 * Author: Peter Ujfalusi <peter.ujfalusi@ti.com>
0b83ddeb
PU
6 *
7 * Copyright: (C) 2009 Nokia Corporation
8 *
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.
12 *
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.
17 *
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
21 * 02110-1301 USA
22 *
23 */
24
25#include <linux/module.h>
26#include <linux/types.h>
5a0e3ad6 27#include <linux/slab.h>
0b83ddeb
PU
28#include <linux/kernel.h>
29#include <linux/fs.h>
30#include <linux/platform_device.h>
019a7e6b
PU
31#include <linux/of.h>
32#include <linux/of_platform.h>
a2054256 33#include <linux/mfd/twl.h>
0b83ddeb 34#include <linux/mfd/core.h>
57fe7251 35#include <linux/mfd/twl4030-audio.h>
0b83ddeb 36
4fe5668b 37#define TWL4030_AUDIO_CELLS 2
0b83ddeb 38
4fe5668b 39static struct platform_device *twl4030_audio_dev;
0b83ddeb 40
4fe5668b 41struct twl4030_audio_resource {
0b83ddeb
PU
42 int request_count;
43 u8 reg;
44 u8 mask;
45};
46
4fe5668b 47struct twl4030_audio {
f9b4639e 48 unsigned int audio_mclk;
0b83ddeb 49 struct mutex mutex;
57fe7251 50 struct twl4030_audio_resource resource[TWL4030_AUDIO_RES_MAX];
4fe5668b 51 struct mfd_cell cells[TWL4030_AUDIO_CELLS];
0b83ddeb
PU
52};
53
54/*
55 * Modify the resource, the function returns the content of the register
56 * after the modification.
57 */
57fe7251 58static int twl4030_audio_set_resource(enum twl4030_audio_res id, int enable)
0b83ddeb 59{
4fe5668b 60 struct twl4030_audio *audio = platform_get_drvdata(twl4030_audio_dev);
0b83ddeb
PU
61 u8 val;
62
8bea8672 63 twl_i2c_read_u8(TWL4030_MODULE_AUDIO_VOICE, &val,
4fe5668b 64 audio->resource[id].reg);
0b83ddeb
PU
65
66 if (enable)
4fe5668b 67 val |= audio->resource[id].mask;
0b83ddeb 68 else
4fe5668b 69 val &= ~audio->resource[id].mask;
0b83ddeb 70
8bea8672 71 twl_i2c_write_u8(TWL4030_MODULE_AUDIO_VOICE,
4fe5668b 72 val, audio->resource[id].reg);
0b83ddeb
PU
73
74 return val;
75}
76
57fe7251 77static inline int twl4030_audio_get_resource(enum twl4030_audio_res id)
0b83ddeb 78{
4fe5668b 79 struct twl4030_audio *audio = platform_get_drvdata(twl4030_audio_dev);
0b83ddeb
PU
80 u8 val;
81
8bea8672 82 twl_i2c_read_u8(TWL4030_MODULE_AUDIO_VOICE, &val,
4fe5668b 83 audio->resource[id].reg);
0b83ddeb
PU
84
85 return val;
86}
87
88/*
89 * Enable the resource.
90 * The function returns with error or the content of the register
91 */
57fe7251 92int twl4030_audio_enable_resource(enum twl4030_audio_res id)
0b83ddeb 93{
4fe5668b 94 struct twl4030_audio *audio = platform_get_drvdata(twl4030_audio_dev);
0b83ddeb
PU
95 int val;
96
57fe7251 97 if (id >= TWL4030_AUDIO_RES_MAX) {
4fe5668b 98 dev_err(&twl4030_audio_dev->dev,
0b83ddeb
PU
99 "Invalid resource ID (%u)\n", id);
100 return -EINVAL;
101 }
102
4fe5668b
PU
103 mutex_lock(&audio->mutex);
104 if (!audio->resource[id].request_count)
0b83ddeb 105 /* Resource was disabled, enable it */
4fe5668b 106 val = twl4030_audio_set_resource(id, 1);
0b83ddeb 107 else
4fe5668b 108 val = twl4030_audio_get_resource(id);
0b83ddeb 109
4fe5668b
PU
110 audio->resource[id].request_count++;
111 mutex_unlock(&audio->mutex);
0b83ddeb
PU
112
113 return val;
114}
57fe7251 115EXPORT_SYMBOL_GPL(twl4030_audio_enable_resource);
0b83ddeb
PU
116
117/*
118 * Disable the resource.
119 * The function returns with error or the content of the register
120 */
6049bcef 121int twl4030_audio_disable_resource(enum twl4030_audio_res id)
0b83ddeb 122{
4fe5668b 123 struct twl4030_audio *audio = platform_get_drvdata(twl4030_audio_dev);
0b83ddeb
PU
124 int val;
125
57fe7251 126 if (id >= TWL4030_AUDIO_RES_MAX) {
4fe5668b 127 dev_err(&twl4030_audio_dev->dev,
0b83ddeb
PU
128 "Invalid resource ID (%u)\n", id);
129 return -EINVAL;
130 }
131
4fe5668b
PU
132 mutex_lock(&audio->mutex);
133 if (!audio->resource[id].request_count) {
134 dev_err(&twl4030_audio_dev->dev,
0b83ddeb 135 "Resource has been disabled already (%u)\n", id);
4fe5668b 136 mutex_unlock(&audio->mutex);
0b83ddeb
PU
137 return -EPERM;
138 }
4fe5668b 139 audio->resource[id].request_count--;
0b83ddeb 140
4fe5668b 141 if (!audio->resource[id].request_count)
0b83ddeb 142 /* Resource can be disabled now */
4fe5668b 143 val = twl4030_audio_set_resource(id, 0);
0b83ddeb 144 else
4fe5668b 145 val = twl4030_audio_get_resource(id);
0b83ddeb 146
4fe5668b 147 mutex_unlock(&audio->mutex);
0b83ddeb
PU
148
149 return val;
150}
57fe7251 151EXPORT_SYMBOL_GPL(twl4030_audio_disable_resource);
0b83ddeb 152
57fe7251 153unsigned int twl4030_audio_get_mclk(void)
f9b4639e 154{
4fe5668b 155 struct twl4030_audio *audio = platform_get_drvdata(twl4030_audio_dev);
f9b4639e 156
4fe5668b 157 return audio->audio_mclk;
f9b4639e 158}
57fe7251 159EXPORT_SYMBOL_GPL(twl4030_audio_get_mclk);
f9b4639e 160
019a7e6b 161static bool twl4030_audio_has_codec(struct twl4030_audio_data *pdata,
0a423772 162 struct device_node *parent)
019a7e6b 163{
0a423772
JH
164 struct device_node *node;
165
019a7e6b
PU
166 if (pdata && pdata->codec)
167 return true;
168
0a423772
JH
169 node = of_get_child_by_name(parent, "codec");
170 if (node) {
171 of_node_put(node);
019a7e6b 172 return true;
0a423772 173 }
019a7e6b
PU
174
175 return false;
176}
177
178static bool twl4030_audio_has_vibra(struct twl4030_audio_data *pdata,
179 struct device_node *node)
180{
181 int vibra;
182
183 if (pdata && pdata->vibra)
184 return true;
185
186 if (!of_property_read_u32(node, "ti,enable-vibra", &vibra) && vibra)
187 return true;
188
189 return false;
190}
191
f791be49 192static int twl4030_audio_probe(struct platform_device *pdev)
0b83ddeb 193{
4fe5668b 194 struct twl4030_audio *audio;
334a41ce 195 struct twl4030_audio_data *pdata = dev_get_platdata(&pdev->dev);
019a7e6b 196 struct device_node *node = pdev->dev.of_node;
0b83ddeb
PU
197 struct mfd_cell *cell = NULL;
198 int ret, childs = 0;
f9b4639e
PU
199 u8 val;
200
019a7e6b 201 if (!pdata && !node) {
f9b4639e
PU
202 dev_err(&pdev->dev, "Platform data is missing\n");
203 return -EINVAL;
204 }
205
cdf4b670
PU
206 audio = devm_kzalloc(&pdev->dev, sizeof(struct twl4030_audio),
207 GFP_KERNEL);
208 if (!audio)
209 return -ENOMEM;
210
211 mutex_init(&audio->mutex);
c531241d 212 audio->audio_mclk = twl_get_hfclk_rate();
cdf4b670 213
f9b4639e 214 /* Configure APLL_INFREQ and disable APLL if enabled */
cdf4b670 215 switch (audio->audio_mclk) {
f9b4639e 216 case 19200000:
cdf4b670 217 val = TWL4030_APLL_INFREQ_19200KHZ;
f9b4639e
PU
218 break;
219 case 26000000:
cdf4b670 220 val = TWL4030_APLL_INFREQ_26000KHZ;
f9b4639e
PU
221 break;
222 case 38400000:
cdf4b670 223 val = TWL4030_APLL_INFREQ_38400KHZ;
f9b4639e
PU
224 break;
225 default:
226 dev_err(&pdev->dev, "Invalid audio_mclk\n");
227 return -EINVAL;
228 }
cdf4b670 229 twl_i2c_write_u8(TWL4030_MODULE_AUDIO_VOICE, val, TWL4030_REG_APLL_CTL);
0b83ddeb
PU
230
231 /* Codec power */
57fe7251
PU
232 audio->resource[TWL4030_AUDIO_RES_POWER].reg = TWL4030_REG_CODEC_MODE;
233 audio->resource[TWL4030_AUDIO_RES_POWER].mask = TWL4030_CODECPDZ;
0b83ddeb
PU
234
235 /* PLL */
57fe7251
PU
236 audio->resource[TWL4030_AUDIO_RES_APLL].reg = TWL4030_REG_APLL_CTL;
237 audio->resource[TWL4030_AUDIO_RES_APLL].mask = TWL4030_APLL_EN;
0b83ddeb 238
019a7e6b 239 if (twl4030_audio_has_codec(pdata, node)) {
4fe5668b 240 cell = &audio->cells[childs];
f0fba2ad 241 cell->name = "twl4030-codec";
019a7e6b
PU
242 if (pdata) {
243 cell->platform_data = pdata->codec;
244 cell->pdata_size = sizeof(*pdata->codec);
245 }
0b83ddeb
PU
246 childs++;
247 }
019a7e6b 248 if (twl4030_audio_has_vibra(pdata, node)) {
4fe5668b 249 cell = &audio->cells[childs];
f0fba2ad 250 cell->name = "twl4030-vibra";
019a7e6b
PU
251 if (pdata) {
252 cell->platform_data = pdata->vibra;
253 cell->pdata_size = sizeof(*pdata->vibra);
254 }
0b83ddeb
PU
255 childs++;
256 }
257
cdf4b670
PU
258 platform_set_drvdata(pdev, audio);
259 twl4030_audio_dev = pdev;
260
0b83ddeb 261 if (childs)
4fe5668b 262 ret = mfd_add_devices(&pdev->dev, pdev->id, audio->cells,
55692af5 263 childs, NULL, 0, NULL);
0b83ddeb
PU
264 else {
265 dev_err(&pdev->dev, "No platform data found for childs\n");
266 ret = -ENODEV;
267 }
268
96ac2d1a 269 if (ret)
39c1421d 270 twl4030_audio_dev = NULL;
0b83ddeb 271
0b83ddeb
PU
272 return ret;
273}
274
4740f73f 275static int twl4030_audio_remove(struct platform_device *pdev)
0b83ddeb 276{
0b83ddeb 277 mfd_remove_devices(&pdev->dev);
4fe5668b 278 twl4030_audio_dev = NULL;
0b83ddeb
PU
279
280 return 0;
281}
282
019a7e6b
PU
283static const struct of_device_id twl4030_audio_of_match[] = {
284 {.compatible = "ti,twl4030-audio", },
285 { },
286};
287MODULE_DEVICE_TABLE(of, twl4030_audio_of_match);
288
4fe5668b 289static struct platform_driver twl4030_audio_driver = {
0b83ddeb 290 .driver = {
f0fba2ad 291 .name = "twl4030-audio",
019a7e6b 292 .of_match_table = twl4030_audio_of_match,
0b83ddeb 293 },
41569a16 294 .probe = twl4030_audio_probe,
84449216 295 .remove = twl4030_audio_remove,
0b83ddeb
PU
296};
297
65349d60 298module_platform_driver(twl4030_audio_driver);
0b83ddeb 299
4a7c00cd 300MODULE_AUTHOR("Peter Ujfalusi <peter.ujfalusi@ti.com>");
41569a16 301MODULE_DESCRIPTION("TWL4030 audio block MFD driver");
0b83ddeb 302MODULE_LICENSE("GPL");
41569a16 303MODULE_ALIAS("platform:twl4030-audio");