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/init.h> 14c746b551SMark Brown #include <linux/platform_device.h> 155a0e3ad6STejun Heo #include <linux/slab.h> 16c746b551SMark Brown #include <linux/leds.h> 17c746b551SMark Brown #include <linux/err.h> 18c746b551SMark Brown #include <linux/mfd/wm831x/core.h> 19c746b551SMark Brown #include <linux/mfd/wm831x/pdata.h> 20c746b551SMark Brown #include <linux/mfd/wm831x/status.h> 2154f4dedbSPaul Gortmaker #include <linux/module.h> 22c746b551SMark Brown 23c746b551SMark Brown 24c746b551SMark Brown struct wm831x_status { 25c746b551SMark Brown struct led_classdev cdev; 26c746b551SMark Brown struct wm831x *wm831x; 27c746b551SMark Brown struct work_struct work; 28c746b551SMark Brown struct mutex mutex; 29c746b551SMark Brown 30c746b551SMark Brown spinlock_t value_lock; 31c746b551SMark Brown int reg; /* Control register */ 32c746b551SMark Brown int reg_val; /* Control register value */ 33c746b551SMark Brown 34c746b551SMark Brown int blink; 35c746b551SMark Brown int blink_time; 36c746b551SMark Brown int blink_cyc; 37c746b551SMark Brown int src; 38c746b551SMark Brown enum led_brightness brightness; 39c746b551SMark Brown }; 40c746b551SMark Brown 41c746b551SMark Brown #define to_wm831x_status(led_cdev) \ 42c746b551SMark Brown container_of(led_cdev, struct wm831x_status, cdev) 43c746b551SMark Brown 44c746b551SMark Brown static void wm831x_status_work(struct work_struct *work) 45c746b551SMark Brown { 46c746b551SMark Brown struct wm831x_status *led = container_of(work, struct wm831x_status, 47c746b551SMark Brown work); 48c746b551SMark Brown unsigned long flags; 49c746b551SMark Brown 50c746b551SMark Brown mutex_lock(&led->mutex); 51c746b551SMark Brown 52c746b551SMark Brown led->reg_val &= ~(WM831X_LED_SRC_MASK | WM831X_LED_MODE_MASK | 53c746b551SMark Brown WM831X_LED_DUTY_CYC_MASK | WM831X_LED_DUR_MASK); 54c746b551SMark Brown 55c746b551SMark Brown spin_lock_irqsave(&led->value_lock, flags); 56c746b551SMark Brown 57c746b551SMark Brown led->reg_val |= led->src << WM831X_LED_SRC_SHIFT; 58c746b551SMark Brown if (led->blink) { 59c746b551SMark Brown led->reg_val |= 2 << WM831X_LED_MODE_SHIFT; 60c746b551SMark Brown led->reg_val |= led->blink_time << WM831X_LED_DUR_SHIFT; 61c746b551SMark Brown led->reg_val |= led->blink_cyc; 62c746b551SMark Brown } else { 63c746b551SMark Brown if (led->brightness != LED_OFF) 64c746b551SMark Brown led->reg_val |= 1 << WM831X_LED_MODE_SHIFT; 65c746b551SMark Brown } 66c746b551SMark Brown 67c746b551SMark Brown spin_unlock_irqrestore(&led->value_lock, flags); 68c746b551SMark Brown 69c746b551SMark Brown wm831x_reg_write(led->wm831x, led->reg, led->reg_val); 70c746b551SMark Brown 71c746b551SMark Brown mutex_unlock(&led->mutex); 72c746b551SMark Brown } 73c746b551SMark Brown 74c746b551SMark Brown static void wm831x_status_set(struct led_classdev *led_cdev, 75c746b551SMark Brown enum led_brightness value) 76c746b551SMark Brown { 77c746b551SMark Brown struct wm831x_status *led = to_wm831x_status(led_cdev); 78c746b551SMark Brown unsigned long flags; 79c746b551SMark Brown 80c746b551SMark Brown spin_lock_irqsave(&led->value_lock, flags); 81c746b551SMark Brown led->brightness = value; 82c746b551SMark Brown if (value == LED_OFF) 83c746b551SMark Brown led->blink = 0; 84c746b551SMark Brown schedule_work(&led->work); 85c746b551SMark Brown spin_unlock_irqrestore(&led->value_lock, flags); 86c746b551SMark Brown } 87c746b551SMark Brown 88c746b551SMark Brown static int wm831x_status_blink_set(struct led_classdev *led_cdev, 89c746b551SMark Brown unsigned long *delay_on, 90c746b551SMark Brown unsigned long *delay_off) 91c746b551SMark Brown { 92c746b551SMark Brown struct wm831x_status *led = to_wm831x_status(led_cdev); 93c746b551SMark Brown unsigned long flags; 94c746b551SMark Brown int ret = 0; 95c746b551SMark Brown 96c746b551SMark Brown /* Pick some defaults if we've not been given times */ 97c746b551SMark Brown if (*delay_on == 0 && *delay_off == 0) { 98c746b551SMark Brown *delay_on = 250; 99c746b551SMark Brown *delay_off = 250; 100c746b551SMark Brown } 101c746b551SMark Brown 102c746b551SMark Brown spin_lock_irqsave(&led->value_lock, flags); 103c746b551SMark Brown 104c746b551SMark Brown /* We only have a limited selection of settings, see if we can 105c746b551SMark Brown * support the configuration we're being given */ 106c746b551SMark Brown switch (*delay_on) { 107c746b551SMark Brown case 1000: 108c746b551SMark Brown led->blink_time = 0; 109c746b551SMark Brown break; 110c746b551SMark Brown case 250: 111c746b551SMark Brown led->blink_time = 1; 112c746b551SMark Brown break; 113c746b551SMark Brown case 125: 114c746b551SMark Brown led->blink_time = 2; 115c746b551SMark Brown break; 116c746b551SMark Brown case 62: 117c746b551SMark Brown case 63: 118c746b551SMark Brown /* Actually 62.5ms */ 119c746b551SMark Brown led->blink_time = 3; 120c746b551SMark Brown break; 121c746b551SMark Brown default: 122c746b551SMark Brown ret = -EINVAL; 123c746b551SMark Brown break; 124c746b551SMark Brown } 125c746b551SMark Brown 126c746b551SMark Brown if (ret == 0) { 127c746b551SMark Brown switch (*delay_off / *delay_on) { 128c746b551SMark Brown case 1: 129c746b551SMark Brown led->blink_cyc = 0; 130c746b551SMark Brown break; 131c746b551SMark Brown case 3: 132c746b551SMark Brown led->blink_cyc = 1; 133c746b551SMark Brown break; 134c746b551SMark Brown case 4: 135c746b551SMark Brown led->blink_cyc = 2; 136c746b551SMark Brown break; 137c746b551SMark Brown case 8: 138c746b551SMark Brown led->blink_cyc = 3; 139c746b551SMark Brown break; 140c746b551SMark Brown default: 141c746b551SMark Brown ret = -EINVAL; 142c746b551SMark Brown break; 143c746b551SMark Brown } 144c746b551SMark Brown } 145c746b551SMark Brown 146c746b551SMark Brown if (ret == 0) 147c746b551SMark Brown led->blink = 1; 148c746b551SMark Brown else 149c746b551SMark Brown led->blink = 0; 150c746b551SMark Brown 151c746b551SMark Brown /* Always update; if we fail turn off blinking since we expect 152c746b551SMark Brown * a software fallback. */ 153c746b551SMark Brown schedule_work(&led->work); 154c746b551SMark Brown 155c746b551SMark Brown spin_unlock_irqrestore(&led->value_lock, flags); 156c746b551SMark Brown 157c746b551SMark Brown return ret; 158c746b551SMark Brown } 159c746b551SMark Brown 1608a4529a3SJingoo Han static const char * const led_src_texts[] = { 161c746b551SMark Brown "otp", 162c746b551SMark Brown "power", 163c746b551SMark Brown "charger", 164c746b551SMark Brown "soft", 165c746b551SMark Brown }; 166c746b551SMark Brown 167c746b551SMark Brown static ssize_t wm831x_status_src_show(struct device *dev, 168c746b551SMark Brown struct device_attribute *attr, char *buf) 169c746b551SMark Brown { 170c746b551SMark Brown struct led_classdev *led_cdev = dev_get_drvdata(dev); 171c746b551SMark Brown struct wm831x_status *led = to_wm831x_status(led_cdev); 172c746b551SMark Brown int i; 173c746b551SMark Brown ssize_t ret = 0; 174c746b551SMark Brown 175c746b551SMark Brown mutex_lock(&led->mutex); 176c746b551SMark Brown 177c746b551SMark Brown for (i = 0; i < ARRAY_SIZE(led_src_texts); i++) 178c746b551SMark Brown if (i == led->src) 179c746b551SMark Brown ret += sprintf(&buf[ret], "[%s] ", led_src_texts[i]); 180c746b551SMark Brown else 181c746b551SMark Brown ret += sprintf(&buf[ret], "%s ", led_src_texts[i]); 182c746b551SMark Brown 183c746b551SMark Brown mutex_unlock(&led->mutex); 184c746b551SMark Brown 185c746b551SMark Brown ret += sprintf(&buf[ret], "\n"); 186c746b551SMark Brown 187c746b551SMark Brown return ret; 188c746b551SMark Brown } 189c746b551SMark Brown 190c746b551SMark Brown static ssize_t wm831x_status_src_store(struct device *dev, 191c746b551SMark Brown struct device_attribute *attr, 192c746b551SMark Brown const char *buf, size_t size) 193c746b551SMark Brown { 194c746b551SMark Brown struct led_classdev *led_cdev = dev_get_drvdata(dev); 195c746b551SMark Brown struct wm831x_status *led = to_wm831x_status(led_cdev); 196c746b551SMark Brown char name[20]; 197c746b551SMark Brown int i; 198c746b551SMark Brown size_t len; 199c746b551SMark Brown 200c746b551SMark Brown name[sizeof(name) - 1] = '\0'; 201c746b551SMark Brown strncpy(name, buf, sizeof(name) - 1); 202c746b551SMark Brown len = strlen(name); 203c746b551SMark Brown 204c746b551SMark Brown if (len && name[len - 1] == '\n') 205c746b551SMark Brown name[len - 1] = '\0'; 206c746b551SMark Brown 207c746b551SMark Brown for (i = 0; i < ARRAY_SIZE(led_src_texts); i++) { 208c746b551SMark Brown if (!strcmp(name, led_src_texts[i])) { 209c746b551SMark Brown mutex_lock(&led->mutex); 210c746b551SMark Brown 211c746b551SMark Brown led->src = i; 212c746b551SMark Brown schedule_work(&led->work); 213c746b551SMark Brown 214c746b551SMark Brown mutex_unlock(&led->mutex); 215c746b551SMark Brown } 216c746b551SMark Brown } 217c746b551SMark Brown 218c746b551SMark Brown return size; 219c746b551SMark Brown } 220c746b551SMark Brown 221c746b551SMark Brown static DEVICE_ATTR(src, 0644, wm831x_status_src_show, wm831x_status_src_store); 222c746b551SMark Brown 223c746b551SMark Brown static int wm831x_status_probe(struct platform_device *pdev) 224c746b551SMark Brown { 225c746b551SMark Brown struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent); 226c746b551SMark Brown struct wm831x_pdata *chip_pdata; 227c746b551SMark Brown struct wm831x_status_pdata pdata; 228c746b551SMark Brown struct wm831x_status *drvdata; 229c746b551SMark Brown struct resource *res; 230c746b551SMark Brown int id = pdev->id % ARRAY_SIZE(chip_pdata->status); 231c746b551SMark Brown int ret; 232c746b551SMark Brown 233c746b551SMark Brown res = platform_get_resource(pdev, IORESOURCE_IO, 0); 234c746b551SMark Brown if (res == NULL) { 235c746b551SMark Brown dev_err(&pdev->dev, "No I/O resource\n"); 236c746b551SMark Brown ret = -EINVAL; 237c746b551SMark Brown goto err; 238c746b551SMark Brown } 239c746b551SMark Brown 2401713cb9dSMark Brown drvdata = devm_kzalloc(&pdev->dev, sizeof(struct wm831x_status), 2411713cb9dSMark Brown GFP_KERNEL); 242c746b551SMark Brown if (!drvdata) 243c746b551SMark Brown return -ENOMEM; 24484196a2fSJingoo Han platform_set_drvdata(pdev, drvdata); 245c746b551SMark Brown 246c746b551SMark Brown drvdata->wm831x = wm831x; 247c746b551SMark Brown drvdata->reg = res->start; 248c746b551SMark Brown 249c746b551SMark Brown if (wm831x->dev->platform_data) 250c746b551SMark Brown chip_pdata = wm831x->dev->platform_data; 251c746b551SMark Brown else 252c746b551SMark Brown chip_pdata = NULL; 253c746b551SMark Brown 254c746b551SMark Brown memset(&pdata, 0, sizeof(pdata)); 255c746b551SMark Brown if (chip_pdata && chip_pdata->status[id]) 256c746b551SMark Brown memcpy(&pdata, chip_pdata->status[id], sizeof(pdata)); 257c746b551SMark Brown else 258c746b551SMark Brown pdata.name = dev_name(&pdev->dev); 259c746b551SMark Brown 260c746b551SMark Brown mutex_init(&drvdata->mutex); 261c746b551SMark Brown INIT_WORK(&drvdata->work, wm831x_status_work); 262c746b551SMark Brown spin_lock_init(&drvdata->value_lock); 263c746b551SMark Brown 264c746b551SMark Brown /* We cache the configuration register and read startup values 265c746b551SMark Brown * from it. */ 266c746b551SMark Brown drvdata->reg_val = wm831x_reg_read(wm831x, drvdata->reg); 267c746b551SMark Brown 268c746b551SMark Brown if (drvdata->reg_val & WM831X_LED_MODE_MASK) 269c746b551SMark Brown drvdata->brightness = LED_FULL; 270c746b551SMark Brown else 271c746b551SMark Brown drvdata->brightness = LED_OFF; 272c746b551SMark Brown 273c746b551SMark Brown /* Set a default source if configured, otherwise leave the 274c746b551SMark Brown * current hardware setting. 275c746b551SMark Brown */ 276c746b551SMark Brown if (pdata.default_src == WM831X_STATUS_PRESERVE) { 277c746b551SMark Brown drvdata->src = drvdata->reg_val; 278c746b551SMark Brown drvdata->src &= WM831X_LED_SRC_MASK; 279c746b551SMark Brown drvdata->src >>= WM831X_LED_SRC_SHIFT; 280c746b551SMark Brown } else { 281c746b551SMark Brown drvdata->src = pdata.default_src - 1; 282c746b551SMark Brown } 283c746b551SMark Brown 284c746b551SMark Brown drvdata->cdev.name = pdata.name; 285c746b551SMark Brown drvdata->cdev.default_trigger = pdata.default_trigger; 286c746b551SMark Brown drvdata->cdev.brightness_set = wm831x_status_set; 287c746b551SMark Brown drvdata->cdev.blink_set = wm831x_status_blink_set; 288c746b551SMark Brown 289c746b551SMark Brown ret = led_classdev_register(wm831x->dev, &drvdata->cdev); 290c746b551SMark Brown if (ret < 0) { 291c746b551SMark Brown dev_err(&pdev->dev, "Failed to register LED: %d\n", ret); 292c746b551SMark Brown goto err_led; 293c746b551SMark Brown } 294c746b551SMark Brown 295c746b551SMark Brown ret = device_create_file(drvdata->cdev.dev, &dev_attr_src); 296c746b551SMark Brown if (ret != 0) 297c746b551SMark Brown dev_err(&pdev->dev, 298c746b551SMark Brown "No source control for LED: %d\n", ret); 299c746b551SMark Brown 300c746b551SMark Brown return 0; 301c746b551SMark Brown 302c746b551SMark Brown err_led: 303c746b551SMark Brown led_classdev_unregister(&drvdata->cdev); 304c746b551SMark Brown err: 305c746b551SMark Brown return ret; 306c746b551SMark Brown } 307c746b551SMark Brown 308c746b551SMark Brown static int wm831x_status_remove(struct platform_device *pdev) 309c746b551SMark Brown { 310c746b551SMark Brown struct wm831x_status *drvdata = platform_get_drvdata(pdev); 311c746b551SMark Brown 312c746b551SMark Brown device_remove_file(drvdata->cdev.dev, &dev_attr_src); 313c746b551SMark Brown led_classdev_unregister(&drvdata->cdev); 314c746b551SMark Brown 315c746b551SMark Brown return 0; 316c746b551SMark Brown } 317c746b551SMark Brown 318c746b551SMark Brown static struct platform_driver wm831x_status_driver = { 319c746b551SMark Brown .driver = { 320c746b551SMark Brown .name = "wm831x-status", 321c746b551SMark Brown .owner = THIS_MODULE, 322c746b551SMark Brown }, 323c746b551SMark Brown .probe = wm831x_status_probe, 324c746b551SMark Brown .remove = wm831x_status_remove, 325c746b551SMark Brown }; 326c746b551SMark Brown 327892a8843SAxel Lin module_platform_driver(wm831x_status_driver); 328c746b551SMark Brown 329c746b551SMark Brown MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>"); 330c746b551SMark Brown MODULE_DESCRIPTION("WM831x status LED driver"); 331c746b551SMark Brown MODULE_LICENSE("GPL"); 332c746b551SMark Brown MODULE_ALIAS("platform:wm831x-status"); 333