pwm-iqs620a.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Azoteq IQS620A PWM Generator
  4. *
  5. * Copyright (C) 2019 Jeff LaBundy <jeff@labundy.com>
  6. *
  7. * Limitations:
  8. * - The period is fixed to 1 ms and is generated continuously despite changes
  9. * to the duty cycle or enable/disable state.
  10. * - Changes to the duty cycle or enable/disable state take effect immediately
  11. * and may result in a glitch during the period in which the change is made.
  12. * - The device cannot generate a 0% duty cycle. For duty cycles below 1 / 256
  13. * ms, the output is disabled and relies upon an external pull-down resistor
  14. * to hold the GPIO3/LTX pin low.
  15. */
  16. #include <linux/device.h>
  17. #include <linux/kernel.h>
  18. #include <linux/mfd/iqs62x.h>
  19. #include <linux/module.h>
  20. #include <linux/mutex.h>
  21. #include <linux/notifier.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/pwm.h>
  24. #include <linux/regmap.h>
  25. #include <linux/slab.h>
  26. #define IQS620_PWR_SETTINGS 0xd2
  27. #define IQS620_PWR_SETTINGS_PWM_OUT BIT(7)
  28. #define IQS620_PWM_DUTY_CYCLE 0xd8
  29. #define IQS620_PWM_PERIOD_NS 1000000
  30. struct iqs620_pwm_private {
  31. struct iqs62x_core *iqs62x;
  32. struct device *dev;
  33. struct notifier_block notifier;
  34. struct mutex lock;
  35. unsigned int duty_scale;
  36. };
  37. static inline struct iqs620_pwm_private *iqs620_pwm_from_chip(struct pwm_chip *chip)
  38. {
  39. return pwmchip_get_drvdata(chip);
  40. }
  41. static int iqs620_pwm_init(struct iqs620_pwm_private *iqs620_pwm,
  42. unsigned int duty_scale)
  43. {
  44. struct iqs62x_core *iqs62x = iqs620_pwm->iqs62x;
  45. int ret;
  46. if (!duty_scale)
  47. return regmap_clear_bits(iqs62x->regmap, IQS620_PWR_SETTINGS,
  48. IQS620_PWR_SETTINGS_PWM_OUT);
  49. ret = regmap_write(iqs62x->regmap, IQS620_PWM_DUTY_CYCLE,
  50. duty_scale - 1);
  51. if (ret)
  52. return ret;
  53. return regmap_set_bits(iqs62x->regmap, IQS620_PWR_SETTINGS,
  54. IQS620_PWR_SETTINGS_PWM_OUT);
  55. }
  56. static int iqs620_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
  57. const struct pwm_state *state)
  58. {
  59. struct iqs620_pwm_private *iqs620_pwm;
  60. unsigned int duty_cycle;
  61. unsigned int duty_scale;
  62. int ret;
  63. if (state->polarity != PWM_POLARITY_NORMAL)
  64. return -EINVAL;
  65. if (state->period < IQS620_PWM_PERIOD_NS)
  66. return -EINVAL;
  67. iqs620_pwm = iqs620_pwm_from_chip(chip);
  68. /*
  69. * The duty cycle generated by the device is calculated as follows:
  70. *
  71. * duty_cycle = (IQS620_PWM_DUTY_CYCLE + 1) / 256 * 1 ms
  72. *
  73. * ...where IQS620_PWM_DUTY_CYCLE is a register value between 0 and 255
  74. * (inclusive). Therefore the lowest duty cycle the device can generate
  75. * while the output is enabled is 1 / 256 ms.
  76. *
  77. * For lower duty cycles (e.g. 0), the PWM output is simply disabled to
  78. * allow an external pull-down resistor to hold the GPIO3/LTX pin low.
  79. */
  80. duty_cycle = min_t(u64, state->duty_cycle, IQS620_PWM_PERIOD_NS);
  81. duty_scale = duty_cycle * 256 / IQS620_PWM_PERIOD_NS;
  82. if (!state->enabled)
  83. duty_scale = 0;
  84. mutex_lock(&iqs620_pwm->lock);
  85. ret = iqs620_pwm_init(iqs620_pwm, duty_scale);
  86. if (!ret)
  87. iqs620_pwm->duty_scale = duty_scale;
  88. mutex_unlock(&iqs620_pwm->lock);
  89. return ret;
  90. }
  91. static int iqs620_pwm_get_state(struct pwm_chip *chip, struct pwm_device *pwm,
  92. struct pwm_state *state)
  93. {
  94. struct iqs620_pwm_private *iqs620_pwm;
  95. iqs620_pwm = iqs620_pwm_from_chip(chip);
  96. mutex_lock(&iqs620_pwm->lock);
  97. /*
  98. * Since the device cannot generate a 0% duty cycle, requests to do so
  99. * cause subsequent calls to iqs620_pwm_get_state to report the output
  100. * as disabled. This is not ideal, but is the best compromise based on
  101. * the capabilities of the device.
  102. */
  103. state->enabled = iqs620_pwm->duty_scale > 0;
  104. state->duty_cycle = DIV_ROUND_UP(iqs620_pwm->duty_scale *
  105. IQS620_PWM_PERIOD_NS, 256);
  106. mutex_unlock(&iqs620_pwm->lock);
  107. state->period = IQS620_PWM_PERIOD_NS;
  108. state->polarity = PWM_POLARITY_NORMAL;
  109. return 0;
  110. }
  111. static int iqs620_pwm_notifier(struct notifier_block *notifier,
  112. unsigned long event_flags, void *context)
  113. {
  114. struct iqs620_pwm_private *iqs620_pwm;
  115. int ret;
  116. if (!(event_flags & BIT(IQS62X_EVENT_SYS_RESET)))
  117. return NOTIFY_DONE;
  118. iqs620_pwm = container_of(notifier, struct iqs620_pwm_private,
  119. notifier);
  120. mutex_lock(&iqs620_pwm->lock);
  121. /*
  122. * The parent MFD driver already prints an error message in the event
  123. * of a device reset, so nothing else is printed here unless there is
  124. * an additional failure.
  125. */
  126. ret = iqs620_pwm_init(iqs620_pwm, iqs620_pwm->duty_scale);
  127. mutex_unlock(&iqs620_pwm->lock);
  128. if (ret) {
  129. dev_err(iqs620_pwm->dev,
  130. "Failed to re-initialize device: %d\n", ret);
  131. return NOTIFY_BAD;
  132. }
  133. return NOTIFY_OK;
  134. }
  135. static const struct pwm_ops iqs620_pwm_ops = {
  136. .apply = iqs620_pwm_apply,
  137. .get_state = iqs620_pwm_get_state,
  138. };
  139. static void iqs620_pwm_notifier_unregister(void *context)
  140. {
  141. struct iqs620_pwm_private *iqs620_pwm = context;
  142. int ret;
  143. ret = blocking_notifier_chain_unregister(&iqs620_pwm->iqs62x->nh,
  144. &iqs620_pwm->notifier);
  145. if (ret)
  146. dev_err(iqs620_pwm->dev,
  147. "Failed to unregister notifier: %d\n", ret);
  148. }
  149. static int iqs620_pwm_probe(struct platform_device *pdev)
  150. {
  151. struct iqs62x_core *iqs62x = dev_get_drvdata(pdev->dev.parent);
  152. struct pwm_chip *chip;
  153. struct iqs620_pwm_private *iqs620_pwm;
  154. unsigned int val;
  155. int ret;
  156. chip = devm_pwmchip_alloc(&pdev->dev, 1, sizeof(*iqs620_pwm));
  157. if (IS_ERR(chip))
  158. return PTR_ERR(chip);
  159. iqs620_pwm = iqs620_pwm_from_chip(chip);
  160. iqs620_pwm->dev = &pdev->dev;
  161. iqs620_pwm->iqs62x = iqs62x;
  162. ret = regmap_read(iqs62x->regmap, IQS620_PWR_SETTINGS, &val);
  163. if (ret)
  164. return ret;
  165. if (val & IQS620_PWR_SETTINGS_PWM_OUT) {
  166. ret = regmap_read(iqs62x->regmap, IQS620_PWM_DUTY_CYCLE, &val);
  167. if (ret)
  168. return ret;
  169. iqs620_pwm->duty_scale = val + 1;
  170. }
  171. chip->ops = &iqs620_pwm_ops;
  172. mutex_init(&iqs620_pwm->lock);
  173. iqs620_pwm->notifier.notifier_call = iqs620_pwm_notifier;
  174. ret = blocking_notifier_chain_register(&iqs620_pwm->iqs62x->nh,
  175. &iqs620_pwm->notifier);
  176. if (ret) {
  177. dev_err(&pdev->dev, "Failed to register notifier: %d\n", ret);
  178. return ret;
  179. }
  180. ret = devm_add_action_or_reset(&pdev->dev,
  181. iqs620_pwm_notifier_unregister,
  182. iqs620_pwm);
  183. if (ret)
  184. return ret;
  185. ret = devm_pwmchip_add(&pdev->dev, chip);
  186. if (ret)
  187. dev_err(&pdev->dev, "Failed to add device: %d\n", ret);
  188. return ret;
  189. }
  190. static struct platform_driver iqs620_pwm_platform_driver = {
  191. .driver = {
  192. .name = "iqs620a-pwm",
  193. },
  194. .probe = iqs620_pwm_probe,
  195. };
  196. module_platform_driver(iqs620_pwm_platform_driver);
  197. MODULE_AUTHOR("Jeff LaBundy <jeff@labundy.com>");
  198. MODULE_DESCRIPTION("Azoteq IQS620A PWM Generator");
  199. MODULE_LICENSE("GPL");
  200. MODULE_ALIAS("platform:iqs620a-pwm");