2 * ST Microelectronics SPEAr Pulse Width Modulator driver
4 * Copyright (C) 2012 ST Microelectronics
5 * Shiraz Hashim <shiraz.hashim@st.com>
7 * This file is licensed under the terms of the GNU General Public
8 * License version 2. This program is licensed "as is" without any
9 * warranty of any kind, whether express or implied.
12 #include <linux/clk.h>
13 #include <linux/err.h>
15 #include <linux/ioport.h>
16 #include <linux/kernel.h>
17 #include <linux/math64.h>
18 #include <linux/module.h>
20 #include <linux/platform_device.h>
21 #include <linux/pwm.h>
22 #include <linux/slab.h>
23 #include <linux/types.h>
27 /* PWM registers and bits definitions */
28 #define PWMCR 0x00 /* Control Register */
29 #define PWMCR_PWM_ENABLE 0x1
30 #define PWMCR_PRESCALE_SHIFT 2
31 #define PWMCR_MIN_PRESCALE 0x00
32 #define PWMCR_MAX_PRESCALE 0x3FFF
34 #define PWMDCR 0x04 /* Duty Cycle Register */
35 #define PWMDCR_MIN_DUTY 0x0001
36 #define PWMDCR_MAX_DUTY 0xFFFF
38 #define PWMPCR 0x08 /* Period Register */
39 #define PWMPCR_MIN_PERIOD 0x0001
40 #define PWMPCR_MAX_PERIOD 0xFFFF
42 /* Following only available on 13xx SoCs */
43 #define PWMMCR 0x3C /* Master Control Register */
44 #define PWMMCR_PWM_ENABLE 0x1
47 * struct spear_pwm_chip - struct representing pwm chip
49 * @mmio_base: base address of pwm chip
50 * @clk: pointer to clk structure of pwm chip
51 * @chip: linux pwm chip representation
52 * @dev: pointer to device structure of pwm chip
54 struct spear_pwm_chip {
55 void __iomem *mmio_base;
61 static inline struct spear_pwm_chip *to_spear_pwm_chip(struct pwm_chip *chip)
63 return container_of(chip, struct spear_pwm_chip, chip);
66 static inline u32 spear_pwm_readl(struct spear_pwm_chip *chip, unsigned int num,
69 return readl_relaxed(chip->mmio_base + (num << 4) + offset);
72 static inline void spear_pwm_writel(struct spear_pwm_chip *chip,
73 unsigned int num, unsigned long offset,
76 writel_relaxed(val, chip->mmio_base + (num << 4) + offset);
79 static int spear_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
80 int duty_ns, int period_ns)
82 struct spear_pwm_chip *pc = to_spear_pwm_chip(chip);
83 u64 val, div, clk_rate;
84 unsigned long prescale = PWMCR_MIN_PRESCALE, pv, dc;
88 * Find pv, dc and prescale to suit duty_ns and period_ns. This is done
89 * according to formulas described below:
91 * period_ns = 10^9 * (PRESCALE + 1) * PV / PWM_CLK_RATE
92 * duty_ns = 10^9 * (PRESCALE + 1) * DC / PWM_CLK_RATE
94 * PV = (PWM_CLK_RATE * period_ns) / (10^9 * (PRESCALE + 1))
95 * DC = (PWM_CLK_RATE * duty_ns) / (10^9 * (PRESCALE + 1))
97 clk_rate = clk_get_rate(pc->clk);
101 val = clk_rate * period_ns;
102 pv = div64_u64(val, div);
103 val = clk_rate * duty_ns;
104 dc = div64_u64(val, div);
106 /* if duty_ns and period_ns are not achievable then return */
107 if (pv < PWMPCR_MIN_PERIOD || dc < PWMDCR_MIN_DUTY)
111 * if pv and dc have crossed their upper limit, then increase
112 * prescale and recalculate pv and dc.
114 if (pv > PWMPCR_MAX_PERIOD || dc > PWMDCR_MAX_DUTY) {
115 if (++prescale > PWMCR_MAX_PRESCALE)
123 * NOTE: the clock to PWM has to be enabled first before writing to the
126 ret = clk_enable(pc->clk);
130 spear_pwm_writel(pc, pwm->hwpwm, PWMCR,
131 prescale << PWMCR_PRESCALE_SHIFT);
132 spear_pwm_writel(pc, pwm->hwpwm, PWMDCR, dc);
133 spear_pwm_writel(pc, pwm->hwpwm, PWMPCR, pv);
134 clk_disable(pc->clk);
139 static int spear_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
141 struct spear_pwm_chip *pc = to_spear_pwm_chip(chip);
145 rc = clk_enable(pc->clk);
149 val = spear_pwm_readl(pc, pwm->hwpwm, PWMCR);
150 val |= PWMCR_PWM_ENABLE;
151 spear_pwm_writel(pc, pwm->hwpwm, PWMCR, val);
156 static void spear_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
158 struct spear_pwm_chip *pc = to_spear_pwm_chip(chip);
161 val = spear_pwm_readl(pc, pwm->hwpwm, PWMCR);
162 val &= ~PWMCR_PWM_ENABLE;
163 spear_pwm_writel(pc, pwm->hwpwm, PWMCR, val);
165 clk_disable(pc->clk);
168 static const struct pwm_ops spear_pwm_ops = {
169 .config = spear_pwm_config,
170 .enable = spear_pwm_enable,
171 .disable = spear_pwm_disable,
172 .owner = THIS_MODULE,
175 static int spear_pwm_probe(struct platform_device *pdev)
177 struct device_node *np = pdev->dev.of_node;
178 struct spear_pwm_chip *pc;
183 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
185 dev_err(&pdev->dev, "no memory resources defined\n");
189 pc = devm_kzalloc(&pdev->dev, sizeof(*pc), GFP_KERNEL);
191 dev_err(&pdev->dev, "failed to allocate memory\n");
195 pc->mmio_base = devm_ioremap_resource(&pdev->dev, r);
196 if (IS_ERR(pc->mmio_base))
197 return PTR_ERR(pc->mmio_base);
199 pc->clk = devm_clk_get(&pdev->dev, NULL);
201 return PTR_ERR(pc->clk);
203 pc->dev = &pdev->dev;
204 platform_set_drvdata(pdev, pc);
206 pc->chip.dev = &pdev->dev;
207 pc->chip.ops = &spear_pwm_ops;
209 pc->chip.npwm = NUM_PWM;
211 ret = clk_prepare(pc->clk);
215 if (of_device_is_compatible(np, "st,spear1340-pwm")) {
216 ret = clk_enable(pc->clk);
218 clk_unprepare(pc->clk);
222 * Following enables PWM chip, channels would still be
223 * enabled individually through their control register
225 val = readl_relaxed(pc->mmio_base + PWMMCR);
226 val |= PWMMCR_PWM_ENABLE;
227 writel_relaxed(val, pc->mmio_base + PWMMCR);
229 clk_disable(pc->clk);
232 ret = pwmchip_add(&pc->chip);
234 clk_unprepare(pc->clk);
235 dev_err(&pdev->dev, "pwmchip_add() failed: %d\n", ret);
241 static int spear_pwm_remove(struct platform_device *pdev)
243 struct spear_pwm_chip *pc = platform_get_drvdata(pdev);
246 for (i = 0; i < NUM_PWM; i++)
247 pwm_disable(&pc->chip.pwms[i]);
249 /* clk was prepared in probe, hence unprepare it here */
250 clk_unprepare(pc->clk);
251 return pwmchip_remove(&pc->chip);
254 static struct of_device_id spear_pwm_of_match[] = {
255 { .compatible = "st,spear320-pwm" },
256 { .compatible = "st,spear1340-pwm" },
260 MODULE_DEVICE_TABLE(of, spear_pwm_of_match);
262 static struct platform_driver spear_pwm_driver = {
265 .of_match_table = spear_pwm_of_match,
267 .probe = spear_pwm_probe,
268 .remove = spear_pwm_remove,
271 module_platform_driver(spear_pwm_driver);
273 MODULE_LICENSE("GPL");
274 MODULE_AUTHOR("Shiraz Hashim <shiraz.hashim@st.com>");
275 MODULE_AUTHOR("Viresh Kumar <viresh.kumar@linaro.com>");
276 MODULE_ALIAS("platform:spear-pwm");