cfag12864b.c 8.2 KB

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