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