blob: 4c0df470417a313f0f2bfb23d0ec19b6a8c4c75c [file] [log] [blame]
Deepa Dinamani0bb64ff2011-12-06 10:45:44 -08001/*
Duy Truongf3ac7b32013-02-13 01:07:28 -08002 * * Copyright (c) 2011, The Linux Foundation. All rights reserved.
Deepa Dinamani0bb64ff2011-12-06 10:45:44 -08003 *
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.
Duy Truongf3ac7b32013-02-13 01:07:28 -080013 * * Neither the name of The Linux Foundation nor the names of its
Deepa Dinamani0bb64ff2011-12-06 10:45:44 -080014 * 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 <dev/pm8921.h>
32#include <dev/pm8921_pwm.h>
33
34
35static char *clks[NUM_CLOCKS] = {
36 "1K", "32768", "19.2M"
37};
38
39static unsigned pre_div[NUM_PRE_DIVIDE] = {
40 PRE_DIVIDE_0, PRE_DIVIDE_1, PRE_DIVIDE_2, PRE_DIVIDE_3
41};
42
43static unsigned int pt_t[NUM_PRE_DIVIDE][NUM_CLOCKS] = {
44 { PRE_DIVIDE_0 * NSEC_1000HZ,
45 PRE_DIVIDE_0 * NSEC_32768HZ,
46 PRE_DIVIDE_0 * NSEC_19P2MHZ,
47 },
48 { PRE_DIVIDE_1 * NSEC_1000HZ,
49 PRE_DIVIDE_1 * NSEC_32768HZ,
50 PRE_DIVIDE_1 * NSEC_19P2MHZ,
51 },
52 { PRE_DIVIDE_2 * NSEC_1000HZ,
53 PRE_DIVIDE_2 * NSEC_32768HZ,
54 PRE_DIVIDE_2 * NSEC_19P2MHZ,
55 },
56
57 { PRE_DIVIDE_2 * NSEC_1000HZ,
58 PRE_DIVIDE_2 * NSEC_32768HZ,
59 PRE_DIVIDE_2 * NSEC_19P2MHZ,
60 },
61};
62
63static uint16_t duty_msec[PM_PWM_1KHZ_COUNT_MAX + 1] = {
64 0, 1, 2, 3, 4, 6, 8, 16, 18, 24, 32, 36, 64, 128, 256, 512
65};
66
67static uint16_t pause_count[PM_PWM_PAUSE_COUNT_MAX + 1] = {
68 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
69 23, 28, 31, 42, 47, 56, 63, 83, 94, 111, 125, 167, 188, 222, 250, 333,
70 375, 500, 667, 750, 800, 900, 1000, 1100,
71 1200, 1300, 1400, 1500, 1600, 1800, 2000, 2500,
72 3000, 3500, 4000, 4500, 5000, 5500, 6000, 6500,
73 7000
74};
75
76/* Function to get the PWM size, divider, clock for the given period */
77
Deepa Dinamaniad4752a2011-12-08 17:51:21 -080078static void pm8921_pwm_calc_period(uint32_t period_us,
79 struct pm8921_pwm_config *pwm_conf)
Deepa Dinamani0bb64ff2011-12-06 10:45:44 -080080{
81 int n, m, clk, div;
82 int best_m, best_div, best_clk;
83 int last_err, cur_err, better_err, better_m;
84 uint32_t tmp_p, last_p, min_err, period_n;
85
86 /* PWM Period / N : handle underflow or overflow */
87 if (period_us < (PM_PWM_PERIOD_MAX / NSEC_PER_USEC))
88 period_n = (period_us * NSEC_PER_USEC) >> 6;
89 else
90 period_n = (period_us >> 6) * NSEC_PER_USEC;
91
92 if (period_n >= MAX_MPT)
93 {
94 n = 9;
95 period_n >>= 3;
96 }
97 else
98 n = 6;
99
100 min_err = MAX_MPT;
101 best_m = 0;
102 best_clk = 0;
103 best_div = 0;
104
105 for (clk = 0; clk < NUM_CLOCKS; clk++)
106 {
107 for (div = 0; div < NUM_PRE_DIVIDE; div++)
108 {
109 tmp_p = period_n;
110 last_p = tmp_p;
111 for (m = 0; m <= PM_PWM_M_MAX; m++)
112 {
113 if (tmp_p <= pt_t[div][clk])
114 {
115 /* Found local best */
116 if (!m)
117 {
118 better_err = pt_t[div][clk] - tmp_p;
119 better_m = m;
120 }
121 else
122 {
123 last_err = last_p - pt_t[div][clk];
124 cur_err = pt_t[div][clk] - tmp_p;
125
126 if (cur_err < last_err)
127 {
128 better_err = cur_err;
129 better_m = m;
130 }
131 else
132 {
133 better_err = last_err;
134 better_m = m - 1;
135 }
136 }
137
138 if (better_err < min_err)
139 {
140 min_err = better_err;
141 best_m = better_m;
142 best_clk = clk;
143 best_div = div;
144 }
145 break;
146 }
147 else
148 {
149 last_p = tmp_p;
150 tmp_p >>= 1;
151 }
152 }
153 }
154 }
155
156 pwm_conf->pwm_size = n;
157 pwm_conf->clk = best_clk;
158 pwm_conf->pre_div = best_div;
159 pwm_conf->pre_div_exp = best_m;
160}
161
162/* Function to configure PWM control registers with clock, divider values */
163
164static int pm8921_pwm_configure(uint8_t pwm_id,
Deepa Dinamaniad4752a2011-12-08 17:51:21 -0800165 struct pm8921_pwm_config *pwm_conf,
Deepa Dinamani0bb64ff2011-12-06 10:45:44 -0800166 pm8921_dev_t *dev)
167{
168 int i, len, rc = -1;
169 uint8_t reg;
170
171 reg = (pwm_conf->pwm_size > 6) ? PM_PWM_SIZE_9_BIT : 0;
172 pwm_conf->pwm_ctl[5] = reg;
173
174 reg = ((pwm_conf->clk + 1) << PM_PWM_CLK_SEL_SHIFT)
175 & PM_PWM_CLK_SEL_MASK;
176 reg |= (pwm_conf->pre_div << PM_PWM_PREDIVIDE_SHIFT)
177 & PM_PWM_PREDIVIDE_MASK;
178 reg |= pwm_conf->pre_div_exp & PM_PWM_M_MASK;
179 pwm_conf->pwm_ctl[4] = reg;
180
181 /* Just to let know we bypass LUT */
182 if (pwm_conf->bypass_lut)
183 {
184 /* CTL0 is set in pwm_enable() */
185 pwm_conf->pwm_ctl[0] &= PM_PWM_PWM_START;
186 pwm_conf->pwm_ctl[1] = PM_PWM_BYPASS_LUT;
187 pwm_conf->pwm_ctl[2] = 0;
188
189 if (pwm_conf->pwm_size > 6)
190 {
191 pwm_conf->pwm_ctl[3] = pwm_conf->pwm_value
192 & PM_PWM_VALUE_BIT7_0;
193 pwm_conf->pwm_ctl[4] |= (pwm_conf->pwm_value >> 1)
194 & PM_PWM_VALUE_BIT8;
195 }
196 else
197 {
198 pwm_conf->pwm_ctl[3] = pwm_conf->pwm_value
199 & PM_PWM_VALUE_BIT5_0;
200 }
201
202 len = 6;
203 }
204 else
205 {
206 /* Right now, we are not using LUT */
207 goto bail_out;
208 }
209
210 /* Selecting the bank */
211 rc = dev->write(&pwm_id, 1, PM8921_LPG_BANK_SEL);
212 if (rc)
213 goto bail_out;
214
215 for (i = 0; i < len; i++)
216 {
217 rc = dev->write(&pwm_conf->pwm_ctl[i], 1, PM8921_LPG_CTL(i));
218 if (rc)
219 {
220 dprintf(CRITICAL, "pm8921_write() failed in \
221 pwm_configure %d\n", rc);
222 break;
223 }
224 }
225
226bail_out:
227 if (rc)
Deepa Dinamaniad4752a2011-12-08 17:51:21 -0800228 dprintf(CRITICAL, "Error in pm8921_pwm_configure()\n");
Deepa Dinamani0bb64ff2011-12-06 10:45:44 -0800229 return rc;
230}
231
232/* Top level function for configuring PWM
233 * Always called from the main pm8921.c file
234 */
235
236int pm8921_pwm_config(uint8_t pwm_id,
237 uint32_t duty_us,
238 uint32_t period_us,
239 pm8921_dev_t *dev)
240{
Deepa Dinamaniad4752a2011-12-08 17:51:21 -0800241 struct pm8921_pwm_config pwm_conf;
Deepa Dinamani0bb64ff2011-12-06 10:45:44 -0800242 uint32_t max_pwm_value, tmp;
243 int rc = -1;
244
245 if ((duty_us > period_us) || (period_us > PM_PWM_PERIOD_MAX) ||
246 (period_us < PM_PWM_PERIOD_MIN))
247 {
248 dprintf(CRITICAL, "Error in duty cycle and period\n");
249 return -1;
250 }
251
Deepa Dinamaniad4752a2011-12-08 17:51:21 -0800252 pm8921_pwm_calc_period(period_us, &pwm_conf);
Deepa Dinamani0bb64ff2011-12-06 10:45:44 -0800253
254 /* Figure out pwm_value with overflow handling */
255 if (period_us > (1 << pwm_conf.pwm_size))
256 {
257 tmp = period_us;
258 tmp >>= pwm_conf.pwm_size;
259 pwm_conf.pwm_value = duty_us / tmp;
260 }
261 else
262 {
263 tmp = duty_us;
264 tmp <<= pwm_conf.pwm_size;
265 pwm_conf.pwm_value = tmp / period_us;
266 }
267
268 max_pwm_value = (1 << pwm_conf.pwm_size) - 1;
269
270 if (pwm_conf.pwm_value > max_pwm_value)
271 pwm_conf.pwm_value = max_pwm_value;
272
273 /* Bypassing LUT */
274 pwm_conf.bypass_lut = 1;
275
276 dprintf(SPEW, "duty/period=%u/%u usec: pwm_value=%d (of %d)\n",
277 duty_us, period_us, pwm_conf.pwm_value,
278 1 << pwm_conf.pwm_size);
279
Deepa Dinamaniad4752a2011-12-08 17:51:21 -0800280 rc = pm8921_pwm_configure(pwm_id, &pwm_conf, dev);
Deepa Dinamani0bb64ff2011-12-06 10:45:44 -0800281
282 if (rc)
283 dprintf(CRITICAL, "Error in pwm_config()\n");
284
285 return rc;
286}
287
288/* Top level function to enable PWM with specified id
289 * Always called from the main pm8921.c file
290 */
291
292
293int pm8921_pwm_enable(uint8_t pwm_id, pm8921_dev_t *dev)
294{
295 int rc = -1;
296 uint8_t reg;
297
298 /* Read it before enabling other bank */
299 rc = dev->read(&reg, 1, PM8921_LPG_BANK_ENABLE);
300 if (rc)
301 goto bail_out;
302
303 reg |= (1 << pwm_id);
304
305 rc = dev->write(&reg, 1, PM8921_LPG_BANK_ENABLE);
306 if (rc)
307 goto bail_out;
308
309 /* Selecting the bank */
310 rc = dev->write(&pwm_id, 1, PM8921_LPG_BANK_SEL);
311 if (rc)
312 goto bail_out;
313
314 /* Read it before setting PWM start */
315 rc = dev->read(&reg, 1, PM8921_LPG_CTL(0));
316 if (rc)
317 goto bail_out;
318
319 reg |= PM_PWM_PWM_START;
320 reg &= ~PM_PWM_RAMP_GEN_START;
321 rc = dev->write(&reg, 1, PM8921_LPG_CTL(0));
322
323bail_out:
324 if (rc)
325 dprintf(CRITICAL, "Error in pwm_enable()\n");
326 return rc;
327}