2 * Copyright (C) 2011 Freescale Semiconductor, Inc. All Rights Reserved.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #include <linux/slab.h>
22 #include <linux/device.h>
23 #include <linux/module.h>
24 #include <linux/err.h>
26 #include <linux/platform_device.h>
28 #include <linux/of_address.h>
29 #include <linux/mfd/anatop.h>
30 #include <linux/regulator/driver.h>
31 #include <linux/regulator/of_regulator.h>
33 struct anatop_regulator
{
42 struct regulator_desc rdesc
;
43 struct regulator_init_data
*initdata
;
46 static int anatop_set_voltage(struct regulator_dev
*reg
, int min_uV
,
47 int max_uV
, unsigned *selector
)
49 struct anatop_regulator
*anatop_reg
= rdev_get_drvdata(reg
);
54 dev_dbg(®
->dev
, "%s: uv %d, min %d, max %d\n", __func__
,
55 uv
, anatop_reg
->min_voltage
,
56 anatop_reg
->max_voltage
);
58 if (uv
< anatop_reg
->min_voltage
) {
59 if (max_uV
> anatop_reg
->min_voltage
)
60 uv
= anatop_reg
->min_voltage
;
65 if (!anatop_reg
->control_reg
)
68 sel
= DIV_ROUND_UP(uv
- anatop_reg
->min_voltage
, 25000);
69 if (sel
* 25000 + anatop_reg
->min_voltage
> anatop_reg
->max_voltage
)
71 val
= anatop_reg
->min_bit_val
+ sel
;
73 dev_dbg(®
->dev
, "%s: calculated val %d\n", __func__
, val
);
74 mask
= ((1 << anatop_reg
->vol_bit_width
) - 1) <<
75 anatop_reg
->vol_bit_shift
;
76 val
<<= anatop_reg
->vol_bit_shift
;
77 anatop_write_reg(anatop_reg
->mfd
, anatop_reg
->control_reg
, val
, mask
);
82 static int anatop_get_voltage_sel(struct regulator_dev
*reg
)
84 struct anatop_regulator
*anatop_reg
= rdev_get_drvdata(reg
);
87 if (!anatop_reg
->control_reg
)
90 val
= anatop_read_reg(anatop_reg
->mfd
, anatop_reg
->control_reg
);
91 val
= (val
& ((1 << anatop_reg
->vol_bit_width
) - 1)) >>
92 anatop_reg
->vol_bit_shift
;
94 return val
- anatop_reg
->min_bit_val
;
97 static int anatop_list_voltage(struct regulator_dev
*reg
, unsigned selector
)
99 struct anatop_regulator
*anatop_reg
= rdev_get_drvdata(reg
);
102 uv
= anatop_reg
->min_voltage
+ selector
* 25000;
103 dev_dbg(®
->dev
, "vddio = %d, selector = %u\n", uv
, selector
);
108 static struct regulator_ops anatop_rops
= {
109 .set_voltage
= anatop_set_voltage
,
110 .get_voltage_sel
= anatop_get_voltage_sel
,
111 .list_voltage
= anatop_list_voltage
,
114 static int __devinit
anatop_regulator_probe(struct platform_device
*pdev
)
116 struct device
*dev
= &pdev
->dev
;
117 struct device_node
*np
= dev
->of_node
;
118 struct regulator_desc
*rdesc
;
119 struct regulator_dev
*rdev
;
120 struct anatop_regulator
*sreg
;
121 struct regulator_init_data
*initdata
;
122 struct anatop
*anatopmfd
= dev_get_drvdata(pdev
->dev
.parent
);
123 struct regulator_config config
= { };
126 initdata
= of_get_regulator_init_data(dev
, np
);
127 sreg
= devm_kzalloc(dev
, sizeof(*sreg
), GFP_KERNEL
);
130 sreg
->initdata
= initdata
;
131 sreg
->name
= kstrdup(of_get_property(np
, "regulator-name", NULL
),
133 rdesc
= &sreg
->rdesc
;
134 memset(rdesc
, 0, sizeof(*rdesc
));
135 rdesc
->name
= sreg
->name
;
136 rdesc
->ops
= &anatop_rops
;
137 rdesc
->type
= REGULATOR_VOLTAGE
;
138 rdesc
->owner
= THIS_MODULE
;
139 sreg
->mfd
= anatopmfd
;
140 ret
= of_property_read_u32(np
, "anatop-reg-offset",
143 dev_err(dev
, "no anatop-reg-offset property set\n");
144 goto anatop_probe_end
;
146 ret
= of_property_read_u32(np
, "anatop-vol-bit-width",
147 &sreg
->vol_bit_width
);
149 dev_err(dev
, "no anatop-vol-bit-width property set\n");
150 goto anatop_probe_end
;
152 ret
= of_property_read_u32(np
, "anatop-vol-bit-shift",
153 &sreg
->vol_bit_shift
);
155 dev_err(dev
, "no anatop-vol-bit-shift property set\n");
156 goto anatop_probe_end
;
158 ret
= of_property_read_u32(np
, "anatop-min-bit-val",
161 dev_err(dev
, "no anatop-min-bit-val property set\n");
162 goto anatop_probe_end
;
164 ret
= of_property_read_u32(np
, "anatop-min-voltage",
167 dev_err(dev
, "no anatop-min-voltage property set\n");
168 goto anatop_probe_end
;
170 ret
= of_property_read_u32(np
, "anatop-max-voltage",
173 dev_err(dev
, "no anatop-max-voltage property set\n");
174 goto anatop_probe_end
;
177 rdesc
->n_voltages
= (sreg
->max_voltage
- sreg
->min_voltage
)
180 config
.dev
= &pdev
->dev
;
181 config
.init_data
= initdata
;
182 config
.driver_data
= sreg
;
183 config
.of_node
= pdev
->dev
.of_node
;
185 /* register regulator */
186 rdev
= regulator_register(rdesc
, &config
);
188 dev_err(dev
, "failed to register %s\n",
191 goto anatop_probe_end
;
194 platform_set_drvdata(pdev
, rdev
);
203 static int __devexit
anatop_regulator_remove(struct platform_device
*pdev
)
205 struct regulator_dev
*rdev
= platform_get_drvdata(pdev
);
206 struct anatop_regulator
*sreg
= rdev_get_drvdata(rdev
);
207 const char *name
= sreg
->name
;
209 regulator_unregister(rdev
);
215 static struct of_device_id __devinitdata of_anatop_regulator_match_tbl
[] = {
216 { .compatible
= "fsl,anatop-regulator", },
220 static struct platform_driver anatop_regulator_driver
= {
222 .name
= "anatop_regulator",
223 .owner
= THIS_MODULE
,
224 .of_match_table
= of_anatop_regulator_match_tbl
,
226 .probe
= anatop_regulator_probe
,
227 .remove
= anatop_regulator_remove
,
230 static int __init
anatop_regulator_init(void)
232 return platform_driver_register(&anatop_regulator_driver
);
234 postcore_initcall(anatop_regulator_init
);
236 static void __exit
anatop_regulator_exit(void)
238 platform_driver_unregister(&anatop_regulator_driver
);
240 module_exit(anatop_regulator_exit
);
242 MODULE_AUTHOR("Nancy Chen <Nancy.Chen@freescale.com>, "
243 "Ying-Chun Liu (PaulLiu) <paul.liu@linaro.org>");
244 MODULE_DESCRIPTION("ANATOP Regulator driver");
245 MODULE_LICENSE("GPL v2");