lcd.c 9.6 KB

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