pmic8058-pwm.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086
  1. /* Copyright (c) 2010-2011, The Linux Foundation. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. */
  13. /*
  14. * Qualcomm PMIC8058 PWM driver
  15. *
  16. */
  17. #define pr_fmt(fmt) "%s: " fmt, __func__
  18. #include <linux/module.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/slab.h>
  21. #include <linux/err.h>
  22. #include <linux/pwm.h>
  23. #include <linux/mfd/pm8xxx/core.h>
  24. #include <linux/pmic8058-pwm.h>
  25. #define PM8058_LPG_BANKS 8
  26. #define PM8058_PWM_CHANNELS PM8058_LPG_BANKS /* MAX=8 */
  27. #define PM8058_LPG_CTL_REGS 7
  28. /* PMIC8058 LPG/PWM */
  29. #define SSBI_REG_ADDR_LPG_CTL_BASE 0x13C
  30. #define SSBI_REG_ADDR_LPG_CTL(n) (SSBI_REG_ADDR_LPG_CTL_BASE + (n))
  31. #define SSBI_REG_ADDR_LPG_BANK_SEL 0x143
  32. #define SSBI_REG_ADDR_LPG_BANK_EN 0x144
  33. #define SSBI_REG_ADDR_LPG_LUT_CFG0 0x145
  34. #define SSBI_REG_ADDR_LPG_LUT_CFG1 0x146
  35. #define SSBI_REG_ADDR_LPG_TEST 0x147
  36. /* Control 0 */
  37. #define PM8058_PWM_1KHZ_COUNT_MASK 0xF0
  38. #define PM8058_PWM_1KHZ_COUNT_SHIFT 4
  39. #define PM8058_PWM_1KHZ_COUNT_MAX 15
  40. #define PM8058_PWM_OUTPUT_EN 0x08
  41. #define PM8058_PWM_PWM_EN 0x04
  42. #define PM8058_PWM_RAMP_GEN_EN 0x02
  43. #define PM8058_PWM_RAMP_START 0x01
  44. #define PM8058_PWM_PWM_START (PM8058_PWM_OUTPUT_EN \
  45. | PM8058_PWM_PWM_EN)
  46. #define PM8058_PWM_RAMP_GEN_START (PM8058_PWM_RAMP_GEN_EN \
  47. | PM8058_PWM_RAMP_START)
  48. /* Control 1 */
  49. #define PM8058_PWM_REVERSE_EN 0x80
  50. #define PM8058_PWM_BYPASS_LUT 0x40
  51. #define PM8058_PWM_HIGH_INDEX_MASK 0x3F
  52. /* Control 2 */
  53. #define PM8058_PWM_LOOP_EN 0x80
  54. #define PM8058_PWM_RAMP_UP 0x40
  55. #define PM8058_PWM_LOW_INDEX_MASK 0x3F
  56. /* Control 3 */
  57. #define PM8058_PWM_VALUE_BIT7_0 0xFF
  58. #define PM8058_PWM_VALUE_BIT5_0 0x3F
  59. /* Control 4 */
  60. #define PM8058_PWM_VALUE_BIT8 0x80
  61. #define PM8058_PWM_CLK_SEL_MASK 0x60
  62. #define PM8058_PWM_CLK_SEL_SHIFT 5
  63. #define PM8058_PWM_CLK_SEL_NO 0
  64. #define PM8058_PWM_CLK_SEL_1KHZ 1
  65. #define PM8058_PWM_CLK_SEL_32KHZ 2
  66. #define PM8058_PWM_CLK_SEL_19P2MHZ 3
  67. #define PM8058_PWM_PREDIVIDE_MASK 0x18
  68. #define PM8058_PWM_PREDIVIDE_SHIFT 3
  69. #define PM8058_PWM_PREDIVIDE_2 0
  70. #define PM8058_PWM_PREDIVIDE_3 1
  71. #define PM8058_PWM_PREDIVIDE_5 2
  72. #define PM8058_PWM_PREDIVIDE_6 3
  73. #define PM8058_PWM_M_MASK 0x07
  74. #define PM8058_PWM_M_MIN 0
  75. #define PM8058_PWM_M_MAX 7
  76. /* Control 5 */
  77. #define PM8058_PWM_PAUSE_COUNT_HI_MASK 0xFC
  78. #define PM8058_PWM_PAUSE_COUNT_HI_SHIFT 2
  79. #define PM8058_PWM_PAUSE_ENABLE_HIGH 0x02
  80. #define PM8058_PWM_SIZE_9_BIT 0x01
  81. /* Control 6 */
  82. #define PM8058_PWM_PAUSE_COUNT_LO_MASK 0xFC
  83. #define PM8058_PWM_PAUSE_COUNT_LO_SHIFT 2
  84. #define PM8058_PWM_PAUSE_ENABLE_LOW 0x02
  85. #define PM8058_PWM_RESERVED 0x01
  86. #define PM8058_PWM_PAUSE_COUNT_MAX 56 /* < 2^6 = 64*/
  87. /* LUT_CFG1 */
  88. #define PM8058_PWM_LUT_READ 0x40
  89. /* TEST */
  90. #define PM8058_PWM_DTEST_MASK 0x38
  91. #define PM8058_PWM_DTEST_SHIFT 3
  92. #define PM8058_PWM_DTEST_BANK_MASK 0x07
  93. /* PWM frequency support
  94. *
  95. * PWM Frequency = Clock Frequency / (N * T)
  96. * or
  97. * PWM Period = Clock Period * (N * T)
  98. * where
  99. * N = 2^9 or 2^6 for 9-bit or 6-bit PWM size
  100. * T = Pre-divide * 2^m, m = 0..7 (exponent)
  101. *
  102. * We use this formula to figure out m for the best pre-divide and clock:
  103. * (PWM Period / N) / 2^m = (Pre-divide * Clock Period)
  104. */
  105. #define NUM_CLOCKS 3
  106. #define NSEC_1000HZ (NSEC_PER_SEC / 1000)
  107. #define NSEC_32768HZ (NSEC_PER_SEC / 32768)
  108. #define NSEC_19P2MHZ (NSEC_PER_SEC / 19200000)
  109. #define CLK_PERIOD_MIN NSEC_19P2MHZ
  110. #define CLK_PERIOD_MAX NSEC_1000HZ
  111. #define NUM_PRE_DIVIDE 3 /* No default support for pre-divide = 6 */
  112. #define PRE_DIVIDE_0 2
  113. #define PRE_DIVIDE_1 3
  114. #define PRE_DIVIDE_2 5
  115. #define PRE_DIVIDE_MIN PRE_DIVIDE_0
  116. #define PRE_DIVIDE_MAX PRE_DIVIDE_2
  117. static char *clks[NUM_CLOCKS] = {
  118. "1K", "32768", "19.2M"
  119. };
  120. static unsigned pre_div[NUM_PRE_DIVIDE] = {
  121. PRE_DIVIDE_0, PRE_DIVIDE_1, PRE_DIVIDE_2
  122. };
  123. static unsigned int pt_t[NUM_PRE_DIVIDE][NUM_CLOCKS] = {
  124. { PRE_DIVIDE_0 * NSEC_1000HZ,
  125. PRE_DIVIDE_0 * NSEC_32768HZ,
  126. PRE_DIVIDE_0 * NSEC_19P2MHZ,
  127. },
  128. { PRE_DIVIDE_1 * NSEC_1000HZ,
  129. PRE_DIVIDE_1 * NSEC_32768HZ,
  130. PRE_DIVIDE_1 * NSEC_19P2MHZ,
  131. },
  132. { PRE_DIVIDE_2 * NSEC_1000HZ,
  133. PRE_DIVIDE_2 * NSEC_32768HZ,
  134. PRE_DIVIDE_2 * NSEC_19P2MHZ,
  135. },
  136. };
  137. #define MIN_MPT ((PRE_DIVIDE_MIN * CLK_PERIOD_MIN) << PM8058_PWM_M_MIN)
  138. #define MAX_MPT ((PRE_DIVIDE_MAX * CLK_PERIOD_MAX) << PM8058_PWM_M_MAX)
  139. #define CHAN_LUT_SIZE (PM_PWM_LUT_SIZE / PM8058_PWM_CHANNELS)
  140. /* Private data */
  141. struct pm8058_pwm_chip;
  142. struct pwm_device {
  143. struct device *dev;
  144. int pwm_id; /* = bank/channel id */
  145. int in_use;
  146. const char *label;
  147. struct pm8058_pwm_period period;
  148. int pwm_value;
  149. int pwm_period;
  150. int use_lut; /* Use LUT to output PWM */
  151. u8 pwm_ctl[PM8058_LPG_CTL_REGS];
  152. int irq;
  153. struct pm8058_pwm_chip *chip;
  154. };
  155. struct pm8058_pwm_chip {
  156. struct pwm_device pwm_dev[PM8058_PWM_CHANNELS];
  157. u8 bank_mask;
  158. struct mutex pwm_mutex;
  159. struct pm8058_pwm_pdata *pdata;
  160. };
  161. static struct pm8058_pwm_chip *pwm_chip;
  162. struct pm8058_pwm_lut {
  163. /* LUT parameters */
  164. int lut_duty_ms;
  165. int lut_lo_index;
  166. int lut_hi_index;
  167. int lut_pause_hi;
  168. int lut_pause_lo;
  169. int flags;
  170. };
  171. static u16 duty_msec[PM8058_PWM_1KHZ_COUNT_MAX + 1] = {
  172. 0, 1, 2, 3, 4, 6, 8, 16, 18, 24, 32, 36, 64, 128, 256, 512
  173. };
  174. static u16 pause_count[PM8058_PWM_PAUSE_COUNT_MAX + 1] = {
  175. 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
  176. 23, 28, 31, 42, 47, 56, 63, 83, 94, 111, 125, 167, 188, 222, 250, 333,
  177. 375, 500, 667, 750, 800, 900, 1000, 1100,
  178. 1200, 1300, 1400, 1500, 1600, 1800, 2000, 2500,
  179. 3000, 3500, 4000, 4500, 5000, 5500, 6000, 6500,
  180. 7000
  181. };
  182. /* Internal functions */
  183. static void pm8058_pwm_save(u8 *u8p, u8 mask, u8 val)
  184. {
  185. *u8p &= ~mask;
  186. *u8p |= val & mask;
  187. }
  188. static int pm8058_pwm_bank_enable(struct pwm_device *pwm, int enable)
  189. {
  190. int rc;
  191. u8 reg;
  192. struct pm8058_pwm_chip *chip;
  193. chip = pwm->chip;
  194. if (enable)
  195. reg = chip->bank_mask | (1 << pwm->pwm_id);
  196. else
  197. reg = chip->bank_mask & ~(1 << pwm->pwm_id);
  198. rc = pm8xxx_writeb(pwm->dev->parent,
  199. SSBI_REG_ADDR_LPG_BANK_EN, reg);
  200. if (rc) {
  201. pr_err("pm8xxx_write(): rc=%d (Enable LPG Bank)\n", rc);
  202. goto bail_out;
  203. }
  204. chip->bank_mask = reg;
  205. bail_out:
  206. return rc;
  207. }
  208. static int pm8058_pwm_bank_sel(struct pwm_device *pwm)
  209. {
  210. int rc;
  211. u8 reg;
  212. reg = pwm->pwm_id;
  213. rc = pm8xxx_writeb(pwm->dev->parent,
  214. SSBI_REG_ADDR_LPG_BANK_SEL, reg);
  215. if (rc)
  216. pr_err("pm8xxx_write(): rc=%d (Select PWM Bank)\n", rc);
  217. return rc;
  218. }
  219. static int pm8058_pwm_start(struct pwm_device *pwm, int start, int ramp_start)
  220. {
  221. int rc;
  222. u8 reg;
  223. if (start) {
  224. reg = pwm->pwm_ctl[0] | PM8058_PWM_PWM_START;
  225. if (ramp_start)
  226. reg |= PM8058_PWM_RAMP_GEN_START;
  227. else
  228. reg &= ~PM8058_PWM_RAMP_GEN_START;
  229. } else {
  230. reg = pwm->pwm_ctl[0] & ~PM8058_PWM_PWM_START;
  231. reg &= ~PM8058_PWM_RAMP_GEN_START;
  232. }
  233. rc = pm8xxx_writeb(pwm->dev->parent, SSBI_REG_ADDR_LPG_CTL(0),
  234. reg);
  235. if (rc)
  236. pr_err("pm8xxx_write(): rc=%d (Enable PWM Ctl 0)\n", rc);
  237. else
  238. pwm->pwm_ctl[0] = reg;
  239. return rc;
  240. }
  241. static void pm8058_pwm_calc_period(unsigned int period_us,
  242. struct pm8058_pwm_period *period)
  243. {
  244. int n, m, clk, div;
  245. int best_m, best_div, best_clk;
  246. int last_err, cur_err, better_err, better_m;
  247. unsigned int tmp_p, last_p, min_err, period_n;
  248. /* PWM Period / N : handle underflow or overflow */
  249. if (period_us < (PM_PWM_PERIOD_MAX / NSEC_PER_USEC))
  250. period_n = (period_us * NSEC_PER_USEC) >> 6;
  251. else
  252. period_n = (period_us >> 6) * NSEC_PER_USEC;
  253. if (period_n >= MAX_MPT) {
  254. n = 9;
  255. period_n >>= 3;
  256. } else
  257. n = 6;
  258. min_err = MAX_MPT;
  259. best_m = 0;
  260. best_clk = 0;
  261. best_div = 0;
  262. for (clk = 0; clk < NUM_CLOCKS; clk++) {
  263. for (div = 0; div < NUM_PRE_DIVIDE; div++) {
  264. tmp_p = period_n;
  265. last_p = tmp_p;
  266. for (m = 0; m <= PM8058_PWM_M_MAX; m++) {
  267. if (tmp_p <= pt_t[div][clk]) {
  268. /* Found local best */
  269. if (!m) {
  270. better_err = pt_t[div][clk] -
  271. tmp_p;
  272. better_m = m;
  273. } else {
  274. last_err = last_p -
  275. pt_t[div][clk];
  276. cur_err = pt_t[div][clk] -
  277. tmp_p;
  278. if (cur_err < last_err) {
  279. better_err = cur_err;
  280. better_m = m;
  281. } else {
  282. better_err = last_err;
  283. better_m = m - 1;
  284. }
  285. }
  286. if (better_err < min_err) {
  287. min_err = better_err;
  288. best_m = better_m;
  289. best_clk = clk;
  290. best_div = div;
  291. }
  292. break;
  293. } else {
  294. last_p = tmp_p;
  295. tmp_p >>= 1;
  296. }
  297. }
  298. }
  299. }
  300. /* Use higher resolution */
  301. if (best_m >= 3 && n == 6) {
  302. n += 3;
  303. best_m -= 3;
  304. }
  305. period->pwm_size = n;
  306. period->clk = best_clk;
  307. period->pre_div = best_div;
  308. period->pre_div_exp = best_m;
  309. pr_debug("period=%u: n=%d, m=%d, clk[%d]=%s, div[%d]=%d\n",
  310. (unsigned)period_us, n, best_m,
  311. best_clk, clks[best_clk], best_div, pre_div[best_div]);
  312. }
  313. static void pm8058_pwm_calc_pwm_value(struct pwm_device *pwm,
  314. unsigned int period_us,
  315. unsigned int duty_us)
  316. {
  317. unsigned int max_pwm_value, tmp;
  318. /* Figure out pwm_value with overflow handling */
  319. tmp = 1 << (sizeof(tmp) * 8 - pwm->period.pwm_size);
  320. if (duty_us < tmp) {
  321. tmp = duty_us << pwm->period.pwm_size;
  322. pwm->pwm_value = tmp / period_us;
  323. } else {
  324. tmp = period_us >> pwm->period.pwm_size;
  325. pwm->pwm_value = duty_us / tmp;
  326. }
  327. max_pwm_value = (1 << pwm->period.pwm_size) - 1;
  328. if (pwm->pwm_value > max_pwm_value)
  329. pwm->pwm_value = max_pwm_value;
  330. }
  331. static int pm8058_pwm_change_table(struct pwm_device *pwm, int duty_pct[],
  332. int start_idx, int len, int raw_value)
  333. {
  334. unsigned int pwm_value, max_pwm_value;
  335. u8 cfg0, cfg1;
  336. int i, pwm_size;
  337. int rc = 0;
  338. pwm_size = (pwm->pwm_ctl[5] & PM8058_PWM_SIZE_9_BIT) ? 9 : 6;
  339. max_pwm_value = (1 << pwm_size) - 1;
  340. for (i = 0; i < len; i++) {
  341. if (raw_value)
  342. pwm_value = duty_pct[i];
  343. else
  344. pwm_value = (duty_pct[i] << pwm_size) / 100;
  345. if (pwm_value > max_pwm_value)
  346. pwm_value = max_pwm_value;
  347. cfg0 = pwm_value;
  348. cfg1 = (pwm_value >> 1) & 0x80;
  349. cfg1 |= start_idx + i;
  350. rc = pm8xxx_writeb(pwm->dev->parent,
  351. SSBI_REG_ADDR_LPG_LUT_CFG0, cfg0);
  352. if (rc)
  353. break;
  354. rc = pm8xxx_writeb(pwm->dev->parent,
  355. SSBI_REG_ADDR_LPG_LUT_CFG1, cfg1);
  356. if (rc)
  357. break;
  358. }
  359. return rc;
  360. }
  361. static void pm8058_pwm_save_index(struct pwm_device *pwm,
  362. int low_idx, int high_idx, int flags)
  363. {
  364. pwm->pwm_ctl[1] = high_idx & PM8058_PWM_HIGH_INDEX_MASK;
  365. pwm->pwm_ctl[2] = low_idx & PM8058_PWM_LOW_INDEX_MASK;
  366. if (flags & PM_PWM_LUT_REVERSE)
  367. pwm->pwm_ctl[1] |= PM8058_PWM_REVERSE_EN;
  368. if (flags & PM_PWM_LUT_RAMP_UP)
  369. pwm->pwm_ctl[2] |= PM8058_PWM_RAMP_UP;
  370. if (flags & PM_PWM_LUT_LOOP)
  371. pwm->pwm_ctl[2] |= PM8058_PWM_LOOP_EN;
  372. }
  373. static void pm8058_pwm_save_period(struct pwm_device *pwm)
  374. {
  375. u8 mask, val;
  376. val = ((pwm->period.clk + 1) << PM8058_PWM_CLK_SEL_SHIFT)
  377. & PM8058_PWM_CLK_SEL_MASK;
  378. val |= (pwm->period.pre_div << PM8058_PWM_PREDIVIDE_SHIFT)
  379. & PM8058_PWM_PREDIVIDE_MASK;
  380. val |= pwm->period.pre_div_exp & PM8058_PWM_M_MASK;
  381. mask = PM8058_PWM_CLK_SEL_MASK | PM8058_PWM_PREDIVIDE_MASK |
  382. PM8058_PWM_M_MASK;
  383. pm8058_pwm_save(&pwm->pwm_ctl[4], mask, val);
  384. val = (pwm->period.pwm_size > 6) ? PM8058_PWM_SIZE_9_BIT : 0;
  385. mask = PM8058_PWM_SIZE_9_BIT;
  386. pm8058_pwm_save(&pwm->pwm_ctl[5], mask, val);
  387. }
  388. static void pm8058_pwm_save_pwm_value(struct pwm_device *pwm)
  389. {
  390. u8 mask, val;
  391. pwm->pwm_ctl[3] = pwm->pwm_value;
  392. val = (pwm->period.pwm_size > 6) ? (pwm->pwm_value >> 1) : 0;
  393. mask = PM8058_PWM_VALUE_BIT8;
  394. pm8058_pwm_save(&pwm->pwm_ctl[4], mask, val);
  395. }
  396. static void pm8058_pwm_save_duty_time(struct pwm_device *pwm,
  397. struct pm8058_pwm_lut *lut)
  398. {
  399. int i;
  400. u8 mask, val;
  401. /* Linear search for duty time */
  402. for (i = 0; i < PM8058_PWM_1KHZ_COUNT_MAX; i++) {
  403. if (duty_msec[i] >= lut->lut_duty_ms)
  404. break;
  405. }
  406. val = i << PM8058_PWM_1KHZ_COUNT_SHIFT;
  407. mask = PM8058_PWM_1KHZ_COUNT_MASK;
  408. pm8058_pwm_save(&pwm->pwm_ctl[0], mask, val);
  409. }
  410. static void pm8058_pwm_save_pause(struct pwm_device *pwm,
  411. struct pm8058_pwm_lut *lut)
  412. {
  413. int i, pause_cnt, time_cnt;
  414. u8 mask, val;
  415. time_cnt = (pwm->pwm_ctl[0] & PM8058_PWM_1KHZ_COUNT_MASK)
  416. >> PM8058_PWM_1KHZ_COUNT_SHIFT;
  417. if (lut->flags & PM_PWM_LUT_PAUSE_HI_EN) {
  418. pause_cnt = (lut->lut_pause_hi + duty_msec[time_cnt] / 2)
  419. / duty_msec[time_cnt];
  420. /* Linear search for pause time */
  421. for (i = 0; i < PM8058_PWM_PAUSE_COUNT_MAX; i++) {
  422. if (pause_count[i] >= pause_cnt)
  423. break;
  424. }
  425. val = (i << PM8058_PWM_PAUSE_COUNT_HI_SHIFT) &
  426. PM8058_PWM_PAUSE_COUNT_HI_MASK;
  427. val |= PM8058_PWM_PAUSE_ENABLE_HIGH;
  428. } else
  429. val = 0;
  430. mask = PM8058_PWM_PAUSE_COUNT_HI_MASK | PM8058_PWM_PAUSE_ENABLE_HIGH;
  431. pm8058_pwm_save(&pwm->pwm_ctl[5], mask, val);
  432. if (lut->flags & PM_PWM_LUT_PAUSE_LO_EN) {
  433. /* Linear search for pause time */
  434. pause_cnt = (lut->lut_pause_lo + duty_msec[time_cnt] / 2)
  435. / duty_msec[time_cnt];
  436. for (i = 0; i < PM8058_PWM_PAUSE_COUNT_MAX; i++) {
  437. if (pause_count[i] >= pause_cnt)
  438. break;
  439. }
  440. val = (i << PM8058_PWM_PAUSE_COUNT_LO_SHIFT) &
  441. PM8058_PWM_PAUSE_COUNT_LO_MASK;
  442. val |= PM8058_PWM_PAUSE_ENABLE_LOW;
  443. } else
  444. val = 0;
  445. mask = PM8058_PWM_PAUSE_COUNT_LO_MASK | PM8058_PWM_PAUSE_ENABLE_LOW;
  446. pm8058_pwm_save(&pwm->pwm_ctl[6], mask, val);
  447. }
  448. static int pm8058_pwm_write(struct pwm_device *pwm, int start, int end)
  449. {
  450. int i, rc;
  451. /* Write in reverse way so 0 would be the last */
  452. for (i = end - 1; i >= start; i--) {
  453. rc = pm8xxx_writeb(pwm->dev->parent,
  454. SSBI_REG_ADDR_LPG_CTL(i),
  455. pwm->pwm_ctl[i]);
  456. if (rc) {
  457. pr_err("pm8xxx_write(): rc=%d (PWM Ctl[%d])\n", rc, i);
  458. return rc;
  459. }
  460. }
  461. return 0;
  462. }
  463. static int pm8058_pwm_change_lut(struct pwm_device *pwm,
  464. struct pm8058_pwm_lut *lut)
  465. {
  466. int rc;
  467. pm8058_pwm_save_index(pwm, lut->lut_lo_index,
  468. lut->lut_hi_index, lut->flags);
  469. pm8058_pwm_save_duty_time(pwm, lut);
  470. pm8058_pwm_save_pause(pwm, lut);
  471. pm8058_pwm_save(&pwm->pwm_ctl[1], PM8058_PWM_BYPASS_LUT, 0);
  472. pm8058_pwm_bank_sel(pwm);
  473. rc = pm8058_pwm_write(pwm, 0, 7);
  474. return rc;
  475. }
  476. /* APIs */
  477. /*
  478. * pwm_request - request a PWM device
  479. */
  480. struct pwm_device *pwm_request(int pwm_id, const char *label)
  481. {
  482. struct pwm_device *pwm;
  483. if (pwm_id > PM8058_PWM_CHANNELS || pwm_id < 0)
  484. return ERR_PTR(-EINVAL);
  485. if (pwm_chip == NULL)
  486. return ERR_PTR(-ENODEV);
  487. mutex_lock(&pwm_chip->pwm_mutex);
  488. pwm = &pwm_chip->pwm_dev[pwm_id];
  489. if (!pwm->in_use) {
  490. pwm->in_use = 1;
  491. pwm->label = label;
  492. pwm->use_lut = 0;
  493. if (pwm_chip->pdata && pwm_chip->pdata->config)
  494. pwm_chip->pdata->config(pwm, pwm_id, 1);
  495. } else
  496. pwm = ERR_PTR(-EBUSY);
  497. mutex_unlock(&pwm_chip->pwm_mutex);
  498. return pwm;
  499. }
  500. EXPORT_SYMBOL(pwm_request);
  501. /*
  502. * pwm_free - free a PWM device
  503. */
  504. void pwm_free(struct pwm_device *pwm)
  505. {
  506. if (pwm == NULL || IS_ERR(pwm) || pwm->chip == NULL)
  507. return;
  508. mutex_lock(&pwm->chip->pwm_mutex);
  509. if (pwm->in_use) {
  510. pm8058_pwm_bank_sel(pwm);
  511. pm8058_pwm_start(pwm, 0, 0);
  512. if (pwm->chip->pdata && pwm->chip->pdata->config)
  513. pwm->chip->pdata->config(pwm, pwm->pwm_id, 0);
  514. pwm->in_use = 0;
  515. pwm->label = NULL;
  516. }
  517. pm8058_pwm_bank_enable(pwm, 0);
  518. mutex_unlock(&pwm->chip->pwm_mutex);
  519. }
  520. EXPORT_SYMBOL(pwm_free);
  521. /*
  522. * pwm_config - change a PWM device configuration
  523. *
  524. * @pwm: the PWM device
  525. * @period_us: period in micro second
  526. * @duty_us: duty cycle in micro second
  527. */
  528. int pwm_config(struct pwm_device *pwm, int duty_us, int period_us)
  529. {
  530. int rc;
  531. if (pwm == NULL || IS_ERR(pwm) ||
  532. duty_us > period_us ||
  533. (unsigned)period_us > PM_PWM_PERIOD_MAX ||
  534. (unsigned)period_us < PM_PWM_PERIOD_MIN)
  535. return -EINVAL;
  536. if (pwm->chip == NULL)
  537. return -ENODEV;
  538. mutex_lock(&pwm->chip->pwm_mutex);
  539. if (!pwm->in_use) {
  540. rc = -EINVAL;
  541. goto out_unlock;
  542. }
  543. if (pwm->pwm_period != period_us) {
  544. pm8058_pwm_calc_period(period_us, &pwm->period);
  545. pm8058_pwm_save_period(pwm);
  546. pwm->pwm_period = period_us;
  547. }
  548. pm8058_pwm_calc_pwm_value(pwm, period_us, duty_us);
  549. pm8058_pwm_save_pwm_value(pwm);
  550. pm8058_pwm_save(&pwm->pwm_ctl[1],
  551. PM8058_PWM_BYPASS_LUT, PM8058_PWM_BYPASS_LUT);
  552. pm8058_pwm_bank_sel(pwm);
  553. rc = pm8058_pwm_write(pwm, 1, 6);
  554. pr_debug("duty/period=%u/%u usec: pwm_value=%d (of %d)\n",
  555. (unsigned)duty_us, (unsigned)period_us,
  556. pwm->pwm_value, 1 << pwm->period.pwm_size);
  557. out_unlock:
  558. mutex_unlock(&pwm->chip->pwm_mutex);
  559. return rc;
  560. }
  561. EXPORT_SYMBOL(pwm_config);
  562. /*
  563. * pwm_enable - start a PWM output toggling
  564. */
  565. int pwm_enable(struct pwm_device *pwm)
  566. {
  567. int rc;
  568. if (pwm == NULL || IS_ERR(pwm))
  569. return -EINVAL;
  570. if (pwm->chip == NULL)
  571. return -ENODEV;
  572. mutex_lock(&pwm->chip->pwm_mutex);
  573. if (!pwm->in_use)
  574. rc = -EINVAL;
  575. else {
  576. if (pwm->chip->pdata && pwm->chip->pdata->enable)
  577. pwm->chip->pdata->enable(pwm, pwm->pwm_id, 1);
  578. rc = pm8058_pwm_bank_enable(pwm, 1);
  579. pm8058_pwm_bank_sel(pwm);
  580. pm8058_pwm_start(pwm, 1, 0);
  581. }
  582. mutex_unlock(&pwm->chip->pwm_mutex);
  583. return rc;
  584. }
  585. EXPORT_SYMBOL(pwm_enable);
  586. /*
  587. * pwm_disable - stop a PWM output toggling
  588. */
  589. void pwm_disable(struct pwm_device *pwm)
  590. {
  591. if (pwm == NULL || IS_ERR(pwm) || pwm->chip == NULL)
  592. return;
  593. mutex_lock(&pwm->chip->pwm_mutex);
  594. if (pwm->in_use) {
  595. pm8058_pwm_bank_sel(pwm);
  596. pm8058_pwm_start(pwm, 0, 0);
  597. pm8058_pwm_bank_enable(pwm, 0);
  598. if (pwm->chip->pdata && pwm->chip->pdata->enable)
  599. pwm->chip->pdata->enable(pwm, pwm->pwm_id, 0);
  600. }
  601. mutex_unlock(&pwm->chip->pwm_mutex);
  602. }
  603. EXPORT_SYMBOL(pwm_disable);
  604. /**
  605. * pm8058_pwm_config_period - change PWM period
  606. *
  607. * @pwm: the PWM device
  608. * @pwm_p: period in struct pm8058_pwm_period
  609. */
  610. int pm8058_pwm_config_period(struct pwm_device *pwm,
  611. struct pm8058_pwm_period *period)
  612. {
  613. int rc;
  614. if (pwm == NULL || IS_ERR(pwm) || period == NULL)
  615. return -EINVAL;
  616. if (pwm->chip == NULL)
  617. return -ENODEV;
  618. mutex_lock(&pwm->chip->pwm_mutex);
  619. if (!pwm->in_use) {
  620. rc = -EINVAL;
  621. goto out_unlock;
  622. }
  623. pwm->period.pwm_size = period->pwm_size;
  624. pwm->period.clk = period->clk;
  625. pwm->period.pre_div = period->pre_div;
  626. pwm->period.pre_div_exp = period->pre_div_exp;
  627. pm8058_pwm_save_period(pwm);
  628. pm8058_pwm_bank_sel(pwm);
  629. rc = pm8058_pwm_write(pwm, 4, 6);
  630. out_unlock:
  631. mutex_unlock(&pwm->chip->pwm_mutex);
  632. return rc;
  633. }
  634. EXPORT_SYMBOL(pm8058_pwm_config_period);
  635. /**
  636. * pm8058_pwm_config_duty_cycle - change PWM duty cycle
  637. *
  638. * @pwm: the PWM device
  639. * @pwm_value: the duty cycle in raw PWM value (< 2^pwm_size)
  640. */
  641. int pm8058_pwm_config_duty_cycle(struct pwm_device *pwm, int pwm_value)
  642. {
  643. struct pm8058_pwm_lut lut;
  644. int flags, start_idx;
  645. int rc = 0;
  646. if (pwm == NULL || IS_ERR(pwm))
  647. return -EINVAL;
  648. if (pwm->chip == NULL)
  649. return -ENODEV;
  650. mutex_lock(&pwm->chip->pwm_mutex);
  651. if (!pwm->in_use || !pwm->pwm_period) {
  652. rc = -EINVAL;
  653. goto out_unlock;
  654. }
  655. if (pwm->pwm_value == pwm_value)
  656. goto out_unlock;
  657. pwm->pwm_value = pwm_value;
  658. flags = PM_PWM_LUT_RAMP_UP;
  659. start_idx = pwm->pwm_id * CHAN_LUT_SIZE;
  660. pm8058_pwm_change_table(pwm, &pwm_value, start_idx, 1, 1);
  661. if (!pwm->use_lut) {
  662. pwm->use_lut = 1;
  663. lut.lut_duty_ms = 1;
  664. lut.lut_lo_index = start_idx;
  665. lut.lut_hi_index = start_idx;
  666. lut.lut_pause_lo = 0;
  667. lut.lut_pause_hi = 0;
  668. lut.flags = flags;
  669. rc = pm8058_pwm_change_lut(pwm, &lut);
  670. } else {
  671. pm8058_pwm_save_index(pwm, start_idx, start_idx, flags);
  672. pm8058_pwm_save(&pwm->pwm_ctl[1], PM8058_PWM_BYPASS_LUT, 0);
  673. pm8058_pwm_bank_sel(pwm);
  674. rc = pm8058_pwm_write(pwm, 0, 3);
  675. }
  676. if (rc)
  677. pr_err("[%d]: pm8058_pwm_write: rc=%d\n", pwm->pwm_id, rc);
  678. out_unlock:
  679. mutex_unlock(&pwm->chip->pwm_mutex);
  680. return rc;
  681. }
  682. EXPORT_SYMBOL(pm8058_pwm_config_duty_cycle);
  683. /**
  684. * pm8058_pwm_lut_config - change a PWM device configuration to use LUT
  685. *
  686. * @pwm: the PWM device
  687. * @period_us: period in micro second
  688. * @duty_pct: arrary of duty cycles in percent, like 20, 50.
  689. * @duty_time_ms: time for each duty cycle in millisecond
  690. * @start_idx: start index in lookup table from 0 to MAX-1
  691. * @idx_len: number of index
  692. * @pause_lo: pause time in millisecond at low index
  693. * @pause_hi: pause time in millisecond at high index
  694. * @flags: control flags
  695. */
  696. int pm8058_pwm_lut_config(struct pwm_device *pwm, int period_us,
  697. int duty_pct[], int duty_time_ms, int start_idx,
  698. int idx_len, int pause_lo, int pause_hi, int flags)
  699. {
  700. struct pm8058_pwm_lut lut;
  701. int len;
  702. int rc;
  703. if (pwm == NULL || IS_ERR(pwm) || !idx_len)
  704. return -EINVAL;
  705. if (duty_pct == NULL && !(flags & PM_PWM_LUT_NO_TABLE))
  706. return -EINVAL;
  707. if (pwm->chip == NULL)
  708. return -ENODEV;
  709. if (idx_len >= PM_PWM_LUT_SIZE && start_idx)
  710. return -EINVAL;
  711. if ((start_idx + idx_len) > PM_PWM_LUT_SIZE)
  712. return -EINVAL;
  713. if ((unsigned)period_us > PM_PWM_PERIOD_MAX ||
  714. (unsigned)period_us < PM_PWM_PERIOD_MIN)
  715. return -EINVAL;
  716. mutex_lock(&pwm->chip->pwm_mutex);
  717. if (!pwm->in_use) {
  718. rc = -EINVAL;
  719. goto out_unlock;
  720. }
  721. if (pwm->pwm_period != period_us) {
  722. pm8058_pwm_calc_period(period_us, &pwm->period);
  723. pm8058_pwm_save_period(pwm);
  724. pwm->pwm_period = period_us;
  725. }
  726. len = (idx_len > PM_PWM_LUT_SIZE) ? PM_PWM_LUT_SIZE : idx_len;
  727. if (flags & PM_PWM_LUT_NO_TABLE)
  728. goto after_table_write;
  729. rc = pm8058_pwm_change_table(pwm, duty_pct, start_idx, len, 0);
  730. if (rc) {
  731. pr_err("pm8058_pwm_change_table: rc=%d\n", rc);
  732. goto out_unlock;
  733. }
  734. after_table_write:
  735. lut.lut_duty_ms = duty_time_ms;
  736. lut.lut_lo_index = start_idx;
  737. lut.lut_hi_index = start_idx + len - 1;
  738. lut.lut_pause_lo = pause_lo;
  739. lut.lut_pause_hi = pause_hi;
  740. lut.flags = flags;
  741. rc = pm8058_pwm_change_lut(pwm, &lut);
  742. out_unlock:
  743. mutex_unlock(&pwm->chip->pwm_mutex);
  744. return rc;
  745. }
  746. EXPORT_SYMBOL(pm8058_pwm_lut_config);
  747. /**
  748. * pm8058_pwm_lut_enable - control a PWM device to start/stop LUT ramp
  749. *
  750. * @pwm: the PWM device
  751. * @start: to start (1), or stop (0)
  752. */
  753. int pm8058_pwm_lut_enable(struct pwm_device *pwm, int start)
  754. {
  755. if (pwm == NULL || IS_ERR(pwm))
  756. return -EINVAL;
  757. if (pwm->chip == NULL)
  758. return -ENODEV;
  759. mutex_lock(&pwm->chip->pwm_mutex);
  760. if (start) {
  761. pm8058_pwm_bank_enable(pwm, 1);
  762. pm8058_pwm_bank_sel(pwm);
  763. pm8058_pwm_start(pwm, 1, 1);
  764. } else {
  765. pm8058_pwm_bank_sel(pwm);
  766. pm8058_pwm_start(pwm, 0, 0);
  767. pm8058_pwm_bank_enable(pwm, 0);
  768. }
  769. mutex_unlock(&pwm->chip->pwm_mutex);
  770. return 0;
  771. }
  772. EXPORT_SYMBOL(pm8058_pwm_lut_enable);
  773. #define SSBI_REG_ADDR_LED_BASE 0x131
  774. #define SSBI_REG_ADDR_LED(n) (SSBI_REG_ADDR_LED_BASE + (n))
  775. #define SSBI_REG_ADDR_FLASH_BASE 0x48
  776. #define SSBI_REG_ADDR_FLASH_DRV_1 0xFB
  777. #define SSBI_REG_ADDR_FLASH(n) (((n) < 2 ? \
  778. SSBI_REG_ADDR_FLASH_BASE + (n) : \
  779. SSBI_REG_ADDR_FLASH_DRV_1))
  780. #define PM8058_LED_CURRENT_SHIFT 3
  781. #define PM8058_LED_MODE_MASK 0x07
  782. #define PM8058_FLASH_CURRENT_SHIFT 4
  783. #define PM8058_FLASH_MODE_MASK 0x03
  784. #define PM8058_FLASH_MODE_NONE 0
  785. #define PM8058_FLASH_MODE_DTEST1 1
  786. #define PM8058_FLASH_MODE_DTEST2 2
  787. #define PM8058_FLASH_MODE_PWM 3
  788. int pm8058_pwm_config_led(struct pwm_device *pwm, int id,
  789. int mode, int max_current)
  790. {
  791. int rc;
  792. u8 conf;
  793. switch (id) {
  794. case PM_PWM_LED_0:
  795. case PM_PWM_LED_1:
  796. case PM_PWM_LED_2:
  797. conf = mode & PM8058_LED_MODE_MASK;
  798. conf |= (max_current / 2) << PM8058_LED_CURRENT_SHIFT;
  799. rc = pm8xxx_writeb(pwm->dev->parent,
  800. SSBI_REG_ADDR_LED(id), conf);
  801. break;
  802. case PM_PWM_LED_KPD:
  803. case PM_PWM_LED_FLASH:
  804. case PM_PWM_LED_FLASH1:
  805. switch (mode) {
  806. case PM_PWM_CONF_PWM1:
  807. case PM_PWM_CONF_PWM2:
  808. case PM_PWM_CONF_PWM3:
  809. conf = PM8058_FLASH_MODE_PWM;
  810. break;
  811. case PM_PWM_CONF_DTEST1:
  812. conf = PM8058_FLASH_MODE_DTEST1;
  813. break;
  814. case PM_PWM_CONF_DTEST2:
  815. conf = PM8058_FLASH_MODE_DTEST2;
  816. break;
  817. default:
  818. conf = PM8058_FLASH_MODE_NONE;
  819. break;
  820. }
  821. conf |= (max_current / 20) << PM8058_FLASH_CURRENT_SHIFT;
  822. id -= PM_PWM_LED_KPD;
  823. rc = pm8xxx_writeb(pwm->dev->parent,
  824. SSBI_REG_ADDR_FLASH(id), conf);
  825. break;
  826. default:
  827. rc = -EINVAL;
  828. break;
  829. }
  830. return rc;
  831. }
  832. EXPORT_SYMBOL(pm8058_pwm_config_led);
  833. int pm8058_pwm_set_dtest(struct pwm_device *pwm, int enable)
  834. {
  835. int rc;
  836. u8 reg;
  837. if (pwm == NULL || IS_ERR(pwm))
  838. return -EINVAL;
  839. if (pwm->chip == NULL)
  840. return -ENODEV;
  841. if (!pwm->in_use)
  842. rc = -EINVAL;
  843. else {
  844. reg = pwm->pwm_id & PM8058_PWM_DTEST_BANK_MASK;
  845. if (enable)
  846. /* Only Test 1 available */
  847. reg |= (1 << PM8058_PWM_DTEST_SHIFT) &
  848. PM8058_PWM_DTEST_MASK;
  849. rc = pm8xxx_writeb(pwm->dev->parent,
  850. SSBI_REG_ADDR_LPG_TEST, reg);
  851. if (rc)
  852. pr_err("pm8xxx_write(DTEST=0x%x): rc=%d\n", reg, rc);
  853. }
  854. return rc;
  855. }
  856. EXPORT_SYMBOL(pm8058_pwm_set_dtest);
  857. static int __devinit pmic8058_pwm_probe(struct platform_device *pdev)
  858. {
  859. struct pm8058_pwm_chip *chip;
  860. int i;
  861. chip = kzalloc(sizeof *chip, GFP_KERNEL);
  862. if (chip == NULL) {
  863. pr_err("kzalloc() failed.\n");
  864. return -ENOMEM;
  865. }
  866. for (i = 0; i < PM8058_PWM_CHANNELS; i++) {
  867. chip->pwm_dev[i].pwm_id = i;
  868. chip->pwm_dev[i].chip = chip;
  869. chip->pwm_dev[i].dev = &pdev->dev;
  870. }
  871. mutex_init(&chip->pwm_mutex);
  872. chip->pdata = pdev->dev.platform_data;
  873. pwm_chip = chip;
  874. platform_set_drvdata(pdev, chip);
  875. pr_notice("OK\n");
  876. return 0;
  877. }
  878. static int __devexit pmic8058_pwm_remove(struct platform_device *pdev)
  879. {
  880. struct pm8058_pwm_chip *chip = platform_get_drvdata(pdev);
  881. platform_set_drvdata(pdev, NULL);
  882. kfree(chip);
  883. return 0;
  884. }
  885. static struct platform_driver pmic8058_pwm_driver = {
  886. .probe = pmic8058_pwm_probe,
  887. .remove = __devexit_p(pmic8058_pwm_remove),
  888. .driver = {
  889. .name = "pm8058-pwm",
  890. .owner = THIS_MODULE,
  891. },
  892. };
  893. static int __init pm8058_pwm_init(void)
  894. {
  895. return platform_driver_register(&pmic8058_pwm_driver);
  896. }
  897. static void __exit pm8058_pwm_exit(void)
  898. {
  899. platform_driver_unregister(&pmic8058_pwm_driver);
  900. }
  901. subsys_initcall(pm8058_pwm_init);
  902. module_exit(pm8058_pwm_exit);
  903. MODULE_LICENSE("GPL v2");
  904. MODULE_DESCRIPTION("PMIC8058 PWM driver");
  905. MODULE_VERSION("1.0");
  906. MODULE_ALIAS("platform:pmic8058_pwm");