1 /* SPDX-License-Identifier: GPL-2.0-only */ 2 /* 3 * driver.h -- SoC Regulator driver support. 4 * 5 * Copyright (C) 2007, 2008 Wolfson Microelectronics PLC. 6 * 7 * Author: Liam Girdwood <lrg@slimlogic.co.uk> 8 * 9 * Regulator Driver Interface. 10 */ 11 12 #ifndef __LINUX_REGULATOR_DRIVER_H_ 13 #define __LINUX_REGULATOR_DRIVER_H_ 14 15 #include <linux/device.h> 16 #include <linux/linear_range.h> 17 #include <linux/notifier.h> 18 #include <linux/regulator/consumer.h> 19 #include <linux/ww_mutex.h> 20 21 struct gpio_desc; 22 struct regmap; 23 struct regulator_dev; 24 struct regulator_config; 25 struct regulator_init_data; 26 struct regulator_enable_gpio; 27 28 enum regulator_status { 29 REGULATOR_STATUS_OFF, 30 REGULATOR_STATUS_ON, 31 REGULATOR_STATUS_ERROR, 32 /* fast/normal/idle/standby are flavors of "on" */ 33 REGULATOR_STATUS_FAST, 34 REGULATOR_STATUS_NORMAL, 35 REGULATOR_STATUS_IDLE, 36 REGULATOR_STATUS_STANDBY, 37 /* The regulator is enabled but not regulating */ 38 REGULATOR_STATUS_BYPASS, 39 /* in case that any other status doesn't apply */ 40 REGULATOR_STATUS_UNDEFINED, 41 }; 42 43 /* Initialize struct linear_range for regulators */ 44 #define REGULATOR_LINEAR_RANGE(_min_uV, _min_sel, _max_sel, _step_uV) \ 45 { \ 46 .min = (_min_uV), .min_sel = (_min_sel), .max_sel = (_max_sel), .step = (_step_uV), \ 47 } 48 49 /** 50 * struct regulator_ops - regulator operations. 51 * 52 * @enable: Configure the regulator as enabled. 53 * @disable: Configure the regulator as disabled. 54 * @is_enabled: Return 1 if the regulator is enabled, 0 if not. 55 * May also return negative errno. 56 * 57 * @set_voltage: Set the voltage for the regulator within the range specified. 58 * The driver should select the voltage closest to min_uV. 59 * @set_voltage_sel: Set the voltage for the regulator using the specified 60 * selector. 61 * @map_voltage: Convert a voltage into a selector 62 * @get_voltage: Return the currently configured voltage for the regulator; 63 * return -ENOTRECOVERABLE if regulator can't be read at 64 * bootup and hasn't been set yet. 65 * @get_voltage_sel: Return the currently configured voltage selector for the 66 * regulator; return -ENOTRECOVERABLE if regulator can't 67 * be read at bootup and hasn't been set yet. 68 * @list_voltage: Return one of the supported voltages, in microvolts; zero 69 * if the selector indicates a voltage that is unusable on this system; 70 * or negative errno. Selectors range from zero to one less than 71 * regulator_desc.n_voltages. Voltages may be reported in any order. 72 * 73 * @set_current_limit: Configure a limit for a current-limited regulator. 74 * The driver should select the current closest to max_uA. 75 * @get_current_limit: Get the configured limit for a current-limited regulator. 76 * @set_input_current_limit: Configure an input limit. 77 * 78 * @set_over_current_protection: Support capability of automatically shutting 79 * down when detecting an over current event. 80 * 81 * @set_active_discharge: Set active discharge enable/disable of regulators. 82 * 83 * @set_mode: Set the configured operating mode for the regulator. 84 * @get_mode: Get the configured operating mode for the regulator. 85 * @get_error_flags: Get the current error(s) for the regulator. 86 * @get_status: Return actual (not as-configured) status of regulator, as a 87 * REGULATOR_STATUS value (or negative errno) 88 * @get_optimum_mode: Get the most efficient operating mode for the regulator 89 * when running with the specified parameters. 90 * @set_load: Set the load for the regulator. 91 * 92 * @set_bypass: Set the regulator in bypass mode. 93 * @get_bypass: Get the regulator bypass mode state. 94 * 95 * @enable_time: Time taken for the regulator voltage output voltage to 96 * stabilise after being enabled, in microseconds. 97 * @set_ramp_delay: Set the ramp delay for the regulator. The driver should 98 * select ramp delay equal to or less than(closest) ramp_delay. 99 * @set_voltage_time: Time taken for the regulator voltage output voltage 100 * to stabilise after being set to a new value, in microseconds. 101 * The function receives the from and to voltage as input, it 102 * should return the worst case. 103 * @set_voltage_time_sel: Time taken for the regulator voltage output voltage 104 * to stabilise after being set to a new value, in microseconds. 105 * The function receives the from and to voltage selector as 106 * input, it should return the worst case. 107 * @set_soft_start: Enable soft start for the regulator. 108 * 109 * @set_suspend_voltage: Set the voltage for the regulator when the system 110 * is suspended. 111 * @set_suspend_enable: Mark the regulator as enabled when the system is 112 * suspended. 113 * @set_suspend_disable: Mark the regulator as disabled when the system is 114 * suspended. 115 * @set_suspend_mode: Set the operating mode for the regulator when the 116 * system is suspended. 117 * @resume: Resume operation of suspended regulator. 118 * @set_pull_down: Configure the regulator to pull down when the regulator 119 * is disabled. 120 * 121 * This struct describes regulator operations which can be implemented by 122 * regulator chip drivers. 123 */ 124 struct regulator_ops { 125 /* enumerate supported voltages */ 126 int (*list_voltage)(struct regulator_dev *, unsigned selector); 127 128 /* get/set regulator voltage */ 129 int (*set_voltage)(struct regulator_dev *, int min_uV, int max_uV, unsigned *selector); 130 int (*map_voltage)(struct regulator_dev *, int min_uV, int max_uV); 131 int (*set_voltage_sel)(struct regulator_dev *, unsigned selector); 132 int (*get_voltage)(struct regulator_dev *); 133 int (*get_voltage_sel)(struct regulator_dev *); 134 135 /* get/set regulator current */ 136 int (*set_current_limit)(struct regulator_dev *, int min_uA, int max_uA); 137 int (*get_current_limit)(struct regulator_dev *); 138 139 int (*set_input_current_limit)(struct regulator_dev *, int lim_uA); 140 int (*set_over_current_protection)(struct regulator_dev *); 141 int (*set_active_discharge)(struct regulator_dev *, bool enable); 142 143 /* enable/disable regulator */ 144 int (*enable)(struct regulator_dev *); 145 int (*disable)(struct regulator_dev *); 146 int (*is_enabled)(struct regulator_dev *); 147 148 /* get/set regulator operating mode (defined in consumer.h) */ 149 int (*set_mode)(struct regulator_dev *, unsigned int mode); 150 unsigned int (*get_mode)(struct regulator_dev *); 151 152 /* retrieve current error flags on the regulator */ 153 int (*get_error_flags)(struct regulator_dev *, unsigned int *flags); 154 155 /* Time taken to enable or set voltage on the regulator */ 156 int (*enable_time)(struct regulator_dev *); 157 int (*set_ramp_delay)(struct regulator_dev *, int ramp_delay); 158 int (*set_voltage_time)(struct regulator_dev *, int old_uV, int new_uV); 159 int (*set_voltage_time_sel)(struct regulator_dev *, unsigned int old_selector, unsigned int new_selector); 160 161 int (*set_soft_start)(struct regulator_dev *); 162 163 /* report regulator status ... most other accessors report 164 * control inputs, this reports results of combining inputs 165 * from Linux (and other sources) with the actual load. 166 * returns REGULATOR_STATUS_* or negative errno. 167 */ 168 int (*get_status)(struct regulator_dev *); 169 170 /* get most efficient regulator operating mode for load */ 171 unsigned int (*get_optimum_mode)(struct regulator_dev *, int input_uV, int output_uV, int load_uA); 172 /* set the load on the regulator */ 173 int (*set_load)(struct regulator_dev *, int load_uA); 174 175 /* control and report on bypass mode */ 176 int (*set_bypass)(struct regulator_dev *dev, bool enable); 177 int (*get_bypass)(struct regulator_dev *dev, bool *enable); 178 179 /* the operations below are for configuration of regulator state when 180 * its parent PMIC enters a global STANDBY/HIBERNATE state */ 181 182 /* set regulator suspend voltage */ 183 int (*set_suspend_voltage)(struct regulator_dev *, int uV); 184 185 /* enable/disable regulator in suspend state */ 186 int (*set_suspend_enable)(struct regulator_dev *); 187 int (*set_suspend_disable)(struct regulator_dev *); 188 189 /* set regulator suspend operating mode (defined in consumer.h) */ 190 int (*set_suspend_mode)(struct regulator_dev *, unsigned int mode); 191 192 int (*resume)(struct regulator_dev *rdev); 193 194 int (*set_pull_down)(struct regulator_dev *); 195 }; 196 197 /* 198 * Regulators can either control voltage or current. 199 */ 200 enum regulator_type { 201 REGULATOR_VOLTAGE, 202 REGULATOR_CURRENT, 203 }; 204 205 /** 206 * struct regulator_desc - Static regulator descriptor 207 * 208 * Each regulator registered with the core is described with a 209 * structure of this type and a struct regulator_config. This 210 * structure contains the non-varying parts of the regulator 211 * description. 212 * 213 * @name: Identifying name for the regulator. 214 * @supply_name: Identifying the regulator supply 215 * @of_match: Name used to identify regulator in DT. 216 * @of_match_full_name: A flag to indicate that the of_match string, if 217 * present, should be matched against the node full_name. 218 * @regulators_node: Name of node containing regulator definitions in DT. 219 * @of_parse_cb: Optional callback called only if of_match is present. 220 * Will be called for each regulator parsed from DT, during 221 * init_data parsing. 222 * The regulator_config passed as argument to the callback will 223 * be a copy of config passed to regulator_register, valid only 224 * for this particular call. Callback may freely change the 225 * config but it cannot store it for later usage. 226 * Callback should return 0 on success or negative ERRNO 227 * indicating failure. 228 * @id: Numerical identifier for the regulator. 229 * @ops: Regulator operations table. 230 * @irq: Interrupt number for the regulator. 231 * @type: Indicates if the regulator is a voltage or current regulator. 232 * @owner: Module providing the regulator, used for refcounting. 233 * 234 * @continuous_voltage_range: Indicates if the regulator can set any 235 * voltage within constrains range. 236 * @n_voltages: Number of selectors available for ops.list_voltage(). 237 * @n_current_limits: Number of selectors available for current limits 238 * 239 * @min_uV: Voltage given by the lowest selector (if linear mapping) 240 * @uV_step: Voltage increase with each selector (if linear mapping) 241 * @linear_min_sel: Minimal selector for starting linear mapping 242 * @fixed_uV: Fixed voltage of rails. 243 * @ramp_delay: Time to settle down after voltage change (unit: uV/us) 244 * @min_dropout_uV: The minimum dropout voltage this regulator can handle 245 * @linear_ranges: A constant table of possible voltage ranges. 246 * @linear_range_selectors: A constant table of voltage range selectors. 247 * If pickable ranges are used each range must 248 * have corresponding selector here. 249 * @n_linear_ranges: Number of entries in the @linear_ranges (and in 250 * linear_range_selectors if used) table(s). 251 * @volt_table: Voltage mapping table (if table based mapping) 252 * @curr_table: Current limit mapping table (if table based mapping) 253 * 254 * @vsel_range_reg: Register for range selector when using pickable ranges 255 * and ``regulator_map_*_voltage_*_pickable`` functions. 256 * @vsel_range_mask: Mask for register bitfield used for range selector 257 * @vsel_reg: Register for selector when using ``regulator_map_*_voltage_*`` 258 * @vsel_mask: Mask for register bitfield used for selector 259 * @vsel_step: Specify the resolution of selector stepping when setting 260 * voltage. If 0, then no stepping is done (requested selector is 261 * set directly), if >0 then the regulator API will ramp the 262 * voltage up/down gradually each time increasing/decreasing the 263 * selector by the specified step value. 264 * @csel_reg: Register for current limit selector using regmap set_current_limit 265 * @csel_mask: Mask for register bitfield used for current limit selector 266 * @apply_reg: Register for initiate voltage change on the output when 267 * using regulator_set_voltage_sel_regmap 268 * @apply_bit: Register bitfield used for initiate voltage change on the 269 * output when using regulator_set_voltage_sel_regmap 270 * @enable_reg: Register for control when using regmap enable/disable ops 271 * @enable_mask: Mask for control when using regmap enable/disable ops 272 * @enable_val: Enabling value for control when using regmap enable/disable ops 273 * @disable_val: Disabling value for control when using regmap enable/disable ops 274 * @enable_is_inverted: A flag to indicate set enable_mask bits to disable 275 * when using regulator_enable_regmap and friends APIs. 276 * @bypass_reg: Register for control when using regmap set_bypass 277 * @bypass_mask: Mask for control when using regmap set_bypass 278 * @bypass_val_on: Enabling value for control when using regmap set_bypass 279 * @bypass_val_off: Disabling value for control when using regmap set_bypass 280 * @active_discharge_off: Enabling value for control when using regmap 281 * set_active_discharge 282 * @active_discharge_on: Disabling value for control when using regmap 283 * set_active_discharge 284 * @active_discharge_mask: Mask for control when using regmap 285 * set_active_discharge 286 * @active_discharge_reg: Register for control when using regmap 287 * set_active_discharge 288 * @soft_start_reg: Register for control when using regmap set_soft_start 289 * @soft_start_mask: Mask for control when using regmap set_soft_start 290 * @soft_start_val_on: Enabling value for control when using regmap 291 * set_soft_start 292 * @pull_down_reg: Register for control when using regmap set_pull_down 293 * @pull_down_mask: Mask for control when using regmap set_pull_down 294 * @pull_down_val_on: Enabling value for control when using regmap 295 * set_pull_down 296 * 297 * @enable_time: Time taken for initial enable of regulator (in uS). 298 * @off_on_delay: guard time (in uS), before re-enabling a regulator 299 * 300 * @poll_enabled_time: The polling interval (in uS) to use while checking that 301 * the regulator was actually enabled. Max upto enable_time. 302 * 303 * @of_map_mode: Maps a hardware mode defined in a DeviceTree to a standard mode 304 */ 305 struct regulator_desc { 306 const char *name; 307 const char *supply_name; 308 const char *of_match; 309 bool of_match_full_name; 310 const char *regulators_node; 311 int (*of_parse_cb)(struct device_node *, const struct regulator_desc *, struct regulator_config *); 312 int id; 313 unsigned int continuous_voltage_range : 1; 314 unsigned n_voltages; 315 unsigned int n_current_limits; 316 const struct regulator_ops *ops; 317 int irq; 318 enum regulator_type type; 319 struct module *owner; 320 321 unsigned int min_uV; 322 unsigned int uV_step; 323 unsigned int linear_min_sel; 324 int fixed_uV; 325 unsigned int ramp_delay; 326 int min_dropout_uV; 327 328 const struct linear_range *linear_ranges; 329 const unsigned int *linear_range_selectors; 330 331 int n_linear_ranges; 332 333 const unsigned int *volt_table; 334 const unsigned int *curr_table; 335 336 unsigned int vsel_range_reg; 337 unsigned int vsel_range_mask; 338 unsigned int vsel_reg; 339 unsigned int vsel_mask; 340 unsigned int vsel_step; 341 unsigned int csel_reg; 342 unsigned int csel_mask; 343 unsigned int apply_reg; 344 unsigned int apply_bit; 345 unsigned int enable_reg; 346 unsigned int enable_mask; 347 unsigned int enable_val; 348 unsigned int disable_val; 349 bool enable_is_inverted; 350 unsigned int bypass_reg; 351 unsigned int bypass_mask; 352 unsigned int bypass_val_on; 353 unsigned int bypass_val_off; 354 unsigned int active_discharge_on; 355 unsigned int active_discharge_off; 356 unsigned int active_discharge_mask; 357 unsigned int active_discharge_reg; 358 unsigned int soft_start_reg; 359 unsigned int soft_start_mask; 360 unsigned int soft_start_val_on; 361 unsigned int pull_down_reg; 362 unsigned int pull_down_mask; 363 unsigned int pull_down_val_on; 364 365 unsigned int enable_time; 366 367 unsigned int off_on_delay; 368 369 unsigned int poll_enabled_time; 370 371 unsigned int (*of_map_mode)(unsigned int mode); 372 }; 373 374 /** 375 * struct regulator_config - Dynamic regulator descriptor 376 * 377 * Each regulator registered with the core is described with a 378 * structure of this type and a struct regulator_desc. This structure 379 * contains the runtime variable parts of the regulator description. 380 * 381 * @dev: struct device for the regulator 382 * @init_data: platform provided init data, passed through by driver 383 * @driver_data: private regulator data 384 * @of_node: OpenFirmware node to parse for device tree bindings (may be 385 * NULL). 386 * @regmap: regmap to use for core regmap helpers if dev_get_regmap() is 387 * insufficient. 388 * @ena_gpiod: GPIO controlling regulator enable. 389 */ 390 struct regulator_config { 391 struct device *dev; 392 const struct regulator_init_data *init_data; 393 void *driver_data; 394 struct device_node *of_node; 395 struct regmap *regmap; 396 397 struct gpio_desc *ena_gpiod; 398 }; 399 400 /* 401 * struct coupling_desc 402 * 403 * Describes coupling of regulators. Each regulator should have 404 * at least a pointer to itself in coupled_rdevs array. 405 * When a new coupled regulator is resolved, n_resolved is 406 * incremented. 407 */ 408 struct coupling_desc { 409 struct regulator_dev **coupled_rdevs; 410 struct regulator_coupler *coupler; 411 int n_resolved; 412 int n_coupled; 413 }; 414 415 /* 416 * struct regulator_dev 417 * 418 * Voltage / Current regulator class device. One for each 419 * regulator. 420 * 421 * This should *not* be used directly by anything except the regulator 422 * core and notification injection (which should take the mutex and do 423 * no other direct access). 424 */ 425 struct regulator_dev { 426 const struct regulator_desc *desc; 427 int exclusive; 428 u32 use_count; 429 u32 open_count; 430 u32 bypass_count; 431 432 /* lists we belong to */ 433 struct list_head list; /* list of all regulators */ 434 435 /* lists we own */ 436 struct list_head consumer_list; /* consumers we supply */ 437 438 struct coupling_desc coupling_desc; 439 440 struct blocking_notifier_head notifier; 441 struct ww_mutex mutex; /* consumer lock */ 442 struct task_struct *mutex_owner; 443 int ref_cnt; 444 struct module *owner; 445 struct device dev; 446 struct regulation_constraints *constraints; 447 struct regulator *supply; /* for tree */ 448 const char *supply_name; 449 struct regmap *regmap; 450 451 struct delayed_work disable_work; 452 453 void *reg_data; /* regulator_dev data */ 454 455 struct dentry *debugfs; 456 457 struct regulator_enable_gpio *ena_pin; 458 unsigned int ena_gpio_state : 1; 459 460 unsigned int is_switch : 1; 461 462 /* time when this regulator was disabled last time */ 463 unsigned long last_off_jiffy; 464 }; 465 466 struct regulator_dev *regulator_register(const struct regulator_desc *regulator_desc, 467 const struct regulator_config *config); 468 struct regulator_dev *devm_regulator_register(struct device *dev, const struct regulator_desc *regulator_desc, 469 const struct regulator_config *config); 470 void regulator_unregister(struct regulator_dev *rdev); 471 void devm_regulator_unregister(struct device *dev, struct regulator_dev *rdev); 472 473 int regulator_notifier_call_chain(struct regulator_dev *rdev, unsigned long event, void *data); 474 475 void *rdev_get_drvdata(struct regulator_dev *rdev); 476 struct device *rdev_get_dev(struct regulator_dev *rdev); 477 struct regmap *rdev_get_regmap(struct regulator_dev *rdev); 478 int rdev_get_id(struct regulator_dev *rdev); 479 480 int regulator_mode_to_status(unsigned int); 481 482 int regulator_list_voltage_linear(struct regulator_dev *rdev, unsigned int selector); 483 int regulator_list_voltage_pickable_linear_range(struct regulator_dev *rdev, unsigned int selector); 484 int regulator_list_voltage_linear_range(struct regulator_dev *rdev, unsigned int selector); 485 int regulator_list_voltage_table(struct regulator_dev *rdev, unsigned int selector); 486 int regulator_map_voltage_linear(struct regulator_dev *rdev, int min_uV, int max_uV); 487 int regulator_map_voltage_pickable_linear_range(struct regulator_dev *rdev, int min_uV, int max_uV); 488 int regulator_map_voltage_linear_range(struct regulator_dev *rdev, int min_uV, int max_uV); 489 int regulator_map_voltage_iterate(struct regulator_dev *rdev, int min_uV, int max_uV); 490 int regulator_map_voltage_ascend(struct regulator_dev *rdev, int min_uV, int max_uV); 491 int regulator_get_voltage_sel_pickable_regmap(struct regulator_dev *rdev); 492 int regulator_set_voltage_sel_pickable_regmap(struct regulator_dev *rdev, unsigned int sel); 493 int regulator_get_voltage_sel_regmap(struct regulator_dev *rdev); 494 int regulator_set_voltage_sel_regmap(struct regulator_dev *rdev, unsigned sel); 495 int regulator_is_enabled_regmap(struct regulator_dev *rdev); 496 int regulator_enable_regmap(struct regulator_dev *rdev); 497 int regulator_disable_regmap(struct regulator_dev *rdev); 498 int regulator_set_voltage_time_sel(struct regulator_dev *rdev, unsigned int old_selector, unsigned int new_selector); 499 int regulator_set_bypass_regmap(struct regulator_dev *rdev, bool enable); 500 int regulator_get_bypass_regmap(struct regulator_dev *rdev, bool *enable); 501 int regulator_set_soft_start_regmap(struct regulator_dev *rdev); 502 int regulator_set_pull_down_regmap(struct regulator_dev *rdev); 503 504 int regulator_set_active_discharge_regmap(struct regulator_dev *rdev, bool enable); 505 int regulator_set_current_limit_regmap(struct regulator_dev *rdev, int min_uA, int max_uA); 506 int regulator_get_current_limit_regmap(struct regulator_dev *rdev); 507 void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data); 508 509 /* 510 * Helper functions intended to be used by regulator drivers prior registering 511 * their regulators. 512 */ 513 int regulator_desc_list_voltage_linear_range(const struct regulator_desc *desc, unsigned int selector); 514 515 #endif 516