1 // SPDX-License-Identifier: GPL-2.0
3 * Core MFD support for Cirrus Logic Madera codecs
5 * Copyright (C) 2015-2018 Cirrus Logic
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; version 2.
12 #include <linux/device.h>
13 #include <linux/delay.h>
14 #include <linux/err.h>
15 #include <linux/gpio.h>
16 #include <linux/mfd/core.h>
17 #include <linux/module.h>
18 #include <linux/notifier.h>
20 #include <linux/of_gpio.h>
21 #include <linux/platform_device.h>
22 #include <linux/pm_runtime.h>
23 #include <linux/regmap.h>
24 #include <linux/regulator/consumer.h>
25 #include <linux/regulator/machine.h>
26 #include <linux/regulator/of_regulator.h>
28 #include <linux/mfd/madera/core.h>
29 #include <linux/mfd/madera/registers.h>
33 #define CS47L35_SILICON_ID 0x6360
34 #define CS47L85_SILICON_ID 0x6338
35 #define CS47L90_SILICON_ID 0x6364
37 #define MADERA_32KZ_MCLK2 1
39 static const char * const madera_core_supplies
[] = {
44 static const struct mfd_cell madera_ldo1_devs
[] = {
45 { .name
= "madera-ldo1" },
48 static const char * const cs47l35_supplies
[] = {
56 static const struct mfd_cell cs47l35_devs
[] = {
57 { .name
= "madera-pinctrl", },
58 { .name
= "madera-irq", },
59 { .name
= "madera-micsupp", },
60 { .name
= "madera-gpio", },
61 { .name
= "madera-extcon", },
63 .name
= "cs47l35-codec",
64 .parent_supplies
= cs47l35_supplies
,
65 .num_parent_supplies
= ARRAY_SIZE(cs47l35_supplies
),
69 static const char * const cs47l85_supplies
[] = {
80 static const struct mfd_cell cs47l85_devs
[] = {
81 { .name
= "madera-pinctrl", },
82 { .name
= "madera-irq", },
83 { .name
= "madera-micsupp" },
84 { .name
= "madera-gpio", },
85 { .name
= "madera-extcon", },
87 .name
= "cs47l85-codec",
88 .parent_supplies
= cs47l85_supplies
,
89 .num_parent_supplies
= ARRAY_SIZE(cs47l85_supplies
),
93 static const char * const cs47l90_supplies
[] = {
102 static const struct mfd_cell cs47l90_devs
[] = {
103 { .name
= "madera-pinctrl", },
104 { .name
= "madera-irq", },
105 { .name
= "madera-micsupp", },
106 { .name
= "madera-gpio", },
107 { .name
= "madera-extcon", },
109 .name
= "cs47l90-codec",
110 .parent_supplies
= cs47l90_supplies
,
111 .num_parent_supplies
= ARRAY_SIZE(cs47l90_supplies
),
115 /* Used by madera-i2c and madera-spi drivers */
116 const char *madera_name_from_type(enum madera_type type
)
133 EXPORT_SYMBOL_GPL(madera_name_from_type
);
135 #define MADERA_BOOT_POLL_MAX_INTERVAL_US 5000
136 #define MADERA_BOOT_POLL_TIMEOUT_US 25000
138 static int madera_wait_for_boot(struct madera
*madera
)
144 * We can't use an interrupt as we need to runtime resume to do so,
145 * so we poll the status bit. This won't race with the interrupt
146 * handler because it will be blocked on runtime resume.
148 ret
= regmap_read_poll_timeout(madera
->regmap
,
149 MADERA_IRQ1_RAW_STATUS_1
,
151 (val
& MADERA_BOOT_DONE_STS1
),
152 MADERA_BOOT_POLL_MAX_INTERVAL_US
,
153 MADERA_BOOT_POLL_TIMEOUT_US
);
156 dev_err(madera
->dev
, "Polling BOOT_DONE_STS failed: %d\n", ret
);
159 * BOOT_DONE defaults to unmasked on boot so we must ack it.
160 * Do this unconditionally to avoid interrupt storms.
162 regmap_write(madera
->regmap
, MADERA_IRQ1_STATUS_1
,
163 MADERA_BOOT_DONE_EINT1
);
165 pm_runtime_mark_last_busy(madera
->dev
);
170 static int madera_soft_reset(struct madera
*madera
)
174 ret
= regmap_write(madera
->regmap
, MADERA_SOFTWARE_RESET
, 0);
176 dev_err(madera
->dev
, "Failed to soft reset device: %d\n", ret
);
180 /* Allow time for internal clocks to startup after reset */
181 usleep_range(1000, 2000);
186 static void madera_enable_hard_reset(struct madera
*madera
)
188 if (!madera
->pdata
.reset
)
192 * There are many existing out-of-tree users of these codecs that we
193 * can't break so preserve the expected behaviour of setting the line
194 * low to assert reset.
196 gpiod_set_raw_value_cansleep(madera
->pdata
.reset
, 0);
199 static void madera_disable_hard_reset(struct madera
*madera
)
201 if (!madera
->pdata
.reset
)
204 gpiod_set_raw_value_cansleep(madera
->pdata
.reset
, 1);
205 usleep_range(1000, 2000);
208 static int __maybe_unused
madera_runtime_resume(struct device
*dev
)
210 struct madera
*madera
= dev_get_drvdata(dev
);
213 dev_dbg(dev
, "Leaving sleep mode\n");
215 ret
= regulator_enable(madera
->dcvdd
);
217 dev_err(dev
, "Failed to enable DCVDD: %d\n", ret
);
221 regcache_cache_only(madera
->regmap
, false);
222 regcache_cache_only(madera
->regmap_32bit
, false);
224 ret
= madera_wait_for_boot(madera
);
228 ret
= regcache_sync(madera
->regmap
);
230 dev_err(dev
, "Failed to restore 16-bit register cache\n");
234 ret
= regcache_sync(madera
->regmap_32bit
);
236 dev_err(dev
, "Failed to restore 32-bit register cache\n");
243 regcache_cache_only(madera
->regmap_32bit
, true);
244 regcache_cache_only(madera
->regmap
, true);
245 regulator_disable(madera
->dcvdd
);
250 static int __maybe_unused
madera_runtime_suspend(struct device
*dev
)
252 struct madera
*madera
= dev_get_drvdata(dev
);
254 dev_dbg(madera
->dev
, "Entering sleep mode\n");
256 regcache_cache_only(madera
->regmap
, true);
257 regcache_mark_dirty(madera
->regmap
);
258 regcache_cache_only(madera
->regmap_32bit
, true);
259 regcache_mark_dirty(madera
->regmap_32bit
);
261 regulator_disable(madera
->dcvdd
);
266 const struct dev_pm_ops madera_pm_ops
= {
267 SET_RUNTIME_PM_OPS(madera_runtime_suspend
,
268 madera_runtime_resume
,
271 EXPORT_SYMBOL_GPL(madera_pm_ops
);
273 const struct of_device_id madera_of_match
[] = {
274 { .compatible
= "cirrus,cs47l35", .data
= (void *)CS47L35
},
275 { .compatible
= "cirrus,cs47l85", .data
= (void *)CS47L85
},
276 { .compatible
= "cirrus,cs47l90", .data
= (void *)CS47L90
},
277 { .compatible
= "cirrus,cs47l91", .data
= (void *)CS47L91
},
278 { .compatible
= "cirrus,wm1840", .data
= (void *)WM1840
},
281 MODULE_DEVICE_TABLE(of
, madera_of_match
);
282 EXPORT_SYMBOL_GPL(madera_of_match
);
284 static int madera_get_reset_gpio(struct madera
*madera
)
286 struct gpio_desc
*reset
;
289 if (madera
->pdata
.reset
)
292 reset
= devm_gpiod_get_optional(madera
->dev
, "reset", GPIOD_OUT_LOW
);
294 ret
= PTR_ERR(reset
);
295 if (ret
!= -EPROBE_DEFER
)
296 dev_err(madera
->dev
, "Failed to request /RESET: %d\n",
302 * A hard reset is needed for full reset of the chip. We allow running
303 * without hard reset only because it can be useful for early
304 * prototyping and some debugging, but we need to warn it's not ideal.
307 dev_warn(madera
->dev
,
308 "Running without reset GPIO is not recommended\n");
310 madera
->pdata
.reset
= reset
;
315 static void madera_set_micbias_info(struct madera
*madera
)
318 * num_childbias is an array because future codecs can have different
319 * childbiases for each micbias. Unspecified values default to 0.
321 switch (madera
->type
) {
323 madera
->num_micbias
= 2;
324 madera
->num_childbias
[0] = 2;
325 madera
->num_childbias
[1] = 2;
329 madera
->num_micbias
= 4;
330 /* no child biases */
334 madera
->num_micbias
= 2;
335 madera
->num_childbias
[0] = 4;
336 madera
->num_childbias
[1] = 4;
343 int madera_dev_init(struct madera
*madera
)
345 struct device
*dev
= madera
->dev
;
347 int (*patch_fn
)(struct madera
*) = NULL
;
348 const struct mfd_cell
*mfd_devs
;
352 dev_set_drvdata(madera
->dev
, madera
);
353 BLOCKING_INIT_NOTIFIER_HEAD(&madera
->notifier
);
354 madera_set_micbias_info(madera
);
357 * We need writable hw config info that all children can share.
358 * Simplest to take one shared copy of pdata struct.
360 if (dev_get_platdata(madera
->dev
)) {
361 memcpy(&madera
->pdata
, dev_get_platdata(madera
->dev
),
362 sizeof(madera
->pdata
));
365 ret
= madera_get_reset_gpio(madera
);
369 regcache_cache_only(madera
->regmap
, true);
370 regcache_cache_only(madera
->regmap_32bit
, true);
372 for (i
= 0; i
< ARRAY_SIZE(madera_core_supplies
); i
++)
373 madera
->core_supplies
[i
].supply
= madera_core_supplies
[i
];
375 madera
->num_core_supplies
= ARRAY_SIZE(madera_core_supplies
);
378 * On some codecs DCVDD could be supplied by the internal LDO1.
379 * For those we must add the LDO1 driver before requesting DCVDD
380 * No devm_ because we need to control shutdown order of children.
382 switch (madera
->type
) {
389 ret
= mfd_add_devices(madera
->dev
, PLATFORM_DEVID_NONE
,
391 ARRAY_SIZE(madera_ldo1_devs
),
394 dev_err(dev
, "Failed to add LDO1 child: %d\n", ret
);
399 /* No point continuing if the type is unknown */
400 dev_err(madera
->dev
, "Unknown device type %d\n", madera
->type
);
404 ret
= devm_regulator_bulk_get(dev
, madera
->num_core_supplies
,
405 madera
->core_supplies
);
407 dev_err(dev
, "Failed to request core supplies: %d\n", ret
);
412 * Don't use devres here. If the regulator is one of our children it
413 * will already have been removed before devres cleanup on this mfd
414 * driver tries to call put() on it. We need control of shutdown order.
416 madera
->dcvdd
= regulator_get(madera
->dev
, "DCVDD");
417 if (IS_ERR(madera
->dcvdd
)) {
418 ret
= PTR_ERR(madera
->dcvdd
);
419 dev_err(dev
, "Failed to request DCVDD: %d\n", ret
);
423 ret
= regulator_bulk_enable(madera
->num_core_supplies
,
424 madera
->core_supplies
);
426 dev_err(dev
, "Failed to enable core supplies: %d\n", ret
);
430 ret
= regulator_enable(madera
->dcvdd
);
432 dev_err(dev
, "Failed to enable DCVDD: %d\n", ret
);
436 madera_disable_hard_reset(madera
);
438 regcache_cache_only(madera
->regmap
, false);
439 regcache_cache_only(madera
->regmap_32bit
, false);
442 * Now we can power up and verify that this is a chip we know about
443 * before we start doing any writes to its registers.
445 ret
= regmap_read(madera
->regmap
, MADERA_SOFTWARE_RESET
, &hwid
);
447 dev_err(dev
, "Failed to read ID register: %d\n", ret
);
452 case CS47L35_SILICON_ID
:
453 if (IS_ENABLED(CONFIG_MFD_CS47L35
)) {
454 switch (madera
->type
) {
456 patch_fn
= cs47l35_patch
;
457 mfd_devs
= cs47l35_devs
;
458 n_devs
= ARRAY_SIZE(cs47l35_devs
);
465 case CS47L85_SILICON_ID
:
466 if (IS_ENABLED(CONFIG_MFD_CS47L85
)) {
467 switch (madera
->type
) {
470 patch_fn
= cs47l85_patch
;
471 mfd_devs
= cs47l85_devs
;
472 n_devs
= ARRAY_SIZE(cs47l85_devs
);
479 case CS47L90_SILICON_ID
:
480 if (IS_ENABLED(CONFIG_MFD_CS47L90
)) {
481 switch (madera
->type
) {
484 patch_fn
= cs47l90_patch
;
485 mfd_devs
= cs47l90_devs
;
486 n_devs
= ARRAY_SIZE(cs47l90_devs
);
494 dev_err(madera
->dev
, "Unknown device ID: %x\n", hwid
);
500 dev_err(madera
->dev
, "Device ID 0x%x not a %s\n", hwid
,
507 * It looks like a device we support. If we don't have a hard reset
508 * we can now attempt a soft reset.
510 if (!madera
->pdata
.reset
) {
511 ret
= madera_soft_reset(madera
);
516 ret
= madera_wait_for_boot(madera
);
518 dev_err(madera
->dev
, "Device failed initial boot: %d\n", ret
);
522 ret
= regmap_read(madera
->regmap
, MADERA_HARDWARE_REVISION
,
525 dev_err(dev
, "Failed to read revision register: %d\n", ret
);
528 madera
->rev
&= MADERA_HW_REVISION_MASK
;
530 dev_info(dev
, "%s silicon revision %d\n", madera
->type_name
,
533 /* Apply hardware patch */
535 ret
= patch_fn(madera
);
537 dev_err(madera
->dev
, "Failed to apply patch %d\n", ret
);
542 /* Init 32k clock sourced from MCLK2 */
543 ret
= regmap_update_bits(madera
->regmap
,
545 MADERA_CLK_32K_ENA_MASK
| MADERA_CLK_32K_SRC_MASK
,
546 MADERA_CLK_32K_ENA
| MADERA_32KZ_MCLK2
);
548 dev_err(madera
->dev
, "Failed to init 32k clock: %d\n", ret
);
552 pm_runtime_set_active(madera
->dev
);
553 pm_runtime_enable(madera
->dev
);
554 pm_runtime_set_autosuspend_delay(madera
->dev
, 100);
555 pm_runtime_use_autosuspend(madera
->dev
);
557 /* No devm_ because we need to control shutdown order of children */
558 ret
= mfd_add_devices(madera
->dev
, PLATFORM_DEVID_NONE
,
562 dev_err(madera
->dev
, "Failed to add subdevices: %d\n", ret
);
569 pm_runtime_disable(madera
->dev
);
571 madera_enable_hard_reset(madera
);
572 regulator_disable(madera
->dcvdd
);
574 regulator_bulk_disable(madera
->num_core_supplies
,
575 madera
->core_supplies
);
577 regulator_put(madera
->dcvdd
);
579 mfd_remove_devices(dev
);
583 EXPORT_SYMBOL_GPL(madera_dev_init
);
585 int madera_dev_exit(struct madera
*madera
)
587 /* Prevent any IRQs being serviced while we clean up */
588 disable_irq(madera
->irq
);
591 * DCVDD could be supplied by a child node, we must disable it before
592 * removing the children, and prevent PM runtime from turning it back on
594 pm_runtime_disable(madera
->dev
);
596 regulator_disable(madera
->dcvdd
);
597 regulator_put(madera
->dcvdd
);
599 mfd_remove_devices(madera
->dev
);
600 madera_enable_hard_reset(madera
);
602 regulator_bulk_disable(madera
->num_core_supplies
,
603 madera
->core_supplies
);
606 EXPORT_SYMBOL_GPL(madera_dev_exit
);
608 MODULE_DESCRIPTION("Madera core MFD driver");
609 MODULE_AUTHOR("Richard Fitzgerald <rf@opensource.cirrus.com>");
610 MODULE_LICENSE("GPL v2");