^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) // Copyright (c) 2019 Crane Merchandising Systems. All rights reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) // Copyright (C) 2019 Oleh Kravchenko <oleg@kaa.org.ua>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <linux/leds.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/of_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/spi/spi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) * EL15203000 SPI protocol description:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * +-----+---------+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) * | LED | COMMAND |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) * +-----+---------+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) * | 1 | 1 |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) * +-----+---------+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) * (*) LEDs MCU board expects 20 msec delay per byte.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) * LEDs:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) * +----------+--------------+-------------------------------------------+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) * | ID | NAME | DESCRIPTION |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) * +----------+--------------+-------------------------------------------+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) * | 'P' 0x50 | Pipe | Consists from 5 LEDs, controlled by board |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) * +----------+--------------+-------------------------------------------+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) * | 'S' 0x53 | Screen frame | Light tube around the screen |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) * +----------+--------------+-------------------------------------------+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) * | 'V' 0x56 | Vending area | Highlights a cup of coffee |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) * +----------+--------------+-------------------------------------------+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) * COMMAND:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) * +----------+-----------------+--------------+--------------+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) * | VALUES | PIPE | SCREEN FRAME | VENDING AREA |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) * +----------+-----------------+--------------+--------------+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) * | '0' 0x30 | Off |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) * +----------+-----------------------------------------------+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) * | '1' 0x31 | On |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) * +----------+-----------------+--------------+--------------+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) * | '2' 0x32 | Cascade | Breathing |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) * +----------+-----------------+--------------+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) * | '3' 0x33 | Inverse cascade |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) * +----------+-----------------+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) * | '4' 0x34 | Bounce |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) * +----------+-----------------+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) * | '5' 0x35 | Inverse bounce |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) * +----------+-----------------+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) /* EL15203000 default settings */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) #define EL_FW_DELAY_USEC 20000ul
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) #define EL_PATTERN_DELAY_MSEC 800u
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) #define EL_PATTERN_LEN 10u
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) #define EL_PATTERN_HALF_LEN (EL_PATTERN_LEN / 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) enum el15203000_command {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) /* for all LEDs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) EL_OFF = '0',
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) EL_ON = '1',
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) /* for Screen LED */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) EL_SCREEN_BREATHING = '2',
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) /* for Pipe LED */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) EL_PIPE_CASCADE = '2',
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) EL_PIPE_INV_CASCADE = '3',
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) EL_PIPE_BOUNCE = '4',
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) EL_PIPE_INV_BOUNCE = '5',
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) struct el15203000_led {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) struct el15203000 *priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) struct led_classdev ldev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) u32 reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) struct el15203000 {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) struct device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) struct mutex lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) struct spi_device *spi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) unsigned long delay;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) size_t count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) struct el15203000_led leds[];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) static int el15203000_cmd(struct el15203000_led *led, u8 brightness)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) u8 cmd[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) size_t i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) mutex_lock(&led->priv->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) dev_dbg(led->priv->dev, "Set brightness of 0x%02x(%c) to 0x%02x(%c)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) led->reg, led->reg, brightness, brightness);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) /* to avoid SPI mistiming with firmware we should wait some time */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) if (time_after(led->priv->delay, jiffies)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) dev_dbg(led->priv->dev, "Wait %luus to sync",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) EL_FW_DELAY_USEC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) usleep_range(EL_FW_DELAY_USEC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) EL_FW_DELAY_USEC + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) cmd[0] = led->reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) cmd[1] = brightness;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) for (i = 0; i < ARRAY_SIZE(cmd); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) if (i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) usleep_range(EL_FW_DELAY_USEC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) EL_FW_DELAY_USEC + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) ret = spi_write(led->priv->spi, &cmd[i], sizeof(cmd[i]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) dev_err(led->priv->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) "spi_write() error %d", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) led->priv->delay = jiffies + usecs_to_jiffies(EL_FW_DELAY_USEC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) mutex_unlock(&led->priv->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) static int el15203000_set_blocking(struct led_classdev *ldev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) enum led_brightness brightness)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) struct el15203000_led *led = container_of(ldev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) struct el15203000_led,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) ldev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) return el15203000_cmd(led, brightness == LED_OFF ? EL_OFF : EL_ON);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) static int el15203000_pattern_set_S(struct led_classdev *ldev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) struct led_pattern *pattern,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) u32 len, int repeat)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) struct el15203000_led *led = container_of(ldev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) struct el15203000_led,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) ldev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) if (repeat > 0 || len != 2 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) pattern[0].delta_t != 4000 || pattern[0].brightness != 0 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) pattern[1].delta_t != 4000 || pattern[1].brightness != 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) dev_dbg(led->priv->dev, "Breathing mode for 0x%02x(%c)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) led->reg, led->reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) return el15203000_cmd(led, EL_SCREEN_BREATHING);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) static bool is_cascade(const struct led_pattern *pattern, u32 len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) bool inv, bool right)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) int val, t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) u32 i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) if (len != EL_PATTERN_HALF_LEN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) val = right ? BIT(4) : BIT(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) for (i = 0; i < len; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) t = inv ? ~val & GENMASK(4, 0) : val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) if (pattern[i].delta_t != EL_PATTERN_DELAY_MSEC ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) pattern[i].brightness != t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) val = right ? val >> 1 : val << 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) static bool is_bounce(const struct led_pattern *pattern, u32 len, bool inv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) if (len != EL_PATTERN_LEN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) return is_cascade(pattern, EL_PATTERN_HALF_LEN, inv, false) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) is_cascade(pattern + EL_PATTERN_HALF_LEN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) EL_PATTERN_HALF_LEN, inv, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) static int el15203000_pattern_set_P(struct led_classdev *ldev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) struct led_pattern *pattern,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) u32 len, int repeat)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) u8 cmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) struct el15203000_led *led = container_of(ldev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) struct el15203000_led,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) ldev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) if (repeat > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) if (is_cascade(pattern, len, false, false)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) dev_dbg(led->priv->dev, "Cascade mode for 0x%02x(%c)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) led->reg, led->reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) cmd = EL_PIPE_CASCADE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) } else if (is_cascade(pattern, len, true, false)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) dev_dbg(led->priv->dev, "Inverse cascade mode for 0x%02x(%c)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) led->reg, led->reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) cmd = EL_PIPE_INV_CASCADE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) } else if (is_bounce(pattern, len, false)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) dev_dbg(led->priv->dev, "Bounce mode for 0x%02x(%c)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) led->reg, led->reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) cmd = EL_PIPE_BOUNCE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) } else if (is_bounce(pattern, len, true)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) dev_dbg(led->priv->dev, "Inverse bounce mode for 0x%02x(%c)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) led->reg, led->reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) cmd = EL_PIPE_INV_BOUNCE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) dev_err(led->priv->dev, "Invalid hw_pattern for 0x%02x(%c)!",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) led->reg, led->reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) return el15203000_cmd(led, cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) static int el15203000_pattern_clear(struct led_classdev *ldev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) struct el15203000_led *led = container_of(ldev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) struct el15203000_led,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) ldev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) return el15203000_cmd(led, EL_OFF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) static int el15203000_probe_dt(struct el15203000 *priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) struct el15203000_led *led = priv->leds;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) struct fwnode_handle *child;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) device_for_each_child_node(priv->dev, child) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) struct led_init_data init_data = {};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) ret = fwnode_property_read_u32(child, "reg", &led->reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) dev_err(priv->dev, "LED without ID number");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) fwnode_handle_put(child);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) if (led->reg > U8_MAX) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) dev_err(priv->dev, "LED value %d is invalid", led->reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) fwnode_handle_put(child);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) led->priv = priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) led->ldev.max_brightness = LED_ON;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) led->ldev.brightness_set_blocking = el15203000_set_blocking;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) if (led->reg == 'S') {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) led->ldev.pattern_set = el15203000_pattern_set_S;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) led->ldev.pattern_clear = el15203000_pattern_clear;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) } else if (led->reg == 'P') {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) led->ldev.pattern_set = el15203000_pattern_set_P;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) led->ldev.pattern_clear = el15203000_pattern_clear;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) init_data.fwnode = child;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) ret = devm_led_classdev_register_ext(priv->dev, &led->ldev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) &init_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) dev_err(priv->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) "failed to register LED device %s, err %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) led->ldev.name, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) fwnode_handle_put(child);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) led++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) static int el15203000_probe(struct spi_device *spi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) struct el15203000 *priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) size_t count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) count = device_get_child_node_count(&spi->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) if (!count) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) dev_err(&spi->dev, "LEDs are not defined in device tree!");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) priv = devm_kzalloc(&spi->dev, struct_size(priv, leds, count),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) if (!priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) mutex_init(&priv->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) priv->count = count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) priv->dev = &spi->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) priv->spi = spi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) priv->delay = jiffies -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) usecs_to_jiffies(EL_FW_DELAY_USEC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) spi_set_drvdata(spi, priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) return el15203000_probe_dt(priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) static int el15203000_remove(struct spi_device *spi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) struct el15203000 *priv = spi_get_drvdata(spi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) mutex_destroy(&priv->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) static const struct of_device_id el15203000_dt_ids[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) { .compatible = "crane,el15203000", },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) {},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) MODULE_DEVICE_TABLE(of, el15203000_dt_ids);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) static struct spi_driver el15203000_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) .probe = el15203000_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) .remove = el15203000_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) .driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) .name = KBUILD_MODNAME,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) .of_match_table = el15203000_dt_ids,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) module_spi_driver(el15203000_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) MODULE_AUTHOR("Oleh Kravchenko <oleg@kaa.org.ua>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) MODULE_DESCRIPTION("el15203000 LED driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) MODULE_LICENSE("GPL v2");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) MODULE_ALIAS("spi:el15203000");