lcd.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. /*
  2. * LCD, LED and Button interface for Cobalt
  3. *
  4. * This file is subject to the terms and conditions of the GNU General Public
  5. * License. See the file "COPYING" in the main directory of this archive
  6. * for more details.
  7. *
  8. * Copyright (C) 1996, 1997 by Andrew Bose
  9. *
  10. * Linux kernel version history:
  11. * March 2001: Ported from 2.0.34 by Liam Davies
  12. *
  13. */
  14. #include <linux/types.h>
  15. #include <linux/errno.h>
  16. #include <linux/miscdevice.h>
  17. #include <linux/slab.h>
  18. #include <linux/ioport.h>
  19. #include <linux/fcntl.h>
  20. #include <linux/mc146818rtc.h>
  21. #include <linux/netdevice.h>
  22. #include <linux/sched.h>
  23. #include <linux/delay.h>
  24. #include <asm/io.h>
  25. #include <asm/uaccess.h>
  26. #include <asm/system.h>
  27. #include "lcd.h"
  28. static int lcd_ioctl(struct inode *inode, struct file *file,
  29. unsigned int cmd, unsigned long arg);
  30. static unsigned int lcd_present = 1;
  31. /* used in arch/mips/cobalt/reset.c */
  32. int led_state = 0;
  33. #if defined(CONFIG_TULIP) && 0
  34. #define MAX_INTERFACES 8
  35. static linkcheck_func_t linkcheck_callbacks[MAX_INTERFACES];
  36. static void *linkcheck_cookies[MAX_INTERFACES];
  37. int lcd_register_linkcheck_func(int iface_num, void *func, void *cookie)
  38. {
  39. if (iface_num < 0 ||
  40. iface_num >= MAX_INTERFACES ||
  41. linkcheck_callbacks[iface_num] != NULL)
  42. return -1;
  43. linkcheck_callbacks[iface_num] = (linkcheck_func_t) func;
  44. linkcheck_cookies[iface_num] = cookie;
  45. return 0;
  46. }
  47. #endif
  48. static int lcd_ioctl(struct inode *inode, struct file *file,
  49. unsigned int cmd, unsigned long arg)
  50. {
  51. struct lcd_display button_display;
  52. unsigned long address, a;
  53. switch (cmd) {
  54. case LCD_On:
  55. udelay(150);
  56. BusyCheck();
  57. LCDWriteInst(0x0F);
  58. break;
  59. case LCD_Off:
  60. udelay(150);
  61. BusyCheck();
  62. LCDWriteInst(0x08);
  63. break;
  64. case LCD_Reset:
  65. udelay(150);
  66. LCDWriteInst(0x3F);
  67. udelay(150);
  68. LCDWriteInst(0x3F);
  69. udelay(150);
  70. LCDWriteInst(0x3F);
  71. udelay(150);
  72. LCDWriteInst(0x3F);
  73. udelay(150);
  74. LCDWriteInst(0x01);
  75. udelay(150);
  76. LCDWriteInst(0x06);
  77. break;
  78. case LCD_Clear:
  79. udelay(150);
  80. BusyCheck();
  81. LCDWriteInst(0x01);
  82. break;
  83. case LCD_Cursor_Left:
  84. udelay(150);
  85. BusyCheck();
  86. LCDWriteInst(0x10);
  87. break;
  88. case LCD_Cursor_Right:
  89. udelay(150);
  90. BusyCheck();
  91. LCDWriteInst(0x14);
  92. break;
  93. case LCD_Cursor_Off:
  94. udelay(150);
  95. BusyCheck();
  96. LCDWriteInst(0x0C);
  97. break;
  98. case LCD_Cursor_On:
  99. udelay(150);
  100. BusyCheck();
  101. LCDWriteInst(0x0F);
  102. break;
  103. case LCD_Blink_Off:
  104. udelay(150);
  105. BusyCheck();
  106. LCDWriteInst(0x0E);
  107. break;
  108. case LCD_Get_Cursor_Pos:{
  109. struct lcd_display display;
  110. udelay(150);
  111. BusyCheck();
  112. display.cursor_address = (LCDReadInst);
  113. display.cursor_address =
  114. (display.cursor_address & 0x07F);
  115. if (copy_to_user
  116. ((struct lcd_display *) arg, &display,
  117. sizeof(struct lcd_display)))
  118. return -EFAULT;
  119. break;
  120. }
  121. case LCD_Set_Cursor_Pos:{
  122. struct lcd_display display;
  123. if (copy_from_user
  124. (&display, (struct lcd_display *) arg,
  125. sizeof(struct lcd_display)))
  126. return -EFAULT;
  127. a = (display.cursor_address | kLCD_Addr);
  128. udelay(150);
  129. BusyCheck();
  130. LCDWriteInst(a);
  131. break;
  132. }
  133. case LCD_Get_Cursor:{
  134. struct lcd_display display;
  135. udelay(150);
  136. BusyCheck();
  137. display.character = LCDReadData;
  138. if (copy_to_user
  139. ((struct lcd_display *) arg, &display,
  140. sizeof(struct lcd_display)))
  141. return -EFAULT;
  142. udelay(150);
  143. BusyCheck();
  144. LCDWriteInst(0x10);
  145. break;
  146. }
  147. case LCD_Set_Cursor:{
  148. struct lcd_display display;
  149. if (copy_from_user
  150. (&display, (struct lcd_display *) arg,
  151. sizeof(struct lcd_display)))
  152. return -EFAULT;
  153. udelay(150);
  154. BusyCheck();
  155. LCDWriteData(display.character);
  156. udelay(150);
  157. BusyCheck();
  158. LCDWriteInst(0x10);
  159. break;
  160. }
  161. case LCD_Disp_Left:
  162. udelay(150);
  163. BusyCheck();
  164. LCDWriteInst(0x18);
  165. break;
  166. case LCD_Disp_Right:
  167. udelay(150);
  168. BusyCheck();
  169. LCDWriteInst(0x1C);
  170. break;
  171. case LCD_Home:
  172. udelay(150);
  173. BusyCheck();
  174. LCDWriteInst(0x02);
  175. break;
  176. case LCD_Write:{
  177. struct lcd_display display;
  178. unsigned int index;
  179. if (copy_from_user
  180. (&display, (struct lcd_display *) arg,
  181. sizeof(struct lcd_display)))
  182. return -EFAULT;
  183. udelay(150);
  184. BusyCheck();
  185. LCDWriteInst(0x80);
  186. udelay(150);
  187. BusyCheck();
  188. for (index = 0; index < (display.size1); index++) {
  189. udelay(150);
  190. BusyCheck();
  191. LCDWriteData(display.line1[index]);
  192. BusyCheck();
  193. }
  194. udelay(150);
  195. BusyCheck();
  196. LCDWriteInst(0xC0);
  197. udelay(150);
  198. BusyCheck();
  199. for (index = 0; index < (display.size2); index++) {
  200. udelay(150);
  201. BusyCheck();
  202. LCDWriteData(display.line2[index]);
  203. }
  204. break;
  205. }
  206. case LCD_Read:{
  207. struct lcd_display display;
  208. BusyCheck();
  209. for (address = kDD_R00; address <= kDD_R01;
  210. address++) {
  211. a = (address | kLCD_Addr);
  212. udelay(150);
  213. BusyCheck();
  214. LCDWriteInst(a);
  215. udelay(150);
  216. BusyCheck();
  217. display.line1[address] = LCDReadData;
  218. }
  219. display.line1[0x27] = '\0';
  220. for (address = kDD_R10; address <= kDD_R11;
  221. address++) {
  222. a = (address | kLCD_Addr);
  223. udelay(150);
  224. BusyCheck();
  225. LCDWriteInst(a);
  226. udelay(150);
  227. BusyCheck();
  228. display.line2[address - 0x40] =
  229. LCDReadData;
  230. }
  231. display.line2[0x27] = '\0';
  232. if (copy_to_user
  233. ((struct lcd_display *) arg, &display,
  234. sizeof(struct lcd_display)))
  235. return -EFAULT;
  236. break;
  237. }
  238. // set all GPIO leds to led_display.leds
  239. case LED_Set:{
  240. struct lcd_display led_display;
  241. if (copy_from_user
  242. (&led_display, (struct lcd_display *) arg,
  243. sizeof(struct lcd_display)))
  244. return -EFAULT;
  245. led_state = led_display.leds;
  246. LEDSet(led_state);
  247. break;
  248. }
  249. // set only bit led_display.leds
  250. case LED_Bit_Set:{
  251. unsigned int i;
  252. int bit = 1;
  253. struct lcd_display led_display;
  254. if (copy_from_user
  255. (&led_display, (struct lcd_display *) arg,
  256. sizeof(struct lcd_display)))
  257. return -EFAULT;
  258. for (i = 0; i < (int) led_display.leds; i++) {
  259. bit = 2 * bit;
  260. }
  261. led_state = led_state | bit;
  262. LEDSet(led_state);
  263. break;
  264. }
  265. // clear only bit led_display.leds
  266. case LED_Bit_Clear:{
  267. unsigned int i;
  268. int bit = 1;
  269. struct lcd_display led_display;
  270. if (copy_from_user
  271. (&led_display, (struct lcd_display *) arg,
  272. sizeof(struct lcd_display)))
  273. return -EFAULT;
  274. for (i = 0; i < (int) led_display.leds; i++) {
  275. bit = 2 * bit;
  276. }
  277. led_state = led_state & ~bit;
  278. LEDSet(led_state);
  279. break;
  280. }
  281. case BUTTON_Read:{
  282. button_display.buttons = GPIRead;
  283. if (copy_to_user
  284. ((struct lcd_display *) arg, &button_display,
  285. sizeof(struct lcd_display)))
  286. return -EFAULT;
  287. break;
  288. }
  289. case LINK_Check:{
  290. button_display.buttons =
  291. *((volatile unsigned long *) (0xB0100060));
  292. if (copy_to_user
  293. ((struct lcd_display *) arg, &button_display,
  294. sizeof(struct lcd_display)))
  295. return -EFAULT;
  296. break;
  297. }
  298. case LINK_Check_2:{
  299. int iface_num;
  300. /* panel-utils should pass in the desired interface status is wanted for
  301. * in "buttons" of the structure. We will set this to non-zero if the
  302. * link is in fact up for the requested interface. --DaveM
  303. */
  304. if (copy_from_user
  305. (&button_display, (struct lcd_display *) arg,
  306. sizeof(button_display)))
  307. return -EFAULT;
  308. iface_num = button_display.buttons;
  309. #if defined(CONFIG_TULIP) && 0
  310. if (iface_num >= 0 &&
  311. iface_num < MAX_INTERFACES &&
  312. linkcheck_callbacks[iface_num] != NULL) {
  313. button_display.buttons =
  314. linkcheck_callbacks[iface_num]
  315. (linkcheck_cookies[iface_num]);
  316. } else
  317. #endif
  318. button_display.buttons = 0;
  319. if (__copy_to_user
  320. ((struct lcd_display *) arg, &button_display,
  321. sizeof(struct lcd_display)))
  322. return -EFAULT;
  323. break;
  324. }
  325. default:
  326. return -EINVAL;
  327. }
  328. return 0;
  329. }
  330. static int lcd_open(struct inode *inode, struct file *file)
  331. {
  332. if (!lcd_present)
  333. return -ENXIO;
  334. else
  335. return 0;
  336. }
  337. /* Only RESET or NEXT counts as button pressed */
  338. static inline int button_pressed(void)
  339. {
  340. unsigned long buttons = GPIRead;
  341. if ((buttons == BUTTON_Next) || (buttons == BUTTON_Next_B)
  342. || (buttons == BUTTON_Reset_B))
  343. return buttons;
  344. return 0;
  345. }
  346. /* LED daemon sits on this and we wake him up once a key is pressed. */
  347. static int lcd_waiters = 0;
  348. static ssize_t lcd_read(struct file *file, char *buf,
  349. size_t count, loff_t *ofs)
  350. {
  351. long buttons_now;
  352. if (lcd_waiters > 0)
  353. return -EINVAL;
  354. lcd_waiters++;
  355. while (((buttons_now = (long) button_pressed()) == 0) &&
  356. !(signal_pending(current))) {
  357. msleep_interruptible(2000);
  358. }
  359. lcd_waiters--;
  360. if (signal_pending(current))
  361. return -ERESTARTSYS;
  362. return buttons_now;
  363. }
  364. /*
  365. * The various file operations we support.
  366. */
  367. static const struct file_operations lcd_fops = {
  368. .read = lcd_read,
  369. .ioctl = lcd_ioctl,
  370. .open = lcd_open,
  371. };
  372. static struct miscdevice lcd_dev = {
  373. MISC_DYNAMIC_MINOR,
  374. "lcd",
  375. &lcd_fops
  376. };
  377. static int lcd_init(void)
  378. {
  379. int ret;
  380. unsigned long data;
  381. pr_info("%s\n", LCD_DRIVER);
  382. ret = misc_register(&lcd_dev);
  383. if (ret) {
  384. printk(KERN_WARNING LCD "Unable to register misc device.\n");
  385. return ret;
  386. }
  387. /* Check region? Naaah! Just snarf it up. */
  388. /* request_region(RTC_PORT(0), RTC_IO_EXTENT, "lcd");*/
  389. udelay(150);
  390. data = LCDReadData;
  391. if ((data & 0x000000FF) == (0x00)) {
  392. lcd_present = 0;
  393. pr_info(LCD "LCD Not Present\n");
  394. } else {
  395. lcd_present = 1;
  396. WRITE_GAL(kGal_DevBank2PReg, kGal_DevBank2Cfg);
  397. WRITE_GAL(kGal_DevBank3PReg, kGal_DevBank3Cfg);
  398. }
  399. return 0;
  400. }
  401. static void __exit lcd_exit(void)
  402. {
  403. misc_deregister(&lcd_dev);
  404. }
  405. module_init(lcd_init);
  406. module_exit(lcd_exit);
  407. MODULE_AUTHOR("Andrew Bose");
  408. MODULE_LICENSE("GPL");