pwrseq_simple.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * Copyright (C) 2014 Linaro Ltd
  3. *
  4. * Author: Ulf Hansson <ulf.hansson@linaro.org>
  5. *
  6. * License terms: GNU General Public License (GPL) version 2
  7. *
  8. * Simple MMC power sequence management
  9. */
  10. #include <linux/clk.h>
  11. #include <linux/init.h>
  12. #include <linux/kernel.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/module.h>
  15. #include <linux/slab.h>
  16. #include <linux/device.h>
  17. #include <linux/err.h>
  18. #include <linux/gpio/consumer.h>
  19. #include <linux/delay.h>
  20. #include <linux/property.h>
  21. #include <linux/mmc/host.h>
  22. #include "pwrseq.h"
  23. struct mmc_pwrseq_simple {
  24. struct mmc_pwrseq pwrseq;
  25. bool clk_enabled;
  26. u32 post_power_on_delay_ms;
  27. struct clk *ext_clk;
  28. struct gpio_descs *reset_gpios;
  29. };
  30. #define to_pwrseq_simple(p) container_of(p, struct mmc_pwrseq_simple, pwrseq)
  31. static void mmc_pwrseq_simple_set_gpios_value(struct mmc_pwrseq_simple *pwrseq,
  32. int value)
  33. {
  34. struct gpio_descs *reset_gpios = pwrseq->reset_gpios;
  35. if (!IS_ERR(reset_gpios)) {
  36. int i, *values;
  37. int nvalues = reset_gpios->ndescs;
  38. values = kmalloc_array(nvalues, sizeof(int), GFP_KERNEL);
  39. if (!values)
  40. return;
  41. for (i = 0; i < nvalues; i++)
  42. values[i] = value;
  43. gpiod_set_array_value_cansleep(nvalues, reset_gpios->desc, values);
  44. kfree(values);
  45. }
  46. }
  47. static void mmc_pwrseq_simple_pre_power_on(struct mmc_host *host)
  48. {
  49. struct mmc_pwrseq_simple *pwrseq = to_pwrseq_simple(host->pwrseq);
  50. if (!IS_ERR(pwrseq->ext_clk) && !pwrseq->clk_enabled) {
  51. clk_prepare_enable(pwrseq->ext_clk);
  52. pwrseq->clk_enabled = true;
  53. }
  54. mmc_pwrseq_simple_set_gpios_value(pwrseq, 1);
  55. }
  56. static void mmc_pwrseq_simple_post_power_on(struct mmc_host *host)
  57. {
  58. struct mmc_pwrseq_simple *pwrseq = to_pwrseq_simple(host->pwrseq);
  59. mmc_pwrseq_simple_set_gpios_value(pwrseq, 0);
  60. if (pwrseq->post_power_on_delay_ms)
  61. msleep(pwrseq->post_power_on_delay_ms);
  62. }
  63. static void mmc_pwrseq_simple_power_off(struct mmc_host *host)
  64. {
  65. struct mmc_pwrseq_simple *pwrseq = to_pwrseq_simple(host->pwrseq);
  66. mmc_pwrseq_simple_set_gpios_value(pwrseq, 1);
  67. if (!IS_ERR(pwrseq->ext_clk) && pwrseq->clk_enabled) {
  68. clk_disable_unprepare(pwrseq->ext_clk);
  69. pwrseq->clk_enabled = false;
  70. }
  71. }
  72. static const struct mmc_pwrseq_ops mmc_pwrseq_simple_ops = {
  73. .pre_power_on = mmc_pwrseq_simple_pre_power_on,
  74. .post_power_on = mmc_pwrseq_simple_post_power_on,
  75. .power_off = mmc_pwrseq_simple_power_off,
  76. };
  77. static const struct of_device_id mmc_pwrseq_simple_of_match[] = {
  78. { .compatible = "mmc-pwrseq-simple",},
  79. {/* sentinel */},
  80. };
  81. MODULE_DEVICE_TABLE(of, mmc_pwrseq_simple_of_match);
  82. static int mmc_pwrseq_simple_probe(struct platform_device *pdev)
  83. {
  84. struct mmc_pwrseq_simple *pwrseq;
  85. struct device *dev = &pdev->dev;
  86. pwrseq = devm_kzalloc(dev, sizeof(*pwrseq), GFP_KERNEL);
  87. if (!pwrseq)
  88. return -ENOMEM;
  89. pwrseq->ext_clk = devm_clk_get(dev, "ext_clock");
  90. if (IS_ERR(pwrseq->ext_clk) && PTR_ERR(pwrseq->ext_clk) != -ENOENT)
  91. return PTR_ERR(pwrseq->ext_clk);
  92. pwrseq->reset_gpios = devm_gpiod_get_array(dev, "reset",
  93. GPIOD_OUT_HIGH);
  94. if (IS_ERR(pwrseq->reset_gpios) &&
  95. PTR_ERR(pwrseq->reset_gpios) != -ENOENT &&
  96. PTR_ERR(pwrseq->reset_gpios) != -ENOSYS) {
  97. return PTR_ERR(pwrseq->reset_gpios);
  98. }
  99. device_property_read_u32(dev, "post-power-on-delay-ms",
  100. &pwrseq->post_power_on_delay_ms);
  101. pwrseq->pwrseq.dev = dev;
  102. pwrseq->pwrseq.ops = &mmc_pwrseq_simple_ops;
  103. pwrseq->pwrseq.owner = THIS_MODULE;
  104. platform_set_drvdata(pdev, pwrseq);
  105. return mmc_pwrseq_register(&pwrseq->pwrseq);
  106. }
  107. static int mmc_pwrseq_simple_remove(struct platform_device *pdev)
  108. {
  109. struct mmc_pwrseq_simple *pwrseq = platform_get_drvdata(pdev);
  110. mmc_pwrseq_unregister(&pwrseq->pwrseq);
  111. return 0;
  112. }
  113. static struct platform_driver mmc_pwrseq_simple_driver = {
  114. .probe = mmc_pwrseq_simple_probe,
  115. .remove = mmc_pwrseq_simple_remove,
  116. .driver = {
  117. .name = "pwrseq_simple",
  118. .of_match_table = mmc_pwrseq_simple_of_match,
  119. },
  120. };
  121. module_platform_driver(mmc_pwrseq_simple_driver);
  122. MODULE_LICENSE("GPL v2");