Ying-Chun Liu (PaulLiu) | e3e5aff | 2012-03-14 10:29:12 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 Freescale Semiconductor, Inc. All Rights Reserved. |
| 3 | */ |
| 4 | |
| 5 | /* |
| 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. |
| 10 | |
| 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. |
| 15 | |
| 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. |
| 19 | */ |
| 20 | |
| 21 | #include <linux/slab.h> |
| 22 | #include <linux/device.h> |
| 23 | #include <linux/module.h> |
Dong Aisheng | baa6415 | 2012-09-05 10:57:15 +0800 | [diff] [blame] | 24 | #include <linux/mfd/syscon.h> |
Ying-Chun Liu (PaulLiu) | e3e5aff | 2012-03-14 10:29:12 +0800 | [diff] [blame] | 25 | #include <linux/err.h> |
| 26 | #include <linux/io.h> |
| 27 | #include <linux/platform_device.h> |
| 28 | #include <linux/of.h> |
| 29 | #include <linux/of_address.h> |
Dong Aisheng | baa6415 | 2012-09-05 10:57:15 +0800 | [diff] [blame] | 30 | #include <linux/regmap.h> |
Ying-Chun Liu (PaulLiu) | e3e5aff | 2012-03-14 10:29:12 +0800 | [diff] [blame] | 31 | #include <linux/regulator/driver.h> |
| 32 | #include <linux/regulator/of_regulator.h> |
| 33 | |
Anson Huang | 9ee417c | 2013-01-31 11:23:53 -0500 | [diff] [blame] | 34 | #define LDO_RAMP_UP_UNIT_IN_CYCLES 64 /* 64 cycles per step */ |
| 35 | #define LDO_RAMP_UP_FREQ_IN_MHZ 24 /* cycle based on 24M OSC */ |
| 36 | |
Ying-Chun Liu (PaulLiu) | e3e5aff | 2012-03-14 10:29:12 +0800 | [diff] [blame] | 37 | struct anatop_regulator { |
| 38 | const char *name; |
| 39 | u32 control_reg; |
Dong Aisheng | baa6415 | 2012-09-05 10:57:15 +0800 | [diff] [blame] | 40 | struct regmap *anatop; |
Ying-Chun Liu (PaulLiu) | e3e5aff | 2012-03-14 10:29:12 +0800 | [diff] [blame] | 41 | int vol_bit_shift; |
| 42 | int vol_bit_width; |
Anson Huang | 9ee417c | 2013-01-31 11:23:53 -0500 | [diff] [blame] | 43 | u32 delay_reg; |
| 44 | int delay_bit_shift; |
| 45 | int delay_bit_width; |
Ying-Chun Liu (PaulLiu) | e3e5aff | 2012-03-14 10:29:12 +0800 | [diff] [blame] | 46 | int min_bit_val; |
| 47 | int min_voltage; |
| 48 | int max_voltage; |
| 49 | struct regulator_desc rdesc; |
| 50 | struct regulator_init_data *initdata; |
| 51 | }; |
| 52 | |
Dong Aisheng | baa6415 | 2012-09-05 10:57:15 +0800 | [diff] [blame] | 53 | static int anatop_regmap_set_voltage_sel(struct regulator_dev *reg, |
| 54 | unsigned selector) |
Ying-Chun Liu (PaulLiu) | e3e5aff | 2012-03-14 10:29:12 +0800 | [diff] [blame] | 55 | { |
| 56 | struct anatop_regulator *anatop_reg = rdev_get_drvdata(reg); |
Ying-Chun Liu (PaulLiu) | e3e5aff | 2012-03-14 10:29:12 +0800 | [diff] [blame] | 57 | |
| 58 | if (!anatop_reg->control_reg) |
| 59 | return -ENOTSUPP; |
| 60 | |
Axel Lin | e1b0144 | 2012-12-09 08:07:43 +0800 | [diff] [blame] | 61 | return regulator_set_voltage_sel_regmap(reg, selector); |
Ying-Chun Liu (PaulLiu) | e3e5aff | 2012-03-14 10:29:12 +0800 | [diff] [blame] | 62 | } |
| 63 | |
Anson Huang | 9ee417c | 2013-01-31 11:23:53 -0500 | [diff] [blame] | 64 | static int anatop_regmap_set_voltage_time_sel(struct regulator_dev *reg, |
| 65 | unsigned int old_sel, |
| 66 | unsigned int new_sel) |
| 67 | { |
| 68 | struct anatop_regulator *anatop_reg = rdev_get_drvdata(reg); |
| 69 | u32 val; |
| 70 | int ret = 0; |
| 71 | |
| 72 | /* check whether need to care about LDO ramp up speed */ |
| 73 | if (anatop_reg->delay_bit_width && new_sel > old_sel) { |
| 74 | /* |
| 75 | * the delay for LDO ramp up time is |
| 76 | * based on the register setting, we need |
| 77 | * to calculate how many steps LDO need to |
| 78 | * ramp up, and how much delay needed. (us) |
| 79 | */ |
| 80 | regmap_read(anatop_reg->anatop, anatop_reg->delay_reg, &val); |
| 81 | val = (val >> anatop_reg->delay_bit_shift) & |
| 82 | ((1 << anatop_reg->delay_bit_width) - 1); |
Shawn Guo | ff1ce05 | 2013-02-04 10:21:32 +0800 | [diff] [blame] | 83 | ret = (new_sel - old_sel) * (LDO_RAMP_UP_UNIT_IN_CYCLES << |
| 84 | val) / LDO_RAMP_UP_FREQ_IN_MHZ + 1; |
Anson Huang | 9ee417c | 2013-01-31 11:23:53 -0500 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | return ret; |
| 88 | } |
| 89 | |
Dong Aisheng | baa6415 | 2012-09-05 10:57:15 +0800 | [diff] [blame] | 90 | static int anatop_regmap_get_voltage_sel(struct regulator_dev *reg) |
Ying-Chun Liu (PaulLiu) | e3e5aff | 2012-03-14 10:29:12 +0800 | [diff] [blame] | 91 | { |
| 92 | struct anatop_regulator *anatop_reg = rdev_get_drvdata(reg); |
Ying-Chun Liu (PaulLiu) | e3e5aff | 2012-03-14 10:29:12 +0800 | [diff] [blame] | 93 | |
| 94 | if (!anatop_reg->control_reg) |
| 95 | return -ENOTSUPP; |
| 96 | |
Axel Lin | e1b0144 | 2012-12-09 08:07:43 +0800 | [diff] [blame] | 97 | return regulator_get_voltage_sel_regmap(reg); |
Ying-Chun Liu (PaulLiu) | e3e5aff | 2012-03-14 10:29:12 +0800 | [diff] [blame] | 98 | } |
| 99 | |
Ying-Chun Liu (PaulLiu) | e3e5aff | 2012-03-14 10:29:12 +0800 | [diff] [blame] | 100 | static struct regulator_ops anatop_rops = { |
Dong Aisheng | baa6415 | 2012-09-05 10:57:15 +0800 | [diff] [blame] | 101 | .set_voltage_sel = anatop_regmap_set_voltage_sel, |
Anson Huang | 9ee417c | 2013-01-31 11:23:53 -0500 | [diff] [blame] | 102 | .set_voltage_time_sel = anatop_regmap_set_voltage_time_sel, |
Dong Aisheng | baa6415 | 2012-09-05 10:57:15 +0800 | [diff] [blame] | 103 | .get_voltage_sel = anatop_regmap_get_voltage_sel, |
Axel Lin | d01c3a1 | 2012-06-03 23:02:34 +0800 | [diff] [blame] | 104 | .list_voltage = regulator_list_voltage_linear, |
| 105 | .map_voltage = regulator_map_voltage_linear, |
Ying-Chun Liu (PaulLiu) | e3e5aff | 2012-03-14 10:29:12 +0800 | [diff] [blame] | 106 | }; |
| 107 | |
Bill Pemberton | a502357 | 2012-11-19 13:22:22 -0500 | [diff] [blame] | 108 | static int anatop_regulator_probe(struct platform_device *pdev) |
Ying-Chun Liu (PaulLiu) | e3e5aff | 2012-03-14 10:29:12 +0800 | [diff] [blame] | 109 | { |
| 110 | struct device *dev = &pdev->dev; |
| 111 | struct device_node *np = dev->of_node; |
Dong Aisheng | baa6415 | 2012-09-05 10:57:15 +0800 | [diff] [blame] | 112 | struct device_node *anatop_np; |
Ying-Chun Liu (PaulLiu) | e3e5aff | 2012-03-14 10:29:12 +0800 | [diff] [blame] | 113 | struct regulator_desc *rdesc; |
| 114 | struct regulator_dev *rdev; |
| 115 | struct anatop_regulator *sreg; |
| 116 | struct regulator_init_data *initdata; |
Axel Lin | d914d81 | 2012-04-10 22:45:01 +0800 | [diff] [blame] | 117 | struct regulator_config config = { }; |
Ying-Chun Liu (PaulLiu) | e3e5aff | 2012-03-14 10:29:12 +0800 | [diff] [blame] | 118 | int ret = 0; |
| 119 | |
| 120 | initdata = of_get_regulator_init_data(dev, np); |
| 121 | sreg = devm_kzalloc(dev, sizeof(*sreg), GFP_KERNEL); |
| 122 | if (!sreg) |
| 123 | return -ENOMEM; |
| 124 | sreg->initdata = initdata; |
| 125 | sreg->name = kstrdup(of_get_property(np, "regulator-name", NULL), |
| 126 | GFP_KERNEL); |
| 127 | rdesc = &sreg->rdesc; |
| 128 | memset(rdesc, 0, sizeof(*rdesc)); |
| 129 | rdesc->name = sreg->name; |
| 130 | rdesc->ops = &anatop_rops; |
| 131 | rdesc->type = REGULATOR_VOLTAGE; |
| 132 | rdesc->owner = THIS_MODULE; |
Dong Aisheng | baa6415 | 2012-09-05 10:57:15 +0800 | [diff] [blame] | 133 | |
| 134 | anatop_np = of_get_parent(np); |
| 135 | if (!anatop_np) |
| 136 | return -ENODEV; |
| 137 | sreg->anatop = syscon_node_to_regmap(anatop_np); |
| 138 | of_node_put(anatop_np); |
| 139 | if (IS_ERR(sreg->anatop)) |
| 140 | return PTR_ERR(sreg->anatop); |
| 141 | |
Ying-Chun Liu (PaulLiu) | 2f2cc27 | 2012-03-27 15:54:01 +0800 | [diff] [blame] | 142 | ret = of_property_read_u32(np, "anatop-reg-offset", |
| 143 | &sreg->control_reg); |
Ying-Chun Liu (PaulLiu) | e3e5aff | 2012-03-14 10:29:12 +0800 | [diff] [blame] | 144 | if (ret) { |
Ying-Chun Liu (PaulLiu) | 2f2cc27 | 2012-03-27 15:54:01 +0800 | [diff] [blame] | 145 | dev_err(dev, "no anatop-reg-offset property set\n"); |
Ying-Chun Liu (PaulLiu) | e3e5aff | 2012-03-14 10:29:12 +0800 | [diff] [blame] | 146 | goto anatop_probe_end; |
| 147 | } |
| 148 | ret = of_property_read_u32(np, "anatop-vol-bit-width", |
| 149 | &sreg->vol_bit_width); |
| 150 | if (ret) { |
| 151 | dev_err(dev, "no anatop-vol-bit-width property set\n"); |
| 152 | goto anatop_probe_end; |
| 153 | } |
| 154 | ret = of_property_read_u32(np, "anatop-vol-bit-shift", |
| 155 | &sreg->vol_bit_shift); |
| 156 | if (ret) { |
| 157 | dev_err(dev, "no anatop-vol-bit-shift property set\n"); |
| 158 | goto anatop_probe_end; |
| 159 | } |
| 160 | ret = of_property_read_u32(np, "anatop-min-bit-val", |
| 161 | &sreg->min_bit_val); |
| 162 | if (ret) { |
| 163 | dev_err(dev, "no anatop-min-bit-val property set\n"); |
| 164 | goto anatop_probe_end; |
| 165 | } |
| 166 | ret = of_property_read_u32(np, "anatop-min-voltage", |
| 167 | &sreg->min_voltage); |
| 168 | if (ret) { |
| 169 | dev_err(dev, "no anatop-min-voltage property set\n"); |
| 170 | goto anatop_probe_end; |
| 171 | } |
| 172 | ret = of_property_read_u32(np, "anatop-max-voltage", |
| 173 | &sreg->max_voltage); |
| 174 | if (ret) { |
| 175 | dev_err(dev, "no anatop-max-voltage property set\n"); |
| 176 | goto anatop_probe_end; |
| 177 | } |
| 178 | |
Anson Huang | 9ee417c | 2013-01-31 11:23:53 -0500 | [diff] [blame] | 179 | /* read LDO ramp up setting, only for core reg */ |
| 180 | of_property_read_u32(np, "anatop-delay-reg-offset", |
| 181 | &sreg->delay_reg); |
| 182 | of_property_read_u32(np, "anatop-delay-bit-width", |
| 183 | &sreg->delay_bit_width); |
| 184 | of_property_read_u32(np, "anatop-delay-bit-shift", |
| 185 | &sreg->delay_bit_shift); |
| 186 | |
Axel Lin | 985884d | 2012-12-09 08:05:45 +0800 | [diff] [blame] | 187 | rdesc->n_voltages = (sreg->max_voltage - sreg->min_voltage) / 25000 + 1 |
| 188 | + sreg->min_bit_val; |
Axel Lin | 0713e6a | 2012-05-14 11:06:44 +0800 | [diff] [blame] | 189 | rdesc->min_uV = sreg->min_voltage; |
| 190 | rdesc->uV_step = 25000; |
Axel Lin | 985884d | 2012-12-09 08:05:45 +0800 | [diff] [blame] | 191 | rdesc->linear_min_sel = sreg->min_bit_val; |
Axel Lin | e1b0144 | 2012-12-09 08:07:43 +0800 | [diff] [blame] | 192 | rdesc->vsel_reg = sreg->control_reg; |
| 193 | rdesc->vsel_mask = ((1 << sreg->vol_bit_width) - 1) << |
| 194 | sreg->vol_bit_shift; |
Ying-Chun Liu (PaulLiu) | e3e5aff | 2012-03-14 10:29:12 +0800 | [diff] [blame] | 195 | |
Axel Lin | d914d81 | 2012-04-10 22:45:01 +0800 | [diff] [blame] | 196 | config.dev = &pdev->dev; |
| 197 | config.init_data = initdata; |
| 198 | config.driver_data = sreg; |
| 199 | config.of_node = pdev->dev.of_node; |
Axel Lin | e1b0144 | 2012-12-09 08:07:43 +0800 | [diff] [blame] | 200 | config.regmap = sreg->anatop; |
Axel Lin | d914d81 | 2012-04-10 22:45:01 +0800 | [diff] [blame] | 201 | |
Ying-Chun Liu (PaulLiu) | e3e5aff | 2012-03-14 10:29:12 +0800 | [diff] [blame] | 202 | /* register regulator */ |
Axel Lin | d914d81 | 2012-04-10 22:45:01 +0800 | [diff] [blame] | 203 | rdev = regulator_register(rdesc, &config); |
Ying-Chun Liu (PaulLiu) | e3e5aff | 2012-03-14 10:29:12 +0800 | [diff] [blame] | 204 | if (IS_ERR(rdev)) { |
| 205 | dev_err(dev, "failed to register %s\n", |
| 206 | rdesc->name); |
| 207 | ret = PTR_ERR(rdev); |
| 208 | goto anatop_probe_end; |
| 209 | } |
| 210 | |
| 211 | platform_set_drvdata(pdev, rdev); |
| 212 | |
| 213 | anatop_probe_end: |
| 214 | if (ret) |
| 215 | kfree(sreg->name); |
| 216 | |
| 217 | return ret; |
| 218 | } |
| 219 | |
Bill Pemberton | 8dc995f | 2012-11-19 13:26:10 -0500 | [diff] [blame] | 220 | static int anatop_regulator_remove(struct platform_device *pdev) |
Ying-Chun Liu (PaulLiu) | e3e5aff | 2012-03-14 10:29:12 +0800 | [diff] [blame] | 221 | { |
| 222 | struct regulator_dev *rdev = platform_get_drvdata(pdev); |
| 223 | struct anatop_regulator *sreg = rdev_get_drvdata(rdev); |
| 224 | const char *name = sreg->name; |
| 225 | |
| 226 | regulator_unregister(rdev); |
| 227 | kfree(name); |
| 228 | |
| 229 | return 0; |
| 230 | } |
| 231 | |
Greg Kroah-Hartman | 3d68dfe | 2012-12-21 13:26:06 -0800 | [diff] [blame] | 232 | static struct of_device_id of_anatop_regulator_match_tbl[] = { |
Ying-Chun Liu (PaulLiu) | e3e5aff | 2012-03-14 10:29:12 +0800 | [diff] [blame] | 233 | { .compatible = "fsl,anatop-regulator", }, |
| 234 | { /* end */ } |
| 235 | }; |
| 236 | |
Shawn Guo | c0d78c2 | 2012-04-02 14:57:01 +0800 | [diff] [blame] | 237 | static struct platform_driver anatop_regulator_driver = { |
Ying-Chun Liu (PaulLiu) | e3e5aff | 2012-03-14 10:29:12 +0800 | [diff] [blame] | 238 | .driver = { |
| 239 | .name = "anatop_regulator", |
| 240 | .owner = THIS_MODULE, |
| 241 | .of_match_table = of_anatop_regulator_match_tbl, |
| 242 | }, |
| 243 | .probe = anatop_regulator_probe, |
Bill Pemberton | 5eb9f2b | 2012-11-19 13:20:42 -0500 | [diff] [blame] | 244 | .remove = anatop_regulator_remove, |
Ying-Chun Liu (PaulLiu) | e3e5aff | 2012-03-14 10:29:12 +0800 | [diff] [blame] | 245 | }; |
| 246 | |
| 247 | static int __init anatop_regulator_init(void) |
| 248 | { |
Shawn Guo | c0d78c2 | 2012-04-02 14:57:01 +0800 | [diff] [blame] | 249 | return platform_driver_register(&anatop_regulator_driver); |
Ying-Chun Liu (PaulLiu) | e3e5aff | 2012-03-14 10:29:12 +0800 | [diff] [blame] | 250 | } |
| 251 | postcore_initcall(anatop_regulator_init); |
| 252 | |
| 253 | static void __exit anatop_regulator_exit(void) |
| 254 | { |
Shawn Guo | c0d78c2 | 2012-04-02 14:57:01 +0800 | [diff] [blame] | 255 | platform_driver_unregister(&anatop_regulator_driver); |
Ying-Chun Liu (PaulLiu) | e3e5aff | 2012-03-14 10:29:12 +0800 | [diff] [blame] | 256 | } |
| 257 | module_exit(anatop_regulator_exit); |
| 258 | |
| 259 | MODULE_AUTHOR("Nancy Chen <Nancy.Chen@freescale.com>, " |
| 260 | "Ying-Chun Liu (PaulLiu) <paul.liu@linaro.org>"); |
| 261 | MODULE_DESCRIPTION("ANATOP Regulator driver"); |
| 262 | MODULE_LICENSE("GPL v2"); |