blob: 72fe459f02676b3f561137b6604be231bd37ec14 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Russell King1a189b92008-04-13 21:41:55 +01002#ifndef __LINUX_PWM_H
3#define __LINUX_PWM_H
4
Tushar Behera0bcf1682012-09-12 15:31:46 +05305#include <linux/err.h>
Jonathan Richardsond1cd2142015-10-16 17:40:58 -07006#include <linux/mutex.h>
Thierry Reding7299ab72011-12-14 11:10:32 +01007#include <linux/of.h>
Greg Kroah-Hartman37485a32021-07-02 15:58:26 +02008#include <linux/android_kabi.h>
Thierry Reding7299ab72011-12-14 11:10:32 +01009
Lee Jones3a3d1a42016-06-08 10:21:23 +010010struct pwm_capture;
Thierry Reding62099ab2012-03-26 09:31:48 +020011struct seq_file;
Lee Jones3a3d1a42016-06-08 10:21:23 +010012
Sascha Hauer0c2498f2011-01-28 09:40:40 +010013struct pwm_chip;
14
Philip, Avinash0aa0869c2012-07-24 19:35:32 +053015/**
16 * enum pwm_polarity - polarity of a PWM signal
17 * @PWM_POLARITY_NORMAL: a high signal for the duration of the duty-
18 * cycle, followed by a low signal for the remainder of the pulse
19 * period
20 * @PWM_POLARITY_INVERSED: a low signal for the duration of the duty-
21 * cycle, followed by a high signal for the remainder of the pulse
22 * period
23 */
24enum pwm_polarity {
25 PWM_POLARITY_NORMAL,
26 PWM_POLARITY_INVERSED,
27};
28
Boris Brezillone39c0df2016-04-14 21:17:21 +020029/**
30 * struct pwm_args - board-dependent PWM arguments
31 * @period: reference period
32 * @polarity: reference polarity
33 *
34 * This structure describes board-dependent arguments attached to a PWM
35 * device. These arguments are usually retrieved from the PWM lookup table or
36 * device tree.
37 *
38 * Do not confuse this with the PWM state: PWM arguments represent the initial
39 * configuration that users want to use on this PWM device rather than the
40 * current PWM hardware state.
41 */
42struct pwm_args {
Guru Das Srinagesha9d887d2020-06-02 15:31:16 -070043 u64 period;
Boris Brezillone39c0df2016-04-14 21:17:21 +020044 enum pwm_polarity polarity;
45};
46
Thierry Redingf051c462011-12-14 11:12:23 +010047enum {
48 PWMF_REQUESTED = 1 << 0,
Boris Brezillon09a7e4a2016-04-14 21:17:39 +020049 PWMF_EXPORTED = 1 << 1,
Thierry Redingf051c462011-12-14 11:12:23 +010050};
51
Fenglin Wu106a4b82017-12-04 09:56:51 +080052/**
53 * enum pwm_output_type - output type of the PWM signal
54 * @PWM_OUTPUT_FIXED: PWM output is fixed until a change request
55 * @PWM_OUTPUT_MODULATED: PWM output is modulated in hardware
56 * autonomously with a predefined pattern
57 */
58enum pwm_output_type {
59 PWM_OUTPUT_FIXED = 1 << 0,
60 PWM_OUTPUT_MODULATED = 1 << 1,
61};
62
Boris Brezillon43a276b2016-04-14 21:17:38 +020063/*
64 * struct pwm_state - state of a PWM channel
65 * @period: PWM period (in nanoseconds)
66 * @duty_cycle: PWM duty cycle (in nanoseconds)
67 * @polarity: PWM polarity
Boris Brezillon09a7e4a2016-04-14 21:17:39 +020068 * @enabled: PWM enabled status
Boris Brezillon43a276b2016-04-14 21:17:38 +020069 */
70struct pwm_state {
Guru Das Srinagesha9d887d2020-06-02 15:31:16 -070071 u64 period;
72 u64 duty_cycle;
Boris Brezillon43a276b2016-04-14 21:17:38 +020073 enum pwm_polarity polarity;
Fenglin Wu106a4b82017-12-04 09:56:51 +080074 enum pwm_output_type output_type;
Boris Brezillon09a7e4a2016-04-14 21:17:39 +020075 bool enabled;
Boris Brezillon43a276b2016-04-14 21:17:38 +020076};
77
Thierry Reding04883802015-07-27 11:58:32 +020078/**
79 * struct pwm_device - PWM channel object
80 * @label: name of the PWM device
81 * @flags: flags associated with the PWM device
82 * @hwpwm: per-chip relative index of the PWM device
83 * @pwm: global index of the PWM device
84 * @chip: PWM chip providing this PWM device
85 * @chip_data: chip-private data associated with the PWM device
Boris Brezillone39c0df2016-04-14 21:17:21 +020086 * @args: PWM arguments
Uwe Kleine-König3ad1f3a2020-02-10 22:35:18 +010087 * @state: last applied state
88 * @last: last implemented state (for PWM_DEBUG)
Thierry Reding04883802015-07-27 11:58:32 +020089 */
Thierry Redingf051c462011-12-14 11:12:23 +010090struct pwm_device {
Thierry Reding6bc70642015-07-27 11:57:28 +020091 const char *label;
92 unsigned long flags;
93 unsigned int hwpwm;
94 unsigned int pwm;
95 struct pwm_chip *chip;
96 void *chip_data;
Thierry Redingf051c462011-12-14 11:12:23 +010097
Boris Brezillone39c0df2016-04-14 21:17:21 +020098 struct pwm_args args;
Boris Brezillon43a276b2016-04-14 21:17:38 +020099 struct pwm_state state;
Uwe Kleine-König3ad1f3a2020-02-10 22:35:18 +0100100 struct pwm_state last;
Greg Kroah-Hartman37485a32021-07-02 15:58:26 +0200101
102 ANDROID_KABI_RESERVE(1);
Thierry Redingf051c462011-12-14 11:12:23 +0100103};
104
Boris Brezillon43a276b2016-04-14 21:17:38 +0200105/**
106 * pwm_get_state() - retrieve the current PWM state
107 * @pwm: PWM device
108 * @state: state to fill with the current PWM state
109 */
110static inline void pwm_get_state(const struct pwm_device *pwm,
111 struct pwm_state *state)
112{
113 *state = pwm->state;
114}
115
Boris Brezillon5c312522015-07-01 10:21:47 +0200116static inline bool pwm_is_enabled(const struct pwm_device *pwm)
117{
Boris Brezillon09a7e4a2016-04-14 21:17:39 +0200118 struct pwm_state state;
119
120 pwm_get_state(pwm, &state);
121
122 return state.enabled;
Boris Brezillon5c312522015-07-01 10:21:47 +0200123}
124
Guru Das Srinagesha9d887d2020-06-02 15:31:16 -0700125static inline void pwm_set_period(struct pwm_device *pwm, u64 period)
Thierry Redingf051c462011-12-14 11:12:23 +0100126{
127 if (pwm)
Boris Brezillon43a276b2016-04-14 21:17:38 +0200128 pwm->state.period = period;
Thierry Redingf051c462011-12-14 11:12:23 +0100129}
130
Guru Das Srinagesha9d887d2020-06-02 15:31:16 -0700131static inline u64 pwm_get_period(const struct pwm_device *pwm)
Thierry Redingf051c462011-12-14 11:12:23 +0100132{
Boris Brezillon43a276b2016-04-14 21:17:38 +0200133 struct pwm_state state;
134
135 pwm_get_state(pwm, &state);
136
137 return state.period;
Thierry Redingf051c462011-12-14 11:12:23 +0100138}
139
H Hartley Sweeten76abbdde2013-06-11 10:38:59 -0700140static inline void pwm_set_duty_cycle(struct pwm_device *pwm, unsigned int duty)
141{
142 if (pwm)
Boris Brezillon43a276b2016-04-14 21:17:38 +0200143 pwm->state.duty_cycle = duty;
H Hartley Sweeten76abbdde2013-06-11 10:38:59 -0700144}
145
Guru Das Srinagesha9d887d2020-06-02 15:31:16 -0700146static inline u64 pwm_get_duty_cycle(const struct pwm_device *pwm)
H Hartley Sweeten76abbdde2013-06-11 10:38:59 -0700147{
Boris Brezillon43a276b2016-04-14 21:17:38 +0200148 struct pwm_state state;
149
150 pwm_get_state(pwm, &state);
151
152 return state.duty_cycle;
H Hartley Sweeten76abbdde2013-06-11 10:38:59 -0700153}
154
Boris Brezillon011e7632015-07-01 10:21:49 +0200155static inline enum pwm_polarity pwm_get_polarity(const struct pwm_device *pwm)
156{
Boris Brezillon43a276b2016-04-14 21:17:38 +0200157 struct pwm_state state;
158
159 pwm_get_state(pwm, &state);
160
161 return state.polarity;
Boris Brezillon011e7632015-07-01 10:21:49 +0200162}
163
Fenglin Wu106a4b82017-12-04 09:56:51 +0800164static inline enum pwm_output_type pwm_get_output_type(
165 const struct pwm_device *pwm)
166{
167 struct pwm_state state;
168
169 pwm_get_state(pwm, &state);
170
171 return state.output_type;
172}
173
Boris Brezillone39c0df2016-04-14 21:17:21 +0200174static inline void pwm_get_args(const struct pwm_device *pwm,
175 struct pwm_args *args)
176{
177 *args = pwm->args;
178}
179
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100180/**
Boris Brezillona6a0dbb2016-06-14 11:13:09 +0200181 * pwm_init_state() - prepare a new state to be applied with pwm_apply_state()
182 * @pwm: PWM device
183 * @state: state to fill with the prepared PWM state
184 *
185 * This functions prepares a state that can later be tweaked and applied
186 * to the PWM device with pwm_apply_state(). This is a convenient function
187 * that first retrieves the current PWM state and the replaces the period
188 * and polarity fields with the reference values defined in pwm->args.
189 * Once the function returns, you can adjust the ->enabled and ->duty_cycle
190 * fields according to your needs before calling pwm_apply_state().
191 *
192 * ->duty_cycle is initially set to zero to avoid cases where the current
193 * ->duty_cycle value exceed the pwm_args->period one, which would trigger
194 * an error if the user calls pwm_apply_state() without adjusting ->duty_cycle
195 * first.
196 */
197static inline void pwm_init_state(const struct pwm_device *pwm,
198 struct pwm_state *state)
199{
200 struct pwm_args args;
201
202 /* First get the current state. */
203 pwm_get_state(pwm, state);
204
205 /* Then fill it with the reference config */
206 pwm_get_args(pwm, &args);
207
208 state->period = args.period;
209 state->polarity = args.polarity;
210 state->duty_cycle = 0;
211}
212
213/**
Boris Brezillonf6f3bdd2016-06-14 11:13:10 +0200214 * pwm_get_relative_duty_cycle() - Get a relative duty cycle value
215 * @state: PWM state to extract the duty cycle from
216 * @scale: target scale of the relative duty cycle
217 *
218 * This functions converts the absolute duty cycle stored in @state (expressed
219 * in nanosecond) into a value relative to the period.
220 *
221 * For example if you want to get the duty_cycle expressed in percent, call:
222 *
223 * pwm_get_state(pwm, &state);
224 * duty = pwm_get_relative_duty_cycle(&state, 100);
225 */
226static inline unsigned int
227pwm_get_relative_duty_cycle(const struct pwm_state *state, unsigned int scale)
228{
229 if (!state->period)
230 return 0;
231
232 return DIV_ROUND_CLOSEST_ULL((u64)state->duty_cycle * scale,
233 state->period);
234}
235
236/**
237 * pwm_set_relative_duty_cycle() - Set a relative duty cycle value
238 * @state: PWM state to fill
239 * @duty_cycle: relative duty cycle value
240 * @scale: scale in which @duty_cycle is expressed
241 *
242 * This functions converts a relative into an absolute duty cycle (expressed
243 * in nanoseconds), and puts the result in state->duty_cycle.
244 *
245 * For example if you want to configure a 50% duty cycle, call:
246 *
247 * pwm_init_state(pwm, &state);
248 * pwm_set_relative_duty_cycle(&state, 50, 100);
249 * pwm_apply_state(pwm, &state);
250 *
251 * This functions returns -EINVAL if @duty_cycle and/or @scale are
252 * inconsistent (@scale == 0 or @duty_cycle > @scale).
253 */
254static inline int
255pwm_set_relative_duty_cycle(struct pwm_state *state, unsigned int duty_cycle,
256 unsigned int scale)
257{
258 if (!scale || duty_cycle > scale)
259 return -EINVAL;
260
261 state->duty_cycle = DIV_ROUND_CLOSEST_ULL((u64)duty_cycle *
262 state->period,
263 scale);
264
265 return 0;
266}
267
268/**
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100269 * struct pwm_ops - PWM controller operations
270 * @request: optional hook for requesting a PWM
271 * @free: optional hook for freeing a PWM
Lee Jones3a3d1a42016-06-08 10:21:23 +0100272 * @capture: capture and report PWM signal
Rasmus Villemoes27938fd2019-10-04 15:32:07 +0200273 * @apply: atomically apply a new PWM config
Boris Brezillon15fa8a432016-04-14 21:17:40 +0200274 * @get_state: get the current PWM state. This function is only
275 * called once per PWM device when the PWM chip is
276 * registered.
Fenglin Wu106a4b82017-12-04 09:56:51 +0800277 * @get_output_type_supported: get the supported output type of this PWM
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100278 * @owner: helps prevent removal of modules exporting active PWMs
Uwe Kleine-König5d0a4c12019-01-07 20:49:41 +0100279 * @config: configure duty cycles and period length for this PWM
280 * @set_polarity: configure the polarity of this PWM
281 * @enable: enable PWM output toggling
282 * @disable: disable PWM output toggling
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100283 */
284struct pwm_ops {
Thierry Reding6bc70642015-07-27 11:57:28 +0200285 int (*request)(struct pwm_chip *chip, struct pwm_device *pwm);
286 void (*free)(struct pwm_chip *chip, struct pwm_device *pwm);
Lee Jones3a3d1a42016-06-08 10:21:23 +0100287 int (*capture)(struct pwm_chip *chip, struct pwm_device *pwm,
288 struct pwm_capture *result, unsigned long timeout);
Boris Brezillon5ec803e2016-04-14 21:17:41 +0200289 int (*apply)(struct pwm_chip *chip, struct pwm_device *pwm,
Uwe Kleine-König71523d12019-08-24 17:37:07 +0200290 const struct pwm_state *state);
Boris Brezillon15fa8a432016-04-14 21:17:40 +0200291 void (*get_state)(struct pwm_chip *chip, struct pwm_device *pwm,
292 struct pwm_state *state);
Fenglin Wu106a4b82017-12-04 09:56:51 +0800293 int (*get_output_type_supported)(struct pwm_chip *chip,
294 struct pwm_device *pwm);
Thierry Reding6bc70642015-07-27 11:57:28 +0200295 struct module *owner;
Uwe Kleine-König5d0a4c12019-01-07 20:49:41 +0100296
297 /* Only used by legacy drivers */
298 int (*config)(struct pwm_chip *chip, struct pwm_device *pwm,
299 int duty_ns, int period_ns);
300 int (*set_polarity)(struct pwm_chip *chip, struct pwm_device *pwm,
301 enum pwm_polarity polarity);
302 int (*enable)(struct pwm_chip *chip, struct pwm_device *pwm);
303 void (*disable)(struct pwm_chip *chip, struct pwm_device *pwm);
Greg Kroah-Hartman37485a32021-07-02 15:58:26 +0200304
305 ANDROID_KABI_RESERVE(1);
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100306};
307
308/**
Thierry Redingf051c462011-12-14 11:12:23 +0100309 * struct pwm_chip - abstract a PWM controller
310 * @dev: device providing the PWMs
Thierry Redingf051c462011-12-14 11:12:23 +0100311 * @ops: callbacks for this PWM controller
312 * @base: number of first PWM controlled by this chip
313 * @npwm: number of PWMs controlled by this chip
Thierry Reding04883802015-07-27 11:58:32 +0200314 * @of_xlate: request a PWM device given a device tree PWM specifier
315 * @of_pwm_n_cells: number of cells expected in the device tree PWM specifier
Uwe Kleine-König5d0a4c12019-01-07 20:49:41 +0100316 * @list: list node for internal use
317 * @pwms: array of PWM devices allocated by the framework
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100318 */
319struct pwm_chip {
Thierry Reding6bc70642015-07-27 11:57:28 +0200320 struct device *dev;
Thierry Reding6bc70642015-07-27 11:57:28 +0200321 const struct pwm_ops *ops;
322 int base;
323 unsigned int npwm;
Thierry Redingf051c462011-12-14 11:12:23 +0100324
Thierry Reding6bc70642015-07-27 11:57:28 +0200325 struct pwm_device * (*of_xlate)(struct pwm_chip *pc,
326 const struct of_phandle_args *args);
327 unsigned int of_pwm_n_cells;
Uwe Kleine-König5d0a4c12019-01-07 20:49:41 +0100328
329 /* only used internally by the PWM framework */
330 struct list_head list;
331 struct pwm_device *pwms;
Greg Kroah-Hartman37485a32021-07-02 15:58:26 +0200332
333 ANDROID_KABI_RESERVE(1);
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100334};
335
Lee Jones3a3d1a42016-06-08 10:21:23 +0100336/**
337 * struct pwm_capture - PWM capture data
338 * @period: period of the PWM signal (in nanoseconds)
339 * @duty_cycle: duty cycle of the PWM signal (in nanoseconds)
340 */
341struct pwm_capture {
Greg Kroah-Hartmanfd0cd972020-06-25 15:48:52 +0200342 unsigned int period;
343 unsigned int duty_cycle;
Lee Jones3a3d1a42016-06-08 10:21:23 +0100344};
345
Tushar Behera0bcf1682012-09-12 15:31:46 +0530346#if IS_ENABLED(CONFIG_PWM)
Boris Brezillon5ec803e2016-04-14 21:17:41 +0200347/* PWM user APIs */
348struct pwm_device *pwm_request(int pwm_id, const char *label);
349void pwm_free(struct pwm_device *pwm);
Uwe Kleine-König71523d12019-08-24 17:37:07 +0200350int pwm_apply_state(struct pwm_device *pwm, const struct pwm_state *state);
Boris Brezillon5ec803e2016-04-14 21:17:41 +0200351int pwm_adjust_config(struct pwm_device *pwm);
352
353/**
Fenglin Wu106a4b82017-12-04 09:56:51 +0800354 * pwm_get_output_type_supported() - obtain output type of a PWM device.
355 * @pwm: PWM device
356 *
357 * Returns: output type supported by the PWM device
358 */
359static inline int pwm_get_output_type_supported(struct pwm_device *pwm)
360{
361 if (!pwm)
362 return -EINVAL;
363
364 if (pwm->chip->ops->get_output_type_supported)
365 return pwm->chip->ops->get_output_type_supported(pwm->chip,
366 pwm);
367
368 return PWM_OUTPUT_FIXED;
369}
370
371/**
Boris Brezillon5ec803e2016-04-14 21:17:41 +0200372 * pwm_config() - change a PWM device configuration
373 * @pwm: PWM device
374 * @duty_ns: "on" time (in nanoseconds)
375 * @period_ns: duration (in nanoseconds) of one cycle
376 *
377 * Returns: 0 on success or a negative error code on failure.
378 */
379static inline int pwm_config(struct pwm_device *pwm, int duty_ns,
380 int period_ns)
381{
382 struct pwm_state state;
383
384 if (!pwm)
385 return -EINVAL;
386
Brian Norrisef2bf492016-05-27 09:45:49 -0700387 if (duty_ns < 0 || period_ns < 0)
388 return -EINVAL;
389
Boris Brezillon5ec803e2016-04-14 21:17:41 +0200390 pwm_get_state(pwm, &state);
391 if (state.duty_cycle == duty_ns && state.period == period_ns)
392 return 0;
393
394 state.duty_cycle = duty_ns;
395 state.period = period_ns;
396 return pwm_apply_state(pwm, &state);
397}
398
399/**
Boris Brezillon5ec803e2016-04-14 21:17:41 +0200400 * pwm_enable() - start a PWM output toggling
401 * @pwm: PWM device
402 *
403 * Returns: 0 on success or a negative error code on failure.
404 */
405static inline int pwm_enable(struct pwm_device *pwm)
406{
407 struct pwm_state state;
408
409 if (!pwm)
410 return -EINVAL;
411
412 pwm_get_state(pwm, &state);
413 if (state.enabled)
414 return 0;
415
416 state.enabled = true;
417 return pwm_apply_state(pwm, &state);
418}
419
420/**
421 * pwm_disable() - stop a PWM output toggling
422 * @pwm: PWM device
423 */
424static inline void pwm_disable(struct pwm_device *pwm)
425{
426 struct pwm_state state;
427
428 if (!pwm)
429 return;
430
431 pwm_get_state(pwm, &state);
432 if (!state.enabled)
433 return;
434
435 state.enabled = false;
436 pwm_apply_state(pwm, &state);
437}
438
Boris Brezillon5ec803e2016-04-14 21:17:41 +0200439/* PWM provider APIs */
Lee Jones3a3d1a42016-06-08 10:21:23 +0100440int pwm_capture(struct pwm_device *pwm, struct pwm_capture *result,
441 unsigned long timeout);
Thierry Redingf051c462011-12-14 11:12:23 +0100442int pwm_set_chip_data(struct pwm_device *pwm, void *data);
443void *pwm_get_chip_data(struct pwm_device *pwm);
444
Tim Krygerb6a00fa2015-05-26 13:08:16 -0700445int pwmchip_add_with_polarity(struct pwm_chip *chip,
446 enum pwm_polarity polarity);
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100447int pwmchip_add(struct pwm_chip *chip);
448int pwmchip_remove(struct pwm_chip *chip);
Thierry Redingf051c462011-12-14 11:12:23 +0100449struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip,
450 unsigned int index,
451 const char *label);
Thierry Reding8138d2d2012-03-26 08:42:48 +0200452
Philip, Avinash83af2402012-11-21 13:10:44 +0530453struct pwm_device *of_pwm_xlate_with_flags(struct pwm_chip *pc,
454 const struct of_phandle_args *args);
455
Peter Ujfalusid4c0c472012-12-21 01:43:57 -0800456struct pwm_device *pwm_get(struct device *dev, const char *con_id);
Fabrice Gasnierb2c200e2019-04-18 11:37:47 +0200457struct pwm_device *of_pwm_get(struct device *dev, struct device_node *np,
458 const char *con_id);
Thierry Reding8138d2d2012-03-26 08:42:48 +0200459void pwm_put(struct pwm_device *pwm);
460
Peter Ujfalusid4c0c472012-12-21 01:43:57 -0800461struct pwm_device *devm_pwm_get(struct device *dev, const char *con_id);
Peter Ujfalusi261a5ed2012-12-21 01:43:59 -0800462struct pwm_device *devm_of_pwm_get(struct device *dev, struct device_node *np,
463 const char *con_id);
Nikolaus Voss4a6ef8e2019-06-12 10:36:07 +0200464struct pwm_device *devm_fwnode_pwm_get(struct device *dev,
465 struct fwnode_handle *fwnode,
466 const char *con_id);
Alexandre Courbot63543162012-08-01 19:20:58 +0900467void devm_pwm_put(struct device *dev, struct pwm_device *pwm);
Tushar Behera0bcf1682012-09-12 15:31:46 +0530468#else
Boris Brezillon5ec803e2016-04-14 21:17:41 +0200469static inline struct pwm_device *pwm_request(int pwm_id, const char *label)
470{
471 return ERR_PTR(-ENODEV);
472}
473
474static inline void pwm_free(struct pwm_device *pwm)
475{
476}
477
478static inline int pwm_apply_state(struct pwm_device *pwm,
479 const struct pwm_state *state)
480{
481 return -ENOTSUPP;
482}
483
484static inline int pwm_adjust_config(struct pwm_device *pwm)
485{
486 return -ENOTSUPP;
487}
488
Fenglin Wu106a4b82017-12-04 09:56:51 +0800489static inline int pwm_get_output_type_supported(struct pwm_device *pwm)
490{
491 return -EINVAL;
492}
493
Boris Brezillon5ec803e2016-04-14 21:17:41 +0200494static inline int pwm_config(struct pwm_device *pwm, int duty_ns,
495 int period_ns)
496{
497 return -EINVAL;
498}
499
Lee Jones3a3d1a42016-06-08 10:21:23 +0100500static inline int pwm_capture(struct pwm_device *pwm,
501 struct pwm_capture *result,
502 unsigned long timeout)
503{
504 return -EINVAL;
505}
506
Boris Brezillon5ec803e2016-04-14 21:17:41 +0200507static inline int pwm_enable(struct pwm_device *pwm)
508{
509 return -EINVAL;
510}
511
512static inline void pwm_disable(struct pwm_device *pwm)
513{
514}
515
Tushar Behera0bcf1682012-09-12 15:31:46 +0530516static inline int pwm_set_chip_data(struct pwm_device *pwm, void *data)
517{
518 return -EINVAL;
519}
520
521static inline void *pwm_get_chip_data(struct pwm_device *pwm)
522{
523 return NULL;
524}
525
526static inline int pwmchip_add(struct pwm_chip *chip)
527{
528 return -EINVAL;
529}
530
Tim Krygerb6a00fa2015-05-26 13:08:16 -0700531static inline int pwmchip_add_inversed(struct pwm_chip *chip)
532{
533 return -EINVAL;
534}
535
Tushar Behera0bcf1682012-09-12 15:31:46 +0530536static inline int pwmchip_remove(struct pwm_chip *chip)
537{
538 return -EINVAL;
539}
540
541static inline struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip,
542 unsigned int index,
543 const char *label)
544{
545 return ERR_PTR(-ENODEV);
546}
547
548static inline struct pwm_device *pwm_get(struct device *dev,
549 const char *consumer)
550{
551 return ERR_PTR(-ENODEV);
552}
553
Fabrice Gasnierb2c200e2019-04-18 11:37:47 +0200554static inline struct pwm_device *of_pwm_get(struct device *dev,
555 struct device_node *np,
Peter Ujfalusi8eb96122012-12-21 01:43:58 -0800556 const char *con_id)
557{
558 return ERR_PTR(-ENODEV);
559}
560
Tushar Behera0bcf1682012-09-12 15:31:46 +0530561static inline void pwm_put(struct pwm_device *pwm)
562{
563}
564
565static inline struct pwm_device *devm_pwm_get(struct device *dev,
566 const char *consumer)
567{
568 return ERR_PTR(-ENODEV);
569}
570
Peter Ujfalusi261a5ed2012-12-21 01:43:59 -0800571static inline struct pwm_device *devm_of_pwm_get(struct device *dev,
572 struct device_node *np,
573 const char *con_id)
574{
575 return ERR_PTR(-ENODEV);
576}
577
Nikolaus Voss4a6ef8e2019-06-12 10:36:07 +0200578static inline struct pwm_device *
579devm_fwnode_pwm_get(struct device *dev, struct fwnode_handle *fwnode,
580 const char *con_id)
581{
582 return ERR_PTR(-ENODEV);
583}
584
Tushar Behera0bcf1682012-09-12 15:31:46 +0530585static inline void devm_pwm_put(struct device *dev, struct pwm_device *pwm)
586{
587}
588#endif
Alexandre Courbot63543162012-08-01 19:20:58 +0900589
Boris Brezillon5ec803e2016-04-14 21:17:41 +0200590static inline void pwm_apply_args(struct pwm_device *pwm)
591{
Boris Brezillon33cdcee2016-06-22 09:25:14 +0200592 struct pwm_state state = { };
593
Boris Brezillon5ec803e2016-04-14 21:17:41 +0200594 /*
595 * PWM users calling pwm_apply_args() expect to have a fresh config
596 * where the polarity and period are set according to pwm_args info.
597 * The problem is, polarity can only be changed when the PWM is
598 * disabled.
599 *
600 * PWM drivers supporting hardware readout may declare the PWM device
601 * as enabled, and prevent polarity setting, which changes from the
602 * existing behavior, where all PWM devices are declared as disabled
603 * at startup (even if they are actually enabled), thus authorizing
604 * polarity setting.
605 *
Boris Brezillon33cdcee2016-06-22 09:25:14 +0200606 * To fulfill this requirement, we apply a new state which disables
607 * the PWM device and set the reference period and polarity config.
Boris Brezillon5ec803e2016-04-14 21:17:41 +0200608 *
609 * Note that PWM users requiring a smooth handover between the
610 * bootloader and the kernel (like critical regulators controlled by
611 * PWM devices) will have to switch to the atomic API and avoid calling
612 * pwm_apply_args().
613 */
Boris Brezillon33cdcee2016-06-22 09:25:14 +0200614
615 state.enabled = false;
616 state.polarity = pwm->args.polarity;
617 state.period = pwm->args.period;
618
619 pwm_apply_state(pwm, &state);
Boris Brezillon5ec803e2016-04-14 21:17:41 +0200620}
621
Thierry Reding8138d2d2012-03-26 08:42:48 +0200622struct pwm_lookup {
623 struct list_head list;
624 const char *provider;
625 unsigned int index;
626 const char *dev_id;
627 const char *con_id;
Alexandre Belloni3796ce12014-05-19 22:42:32 +0200628 unsigned int period;
629 enum pwm_polarity polarity;
Hans de Goedeb526a312017-01-22 17:14:08 +0100630 const char *module; /* optional, may be NULL */
Thierry Reding8138d2d2012-03-26 08:42:48 +0200631};
632
Hans de Goedeb526a312017-01-22 17:14:08 +0100633#define PWM_LOOKUP_WITH_MODULE(_provider, _index, _dev_id, _con_id, \
634 _period, _polarity, _module) \
635 { \
636 .provider = _provider, \
637 .index = _index, \
638 .dev_id = _dev_id, \
639 .con_id = _con_id, \
640 .period = _period, \
641 .polarity = _polarity, \
642 .module = _module, \
Thierry Reding8138d2d2012-03-26 08:42:48 +0200643 }
644
Hans de Goedeb526a312017-01-22 17:14:08 +0100645#define PWM_LOOKUP(_provider, _index, _dev_id, _con_id, _period, _polarity) \
646 PWM_LOOKUP_WITH_MODULE(_provider, _index, _dev_id, _con_id, _period, \
647 _polarity, NULL)
648
Tushar Behera0bcf1682012-09-12 15:31:46 +0530649#if IS_ENABLED(CONFIG_PWM)
Thierry Reding8138d2d2012-03-26 08:42:48 +0200650void pwm_add_table(struct pwm_lookup *table, size_t num);
Shobhit Kumarefb0de52015-05-05 15:04:18 +0530651void pwm_remove_table(struct pwm_lookup *table, size_t num);
Tushar Behera0bcf1682012-09-12 15:31:46 +0530652#else
653static inline void pwm_add_table(struct pwm_lookup *table, size_t num)
654{
655}
Shobhit Kumarefb0de52015-05-05 15:04:18 +0530656
657static inline void pwm_remove_table(struct pwm_lookup *table, size_t num)
658{
659}
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100660#endif
661
H Hartley Sweeten76abbdde2013-06-11 10:38:59 -0700662#ifdef CONFIG_PWM_SYSFS
663void pwmchip_sysfs_export(struct pwm_chip *chip);
664void pwmchip_sysfs_unexport(struct pwm_chip *chip);
665#else
666static inline void pwmchip_sysfs_export(struct pwm_chip *chip)
667{
668}
669
670static inline void pwmchip_sysfs_unexport(struct pwm_chip *chip)
671{
672}
673#endif /* CONFIG_PWM_SYSFS */
674
Mark Vels5243ef82009-01-18 18:42:45 +0100675#endif /* __LINUX_PWM_H */