pwrseq.c (e5451c8f8330e03ad3cfa16048b4daf961af434f) pwrseq.c (d97a1e5d7cd2b5b0edc02a40fe6897b710c9e10f)
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 * MMC power sequence management
9 */
10#include <linux/kernel.h>
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 * MMC power sequence management
9 */
10#include <linux/kernel.h>
11#include <linux/platform_device.h>
12#include <linux/err.h>
11#include <linux/err.h>
12#include <linux/module.h>
13#include <linux/of.h>
13#include <linux/of.h>
14#include <linux/of_platform.h>
15
16#include <linux/mmc/host.h>
17
18#include "pwrseq.h"
19
14
15#include <linux/mmc/host.h>
16
17#include "pwrseq.h"
18
20struct mmc_pwrseq_match {
21 const char *compatible;
22 struct mmc_pwrseq *(*alloc)(struct mmc_host *host, struct device *dev);
23};
19static DEFINE_MUTEX(pwrseq_list_mutex);
20static LIST_HEAD(pwrseq_list);
24
21
25static struct mmc_pwrseq_match pwrseq_match[] = {
26 {
27 .compatible = "mmc-pwrseq-simple",
28 .alloc = mmc_pwrseq_simple_alloc,
29 }, {
30 .compatible = "mmc-pwrseq-emmc",
31 .alloc = mmc_pwrseq_emmc_alloc,
32 },
33};
34
35static struct mmc_pwrseq_match *mmc_pwrseq_find(struct device_node *np)
36{
37 struct mmc_pwrseq_match *match = ERR_PTR(-ENODEV);
38 int i;
39
40 for (i = 0; i < ARRAY_SIZE(pwrseq_match); i++) {
41 if (of_device_is_compatible(np, pwrseq_match[i].compatible)) {
42 match = &pwrseq_match[i];
43 break;
44 }
45 }
46
47 return match;
48}
49
50int mmc_pwrseq_alloc(struct mmc_host *host)
51{
22int mmc_pwrseq_alloc(struct mmc_host *host)
23{
52 struct platform_device *pdev;
53 struct device_node *np;
24 struct device_node *np;
54 struct mmc_pwrseq_match *match;
55 struct mmc_pwrseq *pwrseq;
56 int ret = 0;
25 struct mmc_pwrseq *p;
57
58 np = of_parse_phandle(host->parent->of_node, "mmc-pwrseq", 0);
59 if (!np)
60 return 0;
61
26
27 np = of_parse_phandle(host->parent->of_node, "mmc-pwrseq", 0);
28 if (!np)
29 return 0;
30
62 pdev = of_find_device_by_node(np);
63 if (!pdev) {
64 ret = -ENODEV;
65 goto err;
66 }
31 mutex_lock(&pwrseq_list_mutex);
32 list_for_each_entry(p, &pwrseq_list, pwrseq_node) {
33 if (p->dev->of_node == np) {
34 if (!try_module_get(p->owner))
35 dev_err(host->parent,
36 "increasing module refcount failed\n");
37 else
38 host->pwrseq = p;
67
39
68 match = mmc_pwrseq_find(np);
69 if (IS_ERR(match)) {
70 ret = PTR_ERR(match);
71 goto err;
40 break;
41 }
72 }
73
42 }
43
74 pwrseq = match->alloc(host, &pdev->dev);
75 if (IS_ERR(pwrseq)) {
76 ret = PTR_ERR(pwrseq);
77 goto err;
78 }
44 of_node_put(np);
45 mutex_unlock(&pwrseq_list_mutex);
79
46
80 host->pwrseq = pwrseq;
47 if (!host->pwrseq)
48 return -EPROBE_DEFER;
49
81 dev_info(host->parent, "allocated mmc-pwrseq\n");
82
50 dev_info(host->parent, "allocated mmc-pwrseq\n");
51
83err:
84 of_node_put(np);
85 return ret;
52 return 0;
86}
87
88void mmc_pwrseq_pre_power_on(struct mmc_host *host)
89{
90 struct mmc_pwrseq *pwrseq = host->pwrseq;
91
53}
54
55void mmc_pwrseq_pre_power_on(struct mmc_host *host)
56{
57 struct mmc_pwrseq *pwrseq = host->pwrseq;
58
92 if (pwrseq && pwrseq->ops && pwrseq->ops->pre_power_on)
59 if (pwrseq && pwrseq->ops->pre_power_on)
93 pwrseq->ops->pre_power_on(host);
94}
95
96void mmc_pwrseq_post_power_on(struct mmc_host *host)
97{
98 struct mmc_pwrseq *pwrseq = host->pwrseq;
99
60 pwrseq->ops->pre_power_on(host);
61}
62
63void mmc_pwrseq_post_power_on(struct mmc_host *host)
64{
65 struct mmc_pwrseq *pwrseq = host->pwrseq;
66
100 if (pwrseq && pwrseq->ops && pwrseq->ops->post_power_on)
67 if (pwrseq && pwrseq->ops->post_power_on)
101 pwrseq->ops->post_power_on(host);
102}
103
104void mmc_pwrseq_power_off(struct mmc_host *host)
105{
106 struct mmc_pwrseq *pwrseq = host->pwrseq;
107
68 pwrseq->ops->post_power_on(host);
69}
70
71void mmc_pwrseq_power_off(struct mmc_host *host)
72{
73 struct mmc_pwrseq *pwrseq = host->pwrseq;
74
108 if (pwrseq && pwrseq->ops && pwrseq->ops->power_off)
75 if (pwrseq && pwrseq->ops->power_off)
109 pwrseq->ops->power_off(host);
110}
111
112void mmc_pwrseq_free(struct mmc_host *host)
113{
114 struct mmc_pwrseq *pwrseq = host->pwrseq;
115
76 pwrseq->ops->power_off(host);
77}
78
79void mmc_pwrseq_free(struct mmc_host *host)
80{
81 struct mmc_pwrseq *pwrseq = host->pwrseq;
82
116 if (pwrseq && pwrseq->ops && pwrseq->ops->free)
117 pwrseq->ops->free(host);
83 if (pwrseq) {
84 module_put(pwrseq->owner);
85 host->pwrseq = NULL;
86 }
87}
118
88
119 host->pwrseq = NULL;
89int mmc_pwrseq_register(struct mmc_pwrseq *pwrseq)
90{
91 if (!pwrseq || !pwrseq->ops || !pwrseq->dev)
92 return -EINVAL;
93
94 mutex_lock(&pwrseq_list_mutex);
95 list_add(&pwrseq->pwrseq_node, &pwrseq_list);
96 mutex_unlock(&pwrseq_list_mutex);
97
98 return 0;
120}
99}
100EXPORT_SYMBOL_GPL(mmc_pwrseq_register);
101
102void mmc_pwrseq_unregister(struct mmc_pwrseq *pwrseq)
103{
104 if (pwrseq) {
105 mutex_lock(&pwrseq_list_mutex);
106 list_del(&pwrseq->pwrseq_node);
107 mutex_unlock(&pwrseq_list_mutex);
108 }
109}
110EXPORT_SYMBOL_GPL(mmc_pwrseq_unregister);