pwm-omap-dmtimer.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. /*
  2. * Copyright (c) 2015 Neil Armstrong <narmstrong@baylibre.com>
  3. * Copyright (c) 2014 Joachim Eastwood <manabian@gmail.com>
  4. * Copyright (c) 2012 NeilBrown <neilb@suse.de>
  5. * Heavily based on earlier code which is:
  6. * Copyright (c) 2010 Grant Erickson <marathon96@gmail.com>
  7. *
  8. * Also based on pwm-samsung.c
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License
  12. * version 2 as published by the Free Software Foundation.
  13. *
  14. * Description:
  15. * This file is the core OMAP support for the generic, Linux
  16. * PWM driver / controller, using the OMAP's dual-mode timers.
  17. */
  18. #include <linux/clk.h>
  19. #include <linux/err.h>
  20. #include <linux/kernel.h>
  21. #include <linux/module.h>
  22. #include <linux/mutex.h>
  23. #include <linux/of.h>
  24. #include <linux/of_platform.h>
  25. #include <linux/platform_data/pwm_omap_dmtimer.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/pm_runtime.h>
  28. #include <linux/pwm.h>
  29. #include <linux/slab.h>
  30. #include <linux/time.h>
  31. #define DM_TIMER_LOAD_MIN 0xfffffffe
  32. struct pwm_omap_dmtimer_chip {
  33. struct pwm_chip chip;
  34. struct mutex mutex;
  35. pwm_omap_dmtimer *dm_timer;
  36. struct pwm_omap_dmtimer_pdata *pdata;
  37. struct platform_device *dm_timer_pdev;
  38. };
  39. static inline struct pwm_omap_dmtimer_chip *
  40. to_pwm_omap_dmtimer_chip(struct pwm_chip *chip)
  41. {
  42. return container_of(chip, struct pwm_omap_dmtimer_chip, chip);
  43. }
  44. static int pwm_omap_dmtimer_calc_value(unsigned long clk_rate, int ns)
  45. {
  46. u64 c = (u64)clk_rate * ns;
  47. do_div(c, NSEC_PER_SEC);
  48. return DM_TIMER_LOAD_MIN - c;
  49. }
  50. static void pwm_omap_dmtimer_start(struct pwm_omap_dmtimer_chip *omap)
  51. {
  52. /*
  53. * According to OMAP 4 TRM section 22.2.4.10 the counter should be
  54. * started at 0xFFFFFFFE when overflow and match is used to ensure
  55. * that the PWM line is toggled on the first event.
  56. *
  57. * Note that omap_dm_timer_enable/disable is for register access and
  58. * not the timer counter itself.
  59. */
  60. omap->pdata->enable(omap->dm_timer);
  61. omap->pdata->write_counter(omap->dm_timer, DM_TIMER_LOAD_MIN);
  62. omap->pdata->disable(omap->dm_timer);
  63. omap->pdata->start(omap->dm_timer);
  64. }
  65. static int pwm_omap_dmtimer_enable(struct pwm_chip *chip,
  66. struct pwm_device *pwm)
  67. {
  68. struct pwm_omap_dmtimer_chip *omap = to_pwm_omap_dmtimer_chip(chip);
  69. mutex_lock(&omap->mutex);
  70. pwm_omap_dmtimer_start(omap);
  71. mutex_unlock(&omap->mutex);
  72. return 0;
  73. }
  74. static void pwm_omap_dmtimer_disable(struct pwm_chip *chip,
  75. struct pwm_device *pwm)
  76. {
  77. struct pwm_omap_dmtimer_chip *omap = to_pwm_omap_dmtimer_chip(chip);
  78. mutex_lock(&omap->mutex);
  79. omap->pdata->stop(omap->dm_timer);
  80. mutex_unlock(&omap->mutex);
  81. }
  82. static int pwm_omap_dmtimer_config(struct pwm_chip *chip,
  83. struct pwm_device *pwm,
  84. int duty_ns, int period_ns)
  85. {
  86. struct pwm_omap_dmtimer_chip *omap = to_pwm_omap_dmtimer_chip(chip);
  87. int load_value, match_value;
  88. struct clk *fclk;
  89. unsigned long clk_rate;
  90. bool timer_active;
  91. dev_dbg(chip->dev, "duty cycle: %d, period %d\n", duty_ns, period_ns);
  92. mutex_lock(&omap->mutex);
  93. if (duty_ns == pwm_get_duty_cycle(pwm) &&
  94. period_ns == pwm_get_period(pwm)) {
  95. /* No change - don't cause any transients. */
  96. mutex_unlock(&omap->mutex);
  97. return 0;
  98. }
  99. fclk = omap->pdata->get_fclk(omap->dm_timer);
  100. if (!fclk) {
  101. dev_err(chip->dev, "invalid pmtimer fclk\n");
  102. mutex_unlock(&omap->mutex);
  103. return -EINVAL;
  104. }
  105. clk_rate = clk_get_rate(fclk);
  106. if (!clk_rate) {
  107. dev_err(chip->dev, "invalid pmtimer fclk rate\n");
  108. mutex_unlock(&omap->mutex);
  109. return -EINVAL;
  110. }
  111. dev_dbg(chip->dev, "clk rate: %luHz\n", clk_rate);
  112. /*
  113. * Calculate the appropriate load and match values based on the
  114. * specified period and duty cycle. The load value determines the
  115. * cycle time and the match value determines the duty cycle.
  116. */
  117. load_value = pwm_omap_dmtimer_calc_value(clk_rate, period_ns);
  118. match_value = pwm_omap_dmtimer_calc_value(clk_rate,
  119. period_ns - duty_ns);
  120. /*
  121. * We MUST stop the associated dual-mode timer before attempting to
  122. * write its registers, but calls to omap_dm_timer_start/stop must
  123. * be balanced so check if timer is active before calling timer_stop.
  124. */
  125. timer_active = pm_runtime_active(&omap->dm_timer_pdev->dev);
  126. if (timer_active)
  127. omap->pdata->stop(omap->dm_timer);
  128. omap->pdata->set_load(omap->dm_timer, true, load_value);
  129. omap->pdata->set_match(omap->dm_timer, true, match_value);
  130. dev_dbg(chip->dev, "load value: %#08x (%d), match value: %#08x (%d)\n",
  131. load_value, load_value, match_value, match_value);
  132. omap->pdata->set_pwm(omap->dm_timer,
  133. pwm->polarity == PWM_POLARITY_INVERSED,
  134. true,
  135. PWM_OMAP_DMTIMER_TRIGGER_OVERFLOW_AND_COMPARE);
  136. /* If config was called while timer was running it must be reenabled. */
  137. if (timer_active)
  138. pwm_omap_dmtimer_start(omap);
  139. mutex_unlock(&omap->mutex);
  140. return 0;
  141. }
  142. static int pwm_omap_dmtimer_set_polarity(struct pwm_chip *chip,
  143. struct pwm_device *pwm,
  144. enum pwm_polarity polarity)
  145. {
  146. struct pwm_omap_dmtimer_chip *omap = to_pwm_omap_dmtimer_chip(chip);
  147. /*
  148. * PWM core will not call set_polarity while PWM is enabled so it's
  149. * safe to reconfigure the timer here without stopping it first.
  150. */
  151. mutex_lock(&omap->mutex);
  152. omap->pdata->set_pwm(omap->dm_timer,
  153. polarity == PWM_POLARITY_INVERSED,
  154. true,
  155. PWM_OMAP_DMTIMER_TRIGGER_OVERFLOW_AND_COMPARE);
  156. mutex_unlock(&omap->mutex);
  157. return 0;
  158. }
  159. static const struct pwm_ops pwm_omap_dmtimer_ops = {
  160. .enable = pwm_omap_dmtimer_enable,
  161. .disable = pwm_omap_dmtimer_disable,
  162. .config = pwm_omap_dmtimer_config,
  163. .set_polarity = pwm_omap_dmtimer_set_polarity,
  164. .owner = THIS_MODULE,
  165. };
  166. static int pwm_omap_dmtimer_probe(struct platform_device *pdev)
  167. {
  168. struct device_node *np = pdev->dev.of_node;
  169. struct device_node *timer;
  170. struct pwm_omap_dmtimer_chip *omap;
  171. struct pwm_omap_dmtimer_pdata *pdata;
  172. pwm_omap_dmtimer *dm_timer;
  173. u32 prescaler;
  174. int status;
  175. pdata = dev_get_platdata(&pdev->dev);
  176. if (!pdata) {
  177. dev_err(&pdev->dev, "Missing dmtimer platform data\n");
  178. return -EINVAL;
  179. }
  180. if (!pdata->request_by_node ||
  181. !pdata->free ||
  182. !pdata->enable ||
  183. !pdata->disable ||
  184. !pdata->get_fclk ||
  185. !pdata->start ||
  186. !pdata->stop ||
  187. !pdata->set_load ||
  188. !pdata->set_match ||
  189. !pdata->set_pwm ||
  190. !pdata->set_prescaler ||
  191. !pdata->write_counter) {
  192. dev_err(&pdev->dev, "Incomplete dmtimer pdata structure\n");
  193. return -EINVAL;
  194. }
  195. timer = of_parse_phandle(np, "ti,timers", 0);
  196. if (!timer)
  197. return -ENODEV;
  198. if (!of_get_property(timer, "ti,timer-pwm", NULL)) {
  199. dev_err(&pdev->dev, "Missing ti,timer-pwm capability\n");
  200. return -ENODEV;
  201. }
  202. dm_timer = pdata->request_by_node(timer);
  203. if (!dm_timer)
  204. return -EPROBE_DEFER;
  205. omap = devm_kzalloc(&pdev->dev, sizeof(*omap), GFP_KERNEL);
  206. if (!omap) {
  207. pdata->free(dm_timer);
  208. return -ENOMEM;
  209. }
  210. omap->pdata = pdata;
  211. omap->dm_timer = dm_timer;
  212. omap->dm_timer_pdev = of_find_device_by_node(timer);
  213. if (!omap->dm_timer_pdev) {
  214. dev_err(&pdev->dev, "Unable to find timer pdev\n");
  215. omap->pdata->free(dm_timer);
  216. return -EINVAL;
  217. }
  218. /*
  219. * Ensure that the timer is stopped before we allow PWM core to call
  220. * pwm_enable.
  221. */
  222. if (pm_runtime_active(&omap->dm_timer_pdev->dev))
  223. omap->pdata->stop(omap->dm_timer);
  224. /* setup dmtimer prescaler */
  225. if (!of_property_read_u32(pdev->dev.of_node, "ti,prescaler",
  226. &prescaler))
  227. omap->pdata->set_prescaler(omap->dm_timer, prescaler);
  228. omap->chip.dev = &pdev->dev;
  229. omap->chip.ops = &pwm_omap_dmtimer_ops;
  230. omap->chip.base = -1;
  231. omap->chip.npwm = 1;
  232. omap->chip.of_xlate = of_pwm_xlate_with_flags;
  233. omap->chip.of_pwm_n_cells = 3;
  234. mutex_init(&omap->mutex);
  235. status = pwmchip_add(&omap->chip);
  236. if (status < 0) {
  237. dev_err(&pdev->dev, "failed to register PWM\n");
  238. omap->pdata->free(omap->dm_timer);
  239. return status;
  240. }
  241. platform_set_drvdata(pdev, omap);
  242. return 0;
  243. }
  244. static int pwm_omap_dmtimer_remove(struct platform_device *pdev)
  245. {
  246. struct pwm_omap_dmtimer_chip *omap = platform_get_drvdata(pdev);
  247. if (pm_runtime_active(&omap->dm_timer_pdev->dev))
  248. omap->pdata->stop(omap->dm_timer);
  249. omap->pdata->free(omap->dm_timer);
  250. mutex_destroy(&omap->mutex);
  251. return pwmchip_remove(&omap->chip);
  252. }
  253. static const struct of_device_id pwm_omap_dmtimer_of_match[] = {
  254. {.compatible = "ti,omap-dmtimer-pwm"},
  255. {}
  256. };
  257. MODULE_DEVICE_TABLE(of, pwm_omap_dmtimer_of_match);
  258. static struct platform_driver pwm_omap_dmtimer_driver = {
  259. .driver = {
  260. .name = "omap-dmtimer-pwm",
  261. .of_match_table = of_match_ptr(pwm_omap_dmtimer_of_match),
  262. },
  263. .probe = pwm_omap_dmtimer_probe,
  264. .remove = pwm_omap_dmtimer_remove,
  265. };
  266. module_platform_driver(pwm_omap_dmtimer_driver);
  267. MODULE_AUTHOR("Grant Erickson <marathon96@gmail.com>");
  268. MODULE_AUTHOR("NeilBrown <neilb@suse.de>");
  269. MODULE_AUTHOR("Neil Armstrong <narmstrong@baylibre.com>");
  270. MODULE_LICENSE("GPL v2");
  271. MODULE_DESCRIPTION("OMAP PWM Driver using Dual-mode Timers");