blob: 484761252b31a1a1013e562a37933fbf07e03f33 [file] [log] [blame]
Subbaraman Narayanamurthy8f1ffa52011-06-03 12:33:01 -07001/*
2 * * Copyright (c) 2011, Code Aurora Forum. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above
10 * copyright notice, this list of conditions and the following
11 * disclaimer in the documentation and/or other materials provided
12 * with the distribution.
13 * * Neither the name of Code Aurora Forum, Inc. nor the names of its
14 * contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
24 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
26 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30#include <debug.h>
31#include <platform/pmic.h>
32#include <platform/pmic_pwm.h>
33
34static char *clks[NUM_CLOCKS] = {
35 "1K", "32768", "19.2M"
36};
37
38static unsigned pre_div[NUM_PRE_DIVIDE] = {
39 PRE_DIVIDE_0, PRE_DIVIDE_1, PRE_DIVIDE_2
40};
41
42static unsigned int pt_t[NUM_PRE_DIVIDE][NUM_CLOCKS] = {
43 { PRE_DIVIDE_0 * NSEC_1000HZ,
44 PRE_DIVIDE_0 * NSEC_32768HZ,
45 PRE_DIVIDE_0 * NSEC_19P2MHZ,
46 },
47 { PRE_DIVIDE_1 * NSEC_1000HZ,
48 PRE_DIVIDE_1 * NSEC_32768HZ,
49 PRE_DIVIDE_1 * NSEC_19P2MHZ,
50 },
51 { PRE_DIVIDE_2 * NSEC_1000HZ,
52 PRE_DIVIDE_2 * NSEC_32768HZ,
53 PRE_DIVIDE_2 * NSEC_19P2MHZ,
54 },
55};
56
57static uint16_t duty_msec[PM_PWM_1KHZ_COUNT_MAX + 1] = {
58 0, 1, 2, 3, 4, 6, 8, 16, 18, 24, 32, 36, 64, 128, 256, 512
59};
60
61static uint16_t pause_count[PM_PWM_PAUSE_COUNT_MAX + 1] = {
62 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
63 23, 28, 31, 42, 47, 56, 63, 83, 94, 111, 125, 167, 188, 222, 250, 333,
64 375, 500, 667, 750, 800, 900, 1000, 1100,
65 1200, 1300, 1400, 1500, 1600, 1800, 2000, 2500,
66 3000, 3500, 4000, 4500, 5000, 5500, 6000, 6500,
67 7000
68};
69
70/* Function to get the PWM size, divider, clock for the given period */
71
72static void pm_pwm_calc_period(uint32_t period_us,
73 struct pm_pwm_config *pwm_conf)
74{
75 int n, m, clk, div;
76 int best_m, best_div, best_clk;
77 int last_err, cur_err, better_err, better_m;
78 uint32_t tmp_p, last_p, min_err, period_n;
79
80 /* PWM Period / N : handle underflow or overflow */
81 if (period_us < (PM_PWM_PERIOD_MAX / NSEC_PER_USEC))
82 period_n = (period_us * NSEC_PER_USEC) >> 6;
83 else
84 period_n = (period_us >> 6) * NSEC_PER_USEC;
85 if (period_n >= MAX_MPT) {
86 n = 9;
87 period_n >>= 3;
88 } else
89 n = 6;
90
91 min_err = MAX_MPT;
92 best_m = 0;
93 best_clk = 0;
94 best_div = 0;
95 for (clk = 0; clk < NUM_CLOCKS; clk++) {
96 for (div = 0; div < NUM_PRE_DIVIDE; div++) {
97 tmp_p = period_n;
98 last_p = tmp_p;
99 for (m = 0; m <= PM_PWM_M_MAX; m++) {
100 if (tmp_p <= pt_t[div][clk]) {
101 /* Found local best */
102 if (!m) {
103 better_err = pt_t[div][clk] - tmp_p;
104 better_m = m;
105 } else {
106 last_err = last_p - pt_t[div][clk];
107 cur_err = pt_t[div][clk] - tmp_p;
108
109 if (cur_err < last_err) {
110 better_err = cur_err;
111 better_m = m;
112 } else {
113 better_err = last_err;
114 better_m = m - 1;
115 }
116 }
117
118 if (better_err < min_err) {
119 min_err = better_err;
120 best_m = better_m;
121 best_clk = clk;
122 best_div = div;
123 }
124 break;
125 } else {
126 last_p = tmp_p;
127 tmp_p >>= 1;
128 }
129 }
130 }
131 }
132
133 pwm_conf->pwm_size = n;
134 pwm_conf->clk = best_clk;
135 pwm_conf->pre_div = best_div;
136 pwm_conf->pre_div_exp = best_m;
137}
138
139/* Function to configure PWM control registers with clock, divider values */
140
141static int pm_pwm_configure(uint8_t pwm_id, struct pm_pwm_config *pwm_conf)
142{
143 int i, len, rc = -1;
144 uint8_t reg;
145
146 reg = (pwm_conf->pwm_size > 6) ? PM_PWM_SIZE_9_BIT : 0;
147 pwm_conf->pwm_ctl[5] = reg;
148
149 reg = ((pwm_conf->clk + 1) << PM_PWM_CLK_SEL_SHIFT)
150 & PM_PWM_CLK_SEL_MASK;
151 reg |= (pwm_conf->pre_div << PM_PWM_PREDIVIDE_SHIFT)
152 & PM_PWM_PREDIVIDE_MASK;
153 reg |= pwm_conf->pre_div_exp & PM_PWM_M_MASK;
154 pwm_conf->pwm_ctl[4] = reg;
155
156 /* Just to let know we bypass LUT */
157 if (pwm_conf->bypass_lut) {
158 /* CTL0 is set in pwm_enable() */
159 pwm_conf->pwm_ctl[0] &= PM_PWM_PWM_START;
160 pwm_conf->pwm_ctl[1] = PM_PWM_BYPASS_LUT;
161 pwm_conf->pwm_ctl[2] = 0;
162
163 if (pwm_conf->pwm_size > 6) {
164 pwm_conf->pwm_ctl[3] = pwm_conf->pwm_value
165 & PM_PWM_VALUE_BIT7_0;
166 pwm_conf->pwm_ctl[4] |= (pwm_conf->pwm_value >> 1)
167 & PM_PWM_VALUE_BIT8;
168 } else {
169 pwm_conf->pwm_ctl[3] = pwm_conf->pwm_value
170 & PM_PWM_VALUE_BIT5_0;
171 }
172
173 len = 6;
174 }
175 else
176 {
177 /* Right now, we are not using LUT */
178 goto bail_out;
179 }
180
181 /* Selecting the bank */
182 rc = pm8058_write(PM8058_LPG_BANK_SEL, &pwm_id, 1);
183 if(rc)
184 goto bail_out;
185
186 for (i = 0; i < len; i++) {
187 rc = pm8058_write(PM8058_LPG_CTL(i),&pwm_conf->pwm_ctl[i], 1);
188 if (rc) {
189 dprintf(CRITICAL,"pm8058_write() failed in pwm_configure %d\n", rc);
190 break;
191 }
192 }
193
194bail_out:
195 if(rc)
196 dprintf(CRITICAL,"Error in pm_pwm_configure()\n");
197 return rc;
198}
199
200/* Top level function for configuring PWM */
201
202int pm_pwm_config(uint8_t pwm_id, uint32_t duty_us, uint32_t period_us)
203{
204 struct pm_pwm_config pwm_conf;
205 uint32_t max_pwm_value, tmp;
206 int rc = -1;
207
208 if((duty_us > period_us) || (period_us > PM_PWM_PERIOD_MAX) ||
209 (period_us < PM_PWM_PERIOD_MIN))
210 {
211 dprintf(CRITICAL,"Error in duty cycle and period\n");
212 return -1;
213 }
214
215 pm_pwm_calc_period(period_us, &pwm_conf);
216
217 /* Figure out pwm_value with overflow handling */
218 if (period_us > (1 << pwm_conf.pwm_size)) {
219 tmp = period_us;
220 tmp >>= pwm_conf.pwm_size;
221 pwm_conf.pwm_value = duty_us / tmp;
222 } else {
223 tmp = duty_us;
224 tmp <<= pwm_conf.pwm_size;
225 pwm_conf.pwm_value = tmp / period_us;
226 }
227 max_pwm_value = (1 << pwm_conf.pwm_size) - 1;
228 if (pwm_conf.pwm_value > max_pwm_value)
229 pwm_conf.pwm_value = max_pwm_value;
230
231 /* Bypassing LUT */
232 pwm_conf.bypass_lut = 1;
233
234 dprintf(SPEW,"duty/period=%u/%u usec: pwm_value=%d (of %d)\n",
235 duty_us, period_us,pwm_conf.pwm_value,
236 1 << pwm_conf.pwm_size);
237
238 rc = pm_pwm_configure(pwm_id, &pwm_conf);
239 if(rc)
240 dprintf(CRITICAL,"Error in pwm_config()\n");
241
242 return rc;
243}
244
245/* Top level function to enable PWM with specified id */
246
247int pm_pwm_enable(uint8_t pwm_id)
248{
249 int rc = -1;
250 uint8_t reg;
251
252 /* Read it before enabling other bank */
253 rc = pm8058_read(PM8058_LPG_BANK_ENABLE, &reg, 1);
254 if(rc)
255 goto bail_out;
256
257 reg |= (1 << pwm_id);
258
259 rc = pm8058_write(PM8058_LPG_BANK_ENABLE,&reg,1);
260 if(rc)
261 goto bail_out;
262
263 /* Selecting the bank */
264 rc = pm8058_write(PM8058_LPG_BANK_SEL, &pwm_id, 1);
265 if(rc)
266 goto bail_out;
267
268 /* Read it before setting PWM start */
269 rc = pm8058_read(PM8058_LPG_CTL(0), &reg, 1);
270 if(rc)
271 goto bail_out;
272
273 reg |= PM_PWM_PWM_START;
274 reg &= ~PM_PWM_RAMP_GEN_START;
275 rc = pm8058_write(PM8058_LPG_CTL(0),&reg, 1);
276
277bail_out:
278 if(rc)
279 dprintf(CRITICAL, "Error in pwm_enable()\n");
280 return rc;
281}