xref: /linux-6.15/drivers/mmc/core/pwrseq_simple.c (revision 5b96fea7)
1 /*
2  *  Copyright (C) 2014 Linaro Ltd
3  *
4  * Author: Ulf Hansson <[email protected]>
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 
17 #include <linux/mmc/host.h>
18 
19 #include "pwrseq.h"
20 
21 struct mmc_pwrseq_simple {
22 	struct mmc_pwrseq pwrseq;
23 	bool clk_enabled;
24 	struct clk *ext_clk;
25 	struct gpio_descs *reset_gpios;
26 };
27 
28 #define to_pwrseq_simple(p) container_of(p, struct mmc_pwrseq_simple, pwrseq)
29 
30 static void mmc_pwrseq_simple_set_gpios_value(struct mmc_pwrseq_simple *pwrseq,
31 					      int value)
32 {
33 	struct gpio_descs *reset_gpios = pwrseq->reset_gpios;
34 
35 	if (!IS_ERR(reset_gpios)) {
36 		int i;
37 		int values[reset_gpios->ndescs];
38 
39 		for (i = 0; i < reset_gpios->ndescs; i++)
40 			values[i] = value;
41 
42 		gpiod_set_array_value_cansleep(
43 			reset_gpios->ndescs, reset_gpios->desc, values);
44 	}
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 
51 	if (!IS_ERR(pwrseq->ext_clk) && !pwrseq->clk_enabled) {
52 		clk_prepare_enable(pwrseq->ext_clk);
53 		pwrseq->clk_enabled = true;
54 	}
55 
56 	mmc_pwrseq_simple_set_gpios_value(pwrseq, 1);
57 }
58 
59 static void mmc_pwrseq_simple_post_power_on(struct mmc_host *host)
60 {
61 	struct mmc_pwrseq_simple *pwrseq = to_pwrseq_simple(host->pwrseq);
62 
63 	mmc_pwrseq_simple_set_gpios_value(pwrseq, 0);
64 }
65 
66 static void mmc_pwrseq_simple_power_off(struct mmc_host *host)
67 {
68 	struct mmc_pwrseq_simple *pwrseq = to_pwrseq_simple(host->pwrseq);
69 
70 	mmc_pwrseq_simple_set_gpios_value(pwrseq, 1);
71 
72 	if (!IS_ERR(pwrseq->ext_clk) && pwrseq->clk_enabled) {
73 		clk_disable_unprepare(pwrseq->ext_clk);
74 		pwrseq->clk_enabled = false;
75 	}
76 }
77 
78 static void mmc_pwrseq_simple_free(struct mmc_host *host)
79 {
80 	struct mmc_pwrseq_simple *pwrseq = to_pwrseq_simple(host->pwrseq);
81 
82 	if (!IS_ERR(pwrseq->reset_gpios))
83 		gpiod_put_array(pwrseq->reset_gpios);
84 
85 	if (!IS_ERR(pwrseq->ext_clk))
86 		clk_put(pwrseq->ext_clk);
87 
88 	kfree(pwrseq);
89 }
90 
91 static const struct mmc_pwrseq_ops mmc_pwrseq_simple_ops = {
92 	.pre_power_on = mmc_pwrseq_simple_pre_power_on,
93 	.post_power_on = mmc_pwrseq_simple_post_power_on,
94 	.power_off = mmc_pwrseq_simple_power_off,
95 	.free = mmc_pwrseq_simple_free,
96 };
97 
98 struct mmc_pwrseq *mmc_pwrseq_simple_alloc(struct mmc_host *host,
99 					   struct device *dev)
100 {
101 	struct mmc_pwrseq_simple *pwrseq;
102 	int ret = 0;
103 
104 	pwrseq = kzalloc(sizeof(*pwrseq), GFP_KERNEL);
105 	if (!pwrseq)
106 		return ERR_PTR(-ENOMEM);
107 
108 	pwrseq->ext_clk = clk_get(dev, "ext_clock");
109 	if (IS_ERR(pwrseq->ext_clk) &&
110 	    PTR_ERR(pwrseq->ext_clk) != -ENOENT) {
111 		ret = PTR_ERR(pwrseq->ext_clk);
112 		goto free;
113 	}
114 
115 	pwrseq->reset_gpios = gpiod_get_array(dev, "reset", GPIOD_OUT_HIGH);
116 	if (IS_ERR(pwrseq->reset_gpios) &&
117 	    PTR_ERR(pwrseq->reset_gpios) != -ENOENT &&
118 	    PTR_ERR(pwrseq->reset_gpios) != -ENOSYS) {
119 		ret = PTR_ERR(pwrseq->reset_gpios);
120 		goto clk_put;
121 	}
122 
123 	pwrseq->pwrseq.ops = &mmc_pwrseq_simple_ops;
124 
125 	return &pwrseq->pwrseq;
126 clk_put:
127 	if (!IS_ERR(pwrseq->ext_clk))
128 		clk_put(pwrseq->ext_clk);
129 free:
130 	kfree(pwrseq);
131 	return ERR_PTR(ret);
132 }
133