blob: a709655b978cbcc966ca61412dea2be397f7c3cf [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Kevin Hilman01f48d32011-03-21 14:29:13 -07002#include <linux/kernel.h>
3#include <linux/init.h>
Kevin Hilman01f48d32011-03-21 14:29:13 -07004
Tony Lindgren4e653312011-11-10 22:45:17 +01005#include "common.h"
Kevin Hilman01f48d32011-03-21 14:29:13 -07006
7#include "voltage.h"
8#include "vp.h"
9#include "prm-regbits-34xx.h"
10#include "prm-regbits-44xx.h"
11#include "prm44xx.h"
12
Kevin Hilmanb666b472011-07-15 17:05:48 -070013static u32 _vp_set_init_voltage(struct voltagedomain *voltdm, u32 volt)
Kevin Hilman01f48d32011-03-21 14:29:13 -070014{
Kevin Hilmanb7ea8032011-04-04 15:25:07 -070015 struct omap_vp_instance *vp = voltdm->vp;
Kevin Hilman01f48d32011-03-21 14:29:13 -070016 u32 vpconfig;
Kevin Hilman01f48d32011-03-21 14:29:13 -070017 char vsel;
Kevin Hilman01f48d32011-03-21 14:29:13 -070018
Kevin Hilmanb666b472011-07-15 17:05:48 -070019 vsel = voltdm->pmic->uv_to_vsel(volt);
Kevin Hilman01f48d32011-03-21 14:29:13 -070020
Kevin Hilman4bcc4752011-03-28 10:40:15 -070021 vpconfig = voltdm->read(vp->vpconfig);
Kevin Hilmanb7ea8032011-04-04 15:25:07 -070022 vpconfig &= ~(vp->common->vpconfig_initvoltage_mask |
Kevin Hilmanb666b472011-07-15 17:05:48 -070023 vp->common->vpconfig_forceupdate |
24 vp->common->vpconfig_initvdd);
Kevin Hilman0ec30412011-04-04 16:02:28 -070025 vpconfig |= vsel << __ffs(vp->common->vpconfig_initvoltage_mask);
Kevin Hilman4bcc4752011-03-28 10:40:15 -070026 voltdm->write(vpconfig, vp->vpconfig);
Kevin Hilman01f48d32011-03-21 14:29:13 -070027
28 /* Trigger initVDD value copy to voltage processor */
Kevin Hilmanb7ea8032011-04-04 15:25:07 -070029 voltdm->write((vpconfig | vp->common->vpconfig_initvdd),
Kevin Hilman4bcc4752011-03-28 10:40:15 -070030 vp->vpconfig);
Kevin Hilman01f48d32011-03-21 14:29:13 -070031
32 /* Clear initVDD copy trigger bit */
Kevin Hilman4bcc4752011-03-28 10:40:15 -070033 voltdm->write(vpconfig, vp->vpconfig);
Kevin Hilmanb666b472011-07-15 17:05:48 -070034
35 return vpconfig;
Kevin Hilman01f48d32011-03-21 14:29:13 -070036}
37
38/* Generic voltage init functions */
39void __init omap_vp_init(struct voltagedomain *voltdm)
40{
Kevin Hilmanb7ea8032011-04-04 15:25:07 -070041 struct omap_vp_instance *vp = voltdm->vp;
Kevin Hilman667216d2011-04-04 17:58:21 -070042 u32 val, sys_clk_rate, timeout, waittime;
43 u32 vddmin, vddmax, vstepmin, vstepmax;
Kevin Hilman01f48d32011-03-21 14:29:13 -070044
Russell Kingd980e0f2012-02-07 09:42:11 +000045 if (!voltdm->pmic || !voltdm->pmic->uv_to_vsel) {
46 pr_err("%s: No PMIC info for vdd_%s\n", __func__, voltdm->name);
47 return;
48 }
49
Kevin Hilman4bcc4752011-03-28 10:40:15 -070050 if (!voltdm->read || !voltdm->write) {
Kevin Hilman01f48d32011-03-21 14:29:13 -070051 pr_err("%s: No read/write API for accessing vdd_%s regs\n",
52 __func__, voltdm->name);
53 return;
54 }
55
Kevin Hilman6f567272011-07-14 11:10:27 -070056 vp->enabled = false;
57
58 /* Divide to avoid overflow */
59 sys_clk_rate = voltdm->sys_clk.rate / 1000;
60
Kevin Hilman667216d2011-04-04 17:58:21 -070061 timeout = (sys_clk_rate * voltdm->pmic->vp_timeout_us) / 1000;
Tero Kristo085b3022012-09-25 19:33:40 +030062 vddmin = max(voltdm->vp_param->vddmin, voltdm->pmic->vddmin);
63 vddmax = min(voltdm->vp_param->vddmax, voltdm->pmic->vddmax);
64 vddmin = voltdm->pmic->uv_to_vsel(vddmin);
65 vddmax = voltdm->pmic->uv_to_vsel(vddmax);
Kevin Hilman6f567272011-07-14 11:10:27 -070066
Yuan Jiangli3223d002012-03-06 12:51:12 -060067 waittime = DIV_ROUND_UP(voltdm->pmic->step_size * sys_clk_rate,
68 1000 * voltdm->pmic->slew_rate);
Kevin Hilman667216d2011-04-04 17:58:21 -070069 vstepmin = voltdm->pmic->vp_vstepmin;
70 vstepmax = voltdm->pmic->vp_vstepmax;
Kevin Hilman6f567272011-07-14 11:10:27 -070071
Kevin Hilman667216d2011-04-04 17:58:21 -070072 /*
73 * VP_CONFIG: error gain is not set here, it will be updated
74 * on each scale, based on OPP.
75 */
76 val = (voltdm->pmic->vp_erroroffset <<
77 __ffs(voltdm->vp->common->vpconfig_erroroffset_mask)) |
Kevin Hilmanb7ea8032011-04-04 15:25:07 -070078 vp->common->vpconfig_timeouten;
Kevin Hilman667216d2011-04-04 17:58:21 -070079 voltdm->write(val, vp->vpconfig);
Kevin Hilman01f48d32011-03-21 14:29:13 -070080
Kevin Hilman667216d2011-04-04 17:58:21 -070081 /* VSTEPMIN */
82 val = (waittime << vp->common->vstepmin_smpswaittimemin_shift) |
83 (vstepmin << vp->common->vstepmin_stepmin_shift);
84 voltdm->write(val, vp->vstepmin);
Kevin Hilman01f48d32011-03-21 14:29:13 -070085
Kevin Hilman667216d2011-04-04 17:58:21 -070086 /* VSTEPMAX */
87 val = (vstepmax << vp->common->vstepmax_stepmax_shift) |
88 (waittime << vp->common->vstepmax_smpswaittimemax_shift);
89 voltdm->write(val, vp->vstepmax);
Kevin Hilman01f48d32011-03-21 14:29:13 -070090
Kevin Hilman667216d2011-04-04 17:58:21 -070091 /* VLIMITTO */
92 val = (vddmax << vp->common->vlimitto_vddmax_shift) |
93 (vddmin << vp->common->vlimitto_vddmin_shift) |
94 (timeout << vp->common->vlimitto_timeout_shift);
95 voltdm->write(val, vp->vlimitto);
Kevin Hilman01f48d32011-03-21 14:29:13 -070096}
97
Kevin Hilman76ea7422011-04-05 15:15:31 -070098int omap_vp_update_errorgain(struct voltagedomain *voltdm,
99 unsigned long target_volt)
100{
101 struct omap_volt_data *volt_data;
102
Kevin Hilman8798c4ab2011-07-18 15:31:43 -0700103 if (!voltdm->vp)
104 return -EINVAL;
105
Kevin Hilman76ea7422011-04-05 15:15:31 -0700106 /* Get volt_data corresponding to target_volt */
107 volt_data = omap_voltage_get_voltdata(voltdm, target_volt);
108 if (IS_ERR(volt_data))
109 return -EINVAL;
110
111 /* Setting vp errorgain based on the voltage */
112 voltdm->rmw(voltdm->vp->common->vpconfig_errorgain_mask,
113 volt_data->vp_errgain <<
114 __ffs(voltdm->vp->common->vpconfig_errorgain_mask),
115 voltdm->vp->vpconfig);
116
117 return 0;
118}
119
Kevin Hilman01f48d32011-03-21 14:29:13 -0700120/* VP force update method of voltage scaling */
121int omap_vp_forceupdate_scale(struct voltagedomain *voltdm,
122 unsigned long target_volt)
123{
Kevin Hilmanb7ea8032011-04-04 15:25:07 -0700124 struct omap_vp_instance *vp = voltdm->vp;
Kevin Hilman01f48d32011-03-21 14:29:13 -0700125 u32 vpconfig;
126 u8 target_vsel, current_vsel;
127 int ret, timeout = 0;
128
129 ret = omap_vc_pre_scale(voltdm, target_volt, &target_vsel, &current_vsel);
130 if (ret)
131 return ret;
132
133 /*
134 * Clear all pending TransactionDone interrupt/status. Typical latency
135 * is <3us
136 */
137 while (timeout++ < VP_TRANXDONE_TIMEOUT) {
Kevin Hilmanb7ea8032011-04-04 15:25:07 -0700138 vp->common->ops->clear_txdone(vp->id);
139 if (!vp->common->ops->check_txdone(vp->id))
Kevin Hilman01f48d32011-03-21 14:29:13 -0700140 break;
141 udelay(1);
142 }
143 if (timeout >= VP_TRANXDONE_TIMEOUT) {
Nishanth Menon9bb05372012-10-25 13:37:16 -0500144 pr_warn("%s: vdd_%s TRANXDONE timeout exceeded. Voltage change aborted\n",
Paul Walmsley7852ec02012-07-26 00:54:26 -0600145 __func__, voltdm->name);
Kevin Hilman01f48d32011-03-21 14:29:13 -0700146 return -ETIMEDOUT;
147 }
148
Kevin Hilmanb666b472011-07-15 17:05:48 -0700149 vpconfig = _vp_set_init_voltage(voltdm, target_volt);
Kevin Hilman01f48d32011-03-21 14:29:13 -0700150
151 /* Force update of voltage */
Kevin Hilmanb666b472011-07-15 17:05:48 -0700152 voltdm->write(vpconfig | vp->common->vpconfig_forceupdate,
153 voltdm->vp->vpconfig);
Kevin Hilman01f48d32011-03-21 14:29:13 -0700154
155 /*
156 * Wait for TransactionDone. Typical latency is <200us.
157 * Depends on SMPSWAITTIMEMIN/MAX and voltage change
158 */
159 timeout = 0;
Kevin Hilmanb7ea8032011-04-04 15:25:07 -0700160 omap_test_timeout(vp->common->ops->check_txdone(vp->id),
Kevin Hilman01f48d32011-03-21 14:29:13 -0700161 VP_TRANXDONE_TIMEOUT, timeout);
162 if (timeout >= VP_TRANXDONE_TIMEOUT)
Paul Walmsley7852ec02012-07-26 00:54:26 -0600163 pr_err("%s: vdd_%s TRANXDONE timeout exceeded. TRANXDONE never got set after the voltage update\n",
164 __func__, voltdm->name);
Kevin Hilman01f48d32011-03-21 14:29:13 -0700165
166 omap_vc_post_scale(voltdm, target_volt, target_vsel, current_vsel);
167
168 /*
169 * Disable TransactionDone interrupt , clear all status, clear
170 * control registers
171 */
172 timeout = 0;
173 while (timeout++ < VP_TRANXDONE_TIMEOUT) {
Kevin Hilmanb7ea8032011-04-04 15:25:07 -0700174 vp->common->ops->clear_txdone(vp->id);
175 if (!vp->common->ops->check_txdone(vp->id))
Kevin Hilman01f48d32011-03-21 14:29:13 -0700176 break;
177 udelay(1);
178 }
179
180 if (timeout >= VP_TRANXDONE_TIMEOUT)
Paul Walmsley7852ec02012-07-26 00:54:26 -0600181 pr_warn("%s: vdd_%s TRANXDONE timeout exceeded while trying to clear the TRANXDONE status\n",
Kevin Hilman01f48d32011-03-21 14:29:13 -0700182 __func__, voltdm->name);
183
Kevin Hilman01f48d32011-03-21 14:29:13 -0700184 /* Clear force bit */
Kevin Hilman4bcc4752011-03-28 10:40:15 -0700185 voltdm->write(vpconfig, vp->vpconfig);
Kevin Hilman01f48d32011-03-21 14:29:13 -0700186
187 return 0;
188}
189
190/**
Kevin Hilman01f48d32011-03-21 14:29:13 -0700191 * omap_vp_enable() - API to enable a particular VP
192 * @voltdm: pointer to the VDD whose VP is to be enabled.
193 *
194 * This API enables a particular voltage processor. Needed by the smartreflex
195 * class drivers.
196 */
197void omap_vp_enable(struct voltagedomain *voltdm)
198{
Kevin Hilmanb7ea8032011-04-04 15:25:07 -0700199 struct omap_vp_instance *vp;
Kevin Hilmanb666b472011-07-15 17:05:48 -0700200 u32 vpconfig, volt;
Kevin Hilman01f48d32011-03-21 14:29:13 -0700201
202 if (!voltdm || IS_ERR(voltdm)) {
Nishanth Menon9bb05372012-10-25 13:37:16 -0500203 pr_warn("%s: VDD specified does not exist!\n", __func__);
Kevin Hilman01f48d32011-03-21 14:29:13 -0700204 return;
205 }
206
Kevin Hilmanb7ea8032011-04-04 15:25:07 -0700207 vp = voltdm->vp;
Kevin Hilman4bcc4752011-03-28 10:40:15 -0700208 if (!voltdm->read || !voltdm->write) {
Kevin Hilman01f48d32011-03-21 14:29:13 -0700209 pr_err("%s: No read/write API for accessing vdd_%s regs\n",
210 __func__, voltdm->name);
211 return;
212 }
213
214 /* If VP is already enabled, do nothing. Return */
Kevin Hilmanb7ea8032011-04-04 15:25:07 -0700215 if (vp->enabled)
Kevin Hilman01f48d32011-03-21 14:29:13 -0700216 return;
217
Kevin Hilmanb666b472011-07-15 17:05:48 -0700218 volt = voltdm_get_voltage(voltdm);
219 if (!volt) {
Nishanth Menon9bb05372012-10-25 13:37:16 -0500220 pr_warn("%s: unable to find current voltage for %s\n",
221 __func__, voltdm->name);
Kevin Hilmanb666b472011-07-15 17:05:48 -0700222 return;
223 }
224
225 vpconfig = _vp_set_init_voltage(voltdm, volt);
Kevin Hilman01f48d32011-03-21 14:29:13 -0700226
227 /* Enable VP */
Kevin Hilmanb7ea8032011-04-04 15:25:07 -0700228 vpconfig |= vp->common->vpconfig_vpenable;
Kevin Hilman4bcc4752011-03-28 10:40:15 -0700229 voltdm->write(vpconfig, vp->vpconfig);
Kevin Hilmanb666b472011-07-15 17:05:48 -0700230
Kevin Hilmanb7ea8032011-04-04 15:25:07 -0700231 vp->enabled = true;
Kevin Hilman01f48d32011-03-21 14:29:13 -0700232}
233
234/**
235 * omap_vp_disable() - API to disable a particular VP
236 * @voltdm: pointer to the VDD whose VP is to be disabled.
237 *
238 * This API disables a particular voltage processor. Needed by the smartreflex
239 * class drivers.
240 */
241void omap_vp_disable(struct voltagedomain *voltdm)
242{
Kevin Hilmanb7ea8032011-04-04 15:25:07 -0700243 struct omap_vp_instance *vp;
Kevin Hilman01f48d32011-03-21 14:29:13 -0700244 u32 vpconfig;
245 int timeout;
246
247 if (!voltdm || IS_ERR(voltdm)) {
Nishanth Menon9bb05372012-10-25 13:37:16 -0500248 pr_warn("%s: VDD specified does not exist!\n", __func__);
Kevin Hilman01f48d32011-03-21 14:29:13 -0700249 return;
250 }
251
Kevin Hilmanb7ea8032011-04-04 15:25:07 -0700252 vp = voltdm->vp;
Kevin Hilman4bcc4752011-03-28 10:40:15 -0700253 if (!voltdm->read || !voltdm->write) {
Kevin Hilman01f48d32011-03-21 14:29:13 -0700254 pr_err("%s: No read/write API for accessing vdd_%s regs\n",
255 __func__, voltdm->name);
256 return;
257 }
258
259 /* If VP is already disabled, do nothing. Return */
Kevin Hilmanb7ea8032011-04-04 15:25:07 -0700260 if (!vp->enabled) {
Paul Walmsley7852ec02012-07-26 00:54:26 -0600261 pr_warn("%s: Trying to disable VP for vdd_%s when it is already disabled\n",
262 __func__, voltdm->name);
Kevin Hilman01f48d32011-03-21 14:29:13 -0700263 return;
264 }
265
266 /* Disable VP */
Kevin Hilman4bcc4752011-03-28 10:40:15 -0700267 vpconfig = voltdm->read(vp->vpconfig);
Kevin Hilmanb7ea8032011-04-04 15:25:07 -0700268 vpconfig &= ~vp->common->vpconfig_vpenable;
Kevin Hilman4bcc4752011-03-28 10:40:15 -0700269 voltdm->write(vpconfig, vp->vpconfig);
Kevin Hilman01f48d32011-03-21 14:29:13 -0700270
271 /*
272 * Wait for VP idle Typical latency is <2us. Maximum latency is ~100us
273 */
Kevin Hilman4bcc4752011-03-28 10:40:15 -0700274 omap_test_timeout((voltdm->read(vp->vstatus)),
275 VP_IDLE_TIMEOUT, timeout);
Kevin Hilman01f48d32011-03-21 14:29:13 -0700276
277 if (timeout >= VP_IDLE_TIMEOUT)
Nishanth Menon9bb05372012-10-25 13:37:16 -0500278 pr_warn("%s: vdd_%s idle timedout\n", __func__, voltdm->name);
Kevin Hilman01f48d32011-03-21 14:29:13 -0700279
Kevin Hilmanb7ea8032011-04-04 15:25:07 -0700280 vp->enabled = false;
Kevin Hilman01f48d32011-03-21 14:29:13 -0700281
282 return;
283}