0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012 #include <linux/init.h>
0013 #include <linux/module.h>
0014 #include <linux/kernel.h>
0015 #include <linux/fs.h>
0016 #include <linux/slab.h>
0017 #include <linux/cdev.h>
0018 #include <linux/delay.h>
0019 #include <linux/device.h>
0020 #include <linux/jiffies.h>
0021 #include <linux/mutex.h>
0022 #include <linux/uaccess.h>
0023 #include <linux/vmalloc.h>
0024 #include <linux/workqueue.h>
0025 #include <linux/ks0108.h>
0026 #include <linux/cfag12864b.h>
0027
0028
0029 #define CFAG12864B_NAME "cfag12864b"
0030
0031
0032
0033
0034
0035 static unsigned int cfag12864b_rate = CONFIG_CFAG12864B_RATE;
0036 module_param(cfag12864b_rate, uint, 0444);
0037 MODULE_PARM_DESC(cfag12864b_rate,
0038 "Refresh rate (hertz)");
0039
0040 unsigned int cfag12864b_getrate(void)
0041 {
0042 return cfag12864b_rate;
0043 }
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064 #define bit(n) (((unsigned char)1)<<(n))
0065
0066 #define CFAG12864B_BIT_E (0)
0067 #define CFAG12864B_BIT_CS1 (2)
0068 #define CFAG12864B_BIT_CS2 (1)
0069 #define CFAG12864B_BIT_DI (3)
0070
0071 static unsigned char cfag12864b_state;
0072
0073 static void cfag12864b_set(void)
0074 {
0075 ks0108_writecontrol(cfag12864b_state);
0076 }
0077
0078 static void cfag12864b_setbit(unsigned char state, unsigned char n)
0079 {
0080 if (state)
0081 cfag12864b_state |= bit(n);
0082 else
0083 cfag12864b_state &= ~bit(n);
0084 }
0085
0086 static void cfag12864b_e(unsigned char state)
0087 {
0088 cfag12864b_setbit(state, CFAG12864B_BIT_E);
0089 cfag12864b_set();
0090 }
0091
0092 static void cfag12864b_cs1(unsigned char state)
0093 {
0094 cfag12864b_setbit(state, CFAG12864B_BIT_CS1);
0095 }
0096
0097 static void cfag12864b_cs2(unsigned char state)
0098 {
0099 cfag12864b_setbit(state, CFAG12864B_BIT_CS2);
0100 }
0101
0102 static void cfag12864b_di(unsigned char state)
0103 {
0104 cfag12864b_setbit(state, CFAG12864B_BIT_DI);
0105 }
0106
0107 static void cfag12864b_setcontrollers(unsigned char first,
0108 unsigned char second)
0109 {
0110 if (first)
0111 cfag12864b_cs1(0);
0112 else
0113 cfag12864b_cs1(1);
0114
0115 if (second)
0116 cfag12864b_cs2(0);
0117 else
0118 cfag12864b_cs2(1);
0119 }
0120
0121 static void cfag12864b_controller(unsigned char which)
0122 {
0123 if (which == 0)
0124 cfag12864b_setcontrollers(1, 0);
0125 else if (which == 1)
0126 cfag12864b_setcontrollers(0, 1);
0127 }
0128
0129 static void cfag12864b_displaystate(unsigned char state)
0130 {
0131 cfag12864b_di(0);
0132 cfag12864b_e(1);
0133 ks0108_displaystate(state);
0134 cfag12864b_e(0);
0135 }
0136
0137 static void cfag12864b_address(unsigned char address)
0138 {
0139 cfag12864b_di(0);
0140 cfag12864b_e(1);
0141 ks0108_address(address);
0142 cfag12864b_e(0);
0143 }
0144
0145 static void cfag12864b_page(unsigned char page)
0146 {
0147 cfag12864b_di(0);
0148 cfag12864b_e(1);
0149 ks0108_page(page);
0150 cfag12864b_e(0);
0151 }
0152
0153 static void cfag12864b_startline(unsigned char startline)
0154 {
0155 cfag12864b_di(0);
0156 cfag12864b_e(1);
0157 ks0108_startline(startline);
0158 cfag12864b_e(0);
0159 }
0160
0161 static void cfag12864b_writebyte(unsigned char byte)
0162 {
0163 cfag12864b_di(1);
0164 cfag12864b_e(1);
0165 ks0108_writedata(byte);
0166 cfag12864b_e(0);
0167 }
0168
0169 static void cfag12864b_nop(void)
0170 {
0171 cfag12864b_startline(0);
0172 }
0173
0174
0175
0176
0177
0178 static void cfag12864b_on(void)
0179 {
0180 cfag12864b_setcontrollers(1, 1);
0181 cfag12864b_displaystate(1);
0182 }
0183
0184 static void cfag12864b_off(void)
0185 {
0186 cfag12864b_setcontrollers(1, 1);
0187 cfag12864b_displaystate(0);
0188 }
0189
0190 static void cfag12864b_clear(void)
0191 {
0192 unsigned char i, j;
0193
0194 cfag12864b_setcontrollers(1, 1);
0195 for (i = 0; i < CFAG12864B_PAGES; i++) {
0196 cfag12864b_page(i);
0197 cfag12864b_address(0);
0198 for (j = 0; j < CFAG12864B_ADDRESSES; j++)
0199 cfag12864b_writebyte(0);
0200 }
0201 }
0202
0203
0204
0205
0206
0207 unsigned char *cfag12864b_buffer;
0208 static unsigned char *cfag12864b_cache;
0209 static DEFINE_MUTEX(cfag12864b_mutex);
0210 static unsigned char cfag12864b_updating;
0211 static void cfag12864b_update(struct work_struct *delayed_work);
0212 static struct workqueue_struct *cfag12864b_workqueue;
0213 static DECLARE_DELAYED_WORK(cfag12864b_work, cfag12864b_update);
0214
0215 static void cfag12864b_queue(void)
0216 {
0217 queue_delayed_work(cfag12864b_workqueue, &cfag12864b_work,
0218 HZ / cfag12864b_rate);
0219 }
0220
0221 unsigned char cfag12864b_enable(void)
0222 {
0223 unsigned char ret;
0224
0225 mutex_lock(&cfag12864b_mutex);
0226
0227 if (!cfag12864b_updating) {
0228 cfag12864b_updating = 1;
0229 cfag12864b_queue();
0230 ret = 0;
0231 } else
0232 ret = 1;
0233
0234 mutex_unlock(&cfag12864b_mutex);
0235
0236 return ret;
0237 }
0238
0239 void cfag12864b_disable(void)
0240 {
0241 mutex_lock(&cfag12864b_mutex);
0242
0243 if (cfag12864b_updating) {
0244 cfag12864b_updating = 0;
0245 cancel_delayed_work(&cfag12864b_work);
0246 flush_workqueue(cfag12864b_workqueue);
0247 }
0248
0249 mutex_unlock(&cfag12864b_mutex);
0250 }
0251
0252 unsigned char cfag12864b_isenabled(void)
0253 {
0254 return cfag12864b_updating;
0255 }
0256
0257 static void cfag12864b_update(struct work_struct *work)
0258 {
0259 unsigned char c;
0260 unsigned short i, j, k, b;
0261
0262 if (memcmp(cfag12864b_cache, cfag12864b_buffer, CFAG12864B_SIZE)) {
0263 for (i = 0; i < CFAG12864B_CONTROLLERS; i++) {
0264 cfag12864b_controller(i);
0265 cfag12864b_nop();
0266 for (j = 0; j < CFAG12864B_PAGES; j++) {
0267 cfag12864b_page(j);
0268 cfag12864b_nop();
0269 cfag12864b_address(0);
0270 cfag12864b_nop();
0271 for (k = 0; k < CFAG12864B_ADDRESSES; k++) {
0272 for (c = 0, b = 0; b < 8; b++)
0273 if (cfag12864b_buffer
0274 [i * CFAG12864B_ADDRESSES / 8
0275 + k / 8 + (j * 8 + b) *
0276 CFAG12864B_WIDTH / 8]
0277 & bit(k % 8))
0278 c |= bit(b);
0279 cfag12864b_writebyte(c);
0280 }
0281 }
0282 }
0283
0284 memcpy(cfag12864b_cache, cfag12864b_buffer, CFAG12864B_SIZE);
0285 }
0286
0287 if (cfag12864b_updating)
0288 cfag12864b_queue();
0289 }
0290
0291
0292
0293
0294
0295 EXPORT_SYMBOL_GPL(cfag12864b_buffer);
0296 EXPORT_SYMBOL_GPL(cfag12864b_getrate);
0297 EXPORT_SYMBOL_GPL(cfag12864b_enable);
0298 EXPORT_SYMBOL_GPL(cfag12864b_disable);
0299 EXPORT_SYMBOL_GPL(cfag12864b_isenabled);
0300
0301
0302
0303
0304
0305 static unsigned char cfag12864b_inited;
0306 unsigned char cfag12864b_isinited(void)
0307 {
0308 return cfag12864b_inited;
0309 }
0310 EXPORT_SYMBOL_GPL(cfag12864b_isinited);
0311
0312
0313
0314
0315
0316 static int __init cfag12864b_init(void)
0317 {
0318 int ret = -EINVAL;
0319
0320
0321 if (!ks0108_isinited()) {
0322 printk(KERN_ERR CFAG12864B_NAME ": ERROR: "
0323 "ks0108 is not initialized\n");
0324 goto none;
0325 }
0326 BUILD_BUG_ON(PAGE_SIZE < CFAG12864B_SIZE);
0327
0328 cfag12864b_buffer = (unsigned char *) get_zeroed_page(GFP_KERNEL);
0329 if (cfag12864b_buffer == NULL) {
0330 printk(KERN_ERR CFAG12864B_NAME ": ERROR: "
0331 "can't get a free page\n");
0332 ret = -ENOMEM;
0333 goto none;
0334 }
0335
0336 cfag12864b_cache = kmalloc(CFAG12864B_SIZE,
0337 GFP_KERNEL);
0338 if (cfag12864b_cache == NULL) {
0339 printk(KERN_ERR CFAG12864B_NAME ": ERROR: "
0340 "can't alloc cache buffer (%i bytes)\n",
0341 CFAG12864B_SIZE);
0342 ret = -ENOMEM;
0343 goto bufferalloced;
0344 }
0345
0346 cfag12864b_workqueue = create_singlethread_workqueue(CFAG12864B_NAME);
0347 if (cfag12864b_workqueue == NULL)
0348 goto cachealloced;
0349
0350 cfag12864b_clear();
0351 cfag12864b_on();
0352
0353 cfag12864b_inited = 1;
0354 return 0;
0355
0356 cachealloced:
0357 kfree(cfag12864b_cache);
0358
0359 bufferalloced:
0360 free_page((unsigned long) cfag12864b_buffer);
0361
0362 none:
0363 return ret;
0364 }
0365
0366 static void __exit cfag12864b_exit(void)
0367 {
0368 cfag12864b_disable();
0369 cfag12864b_off();
0370 destroy_workqueue(cfag12864b_workqueue);
0371 kfree(cfag12864b_cache);
0372 free_page((unsigned long) cfag12864b_buffer);
0373 }
0374
0375 module_init(cfag12864b_init);
0376 module_exit(cfag12864b_exit);
0377
0378 MODULE_LICENSE("GPL v2");
0379 MODULE_AUTHOR("Miguel Ojeda <ojeda@kernel.org>");
0380 MODULE_DESCRIPTION("cfag12864b LCD driver");