blob: 9386c4771814b648496f8119810cc3bface68788 [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>
Ulf Hansson8c96f892014-12-05 14:36:58 +010011#include <linux/err.h>
Srinivas Kandagatlad97a1e52016-04-14 14:02:16 +010012#include <linux/module.h>
Ulf Hansson8c96f892014-12-05 14:36:58 +010013#include <linux/of.h>
Ulf Hansson8c96f892014-12-05 14:36:58 +010014
Ulf Hansson3aa87932014-11-28 14:38:36 +010015#include <linux/mmc/host.h>
16
17#include "pwrseq.h"
18
Srinivas Kandagatlad97a1e52016-04-14 14:02:16 +010019static DEFINE_MUTEX(pwrseq_list_mutex);
20static LIST_HEAD(pwrseq_list);
Ulf Hansson3aa87932014-11-28 14:38:36 +010021
22int mmc_pwrseq_alloc(struct mmc_host *host)
23{
Ulf Hansson8c96f892014-12-05 14:36:58 +010024 struct device_node *np;
Srinivas Kandagatlad97a1e52016-04-14 14:02:16 +010025 struct mmc_pwrseq *p;
Ulf Hansson8c96f892014-12-05 14:36:58 +010026
27 np = of_parse_phandle(host->parent->of_node, "mmc-pwrseq", 0);
28 if (!np)
29 return 0;
30
Srinivas Kandagatlad97a1e52016-04-14 14:02:16 +010031 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;
39
40 break;
41 }
Ulf Hansson8c96f892014-12-05 14:36:58 +010042 }
43
Srinivas Kandagatlad97a1e52016-04-14 14:02:16 +010044 of_node_put(np);
45 mutex_unlock(&pwrseq_list_mutex);
Ulf Hansson8c96f892014-12-05 14:36:58 +010046
Srinivas Kandagatlad97a1e52016-04-14 14:02:16 +010047 if (!host->pwrseq)
48 return -EPROBE_DEFER;
Alexandre Courbot0f12a0c2015-02-12 13:36:11 +090049
Alexandre Courbot0f12a0c2015-02-12 13:36:11 +090050 dev_info(host->parent, "allocated mmc-pwrseq\n");
Ulf Hansson8c96f892014-12-05 14:36:58 +010051
Srinivas Kandagatlad97a1e52016-04-14 14:02:16 +010052 return 0;
Ulf Hansson3aa87932014-11-28 14:38:36 +010053}
54
55void mmc_pwrseq_pre_power_on(struct mmc_host *host)
56{
57 struct mmc_pwrseq *pwrseq = host->pwrseq;
58
Srinivas Kandagatlad97a1e52016-04-14 14:02:16 +010059 if (pwrseq && pwrseq->ops->pre_power_on)
Ulf Hansson3aa87932014-11-28 14:38:36 +010060 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
Srinivas Kandagatlad97a1e52016-04-14 14:02:16 +010067 if (pwrseq && pwrseq->ops->post_power_on)
Ulf Hansson3aa87932014-11-28 14:38:36 +010068 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
Srinivas Kandagatlad97a1e52016-04-14 14:02:16 +010075 if (pwrseq && pwrseq->ops->power_off)
Ulf Hansson3aa87932014-11-28 14:38:36 +010076 pwrseq->ops->power_off(host);
77}
78
79void mmc_pwrseq_free(struct mmc_host *host)
80{
81 struct mmc_pwrseq *pwrseq = host->pwrseq;
82
Srinivas Kandagatlad97a1e52016-04-14 14:02:16 +010083 if (pwrseq) {
84 module_put(pwrseq->owner);
85 host->pwrseq = NULL;
86 }
Ulf Hansson3aa87932014-11-28 14:38:36 +010087}
Srinivas Kandagatlad97a1e52016-04-14 14:02:16 +010088
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;
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);