1 // SPDX-License-Identifier: GPL-2.0
3 * Filename: cfag12864b.c
5 * Description: cfag12864b LCD driver
8 * Author: Copyright (C) Miguel Ojeda <ojeda@kernel.org>
12 #include <linux/init.h>
13 #include <linux/module.h>
14 #include <linux/kernel.h>
16 #include <linux/slab.h>
17 #include <linux/cdev.h>
18 #include <linux/delay.h>
19 #include <linux/device.h>
20 #include <linux/jiffies.h>
21 #include <linux/mutex.h>
22 #include <linux/uaccess.h>
23 #include <linux/vmalloc.h>
24 #include <linux/workqueue.h>
25 #include <linux/ks0108.h>
26 #include <linux/cfag12864b.h>
29 #define CFAG12864B_NAME "cfag12864b"
35 static unsigned int cfag12864b_rate
= CONFIG_CFAG12864B_RATE
;
36 module_param(cfag12864b_rate
, uint
, 0444);
37 MODULE_PARM_DESC(cfag12864b_rate
,
38 "Refresh rate (hertz)");
44 * Every time E switch from low to high,
45 * cfag12864b/ks0108 reads the command/data.
47 * CS1 = First ks0108controller.
48 * If high, the first ks0108 controller receives commands/data.
50 * CS2 = Second ks0108 controller
51 * If high, the second ks0108 controller receives commands/data.
53 * DI = Data/Instruction
54 * If low, cfag12864b will expect commands.
55 * If high, cfag12864b will expect data.
59 #define bit(n) (((unsigned char)1)<<(n))
61 #define CFAG12864B_BIT_E (0)
62 #define CFAG12864B_BIT_CS1 (2)
63 #define CFAG12864B_BIT_CS2 (1)
64 #define CFAG12864B_BIT_DI (3)
66 static unsigned char cfag12864b_state
;
68 static void cfag12864b_set(void)
70 ks0108_writecontrol(cfag12864b_state
);
73 static void cfag12864b_setbit(unsigned char state
, unsigned char n
)
76 cfag12864b_state
|= bit(n
);
78 cfag12864b_state
&= ~bit(n
);
81 static void cfag12864b_e(unsigned char state
)
83 cfag12864b_setbit(state
, CFAG12864B_BIT_E
);
87 static void cfag12864b_cs1(unsigned char state
)
89 cfag12864b_setbit(state
, CFAG12864B_BIT_CS1
);
92 static void cfag12864b_cs2(unsigned char state
)
94 cfag12864b_setbit(state
, CFAG12864B_BIT_CS2
);
97 static void cfag12864b_di(unsigned char state
)
99 cfag12864b_setbit(state
, CFAG12864B_BIT_DI
);
102 static void cfag12864b_setcontrollers(unsigned char first
,
103 unsigned char second
)
116 static void cfag12864b_controller(unsigned char which
)
119 cfag12864b_setcontrollers(1, 0);
121 cfag12864b_setcontrollers(0, 1);
124 static void cfag12864b_displaystate(unsigned char state
)
128 ks0108_displaystate(state
);
132 static void cfag12864b_address(unsigned char address
)
136 ks0108_address(address
);
140 static void cfag12864b_page(unsigned char page
)
148 static void cfag12864b_startline(unsigned char startline
)
152 ks0108_startline(startline
);
156 static void cfag12864b_writebyte(unsigned char byte
)
160 ks0108_writedata(byte
);
164 static void cfag12864b_nop(void)
166 cfag12864b_startline(0);
170 * cfag12864b Internal Commands
173 static void cfag12864b_on(void)
175 cfag12864b_setcontrollers(1, 1);
176 cfag12864b_displaystate(1);
179 static void cfag12864b_off(void)
181 cfag12864b_setcontrollers(1, 1);
182 cfag12864b_displaystate(0);
185 static void cfag12864b_clear(void)
189 cfag12864b_setcontrollers(1, 1);
190 for (i
= 0; i
< CFAG12864B_PAGES
; i
++) {
192 cfag12864b_address(0);
193 for (j
= 0; j
< CFAG12864B_ADDRESSES
; j
++)
194 cfag12864b_writebyte(0);
202 unsigned char *cfag12864b_buffer
;
203 static unsigned char *cfag12864b_cache
;
204 static DEFINE_MUTEX(cfag12864b_mutex
);
205 static unsigned char cfag12864b_updating
;
206 static void cfag12864b_update(struct work_struct
*delayed_work
);
207 static struct workqueue_struct
*cfag12864b_workqueue
;
208 static DECLARE_DELAYED_WORK(cfag12864b_work
, cfag12864b_update
);
210 static void cfag12864b_queue(void)
212 queue_delayed_work(cfag12864b_workqueue
, &cfag12864b_work
,
213 HZ
/ cfag12864b_rate
);
216 unsigned char cfag12864b_enable(void)
220 mutex_lock(&cfag12864b_mutex
);
222 if (!cfag12864b_updating
) {
223 cfag12864b_updating
= 1;
229 mutex_unlock(&cfag12864b_mutex
);
234 void cfag12864b_disable(void)
236 mutex_lock(&cfag12864b_mutex
);
238 if (cfag12864b_updating
) {
239 cfag12864b_updating
= 0;
240 cancel_delayed_work(&cfag12864b_work
);
241 flush_workqueue(cfag12864b_workqueue
);
244 mutex_unlock(&cfag12864b_mutex
);
247 static void cfag12864b_update(struct work_struct
*work
)
250 unsigned short i
, j
, k
, b
;
252 if (memcmp(cfag12864b_cache
, cfag12864b_buffer
, CFAG12864B_SIZE
)) {
253 for (i
= 0; i
< CFAG12864B_CONTROLLERS
; i
++) {
254 cfag12864b_controller(i
);
256 for (j
= 0; j
< CFAG12864B_PAGES
; j
++) {
259 cfag12864b_address(0);
261 for (k
= 0; k
< CFAG12864B_ADDRESSES
; k
++) {
262 for (c
= 0, b
= 0; b
< 8; b
++)
263 if (cfag12864b_buffer
264 [i
* CFAG12864B_ADDRESSES
/ 8
265 + k
/ 8 + (j
* 8 + b
) *
266 CFAG12864B_WIDTH
/ 8]
269 cfag12864b_writebyte(c
);
274 memcpy(cfag12864b_cache
, cfag12864b_buffer
, CFAG12864B_SIZE
);
277 if (cfag12864b_updating
)
282 * cfag12864b Exported Symbols
285 EXPORT_SYMBOL_GPL(cfag12864b_buffer
);
286 EXPORT_SYMBOL_GPL(cfag12864b_enable
);
287 EXPORT_SYMBOL_GPL(cfag12864b_disable
);
290 * Is the module inited?
293 static unsigned char cfag12864b_inited
;
294 unsigned char cfag12864b_isinited(void)
296 return cfag12864b_inited
;
298 EXPORT_SYMBOL_GPL(cfag12864b_isinited
);
304 static int __init
cfag12864b_init(void)
308 /* ks0108_init() must be called first */
309 if (!ks0108_isinited()) {
310 printk(KERN_ERR CFAG12864B_NAME
": ERROR: "
311 "ks0108 is not initialized\n");
314 BUILD_BUG_ON(PAGE_SIZE
< CFAG12864B_SIZE
);
316 cfag12864b_buffer
= (unsigned char *) get_zeroed_page(GFP_KERNEL
);
317 if (cfag12864b_buffer
== NULL
) {
318 printk(KERN_ERR CFAG12864B_NAME
": ERROR: "
319 "can't get a free page\n");
324 cfag12864b_cache
= kmalloc(CFAG12864B_SIZE
,
326 if (cfag12864b_cache
== NULL
) {
327 printk(KERN_ERR CFAG12864B_NAME
": ERROR: "
328 "can't alloc cache buffer (%i bytes)\n",
334 cfag12864b_workqueue
= create_singlethread_workqueue(CFAG12864B_NAME
);
335 if (cfag12864b_workqueue
== NULL
)
341 cfag12864b_inited
= 1;
345 kfree(cfag12864b_cache
);
348 free_page((unsigned long) cfag12864b_buffer
);
354 static void __exit
cfag12864b_exit(void)
356 cfag12864b_disable();
358 destroy_workqueue(cfag12864b_workqueue
);
359 kfree(cfag12864b_cache
);
360 free_page((unsigned long) cfag12864b_buffer
);
363 module_init(cfag12864b_init
);
364 module_exit(cfag12864b_exit
);
366 MODULE_LICENSE("GPL v2");
367 MODULE_AUTHOR("Miguel Ojeda <ojeda@kernel.org>");
368 MODULE_DESCRIPTION("cfag12864b LCD driver");