pwm-omap-dmtimer.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2015 Neil Armstrong <narmstrong@baylibre.com>
  4. * Copyright (c) 2014 Joachim Eastwood <manabian@gmail.com>
  5. * Copyright (c) 2012 NeilBrown <neilb@suse.de>
  6. * Heavily based on earlier code which is:
  7. * Copyright (c) 2010 Grant Erickson <marathon96@gmail.com>
  8. *
  9. * Also based on pwm-samsung.c
  10. *
  11. * Description:
  12. * This file is the core OMAP support for the generic, Linux
  13. * PWM driver / controller, using the OMAP's dual-mode timers
  14. * with a timer counter that goes up. When it overflows it gets
  15. * reloaded with the load value and the pwm output goes up.
  16. * When counter matches with match register, the output goes down.
  17. * Reference Manual: https://www.ti.com/lit/ug/spruh73q/spruh73q.pdf
  18. *
  19. * Limitations:
  20. * - When PWM is stopped, timer counter gets stopped immediately. This
  21. * doesn't allow the current PWM period to complete and stops abruptly.
  22. * - When PWM is running and changing both duty cycle and period,
  23. * we cannot prevent in software that the output might produce
  24. * a period with mixed settings. Especially when period/duty_cyle
  25. * is updated while the pwm pin is high, current pwm period/duty_cycle
  26. * can get updated as below based on the current timer counter:
  27. * - period for current cycle = current_period + new period
  28. * - duty_cycle for current period = current period + new duty_cycle.
  29. * - PWM OMAP DM timer cannot change the polarity when pwm is active. When
  30. * user requests a change in polarity when in active state:
  31. * - PWM is stopped abruptly(without completing the current cycle)
  32. * - Polarity is changed
  33. * - A fresh cycle is started.
  34. */
  35. #include <linux/clk.h>
  36. #include <linux/err.h>
  37. #include <linux/kernel.h>
  38. #include <linux/module.h>
  39. #include <linux/of.h>
  40. #include <linux/of_platform.h>
  41. #include <clocksource/timer-ti-dm.h>
  42. #include <linux/platform_data/dmtimer-omap.h>
  43. #include <linux/platform_device.h>
  44. #include <linux/pm_runtime.h>
  45. #include <linux/pwm.h>
  46. #include <linux/slab.h>
  47. #include <linux/time.h>
  48. #define DM_TIMER_LOAD_MIN 0xfffffffe
  49. #define DM_TIMER_MAX 0xffffffff
  50. /**
  51. * struct pwm_omap_dmtimer_chip - Structure representing a pwm chip
  52. * corresponding to omap dmtimer.
  53. * @dm_timer: Pointer to omap dm timer.
  54. * @pdata: Pointer to omap dm timer ops.
  55. * @dm_timer_pdev: Pointer to omap dm timer platform device
  56. */
  57. struct pwm_omap_dmtimer_chip {
  58. /* Mutex to protect pwm apply state */
  59. struct omap_dm_timer *dm_timer;
  60. const struct omap_dm_timer_ops *pdata;
  61. struct platform_device *dm_timer_pdev;
  62. };
  63. static inline struct pwm_omap_dmtimer_chip *
  64. to_pwm_omap_dmtimer_chip(struct pwm_chip *chip)
  65. {
  66. return pwmchip_get_drvdata(chip);
  67. }
  68. /**
  69. * pwm_omap_dmtimer_get_clock_cycles() - Get clock cycles in a time frame
  70. * @clk_rate: pwm timer clock rate
  71. * @ns: time frame in nano seconds.
  72. *
  73. * Return number of clock cycles in a given period(ins ns).
  74. */
  75. static u32 pwm_omap_dmtimer_get_clock_cycles(unsigned long clk_rate, int ns)
  76. {
  77. return DIV_ROUND_CLOSEST_ULL((u64)clk_rate * ns, NSEC_PER_SEC);
  78. }
  79. /**
  80. * pwm_omap_dmtimer_start() - Start the pwm omap dm timer in pwm mode
  81. * @omap: Pointer to pwm omap dm timer chip
  82. */
  83. static void pwm_omap_dmtimer_start(struct pwm_omap_dmtimer_chip *omap)
  84. {
  85. /*
  86. * According to OMAP 4 TRM section 22.2.4.10 the counter should be
  87. * started at 0xFFFFFFFE when overflow and match is used to ensure
  88. * that the PWM line is toggled on the first event.
  89. *
  90. * Note that omap_dm_timer_enable/disable is for register access and
  91. * not the timer counter itself.
  92. */
  93. omap->pdata->enable(omap->dm_timer);
  94. omap->pdata->write_counter(omap->dm_timer, DM_TIMER_LOAD_MIN);
  95. omap->pdata->disable(omap->dm_timer);
  96. omap->pdata->start(omap->dm_timer);
  97. }
  98. /**
  99. * pwm_omap_dmtimer_is_enabled() - Detect if the pwm is enabled.
  100. * @omap: Pointer to pwm omap dm timer chip
  101. *
  102. * Return true if pwm is enabled else false.
  103. */
  104. static bool pwm_omap_dmtimer_is_enabled(struct pwm_omap_dmtimer_chip *omap)
  105. {
  106. u32 status;
  107. status = omap->pdata->get_pwm_status(omap->dm_timer);
  108. return !!(status & OMAP_TIMER_CTRL_ST);
  109. }
  110. /**
  111. * pwm_omap_dmtimer_polarity() - Detect the polarity of pwm.
  112. * @omap: Pointer to pwm omap dm timer chip
  113. *
  114. * Return the polarity of pwm.
  115. */
  116. static int pwm_omap_dmtimer_polarity(struct pwm_omap_dmtimer_chip *omap)
  117. {
  118. u32 status;
  119. status = omap->pdata->get_pwm_status(omap->dm_timer);
  120. return !!(status & OMAP_TIMER_CTRL_SCPWM);
  121. }
  122. /**
  123. * pwm_omap_dmtimer_config() - Update the configuration of pwm omap dm timer
  124. * @chip: Pointer to PWM controller
  125. * @pwm: Pointer to PWM channel
  126. * @duty_ns: New duty cycle in nano seconds
  127. * @period_ns: New period in nano seconds
  128. *
  129. * Return 0 if successfully changed the period/duty_cycle else appropriate
  130. * error.
  131. */
  132. static int pwm_omap_dmtimer_config(struct pwm_chip *chip,
  133. struct pwm_device *pwm,
  134. int duty_ns, int period_ns)
  135. {
  136. struct pwm_omap_dmtimer_chip *omap = to_pwm_omap_dmtimer_chip(chip);
  137. u32 period_cycles, duty_cycles;
  138. u32 load_value, match_value;
  139. unsigned long clk_rate;
  140. struct clk *fclk;
  141. dev_dbg(pwmchip_parent(chip), "requested duty cycle: %d ns, period: %d ns\n",
  142. duty_ns, period_ns);
  143. if (duty_ns == pwm_get_duty_cycle(pwm) &&
  144. period_ns == pwm_get_period(pwm))
  145. return 0;
  146. fclk = omap->pdata->get_fclk(omap->dm_timer);
  147. if (!fclk) {
  148. dev_err(pwmchip_parent(chip), "invalid pmtimer fclk\n");
  149. return -EINVAL;
  150. }
  151. clk_rate = clk_get_rate(fclk);
  152. if (!clk_rate) {
  153. dev_err(pwmchip_parent(chip), "invalid pmtimer fclk rate\n");
  154. return -EINVAL;
  155. }
  156. dev_dbg(pwmchip_parent(chip), "clk rate: %luHz\n", clk_rate);
  157. /*
  158. * Calculate the appropriate load and match values based on the
  159. * specified period and duty cycle. The load value determines the
  160. * period time and the match value determines the duty time.
  161. *
  162. * The period lasts for (DM_TIMER_MAX-load_value+1) clock cycles.
  163. * Similarly, the active time lasts (match_value-load_value+1) cycles.
  164. * The non-active time is the remainder: (DM_TIMER_MAX-match_value)
  165. * clock cycles.
  166. *
  167. * NOTE: It is required that: load_value <= match_value < DM_TIMER_MAX
  168. *
  169. * References:
  170. * OMAP4430/60/70 TRM sections 22.2.4.10 and 22.2.4.11
  171. * AM335x Sitara TRM sections 20.1.3.5 and 20.1.3.6
  172. */
  173. period_cycles = pwm_omap_dmtimer_get_clock_cycles(clk_rate, period_ns);
  174. duty_cycles = pwm_omap_dmtimer_get_clock_cycles(clk_rate, duty_ns);
  175. if (period_cycles < 2) {
  176. dev_info(pwmchip_parent(chip),
  177. "period %d ns too short for clock rate %lu Hz\n",
  178. period_ns, clk_rate);
  179. return -EINVAL;
  180. }
  181. if (duty_cycles < 1) {
  182. dev_dbg(pwmchip_parent(chip),
  183. "duty cycle %d ns is too short for clock rate %lu Hz\n",
  184. duty_ns, clk_rate);
  185. dev_dbg(pwmchip_parent(chip), "using minimum of 1 clock cycle\n");
  186. duty_cycles = 1;
  187. } else if (duty_cycles >= period_cycles) {
  188. dev_dbg(pwmchip_parent(chip),
  189. "duty cycle %d ns is too long for period %d ns at clock rate %lu Hz\n",
  190. duty_ns, period_ns, clk_rate);
  191. dev_dbg(pwmchip_parent(chip), "using maximum of 1 clock cycle less than period\n");
  192. duty_cycles = period_cycles - 1;
  193. }
  194. dev_dbg(pwmchip_parent(chip), "effective duty cycle: %lld ns, period: %lld ns\n",
  195. DIV_ROUND_CLOSEST_ULL((u64)NSEC_PER_SEC * duty_cycles,
  196. clk_rate),
  197. DIV_ROUND_CLOSEST_ULL((u64)NSEC_PER_SEC * period_cycles,
  198. clk_rate));
  199. load_value = (DM_TIMER_MAX - period_cycles) + 1;
  200. match_value = load_value + duty_cycles - 1;
  201. omap->pdata->set_load(omap->dm_timer, load_value);
  202. omap->pdata->set_match(omap->dm_timer, true, match_value);
  203. dev_dbg(pwmchip_parent(chip), "load value: %#08x (%d), match value: %#08x (%d)\n",
  204. load_value, load_value, match_value, match_value);
  205. return 0;
  206. }
  207. /**
  208. * pwm_omap_dmtimer_set_polarity() - Changes the polarity of the pwm dm timer.
  209. * @chip: Pointer to PWM controller
  210. * @pwm: Pointer to PWM channel
  211. * @polarity: New pwm polarity to be set
  212. */
  213. static void pwm_omap_dmtimer_set_polarity(struct pwm_chip *chip,
  214. struct pwm_device *pwm,
  215. enum pwm_polarity polarity)
  216. {
  217. struct pwm_omap_dmtimer_chip *omap = to_pwm_omap_dmtimer_chip(chip);
  218. bool enabled;
  219. /* Disable the PWM before changing the polarity. */
  220. enabled = pwm_omap_dmtimer_is_enabled(omap);
  221. if (enabled)
  222. omap->pdata->stop(omap->dm_timer);
  223. omap->pdata->set_pwm(omap->dm_timer,
  224. polarity == PWM_POLARITY_INVERSED,
  225. true, OMAP_TIMER_TRIGGER_OVERFLOW_AND_COMPARE,
  226. true);
  227. if (enabled)
  228. pwm_omap_dmtimer_start(omap);
  229. }
  230. /**
  231. * pwm_omap_dmtimer_apply() - Changes the state of the pwm omap dm timer.
  232. * @chip: Pointer to PWM controller
  233. * @pwm: Pointer to PWM channel
  234. * @state: New state to apply
  235. *
  236. * Return 0 if successfully changed the state else appropriate error.
  237. */
  238. static int pwm_omap_dmtimer_apply(struct pwm_chip *chip,
  239. struct pwm_device *pwm,
  240. const struct pwm_state *state)
  241. {
  242. struct pwm_omap_dmtimer_chip *omap = to_pwm_omap_dmtimer_chip(chip);
  243. int ret;
  244. if (pwm_omap_dmtimer_is_enabled(omap) && !state->enabled) {
  245. omap->pdata->stop(omap->dm_timer);
  246. return 0;
  247. }
  248. if (pwm_omap_dmtimer_polarity(omap) != state->polarity)
  249. pwm_omap_dmtimer_set_polarity(chip, pwm, state->polarity);
  250. ret = pwm_omap_dmtimer_config(chip, pwm, state->duty_cycle,
  251. state->period);
  252. if (ret)
  253. return ret;
  254. if (!pwm_omap_dmtimer_is_enabled(omap) && state->enabled) {
  255. omap->pdata->set_pwm(omap->dm_timer,
  256. state->polarity == PWM_POLARITY_INVERSED,
  257. true,
  258. OMAP_TIMER_TRIGGER_OVERFLOW_AND_COMPARE,
  259. true);
  260. pwm_omap_dmtimer_start(omap);
  261. }
  262. return 0;
  263. }
  264. static const struct pwm_ops pwm_omap_dmtimer_ops = {
  265. .apply = pwm_omap_dmtimer_apply,
  266. };
  267. static int pwm_omap_dmtimer_probe(struct platform_device *pdev)
  268. {
  269. struct device_node *np = pdev->dev.of_node;
  270. struct dmtimer_platform_data *timer_pdata;
  271. const struct omap_dm_timer_ops *pdata;
  272. struct platform_device *timer_pdev;
  273. struct pwm_chip *chip;
  274. struct pwm_omap_dmtimer_chip *omap;
  275. struct omap_dm_timer *dm_timer;
  276. struct device_node *timer;
  277. int ret = 0;
  278. u32 v;
  279. timer = of_parse_phandle(np, "ti,timers", 0);
  280. if (!timer)
  281. return -ENODEV;
  282. timer_pdev = of_find_device_by_node(timer);
  283. if (!timer_pdev) {
  284. dev_err(&pdev->dev, "Unable to find Timer pdev\n");
  285. ret = -ENODEV;
  286. goto err_find_timer_pdev;
  287. }
  288. timer_pdata = dev_get_platdata(&timer_pdev->dev);
  289. if (!timer_pdata) {
  290. dev_dbg(&pdev->dev,
  291. "dmtimer pdata structure NULL, deferring probe\n");
  292. ret = -EPROBE_DEFER;
  293. goto err_platdata;
  294. }
  295. pdata = timer_pdata->timer_ops;
  296. if (!pdata || !pdata->request_by_node ||
  297. !pdata->free ||
  298. !pdata->enable ||
  299. !pdata->disable ||
  300. !pdata->get_fclk ||
  301. !pdata->start ||
  302. !pdata->stop ||
  303. !pdata->set_load ||
  304. !pdata->set_match ||
  305. !pdata->set_pwm ||
  306. !pdata->get_pwm_status ||
  307. !pdata->set_prescaler ||
  308. !pdata->write_counter) {
  309. dev_err(&pdev->dev, "Incomplete dmtimer pdata structure\n");
  310. ret = -EINVAL;
  311. goto err_platdata;
  312. }
  313. if (!of_property_read_bool(timer, "ti,timer-pwm")) {
  314. dev_err(&pdev->dev, "Missing ti,timer-pwm capability\n");
  315. ret = -ENODEV;
  316. goto err_timer_property;
  317. }
  318. dm_timer = pdata->request_by_node(timer);
  319. if (!dm_timer) {
  320. ret = -EPROBE_DEFER;
  321. goto err_request_timer;
  322. }
  323. chip = devm_pwmchip_alloc(&pdev->dev, 1, sizeof(*omap));
  324. if (IS_ERR(chip)) {
  325. ret = PTR_ERR(chip);
  326. goto err_alloc_omap;
  327. }
  328. omap = to_pwm_omap_dmtimer_chip(chip);
  329. omap->pdata = pdata;
  330. omap->dm_timer = dm_timer;
  331. omap->dm_timer_pdev = timer_pdev;
  332. /*
  333. * Ensure that the timer is stopped before we allow PWM core to call
  334. * pwm_enable.
  335. */
  336. if (pm_runtime_active(&omap->dm_timer_pdev->dev))
  337. omap->pdata->stop(omap->dm_timer);
  338. if (!of_property_read_u32(pdev->dev.of_node, "ti,prescaler", &v))
  339. omap->pdata->set_prescaler(omap->dm_timer, v);
  340. /* setup dmtimer clock source */
  341. if (!of_property_read_u32(pdev->dev.of_node, "ti,clock-source", &v))
  342. omap->pdata->set_source(omap->dm_timer, v);
  343. chip->ops = &pwm_omap_dmtimer_ops;
  344. ret = pwmchip_add(chip);
  345. if (ret < 0) {
  346. dev_err(&pdev->dev, "failed to register PWM\n");
  347. goto err_pwmchip_add;
  348. }
  349. of_node_put(timer);
  350. platform_set_drvdata(pdev, chip);
  351. return 0;
  352. err_pwmchip_add:
  353. /*
  354. * *omap is allocated using devm_kzalloc,
  355. * so no free necessary here
  356. */
  357. err_alloc_omap:
  358. pdata->free(dm_timer);
  359. err_request_timer:
  360. err_timer_property:
  361. err_platdata:
  362. put_device(&timer_pdev->dev);
  363. err_find_timer_pdev:
  364. of_node_put(timer);
  365. return ret;
  366. }
  367. static void pwm_omap_dmtimer_remove(struct platform_device *pdev)
  368. {
  369. struct pwm_chip *chip = platform_get_drvdata(pdev);
  370. struct pwm_omap_dmtimer_chip *omap = to_pwm_omap_dmtimer_chip(chip);
  371. pwmchip_remove(chip);
  372. if (pm_runtime_active(&omap->dm_timer_pdev->dev))
  373. omap->pdata->stop(omap->dm_timer);
  374. omap->pdata->free(omap->dm_timer);
  375. put_device(&omap->dm_timer_pdev->dev);
  376. }
  377. static const struct of_device_id pwm_omap_dmtimer_of_match[] = {
  378. {.compatible = "ti,omap-dmtimer-pwm"},
  379. {}
  380. };
  381. MODULE_DEVICE_TABLE(of, pwm_omap_dmtimer_of_match);
  382. static struct platform_driver pwm_omap_dmtimer_driver = {
  383. .driver = {
  384. .name = "omap-dmtimer-pwm",
  385. .of_match_table = pwm_omap_dmtimer_of_match,
  386. },
  387. .probe = pwm_omap_dmtimer_probe,
  388. .remove = pwm_omap_dmtimer_remove,
  389. };
  390. module_platform_driver(pwm_omap_dmtimer_driver);
  391. MODULE_AUTHOR("Grant Erickson <marathon96@gmail.com>");
  392. MODULE_AUTHOR("NeilBrown <neilb@suse.de>");
  393. MODULE_AUTHOR("Neil Armstrong <narmstrong@baylibre.com>");
  394. MODULE_LICENSE("GPL v2");
  395. MODULE_DESCRIPTION("OMAP PWM Driver using Dual-mode Timers");