Blame view
drivers/pwm/pwm-lpc32xx.c
4.39 KB
2132fa8d9 pwm: add lpc32xx ... |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
/* * Copyright 2012 Alexandre Pereira da Silva <aletes.xgr@gmail.com> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; version 2. * */ #include <linux/clk.h> #include <linux/err.h> #include <linux/io.h> #include <linux/kernel.h> #include <linux/module.h> #include <linux/of.h> #include <linux/of_address.h> #include <linux/platform_device.h> #include <linux/pwm.h> #include <linux/slab.h> struct lpc32xx_pwm_chip { struct pwm_chip chip; struct clk *clk; void __iomem *base; }; |
5a9fc9c66 pwm: lpc32xx: fix... |
26 |
#define PWM_ENABLE BIT(31) |
acfd92fdf pwm: lpc32xx: Set... |
27 |
#define PWM_PIN_LEVEL BIT(30) |
2132fa8d9 pwm: add lpc32xx ... |
28 29 30 31 32 33 34 35 36 37 |
#define to_lpc32xx_pwm_chip(_chip) \ container_of(_chip, struct lpc32xx_pwm_chip, chip) static int lpc32xx_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm, int duty_ns, int period_ns) { struct lpc32xx_pwm_chip *lpc32xx = to_lpc32xx_pwm_chip(chip); unsigned long long c; int period_cycles, duty_cycles; |
affb923df pwm: lpc32xx: Don... |
38 |
u32 val; |
5a9fc9c66 pwm: lpc32xx: fix... |
39 |
c = clk_get_rate(lpc32xx->clk); |
2132fa8d9 pwm: add lpc32xx ... |
40 |
|
5a9fc9c66 pwm: lpc32xx: fix... |
41 42 43 |
/* The highest acceptable divisor is 256, which is represented by 0 */ period_cycles = div64_u64(c * period_ns, (unsigned long long)NSEC_PER_SEC * 256); |
d6dbdf0dd pwm: lpc32xx: ret... |
44 45 46 |
if (!period_cycles || period_cycles > 256) return -ERANGE; if (period_cycles == 256) |
5a9fc9c66 pwm: lpc32xx: fix... |
47 |
period_cycles = 0; |
2132fa8d9 pwm: add lpc32xx ... |
48 |
|
5a9fc9c66 pwm: lpc32xx: fix... |
49 50 51 52 53 54 55 |
/* Compute 256 x #duty/period value and care for corner cases */ duty_cycles = div64_u64((unsigned long long)(period_ns - duty_ns) * 256, period_ns); if (!duty_cycles) duty_cycles = 1; if (duty_cycles > 255) duty_cycles = 255; |
2132fa8d9 pwm: add lpc32xx ... |
56 |
|
affb923df pwm: lpc32xx: Don... |
57 58 |
val = readl(lpc32xx->base + (pwm->hwpwm << 2)); val &= ~0xFFFF; |
5a9fc9c66 pwm: lpc32xx: fix... |
59 |
val |= (period_cycles << 8) | duty_cycles; |
affb923df pwm: lpc32xx: Don... |
60 |
writel(val, lpc32xx->base + (pwm->hwpwm << 2)); |
2132fa8d9 pwm: add lpc32xx ... |
61 62 63 64 65 66 67 |
return 0; } static int lpc32xx_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm) { struct lpc32xx_pwm_chip *lpc32xx = to_lpc32xx_pwm_chip(chip); |
08ee77b5a pwm: lpc32xx: Pro... |
68 69 |
u32 val; int ret; |
82aff048d pwm: lpc32xx: mak... |
70 |
ret = clk_prepare_enable(lpc32xx->clk); |
08ee77b5a pwm: lpc32xx: Pro... |
71 72 73 74 75 76 |
if (ret) return ret; val = readl(lpc32xx->base + (pwm->hwpwm << 2)); val |= PWM_ENABLE; writel(val, lpc32xx->base + (pwm->hwpwm << 2)); |
2132fa8d9 pwm: add lpc32xx ... |
77 |
|
08ee77b5a pwm: lpc32xx: Pro... |
78 |
return 0; |
2132fa8d9 pwm: add lpc32xx ... |
79 80 81 82 83 |
} static void lpc32xx_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm) { struct lpc32xx_pwm_chip *lpc32xx = to_lpc32xx_pwm_chip(chip); |
08ee77b5a pwm: lpc32xx: Pro... |
84 85 86 87 88 |
u32 val; val = readl(lpc32xx->base + (pwm->hwpwm << 2)); val &= ~PWM_ENABLE; writel(val, lpc32xx->base + (pwm->hwpwm << 2)); |
2132fa8d9 pwm: add lpc32xx ... |
89 |
|
82aff048d pwm: lpc32xx: mak... |
90 |
clk_disable_unprepare(lpc32xx->clk); |
2132fa8d9 pwm: add lpc32xx ... |
91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
} static const struct pwm_ops lpc32xx_pwm_ops = { .config = lpc32xx_pwm_config, .enable = lpc32xx_pwm_enable, .disable = lpc32xx_pwm_disable, .owner = THIS_MODULE, }; static int lpc32xx_pwm_probe(struct platform_device *pdev) { struct lpc32xx_pwm_chip *lpc32xx; struct resource *res; int ret; |
acfd92fdf pwm: lpc32xx: Set... |
105 |
u32 val; |
2132fa8d9 pwm: add lpc32xx ... |
106 107 108 109 110 111 |
lpc32xx = devm_kzalloc(&pdev->dev, sizeof(*lpc32xx), GFP_KERNEL); if (!lpc32xx) return -ENOMEM; res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
6d4294d16 pwm: Convert to d... |
112 113 114 |
lpc32xx->base = devm_ioremap_resource(&pdev->dev, res); if (IS_ERR(lpc32xx->base)) return PTR_ERR(lpc32xx->base); |
2132fa8d9 pwm: add lpc32xx ... |
115 116 117 118 119 120 121 |
lpc32xx->clk = devm_clk_get(&pdev->dev, NULL); if (IS_ERR(lpc32xx->clk)) return PTR_ERR(lpc32xx->clk); lpc32xx->chip.dev = &pdev->dev; lpc32xx->chip.ops = &lpc32xx_pwm_ops; |
ebe1fca35 pwm: lpc32xx: cor... |
122 |
lpc32xx->chip.npwm = 1; |
8fc6d09dc pwm: lpc32xx: Set... |
123 |
lpc32xx->chip.base = -1; |
2132fa8d9 pwm: add lpc32xx ... |
124 125 126 127 128 129 130 |
ret = pwmchip_add(&lpc32xx->chip); if (ret < 0) { dev_err(&pdev->dev, "failed to add PWM chip, error %d ", ret); return ret; } |
acfd92fdf pwm: lpc32xx: Set... |
131 132 133 134 |
/* When PWM is disable, configure the output to the default value */ val = readl(lpc32xx->base + (lpc32xx->chip.pwms[0].hwpwm << 2)); val &= ~PWM_PIN_LEVEL; writel(val, lpc32xx->base + (lpc32xx->chip.pwms[0].hwpwm << 2)); |
2132fa8d9 pwm: add lpc32xx ... |
135 136 137 138 |
platform_set_drvdata(pdev, lpc32xx); return 0; } |
77f37917a pwm: remove use o... |
139 |
static int lpc32xx_pwm_remove(struct platform_device *pdev) |
2132fa8d9 pwm: add lpc32xx ... |
140 141 |
{ struct lpc32xx_pwm_chip *lpc32xx = platform_get_drvdata(pdev); |
54b2a999a pwm: lpc32xx: Pro... |
142 143 144 145 |
unsigned int i; for (i = 0; i < lpc32xx->chip.npwm; i++) pwm_disable(&lpc32xx->chip.pwms[i]); |
2132fa8d9 pwm: add lpc32xx ... |
146 |
|
2132fa8d9 pwm: add lpc32xx ... |
147 148 |
return pwmchip_remove(&lpc32xx->chip); } |
f1a8870ae pwm: Constify OF ... |
149 |
static const struct of_device_id lpc32xx_pwm_dt_ids[] = { |
2132fa8d9 pwm: add lpc32xx ... |
150 151 152 153 154 155 156 157 |
{ .compatible = "nxp,lpc3220-pwm", }, { /* sentinel */ } }; MODULE_DEVICE_TABLE(of, lpc32xx_pwm_dt_ids); static struct platform_driver lpc32xx_pwm_driver = { .driver = { .name = "lpc32xx-pwm", |
3cb3b2bfd pwm: lpc32xx: Rem... |
158 |
.of_match_table = lpc32xx_pwm_dt_ids, |
2132fa8d9 pwm: add lpc32xx ... |
159 160 |
}, .probe = lpc32xx_pwm_probe, |
fd1091125 pwm: remove use o... |
161 |
.remove = lpc32xx_pwm_remove, |
2132fa8d9 pwm: add lpc32xx ... |
162 163 164 165 166 167 168 |
}; module_platform_driver(lpc32xx_pwm_driver); MODULE_ALIAS("platform:lpc32xx-pwm"); MODULE_AUTHOR("Alexandre Pereira da Silva <aletes.xgr@gmail.com>"); MODULE_DESCRIPTION("LPC32XX PWM Driver"); MODULE_LICENSE("GPL v2"); |