1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * fixed.c 4 * 5 * Copyright 2008 Wolfson Microelectronics PLC. 6 * 7 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com> 8 * 9 * Copyright (c) 2009 Nokia Corporation 10 * Roger Quadros <ext-roger.quadros@nokia.com> 11 * 12 * This is useful for systems with mixed controllable and 13 * non-controllable regulators, as well as for allowing testing on 14 * systems with no controllable regulators. 15 */ 16 17 #include <linux/err.h> 18 #include <linux/mutex.h> 19 #include <linux/module.h> 20 #include <linux/platform_device.h> 21 #include <linux/pm_domain.h> 22 #include <linux/pm_opp.h> 23 #include <linux/regulator/driver.h> 24 #include <linux/regulator/fixed.h> 25 #include <linux/gpio/consumer.h> 26 #include <linux/slab.h> 27 #include <linux/of.h> 28 #include <linux/regulator/of_regulator.h> 29 #include <linux/regulator/machine.h> 30 #include <linux/clk.h> 31 32 struct fixed_voltage_data { 33 struct regulator_desc desc; 34 struct regulator_dev *dev; 35 36 struct clk *enable_clock; 37 unsigned int enable_counter; 38 int performance_state; 39 }; 40 41 struct fixed_dev_type { 42 bool has_enable_clock; 43 bool has_performance_state; 44 }; 45 46 static int reg_clock_enable(struct regulator_dev *rdev) 47 { 48 struct fixed_voltage_data *priv = rdev_get_drvdata(rdev); 49 int ret = 0; 50 51 ret = clk_prepare_enable(priv->enable_clock); 52 if (ret) 53 return ret; 54 55 priv->enable_counter++; 56 57 return ret; 58 } 59 60 static int reg_clock_disable(struct regulator_dev *rdev) 61 { 62 struct fixed_voltage_data *priv = rdev_get_drvdata(rdev); 63 64 clk_disable_unprepare(priv->enable_clock); 65 priv->enable_counter--; 66 67 return 0; 68 } 69 70 static int reg_domain_enable(struct regulator_dev *rdev) 71 { 72 struct fixed_voltage_data *priv = rdev_get_drvdata(rdev); 73 struct device *dev = rdev->dev.parent; 74 int ret; 75 76 ret = dev_pm_genpd_set_performance_state(dev, priv->performance_state); 77 if (ret) 78 return ret; 79 80 priv->enable_counter++; 81 82 return ret; 83 } 84 85 static int reg_domain_disable(struct regulator_dev *rdev) 86 { 87 struct fixed_voltage_data *priv = rdev_get_drvdata(rdev); 88 struct device *dev = rdev->dev.parent; 89 int ret; 90 91 ret = dev_pm_genpd_set_performance_state(dev, 0); 92 if (ret) 93 return ret; 94 95 priv->enable_counter--; 96 97 return 0; 98 } 99 100 static int reg_is_enabled(struct regulator_dev *rdev) 101 { 102 struct fixed_voltage_data *priv = rdev_get_drvdata(rdev); 103 104 return priv->enable_counter > 0; 105 } 106 107 static irqreturn_t reg_fixed_under_voltage_irq_handler(int irq, void *data) 108 { 109 struct fixed_voltage_data *priv = data; 110 struct regulator_dev *rdev = priv->dev; 111 112 regulator_notifier_call_chain(rdev, REGULATOR_EVENT_UNDER_VOLTAGE, 113 NULL); 114 115 return IRQ_HANDLED; 116 } 117 118 /** 119 * reg_fixed_get_irqs - Get and register the optional IRQ for fixed voltage 120 * regulator. 121 * @dev: Pointer to the device structure. 122 * @priv: Pointer to fixed_voltage_data structure containing private data. 123 * 124 * This function tries to get the IRQ from the device firmware node. 125 * If it's an optional IRQ and not found, it returns 0. 126 * Otherwise, it attempts to request the threaded IRQ. 127 * 128 * Return: 0 on success, or a negative error number on failure. 129 */ 130 static int reg_fixed_get_irqs(struct device *dev, 131 struct fixed_voltage_data *priv) 132 { 133 int ret; 134 135 ret = fwnode_irq_get(dev_fwnode(dev), 0); 136 /* This is optional IRQ. If not found we will get -EINVAL */ 137 if (ret == -EINVAL) 138 return 0; 139 if (ret < 0) 140 return dev_err_probe(dev, ret, "Failed to get IRQ\n"); 141 142 ret = devm_request_threaded_irq(dev, ret, NULL, 143 reg_fixed_under_voltage_irq_handler, 144 IRQF_ONESHOT, "under-voltage", priv); 145 if (ret) 146 return dev_err_probe(dev, ret, "Failed to request IRQ\n"); 147 148 return 0; 149 } 150 151 /** 152 * of_get_fixed_voltage_config - extract fixed_voltage_config structure info 153 * @dev: device requesting for fixed_voltage_config 154 * @desc: regulator description 155 * 156 * Populates fixed_voltage_config structure by extracting data from device 157 * tree node. 158 * 159 * Return: Pointer to a populated &struct fixed_voltage_config or %NULL if 160 * memory allocation fails. 161 */ 162 static struct fixed_voltage_config * 163 of_get_fixed_voltage_config(struct device *dev, 164 const struct regulator_desc *desc) 165 { 166 struct fixed_voltage_config *config; 167 struct device_node *np = dev->of_node; 168 struct regulator_init_data *init_data; 169 170 config = devm_kzalloc(dev, sizeof(struct fixed_voltage_config), 171 GFP_KERNEL); 172 if (!config) 173 return ERR_PTR(-ENOMEM); 174 175 config->init_data = of_get_regulator_init_data(dev, dev->of_node, desc); 176 if (!config->init_data) 177 return ERR_PTR(-EINVAL); 178 179 init_data = config->init_data; 180 init_data->constraints.apply_uV = 0; 181 182 config->supply_name = init_data->constraints.name; 183 if (init_data->constraints.min_uV == init_data->constraints.max_uV) { 184 config->microvolts = init_data->constraints.min_uV; 185 } else { 186 dev_err(dev, 187 "Fixed regulator specified with variable voltages\n"); 188 return ERR_PTR(-EINVAL); 189 } 190 191 if (init_data->constraints.boot_on) 192 config->enabled_at_boot = true; 193 194 of_property_read_u32(np, "startup-delay-us", &config->startup_delay); 195 of_property_read_u32(np, "off-on-delay-us", &config->off_on_delay); 196 197 if (of_property_present(np, "vin-supply")) 198 config->input_supply = "vin"; 199 200 return config; 201 } 202 203 static const struct regulator_ops fixed_voltage_ops = { 204 }; 205 206 static const struct regulator_ops fixed_voltage_clkenabled_ops = { 207 .enable = reg_clock_enable, 208 .disable = reg_clock_disable, 209 .is_enabled = reg_is_enabled, 210 }; 211 212 static const struct regulator_ops fixed_voltage_domain_ops = { 213 .enable = reg_domain_enable, 214 .disable = reg_domain_disable, 215 .is_enabled = reg_is_enabled, 216 }; 217 218 static int reg_fixed_voltage_probe(struct platform_device *pdev) 219 { 220 struct device *dev = &pdev->dev; 221 struct fixed_voltage_config *config; 222 struct fixed_voltage_data *drvdata; 223 const struct fixed_dev_type *drvtype = of_device_get_match_data(dev); 224 struct regulator_config cfg = { }; 225 enum gpiod_flags gflags; 226 int ret; 227 228 drvdata = devm_kzalloc(&pdev->dev, sizeof(struct fixed_voltage_data), 229 GFP_KERNEL); 230 if (!drvdata) 231 return -ENOMEM; 232 233 if (pdev->dev.of_node) { 234 config = of_get_fixed_voltage_config(&pdev->dev, 235 &drvdata->desc); 236 if (IS_ERR(config)) 237 return PTR_ERR(config); 238 } else { 239 config = dev_get_platdata(&pdev->dev); 240 } 241 242 if (!config) 243 return -ENOMEM; 244 245 drvdata->desc.name = devm_kstrdup(&pdev->dev, 246 config->supply_name, 247 GFP_KERNEL); 248 if (drvdata->desc.name == NULL) { 249 dev_err(&pdev->dev, "Failed to allocate supply name\n"); 250 return -ENOMEM; 251 } 252 drvdata->desc.type = REGULATOR_VOLTAGE; 253 drvdata->desc.owner = THIS_MODULE; 254 255 if (drvtype && drvtype->has_enable_clock) { 256 drvdata->desc.ops = &fixed_voltage_clkenabled_ops; 257 258 drvdata->enable_clock = devm_clk_get(dev, NULL); 259 if (IS_ERR(drvdata->enable_clock)) { 260 dev_err(dev, "Can't get enable-clock from devicetree\n"); 261 return PTR_ERR(drvdata->enable_clock); 262 } 263 } else if (drvtype && drvtype->has_performance_state) { 264 drvdata->desc.ops = &fixed_voltage_domain_ops; 265 266 drvdata->performance_state = of_get_required_opp_performance_state(dev->of_node, 0); 267 if (drvdata->performance_state < 0) { 268 dev_err(dev, "Can't get performance state from devicetree\n"); 269 return drvdata->performance_state; 270 } 271 } else { 272 drvdata->desc.ops = &fixed_voltage_ops; 273 } 274 275 drvdata->desc.enable_time = config->startup_delay; 276 drvdata->desc.off_on_delay = config->off_on_delay; 277 278 if (config->input_supply) { 279 drvdata->desc.supply_name = devm_kstrdup(&pdev->dev, 280 config->input_supply, 281 GFP_KERNEL); 282 if (!drvdata->desc.supply_name) 283 return -ENOMEM; 284 } 285 286 if (config->microvolts) 287 drvdata->desc.n_voltages = 1; 288 289 drvdata->desc.fixed_uV = config->microvolts; 290 291 /* 292 * The signal will be inverted by the GPIO core if flagged so in the 293 * descriptor. 294 */ 295 if (config->enabled_at_boot) 296 gflags = GPIOD_OUT_HIGH; 297 else 298 gflags = GPIOD_OUT_LOW; 299 300 /* 301 * Some fixed regulators share the enable line between two 302 * regulators which makes it necessary to get a handle on the 303 * same descriptor for two different consumers. This will get 304 * the GPIO descriptor, but only the first call will initialize 305 * it so any flags such as inversion or open drain will only 306 * be set up by the first caller and assumed identical on the 307 * next caller. 308 * 309 * FIXME: find a better way to deal with this. 310 */ 311 gflags |= GPIOD_FLAGS_BIT_NONEXCLUSIVE; 312 313 /* 314 * Do not use devm* here: the regulator core takes over the 315 * lifecycle management of the GPIO descriptor. 316 */ 317 cfg.ena_gpiod = gpiod_get_optional(&pdev->dev, NULL, gflags); 318 if (IS_ERR(cfg.ena_gpiod)) 319 return dev_err_probe(&pdev->dev, PTR_ERR(cfg.ena_gpiod), 320 "can't get GPIO\n"); 321 322 cfg.dev = &pdev->dev; 323 cfg.init_data = config->init_data; 324 cfg.driver_data = drvdata; 325 cfg.of_node = pdev->dev.of_node; 326 327 drvdata->dev = devm_regulator_register(&pdev->dev, &drvdata->desc, 328 &cfg); 329 if (IS_ERR(drvdata->dev)) 330 return dev_err_probe(&pdev->dev, PTR_ERR(drvdata->dev), 331 "Failed to register regulator: %ld\n", 332 PTR_ERR(drvdata->dev)); 333 334 platform_set_drvdata(pdev, drvdata); 335 336 dev_dbg(&pdev->dev, "%s supplying %duV\n", drvdata->desc.name, 337 drvdata->desc.fixed_uV); 338 339 ret = reg_fixed_get_irqs(dev, drvdata); 340 if (ret) 341 return ret; 342 343 return 0; 344 } 345 346 #if defined(CONFIG_OF) 347 static const struct fixed_dev_type fixed_voltage_data = { 348 .has_enable_clock = false, 349 }; 350 351 static const struct fixed_dev_type fixed_clkenable_data = { 352 .has_enable_clock = true, 353 }; 354 355 static const struct fixed_dev_type fixed_domain_data = { 356 .has_performance_state = true, 357 }; 358 359 static const struct of_device_id fixed_of_match[] = { 360 { 361 .compatible = "regulator-fixed", 362 .data = &fixed_voltage_data, 363 }, 364 { 365 .compatible = "regulator-fixed-clock", 366 .data = &fixed_clkenable_data, 367 }, 368 { 369 .compatible = "regulator-fixed-domain", 370 .data = &fixed_domain_data, 371 }, 372 { 373 }, 374 }; 375 MODULE_DEVICE_TABLE(of, fixed_of_match); 376 #endif 377 378 static struct platform_driver regulator_fixed_voltage_driver = { 379 .probe = reg_fixed_voltage_probe, 380 .driver = { 381 .name = "reg-fixed-voltage", 382 .probe_type = PROBE_PREFER_ASYNCHRONOUS, 383 .of_match_table = of_match_ptr(fixed_of_match), 384 }, 385 }; 386 387 static int __init regulator_fixed_voltage_init(void) 388 { 389 return platform_driver_register(®ulator_fixed_voltage_driver); 390 } 391 subsys_initcall(regulator_fixed_voltage_init); 392 393 static void __exit regulator_fixed_voltage_exit(void) 394 { 395 platform_driver_unregister(®ulator_fixed_voltage_driver); 396 } 397 module_exit(regulator_fixed_voltage_exit); 398 399 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>"); 400 MODULE_DESCRIPTION("Fixed voltage regulator"); 401 MODULE_LICENSE("GPL"); 402 MODULE_ALIAS("platform:reg-fixed-voltage"); 403