2 * Filename: cfag12864b.c
4 * Description: cfag12864b LCD driver
8 * Author: Copyright (C) Miguel Ojeda Sandonis
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>
30 #include <linux/slab.h>
31 #include <linux/cdev.h>
32 #include <linux/delay.h>
33 #include <linux/device.h>
34 #include <linux/jiffies.h>
35 #include <linux/mutex.h>
36 #include <linux/uaccess.h>
37 #include <linux/vmalloc.h>
38 #include <linux/workqueue.h>
39 #include <linux/ks0108.h>
40 #include <linux/cfag12864b.h>
43 #define CFAG12864B_NAME "cfag12864b"
49 static unsigned int cfag12864b_rate
= CONFIG_CFAG12864B_RATE
;
50 module_param(cfag12864b_rate
, uint
, S_IRUGO
);
51 MODULE_PARM_DESC(cfag12864b_rate
,
52 "Refresh rate (hertz)");
54 unsigned int cfag12864b_getrate(void)
56 return cfag12864b_rate
;
63 * Every time E switch from low to high,
64 * cfag12864b/ks0108 reads the command/data.
66 * CS1 = First ks0108controller.
67 * If high, the first ks0108 controller receives commands/data.
69 * CS2 = Second ks0108 controller
70 * If high, the second ks0108 controller receives commands/data.
72 * DI = Data/Instruction
73 * If low, cfag12864b will expect commands.
74 * If high, cfag12864b will expect data.
78 #define bit(n) (((unsigned char)1)<<(n))
80 #define CFAG12864B_BIT_E (0)
81 #define CFAG12864B_BIT_CS1 (2)
82 #define CFAG12864B_BIT_CS2 (1)
83 #define CFAG12864B_BIT_DI (3)
85 static unsigned char cfag12864b_state
;
87 static void cfag12864b_set(void)
89 ks0108_writecontrol(cfag12864b_state
);
92 static void cfag12864b_setbit(unsigned char state
, unsigned char n
)
95 cfag12864b_state
|= bit(n
);
97 cfag12864b_state
&= ~bit(n
);
100 static void cfag12864b_e(unsigned char state
)
102 cfag12864b_setbit(state
, CFAG12864B_BIT_E
);
106 static void cfag12864b_cs1(unsigned char state
)
108 cfag12864b_setbit(state
, CFAG12864B_BIT_CS1
);
111 static void cfag12864b_cs2(unsigned char state
)
113 cfag12864b_setbit(state
, CFAG12864B_BIT_CS2
);
116 static void cfag12864b_di(unsigned char state
)
118 cfag12864b_setbit(state
, CFAG12864B_BIT_DI
);
121 static void cfag12864b_setcontrollers(unsigned char first
,
122 unsigned char second
)
135 static void cfag12864b_controller(unsigned char which
)
138 cfag12864b_setcontrollers(1, 0);
140 cfag12864b_setcontrollers(0, 1);
143 static void cfag12864b_displaystate(unsigned char state
)
147 ks0108_displaystate(state
);
151 static void cfag12864b_address(unsigned char address
)
155 ks0108_address(address
);
159 static void cfag12864b_page(unsigned char page
)
167 static void cfag12864b_startline(unsigned char startline
)
171 ks0108_startline(startline
);
175 static void cfag12864b_writebyte(unsigned char byte
)
179 ks0108_writedata(byte
);
183 static void cfag12864b_nop(void)
185 cfag12864b_startline(0);
189 * cfag12864b Internal Commands
192 static void cfag12864b_on(void)
194 cfag12864b_setcontrollers(1, 1);
195 cfag12864b_displaystate(1);
198 static void cfag12864b_off(void)
200 cfag12864b_setcontrollers(1, 1);
201 cfag12864b_displaystate(0);
204 static void cfag12864b_clear(void)
208 cfag12864b_setcontrollers(1, 1);
209 for (i
= 0; i
< CFAG12864B_PAGES
; i
++) {
211 cfag12864b_address(0);
212 for (j
= 0; j
< CFAG12864B_ADDRESSES
; j
++)
213 cfag12864b_writebyte(0);
221 unsigned char *cfag12864b_buffer
;
222 static unsigned char *cfag12864b_cache
;
223 static DEFINE_MUTEX(cfag12864b_mutex
);
224 static unsigned char cfag12864b_updating
;
225 static void cfag12864b_update(struct work_struct
*delayed_work
);
226 static struct workqueue_struct
*cfag12864b_workqueue
;
227 static DECLARE_DELAYED_WORK(cfag12864b_work
, cfag12864b_update
);
229 static void cfag12864b_queue(void)
231 queue_delayed_work(cfag12864b_workqueue
, &cfag12864b_work
,
232 HZ
/ cfag12864b_rate
);
235 unsigned char cfag12864b_enable(void)
239 mutex_lock(&cfag12864b_mutex
);
241 if (!cfag12864b_updating
) {
242 cfag12864b_updating
= 1;
248 mutex_unlock(&cfag12864b_mutex
);
253 void cfag12864b_disable(void)
255 mutex_lock(&cfag12864b_mutex
);
257 if (cfag12864b_updating
) {
258 cfag12864b_updating
= 0;
259 cancel_delayed_work(&cfag12864b_work
);
260 flush_workqueue(cfag12864b_workqueue
);
263 mutex_unlock(&cfag12864b_mutex
);
266 unsigned char cfag12864b_isenabled(void)
268 return cfag12864b_updating
;
271 static void cfag12864b_update(struct work_struct
*work
)
274 unsigned short i
, j
, k
, b
;
276 if (memcmp(cfag12864b_cache
, cfag12864b_buffer
, CFAG12864B_SIZE
)) {
277 for (i
= 0; i
< CFAG12864B_CONTROLLERS
; i
++) {
278 cfag12864b_controller(i
);
280 for (j
= 0; j
< CFAG12864B_PAGES
; j
++) {
283 cfag12864b_address(0);
285 for (k
= 0; k
< CFAG12864B_ADDRESSES
; k
++) {
286 for (c
= 0, b
= 0; b
< 8; b
++)
287 if (cfag12864b_buffer
288 [i
* CFAG12864B_ADDRESSES
/ 8
289 + k
/ 8 + (j
* 8 + b
) *
290 CFAG12864B_WIDTH
/ 8]
293 cfag12864b_writebyte(c
);
298 memcpy(cfag12864b_cache
, cfag12864b_buffer
, CFAG12864B_SIZE
);
301 if (cfag12864b_updating
)
306 * cfag12864b Exported Symbols
309 EXPORT_SYMBOL_GPL(cfag12864b_buffer
);
310 EXPORT_SYMBOL_GPL(cfag12864b_getrate
);
311 EXPORT_SYMBOL_GPL(cfag12864b_enable
);
312 EXPORT_SYMBOL_GPL(cfag12864b_disable
);
313 EXPORT_SYMBOL_GPL(cfag12864b_isenabled
);
316 * Is the module inited?
319 static unsigned char cfag12864b_inited
;
320 unsigned char cfag12864b_isinited(void)
322 return cfag12864b_inited
;
324 EXPORT_SYMBOL_GPL(cfag12864b_isinited
);
330 static int __init
cfag12864b_init(void)
334 /* ks0108_init() must be called first */
335 if (!ks0108_isinited()) {
336 printk(KERN_ERR CFAG12864B_NAME
": ERROR: "
337 "ks0108 is not initialized\n");
340 BUILD_BUG_ON(PAGE_SIZE
< CFAG12864B_SIZE
);
342 cfag12864b_buffer
= (unsigned char *) get_zeroed_page(GFP_KERNEL
);
343 if (cfag12864b_buffer
== NULL
) {
344 printk(KERN_ERR CFAG12864B_NAME
": ERROR: "
345 "can't get a free page\n");
350 cfag12864b_cache
= kmalloc(sizeof(unsigned char) *
351 CFAG12864B_SIZE
, GFP_KERNEL
);
352 if (cfag12864b_cache
== NULL
) {
353 printk(KERN_ERR CFAG12864B_NAME
": ERROR: "
354 "can't alloc cache buffer (%i bytes)\n",
360 cfag12864b_workqueue
= create_singlethread_workqueue(CFAG12864B_NAME
);
361 if (cfag12864b_workqueue
== NULL
)
367 cfag12864b_inited
= 1;
371 kfree(cfag12864b_cache
);
374 free_page((unsigned long) cfag12864b_buffer
);
380 static void __exit
cfag12864b_exit(void)
382 cfag12864b_disable();
384 destroy_workqueue(cfag12864b_workqueue
);
385 kfree(cfag12864b_cache
);
386 free_page((unsigned long) cfag12864b_buffer
);
389 module_init(cfag12864b_init
);
390 module_exit(cfag12864b_exit
);
392 MODULE_LICENSE("GPL v2");
393 MODULE_AUTHOR("Miguel Ojeda Sandonis <miguel.ojeda.sandonis@gmail.com>");
394 MODULE_DESCRIPTION("cfag12864b LCD driver");