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.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/of_gpio.h>
18 #include <linux/platform_device.h>
19 #include <linux/pm_runtime.h>
20 #include <linux/regmap.h>
21 #include <linux/regulator/consumer.h>
22 #include <linux/regulator/machine.h>
23 #include <linux/regulator/of_regulator.h>
25 #include <linux/mfd/madera/core.h>
26 #include <linux/mfd/madera/registers.h>
30 #define CS47L15_SILICON_ID 0x6370
31 #define CS47L35_SILICON_ID 0x6360
32 #define CS47L85_SILICON_ID 0x6338
33 #define CS47L90_SILICON_ID 0x6364
34 #define CS47L92_SILICON_ID 0x6371
36 #define MADERA_32KZ_MCLK2 1
38 #define MADERA_RESET_MIN_US 2000
39 #define MADERA_RESET_MAX_US 3000
41 #define ERRATA_DCVDD_MIN_US 10000
42 #define ERRATA_DCVDD_MAX_US 15000
44 static const char * const madera_core_supplies
[] = {
49 static const struct mfd_cell madera_ldo1_devs
[] = {
51 .name
= "madera-ldo1",
52 .level
= MFD_DEP_LEVEL_HIGH
,
56 static const char * const cs47l15_supplies
[] = {
62 static const struct mfd_cell cs47l15_devs
[] = {
63 { .name
= "madera-pinctrl", },
64 { .name
= "madera-irq", },
65 { .name
= "madera-gpio", },
67 .name
= "madera-extcon",
68 .parent_supplies
= cs47l15_supplies
,
69 .num_parent_supplies
= 1, /* We only need MICVDD */
72 .name
= "cs47l15-codec",
73 .parent_supplies
= cs47l15_supplies
,
74 .num_parent_supplies
= ARRAY_SIZE(cs47l15_supplies
),
78 static const char * const cs47l35_supplies
[] = {
86 static const struct mfd_cell cs47l35_devs
[] = {
87 { .name
= "madera-pinctrl", },
88 { .name
= "madera-irq", },
89 { .name
= "madera-micsupp", },
90 { .name
= "madera-gpio", },
92 .name
= "madera-extcon",
93 .parent_supplies
= cs47l35_supplies
,
94 .num_parent_supplies
= 1, /* We only need MICVDD */
97 .name
= "cs47l35-codec",
98 .parent_supplies
= cs47l35_supplies
,
99 .num_parent_supplies
= ARRAY_SIZE(cs47l35_supplies
),
103 static const char * const cs47l85_supplies
[] = {
114 static const struct mfd_cell cs47l85_devs
[] = {
115 { .name
= "madera-pinctrl", },
116 { .name
= "madera-irq", },
117 { .name
= "madera-micsupp", },
118 { .name
= "madera-gpio", },
120 .name
= "madera-extcon",
121 .parent_supplies
= cs47l85_supplies
,
122 .num_parent_supplies
= 1, /* We only need MICVDD */
125 .name
= "cs47l85-codec",
126 .parent_supplies
= cs47l85_supplies
,
127 .num_parent_supplies
= ARRAY_SIZE(cs47l85_supplies
),
131 static const char * const cs47l90_supplies
[] = {
140 static const struct mfd_cell cs47l90_devs
[] = {
141 { .name
= "madera-pinctrl", },
142 { .name
= "madera-irq", },
143 { .name
= "madera-micsupp", },
144 { .name
= "madera-gpio", },
146 .name
= "madera-extcon",
147 .parent_supplies
= cs47l90_supplies
,
148 .num_parent_supplies
= 1, /* We only need MICVDD */
151 .name
= "cs47l90-codec",
152 .parent_supplies
= cs47l90_supplies
,
153 .num_parent_supplies
= ARRAY_SIZE(cs47l90_supplies
),
157 static const char * const cs47l92_supplies
[] = {
163 static const struct mfd_cell cs47l92_devs
[] = {
164 { .name
= "madera-pinctrl", },
165 { .name
= "madera-irq", },
166 { .name
= "madera-micsupp", },
167 { .name
= "madera-gpio", },
169 .name
= "madera-extcon",
170 .parent_supplies
= cs47l92_supplies
,
171 .num_parent_supplies
= 1, /* We only need MICVDD */
174 .name
= "cs47l92-codec",
175 .parent_supplies
= cs47l92_supplies
,
176 .num_parent_supplies
= ARRAY_SIZE(cs47l92_supplies
),
180 /* Used by madera-i2c and madera-spi drivers */
181 const char *madera_name_from_type(enum madera_type type
)
206 EXPORT_SYMBOL_GPL(madera_name_from_type
);
208 #define MADERA_BOOT_POLL_INTERVAL_USEC 5000
209 #define MADERA_BOOT_POLL_TIMEOUT_USEC 25000
211 static int madera_wait_for_boot_noack(struct madera
*madera
)
214 unsigned int val
= 0;
218 * We can't use an interrupt as we need to runtime resume to do so,
219 * so we poll the status bit. This won't race with the interrupt
220 * handler because it will be blocked on runtime resume.
221 * The chip could NAK a read request while it is booting so ignore
222 * errors from regmap_read.
224 timeout
= ktime_add_us(ktime_get(), MADERA_BOOT_POLL_TIMEOUT_USEC
);
225 regmap_read(madera
->regmap
, MADERA_IRQ1_RAW_STATUS_1
, &val
);
226 while (!(val
& MADERA_BOOT_DONE_STS1
) &&
227 !ktime_after(ktime_get(), timeout
)) {
228 usleep_range(MADERA_BOOT_POLL_INTERVAL_USEC
/ 2,
229 MADERA_BOOT_POLL_INTERVAL_USEC
);
230 regmap_read(madera
->regmap
, MADERA_IRQ1_RAW_STATUS_1
, &val
);
233 if (!(val
& MADERA_BOOT_DONE_STS1
)) {
234 dev_err(madera
->dev
, "Polling BOOT_DONE_STS timed out\n");
241 static int madera_wait_for_boot(struct madera
*madera
)
243 int ret
= madera_wait_for_boot_noack(madera
);
246 * BOOT_DONE defaults to unmasked on boot so we must ack it.
247 * Do this even after a timeout to avoid interrupt storms.
249 regmap_write(madera
->regmap
, MADERA_IRQ1_STATUS_1
,
250 MADERA_BOOT_DONE_EINT1
);
252 pm_runtime_mark_last_busy(madera
->dev
);
257 static int madera_soft_reset(struct madera
*madera
)
261 ret
= regmap_write(madera
->regmap
, MADERA_SOFTWARE_RESET
, 0);
263 dev_err(madera
->dev
, "Failed to soft reset device: %d\n", ret
);
267 /* Allow time for internal clocks to startup after reset */
268 usleep_range(MADERA_RESET_MIN_US
, MADERA_RESET_MAX_US
);
273 static void madera_enable_hard_reset(struct madera
*madera
)
276 * There are many existing out-of-tree users of these codecs that we
277 * can't break so preserve the expected behaviour of setting the line
278 * low to assert reset.
280 gpiod_set_raw_value_cansleep(madera
->pdata
.reset
, 0);
283 static void madera_disable_hard_reset(struct madera
*madera
)
285 gpiod_set_raw_value_cansleep(madera
->pdata
.reset
, 1);
287 usleep_range(MADERA_RESET_MIN_US
, MADERA_RESET_MAX_US
);
290 static int __maybe_unused
madera_runtime_resume(struct device
*dev
)
292 struct madera
*madera
= dev_get_drvdata(dev
);
295 dev_dbg(dev
, "Leaving sleep mode\n");
297 if (!madera
->reset_errata
)
298 madera_enable_hard_reset(madera
);
300 ret
= regulator_enable(madera
->dcvdd
);
302 dev_err(dev
, "Failed to enable DCVDD: %d\n", ret
);
306 regcache_cache_only(madera
->regmap
, false);
307 regcache_cache_only(madera
->regmap_32bit
, false);
309 if (madera
->reset_errata
)
310 usleep_range(ERRATA_DCVDD_MIN_US
, ERRATA_DCVDD_MAX_US
);
312 madera_disable_hard_reset(madera
);
314 if (!madera
->pdata
.reset
|| madera
->reset_errata
) {
315 ret
= madera_wait_for_boot(madera
);
319 ret
= madera_soft_reset(madera
);
321 dev_err(dev
, "Failed to reset: %d\n", ret
);
326 ret
= madera_wait_for_boot(madera
);
330 ret
= regcache_sync(madera
->regmap
);
332 dev_err(dev
, "Failed to restore 16-bit register cache\n");
336 ret
= regcache_sync(madera
->regmap_32bit
);
338 dev_err(dev
, "Failed to restore 32-bit register cache\n");
345 regcache_cache_only(madera
->regmap_32bit
, true);
346 regcache_cache_only(madera
->regmap
, true);
347 regulator_disable(madera
->dcvdd
);
352 static int __maybe_unused
madera_runtime_suspend(struct device
*dev
)
354 struct madera
*madera
= dev_get_drvdata(dev
);
356 dev_dbg(madera
->dev
, "Entering sleep mode\n");
358 regcache_cache_only(madera
->regmap
, true);
359 regcache_mark_dirty(madera
->regmap
);
360 regcache_cache_only(madera
->regmap_32bit
, true);
361 regcache_mark_dirty(madera
->regmap_32bit
);
363 regulator_disable(madera
->dcvdd
);
368 const struct dev_pm_ops madera_pm_ops
= {
369 SET_RUNTIME_PM_OPS(madera_runtime_suspend
,
370 madera_runtime_resume
,
373 EXPORT_SYMBOL_GPL(madera_pm_ops
);
375 const struct of_device_id madera_of_match
[] = {
376 { .compatible
= "cirrus,cs47l15", .data
= (void *)CS47L15
},
377 { .compatible
= "cirrus,cs47l35", .data
= (void *)CS47L35
},
378 { .compatible
= "cirrus,cs47l85", .data
= (void *)CS47L85
},
379 { .compatible
= "cirrus,cs47l90", .data
= (void *)CS47L90
},
380 { .compatible
= "cirrus,cs47l91", .data
= (void *)CS47L91
},
381 { .compatible
= "cirrus,cs42l92", .data
= (void *)CS42L92
},
382 { .compatible
= "cirrus,cs47l92", .data
= (void *)CS47L92
},
383 { .compatible
= "cirrus,cs47l93", .data
= (void *)CS47L93
},
384 { .compatible
= "cirrus,wm1840", .data
= (void *)WM1840
},
387 MODULE_DEVICE_TABLE(of
, madera_of_match
);
388 EXPORT_SYMBOL_GPL(madera_of_match
);
390 static int madera_get_reset_gpio(struct madera
*madera
)
392 struct gpio_desc
*reset
;
394 if (madera
->pdata
.reset
)
397 reset
= devm_gpiod_get_optional(madera
->dev
, "reset", GPIOD_OUT_LOW
);
399 return dev_err_probe(madera
->dev
, PTR_ERR(reset
),
400 "Failed to request /RESET");
403 * A hard reset is needed for full reset of the chip. We allow running
404 * without hard reset only because it can be useful for early
405 * prototyping and some debugging, but we need to warn it's not ideal.
408 dev_warn(madera
->dev
,
409 "Running without reset GPIO is not recommended\n");
411 madera
->pdata
.reset
= reset
;
416 static void madera_set_micbias_info(struct madera
*madera
)
419 * num_childbias is an array because future codecs can have different
420 * childbiases for each micbias. Unspecified values default to 0.
422 switch (madera
->type
) {
424 madera
->num_micbias
= 1;
425 madera
->num_childbias
[0] = 3;
428 madera
->num_micbias
= 2;
429 madera
->num_childbias
[0] = 2;
430 madera
->num_childbias
[1] = 2;
434 madera
->num_micbias
= 4;
435 /* no child biases */
439 madera
->num_micbias
= 2;
440 madera
->num_childbias
[0] = 4;
441 madera
->num_childbias
[1] = 4;
446 madera
->num_micbias
= 2;
447 madera
->num_childbias
[0] = 4;
448 madera
->num_childbias
[1] = 2;
455 int madera_dev_init(struct madera
*madera
)
457 struct device
*dev
= madera
->dev
;
459 int (*patch_fn
)(struct madera
*) = NULL
;
460 const struct mfd_cell
*mfd_devs
;
464 dev_set_drvdata(madera
->dev
, madera
);
465 BLOCKING_INIT_NOTIFIER_HEAD(&madera
->notifier
);
466 mutex_init(&madera
->dapm_ptr_lock
);
468 madera_set_micbias_info(madera
);
471 * We need writable hw config info that all children can share.
472 * Simplest to take one shared copy of pdata struct.
474 if (dev_get_platdata(madera
->dev
)) {
475 memcpy(&madera
->pdata
, dev_get_platdata(madera
->dev
),
476 sizeof(madera
->pdata
));
479 madera
->mclk
[MADERA_MCLK1
].id
= "mclk1";
480 madera
->mclk
[MADERA_MCLK2
].id
= "mclk2";
481 madera
->mclk
[MADERA_MCLK3
].id
= "mclk3";
483 ret
= devm_clk_bulk_get_optional(madera
->dev
, ARRAY_SIZE(madera
->mclk
),
486 dev_err(madera
->dev
, "Failed to get clocks: %d\n", ret
);
490 /* Not using devm_clk_get to prevent breakage of existing DTs */
491 if (!madera
->mclk
[MADERA_MCLK2
].clk
)
492 dev_warn(madera
->dev
, "Missing MCLK2, requires 32kHz clock\n");
494 ret
= madera_get_reset_gpio(madera
);
498 regcache_cache_only(madera
->regmap
, true);
499 regcache_cache_only(madera
->regmap_32bit
, true);
501 for (i
= 0; i
< ARRAY_SIZE(madera_core_supplies
); i
++)
502 madera
->core_supplies
[i
].supply
= madera_core_supplies
[i
];
504 madera
->num_core_supplies
= ARRAY_SIZE(madera_core_supplies
);
507 * On some codecs DCVDD could be supplied by the internal LDO1.
508 * For those we must add the LDO1 driver before requesting DCVDD
509 * No devm_ because we need to control shutdown order of children.
511 switch (madera
->type
) {
513 madera
->reset_errata
= true;
524 ret
= mfd_add_devices(madera
->dev
, PLATFORM_DEVID_NONE
,
526 ARRAY_SIZE(madera_ldo1_devs
),
529 dev_err(dev
, "Failed to add LDO1 child: %d\n", ret
);
534 /* No point continuing if the type is unknown */
535 dev_err(madera
->dev
, "Unknown device type %d\n", madera
->type
);
539 ret
= devm_regulator_bulk_get(dev
, madera
->num_core_supplies
,
540 madera
->core_supplies
);
542 dev_err(dev
, "Failed to request core supplies: %d\n", ret
);
547 * Don't use devres here. If the regulator is one of our children it
548 * will already have been removed before devres cleanup on this mfd
549 * driver tries to call put() on it. We need control of shutdown order.
551 madera
->dcvdd
= regulator_get(madera
->dev
, "DCVDD");
552 if (IS_ERR(madera
->dcvdd
)) {
553 ret
= PTR_ERR(madera
->dcvdd
);
554 dev_err(dev
, "Failed to request DCVDD: %d\n", ret
);
558 ret
= regulator_bulk_enable(madera
->num_core_supplies
,
559 madera
->core_supplies
);
561 dev_err(dev
, "Failed to enable core supplies: %d\n", ret
);
565 if (madera
->reset_errata
)
566 madera_disable_hard_reset(madera
);
568 ret
= regulator_enable(madera
->dcvdd
);
570 dev_err(dev
, "Failed to enable DCVDD: %d\n", ret
);
574 if (madera
->reset_errata
)
575 usleep_range(ERRATA_DCVDD_MIN_US
, ERRATA_DCVDD_MAX_US
);
577 madera_disable_hard_reset(madera
);
579 regcache_cache_only(madera
->regmap
, false);
580 regcache_cache_only(madera
->regmap_32bit
, false);
582 ret
= madera_wait_for_boot_noack(madera
);
584 dev_err(madera
->dev
, "Device failed initial boot: %d\n", ret
);
589 * Now we can power up and verify that this is a chip we know about
590 * before we start doing any writes to its registers.
592 ret
= regmap_read(madera
->regmap
, MADERA_SOFTWARE_RESET
, &hwid
);
594 dev_err(dev
, "Failed to read ID register: %d\n", ret
);
599 case CS47L15_SILICON_ID
:
600 if (IS_ENABLED(CONFIG_MFD_CS47L15
)) {
601 switch (madera
->type
) {
603 patch_fn
= &cs47l15_patch
;
604 mfd_devs
= cs47l15_devs
;
605 n_devs
= ARRAY_SIZE(cs47l15_devs
);
612 case CS47L35_SILICON_ID
:
613 if (IS_ENABLED(CONFIG_MFD_CS47L35
)) {
614 switch (madera
->type
) {
616 patch_fn
= cs47l35_patch
;
617 mfd_devs
= cs47l35_devs
;
618 n_devs
= ARRAY_SIZE(cs47l35_devs
);
625 case CS47L85_SILICON_ID
:
626 if (IS_ENABLED(CONFIG_MFD_CS47L85
)) {
627 switch (madera
->type
) {
630 patch_fn
= cs47l85_patch
;
631 mfd_devs
= cs47l85_devs
;
632 n_devs
= ARRAY_SIZE(cs47l85_devs
);
639 case CS47L90_SILICON_ID
:
640 if (IS_ENABLED(CONFIG_MFD_CS47L90
)) {
641 switch (madera
->type
) {
644 patch_fn
= cs47l90_patch
;
645 mfd_devs
= cs47l90_devs
;
646 n_devs
= ARRAY_SIZE(cs47l90_devs
);
653 case CS47L92_SILICON_ID
:
654 if (IS_ENABLED(CONFIG_MFD_CS47L92
)) {
655 switch (madera
->type
) {
659 patch_fn
= cs47l92_patch
;
660 mfd_devs
= cs47l92_devs
;
661 n_devs
= ARRAY_SIZE(cs47l92_devs
);
669 dev_err(madera
->dev
, "Unknown device ID: %x\n", hwid
);
675 dev_err(madera
->dev
, "Device ID 0x%x not a %s\n", hwid
,
682 * It looks like a device we support. If we don't have a hard reset
683 * we can now attempt a soft reset.
685 if (!madera
->pdata
.reset
|| madera
->reset_errata
) {
686 ret
= madera_soft_reset(madera
);
691 ret
= madera_wait_for_boot(madera
);
693 dev_err(madera
->dev
, "Failed to clear boot done: %d\n", ret
);
697 ret
= regmap_read(madera
->regmap
, MADERA_HARDWARE_REVISION
,
700 dev_err(dev
, "Failed to read revision register: %d\n", ret
);
703 madera
->rev
&= MADERA_HW_REVISION_MASK
;
705 dev_info(dev
, "%s silicon revision %d\n", madera
->type_name
,
708 /* Apply hardware patch */
710 ret
= patch_fn(madera
);
712 dev_err(madera
->dev
, "Failed to apply patch %d\n", ret
);
717 /* Init 32k clock sourced from MCLK2 */
718 ret
= clk_prepare_enable(madera
->mclk
[MADERA_MCLK2
].clk
);
720 dev_err(madera
->dev
, "Failed to enable 32k clock: %d\n", ret
);
724 ret
= regmap_update_bits(madera
->regmap
,
726 MADERA_CLK_32K_ENA_MASK
| MADERA_CLK_32K_SRC_MASK
,
727 MADERA_CLK_32K_ENA
| MADERA_32KZ_MCLK2
);
729 dev_err(madera
->dev
, "Failed to init 32k clock: %d\n", ret
);
733 pm_runtime_set_active(madera
->dev
);
734 pm_runtime_enable(madera
->dev
);
735 pm_runtime_set_autosuspend_delay(madera
->dev
, 100);
736 pm_runtime_use_autosuspend(madera
->dev
);
738 /* No devm_ because we need to control shutdown order of children */
739 ret
= mfd_add_devices(madera
->dev
, PLATFORM_DEVID_NONE
,
743 dev_err(madera
->dev
, "Failed to add subdevices: %d\n", ret
);
750 pm_runtime_disable(madera
->dev
);
752 clk_disable_unprepare(madera
->mclk
[MADERA_MCLK2
].clk
);
754 madera_enable_hard_reset(madera
);
755 regulator_disable(madera
->dcvdd
);
757 regulator_bulk_disable(madera
->num_core_supplies
,
758 madera
->core_supplies
);
760 regulator_put(madera
->dcvdd
);
762 mfd_remove_devices(dev
);
766 EXPORT_SYMBOL_GPL(madera_dev_init
);
768 int madera_dev_exit(struct madera
*madera
)
770 /* Prevent any IRQs being serviced while we clean up */
771 disable_irq(madera
->irq
);
773 pm_runtime_get_sync(madera
->dev
);
775 mfd_remove_devices(madera
->dev
);
777 pm_runtime_disable(madera
->dev
);
779 regulator_disable(madera
->dcvdd
);
780 regulator_put(madera
->dcvdd
);
782 mfd_remove_devices_late(madera
->dev
);
784 pm_runtime_set_suspended(madera
->dev
);
785 pm_runtime_put_noidle(madera
->dev
);
787 clk_disable_unprepare(madera
->mclk
[MADERA_MCLK2
].clk
);
789 madera_enable_hard_reset(madera
);
791 regulator_bulk_disable(madera
->num_core_supplies
,
792 madera
->core_supplies
);
795 EXPORT_SYMBOL_GPL(madera_dev_exit
);
797 MODULE_DESCRIPTION("Madera core MFD driver");
798 MODULE_AUTHOR("Richard Fitzgerald <rf@opensource.cirrus.com>");
799 MODULE_LICENSE("GPL v2");