lcd.c 9.7 KB

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