pwm-omap-dmtimer.c 9.9 KB

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