pwm-lp3943.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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 <[email protected]>
  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. u64 duty_ns, u64 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. /*
  100. * Note that after this clamping, period_ns fits into an int. This is
  101. * helpful because we can resort to integer division below instead of
  102. * the (more expensive) 64 bit division.
  103. */
  104. period_ns = clamp(period_ns, (u64)LP3943_MIN_PERIOD, (u64)LP3943_MAX_PERIOD);
  105. val = (u8)((int)period_ns / LP3943_MIN_PERIOD - 1);
  106. err = lp3943_write_byte(lp3943, reg_prescale, val);
  107. if (err)
  108. return err;
  109. duty_ns = min(duty_ns, period_ns);
  110. val = (u8)((int)duty_ns * LP3943_MAX_DUTY / (int)period_ns);
  111. return lp3943_write_byte(lp3943, reg_duty, val);
  112. }
  113. static int lp3943_pwm_set_mode(struct lp3943_pwm *lp3943_pwm,
  114. struct lp3943_pwm_map *pwm_map,
  115. u8 val)
  116. {
  117. struct lp3943 *lp3943 = lp3943_pwm->lp3943;
  118. const struct lp3943_reg_cfg *mux = lp3943->mux_cfg;
  119. int i, index, err;
  120. for (i = 0; i < pwm_map->num_outputs; i++) {
  121. index = pwm_map->output[i];
  122. err = lp3943_update_bits(lp3943, mux[index].reg,
  123. mux[index].mask,
  124. val << mux[index].shift);
  125. if (err)
  126. return err;
  127. }
  128. return 0;
  129. }
  130. static int lp3943_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
  131. {
  132. struct lp3943_pwm *lp3943_pwm = to_lp3943_pwm(chip);
  133. struct lp3943_pwm_map *pwm_map = pwm_get_chip_data(pwm);
  134. u8 val;
  135. if (pwm->hwpwm == 0)
  136. val = LP3943_DIM_PWM0;
  137. else
  138. val = LP3943_DIM_PWM1;
  139. /*
  140. * Each PWM generator is set to control any of outputs of LP3943.
  141. * To enable/disable the PWM, these output pins should be configured.
  142. */
  143. return lp3943_pwm_set_mode(lp3943_pwm, pwm_map, val);
  144. }
  145. static void lp3943_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
  146. {
  147. struct lp3943_pwm *lp3943_pwm = to_lp3943_pwm(chip);
  148. struct lp3943_pwm_map *pwm_map = pwm_get_chip_data(pwm);
  149. /*
  150. * LP3943 outputs are open-drain, so the pin should be configured
  151. * when the PWM is disabled.
  152. */
  153. lp3943_pwm_set_mode(lp3943_pwm, pwm_map, LP3943_GPIO_OUT_HIGH);
  154. }
  155. static int lp3943_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
  156. const struct pwm_state *state)
  157. {
  158. int err;
  159. if (state->polarity != PWM_POLARITY_NORMAL)
  160. return -EINVAL;
  161. if (!state->enabled) {
  162. if (pwm->state.enabled)
  163. lp3943_pwm_disable(chip, pwm);
  164. return 0;
  165. }
  166. err = lp3943_pwm_config(chip, pwm, state->duty_cycle, state->period);
  167. if (err)
  168. return err;
  169. if (!pwm->state.enabled)
  170. err = lp3943_pwm_enable(chip, pwm);
  171. return err;
  172. }
  173. static const struct pwm_ops lp3943_pwm_ops = {
  174. .request = lp3943_pwm_request,
  175. .free = lp3943_pwm_free,
  176. .apply = lp3943_pwm_apply,
  177. .owner = THIS_MODULE,
  178. };
  179. static int lp3943_pwm_parse_dt(struct device *dev,
  180. struct lp3943_pwm *lp3943_pwm)
  181. {
  182. static const char * const name[] = { "ti,pwm0", "ti,pwm1", };
  183. struct device_node *node = dev->of_node;
  184. struct lp3943_platform_data *pdata;
  185. struct lp3943_pwm_map *pwm_map;
  186. enum lp3943_pwm_output *output;
  187. int i, err, proplen, count = 0;
  188. u32 num_outputs;
  189. if (!node)
  190. return -EINVAL;
  191. pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
  192. if (!pdata)
  193. return -ENOMEM;
  194. /*
  195. * Read the output map configuration from the device tree.
  196. * Each of the two PWM generators can drive zero or more outputs.
  197. */
  198. for (i = 0; i < LP3943_NUM_PWMS; i++) {
  199. if (!of_get_property(node, name[i], &proplen))
  200. continue;
  201. num_outputs = proplen / sizeof(u32);
  202. if (num_outputs == 0)
  203. continue;
  204. output = devm_kcalloc(dev, num_outputs, sizeof(*output),
  205. GFP_KERNEL);
  206. if (!output)
  207. return -ENOMEM;
  208. err = of_property_read_u32_array(node, name[i], output,
  209. num_outputs);
  210. if (err)
  211. return err;
  212. pwm_map = devm_kzalloc(dev, sizeof(*pwm_map), GFP_KERNEL);
  213. if (!pwm_map)
  214. return -ENOMEM;
  215. pwm_map->output = output;
  216. pwm_map->num_outputs = num_outputs;
  217. pdata->pwms[i] = pwm_map;
  218. count++;
  219. }
  220. if (count == 0)
  221. return -ENODATA;
  222. lp3943_pwm->pdata = pdata;
  223. return 0;
  224. }
  225. static int lp3943_pwm_probe(struct platform_device *pdev)
  226. {
  227. struct lp3943 *lp3943 = dev_get_drvdata(pdev->dev.parent);
  228. struct lp3943_pwm *lp3943_pwm;
  229. int ret;
  230. lp3943_pwm = devm_kzalloc(&pdev->dev, sizeof(*lp3943_pwm), GFP_KERNEL);
  231. if (!lp3943_pwm)
  232. return -ENOMEM;
  233. lp3943_pwm->pdata = lp3943->pdata;
  234. if (!lp3943_pwm->pdata) {
  235. if (IS_ENABLED(CONFIG_OF))
  236. ret = lp3943_pwm_parse_dt(&pdev->dev, lp3943_pwm);
  237. else
  238. ret = -ENODEV;
  239. if (ret)
  240. return ret;
  241. }
  242. lp3943_pwm->lp3943 = lp3943;
  243. lp3943_pwm->chip.dev = &pdev->dev;
  244. lp3943_pwm->chip.ops = &lp3943_pwm_ops;
  245. lp3943_pwm->chip.npwm = LP3943_NUM_PWMS;
  246. return devm_pwmchip_add(&pdev->dev, &lp3943_pwm->chip);
  247. }
  248. #ifdef CONFIG_OF
  249. static const struct of_device_id lp3943_pwm_of_match[] = {
  250. { .compatible = "ti,lp3943-pwm", },
  251. { }
  252. };
  253. MODULE_DEVICE_TABLE(of, lp3943_pwm_of_match);
  254. #endif
  255. static struct platform_driver lp3943_pwm_driver = {
  256. .probe = lp3943_pwm_probe,
  257. .driver = {
  258. .name = "lp3943-pwm",
  259. .of_match_table = of_match_ptr(lp3943_pwm_of_match),
  260. },
  261. };
  262. module_platform_driver(lp3943_pwm_driver);
  263. MODULE_DESCRIPTION("LP3943 PWM driver");
  264. MODULE_ALIAS("platform:lp3943-pwm");
  265. MODULE_AUTHOR("Milo Kim");
  266. MODULE_LICENSE("GPL");