simatic-ipc-leds-gpio.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Siemens SIMATIC IPC driver for GPIO based LEDs
  4. *
  5. * Copyright (c) Siemens AG, 2022
  6. *
  7. * Authors:
  8. * Henning Schild <[email protected]>
  9. */
  10. #include <linux/gpio/machine.h>
  11. #include <linux/gpio/consumer.h>
  12. #include <linux/leds.h>
  13. #include <linux/module.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/platform_data/x86/simatic-ipc-base.h>
  16. static struct gpiod_lookup_table *simatic_ipc_led_gpio_table;
  17. static struct gpiod_lookup_table simatic_ipc_led_gpio_table_127e = {
  18. .dev_id = "leds-gpio",
  19. .table = {
  20. GPIO_LOOKUP_IDX("apollolake-pinctrl.0", 52, NULL, 0, GPIO_ACTIVE_LOW),
  21. GPIO_LOOKUP_IDX("apollolake-pinctrl.0", 53, NULL, 1, GPIO_ACTIVE_LOW),
  22. GPIO_LOOKUP_IDX("apollolake-pinctrl.0", 57, NULL, 2, GPIO_ACTIVE_LOW),
  23. GPIO_LOOKUP_IDX("apollolake-pinctrl.0", 58, NULL, 3, GPIO_ACTIVE_LOW),
  24. GPIO_LOOKUP_IDX("apollolake-pinctrl.0", 60, NULL, 4, GPIO_ACTIVE_LOW),
  25. GPIO_LOOKUP_IDX("apollolake-pinctrl.0", 51, NULL, 5, GPIO_ACTIVE_LOW),
  26. GPIO_LOOKUP_IDX("apollolake-pinctrl.0", 56, NULL, 6, GPIO_ACTIVE_LOW),
  27. GPIO_LOOKUP_IDX("apollolake-pinctrl.0", 59, NULL, 7, GPIO_ACTIVE_HIGH),
  28. },
  29. };
  30. static struct gpiod_lookup_table simatic_ipc_led_gpio_table_227g = {
  31. .dev_id = "leds-gpio",
  32. .table = {
  33. GPIO_LOOKUP_IDX("gpio-f7188x-2", 0, NULL, 0, GPIO_ACTIVE_LOW),
  34. GPIO_LOOKUP_IDX("gpio-f7188x-2", 1, NULL, 1, GPIO_ACTIVE_LOW),
  35. GPIO_LOOKUP_IDX("gpio-f7188x-2", 2, NULL, 2, GPIO_ACTIVE_LOW),
  36. GPIO_LOOKUP_IDX("gpio-f7188x-2", 3, NULL, 3, GPIO_ACTIVE_LOW),
  37. GPIO_LOOKUP_IDX("gpio-f7188x-2", 4, NULL, 4, GPIO_ACTIVE_LOW),
  38. GPIO_LOOKUP_IDX("gpio-f7188x-2", 5, NULL, 5, GPIO_ACTIVE_LOW),
  39. GPIO_LOOKUP_IDX("gpio-f7188x-3", 6, NULL, 6, GPIO_ACTIVE_HIGH),
  40. GPIO_LOOKUP_IDX("gpio-f7188x-3", 7, NULL, 7, GPIO_ACTIVE_HIGH),
  41. }
  42. };
  43. static const struct gpio_led simatic_ipc_gpio_leds[] = {
  44. { .name = "red:" LED_FUNCTION_STATUS "-1" },
  45. { .name = "green:" LED_FUNCTION_STATUS "-1" },
  46. { .name = "red:" LED_FUNCTION_STATUS "-2" },
  47. { .name = "green:" LED_FUNCTION_STATUS "-2" },
  48. { .name = "red:" LED_FUNCTION_STATUS "-3" },
  49. { .name = "green:" LED_FUNCTION_STATUS "-3" },
  50. };
  51. static const struct gpio_led_platform_data simatic_ipc_gpio_leds_pdata = {
  52. .num_leds = ARRAY_SIZE(simatic_ipc_gpio_leds),
  53. .leds = simatic_ipc_gpio_leds,
  54. };
  55. static struct platform_device *simatic_leds_pdev;
  56. static int simatic_ipc_leds_gpio_remove(struct platform_device *pdev)
  57. {
  58. gpiod_remove_lookup_table(simatic_ipc_led_gpio_table);
  59. platform_device_unregister(simatic_leds_pdev);
  60. return 0;
  61. }
  62. static int simatic_ipc_leds_gpio_probe(struct platform_device *pdev)
  63. {
  64. const struct simatic_ipc_platform *plat = pdev->dev.platform_data;
  65. struct gpio_desc *gpiod;
  66. int err;
  67. switch (plat->devmode) {
  68. case SIMATIC_IPC_DEVICE_127E:
  69. if (!IS_ENABLED(CONFIG_PINCTRL_BROXTON))
  70. return -ENODEV;
  71. simatic_ipc_led_gpio_table = &simatic_ipc_led_gpio_table_127e;
  72. break;
  73. case SIMATIC_IPC_DEVICE_227G:
  74. if (!IS_ENABLED(CONFIG_GPIO_F7188X))
  75. return -ENODEV;
  76. request_module("gpio-f7188x");
  77. simatic_ipc_led_gpio_table = &simatic_ipc_led_gpio_table_227g;
  78. break;
  79. default:
  80. return -ENODEV;
  81. }
  82. gpiod_add_lookup_table(simatic_ipc_led_gpio_table);
  83. simatic_leds_pdev = platform_device_register_resndata(NULL,
  84. "leds-gpio", PLATFORM_DEVID_NONE, NULL, 0,
  85. &simatic_ipc_gpio_leds_pdata,
  86. sizeof(simatic_ipc_gpio_leds_pdata));
  87. if (IS_ERR(simatic_leds_pdev)) {
  88. err = PTR_ERR(simatic_leds_pdev);
  89. goto out;
  90. }
  91. /* PM_BIOS_BOOT_N */
  92. gpiod = gpiod_get_index(&simatic_leds_pdev->dev, NULL, 6, GPIOD_OUT_LOW);
  93. if (IS_ERR(gpiod)) {
  94. err = PTR_ERR(gpiod);
  95. goto out;
  96. }
  97. gpiod_put(gpiod);
  98. /* PM_WDT_OUT */
  99. gpiod = gpiod_get_index(&simatic_leds_pdev->dev, NULL, 7, GPIOD_OUT_LOW);
  100. if (IS_ERR(gpiod)) {
  101. err = PTR_ERR(gpiod);
  102. goto out;
  103. }
  104. gpiod_put(gpiod);
  105. return 0;
  106. out:
  107. simatic_ipc_leds_gpio_remove(pdev);
  108. return err;
  109. }
  110. static struct platform_driver simatic_ipc_led_gpio_driver = {
  111. .probe = simatic_ipc_leds_gpio_probe,
  112. .remove = simatic_ipc_leds_gpio_remove,
  113. .driver = {
  114. .name = KBUILD_MODNAME,
  115. }
  116. };
  117. module_platform_driver(simatic_ipc_led_gpio_driver);
  118. MODULE_LICENSE("GPL v2");
  119. MODULE_ALIAS("platform:" KBUILD_MODNAME);
  120. MODULE_SOFTDEP("pre: platform:leds-gpio");
  121. MODULE_AUTHOR("Henning Schild <[email protected]>");