pwm-omap-dmtimer.c 9.2 KB

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