cfag12864b.c 7.5 KB

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