cfag12864b.c 7.3 KB

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