Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Álvaro Fernández Rojas | 589fca1 | 2015-05-21 10:11:10 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Driver for BCM6358 memory-mapped LEDs, based on leds-syscon.c |
| 4 | * |
| 5 | * Copyright 2015 Álvaro Fernández Rojas <noltari@gmail.com> |
Álvaro Fernández Rojas | 589fca1 | 2015-05-21 10:11:10 -0700 | [diff] [blame] | 6 | */ |
| 7 | #include <linux/delay.h> |
| 8 | #include <linux/io.h> |
| 9 | #include <linux/leds.h> |
| 10 | #include <linux/module.h> |
| 11 | #include <linux/of.h> |
| 12 | #include <linux/platform_device.h> |
| 13 | #include <linux/spinlock.h> |
| 14 | |
| 15 | #define BCM6358_REG_MODE 0x0 |
| 16 | #define BCM6358_REG_CTRL 0x4 |
| 17 | |
| 18 | #define BCM6358_SLED_CLKDIV_MASK 3 |
| 19 | #define BCM6358_SLED_CLKDIV_1 0 |
| 20 | #define BCM6358_SLED_CLKDIV_2 1 |
| 21 | #define BCM6358_SLED_CLKDIV_4 2 |
| 22 | #define BCM6358_SLED_CLKDIV_8 3 |
| 23 | |
| 24 | #define BCM6358_SLED_POLARITY BIT(2) |
| 25 | #define BCM6358_SLED_BUSY BIT(3) |
| 26 | |
| 27 | #define BCM6358_SLED_MAX_COUNT 32 |
| 28 | #define BCM6358_SLED_WAIT 100 |
| 29 | |
| 30 | /** |
| 31 | * struct bcm6358_led - state container for bcm6358 based LEDs |
| 32 | * @cdev: LED class device for this LED |
| 33 | * @mem: memory resource |
| 34 | * @lock: memory lock |
| 35 | * @pin: LED pin number |
| 36 | * @active_low: LED is active low |
| 37 | */ |
| 38 | struct bcm6358_led { |
| 39 | struct led_classdev cdev; |
| 40 | void __iomem *mem; |
| 41 | spinlock_t *lock; |
| 42 | unsigned long pin; |
| 43 | bool active_low; |
| 44 | }; |
| 45 | |
| 46 | static void bcm6358_led_write(void __iomem *reg, unsigned long data) |
| 47 | { |
Álvaro Fernández Rojas | 4ba113b | 2015-12-15 21:03:50 +0100 | [diff] [blame] | 48 | #ifdef CONFIG_CPU_BIG_ENDIAN |
Álvaro Fernández Rojas | 589fca1 | 2015-05-21 10:11:10 -0700 | [diff] [blame] | 49 | iowrite32be(data, reg); |
Álvaro Fernández Rojas | 4ba113b | 2015-12-15 21:03:50 +0100 | [diff] [blame] | 50 | #else |
| 51 | writel(data, reg); |
| 52 | #endif |
Álvaro Fernández Rojas | 589fca1 | 2015-05-21 10:11:10 -0700 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | static unsigned long bcm6358_led_read(void __iomem *reg) |
| 56 | { |
Álvaro Fernández Rojas | 4ba113b | 2015-12-15 21:03:50 +0100 | [diff] [blame] | 57 | #ifdef CONFIG_CPU_BIG_ENDIAN |
Álvaro Fernández Rojas | 589fca1 | 2015-05-21 10:11:10 -0700 | [diff] [blame] | 58 | return ioread32be(reg); |
Álvaro Fernández Rojas | 4ba113b | 2015-12-15 21:03:50 +0100 | [diff] [blame] | 59 | #else |
| 60 | return readl(reg); |
| 61 | #endif |
Álvaro Fernández Rojas | 589fca1 | 2015-05-21 10:11:10 -0700 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | static unsigned long bcm6358_led_busy(void __iomem *mem) |
| 65 | { |
| 66 | unsigned long val; |
| 67 | |
| 68 | while ((val = bcm6358_led_read(mem + BCM6358_REG_CTRL)) & |
| 69 | BCM6358_SLED_BUSY) |
| 70 | udelay(BCM6358_SLED_WAIT); |
| 71 | |
| 72 | return val; |
| 73 | } |
| 74 | |
Álvaro Fernández Rojas | 6e636a0 | 2015-12-15 21:03:52 +0100 | [diff] [blame] | 75 | static void bcm6358_led_set(struct led_classdev *led_cdev, |
| 76 | enum led_brightness value) |
Álvaro Fernández Rojas | 589fca1 | 2015-05-21 10:11:10 -0700 | [diff] [blame] | 77 | { |
Álvaro Fernández Rojas | 6e636a0 | 2015-12-15 21:03:52 +0100 | [diff] [blame] | 78 | struct bcm6358_led *led = |
| 79 | container_of(led_cdev, struct bcm6358_led, cdev); |
| 80 | unsigned long flags, val; |
Álvaro Fernández Rojas | 589fca1 | 2015-05-21 10:11:10 -0700 | [diff] [blame] | 81 | |
Álvaro Fernández Rojas | 6e636a0 | 2015-12-15 21:03:52 +0100 | [diff] [blame] | 82 | spin_lock_irqsave(led->lock, flags); |
Álvaro Fernández Rojas | 589fca1 | 2015-05-21 10:11:10 -0700 | [diff] [blame] | 83 | bcm6358_led_busy(led->mem); |
Álvaro Fernández Rojas | 589fca1 | 2015-05-21 10:11:10 -0700 | [diff] [blame] | 84 | val = bcm6358_led_read(led->mem + BCM6358_REG_MODE); |
| 85 | if ((led->active_low && value == LED_OFF) || |
| 86 | (!led->active_low && value != LED_OFF)) |
| 87 | val |= BIT(led->pin); |
| 88 | else |
| 89 | val &= ~(BIT(led->pin)); |
| 90 | bcm6358_led_write(led->mem + BCM6358_REG_MODE, val); |
Álvaro Fernández Rojas | 589fca1 | 2015-05-21 10:11:10 -0700 | [diff] [blame] | 91 | spin_unlock_irqrestore(led->lock, flags); |
| 92 | } |
| 93 | |
| 94 | static int bcm6358_led(struct device *dev, struct device_node *nc, u32 reg, |
| 95 | void __iomem *mem, spinlock_t *lock) |
| 96 | { |
| 97 | struct bcm6358_led *led; |
Álvaro Fernández Rojas | 589fca1 | 2015-05-21 10:11:10 -0700 | [diff] [blame] | 98 | const char *state; |
| 99 | int rc; |
| 100 | |
| 101 | led = devm_kzalloc(dev, sizeof(*led), GFP_KERNEL); |
| 102 | if (!led) |
| 103 | return -ENOMEM; |
| 104 | |
| 105 | led->pin = reg; |
| 106 | led->mem = mem; |
| 107 | led->lock = lock; |
| 108 | |
| 109 | if (of_property_read_bool(nc, "active-low")) |
| 110 | led->active_low = true; |
| 111 | |
| 112 | led->cdev.name = of_get_property(nc, "label", NULL) ? : nc->name; |
| 113 | led->cdev.default_trigger = of_get_property(nc, |
| 114 | "linux,default-trigger", |
| 115 | NULL); |
| 116 | |
Álvaro Fernández Rojas | 589fca1 | 2015-05-21 10:11:10 -0700 | [diff] [blame] | 117 | if (!of_property_read_string(nc, "default-state", &state)) { |
| 118 | if (!strcmp(state, "on")) { |
| 119 | led->cdev.brightness = LED_FULL; |
| 120 | } else if (!strcmp(state, "keep")) { |
| 121 | unsigned long val; |
Álvaro Fernández Rojas | 589fca1 | 2015-05-21 10:11:10 -0700 | [diff] [blame] | 122 | val = bcm6358_led_read(led->mem + BCM6358_REG_MODE); |
| 123 | val &= BIT(led->pin); |
| 124 | if ((led->active_low && !val) || |
| 125 | (!led->active_low && val)) |
| 126 | led->cdev.brightness = LED_FULL; |
| 127 | else |
| 128 | led->cdev.brightness = LED_OFF; |
| 129 | } else { |
| 130 | led->cdev.brightness = LED_OFF; |
| 131 | } |
| 132 | } else { |
| 133 | led->cdev.brightness = LED_OFF; |
| 134 | } |
Álvaro Fernández Rojas | 589fca1 | 2015-05-21 10:11:10 -0700 | [diff] [blame] | 135 | |
Álvaro Fernández Rojas | 42273ca | 2015-12-15 21:03:47 +0100 | [diff] [blame] | 136 | bcm6358_led_set(&led->cdev, led->cdev.brightness); |
| 137 | |
Álvaro Fernández Rojas | 589fca1 | 2015-05-21 10:11:10 -0700 | [diff] [blame] | 138 | led->cdev.brightness_set = bcm6358_led_set; |
| 139 | |
| 140 | rc = led_classdev_register(dev, &led->cdev); |
| 141 | if (rc < 0) |
| 142 | return rc; |
| 143 | |
| 144 | dev_dbg(dev, "registered LED %s\n", led->cdev.name); |
| 145 | |
| 146 | return 0; |
| 147 | } |
| 148 | |
| 149 | static int bcm6358_leds_probe(struct platform_device *pdev) |
| 150 | { |
| 151 | struct device *dev = &pdev->dev; |
| 152 | struct device_node *np = pdev->dev.of_node; |
| 153 | struct device_node *child; |
Álvaro Fernández Rojas | 589fca1 | 2015-05-21 10:11:10 -0700 | [diff] [blame] | 154 | void __iomem *mem; |
| 155 | spinlock_t *lock; /* memory lock */ |
| 156 | unsigned long val; |
| 157 | u32 clk_div; |
| 158 | |
Markus Elfring | 8b4423d | 2019-09-20 14:44:06 +0200 | [diff] [blame] | 159 | mem = devm_platform_ioremap_resource(pdev, 0); |
Álvaro Fernández Rojas | 589fca1 | 2015-05-21 10:11:10 -0700 | [diff] [blame] | 160 | if (IS_ERR(mem)) |
| 161 | return PTR_ERR(mem); |
| 162 | |
| 163 | lock = devm_kzalloc(dev, sizeof(*lock), GFP_KERNEL); |
| 164 | if (!lock) |
| 165 | return -ENOMEM; |
| 166 | |
| 167 | spin_lock_init(lock); |
| 168 | |
| 169 | val = bcm6358_led_busy(mem); |
| 170 | val &= ~(BCM6358_SLED_POLARITY | BCM6358_SLED_CLKDIV_MASK); |
| 171 | if (of_property_read_bool(np, "brcm,clk-dat-low")) |
| 172 | val |= BCM6358_SLED_POLARITY; |
| 173 | of_property_read_u32(np, "brcm,clk-div", &clk_div); |
| 174 | switch (clk_div) { |
| 175 | case 8: |
| 176 | val |= BCM6358_SLED_CLKDIV_8; |
| 177 | break; |
| 178 | case 4: |
| 179 | val |= BCM6358_SLED_CLKDIV_4; |
| 180 | break; |
| 181 | case 2: |
| 182 | val |= BCM6358_SLED_CLKDIV_2; |
| 183 | break; |
| 184 | default: |
| 185 | val |= BCM6358_SLED_CLKDIV_1; |
| 186 | break; |
| 187 | } |
| 188 | bcm6358_led_write(mem + BCM6358_REG_CTRL, val); |
| 189 | |
| 190 | for_each_available_child_of_node(np, child) { |
| 191 | int rc; |
| 192 | u32 reg; |
| 193 | |
| 194 | if (of_property_read_u32(child, "reg", ®)) |
| 195 | continue; |
| 196 | |
| 197 | if (reg >= BCM6358_SLED_MAX_COUNT) { |
| 198 | dev_err(dev, "invalid LED (%u >= %d)\n", reg, |
| 199 | BCM6358_SLED_MAX_COUNT); |
| 200 | continue; |
| 201 | } |
| 202 | |
| 203 | rc = bcm6358_led(dev, child, reg, mem, lock); |
Julia Lawall | 4b6ba5e | 2015-10-24 16:42:28 +0200 | [diff] [blame] | 204 | if (rc < 0) { |
| 205 | of_node_put(child); |
Álvaro Fernández Rojas | 589fca1 | 2015-05-21 10:11:10 -0700 | [diff] [blame] | 206 | return rc; |
Julia Lawall | 4b6ba5e | 2015-10-24 16:42:28 +0200 | [diff] [blame] | 207 | } |
Álvaro Fernández Rojas | 589fca1 | 2015-05-21 10:11:10 -0700 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | return 0; |
| 211 | } |
| 212 | |
| 213 | static const struct of_device_id bcm6358_leds_of_match[] = { |
| 214 | { .compatible = "brcm,bcm6358-leds", }, |
| 215 | { }, |
| 216 | }; |
Luis de Bethencourt | 01736f0 | 2015-09-01 23:35:55 +0200 | [diff] [blame] | 217 | MODULE_DEVICE_TABLE(of, bcm6358_leds_of_match); |
Álvaro Fernández Rojas | 589fca1 | 2015-05-21 10:11:10 -0700 | [diff] [blame] | 218 | |
| 219 | static struct platform_driver bcm6358_leds_driver = { |
| 220 | .probe = bcm6358_leds_probe, |
| 221 | .driver = { |
| 222 | .name = "leds-bcm6358", |
| 223 | .of_match_table = bcm6358_leds_of_match, |
| 224 | }, |
| 225 | }; |
| 226 | |
| 227 | module_platform_driver(bcm6358_leds_driver); |
| 228 | |
| 229 | MODULE_AUTHOR("Álvaro Fernández Rojas <noltari@gmail.com>"); |
| 230 | MODULE_DESCRIPTION("LED driver for BCM6358 controllers"); |
| 231 | MODULE_LICENSE("GPL v2"); |
| 232 | MODULE_ALIAS("platform:leds-bcm6358"); |