simatic-ipc-leds.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Siemens SIMATIC IPC driver for LEDs
  4. *
  5. * Copyright (c) Siemens AG, 2018-2021
  6. *
  7. * Authors:
  8. * Henning Schild <[email protected]>
  9. * Jan Kiszka <[email protected]>
  10. * Gerd Haeussler <[email protected]>
  11. */
  12. #include <linux/ioport.h>
  13. #include <linux/kernel.h>
  14. #include <linux/leds.h>
  15. #include <linux/module.h>
  16. #include <linux/pci.h>
  17. #include <linux/platform_data/x86/simatic-ipc-base.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/sizes.h>
  20. #include <linux/spinlock.h>
  21. #define SIMATIC_IPC_LED_PORT_BASE 0x404E
  22. struct simatic_ipc_led {
  23. unsigned int value; /* mask for io */
  24. char *name;
  25. struct led_classdev cdev;
  26. };
  27. static struct simatic_ipc_led simatic_ipc_leds_io[] = {
  28. {1 << 15, "green:" LED_FUNCTION_STATUS "-1" },
  29. {1 << 7, "yellow:" LED_FUNCTION_STATUS "-1" },
  30. {1 << 14, "red:" LED_FUNCTION_STATUS "-2" },
  31. {1 << 6, "yellow:" LED_FUNCTION_STATUS "-2" },
  32. {1 << 13, "red:" LED_FUNCTION_STATUS "-3" },
  33. {1 << 5, "yellow:" LED_FUNCTION_STATUS "-3" },
  34. { }
  35. };
  36. static struct resource simatic_ipc_led_io_res =
  37. DEFINE_RES_IO_NAMED(SIMATIC_IPC_LED_PORT_BASE, SZ_2, KBUILD_MODNAME);
  38. static DEFINE_SPINLOCK(reg_lock);
  39. static inline struct simatic_ipc_led *cdev_to_led(struct led_classdev *led_cd)
  40. {
  41. return container_of(led_cd, struct simatic_ipc_led, cdev);
  42. }
  43. static void simatic_ipc_led_set_io(struct led_classdev *led_cd,
  44. enum led_brightness brightness)
  45. {
  46. struct simatic_ipc_led *led = cdev_to_led(led_cd);
  47. unsigned long flags;
  48. unsigned int val;
  49. spin_lock_irqsave(&reg_lock, flags);
  50. val = inw(SIMATIC_IPC_LED_PORT_BASE);
  51. if (brightness == LED_OFF)
  52. outw(val | led->value, SIMATIC_IPC_LED_PORT_BASE);
  53. else
  54. outw(val & ~led->value, SIMATIC_IPC_LED_PORT_BASE);
  55. spin_unlock_irqrestore(&reg_lock, flags);
  56. }
  57. static enum led_brightness simatic_ipc_led_get_io(struct led_classdev *led_cd)
  58. {
  59. struct simatic_ipc_led *led = cdev_to_led(led_cd);
  60. return inw(SIMATIC_IPC_LED_PORT_BASE) & led->value ? LED_OFF : led_cd->max_brightness;
  61. }
  62. static int simatic_ipc_leds_probe(struct platform_device *pdev)
  63. {
  64. const struct simatic_ipc_platform *plat = pdev->dev.platform_data;
  65. struct device *dev = &pdev->dev;
  66. struct simatic_ipc_led *ipcled;
  67. struct led_classdev *cdev;
  68. struct resource *res;
  69. int err;
  70. switch (plat->devmode) {
  71. case SIMATIC_IPC_DEVICE_227D:
  72. case SIMATIC_IPC_DEVICE_427E:
  73. res = &simatic_ipc_led_io_res;
  74. ipcled = simatic_ipc_leds_io;
  75. /* on 227D the two bytes work the other way araound */
  76. if (plat->devmode == SIMATIC_IPC_DEVICE_227D) {
  77. while (ipcled->value) {
  78. ipcled->value = swab16(ipcled->value);
  79. ipcled++;
  80. }
  81. ipcled = simatic_ipc_leds_io;
  82. }
  83. if (!devm_request_region(dev, res->start, resource_size(res), KBUILD_MODNAME)) {
  84. dev_err(dev, "Unable to register IO resource at %pR\n", res);
  85. return -EBUSY;
  86. }
  87. break;
  88. default:
  89. return -ENODEV;
  90. }
  91. while (ipcled->value) {
  92. cdev = &ipcled->cdev;
  93. cdev->brightness_set = simatic_ipc_led_set_io;
  94. cdev->brightness_get = simatic_ipc_led_get_io;
  95. cdev->max_brightness = LED_ON;
  96. cdev->name = ipcled->name;
  97. err = devm_led_classdev_register(dev, cdev);
  98. if (err < 0)
  99. return err;
  100. ipcled++;
  101. }
  102. return 0;
  103. }
  104. static struct platform_driver simatic_ipc_led_driver = {
  105. .probe = simatic_ipc_leds_probe,
  106. .driver = {
  107. .name = KBUILD_MODNAME,
  108. }
  109. };
  110. module_platform_driver(simatic_ipc_led_driver);
  111. MODULE_LICENSE("GPL v2");
  112. MODULE_ALIAS("platform:" KBUILD_MODNAME);
  113. MODULE_AUTHOR("Henning Schild <[email protected]>");