Viresh Kumar | f59d3ee | 2015-09-04 13:47:26 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Generic OPP Interface |
| 3 | * |
| 4 | * Copyright (C) 2009-2010 Texas Instruments Incorporated. |
| 5 | * Nishanth Menon |
| 6 | * Romit Dasgupta |
| 7 | * Kevin Hilman |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License version 2 as |
| 11 | * published by the Free Software Foundation. |
| 12 | */ |
| 13 | |
| 14 | #ifndef __DRIVER_OPP_H__ |
| 15 | #define __DRIVER_OPP_H__ |
| 16 | |
| 17 | #include <linux/device.h> |
| 18 | #include <linux/kernel.h> |
| 19 | #include <linux/list.h> |
Viresh Kumar | deaa514 | 2015-11-11 07:59:01 +0530 | [diff] [blame] | 20 | #include <linux/limits.h> |
Viresh Kumar | f59d3ee | 2015-09-04 13:47:26 +0530 | [diff] [blame] | 21 | #include <linux/pm_opp.h> |
| 22 | #include <linux/rculist.h> |
| 23 | #include <linux/rcupdate.h> |
| 24 | |
Viresh Kumar | d54974c | 2016-02-09 10:30:38 +0530 | [diff] [blame] | 25 | struct clk; |
Viresh Kumar | 9f8ea96 | 2016-02-09 10:30:33 +0530 | [diff] [blame] | 26 | struct regulator; |
| 27 | |
Viresh Kumar | 87b4115 | 2015-11-05 14:21:19 +0530 | [diff] [blame] | 28 | /* Lock to allow exclusive modification to the device and opp lists */ |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 29 | extern struct mutex opp_table_lock; |
Viresh Kumar | 87b4115 | 2015-11-05 14:21:19 +0530 | [diff] [blame] | 30 | |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 31 | extern struct list_head opp_tables; |
| 32 | |
Viresh Kumar | f59d3ee | 2015-09-04 13:47:26 +0530 | [diff] [blame] | 33 | /* |
| 34 | * Internal data structure organization with the OPP layer library is as |
| 35 | * follows: |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 36 | * opp_tables (root) |
Viresh Kumar | f59d3ee | 2015-09-04 13:47:26 +0530 | [diff] [blame] | 37 | * |- device 1 (represents voltage domain 1) |
| 38 | * | |- opp 1 (availability, freq, voltage) |
| 39 | * | |- opp 2 .. |
| 40 | * ... ... |
| 41 | * | `- opp n .. |
| 42 | * |- device 2 (represents the next voltage domain) |
| 43 | * ... |
| 44 | * `- device m (represents mth voltage domain) |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 45 | * device 1, 2.. are represented by opp_table structure while each opp |
Viresh Kumar | f59d3ee | 2015-09-04 13:47:26 +0530 | [diff] [blame] | 46 | * is represented by the opp structure. |
| 47 | */ |
| 48 | |
| 49 | /** |
| 50 | * struct dev_pm_opp - Generic OPP description structure |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 51 | * @node: opp table node. The nodes are maintained throughout the lifetime |
Viresh Kumar | f59d3ee | 2015-09-04 13:47:26 +0530 | [diff] [blame] | 52 | * of boot. It is expected only an optimal set of OPPs are |
| 53 | * added to the library by the SoC framework. |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 54 | * RCU usage: opp table is traversed with RCU locks. node |
Viresh Kumar | f59d3ee | 2015-09-04 13:47:26 +0530 | [diff] [blame] | 55 | * modification is possible realtime, hence the modifications |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 56 | * are protected by the opp_table_lock for integrity. |
Viresh Kumar | f59d3ee | 2015-09-04 13:47:26 +0530 | [diff] [blame] | 57 | * IMPORTANT: the opp nodes should be maintained in increasing |
| 58 | * order. |
Viresh Kumar | f59d3ee | 2015-09-04 13:47:26 +0530 | [diff] [blame] | 59 | * @available: true/false - marks if this OPP as available or not |
Viresh Kumar | dc4e7b1 | 2015-11-19 09:13:56 +0530 | [diff] [blame] | 60 | * @dynamic: not-created from static DT entries. |
Viresh Kumar | f59d3ee | 2015-09-04 13:47:26 +0530 | [diff] [blame] | 61 | * @turbo: true if turbo (boost) OPP |
Viresh Kumar | deaa514 | 2015-11-11 07:59:01 +0530 | [diff] [blame] | 62 | * @suspend: true if suspend OPP |
Viresh Kumar | f59d3ee | 2015-09-04 13:47:26 +0530 | [diff] [blame] | 63 | * @rate: Frequency in hertz |
Viresh Kumar | dfbe467 | 2016-12-01 16:28:19 +0530 | [diff] [blame] | 64 | * @supplies: Power supplies voltage/current values |
Viresh Kumar | f59d3ee | 2015-09-04 13:47:26 +0530 | [diff] [blame] | 65 | * @clock_latency_ns: Latency (in nanoseconds) of switching to this OPP's |
| 66 | * frequency from any other OPP's frequency. |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 67 | * @opp_table: points back to the opp_table struct this opp belongs to |
Viresh Kumar | f59d3ee | 2015-09-04 13:47:26 +0530 | [diff] [blame] | 68 | * @rcu_head: RCU callback head used for deferred freeing |
| 69 | * @np: OPP's device node. |
Viresh Kumar | deaa514 | 2015-11-11 07:59:01 +0530 | [diff] [blame] | 70 | * @dentry: debugfs dentry pointer (per opp) |
Viresh Kumar | f59d3ee | 2015-09-04 13:47:26 +0530 | [diff] [blame] | 71 | * |
| 72 | * This structure stores the OPP information for a given device. |
| 73 | */ |
| 74 | struct dev_pm_opp { |
| 75 | struct list_head node; |
| 76 | |
| 77 | bool available; |
| 78 | bool dynamic; |
| 79 | bool turbo; |
Viresh Kumar | deaa514 | 2015-11-11 07:59:01 +0530 | [diff] [blame] | 80 | bool suspend; |
Viresh Kumar | f59d3ee | 2015-09-04 13:47:26 +0530 | [diff] [blame] | 81 | unsigned long rate; |
| 82 | |
Viresh Kumar | dfbe467 | 2016-12-01 16:28:19 +0530 | [diff] [blame] | 83 | struct dev_pm_opp_supply *supplies; |
Viresh Kumar | 0f0fe7e | 2016-12-01 16:28:17 +0530 | [diff] [blame] | 84 | |
Viresh Kumar | f59d3ee | 2015-09-04 13:47:26 +0530 | [diff] [blame] | 85 | unsigned long clock_latency_ns; |
| 86 | |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 87 | struct opp_table *opp_table; |
Viresh Kumar | f59d3ee | 2015-09-04 13:47:26 +0530 | [diff] [blame] | 88 | struct rcu_head rcu_head; |
| 89 | |
| 90 | struct device_node *np; |
Viresh Kumar | deaa514 | 2015-11-11 07:59:01 +0530 | [diff] [blame] | 91 | |
| 92 | #ifdef CONFIG_DEBUG_FS |
| 93 | struct dentry *dentry; |
| 94 | #endif |
Viresh Kumar | f59d3ee | 2015-09-04 13:47:26 +0530 | [diff] [blame] | 95 | }; |
| 96 | |
| 97 | /** |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 98 | * struct opp_device - devices managed by 'struct opp_table' |
Viresh Kumar | f59d3ee | 2015-09-04 13:47:26 +0530 | [diff] [blame] | 99 | * @node: list node |
| 100 | * @dev: device to which the struct object belongs |
| 101 | * @rcu_head: RCU callback head used for deferred freeing |
Viresh Kumar | deaa514 | 2015-11-11 07:59:01 +0530 | [diff] [blame] | 102 | * @dentry: debugfs dentry pointer (per device) |
Viresh Kumar | f59d3ee | 2015-09-04 13:47:26 +0530 | [diff] [blame] | 103 | * |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 104 | * This is an internal data structure maintaining the devices that are managed |
| 105 | * by 'struct opp_table'. |
Viresh Kumar | f59d3ee | 2015-09-04 13:47:26 +0530 | [diff] [blame] | 106 | */ |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 107 | struct opp_device { |
Viresh Kumar | f59d3ee | 2015-09-04 13:47:26 +0530 | [diff] [blame] | 108 | struct list_head node; |
| 109 | const struct device *dev; |
| 110 | struct rcu_head rcu_head; |
Viresh Kumar | deaa514 | 2015-11-11 07:59:01 +0530 | [diff] [blame] | 111 | |
| 112 | #ifdef CONFIG_DEBUG_FS |
| 113 | struct dentry *dentry; |
| 114 | #endif |
Viresh Kumar | f59d3ee | 2015-09-04 13:47:26 +0530 | [diff] [blame] | 115 | }; |
| 116 | |
Viresh Kumar | 79ee2e8 | 2016-06-16 19:03:11 +0530 | [diff] [blame] | 117 | enum opp_table_access { |
| 118 | OPP_TABLE_ACCESS_UNKNOWN = 0, |
| 119 | OPP_TABLE_ACCESS_EXCLUSIVE = 1, |
| 120 | OPP_TABLE_ACCESS_SHARED = 2, |
| 121 | }; |
| 122 | |
Viresh Kumar | f59d3ee | 2015-09-04 13:47:26 +0530 | [diff] [blame] | 123 | /** |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 124 | * struct opp_table - Device opp structure |
| 125 | * @node: table node - contains the devices with OPPs that |
Viresh Kumar | f59d3ee | 2015-09-04 13:47:26 +0530 | [diff] [blame] | 126 | * have been registered. Nodes once added are not modified in this |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 127 | * table. |
| 128 | * RCU usage: nodes are not modified in the table of opp_table, |
| 129 | * however addition is possible and is secured by opp_table_lock |
Viresh Kumar | f59d3ee | 2015-09-04 13:47:26 +0530 | [diff] [blame] | 130 | * @srcu_head: notifier head to notify the OPP availability changes. |
| 131 | * @rcu_head: RCU callback head used for deferred freeing |
| 132 | * @dev_list: list of devices that share these OPPs |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 133 | * @opp_list: table of opps |
Viresh Kumar | f59d3ee | 2015-09-04 13:47:26 +0530 | [diff] [blame] | 134 | * @np: struct device_node pointer for opp's DT node. |
Viresh Kumar | dc4e7b1 | 2015-11-19 09:13:56 +0530 | [diff] [blame] | 135 | * @clock_latency_ns_max: Max clock latency in nanoseconds. |
Viresh Kumar | f59d3ee | 2015-09-04 13:47:26 +0530 | [diff] [blame] | 136 | * @shared_opp: OPP is shared between multiple devices. |
Viresh Kumar | dc4e7b1 | 2015-11-19 09:13:56 +0530 | [diff] [blame] | 137 | * @suspend_opp: Pointer to OPP to be used during device suspend. |
Viresh Kumar | 7de36b0 | 2015-12-09 08:01:46 +0530 | [diff] [blame] | 138 | * @supported_hw: Array of version number to support. |
| 139 | * @supported_hw_count: Number of elements in supported_hw array. |
Viresh Kumar | 01fb4d3 | 2015-12-09 08:01:47 +0530 | [diff] [blame] | 140 | * @prop_name: A name to postfix to many DT properties, while parsing them. |
Viresh Kumar | d54974c | 2016-02-09 10:30:38 +0530 | [diff] [blame] | 141 | * @clk: Device's clock handle |
Viresh Kumar | dfbe467 | 2016-12-01 16:28:19 +0530 | [diff] [blame] | 142 | * @regulators: Supply regulators |
| 143 | * @regulator_count: Number of power supply regulators |
Viresh Kumar | 4dab160 | 2016-12-01 16:28:21 +0530 | [diff] [blame] | 144 | * @set_opp: Platform specific set_opp callback |
Viresh Kumar | 9473558 | 2016-12-01 16:28:20 +0530 | [diff] [blame] | 145 | * @set_opp_data: Data to be passed to set_opp callback |
Viresh Kumar | deaa514 | 2015-11-11 07:59:01 +0530 | [diff] [blame] | 146 | * @dentry: debugfs dentry pointer of the real device directory (not links). |
| 147 | * @dentry_name: Name of the real dentry. |
Viresh Kumar | f59d3ee | 2015-09-04 13:47:26 +0530 | [diff] [blame] | 148 | * |
Viresh Kumar | 50f8cfb | 2016-02-09 10:30:37 +0530 | [diff] [blame] | 149 | * @voltage_tolerance_v1: In percentage, for v1 bindings only. |
| 150 | * |
Viresh Kumar | f59d3ee | 2015-09-04 13:47:26 +0530 | [diff] [blame] | 151 | * This is an internal data structure maintaining the link to opps attached to |
| 152 | * a device. This structure is not meant to be shared to users as it is |
| 153 | * meant for book keeping and private to OPP library. |
| 154 | * |
| 155 | * Because the opp structures can be used from both rcu and srcu readers, we |
| 156 | * need to wait for the grace period of both of them before freeing any |
| 157 | * resources. And so we have used kfree_rcu() from within call_srcu() handlers. |
| 158 | */ |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 159 | struct opp_table { |
Viresh Kumar | f59d3ee | 2015-09-04 13:47:26 +0530 | [diff] [blame] | 160 | struct list_head node; |
| 161 | |
| 162 | struct srcu_notifier_head srcu_head; |
| 163 | struct rcu_head rcu_head; |
| 164 | struct list_head dev_list; |
| 165 | struct list_head opp_list; |
| 166 | |
| 167 | struct device_node *np; |
| 168 | unsigned long clock_latency_ns_max; |
Viresh Kumar | 50f8cfb | 2016-02-09 10:30:37 +0530 | [diff] [blame] | 169 | |
| 170 | /* For backward compatibility with v1 bindings */ |
| 171 | unsigned int voltage_tolerance_v1; |
| 172 | |
Viresh Kumar | 79ee2e8 | 2016-06-16 19:03:11 +0530 | [diff] [blame] | 173 | enum opp_table_access shared_opp; |
Viresh Kumar | f59d3ee | 2015-09-04 13:47:26 +0530 | [diff] [blame] | 174 | struct dev_pm_opp *suspend_opp; |
Viresh Kumar | deaa514 | 2015-11-11 07:59:01 +0530 | [diff] [blame] | 175 | |
Viresh Kumar | 7de36b0 | 2015-12-09 08:01:46 +0530 | [diff] [blame] | 176 | unsigned int *supported_hw; |
| 177 | unsigned int supported_hw_count; |
Viresh Kumar | 01fb4d3 | 2015-12-09 08:01:47 +0530 | [diff] [blame] | 178 | const char *prop_name; |
Viresh Kumar | d54974c | 2016-02-09 10:30:38 +0530 | [diff] [blame] | 179 | struct clk *clk; |
Viresh Kumar | dfbe467 | 2016-12-01 16:28:19 +0530 | [diff] [blame] | 180 | struct regulator **regulators; |
| 181 | unsigned int regulator_count; |
Viresh Kumar | 7de36b0 | 2015-12-09 08:01:46 +0530 | [diff] [blame] | 182 | |
Viresh Kumar | 4dab160 | 2016-12-01 16:28:21 +0530 | [diff] [blame] | 183 | int (*set_opp)(struct dev_pm_set_opp_data *data); |
Viresh Kumar | 9473558 | 2016-12-01 16:28:20 +0530 | [diff] [blame] | 184 | struct dev_pm_set_opp_data *set_opp_data; |
| 185 | |
Viresh Kumar | deaa514 | 2015-11-11 07:59:01 +0530 | [diff] [blame] | 186 | #ifdef CONFIG_DEBUG_FS |
| 187 | struct dentry *dentry; |
| 188 | char dentry_name[NAME_MAX]; |
| 189 | #endif |
Viresh Kumar | f59d3ee | 2015-09-04 13:47:26 +0530 | [diff] [blame] | 190 | }; |
| 191 | |
| 192 | /* Routines internal to opp core */ |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 193 | struct opp_table *_find_opp_table(struct device *dev); |
| 194 | struct opp_device *_add_opp_dev(const struct device *dev, struct opp_table *opp_table); |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 195 | void _dev_pm_opp_remove_table(struct device *dev, bool remove_all); |
Viresh Kumar | 63a69ea | 2017-01-02 14:40:57 +0530 | [diff] [blame] | 196 | struct dev_pm_opp *_opp_allocate(struct device *dev, struct opp_table **opp_table); |
Viresh Kumar | 969fceb | 2017-01-02 14:40:59 +0530 | [diff] [blame^] | 197 | void _opp_free(struct dev_pm_opp *opp, struct opp_table *opp_table); |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 198 | int _opp_add(struct device *dev, struct dev_pm_opp *new_opp, struct opp_table *opp_table); |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 199 | int _opp_add_v1(struct device *dev, unsigned long freq, long u_volt, bool dynamic); |
| 200 | void _dev_pm_opp_cpumask_remove_table(const struct cpumask *cpumask, bool of); |
| 201 | |
| 202 | #ifdef CONFIG_OF |
| 203 | void _of_init_opp_table(struct opp_table *opp_table, struct device *dev); |
| 204 | #else |
| 205 | static inline void _of_init_opp_table(struct opp_table *opp_table, struct device *dev) {} |
| 206 | #endif |
Viresh Kumar | f59d3ee | 2015-09-04 13:47:26 +0530 | [diff] [blame] | 207 | |
Viresh Kumar | deaa514 | 2015-11-11 07:59:01 +0530 | [diff] [blame] | 208 | #ifdef CONFIG_DEBUG_FS |
| 209 | void opp_debug_remove_one(struct dev_pm_opp *opp); |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 210 | int opp_debug_create_one(struct dev_pm_opp *opp, struct opp_table *opp_table); |
| 211 | int opp_debug_register(struct opp_device *opp_dev, struct opp_table *opp_table); |
| 212 | void opp_debug_unregister(struct opp_device *opp_dev, struct opp_table *opp_table); |
Viresh Kumar | deaa514 | 2015-11-11 07:59:01 +0530 | [diff] [blame] | 213 | #else |
| 214 | static inline void opp_debug_remove_one(struct dev_pm_opp *opp) {} |
| 215 | |
| 216 | static inline int opp_debug_create_one(struct dev_pm_opp *opp, |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 217 | struct opp_table *opp_table) |
Viresh Kumar | deaa514 | 2015-11-11 07:59:01 +0530 | [diff] [blame] | 218 | { return 0; } |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 219 | static inline int opp_debug_register(struct opp_device *opp_dev, |
| 220 | struct opp_table *opp_table) |
Viresh Kumar | deaa514 | 2015-11-11 07:59:01 +0530 | [diff] [blame] | 221 | { return 0; } |
| 222 | |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 223 | static inline void opp_debug_unregister(struct opp_device *opp_dev, |
| 224 | struct opp_table *opp_table) |
Viresh Kumar | deaa514 | 2015-11-11 07:59:01 +0530 | [diff] [blame] | 225 | { } |
| 226 | #endif /* DEBUG_FS */ |
| 227 | |
Viresh Kumar | f59d3ee | 2015-09-04 13:47:26 +0530 | [diff] [blame] | 228 | #endif /* __DRIVER_OPP_H__ */ |