19e84561cSMike Rapoport /* 29e84561cSMike Rapoport * LEDs driver for Dialog Semiconductor DA9030/DA9034 39e84561cSMike Rapoport * 49e84561cSMike Rapoport * Copyright (C) 2008 Compulab, Ltd. 59e84561cSMike Rapoport * Mike Rapoport <mike@compulab.co.il> 69e84561cSMike Rapoport * 79e84561cSMike Rapoport * Copyright (C) 2006-2008 Marvell International Ltd. 89e84561cSMike Rapoport * Eric Miao <eric.miao@marvell.com> 99e84561cSMike Rapoport * 109e84561cSMike Rapoport * This program is free software; you can redistribute it and/or modify 119e84561cSMike Rapoport * it under the terms of the GNU General Public License version 2 as 129e84561cSMike Rapoport * published by the Free Software Foundation. 139e84561cSMike Rapoport */ 149e84561cSMike Rapoport 159e84561cSMike Rapoport #include <linux/module.h> 169e84561cSMike Rapoport #include <linux/kernel.h> 179e84561cSMike Rapoport #include <linux/init.h> 189e84561cSMike Rapoport #include <linux/platform_device.h> 199e84561cSMike Rapoport #include <linux/leds.h> 20b9b54aa2SEric Miao #include <linux/workqueue.h> 219e84561cSMike Rapoport #include <linux/mfd/da903x.h> 225a0e3ad6STejun Heo #include <linux/slab.h> 239e84561cSMike Rapoport 249e84561cSMike Rapoport #define DA9030_LED1_CONTROL 0x20 259e84561cSMike Rapoport #define DA9030_LED2_CONTROL 0x21 269e84561cSMike Rapoport #define DA9030_LED3_CONTROL 0x22 279e84561cSMike Rapoport #define DA9030_LED4_CONTROL 0x23 289e84561cSMike Rapoport #define DA9030_LEDPC_CONTROL 0x24 299e84561cSMike Rapoport #define DA9030_MISC_CONTROL_A 0x26 /* Vibrator Control */ 309e84561cSMike Rapoport 319e84561cSMike Rapoport #define DA9034_LED1_CONTROL 0x35 329e84561cSMike Rapoport #define DA9034_LED2_CONTROL 0x36 339e84561cSMike Rapoport #define DA9034_VIBRA 0x40 349e84561cSMike Rapoport 359e84561cSMike Rapoport struct da903x_led { 369e84561cSMike Rapoport struct led_classdev cdev; 379e84561cSMike Rapoport struct work_struct work; 389e84561cSMike Rapoport struct device *master; 399e84561cSMike Rapoport enum led_brightness new_brightness; 409e84561cSMike Rapoport int id; 419e84561cSMike Rapoport int flags; 429e84561cSMike Rapoport }; 439e84561cSMike Rapoport 449e84561cSMike Rapoport #define DA9030_LED_OFFSET(id) ((id) - DA9030_ID_LED_1) 459e84561cSMike Rapoport #define DA9034_LED_OFFSET(id) ((id) - DA9034_ID_LED_1) 469e84561cSMike Rapoport 479e84561cSMike Rapoport static void da903x_led_work(struct work_struct *work) 489e84561cSMike Rapoport { 499e84561cSMike Rapoport struct da903x_led *led = container_of(work, struct da903x_led, work); 509e84561cSMike Rapoport uint8_t val; 519e84561cSMike Rapoport int offset; 529e84561cSMike Rapoport 539e84561cSMike Rapoport switch (led->id) { 549e84561cSMike Rapoport case DA9030_ID_LED_1: 559e84561cSMike Rapoport case DA9030_ID_LED_2: 569e84561cSMike Rapoport case DA9030_ID_LED_3: 579e84561cSMike Rapoport case DA9030_ID_LED_4: 589e84561cSMike Rapoport case DA9030_ID_LED_PC: 599e84561cSMike Rapoport offset = DA9030_LED_OFFSET(led->id); 609e84561cSMike Rapoport val = led->flags & ~0x87; 619e84561cSMike Rapoport val |= (led->new_brightness) ? 0x80 : 0; /* EN bit */ 62a0601c89SJonathan Cameron val |= (0x7 - (led->new_brightness >> 5)) & 0x7; /* PWM<2:0> */ 639e84561cSMike Rapoport da903x_write(led->master, DA9030_LED1_CONTROL + offset, val); 649e84561cSMike Rapoport break; 659e84561cSMike Rapoport case DA9030_ID_VIBRA: 669e84561cSMike Rapoport val = led->flags & ~0x80; 679e84561cSMike Rapoport val |= (led->new_brightness) ? 0x80 : 0; /* EN bit */ 689e84561cSMike Rapoport da903x_write(led->master, DA9030_MISC_CONTROL_A, val); 699e84561cSMike Rapoport break; 709e84561cSMike Rapoport case DA9034_ID_LED_1: 719e84561cSMike Rapoport case DA9034_ID_LED_2: 729e84561cSMike Rapoport offset = DA9034_LED_OFFSET(led->id); 739e84561cSMike Rapoport val = (led->new_brightness * 0x5f / LED_FULL) & 0x7f; 749e84561cSMike Rapoport val |= (led->flags & DA9034_LED_RAMP) ? 0x80 : 0; 759e84561cSMike Rapoport da903x_write(led->master, DA9034_LED1_CONTROL + offset, val); 769e84561cSMike Rapoport break; 779e84561cSMike Rapoport case DA9034_ID_VIBRA: 789e84561cSMike Rapoport val = led->new_brightness & 0xfe; 799e84561cSMike Rapoport da903x_write(led->master, DA9034_VIBRA, val); 809e84561cSMike Rapoport break; 819e84561cSMike Rapoport } 829e84561cSMike Rapoport } 839e84561cSMike Rapoport 849e84561cSMike Rapoport static void da903x_led_set(struct led_classdev *led_cdev, 859e84561cSMike Rapoport enum led_brightness value) 869e84561cSMike Rapoport { 879e84561cSMike Rapoport struct da903x_led *led; 889e84561cSMike Rapoport 899e84561cSMike Rapoport led = container_of(led_cdev, struct da903x_led, cdev); 909e84561cSMike Rapoport led->new_brightness = value; 919e84561cSMike Rapoport schedule_work(&led->work); 929e84561cSMike Rapoport } 939e84561cSMike Rapoport 949e84561cSMike Rapoport static int __devinit da903x_led_probe(struct platform_device *pdev) 959e84561cSMike Rapoport { 969e84561cSMike Rapoport struct led_info *pdata = pdev->dev.platform_data; 979e84561cSMike Rapoport struct da903x_led *led; 989e84561cSMike Rapoport int id, ret; 999e84561cSMike Rapoport 1009e84561cSMike Rapoport if (pdata == NULL) 1019e84561cSMike Rapoport return 0; 1029e84561cSMike Rapoport 1039e84561cSMike Rapoport id = pdev->id; 1049e84561cSMike Rapoport 1059e84561cSMike Rapoport if (!((id >= DA9030_ID_LED_1 && id <= DA9030_ID_VIBRA) || 1069e84561cSMike Rapoport (id >= DA9034_ID_LED_1 && id <= DA9034_ID_VIBRA))) { 1079e84561cSMike Rapoport dev_err(&pdev->dev, "invalid LED ID (%d) specified\n", id); 1089e84561cSMike Rapoport return -EINVAL; 1099e84561cSMike Rapoport } 1109e84561cSMike Rapoport 111*bad1c898SBryan Wu led = devm_kzalloc(&pdev->dev, sizeof(struct da903x_led), GFP_KERNEL); 1129e84561cSMike Rapoport if (led == NULL) { 1139e84561cSMike Rapoport dev_err(&pdev->dev, "failed to alloc memory for LED%d\n", id); 1149e84561cSMike Rapoport return -ENOMEM; 1159e84561cSMike Rapoport } 1169e84561cSMike Rapoport 1179e84561cSMike Rapoport led->cdev.name = pdata->name; 1189e84561cSMike Rapoport led->cdev.default_trigger = pdata->default_trigger; 1199e84561cSMike Rapoport led->cdev.brightness_set = da903x_led_set; 1209e84561cSMike Rapoport led->cdev.brightness = LED_OFF; 1219e84561cSMike Rapoport 1229e84561cSMike Rapoport led->id = id; 1239e84561cSMike Rapoport led->flags = pdata->flags; 1249e84561cSMike Rapoport led->master = pdev->dev.parent; 1259e84561cSMike Rapoport led->new_brightness = LED_OFF; 1269e84561cSMike Rapoport 1279e84561cSMike Rapoport INIT_WORK(&led->work, da903x_led_work); 1289e84561cSMike Rapoport 1299e84561cSMike Rapoport ret = led_classdev_register(led->master, &led->cdev); 1309e84561cSMike Rapoport if (ret) { 1319e84561cSMike Rapoport dev_err(&pdev->dev, "failed to register LED %d\n", id); 132*bad1c898SBryan Wu return ret; 1339e84561cSMike Rapoport } 1349e84561cSMike Rapoport 1359e84561cSMike Rapoport platform_set_drvdata(pdev, led); 1369e84561cSMike Rapoport return 0; 1379e84561cSMike Rapoport } 1389e84561cSMike Rapoport 1399e84561cSMike Rapoport static int __devexit da903x_led_remove(struct platform_device *pdev) 1409e84561cSMike Rapoport { 1419e84561cSMike Rapoport struct da903x_led *led = platform_get_drvdata(pdev); 1429e84561cSMike Rapoport 1439e84561cSMike Rapoport led_classdev_unregister(&led->cdev); 1449e84561cSMike Rapoport return 0; 1459e84561cSMike Rapoport } 1469e84561cSMike Rapoport 1479e84561cSMike Rapoport static struct platform_driver da903x_led_driver = { 1489e84561cSMike Rapoport .driver = { 1499e84561cSMike Rapoport .name = "da903x-led", 1509e84561cSMike Rapoport .owner = THIS_MODULE, 1519e84561cSMike Rapoport }, 1529e84561cSMike Rapoport .probe = da903x_led_probe, 1539e84561cSMike Rapoport .remove = __devexit_p(da903x_led_remove), 1549e84561cSMike Rapoport }; 1559e84561cSMike Rapoport 156892a8843SAxel Lin module_platform_driver(da903x_led_driver); 1579e84561cSMike Rapoport 1589e84561cSMike Rapoport MODULE_DESCRIPTION("LEDs driver for Dialog Semiconductor DA9030/DA9034"); 1599e84561cSMike Rapoport MODULE_AUTHOR("Eric Miao <eric.miao@marvell.com>" 1609e84561cSMike Rapoport "Mike Rapoport <mike@compulab.co.il>"); 1619e84561cSMike Rapoport MODULE_LICENSE("GPL"); 1629e84561cSMike Rapoport MODULE_ALIAS("platform:da903x-led"); 163