pwm-iqs620a.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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 pwm_chip chip;
  33. struct notifier_block notifier;
  34. struct mutex lock;
  35. bool out_en;
  36. u8 duty_val;
  37. };
  38. static int iqs620_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
  39. const struct pwm_state *state)
  40. {
  41. struct iqs620_pwm_private *iqs620_pwm;
  42. struct iqs62x_core *iqs62x;
  43. unsigned int duty_cycle;
  44. unsigned int duty_scale;
  45. int ret;
  46. if (state->polarity != PWM_POLARITY_NORMAL)
  47. return -ENOTSUPP;
  48. if (state->period < IQS620_PWM_PERIOD_NS)
  49. return -EINVAL;
  50. iqs620_pwm = container_of(chip, struct iqs620_pwm_private, chip);
  51. iqs62x = iqs620_pwm->iqs62x;
  52. /*
  53. * The duty cycle generated by the device is calculated as follows:
  54. *
  55. * duty_cycle = (IQS620_PWM_DUTY_CYCLE + 1) / 256 * 1 ms
  56. *
  57. * ...where IQS620_PWM_DUTY_CYCLE is a register value between 0 and 255
  58. * (inclusive). Therefore the lowest duty cycle the device can generate
  59. * while the output is enabled is 1 / 256 ms.
  60. *
  61. * For lower duty cycles (e.g. 0), the PWM output is simply disabled to
  62. * allow an external pull-down resistor to hold the GPIO3/LTX pin low.
  63. */
  64. duty_cycle = min_t(u64, state->duty_cycle, IQS620_PWM_PERIOD_NS);
  65. duty_scale = duty_cycle * 256 / IQS620_PWM_PERIOD_NS;
  66. mutex_lock(&iqs620_pwm->lock);
  67. if (!state->enabled || !duty_scale) {
  68. ret = regmap_update_bits(iqs62x->regmap, IQS620_PWR_SETTINGS,
  69. IQS620_PWR_SETTINGS_PWM_OUT, 0);
  70. if (ret)
  71. goto err_mutex;
  72. }
  73. if (duty_scale) {
  74. u8 duty_val = duty_scale - 1;
  75. ret = regmap_write(iqs62x->regmap, IQS620_PWM_DUTY_CYCLE,
  76. duty_val);
  77. if (ret)
  78. goto err_mutex;
  79. iqs620_pwm->duty_val = duty_val;
  80. }
  81. if (state->enabled && duty_scale) {
  82. ret = regmap_update_bits(iqs62x->regmap, IQS620_PWR_SETTINGS,
  83. IQS620_PWR_SETTINGS_PWM_OUT, 0xff);
  84. if (ret)
  85. goto err_mutex;
  86. }
  87. iqs620_pwm->out_en = state->enabled;
  88. err_mutex:
  89. mutex_unlock(&iqs620_pwm->lock);
  90. return ret;
  91. }
  92. static void iqs620_pwm_get_state(struct pwm_chip *chip, struct pwm_device *pwm,
  93. struct pwm_state *state)
  94. {
  95. struct iqs620_pwm_private *iqs620_pwm;
  96. iqs620_pwm = container_of(chip, struct iqs620_pwm_private, chip);
  97. mutex_lock(&iqs620_pwm->lock);
  98. /*
  99. * Since the device cannot generate a 0% duty cycle, requests to do so
  100. * cause subsequent calls to iqs620_pwm_get_state to report the output
  101. * as disabled with duty cycle equal to that which was in use prior to
  102. * the request. This is not ideal, but is the best compromise based on
  103. * the capabilities of the device.
  104. */
  105. state->enabled = iqs620_pwm->out_en;
  106. state->duty_cycle = DIV_ROUND_UP((iqs620_pwm->duty_val + 1) *
  107. IQS620_PWM_PERIOD_NS, 256);
  108. mutex_unlock(&iqs620_pwm->lock);
  109. state->period = IQS620_PWM_PERIOD_NS;
  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. struct iqs62x_core *iqs62x;
  116. int ret;
  117. if (!(event_flags & BIT(IQS62X_EVENT_SYS_RESET)))
  118. return NOTIFY_DONE;
  119. iqs620_pwm = container_of(notifier, struct iqs620_pwm_private,
  120. notifier);
  121. iqs62x = iqs620_pwm->iqs62x;
  122. mutex_lock(&iqs620_pwm->lock);
  123. /*
  124. * The parent MFD driver already prints an error message in the event
  125. * of a device reset, so nothing else is printed here unless there is
  126. * an additional failure.
  127. */
  128. ret = regmap_write(iqs62x->regmap, IQS620_PWM_DUTY_CYCLE,
  129. iqs620_pwm->duty_val);
  130. if (ret)
  131. goto err_mutex;
  132. ret = regmap_update_bits(iqs62x->regmap, IQS620_PWR_SETTINGS,
  133. IQS620_PWR_SETTINGS_PWM_OUT,
  134. iqs620_pwm->out_en ? 0xff : 0);
  135. err_mutex:
  136. mutex_unlock(&iqs620_pwm->lock);
  137. if (ret) {
  138. dev_err(iqs620_pwm->chip.dev,
  139. "Failed to re-initialize device: %d\n", ret);
  140. return NOTIFY_BAD;
  141. }
  142. return NOTIFY_OK;
  143. }
  144. static const struct pwm_ops iqs620_pwm_ops = {
  145. .apply = iqs620_pwm_apply,
  146. .get_state = iqs620_pwm_get_state,
  147. .owner = THIS_MODULE,
  148. };
  149. static void iqs620_pwm_notifier_unregister(void *context)
  150. {
  151. struct iqs620_pwm_private *iqs620_pwm = context;
  152. int ret;
  153. ret = blocking_notifier_chain_unregister(&iqs620_pwm->iqs62x->nh,
  154. &iqs620_pwm->notifier);
  155. if (ret)
  156. dev_err(iqs620_pwm->chip.dev,
  157. "Failed to unregister notifier: %d\n", ret);
  158. }
  159. static int iqs620_pwm_probe(struct platform_device *pdev)
  160. {
  161. struct iqs62x_core *iqs62x = dev_get_drvdata(pdev->dev.parent);
  162. struct iqs620_pwm_private *iqs620_pwm;
  163. unsigned int val;
  164. int ret;
  165. iqs620_pwm = devm_kzalloc(&pdev->dev, sizeof(*iqs620_pwm), GFP_KERNEL);
  166. if (!iqs620_pwm)
  167. return -ENOMEM;
  168. platform_set_drvdata(pdev, iqs620_pwm);
  169. iqs620_pwm->iqs62x = iqs62x;
  170. ret = regmap_read(iqs62x->regmap, IQS620_PWR_SETTINGS, &val);
  171. if (ret)
  172. return ret;
  173. iqs620_pwm->out_en = val & IQS620_PWR_SETTINGS_PWM_OUT;
  174. ret = regmap_read(iqs62x->regmap, IQS620_PWM_DUTY_CYCLE, &val);
  175. if (ret)
  176. return ret;
  177. iqs620_pwm->duty_val = val;
  178. iqs620_pwm->chip.dev = &pdev->dev;
  179. iqs620_pwm->chip.ops = &iqs620_pwm_ops;
  180. iqs620_pwm->chip.base = -1;
  181. iqs620_pwm->chip.npwm = 1;
  182. mutex_init(&iqs620_pwm->lock);
  183. iqs620_pwm->notifier.notifier_call = iqs620_pwm_notifier;
  184. ret = blocking_notifier_chain_register(&iqs620_pwm->iqs62x->nh,
  185. &iqs620_pwm->notifier);
  186. if (ret) {
  187. dev_err(&pdev->dev, "Failed to register notifier: %d\n", ret);
  188. return ret;
  189. }
  190. ret = devm_add_action_or_reset(&pdev->dev,
  191. iqs620_pwm_notifier_unregister,
  192. iqs620_pwm);
  193. if (ret)
  194. return ret;
  195. ret = pwmchip_add(&iqs620_pwm->chip);
  196. if (ret)
  197. dev_err(&pdev->dev, "Failed to add device: %d\n", ret);
  198. return ret;
  199. }
  200. static int iqs620_pwm_remove(struct platform_device *pdev)
  201. {
  202. struct iqs620_pwm_private *iqs620_pwm = platform_get_drvdata(pdev);
  203. int ret;
  204. ret = pwmchip_remove(&iqs620_pwm->chip);
  205. if (ret)
  206. dev_err(&pdev->dev, "Failed to remove device: %d\n", ret);
  207. return ret;
  208. }
  209. static struct platform_driver iqs620_pwm_platform_driver = {
  210. .driver = {
  211. .name = "iqs620a-pwm",
  212. },
  213. .probe = iqs620_pwm_probe,
  214. .remove = iqs620_pwm_remove,
  215. };
  216. module_platform_driver(iqs620_pwm_platform_driver);
  217. MODULE_AUTHOR("Jeff LaBundy <jeff@labundy.com>");
  218. MODULE_DESCRIPTION("Azoteq IQS620A PWM Generator");
  219. MODULE_LICENSE("GPL");
  220. MODULE_ALIAS("platform:iqs620a-pwm");