pwm-mediatek.c 8.1 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0
J
John Crispin 已提交
2
/*
3
 * MediaTek Pulse Width Modulator driver
J
John Crispin 已提交
4 5
 *
 * Copyright (C) 2015 John Crispin <blogic@openwrt.org>
Z
Zhi Mao 已提交
6
 * Copyright (C) 2017 Zhi Mao <zhi.mao@mediatek.com>
J
John Crispin 已提交
7 8 9 10 11 12 13 14 15 16
 *
 */

#include <linux/err.h>
#include <linux/io.h>
#include <linux/ioport.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/clk.h>
#include <linux/of.h>
Z
Zhi Mao 已提交
17
#include <linux/of_device.h>
J
John Crispin 已提交
18 19 20 21 22 23 24 25 26 27 28 29
#include <linux/platform_device.h>
#include <linux/pwm.h>
#include <linux/slab.h>
#include <linux/types.h>

/* PWM registers and bits definitions */
#define PWMCON			0x00
#define PWMHDUR			0x04
#define PWMLDUR			0x08
#define PWMGDUR			0x0c
#define PWMWAVENUM		0x28
#define PWMDWIDTH		0x2c
30
#define PWM45DWIDTH_FIXUP	0x30
J
John Crispin 已提交
31
#define PWMTHRES		0x30
32
#define PWM45THRES_FIXUP	0x34
J
John Crispin 已提交
33

34 35
#define PWM_CLK_DIV_MAX		7

36
struct pwm_mediatek_of_data {
Z
Zhi Mao 已提交
37
	unsigned int num_pwms;
38
	bool pwm45_fixup;
J
John Crispin 已提交
39 40 41
};

/**
42
 * struct pwm_mediatek_chip - struct representing PWM chip
J
John Crispin 已提交
43 44
 * @chip: linux PWM chip representation
 * @regs: base address of PWM chip
45 46 47 48
 * @clk_top: the top clock generator
 * @clk_main: the clock used by PWM core
 * @clk_pwms: the clock used by each PWM channel
 * @clk_freq: the fix clock frequency of legacy MIPS SoC
J
John Crispin 已提交
49
 */
50
struct pwm_mediatek_chip {
J
John Crispin 已提交
51 52
	struct pwm_chip chip;
	void __iomem *regs;
53 54 55
	struct clk *clk_top;
	struct clk *clk_main;
	struct clk **clk_pwms;
56
	const struct pwm_mediatek_of_data *soc;
J
John Crispin 已提交
57 58
};

59
static const unsigned int pwm_mediatek_reg_offset[] = {
Z
Zhi Mao 已提交
60 61 62
	0x0010, 0x0050, 0x0090, 0x00d0, 0x0110, 0x0150, 0x0190, 0x0220
};

63 64
static inline struct pwm_mediatek_chip *
to_pwm_mediatek_chip(struct pwm_chip *chip)
J
John Crispin 已提交
65
{
66
	return container_of(chip, struct pwm_mediatek_chip, chip);
J
John Crispin 已提交
67 68
}

69 70
static int pwm_mediatek_clk_enable(struct pwm_chip *chip,
				   struct pwm_device *pwm)
Z
Zhi Mao 已提交
71
{
72
	struct pwm_mediatek_chip *pc = to_pwm_mediatek_chip(chip);
Z
Zhi Mao 已提交
73 74
	int ret;

75
	ret = clk_prepare_enable(pc->clk_top);
Z
Zhi Mao 已提交
76 77 78
	if (ret < 0)
		return ret;

79
	ret = clk_prepare_enable(pc->clk_main);
Z
Zhi Mao 已提交
80 81 82
	if (ret < 0)
		goto disable_clk_top;

83
	ret = clk_prepare_enable(pc->clk_pwms[pwm->hwpwm]);
Z
Zhi Mao 已提交
84 85 86 87 88 89
	if (ret < 0)
		goto disable_clk_main;

	return 0;

disable_clk_main:
90
	clk_disable_unprepare(pc->clk_main);
Z
Zhi Mao 已提交
91
disable_clk_top:
92
	clk_disable_unprepare(pc->clk_top);
Z
Zhi Mao 已提交
93 94 95 96

	return ret;
}

97 98
static void pwm_mediatek_clk_disable(struct pwm_chip *chip,
				     struct pwm_device *pwm)
Z
Zhi Mao 已提交
99
{
100
	struct pwm_mediatek_chip *pc = to_pwm_mediatek_chip(chip);
Z
Zhi Mao 已提交
101

102 103 104
	clk_disable_unprepare(pc->clk_pwms[pwm->hwpwm]);
	clk_disable_unprepare(pc->clk_main);
	clk_disable_unprepare(pc->clk_top);
Z
Zhi Mao 已提交
105 106
}

107 108
static inline u32 pwm_mediatek_readl(struct pwm_mediatek_chip *chip,
				     unsigned int num, unsigned int offset)
J
John Crispin 已提交
109
{
110
	return readl(chip->regs + pwm_mediatek_reg_offset[num] + offset);
J
John Crispin 已提交
111 112
}

113 114 115
static inline void pwm_mediatek_writel(struct pwm_mediatek_chip *chip,
				       unsigned int num, unsigned int offset,
				       u32 value)
J
John Crispin 已提交
116
{
117
	writel(value, chip->regs + pwm_mediatek_reg_offset[num] + offset);
J
John Crispin 已提交
118 119
}

120 121
static int pwm_mediatek_config(struct pwm_chip *chip, struct pwm_device *pwm,
			       int duty_ns, int period_ns)
J
John Crispin 已提交
122
{
123
	struct pwm_mediatek_chip *pc = to_pwm_mediatek_chip(chip);
124
	u32 clkdiv = 0, cnt_period, cnt_duty, reg_width = PWMDWIDTH,
125
	    reg_thres = PWMTHRES;
126
	u64 resolution;
Z
Zhi Mao 已提交
127 128
	int ret;

129 130
	ret = pwm_mediatek_clk_enable(chip, pwm);

Z
Zhi Mao 已提交
131 132
	if (ret < 0)
		return ret;
J
John Crispin 已提交
133

134 135
	/* Using resolution in picosecond gets accuracy higher */
	resolution = (u64)NSEC_PER_SEC * 1000;
136
	do_div(resolution, clk_get_rate(pc->clk_pwms[pwm->hwpwm]));
J
John Crispin 已提交
137

138 139
	cnt_period = DIV_ROUND_CLOSEST_ULL((u64)period_ns * 1000, resolution);
	while (cnt_period > 8191) {
J
John Crispin 已提交
140 141
		resolution *= 2;
		clkdiv++;
142 143
		cnt_period = DIV_ROUND_CLOSEST_ULL((u64)period_ns * 1000,
						   resolution);
J
John Crispin 已提交
144 145
	}

146
	if (clkdiv > PWM_CLK_DIV_MAX) {
147
		pwm_mediatek_clk_disable(chip, pwm);
148
		dev_err(chip->dev, "period %d not supported\n", period_ns);
J
John Crispin 已提交
149
		return -EINVAL;
150
	}
J
John Crispin 已提交
151

152 153 154 155 156 157 158 159 160
	if (pc->soc->pwm45_fixup && pwm->hwpwm > 2) {
		/*
		 * PWM[4,5] has distinct offset for PWMDWIDTH and PWMTHRES
		 * from the other PWMs on MT7623.
		 */
		reg_width = PWM45DWIDTH_FIXUP;
		reg_thres = PWM45THRES_FIXUP;
	}

161
	cnt_duty = DIV_ROUND_CLOSEST_ULL((u64)duty_ns * 1000, resolution);
162 163 164
	pwm_mediatek_writel(pc, pwm->hwpwm, PWMCON, BIT(15) | clkdiv);
	pwm_mediatek_writel(pc, pwm->hwpwm, reg_width, cnt_period);
	pwm_mediatek_writel(pc, pwm->hwpwm, reg_thres, cnt_duty);
J
John Crispin 已提交
165

166
	pwm_mediatek_clk_disable(chip, pwm);
Z
Zhi Mao 已提交
167

J
John Crispin 已提交
168 169 170
	return 0;
}

171
static int pwm_mediatek_enable(struct pwm_chip *chip, struct pwm_device *pwm)
J
John Crispin 已提交
172
{
173
	struct pwm_mediatek_chip *pc = to_pwm_mediatek_chip(chip);
J
John Crispin 已提交
174 175 176
	u32 value;
	int ret;

177
	ret = pwm_mediatek_clk_enable(chip, pwm);
J
John Crispin 已提交
178 179 180 181 182 183 184 185 186 187
	if (ret < 0)
		return ret;

	value = readl(pc->regs);
	value |= BIT(pwm->hwpwm);
	writel(value, pc->regs);

	return 0;
}

188
static void pwm_mediatek_disable(struct pwm_chip *chip, struct pwm_device *pwm)
J
John Crispin 已提交
189
{
190
	struct pwm_mediatek_chip *pc = to_pwm_mediatek_chip(chip);
J
John Crispin 已提交
191 192 193 194 195 196
	u32 value;

	value = readl(pc->regs);
	value &= ~BIT(pwm->hwpwm);
	writel(value, pc->regs);

197
	pwm_mediatek_clk_disable(chip, pwm);
J
John Crispin 已提交
198 199
}

200 201 202 203
static const struct pwm_ops pwm_mediatek_ops = {
	.config = pwm_mediatek_config,
	.enable = pwm_mediatek_enable,
	.disable = pwm_mediatek_disable,
J
John Crispin 已提交
204 205 206
	.owner = THIS_MODULE,
};

207
static int pwm_mediatek_probe(struct platform_device *pdev)
J
John Crispin 已提交
208
{
209
	struct pwm_mediatek_chip *pc;
J
John Crispin 已提交
210 211 212 213 214 215 216 217
	struct resource *res;
	unsigned int i;
	int ret;

	pc = devm_kzalloc(&pdev->dev, sizeof(*pc), GFP_KERNEL);
	if (!pc)
		return -ENOMEM;

218
	pc->soc = of_device_get_match_data(&pdev->dev);
Z
Zhi Mao 已提交
219

J
John Crispin 已提交
220 221 222 223 224
	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	pc->regs = devm_ioremap_resource(&pdev->dev, res);
	if (IS_ERR(pc->regs))
		return PTR_ERR(pc->regs);

225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
	pc->clk_pwms = devm_kcalloc(&pdev->dev, pc->soc->num_pwms,
				    sizeof(*pc->clk_pwms), GFP_KERNEL);
	if (!pc->clk_pwms)
		return -ENOMEM;

	pc->clk_top = devm_clk_get(&pdev->dev, "top");
	if (IS_ERR(pc->clk_top)) {
		dev_err(&pdev->dev, "clock: top fail: %ld\n",
			PTR_ERR(pc->clk_top));
		return PTR_ERR(pc->clk_top);
	}

	pc->clk_main = devm_clk_get(&pdev->dev, "main");
	if (IS_ERR(pc->clk_main)) {
		dev_err(&pdev->dev, "clock: main fail: %ld\n",
			PTR_ERR(pc->clk_main));
		return PTR_ERR(pc->clk_main);
	}

	for (i = 0; i < pc->soc->num_pwms; i++) {
		char name[8];

		snprintf(name, sizeof(name), "pwm%d", i + 1);

		pc->clk_pwms[i] = devm_clk_get(&pdev->dev, name);
		if (IS_ERR(pc->clk_pwms[i])) {
Z
Zhi Mao 已提交
251
			dev_err(&pdev->dev, "clock: %s fail: %ld\n",
252 253
				name, PTR_ERR(pc->clk_pwms[i]));
			return PTR_ERR(pc->clk_pwms[i]);
Z
Zhi Mao 已提交
254
		}
J
John Crispin 已提交
255 256 257 258 259
	}

	platform_set_drvdata(pdev, pc);

	pc->chip.dev = &pdev->dev;
260
	pc->chip.ops = &pwm_mediatek_ops;
J
John Crispin 已提交
261
	pc->chip.base = -1;
262
	pc->chip.npwm = pc->soc->num_pwms;
J
John Crispin 已提交
263 264 265 266

	ret = pwmchip_add(&pc->chip);
	if (ret < 0) {
		dev_err(&pdev->dev, "pwmchip_add() failed: %d\n", ret);
Z
Zhi Mao 已提交
267
		return ret;
J
John Crispin 已提交
268 269 270 271 272
	}

	return 0;
}

273
static int pwm_mediatek_remove(struct platform_device *pdev)
J
John Crispin 已提交
274
{
275
	struct pwm_mediatek_chip *pc = platform_get_drvdata(pdev);
J
John Crispin 已提交
276 277 278 279

	return pwmchip_remove(&pc->chip);
}

280
static const struct pwm_mediatek_of_data mt2712_pwm_data = {
Z
Zhi Mao 已提交
281
	.num_pwms = 8,
282
	.pwm45_fixup = false,
Z
Zhi Mao 已提交
283 284
};

285
static const struct pwm_mediatek_of_data mt7622_pwm_data = {
Z
Zhi Mao 已提交
286
	.num_pwms = 6,
287
	.pwm45_fixup = false,
Z
Zhi Mao 已提交
288 289
};

290
static const struct pwm_mediatek_of_data mt7623_pwm_data = {
Z
Zhi Mao 已提交
291
	.num_pwms = 5,
292
	.pwm45_fixup = true,
J
John Crispin 已提交
293 294
};

295
static const struct pwm_mediatek_of_data mt7628_pwm_data = {
J
John Crispin 已提交
296 297
	.num_pwms = 4,
	.pwm45_fixup = true,
Z
Zhi Mao 已提交
298 299
};

300 301 302 303 304
static const struct pwm_mediatek_of_data mt7629_pwm_data = {
	.num_pwms = 1,
	.pwm45_fixup = false,
};

305
static const struct pwm_mediatek_of_data mt8516_pwm_data = {
306 307 308 309
	.num_pwms = 5,
	.pwm45_fixup = false,
};

310
static const struct of_device_id pwm_mediatek_of_match[] = {
Z
Zhi Mao 已提交
311 312 313
	{ .compatible = "mediatek,mt2712-pwm", .data = &mt2712_pwm_data },
	{ .compatible = "mediatek,mt7622-pwm", .data = &mt7622_pwm_data },
	{ .compatible = "mediatek,mt7623-pwm", .data = &mt7623_pwm_data },
J
John Crispin 已提交
314
	{ .compatible = "mediatek,mt7628-pwm", .data = &mt7628_pwm_data },
315
	{ .compatible = "mediatek,mt7629-pwm", .data = &mt7629_pwm_data },
316
	{ .compatible = "mediatek,mt8516-pwm", .data = &mt8516_pwm_data },
Z
Zhi Mao 已提交
317
	{ },
J
John Crispin 已提交
318
};
319
MODULE_DEVICE_TABLE(of, pwm_mediatek_of_match);
J
John Crispin 已提交
320

321
static struct platform_driver pwm_mediatek_driver = {
J
John Crispin 已提交
322
	.driver = {
323 324
		.name = "pwm-mediatek",
		.of_match_table = pwm_mediatek_of_match,
J
John Crispin 已提交
325
	},
326 327
	.probe = pwm_mediatek_probe,
	.remove = pwm_mediatek_remove,
J
John Crispin 已提交
328
};
329
module_platform_driver(pwm_mediatek_driver);
J
John Crispin 已提交
330 331

MODULE_AUTHOR("John Crispin <blogic@openwrt.org>");
332
MODULE_LICENSE("GPL v2");