18c2ecf20Sopenharmony_ci/* 28c2ecf20Sopenharmony_ci * tps65217-regulator.c 38c2ecf20Sopenharmony_ci * 48c2ecf20Sopenharmony_ci * Regulator driver for TPS65217 PMIC 58c2ecf20Sopenharmony_ci * 68c2ecf20Sopenharmony_ci * Copyright (C) 2011 Texas Instruments Incorporated - https://www.ti.com/ 78c2ecf20Sopenharmony_ci * 88c2ecf20Sopenharmony_ci * This program is free software; you can redistribute it and/or 98c2ecf20Sopenharmony_ci * modify it under the terms of the GNU General Public License as 108c2ecf20Sopenharmony_ci * published by the Free Software Foundation version 2. 118c2ecf20Sopenharmony_ci * 128c2ecf20Sopenharmony_ci * This program is distributed "as is" WITHOUT ANY WARRANTY of any 138c2ecf20Sopenharmony_ci * kind, whether express or implied; without even the implied warranty 148c2ecf20Sopenharmony_ci * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 158c2ecf20Sopenharmony_ci * GNU General Public License for more details. 168c2ecf20Sopenharmony_ci */ 178c2ecf20Sopenharmony_ci 188c2ecf20Sopenharmony_ci#include <linux/kernel.h> 198c2ecf20Sopenharmony_ci#include <linux/module.h> 208c2ecf20Sopenharmony_ci#include <linux/device.h> 218c2ecf20Sopenharmony_ci#include <linux/init.h> 228c2ecf20Sopenharmony_ci#include <linux/err.h> 238c2ecf20Sopenharmony_ci#include <linux/platform_device.h> 248c2ecf20Sopenharmony_ci 258c2ecf20Sopenharmony_ci#include <linux/regulator/of_regulator.h> 268c2ecf20Sopenharmony_ci#include <linux/regulator/driver.h> 278c2ecf20Sopenharmony_ci#include <linux/regulator/machine.h> 288c2ecf20Sopenharmony_ci#include <linux/mfd/tps65217.h> 298c2ecf20Sopenharmony_ci 308c2ecf20Sopenharmony_ci#define TPS65217_REGULATOR(_name, _id, _of_match, _ops, _n, _vr, _vm, _em, \ 318c2ecf20Sopenharmony_ci _t, _lr, _nlr, _sr, _sm) \ 328c2ecf20Sopenharmony_ci { \ 338c2ecf20Sopenharmony_ci .name = _name, \ 348c2ecf20Sopenharmony_ci .id = _id, \ 358c2ecf20Sopenharmony_ci .of_match = of_match_ptr(_of_match), \ 368c2ecf20Sopenharmony_ci .regulators_node= of_match_ptr("regulators"), \ 378c2ecf20Sopenharmony_ci .ops = &_ops, \ 388c2ecf20Sopenharmony_ci .n_voltages = _n, \ 398c2ecf20Sopenharmony_ci .type = REGULATOR_VOLTAGE, \ 408c2ecf20Sopenharmony_ci .owner = THIS_MODULE, \ 418c2ecf20Sopenharmony_ci .vsel_reg = _vr, \ 428c2ecf20Sopenharmony_ci .vsel_mask = _vm, \ 438c2ecf20Sopenharmony_ci .enable_reg = TPS65217_REG_ENABLE, \ 448c2ecf20Sopenharmony_ci .enable_mask = _em, \ 458c2ecf20Sopenharmony_ci .volt_table = _t, \ 468c2ecf20Sopenharmony_ci .linear_ranges = _lr, \ 478c2ecf20Sopenharmony_ci .n_linear_ranges = _nlr, \ 488c2ecf20Sopenharmony_ci .bypass_reg = _sr, \ 498c2ecf20Sopenharmony_ci .bypass_mask = _sm, \ 508c2ecf20Sopenharmony_ci } \ 518c2ecf20Sopenharmony_ci 528c2ecf20Sopenharmony_cistatic const unsigned int LDO1_VSEL_table[] = { 538c2ecf20Sopenharmony_ci 1000000, 1100000, 1200000, 1250000, 548c2ecf20Sopenharmony_ci 1300000, 1350000, 1400000, 1500000, 558c2ecf20Sopenharmony_ci 1600000, 1800000, 2500000, 2750000, 568c2ecf20Sopenharmony_ci 2800000, 3000000, 3100000, 3300000, 578c2ecf20Sopenharmony_ci}; 588c2ecf20Sopenharmony_ci 598c2ecf20Sopenharmony_cistatic const struct linear_range tps65217_uv1_ranges[] = { 608c2ecf20Sopenharmony_ci REGULATOR_LINEAR_RANGE(900000, 0, 24, 25000), 618c2ecf20Sopenharmony_ci REGULATOR_LINEAR_RANGE(1550000, 25, 52, 50000), 628c2ecf20Sopenharmony_ci REGULATOR_LINEAR_RANGE(3000000, 53, 55, 100000), 638c2ecf20Sopenharmony_ci REGULATOR_LINEAR_RANGE(3300000, 56, 63, 0), 648c2ecf20Sopenharmony_ci}; 658c2ecf20Sopenharmony_ci 668c2ecf20Sopenharmony_cistatic const struct linear_range tps65217_uv2_ranges[] = { 678c2ecf20Sopenharmony_ci REGULATOR_LINEAR_RANGE(1500000, 0, 8, 50000), 688c2ecf20Sopenharmony_ci REGULATOR_LINEAR_RANGE(2000000, 9, 13, 100000), 698c2ecf20Sopenharmony_ci REGULATOR_LINEAR_RANGE(2450000, 14, 31, 50000), 708c2ecf20Sopenharmony_ci}; 718c2ecf20Sopenharmony_ci 728c2ecf20Sopenharmony_cistatic int tps65217_pmic_enable(struct regulator_dev *dev) 738c2ecf20Sopenharmony_ci{ 748c2ecf20Sopenharmony_ci struct tps65217 *tps = rdev_get_drvdata(dev); 758c2ecf20Sopenharmony_ci int rid = rdev_get_id(dev); 768c2ecf20Sopenharmony_ci 778c2ecf20Sopenharmony_ci if (rid < TPS65217_DCDC_1 || rid > TPS65217_LDO_4) 788c2ecf20Sopenharmony_ci return -EINVAL; 798c2ecf20Sopenharmony_ci 808c2ecf20Sopenharmony_ci /* Enable the regulator and password protection is level 1 */ 818c2ecf20Sopenharmony_ci return tps65217_set_bits(tps, TPS65217_REG_ENABLE, 828c2ecf20Sopenharmony_ci dev->desc->enable_mask, dev->desc->enable_mask, 838c2ecf20Sopenharmony_ci TPS65217_PROTECT_L1); 848c2ecf20Sopenharmony_ci} 858c2ecf20Sopenharmony_ci 868c2ecf20Sopenharmony_cistatic int tps65217_pmic_disable(struct regulator_dev *dev) 878c2ecf20Sopenharmony_ci{ 888c2ecf20Sopenharmony_ci struct tps65217 *tps = rdev_get_drvdata(dev); 898c2ecf20Sopenharmony_ci int rid = rdev_get_id(dev); 908c2ecf20Sopenharmony_ci 918c2ecf20Sopenharmony_ci if (rid < TPS65217_DCDC_1 || rid > TPS65217_LDO_4) 928c2ecf20Sopenharmony_ci return -EINVAL; 938c2ecf20Sopenharmony_ci 948c2ecf20Sopenharmony_ci /* Disable the regulator and password protection is level 1 */ 958c2ecf20Sopenharmony_ci return tps65217_clear_bits(tps, TPS65217_REG_ENABLE, 968c2ecf20Sopenharmony_ci dev->desc->enable_mask, TPS65217_PROTECT_L1); 978c2ecf20Sopenharmony_ci} 988c2ecf20Sopenharmony_ci 998c2ecf20Sopenharmony_cistatic int tps65217_pmic_set_voltage_sel(struct regulator_dev *dev, 1008c2ecf20Sopenharmony_ci unsigned selector) 1018c2ecf20Sopenharmony_ci{ 1028c2ecf20Sopenharmony_ci int ret; 1038c2ecf20Sopenharmony_ci struct tps65217 *tps = rdev_get_drvdata(dev); 1048c2ecf20Sopenharmony_ci unsigned int rid = rdev_get_id(dev); 1058c2ecf20Sopenharmony_ci 1068c2ecf20Sopenharmony_ci /* Set the voltage based on vsel value and write protect level is 2 */ 1078c2ecf20Sopenharmony_ci ret = tps65217_set_bits(tps, dev->desc->vsel_reg, dev->desc->vsel_mask, 1088c2ecf20Sopenharmony_ci selector, TPS65217_PROTECT_L2); 1098c2ecf20Sopenharmony_ci 1108c2ecf20Sopenharmony_ci /* Set GO bit for DCDCx to initiate voltage transistion */ 1118c2ecf20Sopenharmony_ci switch (rid) { 1128c2ecf20Sopenharmony_ci case TPS65217_DCDC_1 ... TPS65217_DCDC_3: 1138c2ecf20Sopenharmony_ci ret = tps65217_set_bits(tps, TPS65217_REG_DEFSLEW, 1148c2ecf20Sopenharmony_ci TPS65217_DEFSLEW_GO, TPS65217_DEFSLEW_GO, 1158c2ecf20Sopenharmony_ci TPS65217_PROTECT_L2); 1168c2ecf20Sopenharmony_ci break; 1178c2ecf20Sopenharmony_ci } 1188c2ecf20Sopenharmony_ci 1198c2ecf20Sopenharmony_ci return ret; 1208c2ecf20Sopenharmony_ci} 1218c2ecf20Sopenharmony_ci 1228c2ecf20Sopenharmony_cistatic int tps65217_pmic_set_suspend_enable(struct regulator_dev *dev) 1238c2ecf20Sopenharmony_ci{ 1248c2ecf20Sopenharmony_ci struct tps65217 *tps = rdev_get_drvdata(dev); 1258c2ecf20Sopenharmony_ci unsigned int rid = rdev_get_id(dev); 1268c2ecf20Sopenharmony_ci 1278c2ecf20Sopenharmony_ci if (rid > TPS65217_LDO_4) 1288c2ecf20Sopenharmony_ci return -EINVAL; 1298c2ecf20Sopenharmony_ci 1308c2ecf20Sopenharmony_ci return tps65217_clear_bits(tps, dev->desc->bypass_reg, 1318c2ecf20Sopenharmony_ci dev->desc->bypass_mask, 1328c2ecf20Sopenharmony_ci TPS65217_PROTECT_L1); 1338c2ecf20Sopenharmony_ci} 1348c2ecf20Sopenharmony_ci 1358c2ecf20Sopenharmony_cistatic int tps65217_pmic_set_suspend_disable(struct regulator_dev *dev) 1368c2ecf20Sopenharmony_ci{ 1378c2ecf20Sopenharmony_ci struct tps65217 *tps = rdev_get_drvdata(dev); 1388c2ecf20Sopenharmony_ci unsigned int rid = rdev_get_id(dev); 1398c2ecf20Sopenharmony_ci 1408c2ecf20Sopenharmony_ci if (rid > TPS65217_LDO_4) 1418c2ecf20Sopenharmony_ci return -EINVAL; 1428c2ecf20Sopenharmony_ci 1438c2ecf20Sopenharmony_ci if (!tps->strobes[rid]) 1448c2ecf20Sopenharmony_ci return -EINVAL; 1458c2ecf20Sopenharmony_ci 1468c2ecf20Sopenharmony_ci return tps65217_set_bits(tps, dev->desc->bypass_reg, 1478c2ecf20Sopenharmony_ci dev->desc->bypass_mask, 1488c2ecf20Sopenharmony_ci tps->strobes[rid], TPS65217_PROTECT_L1); 1498c2ecf20Sopenharmony_ci} 1508c2ecf20Sopenharmony_ci 1518c2ecf20Sopenharmony_ci/* Operations permitted on DCDCx, LDO2, LDO3 and LDO4 */ 1528c2ecf20Sopenharmony_cistatic const struct regulator_ops tps65217_pmic_ops = { 1538c2ecf20Sopenharmony_ci .is_enabled = regulator_is_enabled_regmap, 1548c2ecf20Sopenharmony_ci .enable = tps65217_pmic_enable, 1558c2ecf20Sopenharmony_ci .disable = tps65217_pmic_disable, 1568c2ecf20Sopenharmony_ci .get_voltage_sel = regulator_get_voltage_sel_regmap, 1578c2ecf20Sopenharmony_ci .set_voltage_sel = tps65217_pmic_set_voltage_sel, 1588c2ecf20Sopenharmony_ci .list_voltage = regulator_list_voltage_linear_range, 1598c2ecf20Sopenharmony_ci .map_voltage = regulator_map_voltage_linear_range, 1608c2ecf20Sopenharmony_ci .set_suspend_enable = tps65217_pmic_set_suspend_enable, 1618c2ecf20Sopenharmony_ci .set_suspend_disable = tps65217_pmic_set_suspend_disable, 1628c2ecf20Sopenharmony_ci}; 1638c2ecf20Sopenharmony_ci 1648c2ecf20Sopenharmony_ci/* Operations permitted on LDO1 */ 1658c2ecf20Sopenharmony_cistatic const struct regulator_ops tps65217_pmic_ldo1_ops = { 1668c2ecf20Sopenharmony_ci .is_enabled = regulator_is_enabled_regmap, 1678c2ecf20Sopenharmony_ci .enable = tps65217_pmic_enable, 1688c2ecf20Sopenharmony_ci .disable = tps65217_pmic_disable, 1698c2ecf20Sopenharmony_ci .get_voltage_sel = regulator_get_voltage_sel_regmap, 1708c2ecf20Sopenharmony_ci .set_voltage_sel = tps65217_pmic_set_voltage_sel, 1718c2ecf20Sopenharmony_ci .list_voltage = regulator_list_voltage_table, 1728c2ecf20Sopenharmony_ci .map_voltage = regulator_map_voltage_ascend, 1738c2ecf20Sopenharmony_ci .set_suspend_enable = tps65217_pmic_set_suspend_enable, 1748c2ecf20Sopenharmony_ci .set_suspend_disable = tps65217_pmic_set_suspend_disable, 1758c2ecf20Sopenharmony_ci}; 1768c2ecf20Sopenharmony_ci 1778c2ecf20Sopenharmony_cistatic const struct regulator_desc regulators[] = { 1788c2ecf20Sopenharmony_ci TPS65217_REGULATOR("DCDC1", TPS65217_DCDC_1, "dcdc1", 1798c2ecf20Sopenharmony_ci tps65217_pmic_ops, 64, TPS65217_REG_DEFDCDC1, 1808c2ecf20Sopenharmony_ci TPS65217_DEFDCDCX_DCDC_MASK, TPS65217_ENABLE_DC1_EN, 1818c2ecf20Sopenharmony_ci NULL, tps65217_uv1_ranges, 1828c2ecf20Sopenharmony_ci ARRAY_SIZE(tps65217_uv1_ranges), TPS65217_REG_SEQ1, 1838c2ecf20Sopenharmony_ci TPS65217_SEQ1_DC1_SEQ_MASK), 1848c2ecf20Sopenharmony_ci TPS65217_REGULATOR("DCDC2", TPS65217_DCDC_2, "dcdc2", 1858c2ecf20Sopenharmony_ci tps65217_pmic_ops, 64, TPS65217_REG_DEFDCDC2, 1868c2ecf20Sopenharmony_ci TPS65217_DEFDCDCX_DCDC_MASK, TPS65217_ENABLE_DC2_EN, 1878c2ecf20Sopenharmony_ci NULL, tps65217_uv1_ranges, 1888c2ecf20Sopenharmony_ci ARRAY_SIZE(tps65217_uv1_ranges), TPS65217_REG_SEQ1, 1898c2ecf20Sopenharmony_ci TPS65217_SEQ1_DC2_SEQ_MASK), 1908c2ecf20Sopenharmony_ci TPS65217_REGULATOR("DCDC3", TPS65217_DCDC_3, "dcdc3", 1918c2ecf20Sopenharmony_ci tps65217_pmic_ops, 64, TPS65217_REG_DEFDCDC3, 1928c2ecf20Sopenharmony_ci TPS65217_DEFDCDCX_DCDC_MASK, TPS65217_ENABLE_DC3_EN, 1938c2ecf20Sopenharmony_ci NULL, tps65217_uv1_ranges, 1948c2ecf20Sopenharmony_ci ARRAY_SIZE(tps65217_uv1_ranges), TPS65217_REG_SEQ2, 1958c2ecf20Sopenharmony_ci TPS65217_SEQ2_DC3_SEQ_MASK), 1968c2ecf20Sopenharmony_ci TPS65217_REGULATOR("LDO1", TPS65217_LDO_1, "ldo1", 1978c2ecf20Sopenharmony_ci tps65217_pmic_ldo1_ops, 16, TPS65217_REG_DEFLDO1, 1988c2ecf20Sopenharmony_ci TPS65217_DEFLDO1_LDO1_MASK, TPS65217_ENABLE_LDO1_EN, 1998c2ecf20Sopenharmony_ci LDO1_VSEL_table, NULL, 0, TPS65217_REG_SEQ2, 2008c2ecf20Sopenharmony_ci TPS65217_SEQ2_LDO1_SEQ_MASK), 2018c2ecf20Sopenharmony_ci TPS65217_REGULATOR("LDO2", TPS65217_LDO_2, "ldo2", tps65217_pmic_ops, 2028c2ecf20Sopenharmony_ci 64, TPS65217_REG_DEFLDO2, 2038c2ecf20Sopenharmony_ci TPS65217_DEFLDO2_LDO2_MASK, TPS65217_ENABLE_LDO2_EN, 2048c2ecf20Sopenharmony_ci NULL, tps65217_uv1_ranges, 2058c2ecf20Sopenharmony_ci ARRAY_SIZE(tps65217_uv1_ranges), TPS65217_REG_SEQ3, 2068c2ecf20Sopenharmony_ci TPS65217_SEQ3_LDO2_SEQ_MASK), 2078c2ecf20Sopenharmony_ci TPS65217_REGULATOR("LDO3", TPS65217_LDO_3, "ldo3", tps65217_pmic_ops, 2088c2ecf20Sopenharmony_ci 32, TPS65217_REG_DEFLS1, TPS65217_DEFLDO3_LDO3_MASK, 2098c2ecf20Sopenharmony_ci TPS65217_ENABLE_LS1_EN | TPS65217_DEFLDO3_LDO3_EN, 2108c2ecf20Sopenharmony_ci NULL, tps65217_uv2_ranges, 2118c2ecf20Sopenharmony_ci ARRAY_SIZE(tps65217_uv2_ranges), TPS65217_REG_SEQ3, 2128c2ecf20Sopenharmony_ci TPS65217_SEQ3_LDO3_SEQ_MASK), 2138c2ecf20Sopenharmony_ci TPS65217_REGULATOR("LDO4", TPS65217_LDO_4, "ldo4", tps65217_pmic_ops, 2148c2ecf20Sopenharmony_ci 32, TPS65217_REG_DEFLS2, TPS65217_DEFLDO4_LDO4_MASK, 2158c2ecf20Sopenharmony_ci TPS65217_ENABLE_LS2_EN | TPS65217_DEFLDO4_LDO4_EN, 2168c2ecf20Sopenharmony_ci NULL, tps65217_uv2_ranges, 2178c2ecf20Sopenharmony_ci ARRAY_SIZE(tps65217_uv2_ranges), TPS65217_REG_SEQ4, 2188c2ecf20Sopenharmony_ci TPS65217_SEQ4_LDO4_SEQ_MASK), 2198c2ecf20Sopenharmony_ci}; 2208c2ecf20Sopenharmony_ci 2218c2ecf20Sopenharmony_cistatic int tps65217_regulator_probe(struct platform_device *pdev) 2228c2ecf20Sopenharmony_ci{ 2238c2ecf20Sopenharmony_ci struct tps65217 *tps = dev_get_drvdata(pdev->dev.parent); 2248c2ecf20Sopenharmony_ci struct tps65217_board *pdata = dev_get_platdata(tps->dev); 2258c2ecf20Sopenharmony_ci struct regulator_dev *rdev; 2268c2ecf20Sopenharmony_ci struct regulator_config config = { }; 2278c2ecf20Sopenharmony_ci int i, ret; 2288c2ecf20Sopenharmony_ci unsigned int val; 2298c2ecf20Sopenharmony_ci 2308c2ecf20Sopenharmony_ci /* Allocate memory for strobes */ 2318c2ecf20Sopenharmony_ci tps->strobes = devm_kcalloc(&pdev->dev, 2328c2ecf20Sopenharmony_ci TPS65217_NUM_REGULATOR, sizeof(u8), 2338c2ecf20Sopenharmony_ci GFP_KERNEL); 2348c2ecf20Sopenharmony_ci if (!tps->strobes) 2358c2ecf20Sopenharmony_ci return -ENOMEM; 2368c2ecf20Sopenharmony_ci 2378c2ecf20Sopenharmony_ci platform_set_drvdata(pdev, tps); 2388c2ecf20Sopenharmony_ci 2398c2ecf20Sopenharmony_ci for (i = 0; i < TPS65217_NUM_REGULATOR; i++) { 2408c2ecf20Sopenharmony_ci /* Register the regulators */ 2418c2ecf20Sopenharmony_ci config.dev = tps->dev; 2428c2ecf20Sopenharmony_ci if (pdata) 2438c2ecf20Sopenharmony_ci config.init_data = pdata->tps65217_init_data[i]; 2448c2ecf20Sopenharmony_ci config.driver_data = tps; 2458c2ecf20Sopenharmony_ci config.regmap = tps->regmap; 2468c2ecf20Sopenharmony_ci 2478c2ecf20Sopenharmony_ci rdev = devm_regulator_register(&pdev->dev, ®ulators[i], 2488c2ecf20Sopenharmony_ci &config); 2498c2ecf20Sopenharmony_ci if (IS_ERR(rdev)) { 2508c2ecf20Sopenharmony_ci dev_err(tps->dev, "failed to register %s regulator\n", 2518c2ecf20Sopenharmony_ci pdev->name); 2528c2ecf20Sopenharmony_ci return PTR_ERR(rdev); 2538c2ecf20Sopenharmony_ci } 2548c2ecf20Sopenharmony_ci 2558c2ecf20Sopenharmony_ci /* Store default strobe info */ 2568c2ecf20Sopenharmony_ci ret = tps65217_reg_read(tps, regulators[i].bypass_reg, &val); 2578c2ecf20Sopenharmony_ci if (ret) 2588c2ecf20Sopenharmony_ci return ret; 2598c2ecf20Sopenharmony_ci 2608c2ecf20Sopenharmony_ci tps->strobes[i] = val & regulators[i].bypass_mask; 2618c2ecf20Sopenharmony_ci } 2628c2ecf20Sopenharmony_ci 2638c2ecf20Sopenharmony_ci return 0; 2648c2ecf20Sopenharmony_ci} 2658c2ecf20Sopenharmony_ci 2668c2ecf20Sopenharmony_cistatic struct platform_driver tps65217_regulator_driver = { 2678c2ecf20Sopenharmony_ci .driver = { 2688c2ecf20Sopenharmony_ci .name = "tps65217-pmic", 2698c2ecf20Sopenharmony_ci }, 2708c2ecf20Sopenharmony_ci .probe = tps65217_regulator_probe, 2718c2ecf20Sopenharmony_ci}; 2728c2ecf20Sopenharmony_ci 2738c2ecf20Sopenharmony_cistatic int __init tps65217_regulator_init(void) 2748c2ecf20Sopenharmony_ci{ 2758c2ecf20Sopenharmony_ci return platform_driver_register(&tps65217_regulator_driver); 2768c2ecf20Sopenharmony_ci} 2778c2ecf20Sopenharmony_cisubsys_initcall(tps65217_regulator_init); 2788c2ecf20Sopenharmony_ci 2798c2ecf20Sopenharmony_cistatic void __exit tps65217_regulator_exit(void) 2808c2ecf20Sopenharmony_ci{ 2818c2ecf20Sopenharmony_ci platform_driver_unregister(&tps65217_regulator_driver); 2828c2ecf20Sopenharmony_ci} 2838c2ecf20Sopenharmony_cimodule_exit(tps65217_regulator_exit); 2848c2ecf20Sopenharmony_ci 2858c2ecf20Sopenharmony_ciMODULE_AUTHOR("AnilKumar Ch <anilkumar@ti.com>"); 2868c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("TPS65217 voltage regulator driver"); 2878c2ecf20Sopenharmony_ciMODULE_ALIAS("platform:tps65217-pmic"); 2888c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2"); 289