pwm-omap-dmtimer.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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/dmtimer-omap.h>
  26. #include <linux/platform_data/pwm_omap_dmtimer.h>
  27. #include <linux/platform_device.h>
  28. #include <linux/pm_runtime.h>
  29. #include <linux/pwm.h>
  30. #include <linux/slab.h>
  31. #include <linux/time.h>
  32. #define DM_TIMER_LOAD_MIN 0xfffffffe
  33. #define DM_TIMER_MAX 0xffffffff
  34. struct pwm_omap_dmtimer_chip {
  35. struct pwm_chip chip;
  36. struct mutex mutex;
  37. pwm_omap_dmtimer *dm_timer;
  38. const struct omap_dm_timer_ops *pdata;
  39. struct platform_device *dm_timer_pdev;
  40. };
  41. static inline struct pwm_omap_dmtimer_chip *
  42. to_pwm_omap_dmtimer_chip(struct pwm_chip *chip)
  43. {
  44. return container_of(chip, struct pwm_omap_dmtimer_chip, chip);
  45. }
  46. static u32 pwm_omap_dmtimer_get_clock_cycles(unsigned long clk_rate, int ns)
  47. {
  48. return DIV_ROUND_CLOSEST_ULL((u64)clk_rate * ns, NSEC_PER_SEC);
  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. u32 period_cycles, duty_cycles;
  88. u32 load_value, match_value;
  89. struct clk *fclk;
  90. unsigned long clk_rate;
  91. bool timer_active;
  92. dev_dbg(chip->dev, "requested duty cycle: %d ns, period: %d ns\n",
  93. 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. goto err_einval;
  105. }
  106. clk_rate = clk_get_rate(fclk);
  107. if (!clk_rate) {
  108. dev_err(chip->dev, "invalid pmtimer fclk rate\n");
  109. goto err_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. * period time and the match value determines the duty time.
  116. *
  117. * The period lasts for (DM_TIMER_MAX-load_value+1) clock cycles.
  118. * Similarly, the active time lasts (match_value-load_value+1) cycles.
  119. * The non-active time is the remainder: (DM_TIMER_MAX-match_value)
  120. * clock cycles.
  121. *
  122. * NOTE: It is required that: load_value <= match_value < DM_TIMER_MAX
  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. if (period_cycles < 2) {
  131. dev_info(chip->dev,
  132. "period %d ns too short for clock rate %lu Hz\n",
  133. period_ns, clk_rate);
  134. goto err_einval;
  135. }
  136. if (duty_cycles < 1) {
  137. dev_dbg(chip->dev,
  138. "duty cycle %d ns is too short for clock rate %lu Hz\n",
  139. duty_ns, clk_rate);
  140. dev_dbg(chip->dev, "using minimum of 1 clock cycle\n");
  141. duty_cycles = 1;
  142. } else if (duty_cycles >= period_cycles) {
  143. dev_dbg(chip->dev,
  144. "duty cycle %d ns is too long for period %d ns at clock rate %lu Hz\n",
  145. duty_ns, period_ns, clk_rate);
  146. dev_dbg(chip->dev, "using maximum of 1 clock cycle less than period\n");
  147. duty_cycles = period_cycles - 1;
  148. }
  149. dev_dbg(chip->dev, "effective duty cycle: %lld ns, period: %lld ns\n",
  150. DIV_ROUND_CLOSEST_ULL((u64)NSEC_PER_SEC * duty_cycles,
  151. clk_rate),
  152. DIV_ROUND_CLOSEST_ULL((u64)NSEC_PER_SEC * period_cycles,
  153. clk_rate));
  154. load_value = (DM_TIMER_MAX - period_cycles) + 1;
  155. match_value = load_value + duty_cycles - 1;
  156. /*
  157. * We MUST stop the associated dual-mode timer before attempting to
  158. * write its registers, but calls to omap_dm_timer_start/stop must
  159. * be balanced so check if timer is active before calling timer_stop.
  160. */
  161. timer_active = pm_runtime_active(&omap->dm_timer_pdev->dev);
  162. if (timer_active)
  163. omap->pdata->stop(omap->dm_timer);
  164. omap->pdata->set_load(omap->dm_timer, true, load_value);
  165. omap->pdata->set_match(omap->dm_timer, true, match_value);
  166. dev_dbg(chip->dev, "load value: %#08x (%d), match value: %#08x (%d)\n",
  167. load_value, load_value, match_value, match_value);
  168. omap->pdata->set_pwm(omap->dm_timer,
  169. pwm_get_polarity(pwm) == PWM_POLARITY_INVERSED,
  170. true,
  171. PWM_OMAP_DMTIMER_TRIGGER_OVERFLOW_AND_COMPARE);
  172. /* If config was called while timer was running it must be reenabled. */
  173. if (timer_active)
  174. pwm_omap_dmtimer_start(omap);
  175. mutex_unlock(&omap->mutex);
  176. return 0;
  177. err_einval:
  178. mutex_unlock(&omap->mutex);
  179. return -EINVAL;
  180. }
  181. static int pwm_omap_dmtimer_set_polarity(struct pwm_chip *chip,
  182. struct pwm_device *pwm,
  183. enum pwm_polarity polarity)
  184. {
  185. struct pwm_omap_dmtimer_chip *omap = to_pwm_omap_dmtimer_chip(chip);
  186. /*
  187. * PWM core will not call set_polarity while PWM is enabled so it's
  188. * safe to reconfigure the timer here without stopping it first.
  189. */
  190. mutex_lock(&omap->mutex);
  191. omap->pdata->set_pwm(omap->dm_timer,
  192. polarity == PWM_POLARITY_INVERSED,
  193. true,
  194. PWM_OMAP_DMTIMER_TRIGGER_OVERFLOW_AND_COMPARE);
  195. mutex_unlock(&omap->mutex);
  196. return 0;
  197. }
  198. static const struct pwm_ops pwm_omap_dmtimer_ops = {
  199. .enable = pwm_omap_dmtimer_enable,
  200. .disable = pwm_omap_dmtimer_disable,
  201. .config = pwm_omap_dmtimer_config,
  202. .set_polarity = pwm_omap_dmtimer_set_polarity,
  203. .owner = THIS_MODULE,
  204. };
  205. static int pwm_omap_dmtimer_probe(struct platform_device *pdev)
  206. {
  207. struct device_node *np = pdev->dev.of_node;
  208. struct device_node *timer;
  209. struct platform_device *timer_pdev;
  210. struct pwm_omap_dmtimer_chip *omap;
  211. struct dmtimer_platform_data *timer_pdata;
  212. const struct omap_dm_timer_ops *pdata;
  213. pwm_omap_dmtimer *dm_timer;
  214. u32 v;
  215. int ret = 0;
  216. timer = of_parse_phandle(np, "ti,timers", 0);
  217. if (!timer)
  218. return -ENODEV;
  219. timer_pdev = of_find_device_by_node(timer);
  220. if (!timer_pdev) {
  221. dev_err(&pdev->dev, "Unable to find Timer pdev\n");
  222. ret = -ENODEV;
  223. goto err_find_timer_pdev;
  224. }
  225. timer_pdata = dev_get_platdata(&timer_pdev->dev);
  226. if (!timer_pdata) {
  227. dev_dbg(&pdev->dev,
  228. "dmtimer pdata structure NULL, deferring probe\n");
  229. ret = -EPROBE_DEFER;
  230. goto err_platdata;
  231. }
  232. pdata = timer_pdata->timer_ops;
  233. if (!pdata || !pdata->request_by_node ||
  234. !pdata->free ||
  235. !pdata->enable ||
  236. !pdata->disable ||
  237. !pdata->get_fclk ||
  238. !pdata->start ||
  239. !pdata->stop ||
  240. !pdata->set_load ||
  241. !pdata->set_match ||
  242. !pdata->set_pwm ||
  243. !pdata->set_prescaler ||
  244. !pdata->write_counter) {
  245. dev_err(&pdev->dev, "Incomplete dmtimer pdata structure\n");
  246. ret = -EINVAL;
  247. goto err_platdata;
  248. }
  249. if (!of_get_property(timer, "ti,timer-pwm", NULL)) {
  250. dev_err(&pdev->dev, "Missing ti,timer-pwm capability\n");
  251. ret = -ENODEV;
  252. goto err_timer_property;
  253. }
  254. dm_timer = pdata->request_by_node(timer);
  255. if (!dm_timer) {
  256. ret = -EPROBE_DEFER;
  257. goto err_request_timer;
  258. }
  259. omap = devm_kzalloc(&pdev->dev, sizeof(*omap), GFP_KERNEL);
  260. if (!omap) {
  261. ret = -ENOMEM;
  262. goto err_alloc_omap;
  263. }
  264. omap->pdata = pdata;
  265. omap->dm_timer = dm_timer;
  266. omap->dm_timer_pdev = timer_pdev;
  267. /*
  268. * Ensure that the timer is stopped before we allow PWM core to call
  269. * pwm_enable.
  270. */
  271. if (pm_runtime_active(&omap->dm_timer_pdev->dev))
  272. omap->pdata->stop(omap->dm_timer);
  273. if (!of_property_read_u32(pdev->dev.of_node, "ti,prescaler", &v))
  274. omap->pdata->set_prescaler(omap->dm_timer, v);
  275. /* setup dmtimer clock source */
  276. if (!of_property_read_u32(pdev->dev.of_node, "ti,clock-source", &v))
  277. omap->pdata->set_source(omap->dm_timer, v);
  278. omap->chip.dev = &pdev->dev;
  279. omap->chip.ops = &pwm_omap_dmtimer_ops;
  280. omap->chip.base = -1;
  281. omap->chip.npwm = 1;
  282. omap->chip.of_xlate = of_pwm_xlate_with_flags;
  283. omap->chip.of_pwm_n_cells = 3;
  284. mutex_init(&omap->mutex);
  285. ret = pwmchip_add(&omap->chip);
  286. if (ret < 0) {
  287. dev_err(&pdev->dev, "failed to register PWM\n");
  288. goto err_pwmchip_add;
  289. }
  290. of_node_put(timer);
  291. platform_set_drvdata(pdev, omap);
  292. return 0;
  293. err_pwmchip_add:
  294. /*
  295. * *omap is allocated using devm_kzalloc,
  296. * so no free necessary here
  297. */
  298. err_alloc_omap:
  299. pdata->free(dm_timer);
  300. err_request_timer:
  301. err_timer_property:
  302. err_platdata:
  303. put_device(&timer_pdev->dev);
  304. err_find_timer_pdev:
  305. of_node_put(timer);
  306. return ret;
  307. }
  308. static int pwm_omap_dmtimer_remove(struct platform_device *pdev)
  309. {
  310. struct pwm_omap_dmtimer_chip *omap = platform_get_drvdata(pdev);
  311. int ret;
  312. ret = pwmchip_remove(&omap->chip);
  313. if (ret)
  314. return ret;
  315. if (pm_runtime_active(&omap->dm_timer_pdev->dev))
  316. omap->pdata->stop(omap->dm_timer);
  317. omap->pdata->free(omap->dm_timer);
  318. put_device(&omap->dm_timer_pdev->dev);
  319. mutex_destroy(&omap->mutex);
  320. return 0;
  321. }
  322. static const struct of_device_id pwm_omap_dmtimer_of_match[] = {
  323. {.compatible = "ti,omap-dmtimer-pwm"},
  324. {}
  325. };
  326. MODULE_DEVICE_TABLE(of, pwm_omap_dmtimer_of_match);
  327. static struct platform_driver pwm_omap_dmtimer_driver = {
  328. .driver = {
  329. .name = "omap-dmtimer-pwm",
  330. .of_match_table = of_match_ptr(pwm_omap_dmtimer_of_match),
  331. },
  332. .probe = pwm_omap_dmtimer_probe,
  333. .remove = pwm_omap_dmtimer_remove,
  334. };
  335. module_platform_driver(pwm_omap_dmtimer_driver);
  336. MODULE_AUTHOR("Grant Erickson <marathon96@gmail.com>");
  337. MODULE_AUTHOR("NeilBrown <neilb@suse.de>");
  338. MODULE_AUTHOR("Neil Armstrong <narmstrong@baylibre.com>");
  339. MODULE_LICENSE("GPL v2");
  340. MODULE_DESCRIPTION("OMAP PWM Driver using Dual-mode Timers");