blob: 708d658502d015dd005e5f1e4e090f64ff8b66fb [file] [log] [blame]
Jay Chokshi12cc1dd2012-04-30 17:07:35 -07001/* Copyright (c) 2012, Code Aurora Forum. 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 * Qualcomm QPNP Pulse Width Modulation (PWM) driver
14 *
15 * The HW module is also called LPG (Light Pattern Generator).
16 */
17
18#define pr_fmt(fmt) "%s: " fmt, __func__
19
20#include <linux/module.h>
21#include <linux/slab.h>
22#include <linux/err.h>
23#include <linux/spmi.h>
24#include <linux/of.h>
25#include <linux/of_device.h>
26#include <linux/radix-tree.h>
27#include <linux/qpnp/pwm.h>
28
29#define QPNP_LPG_DRIVER_NAME "qcom,qpnp-pwm"
30
31/* LPG Control for LPG_PATTERN_CONFIG */
32#define QPNP_RAMP_DIRECTION_SHIFT 4
33#define QPNP_RAMP_DIRECTION_MASK 0x10
34#define QPNP_PATTERN_REPEAT_SHIFT 3
35#define QPNP_PATTERN_REPEAT_MASK 0x08
36#define QPNP_RAMP_TOGGLE_SHIFT 2
37#define QPNP_RAMP_TOGGLE_MASK 0x04
38#define QPNP_EN_PAUSE_HI_SHIFT 1
39#define QPNP_EN_PAUSE_HI_MASK 0x02
40#define QPNP_EN_PAUSE_LO_MASK 0x01
41
42/* LPG Control for LPG_PWM_SIZE_CLK */
43#define QPNP_PWM_SIZE_SHIFT 4
44#define QPNP_PWM_SIZE_MASK 0x30
45#define QPNP_PWM_FREQ_CLK_SELECT_SHIFT 0
46#define QPNP_PWM_FREQ_CLK_SELECT_MASK 0x03
47#define QPNP_PWM_SIZE_9_BIT 0x03
48
49#define QPNP_SET_PWM_CLK(val, clk, pwm_size) \
50do { \
51 val = (clk + 1) & QPNP_PWM_FREQ_CLK_SELECT_MASK; \
52 val |= ((pwm_size > 6 ? QPNP_PWM_SIZE_9_BIT : 0) << \
53 QPNP_PWM_SIZE_SHIFT) & QPNP_PWM_SIZE_MASK; \
54} while (0)
55
56#define QPNP_GET_PWM_SIZE(reg) ((reg & QPNP_PWM_SIZE_MASK) \
57 >> QPNP_PWM_SIZE_SHIFT)
58
59/* LPG Control for LPG_PWM_FREQ_PREDIV_CLK */
60#define QPNP_PWM_FREQ_PRE_DIVIDE_SHIFT 5
61#define QPNP_PWM_FREQ_PRE_DIVIDE_MASK 0x60
62#define QPNP_PWM_FREQ_EXP_MASK 0x07
63
64#define QPNP_SET_PWM_FREQ_PREDIV(val, pre_div, pre_div_exp) \
65do { \
66 val = (pre_div << QPNP_PWM_FREQ_PRE_DIVIDE_SHIFT) & \
67 QPNP_PWM_FREQ_PRE_DIVIDE_MASK; \
68 val |= pre_div_exp & QPNP_PWM_FREQ_EXP_MASK; \
69} while (0)
70
71/* LPG Control for LPG_PWM_TYPE_CONFIG */
72#define QPNP_EN_GLITCH_REMOVAL_SHIFT 5
73#define QPNP_EN_GLITCH_REMOVAL_MASK 0x20
74#define QPNP_EN_FULL_SCALE_SHIFT 3
75#define QPNP_EN_FULL_SCALE_MASK 0x08
76#define QPNP_EN_PHASE_STAGGER_SHIFT 2
77#define QPNP_EN_PHASE_STAGGER_MASK 0x04
78#define QPNP_PHASE_STAGGER_MASK 0x03
79
80/* LPG Control for PWM_VALUE_LSB */
81#define QPNP_PWM_VALUE_LSB_MASK 0xFF
82
83/* LPG Control for PWM_VALUE_MSB */
84#define QPNP_PWM_VALUE_MSB_SHIFT 8
85#define QPNP_PWM_VALUE_MSB_MASK 0x01
86
87/* LPG Control for ENABLE_CONTROL */
88#define QPNP_EN_PWM_HIGH_SHIFT 7
89#define QPNP_EN_PWM_HIGH_MASK 0x80
90#define QPNP_EN_PWM_LO_SHIFT 6
91#define QPNP_EN_PWM_LO_MASK 0x40
92#define QPNP_EN_PWM_OUTPUT_SHIFT 5
93#define QPNP_EN_PWM_OUTPUT_MASK 0x20
94#define QPNP_PWM_SRC_SELECT_SHIFT 2
95#define QPNP_PWM_SRC_SELECT_MASK 0x04
96#define QPNP_PWM_EN_RAMP_GEN_SHIFT 1
97#define QPNP_PWM_EN_RAMP_GEN_MASK 0x02
98
99#define QPNP_ENABLE_PWM(value) \
100 (value |= (1 << QPNP_EN_PWM_OUTPUT_SHIFT) & QPNP_EN_PWM_OUTPUT_MASK)
101
102#define QPNP_DISABLE_PWM(value) (value &= ~QPNP_EN_PWM_OUTPUT_MASK)
103
104/* LPG Control for RAMP_CONTROL */
105#define QPNP_RAMP_START_MASK 0x01
106
107#define QPNP_ENABLE_LUT(value) (value |= QPNP_RAMP_START_MASK)
108#define QPNP_DISABLE_LUT(value) (value &= ~QPNP_RAMP_START_MASK)
109
110/* LPG Control for RAMP_STEP_DURATION_LSB */
111#define QPNP_RAMP_STEP_DURATION_LSB_MASK 0xFF
112
113/* LPG Control for RAMP_STEP_DURATION_MSB */
114#define QPNP_RAMP_STEP_DURATION_MSB_SHIFT 8
115#define QPNP_RAMP_STEP_DURATION_MSB_MASK 0x01
116
117#define QPNP_PWM_1KHZ 1024
118#define QPNP_GET_RAMP_STEP_DURATION(ramp_time_ms) \
119 ((ramp_time_ms * QPNP_PWM_1KHZ) / 1000)
120
121/* LPG Control for PAUSE_HI_MULTIPLIER_LSB */
122#define QPNP_PAUSE_HI_MULTIPLIER_LSB_MASK 0xFF
123
124/* LPG Control for PAUSE_HI_MULTIPLIER_MSB */
125#define QPNP_PAUSE_HI_MULTIPLIER_MSB_SHIFT 8
126#define QPNP_PAUSE_HI_MULTIPLIER_MSB_MASK 0x1F
127
128/* LPG Control for PAUSE_LO_MULTIPLIER_LSB */
129#define QPNP_PAUSE_LO_MULTIPLIER_LSB_MASK 0xFF
130
131/* LPG Control for PAUSE_LO_MULTIPLIER_MSB */
132#define QPNP_PAUSE_LO_MULTIPLIER_MSB_SHIFT 8
133#define QPNP_PAUSE_LO_MULTIPLIER_MSB_MASK 0x1F
134
135/* LPG Control for HI_INDEX */
136#define QPNP_HI_INDEX_MASK 0x3F
137
138/* LPG Control for LO_INDEX */
139#define QPNP_LO_INDEX_MASK 0x3F
140
141#define NUM_CLOCKS 3
142#define QPNP_PWM_M_MAX 7
143#define NSEC_1024HZ (NSEC_PER_SEC / 1024)
144#define NSEC_32768HZ (NSEC_PER_SEC / 32768)
145#define NSEC_19P2MHZ (NSEC_PER_SEC / 19200000)
146
147#define NUM_LPG_PRE_DIVIDE 4
148
149#define PRE_DIVIDE_1 1
150#define PRE_DIVIDE_3 3
151#define PRE_DIVIDE_5 5
152#define PRE_DIVIDE_6 6
153
154#define SPMI_LPG_REG_ADDR_BASE 0x40
155#define SPMI_LPG_REG_ADDR(b, n) (b + SPMI_LPG_REG_ADDR_BASE + (n))
156#define SPMI_MAX_BUF_LEN 8
157
158/* SPMI LPG registers */
159enum qpnp_lpg_registers_list {
160 QPNP_LPG_PATTERN_CONFIG,
161 QPNP_LPG_PWM_SIZE_CLK,
162 QPNP_LPG_PWM_FREQ_PREDIV_CLK,
163 QPNP_LPG_PWM_TYPE_CONFIG,
164 QPNP_PWM_VALUE_LSB,
165 QPNP_PWM_VALUE_MSB,
166 QPNP_ENABLE_CONTROL,
167 QPNP_RAMP_CONTROL,
168 QPNP_RAMP_STEP_DURATION_LSB = QPNP_RAMP_CONTROL + 9,
169 QPNP_RAMP_STEP_DURATION_MSB,
170 QPNP_PAUSE_HI_MULTIPLIER_LSB,
171 QPNP_PAUSE_HI_MULTIPLIER_MSB,
172 QPNP_PAUSE_LO_MULTIPLIER_LSB,
173 QPNP_PAUSE_LO_MULTIPLIER_MSB,
174 QPNP_HI_INDEX,
175 QPNP_LO_INDEX,
176 QPNP_TOTAL_LPG_SPMI_REGISTERS
177};
178
179/*
180 * Formula from HSID,
181 * pause_time (hi/lo) = (pause_cnt- 1)*(ramp_ms)
182 * OR,
183 * pause_cnt = (pause_time / ramp_ms) + 1
184 */
185#define QPNP_SET_PAUSE_CNT(to_pause_cnt, from_pause, ramp_ms) \
186 (to_pause_cnt = (from_pause / (ramp_ms ? ramp_ms : 1)) + 1)
187
188
189static unsigned int pt_t[NUM_LPG_PRE_DIVIDE][NUM_CLOCKS] = {
190 { PRE_DIVIDE_1 * NSEC_1024HZ,
191 PRE_DIVIDE_1 * NSEC_32768HZ,
192 PRE_DIVIDE_1 * NSEC_19P2MHZ,
193 },
194 { PRE_DIVIDE_3 * NSEC_1024HZ,
195 PRE_DIVIDE_3 * NSEC_32768HZ,
196 PRE_DIVIDE_3 * NSEC_19P2MHZ,
197 },
198 { PRE_DIVIDE_5 * NSEC_1024HZ,
199 PRE_DIVIDE_5 * NSEC_32768HZ,
200 PRE_DIVIDE_5 * NSEC_19P2MHZ,
201 },
202 { PRE_DIVIDE_6 * NSEC_1024HZ,
203 PRE_DIVIDE_6 * NSEC_32768HZ,
204 PRE_DIVIDE_6 * NSEC_19P2MHZ,
205 },
206};
207
208static RADIX_TREE(lpg_dev_tree, GFP_KERNEL);
209
210struct qpnp_lut_default_config {
211 u32 *duty_pct_list;
212 int size;
213 int start_idx;
214};
215
216struct qpnp_lut_config {
217 struct qpnp_lut_default_config def_config;
218 u8 *duty_pct_list;
219 int list_size;
220 int lo_index;
221 int hi_index;
222 int lut_pause_hi_cnt;
223 int lut_pause_lo_cnt;
224 int ramp_step_ms;
225 bool ramp_direction;
226 bool pattern_repeat;
227 bool ramp_toggle;
228 bool enable_pause_hi;
229 bool enable_pause_lo;
230};
231
232struct qpnp_lpg_config {
233 struct qpnp_lut_config lut_config;
234 u16 base_addr;
235 u16 lut_base_addr;
236 u16 lut_size;
237 bool bypass_lut;
238 bool lpg_configured;
239};
240
241struct qpnp_pwm_config {
242 int channel_id;
243 bool in_use;
244 const char *lable;
245 int pwm_value;
246 int pwm_period;
247 int pwm_duty;
248 struct pwm_period_config period;
249};
250
251/* Public facing structure */
252struct pwm_device {
253 struct qpnp_lpg_chip *chip;
254 struct qpnp_pwm_config pwm_config;
255};
256
257struct qpnp_lpg_chip {
258 struct spmi_device *spmi_dev;
259 struct pwm_device pwm_dev;
260 struct mutex lpg_mutex;
261 struct qpnp_lpg_config lpg_config;
262 u8 qpnp_lpg_registers[QPNP_TOTAL_LPG_SPMI_REGISTERS];
263};
264
265/* Internal functions */
266static inline void qpnp_set_pattern_config(u8 *val,
267 struct qpnp_lut_config *lut_config)
268{
269 *val = lut_config->enable_pause_lo & QPNP_EN_PAUSE_LO_MASK;
270 *val |= (lut_config->enable_pause_hi << QPNP_EN_PAUSE_HI_SHIFT) &
271 QPNP_EN_PAUSE_HI_MASK;
272 *val |= (lut_config->ramp_toggle << QPNP_RAMP_TOGGLE_SHIFT) &
273 QPNP_RAMP_TOGGLE_MASK;
274 *val |= (lut_config->pattern_repeat << QPNP_PATTERN_REPEAT_SHIFT) &
275 QPNP_PATTERN_REPEAT_MASK;
276 *val |= (lut_config->ramp_direction << QPNP_RAMP_DIRECTION_SHIFT) &
277 QPNP_RAMP_DIRECTION_MASK;
278}
279
280static inline void qpnp_set_pwm_type_config(u8 *val, bool glitch,
281 bool full_scale, bool en_phase, bool phase)
282{
283 *val = phase;
284 *val |= (en_phase << QPNP_EN_PHASE_STAGGER_SHIFT) &
285 QPNP_EN_PHASE_STAGGER_MASK;
286 *val |= (full_scale << QPNP_EN_FULL_SCALE_SHIFT) &
287 QPNP_EN_FULL_SCALE_MASK;
288 *val |= (glitch << QPNP_EN_GLITCH_REMOVAL_SHIFT) &
289 QPNP_EN_GLITCH_REMOVAL_MASK;
290}
291
292static inline void qpnp_set_control(u8 *val, bool pwm_hi, bool pwm_lo,
293 bool pwm_out, bool pwm_src, bool ramp_gen)
294{
295 *val = (ramp_gen << QPNP_PWM_EN_RAMP_GEN_SHIFT) &
296 QPNP_PWM_EN_RAMP_GEN_MASK;
297 *val |= (pwm_src << QPNP_PWM_SRC_SELECT_SHIFT) &
298 QPNP_PWM_SRC_SELECT_MASK;
299 *val |= (pwm_out << QPNP_EN_PWM_OUTPUT_SHIFT) &
300 QPNP_EN_PWM_OUTPUT_MASK;
301 *val |= (pwm_lo << QPNP_EN_PWM_LO_SHIFT) & QPNP_EN_PWM_LO_MASK;
302 *val |= (pwm_hi << QPNP_EN_PWM_HIGH_SHIFT) & QPNP_EN_PWM_HIGH_MASK;
303}
304
305#define QPNP_ENABLE_LUT_CONTROL(p_val) qpnp_set_control(p_val, 1, 1, 1, 0, 1)
306#define QPNP_ENABLE_PWM_CONTROL(p_val) qpnp_set_control(p_val, 1, 1, 0, 1, 0)
307
308static inline void qpnp_convert_to_lut_flags(int *flags,
309 struct qpnp_lut_config *l_config)
310{
311 *flags = ((l_config->ramp_direction ? PM_PWM_LUT_RAMP_UP : 0) |
312 (l_config->pattern_repeat ? PM_PWM_LUT_LOOP : 0)|
313 (l_config->ramp_toggle ? PM_PWM_LUT_REVERSE : 0) |
314 (l_config->enable_pause_hi ? PM_PWM_LUT_PAUSE_HI_EN : 0) |
315 (l_config->enable_pause_lo ? PM_PWM_LUT_PAUSE_LO_EN : 0));
316}
317
318static inline void qpnp_set_lut_params(struct lut_params *l_params,
319 struct qpnp_lut_config *l_config)
320{
321 l_params->start_idx = l_config->def_config.start_idx;
322 l_params->idx_len = l_config->def_config.size;
323 l_params->lut_pause_hi = l_config->lut_pause_hi_cnt;
324 l_params->lut_pause_lo = l_config->lut_pause_lo_cnt;
325 l_params->ramp_step_ms = l_config->ramp_step_ms;
326 qpnp_convert_to_lut_flags(&l_params->flags, l_config);
327}
328
329static void qpnp_lpg_save(u8 *u8p, u8 mask, u8 val)
330{
331 *u8p &= ~mask;
332 *u8p |= val & mask;
333}
334
335static int qpnp_lpg_save_and_write(u8 value, u8 mask, u8 *reg, u16 base_addr,
336 u16 offset, u16 size, struct qpnp_lpg_chip *chip)
337{
338 qpnp_lpg_save(reg, mask, value);
339
340 return spmi_ext_register_writel(chip->spmi_dev->ctrl,
341 chip->spmi_dev->sid, SPMI_LPG_REG_ADDR(base_addr, offset), reg, size);
342}
343
344/*
345 * PWM Frequency = Clock Frequency / (N * T)
346 * or
347 * PWM Period = Clock Period * (N * T)
348 * where
349 * N = 2^9 or 2^6 for 9-bit or 6-bit PWM size
350 * T = Pre-divide * 2^m, where m = 0..7 (exponent)
351 *
352 * This is the formula to figure out m for the best pre-divide and clock:
353 * (PWM Period / N) = (Pre-divide * Clock Period) * 2^m
354 */
355static void qpnp_lpg_calc_period(unsigned int period_us,
356 struct pwm_period_config *period)
357{
358 int n, m, clk, div;
359 int best_m, best_div, best_clk;
360 unsigned int last_err, cur_err, min_err;
361 unsigned int tmp_p, period_n;
362
363 /* PWM Period / N */
364 if (period_us < ((unsigned)(-1) / NSEC_PER_USEC)) {
365 period_n = (period_us * NSEC_PER_USEC) >> 6;
366 n = 6;
367 } else {
368 period_n = (period_us >> 9) * NSEC_PER_USEC;
369 n = 9;
370 }
371
372 min_err = last_err = (unsigned)(-1);
373 best_m = 0;
374 best_clk = 0;
375 best_div = 0;
376 for (clk = 0; clk < NUM_CLOCKS; clk++) {
377 for (div = 0; div < NUM_LPG_PRE_DIVIDE; div++) {
378 /* period_n = (PWM Period / N) */
379 /* tmp_p = (Pre-divide * Clock Period) * 2^m */
380 tmp_p = pt_t[div][clk];
381 for (m = 0; m <= QPNP_PWM_M_MAX; m++) {
382 if (period_n > tmp_p)
383 cur_err = period_n - tmp_p;
384 else
385 cur_err = tmp_p - period_n;
386
387 if (cur_err < min_err) {
388 min_err = cur_err;
389 best_m = m;
390 best_clk = clk;
391 best_div = div;
392 }
393
394 if (m && cur_err > last_err)
395 /* Break for bigger cur_err */
396 break;
397
398 last_err = cur_err;
399 tmp_p <<= 1;
400 }
401 }
402 }
403
404 /* Use higher resolution */
405 if (best_m >= 3 && n == 6) {
406 n += 3;
407 best_m -= 3;
408 }
409
410 period->pwm_size = n;
411 period->clk = best_clk;
412 period->pre_div = best_div;
413 period->pre_div_exp = best_m;
414}
415
416static void qpnp_lpg_calc_pwm_value(struct pwm_device *pwm,
417 unsigned int period_us,
418 unsigned int duty_us)
419{
420 unsigned int max_pwm_value, tmp;
421 struct qpnp_pwm_config *pwm_config = &pwm->pwm_config;
422
423 /* Figure out pwm_value with overflow handling */
424 tmp = 1 << (sizeof(tmp) * 8 - pwm_config->period.pwm_size);
425 if (duty_us < tmp) {
426 tmp = duty_us << pwm_config->period.pwm_size;
427 pwm_config->pwm_value = tmp / period_us;
428 } else {
429 tmp = period_us >> pwm_config->period.pwm_size;
430 pwm_config->pwm_value = duty_us / tmp;
431 }
432 max_pwm_value = (1 << pwm_config->period.pwm_size) - 1;
433 if (pwm_config->pwm_value > max_pwm_value)
434 pwm_config->pwm_value = max_pwm_value;
435}
436
437static int qpnp_lpg_change_table(struct pwm_device *pwm,
438 int duty_pct[], int raw_value)
439{
440 unsigned int pwm_value, max_pwm_value;
441 struct qpnp_lpg_chip *chip = pwm->chip;
442 struct qpnp_lut_config *lut = &chip->lpg_config.lut_config;
Jay Chokshieef72ab2012-07-27 14:02:35 -0700443 int i, pwm_size, rc = 0;
Jay Chokshi12cc1dd2012-04-30 17:07:35 -0700444 int burst_size = SPMI_MAX_BUF_LEN;
445 int list_len = lut->list_size << 1;
446 int offset = lut->lo_index << 2;
447
448 pwm_size = QPNP_GET_PWM_SIZE(
449 chip->qpnp_lpg_registers[QPNP_LPG_PWM_SIZE_CLK]) &
450 QPNP_PWM_SIZE_9_BIT ? 9 : 6;
451
452 max_pwm_value = (1 << pwm_size) - 1;
453
454 if (unlikely(lut->list_size != (lut->hi_index - lut->lo_index + 1))) {
455 pr_err("LUT internal Data structure corruption detected\n");
456 pr_err("LUT list size: %d\n", lut->list_size);
457 pr_err("However, index size is: %d\n",
458 (lut->hi_index - lut->lo_index + 1));
459 return -EINVAL;
460 }
461
462 for (i = 0; i <= lut->list_size; i++) {
463 if (raw_value)
464 pwm_value = duty_pct[i];
465 else
466 pwm_value = (duty_pct[i] << pwm_size) / 100;
467
468 if (pwm_value > max_pwm_value)
469 pwm_value = max_pwm_value;
470
471 lut->duty_pct_list[i*2] = pwm_value;
472 lut->duty_pct_list[(i*2)+1] = (pwm_value >>
473 QPNP_PWM_VALUE_MSB_SHIFT) & QPNP_PWM_VALUE_MSB_MASK;
474 }
475
476 /* Write with max allowable burst mode, each entry is of two bytes */
477 for (i = 0; i < list_len;) {
478 if (i + burst_size >= list_len)
479 burst_size = list_len - i;
480 rc = spmi_ext_register_writel(chip->spmi_dev->ctrl,
481 chip->spmi_dev->sid,
482 chip->lpg_config.lut_base_addr + offset + i,
483 lut->duty_pct_list + i, burst_size);
484 i += burst_size;
485 }
486
487 return rc;
488}
489
490static void qpnp_lpg_save_period(struct pwm_device *pwm)
491{
492 u8 mask, val;
493 struct qpnp_lpg_chip *chip = pwm->chip;
494 struct qpnp_pwm_config *pwm_config = &pwm->pwm_config;
495
496 QPNP_SET_PWM_CLK(val, pwm_config->period.clk,
497 pwm_config->period.pwm_size);
498
499 mask = QPNP_PWM_SIZE_MASK | QPNP_PWM_FREQ_CLK_SELECT_MASK;
500
501 qpnp_lpg_save(&chip->qpnp_lpg_registers[QPNP_LPG_PWM_SIZE_CLK],
502 mask, val);
503
504 QPNP_SET_PWM_FREQ_PREDIV(val, pwm_config->period.pre_div,
505 pwm_config->period.pre_div_exp);
506
507 mask = QPNP_PWM_FREQ_PRE_DIVIDE_MASK | QPNP_PWM_FREQ_EXP_MASK;
508
509 qpnp_lpg_save(&chip->qpnp_lpg_registers[QPNP_LPG_PWM_FREQ_PREDIV_CLK],
510 mask, val);
511}
512
513static int qpnp_lpg_save_pwm_value(struct pwm_device *pwm)
514{
515 unsigned int max_pwm_value;
516 int pwm_size;
517 u8 mask, value;
518 struct qpnp_lpg_chip *chip = pwm->chip;
519 struct qpnp_pwm_config *pwm_config = &pwm->pwm_config;
520 struct qpnp_lpg_config *lpg_config = &chip->lpg_config;
521 int rc;
522
523 pwm_size = QPNP_GET_PWM_SIZE(
524 chip->qpnp_lpg_registers[QPNP_LPG_PWM_SIZE_CLK]) &
525 QPNP_PWM_SIZE_9_BIT ? 9 : 6;
526
527 max_pwm_value = (1 << pwm_size) - 1;
528
529 if (pwm_config->pwm_value > max_pwm_value)
530 pwm_config->pwm_value = max_pwm_value;
531
532 value = pwm_config->pwm_value;
533 mask = QPNP_PWM_VALUE_LSB_MASK;
534
535 rc = qpnp_lpg_save_and_write(value, mask,
536 &pwm->chip->qpnp_lpg_registers[QPNP_PWM_VALUE_LSB],
537 lpg_config->base_addr, QPNP_PWM_VALUE_LSB, 1, chip);
538 if (rc)
539 return rc;
540
541 value = (pwm_config->pwm_value >> QPNP_PWM_VALUE_MSB_SHIFT) &
542 QPNP_PWM_VALUE_MSB_MASK;
543
544 mask = QPNP_PWM_VALUE_MSB_MASK;
545
546 return qpnp_lpg_save_and_write(value, mask,
547 &pwm->chip->qpnp_lpg_registers[QPNP_PWM_VALUE_MSB],
548 lpg_config->base_addr, QPNP_PWM_VALUE_MSB, 1, chip);
549}
550
551static int qpnp_lpg_configure_pattern(struct pwm_device *pwm)
552{
553 struct qpnp_lpg_config *lpg_config = &pwm->chip->lpg_config;
554 struct qpnp_lut_config *lut_config = &lpg_config->lut_config;
555 struct qpnp_lpg_chip *chip = pwm->chip;
556 u8 value, mask;
557
558 qpnp_set_pattern_config(&value, lut_config);
559
560 mask = QPNP_RAMP_DIRECTION_MASK | QPNP_PATTERN_REPEAT_MASK |
561 QPNP_RAMP_TOGGLE_MASK | QPNP_EN_PAUSE_HI_MASK |
562 QPNP_EN_PAUSE_LO_MASK;
563
564 return qpnp_lpg_save_and_write(value, mask,
565 &pwm->chip->qpnp_lpg_registers[QPNP_LPG_PATTERN_CONFIG],
566 lpg_config->base_addr, QPNP_LPG_PATTERN_CONFIG, 1, chip);
567}
568
569static int qpnp_lpg_configure_pwm(struct pwm_device *pwm)
570{
571 struct qpnp_lpg_config *lpg_config = &pwm->chip->lpg_config;
572 struct qpnp_lpg_chip *chip = pwm->chip;
573 int rc;
574 u8 value, mask;
575
576 rc = spmi_ext_register_writel(chip->spmi_dev->ctrl, chip->spmi_dev->sid,
577 SPMI_LPG_REG_ADDR(lpg_config->base_addr, QPNP_LPG_PWM_SIZE_CLK),
578 &chip->qpnp_lpg_registers[QPNP_LPG_PWM_SIZE_CLK], 1);
579
580 if (rc)
581 return rc;
582
583 rc = spmi_ext_register_writel(chip->spmi_dev->ctrl, chip->spmi_dev->sid,
584 SPMI_LPG_REG_ADDR(lpg_config->base_addr,
585 QPNP_LPG_PWM_FREQ_PREDIV_CLK),
586 &chip->qpnp_lpg_registers[QPNP_LPG_PWM_FREQ_PREDIV_CLK], 1);
587 if (rc)
588 return rc;
589
590 qpnp_set_pwm_type_config(&value, 1, 0, 0, 0);
591
592 mask = QPNP_EN_GLITCH_REMOVAL_MASK | QPNP_EN_FULL_SCALE_MASK |
593 QPNP_EN_PHASE_STAGGER_MASK | QPNP_PHASE_STAGGER_MASK;
594
595 return qpnp_lpg_save_and_write(value, mask,
596 &pwm->chip->qpnp_lpg_registers[QPNP_LPG_PWM_TYPE_CONFIG],
597 lpg_config->base_addr, QPNP_LPG_PWM_TYPE_CONFIG, 1, chip);
598}
599
600static int qpnp_pwm_configure_control(struct pwm_device *pwm)
601{
602 struct qpnp_lpg_config *lpg_config = &pwm->chip->lpg_config;
603 struct qpnp_lpg_chip *chip = pwm->chip;
604 u8 value, mask;
605
606 QPNP_ENABLE_PWM_CONTROL(&value);
607
608 mask = QPNP_EN_PWM_HIGH_MASK | QPNP_EN_PWM_LO_MASK |
609 QPNP_EN_PWM_OUTPUT_MASK | QPNP_PWM_SRC_SELECT_MASK |
610 QPNP_PWM_EN_RAMP_GEN_MASK;
611
612 return qpnp_lpg_save_and_write(value, mask,
613 &pwm->chip->qpnp_lpg_registers[QPNP_ENABLE_CONTROL],
614 lpg_config->base_addr, QPNP_ENABLE_CONTROL, 1, chip);
615
616}
617
618static int qpnp_lpg_configure_control(struct pwm_device *pwm)
619{
620 struct qpnp_lpg_config *lpg_config = &pwm->chip->lpg_config;
621 struct qpnp_lpg_chip *chip = pwm->chip;
622 u8 value, mask;
623
624 QPNP_ENABLE_LUT_CONTROL(&value);
625
626 mask = QPNP_EN_PWM_HIGH_MASK | QPNP_EN_PWM_LO_MASK |
627 QPNP_EN_PWM_OUTPUT_MASK | QPNP_PWM_SRC_SELECT_MASK |
628 QPNP_PWM_EN_RAMP_GEN_MASK;
629
630 return qpnp_lpg_save_and_write(value, mask,
631 &pwm->chip->qpnp_lpg_registers[QPNP_ENABLE_CONTROL],
632 lpg_config->base_addr, QPNP_ENABLE_CONTROL, 1, chip);
633
634}
635
636static int qpnp_lpg_configure_ramp_step_duration(struct pwm_device *pwm)
637{
638 struct qpnp_lpg_config *lpg_config = &pwm->chip->lpg_config;
639 struct qpnp_lut_config lut_config = lpg_config->lut_config;
640 struct qpnp_lpg_chip *chip = pwm->chip;
641 int rc, value;
642 u8 val, mask;
643
644 value = QPNP_GET_RAMP_STEP_DURATION(lut_config.ramp_step_ms);
645 val = value & QPNP_RAMP_STEP_DURATION_LSB_MASK;
646 mask = QPNP_RAMP_STEP_DURATION_LSB_MASK;
647
648 rc = qpnp_lpg_save_and_write(val, mask,
649 &pwm->chip->qpnp_lpg_registers[QPNP_RAMP_STEP_DURATION_LSB],
650 lpg_config->base_addr, QPNP_RAMP_STEP_DURATION_LSB, 1, chip);
651 if (rc)
652 return rc;
653
654 val = (value >> QPNP_RAMP_STEP_DURATION_MSB_SHIFT) &
655 QPNP_RAMP_STEP_DURATION_MSB_MASK;
656
657 mask = QPNP_RAMP_STEP_DURATION_MSB_MASK;
658
659 return qpnp_lpg_save_and_write(val, mask,
660 &pwm->chip->qpnp_lpg_registers[QPNP_RAMP_STEP_DURATION_MSB],
661 lpg_config->base_addr, QPNP_RAMP_STEP_DURATION_MSB, 1, chip);
662}
663
664static int qpnp_lpg_configure_pause(struct pwm_device *pwm)
665{
666 struct qpnp_lpg_config *lpg_config = &pwm->chip->lpg_config;
667 struct qpnp_lut_config lut_config = lpg_config->lut_config;
668 struct qpnp_lpg_chip *chip = pwm->chip;
669 u8 value, mask;
670 int rc = 0;
671
672 if (lut_config.enable_pause_hi) {
673 value = lut_config.lut_pause_hi_cnt;
674 mask = QPNP_PAUSE_HI_MULTIPLIER_LSB_MASK;
675
676 rc = qpnp_lpg_save_and_write(value, mask,
677 &pwm->chip->qpnp_lpg_registers[QPNP_PAUSE_HI_MULTIPLIER_LSB],
678 lpg_config->base_addr, QPNP_PAUSE_HI_MULTIPLIER_LSB, 1, chip);
679 if (rc)
680 return rc;
681
682 value = (lut_config.lut_pause_hi_cnt >>
683 QPNP_PAUSE_HI_MULTIPLIER_MSB_SHIFT) &
684 QPNP_PAUSE_HI_MULTIPLIER_MSB_MASK;
685
686 mask = QPNP_PAUSE_HI_MULTIPLIER_MSB_MASK;
687
688 rc = qpnp_lpg_save_and_write(value, mask,
689 &pwm->chip->qpnp_lpg_registers[QPNP_PAUSE_HI_MULTIPLIER_MSB],
690 lpg_config->base_addr, QPNP_PAUSE_HI_MULTIPLIER_MSB, 1, chip);
691 } else {
692 value = 0;
693 mask = QPNP_PAUSE_HI_MULTIPLIER_LSB_MASK;
694
695 rc = qpnp_lpg_save_and_write(value, mask,
696 &pwm->chip->qpnp_lpg_registers[QPNP_PAUSE_HI_MULTIPLIER_LSB],
697 lpg_config->base_addr, QPNP_PAUSE_HI_MULTIPLIER_LSB, 1, chip);
698 if (rc)
699 return rc;
700
701 mask = QPNP_PAUSE_HI_MULTIPLIER_MSB_MASK;
702
703 rc = qpnp_lpg_save_and_write(value, mask,
704 &pwm->chip->qpnp_lpg_registers[QPNP_PAUSE_HI_MULTIPLIER_MSB],
705 lpg_config->base_addr, QPNP_PAUSE_HI_MULTIPLIER_MSB, 1, chip);
706 if (rc)
707 return rc;
708
709 }
710
711 if (lut_config.enable_pause_lo) {
712 value = lut_config.lut_pause_lo_cnt;
713 mask = QPNP_PAUSE_LO_MULTIPLIER_LSB_MASK;
714
715 rc = qpnp_lpg_save_and_write(value, mask,
716 &pwm->chip->qpnp_lpg_registers[QPNP_PAUSE_LO_MULTIPLIER_LSB],
717 lpg_config->base_addr, QPNP_PAUSE_LO_MULTIPLIER_LSB, 1, chip);
718 if (rc)
719 return rc;
720
721 value = (lut_config.lut_pause_lo_cnt >>
722 QPNP_PAUSE_LO_MULTIPLIER_MSB_SHIFT) &
723 QPNP_PAUSE_LO_MULTIPLIER_MSB_MASK;
724
725 mask = QPNP_PAUSE_LO_MULTIPLIER_MSB_MASK;
726
727 rc = qpnp_lpg_save_and_write(value, mask,
728 &pwm->chip->qpnp_lpg_registers[QPNP_PAUSE_LO_MULTIPLIER_MSB],
729 lpg_config->base_addr, QPNP_PAUSE_LO_MULTIPLIER_MSB, 1, chip);
730 } else {
731 value = 0;
732 mask = QPNP_PAUSE_LO_MULTIPLIER_LSB_MASK;
733
734 rc = qpnp_lpg_save_and_write(value, mask,
735 &pwm->chip->qpnp_lpg_registers[QPNP_PAUSE_LO_MULTIPLIER_LSB],
736 lpg_config->base_addr, QPNP_PAUSE_LO_MULTIPLIER_LSB, 1, chip);
737 if (rc)
738 return rc;
739
740 mask = QPNP_PAUSE_LO_MULTIPLIER_MSB_MASK;
741
742 rc = qpnp_lpg_save_and_write(value, mask,
743 &pwm->chip->qpnp_lpg_registers[QPNP_PAUSE_LO_MULTIPLIER_MSB],
744 lpg_config->base_addr, QPNP_PAUSE_LO_MULTIPLIER_MSB, 1, chip);
745 return rc;
746 }
747
748 return rc;
749}
750
751static int qpnp_lpg_configure_index(struct pwm_device *pwm)
752{
753 struct qpnp_lpg_config *lpg_config = &pwm->chip->lpg_config;
754 struct qpnp_lut_config lut_config = lpg_config->lut_config;
755 struct qpnp_lpg_chip *chip = pwm->chip;
756 u8 value, mask;
757 int rc = 0;
758
759 value = lut_config.hi_index;
760 mask = QPNP_HI_INDEX_MASK;
761
762 rc = qpnp_lpg_save_and_write(value, mask,
763 &pwm->chip->qpnp_lpg_registers[QPNP_HI_INDEX],
764 lpg_config->base_addr, QPNP_HI_INDEX, 1, chip);
765 if (rc)
766 return rc;
767
768 value = lut_config.lo_index;
769 mask = QPNP_LO_INDEX_MASK;
770
771 rc = qpnp_lpg_save_and_write(value, mask,
772 &pwm->chip->qpnp_lpg_registers[QPNP_LO_INDEX],
773 lpg_config->base_addr, QPNP_LO_INDEX, 1, chip);
774
775 return rc;
776}
777
778static int qpnp_lpg_change_lut(struct pwm_device *pwm)
779{
780 int rc;
781
782 rc = qpnp_lpg_configure_pattern(pwm);
783 if (rc) {
784 pr_err("Failed to configure LUT pattern");
785 return rc;
786 }
787 rc = qpnp_lpg_configure_pwm(pwm);
788 if (rc) {
789 pr_err("Failed to configure LUT pattern");
790 return rc;
791 }
792 rc = qpnp_lpg_configure_control(pwm);
793 if (rc) {
794 pr_err("Failed to configure pause registers");
795 return rc;
796 }
797 rc = qpnp_lpg_configure_ramp_step_duration(pwm);
798 if (rc) {
799 pr_err("Failed to configure duty time");
800 return rc;
801 }
802 rc = qpnp_lpg_configure_pause(pwm);
803 if (rc) {
804 pr_err("Failed to configure pause registers");
805 return rc;
806 }
807 rc = qpnp_lpg_configure_index(pwm);
808 if (rc) {
809 pr_err("Failed to configure index registers");
810 return rc;
811 }
812 return rc;
813}
814
815static int qpnp_lpg_enable_lut(struct pwm_device *pwm)
816{
817 struct qpnp_lpg_config *lpg_config = &pwm->chip->lpg_config;
818 struct qpnp_lpg_chip *chip = pwm->chip;
819 u8 value, mask;
820
821 value = pwm->chip->qpnp_lpg_registers[QPNP_RAMP_CONTROL];
822
823 QPNP_ENABLE_LUT(value);
824
825 mask = QPNP_RAMP_START_MASK;
826
827 return qpnp_lpg_save_and_write(value, mask,
828 &pwm->chip->qpnp_lpg_registers[QPNP_RAMP_CONTROL],
829 lpg_config->base_addr, QPNP_RAMP_CONTROL, 1, chip);
830}
831
832static int qpnp_lpg_disable_lut(struct pwm_device *pwm)
833{
834 struct qpnp_lpg_config *lpg_config = &pwm->chip->lpg_config;
835 struct qpnp_lpg_chip *chip = pwm->chip;
836 u8 value, mask;
837
838 value = pwm->chip->qpnp_lpg_registers[QPNP_RAMP_CONTROL];
839
840 QPNP_DISABLE_LUT(value);
841
842 mask = QPNP_RAMP_START_MASK;
843
844 return qpnp_lpg_save_and_write(value, mask,
845 &pwm->chip->qpnp_lpg_registers[QPNP_RAMP_CONTROL],
846 lpg_config->base_addr, QPNP_RAMP_CONTROL, 1, chip);
847}
848
849static int qpnp_lpg_enable_pwm(struct pwm_device *pwm)
850{
851 struct qpnp_lpg_config *lpg_config = &pwm->chip->lpg_config;
852 struct qpnp_lpg_chip *chip = pwm->chip;
853 u8 value, mask;
854
855 value = pwm->chip->qpnp_lpg_registers[QPNP_ENABLE_CONTROL];
856
857 QPNP_ENABLE_PWM(value);
858
859 mask = QPNP_EN_PWM_OUTPUT_MASK;
860
861 return qpnp_lpg_save_and_write(value, mask,
862 &pwm->chip->qpnp_lpg_registers[QPNP_ENABLE_CONTROL],
863 lpg_config->base_addr, QPNP_RAMP_CONTROL, 1, chip);
864}
865
866static int qpnp_lpg_disable_pwm(struct pwm_device *pwm)
867{
868 struct qpnp_lpg_config *lpg_config = &pwm->chip->lpg_config;
869 struct qpnp_lpg_chip *chip = pwm->chip;
870 u8 value, mask;
871
872 value = pwm->chip->qpnp_lpg_registers[QPNP_ENABLE_CONTROL];
873
874 QPNP_DISABLE_PWM(value);
875
876 mask = QPNP_EN_PWM_OUTPUT_MASK;
877
878 return qpnp_lpg_save_and_write(value, mask,
879 &pwm->chip->qpnp_lpg_registers[QPNP_ENABLE_CONTROL],
880 lpg_config->base_addr, QPNP_RAMP_CONTROL, 1, chip);
881}
882
883static int _pwm_config(struct pwm_device *pwm, int duty_us, int period_us)
884{
885 struct qpnp_pwm_config *pwm_config;
886 struct qpnp_lpg_chip *chip;
887 struct pwm_period_config *period;
888 int rc;
889
890 chip = pwm->chip;
891 pwm_config = &pwm->pwm_config;
892 period = &pwm_config->period;
893
894 if (pwm_config->pwm_period != period_us) {
895 qpnp_lpg_calc_period(period_us, period);
896 qpnp_lpg_save_period(pwm);
897 pwm_config->pwm_period = period_us;
898 }
899
900 pwm_config->pwm_duty = duty_us;
901 qpnp_lpg_calc_pwm_value(pwm, period_us, duty_us);
902 rc = qpnp_lpg_save_pwm_value(pwm);
903
904 if (rc) {
905 pr_err("Could not update PWM value for channel %d rc=%d\n",
906 pwm_config->channel_id, rc);
907 return rc;
908 }
909
910 rc = qpnp_lpg_configure_pwm(pwm);
911 if (rc) {
912 pr_err("Could not configure PWM clock for\n");
913 pr_err("channel %d rc=%d\n", pwm_config->channel_id, rc);
914 return rc;
915 }
916
917 rc = qpnp_pwm_configure_control(pwm);
918 if (rc) {
919 pr_err("Could not update PWM control for");
920 pr_err("channel %d rc=%d\n", pwm_config->channel_id, rc);
921 return rc;
922 }
923
924 pwm->chip->lpg_config.lpg_configured = 1;
925
926 pr_debug("duty/period=%u/%u usec: pwm_value=%d (of %d)\n",
927 (unsigned)duty_us, (unsigned)period_us,
928 pwm_config->pwm_value, 1 << period->pwm_size);
929
930 return 0;
931}
932
933static int _pwm_lut_config(struct pwm_device *pwm, int period_us,
934 int duty_pct[], struct lut_params lut_params)
935{
936 struct qpnp_lpg_config *lpg_config;
937 struct qpnp_lut_config *lut_config;
938 struct qpnp_lut_default_config *def_lut_config =
939 &lut_config->def_config;
940 struct pwm_period_config *period;
941 struct qpnp_pwm_config *pwm_config;
942 int start_idx = lut_params.start_idx;
943 int len = lut_params.idx_len;
944 int flags = lut_params.flags;
945 int raw_lut, ramp_step_ms;
946 int rc = 0;
947
948 pwm_config = &pwm->pwm_config;
949 lpg_config = &pwm->chip->lpg_config;
950 lut_config = &lpg_config->lut_config;
951 def_lut_config = &lut_config->def_config;
952
953 if ((start_idx + len) > lpg_config->lut_size) {
954 pr_err("Exceed LUT limit\n");
955 return -EINVAL;
956 }
957 if ((unsigned)period_us > PM_PWM_PERIOD_MAX ||
958 (unsigned)period_us < PM_PWM_PERIOD_MIN) {
959 pr_err("Period out of range\n");
960 return -EINVAL;
961 }
962
963 if (!pwm_config->in_use) {
964 pr_err("channel_id: %d: stale handle?\n",
965 pwm_config->channel_id);
966 return -EINVAL;
967 }
968
969 period = &pwm_config->period;
970
971 if (pwm_config->pwm_period != period_us) {
972 qpnp_lpg_calc_period(period_us, period);
973 qpnp_lpg_save_period(pwm);
974 pwm_config->pwm_period = period_us;
975 }
976
977 if (flags & PM_PWM_LUT_NO_TABLE)
978 goto after_table_write;
979
980 raw_lut = 0;
981 if (flags & PM_PWM_LUT_USE_RAW_VALUE)
982 raw_lut = 1;
983
984 lut_config->list_size = len;
985 lut_config->lo_index = start_idx;
986 lut_config->hi_index = start_idx + len - 1;
987
988 /*
989 * LUT may not be specified in device tree by default.
990 * This is the first time user is configuring it.
991 */
992 if (lpg_config->bypass_lut) {
993 def_lut_config->duty_pct_list = kzalloc(sizeof(u32) *
994 len, GFP_KERNEL);
995 if (!def_lut_config->duty_pct_list) {
996 pr_err("kzalloc failed on def_duty_pct_list\n");
997 return -ENOMEM;
998 }
999
1000 lut_config->duty_pct_list = kzalloc(lpg_config->lut_size *
1001 sizeof(u16), GFP_KERNEL);
1002 if (!lut_config->duty_pct_list) {
1003 pr_err("kzalloc failed on duty_pct_list\n");
1004 kfree(def_lut_config->duty_pct_list);
1005 return -ENOMEM;
1006 }
1007
1008 def_lut_config->size = len;
1009 def_lut_config->start_idx = start_idx;
1010 memcpy(def_lut_config->duty_pct_list, duty_pct, len);
1011
1012 lpg_config->bypass_lut = 0;
1013 }
1014
1015 rc = qpnp_lpg_change_table(pwm, duty_pct, raw_lut);
1016 if (rc) {
1017 pr_err("qpnp_lpg_change_table: rc=%d\n", rc);
1018 return -EINVAL;
1019 }
1020
1021after_table_write:
1022 ramp_step_ms = lut_params.ramp_step_ms;
1023
1024 if (ramp_step_ms > PM_PWM_LUT_RAMP_STEP_TIME_MAX)
1025 ramp_step_ms = PM_PWM_LUT_RAMP_STEP_TIME_MAX;
1026
1027 QPNP_SET_PAUSE_CNT(lut_config->lut_pause_lo_cnt,
1028 lut_params.lut_pause_lo, ramp_step_ms);
1029 if (lut_config->lut_pause_lo_cnt > PM_PWM_LUT_PAUSE_MAX)
1030 lut_config->lut_pause_lo_cnt = PM_PWM_LUT_PAUSE_MAX;
1031
1032 QPNP_SET_PAUSE_CNT(lut_config->lut_pause_hi_cnt,
1033 lut_params.lut_pause_hi, ramp_step_ms);
1034 if (lut_config->lut_pause_hi_cnt > PM_PWM_LUT_PAUSE_MAX)
1035 lut_config->lut_pause_hi_cnt = PM_PWM_LUT_PAUSE_MAX;
1036
1037 lut_config->ramp_step_ms = ramp_step_ms;
1038
1039 lut_config->ramp_direction = !!(flags & PM_PWM_LUT_RAMP_UP);
1040 lut_config->pattern_repeat = !!(flags & PM_PWM_LUT_LOOP);
1041 lut_config->ramp_toggle = !!(flags & PM_PWM_LUT_REVERSE);
1042 lut_config->enable_pause_hi = !!(flags & PM_PWM_LUT_PAUSE_HI_EN);
1043 lut_config->enable_pause_lo = !!(flags & PM_PWM_LUT_PAUSE_LO_EN);
1044 lpg_config->bypass_lut = 0;
1045
1046 rc = qpnp_lpg_change_lut(pwm);
1047
1048 if (!rc)
1049 lpg_config->lpg_configured = 1;
1050
1051 return rc;
1052}
1053
1054/* APIs */
1055/**
1056 * pwm_request - request a PWM device
1057 * @channel_id: PWM id or channel
1058 * @lable: the label to identify the user
1059 */
1060struct pwm_device *pwm_request(int pwm_id, const char *lable)
1061{
1062 struct qpnp_lpg_chip *chip;
1063 struct pwm_device *pwm;
1064
1065 chip = radix_tree_lookup(&lpg_dev_tree, pwm_id);
1066
1067 if (!chip) {
1068 pr_err("Could not find PWM Device for the\n");
1069 pr_err("input pwm channel %d\n", pwm_id);
1070 return ERR_PTR(-EINVAL);
1071 }
1072
1073 mutex_lock(&chip->lpg_mutex);
1074
1075 pwm = &chip->pwm_dev;
1076
1077 if (pwm->pwm_config.in_use) {
1078 pr_err("PWM device associated with the");
1079 pr_err("input pwm id: %d is in use by %s",
1080 pwm_id, pwm->pwm_config.lable);
1081 pwm = ERR_PTR(-EBUSY);
1082 } else {
1083 pwm->pwm_config.in_use = 1;
1084 pwm->pwm_config.lable = lable;
1085 }
1086
1087 mutex_unlock(&chip->lpg_mutex);
1088
1089 return pwm;
1090}
1091EXPORT_SYMBOL_GPL(pwm_request);
1092
1093/**
1094 * pwm_free - free a PWM device
1095 * @pwm: the PWM device
1096 */
1097void pwm_free(struct pwm_device *pwm)
1098{
1099 struct qpnp_pwm_config *pwm_config;
1100
1101 if (pwm == NULL || IS_ERR(pwm) || pwm->chip == NULL) {
1102 pr_err("Invalid pwm handle or no pwm_chip\n");
1103 return;
1104 }
1105
1106 mutex_lock(&pwm->chip->lpg_mutex);
1107
1108 pwm_config = &pwm->pwm_config;
1109
1110 if (pwm_config->in_use) {
1111 qpnp_lpg_disable_pwm(pwm);
1112 qpnp_lpg_disable_lut(pwm);
1113 pwm_config->in_use = 0;
1114 pwm_config->lable = NULL;
1115 pwm->chip->lpg_config.lpg_configured = 0;
1116 }
1117
1118 mutex_unlock(&pwm->chip->lpg_mutex);
1119}
1120EXPORT_SYMBOL_GPL(pwm_free);
1121
1122/**
1123 * pwm_config - change a PWM device configuration
1124 * @pwm: the PWM device
1125 * @period_us: period in microseconds
1126 * @duty_us: duty cycle in microseconds
1127 */
1128int pwm_config(struct pwm_device *pwm, int duty_us, int period_us)
1129{
1130 int rc;
1131
1132 if (pwm == NULL || IS_ERR(pwm) ||
1133 duty_us > period_us ||
1134 (unsigned)period_us > PM_PWM_PERIOD_MAX ||
1135 (unsigned)period_us < PM_PWM_PERIOD_MIN) {
1136 pr_err("Invalid pwm handle or parameters\n");
1137 return -EINVAL;
1138 }
1139
1140 if (!pwm->pwm_config.in_use)
1141 return -EINVAL;
1142
1143 mutex_lock(&pwm->chip->lpg_mutex);
1144 rc = _pwm_config(pwm, duty_us, period_us);
1145 mutex_unlock(&pwm->chip->lpg_mutex);
1146
1147 return rc;
1148}
1149EXPORT_SYMBOL_GPL(pwm_config);
1150
1151/**
1152 * pwm_enable - start a PWM output toggling
1153 * @pwm: the PWM device
1154 */
1155int pwm_enable(struct pwm_device *pwm)
1156{
1157 struct qpnp_pwm_config *p_config;
1158 struct qpnp_lpg_chip *chip;
1159 int rc = 0;
1160
1161 if (pwm == NULL || IS_ERR(pwm) || pwm->chip == NULL) {
1162 pr_err("Invalid pwm handle or no pwm_chip\n");
1163 return -EINVAL;
1164 }
1165
1166 mutex_lock(&pwm->chip->lpg_mutex);
1167
1168 chip = pwm->chip;
1169 p_config = &pwm->pwm_config;
1170
1171 if (!p_config->in_use) {
1172 pr_err("channel_id: %d: stale handle?\n", p_config->channel_id);
1173 rc = -EINVAL;
1174 goto out_unlock;
1175 }
1176
1177 if (!pwm->chip->lpg_config.lpg_configured) {
1178 pr_err("Request received to enable PWM for channel Id: %d\n",
1179 p_config->channel_id);
1180 pr_err("However, PWM isn't configured\n");
1181 pr_err("falling back to defaultconfiguration\n");
1182 rc = _pwm_config(pwm, p_config->pwm_duty,
1183 p_config->pwm_period);
1184 if (rc) {
1185 pr_err("Could not apply default PWM config\n");
1186 goto out_unlock;
1187 }
1188 }
1189
1190 rc = qpnp_lpg_enable_pwm(pwm);
1191
1192out_unlock:
1193 mutex_unlock(&pwm->chip->lpg_mutex);
1194 return rc;
1195}
1196EXPORT_SYMBOL_GPL(pwm_enable);
1197
1198/**
1199 * pwm_disable - stop a PWM output toggling
1200 * @pwm: the PWM device
1201 */
1202void pwm_disable(struct pwm_device *pwm)
1203{
1204 struct qpnp_pwm_config *pwm_config;
1205 struct qpnp_lpg_chip *chip;
1206
1207 if (pwm == NULL || IS_ERR(pwm) || pwm->chip == NULL) {
1208 pr_err("Invalid pwm handle or no pwm_chip\n");
1209 return;
1210 }
1211
1212 mutex_lock(&pwm->chip->lpg_mutex);
1213
1214 chip = pwm->chip;
1215 pwm_config = &pwm->pwm_config;
1216
1217 if (pwm_config->in_use) {
1218 if (!pwm->chip->lpg_config.lpg_configured) {
1219 pr_err("Request received to disable PWM for\n");
1220 pr_err("channel Id: %d\n", pwm_config->channel_id);
1221 pr_err("However PWM is not configured by any means\n");
1222 goto out_unlock;
1223 }
1224 qpnp_lpg_disable_pwm(pwm);
1225 }
1226
1227out_unlock:
1228 mutex_unlock(&pwm->chip->lpg_mutex);
1229}
1230EXPORT_SYMBOL_GPL(pwm_disable);
1231
1232/**
1233 * pwm_config_period - change PWM period
1234 *
1235 * @pwm: the PWM device
1236 * @pwm_p: period in struct qpnp_lpg_period
1237 */
1238int pwm_config_period(struct pwm_device *pwm,
1239 struct pwm_period_config *period)
1240{
1241 struct qpnp_pwm_config *pwm_config;
1242 struct qpnp_lpg_config *lpg_config;
1243 struct qpnp_lpg_chip *chip;
1244 int rc = 0;
1245
1246 if (pwm == NULL || IS_ERR(pwm) || period == NULL)
1247 return -EINVAL;
1248 if (pwm->chip == NULL)
1249 return -ENODEV;
1250
1251 mutex_lock(&pwm->chip->lpg_mutex);
1252
1253 chip = pwm->chip;
1254 pwm_config = &pwm->pwm_config;
1255 lpg_config = &chip->lpg_config;
1256
1257 if (!pwm_config->in_use) {
1258 rc = -EINVAL;
1259 goto out_unlock;
1260 }
1261
1262 pwm_config->period.pwm_size = period->pwm_size;
1263 pwm_config->period.clk = period->clk;
1264 pwm_config->period.pre_div = period->pre_div;
1265 pwm_config->period.pre_div_exp = period->pre_div_exp;
1266
1267 qpnp_lpg_save_period(pwm);
1268
1269 rc = spmi_ext_register_writel(chip->spmi_dev->ctrl, chip->spmi_dev->sid,
1270 SPMI_LPG_REG_ADDR(lpg_config->base_addr,
1271 QPNP_LPG_PWM_SIZE_CLK),
1272 &chip->qpnp_lpg_registers[QPNP_LPG_PWM_SIZE_CLK], 1);
1273
1274 if (rc) {
1275 pr_err("Write failed: QPNP_LPG_PWM_SIZE_CLK register, rc: %d\n",
1276 rc);
1277 goto out_unlock;
1278 }
1279
1280 rc = spmi_ext_register_writel(chip->spmi_dev->ctrl, chip->spmi_dev->sid,
1281 SPMI_LPG_REG_ADDR(lpg_config->base_addr,
1282 QPNP_LPG_PWM_FREQ_PREDIV_CLK),
1283 &chip->qpnp_lpg_registers[QPNP_LPG_PWM_FREQ_PREDIV_CLK], 1);
1284 if (rc) {
1285 pr_err("Failed to write to QPNP_LPG_PWM_FREQ_PREDIV_CLK\n");
1286 pr_err("register, rc = %d\n", rc);
1287 }
1288
1289out_unlock:
1290 mutex_unlock(&pwm->chip->lpg_mutex);
1291 return rc;
1292}
1293EXPORT_SYMBOL(pwm_config_period);
1294
1295/**
1296 * pwm_config_pwm_value - change a PWM device configuration
1297 * @pwm: the PWM device
1298 * @pwm_value: the duty cycle in raw PWM value (< 2^pwm_size)
1299 */
1300int pwm_config_pwm_value(struct pwm_device *pwm, int pwm_value)
1301{
1302 struct qpnp_lpg_config *lpg_config;
1303 struct qpnp_pwm_config *pwm_config;
1304 int rc = 0;
1305
1306 if (pwm == NULL || IS_ERR(pwm))
1307 return -EINVAL;
1308
1309 if (pwm->chip == NULL)
1310 return -ENODEV;
1311
1312 lpg_config = &pwm->chip->lpg_config;
1313 pwm_config = &pwm->pwm_config;
1314
1315 mutex_lock(&pwm->chip->lpg_mutex);
1316
1317 if (!pwm_config->in_use || !pwm_config->pwm_period) {
1318 rc = -EINVAL;
1319 goto out_unlock;
1320 }
1321
1322 if (pwm_config->pwm_value == pwm_value)
1323 goto out_unlock;
1324
1325 pwm_config->pwm_value = pwm_value;
1326
1327 rc = qpnp_lpg_save_pwm_value(pwm);
1328
1329 if (rc)
1330 pr_err("Could not update PWM value for channel %d rc=%d\n",
1331 pwm_config->channel_id, rc);
1332
1333out_unlock:
1334 mutex_unlock(&pwm->chip->lpg_mutex);
1335 return rc;
1336}
1337EXPORT_SYMBOL_GPL(pwm_config_pwm_value);
1338
1339/**
1340 * pwm_lut_config - change LPG LUT device configuration
1341 * @pwm: the PWM device
1342 * @period_us: period in micro second
1343 * @duty_pct: array of duty cycles in percent, like 20, 50.
1344 * @lut_params: Lookup table parameters
1345 */
1346int pwm_lut_config(struct pwm_device *pwm, int period_us,
1347 int duty_pct[], struct lut_params lut_params)
1348{
1349 int rc = 0;
1350
1351 if (pwm == NULL || IS_ERR(pwm) || !lut_params.idx_len) {
1352 pr_err("Invalid pwm handle or idx_len=0\n");
1353 return -EINVAL;
1354 }
1355
1356 if (pwm->chip == NULL)
1357 return -ENODEV;
1358
1359 if (duty_pct == NULL && !(lut_params.flags & PM_PWM_LUT_NO_TABLE)) {
1360 pr_err("Invalid duty_pct with flag\n");
1361 return -EINVAL;
1362 }
1363
1364 mutex_lock(&pwm->chip->lpg_mutex);
1365
1366 rc = _pwm_lut_config(pwm, period_us, duty_pct, lut_params);
1367
1368 mutex_unlock(&pwm->chip->lpg_mutex);
1369
1370 return rc;
1371}
1372EXPORT_SYMBOL_GPL(pwm_lut_config);
1373
1374/**
1375 * pwm_lut_enable - control a PWM device to start/stop LUT ramp
1376 * @pwm: the PWM device
1377 * @start: to start (1), or stop (0)
1378 */
1379int pwm_lut_enable(struct pwm_device *pwm, int start)
1380{
1381 struct qpnp_lpg_config *lpg_config;
1382 struct qpnp_pwm_config *p_config;
1383 struct lut_params lut_params;
1384 int rc = 0;
1385
1386 if (pwm == NULL || IS_ERR(pwm)) {
1387 pr_err("Invalid pwm handle\n");
1388 return -EINVAL;
1389 }
1390
1391 if (pwm->chip == NULL)
1392 return -ENODEV;
1393
1394 lpg_config = &pwm->chip->lpg_config;
1395 p_config = &pwm->pwm_config;
1396
1397 mutex_lock(&pwm->chip->lpg_mutex);
1398
1399 if (start) {
1400 if (!lpg_config->lpg_configured) {
1401 pr_err("Request received to enable LUT for\n");
1402 pr_err("LPG channel %d\n", pwm->pwm_config.channel_id);
1403 pr_err("But LPG is not configured, falling back to\n");
1404 pr_err(" default LUT configuration if available\n");
1405
1406 if (lpg_config->bypass_lut) {
1407 pr_err("No default LUT configuration found\n");
1408 pr_err("Use pwm_lut_config() to configure\n");
1409 rc = -EINVAL;
1410 goto out;
1411 }
1412
1413 qpnp_set_lut_params(&lut_params,
1414 &lpg_config->lut_config);
1415
1416 rc = _pwm_lut_config(pwm, p_config->pwm_period,
1417 (int *)lpg_config->lut_config.def_config.duty_pct_list,
1418 lut_params);
1419 if (rc) {
1420 pr_err("Could not set the default LUT conf\n");
1421 goto out;
1422 }
1423 }
1424
1425 rc = qpnp_lpg_enable_lut(pwm);
1426 } else {
1427 if (unlikely(!lpg_config->lpg_configured)) {
1428 pr_err("LPG isn't configured\n");
1429 rc = -EINVAL;
1430 goto out;
1431 }
1432 rc = qpnp_lpg_disable_lut(pwm);
1433 }
1434
1435out:
1436 mutex_unlock(&pwm->chip->lpg_mutex);
1437 return rc;
1438}
1439EXPORT_SYMBOL_GPL(pwm_lut_enable);
1440
1441/* Fill in lpg device elements based on values found in device tree. */
1442static int qpnp_lpg_get_dt_config(struct spmi_device *spmi,
1443 struct qpnp_lpg_chip *chip)
1444{
1445 int rc;
1446 struct resource *res;
1447 struct device_node *of_node = spmi->dev.of_node;
1448 struct qpnp_lpg_config *lpg_config = &chip->lpg_config;
1449 struct pwm_device *pwm_dev = &chip->pwm_dev;
1450 struct qpnp_lut_config *lut_config = &chip->lpg_config.lut_config;
1451 struct qpnp_lut_default_config *def_lut_config =
1452 &lut_config->def_config;
1453
1454 res = spmi_get_resource(spmi, 0, IORESOURCE_MEM, 0);
1455 if (!res) {
1456 dev_err(&spmi->dev, "%s: node is missing base address\n",
1457 __func__);
1458 return -EINVAL;
1459 }
1460
1461 lpg_config->base_addr = res->start;
1462
1463 res = spmi_get_resource(spmi, 0, IORESOURCE_MEM, 1);
1464 if (!res) {
1465 dev_err(&spmi->dev, "%s: node is missing LUT base address\n",
1466 __func__);
1467 return -EINVAL;
1468 }
1469
1470 lpg_config->lut_base_addr = res->start;
1471 /* Each entry of LUT is of 2 bytes */
1472 lpg_config->lut_size = resource_size(res) >> 1;
1473
1474
1475 rc = of_property_read_u32(of_node, "qcom,channel-id",
1476 &pwm_dev->pwm_config.channel_id);
1477 if (rc) {
1478 dev_err(&spmi->dev, "%s: node is missing LPG channel id",
1479 __func__);
1480 return rc;
1481 }
1482
1483 rc = of_property_read_u32(of_node, "qcom,period",
1484 &pwm_dev->pwm_config.pwm_period);
1485 if (rc) {
1486 dev_err(&spmi->dev, "%s: node is missing PWM Period value",
1487 __func__);
1488 return rc;
1489 }
1490
1491 if (!of_get_property(of_node, "qcom,duty-percents",
1492 &def_lut_config->size)) {
1493 lpg_config->bypass_lut = 1;
1494 }
1495
1496 if (lpg_config->bypass_lut)
1497 goto read_opt_props;
1498
1499 rc = of_property_read_u32(of_node, "qcom,start-index",
1500 &def_lut_config->start_idx);
1501
1502 if (rc) {
1503 dev_err(&spmi->dev, "Missing start index");
1504 return rc;
1505 }
1506
1507 def_lut_config->size /= sizeof(u32);
1508
1509 def_lut_config->duty_pct_list = kzalloc(sizeof(u32) *
1510 def_lut_config->size, GFP_KERNEL);
1511 if (!def_lut_config->duty_pct_list) {
1512 dev_err(&spmi->dev, "%s: kzalloc failed on duty_pct_list\n",
1513 __func__);
1514 return -ENOMEM;
1515 }
1516
1517 rc = of_property_read_u32_array(of_node, "qcom,duty-percents",
1518 def_lut_config->duty_pct_list, def_lut_config->size);
1519 if (rc) {
1520 dev_err(&spmi->dev, "invalid or missing property:\n");
1521 dev_err(&spmi->dev, "qcom,duty-pcts-list\n");
1522 kfree(def_lut_config->duty_pct_list);
1523 return rc;
1524 }
1525
1526 lut_config->duty_pct_list = kzalloc(lpg_config->lut_size * sizeof(u16),
1527 GFP_KERNEL);
1528 if (!lut_config->duty_pct_list) {
1529 dev_err(&spmi->dev, "can not allocate duty pct list\n");
1530 kfree(def_lut_config->duty_pct_list);
1531 return -ENOMEM;
1532 }
1533
1534read_opt_props:
1535 /* Initialize optional config parameters from DT if provided */
1536 of_property_read_u32(of_node, "qcom,duty",
1537 &pwm_dev->pwm_config.pwm_duty);
1538 of_property_read_u32(of_node, "qcom,ramp-step-duration",
1539 &lut_config->ramp_step_ms);
1540 of_property_read_u32(of_node, "qcom,lpg-lut-pause-hi",
1541 &lut_config->lut_pause_hi_cnt);
1542 of_property_read_u32(of_node, "qcom,lpg-lut-pause-lo",
1543 &lut_config->lut_pause_lo_cnt);
1544 of_property_read_u32(of_node, "qcom,lpg-lut-ramp-direction",
1545 (u32 *)&lut_config->ramp_direction);
1546 of_property_read_u32(of_node, "qcom,lpg-lut-pattern-repeat",
1547 (u32 *)&lut_config->pattern_repeat);
1548 of_property_read_u32(of_node, "qcom,lpg-lut-ramp-toggle",
1549 (u32 *)&lut_config->ramp_toggle);
1550 of_property_read_u32(of_node, "qcom,lpg-lut-enable-pause-hi",
1551 (u32 *)&lut_config->enable_pause_hi);
1552 of_property_read_u32(of_node, "qcom,lpg-lut-enable-pause-lo",
1553 (u32 *)&lut_config->enable_pause_lo);
1554
1555 return 0;
1556}
1557
1558static int __devinit qpnp_pwm_probe(struct spmi_device *spmi)
1559{
1560 struct qpnp_lpg_chip *chip;
1561 int rc, id;
1562
1563 chip = kzalloc(sizeof *chip, GFP_KERNEL);
1564 if (chip == NULL) {
1565 pr_err("kzalloc() failed.\n");
1566 return -ENOMEM;
1567 }
1568
1569 mutex_init(&chip->lpg_mutex);
1570
1571 chip->spmi_dev = spmi;
1572 chip->pwm_dev.chip = chip;
1573 dev_set_drvdata(&spmi->dev, chip);
1574
1575 rc = qpnp_lpg_get_dt_config(spmi, chip);
1576
1577 if (rc)
1578 goto failed_config;
1579
1580 id = chip->pwm_dev.pwm_config.channel_id;
1581
1582 rc = radix_tree_insert(&lpg_dev_tree, id, chip);
1583
1584 if (rc) {
1585 dev_err(&spmi->dev, "%s: Failed to register LPG Channel %d\n",
1586 __func__, id);
1587 goto failed_insert;
1588 }
1589
1590 return 0;
1591
1592failed_insert:
1593 kfree(chip->lpg_config.lut_config.duty_pct_list);
1594failed_config:
1595 dev_set_drvdata(&spmi->dev, NULL);
1596 mutex_destroy(&chip->lpg_mutex);
1597 kfree(chip);
1598 return rc;
1599}
1600
1601static int __devexit qpnp_pwm_remove(struct spmi_device *spmi)
1602{
1603 struct qpnp_lpg_chip *chip;
1604 struct qpnp_lpg_config *lpg_config;
1605
1606 chip = dev_get_drvdata(&spmi->dev);
1607
1608 dev_set_drvdata(&spmi->dev, NULL);
1609
1610 if (chip) {
1611 lpg_config = &chip->lpg_config;
1612 kfree(lpg_config->lut_config.duty_pct_list);
1613 kfree(lpg_config->lut_config.def_config.duty_pct_list);
1614 mutex_destroy(&chip->lpg_mutex);
1615 kfree(chip);
1616 }
1617
1618 return 0;
1619}
1620
1621static struct of_device_id spmi_match_table[] = {
1622 { .compatible = QPNP_LPG_DRIVER_NAME, },
1623 {}
1624};
1625
1626static const struct spmi_device_id qpnp_lpg_id[] = {
1627 { QPNP_LPG_DRIVER_NAME, 0 },
1628 { }
1629};
1630MODULE_DEVICE_TABLE(spmi, qpnp_lpg_id);
1631
1632static struct spmi_driver qpnp_lpg_driver = {
1633 .driver = {
1634 .name = QPNP_LPG_DRIVER_NAME,
1635 .of_match_table = spmi_match_table,
1636 .owner = THIS_MODULE,
1637 },
1638 .probe = qpnp_pwm_probe,
1639 .remove = __devexit_p(qpnp_pwm_remove),
1640 .id_table = qpnp_lpg_id,
1641};
1642
1643/**
1644 * qpnp_lpg_init() - register spmi driver for qpnp-lpg
1645 */
1646int __init qpnp_lpg_init(void)
1647{
1648 return spmi_driver_register(&qpnp_lpg_driver);
1649}
1650
1651static void __exit qpnp_lpg_exit(void)
1652{
1653 spmi_driver_unregister(&qpnp_lpg_driver);
1654}
1655
1656MODULE_DESCRIPTION("QPNP PMIC LPG driver");
1657MODULE_LICENSE("GPL v2");
1658MODULE_ALIAS("platform:" QPNP_LPG_DRIVER_NAME);
1659
1660subsys_initcall(qpnp_lpg_init);
1661module_exit(qpnp_lpg_exit);