blob: e7267dec1a0ccd9c82d455c3fc20e62977baa85b [file] [log] [blame]
David Collins8885f792017-01-26 14:36:34 -08001/* Copyright (c) 2012-2017, 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 * Qualcomm Technologies, Inc. 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/platform_device.h>
25#include <linux/regmap.h>
26#include <linux/of.h>
27#include <linux/of_device.h>
28#include <linux/of_address.h>
29#include <linux/radix-tree.h>
30#include <linux/qpnp/pwm.h>
31
32#define QPNP_LPG_DRIVER_NAME "qcom,qpnp-pwm"
33#define QPNP_LPG_CHANNEL_BASE "qpnp-lpg-channel-base"
34#define QPNP_LPG_LUT_BASE "qpnp-lpg-lut-base"
35
36#define QPNP_PWM_MODE_ONLY_SUB_TYPE 0x0B
37#define QPNP_LPG_CHAN_SUB_TYPE 0x2
38#define QPNP_LPG_S_CHAN_SUB_TYPE 0x11
39
40/* LPG Control for LPG_PATTERN_CONFIG */
41#define QPNP_RAMP_DIRECTION_SHIFT 4
42#define QPNP_RAMP_DIRECTION_MASK 0x10
43#define QPNP_PATTERN_REPEAT_SHIFT 3
44#define QPNP_PATTERN_REPEAT_MASK 0x08
45#define QPNP_RAMP_TOGGLE_SHIFT 2
46#define QPNP_RAMP_TOGGLE_MASK 0x04
47#define QPNP_EN_PAUSE_HI_SHIFT 1
48#define QPNP_EN_PAUSE_HI_MASK 0x02
49#define QPNP_EN_PAUSE_LO_MASK 0x01
50
51/* LPG Control for LPG_PWM_SIZE_CLK */
52#define QPNP_PWM_SIZE_SHIFT_SUB_TYPE 2
53#define QPNP_PWM_SIZE_MASK_SUB_TYPE 0x4
54#define QPNP_PWM_FREQ_CLK_SELECT_MASK_SUB_TYPE 0x03
55#define QPNP_PWM_SIZE_9_BIT_SUB_TYPE 0x01
56
57#define QPNP_SET_PWM_CLK_SUB_TYPE(val, clk, pwm_size) \
58do { \
59 val = (clk + 1) & QPNP_PWM_FREQ_CLK_SELECT_MASK_SUB_TYPE; \
60 val |= (((pwm_size > 6 ? QPNP_PWM_SIZE_9_BIT_SUB_TYPE : 0) << \
61 QPNP_PWM_SIZE_SHIFT_SUB_TYPE) & QPNP_PWM_SIZE_MASK_SUB_TYPE); \
62} while (0)
63
64#define QPNP_GET_PWM_SIZE_SUB_TYPE(reg) ((reg & QPNP_PWM_SIZE_MASK_SUB_TYPE) \
65 >> QPNP_PWM_SIZE_SHIFT_SUB_TYPE)
66
67#define QPNP_PWM_SIZE_SHIFT 4
68#define QPNP_PWM_SIZE_MASK 0x30
69#define QPNP_PWM_FREQ_CLK_SELECT_MASK 0x03
70#define QPNP_MIN_PWM_BIT_SIZE 6
71#define QPNP_MAX_PWM_BIT_SIZE 9
72#define QPNP_PWM_SIZES_SUPPORTED 10
73
74#define QPNP_SET_PWM_CLK(val, clk, pwm_size) \
75do { \
76 val = (clk + 1) & QPNP_PWM_FREQ_CLK_SELECT_MASK; \
77 val |= (((pwm_size - QPNP_MIN_PWM_BIT_SIZE) << \
78 QPNP_PWM_SIZE_SHIFT) & QPNP_PWM_SIZE_MASK); \
79} while (0)
80
81#define QPNP_GET_PWM_SIZE(reg) ((reg & QPNP_PWM_SIZE_MASK) \
82 >> QPNP_PWM_SIZE_SHIFT)
83
84/* LPG Control for LPG_PWM_FREQ_PREDIV_CLK */
85#define QPNP_PWM_FREQ_PRE_DIVIDE_SHIFT 5
86#define QPNP_PWM_FREQ_PRE_DIVIDE_MASK 0x60
87#define QPNP_PWM_FREQ_EXP_MASK 0x07
88
89#define QPNP_SET_PWM_FREQ_PREDIV(val, pre_div, pre_div_exp) \
90do { \
91 val = (pre_div << QPNP_PWM_FREQ_PRE_DIVIDE_SHIFT) & \
92 QPNP_PWM_FREQ_PRE_DIVIDE_MASK; \
93 val |= (pre_div_exp & QPNP_PWM_FREQ_EXP_MASK); \
94} while (0)
95
96/* LPG Control for LPG_PWM_TYPE_CONFIG */
97#define QPNP_EN_GLITCH_REMOVAL_SHIFT 5
98#define QPNP_EN_GLITCH_REMOVAL_MASK 0x20
99#define QPNP_EN_FULL_SCALE_SHIFT 3
100#define QPNP_EN_FULL_SCALE_MASK 0x08
101#define QPNP_EN_PHASE_STAGGER_SHIFT 2
102#define QPNP_EN_PHASE_STAGGER_MASK 0x04
103#define QPNP_PHASE_STAGGER_MASK 0x03
104
105/* LPG Control for PWM_VALUE_LSB */
106#define QPNP_PWM_VALUE_LSB_MASK 0xFF
107
108/* LPG Control for PWM_VALUE_MSB */
109#define QPNP_PWM_VALUE_MSB_SHIFT 8
110#define QPNP_PWM_VALUE_MSB_MASK 0x01
111
112/* LPG Control for ENABLE_CONTROL */
113#define QPNP_EN_PWM_HIGH_SHIFT 7
114#define QPNP_EN_PWM_HIGH_MASK 0x80
115#define QPNP_EN_PWM_LO_SHIFT 6
116#define QPNP_EN_PWM_LO_MASK 0x40
117#define QPNP_EN_PWM_OUTPUT_SHIFT 5
118#define QPNP_EN_PWM_OUTPUT_MASK 0x20
119#define QPNP_PWM_SRC_SELECT_SHIFT 2
120#define QPNP_PWM_SRC_SELECT_MASK 0x04
121#define QPNP_PWM_EN_RAMP_GEN_SHIFT 1
122#define QPNP_PWM_EN_RAMP_GEN_MASK 0x02
123
124/* LPG Control for PWM_SYNC */
125#define QPNP_PWM_SYNC_VALUE 0x01
126#define QPNP_PWM_SYNC_MASK 0x01
127
128/* LPG Control for RAMP_CONTROL */
129#define QPNP_RAMP_START_MASK 0x01
130
131#define QPNP_ENABLE_LUT_V0(value) (value |= QPNP_RAMP_START_MASK)
132#define QPNP_DISABLE_LUT_V0(value) (value &= ~QPNP_RAMP_START_MASK)
133#define QPNP_ENABLE_LUT_V1(value, id) (value |= BIT(id))
134
135/* LPG Control for RAMP_STEP_DURATION_LSB */
136#define QPNP_RAMP_STEP_DURATION_LSB_MASK 0xFF
137
138/* LPG Control for RAMP_STEP_DURATION_MSB */
139#define QPNP_RAMP_STEP_DURATION_MSB_SHIFT 8
140#define QPNP_RAMP_STEP_DURATION_MSB_MASK 0x01
141
142#define QPNP_PWM_1KHZ 1024
143#define QPNP_GET_RAMP_STEP_DURATION(ramp_time_ms) \
144 ((ramp_time_ms * QPNP_PWM_1KHZ) / 1000)
145
146/* LPG Control for PAUSE_HI_MULTIPLIER_LSB */
147#define QPNP_PAUSE_HI_MULTIPLIER_LSB_MASK 0xFF
148
149/* LPG Control for PAUSE_HI_MULTIPLIER_MSB */
150#define QPNP_PAUSE_HI_MULTIPLIER_MSB_SHIFT 8
151#define QPNP_PAUSE_HI_MULTIPLIER_MSB_MASK 0x1F
152
153/* LPG Control for PAUSE_LO_MULTIPLIER_LSB */
154#define QPNP_PAUSE_LO_MULTIPLIER_LSB_MASK 0xFF
155
156/* LPG Control for PAUSE_LO_MULTIPLIER_MSB */
157#define QPNP_PAUSE_LO_MULTIPLIER_MSB_SHIFT 8
158#define QPNP_PAUSE_LO_MULTIPLIER_MSB_MASK 0x1F
159
160/* LPG Control for HI_INDEX */
161#define QPNP_HI_INDEX_MASK 0x3F
162
163/* LPG Control for LO_INDEX */
164#define QPNP_LO_INDEX_MASK 0x3F
165
166/* LPG DTEST */
167#define QPNP_LPG_DTEST_LINE_MAX 4
168#define QPNP_LPG_DTEST_OUTPUT_MAX 5
169#define QPNP_LPG_DTEST_OUTPUT_MASK 0x07
170
171/* PWM DTEST */
172#define QPNP_PWM_DTEST_LINE_MAX 2
173#define QPNP_PWM_DTEST_OUTPUT_MAX 2
174#define QPNP_PWM_DTEST_OUTPUT_MASK 0x03
175
176#define NUM_CLOCKS 3
177#define QPNP_PWM_M_MAX 7
178#define NSEC_1024HZ (NSEC_PER_SEC / 1024)
179#define NSEC_32768HZ (NSEC_PER_SEC / 32768)
180#define NSEC_19P2MHZ (NSEC_PER_SEC / 19200000)
181
182#define NUM_LPG_PRE_DIVIDE 4
183
184#define PRE_DIVIDE_1 1
185#define PRE_DIVIDE_3 3
186#define PRE_DIVIDE_5 5
187#define PRE_DIVIDE_6 6
188
189#define SPMI_LPG_REG_BASE_OFFSET 0x40
190#define SPMI_LPG_REVISION2_OFFSET 0x1
191#define SPMI_LPG_REV1_RAMP_CONTROL_OFFSET 0x86
192#define SPMI_LPG_SUB_TYPE_OFFSET 0x5
193#define SPMI_LPG_PWM_SYNC 0x7
194#define SPMI_LPG_REG_ADDR(b, n) (b + SPMI_LPG_REG_BASE_OFFSET + (n))
195#define SPMI_MAX_BUF_LEN 8
196
197#define QPNP_PWM_LUT_NOT_SUPPORTED 0x1
198
199/* Supported PWM sizes */
200#define QPNP_PWM_SIZE_6_BIT 6
201#define QPNP_PWM_SIZE_7_BIT 7
202#define QPNP_PWM_SIZE_8_BIT 8
203#define QPNP_PWM_SIZE_9_BIT 9
204
205#define QPNP_PWM_SIZE_6_9_BIT 0x9
206#define QPNP_PWM_SIZE_7_8_BIT 0x6
207#define QPNP_PWM_SIZE_6_7_9_BIT 0xB
208
209/*
210 * Registers that don't need to be cached are defined below from an offset
211 * of SPMI_LPG_REG_BASE_OFFSET.
212 */
213#define QPNP_LPG_SEC_ACCESS 0x90
214#define QPNP_LPG_DTEST 0xA2
215
216/* Supported time levels */
217enum time_level {
218 LVL_NSEC,
219 LVL_USEC,
220};
221
222/* LPG revisions */
223enum qpnp_lpg_revision {
224 QPNP_LPG_REVISION_0 = 0x0,
225 QPNP_LPG_REVISION_1 = 0x1,
226};
227
228/* LPG LUT MODE STATE */
229enum qpnp_lut_state {
230 QPNP_LUT_ENABLE = 0x0,
231 QPNP_LUT_DISABLE = 0x1,
232};
233
234/* PWM MODE STATE */
235enum qpnp_pwm_state {
236 QPNP_PWM_ENABLE = 0x0,
237 QPNP_PWM_DISABLE = 0x1,
238};
239
240/* SPMI LPG registers */
241enum qpnp_lpg_registers_list {
242 QPNP_LPG_PATTERN_CONFIG,
243 QPNP_LPG_PWM_SIZE_CLK,
244 QPNP_LPG_PWM_FREQ_PREDIV_CLK,
245 QPNP_LPG_PWM_TYPE_CONFIG,
246 QPNP_PWM_VALUE_LSB,
247 QPNP_PWM_VALUE_MSB,
248 QPNP_ENABLE_CONTROL,
249 QPNP_RAMP_CONTROL,
250 QPNP_RAMP_STEP_DURATION_LSB = QPNP_RAMP_CONTROL + 9,
251 QPNP_RAMP_STEP_DURATION_MSB,
252 QPNP_PAUSE_HI_MULTIPLIER_LSB,
253 QPNP_PAUSE_HI_MULTIPLIER_MSB,
254 QPNP_PAUSE_LO_MULTIPLIER_LSB,
255 QPNP_PAUSE_LO_MULTIPLIER_MSB,
256 QPNP_HI_INDEX,
257 QPNP_LO_INDEX,
258 QPNP_TOTAL_LPG_SPMI_REGISTERS
259};
260
261/*
262 * Formula from HSID,
263 * pause_time (hi/lo) = (pause_cnt- 1)*(ramp_ms)
264 * OR,
265 * pause_cnt = (pause_time / ramp_ms) + 1
266 */
267#define QPNP_SET_PAUSE_CNT(to_pause_cnt, from_pause, ramp_ms) \
268 (to_pause_cnt = (from_pause / (ramp_ms ? ramp_ms : 1)) + 1)
269
270
271static unsigned int pt_t[NUM_LPG_PRE_DIVIDE][NUM_CLOCKS] = {
272 { PRE_DIVIDE_1 * NSEC_1024HZ,
273 PRE_DIVIDE_1 * NSEC_32768HZ,
274 PRE_DIVIDE_1 * NSEC_19P2MHZ,
275 },
276 { PRE_DIVIDE_3 * NSEC_1024HZ,
277 PRE_DIVIDE_3 * NSEC_32768HZ,
278 PRE_DIVIDE_3 * NSEC_19P2MHZ,
279 },
280 { PRE_DIVIDE_5 * NSEC_1024HZ,
281 PRE_DIVIDE_5 * NSEC_32768HZ,
282 PRE_DIVIDE_5 * NSEC_19P2MHZ,
283 },
284 { PRE_DIVIDE_6 * NSEC_1024HZ,
285 PRE_DIVIDE_6 * NSEC_32768HZ,
286 PRE_DIVIDE_6 * NSEC_19P2MHZ,
287 },
288};
289
290struct qpnp_lut_config {
291 u8 *duty_pct_list;
292 int list_len;
293 int ramp_index;
294 int lo_index;
295 int hi_index;
296 int lut_pause_hi_cnt;
297 int lut_pause_lo_cnt;
298 int ramp_step_ms;
299 bool ramp_direction;
300 bool pattern_repeat;
301 bool ramp_toggle;
302 bool enable_pause_hi;
303 bool enable_pause_lo;
304};
305
306struct qpnp_lpg_config {
307 struct qpnp_lut_config lut_config;
308 u16 base_addr;
309 u16 lut_base_addr;
310 u16 lut_size;
311};
312
313struct _qpnp_pwm_config {
314 int pwm_value;
315 int pwm_period; /* in microseconds */
316 int pwm_duty; /* in microseconds */
317 struct pwm_period_config period;
318 int supported_sizes;
319 int force_pwm_size;
Fenglin Wu19d2c032017-07-03 12:44:59 +0800320 bool update_period;
David Collins8885f792017-01-26 14:36:34 -0800321};
322
323/* Public facing structure */
324struct qpnp_pwm_chip {
325 struct platform_device *pdev;
326 struct regmap *regmap;
327 struct pwm_chip chip;
328 bool enabled;
329 struct _qpnp_pwm_config pwm_config;
330 struct qpnp_lpg_config lpg_config;
Fenglin Wuc81fb382017-08-14 12:45:21 +0800331 enum pm_pwm_mode pwm_mode;
David Collins8885f792017-01-26 14:36:34 -0800332 spinlock_t lpg_lock;
333 enum qpnp_lpg_revision revision;
334 u8 sub_type;
335 u32 flags;
336 u8 qpnp_lpg_registers[QPNP_TOTAL_LPG_SPMI_REGISTERS];
337 int channel_id;
338 const char *channel_owner;
339 u32 dtest_line;
340 u32 dtest_output;
341 bool in_test_mode;
342};
343
344/* Internal functions */
345static inline struct qpnp_pwm_chip *qpnp_pwm_from_pwm_dev(
346 struct pwm_device *pwm)
347{
348 return container_of(pwm->chip, struct qpnp_pwm_chip, chip);
349}
350
351static inline struct qpnp_pwm_chip *qpnp_pwm_from_pwm_chip(
352 struct pwm_chip *chip)
353{
354 return container_of(chip, struct qpnp_pwm_chip, chip);
355}
356
357static inline void qpnp_set_pattern_config(u8 *val,
358 struct qpnp_lut_config *lut_config)
359{
360 *val = lut_config->enable_pause_lo & QPNP_EN_PAUSE_LO_MASK;
361 *val |= (lut_config->enable_pause_hi << QPNP_EN_PAUSE_HI_SHIFT) &
362 QPNP_EN_PAUSE_HI_MASK;
363 *val |= (lut_config->ramp_toggle << QPNP_RAMP_TOGGLE_SHIFT) &
364 QPNP_RAMP_TOGGLE_MASK;
365 *val |= (lut_config->pattern_repeat << QPNP_PATTERN_REPEAT_SHIFT) &
366 QPNP_PATTERN_REPEAT_MASK;
367 *val |= (lut_config->ramp_direction << QPNP_RAMP_DIRECTION_SHIFT) &
368 QPNP_RAMP_DIRECTION_MASK;
369}
370
371static inline void qpnp_set_pwm_type_config(u8 *val, bool glitch,
372 bool full_scale, bool en_phase, bool phase)
373{
374 *val = phase;
375 *val |= (en_phase << QPNP_EN_PHASE_STAGGER_SHIFT) &
376 QPNP_EN_PHASE_STAGGER_MASK;
377 *val |= (full_scale << QPNP_EN_FULL_SCALE_SHIFT) &
378 QPNP_EN_FULL_SCALE_MASK;
379 *val |= (glitch << QPNP_EN_GLITCH_REMOVAL_SHIFT) &
380 QPNP_EN_GLITCH_REMOVAL_MASK;
381}
382
383static int qpnp_set_control(struct qpnp_pwm_chip *chip, bool pwm_hi,
384 bool pwm_lo, bool pwm_out, bool pwm_src, bool ramp_gen)
385{
386 int value;
387
388 value = (ramp_gen << QPNP_PWM_EN_RAMP_GEN_SHIFT) |
389 (pwm_src << QPNP_PWM_SRC_SELECT_SHIFT) |
390 (pwm_lo << QPNP_EN_PWM_LO_SHIFT) |
391 (pwm_hi << QPNP_EN_PWM_HIGH_SHIFT);
392 if (chip->sub_type != QPNP_LPG_S_CHAN_SUB_TYPE)
393 value |= (pwm_out << QPNP_EN_PWM_OUTPUT_SHIFT);
394 return value;
395}
396
397#define QPNP_ENABLE_LUT_CONTROL(chip) \
398 qpnp_set_control((chip), 0, 0, 0, 0, 1)
399#define QPNP_ENABLE_PWM_CONTROL(chip) \
400 qpnp_set_control((chip), 0, 0, 0, 1, 0)
401#define QPNP_ENABLE_PWM_MODE(chip) \
402 qpnp_set_control((chip), 1, 1, 1, 1, 0)
403#define QPNP_ENABLE_PWM_MODE_GPLED_CHANNEL(chip) \
404 qpnp_set_control((chip), 1, 1, 1, 1, 1)
405#define QPNP_ENABLE_LPG_MODE(chip) \
406 qpnp_set_control((chip), 1, 1, 1, 0, 1)
407#define QPNP_DISABLE_PWM_MODE(chip) \
408 qpnp_set_control((chip), 0, 0, 0, 1, 0)
409#define QPNP_DISABLE_LPG_MODE(chip) \
410 qpnp_set_control((chip), 0, 0, 0, 0, 1)
411#define QPNP_IS_PWM_CONFIG_SELECTED(val) (val & QPNP_PWM_SRC_SELECT_MASK)
412
413#define QPNP_ENABLE_PWM_MODE_ONLY_SUB_TYPE 0x80
414#define QPNP_DISABLE_PWM_MODE_ONLY_SUB_TYPE 0x0
415#define QPNP_PWM_MODE_ONLY_ENABLE_DISABLE_MASK_SUB_TYPE 0x80
416
417static inline void qpnp_convert_to_lut_flags(int *flags,
418 struct qpnp_lut_config *l_config)
419{
420 *flags = ((l_config->ramp_direction ? PM_PWM_LUT_RAMP_UP : 0) |
421 (l_config->pattern_repeat ? PM_PWM_LUT_LOOP : 0)|
422 (l_config->ramp_toggle ? PM_PWM_LUT_REVERSE : 0) |
423 (l_config->enable_pause_hi ? PM_PWM_LUT_PAUSE_HI_EN : 0) |
424 (l_config->enable_pause_lo ? PM_PWM_LUT_PAUSE_LO_EN : 0));
425}
426
427static inline void qpnp_set_lut_params(struct lut_params *l_params,
428 struct qpnp_lut_config *l_config, int s_idx, int size)
429{
430 l_params->start_idx = s_idx;
431 l_params->idx_len = size;
432 l_params->lut_pause_hi = l_config->lut_pause_hi_cnt;
433 l_params->lut_pause_lo = l_config->lut_pause_lo_cnt;
434 l_params->ramp_step_ms = l_config->ramp_step_ms;
435 qpnp_convert_to_lut_flags(&l_params->flags, l_config);
436}
437
438static void qpnp_lpg_save(u8 *u8p, u8 mask, u8 val)
439{
440 *u8p &= ~mask;
441 *u8p |= val & mask;
442}
443
444static int qpnp_lpg_save_and_write(u8 value, u8 mask, u8 *reg, u16 addr,
445 u16 size, struct qpnp_pwm_chip *chip)
446{
447 qpnp_lpg_save(reg, mask, value);
448
449 return regmap_bulk_write(chip->regmap, addr, reg, size);
450}
451
452/*
453 * PWM Frequency = Clock Frequency / (N * T)
454 * or
455 * PWM Period = Clock Period * (N * T)
456 * where
457 * N = 2^9 or 2^6 for 9-bit or 6-bit PWM size
458 * T = Pre-divide * 2^m, where m = 0..7 (exponent)
459 *
460 * This is the formula to figure out m for the best pre-divide and clock:
461 * (PWM Period / N) = (Pre-divide * Clock Period) * 2^m
462 */
463static void qpnp_lpg_calc_period(enum time_level tm_lvl,
464 unsigned int period_value,
465 struct qpnp_pwm_chip *chip)
466{
467 int n, m, clk, div;
468 int best_m, best_div, best_clk;
469 unsigned int last_err, cur_err, min_err;
470 unsigned int tmp_p, period_n;
471 int supported_sizes = chip->pwm_config.supported_sizes;
472 int force_pwm_size = chip->pwm_config.force_pwm_size;
473 struct pwm_period_config *period = &chip->pwm_config.period;
474
475 /* PWM Period / N */
476 if (supported_sizes == QPNP_PWM_SIZE_7_8_BIT)
477 n = 7;
478 else
479 n = 6;
480
481 if (tm_lvl == LVL_USEC) {
482 if (period_value < ((unsigned int)(-1) / NSEC_PER_USEC)) {
483 period_n = (period_value * NSEC_PER_USEC) >> n;
484 } else {
485 if (supported_sizes == QPNP_PWM_SIZE_7_8_BIT)
486 n = 8;
487 else
488 n = 9;
489 period_n = (period_value >> n) * NSEC_PER_USEC;
490 }
491 } else {
492 period_n = period_value >> n;
493 }
494
495 if (force_pwm_size != 0) {
496 if (n < force_pwm_size)
497 period_n = period_n >> (force_pwm_size - n);
498 else
499 period_n = period_n << (n - force_pwm_size);
500 n = force_pwm_size;
501 pr_info("LPG channel '%d' pwm size is forced to=%d\n",
502 chip->channel_id, n);
503 }
504
505 min_err = last_err = (unsigned int)(-1);
506 best_m = 0;
507 best_clk = 0;
508 best_div = 0;
509 for (clk = 0; clk < NUM_CLOCKS; clk++) {
510 for (div = 0; div < NUM_LPG_PRE_DIVIDE; div++) {
511 /* period_n = (PWM Period / N) */
512 /* tmp_p = (Pre-divide * Clock Period) * 2^m */
513 tmp_p = pt_t[div][clk];
514 for (m = 0; m <= QPNP_PWM_M_MAX; m++) {
515 if (period_n > tmp_p)
516 cur_err = period_n - tmp_p;
517 else
518 cur_err = tmp_p - period_n;
519
520 if (cur_err < min_err) {
521 min_err = cur_err;
522 best_m = m;
523 best_clk = clk;
524 best_div = div;
525 }
526
527 if (m && cur_err > last_err)
528 /* Break for bigger cur_err */
529 break;
530
531 last_err = cur_err;
532 tmp_p <<= 1;
533 }
534 }
535 }
536
537 /* Adapt to optimal pwm size, the higher the resolution the better */
538 if (!force_pwm_size) {
539 if (supported_sizes == QPNP_PWM_SIZE_7_8_BIT) {
540 if (n == 7 && best_m >= 1) {
541 n += 1;
542 best_m -= 1;
543 }
544 } else if (n == 6) {
545 if (best_m >= 3) {
546 n += 3;
547 best_m -= 3;
548 } else if (best_m >= 1 && (
549 chip->sub_type != QPNP_PWM_MODE_ONLY_SUB_TYPE &&
550 chip->sub_type != QPNP_LPG_S_CHAN_SUB_TYPE)) {
551 n += 1;
552 best_m -= 1;
553 }
554 }
555 }
556
557 period->pwm_size = n;
558 period->clk = best_clk;
559 period->pre_div = best_div;
560 period->pre_div_exp = best_m;
561}
562
563static void qpnp_lpg_calc_pwm_value(struct _qpnp_pwm_config *pwm_config,
564 unsigned int period_value,
565 unsigned int duty_value)
566{
567 unsigned int max_pwm_value, tmp;
568
569 /* Figure out pwm_value with overflow handling */
570 tmp = 1 << (sizeof(tmp) * 8 - pwm_config->period.pwm_size);
571 if (duty_value < tmp) {
572 tmp = duty_value << pwm_config->period.pwm_size;
573 pwm_config->pwm_value = tmp / period_value;
574 } else {
575 tmp = period_value >> pwm_config->period.pwm_size;
576 pwm_config->pwm_value = duty_value / tmp;
577 }
578 max_pwm_value = (1 << pwm_config->period.pwm_size) - 1;
579 if (pwm_config->pwm_value > max_pwm_value)
580 pwm_config->pwm_value = max_pwm_value;
581 pr_debug("pwm_value: %d\n", pwm_config->pwm_value);
582}
583
584static int qpnp_lpg_change_table(struct qpnp_pwm_chip *chip,
585 int duty_pct[], int raw_value)
586{
587 unsigned int pwm_value, max_pwm_value;
588 struct qpnp_lut_config *lut = &chip->lpg_config.lut_config;
589 int i, pwm_size, rc = 0;
590 int burst_size = SPMI_MAX_BUF_LEN;
591 int list_len = lut->list_len << 1;
592 int offset = (lut->lo_index << 1) - 2;
593
594 pwm_size = QPNP_GET_PWM_SIZE(
595 chip->qpnp_lpg_registers[QPNP_LPG_PWM_SIZE_CLK]) +
596 QPNP_MIN_PWM_BIT_SIZE;
597
598 max_pwm_value = (1 << pwm_size) - 1;
599
600 if (unlikely(lut->list_len != (lut->hi_index - lut->lo_index + 1))) {
601 pr_err("LUT internal Data structure corruption detected\n");
602 pr_err("LUT list size: %d\n", lut->list_len);
603 pr_err("However, index size is: %d\n",
604 (lut->hi_index - lut->lo_index + 1));
605 return -EINVAL;
606 }
607
608 for (i = 0; i < lut->list_len; i++) {
609 if (raw_value)
610 pwm_value = duty_pct[i];
611 else
612 pwm_value = (duty_pct[i] << pwm_size) / 100;
613
614 if (pwm_value > max_pwm_value)
615 pwm_value = max_pwm_value;
616
617 if (chip->pwm_config.supported_sizes == QPNP_PWM_SIZE_7_8_BIT) {
618 lut->duty_pct_list[i] = pwm_value;
619 } else {
620 lut->duty_pct_list[i*2] = pwm_value;
621 lut->duty_pct_list[(i*2)+1] = (pwm_value >>
622 QPNP_PWM_VALUE_MSB_SHIFT) & QPNP_PWM_VALUE_MSB_MASK;
623 }
624 }
625
626 /*
627 * For the Keypad Backlight Lookup Table (KPDBL_LUT),
628 * offset is lo_index.
629 */
630 if (chip->pwm_config.supported_sizes == QPNP_PWM_SIZE_7_8_BIT)
631 offset = lut->lo_index;
632
633 /* Write with max allowable burst mode, each entry is of two bytes */
634 for (i = 0; i < list_len; i += burst_size) {
635 if (i + burst_size >= list_len)
636 burst_size = list_len - i;
637 rc = regmap_bulk_write(chip->regmap,
638 chip->lpg_config.lut_base_addr + offset + i,
639 lut->duty_pct_list + i,
640 burst_size);
641 }
642
643 return rc;
644}
645
646static void qpnp_lpg_save_period(struct qpnp_pwm_chip *chip)
647{
648 u8 mask, val;
649 struct _qpnp_pwm_config *pwm_config = &chip->pwm_config;
650
651 if (chip->sub_type == QPNP_PWM_MODE_ONLY_SUB_TYPE) {
652 QPNP_SET_PWM_CLK_SUB_TYPE(val, pwm_config->period.clk,
653 pwm_config->period.pwm_size);
654 mask = QPNP_PWM_SIZE_MASK_SUB_TYPE |
655 QPNP_PWM_FREQ_CLK_SELECT_MASK_SUB_TYPE;
656 } else {
657 QPNP_SET_PWM_CLK(val, pwm_config->period.clk,
658 pwm_config->period.pwm_size);
659 mask = QPNP_PWM_SIZE_MASK | QPNP_PWM_FREQ_CLK_SELECT_MASK;
660 }
661
662 qpnp_lpg_save(&chip->qpnp_lpg_registers[QPNP_LPG_PWM_SIZE_CLK],
663 mask, val);
664
665 QPNP_SET_PWM_FREQ_PREDIV(val, pwm_config->period.pre_div,
666 pwm_config->period.pre_div_exp);
667
668 mask = QPNP_PWM_FREQ_PRE_DIVIDE_MASK | QPNP_PWM_FREQ_EXP_MASK;
669
670 qpnp_lpg_save(&chip->qpnp_lpg_registers[QPNP_LPG_PWM_FREQ_PREDIV_CLK],
671 mask, val);
672}
673
674static int qpnp_lpg_save_pwm_value(struct qpnp_pwm_chip *chip)
675{
676 unsigned int max_pwm_value;
677 int pwm_size;
678 u8 mask, value;
679 struct _qpnp_pwm_config *pwm_config = &chip->pwm_config;
680 struct qpnp_lpg_config *lpg_config = &chip->lpg_config;
681 int rc;
682
683 if (chip->sub_type == QPNP_PWM_MODE_ONLY_SUB_TYPE)
684 pwm_size = QPNP_GET_PWM_SIZE_SUB_TYPE(
685 chip->qpnp_lpg_registers[QPNP_LPG_PWM_SIZE_CLK]) ?
686 QPNP_MAX_PWM_BIT_SIZE : QPNP_MIN_PWM_BIT_SIZE;
687 else
688 pwm_size = QPNP_GET_PWM_SIZE(
689 chip->qpnp_lpg_registers[QPNP_LPG_PWM_SIZE_CLK]) +
690 QPNP_MIN_PWM_BIT_SIZE;
691
692 max_pwm_value = (1 << pwm_size) - 1;
693
694 if (pwm_config->pwm_value > max_pwm_value)
695 pwm_config->pwm_value = max_pwm_value;
696
697 value = pwm_config->pwm_value;
698 mask = QPNP_PWM_VALUE_LSB_MASK;
699
700 pr_debug("pwm_lsb value:%d\n", value & mask);
701 rc = qpnp_lpg_save_and_write(value, mask,
702 &chip->qpnp_lpg_registers[QPNP_PWM_VALUE_LSB],
703 SPMI_LPG_REG_ADDR(lpg_config->base_addr,
704 QPNP_PWM_VALUE_LSB), 1, chip);
705 if (rc)
706 return rc;
707
708 value = (pwm_config->pwm_value >> QPNP_PWM_VALUE_MSB_SHIFT) &
709 QPNP_PWM_VALUE_MSB_MASK;
710
711 mask = QPNP_PWM_VALUE_MSB_MASK;
712
713 pr_debug("pwm_msb value:%d\n", value);
714 rc = qpnp_lpg_save_and_write(value, mask,
715 &chip->qpnp_lpg_registers[QPNP_PWM_VALUE_MSB],
716 SPMI_LPG_REG_ADDR(lpg_config->base_addr,
717 QPNP_PWM_VALUE_MSB), 1, chip);
718 if (rc)
719 return rc;
720
721 if (chip->sub_type == QPNP_PWM_MODE_ONLY_SUB_TYPE ||
722 chip->sub_type == QPNP_LPG_S_CHAN_SUB_TYPE) {
723 value = QPNP_PWM_SYNC_VALUE & QPNP_PWM_SYNC_MASK;
724 rc = regmap_write(chip->regmap,
725 SPMI_LPG_REG_ADDR(lpg_config->base_addr,
726 SPMI_LPG_PWM_SYNC),
727 value);
728 }
729
730 return rc;
731}
732
733static int qpnp_lpg_configure_pattern(struct qpnp_pwm_chip *chip)
734{
735 struct qpnp_lpg_config *lpg_config = &chip->lpg_config;
736 struct qpnp_lut_config *lut_config = &lpg_config->lut_config;
737 u8 value, mask;
738
739 qpnp_set_pattern_config(&value, lut_config);
740
741 mask = QPNP_RAMP_DIRECTION_MASK | QPNP_PATTERN_REPEAT_MASK |
742 QPNP_RAMP_TOGGLE_MASK | QPNP_EN_PAUSE_HI_MASK |
743 QPNP_EN_PAUSE_LO_MASK;
744
745 return qpnp_lpg_save_and_write(value, mask,
746 &chip->qpnp_lpg_registers[QPNP_LPG_PATTERN_CONFIG],
747 SPMI_LPG_REG_ADDR(lpg_config->base_addr,
748 QPNP_LPG_PATTERN_CONFIG), 1, chip);
749}
750
751static int qpnp_lpg_glitch_removal(struct qpnp_pwm_chip *chip, bool enable)
752{
753 struct qpnp_lpg_config *lpg_config = &chip->lpg_config;
754 u8 value, mask;
755
756 qpnp_set_pwm_type_config(&value, enable ? 1 : 0, 0, 0, 0);
757
758 mask = QPNP_EN_GLITCH_REMOVAL_MASK | QPNP_EN_FULL_SCALE_MASK |
759 QPNP_EN_PHASE_STAGGER_MASK | QPNP_PHASE_STAGGER_MASK;
760
761 pr_debug("pwm_type_config: %d\n", value);
762 return qpnp_lpg_save_and_write(value, mask,
763 &chip->qpnp_lpg_registers[QPNP_LPG_PWM_TYPE_CONFIG],
764 SPMI_LPG_REG_ADDR(lpg_config->base_addr,
765 QPNP_LPG_PWM_TYPE_CONFIG), 1, chip);
766}
767
768static int qpnp_lpg_configure_pwm(struct qpnp_pwm_chip *chip)
769{
770 struct qpnp_lpg_config *lpg_config = &chip->lpg_config;
771 int rc;
772
773 pr_debug("pwm_size_clk: %d\n",
774 chip->qpnp_lpg_registers[QPNP_LPG_PWM_SIZE_CLK]);
775 rc = regmap_write(chip->regmap,
776 SPMI_LPG_REG_ADDR(lpg_config->base_addr,
777 QPNP_LPG_PWM_SIZE_CLK),
778 *&chip->qpnp_lpg_registers[QPNP_LPG_PWM_SIZE_CLK]);
779
780 if (rc)
781 return rc;
782
783 pr_debug("pwm_freq_prediv_clk: %d\n",
784 chip->qpnp_lpg_registers[QPNP_LPG_PWM_FREQ_PREDIV_CLK]);
785 rc = regmap_write(chip->regmap,
786 SPMI_LPG_REG_ADDR(lpg_config->base_addr,
787 QPNP_LPG_PWM_FREQ_PREDIV_CLK),
788 *&chip->qpnp_lpg_registers[QPNP_LPG_PWM_FREQ_PREDIV_CLK]);
789 if (rc)
790 return rc;
791
792 /* Disable glitch removal when LPG/PWM is configured */
793 rc = qpnp_lpg_glitch_removal(chip, false);
794 if (rc) {
795 pr_err("Error in disabling glitch control, rc=%d\n", rc);
796 return rc;
797 }
798 return rc;
799}
800
801static int qpnp_configure_pwm_control(struct qpnp_pwm_chip *chip)
802{
803 struct qpnp_lpg_config *lpg_config = &chip->lpg_config;
804 u8 value, mask;
805
806 if (chip->sub_type == QPNP_PWM_MODE_ONLY_SUB_TYPE)
807 return 0;
808
809 value = QPNP_ENABLE_PWM_CONTROL(chip);
810
811 mask = QPNP_EN_PWM_HIGH_MASK | QPNP_EN_PWM_LO_MASK |
812 QPNP_PWM_SRC_SELECT_MASK | QPNP_PWM_EN_RAMP_GEN_MASK;
813 if (chip->sub_type != QPNP_LPG_S_CHAN_SUB_TYPE)
814 mask |= QPNP_EN_PWM_OUTPUT_MASK;
815
816 return qpnp_lpg_save_and_write(value, mask,
817 &chip->qpnp_lpg_registers[QPNP_ENABLE_CONTROL],
818 SPMI_LPG_REG_ADDR(lpg_config->base_addr,
819 QPNP_ENABLE_CONTROL), 1, chip);
820
821}
822
823static int qpnp_configure_lpg_control(struct qpnp_pwm_chip *chip)
824{
825 struct qpnp_lpg_config *lpg_config = &chip->lpg_config;
826 u8 value, mask;
827
828 value = QPNP_ENABLE_LUT_CONTROL(chip);
829
830 mask = QPNP_EN_PWM_HIGH_MASK | QPNP_EN_PWM_LO_MASK |
831 QPNP_PWM_SRC_SELECT_MASK | QPNP_PWM_EN_RAMP_GEN_MASK;
832 if (chip->sub_type != QPNP_LPG_S_CHAN_SUB_TYPE)
833 mask |= QPNP_EN_PWM_OUTPUT_MASK;
834
835 return qpnp_lpg_save_and_write(value, mask,
836 &chip->qpnp_lpg_registers[QPNP_ENABLE_CONTROL],
837 SPMI_LPG_REG_ADDR(lpg_config->base_addr,
838 QPNP_ENABLE_CONTROL), 1, chip);
839
840}
841
842static int qpnp_lpg_configure_ramp_step_duration(struct qpnp_pwm_chip *chip)
843{
844 struct qpnp_lpg_config *lpg_config = &chip->lpg_config;
845 struct qpnp_lut_config lut_config = lpg_config->lut_config;
846 int rc, value;
847 u8 val, mask;
848
849 value = QPNP_GET_RAMP_STEP_DURATION(lut_config.ramp_step_ms);
850 val = value & QPNP_RAMP_STEP_DURATION_LSB_MASK;
851 mask = QPNP_RAMP_STEP_DURATION_LSB_MASK;
852
853 rc = qpnp_lpg_save_and_write(val, mask,
854 &chip->qpnp_lpg_registers[QPNP_RAMP_STEP_DURATION_LSB],
855 SPMI_LPG_REG_ADDR(lpg_config->base_addr,
856 QPNP_RAMP_STEP_DURATION_LSB), 1, chip);
857 if (rc)
858 return rc;
859
860 val = (value >> QPNP_RAMP_STEP_DURATION_MSB_SHIFT) &
861 QPNP_RAMP_STEP_DURATION_MSB_MASK;
862
863 mask = QPNP_RAMP_STEP_DURATION_MSB_MASK;
864
865 return qpnp_lpg_save_and_write(val, mask,
866 &chip->qpnp_lpg_registers[QPNP_RAMP_STEP_DURATION_MSB],
867 SPMI_LPG_REG_ADDR(lpg_config->base_addr,
868 QPNP_RAMP_STEP_DURATION_MSB), 1, chip);
869}
870
871static int qpnp_lpg_configure_pause(struct qpnp_pwm_chip *chip)
872{
873 struct qpnp_lpg_config *lpg_config = &chip->lpg_config;
874 struct qpnp_lut_config lut_config = lpg_config->lut_config;
875 u8 value, mask;
876 int rc = 0;
877
878 if (lut_config.enable_pause_hi) {
879 value = lut_config.lut_pause_hi_cnt;
880 mask = QPNP_PAUSE_HI_MULTIPLIER_LSB_MASK;
881
882 rc = qpnp_lpg_save_and_write(value, mask,
883 &chip->qpnp_lpg_registers[QPNP_PAUSE_HI_MULTIPLIER_LSB],
884 SPMI_LPG_REG_ADDR(lpg_config->base_addr,
885 QPNP_PAUSE_HI_MULTIPLIER_LSB), 1, chip);
886 if (rc)
887 return rc;
888
889 value = (lut_config.lut_pause_hi_cnt >>
890 QPNP_PAUSE_HI_MULTIPLIER_MSB_SHIFT) &
891 QPNP_PAUSE_HI_MULTIPLIER_MSB_MASK;
892
893 mask = QPNP_PAUSE_HI_MULTIPLIER_MSB_MASK;
894
895 rc = qpnp_lpg_save_and_write(value, mask,
896 &chip->qpnp_lpg_registers[QPNP_PAUSE_HI_MULTIPLIER_MSB],
897 SPMI_LPG_REG_ADDR(lpg_config->base_addr,
898 QPNP_PAUSE_HI_MULTIPLIER_MSB), 1, chip);
899 } else {
900 value = 0;
901 mask = QPNP_PAUSE_HI_MULTIPLIER_LSB_MASK;
902
903 rc = qpnp_lpg_save_and_write(value, mask,
904 &chip->qpnp_lpg_registers[QPNP_PAUSE_HI_MULTIPLIER_LSB],
905 SPMI_LPG_REG_ADDR(lpg_config->base_addr,
906 QPNP_PAUSE_HI_MULTIPLIER_LSB), 1, chip);
907 if (rc)
908 return rc;
909
910 mask = QPNP_PAUSE_HI_MULTIPLIER_MSB_MASK;
911
912 rc = qpnp_lpg_save_and_write(value, mask,
913 &chip->qpnp_lpg_registers[QPNP_PAUSE_HI_MULTIPLIER_MSB],
914 SPMI_LPG_REG_ADDR(lpg_config->base_addr,
915 QPNP_PAUSE_HI_MULTIPLIER_MSB), 1, chip);
916 if (rc)
917 return rc;
918
919 }
920
921 if (lut_config.enable_pause_lo) {
922 value = lut_config.lut_pause_lo_cnt;
923 mask = QPNP_PAUSE_LO_MULTIPLIER_LSB_MASK;
924
925 rc = qpnp_lpg_save_and_write(value, mask,
926 &chip->qpnp_lpg_registers[QPNP_PAUSE_LO_MULTIPLIER_LSB],
927 SPMI_LPG_REG_ADDR(lpg_config->base_addr,
928 QPNP_PAUSE_LO_MULTIPLIER_LSB), 1, chip);
929 if (rc)
930 return rc;
931
932 value = (lut_config.lut_pause_lo_cnt >>
933 QPNP_PAUSE_LO_MULTIPLIER_MSB_SHIFT) &
934 QPNP_PAUSE_LO_MULTIPLIER_MSB_MASK;
935
936 mask = QPNP_PAUSE_LO_MULTIPLIER_MSB_MASK;
937
938 rc = qpnp_lpg_save_and_write(value, mask,
939 &chip->qpnp_lpg_registers[QPNP_PAUSE_LO_MULTIPLIER_MSB],
940 SPMI_LPG_REG_ADDR(lpg_config->base_addr,
941 QPNP_PAUSE_LO_MULTIPLIER_MSB), 1, chip);
942 } else {
943 value = 0;
944 mask = QPNP_PAUSE_LO_MULTIPLIER_LSB_MASK;
945
946 rc = qpnp_lpg_save_and_write(value, mask,
947 &chip->qpnp_lpg_registers[QPNP_PAUSE_LO_MULTIPLIER_LSB],
948 SPMI_LPG_REG_ADDR(lpg_config->base_addr,
949 QPNP_PAUSE_LO_MULTIPLIER_LSB), 1, chip);
950 if (rc)
951 return rc;
952
953 mask = QPNP_PAUSE_LO_MULTIPLIER_MSB_MASK;
954
955 rc = qpnp_lpg_save_and_write(value, mask,
956 &chip->qpnp_lpg_registers[QPNP_PAUSE_LO_MULTIPLIER_MSB],
957 SPMI_LPG_REG_ADDR(lpg_config->base_addr,
958 QPNP_PAUSE_LO_MULTIPLIER_MSB), 1, chip);
959 return rc;
960 }
961
962 return rc;
963}
964
965static int qpnp_lpg_configure_index(struct qpnp_pwm_chip *chip)
966{
967 struct qpnp_lpg_config *lpg_config = &chip->lpg_config;
968 struct qpnp_lut_config lut_config = lpg_config->lut_config;
969 u8 value, mask;
970 int rc = 0;
971
972 value = lut_config.hi_index;
973 mask = QPNP_HI_INDEX_MASK;
974
975 rc = qpnp_lpg_save_and_write(value, mask,
976 &chip->qpnp_lpg_registers[QPNP_HI_INDEX],
977 SPMI_LPG_REG_ADDR(lpg_config->base_addr,
978 QPNP_HI_INDEX), 1, chip);
979 if (rc)
980 return rc;
981
982 value = lut_config.lo_index;
983 mask = QPNP_LO_INDEX_MASK;
984
985 rc = qpnp_lpg_save_and_write(value, mask,
986 &chip->qpnp_lpg_registers[QPNP_LO_INDEX],
987 SPMI_LPG_REG_ADDR(lpg_config->base_addr,
988 QPNP_LO_INDEX), 1, chip);
989
990 return rc;
991}
992
993static int qpnp_lpg_change_lut(struct qpnp_pwm_chip *chip)
994{
995 int rc;
996
997 rc = qpnp_lpg_configure_pattern(chip);
998 if (rc) {
999 pr_err("Failed to configure LUT pattern");
1000 return rc;
1001 }
1002 rc = qpnp_lpg_configure_pwm(chip);
1003 if (rc) {
1004 pr_err("Failed to configure LUT pattern");
1005 return rc;
1006 }
1007 rc = qpnp_configure_lpg_control(chip);
1008 if (rc) {
1009 pr_err("Failed to configure pause registers");
1010 return rc;
1011 }
1012 rc = qpnp_lpg_configure_ramp_step_duration(chip);
1013 if (rc) {
1014 pr_err("Failed to configure duty time");
1015 return rc;
1016 }
1017 rc = qpnp_lpg_configure_pause(chip);
1018 if (rc) {
1019 pr_err("Failed to configure pause registers");
1020 return rc;
1021 }
1022 rc = qpnp_lpg_configure_index(chip);
1023 if (rc) {
1024 pr_err("Failed to configure index registers");
1025 return rc;
1026 }
1027 return rc;
1028}
1029
1030static int qpnp_dtest_config(struct qpnp_pwm_chip *chip, bool enable)
1031{
1032 struct qpnp_lpg_config *lpg_config = &chip->lpg_config;
1033 u8 value;
1034 u8 mask;
1035 u16 addr;
1036 int rc = 0;
1037
1038 value = 0xA5;
1039
1040 addr = SPMI_LPG_REG_ADDR(lpg_config->base_addr, QPNP_LPG_SEC_ACCESS);
1041
1042 rc = regmap_write(chip->regmap, addr, value);
1043
1044 if (rc) {
1045 pr_err("Couldn't set the access for test mode\n");
1046 return rc;
1047 }
1048
1049 addr = SPMI_LPG_REG_ADDR(lpg_config->base_addr,
1050 QPNP_LPG_DTEST + chip->dtest_line - 1);
1051
1052 if (chip->sub_type == QPNP_PWM_MODE_ONLY_SUB_TYPE)
1053 mask = QPNP_PWM_DTEST_OUTPUT_MASK;
1054 else
1055 mask = QPNP_LPG_DTEST_OUTPUT_MASK;
1056
1057 if (enable)
1058 value = chip->dtest_output & mask;
1059 else
1060 value = 0;
1061
1062 pr_debug("Setting TEST mode for channel %d addr:%x value: %x\n",
1063 chip->channel_id, addr, value);
1064
1065 rc = regmap_write(chip->regmap, addr, value);
1066
1067 return rc;
1068}
1069
1070static int qpnp_lpg_configure_lut_state(struct qpnp_pwm_chip *chip,
1071 enum qpnp_lut_state state)
1072{
1073 struct qpnp_lpg_config *lpg_config = &chip->lpg_config;
1074 u8 value1, value2, mask1, mask2;
1075 u8 *reg1, *reg2;
1076 u16 addr, addr1;
1077 int rc;
1078 bool test_enable;
1079
1080 value1 = chip->qpnp_lpg_registers[QPNP_RAMP_CONTROL];
1081 reg1 = &chip->qpnp_lpg_registers[QPNP_RAMP_CONTROL];
1082 reg2 = &chip->qpnp_lpg_registers[QPNP_ENABLE_CONTROL];
1083 mask2 = QPNP_EN_PWM_HIGH_MASK | QPNP_EN_PWM_LO_MASK |
1084 QPNP_PWM_SRC_SELECT_MASK | QPNP_PWM_EN_RAMP_GEN_MASK;
1085 if (chip->sub_type != QPNP_LPG_S_CHAN_SUB_TYPE)
1086 mask2 |= QPNP_EN_PWM_OUTPUT_MASK;
1087
1088 if (chip->sub_type == QPNP_LPG_CHAN_SUB_TYPE
1089 && chip->revision == QPNP_LPG_REVISION_0) {
1090 if (state == QPNP_LUT_ENABLE) {
1091 QPNP_ENABLE_LUT_V0(value1);
1092 value2 = QPNP_ENABLE_LPG_MODE(chip);
1093 } else {
1094 QPNP_DISABLE_LUT_V0(value1);
1095 value2 = QPNP_DISABLE_LPG_MODE(chip);
1096 }
1097 mask1 = QPNP_RAMP_START_MASK;
1098 addr1 = SPMI_LPG_REG_ADDR(lpg_config->base_addr,
1099 QPNP_RAMP_CONTROL);
1100 } else if ((chip->sub_type == QPNP_LPG_CHAN_SUB_TYPE
1101 && chip->revision == QPNP_LPG_REVISION_1)
1102 || chip->sub_type == QPNP_LPG_S_CHAN_SUB_TYPE) {
1103 if (state == QPNP_LUT_ENABLE) {
1104 QPNP_ENABLE_LUT_V1(value1,
1105 lpg_config->lut_config.ramp_index);
1106 value2 = QPNP_ENABLE_LPG_MODE(chip);
1107 } else {
1108 value2 = QPNP_DISABLE_LPG_MODE(chip);
1109 }
1110 mask1 = value1;
1111 addr1 = lpg_config->lut_base_addr +
1112 SPMI_LPG_REV1_RAMP_CONTROL_OFFSET;
1113 } else {
1114 pr_err("Unsupported LPG subtype 0x%02x, revision 0x%02x\n",
1115 chip->sub_type, chip->revision);
1116 return -EINVAL;
1117 }
1118
1119 addr = SPMI_LPG_REG_ADDR(lpg_config->base_addr,
1120 QPNP_ENABLE_CONTROL);
1121
1122 if (chip->in_test_mode) {
1123 test_enable = (state == QPNP_LUT_ENABLE) ? 1 : 0;
1124 rc = qpnp_dtest_config(chip, test_enable);
1125 if (rc)
1126 pr_err("Failed to configure TEST mode\n");
1127 }
1128
1129 rc = qpnp_lpg_save_and_write(value2, mask2, reg2,
1130 addr, 1, chip);
1131 if (rc)
1132 return rc;
1133
1134 if (state == QPNP_LUT_ENABLE
1135 || (chip->sub_type == QPNP_LPG_CHAN_SUB_TYPE
1136 && chip->revision == QPNP_LPG_REVISION_0))
1137 rc = qpnp_lpg_save_and_write(value1, mask1, reg1,
1138 addr1, 1, chip);
1139 return rc;
1140}
1141
1142static inline int qpnp_enable_pwm_mode(struct qpnp_pwm_chip *chip)
1143{
1144 if (chip->pwm_config.supported_sizes == QPNP_PWM_SIZE_7_8_BIT)
1145 return QPNP_ENABLE_PWM_MODE_GPLED_CHANNEL(chip);
1146 return QPNP_ENABLE_PWM_MODE(chip);
1147}
1148
1149static int qpnp_lpg_configure_pwm_state(struct qpnp_pwm_chip *chip,
1150 enum qpnp_pwm_state state)
1151{
1152 struct qpnp_lpg_config *lpg_config = &chip->lpg_config;
1153 u8 value, mask;
1154 int rc;
1155 bool test_enable;
1156
1157 if (chip->sub_type == QPNP_PWM_MODE_ONLY_SUB_TYPE) {
1158 if (state == QPNP_PWM_ENABLE)
1159 value = QPNP_ENABLE_PWM_MODE_ONLY_SUB_TYPE;
1160 else
1161 value = QPNP_DISABLE_PWM_MODE_ONLY_SUB_TYPE;
1162
1163 mask = QPNP_PWM_MODE_ONLY_ENABLE_DISABLE_MASK_SUB_TYPE;
1164 } else {
1165 if (state == QPNP_PWM_ENABLE)
1166 value = qpnp_enable_pwm_mode(chip);
1167 else
1168 value = QPNP_DISABLE_PWM_MODE(chip);
1169
1170 mask = QPNP_EN_PWM_HIGH_MASK | QPNP_EN_PWM_LO_MASK |
1171 QPNP_PWM_SRC_SELECT_MASK | QPNP_PWM_EN_RAMP_GEN_MASK;
1172 if (chip->sub_type != QPNP_LPG_S_CHAN_SUB_TYPE)
1173 mask |= QPNP_EN_PWM_OUTPUT_MASK;
1174 }
1175
1176 if (chip->in_test_mode) {
1177 test_enable = (state == QPNP_PWM_ENABLE) ? 1 : 0;
1178 rc = qpnp_dtest_config(chip, test_enable);
1179 if (rc)
1180 pr_err("Failed to configure TEST mode\n");
1181 }
1182
1183 pr_debug("pwm_enable_control: %d\n", value);
1184 rc = qpnp_lpg_save_and_write(value, mask,
1185 &chip->qpnp_lpg_registers[QPNP_ENABLE_CONTROL],
1186 SPMI_LPG_REG_ADDR(lpg_config->base_addr,
1187 QPNP_ENABLE_CONTROL), 1, chip);
1188 if (rc)
1189 goto out;
1190
1191 /*
1192 * Due to LPG hardware bug, in the PWM mode, having enabled PWM,
1193 * We have to write PWM values one more time.
1194 */
1195 if (state == QPNP_PWM_ENABLE)
1196 return qpnp_lpg_save_pwm_value(chip);
1197
1198out:
1199 return rc;
1200}
1201
1202static int _pwm_config(struct qpnp_pwm_chip *chip,
1203 enum time_level tm_lvl,
1204 int duty_value, int period_value)
1205{
1206 int rc;
1207 struct _qpnp_pwm_config *pwm_config = &chip->pwm_config;
1208 struct pwm_period_config *period = &pwm_config->period;
1209
1210 pwm_config->pwm_duty = (tm_lvl == LVL_USEC) ? duty_value :
1211 duty_value / NSEC_PER_USEC;
1212 qpnp_lpg_calc_pwm_value(pwm_config, period_value, duty_value);
1213 rc = qpnp_lpg_save_pwm_value(chip);
1214 if (rc)
1215 goto out;
David Collins8885f792017-01-26 14:36:34 -08001216
Fenglin Wu19d2c032017-07-03 12:44:59 +08001217 if (pwm_config->update_period) {
1218 rc = qpnp_lpg_configure_pwm(chip);
1219 if (rc)
1220 goto out;
1221 rc = qpnp_configure_pwm_control(chip);
1222 if (rc)
1223 goto out;
1224 if (!rc && chip->enabled) {
1225 rc = qpnp_lpg_configure_pwm_state(chip,
1226 QPNP_PWM_ENABLE);
1227 if (rc) {
1228 pr_err("Error in configuring pwm state, rc=%d\n",
1229 rc);
1230 return rc;
1231 }
David Collins8885f792017-01-26 14:36:34 -08001232
Fenglin Wu19d2c032017-07-03 12:44:59 +08001233 /* Enable the glitch removal after PWM is enabled */
1234 rc = qpnp_lpg_glitch_removal(chip, true);
1235 if (rc) {
1236 pr_err("Error in enabling glitch control, rc=%d\n",
1237 rc);
1238 return rc;
1239 }
David Collins8885f792017-01-26 14:36:34 -08001240 }
1241 }
David Collins8885f792017-01-26 14:36:34 -08001242 pr_debug("duty/period=%u/%u %s: pwm_value=%d (of %d)\n",
1243 (unsigned int)duty_value, (unsigned int)period_value,
1244 (tm_lvl == LVL_USEC) ? "usec" : "nsec",
1245 pwm_config->pwm_value, 1 << period->pwm_size);
1246
1247out:
1248 return rc;
1249}
1250
1251static int _pwm_lut_config(struct qpnp_pwm_chip *chip, int period_us,
1252 int duty_pct[], struct lut_params lut_params)
1253{
1254 struct qpnp_lpg_config *lpg_config;
1255 struct qpnp_lut_config *lut_config;
1256 struct pwm_period_config *period;
1257 struct _qpnp_pwm_config *pwm_config;
1258 int start_idx = lut_params.start_idx;
1259 int len = lut_params.idx_len;
1260 int flags = lut_params.flags;
1261 int raw_lut, ramp_step_ms;
1262 int rc = 0;
1263
1264 pwm_config = &chip->pwm_config;
1265 lpg_config = &chip->lpg_config;
1266 lut_config = &lpg_config->lut_config;
1267 period = &pwm_config->period;
1268
1269 if (flags & PM_PWM_LUT_NO_TABLE)
1270 goto after_table_write;
1271
1272 raw_lut = 0;
1273 if (flags & PM_PWM_LUT_USE_RAW_VALUE)
1274 raw_lut = 1;
1275
1276 lut_config->list_len = len;
1277 lut_config->lo_index = start_idx + 1;
1278 lut_config->hi_index = start_idx + len;
1279
1280 rc = qpnp_lpg_change_table(chip, duty_pct, raw_lut);
1281 if (rc) {
1282 pr_err("qpnp_lpg_change_table: rc=%d\n", rc);
1283 return -EINVAL;
1284 }
1285
1286after_table_write:
1287 ramp_step_ms = lut_params.ramp_step_ms;
1288
1289 if (ramp_step_ms > PM_PWM_LUT_RAMP_STEP_TIME_MAX)
1290 ramp_step_ms = PM_PWM_LUT_RAMP_STEP_TIME_MAX;
1291
1292 QPNP_SET_PAUSE_CNT(lut_config->lut_pause_lo_cnt,
1293 lut_params.lut_pause_lo, ramp_step_ms);
1294 if (lut_config->lut_pause_lo_cnt > PM_PWM_MAX_PAUSE_CNT)
1295 lut_config->lut_pause_lo_cnt = PM_PWM_MAX_PAUSE_CNT;
1296
1297 QPNP_SET_PAUSE_CNT(lut_config->lut_pause_hi_cnt,
1298 lut_params.lut_pause_hi, ramp_step_ms);
1299 if (lut_config->lut_pause_hi_cnt > PM_PWM_MAX_PAUSE_CNT)
1300 lut_config->lut_pause_hi_cnt = PM_PWM_MAX_PAUSE_CNT;
1301
1302 lut_config->ramp_step_ms = ramp_step_ms;
1303
1304 lut_config->ramp_direction = !!(flags & PM_PWM_LUT_RAMP_UP);
1305 lut_config->pattern_repeat = !!(flags & PM_PWM_LUT_LOOP);
1306 lut_config->ramp_toggle = !!(flags & PM_PWM_LUT_REVERSE);
1307 lut_config->enable_pause_hi = !!(flags & PM_PWM_LUT_PAUSE_HI_EN);
1308 lut_config->enable_pause_lo = !!(flags & PM_PWM_LUT_PAUSE_LO_EN);
1309
1310 rc = qpnp_lpg_change_lut(chip);
1311
1312 if (!rc && chip->enabled)
1313 rc = qpnp_lpg_configure_lut_state(chip, QPNP_LUT_ENABLE);
1314
1315 return rc;
1316}
1317
Fenglin Wuc81fb382017-08-14 12:45:21 +08001318/* lpg_lock should be held while calling _pwm_enable() */
David Collins8885f792017-01-26 14:36:34 -08001319static int _pwm_enable(struct qpnp_pwm_chip *chip)
1320{
1321 int rc = 0;
David Collins8885f792017-01-26 14:36:34 -08001322
1323 if (QPNP_IS_PWM_CONFIG_SELECTED(
1324 chip->qpnp_lpg_registers[QPNP_ENABLE_CONTROL]) ||
1325 chip->flags & QPNP_PWM_LUT_NOT_SUPPORTED) {
1326 rc = qpnp_lpg_configure_pwm_state(chip, QPNP_PWM_ENABLE);
1327 } else if (!(chip->flags & QPNP_PWM_LUT_NOT_SUPPORTED)) {
1328 rc = qpnp_lpg_configure_lut_state(chip, QPNP_LUT_ENABLE);
1329 }
1330
1331 if (!rc)
1332 chip->enabled = true;
1333
Fenglin Wuc81fb382017-08-14 12:45:21 +08001334 return rc;
1335}
David Collins8885f792017-01-26 14:36:34 -08001336
Fenglin Wuc81fb382017-08-14 12:45:21 +08001337/* lpg_lock should be held while calling _pwm_change_mode() */
1338static int _pwm_change_mode(struct qpnp_pwm_chip *chip, enum pm_pwm_mode mode)
1339{
1340 int rc;
1341
1342 if (mode == PM_PWM_MODE_LPG)
1343 rc = qpnp_configure_lpg_control(chip);
1344 else
1345 rc = qpnp_configure_pwm_control(chip);
1346
1347 if (rc)
1348 pr_err("Failed to change the mode\n");
David Collins8885f792017-01-26 14:36:34 -08001349 return rc;
1350}
1351
1352/* APIs */
1353/**
1354 * qpnp_pwm_free - free a PWM device
1355 * @pwm_chip: the PWM chip
1356 * @pwm: the PWM device
1357 */
1358static void qpnp_pwm_free(struct pwm_chip *pwm_chip,
1359 struct pwm_device *pwm)
1360{
1361 struct qpnp_pwm_chip *chip = qpnp_pwm_from_pwm_chip(pwm_chip);
1362 unsigned long flags;
1363
1364 spin_lock_irqsave(&chip->lpg_lock, flags);
1365
1366 qpnp_lpg_configure_pwm_state(chip, QPNP_PWM_DISABLE);
1367 if (!(chip->flags & QPNP_PWM_LUT_NOT_SUPPORTED))
1368 qpnp_lpg_configure_lut_state(chip, QPNP_LUT_DISABLE);
1369
1370 chip->enabled = false;
1371 spin_unlock_irqrestore(&chip->lpg_lock, flags);
1372}
1373
1374/**
1375 * qpnp_pwm_config - change a PWM device configuration
1376 * @pwm: the PWM device
1377 * @period_ns: period in nanoseconds
1378 * @duty_ns: duty cycle in nanoseconds
1379 */
1380static int qpnp_pwm_config(struct pwm_chip *pwm_chip,
1381 struct pwm_device *pwm, int duty_ns, int period_ns)
1382{
1383 int rc;
1384 unsigned long flags;
1385 struct qpnp_pwm_chip *chip = qpnp_pwm_from_pwm_chip(pwm_chip);
1386 int prev_period_us = chip->pwm_config.pwm_period;
1387
1388 if ((unsigned int)period_ns < PM_PWM_PERIOD_MIN * NSEC_PER_USEC) {
1389 pr_err("Invalid pwm handle or parameters\n");
1390 return -EINVAL;
1391 }
1392
1393 spin_lock_irqsave(&chip->lpg_lock, flags);
1394
Fenglin Wu19d2c032017-07-03 12:44:59 +08001395 chip->pwm_config.update_period = false;
David Collins8885f792017-01-26 14:36:34 -08001396 if (prev_period_us > INT_MAX / NSEC_PER_USEC ||
1397 prev_period_us * NSEC_PER_USEC != period_ns) {
1398 qpnp_lpg_calc_period(LVL_NSEC, period_ns, chip);
1399 qpnp_lpg_save_period(chip);
1400 pwm->state.period = period_ns;
1401 chip->pwm_config.pwm_period = period_ns / NSEC_PER_USEC;
Fenglin Wu19d2c032017-07-03 12:44:59 +08001402 chip->pwm_config.update_period = true;
David Collins8885f792017-01-26 14:36:34 -08001403 }
1404
1405 rc = _pwm_config(chip, LVL_NSEC, duty_ns, period_ns);
1406
1407 spin_unlock_irqrestore(&chip->lpg_lock, flags);
1408
1409 if (rc)
1410 pr_err("Failed to configure PWM mode\n");
1411
1412 return rc;
1413}
1414
1415/**
1416 * qpnp_pwm_enable - start a PWM output toggling
1417 * @pwm_chip: the PWM chip
1418 * @pwm: the PWM device
1419 */
1420static int qpnp_pwm_enable(struct pwm_chip *pwm_chip,
1421 struct pwm_device *pwm)
1422{
1423 int rc;
1424 struct qpnp_pwm_chip *chip = qpnp_pwm_from_pwm_chip(pwm_chip);
Fenglin Wuc81fb382017-08-14 12:45:21 +08001425 unsigned long flags;
David Collins8885f792017-01-26 14:36:34 -08001426
Fenglin Wuc81fb382017-08-14 12:45:21 +08001427 spin_lock_irqsave(&chip->lpg_lock, flags);
David Collins8885f792017-01-26 14:36:34 -08001428 rc = _pwm_enable(chip);
1429 if (rc)
1430 pr_err("Failed to enable PWM channel: %d\n", chip->channel_id);
1431
Fenglin Wuc81fb382017-08-14 12:45:21 +08001432 spin_unlock_irqrestore(&chip->lpg_lock, flags);
1433
David Collins8885f792017-01-26 14:36:34 -08001434 return rc;
1435}
1436
1437/**
1438 * qpnp_pwm_disable - stop a PWM output toggling
1439 * @pwm_chip: the PWM chip
1440 * @pwm: the PWM device
1441 */
1442static void qpnp_pwm_disable(struct pwm_chip *pwm_chip,
1443 struct pwm_device *pwm)
1444{
1445
1446 struct qpnp_pwm_chip *chip = qpnp_pwm_from_pwm_chip(pwm_chip);
1447 unsigned long flags;
1448 int rc = 0;
1449
1450 spin_lock_irqsave(&chip->lpg_lock, flags);
1451
1452 if (QPNP_IS_PWM_CONFIG_SELECTED(
1453 chip->qpnp_lpg_registers[QPNP_ENABLE_CONTROL]) ||
1454 chip->flags & QPNP_PWM_LUT_NOT_SUPPORTED)
1455 rc = qpnp_lpg_configure_pwm_state(chip,
1456 QPNP_PWM_DISABLE);
1457 else if (!(chip->flags & QPNP_PWM_LUT_NOT_SUPPORTED))
1458 rc = qpnp_lpg_configure_lut_state(chip,
1459 QPNP_LUT_DISABLE);
1460
1461 if (!rc)
1462 chip->enabled = false;
1463
1464 spin_unlock_irqrestore(&chip->lpg_lock, flags);
1465
1466 if (rc)
1467 pr_err("Failed to disable PWM channel: %d\n",
1468 chip->channel_id);
1469}
1470
David Collins8885f792017-01-26 14:36:34 -08001471/**
1472 * pwm_change_mode - Change the PWM mode configuration
1473 * @pwm: the PWM device
1474 * @mode: Mode selection value
1475 */
1476int pwm_change_mode(struct pwm_device *pwm, enum pm_pwm_mode mode)
1477{
Fenglin Wuc81fb382017-08-14 12:45:21 +08001478 int rc = 0;
David Collins8885f792017-01-26 14:36:34 -08001479 unsigned long flags;
1480 struct qpnp_pwm_chip *chip;
1481
1482 if (pwm == NULL || IS_ERR(pwm) || pwm->chip == NULL) {
1483 pr_err("Invalid pwm handle or no pwm_chip\n");
1484 return -EINVAL;
1485 }
1486
1487 if (mode < PM_PWM_MODE_PWM || mode > PM_PWM_MODE_LPG) {
1488 pr_err("Invalid mode value\n");
1489 return -EINVAL;
1490 }
1491
1492 chip = qpnp_pwm_from_pwm_dev(pwm);
1493
1494 spin_lock_irqsave(&chip->lpg_lock, flags);
Fenglin Wuc81fb382017-08-14 12:45:21 +08001495 if (chip->pwm_mode != mode) {
1496 rc = _pwm_change_mode(chip, mode);
1497 if (rc) {
1498 pr_err("Failed to change mode: %d, rc=%d\n", mode, rc);
1499 goto unlock;
1500 }
1501 chip->pwm_mode = mode;
1502 if (chip->enabled) {
1503 rc = _pwm_enable(chip);
1504 if (rc) {
1505 pr_err("Failed to enable PWM, rc=%d\n", rc);
1506 goto unlock;
1507 }
1508 }
1509 }
1510unlock:
David Collins8885f792017-01-26 14:36:34 -08001511 spin_unlock_irqrestore(&chip->lpg_lock, flags);
1512
1513 return rc;
1514}
1515EXPORT_SYMBOL(pwm_change_mode);
1516
1517/**
1518 * pwm_config_period - change PWM period
1519 *
1520 * @pwm: the PWM device
1521 * @pwm_p: period in struct qpnp_lpg_period
1522 */
1523int pwm_config_period(struct pwm_device *pwm,
1524 struct pwm_period_config *period)
1525{
1526 struct _qpnp_pwm_config *pwm_config;
1527 struct qpnp_lpg_config *lpg_config;
1528 struct qpnp_pwm_chip *chip;
1529 unsigned long flags;
1530 int rc = 0;
1531
1532 if (pwm == NULL || IS_ERR(pwm) || period == NULL)
1533 return -EINVAL;
1534 if (pwm->chip == NULL)
1535 return -ENODEV;
1536
1537 chip = qpnp_pwm_from_pwm_dev(pwm);
1538 pwm_config = &chip->pwm_config;
1539 lpg_config = &chip->lpg_config;
1540
1541 spin_lock_irqsave(&chip->lpg_lock, flags);
1542
1543 pwm_config->period.pwm_size = period->pwm_size;
1544 pwm_config->period.clk = period->clk;
1545 pwm_config->period.pre_div = period->pre_div;
1546 pwm_config->period.pre_div_exp = period->pre_div_exp;
1547
1548 qpnp_lpg_save_period(chip);
1549
1550 rc = regmap_write(chip->regmap,
1551 SPMI_LPG_REG_ADDR(lpg_config->base_addr,
1552 QPNP_LPG_PWM_SIZE_CLK),
1553 *&chip->qpnp_lpg_registers[QPNP_LPG_PWM_SIZE_CLK]);
1554
1555 if (rc) {
1556 pr_err("Write failed: QPNP_LPG_PWM_SIZE_CLK register, rc: %d\n",
1557 rc);
1558 goto out_unlock;
1559 }
1560
1561 rc = regmap_write(chip->regmap,
1562 SPMI_LPG_REG_ADDR(lpg_config->base_addr,
1563 QPNP_LPG_PWM_FREQ_PREDIV_CLK),
1564 *&chip->qpnp_lpg_registers[QPNP_LPG_PWM_FREQ_PREDIV_CLK]);
1565 if (rc) {
1566 pr_err("Failed to write to QPNP_LPG_PWM_FREQ_PREDIV_CLK\n");
1567 pr_err("register, rc = %d\n", rc);
1568 }
1569
1570out_unlock:
1571 spin_unlock_irqrestore(&chip->lpg_lock, flags);
1572 return rc;
1573}
1574EXPORT_SYMBOL(pwm_config_period);
1575
1576/**
1577 * pwm_config_pwm_value - change a PWM device configuration
1578 * @pwm: the PWM device
1579 * @pwm_value: the duty cycle in raw PWM value (< 2^pwm_size)
1580 */
1581int pwm_config_pwm_value(struct pwm_device *pwm, int pwm_value)
1582{
1583 struct qpnp_lpg_config *lpg_config;
1584 struct _qpnp_pwm_config *pwm_config;
1585 struct qpnp_pwm_chip *chip;
1586 unsigned long flags;
1587 int rc = 0;
1588
1589 if (pwm == NULL || IS_ERR(pwm)) {
1590 pr_err("Invalid parameter passed\n");
1591 return -EINVAL;
1592 }
1593
1594 if (pwm->chip == NULL) {
1595 pr_err("Invalid device handle\n");
1596 return -ENODEV;
1597 }
1598
1599 chip = qpnp_pwm_from_pwm_dev(pwm);
1600 lpg_config = &chip->lpg_config;
1601 pwm_config = &chip->pwm_config;
1602
1603 spin_lock_irqsave(&chip->lpg_lock, flags);
1604
1605 if (pwm_config->pwm_value == pwm_value)
1606 goto out_unlock;
1607
1608 pwm_config->pwm_value = pwm_value;
1609
1610 rc = qpnp_lpg_save_pwm_value(chip);
1611
1612 if (rc)
1613 pr_err("Could not update PWM value for channel %d rc=%d\n",
1614 chip->channel_id, rc);
1615
1616out_unlock:
1617 spin_unlock_irqrestore(&chip->lpg_lock, flags);
1618 return rc;
1619}
1620EXPORT_SYMBOL(pwm_config_pwm_value);
1621
1622/**
1623 * pwm_config_us - change a PWM device configuration
1624 * @pwm: the PWM device
1625 * @period_us: period in microseconds
1626 * @duty_us: duty cycle in microseconds
1627 */
1628int pwm_config_us(struct pwm_device *pwm, int duty_us, int period_us)
1629{
1630 int rc;
1631 unsigned long flags;
1632 struct qpnp_pwm_chip *chip;
1633
1634 if (pwm == NULL || IS_ERR(pwm) ||
1635 duty_us > period_us ||
1636 (unsigned int)period_us > PM_PWM_PERIOD_MAX ||
1637 (unsigned int)period_us < PM_PWM_PERIOD_MIN) {
1638 pr_err("Invalid pwm handle or parameters\n");
1639 return -EINVAL;
1640 }
1641
1642 chip = qpnp_pwm_from_pwm_dev(pwm);
1643
1644 spin_lock_irqsave(&chip->lpg_lock, flags);
1645
Fenglin Wu19d2c032017-07-03 12:44:59 +08001646 chip->pwm_config.update_period = false;
David Collins8885f792017-01-26 14:36:34 -08001647 if (chip->pwm_config.pwm_period != period_us) {
1648 qpnp_lpg_calc_period(LVL_USEC, period_us, chip);
1649 qpnp_lpg_save_period(chip);
1650 chip->pwm_config.pwm_period = period_us;
1651 if ((unsigned int)period_us >
1652 (unsigned int)(-1) / NSEC_PER_USEC)
1653 pwm->state.period = 0;
1654 else
1655 pwm->state.period
1656 = (unsigned int)period_us * NSEC_PER_USEC;
Fenglin Wu19d2c032017-07-03 12:44:59 +08001657 chip->pwm_config.update_period = true;
David Collins8885f792017-01-26 14:36:34 -08001658 }
1659
1660 rc = _pwm_config(chip, LVL_USEC, duty_us, period_us);
1661
1662 spin_unlock_irqrestore(&chip->lpg_lock, flags);
1663
1664 if (rc)
1665 pr_err("Failed to configure PWM mode\n");
1666
1667 return rc;
1668}
1669EXPORT_SYMBOL(pwm_config_us);
1670
1671/**
1672 * pwm_lut_config - change LPG LUT device configuration
1673 * @pwm: the PWM device
1674 * @period_us: period in micro second
1675 * @duty_pct: array of duty cycles in percent, like 20, 50.
1676 * @lut_params: Lookup table parameters
1677 */
1678int pwm_lut_config(struct pwm_device *pwm, int period_us,
1679 int duty_pct[], struct lut_params lut_params)
1680{
1681 unsigned long flags;
1682 struct qpnp_pwm_chip *chip;
1683 int rc = 0;
1684
1685 if (pwm == NULL || IS_ERR(pwm) || !lut_params.idx_len) {
1686 pr_err("Invalid pwm handle or idx_len=0\n");
1687 return -EINVAL;
1688 }
1689
1690 if (pwm->chip == NULL)
1691 return -ENODEV;
1692
1693 if (duty_pct == NULL && !(lut_params.flags & PM_PWM_LUT_NO_TABLE)) {
1694 pr_err("Invalid duty_pct with flag\n");
1695 return -EINVAL;
1696 }
1697
1698 chip = qpnp_pwm_from_pwm_dev(pwm);
1699
1700 if (chip->flags & QPNP_PWM_LUT_NOT_SUPPORTED) {
1701 pr_err("LUT mode isn't supported\n");
1702 return -EINVAL;
1703 }
1704
1705 if ((lut_params.start_idx + lut_params.idx_len) >
1706 chip->lpg_config.lut_size) {
1707 pr_err("Exceed LUT limit\n");
1708 return -EINVAL;
1709 }
1710
1711 if ((unsigned int)period_us > PM_PWM_PERIOD_MAX ||
1712 (unsigned int)period_us < PM_PWM_PERIOD_MIN) {
1713 pr_err("Period out of range\n");
1714 return -EINVAL;
1715 }
1716
1717 spin_lock_irqsave(&chip->lpg_lock, flags);
1718
1719 if (chip->pwm_config.pwm_period != period_us) {
1720 qpnp_lpg_calc_period(LVL_USEC, period_us, chip);
1721 qpnp_lpg_save_period(chip);
1722 chip->pwm_config.pwm_period = period_us;
1723 }
1724
1725 rc = _pwm_lut_config(chip, period_us, duty_pct, lut_params);
1726
1727 spin_unlock_irqrestore(&chip->lpg_lock, flags);
1728
1729 if (rc)
1730 pr_err("Failed to configure LUT\n");
1731
1732 return rc;
1733}
1734EXPORT_SYMBOL(pwm_lut_config);
1735
1736static int qpnp_parse_pwm_dt_config(struct device_node *of_pwm_node,
1737 struct device_node *of_parent, struct qpnp_pwm_chip *chip)
1738{
1739 int rc, period;
1740
1741 rc = of_property_read_u32(of_parent, "qcom,period", (u32 *)&period);
1742 if (rc) {
1743 pr_err("node is missing PWM Period prop");
1744 return rc;
1745 }
1746
1747 rc = of_property_read_u32(of_pwm_node, "qcom,duty",
1748 &chip->pwm_config.pwm_duty);
1749 if (rc) {
1750 pr_err("node is missing PWM Duty prop");
1751 return rc;
1752 }
1753
1754 if (period < chip->pwm_config.pwm_duty || period > PM_PWM_PERIOD_MAX ||
1755 period < PM_PWM_PERIOD_MIN) {
1756 pr_err("Invalid pwm period(%d) or duty(%d)\n", period,
1757 chip->pwm_config.pwm_duty);
1758 return -EINVAL;
1759 }
1760
1761 qpnp_lpg_calc_period(LVL_USEC, period, chip);
1762 qpnp_lpg_save_period(chip);
1763 chip->pwm_config.pwm_period = period;
Fenglin Wu19d2c032017-07-03 12:44:59 +08001764 chip->pwm_config.update_period = true;
David Collins8885f792017-01-26 14:36:34 -08001765
1766 rc = _pwm_config(chip, LVL_USEC, chip->pwm_config.pwm_duty, period);
1767
1768 return rc;
1769}
1770
1771static int qpnp_parse_lpg_dt_config(struct device_node *of_lpg_node,
1772 struct device_node *of_parent, struct qpnp_pwm_chip *chip)
1773{
1774 int rc, period, list_size, start_idx, *duty_pct_list;
1775 struct qpnp_lpg_config *lpg_config = &chip->lpg_config;
1776 struct qpnp_lut_config *lut_config = &lpg_config->lut_config;
1777 struct lut_params lut_params;
1778
1779 rc = of_property_read_u32(of_parent, "qcom,period", &period);
1780 if (rc) {
1781 pr_err("node is missing PWM Period prop\n");
1782 return rc;
1783 }
1784
1785 if (!of_get_property(of_lpg_node, "qcom,duty-percents", &list_size)) {
1786 pr_err("node is missing duty-pct list\n");
1787 return rc;
1788 }
1789
1790 rc = of_property_read_u32(of_lpg_node, "cell-index", &start_idx);
1791 if (rc) {
1792 pr_err("Missing start index\n");
1793 return rc;
1794 }
1795
1796 list_size /= sizeof(u32);
1797
1798 if (list_size + start_idx > lpg_config->lut_size) {
1799 pr_err("duty pct list size overflows\n");
1800 return -EINVAL;
1801 }
1802
1803 duty_pct_list = kcalloc(list_size, sizeof(*duty_pct_list), GFP_KERNEL);
1804 if (!duty_pct_list)
1805 return -ENOMEM;
1806
1807 rc = of_property_read_u32_array(of_lpg_node, "qcom,duty-percents",
1808 duty_pct_list, list_size);
1809 if (rc) {
1810 pr_err("invalid or missing property: qcom,duty-pcts-list\n");
1811 goto out;
1812 }
1813
1814 /* Read optional properties */
1815 rc = of_property_read_u32(of_lpg_node, "qcom,ramp-step-duration",
1816 &lut_config->ramp_step_ms);
1817 if (rc && rc != -EINVAL)
1818 goto out;
1819
1820 rc = of_property_read_u32(of_lpg_node, "qcom,lpg-lut-pause-hi",
1821 &lut_config->lut_pause_hi_cnt);
1822 if (rc && rc != -EINVAL)
1823 goto out;
1824
1825 rc = of_property_read_u32(of_lpg_node, "qcom,lpg-lut-pause-lo",
1826 &lut_config->lut_pause_lo_cnt);
1827 if (rc && rc != -EINVAL)
1828 goto out;
1829
1830 rc = of_property_read_u32(of_lpg_node, "qcom,lpg-lut-ramp-direction",
1831 (u32 *)&lut_config->ramp_direction);
1832 if (rc && rc != -EINVAL)
1833 goto out;
1834
1835 rc = of_property_read_u32(of_lpg_node, "qcom,lpg-lut-pattern-repeat",
1836 (u32 *)&lut_config->pattern_repeat);
1837 if (rc && rc != -EINVAL)
1838 goto out;
1839
1840 rc = of_property_read_u32(of_lpg_node, "qcom,lpg-lut-ramp-toggle",
1841 (u32 *)&lut_config->ramp_toggle);
1842 if (rc && rc != -EINVAL)
1843 goto out;
1844
1845 rc = of_property_read_u32(of_lpg_node, "qcom,lpg-lut-enable-pause-hi",
1846 (u32 *)&lut_config->enable_pause_hi);
1847 if (rc && rc != -EINVAL)
1848 goto out;
1849
1850 rc = of_property_read_u32(of_lpg_node, "qcom,lpg-lut-enable-pause-lo",
1851 (u32 *)&lut_config->enable_pause_lo);
1852 if (rc && rc != -EINVAL)
1853 goto out;
1854 rc = 0;
1855
1856 qpnp_set_lut_params(&lut_params, lut_config, start_idx, list_size);
1857
1858 _pwm_lut_config(chip, period, duty_pct_list, lut_params);
1859
1860out:
1861 kfree(duty_pct_list);
1862 return rc;
1863}
1864
1865static int qpnp_lpg_get_rev_subtype(struct qpnp_pwm_chip *chip)
1866{
1867 int rc;
1868 uint val;
1869
1870 rc = regmap_read(chip->regmap,
1871 chip->lpg_config.base_addr + SPMI_LPG_SUB_TYPE_OFFSET,
1872 &val);
1873
1874 if (rc) {
1875 pr_err("Couldn't read subtype rc: %d\n", rc);
1876 goto out;
1877 }
1878 chip->sub_type = (u8)val;
1879
1880 rc = regmap_read(chip->regmap,
1881 chip->lpg_config.base_addr + SPMI_LPG_REVISION2_OFFSET,
1882 &val);
1883
1884 if (rc) {
1885 pr_err("Couldn't read revision2 rc: %d\n", rc);
1886 goto out;
1887 }
1888 chip->revision = (u8)val;
1889
1890 if (chip->revision < QPNP_LPG_REVISION_0 ||
1891 chip->revision > QPNP_LPG_REVISION_1) {
1892 pr_err("Unknown LPG revision detected, rev:%d\n",
1893 chip->revision);
1894 rc = -EINVAL;
1895 goto out;
1896 }
1897
1898 if (chip->sub_type != QPNP_PWM_MODE_ONLY_SUB_TYPE
1899 && chip->sub_type != QPNP_LPG_CHAN_SUB_TYPE
1900 && chip->sub_type != QPNP_LPG_S_CHAN_SUB_TYPE) {
1901 pr_err("Unknown LPG/PWM subtype detected, subtype:%d\n",
1902 chip->sub_type);
1903 rc = -EINVAL;
1904 }
1905out:
1906 pr_debug("LPG rev 0x%02x subtype 0x%02x rc: %d\n", chip->revision,
1907 chip->sub_type, rc);
1908 return rc;
1909}
1910
1911/* Fill in lpg device elements based on values found in device tree. */
1912static int qpnp_parse_dt_config(struct platform_device *pdev,
1913 struct qpnp_pwm_chip *chip)
1914{
Fenglin Wuc81fb382017-08-14 12:45:21 +08001915 int rc, mode, lut_entry_size, list_size, i;
David Collins8885f792017-01-26 14:36:34 -08001916 const char *label;
1917 const __be32 *prop;
1918 u32 size;
1919 struct device_node *node;
1920 int found_pwm_subnode = 0;
1921 int found_lpg_subnode = 0;
1922 struct device_node *of_node = pdev->dev.of_node;
1923 struct qpnp_lpg_config *lpg_config = &chip->lpg_config;
1924 struct qpnp_lut_config *lut_config = &lpg_config->lut_config;
1925 struct _qpnp_pwm_config *pwm_config = &chip->pwm_config;
1926 int force_pwm_size = 0;
1927 int pwm_size_list[QPNP_PWM_SIZES_SUPPORTED];
1928
1929 rc = of_property_read_u32(of_node, "qcom,channel-id",
1930 &chip->channel_id);
1931 if (rc) {
1932 dev_err(&pdev->dev, "%s: node is missing LPG channel id\n",
1933 __func__);
1934 return -EINVAL;
1935 }
1936
1937 if (!of_get_property(of_node, "qcom,supported-sizes", &list_size)) {
1938 pr_err("Missing qcom,supported-size list\n");
1939 return -EINVAL;
1940 }
1941
1942 list_size /= sizeof(u32);
1943 if (list_size > QPNP_PWM_SIZES_SUPPORTED) {
1944 pr_err(" qcom,supported-size list is too big\n");
1945 return -EINVAL;
1946 }
1947
1948 rc = of_property_read_u32_array(of_node, "qcom,supported-sizes",
1949 pwm_size_list, list_size);
1950
1951 if (rc) {
1952 pr_err("Invalid qcom,supported-size property\n");
1953 return rc;
1954 }
1955
1956 for (i = 0; i < list_size; i++) {
1957 pwm_config->supported_sizes |=
1958 (1 << (pwm_size_list[i] - QPNP_MIN_PWM_BIT_SIZE));
1959 }
1960
1961 if (!(pwm_config->supported_sizes == QPNP_PWM_SIZE_6_9_BIT ||
1962 pwm_config->supported_sizes == QPNP_PWM_SIZE_7_8_BIT ||
1963 pwm_config->supported_sizes == QPNP_PWM_SIZE_6_7_9_BIT)) {
1964 pr_err("PWM sizes list qcom,supported-size is not proper\n");
1965 return -EINVAL;
1966 }
1967
1968 /*
1969 * For cetrain LPG channels PWM size can be forced. So that
1970 * for every requested pwm period closest pwm frequency is
1971 * selected in qpnp_lpg_calc_period() for the forced pwm size.
1972 */
1973 rc = of_property_read_u32(of_node, "qcom,force-pwm-size",
1974 &force_pwm_size);
1975 if (pwm_config->supported_sizes == QPNP_PWM_SIZE_7_8_BIT) {
1976 if (!(force_pwm_size == QPNP_PWM_SIZE_7_BIT ||
1977 force_pwm_size == QPNP_PWM_SIZE_8_BIT))
1978 force_pwm_size = 0;
1979 } else if (chip->sub_type == QPNP_PWM_MODE_ONLY_SUB_TYPE) {
1980 if (!(force_pwm_size == QPNP_PWM_SIZE_6_BIT ||
1981 force_pwm_size == QPNP_PWM_SIZE_9_BIT))
1982 force_pwm_size = 0;
1983 } else if (pwm_config->supported_sizes == QPNP_PWM_SIZE_6_7_9_BIT) {
1984 if (!(force_pwm_size == QPNP_PWM_SIZE_6_BIT ||
1985 force_pwm_size == QPNP_PWM_SIZE_7_BIT ||
1986 force_pwm_size == QPNP_PWM_SIZE_9_BIT))
1987 force_pwm_size = 0;
1988 }
1989
1990 pwm_config->force_pwm_size = force_pwm_size;
1991
1992
1993 prop = of_get_address_by_name(pdev->dev.of_node, QPNP_LPG_CHANNEL_BASE,
1994 0, 0);
1995 if (!prop) {
1996 dev_err(&pdev->dev, "Couldnt find channel's base addr rc %d\n",
1997 rc);
1998 return rc;
1999 }
2000 lpg_config->base_addr = be32_to_cpu(*prop);
2001
2002 rc = qpnp_lpg_get_rev_subtype(chip);
2003 if (rc)
2004 return rc;
2005
2006 prop = of_get_address_by_name(pdev->dev.of_node, QPNP_LPG_LUT_BASE,
2007 0, 0);
2008 if (!prop) {
2009 chip->flags |= QPNP_PWM_LUT_NOT_SUPPORTED;
2010 } else {
2011 lpg_config->lut_base_addr = be32_to_cpu(*prop);
2012 rc = of_property_read_u32(of_node, "qcom,lpg-lut-size", &size);
2013 if (rc < 0) {
2014 dev_err(&pdev->dev, "Error reading qcom,lpg-lut-size, rc=%d\n",
2015 rc);
2016 return rc;
2017 }
2018
2019 /*
2020 * Each entry of LUT is of 2 bytes for generic LUT and of 1 byte
2021 * for KPDBL/GLED LUT.
2022 */
2023 lpg_config->lut_size = size >> 1;
2024 lut_entry_size = sizeof(u16);
2025
2026 if (pwm_config->supported_sizes == QPNP_PWM_SIZE_7_8_BIT) {
2027 lpg_config->lut_size = size;
2028 lut_entry_size = sizeof(u8);
2029 }
2030
2031 lut_config->duty_pct_list = kcalloc(lpg_config->lut_size,
2032 lut_entry_size, GFP_KERNEL);
2033 if (!lut_config->duty_pct_list)
2034 return -ENOMEM;
2035
2036 rc = of_property_read_u32(of_node, "qcom,ramp-index",
2037 &lut_config->ramp_index);
2038 if (rc) {
2039 pr_err("Missing LPG qcom,ramp-index property\n");
2040 kfree(lut_config->duty_pct_list);
2041 return rc;
2042 }
2043 }
2044
2045 rc = of_property_read_u32(of_node, "qcom,dtest-line",
2046 &chip->dtest_line);
2047 if (rc) {
2048 chip->in_test_mode = 0;
2049 } else {
2050 rc = of_property_read_u32(of_node, "qcom,dtest-output",
2051 &chip->dtest_output);
2052 if (rc) {
2053 pr_err("Missing DTEST output configuration\n");
2054 return rc;
2055 }
2056 chip->in_test_mode = 1;
2057 }
2058
2059 if (chip->in_test_mode) {
2060 if ((chip->sub_type == QPNP_PWM_MODE_ONLY_SUB_TYPE) &&
2061 (chip->dtest_line > QPNP_PWM_DTEST_LINE_MAX ||
2062 chip->dtest_output > QPNP_PWM_DTEST_OUTPUT_MAX)) {
2063 pr_err("DTEST line/output values are improper for PWM channel %d\n",
2064 chip->channel_id);
2065 return -EINVAL;
2066 } else if (chip->dtest_line > QPNP_LPG_DTEST_LINE_MAX ||
2067 chip->dtest_output > QPNP_LPG_DTEST_OUTPUT_MAX) {
2068 pr_err("DTEST line/output values are improper for LPG channel %d\n",
2069 chip->channel_id);
2070 return -EINVAL;
2071 }
2072 }
2073
2074 for_each_child_of_node(of_node, node) {
2075 rc = of_property_read_string(node, "label", &label);
2076 if (rc) {
2077 dev_err(&pdev->dev, "%s: Missing label property\n",
2078 __func__);
2079 goto out;
2080 }
2081 if (!strcmp(label, "pwm")) {
2082 rc = qpnp_parse_pwm_dt_config(node, of_node, chip);
2083 if (rc)
2084 goto out;
2085 found_pwm_subnode = 1;
2086 } else if (!strcmp(label, "lpg") &&
2087 !(chip->flags & QPNP_PWM_LUT_NOT_SUPPORTED)) {
2088 rc = qpnp_parse_lpg_dt_config(node, of_node, chip);
2089 if (rc)
2090 goto out;
2091 found_lpg_subnode = 1;
2092 } else {
2093 dev_err(&pdev->dev,
2094 "%s: Invalid value for lable prop",
2095 __func__);
2096 }
2097 }
2098
Fenglin Wuc81fb382017-08-14 12:45:21 +08002099 rc = of_property_read_u32(of_node, "qcom,mode-select", &mode);
David Collins8885f792017-01-26 14:36:34 -08002100 if (rc)
2101 goto read_opt_props;
2102
Fenglin Wuc81fb382017-08-14 12:45:21 +08002103 if (mode > PM_PWM_MODE_LPG ||
2104 (mode == PM_PWM_MODE_PWM && found_pwm_subnode == 0) ||
2105 (mode == PM_PWM_MODE_LPG && found_lpg_subnode == 0)) {
David Collins8885f792017-01-26 14:36:34 -08002106 dev_err(&pdev->dev, "%s: Invalid mode select\n", __func__);
2107 rc = -EINVAL;
2108 goto out;
2109 }
2110
Fenglin Wuc81fb382017-08-14 12:45:21 +08002111 chip->pwm_mode = mode;
2112 _pwm_change_mode(chip, mode);
David Collins8885f792017-01-26 14:36:34 -08002113 _pwm_enable(chip);
2114
2115read_opt_props:
2116 /* Initialize optional config parameters from DT if provided */
2117 of_property_read_string(node, "qcom,channel-owner",
2118 &chip->channel_owner);
2119
2120 return 0;
2121
2122out:
2123 kfree(lut_config->duty_pct_list);
2124 return rc;
2125}
2126
2127static struct pwm_ops qpnp_pwm_ops = {
2128 .enable = qpnp_pwm_enable,
2129 .disable = qpnp_pwm_disable,
2130 .config = qpnp_pwm_config,
2131 .free = qpnp_pwm_free,
2132 .owner = THIS_MODULE,
2133};
2134
2135static int qpnp_pwm_probe(struct platform_device *pdev)
2136{
2137 struct qpnp_pwm_chip *pwm_chip;
2138 int rc;
2139
2140 pwm_chip = kzalloc(sizeof(*pwm_chip), GFP_KERNEL);
2141 if (pwm_chip == NULL)
2142 return -ENOMEM;
2143
2144 pwm_chip->regmap = dev_get_regmap(pdev->dev.parent, NULL);
2145 if (!pwm_chip->regmap) {
2146 dev_err(&pdev->dev, "Couldn't get parent's regmap\n");
2147 return -EINVAL;
2148 }
2149
2150 spin_lock_init(&pwm_chip->lpg_lock);
2151
2152 pwm_chip->pdev = pdev;
2153 dev_set_drvdata(&pdev->dev, pwm_chip);
2154
2155 rc = qpnp_parse_dt_config(pdev, pwm_chip);
2156
2157 if (rc) {
2158 pr_err("Failed parsing DT parameters, rc=%d\n", rc);
2159 goto failed_config;
2160 }
2161
2162 pwm_chip->chip.dev = &pdev->dev;
2163 pwm_chip->chip.ops = &qpnp_pwm_ops;
2164 pwm_chip->chip.base = -1;
2165 pwm_chip->chip.npwm = 1;
2166
2167 rc = pwmchip_add(&pwm_chip->chip);
2168 if (rc < 0) {
2169 pr_err("pwmchip_add() failed: %d\n", rc);
2170 goto failed_insert;
2171 }
2172
2173 if (pwm_chip->channel_owner)
2174 pwm_chip->chip.pwms[0].label = pwm_chip->channel_owner;
2175
2176 pr_debug("PWM device channel:%d probed successfully\n",
2177 pwm_chip->channel_id);
2178 return 0;
2179
2180failed_insert:
2181 kfree(pwm_chip->lpg_config.lut_config.duty_pct_list);
2182failed_config:
2183 dev_set_drvdata(&pdev->dev, NULL);
2184 kfree(pwm_chip);
2185 return rc;
2186}
2187
2188static int qpnp_pwm_remove(struct platform_device *pdev)
2189{
2190 struct qpnp_pwm_chip *pwm_chip;
2191 struct qpnp_lpg_config *lpg_config;
2192
2193 pwm_chip = dev_get_drvdata(&pdev->dev);
2194
2195 dev_set_drvdata(&pdev->dev, NULL);
2196
2197 if (pwm_chip) {
2198 lpg_config = &pwm_chip->lpg_config;
2199 pwmchip_remove(&pwm_chip->chip);
2200 kfree(lpg_config->lut_config.duty_pct_list);
2201 kfree(pwm_chip);
2202 }
2203
2204 return 0;
2205}
2206
2207static const struct of_device_id spmi_match_table[] = {
2208 { .compatible = QPNP_LPG_DRIVER_NAME, },
2209 {}
2210};
2211
2212static const struct platform_device_id qpnp_lpg_id[] = {
2213 { QPNP_LPG_DRIVER_NAME, 0 },
2214 { }
2215};
2216MODULE_DEVICE_TABLE(spmi, qpnp_lpg_id);
2217
2218static struct platform_driver qpnp_lpg_driver = {
2219 .driver = {
2220 .name = QPNP_LPG_DRIVER_NAME,
2221 .of_match_table = spmi_match_table,
2222 .owner = THIS_MODULE,
2223 },
2224 .probe = qpnp_pwm_probe,
2225 .remove = qpnp_pwm_remove,
2226 .id_table = qpnp_lpg_id,
2227};
2228
2229/**
2230 * qpnp_lpg_init() - register spmi driver for qpnp-lpg
2231 */
2232int __init qpnp_lpg_init(void)
2233{
2234 return platform_driver_register(&qpnp_lpg_driver);
2235}
2236
2237static void __exit qpnp_lpg_exit(void)
2238{
2239 platform_driver_unregister(&qpnp_lpg_driver);
2240}
2241
2242MODULE_DESCRIPTION("QPNP PMIC LPG driver");
2243MODULE_LICENSE("GPL v2");
2244MODULE_ALIAS("platform:" QPNP_LPG_DRIVER_NAME);
2245
2246subsys_initcall(qpnp_lpg_init);
2247module_exit(qpnp_lpg_exit);