blob: 528e9d5a1e723851862bf51f23b5aca2dd0a0281 [file] [log] [blame]
Matt Wagantall33d01f52012-02-23 23:27:44 -08001/*
2 * Copyright (C) 2007 Google, Inc.
Saravana Kannanc85ecf92013-01-21 17:58:35 -08003 * Copyright (c) 2007-2013, The Linux Foundation. All rights reserved.
Matt Wagantall33d01f52012-02-23 23:27:44 -08004 *
5 * This software is licensed under the terms of the GNU General Public
6 * License version 2, as published by the Free Software Foundation, and
7 * may be copied, distributed, and modified under those terms.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 */
15
16#ifndef __MACH_CLK_PROVIDER_H
17#define __MACH_CLK_PROVIDER_H
18
19#include <linux/types.h>
20#include <linux/list.h>
21#include <linux/clkdev.h>
22#include <linux/spinlock.h>
Stephen Boyda1f610a2012-09-22 00:40:37 -070023#include <linux/mutex.h>
Patrick Dalyebc26bc2013-02-05 11:49:07 -080024#include <linux/regulator/consumer.h>
Matt Wagantall33d01f52012-02-23 23:27:44 -080025#include <mach/clk.h>
26
27/*
28 * Bit manipulation macros
29 */
30#define BM(msb, lsb) (((((uint32_t)-1) << (31-msb)) >> (31-msb+lsb)) << lsb)
31#define BVAL(msb, lsb, val) (((val) << lsb) & BM(msb, lsb))
32
33/*
34 * Halt/Status Checking Mode Macros
35 */
36#define HALT 0 /* Bit pol: 1 = halted */
37#define NOCHECK 1 /* No bit to check, do nothing */
38#define HALT_VOTED 2 /* Bit pol: 1 = halted; delay on disable */
39#define ENABLE 3 /* Bit pol: 1 = running */
40#define ENABLE_VOTED 4 /* Bit pol: 1 = running; delay on disable */
41#define DELAY 5 /* No bit to check, just delay */
42
Matt Wagantall33d01f52012-02-23 23:27:44 -080043/**
44 * struct clk_vdd_class - Voltage scaling class
45 * @class_name: name of the class
Patrick Dalyebc26bc2013-02-05 11:49:07 -080046 * @regulator: array of regulators.
47 * @num_regulators: size of regulator array. Standard regulator APIs will be
48 used if this field > 0.
49 * @set_vdd: function to call when applying a new voltage setting.
50 * @vdd_uv: sorted 2D array of legal voltage settings. Indexed by level, then
51 regulator.
52 * @level_votes: array of votes for each level.
53 * @num_levels: specifies the size of level_votes array.
Matt Wagantall33d01f52012-02-23 23:27:44 -080054 * @cur_level: the currently set voltage level
55 * @lock: lock to protect this struct
56 */
57struct clk_vdd_class {
58 const char *class_name;
Patrick Dalyebc26bc2013-02-05 11:49:07 -080059 struct regulator **regulator;
60 int num_regulators;
Matt Wagantall33d01f52012-02-23 23:27:44 -080061 int (*set_vdd)(struct clk_vdd_class *v_class, int level);
Patrick Dalyebc26bc2013-02-05 11:49:07 -080062 const int **vdd_uv;
Saravana Kannan55e959d2012-10-15 22:16:04 -070063 int *level_votes;
64 int num_levels;
Matt Wagantall33d01f52012-02-23 23:27:44 -080065 unsigned long cur_level;
Stephen Boyda1f610a2012-09-22 00:40:37 -070066 struct mutex lock;
Matt Wagantall33d01f52012-02-23 23:27:44 -080067};
68
Saravana Kannan55e959d2012-10-15 22:16:04 -070069#define DEFINE_VDD_CLASS(_name, _set_vdd, _num_levels) \
Matt Wagantall33d01f52012-02-23 23:27:44 -080070 struct clk_vdd_class _name = { \
71 .class_name = #_name, \
72 .set_vdd = _set_vdd, \
Saravana Kannan55e959d2012-10-15 22:16:04 -070073 .level_votes = (int [_num_levels]) {}, \
74 .num_levels = _num_levels, \
75 .cur_level = _num_levels, \
Stephen Boyda1f610a2012-09-22 00:40:37 -070076 .lock = __MUTEX_INITIALIZER(_name.lock) \
Matt Wagantall33d01f52012-02-23 23:27:44 -080077 }
78
Patrick Dalyebc26bc2013-02-05 11:49:07 -080079#define DEFINE_VDD_REGULATORS(_name, _num_levels, _num_regulators, _vdd_uv) \
80 struct clk_vdd_class _name = { \
81 .class_name = #_name, \
82 .vdd_uv = _vdd_uv, \
83 .regulator = (struct regulator * [_num_regulators]) {}, \
84 .num_regulators = _num_regulators, \
85 .level_votes = (int [_num_levels]) {}, \
86 .num_levels = _num_levels, \
87 .cur_level = _num_levels, \
88 .lock = __MUTEX_INITIALIZER(_name.lock) \
89 }
90
91#define VDD_UV(...) ((int []){__VA_ARGS__})
92
Matt Wagantall33d01f52012-02-23 23:27:44 -080093enum handoff {
94 HANDOFF_ENABLED_CLK,
95 HANDOFF_DISABLED_CLK,
Matt Wagantall33d01f52012-02-23 23:27:44 -080096};
97
98struct clk_ops {
99 int (*prepare)(struct clk *clk);
100 int (*enable)(struct clk *clk);
101 void (*disable)(struct clk *clk);
102 void (*unprepare)(struct clk *clk);
103 void (*enable_hwcg)(struct clk *clk);
104 void (*disable_hwcg)(struct clk *clk);
105 int (*in_hwcg_mode)(struct clk *clk);
106 enum handoff (*handoff)(struct clk *clk);
107 int (*reset)(struct clk *clk, enum clk_reset_action action);
108 int (*set_rate)(struct clk *clk, unsigned long rate);
109 int (*set_max_rate)(struct clk *clk, unsigned long rate);
110 int (*set_flags)(struct clk *clk, unsigned flags);
111 unsigned long (*get_rate)(struct clk *clk);
112 int (*list_rate)(struct clk *clk, unsigned n);
113 int (*is_enabled)(struct clk *clk);
114 long (*round_rate)(struct clk *clk, unsigned long rate);
115 int (*set_parent)(struct clk *clk, struct clk *parent);
116 struct clk *(*get_parent)(struct clk *clk);
117 bool (*is_local)(struct clk *clk);
118};
119
120/**
121 * struct clk
122 * @prepare_count: prepare refcount
123 * @prepare_lock: protects clk_prepare()/clk_unprepare() path and @prepare_count
124 * @count: enable refcount
125 * @lock: protects clk_enable()/clk_disable() path and @count
126 * @depends: non-direct parent of clock to enable when this clock is enabled
127 * @vdd_class: voltage scaling requirement class
128 * @fmax: maximum frequency in Hz supported at each voltage level
Saravana Kannan7a6532e2012-10-18 20:51:13 -0700129 * @parent: the current source of this clock
Matt Wagantall33d01f52012-02-23 23:27:44 -0800130 */
131struct clk {
132 uint32_t flags;
133 struct clk_ops *ops;
134 const char *dbg_name;
135 struct clk *depends;
136 struct clk_vdd_class *vdd_class;
Saravana Kannan55e959d2012-10-15 22:16:04 -0700137 unsigned long *fmax;
138 int num_fmax;
Matt Wagantall33d01f52012-02-23 23:27:44 -0800139 unsigned long rate;
Saravana Kannan7a6532e2012-10-18 20:51:13 -0700140 struct clk *parent;
Matt Wagantall33d01f52012-02-23 23:27:44 -0800141
142 struct list_head children;
143 struct list_head siblings;
144
145 unsigned count;
146 spinlock_t lock;
147 unsigned prepare_count;
148 struct mutex prepare_lock;
149};
150
151#define CLK_INIT(name) \
152 .lock = __SPIN_LOCK_UNLOCKED((name).lock), \
153 .prepare_lock = __MUTEX_INITIALIZER((name).prepare_lock), \
154 .children = LIST_HEAD_INIT((name).children), \
155 .siblings = LIST_HEAD_INIT((name).siblings)
156
157int vote_vdd_level(struct clk_vdd_class *vdd_class, int level);
158int unvote_vdd_level(struct clk_vdd_class *vdd_class, int level);
159
160/* Register clocks with the MSM clock driver */
161int msm_clock_register(struct clk_lookup *table, size_t size);
162
163extern struct clk dummy_clk;
164
165#define CLK_DUMMY(clk_name, clk_id, clk_dev, flags) { \
166 .con_id = clk_name, \
167 .dev_id = clk_dev, \
168 .clk = &dummy_clk, \
169 }
170
171#define CLK_LOOKUP(con, c, dev) { .con_id = con, .clk = &c, .dev_id = dev }
172
173#endif