led.c 14 KB

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