cfag12864b.c 8.2 KB

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