142fad570SMark Brown /* 242fad570SMark Brown * Regulator support for WM8400 342fad570SMark Brown * 442fad570SMark Brown * Copyright 2008 Wolfson Microelectronics PLC. 542fad570SMark Brown * 642fad570SMark Brown * Author: Mark Brown <broonie@opensource.wolfsonmicro.com> 742fad570SMark Brown * 842fad570SMark Brown * This program is free software; you can redistribute it and/or 942fad570SMark Brown * modify it under the terms of the GNU General Public License as 1042fad570SMark Brown * published by the Free Software Foundation; either version 2 of the 1142fad570SMark Brown * License, or (at your option) any later version. 1242fad570SMark Brown * 1342fad570SMark Brown */ 1442fad570SMark Brown 1542fad570SMark Brown #include <linux/bug.h> 1642fad570SMark Brown #include <linux/err.h> 1742fad570SMark Brown #include <linux/kernel.h> 1842fad570SMark Brown #include <linux/regulator/driver.h> 1942fad570SMark Brown #include <linux/mfd/wm8400-private.h> 2042fad570SMark Brown 2142fad570SMark Brown static int wm8400_ldo_is_enabled(struct regulator_dev *dev) 2242fad570SMark Brown { 2342fad570SMark Brown struct wm8400 *wm8400 = rdev_get_drvdata(dev); 2442fad570SMark Brown u16 val; 2542fad570SMark Brown 2642fad570SMark Brown val = wm8400_reg_read(wm8400, WM8400_LDO1_CONTROL + rdev_get_id(dev)); 2742fad570SMark Brown return (val & WM8400_LDO1_ENA) != 0; 2842fad570SMark Brown } 2942fad570SMark Brown 3042fad570SMark Brown static int wm8400_ldo_enable(struct regulator_dev *dev) 3142fad570SMark Brown { 3242fad570SMark Brown struct wm8400 *wm8400 = rdev_get_drvdata(dev); 3342fad570SMark Brown 3442fad570SMark Brown return wm8400_set_bits(wm8400, WM8400_LDO1_CONTROL + rdev_get_id(dev), 3542fad570SMark Brown WM8400_LDO1_ENA, WM8400_LDO1_ENA); 3642fad570SMark Brown } 3742fad570SMark Brown 3842fad570SMark Brown static int wm8400_ldo_disable(struct regulator_dev *dev) 3942fad570SMark Brown { 4042fad570SMark Brown struct wm8400 *wm8400 = rdev_get_drvdata(dev); 4142fad570SMark Brown 4242fad570SMark Brown return wm8400_set_bits(wm8400, WM8400_LDO1_CONTROL + rdev_get_id(dev), 4342fad570SMark Brown WM8400_LDO1_ENA, 0); 4442fad570SMark Brown } 4542fad570SMark Brown 46*216765d9SMark Brown static int wm8400_ldo_list_voltage(struct regulator_dev *dev, 47*216765d9SMark Brown unsigned selector) 48*216765d9SMark Brown { 49*216765d9SMark Brown if (selector > WM8400_LDO1_VSEL_MASK) 50*216765d9SMark Brown return -EINVAL; 51*216765d9SMark Brown 52*216765d9SMark Brown if (selector < 15) 53*216765d9SMark Brown return 900000 + (selector * 50000); 54*216765d9SMark Brown else 55*216765d9SMark Brown return 1600000 + ((selector - 14) * 100000); 56*216765d9SMark Brown } 57*216765d9SMark Brown 5842fad570SMark Brown static int wm8400_ldo_get_voltage(struct regulator_dev *dev) 5942fad570SMark Brown { 6042fad570SMark Brown struct wm8400 *wm8400 = rdev_get_drvdata(dev); 6142fad570SMark Brown u16 val; 6242fad570SMark Brown 6342fad570SMark Brown val = wm8400_reg_read(wm8400, WM8400_LDO1_CONTROL + rdev_get_id(dev)); 6442fad570SMark Brown val &= WM8400_LDO1_VSEL_MASK; 6542fad570SMark Brown 66*216765d9SMark Brown return wm8400_ldo_list_voltage(dev, val); 6742fad570SMark Brown } 6842fad570SMark Brown 6942fad570SMark Brown static int wm8400_ldo_set_voltage(struct regulator_dev *dev, 7042fad570SMark Brown int min_uV, int max_uV) 7142fad570SMark Brown { 7242fad570SMark Brown struct wm8400 *wm8400 = rdev_get_drvdata(dev); 7342fad570SMark Brown u16 val; 7442fad570SMark Brown 7542fad570SMark Brown if (min_uV < 900000 || min_uV > 3300000) 7642fad570SMark Brown return -EINVAL; 7742fad570SMark Brown 7842fad570SMark Brown if (min_uV < 1700000) { 7942fad570SMark Brown /* Steps of 50mV from 900mV; */ 8042fad570SMark Brown val = (min_uV - 850001) / 50000; 8142fad570SMark Brown 8242fad570SMark Brown if ((val * 50000) + 900000 > max_uV) 8342fad570SMark Brown return -EINVAL; 8442fad570SMark Brown BUG_ON((val * 50000) + 900000 < min_uV); 8542fad570SMark Brown } else { 8642fad570SMark Brown /* Steps of 100mV from 1700mV */ 8742fad570SMark Brown val = ((min_uV - 1600001) / 100000); 8842fad570SMark Brown 8942fad570SMark Brown if ((val * 100000) + 1700000 > max_uV) 9042fad570SMark Brown return -EINVAL; 9142fad570SMark Brown BUG_ON((val * 100000) + 1700000 < min_uV); 9242fad570SMark Brown 9342fad570SMark Brown val += 0xf; 9442fad570SMark Brown } 9542fad570SMark Brown 9642fad570SMark Brown return wm8400_set_bits(wm8400, WM8400_LDO1_CONTROL + rdev_get_id(dev), 9742fad570SMark Brown WM8400_LDO1_VSEL_MASK, val); 9842fad570SMark Brown } 9942fad570SMark Brown 10042fad570SMark Brown static struct regulator_ops wm8400_ldo_ops = { 10142fad570SMark Brown .is_enabled = wm8400_ldo_is_enabled, 10242fad570SMark Brown .enable = wm8400_ldo_enable, 10342fad570SMark Brown .disable = wm8400_ldo_disable, 104*216765d9SMark Brown .list_voltage = wm8400_ldo_list_voltage, 10542fad570SMark Brown .get_voltage = wm8400_ldo_get_voltage, 10642fad570SMark Brown .set_voltage = wm8400_ldo_set_voltage, 10742fad570SMark Brown }; 10842fad570SMark Brown 10942fad570SMark Brown static int wm8400_dcdc_is_enabled(struct regulator_dev *dev) 11042fad570SMark Brown { 11142fad570SMark Brown struct wm8400 *wm8400 = rdev_get_drvdata(dev); 11242fad570SMark Brown int offset = (rdev_get_id(dev) - WM8400_DCDC1) * 2; 11342fad570SMark Brown u16 val; 11442fad570SMark Brown 11542fad570SMark Brown val = wm8400_reg_read(wm8400, WM8400_DCDC1_CONTROL_1 + offset); 11642fad570SMark Brown return (val & WM8400_DC1_ENA) != 0; 11742fad570SMark Brown } 11842fad570SMark Brown 11942fad570SMark Brown static int wm8400_dcdc_enable(struct regulator_dev *dev) 12042fad570SMark Brown { 12142fad570SMark Brown struct wm8400 *wm8400 = rdev_get_drvdata(dev); 12242fad570SMark Brown int offset = (rdev_get_id(dev) - WM8400_DCDC1) * 2; 12342fad570SMark Brown 12442fad570SMark Brown return wm8400_set_bits(wm8400, WM8400_DCDC1_CONTROL_1 + offset, 12542fad570SMark Brown WM8400_DC1_ENA, WM8400_DC1_ENA); 12642fad570SMark Brown } 12742fad570SMark Brown 12842fad570SMark Brown static int wm8400_dcdc_disable(struct regulator_dev *dev) 12942fad570SMark Brown { 13042fad570SMark Brown struct wm8400 *wm8400 = rdev_get_drvdata(dev); 13142fad570SMark Brown int offset = (rdev_get_id(dev) - WM8400_DCDC1) * 2; 13242fad570SMark Brown 13342fad570SMark Brown return wm8400_set_bits(wm8400, WM8400_DCDC1_CONTROL_1 + offset, 13442fad570SMark Brown WM8400_DC1_ENA, 0); 13542fad570SMark Brown } 13642fad570SMark Brown 137*216765d9SMark Brown static int wm8400_dcdc_list_voltage(struct regulator_dev *dev, 138*216765d9SMark Brown unsigned selector) 139*216765d9SMark Brown { 140*216765d9SMark Brown if (selector > WM8400_DC1_VSEL_MASK) 141*216765d9SMark Brown return -EINVAL; 142*216765d9SMark Brown 143*216765d9SMark Brown return 850000 + (selector * 25000); 144*216765d9SMark Brown } 145*216765d9SMark Brown 14642fad570SMark Brown static int wm8400_dcdc_get_voltage(struct regulator_dev *dev) 14742fad570SMark Brown { 14842fad570SMark Brown struct wm8400 *wm8400 = rdev_get_drvdata(dev); 14942fad570SMark Brown u16 val; 15042fad570SMark Brown int offset = (rdev_get_id(dev) - WM8400_DCDC1) * 2; 15142fad570SMark Brown 15242fad570SMark Brown val = wm8400_reg_read(wm8400, WM8400_DCDC1_CONTROL_1 + offset); 15342fad570SMark Brown val &= WM8400_DC1_VSEL_MASK; 15442fad570SMark Brown 15542fad570SMark Brown return 850000 + (25000 * val); 15642fad570SMark Brown } 15742fad570SMark Brown 15842fad570SMark Brown static int wm8400_dcdc_set_voltage(struct regulator_dev *dev, 15942fad570SMark Brown int min_uV, int max_uV) 16042fad570SMark Brown { 16142fad570SMark Brown struct wm8400 *wm8400 = rdev_get_drvdata(dev); 16242fad570SMark Brown u16 val; 16342fad570SMark Brown int offset = (rdev_get_id(dev) - WM8400_DCDC1) * 2; 16442fad570SMark Brown 16542fad570SMark Brown if (min_uV < 850000) 16642fad570SMark Brown return -EINVAL; 16742fad570SMark Brown 16842fad570SMark Brown val = (min_uV - 825001) / 25000; 16942fad570SMark Brown 17042fad570SMark Brown if (850000 + (25000 * val) > max_uV) 17142fad570SMark Brown return -EINVAL; 17242fad570SMark Brown BUG_ON(850000 + (25000 * val) < min_uV); 17342fad570SMark Brown 17442fad570SMark Brown return wm8400_set_bits(wm8400, WM8400_DCDC1_CONTROL_1 + offset, 17542fad570SMark Brown WM8400_DC1_VSEL_MASK, val); 17642fad570SMark Brown } 17742fad570SMark Brown 17842fad570SMark Brown static unsigned int wm8400_dcdc_get_mode(struct regulator_dev *dev) 17942fad570SMark Brown { 18042fad570SMark Brown struct wm8400 *wm8400 = rdev_get_drvdata(dev); 18142fad570SMark Brown int offset = (rdev_get_id(dev) - WM8400_DCDC1) * 2; 18242fad570SMark Brown u16 data[2]; 18342fad570SMark Brown int ret; 18442fad570SMark Brown 18542fad570SMark Brown ret = wm8400_block_read(wm8400, WM8400_DCDC1_CONTROL_1 + offset, 2, 18642fad570SMark Brown data); 18742fad570SMark Brown if (ret != 0) 18842fad570SMark Brown return 0; 18942fad570SMark Brown 19042fad570SMark Brown /* Datasheet: hibernate */ 19142fad570SMark Brown if (data[0] & WM8400_DC1_SLEEP) 19242fad570SMark Brown return REGULATOR_MODE_STANDBY; 19342fad570SMark Brown 19442fad570SMark Brown /* Datasheet: standby */ 19542fad570SMark Brown if (!(data[0] & WM8400_DC1_ACTIVE)) 19642fad570SMark Brown return REGULATOR_MODE_IDLE; 19742fad570SMark Brown 19842fad570SMark Brown /* Datasheet: active with or without force PWM */ 19942fad570SMark Brown if (data[1] & WM8400_DC1_FRC_PWM) 20042fad570SMark Brown return REGULATOR_MODE_FAST; 20142fad570SMark Brown else 20242fad570SMark Brown return REGULATOR_MODE_NORMAL; 20342fad570SMark Brown } 20442fad570SMark Brown 20542fad570SMark Brown static int wm8400_dcdc_set_mode(struct regulator_dev *dev, unsigned int mode) 20642fad570SMark Brown { 20742fad570SMark Brown struct wm8400 *wm8400 = rdev_get_drvdata(dev); 20842fad570SMark Brown int offset = (rdev_get_id(dev) - WM8400_DCDC1) * 2; 20942fad570SMark Brown int ret; 21042fad570SMark Brown 21142fad570SMark Brown switch (mode) { 21242fad570SMark Brown case REGULATOR_MODE_FAST: 21342fad570SMark Brown /* Datasheet: active with force PWM */ 21442fad570SMark Brown ret = wm8400_set_bits(wm8400, WM8400_DCDC1_CONTROL_2 + offset, 21542fad570SMark Brown WM8400_DC1_FRC_PWM, WM8400_DC1_FRC_PWM); 21642fad570SMark Brown if (ret != 0) 21742fad570SMark Brown return ret; 21842fad570SMark Brown 21942fad570SMark Brown return wm8400_set_bits(wm8400, WM8400_DCDC1_CONTROL_1 + offset, 22042fad570SMark Brown WM8400_DC1_ACTIVE | WM8400_DC1_SLEEP, 22142fad570SMark Brown WM8400_DC1_ACTIVE); 22242fad570SMark Brown 22342fad570SMark Brown case REGULATOR_MODE_NORMAL: 22442fad570SMark Brown /* Datasheet: active */ 22542fad570SMark Brown ret = wm8400_set_bits(wm8400, WM8400_DCDC1_CONTROL_2 + offset, 22642fad570SMark Brown WM8400_DC1_FRC_PWM, 0); 22742fad570SMark Brown if (ret != 0) 22842fad570SMark Brown return ret; 22942fad570SMark Brown 23042fad570SMark Brown return wm8400_set_bits(wm8400, WM8400_DCDC1_CONTROL_1 + offset, 23142fad570SMark Brown WM8400_DC1_ACTIVE | WM8400_DC1_SLEEP, 23242fad570SMark Brown WM8400_DC1_ACTIVE); 23342fad570SMark Brown 23442fad570SMark Brown case REGULATOR_MODE_IDLE: 23542fad570SMark Brown /* Datasheet: standby */ 23642fad570SMark Brown ret = wm8400_set_bits(wm8400, WM8400_DCDC1_CONTROL_1 + offset, 23742fad570SMark Brown WM8400_DC1_ACTIVE, 0); 23842fad570SMark Brown if (ret != 0) 23942fad570SMark Brown return ret; 24042fad570SMark Brown return wm8400_set_bits(wm8400, WM8400_DCDC1_CONTROL_1 + offset, 24142fad570SMark Brown WM8400_DC1_SLEEP, 0); 24242fad570SMark Brown 24342fad570SMark Brown default: 24442fad570SMark Brown return -EINVAL; 24542fad570SMark Brown } 24642fad570SMark Brown } 24742fad570SMark Brown 24842fad570SMark Brown static unsigned int wm8400_dcdc_get_optimum_mode(struct regulator_dev *dev, 24942fad570SMark Brown int input_uV, int output_uV, 25042fad570SMark Brown int load_uA) 25142fad570SMark Brown { 25242fad570SMark Brown return REGULATOR_MODE_NORMAL; 25342fad570SMark Brown } 25442fad570SMark Brown 25542fad570SMark Brown static struct regulator_ops wm8400_dcdc_ops = { 25642fad570SMark Brown .is_enabled = wm8400_dcdc_is_enabled, 25742fad570SMark Brown .enable = wm8400_dcdc_enable, 25842fad570SMark Brown .disable = wm8400_dcdc_disable, 259*216765d9SMark Brown .list_voltage = wm8400_dcdc_list_voltage, 26042fad570SMark Brown .get_voltage = wm8400_dcdc_get_voltage, 26142fad570SMark Brown .set_voltage = wm8400_dcdc_set_voltage, 26242fad570SMark Brown .get_mode = wm8400_dcdc_get_mode, 26342fad570SMark Brown .set_mode = wm8400_dcdc_set_mode, 26442fad570SMark Brown .get_optimum_mode = wm8400_dcdc_get_optimum_mode, 26542fad570SMark Brown }; 26642fad570SMark Brown 26742fad570SMark Brown static struct regulator_desc regulators[] = { 26842fad570SMark Brown { 26942fad570SMark Brown .name = "LDO1", 27042fad570SMark Brown .id = WM8400_LDO1, 27142fad570SMark Brown .ops = &wm8400_ldo_ops, 272*216765d9SMark Brown .n_voltages = WM8400_LDO1_VSEL_MASK + 1, 27342fad570SMark Brown .type = REGULATOR_VOLTAGE, 27442fad570SMark Brown .owner = THIS_MODULE, 27542fad570SMark Brown }, 27642fad570SMark Brown { 27742fad570SMark Brown .name = "LDO2", 27842fad570SMark Brown .id = WM8400_LDO2, 27942fad570SMark Brown .ops = &wm8400_ldo_ops, 280*216765d9SMark Brown .n_voltages = WM8400_LDO2_VSEL_MASK + 1, 28142fad570SMark Brown .type = REGULATOR_VOLTAGE, 28242fad570SMark Brown .owner = THIS_MODULE, 28342fad570SMark Brown }, 28442fad570SMark Brown { 28542fad570SMark Brown .name = "LDO3", 28642fad570SMark Brown .id = WM8400_LDO3, 28742fad570SMark Brown .ops = &wm8400_ldo_ops, 288*216765d9SMark Brown .n_voltages = WM8400_LDO3_VSEL_MASK + 1, 28942fad570SMark Brown .type = REGULATOR_VOLTAGE, 29042fad570SMark Brown .owner = THIS_MODULE, 29142fad570SMark Brown }, 29242fad570SMark Brown { 29342fad570SMark Brown .name = "LDO4", 29442fad570SMark Brown .id = WM8400_LDO4, 29542fad570SMark Brown .ops = &wm8400_ldo_ops, 296*216765d9SMark Brown .n_voltages = WM8400_LDO4_VSEL_MASK + 1, 29742fad570SMark Brown .type = REGULATOR_VOLTAGE, 29842fad570SMark Brown .owner = THIS_MODULE, 29942fad570SMark Brown }, 30042fad570SMark Brown { 30142fad570SMark Brown .name = "DCDC1", 30242fad570SMark Brown .id = WM8400_DCDC1, 30342fad570SMark Brown .ops = &wm8400_dcdc_ops, 304*216765d9SMark Brown .n_voltages = WM8400_DC1_VSEL_MASK + 1, 30542fad570SMark Brown .type = REGULATOR_VOLTAGE, 30642fad570SMark Brown .owner = THIS_MODULE, 30742fad570SMark Brown }, 30842fad570SMark Brown { 30942fad570SMark Brown .name = "DCDC2", 31042fad570SMark Brown .id = WM8400_DCDC2, 31142fad570SMark Brown .ops = &wm8400_dcdc_ops, 312*216765d9SMark Brown .n_voltages = WM8400_DC2_VSEL_MASK + 1, 31342fad570SMark Brown .type = REGULATOR_VOLTAGE, 31442fad570SMark Brown .owner = THIS_MODULE, 31542fad570SMark Brown }, 31642fad570SMark Brown }; 31742fad570SMark Brown 3185dbdf735SUwe Kleine-König static int __devinit wm8400_regulator_probe(struct platform_device *pdev) 31942fad570SMark Brown { 32042fad570SMark Brown struct regulator_dev *rdev; 32142fad570SMark Brown 32242fad570SMark Brown rdev = regulator_register(®ulators[pdev->id], &pdev->dev, 3230527100fSMark Brown pdev->dev.platform_data, pdev->dev.driver_data); 32442fad570SMark Brown 32542fad570SMark Brown if (IS_ERR(rdev)) 32642fad570SMark Brown return PTR_ERR(rdev); 32742fad570SMark Brown 32842fad570SMark Brown return 0; 32942fad570SMark Brown } 33042fad570SMark Brown 33142fad570SMark Brown static int __devexit wm8400_regulator_remove(struct platform_device *pdev) 33242fad570SMark Brown { 33342fad570SMark Brown struct regulator_dev *rdev = platform_get_drvdata(pdev); 33442fad570SMark Brown 33542fad570SMark Brown regulator_unregister(rdev); 33642fad570SMark Brown 33742fad570SMark Brown return 0; 33842fad570SMark Brown } 33942fad570SMark Brown 34042fad570SMark Brown static struct platform_driver wm8400_regulator_driver = { 34142fad570SMark Brown .driver = { 34242fad570SMark Brown .name = "wm8400-regulator", 34342fad570SMark Brown }, 34442fad570SMark Brown .probe = wm8400_regulator_probe, 34542fad570SMark Brown .remove = __devexit_p(wm8400_regulator_remove), 34642fad570SMark Brown }; 34742fad570SMark Brown 34842fad570SMark Brown /** 34942fad570SMark Brown * wm8400_register_regulator - enable software control of a WM8400 regulator 35042fad570SMark Brown * 35142fad570SMark Brown * This function enables software control of a WM8400 regulator via 35242fad570SMark Brown * the regulator API. It is intended to be called from the 35342fad570SMark Brown * platform_init() callback of the WM8400 MFD driver. 35442fad570SMark Brown * 35542fad570SMark Brown * @param dev The WM8400 device to operate on. 35642fad570SMark Brown * @param reg The regulator to control. 35742fad570SMark Brown * @param initdata Regulator initdata for the regulator. 35842fad570SMark Brown */ 35942fad570SMark Brown int wm8400_register_regulator(struct device *dev, int reg, 36042fad570SMark Brown struct regulator_init_data *initdata) 36142fad570SMark Brown { 36242fad570SMark Brown struct wm8400 *wm8400 = dev->driver_data; 36342fad570SMark Brown 36442fad570SMark Brown if (wm8400->regulators[reg].name) 36542fad570SMark Brown return -EBUSY; 36642fad570SMark Brown 36742fad570SMark Brown initdata->driver_data = wm8400; 36842fad570SMark Brown 36942fad570SMark Brown wm8400->regulators[reg].name = "wm8400-regulator"; 37042fad570SMark Brown wm8400->regulators[reg].id = reg; 37142fad570SMark Brown wm8400->regulators[reg].dev.parent = dev; 37242fad570SMark Brown wm8400->regulators[reg].dev.driver_data = wm8400; 37342fad570SMark Brown wm8400->regulators[reg].dev.platform_data = initdata; 37442fad570SMark Brown 37542fad570SMark Brown return platform_device_register(&wm8400->regulators[reg]); 37642fad570SMark Brown } 37742fad570SMark Brown EXPORT_SYMBOL_GPL(wm8400_register_regulator); 37842fad570SMark Brown 37942fad570SMark Brown static int __init wm8400_regulator_init(void) 38042fad570SMark Brown { 38142fad570SMark Brown return platform_driver_register(&wm8400_regulator_driver); 38242fad570SMark Brown } 38342fad570SMark Brown module_init(wm8400_regulator_init); 38442fad570SMark Brown 38542fad570SMark Brown static void __exit wm8400_regulator_exit(void) 38642fad570SMark Brown { 38742fad570SMark Brown platform_driver_unregister(&wm8400_regulator_driver); 38842fad570SMark Brown } 38942fad570SMark Brown module_exit(wm8400_regulator_exit); 39042fad570SMark Brown 39142fad570SMark Brown MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>"); 39242fad570SMark Brown MODULE_DESCRIPTION("WM8400 regulator driver"); 39342fad570SMark Brown MODULE_LICENSE("GPL"); 39442fad570SMark Brown MODULE_ALIAS("platform:wm8400-regulator"); 395