blob: 8a5658f2b947f90e17603ef7c8800ec303aed9e0 [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>
15#include <linux/module.h>
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#include <linux/spinlock.h>
22
23#define REG_PWM_A 0x0
24#define REG_PWM_B 0x4
Martin Blumenstingl181164b2019-06-12 21:59:00 +020025#define PWM_LOW_MASK GENMASK(15, 0)
26#define PWM_HIGH_MASK GENMASK(31, 16)
Neil Armstrong211ed632016-08-22 17:36:30 +020027
28#define REG_MISC_AB 0x8
29#define MISC_B_CLK_EN BIT(23)
30#define MISC_A_CLK_EN BIT(15)
31#define MISC_CLK_DIV_MASK 0x7f
32#define MISC_B_CLK_DIV_SHIFT 16
33#define MISC_A_CLK_DIV_SHIFT 8
34#define MISC_B_CLK_SEL_SHIFT 6
35#define MISC_A_CLK_SEL_SHIFT 4
Martin Blumenstingl33cefd82019-06-12 21:59:01 +020036#define MISC_CLK_SEL_MASK 0x3
Neil Armstrong211ed632016-08-22 17:36:30 +020037#define MISC_B_EN BIT(1)
38#define MISC_A_EN BIT(0)
39
Martin Blumenstingla50a49a2019-06-12 21:59:04 +020040#define MESON_NUM_PWMS 2
41
Martin Blumenstingl8bbf3162019-06-12 21:59:05 +020042static struct meson_pwm_channel_data {
43 u8 reg_offset;
44 u8 clk_sel_shift;
45 u8 clk_div_shift;
46 u32 clk_en_mask;
47 u32 pwm_en_mask;
48} meson_pwm_per_channel_data[MESON_NUM_PWMS] = {
49 {
50 .reg_offset = REG_PWM_A,
51 .clk_sel_shift = MISC_A_CLK_SEL_SHIFT,
52 .clk_div_shift = MISC_A_CLK_DIV_SHIFT,
53 .clk_en_mask = MISC_A_CLK_EN,
54 .pwm_en_mask = MISC_A_EN,
55 },
56 {
57 .reg_offset = REG_PWM_B,
58 .clk_sel_shift = MISC_B_CLK_SEL_SHIFT,
59 .clk_div_shift = MISC_B_CLK_DIV_SHIFT,
60 .clk_en_mask = MISC_B_CLK_EN,
61 .pwm_en_mask = MISC_B_EN,
62 }
Neil Armstrong211ed632016-08-22 17:36:30 +020063};
64
65struct meson_pwm_channel {
66 unsigned int hi;
67 unsigned int lo;
68 u8 pre_div;
69
70 struct pwm_state state;
71
72 struct clk *clk_parent;
73 struct clk_mux mux;
74 struct clk *clk;
75};
76
77struct meson_pwm_data {
78 const char * const *parent_names;
Jerome Brunetd396b202017-06-08 14:24:15 +020079 unsigned int num_parents;
Neil Armstrong211ed632016-08-22 17:36:30 +020080};
81
82struct meson_pwm {
83 struct pwm_chip chip;
84 const struct meson_pwm_data *data;
Martin Blumenstingla50a49a2019-06-12 21:59:04 +020085 struct meson_pwm_channel channels[MESON_NUM_PWMS];
Neil Armstrong211ed632016-08-22 17:36:30 +020086 void __iomem *base;
Martin Blumenstinglf1737472019-04-01 19:57:48 +020087 /*
88 * Protects register (write) access to the REG_MISC_AB register
89 * that is shared between the two PWMs.
90 */
Neil Armstrong211ed632016-08-22 17:36:30 +020091 spinlock_t lock;
92};
93
94static inline struct meson_pwm *to_meson_pwm(struct pwm_chip *chip)
95{
96 return container_of(chip, struct meson_pwm, chip);
97}
98
99static int meson_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm)
100{
Martin Blumenstingl1064c6b2019-06-12 21:59:06 +0200101 struct meson_pwm *meson = to_meson_pwm(chip);
102 struct meson_pwm_channel *channel;
Neil Armstrong211ed632016-08-22 17:36:30 +0200103 struct device *dev = chip->dev;
104 int err;
105
Martin Blumenstingl1064c6b2019-06-12 21:59:06 +0200106 channel = pwm_get_chip_data(pwm);
107 if (channel)
108 return 0;
109
110 channel = &meson->channels[pwm->hwpwm];
Neil Armstrong211ed632016-08-22 17:36:30 +0200111
112 if (channel->clk_parent) {
113 err = clk_set_parent(channel->clk, channel->clk_parent);
114 if (err < 0) {
115 dev_err(dev, "failed to set parent %s for %s: %d\n",
116 __clk_get_name(channel->clk_parent),
117 __clk_get_name(channel->clk), err);
118 return err;
119 }
120 }
121
122 err = clk_prepare_enable(channel->clk);
123 if (err < 0) {
124 dev_err(dev, "failed to enable clock %s: %d\n",
125 __clk_get_name(channel->clk), err);
126 return err;
127 }
128
129 chip->ops->get_state(chip, pwm, &channel->state);
130
Martin Blumenstingl1064c6b2019-06-12 21:59:06 +0200131 return pwm_set_chip_data(pwm, channel);
Neil Armstrong211ed632016-08-22 17:36:30 +0200132}
133
134static void meson_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm)
135{
136 struct meson_pwm_channel *channel = pwm_get_chip_data(pwm);
137
138 if (channel)
139 clk_disable_unprepare(channel->clk);
140}
141
Martin Blumenstingl7e032162019-06-12 21:59:03 +0200142static int meson_pwm_calc(struct meson_pwm *meson, struct pwm_device *pwm,
Martin Blumenstinglb79c3672019-06-12 21:59:02 +0200143 struct pwm_state *state)
Neil Armstrong211ed632016-08-22 17:36:30 +0200144{
Martin Blumenstingl7e032162019-06-12 21:59:03 +0200145 struct meson_pwm_channel *channel = pwm_get_chip_data(pwm);
Martin Blumenstinglb79c3672019-06-12 21:59:02 +0200146 unsigned int duty, period, pre_div, cnt, duty_cnt;
Jerome Brunetfd7b2be2017-06-08 14:24:16 +0200147 unsigned long fin_freq = -1;
148 u64 fin_ps;
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);
Jerome Brunetfd7b2be2017-06-08 14:24:16 +0200167 fin_ps = (u64)NSEC_PER_SEC * 1000;
168 do_div(fin_ps, fin_freq);
Neil Armstrong211ed632016-08-22 17:36:30 +0200169
170 /* Calc pre_div with the period */
Martin Blumenstingl51496e42019-04-01 20:18:16 +0200171 for (pre_div = 0; pre_div <= MISC_CLK_DIV_MASK; pre_div++) {
Jerome Brunetfd7b2be2017-06-08 14:24:16 +0200172 cnt = DIV_ROUND_CLOSEST_ULL((u64)period * 1000,
173 fin_ps * (pre_div + 1));
174 dev_dbg(meson->chip.dev, "fin_ps=%llu pre_div=%u cnt=%u\n",
175 fin_ps, pre_div, cnt);
Neil Armstrong211ed632016-08-22 17:36:30 +0200176 if (cnt <= 0xffff)
177 break;
178 }
179
Martin Blumenstingl51496e42019-04-01 20:18:16 +0200180 if (pre_div > MISC_CLK_DIV_MASK) {
Neil Armstrong211ed632016-08-22 17:36:30 +0200181 dev_err(meson->chip.dev, "unable to get period pre_div\n");
182 return -EINVAL;
183 }
184
185 dev_dbg(meson->chip.dev, "period=%u pre_div=%u cnt=%u\n", period,
186 pre_div, cnt);
187
188 if (duty == period) {
189 channel->pre_div = pre_div;
190 channel->hi = cnt;
191 channel->lo = 0;
192 } else if (duty == 0) {
193 channel->pre_div = pre_div;
194 channel->hi = 0;
195 channel->lo = cnt;
196 } else {
197 /* Then check is we can have the duty with the same pre_div */
Jerome Brunetfd7b2be2017-06-08 14:24:16 +0200198 duty_cnt = DIV_ROUND_CLOSEST_ULL((u64)duty * 1000,
199 fin_ps * (pre_div + 1));
Neil Armstrong211ed632016-08-22 17:36:30 +0200200 if (duty_cnt > 0xffff) {
201 dev_err(meson->chip.dev, "unable to get duty cycle\n");
202 return -EINVAL;
203 }
204
205 dev_dbg(meson->chip.dev, "duty=%u pre_div=%u duty_cnt=%u\n",
206 duty, pre_div, duty_cnt);
207
208 channel->pre_div = pre_div;
209 channel->hi = duty_cnt;
210 channel->lo = cnt - duty_cnt;
211 }
212
213 return 0;
214}
215
Martin Blumenstingl084f1372019-06-12 21:58:58 +0200216static void meson_pwm_enable(struct meson_pwm *meson, struct pwm_device *pwm)
Neil Armstrong211ed632016-08-22 17:36:30 +0200217{
Martin Blumenstingl084f1372019-06-12 21:58:58 +0200218 struct meson_pwm_channel *channel = pwm_get_chip_data(pwm);
Martin Blumenstingl8bbf3162019-06-12 21:59:05 +0200219 struct meson_pwm_channel_data *channel_data;
Martin Blumenstinglf1737472019-04-01 19:57:48 +0200220 unsigned long flags;
Martin Blumenstingl8bbf3162019-06-12 21:59:05 +0200221 u32 value;
Neil Armstrong211ed632016-08-22 17:36:30 +0200222
Martin Blumenstingl8bbf3162019-06-12 21:59:05 +0200223 channel_data = &meson_pwm_per_channel_data[pwm->hwpwm];
Neil Armstrong211ed632016-08-22 17:36:30 +0200224
Martin Blumenstinglf1737472019-04-01 19:57:48 +0200225 spin_lock_irqsave(&meson->lock, flags);
226
Neil Armstrong211ed632016-08-22 17:36:30 +0200227 value = readl(meson->base + REG_MISC_AB);
Martin Blumenstingl8bbf3162019-06-12 21:59:05 +0200228 value &= ~(MISC_CLK_DIV_MASK << channel_data->clk_div_shift);
229 value |= channel->pre_div << channel_data->clk_div_shift;
230 value |= channel_data->clk_en_mask;
Neil Armstrong211ed632016-08-22 17:36:30 +0200231 writel(value, meson->base + REG_MISC_AB);
232
Martin Blumenstingl181164b2019-06-12 21:59:00 +0200233 value = FIELD_PREP(PWM_HIGH_MASK, channel->hi) |
234 FIELD_PREP(PWM_LOW_MASK, channel->lo);
Martin Blumenstingl8bbf3162019-06-12 21:59:05 +0200235 writel(value, meson->base + channel_data->reg_offset);
Neil Armstrong211ed632016-08-22 17:36:30 +0200236
237 value = readl(meson->base + REG_MISC_AB);
Martin Blumenstingl8bbf3162019-06-12 21:59:05 +0200238 value |= channel_data->pwm_en_mask;
Neil Armstrong211ed632016-08-22 17:36:30 +0200239 writel(value, meson->base + REG_MISC_AB);
Martin Blumenstinglf1737472019-04-01 19:57:48 +0200240
241 spin_unlock_irqrestore(&meson->lock, flags);
Neil Armstrong211ed632016-08-22 17:36:30 +0200242}
243
Martin Blumenstingl084f1372019-06-12 21:58:58 +0200244static void meson_pwm_disable(struct meson_pwm *meson, struct pwm_device *pwm)
Neil Armstrong211ed632016-08-22 17:36:30 +0200245{
Martin Blumenstinglf1737472019-04-01 19:57:48 +0200246 unsigned long flags;
Martin Blumenstingl8bbf3162019-06-12 21:59:05 +0200247 u32 value;
Neil Armstrong211ed632016-08-22 17:36:30 +0200248
Martin Blumenstinglf1737472019-04-01 19:57:48 +0200249 spin_lock_irqsave(&meson->lock, flags);
250
Neil Armstrong211ed632016-08-22 17:36:30 +0200251 value = readl(meson->base + REG_MISC_AB);
Martin Blumenstingl8bbf3162019-06-12 21:59:05 +0200252 value &= ~meson_pwm_per_channel_data[pwm->hwpwm].pwm_en_mask;
Neil Armstrong211ed632016-08-22 17:36:30 +0200253 writel(value, meson->base + REG_MISC_AB);
Martin Blumenstinglf1737472019-04-01 19:57:48 +0200254
255 spin_unlock_irqrestore(&meson->lock, flags);
Neil Armstrong211ed632016-08-22 17:36:30 +0200256}
257
258static int meson_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
259 struct pwm_state *state)
260{
261 struct meson_pwm_channel *channel = pwm_get_chip_data(pwm);
262 struct meson_pwm *meson = to_meson_pwm(chip);
Neil Armstrong211ed632016-08-22 17:36:30 +0200263 int err = 0;
264
265 if (!state)
266 return -EINVAL;
267
Neil Armstrong211ed632016-08-22 17:36:30 +0200268 if (!state->enabled) {
Martin Blumenstingl084f1372019-06-12 21:58:58 +0200269 meson_pwm_disable(meson, pwm);
Neil Armstrong211ed632016-08-22 17:36:30 +0200270 channel->state.enabled = false;
271
Martin Blumenstinglf1737472019-04-01 19:57:48 +0200272 return 0;
Neil Armstrong211ed632016-08-22 17:36:30 +0200273 }
274
275 if (state->period != channel->state.period ||
276 state->duty_cycle != channel->state.duty_cycle ||
277 state->polarity != channel->state.polarity) {
Martin Blumenstingl7e032162019-06-12 21:59:03 +0200278 err = meson_pwm_calc(meson, pwm, state);
Neil Armstrong211ed632016-08-22 17:36:30 +0200279 if (err < 0)
Martin Blumenstinglf1737472019-04-01 19:57:48 +0200280 return err;
Neil Armstrong211ed632016-08-22 17:36:30 +0200281
282 channel->state.polarity = state->polarity;
283 channel->state.period = state->period;
284 channel->state.duty_cycle = state->duty_cycle;
285 }
286
287 if (state->enabled && !channel->state.enabled) {
Martin Blumenstingl084f1372019-06-12 21:58:58 +0200288 meson_pwm_enable(meson, pwm);
Neil Armstrong211ed632016-08-22 17:36:30 +0200289 channel->state.enabled = true;
290 }
291
Martin Blumenstinglf1737472019-04-01 19:57:48 +0200292 return 0;
Neil Armstrong211ed632016-08-22 17:36:30 +0200293}
294
295static void meson_pwm_get_state(struct pwm_chip *chip, struct pwm_device *pwm,
296 struct pwm_state *state)
297{
298 struct meson_pwm *meson = to_meson_pwm(chip);
299 u32 value, mask;
300
301 if (!state)
302 return;
303
Martin Blumenstingl8bbf3162019-06-12 21:59:05 +0200304 mask = meson_pwm_per_channel_data[pwm->hwpwm].pwm_en_mask;
Neil Armstrong211ed632016-08-22 17:36:30 +0200305
306 value = readl(meson->base + REG_MISC_AB);
307 state->enabled = (value & mask) != 0;
308}
309
310static const struct pwm_ops meson_pwm_ops = {
311 .request = meson_pwm_request,
312 .free = meson_pwm_free,
313 .apply = meson_pwm_apply,
314 .get_state = meson_pwm_get_state,
315 .owner = THIS_MODULE,
316};
317
318static const char * const pwm_meson8b_parent_names[] = {
319 "xtal", "vid_pll", "fclk_div4", "fclk_div3"
320};
321
322static const struct meson_pwm_data pwm_meson8b_data = {
323 .parent_names = pwm_meson8b_parent_names,
Jerome Brunetd396b202017-06-08 14:24:15 +0200324 .num_parents = ARRAY_SIZE(pwm_meson8b_parent_names),
Neil Armstrong211ed632016-08-22 17:36:30 +0200325};
326
327static const char * const pwm_gxbb_parent_names[] = {
328 "xtal", "hdmi_pll", "fclk_div4", "fclk_div3"
329};
330
331static const struct meson_pwm_data pwm_gxbb_data = {
332 .parent_names = pwm_gxbb_parent_names,
Jerome Brunetd396b202017-06-08 14:24:15 +0200333 .num_parents = ARRAY_SIZE(pwm_gxbb_parent_names),
334};
335
336/*
337 * Only the 2 first inputs of the GXBB AO PWMs are valid
338 * The last 2 are grounded
339 */
340static const char * const pwm_gxbb_ao_parent_names[] = {
341 "xtal", "clk81"
342};
343
344static const struct meson_pwm_data pwm_gxbb_ao_data = {
345 .parent_names = pwm_gxbb_ao_parent_names,
346 .num_parents = ARRAY_SIZE(pwm_gxbb_ao_parent_names),
Neil Armstrong211ed632016-08-22 17:36:30 +0200347};
348
Jian Hubccaa3f2017-12-04 14:00:17 +0800349static const char * const pwm_axg_ee_parent_names[] = {
350 "xtal", "fclk_div5", "fclk_div4", "fclk_div3"
351};
352
353static const struct meson_pwm_data pwm_axg_ee_data = {
354 .parent_names = pwm_axg_ee_parent_names,
355 .num_parents = ARRAY_SIZE(pwm_axg_ee_parent_names),
356};
357
358static const char * const pwm_axg_ao_parent_names[] = {
359 "aoclk81", "xtal", "fclk_div4", "fclk_div5"
360};
361
362static const struct meson_pwm_data pwm_axg_ao_data = {
363 .parent_names = pwm_axg_ao_parent_names,
364 .num_parents = ARRAY_SIZE(pwm_axg_ao_parent_names),
365};
366
Neil Armstrong9bce02e2019-06-20 16:46:55 +0200367static const char * const pwm_g12a_ao_ab_parent_names[] = {
368 "xtal", "aoclk81", "fclk_div4", "fclk_div5"
369};
370
371static const struct meson_pwm_data pwm_g12a_ao_ab_data = {
372 .parent_names = pwm_g12a_ao_ab_parent_names,
373 .num_parents = ARRAY_SIZE(pwm_g12a_ao_ab_parent_names),
374};
375
Neil Armstrongf41efce2019-04-23 15:36:45 +0200376static const char * const pwm_g12a_ao_cd_parent_names[] = {
Neil Armstrong9bce02e2019-06-20 16:46:55 +0200377 "xtal", "aoclk81",
Neil Armstrongf41efce2019-04-23 15:36:45 +0200378};
379
380static const struct meson_pwm_data pwm_g12a_ao_cd_data = {
381 .parent_names = pwm_g12a_ao_cd_parent_names,
382 .num_parents = ARRAY_SIZE(pwm_g12a_ao_cd_parent_names),
383};
384
385static const char * const pwm_g12a_ee_parent_names[] = {
386 "xtal", "hdmi_pll", "fclk_div4", "fclk_div3"
387};
388
389static const struct meson_pwm_data pwm_g12a_ee_data = {
390 .parent_names = pwm_g12a_ee_parent_names,
391 .num_parents = ARRAY_SIZE(pwm_g12a_ee_parent_names),
392};
393
Neil Armstrong211ed632016-08-22 17:36:30 +0200394static const struct of_device_id meson_pwm_matches[] = {
Jerome Brunetd396b202017-06-08 14:24:15 +0200395 {
396 .compatible = "amlogic,meson8b-pwm",
397 .data = &pwm_meson8b_data
398 },
399 {
400 .compatible = "amlogic,meson-gxbb-pwm",
401 .data = &pwm_gxbb_data
402 },
403 {
404 .compatible = "amlogic,meson-gxbb-ao-pwm",
405 .data = &pwm_gxbb_ao_data
406 },
Jian Hubccaa3f2017-12-04 14:00:17 +0800407 {
408 .compatible = "amlogic,meson-axg-ee-pwm",
409 .data = &pwm_axg_ee_data
410 },
411 {
412 .compatible = "amlogic,meson-axg-ao-pwm",
413 .data = &pwm_axg_ao_data
414 },
Neil Armstrongf41efce2019-04-23 15:36:45 +0200415 {
416 .compatible = "amlogic,meson-g12a-ee-pwm",
417 .data = &pwm_g12a_ee_data
418 },
419 {
420 .compatible = "amlogic,meson-g12a-ao-pwm-ab",
Neil Armstrong9bce02e2019-06-20 16:46:55 +0200421 .data = &pwm_g12a_ao_ab_data
Neil Armstrongf41efce2019-04-23 15:36:45 +0200422 },
423 {
424 .compatible = "amlogic,meson-g12a-ao-pwm-cd",
425 .data = &pwm_g12a_ao_cd_data
426 },
Neil Armstrong211ed632016-08-22 17:36:30 +0200427 {},
428};
429MODULE_DEVICE_TABLE(of, meson_pwm_matches);
430
Martin Blumenstingla50a49a2019-06-12 21:59:04 +0200431static int meson_pwm_init_channels(struct meson_pwm *meson)
Neil Armstrong211ed632016-08-22 17:36:30 +0200432{
433 struct device *dev = meson->chip.dev;
Neil Armstrong211ed632016-08-22 17:36:30 +0200434 struct clk_init_data init;
435 unsigned int i;
436 char name[255];
437 int err;
438
439 for (i = 0; i < meson->chip.npwm; i++) {
Martin Blumenstingla50a49a2019-06-12 21:59:04 +0200440 struct meson_pwm_channel *channel = &meson->channels[i];
Neil Armstrong211ed632016-08-22 17:36:30 +0200441
Jerome Brunetb96e9eb2018-08-01 12:57:20 +0200442 snprintf(name, sizeof(name), "%s#mux%u", dev_name(dev), i);
Neil Armstrong211ed632016-08-22 17:36:30 +0200443
444 init.name = name;
445 init.ops = &clk_mux_ops;
Stephen Boyd90b6c5c2019-04-25 10:57:37 -0700446 init.flags = 0;
Neil Armstrong211ed632016-08-22 17:36:30 +0200447 init.parent_names = meson->data->parent_names;
Jerome Brunetd396b202017-06-08 14:24:15 +0200448 init.num_parents = meson->data->num_parents;
Neil Armstrong211ed632016-08-22 17:36:30 +0200449
450 channel->mux.reg = meson->base + REG_MISC_AB;
Martin Blumenstingl8bbf3162019-06-12 21:59:05 +0200451 channel->mux.shift =
452 meson_pwm_per_channel_data[i].clk_sel_shift;
Martin Blumenstingl33cefd82019-06-12 21:59:01 +0200453 channel->mux.mask = MISC_CLK_SEL_MASK;
Neil Armstrong211ed632016-08-22 17:36:30 +0200454 channel->mux.flags = 0;
455 channel->mux.lock = &meson->lock;
456 channel->mux.table = NULL;
457 channel->mux.hw.init = &init;
458
459 channel->clk = devm_clk_register(dev, &channel->mux.hw);
460 if (IS_ERR(channel->clk)) {
461 err = PTR_ERR(channel->clk);
462 dev_err(dev, "failed to register %s: %d\n", name, err);
463 return err;
464 }
465
466 snprintf(name, sizeof(name), "clkin%u", i);
467
Martin Blumenstinglba4004c2019-06-12 21:58:59 +0200468 channel->clk_parent = devm_clk_get_optional(dev, name);
469 if (IS_ERR(channel->clk_parent))
470 return PTR_ERR(channel->clk_parent);
Neil Armstrong211ed632016-08-22 17:36:30 +0200471 }
472
473 return 0;
474}
475
Neil Armstrong211ed632016-08-22 17:36:30 +0200476static int meson_pwm_probe(struct platform_device *pdev)
477{
Neil Armstrong211ed632016-08-22 17:36:30 +0200478 struct meson_pwm *meson;
479 struct resource *regs;
480 int err;
481
482 meson = devm_kzalloc(&pdev->dev, sizeof(*meson), GFP_KERNEL);
483 if (!meson)
484 return -ENOMEM;
485
486 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
487 meson->base = devm_ioremap_resource(&pdev->dev, regs);
488 if (IS_ERR(meson->base))
489 return PTR_ERR(meson->base);
490
Axel Linc6999952016-09-10 09:55:49 +0800491 spin_lock_init(&meson->lock);
Neil Armstrong211ed632016-08-22 17:36:30 +0200492 meson->chip.dev = &pdev->dev;
493 meson->chip.ops = &meson_pwm_ops;
494 meson->chip.base = -1;
Martin Blumenstingla50a49a2019-06-12 21:59:04 +0200495 meson->chip.npwm = MESON_NUM_PWMS;
Neil Armstrong211ed632016-08-22 17:36:30 +0200496 meson->chip.of_xlate = of_pwm_xlate_with_flags;
497 meson->chip.of_pwm_n_cells = 3;
498
499 meson->data = of_device_get_match_data(&pdev->dev);
Neil Armstrong211ed632016-08-22 17:36:30 +0200500
Martin Blumenstingla50a49a2019-06-12 21:59:04 +0200501 err = meson_pwm_init_channels(meson);
Neil Armstrong211ed632016-08-22 17:36:30 +0200502 if (err < 0)
503 return err;
504
505 err = pwmchip_add(&meson->chip);
506 if (err < 0) {
507 dev_err(&pdev->dev, "failed to register PWM chip: %d\n", err);
508 return err;
509 }
510
Neil Armstrong211ed632016-08-22 17:36:30 +0200511 platform_set_drvdata(pdev, meson);
512
513 return 0;
514}
515
516static int meson_pwm_remove(struct platform_device *pdev)
517{
518 struct meson_pwm *meson = platform_get_drvdata(pdev);
519
520 return pwmchip_remove(&meson->chip);
521}
522
523static struct platform_driver meson_pwm_driver = {
524 .driver = {
525 .name = "meson-pwm",
526 .of_match_table = meson_pwm_matches,
527 },
528 .probe = meson_pwm_probe,
529 .remove = meson_pwm_remove,
530};
531module_platform_driver(meson_pwm_driver);
532
Neil Armstrong211ed632016-08-22 17:36:30 +0200533MODULE_DESCRIPTION("Amlogic Meson PWM Generator driver");
534MODULE_AUTHOR("Neil Armstrong <narmstrong@baylibre.com>");
535MODULE_LICENSE("Dual BSD/GPL");