blob: 2cea00ed4e655d26aae4bb6af62d32077b5bafb0 [file] [log] [blame]
Ulf Hansson3aa87932014-11-28 14:38:36 +01001/*
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 * MMC power sequence management
9 */
Ulf Hansson8c96f892014-12-05 14:36:58 +010010#include <linux/kernel.h>
11#include <linux/platform_device.h>
12#include <linux/err.h>
13#include <linux/of.h>
14#include <linux/of_platform.h>
15
Ulf Hansson3aa87932014-11-28 14:38:36 +010016#include <linux/mmc/host.h>
17
18#include "pwrseq.h"
19
Ulf Hansson8c96f892014-12-05 14:36:58 +010020struct mmc_pwrseq_match {
21 const char *compatible;
22 int (*alloc)(struct mmc_host *host, struct device *dev);
23};
24
25static struct mmc_pwrseq_match pwrseq_match[] = {
26 {
27 .compatible = "mmc-pwrseq-simple",
28 .alloc = mmc_pwrseq_simple_alloc,
29 },
30};
31
32static struct mmc_pwrseq_match *mmc_pwrseq_find(struct device_node *np)
33{
34 struct mmc_pwrseq_match *match = ERR_PTR(-ENODEV);
35 int i;
36
37 for (i = 0; i < ARRAY_SIZE(pwrseq_match); i++) {
38 if (of_device_is_compatible(np, pwrseq_match[i].compatible)) {
39 match = &pwrseq_match[i];
40 break;
41 }
42 }
43
44 return match;
45}
Ulf Hansson3aa87932014-11-28 14:38:36 +010046
47int mmc_pwrseq_alloc(struct mmc_host *host)
48{
Ulf Hansson8c96f892014-12-05 14:36:58 +010049 struct platform_device *pdev;
50 struct device_node *np;
51 struct mmc_pwrseq_match *match;
52 int ret = 0;
53
54 np = of_parse_phandle(host->parent->of_node, "mmc-pwrseq", 0);
55 if (!np)
56 return 0;
57
58 pdev = of_find_device_by_node(np);
59 if (!pdev) {
60 ret = -ENODEV;
61 goto err;
62 }
63
64 match = mmc_pwrseq_find(np);
65 if (IS_ERR(match)) {
66 ret = PTR_ERR(match);
67 goto err;
68 }
69
70 ret = match->alloc(host, &pdev->dev);
71 if (!ret)
72 dev_info(host->parent, "allocated mmc-pwrseq\n");
73
74err:
75 of_node_put(np);
76 return ret;
Ulf Hansson3aa87932014-11-28 14:38:36 +010077}
78
79void mmc_pwrseq_pre_power_on(struct mmc_host *host)
80{
81 struct mmc_pwrseq *pwrseq = host->pwrseq;
82
83 if (pwrseq && pwrseq->ops && pwrseq->ops->pre_power_on)
84 pwrseq->ops->pre_power_on(host);
85}
86
87void mmc_pwrseq_post_power_on(struct mmc_host *host)
88{
89 struct mmc_pwrseq *pwrseq = host->pwrseq;
90
91 if (pwrseq && pwrseq->ops && pwrseq->ops->post_power_on)
92 pwrseq->ops->post_power_on(host);
93}
94
95void mmc_pwrseq_power_off(struct mmc_host *host)
96{
97 struct mmc_pwrseq *pwrseq = host->pwrseq;
98
99 if (pwrseq && pwrseq->ops && pwrseq->ops->power_off)
100 pwrseq->ops->power_off(host);
101}
102
103void mmc_pwrseq_free(struct mmc_host *host)
104{
105 struct mmc_pwrseq *pwrseq = host->pwrseq;
106
107 if (pwrseq && pwrseq->ops && pwrseq->ops->free)
108 pwrseq->ops->free(host);
109}