1 // SPDX-License-Identifier: GPL-2.0-only
3 * Core MFD support for Cirrus Logic Madera codecs
5 * Copyright (C) 2015-2018 Cirrus Logic
8 #include <linux/device.h>
9 #include <linux/delay.h>
10 #include <linux/err.h>
11 #include <linux/gpio/consumer.h>
12 #include <linux/mfd/core.h>
13 #include <linux/module.h>
14 #include <linux/mutex.h>
15 #include <linux/notifier.h>
17 #include <linux/platform_device.h>
18 #include <linux/pm_runtime.h>
19 #include <linux/regmap.h>
20 #include <linux/regulator/consumer.h>
21 #include <linux/regulator/machine.h>
22 #include <linux/regulator/of_regulator.h>
24 #include <linux/mfd/madera/core.h>
25 #include <linux/mfd/madera/registers.h>
29 #define CS47L15_SILICON_ID 0x6370
30 #define CS47L35_SILICON_ID 0x6360
31 #define CS47L85_SILICON_ID 0x6338
32 #define CS47L90_SILICON_ID 0x6364
33 #define CS47L92_SILICON_ID 0x6371
35 #define MADERA_32KZ_MCLK2 1
37 #define MADERA_RESET_MIN_US 2000
38 #define MADERA_RESET_MAX_US 3000
40 #define ERRATA_DCVDD_MIN_US 10000
41 #define ERRATA_DCVDD_MAX_US 15000
43 static const char * const madera_core_supplies
[] = {
48 static const struct mfd_cell madera_ldo1_devs
[] = {
50 .name
= "madera-ldo1",
51 .level
= MFD_DEP_LEVEL_HIGH
,
55 static const char * const cs47l15_supplies
[] = {
61 static const struct mfd_cell cs47l15_devs
[] = {
62 { .name
= "madera-pinctrl", },
63 { .name
= "madera-irq", },
64 { .name
= "madera-gpio", },
66 .name
= "madera-extcon",
67 .parent_supplies
= cs47l15_supplies
,
68 .num_parent_supplies
= 1, /* We only need MICVDD */
71 .name
= "cs47l15-codec",
72 .parent_supplies
= cs47l15_supplies
,
73 .num_parent_supplies
= ARRAY_SIZE(cs47l15_supplies
),
77 static const char * const cs47l35_supplies
[] = {
85 static const struct mfd_cell cs47l35_devs
[] = {
86 { .name
= "madera-pinctrl", },
87 { .name
= "madera-irq", },
88 { .name
= "madera-micsupp", },
89 { .name
= "madera-gpio", },
91 .name
= "madera-extcon",
92 .parent_supplies
= cs47l35_supplies
,
93 .num_parent_supplies
= 1, /* We only need MICVDD */
96 .name
= "cs47l35-codec",
97 .parent_supplies
= cs47l35_supplies
,
98 .num_parent_supplies
= ARRAY_SIZE(cs47l35_supplies
),
102 static const char * const cs47l85_supplies
[] = {
113 static const struct mfd_cell cs47l85_devs
[] = {
114 { .name
= "madera-pinctrl", },
115 { .name
= "madera-irq", },
116 { .name
= "madera-micsupp", },
117 { .name
= "madera-gpio", },
119 .name
= "madera-extcon",
120 .parent_supplies
= cs47l85_supplies
,
121 .num_parent_supplies
= 1, /* We only need MICVDD */
124 .name
= "cs47l85-codec",
125 .parent_supplies
= cs47l85_supplies
,
126 .num_parent_supplies
= ARRAY_SIZE(cs47l85_supplies
),
130 static const char * const cs47l90_supplies
[] = {
139 static const struct mfd_cell cs47l90_devs
[] = {
140 { .name
= "madera-pinctrl", },
141 { .name
= "madera-irq", },
142 { .name
= "madera-micsupp", },
143 { .name
= "madera-gpio", },
145 .name
= "madera-extcon",
146 .parent_supplies
= cs47l90_supplies
,
147 .num_parent_supplies
= 1, /* We only need MICVDD */
150 .name
= "cs47l90-codec",
151 .parent_supplies
= cs47l90_supplies
,
152 .num_parent_supplies
= ARRAY_SIZE(cs47l90_supplies
),
156 static const char * const cs47l92_supplies
[] = {
162 static const struct mfd_cell cs47l92_devs
[] = {
163 { .name
= "madera-pinctrl", },
164 { .name
= "madera-irq", },
165 { .name
= "madera-micsupp", },
166 { .name
= "madera-gpio", },
168 .name
= "madera-extcon",
169 .parent_supplies
= cs47l92_supplies
,
170 .num_parent_supplies
= 1, /* We only need MICVDD */
173 .name
= "cs47l92-codec",
174 .parent_supplies
= cs47l92_supplies
,
175 .num_parent_supplies
= ARRAY_SIZE(cs47l92_supplies
),
179 /* Used by madera-i2c and madera-spi drivers */
180 const char *madera_name_from_type(enum madera_type type
)
205 EXPORT_SYMBOL_GPL(madera_name_from_type
);
207 #define MADERA_BOOT_POLL_INTERVAL_USEC 5000
208 #define MADERA_BOOT_POLL_TIMEOUT_USEC 25000
210 static int madera_wait_for_boot_noack(struct madera
*madera
)
213 unsigned int val
= 0;
217 * We can't use an interrupt as we need to runtime resume to do so,
218 * so we poll the status bit. This won't race with the interrupt
219 * handler because it will be blocked on runtime resume.
220 * The chip could NAK a read request while it is booting so ignore
221 * errors from regmap_read.
223 timeout
= ktime_add_us(ktime_get(), MADERA_BOOT_POLL_TIMEOUT_USEC
);
224 regmap_read(madera
->regmap
, MADERA_IRQ1_RAW_STATUS_1
, &val
);
225 while (!(val
& MADERA_BOOT_DONE_STS1
) &&
226 !ktime_after(ktime_get(), timeout
)) {
227 usleep_range(MADERA_BOOT_POLL_INTERVAL_USEC
/ 2,
228 MADERA_BOOT_POLL_INTERVAL_USEC
);
229 regmap_read(madera
->regmap
, MADERA_IRQ1_RAW_STATUS_1
, &val
);
232 if (!(val
& MADERA_BOOT_DONE_STS1
)) {
233 dev_err(madera
->dev
, "Polling BOOT_DONE_STS timed out\n");
240 static int madera_wait_for_boot(struct madera
*madera
)
242 int ret
= madera_wait_for_boot_noack(madera
);
245 * BOOT_DONE defaults to unmasked on boot so we must ack it.
246 * Do this even after a timeout to avoid interrupt storms.
248 regmap_write(madera
->regmap
, MADERA_IRQ1_STATUS_1
,
249 MADERA_BOOT_DONE_EINT1
);
251 pm_runtime_mark_last_busy(madera
->dev
);
256 static int madera_soft_reset(struct madera
*madera
)
260 ret
= regmap_write(madera
->regmap
, MADERA_SOFTWARE_RESET
, 0);
262 dev_err(madera
->dev
, "Failed to soft reset device: %d\n", ret
);
266 /* Allow time for internal clocks to startup after reset */
267 usleep_range(MADERA_RESET_MIN_US
, MADERA_RESET_MAX_US
);
272 static void madera_enable_hard_reset(struct madera
*madera
)
275 * There are many existing out-of-tree users of these codecs that we
276 * can't break so preserve the expected behaviour of setting the line
277 * low to assert reset.
279 gpiod_set_raw_value_cansleep(madera
->pdata
.reset
, 0);
282 static void madera_disable_hard_reset(struct madera
*madera
)
284 gpiod_set_raw_value_cansleep(madera
->pdata
.reset
, 1);
286 usleep_range(MADERA_RESET_MIN_US
, MADERA_RESET_MAX_US
);
289 static int __maybe_unused
madera_runtime_resume(struct device
*dev
)
291 struct madera
*madera
= dev_get_drvdata(dev
);
294 dev_dbg(dev
, "Leaving sleep mode\n");
296 if (!madera
->reset_errata
)
297 madera_enable_hard_reset(madera
);
299 ret
= regulator_enable(madera
->dcvdd
);
301 dev_err(dev
, "Failed to enable DCVDD: %d\n", ret
);
305 regcache_cache_only(madera
->regmap
, false);
306 regcache_cache_only(madera
->regmap_32bit
, false);
308 if (madera
->reset_errata
)
309 usleep_range(ERRATA_DCVDD_MIN_US
, ERRATA_DCVDD_MAX_US
);
311 madera_disable_hard_reset(madera
);
313 if (!madera
->pdata
.reset
|| madera
->reset_errata
) {
314 ret
= madera_wait_for_boot(madera
);
318 ret
= madera_soft_reset(madera
);
320 dev_err(dev
, "Failed to reset: %d\n", ret
);
325 ret
= madera_wait_for_boot(madera
);
329 ret
= regcache_sync(madera
->regmap
);
331 dev_err(dev
, "Failed to restore 16-bit register cache\n");
335 ret
= regcache_sync(madera
->regmap_32bit
);
337 dev_err(dev
, "Failed to restore 32-bit register cache\n");
344 regcache_cache_only(madera
->regmap_32bit
, true);
345 regcache_cache_only(madera
->regmap
, true);
346 regulator_disable(madera
->dcvdd
);
351 static int __maybe_unused
madera_runtime_suspend(struct device
*dev
)
353 struct madera
*madera
= dev_get_drvdata(dev
);
355 dev_dbg(madera
->dev
, "Entering sleep mode\n");
357 regcache_cache_only(madera
->regmap
, true);
358 regcache_mark_dirty(madera
->regmap
);
359 regcache_cache_only(madera
->regmap_32bit
, true);
360 regcache_mark_dirty(madera
->regmap_32bit
);
362 regulator_disable(madera
->dcvdd
);
367 const struct dev_pm_ops madera_pm_ops
= {
368 SET_RUNTIME_PM_OPS(madera_runtime_suspend
,
369 madera_runtime_resume
,
372 EXPORT_SYMBOL_GPL(madera_pm_ops
);
374 const struct of_device_id madera_of_match
[] = {
375 { .compatible
= "cirrus,cs47l15", .data
= (void *)CS47L15
},
376 { .compatible
= "cirrus,cs47l35", .data
= (void *)CS47L35
},
377 { .compatible
= "cirrus,cs47l85", .data
= (void *)CS47L85
},
378 { .compatible
= "cirrus,cs47l90", .data
= (void *)CS47L90
},
379 { .compatible
= "cirrus,cs47l91", .data
= (void *)CS47L91
},
380 { .compatible
= "cirrus,cs42l92", .data
= (void *)CS42L92
},
381 { .compatible
= "cirrus,cs47l92", .data
= (void *)CS47L92
},
382 { .compatible
= "cirrus,cs47l93", .data
= (void *)CS47L93
},
383 { .compatible
= "cirrus,wm1840", .data
= (void *)WM1840
},
386 MODULE_DEVICE_TABLE(of
, madera_of_match
);
387 EXPORT_SYMBOL_GPL(madera_of_match
);
389 static int madera_get_reset_gpio(struct madera
*madera
)
391 struct gpio_desc
*reset
;
393 if (madera
->pdata
.reset
)
396 reset
= devm_gpiod_get_optional(madera
->dev
, "reset", GPIOD_OUT_LOW
);
398 return dev_err_probe(madera
->dev
, PTR_ERR(reset
),
399 "Failed to request /RESET");
402 * A hard reset is needed for full reset of the chip. We allow running
403 * without hard reset only because it can be useful for early
404 * prototyping and some debugging, but we need to warn it's not ideal.
407 dev_warn(madera
->dev
,
408 "Running without reset GPIO is not recommended\n");
410 madera
->pdata
.reset
= reset
;
415 static void madera_set_micbias_info(struct madera
*madera
)
418 * num_childbias is an array because future codecs can have different
419 * childbiases for each micbias. Unspecified values default to 0.
421 switch (madera
->type
) {
423 madera
->num_micbias
= 1;
424 madera
->num_childbias
[0] = 3;
427 madera
->num_micbias
= 2;
428 madera
->num_childbias
[0] = 2;
429 madera
->num_childbias
[1] = 2;
433 madera
->num_micbias
= 4;
434 /* no child biases */
438 madera
->num_micbias
= 2;
439 madera
->num_childbias
[0] = 4;
440 madera
->num_childbias
[1] = 4;
445 madera
->num_micbias
= 2;
446 madera
->num_childbias
[0] = 4;
447 madera
->num_childbias
[1] = 2;
454 int madera_dev_init(struct madera
*madera
)
456 struct device
*dev
= madera
->dev
;
458 int (*patch_fn
)(struct madera
*) = NULL
;
459 const struct mfd_cell
*mfd_devs
;
463 dev_set_drvdata(madera
->dev
, madera
);
464 BLOCKING_INIT_NOTIFIER_HEAD(&madera
->notifier
);
465 mutex_init(&madera
->dapm_ptr_lock
);
467 madera_set_micbias_info(madera
);
470 * We need writable hw config info that all children can share.
471 * Simplest to take one shared copy of pdata struct.
473 if (dev_get_platdata(madera
->dev
)) {
474 memcpy(&madera
->pdata
, dev_get_platdata(madera
->dev
),
475 sizeof(madera
->pdata
));
478 madera
->mclk
[MADERA_MCLK1
].id
= "mclk1";
479 madera
->mclk
[MADERA_MCLK2
].id
= "mclk2";
480 madera
->mclk
[MADERA_MCLK3
].id
= "mclk3";
482 ret
= devm_clk_bulk_get_optional(madera
->dev
, ARRAY_SIZE(madera
->mclk
),
485 dev_err(madera
->dev
, "Failed to get clocks: %d\n", ret
);
489 /* Not using devm_clk_get to prevent breakage of existing DTs */
490 if (!madera
->mclk
[MADERA_MCLK2
].clk
)
491 dev_warn(madera
->dev
, "Missing MCLK2, requires 32kHz clock\n");
493 ret
= madera_get_reset_gpio(madera
);
497 regcache_cache_only(madera
->regmap
, true);
498 regcache_cache_only(madera
->regmap_32bit
, true);
500 for (i
= 0; i
< ARRAY_SIZE(madera_core_supplies
); i
++)
501 madera
->core_supplies
[i
].supply
= madera_core_supplies
[i
];
503 madera
->num_core_supplies
= ARRAY_SIZE(madera_core_supplies
);
506 * On some codecs DCVDD could be supplied by the internal LDO1.
507 * For those we must add the LDO1 driver before requesting DCVDD
508 * No devm_ because we need to control shutdown order of children.
510 switch (madera
->type
) {
512 madera
->reset_errata
= true;
523 ret
= mfd_add_devices(madera
->dev
, PLATFORM_DEVID_NONE
,
525 ARRAY_SIZE(madera_ldo1_devs
),
528 dev_err(dev
, "Failed to add LDO1 child: %d\n", ret
);
533 /* No point continuing if the type is unknown */
534 dev_err(madera
->dev
, "Unknown device type %d\n", madera
->type
);
538 ret
= devm_regulator_bulk_get(dev
, madera
->num_core_supplies
,
539 madera
->core_supplies
);
541 dev_err(dev
, "Failed to request core supplies: %d\n", ret
);
546 * Don't use devres here. If the regulator is one of our children it
547 * will already have been removed before devres cleanup on this mfd
548 * driver tries to call put() on it. We need control of shutdown order.
550 madera
->dcvdd
= regulator_get(madera
->dev
, "DCVDD");
551 if (IS_ERR(madera
->dcvdd
)) {
552 ret
= PTR_ERR(madera
->dcvdd
);
553 dev_err(dev
, "Failed to request DCVDD: %d\n", ret
);
557 ret
= regulator_bulk_enable(madera
->num_core_supplies
,
558 madera
->core_supplies
);
560 dev_err(dev
, "Failed to enable core supplies: %d\n", ret
);
564 if (madera
->reset_errata
)
565 madera_disable_hard_reset(madera
);
567 ret
= regulator_enable(madera
->dcvdd
);
569 dev_err(dev
, "Failed to enable DCVDD: %d\n", ret
);
573 if (madera
->reset_errata
)
574 usleep_range(ERRATA_DCVDD_MIN_US
, ERRATA_DCVDD_MAX_US
);
576 madera_disable_hard_reset(madera
);
578 regcache_cache_only(madera
->regmap
, false);
579 regcache_cache_only(madera
->regmap_32bit
, false);
581 ret
= madera_wait_for_boot_noack(madera
);
583 dev_err(madera
->dev
, "Device failed initial boot: %d\n", ret
);
588 * Now we can power up and verify that this is a chip we know about
589 * before we start doing any writes to its registers.
591 ret
= regmap_read(madera
->regmap
, MADERA_SOFTWARE_RESET
, &hwid
);
593 dev_err(dev
, "Failed to read ID register: %d\n", ret
);
598 case CS47L15_SILICON_ID
:
599 if (IS_ENABLED(CONFIG_MFD_CS47L15
)) {
600 switch (madera
->type
) {
602 patch_fn
= &cs47l15_patch
;
603 mfd_devs
= cs47l15_devs
;
604 n_devs
= ARRAY_SIZE(cs47l15_devs
);
611 case CS47L35_SILICON_ID
:
612 if (IS_ENABLED(CONFIG_MFD_CS47L35
)) {
613 switch (madera
->type
) {
615 patch_fn
= cs47l35_patch
;
616 mfd_devs
= cs47l35_devs
;
617 n_devs
= ARRAY_SIZE(cs47l35_devs
);
624 case CS47L85_SILICON_ID
:
625 if (IS_ENABLED(CONFIG_MFD_CS47L85
)) {
626 switch (madera
->type
) {
629 patch_fn
= cs47l85_patch
;
630 mfd_devs
= cs47l85_devs
;
631 n_devs
= ARRAY_SIZE(cs47l85_devs
);
638 case CS47L90_SILICON_ID
:
639 if (IS_ENABLED(CONFIG_MFD_CS47L90
)) {
640 switch (madera
->type
) {
643 patch_fn
= cs47l90_patch
;
644 mfd_devs
= cs47l90_devs
;
645 n_devs
= ARRAY_SIZE(cs47l90_devs
);
652 case CS47L92_SILICON_ID
:
653 if (IS_ENABLED(CONFIG_MFD_CS47L92
)) {
654 switch (madera
->type
) {
658 patch_fn
= cs47l92_patch
;
659 mfd_devs
= cs47l92_devs
;
660 n_devs
= ARRAY_SIZE(cs47l92_devs
);
668 dev_err(madera
->dev
, "Unknown device ID: %x\n", hwid
);
674 dev_err(madera
->dev
, "Device ID 0x%x not a %s\n", hwid
,
681 * It looks like a device we support. If we don't have a hard reset
682 * we can now attempt a soft reset.
684 if (!madera
->pdata
.reset
|| madera
->reset_errata
) {
685 ret
= madera_soft_reset(madera
);
690 ret
= madera_wait_for_boot(madera
);
692 dev_err(madera
->dev
, "Failed to clear boot done: %d\n", ret
);
696 ret
= regmap_read(madera
->regmap
, MADERA_HARDWARE_REVISION
,
699 dev_err(dev
, "Failed to read revision register: %d\n", ret
);
702 madera
->rev
&= MADERA_HW_REVISION_MASK
;
704 dev_info(dev
, "%s silicon revision %d\n", madera
->type_name
,
707 /* Apply hardware patch */
709 ret
= patch_fn(madera
);
711 dev_err(madera
->dev
, "Failed to apply patch %d\n", ret
);
716 /* Init 32k clock sourced from MCLK2 */
717 ret
= clk_prepare_enable(madera
->mclk
[MADERA_MCLK2
].clk
);
719 dev_err(madera
->dev
, "Failed to enable 32k clock: %d\n", ret
);
723 ret
= regmap_update_bits(madera
->regmap
,
725 MADERA_CLK_32K_ENA_MASK
| MADERA_CLK_32K_SRC_MASK
,
726 MADERA_CLK_32K_ENA
| MADERA_32KZ_MCLK2
);
728 dev_err(madera
->dev
, "Failed to init 32k clock: %d\n", ret
);
732 pm_runtime_set_active(madera
->dev
);
733 pm_runtime_enable(madera
->dev
);
734 pm_runtime_set_autosuspend_delay(madera
->dev
, 100);
735 pm_runtime_use_autosuspend(madera
->dev
);
737 /* No devm_ because we need to control shutdown order of children */
738 ret
= mfd_add_devices(madera
->dev
, PLATFORM_DEVID_NONE
,
742 dev_err(madera
->dev
, "Failed to add subdevices: %d\n", ret
);
749 pm_runtime_disable(madera
->dev
);
751 clk_disable_unprepare(madera
->mclk
[MADERA_MCLK2
].clk
);
753 madera_enable_hard_reset(madera
);
754 regulator_disable(madera
->dcvdd
);
756 regulator_bulk_disable(madera
->num_core_supplies
,
757 madera
->core_supplies
);
759 regulator_put(madera
->dcvdd
);
761 mfd_remove_devices(dev
);
765 EXPORT_SYMBOL_GPL(madera_dev_init
);
767 int madera_dev_exit(struct madera
*madera
)
769 /* Prevent any IRQs being serviced while we clean up */
770 disable_irq(madera
->irq
);
772 pm_runtime_get_sync(madera
->dev
);
774 mfd_remove_devices(madera
->dev
);
776 pm_runtime_disable(madera
->dev
);
778 regulator_disable(madera
->dcvdd
);
779 regulator_put(madera
->dcvdd
);
781 mfd_remove_devices_late(madera
->dev
);
783 pm_runtime_set_suspended(madera
->dev
);
784 pm_runtime_put_noidle(madera
->dev
);
786 clk_disable_unprepare(madera
->mclk
[MADERA_MCLK2
].clk
);
788 madera_enable_hard_reset(madera
);
790 regulator_bulk_disable(madera
->num_core_supplies
,
791 madera
->core_supplies
);
794 EXPORT_SYMBOL_GPL(madera_dev_exit
);
796 MODULE_DESCRIPTION("Madera core MFD driver");
797 MODULE_AUTHOR("Richard Fitzgerald <rf@opensource.cirrus.com>");
798 MODULE_LICENSE("GPL v2");