blob: cc832d309599a0b6985e747c8546e80d40f121e7 [file] [log] [blame]
Peter Ujfalusi0b83dde2009-10-22 13:26:45 +03001/*
Peter Ujfalusi4fe56682011-05-03 18:03:43 +03002 * MFD driver for twl4030 audio submodule, which contains an audio codec, and
3 * the vibra control.
Peter Ujfalusi0b83dde2009-10-22 13:26:45 +03004 *
Peter Ujfalusi4a7c00c2011-05-10 08:59:23 +03005 * Author: Peter Ujfalusi <peter.ujfalusi@ti.com>
Peter Ujfalusi0b83dde2009-10-22 13:26:45 +03006 *
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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090027#include <linux/slab.h>
Peter Ujfalusi0b83dde2009-10-22 13:26:45 +030028#include <linux/kernel.h>
29#include <linux/fs.h>
30#include <linux/platform_device.h>
Peter Ujfalusi019a7e62012-09-10 13:46:25 +030031#include <linux/of.h>
32#include <linux/of_platform.h>
Stephen Rothwell8bea8672009-12-15 16:33:10 +110033#include <linux/i2c/twl.h>
Peter Ujfalusi0b83dde2009-10-22 13:26:45 +030034#include <linux/mfd/core.h>
Peter Ujfalusi57fe7252011-05-31 12:02:49 +030035#include <linux/mfd/twl4030-audio.h>
Peter Ujfalusi0b83dde2009-10-22 13:26:45 +030036
Peter Ujfalusi4fe56682011-05-03 18:03:43 +030037#define TWL4030_AUDIO_CELLS 2
Peter Ujfalusi0b83dde2009-10-22 13:26:45 +030038
Peter Ujfalusi4fe56682011-05-03 18:03:43 +030039static struct platform_device *twl4030_audio_dev;
Peter Ujfalusi0b83dde2009-10-22 13:26:45 +030040
Peter Ujfalusi4fe56682011-05-03 18:03:43 +030041struct twl4030_audio_resource {
Peter Ujfalusi0b83dde2009-10-22 13:26:45 +030042 int request_count;
43 u8 reg;
44 u8 mask;
45};
46
Peter Ujfalusi4fe56682011-05-03 18:03:43 +030047struct twl4030_audio {
Peter Ujfalusif9b46392009-11-04 09:58:19 +020048 unsigned int audio_mclk;
Peter Ujfalusi0b83dde2009-10-22 13:26:45 +030049 struct mutex mutex;
Peter Ujfalusi57fe7252011-05-31 12:02:49 +030050 struct twl4030_audio_resource resource[TWL4030_AUDIO_RES_MAX];
Peter Ujfalusi4fe56682011-05-03 18:03:43 +030051 struct mfd_cell cells[TWL4030_AUDIO_CELLS];
Peter Ujfalusi0b83dde2009-10-22 13:26:45 +030052};
53
54/*
55 * Modify the resource, the function returns the content of the register
56 * after the modification.
57 */
Peter Ujfalusi57fe7252011-05-31 12:02:49 +030058static int twl4030_audio_set_resource(enum twl4030_audio_res id, int enable)
Peter Ujfalusi0b83dde2009-10-22 13:26:45 +030059{
Peter Ujfalusi4fe56682011-05-03 18:03:43 +030060 struct twl4030_audio *audio = platform_get_drvdata(twl4030_audio_dev);
Peter Ujfalusi0b83dde2009-10-22 13:26:45 +030061 u8 val;
62
Stephen Rothwell8bea8672009-12-15 16:33:10 +110063 twl_i2c_read_u8(TWL4030_MODULE_AUDIO_VOICE, &val,
Peter Ujfalusi4fe56682011-05-03 18:03:43 +030064 audio->resource[id].reg);
Peter Ujfalusi0b83dde2009-10-22 13:26:45 +030065
66 if (enable)
Peter Ujfalusi4fe56682011-05-03 18:03:43 +030067 val |= audio->resource[id].mask;
Peter Ujfalusi0b83dde2009-10-22 13:26:45 +030068 else
Peter Ujfalusi4fe56682011-05-03 18:03:43 +030069 val &= ~audio->resource[id].mask;
Peter Ujfalusi0b83dde2009-10-22 13:26:45 +030070
Stephen Rothwell8bea8672009-12-15 16:33:10 +110071 twl_i2c_write_u8(TWL4030_MODULE_AUDIO_VOICE,
Peter Ujfalusi4fe56682011-05-03 18:03:43 +030072 val, audio->resource[id].reg);
Peter Ujfalusi0b83dde2009-10-22 13:26:45 +030073
74 return val;
75}
76
Peter Ujfalusi57fe7252011-05-31 12:02:49 +030077static inline int twl4030_audio_get_resource(enum twl4030_audio_res id)
Peter Ujfalusi0b83dde2009-10-22 13:26:45 +030078{
Peter Ujfalusi4fe56682011-05-03 18:03:43 +030079 struct twl4030_audio *audio = platform_get_drvdata(twl4030_audio_dev);
Peter Ujfalusi0b83dde2009-10-22 13:26:45 +030080 u8 val;
81
Stephen Rothwell8bea8672009-12-15 16:33:10 +110082 twl_i2c_read_u8(TWL4030_MODULE_AUDIO_VOICE, &val,
Peter Ujfalusi4fe56682011-05-03 18:03:43 +030083 audio->resource[id].reg);
Peter Ujfalusi0b83dde2009-10-22 13:26:45 +030084
85 return val;
86}
87
88/*
89 * Enable the resource.
90 * The function returns with error or the content of the register
91 */
Peter Ujfalusi57fe7252011-05-31 12:02:49 +030092int twl4030_audio_enable_resource(enum twl4030_audio_res id)
Peter Ujfalusi0b83dde2009-10-22 13:26:45 +030093{
Peter Ujfalusi4fe56682011-05-03 18:03:43 +030094 struct twl4030_audio *audio = platform_get_drvdata(twl4030_audio_dev);
Peter Ujfalusi0b83dde2009-10-22 13:26:45 +030095 int val;
96
Peter Ujfalusi57fe7252011-05-31 12:02:49 +030097 if (id >= TWL4030_AUDIO_RES_MAX) {
Peter Ujfalusi4fe56682011-05-03 18:03:43 +030098 dev_err(&twl4030_audio_dev->dev,
Peter Ujfalusi0b83dde2009-10-22 13:26:45 +030099 "Invalid resource ID (%u)\n", id);
100 return -EINVAL;
101 }
102
Peter Ujfalusi4fe56682011-05-03 18:03:43 +0300103 mutex_lock(&audio->mutex);
104 if (!audio->resource[id].request_count)
Peter Ujfalusi0b83dde2009-10-22 13:26:45 +0300105 /* Resource was disabled, enable it */
Peter Ujfalusi4fe56682011-05-03 18:03:43 +0300106 val = twl4030_audio_set_resource(id, 1);
Peter Ujfalusi0b83dde2009-10-22 13:26:45 +0300107 else
Peter Ujfalusi4fe56682011-05-03 18:03:43 +0300108 val = twl4030_audio_get_resource(id);
Peter Ujfalusi0b83dde2009-10-22 13:26:45 +0300109
Peter Ujfalusi4fe56682011-05-03 18:03:43 +0300110 audio->resource[id].request_count++;
111 mutex_unlock(&audio->mutex);
Peter Ujfalusi0b83dde2009-10-22 13:26:45 +0300112
113 return val;
114}
Peter Ujfalusi57fe7252011-05-31 12:02:49 +0300115EXPORT_SYMBOL_GPL(twl4030_audio_enable_resource);
Peter Ujfalusi0b83dde2009-10-22 13:26:45 +0300116
117/*
118 * Disable the resource.
119 * The function returns with error or the content of the register
120 */
Mark Brown6049bce2013-03-02 15:30:35 +0800121int twl4030_audio_disable_resource(enum twl4030_audio_res id)
Peter Ujfalusi0b83dde2009-10-22 13:26:45 +0300122{
Peter Ujfalusi4fe56682011-05-03 18:03:43 +0300123 struct twl4030_audio *audio = platform_get_drvdata(twl4030_audio_dev);
Peter Ujfalusi0b83dde2009-10-22 13:26:45 +0300124 int val;
125
Peter Ujfalusi57fe7252011-05-31 12:02:49 +0300126 if (id >= TWL4030_AUDIO_RES_MAX) {
Peter Ujfalusi4fe56682011-05-03 18:03:43 +0300127 dev_err(&twl4030_audio_dev->dev,
Peter Ujfalusi0b83dde2009-10-22 13:26:45 +0300128 "Invalid resource ID (%u)\n", id);
129 return -EINVAL;
130 }
131
Peter Ujfalusi4fe56682011-05-03 18:03:43 +0300132 mutex_lock(&audio->mutex);
133 if (!audio->resource[id].request_count) {
134 dev_err(&twl4030_audio_dev->dev,
Peter Ujfalusi0b83dde2009-10-22 13:26:45 +0300135 "Resource has been disabled already (%u)\n", id);
Peter Ujfalusi4fe56682011-05-03 18:03:43 +0300136 mutex_unlock(&audio->mutex);
Peter Ujfalusi0b83dde2009-10-22 13:26:45 +0300137 return -EPERM;
138 }
Peter Ujfalusi4fe56682011-05-03 18:03:43 +0300139 audio->resource[id].request_count--;
Peter Ujfalusi0b83dde2009-10-22 13:26:45 +0300140
Peter Ujfalusi4fe56682011-05-03 18:03:43 +0300141 if (!audio->resource[id].request_count)
Peter Ujfalusi0b83dde2009-10-22 13:26:45 +0300142 /* Resource can be disabled now */
Peter Ujfalusi4fe56682011-05-03 18:03:43 +0300143 val = twl4030_audio_set_resource(id, 0);
Peter Ujfalusi0b83dde2009-10-22 13:26:45 +0300144 else
Peter Ujfalusi4fe56682011-05-03 18:03:43 +0300145 val = twl4030_audio_get_resource(id);
Peter Ujfalusi0b83dde2009-10-22 13:26:45 +0300146
Peter Ujfalusi4fe56682011-05-03 18:03:43 +0300147 mutex_unlock(&audio->mutex);
Peter Ujfalusi0b83dde2009-10-22 13:26:45 +0300148
149 return val;
150}
Peter Ujfalusi57fe7252011-05-31 12:02:49 +0300151EXPORT_SYMBOL_GPL(twl4030_audio_disable_resource);
Peter Ujfalusi0b83dde2009-10-22 13:26:45 +0300152
Peter Ujfalusi57fe7252011-05-31 12:02:49 +0300153unsigned int twl4030_audio_get_mclk(void)
Peter Ujfalusif9b46392009-11-04 09:58:19 +0200154{
Peter Ujfalusi4fe56682011-05-03 18:03:43 +0300155 struct twl4030_audio *audio = platform_get_drvdata(twl4030_audio_dev);
Peter Ujfalusif9b46392009-11-04 09:58:19 +0200156
Peter Ujfalusi4fe56682011-05-03 18:03:43 +0300157 return audio->audio_mclk;
Peter Ujfalusif9b46392009-11-04 09:58:19 +0200158}
Peter Ujfalusi57fe7252011-05-31 12:02:49 +0300159EXPORT_SYMBOL_GPL(twl4030_audio_get_mclk);
Peter Ujfalusif9b46392009-11-04 09:58:19 +0200160
Peter Ujfalusi019a7e62012-09-10 13:46:25 +0300161static bool twl4030_audio_has_codec(struct twl4030_audio_data *pdata,
Johan Hovoldf4c07962017-11-11 16:38:43 +0100162 struct device_node *parent)
Peter Ujfalusi019a7e62012-09-10 13:46:25 +0300163{
Johan Hovoldf4c07962017-11-11 16:38:43 +0100164 struct device_node *node;
165
Peter Ujfalusi019a7e62012-09-10 13:46:25 +0300166 if (pdata && pdata->codec)
167 return true;
168
Johan Hovoldf4c07962017-11-11 16:38:43 +0100169 node = of_get_child_by_name(parent, "codec");
170 if (node) {
171 of_node_put(node);
Peter Ujfalusi019a7e62012-09-10 13:46:25 +0300172 return true;
Johan Hovoldf4c07962017-11-11 16:38:43 +0100173 }
Peter Ujfalusi019a7e62012-09-10 13:46:25 +0300174
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
Bill Pembertonf791be42012-11-19 13:23:04 -0500192static int twl4030_audio_probe(struct platform_device *pdev)
Peter Ujfalusi0b83dde2009-10-22 13:26:45 +0300193{
Peter Ujfalusi4fe56682011-05-03 18:03:43 +0300194 struct twl4030_audio *audio;
Jingoo Han334a41c2013-07-30 17:10:05 +0900195 struct twl4030_audio_data *pdata = dev_get_platdata(&pdev->dev);
Peter Ujfalusi019a7e62012-09-10 13:46:25 +0300196 struct device_node *node = pdev->dev.of_node;
Peter Ujfalusi0b83dde2009-10-22 13:26:45 +0300197 struct mfd_cell *cell = NULL;
198 int ret, childs = 0;
Peter Ujfalusif9b46392009-11-04 09:58:19 +0200199 u8 val;
200
Peter Ujfalusi019a7e62012-09-10 13:46:25 +0300201 if (!pdata && !node) {
Peter Ujfalusif9b46392009-11-04 09:58:19 +0200202 dev_err(&pdev->dev, "Platform data is missing\n");
203 return -EINVAL;
204 }
205
Peter Ujfalusi39c14212012-09-10 13:46:20 +0300206 audio = devm_kzalloc(&pdev->dev, sizeof(struct twl4030_audio),
207 GFP_KERNEL);
Peter Ujfalusi4fe56682011-05-03 18:03:43 +0300208 if (!audio)
Peter Ujfalusi0b83dde2009-10-22 13:26:45 +0300209 return -ENOMEM;
210
Peter Ujfalusi4fe56682011-05-03 18:03:43 +0300211 mutex_init(&audio->mutex);
Peter Ujfalusic5312412012-09-10 13:46:23 +0300212 audio->audio_mclk = twl_get_hfclk_rate();
Peter Ujfalusi0b83dde2009-10-22 13:26:45 +0300213
Peter Ujfalusicdf4b672012-09-10 13:46:21 +0300214 /* Configure APLL_INFREQ and disable APLL if enabled */
215 switch (audio->audio_mclk) {
216 case 19200000:
217 val = TWL4030_APLL_INFREQ_19200KHZ;
218 break;
219 case 26000000:
220 val = TWL4030_APLL_INFREQ_26000KHZ;
221 break;
222 case 38400000:
223 val = TWL4030_APLL_INFREQ_38400KHZ;
224 break;
225 default:
226 dev_err(&pdev->dev, "Invalid audio_mclk\n");
227 return -EINVAL;
228 }
229 twl_i2c_write_u8(TWL4030_MODULE_AUDIO_VOICE, val, TWL4030_REG_APLL_CTL);
230
Peter Ujfalusi0b83dde2009-10-22 13:26:45 +0300231 /* Codec power */
Peter Ujfalusi57fe7252011-05-31 12:02:49 +0300232 audio->resource[TWL4030_AUDIO_RES_POWER].reg = TWL4030_REG_CODEC_MODE;
233 audio->resource[TWL4030_AUDIO_RES_POWER].mask = TWL4030_CODECPDZ;
Peter Ujfalusi0b83dde2009-10-22 13:26:45 +0300234
235 /* PLL */
Peter Ujfalusi57fe7252011-05-31 12:02:49 +0300236 audio->resource[TWL4030_AUDIO_RES_APLL].reg = TWL4030_REG_APLL_CTL;
237 audio->resource[TWL4030_AUDIO_RES_APLL].mask = TWL4030_APLL_EN;
Peter Ujfalusi0b83dde2009-10-22 13:26:45 +0300238
Peter Ujfalusi019a7e62012-09-10 13:46:25 +0300239 if (twl4030_audio_has_codec(pdata, node)) {
Peter Ujfalusi4fe56682011-05-03 18:03:43 +0300240 cell = &audio->cells[childs];
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000241 cell->name = "twl4030-codec";
Peter Ujfalusi019a7e62012-09-10 13:46:25 +0300242 if (pdata) {
243 cell->platform_data = pdata->codec;
244 cell->pdata_size = sizeof(*pdata->codec);
245 }
Peter Ujfalusi0b83dde2009-10-22 13:26:45 +0300246 childs++;
247 }
Peter Ujfalusi019a7e62012-09-10 13:46:25 +0300248 if (twl4030_audio_has_vibra(pdata, node)) {
Peter Ujfalusi4fe56682011-05-03 18:03:43 +0300249 cell = &audio->cells[childs];
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000250 cell->name = "twl4030-vibra";
Peter Ujfalusi019a7e62012-09-10 13:46:25 +0300251 if (pdata) {
252 cell->platform_data = pdata->vibra;
253 cell->pdata_size = sizeof(*pdata->vibra);
254 }
Peter Ujfalusi0b83dde2009-10-22 13:26:45 +0300255 childs++;
256 }
257
Peter Ujfalusicdf4b672012-09-10 13:46:21 +0300258 platform_set_drvdata(pdev, audio);
259 twl4030_audio_dev = pdev;
260
Peter Ujfalusi0b83dde2009-10-22 13:26:45 +0300261 if (childs)
Peter Ujfalusi4fe56682011-05-03 18:03:43 +0300262 ret = mfd_add_devices(&pdev->dev, pdev->id, audio->cells,
Mark Brown55692af2012-09-11 15:16:36 +0800263 childs, NULL, 0, NULL);
Peter Ujfalusi0b83dde2009-10-22 13:26:45 +0300264 else {
265 dev_err(&pdev->dev, "No platform data found for childs\n");
266 ret = -ENODEV;
267 }
268
Jingoo Han96ac2d12013-05-06 13:06:31 +0900269 if (ret)
Peter Ujfalusi39c14212012-09-10 13:46:20 +0300270 twl4030_audio_dev = NULL;
Peter Ujfalusi0b83dde2009-10-22 13:26:45 +0300271
Peter Ujfalusi0b83dde2009-10-22 13:26:45 +0300272 return ret;
273}
274
Bill Pemberton4740f732012-11-19 13:26:01 -0500275static int twl4030_audio_remove(struct platform_device *pdev)
Peter Ujfalusi0b83dde2009-10-22 13:26:45 +0300276{
Peter Ujfalusi0b83dde2009-10-22 13:26:45 +0300277 mfd_remove_devices(&pdev->dev);
Peter Ujfalusi4fe56682011-05-03 18:03:43 +0300278 twl4030_audio_dev = NULL;
Peter Ujfalusi0b83dde2009-10-22 13:26:45 +0300279
280 return 0;
281}
282
Peter Ujfalusi019a7e62012-09-10 13:46:25 +0300283static 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
Peter Ujfalusi4fe56682011-05-03 18:03:43 +0300289static struct platform_driver twl4030_audio_driver = {
Peter Ujfalusi0b83dde2009-10-22 13:26:45 +0300290 .driver = {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000291 .name = "twl4030-audio",
Peter Ujfalusi019a7e62012-09-10 13:46:25 +0300292 .of_match_table = twl4030_audio_of_match,
Peter Ujfalusi0b83dde2009-10-22 13:26:45 +0300293 },
Peter Ujfalusi41569a12012-09-10 13:46:19 +0300294 .probe = twl4030_audio_probe,
Bill Pemberton84449212012-11-19 13:20:24 -0500295 .remove = twl4030_audio_remove,
Peter Ujfalusi0b83dde2009-10-22 13:26:45 +0300296};
297
Mark Brown65349d62011-11-23 22:58:34 +0000298module_platform_driver(twl4030_audio_driver);
Peter Ujfalusi0b83dde2009-10-22 13:26:45 +0300299
Peter Ujfalusi4a7c00c2011-05-10 08:59:23 +0300300MODULE_AUTHOR("Peter Ujfalusi <peter.ujfalusi@ti.com>");
Peter Ujfalusi41569a12012-09-10 13:46:19 +0300301MODULE_DESCRIPTION("TWL4030 audio block MFD driver");
Peter Ujfalusi0b83dde2009-10-22 13:26:45 +0300302MODULE_LICENSE("GPL");
Peter Ujfalusi41569a12012-09-10 13:46:19 +0300303MODULE_ALIAS("platform:twl4030-audio");