18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * AS3711 PMIC regulator driver, using DCDC Step Down and LDO supplies 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright (C) 2012 Renesas Electronics Corporation 68c2ecf20Sopenharmony_ci * Author: Guennadi Liakhovetski, <g.liakhovetski@gmx.de> 78c2ecf20Sopenharmony_ci */ 88c2ecf20Sopenharmony_ci 98c2ecf20Sopenharmony_ci#include <linux/err.h> 108c2ecf20Sopenharmony_ci#include <linux/init.h> 118c2ecf20Sopenharmony_ci#include <linux/mfd/as3711.h> 128c2ecf20Sopenharmony_ci#include <linux/module.h> 138c2ecf20Sopenharmony_ci#include <linux/of.h> 148c2ecf20Sopenharmony_ci#include <linux/platform_device.h> 158c2ecf20Sopenharmony_ci#include <linux/regmap.h> 168c2ecf20Sopenharmony_ci#include <linux/regulator/driver.h> 178c2ecf20Sopenharmony_ci#include <linux/regulator/of_regulator.h> 188c2ecf20Sopenharmony_ci#include <linux/slab.h> 198c2ecf20Sopenharmony_ci 208c2ecf20Sopenharmony_ci/* 218c2ecf20Sopenharmony_ci * The regulator API supports 4 modes of operataion: FAST, NORMAL, IDLE and 228c2ecf20Sopenharmony_ci * STANDBY. We map them in the following way to AS3711 SD1-4 DCDC modes: 238c2ecf20Sopenharmony_ci * FAST: sdX_fast=1 248c2ecf20Sopenharmony_ci * NORMAL: low_noise=1 258c2ecf20Sopenharmony_ci * IDLE: low_noise=0 268c2ecf20Sopenharmony_ci */ 278c2ecf20Sopenharmony_ci 288c2ecf20Sopenharmony_cistatic int as3711_set_mode_sd(struct regulator_dev *rdev, unsigned int mode) 298c2ecf20Sopenharmony_ci{ 308c2ecf20Sopenharmony_ci unsigned int fast_bit = rdev->desc->enable_mask, 318c2ecf20Sopenharmony_ci low_noise_bit = fast_bit << 4; 328c2ecf20Sopenharmony_ci u8 val; 338c2ecf20Sopenharmony_ci 348c2ecf20Sopenharmony_ci switch (mode) { 358c2ecf20Sopenharmony_ci case REGULATOR_MODE_FAST: 368c2ecf20Sopenharmony_ci val = fast_bit | low_noise_bit; 378c2ecf20Sopenharmony_ci break; 388c2ecf20Sopenharmony_ci case REGULATOR_MODE_NORMAL: 398c2ecf20Sopenharmony_ci val = low_noise_bit; 408c2ecf20Sopenharmony_ci break; 418c2ecf20Sopenharmony_ci case REGULATOR_MODE_IDLE: 428c2ecf20Sopenharmony_ci val = 0; 438c2ecf20Sopenharmony_ci break; 448c2ecf20Sopenharmony_ci default: 458c2ecf20Sopenharmony_ci return -EINVAL; 468c2ecf20Sopenharmony_ci } 478c2ecf20Sopenharmony_ci 488c2ecf20Sopenharmony_ci return regmap_update_bits(rdev->regmap, AS3711_SD_CONTROL_1, 498c2ecf20Sopenharmony_ci low_noise_bit | fast_bit, val); 508c2ecf20Sopenharmony_ci} 518c2ecf20Sopenharmony_ci 528c2ecf20Sopenharmony_cistatic unsigned int as3711_get_mode_sd(struct regulator_dev *rdev) 538c2ecf20Sopenharmony_ci{ 548c2ecf20Sopenharmony_ci unsigned int fast_bit = rdev->desc->enable_mask, 558c2ecf20Sopenharmony_ci low_noise_bit = fast_bit << 4, mask = fast_bit | low_noise_bit; 568c2ecf20Sopenharmony_ci unsigned int val; 578c2ecf20Sopenharmony_ci int ret = regmap_read(rdev->regmap, AS3711_SD_CONTROL_1, &val); 588c2ecf20Sopenharmony_ci 598c2ecf20Sopenharmony_ci if (ret < 0) 608c2ecf20Sopenharmony_ci return ret; 618c2ecf20Sopenharmony_ci 628c2ecf20Sopenharmony_ci if ((val & mask) == mask) 638c2ecf20Sopenharmony_ci return REGULATOR_MODE_FAST; 648c2ecf20Sopenharmony_ci 658c2ecf20Sopenharmony_ci if ((val & mask) == low_noise_bit) 668c2ecf20Sopenharmony_ci return REGULATOR_MODE_NORMAL; 678c2ecf20Sopenharmony_ci 688c2ecf20Sopenharmony_ci if (!(val & mask)) 698c2ecf20Sopenharmony_ci return REGULATOR_MODE_IDLE; 708c2ecf20Sopenharmony_ci 718c2ecf20Sopenharmony_ci return -EINVAL; 728c2ecf20Sopenharmony_ci} 738c2ecf20Sopenharmony_ci 748c2ecf20Sopenharmony_cistatic const struct regulator_ops as3711_sd_ops = { 758c2ecf20Sopenharmony_ci .is_enabled = regulator_is_enabled_regmap, 768c2ecf20Sopenharmony_ci .enable = regulator_enable_regmap, 778c2ecf20Sopenharmony_ci .disable = regulator_disable_regmap, 788c2ecf20Sopenharmony_ci .get_voltage_sel = regulator_get_voltage_sel_regmap, 798c2ecf20Sopenharmony_ci .set_voltage_sel = regulator_set_voltage_sel_regmap, 808c2ecf20Sopenharmony_ci .list_voltage = regulator_list_voltage_linear_range, 818c2ecf20Sopenharmony_ci .map_voltage = regulator_map_voltage_linear_range, 828c2ecf20Sopenharmony_ci .get_mode = as3711_get_mode_sd, 838c2ecf20Sopenharmony_ci .set_mode = as3711_set_mode_sd, 848c2ecf20Sopenharmony_ci}; 858c2ecf20Sopenharmony_ci 868c2ecf20Sopenharmony_cistatic const struct regulator_ops as3711_aldo_ops = { 878c2ecf20Sopenharmony_ci .is_enabled = regulator_is_enabled_regmap, 888c2ecf20Sopenharmony_ci .enable = regulator_enable_regmap, 898c2ecf20Sopenharmony_ci .disable = regulator_disable_regmap, 908c2ecf20Sopenharmony_ci .get_voltage_sel = regulator_get_voltage_sel_regmap, 918c2ecf20Sopenharmony_ci .set_voltage_sel = regulator_set_voltage_sel_regmap, 928c2ecf20Sopenharmony_ci .list_voltage = regulator_list_voltage_linear_range, 938c2ecf20Sopenharmony_ci .map_voltage = regulator_map_voltage_linear_range, 948c2ecf20Sopenharmony_ci}; 958c2ecf20Sopenharmony_ci 968c2ecf20Sopenharmony_cistatic const struct regulator_ops as3711_dldo_ops = { 978c2ecf20Sopenharmony_ci .is_enabled = regulator_is_enabled_regmap, 988c2ecf20Sopenharmony_ci .enable = regulator_enable_regmap, 998c2ecf20Sopenharmony_ci .disable = regulator_disable_regmap, 1008c2ecf20Sopenharmony_ci .get_voltage_sel = regulator_get_voltage_sel_regmap, 1018c2ecf20Sopenharmony_ci .set_voltage_sel = regulator_set_voltage_sel_regmap, 1028c2ecf20Sopenharmony_ci .list_voltage = regulator_list_voltage_linear_range, 1038c2ecf20Sopenharmony_ci .map_voltage = regulator_map_voltage_linear_range, 1048c2ecf20Sopenharmony_ci}; 1058c2ecf20Sopenharmony_ci 1068c2ecf20Sopenharmony_cistatic const struct linear_range as3711_sd_ranges[] = { 1078c2ecf20Sopenharmony_ci REGULATOR_LINEAR_RANGE(612500, 0x1, 0x40, 12500), 1088c2ecf20Sopenharmony_ci REGULATOR_LINEAR_RANGE(1425000, 0x41, 0x70, 25000), 1098c2ecf20Sopenharmony_ci REGULATOR_LINEAR_RANGE(2650000, 0x71, 0x7f, 50000), 1108c2ecf20Sopenharmony_ci}; 1118c2ecf20Sopenharmony_ci 1128c2ecf20Sopenharmony_cistatic const struct linear_range as3711_aldo_ranges[] = { 1138c2ecf20Sopenharmony_ci REGULATOR_LINEAR_RANGE(1200000, 0, 0xf, 50000), 1148c2ecf20Sopenharmony_ci REGULATOR_LINEAR_RANGE(1800000, 0x10, 0x1f, 100000), 1158c2ecf20Sopenharmony_ci}; 1168c2ecf20Sopenharmony_ci 1178c2ecf20Sopenharmony_cistatic const struct linear_range as3711_dldo_ranges[] = { 1188c2ecf20Sopenharmony_ci REGULATOR_LINEAR_RANGE(900000, 0, 0x10, 50000), 1198c2ecf20Sopenharmony_ci REGULATOR_LINEAR_RANGE(1750000, 0x20, 0x3f, 50000), 1208c2ecf20Sopenharmony_ci}; 1218c2ecf20Sopenharmony_ci 1228c2ecf20Sopenharmony_ci#define AS3711_REG(_id, _en_reg, _en_bit, _vmask, _sfx) \ 1238c2ecf20Sopenharmony_ci [AS3711_REGULATOR_ ## _id] = { \ 1248c2ecf20Sopenharmony_ci .name = "as3711-regulator-" # _id, \ 1258c2ecf20Sopenharmony_ci .id = AS3711_REGULATOR_ ## _id, \ 1268c2ecf20Sopenharmony_ci .n_voltages = (_vmask + 1), \ 1278c2ecf20Sopenharmony_ci .ops = &as3711_ ## _sfx ## _ops, \ 1288c2ecf20Sopenharmony_ci .type = REGULATOR_VOLTAGE, \ 1298c2ecf20Sopenharmony_ci .owner = THIS_MODULE, \ 1308c2ecf20Sopenharmony_ci .vsel_reg = AS3711_ ## _id ## _VOLTAGE, \ 1318c2ecf20Sopenharmony_ci .vsel_mask = _vmask, \ 1328c2ecf20Sopenharmony_ci .enable_reg = AS3711_ ## _en_reg, \ 1338c2ecf20Sopenharmony_ci .enable_mask = BIT(_en_bit), \ 1348c2ecf20Sopenharmony_ci .linear_ranges = as3711_ ## _sfx ## _ranges, \ 1358c2ecf20Sopenharmony_ci .n_linear_ranges = ARRAY_SIZE(as3711_ ## _sfx ## _ranges), \ 1368c2ecf20Sopenharmony_ci} 1378c2ecf20Sopenharmony_ci 1388c2ecf20Sopenharmony_cistatic const struct regulator_desc as3711_reg_desc[] = { 1398c2ecf20Sopenharmony_ci AS3711_REG(SD_1, SD_CONTROL, 0, 0x7f, sd), 1408c2ecf20Sopenharmony_ci AS3711_REG(SD_2, SD_CONTROL, 1, 0x7f, sd), 1418c2ecf20Sopenharmony_ci AS3711_REG(SD_3, SD_CONTROL, 2, 0x7f, sd), 1428c2ecf20Sopenharmony_ci AS3711_REG(SD_4, SD_CONTROL, 3, 0x7f, sd), 1438c2ecf20Sopenharmony_ci AS3711_REG(LDO_1, LDO_1_VOLTAGE, 7, 0x1f, aldo), 1448c2ecf20Sopenharmony_ci AS3711_REG(LDO_2, LDO_2_VOLTAGE, 7, 0x1f, aldo), 1458c2ecf20Sopenharmony_ci AS3711_REG(LDO_3, LDO_3_VOLTAGE, 7, 0x3f, dldo), 1468c2ecf20Sopenharmony_ci AS3711_REG(LDO_4, LDO_4_VOLTAGE, 7, 0x3f, dldo), 1478c2ecf20Sopenharmony_ci AS3711_REG(LDO_5, LDO_5_VOLTAGE, 7, 0x3f, dldo), 1488c2ecf20Sopenharmony_ci AS3711_REG(LDO_6, LDO_6_VOLTAGE, 7, 0x3f, dldo), 1498c2ecf20Sopenharmony_ci AS3711_REG(LDO_7, LDO_7_VOLTAGE, 7, 0x3f, dldo), 1508c2ecf20Sopenharmony_ci AS3711_REG(LDO_8, LDO_8_VOLTAGE, 7, 0x3f, dldo), 1518c2ecf20Sopenharmony_ci /* StepUp output voltage depends on supplying regulator */ 1528c2ecf20Sopenharmony_ci}; 1538c2ecf20Sopenharmony_ci 1548c2ecf20Sopenharmony_ci#define AS3711_REGULATOR_NUM ARRAY_SIZE(as3711_reg_desc) 1558c2ecf20Sopenharmony_ci 1568c2ecf20Sopenharmony_cistatic struct of_regulator_match 1578c2ecf20Sopenharmony_cias3711_regulator_matches[AS3711_REGULATOR_NUM] = { 1588c2ecf20Sopenharmony_ci [AS3711_REGULATOR_SD_1] = { .name = "sd1" }, 1598c2ecf20Sopenharmony_ci [AS3711_REGULATOR_SD_2] = { .name = "sd2" }, 1608c2ecf20Sopenharmony_ci [AS3711_REGULATOR_SD_3] = { .name = "sd3" }, 1618c2ecf20Sopenharmony_ci [AS3711_REGULATOR_SD_4] = { .name = "sd4" }, 1628c2ecf20Sopenharmony_ci [AS3711_REGULATOR_LDO_1] = { .name = "ldo1" }, 1638c2ecf20Sopenharmony_ci [AS3711_REGULATOR_LDO_2] = { .name = "ldo2" }, 1648c2ecf20Sopenharmony_ci [AS3711_REGULATOR_LDO_3] = { .name = "ldo3" }, 1658c2ecf20Sopenharmony_ci [AS3711_REGULATOR_LDO_4] = { .name = "ldo4" }, 1668c2ecf20Sopenharmony_ci [AS3711_REGULATOR_LDO_5] = { .name = "ldo5" }, 1678c2ecf20Sopenharmony_ci [AS3711_REGULATOR_LDO_6] = { .name = "ldo6" }, 1688c2ecf20Sopenharmony_ci [AS3711_REGULATOR_LDO_7] = { .name = "ldo7" }, 1698c2ecf20Sopenharmony_ci [AS3711_REGULATOR_LDO_8] = { .name = "ldo8" }, 1708c2ecf20Sopenharmony_ci}; 1718c2ecf20Sopenharmony_ci 1728c2ecf20Sopenharmony_cistatic int as3711_regulator_parse_dt(struct device *dev, 1738c2ecf20Sopenharmony_ci struct device_node **of_node, const int count) 1748c2ecf20Sopenharmony_ci{ 1758c2ecf20Sopenharmony_ci struct as3711_regulator_pdata *pdata = dev_get_platdata(dev); 1768c2ecf20Sopenharmony_ci struct device_node *regulators = 1778c2ecf20Sopenharmony_ci of_get_child_by_name(dev->parent->of_node, "regulators"); 1788c2ecf20Sopenharmony_ci struct of_regulator_match *match; 1798c2ecf20Sopenharmony_ci int ret, i; 1808c2ecf20Sopenharmony_ci 1818c2ecf20Sopenharmony_ci if (!regulators) { 1828c2ecf20Sopenharmony_ci dev_err(dev, "regulator node not found\n"); 1838c2ecf20Sopenharmony_ci return -ENODEV; 1848c2ecf20Sopenharmony_ci } 1858c2ecf20Sopenharmony_ci 1868c2ecf20Sopenharmony_ci ret = of_regulator_match(dev->parent, regulators, 1878c2ecf20Sopenharmony_ci as3711_regulator_matches, count); 1888c2ecf20Sopenharmony_ci of_node_put(regulators); 1898c2ecf20Sopenharmony_ci if (ret < 0) { 1908c2ecf20Sopenharmony_ci dev_err(dev, "Error parsing regulator init data: %d\n", ret); 1918c2ecf20Sopenharmony_ci return ret; 1928c2ecf20Sopenharmony_ci } 1938c2ecf20Sopenharmony_ci 1948c2ecf20Sopenharmony_ci for (i = 0, match = as3711_regulator_matches; i < count; i++, match++) 1958c2ecf20Sopenharmony_ci if (match->of_node) { 1968c2ecf20Sopenharmony_ci pdata->init_data[i] = match->init_data; 1978c2ecf20Sopenharmony_ci of_node[i] = match->of_node; 1988c2ecf20Sopenharmony_ci } 1998c2ecf20Sopenharmony_ci 2008c2ecf20Sopenharmony_ci return 0; 2018c2ecf20Sopenharmony_ci} 2028c2ecf20Sopenharmony_ci 2038c2ecf20Sopenharmony_cistatic int as3711_regulator_probe(struct platform_device *pdev) 2048c2ecf20Sopenharmony_ci{ 2058c2ecf20Sopenharmony_ci struct as3711_regulator_pdata *pdata = dev_get_platdata(&pdev->dev); 2068c2ecf20Sopenharmony_ci struct as3711 *as3711 = dev_get_drvdata(pdev->dev.parent); 2078c2ecf20Sopenharmony_ci struct regulator_config config = {.dev = &pdev->dev,}; 2088c2ecf20Sopenharmony_ci struct device_node *of_node[AS3711_REGULATOR_NUM] = {}; 2098c2ecf20Sopenharmony_ci struct regulator_dev *rdev; 2108c2ecf20Sopenharmony_ci int ret; 2118c2ecf20Sopenharmony_ci int id; 2128c2ecf20Sopenharmony_ci 2138c2ecf20Sopenharmony_ci if (!pdata) { 2148c2ecf20Sopenharmony_ci dev_err(&pdev->dev, "No platform data...\n"); 2158c2ecf20Sopenharmony_ci return -ENODEV; 2168c2ecf20Sopenharmony_ci } 2178c2ecf20Sopenharmony_ci 2188c2ecf20Sopenharmony_ci if (pdev->dev.parent->of_node) { 2198c2ecf20Sopenharmony_ci ret = as3711_regulator_parse_dt(&pdev->dev, of_node, AS3711_REGULATOR_NUM); 2208c2ecf20Sopenharmony_ci if (ret < 0) { 2218c2ecf20Sopenharmony_ci dev_err(&pdev->dev, "DT parsing failed: %d\n", ret); 2228c2ecf20Sopenharmony_ci return ret; 2238c2ecf20Sopenharmony_ci } 2248c2ecf20Sopenharmony_ci } 2258c2ecf20Sopenharmony_ci 2268c2ecf20Sopenharmony_ci for (id = 0; id < AS3711_REGULATOR_NUM; id++) { 2278c2ecf20Sopenharmony_ci config.init_data = pdata->init_data[id]; 2288c2ecf20Sopenharmony_ci config.regmap = as3711->regmap; 2298c2ecf20Sopenharmony_ci config.of_node = of_node[id]; 2308c2ecf20Sopenharmony_ci 2318c2ecf20Sopenharmony_ci rdev = devm_regulator_register(&pdev->dev, &as3711_reg_desc[id], 2328c2ecf20Sopenharmony_ci &config); 2338c2ecf20Sopenharmony_ci if (IS_ERR(rdev)) { 2348c2ecf20Sopenharmony_ci dev_err(&pdev->dev, "Failed to register regulator %s\n", 2358c2ecf20Sopenharmony_ci as3711_reg_desc[id].name); 2368c2ecf20Sopenharmony_ci return PTR_ERR(rdev); 2378c2ecf20Sopenharmony_ci } 2388c2ecf20Sopenharmony_ci } 2398c2ecf20Sopenharmony_ci 2408c2ecf20Sopenharmony_ci return 0; 2418c2ecf20Sopenharmony_ci} 2428c2ecf20Sopenharmony_ci 2438c2ecf20Sopenharmony_cistatic struct platform_driver as3711_regulator_driver = { 2448c2ecf20Sopenharmony_ci .driver = { 2458c2ecf20Sopenharmony_ci .name = "as3711-regulator", 2468c2ecf20Sopenharmony_ci }, 2478c2ecf20Sopenharmony_ci .probe = as3711_regulator_probe, 2488c2ecf20Sopenharmony_ci}; 2498c2ecf20Sopenharmony_ci 2508c2ecf20Sopenharmony_cistatic int __init as3711_regulator_init(void) 2518c2ecf20Sopenharmony_ci{ 2528c2ecf20Sopenharmony_ci return platform_driver_register(&as3711_regulator_driver); 2538c2ecf20Sopenharmony_ci} 2548c2ecf20Sopenharmony_cisubsys_initcall(as3711_regulator_init); 2558c2ecf20Sopenharmony_ci 2568c2ecf20Sopenharmony_cistatic void __exit as3711_regulator_exit(void) 2578c2ecf20Sopenharmony_ci{ 2588c2ecf20Sopenharmony_ci platform_driver_unregister(&as3711_regulator_driver); 2598c2ecf20Sopenharmony_ci} 2608c2ecf20Sopenharmony_cimodule_exit(as3711_regulator_exit); 2618c2ecf20Sopenharmony_ci 2628c2ecf20Sopenharmony_ciMODULE_AUTHOR("Guennadi Liakhovetski <g.liakhovetski@gmx.de>"); 2638c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("AS3711 regulator driver"); 2648c2ecf20Sopenharmony_ciMODULE_ALIAS("platform:as3711-regulator"); 2658c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2"); 266