summaryrefslogtreecommitdiff
path: root/arch/arm/mach-ns9xxx/ns9215_devices.c
blob: 0a6f473b68f1894ca74d5584d374dcc22b8aa185 (plain)
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
/*
 * arch/arm/mach-ns9xxx/ns9215_devices.c
 *
 * Copyright (C) 2008 by Digi International Inc.
 * All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 as published by
 * the Free Software Foundation.
 */

#include <linux/clk.h>
#include <linux/interrupt.h>
#include <linux/io.h>
#include <linux/platform_device.h>
#include <linux/delay.h>

#include <mach/ns9xxx-pwm.h>
#include <linux/leds.h>
#include <linux/io.h>
#include <linux/pwm.h>
#include <linux/pwm-led.h>

#include <mach/hardware.h>
#include <mach/regs-sys-ns921x.h>

#include "clock.h"
#include "processor-ns921x.h"

#if defined(CONFIG_ARCH_NS9XXX)
static struct led_info ns9xxx_leds_pdata_info = {
        	.name			= "leds-pwm",
        	.default_trigger	= "ledtrig-dim",
        	.flags			= 0,
};

static struct pwm_channel_config ns9xxx_leds_pdata_config = {
	.duty_ns	= 0,
	.period_ns	= 0,
	
};

static struct pwm_led_platform_data ns9xxx_leds_pdata = {
	.bus_id		= "ns9xxx_pwmc.0",
	.chan		= 0,
	.led_info	= &ns9xxx_leds_pdata_info,
	.config		= &ns9xxx_leds_pdata_config,
};

static struct platform_device ns9xxx_device_ns9215_leds = {
	.name		= "leds-pwm",
	.id		= 0,
	.dev		= {
		.platform_data = &ns9xxx_leds_pdata,
	}
};

static struct ns9xxx_pwm_channel ns9215_pwm_channels[] = {
	[0] = {
		.timer		= 6,
		.gpio		= 5,
	},
	[1] = {
		.timer		= 7,
		.gpio		= 7,
	},
	[2] = {
		.timer		= 8,
		.gpio		= 8,
	},
	[3] = {
		.timer		= 9,
		.gpio		= 13,
	}
};


/* This structure will be initialized in the init function (see below) */
static struct ns9xxx_pwm_pdata ns9215_pwm_pdata;


static struct platform_device ns9xxx_device_ns9215_pwm = {
	.name		= "ns9xxx_pwmc",
	.id		= 0,
	.dev		= {
		.platform_data = &ns9215_pwm_pdata,
	}
};

void __init ns9xxx_add_device_ns9215_leds(void)
{
 	platform_device_register(&ns9xxx_device_ns9215_leds);

#if 1 /* Enabled PWM by the user configuration */
	ns9215_pwm_pdata.channels = ns9215_pwm_channels;
	ns9215_pwm_pdata.number_channels = ARRAY_SIZE(ns9215_pwm_channels);
	platform_device_register(&ns9xxx_device_ns9215_pwm);
#endif

}
#else
void __init ns9xxx_add_device_ns9215_leds(void) {}
#endif

#if defined(CONFIG_ADC_NS9215) || defined(CONFIG_ADC_NS9215_MODULE)
static struct ns921x_sysclk adc_clk = {
	.clk = {
		.name	= "adc-ns9215",
		.id	= -1,
		.owner	= THIS_MODULE,
	},
	.mask = SYS_CLOCK_ADC,
};

static struct resource adc_resources[] = {
	{
		.start	= 0x90039000,
		.end	= 0x90039027,
		.flags	= IORESOURCE_MEM,
	},
};

static struct platform_device ns9xxx_device_ns9215_adc = {
	.name		= "adc-ns9215",
	.id		= -1,
	.resource	= adc_resources,
	.num_resources	= ARRAY_SIZE(adc_resources),
};

void __init ns9xxx_add_device_ns9215_adc(void)
{
	if (clk_register(&adc_clk.clk))
		return;

	platform_device_register(&ns9xxx_device_ns9215_adc);
}
#else
void __init ns9xxx_add_device_ns9215_adc(void) {}
#endif

#if defined(CONFIG_RTC_DRV_NS9XXX) || defined(CONFIG_RTC_DRV_NS9XXX_MODULE)
static irqreturn_t ns9xxx_plat_rtc_irq(int irq, void *data)
{
	u32 sysrtcmc = __raw_readl(SYS_RTCMC);

	if (sysrtcmc & SYS_RTCMC_RIS) {
		REGSETIM(sysrtcmc, SYS_RTCMC, RIC, 1);
		__raw_writel(sysrtcmc, SYS_RTCMC);
		REGSETIM(sysrtcmc, SYS_RTCMC, RIC, 0);
		__raw_writel(sysrtcmc, SYS_RTCMC);

		return IRQ_HANDLED;
	}

	return IRQ_NONE;
}

static inline int wait_for_clkrdy(void)
{
	u32 sysrtcmc;
	unsigned int timeout = 0x20;

wait:
	sysrtcmc = __raw_readl(SYS_RTCMC);
	if (!(sysrtcmc & SYS_RTCMC_RIS)) {
		if (unlikely(!--timeout))
			return -ETIMEDOUT;
		udelay(1);
		goto wait;
	}
	pr_debug("%s: SYS_RTCMC = 0x%x\n", __func__, sysrtcmc);

	REGSETIM(sysrtcmc, SYS_RTCMC, RIC, 1);
	__raw_writel(sysrtcmc, SYS_RTCMC);
	REGSETIM(sysrtcmc, SYS_RTCMC, RIC, 0);
	__raw_writel(sysrtcmc, SYS_RTCMC);

	return sysrtcmc & SYS_RTCMC_SS ? 0 : -EIO;
}

static int ns9215_rtc_endisable(struct clk *clk, int enable)
{
	u32 sysrtcmc = __raw_readl(SYS_RTCMC);

	pr_debug("%s: enable=%d, SYS_RTCMC=%08x\n", __func__, enable, sysrtcmc);

	if (enable && (sysrtcmc & (SYS_RTCMC_SS | SYS_RTCMC_MODE)) ==
			SYS_RTCMC_SS) {
		/* the clock was disabled but is still active */

		unsigned int timeout = 0x20;

		pr_debug("%s: wait until disabled clock becomes inactive\n",
		__func__);
wait:
		sysrtcmc = __raw_readl(SYS_RTCMC);
		if (sysrtcmc & SYS_RTCMC_SS) {
			if (unlikely(!--timeout))
				return -ETIMEDOUT;
			udelay(1);
			goto wait;
		}
	}

	if (enable && (sysrtcmc & SYS_RTCMC_SS)) {
		pr_debug("%s: RTC clock already on\n", __func__);
		return 0;
	}

	if (enable) {
		int ret;

		/* disable rtc irq because the irq is acked in wait_for_clkrdy
		 * and this might stuck the irq controller.  (It doesn't like an
		 * irq to become active and then inactive before it is serviced.
		 *
		 * Not acking doesn't do the trick.  Then the following might
		 * happen in an endless loop:
		 *   - clk_enable + clk_disable with irqs off.  This makes
		 *     IRQ_NS9215_RTC pending.
		 *   - reenable irqs
		 *   - service IRQ_NS9215_RTC
		 *   - platform handler runs and acks irq.
		 *   - driver handler enables clk, sees that there is nothing to
		 *     do, and disables clk again.  All this with IRQ_NS9215_RTC
		 *     being masked.
		 */
		disable_irq(IRQ_NS9215_RTC);

		__raw_writel(SYS_RTCMC_MODE_NORMAL, SYS_RTCMC);

		ret = wait_for_clkrdy();

		enable_irq(IRQ_NS9215_RTC);

		return ret;
	} else {
		__raw_writel(SYS_RTCMC_MODE_STANDBY, SYS_RTCMC);
		sysrtcmc = __raw_readl(SYS_RTCMC);
		pr_debug("%s: disable done, SYS_RTCMC=%08x\n",
				__func__, sysrtcmc);

		return 0;
	}
}

static struct ns921x_sysclk rtc_pm_clk = {
	.clk = {
		.name		= "rtc-ns9xxx-pm",
		.id		= -1,
		.owner		= THIS_MODULE,
		.endisable	= ns921x_endisable_sysclock,
	},
	.mask	= SYS_CLOCK_RTC,
 };

static struct clk rtc_clk = {
	.name		= "rtc-ns9xxx",
	.id		= 0,
	.owner		= THIS_MODULE,
	.endisable	= ns9215_rtc_endisable,
};

static struct resource rtc_resources[] = {
	{
		.start	= 0x90060000,
		.end	= 0x900600ff,
		.flags	= IORESOURCE_MEM,
	}, {
		.start	= IRQ_NS9215_RTC,
		.flags	= IORESOURCE_IRQ,
	}
};

static struct platform_device ns9xxx_device_ns9215_rtc = {
	.name		= "rtc-ns9xxx",
	.id		= 0,
	.resource	= rtc_resources,
	.num_resources	= ARRAY_SIZE(rtc_resources),
};

void __init ns9xxx_add_device_ns9215_rtc(void)
{
	if (clk_register(&rtc_pm_clk.clk))
		return;

	rtc_clk.parent = &rtc_pm_clk.clk;
	if (clk_register(&rtc_clk))
		return;

	if (request_irq(IRQ_NS9215_RTC, ns9xxx_plat_rtc_irq, IRQF_SHARED,
				"plat-rtc-ns9xxx", ns9xxx_plat_rtc_irq))
		return;

	platform_device_register(&ns9xxx_device_ns9215_rtc);
}
#else
void __init ns9xxx_add_device_ns9215_rtc(void) {}
#endif