Merge branch 'for-linus' of git://neil.brown.name/md
[linux-2.6/mini2440.git] / drivers / auxdisplay / cfag12864b.c
blobeacb175f6bd37a4e5c4314c65a039a1fb04b91d8
1 /*
2 * Filename: cfag12864b.c
3 * Version: 0.1.0
4 * Description: cfag12864b LCD driver
5 * License: GPLv2
6 * Depends: ks0108
8 * Author: Copyright (C) Miguel Ojeda Sandonis
9 * Date: 2006-10-31
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #include <linux/init.h>
27 #include <linux/module.h>
28 #include <linux/kernel.h>
29 #include <linux/fs.h>
30 #include <linux/cdev.h>
31 #include <linux/delay.h>
32 #include <linux/device.h>
33 #include <linux/jiffies.h>
34 #include <linux/mutex.h>
35 #include <linux/uaccess.h>
36 #include <linux/vmalloc.h>
37 #include <linux/workqueue.h>
38 #include <linux/ks0108.h>
39 #include <linux/cfag12864b.h>
42 #define CFAG12864B_NAME "cfag12864b"
45 * Module Parameters
48 static unsigned int cfag12864b_rate = CONFIG_CFAG12864B_RATE;
49 module_param(cfag12864b_rate, uint, S_IRUGO);
50 MODULE_PARM_DESC(cfag12864b_rate,
51 "Refresh rate (hertzs)");
53 unsigned int cfag12864b_getrate(void)
55 return cfag12864b_rate;
59 * cfag12864b Commands
61 * E = Enable signal
62 * Everytime E switch from low to high,
63 * cfag12864b/ks0108 reads the command/data.
65 * CS1 = First ks0108controller.
66 * If high, the first ks0108 controller receives commands/data.
68 * CS2 = Second ks0108 controller
69 * If high, the second ks0108 controller receives commands/data.
71 * DI = Data/Instruction
72 * If low, cfag12864b will expect commands.
73 * If high, cfag12864b will expect data.
77 #define bit(n) (((unsigned char)1)<<(n))
79 #define CFAG12864B_BIT_E (0)
80 #define CFAG12864B_BIT_CS1 (2)
81 #define CFAG12864B_BIT_CS2 (1)
82 #define CFAG12864B_BIT_DI (3)
84 static unsigned char cfag12864b_state;
86 static void cfag12864b_set(void)
88 ks0108_writecontrol(cfag12864b_state);
91 static void cfag12864b_setbit(unsigned char state, unsigned char n)
93 if (state)
94 cfag12864b_state |= bit(n);
95 else
96 cfag12864b_state &= ~bit(n);
99 static void cfag12864b_e(unsigned char state)
101 cfag12864b_setbit(state, CFAG12864B_BIT_E);
102 cfag12864b_set();
105 static void cfag12864b_cs1(unsigned char state)
107 cfag12864b_setbit(state, CFAG12864B_BIT_CS1);
110 static void cfag12864b_cs2(unsigned char state)
112 cfag12864b_setbit(state, CFAG12864B_BIT_CS2);
115 static void cfag12864b_di(unsigned char state)
117 cfag12864b_setbit(state, CFAG12864B_BIT_DI);
120 static void cfag12864b_setcontrollers(unsigned char first,
121 unsigned char second)
123 if (first)
124 cfag12864b_cs1(0);
125 else
126 cfag12864b_cs1(1);
128 if (second)
129 cfag12864b_cs2(0);
130 else
131 cfag12864b_cs2(1);
134 static void cfag12864b_controller(unsigned char which)
136 if (which == 0)
137 cfag12864b_setcontrollers(1, 0);
138 else if (which == 1)
139 cfag12864b_setcontrollers(0, 1);
142 static void cfag12864b_displaystate(unsigned char state)
144 cfag12864b_di(0);
145 cfag12864b_e(1);
146 ks0108_displaystate(state);
147 cfag12864b_e(0);
150 static void cfag12864b_address(unsigned char address)
152 cfag12864b_di(0);
153 cfag12864b_e(1);
154 ks0108_address(address);
155 cfag12864b_e(0);
158 static void cfag12864b_page(unsigned char page)
160 cfag12864b_di(0);
161 cfag12864b_e(1);
162 ks0108_page(page);
163 cfag12864b_e(0);
166 static void cfag12864b_startline(unsigned char startline)
168 cfag12864b_di(0);
169 cfag12864b_e(1);
170 ks0108_startline(startline);
171 cfag12864b_e(0);
174 static void cfag12864b_writebyte(unsigned char byte)
176 cfag12864b_di(1);
177 cfag12864b_e(1);
178 ks0108_writedata(byte);
179 cfag12864b_e(0);
182 static void cfag12864b_nop(void)
184 cfag12864b_startline(0);
188 * cfag12864b Internal Commands
191 static void cfag12864b_on(void)
193 cfag12864b_setcontrollers(1, 1);
194 cfag12864b_displaystate(1);
197 static void cfag12864b_off(void)
199 cfag12864b_setcontrollers(1, 1);
200 cfag12864b_displaystate(0);
203 static void cfag12864b_clear(void)
205 unsigned char i, j;
207 cfag12864b_setcontrollers(1, 1);
208 for (i = 0; i < CFAG12864B_PAGES; i++) {
209 cfag12864b_page(i);
210 cfag12864b_address(0);
211 for (j = 0; j < CFAG12864B_ADDRESSES; j++)
212 cfag12864b_writebyte(0);
217 * Update work
220 unsigned char *cfag12864b_buffer;
221 static unsigned char *cfag12864b_cache;
222 static DEFINE_MUTEX(cfag12864b_mutex);
223 static unsigned char cfag12864b_updating;
224 static void cfag12864b_update(struct work_struct *delayed_work);
225 static struct workqueue_struct *cfag12864b_workqueue;
226 static DECLARE_DELAYED_WORK(cfag12864b_work, cfag12864b_update);
228 static void cfag12864b_queue(void)
230 queue_delayed_work(cfag12864b_workqueue, &cfag12864b_work,
231 HZ / cfag12864b_rate);
234 unsigned char cfag12864b_enable(void)
236 unsigned char ret;
238 mutex_lock(&cfag12864b_mutex);
240 if (!cfag12864b_updating) {
241 cfag12864b_updating = 1;
242 cfag12864b_queue();
243 ret = 0;
244 } else
245 ret = 1;
247 mutex_unlock(&cfag12864b_mutex);
249 return ret;
252 void cfag12864b_disable(void)
254 mutex_lock(&cfag12864b_mutex);
256 if (cfag12864b_updating) {
257 cfag12864b_updating = 0;
258 cancel_delayed_work(&cfag12864b_work);
259 flush_workqueue(cfag12864b_workqueue);
262 mutex_unlock(&cfag12864b_mutex);
265 unsigned char cfag12864b_isenabled(void)
267 return cfag12864b_updating;
270 static void cfag12864b_update(struct work_struct *work)
272 unsigned char c;
273 unsigned short i, j, k, b;
275 if (memcmp(cfag12864b_cache, cfag12864b_buffer, CFAG12864B_SIZE)) {
276 for (i = 0; i < CFAG12864B_CONTROLLERS; i++) {
277 cfag12864b_controller(i);
278 cfag12864b_nop();
279 for (j = 0; j < CFAG12864B_PAGES; j++) {
280 cfag12864b_page(j);
281 cfag12864b_nop();
282 cfag12864b_address(0);
283 cfag12864b_nop();
284 for (k = 0; k < CFAG12864B_ADDRESSES; k++) {
285 for (c = 0, b = 0; b < 8; b++)
286 if (cfag12864b_buffer
287 [i * CFAG12864B_ADDRESSES / 8
288 + k / 8 + (j * 8 + b) *
289 CFAG12864B_WIDTH / 8]
290 & bit(k % 8))
291 c |= bit(b);
292 cfag12864b_writebyte(c);
297 memcpy(cfag12864b_cache, cfag12864b_buffer, CFAG12864B_SIZE);
300 if (cfag12864b_updating)
301 cfag12864b_queue();
305 * cfag12864b Exported Symbols
308 EXPORT_SYMBOL_GPL(cfag12864b_buffer);
309 EXPORT_SYMBOL_GPL(cfag12864b_getrate);
310 EXPORT_SYMBOL_GPL(cfag12864b_enable);
311 EXPORT_SYMBOL_GPL(cfag12864b_disable);
312 EXPORT_SYMBOL_GPL(cfag12864b_isenabled);
315 * Is the module inited?
318 static unsigned char cfag12864b_inited;
319 unsigned char cfag12864b_isinited(void)
321 return cfag12864b_inited;
323 EXPORT_SYMBOL_GPL(cfag12864b_isinited);
326 * Module Init & Exit
329 static int __init cfag12864b_init(void)
331 int ret = -EINVAL;
333 /* ks0108_init() must be called first */
334 if (!ks0108_isinited()) {
335 printk(KERN_ERR CFAG12864B_NAME ": ERROR: "
336 "ks0108 is not initialized\n");
337 goto none;
339 BUILD_BUG_ON(PAGE_SIZE < CFAG12864B_SIZE);
341 cfag12864b_buffer = (unsigned char *) get_zeroed_page(GFP_KERNEL);
342 if (cfag12864b_buffer == NULL) {
343 printk(KERN_ERR CFAG12864B_NAME ": ERROR: "
344 "can't get a free page\n");
345 ret = -ENOMEM;
346 goto none;
349 cfag12864b_cache = kmalloc(sizeof(unsigned char) *
350 CFAG12864B_SIZE, GFP_KERNEL);
351 if (cfag12864b_cache == NULL) {
352 printk(KERN_ERR CFAG12864B_NAME ": ERROR: "
353 "can't alloc cache buffer (%i bytes)\n",
354 CFAG12864B_SIZE);
355 ret = -ENOMEM;
356 goto bufferalloced;
359 cfag12864b_workqueue = create_singlethread_workqueue(CFAG12864B_NAME);
360 if (cfag12864b_workqueue == NULL)
361 goto cachealloced;
363 cfag12864b_clear();
364 cfag12864b_on();
366 cfag12864b_inited = 1;
367 return 0;
369 cachealloced:
370 kfree(cfag12864b_cache);
372 bufferalloced:
373 free_page((unsigned long) cfag12864b_buffer);
375 none:
376 return ret;
379 static void __exit cfag12864b_exit(void)
381 cfag12864b_disable();
382 cfag12864b_off();
383 destroy_workqueue(cfag12864b_workqueue);
384 kfree(cfag12864b_cache);
385 free_page((unsigned long) cfag12864b_buffer);
388 module_init(cfag12864b_init);
389 module_exit(cfag12864b_exit);
391 MODULE_LICENSE("GPL v2");
392 MODULE_AUTHOR("Miguel Ojeda Sandonis <miguel.ojeda.sandonis@gmail.com>");
393 MODULE_DESCRIPTION("cfag12864b LCD driver");