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 862099abfSThierry Reding struct seq_file; 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 { 39e39c0df1SBoris Brezillon unsigned int period; 40e39c0df1SBoris Brezillon enum pwm_polarity polarity; 41e39c0df1SBoris Brezillon }; 42e39c0df1SBoris Brezillon 43f051c466SThierry Reding enum { 44f051c466SThierry Reding PWMF_REQUESTED = 1 << 0, 4509a7e4a3SBoris Brezillon PWMF_EXPORTED = 1 << 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 5443a276b0SBoris Brezillon */ 5543a276b0SBoris Brezillon struct pwm_state { 5643a276b0SBoris Brezillon unsigned int period; 5743a276b0SBoris Brezillon unsigned int duty_cycle; 5843a276b0SBoris Brezillon enum pwm_polarity polarity; 5909a7e4a3SBoris Brezillon bool enabled; 6043a276b0SBoris Brezillon }; 6143a276b0SBoris Brezillon 6204883802SThierry Reding /** 6304883802SThierry Reding * struct pwm_device - PWM channel object 6404883802SThierry Reding * @label: name of the PWM device 6504883802SThierry Reding * @flags: flags associated with the PWM device 6604883802SThierry Reding * @hwpwm: per-chip relative index of the PWM device 6704883802SThierry Reding * @pwm: global index of the PWM device 6804883802SThierry Reding * @chip: PWM chip providing this PWM device 6904883802SThierry Reding * @chip_data: chip-private data associated with the PWM device 70e39c0df1SBoris Brezillon * @args: PWM arguments 7143a276b0SBoris Brezillon * @state: curent PWM channel state 7204883802SThierry Reding */ 73f051c466SThierry Reding struct pwm_device { 74f051c466SThierry Reding const char *label; 75f051c466SThierry Reding unsigned long flags; 76f051c466SThierry Reding unsigned int hwpwm; 77f051c466SThierry Reding unsigned int pwm; 78f051c466SThierry Reding struct pwm_chip *chip; 79f051c466SThierry Reding void *chip_data; 80f051c466SThierry Reding 81e39c0df1SBoris Brezillon struct pwm_args args; 8243a276b0SBoris Brezillon struct pwm_state state; 83f051c466SThierry Reding }; 84f051c466SThierry Reding 8543a276b0SBoris Brezillon /** 8643a276b0SBoris Brezillon * pwm_get_state() - retrieve the current PWM state 8743a276b0SBoris Brezillon * @pwm: PWM device 8843a276b0SBoris Brezillon * @state: state to fill with the current PWM state 8943a276b0SBoris Brezillon */ 9043a276b0SBoris Brezillon static inline void pwm_get_state(const struct pwm_device *pwm, 9143a276b0SBoris Brezillon struct pwm_state *state) 9243a276b0SBoris Brezillon { 9343a276b0SBoris Brezillon *state = pwm->state; 9443a276b0SBoris Brezillon } 9543a276b0SBoris Brezillon 965c31252cSBoris Brezillon static inline bool pwm_is_enabled(const struct pwm_device *pwm) 975c31252cSBoris Brezillon { 9809a7e4a3SBoris Brezillon struct pwm_state state; 9909a7e4a3SBoris Brezillon 10009a7e4a3SBoris Brezillon pwm_get_state(pwm, &state); 10109a7e4a3SBoris Brezillon 10209a7e4a3SBoris Brezillon return state.enabled; 1035c31252cSBoris Brezillon } 1045c31252cSBoris Brezillon 105f051c466SThierry Reding static inline void pwm_set_period(struct pwm_device *pwm, unsigned int period) 106f051c466SThierry Reding { 107f051c466SThierry Reding if (pwm) 10843a276b0SBoris Brezillon pwm->state.period = period; 109f051c466SThierry Reding } 110f051c466SThierry Reding 111a1cf4217SBoris Brezillon static inline unsigned int pwm_get_period(const struct pwm_device *pwm) 112f051c466SThierry Reding { 11343a276b0SBoris Brezillon struct pwm_state state; 11443a276b0SBoris Brezillon 11543a276b0SBoris Brezillon pwm_get_state(pwm, &state); 11643a276b0SBoris Brezillon 11743a276b0SBoris Brezillon return state.period; 118f051c466SThierry Reding } 119f051c466SThierry Reding 12076abbddeSH Hartley Sweeten static inline void pwm_set_duty_cycle(struct pwm_device *pwm, unsigned int duty) 12176abbddeSH Hartley Sweeten { 12276abbddeSH Hartley Sweeten if (pwm) 12343a276b0SBoris Brezillon pwm->state.duty_cycle = duty; 12476abbddeSH Hartley Sweeten } 12576abbddeSH Hartley Sweeten 126a1cf4217SBoris Brezillon static inline unsigned int pwm_get_duty_cycle(const struct pwm_device *pwm) 12776abbddeSH Hartley Sweeten { 12843a276b0SBoris Brezillon struct pwm_state state; 12943a276b0SBoris Brezillon 13043a276b0SBoris Brezillon pwm_get_state(pwm, &state); 13143a276b0SBoris Brezillon 13243a276b0SBoris Brezillon return state.duty_cycle; 13376abbddeSH Hartley Sweeten } 13476abbddeSH Hartley Sweeten 135011e7631SBoris Brezillon static inline enum pwm_polarity pwm_get_polarity(const struct pwm_device *pwm) 136011e7631SBoris Brezillon { 13743a276b0SBoris Brezillon struct pwm_state state; 13843a276b0SBoris Brezillon 13943a276b0SBoris Brezillon pwm_get_state(pwm, &state); 14043a276b0SBoris Brezillon 14143a276b0SBoris Brezillon return state.polarity; 142011e7631SBoris Brezillon } 143011e7631SBoris Brezillon 144e39c0df1SBoris Brezillon static inline void pwm_get_args(const struct pwm_device *pwm, 145e39c0df1SBoris Brezillon struct pwm_args *args) 146e39c0df1SBoris Brezillon { 147e39c0df1SBoris Brezillon *args = pwm->args; 148e39c0df1SBoris Brezillon } 149e39c0df1SBoris Brezillon 1500c2498f1SSascha Hauer /** 151*a6a0dbbcSBoris Brezillon * pwm_init_state() - prepare a new state to be applied with pwm_apply_state() 152*a6a0dbbcSBoris Brezillon * @pwm: PWM device 153*a6a0dbbcSBoris Brezillon * @state: state to fill with the prepared PWM state 154*a6a0dbbcSBoris Brezillon * 155*a6a0dbbcSBoris Brezillon * This functions prepares a state that can later be tweaked and applied 156*a6a0dbbcSBoris Brezillon * to the PWM device with pwm_apply_state(). This is a convenient function 157*a6a0dbbcSBoris Brezillon * that first retrieves the current PWM state and the replaces the period 158*a6a0dbbcSBoris Brezillon * and polarity fields with the reference values defined in pwm->args. 159*a6a0dbbcSBoris Brezillon * Once the function returns, you can adjust the ->enabled and ->duty_cycle 160*a6a0dbbcSBoris Brezillon * fields according to your needs before calling pwm_apply_state(). 161*a6a0dbbcSBoris Brezillon * 162*a6a0dbbcSBoris Brezillon * ->duty_cycle is initially set to zero to avoid cases where the current 163*a6a0dbbcSBoris Brezillon * ->duty_cycle value exceed the pwm_args->period one, which would trigger 164*a6a0dbbcSBoris Brezillon * an error if the user calls pwm_apply_state() without adjusting ->duty_cycle 165*a6a0dbbcSBoris Brezillon * first. 166*a6a0dbbcSBoris Brezillon */ 167*a6a0dbbcSBoris Brezillon static inline void pwm_init_state(const struct pwm_device *pwm, 168*a6a0dbbcSBoris Brezillon struct pwm_state *state) 169*a6a0dbbcSBoris Brezillon { 170*a6a0dbbcSBoris Brezillon struct pwm_args args; 171*a6a0dbbcSBoris Brezillon 172*a6a0dbbcSBoris Brezillon /* First get the current state. */ 173*a6a0dbbcSBoris Brezillon pwm_get_state(pwm, state); 174*a6a0dbbcSBoris Brezillon 175*a6a0dbbcSBoris Brezillon /* Then fill it with the reference config */ 176*a6a0dbbcSBoris Brezillon pwm_get_args(pwm, &args); 177*a6a0dbbcSBoris Brezillon 178*a6a0dbbcSBoris Brezillon state->period = args.period; 179*a6a0dbbcSBoris Brezillon state->polarity = args.polarity; 180*a6a0dbbcSBoris Brezillon state->duty_cycle = 0; 181*a6a0dbbcSBoris Brezillon } 182*a6a0dbbcSBoris Brezillon 183*a6a0dbbcSBoris Brezillon /** 1840c2498f1SSascha Hauer * struct pwm_ops - PWM controller operations 1850c2498f1SSascha Hauer * @request: optional hook for requesting a PWM 1860c2498f1SSascha Hauer * @free: optional hook for freeing a PWM 1870c2498f1SSascha Hauer * @config: configure duty cycles and period length for this PWM 1880aa0869cSPhilip, Avinash * @set_polarity: configure the polarity of this PWM 1890c2498f1SSascha Hauer * @enable: enable PWM output toggling 1900c2498f1SSascha Hauer * @disable: disable PWM output toggling 1915ec803edSBoris Brezillon * @apply: atomically apply a new PWM config. The state argument 1925ec803edSBoris Brezillon * should be adjusted with the real hardware config (if the 1935ec803edSBoris Brezillon * approximate the period or duty_cycle value, state should 1945ec803edSBoris Brezillon * reflect it) 19515fa8a43SBoris Brezillon * @get_state: get the current PWM state. This function is only 19615fa8a43SBoris Brezillon * called once per PWM device when the PWM chip is 19715fa8a43SBoris Brezillon * registered. 19862099abfSThierry Reding * @dbg_show: optional routine to show contents in debugfs 1990c2498f1SSascha Hauer * @owner: helps prevent removal of modules exporting active PWMs 2000c2498f1SSascha Hauer */ 2010c2498f1SSascha Hauer struct pwm_ops { 2026bc7064aSThierry Reding int (*request)(struct pwm_chip *chip, struct pwm_device *pwm); 2036bc7064aSThierry Reding void (*free)(struct pwm_chip *chip, struct pwm_device *pwm); 2046bc7064aSThierry Reding int (*config)(struct pwm_chip *chip, struct pwm_device *pwm, 205f051c466SThierry Reding int duty_ns, int period_ns); 2066bc7064aSThierry Reding int (*set_polarity)(struct pwm_chip *chip, struct pwm_device *pwm, 2070aa0869cSPhilip, Avinash enum pwm_polarity polarity); 2086bc7064aSThierry Reding int (*enable)(struct pwm_chip *chip, struct pwm_device *pwm); 2096bc7064aSThierry Reding void (*disable)(struct pwm_chip *chip, struct pwm_device *pwm); 2105ec803edSBoris Brezillon int (*apply)(struct pwm_chip *chip, struct pwm_device *pwm, 2115ec803edSBoris Brezillon struct pwm_state *state); 21215fa8a43SBoris Brezillon void (*get_state)(struct pwm_chip *chip, struct pwm_device *pwm, 21315fa8a43SBoris Brezillon struct pwm_state *state); 21462099abfSThierry Reding #ifdef CONFIG_DEBUG_FS 2156bc7064aSThierry Reding void (*dbg_show)(struct pwm_chip *chip, struct seq_file *s); 21662099abfSThierry Reding #endif 2170c2498f1SSascha Hauer struct module *owner; 2180c2498f1SSascha Hauer }; 2190c2498f1SSascha Hauer 2200c2498f1SSascha Hauer /** 221f051c466SThierry Reding * struct pwm_chip - abstract a PWM controller 222f051c466SThierry Reding * @dev: device providing the PWMs 223f051c466SThierry Reding * @list: list node for internal use 224f051c466SThierry Reding * @ops: callbacks for this PWM controller 225f051c466SThierry Reding * @base: number of first PWM controlled by this chip 226f051c466SThierry Reding * @npwm: number of PWMs controlled by this chip 227f051c466SThierry Reding * @pwms: array of PWM devices allocated by the framework 22804883802SThierry Reding * @of_xlate: request a PWM device given a device tree PWM specifier 22904883802SThierry Reding * @of_pwm_n_cells: number of cells expected in the device tree PWM specifier 2306e69ab13SFlorian Vaussard * @can_sleep: must be true if the .config(), .enable() or .disable() 2316e69ab13SFlorian Vaussard * operations may sleep 2320c2498f1SSascha Hauer */ 2330c2498f1SSascha Hauer struct pwm_chip { 234f051c466SThierry Reding struct device *dev; 235f051c466SThierry Reding struct list_head list; 236f051c466SThierry Reding const struct pwm_ops *ops; 237f051c466SThierry Reding int base; 238f051c466SThierry Reding unsigned int npwm; 239f051c466SThierry Reding 240f051c466SThierry Reding struct pwm_device *pwms; 2417299ab70SThierry Reding 2427299ab70SThierry Reding struct pwm_device * (*of_xlate)(struct pwm_chip *pc, 2437299ab70SThierry Reding const struct of_phandle_args *args); 2447299ab70SThierry Reding unsigned int of_pwm_n_cells; 2456e69ab13SFlorian Vaussard bool can_sleep; 2460c2498f1SSascha Hauer }; 2470c2498f1SSascha Hauer 2480bcf168bSTushar Behera #if IS_ENABLED(CONFIG_PWM) 2495ec803edSBoris Brezillon /* PWM user APIs */ 2505ec803edSBoris Brezillon struct pwm_device *pwm_request(int pwm_id, const char *label); 2515ec803edSBoris Brezillon void pwm_free(struct pwm_device *pwm); 2525ec803edSBoris Brezillon int pwm_apply_state(struct pwm_device *pwm, struct pwm_state *state); 2535ec803edSBoris Brezillon int pwm_adjust_config(struct pwm_device *pwm); 2545ec803edSBoris Brezillon 2555ec803edSBoris Brezillon /** 2565ec803edSBoris Brezillon * pwm_config() - change a PWM device configuration 2575ec803edSBoris Brezillon * @pwm: PWM device 2585ec803edSBoris Brezillon * @duty_ns: "on" time (in nanoseconds) 2595ec803edSBoris Brezillon * @period_ns: duration (in nanoseconds) of one cycle 2605ec803edSBoris Brezillon * 2615ec803edSBoris Brezillon * Returns: 0 on success or a negative error code on failure. 2625ec803edSBoris Brezillon */ 2635ec803edSBoris Brezillon static inline int pwm_config(struct pwm_device *pwm, int duty_ns, 2645ec803edSBoris Brezillon int period_ns) 2655ec803edSBoris Brezillon { 2665ec803edSBoris Brezillon struct pwm_state state; 2675ec803edSBoris Brezillon 2685ec803edSBoris Brezillon if (!pwm) 2695ec803edSBoris Brezillon return -EINVAL; 2705ec803edSBoris Brezillon 2715ec803edSBoris Brezillon pwm_get_state(pwm, &state); 2725ec803edSBoris Brezillon if (state.duty_cycle == duty_ns && state.period == period_ns) 2735ec803edSBoris Brezillon return 0; 2745ec803edSBoris Brezillon 2755ec803edSBoris Brezillon state.duty_cycle = duty_ns; 2765ec803edSBoris Brezillon state.period = period_ns; 2775ec803edSBoris Brezillon return pwm_apply_state(pwm, &state); 2785ec803edSBoris Brezillon } 2795ec803edSBoris Brezillon 2805ec803edSBoris Brezillon /** 2815ec803edSBoris Brezillon * pwm_set_polarity() - configure the polarity of a PWM signal 2825ec803edSBoris Brezillon * @pwm: PWM device 2835ec803edSBoris Brezillon * @polarity: new polarity of the PWM signal 2845ec803edSBoris Brezillon * 2855ec803edSBoris Brezillon * Note that the polarity cannot be configured while the PWM device is 2865ec803edSBoris Brezillon * enabled. 2875ec803edSBoris Brezillon * 2885ec803edSBoris Brezillon * Returns: 0 on success or a negative error code on failure. 2895ec803edSBoris Brezillon */ 2905ec803edSBoris Brezillon static inline int pwm_set_polarity(struct pwm_device *pwm, 2915ec803edSBoris Brezillon enum pwm_polarity polarity) 2925ec803edSBoris Brezillon { 2935ec803edSBoris Brezillon struct pwm_state state; 2945ec803edSBoris Brezillon 2955ec803edSBoris Brezillon if (!pwm) 2965ec803edSBoris Brezillon return -EINVAL; 2975ec803edSBoris Brezillon 2985ec803edSBoris Brezillon pwm_get_state(pwm, &state); 2995ec803edSBoris Brezillon if (state.polarity == polarity) 3005ec803edSBoris Brezillon return 0; 3015ec803edSBoris Brezillon 3025ec803edSBoris Brezillon /* 3035ec803edSBoris Brezillon * Changing the polarity of a running PWM without adjusting the 3045ec803edSBoris Brezillon * dutycycle/period value is a bit risky (can introduce glitches). 3055ec803edSBoris Brezillon * Return -EBUSY in this case. 3065ec803edSBoris Brezillon * Note that this is allowed when using pwm_apply_state() because 3075ec803edSBoris Brezillon * the user specifies all the parameters. 3085ec803edSBoris Brezillon */ 3095ec803edSBoris Brezillon if (state.enabled) 3105ec803edSBoris Brezillon return -EBUSY; 3115ec803edSBoris Brezillon 3125ec803edSBoris Brezillon state.polarity = polarity; 3135ec803edSBoris Brezillon return pwm_apply_state(pwm, &state); 3145ec803edSBoris Brezillon } 3155ec803edSBoris Brezillon 3165ec803edSBoris Brezillon /** 3175ec803edSBoris Brezillon * pwm_enable() - start a PWM output toggling 3185ec803edSBoris Brezillon * @pwm: PWM device 3195ec803edSBoris Brezillon * 3205ec803edSBoris Brezillon * Returns: 0 on success or a negative error code on failure. 3215ec803edSBoris Brezillon */ 3225ec803edSBoris Brezillon static inline int pwm_enable(struct pwm_device *pwm) 3235ec803edSBoris Brezillon { 3245ec803edSBoris Brezillon struct pwm_state state; 3255ec803edSBoris Brezillon 3265ec803edSBoris Brezillon if (!pwm) 3275ec803edSBoris Brezillon return -EINVAL; 3285ec803edSBoris Brezillon 3295ec803edSBoris Brezillon pwm_get_state(pwm, &state); 3305ec803edSBoris Brezillon if (state.enabled) 3315ec803edSBoris Brezillon return 0; 3325ec803edSBoris Brezillon 3335ec803edSBoris Brezillon state.enabled = true; 3345ec803edSBoris Brezillon return pwm_apply_state(pwm, &state); 3355ec803edSBoris Brezillon } 3365ec803edSBoris Brezillon 3375ec803edSBoris Brezillon /** 3385ec803edSBoris Brezillon * pwm_disable() - stop a PWM output toggling 3395ec803edSBoris Brezillon * @pwm: PWM device 3405ec803edSBoris Brezillon */ 3415ec803edSBoris Brezillon static inline void pwm_disable(struct pwm_device *pwm) 3425ec803edSBoris Brezillon { 3435ec803edSBoris Brezillon struct pwm_state state; 3445ec803edSBoris Brezillon 3455ec803edSBoris Brezillon if (!pwm) 3465ec803edSBoris Brezillon return; 3475ec803edSBoris Brezillon 3485ec803edSBoris Brezillon pwm_get_state(pwm, &state); 3495ec803edSBoris Brezillon if (!state.enabled) 3505ec803edSBoris Brezillon return; 3515ec803edSBoris Brezillon 3525ec803edSBoris Brezillon state.enabled = false; 3535ec803edSBoris Brezillon pwm_apply_state(pwm, &state); 3545ec803edSBoris Brezillon } 3555ec803edSBoris Brezillon 3565ec803edSBoris Brezillon 3575ec803edSBoris Brezillon /* PWM provider APIs */ 358f051c466SThierry Reding int pwm_set_chip_data(struct pwm_device *pwm, void *data); 359f051c466SThierry Reding void *pwm_get_chip_data(struct pwm_device *pwm); 360f051c466SThierry Reding 361b6a00faeSTim Kryger int pwmchip_add_with_polarity(struct pwm_chip *chip, 362b6a00faeSTim Kryger enum pwm_polarity polarity); 3630c2498f1SSascha Hauer int pwmchip_add(struct pwm_chip *chip); 3640c2498f1SSascha Hauer int pwmchip_remove(struct pwm_chip *chip); 365f051c466SThierry Reding struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip, 366f051c466SThierry Reding unsigned int index, 367f051c466SThierry Reding const char *label); 3688138d2ddSThierry Reding 36983af2402SPhilip, Avinash struct pwm_device *of_pwm_xlate_with_flags(struct pwm_chip *pc, 37083af2402SPhilip, Avinash const struct of_phandle_args *args); 37183af2402SPhilip, Avinash 372d4c0c470SPeter Ujfalusi struct pwm_device *pwm_get(struct device *dev, const char *con_id); 3738eb96127SPeter Ujfalusi struct pwm_device *of_pwm_get(struct device_node *np, const char *con_id); 3748138d2ddSThierry Reding void pwm_put(struct pwm_device *pwm); 3758138d2ddSThierry Reding 376d4c0c470SPeter Ujfalusi struct pwm_device *devm_pwm_get(struct device *dev, const char *con_id); 377261a5eddSPeter Ujfalusi struct pwm_device *devm_of_pwm_get(struct device *dev, struct device_node *np, 378261a5eddSPeter Ujfalusi const char *con_id); 3796354316dSAlexandre Courbot void devm_pwm_put(struct device *dev, struct pwm_device *pwm); 3806e69ab13SFlorian Vaussard 3816e69ab13SFlorian Vaussard bool pwm_can_sleep(struct pwm_device *pwm); 3820bcf168bSTushar Behera #else 3835ec803edSBoris Brezillon static inline struct pwm_device *pwm_request(int pwm_id, const char *label) 3845ec803edSBoris Brezillon { 3855ec803edSBoris Brezillon return ERR_PTR(-ENODEV); 3865ec803edSBoris Brezillon } 3875ec803edSBoris Brezillon 3885ec803edSBoris Brezillon static inline void pwm_free(struct pwm_device *pwm) 3895ec803edSBoris Brezillon { 3905ec803edSBoris Brezillon } 3915ec803edSBoris Brezillon 3925ec803edSBoris Brezillon static inline int pwm_apply_state(struct pwm_device *pwm, 3935ec803edSBoris Brezillon const struct pwm_state *state) 3945ec803edSBoris Brezillon { 3955ec803edSBoris Brezillon return -ENOTSUPP; 3965ec803edSBoris Brezillon } 3975ec803edSBoris Brezillon 3985ec803edSBoris Brezillon static inline int pwm_adjust_config(struct pwm_device *pwm) 3995ec803edSBoris Brezillon { 4005ec803edSBoris Brezillon return -ENOTSUPP; 4015ec803edSBoris Brezillon } 4025ec803edSBoris Brezillon 4035ec803edSBoris Brezillon static inline int pwm_config(struct pwm_device *pwm, int duty_ns, 4045ec803edSBoris Brezillon int period_ns) 4055ec803edSBoris Brezillon { 4065ec803edSBoris Brezillon return -EINVAL; 4075ec803edSBoris Brezillon } 4085ec803edSBoris Brezillon 4095ec803edSBoris Brezillon static inline int pwm_set_polarity(struct pwm_device *pwm, 4105ec803edSBoris Brezillon enum pwm_polarity polarity) 4115ec803edSBoris Brezillon { 4125ec803edSBoris Brezillon return -ENOTSUPP; 4135ec803edSBoris Brezillon } 4145ec803edSBoris Brezillon 4155ec803edSBoris Brezillon static inline int pwm_enable(struct pwm_device *pwm) 4165ec803edSBoris Brezillon { 4175ec803edSBoris Brezillon return -EINVAL; 4185ec803edSBoris Brezillon } 4195ec803edSBoris Brezillon 4205ec803edSBoris Brezillon static inline void pwm_disable(struct pwm_device *pwm) 4215ec803edSBoris Brezillon { 4225ec803edSBoris Brezillon } 4235ec803edSBoris Brezillon 4240bcf168bSTushar Behera static inline int pwm_set_chip_data(struct pwm_device *pwm, void *data) 4250bcf168bSTushar Behera { 4260bcf168bSTushar Behera return -EINVAL; 4270bcf168bSTushar Behera } 4280bcf168bSTushar Behera 4290bcf168bSTushar Behera static inline void *pwm_get_chip_data(struct pwm_device *pwm) 4300bcf168bSTushar Behera { 4310bcf168bSTushar Behera return NULL; 4320bcf168bSTushar Behera } 4330bcf168bSTushar Behera 4340bcf168bSTushar Behera static inline int pwmchip_add(struct pwm_chip *chip) 4350bcf168bSTushar Behera { 4360bcf168bSTushar Behera return -EINVAL; 4370bcf168bSTushar Behera } 4380bcf168bSTushar Behera 439b6a00faeSTim Kryger static inline int pwmchip_add_inversed(struct pwm_chip *chip) 440b6a00faeSTim Kryger { 441b6a00faeSTim Kryger return -EINVAL; 442b6a00faeSTim Kryger } 443b6a00faeSTim Kryger 4440bcf168bSTushar Behera static inline int pwmchip_remove(struct pwm_chip *chip) 4450bcf168bSTushar Behera { 4460bcf168bSTushar Behera return -EINVAL; 4470bcf168bSTushar Behera } 4480bcf168bSTushar Behera 4490bcf168bSTushar Behera static inline struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip, 4500bcf168bSTushar Behera unsigned int index, 4510bcf168bSTushar Behera const char *label) 4520bcf168bSTushar Behera { 4530bcf168bSTushar Behera return ERR_PTR(-ENODEV); 4540bcf168bSTushar Behera } 4550bcf168bSTushar Behera 4560bcf168bSTushar Behera static inline struct pwm_device *pwm_get(struct device *dev, 4570bcf168bSTushar Behera const char *consumer) 4580bcf168bSTushar Behera { 4590bcf168bSTushar Behera return ERR_PTR(-ENODEV); 4600bcf168bSTushar Behera } 4610bcf168bSTushar Behera 4628eb96127SPeter Ujfalusi static inline struct pwm_device *of_pwm_get(struct device_node *np, 4638eb96127SPeter Ujfalusi const char *con_id) 4648eb96127SPeter Ujfalusi { 4658eb96127SPeter Ujfalusi return ERR_PTR(-ENODEV); 4668eb96127SPeter Ujfalusi } 4678eb96127SPeter Ujfalusi 4680bcf168bSTushar Behera static inline void pwm_put(struct pwm_device *pwm) 4690bcf168bSTushar Behera { 4700bcf168bSTushar Behera } 4710bcf168bSTushar Behera 4720bcf168bSTushar Behera static inline struct pwm_device *devm_pwm_get(struct device *dev, 4730bcf168bSTushar Behera const char *consumer) 4740bcf168bSTushar Behera { 4750bcf168bSTushar Behera return ERR_PTR(-ENODEV); 4760bcf168bSTushar Behera } 4770bcf168bSTushar Behera 478261a5eddSPeter Ujfalusi static inline struct pwm_device *devm_of_pwm_get(struct device *dev, 479261a5eddSPeter Ujfalusi struct device_node *np, 480261a5eddSPeter Ujfalusi const char *con_id) 481261a5eddSPeter Ujfalusi { 482261a5eddSPeter Ujfalusi return ERR_PTR(-ENODEV); 483261a5eddSPeter Ujfalusi } 484261a5eddSPeter Ujfalusi 4850bcf168bSTushar Behera static inline void devm_pwm_put(struct device *dev, struct pwm_device *pwm) 4860bcf168bSTushar Behera { 4870bcf168bSTushar Behera } 4886e69ab13SFlorian Vaussard 4896e69ab13SFlorian Vaussard static inline bool pwm_can_sleep(struct pwm_device *pwm) 4906e69ab13SFlorian Vaussard { 4916e69ab13SFlorian Vaussard return false; 4926e69ab13SFlorian Vaussard } 4930bcf168bSTushar Behera #endif 4946354316dSAlexandre Courbot 4955ec803edSBoris Brezillon static inline void pwm_apply_args(struct pwm_device *pwm) 4965ec803edSBoris Brezillon { 4975ec803edSBoris Brezillon /* 4985ec803edSBoris Brezillon * PWM users calling pwm_apply_args() expect to have a fresh config 4995ec803edSBoris Brezillon * where the polarity and period are set according to pwm_args info. 5005ec803edSBoris Brezillon * The problem is, polarity can only be changed when the PWM is 5015ec803edSBoris Brezillon * disabled. 5025ec803edSBoris Brezillon * 5035ec803edSBoris Brezillon * PWM drivers supporting hardware readout may declare the PWM device 5045ec803edSBoris Brezillon * as enabled, and prevent polarity setting, which changes from the 5055ec803edSBoris Brezillon * existing behavior, where all PWM devices are declared as disabled 5065ec803edSBoris Brezillon * at startup (even if they are actually enabled), thus authorizing 5075ec803edSBoris Brezillon * polarity setting. 5085ec803edSBoris Brezillon * 5095ec803edSBoris Brezillon * Instead of setting ->enabled to false, we call pwm_disable() 5105ec803edSBoris Brezillon * before pwm_set_polarity() to ensure that everything is configured 5115ec803edSBoris Brezillon * as expected, and the PWM is really disabled when the user request 5125ec803edSBoris Brezillon * it. 5135ec803edSBoris Brezillon * 5145ec803edSBoris Brezillon * Note that PWM users requiring a smooth handover between the 5155ec803edSBoris Brezillon * bootloader and the kernel (like critical regulators controlled by 5165ec803edSBoris Brezillon * PWM devices) will have to switch to the atomic API and avoid calling 5175ec803edSBoris Brezillon * pwm_apply_args(). 5185ec803edSBoris Brezillon */ 5195ec803edSBoris Brezillon pwm_disable(pwm); 5205ec803edSBoris Brezillon pwm_set_polarity(pwm, pwm->args.polarity); 5215ec803edSBoris Brezillon } 5225ec803edSBoris Brezillon 5238138d2ddSThierry Reding struct pwm_lookup { 5248138d2ddSThierry Reding struct list_head list; 5258138d2ddSThierry Reding const char *provider; 5268138d2ddSThierry Reding unsigned int index; 5278138d2ddSThierry Reding const char *dev_id; 5288138d2ddSThierry Reding const char *con_id; 5293796ce1dSAlexandre Belloni unsigned int period; 5303796ce1dSAlexandre Belloni enum pwm_polarity polarity; 5318138d2ddSThierry Reding }; 5328138d2ddSThierry Reding 53342844029SAlexandre Belloni #define PWM_LOOKUP(_provider, _index, _dev_id, _con_id, _period, _polarity) \ 5348138d2ddSThierry Reding { \ 5358138d2ddSThierry Reding .provider = _provider, \ 5368138d2ddSThierry Reding .index = _index, \ 5378138d2ddSThierry Reding .dev_id = _dev_id, \ 5388138d2ddSThierry Reding .con_id = _con_id, \ 53942844029SAlexandre Belloni .period = _period, \ 54042844029SAlexandre Belloni .polarity = _polarity \ 5418138d2ddSThierry Reding } 5428138d2ddSThierry Reding 5430bcf168bSTushar Behera #if IS_ENABLED(CONFIG_PWM) 5448138d2ddSThierry Reding void pwm_add_table(struct pwm_lookup *table, size_t num); 545efb0de55SShobhit Kumar void pwm_remove_table(struct pwm_lookup *table, size_t num); 5460bcf168bSTushar Behera #else 5470bcf168bSTushar Behera static inline void pwm_add_table(struct pwm_lookup *table, size_t num) 5480bcf168bSTushar Behera { 5490bcf168bSTushar Behera } 550efb0de55SShobhit Kumar 551efb0de55SShobhit Kumar static inline void pwm_remove_table(struct pwm_lookup *table, size_t num) 552efb0de55SShobhit Kumar { 553efb0de55SShobhit Kumar } 5540c2498f1SSascha Hauer #endif 5550c2498f1SSascha Hauer 55676abbddeSH Hartley Sweeten #ifdef CONFIG_PWM_SYSFS 55776abbddeSH Hartley Sweeten void pwmchip_sysfs_export(struct pwm_chip *chip); 55876abbddeSH Hartley Sweeten void pwmchip_sysfs_unexport(struct pwm_chip *chip); 55976abbddeSH Hartley Sweeten #else 56076abbddeSH Hartley Sweeten static inline void pwmchip_sysfs_export(struct pwm_chip *chip) 56176abbddeSH Hartley Sweeten { 56276abbddeSH Hartley Sweeten } 56376abbddeSH Hartley Sweeten 56476abbddeSH Hartley Sweeten static inline void pwmchip_sysfs_unexport(struct pwm_chip *chip) 56576abbddeSH Hartley Sweeten { 56676abbddeSH Hartley Sweeten } 56776abbddeSH Hartley Sweeten #endif /* CONFIG_PWM_SYSFS */ 56876abbddeSH Hartley Sweeten 5695243ef8bSMark Vels #endif /* __LINUX_PWM_H */ 570