pwrseq_simple.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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/kernel.h>
  12. #include <linux/slab.h>
  13. #include <linux/device.h>
  14. #include <linux/err.h>
  15. #include <linux/gpio/consumer.h>
  16. #include <linux/mmc/host.h>
  17. #include "pwrseq.h"
  18. struct mmc_pwrseq_simple {
  19. struct mmc_pwrseq pwrseq;
  20. bool clk_enabled;
  21. struct clk *ext_clk;
  22. struct gpio_descs *reset_gpios;
  23. };
  24. static void mmc_pwrseq_simple_set_gpios_value(struct mmc_pwrseq_simple *pwrseq,
  25. int value)
  26. {
  27. struct gpio_descs *reset_gpios = pwrseq->reset_gpios;
  28. if (!IS_ERR(reset_gpios)) {
  29. int i;
  30. int values[reset_gpios->ndescs];
  31. for (i = 0; i < reset_gpios->ndescs; i++)
  32. values[i] = value;
  33. gpiod_set_array_value_cansleep(
  34. reset_gpios->ndescs, reset_gpios->desc, values);
  35. }
  36. }
  37. static void mmc_pwrseq_simple_pre_power_on(struct mmc_host *host)
  38. {
  39. struct mmc_pwrseq_simple *pwrseq = container_of(host->pwrseq,
  40. struct mmc_pwrseq_simple, pwrseq);
  41. if (!IS_ERR(pwrseq->ext_clk) && !pwrseq->clk_enabled) {
  42. clk_prepare_enable(pwrseq->ext_clk);
  43. pwrseq->clk_enabled = true;
  44. }
  45. mmc_pwrseq_simple_set_gpios_value(pwrseq, 1);
  46. }
  47. static void mmc_pwrseq_simple_post_power_on(struct mmc_host *host)
  48. {
  49. struct mmc_pwrseq_simple *pwrseq = container_of(host->pwrseq,
  50. struct mmc_pwrseq_simple, pwrseq);
  51. mmc_pwrseq_simple_set_gpios_value(pwrseq, 0);
  52. }
  53. static void mmc_pwrseq_simple_power_off(struct mmc_host *host)
  54. {
  55. struct mmc_pwrseq_simple *pwrseq = container_of(host->pwrseq,
  56. struct mmc_pwrseq_simple, pwrseq);
  57. mmc_pwrseq_simple_set_gpios_value(pwrseq, 1);
  58. if (!IS_ERR(pwrseq->ext_clk) && pwrseq->clk_enabled) {
  59. clk_disable_unprepare(pwrseq->ext_clk);
  60. pwrseq->clk_enabled = false;
  61. }
  62. }
  63. static void mmc_pwrseq_simple_free(struct mmc_host *host)
  64. {
  65. struct mmc_pwrseq_simple *pwrseq = container_of(host->pwrseq,
  66. struct mmc_pwrseq_simple, pwrseq);
  67. if (!IS_ERR(pwrseq->reset_gpios))
  68. gpiod_put_array(pwrseq->reset_gpios);
  69. if (!IS_ERR(pwrseq->ext_clk))
  70. clk_put(pwrseq->ext_clk);
  71. kfree(pwrseq);
  72. }
  73. static const struct mmc_pwrseq_ops mmc_pwrseq_simple_ops = {
  74. .pre_power_on = mmc_pwrseq_simple_pre_power_on,
  75. .post_power_on = mmc_pwrseq_simple_post_power_on,
  76. .power_off = mmc_pwrseq_simple_power_off,
  77. .free = mmc_pwrseq_simple_free,
  78. };
  79. struct mmc_pwrseq *mmc_pwrseq_simple_alloc(struct mmc_host *host,
  80. struct device *dev)
  81. {
  82. struct mmc_pwrseq_simple *pwrseq;
  83. int ret = 0;
  84. pwrseq = kzalloc(sizeof(*pwrseq), GFP_KERNEL);
  85. if (!pwrseq)
  86. return ERR_PTR(-ENOMEM);
  87. pwrseq->ext_clk = clk_get(dev, "ext_clock");
  88. if (IS_ERR(pwrseq->ext_clk) &&
  89. PTR_ERR(pwrseq->ext_clk) != -ENOENT) {
  90. ret = PTR_ERR(pwrseq->ext_clk);
  91. goto free;
  92. }
  93. pwrseq->reset_gpios = gpiod_get_array(dev, "reset", 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. ret = PTR_ERR(pwrseq->reset_gpios);
  98. goto clk_put;
  99. }
  100. pwrseq->pwrseq.ops = &mmc_pwrseq_simple_ops;
  101. return &pwrseq->pwrseq;
  102. clk_put:
  103. if (!IS_ERR(pwrseq->ext_clk))
  104. clk_put(pwrseq->ext_clk);
  105. free:
  106. kfree(pwrseq);
  107. return ERR_PTR(ret);
  108. }