linux/include/linux/devfreq.h
<<
>>
Prefs
   1/*
   2 * devfreq: Generic Dynamic Voltage and Frequency Scaling (DVFS) Framework
   3 *          for Non-CPU Devices.
   4 *
   5 * Copyright (C) 2011 Samsung Electronics
   6 *      MyungJoo Ham <myungjoo.ham@samsung.com>
   7 *
   8 * This program is free software; you can redistribute it and/or modify
   9 * it under the terms of the GNU General Public License version 2 as
  10 * published by the Free Software Foundation.
  11 */
  12
  13#ifndef __LINUX_DEVFREQ_H__
  14#define __LINUX_DEVFREQ_H__
  15
  16#include <linux/device.h>
  17#include <linux/notifier.h>
  18#include <linux/pm_opp.h>
  19
  20#define DEVFREQ_NAME_LEN 16
  21
  22/* DEVFREQ governor name */
  23#define DEVFREQ_GOV_SIMPLE_ONDEMAND     "simple_ondemand"
  24#define DEVFREQ_GOV_PERFORMANCE         "performance"
  25#define DEVFREQ_GOV_POWERSAVE           "powersave"
  26#define DEVFREQ_GOV_USERSPACE           "userspace"
  27#define DEVFREQ_GOV_PASSIVE             "passive"
  28
  29/* DEVFREQ notifier interface */
  30#define DEVFREQ_TRANSITION_NOTIFIER     (0)
  31
  32/* Transition notifiers of DEVFREQ_TRANSITION_NOTIFIER */
  33#define DEVFREQ_PRECHANGE               (0)
  34#define DEVFREQ_POSTCHANGE              (1)
  35
  36struct devfreq;
  37struct devfreq_governor;
  38
  39/**
  40 * struct devfreq_dev_status - Data given from devfreq user device to
  41 *                           governors. Represents the performance
  42 *                           statistics.
  43 * @total_time:         The total time represented by this instance of
  44 *                      devfreq_dev_status
  45 * @busy_time:          The time that the device was working among the
  46 *                      total_time.
  47 * @current_frequency:  The operating frequency.
  48 * @private_data:       An entry not specified by the devfreq framework.
  49 *                      A device and a specific governor may have their
  50 *                      own protocol with private_data. However, because
  51 *                      this is governor-specific, a governor using this
  52 *                      will be only compatible with devices aware of it.
  53 */
  54struct devfreq_dev_status {
  55        /* both since the last measure */
  56        unsigned long total_time;
  57        unsigned long busy_time;
  58        unsigned long current_frequency;
  59        void *private_data;
  60};
  61
  62/*
  63 * The resulting frequency should be at most this. (this bound is the
  64 * least upper bound; thus, the resulting freq should be lower or same)
  65 * If the flag is not set, the resulting frequency should be at most the
  66 * bound (greatest lower bound)
  67 */
  68#define DEVFREQ_FLAG_LEAST_UPPER_BOUND          0x1
  69
  70/**
  71 * struct devfreq_dev_profile - Devfreq's user device profile
  72 * @initial_freq:       The operating frequency when devfreq_add_device() is
  73 *                      called.
  74 * @polling_ms:         The polling interval in ms. 0 disables polling.
  75 * @target:             The device should set its operating frequency at
  76 *                      freq or lowest-upper-than-freq value. If freq is
  77 *                      higher than any operable frequency, set maximum.
  78 *                      Before returning, target function should set
  79 *                      freq at the current frequency.
  80 *                      The "flags" parameter's possible values are
  81 *                      explained above with "DEVFREQ_FLAG_*" macros.
  82 * @get_dev_status:     The device should provide the current performance
  83 *                      status to devfreq. Governors are recommended not to
  84 *                      use this directly. Instead, governors are recommended
  85 *                      to use devfreq_update_stats() along with
  86 *                      devfreq.last_status.
  87 * @get_cur_freq:       The device should provide the current frequency
  88 *                      at which it is operating.
  89 * @exit:               An optional callback that is called when devfreq
  90 *                      is removing the devfreq object due to error or
  91 *                      from devfreq_remove_device() call. If the user
  92 *                      has registered devfreq->nb at a notifier-head,
  93 *                      this is the time to unregister it.
  94 * @freq_table:         Optional list of frequencies to support statistics
  95 *                      and freq_table must be generated in ascending order.
  96 * @max_state:          The size of freq_table.
  97 */
  98struct devfreq_dev_profile {
  99        unsigned long initial_freq;
 100        unsigned int polling_ms;
 101
 102        int (*target)(struct device *dev, unsigned long *freq, u32 flags);
 103        int (*get_dev_status)(struct device *dev,
 104                              struct devfreq_dev_status *stat);
 105        int (*get_cur_freq)(struct device *dev, unsigned long *freq);
 106        void (*exit)(struct device *dev);
 107
 108        unsigned long *freq_table;
 109        unsigned int max_state;
 110};
 111
 112/**
 113 * struct devfreq - Device devfreq structure
 114 * @node:       list node - contains the devices with devfreq that have been
 115 *              registered.
 116 * @lock:       a mutex to protect accessing devfreq.
 117 * @dev:        device registered by devfreq class. dev.parent is the device
 118 *              using devfreq.
 119 * @profile:    device-specific devfreq profile
 120 * @governor:   method how to choose frequency based on the usage.
 121 * @governor_name:      devfreq governor name for use with this devfreq
 122 * @nb:         notifier block used to notify devfreq object that it should
 123 *              reevaluate operable frequencies. Devfreq users may use
 124 *              devfreq.nb to the corresponding register notifier call chain.
 125 * @work:       delayed work for load monitoring.
 126 * @previous_freq:      previously configured frequency value.
 127 * @data:       Private data of the governor. The devfreq framework does not
 128 *              touch this.
 129 * @min_freq:   Limit minimum frequency requested by user (0: none)
 130 * @max_freq:   Limit maximum frequency requested by user (0: none)
 131 * @scaling_min_freq:   Limit minimum frequency requested by OPP interface
 132 * @scaling_max_freq:   Limit maximum frequency requested by OPP interface
 133 * @stop_polling:        devfreq polling status of a device.
 134 * @total_trans:        Number of devfreq transitions
 135 * @trans_table:        Statistics of devfreq transitions
 136 * @time_in_state:      Statistics of devfreq states
 137 * @last_stat_updated:  The last time stat updated
 138 * @transition_notifier_list: list head of DEVFREQ_TRANSITION_NOTIFIER notifier
 139 *
 140 * This structure stores the devfreq information for a give device.
 141 *
 142 * Note that when a governor accesses entries in struct devfreq in its
 143 * functions except for the context of callbacks defined in struct
 144 * devfreq_governor, the governor should protect its access with the
 145 * struct mutex lock in struct devfreq. A governor may use this mutex
 146 * to protect its own private data in void *data as well.
 147 */
 148struct devfreq {
 149        struct list_head node;
 150
 151        struct mutex lock;
 152        struct device dev;
 153        struct devfreq_dev_profile *profile;
 154        const struct devfreq_governor *governor;
 155        char governor_name[DEVFREQ_NAME_LEN];
 156        struct notifier_block nb;
 157        struct delayed_work work;
 158
 159        unsigned long previous_freq;
 160        struct devfreq_dev_status last_status;
 161
 162        void *data; /* private data for governors */
 163
 164        unsigned long min_freq;
 165        unsigned long max_freq;
 166        unsigned long scaling_min_freq;
 167        unsigned long scaling_max_freq;
 168        bool stop_polling;
 169
 170        /* information for device frequency transition */
 171        unsigned int total_trans;
 172        unsigned int *trans_table;
 173        unsigned long *time_in_state;
 174        unsigned long last_stat_updated;
 175
 176        struct srcu_notifier_head transition_notifier_list;
 177};
 178
 179struct devfreq_freqs {
 180        unsigned long old;
 181        unsigned long new;
 182};
 183
 184#if defined(CONFIG_PM_DEVFREQ)
 185extern struct devfreq *devfreq_add_device(struct device *dev,
 186                                  struct devfreq_dev_profile *profile,
 187                                  const char *governor_name,
 188                                  void *data);
 189extern int devfreq_remove_device(struct devfreq *devfreq);
 190extern struct devfreq *devm_devfreq_add_device(struct device *dev,
 191                                  struct devfreq_dev_profile *profile,
 192                                  const char *governor_name,
 193                                  void *data);
 194extern void devm_devfreq_remove_device(struct device *dev,
 195                                  struct devfreq *devfreq);
 196
 197/* Supposed to be called by PM callbacks */
 198extern int devfreq_suspend_device(struct devfreq *devfreq);
 199extern int devfreq_resume_device(struct devfreq *devfreq);
 200
 201extern void devfreq_suspend(void);
 202extern void devfreq_resume(void);
 203
 204/* Helper functions for devfreq user device driver with OPP. */
 205extern struct dev_pm_opp *devfreq_recommended_opp(struct device *dev,
 206                                           unsigned long *freq, u32 flags);
 207extern int devfreq_register_opp_notifier(struct device *dev,
 208                                         struct devfreq *devfreq);
 209extern int devfreq_unregister_opp_notifier(struct device *dev,
 210                                           struct devfreq *devfreq);
 211extern int devm_devfreq_register_opp_notifier(struct device *dev,
 212                                              struct devfreq *devfreq);
 213extern void devm_devfreq_unregister_opp_notifier(struct device *dev,
 214                                                struct devfreq *devfreq);
 215extern int devfreq_register_notifier(struct devfreq *devfreq,
 216                                        struct notifier_block *nb,
 217                                        unsigned int list);
 218extern int devfreq_unregister_notifier(struct devfreq *devfreq,
 219                                        struct notifier_block *nb,
 220                                        unsigned int list);
 221extern int devm_devfreq_register_notifier(struct device *dev,
 222                                struct devfreq *devfreq,
 223                                struct notifier_block *nb,
 224                                unsigned int list);
 225extern void devm_devfreq_unregister_notifier(struct device *dev,
 226                                struct devfreq *devfreq,
 227                                struct notifier_block *nb,
 228                                unsigned int list);
 229extern struct devfreq *devfreq_get_devfreq_by_phandle(struct device *dev,
 230                                                int index);
 231
 232#if IS_ENABLED(CONFIG_DEVFREQ_GOV_SIMPLE_ONDEMAND)
 233/**
 234 * struct devfreq_simple_ondemand_data - void *data fed to struct devfreq
 235 *      and devfreq_add_device
 236 * @upthreshold:        If the load is over this value, the frequency jumps.
 237 *                      Specify 0 to use the default. Valid value = 0 to 100.
 238 * @downdifferential:   If the load is under upthreshold - downdifferential,
 239 *                      the governor may consider slowing the frequency down.
 240 *                      Specify 0 to use the default. Valid value = 0 to 100.
 241 *                      downdifferential < upthreshold must hold.
 242 *
 243 * If the fed devfreq_simple_ondemand_data pointer is NULL to the governor,
 244 * the governor uses the default values.
 245 */
 246struct devfreq_simple_ondemand_data {
 247        unsigned int upthreshold;
 248        unsigned int downdifferential;
 249};
 250#endif
 251
 252#if IS_ENABLED(CONFIG_DEVFREQ_GOV_PASSIVE)
 253/**
 254 * struct devfreq_passive_data - void *data fed to struct devfreq
 255 *      and devfreq_add_device
 256 * @parent:     the devfreq instance of parent device.
 257 * @get_target_freq:    Optional callback, Returns desired operating frequency
 258 *                      for the device using passive governor. That is called
 259 *                      when passive governor should decide the next frequency
 260 *                      by using the new frequency of parent devfreq device
 261 *                      using governors except for passive governor.
 262 *                      If the devfreq device has the specific method to decide
 263 *                      the next frequency, should use this callback.
 264 * @this:       the devfreq instance of own device.
 265 * @nb:         the notifier block for DEVFREQ_TRANSITION_NOTIFIER list
 266 *
 267 * The devfreq_passive_data have to set the devfreq instance of parent
 268 * device with governors except for the passive governor. But, don't need to
 269 * initialize the 'this' and 'nb' field because the devfreq core will handle
 270 * them.
 271 */
 272struct devfreq_passive_data {
 273        /* Should set the devfreq instance of parent device */
 274        struct devfreq *parent;
 275
 276        /* Optional callback to decide the next frequency of passvice device */
 277        int (*get_target_freq)(struct devfreq *this, unsigned long *freq);
 278
 279        /* For passive governor's internal use. Don't need to set them */
 280        struct devfreq *this;
 281        struct notifier_block nb;
 282};
 283#endif
 284
 285#else /* !CONFIG_PM_DEVFREQ */
 286static inline struct devfreq *devfreq_add_device(struct device *dev,
 287                                          struct devfreq_dev_profile *profile,
 288                                          const char *governor_name,
 289                                          void *data)
 290{
 291        return ERR_PTR(-ENOSYS);
 292}
 293
 294static inline int devfreq_remove_device(struct devfreq *devfreq)
 295{
 296        return 0;
 297}
 298
 299static inline struct devfreq *devm_devfreq_add_device(struct device *dev,
 300                                        struct devfreq_dev_profile *profile,
 301                                        const char *governor_name,
 302                                        void *data)
 303{
 304        return ERR_PTR(-ENOSYS);
 305}
 306
 307static inline void devm_devfreq_remove_device(struct device *dev,
 308                                        struct devfreq *devfreq)
 309{
 310}
 311
 312static inline int devfreq_suspend_device(struct devfreq *devfreq)
 313{
 314        return 0;
 315}
 316
 317static inline int devfreq_resume_device(struct devfreq *devfreq)
 318{
 319        return 0;
 320}
 321
 322static inline void devfreq_suspend(void) {}
 323static inline void devfreq_resume(void) {}
 324
 325static inline struct dev_pm_opp *devfreq_recommended_opp(struct device *dev,
 326                                           unsigned long *freq, u32 flags)
 327{
 328        return ERR_PTR(-EINVAL);
 329}
 330
 331static inline int devfreq_register_opp_notifier(struct device *dev,
 332                                         struct devfreq *devfreq)
 333{
 334        return -EINVAL;
 335}
 336
 337static inline int devfreq_unregister_opp_notifier(struct device *dev,
 338                                           struct devfreq *devfreq)
 339{
 340        return -EINVAL;
 341}
 342
 343static inline int devm_devfreq_register_opp_notifier(struct device *dev,
 344                                                     struct devfreq *devfreq)
 345{
 346        return -EINVAL;
 347}
 348
 349static inline void devm_devfreq_unregister_opp_notifier(struct device *dev,
 350                                                        struct devfreq *devfreq)
 351{
 352}
 353
 354static inline int devfreq_register_notifier(struct devfreq *devfreq,
 355                                        struct notifier_block *nb,
 356                                        unsigned int list)
 357{
 358        return 0;
 359}
 360
 361static inline int devfreq_unregister_notifier(struct devfreq *devfreq,
 362                                        struct notifier_block *nb,
 363                                        unsigned int list)
 364{
 365        return 0;
 366}
 367
 368static inline int devm_devfreq_register_notifier(struct device *dev,
 369                                struct devfreq *devfreq,
 370                                struct notifier_block *nb,
 371                                unsigned int list)
 372{
 373        return 0;
 374}
 375
 376static inline void devm_devfreq_unregister_notifier(struct device *dev,
 377                                struct devfreq *devfreq,
 378                                struct notifier_block *nb,
 379                                unsigned int list)
 380{
 381}
 382
 383static inline struct devfreq *devfreq_get_devfreq_by_phandle(struct device *dev,
 384                                                        int index)
 385{
 386        return ERR_PTR(-ENODEV);
 387}
 388
 389static inline int devfreq_update_stats(struct devfreq *df)
 390{
 391        return -EINVAL;
 392}
 393#endif /* CONFIG_PM_DEVFREQ */
 394
 395#endif /* __LINUX_DEVFREQ_H__ */
 396