1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */ 21a189b97SRussell King #ifndef __LINUX_PWM_H 31a189b97SRussell King #define __LINUX_PWM_H 41a189b97SRussell King 50bcf168bSTushar Behera #include <linux/err.h> 6d1cd2142SJonathan Richardson #include <linux/mutex.h> 77299ab70SThierry Reding #include <linux/of.h> 87299ab70SThierry Reding 90c2498f1SSascha Hauer struct pwm_chip; 100c2498f1SSascha Hauer 110aa0869cSPhilip, Avinash /** 120aa0869cSPhilip, Avinash * enum pwm_polarity - polarity of a PWM signal 130aa0869cSPhilip, Avinash * @PWM_POLARITY_NORMAL: a high signal for the duration of the duty- 140aa0869cSPhilip, Avinash * cycle, followed by a low signal for the remainder of the pulse 150aa0869cSPhilip, Avinash * period 160aa0869cSPhilip, Avinash * @PWM_POLARITY_INVERSED: a low signal for the duration of the duty- 170aa0869cSPhilip, Avinash * cycle, followed by a high signal for the remainder of the pulse 180aa0869cSPhilip, Avinash * period 190aa0869cSPhilip, Avinash */ 200aa0869cSPhilip, Avinash enum pwm_polarity { 210aa0869cSPhilip, Avinash PWM_POLARITY_NORMAL, 220aa0869cSPhilip, Avinash PWM_POLARITY_INVERSED, 230aa0869cSPhilip, Avinash }; 240aa0869cSPhilip, Avinash 25e39c0df1SBoris Brezillon /** 26e39c0df1SBoris Brezillon * struct pwm_args - board-dependent PWM arguments 27e39c0df1SBoris Brezillon * @period: reference period 28e39c0df1SBoris Brezillon * @polarity: reference polarity 29e39c0df1SBoris Brezillon * 30e39c0df1SBoris Brezillon * This structure describes board-dependent arguments attached to a PWM 31e39c0df1SBoris Brezillon * device. These arguments are usually retrieved from the PWM lookup table or 32e39c0df1SBoris Brezillon * device tree. 33e39c0df1SBoris Brezillon * 34e39c0df1SBoris Brezillon * Do not confuse this with the PWM state: PWM arguments represent the initial 35e39c0df1SBoris Brezillon * configuration that users want to use on this PWM device rather than the 36e39c0df1SBoris Brezillon * current PWM hardware state. 37e39c0df1SBoris Brezillon */ 38e39c0df1SBoris Brezillon struct pwm_args { 39a9d887dcSGuru Das Srinagesh u64 period; 40e39c0df1SBoris Brezillon enum pwm_polarity polarity; 41e39c0df1SBoris Brezillon }; 42e39c0df1SBoris Brezillon 43f051c466SThierry Reding enum { 44*45d0a298SDan Carpenter PWMF_REQUESTED = 0, 45*45d0a298SDan Carpenter PWMF_EXPORTED = 1, 46f051c466SThierry Reding }; 47f051c466SThierry Reding 4843a276b0SBoris Brezillon /* 4943a276b0SBoris Brezillon * struct pwm_state - state of a PWM channel 5043a276b0SBoris Brezillon * @period: PWM period (in nanoseconds) 5143a276b0SBoris Brezillon * @duty_cycle: PWM duty cycle (in nanoseconds) 5243a276b0SBoris Brezillon * @polarity: PWM polarity 5309a7e4a3SBoris Brezillon * @enabled: PWM enabled status 549e40ee18SClemens Gruber * @usage_power: If set, the PWM driver is only required to maintain the power 559e40ee18SClemens Gruber * output but has more freedom regarding signal form. 569e40ee18SClemens Gruber * If supported, the signal can be optimized, for example to 579e40ee18SClemens Gruber * improve EMI by phase shifting individual channels. 5843a276b0SBoris Brezillon */ 5943a276b0SBoris Brezillon struct pwm_state { 60a9d887dcSGuru Das Srinagesh u64 period; 61a9d887dcSGuru Das Srinagesh u64 duty_cycle; 6243a276b0SBoris Brezillon enum pwm_polarity polarity; 6309a7e4a3SBoris Brezillon bool enabled; 649e40ee18SClemens Gruber bool usage_power; 6543a276b0SBoris Brezillon }; 6643a276b0SBoris Brezillon 6704883802SThierry Reding /** 6804883802SThierry Reding * struct pwm_device - PWM channel object 6904883802SThierry Reding * @label: name of the PWM device 7004883802SThierry Reding * @flags: flags associated with the PWM device 7104883802SThierry Reding * @hwpwm: per-chip relative index of the PWM device 7204883802SThierry Reding * @pwm: global index of the PWM device 7304883802SThierry Reding * @chip: PWM chip providing this PWM device 7404883802SThierry Reding * @chip_data: chip-private data associated with the PWM device 75e39c0df1SBoris Brezillon * @args: PWM arguments 763ad1f3a3SUwe Kleine-König * @state: last applied state 773ad1f3a3SUwe Kleine-König * @last: last implemented state (for PWM_DEBUG) 7804883802SThierry Reding */ 79f051c466SThierry Reding struct pwm_device { 80f051c466SThierry Reding const char *label; 81f051c466SThierry Reding unsigned long flags; 82f051c466SThierry Reding unsigned int hwpwm; 83f051c466SThierry Reding unsigned int pwm; 84f051c466SThierry Reding struct pwm_chip *chip; 85f051c466SThierry Reding void *chip_data; 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 981a7a6e80SUwe Kleine-König * pwm_apply_state(). Drivers may have to slightly tweak that state before programming it to 991a7a6e80SUwe Kleine-König * hardware. If pwm_apply_state() 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 void pwm_set_period(struct pwm_device *pwm, u64 period) 118f051c466SThierry Reding { 119f051c466SThierry Reding if (pwm) 12043a276b0SBoris Brezillon pwm->state.period = period; 121f051c466SThierry Reding } 122f051c466SThierry Reding 123a9d887dcSGuru Das Srinagesh static inline u64 pwm_get_period(const struct pwm_device *pwm) 124f051c466SThierry Reding { 12543a276b0SBoris Brezillon struct pwm_state state; 12643a276b0SBoris Brezillon 12743a276b0SBoris Brezillon pwm_get_state(pwm, &state); 12843a276b0SBoris Brezillon 12943a276b0SBoris Brezillon return state.period; 130f051c466SThierry Reding } 131f051c466SThierry Reding 13276abbddeSH Hartley Sweeten static inline void pwm_set_duty_cycle(struct pwm_device *pwm, unsigned int duty) 13376abbddeSH Hartley Sweeten { 13476abbddeSH Hartley Sweeten if (pwm) 13543a276b0SBoris Brezillon pwm->state.duty_cycle = duty; 13676abbddeSH Hartley Sweeten } 13776abbddeSH Hartley Sweeten 138a9d887dcSGuru Das Srinagesh static inline u64 pwm_get_duty_cycle(const struct pwm_device *pwm) 13976abbddeSH Hartley Sweeten { 14043a276b0SBoris Brezillon struct pwm_state state; 14143a276b0SBoris Brezillon 14243a276b0SBoris Brezillon pwm_get_state(pwm, &state); 14343a276b0SBoris Brezillon 14443a276b0SBoris Brezillon return state.duty_cycle; 14576abbddeSH Hartley Sweeten } 14676abbddeSH Hartley Sweeten 147011e7631SBoris Brezillon static inline enum pwm_polarity pwm_get_polarity(const struct pwm_device *pwm) 148011e7631SBoris Brezillon { 14943a276b0SBoris Brezillon struct pwm_state state; 15043a276b0SBoris Brezillon 15143a276b0SBoris Brezillon pwm_get_state(pwm, &state); 15243a276b0SBoris Brezillon 15343a276b0SBoris Brezillon return state.polarity; 154011e7631SBoris Brezillon } 155011e7631SBoris Brezillon 156e39c0df1SBoris Brezillon static inline void pwm_get_args(const struct pwm_device *pwm, 157e39c0df1SBoris Brezillon struct pwm_args *args) 158e39c0df1SBoris Brezillon { 159e39c0df1SBoris Brezillon *args = pwm->args; 160e39c0df1SBoris Brezillon } 161e39c0df1SBoris Brezillon 1620c2498f1SSascha Hauer /** 163a6a0dbbcSBoris Brezillon * pwm_init_state() - prepare a new state to be applied with pwm_apply_state() 164a6a0dbbcSBoris Brezillon * @pwm: PWM device 165a6a0dbbcSBoris Brezillon * @state: state to fill with the prepared PWM state 166a6a0dbbcSBoris Brezillon * 167a6a0dbbcSBoris Brezillon * This functions prepares a state that can later be tweaked and applied 168a6a0dbbcSBoris Brezillon * to the PWM device with pwm_apply_state(). This is a convenient function 169a6a0dbbcSBoris Brezillon * that first retrieves the current PWM state and the replaces the period 170a6a0dbbcSBoris Brezillon * and polarity fields with the reference values defined in pwm->args. 171a6a0dbbcSBoris Brezillon * Once the function returns, you can adjust the ->enabled and ->duty_cycle 172a6a0dbbcSBoris Brezillon * fields according to your needs before calling pwm_apply_state(). 173a6a0dbbcSBoris Brezillon * 174a6a0dbbcSBoris Brezillon * ->duty_cycle is initially set to zero to avoid cases where the current 175a6a0dbbcSBoris Brezillon * ->duty_cycle value exceed the pwm_args->period one, which would trigger 176a6a0dbbcSBoris Brezillon * an error if the user calls pwm_apply_state() without adjusting ->duty_cycle 177a6a0dbbcSBoris Brezillon * first. 178a6a0dbbcSBoris Brezillon */ 179a6a0dbbcSBoris Brezillon static inline void pwm_init_state(const struct pwm_device *pwm, 180a6a0dbbcSBoris Brezillon struct pwm_state *state) 181a6a0dbbcSBoris Brezillon { 182a6a0dbbcSBoris Brezillon struct pwm_args args; 183a6a0dbbcSBoris Brezillon 184a6a0dbbcSBoris Brezillon /* First get the current state. */ 185a6a0dbbcSBoris Brezillon pwm_get_state(pwm, state); 186a6a0dbbcSBoris Brezillon 187a6a0dbbcSBoris Brezillon /* Then fill it with the reference config */ 188a6a0dbbcSBoris Brezillon pwm_get_args(pwm, &args); 189a6a0dbbcSBoris Brezillon 190a6a0dbbcSBoris Brezillon state->period = args.period; 191a6a0dbbcSBoris Brezillon state->polarity = args.polarity; 192a6a0dbbcSBoris Brezillon state->duty_cycle = 0; 1939e40ee18SClemens Gruber state->usage_power = false; 194a6a0dbbcSBoris Brezillon } 195a6a0dbbcSBoris Brezillon 196a6a0dbbcSBoris Brezillon /** 197f6f3bddfSBoris Brezillon * pwm_get_relative_duty_cycle() - Get a relative duty cycle value 198f6f3bddfSBoris Brezillon * @state: PWM state to extract the duty cycle from 199f6f3bddfSBoris Brezillon * @scale: target scale of the relative duty cycle 200f6f3bddfSBoris Brezillon * 201f6f3bddfSBoris Brezillon * This functions converts the absolute duty cycle stored in @state (expressed 202f6f3bddfSBoris Brezillon * in nanosecond) into a value relative to the period. 203f6f3bddfSBoris Brezillon * 204f6f3bddfSBoris Brezillon * For example if you want to get the duty_cycle expressed in percent, call: 205f6f3bddfSBoris Brezillon * 206f6f3bddfSBoris Brezillon * pwm_get_state(pwm, &state); 207f6f3bddfSBoris Brezillon * duty = pwm_get_relative_duty_cycle(&state, 100); 208f6f3bddfSBoris Brezillon */ 209f6f3bddfSBoris Brezillon static inline unsigned int 210f6f3bddfSBoris Brezillon pwm_get_relative_duty_cycle(const struct pwm_state *state, unsigned int scale) 211f6f3bddfSBoris Brezillon { 212f6f3bddfSBoris Brezillon if (!state->period) 213f6f3bddfSBoris Brezillon return 0; 214f6f3bddfSBoris Brezillon 215f6f3bddfSBoris Brezillon return DIV_ROUND_CLOSEST_ULL((u64)state->duty_cycle * scale, 216f6f3bddfSBoris Brezillon state->period); 217f6f3bddfSBoris Brezillon } 218f6f3bddfSBoris Brezillon 219f6f3bddfSBoris Brezillon /** 220f6f3bddfSBoris Brezillon * pwm_set_relative_duty_cycle() - Set a relative duty cycle value 221f6f3bddfSBoris Brezillon * @state: PWM state to fill 222f6f3bddfSBoris Brezillon * @duty_cycle: relative duty cycle value 223f6f3bddfSBoris Brezillon * @scale: scale in which @duty_cycle is expressed 224f6f3bddfSBoris Brezillon * 225f6f3bddfSBoris Brezillon * This functions converts a relative into an absolute duty cycle (expressed 226f6f3bddfSBoris Brezillon * in nanoseconds), and puts the result in state->duty_cycle. 227f6f3bddfSBoris Brezillon * 228f6f3bddfSBoris Brezillon * For example if you want to configure a 50% duty cycle, call: 229f6f3bddfSBoris Brezillon * 230f6f3bddfSBoris Brezillon * pwm_init_state(pwm, &state); 231f6f3bddfSBoris Brezillon * pwm_set_relative_duty_cycle(&state, 50, 100); 232f6f3bddfSBoris Brezillon * pwm_apply_state(pwm, &state); 233f6f3bddfSBoris Brezillon * 234f6f3bddfSBoris Brezillon * This functions returns -EINVAL if @duty_cycle and/or @scale are 235f6f3bddfSBoris Brezillon * inconsistent (@scale == 0 or @duty_cycle > @scale). 236f6f3bddfSBoris Brezillon */ 237f6f3bddfSBoris Brezillon static inline int 238f6f3bddfSBoris Brezillon pwm_set_relative_duty_cycle(struct pwm_state *state, unsigned int duty_cycle, 239f6f3bddfSBoris Brezillon unsigned int scale) 240f6f3bddfSBoris Brezillon { 241f6f3bddfSBoris Brezillon if (!scale || duty_cycle > scale) 242f6f3bddfSBoris Brezillon return -EINVAL; 243f6f3bddfSBoris Brezillon 244f6f3bddfSBoris Brezillon state->duty_cycle = DIV_ROUND_CLOSEST_ULL((u64)duty_cycle * 245f6f3bddfSBoris Brezillon state->period, 246f6f3bddfSBoris Brezillon scale); 247f6f3bddfSBoris Brezillon 248f6f3bddfSBoris Brezillon return 0; 249f6f3bddfSBoris Brezillon } 250f6f3bddfSBoris Brezillon 251f6f3bddfSBoris Brezillon /** 252ef2e35d9SUwe Kleine-König * struct pwm_capture - PWM capture data 253ef2e35d9SUwe Kleine-König * @period: period of the PWM signal (in nanoseconds) 254ef2e35d9SUwe Kleine-König * @duty_cycle: duty cycle of the PWM signal (in nanoseconds) 255ef2e35d9SUwe Kleine-König */ 256ef2e35d9SUwe Kleine-König struct pwm_capture { 257ef2e35d9SUwe Kleine-König unsigned int period; 258ef2e35d9SUwe Kleine-König unsigned int duty_cycle; 259ef2e35d9SUwe Kleine-König }; 260ef2e35d9SUwe Kleine-König 261ef2e35d9SUwe Kleine-König /** 2620c2498f1SSascha Hauer * struct pwm_ops - PWM controller operations 2630c2498f1SSascha Hauer * @request: optional hook for requesting a PWM 2640c2498f1SSascha Hauer * @free: optional hook for freeing a PWM 2653a3d1a4eSLee Jones * @capture: capture and report PWM signal 26627938fd8SRasmus Villemoes * @apply: atomically apply a new PWM config 26715fa8a43SBoris Brezillon * @get_state: get the current PWM state. This function is only 26815fa8a43SBoris Brezillon * called once per PWM device when the PWM chip is 26915fa8a43SBoris Brezillon * registered. 2700c2498f1SSascha Hauer * @owner: helps prevent removal of modules exporting active PWMs 2710c2498f1SSascha Hauer */ 2720c2498f1SSascha Hauer struct pwm_ops { 2736bc7064aSThierry Reding int (*request)(struct pwm_chip *chip, struct pwm_device *pwm); 2746bc7064aSThierry Reding void (*free)(struct pwm_chip *chip, struct pwm_device *pwm); 2753a3d1a4eSLee Jones int (*capture)(struct pwm_chip *chip, struct pwm_device *pwm, 2763a3d1a4eSLee Jones struct pwm_capture *result, unsigned long timeout); 2775ec803edSBoris Brezillon int (*apply)(struct pwm_chip *chip, struct pwm_device *pwm, 27871523d18SUwe Kleine-König const struct pwm_state *state); 2796c452cffSUwe Kleine-König int (*get_state)(struct pwm_chip *chip, struct pwm_device *pwm, 28015fa8a43SBoris Brezillon struct pwm_state *state); 2810c2498f1SSascha Hauer struct module *owner; 2820c2498f1SSascha Hauer }; 2830c2498f1SSascha Hauer 2840c2498f1SSascha Hauer /** 285f051c466SThierry Reding * struct pwm_chip - abstract a PWM controller 286f051c466SThierry Reding * @dev: device providing the PWMs 287f051c466SThierry Reding * @ops: callbacks for this PWM controller 288f051c466SThierry Reding * @base: number of first PWM controlled by this chip 289f051c466SThierry Reding * @npwm: number of PWMs controlled by this chip 29004883802SThierry Reding * @of_xlate: request a PWM device given a device tree PWM specifier 29104883802SThierry Reding * @of_pwm_n_cells: number of cells expected in the device tree PWM specifier 2925d0a4c11SUwe Kleine-König * @list: list node for internal use 2935d0a4c11SUwe Kleine-König * @pwms: array of PWM devices allocated by the framework 2940c2498f1SSascha Hauer */ 2950c2498f1SSascha Hauer struct pwm_chip { 296f051c466SThierry Reding struct device *dev; 297f051c466SThierry Reding const struct pwm_ops *ops; 298f051c466SThierry Reding int base; 299f051c466SThierry Reding unsigned int npwm; 300f051c466SThierry Reding 301b4f78ff7SUwe Kleine-König struct pwm_device * (*of_xlate)(struct pwm_chip *chip, 3027299ab70SThierry Reding const struct of_phandle_args *args); 3037299ab70SThierry Reding unsigned int of_pwm_n_cells; 3045d0a4c11SUwe Kleine-König 3055d0a4c11SUwe Kleine-König /* only used internally by the PWM framework */ 3065d0a4c11SUwe Kleine-König struct list_head list; 3075d0a4c11SUwe Kleine-König struct pwm_device *pwms; 3080c2498f1SSascha Hauer }; 3090c2498f1SSascha Hauer 3100bcf168bSTushar Behera #if IS_ENABLED(CONFIG_PWM) 3115ec803edSBoris Brezillon /* PWM user APIs */ 31271523d18SUwe Kleine-König int pwm_apply_state(struct pwm_device *pwm, const struct pwm_state *state); 3135ec803edSBoris Brezillon int pwm_adjust_config(struct pwm_device *pwm); 3145ec803edSBoris Brezillon 3155ec803edSBoris Brezillon /** 3165ec803edSBoris Brezillon * pwm_config() - change a PWM device configuration 3175ec803edSBoris Brezillon * @pwm: PWM device 3185ec803edSBoris Brezillon * @duty_ns: "on" time (in nanoseconds) 3195ec803edSBoris Brezillon * @period_ns: duration (in nanoseconds) of one cycle 3205ec803edSBoris Brezillon * 3215ec803edSBoris Brezillon * Returns: 0 on success or a negative error code on failure. 3225ec803edSBoris Brezillon */ 3235ec803edSBoris Brezillon static inline int pwm_config(struct pwm_device *pwm, int duty_ns, 3245ec803edSBoris Brezillon int period_ns) 3255ec803edSBoris Brezillon { 3265ec803edSBoris Brezillon struct pwm_state state; 3275ec803edSBoris Brezillon 3285ec803edSBoris Brezillon if (!pwm) 3295ec803edSBoris Brezillon return -EINVAL; 3305ec803edSBoris Brezillon 331ef2bf499SBrian Norris if (duty_ns < 0 || period_ns < 0) 332ef2bf499SBrian Norris return -EINVAL; 333ef2bf499SBrian Norris 3345ec803edSBoris Brezillon pwm_get_state(pwm, &state); 3355ec803edSBoris Brezillon if (state.duty_cycle == duty_ns && state.period == period_ns) 3365ec803edSBoris Brezillon return 0; 3375ec803edSBoris Brezillon 3385ec803edSBoris Brezillon state.duty_cycle = duty_ns; 3395ec803edSBoris Brezillon state.period = period_ns; 3405ec803edSBoris Brezillon return pwm_apply_state(pwm, &state); 3415ec803edSBoris Brezillon } 3425ec803edSBoris Brezillon 3435ec803edSBoris Brezillon /** 3445ec803edSBoris Brezillon * pwm_enable() - start a PWM output toggling 3455ec803edSBoris Brezillon * @pwm: PWM device 3465ec803edSBoris Brezillon * 3475ec803edSBoris Brezillon * Returns: 0 on success or a negative error code on failure. 3485ec803edSBoris Brezillon */ 3495ec803edSBoris Brezillon static inline int pwm_enable(struct pwm_device *pwm) 3505ec803edSBoris Brezillon { 3515ec803edSBoris Brezillon struct pwm_state state; 3525ec803edSBoris Brezillon 3535ec803edSBoris Brezillon if (!pwm) 3545ec803edSBoris Brezillon return -EINVAL; 3555ec803edSBoris Brezillon 3565ec803edSBoris Brezillon pwm_get_state(pwm, &state); 3575ec803edSBoris Brezillon if (state.enabled) 3585ec803edSBoris Brezillon return 0; 3595ec803edSBoris Brezillon 3605ec803edSBoris Brezillon state.enabled = true; 3615ec803edSBoris Brezillon return pwm_apply_state(pwm, &state); 3625ec803edSBoris Brezillon } 3635ec803edSBoris Brezillon 3645ec803edSBoris Brezillon /** 3655ec803edSBoris Brezillon * pwm_disable() - stop a PWM output toggling 3665ec803edSBoris Brezillon * @pwm: PWM device 3675ec803edSBoris Brezillon */ 3685ec803edSBoris Brezillon static inline void pwm_disable(struct pwm_device *pwm) 3695ec803edSBoris Brezillon { 3705ec803edSBoris Brezillon struct pwm_state state; 3715ec803edSBoris Brezillon 3725ec803edSBoris Brezillon if (!pwm) 3735ec803edSBoris Brezillon return; 3745ec803edSBoris Brezillon 3755ec803edSBoris Brezillon pwm_get_state(pwm, &state); 3765ec803edSBoris Brezillon if (!state.enabled) 3775ec803edSBoris Brezillon return; 3785ec803edSBoris Brezillon 3795ec803edSBoris Brezillon state.enabled = false; 3805ec803edSBoris Brezillon pwm_apply_state(pwm, &state); 3815ec803edSBoris Brezillon } 3825ec803edSBoris Brezillon 3835ec803edSBoris Brezillon /* PWM provider APIs */ 3843a3d1a4eSLee Jones int pwm_capture(struct pwm_device *pwm, struct pwm_capture *result, 3853a3d1a4eSLee Jones unsigned long timeout); 386f051c466SThierry Reding int pwm_set_chip_data(struct pwm_device *pwm, void *data); 387f051c466SThierry Reding void *pwm_get_chip_data(struct pwm_device *pwm); 388f051c466SThierry Reding 3890c2498f1SSascha Hauer int pwmchip_add(struct pwm_chip *chip); 3908083f58dSUwe Kleine-König void pwmchip_remove(struct pwm_chip *chip); 391bcda91bfSUwe Kleine-König 392bcda91bfSUwe Kleine-König int devm_pwmchip_add(struct device *dev, struct pwm_chip *chip); 393bcda91bfSUwe Kleine-König 394f051c466SThierry Reding struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip, 395f051c466SThierry Reding unsigned int index, 396f051c466SThierry Reding const char *label); 3978138d2ddSThierry Reding 398b4f78ff7SUwe Kleine-König struct pwm_device *of_pwm_xlate_with_flags(struct pwm_chip *chip, 39983af2402SPhilip, Avinash const struct of_phandle_args *args); 400b4f78ff7SUwe Kleine-König struct pwm_device *of_pwm_single_xlate(struct pwm_chip *chip, 4013ab7b6acSBjorn Andersson const struct of_phandle_args *args); 40283af2402SPhilip, Avinash 403d4c0c470SPeter Ujfalusi struct pwm_device *pwm_get(struct device *dev, const char *con_id); 4048138d2ddSThierry Reding void pwm_put(struct pwm_device *pwm); 4058138d2ddSThierry Reding 406d4c0c470SPeter Ujfalusi struct pwm_device *devm_pwm_get(struct device *dev, const char *con_id); 4074a6ef8e3SNikolaus Voss struct pwm_device *devm_fwnode_pwm_get(struct device *dev, 4084a6ef8e3SNikolaus Voss struct fwnode_handle *fwnode, 4094a6ef8e3SNikolaus Voss const char *con_id); 4100bcf168bSTushar Behera #else 4115ec803edSBoris Brezillon static inline int pwm_apply_state(struct pwm_device *pwm, 4125ec803edSBoris Brezillon const struct pwm_state *state) 4135ec803edSBoris Brezillon { 4144ad91a22SUwe Kleine-König might_sleep(); 4155ec803edSBoris Brezillon return -ENOTSUPP; 4165ec803edSBoris Brezillon } 4175ec803edSBoris Brezillon 4185ec803edSBoris Brezillon static inline int pwm_adjust_config(struct pwm_device *pwm) 4195ec803edSBoris Brezillon { 4205ec803edSBoris Brezillon return -ENOTSUPP; 4215ec803edSBoris Brezillon } 4225ec803edSBoris Brezillon 4235ec803edSBoris Brezillon static inline int pwm_config(struct pwm_device *pwm, int duty_ns, 4245ec803edSBoris Brezillon int period_ns) 4255ec803edSBoris Brezillon { 4264ad91a22SUwe Kleine-König might_sleep(); 4275ec803edSBoris Brezillon return -EINVAL; 4285ec803edSBoris Brezillon } 4295ec803edSBoris Brezillon 4305ec803edSBoris Brezillon static inline int pwm_enable(struct pwm_device *pwm) 4315ec803edSBoris Brezillon { 4324ad91a22SUwe Kleine-König might_sleep(); 4335ec803edSBoris Brezillon return -EINVAL; 4345ec803edSBoris Brezillon } 4355ec803edSBoris Brezillon 4365ec803edSBoris Brezillon static inline void pwm_disable(struct pwm_device *pwm) 4375ec803edSBoris Brezillon { 4384ad91a22SUwe Kleine-König might_sleep(); 4395ec803edSBoris Brezillon } 4405ec803edSBoris Brezillon 441b3c650adSGeert Uytterhoeven static inline int pwm_capture(struct pwm_device *pwm, 442b3c650adSGeert Uytterhoeven struct pwm_capture *result, 443b3c650adSGeert Uytterhoeven unsigned long timeout) 444b3c650adSGeert Uytterhoeven { 445b3c650adSGeert Uytterhoeven return -EINVAL; 446b3c650adSGeert Uytterhoeven } 447b3c650adSGeert Uytterhoeven 4480bcf168bSTushar Behera static inline int pwm_set_chip_data(struct pwm_device *pwm, void *data) 4490bcf168bSTushar Behera { 4500bcf168bSTushar Behera return -EINVAL; 4510bcf168bSTushar Behera } 4520bcf168bSTushar Behera 4530bcf168bSTushar Behera static inline void *pwm_get_chip_data(struct pwm_device *pwm) 4540bcf168bSTushar Behera { 4550bcf168bSTushar Behera return NULL; 4560bcf168bSTushar Behera } 4570bcf168bSTushar Behera 4580bcf168bSTushar Behera static inline int pwmchip_add(struct pwm_chip *chip) 4590bcf168bSTushar Behera { 4600bcf168bSTushar Behera return -EINVAL; 4610bcf168bSTushar Behera } 4620bcf168bSTushar Behera 4630bcf168bSTushar Behera static inline int pwmchip_remove(struct pwm_chip *chip) 4640bcf168bSTushar Behera { 4650bcf168bSTushar Behera return -EINVAL; 4660bcf168bSTushar Behera } 4670bcf168bSTushar Behera 46888da4e81SAndy Shevchenko static inline int devm_pwmchip_add(struct device *dev, struct pwm_chip *chip) 46988da4e81SAndy Shevchenko { 47088da4e81SAndy Shevchenko return -EINVAL; 47188da4e81SAndy Shevchenko } 47288da4e81SAndy Shevchenko 4730bcf168bSTushar Behera static inline struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip, 4740bcf168bSTushar Behera unsigned int index, 4750bcf168bSTushar Behera const char *label) 4760bcf168bSTushar Behera { 47727d9a4d6SUwe Kleine-König might_sleep(); 4780bcf168bSTushar Behera return ERR_PTR(-ENODEV); 4790bcf168bSTushar Behera } 4800bcf168bSTushar Behera 4810bcf168bSTushar Behera static inline struct pwm_device *pwm_get(struct device *dev, 4820bcf168bSTushar Behera const char *consumer) 4830bcf168bSTushar Behera { 48427d9a4d6SUwe Kleine-König might_sleep(); 4850bcf168bSTushar Behera return ERR_PTR(-ENODEV); 4860bcf168bSTushar Behera } 4870bcf168bSTushar Behera 4880bcf168bSTushar Behera static inline void pwm_put(struct pwm_device *pwm) 4890bcf168bSTushar Behera { 49027d9a4d6SUwe Kleine-König might_sleep(); 4910bcf168bSTushar Behera } 4920bcf168bSTushar Behera 4930bcf168bSTushar Behera static inline struct pwm_device *devm_pwm_get(struct device *dev, 4940bcf168bSTushar Behera const char *consumer) 4950bcf168bSTushar Behera { 49627d9a4d6SUwe Kleine-König might_sleep(); 4970bcf168bSTushar Behera return ERR_PTR(-ENODEV); 4980bcf168bSTushar Behera } 4990bcf168bSTushar Behera 5004a6ef8e3SNikolaus Voss static inline struct pwm_device * 5014a6ef8e3SNikolaus Voss devm_fwnode_pwm_get(struct device *dev, struct fwnode_handle *fwnode, 5024a6ef8e3SNikolaus Voss const char *con_id) 5034a6ef8e3SNikolaus Voss { 50427d9a4d6SUwe Kleine-König might_sleep(); 5054a6ef8e3SNikolaus Voss return ERR_PTR(-ENODEV); 5064a6ef8e3SNikolaus Voss } 5070bcf168bSTushar Behera #endif 5086354316dSAlexandre Courbot 5095ec803edSBoris Brezillon static inline void pwm_apply_args(struct pwm_device *pwm) 5105ec803edSBoris Brezillon { 51133cdcee0SBoris Brezillon struct pwm_state state = { }; 51233cdcee0SBoris Brezillon 5135ec803edSBoris Brezillon /* 5145ec803edSBoris Brezillon * PWM users calling pwm_apply_args() expect to have a fresh config 5155ec803edSBoris Brezillon * where the polarity and period are set according to pwm_args info. 5165ec803edSBoris Brezillon * The problem is, polarity can only be changed when the PWM is 5175ec803edSBoris Brezillon * disabled. 5185ec803edSBoris Brezillon * 5195ec803edSBoris Brezillon * PWM drivers supporting hardware readout may declare the PWM device 5205ec803edSBoris Brezillon * as enabled, and prevent polarity setting, which changes from the 5215ec803edSBoris Brezillon * existing behavior, where all PWM devices are declared as disabled 5225ec803edSBoris Brezillon * at startup (even if they are actually enabled), thus authorizing 5235ec803edSBoris Brezillon * polarity setting. 5245ec803edSBoris Brezillon * 52533cdcee0SBoris Brezillon * To fulfill this requirement, we apply a new state which disables 52633cdcee0SBoris Brezillon * the PWM device and set the reference period and polarity config. 5275ec803edSBoris Brezillon * 5285ec803edSBoris Brezillon * Note that PWM users requiring a smooth handover between the 5295ec803edSBoris Brezillon * bootloader and the kernel (like critical regulators controlled by 5305ec803edSBoris Brezillon * PWM devices) will have to switch to the atomic API and avoid calling 5315ec803edSBoris Brezillon * pwm_apply_args(). 5325ec803edSBoris Brezillon */ 53333cdcee0SBoris Brezillon 53433cdcee0SBoris Brezillon state.enabled = false; 53533cdcee0SBoris Brezillon state.polarity = pwm->args.polarity; 53633cdcee0SBoris Brezillon state.period = pwm->args.period; 5379e40ee18SClemens Gruber state.usage_power = false; 53833cdcee0SBoris Brezillon 53933cdcee0SBoris Brezillon pwm_apply_state(pwm, &state); 5405ec803edSBoris Brezillon } 5415ec803edSBoris Brezillon 5428138d2ddSThierry Reding struct pwm_lookup { 5438138d2ddSThierry Reding struct list_head list; 5448138d2ddSThierry Reding const char *provider; 5458138d2ddSThierry Reding unsigned int index; 5468138d2ddSThierry Reding const char *dev_id; 5478138d2ddSThierry Reding const char *con_id; 5483796ce1dSAlexandre Belloni unsigned int period; 5493796ce1dSAlexandre Belloni enum pwm_polarity polarity; 550b526a314SHans de Goede const char *module; /* optional, may be NULL */ 5518138d2ddSThierry Reding }; 5528138d2ddSThierry Reding 553b526a314SHans de Goede #define PWM_LOOKUP_WITH_MODULE(_provider, _index, _dev_id, _con_id, \ 554b526a314SHans de Goede _period, _polarity, _module) \ 5558138d2ddSThierry Reding { \ 5568138d2ddSThierry Reding .provider = _provider, \ 5578138d2ddSThierry Reding .index = _index, \ 5588138d2ddSThierry Reding .dev_id = _dev_id, \ 5598138d2ddSThierry Reding .con_id = _con_id, \ 56042844029SAlexandre Belloni .period = _period, \ 561b526a314SHans de Goede .polarity = _polarity, \ 562b526a314SHans de Goede .module = _module, \ 5638138d2ddSThierry Reding } 5648138d2ddSThierry Reding 565b526a314SHans de Goede #define PWM_LOOKUP(_provider, _index, _dev_id, _con_id, _period, _polarity) \ 566b526a314SHans de Goede PWM_LOOKUP_WITH_MODULE(_provider, _index, _dev_id, _con_id, _period, \ 567b526a314SHans de Goede _polarity, NULL) 568b526a314SHans de Goede 5690bcf168bSTushar Behera #if IS_ENABLED(CONFIG_PWM) 5708138d2ddSThierry Reding void pwm_add_table(struct pwm_lookup *table, size_t num); 571efb0de55SShobhit Kumar void pwm_remove_table(struct pwm_lookup *table, size_t num); 5720bcf168bSTushar Behera #else 5730bcf168bSTushar Behera static inline void pwm_add_table(struct pwm_lookup *table, size_t num) 5740bcf168bSTushar Behera { 5750bcf168bSTushar Behera } 576efb0de55SShobhit Kumar 577efb0de55SShobhit Kumar static inline void pwm_remove_table(struct pwm_lookup *table, size_t num) 578efb0de55SShobhit Kumar { 579efb0de55SShobhit Kumar } 5800c2498f1SSascha Hauer #endif 5810c2498f1SSascha Hauer 58276abbddeSH Hartley Sweeten #ifdef CONFIG_PWM_SYSFS 58376abbddeSH Hartley Sweeten void pwmchip_sysfs_export(struct pwm_chip *chip); 58476abbddeSH Hartley Sweeten void pwmchip_sysfs_unexport(struct pwm_chip *chip); 58576abbddeSH Hartley Sweeten #else 58676abbddeSH Hartley Sweeten static inline void pwmchip_sysfs_export(struct pwm_chip *chip) 58776abbddeSH Hartley Sweeten { 58876abbddeSH Hartley Sweeten } 58976abbddeSH Hartley Sweeten 59076abbddeSH Hartley Sweeten static inline void pwmchip_sysfs_unexport(struct pwm_chip *chip) 59176abbddeSH Hartley Sweeten { 59276abbddeSH Hartley Sweeten } 59376abbddeSH Hartley Sweeten #endif /* CONFIG_PWM_SYSFS */ 59476abbddeSH Hartley Sweeten 5955243ef8bSMark Vels #endif /* __LINUX_PWM_H */ 596