Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Documentation/devicetree/bindings/pwm/sophgo,sg2042-pwm.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ allOf:

properties:
compatible:
const: sophgo,sg2042-pwm
enum:
- sophgo,sg2042-pwm
- sophgo,sg2044-pwm

reg:
maxItems: 1
Expand Down
141 changes: 124 additions & 17 deletions drivers/pwm/pwm-sophgo-sg2042.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* the running period.
* - When PERIOD and HLPERIOD is set to 0, the PWM wave output will
* be stopped and the output is pulled to high.
* - SG2044 supports both polarities, SG2042 only normal polarity.
* See the datasheet [1] for more details.
* [1]:https://github.com/sophgo/sophgo-doc/tree/main/SG2042/TRM
*/
Expand Down Expand Up @@ -41,6 +42,10 @@
#define SG2042_PWM_HLPERIOD(chan) ((chan) * 8 + 0)
#define SG2042_PWM_PERIOD(chan) ((chan) * 8 + 4)

#define SG2044_PWM_POLARITY 0x40
#define SG2044_PWM_PWMSTART 0x44
#define SG2044_PWM_OE 0xd0

#define SG2042_PWM_CHANNELNUM 4

/**
Expand All @@ -53,6 +58,10 @@ struct sg2042_pwm_ddata {
unsigned long clk_rate_hz;
};

struct sg2042_chip_data {
const struct pwm_ops ops;
};

/*
* period_ticks: PERIOD
* hlperiod_ticks: HLPERIOD
Expand All @@ -66,32 +75,40 @@ static void pwm_sg2042_config(struct sg2042_pwm_ddata *ddata, unsigned int chan,
writel(hlperiod_ticks, base + SG2042_PWM_HLPERIOD(chan));
}

static int pwm_sg2042_apply(struct pwm_chip *chip, struct pwm_device *pwm,
const struct pwm_state *state)
static void pwm_sg2042_set_dutycycle(struct pwm_chip *chip, struct pwm_device *pwm,
const struct pwm_state *state)
{
struct sg2042_pwm_ddata *ddata = pwmchip_get_drvdata(chip);
u32 hlperiod_ticks;
u32 period_ticks;

if (state->polarity == PWM_POLARITY_INVERSED)
return -EINVAL;

if (!state->enabled) {
pwm_sg2042_config(ddata, pwm->hwpwm, 0, 0);
return 0;
}

/*
* Duration of High level (duty_cycle) = HLPERIOD x Period_of_input_clk
* Duration of One Cycle (period) = PERIOD x Period_of_input_clk
*/
period_ticks = min(mul_u64_u64_div_u64(ddata->clk_rate_hz, state->period, NSEC_PER_SEC), U32_MAX);
hlperiod_ticks = min(mul_u64_u64_div_u64(ddata->clk_rate_hz, state->duty_cycle, NSEC_PER_SEC), U32_MAX);

dev_dbg(pwmchip_parent(chip), "chan[%u]: PERIOD=%u, HLPERIOD=%u\n",
pwm->hwpwm, period_ticks, hlperiod_ticks);
dev_dbg(pwmchip_parent(chip), "chan[%u]: ENABLE=%u, PERIOD=%u, HLPERIOD=%u, POLARITY=%u\n",
pwm->hwpwm, state->enabled, period_ticks, hlperiod_ticks, state->polarity);

pwm_sg2042_config(ddata, pwm->hwpwm, period_ticks, hlperiod_ticks);
}

static int pwm_sg2042_apply(struct pwm_chip *chip, struct pwm_device *pwm,
const struct pwm_state *state)
{
struct sg2042_pwm_ddata *ddata = pwmchip_get_drvdata(chip);

if (state->polarity == PWM_POLARITY_INVERSED)
return -EINVAL;

if (!state->enabled) {
pwm_sg2042_config(ddata, pwm->hwpwm, 0, 0);
return 0;
}

pwm_sg2042_set_dutycycle(chip, pwm, state);

return 0;
}
Expand Down Expand Up @@ -123,26 +140,115 @@ static int pwm_sg2042_get_state(struct pwm_chip *chip, struct pwm_device *pwm,
return 0;
}

static const struct pwm_ops pwm_sg2042_ops = {
.apply = pwm_sg2042_apply,
.get_state = pwm_sg2042_get_state,
static void pwm_sg2044_set_outputen(struct sg2042_pwm_ddata *ddata, struct pwm_device *pwm,
bool enabled)
{
u32 pwmstart;

pwmstart = readl(ddata->base + SG2044_PWM_PWMSTART);

if (enabled)
pwmstart |= BIT(pwm->hwpwm);
else
pwmstart &= ~BIT(pwm->hwpwm);

writel(pwmstart, ddata->base + SG2044_PWM_PWMSTART);
}

static void pwm_sg2044_set_outputdir(struct sg2042_pwm_ddata *ddata, struct pwm_device *pwm,
bool enabled)
{
u32 pwm_oe;

pwm_oe = readl(ddata->base + SG2044_PWM_OE);

if (enabled)
pwm_oe |= BIT(pwm->hwpwm);
else
pwm_oe &= ~BIT(pwm->hwpwm);

writel(pwm_oe, ddata->base + SG2044_PWM_OE);
}

static void pwm_sg2044_set_polarity(struct sg2042_pwm_ddata *ddata, struct pwm_device *pwm,
const struct pwm_state *state)
{
u32 pwm_polarity;

pwm_polarity = readl(ddata->base + SG2044_PWM_POLARITY);

if (state->polarity == PWM_POLARITY_NORMAL)
pwm_polarity &= ~BIT(pwm->hwpwm);
else
pwm_polarity |= BIT(pwm->hwpwm);

writel(pwm_polarity, ddata->base + SG2044_PWM_POLARITY);
}

static int pwm_sg2044_apply(struct pwm_chip *chip, struct pwm_device *pwm,
const struct pwm_state *state)
{
struct sg2042_pwm_ddata *ddata = pwmchip_get_drvdata(chip);

pwm_sg2044_set_polarity(ddata, pwm, state);

pwm_sg2042_set_dutycycle(chip, pwm, state);

/*
* re-enable PWMSTART to refresh the register period
*/
pwm_sg2044_set_outputen(ddata, pwm, false);

if (!state->enabled)
return 0;

pwm_sg2044_set_outputdir(ddata, pwm, true);
pwm_sg2044_set_outputen(ddata, pwm, true);

return 0;
}

static const struct sg2042_chip_data sg2042_chip_data = {
.ops = {
.apply = pwm_sg2042_apply,
.get_state = pwm_sg2042_get_state,
}
};

static const struct sg2042_chip_data sg2044_chip_data = {
.ops = {
.apply = pwm_sg2044_apply,
.get_state = pwm_sg2042_get_state,
}
};

static const struct of_device_id sg2042_pwm_ids[] = {
{ .compatible = "sophgo,sg2042-pwm" },
{
.compatible = "sophgo,sg2042-pwm",
.data = &sg2042_chip_data
},
{
.compatible = "sophgo,sg2044-pwm",
.data = &sg2044_chip_data
},
{ }
};
MODULE_DEVICE_TABLE(of, sg2042_pwm_ids);

static int pwm_sg2042_probe(struct platform_device *pdev)
{
struct device *dev = &pdev->dev;
const struct sg2042_chip_data *chip_data;
struct sg2042_pwm_ddata *ddata;
struct reset_control *rst;
struct pwm_chip *chip;
struct clk *clk;
int ret;

chip_data = device_get_match_data(dev);
if (!chip_data)
return -ENODEV;

chip = devm_pwmchip_alloc(dev, SG2042_PWM_CHANNELNUM, sizeof(*ddata));
if (IS_ERR(chip))
return PTR_ERR(chip);
Expand Down Expand Up @@ -170,7 +276,7 @@ static int pwm_sg2042_probe(struct platform_device *pdev)
if (IS_ERR(rst))
return dev_err_probe(dev, PTR_ERR(rst), "Failed to get reset\n");

chip->ops = &pwm_sg2042_ops;
chip->ops = &chip_data->ops;
chip->atomic = true;

ret = devm_pwmchip_add(dev, chip);
Expand All @@ -190,5 +296,6 @@ static struct platform_driver pwm_sg2042_driver = {
module_platform_driver(pwm_sg2042_driver);

MODULE_AUTHOR("Chen Wang");
MODULE_AUTHOR("Longbin Li <looong.bin@gmail.com>");
MODULE_DESCRIPTION("Sophgo SG2042 PWM driver");
MODULE_LICENSE("GPL");