led.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Chassis LCD/LED driver for HP-PARISC workstations
  4. *
  5. * (c) Copyright 2000 Red Hat Software
  6. * (c) Copyright 2000 Helge Deller <hdeller@redhat.com>
  7. * (c) Copyright 2001 Randolph Chung <tausq@debian.org>
  8. * (c) Copyright 2000-2023 Helge Deller <deller@gmx.de>
  9. *
  10. * The control of the LEDs and LCDs on PARISC machines has to be done
  11. * completely in software.
  12. *
  13. * The LEDs can be configured at runtime in /sys/class/leds/
  14. */
  15. #include <linux/module.h>
  16. #include <linux/init.h>
  17. #include <linux/types.h>
  18. #include <linux/ioport.h>
  19. #include <linux/utsname.h>
  20. #include <linux/capability.h>
  21. #include <linux/delay.h>
  22. #include <linux/reboot.h>
  23. #include <linux/uaccess.h>
  24. #include <linux/leds.h>
  25. #include <linux/platform_device.h>
  26. #include <asm/io.h>
  27. #include <asm/processor.h>
  28. #include <asm/hardware.h>
  29. #include <asm/param.h> /* HZ */
  30. #include <asm/led.h>
  31. #include <asm/pdc.h>
  32. #define LED_HAS_LCD 1
  33. #define LED_HAS_LED 2
  34. static unsigned char led_type; /* bitmask of LED_HAS_XXX */
  35. static unsigned char lastleds; /* LED state from most recent update */
  36. static unsigned char lcd_new_text;
  37. static unsigned char lcd_text[20];
  38. static unsigned char lcd_no_led_support; /* KittyHawk doesn't support LED on its LCD */
  39. struct lcd_block {
  40. unsigned char command; /* stores the command byte */
  41. unsigned char on; /* value for turning LED on */
  42. unsigned char off; /* value for turning LED off */
  43. };
  44. /* Structure returned by PDC_RETURN_CHASSIS_INFO */
  45. /* NOTE: we use unsigned long:16 two times, since the following member
  46. lcd_cmd_reg_addr needs to be 64bit aligned on 64bit PA2.0-machines */
  47. struct pdc_chassis_lcd_info_ret_block {
  48. unsigned long model:16; /* DISPLAY_MODEL_XXXX */
  49. unsigned long lcd_width:16; /* width of the LCD in chars (DISPLAY_MODEL_LCD only) */
  50. unsigned long lcd_cmd_reg_addr; /* ptr to LCD cmd-register & data ptr for LED */
  51. unsigned long lcd_data_reg_addr; /* ptr to LCD data-register (LCD only) */
  52. unsigned int min_cmd_delay; /* delay in uS after cmd-write (LCD only) */
  53. unsigned char reset_cmd1; /* command #1 for writing LCD string (LCD only) */
  54. unsigned char reset_cmd2; /* command #2 for writing LCD string (LCD only) */
  55. unsigned char act_enable; /* 0 = no activity (LCD only) */
  56. struct lcd_block heartbeat;
  57. struct lcd_block disk_io;
  58. struct lcd_block lan_rcv;
  59. struct lcd_block lan_tx;
  60. char _pad;
  61. };
  62. /* LCD_CMD and LCD_DATA for KittyHawk machines */
  63. #define KITTYHAWK_LCD_CMD F_EXTEND(0xf0190000UL)
  64. #define KITTYHAWK_LCD_DATA (KITTYHAWK_LCD_CMD + 1)
  65. /* lcd_info is pre-initialized to the values needed to program KittyHawk LCD's
  66. * HP seems to have used Sharp/Hitachi HD44780 LCDs most of the time. */
  67. static struct pdc_chassis_lcd_info_ret_block
  68. lcd_info __attribute__((aligned(8))) =
  69. {
  70. .model = DISPLAY_MODEL_NONE,
  71. .lcd_width = 16,
  72. .lcd_cmd_reg_addr = KITTYHAWK_LCD_CMD,
  73. .lcd_data_reg_addr = KITTYHAWK_LCD_DATA,
  74. .min_cmd_delay = 80,
  75. .reset_cmd1 = 0x80,
  76. .reset_cmd2 = 0xc0,
  77. };
  78. /* direct access to some of the lcd_info variables */
  79. #define LCD_CMD_REG lcd_info.lcd_cmd_reg_addr
  80. #define LCD_DATA_REG lcd_info.lcd_data_reg_addr
  81. #define LED_DATA_REG lcd_info.lcd_cmd_reg_addr /* LASI & ASP only */
  82. /* ptr to LCD/LED-specific function */
  83. static void (*led_func_ptr) (unsigned char);
  84. static void lcd_print_now(void)
  85. {
  86. int i;
  87. char *str = lcd_text;
  88. if (lcd_info.model != DISPLAY_MODEL_LCD)
  89. return;
  90. if (!lcd_new_text)
  91. return;
  92. lcd_new_text = 0;
  93. /* Set LCD Cursor to 1st character */
  94. gsc_writeb(lcd_info.reset_cmd1, LCD_CMD_REG);
  95. udelay(lcd_info.min_cmd_delay);
  96. /* Print the string */
  97. for (i = 0; i < lcd_info.lcd_width; i++) {
  98. gsc_writeb(*str ? *str++ : ' ', LCD_DATA_REG);
  99. udelay(lcd_info.min_cmd_delay);
  100. }
  101. }
  102. /**
  103. * lcd_print()
  104. *
  105. * @str: string to show on the LCD. If NULL, print current string again.
  106. *
  107. * Displays the given string on the LCD-Display of newer machines.
  108. */
  109. void lcd_print(const char *str)
  110. {
  111. /* copy display string to buffer for procfs */
  112. if (str)
  113. strscpy(lcd_text, str, sizeof(lcd_text));
  114. lcd_new_text = 1;
  115. /* print now if LCD without any LEDs */
  116. if (led_type == LED_HAS_LCD)
  117. lcd_print_now();
  118. }
  119. #define LED_DATA 0x01 /* data to shift (0:on 1:off) */
  120. #define LED_STROBE 0x02 /* strobe to clock data */
  121. /**
  122. * led_ASP_driver() - LED driver for the ASP controller chip
  123. *
  124. * @leds: bitmap representing the LED status
  125. */
  126. static void led_ASP_driver(unsigned char leds)
  127. {
  128. int i;
  129. leds = ~leds;
  130. for (i = 0; i < 8; i++) {
  131. unsigned char value;
  132. value = (leds & 0x80) >> 7;
  133. gsc_writeb( value, LED_DATA_REG );
  134. gsc_writeb( value | LED_STROBE, LED_DATA_REG );
  135. leds <<= 1;
  136. }
  137. }
  138. /**
  139. * led_LASI_driver() - LED driver for the LASI controller chip
  140. *
  141. * @leds: bitmap representing the LED status
  142. */
  143. static void led_LASI_driver(unsigned char leds)
  144. {
  145. leds = ~leds;
  146. gsc_writeb( leds, LED_DATA_REG );
  147. }
  148. /**
  149. * led_LCD_driver() - LED & LCD driver for LCD chips
  150. *
  151. * @leds: bitmap representing the LED status
  152. */
  153. static void led_LCD_driver(unsigned char leds)
  154. {
  155. static const unsigned char mask[4] = {
  156. LED_HEARTBEAT, LED_DISK_IO,
  157. LED_LAN_RCV, LED_LAN_TX };
  158. static struct lcd_block * const blockp[4] = {
  159. &lcd_info.heartbeat,
  160. &lcd_info.disk_io,
  161. &lcd_info.lan_rcv,
  162. &lcd_info.lan_tx
  163. };
  164. static unsigned char latest_leds;
  165. int i;
  166. for (i = 0; i < 4; ++i) {
  167. if ((leds & mask[i]) == (latest_leds & mask[i]))
  168. continue;
  169. gsc_writeb( blockp[i]->command, LCD_CMD_REG );
  170. udelay(lcd_info.min_cmd_delay);
  171. gsc_writeb( leds & mask[i] ? blockp[i]->on :
  172. blockp[i]->off, LCD_DATA_REG );
  173. udelay(lcd_info.min_cmd_delay);
  174. }
  175. latest_leds = leds;
  176. lcd_print_now();
  177. }
  178. /**
  179. * lcd_system_halt()
  180. *
  181. * @nb: pointer to the notifier_block structure
  182. * @event: the event (SYS_RESTART, SYS_HALT or SYS_POWER_OFF)
  183. * @buf: pointer to a buffer (not used)
  184. *
  185. * Called by the reboot notifier chain at shutdown. Stops all
  186. * LED/LCD activities.
  187. */
  188. static int lcd_system_halt(struct notifier_block *nb, unsigned long event, void *buf)
  189. {
  190. const char *txt;
  191. switch (event) {
  192. case SYS_RESTART: txt = "SYSTEM RESTART";
  193. break;
  194. case SYS_HALT: txt = "SYSTEM HALT";
  195. break;
  196. case SYS_POWER_OFF: txt = "SYSTEM POWER OFF";
  197. break;
  198. default: return NOTIFY_DONE;
  199. }
  200. lcd_print(txt);
  201. return NOTIFY_OK;
  202. }
  203. static struct notifier_block lcd_system_halt_notifier = {
  204. .notifier_call = lcd_system_halt,
  205. };
  206. static void set_led(struct led_classdev *led_cdev, enum led_brightness brightness);
  207. struct hppa_led {
  208. struct led_classdev led_cdev;
  209. unsigned char led_bit;
  210. };
  211. #define to_hppa_led(d) container_of(d, struct hppa_led, led_cdev)
  212. typedef void (*set_handler)(struct led_classdev *, enum led_brightness);
  213. struct led_type {
  214. const char *name;
  215. set_handler handler;
  216. const char *default_trigger;
  217. };
  218. #define NUM_LEDS_PER_BOARD 8
  219. struct hppa_drvdata {
  220. struct hppa_led leds[NUM_LEDS_PER_BOARD];
  221. };
  222. static void set_led(struct led_classdev *led_cdev, enum led_brightness brightness)
  223. {
  224. struct hppa_led *p = to_hppa_led(led_cdev);
  225. unsigned char led_bit = p->led_bit;
  226. if (brightness == LED_OFF)
  227. lastleds &= ~led_bit;
  228. else
  229. lastleds |= led_bit;
  230. if (led_func_ptr)
  231. led_func_ptr(lastleds);
  232. }
  233. static int hppa_led_generic_probe(struct platform_device *pdev,
  234. struct led_type *types)
  235. {
  236. struct hppa_drvdata *p;
  237. int i, err;
  238. p = devm_kzalloc(&pdev->dev, sizeof(*p), GFP_KERNEL);
  239. if (!p)
  240. return -ENOMEM;
  241. for (i = 0; i < NUM_LEDS_PER_BOARD; i++) {
  242. struct led_classdev *lp = &p->leds[i].led_cdev;
  243. p->leds[i].led_bit = BIT(i);
  244. lp->name = types[i].name;
  245. lp->brightness = LED_FULL;
  246. lp->brightness_set = types[i].handler;
  247. lp->default_trigger = types[i].default_trigger;
  248. err = led_classdev_register(&pdev->dev, lp);
  249. if (err) {
  250. dev_err(&pdev->dev, "Could not register %s LED\n",
  251. lp->name);
  252. for (i--; i >= 0; i--)
  253. led_classdev_unregister(&p->leds[i].led_cdev);
  254. return err;
  255. }
  256. }
  257. platform_set_drvdata(pdev, p);
  258. return 0;
  259. }
  260. static void platform_led_remove(struct platform_device *pdev)
  261. {
  262. struct hppa_drvdata *p = platform_get_drvdata(pdev);
  263. int i;
  264. for (i = 0; i < NUM_LEDS_PER_BOARD; i++)
  265. led_classdev_unregister(&p->leds[i].led_cdev);
  266. }
  267. static struct led_type mainboard_led_types[NUM_LEDS_PER_BOARD] = {
  268. {
  269. .name = "platform-lan-tx",
  270. .handler = set_led,
  271. .default_trigger = "tx",
  272. },
  273. {
  274. .name = "platform-lan-rx",
  275. .handler = set_led,
  276. .default_trigger = "rx",
  277. },
  278. {
  279. .name = "platform-disk",
  280. .handler = set_led,
  281. .default_trigger = "disk-activity",
  282. },
  283. {
  284. .name = "platform-heartbeat",
  285. .handler = set_led,
  286. .default_trigger = "heartbeat",
  287. },
  288. {
  289. .name = "platform-LED4",
  290. .handler = set_led,
  291. .default_trigger = "panic",
  292. },
  293. {
  294. .name = "platform-LED5",
  295. .handler = set_led,
  296. .default_trigger = "panic",
  297. },
  298. {
  299. .name = "platform-LED6",
  300. .handler = set_led,
  301. .default_trigger = "panic",
  302. },
  303. {
  304. .name = "platform-LED7",
  305. .handler = set_led,
  306. .default_trigger = "panic",
  307. },
  308. };
  309. static int platform_led_probe(struct platform_device *pdev)
  310. {
  311. return hppa_led_generic_probe(pdev, mainboard_led_types);
  312. }
  313. MODULE_ALIAS("platform:platform-leds");
  314. static struct platform_driver hppa_mainboard_led_driver = {
  315. .probe = platform_led_probe,
  316. .remove = platform_led_remove,
  317. .driver = {
  318. .name = "platform-leds",
  319. },
  320. };
  321. static struct platform_driver * const drivers[] = {
  322. &hppa_mainboard_led_driver,
  323. };
  324. static struct platform_device platform_leds = {
  325. .name = "platform-leds",
  326. };
  327. /**
  328. * register_led_driver()
  329. *
  330. * @model: model type, one of the DISPLAY_MODEL_XXXX values
  331. * @cmd_reg: physical address of cmd register for the LED/LCD
  332. * @data_reg: physical address of data register for the LED/LCD
  333. *
  334. * Registers a chassis LED or LCD which should be driven by this driver.
  335. * Only PDC-based, LASI- or ASP-style LEDs and LCDs are supported.
  336. */
  337. int __init register_led_driver(int model, unsigned long cmd_reg, unsigned long data_reg)
  338. {
  339. if (led_func_ptr || !data_reg)
  340. return 1;
  341. /* No LEDs when running in QEMU */
  342. if (running_on_qemu)
  343. return 1;
  344. lcd_info.model = model; /* store the values */
  345. LCD_CMD_REG = (cmd_reg == LED_CMD_REG_NONE) ? 0 : cmd_reg;
  346. switch (lcd_info.model) {
  347. case DISPLAY_MODEL_LCD:
  348. LCD_DATA_REG = data_reg;
  349. pr_info("led: LCD display at %#lx and %#lx\n",
  350. LCD_CMD_REG , LCD_DATA_REG);
  351. led_func_ptr = led_LCD_driver;
  352. if (lcd_no_led_support)
  353. led_type = LED_HAS_LCD;
  354. else
  355. led_type = LED_HAS_LCD | LED_HAS_LED;
  356. break;
  357. case DISPLAY_MODEL_LASI:
  358. LED_DATA_REG = data_reg;
  359. led_func_ptr = led_LASI_driver;
  360. pr_info("led: LED display at %#lx\n", LED_DATA_REG);
  361. led_type = LED_HAS_LED;
  362. break;
  363. case DISPLAY_MODEL_OLD_ASP:
  364. LED_DATA_REG = data_reg;
  365. led_func_ptr = led_ASP_driver;
  366. pr_info("led: LED (ASP-style) display at %#lx\n",
  367. LED_DATA_REG);
  368. led_type = LED_HAS_LED;
  369. break;
  370. default:
  371. pr_err("led: Unknown LCD/LED model type %d\n", lcd_info.model);
  372. return 1;
  373. }
  374. platform_register_drivers(drivers, ARRAY_SIZE(drivers));
  375. return register_reboot_notifier(&lcd_system_halt_notifier);
  376. }
  377. /**
  378. * early_led_init()
  379. *
  380. * early_led_init() is called early in the bootup-process and asks the
  381. * PDC for an usable chassis LCD or LED. If the PDC doesn't return any
  382. * info, then a LED might be detected by the LASI or ASP drivers later.
  383. * KittyHawk machines have often a buggy PDC, so that we explicitly check
  384. * for those machines here.
  385. */
  386. static int __init early_led_init(void)
  387. {
  388. struct pdc_chassis_info chassis_info;
  389. int ret;
  390. scnprintf(lcd_text, sizeof(lcd_text),
  391. "Linux %s", init_utsname()->release);
  392. lcd_new_text = 1;
  393. /* Work around the buggy PDC of KittyHawk-machines */
  394. switch (CPU_HVERSION) {
  395. case 0x580: /* KittyHawk DC2-100 (K100) */
  396. case 0x581: /* KittyHawk DC3-120 (K210) */
  397. case 0x582: /* KittyHawk DC3 100 (K400) */
  398. case 0x583: /* KittyHawk DC3 120 (K410) */
  399. case 0x58B: /* KittyHawk DC2 100 (K200) */
  400. pr_info("LCD on KittyHawk-Machine found.\n");
  401. lcd_info.model = DISPLAY_MODEL_LCD;
  402. /* KittyHawk has no LED support on its LCD, so skip LED detection */
  403. lcd_no_led_support = 1;
  404. goto found; /* use the preinitialized values of lcd_info */
  405. }
  406. /* initialize the struct, so that we can check for valid return values */
  407. chassis_info.actcnt = chassis_info.maxcnt = 0;
  408. ret = pdc_chassis_info(&chassis_info, &lcd_info, sizeof(lcd_info));
  409. if (ret != PDC_OK) {
  410. not_found:
  411. lcd_info.model = DISPLAY_MODEL_NONE;
  412. return 1;
  413. }
  414. /* check the results. Some machines have a buggy PDC */
  415. if (chassis_info.actcnt <= 0 || chassis_info.actcnt != chassis_info.maxcnt)
  416. goto not_found;
  417. switch (lcd_info.model) {
  418. case DISPLAY_MODEL_LCD: /* LCD display */
  419. if (chassis_info.actcnt <
  420. offsetof(struct pdc_chassis_lcd_info_ret_block, _pad)-1)
  421. goto not_found;
  422. if (!lcd_info.act_enable) {
  423. /* PDC tells LCD should not be used. */
  424. goto not_found;
  425. }
  426. break;
  427. case DISPLAY_MODEL_NONE: /* no LED or LCD available */
  428. goto not_found;
  429. case DISPLAY_MODEL_LASI: /* Lasi style 8 bit LED display */
  430. if (chassis_info.actcnt != 8 && chassis_info.actcnt != 32)
  431. goto not_found;
  432. break;
  433. default:
  434. pr_warn("PDC reported unknown LCD/LED model %d\n",
  435. lcd_info.model);
  436. goto not_found;
  437. }
  438. found:
  439. /* register the LCD/LED driver */
  440. return register_led_driver(lcd_info.model, LCD_CMD_REG, LCD_DATA_REG);
  441. }
  442. arch_initcall(early_led_init);
  443. /**
  444. * register_led_regions()
  445. *
  446. * Register_led_regions() registers the LCD/LED regions for /procfs.
  447. * At bootup - where the initialisation of the LCD/LED often happens
  448. * not all internal structures of request_region() are properly set up,
  449. * so that we delay the led-registration until after busdevices_init()
  450. * has been executed.
  451. */
  452. static void __init register_led_regions(void)
  453. {
  454. switch (lcd_info.model) {
  455. case DISPLAY_MODEL_LCD:
  456. request_mem_region((unsigned long)LCD_CMD_REG, 1, "lcd_cmd");
  457. request_mem_region((unsigned long)LCD_DATA_REG, 1, "lcd_data");
  458. break;
  459. case DISPLAY_MODEL_LASI:
  460. case DISPLAY_MODEL_OLD_ASP:
  461. request_mem_region((unsigned long)LED_DATA_REG, 1, "led_data");
  462. break;
  463. }
  464. }
  465. static int __init startup_leds(void)
  466. {
  467. if (platform_device_register(&platform_leds))
  468. printk(KERN_INFO "LED: failed to register LEDs\n");
  469. register_led_regions();
  470. return 0;
  471. }
  472. device_initcall(startup_leds);