pwm-lp3943.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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. return ERR_PTR(-EBUSY);
  48. }
  49. return pwm_map;
  50. }
  51. static int lp3943_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm)
  52. {
  53. struct lp3943_pwm *lp3943_pwm = to_lp3943_pwm(chip);
  54. struct lp3943_pwm_map *pwm_map;
  55. pwm_map = lp3943_pwm_request_map(lp3943_pwm, pwm->hwpwm);
  56. if (IS_ERR(pwm_map))
  57. return PTR_ERR(pwm_map);
  58. return pwm_set_chip_data(pwm, pwm_map);
  59. }
  60. static void lp3943_pwm_free_map(struct lp3943_pwm *lp3943_pwm,
  61. struct lp3943_pwm_map *pwm_map)
  62. {
  63. struct lp3943 *lp3943 = lp3943_pwm->lp3943;
  64. int i, offset;
  65. for (i = 0; i < pwm_map->num_outputs; i++) {
  66. offset = pwm_map->output[i];
  67. clear_bit(offset, &lp3943->pin_used);
  68. }
  69. kfree(pwm_map);
  70. }
  71. static void lp3943_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm)
  72. {
  73. struct lp3943_pwm *lp3943_pwm = to_lp3943_pwm(chip);
  74. struct lp3943_pwm_map *pwm_map = pwm_get_chip_data(pwm);
  75. lp3943_pwm_free_map(lp3943_pwm, pwm_map);
  76. }
  77. static int lp3943_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
  78. int duty_ns, int period_ns)
  79. {
  80. struct lp3943_pwm *lp3943_pwm = to_lp3943_pwm(chip);
  81. struct lp3943 *lp3943 = lp3943_pwm->lp3943;
  82. u8 val, reg_duty, reg_prescale;
  83. int err;
  84. /*
  85. * How to configure the LP3943 PWMs
  86. *
  87. * 1) Period = 6250 ~ 1600000
  88. * 2) Prescale = period / 6250 -1
  89. * 3) Duty = input duty
  90. *
  91. * Prescale and duty are register values
  92. */
  93. if (pwm->hwpwm == 0) {
  94. reg_prescale = LP3943_REG_PRESCALE0;
  95. reg_duty = LP3943_REG_PWM0;
  96. } else {
  97. reg_prescale = LP3943_REG_PRESCALE1;
  98. reg_duty = LP3943_REG_PWM1;
  99. }
  100. period_ns = clamp(period_ns, LP3943_MIN_PERIOD, LP3943_MAX_PERIOD);
  101. val = (u8)(period_ns / LP3943_MIN_PERIOD - 1);
  102. err = lp3943_write_byte(lp3943, reg_prescale, val);
  103. if (err)
  104. return err;
  105. val = (u8)(duty_ns * LP3943_MAX_DUTY / period_ns);
  106. return lp3943_write_byte(lp3943, reg_duty, val);
  107. }
  108. static int lp3943_pwm_set_mode(struct lp3943_pwm *lp3943_pwm,
  109. struct lp3943_pwm_map *pwm_map,
  110. u8 val)
  111. {
  112. struct lp3943 *lp3943 = lp3943_pwm->lp3943;
  113. const struct lp3943_reg_cfg *mux = lp3943->mux_cfg;
  114. int i, index, err;
  115. for (i = 0; i < pwm_map->num_outputs; i++) {
  116. index = pwm_map->output[i];
  117. err = lp3943_update_bits(lp3943, mux[index].reg,
  118. mux[index].mask,
  119. val << mux[index].shift);
  120. if (err)
  121. return err;
  122. }
  123. return 0;
  124. }
  125. static int lp3943_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
  126. {
  127. struct lp3943_pwm *lp3943_pwm = to_lp3943_pwm(chip);
  128. struct lp3943_pwm_map *pwm_map = pwm_get_chip_data(pwm);
  129. u8 val;
  130. if (pwm->hwpwm == 0)
  131. val = LP3943_DIM_PWM0;
  132. else
  133. val = LP3943_DIM_PWM1;
  134. /*
  135. * Each PWM generator is set to control any of outputs of LP3943.
  136. * To enable/disable the PWM, these output pins should be configured.
  137. */
  138. return lp3943_pwm_set_mode(lp3943_pwm, pwm_map, val);
  139. }
  140. static void lp3943_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
  141. {
  142. struct lp3943_pwm *lp3943_pwm = to_lp3943_pwm(chip);
  143. struct lp3943_pwm_map *pwm_map = pwm_get_chip_data(pwm);
  144. /*
  145. * LP3943 outputs are open-drain, so the pin should be configured
  146. * when the PWM is disabled.
  147. */
  148. lp3943_pwm_set_mode(lp3943_pwm, pwm_map, LP3943_GPIO_OUT_HIGH);
  149. }
  150. static const struct pwm_ops lp3943_pwm_ops = {
  151. .request = lp3943_pwm_request,
  152. .free = lp3943_pwm_free,
  153. .config = lp3943_pwm_config,
  154. .enable = lp3943_pwm_enable,
  155. .disable = lp3943_pwm_disable,
  156. .owner = THIS_MODULE,
  157. };
  158. static int lp3943_pwm_parse_dt(struct device *dev,
  159. struct lp3943_pwm *lp3943_pwm)
  160. {
  161. static const char * const name[] = { "ti,pwm0", "ti,pwm1", };
  162. struct device_node *node = dev->of_node;
  163. struct lp3943_platform_data *pdata;
  164. struct lp3943_pwm_map *pwm_map;
  165. enum lp3943_pwm_output *output;
  166. int i, err, proplen, count = 0;
  167. u32 num_outputs;
  168. if (!node)
  169. return -EINVAL;
  170. pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
  171. if (!pdata)
  172. return -ENOMEM;
  173. /*
  174. * Read the output map configuration from the device tree.
  175. * Each of the two PWM generators can drive zero or more outputs.
  176. */
  177. for (i = 0; i < LP3943_NUM_PWMS; i++) {
  178. if (!of_get_property(node, name[i], &proplen))
  179. continue;
  180. num_outputs = proplen / sizeof(u32);
  181. if (num_outputs == 0)
  182. continue;
  183. output = devm_kzalloc(dev, sizeof(*output) * num_outputs,
  184. GFP_KERNEL);
  185. if (!output)
  186. return -ENOMEM;
  187. err = of_property_read_u32_array(node, name[i], output,
  188. num_outputs);
  189. if (err)
  190. return err;
  191. pwm_map = devm_kzalloc(dev, sizeof(*pwm_map), GFP_KERNEL);
  192. if (!pwm_map)
  193. return -ENOMEM;
  194. pwm_map->output = output;
  195. pwm_map->num_outputs = num_outputs;
  196. pdata->pwms[i] = pwm_map;
  197. count++;
  198. }
  199. if (count == 0)
  200. return -ENODATA;
  201. lp3943_pwm->pdata = pdata;
  202. return 0;
  203. }
  204. static int lp3943_pwm_probe(struct platform_device *pdev)
  205. {
  206. struct lp3943 *lp3943 = dev_get_drvdata(pdev->dev.parent);
  207. struct lp3943_pwm *lp3943_pwm;
  208. int ret;
  209. lp3943_pwm = devm_kzalloc(&pdev->dev, sizeof(*lp3943_pwm), GFP_KERNEL);
  210. if (!lp3943_pwm)
  211. return -ENOMEM;
  212. lp3943_pwm->pdata = lp3943->pdata;
  213. if (!lp3943_pwm->pdata) {
  214. if (IS_ENABLED(CONFIG_OF))
  215. ret = lp3943_pwm_parse_dt(&pdev->dev, lp3943_pwm);
  216. else
  217. ret = -ENODEV;
  218. if (ret)
  219. return ret;
  220. }
  221. lp3943_pwm->lp3943 = lp3943;
  222. lp3943_pwm->chip.dev = &pdev->dev;
  223. lp3943_pwm->chip.ops = &lp3943_pwm_ops;
  224. lp3943_pwm->chip.npwm = LP3943_NUM_PWMS;
  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. .owner = THIS_MODULE,
  246. .of_match_table = of_match_ptr(lp3943_pwm_of_match),
  247. },
  248. };
  249. module_platform_driver(lp3943_pwm_driver);
  250. MODULE_DESCRIPTION("LP3943 PWM driver");
  251. MODULE_ALIAS("platform:lp3943-pwm");
  252. MODULE_AUTHOR("Milo Kim");
  253. MODULE_LICENSE("GPL");