pm8921_pwm.c 7.7 KB

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