2 * Regulator Driver for Freescale MC13xxx PMIC
4 * Copyright 2010 Yong Shen <yong.shen@linaro.org>
6 * Based on mc13783 regulator driver :
7 * Copyright (C) 2008 Sascha Hauer, Pengutronix <s.hauer@pengutronix.de>
8 * Copyright 2009 Alberto Panizzo <maramaopercheseimorto@gmail.com>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
14 * Regs infos taken from mc13xxx drivers from freescale and mc13xxx.pdf file
18 #include <linux/mfd/mc13xxx.h>
19 #include <linux/regulator/machine.h>
20 #include <linux/regulator/driver.h>
21 #include <linux/regulator/of_regulator.h>
22 #include <linux/platform_device.h>
23 #include <linux/kernel.h>
24 #include <linux/slab.h>
25 #include <linux/init.h>
26 #include <linux/err.h>
27 #include <linux/module.h>
31 static int mc13xxx_regulator_enable(struct regulator_dev
*rdev
)
33 struct mc13xxx_regulator_priv
*priv
= rdev_get_drvdata(rdev
);
34 struct mc13xxx_regulator
*mc13xxx_regulators
= priv
->mc13xxx_regulators
;
35 int id
= rdev_get_id(rdev
);
37 dev_dbg(rdev_get_dev(rdev
), "%s id: %d\n", __func__
, id
);
39 return mc13xxx_reg_rmw(priv
->mc13xxx
, mc13xxx_regulators
[id
].reg
,
40 mc13xxx_regulators
[id
].enable_bit
,
41 mc13xxx_regulators
[id
].enable_bit
);
44 static int mc13xxx_regulator_disable(struct regulator_dev
*rdev
)
46 struct mc13xxx_regulator_priv
*priv
= rdev_get_drvdata(rdev
);
47 struct mc13xxx_regulator
*mc13xxx_regulators
= priv
->mc13xxx_regulators
;
48 int id
= rdev_get_id(rdev
);
50 dev_dbg(rdev_get_dev(rdev
), "%s id: %d\n", __func__
, id
);
52 return mc13xxx_reg_rmw(priv
->mc13xxx
, mc13xxx_regulators
[id
].reg
,
53 mc13xxx_regulators
[id
].enable_bit
, 0);
56 static int mc13xxx_regulator_is_enabled(struct regulator_dev
*rdev
)
58 struct mc13xxx_regulator_priv
*priv
= rdev_get_drvdata(rdev
);
59 struct mc13xxx_regulator
*mc13xxx_regulators
= priv
->mc13xxx_regulators
;
60 int ret
, id
= rdev_get_id(rdev
);
63 ret
= mc13xxx_reg_read(priv
->mc13xxx
, mc13xxx_regulators
[id
].reg
, &val
);
67 return (val
& mc13xxx_regulators
[id
].enable_bit
) != 0;
70 static int mc13xxx_regulator_set_voltage_sel(struct regulator_dev
*rdev
,
73 struct mc13xxx_regulator_priv
*priv
= rdev_get_drvdata(rdev
);
74 struct mc13xxx_regulator
*mc13xxx_regulators
= priv
->mc13xxx_regulators
;
75 int id
= rdev_get_id(rdev
);
77 return mc13xxx_reg_rmw(priv
->mc13xxx
, mc13xxx_regulators
[id
].vsel_reg
,
78 mc13xxx_regulators
[id
].vsel_mask
,
79 selector
<< mc13xxx_regulators
[id
].vsel_shift
);
82 static int mc13xxx_regulator_get_voltage(struct regulator_dev
*rdev
)
84 struct mc13xxx_regulator_priv
*priv
= rdev_get_drvdata(rdev
);
85 struct mc13xxx_regulator
*mc13xxx_regulators
= priv
->mc13xxx_regulators
;
86 int ret
, id
= rdev_get_id(rdev
);
89 dev_dbg(rdev_get_dev(rdev
), "%s id: %d\n", __func__
, id
);
91 ret
= mc13xxx_reg_read(priv
->mc13xxx
,
92 mc13xxx_regulators
[id
].vsel_reg
, &val
);
96 val
= (val
& mc13xxx_regulators
[id
].vsel_mask
)
97 >> mc13xxx_regulators
[id
].vsel_shift
;
99 dev_dbg(rdev_get_dev(rdev
), "%s id: %d val: %d\n", __func__
, id
, val
);
101 BUG_ON(val
>= mc13xxx_regulators
[id
].desc
.n_voltages
);
103 return rdev
->desc
->volt_table
[val
];
106 struct regulator_ops mc13xxx_regulator_ops
= {
107 .enable
= mc13xxx_regulator_enable
,
108 .disable
= mc13xxx_regulator_disable
,
109 .is_enabled
= mc13xxx_regulator_is_enabled
,
110 .list_voltage
= regulator_list_voltage_table
,
111 .set_voltage_sel
= mc13xxx_regulator_set_voltage_sel
,
112 .get_voltage
= mc13xxx_regulator_get_voltage
,
114 EXPORT_SYMBOL_GPL(mc13xxx_regulator_ops
);
116 int mc13xxx_fixed_regulator_set_voltage(struct regulator_dev
*rdev
, int min_uV
,
117 int max_uV
, unsigned *selector
)
119 int id
= rdev_get_id(rdev
);
121 dev_dbg(rdev_get_dev(rdev
), "%s id: %d min_uV: %d max_uV: %d\n",
122 __func__
, id
, min_uV
, max_uV
);
124 if (min_uV
<= rdev
->desc
->volt_table
[0] &&
125 rdev
->desc
->volt_table
[0] <= max_uV
) {
132 EXPORT_SYMBOL_GPL(mc13xxx_fixed_regulator_set_voltage
);
134 struct regulator_ops mc13xxx_fixed_regulator_ops
= {
135 .enable
= mc13xxx_regulator_enable
,
136 .disable
= mc13xxx_regulator_disable
,
137 .is_enabled
= mc13xxx_regulator_is_enabled
,
138 .list_voltage
= regulator_list_voltage_table
,
139 .set_voltage
= mc13xxx_fixed_regulator_set_voltage
,
141 EXPORT_SYMBOL_GPL(mc13xxx_fixed_regulator_ops
);
144 int mc13xxx_get_num_regulators_dt(struct platform_device
*pdev
)
146 struct device_node
*parent
;
149 if (!pdev
->dev
.parent
->of_node
)
152 parent
= of_get_child_by_name(pdev
->dev
.parent
->of_node
, "regulators");
156 num
= of_get_child_count(parent
);
160 EXPORT_SYMBOL_GPL(mc13xxx_get_num_regulators_dt
);
162 struct mc13xxx_regulator_init_data
*mc13xxx_parse_regulators_dt(
163 struct platform_device
*pdev
, struct mc13xxx_regulator
*regulators
,
166 struct mc13xxx_regulator_priv
*priv
= platform_get_drvdata(pdev
);
167 struct mc13xxx_regulator_init_data
*data
, *p
;
168 struct device_node
*parent
, *child
;
171 if (!pdev
->dev
.parent
->of_node
)
174 parent
= of_get_child_by_name(pdev
->dev
.parent
->of_node
, "regulators");
178 data
= devm_kzalloc(&pdev
->dev
, sizeof(*data
) * priv
->num_regulators
,
187 for_each_child_of_node(parent
, child
) {
190 for (i
= 0; i
< num_regulators
; i
++) {
191 if (!regulators
[i
].desc
.name
)
193 if (!of_node_cmp(child
->name
,
194 regulators
[i
].desc
.name
)) {
196 p
->init_data
= of_get_regulator_init_data(
198 ®ulators
[i
].desc
);
210 "Unknown regulator: %s\n", child
->name
);
214 priv
->num_regulators
= parsed
;
218 EXPORT_SYMBOL_GPL(mc13xxx_parse_regulators_dt
);
221 MODULE_LICENSE("GPL v2");
222 MODULE_AUTHOR("Yong Shen <yong.shen@linaro.org>");
223 MODULE_DESCRIPTION("Regulator Driver for Freescale MC13xxx PMIC");
224 MODULE_ALIAS("mc13xxx-regulator-core");