pwm-lp3943.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * TI/National Semiconductor LP3943 PWM driver
  4. *
  5. * Copyright 2013 Texas Instruments
  6. *
  7. * Author: Milo Kim <milo.kim@ti.com>
  8. */
  9. #include <linux/err.h>
  10. #include <linux/mfd/lp3943.h>
  11. #include <linux/module.h>
  12. #include <linux/of.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/pwm.h>
  15. #include <linux/slab.h>
  16. #define LP3943_MAX_DUTY 255
  17. #define LP3943_MIN_PERIOD 6250
  18. #define LP3943_MAX_PERIOD 1600000
  19. struct lp3943_pwm {
  20. struct lp3943 *lp3943;
  21. struct lp3943_platform_data *pdata;
  22. struct lp3943_pwm_map pwm_map[LP3943_NUM_PWMS];
  23. };
  24. static inline struct lp3943_pwm *to_lp3943_pwm(struct pwm_chip *chip)
  25. {
  26. return pwmchip_get_drvdata(chip);
  27. }
  28. static struct lp3943_pwm_map *
  29. lp3943_pwm_request_map(struct lp3943_pwm *lp3943_pwm, int hwpwm)
  30. {
  31. struct lp3943_platform_data *pdata = lp3943_pwm->pdata;
  32. struct lp3943 *lp3943 = lp3943_pwm->lp3943;
  33. struct lp3943_pwm_map *pwm_map = &lp3943_pwm->pwm_map[hwpwm];
  34. int i, offset;
  35. pwm_map->output = pdata->pwms[hwpwm]->output;
  36. pwm_map->num_outputs = pdata->pwms[hwpwm]->num_outputs;
  37. for (i = 0; i < pwm_map->num_outputs; i++) {
  38. offset = pwm_map->output[i];
  39. /* Return an error if the pin is already assigned */
  40. if (test_and_set_bit(offset, &lp3943->pin_used))
  41. return ERR_PTR(-EBUSY);
  42. }
  43. return pwm_map;
  44. }
  45. static int lp3943_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm)
  46. {
  47. struct lp3943_pwm *lp3943_pwm = to_lp3943_pwm(chip);
  48. struct lp3943_pwm_map *pwm_map;
  49. pwm_map = lp3943_pwm_request_map(lp3943_pwm, pwm->hwpwm);
  50. if (IS_ERR(pwm_map))
  51. return PTR_ERR(pwm_map);
  52. return 0;
  53. }
  54. static void lp3943_pwm_free_map(struct lp3943_pwm *lp3943_pwm,
  55. struct lp3943_pwm_map *pwm_map)
  56. {
  57. struct lp3943 *lp3943 = lp3943_pwm->lp3943;
  58. int i, offset;
  59. for (i = 0; i < pwm_map->num_outputs; i++) {
  60. offset = pwm_map->output[i];
  61. clear_bit(offset, &lp3943->pin_used);
  62. }
  63. }
  64. static void lp3943_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm)
  65. {
  66. struct lp3943_pwm *lp3943_pwm = to_lp3943_pwm(chip);
  67. struct lp3943_pwm_map *pwm_map = &lp3943_pwm->pwm_map[pwm->hwpwm];
  68. lp3943_pwm_free_map(lp3943_pwm, pwm_map);
  69. }
  70. static int lp3943_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
  71. u64 duty_ns, u64 period_ns)
  72. {
  73. struct lp3943_pwm *lp3943_pwm = to_lp3943_pwm(chip);
  74. struct lp3943 *lp3943 = lp3943_pwm->lp3943;
  75. u8 val, reg_duty, reg_prescale;
  76. int err;
  77. /*
  78. * How to configure the LP3943 PWMs
  79. *
  80. * 1) Period = 6250 ~ 1600000
  81. * 2) Prescale = period / 6250 -1
  82. * 3) Duty = input duty
  83. *
  84. * Prescale and duty are register values
  85. */
  86. if (pwm->hwpwm == 0) {
  87. reg_prescale = LP3943_REG_PRESCALE0;
  88. reg_duty = LP3943_REG_PWM0;
  89. } else {
  90. reg_prescale = LP3943_REG_PRESCALE1;
  91. reg_duty = LP3943_REG_PWM1;
  92. }
  93. /*
  94. * Note that after this clamping, period_ns fits into an int. This is
  95. * helpful because we can resort to integer division below instead of
  96. * the (more expensive) 64 bit division.
  97. */
  98. period_ns = clamp(period_ns, (u64)LP3943_MIN_PERIOD, (u64)LP3943_MAX_PERIOD);
  99. val = (u8)((int)period_ns / LP3943_MIN_PERIOD - 1);
  100. err = lp3943_write_byte(lp3943, reg_prescale, val);
  101. if (err)
  102. return err;
  103. duty_ns = min(duty_ns, period_ns);
  104. val = (u8)((int)duty_ns * LP3943_MAX_DUTY / (int)period_ns);
  105. return lp3943_write_byte(lp3943, reg_duty, val);
  106. }
  107. static int lp3943_pwm_set_mode(struct lp3943_pwm *lp3943_pwm,
  108. struct lp3943_pwm_map *pwm_map,
  109. u8 val)
  110. {
  111. struct lp3943 *lp3943 = lp3943_pwm->lp3943;
  112. const struct lp3943_reg_cfg *mux = lp3943->mux_cfg;
  113. int i, index, err;
  114. for (i = 0; i < pwm_map->num_outputs; i++) {
  115. index = pwm_map->output[i];
  116. err = lp3943_update_bits(lp3943, mux[index].reg,
  117. mux[index].mask,
  118. val << mux[index].shift);
  119. if (err)
  120. return err;
  121. }
  122. return 0;
  123. }
  124. static int lp3943_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
  125. {
  126. struct lp3943_pwm *lp3943_pwm = to_lp3943_pwm(chip);
  127. struct lp3943_pwm_map *pwm_map = &lp3943_pwm->pwm_map[pwm->hwpwm];
  128. u8 val;
  129. if (pwm->hwpwm == 0)
  130. val = LP3943_DIM_PWM0;
  131. else
  132. val = LP3943_DIM_PWM1;
  133. /*
  134. * Each PWM generator is set to control any of outputs of LP3943.
  135. * To enable/disable the PWM, these output pins should be configured.
  136. */
  137. return lp3943_pwm_set_mode(lp3943_pwm, pwm_map, val);
  138. }
  139. static void lp3943_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
  140. {
  141. struct lp3943_pwm *lp3943_pwm = to_lp3943_pwm(chip);
  142. struct lp3943_pwm_map *pwm_map = &lp3943_pwm->pwm_map[pwm->hwpwm];
  143. /*
  144. * LP3943 outputs are open-drain, so the pin should be configured
  145. * when the PWM is disabled.
  146. */
  147. lp3943_pwm_set_mode(lp3943_pwm, pwm_map, LP3943_GPIO_OUT_HIGH);
  148. }
  149. static int lp3943_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
  150. const struct pwm_state *state)
  151. {
  152. int err;
  153. if (state->polarity != PWM_POLARITY_NORMAL)
  154. return -EINVAL;
  155. if (!state->enabled) {
  156. if (pwm->state.enabled)
  157. lp3943_pwm_disable(chip, pwm);
  158. return 0;
  159. }
  160. err = lp3943_pwm_config(chip, pwm, state->duty_cycle, state->period);
  161. if (err)
  162. return err;
  163. if (!pwm->state.enabled)
  164. err = lp3943_pwm_enable(chip, pwm);
  165. return err;
  166. }
  167. static const struct pwm_ops lp3943_pwm_ops = {
  168. .request = lp3943_pwm_request,
  169. .free = lp3943_pwm_free,
  170. .apply = lp3943_pwm_apply,
  171. };
  172. static int lp3943_pwm_parse_dt(struct device *dev,
  173. struct lp3943_pwm *lp3943_pwm)
  174. {
  175. static const char * const name[] = { "ti,pwm0", "ti,pwm1", };
  176. struct device_node *node = dev->of_node;
  177. struct lp3943_platform_data *pdata;
  178. struct lp3943_pwm_map *pwm_map;
  179. enum lp3943_pwm_output *output;
  180. int i, err, num_outputs, count = 0;
  181. if (!node)
  182. return -EINVAL;
  183. pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
  184. if (!pdata)
  185. return -ENOMEM;
  186. /*
  187. * Read the output map configuration from the device tree.
  188. * Each of the two PWM generators can drive zero or more outputs.
  189. */
  190. for (i = 0; i < LP3943_NUM_PWMS; i++) {
  191. num_outputs = of_property_count_u32_elems(node, name[i]);
  192. if (num_outputs <= 0)
  193. continue;
  194. output = devm_kcalloc(dev, num_outputs, sizeof(*output),
  195. GFP_KERNEL);
  196. if (!output)
  197. return -ENOMEM;
  198. err = of_property_read_u32_array(node, name[i], output,
  199. num_outputs);
  200. if (err)
  201. return err;
  202. pwm_map = devm_kzalloc(dev, sizeof(*pwm_map), GFP_KERNEL);
  203. if (!pwm_map)
  204. return -ENOMEM;
  205. pwm_map->output = output;
  206. pwm_map->num_outputs = num_outputs;
  207. pdata->pwms[i] = pwm_map;
  208. count++;
  209. }
  210. if (count == 0)
  211. return -ENODATA;
  212. lp3943_pwm->pdata = pdata;
  213. return 0;
  214. }
  215. static int lp3943_pwm_probe(struct platform_device *pdev)
  216. {
  217. struct lp3943 *lp3943 = dev_get_drvdata(pdev->dev.parent);
  218. struct pwm_chip *chip;
  219. struct lp3943_pwm *lp3943_pwm;
  220. int ret;
  221. chip = devm_pwmchip_alloc(&pdev->dev, LP3943_NUM_PWMS, sizeof(*lp3943_pwm));
  222. if (IS_ERR(chip))
  223. return PTR_ERR(chip);
  224. lp3943_pwm = to_lp3943_pwm(chip);
  225. lp3943_pwm->pdata = lp3943->pdata;
  226. if (!lp3943_pwm->pdata) {
  227. if (IS_ENABLED(CONFIG_OF))
  228. ret = lp3943_pwm_parse_dt(&pdev->dev, lp3943_pwm);
  229. else
  230. ret = -ENODEV;
  231. if (ret)
  232. return ret;
  233. }
  234. lp3943_pwm->lp3943 = lp3943;
  235. chip->ops = &lp3943_pwm_ops;
  236. return devm_pwmchip_add(&pdev->dev, chip);
  237. }
  238. #ifdef CONFIG_OF
  239. static const struct of_device_id lp3943_pwm_of_match[] = {
  240. { .compatible = "ti,lp3943-pwm", },
  241. { }
  242. };
  243. MODULE_DEVICE_TABLE(of, lp3943_pwm_of_match);
  244. #endif
  245. static struct platform_driver lp3943_pwm_driver = {
  246. .probe = lp3943_pwm_probe,
  247. .driver = {
  248. .name = "lp3943-pwm",
  249. .of_match_table = of_match_ptr(lp3943_pwm_of_match),
  250. },
  251. };
  252. module_platform_driver(lp3943_pwm_driver);
  253. MODULE_DESCRIPTION("LP3943 PWM driver");
  254. MODULE_ALIAS("platform:lp3943-pwm");
  255. MODULE_AUTHOR("Milo Kim");
  256. MODULE_LICENSE("GPL");