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 11*cdd30ebbSPeter Zijlstra 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 5217e40c25SUwe Kleine-König /** 5317e40c25SUwe Kleine-König * struct pwm_waveform - description of a PWM waveform 5417e40c25SUwe Kleine-König * @period_length_ns: PWM period 5517e40c25SUwe Kleine-König * @duty_length_ns: PWM duty cycle 5617e40c25SUwe Kleine-König * @duty_offset_ns: offset of the rising edge from the period's start 5717e40c25SUwe Kleine-König * 5817e40c25SUwe Kleine-König * This is a representation of a PWM waveform alternative to struct pwm_state 5917e40c25SUwe Kleine-König * below. It's more expressive than struct pwm_state as it contains a 6017e40c25SUwe Kleine-König * duty_offset_ns and so can represent offsets other than zero (with .polarity = 6117e40c25SUwe Kleine-König * PWM_POLARITY_NORMAL) and period - duty_cycle (.polarity = 6217e40c25SUwe Kleine-König * PWM_POLARITY_INVERSED). 6317e40c25SUwe Kleine-König * 6417e40c25SUwe Kleine-König * Note there is no explicit bool for enabled. A "disabled" PWM is represented 6517e40c25SUwe Kleine-König * by .period_length_ns = 0. Note further that the behaviour of a "disabled" PWM 6617e40c25SUwe Kleine-König * is undefined. Depending on the hardware's capabilities it might drive the 6717e40c25SUwe Kleine-König * active or inactive level, go high-z or even continue to toggle. 6817e40c25SUwe Kleine-König * 6917e40c25SUwe Kleine-König * The unit for all three members is nanoseconds. 7017e40c25SUwe Kleine-König */ 7117e40c25SUwe Kleine-König struct pwm_waveform { 7217e40c25SUwe Kleine-König u64 period_length_ns; 7317e40c25SUwe Kleine-König u64 duty_length_ns; 7417e40c25SUwe Kleine-König u64 duty_offset_ns; 7517e40c25SUwe Kleine-König }; 7617e40c25SUwe Kleine-König 7743a276b0SBoris Brezillon /* 7843a276b0SBoris Brezillon * struct pwm_state - state of a PWM channel 7943a276b0SBoris Brezillon * @period: PWM period (in nanoseconds) 8043a276b0SBoris Brezillon * @duty_cycle: PWM duty cycle (in nanoseconds) 8143a276b0SBoris Brezillon * @polarity: PWM polarity 8209a7e4a3SBoris Brezillon * @enabled: PWM enabled status 839e40ee18SClemens Gruber * @usage_power: If set, the PWM driver is only required to maintain the power 849e40ee18SClemens Gruber * output but has more freedom regarding signal form. 859e40ee18SClemens Gruber * If supported, the signal can be optimized, for example to 869e40ee18SClemens Gruber * improve EMI by phase shifting individual channels. 8743a276b0SBoris Brezillon */ 8843a276b0SBoris Brezillon struct pwm_state { 89a9d887dcSGuru Das Srinagesh u64 period; 90a9d887dcSGuru Das Srinagesh u64 duty_cycle; 9143a276b0SBoris Brezillon enum pwm_polarity polarity; 9209a7e4a3SBoris Brezillon bool enabled; 939e40ee18SClemens Gruber bool usage_power; 9443a276b0SBoris Brezillon }; 9543a276b0SBoris Brezillon 9604883802SThierry Reding /** 9704883802SThierry Reding * struct pwm_device - PWM channel object 9804883802SThierry Reding * @label: name of the PWM device 9904883802SThierry Reding * @flags: flags associated with the PWM device 10004883802SThierry Reding * @hwpwm: per-chip relative index of the PWM device 10104883802SThierry Reding * @chip: PWM chip providing this PWM device 102e39c0df1SBoris Brezillon * @args: PWM arguments 1033ad1f3a3SUwe Kleine-König * @state: last applied state 1043ad1f3a3SUwe Kleine-König * @last: last implemented state (for PWM_DEBUG) 10504883802SThierry Reding */ 106f051c466SThierry Reding struct pwm_device { 107f051c466SThierry Reding const char *label; 108f051c466SThierry Reding unsigned long flags; 109f051c466SThierry Reding unsigned int hwpwm; 110f051c466SThierry Reding struct pwm_chip *chip; 111f051c466SThierry Reding 112e39c0df1SBoris Brezillon struct pwm_args args; 11343a276b0SBoris Brezillon struct pwm_state state; 1143ad1f3a3SUwe Kleine-König struct pwm_state last; 115f051c466SThierry Reding }; 116f051c466SThierry Reding 11743a276b0SBoris Brezillon /** 11843a276b0SBoris Brezillon * pwm_get_state() - retrieve the current PWM state 11943a276b0SBoris Brezillon * @pwm: PWM device 12043a276b0SBoris Brezillon * @state: state to fill with the current PWM state 1211a7a6e80SUwe Kleine-König * 1221a7a6e80SUwe Kleine-König * The returned PWM state represents the state that was applied by a previous call to 123c748a6d7SSean Young * pwm_apply_might_sleep(). Drivers may have to slightly tweak that state before programming it to 124c748a6d7SSean Young * hardware. If pwm_apply_might_sleep() was never called, this returns either the current hardware 1251a7a6e80SUwe Kleine-König * state (if supported) or the default settings. 12643a276b0SBoris Brezillon */ 12743a276b0SBoris Brezillon static inline void pwm_get_state(const struct pwm_device *pwm, 12843a276b0SBoris Brezillon struct pwm_state *state) 12943a276b0SBoris Brezillon { 13043a276b0SBoris Brezillon *state = pwm->state; 13143a276b0SBoris Brezillon } 13243a276b0SBoris Brezillon 1335c31252cSBoris Brezillon static inline bool pwm_is_enabled(const struct pwm_device *pwm) 1345c31252cSBoris Brezillon { 13509a7e4a3SBoris Brezillon struct pwm_state state; 13609a7e4a3SBoris Brezillon 13709a7e4a3SBoris Brezillon pwm_get_state(pwm, &state); 13809a7e4a3SBoris Brezillon 13909a7e4a3SBoris Brezillon return state.enabled; 1405c31252cSBoris Brezillon } 1415c31252cSBoris Brezillon 142a9d887dcSGuru Das Srinagesh static inline u64 pwm_get_period(const struct pwm_device *pwm) 143f051c466SThierry Reding { 14443a276b0SBoris Brezillon struct pwm_state state; 14543a276b0SBoris Brezillon 14643a276b0SBoris Brezillon pwm_get_state(pwm, &state); 14743a276b0SBoris Brezillon 14843a276b0SBoris Brezillon return state.period; 149f051c466SThierry Reding } 150f051c466SThierry Reding 151a9d887dcSGuru Das Srinagesh static inline u64 pwm_get_duty_cycle(const struct pwm_device *pwm) 15276abbddeSH Hartley Sweeten { 15343a276b0SBoris Brezillon struct pwm_state state; 15443a276b0SBoris Brezillon 15543a276b0SBoris Brezillon pwm_get_state(pwm, &state); 15643a276b0SBoris Brezillon 15743a276b0SBoris Brezillon return state.duty_cycle; 15876abbddeSH Hartley Sweeten } 15976abbddeSH Hartley Sweeten 160011e7631SBoris Brezillon static inline enum pwm_polarity pwm_get_polarity(const struct pwm_device *pwm) 161011e7631SBoris Brezillon { 16243a276b0SBoris Brezillon struct pwm_state state; 16343a276b0SBoris Brezillon 16443a276b0SBoris Brezillon pwm_get_state(pwm, &state); 16543a276b0SBoris Brezillon 16643a276b0SBoris Brezillon return state.polarity; 167011e7631SBoris Brezillon } 168011e7631SBoris Brezillon 169e39c0df1SBoris Brezillon static inline void pwm_get_args(const struct pwm_device *pwm, 170e39c0df1SBoris Brezillon struct pwm_args *args) 171e39c0df1SBoris Brezillon { 172e39c0df1SBoris Brezillon *args = pwm->args; 173e39c0df1SBoris Brezillon } 174e39c0df1SBoris Brezillon 1750c2498f1SSascha Hauer /** 176c748a6d7SSean Young * pwm_init_state() - prepare a new state to be applied with pwm_apply_might_sleep() 177a6a0dbbcSBoris Brezillon * @pwm: PWM device 178a6a0dbbcSBoris Brezillon * @state: state to fill with the prepared PWM state 179a6a0dbbcSBoris Brezillon * 180a6a0dbbcSBoris Brezillon * This functions prepares a state that can later be tweaked and applied 181c748a6d7SSean Young * to the PWM device with pwm_apply_might_sleep(). This is a convenient function 182a6a0dbbcSBoris Brezillon * that first retrieves the current PWM state and the replaces the period 183a6a0dbbcSBoris Brezillon * and polarity fields with the reference values defined in pwm->args. 184a6a0dbbcSBoris Brezillon * Once the function returns, you can adjust the ->enabled and ->duty_cycle 185c748a6d7SSean Young * fields according to your needs before calling pwm_apply_might_sleep(). 186a6a0dbbcSBoris Brezillon * 187a6a0dbbcSBoris Brezillon * ->duty_cycle is initially set to zero to avoid cases where the current 188a6a0dbbcSBoris Brezillon * ->duty_cycle value exceed the pwm_args->period one, which would trigger 189c748a6d7SSean Young * an error if the user calls pwm_apply_might_sleep() without adjusting ->duty_cycle 190a6a0dbbcSBoris Brezillon * first. 191a6a0dbbcSBoris Brezillon */ 192a6a0dbbcSBoris Brezillon static inline void pwm_init_state(const struct pwm_device *pwm, 193a6a0dbbcSBoris Brezillon struct pwm_state *state) 194a6a0dbbcSBoris Brezillon { 195a6a0dbbcSBoris Brezillon struct pwm_args args; 196a6a0dbbcSBoris Brezillon 197a6a0dbbcSBoris Brezillon /* First get the current state. */ 198a6a0dbbcSBoris Brezillon pwm_get_state(pwm, state); 199a6a0dbbcSBoris Brezillon 200a6a0dbbcSBoris Brezillon /* Then fill it with the reference config */ 201a6a0dbbcSBoris Brezillon pwm_get_args(pwm, &args); 202a6a0dbbcSBoris Brezillon 203a6a0dbbcSBoris Brezillon state->period = args.period; 204a6a0dbbcSBoris Brezillon state->polarity = args.polarity; 205a6a0dbbcSBoris Brezillon state->duty_cycle = 0; 2069e40ee18SClemens Gruber state->usage_power = false; 207a6a0dbbcSBoris Brezillon } 208a6a0dbbcSBoris Brezillon 209a6a0dbbcSBoris Brezillon /** 210f6f3bddfSBoris Brezillon * pwm_get_relative_duty_cycle() - Get a relative duty cycle value 211f6f3bddfSBoris Brezillon * @state: PWM state to extract the duty cycle from 212f6f3bddfSBoris Brezillon * @scale: target scale of the relative duty cycle 213f6f3bddfSBoris Brezillon * 214f6f3bddfSBoris Brezillon * This functions converts the absolute duty cycle stored in @state (expressed 215f6f3bddfSBoris Brezillon * in nanosecond) into a value relative to the period. 216f6f3bddfSBoris Brezillon * 217f6f3bddfSBoris Brezillon * For example if you want to get the duty_cycle expressed in percent, call: 218f6f3bddfSBoris Brezillon * 219f6f3bddfSBoris Brezillon * pwm_get_state(pwm, &state); 220f6f3bddfSBoris Brezillon * duty = pwm_get_relative_duty_cycle(&state, 100); 221f6f3bddfSBoris Brezillon */ 222f6f3bddfSBoris Brezillon static inline unsigned int 223f6f3bddfSBoris Brezillon pwm_get_relative_duty_cycle(const struct pwm_state *state, unsigned int scale) 224f6f3bddfSBoris Brezillon { 225f6f3bddfSBoris Brezillon if (!state->period) 226f6f3bddfSBoris Brezillon return 0; 227f6f3bddfSBoris Brezillon 228f6f3bddfSBoris Brezillon return DIV_ROUND_CLOSEST_ULL((u64)state->duty_cycle * scale, 229f6f3bddfSBoris Brezillon state->period); 230f6f3bddfSBoris Brezillon } 231f6f3bddfSBoris Brezillon 232f6f3bddfSBoris Brezillon /** 233f6f3bddfSBoris Brezillon * pwm_set_relative_duty_cycle() - Set a relative duty cycle value 234f6f3bddfSBoris Brezillon * @state: PWM state to fill 235f6f3bddfSBoris Brezillon * @duty_cycle: relative duty cycle value 236f6f3bddfSBoris Brezillon * @scale: scale in which @duty_cycle is expressed 237f6f3bddfSBoris Brezillon * 238f6f3bddfSBoris Brezillon * This functions converts a relative into an absolute duty cycle (expressed 239f6f3bddfSBoris Brezillon * in nanoseconds), and puts the result in state->duty_cycle. 240f6f3bddfSBoris Brezillon * 241f6f3bddfSBoris Brezillon * For example if you want to configure a 50% duty cycle, call: 242f6f3bddfSBoris Brezillon * 243f6f3bddfSBoris Brezillon * pwm_init_state(pwm, &state); 244f6f3bddfSBoris Brezillon * pwm_set_relative_duty_cycle(&state, 50, 100); 245c748a6d7SSean Young * pwm_apply_might_sleep(pwm, &state); 246f6f3bddfSBoris Brezillon * 247f6f3bddfSBoris Brezillon * This functions returns -EINVAL if @duty_cycle and/or @scale are 248f6f3bddfSBoris Brezillon * inconsistent (@scale == 0 or @duty_cycle > @scale). 249f6f3bddfSBoris Brezillon */ 250f6f3bddfSBoris Brezillon static inline int 251f6f3bddfSBoris Brezillon pwm_set_relative_duty_cycle(struct pwm_state *state, unsigned int duty_cycle, 252f6f3bddfSBoris Brezillon unsigned int scale) 253f6f3bddfSBoris Brezillon { 254f6f3bddfSBoris Brezillon if (!scale || duty_cycle > scale) 255f6f3bddfSBoris Brezillon return -EINVAL; 256f6f3bddfSBoris Brezillon 257f6f3bddfSBoris Brezillon state->duty_cycle = DIV_ROUND_CLOSEST_ULL((u64)duty_cycle * 258f6f3bddfSBoris Brezillon state->period, 259f6f3bddfSBoris Brezillon scale); 260f6f3bddfSBoris Brezillon 261f6f3bddfSBoris Brezillon return 0; 262f6f3bddfSBoris Brezillon } 263f6f3bddfSBoris Brezillon 264f6f3bddfSBoris Brezillon /** 265ef2e35d9SUwe Kleine-König * struct pwm_capture - PWM capture data 266ef2e35d9SUwe Kleine-König * @period: period of the PWM signal (in nanoseconds) 267ef2e35d9SUwe Kleine-König * @duty_cycle: duty cycle of the PWM signal (in nanoseconds) 268ef2e35d9SUwe Kleine-König */ 269ef2e35d9SUwe Kleine-König struct pwm_capture { 270ef2e35d9SUwe Kleine-König unsigned int period; 271ef2e35d9SUwe Kleine-König unsigned int duty_cycle; 272ef2e35d9SUwe Kleine-König }; 273ef2e35d9SUwe Kleine-König 274ef2e35d9SUwe Kleine-König /** 2750c2498f1SSascha Hauer * struct pwm_ops - PWM controller operations 2760c2498f1SSascha Hauer * @request: optional hook for requesting a PWM 2770c2498f1SSascha Hauer * @free: optional hook for freeing a PWM 2783a3d1a4eSLee Jones * @capture: capture and report PWM signal 279dab9cd4bSUwe Kleine-König * @sizeof_wfhw: size (in bytes) of driver specific waveform presentation 280dab9cd4bSUwe Kleine-König * @round_waveform_tohw: convert a struct pwm_waveform to driver specific presentation 281dab9cd4bSUwe Kleine-König * @round_waveform_fromhw: convert a driver specific waveform presentation to struct pwm_waveform 282dab9cd4bSUwe Kleine-König * @read_waveform: read driver specific waveform presentation from hardware 283dab9cd4bSUwe Kleine-König * @write_waveform: write driver specific waveform presentation to hardware 28427938fd8SRasmus Villemoes * @apply: atomically apply a new PWM config 285a96d3659SUwe Kleine-König * @get_state: get the current PWM state. 2860c2498f1SSascha Hauer */ 2870c2498f1SSascha Hauer struct pwm_ops { 2886bc7064aSThierry Reding int (*request)(struct pwm_chip *chip, struct pwm_device *pwm); 2896bc7064aSThierry Reding void (*free)(struct pwm_chip *chip, struct pwm_device *pwm); 2903a3d1a4eSLee Jones int (*capture)(struct pwm_chip *chip, struct pwm_device *pwm, 2913a3d1a4eSLee Jones struct pwm_capture *result, unsigned long timeout); 29217e40c25SUwe Kleine-König 29317e40c25SUwe Kleine-König size_t sizeof_wfhw; 29417e40c25SUwe Kleine-König int (*round_waveform_tohw)(struct pwm_chip *chip, struct pwm_device *pwm, 29517e40c25SUwe Kleine-König const struct pwm_waveform *wf, void *wfhw); 29617e40c25SUwe Kleine-König int (*round_waveform_fromhw)(struct pwm_chip *chip, struct pwm_device *pwm, 29717e40c25SUwe Kleine-König const void *wfhw, struct pwm_waveform *wf); 29817e40c25SUwe Kleine-König int (*read_waveform)(struct pwm_chip *chip, struct pwm_device *pwm, 29917e40c25SUwe Kleine-König void *wfhw); 30017e40c25SUwe Kleine-König int (*write_waveform)(struct pwm_chip *chip, struct pwm_device *pwm, 30117e40c25SUwe Kleine-König const void *wfhw); 30217e40c25SUwe Kleine-König 3035ec803edSBoris Brezillon int (*apply)(struct pwm_chip *chip, struct pwm_device *pwm, 30471523d18SUwe Kleine-König const struct pwm_state *state); 3056c452cffSUwe Kleine-König int (*get_state)(struct pwm_chip *chip, struct pwm_device *pwm, 30615fa8a43SBoris Brezillon struct pwm_state *state); 3070c2498f1SSascha Hauer }; 3080c2498f1SSascha Hauer 3090c2498f1SSascha Hauer /** 310f051c466SThierry Reding * struct pwm_chip - abstract a PWM controller 311f051c466SThierry Reding * @dev: device providing the PWMs 312f051c466SThierry Reding * @ops: callbacks for this PWM controller 313384461abSUwe Kleine-König * @owner: module providing this chip 3142d91123aSUwe Kleine-König * @id: unique number of this PWM chip 315f051c466SThierry Reding * @npwm: number of PWMs controlled by this chip 31604883802SThierry Reding * @of_xlate: request a PWM device given a device tree PWM specifier 3177170d3beSSean Young * @atomic: can the driver's ->apply() be called in atomic context 31805947224SUwe Kleine-König * @uses_pwmchip_alloc: signals if pwmchip_allow was used to allocate this chip 3191cc2e1faSUwe Kleine-König * @operational: signals if the chip can be used (or is already deregistered) 3201cc2e1faSUwe Kleine-König * @nonatomic_lock: mutex for nonatomic chips 3211cc2e1faSUwe Kleine-König * @atomic_lock: mutex for atomic chips 3225d0a4c11SUwe Kleine-König * @pwms: array of PWM devices allocated by the framework 3230c2498f1SSascha Hauer */ 3240c2498f1SSascha Hauer struct pwm_chip { 3254c56b143SUwe Kleine-König struct device dev; 326f051c466SThierry Reding const struct pwm_ops *ops; 327384461abSUwe Kleine-König struct module *owner; 32854c86dd2SUwe Kleine-König unsigned int id; 329f051c466SThierry Reding unsigned int npwm; 330f051c466SThierry Reding 331b4f78ff7SUwe Kleine-König struct pwm_device * (*of_xlate)(struct pwm_chip *chip, 3327299ab70SThierry Reding const struct of_phandle_args *args); 3337170d3beSSean Young bool atomic; 3345d0a4c11SUwe Kleine-König 3355d0a4c11SUwe Kleine-König /* only used internally by the PWM framework */ 33605947224SUwe Kleine-König bool uses_pwmchip_alloc; 3371cc2e1faSUwe Kleine-König bool operational; 3381cc2e1faSUwe Kleine-König union { 3391cc2e1faSUwe Kleine-König /* 3401cc2e1faSUwe Kleine-König * depending on the chip being atomic or not either the mutex or 3411cc2e1faSUwe Kleine-König * the spinlock is used. It protects .operational and 3421cc2e1faSUwe Kleine-König * synchronizes the callbacks in .ops 3431cc2e1faSUwe Kleine-König */ 3441cc2e1faSUwe Kleine-König struct mutex nonatomic_lock; 3451cc2e1faSUwe Kleine-König spinlock_t atomic_lock; 3461cc2e1faSUwe Kleine-König }; 347ee37bf50SUwe Kleine-König struct pwm_device pwms[] __counted_by(npwm); 3480c2498f1SSascha Hauer }; 3490c2498f1SSascha Hauer 3504e59267cSUwe Kleine-König static inline struct device *pwmchip_parent(const struct pwm_chip *chip) 3514e59267cSUwe Kleine-König { 3524c56b143SUwe Kleine-König return chip->dev.parent; 3534e59267cSUwe Kleine-König } 3544e59267cSUwe Kleine-König 35524003d50SUwe Kleine-König static inline void *pwmchip_get_drvdata(struct pwm_chip *chip) 35624003d50SUwe Kleine-König { 35738ae7142SUwe Kleine-König return dev_get_drvdata(&chip->dev); 35824003d50SUwe Kleine-König } 35924003d50SUwe Kleine-König 36024003d50SUwe Kleine-König static inline void pwmchip_set_drvdata(struct pwm_chip *chip, void *data) 36124003d50SUwe Kleine-König { 36238ae7142SUwe Kleine-König dev_set_drvdata(&chip->dev, data); 36324003d50SUwe Kleine-König } 36424003d50SUwe Kleine-König 3650bcf168bSTushar Behera #if IS_ENABLED(CONFIG_PWM) 3666c5126c6SUwe Kleine-König 3676c5126c6SUwe Kleine-König /* PWM consumer APIs */ 3686c5126c6SUwe Kleine-König int pwm_round_waveform_might_sleep(struct pwm_device *pwm, struct pwm_waveform *wf); 3696c5126c6SUwe Kleine-König int pwm_get_waveform_might_sleep(struct pwm_device *pwm, struct pwm_waveform *wf); 3706c5126c6SUwe Kleine-König int pwm_set_waveform_might_sleep(struct pwm_device *pwm, const struct pwm_waveform *wf, bool exact); 371c748a6d7SSean Young int pwm_apply_might_sleep(struct pwm_device *pwm, const struct pwm_state *state); 3727170d3beSSean Young int pwm_apply_atomic(struct pwm_device *pwm, const struct pwm_state *state); 3732ea25aabSDavid Lechner int pwm_get_state_hw(struct pwm_device *pwm, struct pwm_state *state); 3745ec803edSBoris Brezillon int pwm_adjust_config(struct pwm_device *pwm); 3755ec803edSBoris Brezillon 3765ec803edSBoris Brezillon /** 3775ec803edSBoris Brezillon * pwm_config() - change a PWM device configuration 3785ec803edSBoris Brezillon * @pwm: PWM device 3795ec803edSBoris Brezillon * @duty_ns: "on" time (in nanoseconds) 3805ec803edSBoris Brezillon * @period_ns: duration (in nanoseconds) of one cycle 3815ec803edSBoris Brezillon * 3825ec803edSBoris Brezillon * Returns: 0 on success or a negative error code on failure. 3835ec803edSBoris Brezillon */ 3845ec803edSBoris Brezillon static inline int pwm_config(struct pwm_device *pwm, int duty_ns, 3855ec803edSBoris Brezillon int period_ns) 3865ec803edSBoris Brezillon { 3875ec803edSBoris Brezillon struct pwm_state state; 3885ec803edSBoris Brezillon 3895ec803edSBoris Brezillon if (!pwm) 3905ec803edSBoris Brezillon return -EINVAL; 3915ec803edSBoris Brezillon 392ef2bf499SBrian Norris if (duty_ns < 0 || period_ns < 0) 393ef2bf499SBrian Norris return -EINVAL; 394ef2bf499SBrian Norris 3955ec803edSBoris Brezillon pwm_get_state(pwm, &state); 3965ec803edSBoris Brezillon if (state.duty_cycle == duty_ns && state.period == period_ns) 3975ec803edSBoris Brezillon return 0; 3985ec803edSBoris Brezillon 3995ec803edSBoris Brezillon state.duty_cycle = duty_ns; 4005ec803edSBoris Brezillon state.period = period_ns; 401c748a6d7SSean Young return pwm_apply_might_sleep(pwm, &state); 4025ec803edSBoris Brezillon } 4035ec803edSBoris Brezillon 4045ec803edSBoris Brezillon /** 4055ec803edSBoris Brezillon * pwm_enable() - start a PWM output toggling 4065ec803edSBoris Brezillon * @pwm: PWM device 4075ec803edSBoris Brezillon * 4085ec803edSBoris Brezillon * Returns: 0 on success or a negative error code on failure. 4095ec803edSBoris Brezillon */ 4105ec803edSBoris Brezillon static inline int pwm_enable(struct pwm_device *pwm) 4115ec803edSBoris Brezillon { 4125ec803edSBoris Brezillon struct pwm_state state; 4135ec803edSBoris Brezillon 4145ec803edSBoris Brezillon if (!pwm) 4155ec803edSBoris Brezillon return -EINVAL; 4165ec803edSBoris Brezillon 4175ec803edSBoris Brezillon pwm_get_state(pwm, &state); 4185ec803edSBoris Brezillon if (state.enabled) 4195ec803edSBoris Brezillon return 0; 4205ec803edSBoris Brezillon 4215ec803edSBoris Brezillon state.enabled = true; 422c748a6d7SSean Young return pwm_apply_might_sleep(pwm, &state); 4235ec803edSBoris Brezillon } 4245ec803edSBoris Brezillon 4255ec803edSBoris Brezillon /** 4265ec803edSBoris Brezillon * pwm_disable() - stop a PWM output toggling 4275ec803edSBoris Brezillon * @pwm: PWM device 4285ec803edSBoris Brezillon */ 4295ec803edSBoris Brezillon static inline void pwm_disable(struct pwm_device *pwm) 4305ec803edSBoris Brezillon { 4315ec803edSBoris Brezillon struct pwm_state state; 4325ec803edSBoris Brezillon 4335ec803edSBoris Brezillon if (!pwm) 4345ec803edSBoris Brezillon return; 4355ec803edSBoris Brezillon 4365ec803edSBoris Brezillon pwm_get_state(pwm, &state); 4375ec803edSBoris Brezillon if (!state.enabled) 4385ec803edSBoris Brezillon return; 4395ec803edSBoris Brezillon 4405ec803edSBoris Brezillon state.enabled = false; 441c748a6d7SSean Young pwm_apply_might_sleep(pwm, &state); 4425ec803edSBoris Brezillon } 4435ec803edSBoris Brezillon 4447170d3beSSean Young /** 4457170d3beSSean Young * pwm_might_sleep() - is pwm_apply_atomic() supported? 4467170d3beSSean Young * @pwm: PWM device 4477170d3beSSean Young * 4487170d3beSSean Young * Returns: false if pwm_apply_atomic() can be called from atomic context. 4497170d3beSSean Young */ 4507170d3beSSean Young static inline bool pwm_might_sleep(struct pwm_device *pwm) 4517170d3beSSean Young { 4527170d3beSSean Young return !pwm->chip->atomic; 4537170d3beSSean Young } 4547170d3beSSean Young 4555ec803edSBoris Brezillon /* PWM provider APIs */ 456024913dbSUwe Kleine-König void pwmchip_put(struct pwm_chip *chip); 457024913dbSUwe Kleine-König struct pwm_chip *pwmchip_alloc(struct device *parent, unsigned int npwm, size_t sizeof_priv); 458024913dbSUwe Kleine-König struct pwm_chip *devm_pwmchip_alloc(struct device *parent, unsigned int npwm, size_t sizeof_priv); 459024913dbSUwe Kleine-König 460384461abSUwe Kleine-König int __pwmchip_add(struct pwm_chip *chip, struct module *owner); 461384461abSUwe Kleine-König #define pwmchip_add(chip) __pwmchip_add(chip, THIS_MODULE) 4628083f58dSUwe Kleine-König void pwmchip_remove(struct pwm_chip *chip); 463bcda91bfSUwe Kleine-König 464384461abSUwe Kleine-König int __devm_pwmchip_add(struct device *dev, struct pwm_chip *chip, struct module *owner); 465384461abSUwe Kleine-König #define devm_pwmchip_add(dev, chip) __devm_pwmchip_add(dev, chip, THIS_MODULE) 466bcda91bfSUwe Kleine-König 467b4f78ff7SUwe Kleine-König struct pwm_device *of_pwm_xlate_with_flags(struct pwm_chip *chip, 46883af2402SPhilip, Avinash const struct of_phandle_args *args); 469b4f78ff7SUwe Kleine-König struct pwm_device *of_pwm_single_xlate(struct pwm_chip *chip, 4703ab7b6acSBjorn Andersson const struct of_phandle_args *args); 47183af2402SPhilip, Avinash 472d4c0c470SPeter Ujfalusi struct pwm_device *pwm_get(struct device *dev, const char *con_id); 4738138d2ddSThierry Reding void pwm_put(struct pwm_device *pwm); 4748138d2ddSThierry Reding 475d4c0c470SPeter Ujfalusi struct pwm_device *devm_pwm_get(struct device *dev, const char *con_id); 4764a6ef8e3SNikolaus Voss struct pwm_device *devm_fwnode_pwm_get(struct device *dev, 4774a6ef8e3SNikolaus Voss struct fwnode_handle *fwnode, 4784a6ef8e3SNikolaus Voss const char *con_id); 4790bcf168bSTushar Behera #else 4807170d3beSSean Young static inline bool pwm_might_sleep(struct pwm_device *pwm) 4817170d3beSSean Young { 4827170d3beSSean Young return true; 4837170d3beSSean Young } 4847170d3beSSean Young 485c748a6d7SSean Young static inline int pwm_apply_might_sleep(struct pwm_device *pwm, 4865ec803edSBoris Brezillon const struct pwm_state *state) 4875ec803edSBoris Brezillon { 4884ad91a22SUwe Kleine-König might_sleep(); 489dc518b37SSean Young return -EOPNOTSUPP; 4905ec803edSBoris Brezillon } 4915ec803edSBoris Brezillon 4927170d3beSSean Young static inline int pwm_apply_atomic(struct pwm_device *pwm, 4937170d3beSSean Young const struct pwm_state *state) 4947170d3beSSean Young { 4957170d3beSSean Young return -EOPNOTSUPP; 4967170d3beSSean Young } 4977170d3beSSean Young 4982ea25aabSDavid Lechner static inline int pwm_get_state_hw(struct pwm_device *pwm, struct pwm_state *state) 4992ea25aabSDavid Lechner { 5002ea25aabSDavid Lechner return -EOPNOTSUPP; 5012ea25aabSDavid Lechner } 5022ea25aabSDavid Lechner 5035ec803edSBoris Brezillon static inline int pwm_adjust_config(struct pwm_device *pwm) 5045ec803edSBoris Brezillon { 505dc518b37SSean Young return -EOPNOTSUPP; 5065ec803edSBoris Brezillon } 5075ec803edSBoris Brezillon 5085ec803edSBoris Brezillon static inline int pwm_config(struct pwm_device *pwm, int duty_ns, 5095ec803edSBoris Brezillon int period_ns) 5105ec803edSBoris Brezillon { 5114ad91a22SUwe Kleine-König might_sleep(); 5125ec803edSBoris Brezillon return -EINVAL; 5135ec803edSBoris Brezillon } 5145ec803edSBoris Brezillon 5155ec803edSBoris Brezillon static inline int pwm_enable(struct pwm_device *pwm) 5165ec803edSBoris Brezillon { 5174ad91a22SUwe Kleine-König might_sleep(); 5185ec803edSBoris Brezillon return -EINVAL; 5195ec803edSBoris Brezillon } 5205ec803edSBoris Brezillon 5215ec803edSBoris Brezillon static inline void pwm_disable(struct pwm_device *pwm) 5225ec803edSBoris Brezillon { 5234ad91a22SUwe Kleine-König might_sleep(); 5245ec803edSBoris Brezillon } 5255ec803edSBoris Brezillon 526024913dbSUwe Kleine-König static inline void pwmchip_put(struct pwm_chip *chip) 527024913dbSUwe Kleine-König { 528024913dbSUwe Kleine-König } 529024913dbSUwe Kleine-König 530024913dbSUwe Kleine-König static inline struct pwm_chip *pwmchip_alloc(struct device *parent, 531024913dbSUwe Kleine-König unsigned int npwm, 532024913dbSUwe Kleine-König size_t sizeof_priv) 533024913dbSUwe Kleine-König { 534024913dbSUwe Kleine-König return ERR_PTR(-EINVAL); 535024913dbSUwe Kleine-König } 536024913dbSUwe Kleine-König 537024913dbSUwe Kleine-König static inline struct pwm_chip *devm_pwmchip_alloc(struct device *parent, 538024913dbSUwe Kleine-König unsigned int npwm, 539024913dbSUwe Kleine-König size_t sizeof_priv) 540024913dbSUwe Kleine-König { 541024913dbSUwe Kleine-König return pwmchip_alloc(parent, npwm, sizeof_priv); 542024913dbSUwe Kleine-König } 543024913dbSUwe Kleine-König 5440bcf168bSTushar Behera static inline int pwmchip_add(struct pwm_chip *chip) 5450bcf168bSTushar Behera { 5460bcf168bSTushar Behera return -EINVAL; 5470bcf168bSTushar Behera } 5480bcf168bSTushar Behera 5490bcf168bSTushar Behera static inline int pwmchip_remove(struct pwm_chip *chip) 5500bcf168bSTushar Behera { 5510bcf168bSTushar Behera return -EINVAL; 5520bcf168bSTushar Behera } 5530bcf168bSTushar Behera 55488da4e81SAndy Shevchenko static inline int devm_pwmchip_add(struct device *dev, struct pwm_chip *chip) 55588da4e81SAndy Shevchenko { 55688da4e81SAndy Shevchenko return -EINVAL; 55788da4e81SAndy Shevchenko } 55888da4e81SAndy Shevchenko 5590bcf168bSTushar Behera static inline struct pwm_device *pwm_get(struct device *dev, 5600bcf168bSTushar Behera const char *consumer) 5610bcf168bSTushar Behera { 56227d9a4d6SUwe Kleine-König might_sleep(); 5630bcf168bSTushar Behera return ERR_PTR(-ENODEV); 5640bcf168bSTushar Behera } 5650bcf168bSTushar Behera 5660bcf168bSTushar Behera static inline void pwm_put(struct pwm_device *pwm) 5670bcf168bSTushar Behera { 56827d9a4d6SUwe Kleine-König might_sleep(); 5690bcf168bSTushar Behera } 5700bcf168bSTushar Behera 5710bcf168bSTushar Behera static inline struct pwm_device *devm_pwm_get(struct device *dev, 5720bcf168bSTushar Behera const char *consumer) 5730bcf168bSTushar Behera { 57427d9a4d6SUwe Kleine-König might_sleep(); 5750bcf168bSTushar Behera return ERR_PTR(-ENODEV); 5760bcf168bSTushar Behera } 5770bcf168bSTushar Behera 5784a6ef8e3SNikolaus Voss static inline struct pwm_device * 5794a6ef8e3SNikolaus Voss devm_fwnode_pwm_get(struct device *dev, struct fwnode_handle *fwnode, 5804a6ef8e3SNikolaus Voss const char *con_id) 5814a6ef8e3SNikolaus Voss { 58227d9a4d6SUwe Kleine-König might_sleep(); 5834a6ef8e3SNikolaus Voss return ERR_PTR(-ENODEV); 5844a6ef8e3SNikolaus Voss } 5850bcf168bSTushar Behera #endif 5866354316dSAlexandre Courbot 5875ec803edSBoris Brezillon static inline void pwm_apply_args(struct pwm_device *pwm) 5885ec803edSBoris Brezillon { 58933cdcee0SBoris Brezillon struct pwm_state state = { }; 59033cdcee0SBoris Brezillon 5915ec803edSBoris Brezillon /* 5925ec803edSBoris Brezillon * PWM users calling pwm_apply_args() expect to have a fresh config 5935ec803edSBoris Brezillon * where the polarity and period are set according to pwm_args info. 5945ec803edSBoris Brezillon * The problem is, polarity can only be changed when the PWM is 5955ec803edSBoris Brezillon * disabled. 5965ec803edSBoris Brezillon * 5975ec803edSBoris Brezillon * PWM drivers supporting hardware readout may declare the PWM device 5985ec803edSBoris Brezillon * as enabled, and prevent polarity setting, which changes from the 5995ec803edSBoris Brezillon * existing behavior, where all PWM devices are declared as disabled 6005ec803edSBoris Brezillon * at startup (even if they are actually enabled), thus authorizing 6015ec803edSBoris Brezillon * polarity setting. 6025ec803edSBoris Brezillon * 60333cdcee0SBoris Brezillon * To fulfill this requirement, we apply a new state which disables 60433cdcee0SBoris Brezillon * the PWM device and set the reference period and polarity config. 6055ec803edSBoris Brezillon * 6065ec803edSBoris Brezillon * Note that PWM users requiring a smooth handover between the 6075ec803edSBoris Brezillon * bootloader and the kernel (like critical regulators controlled by 6085ec803edSBoris Brezillon * PWM devices) will have to switch to the atomic API and avoid calling 6095ec803edSBoris Brezillon * pwm_apply_args(). 6105ec803edSBoris Brezillon */ 61133cdcee0SBoris Brezillon 61233cdcee0SBoris Brezillon state.enabled = false; 61333cdcee0SBoris Brezillon state.polarity = pwm->args.polarity; 61433cdcee0SBoris Brezillon state.period = pwm->args.period; 6159e40ee18SClemens Gruber state.usage_power = false; 61633cdcee0SBoris Brezillon 617c748a6d7SSean Young pwm_apply_might_sleep(pwm, &state); 6185ec803edSBoris Brezillon } 6195ec803edSBoris Brezillon 6208138d2ddSThierry Reding struct pwm_lookup { 6218138d2ddSThierry Reding struct list_head list; 6228138d2ddSThierry Reding const char *provider; 6238138d2ddSThierry Reding unsigned int index; 6248138d2ddSThierry Reding const char *dev_id; 6258138d2ddSThierry Reding const char *con_id; 6263796ce1dSAlexandre Belloni unsigned int period; 6273796ce1dSAlexandre Belloni enum pwm_polarity polarity; 628b526a314SHans de Goede const char *module; /* optional, may be NULL */ 6298138d2ddSThierry Reding }; 6308138d2ddSThierry Reding 631b526a314SHans de Goede #define PWM_LOOKUP_WITH_MODULE(_provider, _index, _dev_id, _con_id, \ 632b526a314SHans de Goede _period, _polarity, _module) \ 6338138d2ddSThierry Reding { \ 6348138d2ddSThierry Reding .provider = _provider, \ 6358138d2ddSThierry Reding .index = _index, \ 6368138d2ddSThierry Reding .dev_id = _dev_id, \ 6378138d2ddSThierry Reding .con_id = _con_id, \ 63842844029SAlexandre Belloni .period = _period, \ 639b526a314SHans de Goede .polarity = _polarity, \ 640b526a314SHans de Goede .module = _module, \ 6418138d2ddSThierry Reding } 6428138d2ddSThierry Reding 643b526a314SHans de Goede #define PWM_LOOKUP(_provider, _index, _dev_id, _con_id, _period, _polarity) \ 644b526a314SHans de Goede PWM_LOOKUP_WITH_MODULE(_provider, _index, _dev_id, _con_id, _period, \ 645b526a314SHans de Goede _polarity, NULL) 646b526a314SHans de Goede 6470bcf168bSTushar Behera #if IS_ENABLED(CONFIG_PWM) 6488138d2ddSThierry Reding void pwm_add_table(struct pwm_lookup *table, size_t num); 649efb0de55SShobhit Kumar void pwm_remove_table(struct pwm_lookup *table, size_t num); 6500bcf168bSTushar Behera #else 6510bcf168bSTushar Behera static inline void pwm_add_table(struct pwm_lookup *table, size_t num) 6520bcf168bSTushar Behera { 6530bcf168bSTushar Behera } 654efb0de55SShobhit Kumar 655efb0de55SShobhit Kumar static inline void pwm_remove_table(struct pwm_lookup *table, size_t num) 656efb0de55SShobhit Kumar { 657efb0de55SShobhit Kumar } 6580c2498f1SSascha Hauer #endif 6590c2498f1SSascha Hauer 6605243ef8bSMark Vels #endif /* __LINUX_PWM_H */ 661