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[] = {
48 .name = "madera-ldo1",
49 .level = MFD_DEP_LEVEL_HIGH,
53 static const char * const cs47l15_supplies[] = {
59 static const struct mfd_cell cs47l15_devs[] = {
60 { .name = "madera-pinctrl", },
61 { .name = "madera-irq", },
62 { .name = "madera-gpio", },
64 .name = "madera-extcon",
65 .parent_supplies = cs47l15_supplies,
66 .num_parent_supplies = 1, /* We only need MICVDD */
69 .name = "cs47l15-codec",
70 .parent_supplies = cs47l15_supplies,
71 .num_parent_supplies = ARRAY_SIZE(cs47l15_supplies),
75 static const char * const cs47l35_supplies[] = {
83 static const struct mfd_cell cs47l35_devs[] = {
84 { .name = "madera-pinctrl", },
85 { .name = "madera-irq", },
86 { .name = "madera-micsupp", },
87 { .name = "madera-gpio", },
89 .name = "madera-extcon",
90 .parent_supplies = cs47l35_supplies,
91 .num_parent_supplies = 1, /* We only need MICVDD */
94 .name = "cs47l35-codec",
95 .parent_supplies = cs47l35_supplies,
96 .num_parent_supplies = ARRAY_SIZE(cs47l35_supplies),
100 static const char * const cs47l85_supplies[] = {
111 static const struct mfd_cell cs47l85_devs[] = {
112 { .name = "madera-pinctrl", },
113 { .name = "madera-irq", },
114 { .name = "madera-micsupp", },
115 { .name = "madera-gpio", },
117 .name = "madera-extcon",
118 .parent_supplies = cs47l85_supplies,
119 .num_parent_supplies = 1, /* We only need MICVDD */
122 .name = "cs47l85-codec",
123 .parent_supplies = cs47l85_supplies,
124 .num_parent_supplies = ARRAY_SIZE(cs47l85_supplies),
128 static const char * const cs47l90_supplies[] = {
137 static const struct mfd_cell cs47l90_devs[] = {
138 { .name = "madera-pinctrl", },
139 { .name = "madera-irq", },
140 { .name = "madera-micsupp", },
141 { .name = "madera-gpio", },
143 .name = "madera-extcon",
144 .parent_supplies = cs47l90_supplies,
145 .num_parent_supplies = 1, /* We only need MICVDD */
148 .name = "cs47l90-codec",
149 .parent_supplies = cs47l90_supplies,
150 .num_parent_supplies = ARRAY_SIZE(cs47l90_supplies),
154 static const char * const cs47l92_supplies[] = {
160 static const struct mfd_cell cs47l92_devs[] = {
161 { .name = "madera-pinctrl", },
162 { .name = "madera-irq", },
163 { .name = "madera-micsupp", },
164 { .name = "madera-gpio", },
166 .name = "madera-extcon",
167 .parent_supplies = cs47l92_supplies,
168 .num_parent_supplies = 1, /* We only need MICVDD */
171 .name = "cs47l92-codec",
172 .parent_supplies = cs47l92_supplies,
173 .num_parent_supplies = ARRAY_SIZE(cs47l92_supplies),
177 /* Used by madera-i2c and madera-spi drivers */
178 const char *madera_name_from_type(enum madera_type type)
203 EXPORT_SYMBOL_GPL(madera_name_from_type);
205 #define MADERA_BOOT_POLL_INTERVAL_USEC 5000
206 #define MADERA_BOOT_POLL_TIMEOUT_USEC 25000
208 static int madera_wait_for_boot_noack(struct madera *madera)
211 unsigned int val = 0;
215 * We can't use an interrupt as we need to runtime resume to do so,
216 * so we poll the status bit. This won't race with the interrupt
217 * handler because it will be blocked on runtime resume.
218 * The chip could NAK a read request while it is booting so ignore
219 * errors from regmap_read.
221 timeout = ktime_add_us(ktime_get(), MADERA_BOOT_POLL_TIMEOUT_USEC);
222 regmap_read(madera->regmap, MADERA_IRQ1_RAW_STATUS_1, &val);
223 while (!(val & MADERA_BOOT_DONE_STS1) &&
224 !ktime_after(ktime_get(), timeout)) {
225 usleep_range(MADERA_BOOT_POLL_INTERVAL_USEC / 2,
226 MADERA_BOOT_POLL_INTERVAL_USEC);
227 regmap_read(madera->regmap, MADERA_IRQ1_RAW_STATUS_1, &val);
230 if (!(val & MADERA_BOOT_DONE_STS1)) {
231 dev_err(madera->dev, "Polling BOOT_DONE_STS timed out\n");
238 static int madera_wait_for_boot(struct madera *madera)
240 int ret = madera_wait_for_boot_noack(madera);
243 * BOOT_DONE defaults to unmasked on boot so we must ack it.
244 * Do this even after a timeout to avoid interrupt storms.
246 regmap_write(madera->regmap, MADERA_IRQ1_STATUS_1,
247 MADERA_BOOT_DONE_EINT1);
249 pm_runtime_mark_last_busy(madera->dev);
254 static int madera_soft_reset(struct madera *madera)
258 ret = regmap_write(madera->regmap, MADERA_SOFTWARE_RESET, 0);
260 dev_err(madera->dev, "Failed to soft reset device: %d\n", ret);
264 /* Allow time for internal clocks to startup after reset */
265 usleep_range(MADERA_RESET_MIN_US, MADERA_RESET_MAX_US);
270 static void madera_enable_hard_reset(struct madera *madera)
273 * There are many existing out-of-tree users of these codecs that we
274 * can't break so preserve the expected behaviour of setting the line
275 * low to assert reset.
277 gpiod_set_raw_value_cansleep(madera->pdata.reset, 0);
280 static void madera_disable_hard_reset(struct madera *madera)
282 gpiod_set_raw_value_cansleep(madera->pdata.reset, 1);
284 usleep_range(MADERA_RESET_MIN_US, MADERA_RESET_MAX_US);
287 static int __maybe_unused madera_runtime_resume(struct device *dev)
289 struct madera *madera = dev_get_drvdata(dev);
292 dev_dbg(dev, "Leaving sleep mode\n");
294 ret = regulator_enable(madera->dcvdd);
296 dev_err(dev, "Failed to enable DCVDD: %d\n", ret);
300 regcache_cache_only(madera->regmap, false);
301 regcache_cache_only(madera->regmap_32bit, false);
303 usleep_range(MADERA_RESET_MIN_US, MADERA_RESET_MAX_US);
305 ret = madera_wait_for_boot(madera);
309 ret = regcache_sync(madera->regmap);
311 dev_err(dev, "Failed to restore 16-bit register cache\n");
315 ret = regcache_sync(madera->regmap_32bit);
317 dev_err(dev, "Failed to restore 32-bit register cache\n");
324 regcache_cache_only(madera->regmap_32bit, true);
325 regcache_cache_only(madera->regmap, true);
326 regulator_disable(madera->dcvdd);
331 static int __maybe_unused madera_runtime_suspend(struct device *dev)
333 struct madera *madera = dev_get_drvdata(dev);
335 dev_dbg(madera->dev, "Entering sleep mode\n");
337 regcache_cache_only(madera->regmap, true);
338 regcache_mark_dirty(madera->regmap);
339 regcache_cache_only(madera->regmap_32bit, true);
340 regcache_mark_dirty(madera->regmap_32bit);
342 regulator_disable(madera->dcvdd);
347 const struct dev_pm_ops madera_pm_ops = {
348 SET_RUNTIME_PM_OPS(madera_runtime_suspend,
349 madera_runtime_resume,
352 EXPORT_SYMBOL_GPL(madera_pm_ops);
354 const struct of_device_id madera_of_match[] = {
355 { .compatible = "cirrus,cs47l15", .data = (void *)CS47L15 },
356 { .compatible = "cirrus,cs47l35", .data = (void *)CS47L35 },
357 { .compatible = "cirrus,cs47l85", .data = (void *)CS47L85 },
358 { .compatible = "cirrus,cs47l90", .data = (void *)CS47L90 },
359 { .compatible = "cirrus,cs47l91", .data = (void *)CS47L91 },
360 { .compatible = "cirrus,cs42l92", .data = (void *)CS42L92 },
361 { .compatible = "cirrus,cs47l92", .data = (void *)CS47L92 },
362 { .compatible = "cirrus,cs47l93", .data = (void *)CS47L93 },
363 { .compatible = "cirrus,wm1840", .data = (void *)WM1840 },
366 MODULE_DEVICE_TABLE(of, madera_of_match);
367 EXPORT_SYMBOL_GPL(madera_of_match);
369 static int madera_get_reset_gpio(struct madera *madera)
371 struct gpio_desc *reset;
373 if (madera->pdata.reset)
376 reset = devm_gpiod_get_optional(madera->dev, "reset", GPIOD_OUT_LOW);
378 return dev_err_probe(madera->dev, PTR_ERR(reset),
379 "Failed to request /RESET");
382 * A hard reset is needed for full reset of the chip. We allow running
383 * without hard reset only because it can be useful for early
384 * prototyping and some debugging, but we need to warn it's not ideal.
387 dev_warn(madera->dev,
388 "Running without reset GPIO is not recommended\n");
390 madera->pdata.reset = reset;
395 static void madera_set_micbias_info(struct madera *madera)
398 * num_childbias is an array because future codecs can have different
399 * childbiases for each micbias. Unspecified values default to 0.
401 switch (madera->type) {
403 madera->num_micbias = 1;
404 madera->num_childbias[0] = 3;
407 madera->num_micbias = 2;
408 madera->num_childbias[0] = 2;
409 madera->num_childbias[1] = 2;
413 madera->num_micbias = 4;
414 /* no child biases */
418 madera->num_micbias = 2;
419 madera->num_childbias[0] = 4;
420 madera->num_childbias[1] = 4;
425 madera->num_micbias = 2;
426 madera->num_childbias[0] = 4;
427 madera->num_childbias[1] = 2;
434 int madera_dev_init(struct madera *madera)
436 struct device *dev = madera->dev;
438 int (*patch_fn)(struct madera *) = NULL;
439 const struct mfd_cell *mfd_devs;
443 dev_set_drvdata(madera->dev, madera);
444 BLOCKING_INIT_NOTIFIER_HEAD(&madera->notifier);
445 mutex_init(&madera->dapm_ptr_lock);
447 madera_set_micbias_info(madera);
450 * We need writable hw config info that all children can share.
451 * Simplest to take one shared copy of pdata struct.
453 if (dev_get_platdata(madera->dev)) {
454 memcpy(&madera->pdata, dev_get_platdata(madera->dev),
455 sizeof(madera->pdata));
458 madera->mclk[MADERA_MCLK1].id = "mclk1";
459 madera->mclk[MADERA_MCLK2].id = "mclk2";
460 madera->mclk[MADERA_MCLK3].id = "mclk3";
462 ret = devm_clk_bulk_get_optional(madera->dev, ARRAY_SIZE(madera->mclk),
465 dev_err(madera->dev, "Failed to get clocks: %d\n", ret);
469 /* Not using devm_clk_get to prevent breakage of existing DTs */
470 if (!madera->mclk[MADERA_MCLK2].clk)
471 dev_warn(madera->dev, "Missing MCLK2, requires 32kHz clock\n");
473 ret = madera_get_reset_gpio(madera);
477 regcache_cache_only(madera->regmap, true);
478 regcache_cache_only(madera->regmap_32bit, true);
480 for (i = 0; i < ARRAY_SIZE(madera_core_supplies); i++)
481 madera->core_supplies[i].supply = madera_core_supplies[i];
483 madera->num_core_supplies = ARRAY_SIZE(madera_core_supplies);
486 * On some codecs DCVDD could be supplied by the internal LDO1.
487 * For those we must add the LDO1 driver before requesting DCVDD
488 * No devm_ because we need to control shutdown order of children.
490 switch (madera->type) {
501 ret = mfd_add_devices(madera->dev, PLATFORM_DEVID_NONE,
503 ARRAY_SIZE(madera_ldo1_devs),
506 dev_err(dev, "Failed to add LDO1 child: %d\n", ret);
511 /* No point continuing if the type is unknown */
512 dev_err(madera->dev, "Unknown device type %d\n", madera->type);
516 ret = devm_regulator_bulk_get(dev, madera->num_core_supplies,
517 madera->core_supplies);
519 dev_err(dev, "Failed to request core supplies: %d\n", ret);
524 * Don't use devres here. If the regulator is one of our children it
525 * will already have been removed before devres cleanup on this mfd
526 * driver tries to call put() on it. We need control of shutdown order.
528 madera->dcvdd = regulator_get(madera->dev, "DCVDD");
529 if (IS_ERR(madera->dcvdd)) {
530 ret = PTR_ERR(madera->dcvdd);
531 dev_err(dev, "Failed to request DCVDD: %d\n", ret);
535 ret = regulator_bulk_enable(madera->num_core_supplies,
536 madera->core_supplies);
538 dev_err(dev, "Failed to enable core supplies: %d\n", ret);
542 ret = regulator_enable(madera->dcvdd);
544 dev_err(dev, "Failed to enable DCVDD: %d\n", ret);
548 madera_disable_hard_reset(madera);
550 regcache_cache_only(madera->regmap, false);
551 regcache_cache_only(madera->regmap_32bit, false);
553 ret = madera_wait_for_boot_noack(madera);
555 dev_err(madera->dev, "Device failed initial boot: %d\n", ret);
560 * Now we can power up and verify that this is a chip we know about
561 * before we start doing any writes to its registers.
563 ret = regmap_read(madera->regmap, MADERA_SOFTWARE_RESET, &hwid);
565 dev_err(dev, "Failed to read ID register: %d\n", ret);
570 case CS47L15_SILICON_ID:
571 if (IS_ENABLED(CONFIG_MFD_CS47L15)) {
572 switch (madera->type) {
574 patch_fn = &cs47l15_patch;
575 mfd_devs = cs47l15_devs;
576 n_devs = ARRAY_SIZE(cs47l15_devs);
583 case CS47L35_SILICON_ID:
584 if (IS_ENABLED(CONFIG_MFD_CS47L35)) {
585 switch (madera->type) {
587 patch_fn = cs47l35_patch;
588 mfd_devs = cs47l35_devs;
589 n_devs = ARRAY_SIZE(cs47l35_devs);
596 case CS47L85_SILICON_ID:
597 if (IS_ENABLED(CONFIG_MFD_CS47L85)) {
598 switch (madera->type) {
601 patch_fn = cs47l85_patch;
602 mfd_devs = cs47l85_devs;
603 n_devs = ARRAY_SIZE(cs47l85_devs);
610 case CS47L90_SILICON_ID:
611 if (IS_ENABLED(CONFIG_MFD_CS47L90)) {
612 switch (madera->type) {
615 patch_fn = cs47l90_patch;
616 mfd_devs = cs47l90_devs;
617 n_devs = ARRAY_SIZE(cs47l90_devs);
624 case CS47L92_SILICON_ID:
625 if (IS_ENABLED(CONFIG_MFD_CS47L92)) {
626 switch (madera->type) {
630 patch_fn = cs47l92_patch;
631 mfd_devs = cs47l92_devs;
632 n_devs = ARRAY_SIZE(cs47l92_devs);
640 dev_err(madera->dev, "Unknown device ID: %x\n", hwid);
646 dev_err(madera->dev, "Device ID 0x%x not a %s\n", hwid,
653 * It looks like a device we support. If we don't have a hard reset
654 * we can now attempt a soft reset.
656 if (!madera->pdata.reset) {
657 ret = madera_soft_reset(madera);
662 ret = madera_wait_for_boot(madera);
664 dev_err(madera->dev, "Failed to clear boot done: %d\n", ret);
668 ret = regmap_read(madera->regmap, MADERA_HARDWARE_REVISION,
671 dev_err(dev, "Failed to read revision register: %d\n", ret);
674 madera->rev &= MADERA_HW_REVISION_MASK;
676 dev_info(dev, "%s silicon revision %d\n", madera->type_name,
679 /* Apply hardware patch */
681 ret = patch_fn(madera);
683 dev_err(madera->dev, "Failed to apply patch %d\n", ret);
688 /* Init 32k clock sourced from MCLK2 */
689 ret = clk_prepare_enable(madera->mclk[MADERA_MCLK2].clk);
691 dev_err(madera->dev, "Failed to enable 32k clock: %d\n", ret);
695 ret = regmap_update_bits(madera->regmap,
697 MADERA_CLK_32K_ENA_MASK | MADERA_CLK_32K_SRC_MASK,
698 MADERA_CLK_32K_ENA | MADERA_32KZ_MCLK2);
700 dev_err(madera->dev, "Failed to init 32k clock: %d\n", ret);
704 pm_runtime_set_active(madera->dev);
705 pm_runtime_enable(madera->dev);
706 pm_runtime_set_autosuspend_delay(madera->dev, 100);
707 pm_runtime_use_autosuspend(madera->dev);
709 /* No devm_ because we need to control shutdown order of children */
710 ret = mfd_add_devices(madera->dev, PLATFORM_DEVID_NONE,
714 dev_err(madera->dev, "Failed to add subdevices: %d\n", ret);
721 pm_runtime_disable(madera->dev);
723 clk_disable_unprepare(madera->mclk[MADERA_MCLK2].clk);
725 madera_enable_hard_reset(madera);
726 regulator_disable(madera->dcvdd);
728 regulator_bulk_disable(madera->num_core_supplies,
729 madera->core_supplies);
731 regulator_put(madera->dcvdd);
733 mfd_remove_devices(dev);
737 EXPORT_SYMBOL_GPL(madera_dev_init);
739 int madera_dev_exit(struct madera *madera)
741 /* Prevent any IRQs being serviced while we clean up */
742 disable_irq(madera->irq);
744 pm_runtime_get_sync(madera->dev);
746 mfd_remove_devices(madera->dev);
748 pm_runtime_disable(madera->dev);
750 regulator_disable(madera->dcvdd);
751 regulator_put(madera->dcvdd);
753 mfd_remove_devices_late(madera->dev);
755 pm_runtime_set_suspended(madera->dev);
756 pm_runtime_put_noidle(madera->dev);
758 clk_disable_unprepare(madera->mclk[MADERA_MCLK2].clk);
760 madera_enable_hard_reset(madera);
762 regulator_bulk_disable(madera->num_core_supplies,
763 madera->core_supplies);
766 EXPORT_SYMBOL_GPL(madera_dev_exit);
768 MODULE_DESCRIPTION("Madera core MFD driver");
769 MODULE_AUTHOR("Richard Fitzgerald <rf@opensource.cirrus.com>");
770 MODULE_LICENSE("GPL v2");