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> |
Peter Ujfalusi | 37e13ce | 2012-05-16 14:11:58 +0300 | [diff] [blame] | 32 | #include <linux/of.h> |
| 33 | #include <linux/of_irq.h> |
| 34 | #include <linux/of_gpio.h> |
| 35 | #include <linux/of_platform.h> |
Misael Lopez Cruz | f19b282 | 2011-04-27 02:14:07 -0500 | [diff] [blame] | 36 | #include <linux/gpio.h> |
| 37 | #include <linux/delay.h> |
Peter Ujfalusi | 8eaeb93 | 2012-04-03 11:56:51 +0300 | [diff] [blame] | 38 | #include <linux/i2c.h> |
| 39 | #include <linux/regmap.h> |
Misael Lopez Cruz | f19b282 | 2011-04-27 02:14:07 -0500 | [diff] [blame] | 40 | #include <linux/mfd/core.h> |
| 41 | #include <linux/mfd/twl6040.h> |
Peter Ujfalusi | 5af7df6 | 2012-05-02 16:54:42 +0300 | [diff] [blame] | 42 | #include <linux/regulator/consumer.h> |
Misael Lopez Cruz | f19b282 | 2011-04-27 02:14:07 -0500 | [diff] [blame] | 43 | |
Peter Ujfalusi | 31b402e | 2011-10-12 11:57:54 +0300 | [diff] [blame] | 44 | #define VIBRACTRL_MEMBER(reg) ((reg == TWL6040_REG_VIBCTLL) ? 0 : 1) |
Peter Ujfalusi | 5af7df6 | 2012-05-02 16:54:42 +0300 | [diff] [blame] | 45 | #define TWL6040_NUM_SUPPLIES (2) |
Peter Ujfalusi | 31b402e | 2011-10-12 11:57:54 +0300 | [diff] [blame] | 46 | |
Krzysztof Kozlowski | de1e23f | 2015-01-05 10:01:33 +0100 | [diff] [blame] | 47 | static const struct reg_default twl6040_defaults[] = { |
Peter Ujfalusi | c7f9129 | 2013-11-29 16:03:45 +0200 | [diff] [blame] | 48 | { 0x01, 0x4B }, /* REG_ASICID (ro) */ |
| 49 | { 0x02, 0x00 }, /* REG_ASICREV (ro) */ |
| 50 | { 0x03, 0x00 }, /* REG_INTID */ |
| 51 | { 0x04, 0x00 }, /* REG_INTMR */ |
| 52 | { 0x05, 0x00 }, /* REG_NCPCTRL */ |
| 53 | { 0x06, 0x00 }, /* REG_LDOCTL */ |
| 54 | { 0x07, 0x60 }, /* REG_HPPLLCTL */ |
| 55 | { 0x08, 0x00 }, /* REG_LPPLLCTL */ |
| 56 | { 0x09, 0x4A }, /* REG_LPPLLDIV */ |
| 57 | { 0x0A, 0x00 }, /* REG_AMICBCTL */ |
| 58 | { 0x0B, 0x00 }, /* REG_DMICBCTL */ |
| 59 | { 0x0C, 0x00 }, /* REG_MICLCTL */ |
| 60 | { 0x0D, 0x00 }, /* REG_MICRCTL */ |
| 61 | { 0x0E, 0x00 }, /* REG_MICGAIN */ |
| 62 | { 0x0F, 0x1B }, /* REG_LINEGAIN */ |
| 63 | { 0x10, 0x00 }, /* REG_HSLCTL */ |
| 64 | { 0x11, 0x00 }, /* REG_HSRCTL */ |
| 65 | { 0x12, 0x00 }, /* REG_HSGAIN */ |
| 66 | { 0x13, 0x00 }, /* REG_EARCTL */ |
| 67 | { 0x14, 0x00 }, /* REG_HFLCTL */ |
| 68 | { 0x15, 0x00 }, /* REG_HFLGAIN */ |
| 69 | { 0x16, 0x00 }, /* REG_HFRCTL */ |
| 70 | { 0x17, 0x00 }, /* REG_HFRGAIN */ |
| 71 | { 0x18, 0x00 }, /* REG_VIBCTLL */ |
| 72 | { 0x19, 0x00 }, /* REG_VIBDATL */ |
| 73 | { 0x1A, 0x00 }, /* REG_VIBCTLR */ |
| 74 | { 0x1B, 0x00 }, /* REG_VIBDATR */ |
| 75 | { 0x1C, 0x00 }, /* REG_HKCTL1 */ |
| 76 | { 0x1D, 0x00 }, /* REG_HKCTL2 */ |
| 77 | { 0x1E, 0x00 }, /* REG_GPOCTL */ |
| 78 | { 0x1F, 0x00 }, /* REG_ALB */ |
| 79 | { 0x20, 0x00 }, /* REG_DLB */ |
| 80 | /* 0x28, REG_TRIM1 */ |
| 81 | /* 0x29, REG_TRIM2 */ |
| 82 | /* 0x2A, REG_TRIM3 */ |
| 83 | /* 0x2B, REG_HSOTRIM */ |
| 84 | /* 0x2C, REG_HFOTRIM */ |
| 85 | { 0x2D, 0x08 }, /* REG_ACCCTL */ |
| 86 | { 0x2E, 0x00 }, /* REG_STATUS (ro) */ |
| 87 | }; |
| 88 | |
Nariman Poushin | 8019ff6 | 2015-07-16 16:36:21 +0100 | [diff] [blame] | 89 | static struct reg_sequence twl6040_patch[] = { |
Peter Ujfalusi | 11e38e1 | 2014-04-01 16:44:59 +0300 | [diff] [blame] | 90 | /* |
| 91 | * Select I2C bus access to dual access registers |
| 92 | * Interrupt register is cleared on read |
| 93 | * Select fast mode for i2c (400KHz) |
| 94 | */ |
| 95 | { TWL6040_REG_ACCCTL, |
| 96 | TWL6040_I2CSEL | TWL6040_INTCLRMODE | TWL6040_I2CMODE(1) }, |
Peter Ujfalusi | c7f9129 | 2013-11-29 16:03:45 +0200 | [diff] [blame] | 97 | }; |
| 98 | |
| 99 | |
Peter Ujfalusi | df04b62 | 2013-07-12 13:32:02 +0200 | [diff] [blame] | 100 | static bool twl6040_has_vibra(struct device_node *node) |
Samuel Ortiz | ca2cad6 | 2012-05-23 16:23:21 +0200 | [diff] [blame] | 101 | { |
Samuel Ortiz | ca2cad6 | 2012-05-23 16:23:21 +0200 | [diff] [blame] | 102 | #ifdef CONFIG_OF |
| 103 | if (of_find_node_by_name(node, "vibra")) |
| 104 | return true; |
| 105 | #endif |
Samuel Ortiz | ca2cad6 | 2012-05-23 16:23:21 +0200 | [diff] [blame] | 106 | return false; |
| 107 | } |
| 108 | |
Misael Lopez Cruz | f19b282 | 2011-04-27 02:14:07 -0500 | [diff] [blame] | 109 | int twl6040_reg_read(struct twl6040 *twl6040, unsigned int reg) |
| 110 | { |
| 111 | int ret; |
Peter Ujfalusi | 8eaeb93 | 2012-04-03 11:56:51 +0300 | [diff] [blame] | 112 | unsigned int val; |
Misael Lopez Cruz | f19b282 | 2011-04-27 02:14:07 -0500 | [diff] [blame] | 113 | |
Mark Brown | c6f3925 | 2013-08-31 17:48:19 +0100 | [diff] [blame] | 114 | ret = regmap_read(twl6040->regmap, reg, &val); |
| 115 | if (ret < 0) |
| 116 | return ret; |
Misael Lopez Cruz | f19b282 | 2011-04-27 02:14:07 -0500 | [diff] [blame] | 117 | |
| 118 | return val; |
| 119 | } |
| 120 | EXPORT_SYMBOL(twl6040_reg_read); |
| 121 | |
| 122 | int twl6040_reg_write(struct twl6040 *twl6040, unsigned int reg, u8 val) |
| 123 | { |
| 124 | int ret; |
| 125 | |
Peter Ujfalusi | 8eaeb93 | 2012-04-03 11:56:51 +0300 | [diff] [blame] | 126 | ret = regmap_write(twl6040->regmap, reg, val); |
Misael Lopez Cruz | f19b282 | 2011-04-27 02:14:07 -0500 | [diff] [blame] | 127 | |
| 128 | return ret; |
| 129 | } |
| 130 | EXPORT_SYMBOL(twl6040_reg_write); |
| 131 | |
| 132 | int twl6040_set_bits(struct twl6040 *twl6040, unsigned int reg, u8 mask) |
| 133 | { |
Axel Lin | c600040 | 2012-07-11 10:06:34 +0800 | [diff] [blame] | 134 | return regmap_update_bits(twl6040->regmap, reg, mask, mask); |
Misael Lopez Cruz | f19b282 | 2011-04-27 02:14:07 -0500 | [diff] [blame] | 135 | } |
| 136 | EXPORT_SYMBOL(twl6040_set_bits); |
| 137 | |
| 138 | int twl6040_clear_bits(struct twl6040 *twl6040, unsigned int reg, u8 mask) |
| 139 | { |
Axel Lin | c600040 | 2012-07-11 10:06:34 +0800 | [diff] [blame] | 140 | return regmap_update_bits(twl6040->regmap, reg, mask, 0); |
Misael Lopez Cruz | f19b282 | 2011-04-27 02:14:07 -0500 | [diff] [blame] | 141 | } |
| 142 | EXPORT_SYMBOL(twl6040_clear_bits); |
| 143 | |
| 144 | /* twl6040 codec manual power-up sequence */ |
Peter Ujfalusi | f9be134 | 2012-10-11 13:55:30 +0200 | [diff] [blame] | 145 | static int twl6040_power_up_manual(struct twl6040 *twl6040) |
Misael Lopez Cruz | f19b282 | 2011-04-27 02:14:07 -0500 | [diff] [blame] | 146 | { |
| 147 | u8 ldoctl, ncpctl, lppllctl; |
| 148 | int ret; |
| 149 | |
| 150 | /* enable high-side LDO, reference system and internal oscillator */ |
| 151 | ldoctl = TWL6040_HSLDOENA | TWL6040_REFENA | TWL6040_OSCENA; |
| 152 | ret = twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl); |
| 153 | if (ret) |
| 154 | return ret; |
| 155 | usleep_range(10000, 10500); |
| 156 | |
| 157 | /* enable negative charge pump */ |
| 158 | ncpctl = TWL6040_NCPENA; |
| 159 | ret = twl6040_reg_write(twl6040, TWL6040_REG_NCPCTL, ncpctl); |
| 160 | if (ret) |
| 161 | goto ncp_err; |
| 162 | usleep_range(1000, 1500); |
| 163 | |
| 164 | /* enable low-side LDO */ |
| 165 | ldoctl |= TWL6040_LSLDOENA; |
| 166 | ret = twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl); |
| 167 | if (ret) |
| 168 | goto lsldo_err; |
| 169 | usleep_range(1000, 1500); |
| 170 | |
| 171 | /* enable low-power PLL */ |
| 172 | lppllctl = TWL6040_LPLLENA; |
| 173 | ret = twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL, lppllctl); |
| 174 | if (ret) |
| 175 | goto lppll_err; |
| 176 | usleep_range(5000, 5500); |
| 177 | |
| 178 | /* disable internal oscillator */ |
| 179 | ldoctl &= ~TWL6040_OSCENA; |
| 180 | ret = twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl); |
| 181 | if (ret) |
| 182 | goto osc_err; |
| 183 | |
| 184 | return 0; |
| 185 | |
| 186 | osc_err: |
| 187 | lppllctl &= ~TWL6040_LPLLENA; |
| 188 | twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL, lppllctl); |
| 189 | lppll_err: |
| 190 | ldoctl &= ~TWL6040_LSLDOENA; |
| 191 | twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl); |
| 192 | lsldo_err: |
| 193 | ncpctl &= ~TWL6040_NCPENA; |
| 194 | twl6040_reg_write(twl6040, TWL6040_REG_NCPCTL, ncpctl); |
| 195 | ncp_err: |
| 196 | ldoctl &= ~(TWL6040_HSLDOENA | TWL6040_REFENA | TWL6040_OSCENA); |
| 197 | twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl); |
| 198 | |
Peter Ujfalusi | f9be134 | 2012-10-11 13:55:30 +0200 | [diff] [blame] | 199 | dev_err(twl6040->dev, "manual power-up failed\n"); |
Misael Lopez Cruz | f19b282 | 2011-04-27 02:14:07 -0500 | [diff] [blame] | 200 | return ret; |
| 201 | } |
| 202 | |
| 203 | /* twl6040 manual power-down sequence */ |
Peter Ujfalusi | f9be134 | 2012-10-11 13:55:30 +0200 | [diff] [blame] | 204 | static void twl6040_power_down_manual(struct twl6040 *twl6040) |
Misael Lopez Cruz | f19b282 | 2011-04-27 02:14:07 -0500 | [diff] [blame] | 205 | { |
| 206 | u8 ncpctl, ldoctl, lppllctl; |
| 207 | |
| 208 | ncpctl = twl6040_reg_read(twl6040, TWL6040_REG_NCPCTL); |
| 209 | ldoctl = twl6040_reg_read(twl6040, TWL6040_REG_LDOCTL); |
| 210 | lppllctl = twl6040_reg_read(twl6040, TWL6040_REG_LPPLLCTL); |
| 211 | |
| 212 | /* enable internal oscillator */ |
| 213 | ldoctl |= TWL6040_OSCENA; |
| 214 | twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl); |
| 215 | usleep_range(1000, 1500); |
| 216 | |
| 217 | /* disable low-power PLL */ |
| 218 | lppllctl &= ~TWL6040_LPLLENA; |
| 219 | twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL, lppllctl); |
| 220 | |
| 221 | /* disable low-side LDO */ |
| 222 | ldoctl &= ~TWL6040_LSLDOENA; |
| 223 | twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl); |
| 224 | |
| 225 | /* disable negative charge pump */ |
| 226 | ncpctl &= ~TWL6040_NCPENA; |
| 227 | twl6040_reg_write(twl6040, TWL6040_REG_NCPCTL, ncpctl); |
| 228 | |
| 229 | /* disable high-side LDO, reference system and internal oscillator */ |
| 230 | ldoctl &= ~(TWL6040_HSLDOENA | TWL6040_REFENA | TWL6040_OSCENA); |
| 231 | twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl); |
| 232 | } |
| 233 | |
Peter Ujfalusi | 1ac9626 | 2012-10-11 13:55:31 +0200 | [diff] [blame] | 234 | static irqreturn_t twl6040_readyint_handler(int irq, void *data) |
Misael Lopez Cruz | f19b282 | 2011-04-27 02:14:07 -0500 | [diff] [blame] | 235 | { |
| 236 | struct twl6040 *twl6040 = data; |
Misael Lopez Cruz | f19b282 | 2011-04-27 02:14:07 -0500 | [diff] [blame] | 237 | |
Peter Ujfalusi | 1ac9626 | 2012-10-11 13:55:31 +0200 | [diff] [blame] | 238 | complete(&twl6040->ready); |
Misael Lopez Cruz | f19b282 | 2011-04-27 02:14:07 -0500 | [diff] [blame] | 239 | |
Peter Ujfalusi | 1ac9626 | 2012-10-11 13:55:31 +0200 | [diff] [blame] | 240 | return IRQ_HANDLED; |
| 241 | } |
Misael Lopez Cruz | f19b282 | 2011-04-27 02:14:07 -0500 | [diff] [blame] | 242 | |
Peter Ujfalusi | 1ac9626 | 2012-10-11 13:55:31 +0200 | [diff] [blame] | 243 | static irqreturn_t twl6040_thint_handler(int irq, void *data) |
| 244 | { |
| 245 | struct twl6040 *twl6040 = data; |
| 246 | u8 status; |
| 247 | |
| 248 | status = twl6040_reg_read(twl6040, TWL6040_REG_STATUS); |
| 249 | if (status & TWL6040_TSHUTDET) { |
| 250 | dev_warn(twl6040->dev, "Thermal shutdown, powering-off"); |
| 251 | twl6040_power(twl6040, 0); |
| 252 | } else { |
| 253 | dev_warn(twl6040->dev, "Leaving thermal shutdown, powering-on"); |
| 254 | twl6040_power(twl6040, 1); |
Misael Lopez Cruz | f19b282 | 2011-04-27 02:14:07 -0500 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | return IRQ_HANDLED; |
| 258 | } |
| 259 | |
Peter Ujfalusi | f9be134 | 2012-10-11 13:55:30 +0200 | [diff] [blame] | 260 | static int twl6040_power_up_automatic(struct twl6040 *twl6040) |
Misael Lopez Cruz | f19b282 | 2011-04-27 02:14:07 -0500 | [diff] [blame] | 261 | { |
| 262 | int time_left; |
Peter Ujfalusi | f9be134 | 2012-10-11 13:55:30 +0200 | [diff] [blame] | 263 | |
| 264 | gpio_set_value(twl6040->audpwron, 1); |
Misael Lopez Cruz | f19b282 | 2011-04-27 02:14:07 -0500 | [diff] [blame] | 265 | |
| 266 | time_left = wait_for_completion_timeout(&twl6040->ready, |
| 267 | msecs_to_jiffies(144)); |
| 268 | if (!time_left) { |
Peter Ujfalusi | f9be134 | 2012-10-11 13:55:30 +0200 | [diff] [blame] | 269 | u8 intid; |
| 270 | |
| 271 | dev_warn(twl6040->dev, "timeout waiting for READYINT\n"); |
Misael Lopez Cruz | f19b282 | 2011-04-27 02:14:07 -0500 | [diff] [blame] | 272 | intid = twl6040_reg_read(twl6040, TWL6040_REG_INTID); |
| 273 | if (!(intid & TWL6040_READYINT)) { |
Peter Ujfalusi | f9be134 | 2012-10-11 13:55:30 +0200 | [diff] [blame] | 274 | dev_err(twl6040->dev, "automatic power-up failed\n"); |
| 275 | gpio_set_value(twl6040->audpwron, 0); |
Misael Lopez Cruz | f19b282 | 2011-04-27 02:14:07 -0500 | [diff] [blame] | 276 | return -ETIMEDOUT; |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | return 0; |
| 281 | } |
| 282 | |
| 283 | int twl6040_power(struct twl6040 *twl6040, int on) |
| 284 | { |
Misael Lopez Cruz | f19b282 | 2011-04-27 02:14:07 -0500 | [diff] [blame] | 285 | int ret = 0; |
| 286 | |
| 287 | mutex_lock(&twl6040->mutex); |
| 288 | |
| 289 | if (on) { |
| 290 | /* already powered-up */ |
| 291 | if (twl6040->power_count++) |
| 292 | goto out; |
| 293 | |
Javier Martinez Canillas | 32852bc | 2016-04-20 11:16:38 -0400 | [diff] [blame] | 294 | ret = clk_prepare_enable(twl6040->clk32k); |
| 295 | if (ret) { |
| 296 | twl6040->power_count = 0; |
| 297 | goto out; |
| 298 | } |
Peter Ujfalusi | 68bab86 | 2014-04-03 13:54:41 +0300 | [diff] [blame] | 299 | |
Peter Ujfalusi | c7f9129 | 2013-11-29 16:03:45 +0200 | [diff] [blame] | 300 | /* Allow writes to the chip */ |
| 301 | regcache_cache_only(twl6040->regmap, false); |
| 302 | |
Peter Ujfalusi | f9be134 | 2012-10-11 13:55:30 +0200 | [diff] [blame] | 303 | if (gpio_is_valid(twl6040->audpwron)) { |
| 304 | /* use automatic power-up sequence */ |
| 305 | ret = twl6040_power_up_automatic(twl6040); |
Misael Lopez Cruz | f19b282 | 2011-04-27 02:14:07 -0500 | [diff] [blame] | 306 | if (ret) { |
Javier Martinez Canillas | d6441dc | 2016-04-20 11:16:37 -0400 | [diff] [blame] | 307 | clk_disable_unprepare(twl6040->clk32k); |
Misael Lopez Cruz | f19b282 | 2011-04-27 02:14:07 -0500 | [diff] [blame] | 308 | twl6040->power_count = 0; |
| 309 | goto out; |
| 310 | } |
| 311 | } else { |
| 312 | /* use manual power-up sequence */ |
Peter Ujfalusi | f9be134 | 2012-10-11 13:55:30 +0200 | [diff] [blame] | 313 | ret = twl6040_power_up_manual(twl6040); |
Misael Lopez Cruz | f19b282 | 2011-04-27 02:14:07 -0500 | [diff] [blame] | 314 | if (ret) { |
Javier Martinez Canillas | d6441dc | 2016-04-20 11:16:37 -0400 | [diff] [blame] | 315 | clk_disable_unprepare(twl6040->clk32k); |
Misael Lopez Cruz | f19b282 | 2011-04-27 02:14:07 -0500 | [diff] [blame] | 316 | twl6040->power_count = 0; |
| 317 | goto out; |
| 318 | } |
| 319 | } |
Peter Ujfalusi | c7f9129 | 2013-11-29 16:03:45 +0200 | [diff] [blame] | 320 | |
| 321 | /* Sync with the HW */ |
| 322 | regcache_sync(twl6040->regmap); |
| 323 | |
Peter Ujfalusi | cfb7a33 | 2011-07-04 10:28:28 +0300 | [diff] [blame] | 324 | /* Default PLL configuration after power up */ |
| 325 | twl6040->pll = TWL6040_SYSCLK_SEL_LPPLL; |
Peter Ujfalusi | 0a58da1 | 2016-06-20 17:07:18 +0300 | [diff] [blame^] | 326 | twl6040->sysclk_rate = 19200000; |
Misael Lopez Cruz | f19b282 | 2011-04-27 02:14:07 -0500 | [diff] [blame] | 327 | } else { |
| 328 | /* already powered-down */ |
| 329 | if (!twl6040->power_count) { |
Peter Ujfalusi | 2d7c957 | 2011-09-15 15:39:23 +0300 | [diff] [blame] | 330 | dev_err(twl6040->dev, |
Misael Lopez Cruz | f19b282 | 2011-04-27 02:14:07 -0500 | [diff] [blame] | 331 | "device is already powered-off\n"); |
| 332 | ret = -EPERM; |
| 333 | goto out; |
| 334 | } |
| 335 | |
| 336 | if (--twl6040->power_count) |
| 337 | goto out; |
| 338 | |
Peter Ujfalusi | f9be134 | 2012-10-11 13:55:30 +0200 | [diff] [blame] | 339 | if (gpio_is_valid(twl6040->audpwron)) { |
Misael Lopez Cruz | f19b282 | 2011-04-27 02:14:07 -0500 | [diff] [blame] | 340 | /* use AUDPWRON line */ |
Peter Ujfalusi | f9be134 | 2012-10-11 13:55:30 +0200 | [diff] [blame] | 341 | gpio_set_value(twl6040->audpwron, 0); |
Misael Lopez Cruz | f19b282 | 2011-04-27 02:14:07 -0500 | [diff] [blame] | 342 | |
| 343 | /* power-down sequence latency */ |
| 344 | usleep_range(500, 700); |
| 345 | } else { |
| 346 | /* use manual power-down sequence */ |
Peter Ujfalusi | f9be134 | 2012-10-11 13:55:30 +0200 | [diff] [blame] | 347 | twl6040_power_down_manual(twl6040); |
Misael Lopez Cruz | f19b282 | 2011-04-27 02:14:07 -0500 | [diff] [blame] | 348 | } |
Peter Ujfalusi | c7f9129 | 2013-11-29 16:03:45 +0200 | [diff] [blame] | 349 | |
| 350 | /* Set regmap to cache only and mark it as dirty */ |
| 351 | regcache_cache_only(twl6040->regmap, true); |
| 352 | regcache_mark_dirty(twl6040->regmap); |
| 353 | |
Peter Ujfalusi | 0a58da1 | 2016-06-20 17:07:18 +0300 | [diff] [blame^] | 354 | twl6040->sysclk_rate = 0; |
| 355 | |
| 356 | if (twl6040->pll == TWL6040_SYSCLK_SEL_HPPLL) { |
| 357 | clk_disable_unprepare(twl6040->mclk); |
| 358 | twl6040->mclk_rate = 0; |
| 359 | } |
Peter Ujfalusi | 68bab86 | 2014-04-03 13:54:41 +0300 | [diff] [blame] | 360 | |
| 361 | clk_disable_unprepare(twl6040->clk32k); |
Misael Lopez Cruz | f19b282 | 2011-04-27 02:14:07 -0500 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | out: |
| 365 | mutex_unlock(&twl6040->mutex); |
| 366 | return ret; |
| 367 | } |
| 368 | EXPORT_SYMBOL(twl6040_power); |
| 369 | |
Peter Ujfalusi | cfb7a33 | 2011-07-04 10:28:28 +0300 | [diff] [blame] | 370 | int twl6040_set_pll(struct twl6040 *twl6040, int pll_id, |
Misael Lopez Cruz | f19b282 | 2011-04-27 02:14:07 -0500 | [diff] [blame] | 371 | unsigned int freq_in, unsigned int freq_out) |
| 372 | { |
| 373 | u8 hppllctl, lppllctl; |
| 374 | int ret = 0; |
| 375 | |
| 376 | mutex_lock(&twl6040->mutex); |
| 377 | |
| 378 | hppllctl = twl6040_reg_read(twl6040, TWL6040_REG_HPPLLCTL); |
| 379 | lppllctl = twl6040_reg_read(twl6040, TWL6040_REG_LPPLLCTL); |
| 380 | |
Peter Ujfalusi | 2bd05db | 2012-01-14 20:58:44 +0100 | [diff] [blame] | 381 | /* Force full reconfiguration when switching between PLL */ |
| 382 | if (pll_id != twl6040->pll) { |
Peter Ujfalusi | 0a58da1 | 2016-06-20 17:07:18 +0300 | [diff] [blame^] | 383 | twl6040->sysclk_rate = 0; |
| 384 | twl6040->mclk_rate = 0; |
Peter Ujfalusi | 2bd05db | 2012-01-14 20:58:44 +0100 | [diff] [blame] | 385 | } |
| 386 | |
Peter Ujfalusi | cfb7a33 | 2011-07-04 10:28:28 +0300 | [diff] [blame] | 387 | switch (pll_id) { |
| 388 | case TWL6040_SYSCLK_SEL_LPPLL: |
Misael Lopez Cruz | f19b282 | 2011-04-27 02:14:07 -0500 | [diff] [blame] | 389 | /* low-power PLL divider */ |
Peter Ujfalusi | 2bd05db | 2012-01-14 20:58:44 +0100 | [diff] [blame] | 390 | /* Change the sysclk configuration only if it has been canged */ |
Peter Ujfalusi | 0a58da1 | 2016-06-20 17:07:18 +0300 | [diff] [blame^] | 391 | if (twl6040->sysclk_rate != freq_out) { |
Peter Ujfalusi | 2bd05db | 2012-01-14 20:58:44 +0100 | [diff] [blame] | 392 | switch (freq_out) { |
| 393 | case 17640000: |
| 394 | lppllctl |= TWL6040_LPLLFIN; |
| 395 | break; |
| 396 | case 19200000: |
| 397 | lppllctl &= ~TWL6040_LPLLFIN; |
| 398 | break; |
| 399 | default: |
| 400 | dev_err(twl6040->dev, |
| 401 | "freq_out %d not supported\n", |
| 402 | freq_out); |
| 403 | ret = -EINVAL; |
| 404 | goto pll_out; |
| 405 | } |
| 406 | twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL, |
| 407 | lppllctl); |
Misael Lopez Cruz | f19b282 | 2011-04-27 02:14:07 -0500 | [diff] [blame] | 408 | } |
Peter Ujfalusi | 2bd05db | 2012-01-14 20:58:44 +0100 | [diff] [blame] | 409 | |
| 410 | /* The PLL in use has not been change, we can exit */ |
| 411 | if (twl6040->pll == pll_id) |
| 412 | break; |
Misael Lopez Cruz | f19b282 | 2011-04-27 02:14:07 -0500 | [diff] [blame] | 413 | |
| 414 | switch (freq_in) { |
| 415 | case 32768: |
| 416 | lppllctl |= TWL6040_LPLLENA; |
| 417 | twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL, |
| 418 | lppllctl); |
| 419 | mdelay(5); |
| 420 | lppllctl &= ~TWL6040_HPLLSEL; |
| 421 | twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL, |
| 422 | lppllctl); |
| 423 | hppllctl &= ~TWL6040_HPLLENA; |
| 424 | twl6040_reg_write(twl6040, TWL6040_REG_HPPLLCTL, |
| 425 | hppllctl); |
| 426 | break; |
| 427 | default: |
Peter Ujfalusi | 2d7c957 | 2011-09-15 15:39:23 +0300 | [diff] [blame] | 428 | dev_err(twl6040->dev, |
Misael Lopez Cruz | f19b282 | 2011-04-27 02:14:07 -0500 | [diff] [blame] | 429 | "freq_in %d not supported\n", freq_in); |
| 430 | ret = -EINVAL; |
| 431 | goto pll_out; |
| 432 | } |
Peter Ujfalusi | 0a58da1 | 2016-06-20 17:07:18 +0300 | [diff] [blame^] | 433 | |
| 434 | clk_disable_unprepare(twl6040->mclk); |
Misael Lopez Cruz | f19b282 | 2011-04-27 02:14:07 -0500 | [diff] [blame] | 435 | break; |
Peter Ujfalusi | cfb7a33 | 2011-07-04 10:28:28 +0300 | [diff] [blame] | 436 | case TWL6040_SYSCLK_SEL_HPPLL: |
Misael Lopez Cruz | f19b282 | 2011-04-27 02:14:07 -0500 | [diff] [blame] | 437 | /* high-performance PLL can provide only 19.2 MHz */ |
| 438 | if (freq_out != 19200000) { |
Peter Ujfalusi | 2d7c957 | 2011-09-15 15:39:23 +0300 | [diff] [blame] | 439 | dev_err(twl6040->dev, |
Misael Lopez Cruz | f19b282 | 2011-04-27 02:14:07 -0500 | [diff] [blame] | 440 | "freq_out %d not supported\n", freq_out); |
| 441 | ret = -EINVAL; |
| 442 | goto pll_out; |
| 443 | } |
| 444 | |
Peter Ujfalusi | 0a58da1 | 2016-06-20 17:07:18 +0300 | [diff] [blame^] | 445 | if (twl6040->mclk_rate != freq_in) { |
Peter Ujfalusi | 2bd05db | 2012-01-14 20:58:44 +0100 | [diff] [blame] | 446 | hppllctl &= ~TWL6040_MCLK_MSK; |
Misael Lopez Cruz | f19b282 | 2011-04-27 02:14:07 -0500 | [diff] [blame] | 447 | |
Peter Ujfalusi | 2bd05db | 2012-01-14 20:58:44 +0100 | [diff] [blame] | 448 | switch (freq_in) { |
| 449 | case 12000000: |
| 450 | /* PLL enabled, active mode */ |
| 451 | hppllctl |= TWL6040_MCLK_12000KHZ | |
| 452 | TWL6040_HPLLENA; |
| 453 | break; |
| 454 | case 19200000: |
Peter Ujfalusi | ac8320c | 2014-05-06 11:46:10 +0300 | [diff] [blame] | 455 | /* PLL enabled, bypass mode */ |
| 456 | hppllctl |= TWL6040_MCLK_19200KHZ | |
| 457 | TWL6040_HPLLBP | TWL6040_HPLLENA; |
Peter Ujfalusi | 2bd05db | 2012-01-14 20:58:44 +0100 | [diff] [blame] | 458 | break; |
| 459 | case 26000000: |
| 460 | /* PLL enabled, active mode */ |
| 461 | hppllctl |= TWL6040_MCLK_26000KHZ | |
| 462 | TWL6040_HPLLENA; |
| 463 | break; |
| 464 | case 38400000: |
Peter Ujfalusi | ac8320c | 2014-05-06 11:46:10 +0300 | [diff] [blame] | 465 | /* PLL enabled, bypass mode */ |
Peter Ujfalusi | 2bd05db | 2012-01-14 20:58:44 +0100 | [diff] [blame] | 466 | hppllctl |= TWL6040_MCLK_38400KHZ | |
Peter Ujfalusi | ac8320c | 2014-05-06 11:46:10 +0300 | [diff] [blame] | 467 | TWL6040_HPLLBP | TWL6040_HPLLENA; |
Peter Ujfalusi | 2bd05db | 2012-01-14 20:58:44 +0100 | [diff] [blame] | 468 | break; |
| 469 | default: |
| 470 | dev_err(twl6040->dev, |
| 471 | "freq_in %d not supported\n", freq_in); |
| 472 | ret = -EINVAL; |
| 473 | goto pll_out; |
| 474 | } |
| 475 | |
Peter Ujfalusi | 0a58da1 | 2016-06-20 17:07:18 +0300 | [diff] [blame^] | 476 | /* When switching to HPPLL, enable the mclk first */ |
| 477 | if (pll_id != twl6040->pll) |
| 478 | clk_prepare_enable(twl6040->mclk); |
Misael Lopez Cruz | f19b282 | 2011-04-27 02:14:07 -0500 | [diff] [blame] | 479 | /* |
Peter Ujfalusi | 2bd05db | 2012-01-14 20:58:44 +0100 | [diff] [blame] | 480 | * enable clock slicer to ensure input waveform is |
| 481 | * square |
Misael Lopez Cruz | f19b282 | 2011-04-27 02:14:07 -0500 | [diff] [blame] | 482 | */ |
Peter Ujfalusi | 2bd05db | 2012-01-14 20:58:44 +0100 | [diff] [blame] | 483 | hppllctl |= TWL6040_HPLLSQRENA; |
| 484 | |
| 485 | twl6040_reg_write(twl6040, TWL6040_REG_HPPLLCTL, |
| 486 | hppllctl); |
| 487 | usleep_range(500, 700); |
| 488 | lppllctl |= TWL6040_HPLLSEL; |
| 489 | twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL, |
| 490 | lppllctl); |
| 491 | lppllctl &= ~TWL6040_LPLLENA; |
| 492 | twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL, |
| 493 | lppllctl); |
Peter Ujfalusi | 0a58da1 | 2016-06-20 17:07:18 +0300 | [diff] [blame^] | 494 | |
| 495 | twl6040->mclk_rate = freq_in; |
Misael Lopez Cruz | f19b282 | 2011-04-27 02:14:07 -0500 | [diff] [blame] | 496 | } |
Misael Lopez Cruz | f19b282 | 2011-04-27 02:14:07 -0500 | [diff] [blame] | 497 | break; |
| 498 | default: |
Peter Ujfalusi | 2d7c957 | 2011-09-15 15:39:23 +0300 | [diff] [blame] | 499 | dev_err(twl6040->dev, "unknown pll id %d\n", pll_id); |
Misael Lopez Cruz | f19b282 | 2011-04-27 02:14:07 -0500 | [diff] [blame] | 500 | ret = -EINVAL; |
| 501 | goto pll_out; |
| 502 | } |
| 503 | |
Peter Ujfalusi | 0a58da1 | 2016-06-20 17:07:18 +0300 | [diff] [blame^] | 504 | twl6040->sysclk_rate = freq_out; |
Peter Ujfalusi | cfb7a33 | 2011-07-04 10:28:28 +0300 | [diff] [blame] | 505 | twl6040->pll = pll_id; |
Misael Lopez Cruz | f19b282 | 2011-04-27 02:14:07 -0500 | [diff] [blame] | 506 | |
| 507 | pll_out: |
| 508 | mutex_unlock(&twl6040->mutex); |
| 509 | return ret; |
| 510 | } |
| 511 | EXPORT_SYMBOL(twl6040_set_pll); |
| 512 | |
Peter Ujfalusi | cfb7a33 | 2011-07-04 10:28:28 +0300 | [diff] [blame] | 513 | int twl6040_get_pll(struct twl6040 *twl6040) |
Misael Lopez Cruz | f19b282 | 2011-04-27 02:14:07 -0500 | [diff] [blame] | 514 | { |
Peter Ujfalusi | cfb7a33 | 2011-07-04 10:28:28 +0300 | [diff] [blame] | 515 | if (twl6040->power_count) |
| 516 | return twl6040->pll; |
| 517 | else |
| 518 | return -ENODEV; |
Misael Lopez Cruz | f19b282 | 2011-04-27 02:14:07 -0500 | [diff] [blame] | 519 | } |
| 520 | EXPORT_SYMBOL(twl6040_get_pll); |
| 521 | |
| 522 | unsigned int twl6040_get_sysclk(struct twl6040 *twl6040) |
| 523 | { |
Peter Ujfalusi | 0a58da1 | 2016-06-20 17:07:18 +0300 | [diff] [blame^] | 524 | return twl6040->sysclk_rate; |
Misael Lopez Cruz | f19b282 | 2011-04-27 02:14:07 -0500 | [diff] [blame] | 525 | } |
| 526 | EXPORT_SYMBOL(twl6040_get_sysclk); |
| 527 | |
Peter Ujfalusi | 70601ec | 2011-10-12 11:57:55 +0300 | [diff] [blame] | 528 | /* Get the combined status of the vibra control register */ |
| 529 | int twl6040_get_vibralr_status(struct twl6040 *twl6040) |
| 530 | { |
Mark Brown | c6f3925 | 2013-08-31 17:48:19 +0100 | [diff] [blame] | 531 | unsigned int reg; |
| 532 | int ret; |
Peter Ujfalusi | 70601ec | 2011-10-12 11:57:55 +0300 | [diff] [blame] | 533 | u8 status; |
| 534 | |
Mark Brown | c6f3925 | 2013-08-31 17:48:19 +0100 | [diff] [blame] | 535 | ret = regmap_read(twl6040->regmap, TWL6040_REG_VIBCTLL, ®); |
| 536 | if (ret != 0) |
| 537 | return ret; |
| 538 | status = reg; |
| 539 | |
| 540 | ret = regmap_read(twl6040->regmap, TWL6040_REG_VIBCTLR, ®); |
| 541 | if (ret != 0) |
| 542 | return ret; |
| 543 | status |= reg; |
| 544 | |
Peter Ujfalusi | 70601ec | 2011-10-12 11:57:55 +0300 | [diff] [blame] | 545 | status &= (TWL6040_VIBENA | TWL6040_VIBSEL); |
| 546 | |
| 547 | return status; |
| 548 | } |
| 549 | EXPORT_SYMBOL(twl6040_get_vibralr_status); |
| 550 | |
Peter Ujfalusi | 0f962ae | 2011-07-05 11:40:33 +0300 | [diff] [blame] | 551 | static struct resource twl6040_vibra_rsrc[] = { |
| 552 | { |
| 553 | .flags = IORESOURCE_IRQ, |
| 554 | }, |
| 555 | }; |
| 556 | |
| 557 | static struct resource twl6040_codec_rsrc[] = { |
| 558 | { |
| 559 | .flags = IORESOURCE_IRQ, |
| 560 | }, |
| 561 | }; |
| 562 | |
Peter Ujfalusi | 8eaeb93 | 2012-04-03 11:56:51 +0300 | [diff] [blame] | 563 | static bool twl6040_readable_reg(struct device *dev, unsigned int reg) |
Misael Lopez Cruz | f19b282 | 2011-04-27 02:14:07 -0500 | [diff] [blame] | 564 | { |
Peter Ujfalusi | 8eaeb93 | 2012-04-03 11:56:51 +0300 | [diff] [blame] | 565 | /* Register 0 is not readable */ |
| 566 | if (!reg) |
| 567 | return false; |
| 568 | return true; |
| 569 | } |
| 570 | |
Mark Brown | c6f3925 | 2013-08-31 17:48:19 +0100 | [diff] [blame] | 571 | static bool twl6040_volatile_reg(struct device *dev, unsigned int reg) |
| 572 | { |
| 573 | switch (reg) { |
Peter Ujfalusi | c7f9129 | 2013-11-29 16:03:45 +0200 | [diff] [blame] | 574 | case TWL6040_REG_ASICID: |
| 575 | case TWL6040_REG_ASICREV: |
| 576 | case TWL6040_REG_INTID: |
| 577 | case TWL6040_REG_LPPLLCTL: |
| 578 | case TWL6040_REG_HPPLLCTL: |
| 579 | case TWL6040_REG_STATUS: |
| 580 | return true; |
| 581 | default: |
| 582 | return false; |
| 583 | } |
| 584 | } |
| 585 | |
| 586 | static bool twl6040_writeable_reg(struct device *dev, unsigned int reg) |
| 587 | { |
| 588 | switch (reg) { |
| 589 | case TWL6040_REG_ASICID: |
| 590 | case TWL6040_REG_ASICREV: |
| 591 | case TWL6040_REG_STATUS: |
Mark Brown | c6f3925 | 2013-08-31 17:48:19 +0100 | [diff] [blame] | 592 | return false; |
| 593 | default: |
| 594 | return true; |
| 595 | } |
| 596 | } |
| 597 | |
Krzysztof Kozlowski | de1e23f | 2015-01-05 10:01:33 +0100 | [diff] [blame] | 598 | static const struct regmap_config twl6040_regmap_config = { |
Peter Ujfalusi | 8eaeb93 | 2012-04-03 11:56:51 +0300 | [diff] [blame] | 599 | .reg_bits = 8, |
| 600 | .val_bits = 8, |
Peter Ujfalusi | c7f9129 | 2013-11-29 16:03:45 +0200 | [diff] [blame] | 601 | |
| 602 | .reg_defaults = twl6040_defaults, |
| 603 | .num_reg_defaults = ARRAY_SIZE(twl6040_defaults), |
| 604 | |
Peter Ujfalusi | 8eaeb93 | 2012-04-03 11:56:51 +0300 | [diff] [blame] | 605 | .max_register = TWL6040_REG_STATUS, /* 0x2e */ |
| 606 | |
| 607 | .readable_reg = twl6040_readable_reg, |
Mark Brown | c6f3925 | 2013-08-31 17:48:19 +0100 | [diff] [blame] | 608 | .volatile_reg = twl6040_volatile_reg, |
Peter Ujfalusi | c7f9129 | 2013-11-29 16:03:45 +0200 | [diff] [blame] | 609 | .writeable_reg = twl6040_writeable_reg, |
Mark Brown | c6f3925 | 2013-08-31 17:48:19 +0100 | [diff] [blame] | 610 | |
| 611 | .cache_type = REGCACHE_RBTREE, |
Peter Ujfalusi | 8eaeb93 | 2012-04-03 11:56:51 +0300 | [diff] [blame] | 612 | }; |
| 613 | |
Peter Ujfalusi | ab7edb1 | 2012-10-11 13:55:32 +0200 | [diff] [blame] | 614 | static const struct regmap_irq twl6040_irqs[] = { |
| 615 | { .reg_offset = 0, .mask = TWL6040_THINT, }, |
| 616 | { .reg_offset = 0, .mask = TWL6040_PLUGINT | TWL6040_UNPLUGINT, }, |
| 617 | { .reg_offset = 0, .mask = TWL6040_HOOKINT, }, |
| 618 | { .reg_offset = 0, .mask = TWL6040_HFINT, }, |
| 619 | { .reg_offset = 0, .mask = TWL6040_VIBINT, }, |
| 620 | { .reg_offset = 0, .mask = TWL6040_READYINT, }, |
| 621 | }; |
| 622 | |
| 623 | static struct regmap_irq_chip twl6040_irq_chip = { |
| 624 | .name = "twl6040", |
| 625 | .irqs = twl6040_irqs, |
| 626 | .num_irqs = ARRAY_SIZE(twl6040_irqs), |
| 627 | |
| 628 | .num_regs = 1, |
| 629 | .status_base = TWL6040_REG_INTID, |
| 630 | .mask_base = TWL6040_REG_INTMR, |
| 631 | }; |
| 632 | |
Greg Kroah-Hartman | 612b95c | 2012-12-21 15:03:15 -0800 | [diff] [blame] | 633 | static int twl6040_probe(struct i2c_client *client, |
| 634 | const struct i2c_device_id *id) |
Peter Ujfalusi | 8eaeb93 | 2012-04-03 11:56:51 +0300 | [diff] [blame] | 635 | { |
Peter Ujfalusi | 37e13ce | 2012-05-16 14:11:58 +0300 | [diff] [blame] | 636 | struct device_node *node = client->dev.of_node; |
Misael Lopez Cruz | f19b282 | 2011-04-27 02:14:07 -0500 | [diff] [blame] | 637 | struct twl6040 *twl6040; |
| 638 | struct mfd_cell *cell = NULL; |
Peter Ujfalusi | 1f01d60 | 2012-05-16 14:11:57 +0300 | [diff] [blame] | 639 | int irq, ret, children = 0; |
Misael Lopez Cruz | f19b282 | 2011-04-27 02:14:07 -0500 | [diff] [blame] | 640 | |
Peter Ujfalusi | df04b62 | 2013-07-12 13:32:02 +0200 | [diff] [blame] | 641 | if (!node) { |
| 642 | dev_err(&client->dev, "of node is missing\n"); |
Misael Lopez Cruz | f19b282 | 2011-04-27 02:14:07 -0500 | [diff] [blame] | 643 | return -EINVAL; |
| 644 | } |
| 645 | |
Peter Ujfalusi | d20e1d2 | 2011-07-04 20:15:19 +0300 | [diff] [blame] | 646 | /* In order to operate correctly we need valid interrupt config */ |
Peter Ujfalusi | 6712419 | 2012-05-16 14:11:56 +0300 | [diff] [blame] | 647 | if (!client->irq) { |
Peter Ujfalusi | 8eaeb93 | 2012-04-03 11:56:51 +0300 | [diff] [blame] | 648 | dev_err(&client->dev, "Invalid IRQ configuration\n"); |
Peter Ujfalusi | d20e1d2 | 2011-07-04 20:15:19 +0300 | [diff] [blame] | 649 | return -EINVAL; |
| 650 | } |
| 651 | |
Peter Ujfalusi | 8eaeb93 | 2012-04-03 11:56:51 +0300 | [diff] [blame] | 652 | twl6040 = devm_kzalloc(&client->dev, sizeof(struct twl6040), |
| 653 | GFP_KERNEL); |
Peter Ujfalusi | ecc8fa1 | 2013-07-12 13:32:04 +0200 | [diff] [blame] | 654 | if (!twl6040) |
| 655 | return -ENOMEM; |
Misael Lopez Cruz | f19b282 | 2011-04-27 02:14:07 -0500 | [diff] [blame] | 656 | |
Axel Lin | bbf6adc | 2012-04-25 10:09:46 +0800 | [diff] [blame] | 657 | twl6040->regmap = devm_regmap_init_i2c(client, &twl6040_regmap_config); |
Peter Ujfalusi | ecc8fa1 | 2013-07-12 13:32:04 +0200 | [diff] [blame] | 658 | if (IS_ERR(twl6040->regmap)) |
| 659 | return PTR_ERR(twl6040->regmap); |
Misael Lopez Cruz | f19b282 | 2011-04-27 02:14:07 -0500 | [diff] [blame] | 660 | |
Peter Ujfalusi | 8eaeb93 | 2012-04-03 11:56:51 +0300 | [diff] [blame] | 661 | i2c_set_clientdata(client, twl6040); |
| 662 | |
Peter Ujfalusi | 68bab86 | 2014-04-03 13:54:41 +0300 | [diff] [blame] | 663 | twl6040->clk32k = devm_clk_get(&client->dev, "clk32k"); |
| 664 | if (IS_ERR(twl6040->clk32k)) { |
Tony Lindgren | 75c08f1 | 2015-09-18 09:29:04 -0700 | [diff] [blame] | 665 | if (PTR_ERR(twl6040->clk32k) == -EPROBE_DEFER) |
| 666 | return -EPROBE_DEFER; |
Peter Ujfalusi | 0a58da1 | 2016-06-20 17:07:18 +0300 | [diff] [blame^] | 667 | dev_dbg(&client->dev, "clk32k is not handled\n"); |
Peter Ujfalusi | 68bab86 | 2014-04-03 13:54:41 +0300 | [diff] [blame] | 668 | twl6040->clk32k = NULL; |
| 669 | } |
| 670 | |
Peter Ujfalusi | 0a58da1 | 2016-06-20 17:07:18 +0300 | [diff] [blame^] | 671 | twl6040->mclk = devm_clk_get(&client->dev, "mclk"); |
| 672 | if (IS_ERR(twl6040->mclk)) { |
| 673 | if (PTR_ERR(twl6040->mclk) == -EPROBE_DEFER) |
| 674 | return -EPROBE_DEFER; |
| 675 | dev_dbg(&client->dev, "mclk is not handled\n"); |
| 676 | twl6040->mclk = NULL; |
| 677 | } |
| 678 | |
Peter Ujfalusi | 5af7df6 | 2012-05-02 16:54:42 +0300 | [diff] [blame] | 679 | twl6040->supplies[0].supply = "vio"; |
| 680 | twl6040->supplies[1].supply = "v2v1"; |
Jingoo Han | 990810b | 2013-02-20 18:30:21 +0900 | [diff] [blame] | 681 | ret = devm_regulator_bulk_get(&client->dev, TWL6040_NUM_SUPPLIES, |
Peter Ujfalusi | 37aefe9 | 2013-07-12 13:32:03 +0200 | [diff] [blame] | 682 | twl6040->supplies); |
Peter Ujfalusi | 5af7df6 | 2012-05-02 16:54:42 +0300 | [diff] [blame] | 683 | if (ret != 0) { |
| 684 | dev_err(&client->dev, "Failed to get supplies: %d\n", ret); |
Wolfram Sang | 501d609 | 2013-10-13 18:06:12 +0200 | [diff] [blame] | 685 | return ret; |
Peter Ujfalusi | 5af7df6 | 2012-05-02 16:54:42 +0300 | [diff] [blame] | 686 | } |
| 687 | |
| 688 | ret = regulator_bulk_enable(TWL6040_NUM_SUPPLIES, twl6040->supplies); |
| 689 | if (ret != 0) { |
| 690 | dev_err(&client->dev, "Failed to enable supplies: %d\n", ret); |
Wolfram Sang | 501d609 | 2013-10-13 18:06:12 +0200 | [diff] [blame] | 691 | return ret; |
Peter Ujfalusi | 5af7df6 | 2012-05-02 16:54:42 +0300 | [diff] [blame] | 692 | } |
| 693 | |
Peter Ujfalusi | 8eaeb93 | 2012-04-03 11:56:51 +0300 | [diff] [blame] | 694 | twl6040->dev = &client->dev; |
| 695 | twl6040->irq = client->irq; |
Misael Lopez Cruz | f19b282 | 2011-04-27 02:14:07 -0500 | [diff] [blame] | 696 | |
| 697 | mutex_init(&twl6040->mutex); |
Misael Lopez Cruz | f19b282 | 2011-04-27 02:14:07 -0500 | [diff] [blame] | 698 | init_completion(&twl6040->ready); |
| 699 | |
Peter Ujfalusi | 006cea3 | 2014-04-01 16:45:00 +0300 | [diff] [blame] | 700 | regmap_register_patch(twl6040->regmap, twl6040_patch, |
| 701 | ARRAY_SIZE(twl6040_patch)); |
| 702 | |
Misael Lopez Cruz | f19b282 | 2011-04-27 02:14:07 -0500 | [diff] [blame] | 703 | twl6040->rev = twl6040_reg_read(twl6040, TWL6040_REG_ASICREV); |
Florian Vaussard | 89d6899 | 2014-02-25 14:28:19 +0100 | [diff] [blame] | 704 | if (twl6040->rev < 0) { |
| 705 | dev_err(&client->dev, "Failed to read revision register: %d\n", |
| 706 | twl6040->rev); |
Julia Lawall | f2b8678 | 2014-08-06 22:12:15 +0200 | [diff] [blame] | 707 | ret = twl6040->rev; |
Florian Vaussard | 89d6899 | 2014-02-25 14:28:19 +0100 | [diff] [blame] | 708 | goto gpio_err; |
| 709 | } |
Misael Lopez Cruz | f19b282 | 2011-04-27 02:14:07 -0500 | [diff] [blame] | 710 | |
Peter Ujfalusi | 77f63e0 | 2011-09-15 15:39:26 +0300 | [diff] [blame] | 711 | /* ERRATA: Automatic power-up is not possible in ES1.0 */ |
Peter Ujfalusi | df04b62 | 2013-07-12 13:32:02 +0200 | [diff] [blame] | 712 | if (twl6040_get_revid(twl6040) > TWL6040_REV_ES1_0) |
| 713 | twl6040->audpwron = of_get_named_gpio(node, |
| 714 | "ti,audpwron-gpio", 0); |
| 715 | else |
Peter Ujfalusi | 77f63e0 | 2011-09-15 15:39:26 +0300 | [diff] [blame] | 716 | twl6040->audpwron = -EINVAL; |
| 717 | |
Misael Lopez Cruz | f19b282 | 2011-04-27 02:14:07 -0500 | [diff] [blame] | 718 | if (gpio_is_valid(twl6040->audpwron)) { |
Jingoo Han | 990810b | 2013-02-20 18:30:21 +0900 | [diff] [blame] | 719 | ret = devm_gpio_request_one(&client->dev, twl6040->audpwron, |
Peter Ujfalusi | 37aefe9 | 2013-07-12 13:32:03 +0200 | [diff] [blame] | 720 | GPIOF_OUT_INIT_LOW, "audpwron"); |
Misael Lopez Cruz | f19b282 | 2011-04-27 02:14:07 -0500 | [diff] [blame] | 721 | if (ret) |
Peter Ujfalusi | 5af7df6 | 2012-05-02 16:54:42 +0300 | [diff] [blame] | 722 | goto gpio_err; |
Peter Ujfalusi | 02d0272 | 2014-04-01 16:45:01 +0300 | [diff] [blame] | 723 | |
| 724 | /* Clear any pending interrupt */ |
| 725 | twl6040_reg_read(twl6040, TWL6040_REG_INTID); |
Misael Lopez Cruz | f19b282 | 2011-04-27 02:14:07 -0500 | [diff] [blame] | 726 | } |
| 727 | |
Peter Ujfalusi | 37aefe9 | 2013-07-12 13:32:03 +0200 | [diff] [blame] | 728 | ret = regmap_add_irq_chip(twl6040->regmap, twl6040->irq, IRQF_ONESHOT, |
Lee Jones | c06f308 | 2014-07-21 13:13:48 +0100 | [diff] [blame] | 729 | 0, &twl6040_irq_chip, &twl6040->irq_data); |
Peter Ujfalusi | ab7edb1 | 2012-10-11 13:55:32 +0200 | [diff] [blame] | 730 | if (ret < 0) |
Jingoo Han | 990810b | 2013-02-20 18:30:21 +0900 | [diff] [blame] | 731 | goto gpio_err; |
Misael Lopez Cruz | f19b282 | 2011-04-27 02:14:07 -0500 | [diff] [blame] | 732 | |
Peter Ujfalusi | ab7edb1 | 2012-10-11 13:55:32 +0200 | [diff] [blame] | 733 | twl6040->irq_ready = regmap_irq_get_virq(twl6040->irq_data, |
Peter Ujfalusi | 37aefe9 | 2013-07-12 13:32:03 +0200 | [diff] [blame] | 734 | TWL6040_IRQ_READY); |
Peter Ujfalusi | ab7edb1 | 2012-10-11 13:55:32 +0200 | [diff] [blame] | 735 | twl6040->irq_th = regmap_irq_get_virq(twl6040->irq_data, |
Peter Ujfalusi | 37aefe9 | 2013-07-12 13:32:03 +0200 | [diff] [blame] | 736 | TWL6040_IRQ_TH); |
Peter Ujfalusi | ab7edb1 | 2012-10-11 13:55:32 +0200 | [diff] [blame] | 737 | |
Jingoo Han | 990810b | 2013-02-20 18:30:21 +0900 | [diff] [blame] | 738 | ret = devm_request_threaded_irq(twl6040->dev, twl6040->irq_ready, NULL, |
Peter Ujfalusi | 37aefe9 | 2013-07-12 13:32:03 +0200 | [diff] [blame] | 739 | twl6040_readyint_handler, IRQF_ONESHOT, |
| 740 | "twl6040_irq_ready", twl6040); |
Peter Ujfalusi | d20e1d2 | 2011-07-04 20:15:19 +0300 | [diff] [blame] | 741 | if (ret) { |
Peter Ujfalusi | 1ac9626 | 2012-10-11 13:55:31 +0200 | [diff] [blame] | 742 | dev_err(twl6040->dev, "READY IRQ request failed: %d\n", ret); |
| 743 | goto readyirq_err; |
| 744 | } |
| 745 | |
Jingoo Han | 990810b | 2013-02-20 18:30:21 +0900 | [diff] [blame] | 746 | ret = devm_request_threaded_irq(twl6040->dev, twl6040->irq_th, NULL, |
Peter Ujfalusi | 37aefe9 | 2013-07-12 13:32:03 +0200 | [diff] [blame] | 747 | twl6040_thint_handler, IRQF_ONESHOT, |
| 748 | "twl6040_irq_th", twl6040); |
Peter Ujfalusi | 1ac9626 | 2012-10-11 13:55:31 +0200 | [diff] [blame] | 749 | if (ret) { |
| 750 | dev_err(twl6040->dev, "Thermal IRQ request failed: %d\n", ret); |
Wei Yongjun | fc5ee96 | 2013-09-25 15:37:15 +0800 | [diff] [blame] | 751 | goto readyirq_err; |
Misael Lopez Cruz | f19b282 | 2011-04-27 02:14:07 -0500 | [diff] [blame] | 752 | } |
| 753 | |
Peter Ujfalusi | 1f01d60 | 2012-05-16 14:11:57 +0300 | [diff] [blame] | 754 | /* |
| 755 | * The main functionality of twl6040 to provide audio on OMAP4+ systems. |
| 756 | * We can add the ASoC codec child whenever this driver has been loaded. |
Peter Ujfalusi | 1f01d60 | 2012-05-16 14:11:57 +0300 | [diff] [blame] | 757 | */ |
Peter Ujfalusi | ab7edb1 | 2012-10-11 13:55:32 +0200 | [diff] [blame] | 758 | irq = regmap_irq_get_virq(twl6040->irq_data, TWL6040_IRQ_PLUG); |
Peter Ujfalusi | 1f01d60 | 2012-05-16 14:11:57 +0300 | [diff] [blame] | 759 | cell = &twl6040->cells[children]; |
| 760 | cell->name = "twl6040-codec"; |
| 761 | twl6040_codec_rsrc[0].start = irq; |
| 762 | twl6040_codec_rsrc[0].end = irq; |
| 763 | cell->resources = twl6040_codec_rsrc; |
| 764 | cell->num_resources = ARRAY_SIZE(twl6040_codec_rsrc); |
Peter Ujfalusi | 1f01d60 | 2012-05-16 14:11:57 +0300 | [diff] [blame] | 765 | children++; |
Misael Lopez Cruz | f19b282 | 2011-04-27 02:14:07 -0500 | [diff] [blame] | 766 | |
Peter Ujfalusi | df04b62 | 2013-07-12 13:32:02 +0200 | [diff] [blame] | 767 | /* Vibra input driver support */ |
| 768 | if (twl6040_has_vibra(node)) { |
Peter Ujfalusi | ab7edb1 | 2012-10-11 13:55:32 +0200 | [diff] [blame] | 769 | irq = regmap_irq_get_virq(twl6040->irq_data, TWL6040_IRQ_VIB); |
Peter Ujfalusi | 0f962ae | 2011-07-05 11:40:33 +0300 | [diff] [blame] | 770 | |
Misael Lopez Cruz | f19b282 | 2011-04-27 02:14:07 -0500 | [diff] [blame] | 771 | cell = &twl6040->cells[children]; |
| 772 | cell->name = "twl6040-vibra"; |
Peter Ujfalusi | 0f962ae | 2011-07-05 11:40:33 +0300 | [diff] [blame] | 773 | twl6040_vibra_rsrc[0].start = irq; |
| 774 | twl6040_vibra_rsrc[0].end = irq; |
| 775 | cell->resources = twl6040_vibra_rsrc; |
| 776 | cell->num_resources = ARRAY_SIZE(twl6040_vibra_rsrc); |
Misael Lopez Cruz | f19b282 | 2011-04-27 02:14:07 -0500 | [diff] [blame] | 777 | children++; |
| 778 | } |
| 779 | |
Peter Ujfalusi | df04b62 | 2013-07-12 13:32:02 +0200 | [diff] [blame] | 780 | /* GPO support */ |
| 781 | cell = &twl6040->cells[children]; |
| 782 | cell->name = "twl6040-gpo"; |
| 783 | children++; |
Peter Ujfalusi | 5cbe786 | 2012-08-16 15:13:14 +0300 | [diff] [blame] | 784 | |
Peter Ujfalusi | c7f9129 | 2013-11-29 16:03:45 +0200 | [diff] [blame] | 785 | /* The chip is powered down so mark regmap to cache only and dirty */ |
| 786 | regcache_cache_only(twl6040->regmap, true); |
| 787 | regcache_mark_dirty(twl6040->regmap); |
| 788 | |
Peter Ujfalusi | 1f01d60 | 2012-05-16 14:11:57 +0300 | [diff] [blame] | 789 | ret = mfd_add_devices(&client->dev, -1, twl6040->cells, children, |
Mark Brown | 55692af | 2012-09-11 15:16:36 +0800 | [diff] [blame] | 790 | NULL, 0, NULL); |
Peter Ujfalusi | 1f01d60 | 2012-05-16 14:11:57 +0300 | [diff] [blame] | 791 | if (ret) |
Wei Yongjun | fc5ee96 | 2013-09-25 15:37:15 +0800 | [diff] [blame] | 792 | goto readyirq_err; |
Misael Lopez Cruz | f19b282 | 2011-04-27 02:14:07 -0500 | [diff] [blame] | 793 | |
| 794 | return 0; |
| 795 | |
Peter Ujfalusi | 1ac9626 | 2012-10-11 13:55:31 +0200 | [diff] [blame] | 796 | readyirq_err: |
Peter Ujfalusi | ab7edb1 | 2012-10-11 13:55:32 +0200 | [diff] [blame] | 797 | regmap_del_irq_chip(twl6040->irq, twl6040->irq_data); |
Peter Ujfalusi | 5af7df6 | 2012-05-02 16:54:42 +0300 | [diff] [blame] | 798 | gpio_err: |
| 799 | regulator_bulk_disable(TWL6040_NUM_SUPPLIES, twl6040->supplies); |
Misael Lopez Cruz | f19b282 | 2011-04-27 02:14:07 -0500 | [diff] [blame] | 800 | return ret; |
| 801 | } |
| 802 | |
Greg Kroah-Hartman | 612b95c | 2012-12-21 15:03:15 -0800 | [diff] [blame] | 803 | static int twl6040_remove(struct i2c_client *client) |
Misael Lopez Cruz | f19b282 | 2011-04-27 02:14:07 -0500 | [diff] [blame] | 804 | { |
Peter Ujfalusi | 8eaeb93 | 2012-04-03 11:56:51 +0300 | [diff] [blame] | 805 | struct twl6040 *twl6040 = i2c_get_clientdata(client); |
Misael Lopez Cruz | f19b282 | 2011-04-27 02:14:07 -0500 | [diff] [blame] | 806 | |
| 807 | if (twl6040->power_count) |
| 808 | twl6040_power(twl6040, 0); |
| 809 | |
Peter Ujfalusi | ab7edb1 | 2012-10-11 13:55:32 +0200 | [diff] [blame] | 810 | regmap_del_irq_chip(twl6040->irq, twl6040->irq_data); |
Misael Lopez Cruz | f19b282 | 2011-04-27 02:14:07 -0500 | [diff] [blame] | 811 | |
Peter Ujfalusi | 8eaeb93 | 2012-04-03 11:56:51 +0300 | [diff] [blame] | 812 | mfd_remove_devices(&client->dev); |
Misael Lopez Cruz | f19b282 | 2011-04-27 02:14:07 -0500 | [diff] [blame] | 813 | |
Peter Ujfalusi | 5af7df6 | 2012-05-02 16:54:42 +0300 | [diff] [blame] | 814 | regulator_bulk_disable(TWL6040_NUM_SUPPLIES, twl6040->supplies); |
Peter Ujfalusi | 5af7df6 | 2012-05-02 16:54:42 +0300 | [diff] [blame] | 815 | |
Misael Lopez Cruz | f19b282 | 2011-04-27 02:14:07 -0500 | [diff] [blame] | 816 | return 0; |
| 817 | } |
| 818 | |
Peter Ujfalusi | 8eaeb93 | 2012-04-03 11:56:51 +0300 | [diff] [blame] | 819 | static const struct i2c_device_id twl6040_i2c_id[] = { |
| 820 | { "twl6040", 0, }, |
Peter Ujfalusi | 1fc74ae | 2012-07-16 11:49:44 +0200 | [diff] [blame] | 821 | { "twl6041", 0, }, |
Peter Ujfalusi | 8eaeb93 | 2012-04-03 11:56:51 +0300 | [diff] [blame] | 822 | { }, |
| 823 | }; |
| 824 | MODULE_DEVICE_TABLE(i2c, twl6040_i2c_id); |
| 825 | |
| 826 | static struct i2c_driver twl6040_driver = { |
| 827 | .driver = { |
| 828 | .name = "twl6040", |
Peter Ujfalusi | 8eaeb93 | 2012-04-03 11:56:51 +0300 | [diff] [blame] | 829 | }, |
Misael Lopez Cruz | f19b282 | 2011-04-27 02:14:07 -0500 | [diff] [blame] | 830 | .probe = twl6040_probe, |
Greg Kroah-Hartman | 612b95c | 2012-12-21 15:03:15 -0800 | [diff] [blame] | 831 | .remove = twl6040_remove, |
Peter Ujfalusi | 8eaeb93 | 2012-04-03 11:56:51 +0300 | [diff] [blame] | 832 | .id_table = twl6040_i2c_id, |
Misael Lopez Cruz | f19b282 | 2011-04-27 02:14:07 -0500 | [diff] [blame] | 833 | }; |
| 834 | |
Peter Ujfalusi | 8eaeb93 | 2012-04-03 11:56:51 +0300 | [diff] [blame] | 835 | module_i2c_driver(twl6040_driver); |
Misael Lopez Cruz | f19b282 | 2011-04-27 02:14:07 -0500 | [diff] [blame] | 836 | |
| 837 | MODULE_DESCRIPTION("TWL6040 MFD"); |
| 838 | MODULE_AUTHOR("Misael Lopez Cruz <misael.lopez@ti.com>"); |
| 839 | MODULE_AUTHOR("Jorge Eduardo Candelaria <jorge.candelaria@ti.com>"); |
| 840 | MODULE_LICENSE("GPL"); |