1e3e5aff7SYing-Chun Liu (PaulLiu) /* 2e3e5aff7SYing-Chun Liu (PaulLiu) * Copyright (C) 2011 Freescale Semiconductor, Inc. All Rights Reserved. 3e3e5aff7SYing-Chun Liu (PaulLiu) */ 4e3e5aff7SYing-Chun Liu (PaulLiu) 5e3e5aff7SYing-Chun Liu (PaulLiu) /* 6e3e5aff7SYing-Chun Liu (PaulLiu) * This program is free software; you can redistribute it and/or modify 7e3e5aff7SYing-Chun Liu (PaulLiu) * it under the terms of the GNU General Public License as published by 8e3e5aff7SYing-Chun Liu (PaulLiu) * the Free Software Foundation; either version 2 of the License, or 9e3e5aff7SYing-Chun Liu (PaulLiu) * (at your option) any later version. 10e3e5aff7SYing-Chun Liu (PaulLiu) 11e3e5aff7SYing-Chun Liu (PaulLiu) * This program is distributed in the hope that it will be useful, 12e3e5aff7SYing-Chun Liu (PaulLiu) * but WITHOUT ANY WARRANTY; without even the implied warranty of 13e3e5aff7SYing-Chun Liu (PaulLiu) * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14e3e5aff7SYing-Chun Liu (PaulLiu) * GNU General Public License for more details. 15e3e5aff7SYing-Chun Liu (PaulLiu) 16e3e5aff7SYing-Chun Liu (PaulLiu) * You should have received a copy of the GNU General Public License along 17e3e5aff7SYing-Chun Liu (PaulLiu) * with this program; if not, write to the Free Software Foundation, Inc., 18e3e5aff7SYing-Chun Liu (PaulLiu) * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 19e3e5aff7SYing-Chun Liu (PaulLiu) */ 20e3e5aff7SYing-Chun Liu (PaulLiu) 21e3e5aff7SYing-Chun Liu (PaulLiu) #include <linux/slab.h> 22e3e5aff7SYing-Chun Liu (PaulLiu) #include <linux/device.h> 23e3e5aff7SYing-Chun Liu (PaulLiu) #include <linux/module.h> 24baa64151SDong Aisheng #include <linux/mfd/syscon.h> 25e3e5aff7SYing-Chun Liu (PaulLiu) #include <linux/err.h> 26e3e5aff7SYing-Chun Liu (PaulLiu) #include <linux/io.h> 27e3e5aff7SYing-Chun Liu (PaulLiu) #include <linux/platform_device.h> 28e3e5aff7SYing-Chun Liu (PaulLiu) #include <linux/of.h> 29e3e5aff7SYing-Chun Liu (PaulLiu) #include <linux/of_address.h> 30baa64151SDong Aisheng #include <linux/regmap.h> 31e3e5aff7SYing-Chun Liu (PaulLiu) #include <linux/regulator/driver.h> 32e3e5aff7SYing-Chun Liu (PaulLiu) #include <linux/regulator/of_regulator.h> 33e3e5aff7SYing-Chun Liu (PaulLiu) 34e3e5aff7SYing-Chun Liu (PaulLiu) struct anatop_regulator { 35e3e5aff7SYing-Chun Liu (PaulLiu) const char *name; 36e3e5aff7SYing-Chun Liu (PaulLiu) u32 control_reg; 37baa64151SDong Aisheng struct regmap *anatop; 38e3e5aff7SYing-Chun Liu (PaulLiu) int vol_bit_shift; 39e3e5aff7SYing-Chun Liu (PaulLiu) int vol_bit_width; 40e3e5aff7SYing-Chun Liu (PaulLiu) int min_bit_val; 41e3e5aff7SYing-Chun Liu (PaulLiu) int min_voltage; 42e3e5aff7SYing-Chun Liu (PaulLiu) int max_voltage; 43e3e5aff7SYing-Chun Liu (PaulLiu) struct regulator_desc rdesc; 44e3e5aff7SYing-Chun Liu (PaulLiu) struct regulator_init_data *initdata; 45e3e5aff7SYing-Chun Liu (PaulLiu) }; 46e3e5aff7SYing-Chun Liu (PaulLiu) 47baa64151SDong Aisheng static int anatop_regmap_set_voltage_sel(struct regulator_dev *reg, 48baa64151SDong Aisheng unsigned selector) 49e3e5aff7SYing-Chun Liu (PaulLiu) { 50e3e5aff7SYing-Chun Liu (PaulLiu) struct anatop_regulator *anatop_reg = rdev_get_drvdata(reg); 51e3e5aff7SYing-Chun Liu (PaulLiu) 52e3e5aff7SYing-Chun Liu (PaulLiu) if (!anatop_reg->control_reg) 53e3e5aff7SYing-Chun Liu (PaulLiu) return -ENOTSUPP; 54e3e5aff7SYing-Chun Liu (PaulLiu) 55e1b0144fSAxel Lin return regulator_set_voltage_sel_regmap(reg, selector); 56e3e5aff7SYing-Chun Liu (PaulLiu) } 57e3e5aff7SYing-Chun Liu (PaulLiu) 58baa64151SDong Aisheng static int anatop_regmap_get_voltage_sel(struct regulator_dev *reg) 59e3e5aff7SYing-Chun Liu (PaulLiu) { 60e3e5aff7SYing-Chun Liu (PaulLiu) struct anatop_regulator *anatop_reg = rdev_get_drvdata(reg); 61e3e5aff7SYing-Chun Liu (PaulLiu) 62e3e5aff7SYing-Chun Liu (PaulLiu) if (!anatop_reg->control_reg) 63e3e5aff7SYing-Chun Liu (PaulLiu) return -ENOTSUPP; 64e3e5aff7SYing-Chun Liu (PaulLiu) 65e1b0144fSAxel Lin return regulator_get_voltage_sel_regmap(reg); 66e3e5aff7SYing-Chun Liu (PaulLiu) } 67e3e5aff7SYing-Chun Liu (PaulLiu) 68e3e5aff7SYing-Chun Liu (PaulLiu) static struct regulator_ops anatop_rops = { 69baa64151SDong Aisheng .set_voltage_sel = anatop_regmap_set_voltage_sel, 70baa64151SDong Aisheng .get_voltage_sel = anatop_regmap_get_voltage_sel, 710713e6abSAxel Lin .list_voltage = regulator_list_voltage_linear, 72d01c3a1eSAxel Lin .map_voltage = regulator_map_voltage_linear, 73e3e5aff7SYing-Chun Liu (PaulLiu) }; 74e3e5aff7SYing-Chun Liu (PaulLiu) 75a5023574SBill Pemberton static int anatop_regulator_probe(struct platform_device *pdev) 76e3e5aff7SYing-Chun Liu (PaulLiu) { 77e3e5aff7SYing-Chun Liu (PaulLiu) struct device *dev = &pdev->dev; 78e3e5aff7SYing-Chun Liu (PaulLiu) struct device_node *np = dev->of_node; 79baa64151SDong Aisheng struct device_node *anatop_np; 80e3e5aff7SYing-Chun Liu (PaulLiu) struct regulator_desc *rdesc; 81e3e5aff7SYing-Chun Liu (PaulLiu) struct regulator_dev *rdev; 82e3e5aff7SYing-Chun Liu (PaulLiu) struct anatop_regulator *sreg; 83e3e5aff7SYing-Chun Liu (PaulLiu) struct regulator_init_data *initdata; 84d914d81bSAxel Lin struct regulator_config config = { }; 85e3e5aff7SYing-Chun Liu (PaulLiu) int ret = 0; 86e3e5aff7SYing-Chun Liu (PaulLiu) 87e3e5aff7SYing-Chun Liu (PaulLiu) initdata = of_get_regulator_init_data(dev, np); 88e3e5aff7SYing-Chun Liu (PaulLiu) sreg = devm_kzalloc(dev, sizeof(*sreg), GFP_KERNEL); 89e3e5aff7SYing-Chun Liu (PaulLiu) if (!sreg) 90e3e5aff7SYing-Chun Liu (PaulLiu) return -ENOMEM; 91e3e5aff7SYing-Chun Liu (PaulLiu) sreg->initdata = initdata; 92e3e5aff7SYing-Chun Liu (PaulLiu) sreg->name = kstrdup(of_get_property(np, "regulator-name", NULL), 93e3e5aff7SYing-Chun Liu (PaulLiu) GFP_KERNEL); 94e3e5aff7SYing-Chun Liu (PaulLiu) rdesc = &sreg->rdesc; 95e3e5aff7SYing-Chun Liu (PaulLiu) memset(rdesc, 0, sizeof(*rdesc)); 96e3e5aff7SYing-Chun Liu (PaulLiu) rdesc->name = sreg->name; 97e3e5aff7SYing-Chun Liu (PaulLiu) rdesc->ops = &anatop_rops; 98e3e5aff7SYing-Chun Liu (PaulLiu) rdesc->type = REGULATOR_VOLTAGE; 99e3e5aff7SYing-Chun Liu (PaulLiu) rdesc->owner = THIS_MODULE; 100baa64151SDong Aisheng 101baa64151SDong Aisheng anatop_np = of_get_parent(np); 102baa64151SDong Aisheng if (!anatop_np) 103baa64151SDong Aisheng return -ENODEV; 104baa64151SDong Aisheng sreg->anatop = syscon_node_to_regmap(anatop_np); 105baa64151SDong Aisheng of_node_put(anatop_np); 106baa64151SDong Aisheng if (IS_ERR(sreg->anatop)) 107baa64151SDong Aisheng return PTR_ERR(sreg->anatop); 108baa64151SDong Aisheng 1092f2cc27fSYing-Chun Liu (PaulLiu) ret = of_property_read_u32(np, "anatop-reg-offset", 1102f2cc27fSYing-Chun Liu (PaulLiu) &sreg->control_reg); 111e3e5aff7SYing-Chun Liu (PaulLiu) if (ret) { 1122f2cc27fSYing-Chun Liu (PaulLiu) dev_err(dev, "no anatop-reg-offset property set\n"); 113e3e5aff7SYing-Chun Liu (PaulLiu) goto anatop_probe_end; 114e3e5aff7SYing-Chun Liu (PaulLiu) } 115e3e5aff7SYing-Chun Liu (PaulLiu) ret = of_property_read_u32(np, "anatop-vol-bit-width", 116e3e5aff7SYing-Chun Liu (PaulLiu) &sreg->vol_bit_width); 117e3e5aff7SYing-Chun Liu (PaulLiu) if (ret) { 118e3e5aff7SYing-Chun Liu (PaulLiu) dev_err(dev, "no anatop-vol-bit-width property set\n"); 119e3e5aff7SYing-Chun Liu (PaulLiu) goto anatop_probe_end; 120e3e5aff7SYing-Chun Liu (PaulLiu) } 121e3e5aff7SYing-Chun Liu (PaulLiu) ret = of_property_read_u32(np, "anatop-vol-bit-shift", 122e3e5aff7SYing-Chun Liu (PaulLiu) &sreg->vol_bit_shift); 123e3e5aff7SYing-Chun Liu (PaulLiu) if (ret) { 124e3e5aff7SYing-Chun Liu (PaulLiu) dev_err(dev, "no anatop-vol-bit-shift property set\n"); 125e3e5aff7SYing-Chun Liu (PaulLiu) goto anatop_probe_end; 126e3e5aff7SYing-Chun Liu (PaulLiu) } 127e3e5aff7SYing-Chun Liu (PaulLiu) ret = of_property_read_u32(np, "anatop-min-bit-val", 128e3e5aff7SYing-Chun Liu (PaulLiu) &sreg->min_bit_val); 129e3e5aff7SYing-Chun Liu (PaulLiu) if (ret) { 130e3e5aff7SYing-Chun Liu (PaulLiu) dev_err(dev, "no anatop-min-bit-val property set\n"); 131e3e5aff7SYing-Chun Liu (PaulLiu) goto anatop_probe_end; 132e3e5aff7SYing-Chun Liu (PaulLiu) } 133e3e5aff7SYing-Chun Liu (PaulLiu) ret = of_property_read_u32(np, "anatop-min-voltage", 134e3e5aff7SYing-Chun Liu (PaulLiu) &sreg->min_voltage); 135e3e5aff7SYing-Chun Liu (PaulLiu) if (ret) { 136e3e5aff7SYing-Chun Liu (PaulLiu) dev_err(dev, "no anatop-min-voltage property set\n"); 137e3e5aff7SYing-Chun Liu (PaulLiu) goto anatop_probe_end; 138e3e5aff7SYing-Chun Liu (PaulLiu) } 139e3e5aff7SYing-Chun Liu (PaulLiu) ret = of_property_read_u32(np, "anatop-max-voltage", 140e3e5aff7SYing-Chun Liu (PaulLiu) &sreg->max_voltage); 141e3e5aff7SYing-Chun Liu (PaulLiu) if (ret) { 142e3e5aff7SYing-Chun Liu (PaulLiu) dev_err(dev, "no anatop-max-voltage property set\n"); 143e3e5aff7SYing-Chun Liu (PaulLiu) goto anatop_probe_end; 144e3e5aff7SYing-Chun Liu (PaulLiu) } 145e3e5aff7SYing-Chun Liu (PaulLiu) 146985884dbSAxel Lin rdesc->n_voltages = (sreg->max_voltage - sreg->min_voltage) / 25000 + 1 147985884dbSAxel Lin + sreg->min_bit_val; 1480713e6abSAxel Lin rdesc->min_uV = sreg->min_voltage; 1490713e6abSAxel Lin rdesc->uV_step = 25000; 150985884dbSAxel Lin rdesc->linear_min_sel = sreg->min_bit_val; 151e1b0144fSAxel Lin rdesc->vsel_reg = sreg->control_reg; 152e1b0144fSAxel Lin rdesc->vsel_mask = ((1 << sreg->vol_bit_width) - 1) << 153e1b0144fSAxel Lin sreg->vol_bit_shift; 154e3e5aff7SYing-Chun Liu (PaulLiu) 155d914d81bSAxel Lin config.dev = &pdev->dev; 156d914d81bSAxel Lin config.init_data = initdata; 157d914d81bSAxel Lin config.driver_data = sreg; 158d914d81bSAxel Lin config.of_node = pdev->dev.of_node; 159e1b0144fSAxel Lin config.regmap = sreg->anatop; 160d914d81bSAxel Lin 161e3e5aff7SYing-Chun Liu (PaulLiu) /* register regulator */ 162d914d81bSAxel Lin rdev = regulator_register(rdesc, &config); 163e3e5aff7SYing-Chun Liu (PaulLiu) if (IS_ERR(rdev)) { 164e3e5aff7SYing-Chun Liu (PaulLiu) dev_err(dev, "failed to register %s\n", 165e3e5aff7SYing-Chun Liu (PaulLiu) rdesc->name); 166e3e5aff7SYing-Chun Liu (PaulLiu) ret = PTR_ERR(rdev); 167e3e5aff7SYing-Chun Liu (PaulLiu) goto anatop_probe_end; 168e3e5aff7SYing-Chun Liu (PaulLiu) } 169e3e5aff7SYing-Chun Liu (PaulLiu) 170e3e5aff7SYing-Chun Liu (PaulLiu) platform_set_drvdata(pdev, rdev); 171e3e5aff7SYing-Chun Liu (PaulLiu) 172e3e5aff7SYing-Chun Liu (PaulLiu) anatop_probe_end: 173e3e5aff7SYing-Chun Liu (PaulLiu) if (ret) 174e3e5aff7SYing-Chun Liu (PaulLiu) kfree(sreg->name); 175e3e5aff7SYing-Chun Liu (PaulLiu) 176e3e5aff7SYing-Chun Liu (PaulLiu) return ret; 177e3e5aff7SYing-Chun Liu (PaulLiu) } 178e3e5aff7SYing-Chun Liu (PaulLiu) 1798dc995f5SBill Pemberton static int anatop_regulator_remove(struct platform_device *pdev) 180e3e5aff7SYing-Chun Liu (PaulLiu) { 181e3e5aff7SYing-Chun Liu (PaulLiu) struct regulator_dev *rdev = platform_get_drvdata(pdev); 182e3e5aff7SYing-Chun Liu (PaulLiu) struct anatop_regulator *sreg = rdev_get_drvdata(rdev); 183e3e5aff7SYing-Chun Liu (PaulLiu) const char *name = sreg->name; 184e3e5aff7SYing-Chun Liu (PaulLiu) 185e3e5aff7SYing-Chun Liu (PaulLiu) regulator_unregister(rdev); 186e3e5aff7SYing-Chun Liu (PaulLiu) kfree(name); 187e3e5aff7SYing-Chun Liu (PaulLiu) 188e3e5aff7SYing-Chun Liu (PaulLiu) return 0; 189e3e5aff7SYing-Chun Liu (PaulLiu) } 190e3e5aff7SYing-Chun Liu (PaulLiu) 191*3d68dfe3SGreg Kroah-Hartman static struct of_device_id of_anatop_regulator_match_tbl[] = { 192e3e5aff7SYing-Chun Liu (PaulLiu) { .compatible = "fsl,anatop-regulator", }, 193e3e5aff7SYing-Chun Liu (PaulLiu) { /* end */ } 194e3e5aff7SYing-Chun Liu (PaulLiu) }; 195e3e5aff7SYing-Chun Liu (PaulLiu) 196c0d78c23SShawn Guo static struct platform_driver anatop_regulator_driver = { 197e3e5aff7SYing-Chun Liu (PaulLiu) .driver = { 198e3e5aff7SYing-Chun Liu (PaulLiu) .name = "anatop_regulator", 199e3e5aff7SYing-Chun Liu (PaulLiu) .owner = THIS_MODULE, 200e3e5aff7SYing-Chun Liu (PaulLiu) .of_match_table = of_anatop_regulator_match_tbl, 201e3e5aff7SYing-Chun Liu (PaulLiu) }, 202e3e5aff7SYing-Chun Liu (PaulLiu) .probe = anatop_regulator_probe, 2035eb9f2b9SBill Pemberton .remove = anatop_regulator_remove, 204e3e5aff7SYing-Chun Liu (PaulLiu) }; 205e3e5aff7SYing-Chun Liu (PaulLiu) 206e3e5aff7SYing-Chun Liu (PaulLiu) static int __init anatop_regulator_init(void) 207e3e5aff7SYing-Chun Liu (PaulLiu) { 208c0d78c23SShawn Guo return platform_driver_register(&anatop_regulator_driver); 209e3e5aff7SYing-Chun Liu (PaulLiu) } 210e3e5aff7SYing-Chun Liu (PaulLiu) postcore_initcall(anatop_regulator_init); 211e3e5aff7SYing-Chun Liu (PaulLiu) 212e3e5aff7SYing-Chun Liu (PaulLiu) static void __exit anatop_regulator_exit(void) 213e3e5aff7SYing-Chun Liu (PaulLiu) { 214c0d78c23SShawn Guo platform_driver_unregister(&anatop_regulator_driver); 215e3e5aff7SYing-Chun Liu (PaulLiu) } 216e3e5aff7SYing-Chun Liu (PaulLiu) module_exit(anatop_regulator_exit); 217e3e5aff7SYing-Chun Liu (PaulLiu) 218e3e5aff7SYing-Chun Liu (PaulLiu) MODULE_AUTHOR("Nancy Chen <Nancy.Chen@freescale.com>, " 219e3e5aff7SYing-Chun Liu (PaulLiu) "Ying-Chun Liu (PaulLiu) <paul.liu@linaro.org>"); 220e3e5aff7SYing-Chun Liu (PaulLiu) MODULE_DESCRIPTION("ANATOP Regulator driver"); 221e3e5aff7SYing-Chun Liu (PaulLiu) MODULE_LICENSE("GPL v2"); 222