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