1c746b551SMark Brown /* 2c746b551SMark Brown * LED driver for WM831x status LEDs 3c746b551SMark Brown * 4c746b551SMark Brown * Copyright(C) 2009 Wolfson Microelectronics PLC. 5c746b551SMark Brown * 6c746b551SMark Brown * This program is free software; you can redistribute it and/or modify 7c746b551SMark Brown * it under the terms of the GNU General Public License version 2 as 8c746b551SMark Brown * published by the Free Software Foundation. 9c746b551SMark Brown * 10c746b551SMark Brown */ 11c746b551SMark Brown 12c746b551SMark Brown #include <linux/kernel.h> 13c746b551SMark Brown #include <linux/platform_device.h> 145a0e3ad6STejun Heo #include <linux/slab.h> 15c746b551SMark Brown #include <linux/leds.h> 16c746b551SMark Brown #include <linux/err.h> 17c746b551SMark Brown #include <linux/mfd/wm831x/core.h> 18c746b551SMark Brown #include <linux/mfd/wm831x/pdata.h> 19c746b551SMark Brown #include <linux/mfd/wm831x/status.h> 2054f4dedbSPaul Gortmaker #include <linux/module.h> 21c746b551SMark Brown 22c746b551SMark Brown 23c746b551SMark Brown struct wm831x_status { 24c746b551SMark Brown struct led_classdev cdev; 25c746b551SMark Brown struct wm831x *wm831x; 26c746b551SMark Brown struct mutex mutex; 27c746b551SMark Brown 28c746b551SMark Brown spinlock_t value_lock; 29c746b551SMark Brown int reg; /* Control register */ 30c746b551SMark Brown int reg_val; /* Control register value */ 31c746b551SMark Brown 32c746b551SMark Brown int blink; 33c746b551SMark Brown int blink_time; 34c746b551SMark Brown int blink_cyc; 35c746b551SMark Brown int src; 36c746b551SMark Brown enum led_brightness brightness; 37c746b551SMark Brown }; 38c746b551SMark Brown 39c746b551SMark Brown #define to_wm831x_status(led_cdev) \ 40c746b551SMark Brown container_of(led_cdev, struct wm831x_status, cdev) 41c746b551SMark Brown 42f633a512SAndrew Lunn static void wm831x_status_set(struct wm831x_status *led) 43c746b551SMark Brown { 44c746b551SMark Brown unsigned long flags; 45c746b551SMark Brown 46c746b551SMark Brown mutex_lock(&led->mutex); 47c746b551SMark Brown 48c746b551SMark Brown led->reg_val &= ~(WM831X_LED_SRC_MASK | WM831X_LED_MODE_MASK | 49c746b551SMark Brown WM831X_LED_DUTY_CYC_MASK | WM831X_LED_DUR_MASK); 50c746b551SMark Brown 51c746b551SMark Brown spin_lock_irqsave(&led->value_lock, flags); 52c746b551SMark Brown 53c746b551SMark Brown led->reg_val |= led->src << WM831X_LED_SRC_SHIFT; 54c746b551SMark Brown if (led->blink) { 55c746b551SMark Brown led->reg_val |= 2 << WM831X_LED_MODE_SHIFT; 56c746b551SMark Brown led->reg_val |= led->blink_time << WM831X_LED_DUR_SHIFT; 57c746b551SMark Brown led->reg_val |= led->blink_cyc; 58c746b551SMark Brown } else { 59c746b551SMark Brown if (led->brightness != LED_OFF) 60c746b551SMark Brown led->reg_val |= 1 << WM831X_LED_MODE_SHIFT; 61c746b551SMark Brown } 62c746b551SMark Brown 63c746b551SMark Brown spin_unlock_irqrestore(&led->value_lock, flags); 64c746b551SMark Brown 65c746b551SMark Brown wm831x_reg_write(led->wm831x, led->reg, led->reg_val); 66c746b551SMark Brown 67c746b551SMark Brown mutex_unlock(&led->mutex); 68c746b551SMark Brown } 69c746b551SMark Brown 70f633a512SAndrew Lunn static int wm831x_status_brightness_set(struct led_classdev *led_cdev, 71c746b551SMark Brown enum led_brightness value) 72c746b551SMark Brown { 73c746b551SMark Brown struct wm831x_status *led = to_wm831x_status(led_cdev); 74c746b551SMark Brown unsigned long flags; 75c746b551SMark Brown 76c746b551SMark Brown spin_lock_irqsave(&led->value_lock, flags); 77c746b551SMark Brown led->brightness = value; 78c746b551SMark Brown if (value == LED_OFF) 79c746b551SMark Brown led->blink = 0; 80c746b551SMark Brown spin_unlock_irqrestore(&led->value_lock, flags); 81f633a512SAndrew Lunn wm831x_status_set(led); 82f633a512SAndrew Lunn 83f633a512SAndrew Lunn return 0; 84c746b551SMark Brown } 85c746b551SMark Brown 86c746b551SMark Brown static int wm831x_status_blink_set(struct led_classdev *led_cdev, 87c746b551SMark Brown unsigned long *delay_on, 88c746b551SMark Brown unsigned long *delay_off) 89c746b551SMark Brown { 90c746b551SMark Brown struct wm831x_status *led = to_wm831x_status(led_cdev); 91c746b551SMark Brown unsigned long flags; 92c746b551SMark Brown int ret = 0; 93c746b551SMark Brown 94c746b551SMark Brown /* Pick some defaults if we've not been given times */ 95c746b551SMark Brown if (*delay_on == 0 && *delay_off == 0) { 96c746b551SMark Brown *delay_on = 250; 97c746b551SMark Brown *delay_off = 250; 98c746b551SMark Brown } 99c746b551SMark Brown 100c746b551SMark Brown spin_lock_irqsave(&led->value_lock, flags); 101c746b551SMark Brown 102c746b551SMark Brown /* We only have a limited selection of settings, see if we can 103c746b551SMark Brown * support the configuration we're being given */ 104c746b551SMark Brown switch (*delay_on) { 105c746b551SMark Brown case 1000: 106c746b551SMark Brown led->blink_time = 0; 107c746b551SMark Brown break; 108c746b551SMark Brown case 250: 109c746b551SMark Brown led->blink_time = 1; 110c746b551SMark Brown break; 111c746b551SMark Brown case 125: 112c746b551SMark Brown led->blink_time = 2; 113c746b551SMark Brown break; 114c746b551SMark Brown case 62: 115c746b551SMark Brown case 63: 116c746b551SMark Brown /* Actually 62.5ms */ 117c746b551SMark Brown led->blink_time = 3; 118c746b551SMark Brown break; 119c746b551SMark Brown default: 120c746b551SMark Brown ret = -EINVAL; 121c746b551SMark Brown break; 122c746b551SMark Brown } 123c746b551SMark Brown 124c746b551SMark Brown if (ret == 0) { 125c746b551SMark Brown switch (*delay_off / *delay_on) { 126c746b551SMark Brown case 1: 127c746b551SMark Brown led->blink_cyc = 0; 128c746b551SMark Brown break; 129c746b551SMark Brown case 3: 130c746b551SMark Brown led->blink_cyc = 1; 131c746b551SMark Brown break; 132c746b551SMark Brown case 4: 133c746b551SMark Brown led->blink_cyc = 2; 134c746b551SMark Brown break; 135c746b551SMark Brown case 8: 136c746b551SMark Brown led->blink_cyc = 3; 137c746b551SMark Brown break; 138c746b551SMark Brown default: 139c746b551SMark Brown ret = -EINVAL; 140c746b551SMark Brown break; 141c746b551SMark Brown } 142c746b551SMark Brown } 143c746b551SMark Brown 144c746b551SMark Brown if (ret == 0) 145c746b551SMark Brown led->blink = 1; 146c746b551SMark Brown else 147c746b551SMark Brown led->blink = 0; 148c746b551SMark Brown 149c746b551SMark Brown spin_unlock_irqrestore(&led->value_lock, flags); 150f633a512SAndrew Lunn wm831x_status_set(led); 151c746b551SMark Brown 152c746b551SMark Brown return ret; 153c746b551SMark Brown } 154c746b551SMark Brown 1558a4529a3SJingoo Han static const char * const led_src_texts[] = { 156c746b551SMark Brown "otp", 157c746b551SMark Brown "power", 158c746b551SMark Brown "charger", 159c746b551SMark Brown "soft", 160c746b551SMark Brown }; 161c746b551SMark Brown 162c746b551SMark Brown static ssize_t wm831x_status_src_show(struct device *dev, 163c746b551SMark Brown struct device_attribute *attr, char *buf) 164c746b551SMark Brown { 165c746b551SMark Brown struct led_classdev *led_cdev = dev_get_drvdata(dev); 166c746b551SMark Brown struct wm831x_status *led = to_wm831x_status(led_cdev); 167c746b551SMark Brown int i; 168c746b551SMark Brown ssize_t ret = 0; 169c746b551SMark Brown 170c746b551SMark Brown mutex_lock(&led->mutex); 171c746b551SMark Brown 172c746b551SMark Brown for (i = 0; i < ARRAY_SIZE(led_src_texts); i++) 173c746b551SMark Brown if (i == led->src) 174c746b551SMark Brown ret += sprintf(&buf[ret], "[%s] ", led_src_texts[i]); 175c746b551SMark Brown else 176c746b551SMark Brown ret += sprintf(&buf[ret], "%s ", led_src_texts[i]); 177c746b551SMark Brown 178c746b551SMark Brown mutex_unlock(&led->mutex); 179c746b551SMark Brown 180c746b551SMark Brown ret += sprintf(&buf[ret], "\n"); 181c746b551SMark Brown 182c746b551SMark Brown return ret; 183c746b551SMark Brown } 184c746b551SMark Brown 185c746b551SMark Brown static ssize_t wm831x_status_src_store(struct device *dev, 186c746b551SMark Brown struct device_attribute *attr, 187c746b551SMark Brown const char *buf, size_t size) 188c746b551SMark Brown { 189c746b551SMark Brown struct led_classdev *led_cdev = dev_get_drvdata(dev); 190c746b551SMark Brown struct wm831x_status *led = to_wm831x_status(led_cdev); 191c746b551SMark Brown char name[20]; 192c746b551SMark Brown int i; 193c746b551SMark Brown size_t len; 194c746b551SMark Brown 195c746b551SMark Brown name[sizeof(name) - 1] = '\0'; 196c746b551SMark Brown strncpy(name, buf, sizeof(name) - 1); 197c746b551SMark Brown len = strlen(name); 198c746b551SMark Brown 199c746b551SMark Brown if (len && name[len - 1] == '\n') 200c746b551SMark Brown name[len - 1] = '\0'; 201c746b551SMark Brown 202c746b551SMark Brown for (i = 0; i < ARRAY_SIZE(led_src_texts); i++) { 203c746b551SMark Brown if (!strcmp(name, led_src_texts[i])) { 204c746b551SMark Brown mutex_lock(&led->mutex); 205c746b551SMark Brown led->src = i; 206c746b551SMark Brown mutex_unlock(&led->mutex); 207f633a512SAndrew Lunn wm831x_status_set(led); 208c746b551SMark Brown } 209c746b551SMark Brown } 210c746b551SMark Brown 211c746b551SMark Brown return size; 212c746b551SMark Brown } 213c746b551SMark Brown 214c746b551SMark Brown static DEVICE_ATTR(src, 0644, wm831x_status_src_show, wm831x_status_src_store); 215c746b551SMark Brown 216b2015ed5SJohan Hovold static struct attribute *wm831x_status_attrs[] = { 217b2015ed5SJohan Hovold &dev_attr_src.attr, 218b2015ed5SJohan Hovold NULL 219b2015ed5SJohan Hovold }; 220b2015ed5SJohan Hovold ATTRIBUTE_GROUPS(wm831x_status); 221b2015ed5SJohan Hovold 222c746b551SMark Brown static int wm831x_status_probe(struct platform_device *pdev) 223c746b551SMark Brown { 224c746b551SMark Brown struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent); 225c746b551SMark Brown struct wm831x_pdata *chip_pdata; 226c746b551SMark Brown struct wm831x_status_pdata pdata; 227c746b551SMark Brown struct wm831x_status *drvdata; 228c746b551SMark Brown struct resource *res; 229c746b551SMark Brown int id = pdev->id % ARRAY_SIZE(chip_pdata->status); 230c746b551SMark Brown int ret; 231c746b551SMark Brown 23261abeba5SMark Brown res = platform_get_resource(pdev, IORESOURCE_REG, 0); 233c746b551SMark Brown if (res == NULL) { 23461abeba5SMark Brown dev_err(&pdev->dev, "No register resource\n"); 235b2015ed5SJohan Hovold return -EINVAL; 236c746b551SMark Brown } 237c746b551SMark Brown 2381713cb9dSMark Brown drvdata = devm_kzalloc(&pdev->dev, sizeof(struct wm831x_status), 2391713cb9dSMark Brown GFP_KERNEL); 240c746b551SMark Brown if (!drvdata) 241c746b551SMark Brown return -ENOMEM; 242c746b551SMark Brown 243c746b551SMark Brown drvdata->wm831x = wm831x; 244c746b551SMark Brown drvdata->reg = res->start; 245c746b551SMark Brown 24687aae1eaSJingoo Han if (dev_get_platdata(wm831x->dev)) 24787aae1eaSJingoo Han chip_pdata = dev_get_platdata(wm831x->dev); 248c746b551SMark Brown else 249c746b551SMark Brown chip_pdata = NULL; 250c746b551SMark Brown 251c746b551SMark Brown memset(&pdata, 0, sizeof(pdata)); 252c746b551SMark Brown if (chip_pdata && chip_pdata->status[id]) 253c746b551SMark Brown memcpy(&pdata, chip_pdata->status[id], sizeof(pdata)); 254c746b551SMark Brown else 255c746b551SMark Brown pdata.name = dev_name(&pdev->dev); 256c746b551SMark Brown 257c746b551SMark Brown mutex_init(&drvdata->mutex); 258c746b551SMark Brown spin_lock_init(&drvdata->value_lock); 259c746b551SMark Brown 260c746b551SMark Brown /* We cache the configuration register and read startup values 261c746b551SMark Brown * from it. */ 262c746b551SMark Brown drvdata->reg_val = wm831x_reg_read(wm831x, drvdata->reg); 263c746b551SMark Brown 264c746b551SMark Brown if (drvdata->reg_val & WM831X_LED_MODE_MASK) 265c746b551SMark Brown drvdata->brightness = LED_FULL; 266c746b551SMark Brown else 267c746b551SMark Brown drvdata->brightness = LED_OFF; 268c746b551SMark Brown 269c746b551SMark Brown /* Set a default source if configured, otherwise leave the 270c746b551SMark Brown * current hardware setting. 271c746b551SMark Brown */ 272c746b551SMark Brown if (pdata.default_src == WM831X_STATUS_PRESERVE) { 273c746b551SMark Brown drvdata->src = drvdata->reg_val; 274c746b551SMark Brown drvdata->src &= WM831X_LED_SRC_MASK; 275c746b551SMark Brown drvdata->src >>= WM831X_LED_SRC_SHIFT; 276c746b551SMark Brown } else { 277c746b551SMark Brown drvdata->src = pdata.default_src - 1; 278c746b551SMark Brown } 279c746b551SMark Brown 280c746b551SMark Brown drvdata->cdev.name = pdata.name; 281c746b551SMark Brown drvdata->cdev.default_trigger = pdata.default_trigger; 282f633a512SAndrew Lunn drvdata->cdev.brightness_set_blocking = wm831x_status_brightness_set; 283c746b551SMark Brown drvdata->cdev.blink_set = wm831x_status_blink_set; 284b2015ed5SJohan Hovold drvdata->cdev.groups = wm831x_status_groups; 285c746b551SMark Brown 2868d3b6a40SAmitoj Kaur Chawla ret = devm_led_classdev_register(wm831x->dev, &drvdata->cdev); 287c746b551SMark Brown if (ret < 0) { 288c746b551SMark Brown dev_err(&pdev->dev, "Failed to register LED: %d\n", ret); 289b2015ed5SJohan Hovold return ret; 290c746b551SMark Brown } 291c746b551SMark Brown 292c746b551SMark Brown return 0; 293c746b551SMark Brown } 294c746b551SMark Brown 295c746b551SMark Brown static struct platform_driver wm831x_status_driver = { 296c746b551SMark Brown .driver = { 297c746b551SMark Brown .name = "wm831x-status", 298c746b551SMark Brown }, 299c746b551SMark Brown .probe = wm831x_status_probe, 300c746b551SMark Brown }; 301c746b551SMark Brown 302892a8843SAxel Lin module_platform_driver(wm831x_status_driver); 303c746b551SMark Brown 304c746b551SMark Brown MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>"); 305c746b551SMark Brown MODULE_DESCRIPTION("WM831x status LED driver"); 306c746b551SMark Brown MODULE_LICENSE("GPL"); 307c746b551SMark Brown MODULE_ALIAS("platform:wm831x-status"); 308