RT-AC66 3.0.0.4.374.130 core
[tomato.git] / release / src-rt-6.x / linux / linux-2.6 / drivers / misc / hdpuftrs / hdpu_cpustate.c
blob276ba3c5143fd9646e03a1e75b3e20a90b5a87fd
1 /*
2 * Sky CPU State Driver
4 * Copyright (C) 2002 Brian Waite
6 * This driver allows use of the CPU state bits
7 * It exports the /dev/sky_cpustate and also
8 * /proc/sky_cpustate pseudo-file for status information.
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version
13 * 2 of the License, or (at your option) any later version.
17 #include <linux/module.h>
18 #include <linux/kernel.h>
19 #include <linux/spinlock.h>
20 #include <linux/miscdevice.h>
21 #include <linux/proc_fs.h>
22 #include <linux/platform_device.h>
23 #include <asm/uaccess.h>
24 #include <linux/hdpu_features.h>
26 #define SKY_CPUSTATE_VERSION "1.1"
28 static int hdpu_cpustate_probe(struct platform_device *pdev);
29 static int hdpu_cpustate_remove(struct platform_device *pdev);
31 struct cpustate_t cpustate;
33 static int cpustate_get_ref(int excl)
36 int retval = -EBUSY;
38 spin_lock(&cpustate.lock);
40 if (cpustate.excl)
41 goto out_busy;
43 if (excl) {
44 if (cpustate.open_count)
45 goto out_busy;
46 cpustate.excl = 1;
49 cpustate.open_count++;
50 retval = 0;
52 out_busy:
53 spin_unlock(&cpustate.lock);
54 return retval;
57 static int cpustate_free_ref(void)
60 spin_lock(&cpustate.lock);
62 cpustate.excl = 0;
63 cpustate.open_count--;
65 spin_unlock(&cpustate.lock);
66 return 0;
69 unsigned char cpustate_get_state(void)
72 return cpustate.cached_val;
75 void cpustate_set_state(unsigned char new_state)
77 unsigned int state = (new_state << 21);
79 #ifdef DEBUG_CPUSTATE
80 printk("CPUSTATE -> 0x%x\n", new_state);
81 #endif
82 spin_lock(&cpustate.lock);
83 cpustate.cached_val = new_state;
84 writel((0xff << 21), cpustate.clr_addr);
85 writel(state, cpustate.set_addr);
86 spin_unlock(&cpustate.lock);
90 * Now all the various file operations that we export.
93 static ssize_t cpustate_read(struct file *file, char *buf,
94 size_t count, loff_t * ppos)
96 unsigned char data;
98 if (count < 0)
99 return -EFAULT;
100 if (count == 0)
101 return 0;
103 data = cpustate_get_state();
104 if (copy_to_user(buf, &data, sizeof(unsigned char)))
105 return -EFAULT;
106 return sizeof(unsigned char);
109 static ssize_t cpustate_write(struct file *file, const char *buf,
110 size_t count, loff_t * ppos)
112 unsigned char data;
114 if (count < 0)
115 return -EFAULT;
117 if (count == 0)
118 return 0;
120 if (copy_from_user((unsigned char *)&data, buf, sizeof(unsigned char)))
121 return -EFAULT;
123 cpustate_set_state(data);
124 return sizeof(unsigned char);
127 static int cpustate_open(struct inode *inode, struct file *file)
129 return cpustate_get_ref((file->f_flags & O_EXCL));
132 static int cpustate_release(struct inode *inode, struct file *file)
134 return cpustate_free_ref();
138 * Info exported via "/proc/sky_cpustate".
140 static int cpustate_read_proc(char *page, char **start, off_t off,
141 int count, int *eof, void *data)
143 char *p = page;
144 int len = 0;
146 p += sprintf(p, "CPU State: %04x\n", cpustate_get_state());
147 len = p - page;
149 if (len <= off + count)
150 *eof = 1;
151 *start = page + off;
152 len -= off;
153 if (len > count)
154 len = count;
155 if (len < 0)
156 len = 0;
157 return len;
160 static struct platform_driver hdpu_cpustate_driver = {
161 .probe = hdpu_cpustate_probe,
162 .remove = hdpu_cpustate_remove,
163 .driver = {
164 .name = HDPU_CPUSTATE_NAME,
169 * The various file operations we support.
171 static const struct file_operations cpustate_fops = {
172 owner:THIS_MODULE,
173 open:cpustate_open,
174 release:cpustate_release,
175 read:cpustate_read,
176 write:cpustate_write,
177 fasync:NULL,
178 poll:NULL,
179 ioctl:NULL,
180 llseek:no_llseek,
184 static struct miscdevice cpustate_dev = {
185 MISC_DYNAMIC_MINOR,
186 "sky_cpustate",
187 &cpustate_fops
190 static int hdpu_cpustate_probe(struct platform_device *pdev)
192 struct resource *res;
193 struct proc_dir_entry *proc_de;
194 int ret;
196 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
197 cpustate.set_addr = (unsigned long *)res->start;
198 cpustate.clr_addr = (unsigned long *)res->end - 1;
200 ret = misc_register(&cpustate_dev);
201 if (ret) {
202 printk(KERN_WARNING "sky_cpustate: Unable to register misc "
203 "device.\n");
204 cpustate.set_addr = NULL;
205 cpustate.clr_addr = NULL;
206 return ret;
209 proc_de = create_proc_read_entry("sky_cpustate", 0, 0,
210 cpustate_read_proc, NULL);
211 if (proc_de == NULL)
212 printk(KERN_WARNING "sky_cpustate: Unable to create proc "
213 "dir entry\n");
215 printk(KERN_INFO "Sky CPU State Driver v" SKY_CPUSTATE_VERSION "\n");
216 return 0;
219 static int hdpu_cpustate_remove(struct platform_device *pdev)
222 cpustate.set_addr = NULL;
223 cpustate.clr_addr = NULL;
225 remove_proc_entry("sky_cpustate", NULL);
226 misc_deregister(&cpustate_dev);
227 return 0;
231 static int __init cpustate_init(void)
233 int rc;
234 rc = platform_driver_register(&hdpu_cpustate_driver);
235 return rc;
238 static void __exit cpustate_exit(void)
240 platform_driver_unregister(&hdpu_cpustate_driver);
243 module_init(cpustate_init);
244 module_exit(cpustate_exit);
246 MODULE_AUTHOR("Brian Waite");
247 MODULE_LICENSE("GPL");