Merge tag 'char-misc-6.12-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/gregk...
[linux-block.git] / drivers / pwm / pwm-clps711x.c
CommitLineData
f236d188 1// SPDX-License-Identifier: GPL-2.0+
7eb3f6ff
AS
2/*
3 * Cirrus Logic CLPS711X PWM driver
f236d188 4 * Author: Alexander Shiyan <shc_work@mail.ru>
7eb3f6ff
AS
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
14struct clps711x_chip {
7eb3f6ff
AS
15 void __iomem *pmpcon;
16 struct clk *clk;
17 spinlock_t lock;
18};
19
20static inline struct clps711x_chip *to_clps711x_chip(struct pwm_chip *chip)
21{
5412170f 22 return pwmchip_get_drvdata(chip);
7eb3f6ff
AS
23}
24
7eb3f6ff
AS
25static int clps711x_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm)
26{
27 struct clps711x_chip *priv = to_clps711x_chip(chip);
28 unsigned int freq = clk_get_rate(priv->clk);
29
30 if (!freq)
31 return -EINVAL;
32
33 /* Store constant period value */
e39c0df1 34 pwm->args.period = DIV_ROUND_CLOSEST(NSEC_PER_SEC, freq);
7eb3f6ff
AS
35
36 return 0;
37}
38
4225cd01
UKK
39static int clps711x_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
40 const struct pwm_state *state)
7eb3f6ff
AS
41{
42 struct clps711x_chip *priv = to_clps711x_chip(chip);
4225cd01
UKK
43 /* PWM0 - bits 4..7, PWM1 - bits 8..11 */
44 u32 shift = (pwm->hwpwm + 1) * 4;
45 unsigned long flags;
46 u32 pmpcon, val;
7eb3f6ff 47
4225cd01 48 if (state->polarity != PWM_POLARITY_NORMAL)
7eb3f6ff
AS
49 return -EINVAL;
50
4225cd01
UKK
51 if (state->period != pwm->args.period)
52 return -EINVAL;
7eb3f6ff 53
4225cd01
UKK
54 if (state->enabled)
55 val = mul_u64_u64_div_u64(state->duty_cycle, 0xf, state->period);
56 else
57 val = 0;
7eb3f6ff 58
4225cd01 59 spin_lock_irqsave(&priv->lock, flags);
7eb3f6ff 60
4225cd01
UKK
61 pmpcon = readl(priv->pmpcon);
62 pmpcon &= ~(0xf << shift);
63 pmpcon |= val << shift;
64 writel(pmpcon, priv->pmpcon);
7eb3f6ff 65
4225cd01 66 spin_unlock_irqrestore(&priv->lock, flags);
7eb3f6ff 67
4225cd01 68 return 0;
7eb3f6ff
AS
69}
70
71static const struct pwm_ops clps711x_pwm_ops = {
72 .request = clps711x_pwm_request,
4225cd01 73 .apply = clps711x_pwm_apply,
7eb3f6ff
AS
74};
75
7eb3f6ff
AS
76static int clps711x_pwm_probe(struct platform_device *pdev)
77{
5412170f 78 struct pwm_chip *chip;
7eb3f6ff 79 struct clps711x_chip *priv;
7eb3f6ff 80
5412170f
UKK
81 chip = devm_pwmchip_alloc(&pdev->dev, 2, sizeof(*priv));
82 if (IS_ERR(chip))
83 return PTR_ERR(chip);
84 priv = to_clps711x_chip(chip);
7eb3f6ff 85
3151b130 86 priv->pmpcon = devm_platform_ioremap_resource(pdev, 0);
7eb3f6ff
AS
87 if (IS_ERR(priv->pmpcon))
88 return PTR_ERR(priv->pmpcon);
89
90 priv->clk = devm_clk_get(&pdev->dev, NULL);
91 if (IS_ERR(priv->clk))
92 return PTR_ERR(priv->clk);
93
5412170f 94 chip->ops = &clps711x_pwm_ops;
7eb3f6ff
AS
95
96 spin_lock_init(&priv->lock);
97
5412170f 98 return devm_pwmchip_add(&pdev->dev, chip);
7eb3f6ff
AS
99}
100
101static const struct of_device_id __maybe_unused clps711x_pwm_dt_ids[] = {
ba60ae1d 102 { .compatible = "cirrus,ep7209-pwm", },
7eb3f6ff
AS
103 { }
104};
105MODULE_DEVICE_TABLE(of, clps711x_pwm_dt_ids);
106
107static struct platform_driver clps711x_pwm_driver = {
108 .driver = {
109 .name = "clps711x-pwm",
7eb3f6ff
AS
110 .of_match_table = of_match_ptr(clps711x_pwm_dt_ids),
111 },
112 .probe = clps711x_pwm_probe,
7eb3f6ff
AS
113};
114module_platform_driver(clps711x_pwm_driver);
115
116MODULE_AUTHOR("Alexander Shiyan <shc_work@mail.ru>");
117MODULE_DESCRIPTION("Cirrus Logic CLPS711X PWM driver");
118MODULE_LICENSE("GPL");