pwm-renesas-tpu.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * R-Mobile TPU PWM driver
  4. *
  5. * Copyright (C) 2012 Renesas Solutions Corp.
  6. */
  7. #include <linux/clk.h>
  8. #include <linux/err.h>
  9. #include <linux/io.h>
  10. #include <linux/init.h>
  11. #include <linux/ioport.h>
  12. #include <linux/module.h>
  13. #include <linux/of.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/pm_runtime.h>
  16. #include <linux/pwm.h>
  17. #include <linux/slab.h>
  18. #include <linux/spinlock.h>
  19. #define TPU_CHANNEL_MAX 4
  20. #define TPU_TSTR 0x00 /* Timer start register (shared) */
  21. #define TPU_TCRn 0x00 /* Timer control register */
  22. #define TPU_TCR_CCLR_NONE (0 << 5)
  23. #define TPU_TCR_CCLR_TGRA (1 << 5)
  24. #define TPU_TCR_CCLR_TGRB (2 << 5)
  25. #define TPU_TCR_CCLR_TGRC (5 << 5)
  26. #define TPU_TCR_CCLR_TGRD (6 << 5)
  27. #define TPU_TCR_CKEG_RISING (0 << 3)
  28. #define TPU_TCR_CKEG_FALLING (1 << 3)
  29. #define TPU_TCR_CKEG_BOTH (2 << 3)
  30. #define TPU_TMDRn 0x04 /* Timer mode register */
  31. #define TPU_TMDR_BFWT (1 << 6)
  32. #define TPU_TMDR_BFB (1 << 5)
  33. #define TPU_TMDR_BFA (1 << 4)
  34. #define TPU_TMDR_MD_NORMAL (0 << 0)
  35. #define TPU_TMDR_MD_PWM (2 << 0)
  36. #define TPU_TIORn 0x08 /* Timer I/O control register */
  37. #define TPU_TIOR_IOA_0 (0 << 0)
  38. #define TPU_TIOR_IOA_0_CLR (1 << 0)
  39. #define TPU_TIOR_IOA_0_SET (2 << 0)
  40. #define TPU_TIOR_IOA_0_TOGGLE (3 << 0)
  41. #define TPU_TIOR_IOA_1 (4 << 0)
  42. #define TPU_TIOR_IOA_1_CLR (5 << 0)
  43. #define TPU_TIOR_IOA_1_SET (6 << 0)
  44. #define TPU_TIOR_IOA_1_TOGGLE (7 << 0)
  45. #define TPU_TIERn 0x0c /* Timer interrupt enable register */
  46. #define TPU_TSRn 0x10 /* Timer status register */
  47. #define TPU_TCNTn 0x14 /* Timer counter */
  48. #define TPU_TGRAn 0x18 /* Timer general register A */
  49. #define TPU_TGRBn 0x1c /* Timer general register B */
  50. #define TPU_TGRCn 0x20 /* Timer general register C */
  51. #define TPU_TGRDn 0x24 /* Timer general register D */
  52. #define TPU_CHANNEL_OFFSET 0x10
  53. #define TPU_CHANNEL_SIZE 0x40
  54. enum tpu_pin_state {
  55. TPU_PIN_INACTIVE, /* Pin is driven inactive */
  56. TPU_PIN_PWM, /* Pin is driven by PWM */
  57. TPU_PIN_ACTIVE, /* Pin is driven active */
  58. };
  59. struct tpu_device;
  60. struct tpu_pwm_device {
  61. bool timer_on; /* Whether the timer is running */
  62. struct tpu_device *tpu;
  63. unsigned int channel; /* Channel number in the TPU */
  64. enum pwm_polarity polarity;
  65. unsigned int prescaler;
  66. u16 period;
  67. u16 duty;
  68. };
  69. struct tpu_device {
  70. struct platform_device *pdev;
  71. spinlock_t lock;
  72. void __iomem *base;
  73. struct clk *clk;
  74. struct tpu_pwm_device tpd[TPU_CHANNEL_MAX];
  75. };
  76. static inline struct tpu_device *to_tpu_device(struct pwm_chip *chip)
  77. {
  78. return pwmchip_get_drvdata(chip);
  79. }
  80. static void tpu_pwm_write(struct tpu_pwm_device *tpd, int reg_nr, u16 value)
  81. {
  82. void __iomem *base = tpd->tpu->base + TPU_CHANNEL_OFFSET
  83. + tpd->channel * TPU_CHANNEL_SIZE;
  84. iowrite16(value, base + reg_nr);
  85. }
  86. static void tpu_pwm_set_pin(struct tpu_pwm_device *tpd,
  87. enum tpu_pin_state state)
  88. {
  89. static const char * const states[] = { "inactive", "PWM", "active" };
  90. dev_dbg(&tpd->tpu->pdev->dev, "%u: configuring pin as %s\n",
  91. tpd->channel, states[state]);
  92. switch (state) {
  93. case TPU_PIN_INACTIVE:
  94. tpu_pwm_write(tpd, TPU_TIORn,
  95. tpd->polarity == PWM_POLARITY_INVERSED ?
  96. TPU_TIOR_IOA_1 : TPU_TIOR_IOA_0);
  97. break;
  98. case TPU_PIN_PWM:
  99. tpu_pwm_write(tpd, TPU_TIORn,
  100. tpd->polarity == PWM_POLARITY_INVERSED ?
  101. TPU_TIOR_IOA_0_SET : TPU_TIOR_IOA_1_CLR);
  102. break;
  103. case TPU_PIN_ACTIVE:
  104. tpu_pwm_write(tpd, TPU_TIORn,
  105. tpd->polarity == PWM_POLARITY_INVERSED ?
  106. TPU_TIOR_IOA_0 : TPU_TIOR_IOA_1);
  107. break;
  108. }
  109. }
  110. static void tpu_pwm_start_stop(struct tpu_pwm_device *tpd, int start)
  111. {
  112. unsigned long flags;
  113. u16 value;
  114. spin_lock_irqsave(&tpd->tpu->lock, flags);
  115. value = ioread16(tpd->tpu->base + TPU_TSTR);
  116. if (start)
  117. value |= 1 << tpd->channel;
  118. else
  119. value &= ~(1 << tpd->channel);
  120. iowrite16(value, tpd->tpu->base + TPU_TSTR);
  121. spin_unlock_irqrestore(&tpd->tpu->lock, flags);
  122. }
  123. static int tpu_pwm_timer_start(struct tpu_pwm_device *tpd)
  124. {
  125. int ret;
  126. if (!tpd->timer_on) {
  127. /* Wake up device and enable clock. */
  128. pm_runtime_get_sync(&tpd->tpu->pdev->dev);
  129. ret = clk_prepare_enable(tpd->tpu->clk);
  130. if (ret) {
  131. dev_err(&tpd->tpu->pdev->dev, "cannot enable clock\n");
  132. return ret;
  133. }
  134. tpd->timer_on = true;
  135. }
  136. /*
  137. * Make sure the channel is stopped, as we need to reconfigure it
  138. * completely. First drive the pin to the inactive state to avoid
  139. * glitches.
  140. */
  141. tpu_pwm_set_pin(tpd, TPU_PIN_INACTIVE);
  142. tpu_pwm_start_stop(tpd, false);
  143. /*
  144. * - Clear TCNT on TGRB match
  145. * - Count on rising edge
  146. * - Set prescaler
  147. * - Output 0 until TGRA, output 1 until TGRB (active low polarity)
  148. * - Output 1 until TGRA, output 0 until TGRB (active high polarity
  149. * - PWM mode
  150. */
  151. tpu_pwm_write(tpd, TPU_TCRn, TPU_TCR_CCLR_TGRB | TPU_TCR_CKEG_RISING |
  152. tpd->prescaler);
  153. tpu_pwm_write(tpd, TPU_TMDRn, TPU_TMDR_MD_PWM);
  154. tpu_pwm_set_pin(tpd, TPU_PIN_PWM);
  155. tpu_pwm_write(tpd, TPU_TGRAn, tpd->duty);
  156. tpu_pwm_write(tpd, TPU_TGRBn, tpd->period);
  157. dev_dbg(&tpd->tpu->pdev->dev, "%u: TGRA 0x%04x TGRB 0x%04x\n",
  158. tpd->channel, tpd->duty, tpd->period);
  159. /* Start the channel. */
  160. tpu_pwm_start_stop(tpd, true);
  161. return 0;
  162. }
  163. static void tpu_pwm_timer_stop(struct tpu_pwm_device *tpd)
  164. {
  165. if (!tpd->timer_on)
  166. return;
  167. /* Disable channel. */
  168. tpu_pwm_start_stop(tpd, false);
  169. /* Stop clock and mark device as idle. */
  170. clk_disable_unprepare(tpd->tpu->clk);
  171. pm_runtime_put(&tpd->tpu->pdev->dev);
  172. tpd->timer_on = false;
  173. }
  174. /* -----------------------------------------------------------------------------
  175. * PWM API
  176. */
  177. static int tpu_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm)
  178. {
  179. struct tpu_device *tpu = to_tpu_device(chip);
  180. struct tpu_pwm_device *tpd;
  181. if (pwm->hwpwm >= TPU_CHANNEL_MAX)
  182. return -EINVAL;
  183. tpd = &tpu->tpd[pwm->hwpwm];
  184. tpd->tpu = tpu;
  185. tpd->channel = pwm->hwpwm;
  186. tpd->polarity = PWM_POLARITY_NORMAL;
  187. tpd->prescaler = 0;
  188. tpd->period = 0;
  189. tpd->duty = 0;
  190. tpd->timer_on = false;
  191. return 0;
  192. }
  193. static void tpu_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm)
  194. {
  195. struct tpu_device *tpu = to_tpu_device(chip);
  196. struct tpu_pwm_device *tpd = &tpu->tpd[pwm->hwpwm];
  197. tpu_pwm_timer_stop(tpd);
  198. }
  199. static int tpu_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
  200. u64 duty_ns, u64 period_ns, bool enabled)
  201. {
  202. struct tpu_device *tpu = to_tpu_device(chip);
  203. struct tpu_pwm_device *tpd = &tpu->tpd[pwm->hwpwm];
  204. unsigned int prescaler;
  205. bool duty_only = false;
  206. u32 clk_rate;
  207. u64 period;
  208. u32 duty;
  209. int ret;
  210. clk_rate = clk_get_rate(tpu->clk);
  211. if (unlikely(clk_rate > NSEC_PER_SEC)) {
  212. /*
  213. * This won't happen in the nearer future, so this is only a
  214. * safeguard to prevent the following calculation from
  215. * overflowing. With this clk_rate * period_ns / NSEC_PER_SEC is
  216. * not greater than period_ns and so fits into an u64.
  217. */
  218. return -EINVAL;
  219. }
  220. period = mul_u64_u64_div_u64(clk_rate, period_ns, NSEC_PER_SEC);
  221. /*
  222. * Find the minimal prescaler in [0..3] such that
  223. *
  224. * period >> (2 * prescaler) < 0x10000
  225. *
  226. * This could be calculated using something like:
  227. *
  228. * prescaler = max(ilog2(period) / 2, 7) - 7;
  229. *
  230. * but given there are only four allowed results and that ilog2 isn't
  231. * cheap on all platforms using a switch statement is more effective.
  232. */
  233. switch (period) {
  234. case 1 ... 0xffff:
  235. prescaler = 0;
  236. break;
  237. case 0x10000 ... 0x3ffff:
  238. prescaler = 1;
  239. break;
  240. case 0x40000 ... 0xfffff:
  241. prescaler = 2;
  242. break;
  243. case 0x100000 ... 0x3fffff:
  244. prescaler = 3;
  245. break;
  246. default:
  247. return -EINVAL;
  248. }
  249. period >>= 2 * prescaler;
  250. if (duty_ns)
  251. duty = mul_u64_u64_div_u64(clk_rate, duty_ns,
  252. (u64)NSEC_PER_SEC << (2 * prescaler));
  253. else
  254. duty = 0;
  255. dev_dbg(&tpu->pdev->dev,
  256. "rate %u, prescaler %u, period %u, duty %u\n",
  257. clk_rate, 1 << (2 * prescaler), (u32)period, duty);
  258. if (tpd->prescaler == prescaler && tpd->period == period)
  259. duty_only = true;
  260. tpd->prescaler = prescaler;
  261. tpd->period = period;
  262. tpd->duty = duty;
  263. /* If the channel is disabled we're done. */
  264. if (!enabled)
  265. return 0;
  266. if (duty_only && tpd->timer_on) {
  267. /*
  268. * If only the duty cycle changed and the timer is already
  269. * running, there's no need to reconfigure it completely, Just
  270. * modify the duty cycle.
  271. */
  272. tpu_pwm_write(tpd, TPU_TGRAn, tpd->duty);
  273. dev_dbg(&tpu->pdev->dev, "%u: TGRA 0x%04x\n", tpd->channel,
  274. tpd->duty);
  275. } else {
  276. /* Otherwise perform a full reconfiguration. */
  277. ret = tpu_pwm_timer_start(tpd);
  278. if (ret < 0)
  279. return ret;
  280. }
  281. if (duty == 0 || duty == period) {
  282. /*
  283. * To avoid running the timer when not strictly required, handle
  284. * 0% and 100% duty cycles as fixed levels and stop the timer.
  285. */
  286. tpu_pwm_set_pin(tpd, duty ? TPU_PIN_ACTIVE : TPU_PIN_INACTIVE);
  287. tpu_pwm_timer_stop(tpd);
  288. }
  289. return 0;
  290. }
  291. static int tpu_pwm_set_polarity(struct pwm_chip *chip, struct pwm_device *pwm,
  292. enum pwm_polarity polarity)
  293. {
  294. struct tpu_device *tpu = to_tpu_device(chip);
  295. struct tpu_pwm_device *tpd = &tpu->tpd[pwm->hwpwm];
  296. tpd->polarity = polarity;
  297. return 0;
  298. }
  299. static int tpu_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
  300. {
  301. struct tpu_device *tpu = to_tpu_device(chip);
  302. struct tpu_pwm_device *tpd = &tpu->tpd[pwm->hwpwm];
  303. int ret;
  304. ret = tpu_pwm_timer_start(tpd);
  305. if (ret < 0)
  306. return ret;
  307. /*
  308. * To avoid running the timer when not strictly required, handle 0% and
  309. * 100% duty cycles as fixed levels and stop the timer.
  310. */
  311. if (tpd->duty == 0 || tpd->duty == tpd->period) {
  312. tpu_pwm_set_pin(tpd, tpd->duty ?
  313. TPU_PIN_ACTIVE : TPU_PIN_INACTIVE);
  314. tpu_pwm_timer_stop(tpd);
  315. }
  316. return 0;
  317. }
  318. static void tpu_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
  319. {
  320. struct tpu_device *tpu = to_tpu_device(chip);
  321. struct tpu_pwm_device *tpd = &tpu->tpd[pwm->hwpwm];
  322. /* The timer must be running to modify the pin output configuration. */
  323. tpu_pwm_timer_start(tpd);
  324. tpu_pwm_set_pin(tpd, TPU_PIN_INACTIVE);
  325. tpu_pwm_timer_stop(tpd);
  326. }
  327. static int tpu_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
  328. const struct pwm_state *state)
  329. {
  330. int err;
  331. bool enabled = pwm->state.enabled;
  332. if (state->polarity != pwm->state.polarity) {
  333. if (enabled) {
  334. tpu_pwm_disable(chip, pwm);
  335. enabled = false;
  336. }
  337. err = tpu_pwm_set_polarity(chip, pwm, state->polarity);
  338. if (err)
  339. return err;
  340. }
  341. if (!state->enabled) {
  342. if (enabled)
  343. tpu_pwm_disable(chip, pwm);
  344. return 0;
  345. }
  346. err = tpu_pwm_config(chip, pwm,
  347. state->duty_cycle, state->period, enabled);
  348. if (err)
  349. return err;
  350. if (!enabled)
  351. err = tpu_pwm_enable(chip, pwm);
  352. return err;
  353. }
  354. static const struct pwm_ops tpu_pwm_ops = {
  355. .request = tpu_pwm_request,
  356. .free = tpu_pwm_free,
  357. .apply = tpu_pwm_apply,
  358. };
  359. /* -----------------------------------------------------------------------------
  360. * Probe and remove
  361. */
  362. static int tpu_probe(struct platform_device *pdev)
  363. {
  364. struct pwm_chip *chip;
  365. struct tpu_device *tpu;
  366. int ret;
  367. chip = devm_pwmchip_alloc(&pdev->dev, TPU_CHANNEL_MAX, sizeof(*tpu));
  368. if (IS_ERR(chip))
  369. return PTR_ERR(chip);
  370. tpu = to_tpu_device(chip);
  371. spin_lock_init(&tpu->lock);
  372. tpu->pdev = pdev;
  373. /* Map memory, get clock and pin control. */
  374. tpu->base = devm_platform_ioremap_resource(pdev, 0);
  375. if (IS_ERR(tpu->base))
  376. return PTR_ERR(tpu->base);
  377. tpu->clk = devm_clk_get(&pdev->dev, NULL);
  378. if (IS_ERR(tpu->clk))
  379. return dev_err_probe(&pdev->dev, PTR_ERR(tpu->clk), "Failed to get clock\n");
  380. /* Initialize and register the device. */
  381. platform_set_drvdata(pdev, tpu);
  382. chip->ops = &tpu_pwm_ops;
  383. ret = devm_pm_runtime_enable(&pdev->dev);
  384. if (ret < 0)
  385. return dev_err_probe(&pdev->dev, ret, "Failed to enable runtime PM\n");
  386. ret = devm_pwmchip_add(&pdev->dev, chip);
  387. if (ret < 0)
  388. return dev_err_probe(&pdev->dev, ret, "Failed to register PWM chip\n");
  389. return 0;
  390. }
  391. #ifdef CONFIG_OF
  392. static const struct of_device_id tpu_of_table[] = {
  393. { .compatible = "renesas,tpu-r8a73a4", },
  394. { .compatible = "renesas,tpu-r8a7740", },
  395. { .compatible = "renesas,tpu-r8a7790", },
  396. { .compatible = "renesas,tpu", },
  397. { },
  398. };
  399. MODULE_DEVICE_TABLE(of, tpu_of_table);
  400. #endif
  401. static struct platform_driver tpu_driver = {
  402. .probe = tpu_probe,
  403. .driver = {
  404. .name = "renesas-tpu-pwm",
  405. .of_match_table = of_match_ptr(tpu_of_table),
  406. }
  407. };
  408. module_platform_driver(tpu_driver);
  409. MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
  410. MODULE_DESCRIPTION("Renesas TPU PWM Driver");
  411. MODULE_LICENSE("GPL v2");
  412. MODULE_ALIAS("platform:renesas-tpu-pwm");