1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */ 21a189b97SRussell King #ifndef __LINUX_PWM_H 31a189b97SRussell King #define __LINUX_PWM_H 41a189b97SRussell King 54c56b143SUwe Kleine-König #include <linux/device.h> 60bcf168bSTushar Behera #include <linux/err.h> 733c651a3SUwe Kleine-König #include <linux/module.h> 8d1cd2142SJonathan Richardson #include <linux/mutex.h> 97299ab70SThierry Reding #include <linux/of.h> 107299ab70SThierry Reding 1133c651a3SUwe Kleine-König MODULE_IMPORT_NS(PWM); 1233c651a3SUwe Kleine-König 130c2498f1SSascha Hauer struct pwm_chip; 140c2498f1SSascha Hauer 150aa0869cSPhilip, Avinash /** 160aa0869cSPhilip, Avinash * enum pwm_polarity - polarity of a PWM signal 170aa0869cSPhilip, Avinash * @PWM_POLARITY_NORMAL: a high signal for the duration of the duty- 180aa0869cSPhilip, Avinash * cycle, followed by a low signal for the remainder of the pulse 190aa0869cSPhilip, Avinash * period 200aa0869cSPhilip, Avinash * @PWM_POLARITY_INVERSED: a low signal for the duration of the duty- 210aa0869cSPhilip, Avinash * cycle, followed by a high signal for the remainder of the pulse 220aa0869cSPhilip, Avinash * period 230aa0869cSPhilip, Avinash */ 240aa0869cSPhilip, Avinash enum pwm_polarity { 250aa0869cSPhilip, Avinash PWM_POLARITY_NORMAL, 260aa0869cSPhilip, Avinash PWM_POLARITY_INVERSED, 270aa0869cSPhilip, Avinash }; 280aa0869cSPhilip, Avinash 29e39c0df1SBoris Brezillon /** 30e39c0df1SBoris Brezillon * struct pwm_args - board-dependent PWM arguments 31e39c0df1SBoris Brezillon * @period: reference period 32e39c0df1SBoris Brezillon * @polarity: reference polarity 33e39c0df1SBoris Brezillon * 34e39c0df1SBoris Brezillon * This structure describes board-dependent arguments attached to a PWM 35e39c0df1SBoris Brezillon * device. These arguments are usually retrieved from the PWM lookup table or 36e39c0df1SBoris Brezillon * device tree. 37e39c0df1SBoris Brezillon * 38e39c0df1SBoris Brezillon * Do not confuse this with the PWM state: PWM arguments represent the initial 39e39c0df1SBoris Brezillon * configuration that users want to use on this PWM device rather than the 40e39c0df1SBoris Brezillon * current PWM hardware state. 41e39c0df1SBoris Brezillon */ 42e39c0df1SBoris Brezillon struct pwm_args { 43a9d887dcSGuru Das Srinagesh u64 period; 44e39c0df1SBoris Brezillon enum pwm_polarity polarity; 45e39c0df1SBoris Brezillon }; 46e39c0df1SBoris Brezillon 47f051c466SThierry Reding enum { 48d27abbfdSDan Carpenter PWMF_REQUESTED = 0, 49d27abbfdSDan Carpenter PWMF_EXPORTED = 1, 50f051c466SThierry Reding }; 51f051c466SThierry Reding 5243a276b0SBoris Brezillon /* 5343a276b0SBoris Brezillon * struct pwm_state - state of a PWM channel 5443a276b0SBoris Brezillon * @period: PWM period (in nanoseconds) 5543a276b0SBoris Brezillon * @duty_cycle: PWM duty cycle (in nanoseconds) 5643a276b0SBoris Brezillon * @polarity: PWM polarity 5709a7e4a3SBoris Brezillon * @enabled: PWM enabled status 589e40ee18SClemens Gruber * @usage_power: If set, the PWM driver is only required to maintain the power 599e40ee18SClemens Gruber * output but has more freedom regarding signal form. 609e40ee18SClemens Gruber * If supported, the signal can be optimized, for example to 619e40ee18SClemens Gruber * improve EMI by phase shifting individual channels. 6243a276b0SBoris Brezillon */ 6343a276b0SBoris Brezillon struct pwm_state { 64a9d887dcSGuru Das Srinagesh u64 period; 65a9d887dcSGuru Das Srinagesh u64 duty_cycle; 6643a276b0SBoris Brezillon enum pwm_polarity polarity; 6709a7e4a3SBoris Brezillon bool enabled; 689e40ee18SClemens Gruber bool usage_power; 6943a276b0SBoris Brezillon }; 7043a276b0SBoris Brezillon 7104883802SThierry Reding /** 7204883802SThierry Reding * struct pwm_device - PWM channel object 7304883802SThierry Reding * @label: name of the PWM device 7404883802SThierry Reding * @flags: flags associated with the PWM device 7504883802SThierry Reding * @hwpwm: per-chip relative index of the PWM device 7604883802SThierry Reding * @chip: PWM chip providing this PWM device 77e39c0df1SBoris Brezillon * @args: PWM arguments 783ad1f3a3SUwe Kleine-König * @state: last applied state 793ad1f3a3SUwe Kleine-König * @last: last implemented state (for PWM_DEBUG) 8004883802SThierry Reding */ 81f051c466SThierry Reding struct pwm_device { 82f051c466SThierry Reding const char *label; 83f051c466SThierry Reding unsigned long flags; 84f051c466SThierry Reding unsigned int hwpwm; 85f051c466SThierry Reding struct pwm_chip *chip; 86f051c466SThierry Reding 87e39c0df1SBoris Brezillon struct pwm_args args; 8843a276b0SBoris Brezillon struct pwm_state state; 893ad1f3a3SUwe Kleine-König struct pwm_state last; 90f051c466SThierry Reding }; 91f051c466SThierry Reding 9243a276b0SBoris Brezillon /** 9343a276b0SBoris Brezillon * pwm_get_state() - retrieve the current PWM state 9443a276b0SBoris Brezillon * @pwm: PWM device 9543a276b0SBoris Brezillon * @state: state to fill with the current PWM state 961a7a6e80SUwe Kleine-König * 971a7a6e80SUwe Kleine-König * The returned PWM state represents the state that was applied by a previous call to 98c748a6d7SSean Young * pwm_apply_might_sleep(). Drivers may have to slightly tweak that state before programming it to 99c748a6d7SSean Young * hardware. If pwm_apply_might_sleep() was never called, this returns either the current hardware 1001a7a6e80SUwe Kleine-König * state (if supported) or the default settings. 10143a276b0SBoris Brezillon */ 10243a276b0SBoris Brezillon static inline void pwm_get_state(const struct pwm_device *pwm, 10343a276b0SBoris Brezillon struct pwm_state *state) 10443a276b0SBoris Brezillon { 10543a276b0SBoris Brezillon *state = pwm->state; 10643a276b0SBoris Brezillon } 10743a276b0SBoris Brezillon 1085c31252cSBoris Brezillon static inline bool pwm_is_enabled(const struct pwm_device *pwm) 1095c31252cSBoris Brezillon { 11009a7e4a3SBoris Brezillon struct pwm_state state; 11109a7e4a3SBoris Brezillon 11209a7e4a3SBoris Brezillon pwm_get_state(pwm, &state); 11309a7e4a3SBoris Brezillon 11409a7e4a3SBoris Brezillon return state.enabled; 1155c31252cSBoris Brezillon } 1165c31252cSBoris Brezillon 117a9d887dcSGuru Das Srinagesh static inline u64 pwm_get_period(const struct pwm_device *pwm) 118f051c466SThierry Reding { 11943a276b0SBoris Brezillon struct pwm_state state; 12043a276b0SBoris Brezillon 12143a276b0SBoris Brezillon pwm_get_state(pwm, &state); 12243a276b0SBoris Brezillon 12343a276b0SBoris Brezillon return state.period; 124f051c466SThierry Reding } 125f051c466SThierry Reding 126a9d887dcSGuru Das Srinagesh static inline u64 pwm_get_duty_cycle(const struct pwm_device *pwm) 12776abbddeSH Hartley Sweeten { 12843a276b0SBoris Brezillon struct pwm_state state; 12943a276b0SBoris Brezillon 13043a276b0SBoris Brezillon pwm_get_state(pwm, &state); 13143a276b0SBoris Brezillon 13243a276b0SBoris Brezillon return state.duty_cycle; 13376abbddeSH Hartley Sweeten } 13476abbddeSH Hartley Sweeten 135011e7631SBoris Brezillon static inline enum pwm_polarity pwm_get_polarity(const struct pwm_device *pwm) 136011e7631SBoris Brezillon { 13743a276b0SBoris Brezillon struct pwm_state state; 13843a276b0SBoris Brezillon 13943a276b0SBoris Brezillon pwm_get_state(pwm, &state); 14043a276b0SBoris Brezillon 14143a276b0SBoris Brezillon return state.polarity; 142011e7631SBoris Brezillon } 143011e7631SBoris Brezillon 144e39c0df1SBoris Brezillon static inline void pwm_get_args(const struct pwm_device *pwm, 145e39c0df1SBoris Brezillon struct pwm_args *args) 146e39c0df1SBoris Brezillon { 147e39c0df1SBoris Brezillon *args = pwm->args; 148e39c0df1SBoris Brezillon } 149e39c0df1SBoris Brezillon 1500c2498f1SSascha Hauer /** 151c748a6d7SSean Young * pwm_init_state() - prepare a new state to be applied with pwm_apply_might_sleep() 152a6a0dbbcSBoris Brezillon * @pwm: PWM device 153a6a0dbbcSBoris Brezillon * @state: state to fill with the prepared PWM state 154a6a0dbbcSBoris Brezillon * 155a6a0dbbcSBoris Brezillon * This functions prepares a state that can later be tweaked and applied 156c748a6d7SSean Young * to the PWM device with pwm_apply_might_sleep(). This is a convenient function 157a6a0dbbcSBoris Brezillon * that first retrieves the current PWM state and the replaces the period 158a6a0dbbcSBoris Brezillon * and polarity fields with the reference values defined in pwm->args. 159a6a0dbbcSBoris Brezillon * Once the function returns, you can adjust the ->enabled and ->duty_cycle 160c748a6d7SSean Young * fields according to your needs before calling pwm_apply_might_sleep(). 161a6a0dbbcSBoris Brezillon * 162a6a0dbbcSBoris Brezillon * ->duty_cycle is initially set to zero to avoid cases where the current 163a6a0dbbcSBoris Brezillon * ->duty_cycle value exceed the pwm_args->period one, which would trigger 164c748a6d7SSean Young * an error if the user calls pwm_apply_might_sleep() without adjusting ->duty_cycle 165a6a0dbbcSBoris Brezillon * first. 166a6a0dbbcSBoris Brezillon */ 167a6a0dbbcSBoris Brezillon static inline void pwm_init_state(const struct pwm_device *pwm, 168a6a0dbbcSBoris Brezillon struct pwm_state *state) 169a6a0dbbcSBoris Brezillon { 170a6a0dbbcSBoris Brezillon struct pwm_args args; 171a6a0dbbcSBoris Brezillon 172a6a0dbbcSBoris Brezillon /* First get the current state. */ 173a6a0dbbcSBoris Brezillon pwm_get_state(pwm, state); 174a6a0dbbcSBoris Brezillon 175a6a0dbbcSBoris Brezillon /* Then fill it with the reference config */ 176a6a0dbbcSBoris Brezillon pwm_get_args(pwm, &args); 177a6a0dbbcSBoris Brezillon 178a6a0dbbcSBoris Brezillon state->period = args.period; 179a6a0dbbcSBoris Brezillon state->polarity = args.polarity; 180a6a0dbbcSBoris Brezillon state->duty_cycle = 0; 1819e40ee18SClemens Gruber state->usage_power = false; 182a6a0dbbcSBoris Brezillon } 183a6a0dbbcSBoris Brezillon 184a6a0dbbcSBoris Brezillon /** 185f6f3bddfSBoris Brezillon * pwm_get_relative_duty_cycle() - Get a relative duty cycle value 186f6f3bddfSBoris Brezillon * @state: PWM state to extract the duty cycle from 187f6f3bddfSBoris Brezillon * @scale: target scale of the relative duty cycle 188f6f3bddfSBoris Brezillon * 189f6f3bddfSBoris Brezillon * This functions converts the absolute duty cycle stored in @state (expressed 190f6f3bddfSBoris Brezillon * in nanosecond) into a value relative to the period. 191f6f3bddfSBoris Brezillon * 192f6f3bddfSBoris Brezillon * For example if you want to get the duty_cycle expressed in percent, call: 193f6f3bddfSBoris Brezillon * 194f6f3bddfSBoris Brezillon * pwm_get_state(pwm, &state); 195f6f3bddfSBoris Brezillon * duty = pwm_get_relative_duty_cycle(&state, 100); 196f6f3bddfSBoris Brezillon */ 197f6f3bddfSBoris Brezillon static inline unsigned int 198f6f3bddfSBoris Brezillon pwm_get_relative_duty_cycle(const struct pwm_state *state, unsigned int scale) 199f6f3bddfSBoris Brezillon { 200f6f3bddfSBoris Brezillon if (!state->period) 201f6f3bddfSBoris Brezillon return 0; 202f6f3bddfSBoris Brezillon 203f6f3bddfSBoris Brezillon return DIV_ROUND_CLOSEST_ULL((u64)state->duty_cycle * scale, 204f6f3bddfSBoris Brezillon state->period); 205f6f3bddfSBoris Brezillon } 206f6f3bddfSBoris Brezillon 207f6f3bddfSBoris Brezillon /** 208f6f3bddfSBoris Brezillon * pwm_set_relative_duty_cycle() - Set a relative duty cycle value 209f6f3bddfSBoris Brezillon * @state: PWM state to fill 210f6f3bddfSBoris Brezillon * @duty_cycle: relative duty cycle value 211f6f3bddfSBoris Brezillon * @scale: scale in which @duty_cycle is expressed 212f6f3bddfSBoris Brezillon * 213f6f3bddfSBoris Brezillon * This functions converts a relative into an absolute duty cycle (expressed 214f6f3bddfSBoris Brezillon * in nanoseconds), and puts the result in state->duty_cycle. 215f6f3bddfSBoris Brezillon * 216f6f3bddfSBoris Brezillon * For example if you want to configure a 50% duty cycle, call: 217f6f3bddfSBoris Brezillon * 218f6f3bddfSBoris Brezillon * pwm_init_state(pwm, &state); 219f6f3bddfSBoris Brezillon * pwm_set_relative_duty_cycle(&state, 50, 100); 220c748a6d7SSean Young * pwm_apply_might_sleep(pwm, &state); 221f6f3bddfSBoris Brezillon * 222f6f3bddfSBoris Brezillon * This functions returns -EINVAL if @duty_cycle and/or @scale are 223f6f3bddfSBoris Brezillon * inconsistent (@scale == 0 or @duty_cycle > @scale). 224f6f3bddfSBoris Brezillon */ 225f6f3bddfSBoris Brezillon static inline int 226f6f3bddfSBoris Brezillon pwm_set_relative_duty_cycle(struct pwm_state *state, unsigned int duty_cycle, 227f6f3bddfSBoris Brezillon unsigned int scale) 228f6f3bddfSBoris Brezillon { 229f6f3bddfSBoris Brezillon if (!scale || duty_cycle > scale) 230f6f3bddfSBoris Brezillon return -EINVAL; 231f6f3bddfSBoris Brezillon 232f6f3bddfSBoris Brezillon state->duty_cycle = DIV_ROUND_CLOSEST_ULL((u64)duty_cycle * 233f6f3bddfSBoris Brezillon state->period, 234f6f3bddfSBoris Brezillon scale); 235f6f3bddfSBoris Brezillon 236f6f3bddfSBoris Brezillon return 0; 237f6f3bddfSBoris Brezillon } 238f6f3bddfSBoris Brezillon 239f6f3bddfSBoris Brezillon /** 240ef2e35d9SUwe Kleine-König * struct pwm_capture - PWM capture data 241ef2e35d9SUwe Kleine-König * @period: period of the PWM signal (in nanoseconds) 242ef2e35d9SUwe Kleine-König * @duty_cycle: duty cycle of the PWM signal (in nanoseconds) 243ef2e35d9SUwe Kleine-König */ 244ef2e35d9SUwe Kleine-König struct pwm_capture { 245ef2e35d9SUwe Kleine-König unsigned int period; 246ef2e35d9SUwe Kleine-König unsigned int duty_cycle; 247ef2e35d9SUwe Kleine-König }; 248ef2e35d9SUwe Kleine-König 249ef2e35d9SUwe Kleine-König /** 2500c2498f1SSascha Hauer * struct pwm_ops - PWM controller operations 2510c2498f1SSascha Hauer * @request: optional hook for requesting a PWM 2520c2498f1SSascha Hauer * @free: optional hook for freeing a PWM 2533a3d1a4eSLee Jones * @capture: capture and report PWM signal 25427938fd8SRasmus Villemoes * @apply: atomically apply a new PWM config 255a96d3659SUwe Kleine-König * @get_state: get the current PWM state. 2560c2498f1SSascha Hauer */ 2570c2498f1SSascha Hauer struct pwm_ops { 2586bc7064aSThierry Reding int (*request)(struct pwm_chip *chip, struct pwm_device *pwm); 2596bc7064aSThierry Reding void (*free)(struct pwm_chip *chip, struct pwm_device *pwm); 2603a3d1a4eSLee Jones int (*capture)(struct pwm_chip *chip, struct pwm_device *pwm, 2613a3d1a4eSLee Jones struct pwm_capture *result, unsigned long timeout); 2625ec803edSBoris Brezillon int (*apply)(struct pwm_chip *chip, struct pwm_device *pwm, 26371523d18SUwe Kleine-König const struct pwm_state *state); 2646c452cffSUwe Kleine-König int (*get_state)(struct pwm_chip *chip, struct pwm_device *pwm, 26515fa8a43SBoris Brezillon struct pwm_state *state); 2660c2498f1SSascha Hauer }; 2670c2498f1SSascha Hauer 2680c2498f1SSascha Hauer /** 269f051c466SThierry Reding * struct pwm_chip - abstract a PWM controller 270f051c466SThierry Reding * @dev: device providing the PWMs 271f051c466SThierry Reding * @ops: callbacks for this PWM controller 272384461abSUwe Kleine-König * @owner: module providing this chip 2732d91123aSUwe Kleine-König * @id: unique number of this PWM chip 274f051c466SThierry Reding * @npwm: number of PWMs controlled by this chip 27504883802SThierry Reding * @of_xlate: request a PWM device given a device tree PWM specifier 2767170d3beSSean Young * @atomic: can the driver's ->apply() be called in atomic context 27705947224SUwe Kleine-König * @uses_pwmchip_alloc: signals if pwmchip_allow was used to allocate this chip 278*1cc2e1faSUwe Kleine-König * @operational: signals if the chip can be used (or is already deregistered) 279*1cc2e1faSUwe Kleine-König * @nonatomic_lock: mutex for nonatomic chips 280*1cc2e1faSUwe Kleine-König * @atomic_lock: mutex for atomic chips 2815d0a4c11SUwe Kleine-König * @pwms: array of PWM devices allocated by the framework 2820c2498f1SSascha Hauer */ 2830c2498f1SSascha Hauer struct pwm_chip { 2844c56b143SUwe Kleine-König struct device dev; 285f051c466SThierry Reding const struct pwm_ops *ops; 286384461abSUwe Kleine-König struct module *owner; 28754c86dd2SUwe Kleine-König unsigned int id; 288f051c466SThierry Reding unsigned int npwm; 289f051c466SThierry Reding 290b4f78ff7SUwe Kleine-König struct pwm_device * (*of_xlate)(struct pwm_chip *chip, 2917299ab70SThierry Reding const struct of_phandle_args *args); 2927170d3beSSean Young bool atomic; 2935d0a4c11SUwe Kleine-König 2945d0a4c11SUwe Kleine-König /* only used internally by the PWM framework */ 29505947224SUwe Kleine-König bool uses_pwmchip_alloc; 296*1cc2e1faSUwe Kleine-König bool operational; 297*1cc2e1faSUwe Kleine-König union { 298*1cc2e1faSUwe Kleine-König /* 299*1cc2e1faSUwe Kleine-König * depending on the chip being atomic or not either the mutex or 300*1cc2e1faSUwe Kleine-König * the spinlock is used. It protects .operational and 301*1cc2e1faSUwe Kleine-König * synchronizes the callbacks in .ops 302*1cc2e1faSUwe Kleine-König */ 303*1cc2e1faSUwe Kleine-König struct mutex nonatomic_lock; 304*1cc2e1faSUwe Kleine-König spinlock_t atomic_lock; 305*1cc2e1faSUwe Kleine-König }; 306ee37bf50SUwe Kleine-König struct pwm_device pwms[] __counted_by(npwm); 3070c2498f1SSascha Hauer }; 3080c2498f1SSascha Hauer 3094e59267cSUwe Kleine-König static inline struct device *pwmchip_parent(const struct pwm_chip *chip) 3104e59267cSUwe Kleine-König { 3114c56b143SUwe Kleine-König return chip->dev.parent; 3124e59267cSUwe Kleine-König } 3134e59267cSUwe Kleine-König 31424003d50SUwe Kleine-König static inline void *pwmchip_get_drvdata(struct pwm_chip *chip) 31524003d50SUwe Kleine-König { 31638ae7142SUwe Kleine-König return dev_get_drvdata(&chip->dev); 31724003d50SUwe Kleine-König } 31824003d50SUwe Kleine-König 31924003d50SUwe Kleine-König static inline void pwmchip_set_drvdata(struct pwm_chip *chip, void *data) 32024003d50SUwe Kleine-König { 32138ae7142SUwe Kleine-König dev_set_drvdata(&chip->dev, data); 32224003d50SUwe Kleine-König } 32324003d50SUwe Kleine-König 3240bcf168bSTushar Behera #if IS_ENABLED(CONFIG_PWM) 3255ec803edSBoris Brezillon /* PWM user APIs */ 326c748a6d7SSean Young int pwm_apply_might_sleep(struct pwm_device *pwm, const struct pwm_state *state); 3277170d3beSSean Young int pwm_apply_atomic(struct pwm_device *pwm, const struct pwm_state *state); 3285ec803edSBoris Brezillon int pwm_adjust_config(struct pwm_device *pwm); 3295ec803edSBoris Brezillon 3305ec803edSBoris Brezillon /** 3315ec803edSBoris Brezillon * pwm_config() - change a PWM device configuration 3325ec803edSBoris Brezillon * @pwm: PWM device 3335ec803edSBoris Brezillon * @duty_ns: "on" time (in nanoseconds) 3345ec803edSBoris Brezillon * @period_ns: duration (in nanoseconds) of one cycle 3355ec803edSBoris Brezillon * 3365ec803edSBoris Brezillon * Returns: 0 on success or a negative error code on failure. 3375ec803edSBoris Brezillon */ 3385ec803edSBoris Brezillon static inline int pwm_config(struct pwm_device *pwm, int duty_ns, 3395ec803edSBoris Brezillon int period_ns) 3405ec803edSBoris Brezillon { 3415ec803edSBoris Brezillon struct pwm_state state; 3425ec803edSBoris Brezillon 3435ec803edSBoris Brezillon if (!pwm) 3445ec803edSBoris Brezillon return -EINVAL; 3455ec803edSBoris Brezillon 346ef2bf499SBrian Norris if (duty_ns < 0 || period_ns < 0) 347ef2bf499SBrian Norris return -EINVAL; 348ef2bf499SBrian Norris 3495ec803edSBoris Brezillon pwm_get_state(pwm, &state); 3505ec803edSBoris Brezillon if (state.duty_cycle == duty_ns && state.period == period_ns) 3515ec803edSBoris Brezillon return 0; 3525ec803edSBoris Brezillon 3535ec803edSBoris Brezillon state.duty_cycle = duty_ns; 3545ec803edSBoris Brezillon state.period = period_ns; 355c748a6d7SSean Young return pwm_apply_might_sleep(pwm, &state); 3565ec803edSBoris Brezillon } 3575ec803edSBoris Brezillon 3585ec803edSBoris Brezillon /** 3595ec803edSBoris Brezillon * pwm_enable() - start a PWM output toggling 3605ec803edSBoris Brezillon * @pwm: PWM device 3615ec803edSBoris Brezillon * 3625ec803edSBoris Brezillon * Returns: 0 on success or a negative error code on failure. 3635ec803edSBoris Brezillon */ 3645ec803edSBoris Brezillon static inline int pwm_enable(struct pwm_device *pwm) 3655ec803edSBoris Brezillon { 3665ec803edSBoris Brezillon struct pwm_state state; 3675ec803edSBoris Brezillon 3685ec803edSBoris Brezillon if (!pwm) 3695ec803edSBoris Brezillon return -EINVAL; 3705ec803edSBoris Brezillon 3715ec803edSBoris Brezillon pwm_get_state(pwm, &state); 3725ec803edSBoris Brezillon if (state.enabled) 3735ec803edSBoris Brezillon return 0; 3745ec803edSBoris Brezillon 3755ec803edSBoris Brezillon state.enabled = true; 376c748a6d7SSean Young return pwm_apply_might_sleep(pwm, &state); 3775ec803edSBoris Brezillon } 3785ec803edSBoris Brezillon 3795ec803edSBoris Brezillon /** 3805ec803edSBoris Brezillon * pwm_disable() - stop a PWM output toggling 3815ec803edSBoris Brezillon * @pwm: PWM device 3825ec803edSBoris Brezillon */ 3835ec803edSBoris Brezillon static inline void pwm_disable(struct pwm_device *pwm) 3845ec803edSBoris Brezillon { 3855ec803edSBoris Brezillon struct pwm_state state; 3865ec803edSBoris Brezillon 3875ec803edSBoris Brezillon if (!pwm) 3885ec803edSBoris Brezillon return; 3895ec803edSBoris Brezillon 3905ec803edSBoris Brezillon pwm_get_state(pwm, &state); 3915ec803edSBoris Brezillon if (!state.enabled) 3925ec803edSBoris Brezillon return; 3935ec803edSBoris Brezillon 3945ec803edSBoris Brezillon state.enabled = false; 395c748a6d7SSean Young pwm_apply_might_sleep(pwm, &state); 3965ec803edSBoris Brezillon } 3975ec803edSBoris Brezillon 3987170d3beSSean Young /** 3997170d3beSSean Young * pwm_might_sleep() - is pwm_apply_atomic() supported? 4007170d3beSSean Young * @pwm: PWM device 4017170d3beSSean Young * 4027170d3beSSean Young * Returns: false if pwm_apply_atomic() can be called from atomic context. 4037170d3beSSean Young */ 4047170d3beSSean Young static inline bool pwm_might_sleep(struct pwm_device *pwm) 4057170d3beSSean Young { 4067170d3beSSean Young return !pwm->chip->atomic; 4077170d3beSSean Young } 4087170d3beSSean Young 4095ec803edSBoris Brezillon /* PWM provider APIs */ 410024913dbSUwe Kleine-König void pwmchip_put(struct pwm_chip *chip); 411024913dbSUwe Kleine-König struct pwm_chip *pwmchip_alloc(struct device *parent, unsigned int npwm, size_t sizeof_priv); 412024913dbSUwe Kleine-König struct pwm_chip *devm_pwmchip_alloc(struct device *parent, unsigned int npwm, size_t sizeof_priv); 413024913dbSUwe Kleine-König 414384461abSUwe Kleine-König int __pwmchip_add(struct pwm_chip *chip, struct module *owner); 415384461abSUwe Kleine-König #define pwmchip_add(chip) __pwmchip_add(chip, THIS_MODULE) 4168083f58dSUwe Kleine-König void pwmchip_remove(struct pwm_chip *chip); 417bcda91bfSUwe Kleine-König 418384461abSUwe Kleine-König int __devm_pwmchip_add(struct device *dev, struct pwm_chip *chip, struct module *owner); 419384461abSUwe Kleine-König #define devm_pwmchip_add(dev, chip) __devm_pwmchip_add(dev, chip, THIS_MODULE) 420bcda91bfSUwe Kleine-König 421b4f78ff7SUwe Kleine-König struct pwm_device *of_pwm_xlate_with_flags(struct pwm_chip *chip, 42283af2402SPhilip, Avinash const struct of_phandle_args *args); 423b4f78ff7SUwe Kleine-König struct pwm_device *of_pwm_single_xlate(struct pwm_chip *chip, 4243ab7b6acSBjorn Andersson const struct of_phandle_args *args); 42583af2402SPhilip, Avinash 426d4c0c470SPeter Ujfalusi struct pwm_device *pwm_get(struct device *dev, const char *con_id); 4278138d2ddSThierry Reding void pwm_put(struct pwm_device *pwm); 4288138d2ddSThierry Reding 429d4c0c470SPeter Ujfalusi struct pwm_device *devm_pwm_get(struct device *dev, const char *con_id); 4304a6ef8e3SNikolaus Voss struct pwm_device *devm_fwnode_pwm_get(struct device *dev, 4314a6ef8e3SNikolaus Voss struct fwnode_handle *fwnode, 4324a6ef8e3SNikolaus Voss const char *con_id); 4330bcf168bSTushar Behera #else 4347170d3beSSean Young static inline bool pwm_might_sleep(struct pwm_device *pwm) 4357170d3beSSean Young { 4367170d3beSSean Young return true; 4377170d3beSSean Young } 4387170d3beSSean Young 439c748a6d7SSean Young static inline int pwm_apply_might_sleep(struct pwm_device *pwm, 4405ec803edSBoris Brezillon const struct pwm_state *state) 4415ec803edSBoris Brezillon { 4424ad91a22SUwe Kleine-König might_sleep(); 443dc518b37SSean Young return -EOPNOTSUPP; 4445ec803edSBoris Brezillon } 4455ec803edSBoris Brezillon 4467170d3beSSean Young static inline int pwm_apply_atomic(struct pwm_device *pwm, 4477170d3beSSean Young const struct pwm_state *state) 4487170d3beSSean Young { 4497170d3beSSean Young return -EOPNOTSUPP; 4507170d3beSSean Young } 4517170d3beSSean Young 4525ec803edSBoris Brezillon static inline int pwm_adjust_config(struct pwm_device *pwm) 4535ec803edSBoris Brezillon { 454dc518b37SSean Young return -EOPNOTSUPP; 4555ec803edSBoris Brezillon } 4565ec803edSBoris Brezillon 4575ec803edSBoris Brezillon static inline int pwm_config(struct pwm_device *pwm, int duty_ns, 4585ec803edSBoris Brezillon int period_ns) 4595ec803edSBoris Brezillon { 4604ad91a22SUwe Kleine-König might_sleep(); 4615ec803edSBoris Brezillon return -EINVAL; 4625ec803edSBoris Brezillon } 4635ec803edSBoris Brezillon 4645ec803edSBoris Brezillon static inline int pwm_enable(struct pwm_device *pwm) 4655ec803edSBoris Brezillon { 4664ad91a22SUwe Kleine-König might_sleep(); 4675ec803edSBoris Brezillon return -EINVAL; 4685ec803edSBoris Brezillon } 4695ec803edSBoris Brezillon 4705ec803edSBoris Brezillon static inline void pwm_disable(struct pwm_device *pwm) 4715ec803edSBoris Brezillon { 4724ad91a22SUwe Kleine-König might_sleep(); 4735ec803edSBoris Brezillon } 4745ec803edSBoris Brezillon 475024913dbSUwe Kleine-König static inline void pwmchip_put(struct pwm_chip *chip) 476024913dbSUwe Kleine-König { 477024913dbSUwe Kleine-König } 478024913dbSUwe Kleine-König 479024913dbSUwe Kleine-König static inline struct pwm_chip *pwmchip_alloc(struct device *parent, 480024913dbSUwe Kleine-König unsigned int npwm, 481024913dbSUwe Kleine-König size_t sizeof_priv) 482024913dbSUwe Kleine-König { 483024913dbSUwe Kleine-König return ERR_PTR(-EINVAL); 484024913dbSUwe Kleine-König } 485024913dbSUwe Kleine-König 486024913dbSUwe Kleine-König static inline struct pwm_chip *devm_pwmchip_alloc(struct device *parent, 487024913dbSUwe Kleine-König unsigned int npwm, 488024913dbSUwe Kleine-König size_t sizeof_priv) 489024913dbSUwe Kleine-König { 490024913dbSUwe Kleine-König return pwmchip_alloc(parent, npwm, sizeof_priv); 491024913dbSUwe Kleine-König } 492024913dbSUwe Kleine-König 4930bcf168bSTushar Behera static inline int pwmchip_add(struct pwm_chip *chip) 4940bcf168bSTushar Behera { 4950bcf168bSTushar Behera return -EINVAL; 4960bcf168bSTushar Behera } 4970bcf168bSTushar Behera 4980bcf168bSTushar Behera static inline int pwmchip_remove(struct pwm_chip *chip) 4990bcf168bSTushar Behera { 5000bcf168bSTushar Behera return -EINVAL; 5010bcf168bSTushar Behera } 5020bcf168bSTushar Behera 50388da4e81SAndy Shevchenko static inline int devm_pwmchip_add(struct device *dev, struct pwm_chip *chip) 50488da4e81SAndy Shevchenko { 50588da4e81SAndy Shevchenko return -EINVAL; 50688da4e81SAndy Shevchenko } 50788da4e81SAndy Shevchenko 5080bcf168bSTushar Behera static inline struct pwm_device *pwm_get(struct device *dev, 5090bcf168bSTushar Behera const char *consumer) 5100bcf168bSTushar Behera { 51127d9a4d6SUwe Kleine-König might_sleep(); 5120bcf168bSTushar Behera return ERR_PTR(-ENODEV); 5130bcf168bSTushar Behera } 5140bcf168bSTushar Behera 5150bcf168bSTushar Behera static inline void pwm_put(struct pwm_device *pwm) 5160bcf168bSTushar Behera { 51727d9a4d6SUwe Kleine-König might_sleep(); 5180bcf168bSTushar Behera } 5190bcf168bSTushar Behera 5200bcf168bSTushar Behera static inline struct pwm_device *devm_pwm_get(struct device *dev, 5210bcf168bSTushar Behera const char *consumer) 5220bcf168bSTushar Behera { 52327d9a4d6SUwe Kleine-König might_sleep(); 5240bcf168bSTushar Behera return ERR_PTR(-ENODEV); 5250bcf168bSTushar Behera } 5260bcf168bSTushar Behera 5274a6ef8e3SNikolaus Voss static inline struct pwm_device * 5284a6ef8e3SNikolaus Voss devm_fwnode_pwm_get(struct device *dev, struct fwnode_handle *fwnode, 5294a6ef8e3SNikolaus Voss const char *con_id) 5304a6ef8e3SNikolaus Voss { 53127d9a4d6SUwe Kleine-König might_sleep(); 5324a6ef8e3SNikolaus Voss return ERR_PTR(-ENODEV); 5334a6ef8e3SNikolaus Voss } 5340bcf168bSTushar Behera #endif 5356354316dSAlexandre Courbot 5365ec803edSBoris Brezillon static inline void pwm_apply_args(struct pwm_device *pwm) 5375ec803edSBoris Brezillon { 53833cdcee0SBoris Brezillon struct pwm_state state = { }; 53933cdcee0SBoris Brezillon 5405ec803edSBoris Brezillon /* 5415ec803edSBoris Brezillon * PWM users calling pwm_apply_args() expect to have a fresh config 5425ec803edSBoris Brezillon * where the polarity and period are set according to pwm_args info. 5435ec803edSBoris Brezillon * The problem is, polarity can only be changed when the PWM is 5445ec803edSBoris Brezillon * disabled. 5455ec803edSBoris Brezillon * 5465ec803edSBoris Brezillon * PWM drivers supporting hardware readout may declare the PWM device 5475ec803edSBoris Brezillon * as enabled, and prevent polarity setting, which changes from the 5485ec803edSBoris Brezillon * existing behavior, where all PWM devices are declared as disabled 5495ec803edSBoris Brezillon * at startup (even if they are actually enabled), thus authorizing 5505ec803edSBoris Brezillon * polarity setting. 5515ec803edSBoris Brezillon * 55233cdcee0SBoris Brezillon * To fulfill this requirement, we apply a new state which disables 55333cdcee0SBoris Brezillon * the PWM device and set the reference period and polarity config. 5545ec803edSBoris Brezillon * 5555ec803edSBoris Brezillon * Note that PWM users requiring a smooth handover between the 5565ec803edSBoris Brezillon * bootloader and the kernel (like critical regulators controlled by 5575ec803edSBoris Brezillon * PWM devices) will have to switch to the atomic API and avoid calling 5585ec803edSBoris Brezillon * pwm_apply_args(). 5595ec803edSBoris Brezillon */ 56033cdcee0SBoris Brezillon 56133cdcee0SBoris Brezillon state.enabled = false; 56233cdcee0SBoris Brezillon state.polarity = pwm->args.polarity; 56333cdcee0SBoris Brezillon state.period = pwm->args.period; 5649e40ee18SClemens Gruber state.usage_power = false; 56533cdcee0SBoris Brezillon 566c748a6d7SSean Young pwm_apply_might_sleep(pwm, &state); 5675ec803edSBoris Brezillon } 5685ec803edSBoris Brezillon 5698138d2ddSThierry Reding struct pwm_lookup { 5708138d2ddSThierry Reding struct list_head list; 5718138d2ddSThierry Reding const char *provider; 5728138d2ddSThierry Reding unsigned int index; 5738138d2ddSThierry Reding const char *dev_id; 5748138d2ddSThierry Reding const char *con_id; 5753796ce1dSAlexandre Belloni unsigned int period; 5763796ce1dSAlexandre Belloni enum pwm_polarity polarity; 577b526a314SHans de Goede const char *module; /* optional, may be NULL */ 5788138d2ddSThierry Reding }; 5798138d2ddSThierry Reding 580b526a314SHans de Goede #define PWM_LOOKUP_WITH_MODULE(_provider, _index, _dev_id, _con_id, \ 581b526a314SHans de Goede _period, _polarity, _module) \ 5828138d2ddSThierry Reding { \ 5838138d2ddSThierry Reding .provider = _provider, \ 5848138d2ddSThierry Reding .index = _index, \ 5858138d2ddSThierry Reding .dev_id = _dev_id, \ 5868138d2ddSThierry Reding .con_id = _con_id, \ 58742844029SAlexandre Belloni .period = _period, \ 588b526a314SHans de Goede .polarity = _polarity, \ 589b526a314SHans de Goede .module = _module, \ 5908138d2ddSThierry Reding } 5918138d2ddSThierry Reding 592b526a314SHans de Goede #define PWM_LOOKUP(_provider, _index, _dev_id, _con_id, _period, _polarity) \ 593b526a314SHans de Goede PWM_LOOKUP_WITH_MODULE(_provider, _index, _dev_id, _con_id, _period, \ 594b526a314SHans de Goede _polarity, NULL) 595b526a314SHans de Goede 5960bcf168bSTushar Behera #if IS_ENABLED(CONFIG_PWM) 5978138d2ddSThierry Reding void pwm_add_table(struct pwm_lookup *table, size_t num); 598efb0de55SShobhit Kumar void pwm_remove_table(struct pwm_lookup *table, size_t num); 5990bcf168bSTushar Behera #else 6000bcf168bSTushar Behera static inline void pwm_add_table(struct pwm_lookup *table, size_t num) 6010bcf168bSTushar Behera { 6020bcf168bSTushar Behera } 603efb0de55SShobhit Kumar 604efb0de55SShobhit Kumar static inline void pwm_remove_table(struct pwm_lookup *table, size_t num) 605efb0de55SShobhit Kumar { 606efb0de55SShobhit Kumar } 6070c2498f1SSascha Hauer #endif 6080c2498f1SSascha Hauer 6095243ef8bSMark Vels #endif /* __LINUX_PWM_H */ 610