18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0+ 28c2ecf20Sopenharmony_ci// 38c2ecf20Sopenharmony_ci// max14577.c - Regulator driver for the Maxim 14577/77836 48c2ecf20Sopenharmony_ci// 58c2ecf20Sopenharmony_ci// Copyright (C) 2013,2014 Samsung Electronics 68c2ecf20Sopenharmony_ci// Krzysztof Kozlowski <krzk@kernel.org> 78c2ecf20Sopenharmony_ci 88c2ecf20Sopenharmony_ci#include <linux/module.h> 98c2ecf20Sopenharmony_ci#include <linux/platform_device.h> 108c2ecf20Sopenharmony_ci#include <linux/regulator/driver.h> 118c2ecf20Sopenharmony_ci#include <linux/mfd/max14577.h> 128c2ecf20Sopenharmony_ci#include <linux/mfd/max14577-private.h> 138c2ecf20Sopenharmony_ci#include <linux/regulator/of_regulator.h> 148c2ecf20Sopenharmony_ci 158c2ecf20Sopenharmony_cistatic int max14577_reg_is_enabled(struct regulator_dev *rdev) 168c2ecf20Sopenharmony_ci{ 178c2ecf20Sopenharmony_ci int rid = rdev_get_id(rdev); 188c2ecf20Sopenharmony_ci struct regmap *rmap = rdev->regmap; 198c2ecf20Sopenharmony_ci u8 reg_data; 208c2ecf20Sopenharmony_ci 218c2ecf20Sopenharmony_ci switch (rid) { 228c2ecf20Sopenharmony_ci case MAX14577_CHARGER: 238c2ecf20Sopenharmony_ci max14577_read_reg(rmap, MAX14577_CHG_REG_CHG_CTRL2, ®_data); 248c2ecf20Sopenharmony_ci if ((reg_data & CHGCTRL2_MBCHOSTEN_MASK) == 0) 258c2ecf20Sopenharmony_ci return 0; 268c2ecf20Sopenharmony_ci max14577_read_reg(rmap, MAX14577_CHG_REG_STATUS3, ®_data); 278c2ecf20Sopenharmony_ci if ((reg_data & STATUS3_CGMBC_MASK) == 0) 288c2ecf20Sopenharmony_ci return 0; 298c2ecf20Sopenharmony_ci /* MBCHOSTEN and CGMBC are on */ 308c2ecf20Sopenharmony_ci return 1; 318c2ecf20Sopenharmony_ci default: 328c2ecf20Sopenharmony_ci return -EINVAL; 338c2ecf20Sopenharmony_ci } 348c2ecf20Sopenharmony_ci} 358c2ecf20Sopenharmony_ci 368c2ecf20Sopenharmony_cistatic int max14577_reg_get_current_limit(struct regulator_dev *rdev) 378c2ecf20Sopenharmony_ci{ 388c2ecf20Sopenharmony_ci u8 reg_data; 398c2ecf20Sopenharmony_ci struct regmap *rmap = rdev->regmap; 408c2ecf20Sopenharmony_ci struct max14577 *max14577 = rdev_get_drvdata(rdev); 418c2ecf20Sopenharmony_ci const struct maxim_charger_current *limits = 428c2ecf20Sopenharmony_ci &maxim_charger_currents[max14577->dev_type]; 438c2ecf20Sopenharmony_ci 448c2ecf20Sopenharmony_ci if (rdev_get_id(rdev) != MAX14577_CHARGER) 458c2ecf20Sopenharmony_ci return -EINVAL; 468c2ecf20Sopenharmony_ci 478c2ecf20Sopenharmony_ci max14577_read_reg(rmap, MAX14577_CHG_REG_CHG_CTRL4, ®_data); 488c2ecf20Sopenharmony_ci 498c2ecf20Sopenharmony_ci if ((reg_data & CHGCTRL4_MBCICHWRCL_MASK) == 0) 508c2ecf20Sopenharmony_ci return limits->min; 518c2ecf20Sopenharmony_ci 528c2ecf20Sopenharmony_ci reg_data = ((reg_data & CHGCTRL4_MBCICHWRCH_MASK) >> 538c2ecf20Sopenharmony_ci CHGCTRL4_MBCICHWRCH_SHIFT); 548c2ecf20Sopenharmony_ci return limits->high_start + reg_data * limits->high_step; 558c2ecf20Sopenharmony_ci} 568c2ecf20Sopenharmony_ci 578c2ecf20Sopenharmony_cistatic int max14577_reg_set_current_limit(struct regulator_dev *rdev, 588c2ecf20Sopenharmony_ci int min_uA, int max_uA) 598c2ecf20Sopenharmony_ci{ 608c2ecf20Sopenharmony_ci u8 reg_data; 618c2ecf20Sopenharmony_ci int ret; 628c2ecf20Sopenharmony_ci struct max14577 *max14577 = rdev_get_drvdata(rdev); 638c2ecf20Sopenharmony_ci const struct maxim_charger_current *limits = 648c2ecf20Sopenharmony_ci &maxim_charger_currents[max14577->dev_type]; 658c2ecf20Sopenharmony_ci 668c2ecf20Sopenharmony_ci if (rdev_get_id(rdev) != MAX14577_CHARGER) 678c2ecf20Sopenharmony_ci return -EINVAL; 688c2ecf20Sopenharmony_ci 698c2ecf20Sopenharmony_ci ret = maxim_charger_calc_reg_current(limits, min_uA, max_uA, ®_data); 708c2ecf20Sopenharmony_ci if (ret) 718c2ecf20Sopenharmony_ci return ret; 728c2ecf20Sopenharmony_ci 738c2ecf20Sopenharmony_ci return max14577_update_reg(rdev->regmap, MAX14577_CHG_REG_CHG_CTRL4, 748c2ecf20Sopenharmony_ci CHGCTRL4_MBCICHWRCL_MASK | CHGCTRL4_MBCICHWRCH_MASK, 758c2ecf20Sopenharmony_ci reg_data); 768c2ecf20Sopenharmony_ci} 778c2ecf20Sopenharmony_ci 788c2ecf20Sopenharmony_cistatic const struct regulator_ops max14577_safeout_ops = { 798c2ecf20Sopenharmony_ci .is_enabled = regulator_is_enabled_regmap, 808c2ecf20Sopenharmony_ci .enable = regulator_enable_regmap, 818c2ecf20Sopenharmony_ci .disable = regulator_disable_regmap, 828c2ecf20Sopenharmony_ci .list_voltage = regulator_list_voltage_linear, 838c2ecf20Sopenharmony_ci}; 848c2ecf20Sopenharmony_ci 858c2ecf20Sopenharmony_cistatic const struct regulator_ops max14577_charger_ops = { 868c2ecf20Sopenharmony_ci .is_enabled = max14577_reg_is_enabled, 878c2ecf20Sopenharmony_ci .enable = regulator_enable_regmap, 888c2ecf20Sopenharmony_ci .disable = regulator_disable_regmap, 898c2ecf20Sopenharmony_ci .get_current_limit = max14577_reg_get_current_limit, 908c2ecf20Sopenharmony_ci .set_current_limit = max14577_reg_set_current_limit, 918c2ecf20Sopenharmony_ci}; 928c2ecf20Sopenharmony_ci 938c2ecf20Sopenharmony_ci#define MAX14577_SAFEOUT_REG { \ 948c2ecf20Sopenharmony_ci .name = "SAFEOUT", \ 958c2ecf20Sopenharmony_ci .of_match = of_match_ptr("SAFEOUT"), \ 968c2ecf20Sopenharmony_ci .regulators_node = of_match_ptr("regulators"), \ 978c2ecf20Sopenharmony_ci .id = MAX14577_SAFEOUT, \ 988c2ecf20Sopenharmony_ci .ops = &max14577_safeout_ops, \ 998c2ecf20Sopenharmony_ci .type = REGULATOR_VOLTAGE, \ 1008c2ecf20Sopenharmony_ci .owner = THIS_MODULE, \ 1018c2ecf20Sopenharmony_ci .n_voltages = 1, \ 1028c2ecf20Sopenharmony_ci .min_uV = MAX14577_REGULATOR_SAFEOUT_VOLTAGE, \ 1038c2ecf20Sopenharmony_ci .enable_reg = MAX14577_REG_CONTROL2, \ 1048c2ecf20Sopenharmony_ci .enable_mask = CTRL2_SFOUTORD_MASK, \ 1058c2ecf20Sopenharmony_ci} 1068c2ecf20Sopenharmony_ci#define MAX14577_CHARGER_REG { \ 1078c2ecf20Sopenharmony_ci .name = "CHARGER", \ 1088c2ecf20Sopenharmony_ci .of_match = of_match_ptr("CHARGER"), \ 1098c2ecf20Sopenharmony_ci .regulators_node = of_match_ptr("regulators"), \ 1108c2ecf20Sopenharmony_ci .id = MAX14577_CHARGER, \ 1118c2ecf20Sopenharmony_ci .ops = &max14577_charger_ops, \ 1128c2ecf20Sopenharmony_ci .type = REGULATOR_CURRENT, \ 1138c2ecf20Sopenharmony_ci .owner = THIS_MODULE, \ 1148c2ecf20Sopenharmony_ci .enable_reg = MAX14577_CHG_REG_CHG_CTRL2, \ 1158c2ecf20Sopenharmony_ci .enable_mask = CHGCTRL2_MBCHOSTEN_MASK, \ 1168c2ecf20Sopenharmony_ci} 1178c2ecf20Sopenharmony_ci 1188c2ecf20Sopenharmony_cistatic const struct regulator_desc max14577_supported_regulators[] = { 1198c2ecf20Sopenharmony_ci [MAX14577_SAFEOUT] = MAX14577_SAFEOUT_REG, 1208c2ecf20Sopenharmony_ci [MAX14577_CHARGER] = MAX14577_CHARGER_REG, 1218c2ecf20Sopenharmony_ci}; 1228c2ecf20Sopenharmony_ci 1238c2ecf20Sopenharmony_cistatic const struct regulator_ops max77836_ldo_ops = { 1248c2ecf20Sopenharmony_ci .is_enabled = regulator_is_enabled_regmap, 1258c2ecf20Sopenharmony_ci .enable = regulator_enable_regmap, 1268c2ecf20Sopenharmony_ci .disable = regulator_disable_regmap, 1278c2ecf20Sopenharmony_ci .list_voltage = regulator_list_voltage_linear, 1288c2ecf20Sopenharmony_ci .map_voltage = regulator_map_voltage_linear, 1298c2ecf20Sopenharmony_ci .get_voltage_sel = regulator_get_voltage_sel_regmap, 1308c2ecf20Sopenharmony_ci .set_voltage_sel = regulator_set_voltage_sel_regmap, 1318c2ecf20Sopenharmony_ci /* TODO: add .set_suspend_mode */ 1328c2ecf20Sopenharmony_ci}; 1338c2ecf20Sopenharmony_ci 1348c2ecf20Sopenharmony_ci#define MAX77836_LDO_REG(num) { \ 1358c2ecf20Sopenharmony_ci .name = "LDO" # num, \ 1368c2ecf20Sopenharmony_ci .of_match = of_match_ptr("LDO" # num), \ 1378c2ecf20Sopenharmony_ci .regulators_node = of_match_ptr("regulators"), \ 1388c2ecf20Sopenharmony_ci .id = MAX77836_LDO ## num, \ 1398c2ecf20Sopenharmony_ci .ops = &max77836_ldo_ops, \ 1408c2ecf20Sopenharmony_ci .type = REGULATOR_VOLTAGE, \ 1418c2ecf20Sopenharmony_ci .owner = THIS_MODULE, \ 1428c2ecf20Sopenharmony_ci .n_voltages = MAX77836_REGULATOR_LDO_VOLTAGE_STEPS_NUM, \ 1438c2ecf20Sopenharmony_ci .min_uV = MAX77836_REGULATOR_LDO_VOLTAGE_MIN, \ 1448c2ecf20Sopenharmony_ci .uV_step = MAX77836_REGULATOR_LDO_VOLTAGE_STEP, \ 1458c2ecf20Sopenharmony_ci .enable_reg = MAX77836_LDO_REG_CNFG1_LDO ## num, \ 1468c2ecf20Sopenharmony_ci .enable_mask = MAX77836_CNFG1_LDO_PWRMD_MASK, \ 1478c2ecf20Sopenharmony_ci .vsel_reg = MAX77836_LDO_REG_CNFG1_LDO ## num, \ 1488c2ecf20Sopenharmony_ci .vsel_mask = MAX77836_CNFG1_LDO_TV_MASK, \ 1498c2ecf20Sopenharmony_ci} 1508c2ecf20Sopenharmony_ci 1518c2ecf20Sopenharmony_cistatic const struct regulator_desc max77836_supported_regulators[] = { 1528c2ecf20Sopenharmony_ci [MAX14577_SAFEOUT] = MAX14577_SAFEOUT_REG, 1538c2ecf20Sopenharmony_ci [MAX14577_CHARGER] = MAX14577_CHARGER_REG, 1548c2ecf20Sopenharmony_ci [MAX77836_LDO1] = MAX77836_LDO_REG(1), 1558c2ecf20Sopenharmony_ci [MAX77836_LDO2] = MAX77836_LDO_REG(2), 1568c2ecf20Sopenharmony_ci}; 1578c2ecf20Sopenharmony_ci 1588c2ecf20Sopenharmony_ci/* 1598c2ecf20Sopenharmony_ci * Registers for regulators of max77836 use different I2C slave addresses so 1608c2ecf20Sopenharmony_ci * different regmaps must be used for them. 1618c2ecf20Sopenharmony_ci * 1628c2ecf20Sopenharmony_ci * Returns proper regmap for accessing regulator passed by id. 1638c2ecf20Sopenharmony_ci */ 1648c2ecf20Sopenharmony_cistatic struct regmap *max14577_get_regmap(struct max14577 *max14577, 1658c2ecf20Sopenharmony_ci int reg_id) 1668c2ecf20Sopenharmony_ci{ 1678c2ecf20Sopenharmony_ci switch (max14577->dev_type) { 1688c2ecf20Sopenharmony_ci case MAXIM_DEVICE_TYPE_MAX77836: 1698c2ecf20Sopenharmony_ci switch (reg_id) { 1708c2ecf20Sopenharmony_ci case MAX77836_SAFEOUT ... MAX77836_CHARGER: 1718c2ecf20Sopenharmony_ci return max14577->regmap; 1728c2ecf20Sopenharmony_ci default: 1738c2ecf20Sopenharmony_ci /* MAX77836_LDO1 ... MAX77836_LDO2 */ 1748c2ecf20Sopenharmony_ci return max14577->regmap_pmic; 1758c2ecf20Sopenharmony_ci } 1768c2ecf20Sopenharmony_ci 1778c2ecf20Sopenharmony_ci case MAXIM_DEVICE_TYPE_MAX14577: 1788c2ecf20Sopenharmony_ci default: 1798c2ecf20Sopenharmony_ci return max14577->regmap; 1808c2ecf20Sopenharmony_ci } 1818c2ecf20Sopenharmony_ci} 1828c2ecf20Sopenharmony_ci 1838c2ecf20Sopenharmony_cistatic int max14577_regulator_probe(struct platform_device *pdev) 1848c2ecf20Sopenharmony_ci{ 1858c2ecf20Sopenharmony_ci struct max14577 *max14577 = dev_get_drvdata(pdev->dev.parent); 1868c2ecf20Sopenharmony_ci struct max14577_platform_data *pdata = dev_get_platdata(max14577->dev); 1878c2ecf20Sopenharmony_ci int i, ret = 0; 1888c2ecf20Sopenharmony_ci struct regulator_config config = {}; 1898c2ecf20Sopenharmony_ci const struct regulator_desc *supported_regulators; 1908c2ecf20Sopenharmony_ci unsigned int supported_regulators_size; 1918c2ecf20Sopenharmony_ci enum maxim_device_type dev_type = max14577->dev_type; 1928c2ecf20Sopenharmony_ci 1938c2ecf20Sopenharmony_ci switch (dev_type) { 1948c2ecf20Sopenharmony_ci case MAXIM_DEVICE_TYPE_MAX77836: 1958c2ecf20Sopenharmony_ci supported_regulators = max77836_supported_regulators; 1968c2ecf20Sopenharmony_ci supported_regulators_size = ARRAY_SIZE(max77836_supported_regulators); 1978c2ecf20Sopenharmony_ci break; 1988c2ecf20Sopenharmony_ci case MAXIM_DEVICE_TYPE_MAX14577: 1998c2ecf20Sopenharmony_ci default: 2008c2ecf20Sopenharmony_ci supported_regulators = max14577_supported_regulators; 2018c2ecf20Sopenharmony_ci supported_regulators_size = ARRAY_SIZE(max14577_supported_regulators); 2028c2ecf20Sopenharmony_ci } 2038c2ecf20Sopenharmony_ci 2048c2ecf20Sopenharmony_ci config.dev = max14577->dev; 2058c2ecf20Sopenharmony_ci config.driver_data = max14577; 2068c2ecf20Sopenharmony_ci 2078c2ecf20Sopenharmony_ci for (i = 0; i < supported_regulators_size; i++) { 2088c2ecf20Sopenharmony_ci struct regulator_dev *regulator; 2098c2ecf20Sopenharmony_ci /* 2108c2ecf20Sopenharmony_ci * Index of supported_regulators[] is also the id and must 2118c2ecf20Sopenharmony_ci * match index of pdata->regulators[]. 2128c2ecf20Sopenharmony_ci */ 2138c2ecf20Sopenharmony_ci if (pdata && pdata->regulators) { 2148c2ecf20Sopenharmony_ci config.init_data = pdata->regulators[i].initdata; 2158c2ecf20Sopenharmony_ci config.of_node = pdata->regulators[i].of_node; 2168c2ecf20Sopenharmony_ci } 2178c2ecf20Sopenharmony_ci config.regmap = max14577_get_regmap(max14577, 2188c2ecf20Sopenharmony_ci supported_regulators[i].id); 2198c2ecf20Sopenharmony_ci 2208c2ecf20Sopenharmony_ci regulator = devm_regulator_register(&pdev->dev, 2218c2ecf20Sopenharmony_ci &supported_regulators[i], &config); 2228c2ecf20Sopenharmony_ci if (IS_ERR(regulator)) { 2238c2ecf20Sopenharmony_ci ret = PTR_ERR(regulator); 2248c2ecf20Sopenharmony_ci dev_err(&pdev->dev, 2258c2ecf20Sopenharmony_ci "Regulator init failed for %d/%s with error: %d\n", 2268c2ecf20Sopenharmony_ci i, supported_regulators[i].name, ret); 2278c2ecf20Sopenharmony_ci return ret; 2288c2ecf20Sopenharmony_ci } 2298c2ecf20Sopenharmony_ci } 2308c2ecf20Sopenharmony_ci 2318c2ecf20Sopenharmony_ci return ret; 2328c2ecf20Sopenharmony_ci} 2338c2ecf20Sopenharmony_ci 2348c2ecf20Sopenharmony_cistatic const struct platform_device_id max14577_regulator_id[] = { 2358c2ecf20Sopenharmony_ci { "max14577-regulator", MAXIM_DEVICE_TYPE_MAX14577, }, 2368c2ecf20Sopenharmony_ci { "max77836-regulator", MAXIM_DEVICE_TYPE_MAX77836, }, 2378c2ecf20Sopenharmony_ci { } 2388c2ecf20Sopenharmony_ci}; 2398c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(platform, max14577_regulator_id); 2408c2ecf20Sopenharmony_ci 2418c2ecf20Sopenharmony_cistatic struct platform_driver max14577_regulator_driver = { 2428c2ecf20Sopenharmony_ci .driver = { 2438c2ecf20Sopenharmony_ci .name = "max14577-regulator", 2448c2ecf20Sopenharmony_ci }, 2458c2ecf20Sopenharmony_ci .probe = max14577_regulator_probe, 2468c2ecf20Sopenharmony_ci .id_table = max14577_regulator_id, 2478c2ecf20Sopenharmony_ci}; 2488c2ecf20Sopenharmony_ci 2498c2ecf20Sopenharmony_cistatic int __init max14577_regulator_init(void) 2508c2ecf20Sopenharmony_ci{ 2518c2ecf20Sopenharmony_ci BUILD_BUG_ON(ARRAY_SIZE(max14577_supported_regulators) != MAX14577_REGULATOR_NUM); 2528c2ecf20Sopenharmony_ci BUILD_BUG_ON(ARRAY_SIZE(max77836_supported_regulators) != MAX77836_REGULATOR_NUM); 2538c2ecf20Sopenharmony_ci 2548c2ecf20Sopenharmony_ci BUILD_BUG_ON(MAX77836_REGULATOR_LDO_VOLTAGE_MIN + 2558c2ecf20Sopenharmony_ci (MAX77836_REGULATOR_LDO_VOLTAGE_STEP * 2568c2ecf20Sopenharmony_ci (MAX77836_REGULATOR_LDO_VOLTAGE_STEPS_NUM - 1)) != 2578c2ecf20Sopenharmony_ci MAX77836_REGULATOR_LDO_VOLTAGE_MAX); 2588c2ecf20Sopenharmony_ci 2598c2ecf20Sopenharmony_ci return platform_driver_register(&max14577_regulator_driver); 2608c2ecf20Sopenharmony_ci} 2618c2ecf20Sopenharmony_cisubsys_initcall(max14577_regulator_init); 2628c2ecf20Sopenharmony_ci 2638c2ecf20Sopenharmony_cistatic void __exit max14577_regulator_exit(void) 2648c2ecf20Sopenharmony_ci{ 2658c2ecf20Sopenharmony_ci platform_driver_unregister(&max14577_regulator_driver); 2668c2ecf20Sopenharmony_ci} 2678c2ecf20Sopenharmony_cimodule_exit(max14577_regulator_exit); 2688c2ecf20Sopenharmony_ci 2698c2ecf20Sopenharmony_ciMODULE_AUTHOR("Krzysztof Kozlowski <krzk@kernel.org>"); 2708c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Maxim 14577/77836 regulator driver"); 2718c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL"); 272