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
)
38 spin_lock(&cpustate
.lock
);
44 if (cpustate
.open_count
)
49 cpustate
.open_count
++;
53 spin_unlock(&cpustate
.lock
);
57 static int cpustate_free_ref(void)
60 spin_lock(&cpustate
.lock
);
63 cpustate
.open_count
--;
65 spin_unlock(&cpustate
.lock
);
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);
80 printk("CPUSTATE -> 0x%x\n", new_state
);
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
)
103 data
= cpustate_get_state();
104 if (copy_to_user(buf
, &data
, sizeof(unsigned char)))
106 return sizeof(unsigned char);
109 static ssize_t
cpustate_write(struct file
*file
, const char *buf
,
110 size_t count
, loff_t
* ppos
)
120 if (copy_from_user((unsigned char *)&data
, buf
, sizeof(unsigned char)))
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
)
146 p
+= sprintf(p
, "CPU State: %04x\n", cpustate_get_state());
149 if (len
<= off
+ count
)
160 static struct platform_driver hdpu_cpustate_driver
= {
161 .probe
= hdpu_cpustate_probe
,
162 .remove
= hdpu_cpustate_remove
,
164 .name
= HDPU_CPUSTATE_NAME
,
169 * The various file operations we support.
171 static const struct file_operations cpustate_fops
= {
174 release
:cpustate_release
,
176 write
:cpustate_write
,
184 static struct miscdevice cpustate_dev
= {
190 static int hdpu_cpustate_probe(struct platform_device
*pdev
)
192 struct resource
*res
;
193 struct proc_dir_entry
*proc_de
;
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
);
202 printk(KERN_WARNING
"sky_cpustate: Unable to register misc "
204 cpustate
.set_addr
= NULL
;
205 cpustate
.clr_addr
= NULL
;
209 proc_de
= create_proc_read_entry("sky_cpustate", 0, 0,
210 cpustate_read_proc
, NULL
);
212 printk(KERN_WARNING
"sky_cpustate: Unable to create proc "
215 printk(KERN_INFO
"Sky CPU State Driver v" SKY_CPUSTATE_VERSION
"\n");
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
);
231 static int __init
cpustate_init(void)
234 rc
= platform_driver_register(&hdpu_cpustate_driver
);
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");