blob: 440030cecbbdae952d2a64f1050473b2f254337d [file] [log] [blame]
Richard Fitzgerald16b27462018-05-21 10:59:56 +01001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Core MFD support for Cirrus Logic Madera codecs
4 *
5 * Copyright (C) 2015-2018 Cirrus Logic
6 *
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.
10 */
11
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>
19#include <linux/of.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>
27
28#include <linux/mfd/madera/core.h>
29#include <linux/mfd/madera/registers.h>
30
31#include "madera.h"
32
33#define CS47L35_SILICON_ID 0x6360
34#define CS47L85_SILICON_ID 0x6338
35#define CS47L90_SILICON_ID 0x6364
36
37#define MADERA_32KZ_MCLK2 1
38
39static const char * const madera_core_supplies[] = {
40 "AVDD",
41 "DBVDD1",
42};
43
44static const struct mfd_cell madera_ldo1_devs[] = {
45 { .name = "madera-ldo1" },
46};
47
48static const char * const cs47l35_supplies[] = {
49 "MICVDD",
50 "DBVDD2",
51 "CPVDD1",
52 "CPVDD2",
53 "SPKVDD",
54};
55
56static 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", },
62 {
63 .name = "cs47l35-codec",
64 .parent_supplies = cs47l35_supplies,
65 .num_parent_supplies = ARRAY_SIZE(cs47l35_supplies),
66 },
67};
68
69static const char * const cs47l85_supplies[] = {
70 "MICVDD",
71 "DBVDD2",
72 "DBVDD3",
73 "DBVDD4",
74 "CPVDD1",
75 "CPVDD2",
76 "SPKVDDL",
77 "SPKVDDR",
78};
79
80static 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", },
86 {
87 .name = "cs47l85-codec",
88 .parent_supplies = cs47l85_supplies,
89 .num_parent_supplies = ARRAY_SIZE(cs47l85_supplies),
90 },
91};
92
93static const char * const cs47l90_supplies[] = {
94 "MICVDD",
95 "DBVDD2",
96 "DBVDD3",
97 "DBVDD4",
98 "CPVDD1",
99 "CPVDD2",
100};
101
102static 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", },
108 {
109 .name = "cs47l90-codec",
110 .parent_supplies = cs47l90_supplies,
111 .num_parent_supplies = ARRAY_SIZE(cs47l90_supplies),
112 },
113};
114
115/* Used by madera-i2c and madera-spi drivers */
116const char *madera_name_from_type(enum madera_type type)
117{
118 switch (type) {
119 case CS47L35:
120 return "CS47L35";
121 case CS47L85:
122 return "CS47L85";
123 case CS47L90:
124 return "CS47L90";
125 case CS47L91:
126 return "CS47L91";
127 case WM1840:
128 return "WM1840";
129 default:
130 return "Unknown";
131 }
132}
133EXPORT_SYMBOL_GPL(madera_name_from_type);
134
Richard Fitzgeraldb04e68d2018-08-27 14:51:15 +0100135#define MADERA_BOOT_POLL_INTERVAL_USEC 5000
136#define MADERA_BOOT_POLL_TIMEOUT_USEC 25000
Richard Fitzgerald16b27462018-05-21 10:59:56 +0100137
138static int madera_wait_for_boot(struct madera *madera)
139{
Richard Fitzgeraldb04e68d2018-08-27 14:51:15 +0100140 ktime_t timeout;
Richard Fitzgerald16b27462018-05-21 10:59:56 +0100141 unsigned int val;
Richard Fitzgeraldb04e68d2018-08-27 14:51:15 +0100142 int ret = 0;
Richard Fitzgerald16b27462018-05-21 10:59:56 +0100143
144 /*
145 * We can't use an interrupt as we need to runtime resume to do so,
146 * so we poll the status bit. This won't race with the interrupt
147 * handler because it will be blocked on runtime resume.
Richard Fitzgeraldb04e68d2018-08-27 14:51:15 +0100148 * The chip could NAK a read request while it is booting so ignore
149 * errors from regmap_read.
Richard Fitzgerald16b27462018-05-21 10:59:56 +0100150 */
Richard Fitzgeraldb04e68d2018-08-27 14:51:15 +0100151 timeout = ktime_add_us(ktime_get(), MADERA_BOOT_POLL_TIMEOUT_USEC);
152 regmap_read(madera->regmap, MADERA_IRQ1_RAW_STATUS_1, &val);
153 while (!(val & MADERA_BOOT_DONE_STS1) &&
154 !ktime_after(ktime_get(), timeout)) {
155 usleep_range(MADERA_BOOT_POLL_INTERVAL_USEC / 2,
156 MADERA_BOOT_POLL_INTERVAL_USEC);
157 regmap_read(madera->regmap, MADERA_IRQ1_RAW_STATUS_1, &val);
158 };
Richard Fitzgerald16b27462018-05-21 10:59:56 +0100159
Richard Fitzgeraldb04e68d2018-08-27 14:51:15 +0100160 if (!(val & MADERA_BOOT_DONE_STS1)) {
161 dev_err(madera->dev, "Polling BOOT_DONE_STS timed out\n");
162 ret = -ETIMEDOUT;
163 }
Richard Fitzgerald16b27462018-05-21 10:59:56 +0100164
165 /*
166 * BOOT_DONE defaults to unmasked on boot so we must ack it.
Richard Fitzgeraldb04e68d2018-08-27 14:51:15 +0100167 * Do this even after a timeout to avoid interrupt storms.
Richard Fitzgerald16b27462018-05-21 10:59:56 +0100168 */
169 regmap_write(madera->regmap, MADERA_IRQ1_STATUS_1,
170 MADERA_BOOT_DONE_EINT1);
171
172 pm_runtime_mark_last_busy(madera->dev);
173
174 return ret;
175}
176
177static int madera_soft_reset(struct madera *madera)
178{
179 int ret;
180
181 ret = regmap_write(madera->regmap, MADERA_SOFTWARE_RESET, 0);
182 if (ret != 0) {
183 dev_err(madera->dev, "Failed to soft reset device: %d\n", ret);
184 return ret;
185 }
186
187 /* Allow time for internal clocks to startup after reset */
188 usleep_range(1000, 2000);
189
190 return 0;
191}
192
193static void madera_enable_hard_reset(struct madera *madera)
194{
195 if (!madera->pdata.reset)
196 return;
197
198 /*
199 * There are many existing out-of-tree users of these codecs that we
200 * can't break so preserve the expected behaviour of setting the line
201 * low to assert reset.
202 */
203 gpiod_set_raw_value_cansleep(madera->pdata.reset, 0);
204}
205
206static void madera_disable_hard_reset(struct madera *madera)
207{
208 if (!madera->pdata.reset)
209 return;
210
211 gpiod_set_raw_value_cansleep(madera->pdata.reset, 1);
212 usleep_range(1000, 2000);
213}
214
215static int __maybe_unused madera_runtime_resume(struct device *dev)
216{
217 struct madera *madera = dev_get_drvdata(dev);
218 int ret;
219
220 dev_dbg(dev, "Leaving sleep mode\n");
221
222 ret = regulator_enable(madera->dcvdd);
223 if (ret) {
224 dev_err(dev, "Failed to enable DCVDD: %d\n", ret);
225 return ret;
226 }
227
228 regcache_cache_only(madera->regmap, false);
229 regcache_cache_only(madera->regmap_32bit, false);
230
231 ret = madera_wait_for_boot(madera);
232 if (ret)
233 goto err;
234
235 ret = regcache_sync(madera->regmap);
236 if (ret) {
237 dev_err(dev, "Failed to restore 16-bit register cache\n");
238 goto err;
239 }
240
241 ret = regcache_sync(madera->regmap_32bit);
242 if (ret) {
243 dev_err(dev, "Failed to restore 32-bit register cache\n");
244 goto err;
245 }
246
247 return 0;
248
249err:
250 regcache_cache_only(madera->regmap_32bit, true);
251 regcache_cache_only(madera->regmap, true);
252 regulator_disable(madera->dcvdd);
253
254 return ret;
255}
256
257static int __maybe_unused madera_runtime_suspend(struct device *dev)
258{
259 struct madera *madera = dev_get_drvdata(dev);
260
261 dev_dbg(madera->dev, "Entering sleep mode\n");
262
263 regcache_cache_only(madera->regmap, true);
264 regcache_mark_dirty(madera->regmap);
265 regcache_cache_only(madera->regmap_32bit, true);
266 regcache_mark_dirty(madera->regmap_32bit);
267
268 regulator_disable(madera->dcvdd);
269
270 return 0;
271}
272
273const struct dev_pm_ops madera_pm_ops = {
274 SET_RUNTIME_PM_OPS(madera_runtime_suspend,
275 madera_runtime_resume,
276 NULL)
277};
278EXPORT_SYMBOL_GPL(madera_pm_ops);
279
280const struct of_device_id madera_of_match[] = {
281 { .compatible = "cirrus,cs47l35", .data = (void *)CS47L35 },
282 { .compatible = "cirrus,cs47l85", .data = (void *)CS47L85 },
283 { .compatible = "cirrus,cs47l90", .data = (void *)CS47L90 },
284 { .compatible = "cirrus,cs47l91", .data = (void *)CS47L91 },
285 { .compatible = "cirrus,wm1840", .data = (void *)WM1840 },
286 {}
287};
288EXPORT_SYMBOL_GPL(madera_of_match);
289
290static int madera_get_reset_gpio(struct madera *madera)
291{
292 struct gpio_desc *reset;
293 int ret;
294
295 if (madera->pdata.reset)
296 return 0;
297
298 reset = devm_gpiod_get_optional(madera->dev, "reset", GPIOD_OUT_LOW);
299 if (IS_ERR(reset)) {
300 ret = PTR_ERR(reset);
301 if (ret != -EPROBE_DEFER)
302 dev_err(madera->dev, "Failed to request /RESET: %d\n",
303 ret);
304 return ret;
305 }
306
307 /*
308 * A hard reset is needed for full reset of the chip. We allow running
309 * without hard reset only because it can be useful for early
310 * prototyping and some debugging, but we need to warn it's not ideal.
311 */
312 if (!reset)
313 dev_warn(madera->dev,
314 "Running without reset GPIO is not recommended\n");
315
316 madera->pdata.reset = reset;
317
318 return 0;
319}
320
321static void madera_set_micbias_info(struct madera *madera)
322{
323 /*
324 * num_childbias is an array because future codecs can have different
325 * childbiases for each micbias. Unspecified values default to 0.
326 */
327 switch (madera->type) {
328 case CS47L35:
329 madera->num_micbias = 2;
330 madera->num_childbias[0] = 2;
331 madera->num_childbias[1] = 2;
332 return;
333 case CS47L85:
334 case WM1840:
335 madera->num_micbias = 4;
336 /* no child biases */
337 return;
338 case CS47L90:
339 case CS47L91:
340 madera->num_micbias = 2;
341 madera->num_childbias[0] = 4;
342 madera->num_childbias[1] = 4;
343 return;
344 default:
345 return;
346 }
347}
348
349int madera_dev_init(struct madera *madera)
350{
351 struct device *dev = madera->dev;
352 unsigned int hwid;
353 int (*patch_fn)(struct madera *) = NULL;
354 const struct mfd_cell *mfd_devs;
355 int n_devs = 0;
356 int i, ret;
357
358 dev_set_drvdata(madera->dev, madera);
359 BLOCKING_INIT_NOTIFIER_HEAD(&madera->notifier);
360 madera_set_micbias_info(madera);
361
362 /*
363 * We need writable hw config info that all children can share.
364 * Simplest to take one shared copy of pdata struct.
365 */
366 if (dev_get_platdata(madera->dev)) {
367 memcpy(&madera->pdata, dev_get_platdata(madera->dev),
368 sizeof(madera->pdata));
369 }
370
371 ret = madera_get_reset_gpio(madera);
372 if (ret)
373 return ret;
374
375 regcache_cache_only(madera->regmap, true);
376 regcache_cache_only(madera->regmap_32bit, true);
377
378 for (i = 0; i < ARRAY_SIZE(madera_core_supplies); i++)
379 madera->core_supplies[i].supply = madera_core_supplies[i];
380
381 madera->num_core_supplies = ARRAY_SIZE(madera_core_supplies);
382
383 /*
384 * On some codecs DCVDD could be supplied by the internal LDO1.
385 * For those we must add the LDO1 driver before requesting DCVDD
386 * No devm_ because we need to control shutdown order of children.
387 */
388 switch (madera->type) {
389 case CS47L35:
390 case CS47L90:
391 case CS47L91:
392 break;
393 case CS47L85:
394 case WM1840:
395 ret = mfd_add_devices(madera->dev, PLATFORM_DEVID_NONE,
396 madera_ldo1_devs,
397 ARRAY_SIZE(madera_ldo1_devs),
398 NULL, 0, NULL);
399 if (ret) {
400 dev_err(dev, "Failed to add LDO1 child: %d\n", ret);
401 return ret;
402 }
403 break;
404 default:
405 /* No point continuing if the type is unknown */
406 dev_err(madera->dev, "Unknown device type %d\n", madera->type);
407 return -ENODEV;
408 }
409
410 ret = devm_regulator_bulk_get(dev, madera->num_core_supplies,
411 madera->core_supplies);
412 if (ret) {
413 dev_err(dev, "Failed to request core supplies: %d\n", ret);
414 goto err_devs;
415 }
416
417 /*
418 * Don't use devres here. If the regulator is one of our children it
419 * will already have been removed before devres cleanup on this mfd
420 * driver tries to call put() on it. We need control of shutdown order.
421 */
422 madera->dcvdd = regulator_get(madera->dev, "DCVDD");
423 if (IS_ERR(madera->dcvdd)) {
424 ret = PTR_ERR(madera->dcvdd);
425 dev_err(dev, "Failed to request DCVDD: %d\n", ret);
426 goto err_devs;
427 }
428
429 ret = regulator_bulk_enable(madera->num_core_supplies,
430 madera->core_supplies);
431 if (ret) {
432 dev_err(dev, "Failed to enable core supplies: %d\n", ret);
433 goto err_dcvdd;
434 }
435
436 ret = regulator_enable(madera->dcvdd);
437 if (ret) {
438 dev_err(dev, "Failed to enable DCVDD: %d\n", ret);
439 goto err_enable;
440 }
441
442 madera_disable_hard_reset(madera);
443
444 regcache_cache_only(madera->regmap, false);
445 regcache_cache_only(madera->regmap_32bit, false);
446
447 /*
448 * Now we can power up and verify that this is a chip we know about
449 * before we start doing any writes to its registers.
450 */
451 ret = regmap_read(madera->regmap, MADERA_SOFTWARE_RESET, &hwid);
452 if (ret) {
453 dev_err(dev, "Failed to read ID register: %d\n", ret);
454 goto err_reset;
455 }
456
457 switch (hwid) {
458 case CS47L35_SILICON_ID:
459 if (IS_ENABLED(CONFIG_MFD_CS47L35)) {
460 switch (madera->type) {
461 case CS47L35:
462 patch_fn = cs47l35_patch;
463 mfd_devs = cs47l35_devs;
464 n_devs = ARRAY_SIZE(cs47l35_devs);
465 break;
466 default:
467 break;
468 }
469 }
470 break;
471 case CS47L85_SILICON_ID:
472 if (IS_ENABLED(CONFIG_MFD_CS47L85)) {
473 switch (madera->type) {
474 case CS47L85:
475 case WM1840:
476 patch_fn = cs47l85_patch;
477 mfd_devs = cs47l85_devs;
478 n_devs = ARRAY_SIZE(cs47l85_devs);
479 break;
480 default:
481 break;
482 }
483 }
484 break;
485 case CS47L90_SILICON_ID:
486 if (IS_ENABLED(CONFIG_MFD_CS47L90)) {
487 switch (madera->type) {
488 case CS47L90:
489 case CS47L91:
490 patch_fn = cs47l90_patch;
491 mfd_devs = cs47l90_devs;
492 n_devs = ARRAY_SIZE(cs47l90_devs);
493 break;
494 default:
495 break;
496 }
497 }
498 break;
499 default:
500 dev_err(madera->dev, "Unknown device ID: %x\n", hwid);
501 ret = -EINVAL;
502 goto err_reset;
503 }
504
505 if (!n_devs) {
506 dev_err(madera->dev, "Device ID 0x%x not a %s\n", hwid,
507 madera->type_name);
508 ret = -ENODEV;
509 goto err_reset;
510 }
511
512 /*
513 * It looks like a device we support. If we don't have a hard reset
514 * we can now attempt a soft reset.
515 */
516 if (!madera->pdata.reset) {
517 ret = madera_soft_reset(madera);
518 if (ret)
519 goto err_reset;
520 }
521
522 ret = madera_wait_for_boot(madera);
523 if (ret) {
524 dev_err(madera->dev, "Device failed initial boot: %d\n", ret);
525 goto err_reset;
526 }
527
528 ret = regmap_read(madera->regmap, MADERA_HARDWARE_REVISION,
529 &madera->rev);
530 if (ret) {
531 dev_err(dev, "Failed to read revision register: %d\n", ret);
532 goto err_reset;
533 }
534 madera->rev &= MADERA_HW_REVISION_MASK;
535
536 dev_info(dev, "%s silicon revision %d\n", madera->type_name,
537 madera->rev);
538
539 /* Apply hardware patch */
540 if (patch_fn) {
541 ret = patch_fn(madera);
542 if (ret) {
543 dev_err(madera->dev, "Failed to apply patch %d\n", ret);
544 goto err_reset;
545 }
546 }
547
548 /* Init 32k clock sourced from MCLK2 */
549 ret = regmap_update_bits(madera->regmap,
550 MADERA_CLOCK_32K_1,
551 MADERA_CLK_32K_ENA_MASK | MADERA_CLK_32K_SRC_MASK,
552 MADERA_CLK_32K_ENA | MADERA_32KZ_MCLK2);
553 if (ret) {
554 dev_err(madera->dev, "Failed to init 32k clock: %d\n", ret);
555 goto err_reset;
556 }
557
558 pm_runtime_set_active(madera->dev);
559 pm_runtime_enable(madera->dev);
560 pm_runtime_set_autosuspend_delay(madera->dev, 100);
561 pm_runtime_use_autosuspend(madera->dev);
562
563 /* No devm_ because we need to control shutdown order of children */
564 ret = mfd_add_devices(madera->dev, PLATFORM_DEVID_NONE,
565 mfd_devs, n_devs,
566 NULL, 0, NULL);
567 if (ret) {
568 dev_err(madera->dev, "Failed to add subdevices: %d\n", ret);
569 goto err_pm_runtime;
570 }
571
572 return 0;
573
574err_pm_runtime:
575 pm_runtime_disable(madera->dev);
576err_reset:
577 madera_enable_hard_reset(madera);
578 regulator_disable(madera->dcvdd);
579err_enable:
580 regulator_bulk_disable(madera->num_core_supplies,
581 madera->core_supplies);
582err_dcvdd:
583 regulator_put(madera->dcvdd);
584err_devs:
585 mfd_remove_devices(dev);
586
587 return ret;
588}
589EXPORT_SYMBOL_GPL(madera_dev_init);
590
591int madera_dev_exit(struct madera *madera)
592{
593 /* Prevent any IRQs being serviced while we clean up */
594 disable_irq(madera->irq);
595
596 /*
597 * DCVDD could be supplied by a child node, we must disable it before
598 * removing the children, and prevent PM runtime from turning it back on
599 */
600 pm_runtime_disable(madera->dev);
601
602 regulator_disable(madera->dcvdd);
603 regulator_put(madera->dcvdd);
604
605 mfd_remove_devices(madera->dev);
606 madera_enable_hard_reset(madera);
607
608 regulator_bulk_disable(madera->num_core_supplies,
609 madera->core_supplies);
610 return 0;
611}
612EXPORT_SYMBOL_GPL(madera_dev_exit);
613
614MODULE_DESCRIPTION("Madera core MFD driver");
615MODULE_AUTHOR("Richard Fitzgerald <rf@opensource.cirrus.com>");
616MODULE_LICENSE("GPL v2");