Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0-only */
0002 /*
0003  * driver.h -- SoC Regulator driver support.
0004  *
0005  * Copyright (C) 2007, 2008 Wolfson Microelectronics PLC.
0006  *
0007  * Author: Liam Girdwood <lrg@slimlogic.co.uk>
0008  *
0009  * Regulator Driver Interface.
0010  */
0011 
0012 #ifndef __LINUX_REGULATOR_DRIVER_H_
0013 #define __LINUX_REGULATOR_DRIVER_H_
0014 
0015 #include <linux/device.h>
0016 #include <linux/linear_range.h>
0017 #include <linux/notifier.h>
0018 #include <linux/regulator/consumer.h>
0019 #include <linux/ww_mutex.h>
0020 
0021 struct gpio_desc;
0022 struct regmap;
0023 struct regulator_dev;
0024 struct regulator_config;
0025 struct regulator_init_data;
0026 struct regulator_enable_gpio;
0027 
0028 enum regulator_status {
0029     REGULATOR_STATUS_OFF,
0030     REGULATOR_STATUS_ON,
0031     REGULATOR_STATUS_ERROR,
0032     /* fast/normal/idle/standby are flavors of "on" */
0033     REGULATOR_STATUS_FAST,
0034     REGULATOR_STATUS_NORMAL,
0035     REGULATOR_STATUS_IDLE,
0036     REGULATOR_STATUS_STANDBY,
0037     /* The regulator is enabled but not regulating */
0038     REGULATOR_STATUS_BYPASS,
0039     /* in case that any other status doesn't apply */
0040     REGULATOR_STATUS_UNDEFINED,
0041 };
0042 
0043 enum regulator_detection_severity {
0044     /* Hardware shut down voltage outputs if condition is detected */
0045     REGULATOR_SEVERITY_PROT,
0046     /* Hardware is probably damaged/inoperable */
0047     REGULATOR_SEVERITY_ERR,
0048     /* Hardware is still recoverable but recovery action must be taken */
0049     REGULATOR_SEVERITY_WARN,
0050 };
0051 
0052 /* Initialize struct linear_range for regulators */
0053 #define REGULATOR_LINEAR_RANGE(_min_uV, _min_sel, _max_sel, _step_uV)   \
0054 {                                   \
0055     .min        = _min_uV,                  \
0056     .min_sel    = _min_sel,                 \
0057     .max_sel    = _max_sel,                 \
0058     .step       = _step_uV,                 \
0059 }
0060 
0061 /**
0062  * struct regulator_ops - regulator operations.
0063  *
0064  * @enable: Configure the regulator as enabled.
0065  * @disable: Configure the regulator as disabled.
0066  * @is_enabled: Return 1 if the regulator is enabled, 0 if not.
0067  *      May also return negative errno.
0068  *
0069  * @set_voltage: Set the voltage for the regulator within the range specified.
0070  *               The driver should select the voltage closest to min_uV.
0071  * @set_voltage_sel: Set the voltage for the regulator using the specified
0072  *                   selector.
0073  * @map_voltage: Convert a voltage into a selector
0074  * @get_voltage: Return the currently configured voltage for the regulator;
0075  *                   return -ENOTRECOVERABLE if regulator can't be read at
0076  *                   bootup and hasn't been set yet.
0077  * @get_voltage_sel: Return the currently configured voltage selector for the
0078  *                   regulator; return -ENOTRECOVERABLE if regulator can't
0079  *                   be read at bootup and hasn't been set yet.
0080  * @list_voltage: Return one of the supported voltages, in microvolts; zero
0081  *  if the selector indicates a voltage that is unusable on this system;
0082  *  or negative errno.  Selectors range from zero to one less than
0083  *  regulator_desc.n_voltages.  Voltages may be reported in any order.
0084  *
0085  * @set_current_limit: Configure a limit for a current-limited regulator.
0086  *                     The driver should select the current closest to max_uA.
0087  * @get_current_limit: Get the configured limit for a current-limited regulator.
0088  * @set_input_current_limit: Configure an input limit.
0089  *
0090  * @set_over_current_protection: Support enabling of and setting limits for over
0091  *  current situation detection. Detection can be configured for three
0092  *  levels of severity.
0093  *
0094  *  - REGULATOR_SEVERITY_PROT should automatically shut down the regulator(s).
0095  *
0096  *  - REGULATOR_SEVERITY_ERR should indicate that over-current situation is
0097  *        caused by an unrecoverable error but HW does not perform
0098  *        automatic shut down.
0099  *
0100  *  - REGULATOR_SEVERITY_WARN should indicate situation where hardware is
0101  *        still believed to not be damaged but that a board sepcific
0102  *        recovery action is needed. If lim_uA is 0 the limit should not
0103  *        be changed but the detection should just be enabled/disabled as
0104  *        is requested.
0105  *
0106  * @set_over_voltage_protection: Support enabling of and setting limits for over
0107  *  voltage situation detection. Detection can be configured for same
0108  *  severities as over current protection. Units of uV.
0109  * @set_under_voltage_protection: Support enabling of and setting limits for
0110  *  under voltage situation detection. Detection can be configured for same
0111  *  severities as over current protection. Units of uV.
0112  * @set_thermal_protection: Support enabling of and setting limits for over
0113  *  temperature situation detection.Detection can be configured for same
0114  *  severities as over current protection. Units of degree Kelvin.
0115  *
0116  * @set_active_discharge: Set active discharge enable/disable of regulators.
0117  *
0118  * @set_mode: Set the configured operating mode for the regulator.
0119  * @get_mode: Get the configured operating mode for the regulator.
0120  * @get_error_flags: Get the current error(s) for the regulator.
0121  * @get_status: Return actual (not as-configured) status of regulator, as a
0122  *  REGULATOR_STATUS value (or negative errno)
0123  * @get_optimum_mode: Get the most efficient operating mode for the regulator
0124  *                    when running with the specified parameters.
0125  * @set_load: Set the load for the regulator.
0126  *
0127  * @set_bypass: Set the regulator in bypass mode.
0128  * @get_bypass: Get the regulator bypass mode state.
0129  *
0130  * @enable_time: Time taken for the regulator voltage output voltage to
0131  *               stabilise after being enabled, in microseconds.
0132  * @set_ramp_delay: Set the ramp delay for the regulator. The driver should
0133  *      select ramp delay equal to or less than(closest) ramp_delay.
0134  * @set_voltage_time: Time taken for the regulator voltage output voltage
0135  *               to stabilise after being set to a new value, in microseconds.
0136  *               The function receives the from and to voltage as input, it
0137  *               should return the worst case.
0138  * @set_voltage_time_sel: Time taken for the regulator voltage output voltage
0139  *               to stabilise after being set to a new value, in microseconds.
0140  *               The function receives the from and to voltage selector as
0141  *               input, it should return the worst case.
0142  * @set_soft_start: Enable soft start for the regulator.
0143  *
0144  * @set_suspend_voltage: Set the voltage for the regulator when the system
0145  *                       is suspended.
0146  * @set_suspend_enable: Mark the regulator as enabled when the system is
0147  *                      suspended.
0148  * @set_suspend_disable: Mark the regulator as disabled when the system is
0149  *                       suspended.
0150  * @set_suspend_mode: Set the operating mode for the regulator when the
0151  *                    system is suspended.
0152  * @resume: Resume operation of suspended regulator.
0153  * @set_pull_down: Configure the regulator to pull down when the regulator
0154  *         is disabled.
0155  *
0156  * This struct describes regulator operations which can be implemented by
0157  * regulator chip drivers.
0158  */
0159 struct regulator_ops {
0160 
0161     /* enumerate supported voltages */
0162     int (*list_voltage) (struct regulator_dev *, unsigned selector);
0163 
0164     /* get/set regulator voltage */
0165     int (*set_voltage) (struct regulator_dev *, int min_uV, int max_uV,
0166                 unsigned *selector);
0167     int (*map_voltage)(struct regulator_dev *, int min_uV, int max_uV);
0168     int (*set_voltage_sel) (struct regulator_dev *, unsigned selector);
0169     int (*get_voltage) (struct regulator_dev *);
0170     int (*get_voltage_sel) (struct regulator_dev *);
0171 
0172     /* get/set regulator current  */
0173     int (*set_current_limit) (struct regulator_dev *,
0174                  int min_uA, int max_uA);
0175     int (*get_current_limit) (struct regulator_dev *);
0176 
0177     int (*set_input_current_limit) (struct regulator_dev *, int lim_uA);
0178     int (*set_over_current_protection)(struct regulator_dev *, int lim_uA,
0179                        int severity, bool enable);
0180     int (*set_over_voltage_protection)(struct regulator_dev *, int lim_uV,
0181                        int severity, bool enable);
0182     int (*set_under_voltage_protection)(struct regulator_dev *, int lim_uV,
0183                         int severity, bool enable);
0184     int (*set_thermal_protection)(struct regulator_dev *, int lim,
0185                       int severity, bool enable);
0186     int (*set_active_discharge)(struct regulator_dev *, bool enable);
0187 
0188     /* enable/disable regulator */
0189     int (*enable) (struct regulator_dev *);
0190     int (*disable) (struct regulator_dev *);
0191     int (*is_enabled) (struct regulator_dev *);
0192 
0193     /* get/set regulator operating mode (defined in consumer.h) */
0194     int (*set_mode) (struct regulator_dev *, unsigned int mode);
0195     unsigned int (*get_mode) (struct regulator_dev *);
0196 
0197     /* retrieve current error flags on the regulator */
0198     int (*get_error_flags)(struct regulator_dev *, unsigned int *flags);
0199 
0200     /* Time taken to enable or set voltage on the regulator */
0201     int (*enable_time) (struct regulator_dev *);
0202     int (*set_ramp_delay) (struct regulator_dev *, int ramp_delay);
0203     int (*set_voltage_time) (struct regulator_dev *, int old_uV,
0204                  int new_uV);
0205     int (*set_voltage_time_sel) (struct regulator_dev *,
0206                      unsigned int old_selector,
0207                      unsigned int new_selector);
0208 
0209     int (*set_soft_start) (struct regulator_dev *);
0210 
0211     /* report regulator status ... most other accessors report
0212      * control inputs, this reports results of combining inputs
0213      * from Linux (and other sources) with the actual load.
0214      * returns REGULATOR_STATUS_* or negative errno.
0215      */
0216     int (*get_status)(struct regulator_dev *);
0217 
0218     /* get most efficient regulator operating mode for load */
0219     unsigned int (*get_optimum_mode) (struct regulator_dev *, int input_uV,
0220                       int output_uV, int load_uA);
0221     /* set the load on the regulator */
0222     int (*set_load)(struct regulator_dev *, int load_uA);
0223 
0224     /* control and report on bypass mode */
0225     int (*set_bypass)(struct regulator_dev *dev, bool enable);
0226     int (*get_bypass)(struct regulator_dev *dev, bool *enable);
0227 
0228     /* the operations below are for configuration of regulator state when
0229      * its parent PMIC enters a global STANDBY/HIBERNATE state */
0230 
0231     /* set regulator suspend voltage */
0232     int (*set_suspend_voltage) (struct regulator_dev *, int uV);
0233 
0234     /* enable/disable regulator in suspend state */
0235     int (*set_suspend_enable) (struct regulator_dev *);
0236     int (*set_suspend_disable) (struct regulator_dev *);
0237 
0238     /* set regulator suspend operating mode (defined in consumer.h) */
0239     int (*set_suspend_mode) (struct regulator_dev *, unsigned int mode);
0240 
0241     int (*resume)(struct regulator_dev *rdev);
0242 
0243     int (*set_pull_down) (struct regulator_dev *);
0244 };
0245 
0246 /*
0247  * Regulators can either control voltage or current.
0248  */
0249 enum regulator_type {
0250     REGULATOR_VOLTAGE,
0251     REGULATOR_CURRENT,
0252 };
0253 
0254 /**
0255  * struct regulator_desc - Static regulator descriptor
0256  *
0257  * Each regulator registered with the core is described with a
0258  * structure of this type and a struct regulator_config.  This
0259  * structure contains the non-varying parts of the regulator
0260  * description.
0261  *
0262  * @name: Identifying name for the regulator.
0263  * @supply_name: Identifying the regulator supply
0264  * @of_match: Name used to identify regulator in DT.
0265  * @of_match_full_name: A flag to indicate that the of_match string, if
0266  *          present, should be matched against the node full_name.
0267  * @regulators_node: Name of node containing regulator definitions in DT.
0268  * @of_parse_cb: Optional callback called only if of_match is present.
0269  *               Will be called for each regulator parsed from DT, during
0270  *               init_data parsing.
0271  *               The regulator_config passed as argument to the callback will
0272  *               be a copy of config passed to regulator_register, valid only
0273  *               for this particular call. Callback may freely change the
0274  *               config but it cannot store it for later usage.
0275  *               Callback should return 0 on success or negative ERRNO
0276  *               indicating failure.
0277  * @id: Numerical identifier for the regulator.
0278  * @ops: Regulator operations table.
0279  * @irq: Interrupt number for the regulator.
0280  * @type: Indicates if the regulator is a voltage or current regulator.
0281  * @owner: Module providing the regulator, used for refcounting.
0282  *
0283  * @continuous_voltage_range: Indicates if the regulator can set any
0284  *                            voltage within constrains range.
0285  * @n_voltages: Number of selectors available for ops.list_voltage().
0286  * @n_current_limits: Number of selectors available for current limits
0287  *
0288  * @min_uV: Voltage given by the lowest selector (if linear mapping)
0289  * @uV_step: Voltage increase with each selector (if linear mapping)
0290  * @linear_min_sel: Minimal selector for starting linear mapping
0291  * @fixed_uV: Fixed voltage of rails.
0292  * @ramp_delay: Time to settle down after voltage change (unit: uV/us)
0293  * @min_dropout_uV: The minimum dropout voltage this regulator can handle
0294  * @linear_ranges: A constant table of possible voltage ranges.
0295  * @linear_range_selectors: A constant table of voltage range selectors.
0296  *              If pickable ranges are used each range must
0297  *              have corresponding selector here.
0298  * @n_linear_ranges: Number of entries in the @linear_ranges (and in
0299  *           linear_range_selectors if used) table(s).
0300  * @volt_table: Voltage mapping table (if table based mapping)
0301  * @curr_table: Current limit mapping table (if table based mapping)
0302  *
0303  * @vsel_range_reg: Register for range selector when using pickable ranges
0304  *          and ``regulator_map_*_voltage_*_pickable`` functions.
0305  * @vsel_range_mask: Mask for register bitfield used for range selector
0306  * @vsel_reg: Register for selector when using ``regulator_map_*_voltage_*``
0307  * @vsel_mask: Mask for register bitfield used for selector
0308  * @vsel_step: Specify the resolution of selector stepping when setting
0309  *         voltage. If 0, then no stepping is done (requested selector is
0310  *         set directly), if >0 then the regulator API will ramp the
0311  *         voltage up/down gradually each time increasing/decreasing the
0312  *         selector by the specified step value.
0313  * @csel_reg: Register for current limit selector using regmap set_current_limit
0314  * @csel_mask: Mask for register bitfield used for current limit selector
0315  * @apply_reg: Register for initiate voltage change on the output when
0316  *                using regulator_set_voltage_sel_regmap
0317  * @apply_bit: Register bitfield used for initiate voltage change on the
0318  *                output when using regulator_set_voltage_sel_regmap
0319  * @enable_reg: Register for control when using regmap enable/disable ops
0320  * @enable_mask: Mask for control when using regmap enable/disable ops
0321  * @enable_val: Enabling value for control when using regmap enable/disable ops
0322  * @disable_val: Disabling value for control when using regmap enable/disable ops
0323  * @enable_is_inverted: A flag to indicate set enable_mask bits to disable
0324  *                      when using regulator_enable_regmap and friends APIs.
0325  * @bypass_reg: Register for control when using regmap set_bypass
0326  * @bypass_mask: Mask for control when using regmap set_bypass
0327  * @bypass_val_on: Enabling value for control when using regmap set_bypass
0328  * @bypass_val_off: Disabling value for control when using regmap set_bypass
0329  * @active_discharge_off: Enabling value for control when using regmap
0330  *            set_active_discharge
0331  * @active_discharge_on: Disabling value for control when using regmap
0332  *           set_active_discharge
0333  * @active_discharge_mask: Mask for control when using regmap
0334  *             set_active_discharge
0335  * @active_discharge_reg: Register for control when using regmap
0336  *            set_active_discharge
0337  * @soft_start_reg: Register for control when using regmap set_soft_start
0338  * @soft_start_mask: Mask for control when using regmap set_soft_start
0339  * @soft_start_val_on: Enabling value for control when using regmap
0340  *                     set_soft_start
0341  * @pull_down_reg: Register for control when using regmap set_pull_down
0342  * @pull_down_mask: Mask for control when using regmap set_pull_down
0343  * @pull_down_val_on: Enabling value for control when using regmap
0344  *                     set_pull_down
0345  *
0346  * @ramp_reg:       Register for controlling the regulator ramp-rate.
0347  * @ramp_mask:      Bitmask for the ramp-rate control register.
0348  * @ramp_delay_table:   Table for mapping the regulator ramp-rate values. Values
0349  *          should be given in units of V/S (uV/uS). See the
0350  *          regulator_set_ramp_delay_regmap().
0351  * @n_ramp_values:  number of elements at @ramp_delay_table.
0352  *
0353  * @enable_time: Time taken for initial enable of regulator (in uS).
0354  * @off_on_delay: guard time (in uS), before re-enabling a regulator
0355  *
0356  * @poll_enabled_time: The polling interval (in uS) to use while checking that
0357  *                     the regulator was actually enabled. Max upto enable_time.
0358  *
0359  * @of_map_mode: Maps a hardware mode defined in a DeviceTree to a standard mode
0360  */
0361 struct regulator_desc {
0362     const char *name;
0363     const char *supply_name;
0364     const char *of_match;
0365     bool of_match_full_name;
0366     const char *regulators_node;
0367     int (*of_parse_cb)(struct device_node *,
0368                 const struct regulator_desc *,
0369                 struct regulator_config *);
0370     int id;
0371     unsigned int continuous_voltage_range:1;
0372     unsigned n_voltages;
0373     unsigned int n_current_limits;
0374     const struct regulator_ops *ops;
0375     int irq;
0376     enum regulator_type type;
0377     struct module *owner;
0378 
0379     unsigned int min_uV;
0380     unsigned int uV_step;
0381     unsigned int linear_min_sel;
0382     int fixed_uV;
0383     unsigned int ramp_delay;
0384     int min_dropout_uV;
0385 
0386     const struct linear_range *linear_ranges;
0387     const unsigned int *linear_range_selectors;
0388 
0389     int n_linear_ranges;
0390 
0391     const unsigned int *volt_table;
0392     const unsigned int *curr_table;
0393 
0394     unsigned int vsel_range_reg;
0395     unsigned int vsel_range_mask;
0396     unsigned int vsel_reg;
0397     unsigned int vsel_mask;
0398     unsigned int vsel_step;
0399     unsigned int csel_reg;
0400     unsigned int csel_mask;
0401     unsigned int apply_reg;
0402     unsigned int apply_bit;
0403     unsigned int enable_reg;
0404     unsigned int enable_mask;
0405     unsigned int enable_val;
0406     unsigned int disable_val;
0407     bool enable_is_inverted;
0408     unsigned int bypass_reg;
0409     unsigned int bypass_mask;
0410     unsigned int bypass_val_on;
0411     unsigned int bypass_val_off;
0412     unsigned int active_discharge_on;
0413     unsigned int active_discharge_off;
0414     unsigned int active_discharge_mask;
0415     unsigned int active_discharge_reg;
0416     unsigned int soft_start_reg;
0417     unsigned int soft_start_mask;
0418     unsigned int soft_start_val_on;
0419     unsigned int pull_down_reg;
0420     unsigned int pull_down_mask;
0421     unsigned int pull_down_val_on;
0422     unsigned int ramp_reg;
0423     unsigned int ramp_mask;
0424     const unsigned int *ramp_delay_table;
0425     unsigned int n_ramp_values;
0426 
0427     unsigned int enable_time;
0428 
0429     unsigned int off_on_delay;
0430 
0431     unsigned int poll_enabled_time;
0432 
0433     unsigned int (*of_map_mode)(unsigned int mode);
0434 };
0435 
0436 /**
0437  * struct regulator_config - Dynamic regulator descriptor
0438  *
0439  * Each regulator registered with the core is described with a
0440  * structure of this type and a struct regulator_desc.  This structure
0441  * contains the runtime variable parts of the regulator description.
0442  *
0443  * @dev: struct device for the regulator
0444  * @init_data: platform provided init data, passed through by driver
0445  * @driver_data: private regulator data
0446  * @of_node: OpenFirmware node to parse for device tree bindings (may be
0447  *           NULL).
0448  * @regmap: regmap to use for core regmap helpers if dev_get_regmap() is
0449  *          insufficient.
0450  * @ena_gpiod: GPIO controlling regulator enable.
0451  */
0452 struct regulator_config {
0453     struct device *dev;
0454     const struct regulator_init_data *init_data;
0455     void *driver_data;
0456     struct device_node *of_node;
0457     struct regmap *regmap;
0458 
0459     struct gpio_desc *ena_gpiod;
0460 };
0461 
0462 /**
0463  * struct regulator_err_state - regulator error/notification status
0464  *
0465  * @rdev:       Regulator which status the struct indicates.
0466  * @notifs:     Events which have occurred on the regulator.
0467  * @errors:     Errors which are active on the regulator.
0468  * @possible_errs:  Errors which can be signaled (by given IRQ).
0469  */
0470 struct regulator_err_state {
0471     struct regulator_dev *rdev;
0472     unsigned long notifs;
0473     unsigned long errors;
0474     int possible_errs;
0475 };
0476 
0477 /**
0478  * struct regulator_irq_data - regulator error/notification status data
0479  *
0480  * @states: Status structs for each of the associated regulators.
0481  * @num_states: Amount of associated regulators.
0482  * @data:   Driver data pointer given at regulator_irq_desc.
0483  * @opaque: Value storage for IC driver. Core does not update this. ICs
0484  *      may want to store status register value here at map_event and
0485  *      compare contents at 'renable' callback to see if new problems
0486  *      have been added to status. If that is the case it may be
0487  *      desirable to return REGULATOR_ERROR_CLEARED and not
0488  *      REGULATOR_ERROR_ON to allow IRQ fire again and to generate
0489  *      notifications also for the new issues.
0490  *
0491  * This structure is passed to 'map_event' and 'renable' callbacks for
0492  * reporting regulator status to core.
0493  */
0494 struct regulator_irq_data {
0495     struct regulator_err_state *states;
0496     int num_states;
0497     void *data;
0498     long opaque;
0499 };
0500 
0501 /**
0502  * struct regulator_irq_desc - notification sender for IRQ based events.
0503  *
0504  * @name:   The visible name for the IRQ
0505  * @fatal_cnt:  If this IRQ is used to signal HW damaging condition it may be
0506  *      best to shut-down regulator(s) or reboot the SOC if error
0507  *      handling is repeatedly failing. If fatal_cnt is given the IRQ
0508  *      handling is aborted if it fails for fatal_cnt times and die()
0509  *      callback (if populated) is called. If die() is not populated
0510  *      poweroff for the system is attempted in order to prevent any
0511  *      further damage.
0512  * @reread_ms:  The time which is waited before attempting to re-read status
0513  *      at the worker if IC reading fails. Immediate re-read is done
0514  *      if time is not specified.
0515  * @irq_off_ms: The time which IRQ is kept disabled before re-evaluating the
0516  *      status for devices which keep IRQ disabled for duration of the
0517  *      error. If this is not given the IRQ is left enabled and renable
0518  *      is not called.
0519  * @skip_off:   If set to true the IRQ handler will attempt to check if any of
0520  *      the associated regulators are enabled prior to taking other
0521  *      actions. If no regulators are enabled and this is set to true
0522  *      a spurious IRQ is assumed and IRQ_NONE is returned.
0523  * @high_prio:  Boolean to indicate that high priority WQ should be used.
0524  * @data:   Driver private data pointer which will be passed as such to
0525  *      the renable, map_event and die callbacks in regulator_irq_data.
0526  * @die:    Protection callback. If IC status reading or recovery actions
0527  *      fail fatal_cnt times this callback is called or system is
0528  *      powered off. This callback should implement a final protection
0529  *      attempt like disabling the regulator. If protection succeeded
0530  *      die() may return 0. If anything else is returned the core
0531  *      assumes final protection failed and attempts to perform a
0532  *      poweroff as a last resort.
0533  * @map_event:  Driver callback to map IRQ status into regulator devices with
0534  *      events / errors. NOTE: callback MUST initialize both the
0535  *      errors and notifs for all rdevs which it signals having
0536  *      active events as core does not clean the map data.
0537  *      REGULATOR_FAILED_RETRY can be returned to indicate that the
0538  *      status reading from IC failed. If this is repeated for
0539  *      fatal_cnt times the core will call die() callback or power-off
0540  *      the system as a last resort to protect the HW.
0541  * @renable:    Optional callback to check status (if HW supports that) before
0542  *      re-enabling IRQ. If implemented this should clear the error
0543  *      flags so that errors fetched by regulator_get_error_flags()
0544  *      are updated. If callback is not implemented then errors are
0545  *      assumed to be cleared and IRQ is re-enabled.
0546  *      REGULATOR_FAILED_RETRY can be returned to
0547  *      indicate that the status reading from IC failed. If this is
0548  *      repeated for 'fatal_cnt' times the core will call die()
0549  *      callback or if die() is not populated then attempt to power-off
0550  *      the system as a last resort to protect the HW.
0551  *      Returning zero indicates that the problem in HW has been solved
0552  *      and IRQ will be re-enabled. Returning REGULATOR_ERROR_ON
0553  *      indicates the error condition is still active and keeps IRQ
0554  *      disabled. Please note that returning REGULATOR_ERROR_ON does
0555  *      not retrigger evaluating what events are active or resending
0556  *      notifications. If this is needed you probably want to return
0557  *      zero and allow IRQ to retrigger causing events to be
0558  *      re-evaluated and re-sent.
0559  *
0560  * This structure is used for registering regulator IRQ notification helper.
0561  */
0562 struct regulator_irq_desc {
0563     const char *name;
0564     int fatal_cnt;
0565     int reread_ms;
0566     int irq_off_ms;
0567     bool skip_off;
0568     bool high_prio;
0569     void *data;
0570 
0571     int (*die)(struct regulator_irq_data *rid);
0572     int (*map_event)(int irq, struct regulator_irq_data *rid,
0573               unsigned long *dev_mask);
0574     int (*renable)(struct regulator_irq_data *rid);
0575 };
0576 
0577 /*
0578  * Return values for regulator IRQ helpers.
0579  */
0580 enum {
0581     REGULATOR_ERROR_CLEARED,
0582     REGULATOR_FAILED_RETRY,
0583     REGULATOR_ERROR_ON,
0584 };
0585 
0586 /*
0587  * struct coupling_desc
0588  *
0589  * Describes coupling of regulators. Each regulator should have
0590  * at least a pointer to itself in coupled_rdevs array.
0591  * When a new coupled regulator is resolved, n_resolved is
0592  * incremented.
0593  */
0594 struct coupling_desc {
0595     struct regulator_dev **coupled_rdevs;
0596     struct regulator_coupler *coupler;
0597     int n_resolved;
0598     int n_coupled;
0599 };
0600 
0601 /*
0602  * struct regulator_dev
0603  *
0604  * Voltage / Current regulator class device. One for each
0605  * regulator.
0606  *
0607  * This should *not* be used directly by anything except the regulator
0608  * core and notification injection (which should take the mutex and do
0609  * no other direct access).
0610  */
0611 struct regulator_dev {
0612     const struct regulator_desc *desc;
0613     int exclusive;
0614     u32 use_count;
0615     u32 open_count;
0616     u32 bypass_count;
0617 
0618     /* lists we belong to */
0619     struct list_head list; /* list of all regulators */
0620 
0621     /* lists we own */
0622     struct list_head consumer_list; /* consumers we supply */
0623 
0624     struct coupling_desc coupling_desc;
0625 
0626     struct blocking_notifier_head notifier;
0627     struct ww_mutex mutex; /* consumer lock */
0628     struct task_struct *mutex_owner;
0629     int ref_cnt;
0630     struct module *owner;
0631     struct device dev;
0632     struct regulation_constraints *constraints;
0633     struct regulator *supply;   /* for tree */
0634     const char *supply_name;
0635     struct regmap *regmap;
0636 
0637     struct delayed_work disable_work;
0638 
0639     void *reg_data;     /* regulator_dev data */
0640 
0641     struct dentry *debugfs;
0642 
0643     struct regulator_enable_gpio *ena_pin;
0644     unsigned int ena_gpio_state:1;
0645 
0646     unsigned int is_switch:1;
0647 
0648     /* time when this regulator was disabled last time */
0649     ktime_t last_off;
0650     int cached_err;
0651     bool use_cached_err;
0652     spinlock_t err_lock;
0653 };
0654 
0655 /*
0656  * Convert error flags to corresponding notifications.
0657  *
0658  * Can be used by drivers which use the notification helpers to
0659  * find out correct notification flags based on the error flags. Drivers
0660  * can avoid storing both supported notification and error flags which
0661  * may save few bytes.
0662  */
0663 static inline int regulator_err2notif(int err)
0664 {
0665     switch (err) {
0666     case REGULATOR_ERROR_UNDER_VOLTAGE:
0667         return REGULATOR_EVENT_UNDER_VOLTAGE;
0668     case REGULATOR_ERROR_OVER_CURRENT:
0669         return REGULATOR_EVENT_OVER_CURRENT;
0670     case REGULATOR_ERROR_REGULATION_OUT:
0671         return REGULATOR_EVENT_REGULATION_OUT;
0672     case REGULATOR_ERROR_FAIL:
0673         return REGULATOR_EVENT_FAIL;
0674     case REGULATOR_ERROR_OVER_TEMP:
0675         return REGULATOR_EVENT_OVER_TEMP;
0676     case REGULATOR_ERROR_UNDER_VOLTAGE_WARN:
0677         return REGULATOR_EVENT_UNDER_VOLTAGE_WARN;
0678     case REGULATOR_ERROR_OVER_CURRENT_WARN:
0679         return REGULATOR_EVENT_OVER_CURRENT_WARN;
0680     case REGULATOR_ERROR_OVER_VOLTAGE_WARN:
0681         return REGULATOR_EVENT_OVER_VOLTAGE_WARN;
0682     case REGULATOR_ERROR_OVER_TEMP_WARN:
0683         return REGULATOR_EVENT_OVER_TEMP_WARN;
0684     }
0685     return 0;
0686 }
0687 
0688 
0689 struct regulator_dev *
0690 regulator_register(const struct regulator_desc *regulator_desc,
0691            const struct regulator_config *config);
0692 struct regulator_dev *
0693 devm_regulator_register(struct device *dev,
0694             const struct regulator_desc *regulator_desc,
0695             const struct regulator_config *config);
0696 void regulator_unregister(struct regulator_dev *rdev);
0697 
0698 int regulator_notifier_call_chain(struct regulator_dev *rdev,
0699                   unsigned long event, void *data);
0700 void *devm_regulator_irq_helper(struct device *dev,
0701                 const struct regulator_irq_desc *d, int irq,
0702                 int irq_flags, int common_errs,
0703                 int *per_rdev_errs, struct regulator_dev **rdev,
0704                 int rdev_amount);
0705 void *regulator_irq_helper(struct device *dev,
0706                const struct regulator_irq_desc *d, int irq,
0707                int irq_flags, int common_errs, int *per_rdev_errs,
0708                struct regulator_dev **rdev, int rdev_amount);
0709 void regulator_irq_helper_cancel(void **handle);
0710 int regulator_irq_map_event_simple(int irq, struct regulator_irq_data *rid,
0711                    unsigned long *dev_mask);
0712 
0713 void *rdev_get_drvdata(struct regulator_dev *rdev);
0714 struct device *rdev_get_dev(struct regulator_dev *rdev);
0715 struct regmap *rdev_get_regmap(struct regulator_dev *rdev);
0716 int rdev_get_id(struct regulator_dev *rdev);
0717 
0718 int regulator_mode_to_status(unsigned int);
0719 
0720 int regulator_list_voltage_linear(struct regulator_dev *rdev,
0721                   unsigned int selector);
0722 int regulator_list_voltage_pickable_linear_range(struct regulator_dev *rdev,
0723                            unsigned int selector);
0724 int regulator_list_voltage_linear_range(struct regulator_dev *rdev,
0725                     unsigned int selector);
0726 int regulator_list_voltage_table(struct regulator_dev *rdev,
0727                   unsigned int selector);
0728 int regulator_map_voltage_linear(struct regulator_dev *rdev,
0729                   int min_uV, int max_uV);
0730 int regulator_map_voltage_pickable_linear_range(struct regulator_dev *rdev,
0731                           int min_uV, int max_uV);
0732 int regulator_map_voltage_linear_range(struct regulator_dev *rdev,
0733                        int min_uV, int max_uV);
0734 int regulator_map_voltage_iterate(struct regulator_dev *rdev,
0735                   int min_uV, int max_uV);
0736 int regulator_map_voltage_ascend(struct regulator_dev *rdev,
0737                   int min_uV, int max_uV);
0738 int regulator_get_voltage_sel_pickable_regmap(struct regulator_dev *rdev);
0739 int regulator_set_voltage_sel_pickable_regmap(struct regulator_dev *rdev,
0740                         unsigned int sel);
0741 int regulator_get_voltage_sel_regmap(struct regulator_dev *rdev);
0742 int regulator_set_voltage_sel_regmap(struct regulator_dev *rdev, unsigned sel);
0743 int regulator_is_enabled_regmap(struct regulator_dev *rdev);
0744 int regulator_enable_regmap(struct regulator_dev *rdev);
0745 int regulator_disable_regmap(struct regulator_dev *rdev);
0746 int regulator_set_voltage_time_sel(struct regulator_dev *rdev,
0747                    unsigned int old_selector,
0748                    unsigned int new_selector);
0749 int regulator_set_bypass_regmap(struct regulator_dev *rdev, bool enable);
0750 int regulator_get_bypass_regmap(struct regulator_dev *rdev, bool *enable);
0751 int regulator_set_soft_start_regmap(struct regulator_dev *rdev);
0752 int regulator_set_pull_down_regmap(struct regulator_dev *rdev);
0753 
0754 int regulator_set_active_discharge_regmap(struct regulator_dev *rdev,
0755                       bool enable);
0756 int regulator_set_current_limit_regmap(struct regulator_dev *rdev,
0757                        int min_uA, int max_uA);
0758 int regulator_get_current_limit_regmap(struct regulator_dev *rdev);
0759 void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data);
0760 int regulator_set_ramp_delay_regmap(struct regulator_dev *rdev, int ramp_delay);
0761 int regulator_sync_voltage_rdev(struct regulator_dev *rdev);
0762 
0763 /*
0764  * Helper functions intended to be used by regulator drivers prior registering
0765  * their regulators.
0766  */
0767 int regulator_desc_list_voltage_linear_range(const struct regulator_desc *desc,
0768                          unsigned int selector);
0769 
0770 int regulator_desc_list_voltage_linear(const struct regulator_desc *desc,
0771                        unsigned int selector);
0772 
0773 #ifdef CONFIG_REGULATOR
0774 const char *rdev_get_name(struct regulator_dev *rdev);
0775 #else
0776 static inline const char *rdev_get_name(struct regulator_dev *rdev)
0777 {
0778     return NULL;
0779 }
0780 #endif
0781 
0782 #endif