summaryrefslogtreecommitdiff
path: root/arch/arm/mach-bcm2708/power.c
blob: 796837f2702c154b5b8344f956f2b3e4a33beab0 (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
/*
 *	 linux/arch/arm/mach-bcm2708/power.c
 *
 *	 Copyright (C) 2010 Broadcom
 *
 * 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.
 *
 * This device provides a shared mechanism for controlling the power to
 * VideoCore subsystems.
 */

#include <linux/module.h>
#include <linux/semaphore.h>
#include <linux/bug.h>
#include <linux/platform_data/mailbox-bcm2708.h>
#include <mach/power.h>
#include <mach/arm_power.h>

#define DRIVER_NAME "bcm2708_power"

#define BCM_POWER_MAXCLIENTS 4
#define BCM_POWER_NOCLIENT (1<<31)

/* Some drivers expect there devices to be permanently powered */

#ifdef CONFIG_USB
#define BCM_POWER_ALWAYS_ON (BCM_POWER_USB)
#endif

#if 1
#define DPRINTK printk
#else
#define DPRINTK if (0) printk
#endif

struct state_struct {
	uint32_t global_request;
	uint32_t client_request[BCM_POWER_MAXCLIENTS];
	struct semaphore client_mutex;
	struct semaphore mutex;
} g_state;

int bcm_power_open(BCM_POWER_HANDLE_T *handle)
{
	BCM_POWER_HANDLE_T i;
	int ret = -EBUSY;

	down(&g_state.client_mutex);

	for (i = 0; i < BCM_POWER_MAXCLIENTS; i++) {
		if (g_state.client_request[i] == BCM_POWER_NOCLIENT) {
			g_state.client_request[i] = BCM_POWER_NONE;
			*handle = i;
			ret = 0;
			break;
		}
	}

	up(&g_state.client_mutex);

	DPRINTK("bcm_power_open() -> %d\n", *handle);

	return ret;
}
EXPORT_SYMBOL_GPL(bcm_power_open);

int bcm_power_request(BCM_POWER_HANDLE_T handle, uint32_t request)
{
	int rc = 0;

	DPRINTK("bcm_power_request(%d, %x)\n", handle, request);

	if ((handle < BCM_POWER_MAXCLIENTS) &&
	    (g_state.client_request[handle] != BCM_POWER_NOCLIENT)) {
		if (down_interruptible(&g_state.mutex) != 0) {
			DPRINTK("bcm_power_request -> interrupted\n");
			return -EINTR;
		}

		if (request != g_state.client_request[handle]) {
			uint32_t others_request = 0;
			uint32_t global_request;
			BCM_POWER_HANDLE_T i;

			for (i = 0; i < BCM_POWER_MAXCLIENTS; i++) {
				if (i != handle)
					others_request |=
					    g_state.client_request[i];
			}
			others_request &= ~BCM_POWER_NOCLIENT;

			global_request = request | others_request;
			if (global_request != g_state.global_request) {
				uint32_t actual;

				/* Send a request to VideoCore */
				bcm_mailbox_write(MBOX_CHAN_POWER,
						  global_request << 4);

				/* Wait for a response during power-up */
				if (global_request & ~g_state.global_request) {
					rc = bcm_mailbox_read(MBOX_CHAN_POWER,
							      &actual);
					DPRINTK
					    ("bcm_mailbox_read -> %08x, %d\n",
					     actual, rc);
					actual >>= 4;
				} else {
					rc = 0;
					actual = global_request;
				}

				if (rc == 0) {
					if (actual != global_request) {
						printk(KERN_ERR
						     "%s: prev global %x, new global %x, actual %x, request %x, others_request %x\n",
						     __func__,
						     g_state.global_request,
						     global_request, actual, request, others_request);
						/* A failure */
						BUG_ON((others_request & actual)
						       != others_request);
						request &= actual;
						rc = -EIO;
					}

					g_state.global_request = actual;
					g_state.client_request[handle] =
					    request;
				}
			}
		}
		up(&g_state.mutex);
	} else {
		rc = -EINVAL;
	}
	DPRINTK("bcm_power_request -> %d\n", rc);
	return rc;
}
EXPORT_SYMBOL_GPL(bcm_power_request);

int bcm_power_close(BCM_POWER_HANDLE_T handle)
{
	int rc;

	DPRINTK("bcm_power_close(%d)\n", handle);

	rc = bcm_power_request(handle, BCM_POWER_NONE);
	if (rc == 0)
		g_state.client_request[handle] = BCM_POWER_NOCLIENT;

	return rc;
}
EXPORT_SYMBOL_GPL(bcm_power_close);

static int __init bcm_power_init(void)
{
#if defined(BCM_POWER_ALWAYS_ON)
	BCM_POWER_HANDLE_T always_on_handle;
#endif
	int rc = 0;
	int i;

	printk(KERN_INFO "bcm_power: Broadcom power driver\n");
	bcm_mailbox_write(MBOX_CHAN_POWER, 0);

	for (i = 0; i < BCM_POWER_MAXCLIENTS; i++)
		g_state.client_request[i] = BCM_POWER_NOCLIENT;

	sema_init(&g_state.client_mutex, 1);
	sema_init(&g_state.mutex, 1);

	g_state.global_request = 0;

#if defined(BCM_POWER_ALWAYS_ON)
	if (BCM_POWER_ALWAYS_ON) {
		bcm_power_open(&always_on_handle);
		bcm_power_request(always_on_handle, BCM_POWER_ALWAYS_ON);
	}
#endif

	return rc;
}

static void __exit bcm_power_exit(void)
{
	bcm_mailbox_write(MBOX_CHAN_POWER, 0);
}

/*
 * Load after the mailbox driver is initialized (arch_initcall),
 * but before depending drivers (module_init).
 */
subsys_initcall(bcm_power_init);
module_exit(bcm_power_exit);

MODULE_AUTHOR("Phil Elwell");
MODULE_DESCRIPTION("Interface to BCM2708 power management");
MODULE_LICENSE("GPL");