pwm-lp3943.c 7.5 KB

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