11a189b97SRussell King #ifndef __LINUX_PWM_H 21a189b97SRussell King #define __LINUX_PWM_H 31a189b97SRussell King 40bcf168bSTushar Behera #include <linux/err.h> 5d1cd2142SJonathan Richardson #include <linux/mutex.h> 67299ab70SThierry Reding #include <linux/of.h> 77299ab70SThierry Reding 83a3d1a4eSLee Jones struct pwm_capture; 962099abfSThierry Reding struct seq_file; 103a3d1a4eSLee Jones 110c2498f1SSascha Hauer struct pwm_chip; 120c2498f1SSascha Hauer 130aa0869cSPhilip, Avinash /** 140aa0869cSPhilip, Avinash * enum pwm_polarity - polarity of a PWM signal 150aa0869cSPhilip, Avinash * @PWM_POLARITY_NORMAL: a high signal for the duration of the duty- 160aa0869cSPhilip, Avinash * cycle, followed by a low signal for the remainder of the pulse 170aa0869cSPhilip, Avinash * period 180aa0869cSPhilip, Avinash * @PWM_POLARITY_INVERSED: a low signal for the duration of the duty- 190aa0869cSPhilip, Avinash * cycle, followed by a high signal for the remainder of the pulse 200aa0869cSPhilip, Avinash * period 210aa0869cSPhilip, Avinash */ 220aa0869cSPhilip, Avinash enum pwm_polarity { 230aa0869cSPhilip, Avinash PWM_POLARITY_NORMAL, 240aa0869cSPhilip, Avinash PWM_POLARITY_INVERSED, 250aa0869cSPhilip, Avinash }; 260aa0869cSPhilip, Avinash 27e39c0df1SBoris Brezillon /** 28e39c0df1SBoris Brezillon * struct pwm_args - board-dependent PWM arguments 29e39c0df1SBoris Brezillon * @period: reference period 30e39c0df1SBoris Brezillon * @polarity: reference polarity 31e39c0df1SBoris Brezillon * 32e39c0df1SBoris Brezillon * This structure describes board-dependent arguments attached to a PWM 33e39c0df1SBoris Brezillon * device. These arguments are usually retrieved from the PWM lookup table or 34e39c0df1SBoris Brezillon * device tree. 35e39c0df1SBoris Brezillon * 36e39c0df1SBoris Brezillon * Do not confuse this with the PWM state: PWM arguments represent the initial 37e39c0df1SBoris Brezillon * configuration that users want to use on this PWM device rather than the 38e39c0df1SBoris Brezillon * current PWM hardware state. 39e39c0df1SBoris Brezillon */ 40e39c0df1SBoris Brezillon struct pwm_args { 41e39c0df1SBoris Brezillon unsigned int period; 42e39c0df1SBoris Brezillon enum pwm_polarity polarity; 43e39c0df1SBoris Brezillon }; 44e39c0df1SBoris Brezillon 45f051c466SThierry Reding enum { 46f051c466SThierry Reding PWMF_REQUESTED = 1 << 0, 4709a7e4a3SBoris Brezillon PWMF_EXPORTED = 1 << 1, 48f051c466SThierry Reding }; 49f051c466SThierry Reding 5043a276b0SBoris Brezillon /* 5143a276b0SBoris Brezillon * struct pwm_state - state of a PWM channel 5243a276b0SBoris Brezillon * @period: PWM period (in nanoseconds) 5343a276b0SBoris Brezillon * @duty_cycle: PWM duty cycle (in nanoseconds) 5443a276b0SBoris Brezillon * @polarity: PWM polarity 5509a7e4a3SBoris Brezillon * @enabled: PWM enabled status 5643a276b0SBoris Brezillon */ 5743a276b0SBoris Brezillon struct pwm_state { 5843a276b0SBoris Brezillon unsigned int period; 5943a276b0SBoris Brezillon unsigned int duty_cycle; 6043a276b0SBoris Brezillon enum pwm_polarity polarity; 6109a7e4a3SBoris Brezillon bool enabled; 6243a276b0SBoris Brezillon }; 6343a276b0SBoris Brezillon 6404883802SThierry Reding /** 6504883802SThierry Reding * struct pwm_device - PWM channel object 6604883802SThierry Reding * @label: name of the PWM device 6704883802SThierry Reding * @flags: flags associated with the PWM device 6804883802SThierry Reding * @hwpwm: per-chip relative index of the PWM device 6904883802SThierry Reding * @pwm: global index of the PWM device 7004883802SThierry Reding * @chip: PWM chip providing this PWM device 7104883802SThierry Reding * @chip_data: chip-private data associated with the PWM device 72e39c0df1SBoris Brezillon * @args: PWM arguments 7343a276b0SBoris Brezillon * @state: curent PWM channel state 7404883802SThierry Reding */ 75f051c466SThierry Reding struct pwm_device { 76f051c466SThierry Reding const char *label; 77f051c466SThierry Reding unsigned long flags; 78f051c466SThierry Reding unsigned int hwpwm; 79f051c466SThierry Reding unsigned int pwm; 80f051c466SThierry Reding struct pwm_chip *chip; 81f051c466SThierry Reding void *chip_data; 82f051c466SThierry Reding 83e39c0df1SBoris Brezillon struct pwm_args args; 8443a276b0SBoris Brezillon struct pwm_state state; 85f051c466SThierry Reding }; 86f051c466SThierry Reding 8743a276b0SBoris Brezillon /** 8843a276b0SBoris Brezillon * pwm_get_state() - retrieve the current PWM state 8943a276b0SBoris Brezillon * @pwm: PWM device 9043a276b0SBoris Brezillon * @state: state to fill with the current PWM state 9143a276b0SBoris Brezillon */ 9243a276b0SBoris Brezillon static inline void pwm_get_state(const struct pwm_device *pwm, 9343a276b0SBoris Brezillon struct pwm_state *state) 9443a276b0SBoris Brezillon { 9543a276b0SBoris Brezillon *state = pwm->state; 9643a276b0SBoris Brezillon } 9743a276b0SBoris Brezillon 985c31252cSBoris Brezillon static inline bool pwm_is_enabled(const struct pwm_device *pwm) 995c31252cSBoris Brezillon { 10009a7e4a3SBoris Brezillon struct pwm_state state; 10109a7e4a3SBoris Brezillon 10209a7e4a3SBoris Brezillon pwm_get_state(pwm, &state); 10309a7e4a3SBoris Brezillon 10409a7e4a3SBoris Brezillon return state.enabled; 1055c31252cSBoris Brezillon } 1065c31252cSBoris Brezillon 107f051c466SThierry Reding static inline void pwm_set_period(struct pwm_device *pwm, unsigned int period) 108f051c466SThierry Reding { 109f051c466SThierry Reding if (pwm) 11043a276b0SBoris Brezillon pwm->state.period = period; 111f051c466SThierry Reding } 112f051c466SThierry Reding 113a1cf4217SBoris Brezillon static inline unsigned int pwm_get_period(const struct pwm_device *pwm) 114f051c466SThierry Reding { 11543a276b0SBoris Brezillon struct pwm_state state; 11643a276b0SBoris Brezillon 11743a276b0SBoris Brezillon pwm_get_state(pwm, &state); 11843a276b0SBoris Brezillon 11943a276b0SBoris Brezillon return state.period; 120f051c466SThierry Reding } 121f051c466SThierry Reding 12276abbddeSH Hartley Sweeten static inline void pwm_set_duty_cycle(struct pwm_device *pwm, unsigned int duty) 12376abbddeSH Hartley Sweeten { 12476abbddeSH Hartley Sweeten if (pwm) 12543a276b0SBoris Brezillon pwm->state.duty_cycle = duty; 12676abbddeSH Hartley Sweeten } 12776abbddeSH Hartley Sweeten 128a1cf4217SBoris Brezillon static inline unsigned int pwm_get_duty_cycle(const struct pwm_device *pwm) 12976abbddeSH Hartley Sweeten { 13043a276b0SBoris Brezillon struct pwm_state state; 13143a276b0SBoris Brezillon 13243a276b0SBoris Brezillon pwm_get_state(pwm, &state); 13343a276b0SBoris Brezillon 13443a276b0SBoris Brezillon return state.duty_cycle; 13576abbddeSH Hartley Sweeten } 13676abbddeSH Hartley Sweeten 137011e7631SBoris Brezillon static inline enum pwm_polarity pwm_get_polarity(const struct pwm_device *pwm) 138011e7631SBoris Brezillon { 13943a276b0SBoris Brezillon struct pwm_state state; 14043a276b0SBoris Brezillon 14143a276b0SBoris Brezillon pwm_get_state(pwm, &state); 14243a276b0SBoris Brezillon 14343a276b0SBoris Brezillon return state.polarity; 144011e7631SBoris Brezillon } 145011e7631SBoris Brezillon 146e39c0df1SBoris Brezillon static inline void pwm_get_args(const struct pwm_device *pwm, 147e39c0df1SBoris Brezillon struct pwm_args *args) 148e39c0df1SBoris Brezillon { 149e39c0df1SBoris Brezillon *args = pwm->args; 150e39c0df1SBoris Brezillon } 151e39c0df1SBoris Brezillon 1520c2498f1SSascha Hauer /** 153a6a0dbbcSBoris Brezillon * pwm_init_state() - prepare a new state to be applied with pwm_apply_state() 154a6a0dbbcSBoris Brezillon * @pwm: PWM device 155a6a0dbbcSBoris Brezillon * @state: state to fill with the prepared PWM state 156a6a0dbbcSBoris Brezillon * 157a6a0dbbcSBoris Brezillon * This functions prepares a state that can later be tweaked and applied 158a6a0dbbcSBoris Brezillon * to the PWM device with pwm_apply_state(). This is a convenient function 159a6a0dbbcSBoris Brezillon * that first retrieves the current PWM state and the replaces the period 160a6a0dbbcSBoris Brezillon * and polarity fields with the reference values defined in pwm->args. 161a6a0dbbcSBoris Brezillon * Once the function returns, you can adjust the ->enabled and ->duty_cycle 162a6a0dbbcSBoris Brezillon * fields according to your needs before calling pwm_apply_state(). 163a6a0dbbcSBoris Brezillon * 164a6a0dbbcSBoris Brezillon * ->duty_cycle is initially set to zero to avoid cases where the current 165a6a0dbbcSBoris Brezillon * ->duty_cycle value exceed the pwm_args->period one, which would trigger 166a6a0dbbcSBoris Brezillon * an error if the user calls pwm_apply_state() without adjusting ->duty_cycle 167a6a0dbbcSBoris Brezillon * first. 168a6a0dbbcSBoris Brezillon */ 169a6a0dbbcSBoris Brezillon static inline void pwm_init_state(const struct pwm_device *pwm, 170a6a0dbbcSBoris Brezillon struct pwm_state *state) 171a6a0dbbcSBoris Brezillon { 172a6a0dbbcSBoris Brezillon struct pwm_args args; 173a6a0dbbcSBoris Brezillon 174a6a0dbbcSBoris Brezillon /* First get the current state. */ 175a6a0dbbcSBoris Brezillon pwm_get_state(pwm, state); 176a6a0dbbcSBoris Brezillon 177a6a0dbbcSBoris Brezillon /* Then fill it with the reference config */ 178a6a0dbbcSBoris Brezillon pwm_get_args(pwm, &args); 179a6a0dbbcSBoris Brezillon 180a6a0dbbcSBoris Brezillon state->period = args.period; 181a6a0dbbcSBoris Brezillon state->polarity = args.polarity; 182a6a0dbbcSBoris Brezillon state->duty_cycle = 0; 183a6a0dbbcSBoris Brezillon } 184a6a0dbbcSBoris Brezillon 185a6a0dbbcSBoris Brezillon /** 186f6f3bddfSBoris Brezillon * pwm_get_relative_duty_cycle() - Get a relative duty cycle value 187f6f3bddfSBoris Brezillon * @state: PWM state to extract the duty cycle from 188f6f3bddfSBoris Brezillon * @scale: target scale of the relative duty cycle 189f6f3bddfSBoris Brezillon * 190f6f3bddfSBoris Brezillon * This functions converts the absolute duty cycle stored in @state (expressed 191f6f3bddfSBoris Brezillon * in nanosecond) into a value relative to the period. 192f6f3bddfSBoris Brezillon * 193f6f3bddfSBoris Brezillon * For example if you want to get the duty_cycle expressed in percent, call: 194f6f3bddfSBoris Brezillon * 195f6f3bddfSBoris Brezillon * pwm_get_state(pwm, &state); 196f6f3bddfSBoris Brezillon * duty = pwm_get_relative_duty_cycle(&state, 100); 197f6f3bddfSBoris Brezillon */ 198f6f3bddfSBoris Brezillon static inline unsigned int 199f6f3bddfSBoris Brezillon pwm_get_relative_duty_cycle(const struct pwm_state *state, unsigned int scale) 200f6f3bddfSBoris Brezillon { 201f6f3bddfSBoris Brezillon if (!state->period) 202f6f3bddfSBoris Brezillon return 0; 203f6f3bddfSBoris Brezillon 204f6f3bddfSBoris Brezillon return DIV_ROUND_CLOSEST_ULL((u64)state->duty_cycle * scale, 205f6f3bddfSBoris Brezillon state->period); 206f6f3bddfSBoris Brezillon } 207f6f3bddfSBoris Brezillon 208f6f3bddfSBoris Brezillon /** 209f6f3bddfSBoris Brezillon * pwm_set_relative_duty_cycle() - Set a relative duty cycle value 210f6f3bddfSBoris Brezillon * @state: PWM state to fill 211f6f3bddfSBoris Brezillon * @duty_cycle: relative duty cycle value 212f6f3bddfSBoris Brezillon * @scale: scale in which @duty_cycle is expressed 213f6f3bddfSBoris Brezillon * 214f6f3bddfSBoris Brezillon * This functions converts a relative into an absolute duty cycle (expressed 215f6f3bddfSBoris Brezillon * in nanoseconds), and puts the result in state->duty_cycle. 216f6f3bddfSBoris Brezillon * 217f6f3bddfSBoris Brezillon * For example if you want to configure a 50% duty cycle, call: 218f6f3bddfSBoris Brezillon * 219f6f3bddfSBoris Brezillon * pwm_init_state(pwm, &state); 220f6f3bddfSBoris Brezillon * pwm_set_relative_duty_cycle(&state, 50, 100); 221f6f3bddfSBoris Brezillon * pwm_apply_state(pwm, &state); 222f6f3bddfSBoris Brezillon * 223f6f3bddfSBoris Brezillon * This functions returns -EINVAL if @duty_cycle and/or @scale are 224f6f3bddfSBoris Brezillon * inconsistent (@scale == 0 or @duty_cycle > @scale). 225f6f3bddfSBoris Brezillon */ 226f6f3bddfSBoris Brezillon static inline int 227f6f3bddfSBoris Brezillon pwm_set_relative_duty_cycle(struct pwm_state *state, unsigned int duty_cycle, 228f6f3bddfSBoris Brezillon unsigned int scale) 229f6f3bddfSBoris Brezillon { 230f6f3bddfSBoris Brezillon if (!scale || duty_cycle > scale) 231f6f3bddfSBoris Brezillon return -EINVAL; 232f6f3bddfSBoris Brezillon 233f6f3bddfSBoris Brezillon state->duty_cycle = DIV_ROUND_CLOSEST_ULL((u64)duty_cycle * 234f6f3bddfSBoris Brezillon state->period, 235f6f3bddfSBoris Brezillon scale); 236f6f3bddfSBoris Brezillon 237f6f3bddfSBoris Brezillon return 0; 238f6f3bddfSBoris Brezillon } 239f6f3bddfSBoris Brezillon 240f6f3bddfSBoris Brezillon /** 2410c2498f1SSascha Hauer * struct pwm_ops - PWM controller operations 2420c2498f1SSascha Hauer * @request: optional hook for requesting a PWM 2430c2498f1SSascha Hauer * @free: optional hook for freeing a PWM 2440c2498f1SSascha Hauer * @config: configure duty cycles and period length for this PWM 2450aa0869cSPhilip, Avinash * @set_polarity: configure the polarity of this PWM 2463a3d1a4eSLee Jones * @capture: capture and report PWM signal 2470c2498f1SSascha Hauer * @enable: enable PWM output toggling 2480c2498f1SSascha Hauer * @disable: disable PWM output toggling 2495ec803edSBoris Brezillon * @apply: atomically apply a new PWM config. The state argument 2505ec803edSBoris Brezillon * should be adjusted with the real hardware config (if the 2515ec803edSBoris Brezillon * approximate the period or duty_cycle value, state should 2525ec803edSBoris Brezillon * reflect it) 25315fa8a43SBoris Brezillon * @get_state: get the current PWM state. This function is only 25415fa8a43SBoris Brezillon * called once per PWM device when the PWM chip is 25515fa8a43SBoris Brezillon * registered. 25662099abfSThierry Reding * @dbg_show: optional routine to show contents in debugfs 2570c2498f1SSascha Hauer * @owner: helps prevent removal of modules exporting active PWMs 2580c2498f1SSascha Hauer */ 2590c2498f1SSascha Hauer struct pwm_ops { 2606bc7064aSThierry Reding int (*request)(struct pwm_chip *chip, struct pwm_device *pwm); 2616bc7064aSThierry Reding void (*free)(struct pwm_chip *chip, struct pwm_device *pwm); 2626bc7064aSThierry Reding int (*config)(struct pwm_chip *chip, struct pwm_device *pwm, 263f051c466SThierry Reding int duty_ns, int period_ns); 2646bc7064aSThierry Reding int (*set_polarity)(struct pwm_chip *chip, struct pwm_device *pwm, 2650aa0869cSPhilip, Avinash enum pwm_polarity polarity); 2663a3d1a4eSLee Jones int (*capture)(struct pwm_chip *chip, struct pwm_device *pwm, 2673a3d1a4eSLee Jones struct pwm_capture *result, unsigned long timeout); 2686bc7064aSThierry Reding int (*enable)(struct pwm_chip *chip, struct pwm_device *pwm); 2696bc7064aSThierry Reding void (*disable)(struct pwm_chip *chip, struct pwm_device *pwm); 2705ec803edSBoris Brezillon int (*apply)(struct pwm_chip *chip, struct pwm_device *pwm, 2715ec803edSBoris Brezillon struct pwm_state *state); 27215fa8a43SBoris Brezillon void (*get_state)(struct pwm_chip *chip, struct pwm_device *pwm, 27315fa8a43SBoris Brezillon struct pwm_state *state); 27462099abfSThierry Reding #ifdef CONFIG_DEBUG_FS 2756bc7064aSThierry Reding void (*dbg_show)(struct pwm_chip *chip, struct seq_file *s); 27662099abfSThierry Reding #endif 2770c2498f1SSascha Hauer struct module *owner; 2780c2498f1SSascha Hauer }; 2790c2498f1SSascha Hauer 2800c2498f1SSascha Hauer /** 281f051c466SThierry Reding * struct pwm_chip - abstract a PWM controller 282f051c466SThierry Reding * @dev: device providing the PWMs 283f051c466SThierry Reding * @list: list node for internal use 284f051c466SThierry Reding * @ops: callbacks for this PWM controller 285f051c466SThierry Reding * @base: number of first PWM controlled by this chip 286f051c466SThierry Reding * @npwm: number of PWMs controlled by this chip 287f051c466SThierry Reding * @pwms: array of PWM devices allocated by the framework 28804883802SThierry Reding * @of_xlate: request a PWM device given a device tree PWM specifier 28904883802SThierry Reding * @of_pwm_n_cells: number of cells expected in the device tree PWM specifier 2900c2498f1SSascha Hauer */ 2910c2498f1SSascha Hauer struct pwm_chip { 292f051c466SThierry Reding struct device *dev; 293f051c466SThierry Reding struct list_head list; 294f051c466SThierry Reding const struct pwm_ops *ops; 295f051c466SThierry Reding int base; 296f051c466SThierry Reding unsigned int npwm; 297f051c466SThierry Reding 298f051c466SThierry Reding struct pwm_device *pwms; 2997299ab70SThierry Reding 3007299ab70SThierry Reding struct pwm_device * (*of_xlate)(struct pwm_chip *pc, 3017299ab70SThierry Reding const struct of_phandle_args *args); 3027299ab70SThierry Reding unsigned int of_pwm_n_cells; 3030c2498f1SSascha Hauer }; 3040c2498f1SSascha Hauer 3053a3d1a4eSLee Jones /** 3063a3d1a4eSLee Jones * struct pwm_capture - PWM capture data 3073a3d1a4eSLee Jones * @period: period of the PWM signal (in nanoseconds) 3083a3d1a4eSLee Jones * @duty_cycle: duty cycle of the PWM signal (in nanoseconds) 3093a3d1a4eSLee Jones */ 3103a3d1a4eSLee Jones struct pwm_capture { 3113a3d1a4eSLee Jones unsigned int period; 3123a3d1a4eSLee Jones unsigned int duty_cycle; 3133a3d1a4eSLee Jones }; 3143a3d1a4eSLee Jones 3150bcf168bSTushar Behera #if IS_ENABLED(CONFIG_PWM) 3165ec803edSBoris Brezillon /* PWM user APIs */ 3175ec803edSBoris Brezillon struct pwm_device *pwm_request(int pwm_id, const char *label); 3185ec803edSBoris Brezillon void pwm_free(struct pwm_device *pwm); 3195ec803edSBoris Brezillon int pwm_apply_state(struct pwm_device *pwm, struct pwm_state *state); 3205ec803edSBoris Brezillon int pwm_adjust_config(struct pwm_device *pwm); 3215ec803edSBoris Brezillon 3225ec803edSBoris Brezillon /** 3235ec803edSBoris Brezillon * pwm_config() - change a PWM device configuration 3245ec803edSBoris Brezillon * @pwm: PWM device 3255ec803edSBoris Brezillon * @duty_ns: "on" time (in nanoseconds) 3265ec803edSBoris Brezillon * @period_ns: duration (in nanoseconds) of one cycle 3275ec803edSBoris Brezillon * 3285ec803edSBoris Brezillon * Returns: 0 on success or a negative error code on failure. 3295ec803edSBoris Brezillon */ 3305ec803edSBoris Brezillon static inline int pwm_config(struct pwm_device *pwm, int duty_ns, 3315ec803edSBoris Brezillon int period_ns) 3325ec803edSBoris Brezillon { 3335ec803edSBoris Brezillon struct pwm_state state; 3345ec803edSBoris Brezillon 3355ec803edSBoris Brezillon if (!pwm) 3365ec803edSBoris Brezillon return -EINVAL; 3375ec803edSBoris Brezillon 338ef2bf499SBrian Norris if (duty_ns < 0 || period_ns < 0) 339ef2bf499SBrian Norris return -EINVAL; 340ef2bf499SBrian Norris 3415ec803edSBoris Brezillon pwm_get_state(pwm, &state); 3425ec803edSBoris Brezillon if (state.duty_cycle == duty_ns && state.period == period_ns) 3435ec803edSBoris Brezillon return 0; 3445ec803edSBoris Brezillon 3455ec803edSBoris Brezillon state.duty_cycle = duty_ns; 3465ec803edSBoris Brezillon state.period = period_ns; 3475ec803edSBoris Brezillon return pwm_apply_state(pwm, &state); 3485ec803edSBoris Brezillon } 3495ec803edSBoris Brezillon 3505ec803edSBoris Brezillon /** 3515ec803edSBoris Brezillon * pwm_set_polarity() - configure the polarity of a PWM signal 3525ec803edSBoris Brezillon * @pwm: PWM device 3535ec803edSBoris Brezillon * @polarity: new polarity of the PWM signal 3545ec803edSBoris Brezillon * 3555ec803edSBoris Brezillon * Note that the polarity cannot be configured while the PWM device is 3565ec803edSBoris Brezillon * enabled. 3575ec803edSBoris Brezillon * 3585ec803edSBoris Brezillon * Returns: 0 on success or a negative error code on failure. 3595ec803edSBoris Brezillon */ 3605ec803edSBoris Brezillon static inline int pwm_set_polarity(struct pwm_device *pwm, 3615ec803edSBoris Brezillon enum pwm_polarity polarity) 3625ec803edSBoris Brezillon { 3635ec803edSBoris Brezillon struct pwm_state state; 3645ec803edSBoris Brezillon 3655ec803edSBoris Brezillon if (!pwm) 3665ec803edSBoris Brezillon return -EINVAL; 3675ec803edSBoris Brezillon 3685ec803edSBoris Brezillon pwm_get_state(pwm, &state); 3695ec803edSBoris Brezillon if (state.polarity == polarity) 3705ec803edSBoris Brezillon return 0; 3715ec803edSBoris Brezillon 3725ec803edSBoris Brezillon /* 3735ec803edSBoris Brezillon * Changing the polarity of a running PWM without adjusting the 3745ec803edSBoris Brezillon * dutycycle/period value is a bit risky (can introduce glitches). 3755ec803edSBoris Brezillon * Return -EBUSY in this case. 3765ec803edSBoris Brezillon * Note that this is allowed when using pwm_apply_state() because 3775ec803edSBoris Brezillon * the user specifies all the parameters. 3785ec803edSBoris Brezillon */ 3795ec803edSBoris Brezillon if (state.enabled) 3805ec803edSBoris Brezillon return -EBUSY; 3815ec803edSBoris Brezillon 3825ec803edSBoris Brezillon state.polarity = polarity; 3835ec803edSBoris Brezillon return pwm_apply_state(pwm, &state); 3845ec803edSBoris Brezillon } 3855ec803edSBoris Brezillon 3865ec803edSBoris Brezillon /** 3875ec803edSBoris Brezillon * pwm_enable() - start a PWM output toggling 3885ec803edSBoris Brezillon * @pwm: PWM device 3895ec803edSBoris Brezillon * 3905ec803edSBoris Brezillon * Returns: 0 on success or a negative error code on failure. 3915ec803edSBoris Brezillon */ 3925ec803edSBoris Brezillon static inline int pwm_enable(struct pwm_device *pwm) 3935ec803edSBoris Brezillon { 3945ec803edSBoris Brezillon struct pwm_state state; 3955ec803edSBoris Brezillon 3965ec803edSBoris Brezillon if (!pwm) 3975ec803edSBoris Brezillon return -EINVAL; 3985ec803edSBoris Brezillon 3995ec803edSBoris Brezillon pwm_get_state(pwm, &state); 4005ec803edSBoris Brezillon if (state.enabled) 4015ec803edSBoris Brezillon return 0; 4025ec803edSBoris Brezillon 4035ec803edSBoris Brezillon state.enabled = true; 4045ec803edSBoris Brezillon return pwm_apply_state(pwm, &state); 4055ec803edSBoris Brezillon } 4065ec803edSBoris Brezillon 4075ec803edSBoris Brezillon /** 4085ec803edSBoris Brezillon * pwm_disable() - stop a PWM output toggling 4095ec803edSBoris Brezillon * @pwm: PWM device 4105ec803edSBoris Brezillon */ 4115ec803edSBoris Brezillon static inline void pwm_disable(struct pwm_device *pwm) 4125ec803edSBoris Brezillon { 4135ec803edSBoris Brezillon struct pwm_state state; 4145ec803edSBoris Brezillon 4155ec803edSBoris Brezillon if (!pwm) 4165ec803edSBoris Brezillon return; 4175ec803edSBoris Brezillon 4185ec803edSBoris Brezillon pwm_get_state(pwm, &state); 4195ec803edSBoris Brezillon if (!state.enabled) 4205ec803edSBoris Brezillon return; 4215ec803edSBoris Brezillon 4225ec803edSBoris Brezillon state.enabled = false; 4235ec803edSBoris Brezillon pwm_apply_state(pwm, &state); 4245ec803edSBoris Brezillon } 4255ec803edSBoris Brezillon 4265ec803edSBoris Brezillon /* PWM provider APIs */ 4273a3d1a4eSLee Jones int pwm_capture(struct pwm_device *pwm, struct pwm_capture *result, 4283a3d1a4eSLee Jones unsigned long timeout); 429f051c466SThierry Reding int pwm_set_chip_data(struct pwm_device *pwm, void *data); 430f051c466SThierry Reding void *pwm_get_chip_data(struct pwm_device *pwm); 431f051c466SThierry Reding 432b6a00faeSTim Kryger int pwmchip_add_with_polarity(struct pwm_chip *chip, 433b6a00faeSTim Kryger enum pwm_polarity polarity); 4340c2498f1SSascha Hauer int pwmchip_add(struct pwm_chip *chip); 4350c2498f1SSascha Hauer int pwmchip_remove(struct pwm_chip *chip); 436f051c466SThierry Reding struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip, 437f051c466SThierry Reding unsigned int index, 438f051c466SThierry Reding const char *label); 4398138d2ddSThierry Reding 44083af2402SPhilip, Avinash struct pwm_device *of_pwm_xlate_with_flags(struct pwm_chip *pc, 44183af2402SPhilip, Avinash const struct of_phandle_args *args); 44283af2402SPhilip, Avinash 443d4c0c470SPeter Ujfalusi struct pwm_device *pwm_get(struct device *dev, const char *con_id); 4448eb96127SPeter Ujfalusi struct pwm_device *of_pwm_get(struct device_node *np, const char *con_id); 4458138d2ddSThierry Reding void pwm_put(struct pwm_device *pwm); 4468138d2ddSThierry Reding 447d4c0c470SPeter Ujfalusi struct pwm_device *devm_pwm_get(struct device *dev, const char *con_id); 448261a5eddSPeter Ujfalusi struct pwm_device *devm_of_pwm_get(struct device *dev, struct device_node *np, 449261a5eddSPeter Ujfalusi const char *con_id); 4506354316dSAlexandre Courbot void devm_pwm_put(struct device *dev, struct pwm_device *pwm); 4510bcf168bSTushar Behera #else 4525ec803edSBoris Brezillon static inline struct pwm_device *pwm_request(int pwm_id, const char *label) 4535ec803edSBoris Brezillon { 4545ec803edSBoris Brezillon return ERR_PTR(-ENODEV); 4555ec803edSBoris Brezillon } 4565ec803edSBoris Brezillon 4575ec803edSBoris Brezillon static inline void pwm_free(struct pwm_device *pwm) 4585ec803edSBoris Brezillon { 4595ec803edSBoris Brezillon } 4605ec803edSBoris Brezillon 4615ec803edSBoris Brezillon static inline int pwm_apply_state(struct pwm_device *pwm, 4625ec803edSBoris Brezillon const struct pwm_state *state) 4635ec803edSBoris Brezillon { 4645ec803edSBoris Brezillon return -ENOTSUPP; 4655ec803edSBoris Brezillon } 4665ec803edSBoris Brezillon 4675ec803edSBoris Brezillon static inline int pwm_adjust_config(struct pwm_device *pwm) 4685ec803edSBoris Brezillon { 4695ec803edSBoris Brezillon return -ENOTSUPP; 4705ec803edSBoris Brezillon } 4715ec803edSBoris Brezillon 4725ec803edSBoris Brezillon static inline int pwm_config(struct pwm_device *pwm, int duty_ns, 4735ec803edSBoris Brezillon int period_ns) 4745ec803edSBoris Brezillon { 4755ec803edSBoris Brezillon return -EINVAL; 4765ec803edSBoris Brezillon } 4775ec803edSBoris Brezillon 4783a3d1a4eSLee Jones static inline int pwm_capture(struct pwm_device *pwm, 4793a3d1a4eSLee Jones struct pwm_capture *result, 4803a3d1a4eSLee Jones unsigned long timeout) 4813a3d1a4eSLee Jones { 4823a3d1a4eSLee Jones return -EINVAL; 4833a3d1a4eSLee Jones } 4843a3d1a4eSLee Jones 4855ec803edSBoris Brezillon static inline int pwm_set_polarity(struct pwm_device *pwm, 4865ec803edSBoris Brezillon enum pwm_polarity polarity) 4875ec803edSBoris Brezillon { 4885ec803edSBoris Brezillon return -ENOTSUPP; 4895ec803edSBoris Brezillon } 4905ec803edSBoris Brezillon 4915ec803edSBoris Brezillon static inline int pwm_enable(struct pwm_device *pwm) 4925ec803edSBoris Brezillon { 4935ec803edSBoris Brezillon return -EINVAL; 4945ec803edSBoris Brezillon } 4955ec803edSBoris Brezillon 4965ec803edSBoris Brezillon static inline void pwm_disable(struct pwm_device *pwm) 4975ec803edSBoris Brezillon { 4985ec803edSBoris Brezillon } 4995ec803edSBoris Brezillon 5000bcf168bSTushar Behera static inline int pwm_set_chip_data(struct pwm_device *pwm, void *data) 5010bcf168bSTushar Behera { 5020bcf168bSTushar Behera return -EINVAL; 5030bcf168bSTushar Behera } 5040bcf168bSTushar Behera 5050bcf168bSTushar Behera static inline void *pwm_get_chip_data(struct pwm_device *pwm) 5060bcf168bSTushar Behera { 5070bcf168bSTushar Behera return NULL; 5080bcf168bSTushar Behera } 5090bcf168bSTushar Behera 5100bcf168bSTushar Behera static inline int pwmchip_add(struct pwm_chip *chip) 5110bcf168bSTushar Behera { 5120bcf168bSTushar Behera return -EINVAL; 5130bcf168bSTushar Behera } 5140bcf168bSTushar Behera 515b6a00faeSTim Kryger static inline int pwmchip_add_inversed(struct pwm_chip *chip) 516b6a00faeSTim Kryger { 517b6a00faeSTim Kryger return -EINVAL; 518b6a00faeSTim Kryger } 519b6a00faeSTim Kryger 5200bcf168bSTushar Behera static inline int pwmchip_remove(struct pwm_chip *chip) 5210bcf168bSTushar Behera { 5220bcf168bSTushar Behera return -EINVAL; 5230bcf168bSTushar Behera } 5240bcf168bSTushar Behera 5250bcf168bSTushar Behera static inline struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip, 5260bcf168bSTushar Behera unsigned int index, 5270bcf168bSTushar Behera const char *label) 5280bcf168bSTushar Behera { 5290bcf168bSTushar Behera return ERR_PTR(-ENODEV); 5300bcf168bSTushar Behera } 5310bcf168bSTushar Behera 5320bcf168bSTushar Behera static inline struct pwm_device *pwm_get(struct device *dev, 5330bcf168bSTushar Behera const char *consumer) 5340bcf168bSTushar Behera { 5350bcf168bSTushar Behera return ERR_PTR(-ENODEV); 5360bcf168bSTushar Behera } 5370bcf168bSTushar Behera 5388eb96127SPeter Ujfalusi static inline struct pwm_device *of_pwm_get(struct device_node *np, 5398eb96127SPeter Ujfalusi const char *con_id) 5408eb96127SPeter Ujfalusi { 5418eb96127SPeter Ujfalusi return ERR_PTR(-ENODEV); 5428eb96127SPeter Ujfalusi } 5438eb96127SPeter Ujfalusi 5440bcf168bSTushar Behera static inline void pwm_put(struct pwm_device *pwm) 5450bcf168bSTushar Behera { 5460bcf168bSTushar Behera } 5470bcf168bSTushar Behera 5480bcf168bSTushar Behera static inline struct pwm_device *devm_pwm_get(struct device *dev, 5490bcf168bSTushar Behera const char *consumer) 5500bcf168bSTushar Behera { 5510bcf168bSTushar Behera return ERR_PTR(-ENODEV); 5520bcf168bSTushar Behera } 5530bcf168bSTushar Behera 554261a5eddSPeter Ujfalusi static inline struct pwm_device *devm_of_pwm_get(struct device *dev, 555261a5eddSPeter Ujfalusi struct device_node *np, 556261a5eddSPeter Ujfalusi const char *con_id) 557261a5eddSPeter Ujfalusi { 558261a5eddSPeter Ujfalusi return ERR_PTR(-ENODEV); 559261a5eddSPeter Ujfalusi } 560261a5eddSPeter Ujfalusi 5610bcf168bSTushar Behera static inline void devm_pwm_put(struct device *dev, struct pwm_device *pwm) 5620bcf168bSTushar Behera { 5630bcf168bSTushar Behera } 5640bcf168bSTushar Behera #endif 5656354316dSAlexandre Courbot 5665ec803edSBoris Brezillon static inline void pwm_apply_args(struct pwm_device *pwm) 5675ec803edSBoris Brezillon { 56833cdcee0SBoris Brezillon struct pwm_state state = { }; 56933cdcee0SBoris Brezillon 5705ec803edSBoris Brezillon /* 5715ec803edSBoris Brezillon * PWM users calling pwm_apply_args() expect to have a fresh config 5725ec803edSBoris Brezillon * where the polarity and period are set according to pwm_args info. 5735ec803edSBoris Brezillon * The problem is, polarity can only be changed when the PWM is 5745ec803edSBoris Brezillon * disabled. 5755ec803edSBoris Brezillon * 5765ec803edSBoris Brezillon * PWM drivers supporting hardware readout may declare the PWM device 5775ec803edSBoris Brezillon * as enabled, and prevent polarity setting, which changes from the 5785ec803edSBoris Brezillon * existing behavior, where all PWM devices are declared as disabled 5795ec803edSBoris Brezillon * at startup (even if they are actually enabled), thus authorizing 5805ec803edSBoris Brezillon * polarity setting. 5815ec803edSBoris Brezillon * 58233cdcee0SBoris Brezillon * To fulfill this requirement, we apply a new state which disables 58333cdcee0SBoris Brezillon * the PWM device and set the reference period and polarity config. 5845ec803edSBoris Brezillon * 5855ec803edSBoris Brezillon * Note that PWM users requiring a smooth handover between the 5865ec803edSBoris Brezillon * bootloader and the kernel (like critical regulators controlled by 5875ec803edSBoris Brezillon * PWM devices) will have to switch to the atomic API and avoid calling 5885ec803edSBoris Brezillon * pwm_apply_args(). 5895ec803edSBoris Brezillon */ 59033cdcee0SBoris Brezillon 59133cdcee0SBoris Brezillon state.enabled = false; 59233cdcee0SBoris Brezillon state.polarity = pwm->args.polarity; 59333cdcee0SBoris Brezillon state.period = pwm->args.period; 59433cdcee0SBoris Brezillon 59533cdcee0SBoris Brezillon pwm_apply_state(pwm, &state); 5965ec803edSBoris Brezillon } 5975ec803edSBoris Brezillon 5988138d2ddSThierry Reding struct pwm_lookup { 5998138d2ddSThierry Reding struct list_head list; 6008138d2ddSThierry Reding const char *provider; 6018138d2ddSThierry Reding unsigned int index; 6028138d2ddSThierry Reding const char *dev_id; 6038138d2ddSThierry Reding const char *con_id; 6043796ce1dSAlexandre Belloni unsigned int period; 6053796ce1dSAlexandre Belloni enum pwm_polarity polarity; 606*b526a314SHans de Goede const char *module; /* optional, may be NULL */ 6078138d2ddSThierry Reding }; 6088138d2ddSThierry Reding 609*b526a314SHans de Goede #define PWM_LOOKUP_WITH_MODULE(_provider, _index, _dev_id, _con_id, \ 610*b526a314SHans de Goede _period, _polarity, _module) \ 6118138d2ddSThierry Reding { \ 6128138d2ddSThierry Reding .provider = _provider, \ 6138138d2ddSThierry Reding .index = _index, \ 6148138d2ddSThierry Reding .dev_id = _dev_id, \ 6158138d2ddSThierry Reding .con_id = _con_id, \ 61642844029SAlexandre Belloni .period = _period, \ 617*b526a314SHans de Goede .polarity = _polarity, \ 618*b526a314SHans de Goede .module = _module, \ 6198138d2ddSThierry Reding } 6208138d2ddSThierry Reding 621*b526a314SHans de Goede #define PWM_LOOKUP(_provider, _index, _dev_id, _con_id, _period, _polarity) \ 622*b526a314SHans de Goede PWM_LOOKUP_WITH_MODULE(_provider, _index, _dev_id, _con_id, _period, \ 623*b526a314SHans de Goede _polarity, NULL) 624*b526a314SHans de Goede 6250bcf168bSTushar Behera #if IS_ENABLED(CONFIG_PWM) 6268138d2ddSThierry Reding void pwm_add_table(struct pwm_lookup *table, size_t num); 627efb0de55SShobhit Kumar void pwm_remove_table(struct pwm_lookup *table, size_t num); 6280bcf168bSTushar Behera #else 6290bcf168bSTushar Behera static inline void pwm_add_table(struct pwm_lookup *table, size_t num) 6300bcf168bSTushar Behera { 6310bcf168bSTushar Behera } 632efb0de55SShobhit Kumar 633efb0de55SShobhit Kumar static inline void pwm_remove_table(struct pwm_lookup *table, size_t num) 634efb0de55SShobhit Kumar { 635efb0de55SShobhit Kumar } 6360c2498f1SSascha Hauer #endif 6370c2498f1SSascha Hauer 63876abbddeSH Hartley Sweeten #ifdef CONFIG_PWM_SYSFS 63976abbddeSH Hartley Sweeten void pwmchip_sysfs_export(struct pwm_chip *chip); 64076abbddeSH Hartley Sweeten void pwmchip_sysfs_unexport(struct pwm_chip *chip); 6410733424cSDavid Hsu void pwmchip_sysfs_unexport_children(struct pwm_chip *chip); 64276abbddeSH Hartley Sweeten #else 64376abbddeSH Hartley Sweeten static inline void pwmchip_sysfs_export(struct pwm_chip *chip) 64476abbddeSH Hartley Sweeten { 64576abbddeSH Hartley Sweeten } 64676abbddeSH Hartley Sweeten 64776abbddeSH Hartley Sweeten static inline void pwmchip_sysfs_unexport(struct pwm_chip *chip) 64876abbddeSH Hartley Sweeten { 64976abbddeSH Hartley Sweeten } 6500733424cSDavid Hsu 6510733424cSDavid Hsu static inline void pwmchip_sysfs_unexport_children(struct pwm_chip *chip) 6520733424cSDavid Hsu { 6530733424cSDavid Hsu } 65476abbddeSH Hartley Sweeten #endif /* CONFIG_PWM_SYSFS */ 65576abbddeSH Hartley Sweeten 6565243ef8bSMark Vels #endif /* __LINUX_PWM_H */ 657