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