18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0+ 28c2ecf20Sopenharmony_ci// 38c2ecf20Sopenharmony_ci// mp5416.c - regulator driver for mps mp5416 48c2ecf20Sopenharmony_ci// 58c2ecf20Sopenharmony_ci// Copyright 2020 Monolithic Power Systems, Inc 68c2ecf20Sopenharmony_ci// 78c2ecf20Sopenharmony_ci// Author: Saravanan Sekar <sravanhome@gmail.com> 88c2ecf20Sopenharmony_ci 98c2ecf20Sopenharmony_ci#include <linux/kernel.h> 108c2ecf20Sopenharmony_ci#include <linux/module.h> 118c2ecf20Sopenharmony_ci#include <linux/init.h> 128c2ecf20Sopenharmony_ci#include <linux/err.h> 138c2ecf20Sopenharmony_ci#include <linux/platform_device.h> 148c2ecf20Sopenharmony_ci#include <linux/regmap.h> 158c2ecf20Sopenharmony_ci#include <linux/regulator/driver.h> 168c2ecf20Sopenharmony_ci#include <linux/i2c.h> 178c2ecf20Sopenharmony_ci 188c2ecf20Sopenharmony_ci#define MP5416_REG_CTL0 0x00 198c2ecf20Sopenharmony_ci#define MP5416_REG_CTL1 0x01 208c2ecf20Sopenharmony_ci#define MP5416_REG_CTL2 0x02 218c2ecf20Sopenharmony_ci#define MP5416_REG_ILIM 0x03 228c2ecf20Sopenharmony_ci#define MP5416_REG_BUCK1 0x04 238c2ecf20Sopenharmony_ci#define MP5416_REG_BUCK2 0x05 248c2ecf20Sopenharmony_ci#define MP5416_REG_BUCK3 0x06 258c2ecf20Sopenharmony_ci#define MP5416_REG_BUCK4 0x07 268c2ecf20Sopenharmony_ci#define MP5416_REG_LDO1 0x08 278c2ecf20Sopenharmony_ci#define MP5416_REG_LDO2 0x09 288c2ecf20Sopenharmony_ci#define MP5416_REG_LDO3 0x0a 298c2ecf20Sopenharmony_ci#define MP5416_REG_LDO4 0x0b 308c2ecf20Sopenharmony_ci 318c2ecf20Sopenharmony_ci#define MP5416_REGULATOR_EN BIT(7) 328c2ecf20Sopenharmony_ci#define MP5416_MASK_VSET 0x7f 338c2ecf20Sopenharmony_ci#define MP5416_MASK_BUCK1_ILIM 0xc0 348c2ecf20Sopenharmony_ci#define MP5416_MASK_BUCK2_ILIM 0x0c 358c2ecf20Sopenharmony_ci#define MP5416_MASK_BUCK3_ILIM 0x30 368c2ecf20Sopenharmony_ci#define MP5416_MASK_BUCK4_ILIM 0x03 378c2ecf20Sopenharmony_ci#define MP5416_MASK_DVS_SLEWRATE 0xc0 388c2ecf20Sopenharmony_ci 398c2ecf20Sopenharmony_ci/* values in uV */ 408c2ecf20Sopenharmony_ci#define MP5416_VOLT1_MIN 600000 418c2ecf20Sopenharmony_ci#define MP5416_VOLT1_MAX 2187500 428c2ecf20Sopenharmony_ci#define MP5416_VOLT1_STEP 12500 438c2ecf20Sopenharmony_ci#define MP5416_VOLT2_MIN 800000 448c2ecf20Sopenharmony_ci#define MP5416_VOLT2_MAX 3975000 458c2ecf20Sopenharmony_ci#define MP5416_VOLT2_STEP 25000 468c2ecf20Sopenharmony_ci 478c2ecf20Sopenharmony_ci#define MP5416_VOLT1_RANGE \ 488c2ecf20Sopenharmony_ci ((MP5416_VOLT1_MAX - MP5416_VOLT1_MIN)/MP5416_VOLT1_STEP + 1) 498c2ecf20Sopenharmony_ci#define MP5416_VOLT2_RANGE \ 508c2ecf20Sopenharmony_ci ((MP5416_VOLT2_MAX - MP5416_VOLT2_MIN)/MP5416_VOLT2_STEP + 1) 518c2ecf20Sopenharmony_ci 528c2ecf20Sopenharmony_ci#define MP5416BUCK(_name, _id, _ilim, _dreg, _dval, _vsel) \ 538c2ecf20Sopenharmony_ci [MP5416_BUCK ## _id] = { \ 548c2ecf20Sopenharmony_ci .id = MP5416_BUCK ## _id, \ 558c2ecf20Sopenharmony_ci .name = _name, \ 568c2ecf20Sopenharmony_ci .of_match = _name, \ 578c2ecf20Sopenharmony_ci .regulators_node = "regulators", \ 588c2ecf20Sopenharmony_ci .ops = &mp5416_buck_ops, \ 598c2ecf20Sopenharmony_ci .min_uV = MP5416_VOLT ##_vsel## _MIN, \ 608c2ecf20Sopenharmony_ci .uV_step = MP5416_VOLT ##_vsel## _STEP, \ 618c2ecf20Sopenharmony_ci .n_voltages = MP5416_VOLT ##_vsel## _RANGE, \ 628c2ecf20Sopenharmony_ci .curr_table = _ilim, \ 638c2ecf20Sopenharmony_ci .n_current_limits = ARRAY_SIZE(_ilim), \ 648c2ecf20Sopenharmony_ci .csel_reg = MP5416_REG_ILIM, \ 658c2ecf20Sopenharmony_ci .csel_mask = MP5416_MASK_BUCK ## _id ##_ILIM, \ 668c2ecf20Sopenharmony_ci .vsel_reg = MP5416_REG_BUCK ## _id, \ 678c2ecf20Sopenharmony_ci .vsel_mask = MP5416_MASK_VSET, \ 688c2ecf20Sopenharmony_ci .enable_reg = MP5416_REG_BUCK ## _id, \ 698c2ecf20Sopenharmony_ci .enable_mask = MP5416_REGULATOR_EN, \ 708c2ecf20Sopenharmony_ci .active_discharge_on = _dval, \ 718c2ecf20Sopenharmony_ci .active_discharge_reg = _dreg, \ 728c2ecf20Sopenharmony_ci .active_discharge_mask = _dval, \ 738c2ecf20Sopenharmony_ci .owner = THIS_MODULE, \ 748c2ecf20Sopenharmony_ci } 758c2ecf20Sopenharmony_ci 768c2ecf20Sopenharmony_ci#define MP5416LDO(_name, _id, _dval) \ 778c2ecf20Sopenharmony_ci [MP5416_LDO ## _id] = { \ 788c2ecf20Sopenharmony_ci .id = MP5416_LDO ## _id, \ 798c2ecf20Sopenharmony_ci .name = _name, \ 808c2ecf20Sopenharmony_ci .of_match = _name, \ 818c2ecf20Sopenharmony_ci .regulators_node = "regulators", \ 828c2ecf20Sopenharmony_ci .ops = &mp5416_ldo_ops, \ 838c2ecf20Sopenharmony_ci .min_uV = MP5416_VOLT2_MIN, \ 848c2ecf20Sopenharmony_ci .uV_step = MP5416_VOLT2_STEP, \ 858c2ecf20Sopenharmony_ci .n_voltages = MP5416_VOLT2_RANGE, \ 868c2ecf20Sopenharmony_ci .vsel_reg = MP5416_REG_LDO ##_id, \ 878c2ecf20Sopenharmony_ci .vsel_mask = MP5416_MASK_VSET, \ 888c2ecf20Sopenharmony_ci .enable_reg = MP5416_REG_LDO ##_id, \ 898c2ecf20Sopenharmony_ci .enable_mask = MP5416_REGULATOR_EN, \ 908c2ecf20Sopenharmony_ci .active_discharge_on = _dval, \ 918c2ecf20Sopenharmony_ci .active_discharge_reg = MP5416_REG_CTL2, \ 928c2ecf20Sopenharmony_ci .active_discharge_mask = _dval, \ 938c2ecf20Sopenharmony_ci .owner = THIS_MODULE, \ 948c2ecf20Sopenharmony_ci } 958c2ecf20Sopenharmony_ci 968c2ecf20Sopenharmony_cienum mp5416_regulators { 978c2ecf20Sopenharmony_ci MP5416_BUCK1, 988c2ecf20Sopenharmony_ci MP5416_BUCK2, 998c2ecf20Sopenharmony_ci MP5416_BUCK3, 1008c2ecf20Sopenharmony_ci MP5416_BUCK4, 1018c2ecf20Sopenharmony_ci MP5416_LDO1, 1028c2ecf20Sopenharmony_ci MP5416_LDO2, 1038c2ecf20Sopenharmony_ci MP5416_LDO3, 1048c2ecf20Sopenharmony_ci MP5416_LDO4, 1058c2ecf20Sopenharmony_ci MP5416_MAX_REGULATORS, 1068c2ecf20Sopenharmony_ci}; 1078c2ecf20Sopenharmony_ci 1088c2ecf20Sopenharmony_cistatic const struct regmap_config mp5416_regmap_config = { 1098c2ecf20Sopenharmony_ci .reg_bits = 8, 1108c2ecf20Sopenharmony_ci .val_bits = 8, 1118c2ecf20Sopenharmony_ci .max_register = 0x0d, 1128c2ecf20Sopenharmony_ci}; 1138c2ecf20Sopenharmony_ci 1148c2ecf20Sopenharmony_ci/* Current limits array (in uA) 1158c2ecf20Sopenharmony_ci * ILIM1 & ILIM3 1168c2ecf20Sopenharmony_ci */ 1178c2ecf20Sopenharmony_cistatic const unsigned int mp5416_I_limits1[] = { 1188c2ecf20Sopenharmony_ci 3800000, 4600000, 5600000, 6800000 1198c2ecf20Sopenharmony_ci}; 1208c2ecf20Sopenharmony_ci 1218c2ecf20Sopenharmony_ci/* ILIM2 & ILIM4 */ 1228c2ecf20Sopenharmony_cistatic const unsigned int mp5416_I_limits2[] = { 1238c2ecf20Sopenharmony_ci 2200000, 3200000, 4200000, 5200000 1248c2ecf20Sopenharmony_ci}; 1258c2ecf20Sopenharmony_ci 1268c2ecf20Sopenharmony_cistatic int mp5416_set_ramp_delay(struct regulator_dev *rdev, int ramp_delay); 1278c2ecf20Sopenharmony_ci 1288c2ecf20Sopenharmony_cistatic const struct regulator_ops mp5416_ldo_ops = { 1298c2ecf20Sopenharmony_ci .enable = regulator_enable_regmap, 1308c2ecf20Sopenharmony_ci .disable = regulator_disable_regmap, 1318c2ecf20Sopenharmony_ci .is_enabled = regulator_is_enabled_regmap, 1328c2ecf20Sopenharmony_ci .list_voltage = regulator_list_voltage_linear, 1338c2ecf20Sopenharmony_ci .map_voltage = regulator_map_voltage_linear, 1348c2ecf20Sopenharmony_ci .get_voltage_sel = regulator_get_voltage_sel_regmap, 1358c2ecf20Sopenharmony_ci .set_voltage_sel = regulator_set_voltage_sel_regmap, 1368c2ecf20Sopenharmony_ci .set_active_discharge = regulator_set_active_discharge_regmap, 1378c2ecf20Sopenharmony_ci}; 1388c2ecf20Sopenharmony_ci 1398c2ecf20Sopenharmony_cistatic const struct regulator_ops mp5416_buck_ops = { 1408c2ecf20Sopenharmony_ci .enable = regulator_enable_regmap, 1418c2ecf20Sopenharmony_ci .disable = regulator_disable_regmap, 1428c2ecf20Sopenharmony_ci .is_enabled = regulator_is_enabled_regmap, 1438c2ecf20Sopenharmony_ci .list_voltage = regulator_list_voltage_linear, 1448c2ecf20Sopenharmony_ci .map_voltage = regulator_map_voltage_linear, 1458c2ecf20Sopenharmony_ci .get_voltage_sel = regulator_get_voltage_sel_regmap, 1468c2ecf20Sopenharmony_ci .set_voltage_sel = regulator_set_voltage_sel_regmap, 1478c2ecf20Sopenharmony_ci .set_active_discharge = regulator_set_active_discharge_regmap, 1488c2ecf20Sopenharmony_ci .get_current_limit = regulator_get_current_limit_regmap, 1498c2ecf20Sopenharmony_ci .set_current_limit = regulator_set_current_limit_regmap, 1508c2ecf20Sopenharmony_ci .set_ramp_delay = mp5416_set_ramp_delay, 1518c2ecf20Sopenharmony_ci}; 1528c2ecf20Sopenharmony_ci 1538c2ecf20Sopenharmony_cistatic struct regulator_desc mp5416_regulators_desc[MP5416_MAX_REGULATORS] = { 1548c2ecf20Sopenharmony_ci MP5416BUCK("buck1", 1, mp5416_I_limits1, MP5416_REG_CTL1, BIT(0), 1), 1558c2ecf20Sopenharmony_ci MP5416BUCK("buck2", 2, mp5416_I_limits2, MP5416_REG_CTL1, BIT(1), 2), 1568c2ecf20Sopenharmony_ci MP5416BUCK("buck3", 3, mp5416_I_limits1, MP5416_REG_CTL1, BIT(2), 1), 1578c2ecf20Sopenharmony_ci MP5416BUCK("buck4", 4, mp5416_I_limits2, MP5416_REG_CTL2, BIT(5), 2), 1588c2ecf20Sopenharmony_ci MP5416LDO("ldo1", 1, BIT(4)), 1598c2ecf20Sopenharmony_ci MP5416LDO("ldo2", 2, BIT(3)), 1608c2ecf20Sopenharmony_ci MP5416LDO("ldo3", 3, BIT(2)), 1618c2ecf20Sopenharmony_ci MP5416LDO("ldo4", 4, BIT(1)), 1628c2ecf20Sopenharmony_ci}; 1638c2ecf20Sopenharmony_ci 1648c2ecf20Sopenharmony_ci/* 1658c2ecf20Sopenharmony_ci * DVS ramp rate BUCK1 to BUCK4 1668c2ecf20Sopenharmony_ci * 00: 32mV/us 1678c2ecf20Sopenharmony_ci * 01: 16mV/us 1688c2ecf20Sopenharmony_ci * 10: 8mV/us 1698c2ecf20Sopenharmony_ci * 11: 4mV/us 1708c2ecf20Sopenharmony_ci */ 1718c2ecf20Sopenharmony_cistatic int mp5416_set_ramp_delay(struct regulator_dev *rdev, int ramp_delay) 1728c2ecf20Sopenharmony_ci{ 1738c2ecf20Sopenharmony_ci unsigned int ramp_val; 1748c2ecf20Sopenharmony_ci 1758c2ecf20Sopenharmony_ci if (ramp_delay > 32000 || ramp_delay < 0) 1768c2ecf20Sopenharmony_ci return -EINVAL; 1778c2ecf20Sopenharmony_ci 1788c2ecf20Sopenharmony_ci if (ramp_delay <= 4000) 1798c2ecf20Sopenharmony_ci ramp_val = 3; 1808c2ecf20Sopenharmony_ci else if (ramp_delay <= 8000) 1818c2ecf20Sopenharmony_ci ramp_val = 2; 1828c2ecf20Sopenharmony_ci else if (ramp_delay <= 16000) 1838c2ecf20Sopenharmony_ci ramp_val = 1; 1848c2ecf20Sopenharmony_ci else 1858c2ecf20Sopenharmony_ci ramp_val = 0; 1868c2ecf20Sopenharmony_ci 1878c2ecf20Sopenharmony_ci return regmap_update_bits(rdev->regmap, MP5416_REG_CTL2, 1888c2ecf20Sopenharmony_ci MP5416_MASK_DVS_SLEWRATE, ramp_val << 6); 1898c2ecf20Sopenharmony_ci} 1908c2ecf20Sopenharmony_ci 1918c2ecf20Sopenharmony_cistatic int mp5416_i2c_probe(struct i2c_client *client) 1928c2ecf20Sopenharmony_ci{ 1938c2ecf20Sopenharmony_ci struct device *dev = &client->dev; 1948c2ecf20Sopenharmony_ci struct regulator_config config = { NULL, }; 1958c2ecf20Sopenharmony_ci struct regulator_dev *rdev; 1968c2ecf20Sopenharmony_ci struct regmap *regmap; 1978c2ecf20Sopenharmony_ci int i; 1988c2ecf20Sopenharmony_ci 1998c2ecf20Sopenharmony_ci regmap = devm_regmap_init_i2c(client, &mp5416_regmap_config); 2008c2ecf20Sopenharmony_ci if (IS_ERR(regmap)) { 2018c2ecf20Sopenharmony_ci dev_err(dev, "Failed to allocate regmap!\n"); 2028c2ecf20Sopenharmony_ci return PTR_ERR(regmap); 2038c2ecf20Sopenharmony_ci } 2048c2ecf20Sopenharmony_ci 2058c2ecf20Sopenharmony_ci config.dev = dev; 2068c2ecf20Sopenharmony_ci config.regmap = regmap; 2078c2ecf20Sopenharmony_ci 2088c2ecf20Sopenharmony_ci for (i = 0; i < MP5416_MAX_REGULATORS; i++) { 2098c2ecf20Sopenharmony_ci rdev = devm_regulator_register(dev, 2108c2ecf20Sopenharmony_ci &mp5416_regulators_desc[i], 2118c2ecf20Sopenharmony_ci &config); 2128c2ecf20Sopenharmony_ci if (IS_ERR(rdev)) { 2138c2ecf20Sopenharmony_ci dev_err(dev, "Failed to register regulator!\n"); 2148c2ecf20Sopenharmony_ci return PTR_ERR(rdev); 2158c2ecf20Sopenharmony_ci } 2168c2ecf20Sopenharmony_ci } 2178c2ecf20Sopenharmony_ci 2188c2ecf20Sopenharmony_ci return 0; 2198c2ecf20Sopenharmony_ci} 2208c2ecf20Sopenharmony_ci 2218c2ecf20Sopenharmony_cistatic const struct of_device_id mp5416_of_match[] = { 2228c2ecf20Sopenharmony_ci { .compatible = "mps,mp5416" }, 2238c2ecf20Sopenharmony_ci {}, 2248c2ecf20Sopenharmony_ci}; 2258c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, mp5416_of_match); 2268c2ecf20Sopenharmony_ci 2278c2ecf20Sopenharmony_cistatic const struct i2c_device_id mp5416_id[] = { 2288c2ecf20Sopenharmony_ci { "mp5416", }, 2298c2ecf20Sopenharmony_ci { }, 2308c2ecf20Sopenharmony_ci}; 2318c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(i2c, mp5416_id); 2328c2ecf20Sopenharmony_ci 2338c2ecf20Sopenharmony_cistatic struct i2c_driver mp5416_regulator_driver = { 2348c2ecf20Sopenharmony_ci .driver = { 2358c2ecf20Sopenharmony_ci .name = "mp5416", 2368c2ecf20Sopenharmony_ci .of_match_table = of_match_ptr(mp5416_of_match), 2378c2ecf20Sopenharmony_ci }, 2388c2ecf20Sopenharmony_ci .probe_new = mp5416_i2c_probe, 2398c2ecf20Sopenharmony_ci .id_table = mp5416_id, 2408c2ecf20Sopenharmony_ci}; 2418c2ecf20Sopenharmony_cimodule_i2c_driver(mp5416_regulator_driver); 2428c2ecf20Sopenharmony_ci 2438c2ecf20Sopenharmony_ciMODULE_AUTHOR("Saravanan Sekar <sravanhome@gmail.com>"); 2448c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("MP5416 PMIC regulator driver"); 2458c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL"); 246