Neil Armstrong | 1cdb441 | 2019-05-20 16:04:21 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause |
Neil Armstrong | 211ed63 | 2016-08-22 17:36:30 +0200 | [diff] [blame] | 2 | /* |
Neil Armstrong | 211ed63 | 2016-08-22 17:36:30 +0200 | [diff] [blame] | 3 | * Copyright (c) 2016 BayLibre, SAS. |
| 4 | * Author: Neil Armstrong <narmstrong@baylibre.com> |
| 5 | * Copyright (C) 2014 Amlogic, Inc. |
Neil Armstrong | 211ed63 | 2016-08-22 17:36:30 +0200 | [diff] [blame] | 6 | */ |
| 7 | |
Martin Blumenstingl | 181164b | 2019-06-12 21:59:00 +0200 | [diff] [blame] | 8 | #include <linux/bitfield.h> |
| 9 | #include <linux/bits.h> |
Neil Armstrong | 211ed63 | 2016-08-22 17:36:30 +0200 | [diff] [blame] | 10 | #include <linux/clk.h> |
| 11 | #include <linux/clk-provider.h> |
| 12 | #include <linux/err.h> |
| 13 | #include <linux/io.h> |
| 14 | #include <linux/kernel.h> |
Martin Blumenstingl | fb2081e | 2019-06-12 21:59:07 +0200 | [diff] [blame^] | 15 | #include <linux/math64.h> |
Neil Armstrong | 211ed63 | 2016-08-22 17:36:30 +0200 | [diff] [blame] | 16 | #include <linux/module.h> |
| 17 | #include <linux/of.h> |
| 18 | #include <linux/of_device.h> |
| 19 | #include <linux/platform_device.h> |
| 20 | #include <linux/pwm.h> |
| 21 | #include <linux/slab.h> |
| 22 | #include <linux/spinlock.h> |
| 23 | |
| 24 | #define REG_PWM_A 0x0 |
| 25 | #define REG_PWM_B 0x4 |
Martin Blumenstingl | 181164b | 2019-06-12 21:59:00 +0200 | [diff] [blame] | 26 | #define PWM_LOW_MASK GENMASK(15, 0) |
| 27 | #define PWM_HIGH_MASK GENMASK(31, 16) |
Neil Armstrong | 211ed63 | 2016-08-22 17:36:30 +0200 | [diff] [blame] | 28 | |
| 29 | #define REG_MISC_AB 0x8 |
| 30 | #define MISC_B_CLK_EN BIT(23) |
| 31 | #define MISC_A_CLK_EN BIT(15) |
| 32 | #define MISC_CLK_DIV_MASK 0x7f |
| 33 | #define MISC_B_CLK_DIV_SHIFT 16 |
| 34 | #define MISC_A_CLK_DIV_SHIFT 8 |
| 35 | #define MISC_B_CLK_SEL_SHIFT 6 |
| 36 | #define MISC_A_CLK_SEL_SHIFT 4 |
Martin Blumenstingl | 33cefd8 | 2019-06-12 21:59:01 +0200 | [diff] [blame] | 37 | #define MISC_CLK_SEL_MASK 0x3 |
Neil Armstrong | 211ed63 | 2016-08-22 17:36:30 +0200 | [diff] [blame] | 38 | #define MISC_B_EN BIT(1) |
| 39 | #define MISC_A_EN BIT(0) |
| 40 | |
Martin Blumenstingl | a50a49a | 2019-06-12 21:59:04 +0200 | [diff] [blame] | 41 | #define MESON_NUM_PWMS 2 |
| 42 | |
Martin Blumenstingl | 8bbf316 | 2019-06-12 21:59:05 +0200 | [diff] [blame] | 43 | static struct meson_pwm_channel_data { |
| 44 | u8 reg_offset; |
| 45 | u8 clk_sel_shift; |
| 46 | u8 clk_div_shift; |
| 47 | u32 clk_en_mask; |
| 48 | u32 pwm_en_mask; |
| 49 | } meson_pwm_per_channel_data[MESON_NUM_PWMS] = { |
| 50 | { |
| 51 | .reg_offset = REG_PWM_A, |
| 52 | .clk_sel_shift = MISC_A_CLK_SEL_SHIFT, |
| 53 | .clk_div_shift = MISC_A_CLK_DIV_SHIFT, |
| 54 | .clk_en_mask = MISC_A_CLK_EN, |
| 55 | .pwm_en_mask = MISC_A_EN, |
| 56 | }, |
| 57 | { |
| 58 | .reg_offset = REG_PWM_B, |
| 59 | .clk_sel_shift = MISC_B_CLK_SEL_SHIFT, |
| 60 | .clk_div_shift = MISC_B_CLK_DIV_SHIFT, |
| 61 | .clk_en_mask = MISC_B_CLK_EN, |
| 62 | .pwm_en_mask = MISC_B_EN, |
| 63 | } |
Neil Armstrong | 211ed63 | 2016-08-22 17:36:30 +0200 | [diff] [blame] | 64 | }; |
| 65 | |
| 66 | struct meson_pwm_channel { |
| 67 | unsigned int hi; |
| 68 | unsigned int lo; |
| 69 | u8 pre_div; |
| 70 | |
| 71 | struct pwm_state state; |
| 72 | |
| 73 | struct clk *clk_parent; |
| 74 | struct clk_mux mux; |
| 75 | struct clk *clk; |
| 76 | }; |
| 77 | |
| 78 | struct meson_pwm_data { |
| 79 | const char * const *parent_names; |
Jerome Brunet | d396b20 | 2017-06-08 14:24:15 +0200 | [diff] [blame] | 80 | unsigned int num_parents; |
Neil Armstrong | 211ed63 | 2016-08-22 17:36:30 +0200 | [diff] [blame] | 81 | }; |
| 82 | |
| 83 | struct meson_pwm { |
| 84 | struct pwm_chip chip; |
| 85 | const struct meson_pwm_data *data; |
Martin Blumenstingl | a50a49a | 2019-06-12 21:59:04 +0200 | [diff] [blame] | 86 | struct meson_pwm_channel channels[MESON_NUM_PWMS]; |
Neil Armstrong | 211ed63 | 2016-08-22 17:36:30 +0200 | [diff] [blame] | 87 | void __iomem *base; |
Martin Blumenstingl | f173747 | 2019-04-01 19:57:48 +0200 | [diff] [blame] | 88 | /* |
| 89 | * Protects register (write) access to the REG_MISC_AB register |
| 90 | * that is shared between the two PWMs. |
| 91 | */ |
Neil Armstrong | 211ed63 | 2016-08-22 17:36:30 +0200 | [diff] [blame] | 92 | spinlock_t lock; |
| 93 | }; |
| 94 | |
| 95 | static inline struct meson_pwm *to_meson_pwm(struct pwm_chip *chip) |
| 96 | { |
| 97 | return container_of(chip, struct meson_pwm, chip); |
| 98 | } |
| 99 | |
| 100 | static int meson_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm) |
| 101 | { |
Martin Blumenstingl | 1064c6b | 2019-06-12 21:59:06 +0200 | [diff] [blame] | 102 | struct meson_pwm *meson = to_meson_pwm(chip); |
| 103 | struct meson_pwm_channel *channel; |
Neil Armstrong | 211ed63 | 2016-08-22 17:36:30 +0200 | [diff] [blame] | 104 | struct device *dev = chip->dev; |
| 105 | int err; |
| 106 | |
Martin Blumenstingl | 1064c6b | 2019-06-12 21:59:06 +0200 | [diff] [blame] | 107 | channel = pwm_get_chip_data(pwm); |
| 108 | if (channel) |
| 109 | return 0; |
| 110 | |
| 111 | channel = &meson->channels[pwm->hwpwm]; |
Neil Armstrong | 211ed63 | 2016-08-22 17:36:30 +0200 | [diff] [blame] | 112 | |
| 113 | if (channel->clk_parent) { |
| 114 | err = clk_set_parent(channel->clk, channel->clk_parent); |
| 115 | if (err < 0) { |
| 116 | dev_err(dev, "failed to set parent %s for %s: %d\n", |
| 117 | __clk_get_name(channel->clk_parent), |
| 118 | __clk_get_name(channel->clk), err); |
| 119 | return err; |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | err = clk_prepare_enable(channel->clk); |
| 124 | if (err < 0) { |
| 125 | dev_err(dev, "failed to enable clock %s: %d\n", |
| 126 | __clk_get_name(channel->clk), err); |
| 127 | return err; |
| 128 | } |
| 129 | |
| 130 | chip->ops->get_state(chip, pwm, &channel->state); |
| 131 | |
Martin Blumenstingl | 1064c6b | 2019-06-12 21:59:06 +0200 | [diff] [blame] | 132 | return pwm_set_chip_data(pwm, channel); |
Neil Armstrong | 211ed63 | 2016-08-22 17:36:30 +0200 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | static void meson_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm) |
| 136 | { |
| 137 | struct meson_pwm_channel *channel = pwm_get_chip_data(pwm); |
| 138 | |
| 139 | if (channel) |
| 140 | clk_disable_unprepare(channel->clk); |
| 141 | } |
| 142 | |
Martin Blumenstingl | 7e03216 | 2019-06-12 21:59:03 +0200 | [diff] [blame] | 143 | static int meson_pwm_calc(struct meson_pwm *meson, struct pwm_device *pwm, |
Martin Blumenstingl | b79c367 | 2019-06-12 21:59:02 +0200 | [diff] [blame] | 144 | struct pwm_state *state) |
Neil Armstrong | 211ed63 | 2016-08-22 17:36:30 +0200 | [diff] [blame] | 145 | { |
Martin Blumenstingl | 7e03216 | 2019-06-12 21:59:03 +0200 | [diff] [blame] | 146 | struct meson_pwm_channel *channel = pwm_get_chip_data(pwm); |
Martin Blumenstingl | b79c367 | 2019-06-12 21:59:02 +0200 | [diff] [blame] | 147 | unsigned int duty, period, pre_div, cnt, duty_cnt; |
Jerome Brunet | fd7b2be | 2017-06-08 14:24:16 +0200 | [diff] [blame] | 148 | unsigned long fin_freq = -1; |
Neil Armstrong | 211ed63 | 2016-08-22 17:36:30 +0200 | [diff] [blame] | 149 | |
Martin Blumenstingl | b79c367 | 2019-06-12 21:59:02 +0200 | [diff] [blame] | 150 | duty = state->duty_cycle; |
| 151 | period = state->period; |
| 152 | |
| 153 | if (state->polarity == PWM_POLARITY_INVERSED) |
Neil Armstrong | 211ed63 | 2016-08-22 17:36:30 +0200 | [diff] [blame] | 154 | duty = period - duty; |
| 155 | |
| 156 | if (period == channel->state.period && |
| 157 | duty == channel->state.duty_cycle) |
| 158 | return 0; |
| 159 | |
| 160 | fin_freq = clk_get_rate(channel->clk); |
| 161 | if (fin_freq == 0) { |
| 162 | dev_err(meson->chip.dev, "invalid source clock frequency\n"); |
| 163 | return -EINVAL; |
| 164 | } |
| 165 | |
| 166 | dev_dbg(meson->chip.dev, "fin_freq: %lu Hz\n", fin_freq); |
Neil Armstrong | 211ed63 | 2016-08-22 17:36:30 +0200 | [diff] [blame] | 167 | |
Martin Blumenstingl | fb2081e | 2019-06-12 21:59:07 +0200 | [diff] [blame^] | 168 | pre_div = div64_u64(fin_freq * (u64)period, NSEC_PER_SEC * 0xffffLL); |
Martin Blumenstingl | 51496e4 | 2019-04-01 20:18:16 +0200 | [diff] [blame] | 169 | if (pre_div > MISC_CLK_DIV_MASK) { |
Neil Armstrong | 211ed63 | 2016-08-22 17:36:30 +0200 | [diff] [blame] | 170 | dev_err(meson->chip.dev, "unable to get period pre_div\n"); |
| 171 | return -EINVAL; |
| 172 | } |
| 173 | |
Martin Blumenstingl | fb2081e | 2019-06-12 21:59:07 +0200 | [diff] [blame^] | 174 | cnt = div64_u64(fin_freq * (u64)period, NSEC_PER_SEC * (pre_div + 1)); |
| 175 | if (cnt > 0xffff) { |
| 176 | dev_err(meson->chip.dev, "unable to get period cnt\n"); |
| 177 | return -EINVAL; |
| 178 | } |
| 179 | |
Neil Armstrong | 211ed63 | 2016-08-22 17:36:30 +0200 | [diff] [blame] | 180 | dev_dbg(meson->chip.dev, "period=%u pre_div=%u cnt=%u\n", period, |
| 181 | pre_div, cnt); |
| 182 | |
| 183 | if (duty == period) { |
| 184 | channel->pre_div = pre_div; |
| 185 | channel->hi = cnt; |
| 186 | channel->lo = 0; |
| 187 | } else if (duty == 0) { |
| 188 | channel->pre_div = pre_div; |
| 189 | channel->hi = 0; |
| 190 | channel->lo = cnt; |
| 191 | } else { |
| 192 | /* Then check is we can have the duty with the same pre_div */ |
Martin Blumenstingl | fb2081e | 2019-06-12 21:59:07 +0200 | [diff] [blame^] | 193 | duty_cnt = div64_u64(fin_freq * (u64)duty, |
| 194 | NSEC_PER_SEC * (pre_div + 1)); |
Neil Armstrong | 211ed63 | 2016-08-22 17:36:30 +0200 | [diff] [blame] | 195 | if (duty_cnt > 0xffff) { |
| 196 | dev_err(meson->chip.dev, "unable to get duty cycle\n"); |
| 197 | return -EINVAL; |
| 198 | } |
| 199 | |
| 200 | dev_dbg(meson->chip.dev, "duty=%u pre_div=%u duty_cnt=%u\n", |
| 201 | duty, pre_div, duty_cnt); |
| 202 | |
| 203 | channel->pre_div = pre_div; |
| 204 | channel->hi = duty_cnt; |
| 205 | channel->lo = cnt - duty_cnt; |
| 206 | } |
| 207 | |
| 208 | return 0; |
| 209 | } |
| 210 | |
Martin Blumenstingl | 084f137 | 2019-06-12 21:58:58 +0200 | [diff] [blame] | 211 | static void meson_pwm_enable(struct meson_pwm *meson, struct pwm_device *pwm) |
Neil Armstrong | 211ed63 | 2016-08-22 17:36:30 +0200 | [diff] [blame] | 212 | { |
Martin Blumenstingl | 084f137 | 2019-06-12 21:58:58 +0200 | [diff] [blame] | 213 | struct meson_pwm_channel *channel = pwm_get_chip_data(pwm); |
Martin Blumenstingl | 8bbf316 | 2019-06-12 21:59:05 +0200 | [diff] [blame] | 214 | struct meson_pwm_channel_data *channel_data; |
Martin Blumenstingl | f173747 | 2019-04-01 19:57:48 +0200 | [diff] [blame] | 215 | unsigned long flags; |
Martin Blumenstingl | 8bbf316 | 2019-06-12 21:59:05 +0200 | [diff] [blame] | 216 | u32 value; |
Neil Armstrong | 211ed63 | 2016-08-22 17:36:30 +0200 | [diff] [blame] | 217 | |
Martin Blumenstingl | 8bbf316 | 2019-06-12 21:59:05 +0200 | [diff] [blame] | 218 | channel_data = &meson_pwm_per_channel_data[pwm->hwpwm]; |
Neil Armstrong | 211ed63 | 2016-08-22 17:36:30 +0200 | [diff] [blame] | 219 | |
Martin Blumenstingl | f173747 | 2019-04-01 19:57:48 +0200 | [diff] [blame] | 220 | spin_lock_irqsave(&meson->lock, flags); |
| 221 | |
Neil Armstrong | 211ed63 | 2016-08-22 17:36:30 +0200 | [diff] [blame] | 222 | value = readl(meson->base + REG_MISC_AB); |
Martin Blumenstingl | 8bbf316 | 2019-06-12 21:59:05 +0200 | [diff] [blame] | 223 | value &= ~(MISC_CLK_DIV_MASK << channel_data->clk_div_shift); |
| 224 | value |= channel->pre_div << channel_data->clk_div_shift; |
| 225 | value |= channel_data->clk_en_mask; |
Neil Armstrong | 211ed63 | 2016-08-22 17:36:30 +0200 | [diff] [blame] | 226 | writel(value, meson->base + REG_MISC_AB); |
| 227 | |
Martin Blumenstingl | 181164b | 2019-06-12 21:59:00 +0200 | [diff] [blame] | 228 | value = FIELD_PREP(PWM_HIGH_MASK, channel->hi) | |
| 229 | FIELD_PREP(PWM_LOW_MASK, channel->lo); |
Martin Blumenstingl | 8bbf316 | 2019-06-12 21:59:05 +0200 | [diff] [blame] | 230 | writel(value, meson->base + channel_data->reg_offset); |
Neil Armstrong | 211ed63 | 2016-08-22 17:36:30 +0200 | [diff] [blame] | 231 | |
| 232 | value = readl(meson->base + REG_MISC_AB); |
Martin Blumenstingl | 8bbf316 | 2019-06-12 21:59:05 +0200 | [diff] [blame] | 233 | value |= channel_data->pwm_en_mask; |
Neil Armstrong | 211ed63 | 2016-08-22 17:36:30 +0200 | [diff] [blame] | 234 | writel(value, meson->base + REG_MISC_AB); |
Martin Blumenstingl | f173747 | 2019-04-01 19:57:48 +0200 | [diff] [blame] | 235 | |
| 236 | spin_unlock_irqrestore(&meson->lock, flags); |
Neil Armstrong | 211ed63 | 2016-08-22 17:36:30 +0200 | [diff] [blame] | 237 | } |
| 238 | |
Martin Blumenstingl | 084f137 | 2019-06-12 21:58:58 +0200 | [diff] [blame] | 239 | static void meson_pwm_disable(struct meson_pwm *meson, struct pwm_device *pwm) |
Neil Armstrong | 211ed63 | 2016-08-22 17:36:30 +0200 | [diff] [blame] | 240 | { |
Martin Blumenstingl | f173747 | 2019-04-01 19:57:48 +0200 | [diff] [blame] | 241 | unsigned long flags; |
Martin Blumenstingl | 8bbf316 | 2019-06-12 21:59:05 +0200 | [diff] [blame] | 242 | u32 value; |
Neil Armstrong | 211ed63 | 2016-08-22 17:36:30 +0200 | [diff] [blame] | 243 | |
Martin Blumenstingl | f173747 | 2019-04-01 19:57:48 +0200 | [diff] [blame] | 244 | spin_lock_irqsave(&meson->lock, flags); |
| 245 | |
Neil Armstrong | 211ed63 | 2016-08-22 17:36:30 +0200 | [diff] [blame] | 246 | value = readl(meson->base + REG_MISC_AB); |
Martin Blumenstingl | 8bbf316 | 2019-06-12 21:59:05 +0200 | [diff] [blame] | 247 | value &= ~meson_pwm_per_channel_data[pwm->hwpwm].pwm_en_mask; |
Neil Armstrong | 211ed63 | 2016-08-22 17:36:30 +0200 | [diff] [blame] | 248 | writel(value, meson->base + REG_MISC_AB); |
Martin Blumenstingl | f173747 | 2019-04-01 19:57:48 +0200 | [diff] [blame] | 249 | |
| 250 | spin_unlock_irqrestore(&meson->lock, flags); |
Neil Armstrong | 211ed63 | 2016-08-22 17:36:30 +0200 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | static int meson_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm, |
| 254 | struct pwm_state *state) |
| 255 | { |
| 256 | struct meson_pwm_channel *channel = pwm_get_chip_data(pwm); |
| 257 | struct meson_pwm *meson = to_meson_pwm(chip); |
Neil Armstrong | 211ed63 | 2016-08-22 17:36:30 +0200 | [diff] [blame] | 258 | int err = 0; |
| 259 | |
| 260 | if (!state) |
| 261 | return -EINVAL; |
| 262 | |
Neil Armstrong | 211ed63 | 2016-08-22 17:36:30 +0200 | [diff] [blame] | 263 | if (!state->enabled) { |
Martin Blumenstingl | 084f137 | 2019-06-12 21:58:58 +0200 | [diff] [blame] | 264 | meson_pwm_disable(meson, pwm); |
Neil Armstrong | 211ed63 | 2016-08-22 17:36:30 +0200 | [diff] [blame] | 265 | channel->state.enabled = false; |
| 266 | |
Martin Blumenstingl | f173747 | 2019-04-01 19:57:48 +0200 | [diff] [blame] | 267 | return 0; |
Neil Armstrong | 211ed63 | 2016-08-22 17:36:30 +0200 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | if (state->period != channel->state.period || |
| 271 | state->duty_cycle != channel->state.duty_cycle || |
| 272 | state->polarity != channel->state.polarity) { |
Martin Blumenstingl | 7e03216 | 2019-06-12 21:59:03 +0200 | [diff] [blame] | 273 | err = meson_pwm_calc(meson, pwm, state); |
Neil Armstrong | 211ed63 | 2016-08-22 17:36:30 +0200 | [diff] [blame] | 274 | if (err < 0) |
Martin Blumenstingl | f173747 | 2019-04-01 19:57:48 +0200 | [diff] [blame] | 275 | return err; |
Neil Armstrong | 211ed63 | 2016-08-22 17:36:30 +0200 | [diff] [blame] | 276 | |
| 277 | channel->state.polarity = state->polarity; |
| 278 | channel->state.period = state->period; |
| 279 | channel->state.duty_cycle = state->duty_cycle; |
| 280 | } |
| 281 | |
| 282 | if (state->enabled && !channel->state.enabled) { |
Martin Blumenstingl | 084f137 | 2019-06-12 21:58:58 +0200 | [diff] [blame] | 283 | meson_pwm_enable(meson, pwm); |
Neil Armstrong | 211ed63 | 2016-08-22 17:36:30 +0200 | [diff] [blame] | 284 | channel->state.enabled = true; |
| 285 | } |
| 286 | |
Martin Blumenstingl | f173747 | 2019-04-01 19:57:48 +0200 | [diff] [blame] | 287 | return 0; |
Neil Armstrong | 211ed63 | 2016-08-22 17:36:30 +0200 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | static void meson_pwm_get_state(struct pwm_chip *chip, struct pwm_device *pwm, |
| 291 | struct pwm_state *state) |
| 292 | { |
| 293 | struct meson_pwm *meson = to_meson_pwm(chip); |
| 294 | u32 value, mask; |
| 295 | |
| 296 | if (!state) |
| 297 | return; |
| 298 | |
Martin Blumenstingl | 8bbf316 | 2019-06-12 21:59:05 +0200 | [diff] [blame] | 299 | mask = meson_pwm_per_channel_data[pwm->hwpwm].pwm_en_mask; |
Neil Armstrong | 211ed63 | 2016-08-22 17:36:30 +0200 | [diff] [blame] | 300 | |
| 301 | value = readl(meson->base + REG_MISC_AB); |
| 302 | state->enabled = (value & mask) != 0; |
| 303 | } |
| 304 | |
| 305 | static const struct pwm_ops meson_pwm_ops = { |
| 306 | .request = meson_pwm_request, |
| 307 | .free = meson_pwm_free, |
| 308 | .apply = meson_pwm_apply, |
| 309 | .get_state = meson_pwm_get_state, |
| 310 | .owner = THIS_MODULE, |
| 311 | }; |
| 312 | |
| 313 | static const char * const pwm_meson8b_parent_names[] = { |
| 314 | "xtal", "vid_pll", "fclk_div4", "fclk_div3" |
| 315 | }; |
| 316 | |
| 317 | static const struct meson_pwm_data pwm_meson8b_data = { |
| 318 | .parent_names = pwm_meson8b_parent_names, |
Jerome Brunet | d396b20 | 2017-06-08 14:24:15 +0200 | [diff] [blame] | 319 | .num_parents = ARRAY_SIZE(pwm_meson8b_parent_names), |
Neil Armstrong | 211ed63 | 2016-08-22 17:36:30 +0200 | [diff] [blame] | 320 | }; |
| 321 | |
| 322 | static const char * const pwm_gxbb_parent_names[] = { |
| 323 | "xtal", "hdmi_pll", "fclk_div4", "fclk_div3" |
| 324 | }; |
| 325 | |
| 326 | static const struct meson_pwm_data pwm_gxbb_data = { |
| 327 | .parent_names = pwm_gxbb_parent_names, |
Jerome Brunet | d396b20 | 2017-06-08 14:24:15 +0200 | [diff] [blame] | 328 | .num_parents = ARRAY_SIZE(pwm_gxbb_parent_names), |
| 329 | }; |
| 330 | |
| 331 | /* |
| 332 | * Only the 2 first inputs of the GXBB AO PWMs are valid |
| 333 | * The last 2 are grounded |
| 334 | */ |
| 335 | static const char * const pwm_gxbb_ao_parent_names[] = { |
| 336 | "xtal", "clk81" |
| 337 | }; |
| 338 | |
| 339 | static const struct meson_pwm_data pwm_gxbb_ao_data = { |
| 340 | .parent_names = pwm_gxbb_ao_parent_names, |
| 341 | .num_parents = ARRAY_SIZE(pwm_gxbb_ao_parent_names), |
Neil Armstrong | 211ed63 | 2016-08-22 17:36:30 +0200 | [diff] [blame] | 342 | }; |
| 343 | |
Jian Hu | bccaa3f | 2017-12-04 14:00:17 +0800 | [diff] [blame] | 344 | static const char * const pwm_axg_ee_parent_names[] = { |
| 345 | "xtal", "fclk_div5", "fclk_div4", "fclk_div3" |
| 346 | }; |
| 347 | |
| 348 | static const struct meson_pwm_data pwm_axg_ee_data = { |
| 349 | .parent_names = pwm_axg_ee_parent_names, |
| 350 | .num_parents = ARRAY_SIZE(pwm_axg_ee_parent_names), |
| 351 | }; |
| 352 | |
| 353 | static const char * const pwm_axg_ao_parent_names[] = { |
| 354 | "aoclk81", "xtal", "fclk_div4", "fclk_div5" |
| 355 | }; |
| 356 | |
| 357 | static const struct meson_pwm_data pwm_axg_ao_data = { |
| 358 | .parent_names = pwm_axg_ao_parent_names, |
| 359 | .num_parents = ARRAY_SIZE(pwm_axg_ao_parent_names), |
| 360 | }; |
| 361 | |
Neil Armstrong | 9bce02e | 2019-06-20 16:46:55 +0200 | [diff] [blame] | 362 | static const char * const pwm_g12a_ao_ab_parent_names[] = { |
| 363 | "xtal", "aoclk81", "fclk_div4", "fclk_div5" |
| 364 | }; |
| 365 | |
| 366 | static const struct meson_pwm_data pwm_g12a_ao_ab_data = { |
| 367 | .parent_names = pwm_g12a_ao_ab_parent_names, |
| 368 | .num_parents = ARRAY_SIZE(pwm_g12a_ao_ab_parent_names), |
| 369 | }; |
| 370 | |
Neil Armstrong | f41efce | 2019-04-23 15:36:45 +0200 | [diff] [blame] | 371 | static const char * const pwm_g12a_ao_cd_parent_names[] = { |
Neil Armstrong | 9bce02e | 2019-06-20 16:46:55 +0200 | [diff] [blame] | 372 | "xtal", "aoclk81", |
Neil Armstrong | f41efce | 2019-04-23 15:36:45 +0200 | [diff] [blame] | 373 | }; |
| 374 | |
| 375 | static const struct meson_pwm_data pwm_g12a_ao_cd_data = { |
| 376 | .parent_names = pwm_g12a_ao_cd_parent_names, |
| 377 | .num_parents = ARRAY_SIZE(pwm_g12a_ao_cd_parent_names), |
| 378 | }; |
| 379 | |
| 380 | static const char * const pwm_g12a_ee_parent_names[] = { |
| 381 | "xtal", "hdmi_pll", "fclk_div4", "fclk_div3" |
| 382 | }; |
| 383 | |
| 384 | static const struct meson_pwm_data pwm_g12a_ee_data = { |
| 385 | .parent_names = pwm_g12a_ee_parent_names, |
| 386 | .num_parents = ARRAY_SIZE(pwm_g12a_ee_parent_names), |
| 387 | }; |
| 388 | |
Neil Armstrong | 211ed63 | 2016-08-22 17:36:30 +0200 | [diff] [blame] | 389 | static const struct of_device_id meson_pwm_matches[] = { |
Jerome Brunet | d396b20 | 2017-06-08 14:24:15 +0200 | [diff] [blame] | 390 | { |
| 391 | .compatible = "amlogic,meson8b-pwm", |
| 392 | .data = &pwm_meson8b_data |
| 393 | }, |
| 394 | { |
| 395 | .compatible = "amlogic,meson-gxbb-pwm", |
| 396 | .data = &pwm_gxbb_data |
| 397 | }, |
| 398 | { |
| 399 | .compatible = "amlogic,meson-gxbb-ao-pwm", |
| 400 | .data = &pwm_gxbb_ao_data |
| 401 | }, |
Jian Hu | bccaa3f | 2017-12-04 14:00:17 +0800 | [diff] [blame] | 402 | { |
| 403 | .compatible = "amlogic,meson-axg-ee-pwm", |
| 404 | .data = &pwm_axg_ee_data |
| 405 | }, |
| 406 | { |
| 407 | .compatible = "amlogic,meson-axg-ao-pwm", |
| 408 | .data = &pwm_axg_ao_data |
| 409 | }, |
Neil Armstrong | f41efce | 2019-04-23 15:36:45 +0200 | [diff] [blame] | 410 | { |
| 411 | .compatible = "amlogic,meson-g12a-ee-pwm", |
| 412 | .data = &pwm_g12a_ee_data |
| 413 | }, |
| 414 | { |
| 415 | .compatible = "amlogic,meson-g12a-ao-pwm-ab", |
Neil Armstrong | 9bce02e | 2019-06-20 16:46:55 +0200 | [diff] [blame] | 416 | .data = &pwm_g12a_ao_ab_data |
Neil Armstrong | f41efce | 2019-04-23 15:36:45 +0200 | [diff] [blame] | 417 | }, |
| 418 | { |
| 419 | .compatible = "amlogic,meson-g12a-ao-pwm-cd", |
| 420 | .data = &pwm_g12a_ao_cd_data |
| 421 | }, |
Neil Armstrong | 211ed63 | 2016-08-22 17:36:30 +0200 | [diff] [blame] | 422 | {}, |
| 423 | }; |
| 424 | MODULE_DEVICE_TABLE(of, meson_pwm_matches); |
| 425 | |
Martin Blumenstingl | a50a49a | 2019-06-12 21:59:04 +0200 | [diff] [blame] | 426 | static int meson_pwm_init_channels(struct meson_pwm *meson) |
Neil Armstrong | 211ed63 | 2016-08-22 17:36:30 +0200 | [diff] [blame] | 427 | { |
| 428 | struct device *dev = meson->chip.dev; |
Neil Armstrong | 211ed63 | 2016-08-22 17:36:30 +0200 | [diff] [blame] | 429 | struct clk_init_data init; |
| 430 | unsigned int i; |
| 431 | char name[255]; |
| 432 | int err; |
| 433 | |
| 434 | for (i = 0; i < meson->chip.npwm; i++) { |
Martin Blumenstingl | a50a49a | 2019-06-12 21:59:04 +0200 | [diff] [blame] | 435 | struct meson_pwm_channel *channel = &meson->channels[i]; |
Neil Armstrong | 211ed63 | 2016-08-22 17:36:30 +0200 | [diff] [blame] | 436 | |
Jerome Brunet | b96e9eb | 2018-08-01 12:57:20 +0200 | [diff] [blame] | 437 | snprintf(name, sizeof(name), "%s#mux%u", dev_name(dev), i); |
Neil Armstrong | 211ed63 | 2016-08-22 17:36:30 +0200 | [diff] [blame] | 438 | |
| 439 | init.name = name; |
| 440 | init.ops = &clk_mux_ops; |
Stephen Boyd | 90b6c5c | 2019-04-25 10:57:37 -0700 | [diff] [blame] | 441 | init.flags = 0; |
Neil Armstrong | 211ed63 | 2016-08-22 17:36:30 +0200 | [diff] [blame] | 442 | init.parent_names = meson->data->parent_names; |
Jerome Brunet | d396b20 | 2017-06-08 14:24:15 +0200 | [diff] [blame] | 443 | init.num_parents = meson->data->num_parents; |
Neil Armstrong | 211ed63 | 2016-08-22 17:36:30 +0200 | [diff] [blame] | 444 | |
| 445 | channel->mux.reg = meson->base + REG_MISC_AB; |
Martin Blumenstingl | 8bbf316 | 2019-06-12 21:59:05 +0200 | [diff] [blame] | 446 | channel->mux.shift = |
| 447 | meson_pwm_per_channel_data[i].clk_sel_shift; |
Martin Blumenstingl | 33cefd8 | 2019-06-12 21:59:01 +0200 | [diff] [blame] | 448 | channel->mux.mask = MISC_CLK_SEL_MASK; |
Neil Armstrong | 211ed63 | 2016-08-22 17:36:30 +0200 | [diff] [blame] | 449 | channel->mux.flags = 0; |
| 450 | channel->mux.lock = &meson->lock; |
| 451 | channel->mux.table = NULL; |
| 452 | channel->mux.hw.init = &init; |
| 453 | |
| 454 | channel->clk = devm_clk_register(dev, &channel->mux.hw); |
| 455 | if (IS_ERR(channel->clk)) { |
| 456 | err = PTR_ERR(channel->clk); |
| 457 | dev_err(dev, "failed to register %s: %d\n", name, err); |
| 458 | return err; |
| 459 | } |
| 460 | |
| 461 | snprintf(name, sizeof(name), "clkin%u", i); |
| 462 | |
Martin Blumenstingl | ba4004c | 2019-06-12 21:58:59 +0200 | [diff] [blame] | 463 | channel->clk_parent = devm_clk_get_optional(dev, name); |
| 464 | if (IS_ERR(channel->clk_parent)) |
| 465 | return PTR_ERR(channel->clk_parent); |
Neil Armstrong | 211ed63 | 2016-08-22 17:36:30 +0200 | [diff] [blame] | 466 | } |
| 467 | |
| 468 | return 0; |
| 469 | } |
| 470 | |
Neil Armstrong | 211ed63 | 2016-08-22 17:36:30 +0200 | [diff] [blame] | 471 | static int meson_pwm_probe(struct platform_device *pdev) |
| 472 | { |
Neil Armstrong | 211ed63 | 2016-08-22 17:36:30 +0200 | [diff] [blame] | 473 | struct meson_pwm *meson; |
| 474 | struct resource *regs; |
| 475 | int err; |
| 476 | |
| 477 | meson = devm_kzalloc(&pdev->dev, sizeof(*meson), GFP_KERNEL); |
| 478 | if (!meson) |
| 479 | return -ENOMEM; |
| 480 | |
| 481 | regs = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 482 | meson->base = devm_ioremap_resource(&pdev->dev, regs); |
| 483 | if (IS_ERR(meson->base)) |
| 484 | return PTR_ERR(meson->base); |
| 485 | |
Axel Lin | c699995 | 2016-09-10 09:55:49 +0800 | [diff] [blame] | 486 | spin_lock_init(&meson->lock); |
Neil Armstrong | 211ed63 | 2016-08-22 17:36:30 +0200 | [diff] [blame] | 487 | meson->chip.dev = &pdev->dev; |
| 488 | meson->chip.ops = &meson_pwm_ops; |
| 489 | meson->chip.base = -1; |
Martin Blumenstingl | a50a49a | 2019-06-12 21:59:04 +0200 | [diff] [blame] | 490 | meson->chip.npwm = MESON_NUM_PWMS; |
Neil Armstrong | 211ed63 | 2016-08-22 17:36:30 +0200 | [diff] [blame] | 491 | meson->chip.of_xlate = of_pwm_xlate_with_flags; |
| 492 | meson->chip.of_pwm_n_cells = 3; |
| 493 | |
| 494 | meson->data = of_device_get_match_data(&pdev->dev); |
Neil Armstrong | 211ed63 | 2016-08-22 17:36:30 +0200 | [diff] [blame] | 495 | |
Martin Blumenstingl | a50a49a | 2019-06-12 21:59:04 +0200 | [diff] [blame] | 496 | err = meson_pwm_init_channels(meson); |
Neil Armstrong | 211ed63 | 2016-08-22 17:36:30 +0200 | [diff] [blame] | 497 | if (err < 0) |
| 498 | return err; |
| 499 | |
| 500 | err = pwmchip_add(&meson->chip); |
| 501 | if (err < 0) { |
| 502 | dev_err(&pdev->dev, "failed to register PWM chip: %d\n", err); |
| 503 | return err; |
| 504 | } |
| 505 | |
Neil Armstrong | 211ed63 | 2016-08-22 17:36:30 +0200 | [diff] [blame] | 506 | platform_set_drvdata(pdev, meson); |
| 507 | |
| 508 | return 0; |
| 509 | } |
| 510 | |
| 511 | static int meson_pwm_remove(struct platform_device *pdev) |
| 512 | { |
| 513 | struct meson_pwm *meson = platform_get_drvdata(pdev); |
| 514 | |
| 515 | return pwmchip_remove(&meson->chip); |
| 516 | } |
| 517 | |
| 518 | static struct platform_driver meson_pwm_driver = { |
| 519 | .driver = { |
| 520 | .name = "meson-pwm", |
| 521 | .of_match_table = meson_pwm_matches, |
| 522 | }, |
| 523 | .probe = meson_pwm_probe, |
| 524 | .remove = meson_pwm_remove, |
| 525 | }; |
| 526 | module_platform_driver(meson_pwm_driver); |
| 527 | |
Neil Armstrong | 211ed63 | 2016-08-22 17:36:30 +0200 | [diff] [blame] | 528 | MODULE_DESCRIPTION("Amlogic Meson PWM Generator driver"); |
| 529 | MODULE_AUTHOR("Neil Armstrong <narmstrong@baylibre.com>"); |
| 530 | MODULE_LICENSE("Dual BSD/GPL"); |