blob: 334f7570df32975fe96e199b4f25a9a73c253a33 [file] [log] [blame]
Viresh Kumarf59d3ee2015-09-04 13:47:26 +05301/*
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 Kumardeaa5142015-11-11 07:59:01 +053020#include <linux/limits.h>
Viresh Kumarf59d3ee2015-09-04 13:47:26 +053021#include <linux/pm_opp.h>
22#include <linux/rculist.h>
23#include <linux/rcupdate.h>
24
Viresh Kumard54974c2016-02-09 10:30:38 +053025struct clk;
Viresh Kumar9f8ea962016-02-09 10:30:33 +053026struct regulator;
27
Viresh Kumar87b41152015-11-05 14:21:19 +053028/* Lock to allow exclusive modification to the device and opp lists */
Viresh Kumar2c2709d2016-02-16 14:17:53 +053029extern struct mutex opp_table_lock;
Viresh Kumar87b41152015-11-05 14:21:19 +053030
Viresh Kumarf47b72a2016-05-05 16:20:33 +053031extern struct list_head opp_tables;
32
Viresh Kumarf59d3ee2015-09-04 13:47:26 +053033/*
34 * Internal data structure organization with the OPP layer library is as
35 * follows:
Viresh Kumar2c2709d2016-02-16 14:17:53 +053036 * opp_tables (root)
Viresh Kumarf59d3ee2015-09-04 13:47:26 +053037 * |- 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 Kumar2c2709d2016-02-16 14:17:53 +053045 * device 1, 2.. are represented by opp_table structure while each opp
Viresh Kumarf59d3ee2015-09-04 13:47:26 +053046 * is represented by the opp structure.
47 */
48
49/**
50 * struct dev_pm_opp - Generic OPP description structure
Viresh Kumar2c2709d2016-02-16 14:17:53 +053051 * @node: opp table node. The nodes are maintained throughout the lifetime
Viresh Kumarf59d3ee2015-09-04 13:47:26 +053052 * of boot. It is expected only an optimal set of OPPs are
53 * added to the library by the SoC framework.
Viresh Kumar2c2709d2016-02-16 14:17:53 +053054 * RCU usage: opp table is traversed with RCU locks. node
Viresh Kumarf59d3ee2015-09-04 13:47:26 +053055 * modification is possible realtime, hence the modifications
Viresh Kumar2c2709d2016-02-16 14:17:53 +053056 * are protected by the opp_table_lock for integrity.
Viresh Kumarf59d3ee2015-09-04 13:47:26 +053057 * IMPORTANT: the opp nodes should be maintained in increasing
58 * order.
Viresh Kumarf59d3ee2015-09-04 13:47:26 +053059 * @available: true/false - marks if this OPP as available or not
Viresh Kumardc4e7b12015-11-19 09:13:56 +053060 * @dynamic: not-created from static DT entries.
Viresh Kumarf59d3ee2015-09-04 13:47:26 +053061 * @turbo: true if turbo (boost) OPP
Viresh Kumardeaa5142015-11-11 07:59:01 +053062 * @suspend: true if suspend OPP
Viresh Kumarf59d3ee2015-09-04 13:47:26 +053063 * @rate: Frequency in hertz
Viresh Kumardfbe4672016-12-01 16:28:19 +053064 * @supplies: Power supplies voltage/current values
Viresh Kumarf59d3ee2015-09-04 13:47:26 +053065 * @clock_latency_ns: Latency (in nanoseconds) of switching to this OPP's
66 * frequency from any other OPP's frequency.
Viresh Kumar2c2709d2016-02-16 14:17:53 +053067 * @opp_table: points back to the opp_table struct this opp belongs to
Viresh Kumarf59d3ee2015-09-04 13:47:26 +053068 * @rcu_head: RCU callback head used for deferred freeing
69 * @np: OPP's device node.
Viresh Kumardeaa5142015-11-11 07:59:01 +053070 * @dentry: debugfs dentry pointer (per opp)
Viresh Kumarf59d3ee2015-09-04 13:47:26 +053071 *
72 * This structure stores the OPP information for a given device.
73 */
74struct dev_pm_opp {
75 struct list_head node;
76
77 bool available;
78 bool dynamic;
79 bool turbo;
Viresh Kumardeaa5142015-11-11 07:59:01 +053080 bool suspend;
Viresh Kumarf59d3ee2015-09-04 13:47:26 +053081 unsigned long rate;
82
Viresh Kumardfbe4672016-12-01 16:28:19 +053083 struct dev_pm_opp_supply *supplies;
Viresh Kumar0f0fe7e2016-12-01 16:28:17 +053084
Viresh Kumarf59d3ee2015-09-04 13:47:26 +053085 unsigned long clock_latency_ns;
86
Viresh Kumar2c2709d2016-02-16 14:17:53 +053087 struct opp_table *opp_table;
Viresh Kumarf59d3ee2015-09-04 13:47:26 +053088 struct rcu_head rcu_head;
89
90 struct device_node *np;
Viresh Kumardeaa5142015-11-11 07:59:01 +053091
92#ifdef CONFIG_DEBUG_FS
93 struct dentry *dentry;
94#endif
Viresh Kumarf59d3ee2015-09-04 13:47:26 +053095};
96
97/**
Viresh Kumar2c2709d2016-02-16 14:17:53 +053098 * struct opp_device - devices managed by 'struct opp_table'
Viresh Kumarf59d3ee2015-09-04 13:47:26 +053099 * @node: list node
100 * @dev: device to which the struct object belongs
101 * @rcu_head: RCU callback head used for deferred freeing
Viresh Kumardeaa5142015-11-11 07:59:01 +0530102 * @dentry: debugfs dentry pointer (per device)
Viresh Kumarf59d3ee2015-09-04 13:47:26 +0530103 *
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530104 * This is an internal data structure maintaining the devices that are managed
105 * by 'struct opp_table'.
Viresh Kumarf59d3ee2015-09-04 13:47:26 +0530106 */
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530107struct opp_device {
Viresh Kumarf59d3ee2015-09-04 13:47:26 +0530108 struct list_head node;
109 const struct device *dev;
110 struct rcu_head rcu_head;
Viresh Kumardeaa5142015-11-11 07:59:01 +0530111
112#ifdef CONFIG_DEBUG_FS
113 struct dentry *dentry;
114#endif
Viresh Kumarf59d3ee2015-09-04 13:47:26 +0530115};
116
Viresh Kumar79ee2e82016-06-16 19:03:11 +0530117enum opp_table_access {
118 OPP_TABLE_ACCESS_UNKNOWN = 0,
119 OPP_TABLE_ACCESS_EXCLUSIVE = 1,
120 OPP_TABLE_ACCESS_SHARED = 2,
121};
122
Viresh Kumarf59d3ee2015-09-04 13:47:26 +0530123/**
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530124 * struct opp_table - Device opp structure
125 * @node: table node - contains the devices with OPPs that
Viresh Kumarf59d3ee2015-09-04 13:47:26 +0530126 * have been registered. Nodes once added are not modified in this
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530127 * 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 Kumarf59d3ee2015-09-04 13:47:26 +0530130 * @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 Kumar2c2709d2016-02-16 14:17:53 +0530133 * @opp_list: table of opps
Viresh Kumarf59d3ee2015-09-04 13:47:26 +0530134 * @np: struct device_node pointer for opp's DT node.
Viresh Kumardc4e7b12015-11-19 09:13:56 +0530135 * @clock_latency_ns_max: Max clock latency in nanoseconds.
Viresh Kumarf59d3ee2015-09-04 13:47:26 +0530136 * @shared_opp: OPP is shared between multiple devices.
Viresh Kumardc4e7b12015-11-19 09:13:56 +0530137 * @suspend_opp: Pointer to OPP to be used during device suspend.
Viresh Kumar7de36b02015-12-09 08:01:46 +0530138 * @supported_hw: Array of version number to support.
139 * @supported_hw_count: Number of elements in supported_hw array.
Viresh Kumar01fb4d32015-12-09 08:01:47 +0530140 * @prop_name: A name to postfix to many DT properties, while parsing them.
Viresh Kumard54974c2016-02-09 10:30:38 +0530141 * @clk: Device's clock handle
Viresh Kumardfbe4672016-12-01 16:28:19 +0530142 * @regulators: Supply regulators
143 * @regulator_count: Number of power supply regulators
Viresh Kumar4dab1602016-12-01 16:28:21 +0530144 * @set_opp: Platform specific set_opp callback
Viresh Kumar94735582016-12-01 16:28:20 +0530145 * @set_opp_data: Data to be passed to set_opp callback
Viresh Kumardeaa5142015-11-11 07:59:01 +0530146 * @dentry: debugfs dentry pointer of the real device directory (not links).
147 * @dentry_name: Name of the real dentry.
Viresh Kumarf59d3ee2015-09-04 13:47:26 +0530148 *
Viresh Kumar50f8cfb2016-02-09 10:30:37 +0530149 * @voltage_tolerance_v1: In percentage, for v1 bindings only.
150 *
Viresh Kumarf59d3ee2015-09-04 13:47:26 +0530151 * 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 Kumar2c2709d2016-02-16 14:17:53 +0530159struct opp_table {
Viresh Kumarf59d3ee2015-09-04 13:47:26 +0530160 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 Kumar50f8cfb2016-02-09 10:30:37 +0530169
170 /* For backward compatibility with v1 bindings */
171 unsigned int voltage_tolerance_v1;
172
Viresh Kumar79ee2e82016-06-16 19:03:11 +0530173 enum opp_table_access shared_opp;
Viresh Kumarf59d3ee2015-09-04 13:47:26 +0530174 struct dev_pm_opp *suspend_opp;
Viresh Kumardeaa5142015-11-11 07:59:01 +0530175
Viresh Kumar7de36b02015-12-09 08:01:46 +0530176 unsigned int *supported_hw;
177 unsigned int supported_hw_count;
Viresh Kumar01fb4d32015-12-09 08:01:47 +0530178 const char *prop_name;
Viresh Kumard54974c2016-02-09 10:30:38 +0530179 struct clk *clk;
Viresh Kumardfbe4672016-12-01 16:28:19 +0530180 struct regulator **regulators;
181 unsigned int regulator_count;
Viresh Kumar7de36b02015-12-09 08:01:46 +0530182
Viresh Kumar4dab1602016-12-01 16:28:21 +0530183 int (*set_opp)(struct dev_pm_set_opp_data *data);
Viresh Kumar94735582016-12-01 16:28:20 +0530184 struct dev_pm_set_opp_data *set_opp_data;
185
Viresh Kumardeaa5142015-11-11 07:59:01 +0530186#ifdef CONFIG_DEBUG_FS
187 struct dentry *dentry;
188 char dentry_name[NAME_MAX];
189#endif
Viresh Kumarf59d3ee2015-09-04 13:47:26 +0530190};
191
192/* Routines internal to opp core */
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530193struct opp_table *_find_opp_table(struct device *dev);
194struct opp_device *_add_opp_dev(const struct device *dev, struct opp_table *opp_table);
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530195void _dev_pm_opp_remove_table(struct device *dev, bool remove_all);
Viresh Kumar63a69ea2017-01-02 14:40:57 +0530196struct dev_pm_opp *_opp_allocate(struct device *dev, struct opp_table **opp_table);
Viresh Kumar969fceb2017-01-02 14:40:59 +0530197void _opp_free(struct dev_pm_opp *opp, struct opp_table *opp_table);
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530198int _opp_add(struct device *dev, struct dev_pm_opp *new_opp, struct opp_table *opp_table);
Viresh Kumarf47b72a2016-05-05 16:20:33 +0530199int _opp_add_v1(struct device *dev, unsigned long freq, long u_volt, bool dynamic);
200void _dev_pm_opp_cpumask_remove_table(const struct cpumask *cpumask, bool of);
201
202#ifdef CONFIG_OF
203void _of_init_opp_table(struct opp_table *opp_table, struct device *dev);
204#else
205static inline void _of_init_opp_table(struct opp_table *opp_table, struct device *dev) {}
206#endif
Viresh Kumarf59d3ee2015-09-04 13:47:26 +0530207
Viresh Kumardeaa5142015-11-11 07:59:01 +0530208#ifdef CONFIG_DEBUG_FS
209void opp_debug_remove_one(struct dev_pm_opp *opp);
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530210int opp_debug_create_one(struct dev_pm_opp *opp, struct opp_table *opp_table);
211int opp_debug_register(struct opp_device *opp_dev, struct opp_table *opp_table);
212void opp_debug_unregister(struct opp_device *opp_dev, struct opp_table *opp_table);
Viresh Kumardeaa5142015-11-11 07:59:01 +0530213#else
214static inline void opp_debug_remove_one(struct dev_pm_opp *opp) {}
215
216static inline int opp_debug_create_one(struct dev_pm_opp *opp,
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530217 struct opp_table *opp_table)
Viresh Kumardeaa5142015-11-11 07:59:01 +0530218{ return 0; }
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530219static inline int opp_debug_register(struct opp_device *opp_dev,
220 struct opp_table *opp_table)
Viresh Kumardeaa5142015-11-11 07:59:01 +0530221{ return 0; }
222
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530223static inline void opp_debug_unregister(struct opp_device *opp_dev,
224 struct opp_table *opp_table)
Viresh Kumardeaa5142015-11-11 07:59:01 +0530225{ }
226#endif /* DEBUG_FS */
227
Viresh Kumarf59d3ee2015-09-04 13:47:26 +0530228#endif /* __DRIVER_OPP_H__ */