| 1 | /* |
| 2 | * QEMU single LED device |
| 3 | * |
| 4 | * Copyright (C) 2020 Philippe Mathieu-Daudé |
| 5 | * |
| 6 | * SPDX-License-Identifier: GPL-2.0-or-later |
| 7 | */ |
| 8 | #ifndef HW_MISC_LED_H |
| 9 | #define HW_MISC_LED_H |
| 10 | |
| 11 | #include "qom/object.h" |
| 12 | #include "hw/core/qdev.h" |
| 13 | |
| 14 | #define TYPE_LED "led" |
| 15 | |
| 16 | /** |
| 17 | * LEDColor: Color of a LED |
| 18 | * |
| 19 | * This set is restricted to physically available LED colors. |
| 20 | * |
| 21 | * LED colors from 'Table 1. Product performance of LUXEON Rebel Color |
| 22 | * Line' of the 'DS68 LUXEON Rebel Color Line' datasheet available at: |
| 23 | * https://www.lumileds.com/products/color-leds/luxeon-rebel-color/ |
| 24 | */ |
| 25 | typedef enum { /* Coarse wavelength range */ |
| 26 | LED_COLOR_VIOLET, /* 425 nm */ |
| 27 | LED_COLOR_BLUE, /* 475 nm */ |
| 28 | LED_COLOR_CYAN, /* 500 nm */ |
| 29 | LED_COLOR_GREEN, /* 535 nm */ |
| 30 | LED_COLOR_YELLOW, /* 567 nm */ |
| 31 | LED_COLOR_AMBER, /* 590 nm */ |
| 32 | LED_COLOR_ORANGE, /* 615 nm */ |
| 33 | LED_COLOR_RED, /* 630 nm */ |
| 34 | } LEDColor; |
| 35 | |
| 36 | struct LEDState { |
| 37 | /* Private */ |
| 38 | DeviceState parent_obj; |
| 39 | /* Public */ |
| 40 | |
| 41 | uint8_t intensity_percent; |
| 42 | qemu_irq irq; |
| 43 | |
| 44 | /* Properties */ |
| 45 | char *description; |
| 46 | char *color; |
| 47 | /* |
| 48 | * Determines whether a GPIO is using a positive (active-high) |
| 49 | * logic (when used with GPIO, the intensity at reset is related |
| 50 | * to the GPIO polarity). |
| 51 | */ |
| 52 | bool gpio_active_high; |
| 53 | }; |
| 54 | typedef struct LEDState LEDState; |
| 55 | DECLARE_INSTANCE_CHECKER(LEDState, LED, TYPE_LED) |
| 56 | |
| 57 | /** |
| 58 | * led_set_intensity: Set the intensity of a LED device |
| 59 | * @s: the LED object |
| 60 | * @intensity_percent: intensity as percentage in range 0 to 100. |
| 61 | */ |
| 62 | void led_set_intensity(LEDState *s, unsigned intensity_percent); |
| 63 | |
| 64 | /** |
| 65 | * led_get_intensity: |
| 66 | * @s: the LED object |
| 67 | * |
| 68 | * Returns: The LED intensity as percentage in range 0 to 100. |
| 69 | */ |
| 70 | unsigned led_get_intensity(LEDState *s); |
| 71 | |
| 72 | /** |
| 73 | * led_set_state: Set the state of a LED device |
| 74 | * @s: the LED object |
| 75 | * @is_emitting: boolean indicating whether the LED is emitting |
| 76 | * |
| 77 | * This utility is meant for LED connected to GPIO. |
| 78 | */ |
| 79 | void led_set_state(LEDState *s, bool is_emitting); |
| 80 | |
| 81 | /** |
| 82 | * led_create_simple: Create and realize a LED device |
| 83 | * @parentobj: the parent object |
| 84 | * @gpio_polarity: GPIO polarity |
| 85 | * @color: color of the LED |
| 86 | * @description: description of the LED (optional) |
| 87 | * |
| 88 | * Create the device state structure, initialize it, and |
| 89 | * drop the reference to it (the device is realized). |
| 90 | * |
| 91 | * Returns: The newly allocated and instantiated LED object. |
| 92 | */ |
| 93 | LEDState *led_create_simple(Object *parentobj, |
| 94 | GpioPolarity gpio_polarity, |
| 95 | LEDColor color, |
| 96 | const char *description); |
| 97 | |
| 98 | #endif /* HW_MISC_LED_H */ |