Commit | Line | Data |
---|---|---|
910adc0e FE |
1 | // SPDX-License-Identifier: GPL-2.0 |
2 | // | |
3 | // Regulator Driver for Freescale MC13xxx PMIC | |
4 | // | |
5 | // Copyright 2010 Yong Shen <yong.shen@linaro.org> | |
6 | // | |
7 | // Based on mc13783 regulator driver : | |
8 | // Copyright (C) 2008 Sascha Hauer, Pengutronix <s.hauer@pengutronix.de> | |
9 | // Copyright 2009 Alberto Panizzo <maramaopercheseimorto@gmail.com> | |
10 | // | |
11 | // Regs infos taken from mc13xxx drivers from freescale and mc13xxx.pdf file | |
12 | // from freescale | |
167e3d8a YS |
13 | |
14 | #include <linux/mfd/mc13xxx.h> | |
15 | #include <linux/regulator/machine.h> | |
16 | #include <linux/regulator/driver.h> | |
93bcb23b | 17 | #include <linux/regulator/of_regulator.h> |
167e3d8a YS |
18 | #include <linux/platform_device.h> |
19 | #include <linux/kernel.h> | |
20 | #include <linux/slab.h> | |
21 | #include <linux/init.h> | |
22 | #include <linux/err.h> | |
65602c32 | 23 | #include <linux/module.h> |
93bcb23b | 24 | #include <linux/of.h> |
167e3d8a YS |
25 | #include "mc13xxx.h" |
26 | ||
27 | static int mc13xxx_regulator_enable(struct regulator_dev *rdev) | |
28 | { | |
29 | struct mc13xxx_regulator_priv *priv = rdev_get_drvdata(rdev); | |
30 | struct mc13xxx_regulator *mc13xxx_regulators = priv->mc13xxx_regulators; | |
31 | int id = rdev_get_id(rdev); | |
167e3d8a YS |
32 | |
33 | dev_dbg(rdev_get_dev(rdev), "%s id: %d\n", __func__, id); | |
34 | ||
7dd33c1d AS |
35 | return mc13xxx_reg_rmw(priv->mc13xxx, mc13xxx_regulators[id].reg, |
36 | mc13xxx_regulators[id].enable_bit, | |
37 | mc13xxx_regulators[id].enable_bit); | |
167e3d8a YS |
38 | } |
39 | ||
40 | static int mc13xxx_regulator_disable(struct regulator_dev *rdev) | |
41 | { | |
42 | struct mc13xxx_regulator_priv *priv = rdev_get_drvdata(rdev); | |
43 | struct mc13xxx_regulator *mc13xxx_regulators = priv->mc13xxx_regulators; | |
44 | int id = rdev_get_id(rdev); | |
167e3d8a YS |
45 | |
46 | dev_dbg(rdev_get_dev(rdev), "%s id: %d\n", __func__, id); | |
47 | ||
7dd33c1d AS |
48 | return mc13xxx_reg_rmw(priv->mc13xxx, mc13xxx_regulators[id].reg, |
49 | mc13xxx_regulators[id].enable_bit, 0); | |
167e3d8a YS |
50 | } |
51 | ||
52 | static int mc13xxx_regulator_is_enabled(struct regulator_dev *rdev) | |
53 | { | |
54 | struct mc13xxx_regulator_priv *priv = rdev_get_drvdata(rdev); | |
55 | struct mc13xxx_regulator *mc13xxx_regulators = priv->mc13xxx_regulators; | |
56 | int ret, id = rdev_get_id(rdev); | |
57 | unsigned int val; | |
58 | ||
167e3d8a | 59 | ret = mc13xxx_reg_read(priv->mc13xxx, mc13xxx_regulators[id].reg, &val); |
167e3d8a YS |
60 | if (ret) |
61 | return ret; | |
62 | ||
63 | return (val & mc13xxx_regulators[id].enable_bit) != 0; | |
64 | } | |
65 | ||
939b62d8 AL |
66 | static int mc13xxx_regulator_set_voltage_sel(struct regulator_dev *rdev, |
67 | unsigned selector) | |
167e3d8a YS |
68 | { |
69 | struct mc13xxx_regulator_priv *priv = rdev_get_drvdata(rdev); | |
70 | struct mc13xxx_regulator *mc13xxx_regulators = priv->mc13xxx_regulators; | |
939b62d8 | 71 | int id = rdev_get_id(rdev); |
167e3d8a | 72 | |
7dd33c1d AS |
73 | return mc13xxx_reg_rmw(priv->mc13xxx, mc13xxx_regulators[id].vsel_reg, |
74 | mc13xxx_regulators[id].vsel_mask, | |
75 | selector << mc13xxx_regulators[id].vsel_shift); | |
167e3d8a YS |
76 | } |
77 | ||
78 | static int mc13xxx_regulator_get_voltage(struct regulator_dev *rdev) | |
79 | { | |
80 | struct mc13xxx_regulator_priv *priv = rdev_get_drvdata(rdev); | |
81 | struct mc13xxx_regulator *mc13xxx_regulators = priv->mc13xxx_regulators; | |
82 | int ret, id = rdev_get_id(rdev); | |
83 | unsigned int val; | |
84 | ||
85 | dev_dbg(rdev_get_dev(rdev), "%s id: %d\n", __func__, id); | |
86 | ||
167e3d8a YS |
87 | ret = mc13xxx_reg_read(priv->mc13xxx, |
88 | mc13xxx_regulators[id].vsel_reg, &val); | |
167e3d8a YS |
89 | if (ret) |
90 | return ret; | |
91 | ||
92 | val = (val & mc13xxx_regulators[id].vsel_mask) | |
93 | >> mc13xxx_regulators[id].vsel_shift; | |
94 | ||
95 | dev_dbg(rdev_get_dev(rdev), "%s id: %d val: %d\n", __func__, id, val); | |
96 | ||
3c24019d | 97 | BUG_ON(val >= mc13xxx_regulators[id].desc.n_voltages); |
167e3d8a | 98 | |
34e74f39 | 99 | return rdev->desc->volt_table[val]; |
167e3d8a YS |
100 | } |
101 | ||
e5680c4d | 102 | const struct regulator_ops mc13xxx_regulator_ops = { |
167e3d8a YS |
103 | .enable = mc13xxx_regulator_enable, |
104 | .disable = mc13xxx_regulator_disable, | |
105 | .is_enabled = mc13xxx_regulator_is_enabled, | |
34e74f39 | 106 | .list_voltage = regulator_list_voltage_table, |
939b62d8 | 107 | .set_voltage_sel = mc13xxx_regulator_set_voltage_sel, |
167e3d8a YS |
108 | .get_voltage = mc13xxx_regulator_get_voltage, |
109 | }; | |
4d7071f1 | 110 | EXPORT_SYMBOL_GPL(mc13xxx_regulator_ops); |
167e3d8a YS |
111 | |
112 | int mc13xxx_fixed_regulator_set_voltage(struct regulator_dev *rdev, int min_uV, | |
113 | int max_uV, unsigned *selector) | |
114 | { | |
167e3d8a YS |
115 | int id = rdev_get_id(rdev); |
116 | ||
117 | dev_dbg(rdev_get_dev(rdev), "%s id: %d min_uV: %d max_uV: %d\n", | |
118 | __func__, id, min_uV, max_uV); | |
119 | ||
34e74f39 | 120 | if (min_uV <= rdev->desc->volt_table[0] && |
13407ea8 AL |
121 | rdev->desc->volt_table[0] <= max_uV) { |
122 | *selector = 0; | |
167e3d8a | 123 | return 0; |
13407ea8 | 124 | } else { |
167e3d8a | 125 | return -EINVAL; |
13407ea8 | 126 | } |
167e3d8a | 127 | } |
4d7071f1 | 128 | EXPORT_SYMBOL_GPL(mc13xxx_fixed_regulator_set_voltage); |
167e3d8a | 129 | |
e5680c4d | 130 | const struct regulator_ops mc13xxx_fixed_regulator_ops = { |
167e3d8a YS |
131 | .enable = mc13xxx_regulator_enable, |
132 | .disable = mc13xxx_regulator_disable, | |
133 | .is_enabled = mc13xxx_regulator_is_enabled, | |
34e74f39 | 134 | .list_voltage = regulator_list_voltage_table, |
167e3d8a | 135 | .set_voltage = mc13xxx_fixed_regulator_set_voltage, |
167e3d8a | 136 | }; |
4d7071f1 | 137 | EXPORT_SYMBOL_GPL(mc13xxx_fixed_regulator_ops); |
167e3d8a | 138 | |
93bcb23b | 139 | #ifdef CONFIG_OF |
a5023574 | 140 | int mc13xxx_get_num_regulators_dt(struct platform_device *pdev) |
93bcb23b | 141 | { |
86f66733 AL |
142 | struct device_node *parent; |
143 | int num; | |
93bcb23b | 144 | |
b431e69a | 145 | if (!pdev->dev.parent->of_node) |
bf7f882b SK |
146 | return -ENODEV; |
147 | ||
b431e69a | 148 | parent = of_get_child_by_name(pdev->dev.parent->of_node, "regulators"); |
93bcb23b SG |
149 | if (!parent) |
150 | return -ENODEV; | |
151 | ||
86f66733 | 152 | num = of_get_child_count(parent); |
c92f5dd2 | 153 | of_node_put(parent); |
93bcb23b SG |
154 | return num; |
155 | } | |
53269163 | 156 | EXPORT_SYMBOL_GPL(mc13xxx_get_num_regulators_dt); |
93bcb23b | 157 | |
a5023574 | 158 | struct mc13xxx_regulator_init_data *mc13xxx_parse_regulators_dt( |
93bcb23b | 159 | struct platform_device *pdev, struct mc13xxx_regulator *regulators, |
eb0d8e7a | 160 | int num_regulators) |
93bcb23b SG |
161 | { |
162 | struct mc13xxx_regulator_priv *priv = platform_get_drvdata(pdev); | |
163 | struct mc13xxx_regulator_init_data *data, *p; | |
164 | struct device_node *parent, *child; | |
2c8a5dca MS |
165 | int i, parsed = 0; |
166 | ||
b431e69a | 167 | if (!pdev->dev.parent->of_node) |
bf7f882b SK |
168 | return NULL; |
169 | ||
b431e69a | 170 | parent = of_get_child_by_name(pdev->dev.parent->of_node, "regulators"); |
93bcb23b SG |
171 | if (!parent) |
172 | return NULL; | |
173 | ||
a86854d0 | 174 | data = devm_kcalloc(&pdev->dev, priv->num_regulators, sizeof(*data), |
93bcb23b | 175 | GFP_KERNEL); |
c92f5dd2 AL |
176 | if (!data) { |
177 | of_node_put(parent); | |
93bcb23b | 178 | return NULL; |
c92f5dd2 AL |
179 | } |
180 | ||
93bcb23b SG |
181 | p = data; |
182 | ||
183 | for_each_child_of_node(parent, child) { | |
eb0d8e7a AS |
184 | int found = 0; |
185 | ||
93bcb23b | 186 | for (i = 0; i < num_regulators; i++) { |
eb0d8e7a AS |
187 | if (!regulators[i].desc.name) |
188 | continue; | |
c32569e3 | 189 | if (of_node_name_eq(child, |
93bcb23b SG |
190 | regulators[i].desc.name)) { |
191 | p->id = i; | |
192 | p->init_data = of_get_regulator_init_data( | |
072e78b1 JMC |
193 | &pdev->dev, child, |
194 | ®ulators[i].desc); | |
93bcb23b SG |
195 | p->node = child; |
196 | p++; | |
2c8a5dca MS |
197 | |
198 | parsed++; | |
eb0d8e7a | 199 | found = 1; |
93bcb23b SG |
200 | break; |
201 | } | |
202 | } | |
eb0d8e7a AS |
203 | |
204 | if (!found) | |
205 | dev_warn(&pdev->dev, | |
0c9721a5 | 206 | "Unknown regulator: %pOFn\n", child); |
93bcb23b | 207 | } |
c92f5dd2 | 208 | of_node_put(parent); |
93bcb23b | 209 | |
eb0d8e7a AS |
210 | priv->num_regulators = parsed; |
211 | ||
93bcb23b SG |
212 | return data; |
213 | } | |
53269163 | 214 | EXPORT_SYMBOL_GPL(mc13xxx_parse_regulators_dt); |
93bcb23b SG |
215 | #endif |
216 | ||
167e3d8a YS |
217 | MODULE_LICENSE("GPL v2"); |
218 | MODULE_AUTHOR("Yong Shen <yong.shen@linaro.org>"); | |
219 | MODULE_DESCRIPTION("Regulator Driver for Freescale MC13xxx PMIC"); | |
220 | MODULE_ALIAS("mc13xxx-regulator-core"); |