1362af736SAxel Lin // SPDX-License-Identifier: GPL-2.0+ 2362af736SAxel Lin // 3362af736SAxel Lin // Regulator support for WM8400 4362af736SAxel Lin // 5362af736SAxel Lin // Copyright 2008 Wolfson Microelectronics PLC. 6362af736SAxel Lin // 7362af736SAxel Lin // Author: Mark Brown <broonie@opensource.wolfsonmicro.com> 842fad570SMark Brown 942fad570SMark Brown #include <linux/bug.h> 1042fad570SMark Brown #include <linux/err.h> 1142fad570SMark Brown #include <linux/kernel.h> 1265602c32SPaul Gortmaker #include <linux/module.h> 1342fad570SMark Brown #include <linux/regulator/driver.h> 1442fad570SMark Brown #include <linux/mfd/wm8400-private.h> 1542fad570SMark Brown 1660ab7f41SMatti Vaittinen static const struct linear_range wm8400_ldo_ranges[] = { 178828bae4SAxel Lin REGULATOR_LINEAR_RANGE(900000, 0, 14, 50000), 188828bae4SAxel Lin REGULATOR_LINEAR_RANGE(1700000, 15, 31, 100000), 196692e432SMark Brown }; 2042fad570SMark Brown 21b0d6dd3bSJulia Lawall static const struct regulator_ops wm8400_ldo_ops = { 22c54a155dSMark Brown .is_enabled = regulator_is_enabled_regmap, 23c54a155dSMark Brown .enable = regulator_enable_regmap, 24c54a155dSMark Brown .disable = regulator_disable_regmap, 256692e432SMark Brown .list_voltage = regulator_list_voltage_linear_range, 26c54a155dSMark Brown .get_voltage_sel = regulator_get_voltage_sel_regmap, 27c54a155dSMark Brown .set_voltage_sel = regulator_set_voltage_sel_regmap, 286692e432SMark Brown .map_voltage = regulator_map_voltage_linear_range, 29c54a155dSMark Brown }; 3042fad570SMark Brown 3142fad570SMark Brown static unsigned int wm8400_dcdc_get_mode(struct regulator_dev *dev) 3242fad570SMark Brown { 33e08abecaSAxel Lin struct regmap *rmap = rdev_get_regmap(dev); 3442fad570SMark Brown int offset = (rdev_get_id(dev) - WM8400_DCDC1) * 2; 3542fad570SMark Brown u16 data[2]; 3642fad570SMark Brown int ret; 3742fad570SMark Brown 38e08abecaSAxel Lin ret = regmap_bulk_read(rmap, WM8400_DCDC1_CONTROL_1 + offset, data, 2); 3942fad570SMark Brown if (ret != 0) 4042fad570SMark Brown return 0; 4142fad570SMark Brown 4242fad570SMark Brown /* Datasheet: hibernate */ 4342fad570SMark Brown if (data[0] & WM8400_DC1_SLEEP) 4442fad570SMark Brown return REGULATOR_MODE_STANDBY; 4542fad570SMark Brown 4642fad570SMark Brown /* Datasheet: standby */ 4742fad570SMark Brown if (!(data[0] & WM8400_DC1_ACTIVE)) 4842fad570SMark Brown return REGULATOR_MODE_IDLE; 4942fad570SMark Brown 5042fad570SMark Brown /* Datasheet: active with or without force PWM */ 5142fad570SMark Brown if (data[1] & WM8400_DC1_FRC_PWM) 5242fad570SMark Brown return REGULATOR_MODE_FAST; 5342fad570SMark Brown else 5442fad570SMark Brown return REGULATOR_MODE_NORMAL; 5542fad570SMark Brown } 5642fad570SMark Brown 5742fad570SMark Brown static int wm8400_dcdc_set_mode(struct regulator_dev *dev, unsigned int mode) 5842fad570SMark Brown { 59e08abecaSAxel Lin struct regmap *rmap = rdev_get_regmap(dev); 6042fad570SMark Brown int offset = (rdev_get_id(dev) - WM8400_DCDC1) * 2; 6142fad570SMark Brown int ret; 6242fad570SMark Brown 6342fad570SMark Brown switch (mode) { 6442fad570SMark Brown case REGULATOR_MODE_FAST: 6542fad570SMark Brown /* Datasheet: active with force PWM */ 66e08abecaSAxel Lin ret = regmap_update_bits(rmap, WM8400_DCDC1_CONTROL_2 + offset, 6742fad570SMark Brown WM8400_DC1_FRC_PWM, WM8400_DC1_FRC_PWM); 6842fad570SMark Brown if (ret != 0) 6942fad570SMark Brown return ret; 7042fad570SMark Brown 71e08abecaSAxel Lin return regmap_update_bits(rmap, WM8400_DCDC1_CONTROL_1 + offset, 7242fad570SMark Brown WM8400_DC1_ACTIVE | WM8400_DC1_SLEEP, 7342fad570SMark Brown WM8400_DC1_ACTIVE); 7442fad570SMark Brown 7542fad570SMark Brown case REGULATOR_MODE_NORMAL: 7642fad570SMark Brown /* Datasheet: active */ 77e08abecaSAxel Lin ret = regmap_update_bits(rmap, WM8400_DCDC1_CONTROL_2 + offset, 7842fad570SMark Brown WM8400_DC1_FRC_PWM, 0); 7942fad570SMark Brown if (ret != 0) 8042fad570SMark Brown return ret; 8142fad570SMark Brown 82e08abecaSAxel Lin return regmap_update_bits(rmap, WM8400_DCDC1_CONTROL_1 + offset, 8342fad570SMark Brown WM8400_DC1_ACTIVE | WM8400_DC1_SLEEP, 8442fad570SMark Brown WM8400_DC1_ACTIVE); 8542fad570SMark Brown 8642fad570SMark Brown case REGULATOR_MODE_IDLE: 8742fad570SMark Brown /* Datasheet: standby */ 88e08abecaSAxel Lin return regmap_update_bits(rmap, WM8400_DCDC1_CONTROL_1 + offset, 894001376eSAxel Lin WM8400_DC1_ACTIVE | WM8400_DC1_SLEEP, 0); 9042fad570SMark Brown default: 9142fad570SMark Brown return -EINVAL; 9242fad570SMark Brown } 9342fad570SMark Brown } 9442fad570SMark Brown 9542fad570SMark Brown static unsigned int wm8400_dcdc_get_optimum_mode(struct regulator_dev *dev, 9642fad570SMark Brown int input_uV, int output_uV, 9742fad570SMark Brown int load_uA) 9842fad570SMark Brown { 9942fad570SMark Brown return REGULATOR_MODE_NORMAL; 10042fad570SMark Brown } 10142fad570SMark Brown 102b0d6dd3bSJulia Lawall static const struct regulator_ops wm8400_dcdc_ops = { 103c54a155dSMark Brown .is_enabled = regulator_is_enabled_regmap, 104c54a155dSMark Brown .enable = regulator_enable_regmap, 105c54a155dSMark Brown .disable = regulator_disable_regmap, 106c54a155dSMark Brown .list_voltage = regulator_list_voltage_linear, 10727eeabb7SAxel Lin .map_voltage = regulator_map_voltage_linear, 108c54a155dSMark Brown .get_voltage_sel = regulator_get_voltage_sel_regmap, 109c54a155dSMark Brown .set_voltage_sel = regulator_set_voltage_sel_regmap, 11042fad570SMark Brown .get_mode = wm8400_dcdc_get_mode, 11142fad570SMark Brown .set_mode = wm8400_dcdc_set_mode, 11242fad570SMark Brown .get_optimum_mode = wm8400_dcdc_get_optimum_mode, 11342fad570SMark Brown }; 11442fad570SMark Brown 115*48cc042bSChristophe JAILLET static const struct regulator_desc regulators[] = { 11642fad570SMark Brown { 11742fad570SMark Brown .name = "LDO1", 11842fad570SMark Brown .id = WM8400_LDO1, 11942fad570SMark Brown .ops = &wm8400_ldo_ops, 120c54a155dSMark Brown .enable_reg = WM8400_LDO1_CONTROL, 121c54a155dSMark Brown .enable_mask = WM8400_LDO1_ENA, 122216765d9SMark Brown .n_voltages = WM8400_LDO1_VSEL_MASK + 1, 1236692e432SMark Brown .linear_ranges = wm8400_ldo_ranges, 1246692e432SMark Brown .n_linear_ranges = ARRAY_SIZE(wm8400_ldo_ranges), 125c54a155dSMark Brown .vsel_reg = WM8400_LDO1_CONTROL, 126c54a155dSMark Brown .vsel_mask = WM8400_LDO1_VSEL_MASK, 12742fad570SMark Brown .type = REGULATOR_VOLTAGE, 12842fad570SMark Brown .owner = THIS_MODULE, 12942fad570SMark Brown }, 13042fad570SMark Brown { 13142fad570SMark Brown .name = "LDO2", 13242fad570SMark Brown .id = WM8400_LDO2, 13342fad570SMark Brown .ops = &wm8400_ldo_ops, 134c54a155dSMark Brown .enable_reg = WM8400_LDO2_CONTROL, 135c54a155dSMark Brown .enable_mask = WM8400_LDO2_ENA, 136216765d9SMark Brown .n_voltages = WM8400_LDO2_VSEL_MASK + 1, 1376692e432SMark Brown .linear_ranges = wm8400_ldo_ranges, 1386692e432SMark Brown .n_linear_ranges = ARRAY_SIZE(wm8400_ldo_ranges), 13942fad570SMark Brown .type = REGULATOR_VOLTAGE, 140c54a155dSMark Brown .vsel_reg = WM8400_LDO2_CONTROL, 141c54a155dSMark Brown .vsel_mask = WM8400_LDO2_VSEL_MASK, 14242fad570SMark Brown .owner = THIS_MODULE, 14342fad570SMark Brown }, 14442fad570SMark Brown { 14542fad570SMark Brown .name = "LDO3", 14642fad570SMark Brown .id = WM8400_LDO3, 14742fad570SMark Brown .ops = &wm8400_ldo_ops, 148c54a155dSMark Brown .enable_reg = WM8400_LDO3_CONTROL, 149c54a155dSMark Brown .enable_mask = WM8400_LDO3_ENA, 150216765d9SMark Brown .n_voltages = WM8400_LDO3_VSEL_MASK + 1, 1516692e432SMark Brown .linear_ranges = wm8400_ldo_ranges, 1526692e432SMark Brown .n_linear_ranges = ARRAY_SIZE(wm8400_ldo_ranges), 153c54a155dSMark Brown .vsel_reg = WM8400_LDO3_CONTROL, 154c54a155dSMark Brown .vsel_mask = WM8400_LDO3_VSEL_MASK, 15542fad570SMark Brown .type = REGULATOR_VOLTAGE, 15642fad570SMark Brown .owner = THIS_MODULE, 15742fad570SMark Brown }, 15842fad570SMark Brown { 15942fad570SMark Brown .name = "LDO4", 16042fad570SMark Brown .id = WM8400_LDO4, 16142fad570SMark Brown .ops = &wm8400_ldo_ops, 162c54a155dSMark Brown .enable_reg = WM8400_LDO4_CONTROL, 163c54a155dSMark Brown .enable_mask = WM8400_LDO4_ENA, 164216765d9SMark Brown .n_voltages = WM8400_LDO4_VSEL_MASK + 1, 1656692e432SMark Brown .linear_ranges = wm8400_ldo_ranges, 1666692e432SMark Brown .n_linear_ranges = ARRAY_SIZE(wm8400_ldo_ranges), 167c54a155dSMark Brown .vsel_reg = WM8400_LDO4_CONTROL, 168c54a155dSMark Brown .vsel_mask = WM8400_LDO4_VSEL_MASK, 16942fad570SMark Brown .type = REGULATOR_VOLTAGE, 17042fad570SMark Brown .owner = THIS_MODULE, 17142fad570SMark Brown }, 17242fad570SMark Brown { 17342fad570SMark Brown .name = "DCDC1", 17442fad570SMark Brown .id = WM8400_DCDC1, 17542fad570SMark Brown .ops = &wm8400_dcdc_ops, 176c54a155dSMark Brown .enable_reg = WM8400_DCDC1_CONTROL_1, 177c54a155dSMark Brown .enable_mask = WM8400_DC1_ENA_MASK, 178216765d9SMark Brown .n_voltages = WM8400_DC1_VSEL_MASK + 1, 179c54a155dSMark Brown .vsel_reg = WM8400_DCDC1_CONTROL_1, 180c54a155dSMark Brown .vsel_mask = WM8400_DC1_VSEL_MASK, 181c54a155dSMark Brown .min_uV = 850000, 182c54a155dSMark Brown .uV_step = 25000, 18342fad570SMark Brown .type = REGULATOR_VOLTAGE, 18442fad570SMark Brown .owner = THIS_MODULE, 18542fad570SMark Brown }, 18642fad570SMark Brown { 18742fad570SMark Brown .name = "DCDC2", 18842fad570SMark Brown .id = WM8400_DCDC2, 18942fad570SMark Brown .ops = &wm8400_dcdc_ops, 190c54a155dSMark Brown .enable_reg = WM8400_DCDC2_CONTROL_1, 191178d08eaSAxel Lin .enable_mask = WM8400_DC2_ENA_MASK, 192216765d9SMark Brown .n_voltages = WM8400_DC2_VSEL_MASK + 1, 193c54a155dSMark Brown .vsel_reg = WM8400_DCDC2_CONTROL_1, 194c54a155dSMark Brown .vsel_mask = WM8400_DC2_VSEL_MASK, 195c54a155dSMark Brown .min_uV = 850000, 196c54a155dSMark Brown .uV_step = 25000, 19742fad570SMark Brown .type = REGULATOR_VOLTAGE, 19842fad570SMark Brown .owner = THIS_MODULE, 19942fad570SMark Brown }, 20042fad570SMark Brown }; 20142fad570SMark Brown 202a5023574SBill Pemberton static int wm8400_regulator_probe(struct platform_device *pdev) 20342fad570SMark Brown { 2041ad02bbcSDmitry Torokhov struct wm8400 *wm8400 = container_of(pdev, struct wm8400, regulators[pdev->id]); 205c172708dSMark Brown struct regulator_config config = { }; 20642fad570SMark Brown struct regulator_dev *rdev; 20742fad570SMark Brown 208c172708dSMark Brown config.dev = &pdev->dev; 209dff91d0bSJingoo Han config.init_data = dev_get_platdata(&pdev->dev); 210c172708dSMark Brown config.driver_data = wm8400; 211c54a155dSMark Brown config.regmap = wm8400->regmap; 21242fad570SMark Brown 213eb8b3c83SMark Brown rdev = devm_regulator_register(&pdev->dev, ®ulators[pdev->id], 214eb8b3c83SMark Brown &config); 21542fad570SMark Brown if (IS_ERR(rdev)) 21642fad570SMark Brown return PTR_ERR(rdev); 21742fad570SMark Brown 2181ad02bbcSDmitry Torokhov platform_set_drvdata(pdev, rdev); 2191ad02bbcSDmitry Torokhov 22042fad570SMark Brown return 0; 22142fad570SMark Brown } 22242fad570SMark Brown 22342fad570SMark Brown static struct platform_driver wm8400_regulator_driver = { 22442fad570SMark Brown .driver = { 22542fad570SMark Brown .name = "wm8400-regulator", 226259b93b2SDouglas Anderson .probe_type = PROBE_PREFER_ASYNCHRONOUS, 22742fad570SMark Brown }, 22842fad570SMark Brown .probe = wm8400_regulator_probe, 22942fad570SMark Brown }; 23042fad570SMark Brown 23142fad570SMark Brown /** 23242fad570SMark Brown * wm8400_register_regulator - enable software control of a WM8400 regulator 23342fad570SMark Brown * 23442fad570SMark Brown * This function enables software control of a WM8400 regulator via 23542fad570SMark Brown * the regulator API. It is intended to be called from the 23642fad570SMark Brown * platform_init() callback of the WM8400 MFD driver. 23742fad570SMark Brown * 2380c526166SLee Jones * @dev: The WM8400 device to operate on. 2390c526166SLee Jones * @reg: The regulator to control. 2400c526166SLee Jones * @initdata: Regulator initdata for the regulator. 24142fad570SMark Brown */ 24242fad570SMark Brown int wm8400_register_regulator(struct device *dev, int reg, 24342fad570SMark Brown struct regulator_init_data *initdata) 24442fad570SMark Brown { 2451909e2f6SGreg Kroah-Hartman struct wm8400 *wm8400 = dev_get_drvdata(dev); 24642fad570SMark Brown 24742fad570SMark Brown if (wm8400->regulators[reg].name) 24842fad570SMark Brown return -EBUSY; 24942fad570SMark Brown 25042fad570SMark Brown initdata->driver_data = wm8400; 25142fad570SMark Brown 25242fad570SMark Brown wm8400->regulators[reg].name = "wm8400-regulator"; 25342fad570SMark Brown wm8400->regulators[reg].id = reg; 25442fad570SMark Brown wm8400->regulators[reg].dev.parent = dev; 25542fad570SMark Brown wm8400->regulators[reg].dev.platform_data = initdata; 25642fad570SMark Brown 25742fad570SMark Brown return platform_device_register(&wm8400->regulators[reg]); 25842fad570SMark Brown } 25942fad570SMark Brown EXPORT_SYMBOL_GPL(wm8400_register_regulator); 26042fad570SMark Brown 26142fad570SMark Brown static int __init wm8400_regulator_init(void) 26242fad570SMark Brown { 26342fad570SMark Brown return platform_driver_register(&wm8400_regulator_driver); 26442fad570SMark Brown } 2655a1b22beSMark Brown subsys_initcall(wm8400_regulator_init); 26642fad570SMark Brown 26742fad570SMark Brown static void __exit wm8400_regulator_exit(void) 26842fad570SMark Brown { 26942fad570SMark Brown platform_driver_unregister(&wm8400_regulator_driver); 27042fad570SMark Brown } 27142fad570SMark Brown module_exit(wm8400_regulator_exit); 27242fad570SMark Brown 27342fad570SMark Brown MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>"); 27442fad570SMark Brown MODULE_DESCRIPTION("WM8400 regulator driver"); 27542fad570SMark Brown MODULE_LICENSE("GPL"); 27642fad570SMark Brown MODULE_ALIAS("platform:wm8400-regulator"); 277