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 static const char * const madera_core_supplies
[] = {
46 static const struct mfd_cell madera_ldo1_devs
[] = {
47 { .name
= "madera-ldo1" },
50 static const char * const cs47l15_supplies
[] = {
56 static const struct mfd_cell cs47l15_devs
[] = {
57 { .name
= "madera-pinctrl", },
58 { .name
= "madera-irq" },
59 { .name
= "madera-gpio" },
61 .name
= "madera-extcon",
62 .parent_supplies
= cs47l15_supplies
,
63 .num_parent_supplies
= 1, /* We only need MICVDD */
66 .name
= "cs47l15-codec",
67 .parent_supplies
= cs47l15_supplies
,
68 .num_parent_supplies
= ARRAY_SIZE(cs47l15_supplies
),
72 static const char * const cs47l35_supplies
[] = {
80 static const struct mfd_cell cs47l35_devs
[] = {
81 { .name
= "madera-pinctrl", },
82 { .name
= "madera-irq", },
83 { .name
= "madera-micsupp", },
84 { .name
= "madera-gpio", },
86 .name
= "madera-extcon",
87 .parent_supplies
= cs47l35_supplies
,
88 .num_parent_supplies
= 1, /* We only need MICVDD */
91 .name
= "cs47l35-codec",
92 .parent_supplies
= cs47l35_supplies
,
93 .num_parent_supplies
= ARRAY_SIZE(cs47l35_supplies
),
97 static const char * const cs47l85_supplies
[] = {
108 static const struct mfd_cell cs47l85_devs
[] = {
109 { .name
= "madera-pinctrl", },
110 { .name
= "madera-irq", },
111 { .name
= "madera-micsupp" },
112 { .name
= "madera-gpio", },
114 .name
= "madera-extcon",
115 .parent_supplies
= cs47l85_supplies
,
116 .num_parent_supplies
= 1, /* We only need MICVDD */
119 .name
= "cs47l85-codec",
120 .parent_supplies
= cs47l85_supplies
,
121 .num_parent_supplies
= ARRAY_SIZE(cs47l85_supplies
),
125 static const char * const cs47l90_supplies
[] = {
134 static const struct mfd_cell cs47l90_devs
[] = {
135 { .name
= "madera-pinctrl", },
136 { .name
= "madera-irq", },
137 { .name
= "madera-micsupp", },
138 { .name
= "madera-gpio", },
140 .name
= "madera-extcon",
141 .parent_supplies
= cs47l90_supplies
,
142 .num_parent_supplies
= 1, /* We only need MICVDD */
145 .name
= "cs47l90-codec",
146 .parent_supplies
= cs47l90_supplies
,
147 .num_parent_supplies
= ARRAY_SIZE(cs47l90_supplies
),
151 static const char * const cs47l92_supplies
[] = {
157 static const struct mfd_cell cs47l92_devs
[] = {
158 { .name
= "madera-pinctrl" },
159 { .name
= "madera-irq", },
160 { .name
= "madera-micsupp", },
161 { .name
= "madera-gpio" },
163 .name
= "madera-extcon",
164 .parent_supplies
= cs47l92_supplies
,
165 .num_parent_supplies
= 1, /* We only need MICVDD */
168 .name
= "cs47l92-codec",
169 .parent_supplies
= cs47l92_supplies
,
170 .num_parent_supplies
= ARRAY_SIZE(cs47l92_supplies
),
174 /* Used by madera-i2c and madera-spi drivers */
175 const char *madera_name_from_type(enum madera_type type
)
200 EXPORT_SYMBOL_GPL(madera_name_from_type
);
202 #define MADERA_BOOT_POLL_INTERVAL_USEC 5000
203 #define MADERA_BOOT_POLL_TIMEOUT_USEC 25000
205 static int madera_wait_for_boot_noack(struct madera
*madera
)
208 unsigned int val
= 0;
212 * We can't use an interrupt as we need to runtime resume to do so,
213 * so we poll the status bit. This won't race with the interrupt
214 * handler because it will be blocked on runtime resume.
215 * The chip could NAK a read request while it is booting so ignore
216 * errors from regmap_read.
218 timeout
= ktime_add_us(ktime_get(), MADERA_BOOT_POLL_TIMEOUT_USEC
);
219 regmap_read(madera
->regmap
, MADERA_IRQ1_RAW_STATUS_1
, &val
);
220 while (!(val
& MADERA_BOOT_DONE_STS1
) &&
221 !ktime_after(ktime_get(), timeout
)) {
222 usleep_range(MADERA_BOOT_POLL_INTERVAL_USEC
/ 2,
223 MADERA_BOOT_POLL_INTERVAL_USEC
);
224 regmap_read(madera
->regmap
, MADERA_IRQ1_RAW_STATUS_1
, &val
);
227 if (!(val
& MADERA_BOOT_DONE_STS1
)) {
228 dev_err(madera
->dev
, "Polling BOOT_DONE_STS timed out\n");
235 static int madera_wait_for_boot(struct madera
*madera
)
237 int ret
= madera_wait_for_boot_noack(madera
);
240 * BOOT_DONE defaults to unmasked on boot so we must ack it.
241 * Do this even after a timeout to avoid interrupt storms.
243 regmap_write(madera
->regmap
, MADERA_IRQ1_STATUS_1
,
244 MADERA_BOOT_DONE_EINT1
);
246 pm_runtime_mark_last_busy(madera
->dev
);
251 static int madera_soft_reset(struct madera
*madera
)
255 ret
= regmap_write(madera
->regmap
, MADERA_SOFTWARE_RESET
, 0);
257 dev_err(madera
->dev
, "Failed to soft reset device: %d\n", ret
);
261 /* Allow time for internal clocks to startup after reset */
262 usleep_range(MADERA_RESET_MIN_US
, MADERA_RESET_MAX_US
);
267 static void madera_enable_hard_reset(struct madera
*madera
)
270 * There are many existing out-of-tree users of these codecs that we
271 * can't break so preserve the expected behaviour of setting the line
272 * low to assert reset.
274 gpiod_set_raw_value_cansleep(madera
->pdata
.reset
, 0);
277 static void madera_disable_hard_reset(struct madera
*madera
)
279 gpiod_set_raw_value_cansleep(madera
->pdata
.reset
, 1);
281 usleep_range(MADERA_RESET_MIN_US
, MADERA_RESET_MAX_US
);
284 static int __maybe_unused
madera_runtime_resume(struct device
*dev
)
286 struct madera
*madera
= dev_get_drvdata(dev
);
289 dev_dbg(dev
, "Leaving sleep mode\n");
291 ret
= regulator_enable(madera
->dcvdd
);
293 dev_err(dev
, "Failed to enable DCVDD: %d\n", ret
);
297 regcache_cache_only(madera
->regmap
, false);
298 regcache_cache_only(madera
->regmap_32bit
, false);
300 usleep_range(MADERA_RESET_MIN_US
, MADERA_RESET_MAX_US
);
302 ret
= madera_wait_for_boot(madera
);
306 ret
= regcache_sync(madera
->regmap
);
308 dev_err(dev
, "Failed to restore 16-bit register cache\n");
312 ret
= regcache_sync(madera
->regmap_32bit
);
314 dev_err(dev
, "Failed to restore 32-bit register cache\n");
321 regcache_cache_only(madera
->regmap_32bit
, true);
322 regcache_cache_only(madera
->regmap
, true);
323 regulator_disable(madera
->dcvdd
);
328 static int __maybe_unused
madera_runtime_suspend(struct device
*dev
)
330 struct madera
*madera
= dev_get_drvdata(dev
);
332 dev_dbg(madera
->dev
, "Entering sleep mode\n");
334 regcache_cache_only(madera
->regmap
, true);
335 regcache_mark_dirty(madera
->regmap
);
336 regcache_cache_only(madera
->regmap_32bit
, true);
337 regcache_mark_dirty(madera
->regmap_32bit
);
339 regulator_disable(madera
->dcvdd
);
344 const struct dev_pm_ops madera_pm_ops
= {
345 SET_RUNTIME_PM_OPS(madera_runtime_suspend
,
346 madera_runtime_resume
,
349 EXPORT_SYMBOL_GPL(madera_pm_ops
);
351 const struct of_device_id madera_of_match
[] = {
352 { .compatible
= "cirrus,cs47l15", .data
= (void *)CS47L15
},
353 { .compatible
= "cirrus,cs47l35", .data
= (void *)CS47L35
},
354 { .compatible
= "cirrus,cs47l85", .data
= (void *)CS47L85
},
355 { .compatible
= "cirrus,cs47l90", .data
= (void *)CS47L90
},
356 { .compatible
= "cirrus,cs47l91", .data
= (void *)CS47L91
},
357 { .compatible
= "cirrus,cs42l92", .data
= (void *)CS42L92
},
358 { .compatible
= "cirrus,cs47l92", .data
= (void *)CS47L92
},
359 { .compatible
= "cirrus,cs47l93", .data
= (void *)CS47L93
},
360 { .compatible
= "cirrus,wm1840", .data
= (void *)WM1840
},
363 MODULE_DEVICE_TABLE(of
, madera_of_match
);
364 EXPORT_SYMBOL_GPL(madera_of_match
);
366 static int madera_get_reset_gpio(struct madera
*madera
)
368 struct gpio_desc
*reset
;
371 if (madera
->pdata
.reset
)
374 reset
= devm_gpiod_get_optional(madera
->dev
, "reset", GPIOD_OUT_LOW
);
376 ret
= PTR_ERR(reset
);
377 if (ret
!= -EPROBE_DEFER
)
378 dev_err(madera
->dev
, "Failed to request /RESET: %d\n",
384 * A hard reset is needed for full reset of the chip. We allow running
385 * without hard reset only because it can be useful for early
386 * prototyping and some debugging, but we need to warn it's not ideal.
389 dev_warn(madera
->dev
,
390 "Running without reset GPIO is not recommended\n");
392 madera
->pdata
.reset
= reset
;
397 static void madera_set_micbias_info(struct madera
*madera
)
400 * num_childbias is an array because future codecs can have different
401 * childbiases for each micbias. Unspecified values default to 0.
403 switch (madera
->type
) {
405 madera
->num_micbias
= 1;
406 madera
->num_childbias
[0] = 3;
409 madera
->num_micbias
= 2;
410 madera
->num_childbias
[0] = 2;
411 madera
->num_childbias
[1] = 2;
415 madera
->num_micbias
= 4;
416 /* no child biases */
420 madera
->num_micbias
= 2;
421 madera
->num_childbias
[0] = 4;
422 madera
->num_childbias
[1] = 4;
427 madera
->num_micbias
= 2;
428 madera
->num_childbias
[0] = 4;
429 madera
->num_childbias
[1] = 2;
436 int madera_dev_init(struct madera
*madera
)
438 struct device
*dev
= madera
->dev
;
440 int (*patch_fn
)(struct madera
*) = NULL
;
441 const struct mfd_cell
*mfd_devs
;
445 dev_set_drvdata(madera
->dev
, madera
);
446 BLOCKING_INIT_NOTIFIER_HEAD(&madera
->notifier
);
447 mutex_init(&madera
->dapm_ptr_lock
);
449 madera_set_micbias_info(madera
);
452 * We need writable hw config info that all children can share.
453 * Simplest to take one shared copy of pdata struct.
455 if (dev_get_platdata(madera
->dev
)) {
456 memcpy(&madera
->pdata
, dev_get_platdata(madera
->dev
),
457 sizeof(madera
->pdata
));
460 madera
->mclk
[MADERA_MCLK1
].id
= "mclk1";
461 madera
->mclk
[MADERA_MCLK2
].id
= "mclk2";
462 madera
->mclk
[MADERA_MCLK3
].id
= "mclk3";
464 ret
= devm_clk_bulk_get_optional(madera
->dev
, ARRAY_SIZE(madera
->mclk
),
467 dev_err(madera
->dev
, "Failed to get clocks: %d\n", ret
);
471 /* Not using devm_clk_get to prevent breakage of existing DTs */
472 if (!madera
->mclk
[MADERA_MCLK2
].clk
)
473 dev_warn(madera
->dev
, "Missing MCLK2, requires 32kHz clock\n");
475 ret
= madera_get_reset_gpio(madera
);
479 regcache_cache_only(madera
->regmap
, true);
480 regcache_cache_only(madera
->regmap_32bit
, true);
482 for (i
= 0; i
< ARRAY_SIZE(madera_core_supplies
); i
++)
483 madera
->core_supplies
[i
].supply
= madera_core_supplies
[i
];
485 madera
->num_core_supplies
= ARRAY_SIZE(madera_core_supplies
);
488 * On some codecs DCVDD could be supplied by the internal LDO1.
489 * For those we must add the LDO1 driver before requesting DCVDD
490 * No devm_ because we need to control shutdown order of children.
492 switch (madera
->type
) {
503 ret
= mfd_add_devices(madera
->dev
, PLATFORM_DEVID_NONE
,
505 ARRAY_SIZE(madera_ldo1_devs
),
508 dev_err(dev
, "Failed to add LDO1 child: %d\n", ret
);
513 /* No point continuing if the type is unknown */
514 dev_err(madera
->dev
, "Unknown device type %d\n", madera
->type
);
518 ret
= devm_regulator_bulk_get(dev
, madera
->num_core_supplies
,
519 madera
->core_supplies
);
521 dev_err(dev
, "Failed to request core supplies: %d\n", ret
);
526 * Don't use devres here. If the regulator is one of our children it
527 * will already have been removed before devres cleanup on this mfd
528 * driver tries to call put() on it. We need control of shutdown order.
530 madera
->dcvdd
= regulator_get(madera
->dev
, "DCVDD");
531 if (IS_ERR(madera
->dcvdd
)) {
532 ret
= PTR_ERR(madera
->dcvdd
);
533 dev_err(dev
, "Failed to request DCVDD: %d\n", ret
);
537 ret
= regulator_bulk_enable(madera
->num_core_supplies
,
538 madera
->core_supplies
);
540 dev_err(dev
, "Failed to enable core supplies: %d\n", ret
);
544 ret
= regulator_enable(madera
->dcvdd
);
546 dev_err(dev
, "Failed to enable DCVDD: %d\n", ret
);
550 madera_disable_hard_reset(madera
);
552 regcache_cache_only(madera
->regmap
, false);
553 regcache_cache_only(madera
->regmap_32bit
, false);
555 ret
= madera_wait_for_boot_noack(madera
);
557 dev_err(madera
->dev
, "Device failed initial boot: %d\n", ret
);
562 * Now we can power up and verify that this is a chip we know about
563 * before we start doing any writes to its registers.
565 ret
= regmap_read(madera
->regmap
, MADERA_SOFTWARE_RESET
, &hwid
);
567 dev_err(dev
, "Failed to read ID register: %d\n", ret
);
572 case CS47L15_SILICON_ID
:
573 if (IS_ENABLED(CONFIG_MFD_CS47L15
)) {
574 switch (madera
->type
) {
576 patch_fn
= &cs47l15_patch
;
577 mfd_devs
= cs47l15_devs
;
578 n_devs
= ARRAY_SIZE(cs47l15_devs
);
585 case CS47L35_SILICON_ID
:
586 if (IS_ENABLED(CONFIG_MFD_CS47L35
)) {
587 switch (madera
->type
) {
589 patch_fn
= cs47l35_patch
;
590 mfd_devs
= cs47l35_devs
;
591 n_devs
= ARRAY_SIZE(cs47l35_devs
);
598 case CS47L85_SILICON_ID
:
599 if (IS_ENABLED(CONFIG_MFD_CS47L85
)) {
600 switch (madera
->type
) {
603 patch_fn
= cs47l85_patch
;
604 mfd_devs
= cs47l85_devs
;
605 n_devs
= ARRAY_SIZE(cs47l85_devs
);
612 case CS47L90_SILICON_ID
:
613 if (IS_ENABLED(CONFIG_MFD_CS47L90
)) {
614 switch (madera
->type
) {
617 patch_fn
= cs47l90_patch
;
618 mfd_devs
= cs47l90_devs
;
619 n_devs
= ARRAY_SIZE(cs47l90_devs
);
626 case CS47L92_SILICON_ID
:
627 if (IS_ENABLED(CONFIG_MFD_CS47L92
)) {
628 switch (madera
->type
) {
632 patch_fn
= cs47l92_patch
;
633 mfd_devs
= cs47l92_devs
;
634 n_devs
= ARRAY_SIZE(cs47l92_devs
);
642 dev_err(madera
->dev
, "Unknown device ID: %x\n", hwid
);
648 dev_err(madera
->dev
, "Device ID 0x%x not a %s\n", hwid
,
655 * It looks like a device we support. If we don't have a hard reset
656 * we can now attempt a soft reset.
658 if (!madera
->pdata
.reset
) {
659 ret
= madera_soft_reset(madera
);
664 ret
= madera_wait_for_boot(madera
);
666 dev_err(madera
->dev
, "Failed to clear boot done: %d\n", ret
);
670 ret
= regmap_read(madera
->regmap
, MADERA_HARDWARE_REVISION
,
673 dev_err(dev
, "Failed to read revision register: %d\n", ret
);
676 madera
->rev
&= MADERA_HW_REVISION_MASK
;
678 dev_info(dev
, "%s silicon revision %d\n", madera
->type_name
,
681 /* Apply hardware patch */
683 ret
= patch_fn(madera
);
685 dev_err(madera
->dev
, "Failed to apply patch %d\n", ret
);
690 /* Init 32k clock sourced from MCLK2 */
691 ret
= clk_prepare_enable(madera
->mclk
[MADERA_MCLK2
].clk
);
693 dev_err(madera
->dev
, "Failed to enable 32k clock: %d\n", ret
);
697 ret
= regmap_update_bits(madera
->regmap
,
699 MADERA_CLK_32K_ENA_MASK
| MADERA_CLK_32K_SRC_MASK
,
700 MADERA_CLK_32K_ENA
| MADERA_32KZ_MCLK2
);
702 dev_err(madera
->dev
, "Failed to init 32k clock: %d\n", ret
);
706 pm_runtime_set_active(madera
->dev
);
707 pm_runtime_enable(madera
->dev
);
708 pm_runtime_set_autosuspend_delay(madera
->dev
, 100);
709 pm_runtime_use_autosuspend(madera
->dev
);
711 /* No devm_ because we need to control shutdown order of children */
712 ret
= mfd_add_devices(madera
->dev
, PLATFORM_DEVID_NONE
,
716 dev_err(madera
->dev
, "Failed to add subdevices: %d\n", ret
);
723 pm_runtime_disable(madera
->dev
);
725 clk_disable_unprepare(madera
->mclk
[MADERA_MCLK2
].clk
);
727 madera_enable_hard_reset(madera
);
728 regulator_disable(madera
->dcvdd
);
730 regulator_bulk_disable(madera
->num_core_supplies
,
731 madera
->core_supplies
);
733 regulator_put(madera
->dcvdd
);
735 mfd_remove_devices(dev
);
739 EXPORT_SYMBOL_GPL(madera_dev_init
);
741 int madera_dev_exit(struct madera
*madera
)
743 /* Prevent any IRQs being serviced while we clean up */
744 disable_irq(madera
->irq
);
747 * DCVDD could be supplied by a child node, we must disable it before
748 * removing the children, and prevent PM runtime from turning it back on
750 pm_runtime_disable(madera
->dev
);
752 clk_disable_unprepare(madera
->mclk
[MADERA_MCLK2
].clk
);
754 regulator_disable(madera
->dcvdd
);
755 regulator_put(madera
->dcvdd
);
757 mfd_remove_devices(madera
->dev
);
758 madera_enable_hard_reset(madera
);
760 regulator_bulk_disable(madera
->num_core_supplies
,
761 madera
->core_supplies
);
764 EXPORT_SYMBOL_GPL(madera_dev_exit
);
766 MODULE_DESCRIPTION("Madera core MFD driver");
767 MODULE_AUTHOR("Richard Fitzgerald <rf@opensource.cirrus.com>");
768 MODULE_LICENSE("GPL v2");