Peter Ujfalusi | 0b83dde | 2009-10-22 13:26:45 +0300 | [diff] [blame] | 1 | /* |
| 2 | * MFD driver for twl4030 codec submodule |
| 3 | * |
| 4 | * Author: Peter Ujfalusi <peter.ujfalusi@nokia.com> |
| 5 | * |
| 6 | * Copyright: (C) 2009 Nokia Corporation |
| 7 | * |
| 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. |
| 11 | * |
| 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. |
| 16 | * |
| 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 |
| 20 | * 02110-1301 USA |
| 21 | * |
| 22 | */ |
| 23 | |
| 24 | #include <linux/module.h> |
| 25 | #include <linux/types.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 26 | #include <linux/slab.h> |
Peter Ujfalusi | 0b83dde | 2009-10-22 13:26:45 +0300 | [diff] [blame] | 27 | #include <linux/kernel.h> |
| 28 | #include <linux/fs.h> |
| 29 | #include <linux/platform_device.h> |
Stephen Rothwell | 8bea867 | 2009-12-15 16:33:10 +1100 | [diff] [blame] | 30 | #include <linux/i2c/twl.h> |
Peter Ujfalusi | 0b83dde | 2009-10-22 13:26:45 +0300 | [diff] [blame] | 31 | #include <linux/mfd/core.h> |
| 32 | #include <linux/mfd/twl4030-codec.h> |
| 33 | |
| 34 | #define TWL4030_CODEC_CELLS 2 |
| 35 | |
| 36 | static struct platform_device *twl4030_codec_dev; |
| 37 | |
| 38 | struct twl4030_codec_resource { |
| 39 | int request_count; |
| 40 | u8 reg; |
| 41 | u8 mask; |
| 42 | }; |
| 43 | |
| 44 | struct twl4030_codec { |
Peter Ujfalusi | f9b4639 | 2009-11-04 09:58:19 +0200 | [diff] [blame] | 45 | unsigned int audio_mclk; |
Peter Ujfalusi | 0b83dde | 2009-10-22 13:26:45 +0300 | [diff] [blame] | 46 | struct mutex mutex; |
| 47 | struct twl4030_codec_resource resource[TWL4030_CODEC_RES_MAX]; |
| 48 | struct mfd_cell cells[TWL4030_CODEC_CELLS]; |
| 49 | }; |
| 50 | |
| 51 | /* |
| 52 | * Modify the resource, the function returns the content of the register |
| 53 | * after the modification. |
| 54 | */ |
| 55 | static int twl4030_codec_set_resource(enum twl4030_codec_res id, int enable) |
| 56 | { |
| 57 | struct twl4030_codec *codec = platform_get_drvdata(twl4030_codec_dev); |
| 58 | u8 val; |
| 59 | |
Stephen Rothwell | 8bea867 | 2009-12-15 16:33:10 +1100 | [diff] [blame] | 60 | twl_i2c_read_u8(TWL4030_MODULE_AUDIO_VOICE, &val, |
Peter Ujfalusi | 0b83dde | 2009-10-22 13:26:45 +0300 | [diff] [blame] | 61 | codec->resource[id].reg); |
| 62 | |
| 63 | if (enable) |
| 64 | val |= codec->resource[id].mask; |
| 65 | else |
| 66 | val &= ~codec->resource[id].mask; |
| 67 | |
Stephen Rothwell | 8bea867 | 2009-12-15 16:33:10 +1100 | [diff] [blame] | 68 | twl_i2c_write_u8(TWL4030_MODULE_AUDIO_VOICE, |
Peter Ujfalusi | 0b83dde | 2009-10-22 13:26:45 +0300 | [diff] [blame] | 69 | val, codec->resource[id].reg); |
| 70 | |
| 71 | return val; |
| 72 | } |
| 73 | |
| 74 | static inline int twl4030_codec_get_resource(enum twl4030_codec_res id) |
| 75 | { |
| 76 | struct twl4030_codec *codec = platform_get_drvdata(twl4030_codec_dev); |
| 77 | u8 val; |
| 78 | |
Stephen Rothwell | 8bea867 | 2009-12-15 16:33:10 +1100 | [diff] [blame] | 79 | twl_i2c_read_u8(TWL4030_MODULE_AUDIO_VOICE, &val, |
Peter Ujfalusi | 0b83dde | 2009-10-22 13:26:45 +0300 | [diff] [blame] | 80 | codec->resource[id].reg); |
| 81 | |
| 82 | return val; |
| 83 | } |
| 84 | |
| 85 | /* |
| 86 | * Enable the resource. |
| 87 | * The function returns with error or the content of the register |
| 88 | */ |
| 89 | int twl4030_codec_enable_resource(enum twl4030_codec_res id) |
| 90 | { |
| 91 | struct twl4030_codec *codec = platform_get_drvdata(twl4030_codec_dev); |
| 92 | int val; |
| 93 | |
| 94 | if (id >= TWL4030_CODEC_RES_MAX) { |
| 95 | dev_err(&twl4030_codec_dev->dev, |
| 96 | "Invalid resource ID (%u)\n", id); |
| 97 | return -EINVAL; |
| 98 | } |
| 99 | |
| 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); |
| 104 | else |
| 105 | val = twl4030_codec_get_resource(id); |
| 106 | |
| 107 | codec->resource[id].request_count++; |
| 108 | mutex_unlock(&codec->mutex); |
| 109 | |
| 110 | return val; |
| 111 | } |
| 112 | EXPORT_SYMBOL_GPL(twl4030_codec_enable_resource); |
| 113 | |
| 114 | /* |
| 115 | * Disable the resource. |
| 116 | * The function returns with error or the content of the register |
| 117 | */ |
| 118 | int twl4030_codec_disable_resource(unsigned id) |
| 119 | { |
| 120 | struct twl4030_codec *codec = platform_get_drvdata(twl4030_codec_dev); |
| 121 | int val; |
| 122 | |
| 123 | if (id >= TWL4030_CODEC_RES_MAX) { |
| 124 | dev_err(&twl4030_codec_dev->dev, |
| 125 | "Invalid resource ID (%u)\n", id); |
| 126 | return -EINVAL; |
| 127 | } |
| 128 | |
| 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); |
| 134 | return -EPERM; |
| 135 | } |
| 136 | codec->resource[id].request_count--; |
| 137 | |
| 138 | if (!codec->resource[id].request_count) |
| 139 | /* Resource can be disabled now */ |
| 140 | val = twl4030_codec_set_resource(id, 0); |
| 141 | else |
| 142 | val = twl4030_codec_get_resource(id); |
| 143 | |
| 144 | mutex_unlock(&codec->mutex); |
| 145 | |
| 146 | return val; |
| 147 | } |
| 148 | EXPORT_SYMBOL_GPL(twl4030_codec_disable_resource); |
| 149 | |
Peter Ujfalusi | f9b4639 | 2009-11-04 09:58:19 +0200 | [diff] [blame] | 150 | unsigned int twl4030_codec_get_mclk(void) |
| 151 | { |
| 152 | struct twl4030_codec *codec = platform_get_drvdata(twl4030_codec_dev); |
| 153 | |
| 154 | return codec->audio_mclk; |
| 155 | } |
| 156 | EXPORT_SYMBOL_GPL(twl4030_codec_get_mclk); |
| 157 | |
Peter Ujfalusi | 0b83dde | 2009-10-22 13:26:45 +0300 | [diff] [blame] | 158 | static int __devinit twl4030_codec_probe(struct platform_device *pdev) |
| 159 | { |
| 160 | struct twl4030_codec *codec; |
| 161 | struct twl4030_codec_data *pdata = pdev->dev.platform_data; |
| 162 | struct mfd_cell *cell = NULL; |
| 163 | int ret, childs = 0; |
Peter Ujfalusi | f9b4639 | 2009-11-04 09:58:19 +0200 | [diff] [blame] | 164 | u8 val; |
| 165 | |
| 166 | if (!pdata) { |
| 167 | dev_err(&pdev->dev, "Platform data is missing\n"); |
| 168 | return -EINVAL; |
| 169 | } |
| 170 | |
| 171 | /* Configure APLL_INFREQ and disable APLL if enabled */ |
| 172 | val = 0; |
| 173 | switch (pdata->audio_mclk) { |
| 174 | case 19200000: |
| 175 | val |= TWL4030_APLL_INFREQ_19200KHZ; |
| 176 | break; |
| 177 | case 26000000: |
| 178 | val |= TWL4030_APLL_INFREQ_26000KHZ; |
| 179 | break; |
| 180 | case 38400000: |
| 181 | val |= TWL4030_APLL_INFREQ_38400KHZ; |
| 182 | break; |
| 183 | default: |
| 184 | dev_err(&pdev->dev, "Invalid audio_mclk\n"); |
| 185 | return -EINVAL; |
| 186 | } |
Stephen Rothwell | 8bea867 | 2009-12-15 16:33:10 +1100 | [diff] [blame] | 187 | twl_i2c_write_u8(TWL4030_MODULE_AUDIO_VOICE, |
Peter Ujfalusi | f9b4639 | 2009-11-04 09:58:19 +0200 | [diff] [blame] | 188 | val, TWL4030_REG_APLL_CTL); |
Peter Ujfalusi | 0b83dde | 2009-10-22 13:26:45 +0300 | [diff] [blame] | 189 | |
| 190 | codec = kzalloc(sizeof(struct twl4030_codec), GFP_KERNEL); |
| 191 | if (!codec) |
| 192 | return -ENOMEM; |
| 193 | |
| 194 | platform_set_drvdata(pdev, codec); |
| 195 | |
| 196 | twl4030_codec_dev = pdev; |
| 197 | mutex_init(&codec->mutex); |
Peter Ujfalusi | f9b4639 | 2009-11-04 09:58:19 +0200 | [diff] [blame] | 198 | codec->audio_mclk = pdata->audio_mclk; |
Peter Ujfalusi | 0b83dde | 2009-10-22 13:26:45 +0300 | [diff] [blame] | 199 | |
| 200 | /* Codec power */ |
| 201 | codec->resource[TWL4030_CODEC_RES_POWER].reg = TWL4030_REG_CODEC_MODE; |
| 202 | codec->resource[TWL4030_CODEC_RES_POWER].mask = TWL4030_CODECPDZ; |
| 203 | |
| 204 | /* PLL */ |
| 205 | codec->resource[TWL4030_CODEC_RES_APLL].reg = TWL4030_REG_APLL_CTL; |
| 206 | codec->resource[TWL4030_CODEC_RES_APLL].mask = TWL4030_APLL_EN; |
| 207 | |
| 208 | if (pdata->audio) { |
| 209 | cell = &codec->cells[childs]; |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 210 | cell->name = "twl4030-codec"; |
Andres Salomon | 65e5235 | 2011-02-17 19:07:25 -0800 | [diff] [blame] | 211 | cell->mfd_data = pdata->audio; |
Peter Ujfalusi | 0b83dde | 2009-10-22 13:26:45 +0300 | [diff] [blame] | 212 | childs++; |
| 213 | } |
| 214 | if (pdata->vibra) { |
| 215 | cell = &codec->cells[childs]; |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 216 | cell->name = "twl4030-vibra"; |
Andres Salomon | 65e5235 | 2011-02-17 19:07:25 -0800 | [diff] [blame] | 217 | cell->mfd_data = pdata->vibra; |
Peter Ujfalusi | 0b83dde | 2009-10-22 13:26:45 +0300 | [diff] [blame] | 218 | childs++; |
| 219 | } |
| 220 | |
| 221 | if (childs) |
| 222 | ret = mfd_add_devices(&pdev->dev, pdev->id, codec->cells, |
| 223 | childs, NULL, 0); |
| 224 | else { |
| 225 | dev_err(&pdev->dev, "No platform data found for childs\n"); |
| 226 | ret = -ENODEV; |
| 227 | } |
| 228 | |
| 229 | if (!ret) |
| 230 | return 0; |
| 231 | |
| 232 | platform_set_drvdata(pdev, NULL); |
| 233 | kfree(codec); |
| 234 | twl4030_codec_dev = NULL; |
| 235 | return ret; |
| 236 | } |
| 237 | |
| 238 | static int __devexit twl4030_codec_remove(struct platform_device *pdev) |
| 239 | { |
| 240 | struct twl4030_codec *codec = platform_get_drvdata(pdev); |
| 241 | |
| 242 | mfd_remove_devices(&pdev->dev); |
| 243 | platform_set_drvdata(pdev, NULL); |
| 244 | kfree(codec); |
| 245 | twl4030_codec_dev = NULL; |
| 246 | |
| 247 | return 0; |
| 248 | } |
| 249 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 250 | MODULE_ALIAS("platform:twl4030-audio"); |
Peter Ujfalusi | 0b83dde | 2009-10-22 13:26:45 +0300 | [diff] [blame] | 251 | |
| 252 | static struct platform_driver twl4030_codec_driver = { |
| 253 | .probe = twl4030_codec_probe, |
| 254 | .remove = __devexit_p(twl4030_codec_remove), |
| 255 | .driver = { |
| 256 | .owner = THIS_MODULE, |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 257 | .name = "twl4030-audio", |
Peter Ujfalusi | 0b83dde | 2009-10-22 13:26:45 +0300 | [diff] [blame] | 258 | }, |
| 259 | }; |
| 260 | |
| 261 | static int __devinit twl4030_codec_init(void) |
| 262 | { |
| 263 | return platform_driver_register(&twl4030_codec_driver); |
| 264 | } |
| 265 | module_init(twl4030_codec_init); |
| 266 | |
| 267 | static void __devexit twl4030_codec_exit(void) |
| 268 | { |
| 269 | platform_driver_unregister(&twl4030_codec_driver); |
| 270 | } |
| 271 | module_exit(twl4030_codec_exit); |
| 272 | |
| 273 | MODULE_AUTHOR("Peter Ujfalusi <peter.ujfalusi@nokia.com>"); |
| 274 | MODULE_LICENSE("GPL"); |
| 275 | |