aboutsummaryrefslogtreecommitdiff
path: root/drivers/power/gta02_hdq.c
blob: d51f4c9f55a0eea47c953abc32c08c3d04a74a76 (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
/*
 * HDQ driver for the FIC Neo1973 GTA02 GSM phone
 *
 * (C) 2006-2007 by Openmoko, Inc.
 * Author: Andy Green <andy@openmoko.com>
 * 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/kernel.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <linux/platform_device.h>
#include <mach/hardware.h>
#include <linux/gta02_hdq.h>
#include <asm/mach-types.h>
#include <mach/gta02.h>
#include <mach/fiq_ipc_gta02.h>



#define HDQ_READ 0
#define HDQ_WRITE 0x80

static int fiq_busy(void)
{
	int request = (volatile u8)fiq_ipc.hdq_request_ctr;
	int transact = (volatile u8)fiq_ipc.hdq_transaction_ctr;

	return (request != transact);
}

int gta02hdq_initialized(void)
{
	return fiq_ipc.hdq_probed;
}
EXPORT_SYMBOL_GPL(gta02hdq_initialized);

int gta02hdq_read(int address)
{
	int count_sleeps = 5;
	int ret = -ETIME;

	if (!fiq_ipc.hdq_probed)
		return -EINVAL;

	mutex_lock(&fiq_ipc.hdq_lock);

	fiq_ipc.hdq_error = 0;
	fiq_ipc.hdq_ads = address | HDQ_READ;
	fiq_ipc.hdq_request_ctr++;
	fiq_kick();
	/*
	 * FIQ takes care of it while we block our calling process
	 * But we're not spinning -- other processes run normally while
	 * we wait for the result
	 */
	while (count_sleeps--) {
		msleep(10); /* valid transaction always completes in < 10ms */

		if (fiq_busy())
			continue;

		if (fiq_ipc.hdq_error)
			goto done; /* didn't see a response in good time */

		ret = fiq_ipc.hdq_rx_data;
		goto done;
	}

done:
	mutex_unlock(&fiq_ipc.hdq_lock);
	return ret;
}
EXPORT_SYMBOL_GPL(gta02hdq_read);

int gta02hdq_write(int address, u8 data)
{
	int count_sleeps = 5;
	int ret = -ETIME;

	if (!fiq_ipc.hdq_probed)
		return -EINVAL;

	mutex_lock(&fiq_ipc.hdq_lock);

	fiq_ipc.hdq_error = 0;
	fiq_ipc.hdq_ads = address | HDQ_WRITE;
	fiq_ipc.hdq_tx_data = data;
	fiq_ipc.hdq_request_ctr++;
	fiq_kick();
	/*
	 * FIQ takes care of it while we block our calling process
	 * But we're not spinning -- other processes run normally while
	 * we wait for the result
	 */
	while (count_sleeps--) {
		msleep(10); /* valid transaction always completes in < 10ms */

		if (fiq_busy())
			continue; /* something bad with FIQ */

		if (fiq_ipc.hdq_error)
			goto done; /* didn't see a response in good time */

		ret = 0;
		goto done;
	}

done:
	mutex_unlock(&fiq_ipc.hdq_lock);
	return ret;
}
EXPORT_SYMBOL_GPL(gta02hdq_write);

/* sysfs */

static ssize_t hdq_sysfs_dump(struct device *dev, struct device_attribute *attr,
			 char *buf)
{
	int n;
	int v;
	u8 u8a[128]; /* whole address space for HDQ */
	char *end = buf;

	if (!fiq_ipc.hdq_probed)
		return -EINVAL;

	/* the dump does not take care about 16 bit regs, because at this
	 * bus level we don't know about the chip details
	 */
	for (n = 0; n < sizeof(u8a); n++) {
		v = gta02hdq_read(n);
		if (v < 0)
			goto bail;
		u8a[n] = v;
	}

	for (n = 0; n < sizeof(u8a); n += 16) {
		hex_dump_to_buffer(u8a + n, sizeof(u8a), 16, 1, end, 4096, 0);
		end += strlen(end);
		*end++ = '\n';
		*end = '\0';
	}
	return (end - buf);

bail:
	return sprintf(buf, "ERROR %d\n", v);
}

/* you write by <address> <data>, eg, "34 128" */

#define atoi(str) simple_strtoul(((str != NULL) ? str : ""), NULL, 0)

static ssize_t hdq_sysfs_write(struct device *dev,
			       struct device_attribute *attr,
			       const char *buf, size_t count)
{
	const char *end = buf + count;
	int address = atoi(buf);

	if (!fiq_ipc.hdq_probed)
		return -EINVAL;

	while ((buf != end) && (*buf != ' '))
		buf++;
	if (buf >= end)
		return 0;
	while ((buf < end) && (*buf == ' '))
		buf++;
	if (buf >= end)
		return 0;

	gta02hdq_write(address, atoi(buf));

	return count;
}

static DEVICE_ATTR(dump, 0400, hdq_sysfs_dump, NULL);
static DEVICE_ATTR(write, 0600, NULL, hdq_sysfs_write);

static struct attribute *gta02hdq_sysfs_entries[] = {
	&dev_attr_dump.attr,
	&dev_attr_write.attr,
	NULL
};

static struct attribute_group gta02hdq_attr_group = {
	.name	= "hdq",
	.attrs	= gta02hdq_sysfs_entries,
};


#ifdef CONFIG_PM
static int gta02hdq_suspend(struct platform_device *pdev, pm_message_t state)
{
	/* after 18s of this, the battery monitor will also go to sleep */
	s3c2410_gpio_setpin(fiq_ipc.hdq_gpio_pin, 0);
	s3c2410_gpio_cfgpin(fiq_ipc.hdq_gpio_pin, S3C2410_GPIO_OUTPUT);
	return 0;
}

static int gta02hdq_resume(struct platform_device *pdev)
{
	s3c2410_gpio_setpin(fiq_ipc.hdq_gpio_pin, 1);
	s3c2410_gpio_cfgpin(fiq_ipc.hdq_gpio_pin, S3C2410_GPIO_OUTPUT);
	return 0;
}
#endif

static int __init gta02hdq_probe(struct platform_device *pdev)
{
	struct resource *r = platform_get_resource(pdev, 0, 0);
	int ret;
	struct gta02_hdq_platform_data *pdata = pdev->dev.platform_data;

	if (!machine_is_neo1973_gta02())
		return -EIO;

	if (!r)
		return -EINVAL;

	if (!fiq_ready) {
		printk(KERN_ERR "hdq probe fails on fiq not ready\n");
		return -EINVAL;
	}

	platform_set_drvdata(pdev, NULL);

	mutex_init(&fiq_ipc.hdq_lock);

	/* set our HDQ comms pin from the platform data */
	fiq_ipc.hdq_gpio_pin = r->start;

	s3c2410_gpio_setpin(fiq_ipc.hdq_gpio_pin, 1);
	s3c2410_gpio_cfgpin(fiq_ipc.hdq_gpio_pin, S3C2410_GPIO_OUTPUT);

	ret = sysfs_create_group(&pdev->dev.kobj, &gta02hdq_attr_group);
	if (ret)
		return ret;

	fiq_ipc.hdq_probed = 1; /* we are ready to do stuff now */

	/*
	 * if wanted, users can defer registration of devices
	 * that depend on HDQ until after we register, and can use our
	 * device as parent so suspend-resume ordering is correct
	 */
	if (pdata->attach_child_devices)
		(pdata->attach_child_devices)(&pdev->dev);

	return 0;
}

static int gta02hdq_remove(struct platform_device *pdev)
{
	sysfs_remove_group(&pdev->dev.kobj, &gta02hdq_attr_group);
	return 0;
}

static struct platform_driver gta02hdq_driver = {
	.probe		= gta02hdq_probe,
	.remove		= gta02hdq_remove,
#ifdef CONFIG_PM
	.suspend	= gta02hdq_suspend,
	.resume		= gta02hdq_resume,
#endif
	.driver		= {
		.name		= "gta02-hdq",
	},
};

static int __init gta02hdq_init(void)
{
	return platform_driver_register(&gta02hdq_driver);
}

static void __exit gta02hdq_exit(void)
{
 	platform_driver_unregister(&gta02hdq_driver);
}

module_init(gta02hdq_init);
module_exit(gta02hdq_exit);

MODULE_AUTHOR("Andy Green <andy@openmoko.com>");
MODULE_DESCRIPTION("GTA02 HDQ driver");
MODULE_LICENSE("GPL");