18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * fixed.c 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright 2008 Wolfson Microelectronics PLC. 68c2ecf20Sopenharmony_ci * 78c2ecf20Sopenharmony_ci * Author: Mark Brown <broonie@opensource.wolfsonmicro.com> 88c2ecf20Sopenharmony_ci * 98c2ecf20Sopenharmony_ci * Copyright (c) 2009 Nokia Corporation 108c2ecf20Sopenharmony_ci * Roger Quadros <ext-roger.quadros@nokia.com> 118c2ecf20Sopenharmony_ci * 128c2ecf20Sopenharmony_ci * This is useful for systems with mixed controllable and 138c2ecf20Sopenharmony_ci * non-controllable regulators, as well as for allowing testing on 148c2ecf20Sopenharmony_ci * systems with no controllable regulators. 158c2ecf20Sopenharmony_ci */ 168c2ecf20Sopenharmony_ci 178c2ecf20Sopenharmony_ci#include <linux/err.h> 188c2ecf20Sopenharmony_ci#include <linux/mutex.h> 198c2ecf20Sopenharmony_ci#include <linux/module.h> 208c2ecf20Sopenharmony_ci#include <linux/platform_device.h> 218c2ecf20Sopenharmony_ci#include <linux/regulator/driver.h> 228c2ecf20Sopenharmony_ci#include <linux/regulator/fixed.h> 238c2ecf20Sopenharmony_ci#include <linux/gpio/consumer.h> 248c2ecf20Sopenharmony_ci#include <linux/slab.h> 258c2ecf20Sopenharmony_ci#include <linux/of.h> 268c2ecf20Sopenharmony_ci#include <linux/of_device.h> 278c2ecf20Sopenharmony_ci#include <linux/regulator/of_regulator.h> 288c2ecf20Sopenharmony_ci#include <linux/regulator/machine.h> 298c2ecf20Sopenharmony_ci#include <linux/clk.h> 308c2ecf20Sopenharmony_ci 318c2ecf20Sopenharmony_ci 328c2ecf20Sopenharmony_cistruct fixed_voltage_data { 338c2ecf20Sopenharmony_ci struct regulator_desc desc; 348c2ecf20Sopenharmony_ci struct regulator_dev *dev; 358c2ecf20Sopenharmony_ci 368c2ecf20Sopenharmony_ci struct clk *enable_clock; 378c2ecf20Sopenharmony_ci unsigned int clk_enable_counter; 388c2ecf20Sopenharmony_ci}; 398c2ecf20Sopenharmony_ci 408c2ecf20Sopenharmony_cistruct fixed_dev_type { 418c2ecf20Sopenharmony_ci bool has_enable_clock; 428c2ecf20Sopenharmony_ci}; 438c2ecf20Sopenharmony_ci 448c2ecf20Sopenharmony_cistatic int reg_clock_enable(struct regulator_dev *rdev) 458c2ecf20Sopenharmony_ci{ 468c2ecf20Sopenharmony_ci struct fixed_voltage_data *priv = rdev_get_drvdata(rdev); 478c2ecf20Sopenharmony_ci int ret = 0; 488c2ecf20Sopenharmony_ci 498c2ecf20Sopenharmony_ci ret = clk_prepare_enable(priv->enable_clock); 508c2ecf20Sopenharmony_ci if (ret) 518c2ecf20Sopenharmony_ci return ret; 528c2ecf20Sopenharmony_ci 538c2ecf20Sopenharmony_ci priv->clk_enable_counter++; 548c2ecf20Sopenharmony_ci 558c2ecf20Sopenharmony_ci return ret; 568c2ecf20Sopenharmony_ci} 578c2ecf20Sopenharmony_ci 588c2ecf20Sopenharmony_cistatic int reg_clock_disable(struct regulator_dev *rdev) 598c2ecf20Sopenharmony_ci{ 608c2ecf20Sopenharmony_ci struct fixed_voltage_data *priv = rdev_get_drvdata(rdev); 618c2ecf20Sopenharmony_ci 628c2ecf20Sopenharmony_ci clk_disable_unprepare(priv->enable_clock); 638c2ecf20Sopenharmony_ci priv->clk_enable_counter--; 648c2ecf20Sopenharmony_ci 658c2ecf20Sopenharmony_ci return 0; 668c2ecf20Sopenharmony_ci} 678c2ecf20Sopenharmony_ci 688c2ecf20Sopenharmony_cistatic int reg_clock_is_enabled(struct regulator_dev *rdev) 698c2ecf20Sopenharmony_ci{ 708c2ecf20Sopenharmony_ci struct fixed_voltage_data *priv = rdev_get_drvdata(rdev); 718c2ecf20Sopenharmony_ci 728c2ecf20Sopenharmony_ci return priv->clk_enable_counter > 0; 738c2ecf20Sopenharmony_ci} 748c2ecf20Sopenharmony_ci 758c2ecf20Sopenharmony_ci 768c2ecf20Sopenharmony_ci/** 778c2ecf20Sopenharmony_ci * of_get_fixed_voltage_config - extract fixed_voltage_config structure info 788c2ecf20Sopenharmony_ci * @dev: device requesting for fixed_voltage_config 798c2ecf20Sopenharmony_ci * @desc: regulator description 808c2ecf20Sopenharmony_ci * 818c2ecf20Sopenharmony_ci * Populates fixed_voltage_config structure by extracting data from device 828c2ecf20Sopenharmony_ci * tree node, returns a pointer to the populated structure of NULL if memory 838c2ecf20Sopenharmony_ci * alloc fails. 848c2ecf20Sopenharmony_ci */ 858c2ecf20Sopenharmony_cistatic struct fixed_voltage_config * 868c2ecf20Sopenharmony_ciof_get_fixed_voltage_config(struct device *dev, 878c2ecf20Sopenharmony_ci const struct regulator_desc *desc) 888c2ecf20Sopenharmony_ci{ 898c2ecf20Sopenharmony_ci struct fixed_voltage_config *config; 908c2ecf20Sopenharmony_ci struct device_node *np = dev->of_node; 918c2ecf20Sopenharmony_ci struct regulator_init_data *init_data; 928c2ecf20Sopenharmony_ci 938c2ecf20Sopenharmony_ci config = devm_kzalloc(dev, sizeof(struct fixed_voltage_config), 948c2ecf20Sopenharmony_ci GFP_KERNEL); 958c2ecf20Sopenharmony_ci if (!config) 968c2ecf20Sopenharmony_ci return ERR_PTR(-ENOMEM); 978c2ecf20Sopenharmony_ci 988c2ecf20Sopenharmony_ci config->init_data = of_get_regulator_init_data(dev, dev->of_node, desc); 998c2ecf20Sopenharmony_ci if (!config->init_data) 1008c2ecf20Sopenharmony_ci return ERR_PTR(-EINVAL); 1018c2ecf20Sopenharmony_ci 1028c2ecf20Sopenharmony_ci init_data = config->init_data; 1038c2ecf20Sopenharmony_ci init_data->constraints.apply_uV = 0; 1048c2ecf20Sopenharmony_ci 1058c2ecf20Sopenharmony_ci config->supply_name = init_data->constraints.name; 1068c2ecf20Sopenharmony_ci if (init_data->constraints.min_uV == init_data->constraints.max_uV) { 1078c2ecf20Sopenharmony_ci config->microvolts = init_data->constraints.min_uV; 1088c2ecf20Sopenharmony_ci } else { 1098c2ecf20Sopenharmony_ci dev_err(dev, 1108c2ecf20Sopenharmony_ci "Fixed regulator specified with variable voltages\n"); 1118c2ecf20Sopenharmony_ci return ERR_PTR(-EINVAL); 1128c2ecf20Sopenharmony_ci } 1138c2ecf20Sopenharmony_ci 1148c2ecf20Sopenharmony_ci if (init_data->constraints.boot_on) 1158c2ecf20Sopenharmony_ci config->enabled_at_boot = true; 1168c2ecf20Sopenharmony_ci 1178c2ecf20Sopenharmony_ci of_property_read_u32(np, "startup-delay-us", &config->startup_delay); 1188c2ecf20Sopenharmony_ci of_property_read_u32(np, "off-on-delay-us", &config->off_on_delay); 1198c2ecf20Sopenharmony_ci 1208c2ecf20Sopenharmony_ci if (of_find_property(np, "vin-supply", NULL)) 1218c2ecf20Sopenharmony_ci config->input_supply = "vin"; 1228c2ecf20Sopenharmony_ci 1238c2ecf20Sopenharmony_ci return config; 1248c2ecf20Sopenharmony_ci} 1258c2ecf20Sopenharmony_ci 1268c2ecf20Sopenharmony_cistatic const struct regulator_ops fixed_voltage_ops = { 1278c2ecf20Sopenharmony_ci}; 1288c2ecf20Sopenharmony_ci 1298c2ecf20Sopenharmony_cistatic const struct regulator_ops fixed_voltage_clkenabled_ops = { 1308c2ecf20Sopenharmony_ci .enable = reg_clock_enable, 1318c2ecf20Sopenharmony_ci .disable = reg_clock_disable, 1328c2ecf20Sopenharmony_ci .is_enabled = reg_clock_is_enabled, 1338c2ecf20Sopenharmony_ci}; 1348c2ecf20Sopenharmony_ci 1358c2ecf20Sopenharmony_cistatic int reg_fixed_voltage_probe(struct platform_device *pdev) 1368c2ecf20Sopenharmony_ci{ 1378c2ecf20Sopenharmony_ci struct device *dev = &pdev->dev; 1388c2ecf20Sopenharmony_ci struct fixed_voltage_config *config; 1398c2ecf20Sopenharmony_ci struct fixed_voltage_data *drvdata; 1408c2ecf20Sopenharmony_ci const struct fixed_dev_type *drvtype = of_device_get_match_data(dev); 1418c2ecf20Sopenharmony_ci struct regulator_config cfg = { }; 1428c2ecf20Sopenharmony_ci enum gpiod_flags gflags; 1438c2ecf20Sopenharmony_ci int ret; 1448c2ecf20Sopenharmony_ci 1458c2ecf20Sopenharmony_ci drvdata = devm_kzalloc(&pdev->dev, sizeof(struct fixed_voltage_data), 1468c2ecf20Sopenharmony_ci GFP_KERNEL); 1478c2ecf20Sopenharmony_ci if (!drvdata) 1488c2ecf20Sopenharmony_ci return -ENOMEM; 1498c2ecf20Sopenharmony_ci 1508c2ecf20Sopenharmony_ci if (pdev->dev.of_node) { 1518c2ecf20Sopenharmony_ci config = of_get_fixed_voltage_config(&pdev->dev, 1528c2ecf20Sopenharmony_ci &drvdata->desc); 1538c2ecf20Sopenharmony_ci if (IS_ERR(config)) 1548c2ecf20Sopenharmony_ci return PTR_ERR(config); 1558c2ecf20Sopenharmony_ci } else { 1568c2ecf20Sopenharmony_ci config = dev_get_platdata(&pdev->dev); 1578c2ecf20Sopenharmony_ci } 1588c2ecf20Sopenharmony_ci 1598c2ecf20Sopenharmony_ci if (!config) 1608c2ecf20Sopenharmony_ci return -ENOMEM; 1618c2ecf20Sopenharmony_ci 1628c2ecf20Sopenharmony_ci drvdata->desc.name = devm_kstrdup(&pdev->dev, 1638c2ecf20Sopenharmony_ci config->supply_name, 1648c2ecf20Sopenharmony_ci GFP_KERNEL); 1658c2ecf20Sopenharmony_ci if (drvdata->desc.name == NULL) { 1668c2ecf20Sopenharmony_ci dev_err(&pdev->dev, "Failed to allocate supply name\n"); 1678c2ecf20Sopenharmony_ci return -ENOMEM; 1688c2ecf20Sopenharmony_ci } 1698c2ecf20Sopenharmony_ci drvdata->desc.type = REGULATOR_VOLTAGE; 1708c2ecf20Sopenharmony_ci drvdata->desc.owner = THIS_MODULE; 1718c2ecf20Sopenharmony_ci 1728c2ecf20Sopenharmony_ci if (drvtype && drvtype->has_enable_clock) { 1738c2ecf20Sopenharmony_ci drvdata->desc.ops = &fixed_voltage_clkenabled_ops; 1748c2ecf20Sopenharmony_ci 1758c2ecf20Sopenharmony_ci drvdata->enable_clock = devm_clk_get(dev, NULL); 1768c2ecf20Sopenharmony_ci if (IS_ERR(drvdata->enable_clock)) { 1778c2ecf20Sopenharmony_ci dev_err(dev, "Can't get enable-clock from devicetree\n"); 1788c2ecf20Sopenharmony_ci return PTR_ERR(drvdata->enable_clock); 1798c2ecf20Sopenharmony_ci } 1808c2ecf20Sopenharmony_ci } else { 1818c2ecf20Sopenharmony_ci drvdata->desc.ops = &fixed_voltage_ops; 1828c2ecf20Sopenharmony_ci } 1838c2ecf20Sopenharmony_ci 1848c2ecf20Sopenharmony_ci drvdata->desc.enable_time = config->startup_delay; 1858c2ecf20Sopenharmony_ci drvdata->desc.off_on_delay = config->off_on_delay; 1868c2ecf20Sopenharmony_ci 1878c2ecf20Sopenharmony_ci if (config->input_supply) { 1888c2ecf20Sopenharmony_ci drvdata->desc.supply_name = devm_kstrdup(&pdev->dev, 1898c2ecf20Sopenharmony_ci config->input_supply, 1908c2ecf20Sopenharmony_ci GFP_KERNEL); 1918c2ecf20Sopenharmony_ci if (!drvdata->desc.supply_name) { 1928c2ecf20Sopenharmony_ci dev_err(&pdev->dev, 1938c2ecf20Sopenharmony_ci "Failed to allocate input supply\n"); 1948c2ecf20Sopenharmony_ci return -ENOMEM; 1958c2ecf20Sopenharmony_ci } 1968c2ecf20Sopenharmony_ci } 1978c2ecf20Sopenharmony_ci 1988c2ecf20Sopenharmony_ci if (config->microvolts) 1998c2ecf20Sopenharmony_ci drvdata->desc.n_voltages = 1; 2008c2ecf20Sopenharmony_ci 2018c2ecf20Sopenharmony_ci drvdata->desc.fixed_uV = config->microvolts; 2028c2ecf20Sopenharmony_ci 2038c2ecf20Sopenharmony_ci /* 2048c2ecf20Sopenharmony_ci * The signal will be inverted by the GPIO core if flagged so in the 2058c2ecf20Sopenharmony_ci * descriptor. 2068c2ecf20Sopenharmony_ci */ 2078c2ecf20Sopenharmony_ci if (config->enabled_at_boot) 2088c2ecf20Sopenharmony_ci gflags = GPIOD_OUT_HIGH; 2098c2ecf20Sopenharmony_ci else 2108c2ecf20Sopenharmony_ci gflags = GPIOD_OUT_LOW; 2118c2ecf20Sopenharmony_ci 2128c2ecf20Sopenharmony_ci /* 2138c2ecf20Sopenharmony_ci * Some fixed regulators share the enable line between two 2148c2ecf20Sopenharmony_ci * regulators which makes it necessary to get a handle on the 2158c2ecf20Sopenharmony_ci * same descriptor for two different consumers. This will get 2168c2ecf20Sopenharmony_ci * the GPIO descriptor, but only the first call will initialize 2178c2ecf20Sopenharmony_ci * it so any flags such as inversion or open drain will only 2188c2ecf20Sopenharmony_ci * be set up by the first caller and assumed identical on the 2198c2ecf20Sopenharmony_ci * next caller. 2208c2ecf20Sopenharmony_ci * 2218c2ecf20Sopenharmony_ci * FIXME: find a better way to deal with this. 2228c2ecf20Sopenharmony_ci */ 2238c2ecf20Sopenharmony_ci gflags |= GPIOD_FLAGS_BIT_NONEXCLUSIVE; 2248c2ecf20Sopenharmony_ci 2258c2ecf20Sopenharmony_ci /* 2268c2ecf20Sopenharmony_ci * Do not use devm* here: the regulator core takes over the 2278c2ecf20Sopenharmony_ci * lifecycle management of the GPIO descriptor. 2288c2ecf20Sopenharmony_ci */ 2298c2ecf20Sopenharmony_ci cfg.ena_gpiod = gpiod_get_optional(&pdev->dev, NULL, gflags); 2308c2ecf20Sopenharmony_ci if (IS_ERR(cfg.ena_gpiod)) 2318c2ecf20Sopenharmony_ci return PTR_ERR(cfg.ena_gpiod); 2328c2ecf20Sopenharmony_ci 2338c2ecf20Sopenharmony_ci cfg.dev = &pdev->dev; 2348c2ecf20Sopenharmony_ci cfg.init_data = config->init_data; 2358c2ecf20Sopenharmony_ci cfg.driver_data = drvdata; 2368c2ecf20Sopenharmony_ci cfg.of_node = pdev->dev.of_node; 2378c2ecf20Sopenharmony_ci 2388c2ecf20Sopenharmony_ci drvdata->dev = devm_regulator_register(&pdev->dev, &drvdata->desc, 2398c2ecf20Sopenharmony_ci &cfg); 2408c2ecf20Sopenharmony_ci if (IS_ERR(drvdata->dev)) { 2418c2ecf20Sopenharmony_ci ret = PTR_ERR(drvdata->dev); 2428c2ecf20Sopenharmony_ci dev_err(&pdev->dev, "Failed to register regulator: %d\n", ret); 2438c2ecf20Sopenharmony_ci return ret; 2448c2ecf20Sopenharmony_ci } 2458c2ecf20Sopenharmony_ci 2468c2ecf20Sopenharmony_ci platform_set_drvdata(pdev, drvdata); 2478c2ecf20Sopenharmony_ci 2488c2ecf20Sopenharmony_ci dev_dbg(&pdev->dev, "%s supplying %duV\n", drvdata->desc.name, 2498c2ecf20Sopenharmony_ci drvdata->desc.fixed_uV); 2508c2ecf20Sopenharmony_ci 2518c2ecf20Sopenharmony_ci return 0; 2528c2ecf20Sopenharmony_ci} 2538c2ecf20Sopenharmony_ci 2548c2ecf20Sopenharmony_ci#if defined(CONFIG_OF) 2558c2ecf20Sopenharmony_cistatic const struct fixed_dev_type fixed_voltage_data = { 2568c2ecf20Sopenharmony_ci .has_enable_clock = false, 2578c2ecf20Sopenharmony_ci}; 2588c2ecf20Sopenharmony_ci 2598c2ecf20Sopenharmony_cistatic const struct fixed_dev_type fixed_clkenable_data = { 2608c2ecf20Sopenharmony_ci .has_enable_clock = true, 2618c2ecf20Sopenharmony_ci}; 2628c2ecf20Sopenharmony_ci 2638c2ecf20Sopenharmony_cistatic const struct of_device_id fixed_of_match[] = { 2648c2ecf20Sopenharmony_ci { 2658c2ecf20Sopenharmony_ci .compatible = "regulator-fixed", 2668c2ecf20Sopenharmony_ci .data = &fixed_voltage_data, 2678c2ecf20Sopenharmony_ci }, 2688c2ecf20Sopenharmony_ci { 2698c2ecf20Sopenharmony_ci .compatible = "regulator-fixed-clock", 2708c2ecf20Sopenharmony_ci .data = &fixed_clkenable_data, 2718c2ecf20Sopenharmony_ci }, 2728c2ecf20Sopenharmony_ci { 2738c2ecf20Sopenharmony_ci }, 2748c2ecf20Sopenharmony_ci}; 2758c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, fixed_of_match); 2768c2ecf20Sopenharmony_ci#endif 2778c2ecf20Sopenharmony_ci 2788c2ecf20Sopenharmony_cistatic struct platform_driver regulator_fixed_voltage_driver = { 2798c2ecf20Sopenharmony_ci .probe = reg_fixed_voltage_probe, 2808c2ecf20Sopenharmony_ci .driver = { 2818c2ecf20Sopenharmony_ci .name = "reg-fixed-voltage", 2828c2ecf20Sopenharmony_ci .of_match_table = of_match_ptr(fixed_of_match), 2838c2ecf20Sopenharmony_ci }, 2848c2ecf20Sopenharmony_ci}; 2858c2ecf20Sopenharmony_ci 2868c2ecf20Sopenharmony_cistatic int __init regulator_fixed_voltage_init(void) 2878c2ecf20Sopenharmony_ci{ 2888c2ecf20Sopenharmony_ci return platform_driver_register(®ulator_fixed_voltage_driver); 2898c2ecf20Sopenharmony_ci} 2908c2ecf20Sopenharmony_cisubsys_initcall(regulator_fixed_voltage_init); 2918c2ecf20Sopenharmony_ci 2928c2ecf20Sopenharmony_cistatic void __exit regulator_fixed_voltage_exit(void) 2938c2ecf20Sopenharmony_ci{ 2948c2ecf20Sopenharmony_ci platform_driver_unregister(®ulator_fixed_voltage_driver); 2958c2ecf20Sopenharmony_ci} 2968c2ecf20Sopenharmony_cimodule_exit(regulator_fixed_voltage_exit); 2978c2ecf20Sopenharmony_ci 2988c2ecf20Sopenharmony_ciMODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>"); 2998c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Fixed voltage regulator"); 3008c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL"); 3018c2ecf20Sopenharmony_ciMODULE_ALIAS("platform:reg-fixed-voltage"); 302