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> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 27 | #include <linux/slab.h> |
Igor Grinberg | cc01272 | 2012-08-29 02:18:54 +0300 | [diff] [blame] | 28 | #include <linux/platform_data/omap1_bl.h> |
Andrzej Zaborowski | ec072b0 | 2008-01-13 23:15:43 +0000 | [diff] [blame] | 29 | |
Russell King | a09e64f | 2008-08-05 16:14:15 +0100 | [diff] [blame] | 30 | #include <mach/hardware.h> |
Tony Lindgren | 70c494c | 2012-09-19 10:46:56 -0700 | [diff] [blame] | 31 | #include <mach/mux.h> |
Andrzej Zaborowski | ec072b0 | 2008-01-13 23:15:43 +0000 | [diff] [blame] | 32 | |
| 33 | #define OMAPBL_MAX_INTENSITY 0xff |
| 34 | |
| 35 | struct omap_backlight { |
| 36 | int powermode; |
| 37 | int current_intensity; |
| 38 | |
| 39 | struct device *dev; |
| 40 | struct omap_backlight_config *pdata; |
| 41 | }; |
| 42 | |
Jingoo Han | 7a94f65 | 2012-12-17 16:00:29 -0800 | [diff] [blame] | 43 | static inline void omapbl_send_intensity(int intensity) |
Andrzej Zaborowski | ec072b0 | 2008-01-13 23:15:43 +0000 | [diff] [blame] | 44 | { |
| 45 | omap_writeb(intensity, OMAP_PWL_ENABLE); |
| 46 | } |
| 47 | |
Jingoo Han | 7a94f65 | 2012-12-17 16:00:29 -0800 | [diff] [blame] | 48 | static inline void omapbl_send_enable(int enable) |
Andrzej Zaborowski | ec072b0 | 2008-01-13 23:15:43 +0000 | [diff] [blame] | 49 | { |
| 50 | omap_writeb(enable, OMAP_PWL_CLK_ENABLE); |
| 51 | } |
| 52 | |
| 53 | static void omapbl_blank(struct omap_backlight *bl, int mode) |
| 54 | { |
| 55 | if (bl->pdata->set_power) |
| 56 | bl->pdata->set_power(bl->dev, mode); |
| 57 | |
| 58 | switch (mode) { |
| 59 | case FB_BLANK_NORMAL: |
| 60 | case FB_BLANK_VSYNC_SUSPEND: |
| 61 | case FB_BLANK_HSYNC_SUSPEND: |
| 62 | case FB_BLANK_POWERDOWN: |
| 63 | omapbl_send_intensity(0); |
| 64 | omapbl_send_enable(0); |
| 65 | break; |
| 66 | |
| 67 | case FB_BLANK_UNBLANK: |
| 68 | omapbl_send_intensity(bl->current_intensity); |
| 69 | omapbl_send_enable(1); |
| 70 | break; |
| 71 | } |
| 72 | } |
| 73 | |
Jingoo Han | 07cac9d | 2013-04-29 16:17:59 -0700 | [diff] [blame^] | 74 | #ifdef CONFIG_PM_SLEEP |
| 75 | static int omapbl_suspend(struct device *dev) |
Andrzej Zaborowski | ec072b0 | 2008-01-13 23:15:43 +0000 | [diff] [blame] | 76 | { |
Jingoo Han | 07cac9d | 2013-04-29 16:17:59 -0700 | [diff] [blame^] | 77 | struct backlight_device *bl_dev = dev_get_drvdata(dev); |
| 78 | struct omap_backlight *bl = bl_get_data(bl_dev); |
Andrzej Zaborowski | ec072b0 | 2008-01-13 23:15:43 +0000 | [diff] [blame] | 79 | |
| 80 | omapbl_blank(bl, FB_BLANK_POWERDOWN); |
| 81 | return 0; |
| 82 | } |
| 83 | |
Jingoo Han | 07cac9d | 2013-04-29 16:17:59 -0700 | [diff] [blame^] | 84 | static int omapbl_resume(struct device *dev) |
Andrzej Zaborowski | ec072b0 | 2008-01-13 23:15:43 +0000 | [diff] [blame] | 85 | { |
Jingoo Han | 07cac9d | 2013-04-29 16:17:59 -0700 | [diff] [blame^] | 86 | struct backlight_device *bl_dev = dev_get_drvdata(dev); |
| 87 | struct omap_backlight *bl = bl_get_data(bl_dev); |
Andrzej Zaborowski | ec072b0 | 2008-01-13 23:15:43 +0000 | [diff] [blame] | 88 | |
| 89 | omapbl_blank(bl, bl->powermode); |
| 90 | return 0; |
| 91 | } |
Andrzej Zaborowski | ec072b0 | 2008-01-13 23:15:43 +0000 | [diff] [blame] | 92 | #endif |
| 93 | |
| 94 | static int omapbl_set_power(struct backlight_device *dev, int state) |
| 95 | { |
Jingoo Han | 5f42d5b | 2013-02-21 16:43:50 -0800 | [diff] [blame] | 96 | struct omap_backlight *bl = bl_get_data(dev); |
Andrzej Zaborowski | ec072b0 | 2008-01-13 23:15:43 +0000 | [diff] [blame] | 97 | |
| 98 | omapbl_blank(bl, state); |
| 99 | bl->powermode = state; |
| 100 | |
| 101 | return 0; |
| 102 | } |
| 103 | |
| 104 | static int omapbl_update_status(struct backlight_device *dev) |
| 105 | { |
Jingoo Han | 5f42d5b | 2013-02-21 16:43:50 -0800 | [diff] [blame] | 106 | struct omap_backlight *bl = bl_get_data(dev); |
Andrzej Zaborowski | ec072b0 | 2008-01-13 23:15:43 +0000 | [diff] [blame] | 107 | |
| 108 | if (bl->current_intensity != dev->props.brightness) { |
| 109 | if (bl->powermode == FB_BLANK_UNBLANK) |
| 110 | omapbl_send_intensity(dev->props.brightness); |
| 111 | bl->current_intensity = dev->props.brightness; |
| 112 | } |
| 113 | |
| 114 | if (dev->props.fb_blank != bl->powermode) |
| 115 | omapbl_set_power(dev, dev->props.fb_blank); |
| 116 | |
| 117 | return 0; |
| 118 | } |
| 119 | |
| 120 | static int omapbl_get_intensity(struct backlight_device *dev) |
| 121 | { |
Jingoo Han | 5f42d5b | 2013-02-21 16:43:50 -0800 | [diff] [blame] | 122 | struct omap_backlight *bl = bl_get_data(dev); |
Andrzej Zaborowski | ec072b0 | 2008-01-13 23:15:43 +0000 | [diff] [blame] | 123 | return bl->current_intensity; |
| 124 | } |
| 125 | |
Emese Revfy | 9905a43 | 2009-12-14 00:58:57 +0100 | [diff] [blame] | 126 | static const struct backlight_ops omapbl_ops = { |
Andrzej Zaborowski | ec072b0 | 2008-01-13 23:15:43 +0000 | [diff] [blame] | 127 | .get_brightness = omapbl_get_intensity, |
| 128 | .update_status = omapbl_update_status, |
| 129 | }; |
| 130 | |
| 131 | static int omapbl_probe(struct platform_device *pdev) |
| 132 | { |
Matthew Garrett | a19a6ee | 2010-02-17 16:39:44 -0500 | [diff] [blame] | 133 | struct backlight_properties props; |
Andrzej Zaborowski | ec072b0 | 2008-01-13 23:15:43 +0000 | [diff] [blame] | 134 | struct backlight_device *dev; |
| 135 | struct omap_backlight *bl; |
| 136 | struct omap_backlight_config *pdata = pdev->dev.platform_data; |
| 137 | |
| 138 | if (!pdata) |
| 139 | return -ENXIO; |
| 140 | |
Julia Lawall | ce96922 | 2012-03-23 15:02:00 -0700 | [diff] [blame] | 141 | bl = devm_kzalloc(&pdev->dev, sizeof(struct omap_backlight), |
| 142 | GFP_KERNEL); |
Andrzej Zaborowski | ec072b0 | 2008-01-13 23:15:43 +0000 | [diff] [blame] | 143 | if (unlikely(!bl)) |
| 144 | return -ENOMEM; |
| 145 | |
Matthew Garrett | a19a6ee | 2010-02-17 16:39:44 -0500 | [diff] [blame] | 146 | memset(&props, 0, sizeof(struct backlight_properties)); |
Matthew Garrett | bb7ca74 | 2011-03-22 16:30:21 -0700 | [diff] [blame] | 147 | props.type = BACKLIGHT_RAW; |
Matthew Garrett | a19a6ee | 2010-02-17 16:39:44 -0500 | [diff] [blame] | 148 | props.max_brightness = OMAPBL_MAX_INTENSITY; |
| 149 | dev = backlight_device_register("omap-bl", &pdev->dev, bl, &omapbl_ops, |
| 150 | &props); |
Julia Lawall | ce96922 | 2012-03-23 15:02:00 -0700 | [diff] [blame] | 151 | if (IS_ERR(dev)) |
Andrzej Zaborowski | ec072b0 | 2008-01-13 23:15:43 +0000 | [diff] [blame] | 152 | return PTR_ERR(dev); |
Andrzej Zaborowski | ec072b0 | 2008-01-13 23:15:43 +0000 | [diff] [blame] | 153 | |
| 154 | bl->powermode = FB_BLANK_POWERDOWN; |
| 155 | bl->current_intensity = 0; |
| 156 | |
| 157 | bl->pdata = pdata; |
| 158 | bl->dev = &pdev->dev; |
| 159 | |
| 160 | platform_set_drvdata(pdev, dev); |
| 161 | |
| 162 | omap_cfg_reg(PWL); /* Conflicts with UART3 */ |
| 163 | |
| 164 | dev->props.fb_blank = FB_BLANK_UNBLANK; |
Andrzej Zaborowski | ec072b0 | 2008-01-13 23:15:43 +0000 | [diff] [blame] | 165 | dev->props.brightness = pdata->default_intensity; |
| 166 | omapbl_update_status(dev); |
| 167 | |
Jingoo Han | 8e1274a | 2013-04-29 16:17:32 -0700 | [diff] [blame] | 168 | dev_info(&pdev->dev, "OMAP LCD backlight initialised\n"); |
Andrzej Zaborowski | ec072b0 | 2008-01-13 23:15:43 +0000 | [diff] [blame] | 169 | |
| 170 | return 0; |
| 171 | } |
| 172 | |
| 173 | static int omapbl_remove(struct platform_device *pdev) |
| 174 | { |
| 175 | struct backlight_device *dev = platform_get_drvdata(pdev); |
Andrzej Zaborowski | ec072b0 | 2008-01-13 23:15:43 +0000 | [diff] [blame] | 176 | |
| 177 | backlight_device_unregister(dev); |
Andrzej Zaborowski | ec072b0 | 2008-01-13 23:15:43 +0000 | [diff] [blame] | 178 | |
| 179 | return 0; |
| 180 | } |
| 181 | |
Jingoo Han | 07cac9d | 2013-04-29 16:17:59 -0700 | [diff] [blame^] | 182 | static SIMPLE_DEV_PM_OPS(omapbl_pm_ops, omapbl_suspend, omapbl_resume); |
| 183 | |
Andrzej Zaborowski | ec072b0 | 2008-01-13 23:15:43 +0000 | [diff] [blame] | 184 | static struct platform_driver omapbl_driver = { |
| 185 | .probe = omapbl_probe, |
| 186 | .remove = omapbl_remove, |
Andrzej Zaborowski | ec072b0 | 2008-01-13 23:15:43 +0000 | [diff] [blame] | 187 | .driver = { |
| 188 | .name = "omap-bl", |
Jingoo Han | 07cac9d | 2013-04-29 16:17:59 -0700 | [diff] [blame^] | 189 | .pm = &omapbl_pm_ops, |
Andrzej Zaborowski | ec072b0 | 2008-01-13 23:15:43 +0000 | [diff] [blame] | 190 | }, |
| 191 | }; |
| 192 | |
Axel Lin | 81178e0 | 2012-01-10 15:09:11 -0800 | [diff] [blame] | 193 | module_platform_driver(omapbl_driver); |
Andrzej Zaborowski | ec072b0 | 2008-01-13 23:15:43 +0000 | [diff] [blame] | 194 | |
| 195 | MODULE_AUTHOR("Andrzej Zaborowski <balrog@zabor.org>"); |
| 196 | MODULE_DESCRIPTION("OMAP LCD Backlight driver"); |
| 197 | MODULE_LICENSE("GPL"); |