0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013 #include <linux/clk.h>
0014 #include <linux/io.h>
0015 #include <linux/kernel.h>
0016 #include <linux/module.h>
0017 #include <linux/platform_device.h>
0018 #include <linux/pwm.h>
0019 #include <linux/slab.h>
0020
0021 #define BERLIN_PWM_EN 0x0
0022 #define BERLIN_PWM_ENABLE BIT(0)
0023 #define BERLIN_PWM_CONTROL 0x4
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035 #define BERLIN_PWM_PRESCALE_4096 0x7
0036 #define BERLIN_PWM_INVERT_POLARITY BIT(3)
0037 #define BERLIN_PWM_DUTY 0x8
0038 #define BERLIN_PWM_TCNT 0xc
0039 #define BERLIN_PWM_MAX_TCNT 65535
0040
0041 struct berlin_pwm_channel {
0042 u32 enable;
0043 u32 ctrl;
0044 u32 duty;
0045 u32 tcnt;
0046 };
0047
0048 struct berlin_pwm_chip {
0049 struct pwm_chip chip;
0050 struct clk *clk;
0051 void __iomem *base;
0052 };
0053
0054 static inline struct berlin_pwm_chip *to_berlin_pwm_chip(struct pwm_chip *chip)
0055 {
0056 return container_of(chip, struct berlin_pwm_chip, chip);
0057 }
0058
0059 static inline u32 berlin_pwm_readl(struct berlin_pwm_chip *bpc,
0060 unsigned int channel, unsigned long offset)
0061 {
0062 return readl_relaxed(bpc->base + channel * 0x10 + offset);
0063 }
0064
0065 static inline void berlin_pwm_writel(struct berlin_pwm_chip *bpc,
0066 unsigned int channel, u32 value,
0067 unsigned long offset)
0068 {
0069 writel_relaxed(value, bpc->base + channel * 0x10 + offset);
0070 }
0071
0072 static int berlin_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm)
0073 {
0074 struct berlin_pwm_channel *channel;
0075
0076 channel = kzalloc(sizeof(*channel), GFP_KERNEL);
0077 if (!channel)
0078 return -ENOMEM;
0079
0080 return pwm_set_chip_data(pwm, channel);
0081 }
0082
0083 static void berlin_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm)
0084 {
0085 struct berlin_pwm_channel *channel = pwm_get_chip_data(pwm);
0086
0087 kfree(channel);
0088 }
0089
0090 static int berlin_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
0091 u64 duty_ns, u64 period_ns)
0092 {
0093 struct berlin_pwm_chip *bpc = to_berlin_pwm_chip(chip);
0094 bool prescale_4096 = false;
0095 u32 value, duty, period;
0096 u64 cycles;
0097
0098 cycles = clk_get_rate(bpc->clk);
0099 cycles *= period_ns;
0100 do_div(cycles, NSEC_PER_SEC);
0101
0102 if (cycles > BERLIN_PWM_MAX_TCNT) {
0103 prescale_4096 = true;
0104 cycles >>= 12;
0105
0106 if (cycles > BERLIN_PWM_MAX_TCNT)
0107 return -ERANGE;
0108 }
0109
0110 period = cycles;
0111 cycles *= duty_ns;
0112 do_div(cycles, period_ns);
0113 duty = cycles;
0114
0115 value = berlin_pwm_readl(bpc, pwm->hwpwm, BERLIN_PWM_CONTROL);
0116 if (prescale_4096)
0117 value |= BERLIN_PWM_PRESCALE_4096;
0118 else
0119 value &= ~BERLIN_PWM_PRESCALE_4096;
0120 berlin_pwm_writel(bpc, pwm->hwpwm, value, BERLIN_PWM_CONTROL);
0121
0122 berlin_pwm_writel(bpc, pwm->hwpwm, duty, BERLIN_PWM_DUTY);
0123 berlin_pwm_writel(bpc, pwm->hwpwm, period, BERLIN_PWM_TCNT);
0124
0125 return 0;
0126 }
0127
0128 static int berlin_pwm_set_polarity(struct pwm_chip *chip,
0129 struct pwm_device *pwm,
0130 enum pwm_polarity polarity)
0131 {
0132 struct berlin_pwm_chip *bpc = to_berlin_pwm_chip(chip);
0133 u32 value;
0134
0135 value = berlin_pwm_readl(bpc, pwm->hwpwm, BERLIN_PWM_CONTROL);
0136
0137 if (polarity == PWM_POLARITY_NORMAL)
0138 value &= ~BERLIN_PWM_INVERT_POLARITY;
0139 else
0140 value |= BERLIN_PWM_INVERT_POLARITY;
0141
0142 berlin_pwm_writel(bpc, pwm->hwpwm, value, BERLIN_PWM_CONTROL);
0143
0144 return 0;
0145 }
0146
0147 static int berlin_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
0148 {
0149 struct berlin_pwm_chip *bpc = to_berlin_pwm_chip(chip);
0150 u32 value;
0151
0152 value = berlin_pwm_readl(bpc, pwm->hwpwm, BERLIN_PWM_EN);
0153 value |= BERLIN_PWM_ENABLE;
0154 berlin_pwm_writel(bpc, pwm->hwpwm, value, BERLIN_PWM_EN);
0155
0156 return 0;
0157 }
0158
0159 static void berlin_pwm_disable(struct pwm_chip *chip,
0160 struct pwm_device *pwm)
0161 {
0162 struct berlin_pwm_chip *bpc = to_berlin_pwm_chip(chip);
0163 u32 value;
0164
0165 value = berlin_pwm_readl(bpc, pwm->hwpwm, BERLIN_PWM_EN);
0166 value &= ~BERLIN_PWM_ENABLE;
0167 berlin_pwm_writel(bpc, pwm->hwpwm, value, BERLIN_PWM_EN);
0168 }
0169
0170 static int berlin_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
0171 const struct pwm_state *state)
0172 {
0173 int err;
0174 bool enabled = pwm->state.enabled;
0175
0176 if (state->polarity != pwm->state.polarity) {
0177 if (enabled) {
0178 berlin_pwm_disable(chip, pwm);
0179 enabled = false;
0180 }
0181
0182 err = berlin_pwm_set_polarity(chip, pwm, state->polarity);
0183 if (err)
0184 return err;
0185 }
0186
0187 if (!state->enabled) {
0188 if (enabled)
0189 berlin_pwm_disable(chip, pwm);
0190 return 0;
0191 }
0192
0193 err = berlin_pwm_config(chip, pwm, state->duty_cycle, state->period);
0194 if (err)
0195 return err;
0196
0197 if (!enabled)
0198 return berlin_pwm_enable(chip, pwm);
0199
0200 return 0;
0201 }
0202
0203 static const struct pwm_ops berlin_pwm_ops = {
0204 .request = berlin_pwm_request,
0205 .free = berlin_pwm_free,
0206 .apply = berlin_pwm_apply,
0207 .owner = THIS_MODULE,
0208 };
0209
0210 static const struct of_device_id berlin_pwm_match[] = {
0211 { .compatible = "marvell,berlin-pwm" },
0212 { },
0213 };
0214 MODULE_DEVICE_TABLE(of, berlin_pwm_match);
0215
0216 static int berlin_pwm_probe(struct platform_device *pdev)
0217 {
0218 struct berlin_pwm_chip *bpc;
0219 int ret;
0220
0221 bpc = devm_kzalloc(&pdev->dev, sizeof(*bpc), GFP_KERNEL);
0222 if (!bpc)
0223 return -ENOMEM;
0224
0225 bpc->base = devm_platform_ioremap_resource(pdev, 0);
0226 if (IS_ERR(bpc->base))
0227 return PTR_ERR(bpc->base);
0228
0229 bpc->clk = devm_clk_get(&pdev->dev, NULL);
0230 if (IS_ERR(bpc->clk))
0231 return PTR_ERR(bpc->clk);
0232
0233 ret = clk_prepare_enable(bpc->clk);
0234 if (ret)
0235 return ret;
0236
0237 bpc->chip.dev = &pdev->dev;
0238 bpc->chip.ops = &berlin_pwm_ops;
0239 bpc->chip.npwm = 4;
0240
0241 ret = pwmchip_add(&bpc->chip);
0242 if (ret < 0) {
0243 dev_err(&pdev->dev, "failed to add PWM chip: %d\n", ret);
0244 clk_disable_unprepare(bpc->clk);
0245 return ret;
0246 }
0247
0248 platform_set_drvdata(pdev, bpc);
0249
0250 return 0;
0251 }
0252
0253 static int berlin_pwm_remove(struct platform_device *pdev)
0254 {
0255 struct berlin_pwm_chip *bpc = platform_get_drvdata(pdev);
0256
0257 pwmchip_remove(&bpc->chip);
0258
0259 clk_disable_unprepare(bpc->clk);
0260
0261 return 0;
0262 }
0263
0264 #ifdef CONFIG_PM_SLEEP
0265 static int berlin_pwm_suspend(struct device *dev)
0266 {
0267 struct berlin_pwm_chip *bpc = dev_get_drvdata(dev);
0268 unsigned int i;
0269
0270 for (i = 0; i < bpc->chip.npwm; i++) {
0271 struct berlin_pwm_channel *channel;
0272
0273 channel = pwm_get_chip_data(&bpc->chip.pwms[i]);
0274 if (!channel)
0275 continue;
0276
0277 channel->enable = berlin_pwm_readl(bpc, i, BERLIN_PWM_ENABLE);
0278 channel->ctrl = berlin_pwm_readl(bpc, i, BERLIN_PWM_CONTROL);
0279 channel->duty = berlin_pwm_readl(bpc, i, BERLIN_PWM_DUTY);
0280 channel->tcnt = berlin_pwm_readl(bpc, i, BERLIN_PWM_TCNT);
0281 }
0282
0283 clk_disable_unprepare(bpc->clk);
0284
0285 return 0;
0286 }
0287
0288 static int berlin_pwm_resume(struct device *dev)
0289 {
0290 struct berlin_pwm_chip *bpc = dev_get_drvdata(dev);
0291 unsigned int i;
0292 int ret;
0293
0294 ret = clk_prepare_enable(bpc->clk);
0295 if (ret)
0296 return ret;
0297
0298 for (i = 0; i < bpc->chip.npwm; i++) {
0299 struct berlin_pwm_channel *channel;
0300
0301 channel = pwm_get_chip_data(&bpc->chip.pwms[i]);
0302 if (!channel)
0303 continue;
0304
0305 berlin_pwm_writel(bpc, i, channel->ctrl, BERLIN_PWM_CONTROL);
0306 berlin_pwm_writel(bpc, i, channel->duty, BERLIN_PWM_DUTY);
0307 berlin_pwm_writel(bpc, i, channel->tcnt, BERLIN_PWM_TCNT);
0308 berlin_pwm_writel(bpc, i, channel->enable, BERLIN_PWM_ENABLE);
0309 }
0310
0311 return 0;
0312 }
0313 #endif
0314
0315 static SIMPLE_DEV_PM_OPS(berlin_pwm_pm_ops, berlin_pwm_suspend,
0316 berlin_pwm_resume);
0317
0318 static struct platform_driver berlin_pwm_driver = {
0319 .probe = berlin_pwm_probe,
0320 .remove = berlin_pwm_remove,
0321 .driver = {
0322 .name = "berlin-pwm",
0323 .of_match_table = berlin_pwm_match,
0324 .pm = &berlin_pwm_pm_ops,
0325 },
0326 };
0327 module_platform_driver(berlin_pwm_driver);
0328
0329 MODULE_AUTHOR("Antoine Tenart <antoine.tenart@free-electrons.com>");
0330 MODULE_DESCRIPTION("Marvell Berlin PWM driver");
0331 MODULE_LICENSE("GPL v2");