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