summaryrefslogtreecommitdiff
path: root/include/linux/qpnp/pwm.h
blob: 782b7849da4a3ca77cef5bcdbe03354f427dc904 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/* Copyright (c) 2012-2016, The Linux Foundation. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 and
 * only version 2 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

#ifndef __QPNP_PWM_H__
#define __QPNP_PWM_H__

#include <linux/pwm.h>

/* usec: 19.2M, n=6, m=0, pre=2 */
#define PM_PWM_PERIOD_MIN			7
/* 1K, n=9, m=7, pre=6 */
#define PM_PWM_PERIOD_MAX			(384 * USEC_PER_SEC)
#define PM_PWM_LUT_RAMP_STEP_TIME_MAX		499
#define PM_PWM_MAX_PAUSE_CNT			8191
/*
 * Formula from HSID,
 * pause_time (hi/lo) = (pause_code - 1)*(duty_ms)
 */
#define PM_PWM_LUT_PAUSE_MAX \
	((PM_PWM_MAX_PAUSE_CNT - 1) * PM_PWM_LUT_RAMP_STEP_TIME_MAX) /* ms */

/* Flags for Look Up Table */
#define PM_PWM_LUT_LOOP			0x01
#define PM_PWM_LUT_RAMP_UP		0x02
#define PM_PWM_LUT_REVERSE		0x04
#define PM_PWM_LUT_PAUSE_HI_EN		0x08
#define PM_PWM_LUT_PAUSE_LO_EN		0x10

#define PM_PWM_LUT_NO_TABLE		0x20
#define PM_PWM_LUT_USE_RAW_VALUE	0x40

/*
 * PWM frequency/period control
 *
 * PWM Frequency = ClockFrequency / (N * T)
 *   or
 * PWM Period = Clock Period * (N * T)
 *   where
 * N = 2^9 or 2^6 for 9-bit or 6-bit PWM size
 * T = Pre-divide * 2^m, m = 0..7 (exponent)
 */

/*
 * enum pm_pwm_size - PWM bit mode selection
 * %PM_PWM_SIZE_6BIT - Select 6 bit mode; 64 levels
 * %PM_PWM_SIZE_9BIT - Select 9 bit mode; 512 levels
 */
enum pm_pwm_size {
	PM_PWM_SIZE_6BIT =	6,
	PM_PWM_SIZE_9BIT =	9,
};

/*
 * enum pm_pwm_clk - PWM clock selection
 * %PM_PWM_CLK_1KHZ - 1KHz clock
 * %PM_PWM_CLK_32KHZ - 32KHz clock
 * %PM_PWM_CLK_19P2MHZ - 19.2MHz clock
 * Note: Here 1KHz = 1024Hz
 */
enum pm_pwm_clk {
	PM_PWM_CLK_1KHZ,
	PM_PWM_CLK_32KHZ,
	PM_PWM_CLK_19P2MHZ,
};

/* PWM pre-divider selection */
enum pm_pwm_pre_div {
	PM_PWM_PDIV_2,
	PM_PWM_PDIV_3,
	PM_PWM_PDIV_5,
	PM_PWM_PDIV_6,
};

/*
 * struct pwm_period_config - PWM period configuration
 * @pwm_size: enum pm_pwm_size
 * @clk: enum pm_pwm_clk
 * @pre_div: enum pm_pwm_pre_div
 * @pre_div_exp: exponent of 2 as part of pre-divider: 0..7
 */
struct pwm_period_config {
	enum pm_pwm_size	pwm_size;
	enum pm_pwm_clk		clk;
	enum pm_pwm_pre_div	pre_div;
	int			pre_div_exp;
};

/*
 * struct pwm_duty_cycles - PWM duty cycle info
 * duty_pcts - pointer to an array of duty percentage for a pwm period
 * num_duty_pcts - total entries in duty_pcts array
 * duty_ms - duty cycle time in ms
 * start_idx - index in the LUT
 */
struct pwm_duty_cycles {
	int *duty_pcts;
	int num_duty_pcts;
	int duty_ms;
	int start_idx;
};

/*
 * enum pm_pwm_mode - PWM mode selection
 * %PM_PWM_MODE_PWM - Select PWM mode
 * %PM_PWM_MODE_LPG - Select LPG mode
 */
enum pm_pwm_mode {
	PM_PWM_MODE_PWM,
	PM_PWM_MODE_LPG,
};

/*
 * lut_params: Lookup table (LUT) parameters
 * @start_idx: start index in lookup table from 0 to MAX-1
 * @idx_len: number of index
 * @pause_lo: pause time in millisecond at low index
 * @pause_hi: pause time in millisecond at high index
 * @ramp_step_ms: time before loading next LUT pattern in millisecond
 * @flags: control flags
 */
struct lut_params {
	int start_idx;
	int idx_len;
	int lut_pause_hi;
	int lut_pause_lo;
	int ramp_step_ms;
	int flags;
};

#if IS_ENABLED(CONFIG_PWM_QPNP)
int pwm_config_period(struct pwm_device *pwm,
			     struct pwm_period_config *pwm_p);

int pwm_config_pwm_value(struct pwm_device *pwm, int pwm_value);


int pwm_change_mode(struct pwm_device *pwm, enum pm_pwm_mode mode);


int pwm_lut_config(struct pwm_device *pwm, int period_us,
		int duty_pct[], struct lut_params lut_params);

/*
 * support microsecond level configuration
 */
int pwm_config_us(struct pwm_device *pwm,
		int duty_us, int period_us);

/*
 * synchronized enable of multiple pwm instances
 */
int pwm_enable_synchronized(struct pwm_device **pwms, size_t num);

#else
static inline int pwm_config_period(struct pwm_device *pwm,
			     struct pwm_period_config *pwm_p)
{
	return -EINVAL;
}

static inline int pwm_config_pwm_value(struct pwm_device *pwm, int pwm_value)
{
	return -EINVAL;
}

static inline int pwm_change_mode(struct pwm_device *pwm, enum pm_pwm_mode mode)
{
	return -EINVAL;
}

static inline int pwm_lut_config(struct pwm_device *pwm, int period_us,
		int duty_pct[], struct lut_params lut_params)
{
	return -EINVAL;
}

static inline int pwm_config_us(struct pwm_device *pwm,
		int duty_us, int period_us)
{
	return -EINVAL;
}

static inline int pwm_enable_synchronized(struct pwm_device **pwms, size_t num)
{
	return -EINVAL;
}
#endif

/* Standard APIs supported */
/*
 * pwm_request - request a PWM device
 * @pwm_id: PWM id or channel
 * @label: the label to identify the user
 */

/*
 * pwm_free - free a PWM device
 * @pwm: the PWM device
 */

/*
 * pwm_config - change a PWM device configuration
 * @pwm: the PWM device
 * @period_ns: period in nanosecond
 * @duty_ns: duty cycle in nanosecond
 */

/*
 * pwm_enable - start a PWM output toggling
 * @pwm: the PWM device
 */

/*
 * pwm_disable - stop a PWM output toggling
 * @pwm: the PWM device
 */

#endif /* __QPNP_PWM_H__ */