| 1 |
// SPDX-License-Identifier: GPL-2.0+ |
| 2 |
/* |
| 3 |
* Cirrus Logic CLPS711X PWM driver |
| 4 |
* Author: Alexander Shiyan <[email protected]> |
| 5 |
*/ |
| 6 |
|
| 7 |
#include <linux/clk.h> |
| 8 |
#include <linux/io.h> |
| 9 |
#include <linux/module.h> |
| 10 |
#include <linux/of.h> |
| 11 |
#include <linux/platform_device.h> |
| 12 |
#include <linux/pwm.h> |
| 13 |
|
| 14 |
struct clps711x_chip { |
| 15 |
void __iomem *pmpcon; |
| 16 |
struct clk *clk; |
| 17 |
}; |
| 18 |
|
| 19 |
static inline struct clps711x_chip *to_clps711x_chip(struct pwm_chip *chip) |
| 20 |
{ |
| 21 |
return pwmchip_get_drvdata(chip); |
| 22 |
} |
| 23 |
|
| 24 |
static int clps711x_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm) |
| 25 |
{ |
| 26 |
struct clps711x_chip *priv = to_clps711x_chip(chip); |
| 27 |
unsigned int freq = clk_get_rate(priv->clk); |
| 28 |
|
| 29 |
if (!freq) |
| 30 |
return -EINVAL; |
| 31 |
|
| 32 |
/* Store constant period value */ |
| 33 |
pwm->args.period = DIV_ROUND_CLOSEST(NSEC_PER_SEC, freq); |
| 34 |
|
| 35 |
return 0; |
| 36 |
} |
| 37 |
|
| 38 |
static int clps711x_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm, |
| 39 |
const struct pwm_state *state) |
| 40 |
{ |
| 41 |
struct clps711x_chip *priv = to_clps711x_chip(chip); |
| 42 |
/* PWM0 - bits 4..7, PWM1 - bits 8..11 */ |
| 43 |
u32 shift = (pwm->hwpwm + 1) * 4; |
| 44 |
u32 pmpcon, val; |
| 45 |
|
| 46 |
if (state->polarity != PWM_POLARITY_NORMAL) |
| 47 |
return -EINVAL; |
| 48 |
|
| 49 |
if (state->period != pwm->args.period) |
| 50 |
return -EINVAL; |
| 51 |
|
| 52 |
if (state->enabled) |
| 53 |
val = mul_u64_u64_div_u64(state->duty_cycle, 0xf, state->period); |
| 54 |
else |
| 55 |
val = 0; |
| 56 |
|
| 57 |
pmpcon = readl(priv->pmpcon); |
| 58 |
pmpcon &= ~(0xf << shift); |
| 59 |
pmpcon |= val << shift; |
| 60 |
writel(pmpcon, priv->pmpcon); |
| 61 |
|
| 62 |
return 0; |
| 63 |
} |
| 64 |
|
| 65 |
static const struct pwm_ops clps711x_pwm_ops = { |
| 66 |
.request = clps711x_pwm_request, |
| 67 |
.apply = clps711x_pwm_apply, |
| 68 |
}; |
| 69 |
|
| 70 |
static int clps711x_pwm_probe(struct platform_device *pdev) |
| 71 |
{ |
| 72 |
struct pwm_chip *chip; |
| 73 |
struct clps711x_chip *priv; |
| 74 |
|
| 75 |
chip = devm_pwmchip_alloc(&pdev->dev, 2, sizeof(*priv)); |
| 76 |
if (IS_ERR(chip)) |
| 77 |
return PTR_ERR(chip); |
| 78 |
priv = to_clps711x_chip(chip); |
| 79 |
|
| 80 |
priv->pmpcon = devm_platform_ioremap_resource(pdev, 0); |
| 81 |
if (IS_ERR(priv->pmpcon)) |
| 82 |
return PTR_ERR(priv->pmpcon); |
| 83 |
|
| 84 |
priv->clk = devm_clk_get(&pdev->dev, NULL); |
| 85 |
if (IS_ERR(priv->clk)) |
| 86 |
return PTR_ERR(priv->clk); |
| 87 |
|
| 88 |
chip->ops = &clps711x_pwm_ops; |
| 89 |
|
| 90 |
return devm_pwmchip_add(&pdev->dev, chip); |
| 91 |
} |
| 92 |
|
| 93 |
static const struct of_device_id clps711x_pwm_dt_ids[] = { |
| 94 |
{ .compatible = "cirrus,ep7209-pwm", }, |
| 95 |
{ } |
| 96 |
}; |
| 97 |
MODULE_DEVICE_TABLE(of, clps711x_pwm_dt_ids); |
| 98 |
|
| 99 |
static struct platform_driver clps711x_pwm_driver = { |
| 100 |
.driver = { |
| 101 |
.name = "clps711x-pwm", |
| 102 |
.of_match_table = clps711x_pwm_dt_ids, |
| 103 |
}, |
| 104 |
.probe = clps711x_pwm_probe, |
| 105 |
}; |
| 106 |
module_platform_driver(clps711x_pwm_driver); |
| 107 |
|
| 108 |
MODULE_AUTHOR("Alexander Shiyan <[email protected]>"); |
| 109 |
MODULE_DESCRIPTION("Cirrus Logic CLPS711X PWM driver"); |
| 110 |
MODULE_LICENSE("GPL"); |