madera-core.c 14 KB

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