pwm.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. #ifndef __LINUX_PWM_H
  2. #define __LINUX_PWM_H
  3. #include <linux/of.h>
  4. struct pwm_device;
  5. struct seq_file;
  6. /*
  7. * pwm_request - request a PWM device
  8. */
  9. struct pwm_device *pwm_request(int pwm_id, const char *label);
  10. /*
  11. * pwm_free - free a PWM device
  12. */
  13. void pwm_free(struct pwm_device *pwm);
  14. /*
  15. * pwm_config - change a PWM device configuration
  16. */
  17. int pwm_config(struct pwm_device *pwm, int duty_ns, int period_ns);
  18. /*
  19. * pwm_enable - start a PWM output toggling
  20. */
  21. int pwm_enable(struct pwm_device *pwm);
  22. /*
  23. * pwm_disable - stop a PWM output toggling
  24. */
  25. void pwm_disable(struct pwm_device *pwm);
  26. #ifdef CONFIG_PWM
  27. struct pwm_chip;
  28. /**
  29. * enum pwm_polarity - polarity of a PWM signal
  30. * @PWM_POLARITY_NORMAL: a high signal for the duration of the duty-
  31. * cycle, followed by a low signal for the remainder of the pulse
  32. * period
  33. * @PWM_POLARITY_INVERSED: a low signal for the duration of the duty-
  34. * cycle, followed by a high signal for the remainder of the pulse
  35. * period
  36. */
  37. enum pwm_polarity {
  38. PWM_POLARITY_NORMAL,
  39. PWM_POLARITY_INVERSED,
  40. };
  41. enum {
  42. PWMF_REQUESTED = 1 << 0,
  43. PWMF_ENABLED = 1 << 1,
  44. };
  45. struct pwm_device {
  46. const char *label;
  47. unsigned long flags;
  48. unsigned int hwpwm;
  49. unsigned int pwm;
  50. struct pwm_chip *chip;
  51. void *chip_data;
  52. unsigned int period; /* in nanoseconds */
  53. };
  54. static inline void pwm_set_period(struct pwm_device *pwm, unsigned int period)
  55. {
  56. if (pwm)
  57. pwm->period = period;
  58. }
  59. static inline unsigned int pwm_get_period(struct pwm_device *pwm)
  60. {
  61. return pwm ? pwm->period : 0;
  62. }
  63. /*
  64. * pwm_set_polarity - configure the polarity of a PWM signal
  65. */
  66. int pwm_set_polarity(struct pwm_device *pwm, enum pwm_polarity polarity);
  67. /**
  68. * struct pwm_ops - PWM controller operations
  69. * @request: optional hook for requesting a PWM
  70. * @free: optional hook for freeing a PWM
  71. * @config: configure duty cycles and period length for this PWM
  72. * @set_polarity: configure the polarity of this PWM
  73. * @enable: enable PWM output toggling
  74. * @disable: disable PWM output toggling
  75. * @dbg_show: optional routine to show contents in debugfs
  76. * @owner: helps prevent removal of modules exporting active PWMs
  77. */
  78. struct pwm_ops {
  79. int (*request)(struct pwm_chip *chip,
  80. struct pwm_device *pwm);
  81. void (*free)(struct pwm_chip *chip,
  82. struct pwm_device *pwm);
  83. int (*config)(struct pwm_chip *chip,
  84. struct pwm_device *pwm,
  85. int duty_ns, int period_ns);
  86. int (*set_polarity)(struct pwm_chip *chip,
  87. struct pwm_device *pwm,
  88. enum pwm_polarity polarity);
  89. int (*enable)(struct pwm_chip *chip,
  90. struct pwm_device *pwm);
  91. void (*disable)(struct pwm_chip *chip,
  92. struct pwm_device *pwm);
  93. #ifdef CONFIG_DEBUG_FS
  94. void (*dbg_show)(struct pwm_chip *chip,
  95. struct seq_file *s);
  96. #endif
  97. struct module *owner;
  98. };
  99. /**
  100. * struct pwm_chip - abstract a PWM controller
  101. * @dev: device providing the PWMs
  102. * @list: list node for internal use
  103. * @ops: callbacks for this PWM controller
  104. * @base: number of first PWM controlled by this chip
  105. * @npwm: number of PWMs controlled by this chip
  106. * @pwms: array of PWM devices allocated by the framework
  107. */
  108. struct pwm_chip {
  109. struct device *dev;
  110. struct list_head list;
  111. const struct pwm_ops *ops;
  112. int base;
  113. unsigned int npwm;
  114. struct pwm_device *pwms;
  115. struct pwm_device * (*of_xlate)(struct pwm_chip *pc,
  116. const struct of_phandle_args *args);
  117. unsigned int of_pwm_n_cells;
  118. };
  119. int pwm_set_chip_data(struct pwm_device *pwm, void *data);
  120. void *pwm_get_chip_data(struct pwm_device *pwm);
  121. int pwmchip_add(struct pwm_chip *chip);
  122. int pwmchip_remove(struct pwm_chip *chip);
  123. struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip,
  124. unsigned int index,
  125. const char *label);
  126. struct pwm_device *pwm_get(struct device *dev, const char *consumer);
  127. void pwm_put(struct pwm_device *pwm);
  128. struct pwm_device *devm_pwm_get(struct device *dev, const char *consumer);
  129. void devm_pwm_put(struct device *dev, struct pwm_device *pwm);
  130. struct pwm_lookup {
  131. struct list_head list;
  132. const char *provider;
  133. unsigned int index;
  134. const char *dev_id;
  135. const char *con_id;
  136. };
  137. #define PWM_LOOKUP(_provider, _index, _dev_id, _con_id) \
  138. { \
  139. .provider = _provider, \
  140. .index = _index, \
  141. .dev_id = _dev_id, \
  142. .con_id = _con_id, \
  143. }
  144. void pwm_add_table(struct pwm_lookup *table, size_t num);
  145. #endif
  146. #endif /* __LINUX_PWM_H */