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/hdpu_features.h>
23 #include <linux/platform_device.h>
24 #include <asm/uaccess.h>
25 #include <linux/seq_file.h>
28 #define SKY_CPUSTATE_VERSION "1.1"
30 static int hdpu_cpustate_probe(struct platform_device *pdev);
31 static int hdpu_cpustate_remove(struct platform_device *pdev);
33 static unsigned char cpustate_get_state(void);
34 static int cpustate_proc_open(struct inode *inode, struct file *file);
35 static int cpustate_proc_read(struct seq_file *seq, void *offset);
37 static struct cpustate_t cpustate;
39 static const struct file_operations proc_cpustate = {
40 .open = cpustate_proc_open,
43 .release = single_release,
47 static int cpustate_proc_open(struct inode *inode, struct file *file)
49 return single_open(file, cpustate_proc_read, NULL);
52 static int cpustate_proc_read(struct seq_file *seq, void *offset)
54 seq_printf(seq, "CPU State: %04x\n", cpustate_get_state());
58 static int cpustate_get_ref(int excl)
63 spin_lock(&cpustate.lock);
69 if (cpustate.open_count)
74 cpustate.open_count++;
78 spin_unlock(&cpustate.lock);
82 static int cpustate_free_ref(void)
85 spin_lock(&cpustate.lock);
88 cpustate.open_count--;
90 spin_unlock(&cpustate.lock);
94 static unsigned char cpustate_get_state(void)
97 return cpustate.cached_val;
100 static void cpustate_set_state(unsigned char new_state)
102 unsigned int state = (new_state << 21);
104 #ifdef DEBUG_CPUSTATE
105 printk("CPUSTATE -> 0x%x\n", new_state);
107 spin_lock(&cpustate.lock);
108 cpustate.cached_val = new_state;
109 writel((0xff << 21), cpustate.clr_addr);
110 writel(state, cpustate.set_addr);
111 spin_unlock(&cpustate.lock);
115 * Now all the various file operations that we export.
118 static ssize_t cpustate_read(struct file *file, char *buf,
119 size_t count, loff_t * ppos)
128 data = cpustate_get_state();
129 if (copy_to_user(buf, &data, sizeof(unsigned char)))
131 return sizeof(unsigned char);
134 static ssize_t cpustate_write(struct file *file, const char *buf,
135 size_t count, loff_t * ppos)
145 if (copy_from_user((unsigned char *)&data, buf, sizeof(unsigned char)))
148 cpustate_set_state(data);
149 return sizeof(unsigned char);
152 static int cpustate_open(struct inode *inode, struct file *file)
154 return cpustate_get_ref((file->f_flags & O_EXCL));
157 static int cpustate_release(struct inode *inode, struct file *file)
159 return cpustate_free_ref();
162 static struct platform_driver hdpu_cpustate_driver = {
163 .probe = hdpu_cpustate_probe,
164 .remove = hdpu_cpustate_remove,
166 .name = HDPU_CPUSTATE_NAME,
167 .owner = THIS_MODULE,
172 * The various file operations we support.
174 static const struct file_operations cpustate_fops = {
175 .owner = THIS_MODULE,
176 .open = cpustate_open,
177 .release = cpustate_release,
178 .read = cpustate_read,
179 .write = cpustate_write,
183 static struct miscdevice cpustate_dev = {
184 .minor = MISC_DYNAMIC_MINOR,
185 .name = "sky_cpustate",
186 .fops = &cpustate_fops,
189 static int hdpu_cpustate_probe(struct platform_device *pdev)
191 struct resource *res;
192 struct proc_dir_entry *proc_de;
195 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
197 printk(KERN_ERR "sky_cpustate: "
198 "Invalid memory resource.\n");
201 cpustate.set_addr = (unsigned long *)res->start;
202 cpustate.clr_addr = (unsigned long *)res->end - 1;
204 ret = misc_register(&cpustate_dev);
206 printk(KERN_WARNING "sky_cpustate: "
207 "Unable to register misc device.\n");
208 cpustate.set_addr = NULL;
209 cpustate.clr_addr = NULL;
213 proc_de = proc_create("sky_cpustate", 0666, NULL, &proc_cpustate);
215 printk(KERN_WARNING "sky_cpustate: "
216 "Unable to create proc entry\n");
219 printk(KERN_INFO "Sky CPU State Driver v" SKY_CPUSTATE_VERSION "\n");
223 static int hdpu_cpustate_remove(struct platform_device *pdev)
225 cpustate.set_addr = NULL;
226 cpustate.clr_addr = NULL;
228 remove_proc_entry("sky_cpustate", NULL);
229 misc_deregister(&cpustate_dev);
234 static int __init cpustate_init(void)
236 return platform_driver_register(&hdpu_cpustate_driver);
239 static void __exit cpustate_exit(void)
241 platform_driver_unregister(&hdpu_cpustate_driver);
244 module_init(cpustate_init);
245 module_exit(cpustate_exit);
247 MODULE_AUTHOR("Brian Waite");
248 MODULE_LICENSE("GPL");
249 MODULE_ALIAS("platform:" HDPU_CPUSTATE_NAME);