blob: 4536d829b43e4041a224439d4a674ba373d229a9 [file] [log] [blame]
Thomas Gleixner2b27bdc2019-05-29 16:57:50 -07001// SPDX-License-Identifier: GPL-2.0-only
Peter Ujfalusi0b83dde2009-10-22 13:26:45 +03002/*
Peter Ujfalusi4fe56682011-05-03 18:03:43 +03003 * MFD driver for twl4030 audio submodule, which contains an audio codec, and
4 * the vibra control.
Peter Ujfalusi0b83dde2009-10-22 13:26:45 +03005 *
Peter Ujfalusi4a7c00c2011-05-10 08:59:23 +03006 * Author: Peter Ujfalusi <peter.ujfalusi@ti.com>
Peter Ujfalusi0b83dde2009-10-22 13:26:45 +03007 *
8 * Copyright: (C) 2009 Nokia Corporation
Peter Ujfalusi0b83dde2009-10-22 13:26:45 +03009 */
10
11#include <linux/module.h>
12#include <linux/types.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090013#include <linux/slab.h>
Peter Ujfalusi0b83dde2009-10-22 13:26:45 +030014#include <linux/kernel.h>
15#include <linux/fs.h>
16#include <linux/platform_device.h>
Peter Ujfalusi019a7e62012-09-10 13:46:25 +030017#include <linux/of.h>
18#include <linux/of_platform.h>
Wolfram Sanga2054252017-08-14 18:34:24 +020019#include <linux/mfd/twl.h>
Peter Ujfalusi0b83dde2009-10-22 13:26:45 +030020#include <linux/mfd/core.h>
Peter Ujfalusi57fe7252011-05-31 12:02:49 +030021#include <linux/mfd/twl4030-audio.h>
Peter Ujfalusi0b83dde2009-10-22 13:26:45 +030022
Peter Ujfalusi4fe56682011-05-03 18:03:43 +030023#define TWL4030_AUDIO_CELLS 2
Peter Ujfalusi0b83dde2009-10-22 13:26:45 +030024
Peter Ujfalusi4fe56682011-05-03 18:03:43 +030025static struct platform_device *twl4030_audio_dev;
Peter Ujfalusi0b83dde2009-10-22 13:26:45 +030026
Peter Ujfalusi4fe56682011-05-03 18:03:43 +030027struct twl4030_audio_resource {
Peter Ujfalusi0b83dde2009-10-22 13:26:45 +030028 int request_count;
29 u8 reg;
30 u8 mask;
31};
32
Peter Ujfalusi4fe56682011-05-03 18:03:43 +030033struct twl4030_audio {
Peter Ujfalusif9b4639e2009-11-04 09:58:19 +020034 unsigned int audio_mclk;
Peter Ujfalusi0b83dde2009-10-22 13:26:45 +030035 struct mutex mutex;
Peter Ujfalusi57fe7252011-05-31 12:02:49 +030036 struct twl4030_audio_resource resource[TWL4030_AUDIO_RES_MAX];
Peter Ujfalusi4fe56682011-05-03 18:03:43 +030037 struct mfd_cell cells[TWL4030_AUDIO_CELLS];
Peter Ujfalusi0b83dde2009-10-22 13:26:45 +030038};
39
40/*
41 * Modify the resource, the function returns the content of the register
42 * after the modification.
43 */
Peter Ujfalusi57fe7252011-05-31 12:02:49 +030044static int twl4030_audio_set_resource(enum twl4030_audio_res id, int enable)
Peter Ujfalusi0b83dde2009-10-22 13:26:45 +030045{
Peter Ujfalusi4fe56682011-05-03 18:03:43 +030046 struct twl4030_audio *audio = platform_get_drvdata(twl4030_audio_dev);
Peter Ujfalusi0b83dde2009-10-22 13:26:45 +030047 u8 val;
48
Stephen Rothwell8bea8672009-12-15 16:33:10 +110049 twl_i2c_read_u8(TWL4030_MODULE_AUDIO_VOICE, &val,
Peter Ujfalusi4fe56682011-05-03 18:03:43 +030050 audio->resource[id].reg);
Peter Ujfalusi0b83dde2009-10-22 13:26:45 +030051
52 if (enable)
Peter Ujfalusi4fe56682011-05-03 18:03:43 +030053 val |= audio->resource[id].mask;
Peter Ujfalusi0b83dde2009-10-22 13:26:45 +030054 else
Peter Ujfalusi4fe56682011-05-03 18:03:43 +030055 val &= ~audio->resource[id].mask;
Peter Ujfalusi0b83dde2009-10-22 13:26:45 +030056
Stephen Rothwell8bea8672009-12-15 16:33:10 +110057 twl_i2c_write_u8(TWL4030_MODULE_AUDIO_VOICE,
Peter Ujfalusi4fe56682011-05-03 18:03:43 +030058 val, audio->resource[id].reg);
Peter Ujfalusi0b83dde2009-10-22 13:26:45 +030059
60 return val;
61}
62
Peter Ujfalusi57fe7252011-05-31 12:02:49 +030063static inline int twl4030_audio_get_resource(enum twl4030_audio_res id)
Peter Ujfalusi0b83dde2009-10-22 13:26:45 +030064{
Peter Ujfalusi4fe56682011-05-03 18:03:43 +030065 struct twl4030_audio *audio = platform_get_drvdata(twl4030_audio_dev);
Peter Ujfalusi0b83dde2009-10-22 13:26:45 +030066 u8 val;
67
Stephen Rothwell8bea8672009-12-15 16:33:10 +110068 twl_i2c_read_u8(TWL4030_MODULE_AUDIO_VOICE, &val,
Peter Ujfalusi4fe56682011-05-03 18:03:43 +030069 audio->resource[id].reg);
Peter Ujfalusi0b83dde2009-10-22 13:26:45 +030070
71 return val;
72}
73
74/*
75 * Enable the resource.
76 * The function returns with error or the content of the register
77 */
Peter Ujfalusi57fe7252011-05-31 12:02:49 +030078int twl4030_audio_enable_resource(enum twl4030_audio_res id)
Peter Ujfalusi0b83dde2009-10-22 13:26:45 +030079{
Peter Ujfalusi4fe56682011-05-03 18:03:43 +030080 struct twl4030_audio *audio = platform_get_drvdata(twl4030_audio_dev);
Peter Ujfalusi0b83dde2009-10-22 13:26:45 +030081 int val;
82
Peter Ujfalusi57fe7252011-05-31 12:02:49 +030083 if (id >= TWL4030_AUDIO_RES_MAX) {
Peter Ujfalusi4fe56682011-05-03 18:03:43 +030084 dev_err(&twl4030_audio_dev->dev,
Peter Ujfalusi0b83dde2009-10-22 13:26:45 +030085 "Invalid resource ID (%u)\n", id);
86 return -EINVAL;
87 }
88
Peter Ujfalusi4fe56682011-05-03 18:03:43 +030089 mutex_lock(&audio->mutex);
90 if (!audio->resource[id].request_count)
Peter Ujfalusi0b83dde2009-10-22 13:26:45 +030091 /* Resource was disabled, enable it */
Peter Ujfalusi4fe56682011-05-03 18:03:43 +030092 val = twl4030_audio_set_resource(id, 1);
Peter Ujfalusi0b83dde2009-10-22 13:26:45 +030093 else
Peter Ujfalusi4fe56682011-05-03 18:03:43 +030094 val = twl4030_audio_get_resource(id);
Peter Ujfalusi0b83dde2009-10-22 13:26:45 +030095
Peter Ujfalusi4fe56682011-05-03 18:03:43 +030096 audio->resource[id].request_count++;
97 mutex_unlock(&audio->mutex);
Peter Ujfalusi0b83dde2009-10-22 13:26:45 +030098
99 return val;
100}
Peter Ujfalusi57fe7252011-05-31 12:02:49 +0300101EXPORT_SYMBOL_GPL(twl4030_audio_enable_resource);
Peter Ujfalusi0b83dde2009-10-22 13:26:45 +0300102
103/*
104 * Disable the resource.
105 * The function returns with error or the content of the register
106 */
Mark Brown6049bce2013-03-02 15:30:35 +0800107int twl4030_audio_disable_resource(enum twl4030_audio_res id)
Peter Ujfalusi0b83dde2009-10-22 13:26:45 +0300108{
Peter Ujfalusi4fe56682011-05-03 18:03:43 +0300109 struct twl4030_audio *audio = platform_get_drvdata(twl4030_audio_dev);
Peter Ujfalusi0b83dde2009-10-22 13:26:45 +0300110 int val;
111
Peter Ujfalusi57fe7252011-05-31 12:02:49 +0300112 if (id >= TWL4030_AUDIO_RES_MAX) {
Peter Ujfalusi4fe56682011-05-03 18:03:43 +0300113 dev_err(&twl4030_audio_dev->dev,
Peter Ujfalusi0b83dde2009-10-22 13:26:45 +0300114 "Invalid resource ID (%u)\n", id);
115 return -EINVAL;
116 }
117
Peter Ujfalusi4fe56682011-05-03 18:03:43 +0300118 mutex_lock(&audio->mutex);
119 if (!audio->resource[id].request_count) {
120 dev_err(&twl4030_audio_dev->dev,
Peter Ujfalusi0b83dde2009-10-22 13:26:45 +0300121 "Resource has been disabled already (%u)\n", id);
Peter Ujfalusi4fe56682011-05-03 18:03:43 +0300122 mutex_unlock(&audio->mutex);
Peter Ujfalusi0b83dde2009-10-22 13:26:45 +0300123 return -EPERM;
124 }
Peter Ujfalusi4fe56682011-05-03 18:03:43 +0300125 audio->resource[id].request_count--;
Peter Ujfalusi0b83dde2009-10-22 13:26:45 +0300126
Peter Ujfalusi4fe56682011-05-03 18:03:43 +0300127 if (!audio->resource[id].request_count)
Peter Ujfalusi0b83dde2009-10-22 13:26:45 +0300128 /* Resource can be disabled now */
Peter Ujfalusi4fe56682011-05-03 18:03:43 +0300129 val = twl4030_audio_set_resource(id, 0);
Peter Ujfalusi0b83dde2009-10-22 13:26:45 +0300130 else
Peter Ujfalusi4fe56682011-05-03 18:03:43 +0300131 val = twl4030_audio_get_resource(id);
Peter Ujfalusi0b83dde2009-10-22 13:26:45 +0300132
Peter Ujfalusi4fe56682011-05-03 18:03:43 +0300133 mutex_unlock(&audio->mutex);
Peter Ujfalusi0b83dde2009-10-22 13:26:45 +0300134
135 return val;
136}
Peter Ujfalusi57fe7252011-05-31 12:02:49 +0300137EXPORT_SYMBOL_GPL(twl4030_audio_disable_resource);
Peter Ujfalusi0b83dde2009-10-22 13:26:45 +0300138
Peter Ujfalusi57fe7252011-05-31 12:02:49 +0300139unsigned int twl4030_audio_get_mclk(void)
Peter Ujfalusif9b4639e2009-11-04 09:58:19 +0200140{
Peter Ujfalusi4fe56682011-05-03 18:03:43 +0300141 struct twl4030_audio *audio = platform_get_drvdata(twl4030_audio_dev);
Peter Ujfalusif9b4639e2009-11-04 09:58:19 +0200142
Peter Ujfalusi4fe56682011-05-03 18:03:43 +0300143 return audio->audio_mclk;
Peter Ujfalusif9b4639e2009-11-04 09:58:19 +0200144}
Peter Ujfalusi57fe7252011-05-31 12:02:49 +0300145EXPORT_SYMBOL_GPL(twl4030_audio_get_mclk);
Peter Ujfalusif9b4639e2009-11-04 09:58:19 +0200146
Peter Ujfalusi019a7e62012-09-10 13:46:25 +0300147static bool twl4030_audio_has_codec(struct twl4030_audio_data *pdata,
Johan Hovold0a423772017-11-11 16:38:43 +0100148 struct device_node *parent)
Peter Ujfalusi019a7e62012-09-10 13:46:25 +0300149{
Johan Hovold0a423772017-11-11 16:38:43 +0100150 struct device_node *node;
151
Peter Ujfalusi019a7e62012-09-10 13:46:25 +0300152 if (pdata && pdata->codec)
153 return true;
154
Johan Hovold0a423772017-11-11 16:38:43 +0100155 node = of_get_child_by_name(parent, "codec");
156 if (node) {
157 of_node_put(node);
Peter Ujfalusi019a7e62012-09-10 13:46:25 +0300158 return true;
Johan Hovold0a423772017-11-11 16:38:43 +0100159 }
Peter Ujfalusi019a7e62012-09-10 13:46:25 +0300160
161 return false;
162}
163
164static bool twl4030_audio_has_vibra(struct twl4030_audio_data *pdata,
165 struct device_node *node)
166{
167 int vibra;
168
169 if (pdata && pdata->vibra)
170 return true;
171
172 if (!of_property_read_u32(node, "ti,enable-vibra", &vibra) && vibra)
173 return true;
174
175 return false;
176}
177
Bill Pembertonf791be42012-11-19 13:23:04 -0500178static int twl4030_audio_probe(struct platform_device *pdev)
Peter Ujfalusi0b83dde2009-10-22 13:26:45 +0300179{
Peter Ujfalusi4fe56682011-05-03 18:03:43 +0300180 struct twl4030_audio *audio;
Jingoo Han334a41ce2013-07-30 17:10:05 +0900181 struct twl4030_audio_data *pdata = dev_get_platdata(&pdev->dev);
Peter Ujfalusi019a7e62012-09-10 13:46:25 +0300182 struct device_node *node = pdev->dev.of_node;
Peter Ujfalusi0b83dde2009-10-22 13:26:45 +0300183 struct mfd_cell *cell = NULL;
184 int ret, childs = 0;
Peter Ujfalusif9b4639e2009-11-04 09:58:19 +0200185 u8 val;
186
Peter Ujfalusi019a7e62012-09-10 13:46:25 +0300187 if (!pdata && !node) {
Peter Ujfalusif9b4639e2009-11-04 09:58:19 +0200188 dev_err(&pdev->dev, "Platform data is missing\n");
189 return -EINVAL;
190 }
191
Peter Ujfalusi39c14212012-09-10 13:46:20 +0300192 audio = devm_kzalloc(&pdev->dev, sizeof(struct twl4030_audio),
193 GFP_KERNEL);
Peter Ujfalusi4fe56682011-05-03 18:03:43 +0300194 if (!audio)
Peter Ujfalusi0b83dde2009-10-22 13:26:45 +0300195 return -ENOMEM;
196
Peter Ujfalusi4fe56682011-05-03 18:03:43 +0300197 mutex_init(&audio->mutex);
Peter Ujfalusic5312412012-09-10 13:46:23 +0300198 audio->audio_mclk = twl_get_hfclk_rate();
Peter Ujfalusi0b83dde2009-10-22 13:26:45 +0300199
Peter Ujfalusicdf4b672012-09-10 13:46:21 +0300200 /* Configure APLL_INFREQ and disable APLL if enabled */
201 switch (audio->audio_mclk) {
202 case 19200000:
203 val = TWL4030_APLL_INFREQ_19200KHZ;
204 break;
205 case 26000000:
206 val = TWL4030_APLL_INFREQ_26000KHZ;
207 break;
208 case 38400000:
209 val = TWL4030_APLL_INFREQ_38400KHZ;
210 break;
211 default:
212 dev_err(&pdev->dev, "Invalid audio_mclk\n");
213 return -EINVAL;
214 }
215 twl_i2c_write_u8(TWL4030_MODULE_AUDIO_VOICE, val, TWL4030_REG_APLL_CTL);
216
Peter Ujfalusi0b83dde2009-10-22 13:26:45 +0300217 /* Codec power */
Peter Ujfalusi57fe7252011-05-31 12:02:49 +0300218 audio->resource[TWL4030_AUDIO_RES_POWER].reg = TWL4030_REG_CODEC_MODE;
219 audio->resource[TWL4030_AUDIO_RES_POWER].mask = TWL4030_CODECPDZ;
Peter Ujfalusi0b83dde2009-10-22 13:26:45 +0300220
221 /* PLL */
Peter Ujfalusi57fe7252011-05-31 12:02:49 +0300222 audio->resource[TWL4030_AUDIO_RES_APLL].reg = TWL4030_REG_APLL_CTL;
223 audio->resource[TWL4030_AUDIO_RES_APLL].mask = TWL4030_APLL_EN;
Peter Ujfalusi0b83dde2009-10-22 13:26:45 +0300224
Peter Ujfalusi019a7e62012-09-10 13:46:25 +0300225 if (twl4030_audio_has_codec(pdata, node)) {
Peter Ujfalusi4fe56682011-05-03 18:03:43 +0300226 cell = &audio->cells[childs];
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000227 cell->name = "twl4030-codec";
Peter Ujfalusi019a7e62012-09-10 13:46:25 +0300228 if (pdata) {
229 cell->platform_data = pdata->codec;
230 cell->pdata_size = sizeof(*pdata->codec);
231 }
Peter Ujfalusi0b83dde2009-10-22 13:26:45 +0300232 childs++;
233 }
Peter Ujfalusi019a7e62012-09-10 13:46:25 +0300234 if (twl4030_audio_has_vibra(pdata, node)) {
Peter Ujfalusi4fe56682011-05-03 18:03:43 +0300235 cell = &audio->cells[childs];
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000236 cell->name = "twl4030-vibra";
Peter Ujfalusi019a7e62012-09-10 13:46:25 +0300237 if (pdata) {
238 cell->platform_data = pdata->vibra;
239 cell->pdata_size = sizeof(*pdata->vibra);
240 }
Peter Ujfalusi0b83dde2009-10-22 13:26:45 +0300241 childs++;
242 }
243
Peter Ujfalusicdf4b672012-09-10 13:46:21 +0300244 platform_set_drvdata(pdev, audio);
245 twl4030_audio_dev = pdev;
246
Peter Ujfalusi0b83dde2009-10-22 13:26:45 +0300247 if (childs)
Peter Ujfalusi4fe56682011-05-03 18:03:43 +0300248 ret = mfd_add_devices(&pdev->dev, pdev->id, audio->cells,
Mark Brown55692af2012-09-11 15:16:36 +0800249 childs, NULL, 0, NULL);
Peter Ujfalusi0b83dde2009-10-22 13:26:45 +0300250 else {
251 dev_err(&pdev->dev, "No platform data found for childs\n");
252 ret = -ENODEV;
253 }
254
Jingoo Han96ac2d12013-05-06 13:06:31 +0900255 if (ret)
Peter Ujfalusi39c14212012-09-10 13:46:20 +0300256 twl4030_audio_dev = NULL;
Peter Ujfalusi0b83dde2009-10-22 13:26:45 +0300257
Peter Ujfalusi0b83dde2009-10-22 13:26:45 +0300258 return ret;
259}
260
Bill Pemberton4740f732012-11-19 13:26:01 -0500261static int twl4030_audio_remove(struct platform_device *pdev)
Peter Ujfalusi0b83dde2009-10-22 13:26:45 +0300262{
Peter Ujfalusi0b83dde2009-10-22 13:26:45 +0300263 mfd_remove_devices(&pdev->dev);
Peter Ujfalusi4fe56682011-05-03 18:03:43 +0300264 twl4030_audio_dev = NULL;
Peter Ujfalusi0b83dde2009-10-22 13:26:45 +0300265
266 return 0;
267}
268
Peter Ujfalusi019a7e62012-09-10 13:46:25 +0300269static const struct of_device_id twl4030_audio_of_match[] = {
270 {.compatible = "ti,twl4030-audio", },
271 { },
272};
273MODULE_DEVICE_TABLE(of, twl4030_audio_of_match);
274
Peter Ujfalusi4fe56682011-05-03 18:03:43 +0300275static struct platform_driver twl4030_audio_driver = {
Peter Ujfalusi0b83dde2009-10-22 13:26:45 +0300276 .driver = {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000277 .name = "twl4030-audio",
Peter Ujfalusi019a7e62012-09-10 13:46:25 +0300278 .of_match_table = twl4030_audio_of_match,
Peter Ujfalusi0b83dde2009-10-22 13:26:45 +0300279 },
Peter Ujfalusi41569a12012-09-10 13:46:19 +0300280 .probe = twl4030_audio_probe,
Bill Pemberton84449212012-11-19 13:20:24 -0500281 .remove = twl4030_audio_remove,
Peter Ujfalusi0b83dde2009-10-22 13:26:45 +0300282};
283
Mark Brown65349d62011-11-23 22:58:34 +0000284module_platform_driver(twl4030_audio_driver);
Peter Ujfalusi0b83dde2009-10-22 13:26:45 +0300285
Peter Ujfalusi4a7c00c2011-05-10 08:59:23 +0300286MODULE_AUTHOR("Peter Ujfalusi <peter.ujfalusi@ti.com>");
Peter Ujfalusi41569a12012-09-10 13:46:19 +0300287MODULE_DESCRIPTION("TWL4030 audio block MFD driver");
Peter Ujfalusi0b83dde2009-10-22 13:26:45 +0300288MODULE_LICENSE("GPL");
Peter Ujfalusi41569a12012-09-10 13:46:19 +0300289MODULE_ALIAS("platform:twl4030-audio");