]> Git Repo - linux.git/blob - drivers/pwm/pwm-clps711x.c
Merge tag 'hid-for-linus-2024010801' of git://git.kernel.org/pub/scm/linux/kernel...
[linux.git] / drivers / pwm / pwm-clps711x.c
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         struct pwm_chip chip;
16         void __iomem *pmpcon;
17         struct clk *clk;
18         spinlock_t lock;
19 };
20
21 static inline struct clps711x_chip *to_clps711x_chip(struct pwm_chip *chip)
22 {
23         return container_of(chip, struct clps711x_chip, chip);
24 }
25
26 static int clps711x_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm)
27 {
28         struct clps711x_chip *priv = to_clps711x_chip(chip);
29         unsigned int freq = clk_get_rate(priv->clk);
30
31         if (!freq)
32                 return -EINVAL;
33
34         /* Store constant period value */
35         pwm->args.period = DIV_ROUND_CLOSEST(NSEC_PER_SEC, freq);
36
37         return 0;
38 }
39
40 static int clps711x_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
41                               const struct pwm_state *state)
42 {
43         struct clps711x_chip *priv = to_clps711x_chip(chip);
44         /* PWM0 - bits 4..7, PWM1 - bits 8..11 */
45         u32 shift = (pwm->hwpwm + 1) * 4;
46         unsigned long flags;
47         u32 pmpcon, val;
48
49         if (state->polarity != PWM_POLARITY_NORMAL)
50                 return -EINVAL;
51
52         if (state->period != pwm->args.period)
53                 return -EINVAL;
54
55         if (state->enabled)
56                 val = mul_u64_u64_div_u64(state->duty_cycle, 0xf, state->period);
57         else
58                 val = 0;
59
60         spin_lock_irqsave(&priv->lock, flags);
61
62         pmpcon = readl(priv->pmpcon);
63         pmpcon &= ~(0xf << shift);
64         pmpcon |= val << shift;
65         writel(pmpcon, priv->pmpcon);
66
67         spin_unlock_irqrestore(&priv->lock, flags);
68
69         return 0;
70 }
71
72 static const struct pwm_ops clps711x_pwm_ops = {
73         .request = clps711x_pwm_request,
74         .apply = clps711x_pwm_apply,
75 };
76
77 static struct pwm_device *clps711x_pwm_xlate(struct pwm_chip *chip,
78                                              const struct of_phandle_args *args)
79 {
80         if (args->args[0] >= chip->npwm)
81                 return ERR_PTR(-EINVAL);
82
83         return pwm_request_from_chip(chip, args->args[0], NULL);
84 }
85
86 static int clps711x_pwm_probe(struct platform_device *pdev)
87 {
88         struct clps711x_chip *priv;
89
90         priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
91         if (!priv)
92                 return -ENOMEM;
93
94         priv->pmpcon = devm_platform_ioremap_resource(pdev, 0);
95         if (IS_ERR(priv->pmpcon))
96                 return PTR_ERR(priv->pmpcon);
97
98         priv->clk = devm_clk_get(&pdev->dev, NULL);
99         if (IS_ERR(priv->clk))
100                 return PTR_ERR(priv->clk);
101
102         priv->chip.ops = &clps711x_pwm_ops;
103         priv->chip.dev = &pdev->dev;
104         priv->chip.npwm = 2;
105         priv->chip.of_xlate = clps711x_pwm_xlate;
106         priv->chip.of_pwm_n_cells = 1;
107
108         spin_lock_init(&priv->lock);
109
110         return devm_pwmchip_add(&pdev->dev, &priv->chip);
111 }
112
113 static const struct of_device_id __maybe_unused clps711x_pwm_dt_ids[] = {
114         { .compatible = "cirrus,ep7209-pwm", },
115         { }
116 };
117 MODULE_DEVICE_TABLE(of, clps711x_pwm_dt_ids);
118
119 static struct platform_driver clps711x_pwm_driver = {
120         .driver = {
121                 .name = "clps711x-pwm",
122                 .of_match_table = of_match_ptr(clps711x_pwm_dt_ids),
123         },
124         .probe = clps711x_pwm_probe,
125 };
126 module_platform_driver(clps711x_pwm_driver);
127
128 MODULE_AUTHOR("Alexander Shiyan <[email protected]>");
129 MODULE_DESCRIPTION("Cirrus Logic CLPS711X PWM driver");
130 MODULE_LICENSE("GPL");
This page took 0.040891 seconds and 4 git commands to generate.