blob: 99815b26f6b2b6bc54ac0944a36d4fefcc99f3a2 [file] [log] [blame]
Neil Armstrong1cdb4412019-05-20 16:04:21 +02001// SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
Neil Armstrong211ed632016-08-22 17:36:30 +02002/*
Neil Armstrong211ed632016-08-22 17:36:30 +02003 * Copyright (c) 2016 BayLibre, SAS.
4 * Author: Neil Armstrong <narmstrong@baylibre.com>
5 * Copyright (C) 2014 Amlogic, Inc.
Neil Armstrong211ed632016-08-22 17:36:30 +02006 */
7
Martin Blumenstingl181164b2019-06-12 21:59:00 +02008#include <linux/bitfield.h>
9#include <linux/bits.h>
Neil Armstrong211ed632016-08-22 17:36:30 +020010#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 Blumenstinglfb2081e2019-06-12 21:59:07 +020015#include <linux/math64.h>
Neil Armstrong211ed632016-08-22 17:36:30 +020016#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 Blumenstingl181164b2019-06-12 21:59:00 +020026#define PWM_LOW_MASK GENMASK(15, 0)
27#define PWM_HIGH_MASK GENMASK(31, 16)
Neil Armstrong211ed632016-08-22 17:36:30 +020028
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 Blumenstingl33cefd82019-06-12 21:59:01 +020037#define MISC_CLK_SEL_MASK 0x3
Neil Armstrong211ed632016-08-22 17:36:30 +020038#define MISC_B_EN BIT(1)
39#define MISC_A_EN BIT(0)
40
Martin Blumenstingla50a49a2019-06-12 21:59:04 +020041#define MESON_NUM_PWMS 2
42
Martin Blumenstingl8bbf3162019-06-12 21:59:05 +020043static 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 Armstrong211ed632016-08-22 17:36:30 +020064};
65
66struct 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
78struct meson_pwm_data {
79 const char * const *parent_names;
Jerome Brunetd396b202017-06-08 14:24:15 +020080 unsigned int num_parents;
Neil Armstrong211ed632016-08-22 17:36:30 +020081};
82
83struct meson_pwm {
84 struct pwm_chip chip;
85 const struct meson_pwm_data *data;
Martin Blumenstingla50a49a2019-06-12 21:59:04 +020086 struct meson_pwm_channel channels[MESON_NUM_PWMS];
Neil Armstrong211ed632016-08-22 17:36:30 +020087 void __iomem *base;
Martin Blumenstinglf1737472019-04-01 19:57:48 +020088 /*
89 * Protects register (write) access to the REG_MISC_AB register
90 * that is shared between the two PWMs.
91 */
Neil Armstrong211ed632016-08-22 17:36:30 +020092 spinlock_t lock;
93};
94
95static inline struct meson_pwm *to_meson_pwm(struct pwm_chip *chip)
96{
97 return container_of(chip, struct meson_pwm, chip);
98}
99
100static int meson_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm)
101{
Martin Blumenstingl1064c6b2019-06-12 21:59:06 +0200102 struct meson_pwm *meson = to_meson_pwm(chip);
103 struct meson_pwm_channel *channel;
Neil Armstrong211ed632016-08-22 17:36:30 +0200104 struct device *dev = chip->dev;
105 int err;
106
Martin Blumenstingl1064c6b2019-06-12 21:59:06 +0200107 channel = pwm_get_chip_data(pwm);
108 if (channel)
109 return 0;
110
111 channel = &meson->channels[pwm->hwpwm];
Neil Armstrong211ed632016-08-22 17:36:30 +0200112
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 Blumenstingl1064c6b2019-06-12 21:59:06 +0200132 return pwm_set_chip_data(pwm, channel);
Neil Armstrong211ed632016-08-22 17:36:30 +0200133}
134
135static 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 Blumenstingl7e032162019-06-12 21:59:03 +0200143static int meson_pwm_calc(struct meson_pwm *meson, struct pwm_device *pwm,
Martin Blumenstinglb79c3672019-06-12 21:59:02 +0200144 struct pwm_state *state)
Neil Armstrong211ed632016-08-22 17:36:30 +0200145{
Martin Blumenstingl7e032162019-06-12 21:59:03 +0200146 struct meson_pwm_channel *channel = pwm_get_chip_data(pwm);
Martin Blumenstinglb79c3672019-06-12 21:59:02 +0200147 unsigned int duty, period, pre_div, cnt, duty_cnt;
Jerome Brunetfd7b2be2017-06-08 14:24:16 +0200148 unsigned long fin_freq = -1;
Neil Armstrong211ed632016-08-22 17:36:30 +0200149
Martin Blumenstinglb79c3672019-06-12 21:59:02 +0200150 duty = state->duty_cycle;
151 period = state->period;
152
153 if (state->polarity == PWM_POLARITY_INVERSED)
Neil Armstrong211ed632016-08-22 17:36:30 +0200154 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 Armstrong211ed632016-08-22 17:36:30 +0200167
Martin Blumenstinglfb2081e2019-06-12 21:59:07 +0200168 pre_div = div64_u64(fin_freq * (u64)period, NSEC_PER_SEC * 0xffffLL);
Martin Blumenstingl51496e42019-04-01 20:18:16 +0200169 if (pre_div > MISC_CLK_DIV_MASK) {
Neil Armstrong211ed632016-08-22 17:36:30 +0200170 dev_err(meson->chip.dev, "unable to get period pre_div\n");
171 return -EINVAL;
172 }
173
Martin Blumenstinglfb2081e2019-06-12 21:59:07 +0200174 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 Armstrong211ed632016-08-22 17:36:30 +0200180 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 Blumenstinglfb2081e2019-06-12 21:59:07 +0200193 duty_cnt = div64_u64(fin_freq * (u64)duty,
194 NSEC_PER_SEC * (pre_div + 1));
Neil Armstrong211ed632016-08-22 17:36:30 +0200195 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 Blumenstingl084f1372019-06-12 21:58:58 +0200211static void meson_pwm_enable(struct meson_pwm *meson, struct pwm_device *pwm)
Neil Armstrong211ed632016-08-22 17:36:30 +0200212{
Martin Blumenstingl084f1372019-06-12 21:58:58 +0200213 struct meson_pwm_channel *channel = pwm_get_chip_data(pwm);
Martin Blumenstingl8bbf3162019-06-12 21:59:05 +0200214 struct meson_pwm_channel_data *channel_data;
Martin Blumenstinglf1737472019-04-01 19:57:48 +0200215 unsigned long flags;
Martin Blumenstingl8bbf3162019-06-12 21:59:05 +0200216 u32 value;
Neil Armstrong211ed632016-08-22 17:36:30 +0200217
Martin Blumenstingl8bbf3162019-06-12 21:59:05 +0200218 channel_data = &meson_pwm_per_channel_data[pwm->hwpwm];
Neil Armstrong211ed632016-08-22 17:36:30 +0200219
Martin Blumenstinglf1737472019-04-01 19:57:48 +0200220 spin_lock_irqsave(&meson->lock, flags);
221
Neil Armstrong211ed632016-08-22 17:36:30 +0200222 value = readl(meson->base + REG_MISC_AB);
Martin Blumenstingl8bbf3162019-06-12 21:59:05 +0200223 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 Armstrong211ed632016-08-22 17:36:30 +0200226 writel(value, meson->base + REG_MISC_AB);
227
Martin Blumenstingl181164b2019-06-12 21:59:00 +0200228 value = FIELD_PREP(PWM_HIGH_MASK, channel->hi) |
229 FIELD_PREP(PWM_LOW_MASK, channel->lo);
Martin Blumenstingl8bbf3162019-06-12 21:59:05 +0200230 writel(value, meson->base + channel_data->reg_offset);
Neil Armstrong211ed632016-08-22 17:36:30 +0200231
232 value = readl(meson->base + REG_MISC_AB);
Martin Blumenstingl8bbf3162019-06-12 21:59:05 +0200233 value |= channel_data->pwm_en_mask;
Neil Armstrong211ed632016-08-22 17:36:30 +0200234 writel(value, meson->base + REG_MISC_AB);
Martin Blumenstinglf1737472019-04-01 19:57:48 +0200235
236 spin_unlock_irqrestore(&meson->lock, flags);
Neil Armstrong211ed632016-08-22 17:36:30 +0200237}
238
Martin Blumenstingl084f1372019-06-12 21:58:58 +0200239static void meson_pwm_disable(struct meson_pwm *meson, struct pwm_device *pwm)
Neil Armstrong211ed632016-08-22 17:36:30 +0200240{
Martin Blumenstinglf1737472019-04-01 19:57:48 +0200241 unsigned long flags;
Martin Blumenstingl8bbf3162019-06-12 21:59:05 +0200242 u32 value;
Neil Armstrong211ed632016-08-22 17:36:30 +0200243
Martin Blumenstinglf1737472019-04-01 19:57:48 +0200244 spin_lock_irqsave(&meson->lock, flags);
245
Neil Armstrong211ed632016-08-22 17:36:30 +0200246 value = readl(meson->base + REG_MISC_AB);
Martin Blumenstingl8bbf3162019-06-12 21:59:05 +0200247 value &= ~meson_pwm_per_channel_data[pwm->hwpwm].pwm_en_mask;
Neil Armstrong211ed632016-08-22 17:36:30 +0200248 writel(value, meson->base + REG_MISC_AB);
Martin Blumenstinglf1737472019-04-01 19:57:48 +0200249
250 spin_unlock_irqrestore(&meson->lock, flags);
Neil Armstrong211ed632016-08-22 17:36:30 +0200251}
252
253static 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 Armstrong211ed632016-08-22 17:36:30 +0200258 int err = 0;
259
260 if (!state)
261 return -EINVAL;
262
Neil Armstrong211ed632016-08-22 17:36:30 +0200263 if (!state->enabled) {
Martin Blumenstingl084f1372019-06-12 21:58:58 +0200264 meson_pwm_disable(meson, pwm);
Neil Armstrong211ed632016-08-22 17:36:30 +0200265 channel->state.enabled = false;
266
Martin Blumenstinglf1737472019-04-01 19:57:48 +0200267 return 0;
Neil Armstrong211ed632016-08-22 17:36:30 +0200268 }
269
270 if (state->period != channel->state.period ||
271 state->duty_cycle != channel->state.duty_cycle ||
272 state->polarity != channel->state.polarity) {
Martin Blumenstingl7e032162019-06-12 21:59:03 +0200273 err = meson_pwm_calc(meson, pwm, state);
Neil Armstrong211ed632016-08-22 17:36:30 +0200274 if (err < 0)
Martin Blumenstinglf1737472019-04-01 19:57:48 +0200275 return err;
Neil Armstrong211ed632016-08-22 17:36:30 +0200276
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 Blumenstingl084f1372019-06-12 21:58:58 +0200283 meson_pwm_enable(meson, pwm);
Neil Armstrong211ed632016-08-22 17:36:30 +0200284 channel->state.enabled = true;
285 }
286
Martin Blumenstinglf1737472019-04-01 19:57:48 +0200287 return 0;
Neil Armstrong211ed632016-08-22 17:36:30 +0200288}
289
290static 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 Blumenstingl8bbf3162019-06-12 21:59:05 +0200299 mask = meson_pwm_per_channel_data[pwm->hwpwm].pwm_en_mask;
Neil Armstrong211ed632016-08-22 17:36:30 +0200300
301 value = readl(meson->base + REG_MISC_AB);
302 state->enabled = (value & mask) != 0;
303}
304
305static 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
313static const char * const pwm_meson8b_parent_names[] = {
314 "xtal", "vid_pll", "fclk_div4", "fclk_div3"
315};
316
317static const struct meson_pwm_data pwm_meson8b_data = {
318 .parent_names = pwm_meson8b_parent_names,
Jerome Brunetd396b202017-06-08 14:24:15 +0200319 .num_parents = ARRAY_SIZE(pwm_meson8b_parent_names),
Neil Armstrong211ed632016-08-22 17:36:30 +0200320};
321
322static const char * const pwm_gxbb_parent_names[] = {
323 "xtal", "hdmi_pll", "fclk_div4", "fclk_div3"
324};
325
326static const struct meson_pwm_data pwm_gxbb_data = {
327 .parent_names = pwm_gxbb_parent_names,
Jerome Brunetd396b202017-06-08 14:24:15 +0200328 .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 */
335static const char * const pwm_gxbb_ao_parent_names[] = {
336 "xtal", "clk81"
337};
338
339static 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 Armstrong211ed632016-08-22 17:36:30 +0200342};
343
Jian Hubccaa3f2017-12-04 14:00:17 +0800344static const char * const pwm_axg_ee_parent_names[] = {
345 "xtal", "fclk_div5", "fclk_div4", "fclk_div3"
346};
347
348static 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
353static const char * const pwm_axg_ao_parent_names[] = {
354 "aoclk81", "xtal", "fclk_div4", "fclk_div5"
355};
356
357static 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 Armstrong9bce02e2019-06-20 16:46:55 +0200362static const char * const pwm_g12a_ao_ab_parent_names[] = {
363 "xtal", "aoclk81", "fclk_div4", "fclk_div5"
364};
365
366static 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 Armstrongf41efce2019-04-23 15:36:45 +0200371static const char * const pwm_g12a_ao_cd_parent_names[] = {
Neil Armstrong9bce02e2019-06-20 16:46:55 +0200372 "xtal", "aoclk81",
Neil Armstrongf41efce2019-04-23 15:36:45 +0200373};
374
375static 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
380static const char * const pwm_g12a_ee_parent_names[] = {
381 "xtal", "hdmi_pll", "fclk_div4", "fclk_div3"
382};
383
384static 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 Armstrong211ed632016-08-22 17:36:30 +0200389static const struct of_device_id meson_pwm_matches[] = {
Jerome Brunetd396b202017-06-08 14:24:15 +0200390 {
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 Hubccaa3f2017-12-04 14:00:17 +0800402 {
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 Armstrongf41efce2019-04-23 15:36:45 +0200410 {
411 .compatible = "amlogic,meson-g12a-ee-pwm",
412 .data = &pwm_g12a_ee_data
413 },
414 {
415 .compatible = "amlogic,meson-g12a-ao-pwm-ab",
Neil Armstrong9bce02e2019-06-20 16:46:55 +0200416 .data = &pwm_g12a_ao_ab_data
Neil Armstrongf41efce2019-04-23 15:36:45 +0200417 },
418 {
419 .compatible = "amlogic,meson-g12a-ao-pwm-cd",
420 .data = &pwm_g12a_ao_cd_data
421 },
Neil Armstrong211ed632016-08-22 17:36:30 +0200422 {},
423};
424MODULE_DEVICE_TABLE(of, meson_pwm_matches);
425
Martin Blumenstingla50a49a2019-06-12 21:59:04 +0200426static int meson_pwm_init_channels(struct meson_pwm *meson)
Neil Armstrong211ed632016-08-22 17:36:30 +0200427{
428 struct device *dev = meson->chip.dev;
Neil Armstrong211ed632016-08-22 17:36:30 +0200429 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 Blumenstingla50a49a2019-06-12 21:59:04 +0200435 struct meson_pwm_channel *channel = &meson->channels[i];
Neil Armstrong211ed632016-08-22 17:36:30 +0200436
Jerome Brunetb96e9eb2018-08-01 12:57:20 +0200437 snprintf(name, sizeof(name), "%s#mux%u", dev_name(dev), i);
Neil Armstrong211ed632016-08-22 17:36:30 +0200438
439 init.name = name;
440 init.ops = &clk_mux_ops;
Stephen Boyd90b6c5c2019-04-25 10:57:37 -0700441 init.flags = 0;
Neil Armstrong211ed632016-08-22 17:36:30 +0200442 init.parent_names = meson->data->parent_names;
Jerome Brunetd396b202017-06-08 14:24:15 +0200443 init.num_parents = meson->data->num_parents;
Neil Armstrong211ed632016-08-22 17:36:30 +0200444
445 channel->mux.reg = meson->base + REG_MISC_AB;
Martin Blumenstingl8bbf3162019-06-12 21:59:05 +0200446 channel->mux.shift =
447 meson_pwm_per_channel_data[i].clk_sel_shift;
Martin Blumenstingl33cefd82019-06-12 21:59:01 +0200448 channel->mux.mask = MISC_CLK_SEL_MASK;
Neil Armstrong211ed632016-08-22 17:36:30 +0200449 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 Blumenstinglba4004c2019-06-12 21:58:59 +0200463 channel->clk_parent = devm_clk_get_optional(dev, name);
464 if (IS_ERR(channel->clk_parent))
465 return PTR_ERR(channel->clk_parent);
Neil Armstrong211ed632016-08-22 17:36:30 +0200466 }
467
468 return 0;
469}
470
Neil Armstrong211ed632016-08-22 17:36:30 +0200471static int meson_pwm_probe(struct platform_device *pdev)
472{
Neil Armstrong211ed632016-08-22 17:36:30 +0200473 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 Linc6999952016-09-10 09:55:49 +0800486 spin_lock_init(&meson->lock);
Neil Armstrong211ed632016-08-22 17:36:30 +0200487 meson->chip.dev = &pdev->dev;
488 meson->chip.ops = &meson_pwm_ops;
489 meson->chip.base = -1;
Martin Blumenstingla50a49a2019-06-12 21:59:04 +0200490 meson->chip.npwm = MESON_NUM_PWMS;
Neil Armstrong211ed632016-08-22 17:36:30 +0200491 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 Armstrong211ed632016-08-22 17:36:30 +0200495
Martin Blumenstingla50a49a2019-06-12 21:59:04 +0200496 err = meson_pwm_init_channels(meson);
Neil Armstrong211ed632016-08-22 17:36:30 +0200497 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 Armstrong211ed632016-08-22 17:36:30 +0200506 platform_set_drvdata(pdev, meson);
507
508 return 0;
509}
510
511static 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
518static 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};
526module_platform_driver(meson_pwm_driver);
527
Neil Armstrong211ed632016-08-22 17:36:30 +0200528MODULE_DESCRIPTION("Amlogic Meson PWM Generator driver");
529MODULE_AUTHOR("Neil Armstrong <narmstrong@baylibre.com>");
530MODULE_LICENSE("Dual BSD/GPL");