Bo Shen | 32b16d4 | 2013-12-13 14:41:49 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Driver for Atmel Pulse Width Modulation Controller |
| 3 | * |
| 4 | * Copyright (C) 2013 Atmel Corporation |
| 5 | * Bo Shen <voice.shen@atmel.com> |
| 6 | * |
| 7 | * Licensed under GPLv2. |
| 8 | */ |
| 9 | |
| 10 | #include <linux/clk.h> |
Alexandre Belloni | 472ac3d | 2015-05-25 18:11:49 +0200 | [diff] [blame] | 11 | #include <linux/delay.h> |
Bo Shen | 32b16d4 | 2013-12-13 14:41:49 +0800 | [diff] [blame] | 12 | #include <linux/err.h> |
| 13 | #include <linux/io.h> |
| 14 | #include <linux/module.h> |
Alexandre Belloni | 472ac3d | 2015-05-25 18:11:49 +0200 | [diff] [blame] | 15 | #include <linux/mutex.h> |
Bo Shen | 32b16d4 | 2013-12-13 14:41:49 +0800 | [diff] [blame] | 16 | #include <linux/of.h> |
| 17 | #include <linux/of_device.h> |
| 18 | #include <linux/platform_device.h> |
| 19 | #include <linux/pwm.h> |
| 20 | #include <linux/slab.h> |
| 21 | |
| 22 | /* The following is global registers for PWM controller */ |
| 23 | #define PWM_ENA 0x04 |
| 24 | #define PWM_DIS 0x08 |
| 25 | #define PWM_SR 0x0C |
Alexandre Belloni | 472ac3d | 2015-05-25 18:11:49 +0200 | [diff] [blame] | 26 | #define PWM_ISR 0x1C |
Bo Shen | 32b16d4 | 2013-12-13 14:41:49 +0800 | [diff] [blame] | 27 | /* Bit field in SR */ |
| 28 | #define PWM_SR_ALL_CH_ON 0x0F |
| 29 | |
| 30 | /* The following register is PWM channel related registers */ |
| 31 | #define PWM_CH_REG_OFFSET 0x200 |
| 32 | #define PWM_CH_REG_SIZE 0x20 |
| 33 | |
| 34 | #define PWM_CMR 0x0 |
| 35 | /* Bit field in CMR */ |
| 36 | #define PWM_CMR_CPOL (1 << 9) |
| 37 | #define PWM_CMR_UPD_CDTY (1 << 10) |
Alexandre Belloni | 8db9e29 | 2014-03-14 15:19:08 +0100 | [diff] [blame] | 38 | #define PWM_CMR_CPRE_MSK 0xF |
Bo Shen | 32b16d4 | 2013-12-13 14:41:49 +0800 | [diff] [blame] | 39 | |
| 40 | /* The following registers for PWM v1 */ |
| 41 | #define PWMV1_CDTY 0x04 |
| 42 | #define PWMV1_CPRD 0x08 |
| 43 | #define PWMV1_CUPD 0x10 |
| 44 | |
| 45 | /* The following registers for PWM v2 */ |
| 46 | #define PWMV2_CDTY 0x04 |
| 47 | #define PWMV2_CDTYUPD 0x08 |
| 48 | #define PWMV2_CPRD 0x0C |
| 49 | #define PWMV2_CPRDUPD 0x10 |
| 50 | |
Claudiu Beznea | 0285827d | 2019-02-25 16:44:37 +0000 | [diff] [blame] | 51 | /* Max values for period and prescaler */ |
| 52 | |
| 53 | /* Only the LSB 16 bits are significant. */ |
| 54 | #define PWM_MAXV1_PRD 0xFFFF |
| 55 | #define PRD_MAXV1_PRES 10 |
Bo Shen | 32b16d4 | 2013-12-13 14:41:49 +0800 | [diff] [blame] | 56 | |
Claudiu Beznea | 1a722aa | 2017-03-22 15:29:34 +0200 | [diff] [blame] | 57 | struct atmel_pwm_registers { |
| 58 | u8 period; |
| 59 | u8 period_upd; |
| 60 | u8 duty; |
| 61 | u8 duty_upd; |
| 62 | }; |
| 63 | |
Claudiu Beznea | 0285827d | 2019-02-25 16:44:37 +0000 | [diff] [blame] | 64 | struct atmel_pwm_config { |
| 65 | u32 max_period; |
| 66 | u32 max_pres; |
| 67 | }; |
| 68 | |
Claudiu Beznea | 5378415 | 2019-02-25 16:44:33 +0000 | [diff] [blame] | 69 | struct atmel_pwm_data { |
| 70 | struct atmel_pwm_registers regs; |
Claudiu Beznea | 0285827d | 2019-02-25 16:44:37 +0000 | [diff] [blame] | 71 | struct atmel_pwm_config cfg; |
Claudiu Beznea | 5378415 | 2019-02-25 16:44:33 +0000 | [diff] [blame] | 72 | }; |
| 73 | |
Bo Shen | 32b16d4 | 2013-12-13 14:41:49 +0800 | [diff] [blame] | 74 | struct atmel_pwm_chip { |
| 75 | struct pwm_chip chip; |
| 76 | struct clk *clk; |
| 77 | void __iomem *base; |
Claudiu Beznea | 5378415 | 2019-02-25 16:44:33 +0000 | [diff] [blame] | 78 | const struct atmel_pwm_data *data; |
Bo Shen | 32b16d4 | 2013-12-13 14:41:49 +0800 | [diff] [blame] | 79 | |
Alexandre Belloni | 472ac3d | 2015-05-25 18:11:49 +0200 | [diff] [blame] | 80 | unsigned int updated_pwms; |
Thierry Reding | 313b78e | 2016-07-11 12:14:34 +0200 | [diff] [blame] | 81 | /* ISR is cleared when read, ensure only one thread does that */ |
| 82 | struct mutex isr_lock; |
Bo Shen | 32b16d4 | 2013-12-13 14:41:49 +0800 | [diff] [blame] | 83 | }; |
| 84 | |
| 85 | static inline struct atmel_pwm_chip *to_atmel_pwm_chip(struct pwm_chip *chip) |
| 86 | { |
| 87 | return container_of(chip, struct atmel_pwm_chip, chip); |
| 88 | } |
| 89 | |
| 90 | static inline u32 atmel_pwm_readl(struct atmel_pwm_chip *chip, |
| 91 | unsigned long offset) |
| 92 | { |
| 93 | return readl_relaxed(chip->base + offset); |
| 94 | } |
| 95 | |
| 96 | static inline void atmel_pwm_writel(struct atmel_pwm_chip *chip, |
| 97 | unsigned long offset, unsigned long val) |
| 98 | { |
| 99 | writel_relaxed(val, chip->base + offset); |
| 100 | } |
| 101 | |
| 102 | static inline u32 atmel_pwm_ch_readl(struct atmel_pwm_chip *chip, |
| 103 | unsigned int ch, unsigned long offset) |
| 104 | { |
| 105 | unsigned long base = PWM_CH_REG_OFFSET + ch * PWM_CH_REG_SIZE; |
| 106 | |
| 107 | return readl_relaxed(chip->base + base + offset); |
| 108 | } |
| 109 | |
| 110 | static inline void atmel_pwm_ch_writel(struct atmel_pwm_chip *chip, |
| 111 | unsigned int ch, unsigned long offset, |
| 112 | unsigned long val) |
| 113 | { |
| 114 | unsigned long base = PWM_CH_REG_OFFSET + ch * PWM_CH_REG_SIZE; |
| 115 | |
| 116 | writel_relaxed(val, chip->base + base + offset); |
| 117 | } |
| 118 | |
Claudiu Beznea | 1a722aa | 2017-03-22 15:29:34 +0200 | [diff] [blame] | 119 | static int atmel_pwm_calculate_cprd_and_pres(struct pwm_chip *chip, |
| 120 | const struct pwm_state *state, |
| 121 | unsigned long *cprd, u32 *pres) |
Bo Shen | 32b16d4 | 2013-12-13 14:41:49 +0800 | [diff] [blame] | 122 | { |
| 123 | struct atmel_pwm_chip *atmel_pwm = to_atmel_pwm_chip(chip); |
Claudiu Beznea | 1a722aa | 2017-03-22 15:29:34 +0200 | [diff] [blame] | 124 | unsigned long long cycles = state->period; |
Bo Shen | 32b16d4 | 2013-12-13 14:41:49 +0800 | [diff] [blame] | 125 | |
Nikolaus Voss | e2e0897 | 2014-09-23 15:30:21 +0200 | [diff] [blame] | 126 | /* Calculate the period cycles and prescale value */ |
Claudiu Beznea | 1a722aa | 2017-03-22 15:29:34 +0200 | [diff] [blame] | 127 | cycles *= clk_get_rate(atmel_pwm->clk); |
| 128 | do_div(cycles, NSEC_PER_SEC); |
Bo Shen | 32b16d4 | 2013-12-13 14:41:49 +0800 | [diff] [blame] | 129 | |
Claudiu Beznea | 0285827d | 2019-02-25 16:44:37 +0000 | [diff] [blame] | 130 | for (*pres = 0; cycles > atmel_pwm->data->cfg.max_period; cycles >>= 1) |
Claudiu Beznea | 1a722aa | 2017-03-22 15:29:34 +0200 | [diff] [blame] | 131 | (*pres)++; |
Bo Shen | 32b16d4 | 2013-12-13 14:41:49 +0800 | [diff] [blame] | 132 | |
Claudiu Beznea | 0285827d | 2019-02-25 16:44:37 +0000 | [diff] [blame] | 133 | if (*pres > atmel_pwm->data->cfg.max_pres) { |
Nikolaus Voss | e2e0897 | 2014-09-23 15:30:21 +0200 | [diff] [blame] | 134 | dev_err(chip->dev, "pres exceeds the maximum value\n"); |
| 135 | return -EINVAL; |
Bo Shen | 32b16d4 | 2013-12-13 14:41:49 +0800 | [diff] [blame] | 136 | } |
| 137 | |
Claudiu Beznea | 1a722aa | 2017-03-22 15:29:34 +0200 | [diff] [blame] | 138 | *cprd = cycles; |
Bo Shen | 32b16d4 | 2013-12-13 14:41:49 +0800 | [diff] [blame] | 139 | |
Claudiu Beznea | 1a722aa | 2017-03-22 15:29:34 +0200 | [diff] [blame] | 140 | return 0; |
Bo Shen | 32b16d4 | 2013-12-13 14:41:49 +0800 | [diff] [blame] | 141 | } |
| 142 | |
Claudiu Beznea | 1a722aa | 2017-03-22 15:29:34 +0200 | [diff] [blame] | 143 | static void atmel_pwm_calculate_cdty(const struct pwm_state *state, |
| 144 | unsigned long cprd, unsigned long *cdty) |
Bo Shen | 32b16d4 | 2013-12-13 14:41:49 +0800 | [diff] [blame] | 145 | { |
Claudiu Beznea | 1a722aa | 2017-03-22 15:29:34 +0200 | [diff] [blame] | 146 | unsigned long long cycles = state->duty_cycle; |
Bo Shen | 32b16d4 | 2013-12-13 14:41:49 +0800 | [diff] [blame] | 147 | |
Claudiu Beznea | 1a722aa | 2017-03-22 15:29:34 +0200 | [diff] [blame] | 148 | cycles *= cprd; |
| 149 | do_div(cycles, state->period); |
| 150 | *cdty = cprd - cycles; |
Bo Shen | 32b16d4 | 2013-12-13 14:41:49 +0800 | [diff] [blame] | 151 | } |
| 152 | |
Claudiu Beznea | 1a722aa | 2017-03-22 15:29:34 +0200 | [diff] [blame] | 153 | static void atmel_pwm_update_cdty(struct pwm_chip *chip, struct pwm_device *pwm, |
| 154 | unsigned long cdty) |
Bo Shen | 32b16d4 | 2013-12-13 14:41:49 +0800 | [diff] [blame] | 155 | { |
| 156 | struct atmel_pwm_chip *atmel_pwm = to_atmel_pwm_chip(chip); |
| 157 | u32 val; |
Bo Shen | 32b16d4 | 2013-12-13 14:41:49 +0800 | [diff] [blame] | 158 | |
Claudiu Beznea | 5378415 | 2019-02-25 16:44:33 +0000 | [diff] [blame] | 159 | if (atmel_pwm->data->regs.duty_upd == |
| 160 | atmel_pwm->data->regs.period_upd) { |
Claudiu Beznea | 1a722aa | 2017-03-22 15:29:34 +0200 | [diff] [blame] | 161 | val = atmel_pwm_ch_readl(atmel_pwm, pwm->hwpwm, PWM_CMR); |
| 162 | val &= ~PWM_CMR_UPD_CDTY; |
| 163 | atmel_pwm_ch_writel(atmel_pwm, pwm->hwpwm, PWM_CMR, val); |
Bo Shen | 32b16d4 | 2013-12-13 14:41:49 +0800 | [diff] [blame] | 164 | } |
| 165 | |
Claudiu Beznea | 1a722aa | 2017-03-22 15:29:34 +0200 | [diff] [blame] | 166 | atmel_pwm_ch_writel(atmel_pwm, pwm->hwpwm, |
Claudiu Beznea | 5378415 | 2019-02-25 16:44:33 +0000 | [diff] [blame] | 167 | atmel_pwm->data->regs.duty_upd, cdty); |
Bo Shen | 32b16d4 | 2013-12-13 14:41:49 +0800 | [diff] [blame] | 168 | } |
| 169 | |
Claudiu Beznea | 1a722aa | 2017-03-22 15:29:34 +0200 | [diff] [blame] | 170 | static void atmel_pwm_set_cprd_cdty(struct pwm_chip *chip, |
| 171 | struct pwm_device *pwm, |
| 172 | unsigned long cprd, unsigned long cdty) |
Bo Shen | 32b16d4 | 2013-12-13 14:41:49 +0800 | [diff] [blame] | 173 | { |
| 174 | struct atmel_pwm_chip *atmel_pwm = to_atmel_pwm_chip(chip); |
Bo Shen | 32b16d4 | 2013-12-13 14:41:49 +0800 | [diff] [blame] | 175 | |
Claudiu Beznea | 1a722aa | 2017-03-22 15:29:34 +0200 | [diff] [blame] | 176 | atmel_pwm_ch_writel(atmel_pwm, pwm->hwpwm, |
Claudiu Beznea | 5378415 | 2019-02-25 16:44:33 +0000 | [diff] [blame] | 177 | atmel_pwm->data->regs.duty, cdty); |
Claudiu Beznea | 1a722aa | 2017-03-22 15:29:34 +0200 | [diff] [blame] | 178 | atmel_pwm_ch_writel(atmel_pwm, pwm->hwpwm, |
Claudiu Beznea | 5378415 | 2019-02-25 16:44:33 +0000 | [diff] [blame] | 179 | atmel_pwm->data->regs.period, cprd); |
Bo Shen | 32b16d4 | 2013-12-13 14:41:49 +0800 | [diff] [blame] | 180 | } |
| 181 | |
Claudiu Beznea | 1a722aa | 2017-03-22 15:29:34 +0200 | [diff] [blame] | 182 | static void atmel_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm, |
| 183 | bool disable_clk) |
Bo Shen | 32b16d4 | 2013-12-13 14:41:49 +0800 | [diff] [blame] | 184 | { |
| 185 | struct atmel_pwm_chip *atmel_pwm = to_atmel_pwm_chip(chip); |
Alexandre Belloni | 472ac3d | 2015-05-25 18:11:49 +0200 | [diff] [blame] | 186 | unsigned long timeout = jiffies + 2 * HZ; |
Bo Shen | 32b16d4 | 2013-12-13 14:41:49 +0800 | [diff] [blame] | 187 | |
Alexandre Belloni | 472ac3d | 2015-05-25 18:11:49 +0200 | [diff] [blame] | 188 | /* |
| 189 | * Wait for at least a complete period to have passed before disabling a |
| 190 | * channel to be sure that CDTY has been updated |
| 191 | */ |
| 192 | mutex_lock(&atmel_pwm->isr_lock); |
| 193 | atmel_pwm->updated_pwms |= atmel_pwm_readl(atmel_pwm, PWM_ISR); |
| 194 | |
| 195 | while (!(atmel_pwm->updated_pwms & (1 << pwm->hwpwm)) && |
| 196 | time_before(jiffies, timeout)) { |
| 197 | usleep_range(10, 100); |
| 198 | atmel_pwm->updated_pwms |= atmel_pwm_readl(atmel_pwm, PWM_ISR); |
| 199 | } |
| 200 | |
| 201 | mutex_unlock(&atmel_pwm->isr_lock); |
Bo Shen | 32b16d4 | 2013-12-13 14:41:49 +0800 | [diff] [blame] | 202 | atmel_pwm_writel(atmel_pwm, PWM_DIS, 1 << pwm->hwpwm); |
| 203 | |
Guillermo Rodriguez | f718c54 | 2016-05-13 13:09:37 +0200 | [diff] [blame] | 204 | /* |
| 205 | * Wait for the PWM channel disable operation to be effective before |
| 206 | * stopping the clock. |
| 207 | */ |
| 208 | timeout = jiffies + 2 * HZ; |
| 209 | |
| 210 | while ((atmel_pwm_readl(atmel_pwm, PWM_SR) & (1 << pwm->hwpwm)) && |
| 211 | time_before(jiffies, timeout)) |
| 212 | usleep_range(10, 100); |
| 213 | |
Claudiu Beznea | 1a722aa | 2017-03-22 15:29:34 +0200 | [diff] [blame] | 214 | if (disable_clk) |
| 215 | clk_disable(atmel_pwm->clk); |
| 216 | } |
| 217 | |
| 218 | static int atmel_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm, |
| 219 | struct pwm_state *state) |
| 220 | { |
| 221 | struct atmel_pwm_chip *atmel_pwm = to_atmel_pwm_chip(chip); |
| 222 | struct pwm_state cstate; |
| 223 | unsigned long cprd, cdty; |
| 224 | u32 pres, val; |
| 225 | int ret; |
| 226 | |
| 227 | pwm_get_state(pwm, &cstate); |
| 228 | |
| 229 | if (state->enabled) { |
| 230 | if (cstate.enabled && |
| 231 | cstate.polarity == state->polarity && |
| 232 | cstate.period == state->period) { |
| 233 | cprd = atmel_pwm_ch_readl(atmel_pwm, pwm->hwpwm, |
Claudiu Beznea | 5378415 | 2019-02-25 16:44:33 +0000 | [diff] [blame] | 234 | atmel_pwm->data->regs.period); |
Claudiu Beznea | 1a722aa | 2017-03-22 15:29:34 +0200 | [diff] [blame] | 235 | atmel_pwm_calculate_cdty(state, cprd, &cdty); |
| 236 | atmel_pwm_update_cdty(chip, pwm, cdty); |
| 237 | return 0; |
| 238 | } |
| 239 | |
| 240 | ret = atmel_pwm_calculate_cprd_and_pres(chip, state, &cprd, |
| 241 | &pres); |
| 242 | if (ret) { |
| 243 | dev_err(chip->dev, |
| 244 | "failed to calculate cprd and prescaler\n"); |
| 245 | return ret; |
| 246 | } |
| 247 | |
| 248 | atmel_pwm_calculate_cdty(state, cprd, &cdty); |
| 249 | |
| 250 | if (cstate.enabled) { |
| 251 | atmel_pwm_disable(chip, pwm, false); |
| 252 | } else { |
| 253 | ret = clk_enable(atmel_pwm->clk); |
| 254 | if (ret) { |
| 255 | dev_err(chip->dev, "failed to enable clock\n"); |
| 256 | return ret; |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | /* It is necessary to preserve CPOL, inside CMR */ |
| 261 | val = atmel_pwm_ch_readl(atmel_pwm, pwm->hwpwm, PWM_CMR); |
| 262 | val = (val & ~PWM_CMR_CPRE_MSK) | (pres & PWM_CMR_CPRE_MSK); |
| 263 | if (state->polarity == PWM_POLARITY_NORMAL) |
| 264 | val &= ~PWM_CMR_CPOL; |
| 265 | else |
| 266 | val |= PWM_CMR_CPOL; |
| 267 | atmel_pwm_ch_writel(atmel_pwm, pwm->hwpwm, PWM_CMR, val); |
| 268 | atmel_pwm_set_cprd_cdty(chip, pwm, cprd, cdty); |
| 269 | mutex_lock(&atmel_pwm->isr_lock); |
| 270 | atmel_pwm->updated_pwms |= atmel_pwm_readl(atmel_pwm, PWM_ISR); |
| 271 | atmel_pwm->updated_pwms &= ~(1 << pwm->hwpwm); |
| 272 | mutex_unlock(&atmel_pwm->isr_lock); |
| 273 | atmel_pwm_writel(atmel_pwm, PWM_ENA, 1 << pwm->hwpwm); |
| 274 | } else if (cstate.enabled) { |
| 275 | atmel_pwm_disable(chip, pwm, true); |
| 276 | } |
| 277 | |
| 278 | return 0; |
Bo Shen | 32b16d4 | 2013-12-13 14:41:49 +0800 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | static const struct pwm_ops atmel_pwm_ops = { |
Claudiu Beznea | 1a722aa | 2017-03-22 15:29:34 +0200 | [diff] [blame] | 282 | .apply = atmel_pwm_apply, |
Bo Shen | 32b16d4 | 2013-12-13 14:41:49 +0800 | [diff] [blame] | 283 | .owner = THIS_MODULE, |
| 284 | }; |
| 285 | |
Claudiu Beznea | abcbe37 | 2019-02-25 16:44:41 +0000 | [diff] [blame^] | 286 | static const struct atmel_pwm_data atmel_sam9rl_pwm_data = { |
Claudiu Beznea | 5378415 | 2019-02-25 16:44:33 +0000 | [diff] [blame] | 287 | .regs = { |
| 288 | .period = PWMV1_CPRD, |
| 289 | .period_upd = PWMV1_CUPD, |
| 290 | .duty = PWMV1_CDTY, |
| 291 | .duty_upd = PWMV1_CUPD, |
| 292 | }, |
Claudiu Beznea | 0285827d | 2019-02-25 16:44:37 +0000 | [diff] [blame] | 293 | .cfg = { |
| 294 | /* 16 bits to keep period and duty. */ |
| 295 | .max_period = PWM_MAXV1_PRD, |
| 296 | .max_pres = PRD_MAXV1_PRES, |
| 297 | }, |
Bo Shen | 32b16d4 | 2013-12-13 14:41:49 +0800 | [diff] [blame] | 298 | }; |
| 299 | |
Claudiu Beznea | abcbe37 | 2019-02-25 16:44:41 +0000 | [diff] [blame^] | 300 | static const struct atmel_pwm_data atmel_sama5_pwm_data = { |
Claudiu Beznea | 5378415 | 2019-02-25 16:44:33 +0000 | [diff] [blame] | 301 | .regs = { |
| 302 | .period = PWMV2_CPRD, |
| 303 | .period_upd = PWMV2_CPRDUPD, |
| 304 | .duty = PWMV2_CDTY, |
| 305 | .duty_upd = PWMV2_CDTYUPD, |
| 306 | }, |
Claudiu Beznea | 0285827d | 2019-02-25 16:44:37 +0000 | [diff] [blame] | 307 | .cfg = { |
| 308 | /* 16 bits to keep period and duty. */ |
| 309 | .max_period = PWM_MAXV1_PRD, |
| 310 | .max_pres = PRD_MAXV1_PRES, |
| 311 | }, |
Bo Shen | 32b16d4 | 2013-12-13 14:41:49 +0800 | [diff] [blame] | 312 | }; |
| 313 | |
| 314 | static const struct platform_device_id atmel_pwm_devtypes[] = { |
| 315 | { |
| 316 | .name = "at91sam9rl-pwm", |
Claudiu Beznea | abcbe37 | 2019-02-25 16:44:41 +0000 | [diff] [blame^] | 317 | .driver_data = (kernel_ulong_t)&atmel_sam9rl_pwm_data, |
Bo Shen | 32b16d4 | 2013-12-13 14:41:49 +0800 | [diff] [blame] | 318 | }, { |
| 319 | .name = "sama5d3-pwm", |
Claudiu Beznea | abcbe37 | 2019-02-25 16:44:41 +0000 | [diff] [blame^] | 320 | .driver_data = (kernel_ulong_t)&atmel_sama5_pwm_data, |
Bo Shen | 32b16d4 | 2013-12-13 14:41:49 +0800 | [diff] [blame] | 321 | }, { |
| 322 | /* sentinel */ |
| 323 | }, |
| 324 | }; |
| 325 | MODULE_DEVICE_TABLE(platform, atmel_pwm_devtypes); |
| 326 | |
| 327 | static const struct of_device_id atmel_pwm_dt_ids[] = { |
| 328 | { |
| 329 | .compatible = "atmel,at91sam9rl-pwm", |
Claudiu Beznea | abcbe37 | 2019-02-25 16:44:41 +0000 | [diff] [blame^] | 330 | .data = &atmel_sam9rl_pwm_data, |
Bo Shen | 32b16d4 | 2013-12-13 14:41:49 +0800 | [diff] [blame] | 331 | }, { |
| 332 | .compatible = "atmel,sama5d3-pwm", |
Claudiu Beznea | abcbe37 | 2019-02-25 16:44:41 +0000 | [diff] [blame^] | 333 | .data = &atmel_sama5_pwm_data, |
Bo Shen | 32b16d4 | 2013-12-13 14:41:49 +0800 | [diff] [blame] | 334 | }, { |
Claudiu Beznea | 44521af | 2017-03-22 15:29:35 +0200 | [diff] [blame] | 335 | .compatible = "atmel,sama5d2-pwm", |
Claudiu Beznea | abcbe37 | 2019-02-25 16:44:41 +0000 | [diff] [blame^] | 336 | .data = &atmel_sama5_pwm_data, |
Claudiu Beznea | 44521af | 2017-03-22 15:29:35 +0200 | [diff] [blame] | 337 | }, { |
Bo Shen | 32b16d4 | 2013-12-13 14:41:49 +0800 | [diff] [blame] | 338 | /* sentinel */ |
| 339 | }, |
| 340 | }; |
| 341 | MODULE_DEVICE_TABLE(of, atmel_pwm_dt_ids); |
| 342 | |
Claudiu Beznea | 5378415 | 2019-02-25 16:44:33 +0000 | [diff] [blame] | 343 | static inline const struct atmel_pwm_data * |
Bo Shen | 32b16d4 | 2013-12-13 14:41:49 +0800 | [diff] [blame] | 344 | atmel_pwm_get_driver_data(struct platform_device *pdev) |
| 345 | { |
Thierry Reding | 313b78e | 2016-07-11 12:14:34 +0200 | [diff] [blame] | 346 | const struct platform_device_id *id; |
| 347 | |
Thierry Reding | 017bb04 | 2016-07-11 12:16:01 +0200 | [diff] [blame] | 348 | if (pdev->dev.of_node) |
| 349 | return of_device_get_match_data(&pdev->dev); |
Thierry Reding | 313b78e | 2016-07-11 12:14:34 +0200 | [diff] [blame] | 350 | |
| 351 | id = platform_get_device_id(pdev); |
| 352 | |
Claudiu Beznea | 5378415 | 2019-02-25 16:44:33 +0000 | [diff] [blame] | 353 | return (struct atmel_pwm_data *)id->driver_data; |
Bo Shen | 32b16d4 | 2013-12-13 14:41:49 +0800 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | static int atmel_pwm_probe(struct platform_device *pdev) |
| 357 | { |
Claudiu Beznea | 5378415 | 2019-02-25 16:44:33 +0000 | [diff] [blame] | 358 | const struct atmel_pwm_data *data; |
Bo Shen | 32b16d4 | 2013-12-13 14:41:49 +0800 | [diff] [blame] | 359 | struct atmel_pwm_chip *atmel_pwm; |
| 360 | struct resource *res; |
| 361 | int ret; |
| 362 | |
Claudiu Beznea | 5378415 | 2019-02-25 16:44:33 +0000 | [diff] [blame] | 363 | data = atmel_pwm_get_driver_data(pdev); |
| 364 | if (!data) |
Bo Shen | 32b16d4 | 2013-12-13 14:41:49 +0800 | [diff] [blame] | 365 | return -ENODEV; |
| 366 | |
| 367 | atmel_pwm = devm_kzalloc(&pdev->dev, sizeof(*atmel_pwm), GFP_KERNEL); |
| 368 | if (!atmel_pwm) |
| 369 | return -ENOMEM; |
| 370 | |
| 371 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 372 | atmel_pwm->base = devm_ioremap_resource(&pdev->dev, res); |
| 373 | if (IS_ERR(atmel_pwm->base)) |
| 374 | return PTR_ERR(atmel_pwm->base); |
| 375 | |
| 376 | atmel_pwm->clk = devm_clk_get(&pdev->dev, NULL); |
| 377 | if (IS_ERR(atmel_pwm->clk)) |
| 378 | return PTR_ERR(atmel_pwm->clk); |
| 379 | |
| 380 | ret = clk_prepare(atmel_pwm->clk); |
| 381 | if (ret) { |
| 382 | dev_err(&pdev->dev, "failed to prepare PWM clock\n"); |
| 383 | return ret; |
| 384 | } |
| 385 | |
| 386 | atmel_pwm->chip.dev = &pdev->dev; |
| 387 | atmel_pwm->chip.ops = &atmel_pwm_ops; |
| 388 | |
| 389 | if (pdev->dev.of_node) { |
| 390 | atmel_pwm->chip.of_xlate = of_pwm_xlate_with_flags; |
| 391 | atmel_pwm->chip.of_pwm_n_cells = 3; |
| 392 | } |
| 393 | |
| 394 | atmel_pwm->chip.base = -1; |
| 395 | atmel_pwm->chip.npwm = 4; |
Claudiu Beznea | 5378415 | 2019-02-25 16:44:33 +0000 | [diff] [blame] | 396 | atmel_pwm->data = data; |
Alexandre Belloni | 472ac3d | 2015-05-25 18:11:49 +0200 | [diff] [blame] | 397 | atmel_pwm->updated_pwms = 0; |
| 398 | mutex_init(&atmel_pwm->isr_lock); |
Bo Shen | 32b16d4 | 2013-12-13 14:41:49 +0800 | [diff] [blame] | 399 | |
| 400 | ret = pwmchip_add(&atmel_pwm->chip); |
| 401 | if (ret < 0) { |
| 402 | dev_err(&pdev->dev, "failed to add PWM chip %d\n", ret); |
| 403 | goto unprepare_clk; |
| 404 | } |
| 405 | |
| 406 | platform_set_drvdata(pdev, atmel_pwm); |
| 407 | |
Bo Shen | 6a68335 | 2013-12-19 11:42:22 +0800 | [diff] [blame] | 408 | return ret; |
| 409 | |
Bo Shen | 32b16d4 | 2013-12-13 14:41:49 +0800 | [diff] [blame] | 410 | unprepare_clk: |
| 411 | clk_unprepare(atmel_pwm->clk); |
| 412 | return ret; |
| 413 | } |
| 414 | |
| 415 | static int atmel_pwm_remove(struct platform_device *pdev) |
| 416 | { |
| 417 | struct atmel_pwm_chip *atmel_pwm = platform_get_drvdata(pdev); |
| 418 | |
| 419 | clk_unprepare(atmel_pwm->clk); |
Alexandre Belloni | 472ac3d | 2015-05-25 18:11:49 +0200 | [diff] [blame] | 420 | mutex_destroy(&atmel_pwm->isr_lock); |
Bo Shen | 32b16d4 | 2013-12-13 14:41:49 +0800 | [diff] [blame] | 421 | |
| 422 | return pwmchip_remove(&atmel_pwm->chip); |
| 423 | } |
| 424 | |
| 425 | static struct platform_driver atmel_pwm_driver = { |
| 426 | .driver = { |
| 427 | .name = "atmel-pwm", |
| 428 | .of_match_table = of_match_ptr(atmel_pwm_dt_ids), |
| 429 | }, |
| 430 | .id_table = atmel_pwm_devtypes, |
| 431 | .probe = atmel_pwm_probe, |
| 432 | .remove = atmel_pwm_remove, |
| 433 | }; |
| 434 | module_platform_driver(atmel_pwm_driver); |
| 435 | |
| 436 | MODULE_ALIAS("platform:atmel-pwm"); |
| 437 | MODULE_AUTHOR("Bo Shen <voice.shen@atmel.com>"); |
| 438 | MODULE_DESCRIPTION("Atmel PWM driver"); |
| 439 | MODULE_LICENSE("GPL v2"); |