2  *    Filename: cfag12864b.c
 
   4  * Description: cfag12864b LCD driver
 
   8  *      Author: Copyright (C) Miguel Ojeda Sandonis <maxextreme@gmail.com>
 
  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/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"
 
  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;
 
  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)
 
  94                 cfag12864b_state |= bit(n);
 
  96                 cfag12864b_state &= ~bit(n);
 
  99 static void cfag12864b_e(unsigned char state)
 
 101         cfag12864b_setbit(state, CFAG12864B_BIT_E);
 
 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)
 
 134 static void cfag12864b_controller(unsigned char which)
 
 137                 cfag12864b_setcontrollers(1, 0);
 
 139                 cfag12864b_setcontrollers(0, 1);
 
 142 static void cfag12864b_displaystate(unsigned char state)
 
 146         ks0108_displaystate(state);
 
 150 static void cfag12864b_address(unsigned char address)
 
 154         ks0108_address(address);
 
 158 static void cfag12864b_page(unsigned char page)
 
 166 static void cfag12864b_startline(unsigned char startline)
 
 170         ks0108_startline(startline);
 
 174 static void cfag12864b_writebyte(unsigned char byte)
 
 178         ks0108_writedata(byte);
 
 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)
 
 207         cfag12864b_setcontrollers(1, 1);
 
 208         for (i = 0; i < CFAG12864B_PAGES; i++) {
 
 210                 cfag12864b_address(0);
 
 211                 for (j = 0; j < CFAG12864B_ADDRESSES; j++)
 
 212                         cfag12864b_writebyte(0);
 
 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)
 
 238         mutex_lock(&cfag12864b_mutex);
 
 240         if (!cfag12864b_updating) {
 
 241                 cfag12864b_updating = 1;
 
 247         mutex_unlock(&cfag12864b_mutex);
 
 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)
 
 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);
 
 279                         for (j = 0; j < CFAG12864B_PAGES; j++) {
 
 282                                 cfag12864b_address(0);
 
 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]
 
 292                                         cfag12864b_writebyte(c);
 
 297                 memcpy(cfag12864b_cache, cfag12864b_buffer, CFAG12864B_SIZE);
 
 300         if (cfag12864b_updating)
 
 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);
 
 329 static int __init cfag12864b_init(void)
 
 333         /* ks0108_init() must be called first */
 
 334         if (!ks0108_isinited()) {
 
 335                 printk(KERN_ERR CFAG12864B_NAME ": ERROR: "
 
 336                         "ks0108 is not initialized\n");
 
 340         if (PAGE_SIZE < CFAG12864B_SIZE) {
 
 341                 printk(KERN_ERR CFAG12864B_NAME ": ERROR: "
 
 342                         "page size (%i) < cfag12864b size (%i)\n",
 
 343                         (unsigned int)PAGE_SIZE, CFAG12864B_SIZE);
 
 348         cfag12864b_buffer = (unsigned char *) __get_free_page(GFP_KERNEL);
 
 349         if (cfag12864b_buffer == NULL) {
 
 350                 printk(KERN_ERR CFAG12864B_NAME ": ERROR: "
 
 351                         "can't get a free page\n");
 
 356         cfag12864b_cache = kmalloc(sizeof(unsigned char) *
 
 357                 CFAG12864B_SIZE, GFP_KERNEL);
 
 358         if (cfag12864b_buffer == NULL) {
 
 359                 printk(KERN_ERR CFAG12864B_NAME ": ERROR: "
 
 360                         "can't alloc cache buffer (%i bytes)\n",
 
 366         cfag12864b_workqueue = create_singlethread_workqueue(CFAG12864B_NAME);
 
 367         if (cfag12864b_workqueue == NULL)
 
 370         memset(cfag12864b_buffer, 0, CFAG12864B_SIZE);
 
 375         cfag12864b_inited = 1;
 
 379         kfree(cfag12864b_cache);
 
 382         free_page((unsigned long) cfag12864b_buffer);
 
 388 static void __exit cfag12864b_exit(void)
 
 390         cfag12864b_disable();
 
 392         destroy_workqueue(cfag12864b_workqueue);
 
 393         kfree(cfag12864b_cache);
 
 394         free_page((unsigned long) cfag12864b_buffer);
 
 397 module_init(cfag12864b_init);
 
 398 module_exit(cfag12864b_exit);
 
 400 MODULE_LICENSE("GPL v2");
 
 401 MODULE_AUTHOR("Miguel Ojeda Sandonis <maxextreme@gmail.com>");
 
 402 MODULE_DESCRIPTION("cfag12864b LCD driver");