Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Siemens SIMATIC IPC driver for LEDs
0004  *
0005  * Copyright (c) Siemens AG, 2018-2021
0006  *
0007  * Authors:
0008  *  Henning Schild <henning.schild@siemens.com>
0009  *  Jan Kiszka <jan.kiszka@siemens.com>
0010  *  Gerd Haeussler <gerd.haeussler.ext@siemens.com>
0011  */
0012 
0013 #include <linux/ioport.h>
0014 #include <linux/kernel.h>
0015 #include <linux/leds.h>
0016 #include <linux/module.h>
0017 #include <linux/pci.h>
0018 #include <linux/platform_data/x86/simatic-ipc-base.h>
0019 #include <linux/platform_device.h>
0020 #include <linux/sizes.h>
0021 #include <linux/spinlock.h>
0022 
0023 #define SIMATIC_IPC_LED_PORT_BASE   0x404E
0024 
0025 struct simatic_ipc_led {
0026     unsigned int value; /* mask for io */
0027     char *name;
0028     struct led_classdev cdev;
0029 };
0030 
0031 static struct simatic_ipc_led simatic_ipc_leds_io[] = {
0032     {1 << 15, "green:" LED_FUNCTION_STATUS "-1" },
0033     {1 << 7,  "yellow:" LED_FUNCTION_STATUS "-1" },
0034     {1 << 14, "red:" LED_FUNCTION_STATUS "-2" },
0035     {1 << 6,  "yellow:" LED_FUNCTION_STATUS "-2" },
0036     {1 << 13, "red:" LED_FUNCTION_STATUS "-3" },
0037     {1 << 5,  "yellow:" LED_FUNCTION_STATUS "-3" },
0038     { }
0039 };
0040 
0041 static struct resource simatic_ipc_led_io_res =
0042     DEFINE_RES_IO_NAMED(SIMATIC_IPC_LED_PORT_BASE, SZ_2, KBUILD_MODNAME);
0043 
0044 static DEFINE_SPINLOCK(reg_lock);
0045 
0046 static inline struct simatic_ipc_led *cdev_to_led(struct led_classdev *led_cd)
0047 {
0048     return container_of(led_cd, struct simatic_ipc_led, cdev);
0049 }
0050 
0051 static void simatic_ipc_led_set_io(struct led_classdev *led_cd,
0052                    enum led_brightness brightness)
0053 {
0054     struct simatic_ipc_led *led = cdev_to_led(led_cd);
0055     unsigned long flags;
0056     unsigned int val;
0057 
0058     spin_lock_irqsave(&reg_lock, flags);
0059 
0060     val = inw(SIMATIC_IPC_LED_PORT_BASE);
0061     if (brightness == LED_OFF)
0062         outw(val | led->value, SIMATIC_IPC_LED_PORT_BASE);
0063     else
0064         outw(val & ~led->value, SIMATIC_IPC_LED_PORT_BASE);
0065 
0066     spin_unlock_irqrestore(&reg_lock, flags);
0067 }
0068 
0069 static enum led_brightness simatic_ipc_led_get_io(struct led_classdev *led_cd)
0070 {
0071     struct simatic_ipc_led *led = cdev_to_led(led_cd);
0072 
0073     return inw(SIMATIC_IPC_LED_PORT_BASE) & led->value ? LED_OFF : led_cd->max_brightness;
0074 }
0075 
0076 static int simatic_ipc_leds_probe(struct platform_device *pdev)
0077 {
0078     const struct simatic_ipc_platform *plat = pdev->dev.platform_data;
0079     struct device *dev = &pdev->dev;
0080     struct simatic_ipc_led *ipcled;
0081     struct led_classdev *cdev;
0082     struct resource *res;
0083     int err;
0084 
0085     switch (plat->devmode) {
0086     case SIMATIC_IPC_DEVICE_227D:
0087     case SIMATIC_IPC_DEVICE_427E:
0088         res = &simatic_ipc_led_io_res;
0089         ipcled = simatic_ipc_leds_io;
0090         /* on 227D the two bytes work the other way araound */
0091         if (plat->devmode == SIMATIC_IPC_DEVICE_227D) {
0092             while (ipcled->value) {
0093                 ipcled->value = swab16(ipcled->value);
0094                 ipcled++;
0095             }
0096             ipcled = simatic_ipc_leds_io;
0097         }
0098         if (!devm_request_region(dev, res->start, resource_size(res), KBUILD_MODNAME)) {
0099             dev_err(dev, "Unable to register IO resource at %pR\n", res);
0100             return -EBUSY;
0101         }
0102         break;
0103     default:
0104         return -ENODEV;
0105     }
0106 
0107     while (ipcled->value) {
0108         cdev = &ipcled->cdev;
0109         cdev->brightness_set = simatic_ipc_led_set_io;
0110         cdev->brightness_get = simatic_ipc_led_get_io;
0111         cdev->max_brightness = LED_ON;
0112         cdev->name = ipcled->name;
0113 
0114         err = devm_led_classdev_register(dev, cdev);
0115         if (err < 0)
0116             return err;
0117         ipcled++;
0118     }
0119 
0120     return 0;
0121 }
0122 
0123 static struct platform_driver simatic_ipc_led_driver = {
0124     .probe = simatic_ipc_leds_probe,
0125     .driver = {
0126         .name = KBUILD_MODNAME,
0127     }
0128 };
0129 
0130 module_platform_driver(simatic_ipc_led_driver);
0131 
0132 MODULE_LICENSE("GPL v2");
0133 MODULE_ALIAS("platform:" KBUILD_MODNAME);
0134 MODULE_AUTHOR("Henning Schild <henning.schild@siemens.com>");