blob: 6161e7e3e9ac617f17e44acd618eb52557e932af [file] [log] [blame]
Thomas Gleixnerf50a7f32019-05-28 09:57:18 -07001// SPDX-License-Identifier: GPL-2.0-only
Bo Shen32b16d42013-12-13 14:41:49 +08002/*
3 * Driver for Atmel Pulse Width Modulation Controller
4 *
5 * Copyright (C) 2013 Atmel Corporation
6 * Bo Shen <voice.shen@atmel.com>
Uwe Kleine-König3c269ba2019-08-24 02:10:36 +02007 *
8 * Links to reference manuals for the supported PWM chips can be found in
9 * Documentation/arm/microchip.rst.
Uwe Kleine-König998d1892019-08-24 02:10:39 +020010 *
11 * Limitations:
12 * - Periods start with the inactive level.
13 * - Hardware has to be stopped in general to update settings.
14 *
15 * Software bugs/possible improvements:
16 * - When atmel_pwm_apply() is called with state->enabled=false a change in
17 * state->polarity isn't honored.
18 * - Instead of sleeping to wait for a completed period, the interrupt
19 * functionality could be used.
Bo Shen32b16d42013-12-13 14:41:49 +080020 */
21
22#include <linux/clk.h>
Alexandre Belloni472ac3d2015-05-25 18:11:49 +020023#include <linux/delay.h>
Bo Shen32b16d42013-12-13 14:41:49 +080024#include <linux/err.h>
25#include <linux/io.h>
26#include <linux/module.h>
Alexandre Belloni472ac3d2015-05-25 18:11:49 +020027#include <linux/mutex.h>
Bo Shen32b16d42013-12-13 14:41:49 +080028#include <linux/of.h>
29#include <linux/of_device.h>
30#include <linux/platform_device.h>
31#include <linux/pwm.h>
32#include <linux/slab.h>
33
34/* The following is global registers for PWM controller */
35#define PWM_ENA 0x04
36#define PWM_DIS 0x08
37#define PWM_SR 0x0C
Alexandre Belloni472ac3d2015-05-25 18:11:49 +020038#define PWM_ISR 0x1C
Bo Shen32b16d42013-12-13 14:41:49 +080039/* Bit field in SR */
40#define PWM_SR_ALL_CH_ON 0x0F
41
42/* The following register is PWM channel related registers */
43#define PWM_CH_REG_OFFSET 0x200
44#define PWM_CH_REG_SIZE 0x20
45
46#define PWM_CMR 0x0
47/* Bit field in CMR */
48#define PWM_CMR_CPOL (1 << 9)
49#define PWM_CMR_UPD_CDTY (1 << 10)
Alexandre Belloni8db9e292014-03-14 15:19:08 +010050#define PWM_CMR_CPRE_MSK 0xF
Bo Shen32b16d42013-12-13 14:41:49 +080051
52/* The following registers for PWM v1 */
53#define PWMV1_CDTY 0x04
54#define PWMV1_CPRD 0x08
55#define PWMV1_CUPD 0x10
56
57/* The following registers for PWM v2 */
58#define PWMV2_CDTY 0x04
59#define PWMV2_CDTYUPD 0x08
60#define PWMV2_CPRD 0x0C
61#define PWMV2_CPRDUPD 0x10
62
Uwe Kleine-Königff55e7a2019-08-24 02:10:37 +020063#define PWM_MAX_PRES 10
64
Claudiu Beznea1a722aa2017-03-22 15:29:34 +020065struct atmel_pwm_registers {
66 u8 period;
67 u8 period_upd;
68 u8 duty;
69 u8 duty_upd;
70};
71
Claudiu Beznea0285827d2019-02-25 16:44:37 +000072struct atmel_pwm_config {
Uwe Kleine-König2101c872019-08-24 02:10:38 +020073 u32 period_bits;
Claudiu Beznea0285827d2019-02-25 16:44:37 +000074};
75
Claudiu Beznea53784152019-02-25 16:44:33 +000076struct atmel_pwm_data {
77 struct atmel_pwm_registers regs;
Claudiu Beznea0285827d2019-02-25 16:44:37 +000078 struct atmel_pwm_config cfg;
Claudiu Beznea53784152019-02-25 16:44:33 +000079};
80
Bo Shen32b16d42013-12-13 14:41:49 +080081struct atmel_pwm_chip {
82 struct pwm_chip chip;
83 struct clk *clk;
84 void __iomem *base;
Claudiu Beznea53784152019-02-25 16:44:33 +000085 const struct atmel_pwm_data *data;
Bo Shen32b16d42013-12-13 14:41:49 +080086
Alexandre Belloni472ac3d2015-05-25 18:11:49 +020087 unsigned int updated_pwms;
Thierry Reding313b78e2016-07-11 12:14:34 +020088 /* ISR is cleared when read, ensure only one thread does that */
89 struct mutex isr_lock;
Bo Shen32b16d42013-12-13 14:41:49 +080090};
91
92static inline struct atmel_pwm_chip *to_atmel_pwm_chip(struct pwm_chip *chip)
93{
94 return container_of(chip, struct atmel_pwm_chip, chip);
95}
96
97static inline u32 atmel_pwm_readl(struct atmel_pwm_chip *chip,
98 unsigned long offset)
99{
100 return readl_relaxed(chip->base + offset);
101}
102
103static inline void atmel_pwm_writel(struct atmel_pwm_chip *chip,
104 unsigned long offset, unsigned long val)
105{
106 writel_relaxed(val, chip->base + offset);
107}
108
109static inline u32 atmel_pwm_ch_readl(struct atmel_pwm_chip *chip,
110 unsigned int ch, unsigned long offset)
111{
112 unsigned long base = PWM_CH_REG_OFFSET + ch * PWM_CH_REG_SIZE;
113
Uwe Kleine-König02afb812019-08-24 02:10:40 +0200114 return atmel_pwm_readl(chip, base + offset);
Bo Shen32b16d42013-12-13 14:41:49 +0800115}
116
117static inline void atmel_pwm_ch_writel(struct atmel_pwm_chip *chip,
118 unsigned int ch, unsigned long offset,
119 unsigned long val)
120{
121 unsigned long base = PWM_CH_REG_OFFSET + ch * PWM_CH_REG_SIZE;
122
Uwe Kleine-König02afb812019-08-24 02:10:40 +0200123 atmel_pwm_writel(chip, base + offset, val);
Bo Shen32b16d42013-12-13 14:41:49 +0800124}
125
Claudiu Beznea1a722aa2017-03-22 15:29:34 +0200126static int atmel_pwm_calculate_cprd_and_pres(struct pwm_chip *chip,
127 const struct pwm_state *state,
128 unsigned long *cprd, u32 *pres)
Bo Shen32b16d42013-12-13 14:41:49 +0800129{
130 struct atmel_pwm_chip *atmel_pwm = to_atmel_pwm_chip(chip);
Claudiu Beznea1a722aa2017-03-22 15:29:34 +0200131 unsigned long long cycles = state->period;
Uwe Kleine-König2101c872019-08-24 02:10:38 +0200132 int shift;
Bo Shen32b16d42013-12-13 14:41:49 +0800133
Nikolaus Vosse2e08972014-09-23 15:30:21 +0200134 /* Calculate the period cycles and prescale value */
Claudiu Beznea1a722aa2017-03-22 15:29:34 +0200135 cycles *= clk_get_rate(atmel_pwm->clk);
136 do_div(cycles, NSEC_PER_SEC);
Bo Shen32b16d42013-12-13 14:41:49 +0800137
Uwe Kleine-König2101c872019-08-24 02:10:38 +0200138 /*
139 * The register for the period length is cfg.period_bits bits wide.
140 * So for each bit the number of clock cycles is wider divide the input
141 * clock frequency by two using pres and shift cprd accordingly.
142 */
143 shift = fls(cycles) - atmel_pwm->data->cfg.period_bits;
Bo Shen32b16d42013-12-13 14:41:49 +0800144
Uwe Kleine-König2101c872019-08-24 02:10:38 +0200145 if (shift > PWM_MAX_PRES) {
Nikolaus Vosse2e08972014-09-23 15:30:21 +0200146 dev_err(chip->dev, "pres exceeds the maximum value\n");
147 return -EINVAL;
Uwe Kleine-König2101c872019-08-24 02:10:38 +0200148 } else if (shift > 0) {
149 *pres = shift;
150 cycles >>= *pres;
151 } else {
152 *pres = 0;
Bo Shen32b16d42013-12-13 14:41:49 +0800153 }
154
Claudiu Beznea1a722aa2017-03-22 15:29:34 +0200155 *cprd = cycles;
Bo Shen32b16d42013-12-13 14:41:49 +0800156
Claudiu Beznea1a722aa2017-03-22 15:29:34 +0200157 return 0;
Bo Shen32b16d42013-12-13 14:41:49 +0800158}
159
Claudiu Beznea1a722aa2017-03-22 15:29:34 +0200160static void atmel_pwm_calculate_cdty(const struct pwm_state *state,
161 unsigned long cprd, unsigned long *cdty)
Bo Shen32b16d42013-12-13 14:41:49 +0800162{
Claudiu Beznea1a722aa2017-03-22 15:29:34 +0200163 unsigned long long cycles = state->duty_cycle;
Bo Shen32b16d42013-12-13 14:41:49 +0800164
Claudiu Beznea1a722aa2017-03-22 15:29:34 +0200165 cycles *= cprd;
166 do_div(cycles, state->period);
167 *cdty = cprd - cycles;
Bo Shen32b16d42013-12-13 14:41:49 +0800168}
169
Claudiu Beznea1a722aa2017-03-22 15:29:34 +0200170static void atmel_pwm_update_cdty(struct pwm_chip *chip, struct pwm_device *pwm,
171 unsigned long cdty)
Bo Shen32b16d42013-12-13 14:41:49 +0800172{
173 struct atmel_pwm_chip *atmel_pwm = to_atmel_pwm_chip(chip);
174 u32 val;
Bo Shen32b16d42013-12-13 14:41:49 +0800175
Claudiu Beznea53784152019-02-25 16:44:33 +0000176 if (atmel_pwm->data->regs.duty_upd ==
177 atmel_pwm->data->regs.period_upd) {
Claudiu Beznea1a722aa2017-03-22 15:29:34 +0200178 val = atmel_pwm_ch_readl(atmel_pwm, pwm->hwpwm, PWM_CMR);
179 val &= ~PWM_CMR_UPD_CDTY;
180 atmel_pwm_ch_writel(atmel_pwm, pwm->hwpwm, PWM_CMR, val);
Bo Shen32b16d42013-12-13 14:41:49 +0800181 }
182
Claudiu Beznea1a722aa2017-03-22 15:29:34 +0200183 atmel_pwm_ch_writel(atmel_pwm, pwm->hwpwm,
Claudiu Beznea53784152019-02-25 16:44:33 +0000184 atmel_pwm->data->regs.duty_upd, cdty);
Bo Shen32b16d42013-12-13 14:41:49 +0800185}
186
Claudiu Beznea1a722aa2017-03-22 15:29:34 +0200187static void atmel_pwm_set_cprd_cdty(struct pwm_chip *chip,
188 struct pwm_device *pwm,
189 unsigned long cprd, unsigned long cdty)
Bo Shen32b16d42013-12-13 14:41:49 +0800190{
191 struct atmel_pwm_chip *atmel_pwm = to_atmel_pwm_chip(chip);
Bo Shen32b16d42013-12-13 14:41:49 +0800192
Claudiu Beznea1a722aa2017-03-22 15:29:34 +0200193 atmel_pwm_ch_writel(atmel_pwm, pwm->hwpwm,
Claudiu Beznea53784152019-02-25 16:44:33 +0000194 atmel_pwm->data->regs.duty, cdty);
Claudiu Beznea1a722aa2017-03-22 15:29:34 +0200195 atmel_pwm_ch_writel(atmel_pwm, pwm->hwpwm,
Claudiu Beznea53784152019-02-25 16:44:33 +0000196 atmel_pwm->data->regs.period, cprd);
Bo Shen32b16d42013-12-13 14:41:49 +0800197}
198
Claudiu Beznea1a722aa2017-03-22 15:29:34 +0200199static void atmel_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm,
200 bool disable_clk)
Bo Shen32b16d42013-12-13 14:41:49 +0800201{
202 struct atmel_pwm_chip *atmel_pwm = to_atmel_pwm_chip(chip);
Alexandre Belloni472ac3d2015-05-25 18:11:49 +0200203 unsigned long timeout = jiffies + 2 * HZ;
Bo Shen32b16d42013-12-13 14:41:49 +0800204
Alexandre Belloni472ac3d2015-05-25 18:11:49 +0200205 /*
206 * Wait for at least a complete period to have passed before disabling a
207 * channel to be sure that CDTY has been updated
208 */
209 mutex_lock(&atmel_pwm->isr_lock);
210 atmel_pwm->updated_pwms |= atmel_pwm_readl(atmel_pwm, PWM_ISR);
211
212 while (!(atmel_pwm->updated_pwms & (1 << pwm->hwpwm)) &&
213 time_before(jiffies, timeout)) {
214 usleep_range(10, 100);
215 atmel_pwm->updated_pwms |= atmel_pwm_readl(atmel_pwm, PWM_ISR);
216 }
217
218 mutex_unlock(&atmel_pwm->isr_lock);
Bo Shen32b16d42013-12-13 14:41:49 +0800219 atmel_pwm_writel(atmel_pwm, PWM_DIS, 1 << pwm->hwpwm);
220
Guillermo Rodriguezf718c542016-05-13 13:09:37 +0200221 /*
222 * Wait for the PWM channel disable operation to be effective before
223 * stopping the clock.
224 */
225 timeout = jiffies + 2 * HZ;
226
227 while ((atmel_pwm_readl(atmel_pwm, PWM_SR) & (1 << pwm->hwpwm)) &&
228 time_before(jiffies, timeout))
229 usleep_range(10, 100);
230
Claudiu Beznea1a722aa2017-03-22 15:29:34 +0200231 if (disable_clk)
232 clk_disable(atmel_pwm->clk);
233}
234
235static int atmel_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
Uwe Kleine-König71523d12019-08-24 17:37:07 +0200236 const struct pwm_state *state)
Claudiu Beznea1a722aa2017-03-22 15:29:34 +0200237{
238 struct atmel_pwm_chip *atmel_pwm = to_atmel_pwm_chip(chip);
239 struct pwm_state cstate;
240 unsigned long cprd, cdty;
241 u32 pres, val;
242 int ret;
243
244 pwm_get_state(pwm, &cstate);
245
246 if (state->enabled) {
247 if (cstate.enabled &&
248 cstate.polarity == state->polarity &&
249 cstate.period == state->period) {
250 cprd = atmel_pwm_ch_readl(atmel_pwm, pwm->hwpwm,
Claudiu Beznea53784152019-02-25 16:44:33 +0000251 atmel_pwm->data->regs.period);
Claudiu Beznea1a722aa2017-03-22 15:29:34 +0200252 atmel_pwm_calculate_cdty(state, cprd, &cdty);
253 atmel_pwm_update_cdty(chip, pwm, cdty);
254 return 0;
255 }
256
257 ret = atmel_pwm_calculate_cprd_and_pres(chip, state, &cprd,
258 &pres);
259 if (ret) {
260 dev_err(chip->dev,
261 "failed to calculate cprd and prescaler\n");
262 return ret;
263 }
264
265 atmel_pwm_calculate_cdty(state, cprd, &cdty);
266
267 if (cstate.enabled) {
268 atmel_pwm_disable(chip, pwm, false);
269 } else {
270 ret = clk_enable(atmel_pwm->clk);
271 if (ret) {
272 dev_err(chip->dev, "failed to enable clock\n");
273 return ret;
274 }
275 }
276
277 /* It is necessary to preserve CPOL, inside CMR */
278 val = atmel_pwm_ch_readl(atmel_pwm, pwm->hwpwm, PWM_CMR);
279 val = (val & ~PWM_CMR_CPRE_MSK) | (pres & PWM_CMR_CPRE_MSK);
280 if (state->polarity == PWM_POLARITY_NORMAL)
281 val &= ~PWM_CMR_CPOL;
282 else
283 val |= PWM_CMR_CPOL;
284 atmel_pwm_ch_writel(atmel_pwm, pwm->hwpwm, PWM_CMR, val);
285 atmel_pwm_set_cprd_cdty(chip, pwm, cprd, cdty);
286 mutex_lock(&atmel_pwm->isr_lock);
287 atmel_pwm->updated_pwms |= atmel_pwm_readl(atmel_pwm, PWM_ISR);
288 atmel_pwm->updated_pwms &= ~(1 << pwm->hwpwm);
289 mutex_unlock(&atmel_pwm->isr_lock);
290 atmel_pwm_writel(atmel_pwm, PWM_ENA, 1 << pwm->hwpwm);
291 } else if (cstate.enabled) {
292 atmel_pwm_disable(chip, pwm, true);
293 }
294
295 return 0;
Bo Shen32b16d42013-12-13 14:41:49 +0800296}
297
Uwe Kleine-König651b5102019-08-24 02:10:41 +0200298static void atmel_pwm_get_state(struct pwm_chip *chip, struct pwm_device *pwm,
299 struct pwm_state *state)
300{
301 struct atmel_pwm_chip *atmel_pwm = to_atmel_pwm_chip(chip);
302 u32 sr, cmr;
303
304 sr = atmel_pwm_readl(atmel_pwm, PWM_SR);
305 cmr = atmel_pwm_ch_readl(atmel_pwm, pwm->hwpwm, PWM_CMR);
306
307 if (sr & (1 << pwm->hwpwm)) {
308 unsigned long rate = clk_get_rate(atmel_pwm->clk);
309 u32 cdty, cprd, pres;
310 u64 tmp;
311
312 pres = cmr & PWM_CMR_CPRE_MSK;
313
314 cprd = atmel_pwm_ch_readl(atmel_pwm, pwm->hwpwm,
315 atmel_pwm->data->regs.period);
316 tmp = (u64)cprd * NSEC_PER_SEC;
317 tmp <<= pres;
318 state->period = DIV64_U64_ROUND_UP(tmp, rate);
319
320 cdty = atmel_pwm_ch_readl(atmel_pwm, pwm->hwpwm,
321 atmel_pwm->data->regs.duty);
322 tmp = (u64)cdty * NSEC_PER_SEC;
323 tmp <<= pres;
324 state->duty_cycle = DIV64_U64_ROUND_UP(tmp, rate);
325
326 state->enabled = true;
327 } else {
328 state->enabled = false;
329 }
330
331 if (cmr & PWM_CMR_CPOL)
332 state->polarity = PWM_POLARITY_INVERSED;
333 else
334 state->polarity = PWM_POLARITY_NORMAL;
335}
336
Bo Shen32b16d42013-12-13 14:41:49 +0800337static const struct pwm_ops atmel_pwm_ops = {
Claudiu Beznea1a722aa2017-03-22 15:29:34 +0200338 .apply = atmel_pwm_apply,
Uwe Kleine-König651b5102019-08-24 02:10:41 +0200339 .get_state = atmel_pwm_get_state,
Bo Shen32b16d42013-12-13 14:41:49 +0800340 .owner = THIS_MODULE,
341};
342
Claudiu Bezneaabcbe372019-02-25 16:44:41 +0000343static const struct atmel_pwm_data atmel_sam9rl_pwm_data = {
Claudiu Beznea53784152019-02-25 16:44:33 +0000344 .regs = {
345 .period = PWMV1_CPRD,
346 .period_upd = PWMV1_CUPD,
347 .duty = PWMV1_CDTY,
348 .duty_upd = PWMV1_CUPD,
349 },
Claudiu Beznea0285827d2019-02-25 16:44:37 +0000350 .cfg = {
351 /* 16 bits to keep period and duty. */
Uwe Kleine-König2101c872019-08-24 02:10:38 +0200352 .period_bits = 16,
Claudiu Beznea0285827d2019-02-25 16:44:37 +0000353 },
Bo Shen32b16d42013-12-13 14:41:49 +0800354};
355
Claudiu Bezneaabcbe372019-02-25 16:44:41 +0000356static const struct atmel_pwm_data atmel_sama5_pwm_data = {
Claudiu Beznea53784152019-02-25 16:44:33 +0000357 .regs = {
358 .period = PWMV2_CPRD,
359 .period_upd = PWMV2_CPRDUPD,
360 .duty = PWMV2_CDTY,
361 .duty_upd = PWMV2_CDTYUPD,
362 },
Claudiu Beznea0285827d2019-02-25 16:44:37 +0000363 .cfg = {
364 /* 16 bits to keep period and duty. */
Uwe Kleine-König2101c872019-08-24 02:10:38 +0200365 .period_bits = 16,
Claudiu Beznea0285827d2019-02-25 16:44:37 +0000366 },
Bo Shen32b16d42013-12-13 14:41:49 +0800367};
368
Claudiu Beznea74d0c3b2019-02-25 16:44:45 +0000369static const struct atmel_pwm_data mchp_sam9x60_pwm_data = {
370 .regs = {
371 .period = PWMV1_CPRD,
372 .period_upd = PWMV1_CUPD,
373 .duty = PWMV1_CDTY,
374 .duty_upd = PWMV1_CUPD,
375 },
376 .cfg = {
377 /* 32 bits to keep period and duty. */
Uwe Kleine-König2101c872019-08-24 02:10:38 +0200378 .period_bits = 32,
Claudiu Beznea74d0c3b2019-02-25 16:44:45 +0000379 },
380};
381
Bo Shen32b16d42013-12-13 14:41:49 +0800382static const struct of_device_id atmel_pwm_dt_ids[] = {
383 {
384 .compatible = "atmel,at91sam9rl-pwm",
Claudiu Bezneaabcbe372019-02-25 16:44:41 +0000385 .data = &atmel_sam9rl_pwm_data,
Bo Shen32b16d42013-12-13 14:41:49 +0800386 }, {
387 .compatible = "atmel,sama5d3-pwm",
Claudiu Bezneaabcbe372019-02-25 16:44:41 +0000388 .data = &atmel_sama5_pwm_data,
Bo Shen32b16d42013-12-13 14:41:49 +0800389 }, {
Claudiu Beznea44521af2017-03-22 15:29:35 +0200390 .compatible = "atmel,sama5d2-pwm",
Claudiu Bezneaabcbe372019-02-25 16:44:41 +0000391 .data = &atmel_sama5_pwm_data,
Claudiu Beznea44521af2017-03-22 15:29:35 +0200392 }, {
Claudiu Beznea74d0c3b2019-02-25 16:44:45 +0000393 .compatible = "microchip,sam9x60-pwm",
394 .data = &mchp_sam9x60_pwm_data,
395 }, {
Bo Shen32b16d42013-12-13 14:41:49 +0800396 /* sentinel */
397 },
398};
399MODULE_DEVICE_TABLE(of, atmel_pwm_dt_ids);
400
Bo Shen32b16d42013-12-13 14:41:49 +0800401static int atmel_pwm_probe(struct platform_device *pdev)
402{
Bo Shen32b16d42013-12-13 14:41:49 +0800403 struct atmel_pwm_chip *atmel_pwm;
404 struct resource *res;
405 int ret;
406
Bo Shen32b16d42013-12-13 14:41:49 +0800407 atmel_pwm = devm_kzalloc(&pdev->dev, sizeof(*atmel_pwm), GFP_KERNEL);
408 if (!atmel_pwm)
409 return -ENOMEM;
410
Thierry Reding9193c162019-09-21 01:58:58 +0200411 mutex_init(&atmel_pwm->isr_lock);
Thierry Redingd85b9ce2019-09-21 01:55:48 +0200412 atmel_pwm->data = of_device_get_match_data(&pdev->dev);
Thierry Reding9193c162019-09-21 01:58:58 +0200413 atmel_pwm->updated_pwms = 0;
Thierry Redingd85b9ce2019-09-21 01:55:48 +0200414
Bo Shen32b16d42013-12-13 14:41:49 +0800415 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
416 atmel_pwm->base = devm_ioremap_resource(&pdev->dev, res);
417 if (IS_ERR(atmel_pwm->base))
418 return PTR_ERR(atmel_pwm->base);
419
420 atmel_pwm->clk = devm_clk_get(&pdev->dev, NULL);
421 if (IS_ERR(atmel_pwm->clk))
422 return PTR_ERR(atmel_pwm->clk);
423
424 ret = clk_prepare(atmel_pwm->clk);
425 if (ret) {
426 dev_err(&pdev->dev, "failed to prepare PWM clock\n");
427 return ret;
428 }
429
430 atmel_pwm->chip.dev = &pdev->dev;
431 atmel_pwm->chip.ops = &atmel_pwm_ops;
Kamel Bouhara3d4d8572019-09-18 16:57:16 +0200432 atmel_pwm->chip.of_xlate = of_pwm_xlate_with_flags;
433 atmel_pwm->chip.of_pwm_n_cells = 3;
Bo Shen32b16d42013-12-13 14:41:49 +0800434 atmel_pwm->chip.base = -1;
435 atmel_pwm->chip.npwm = 4;
Bo Shen32b16d42013-12-13 14:41:49 +0800436
437 ret = pwmchip_add(&atmel_pwm->chip);
438 if (ret < 0) {
439 dev_err(&pdev->dev, "failed to add PWM chip %d\n", ret);
440 goto unprepare_clk;
441 }
442
443 platform_set_drvdata(pdev, atmel_pwm);
444
Bo Shen6a683352013-12-19 11:42:22 +0800445 return ret;
446
Bo Shen32b16d42013-12-13 14:41:49 +0800447unprepare_clk:
448 clk_unprepare(atmel_pwm->clk);
449 return ret;
450}
451
452static int atmel_pwm_remove(struct platform_device *pdev)
453{
454 struct atmel_pwm_chip *atmel_pwm = platform_get_drvdata(pdev);
455
456 clk_unprepare(atmel_pwm->clk);
Alexandre Belloni472ac3d2015-05-25 18:11:49 +0200457 mutex_destroy(&atmel_pwm->isr_lock);
Bo Shen32b16d42013-12-13 14:41:49 +0800458
459 return pwmchip_remove(&atmel_pwm->chip);
460}
461
462static struct platform_driver atmel_pwm_driver = {
463 .driver = {
464 .name = "atmel-pwm",
465 .of_match_table = of_match_ptr(atmel_pwm_dt_ids),
466 },
Bo Shen32b16d42013-12-13 14:41:49 +0800467 .probe = atmel_pwm_probe,
468 .remove = atmel_pwm_remove,
469};
470module_platform_driver(atmel_pwm_driver);
471
472MODULE_ALIAS("platform:atmel-pwm");
473MODULE_AUTHOR("Bo Shen <voice.shen@atmel.com>");
474MODULE_DESCRIPTION("Atmel PWM driver");
475MODULE_LICENSE("GPL v2");