e1000e: cleanup PARENTHESIS_ALIGNMENT checkpatch checks
[linux/fpc-iii.git] / drivers / mfd / arizona-core.c
blobb562c7bf8a463903bf4b99d0b6c54b9a1358cf4f
1 /*
2 * Arizona core driver
4 * Copyright 2012 Wolfson Microelectronics plc
6 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
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.
13 #include <linux/delay.h>
14 #include <linux/err.h>
15 #include <linux/gpio.h>
16 #include <linux/interrupt.h>
17 #include <linux/mfd/core.h>
18 #include <linux/module.h>
19 #include <linux/pm_runtime.h>
20 #include <linux/regmap.h>
21 #include <linux/regulator/consumer.h>
22 #include <linux/slab.h>
24 #include <linux/mfd/arizona/core.h>
25 #include <linux/mfd/arizona/registers.h>
27 #include "arizona.h"
29 static const char *wm5102_core_supplies[] = {
30 "AVDD",
31 "DBVDD1",
34 int arizona_clk32k_enable(struct arizona *arizona)
36 int ret = 0;
38 mutex_lock(&arizona->clk_lock);
40 arizona->clk32k_ref++;
42 if (arizona->clk32k_ref == 1)
43 ret = regmap_update_bits(arizona->regmap, ARIZONA_CLOCK_32K_1,
44 ARIZONA_CLK_32K_ENA,
45 ARIZONA_CLK_32K_ENA);
47 if (ret != 0)
48 arizona->clk32k_ref--;
50 mutex_unlock(&arizona->clk_lock);
52 return ret;
54 EXPORT_SYMBOL_GPL(arizona_clk32k_enable);
56 int arizona_clk32k_disable(struct arizona *arizona)
58 int ret = 0;
60 mutex_lock(&arizona->clk_lock);
62 BUG_ON(arizona->clk32k_ref <= 0);
64 arizona->clk32k_ref--;
66 if (arizona->clk32k_ref == 0)
67 regmap_update_bits(arizona->regmap, ARIZONA_CLOCK_32K_1,
68 ARIZONA_CLK_32K_ENA, 0);
70 mutex_unlock(&arizona->clk_lock);
72 return ret;
74 EXPORT_SYMBOL_GPL(arizona_clk32k_disable);
76 static irqreturn_t arizona_clkgen_err(int irq, void *data)
78 struct arizona *arizona = data;
80 dev_err(arizona->dev, "CLKGEN error\n");
82 return IRQ_HANDLED;
85 static irqreturn_t arizona_underclocked(int irq, void *data)
87 struct arizona *arizona = data;
88 unsigned int val;
89 int ret;
91 ret = regmap_read(arizona->regmap, ARIZONA_INTERRUPT_RAW_STATUS_8,
92 &val);
93 if (ret != 0) {
94 dev_err(arizona->dev, "Failed to read underclock status: %d\n",
95 ret);
96 return IRQ_NONE;
99 if (val & ARIZONA_AIF3_UNDERCLOCKED_STS)
100 dev_err(arizona->dev, "AIF3 underclocked\n");
101 if (val & ARIZONA_AIF2_UNDERCLOCKED_STS)
102 dev_err(arizona->dev, "AIF2 underclocked\n");
103 if (val & ARIZONA_AIF1_UNDERCLOCKED_STS)
104 dev_err(arizona->dev, "AIF1 underclocked\n");
105 if (val & ARIZONA_ISRC2_UNDERCLOCKED_STS)
106 dev_err(arizona->dev, "ISRC2 underclocked\n");
107 if (val & ARIZONA_ISRC1_UNDERCLOCKED_STS)
108 dev_err(arizona->dev, "ISRC1 underclocked\n");
109 if (val & ARIZONA_FX_UNDERCLOCKED_STS)
110 dev_err(arizona->dev, "FX underclocked\n");
111 if (val & ARIZONA_ASRC_UNDERCLOCKED_STS)
112 dev_err(arizona->dev, "ASRC underclocked\n");
113 if (val & ARIZONA_DAC_UNDERCLOCKED_STS)
114 dev_err(arizona->dev, "DAC underclocked\n");
115 if (val & ARIZONA_ADC_UNDERCLOCKED_STS)
116 dev_err(arizona->dev, "ADC underclocked\n");
117 if (val & ARIZONA_MIXER_UNDERCLOCKED_STS)
118 dev_err(arizona->dev, "Mixer dropped sample\n");
120 return IRQ_HANDLED;
123 static irqreturn_t arizona_overclocked(int irq, void *data)
125 struct arizona *arizona = data;
126 unsigned int val[2];
127 int ret;
129 ret = regmap_bulk_read(arizona->regmap, ARIZONA_INTERRUPT_RAW_STATUS_6,
130 &val[0], 2);
131 if (ret != 0) {
132 dev_err(arizona->dev, "Failed to read overclock status: %d\n",
133 ret);
134 return IRQ_NONE;
137 if (val[0] & ARIZONA_PWM_OVERCLOCKED_STS)
138 dev_err(arizona->dev, "PWM overclocked\n");
139 if (val[0] & ARIZONA_FX_CORE_OVERCLOCKED_STS)
140 dev_err(arizona->dev, "FX core overclocked\n");
141 if (val[0] & ARIZONA_DAC_SYS_OVERCLOCKED_STS)
142 dev_err(arizona->dev, "DAC SYS overclocked\n");
143 if (val[0] & ARIZONA_DAC_WARP_OVERCLOCKED_STS)
144 dev_err(arizona->dev, "DAC WARP overclocked\n");
145 if (val[0] & ARIZONA_ADC_OVERCLOCKED_STS)
146 dev_err(arizona->dev, "ADC overclocked\n");
147 if (val[0] & ARIZONA_MIXER_OVERCLOCKED_STS)
148 dev_err(arizona->dev, "Mixer overclocked\n");
149 if (val[0] & ARIZONA_AIF3_SYNC_OVERCLOCKED_STS)
150 dev_err(arizona->dev, "AIF3 overclocked\n");
151 if (val[0] & ARIZONA_AIF2_SYNC_OVERCLOCKED_STS)
152 dev_err(arizona->dev, "AIF2 overclocked\n");
153 if (val[0] & ARIZONA_AIF1_SYNC_OVERCLOCKED_STS)
154 dev_err(arizona->dev, "AIF1 overclocked\n");
155 if (val[0] & ARIZONA_PAD_CTRL_OVERCLOCKED_STS)
156 dev_err(arizona->dev, "Pad control overclocked\n");
158 if (val[1] & ARIZONA_SLIMBUS_SUBSYS_OVERCLOCKED_STS)
159 dev_err(arizona->dev, "Slimbus subsystem overclocked\n");
160 if (val[1] & ARIZONA_SLIMBUS_ASYNC_OVERCLOCKED_STS)
161 dev_err(arizona->dev, "Slimbus async overclocked\n");
162 if (val[1] & ARIZONA_SLIMBUS_SYNC_OVERCLOCKED_STS)
163 dev_err(arizona->dev, "Slimbus sync overclocked\n");
164 if (val[1] & ARIZONA_ASRC_ASYNC_SYS_OVERCLOCKED_STS)
165 dev_err(arizona->dev, "ASRC async system overclocked\n");
166 if (val[1] & ARIZONA_ASRC_ASYNC_WARP_OVERCLOCKED_STS)
167 dev_err(arizona->dev, "ASRC async WARP overclocked\n");
168 if (val[1] & ARIZONA_ASRC_SYNC_SYS_OVERCLOCKED_STS)
169 dev_err(arizona->dev, "ASRC sync system overclocked\n");
170 if (val[1] & ARIZONA_ASRC_SYNC_WARP_OVERCLOCKED_STS)
171 dev_err(arizona->dev, "ASRC sync WARP overclocked\n");
172 if (val[1] & ARIZONA_ADSP2_1_OVERCLOCKED_STS)
173 dev_err(arizona->dev, "DSP1 overclocked\n");
174 if (val[1] & ARIZONA_ISRC2_OVERCLOCKED_STS)
175 dev_err(arizona->dev, "ISRC2 overclocked\n");
176 if (val[1] & ARIZONA_ISRC1_OVERCLOCKED_STS)
177 dev_err(arizona->dev, "ISRC1 overclocked\n");
179 return IRQ_HANDLED;
182 static int arizona_wait_for_boot(struct arizona *arizona)
184 unsigned int reg;
185 int ret, i;
188 * We can't use an interrupt as we need to runtime resume to do so,
189 * we won't race with the interrupt handler as it'll be blocked on
190 * runtime resume.
192 for (i = 0; i < 5; i++) {
193 msleep(1);
195 ret = regmap_read(arizona->regmap,
196 ARIZONA_INTERRUPT_RAW_STATUS_5, &reg);
197 if (ret != 0) {
198 dev_err(arizona->dev, "Failed to read boot state: %d\n",
199 ret);
200 continue;
203 if (reg & ARIZONA_BOOT_DONE_STS)
204 break;
207 if (reg & ARIZONA_BOOT_DONE_STS) {
208 regmap_write(arizona->regmap, ARIZONA_INTERRUPT_STATUS_5,
209 ARIZONA_BOOT_DONE_STS);
210 } else {
211 dev_err(arizona->dev, "Device boot timed out: %x\n", reg);
212 return -ETIMEDOUT;
215 pm_runtime_mark_last_busy(arizona->dev);
217 return 0;
220 #ifdef CONFIG_PM_RUNTIME
221 static int arizona_runtime_resume(struct device *dev)
223 struct arizona *arizona = dev_get_drvdata(dev);
224 int ret;
226 dev_dbg(arizona->dev, "Leaving AoD mode\n");
228 ret = regulator_enable(arizona->dcvdd);
229 if (ret != 0) {
230 dev_err(arizona->dev, "Failed to enable DCVDD: %d\n", ret);
231 return ret;
234 regcache_cache_only(arizona->regmap, false);
236 ret = arizona_wait_for_boot(arizona);
237 if (ret != 0) {
238 regulator_disable(arizona->dcvdd);
239 return ret;
242 ret = regcache_sync(arizona->regmap);
243 if (ret != 0) {
244 dev_err(arizona->dev, "Failed to restore register cache\n");
245 regulator_disable(arizona->dcvdd);
246 return ret;
249 return 0;
252 static int arizona_runtime_suspend(struct device *dev)
254 struct arizona *arizona = dev_get_drvdata(dev);
256 dev_dbg(arizona->dev, "Entering AoD mode\n");
258 regulator_disable(arizona->dcvdd);
259 regcache_cache_only(arizona->regmap, true);
260 regcache_mark_dirty(arizona->regmap);
262 return 0;
264 #endif
266 #ifdef CONFIG_PM_SLEEP
267 static int arizona_resume_noirq(struct device *dev)
269 struct arizona *arizona = dev_get_drvdata(dev);
271 dev_dbg(arizona->dev, "Early resume, disabling IRQ\n");
272 disable_irq(arizona->irq);
274 return 0;
277 static int arizona_resume(struct device *dev)
279 struct arizona *arizona = dev_get_drvdata(dev);
281 dev_dbg(arizona->dev, "Late resume, reenabling IRQ\n");
282 enable_irq(arizona->irq);
284 return 0;
286 #endif
288 const struct dev_pm_ops arizona_pm_ops = {
289 SET_RUNTIME_PM_OPS(arizona_runtime_suspend,
290 arizona_runtime_resume,
291 NULL)
292 SET_SYSTEM_SLEEP_PM_OPS(NULL, arizona_resume)
293 #ifdef CONFIG_PM_SLEEP
294 .resume_noirq = arizona_resume_noirq,
295 #endif
297 EXPORT_SYMBOL_GPL(arizona_pm_ops);
299 static struct mfd_cell early_devs[] = {
300 { .name = "arizona-ldo1" },
303 static struct mfd_cell wm5102_devs[] = {
304 { .name = "arizona-micsupp" },
305 { .name = "arizona-extcon" },
306 { .name = "arizona-gpio" },
307 { .name = "arizona-haptics" },
308 { .name = "arizona-pwm" },
309 { .name = "wm5102-codec" },
312 static struct mfd_cell wm5110_devs[] = {
313 { .name = "arizona-micsupp" },
314 { .name = "arizona-extcon" },
315 { .name = "arizona-gpio" },
316 { .name = "arizona-haptics" },
317 { .name = "arizona-pwm" },
318 { .name = "wm5110-codec" },
321 int arizona_dev_init(struct arizona *arizona)
323 struct device *dev = arizona->dev;
324 const char *type_name;
325 unsigned int reg, val;
326 int (*apply_patch)(struct arizona *) = NULL;
327 int ret, i;
329 dev_set_drvdata(arizona->dev, arizona);
330 mutex_init(&arizona->clk_lock);
332 if (dev_get_platdata(arizona->dev))
333 memcpy(&arizona->pdata, dev_get_platdata(arizona->dev),
334 sizeof(arizona->pdata));
336 regcache_cache_only(arizona->regmap, true);
338 switch (arizona->type) {
339 case WM5102:
340 case WM5110:
341 for (i = 0; i < ARRAY_SIZE(wm5102_core_supplies); i++)
342 arizona->core_supplies[i].supply
343 = wm5102_core_supplies[i];
344 arizona->num_core_supplies = ARRAY_SIZE(wm5102_core_supplies);
345 break;
346 default:
347 dev_err(arizona->dev, "Unknown device type %d\n",
348 arizona->type);
349 return -EINVAL;
352 ret = mfd_add_devices(arizona->dev, -1, early_devs,
353 ARRAY_SIZE(early_devs), NULL, 0, NULL);
354 if (ret != 0) {
355 dev_err(dev, "Failed to add early children: %d\n", ret);
356 return ret;
359 ret = devm_regulator_bulk_get(dev, arizona->num_core_supplies,
360 arizona->core_supplies);
361 if (ret != 0) {
362 dev_err(dev, "Failed to request core supplies: %d\n",
363 ret);
364 goto err_early;
367 arizona->dcvdd = devm_regulator_get(arizona->dev, "DCVDD");
368 if (IS_ERR(arizona->dcvdd)) {
369 ret = PTR_ERR(arizona->dcvdd);
370 dev_err(dev, "Failed to request DCVDD: %d\n", ret);
371 goto err_early;
374 ret = regulator_bulk_enable(arizona->num_core_supplies,
375 arizona->core_supplies);
376 if (ret != 0) {
377 dev_err(dev, "Failed to enable core supplies: %d\n",
378 ret);
379 goto err_early;
382 ret = regulator_enable(arizona->dcvdd);
383 if (ret != 0) {
384 dev_err(dev, "Failed to enable DCVDD: %d\n", ret);
385 goto err_enable;
388 if (arizona->pdata.reset) {
389 /* Start out with /RESET low to put the chip into reset */
390 ret = gpio_request_one(arizona->pdata.reset,
391 GPIOF_DIR_OUT | GPIOF_INIT_LOW,
392 "arizona /RESET");
393 if (ret != 0) {
394 dev_err(dev, "Failed to request /RESET: %d\n", ret);
395 goto err_dcvdd;
398 gpio_set_value_cansleep(arizona->pdata.reset, 1);
401 regcache_cache_only(arizona->regmap, false);
403 ret = regmap_read(arizona->regmap, ARIZONA_SOFTWARE_RESET, &reg);
404 if (ret != 0) {
405 dev_err(dev, "Failed to read ID register: %d\n", ret);
406 goto err_reset;
409 ret = regmap_read(arizona->regmap, ARIZONA_DEVICE_REVISION,
410 &arizona->rev);
411 if (ret != 0) {
412 dev_err(dev, "Failed to read revision register: %d\n", ret);
413 goto err_reset;
415 arizona->rev &= ARIZONA_DEVICE_REVISION_MASK;
417 switch (reg) {
418 #ifdef CONFIG_MFD_WM5102
419 case 0x5102:
420 type_name = "WM5102";
421 if (arizona->type != WM5102) {
422 dev_err(arizona->dev, "WM5102 registered as %d\n",
423 arizona->type);
424 arizona->type = WM5102;
426 apply_patch = wm5102_patch;
427 break;
428 #endif
429 #ifdef CONFIG_MFD_WM5110
430 case 0x5110:
431 type_name = "WM5110";
432 if (arizona->type != WM5110) {
433 dev_err(arizona->dev, "WM5110 registered as %d\n",
434 arizona->type);
435 arizona->type = WM5110;
437 apply_patch = wm5110_patch;
438 break;
439 #endif
440 default:
441 dev_err(arizona->dev, "Unknown device ID %x\n", reg);
442 goto err_reset;
445 dev_info(dev, "%s revision %c\n", type_name, arizona->rev + 'A');
447 /* If we have a /RESET GPIO we'll already be reset */
448 if (!arizona->pdata.reset) {
449 regcache_mark_dirty(arizona->regmap);
451 ret = regmap_write(arizona->regmap, ARIZONA_SOFTWARE_RESET, 0);
452 if (ret != 0) {
453 dev_err(dev, "Failed to reset device: %d\n", ret);
454 goto err_reset;
457 ret = regcache_sync(arizona->regmap);
458 if (ret != 0) {
459 dev_err(dev, "Failed to sync device: %d\n", ret);
460 goto err_reset;
464 ret = arizona_wait_for_boot(arizona);
465 if (ret != 0) {
466 dev_err(arizona->dev, "Device failed initial boot: %d\n", ret);
467 goto err_reset;
470 if (apply_patch) {
471 ret = apply_patch(arizona);
472 if (ret != 0) {
473 dev_err(arizona->dev, "Failed to apply patch: %d\n",
474 ret);
475 goto err_reset;
479 for (i = 0; i < ARRAY_SIZE(arizona->pdata.gpio_defaults); i++) {
480 if (!arizona->pdata.gpio_defaults[i])
481 continue;
483 regmap_write(arizona->regmap, ARIZONA_GPIO1_CTRL + i,
484 arizona->pdata.gpio_defaults[i]);
487 pm_runtime_set_autosuspend_delay(arizona->dev, 100);
488 pm_runtime_use_autosuspend(arizona->dev);
489 pm_runtime_enable(arizona->dev);
491 /* Chip default */
492 if (!arizona->pdata.clk32k_src)
493 arizona->pdata.clk32k_src = ARIZONA_32KZ_MCLK2;
495 switch (arizona->pdata.clk32k_src) {
496 case ARIZONA_32KZ_MCLK1:
497 case ARIZONA_32KZ_MCLK2:
498 regmap_update_bits(arizona->regmap, ARIZONA_CLOCK_32K_1,
499 ARIZONA_CLK_32K_SRC_MASK,
500 arizona->pdata.clk32k_src - 1);
501 break;
502 case ARIZONA_32KZ_NONE:
503 regmap_update_bits(arizona->regmap, ARIZONA_CLOCK_32K_1,
504 ARIZONA_CLK_32K_SRC_MASK, 2);
505 break;
506 default:
507 dev_err(arizona->dev, "Invalid 32kHz clock source: %d\n",
508 arizona->pdata.clk32k_src);
509 ret = -EINVAL;
510 goto err_reset;
513 for (i = 0; i < ARIZONA_MAX_MICBIAS; i++) {
514 if (!arizona->pdata.micbias[i].mV)
515 continue;
517 val = (arizona->pdata.micbias[i].mV - 1500) / 100;
518 val <<= ARIZONA_MICB1_LVL_SHIFT;
520 if (arizona->pdata.micbias[i].ext_cap)
521 val |= ARIZONA_MICB1_EXT_CAP;
523 if (arizona->pdata.micbias[i].discharge)
524 val |= ARIZONA_MICB1_DISCH;
526 if (arizona->pdata.micbias[i].fast_start)
527 val |= ARIZONA_MICB1_RATE;
529 regmap_update_bits(arizona->regmap,
530 ARIZONA_MIC_BIAS_CTRL_1 + i,
531 ARIZONA_MICB1_LVL_MASK |
532 ARIZONA_MICB1_DISCH |
533 ARIZONA_MICB1_RATE, val);
536 for (i = 0; i < ARIZONA_MAX_INPUT; i++) {
537 /* Default for both is 0 so noop with defaults */
538 val = arizona->pdata.dmic_ref[i]
539 << ARIZONA_IN1_DMIC_SUP_SHIFT;
540 val |= arizona->pdata.inmode[i] << ARIZONA_IN1_MODE_SHIFT;
542 regmap_update_bits(arizona->regmap,
543 ARIZONA_IN1L_CONTROL + (i * 8),
544 ARIZONA_IN1_DMIC_SUP_MASK |
545 ARIZONA_IN1_MODE_MASK, val);
548 for (i = 0; i < ARIZONA_MAX_OUTPUT; i++) {
549 /* Default is 0 so noop with defaults */
550 if (arizona->pdata.out_mono[i])
551 val = ARIZONA_OUT1_MONO;
552 else
553 val = 0;
555 regmap_update_bits(arizona->regmap,
556 ARIZONA_OUTPUT_PATH_CONFIG_1L + (i * 8),
557 ARIZONA_OUT1_MONO, val);
560 for (i = 0; i < ARIZONA_MAX_PDM_SPK; i++) {
561 if (arizona->pdata.spk_mute[i])
562 regmap_update_bits(arizona->regmap,
563 ARIZONA_PDM_SPK1_CTRL_1 + (i * 2),
564 ARIZONA_SPK1_MUTE_ENDIAN_MASK |
565 ARIZONA_SPK1_MUTE_SEQ1_MASK,
566 arizona->pdata.spk_mute[i]);
568 if (arizona->pdata.spk_fmt[i])
569 regmap_update_bits(arizona->regmap,
570 ARIZONA_PDM_SPK1_CTRL_2 + (i * 2),
571 ARIZONA_SPK1_FMT_MASK,
572 arizona->pdata.spk_fmt[i]);
575 /* Set up for interrupts */
576 ret = arizona_irq_init(arizona);
577 if (ret != 0)
578 goto err_reset;
580 arizona_request_irq(arizona, ARIZONA_IRQ_CLKGEN_ERR, "CLKGEN error",
581 arizona_clkgen_err, arizona);
582 arizona_request_irq(arizona, ARIZONA_IRQ_OVERCLOCKED, "Overclocked",
583 arizona_overclocked, arizona);
584 arizona_request_irq(arizona, ARIZONA_IRQ_UNDERCLOCKED, "Underclocked",
585 arizona_underclocked, arizona);
587 switch (arizona->type) {
588 case WM5102:
589 ret = mfd_add_devices(arizona->dev, -1, wm5102_devs,
590 ARRAY_SIZE(wm5102_devs), NULL, 0, NULL);
591 break;
592 case WM5110:
593 ret = mfd_add_devices(arizona->dev, -1, wm5110_devs,
594 ARRAY_SIZE(wm5110_devs), NULL, 0, NULL);
595 break;
598 if (ret != 0) {
599 dev_err(arizona->dev, "Failed to add subdevices: %d\n", ret);
600 goto err_irq;
603 #ifdef CONFIG_PM_RUNTIME
604 regulator_disable(arizona->dcvdd);
605 #endif
607 return 0;
609 err_irq:
610 arizona_irq_exit(arizona);
611 err_reset:
612 if (arizona->pdata.reset) {
613 gpio_set_value_cansleep(arizona->pdata.reset, 1);
614 gpio_free(arizona->pdata.reset);
616 err_dcvdd:
617 regulator_disable(arizona->dcvdd);
618 err_enable:
619 regulator_bulk_disable(arizona->num_core_supplies,
620 arizona->core_supplies);
621 err_early:
622 mfd_remove_devices(dev);
623 return ret;
625 EXPORT_SYMBOL_GPL(arizona_dev_init);
627 int arizona_dev_exit(struct arizona *arizona)
629 mfd_remove_devices(arizona->dev);
630 arizona_free_irq(arizona, ARIZONA_IRQ_UNDERCLOCKED, arizona);
631 arizona_free_irq(arizona, ARIZONA_IRQ_OVERCLOCKED, arizona);
632 arizona_free_irq(arizona, ARIZONA_IRQ_CLKGEN_ERR, arizona);
633 pm_runtime_disable(arizona->dev);
634 arizona_irq_exit(arizona);
635 return 0;
637 EXPORT_SYMBOL_GPL(arizona_dev_exit);