xref: /openbmc/linux/drivers/pwm/pwm-tegra.c (revision c900529f3d9161bfde5cca0754f83b4d3c3e0220)
116216333SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
20134b932SThierry Reding /*
30134b932SThierry Reding  * drivers/pwm/pwm-tegra.c
40134b932SThierry Reding  *
50134b932SThierry Reding  * Tegra pulse-width-modulation controller driver
60134b932SThierry Reding  *
71d7796bdSSandipan Patra  * Copyright (c) 2010-2020, NVIDIA Corporation.
80134b932SThierry Reding  * Based on arch/arm/plat-mxc/pwm.c by Sascha Hauer <s.hauer@pengutronix.de>
91d7796bdSSandipan Patra  *
101d7796bdSSandipan Patra  * Overview of Tegra Pulse Width Modulator Register:
111d7796bdSSandipan Patra  * 1. 13-bit: Frequency division (SCALE)
121d7796bdSSandipan Patra  * 2. 8-bit : Pulse division (DUTY)
131d7796bdSSandipan Patra  * 3. 1-bit : Enable bit
141d7796bdSSandipan Patra  *
151d7796bdSSandipan Patra  * The PWM clock frequency is divided by 256 before subdividing it based
161d7796bdSSandipan Patra  * on the programmable frequency division value to generate the required
171d7796bdSSandipan Patra  * frequency for PWM output. The maximum output frequency that can be
181d7796bdSSandipan Patra  * achieved is (max rate of source clock) / 256.
191d7796bdSSandipan Patra  * e.g. if source clock rate is 408 MHz, maximum output frequency can be:
201d7796bdSSandipan Patra  * 408 MHz/256 = 1.6 MHz.
211d7796bdSSandipan Patra  * This 1.6 MHz frequency can further be divided using SCALE value in PWM.
221d7796bdSSandipan Patra  *
231d7796bdSSandipan Patra  * PWM pulse width: 8 bits are usable [23:16] for varying pulse width.
241d7796bdSSandipan Patra  * To achieve 100% duty cycle, program Bit [24] of this register to
251d7796bdSSandipan Patra  * 1’b1. In which case the other bits [23:16] are set to don't care.
261d7796bdSSandipan Patra  *
271d7796bdSSandipan Patra  * Limitations:
281d7796bdSSandipan Patra  * -	When PWM is disabled, the output is driven to inactive.
291d7796bdSSandipan Patra  * -	It does not allow the current PWM period to complete and
301d7796bdSSandipan Patra  *	stops abruptly.
311d7796bdSSandipan Patra  *
321d7796bdSSandipan Patra  * -	If the register is reconfigured while PWM is running,
331d7796bdSSandipan Patra  *	it does not complete the currently running period.
341d7796bdSSandipan Patra  *
351d7796bdSSandipan Patra  * -	If the user input duty is beyond acceptible limits,
361d7796bdSSandipan Patra  *	-EINVAL is returned.
370134b932SThierry Reding  */
380134b932SThierry Reding 
390134b932SThierry Reding #include <linux/clk.h>
400134b932SThierry Reding #include <linux/err.h>
410134b932SThierry Reding #include <linux/io.h>
420134b932SThierry Reding #include <linux/module.h>
430134b932SThierry Reding #include <linux/of.h>
443da9b0feSDmitry Osipenko #include <linux/pm_opp.h>
450134b932SThierry Reding #include <linux/pwm.h>
460134b932SThierry Reding #include <linux/platform_device.h>
474a813b26SLaxman Dewangan #include <linux/pinctrl/consumer.h>
483da9b0feSDmitry Osipenko #include <linux/pm_runtime.h>
490134b932SThierry Reding #include <linux/slab.h>
505dfbd2bdSRohith Seelaboyina #include <linux/reset.h>
510134b932SThierry Reding 
523da9b0feSDmitry Osipenko #include <soc/tegra/common.h>
533da9b0feSDmitry Osipenko 
540134b932SThierry Reding #define PWM_ENABLE	(1 << 31)
550134b932SThierry Reding #define PWM_DUTY_WIDTH	8
560134b932SThierry Reding #define PWM_DUTY_SHIFT	16
570134b932SThierry Reding #define PWM_SCALE_WIDTH	13
580134b932SThierry Reding #define PWM_SCALE_SHIFT	0
590134b932SThierry Reding 
60e9be88a2SLaxman Dewangan struct tegra_pwm_soc {
61e9be88a2SLaxman Dewangan 	unsigned int num_channels;
620527eb37SLaxman Dewangan 
630527eb37SLaxman Dewangan 	/* Maximum IP frequency for given SoCs */
640527eb37SLaxman Dewangan 	unsigned long max_frequency;
65e9be88a2SLaxman Dewangan };
66e9be88a2SLaxman Dewangan 
670134b932SThierry Reding struct tegra_pwm_chip {
680134b932SThierry Reding 	struct pwm_chip chip;
690134b932SThierry Reding 	struct device *dev;
700134b932SThierry Reding 
710134b932SThierry Reding 	struct clk *clk;
725dfbd2bdSRohith Seelaboyina 	struct reset_control*rst;
730134b932SThierry Reding 
7446fa8bc0SLaxman Dewangan 	unsigned long clk_rate;
751d7796bdSSandipan Patra 	unsigned long min_period_ns;
7646fa8bc0SLaxman Dewangan 
774f57f5a0SThierry Reding 	void __iomem *regs;
78e9be88a2SLaxman Dewangan 
79e9be88a2SLaxman Dewangan 	const struct tegra_pwm_soc *soc;
800134b932SThierry Reding };
810134b932SThierry Reding 
to_tegra_pwm_chip(struct pwm_chip * chip)820134b932SThierry Reding static inline struct tegra_pwm_chip *to_tegra_pwm_chip(struct pwm_chip *chip)
830134b932SThierry Reding {
840134b932SThierry Reding 	return container_of(chip, struct tegra_pwm_chip, chip);
850134b932SThierry Reding }
860134b932SThierry Reding 
pwm_readl(struct tegra_pwm_chip * pc,unsigned int offset)87f19460c1SUwe Kleine-König static inline u32 pwm_readl(struct tegra_pwm_chip *pc, unsigned int offset)
880134b932SThierry Reding {
89f19460c1SUwe Kleine-König 	return readl(pc->regs + (offset << 4));
900134b932SThierry Reding }
910134b932SThierry Reding 
pwm_writel(struct tegra_pwm_chip * pc,unsigned int offset,u32 value)92f19460c1SUwe Kleine-König static inline void pwm_writel(struct tegra_pwm_chip *pc, unsigned int offset, u32 value)
930134b932SThierry Reding {
94f19460c1SUwe Kleine-König 	writel(value, pc->regs + (offset << 4));
950134b932SThierry Reding }
960134b932SThierry Reding 
tegra_pwm_config(struct pwm_chip * chip,struct pwm_device * pwm,int duty_ns,int period_ns)970134b932SThierry Reding static int tegra_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
980134b932SThierry Reding 			    int duty_ns, int period_ns)
990134b932SThierry Reding {
1000134b932SThierry Reding 	struct tegra_pwm_chip *pc = to_tegra_pwm_chip(chip);
1018c193f47SUwe Kleine-König 	unsigned long long c = duty_ns;
1021d7796bdSSandipan Patra 	unsigned long rate, required_clk_rate;
1030134b932SThierry Reding 	u32 val = 0;
1040134b932SThierry Reding 	int err;
1050134b932SThierry Reding 
1060134b932SThierry Reding 	/*
1070134b932SThierry Reding 	 * Convert from duty_ns / period_ns to a fixed number of duty ticks
1080134b932SThierry Reding 	 * per (1 << PWM_DUTY_WIDTH) cycles and make sure to round to the
1090134b932SThierry Reding 	 * nearest integer during division.
1100134b932SThierry Reding 	 */
111b979ed53SHyong Bin Kim 	c *= (1 << PWM_DUTY_WIDTH);
11290241fb9SLaxman Dewangan 	c = DIV_ROUND_CLOSEST_ULL(c, period_ns);
1130134b932SThierry Reding 
1140134b932SThierry Reding 	val = (u32)c << PWM_DUTY_SHIFT;
1150134b932SThierry Reding 
1160134b932SThierry Reding 	/*
1171d7796bdSSandipan Patra 	 *  min period = max clock limit >> PWM_DUTY_WIDTH
1181d7796bdSSandipan Patra 	 */
1191d7796bdSSandipan Patra 	if (period_ns < pc->min_period_ns)
1201d7796bdSSandipan Patra 		return -EINVAL;
1211d7796bdSSandipan Patra 
1221d7796bdSSandipan Patra 	/*
1230134b932SThierry Reding 	 * Compute the prescaler value for which (1 << PWM_DUTY_WIDTH)
1240134b932SThierry Reding 	 * cycles at the PWM clock rate will take period_ns nanoseconds.
1251d7796bdSSandipan Patra 	 *
1261d7796bdSSandipan Patra 	 * num_channels: If single instance of PWM controller has multiple
1271d7796bdSSandipan Patra 	 * channels (e.g. Tegra210 or older) then it is not possible to
1281d7796bdSSandipan Patra 	 * configure separate clock rates to each of the channels, in such
1291d7796bdSSandipan Patra 	 * case the value stored during probe will be referred.
1301d7796bdSSandipan Patra 	 *
1311d7796bdSSandipan Patra 	 * If every PWM controller instance has one channel respectively, i.e.
1321d7796bdSSandipan Patra 	 * nums_channels == 1 then only the clock rate can be modified
1331d7796bdSSandipan Patra 	 * dynamically (e.g. Tegra186 or Tegra194).
1340134b932SThierry Reding 	 */
1351d7796bdSSandipan Patra 	if (pc->soc->num_channels == 1) {
1361d7796bdSSandipan Patra 		/*
1371d7796bdSSandipan Patra 		 * Rate is multiplied with 2^PWM_DUTY_WIDTH so that it matches
1381d7796bdSSandipan Patra 		 * with the maximum possible rate that the controller can
1391d7796bdSSandipan Patra 		 * provide. Any further lower value can be derived by setting
1401d7796bdSSandipan Patra 		 * PFM bits[0:12].
1411d7796bdSSandipan Patra 		 *
1421d7796bdSSandipan Patra 		 * required_clk_rate is a reference rate for source clock and
1431d7796bdSSandipan Patra 		 * it is derived based on user requested period. By setting the
1441d7796bdSSandipan Patra 		 * source clock rate as required_clk_rate, PWM controller will
1451d7796bdSSandipan Patra 		 * be able to configure the requested period.
1461d7796bdSSandipan Patra 		 */
147dd1f1da4SSteven Price 		required_clk_rate = DIV_ROUND_UP_ULL((u64)NSEC_PER_SEC << PWM_DUTY_WIDTH,
148f2719461SJon Hunter 						     period_ns);
1491d7796bdSSandipan Patra 
1505eccd0d9SJon Hunter 		if (required_clk_rate > clk_round_rate(pc->clk, required_clk_rate))
1515eccd0d9SJon Hunter 			/*
1525eccd0d9SJon Hunter 			 * required_clk_rate is a lower bound for the input
1535eccd0d9SJon Hunter 			 * rate; for lower rates there is no value for PWM_SCALE
1545eccd0d9SJon Hunter 			 * that yields a period less than or equal to the
1555eccd0d9SJon Hunter 			 * requested period. Hence, for lower rates, double the
1565eccd0d9SJon Hunter 			 * required_clk_rate to get a clock rate that can meet
1575eccd0d9SJon Hunter 			 * the requested period.
1585eccd0d9SJon Hunter 			 */
1595eccd0d9SJon Hunter 			required_clk_rate *= 2;
1605eccd0d9SJon Hunter 
1613da9b0feSDmitry Osipenko 		err = dev_pm_opp_set_rate(pc->dev, required_clk_rate);
1621d7796bdSSandipan Patra 		if (err < 0)
1631d7796bdSSandipan Patra 			return -EINVAL;
1641d7796bdSSandipan Patra 
1651d7796bdSSandipan Patra 		/* Store the new rate for further references */
1661d7796bdSSandipan Patra 		pc->clk_rate = clk_get_rate(pc->clk);
1671d7796bdSSandipan Patra 	}
1681d7796bdSSandipan Patra 
169250b76f4SLaxman Dewangan 	/* Consider precision in PWM_SCALE_WIDTH rate calculation */
1708c193f47SUwe Kleine-König 	rate = mul_u64_u64_div_u64(pc->clk_rate, period_ns,
1718c193f47SUwe Kleine-König 				   (u64)NSEC_PER_SEC << PWM_DUTY_WIDTH);
1720134b932SThierry Reding 
1730134b932SThierry Reding 	/*
1740134b932SThierry Reding 	 * Since the actual PWM divider is the register's frequency divider
1751d7796bdSSandipan Patra 	 * field plus 1, we need to decrement to get the correct value to
1760134b932SThierry Reding 	 * write to the register.
1770134b932SThierry Reding 	 */
1780134b932SThierry Reding 	if (rate > 0)
1790134b932SThierry Reding 		rate--;
1808c193f47SUwe Kleine-König 	else
1818c193f47SUwe Kleine-König 		return -EINVAL;
1820134b932SThierry Reding 
1830134b932SThierry Reding 	/*
1840134b932SThierry Reding 	 * Make sure that the rate will fit in the register's frequency
1850134b932SThierry Reding 	 * divider field.
1860134b932SThierry Reding 	 */
1870134b932SThierry Reding 	if (rate >> PWM_SCALE_WIDTH)
1880134b932SThierry Reding 		return -EINVAL;
1890134b932SThierry Reding 
1900134b932SThierry Reding 	val |= rate << PWM_SCALE_SHIFT;
1910134b932SThierry Reding 
1920134b932SThierry Reding 	/*
1930134b932SThierry Reding 	 * If the PWM channel is disabled, make sure to turn on the clock
1940134b932SThierry Reding 	 * before writing the register. Otherwise, keep it enabled.
1950134b932SThierry Reding 	 */
1965c31252cSBoris Brezillon 	if (!pwm_is_enabled(pwm)) {
1973da9b0feSDmitry Osipenko 		err = pm_runtime_resume_and_get(pc->dev);
1983da9b0feSDmitry Osipenko 		if (err)
1990134b932SThierry Reding 			return err;
2000134b932SThierry Reding 	} else
2010134b932SThierry Reding 		val |= PWM_ENABLE;
2020134b932SThierry Reding 
2030134b932SThierry Reding 	pwm_writel(pc, pwm->hwpwm, val);
2040134b932SThierry Reding 
2050134b932SThierry Reding 	/*
2060134b932SThierry Reding 	 * If the PWM is not enabled, turn the clock off again to save power.
2070134b932SThierry Reding 	 */
2085c31252cSBoris Brezillon 	if (!pwm_is_enabled(pwm))
2093da9b0feSDmitry Osipenko 		pm_runtime_put(pc->dev);
2100134b932SThierry Reding 
2110134b932SThierry Reding 	return 0;
2120134b932SThierry Reding }
2130134b932SThierry Reding 
tegra_pwm_enable(struct pwm_chip * chip,struct pwm_device * pwm)2140134b932SThierry Reding static int tegra_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
2150134b932SThierry Reding {
2160134b932SThierry Reding 	struct tegra_pwm_chip *pc = to_tegra_pwm_chip(chip);
2170134b932SThierry Reding 	int rc = 0;
2180134b932SThierry Reding 	u32 val;
2190134b932SThierry Reding 
2203da9b0feSDmitry Osipenko 	rc = pm_runtime_resume_and_get(pc->dev);
2213da9b0feSDmitry Osipenko 	if (rc)
2220134b932SThierry Reding 		return rc;
2230134b932SThierry Reding 
2240134b932SThierry Reding 	val = pwm_readl(pc, pwm->hwpwm);
2250134b932SThierry Reding 	val |= PWM_ENABLE;
2260134b932SThierry Reding 	pwm_writel(pc, pwm->hwpwm, val);
2270134b932SThierry Reding 
2280134b932SThierry Reding 	return 0;
2290134b932SThierry Reding }
2300134b932SThierry Reding 
tegra_pwm_disable(struct pwm_chip * chip,struct pwm_device * pwm)2310134b932SThierry Reding static void tegra_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
2320134b932SThierry Reding {
2330134b932SThierry Reding 	struct tegra_pwm_chip *pc = to_tegra_pwm_chip(chip);
2340134b932SThierry Reding 	u32 val;
2350134b932SThierry Reding 
2360134b932SThierry Reding 	val = pwm_readl(pc, pwm->hwpwm);
2370134b932SThierry Reding 	val &= ~PWM_ENABLE;
2380134b932SThierry Reding 	pwm_writel(pc, pwm->hwpwm, val);
2390134b932SThierry Reding 
2403da9b0feSDmitry Osipenko 	pm_runtime_put_sync(pc->dev);
2410134b932SThierry Reding }
2420134b932SThierry Reding 
tegra_pwm_apply(struct pwm_chip * chip,struct pwm_device * pwm,const struct pwm_state * state)243fd3ddd43SUwe Kleine-König static int tegra_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
244fd3ddd43SUwe Kleine-König 			   const struct pwm_state *state)
245fd3ddd43SUwe Kleine-König {
246fd3ddd43SUwe Kleine-König 	int err;
247fd3ddd43SUwe Kleine-König 	bool enabled = pwm->state.enabled;
248fd3ddd43SUwe Kleine-König 
249fd3ddd43SUwe Kleine-König 	if (state->polarity != PWM_POLARITY_NORMAL)
250fd3ddd43SUwe Kleine-König 		return -EINVAL;
251fd3ddd43SUwe Kleine-König 
252fd3ddd43SUwe Kleine-König 	if (!state->enabled) {
253fd3ddd43SUwe Kleine-König 		if (enabled)
254fd3ddd43SUwe Kleine-König 			tegra_pwm_disable(chip, pwm);
255fd3ddd43SUwe Kleine-König 
256fd3ddd43SUwe Kleine-König 		return 0;
257fd3ddd43SUwe Kleine-König 	}
258fd3ddd43SUwe Kleine-König 
259fd3ddd43SUwe Kleine-König 	err = tegra_pwm_config(pwm->chip, pwm, state->duty_cycle, state->period);
260fd3ddd43SUwe Kleine-König 	if (err)
261fd3ddd43SUwe Kleine-König 		return err;
262fd3ddd43SUwe Kleine-König 
263fd3ddd43SUwe Kleine-König 	if (!enabled)
264fd3ddd43SUwe Kleine-König 		err = tegra_pwm_enable(chip, pwm);
265fd3ddd43SUwe Kleine-König 
266fd3ddd43SUwe Kleine-König 	return err;
267fd3ddd43SUwe Kleine-König }
268fd3ddd43SUwe Kleine-König 
2690134b932SThierry Reding static const struct pwm_ops tegra_pwm_ops = {
270fd3ddd43SUwe Kleine-König 	.apply = tegra_pwm_apply,
2710134b932SThierry Reding 	.owner = THIS_MODULE,
2720134b932SThierry Reding };
2730134b932SThierry Reding 
tegra_pwm_probe(struct platform_device * pdev)2740134b932SThierry Reding static int tegra_pwm_probe(struct platform_device *pdev)
2750134b932SThierry Reding {
276f19460c1SUwe Kleine-König 	struct tegra_pwm_chip *pc;
2770134b932SThierry Reding 	int ret;
2780134b932SThierry Reding 
279f19460c1SUwe Kleine-König 	pc = devm_kzalloc(&pdev->dev, sizeof(*pc), GFP_KERNEL);
280f19460c1SUwe Kleine-König 	if (!pc)
2810134b932SThierry Reding 		return -ENOMEM;
2820134b932SThierry Reding 
283f19460c1SUwe Kleine-König 	pc->soc = of_device_get_match_data(&pdev->dev);
284f19460c1SUwe Kleine-König 	pc->dev = &pdev->dev;
2850134b932SThierry Reding 
286f19460c1SUwe Kleine-König 	pc->regs = devm_platform_ioremap_resource(pdev, 0);
287f19460c1SUwe Kleine-König 	if (IS_ERR(pc->regs))
288f19460c1SUwe Kleine-König 		return PTR_ERR(pc->regs);
2890134b932SThierry Reding 
290f19460c1SUwe Kleine-König 	platform_set_drvdata(pdev, pc);
2910134b932SThierry Reding 
292f19460c1SUwe Kleine-König 	pc->clk = devm_clk_get(&pdev->dev, NULL);
293f19460c1SUwe Kleine-König 	if (IS_ERR(pc->clk))
294f19460c1SUwe Kleine-König 		return PTR_ERR(pc->clk);
2950134b932SThierry Reding 
2963da9b0feSDmitry Osipenko 	ret = devm_tegra_core_dev_init_opp_table_common(&pdev->dev);
2973da9b0feSDmitry Osipenko 	if (ret)
2983da9b0feSDmitry Osipenko 		return ret;
2993da9b0feSDmitry Osipenko 
3003da9b0feSDmitry Osipenko 	pm_runtime_enable(&pdev->dev);
3013da9b0feSDmitry Osipenko 	ret = pm_runtime_resume_and_get(&pdev->dev);
3023da9b0feSDmitry Osipenko 	if (ret)
3033da9b0feSDmitry Osipenko 		return ret;
3043da9b0feSDmitry Osipenko 
3050527eb37SLaxman Dewangan 	/* Set maximum frequency of the IP */
306f19460c1SUwe Kleine-König 	ret = dev_pm_opp_set_rate(pc->dev, pc->soc->max_frequency);
3070527eb37SLaxman Dewangan 	if (ret < 0) {
3080527eb37SLaxman Dewangan 		dev_err(&pdev->dev, "Failed to set max frequency: %d\n", ret);
3093da9b0feSDmitry Osipenko 		goto put_pm;
3100527eb37SLaxman Dewangan 	}
3110527eb37SLaxman Dewangan 
3120527eb37SLaxman Dewangan 	/*
3130527eb37SLaxman Dewangan 	 * The requested and configured frequency may differ due to
3140527eb37SLaxman Dewangan 	 * clock register resolutions. Get the configured frequency
3150527eb37SLaxman Dewangan 	 * so that PWM period can be calculated more accurately.
3160527eb37SLaxman Dewangan 	 */
317f19460c1SUwe Kleine-König 	pc->clk_rate = clk_get_rate(pc->clk);
31846fa8bc0SLaxman Dewangan 
3191d7796bdSSandipan Patra 	/* Set minimum limit of PWM period for the IP */
320f19460c1SUwe Kleine-König 	pc->min_period_ns =
321f19460c1SUwe Kleine-König 	    (NSEC_PER_SEC / (pc->soc->max_frequency >> PWM_DUTY_WIDTH)) + 1;
3221d7796bdSSandipan Patra 
323f19460c1SUwe Kleine-König 	pc->rst = devm_reset_control_get_exclusive(&pdev->dev, "pwm");
324f19460c1SUwe Kleine-König 	if (IS_ERR(pc->rst)) {
325f19460c1SUwe Kleine-König 		ret = PTR_ERR(pc->rst);
3265dfbd2bdSRohith Seelaboyina 		dev_err(&pdev->dev, "Reset control is not found: %d\n", ret);
3273da9b0feSDmitry Osipenko 		goto put_pm;
3285dfbd2bdSRohith Seelaboyina 	}
3295dfbd2bdSRohith Seelaboyina 
330f19460c1SUwe Kleine-König 	reset_control_deassert(pc->rst);
3315dfbd2bdSRohith Seelaboyina 
332f19460c1SUwe Kleine-König 	pc->chip.dev = &pdev->dev;
333f19460c1SUwe Kleine-König 	pc->chip.ops = &tegra_pwm_ops;
334f19460c1SUwe Kleine-König 	pc->chip.npwm = pc->soc->num_channels;
3350134b932SThierry Reding 
336f19460c1SUwe Kleine-König 	ret = pwmchip_add(&pc->chip);
3370134b932SThierry Reding 	if (ret < 0) {
3380134b932SThierry Reding 		dev_err(&pdev->dev, "pwmchip_add() failed: %d\n", ret);
339f19460c1SUwe Kleine-König 		reset_control_assert(pc->rst);
3403da9b0feSDmitry Osipenko 		goto put_pm;
3410134b932SThierry Reding 	}
3420134b932SThierry Reding 
3433da9b0feSDmitry Osipenko 	pm_runtime_put(&pdev->dev);
3443da9b0feSDmitry Osipenko 
3450134b932SThierry Reding 	return 0;
3463da9b0feSDmitry Osipenko put_pm:
3473da9b0feSDmitry Osipenko 	pm_runtime_put_sync_suspend(&pdev->dev);
3483da9b0feSDmitry Osipenko 	pm_runtime_force_suspend(&pdev->dev);
3493da9b0feSDmitry Osipenko 	return ret;
3500134b932SThierry Reding }
3510134b932SThierry Reding 
tegra_pwm_remove(struct platform_device * pdev)352*e39cb6f9SUwe Kleine-König static void tegra_pwm_remove(struct platform_device *pdev)
3530134b932SThierry Reding {
3540134b932SThierry Reding 	struct tegra_pwm_chip *pc = platform_get_drvdata(pdev);
3555dfbd2bdSRohith Seelaboyina 
3562f1a3bd4SUwe Kleine-König 	pwmchip_remove(&pc->chip);
3572f1a3bd4SUwe Kleine-König 
3585dfbd2bdSRohith Seelaboyina 	reset_control_assert(pc->rst);
3595dfbd2bdSRohith Seelaboyina 
3603da9b0feSDmitry Osipenko 	pm_runtime_force_suspend(&pdev->dev);
3610134b932SThierry Reding }
3620134b932SThierry Reding 
tegra_pwm_runtime_suspend(struct device * dev)3633da9b0feSDmitry Osipenko static int __maybe_unused tegra_pwm_runtime_suspend(struct device *dev)
3644a813b26SLaxman Dewangan {
3653da9b0feSDmitry Osipenko 	struct tegra_pwm_chip *pc = dev_get_drvdata(dev);
3663da9b0feSDmitry Osipenko 	int err;
3673da9b0feSDmitry Osipenko 
3683da9b0feSDmitry Osipenko 	clk_disable_unprepare(pc->clk);
3693da9b0feSDmitry Osipenko 
3703da9b0feSDmitry Osipenko 	err = pinctrl_pm_select_sleep_state(dev);
3713da9b0feSDmitry Osipenko 	if (err) {
3723da9b0feSDmitry Osipenko 		clk_prepare_enable(pc->clk);
3733da9b0feSDmitry Osipenko 		return err;
3744a813b26SLaxman Dewangan 	}
3754a813b26SLaxman Dewangan 
3763da9b0feSDmitry Osipenko 	return 0;
3774a813b26SLaxman Dewangan }
3783da9b0feSDmitry Osipenko 
tegra_pwm_runtime_resume(struct device * dev)3793da9b0feSDmitry Osipenko static int __maybe_unused tegra_pwm_runtime_resume(struct device *dev)
3803da9b0feSDmitry Osipenko {
3813da9b0feSDmitry Osipenko 	struct tegra_pwm_chip *pc = dev_get_drvdata(dev);
3823da9b0feSDmitry Osipenko 	int err;
3833da9b0feSDmitry Osipenko 
3843da9b0feSDmitry Osipenko 	err = pinctrl_pm_select_default_state(dev);
3853da9b0feSDmitry Osipenko 	if (err)
3863da9b0feSDmitry Osipenko 		return err;
3873da9b0feSDmitry Osipenko 
3883da9b0feSDmitry Osipenko 	err = clk_prepare_enable(pc->clk);
3893da9b0feSDmitry Osipenko 	if (err) {
3903da9b0feSDmitry Osipenko 		pinctrl_pm_select_sleep_state(dev);
3913da9b0feSDmitry Osipenko 		return err;
3923da9b0feSDmitry Osipenko 	}
3933da9b0feSDmitry Osipenko 
3943da9b0feSDmitry Osipenko 	return 0;
3953da9b0feSDmitry Osipenko }
3964a813b26SLaxman Dewangan 
397e9be88a2SLaxman Dewangan static const struct tegra_pwm_soc tegra20_pwm_soc = {
398e9be88a2SLaxman Dewangan 	.num_channels = 4,
3990527eb37SLaxman Dewangan 	.max_frequency = 48000000UL,
400e9be88a2SLaxman Dewangan };
401e9be88a2SLaxman Dewangan 
402e9be88a2SLaxman Dewangan static const struct tegra_pwm_soc tegra186_pwm_soc = {
403e9be88a2SLaxman Dewangan 	.num_channels = 1,
4040527eb37SLaxman Dewangan 	.max_frequency = 102000000UL,
405e9be88a2SLaxman Dewangan };
406e9be88a2SLaxman Dewangan 
4072d0c08fcSSandipan Patra static const struct tegra_pwm_soc tegra194_pwm_soc = {
4082d0c08fcSSandipan Patra 	.num_channels = 1,
4092d0c08fcSSandipan Patra 	.max_frequency = 408000000UL,
4102d0c08fcSSandipan Patra };
4112d0c08fcSSandipan Patra 
412f1a8870aSThierry Reding static const struct of_device_id tegra_pwm_of_match[] = {
413e9be88a2SLaxman Dewangan 	{ .compatible = "nvidia,tegra20-pwm", .data = &tegra20_pwm_soc },
414e9be88a2SLaxman Dewangan 	{ .compatible = "nvidia,tegra186-pwm", .data = &tegra186_pwm_soc },
4152d0c08fcSSandipan Patra 	{ .compatible = "nvidia,tegra194-pwm", .data = &tegra194_pwm_soc },
416140fd977SThierry Reding 	{ }
417140fd977SThierry Reding };
418140fd977SThierry Reding MODULE_DEVICE_TABLE(of, tegra_pwm_of_match);
419140fd977SThierry Reding 
4204a813b26SLaxman Dewangan static const struct dev_pm_ops tegra_pwm_pm_ops = {
4213da9b0feSDmitry Osipenko 	SET_RUNTIME_PM_OPS(tegra_pwm_runtime_suspend, tegra_pwm_runtime_resume,
4223da9b0feSDmitry Osipenko 			   NULL)
4233da9b0feSDmitry Osipenko 	SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
4243da9b0feSDmitry Osipenko 				pm_runtime_force_resume)
4254a813b26SLaxman Dewangan };
4264a813b26SLaxman Dewangan 
4270134b932SThierry Reding static struct platform_driver tegra_pwm_driver = {
4280134b932SThierry Reding 	.driver = {
4290134b932SThierry Reding 		.name = "tegra-pwm",
430838bf09dSStephen Warren 		.of_match_table = tegra_pwm_of_match,
4314a813b26SLaxman Dewangan 		.pm = &tegra_pwm_pm_ops,
4320134b932SThierry Reding 	},
4330134b932SThierry Reding 	.probe = tegra_pwm_probe,
434*e39cb6f9SUwe Kleine-König 	.remove_new = tegra_pwm_remove,
4350134b932SThierry Reding };
4360134b932SThierry Reding 
4370134b932SThierry Reding module_platform_driver(tegra_pwm_driver);
4380134b932SThierry Reding 
4390134b932SThierry Reding MODULE_LICENSE("GPL");
4401d7796bdSSandipan Patra MODULE_AUTHOR("Sandipan Patra <spatra@nvidia.com>");
4411d7796bdSSandipan Patra MODULE_DESCRIPTION("Tegra PWM controller driver");
4420134b932SThierry Reding MODULE_ALIAS("platform:tegra-pwm");
443