1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright 2014 Bart Tanghe <bart.tanghe@thomasmore.be>
9 #include <linux/module.h>
11 #include <linux/platform_device.h>
12 #include <linux/pwm.h>
14 #define PWM_CONTROL 0x000
15 #define PWM_CONTROL_SHIFT(x) ((x) * 8)
16 #define PWM_CONTROL_MASK 0xff
17 #define PWM_MODE 0x80 /* set timer in PWM mode */
18 #define PWM_ENABLE (1 << 0)
19 #define PWM_POLARITY (1 << 4)
21 #define PERIOD(x) (((x) * 0x10) + 0x10)
22 #define DUTY(x) (((x) * 0x10) + 0x14)
24 #define PERIOD_MIN 0x2
33 static inline struct bcm2835_pwm
*to_bcm2835_pwm(struct pwm_chip
*chip
)
35 return container_of(chip
, struct bcm2835_pwm
, chip
);
38 static int bcm2835_pwm_request(struct pwm_chip
*chip
, struct pwm_device
*pwm
)
40 struct bcm2835_pwm
*pc
= to_bcm2835_pwm(chip
);
43 value
= readl(pc
->base
+ PWM_CONTROL
);
44 value
&= ~(PWM_CONTROL_MASK
<< PWM_CONTROL_SHIFT(pwm
->hwpwm
));
45 value
|= (PWM_MODE
<< PWM_CONTROL_SHIFT(pwm
->hwpwm
));
46 writel(value
, pc
->base
+ PWM_CONTROL
);
51 static void bcm2835_pwm_free(struct pwm_chip
*chip
, struct pwm_device
*pwm
)
53 struct bcm2835_pwm
*pc
= to_bcm2835_pwm(chip
);
56 value
= readl(pc
->base
+ PWM_CONTROL
);
57 value
&= ~(PWM_CONTROL_MASK
<< PWM_CONTROL_SHIFT(pwm
->hwpwm
));
58 writel(value
, pc
->base
+ PWM_CONTROL
);
61 static int bcm2835_pwm_config(struct pwm_chip
*chip
, struct pwm_device
*pwm
,
62 int duty_ns
, int period_ns
)
64 struct bcm2835_pwm
*pc
= to_bcm2835_pwm(chip
);
65 unsigned long rate
= clk_get_rate(pc
->clk
);
70 dev_err(pc
->dev
, "failed to get clock rate\n");
74 scaler
= DIV_ROUND_CLOSEST(NSEC_PER_SEC
, rate
);
75 period
= DIV_ROUND_CLOSEST(period_ns
, scaler
);
77 if (period
< PERIOD_MIN
)
80 writel(DIV_ROUND_CLOSEST(duty_ns
, scaler
),
81 pc
->base
+ DUTY(pwm
->hwpwm
));
82 writel(period
, pc
->base
+ PERIOD(pwm
->hwpwm
));
87 static int bcm2835_pwm_enable(struct pwm_chip
*chip
, struct pwm_device
*pwm
)
89 struct bcm2835_pwm
*pc
= to_bcm2835_pwm(chip
);
92 value
= readl(pc
->base
+ PWM_CONTROL
);
93 value
|= PWM_ENABLE
<< PWM_CONTROL_SHIFT(pwm
->hwpwm
);
94 writel(value
, pc
->base
+ PWM_CONTROL
);
99 static void bcm2835_pwm_disable(struct pwm_chip
*chip
, struct pwm_device
*pwm
)
101 struct bcm2835_pwm
*pc
= to_bcm2835_pwm(chip
);
104 value
= readl(pc
->base
+ PWM_CONTROL
);
105 value
&= ~(PWM_ENABLE
<< PWM_CONTROL_SHIFT(pwm
->hwpwm
));
106 writel(value
, pc
->base
+ PWM_CONTROL
);
109 static int bcm2835_set_polarity(struct pwm_chip
*chip
, struct pwm_device
*pwm
,
110 enum pwm_polarity polarity
)
112 struct bcm2835_pwm
*pc
= to_bcm2835_pwm(chip
);
115 value
= readl(pc
->base
+ PWM_CONTROL
);
117 if (polarity
== PWM_POLARITY_NORMAL
)
118 value
&= ~(PWM_POLARITY
<< PWM_CONTROL_SHIFT(pwm
->hwpwm
));
120 value
|= PWM_POLARITY
<< PWM_CONTROL_SHIFT(pwm
->hwpwm
);
122 writel(value
, pc
->base
+ PWM_CONTROL
);
127 static const struct pwm_ops bcm2835_pwm_ops
= {
128 .request
= bcm2835_pwm_request
,
129 .free
= bcm2835_pwm_free
,
130 .config
= bcm2835_pwm_config
,
131 .enable
= bcm2835_pwm_enable
,
132 .disable
= bcm2835_pwm_disable
,
133 .set_polarity
= bcm2835_set_polarity
,
134 .owner
= THIS_MODULE
,
137 static int bcm2835_pwm_probe(struct platform_device
*pdev
)
139 struct bcm2835_pwm
*pc
;
140 struct resource
*res
;
143 pc
= devm_kzalloc(&pdev
->dev
, sizeof(*pc
), GFP_KERNEL
);
147 pc
->dev
= &pdev
->dev
;
149 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
150 pc
->base
= devm_ioremap_resource(&pdev
->dev
, res
);
151 if (IS_ERR(pc
->base
))
152 return PTR_ERR(pc
->base
);
154 pc
->clk
= devm_clk_get(&pdev
->dev
, NULL
);
155 if (IS_ERR(pc
->clk
)) {
156 ret
= PTR_ERR(pc
->clk
);
157 if (ret
!= -EPROBE_DEFER
)
158 dev_err(&pdev
->dev
, "clock not found: %d\n", ret
);
163 ret
= clk_prepare_enable(pc
->clk
);
167 pc
->chip
.dev
= &pdev
->dev
;
168 pc
->chip
.ops
= &bcm2835_pwm_ops
;
171 pc
->chip
.of_xlate
= of_pwm_xlate_with_flags
;
172 pc
->chip
.of_pwm_n_cells
= 3;
174 platform_set_drvdata(pdev
, pc
);
176 ret
= pwmchip_add(&pc
->chip
);
183 clk_disable_unprepare(pc
->clk
);
187 static int bcm2835_pwm_remove(struct platform_device
*pdev
)
189 struct bcm2835_pwm
*pc
= platform_get_drvdata(pdev
);
191 clk_disable_unprepare(pc
->clk
);
193 return pwmchip_remove(&pc
->chip
);
196 static const struct of_device_id bcm2835_pwm_of_match
[] = {
197 { .compatible
= "brcm,bcm2835-pwm", },
200 MODULE_DEVICE_TABLE(of
, bcm2835_pwm_of_match
);
202 static struct platform_driver bcm2835_pwm_driver
= {
204 .name
= "bcm2835-pwm",
205 .of_match_table
= bcm2835_pwm_of_match
,
207 .probe
= bcm2835_pwm_probe
,
208 .remove
= bcm2835_pwm_remove
,
210 module_platform_driver(bcm2835_pwm_driver
);
212 MODULE_AUTHOR("Bart Tanghe <bart.tanghe@thomasmore.be>");
213 MODULE_DESCRIPTION("Broadcom BCM2835 PWM driver");
214 MODULE_LICENSE("GPL v2");