Mark Brown | 3cc7298 | 2012-06-19 16:31:53 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Arizona core driver |
| 3 | * |
| 4 | * Copyright 2012 Wolfson Microelectronics plc |
| 5 | * |
| 6 | * Author: Mark Brown <broonie@opensource.wolfsonmicro.com> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License version 2 as |
| 10 | * published by the Free Software Foundation. |
| 11 | */ |
| 12 | |
| 13 | #include <linux/delay.h> |
Mark Brown | 59db969 | 2012-07-09 00:31:36 +0200 | [diff] [blame] | 14 | #include <linux/err.h> |
Mark Brown | 3cc7298 | 2012-06-19 16:31:53 +0100 | [diff] [blame] | 15 | #include <linux/gpio.h> |
| 16 | #include <linux/interrupt.h> |
| 17 | #include <linux/mfd/core.h> |
| 18 | #include <linux/module.h> |
Mark Brown | d781009 | 2013-03-25 00:11:27 +0000 | [diff] [blame] | 19 | #include <linux/of.h> |
| 20 | #include <linux/of_device.h> |
| 21 | #include <linux/of_gpio.h> |
Mark Brown | 3cc7298 | 2012-06-19 16:31:53 +0100 | [diff] [blame] | 22 | #include <linux/pm_runtime.h> |
| 23 | #include <linux/regmap.h> |
| 24 | #include <linux/regulator/consumer.h> |
Mark Brown | 5927467 | 2013-04-23 19:44:16 +0100 | [diff] [blame] | 25 | #include <linux/regulator/machine.h> |
Mark Brown | 3cc7298 | 2012-06-19 16:31:53 +0100 | [diff] [blame] | 26 | #include <linux/slab.h> |
| 27 | |
| 28 | #include <linux/mfd/arizona/core.h> |
| 29 | #include <linux/mfd/arizona/registers.h> |
| 30 | |
| 31 | #include "arizona.h" |
| 32 | |
| 33 | static const char *wm5102_core_supplies[] = { |
| 34 | "AVDD", |
| 35 | "DBVDD1", |
Mark Brown | 3cc7298 | 2012-06-19 16:31:53 +0100 | [diff] [blame] | 36 | }; |
| 37 | |
| 38 | int arizona_clk32k_enable(struct arizona *arizona) |
| 39 | { |
| 40 | int ret = 0; |
| 41 | |
| 42 | mutex_lock(&arizona->clk_lock); |
| 43 | |
| 44 | arizona->clk32k_ref++; |
| 45 | |
Mark Brown | 247fa19 | 2013-03-19 14:47:47 +0100 | [diff] [blame] | 46 | if (arizona->clk32k_ref == 1) { |
| 47 | switch (arizona->pdata.clk32k_src) { |
| 48 | case ARIZONA_32KZ_MCLK1: |
| 49 | ret = pm_runtime_get_sync(arizona->dev); |
| 50 | if (ret != 0) |
| 51 | goto out; |
| 52 | break; |
| 53 | } |
| 54 | |
Mark Brown | 3cc7298 | 2012-06-19 16:31:53 +0100 | [diff] [blame] | 55 | ret = regmap_update_bits(arizona->regmap, ARIZONA_CLOCK_32K_1, |
| 56 | ARIZONA_CLK_32K_ENA, |
| 57 | ARIZONA_CLK_32K_ENA); |
Mark Brown | 247fa19 | 2013-03-19 14:47:47 +0100 | [diff] [blame] | 58 | } |
Mark Brown | 3cc7298 | 2012-06-19 16:31:53 +0100 | [diff] [blame] | 59 | |
Mark Brown | 247fa19 | 2013-03-19 14:47:47 +0100 | [diff] [blame] | 60 | out: |
Mark Brown | 3cc7298 | 2012-06-19 16:31:53 +0100 | [diff] [blame] | 61 | if (ret != 0) |
| 62 | arizona->clk32k_ref--; |
| 63 | |
| 64 | mutex_unlock(&arizona->clk_lock); |
| 65 | |
| 66 | return ret; |
| 67 | } |
| 68 | EXPORT_SYMBOL_GPL(arizona_clk32k_enable); |
| 69 | |
| 70 | int arizona_clk32k_disable(struct arizona *arizona) |
| 71 | { |
| 72 | int ret = 0; |
| 73 | |
| 74 | mutex_lock(&arizona->clk_lock); |
| 75 | |
| 76 | BUG_ON(arizona->clk32k_ref <= 0); |
| 77 | |
| 78 | arizona->clk32k_ref--; |
| 79 | |
Mark Brown | 247fa19 | 2013-03-19 14:47:47 +0100 | [diff] [blame] | 80 | if (arizona->clk32k_ref == 0) { |
Mark Brown | 3cc7298 | 2012-06-19 16:31:53 +0100 | [diff] [blame] | 81 | regmap_update_bits(arizona->regmap, ARIZONA_CLOCK_32K_1, |
| 82 | ARIZONA_CLK_32K_ENA, 0); |
| 83 | |
Mark Brown | 247fa19 | 2013-03-19 14:47:47 +0100 | [diff] [blame] | 84 | switch (arizona->pdata.clk32k_src) { |
| 85 | case ARIZONA_32KZ_MCLK1: |
| 86 | pm_runtime_put_sync(arizona->dev); |
| 87 | break; |
| 88 | } |
| 89 | } |
| 90 | |
Mark Brown | 3cc7298 | 2012-06-19 16:31:53 +0100 | [diff] [blame] | 91 | mutex_unlock(&arizona->clk_lock); |
| 92 | |
| 93 | return ret; |
| 94 | } |
| 95 | EXPORT_SYMBOL_GPL(arizona_clk32k_disable); |
| 96 | |
| 97 | static irqreturn_t arizona_clkgen_err(int irq, void *data) |
| 98 | { |
| 99 | struct arizona *arizona = data; |
| 100 | |
| 101 | dev_err(arizona->dev, "CLKGEN error\n"); |
| 102 | |
| 103 | return IRQ_HANDLED; |
| 104 | } |
| 105 | |
| 106 | static irqreturn_t arizona_underclocked(int irq, void *data) |
| 107 | { |
| 108 | struct arizona *arizona = data; |
| 109 | unsigned int val; |
| 110 | int ret; |
| 111 | |
| 112 | ret = regmap_read(arizona->regmap, ARIZONA_INTERRUPT_RAW_STATUS_8, |
| 113 | &val); |
| 114 | if (ret != 0) { |
| 115 | dev_err(arizona->dev, "Failed to read underclock status: %d\n", |
| 116 | ret); |
| 117 | return IRQ_NONE; |
| 118 | } |
| 119 | |
| 120 | if (val & ARIZONA_AIF3_UNDERCLOCKED_STS) |
| 121 | dev_err(arizona->dev, "AIF3 underclocked\n"); |
Mark Brown | 3cc7298 | 2012-06-19 16:31:53 +0100 | [diff] [blame] | 122 | if (val & ARIZONA_AIF2_UNDERCLOCKED_STS) |
Charles Keepax | 3ebef34 | 2012-11-20 13:46:20 +0900 | [diff] [blame] | 123 | dev_err(arizona->dev, "AIF2 underclocked\n"); |
| 124 | if (val & ARIZONA_AIF1_UNDERCLOCKED_STS) |
Mark Brown | 3cc7298 | 2012-06-19 16:31:53 +0100 | [diff] [blame] | 125 | dev_err(arizona->dev, "AIF1 underclocked\n"); |
| 126 | if (val & ARIZONA_ISRC2_UNDERCLOCKED_STS) |
| 127 | dev_err(arizona->dev, "ISRC2 underclocked\n"); |
| 128 | if (val & ARIZONA_ISRC1_UNDERCLOCKED_STS) |
| 129 | dev_err(arizona->dev, "ISRC1 underclocked\n"); |
| 130 | if (val & ARIZONA_FX_UNDERCLOCKED_STS) |
| 131 | dev_err(arizona->dev, "FX underclocked\n"); |
| 132 | if (val & ARIZONA_ASRC_UNDERCLOCKED_STS) |
| 133 | dev_err(arizona->dev, "ASRC underclocked\n"); |
| 134 | if (val & ARIZONA_DAC_UNDERCLOCKED_STS) |
| 135 | dev_err(arizona->dev, "DAC underclocked\n"); |
| 136 | if (val & ARIZONA_ADC_UNDERCLOCKED_STS) |
| 137 | dev_err(arizona->dev, "ADC underclocked\n"); |
| 138 | if (val & ARIZONA_MIXER_UNDERCLOCKED_STS) |
Mark Brown | 648a988 | 2013-01-28 00:32:53 +0800 | [diff] [blame] | 139 | dev_err(arizona->dev, "Mixer dropped sample\n"); |
Mark Brown | 3cc7298 | 2012-06-19 16:31:53 +0100 | [diff] [blame] | 140 | |
| 141 | return IRQ_HANDLED; |
| 142 | } |
| 143 | |
| 144 | static irqreturn_t arizona_overclocked(int irq, void *data) |
| 145 | { |
| 146 | struct arizona *arizona = data; |
| 147 | unsigned int val[2]; |
| 148 | int ret; |
| 149 | |
| 150 | ret = regmap_bulk_read(arizona->regmap, ARIZONA_INTERRUPT_RAW_STATUS_6, |
| 151 | &val[0], 2); |
| 152 | if (ret != 0) { |
| 153 | dev_err(arizona->dev, "Failed to read overclock status: %d\n", |
| 154 | ret); |
| 155 | return IRQ_NONE; |
| 156 | } |
| 157 | |
| 158 | if (val[0] & ARIZONA_PWM_OVERCLOCKED_STS) |
| 159 | dev_err(arizona->dev, "PWM overclocked\n"); |
| 160 | if (val[0] & ARIZONA_FX_CORE_OVERCLOCKED_STS) |
| 161 | dev_err(arizona->dev, "FX core overclocked\n"); |
| 162 | if (val[0] & ARIZONA_DAC_SYS_OVERCLOCKED_STS) |
| 163 | dev_err(arizona->dev, "DAC SYS overclocked\n"); |
| 164 | if (val[0] & ARIZONA_DAC_WARP_OVERCLOCKED_STS) |
| 165 | dev_err(arizona->dev, "DAC WARP overclocked\n"); |
| 166 | if (val[0] & ARIZONA_ADC_OVERCLOCKED_STS) |
| 167 | dev_err(arizona->dev, "ADC overclocked\n"); |
| 168 | if (val[0] & ARIZONA_MIXER_OVERCLOCKED_STS) |
| 169 | dev_err(arizona->dev, "Mixer overclocked\n"); |
| 170 | if (val[0] & ARIZONA_AIF3_SYNC_OVERCLOCKED_STS) |
| 171 | dev_err(arizona->dev, "AIF3 overclocked\n"); |
| 172 | if (val[0] & ARIZONA_AIF2_SYNC_OVERCLOCKED_STS) |
| 173 | dev_err(arizona->dev, "AIF2 overclocked\n"); |
| 174 | if (val[0] & ARIZONA_AIF1_SYNC_OVERCLOCKED_STS) |
| 175 | dev_err(arizona->dev, "AIF1 overclocked\n"); |
| 176 | if (val[0] & ARIZONA_PAD_CTRL_OVERCLOCKED_STS) |
| 177 | dev_err(arizona->dev, "Pad control overclocked\n"); |
| 178 | |
| 179 | if (val[1] & ARIZONA_SLIMBUS_SUBSYS_OVERCLOCKED_STS) |
| 180 | dev_err(arizona->dev, "Slimbus subsystem overclocked\n"); |
| 181 | if (val[1] & ARIZONA_SLIMBUS_ASYNC_OVERCLOCKED_STS) |
| 182 | dev_err(arizona->dev, "Slimbus async overclocked\n"); |
| 183 | if (val[1] & ARIZONA_SLIMBUS_SYNC_OVERCLOCKED_STS) |
| 184 | dev_err(arizona->dev, "Slimbus sync overclocked\n"); |
| 185 | if (val[1] & ARIZONA_ASRC_ASYNC_SYS_OVERCLOCKED_STS) |
| 186 | dev_err(arizona->dev, "ASRC async system overclocked\n"); |
| 187 | if (val[1] & ARIZONA_ASRC_ASYNC_WARP_OVERCLOCKED_STS) |
| 188 | dev_err(arizona->dev, "ASRC async WARP overclocked\n"); |
| 189 | if (val[1] & ARIZONA_ASRC_SYNC_SYS_OVERCLOCKED_STS) |
| 190 | dev_err(arizona->dev, "ASRC sync system overclocked\n"); |
| 191 | if (val[1] & ARIZONA_ASRC_SYNC_WARP_OVERCLOCKED_STS) |
| 192 | dev_err(arizona->dev, "ASRC sync WARP overclocked\n"); |
| 193 | if (val[1] & ARIZONA_ADSP2_1_OVERCLOCKED_STS) |
| 194 | dev_err(arizona->dev, "DSP1 overclocked\n"); |
| 195 | if (val[1] & ARIZONA_ISRC2_OVERCLOCKED_STS) |
| 196 | dev_err(arizona->dev, "ISRC2 overclocked\n"); |
| 197 | if (val[1] & ARIZONA_ISRC1_OVERCLOCKED_STS) |
| 198 | dev_err(arizona->dev, "ISRC1 overclocked\n"); |
| 199 | |
| 200 | return IRQ_HANDLED; |
| 201 | } |
| 202 | |
Charles Keepax | 9d53dfd | 2013-03-26 17:38:45 +0000 | [diff] [blame] | 203 | static int arizona_poll_reg(struct arizona *arizona, |
| 204 | int timeout, unsigned int reg, |
| 205 | unsigned int mask, unsigned int target) |
| 206 | { |
| 207 | unsigned int val = 0; |
| 208 | int ret, i; |
| 209 | |
| 210 | for (i = 0; i < timeout; i++) { |
| 211 | ret = regmap_read(arizona->regmap, reg, &val); |
| 212 | if (ret != 0) { |
| 213 | dev_err(arizona->dev, "Failed to read reg %u: %d\n", |
| 214 | reg, ret); |
| 215 | continue; |
| 216 | } |
| 217 | |
| 218 | if ((val & mask) == target) |
| 219 | return 0; |
| 220 | |
| 221 | msleep(1); |
| 222 | } |
| 223 | |
| 224 | dev_err(arizona->dev, "Polling reg %u timed out: %x\n", reg, val); |
| 225 | return -ETIMEDOUT; |
| 226 | } |
| 227 | |
Mark Brown | 3cc7298 | 2012-06-19 16:31:53 +0100 | [diff] [blame] | 228 | static int arizona_wait_for_boot(struct arizona *arizona) |
| 229 | { |
Charles Keepax | 9d53dfd | 2013-03-26 17:38:45 +0000 | [diff] [blame] | 230 | int ret; |
Mark Brown | 3cc7298 | 2012-06-19 16:31:53 +0100 | [diff] [blame] | 231 | |
| 232 | /* |
| 233 | * We can't use an interrupt as we need to runtime resume to do so, |
| 234 | * we won't race with the interrupt handler as it'll be blocked on |
| 235 | * runtime resume. |
| 236 | */ |
Charles Keepax | 9d53dfd | 2013-03-26 17:38:45 +0000 | [diff] [blame] | 237 | ret = arizona_poll_reg(arizona, 5, ARIZONA_INTERRUPT_RAW_STATUS_5, |
| 238 | ARIZONA_BOOT_DONE_STS, ARIZONA_BOOT_DONE_STS); |
Mark Brown | 3cc7298 | 2012-06-19 16:31:53 +0100 | [diff] [blame] | 239 | |
Charles Keepax | 9d53dfd | 2013-03-26 17:38:45 +0000 | [diff] [blame] | 240 | if (!ret) |
Mark Brown | 3cc7298 | 2012-06-19 16:31:53 +0100 | [diff] [blame] | 241 | regmap_write(arizona->regmap, ARIZONA_INTERRUPT_STATUS_5, |
| 242 | ARIZONA_BOOT_DONE_STS); |
Mark Brown | 3cc7298 | 2012-06-19 16:31:53 +0100 | [diff] [blame] | 243 | |
| 244 | pm_runtime_mark_last_busy(arizona->dev); |
| 245 | |
Charles Keepax | 9d53dfd | 2013-03-26 17:38:45 +0000 | [diff] [blame] | 246 | return ret; |
Mark Brown | 3cc7298 | 2012-06-19 16:31:53 +0100 | [diff] [blame] | 247 | } |
| 248 | |
Charles Keepax | e80436b | 2013-03-26 18:46:15 +0000 | [diff] [blame] | 249 | static int arizona_apply_hardware_patch(struct arizona* arizona) |
| 250 | { |
| 251 | unsigned int fll, sysclk; |
| 252 | int ret, err; |
| 253 | |
Charles Keepax | e80436b | 2013-03-26 18:46:15 +0000 | [diff] [blame] | 254 | /* Cache existing FLL and SYSCLK settings */ |
| 255 | ret = regmap_read(arizona->regmap, ARIZONA_FLL1_CONTROL_1, &fll); |
| 256 | if (ret != 0) { |
| 257 | dev_err(arizona->dev, "Failed to cache FLL settings: %d\n", |
| 258 | ret); |
| 259 | return ret; |
| 260 | } |
| 261 | ret = regmap_read(arizona->regmap, ARIZONA_SYSTEM_CLOCK_1, &sysclk); |
| 262 | if (ret != 0) { |
| 263 | dev_err(arizona->dev, "Failed to cache SYSCLK settings: %d\n", |
| 264 | ret); |
| 265 | return ret; |
| 266 | } |
| 267 | |
| 268 | /* Start up SYSCLK using the FLL in free running mode */ |
| 269 | ret = regmap_write(arizona->regmap, ARIZONA_FLL1_CONTROL_1, |
| 270 | ARIZONA_FLL1_ENA | ARIZONA_FLL1_FREERUN); |
| 271 | if (ret != 0) { |
| 272 | dev_err(arizona->dev, |
| 273 | "Failed to start FLL in freerunning mode: %d\n", |
| 274 | ret); |
| 275 | return ret; |
| 276 | } |
| 277 | ret = arizona_poll_reg(arizona, 25, ARIZONA_INTERRUPT_RAW_STATUS_5, |
| 278 | ARIZONA_FLL1_CLOCK_OK_STS, |
| 279 | ARIZONA_FLL1_CLOCK_OK_STS); |
| 280 | if (ret != 0) { |
| 281 | ret = -ETIMEDOUT; |
| 282 | goto err_fll; |
| 283 | } |
| 284 | |
| 285 | ret = regmap_write(arizona->regmap, ARIZONA_SYSTEM_CLOCK_1, 0x0144); |
| 286 | if (ret != 0) { |
| 287 | dev_err(arizona->dev, "Failed to start SYSCLK: %d\n", ret); |
| 288 | goto err_fll; |
| 289 | } |
| 290 | |
| 291 | /* Start the write sequencer and wait for it to finish */ |
| 292 | ret = regmap_write(arizona->regmap, ARIZONA_WRITE_SEQUENCER_CTRL_0, |
| 293 | ARIZONA_WSEQ_ENA | ARIZONA_WSEQ_START | 160); |
| 294 | if (ret != 0) { |
| 295 | dev_err(arizona->dev, "Failed to start write sequencer: %d\n", |
| 296 | ret); |
| 297 | goto err_sysclk; |
| 298 | } |
| 299 | ret = arizona_poll_reg(arizona, 5, ARIZONA_WRITE_SEQUENCER_CTRL_1, |
| 300 | ARIZONA_WSEQ_BUSY, 0); |
| 301 | if (ret != 0) { |
| 302 | regmap_write(arizona->regmap, ARIZONA_WRITE_SEQUENCER_CTRL_0, |
| 303 | ARIZONA_WSEQ_ABORT); |
| 304 | ret = -ETIMEDOUT; |
| 305 | } |
| 306 | |
| 307 | err_sysclk: |
| 308 | err = regmap_write(arizona->regmap, ARIZONA_SYSTEM_CLOCK_1, sysclk); |
| 309 | if (err != 0) { |
| 310 | dev_err(arizona->dev, |
| 311 | "Failed to re-apply old SYSCLK settings: %d\n", |
| 312 | err); |
| 313 | } |
| 314 | |
| 315 | err_fll: |
| 316 | err = regmap_write(arizona->regmap, ARIZONA_FLL1_CONTROL_1, fll); |
| 317 | if (err != 0) { |
| 318 | dev_err(arizona->dev, |
| 319 | "Failed to re-apply old FLL settings: %d\n", |
| 320 | err); |
| 321 | } |
| 322 | |
Charles Keepax | e80436b | 2013-03-26 18:46:15 +0000 | [diff] [blame] | 323 | if (ret != 0) |
| 324 | return ret; |
| 325 | else |
| 326 | return err; |
| 327 | } |
| 328 | |
Mark Brown | 3cc7298 | 2012-06-19 16:31:53 +0100 | [diff] [blame] | 329 | #ifdef CONFIG_PM_RUNTIME |
| 330 | static int arizona_runtime_resume(struct device *dev) |
| 331 | { |
| 332 | struct arizona *arizona = dev_get_drvdata(dev); |
| 333 | int ret; |
| 334 | |
Mark Brown | 508c829 | 2012-07-20 17:09:12 +0100 | [diff] [blame] | 335 | dev_dbg(arizona->dev, "Leaving AoD mode\n"); |
| 336 | |
Mark Brown | 59db969 | 2012-07-09 00:31:36 +0200 | [diff] [blame] | 337 | ret = regulator_enable(arizona->dcvdd); |
| 338 | if (ret != 0) { |
| 339 | dev_err(arizona->dev, "Failed to enable DCVDD: %d\n", ret); |
| 340 | return ret; |
| 341 | } |
Mark Brown | 3cc7298 | 2012-06-19 16:31:53 +0100 | [diff] [blame] | 342 | |
| 343 | regcache_cache_only(arizona->regmap, false); |
| 344 | |
Charles Keepax | 4c9bb8b | 2013-03-26 18:01:49 +0000 | [diff] [blame] | 345 | switch (arizona->type) { |
| 346 | case WM5102: |
Mark Brown | 5927467 | 2013-04-23 19:44:16 +0100 | [diff] [blame] | 347 | if (arizona->external_dcvdd) { |
| 348 | ret = regmap_update_bits(arizona->regmap, |
| 349 | ARIZONA_ISOLATION_CONTROL, |
| 350 | ARIZONA_ISOLATE_DCVDD1, 0); |
| 351 | if (ret != 0) { |
| 352 | dev_err(arizona->dev, |
| 353 | "Failed to connect DCVDD: %d\n", ret); |
| 354 | goto err; |
| 355 | } |
| 356 | } |
| 357 | |
Charles Keepax | 4c9bb8b | 2013-03-26 18:01:49 +0000 | [diff] [blame] | 358 | ret = wm5102_patch(arizona); |
| 359 | if (ret != 0) { |
| 360 | dev_err(arizona->dev, "Failed to apply patch: %d\n", |
| 361 | ret); |
| 362 | goto err; |
| 363 | } |
Charles Keepax | e80436b | 2013-03-26 18:46:15 +0000 | [diff] [blame] | 364 | |
| 365 | ret = arizona_apply_hardware_patch(arizona); |
| 366 | if (ret != 0) { |
| 367 | dev_err(arizona->dev, |
| 368 | "Failed to apply hardware patch: %d\n", |
| 369 | ret); |
| 370 | goto err; |
| 371 | } |
| 372 | break; |
| 373 | default: |
Charles Keepax | 12bb68e | 2013-03-27 09:49:40 +0000 | [diff] [blame] | 374 | ret = arizona_wait_for_boot(arizona); |
| 375 | if (ret != 0) { |
| 376 | goto err; |
| 377 | } |
| 378 | |
Mark Brown | 5927467 | 2013-04-23 19:44:16 +0100 | [diff] [blame] | 379 | if (arizona->external_dcvdd) { |
| 380 | ret = regmap_update_bits(arizona->regmap, |
| 381 | ARIZONA_ISOLATION_CONTROL, |
| 382 | ARIZONA_ISOLATE_DCVDD1, 0); |
| 383 | if (ret != 0) { |
| 384 | dev_err(arizona->dev, |
| 385 | "Failed to connect DCVDD: %d\n", ret); |
| 386 | goto err; |
| 387 | } |
| 388 | } |
Charles Keepax | e80436b | 2013-03-26 18:46:15 +0000 | [diff] [blame] | 389 | break; |
Charles Keepax | 4c9bb8b | 2013-03-26 18:01:49 +0000 | [diff] [blame] | 390 | } |
| 391 | |
Charles Keepax | d9d0349 | 2013-03-26 18:01:49 +0000 | [diff] [blame] | 392 | switch (arizona->type) { |
| 393 | case WM5102: |
| 394 | ret = wm5102_patch(arizona); |
| 395 | if (ret != 0) { |
| 396 | dev_err(arizona->dev, "Failed to apply patch: %d\n", |
| 397 | ret); |
| 398 | goto err; |
| 399 | } |
| 400 | default: |
| 401 | break; |
| 402 | } |
| 403 | |
Mark Brown | 9270bdf | 2013-01-04 17:16:12 +0000 | [diff] [blame] | 404 | ret = regcache_sync(arizona->regmap); |
| 405 | if (ret != 0) { |
| 406 | dev_err(arizona->dev, "Failed to restore register cache\n"); |
Mark Brown | 4816bd1 | 2013-01-14 15:50:38 +0900 | [diff] [blame] | 407 | goto err; |
Mark Brown | 9270bdf | 2013-01-04 17:16:12 +0000 | [diff] [blame] | 408 | } |
Mark Brown | 3cc7298 | 2012-06-19 16:31:53 +0100 | [diff] [blame] | 409 | |
| 410 | return 0; |
Mark Brown | 4816bd1 | 2013-01-14 15:50:38 +0900 | [diff] [blame] | 411 | |
| 412 | err: |
| 413 | regcache_cache_only(arizona->regmap, true); |
| 414 | regulator_disable(arizona->dcvdd); |
| 415 | return ret; |
Mark Brown | 3cc7298 | 2012-06-19 16:31:53 +0100 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | static int arizona_runtime_suspend(struct device *dev) |
| 419 | { |
| 420 | struct arizona *arizona = dev_get_drvdata(dev); |
Mark Brown | 5927467 | 2013-04-23 19:44:16 +0100 | [diff] [blame] | 421 | int ret; |
Mark Brown | 3cc7298 | 2012-06-19 16:31:53 +0100 | [diff] [blame] | 422 | |
Mark Brown | 508c829 | 2012-07-20 17:09:12 +0100 | [diff] [blame] | 423 | dev_dbg(arizona->dev, "Entering AoD mode\n"); |
| 424 | |
Mark Brown | 5927467 | 2013-04-23 19:44:16 +0100 | [diff] [blame] | 425 | if (arizona->external_dcvdd) { |
| 426 | ret = regmap_update_bits(arizona->regmap, |
| 427 | ARIZONA_ISOLATION_CONTROL, |
| 428 | ARIZONA_ISOLATE_DCVDD1, |
| 429 | ARIZONA_ISOLATE_DCVDD1); |
| 430 | if (ret != 0) { |
| 431 | dev_err(arizona->dev, "Failed to isolate DCVDD: %d\n", |
| 432 | ret); |
| 433 | return ret; |
| 434 | } |
| 435 | } |
| 436 | |
Mark Brown | 59db969 | 2012-07-09 00:31:36 +0200 | [diff] [blame] | 437 | regcache_cache_only(arizona->regmap, true); |
| 438 | regcache_mark_dirty(arizona->regmap); |
Charles Keepax | e293e8472 | 2013-08-06 17:18:35 +0100 | [diff] [blame] | 439 | regulator_disable(arizona->dcvdd); |
Mark Brown | 3cc7298 | 2012-06-19 16:31:53 +0100 | [diff] [blame] | 440 | |
| 441 | return 0; |
| 442 | } |
| 443 | #endif |
| 444 | |
Mark Brown | dc781d0 | 2013-01-27 12:07:32 +0800 | [diff] [blame] | 445 | #ifdef CONFIG_PM_SLEEP |
Mark Brown | 67c9929 | 2013-04-10 12:40:26 +0100 | [diff] [blame] | 446 | static int arizona_suspend(struct device *dev) |
| 447 | { |
| 448 | struct arizona *arizona = dev_get_drvdata(dev); |
| 449 | |
| 450 | dev_dbg(arizona->dev, "Suspend, disabling IRQ\n"); |
| 451 | disable_irq(arizona->irq); |
| 452 | |
| 453 | return 0; |
| 454 | } |
| 455 | |
| 456 | static int arizona_suspend_late(struct device *dev) |
| 457 | { |
| 458 | struct arizona *arizona = dev_get_drvdata(dev); |
| 459 | |
| 460 | dev_dbg(arizona->dev, "Late suspend, reenabling IRQ\n"); |
| 461 | enable_irq(arizona->irq); |
| 462 | |
| 463 | return 0; |
| 464 | } |
| 465 | |
Mark Brown | dc781d0 | 2013-01-27 12:07:32 +0800 | [diff] [blame] | 466 | static int arizona_resume_noirq(struct device *dev) |
| 467 | { |
| 468 | struct arizona *arizona = dev_get_drvdata(dev); |
| 469 | |
| 470 | dev_dbg(arizona->dev, "Early resume, disabling IRQ\n"); |
| 471 | disable_irq(arizona->irq); |
| 472 | |
| 473 | return 0; |
| 474 | } |
| 475 | |
| 476 | static int arizona_resume(struct device *dev) |
| 477 | { |
| 478 | struct arizona *arizona = dev_get_drvdata(dev); |
| 479 | |
| 480 | dev_dbg(arizona->dev, "Late resume, reenabling IRQ\n"); |
| 481 | enable_irq(arizona->irq); |
| 482 | |
| 483 | return 0; |
| 484 | } |
| 485 | #endif |
| 486 | |
Mark Brown | 3cc7298 | 2012-06-19 16:31:53 +0100 | [diff] [blame] | 487 | const struct dev_pm_ops arizona_pm_ops = { |
| 488 | SET_RUNTIME_PM_OPS(arizona_runtime_suspend, |
| 489 | arizona_runtime_resume, |
| 490 | NULL) |
Mark Brown | 67c9929 | 2013-04-10 12:40:26 +0100 | [diff] [blame] | 491 | SET_SYSTEM_SLEEP_PM_OPS(arizona_suspend, arizona_resume) |
Mark Brown | dc781d0 | 2013-01-27 12:07:32 +0800 | [diff] [blame] | 492 | #ifdef CONFIG_PM_SLEEP |
Mark Brown | 67c9929 | 2013-04-10 12:40:26 +0100 | [diff] [blame] | 493 | .suspend_late = arizona_suspend_late, |
Mark Brown | dc781d0 | 2013-01-27 12:07:32 +0800 | [diff] [blame] | 494 | .resume_noirq = arizona_resume_noirq, |
| 495 | #endif |
Mark Brown | 3cc7298 | 2012-06-19 16:31:53 +0100 | [diff] [blame] | 496 | }; |
| 497 | EXPORT_SYMBOL_GPL(arizona_pm_ops); |
| 498 | |
Mark Brown | d781009 | 2013-03-25 00:11:27 +0000 | [diff] [blame] | 499 | #ifdef CONFIG_OF |
Lee Jones | 942786e | 2014-07-02 14:28:46 +0100 | [diff] [blame^] | 500 | unsigned long arizona_of_get_type(struct device *dev) |
Mark Brown | d781009 | 2013-03-25 00:11:27 +0000 | [diff] [blame] | 501 | { |
| 502 | const struct of_device_id *id = of_match_device(arizona_of_match, dev); |
| 503 | |
| 504 | if (id) |
Lee Jones | 942786e | 2014-07-02 14:28:46 +0100 | [diff] [blame^] | 505 | return (unsigned long)id->data; |
Mark Brown | d781009 | 2013-03-25 00:11:27 +0000 | [diff] [blame] | 506 | else |
| 507 | return 0; |
| 508 | } |
| 509 | EXPORT_SYMBOL_GPL(arizona_of_get_type); |
| 510 | |
Charles Keepax | e4fcb1d | 2014-04-16 10:01:37 +0100 | [diff] [blame] | 511 | int arizona_of_get_named_gpio(struct arizona *arizona, const char *prop, |
| 512 | bool mandatory) |
| 513 | { |
| 514 | int gpio; |
| 515 | |
| 516 | gpio = of_get_named_gpio(arizona->dev->of_node, prop, 0); |
| 517 | if (gpio < 0) { |
| 518 | if (mandatory) |
| 519 | dev_err(arizona->dev, |
| 520 | "Mandatory DT gpio %s missing/malformed: %d\n", |
| 521 | prop, gpio); |
| 522 | |
| 523 | gpio = 0; |
| 524 | } |
| 525 | |
| 526 | return gpio; |
| 527 | } |
| 528 | EXPORT_SYMBOL_GPL(arizona_of_get_named_gpio); |
| 529 | |
Mark Brown | d781009 | 2013-03-25 00:11:27 +0000 | [diff] [blame] | 530 | static int arizona_of_get_core_pdata(struct arizona *arizona) |
| 531 | { |
Charles Keepax | e4fcb1d | 2014-04-16 10:01:37 +0100 | [diff] [blame] | 532 | struct arizona_pdata *pdata = &arizona->pdata; |
Mark Brown | d781009 | 2013-03-25 00:11:27 +0000 | [diff] [blame] | 533 | int ret, i; |
| 534 | |
Charles Keepax | e4fcb1d | 2014-04-16 10:01:37 +0100 | [diff] [blame] | 535 | pdata->reset = arizona_of_get_named_gpio(arizona, "wlf,reset", true); |
Mark Brown | d781009 | 2013-03-25 00:11:27 +0000 | [diff] [blame] | 536 | |
| 537 | ret = of_property_read_u32_array(arizona->dev->of_node, |
| 538 | "wlf,gpio-defaults", |
| 539 | arizona->pdata.gpio_defaults, |
| 540 | ARRAY_SIZE(arizona->pdata.gpio_defaults)); |
| 541 | if (ret >= 0) { |
| 542 | /* |
| 543 | * All values are literal except out of range values |
| 544 | * which are chip default, translate into platform |
| 545 | * data which uses 0 as chip default and out of range |
| 546 | * as zero. |
| 547 | */ |
| 548 | for (i = 0; i < ARRAY_SIZE(arizona->pdata.gpio_defaults); i++) { |
| 549 | if (arizona->pdata.gpio_defaults[i] > 0xffff) |
| 550 | arizona->pdata.gpio_defaults[i] = 0; |
Charles Keepax | 91c7393 | 2013-10-03 16:16:01 +0100 | [diff] [blame] | 551 | else if (arizona->pdata.gpio_defaults[i] == 0) |
Mark Brown | d781009 | 2013-03-25 00:11:27 +0000 | [diff] [blame] | 552 | arizona->pdata.gpio_defaults[i] = 0x10000; |
| 553 | } |
| 554 | } else { |
| 555 | dev_err(arizona->dev, "Failed to parse GPIO defaults: %d\n", |
| 556 | ret); |
| 557 | } |
| 558 | |
| 559 | return 0; |
| 560 | } |
| 561 | |
| 562 | const struct of_device_id arizona_of_match[] = { |
| 563 | { .compatible = "wlf,wm5102", .data = (void *)WM5102 }, |
| 564 | { .compatible = "wlf,wm5110", .data = (void *)WM5110 }, |
Charles Keepax | dc7d486 | 2013-06-13 09:43:29 +0100 | [diff] [blame] | 565 | { .compatible = "wlf,wm8997", .data = (void *)WM8997 }, |
Mark Brown | d781009 | 2013-03-25 00:11:27 +0000 | [diff] [blame] | 566 | {}, |
| 567 | }; |
| 568 | EXPORT_SYMBOL_GPL(arizona_of_match); |
| 569 | #else |
| 570 | static inline int arizona_of_get_core_pdata(struct arizona *arizona) |
| 571 | { |
| 572 | return 0; |
| 573 | } |
| 574 | #endif |
| 575 | |
Geert Uytterhoeven | 5ac9855 | 2013-11-18 14:33:06 +0100 | [diff] [blame] | 576 | static const struct mfd_cell early_devs[] = { |
Mark Brown | 3cc7298 | 2012-06-19 16:31:53 +0100 | [diff] [blame] | 577 | { .name = "arizona-ldo1" }, |
| 578 | }; |
| 579 | |
Charles Keepax | 32dadef | 2013-10-15 20:14:22 +0100 | [diff] [blame] | 580 | static const char *wm5102_supplies[] = { |
| 581 | "DBVDD2", |
| 582 | "DBVDD3", |
| 583 | "CPVDD", |
| 584 | "SPKVDDL", |
| 585 | "SPKVDDR", |
Charles Keepax | 058c890 | 2014-05-19 17:35:27 +0100 | [diff] [blame] | 586 | "MICVDD", |
Charles Keepax | 32dadef | 2013-10-15 20:14:22 +0100 | [diff] [blame] | 587 | }; |
| 588 | |
Geert Uytterhoeven | 5ac9855 | 2013-11-18 14:33:06 +0100 | [diff] [blame] | 589 | static const struct mfd_cell wm5102_devs[] = { |
Mark Brown | d776811 | 2012-12-20 15:38:03 +0000 | [diff] [blame] | 590 | { .name = "arizona-micsupp" }, |
Mark Brown | 3cc7298 | 2012-06-19 16:31:53 +0100 | [diff] [blame] | 591 | { .name = "arizona-extcon" }, |
| 592 | { .name = "arizona-gpio" }, |
Mark Brown | 503b1ca | 2012-11-27 17:36:38 +0000 | [diff] [blame] | 593 | { .name = "arizona-haptics" }, |
Mark Brown | 3cc7298 | 2012-06-19 16:31:53 +0100 | [diff] [blame] | 594 | { .name = "arizona-pwm" }, |
Charles Keepax | 32dadef | 2013-10-15 20:14:22 +0100 | [diff] [blame] | 595 | { |
| 596 | .name = "wm5102-codec", |
| 597 | .parent_supplies = wm5102_supplies, |
| 598 | .num_parent_supplies = ARRAY_SIZE(wm5102_supplies), |
| 599 | }, |
Mark Brown | 3cc7298 | 2012-06-19 16:31:53 +0100 | [diff] [blame] | 600 | }; |
| 601 | |
Geert Uytterhoeven | 5ac9855 | 2013-11-18 14:33:06 +0100 | [diff] [blame] | 602 | static const struct mfd_cell wm5110_devs[] = { |
Mark Brown | d776811 | 2012-12-20 15:38:03 +0000 | [diff] [blame] | 603 | { .name = "arizona-micsupp" }, |
Mark Brown | e102bef | 2012-07-10 12:37:58 +0100 | [diff] [blame] | 604 | { .name = "arizona-extcon" }, |
| 605 | { .name = "arizona-gpio" }, |
Mark Brown | 503b1ca | 2012-11-27 17:36:38 +0000 | [diff] [blame] | 606 | { .name = "arizona-haptics" }, |
Mark Brown | e102bef | 2012-07-10 12:37:58 +0100 | [diff] [blame] | 607 | { .name = "arizona-pwm" }, |
Charles Keepax | 32dadef | 2013-10-15 20:14:22 +0100 | [diff] [blame] | 608 | { |
| 609 | .name = "wm5110-codec", |
| 610 | .parent_supplies = wm5102_supplies, |
| 611 | .num_parent_supplies = ARRAY_SIZE(wm5102_supplies), |
| 612 | }, |
| 613 | }; |
| 614 | |
| 615 | static const char *wm8997_supplies[] = { |
| 616 | "DBVDD2", |
| 617 | "CPVDD", |
| 618 | "SPKVDD", |
Mark Brown | e102bef | 2012-07-10 12:37:58 +0100 | [diff] [blame] | 619 | }; |
| 620 | |
Geert Uytterhoeven | 5ac9855 | 2013-11-18 14:33:06 +0100 | [diff] [blame] | 621 | static const struct mfd_cell wm8997_devs[] = { |
Charles Keepax | dc7d486 | 2013-06-13 09:43:29 +0100 | [diff] [blame] | 622 | { .name = "arizona-micsupp" }, |
| 623 | { .name = "arizona-extcon" }, |
| 624 | { .name = "arizona-gpio" }, |
| 625 | { .name = "arizona-haptics" }, |
| 626 | { .name = "arizona-pwm" }, |
Charles Keepax | 32dadef | 2013-10-15 20:14:22 +0100 | [diff] [blame] | 627 | { |
| 628 | .name = "wm8997-codec", |
| 629 | .parent_supplies = wm8997_supplies, |
| 630 | .num_parent_supplies = ARRAY_SIZE(wm8997_supplies), |
| 631 | }, |
Charles Keepax | dc7d486 | 2013-06-13 09:43:29 +0100 | [diff] [blame] | 632 | }; |
| 633 | |
Bill Pemberton | f791be4 | 2012-11-19 13:23:04 -0500 | [diff] [blame] | 634 | int arizona_dev_init(struct arizona *arizona) |
Mark Brown | 3cc7298 | 2012-06-19 16:31:53 +0100 | [diff] [blame] | 635 | { |
| 636 | struct device *dev = arizona->dev; |
| 637 | const char *type_name; |
| 638 | unsigned int reg, val; |
Mark Brown | 62d62b5 | 2012-12-02 11:41:46 +0900 | [diff] [blame] | 639 | int (*apply_patch)(struct arizona *) = NULL; |
Mark Brown | 3cc7298 | 2012-06-19 16:31:53 +0100 | [diff] [blame] | 640 | int ret, i; |
| 641 | |
| 642 | dev_set_drvdata(arizona->dev, arizona); |
| 643 | mutex_init(&arizona->clk_lock); |
| 644 | |
| 645 | if (dev_get_platdata(arizona->dev)) |
| 646 | memcpy(&arizona->pdata, dev_get_platdata(arizona->dev), |
| 647 | sizeof(arizona->pdata)); |
Lee Jones | 22d7dc8 | 2013-09-27 14:25:55 +0100 | [diff] [blame] | 648 | else |
| 649 | arizona_of_get_core_pdata(arizona); |
Mark Brown | 3cc7298 | 2012-06-19 16:31:53 +0100 | [diff] [blame] | 650 | |
| 651 | regcache_cache_only(arizona->regmap, true); |
| 652 | |
| 653 | switch (arizona->type) { |
| 654 | case WM5102: |
Mark Brown | e102bef | 2012-07-10 12:37:58 +0100 | [diff] [blame] | 655 | case WM5110: |
Charles Keepax | dc7d486 | 2013-06-13 09:43:29 +0100 | [diff] [blame] | 656 | case WM8997: |
Mark Brown | 3cc7298 | 2012-06-19 16:31:53 +0100 | [diff] [blame] | 657 | for (i = 0; i < ARRAY_SIZE(wm5102_core_supplies); i++) |
| 658 | arizona->core_supplies[i].supply |
| 659 | = wm5102_core_supplies[i]; |
| 660 | arizona->num_core_supplies = ARRAY_SIZE(wm5102_core_supplies); |
| 661 | break; |
| 662 | default: |
| 663 | dev_err(arizona->dev, "Unknown device type %d\n", |
| 664 | arizona->type); |
| 665 | return -EINVAL; |
| 666 | } |
| 667 | |
Charles Keepax | 4a8c475 | 2014-04-16 10:01:38 +0100 | [diff] [blame] | 668 | /* Mark DCVDD as external, LDO1 driver will clear if internal */ |
| 669 | arizona->external_dcvdd = true; |
| 670 | |
Mark Brown | 3cc7298 | 2012-06-19 16:31:53 +0100 | [diff] [blame] | 671 | ret = mfd_add_devices(arizona->dev, -1, early_devs, |
Mark Brown | 0848c94 | 2012-09-11 15:16:36 +0800 | [diff] [blame] | 672 | ARRAY_SIZE(early_devs), NULL, 0, NULL); |
Mark Brown | 3cc7298 | 2012-06-19 16:31:53 +0100 | [diff] [blame] | 673 | if (ret != 0) { |
| 674 | dev_err(dev, "Failed to add early children: %d\n", ret); |
| 675 | return ret; |
| 676 | } |
| 677 | |
| 678 | ret = devm_regulator_bulk_get(dev, arizona->num_core_supplies, |
| 679 | arizona->core_supplies); |
| 680 | if (ret != 0) { |
| 681 | dev_err(dev, "Failed to request core supplies: %d\n", |
| 682 | ret); |
| 683 | goto err_early; |
| 684 | } |
| 685 | |
Charles Keepax | 0c2d0ff | 2014-06-19 16:04:23 +0100 | [diff] [blame] | 686 | /** |
| 687 | * Don't use devres here because the only device we have to get |
| 688 | * against is the MFD device and DCVDD will likely be supplied by |
| 689 | * one of its children. Meaning that the regulator will be |
| 690 | * destroyed by the time devres calls regulator put. |
| 691 | */ |
Charles Keepax | e602151 | 2014-06-02 09:50:41 +0100 | [diff] [blame] | 692 | arizona->dcvdd = regulator_get(arizona->dev, "DCVDD"); |
Mark Brown | 59db969 | 2012-07-09 00:31:36 +0200 | [diff] [blame] | 693 | if (IS_ERR(arizona->dcvdd)) { |
| 694 | ret = PTR_ERR(arizona->dcvdd); |
| 695 | dev_err(dev, "Failed to request DCVDD: %d\n", ret); |
| 696 | goto err_early; |
| 697 | } |
| 698 | |
Mark Brown | 87d3af4 | 2013-03-26 12:15:26 +0000 | [diff] [blame] | 699 | if (arizona->pdata.reset) { |
| 700 | /* Start out with /RESET low to put the chip into reset */ |
| 701 | ret = gpio_request_one(arizona->pdata.reset, |
| 702 | GPIOF_DIR_OUT | GPIOF_INIT_LOW, |
| 703 | "arizona /RESET"); |
| 704 | if (ret != 0) { |
| 705 | dev_err(dev, "Failed to request /RESET: %d\n", ret); |
Charles Keepax | e602151 | 2014-06-02 09:50:41 +0100 | [diff] [blame] | 706 | goto err_dcvdd; |
Mark Brown | 87d3af4 | 2013-03-26 12:15:26 +0000 | [diff] [blame] | 707 | } |
| 708 | } |
| 709 | |
Mark Brown | 3cc7298 | 2012-06-19 16:31:53 +0100 | [diff] [blame] | 710 | ret = regulator_bulk_enable(arizona->num_core_supplies, |
| 711 | arizona->core_supplies); |
| 712 | if (ret != 0) { |
| 713 | dev_err(dev, "Failed to enable core supplies: %d\n", |
| 714 | ret); |
Charles Keepax | e602151 | 2014-06-02 09:50:41 +0100 | [diff] [blame] | 715 | goto err_dcvdd; |
Mark Brown | 3cc7298 | 2012-06-19 16:31:53 +0100 | [diff] [blame] | 716 | } |
| 717 | |
Mark Brown | 59db969 | 2012-07-09 00:31:36 +0200 | [diff] [blame] | 718 | ret = regulator_enable(arizona->dcvdd); |
| 719 | if (ret != 0) { |
| 720 | dev_err(dev, "Failed to enable DCVDD: %d\n", ret); |
| 721 | goto err_enable; |
| 722 | } |
| 723 | |
Charles Keepax | c25feaa | 2013-04-03 12:53:37 +0100 | [diff] [blame] | 724 | if (arizona->pdata.reset) { |
Mark Brown | 3cc7298 | 2012-06-19 16:31:53 +0100 | [diff] [blame] | 725 | gpio_set_value_cansleep(arizona->pdata.reset, 1); |
Charles Keepax | c25feaa | 2013-04-03 12:53:37 +0100 | [diff] [blame] | 726 | msleep(1); |
| 727 | } |
Mark Brown | 3cc7298 | 2012-06-19 16:31:53 +0100 | [diff] [blame] | 728 | |
Mark Brown | 3cc7298 | 2012-06-19 16:31:53 +0100 | [diff] [blame] | 729 | regcache_cache_only(arizona->regmap, false); |
| 730 | |
Mark Brown | ca76ceb | 2013-04-09 16:04:35 +0100 | [diff] [blame] | 731 | /* Verify that this is a chip we know about */ |
| 732 | ret = regmap_read(arizona->regmap, ARIZONA_SOFTWARE_RESET, ®); |
| 733 | if (ret != 0) { |
| 734 | dev_err(dev, "Failed to read ID register: %d\n", ret); |
| 735 | goto err_reset; |
| 736 | } |
| 737 | |
| 738 | switch (reg) { |
| 739 | case 0x5102: |
| 740 | case 0x5110: |
Charles Keepax | dc7d486 | 2013-06-13 09:43:29 +0100 | [diff] [blame] | 741 | case 0x8997: |
Mark Brown | ca76ceb | 2013-04-09 16:04:35 +0100 | [diff] [blame] | 742 | break; |
| 743 | default: |
| 744 | dev_err(arizona->dev, "Unknown device ID: %x\n", reg); |
| 745 | goto err_reset; |
| 746 | } |
| 747 | |
| 748 | /* If we have a /RESET GPIO we'll already be reset */ |
| 749 | if (!arizona->pdata.reset) { |
| 750 | regcache_mark_dirty(arizona->regmap); |
| 751 | |
| 752 | ret = regmap_write(arizona->regmap, ARIZONA_SOFTWARE_RESET, 0); |
| 753 | if (ret != 0) { |
| 754 | dev_err(dev, "Failed to reset device: %d\n", ret); |
| 755 | goto err_reset; |
| 756 | } |
| 757 | |
| 758 | msleep(1); |
| 759 | |
| 760 | ret = regcache_sync(arizona->regmap); |
| 761 | if (ret != 0) { |
| 762 | dev_err(dev, "Failed to sync device: %d\n", ret); |
| 763 | goto err_reset; |
| 764 | } |
| 765 | } |
| 766 | |
| 767 | /* Ensure device startup is complete */ |
| 768 | switch (arizona->type) { |
| 769 | case WM5102: |
| 770 | ret = regmap_read(arizona->regmap, 0x19, &val); |
| 771 | if (ret != 0) |
| 772 | dev_err(dev, |
| 773 | "Failed to check write sequencer state: %d\n", |
| 774 | ret); |
| 775 | else if (val & 0x01) |
| 776 | break; |
| 777 | /* Fall through */ |
| 778 | default: |
| 779 | ret = arizona_wait_for_boot(arizona); |
| 780 | if (ret != 0) { |
| 781 | dev_err(arizona->dev, |
| 782 | "Device failed initial boot: %d\n", ret); |
| 783 | goto err_reset; |
| 784 | } |
| 785 | break; |
| 786 | } |
| 787 | |
| 788 | /* Read the device ID information & do device specific stuff */ |
Mark Brown | 3cc7298 | 2012-06-19 16:31:53 +0100 | [diff] [blame] | 789 | ret = regmap_read(arizona->regmap, ARIZONA_SOFTWARE_RESET, ®); |
| 790 | if (ret != 0) { |
| 791 | dev_err(dev, "Failed to read ID register: %d\n", ret); |
Mark Brown | 59db969 | 2012-07-09 00:31:36 +0200 | [diff] [blame] | 792 | goto err_reset; |
Mark Brown | 3cc7298 | 2012-06-19 16:31:53 +0100 | [diff] [blame] | 793 | } |
| 794 | |
| 795 | ret = regmap_read(arizona->regmap, ARIZONA_DEVICE_REVISION, |
| 796 | &arizona->rev); |
| 797 | if (ret != 0) { |
| 798 | dev_err(dev, "Failed to read revision register: %d\n", ret); |
Mark Brown | 59db969 | 2012-07-09 00:31:36 +0200 | [diff] [blame] | 799 | goto err_reset; |
Mark Brown | 3cc7298 | 2012-06-19 16:31:53 +0100 | [diff] [blame] | 800 | } |
| 801 | arizona->rev &= ARIZONA_DEVICE_REVISION_MASK; |
| 802 | |
| 803 | switch (reg) { |
Mark Brown | 863df8d | 2012-07-05 20:35:31 +0100 | [diff] [blame] | 804 | #ifdef CONFIG_MFD_WM5102 |
Mark Brown | 3cc7298 | 2012-06-19 16:31:53 +0100 | [diff] [blame] | 805 | case 0x5102: |
| 806 | type_name = "WM5102"; |
| 807 | if (arizona->type != WM5102) { |
| 808 | dev_err(arizona->dev, "WM5102 registered as %d\n", |
| 809 | arizona->type); |
| 810 | arizona->type = WM5102; |
| 811 | } |
Mark Brown | 62d62b5 | 2012-12-02 11:41:46 +0900 | [diff] [blame] | 812 | apply_patch = wm5102_patch; |
Mark Brown | c6d6bfb | 2013-03-26 11:14:52 +0000 | [diff] [blame] | 813 | arizona->rev &= 0x7; |
Mark Brown | 3cc7298 | 2012-06-19 16:31:53 +0100 | [diff] [blame] | 814 | break; |
Mark Brown | 863df8d | 2012-07-05 20:35:31 +0100 | [diff] [blame] | 815 | #endif |
Mark Brown | e102bef | 2012-07-10 12:37:58 +0100 | [diff] [blame] | 816 | #ifdef CONFIG_MFD_WM5110 |
| 817 | case 0x5110: |
| 818 | type_name = "WM5110"; |
| 819 | if (arizona->type != WM5110) { |
| 820 | dev_err(arizona->dev, "WM5110 registered as %d\n", |
| 821 | arizona->type); |
| 822 | arizona->type = WM5110; |
| 823 | } |
Mark Brown | 62d62b5 | 2012-12-02 11:41:46 +0900 | [diff] [blame] | 824 | apply_patch = wm5110_patch; |
Mark Brown | e102bef | 2012-07-10 12:37:58 +0100 | [diff] [blame] | 825 | break; |
| 826 | #endif |
Charles Keepax | dc7d486 | 2013-06-13 09:43:29 +0100 | [diff] [blame] | 827 | #ifdef CONFIG_MFD_WM8997 |
| 828 | case 0x8997: |
| 829 | type_name = "WM8997"; |
| 830 | if (arizona->type != WM8997) { |
| 831 | dev_err(arizona->dev, "WM8997 registered as %d\n", |
| 832 | arizona->type); |
| 833 | arizona->type = WM8997; |
| 834 | } |
| 835 | apply_patch = wm8997_patch; |
| 836 | break; |
| 837 | #endif |
Mark Brown | 3cc7298 | 2012-06-19 16:31:53 +0100 | [diff] [blame] | 838 | default: |
| 839 | dev_err(arizona->dev, "Unknown device ID %x\n", reg); |
Mark Brown | 59db969 | 2012-07-09 00:31:36 +0200 | [diff] [blame] | 840 | goto err_reset; |
Mark Brown | 3cc7298 | 2012-06-19 16:31:53 +0100 | [diff] [blame] | 841 | } |
| 842 | |
| 843 | dev_info(dev, "%s revision %c\n", type_name, arizona->rev + 'A'); |
| 844 | |
Mark Brown | 62d62b5 | 2012-12-02 11:41:46 +0900 | [diff] [blame] | 845 | if (apply_patch) { |
| 846 | ret = apply_patch(arizona); |
| 847 | if (ret != 0) { |
| 848 | dev_err(arizona->dev, "Failed to apply patch: %d\n", |
| 849 | ret); |
| 850 | goto err_reset; |
| 851 | } |
Charles Keepax | e80436b | 2013-03-26 18:46:15 +0000 | [diff] [blame] | 852 | |
| 853 | switch (arizona->type) { |
| 854 | case WM5102: |
| 855 | ret = arizona_apply_hardware_patch(arizona); |
| 856 | if (ret != 0) { |
| 857 | dev_err(arizona->dev, |
| 858 | "Failed to apply hardware patch: %d\n", |
| 859 | ret); |
| 860 | goto err_reset; |
| 861 | } |
| 862 | break; |
| 863 | default: |
| 864 | break; |
| 865 | } |
Mark Brown | 62d62b5 | 2012-12-02 11:41:46 +0900 | [diff] [blame] | 866 | } |
| 867 | |
Mark Brown | 3cc7298 | 2012-06-19 16:31:53 +0100 | [diff] [blame] | 868 | for (i = 0; i < ARRAY_SIZE(arizona->pdata.gpio_defaults); i++) { |
| 869 | if (!arizona->pdata.gpio_defaults[i]) |
| 870 | continue; |
| 871 | |
| 872 | regmap_write(arizona->regmap, ARIZONA_GPIO1_CTRL + i, |
| 873 | arizona->pdata.gpio_defaults[i]); |
| 874 | } |
| 875 | |
| 876 | pm_runtime_set_autosuspend_delay(arizona->dev, 100); |
| 877 | pm_runtime_use_autosuspend(arizona->dev); |
| 878 | pm_runtime_enable(arizona->dev); |
| 879 | |
| 880 | /* Chip default */ |
| 881 | if (!arizona->pdata.clk32k_src) |
| 882 | arizona->pdata.clk32k_src = ARIZONA_32KZ_MCLK2; |
| 883 | |
| 884 | switch (arizona->pdata.clk32k_src) { |
| 885 | case ARIZONA_32KZ_MCLK1: |
| 886 | case ARIZONA_32KZ_MCLK2: |
| 887 | regmap_update_bits(arizona->regmap, ARIZONA_CLOCK_32K_1, |
| 888 | ARIZONA_CLK_32K_SRC_MASK, |
| 889 | arizona->pdata.clk32k_src - 1); |
Mark Brown | 767c6dc | 2013-03-19 19:04:46 +0100 | [diff] [blame] | 890 | arizona_clk32k_enable(arizona); |
Mark Brown | 3cc7298 | 2012-06-19 16:31:53 +0100 | [diff] [blame] | 891 | break; |
| 892 | case ARIZONA_32KZ_NONE: |
| 893 | regmap_update_bits(arizona->regmap, ARIZONA_CLOCK_32K_1, |
| 894 | ARIZONA_CLK_32K_SRC_MASK, 2); |
| 895 | break; |
| 896 | default: |
| 897 | dev_err(arizona->dev, "Invalid 32kHz clock source: %d\n", |
| 898 | arizona->pdata.clk32k_src); |
| 899 | ret = -EINVAL; |
Mark Brown | 59db969 | 2012-07-09 00:31:36 +0200 | [diff] [blame] | 900 | goto err_reset; |
Mark Brown | 3cc7298 | 2012-06-19 16:31:53 +0100 | [diff] [blame] | 901 | } |
| 902 | |
Mark Brown | 3d91f82 | 2013-01-29 00:47:37 +0800 | [diff] [blame] | 903 | for (i = 0; i < ARIZONA_MAX_MICBIAS; i++) { |
Mark Brown | 544c7aa | 2013-01-29 18:44:41 +0800 | [diff] [blame] | 904 | if (!arizona->pdata.micbias[i].mV && |
| 905 | !arizona->pdata.micbias[i].bypass) |
Mark Brown | 3d91f82 | 2013-01-29 00:47:37 +0800 | [diff] [blame] | 906 | continue; |
| 907 | |
Mark Brown | 544c7aa | 2013-01-29 18:44:41 +0800 | [diff] [blame] | 908 | /* Apply default for bypass mode */ |
| 909 | if (!arizona->pdata.micbias[i].mV) |
| 910 | arizona->pdata.micbias[i].mV = 2800; |
| 911 | |
Mark Brown | 3d91f82 | 2013-01-29 00:47:37 +0800 | [diff] [blame] | 912 | val = (arizona->pdata.micbias[i].mV - 1500) / 100; |
Mark Brown | 544c7aa | 2013-01-29 18:44:41 +0800 | [diff] [blame] | 913 | |
Mark Brown | 3d91f82 | 2013-01-29 00:47:37 +0800 | [diff] [blame] | 914 | val <<= ARIZONA_MICB1_LVL_SHIFT; |
| 915 | |
| 916 | if (arizona->pdata.micbias[i].ext_cap) |
| 917 | val |= ARIZONA_MICB1_EXT_CAP; |
| 918 | |
| 919 | if (arizona->pdata.micbias[i].discharge) |
| 920 | val |= ARIZONA_MICB1_DISCH; |
| 921 | |
Charles Keepax | f773fc6 | 2013-05-21 14:56:58 +0100 | [diff] [blame] | 922 | if (arizona->pdata.micbias[i].soft_start) |
Mark Brown | 3d91f82 | 2013-01-29 00:47:37 +0800 | [diff] [blame] | 923 | val |= ARIZONA_MICB1_RATE; |
| 924 | |
Mark Brown | 544c7aa | 2013-01-29 18:44:41 +0800 | [diff] [blame] | 925 | if (arizona->pdata.micbias[i].bypass) |
| 926 | val |= ARIZONA_MICB1_BYPASS; |
| 927 | |
Mark Brown | 3d91f82 | 2013-01-29 00:47:37 +0800 | [diff] [blame] | 928 | regmap_update_bits(arizona->regmap, |
| 929 | ARIZONA_MIC_BIAS_CTRL_1 + i, |
| 930 | ARIZONA_MICB1_LVL_MASK | |
| 931 | ARIZONA_MICB1_DISCH | |
Mark Brown | 544c7aa | 2013-01-29 18:44:41 +0800 | [diff] [blame] | 932 | ARIZONA_MICB1_BYPASS | |
Mark Brown | 3d91f82 | 2013-01-29 00:47:37 +0800 | [diff] [blame] | 933 | ARIZONA_MICB1_RATE, val); |
| 934 | } |
| 935 | |
Mark Brown | 3cc7298 | 2012-06-19 16:31:53 +0100 | [diff] [blame] | 936 | for (i = 0; i < ARIZONA_MAX_INPUT; i++) { |
| 937 | /* Default for both is 0 so noop with defaults */ |
| 938 | val = arizona->pdata.dmic_ref[i] |
| 939 | << ARIZONA_IN1_DMIC_SUP_SHIFT; |
| 940 | val |= arizona->pdata.inmode[i] << ARIZONA_IN1_MODE_SHIFT; |
| 941 | |
| 942 | regmap_update_bits(arizona->regmap, |
| 943 | ARIZONA_IN1L_CONTROL + (i * 8), |
| 944 | ARIZONA_IN1_DMIC_SUP_MASK | |
| 945 | ARIZONA_IN1_MODE_MASK, val); |
| 946 | } |
| 947 | |
| 948 | for (i = 0; i < ARIZONA_MAX_OUTPUT; i++) { |
| 949 | /* Default is 0 so noop with defaults */ |
| 950 | if (arizona->pdata.out_mono[i]) |
| 951 | val = ARIZONA_OUT1_MONO; |
| 952 | else |
| 953 | val = 0; |
| 954 | |
| 955 | regmap_update_bits(arizona->regmap, |
| 956 | ARIZONA_OUTPUT_PATH_CONFIG_1L + (i * 8), |
| 957 | ARIZONA_OUT1_MONO, val); |
| 958 | } |
| 959 | |
Mark Brown | 3cc7298 | 2012-06-19 16:31:53 +0100 | [diff] [blame] | 960 | for (i = 0; i < ARIZONA_MAX_PDM_SPK; i++) { |
| 961 | if (arizona->pdata.spk_mute[i]) |
| 962 | regmap_update_bits(arizona->regmap, |
Mark Brown | 2a51da0 | 2012-07-09 19:33:14 +0100 | [diff] [blame] | 963 | ARIZONA_PDM_SPK1_CTRL_1 + (i * 2), |
Mark Brown | 3cc7298 | 2012-06-19 16:31:53 +0100 | [diff] [blame] | 964 | ARIZONA_SPK1_MUTE_ENDIAN_MASK | |
| 965 | ARIZONA_SPK1_MUTE_SEQ1_MASK, |
| 966 | arizona->pdata.spk_mute[i]); |
| 967 | |
| 968 | if (arizona->pdata.spk_fmt[i]) |
| 969 | regmap_update_bits(arizona->regmap, |
Mark Brown | 2a51da0 | 2012-07-09 19:33:14 +0100 | [diff] [blame] | 970 | ARIZONA_PDM_SPK1_CTRL_2 + (i * 2), |
Mark Brown | 3cc7298 | 2012-06-19 16:31:53 +0100 | [diff] [blame] | 971 | ARIZONA_SPK1_FMT_MASK, |
| 972 | arizona->pdata.spk_fmt[i]); |
| 973 | } |
| 974 | |
| 975 | /* Set up for interrupts */ |
| 976 | ret = arizona_irq_init(arizona); |
| 977 | if (ret != 0) |
Mark Brown | 59db969 | 2012-07-09 00:31:36 +0200 | [diff] [blame] | 978 | goto err_reset; |
Mark Brown | 3cc7298 | 2012-06-19 16:31:53 +0100 | [diff] [blame] | 979 | |
| 980 | arizona_request_irq(arizona, ARIZONA_IRQ_CLKGEN_ERR, "CLKGEN error", |
| 981 | arizona_clkgen_err, arizona); |
| 982 | arizona_request_irq(arizona, ARIZONA_IRQ_OVERCLOCKED, "Overclocked", |
| 983 | arizona_overclocked, arizona); |
| 984 | arizona_request_irq(arizona, ARIZONA_IRQ_UNDERCLOCKED, "Underclocked", |
| 985 | arizona_underclocked, arizona); |
| 986 | |
| 987 | switch (arizona->type) { |
| 988 | case WM5102: |
| 989 | ret = mfd_add_devices(arizona->dev, -1, wm5102_devs, |
Mark Brown | 0848c94 | 2012-09-11 15:16:36 +0800 | [diff] [blame] | 990 | ARRAY_SIZE(wm5102_devs), NULL, 0, NULL); |
Mark Brown | 3cc7298 | 2012-06-19 16:31:53 +0100 | [diff] [blame] | 991 | break; |
Mark Brown | e102bef | 2012-07-10 12:37:58 +0100 | [diff] [blame] | 992 | case WM5110: |
| 993 | ret = mfd_add_devices(arizona->dev, -1, wm5110_devs, |
Charles Keepax | 78566af | 2012-11-20 13:46:19 +0900 | [diff] [blame] | 994 | ARRAY_SIZE(wm5110_devs), NULL, 0, NULL); |
Mark Brown | e102bef | 2012-07-10 12:37:58 +0100 | [diff] [blame] | 995 | break; |
Charles Keepax | dc7d486 | 2013-06-13 09:43:29 +0100 | [diff] [blame] | 996 | case WM8997: |
| 997 | ret = mfd_add_devices(arizona->dev, -1, wm8997_devs, |
| 998 | ARRAY_SIZE(wm8997_devs), NULL, 0, NULL); |
| 999 | break; |
Mark Brown | 3cc7298 | 2012-06-19 16:31:53 +0100 | [diff] [blame] | 1000 | } |
| 1001 | |
| 1002 | if (ret != 0) { |
| 1003 | dev_err(arizona->dev, "Failed to add subdevices: %d\n", ret); |
| 1004 | goto err_irq; |
| 1005 | } |
| 1006 | |
Mark Brown | 59db969 | 2012-07-09 00:31:36 +0200 | [diff] [blame] | 1007 | #ifdef CONFIG_PM_RUNTIME |
| 1008 | regulator_disable(arizona->dcvdd); |
| 1009 | #endif |
| 1010 | |
Mark Brown | 3cc7298 | 2012-06-19 16:31:53 +0100 | [diff] [blame] | 1011 | return 0; |
| 1012 | |
| 1013 | err_irq: |
| 1014 | arizona_irq_exit(arizona); |
Mark Brown | 3cc7298 | 2012-06-19 16:31:53 +0100 | [diff] [blame] | 1015 | err_reset: |
| 1016 | if (arizona->pdata.reset) { |
Mark Brown | 87d3af4 | 2013-03-26 12:15:26 +0000 | [diff] [blame] | 1017 | gpio_set_value_cansleep(arizona->pdata.reset, 0); |
Mark Brown | 3cc7298 | 2012-06-19 16:31:53 +0100 | [diff] [blame] | 1018 | gpio_free(arizona->pdata.reset); |
| 1019 | } |
Mark Brown | 59db969 | 2012-07-09 00:31:36 +0200 | [diff] [blame] | 1020 | regulator_disable(arizona->dcvdd); |
Mark Brown | 3cc7298 | 2012-06-19 16:31:53 +0100 | [diff] [blame] | 1021 | err_enable: |
Mark Brown | 3a36a0db | 2012-07-09 00:45:53 +0200 | [diff] [blame] | 1022 | regulator_bulk_disable(arizona->num_core_supplies, |
Mark Brown | 3cc7298 | 2012-06-19 16:31:53 +0100 | [diff] [blame] | 1023 | arizona->core_supplies); |
Charles Keepax | e602151 | 2014-06-02 09:50:41 +0100 | [diff] [blame] | 1024 | err_dcvdd: |
| 1025 | regulator_put(arizona->dcvdd); |
Mark Brown | 3cc7298 | 2012-06-19 16:31:53 +0100 | [diff] [blame] | 1026 | err_early: |
| 1027 | mfd_remove_devices(dev); |
| 1028 | return ret; |
| 1029 | } |
| 1030 | EXPORT_SYMBOL_GPL(arizona_dev_init); |
| 1031 | |
Bill Pemberton | 4740f73 | 2012-11-19 13:26:01 -0500 | [diff] [blame] | 1032 | int arizona_dev_exit(struct arizona *arizona) |
Mark Brown | 3cc7298 | 2012-06-19 16:31:53 +0100 | [diff] [blame] | 1033 | { |
Charles Keepax | b804020 | 2014-06-02 09:50:39 +0100 | [diff] [blame] | 1034 | pm_runtime_disable(arizona->dev); |
| 1035 | |
Charles Keepax | df6b335 | 2014-06-02 09:50:40 +0100 | [diff] [blame] | 1036 | regulator_disable(arizona->dcvdd); |
Charles Keepax | e602151 | 2014-06-02 09:50:41 +0100 | [diff] [blame] | 1037 | regulator_put(arizona->dcvdd); |
Charles Keepax | df6b335 | 2014-06-02 09:50:40 +0100 | [diff] [blame] | 1038 | |
Mark Brown | 3cc7298 | 2012-06-19 16:31:53 +0100 | [diff] [blame] | 1039 | mfd_remove_devices(arizona->dev); |
| 1040 | arizona_free_irq(arizona, ARIZONA_IRQ_UNDERCLOCKED, arizona); |
| 1041 | arizona_free_irq(arizona, ARIZONA_IRQ_OVERCLOCKED, arizona); |
| 1042 | arizona_free_irq(arizona, ARIZONA_IRQ_CLKGEN_ERR, arizona); |
Mark Brown | 3cc7298 | 2012-06-19 16:31:53 +0100 | [diff] [blame] | 1043 | arizona_irq_exit(arizona); |
Mark Brown | 1d017b6 | 2013-03-26 12:16:26 +0000 | [diff] [blame] | 1044 | if (arizona->pdata.reset) |
| 1045 | gpio_set_value_cansleep(arizona->pdata.reset, 0); |
Charles Keepax | df6b335 | 2014-06-02 09:50:40 +0100 | [diff] [blame] | 1046 | |
Charles Keepax | 4420286 | 2014-06-02 09:50:42 +0100 | [diff] [blame] | 1047 | regulator_bulk_disable(arizona->num_core_supplies, |
Mark Brown | 1d017b6 | 2013-03-26 12:16:26 +0000 | [diff] [blame] | 1048 | arizona->core_supplies); |
Mark Brown | 3cc7298 | 2012-06-19 16:31:53 +0100 | [diff] [blame] | 1049 | return 0; |
| 1050 | } |
| 1051 | EXPORT_SYMBOL_GPL(arizona_dev_exit); |