Michael Hennerich | ed4a10b | 2009-10-08 04:00:19 -0400 | [diff] [blame] | 1 | /* |
| 2 | * LEDs driver for Analog Devices ADP5520/ADP5501 MFD PMICs |
| 3 | * |
| 4 | * Copyright 2009 Analog Devices Inc. |
| 5 | * |
| 6 | * Loosely derived from leds-da903x: |
| 7 | * Copyright (C) 2008 Compulab, Ltd. |
| 8 | * Mike Rapoport <mike@compulab.co.il> |
| 9 | * |
| 10 | * Copyright (C) 2006-2008 Marvell International Ltd. |
| 11 | * Eric Miao <eric.miao@marvell.com> |
| 12 | * |
| 13 | * Licensed under the GPL-2 or later. |
| 14 | */ |
| 15 | |
| 16 | #include <linux/module.h> |
| 17 | #include <linux/kernel.h> |
| 18 | #include <linux/init.h> |
| 19 | #include <linux/platform_device.h> |
| 20 | #include <linux/leds.h> |
| 21 | #include <linux/workqueue.h> |
| 22 | #include <linux/mfd/adp5520.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 23 | #include <linux/slab.h> |
Michael Hennerich | ed4a10b | 2009-10-08 04:00:19 -0400 | [diff] [blame] | 24 | |
| 25 | struct adp5520_led { |
| 26 | struct led_classdev cdev; |
| 27 | struct work_struct work; |
| 28 | struct device *master; |
| 29 | enum led_brightness new_brightness; |
| 30 | int id; |
| 31 | int flags; |
| 32 | }; |
| 33 | |
| 34 | static void adp5520_led_work(struct work_struct *work) |
| 35 | { |
| 36 | struct adp5520_led *led = container_of(work, struct adp5520_led, work); |
| 37 | adp5520_write(led->master, ADP5520_LED1_CURRENT + led->id - 1, |
| 38 | led->new_brightness >> 2); |
| 39 | } |
| 40 | |
| 41 | static void adp5520_led_set(struct led_classdev *led_cdev, |
| 42 | enum led_brightness value) |
| 43 | { |
| 44 | struct adp5520_led *led; |
| 45 | |
| 46 | led = container_of(led_cdev, struct adp5520_led, cdev); |
| 47 | led->new_brightness = value; |
| 48 | schedule_work(&led->work); |
| 49 | } |
| 50 | |
| 51 | static int adp5520_led_setup(struct adp5520_led *led) |
| 52 | { |
| 53 | struct device *dev = led->master; |
| 54 | int flags = led->flags; |
| 55 | int ret = 0; |
| 56 | |
| 57 | switch (led->id) { |
| 58 | case FLAG_ID_ADP5520_LED1_ADP5501_LED0: |
| 59 | ret |= adp5520_set_bits(dev, ADP5520_LED_TIME, |
| 60 | (flags >> ADP5520_FLAG_OFFT_SHIFT) & |
| 61 | ADP5520_FLAG_OFFT_MASK); |
| 62 | ret |= adp5520_set_bits(dev, ADP5520_LED_CONTROL, |
| 63 | ADP5520_LED1_EN); |
| 64 | break; |
| 65 | case FLAG_ID_ADP5520_LED2_ADP5501_LED1: |
| 66 | ret |= adp5520_set_bits(dev, ADP5520_LED_TIME, |
| 67 | ((flags >> ADP5520_FLAG_OFFT_SHIFT) & |
| 68 | ADP5520_FLAG_OFFT_MASK) << 2); |
| 69 | ret |= adp5520_clr_bits(dev, ADP5520_LED_CONTROL, |
| 70 | ADP5520_R3_MODE); |
| 71 | ret |= adp5520_set_bits(dev, ADP5520_LED_CONTROL, |
| 72 | ADP5520_LED2_EN); |
| 73 | break; |
| 74 | case FLAG_ID_ADP5520_LED3_ADP5501_LED2: |
| 75 | ret |= adp5520_set_bits(dev, ADP5520_LED_TIME, |
| 76 | ((flags >> ADP5520_FLAG_OFFT_SHIFT) & |
| 77 | ADP5520_FLAG_OFFT_MASK) << 4); |
| 78 | ret |= adp5520_clr_bits(dev, ADP5520_LED_CONTROL, |
| 79 | ADP5520_C3_MODE); |
| 80 | ret |= adp5520_set_bits(dev, ADP5520_LED_CONTROL, |
| 81 | ADP5520_LED3_EN); |
| 82 | break; |
| 83 | } |
| 84 | |
| 85 | return ret; |
| 86 | } |
| 87 | |
| 88 | static int __devinit adp5520_led_prepare(struct platform_device *pdev) |
| 89 | { |
| 90 | struct adp5520_leds_platform_data *pdata = pdev->dev.platform_data; |
| 91 | struct device *dev = pdev->dev.parent; |
| 92 | int ret = 0; |
| 93 | |
| 94 | ret |= adp5520_write(dev, ADP5520_LED1_CURRENT, 0); |
| 95 | ret |= adp5520_write(dev, ADP5520_LED2_CURRENT, 0); |
| 96 | ret |= adp5520_write(dev, ADP5520_LED3_CURRENT, 0); |
| 97 | ret |= adp5520_write(dev, ADP5520_LED_TIME, pdata->led_on_time << 6); |
| 98 | ret |= adp5520_write(dev, ADP5520_LED_FADE, FADE_VAL(pdata->fade_in, |
| 99 | pdata->fade_out)); |
| 100 | |
| 101 | return ret; |
| 102 | } |
| 103 | |
| 104 | static int __devinit adp5520_led_probe(struct platform_device *pdev) |
| 105 | { |
| 106 | struct adp5520_leds_platform_data *pdata = pdev->dev.platform_data; |
| 107 | struct adp5520_led *led, *led_dat; |
| 108 | struct led_info *cur_led; |
| 109 | int ret, i; |
| 110 | |
| 111 | if (pdata == NULL) { |
| 112 | dev_err(&pdev->dev, "missing platform data\n"); |
| 113 | return -ENODEV; |
| 114 | } |
| 115 | |
| 116 | if (pdata->num_leds > ADP5520_01_MAXLEDS) { |
| 117 | dev_err(&pdev->dev, "can't handle more than %d LEDS\n", |
| 118 | ADP5520_01_MAXLEDS); |
| 119 | return -EFAULT; |
| 120 | } |
| 121 | |
| 122 | led = kzalloc(sizeof(*led) * pdata->num_leds, GFP_KERNEL); |
| 123 | if (led == NULL) { |
| 124 | dev_err(&pdev->dev, "failed to alloc memory\n"); |
| 125 | return -ENOMEM; |
| 126 | } |
| 127 | |
| 128 | ret = adp5520_led_prepare(pdev); |
| 129 | |
| 130 | if (ret) { |
| 131 | dev_err(&pdev->dev, "failed to write\n"); |
| 132 | goto err_free; |
| 133 | } |
| 134 | |
| 135 | for (i = 0; i < pdata->num_leds; ++i) { |
| 136 | cur_led = &pdata->leds[i]; |
| 137 | led_dat = &led[i]; |
| 138 | |
| 139 | led_dat->cdev.name = cur_led->name; |
| 140 | led_dat->cdev.default_trigger = cur_led->default_trigger; |
| 141 | led_dat->cdev.brightness_set = adp5520_led_set; |
| 142 | led_dat->cdev.brightness = LED_OFF; |
| 143 | |
| 144 | if (cur_led->flags & ADP5520_FLAG_LED_MASK) |
| 145 | led_dat->flags = cur_led->flags; |
| 146 | else |
| 147 | led_dat->flags = i + 1; |
| 148 | |
| 149 | led_dat->id = led_dat->flags & ADP5520_FLAG_LED_MASK; |
| 150 | |
| 151 | led_dat->master = pdev->dev.parent; |
| 152 | led_dat->new_brightness = LED_OFF; |
| 153 | |
| 154 | INIT_WORK(&led_dat->work, adp5520_led_work); |
| 155 | |
| 156 | ret = led_classdev_register(led_dat->master, &led_dat->cdev); |
| 157 | if (ret) { |
| 158 | dev_err(&pdev->dev, "failed to register LED %d\n", |
| 159 | led_dat->id); |
| 160 | goto err; |
| 161 | } |
| 162 | |
| 163 | ret = adp5520_led_setup(led_dat); |
| 164 | if (ret) { |
| 165 | dev_err(&pdev->dev, "failed to write\n"); |
| 166 | i++; |
| 167 | goto err; |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | platform_set_drvdata(pdev, led); |
| 172 | return 0; |
| 173 | |
| 174 | err: |
| 175 | if (i > 0) { |
| 176 | for (i = i - 1; i >= 0; i--) { |
| 177 | led_classdev_unregister(&led[i].cdev); |
| 178 | cancel_work_sync(&led[i].work); |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | err_free: |
| 183 | kfree(led); |
| 184 | return ret; |
| 185 | } |
| 186 | |
| 187 | static int __devexit adp5520_led_remove(struct platform_device *pdev) |
| 188 | { |
| 189 | struct adp5520_leds_platform_data *pdata = pdev->dev.platform_data; |
| 190 | struct adp5520_led *led; |
| 191 | int i; |
| 192 | |
| 193 | led = platform_get_drvdata(pdev); |
| 194 | |
| 195 | adp5520_clr_bits(led->master, ADP5520_LED_CONTROL, |
| 196 | ADP5520_LED1_EN | ADP5520_LED2_EN | ADP5520_LED3_EN); |
| 197 | |
| 198 | for (i = 0; i < pdata->num_leds; i++) { |
| 199 | led_classdev_unregister(&led[i].cdev); |
| 200 | cancel_work_sync(&led[i].work); |
| 201 | } |
| 202 | |
| 203 | kfree(led); |
| 204 | return 0; |
| 205 | } |
| 206 | |
| 207 | static struct platform_driver adp5520_led_driver = { |
| 208 | .driver = { |
| 209 | .name = "adp5520-led", |
| 210 | .owner = THIS_MODULE, |
| 211 | }, |
| 212 | .probe = adp5520_led_probe, |
| 213 | .remove = __devexit_p(adp5520_led_remove), |
| 214 | }; |
| 215 | |
Axel Lin | 892a884 | 2012-01-10 15:09:24 -0800 | [diff] [blame] | 216 | module_platform_driver(adp5520_led_driver); |
Michael Hennerich | ed4a10b | 2009-10-08 04:00:19 -0400 | [diff] [blame] | 217 | |
| 218 | MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>"); |
| 219 | MODULE_DESCRIPTION("LEDS ADP5520(01) Driver"); |
| 220 | MODULE_LICENSE("GPL"); |
| 221 | MODULE_ALIAS("platform:adp5520-led"); |