Loading...
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Filename: cfag12864b.c
4 * Version: 0.1.0
5 * Description: cfag12864b LCD driver
6 * Depends: ks0108
7 *
8 * Author: Copyright (C) Miguel Ojeda Sandonis
9 * Date: 2006-10-31
10 */
11
12#include <linux/init.h>
13#include <linux/module.h>
14#include <linux/kernel.h>
15#include <linux/fs.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>
27
28
29#define CFAG12864B_NAME "cfag12864b"
30
31/*
32 * Module Parameters
33 */
34
35static unsigned int cfag12864b_rate = CONFIG_CFAG12864B_RATE;
36module_param(cfag12864b_rate, uint, S_IRUGO);
37MODULE_PARM_DESC(cfag12864b_rate,
38 "Refresh rate (hertz)");
39
40unsigned int cfag12864b_getrate(void)
41{
42 return cfag12864b_rate;
43}
44
45/*
46 * cfag12864b Commands
47 *
48 * E = Enable signal
49 * Every time E switch from low to high,
50 * cfag12864b/ks0108 reads the command/data.
51 *
52 * CS1 = First ks0108controller.
53 * If high, the first ks0108 controller receives commands/data.
54 *
55 * CS2 = Second ks0108 controller
56 * If high, the second ks0108 controller receives commands/data.
57 *
58 * DI = Data/Instruction
59 * If low, cfag12864b will expect commands.
60 * If high, cfag12864b will expect data.
61 *
62 */
63
64#define bit(n) (((unsigned char)1)<<(n))
65
66#define CFAG12864B_BIT_E (0)
67#define CFAG12864B_BIT_CS1 (2)
68#define CFAG12864B_BIT_CS2 (1)
69#define CFAG12864B_BIT_DI (3)
70
71static unsigned char cfag12864b_state;
72
73static void cfag12864b_set(void)
74{
75 ks0108_writecontrol(cfag12864b_state);
76}
77
78static void cfag12864b_setbit(unsigned char state, unsigned char n)
79{
80 if (state)
81 cfag12864b_state |= bit(n);
82 else
83 cfag12864b_state &= ~bit(n);
84}
85
86static void cfag12864b_e(unsigned char state)
87{
88 cfag12864b_setbit(state, CFAG12864B_BIT_E);
89 cfag12864b_set();
90}
91
92static void cfag12864b_cs1(unsigned char state)
93{
94 cfag12864b_setbit(state, CFAG12864B_BIT_CS1);
95}
96
97static void cfag12864b_cs2(unsigned char state)
98{
99 cfag12864b_setbit(state, CFAG12864B_BIT_CS2);
100}
101
102static void cfag12864b_di(unsigned char state)
103{
104 cfag12864b_setbit(state, CFAG12864B_BIT_DI);
105}
106
107static void cfag12864b_setcontrollers(unsigned char first,
108 unsigned char second)
109{
110 if (first)
111 cfag12864b_cs1(0);
112 else
113 cfag12864b_cs1(1);
114
115 if (second)
116 cfag12864b_cs2(0);
117 else
118 cfag12864b_cs2(1);
119}
120
121static void cfag12864b_controller(unsigned char which)
122{
123 if (which == 0)
124 cfag12864b_setcontrollers(1, 0);
125 else if (which == 1)
126 cfag12864b_setcontrollers(0, 1);
127}
128
129static void cfag12864b_displaystate(unsigned char state)
130{
131 cfag12864b_di(0);
132 cfag12864b_e(1);
133 ks0108_displaystate(state);
134 cfag12864b_e(0);
135}
136
137static void cfag12864b_address(unsigned char address)
138{
139 cfag12864b_di(0);
140 cfag12864b_e(1);
141 ks0108_address(address);
142 cfag12864b_e(0);
143}
144
145static void cfag12864b_page(unsigned char page)
146{
147 cfag12864b_di(0);
148 cfag12864b_e(1);
149 ks0108_page(page);
150 cfag12864b_e(0);
151}
152
153static void cfag12864b_startline(unsigned char startline)
154{
155 cfag12864b_di(0);
156 cfag12864b_e(1);
157 ks0108_startline(startline);
158 cfag12864b_e(0);
159}
160
161static void cfag12864b_writebyte(unsigned char byte)
162{
163 cfag12864b_di(1);
164 cfag12864b_e(1);
165 ks0108_writedata(byte);
166 cfag12864b_e(0);
167}
168
169static void cfag12864b_nop(void)
170{
171 cfag12864b_startline(0);
172}
173
174/*
175 * cfag12864b Internal Commands
176 */
177
178static void cfag12864b_on(void)
179{
180 cfag12864b_setcontrollers(1, 1);
181 cfag12864b_displaystate(1);
182}
183
184static void cfag12864b_off(void)
185{
186 cfag12864b_setcontrollers(1, 1);
187 cfag12864b_displaystate(0);
188}
189
190static void cfag12864b_clear(void)
191{
192 unsigned char i, j;
193
194 cfag12864b_setcontrollers(1, 1);
195 for (i = 0; i < CFAG12864B_PAGES; i++) {
196 cfag12864b_page(i);
197 cfag12864b_address(0);
198 for (j = 0; j < CFAG12864B_ADDRESSES; j++)
199 cfag12864b_writebyte(0);
200 }
201}
202
203/*
204 * Update work
205 */
206
207unsigned char *cfag12864b_buffer;
208static unsigned char *cfag12864b_cache;
209static DEFINE_MUTEX(cfag12864b_mutex);
210static unsigned char cfag12864b_updating;
211static void cfag12864b_update(struct work_struct *delayed_work);
212static struct workqueue_struct *cfag12864b_workqueue;
213static DECLARE_DELAYED_WORK(cfag12864b_work, cfag12864b_update);
214
215static void cfag12864b_queue(void)
216{
217 queue_delayed_work(cfag12864b_workqueue, &cfag12864b_work,
218 HZ / cfag12864b_rate);
219}
220
221unsigned char cfag12864b_enable(void)
222{
223 unsigned char ret;
224
225 mutex_lock(&cfag12864b_mutex);
226
227 if (!cfag12864b_updating) {
228 cfag12864b_updating = 1;
229 cfag12864b_queue();
230 ret = 0;
231 } else
232 ret = 1;
233
234 mutex_unlock(&cfag12864b_mutex);
235
236 return ret;
237}
238
239void cfag12864b_disable(void)
240{
241 mutex_lock(&cfag12864b_mutex);
242
243 if (cfag12864b_updating) {
244 cfag12864b_updating = 0;
245 cancel_delayed_work(&cfag12864b_work);
246 flush_workqueue(cfag12864b_workqueue);
247 }
248
249 mutex_unlock(&cfag12864b_mutex);
250}
251
252unsigned char cfag12864b_isenabled(void)
253{
254 return cfag12864b_updating;
255}
256
257static void cfag12864b_update(struct work_struct *work)
258{
259 unsigned char c;
260 unsigned short i, j, k, b;
261
262 if (memcmp(cfag12864b_cache, cfag12864b_buffer, CFAG12864B_SIZE)) {
263 for (i = 0; i < CFAG12864B_CONTROLLERS; i++) {
264 cfag12864b_controller(i);
265 cfag12864b_nop();
266 for (j = 0; j < CFAG12864B_PAGES; j++) {
267 cfag12864b_page(j);
268 cfag12864b_nop();
269 cfag12864b_address(0);
270 cfag12864b_nop();
271 for (k = 0; k < CFAG12864B_ADDRESSES; k++) {
272 for (c = 0, b = 0; b < 8; b++)
273 if (cfag12864b_buffer
274 [i * CFAG12864B_ADDRESSES / 8
275 + k / 8 + (j * 8 + b) *
276 CFAG12864B_WIDTH / 8]
277 & bit(k % 8))
278 c |= bit(b);
279 cfag12864b_writebyte(c);
280 }
281 }
282 }
283
284 memcpy(cfag12864b_cache, cfag12864b_buffer, CFAG12864B_SIZE);
285 }
286
287 if (cfag12864b_updating)
288 cfag12864b_queue();
289}
290
291/*
292 * cfag12864b Exported Symbols
293 */
294
295EXPORT_SYMBOL_GPL(cfag12864b_buffer);
296EXPORT_SYMBOL_GPL(cfag12864b_getrate);
297EXPORT_SYMBOL_GPL(cfag12864b_enable);
298EXPORT_SYMBOL_GPL(cfag12864b_disable);
299EXPORT_SYMBOL_GPL(cfag12864b_isenabled);
300
301/*
302 * Is the module inited?
303 */
304
305static unsigned char cfag12864b_inited;
306unsigned char cfag12864b_isinited(void)
307{
308 return cfag12864b_inited;
309}
310EXPORT_SYMBOL_GPL(cfag12864b_isinited);
311
312/*
313 * Module Init & Exit
314 */
315
316static int __init cfag12864b_init(void)
317{
318 int ret = -EINVAL;
319
320 /* ks0108_init() must be called first */
321 if (!ks0108_isinited()) {
322 printk(KERN_ERR CFAG12864B_NAME ": ERROR: "
323 "ks0108 is not initialized\n");
324 goto none;
325 }
326 BUILD_BUG_ON(PAGE_SIZE < CFAG12864B_SIZE);
327
328 cfag12864b_buffer = (unsigned char *) get_zeroed_page(GFP_KERNEL);
329 if (cfag12864b_buffer == NULL) {
330 printk(KERN_ERR CFAG12864B_NAME ": ERROR: "
331 "can't get a free page\n");
332 ret = -ENOMEM;
333 goto none;
334 }
335
336 cfag12864b_cache = kmalloc(CFAG12864B_SIZE,
337 GFP_KERNEL);
338 if (cfag12864b_cache == NULL) {
339 printk(KERN_ERR CFAG12864B_NAME ": ERROR: "
340 "can't alloc cache buffer (%i bytes)\n",
341 CFAG12864B_SIZE);
342 ret = -ENOMEM;
343 goto bufferalloced;
344 }
345
346 cfag12864b_workqueue = create_singlethread_workqueue(CFAG12864B_NAME);
347 if (cfag12864b_workqueue == NULL)
348 goto cachealloced;
349
350 cfag12864b_clear();
351 cfag12864b_on();
352
353 cfag12864b_inited = 1;
354 return 0;
355
356cachealloced:
357 kfree(cfag12864b_cache);
358
359bufferalloced:
360 free_page((unsigned long) cfag12864b_buffer);
361
362none:
363 return ret;
364}
365
366static void __exit cfag12864b_exit(void)
367{
368 cfag12864b_disable();
369 cfag12864b_off();
370 destroy_workqueue(cfag12864b_workqueue);
371 kfree(cfag12864b_cache);
372 free_page((unsigned long) cfag12864b_buffer);
373}
374
375module_init(cfag12864b_init);
376module_exit(cfag12864b_exit);
377
378MODULE_LICENSE("GPL v2");
379MODULE_AUTHOR("Miguel Ojeda Sandonis <miguel.ojeda.sandonis@gmail.com>");
380MODULE_DESCRIPTION("cfag12864b LCD driver");
1/*
2 * Filename: cfag12864b.c
3 * Version: 0.1.0
4 * Description: cfag12864b LCD driver
5 * License: GPLv2
6 * Depends: ks0108
7 *
8 * Author: Copyright (C) Miguel Ojeda Sandonis
9 * Date: 2006-10-31
10 *
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.
14 *
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.
19 *
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
23 *
24 */
25
26#include <linux/init.h>
27#include <linux/module.h>
28#include <linux/kernel.h>
29#include <linux/fs.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>
41
42
43#define CFAG12864B_NAME "cfag12864b"
44
45/*
46 * Module Parameters
47 */
48
49static unsigned int cfag12864b_rate = CONFIG_CFAG12864B_RATE;
50module_param(cfag12864b_rate, uint, S_IRUGO);
51MODULE_PARM_DESC(cfag12864b_rate,
52 "Refresh rate (hertz)");
53
54unsigned int cfag12864b_getrate(void)
55{
56 return cfag12864b_rate;
57}
58
59/*
60 * cfag12864b Commands
61 *
62 * E = Enable signal
63 * Every time E switch from low to high,
64 * cfag12864b/ks0108 reads the command/data.
65 *
66 * CS1 = First ks0108controller.
67 * If high, the first ks0108 controller receives commands/data.
68 *
69 * CS2 = Second ks0108 controller
70 * If high, the second ks0108 controller receives commands/data.
71 *
72 * DI = Data/Instruction
73 * If low, cfag12864b will expect commands.
74 * If high, cfag12864b will expect data.
75 *
76 */
77
78#define bit(n) (((unsigned char)1)<<(n))
79
80#define CFAG12864B_BIT_E (0)
81#define CFAG12864B_BIT_CS1 (2)
82#define CFAG12864B_BIT_CS2 (1)
83#define CFAG12864B_BIT_DI (3)
84
85static unsigned char cfag12864b_state;
86
87static void cfag12864b_set(void)
88{
89 ks0108_writecontrol(cfag12864b_state);
90}
91
92static void cfag12864b_setbit(unsigned char state, unsigned char n)
93{
94 if (state)
95 cfag12864b_state |= bit(n);
96 else
97 cfag12864b_state &= ~bit(n);
98}
99
100static void cfag12864b_e(unsigned char state)
101{
102 cfag12864b_setbit(state, CFAG12864B_BIT_E);
103 cfag12864b_set();
104}
105
106static void cfag12864b_cs1(unsigned char state)
107{
108 cfag12864b_setbit(state, CFAG12864B_BIT_CS1);
109}
110
111static void cfag12864b_cs2(unsigned char state)
112{
113 cfag12864b_setbit(state, CFAG12864B_BIT_CS2);
114}
115
116static void cfag12864b_di(unsigned char state)
117{
118 cfag12864b_setbit(state, CFAG12864B_BIT_DI);
119}
120
121static void cfag12864b_setcontrollers(unsigned char first,
122 unsigned char second)
123{
124 if (first)
125 cfag12864b_cs1(0);
126 else
127 cfag12864b_cs1(1);
128
129 if (second)
130 cfag12864b_cs2(0);
131 else
132 cfag12864b_cs2(1);
133}
134
135static void cfag12864b_controller(unsigned char which)
136{
137 if (which == 0)
138 cfag12864b_setcontrollers(1, 0);
139 else if (which == 1)
140 cfag12864b_setcontrollers(0, 1);
141}
142
143static void cfag12864b_displaystate(unsigned char state)
144{
145 cfag12864b_di(0);
146 cfag12864b_e(1);
147 ks0108_displaystate(state);
148 cfag12864b_e(0);
149}
150
151static void cfag12864b_address(unsigned char address)
152{
153 cfag12864b_di(0);
154 cfag12864b_e(1);
155 ks0108_address(address);
156 cfag12864b_e(0);
157}
158
159static void cfag12864b_page(unsigned char page)
160{
161 cfag12864b_di(0);
162 cfag12864b_e(1);
163 ks0108_page(page);
164 cfag12864b_e(0);
165}
166
167static void cfag12864b_startline(unsigned char startline)
168{
169 cfag12864b_di(0);
170 cfag12864b_e(1);
171 ks0108_startline(startline);
172 cfag12864b_e(0);
173}
174
175static void cfag12864b_writebyte(unsigned char byte)
176{
177 cfag12864b_di(1);
178 cfag12864b_e(1);
179 ks0108_writedata(byte);
180 cfag12864b_e(0);
181}
182
183static void cfag12864b_nop(void)
184{
185 cfag12864b_startline(0);
186}
187
188/*
189 * cfag12864b Internal Commands
190 */
191
192static void cfag12864b_on(void)
193{
194 cfag12864b_setcontrollers(1, 1);
195 cfag12864b_displaystate(1);
196}
197
198static void cfag12864b_off(void)
199{
200 cfag12864b_setcontrollers(1, 1);
201 cfag12864b_displaystate(0);
202}
203
204static void cfag12864b_clear(void)
205{
206 unsigned char i, j;
207
208 cfag12864b_setcontrollers(1, 1);
209 for (i = 0; i < CFAG12864B_PAGES; i++) {
210 cfag12864b_page(i);
211 cfag12864b_address(0);
212 for (j = 0; j < CFAG12864B_ADDRESSES; j++)
213 cfag12864b_writebyte(0);
214 }
215}
216
217/*
218 * Update work
219 */
220
221unsigned char *cfag12864b_buffer;
222static unsigned char *cfag12864b_cache;
223static DEFINE_MUTEX(cfag12864b_mutex);
224static unsigned char cfag12864b_updating;
225static void cfag12864b_update(struct work_struct *delayed_work);
226static struct workqueue_struct *cfag12864b_workqueue;
227static DECLARE_DELAYED_WORK(cfag12864b_work, cfag12864b_update);
228
229static void cfag12864b_queue(void)
230{
231 queue_delayed_work(cfag12864b_workqueue, &cfag12864b_work,
232 HZ / cfag12864b_rate);
233}
234
235unsigned char cfag12864b_enable(void)
236{
237 unsigned char ret;
238
239 mutex_lock(&cfag12864b_mutex);
240
241 if (!cfag12864b_updating) {
242 cfag12864b_updating = 1;
243 cfag12864b_queue();
244 ret = 0;
245 } else
246 ret = 1;
247
248 mutex_unlock(&cfag12864b_mutex);
249
250 return ret;
251}
252
253void cfag12864b_disable(void)
254{
255 mutex_lock(&cfag12864b_mutex);
256
257 if (cfag12864b_updating) {
258 cfag12864b_updating = 0;
259 cancel_delayed_work(&cfag12864b_work);
260 flush_workqueue(cfag12864b_workqueue);
261 }
262
263 mutex_unlock(&cfag12864b_mutex);
264}
265
266unsigned char cfag12864b_isenabled(void)
267{
268 return cfag12864b_updating;
269}
270
271static void cfag12864b_update(struct work_struct *work)
272{
273 unsigned char c;
274 unsigned short i, j, k, b;
275
276 if (memcmp(cfag12864b_cache, cfag12864b_buffer, CFAG12864B_SIZE)) {
277 for (i = 0; i < CFAG12864B_CONTROLLERS; i++) {
278 cfag12864b_controller(i);
279 cfag12864b_nop();
280 for (j = 0; j < CFAG12864B_PAGES; j++) {
281 cfag12864b_page(j);
282 cfag12864b_nop();
283 cfag12864b_address(0);
284 cfag12864b_nop();
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]
291 & bit(k % 8))
292 c |= bit(b);
293 cfag12864b_writebyte(c);
294 }
295 }
296 }
297
298 memcpy(cfag12864b_cache, cfag12864b_buffer, CFAG12864B_SIZE);
299 }
300
301 if (cfag12864b_updating)
302 cfag12864b_queue();
303}
304
305/*
306 * cfag12864b Exported Symbols
307 */
308
309EXPORT_SYMBOL_GPL(cfag12864b_buffer);
310EXPORT_SYMBOL_GPL(cfag12864b_getrate);
311EXPORT_SYMBOL_GPL(cfag12864b_enable);
312EXPORT_SYMBOL_GPL(cfag12864b_disable);
313EXPORT_SYMBOL_GPL(cfag12864b_isenabled);
314
315/*
316 * Is the module inited?
317 */
318
319static unsigned char cfag12864b_inited;
320unsigned char cfag12864b_isinited(void)
321{
322 return cfag12864b_inited;
323}
324EXPORT_SYMBOL_GPL(cfag12864b_isinited);
325
326/*
327 * Module Init & Exit
328 */
329
330static int __init cfag12864b_init(void)
331{
332 int ret = -EINVAL;
333
334 /* ks0108_init() must be called first */
335 if (!ks0108_isinited()) {
336 printk(KERN_ERR CFAG12864B_NAME ": ERROR: "
337 "ks0108 is not initialized\n");
338 goto none;
339 }
340 BUILD_BUG_ON(PAGE_SIZE < CFAG12864B_SIZE);
341
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");
346 ret = -ENOMEM;
347 goto none;
348 }
349
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",
355 CFAG12864B_SIZE);
356 ret = -ENOMEM;
357 goto bufferalloced;
358 }
359
360 cfag12864b_workqueue = create_singlethread_workqueue(CFAG12864B_NAME);
361 if (cfag12864b_workqueue == NULL)
362 goto cachealloced;
363
364 cfag12864b_clear();
365 cfag12864b_on();
366
367 cfag12864b_inited = 1;
368 return 0;
369
370cachealloced:
371 kfree(cfag12864b_cache);
372
373bufferalloced:
374 free_page((unsigned long) cfag12864b_buffer);
375
376none:
377 return ret;
378}
379
380static void __exit cfag12864b_exit(void)
381{
382 cfag12864b_disable();
383 cfag12864b_off();
384 destroy_workqueue(cfag12864b_workqueue);
385 kfree(cfag12864b_cache);
386 free_page((unsigned long) cfag12864b_buffer);
387}
388
389module_init(cfag12864b_init);
390module_exit(cfag12864b_exit);
391
392MODULE_LICENSE("GPL v2");
393MODULE_AUTHOR("Miguel Ojeda Sandonis <miguel.ojeda.sandonis@gmail.com>");
394MODULE_DESCRIPTION("cfag12864b LCD driver");