1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * leds-ns2.c - Driver for the Network Space v2 (and parents) dual-GPIO LED 4 * 5 * Copyright (C) 2010 LaCie 6 * 7 * Author: Simon Guinot <sguinot@lacie.com> 8 * 9 * Based on leds-gpio.c by Raphael Assenat <raph@8d.com> 10 */ 11 12 #include <linux/kernel.h> 13 #include <linux/platform_device.h> 14 #include <linux/slab.h> 15 #include <linux/gpio.h> 16 #include <linux/leds.h> 17 #include <linux/module.h> 18 #include <linux/platform_data/leds-kirkwood-ns2.h> 19 #include <linux/of.h> 20 #include <linux/of_gpio.h> 21 #include "leds.h" 22 23 /* 24 * The Network Space v2 dual-GPIO LED is wired to a CPLD. Three different LED 25 * modes are available: off, on and SATA activity blinking. The LED modes are 26 * controlled through two GPIOs (command and slow): each combination of values 27 * for the command/slow GPIOs corresponds to a LED mode. 28 */ 29 30 struct ns2_led_data { 31 struct led_classdev cdev; 32 unsigned int cmd; 33 unsigned int slow; 34 bool can_sleep; 35 unsigned char sata; /* True when SATA mode active. */ 36 rwlock_t rw_lock; /* Lock GPIOs. */ 37 int num_modes; 38 struct ns2_led_modval *modval; 39 }; 40 41 static int ns2_led_get_mode(struct ns2_led_data *led_dat, 42 enum ns2_led_modes *mode) 43 { 44 int i; 45 int ret = -EINVAL; 46 int cmd_level; 47 int slow_level; 48 49 cmd_level = gpio_get_value_cansleep(led_dat->cmd); 50 slow_level = gpio_get_value_cansleep(led_dat->slow); 51 52 for (i = 0; i < led_dat->num_modes; i++) { 53 if (cmd_level == led_dat->modval[i].cmd_level && 54 slow_level == led_dat->modval[i].slow_level) { 55 *mode = led_dat->modval[i].mode; 56 ret = 0; 57 break; 58 } 59 } 60 61 return ret; 62 } 63 64 static void ns2_led_set_mode(struct ns2_led_data *led_dat, 65 enum ns2_led_modes mode) 66 { 67 int i; 68 bool found = false; 69 unsigned long flags; 70 71 for (i = 0; i < led_dat->num_modes; i++) 72 if (mode == led_dat->modval[i].mode) { 73 found = true; 74 break; 75 } 76 77 if (!found) 78 return; 79 80 write_lock_irqsave(&led_dat->rw_lock, flags); 81 82 if (!led_dat->can_sleep) { 83 gpio_set_value(led_dat->cmd, 84 led_dat->modval[i].cmd_level); 85 gpio_set_value(led_dat->slow, 86 led_dat->modval[i].slow_level); 87 goto exit_unlock; 88 } 89 90 gpio_set_value_cansleep(led_dat->cmd, led_dat->modval[i].cmd_level); 91 gpio_set_value_cansleep(led_dat->slow, led_dat->modval[i].slow_level); 92 93 exit_unlock: 94 write_unlock_irqrestore(&led_dat->rw_lock, flags); 95 } 96 97 static void ns2_led_set(struct led_classdev *led_cdev, 98 enum led_brightness value) 99 { 100 struct ns2_led_data *led_dat = 101 container_of(led_cdev, struct ns2_led_data, cdev); 102 enum ns2_led_modes mode; 103 104 if (value == LED_OFF) 105 mode = NS_V2_LED_OFF; 106 else if (led_dat->sata) 107 mode = NS_V2_LED_SATA; 108 else 109 mode = NS_V2_LED_ON; 110 111 ns2_led_set_mode(led_dat, mode); 112 } 113 114 static int ns2_led_set_blocking(struct led_classdev *led_cdev, 115 enum led_brightness value) 116 { 117 ns2_led_set(led_cdev, value); 118 return 0; 119 } 120 121 static ssize_t ns2_led_sata_store(struct device *dev, 122 struct device_attribute *attr, 123 const char *buff, size_t count) 124 { 125 struct led_classdev *led_cdev = dev_get_drvdata(dev); 126 struct ns2_led_data *led_dat = 127 container_of(led_cdev, struct ns2_led_data, cdev); 128 int ret; 129 unsigned long enable; 130 131 ret = kstrtoul(buff, 10, &enable); 132 if (ret < 0) 133 return ret; 134 135 enable = !!enable; 136 137 if (led_dat->sata == enable) 138 goto exit; 139 140 led_dat->sata = enable; 141 142 if (!led_get_brightness(led_cdev)) 143 goto exit; 144 145 if (enable) 146 ns2_led_set_mode(led_dat, NS_V2_LED_SATA); 147 else 148 ns2_led_set_mode(led_dat, NS_V2_LED_ON); 149 150 exit: 151 return count; 152 } 153 154 static ssize_t ns2_led_sata_show(struct device *dev, 155 struct device_attribute *attr, char *buf) 156 { 157 struct led_classdev *led_cdev = dev_get_drvdata(dev); 158 struct ns2_led_data *led_dat = 159 container_of(led_cdev, struct ns2_led_data, cdev); 160 161 return sprintf(buf, "%d\n", led_dat->sata); 162 } 163 164 static DEVICE_ATTR(sata, 0644, ns2_led_sata_show, ns2_led_sata_store); 165 166 static struct attribute *ns2_led_attrs[] = { 167 &dev_attr_sata.attr, 168 NULL 169 }; 170 ATTRIBUTE_GROUPS(ns2_led); 171 172 static int 173 create_ns2_led(struct platform_device *pdev, struct ns2_led_data *led_dat, 174 const struct ns2_led *template) 175 { 176 int ret; 177 enum ns2_led_modes mode; 178 179 ret = devm_gpio_request_one(&pdev->dev, template->cmd, 180 gpio_get_value_cansleep(template->cmd) ? 181 GPIOF_OUT_INIT_HIGH : GPIOF_OUT_INIT_LOW, 182 template->name); 183 if (ret) { 184 dev_err(&pdev->dev, "%s: failed to setup command GPIO\n", 185 template->name); 186 return ret; 187 } 188 189 ret = devm_gpio_request_one(&pdev->dev, template->slow, 190 gpio_get_value_cansleep(template->slow) ? 191 GPIOF_OUT_INIT_HIGH : GPIOF_OUT_INIT_LOW, 192 template->name); 193 if (ret) { 194 dev_err(&pdev->dev, "%s: failed to setup slow GPIO\n", 195 template->name); 196 return ret; 197 } 198 199 rwlock_init(&led_dat->rw_lock); 200 201 led_dat->cdev.name = template->name; 202 led_dat->cdev.default_trigger = template->default_trigger; 203 led_dat->cdev.blink_set = NULL; 204 led_dat->cdev.flags |= LED_CORE_SUSPENDRESUME; 205 led_dat->cdev.groups = ns2_led_groups; 206 led_dat->cmd = template->cmd; 207 led_dat->slow = template->slow; 208 led_dat->can_sleep = gpio_cansleep(led_dat->cmd) | 209 gpio_cansleep(led_dat->slow); 210 if (led_dat->can_sleep) 211 led_dat->cdev.brightness_set_blocking = ns2_led_set_blocking; 212 else 213 led_dat->cdev.brightness_set = ns2_led_set; 214 led_dat->modval = template->modval; 215 led_dat->num_modes = template->num_modes; 216 217 ret = ns2_led_get_mode(led_dat, &mode); 218 if (ret < 0) 219 return ret; 220 221 /* Set LED initial state. */ 222 led_dat->sata = (mode == NS_V2_LED_SATA) ? 1 : 0; 223 led_dat->cdev.brightness = 224 (mode == NS_V2_LED_OFF) ? LED_OFF : LED_FULL; 225 226 ret = led_classdev_register(&pdev->dev, &led_dat->cdev); 227 if (ret < 0) 228 return ret; 229 230 return 0; 231 } 232 233 static void delete_ns2_led(struct ns2_led_data *led_dat) 234 { 235 led_classdev_unregister(&led_dat->cdev); 236 } 237 238 #ifdef CONFIG_OF_GPIO 239 /* 240 * Translate OpenFirmware node properties into platform_data. 241 */ 242 static int 243 ns2_leds_get_of_pdata(struct device *dev, struct ns2_led_platform_data *pdata) 244 { 245 struct device_node *np = dev->of_node; 246 struct device_node *child; 247 struct ns2_led *led, *leds; 248 int ret, num_leds = 0; 249 250 num_leds = of_get_child_count(np); 251 if (!num_leds) 252 return -ENODEV; 253 254 leds = devm_kcalloc(dev, num_leds, sizeof(struct ns2_led), 255 GFP_KERNEL); 256 if (!leds) 257 return -ENOMEM; 258 259 led = leds; 260 for_each_child_of_node(np, child) { 261 const char *string; 262 int i, num_modes; 263 struct ns2_led_modval *modval; 264 265 ret = of_get_named_gpio(child, "cmd-gpio", 0); 266 if (ret < 0) 267 goto err_node_put; 268 led->cmd = ret; 269 ret = of_get_named_gpio(child, "slow-gpio", 0); 270 if (ret < 0) 271 goto err_node_put; 272 led->slow = ret; 273 ret = of_property_read_string(child, "label", &string); 274 led->name = (ret == 0) ? string : child->name; 275 ret = of_property_read_string(child, "linux,default-trigger", 276 &string); 277 if (ret == 0) 278 led->default_trigger = string; 279 280 ret = of_property_count_u32_elems(child, "modes-map"); 281 if (ret < 0 || ret % 3) { 282 dev_err(dev, 283 "Missing or malformed modes-map property\n"); 284 ret = -EINVAL; 285 goto err_node_put; 286 } 287 288 num_modes = ret / 3; 289 modval = devm_kcalloc(dev, 290 num_modes, 291 sizeof(struct ns2_led_modval), 292 GFP_KERNEL); 293 if (!modval) { 294 ret = -ENOMEM; 295 goto err_node_put; 296 } 297 298 for (i = 0; i < num_modes; i++) { 299 of_property_read_u32_index(child, 300 "modes-map", 3 * i, 301 (u32 *) &modval[i].mode); 302 of_property_read_u32_index(child, 303 "modes-map", 3 * i + 1, 304 (u32 *) &modval[i].cmd_level); 305 of_property_read_u32_index(child, 306 "modes-map", 3 * i + 2, 307 (u32 *) &modval[i].slow_level); 308 } 309 310 led->num_modes = num_modes; 311 led->modval = modval; 312 313 led++; 314 } 315 316 pdata->leds = leds; 317 pdata->num_leds = num_leds; 318 319 return 0; 320 321 err_node_put: 322 of_node_put(child); 323 return ret; 324 } 325 326 static const struct of_device_id of_ns2_leds_match[] = { 327 { .compatible = "lacie,ns2-leds", }, 328 {}, 329 }; 330 MODULE_DEVICE_TABLE(of, of_ns2_leds_match); 331 #endif /* CONFIG_OF_GPIO */ 332 333 struct ns2_led_priv { 334 int num_leds; 335 struct ns2_led_data leds_data[]; 336 }; 337 338 static inline int sizeof_ns2_led_priv(int num_leds) 339 { 340 return sizeof(struct ns2_led_priv) + 341 (sizeof(struct ns2_led_data) * num_leds); 342 } 343 344 static int ns2_led_probe(struct platform_device *pdev) 345 { 346 struct ns2_led_platform_data *pdata = dev_get_platdata(&pdev->dev); 347 struct ns2_led_priv *priv; 348 int i; 349 int ret; 350 351 #ifdef CONFIG_OF_GPIO 352 if (!pdata) { 353 pdata = devm_kzalloc(&pdev->dev, 354 sizeof(struct ns2_led_platform_data), 355 GFP_KERNEL); 356 if (!pdata) 357 return -ENOMEM; 358 359 ret = ns2_leds_get_of_pdata(&pdev->dev, pdata); 360 if (ret) 361 return ret; 362 } 363 #else 364 if (!pdata) 365 return -EINVAL; 366 #endif /* CONFIG_OF_GPIO */ 367 368 priv = devm_kzalloc(&pdev->dev, 369 sizeof_ns2_led_priv(pdata->num_leds), GFP_KERNEL); 370 if (!priv) 371 return -ENOMEM; 372 priv->num_leds = pdata->num_leds; 373 374 for (i = 0; i < priv->num_leds; i++) { 375 ret = create_ns2_led(pdev, &priv->leds_data[i], 376 &pdata->leds[i]); 377 if (ret < 0) { 378 for (i = i - 1; i >= 0; i--) 379 delete_ns2_led(&priv->leds_data[i]); 380 return ret; 381 } 382 } 383 384 platform_set_drvdata(pdev, priv); 385 386 return 0; 387 } 388 389 static int ns2_led_remove(struct platform_device *pdev) 390 { 391 int i; 392 struct ns2_led_priv *priv; 393 394 priv = platform_get_drvdata(pdev); 395 396 for (i = 0; i < priv->num_leds; i++) 397 delete_ns2_led(&priv->leds_data[i]); 398 399 return 0; 400 } 401 402 static struct platform_driver ns2_led_driver = { 403 .probe = ns2_led_probe, 404 .remove = ns2_led_remove, 405 .driver = { 406 .name = "leds-ns2", 407 .of_match_table = of_match_ptr(of_ns2_leds_match), 408 }, 409 }; 410 411 module_platform_driver(ns2_led_driver); 412 413 MODULE_AUTHOR("Simon Guinot <sguinot@lacie.com>"); 414 MODULE_DESCRIPTION("Network Space v2 LED driver"); 415 MODULE_LICENSE("GPL"); 416 MODULE_ALIAS("platform:leds-ns2"); 417