aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/misc/ep93xx_pwm.c
blob: 96787ec15caddb534efac6f2b2a9a162b6487617 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
/*
 *  Simple PWM driver for EP93XX
 *
 *	(c) Copyright 2009  Matthieu Crapet <mcrapet@gmail.com>
 *	(c) Copyright 2009  H Hartley Sweeten <hsweeten@visionengravers.com>
 *
 *	This program is free software; you can redistribute it and/or
 *	modify it under the terms of the GNU General Public License
 *	as published by the Free Software Foundation; either version
 *	2 of the License, or (at your option) any later version.
 *
 *  EP9307 has only one channel:
 *    - PWMOUT
 *
 *  EP9301/02/12/15 have two channels:
 *    - PWMOUT
 *    - PWMOUT1 (alternate function for EGPIO14)
 */

#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/slab.h>
#include <linux/clk.h>
#include <linux/err.h>
#include <linux/io.h>

#include <mach/platform.h>

#define EP93XX_PWMx_TERM_COUNT	0x00
#define EP93XX_PWMx_DUTY_CYCLE	0x04
#define EP93XX_PWMx_ENABLE	0x08
#define EP93XX_PWMx_INVERT	0x0C

#define EP93XX_PWM_MAX_COUNT	0xFFFF

struct ep93xx_pwm {
	void __iomem	*mmio_base;
	struct clk	*clk;
	u32		duty_percent;
};

static inline void ep93xx_pwm_writel(struct ep93xx_pwm *pwm,
		unsigned int val, unsigned int off)
{
	__raw_writel(val, pwm->mmio_base + off);
}

static inline unsigned int ep93xx_pwm_readl(struct ep93xx_pwm *pwm,
		unsigned int off)
{
	return __raw_readl(pwm->mmio_base + off);
}

static inline void ep93xx_pwm_write_tc(struct ep93xx_pwm *pwm, u16 value)
{
	ep93xx_pwm_writel(pwm, value, EP93XX_PWMx_TERM_COUNT);
}

static inline u16 ep93xx_pwm_read_tc(struct ep93xx_pwm *pwm)
{
	return ep93xx_pwm_readl(pwm, EP93XX_PWMx_TERM_COUNT);
}

static inline void ep93xx_pwm_write_dc(struct ep93xx_pwm *pwm, u16 value)
{
	ep93xx_pwm_writel(pwm, value, EP93XX_PWMx_DUTY_CYCLE);
}

static inline void ep93xx_pwm_enable(struct ep93xx_pwm *pwm)
{
	ep93xx_pwm_writel(pwm, 0x1, EP93XX_PWMx_ENABLE);
}

static inline void ep93xx_pwm_disable(struct ep93xx_pwm *pwm)
{
	ep93xx_pwm_writel(pwm, 0x0, EP93XX_PWMx_ENABLE);
}

static inline int ep93xx_pwm_is_enabled(struct ep93xx_pwm *pwm)
{
	return ep93xx_pwm_readl(pwm, EP93XX_PWMx_ENABLE) & 0x1;
}

static inline void ep93xx_pwm_invert(struct ep93xx_pwm *pwm)
{
	ep93xx_pwm_writel(pwm, 0x1, EP93XX_PWMx_INVERT);
}

static inline void ep93xx_pwm_normal(struct ep93xx_pwm *pwm)
{
	ep93xx_pwm_writel(pwm, 0x0, EP93XX_PWMx_INVERT);
}

static inline int ep93xx_pwm_is_inverted(struct ep93xx_pwm *pwm)
{
	return ep93xx_pwm_readl(pwm, EP93XX_PWMx_INVERT) & 0x1;
}

/*
 * /sys/devices/platform/ep93xx-pwm.N
 *   /min_freq      read-only   minimum pwm output frequency
 *   /max_req       read-only   maximum pwm output frequency
 *   /freq          read-write  pwm output frequency (0 = disable output)
 *   /duty_percent  read-write  pwm duty cycle percent (1..99)
 *   /invert        read-write  invert pwm output
 */

static ssize_t ep93xx_pwm_get_min_freq(struct device *dev,
		struct device_attribute *attr, char *buf)
{
	struct platform_device *pdev = to_platform_device(dev);
	struct ep93xx_pwm *pwm = platform_get_drvdata(pdev);
	unsigned long rate = clk_get_rate(pwm->clk);

	return sprintf(buf, "%ld\n", rate / (EP93XX_PWM_MAX_COUNT + 1));
}

static ssize_t ep93xx_pwm_get_max_freq(struct device *dev,
		struct device_attribute *attr, char *buf)
{
	struct platform_device *pdev = to_platform_device(dev);
	struct ep93xx_pwm *pwm = platform_get_drvdata(pdev);
	unsigned long rate = clk_get_rate(pwm->clk);

	return sprintf(buf, "%ld\n", rate / 2);
}

static ssize_t ep93xx_pwm_get_freq(struct device *dev,
		struct device_attribute *attr, char *buf)
{
	struct platform_device *pdev = to_platform_device(dev);
	struct ep93xx_pwm *pwm = platform_get_drvdata(pdev);

	if (ep93xx_pwm_is_enabled(pwm)) {
		unsigned long rate = clk_get_rate(pwm->clk);
		u16 term = ep93xx_pwm_read_tc(pwm);

		return sprintf(buf, "%ld\n", rate / (term + 1));
	} else {
		return sprintf(buf, "disabled\n");
	}
}

static ssize_t ep93xx_pwm_set_freq(struct device *dev,
		struct device_attribute *attr, const char *buf, size_t count)
{
	struct platform_device *pdev = to_platform_device(dev);
	struct ep93xx_pwm *pwm = platform_get_drvdata(pdev);
	long val;
	int err;

	err = strict_strtol(buf, 10, &val);
	if (err)
		return -EINVAL;

	if (val == 0) {
		ep93xx_pwm_disable(pwm);
	} else if (val <= (clk_get_rate(pwm->clk) / 2)) {
		u32 term, duty;

		val = (clk_get_rate(pwm->clk) / val) - 1;
		if (val > EP93XX_PWM_MAX_COUNT)
			val = EP93XX_PWM_MAX_COUNT;
		if (val < 1)
			val = 1;

		term = ep93xx_pwm_read_tc(pwm);
		duty = ((val + 1) * pwm->duty_percent / 100) - 1;

		/* If pwm is running, order is important */
		if (val > term) {
			ep93xx_pwm_write_tc(pwm, val);
			ep93xx_pwm_write_dc(pwm, duty);
		} else {
			ep93xx_pwm_write_dc(pwm, duty);
			ep93xx_pwm_write_tc(pwm, val);
		}

		if (!ep93xx_pwm_is_enabled(pwm))
			ep93xx_pwm_enable(pwm);
	} else {
		return -EINVAL;
	}

	return count;
}

static ssize_t ep93xx_pwm_get_duty_percent(struct device *dev,
		struct device_attribute *attr, char *buf)
{
	struct platform_device *pdev = to_platform_device(dev);
	struct ep93xx_pwm *pwm = platform_get_drvdata(pdev);

	return sprintf(buf, "%d\n", pwm->duty_percent);
}

static ssize_t ep93xx_pwm_set_duty_percent(struct device *dev,
		struct device_attribute *attr, const char *buf, size_t count)
{
	struct platform_device *pdev = to_platform_device(dev);
	struct ep93xx_pwm *pwm = platform_get_drvdata(pdev);
	long val;
	int err;

	err = strict_strtol(buf, 10, &val);
	if (err)
		return -EINVAL;

	if (val > 0 && val < 100) {
		u32 term = ep93xx_pwm_read_tc(pwm);
		ep93xx_pwm_write_dc(pwm, ((term + 1) * val / 100) - 1);
		pwm->duty_percent = val;
		return count;
	}

	return -EINVAL;
}

static ssize_t ep93xx_pwm_get_invert(struct device *dev,
		struct device_attribute *attr, char *buf)
{
	struct platform_device *pdev = to_platform_device(dev);
	struct ep93xx_pwm *pwm = platform_get_drvdata(pdev);

	return sprintf(buf, "%d\n", ep93xx_pwm_is_inverted(pwm));
}

static ssize_t ep93xx_pwm_set_invert(struct device *dev,
		struct device_attribute *attr, const char *buf, size_t count)
{
	struct platform_device *pdev = to_platform_device(dev);
	struct ep93xx_pwm *pwm = platform_get_drvdata(pdev);
	long val;
	int err;

	err = strict_strtol(buf, 10, &val);
	if (err)
		return -EINVAL;

	if (val == 0)
		ep93xx_pwm_normal(pwm);
	else if (val == 1)
		ep93xx_pwm_invert(pwm);
	else
		return -EINVAL;

	return count;
}

static DEVICE_ATTR(min_freq, S_IRUGO, ep93xx_pwm_get_min_freq, NULL);
static DEVICE_ATTR(max_freq, S_IRUGO, ep93xx_pwm_get_max_freq, NULL);
static DEVICE_ATTR(freq, S_IWUSR | S_IRUGO,
		   ep93xx_pwm_get_freq, ep93xx_pwm_set_freq);
static DEVICE_ATTR(duty_percent, S_IWUSR | S_IRUGO,
		   ep93xx_pwm_get_duty_percent, ep93xx_pwm_set_duty_percent);
static DEVICE_ATTR(invert, S_IWUSR | S_IRUGO,
		   ep93xx_pwm_get_invert, ep93xx_pwm_set_invert);

static struct attribute *ep93xx_pwm_attrs[] = {
	&dev_attr_min_freq.attr,
	&dev_attr_max_freq.attr,
	&dev_attr_freq.attr,
	&dev_attr_duty_percent.attr,
	&dev_attr_invert.attr,
	NULL
};

static const struct attribute_group ep93xx_pwm_sysfs_files = {
	.attrs	= ep93xx_pwm_attrs,
};

static int __init ep93xx_pwm_probe(struct platform_device *pdev)
{
	struct ep93xx_pwm *pwm;
	struct resource *res;
	int err;

	err = ep93xx_pwm_acquire_gpio(pdev);
	if (err)
		return err;

	pwm = kzalloc(sizeof(struct ep93xx_pwm), GFP_KERNEL);
	if (!pwm) {
		err = -ENOMEM;
		goto fail_no_mem;
	}

	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	if (res == NULL) {
		err = -ENXIO;
		goto fail_no_mem_resource;
	}

	res = request_mem_region(res->start, resource_size(res), pdev->name);
	if (res == NULL) {
		err = -EBUSY;
		goto fail_no_mem_resource;
	}

	pwm->mmio_base = ioremap(res->start, resource_size(res));
	if (pwm->mmio_base == NULL) {
		err = -ENXIO;
		goto fail_no_ioremap;
	}

	err = sysfs_create_group(&pdev->dev.kobj, &ep93xx_pwm_sysfs_files);
	if (err)
		goto fail_no_sysfs;

	pwm->clk = clk_get(&pdev->dev, "pwm_clk");
	if (IS_ERR(pwm->clk)) {
		err = PTR_ERR(pwm->clk);
		goto fail_no_clk;
	}

	pwm->duty_percent = 50;

	platform_set_drvdata(pdev, pwm);

	/* disable pwm at startup. Avoids zero value. */
	ep93xx_pwm_disable(pwm);
	ep93xx_pwm_write_tc(pwm, EP93XX_PWM_MAX_COUNT);
	ep93xx_pwm_write_dc(pwm, EP93XX_PWM_MAX_COUNT / 2);

	clk_enable(pwm->clk);

	return 0;

fail_no_clk:
	sysfs_remove_group(&pdev->dev.kobj, &ep93xx_pwm_sysfs_files);
fail_no_sysfs:
	iounmap(pwm->mmio_base);
fail_no_ioremap:
	release_mem_region(res->start, resource_size(res));
fail_no_mem_resource:
	kfree(pwm);
fail_no_mem:
	ep93xx_pwm_release_gpio(pdev);
	return err;
}

static int __exit ep93xx_pwm_remove(struct platform_device *pdev)
{
	struct ep93xx_pwm *pwm = platform_get_drvdata(pdev);
	struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);

	ep93xx_pwm_disable(pwm);
	clk_disable(pwm->clk);
	clk_put(pwm->clk);
	platform_set_drvdata(pdev, NULL);
	sysfs_remove_group(&pdev->dev.kobj, &ep93xx_pwm_sysfs_files);
	iounmap(pwm->mmio_base);
	release_mem_region(res->start, resource_size(res));
	kfree(pwm);
	ep93xx_pwm_release_gpio(pdev);

	return 0;
}

static struct platform_driver ep93xx_pwm_driver = {
	.driver		= {
		.name	= "ep93xx-pwm",
		.owner	= THIS_MODULE,
	},
	.remove		= __exit_p(ep93xx_pwm_remove),
};

module_platform_driver_probe(ep93xx_pwm_driver, ep93xx_pwm_probe);

MODULE_AUTHOR("Matthieu Crapet <mcrapet@gmail.com>, "
	      "H Hartley Sweeten <hsweeten@visionengravers.com>");
MODULE_DESCRIPTION("EP93xx PWM driver");
MODULE_LICENSE("GPL");
MODULE_ALIAS("platform:ep93xx-pwm");