1 /* $Id: display7seg.c,v 1.5 2001/10/08 22:19:51 davem Exp $
2  *
3  * display7seg - Driver implementation for the 7-segment display
4  * present on Sun Microsystems CP1400 and CP1500
5  *
6  * Copyright (c) 2000 Eric Brower (ebrower@usa.net)
7  *
8  */
9 
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/version.h>
13 #include <linux/fs.h>
14 #include <linux/errno.h>
15 #include <linux/major.h>
16 #include <linux/init.h>
17 #include <linux/miscdevice.h>
18 #include <linux/ioport.h>		/* request_region, check_region */
19 #include <asm/ebus.h>			/* EBus device					*/
20 #include <asm/oplib.h>			/* OpenProm Library 			*/
21 #include <asm/uaccess.h>		/* put_/get_user			*/
22 
23 #include <asm/display7seg.h>
24 
25 #define D7S_MINOR	193
26 #define D7S_OBPNAME	"display7seg"
27 #define D7S_DEVNAME "d7s"
28 
29 static int sol_compat = 0;		/* Solaris compatibility mode	*/
30 
31 #ifdef MODULE
32 EXPORT_NO_SYMBOLS;
33 
34 /* Solaris compatibility flag -
35  * The Solaris implementation omits support for several
36  * documented driver features (ref Sun doc 806-0180-03).
37  * By default, this module supports the documented driver
38  * abilities, rather than the Solaris implementation:
39  *
40  * 	1) Device ALWAYS reverts to OBP-specified FLIPPED mode
41  * 	   upon closure of device or module unload.
42  * 	2) Device ioctls D7SIOCRD/D7SIOCWR honor toggling of
43  * 	   FLIP bit
44  *
45  * If you wish the device to operate as under Solaris,
46  * omitting above features, set this parameter to non-zero.
47  */
48 MODULE_PARM
49 	(sol_compat, "1i");
50 MODULE_PARM_DESC
51 	(sol_compat,
52 	 "Disables documented functionality omitted from Solaris driver");
53 
54 MODULE_AUTHOR
55 	("Eric Brower <ebrower@usa.net>");
56 MODULE_DESCRIPTION
57 	("7-Segment Display driver for Sun Microsystems CP1400/1500");
58 MODULE_LICENSE("GPL");
59 MODULE_SUPPORTED_DEVICE
60 	("d7s");
61 #endif /* ifdef MODULE */
62 
63 /*
64  * Register block address- see header for details
65  * -----------------------------------------
66  * | DP | ALARM | FLIP | 4 | 3 | 2 | 1 | 0 |
67  * -----------------------------------------
68  *
69  * DP 		- Toggles decimal point on/off
70  * ALARM	- Toggles "Alarm" LED green/red
71  * FLIP		- Inverts display for upside-down mounted board
72  * bits 0-4	- 7-segment display contents
73  */
74 volatile u8* d7s_regs = 0;
75 
d7s_free(void)76 static inline void d7s_free(void)
77 {
78 	iounmap(d7s_regs);
79 }
80 
d7s_obpflipped(void)81 static inline int d7s_obpflipped(void)
82 {
83 	int opt_node;
84 
85 	opt_node = prom_getchild(prom_root_node);
86 	opt_node = prom_searchsiblings(opt_node, "options");
87 	return ((-1 != prom_getintdefault(opt_node, "d7s-flipped?", -1)) ? 0 : 1);
88 }
89 
d7s_open(struct inode * inode,struct file * f)90 static int d7s_open(struct inode *inode, struct file *f)
91 {
92 	if (D7S_MINOR != MINOR(inode->i_rdev))
93 		return -ENODEV;
94 
95 	MOD_INC_USE_COUNT;
96 	return 0;
97 }
98 
d7s_release(struct inode * inode,struct file * f)99 static int d7s_release(struct inode *inode, struct file *f)
100 {
101 	if (D7S_MINOR != MINOR(inode->i_rdev))
102 		return -ENODEV;
103 
104 	MOD_DEC_USE_COUNT;
105 
106 	/* Reset flipped state to OBP default only if
107 	 * no other users have the device open and we
108 	 * are not operating in solaris-compat mode
109 	 */
110 	if (0 == MOD_IN_USE && 0 == sol_compat) {
111 		int regval = 0;
112 
113 		regval = readb(d7s_regs);
114 		(0 == d7s_obpflipped())	?
115 			writeb(regval |= D7S_FLIP,  d7s_regs):
116 			writeb(regval &= ~D7S_FLIP, d7s_regs);
117 	}
118 
119 	return 0;
120 }
121 
d7s_ioctl(struct inode * inode,struct file * f,unsigned int cmd,unsigned long arg)122 static int d7s_ioctl(struct inode *inode, struct file *f,
123 		     unsigned int cmd, unsigned long arg)
124 {
125 	__u8 regs = readb(d7s_regs);
126 	__u8 ireg = 0;
127 
128 	if (D7S_MINOR != MINOR(inode->i_rdev))
129 		return -ENODEV;
130 
131 	switch (cmd) {
132 	case D7SIOCWR:
133 		/* assign device register values
134 		 * we mask-out D7S_FLIP if in sol_compat mode
135 		 */
136 		if (get_user(ireg, (int *) arg))
137 			return -EFAULT;
138 		if (0 != sol_compat) {
139 			(regs & D7S_FLIP) ?
140 				(ireg |= D7S_FLIP) : (ireg &= ~D7S_FLIP);
141 		}
142 		writeb(ireg, d7s_regs);
143 		break;
144 
145 	case D7SIOCRD:
146 		/* retrieve device register values
147 		 * NOTE: Solaris implementation returns D7S_FLIP bit
148 		 * as toggled by user, even though it does not honor it.
149 		 * This driver will not misinform you about the state
150 		 * of your hardware while in sol_compat mode
151 		 */
152 		if (put_user(regs, (int *) arg))
153 			return -EFAULT;
154 		break;
155 
156 	case D7SIOCTM:
157 		/* toggle device mode-- flip display orientation */
158 		(regs & D7S_FLIP) ?
159 			(regs &= ~D7S_FLIP) : (regs |= D7S_FLIP);
160 		writeb(regs, d7s_regs);
161 		break;
162 	};
163 
164 	return 0;
165 }
166 
167 static struct file_operations d7s_fops = {
168 	owner:		THIS_MODULE,
169 	ioctl:		d7s_ioctl,
170 	open:		d7s_open,
171 	release:	d7s_release,
172 };
173 
174 static struct miscdevice d7s_miscdev = { D7S_MINOR, D7S_DEVNAME, &d7s_fops };
175 
d7s_init(void)176 static int __init d7s_init(void)
177 {
178 	struct linux_ebus *ebus = NULL;
179 	struct linux_ebus_device *edev = NULL;
180 	int iTmp = 0, regs = 0;
181 
182 	for_each_ebus(ebus) {
183 		for_each_ebusdev(edev, ebus) {
184 			if (!strcmp(edev->prom_name, D7S_OBPNAME))
185 				goto ebus_done;
186 		}
187 	}
188 
189 ebus_done:
190 	if(!edev) {
191 		printk("%s: unable to locate device\n", D7S_DEVNAME);
192 		return -ENODEV;
193 	}
194 
195 	d7s_regs = ioremap(edev->resource[0].start, sizeof(__u8));
196 
197 	iTmp = misc_register(&d7s_miscdev);
198 	if (0 != iTmp) {
199 		printk("%s: unable to acquire miscdevice minor %i\n",
200 		       D7S_DEVNAME, D7S_MINOR);
201 		iounmap(d7s_regs);
202 		return iTmp;
203 	}
204 
205 	/* OBP option "d7s-flipped?" is honored as default
206 	 * for the device, and reset default when detached
207 	 */
208 	regs = readb(d7s_regs);
209 	iTmp = d7s_obpflipped();
210 	(0 == iTmp) ?
211 		writeb(regs |= D7S_FLIP,  d7s_regs):
212 		writeb(regs &= ~D7S_FLIP, d7s_regs);
213 
214 	printk("%s: 7-Segment Display%s at 0x%lx %s\n",
215 	       D7S_DEVNAME,
216 	       (0 == iTmp) ? (" (FLIPPED)") : (""),
217 	       edev->resource[0].start,
218 	       (0 != sol_compat) ? ("in sol_compat mode") : (""));
219 
220 	return 0;
221 }
222 
d7s_cleanup(void)223 static void __exit d7s_cleanup(void)
224 {
225 	int regs = readb(d7s_regs);
226 
227 	/* Honor OBP d7s-flipped? unless operating in solaris-compat mode */
228 	if (0 == sol_compat) {
229 		(0 == d7s_obpflipped())	?
230 			writeb(regs |= D7S_FLIP,  d7s_regs):
231 			writeb(regs &= ~D7S_FLIP, d7s_regs);
232 	}
233 
234 	misc_deregister(&d7s_miscdev);
235 	d7s_free();
236 }
237 
238 module_init(d7s_init);
239 module_exit(d7s_cleanup);
240