Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 #ifndef __LINUX_PWM_H
0003 #define __LINUX_PWM_H
0004 
0005 #include <linux/err.h>
0006 #include <linux/mutex.h>
0007 #include <linux/of.h>
0008 
0009 struct pwm_chip;
0010 
0011 /**
0012  * enum pwm_polarity - polarity of a PWM signal
0013  * @PWM_POLARITY_NORMAL: a high signal for the duration of the duty-
0014  * cycle, followed by a low signal for the remainder of the pulse
0015  * period
0016  * @PWM_POLARITY_INVERSED: a low signal for the duration of the duty-
0017  * cycle, followed by a high signal for the remainder of the pulse
0018  * period
0019  */
0020 enum pwm_polarity {
0021     PWM_POLARITY_NORMAL,
0022     PWM_POLARITY_INVERSED,
0023 };
0024 
0025 /**
0026  * struct pwm_args - board-dependent PWM arguments
0027  * @period: reference period
0028  * @polarity: reference polarity
0029  *
0030  * This structure describes board-dependent arguments attached to a PWM
0031  * device. These arguments are usually retrieved from the PWM lookup table or
0032  * device tree.
0033  *
0034  * Do not confuse this with the PWM state: PWM arguments represent the initial
0035  * configuration that users want to use on this PWM device rather than the
0036  * current PWM hardware state.
0037  */
0038 struct pwm_args {
0039     u64 period;
0040     enum pwm_polarity polarity;
0041 };
0042 
0043 enum {
0044     PWMF_REQUESTED = 1 << 0,
0045     PWMF_EXPORTED = 1 << 1,
0046 };
0047 
0048 /*
0049  * struct pwm_state - state of a PWM channel
0050  * @period: PWM period (in nanoseconds)
0051  * @duty_cycle: PWM duty cycle (in nanoseconds)
0052  * @polarity: PWM polarity
0053  * @enabled: PWM enabled status
0054  * @usage_power: If set, the PWM driver is only required to maintain the power
0055  *               output but has more freedom regarding signal form.
0056  *               If supported, the signal can be optimized, for example to
0057  *               improve EMI by phase shifting individual channels.
0058  */
0059 struct pwm_state {
0060     u64 period;
0061     u64 duty_cycle;
0062     enum pwm_polarity polarity;
0063     bool enabled;
0064     bool usage_power;
0065 };
0066 
0067 /**
0068  * struct pwm_device - PWM channel object
0069  * @label: name of the PWM device
0070  * @flags: flags associated with the PWM device
0071  * @hwpwm: per-chip relative index of the PWM device
0072  * @pwm: global index of the PWM device
0073  * @chip: PWM chip providing this PWM device
0074  * @chip_data: chip-private data associated with the PWM device
0075  * @args: PWM arguments
0076  * @state: last applied state
0077  * @last: last implemented state (for PWM_DEBUG)
0078  */
0079 struct pwm_device {
0080     const char *label;
0081     unsigned long flags;
0082     unsigned int hwpwm;
0083     unsigned int pwm;
0084     struct pwm_chip *chip;
0085     void *chip_data;
0086 
0087     struct pwm_args args;
0088     struct pwm_state state;
0089     struct pwm_state last;
0090 };
0091 
0092 /**
0093  * pwm_get_state() - retrieve the current PWM state
0094  * @pwm: PWM device
0095  * @state: state to fill with the current PWM state
0096  *
0097  * The returned PWM state represents the state that was applied by a previous call to
0098  * pwm_apply_state(). Drivers may have to slightly tweak that state before programming it to
0099  * hardware. If pwm_apply_state() was never called, this returns either the current hardware
0100  * state (if supported) or the default settings.
0101  */
0102 static inline void pwm_get_state(const struct pwm_device *pwm,
0103                  struct pwm_state *state)
0104 {
0105     *state = pwm->state;
0106 }
0107 
0108 static inline bool pwm_is_enabled(const struct pwm_device *pwm)
0109 {
0110     struct pwm_state state;
0111 
0112     pwm_get_state(pwm, &state);
0113 
0114     return state.enabled;
0115 }
0116 
0117 static inline void pwm_set_period(struct pwm_device *pwm, u64 period)
0118 {
0119     if (pwm)
0120         pwm->state.period = period;
0121 }
0122 
0123 static inline u64 pwm_get_period(const struct pwm_device *pwm)
0124 {
0125     struct pwm_state state;
0126 
0127     pwm_get_state(pwm, &state);
0128 
0129     return state.period;
0130 }
0131 
0132 static inline void pwm_set_duty_cycle(struct pwm_device *pwm, unsigned int duty)
0133 {
0134     if (pwm)
0135         pwm->state.duty_cycle = duty;
0136 }
0137 
0138 static inline u64 pwm_get_duty_cycle(const struct pwm_device *pwm)
0139 {
0140     struct pwm_state state;
0141 
0142     pwm_get_state(pwm, &state);
0143 
0144     return state.duty_cycle;
0145 }
0146 
0147 static inline enum pwm_polarity pwm_get_polarity(const struct pwm_device *pwm)
0148 {
0149     struct pwm_state state;
0150 
0151     pwm_get_state(pwm, &state);
0152 
0153     return state.polarity;
0154 }
0155 
0156 static inline void pwm_get_args(const struct pwm_device *pwm,
0157                 struct pwm_args *args)
0158 {
0159     *args = pwm->args;
0160 }
0161 
0162 /**
0163  * pwm_init_state() - prepare a new state to be applied with pwm_apply_state()
0164  * @pwm: PWM device
0165  * @state: state to fill with the prepared PWM state
0166  *
0167  * This functions prepares a state that can later be tweaked and applied
0168  * to the PWM device with pwm_apply_state(). This is a convenient function
0169  * that first retrieves the current PWM state and the replaces the period
0170  * and polarity fields with the reference values defined in pwm->args.
0171  * Once the function returns, you can adjust the ->enabled and ->duty_cycle
0172  * fields according to your needs before calling pwm_apply_state().
0173  *
0174  * ->duty_cycle is initially set to zero to avoid cases where the current
0175  * ->duty_cycle value exceed the pwm_args->period one, which would trigger
0176  * an error if the user calls pwm_apply_state() without adjusting ->duty_cycle
0177  * first.
0178  */
0179 static inline void pwm_init_state(const struct pwm_device *pwm,
0180                   struct pwm_state *state)
0181 {
0182     struct pwm_args args;
0183 
0184     /* First get the current state. */
0185     pwm_get_state(pwm, state);
0186 
0187     /* Then fill it with the reference config */
0188     pwm_get_args(pwm, &args);
0189 
0190     state->period = args.period;
0191     state->polarity = args.polarity;
0192     state->duty_cycle = 0;
0193     state->usage_power = false;
0194 }
0195 
0196 /**
0197  * pwm_get_relative_duty_cycle() - Get a relative duty cycle value
0198  * @state: PWM state to extract the duty cycle from
0199  * @scale: target scale of the relative duty cycle
0200  *
0201  * This functions converts the absolute duty cycle stored in @state (expressed
0202  * in nanosecond) into a value relative to the period.
0203  *
0204  * For example if you want to get the duty_cycle expressed in percent, call:
0205  *
0206  * pwm_get_state(pwm, &state);
0207  * duty = pwm_get_relative_duty_cycle(&state, 100);
0208  */
0209 static inline unsigned int
0210 pwm_get_relative_duty_cycle(const struct pwm_state *state, unsigned int scale)
0211 {
0212     if (!state->period)
0213         return 0;
0214 
0215     return DIV_ROUND_CLOSEST_ULL((u64)state->duty_cycle * scale,
0216                      state->period);
0217 }
0218 
0219 /**
0220  * pwm_set_relative_duty_cycle() - Set a relative duty cycle value
0221  * @state: PWM state to fill
0222  * @duty_cycle: relative duty cycle value
0223  * @scale: scale in which @duty_cycle is expressed
0224  *
0225  * This functions converts a relative into an absolute duty cycle (expressed
0226  * in nanoseconds), and puts the result in state->duty_cycle.
0227  *
0228  * For example if you want to configure a 50% duty cycle, call:
0229  *
0230  * pwm_init_state(pwm, &state);
0231  * pwm_set_relative_duty_cycle(&state, 50, 100);
0232  * pwm_apply_state(pwm, &state);
0233  *
0234  * This functions returns -EINVAL if @duty_cycle and/or @scale are
0235  * inconsistent (@scale == 0 or @duty_cycle > @scale).
0236  */
0237 static inline int
0238 pwm_set_relative_duty_cycle(struct pwm_state *state, unsigned int duty_cycle,
0239                 unsigned int scale)
0240 {
0241     if (!scale || duty_cycle > scale)
0242         return -EINVAL;
0243 
0244     state->duty_cycle = DIV_ROUND_CLOSEST_ULL((u64)duty_cycle *
0245                           state->period,
0246                           scale);
0247 
0248     return 0;
0249 }
0250 
0251 /**
0252  * struct pwm_capture - PWM capture data
0253  * @period: period of the PWM signal (in nanoseconds)
0254  * @duty_cycle: duty cycle of the PWM signal (in nanoseconds)
0255  */
0256 struct pwm_capture {
0257     unsigned int period;
0258     unsigned int duty_cycle;
0259 };
0260 
0261 /**
0262  * struct pwm_ops - PWM controller operations
0263  * @request: optional hook for requesting a PWM
0264  * @free: optional hook for freeing a PWM
0265  * @capture: capture and report PWM signal
0266  * @apply: atomically apply a new PWM config
0267  * @get_state: get the current PWM state. This function is only
0268  *         called once per PWM device when the PWM chip is
0269  *         registered.
0270  * @owner: helps prevent removal of modules exporting active PWMs
0271  */
0272 struct pwm_ops {
0273     int (*request)(struct pwm_chip *chip, struct pwm_device *pwm);
0274     void (*free)(struct pwm_chip *chip, struct pwm_device *pwm);
0275     int (*capture)(struct pwm_chip *chip, struct pwm_device *pwm,
0276                struct pwm_capture *result, unsigned long timeout);
0277     int (*apply)(struct pwm_chip *chip, struct pwm_device *pwm,
0278              const struct pwm_state *state);
0279     void (*get_state)(struct pwm_chip *chip, struct pwm_device *pwm,
0280               struct pwm_state *state);
0281     struct module *owner;
0282 };
0283 
0284 /**
0285  * struct pwm_chip - abstract a PWM controller
0286  * @dev: device providing the PWMs
0287  * @ops: callbacks for this PWM controller
0288  * @base: number of first PWM controlled by this chip
0289  * @npwm: number of PWMs controlled by this chip
0290  * @of_xlate: request a PWM device given a device tree PWM specifier
0291  * @of_pwm_n_cells: number of cells expected in the device tree PWM specifier
0292  * @list: list node for internal use
0293  * @pwms: array of PWM devices allocated by the framework
0294  */
0295 struct pwm_chip {
0296     struct device *dev;
0297     const struct pwm_ops *ops;
0298     int base;
0299     unsigned int npwm;
0300 
0301     struct pwm_device * (*of_xlate)(struct pwm_chip *pc,
0302                     const struct of_phandle_args *args);
0303     unsigned int of_pwm_n_cells;
0304 
0305     /* only used internally by the PWM framework */
0306     struct list_head list;
0307     struct pwm_device *pwms;
0308 };
0309 
0310 #if IS_ENABLED(CONFIG_PWM)
0311 /* PWM user APIs */
0312 struct pwm_device *pwm_request(int pwm_id, const char *label);
0313 void pwm_free(struct pwm_device *pwm);
0314 int pwm_apply_state(struct pwm_device *pwm, const struct pwm_state *state);
0315 int pwm_adjust_config(struct pwm_device *pwm);
0316 
0317 /**
0318  * pwm_config() - change a PWM device configuration
0319  * @pwm: PWM device
0320  * @duty_ns: "on" time (in nanoseconds)
0321  * @period_ns: duration (in nanoseconds) of one cycle
0322  *
0323  * Returns: 0 on success or a negative error code on failure.
0324  */
0325 static inline int pwm_config(struct pwm_device *pwm, int duty_ns,
0326                  int period_ns)
0327 {
0328     struct pwm_state state;
0329 
0330     if (!pwm)
0331         return -EINVAL;
0332 
0333     if (duty_ns < 0 || period_ns < 0)
0334         return -EINVAL;
0335 
0336     pwm_get_state(pwm, &state);
0337     if (state.duty_cycle == duty_ns && state.period == period_ns)
0338         return 0;
0339 
0340     state.duty_cycle = duty_ns;
0341     state.period = period_ns;
0342     return pwm_apply_state(pwm, &state);
0343 }
0344 
0345 /**
0346  * pwm_enable() - start a PWM output toggling
0347  * @pwm: PWM device
0348  *
0349  * Returns: 0 on success or a negative error code on failure.
0350  */
0351 static inline int pwm_enable(struct pwm_device *pwm)
0352 {
0353     struct pwm_state state;
0354 
0355     if (!pwm)
0356         return -EINVAL;
0357 
0358     pwm_get_state(pwm, &state);
0359     if (state.enabled)
0360         return 0;
0361 
0362     state.enabled = true;
0363     return pwm_apply_state(pwm, &state);
0364 }
0365 
0366 /**
0367  * pwm_disable() - stop a PWM output toggling
0368  * @pwm: PWM device
0369  */
0370 static inline void pwm_disable(struct pwm_device *pwm)
0371 {
0372     struct pwm_state state;
0373 
0374     if (!pwm)
0375         return;
0376 
0377     pwm_get_state(pwm, &state);
0378     if (!state.enabled)
0379         return;
0380 
0381     state.enabled = false;
0382     pwm_apply_state(pwm, &state);
0383 }
0384 
0385 /* PWM provider APIs */
0386 int pwm_capture(struct pwm_device *pwm, struct pwm_capture *result,
0387         unsigned long timeout);
0388 int pwm_set_chip_data(struct pwm_device *pwm, void *data);
0389 void *pwm_get_chip_data(struct pwm_device *pwm);
0390 
0391 int pwmchip_add(struct pwm_chip *chip);
0392 void pwmchip_remove(struct pwm_chip *chip);
0393 
0394 int devm_pwmchip_add(struct device *dev, struct pwm_chip *chip);
0395 
0396 struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip,
0397                      unsigned int index,
0398                      const char *label);
0399 
0400 struct pwm_device *of_pwm_xlate_with_flags(struct pwm_chip *pc,
0401         const struct of_phandle_args *args);
0402 struct pwm_device *of_pwm_single_xlate(struct pwm_chip *pc,
0403                        const struct of_phandle_args *args);
0404 
0405 struct pwm_device *pwm_get(struct device *dev, const char *con_id);
0406 struct pwm_device *of_pwm_get(struct device *dev, struct device_node *np,
0407                   const char *con_id);
0408 void pwm_put(struct pwm_device *pwm);
0409 
0410 struct pwm_device *devm_pwm_get(struct device *dev, const char *con_id);
0411 struct pwm_device *devm_of_pwm_get(struct device *dev, struct device_node *np,
0412                    const char *con_id);
0413 struct pwm_device *devm_fwnode_pwm_get(struct device *dev,
0414                        struct fwnode_handle *fwnode,
0415                        const char *con_id);
0416 #else
0417 static inline struct pwm_device *pwm_request(int pwm_id, const char *label)
0418 {
0419     might_sleep();
0420     return ERR_PTR(-ENODEV);
0421 }
0422 
0423 static inline void pwm_free(struct pwm_device *pwm)
0424 {
0425     might_sleep();
0426 }
0427 
0428 static inline int pwm_apply_state(struct pwm_device *pwm,
0429                   const struct pwm_state *state)
0430 {
0431     might_sleep();
0432     return -ENOTSUPP;
0433 }
0434 
0435 static inline int pwm_adjust_config(struct pwm_device *pwm)
0436 {
0437     return -ENOTSUPP;
0438 }
0439 
0440 static inline int pwm_config(struct pwm_device *pwm, int duty_ns,
0441                  int period_ns)
0442 {
0443     might_sleep();
0444     return -EINVAL;
0445 }
0446 
0447 static inline int pwm_capture(struct pwm_device *pwm,
0448                   struct pwm_capture *result,
0449                   unsigned long timeout)
0450 {
0451     return -EINVAL;
0452 }
0453 
0454 static inline int pwm_enable(struct pwm_device *pwm)
0455 {
0456     might_sleep();
0457     return -EINVAL;
0458 }
0459 
0460 static inline void pwm_disable(struct pwm_device *pwm)
0461 {
0462     might_sleep();
0463 }
0464 
0465 static inline int pwm_set_chip_data(struct pwm_device *pwm, void *data)
0466 {
0467     return -EINVAL;
0468 }
0469 
0470 static inline void *pwm_get_chip_data(struct pwm_device *pwm)
0471 {
0472     return NULL;
0473 }
0474 
0475 static inline int pwmchip_add(struct pwm_chip *chip)
0476 {
0477     return -EINVAL;
0478 }
0479 
0480 static inline int pwmchip_remove(struct pwm_chip *chip)
0481 {
0482     return -EINVAL;
0483 }
0484 
0485 static inline struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip,
0486                                unsigned int index,
0487                                const char *label)
0488 {
0489     might_sleep();
0490     return ERR_PTR(-ENODEV);
0491 }
0492 
0493 static inline struct pwm_device *pwm_get(struct device *dev,
0494                      const char *consumer)
0495 {
0496     might_sleep();
0497     return ERR_PTR(-ENODEV);
0498 }
0499 
0500 static inline struct pwm_device *of_pwm_get(struct device *dev,
0501                         struct device_node *np,
0502                         const char *con_id)
0503 {
0504     might_sleep();
0505     return ERR_PTR(-ENODEV);
0506 }
0507 
0508 static inline void pwm_put(struct pwm_device *pwm)
0509 {
0510     might_sleep();
0511 }
0512 
0513 static inline struct pwm_device *devm_pwm_get(struct device *dev,
0514                           const char *consumer)
0515 {
0516     might_sleep();
0517     return ERR_PTR(-ENODEV);
0518 }
0519 
0520 static inline struct pwm_device *devm_of_pwm_get(struct device *dev,
0521                          struct device_node *np,
0522                          const char *con_id)
0523 {
0524     might_sleep();
0525     return ERR_PTR(-ENODEV);
0526 }
0527 
0528 static inline struct pwm_device *
0529 devm_fwnode_pwm_get(struct device *dev, struct fwnode_handle *fwnode,
0530             const char *con_id)
0531 {
0532     might_sleep();
0533     return ERR_PTR(-ENODEV);
0534 }
0535 #endif
0536 
0537 static inline void pwm_apply_args(struct pwm_device *pwm)
0538 {
0539     struct pwm_state state = { };
0540 
0541     /*
0542      * PWM users calling pwm_apply_args() expect to have a fresh config
0543      * where the polarity and period are set according to pwm_args info.
0544      * The problem is, polarity can only be changed when the PWM is
0545      * disabled.
0546      *
0547      * PWM drivers supporting hardware readout may declare the PWM device
0548      * as enabled, and prevent polarity setting, which changes from the
0549      * existing behavior, where all PWM devices are declared as disabled
0550      * at startup (even if they are actually enabled), thus authorizing
0551      * polarity setting.
0552      *
0553      * To fulfill this requirement, we apply a new state which disables
0554      * the PWM device and set the reference period and polarity config.
0555      *
0556      * Note that PWM users requiring a smooth handover between the
0557      * bootloader and the kernel (like critical regulators controlled by
0558      * PWM devices) will have to switch to the atomic API and avoid calling
0559      * pwm_apply_args().
0560      */
0561 
0562     state.enabled = false;
0563     state.polarity = pwm->args.polarity;
0564     state.period = pwm->args.period;
0565     state.usage_power = false;
0566 
0567     pwm_apply_state(pwm, &state);
0568 }
0569 
0570 struct pwm_lookup {
0571     struct list_head list;
0572     const char *provider;
0573     unsigned int index;
0574     const char *dev_id;
0575     const char *con_id;
0576     unsigned int period;
0577     enum pwm_polarity polarity;
0578     const char *module; /* optional, may be NULL */
0579 };
0580 
0581 #define PWM_LOOKUP_WITH_MODULE(_provider, _index, _dev_id, _con_id, \
0582                    _period, _polarity, _module)     \
0583     {                               \
0584         .provider = _provider,                  \
0585         .index = _index,                    \
0586         .dev_id = _dev_id,                  \
0587         .con_id = _con_id,                  \
0588         .period = _period,                  \
0589         .polarity = _polarity,                  \
0590         .module = _module,                  \
0591     }
0592 
0593 #define PWM_LOOKUP(_provider, _index, _dev_id, _con_id, _period, _polarity) \
0594     PWM_LOOKUP_WITH_MODULE(_provider, _index, _dev_id, _con_id, _period, \
0595                    _polarity, NULL)
0596 
0597 #if IS_ENABLED(CONFIG_PWM)
0598 void pwm_add_table(struct pwm_lookup *table, size_t num);
0599 void pwm_remove_table(struct pwm_lookup *table, size_t num);
0600 #else
0601 static inline void pwm_add_table(struct pwm_lookup *table, size_t num)
0602 {
0603 }
0604 
0605 static inline void pwm_remove_table(struct pwm_lookup *table, size_t num)
0606 {
0607 }
0608 #endif
0609 
0610 #ifdef CONFIG_PWM_SYSFS
0611 void pwmchip_sysfs_export(struct pwm_chip *chip);
0612 void pwmchip_sysfs_unexport(struct pwm_chip *chip);
0613 #else
0614 static inline void pwmchip_sysfs_export(struct pwm_chip *chip)
0615 {
0616 }
0617 
0618 static inline void pwmchip_sysfs_unexport(struct pwm_chip *chip)
0619 {
0620 }
0621 #endif /* CONFIG_PWM_SYSFS */
0622 
0623 #endif /* __LINUX_PWM_H */