Mike Rapoport | 9e84561 | 2008-10-13 09:06:10 +0100 | [diff] [blame] | 1 | /* |
| 2 | * LEDs driver for Dialog Semiconductor DA9030/DA9034 |
| 3 | * |
| 4 | * Copyright (C) 2008 Compulab, Ltd. |
Sachin Kamat | 141bfee | 2012-11-25 11:16:28 +0530 | [diff] [blame] | 5 | * Mike Rapoport <mike@compulab.co.il> |
Mike Rapoport | 9e84561 | 2008-10-13 09:06:10 +0100 | [diff] [blame] | 6 | * |
| 7 | * Copyright (C) 2006-2008 Marvell International Ltd. |
Sachin Kamat | 141bfee | 2012-11-25 11:16:28 +0530 | [diff] [blame] | 8 | * Eric Miao <eric.miao@marvell.com> |
Mike Rapoport | 9e84561 | 2008-10-13 09:06:10 +0100 | [diff] [blame] | 9 | * |
| 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License version 2 as |
| 12 | * published by the Free Software Foundation. |
| 13 | */ |
| 14 | |
| 15 | #include <linux/module.h> |
| 16 | #include <linux/kernel.h> |
Mike Rapoport | 9e84561 | 2008-10-13 09:06:10 +0100 | [diff] [blame] | 17 | #include <linux/platform_device.h> |
| 18 | #include <linux/leds.h> |
Eric Miao | b9b54aa | 2008-10-27 17:48:50 +0800 | [diff] [blame] | 19 | #include <linux/workqueue.h> |
Mike Rapoport | 9e84561 | 2008-10-13 09:06:10 +0100 | [diff] [blame] | 20 | #include <linux/mfd/da903x.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 21 | #include <linux/slab.h> |
Mike Rapoport | 9e84561 | 2008-10-13 09:06:10 +0100 | [diff] [blame] | 22 | |
| 23 | #define DA9030_LED1_CONTROL 0x20 |
| 24 | #define DA9030_LED2_CONTROL 0x21 |
| 25 | #define DA9030_LED3_CONTROL 0x22 |
| 26 | #define DA9030_LED4_CONTROL 0x23 |
| 27 | #define DA9030_LEDPC_CONTROL 0x24 |
| 28 | #define DA9030_MISC_CONTROL_A 0x26 /* Vibrator Control */ |
| 29 | |
| 30 | #define DA9034_LED1_CONTROL 0x35 |
| 31 | #define DA9034_LED2_CONTROL 0x36 |
| 32 | #define DA9034_VIBRA 0x40 |
| 33 | |
| 34 | struct da903x_led { |
| 35 | struct led_classdev cdev; |
| 36 | struct work_struct work; |
| 37 | struct device *master; |
| 38 | enum led_brightness new_brightness; |
| 39 | int id; |
| 40 | int flags; |
| 41 | }; |
| 42 | |
| 43 | #define DA9030_LED_OFFSET(id) ((id) - DA9030_ID_LED_1) |
| 44 | #define DA9034_LED_OFFSET(id) ((id) - DA9034_ID_LED_1) |
| 45 | |
| 46 | static void da903x_led_work(struct work_struct *work) |
| 47 | { |
| 48 | struct da903x_led *led = container_of(work, struct da903x_led, work); |
| 49 | uint8_t val; |
| 50 | int offset; |
| 51 | |
| 52 | switch (led->id) { |
| 53 | case DA9030_ID_LED_1: |
| 54 | case DA9030_ID_LED_2: |
| 55 | case DA9030_ID_LED_3: |
| 56 | case DA9030_ID_LED_4: |
| 57 | case DA9030_ID_LED_PC: |
| 58 | offset = DA9030_LED_OFFSET(led->id); |
| 59 | val = led->flags & ~0x87; |
| 60 | val |= (led->new_brightness) ? 0x80 : 0; /* EN bit */ |
Jonathan Cameron | a0601c8 | 2008-10-30 11:05:00 +0000 | [diff] [blame] | 61 | val |= (0x7 - (led->new_brightness >> 5)) & 0x7; /* PWM<2:0> */ |
Mike Rapoport | 9e84561 | 2008-10-13 09:06:10 +0100 | [diff] [blame] | 62 | da903x_write(led->master, DA9030_LED1_CONTROL + offset, val); |
| 63 | break; |
| 64 | case DA9030_ID_VIBRA: |
| 65 | val = led->flags & ~0x80; |
| 66 | val |= (led->new_brightness) ? 0x80 : 0; /* EN bit */ |
| 67 | da903x_write(led->master, DA9030_MISC_CONTROL_A, val); |
| 68 | break; |
| 69 | case DA9034_ID_LED_1: |
| 70 | case DA9034_ID_LED_2: |
| 71 | offset = DA9034_LED_OFFSET(led->id); |
| 72 | val = (led->new_brightness * 0x5f / LED_FULL) & 0x7f; |
| 73 | val |= (led->flags & DA9034_LED_RAMP) ? 0x80 : 0; |
| 74 | da903x_write(led->master, DA9034_LED1_CONTROL + offset, val); |
| 75 | break; |
| 76 | case DA9034_ID_VIBRA: |
| 77 | val = led->new_brightness & 0xfe; |
| 78 | da903x_write(led->master, DA9034_VIBRA, val); |
| 79 | break; |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | static void da903x_led_set(struct led_classdev *led_cdev, |
| 84 | enum led_brightness value) |
| 85 | { |
| 86 | struct da903x_led *led; |
Sachin Kamat | 141bfee | 2012-11-25 11:16:28 +0530 | [diff] [blame] | 87 | |
Mike Rapoport | 9e84561 | 2008-10-13 09:06:10 +0100 | [diff] [blame] | 88 | led = container_of(led_cdev, struct da903x_led, cdev); |
| 89 | led->new_brightness = value; |
| 90 | schedule_work(&led->work); |
| 91 | } |
| 92 | |
Bill Pemberton | 98ea1ea | 2012-11-19 13:23:02 -0500 | [diff] [blame] | 93 | static int da903x_led_probe(struct platform_device *pdev) |
Mike Rapoport | 9e84561 | 2008-10-13 09:06:10 +0100 | [diff] [blame] | 94 | { |
Jingoo Han | 87aae1e | 2013-07-30 01:07:35 -0700 | [diff] [blame] | 95 | struct led_info *pdata = dev_get_platdata(&pdev->dev); |
Mike Rapoport | 9e84561 | 2008-10-13 09:06:10 +0100 | [diff] [blame] | 96 | struct da903x_led *led; |
| 97 | int id, ret; |
| 98 | |
| 99 | if (pdata == NULL) |
| 100 | return 0; |
| 101 | |
| 102 | id = pdev->id; |
| 103 | |
| 104 | if (!((id >= DA9030_ID_LED_1 && id <= DA9030_ID_VIBRA) || |
| 105 | (id >= DA9034_ID_LED_1 && id <= DA9034_ID_VIBRA))) { |
| 106 | dev_err(&pdev->dev, "invalid LED ID (%d) specified\n", id); |
| 107 | return -EINVAL; |
| 108 | } |
| 109 | |
Bryan Wu | bad1c89 | 2012-07-03 13:07:53 +0800 | [diff] [blame] | 110 | led = devm_kzalloc(&pdev->dev, sizeof(struct da903x_led), GFP_KERNEL); |
Xiubo Li | 0c9a03b | 2014-05-07 20:25:52 -0700 | [diff] [blame] | 111 | if (!led) |
Mike Rapoport | 9e84561 | 2008-10-13 09:06:10 +0100 | [diff] [blame] | 112 | return -ENOMEM; |
Mike Rapoport | 9e84561 | 2008-10-13 09:06:10 +0100 | [diff] [blame] | 113 | |
| 114 | led->cdev.name = pdata->name; |
| 115 | led->cdev.default_trigger = pdata->default_trigger; |
| 116 | led->cdev.brightness_set = da903x_led_set; |
| 117 | led->cdev.brightness = LED_OFF; |
| 118 | |
| 119 | led->id = id; |
| 120 | led->flags = pdata->flags; |
| 121 | led->master = pdev->dev.parent; |
| 122 | led->new_brightness = LED_OFF; |
| 123 | |
| 124 | INIT_WORK(&led->work, da903x_led_work); |
| 125 | |
| 126 | ret = led_classdev_register(led->master, &led->cdev); |
| 127 | if (ret) { |
| 128 | dev_err(&pdev->dev, "failed to register LED %d\n", id); |
Bryan Wu | bad1c89 | 2012-07-03 13:07:53 +0800 | [diff] [blame] | 129 | return ret; |
Mike Rapoport | 9e84561 | 2008-10-13 09:06:10 +0100 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | platform_set_drvdata(pdev, led); |
| 133 | return 0; |
Mike Rapoport | 9e84561 | 2008-10-13 09:06:10 +0100 | [diff] [blame] | 134 | } |
| 135 | |
Bill Pemberton | 678e8a6 | 2012-11-19 13:26:00 -0500 | [diff] [blame] | 136 | static int da903x_led_remove(struct platform_device *pdev) |
Mike Rapoport | 9e84561 | 2008-10-13 09:06:10 +0100 | [diff] [blame] | 137 | { |
| 138 | struct da903x_led *led = platform_get_drvdata(pdev); |
| 139 | |
| 140 | led_classdev_unregister(&led->cdev); |
Mike Rapoport | 9e84561 | 2008-10-13 09:06:10 +0100 | [diff] [blame] | 141 | return 0; |
| 142 | } |
| 143 | |
| 144 | static struct platform_driver da903x_led_driver = { |
| 145 | .driver = { |
| 146 | .name = "da903x-led", |
Mike Rapoport | 9e84561 | 2008-10-13 09:06:10 +0100 | [diff] [blame] | 147 | }, |
| 148 | .probe = da903x_led_probe, |
Bill Pemberton | df07cf8 | 2012-11-19 13:20:20 -0500 | [diff] [blame] | 149 | .remove = da903x_led_remove, |
Mike Rapoport | 9e84561 | 2008-10-13 09:06:10 +0100 | [diff] [blame] | 150 | }; |
| 151 | |
Axel Lin | 892a884 | 2012-01-10 15:09:24 -0800 | [diff] [blame] | 152 | module_platform_driver(da903x_led_driver); |
Mike Rapoport | 9e84561 | 2008-10-13 09:06:10 +0100 | [diff] [blame] | 153 | |
| 154 | MODULE_DESCRIPTION("LEDs driver for Dialog Semiconductor DA9030/DA9034"); |
Sachin Kamat | 141bfee | 2012-11-25 11:16:28 +0530 | [diff] [blame] | 155 | MODULE_AUTHOR("Eric Miao <eric.miao@marvell.com>"); |
| 156 | MODULE_AUTHOR("Mike Rapoport <mike@compulab.co.il>"); |
Mike Rapoport | 9e84561 | 2008-10-13 09:06:10 +0100 | [diff] [blame] | 157 | MODULE_LICENSE("GPL"); |
| 158 | MODULE_ALIAS("platform:da903x-led"); |