pwm-omap-dmtimer.c 10 KB

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