pwm-renesas-tpu.c 12 KB

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