18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * R-Mobile TPU PWM driver 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright (C) 2012 Renesas Solutions Corp. 68c2ecf20Sopenharmony_ci */ 78c2ecf20Sopenharmony_ci 88c2ecf20Sopenharmony_ci#include <linux/clk.h> 98c2ecf20Sopenharmony_ci#include <linux/err.h> 108c2ecf20Sopenharmony_ci#include <linux/io.h> 118c2ecf20Sopenharmony_ci#include <linux/init.h> 128c2ecf20Sopenharmony_ci#include <linux/ioport.h> 138c2ecf20Sopenharmony_ci#include <linux/module.h> 148c2ecf20Sopenharmony_ci#include <linux/mutex.h> 158c2ecf20Sopenharmony_ci#include <linux/of.h> 168c2ecf20Sopenharmony_ci#include <linux/platform_device.h> 178c2ecf20Sopenharmony_ci#include <linux/pm_runtime.h> 188c2ecf20Sopenharmony_ci#include <linux/pwm.h> 198c2ecf20Sopenharmony_ci#include <linux/slab.h> 208c2ecf20Sopenharmony_ci#include <linux/spinlock.h> 218c2ecf20Sopenharmony_ci 228c2ecf20Sopenharmony_ci#define TPU_CHANNEL_MAX 4 238c2ecf20Sopenharmony_ci 248c2ecf20Sopenharmony_ci#define TPU_TSTR 0x00 /* Timer start register (shared) */ 258c2ecf20Sopenharmony_ci 268c2ecf20Sopenharmony_ci#define TPU_TCRn 0x00 /* Timer control register */ 278c2ecf20Sopenharmony_ci#define TPU_TCR_CCLR_NONE (0 << 5) 288c2ecf20Sopenharmony_ci#define TPU_TCR_CCLR_TGRA (1 << 5) 298c2ecf20Sopenharmony_ci#define TPU_TCR_CCLR_TGRB (2 << 5) 308c2ecf20Sopenharmony_ci#define TPU_TCR_CCLR_TGRC (5 << 5) 318c2ecf20Sopenharmony_ci#define TPU_TCR_CCLR_TGRD (6 << 5) 328c2ecf20Sopenharmony_ci#define TPU_TCR_CKEG_RISING (0 << 3) 338c2ecf20Sopenharmony_ci#define TPU_TCR_CKEG_FALLING (1 << 3) 348c2ecf20Sopenharmony_ci#define TPU_TCR_CKEG_BOTH (2 << 3) 358c2ecf20Sopenharmony_ci#define TPU_TMDRn 0x04 /* Timer mode register */ 368c2ecf20Sopenharmony_ci#define TPU_TMDR_BFWT (1 << 6) 378c2ecf20Sopenharmony_ci#define TPU_TMDR_BFB (1 << 5) 388c2ecf20Sopenharmony_ci#define TPU_TMDR_BFA (1 << 4) 398c2ecf20Sopenharmony_ci#define TPU_TMDR_MD_NORMAL (0 << 0) 408c2ecf20Sopenharmony_ci#define TPU_TMDR_MD_PWM (2 << 0) 418c2ecf20Sopenharmony_ci#define TPU_TIORn 0x08 /* Timer I/O control register */ 428c2ecf20Sopenharmony_ci#define TPU_TIOR_IOA_0 (0 << 0) 438c2ecf20Sopenharmony_ci#define TPU_TIOR_IOA_0_CLR (1 << 0) 448c2ecf20Sopenharmony_ci#define TPU_TIOR_IOA_0_SET (2 << 0) 458c2ecf20Sopenharmony_ci#define TPU_TIOR_IOA_0_TOGGLE (3 << 0) 468c2ecf20Sopenharmony_ci#define TPU_TIOR_IOA_1 (4 << 0) 478c2ecf20Sopenharmony_ci#define TPU_TIOR_IOA_1_CLR (5 << 0) 488c2ecf20Sopenharmony_ci#define TPU_TIOR_IOA_1_SET (6 << 0) 498c2ecf20Sopenharmony_ci#define TPU_TIOR_IOA_1_TOGGLE (7 << 0) 508c2ecf20Sopenharmony_ci#define TPU_TIERn 0x0c /* Timer interrupt enable register */ 518c2ecf20Sopenharmony_ci#define TPU_TSRn 0x10 /* Timer status register */ 528c2ecf20Sopenharmony_ci#define TPU_TCNTn 0x14 /* Timer counter */ 538c2ecf20Sopenharmony_ci#define TPU_TGRAn 0x18 /* Timer general register A */ 548c2ecf20Sopenharmony_ci#define TPU_TGRBn 0x1c /* Timer general register B */ 558c2ecf20Sopenharmony_ci#define TPU_TGRCn 0x20 /* Timer general register C */ 568c2ecf20Sopenharmony_ci#define TPU_TGRDn 0x24 /* Timer general register D */ 578c2ecf20Sopenharmony_ci 588c2ecf20Sopenharmony_ci#define TPU_CHANNEL_OFFSET 0x10 598c2ecf20Sopenharmony_ci#define TPU_CHANNEL_SIZE 0x40 608c2ecf20Sopenharmony_ci 618c2ecf20Sopenharmony_cienum tpu_pin_state { 628c2ecf20Sopenharmony_ci TPU_PIN_INACTIVE, /* Pin is driven inactive */ 638c2ecf20Sopenharmony_ci TPU_PIN_PWM, /* Pin is driven by PWM */ 648c2ecf20Sopenharmony_ci TPU_PIN_ACTIVE, /* Pin is driven active */ 658c2ecf20Sopenharmony_ci}; 668c2ecf20Sopenharmony_ci 678c2ecf20Sopenharmony_cistruct tpu_device; 688c2ecf20Sopenharmony_ci 698c2ecf20Sopenharmony_cistruct tpu_pwm_device { 708c2ecf20Sopenharmony_ci bool timer_on; /* Whether the timer is running */ 718c2ecf20Sopenharmony_ci 728c2ecf20Sopenharmony_ci struct tpu_device *tpu; 738c2ecf20Sopenharmony_ci unsigned int channel; /* Channel number in the TPU */ 748c2ecf20Sopenharmony_ci 758c2ecf20Sopenharmony_ci enum pwm_polarity polarity; 768c2ecf20Sopenharmony_ci unsigned int prescaler; 778c2ecf20Sopenharmony_ci u16 period; 788c2ecf20Sopenharmony_ci u16 duty; 798c2ecf20Sopenharmony_ci}; 808c2ecf20Sopenharmony_ci 818c2ecf20Sopenharmony_cistruct tpu_device { 828c2ecf20Sopenharmony_ci struct platform_device *pdev; 838c2ecf20Sopenharmony_ci struct pwm_chip chip; 848c2ecf20Sopenharmony_ci spinlock_t lock; 858c2ecf20Sopenharmony_ci 868c2ecf20Sopenharmony_ci void __iomem *base; 878c2ecf20Sopenharmony_ci struct clk *clk; 888c2ecf20Sopenharmony_ci}; 898c2ecf20Sopenharmony_ci 908c2ecf20Sopenharmony_ci#define to_tpu_device(c) container_of(c, struct tpu_device, chip) 918c2ecf20Sopenharmony_ci 928c2ecf20Sopenharmony_cistatic void tpu_pwm_write(struct tpu_pwm_device *pwm, int reg_nr, u16 value) 938c2ecf20Sopenharmony_ci{ 948c2ecf20Sopenharmony_ci void __iomem *base = pwm->tpu->base + TPU_CHANNEL_OFFSET 958c2ecf20Sopenharmony_ci + pwm->channel * TPU_CHANNEL_SIZE; 968c2ecf20Sopenharmony_ci 978c2ecf20Sopenharmony_ci iowrite16(value, base + reg_nr); 988c2ecf20Sopenharmony_ci} 998c2ecf20Sopenharmony_ci 1008c2ecf20Sopenharmony_cistatic void tpu_pwm_set_pin(struct tpu_pwm_device *pwm, 1018c2ecf20Sopenharmony_ci enum tpu_pin_state state) 1028c2ecf20Sopenharmony_ci{ 1038c2ecf20Sopenharmony_ci static const char * const states[] = { "inactive", "PWM", "active" }; 1048c2ecf20Sopenharmony_ci 1058c2ecf20Sopenharmony_ci dev_dbg(&pwm->tpu->pdev->dev, "%u: configuring pin as %s\n", 1068c2ecf20Sopenharmony_ci pwm->channel, states[state]); 1078c2ecf20Sopenharmony_ci 1088c2ecf20Sopenharmony_ci switch (state) { 1098c2ecf20Sopenharmony_ci case TPU_PIN_INACTIVE: 1108c2ecf20Sopenharmony_ci tpu_pwm_write(pwm, TPU_TIORn, 1118c2ecf20Sopenharmony_ci pwm->polarity == PWM_POLARITY_INVERSED ? 1128c2ecf20Sopenharmony_ci TPU_TIOR_IOA_1 : TPU_TIOR_IOA_0); 1138c2ecf20Sopenharmony_ci break; 1148c2ecf20Sopenharmony_ci case TPU_PIN_PWM: 1158c2ecf20Sopenharmony_ci tpu_pwm_write(pwm, TPU_TIORn, 1168c2ecf20Sopenharmony_ci pwm->polarity == PWM_POLARITY_INVERSED ? 1178c2ecf20Sopenharmony_ci TPU_TIOR_IOA_0_SET : TPU_TIOR_IOA_1_CLR); 1188c2ecf20Sopenharmony_ci break; 1198c2ecf20Sopenharmony_ci case TPU_PIN_ACTIVE: 1208c2ecf20Sopenharmony_ci tpu_pwm_write(pwm, TPU_TIORn, 1218c2ecf20Sopenharmony_ci pwm->polarity == PWM_POLARITY_INVERSED ? 1228c2ecf20Sopenharmony_ci TPU_TIOR_IOA_0 : TPU_TIOR_IOA_1); 1238c2ecf20Sopenharmony_ci break; 1248c2ecf20Sopenharmony_ci } 1258c2ecf20Sopenharmony_ci} 1268c2ecf20Sopenharmony_ci 1278c2ecf20Sopenharmony_cistatic void tpu_pwm_start_stop(struct tpu_pwm_device *pwm, int start) 1288c2ecf20Sopenharmony_ci{ 1298c2ecf20Sopenharmony_ci unsigned long flags; 1308c2ecf20Sopenharmony_ci u16 value; 1318c2ecf20Sopenharmony_ci 1328c2ecf20Sopenharmony_ci spin_lock_irqsave(&pwm->tpu->lock, flags); 1338c2ecf20Sopenharmony_ci value = ioread16(pwm->tpu->base + TPU_TSTR); 1348c2ecf20Sopenharmony_ci 1358c2ecf20Sopenharmony_ci if (start) 1368c2ecf20Sopenharmony_ci value |= 1 << pwm->channel; 1378c2ecf20Sopenharmony_ci else 1388c2ecf20Sopenharmony_ci value &= ~(1 << pwm->channel); 1398c2ecf20Sopenharmony_ci 1408c2ecf20Sopenharmony_ci iowrite16(value, pwm->tpu->base + TPU_TSTR); 1418c2ecf20Sopenharmony_ci spin_unlock_irqrestore(&pwm->tpu->lock, flags); 1428c2ecf20Sopenharmony_ci} 1438c2ecf20Sopenharmony_ci 1448c2ecf20Sopenharmony_cistatic int tpu_pwm_timer_start(struct tpu_pwm_device *pwm) 1458c2ecf20Sopenharmony_ci{ 1468c2ecf20Sopenharmony_ci int ret; 1478c2ecf20Sopenharmony_ci 1488c2ecf20Sopenharmony_ci if (!pwm->timer_on) { 1498c2ecf20Sopenharmony_ci /* Wake up device and enable clock. */ 1508c2ecf20Sopenharmony_ci pm_runtime_get_sync(&pwm->tpu->pdev->dev); 1518c2ecf20Sopenharmony_ci ret = clk_prepare_enable(pwm->tpu->clk); 1528c2ecf20Sopenharmony_ci if (ret) { 1538c2ecf20Sopenharmony_ci dev_err(&pwm->tpu->pdev->dev, "cannot enable clock\n"); 1548c2ecf20Sopenharmony_ci return ret; 1558c2ecf20Sopenharmony_ci } 1568c2ecf20Sopenharmony_ci pwm->timer_on = true; 1578c2ecf20Sopenharmony_ci } 1588c2ecf20Sopenharmony_ci 1598c2ecf20Sopenharmony_ci /* 1608c2ecf20Sopenharmony_ci * Make sure the channel is stopped, as we need to reconfigure it 1618c2ecf20Sopenharmony_ci * completely. First drive the pin to the inactive state to avoid 1628c2ecf20Sopenharmony_ci * glitches. 1638c2ecf20Sopenharmony_ci */ 1648c2ecf20Sopenharmony_ci tpu_pwm_set_pin(pwm, TPU_PIN_INACTIVE); 1658c2ecf20Sopenharmony_ci tpu_pwm_start_stop(pwm, false); 1668c2ecf20Sopenharmony_ci 1678c2ecf20Sopenharmony_ci /* 1688c2ecf20Sopenharmony_ci * - Clear TCNT on TGRB match 1698c2ecf20Sopenharmony_ci * - Count on rising edge 1708c2ecf20Sopenharmony_ci * - Set prescaler 1718c2ecf20Sopenharmony_ci * - Output 0 until TGRA, output 1 until TGRB (active low polarity) 1728c2ecf20Sopenharmony_ci * - Output 1 until TGRA, output 0 until TGRB (active high polarity 1738c2ecf20Sopenharmony_ci * - PWM mode 1748c2ecf20Sopenharmony_ci */ 1758c2ecf20Sopenharmony_ci tpu_pwm_write(pwm, TPU_TCRn, TPU_TCR_CCLR_TGRB | TPU_TCR_CKEG_RISING | 1768c2ecf20Sopenharmony_ci pwm->prescaler); 1778c2ecf20Sopenharmony_ci tpu_pwm_write(pwm, TPU_TMDRn, TPU_TMDR_MD_PWM); 1788c2ecf20Sopenharmony_ci tpu_pwm_set_pin(pwm, TPU_PIN_PWM); 1798c2ecf20Sopenharmony_ci tpu_pwm_write(pwm, TPU_TGRAn, pwm->duty); 1808c2ecf20Sopenharmony_ci tpu_pwm_write(pwm, TPU_TGRBn, pwm->period); 1818c2ecf20Sopenharmony_ci 1828c2ecf20Sopenharmony_ci dev_dbg(&pwm->tpu->pdev->dev, "%u: TGRA 0x%04x TGRB 0x%04x\n", 1838c2ecf20Sopenharmony_ci pwm->channel, pwm->duty, pwm->period); 1848c2ecf20Sopenharmony_ci 1858c2ecf20Sopenharmony_ci /* Start the channel. */ 1868c2ecf20Sopenharmony_ci tpu_pwm_start_stop(pwm, true); 1878c2ecf20Sopenharmony_ci 1888c2ecf20Sopenharmony_ci return 0; 1898c2ecf20Sopenharmony_ci} 1908c2ecf20Sopenharmony_ci 1918c2ecf20Sopenharmony_cistatic void tpu_pwm_timer_stop(struct tpu_pwm_device *pwm) 1928c2ecf20Sopenharmony_ci{ 1938c2ecf20Sopenharmony_ci if (!pwm->timer_on) 1948c2ecf20Sopenharmony_ci return; 1958c2ecf20Sopenharmony_ci 1968c2ecf20Sopenharmony_ci /* Disable channel. */ 1978c2ecf20Sopenharmony_ci tpu_pwm_start_stop(pwm, false); 1988c2ecf20Sopenharmony_ci 1998c2ecf20Sopenharmony_ci /* Stop clock and mark device as idle. */ 2008c2ecf20Sopenharmony_ci clk_disable_unprepare(pwm->tpu->clk); 2018c2ecf20Sopenharmony_ci pm_runtime_put(&pwm->tpu->pdev->dev); 2028c2ecf20Sopenharmony_ci 2038c2ecf20Sopenharmony_ci pwm->timer_on = false; 2048c2ecf20Sopenharmony_ci} 2058c2ecf20Sopenharmony_ci 2068c2ecf20Sopenharmony_ci/* ----------------------------------------------------------------------------- 2078c2ecf20Sopenharmony_ci * PWM API 2088c2ecf20Sopenharmony_ci */ 2098c2ecf20Sopenharmony_ci 2108c2ecf20Sopenharmony_cistatic int tpu_pwm_request(struct pwm_chip *chip, struct pwm_device *_pwm) 2118c2ecf20Sopenharmony_ci{ 2128c2ecf20Sopenharmony_ci struct tpu_device *tpu = to_tpu_device(chip); 2138c2ecf20Sopenharmony_ci struct tpu_pwm_device *pwm; 2148c2ecf20Sopenharmony_ci 2158c2ecf20Sopenharmony_ci if (_pwm->hwpwm >= TPU_CHANNEL_MAX) 2168c2ecf20Sopenharmony_ci return -EINVAL; 2178c2ecf20Sopenharmony_ci 2188c2ecf20Sopenharmony_ci pwm = kzalloc(sizeof(*pwm), GFP_KERNEL); 2198c2ecf20Sopenharmony_ci if (pwm == NULL) 2208c2ecf20Sopenharmony_ci return -ENOMEM; 2218c2ecf20Sopenharmony_ci 2228c2ecf20Sopenharmony_ci pwm->tpu = tpu; 2238c2ecf20Sopenharmony_ci pwm->channel = _pwm->hwpwm; 2248c2ecf20Sopenharmony_ci pwm->polarity = PWM_POLARITY_NORMAL; 2258c2ecf20Sopenharmony_ci pwm->prescaler = 0; 2268c2ecf20Sopenharmony_ci pwm->period = 0; 2278c2ecf20Sopenharmony_ci pwm->duty = 0; 2288c2ecf20Sopenharmony_ci 2298c2ecf20Sopenharmony_ci pwm->timer_on = false; 2308c2ecf20Sopenharmony_ci 2318c2ecf20Sopenharmony_ci pwm_set_chip_data(_pwm, pwm); 2328c2ecf20Sopenharmony_ci 2338c2ecf20Sopenharmony_ci return 0; 2348c2ecf20Sopenharmony_ci} 2358c2ecf20Sopenharmony_ci 2368c2ecf20Sopenharmony_cistatic void tpu_pwm_free(struct pwm_chip *chip, struct pwm_device *_pwm) 2378c2ecf20Sopenharmony_ci{ 2388c2ecf20Sopenharmony_ci struct tpu_pwm_device *pwm = pwm_get_chip_data(_pwm); 2398c2ecf20Sopenharmony_ci 2408c2ecf20Sopenharmony_ci tpu_pwm_timer_stop(pwm); 2418c2ecf20Sopenharmony_ci kfree(pwm); 2428c2ecf20Sopenharmony_ci} 2438c2ecf20Sopenharmony_ci 2448c2ecf20Sopenharmony_cistatic int tpu_pwm_config(struct pwm_chip *chip, struct pwm_device *_pwm, 2458c2ecf20Sopenharmony_ci int duty_ns, int period_ns) 2468c2ecf20Sopenharmony_ci{ 2478c2ecf20Sopenharmony_ci static const unsigned int prescalers[] = { 1, 4, 16, 64 }; 2488c2ecf20Sopenharmony_ci struct tpu_pwm_device *pwm = pwm_get_chip_data(_pwm); 2498c2ecf20Sopenharmony_ci struct tpu_device *tpu = to_tpu_device(chip); 2508c2ecf20Sopenharmony_ci unsigned int prescaler; 2518c2ecf20Sopenharmony_ci bool duty_only = false; 2528c2ecf20Sopenharmony_ci u32 clk_rate; 2538c2ecf20Sopenharmony_ci u32 period; 2548c2ecf20Sopenharmony_ci u32 duty; 2558c2ecf20Sopenharmony_ci int ret; 2568c2ecf20Sopenharmony_ci 2578c2ecf20Sopenharmony_ci /* 2588c2ecf20Sopenharmony_ci * Pick a prescaler to avoid overflowing the counter. 2598c2ecf20Sopenharmony_ci * TODO: Pick the highest acceptable prescaler. 2608c2ecf20Sopenharmony_ci */ 2618c2ecf20Sopenharmony_ci clk_rate = clk_get_rate(tpu->clk); 2628c2ecf20Sopenharmony_ci 2638c2ecf20Sopenharmony_ci for (prescaler = 0; prescaler < ARRAY_SIZE(prescalers); ++prescaler) { 2648c2ecf20Sopenharmony_ci period = clk_rate / prescalers[prescaler] 2658c2ecf20Sopenharmony_ci / (NSEC_PER_SEC / period_ns); 2668c2ecf20Sopenharmony_ci if (period <= 0xffff) 2678c2ecf20Sopenharmony_ci break; 2688c2ecf20Sopenharmony_ci } 2698c2ecf20Sopenharmony_ci 2708c2ecf20Sopenharmony_ci if (prescaler == ARRAY_SIZE(prescalers) || period == 0) { 2718c2ecf20Sopenharmony_ci dev_err(&tpu->pdev->dev, "clock rate mismatch\n"); 2728c2ecf20Sopenharmony_ci return -ENOTSUPP; 2738c2ecf20Sopenharmony_ci } 2748c2ecf20Sopenharmony_ci 2758c2ecf20Sopenharmony_ci if (duty_ns) { 2768c2ecf20Sopenharmony_ci duty = clk_rate / prescalers[prescaler] 2778c2ecf20Sopenharmony_ci / (NSEC_PER_SEC / duty_ns); 2788c2ecf20Sopenharmony_ci if (duty > period) 2798c2ecf20Sopenharmony_ci return -EINVAL; 2808c2ecf20Sopenharmony_ci } else { 2818c2ecf20Sopenharmony_ci duty = 0; 2828c2ecf20Sopenharmony_ci } 2838c2ecf20Sopenharmony_ci 2848c2ecf20Sopenharmony_ci dev_dbg(&tpu->pdev->dev, 2858c2ecf20Sopenharmony_ci "rate %u, prescaler %u, period %u, duty %u\n", 2868c2ecf20Sopenharmony_ci clk_rate, prescalers[prescaler], period, duty); 2878c2ecf20Sopenharmony_ci 2888c2ecf20Sopenharmony_ci if (pwm->prescaler == prescaler && pwm->period == period) 2898c2ecf20Sopenharmony_ci duty_only = true; 2908c2ecf20Sopenharmony_ci 2918c2ecf20Sopenharmony_ci pwm->prescaler = prescaler; 2928c2ecf20Sopenharmony_ci pwm->period = period; 2938c2ecf20Sopenharmony_ci pwm->duty = duty; 2948c2ecf20Sopenharmony_ci 2958c2ecf20Sopenharmony_ci /* If the channel is disabled we're done. */ 2968c2ecf20Sopenharmony_ci if (!pwm_is_enabled(_pwm)) 2978c2ecf20Sopenharmony_ci return 0; 2988c2ecf20Sopenharmony_ci 2998c2ecf20Sopenharmony_ci if (duty_only && pwm->timer_on) { 3008c2ecf20Sopenharmony_ci /* 3018c2ecf20Sopenharmony_ci * If only the duty cycle changed and the timer is already 3028c2ecf20Sopenharmony_ci * running, there's no need to reconfigure it completely, Just 3038c2ecf20Sopenharmony_ci * modify the duty cycle. 3048c2ecf20Sopenharmony_ci */ 3058c2ecf20Sopenharmony_ci tpu_pwm_write(pwm, TPU_TGRAn, pwm->duty); 3068c2ecf20Sopenharmony_ci dev_dbg(&tpu->pdev->dev, "%u: TGRA 0x%04x\n", pwm->channel, 3078c2ecf20Sopenharmony_ci pwm->duty); 3088c2ecf20Sopenharmony_ci } else { 3098c2ecf20Sopenharmony_ci /* Otherwise perform a full reconfiguration. */ 3108c2ecf20Sopenharmony_ci ret = tpu_pwm_timer_start(pwm); 3118c2ecf20Sopenharmony_ci if (ret < 0) 3128c2ecf20Sopenharmony_ci return ret; 3138c2ecf20Sopenharmony_ci } 3148c2ecf20Sopenharmony_ci 3158c2ecf20Sopenharmony_ci if (duty == 0 || duty == period) { 3168c2ecf20Sopenharmony_ci /* 3178c2ecf20Sopenharmony_ci * To avoid running the timer when not strictly required, handle 3188c2ecf20Sopenharmony_ci * 0% and 100% duty cycles as fixed levels and stop the timer. 3198c2ecf20Sopenharmony_ci */ 3208c2ecf20Sopenharmony_ci tpu_pwm_set_pin(pwm, duty ? TPU_PIN_ACTIVE : TPU_PIN_INACTIVE); 3218c2ecf20Sopenharmony_ci tpu_pwm_timer_stop(pwm); 3228c2ecf20Sopenharmony_ci } 3238c2ecf20Sopenharmony_ci 3248c2ecf20Sopenharmony_ci return 0; 3258c2ecf20Sopenharmony_ci} 3268c2ecf20Sopenharmony_ci 3278c2ecf20Sopenharmony_cistatic int tpu_pwm_set_polarity(struct pwm_chip *chip, struct pwm_device *_pwm, 3288c2ecf20Sopenharmony_ci enum pwm_polarity polarity) 3298c2ecf20Sopenharmony_ci{ 3308c2ecf20Sopenharmony_ci struct tpu_pwm_device *pwm = pwm_get_chip_data(_pwm); 3318c2ecf20Sopenharmony_ci 3328c2ecf20Sopenharmony_ci pwm->polarity = polarity; 3338c2ecf20Sopenharmony_ci 3348c2ecf20Sopenharmony_ci return 0; 3358c2ecf20Sopenharmony_ci} 3368c2ecf20Sopenharmony_ci 3378c2ecf20Sopenharmony_cistatic int tpu_pwm_enable(struct pwm_chip *chip, struct pwm_device *_pwm) 3388c2ecf20Sopenharmony_ci{ 3398c2ecf20Sopenharmony_ci struct tpu_pwm_device *pwm = pwm_get_chip_data(_pwm); 3408c2ecf20Sopenharmony_ci int ret; 3418c2ecf20Sopenharmony_ci 3428c2ecf20Sopenharmony_ci ret = tpu_pwm_timer_start(pwm); 3438c2ecf20Sopenharmony_ci if (ret < 0) 3448c2ecf20Sopenharmony_ci return ret; 3458c2ecf20Sopenharmony_ci 3468c2ecf20Sopenharmony_ci /* 3478c2ecf20Sopenharmony_ci * To avoid running the timer when not strictly required, handle 0% and 3488c2ecf20Sopenharmony_ci * 100% duty cycles as fixed levels and stop the timer. 3498c2ecf20Sopenharmony_ci */ 3508c2ecf20Sopenharmony_ci if (pwm->duty == 0 || pwm->duty == pwm->period) { 3518c2ecf20Sopenharmony_ci tpu_pwm_set_pin(pwm, pwm->duty ? 3528c2ecf20Sopenharmony_ci TPU_PIN_ACTIVE : TPU_PIN_INACTIVE); 3538c2ecf20Sopenharmony_ci tpu_pwm_timer_stop(pwm); 3548c2ecf20Sopenharmony_ci } 3558c2ecf20Sopenharmony_ci 3568c2ecf20Sopenharmony_ci return 0; 3578c2ecf20Sopenharmony_ci} 3588c2ecf20Sopenharmony_ci 3598c2ecf20Sopenharmony_cistatic void tpu_pwm_disable(struct pwm_chip *chip, struct pwm_device *_pwm) 3608c2ecf20Sopenharmony_ci{ 3618c2ecf20Sopenharmony_ci struct tpu_pwm_device *pwm = pwm_get_chip_data(_pwm); 3628c2ecf20Sopenharmony_ci 3638c2ecf20Sopenharmony_ci /* The timer must be running to modify the pin output configuration. */ 3648c2ecf20Sopenharmony_ci tpu_pwm_timer_start(pwm); 3658c2ecf20Sopenharmony_ci tpu_pwm_set_pin(pwm, TPU_PIN_INACTIVE); 3668c2ecf20Sopenharmony_ci tpu_pwm_timer_stop(pwm); 3678c2ecf20Sopenharmony_ci} 3688c2ecf20Sopenharmony_ci 3698c2ecf20Sopenharmony_cistatic const struct pwm_ops tpu_pwm_ops = { 3708c2ecf20Sopenharmony_ci .request = tpu_pwm_request, 3718c2ecf20Sopenharmony_ci .free = tpu_pwm_free, 3728c2ecf20Sopenharmony_ci .config = tpu_pwm_config, 3738c2ecf20Sopenharmony_ci .set_polarity = tpu_pwm_set_polarity, 3748c2ecf20Sopenharmony_ci .enable = tpu_pwm_enable, 3758c2ecf20Sopenharmony_ci .disable = tpu_pwm_disable, 3768c2ecf20Sopenharmony_ci .owner = THIS_MODULE, 3778c2ecf20Sopenharmony_ci}; 3788c2ecf20Sopenharmony_ci 3798c2ecf20Sopenharmony_ci/* ----------------------------------------------------------------------------- 3808c2ecf20Sopenharmony_ci * Probe and remove 3818c2ecf20Sopenharmony_ci */ 3828c2ecf20Sopenharmony_ci 3838c2ecf20Sopenharmony_cistatic int tpu_probe(struct platform_device *pdev) 3848c2ecf20Sopenharmony_ci{ 3858c2ecf20Sopenharmony_ci struct tpu_device *tpu; 3868c2ecf20Sopenharmony_ci struct resource *res; 3878c2ecf20Sopenharmony_ci int ret; 3888c2ecf20Sopenharmony_ci 3898c2ecf20Sopenharmony_ci tpu = devm_kzalloc(&pdev->dev, sizeof(*tpu), GFP_KERNEL); 3908c2ecf20Sopenharmony_ci if (tpu == NULL) 3918c2ecf20Sopenharmony_ci return -ENOMEM; 3928c2ecf20Sopenharmony_ci 3938c2ecf20Sopenharmony_ci spin_lock_init(&tpu->lock); 3948c2ecf20Sopenharmony_ci tpu->pdev = pdev; 3958c2ecf20Sopenharmony_ci 3968c2ecf20Sopenharmony_ci /* Map memory, get clock and pin control. */ 3978c2ecf20Sopenharmony_ci res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 3988c2ecf20Sopenharmony_ci tpu->base = devm_ioremap_resource(&pdev->dev, res); 3998c2ecf20Sopenharmony_ci if (IS_ERR(tpu->base)) 4008c2ecf20Sopenharmony_ci return PTR_ERR(tpu->base); 4018c2ecf20Sopenharmony_ci 4028c2ecf20Sopenharmony_ci tpu->clk = devm_clk_get(&pdev->dev, NULL); 4038c2ecf20Sopenharmony_ci if (IS_ERR(tpu->clk)) { 4048c2ecf20Sopenharmony_ci dev_err(&pdev->dev, "cannot get clock\n"); 4058c2ecf20Sopenharmony_ci return PTR_ERR(tpu->clk); 4068c2ecf20Sopenharmony_ci } 4078c2ecf20Sopenharmony_ci 4088c2ecf20Sopenharmony_ci /* Initialize and register the device. */ 4098c2ecf20Sopenharmony_ci platform_set_drvdata(pdev, tpu); 4108c2ecf20Sopenharmony_ci 4118c2ecf20Sopenharmony_ci tpu->chip.dev = &pdev->dev; 4128c2ecf20Sopenharmony_ci tpu->chip.ops = &tpu_pwm_ops; 4138c2ecf20Sopenharmony_ci tpu->chip.of_xlate = of_pwm_xlate_with_flags; 4148c2ecf20Sopenharmony_ci tpu->chip.of_pwm_n_cells = 3; 4158c2ecf20Sopenharmony_ci tpu->chip.base = -1; 4168c2ecf20Sopenharmony_ci tpu->chip.npwm = TPU_CHANNEL_MAX; 4178c2ecf20Sopenharmony_ci 4188c2ecf20Sopenharmony_ci pm_runtime_enable(&pdev->dev); 4198c2ecf20Sopenharmony_ci 4208c2ecf20Sopenharmony_ci ret = pwmchip_add(&tpu->chip); 4218c2ecf20Sopenharmony_ci if (ret < 0) { 4228c2ecf20Sopenharmony_ci dev_err(&pdev->dev, "failed to register PWM chip\n"); 4238c2ecf20Sopenharmony_ci pm_runtime_disable(&pdev->dev); 4248c2ecf20Sopenharmony_ci return ret; 4258c2ecf20Sopenharmony_ci } 4268c2ecf20Sopenharmony_ci 4278c2ecf20Sopenharmony_ci return 0; 4288c2ecf20Sopenharmony_ci} 4298c2ecf20Sopenharmony_ci 4308c2ecf20Sopenharmony_cistatic int tpu_remove(struct platform_device *pdev) 4318c2ecf20Sopenharmony_ci{ 4328c2ecf20Sopenharmony_ci struct tpu_device *tpu = platform_get_drvdata(pdev); 4338c2ecf20Sopenharmony_ci int ret; 4348c2ecf20Sopenharmony_ci 4358c2ecf20Sopenharmony_ci ret = pwmchip_remove(&tpu->chip); 4368c2ecf20Sopenharmony_ci 4378c2ecf20Sopenharmony_ci pm_runtime_disable(&pdev->dev); 4388c2ecf20Sopenharmony_ci 4398c2ecf20Sopenharmony_ci return ret; 4408c2ecf20Sopenharmony_ci} 4418c2ecf20Sopenharmony_ci 4428c2ecf20Sopenharmony_ci#ifdef CONFIG_OF 4438c2ecf20Sopenharmony_cistatic const struct of_device_id tpu_of_table[] = { 4448c2ecf20Sopenharmony_ci { .compatible = "renesas,tpu-r8a73a4", }, 4458c2ecf20Sopenharmony_ci { .compatible = "renesas,tpu-r8a7740", }, 4468c2ecf20Sopenharmony_ci { .compatible = "renesas,tpu-r8a7790", }, 4478c2ecf20Sopenharmony_ci { .compatible = "renesas,tpu", }, 4488c2ecf20Sopenharmony_ci { }, 4498c2ecf20Sopenharmony_ci}; 4508c2ecf20Sopenharmony_ci 4518c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, tpu_of_table); 4528c2ecf20Sopenharmony_ci#endif 4538c2ecf20Sopenharmony_ci 4548c2ecf20Sopenharmony_cistatic struct platform_driver tpu_driver = { 4558c2ecf20Sopenharmony_ci .probe = tpu_probe, 4568c2ecf20Sopenharmony_ci .remove = tpu_remove, 4578c2ecf20Sopenharmony_ci .driver = { 4588c2ecf20Sopenharmony_ci .name = "renesas-tpu-pwm", 4598c2ecf20Sopenharmony_ci .of_match_table = of_match_ptr(tpu_of_table), 4608c2ecf20Sopenharmony_ci } 4618c2ecf20Sopenharmony_ci}; 4628c2ecf20Sopenharmony_ci 4638c2ecf20Sopenharmony_cimodule_platform_driver(tpu_driver); 4648c2ecf20Sopenharmony_ci 4658c2ecf20Sopenharmony_ciMODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>"); 4668c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Renesas TPU PWM Driver"); 4678c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2"); 4688c2ecf20Sopenharmony_ciMODULE_ALIAS("platform:renesas-tpu-pwm"); 469