Subbaraman Narayanamurthy | 8f1ffa5 | 2011-06-03 12:33:01 -0700 | [diff] [blame] | 1 | /* |
Duy Truong | f3ac7b3 | 2013-02-13 01:07:28 -0800 | [diff] [blame] | 2 | * * Copyright (c) 2011, The Linux Foundation. All rights reserved. |
Subbaraman Narayanamurthy | 8f1ffa5 | 2011-06-03 12:33:01 -0700 | [diff] [blame] | 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. |
Duy Truong | f3ac7b3 | 2013-02-13 01:07:28 -0800 | [diff] [blame] | 13 | * * Neither the name of The Linux Foundation nor the names of its |
Subbaraman Narayanamurthy | 8f1ffa5 | 2011-06-03 12:33:01 -0700 | [diff] [blame] | 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 | |
| 34 | static char *clks[NUM_CLOCKS] = { |
| 35 | "1K", "32768", "19.2M" |
| 36 | }; |
| 37 | |
| 38 | static unsigned pre_div[NUM_PRE_DIVIDE] = { |
| 39 | PRE_DIVIDE_0, PRE_DIVIDE_1, PRE_DIVIDE_2 |
| 40 | }; |
| 41 | |
| 42 | static unsigned int pt_t[NUM_PRE_DIVIDE][NUM_CLOCKS] = { |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 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 | }, |
Subbaraman Narayanamurthy | 8f1ffa5 | 2011-06-03 12:33:01 -0700 | [diff] [blame] | 55 | }; |
| 56 | |
| 57 | static 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 | |
| 61 | static 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 | |
| 72 | static void pm_pwm_calc_period(uint32_t period_us, |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 73 | struct pm_pwm_config *pwm_conf) |
Subbaraman Narayanamurthy | 8f1ffa5 | 2011-06-03 12:33:01 -0700 | [diff] [blame] | 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) { |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 103 | better_err = |
| 104 | pt_t[div][clk] - tmp_p; |
Subbaraman Narayanamurthy | 8f1ffa5 | 2011-06-03 12:33:01 -0700 | [diff] [blame] | 105 | better_m = m; |
| 106 | } else { |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 107 | last_err = |
| 108 | last_p - pt_t[div][clk]; |
| 109 | cur_err = |
| 110 | pt_t[div][clk] - tmp_p; |
Subbaraman Narayanamurthy | 8f1ffa5 | 2011-06-03 12:33:01 -0700 | [diff] [blame] | 111 | |
| 112 | if (cur_err < last_err) { |
| 113 | better_err = cur_err; |
| 114 | better_m = m; |
| 115 | } else { |
| 116 | better_err = last_err; |
| 117 | better_m = m - 1; |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | if (better_err < min_err) { |
| 122 | min_err = better_err; |
| 123 | best_m = better_m; |
| 124 | best_clk = clk; |
| 125 | best_div = div; |
| 126 | } |
| 127 | break; |
| 128 | } else { |
| 129 | last_p = tmp_p; |
| 130 | tmp_p >>= 1; |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | pwm_conf->pwm_size = n; |
| 137 | pwm_conf->clk = best_clk; |
| 138 | pwm_conf->pre_div = best_div; |
| 139 | pwm_conf->pre_div_exp = best_m; |
| 140 | } |
| 141 | |
| 142 | /* Function to configure PWM control registers with clock, divider values */ |
| 143 | |
| 144 | static int pm_pwm_configure(uint8_t pwm_id, struct pm_pwm_config *pwm_conf) |
| 145 | { |
| 146 | int i, len, rc = -1; |
| 147 | uint8_t reg; |
| 148 | |
| 149 | reg = (pwm_conf->pwm_size > 6) ? PM_PWM_SIZE_9_BIT : 0; |
| 150 | pwm_conf->pwm_ctl[5] = reg; |
| 151 | |
| 152 | reg = ((pwm_conf->clk + 1) << PM_PWM_CLK_SEL_SHIFT) |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 153 | & PM_PWM_CLK_SEL_MASK; |
Subbaraman Narayanamurthy | 8f1ffa5 | 2011-06-03 12:33:01 -0700 | [diff] [blame] | 154 | reg |= (pwm_conf->pre_div << PM_PWM_PREDIVIDE_SHIFT) |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 155 | & PM_PWM_PREDIVIDE_MASK; |
Subbaraman Narayanamurthy | 8f1ffa5 | 2011-06-03 12:33:01 -0700 | [diff] [blame] | 156 | reg |= pwm_conf->pre_div_exp & PM_PWM_M_MASK; |
| 157 | pwm_conf->pwm_ctl[4] = reg; |
| 158 | |
| 159 | /* Just to let know we bypass LUT */ |
| 160 | if (pwm_conf->bypass_lut) { |
| 161 | /* CTL0 is set in pwm_enable() */ |
| 162 | pwm_conf->pwm_ctl[0] &= PM_PWM_PWM_START; |
| 163 | pwm_conf->pwm_ctl[1] = PM_PWM_BYPASS_LUT; |
| 164 | pwm_conf->pwm_ctl[2] = 0; |
| 165 | |
| 166 | if (pwm_conf->pwm_size > 6) { |
| 167 | pwm_conf->pwm_ctl[3] = pwm_conf->pwm_value |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 168 | & PM_PWM_VALUE_BIT7_0; |
Subbaraman Narayanamurthy | 8f1ffa5 | 2011-06-03 12:33:01 -0700 | [diff] [blame] | 169 | pwm_conf->pwm_ctl[4] |= (pwm_conf->pwm_value >> 1) |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 170 | & PM_PWM_VALUE_BIT8; |
Subbaraman Narayanamurthy | 8f1ffa5 | 2011-06-03 12:33:01 -0700 | [diff] [blame] | 171 | } else { |
| 172 | pwm_conf->pwm_ctl[3] = pwm_conf->pwm_value |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 173 | & PM_PWM_VALUE_BIT5_0; |
Subbaraman Narayanamurthy | 8f1ffa5 | 2011-06-03 12:33:01 -0700 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | len = 6; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 177 | } else { |
Subbaraman Narayanamurthy | 8f1ffa5 | 2011-06-03 12:33:01 -0700 | [diff] [blame] | 178 | /* Right now, we are not using LUT */ |
| 179 | goto bail_out; |
| 180 | } |
| 181 | |
| 182 | /* Selecting the bank */ |
| 183 | rc = pm8058_write(PM8058_LPG_BANK_SEL, &pwm_id, 1); |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 184 | if (rc) |
Subbaraman Narayanamurthy | 8f1ffa5 | 2011-06-03 12:33:01 -0700 | [diff] [blame] | 185 | goto bail_out; |
| 186 | |
| 187 | for (i = 0; i < len; i++) { |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 188 | rc = pm8058_write(PM8058_LPG_CTL(i), &pwm_conf->pwm_ctl[i], 1); |
Subbaraman Narayanamurthy | 8f1ffa5 | 2011-06-03 12:33:01 -0700 | [diff] [blame] | 189 | if (rc) { |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 190 | dprintf(CRITICAL, |
| 191 | "pm8058_write() failed in pwm_configure %d\n", |
| 192 | rc); |
Subbaraman Narayanamurthy | 8f1ffa5 | 2011-06-03 12:33:01 -0700 | [diff] [blame] | 193 | break; |
| 194 | } |
| 195 | } |
| 196 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 197 | bail_out: |
| 198 | if (rc) |
| 199 | dprintf(CRITICAL, "Error in pm_pwm_configure()\n"); |
Subbaraman Narayanamurthy | 8f1ffa5 | 2011-06-03 12:33:01 -0700 | [diff] [blame] | 200 | return rc; |
| 201 | } |
| 202 | |
| 203 | /* Top level function for configuring PWM */ |
| 204 | |
| 205 | int pm_pwm_config(uint8_t pwm_id, uint32_t duty_us, uint32_t period_us) |
| 206 | { |
| 207 | struct pm_pwm_config pwm_conf; |
| 208 | uint32_t max_pwm_value, tmp; |
| 209 | int rc = -1; |
| 210 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 211 | if ((duty_us > period_us) || (period_us > PM_PWM_PERIOD_MAX) || |
| 212 | (period_us < PM_PWM_PERIOD_MIN)) { |
| 213 | dprintf(CRITICAL, "Error in duty cycle and period\n"); |
Subbaraman Narayanamurthy | 8f1ffa5 | 2011-06-03 12:33:01 -0700 | [diff] [blame] | 214 | return -1; |
| 215 | } |
| 216 | |
| 217 | pm_pwm_calc_period(period_us, &pwm_conf); |
| 218 | |
| 219 | /* Figure out pwm_value with overflow handling */ |
| 220 | if (period_us > (1 << pwm_conf.pwm_size)) { |
| 221 | tmp = period_us; |
| 222 | tmp >>= pwm_conf.pwm_size; |
| 223 | pwm_conf.pwm_value = duty_us / tmp; |
| 224 | } else { |
| 225 | tmp = duty_us; |
| 226 | tmp <<= pwm_conf.pwm_size; |
| 227 | pwm_conf.pwm_value = tmp / period_us; |
| 228 | } |
| 229 | max_pwm_value = (1 << pwm_conf.pwm_size) - 1; |
| 230 | if (pwm_conf.pwm_value > max_pwm_value) |
| 231 | pwm_conf.pwm_value = max_pwm_value; |
| 232 | |
| 233 | /* Bypassing LUT */ |
| 234 | pwm_conf.bypass_lut = 1; |
| 235 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 236 | dprintf(SPEW, "duty/period=%u/%u usec: pwm_value=%d (of %d)\n", |
| 237 | duty_us, period_us, pwm_conf.pwm_value, 1 << pwm_conf.pwm_size); |
Subbaraman Narayanamurthy | 8f1ffa5 | 2011-06-03 12:33:01 -0700 | [diff] [blame] | 238 | |
| 239 | rc = pm_pwm_configure(pwm_id, &pwm_conf); |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 240 | if (rc) |
| 241 | dprintf(CRITICAL, "Error in pwm_config()\n"); |
Subbaraman Narayanamurthy | 8f1ffa5 | 2011-06-03 12:33:01 -0700 | [diff] [blame] | 242 | |
| 243 | return rc; |
| 244 | } |
| 245 | |
| 246 | /* Top level function to enable PWM with specified id */ |
| 247 | |
| 248 | int pm_pwm_enable(uint8_t pwm_id) |
| 249 | { |
| 250 | int rc = -1; |
| 251 | uint8_t reg; |
| 252 | |
| 253 | /* Read it before enabling other bank */ |
| 254 | rc = pm8058_read(PM8058_LPG_BANK_ENABLE, ®, 1); |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 255 | if (rc) |
Subbaraman Narayanamurthy | 8f1ffa5 | 2011-06-03 12:33:01 -0700 | [diff] [blame] | 256 | goto bail_out; |
| 257 | |
| 258 | reg |= (1 << pwm_id); |
| 259 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 260 | rc = pm8058_write(PM8058_LPG_BANK_ENABLE, ®, 1); |
| 261 | if (rc) |
Subbaraman Narayanamurthy | 8f1ffa5 | 2011-06-03 12:33:01 -0700 | [diff] [blame] | 262 | goto bail_out; |
| 263 | |
| 264 | /* Selecting the bank */ |
| 265 | rc = pm8058_write(PM8058_LPG_BANK_SEL, &pwm_id, 1); |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 266 | if (rc) |
Subbaraman Narayanamurthy | 8f1ffa5 | 2011-06-03 12:33:01 -0700 | [diff] [blame] | 267 | goto bail_out; |
| 268 | |
| 269 | /* Read it before setting PWM start */ |
| 270 | rc = pm8058_read(PM8058_LPG_CTL(0), ®, 1); |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 271 | if (rc) |
Subbaraman Narayanamurthy | 8f1ffa5 | 2011-06-03 12:33:01 -0700 | [diff] [blame] | 272 | goto bail_out; |
| 273 | |
| 274 | reg |= PM_PWM_PWM_START; |
| 275 | reg &= ~PM_PWM_RAMP_GEN_START; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 276 | rc = pm8058_write(PM8058_LPG_CTL(0), ®, 1); |
Subbaraman Narayanamurthy | 8f1ffa5 | 2011-06-03 12:33:01 -0700 | [diff] [blame] | 277 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 278 | bail_out: |
| 279 | if (rc) |
Subbaraman Narayanamurthy | 8f1ffa5 | 2011-06-03 12:33:01 -0700 | [diff] [blame] | 280 | dprintf(CRITICAL, "Error in pwm_enable()\n"); |
| 281 | return rc; |
| 282 | } |