18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * EHRPWM PWM driver
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2012 Texas Instruments, Inc. - https://www.ti.com/
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci#include <linux/module.h>
98c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
108c2ecf20Sopenharmony_ci#include <linux/pwm.h>
118c2ecf20Sopenharmony_ci#include <linux/io.h>
128c2ecf20Sopenharmony_ci#include <linux/err.h>
138c2ecf20Sopenharmony_ci#include <linux/clk.h>
148c2ecf20Sopenharmony_ci#include <linux/pm_runtime.h>
158c2ecf20Sopenharmony_ci#include <linux/of_device.h>
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ci/* EHRPWM registers and bits definitions */
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci/* Time base module registers */
208c2ecf20Sopenharmony_ci#define TBCTL			0x00
218c2ecf20Sopenharmony_ci#define TBPRD			0x0A
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci#define TBCTL_PRDLD_MASK	BIT(3)
248c2ecf20Sopenharmony_ci#define TBCTL_PRDLD_SHDW	0
258c2ecf20Sopenharmony_ci#define TBCTL_PRDLD_IMDT	BIT(3)
268c2ecf20Sopenharmony_ci#define TBCTL_CLKDIV_MASK	(BIT(12) | BIT(11) | BIT(10) | BIT(9) | \
278c2ecf20Sopenharmony_ci				BIT(8) | BIT(7))
288c2ecf20Sopenharmony_ci#define TBCTL_CTRMODE_MASK	(BIT(1) | BIT(0))
298c2ecf20Sopenharmony_ci#define TBCTL_CTRMODE_UP	0
308c2ecf20Sopenharmony_ci#define TBCTL_CTRMODE_DOWN	BIT(0)
318c2ecf20Sopenharmony_ci#define TBCTL_CTRMODE_UPDOWN	BIT(1)
328c2ecf20Sopenharmony_ci#define TBCTL_CTRMODE_FREEZE	(BIT(1) | BIT(0))
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ci#define TBCTL_HSPCLKDIV_SHIFT	7
358c2ecf20Sopenharmony_ci#define TBCTL_CLKDIV_SHIFT	10
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci#define CLKDIV_MAX		7
388c2ecf20Sopenharmony_ci#define HSPCLKDIV_MAX		7
398c2ecf20Sopenharmony_ci#define PERIOD_MAX		0xFFFF
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci/* compare module registers */
428c2ecf20Sopenharmony_ci#define CMPA			0x12
438c2ecf20Sopenharmony_ci#define CMPB			0x14
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_ci/* Action qualifier module registers */
468c2ecf20Sopenharmony_ci#define AQCTLA			0x16
478c2ecf20Sopenharmony_ci#define AQCTLB			0x18
488c2ecf20Sopenharmony_ci#define AQSFRC			0x1A
498c2ecf20Sopenharmony_ci#define AQCSFRC			0x1C
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci#define AQCTL_CBU_MASK		(BIT(9) | BIT(8))
528c2ecf20Sopenharmony_ci#define AQCTL_CBU_FRCLOW	BIT(8)
538c2ecf20Sopenharmony_ci#define AQCTL_CBU_FRCHIGH	BIT(9)
548c2ecf20Sopenharmony_ci#define AQCTL_CBU_FRCTOGGLE	(BIT(9) | BIT(8))
558c2ecf20Sopenharmony_ci#define AQCTL_CAU_MASK		(BIT(5) | BIT(4))
568c2ecf20Sopenharmony_ci#define AQCTL_CAU_FRCLOW	BIT(4)
578c2ecf20Sopenharmony_ci#define AQCTL_CAU_FRCHIGH	BIT(5)
588c2ecf20Sopenharmony_ci#define AQCTL_CAU_FRCTOGGLE	(BIT(5) | BIT(4))
598c2ecf20Sopenharmony_ci#define AQCTL_PRD_MASK		(BIT(3) | BIT(2))
608c2ecf20Sopenharmony_ci#define AQCTL_PRD_FRCLOW	BIT(2)
618c2ecf20Sopenharmony_ci#define AQCTL_PRD_FRCHIGH	BIT(3)
628c2ecf20Sopenharmony_ci#define AQCTL_PRD_FRCTOGGLE	(BIT(3) | BIT(2))
638c2ecf20Sopenharmony_ci#define AQCTL_ZRO_MASK		(BIT(1) | BIT(0))
648c2ecf20Sopenharmony_ci#define AQCTL_ZRO_FRCLOW	BIT(0)
658c2ecf20Sopenharmony_ci#define AQCTL_ZRO_FRCHIGH	BIT(1)
668c2ecf20Sopenharmony_ci#define AQCTL_ZRO_FRCTOGGLE	(BIT(1) | BIT(0))
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci#define AQCTL_CHANA_POLNORMAL	(AQCTL_CAU_FRCLOW | AQCTL_PRD_FRCHIGH | \
698c2ecf20Sopenharmony_ci				AQCTL_ZRO_FRCHIGH)
708c2ecf20Sopenharmony_ci#define AQCTL_CHANA_POLINVERSED	(AQCTL_CAU_FRCHIGH | AQCTL_PRD_FRCLOW | \
718c2ecf20Sopenharmony_ci				AQCTL_ZRO_FRCLOW)
728c2ecf20Sopenharmony_ci#define AQCTL_CHANB_POLNORMAL	(AQCTL_CBU_FRCLOW | AQCTL_PRD_FRCHIGH | \
738c2ecf20Sopenharmony_ci				AQCTL_ZRO_FRCHIGH)
748c2ecf20Sopenharmony_ci#define AQCTL_CHANB_POLINVERSED	(AQCTL_CBU_FRCHIGH | AQCTL_PRD_FRCLOW | \
758c2ecf20Sopenharmony_ci				AQCTL_ZRO_FRCLOW)
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ci#define AQSFRC_RLDCSF_MASK	(BIT(7) | BIT(6))
788c2ecf20Sopenharmony_ci#define AQSFRC_RLDCSF_ZRO	0
798c2ecf20Sopenharmony_ci#define AQSFRC_RLDCSF_PRD	BIT(6)
808c2ecf20Sopenharmony_ci#define AQSFRC_RLDCSF_ZROPRD	BIT(7)
818c2ecf20Sopenharmony_ci#define AQSFRC_RLDCSF_IMDT	(BIT(7) | BIT(6))
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci#define AQCSFRC_CSFB_MASK	(BIT(3) | BIT(2))
848c2ecf20Sopenharmony_ci#define AQCSFRC_CSFB_FRCDIS	0
858c2ecf20Sopenharmony_ci#define AQCSFRC_CSFB_FRCLOW	BIT(2)
868c2ecf20Sopenharmony_ci#define AQCSFRC_CSFB_FRCHIGH	BIT(3)
878c2ecf20Sopenharmony_ci#define AQCSFRC_CSFB_DISSWFRC	(BIT(3) | BIT(2))
888c2ecf20Sopenharmony_ci#define AQCSFRC_CSFA_MASK	(BIT(1) | BIT(0))
898c2ecf20Sopenharmony_ci#define AQCSFRC_CSFA_FRCDIS	0
908c2ecf20Sopenharmony_ci#define AQCSFRC_CSFA_FRCLOW	BIT(0)
918c2ecf20Sopenharmony_ci#define AQCSFRC_CSFA_FRCHIGH	BIT(1)
928c2ecf20Sopenharmony_ci#define AQCSFRC_CSFA_DISSWFRC	(BIT(1) | BIT(0))
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ci#define NUM_PWM_CHANNEL		2	/* EHRPWM channels */
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_cistruct ehrpwm_context {
978c2ecf20Sopenharmony_ci	u16 tbctl;
988c2ecf20Sopenharmony_ci	u16 tbprd;
998c2ecf20Sopenharmony_ci	u16 cmpa;
1008c2ecf20Sopenharmony_ci	u16 cmpb;
1018c2ecf20Sopenharmony_ci	u16 aqctla;
1028c2ecf20Sopenharmony_ci	u16 aqctlb;
1038c2ecf20Sopenharmony_ci	u16 aqsfrc;
1048c2ecf20Sopenharmony_ci	u16 aqcsfrc;
1058c2ecf20Sopenharmony_ci};
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_cistruct ehrpwm_pwm_chip {
1088c2ecf20Sopenharmony_ci	struct pwm_chip chip;
1098c2ecf20Sopenharmony_ci	unsigned long clk_rate;
1108c2ecf20Sopenharmony_ci	void __iomem *mmio_base;
1118c2ecf20Sopenharmony_ci	unsigned long period_cycles[NUM_PWM_CHANNEL];
1128c2ecf20Sopenharmony_ci	enum pwm_polarity polarity[NUM_PWM_CHANNEL];
1138c2ecf20Sopenharmony_ci	struct clk *tbclk;
1148c2ecf20Sopenharmony_ci	struct ehrpwm_context ctx;
1158c2ecf20Sopenharmony_ci};
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_cistatic inline struct ehrpwm_pwm_chip *to_ehrpwm_pwm_chip(struct pwm_chip *chip)
1188c2ecf20Sopenharmony_ci{
1198c2ecf20Sopenharmony_ci	return container_of(chip, struct ehrpwm_pwm_chip, chip);
1208c2ecf20Sopenharmony_ci}
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_cistatic inline u16 ehrpwm_read(void __iomem *base, unsigned int offset)
1238c2ecf20Sopenharmony_ci{
1248c2ecf20Sopenharmony_ci	return readw(base + offset);
1258c2ecf20Sopenharmony_ci}
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_cistatic inline void ehrpwm_write(void __iomem *base, unsigned int offset,
1288c2ecf20Sopenharmony_ci				u16 value)
1298c2ecf20Sopenharmony_ci{
1308c2ecf20Sopenharmony_ci	writew(value, base + offset);
1318c2ecf20Sopenharmony_ci}
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_cistatic void ehrpwm_modify(void __iomem *base, unsigned int offset, u16 mask,
1348c2ecf20Sopenharmony_ci			  u16 value)
1358c2ecf20Sopenharmony_ci{
1368c2ecf20Sopenharmony_ci	unsigned short val;
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_ci	val = readw(base + offset);
1398c2ecf20Sopenharmony_ci	val &= ~mask;
1408c2ecf20Sopenharmony_ci	val |= value & mask;
1418c2ecf20Sopenharmony_ci	writew(val, base + offset);
1428c2ecf20Sopenharmony_ci}
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_ci/**
1458c2ecf20Sopenharmony_ci * set_prescale_div -	Set up the prescaler divider function
1468c2ecf20Sopenharmony_ci * @rqst_prescaler:	prescaler value min
1478c2ecf20Sopenharmony_ci * @prescale_div:	prescaler value set
1488c2ecf20Sopenharmony_ci * @tb_clk_div:		Time Base Control prescaler bits
1498c2ecf20Sopenharmony_ci */
1508c2ecf20Sopenharmony_cistatic int set_prescale_div(unsigned long rqst_prescaler, u16 *prescale_div,
1518c2ecf20Sopenharmony_ci			    u16 *tb_clk_div)
1528c2ecf20Sopenharmony_ci{
1538c2ecf20Sopenharmony_ci	unsigned int clkdiv, hspclkdiv;
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_ci	for (clkdiv = 0; clkdiv <= CLKDIV_MAX; clkdiv++) {
1568c2ecf20Sopenharmony_ci		for (hspclkdiv = 0; hspclkdiv <= HSPCLKDIV_MAX; hspclkdiv++) {
1578c2ecf20Sopenharmony_ci			/*
1588c2ecf20Sopenharmony_ci			 * calculations for prescaler value :
1598c2ecf20Sopenharmony_ci			 * prescale_div = HSPCLKDIVIDER * CLKDIVIDER.
1608c2ecf20Sopenharmony_ci			 * HSPCLKDIVIDER =  2 ** hspclkdiv
1618c2ecf20Sopenharmony_ci			 * CLKDIVIDER = (1),		if clkdiv == 0 *OR*
1628c2ecf20Sopenharmony_ci			 *		(2 * clkdiv),	if clkdiv != 0
1638c2ecf20Sopenharmony_ci			 *
1648c2ecf20Sopenharmony_ci			 * Configure prescale_div value such that period
1658c2ecf20Sopenharmony_ci			 * register value is less than 65535.
1668c2ecf20Sopenharmony_ci			 */
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_ci			*prescale_div = (1 << clkdiv) *
1698c2ecf20Sopenharmony_ci					(hspclkdiv ? (hspclkdiv * 2) : 1);
1708c2ecf20Sopenharmony_ci			if (*prescale_div > rqst_prescaler) {
1718c2ecf20Sopenharmony_ci				*tb_clk_div = (clkdiv << TBCTL_CLKDIV_SHIFT) |
1728c2ecf20Sopenharmony_ci					(hspclkdiv << TBCTL_HSPCLKDIV_SHIFT);
1738c2ecf20Sopenharmony_ci				return 0;
1748c2ecf20Sopenharmony_ci			}
1758c2ecf20Sopenharmony_ci		}
1768c2ecf20Sopenharmony_ci	}
1778c2ecf20Sopenharmony_ci
1788c2ecf20Sopenharmony_ci	return 1;
1798c2ecf20Sopenharmony_ci}
1808c2ecf20Sopenharmony_ci
1818c2ecf20Sopenharmony_cistatic void configure_polarity(struct ehrpwm_pwm_chip *pc, int chan)
1828c2ecf20Sopenharmony_ci{
1838c2ecf20Sopenharmony_ci	u16 aqctl_val, aqctl_mask;
1848c2ecf20Sopenharmony_ci	unsigned int aqctl_reg;
1858c2ecf20Sopenharmony_ci
1868c2ecf20Sopenharmony_ci	/*
1878c2ecf20Sopenharmony_ci	 * Configure PWM output to HIGH/LOW level on counter
1888c2ecf20Sopenharmony_ci	 * reaches compare register value and LOW/HIGH level
1898c2ecf20Sopenharmony_ci	 * on counter value reaches period register value and
1908c2ecf20Sopenharmony_ci	 * zero value on counter
1918c2ecf20Sopenharmony_ci	 */
1928c2ecf20Sopenharmony_ci	if (chan == 1) {
1938c2ecf20Sopenharmony_ci		aqctl_reg = AQCTLB;
1948c2ecf20Sopenharmony_ci		aqctl_mask = AQCTL_CBU_MASK;
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_ci		if (pc->polarity[chan] == PWM_POLARITY_INVERSED)
1978c2ecf20Sopenharmony_ci			aqctl_val = AQCTL_CHANB_POLINVERSED;
1988c2ecf20Sopenharmony_ci		else
1998c2ecf20Sopenharmony_ci			aqctl_val = AQCTL_CHANB_POLNORMAL;
2008c2ecf20Sopenharmony_ci	} else {
2018c2ecf20Sopenharmony_ci		aqctl_reg = AQCTLA;
2028c2ecf20Sopenharmony_ci		aqctl_mask = AQCTL_CAU_MASK;
2038c2ecf20Sopenharmony_ci
2048c2ecf20Sopenharmony_ci		if (pc->polarity[chan] == PWM_POLARITY_INVERSED)
2058c2ecf20Sopenharmony_ci			aqctl_val = AQCTL_CHANA_POLINVERSED;
2068c2ecf20Sopenharmony_ci		else
2078c2ecf20Sopenharmony_ci			aqctl_val = AQCTL_CHANA_POLNORMAL;
2088c2ecf20Sopenharmony_ci	}
2098c2ecf20Sopenharmony_ci
2108c2ecf20Sopenharmony_ci	aqctl_mask |= AQCTL_PRD_MASK | AQCTL_ZRO_MASK;
2118c2ecf20Sopenharmony_ci	ehrpwm_modify(pc->mmio_base, aqctl_reg, aqctl_mask, aqctl_val);
2128c2ecf20Sopenharmony_ci}
2138c2ecf20Sopenharmony_ci
2148c2ecf20Sopenharmony_ci/*
2158c2ecf20Sopenharmony_ci * period_ns = 10^9 * (ps_divval * period_cycles) / PWM_CLK_RATE
2168c2ecf20Sopenharmony_ci * duty_ns   = 10^9 * (ps_divval * duty_cycles) / PWM_CLK_RATE
2178c2ecf20Sopenharmony_ci */
2188c2ecf20Sopenharmony_cistatic int ehrpwm_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
2198c2ecf20Sopenharmony_ci			     int duty_ns, int period_ns)
2208c2ecf20Sopenharmony_ci{
2218c2ecf20Sopenharmony_ci	struct ehrpwm_pwm_chip *pc = to_ehrpwm_pwm_chip(chip);
2228c2ecf20Sopenharmony_ci	u32 period_cycles, duty_cycles;
2238c2ecf20Sopenharmony_ci	u16 ps_divval, tb_divval;
2248c2ecf20Sopenharmony_ci	unsigned int i, cmp_reg;
2258c2ecf20Sopenharmony_ci	unsigned long long c;
2268c2ecf20Sopenharmony_ci
2278c2ecf20Sopenharmony_ci	if (period_ns > NSEC_PER_SEC)
2288c2ecf20Sopenharmony_ci		return -ERANGE;
2298c2ecf20Sopenharmony_ci
2308c2ecf20Sopenharmony_ci	c = pc->clk_rate;
2318c2ecf20Sopenharmony_ci	c = c * period_ns;
2328c2ecf20Sopenharmony_ci	do_div(c, NSEC_PER_SEC);
2338c2ecf20Sopenharmony_ci	period_cycles = (unsigned long)c;
2348c2ecf20Sopenharmony_ci
2358c2ecf20Sopenharmony_ci	if (period_cycles < 1) {
2368c2ecf20Sopenharmony_ci		period_cycles = 1;
2378c2ecf20Sopenharmony_ci		duty_cycles = 1;
2388c2ecf20Sopenharmony_ci	} else {
2398c2ecf20Sopenharmony_ci		c = pc->clk_rate;
2408c2ecf20Sopenharmony_ci		c = c * duty_ns;
2418c2ecf20Sopenharmony_ci		do_div(c, NSEC_PER_SEC);
2428c2ecf20Sopenharmony_ci		duty_cycles = (unsigned long)c;
2438c2ecf20Sopenharmony_ci	}
2448c2ecf20Sopenharmony_ci
2458c2ecf20Sopenharmony_ci	/*
2468c2ecf20Sopenharmony_ci	 * Period values should be same for multiple PWM channels as IP uses
2478c2ecf20Sopenharmony_ci	 * same period register for multiple channels.
2488c2ecf20Sopenharmony_ci	 */
2498c2ecf20Sopenharmony_ci	for (i = 0; i < NUM_PWM_CHANNEL; i++) {
2508c2ecf20Sopenharmony_ci		if (pc->period_cycles[i] &&
2518c2ecf20Sopenharmony_ci				(pc->period_cycles[i] != period_cycles)) {
2528c2ecf20Sopenharmony_ci			/*
2538c2ecf20Sopenharmony_ci			 * Allow channel to reconfigure period if no other
2548c2ecf20Sopenharmony_ci			 * channels being configured.
2558c2ecf20Sopenharmony_ci			 */
2568c2ecf20Sopenharmony_ci			if (i == pwm->hwpwm)
2578c2ecf20Sopenharmony_ci				continue;
2588c2ecf20Sopenharmony_ci
2598c2ecf20Sopenharmony_ci			dev_err(chip->dev,
2608c2ecf20Sopenharmony_ci				"period value conflicts with channel %u\n",
2618c2ecf20Sopenharmony_ci				i);
2628c2ecf20Sopenharmony_ci			return -EINVAL;
2638c2ecf20Sopenharmony_ci		}
2648c2ecf20Sopenharmony_ci	}
2658c2ecf20Sopenharmony_ci
2668c2ecf20Sopenharmony_ci	pc->period_cycles[pwm->hwpwm] = period_cycles;
2678c2ecf20Sopenharmony_ci
2688c2ecf20Sopenharmony_ci	/* Configure clock prescaler to support Low frequency PWM wave */
2698c2ecf20Sopenharmony_ci	if (set_prescale_div(period_cycles/PERIOD_MAX, &ps_divval,
2708c2ecf20Sopenharmony_ci			     &tb_divval)) {
2718c2ecf20Sopenharmony_ci		dev_err(chip->dev, "Unsupported values\n");
2728c2ecf20Sopenharmony_ci		return -EINVAL;
2738c2ecf20Sopenharmony_ci	}
2748c2ecf20Sopenharmony_ci
2758c2ecf20Sopenharmony_ci	pm_runtime_get_sync(chip->dev);
2768c2ecf20Sopenharmony_ci
2778c2ecf20Sopenharmony_ci	/* Update clock prescaler values */
2788c2ecf20Sopenharmony_ci	ehrpwm_modify(pc->mmio_base, TBCTL, TBCTL_CLKDIV_MASK, tb_divval);
2798c2ecf20Sopenharmony_ci
2808c2ecf20Sopenharmony_ci	/* Update period & duty cycle with presacler division */
2818c2ecf20Sopenharmony_ci	period_cycles = period_cycles / ps_divval;
2828c2ecf20Sopenharmony_ci	duty_cycles = duty_cycles / ps_divval;
2838c2ecf20Sopenharmony_ci
2848c2ecf20Sopenharmony_ci	/* Configure shadow loading on Period register */
2858c2ecf20Sopenharmony_ci	ehrpwm_modify(pc->mmio_base, TBCTL, TBCTL_PRDLD_MASK, TBCTL_PRDLD_SHDW);
2868c2ecf20Sopenharmony_ci
2878c2ecf20Sopenharmony_ci	ehrpwm_write(pc->mmio_base, TBPRD, period_cycles);
2888c2ecf20Sopenharmony_ci
2898c2ecf20Sopenharmony_ci	/* Configure ehrpwm counter for up-count mode */
2908c2ecf20Sopenharmony_ci	ehrpwm_modify(pc->mmio_base, TBCTL, TBCTL_CTRMODE_MASK,
2918c2ecf20Sopenharmony_ci		      TBCTL_CTRMODE_UP);
2928c2ecf20Sopenharmony_ci
2938c2ecf20Sopenharmony_ci	if (pwm->hwpwm == 1)
2948c2ecf20Sopenharmony_ci		/* Channel 1 configured with compare B register */
2958c2ecf20Sopenharmony_ci		cmp_reg = CMPB;
2968c2ecf20Sopenharmony_ci	else
2978c2ecf20Sopenharmony_ci		/* Channel 0 configured with compare A register */
2988c2ecf20Sopenharmony_ci		cmp_reg = CMPA;
2998c2ecf20Sopenharmony_ci
3008c2ecf20Sopenharmony_ci	ehrpwm_write(pc->mmio_base, cmp_reg, duty_cycles);
3018c2ecf20Sopenharmony_ci
3028c2ecf20Sopenharmony_ci	pm_runtime_put_sync(chip->dev);
3038c2ecf20Sopenharmony_ci
3048c2ecf20Sopenharmony_ci	return 0;
3058c2ecf20Sopenharmony_ci}
3068c2ecf20Sopenharmony_ci
3078c2ecf20Sopenharmony_cistatic int ehrpwm_pwm_set_polarity(struct pwm_chip *chip,
3088c2ecf20Sopenharmony_ci				   struct pwm_device *pwm,
3098c2ecf20Sopenharmony_ci				   enum pwm_polarity polarity)
3108c2ecf20Sopenharmony_ci{
3118c2ecf20Sopenharmony_ci	struct ehrpwm_pwm_chip *pc = to_ehrpwm_pwm_chip(chip);
3128c2ecf20Sopenharmony_ci
3138c2ecf20Sopenharmony_ci	/* Configuration of polarity in hardware delayed, do at enable */
3148c2ecf20Sopenharmony_ci	pc->polarity[pwm->hwpwm] = polarity;
3158c2ecf20Sopenharmony_ci
3168c2ecf20Sopenharmony_ci	return 0;
3178c2ecf20Sopenharmony_ci}
3188c2ecf20Sopenharmony_ci
3198c2ecf20Sopenharmony_cistatic int ehrpwm_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
3208c2ecf20Sopenharmony_ci{
3218c2ecf20Sopenharmony_ci	struct ehrpwm_pwm_chip *pc = to_ehrpwm_pwm_chip(chip);
3228c2ecf20Sopenharmony_ci	u16 aqcsfrc_val, aqcsfrc_mask;
3238c2ecf20Sopenharmony_ci	int ret;
3248c2ecf20Sopenharmony_ci
3258c2ecf20Sopenharmony_ci	/* Leave clock enabled on enabling PWM */
3268c2ecf20Sopenharmony_ci	pm_runtime_get_sync(chip->dev);
3278c2ecf20Sopenharmony_ci
3288c2ecf20Sopenharmony_ci	/* Disabling Action Qualifier on PWM output */
3298c2ecf20Sopenharmony_ci	if (pwm->hwpwm) {
3308c2ecf20Sopenharmony_ci		aqcsfrc_val = AQCSFRC_CSFB_FRCDIS;
3318c2ecf20Sopenharmony_ci		aqcsfrc_mask = AQCSFRC_CSFB_MASK;
3328c2ecf20Sopenharmony_ci	} else {
3338c2ecf20Sopenharmony_ci		aqcsfrc_val = AQCSFRC_CSFA_FRCDIS;
3348c2ecf20Sopenharmony_ci		aqcsfrc_mask = AQCSFRC_CSFA_MASK;
3358c2ecf20Sopenharmony_ci	}
3368c2ecf20Sopenharmony_ci
3378c2ecf20Sopenharmony_ci	/* Changes to shadow mode */
3388c2ecf20Sopenharmony_ci	ehrpwm_modify(pc->mmio_base, AQSFRC, AQSFRC_RLDCSF_MASK,
3398c2ecf20Sopenharmony_ci		      AQSFRC_RLDCSF_ZRO);
3408c2ecf20Sopenharmony_ci
3418c2ecf20Sopenharmony_ci	ehrpwm_modify(pc->mmio_base, AQCSFRC, aqcsfrc_mask, aqcsfrc_val);
3428c2ecf20Sopenharmony_ci
3438c2ecf20Sopenharmony_ci	/* Channels polarity can be configured from action qualifier module */
3448c2ecf20Sopenharmony_ci	configure_polarity(pc, pwm->hwpwm);
3458c2ecf20Sopenharmony_ci
3468c2ecf20Sopenharmony_ci	/* Enable TBCLK */
3478c2ecf20Sopenharmony_ci	ret = clk_enable(pc->tbclk);
3488c2ecf20Sopenharmony_ci	if (ret) {
3498c2ecf20Sopenharmony_ci		dev_err(chip->dev, "Failed to enable TBCLK for %s: %d\n",
3508c2ecf20Sopenharmony_ci			dev_name(pc->chip.dev), ret);
3518c2ecf20Sopenharmony_ci		return ret;
3528c2ecf20Sopenharmony_ci	}
3538c2ecf20Sopenharmony_ci
3548c2ecf20Sopenharmony_ci	return 0;
3558c2ecf20Sopenharmony_ci}
3568c2ecf20Sopenharmony_ci
3578c2ecf20Sopenharmony_cistatic void ehrpwm_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
3588c2ecf20Sopenharmony_ci{
3598c2ecf20Sopenharmony_ci	struct ehrpwm_pwm_chip *pc = to_ehrpwm_pwm_chip(chip);
3608c2ecf20Sopenharmony_ci	u16 aqcsfrc_val, aqcsfrc_mask;
3618c2ecf20Sopenharmony_ci
3628c2ecf20Sopenharmony_ci	/* Action Qualifier puts PWM output low forcefully */
3638c2ecf20Sopenharmony_ci	if (pwm->hwpwm) {
3648c2ecf20Sopenharmony_ci		aqcsfrc_val = AQCSFRC_CSFB_FRCLOW;
3658c2ecf20Sopenharmony_ci		aqcsfrc_mask = AQCSFRC_CSFB_MASK;
3668c2ecf20Sopenharmony_ci	} else {
3678c2ecf20Sopenharmony_ci		aqcsfrc_val = AQCSFRC_CSFA_FRCLOW;
3688c2ecf20Sopenharmony_ci		aqcsfrc_mask = AQCSFRC_CSFA_MASK;
3698c2ecf20Sopenharmony_ci	}
3708c2ecf20Sopenharmony_ci
3718c2ecf20Sopenharmony_ci	/* Update shadow register first before modifying active register */
3728c2ecf20Sopenharmony_ci	ehrpwm_modify(pc->mmio_base, AQSFRC, AQSFRC_RLDCSF_MASK,
3738c2ecf20Sopenharmony_ci		      AQSFRC_RLDCSF_ZRO);
3748c2ecf20Sopenharmony_ci	ehrpwm_modify(pc->mmio_base, AQCSFRC, aqcsfrc_mask, aqcsfrc_val);
3758c2ecf20Sopenharmony_ci	/*
3768c2ecf20Sopenharmony_ci	 * Changes to immediate action on Action Qualifier. This puts
3778c2ecf20Sopenharmony_ci	 * Action Qualifier control on PWM output from next TBCLK
3788c2ecf20Sopenharmony_ci	 */
3798c2ecf20Sopenharmony_ci	ehrpwm_modify(pc->mmio_base, AQSFRC, AQSFRC_RLDCSF_MASK,
3808c2ecf20Sopenharmony_ci		      AQSFRC_RLDCSF_IMDT);
3818c2ecf20Sopenharmony_ci
3828c2ecf20Sopenharmony_ci	ehrpwm_modify(pc->mmio_base, AQCSFRC, aqcsfrc_mask, aqcsfrc_val);
3838c2ecf20Sopenharmony_ci
3848c2ecf20Sopenharmony_ci	/* Disabling TBCLK on PWM disable */
3858c2ecf20Sopenharmony_ci	clk_disable(pc->tbclk);
3868c2ecf20Sopenharmony_ci
3878c2ecf20Sopenharmony_ci	/* Disable clock on PWM disable */
3888c2ecf20Sopenharmony_ci	pm_runtime_put_sync(chip->dev);
3898c2ecf20Sopenharmony_ci}
3908c2ecf20Sopenharmony_ci
3918c2ecf20Sopenharmony_cistatic void ehrpwm_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm)
3928c2ecf20Sopenharmony_ci{
3938c2ecf20Sopenharmony_ci	struct ehrpwm_pwm_chip *pc = to_ehrpwm_pwm_chip(chip);
3948c2ecf20Sopenharmony_ci
3958c2ecf20Sopenharmony_ci	if (pwm_is_enabled(pwm)) {
3968c2ecf20Sopenharmony_ci		dev_warn(chip->dev, "Removing PWM device without disabling\n");
3978c2ecf20Sopenharmony_ci		pm_runtime_put_sync(chip->dev);
3988c2ecf20Sopenharmony_ci	}
3998c2ecf20Sopenharmony_ci
4008c2ecf20Sopenharmony_ci	/* set period value to zero on free */
4018c2ecf20Sopenharmony_ci	pc->period_cycles[pwm->hwpwm] = 0;
4028c2ecf20Sopenharmony_ci}
4038c2ecf20Sopenharmony_ci
4048c2ecf20Sopenharmony_cistatic const struct pwm_ops ehrpwm_pwm_ops = {
4058c2ecf20Sopenharmony_ci	.free = ehrpwm_pwm_free,
4068c2ecf20Sopenharmony_ci	.config = ehrpwm_pwm_config,
4078c2ecf20Sopenharmony_ci	.set_polarity = ehrpwm_pwm_set_polarity,
4088c2ecf20Sopenharmony_ci	.enable = ehrpwm_pwm_enable,
4098c2ecf20Sopenharmony_ci	.disable = ehrpwm_pwm_disable,
4108c2ecf20Sopenharmony_ci	.owner = THIS_MODULE,
4118c2ecf20Sopenharmony_ci};
4128c2ecf20Sopenharmony_ci
4138c2ecf20Sopenharmony_cistatic const struct of_device_id ehrpwm_of_match[] = {
4148c2ecf20Sopenharmony_ci	{ .compatible = "ti,am3352-ehrpwm" },
4158c2ecf20Sopenharmony_ci	{ .compatible = "ti,am33xx-ehrpwm" },
4168c2ecf20Sopenharmony_ci	{},
4178c2ecf20Sopenharmony_ci};
4188c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, ehrpwm_of_match);
4198c2ecf20Sopenharmony_ci
4208c2ecf20Sopenharmony_cistatic int ehrpwm_pwm_probe(struct platform_device *pdev)
4218c2ecf20Sopenharmony_ci{
4228c2ecf20Sopenharmony_ci	struct device_node *np = pdev->dev.of_node;
4238c2ecf20Sopenharmony_ci	struct ehrpwm_pwm_chip *pc;
4248c2ecf20Sopenharmony_ci	struct resource *r;
4258c2ecf20Sopenharmony_ci	struct clk *clk;
4268c2ecf20Sopenharmony_ci	int ret;
4278c2ecf20Sopenharmony_ci
4288c2ecf20Sopenharmony_ci	pc = devm_kzalloc(&pdev->dev, sizeof(*pc), GFP_KERNEL);
4298c2ecf20Sopenharmony_ci	if (!pc)
4308c2ecf20Sopenharmony_ci		return -ENOMEM;
4318c2ecf20Sopenharmony_ci
4328c2ecf20Sopenharmony_ci	clk = devm_clk_get(&pdev->dev, "fck");
4338c2ecf20Sopenharmony_ci	if (IS_ERR(clk)) {
4348c2ecf20Sopenharmony_ci		if (of_device_is_compatible(np, "ti,am33xx-ecap")) {
4358c2ecf20Sopenharmony_ci			dev_warn(&pdev->dev, "Binding is obsolete.\n");
4368c2ecf20Sopenharmony_ci			clk = devm_clk_get(pdev->dev.parent, "fck");
4378c2ecf20Sopenharmony_ci		}
4388c2ecf20Sopenharmony_ci	}
4398c2ecf20Sopenharmony_ci
4408c2ecf20Sopenharmony_ci	if (IS_ERR(clk)) {
4418c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "failed to get clock\n");
4428c2ecf20Sopenharmony_ci		return PTR_ERR(clk);
4438c2ecf20Sopenharmony_ci	}
4448c2ecf20Sopenharmony_ci
4458c2ecf20Sopenharmony_ci	pc->clk_rate = clk_get_rate(clk);
4468c2ecf20Sopenharmony_ci	if (!pc->clk_rate) {
4478c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "failed to get clock rate\n");
4488c2ecf20Sopenharmony_ci		return -EINVAL;
4498c2ecf20Sopenharmony_ci	}
4508c2ecf20Sopenharmony_ci
4518c2ecf20Sopenharmony_ci	pc->chip.dev = &pdev->dev;
4528c2ecf20Sopenharmony_ci	pc->chip.ops = &ehrpwm_pwm_ops;
4538c2ecf20Sopenharmony_ci	pc->chip.of_xlate = of_pwm_xlate_with_flags;
4548c2ecf20Sopenharmony_ci	pc->chip.of_pwm_n_cells = 3;
4558c2ecf20Sopenharmony_ci	pc->chip.base = -1;
4568c2ecf20Sopenharmony_ci	pc->chip.npwm = NUM_PWM_CHANNEL;
4578c2ecf20Sopenharmony_ci
4588c2ecf20Sopenharmony_ci	r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
4598c2ecf20Sopenharmony_ci	pc->mmio_base = devm_ioremap_resource(&pdev->dev, r);
4608c2ecf20Sopenharmony_ci	if (IS_ERR(pc->mmio_base))
4618c2ecf20Sopenharmony_ci		return PTR_ERR(pc->mmio_base);
4628c2ecf20Sopenharmony_ci
4638c2ecf20Sopenharmony_ci	/* Acquire tbclk for Time Base EHRPWM submodule */
4648c2ecf20Sopenharmony_ci	pc->tbclk = devm_clk_get(&pdev->dev, "tbclk");
4658c2ecf20Sopenharmony_ci	if (IS_ERR(pc->tbclk)) {
4668c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "Failed to get tbclk\n");
4678c2ecf20Sopenharmony_ci		return PTR_ERR(pc->tbclk);
4688c2ecf20Sopenharmony_ci	}
4698c2ecf20Sopenharmony_ci
4708c2ecf20Sopenharmony_ci	ret = clk_prepare(pc->tbclk);
4718c2ecf20Sopenharmony_ci	if (ret < 0) {
4728c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "clk_prepare() failed: %d\n", ret);
4738c2ecf20Sopenharmony_ci		return ret;
4748c2ecf20Sopenharmony_ci	}
4758c2ecf20Sopenharmony_ci
4768c2ecf20Sopenharmony_ci	ret = pwmchip_add(&pc->chip);
4778c2ecf20Sopenharmony_ci	if (ret < 0) {
4788c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "pwmchip_add() failed: %d\n", ret);
4798c2ecf20Sopenharmony_ci		goto err_clk_unprepare;
4808c2ecf20Sopenharmony_ci	}
4818c2ecf20Sopenharmony_ci
4828c2ecf20Sopenharmony_ci	platform_set_drvdata(pdev, pc);
4838c2ecf20Sopenharmony_ci	pm_runtime_enable(&pdev->dev);
4848c2ecf20Sopenharmony_ci
4858c2ecf20Sopenharmony_ci	return 0;
4868c2ecf20Sopenharmony_ci
4878c2ecf20Sopenharmony_cierr_clk_unprepare:
4888c2ecf20Sopenharmony_ci	clk_unprepare(pc->tbclk);
4898c2ecf20Sopenharmony_ci
4908c2ecf20Sopenharmony_ci	return ret;
4918c2ecf20Sopenharmony_ci}
4928c2ecf20Sopenharmony_ci
4938c2ecf20Sopenharmony_cistatic int ehrpwm_pwm_remove(struct platform_device *pdev)
4948c2ecf20Sopenharmony_ci{
4958c2ecf20Sopenharmony_ci	struct ehrpwm_pwm_chip *pc = platform_get_drvdata(pdev);
4968c2ecf20Sopenharmony_ci
4978c2ecf20Sopenharmony_ci	clk_unprepare(pc->tbclk);
4988c2ecf20Sopenharmony_ci
4998c2ecf20Sopenharmony_ci	pm_runtime_disable(&pdev->dev);
5008c2ecf20Sopenharmony_ci
5018c2ecf20Sopenharmony_ci	return pwmchip_remove(&pc->chip);
5028c2ecf20Sopenharmony_ci}
5038c2ecf20Sopenharmony_ci
5048c2ecf20Sopenharmony_ci#ifdef CONFIG_PM_SLEEP
5058c2ecf20Sopenharmony_cistatic void ehrpwm_pwm_save_context(struct ehrpwm_pwm_chip *pc)
5068c2ecf20Sopenharmony_ci{
5078c2ecf20Sopenharmony_ci	pm_runtime_get_sync(pc->chip.dev);
5088c2ecf20Sopenharmony_ci
5098c2ecf20Sopenharmony_ci	pc->ctx.tbctl = ehrpwm_read(pc->mmio_base, TBCTL);
5108c2ecf20Sopenharmony_ci	pc->ctx.tbprd = ehrpwm_read(pc->mmio_base, TBPRD);
5118c2ecf20Sopenharmony_ci	pc->ctx.cmpa = ehrpwm_read(pc->mmio_base, CMPA);
5128c2ecf20Sopenharmony_ci	pc->ctx.cmpb = ehrpwm_read(pc->mmio_base, CMPB);
5138c2ecf20Sopenharmony_ci	pc->ctx.aqctla = ehrpwm_read(pc->mmio_base, AQCTLA);
5148c2ecf20Sopenharmony_ci	pc->ctx.aqctlb = ehrpwm_read(pc->mmio_base, AQCTLB);
5158c2ecf20Sopenharmony_ci	pc->ctx.aqsfrc = ehrpwm_read(pc->mmio_base, AQSFRC);
5168c2ecf20Sopenharmony_ci	pc->ctx.aqcsfrc = ehrpwm_read(pc->mmio_base, AQCSFRC);
5178c2ecf20Sopenharmony_ci
5188c2ecf20Sopenharmony_ci	pm_runtime_put_sync(pc->chip.dev);
5198c2ecf20Sopenharmony_ci}
5208c2ecf20Sopenharmony_ci
5218c2ecf20Sopenharmony_cistatic void ehrpwm_pwm_restore_context(struct ehrpwm_pwm_chip *pc)
5228c2ecf20Sopenharmony_ci{
5238c2ecf20Sopenharmony_ci	ehrpwm_write(pc->mmio_base, TBPRD, pc->ctx.tbprd);
5248c2ecf20Sopenharmony_ci	ehrpwm_write(pc->mmio_base, CMPA, pc->ctx.cmpa);
5258c2ecf20Sopenharmony_ci	ehrpwm_write(pc->mmio_base, CMPB, pc->ctx.cmpb);
5268c2ecf20Sopenharmony_ci	ehrpwm_write(pc->mmio_base, AQCTLA, pc->ctx.aqctla);
5278c2ecf20Sopenharmony_ci	ehrpwm_write(pc->mmio_base, AQCTLB, pc->ctx.aqctlb);
5288c2ecf20Sopenharmony_ci	ehrpwm_write(pc->mmio_base, AQSFRC, pc->ctx.aqsfrc);
5298c2ecf20Sopenharmony_ci	ehrpwm_write(pc->mmio_base, AQCSFRC, pc->ctx.aqcsfrc);
5308c2ecf20Sopenharmony_ci	ehrpwm_write(pc->mmio_base, TBCTL, pc->ctx.tbctl);
5318c2ecf20Sopenharmony_ci}
5328c2ecf20Sopenharmony_ci
5338c2ecf20Sopenharmony_cistatic int ehrpwm_pwm_suspend(struct device *dev)
5348c2ecf20Sopenharmony_ci{
5358c2ecf20Sopenharmony_ci	struct ehrpwm_pwm_chip *pc = dev_get_drvdata(dev);
5368c2ecf20Sopenharmony_ci	unsigned int i;
5378c2ecf20Sopenharmony_ci
5388c2ecf20Sopenharmony_ci	ehrpwm_pwm_save_context(pc);
5398c2ecf20Sopenharmony_ci
5408c2ecf20Sopenharmony_ci	for (i = 0; i < pc->chip.npwm; i++) {
5418c2ecf20Sopenharmony_ci		struct pwm_device *pwm = &pc->chip.pwms[i];
5428c2ecf20Sopenharmony_ci
5438c2ecf20Sopenharmony_ci		if (!pwm_is_enabled(pwm))
5448c2ecf20Sopenharmony_ci			continue;
5458c2ecf20Sopenharmony_ci
5468c2ecf20Sopenharmony_ci		/* Disable explicitly if PWM is running */
5478c2ecf20Sopenharmony_ci		pm_runtime_put_sync(dev);
5488c2ecf20Sopenharmony_ci	}
5498c2ecf20Sopenharmony_ci
5508c2ecf20Sopenharmony_ci	return 0;
5518c2ecf20Sopenharmony_ci}
5528c2ecf20Sopenharmony_ci
5538c2ecf20Sopenharmony_cistatic int ehrpwm_pwm_resume(struct device *dev)
5548c2ecf20Sopenharmony_ci{
5558c2ecf20Sopenharmony_ci	struct ehrpwm_pwm_chip *pc = dev_get_drvdata(dev);
5568c2ecf20Sopenharmony_ci	unsigned int i;
5578c2ecf20Sopenharmony_ci
5588c2ecf20Sopenharmony_ci	for (i = 0; i < pc->chip.npwm; i++) {
5598c2ecf20Sopenharmony_ci		struct pwm_device *pwm = &pc->chip.pwms[i];
5608c2ecf20Sopenharmony_ci
5618c2ecf20Sopenharmony_ci		if (!pwm_is_enabled(pwm))
5628c2ecf20Sopenharmony_ci			continue;
5638c2ecf20Sopenharmony_ci
5648c2ecf20Sopenharmony_ci		/* Enable explicitly if PWM was running */
5658c2ecf20Sopenharmony_ci		pm_runtime_get_sync(dev);
5668c2ecf20Sopenharmony_ci	}
5678c2ecf20Sopenharmony_ci
5688c2ecf20Sopenharmony_ci	ehrpwm_pwm_restore_context(pc);
5698c2ecf20Sopenharmony_ci
5708c2ecf20Sopenharmony_ci	return 0;
5718c2ecf20Sopenharmony_ci}
5728c2ecf20Sopenharmony_ci#endif
5738c2ecf20Sopenharmony_ci
5748c2ecf20Sopenharmony_cistatic SIMPLE_DEV_PM_OPS(ehrpwm_pwm_pm_ops, ehrpwm_pwm_suspend,
5758c2ecf20Sopenharmony_ci			 ehrpwm_pwm_resume);
5768c2ecf20Sopenharmony_ci
5778c2ecf20Sopenharmony_cistatic struct platform_driver ehrpwm_pwm_driver = {
5788c2ecf20Sopenharmony_ci	.driver = {
5798c2ecf20Sopenharmony_ci		.name = "ehrpwm",
5808c2ecf20Sopenharmony_ci		.of_match_table = ehrpwm_of_match,
5818c2ecf20Sopenharmony_ci		.pm = &ehrpwm_pwm_pm_ops,
5828c2ecf20Sopenharmony_ci	},
5838c2ecf20Sopenharmony_ci	.probe = ehrpwm_pwm_probe,
5848c2ecf20Sopenharmony_ci	.remove = ehrpwm_pwm_remove,
5858c2ecf20Sopenharmony_ci};
5868c2ecf20Sopenharmony_cimodule_platform_driver(ehrpwm_pwm_driver);
5878c2ecf20Sopenharmony_ci
5888c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("EHRPWM PWM driver");
5898c2ecf20Sopenharmony_ciMODULE_AUTHOR("Texas Instruments");
5908c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
591