pwm-lp3943.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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/i2c.h>
  11. #include <linux/mfd/lp3943.h>
  12. #include <linux/module.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 pwm_chip chip;
  21. struct lp3943 *lp3943;
  22. struct lp3943_platform_data *pdata;
  23. };
  24. static inline struct lp3943_pwm *to_lp3943_pwm(struct pwm_chip *_chip)
  25. {
  26. return container_of(_chip, struct lp3943_pwm, 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;
  34. int i, offset;
  35. pwm_map = kzalloc(sizeof(*pwm_map), GFP_KERNEL);
  36. if (!pwm_map)
  37. return ERR_PTR(-ENOMEM);
  38. pwm_map->output = pdata->pwms[hwpwm]->output;
  39. pwm_map->num_outputs = pdata->pwms[hwpwm]->num_outputs;
  40. for (i = 0; i < pwm_map->num_outputs; i++) {
  41. offset = pwm_map->output[i];
  42. /* Return an error if the pin is already assigned */
  43. if (test_and_set_bit(offset, &lp3943->pin_used)) {
  44. kfree(pwm_map);
  45. return ERR_PTR(-EBUSY);
  46. }
  47. }
  48. return pwm_map;
  49. }
  50. static int lp3943_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm)
  51. {
  52. struct lp3943_pwm *lp3943_pwm = to_lp3943_pwm(chip);
  53. struct lp3943_pwm_map *pwm_map;
  54. pwm_map = lp3943_pwm_request_map(lp3943_pwm, pwm->hwpwm);
  55. if (IS_ERR(pwm_map))
  56. return PTR_ERR(pwm_map);
  57. return pwm_set_chip_data(pwm, pwm_map);
  58. }
  59. static void lp3943_pwm_free_map(struct lp3943_pwm *lp3943_pwm,
  60. struct lp3943_pwm_map *pwm_map)
  61. {
  62. struct lp3943 *lp3943 = lp3943_pwm->lp3943;
  63. int i, offset;
  64. for (i = 0; i < pwm_map->num_outputs; i++) {
  65. offset = pwm_map->output[i];
  66. clear_bit(offset, &lp3943->pin_used);
  67. }
  68. kfree(pwm_map);
  69. }
  70. static void lp3943_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm)
  71. {
  72. struct lp3943_pwm *lp3943_pwm = to_lp3943_pwm(chip);
  73. struct lp3943_pwm_map *pwm_map = pwm_get_chip_data(pwm);
  74. lp3943_pwm_free_map(lp3943_pwm, pwm_map);
  75. }
  76. static int lp3943_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
  77. int duty_ns, int period_ns)
  78. {
  79. struct lp3943_pwm *lp3943_pwm = to_lp3943_pwm(chip);
  80. struct lp3943 *lp3943 = lp3943_pwm->lp3943;
  81. u8 val, reg_duty, reg_prescale;
  82. int err;
  83. /*
  84. * How to configure the LP3943 PWMs
  85. *
  86. * 1) Period = 6250 ~ 1600000
  87. * 2) Prescale = period / 6250 -1
  88. * 3) Duty = input duty
  89. *
  90. * Prescale and duty are register values
  91. */
  92. if (pwm->hwpwm == 0) {
  93. reg_prescale = LP3943_REG_PRESCALE0;
  94. reg_duty = LP3943_REG_PWM0;
  95. } else {
  96. reg_prescale = LP3943_REG_PRESCALE1;
  97. reg_duty = LP3943_REG_PWM1;
  98. }
  99. period_ns = clamp(period_ns, LP3943_MIN_PERIOD, LP3943_MAX_PERIOD);
  100. val = (u8)(period_ns / LP3943_MIN_PERIOD - 1);
  101. err = lp3943_write_byte(lp3943, reg_prescale, val);
  102. if (err)
  103. return err;
  104. val = (u8)(duty_ns * LP3943_MAX_DUTY / 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 = pwm_get_chip_data(pwm);
  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 = pwm_get_chip_data(pwm);
  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 const struct pwm_ops lp3943_pwm_ops = {
  150. .request = lp3943_pwm_request,
  151. .free = lp3943_pwm_free,
  152. .config = lp3943_pwm_config,
  153. .enable = lp3943_pwm_enable,
  154. .disable = lp3943_pwm_disable,
  155. .owner = THIS_MODULE,
  156. };
  157. static int lp3943_pwm_parse_dt(struct device *dev,
  158. struct lp3943_pwm *lp3943_pwm)
  159. {
  160. static const char * const name[] = { "ti,pwm0", "ti,pwm1", };
  161. struct device_node *node = dev->of_node;
  162. struct lp3943_platform_data *pdata;
  163. struct lp3943_pwm_map *pwm_map;
  164. enum lp3943_pwm_output *output;
  165. int i, err, proplen, count = 0;
  166. u32 num_outputs;
  167. if (!node)
  168. return -EINVAL;
  169. pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
  170. if (!pdata)
  171. return -ENOMEM;
  172. /*
  173. * Read the output map configuration from the device tree.
  174. * Each of the two PWM generators can drive zero or more outputs.
  175. */
  176. for (i = 0; i < LP3943_NUM_PWMS; i++) {
  177. if (!of_get_property(node, name[i], &proplen))
  178. continue;
  179. num_outputs = proplen / sizeof(u32);
  180. if (num_outputs == 0)
  181. continue;
  182. output = devm_kcalloc(dev, num_outputs, sizeof(*output),
  183. GFP_KERNEL);
  184. if (!output)
  185. return -ENOMEM;
  186. err = of_property_read_u32_array(node, name[i], output,
  187. num_outputs);
  188. if (err)
  189. return err;
  190. pwm_map = devm_kzalloc(dev, sizeof(*pwm_map), GFP_KERNEL);
  191. if (!pwm_map)
  192. return -ENOMEM;
  193. pwm_map->output = output;
  194. pwm_map->num_outputs = num_outputs;
  195. pdata->pwms[i] = pwm_map;
  196. count++;
  197. }
  198. if (count == 0)
  199. return -ENODATA;
  200. lp3943_pwm->pdata = pdata;
  201. return 0;
  202. }
  203. static int lp3943_pwm_probe(struct platform_device *pdev)
  204. {
  205. struct lp3943 *lp3943 = dev_get_drvdata(pdev->dev.parent);
  206. struct lp3943_pwm *lp3943_pwm;
  207. int ret;
  208. lp3943_pwm = devm_kzalloc(&pdev->dev, sizeof(*lp3943_pwm), GFP_KERNEL);
  209. if (!lp3943_pwm)
  210. return -ENOMEM;
  211. lp3943_pwm->pdata = lp3943->pdata;
  212. if (!lp3943_pwm->pdata) {
  213. if (IS_ENABLED(CONFIG_OF))
  214. ret = lp3943_pwm_parse_dt(&pdev->dev, lp3943_pwm);
  215. else
  216. ret = -ENODEV;
  217. if (ret)
  218. return ret;
  219. }
  220. lp3943_pwm->lp3943 = lp3943;
  221. lp3943_pwm->chip.dev = &pdev->dev;
  222. lp3943_pwm->chip.ops = &lp3943_pwm_ops;
  223. lp3943_pwm->chip.npwm = LP3943_NUM_PWMS;
  224. lp3943_pwm->chip.base = -1;
  225. platform_set_drvdata(pdev, lp3943_pwm);
  226. return pwmchip_add(&lp3943_pwm->chip);
  227. }
  228. static int lp3943_pwm_remove(struct platform_device *pdev)
  229. {
  230. struct lp3943_pwm *lp3943_pwm = platform_get_drvdata(pdev);
  231. return pwmchip_remove(&lp3943_pwm->chip);
  232. }
  233. #ifdef CONFIG_OF
  234. static const struct of_device_id lp3943_pwm_of_match[] = {
  235. { .compatible = "ti,lp3943-pwm", },
  236. { }
  237. };
  238. MODULE_DEVICE_TABLE(of, lp3943_pwm_of_match);
  239. #endif
  240. static struct platform_driver lp3943_pwm_driver = {
  241. .probe = lp3943_pwm_probe,
  242. .remove = lp3943_pwm_remove,
  243. .driver = {
  244. .name = "lp3943-pwm",
  245. .of_match_table = of_match_ptr(lp3943_pwm_of_match),
  246. },
  247. };
  248. module_platform_driver(lp3943_pwm_driver);
  249. MODULE_DESCRIPTION("LP3943 PWM driver");
  250. MODULE_ALIAS("platform:lp3943-pwm");
  251. MODULE_AUTHOR("Milo Kim");
  252. MODULE_LICENSE("GPL");