Mike Rapoport | 923ae48 | 2008-10-13 09:03:44 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Backlight driver for Dialog Semiconductor DA9030/DA9034 |
| 3 | * |
| 4 | * Copyright (C) 2008 Compulab, Ltd. |
| 5 | * Mike Rapoport <mike@compulab.co.il> |
| 6 | * |
| 7 | * Copyright (C) 2006-2008 Marvell International Ltd. |
| 8 | * Eric Miao <eric.miao@marvell.com> |
| 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/kernel.h> |
| 16 | #include <linux/init.h> |
| 17 | #include <linux/platform_device.h> |
| 18 | #include <linux/fb.h> |
| 19 | #include <linux/backlight.h> |
| 20 | #include <linux/mfd/da903x.h> |
| 21 | |
| 22 | #define DA9030_WLED_CONTROL 0x25 |
| 23 | #define DA9030_WLED_CP_EN (1 << 6) |
| 24 | #define DA9030_WLED_TRIM(x) ((x) & 0x7) |
| 25 | |
| 26 | #define DA9034_WLED_CONTROL1 0x3C |
| 27 | #define DA9034_WLED_CONTROL2 0x3D |
Haojian Zhuang | b3a8549 | 2009-11-05 10:27:13 -0500 | [diff] [blame] | 28 | #define DA9034_WLED_ISET(x) ((x) & 0x1f) |
Mike Rapoport | 923ae48 | 2008-10-13 09:03:44 +0100 | [diff] [blame] | 29 | |
| 30 | #define DA9034_WLED_BOOST_EN (1 << 5) |
| 31 | |
| 32 | #define DA9030_MAX_BRIGHTNESS 7 |
| 33 | #define DA9034_MAX_BRIGHTNESS 0x7f |
| 34 | |
| 35 | struct da903x_backlight_data { |
| 36 | struct device *da903x_dev; |
| 37 | int id; |
| 38 | int current_brightness; |
| 39 | }; |
| 40 | |
| 41 | static int da903x_backlight_set(struct backlight_device *bl, int brightness) |
| 42 | { |
| 43 | struct da903x_backlight_data *data = bl_get_data(bl); |
| 44 | struct device *dev = data->da903x_dev; |
| 45 | uint8_t val; |
| 46 | int ret = 0; |
| 47 | |
| 48 | switch (data->id) { |
| 49 | case DA9034_ID_WLED: |
| 50 | ret = da903x_update(dev, DA9034_WLED_CONTROL1, |
| 51 | brightness, 0x7f); |
| 52 | if (ret) |
| 53 | return ret; |
| 54 | |
| 55 | if (data->current_brightness && brightness == 0) |
| 56 | ret = da903x_clr_bits(dev, |
| 57 | DA9034_WLED_CONTROL2, |
| 58 | DA9034_WLED_BOOST_EN); |
| 59 | |
| 60 | if (data->current_brightness == 0 && brightness) |
| 61 | ret = da903x_set_bits(dev, |
| 62 | DA9034_WLED_CONTROL2, |
| 63 | DA9034_WLED_BOOST_EN); |
| 64 | break; |
| 65 | case DA9030_ID_WLED: |
| 66 | val = DA9030_WLED_TRIM(brightness); |
| 67 | val |= brightness ? DA9030_WLED_CP_EN : 0; |
| 68 | ret = da903x_write(dev, DA9030_WLED_CONTROL, val); |
| 69 | break; |
| 70 | } |
| 71 | |
| 72 | if (ret) |
| 73 | return ret; |
| 74 | |
| 75 | data->current_brightness = brightness; |
| 76 | return 0; |
| 77 | } |
| 78 | |
| 79 | static int da903x_backlight_update_status(struct backlight_device *bl) |
| 80 | { |
| 81 | int brightness = bl->props.brightness; |
| 82 | |
| 83 | if (bl->props.power != FB_BLANK_UNBLANK) |
| 84 | brightness = 0; |
| 85 | |
| 86 | if (bl->props.fb_blank != FB_BLANK_UNBLANK) |
| 87 | brightness = 0; |
| 88 | |
| 89 | return da903x_backlight_set(bl, brightness); |
| 90 | } |
| 91 | |
| 92 | static int da903x_backlight_get_brightness(struct backlight_device *bl) |
| 93 | { |
| 94 | struct da903x_backlight_data *data = bl_get_data(bl); |
| 95 | return data->current_brightness; |
| 96 | } |
| 97 | |
| 98 | static struct backlight_ops da903x_backlight_ops = { |
| 99 | .update_status = da903x_backlight_update_status, |
| 100 | .get_brightness = da903x_backlight_get_brightness, |
| 101 | }; |
| 102 | |
| 103 | static int da903x_backlight_probe(struct platform_device *pdev) |
| 104 | { |
Haojian Zhuang | b3a8549 | 2009-11-05 10:27:13 -0500 | [diff] [blame] | 105 | struct da9034_backlight_pdata *pdata = pdev->dev.platform_data; |
Mike Rapoport | 923ae48 | 2008-10-13 09:03:44 +0100 | [diff] [blame] | 106 | struct da903x_backlight_data *data; |
| 107 | struct backlight_device *bl; |
| 108 | int max_brightness; |
| 109 | |
| 110 | data = kzalloc(sizeof(*data), GFP_KERNEL); |
| 111 | if (data == NULL) |
| 112 | return -ENOMEM; |
| 113 | |
| 114 | switch (pdev->id) { |
| 115 | case DA9030_ID_WLED: |
| 116 | max_brightness = DA9030_MAX_BRIGHTNESS; |
| 117 | break; |
| 118 | case DA9034_ID_WLED: |
| 119 | max_brightness = DA9034_MAX_BRIGHTNESS; |
| 120 | break; |
| 121 | default: |
| 122 | dev_err(&pdev->dev, "invalid backlight device ID(%d)\n", |
| 123 | pdev->id); |
Julia Lawall | c0d861a | 2008-11-19 15:36:41 -0800 | [diff] [blame] | 124 | kfree(data); |
Mike Rapoport | 923ae48 | 2008-10-13 09:03:44 +0100 | [diff] [blame] | 125 | return -EINVAL; |
| 126 | } |
| 127 | |
| 128 | data->id = pdev->id; |
| 129 | data->da903x_dev = pdev->dev.parent; |
| 130 | data->current_brightness = 0; |
| 131 | |
Haojian Zhuang | b3a8549 | 2009-11-05 10:27:13 -0500 | [diff] [blame] | 132 | /* adjust the WLED output current */ |
| 133 | if (pdata) |
| 134 | da903x_write(data->da903x_dev, DA9034_WLED_CONTROL2, |
| 135 | DA9034_WLED_ISET(pdata->output_current)); |
| 136 | |
Mike Rapoport | 923ae48 | 2008-10-13 09:03:44 +0100 | [diff] [blame] | 137 | bl = backlight_device_register(pdev->name, data->da903x_dev, |
| 138 | data, &da903x_backlight_ops); |
| 139 | if (IS_ERR(bl)) { |
| 140 | dev_err(&pdev->dev, "failed to register backlight\n"); |
Mike Rapoport | f55491a | 2008-11-19 15:36:49 -0800 | [diff] [blame] | 141 | kfree(data); |
Mike Rapoport | 923ae48 | 2008-10-13 09:03:44 +0100 | [diff] [blame] | 142 | return PTR_ERR(bl); |
| 143 | } |
| 144 | |
| 145 | bl->props.max_brightness = max_brightness; |
| 146 | bl->props.brightness = max_brightness; |
| 147 | |
| 148 | platform_set_drvdata(pdev, bl); |
| 149 | backlight_update_status(bl); |
| 150 | return 0; |
| 151 | } |
| 152 | |
| 153 | static int da903x_backlight_remove(struct platform_device *pdev) |
| 154 | { |
| 155 | struct backlight_device *bl = platform_get_drvdata(pdev); |
| 156 | struct da903x_backlight_data *data = bl_get_data(bl); |
| 157 | |
| 158 | backlight_device_unregister(bl); |
| 159 | kfree(data); |
| 160 | return 0; |
| 161 | } |
| 162 | |
| 163 | #ifdef CONFIG_PM |
Mike Rapoport | 43ddc00 | 2009-07-21 16:57:47 +0300 | [diff] [blame] | 164 | static int da903x_backlight_suspend(struct device *dev) |
Mike Rapoport | 923ae48 | 2008-10-13 09:03:44 +0100 | [diff] [blame] | 165 | { |
Mike Rapoport | 43ddc00 | 2009-07-21 16:57:47 +0300 | [diff] [blame] | 166 | struct platform_device *pdev = to_platform_device(dev); |
Mike Rapoport | 923ae48 | 2008-10-13 09:03:44 +0100 | [diff] [blame] | 167 | struct backlight_device *bl = platform_get_drvdata(pdev); |
| 168 | return da903x_backlight_set(bl, 0); |
| 169 | } |
| 170 | |
Mike Rapoport | 43ddc00 | 2009-07-21 16:57:47 +0300 | [diff] [blame] | 171 | static int da903x_backlight_resume(struct device *dev) |
Mike Rapoport | 923ae48 | 2008-10-13 09:03:44 +0100 | [diff] [blame] | 172 | { |
Mike Rapoport | 43ddc00 | 2009-07-21 16:57:47 +0300 | [diff] [blame] | 173 | struct platform_device *pdev = to_platform_device(dev); |
Mike Rapoport | 923ae48 | 2008-10-13 09:03:44 +0100 | [diff] [blame] | 174 | struct backlight_device *bl = platform_get_drvdata(pdev); |
| 175 | |
| 176 | backlight_update_status(bl); |
| 177 | return 0; |
| 178 | } |
Mike Rapoport | 43ddc00 | 2009-07-21 16:57:47 +0300 | [diff] [blame] | 179 | |
Alexey Dobriyan | 4714521 | 2009-12-14 18:00:08 -0800 | [diff] [blame^] | 180 | static const struct dev_pm_ops da903x_backlight_pm_ops = { |
Mike Rapoport | 43ddc00 | 2009-07-21 16:57:47 +0300 | [diff] [blame] | 181 | .suspend = da903x_backlight_suspend, |
| 182 | .resume = da903x_backlight_resume, |
| 183 | }; |
Mike Rapoport | 923ae48 | 2008-10-13 09:03:44 +0100 | [diff] [blame] | 184 | #endif |
| 185 | |
| 186 | static struct platform_driver da903x_backlight_driver = { |
| 187 | .driver = { |
| 188 | .name = "da903x-backlight", |
| 189 | .owner = THIS_MODULE, |
Mike Rapoport | 43ddc00 | 2009-07-21 16:57:47 +0300 | [diff] [blame] | 190 | #ifdef CONFIG_PM |
| 191 | .pm = &da903x_backlight_pm_ops, |
| 192 | #endif |
Mike Rapoport | 923ae48 | 2008-10-13 09:03:44 +0100 | [diff] [blame] | 193 | }, |
| 194 | .probe = da903x_backlight_probe, |
| 195 | .remove = da903x_backlight_remove, |
Mike Rapoport | 923ae48 | 2008-10-13 09:03:44 +0100 | [diff] [blame] | 196 | }; |
| 197 | |
| 198 | static int __init da903x_backlight_init(void) |
| 199 | { |
| 200 | return platform_driver_register(&da903x_backlight_driver); |
| 201 | } |
| 202 | module_init(da903x_backlight_init); |
| 203 | |
| 204 | static void __exit da903x_backlight_exit(void) |
| 205 | { |
| 206 | platform_driver_unregister(&da903x_backlight_driver); |
| 207 | } |
| 208 | module_exit(da903x_backlight_exit); |
| 209 | |
| 210 | MODULE_DESCRIPTION("Backlight Driver for Dialog Semiconductor DA9030/DA9034"); |
| 211 | MODULE_AUTHOR("Eric Miao <eric.miao@marvell.com>" |
| 212 | "Mike Rapoport <mike@compulab.co.il>"); |
| 213 | MODULE_LICENSE("GPL"); |
| 214 | MODULE_ALIAS("platform:da903x-backlight"); |