Misael Lopez Cruz | f19b282 | 2011-04-27 02:14:07 -0500 | [diff] [blame] | 1 | /* |
| 2 | * MFD driver for TWL6040 audio device |
| 3 | * |
| 4 | * Authors: Misael Lopez Cruz <misael.lopez@ti.com> |
| 5 | * Jorge Eduardo Candelaria <jorge.candelaria@ti.com> |
| 6 | * Peter Ujfalusi <peter.ujfalusi@ti.com> |
| 7 | * |
| 8 | * Copyright: (C) 2011 Texas Instruments, Inc. |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License version 2 as |
| 12 | * published by the Free Software Foundation. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, but |
| 15 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 17 | * General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License |
| 20 | * along with this program; if not, write to the Free Software |
| 21 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA |
| 22 | * 02110-1301 USA |
| 23 | * |
| 24 | */ |
| 25 | |
| 26 | #include <linux/module.h> |
| 27 | #include <linux/types.h> |
| 28 | #include <linux/slab.h> |
| 29 | #include <linux/kernel.h> |
| 30 | #include <linux/platform_device.h> |
| 31 | #include <linux/gpio.h> |
| 32 | #include <linux/delay.h> |
| 33 | #include <linux/i2c/twl.h> |
| 34 | #include <linux/mfd/core.h> |
| 35 | #include <linux/mfd/twl6040.h> |
| 36 | |
Peter Ujfalusi | 31b402e | 2011-10-12 11:57:54 +0300 | [diff] [blame] | 37 | #define VIBRACTRL_MEMBER(reg) ((reg == TWL6040_REG_VIBCTLL) ? 0 : 1) |
| 38 | |
Misael Lopez Cruz | f19b282 | 2011-04-27 02:14:07 -0500 | [diff] [blame] | 39 | int twl6040_reg_read(struct twl6040 *twl6040, unsigned int reg) |
| 40 | { |
| 41 | int ret; |
| 42 | u8 val = 0; |
| 43 | |
| 44 | mutex_lock(&twl6040->io_mutex); |
Peter Ujfalusi | 31b402e | 2011-10-12 11:57:54 +0300 | [diff] [blame] | 45 | /* Vibra control registers from cache */ |
| 46 | if (unlikely(reg == TWL6040_REG_VIBCTLL || |
| 47 | reg == TWL6040_REG_VIBCTLR)) { |
| 48 | val = twl6040->vibra_ctrl_cache[VIBRACTRL_MEMBER(reg)]; |
| 49 | } else { |
| 50 | ret = twl_i2c_read_u8(TWL_MODULE_AUDIO_VOICE, &val, reg); |
| 51 | if (ret < 0) { |
| 52 | mutex_unlock(&twl6040->io_mutex); |
| 53 | return ret; |
| 54 | } |
Misael Lopez Cruz | f19b282 | 2011-04-27 02:14:07 -0500 | [diff] [blame] | 55 | } |
| 56 | mutex_unlock(&twl6040->io_mutex); |
| 57 | |
| 58 | return val; |
| 59 | } |
| 60 | EXPORT_SYMBOL(twl6040_reg_read); |
| 61 | |
| 62 | int twl6040_reg_write(struct twl6040 *twl6040, unsigned int reg, u8 val) |
| 63 | { |
| 64 | int ret; |
| 65 | |
| 66 | mutex_lock(&twl6040->io_mutex); |
| 67 | ret = twl_i2c_write_u8(TWL_MODULE_AUDIO_VOICE, val, reg); |
Peter Ujfalusi | 31b402e | 2011-10-12 11:57:54 +0300 | [diff] [blame] | 68 | /* Cache the vibra control registers */ |
| 69 | if (reg == TWL6040_REG_VIBCTLL || reg == TWL6040_REG_VIBCTLR) |
| 70 | twl6040->vibra_ctrl_cache[VIBRACTRL_MEMBER(reg)] = val; |
Misael Lopez Cruz | f19b282 | 2011-04-27 02:14:07 -0500 | [diff] [blame] | 71 | mutex_unlock(&twl6040->io_mutex); |
| 72 | |
| 73 | return ret; |
| 74 | } |
| 75 | EXPORT_SYMBOL(twl6040_reg_write); |
| 76 | |
| 77 | int twl6040_set_bits(struct twl6040 *twl6040, unsigned int reg, u8 mask) |
| 78 | { |
| 79 | int ret; |
| 80 | u8 val; |
| 81 | |
| 82 | mutex_lock(&twl6040->io_mutex); |
| 83 | ret = twl_i2c_read_u8(TWL_MODULE_AUDIO_VOICE, &val, reg); |
| 84 | if (ret) |
| 85 | goto out; |
| 86 | |
| 87 | val |= mask; |
| 88 | ret = twl_i2c_write_u8(TWL_MODULE_AUDIO_VOICE, val, reg); |
| 89 | out: |
| 90 | mutex_unlock(&twl6040->io_mutex); |
| 91 | return ret; |
| 92 | } |
| 93 | EXPORT_SYMBOL(twl6040_set_bits); |
| 94 | |
| 95 | int twl6040_clear_bits(struct twl6040 *twl6040, unsigned int reg, u8 mask) |
| 96 | { |
| 97 | int ret; |
| 98 | u8 val; |
| 99 | |
| 100 | mutex_lock(&twl6040->io_mutex); |
| 101 | ret = twl_i2c_read_u8(TWL_MODULE_AUDIO_VOICE, &val, reg); |
| 102 | if (ret) |
| 103 | goto out; |
| 104 | |
| 105 | val &= ~mask; |
| 106 | ret = twl_i2c_write_u8(TWL_MODULE_AUDIO_VOICE, val, reg); |
| 107 | out: |
| 108 | mutex_unlock(&twl6040->io_mutex); |
| 109 | return ret; |
| 110 | } |
| 111 | EXPORT_SYMBOL(twl6040_clear_bits); |
| 112 | |
| 113 | /* twl6040 codec manual power-up sequence */ |
| 114 | static int twl6040_power_up(struct twl6040 *twl6040) |
| 115 | { |
| 116 | u8 ldoctl, ncpctl, lppllctl; |
| 117 | int ret; |
| 118 | |
| 119 | /* enable high-side LDO, reference system and internal oscillator */ |
| 120 | ldoctl = TWL6040_HSLDOENA | TWL6040_REFENA | TWL6040_OSCENA; |
| 121 | ret = twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl); |
| 122 | if (ret) |
| 123 | return ret; |
| 124 | usleep_range(10000, 10500); |
| 125 | |
| 126 | /* enable negative charge pump */ |
| 127 | ncpctl = TWL6040_NCPENA; |
| 128 | ret = twl6040_reg_write(twl6040, TWL6040_REG_NCPCTL, ncpctl); |
| 129 | if (ret) |
| 130 | goto ncp_err; |
| 131 | usleep_range(1000, 1500); |
| 132 | |
| 133 | /* enable low-side LDO */ |
| 134 | ldoctl |= TWL6040_LSLDOENA; |
| 135 | ret = twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl); |
| 136 | if (ret) |
| 137 | goto lsldo_err; |
| 138 | usleep_range(1000, 1500); |
| 139 | |
| 140 | /* enable low-power PLL */ |
| 141 | lppllctl = TWL6040_LPLLENA; |
| 142 | ret = twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL, lppllctl); |
| 143 | if (ret) |
| 144 | goto lppll_err; |
| 145 | usleep_range(5000, 5500); |
| 146 | |
| 147 | /* disable internal oscillator */ |
| 148 | ldoctl &= ~TWL6040_OSCENA; |
| 149 | ret = twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl); |
| 150 | if (ret) |
| 151 | goto osc_err; |
| 152 | |
| 153 | return 0; |
| 154 | |
| 155 | osc_err: |
| 156 | lppllctl &= ~TWL6040_LPLLENA; |
| 157 | twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL, lppllctl); |
| 158 | lppll_err: |
| 159 | ldoctl &= ~TWL6040_LSLDOENA; |
| 160 | twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl); |
| 161 | lsldo_err: |
| 162 | ncpctl &= ~TWL6040_NCPENA; |
| 163 | twl6040_reg_write(twl6040, TWL6040_REG_NCPCTL, ncpctl); |
| 164 | ncp_err: |
| 165 | ldoctl &= ~(TWL6040_HSLDOENA | TWL6040_REFENA | TWL6040_OSCENA); |
| 166 | twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl); |
| 167 | |
| 168 | return ret; |
| 169 | } |
| 170 | |
| 171 | /* twl6040 manual power-down sequence */ |
| 172 | static void twl6040_power_down(struct twl6040 *twl6040) |
| 173 | { |
| 174 | u8 ncpctl, ldoctl, lppllctl; |
| 175 | |
| 176 | ncpctl = twl6040_reg_read(twl6040, TWL6040_REG_NCPCTL); |
| 177 | ldoctl = twl6040_reg_read(twl6040, TWL6040_REG_LDOCTL); |
| 178 | lppllctl = twl6040_reg_read(twl6040, TWL6040_REG_LPPLLCTL); |
| 179 | |
| 180 | /* enable internal oscillator */ |
| 181 | ldoctl |= TWL6040_OSCENA; |
| 182 | twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl); |
| 183 | usleep_range(1000, 1500); |
| 184 | |
| 185 | /* disable low-power PLL */ |
| 186 | lppllctl &= ~TWL6040_LPLLENA; |
| 187 | twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL, lppllctl); |
| 188 | |
| 189 | /* disable low-side LDO */ |
| 190 | ldoctl &= ~TWL6040_LSLDOENA; |
| 191 | twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl); |
| 192 | |
| 193 | /* disable negative charge pump */ |
| 194 | ncpctl &= ~TWL6040_NCPENA; |
| 195 | twl6040_reg_write(twl6040, TWL6040_REG_NCPCTL, ncpctl); |
| 196 | |
| 197 | /* disable high-side LDO, reference system and internal oscillator */ |
| 198 | ldoctl &= ~(TWL6040_HSLDOENA | TWL6040_REFENA | TWL6040_OSCENA); |
| 199 | twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl); |
| 200 | } |
| 201 | |
| 202 | static irqreturn_t twl6040_naudint_handler(int irq, void *data) |
| 203 | { |
| 204 | struct twl6040 *twl6040 = data; |
| 205 | u8 intid, status; |
| 206 | |
| 207 | intid = twl6040_reg_read(twl6040, TWL6040_REG_INTID); |
| 208 | |
| 209 | if (intid & TWL6040_READYINT) |
| 210 | complete(&twl6040->ready); |
| 211 | |
| 212 | if (intid & TWL6040_THINT) { |
| 213 | status = twl6040_reg_read(twl6040, TWL6040_REG_STATUS); |
| 214 | if (status & TWL6040_TSHUTDET) { |
Peter Ujfalusi | 2d7c957 | 2011-09-15 15:39:23 +0300 | [diff] [blame] | 215 | dev_warn(twl6040->dev, |
Misael Lopez Cruz | f19b282 | 2011-04-27 02:14:07 -0500 | [diff] [blame] | 216 | "Thermal shutdown, powering-off"); |
| 217 | twl6040_power(twl6040, 0); |
| 218 | } else { |
Peter Ujfalusi | 2d7c957 | 2011-09-15 15:39:23 +0300 | [diff] [blame] | 219 | dev_warn(twl6040->dev, |
Misael Lopez Cruz | f19b282 | 2011-04-27 02:14:07 -0500 | [diff] [blame] | 220 | "Leaving thermal shutdown, powering-on"); |
| 221 | twl6040_power(twl6040, 1); |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | return IRQ_HANDLED; |
| 226 | } |
| 227 | |
| 228 | static int twl6040_power_up_completion(struct twl6040 *twl6040, |
| 229 | int naudint) |
| 230 | { |
| 231 | int time_left; |
| 232 | u8 intid; |
| 233 | |
| 234 | time_left = wait_for_completion_timeout(&twl6040->ready, |
| 235 | msecs_to_jiffies(144)); |
| 236 | if (!time_left) { |
| 237 | intid = twl6040_reg_read(twl6040, TWL6040_REG_INTID); |
| 238 | if (!(intid & TWL6040_READYINT)) { |
Peter Ujfalusi | 2d7c957 | 2011-09-15 15:39:23 +0300 | [diff] [blame] | 239 | dev_err(twl6040->dev, |
Misael Lopez Cruz | f19b282 | 2011-04-27 02:14:07 -0500 | [diff] [blame] | 240 | "timeout waiting for READYINT\n"); |
| 241 | return -ETIMEDOUT; |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | return 0; |
| 246 | } |
| 247 | |
| 248 | int twl6040_power(struct twl6040 *twl6040, int on) |
| 249 | { |
| 250 | int audpwron = twl6040->audpwron; |
| 251 | int naudint = twl6040->irq; |
| 252 | int ret = 0; |
| 253 | |
| 254 | mutex_lock(&twl6040->mutex); |
| 255 | |
| 256 | if (on) { |
| 257 | /* already powered-up */ |
| 258 | if (twl6040->power_count++) |
| 259 | goto out; |
| 260 | |
| 261 | if (gpio_is_valid(audpwron)) { |
| 262 | /* use AUDPWRON line */ |
| 263 | gpio_set_value(audpwron, 1); |
| 264 | /* wait for power-up completion */ |
| 265 | ret = twl6040_power_up_completion(twl6040, naudint); |
| 266 | if (ret) { |
Peter Ujfalusi | 2d7c957 | 2011-09-15 15:39:23 +0300 | [diff] [blame] | 267 | dev_err(twl6040->dev, |
Misael Lopez Cruz | f19b282 | 2011-04-27 02:14:07 -0500 | [diff] [blame] | 268 | "automatic power-down failed\n"); |
| 269 | twl6040->power_count = 0; |
| 270 | goto out; |
| 271 | } |
| 272 | } else { |
| 273 | /* use manual power-up sequence */ |
| 274 | ret = twl6040_power_up(twl6040); |
| 275 | if (ret) { |
Peter Ujfalusi | 2d7c957 | 2011-09-15 15:39:23 +0300 | [diff] [blame] | 276 | dev_err(twl6040->dev, |
Misael Lopez Cruz | f19b282 | 2011-04-27 02:14:07 -0500 | [diff] [blame] | 277 | "manual power-up failed\n"); |
| 278 | twl6040->power_count = 0; |
| 279 | goto out; |
| 280 | } |
| 281 | } |
Peter Ujfalusi | cfb7a33 | 2011-07-04 10:28:28 +0300 | [diff] [blame] | 282 | /* Default PLL configuration after power up */ |
| 283 | twl6040->pll = TWL6040_SYSCLK_SEL_LPPLL; |
Misael Lopez Cruz | f19b282 | 2011-04-27 02:14:07 -0500 | [diff] [blame] | 284 | twl6040->sysclk = 19200000; |
Peter Ujfalusi | f8447d6 | 2012-01-14 20:58:43 +0100 | [diff] [blame] | 285 | twl6040->mclk = 32768; |
Misael Lopez Cruz | f19b282 | 2011-04-27 02:14:07 -0500 | [diff] [blame] | 286 | } else { |
| 287 | /* already powered-down */ |
| 288 | if (!twl6040->power_count) { |
Peter Ujfalusi | 2d7c957 | 2011-09-15 15:39:23 +0300 | [diff] [blame] | 289 | dev_err(twl6040->dev, |
Misael Lopez Cruz | f19b282 | 2011-04-27 02:14:07 -0500 | [diff] [blame] | 290 | "device is already powered-off\n"); |
| 291 | ret = -EPERM; |
| 292 | goto out; |
| 293 | } |
| 294 | |
| 295 | if (--twl6040->power_count) |
| 296 | goto out; |
| 297 | |
| 298 | if (gpio_is_valid(audpwron)) { |
| 299 | /* use AUDPWRON line */ |
| 300 | gpio_set_value(audpwron, 0); |
| 301 | |
| 302 | /* power-down sequence latency */ |
| 303 | usleep_range(500, 700); |
| 304 | } else { |
| 305 | /* use manual power-down sequence */ |
| 306 | twl6040_power_down(twl6040); |
| 307 | } |
Misael Lopez Cruz | f19b282 | 2011-04-27 02:14:07 -0500 | [diff] [blame] | 308 | twl6040->sysclk = 0; |
Peter Ujfalusi | f8447d6 | 2012-01-14 20:58:43 +0100 | [diff] [blame] | 309 | twl6040->mclk = 0; |
Misael Lopez Cruz | f19b282 | 2011-04-27 02:14:07 -0500 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | out: |
| 313 | mutex_unlock(&twl6040->mutex); |
| 314 | return ret; |
| 315 | } |
| 316 | EXPORT_SYMBOL(twl6040_power); |
| 317 | |
Peter Ujfalusi | cfb7a33 | 2011-07-04 10:28:28 +0300 | [diff] [blame] | 318 | int twl6040_set_pll(struct twl6040 *twl6040, int pll_id, |
Misael Lopez Cruz | f19b282 | 2011-04-27 02:14:07 -0500 | [diff] [blame] | 319 | unsigned int freq_in, unsigned int freq_out) |
| 320 | { |
| 321 | u8 hppllctl, lppllctl; |
| 322 | int ret = 0; |
| 323 | |
| 324 | mutex_lock(&twl6040->mutex); |
| 325 | |
| 326 | hppllctl = twl6040_reg_read(twl6040, TWL6040_REG_HPPLLCTL); |
| 327 | lppllctl = twl6040_reg_read(twl6040, TWL6040_REG_LPPLLCTL); |
| 328 | |
Peter Ujfalusi | 2bd05db | 2012-01-14 20:58:44 +0100 | [diff] [blame] | 329 | /* Force full reconfiguration when switching between PLL */ |
| 330 | if (pll_id != twl6040->pll) { |
| 331 | twl6040->sysclk = 0; |
| 332 | twl6040->mclk = 0; |
| 333 | } |
| 334 | |
Peter Ujfalusi | cfb7a33 | 2011-07-04 10:28:28 +0300 | [diff] [blame] | 335 | switch (pll_id) { |
| 336 | case TWL6040_SYSCLK_SEL_LPPLL: |
Misael Lopez Cruz | f19b282 | 2011-04-27 02:14:07 -0500 | [diff] [blame] | 337 | /* low-power PLL divider */ |
Peter Ujfalusi | 2bd05db | 2012-01-14 20:58:44 +0100 | [diff] [blame] | 338 | /* Change the sysclk configuration only if it has been canged */ |
| 339 | if (twl6040->sysclk != freq_out) { |
| 340 | switch (freq_out) { |
| 341 | case 17640000: |
| 342 | lppllctl |= TWL6040_LPLLFIN; |
| 343 | break; |
| 344 | case 19200000: |
| 345 | lppllctl &= ~TWL6040_LPLLFIN; |
| 346 | break; |
| 347 | default: |
| 348 | dev_err(twl6040->dev, |
| 349 | "freq_out %d not supported\n", |
| 350 | freq_out); |
| 351 | ret = -EINVAL; |
| 352 | goto pll_out; |
| 353 | } |
| 354 | twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL, |
| 355 | lppllctl); |
Misael Lopez Cruz | f19b282 | 2011-04-27 02:14:07 -0500 | [diff] [blame] | 356 | } |
Peter Ujfalusi | 2bd05db | 2012-01-14 20:58:44 +0100 | [diff] [blame] | 357 | |
| 358 | /* The PLL in use has not been change, we can exit */ |
| 359 | if (twl6040->pll == pll_id) |
| 360 | break; |
Misael Lopez Cruz | f19b282 | 2011-04-27 02:14:07 -0500 | [diff] [blame] | 361 | |
| 362 | switch (freq_in) { |
| 363 | case 32768: |
| 364 | lppllctl |= TWL6040_LPLLENA; |
| 365 | twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL, |
| 366 | lppllctl); |
| 367 | mdelay(5); |
| 368 | lppllctl &= ~TWL6040_HPLLSEL; |
| 369 | twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL, |
| 370 | lppllctl); |
| 371 | hppllctl &= ~TWL6040_HPLLENA; |
| 372 | twl6040_reg_write(twl6040, TWL6040_REG_HPPLLCTL, |
| 373 | hppllctl); |
| 374 | break; |
| 375 | default: |
Peter Ujfalusi | 2d7c957 | 2011-09-15 15:39:23 +0300 | [diff] [blame] | 376 | dev_err(twl6040->dev, |
Misael Lopez Cruz | f19b282 | 2011-04-27 02:14:07 -0500 | [diff] [blame] | 377 | "freq_in %d not supported\n", freq_in); |
| 378 | ret = -EINVAL; |
| 379 | goto pll_out; |
| 380 | } |
Misael Lopez Cruz | f19b282 | 2011-04-27 02:14:07 -0500 | [diff] [blame] | 381 | break; |
Peter Ujfalusi | cfb7a33 | 2011-07-04 10:28:28 +0300 | [diff] [blame] | 382 | case TWL6040_SYSCLK_SEL_HPPLL: |
Misael Lopez Cruz | f19b282 | 2011-04-27 02:14:07 -0500 | [diff] [blame] | 383 | /* high-performance PLL can provide only 19.2 MHz */ |
| 384 | if (freq_out != 19200000) { |
Peter Ujfalusi | 2d7c957 | 2011-09-15 15:39:23 +0300 | [diff] [blame] | 385 | dev_err(twl6040->dev, |
Misael Lopez Cruz | f19b282 | 2011-04-27 02:14:07 -0500 | [diff] [blame] | 386 | "freq_out %d not supported\n", freq_out); |
| 387 | ret = -EINVAL; |
| 388 | goto pll_out; |
| 389 | } |
| 390 | |
Peter Ujfalusi | 2bd05db | 2012-01-14 20:58:44 +0100 | [diff] [blame] | 391 | if (twl6040->mclk != freq_in) { |
| 392 | hppllctl &= ~TWL6040_MCLK_MSK; |
Misael Lopez Cruz | f19b282 | 2011-04-27 02:14:07 -0500 | [diff] [blame] | 393 | |
Peter Ujfalusi | 2bd05db | 2012-01-14 20:58:44 +0100 | [diff] [blame] | 394 | switch (freq_in) { |
| 395 | case 12000000: |
| 396 | /* PLL enabled, active mode */ |
| 397 | hppllctl |= TWL6040_MCLK_12000KHZ | |
| 398 | TWL6040_HPLLENA; |
| 399 | break; |
| 400 | case 19200000: |
| 401 | /* |
| 402 | * PLL disabled |
| 403 | * (enable PLL if MCLK jitter quality |
| 404 | * doesn't meet specification) |
| 405 | */ |
| 406 | hppllctl |= TWL6040_MCLK_19200KHZ; |
| 407 | break; |
| 408 | case 26000000: |
| 409 | /* PLL enabled, active mode */ |
| 410 | hppllctl |= TWL6040_MCLK_26000KHZ | |
| 411 | TWL6040_HPLLENA; |
| 412 | break; |
| 413 | case 38400000: |
| 414 | /* PLL enabled, active mode */ |
| 415 | hppllctl |= TWL6040_MCLK_38400KHZ | |
| 416 | TWL6040_HPLLENA; |
| 417 | break; |
| 418 | default: |
| 419 | dev_err(twl6040->dev, |
| 420 | "freq_in %d not supported\n", freq_in); |
| 421 | ret = -EINVAL; |
| 422 | goto pll_out; |
| 423 | } |
| 424 | |
Misael Lopez Cruz | f19b282 | 2011-04-27 02:14:07 -0500 | [diff] [blame] | 425 | /* |
Peter Ujfalusi | 2bd05db | 2012-01-14 20:58:44 +0100 | [diff] [blame] | 426 | * enable clock slicer to ensure input waveform is |
| 427 | * square |
Misael Lopez Cruz | f19b282 | 2011-04-27 02:14:07 -0500 | [diff] [blame] | 428 | */ |
Peter Ujfalusi | 2bd05db | 2012-01-14 20:58:44 +0100 | [diff] [blame] | 429 | hppllctl |= TWL6040_HPLLSQRENA; |
| 430 | |
| 431 | twl6040_reg_write(twl6040, TWL6040_REG_HPPLLCTL, |
| 432 | hppllctl); |
| 433 | usleep_range(500, 700); |
| 434 | lppllctl |= TWL6040_HPLLSEL; |
| 435 | twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL, |
| 436 | lppllctl); |
| 437 | lppllctl &= ~TWL6040_LPLLENA; |
| 438 | twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL, |
| 439 | lppllctl); |
Misael Lopez Cruz | f19b282 | 2011-04-27 02:14:07 -0500 | [diff] [blame] | 440 | } |
Misael Lopez Cruz | f19b282 | 2011-04-27 02:14:07 -0500 | [diff] [blame] | 441 | break; |
| 442 | default: |
Peter Ujfalusi | 2d7c957 | 2011-09-15 15:39:23 +0300 | [diff] [blame] | 443 | dev_err(twl6040->dev, "unknown pll id %d\n", pll_id); |
Misael Lopez Cruz | f19b282 | 2011-04-27 02:14:07 -0500 | [diff] [blame] | 444 | ret = -EINVAL; |
| 445 | goto pll_out; |
| 446 | } |
| 447 | |
| 448 | twl6040->sysclk = freq_out; |
Peter Ujfalusi | f8447d6 | 2012-01-14 20:58:43 +0100 | [diff] [blame] | 449 | twl6040->mclk = freq_in; |
Peter Ujfalusi | cfb7a33 | 2011-07-04 10:28:28 +0300 | [diff] [blame] | 450 | twl6040->pll = pll_id; |
Misael Lopez Cruz | f19b282 | 2011-04-27 02:14:07 -0500 | [diff] [blame] | 451 | |
| 452 | pll_out: |
| 453 | mutex_unlock(&twl6040->mutex); |
| 454 | return ret; |
| 455 | } |
| 456 | EXPORT_SYMBOL(twl6040_set_pll); |
| 457 | |
Peter Ujfalusi | cfb7a33 | 2011-07-04 10:28:28 +0300 | [diff] [blame] | 458 | int twl6040_get_pll(struct twl6040 *twl6040) |
Misael Lopez Cruz | f19b282 | 2011-04-27 02:14:07 -0500 | [diff] [blame] | 459 | { |
Peter Ujfalusi | cfb7a33 | 2011-07-04 10:28:28 +0300 | [diff] [blame] | 460 | if (twl6040->power_count) |
| 461 | return twl6040->pll; |
| 462 | else |
| 463 | return -ENODEV; |
Misael Lopez Cruz | f19b282 | 2011-04-27 02:14:07 -0500 | [diff] [blame] | 464 | } |
| 465 | EXPORT_SYMBOL(twl6040_get_pll); |
| 466 | |
| 467 | unsigned int twl6040_get_sysclk(struct twl6040 *twl6040) |
| 468 | { |
| 469 | return twl6040->sysclk; |
| 470 | } |
| 471 | EXPORT_SYMBOL(twl6040_get_sysclk); |
| 472 | |
Peter Ujfalusi | 70601ec | 2011-10-12 11:57:55 +0300 | [diff] [blame] | 473 | /* Get the combined status of the vibra control register */ |
| 474 | int twl6040_get_vibralr_status(struct twl6040 *twl6040) |
| 475 | { |
| 476 | u8 status; |
| 477 | |
| 478 | status = twl6040->vibra_ctrl_cache[0] | twl6040->vibra_ctrl_cache[1]; |
| 479 | status &= (TWL6040_VIBENA | TWL6040_VIBSEL); |
| 480 | |
| 481 | return status; |
| 482 | } |
| 483 | EXPORT_SYMBOL(twl6040_get_vibralr_status); |
| 484 | |
Peter Ujfalusi | 0f962ae | 2011-07-05 11:40:33 +0300 | [diff] [blame] | 485 | static struct resource twl6040_vibra_rsrc[] = { |
| 486 | { |
| 487 | .flags = IORESOURCE_IRQ, |
| 488 | }, |
| 489 | }; |
| 490 | |
| 491 | static struct resource twl6040_codec_rsrc[] = { |
| 492 | { |
| 493 | .flags = IORESOURCE_IRQ, |
| 494 | }, |
| 495 | }; |
| 496 | |
Misael Lopez Cruz | f19b282 | 2011-04-27 02:14:07 -0500 | [diff] [blame] | 497 | static int __devinit twl6040_probe(struct platform_device *pdev) |
| 498 | { |
| 499 | struct twl4030_audio_data *pdata = pdev->dev.platform_data; |
| 500 | struct twl6040 *twl6040; |
| 501 | struct mfd_cell *cell = NULL; |
| 502 | int ret, children = 0; |
| 503 | |
| 504 | if (!pdata) { |
| 505 | dev_err(&pdev->dev, "Platform data is missing\n"); |
| 506 | return -EINVAL; |
| 507 | } |
| 508 | |
Peter Ujfalusi | d20e1d2 | 2011-07-04 20:15:19 +0300 | [diff] [blame] | 509 | /* In order to operate correctly we need valid interrupt config */ |
| 510 | if (!pdata->naudint_irq || !pdata->irq_base) { |
| 511 | dev_err(&pdev->dev, "Invalid IRQ configuration\n"); |
| 512 | return -EINVAL; |
| 513 | } |
| 514 | |
Misael Lopez Cruz | f19b282 | 2011-04-27 02:14:07 -0500 | [diff] [blame] | 515 | twl6040 = kzalloc(sizeof(struct twl6040), GFP_KERNEL); |
| 516 | if (!twl6040) |
| 517 | return -ENOMEM; |
| 518 | |
| 519 | platform_set_drvdata(pdev, twl6040); |
| 520 | |
Misael Lopez Cruz | f19b282 | 2011-04-27 02:14:07 -0500 | [diff] [blame] | 521 | twl6040->dev = &pdev->dev; |
Misael Lopez Cruz | f19b282 | 2011-04-27 02:14:07 -0500 | [diff] [blame] | 522 | twl6040->irq = pdata->naudint_irq; |
| 523 | twl6040->irq_base = pdata->irq_base; |
| 524 | |
| 525 | mutex_init(&twl6040->mutex); |
| 526 | mutex_init(&twl6040->io_mutex); |
| 527 | init_completion(&twl6040->ready); |
| 528 | |
| 529 | twl6040->rev = twl6040_reg_read(twl6040, TWL6040_REG_ASICREV); |
| 530 | |
Peter Ujfalusi | 77f63e0 | 2011-09-15 15:39:26 +0300 | [diff] [blame] | 531 | /* ERRATA: Automatic power-up is not possible in ES1.0 */ |
| 532 | if (twl6040_get_revid(twl6040) > TWL6040_REV_ES1_0) |
| 533 | twl6040->audpwron = pdata->audpwron_gpio; |
| 534 | else |
| 535 | twl6040->audpwron = -EINVAL; |
| 536 | |
Misael Lopez Cruz | f19b282 | 2011-04-27 02:14:07 -0500 | [diff] [blame] | 537 | if (gpio_is_valid(twl6040->audpwron)) { |
Axel Lin | b04edb9 | 2011-12-01 09:55:07 +0800 | [diff] [blame] | 538 | ret = gpio_request_one(twl6040->audpwron, GPIOF_OUT_INIT_LOW, |
| 539 | "audpwron"); |
Misael Lopez Cruz | f19b282 | 2011-04-27 02:14:07 -0500 | [diff] [blame] | 540 | if (ret) |
| 541 | goto gpio1_err; |
Misael Lopez Cruz | f19b282 | 2011-04-27 02:14:07 -0500 | [diff] [blame] | 542 | } |
| 543 | |
Peter Ujfalusi | d20e1d2 | 2011-07-04 20:15:19 +0300 | [diff] [blame] | 544 | /* codec interrupt */ |
| 545 | ret = twl6040_irq_init(twl6040); |
| 546 | if (ret) |
| 547 | goto gpio2_err; |
Misael Lopez Cruz | f19b282 | 2011-04-27 02:14:07 -0500 | [diff] [blame] | 548 | |
Peter Ujfalusi | 1b7c472 | 2011-07-04 20:16:23 +0300 | [diff] [blame] | 549 | ret = request_threaded_irq(twl6040->irq_base + TWL6040_IRQ_READY, |
| 550 | NULL, twl6040_naudint_handler, 0, |
| 551 | "twl6040_irq_ready", twl6040); |
Peter Ujfalusi | d20e1d2 | 2011-07-04 20:15:19 +0300 | [diff] [blame] | 552 | if (ret) { |
| 553 | dev_err(twl6040->dev, "READY IRQ request failed: %d\n", |
| 554 | ret); |
| 555 | goto irq_err; |
Misael Lopez Cruz | f19b282 | 2011-04-27 02:14:07 -0500 | [diff] [blame] | 556 | } |
| 557 | |
| 558 | /* dual-access registers controlled by I2C only */ |
| 559 | twl6040_set_bits(twl6040, TWL6040_REG_ACCCTL, TWL6040_I2CSEL); |
| 560 | |
| 561 | if (pdata->codec) { |
Peter Ujfalusi | 0f962ae | 2011-07-05 11:40:33 +0300 | [diff] [blame] | 562 | int irq = twl6040->irq_base + TWL6040_IRQ_PLUG; |
| 563 | |
Misael Lopez Cruz | f19b282 | 2011-04-27 02:14:07 -0500 | [diff] [blame] | 564 | cell = &twl6040->cells[children]; |
| 565 | cell->name = "twl6040-codec"; |
Peter Ujfalusi | 0f962ae | 2011-07-05 11:40:33 +0300 | [diff] [blame] | 566 | twl6040_codec_rsrc[0].start = irq; |
| 567 | twl6040_codec_rsrc[0].end = irq; |
| 568 | cell->resources = twl6040_codec_rsrc; |
| 569 | cell->num_resources = ARRAY_SIZE(twl6040_codec_rsrc); |
Peter Ujfalusi | 6c44863 | 2011-06-01 13:05:10 +0300 | [diff] [blame] | 570 | cell->platform_data = pdata->codec; |
| 571 | cell->pdata_size = sizeof(*pdata->codec); |
Misael Lopez Cruz | f19b282 | 2011-04-27 02:14:07 -0500 | [diff] [blame] | 572 | children++; |
| 573 | } |
| 574 | |
| 575 | if (pdata->vibra) { |
Peter Ujfalusi | 0f962ae | 2011-07-05 11:40:33 +0300 | [diff] [blame] | 576 | int irq = twl6040->irq_base + TWL6040_IRQ_VIB; |
| 577 | |
Misael Lopez Cruz | f19b282 | 2011-04-27 02:14:07 -0500 | [diff] [blame] | 578 | cell = &twl6040->cells[children]; |
| 579 | cell->name = "twl6040-vibra"; |
Peter Ujfalusi | 0f962ae | 2011-07-05 11:40:33 +0300 | [diff] [blame] | 580 | twl6040_vibra_rsrc[0].start = irq; |
| 581 | twl6040_vibra_rsrc[0].end = irq; |
| 582 | cell->resources = twl6040_vibra_rsrc; |
| 583 | cell->num_resources = ARRAY_SIZE(twl6040_vibra_rsrc); |
| 584 | |
Misael Lopez Cruz | f19b282 | 2011-04-27 02:14:07 -0500 | [diff] [blame] | 585 | cell->platform_data = pdata->vibra; |
| 586 | cell->pdata_size = sizeof(*pdata->vibra); |
| 587 | children++; |
| 588 | } |
| 589 | |
| 590 | if (children) { |
| 591 | ret = mfd_add_devices(&pdev->dev, pdev->id, twl6040->cells, |
| 592 | children, NULL, 0); |
| 593 | if (ret) |
| 594 | goto mfd_err; |
| 595 | } else { |
| 596 | dev_err(&pdev->dev, "No platform data found for children\n"); |
| 597 | ret = -ENODEV; |
| 598 | goto mfd_err; |
| 599 | } |
| 600 | |
| 601 | return 0; |
| 602 | |
| 603 | mfd_err: |
Peter Ujfalusi | 1b7c472 | 2011-07-04 20:16:23 +0300 | [diff] [blame] | 604 | free_irq(twl6040->irq_base + TWL6040_IRQ_READY, twl6040); |
Misael Lopez Cruz | f19b282 | 2011-04-27 02:14:07 -0500 | [diff] [blame] | 605 | irq_err: |
Peter Ujfalusi | d20e1d2 | 2011-07-04 20:15:19 +0300 | [diff] [blame] | 606 | twl6040_irq_exit(twl6040); |
Misael Lopez Cruz | f19b282 | 2011-04-27 02:14:07 -0500 | [diff] [blame] | 607 | gpio2_err: |
| 608 | if (gpio_is_valid(twl6040->audpwron)) |
| 609 | gpio_free(twl6040->audpwron); |
| 610 | gpio1_err: |
| 611 | platform_set_drvdata(pdev, NULL); |
| 612 | kfree(twl6040); |
Misael Lopez Cruz | f19b282 | 2011-04-27 02:14:07 -0500 | [diff] [blame] | 613 | return ret; |
| 614 | } |
| 615 | |
| 616 | static int __devexit twl6040_remove(struct platform_device *pdev) |
| 617 | { |
| 618 | struct twl6040 *twl6040 = platform_get_drvdata(pdev); |
| 619 | |
| 620 | if (twl6040->power_count) |
| 621 | twl6040_power(twl6040, 0); |
| 622 | |
| 623 | if (gpio_is_valid(twl6040->audpwron)) |
| 624 | gpio_free(twl6040->audpwron); |
| 625 | |
Peter Ujfalusi | 1b7c472 | 2011-07-04 20:16:23 +0300 | [diff] [blame] | 626 | free_irq(twl6040->irq_base + TWL6040_IRQ_READY, twl6040); |
Peter Ujfalusi | d20e1d2 | 2011-07-04 20:15:19 +0300 | [diff] [blame] | 627 | twl6040_irq_exit(twl6040); |
Misael Lopez Cruz | f19b282 | 2011-04-27 02:14:07 -0500 | [diff] [blame] | 628 | |
| 629 | mfd_remove_devices(&pdev->dev); |
| 630 | platform_set_drvdata(pdev, NULL); |
| 631 | kfree(twl6040); |
Misael Lopez Cruz | f19b282 | 2011-04-27 02:14:07 -0500 | [diff] [blame] | 632 | |
| 633 | return 0; |
| 634 | } |
| 635 | |
| 636 | static struct platform_driver twl6040_driver = { |
| 637 | .probe = twl6040_probe, |
| 638 | .remove = __devexit_p(twl6040_remove), |
| 639 | .driver = { |
| 640 | .owner = THIS_MODULE, |
| 641 | .name = "twl6040", |
| 642 | }, |
| 643 | }; |
| 644 | |
Mark Brown | 65349d6 | 2011-11-23 22:58:34 +0000 | [diff] [blame] | 645 | module_platform_driver(twl6040_driver); |
Misael Lopez Cruz | f19b282 | 2011-04-27 02:14:07 -0500 | [diff] [blame] | 646 | |
| 647 | MODULE_DESCRIPTION("TWL6040 MFD"); |
| 648 | MODULE_AUTHOR("Misael Lopez Cruz <misael.lopez@ti.com>"); |
| 649 | MODULE_AUTHOR("Jorge Eduardo Candelaria <jorge.candelaria@ti.com>"); |
| 650 | MODULE_LICENSE("GPL"); |
| 651 | MODULE_ALIAS("platform:twl6040"); |