Andrzej Zaborowski | ec072b0 | 2008-01-13 23:15:43 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Backlight driver for OMAP based boards. |
| 3 | * |
| 4 | * Copyright (c) 2006 Andrzej Zaborowski <balrog@zabor.org> |
| 5 | * |
| 6 | * This package is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This package is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this package; if not, write to the Free Software |
| 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 19 | */ |
| 20 | |
| 21 | #include <linux/module.h> |
| 22 | #include <linux/kernel.h> |
| 23 | #include <linux/init.h> |
| 24 | #include <linux/platform_device.h> |
| 25 | #include <linux/fb.h> |
| 26 | #include <linux/backlight.h> |
| 27 | |
Russell King | a09e64f | 2008-08-05 16:14:15 +0100 | [diff] [blame] | 28 | #include <mach/hardware.h> |
Tony Lindgren | ce491cf | 2009-10-20 09:40:47 -0700 | [diff] [blame] | 29 | #include <plat/board.h> |
| 30 | #include <plat/mux.h> |
Andrzej Zaborowski | ec072b0 | 2008-01-13 23:15:43 +0000 | [diff] [blame] | 31 | |
| 32 | #define OMAPBL_MAX_INTENSITY 0xff |
| 33 | |
| 34 | struct omap_backlight { |
| 35 | int powermode; |
| 36 | int current_intensity; |
| 37 | |
| 38 | struct device *dev; |
| 39 | struct omap_backlight_config *pdata; |
| 40 | }; |
| 41 | |
| 42 | static void inline omapbl_send_intensity(int intensity) |
| 43 | { |
| 44 | omap_writeb(intensity, OMAP_PWL_ENABLE); |
| 45 | } |
| 46 | |
| 47 | static void inline omapbl_send_enable(int enable) |
| 48 | { |
| 49 | omap_writeb(enable, OMAP_PWL_CLK_ENABLE); |
| 50 | } |
| 51 | |
| 52 | static void omapbl_blank(struct omap_backlight *bl, int mode) |
| 53 | { |
| 54 | if (bl->pdata->set_power) |
| 55 | bl->pdata->set_power(bl->dev, mode); |
| 56 | |
| 57 | switch (mode) { |
| 58 | case FB_BLANK_NORMAL: |
| 59 | case FB_BLANK_VSYNC_SUSPEND: |
| 60 | case FB_BLANK_HSYNC_SUSPEND: |
| 61 | case FB_BLANK_POWERDOWN: |
| 62 | omapbl_send_intensity(0); |
| 63 | omapbl_send_enable(0); |
| 64 | break; |
| 65 | |
| 66 | case FB_BLANK_UNBLANK: |
| 67 | omapbl_send_intensity(bl->current_intensity); |
| 68 | omapbl_send_enable(1); |
| 69 | break; |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | #ifdef CONFIG_PM |
| 74 | static int omapbl_suspend(struct platform_device *pdev, pm_message_t state) |
| 75 | { |
| 76 | struct backlight_device *dev = platform_get_drvdata(pdev); |
| 77 | struct omap_backlight *bl = dev_get_drvdata(&dev->dev); |
| 78 | |
| 79 | omapbl_blank(bl, FB_BLANK_POWERDOWN); |
| 80 | return 0; |
| 81 | } |
| 82 | |
| 83 | static int omapbl_resume(struct platform_device *pdev) |
| 84 | { |
| 85 | struct backlight_device *dev = platform_get_drvdata(pdev); |
| 86 | struct omap_backlight *bl = dev_get_drvdata(&dev->dev); |
| 87 | |
| 88 | omapbl_blank(bl, bl->powermode); |
| 89 | return 0; |
| 90 | } |
| 91 | #else |
| 92 | #define omapbl_suspend NULL |
| 93 | #define omapbl_resume NULL |
| 94 | #endif |
| 95 | |
| 96 | static int omapbl_set_power(struct backlight_device *dev, int state) |
| 97 | { |
| 98 | struct omap_backlight *bl = dev_get_drvdata(&dev->dev); |
| 99 | |
| 100 | omapbl_blank(bl, state); |
| 101 | bl->powermode = state; |
| 102 | |
| 103 | return 0; |
| 104 | } |
| 105 | |
| 106 | static int omapbl_update_status(struct backlight_device *dev) |
| 107 | { |
| 108 | struct omap_backlight *bl = dev_get_drvdata(&dev->dev); |
| 109 | |
| 110 | if (bl->current_intensity != dev->props.brightness) { |
| 111 | if (bl->powermode == FB_BLANK_UNBLANK) |
| 112 | omapbl_send_intensity(dev->props.brightness); |
| 113 | bl->current_intensity = dev->props.brightness; |
| 114 | } |
| 115 | |
| 116 | if (dev->props.fb_blank != bl->powermode) |
| 117 | omapbl_set_power(dev, dev->props.fb_blank); |
| 118 | |
| 119 | return 0; |
| 120 | } |
| 121 | |
| 122 | static int omapbl_get_intensity(struct backlight_device *dev) |
| 123 | { |
| 124 | struct omap_backlight *bl = dev_get_drvdata(&dev->dev); |
| 125 | return bl->current_intensity; |
| 126 | } |
| 127 | |
Emese Revfy | 9905a43 | 2009-12-14 00:58:57 +0100 | [diff] [blame] | 128 | static const struct backlight_ops omapbl_ops = { |
Andrzej Zaborowski | ec072b0 | 2008-01-13 23:15:43 +0000 | [diff] [blame] | 129 | .get_brightness = omapbl_get_intensity, |
| 130 | .update_status = omapbl_update_status, |
| 131 | }; |
| 132 | |
| 133 | static int omapbl_probe(struct platform_device *pdev) |
| 134 | { |
Matthew Garrett | a19a6ee | 2010-02-17 16:39:44 -0500 | [diff] [blame^] | 135 | struct backlight_properties props; |
Andrzej Zaborowski | ec072b0 | 2008-01-13 23:15:43 +0000 | [diff] [blame] | 136 | struct backlight_device *dev; |
| 137 | struct omap_backlight *bl; |
| 138 | struct omap_backlight_config *pdata = pdev->dev.platform_data; |
| 139 | |
| 140 | if (!pdata) |
| 141 | return -ENXIO; |
| 142 | |
Andrzej Zaborowski | ec072b0 | 2008-01-13 23:15:43 +0000 | [diff] [blame] | 143 | bl = kzalloc(sizeof(struct omap_backlight), GFP_KERNEL); |
| 144 | if (unlikely(!bl)) |
| 145 | return -ENOMEM; |
| 146 | |
Matthew Garrett | a19a6ee | 2010-02-17 16:39:44 -0500 | [diff] [blame^] | 147 | memset(&props, 0, sizeof(struct backlight_properties)); |
| 148 | props.max_brightness = OMAPBL_MAX_INTENSITY; |
| 149 | dev = backlight_device_register("omap-bl", &pdev->dev, bl, &omapbl_ops, |
| 150 | &props); |
Andrzej Zaborowski | ec072b0 | 2008-01-13 23:15:43 +0000 | [diff] [blame] | 151 | if (IS_ERR(dev)) { |
| 152 | kfree(bl); |
| 153 | return PTR_ERR(dev); |
| 154 | } |
| 155 | |
| 156 | bl->powermode = FB_BLANK_POWERDOWN; |
| 157 | bl->current_intensity = 0; |
| 158 | |
| 159 | bl->pdata = pdata; |
| 160 | bl->dev = &pdev->dev; |
| 161 | |
| 162 | platform_set_drvdata(pdev, dev); |
| 163 | |
| 164 | omap_cfg_reg(PWL); /* Conflicts with UART3 */ |
| 165 | |
| 166 | dev->props.fb_blank = FB_BLANK_UNBLANK; |
Andrzej Zaborowski | ec072b0 | 2008-01-13 23:15:43 +0000 | [diff] [blame] | 167 | dev->props.brightness = pdata->default_intensity; |
| 168 | omapbl_update_status(dev); |
| 169 | |
| 170 | printk(KERN_INFO "OMAP LCD backlight initialised\n"); |
| 171 | |
| 172 | return 0; |
| 173 | } |
| 174 | |
| 175 | static int omapbl_remove(struct platform_device *pdev) |
| 176 | { |
| 177 | struct backlight_device *dev = platform_get_drvdata(pdev); |
| 178 | struct omap_backlight *bl = dev_get_drvdata(&dev->dev); |
| 179 | |
| 180 | backlight_device_unregister(dev); |
| 181 | kfree(bl); |
| 182 | |
| 183 | return 0; |
| 184 | } |
| 185 | |
| 186 | static struct platform_driver omapbl_driver = { |
| 187 | .probe = omapbl_probe, |
| 188 | .remove = omapbl_remove, |
| 189 | .suspend = omapbl_suspend, |
| 190 | .resume = omapbl_resume, |
| 191 | .driver = { |
| 192 | .name = "omap-bl", |
| 193 | }, |
| 194 | }; |
| 195 | |
| 196 | static int __init omapbl_init(void) |
| 197 | { |
| 198 | return platform_driver_register(&omapbl_driver); |
| 199 | } |
| 200 | |
| 201 | static void __exit omapbl_exit(void) |
| 202 | { |
| 203 | platform_driver_unregister(&omapbl_driver); |
| 204 | } |
| 205 | |
| 206 | module_init(omapbl_init); |
| 207 | module_exit(omapbl_exit); |
| 208 | |
| 209 | MODULE_AUTHOR("Andrzej Zaborowski <balrog@zabor.org>"); |
| 210 | MODULE_DESCRIPTION("OMAP LCD Backlight driver"); |
| 211 | MODULE_LICENSE("GPL"); |