1 // SPDX-License-Identifier: GPL-2.0
3 // Regulator Driver for Freescale MC13xxx PMIC
5 // Copyright 2010 Yong Shen <yong.shen@linaro.org>
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>
11 // Regs infos taken from mc13xxx drivers from freescale and mc13xxx.pdf file
14 #include <linux/mfd/mc13xxx.h>
15 #include <linux/regulator/machine.h>
16 #include <linux/regulator/driver.h>
17 #include <linux/regulator/of_regulator.h>
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>
23 #include <linux/module.h>
27 static int mc13xxx_regulator_enable(struct regulator_dev
*rdev
)
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
);
33 dev_dbg(rdev_get_dev(rdev
), "%s id: %d\n", __func__
, id
);
35 return mc13xxx_reg_rmw(priv
->mc13xxx
, mc13xxx_regulators
[id
].reg
,
36 mc13xxx_regulators
[id
].enable_bit
,
37 mc13xxx_regulators
[id
].enable_bit
);
40 static int mc13xxx_regulator_disable(struct regulator_dev
*rdev
)
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
);
46 dev_dbg(rdev_get_dev(rdev
), "%s id: %d\n", __func__
, id
);
48 return mc13xxx_reg_rmw(priv
->mc13xxx
, mc13xxx_regulators
[id
].reg
,
49 mc13xxx_regulators
[id
].enable_bit
, 0);
52 static int mc13xxx_regulator_is_enabled(struct regulator_dev
*rdev
)
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
);
59 ret
= mc13xxx_reg_read(priv
->mc13xxx
, mc13xxx_regulators
[id
].reg
, &val
);
63 return (val
& mc13xxx_regulators
[id
].enable_bit
) != 0;
66 static int mc13xxx_regulator_set_voltage_sel(struct regulator_dev
*rdev
,
69 struct mc13xxx_regulator_priv
*priv
= rdev_get_drvdata(rdev
);
70 struct mc13xxx_regulator
*mc13xxx_regulators
= priv
->mc13xxx_regulators
;
71 int id
= rdev_get_id(rdev
);
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
);
78 static int mc13xxx_regulator_get_voltage(struct regulator_dev
*rdev
)
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
);
85 dev_dbg(rdev_get_dev(rdev
), "%s id: %d\n", __func__
, id
);
87 ret
= mc13xxx_reg_read(priv
->mc13xxx
,
88 mc13xxx_regulators
[id
].vsel_reg
, &val
);
92 val
= (val
& mc13xxx_regulators
[id
].vsel_mask
)
93 >> mc13xxx_regulators
[id
].vsel_shift
;
95 dev_dbg(rdev_get_dev(rdev
), "%s id: %d val: %d\n", __func__
, id
, val
);
97 BUG_ON(val
>= mc13xxx_regulators
[id
].desc
.n_voltages
);
99 return rdev
->desc
->volt_table
[val
];
102 const struct regulator_ops mc13xxx_regulator_ops
= {
103 .enable
= mc13xxx_regulator_enable
,
104 .disable
= mc13xxx_regulator_disable
,
105 .is_enabled
= mc13xxx_regulator_is_enabled
,
106 .list_voltage
= regulator_list_voltage_table
,
107 .set_voltage_sel
= mc13xxx_regulator_set_voltage_sel
,
108 .get_voltage
= mc13xxx_regulator_get_voltage
,
110 EXPORT_SYMBOL_GPL(mc13xxx_regulator_ops
);
112 int mc13xxx_fixed_regulator_set_voltage(struct regulator_dev
*rdev
, int min_uV
,
113 int max_uV
, unsigned *selector
)
115 int id
= rdev_get_id(rdev
);
117 dev_dbg(rdev_get_dev(rdev
), "%s id: %d min_uV: %d max_uV: %d\n",
118 __func__
, id
, min_uV
, max_uV
);
120 if (min_uV
<= rdev
->desc
->volt_table
[0] &&
121 rdev
->desc
->volt_table
[0] <= max_uV
) {
128 EXPORT_SYMBOL_GPL(mc13xxx_fixed_regulator_set_voltage
);
130 const struct regulator_ops mc13xxx_fixed_regulator_ops
= {
131 .enable
= mc13xxx_regulator_enable
,
132 .disable
= mc13xxx_regulator_disable
,
133 .is_enabled
= mc13xxx_regulator_is_enabled
,
134 .list_voltage
= regulator_list_voltage_table
,
135 .set_voltage
= mc13xxx_fixed_regulator_set_voltage
,
137 EXPORT_SYMBOL_GPL(mc13xxx_fixed_regulator_ops
);
140 int mc13xxx_get_num_regulators_dt(struct platform_device
*pdev
)
142 struct device_node
*parent
;
145 if (!pdev
->dev
.parent
->of_node
)
148 parent
= of_get_child_by_name(pdev
->dev
.parent
->of_node
, "regulators");
152 num
= of_get_child_count(parent
);
156 EXPORT_SYMBOL_GPL(mc13xxx_get_num_regulators_dt
);
158 struct mc13xxx_regulator_init_data
*mc13xxx_parse_regulators_dt(
159 struct platform_device
*pdev
, struct mc13xxx_regulator
*regulators
,
162 struct mc13xxx_regulator_priv
*priv
= platform_get_drvdata(pdev
);
163 struct mc13xxx_regulator_init_data
*data
, *p
;
164 struct device_node
*parent
, *child
;
167 if (!pdev
->dev
.parent
->of_node
)
170 parent
= of_get_child_by_name(pdev
->dev
.parent
->of_node
, "regulators");
174 data
= devm_kcalloc(&pdev
->dev
, priv
->num_regulators
, sizeof(*data
),
183 for_each_child_of_node(parent
, child
) {
186 for (i
= 0; i
< num_regulators
; i
++) {
187 if (!regulators
[i
].desc
.name
)
189 if (of_node_name_eq(child
,
190 regulators
[i
].desc
.name
)) {
192 p
->init_data
= of_get_regulator_init_data(
194 ®ulators
[i
].desc
);
206 "Unknown regulator: %pOFn\n", child
);
210 priv
->num_regulators
= parsed
;
214 EXPORT_SYMBOL_GPL(mc13xxx_parse_regulators_dt
);
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");