1 /* 2 * fixed.c 3 * 4 * Copyright 2008 Wolfson Microelectronics PLC. 5 * 6 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com> 7 * 8 * Copyright (c) 2009 Nokia Corporation 9 * Roger Quadros <ext-roger.quadros@nokia.com> 10 * 11 * This program is free software; you can redistribute it and/or 12 * modify it under the terms of the GNU General Public License as 13 * published by the Free Software Foundation; either version 2 of the 14 * License, or (at your option) any later version. 15 * 16 * This is useful for systems with mixed controllable and 17 * non-controllable regulators, as well as for allowing testing on 18 * systems with no controllable regulators. 19 */ 20 21 #include <linux/err.h> 22 #include <linux/mutex.h> 23 #include <linux/module.h> 24 #include <linux/platform_device.h> 25 #include <linux/regulator/driver.h> 26 #include <linux/regulator/fixed.h> 27 #include <linux/gpio/consumer.h> 28 #include <linux/slab.h> 29 #include <linux/of.h> 30 #include <linux/regulator/of_regulator.h> 31 #include <linux/regulator/machine.h> 32 33 struct fixed_voltage_data { 34 struct regulator_desc desc; 35 struct regulator_dev *dev; 36 }; 37 38 39 /** 40 * of_get_fixed_voltage_config - extract fixed_voltage_config structure info 41 * @dev: device requesting for fixed_voltage_config 42 * @desc: regulator description 43 * 44 * Populates fixed_voltage_config structure by extracting data from device 45 * tree node, returns a pointer to the populated structure of NULL if memory 46 * alloc fails. 47 */ 48 static struct fixed_voltage_config * 49 of_get_fixed_voltage_config(struct device *dev, 50 const struct regulator_desc *desc) 51 { 52 struct fixed_voltage_config *config; 53 struct device_node *np = dev->of_node; 54 struct regulator_init_data *init_data; 55 56 config = devm_kzalloc(dev, sizeof(struct fixed_voltage_config), 57 GFP_KERNEL); 58 if (!config) 59 return ERR_PTR(-ENOMEM); 60 61 config->init_data = of_get_regulator_init_data(dev, dev->of_node, desc); 62 if (!config->init_data) 63 return ERR_PTR(-EINVAL); 64 65 init_data = config->init_data; 66 init_data->constraints.apply_uV = 0; 67 68 config->supply_name = init_data->constraints.name; 69 if (init_data->constraints.min_uV == init_data->constraints.max_uV) { 70 config->microvolts = init_data->constraints.min_uV; 71 } else { 72 dev_err(dev, 73 "Fixed regulator specified with variable voltages\n"); 74 return ERR_PTR(-EINVAL); 75 } 76 77 if (init_data->constraints.boot_on) 78 config->enabled_at_boot = true; 79 80 of_property_read_u32(np, "startup-delay-us", &config->startup_delay); 81 82 /* 83 * FIXME: we pulled active low/high and open drain handling into 84 * gpiolib so it will be handled there. Delete this in the second 85 * step when we also remove the custom inversion handling for all 86 * legacy boardfiles. 87 */ 88 config->enable_high = 1; 89 config->gpio_is_open_drain = 0; 90 91 if (of_find_property(np, "vin-supply", NULL)) 92 config->input_supply = "vin"; 93 94 return config; 95 } 96 97 static struct regulator_ops fixed_voltage_ops = { 98 }; 99 100 static int reg_fixed_voltage_probe(struct platform_device *pdev) 101 { 102 struct fixed_voltage_config *config; 103 struct fixed_voltage_data *drvdata; 104 struct regulator_config cfg = { }; 105 enum gpiod_flags gflags; 106 int ret; 107 108 drvdata = devm_kzalloc(&pdev->dev, sizeof(struct fixed_voltage_data), 109 GFP_KERNEL); 110 if (!drvdata) 111 return -ENOMEM; 112 113 if (pdev->dev.of_node) { 114 config = of_get_fixed_voltage_config(&pdev->dev, 115 &drvdata->desc); 116 if (IS_ERR(config)) 117 return PTR_ERR(config); 118 } else { 119 config = dev_get_platdata(&pdev->dev); 120 } 121 122 if (!config) 123 return -ENOMEM; 124 125 drvdata->desc.name = devm_kstrdup(&pdev->dev, 126 config->supply_name, 127 GFP_KERNEL); 128 if (drvdata->desc.name == NULL) { 129 dev_err(&pdev->dev, "Failed to allocate supply name\n"); 130 return -ENOMEM; 131 } 132 drvdata->desc.type = REGULATOR_VOLTAGE; 133 drvdata->desc.owner = THIS_MODULE; 134 drvdata->desc.ops = &fixed_voltage_ops; 135 136 drvdata->desc.enable_time = config->startup_delay; 137 138 if (config->input_supply) { 139 drvdata->desc.supply_name = devm_kstrdup(&pdev->dev, 140 config->input_supply, 141 GFP_KERNEL); 142 if (!drvdata->desc.supply_name) { 143 dev_err(&pdev->dev, 144 "Failed to allocate input supply\n"); 145 return -ENOMEM; 146 } 147 } 148 149 if (config->microvolts) 150 drvdata->desc.n_voltages = 1; 151 152 drvdata->desc.fixed_uV = config->microvolts; 153 154 cfg.ena_gpio_invert = !config->enable_high; 155 if (config->enabled_at_boot) { 156 if (config->enable_high) 157 gflags = GPIOD_OUT_HIGH; 158 else 159 gflags = GPIOD_OUT_LOW; 160 } else { 161 if (config->enable_high) 162 gflags = GPIOD_OUT_LOW; 163 else 164 gflags = GPIOD_OUT_HIGH; 165 } 166 if (config->gpio_is_open_drain) { 167 if (gflags == GPIOD_OUT_HIGH) 168 gflags = GPIOD_OUT_HIGH_OPEN_DRAIN; 169 else 170 gflags = GPIOD_OUT_LOW_OPEN_DRAIN; 171 } 172 173 /* 174 * Some fixed regulators share the enable line between two 175 * regulators which makes it necessary to get a handle on the 176 * same descriptor for two different consumers. This will get 177 * the GPIO descriptor, but only the first call will initialize 178 * it so any flags such as inversion or open drain will only 179 * be set up by the first caller and assumed identical on the 180 * next caller. 181 * 182 * FIXME: find a better way to deal with this. 183 */ 184 gflags |= GPIOD_FLAGS_BIT_NONEXCLUSIVE; 185 186 cfg.ena_gpiod = devm_gpiod_get_optional(&pdev->dev, NULL, gflags); 187 if (IS_ERR(cfg.ena_gpiod)) 188 return PTR_ERR(cfg.ena_gpiod); 189 190 cfg.dev = &pdev->dev; 191 cfg.init_data = config->init_data; 192 cfg.driver_data = drvdata; 193 cfg.of_node = pdev->dev.of_node; 194 195 drvdata->dev = devm_regulator_register(&pdev->dev, &drvdata->desc, 196 &cfg); 197 if (IS_ERR(drvdata->dev)) { 198 ret = PTR_ERR(drvdata->dev); 199 dev_err(&pdev->dev, "Failed to register regulator: %d\n", ret); 200 return ret; 201 } 202 203 platform_set_drvdata(pdev, drvdata); 204 205 dev_dbg(&pdev->dev, "%s supplying %duV\n", drvdata->desc.name, 206 drvdata->desc.fixed_uV); 207 208 return 0; 209 } 210 211 #if defined(CONFIG_OF) 212 static const struct of_device_id fixed_of_match[] = { 213 { .compatible = "regulator-fixed", }, 214 {}, 215 }; 216 MODULE_DEVICE_TABLE(of, fixed_of_match); 217 #endif 218 219 static struct platform_driver regulator_fixed_voltage_driver = { 220 .probe = reg_fixed_voltage_probe, 221 .driver = { 222 .name = "reg-fixed-voltage", 223 .of_match_table = of_match_ptr(fixed_of_match), 224 }, 225 }; 226 227 static int __init regulator_fixed_voltage_init(void) 228 { 229 return platform_driver_register(®ulator_fixed_voltage_driver); 230 } 231 subsys_initcall(regulator_fixed_voltage_init); 232 233 static void __exit regulator_fixed_voltage_exit(void) 234 { 235 platform_driver_unregister(®ulator_fixed_voltage_driver); 236 } 237 module_exit(regulator_fixed_voltage_exit); 238 239 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>"); 240 MODULE_DESCRIPTION("Fixed voltage regulator"); 241 MODULE_LICENSE("GPL"); 242 MODULE_ALIAS("platform:reg-fixed-voltage"); 243