eric miao | 42796d3 | 2008-04-14 09:35:08 +0100 | [diff] [blame] | 1 | /* |
| 2 | * linux/drivers/video/backlight/pwm_bl.c |
| 3 | * |
| 4 | * simple PWM based backlight control, board code has to setup |
| 5 | * 1) pin configuration so PWM waveforms can output |
Eric Miao | b8cdd87 | 2009-02-10 13:30:37 +0800 | [diff] [blame] | 6 | * 2) platform_data being correctly configured |
eric miao | 42796d3 | 2008-04-14 09:35:08 +0100 | [diff] [blame] | 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License version 2 as |
| 10 | * published by the Free Software Foundation. |
| 11 | */ |
| 12 | |
| 13 | #include <linux/module.h> |
| 14 | #include <linux/kernel.h> |
| 15 | #include <linux/init.h> |
| 16 | #include <linux/platform_device.h> |
| 17 | #include <linux/fb.h> |
| 18 | #include <linux/backlight.h> |
| 19 | #include <linux/err.h> |
| 20 | #include <linux/pwm.h> |
| 21 | #include <linux/pwm_backlight.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 22 | #include <linux/slab.h> |
eric miao | 42796d3 | 2008-04-14 09:35:08 +0100 | [diff] [blame] | 23 | |
| 24 | struct pwm_bl_data { |
| 25 | struct pwm_device *pwm; |
Ben Dooks | cfc3899 | 2009-11-10 17:20:40 +0000 | [diff] [blame] | 26 | struct device *dev; |
eric miao | 42796d3 | 2008-04-14 09:35:08 +0100 | [diff] [blame] | 27 | unsigned int period; |
Arun Murthy | fef7764 | 2010-11-11 14:05:28 -0800 | [diff] [blame] | 28 | unsigned int lth_brightness; |
Thierry Reding | 3e3ed6c | 2011-12-16 21:25:29 +0100 | [diff] [blame] | 29 | unsigned int *levels; |
Ben Dooks | cfc3899 | 2009-11-10 17:20:40 +0000 | [diff] [blame] | 30 | int (*notify)(struct device *, |
| 31 | int brightness); |
Dilan Lee | cc7993f | 2011-08-25 15:59:17 -0700 | [diff] [blame] | 32 | void (*notify_after)(struct device *, |
| 33 | int brightness); |
Robert Morell | ef0a5e8 | 2011-03-22 16:30:31 -0700 | [diff] [blame] | 34 | int (*check_fb)(struct device *, struct fb_info *); |
Thierry Reding | 3e3ed6c | 2011-12-16 21:25:29 +0100 | [diff] [blame] | 35 | void (*exit)(struct device *); |
eric miao | 42796d3 | 2008-04-14 09:35:08 +0100 | [diff] [blame] | 36 | }; |
| 37 | |
| 38 | static int pwm_backlight_update_status(struct backlight_device *bl) |
| 39 | { |
Jingoo Han | e6e3dbf | 2013-02-21 16:43:46 -0800 | [diff] [blame] | 40 | struct pwm_bl_data *pb = bl_get_data(bl); |
eric miao | 42796d3 | 2008-04-14 09:35:08 +0100 | [diff] [blame] | 41 | int brightness = bl->props.brightness; |
| 42 | int max = bl->props.max_brightness; |
| 43 | |
Alexandre Courbot | 0132267 | 2013-01-30 15:19:15 +0900 | [diff] [blame] | 44 | if (bl->props.power != FB_BLANK_UNBLANK || |
| 45 | bl->props.fb_blank != FB_BLANK_UNBLANK || |
| 46 | bl->props.state & BL_CORE_FBBLANK) |
eric miao | 42796d3 | 2008-04-14 09:35:08 +0100 | [diff] [blame] | 47 | brightness = 0; |
| 48 | |
Philipp Zabel | 3b73125 | 2008-05-22 14:18:40 +0100 | [diff] [blame] | 49 | if (pb->notify) |
Ben Dooks | cfc3899 | 2009-11-10 17:20:40 +0000 | [diff] [blame] | 50 | brightness = pb->notify(pb->dev, brightness); |
Philipp Zabel | 3b73125 | 2008-05-22 14:18:40 +0100 | [diff] [blame] | 51 | |
eric miao | 42796d3 | 2008-04-14 09:35:08 +0100 | [diff] [blame] | 52 | if (brightness == 0) { |
| 53 | pwm_config(pb->pwm, 0, pb->period); |
| 54 | pwm_disable(pb->pwm); |
| 55 | } else { |
Alexandre Courbot | 9fb978b | 2012-07-09 15:04:23 +0900 | [diff] [blame] | 56 | int duty_cycle; |
| 57 | |
Thierry Reding | 3e3ed6c | 2011-12-16 21:25:29 +0100 | [diff] [blame] | 58 | if (pb->levels) { |
Alexandre Courbot | 9fb978b | 2012-07-09 15:04:23 +0900 | [diff] [blame] | 59 | duty_cycle = pb->levels[brightness]; |
Thierry Reding | 3e3ed6c | 2011-12-16 21:25:29 +0100 | [diff] [blame] | 60 | max = pb->levels[max]; |
Alexandre Courbot | 9fb978b | 2012-07-09 15:04:23 +0900 | [diff] [blame] | 61 | } else { |
| 62 | duty_cycle = brightness; |
Thierry Reding | 3e3ed6c | 2011-12-16 21:25:29 +0100 | [diff] [blame] | 63 | } |
| 64 | |
Alexandre Courbot | 9fb978b | 2012-07-09 15:04:23 +0900 | [diff] [blame] | 65 | duty_cycle = pb->lth_brightness + |
| 66 | (duty_cycle * (pb->period - pb->lth_brightness) / max); |
| 67 | pwm_config(pb->pwm, duty_cycle, pb->period); |
eric miao | 42796d3 | 2008-04-14 09:35:08 +0100 | [diff] [blame] | 68 | pwm_enable(pb->pwm); |
| 69 | } |
Dilan Lee | cc7993f | 2011-08-25 15:59:17 -0700 | [diff] [blame] | 70 | |
| 71 | if (pb->notify_after) |
| 72 | pb->notify_after(pb->dev, brightness); |
| 73 | |
eric miao | 42796d3 | 2008-04-14 09:35:08 +0100 | [diff] [blame] | 74 | return 0; |
| 75 | } |
| 76 | |
| 77 | static int pwm_backlight_get_brightness(struct backlight_device *bl) |
| 78 | { |
| 79 | return bl->props.brightness; |
| 80 | } |
| 81 | |
Robert Morell | ef0a5e8 | 2011-03-22 16:30:31 -0700 | [diff] [blame] | 82 | static int pwm_backlight_check_fb(struct backlight_device *bl, |
| 83 | struct fb_info *info) |
| 84 | { |
Jingoo Han | e6e3dbf | 2013-02-21 16:43:46 -0800 | [diff] [blame] | 85 | struct pwm_bl_data *pb = bl_get_data(bl); |
Robert Morell | ef0a5e8 | 2011-03-22 16:30:31 -0700 | [diff] [blame] | 86 | |
| 87 | return !pb->check_fb || pb->check_fb(pb->dev, info); |
| 88 | } |
| 89 | |
Emese Revfy | 9905a43 | 2009-12-14 00:58:57 +0100 | [diff] [blame] | 90 | static const struct backlight_ops pwm_backlight_ops = { |
eric miao | 42796d3 | 2008-04-14 09:35:08 +0100 | [diff] [blame] | 91 | .update_status = pwm_backlight_update_status, |
| 92 | .get_brightness = pwm_backlight_get_brightness, |
Robert Morell | ef0a5e8 | 2011-03-22 16:30:31 -0700 | [diff] [blame] | 93 | .check_fb = pwm_backlight_check_fb, |
eric miao | 42796d3 | 2008-04-14 09:35:08 +0100 | [diff] [blame] | 94 | }; |
| 95 | |
Thierry Reding | 3e3ed6c | 2011-12-16 21:25:29 +0100 | [diff] [blame] | 96 | #ifdef CONFIG_OF |
| 97 | static int pwm_backlight_parse_dt(struct device *dev, |
| 98 | struct platform_pwm_backlight_data *data) |
| 99 | { |
| 100 | struct device_node *node = dev->of_node; |
| 101 | struct property *prop; |
| 102 | int length; |
| 103 | u32 value; |
| 104 | int ret; |
| 105 | |
| 106 | if (!node) |
| 107 | return -ENODEV; |
| 108 | |
| 109 | memset(data, 0, sizeof(*data)); |
| 110 | |
| 111 | /* determine the number of brightness levels */ |
| 112 | prop = of_find_property(node, "brightness-levels", &length); |
| 113 | if (!prop) |
| 114 | return -EINVAL; |
| 115 | |
| 116 | data->max_brightness = length / sizeof(u32); |
| 117 | |
| 118 | /* read brightness levels from DT property */ |
| 119 | if (data->max_brightness > 0) { |
| 120 | size_t size = sizeof(*data->levels) * data->max_brightness; |
| 121 | |
| 122 | data->levels = devm_kzalloc(dev, size, GFP_KERNEL); |
| 123 | if (!data->levels) |
| 124 | return -ENOMEM; |
| 125 | |
| 126 | ret = of_property_read_u32_array(node, "brightness-levels", |
| 127 | data->levels, |
| 128 | data->max_brightness); |
| 129 | if (ret < 0) |
| 130 | return ret; |
| 131 | |
| 132 | ret = of_property_read_u32(node, "default-brightness-level", |
| 133 | &value); |
| 134 | if (ret < 0) |
| 135 | return ret; |
| 136 | |
Thierry Reding | 3e3ed6c | 2011-12-16 21:25:29 +0100 | [diff] [blame] | 137 | data->dft_brightness = value; |
| 138 | data->max_brightness--; |
| 139 | } |
| 140 | |
| 141 | /* |
| 142 | * TODO: Most users of this driver use a number of GPIOs to control |
| 143 | * backlight power. Support for specifying these needs to be |
| 144 | * added. |
| 145 | */ |
| 146 | |
| 147 | return 0; |
| 148 | } |
| 149 | |
| 150 | static struct of_device_id pwm_backlight_of_match[] = { |
| 151 | { .compatible = "pwm-backlight" }, |
| 152 | { } |
| 153 | }; |
| 154 | |
| 155 | MODULE_DEVICE_TABLE(of, pwm_backlight_of_match); |
| 156 | #else |
| 157 | static int pwm_backlight_parse_dt(struct device *dev, |
| 158 | struct platform_pwm_backlight_data *data) |
| 159 | { |
| 160 | return -ENODEV; |
| 161 | } |
| 162 | #endif |
| 163 | |
eric miao | 42796d3 | 2008-04-14 09:35:08 +0100 | [diff] [blame] | 164 | static int pwm_backlight_probe(struct platform_device *pdev) |
| 165 | { |
| 166 | struct platform_pwm_backlight_data *data = pdev->dev.platform_data; |
Thierry Reding | 3e3ed6c | 2011-12-16 21:25:29 +0100 | [diff] [blame] | 167 | struct platform_pwm_backlight_data defdata; |
| 168 | struct backlight_properties props; |
eric miao | 42796d3 | 2008-04-14 09:35:08 +0100 | [diff] [blame] | 169 | struct backlight_device *bl; |
| 170 | struct pwm_bl_data *pb; |
Thierry Reding | 3e3ed6c | 2011-12-16 21:25:29 +0100 | [diff] [blame] | 171 | unsigned int max; |
Philipp Zabel | 3b73125 | 2008-05-22 14:18:40 +0100 | [diff] [blame] | 172 | int ret; |
eric miao | 42796d3 | 2008-04-14 09:35:08 +0100 | [diff] [blame] | 173 | |
Ben Dooks | 14563a4 | 2008-08-05 13:01:22 -0700 | [diff] [blame] | 174 | if (!data) { |
Thierry Reding | 3e3ed6c | 2011-12-16 21:25:29 +0100 | [diff] [blame] | 175 | ret = pwm_backlight_parse_dt(&pdev->dev, &defdata); |
| 176 | if (ret < 0) { |
| 177 | dev_err(&pdev->dev, "failed to find platform data\n"); |
| 178 | return ret; |
| 179 | } |
| 180 | |
| 181 | data = &defdata; |
Ben Dooks | 14563a4 | 2008-08-05 13:01:22 -0700 | [diff] [blame] | 182 | } |
eric miao | 42796d3 | 2008-04-14 09:35:08 +0100 | [diff] [blame] | 183 | |
Philipp Zabel | 3b73125 | 2008-05-22 14:18:40 +0100 | [diff] [blame] | 184 | if (data->init) { |
| 185 | ret = data->init(&pdev->dev); |
| 186 | if (ret < 0) |
| 187 | return ret; |
| 188 | } |
| 189 | |
Julia Lawall | ce96922 | 2012-03-23 15:02:00 -0700 | [diff] [blame] | 190 | pb = devm_kzalloc(&pdev->dev, sizeof(*pb), GFP_KERNEL); |
Philipp Zabel | 3b73125 | 2008-05-22 14:18:40 +0100 | [diff] [blame] | 191 | if (!pb) { |
Ben Dooks | 14563a4 | 2008-08-05 13:01:22 -0700 | [diff] [blame] | 192 | dev_err(&pdev->dev, "no memory for state\n"); |
Philipp Zabel | 3b73125 | 2008-05-22 14:18:40 +0100 | [diff] [blame] | 193 | ret = -ENOMEM; |
| 194 | goto err_alloc; |
| 195 | } |
eric miao | 42796d3 | 2008-04-14 09:35:08 +0100 | [diff] [blame] | 196 | |
Thierry Reding | 3e3ed6c | 2011-12-16 21:25:29 +0100 | [diff] [blame] | 197 | if (data->levels) { |
| 198 | max = data->levels[data->max_brightness]; |
| 199 | pb->levels = data->levels; |
| 200 | } else |
| 201 | max = data->max_brightness; |
| 202 | |
Philipp Zabel | 3b73125 | 2008-05-22 14:18:40 +0100 | [diff] [blame] | 203 | pb->notify = data->notify; |
Dilan Lee | cc7993f | 2011-08-25 15:59:17 -0700 | [diff] [blame] | 204 | pb->notify_after = data->notify_after; |
Robert Morell | ef0a5e8 | 2011-03-22 16:30:31 -0700 | [diff] [blame] | 205 | pb->check_fb = data->check_fb; |
Thierry Reding | 3e3ed6c | 2011-12-16 21:25:29 +0100 | [diff] [blame] | 206 | pb->exit = data->exit; |
Ben Dooks | cfc3899 | 2009-11-10 17:20:40 +0000 | [diff] [blame] | 207 | pb->dev = &pdev->dev; |
eric miao | 42796d3 | 2008-04-14 09:35:08 +0100 | [diff] [blame] | 208 | |
Sachin Kamat | 60ce702 | 2012-09-17 11:50:47 +0530 | [diff] [blame] | 209 | pb->pwm = devm_pwm_get(&pdev->dev, NULL); |
Ben Dooks | 43bda1a | 2008-07-01 14:18:27 +0100 | [diff] [blame] | 210 | if (IS_ERR(pb->pwm)) { |
Thierry Reding | 3e3ed6c | 2011-12-16 21:25:29 +0100 | [diff] [blame] | 211 | dev_err(&pdev->dev, "unable to request PWM, trying legacy API\n"); |
| 212 | |
| 213 | pb->pwm = pwm_request(data->pwm_id, "pwm-backlight"); |
| 214 | if (IS_ERR(pb->pwm)) { |
| 215 | dev_err(&pdev->dev, "unable to request legacy PWM\n"); |
| 216 | ret = PTR_ERR(pb->pwm); |
| 217 | goto err_alloc; |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | dev_dbg(&pdev->dev, "got pwm for backlight\n"); |
| 222 | |
| 223 | /* |
| 224 | * The DT case will set the pwm_period_ns field to 0 and store the |
| 225 | * period, parsed from the DT, in the PWM device. For the non-DT case, |
| 226 | * set the period from platform data. |
| 227 | */ |
| 228 | if (data->pwm_period_ns > 0) |
| 229 | pwm_set_period(pb->pwm, data->pwm_period_ns); |
| 230 | |
| 231 | pb->period = pwm_get_period(pb->pwm); |
| 232 | pb->lth_brightness = data->lth_brightness * (pb->period / max); |
eric miao | 42796d3 | 2008-04-14 09:35:08 +0100 | [diff] [blame] | 233 | |
Matthew Garrett | a19a6ee | 2010-02-17 16:39:44 -0500 | [diff] [blame] | 234 | memset(&props, 0, sizeof(struct backlight_properties)); |
Matthew Garrett | bb7ca74 | 2011-03-22 16:30:21 -0700 | [diff] [blame] | 235 | props.type = BACKLIGHT_RAW; |
Matthew Garrett | a19a6ee | 2010-02-17 16:39:44 -0500 | [diff] [blame] | 236 | props.max_brightness = data->max_brightness; |
| 237 | bl = backlight_device_register(dev_name(&pdev->dev), &pdev->dev, pb, |
| 238 | &pwm_backlight_ops, &props); |
eric miao | 42796d3 | 2008-04-14 09:35:08 +0100 | [diff] [blame] | 239 | if (IS_ERR(bl)) { |
| 240 | dev_err(&pdev->dev, "failed to register backlight\n"); |
Philipp Zabel | 3b73125 | 2008-05-22 14:18:40 +0100 | [diff] [blame] | 241 | ret = PTR_ERR(bl); |
Sachin Kamat | 60ce702 | 2012-09-17 11:50:47 +0530 | [diff] [blame] | 242 | goto err_alloc; |
eric miao | 42796d3 | 2008-04-14 09:35:08 +0100 | [diff] [blame] | 243 | } |
| 244 | |
Peter Ujfalusi | 83cfd72 | 2013-01-22 14:39:54 +0100 | [diff] [blame] | 245 | if (data->dft_brightness > data->max_brightness) { |
| 246 | dev_warn(&pdev->dev, |
| 247 | "invalid default brightness level: %u, using %u\n", |
| 248 | data->dft_brightness, data->max_brightness); |
| 249 | data->dft_brightness = data->max_brightness; |
| 250 | } |
| 251 | |
eric miao | 42796d3 | 2008-04-14 09:35:08 +0100 | [diff] [blame] | 252 | bl->props.brightness = data->dft_brightness; |
| 253 | backlight_update_status(bl); |
| 254 | |
| 255 | platform_set_drvdata(pdev, bl); |
| 256 | return 0; |
Philipp Zabel | 3b73125 | 2008-05-22 14:18:40 +0100 | [diff] [blame] | 257 | |
Philipp Zabel | 3b73125 | 2008-05-22 14:18:40 +0100 | [diff] [blame] | 258 | err_alloc: |
| 259 | if (data->exit) |
| 260 | data->exit(&pdev->dev); |
| 261 | return ret; |
eric miao | 42796d3 | 2008-04-14 09:35:08 +0100 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | static int pwm_backlight_remove(struct platform_device *pdev) |
| 265 | { |
| 266 | struct backlight_device *bl = platform_get_drvdata(pdev); |
Jingoo Han | e6e3dbf | 2013-02-21 16:43:46 -0800 | [diff] [blame] | 267 | struct pwm_bl_data *pb = bl_get_data(bl); |
eric miao | 42796d3 | 2008-04-14 09:35:08 +0100 | [diff] [blame] | 268 | |
| 269 | backlight_device_unregister(bl); |
| 270 | pwm_config(pb->pwm, 0, pb->period); |
| 271 | pwm_disable(pb->pwm); |
Thierry Reding | 3e3ed6c | 2011-12-16 21:25:29 +0100 | [diff] [blame] | 272 | if (pb->exit) |
| 273 | pb->exit(&pdev->dev); |
eric miao | 42796d3 | 2008-04-14 09:35:08 +0100 | [diff] [blame] | 274 | return 0; |
| 275 | } |
| 276 | |
Jingoo Han | c791126 | 2013-02-25 17:15:47 +0900 | [diff] [blame] | 277 | #ifdef CONFIG_PM_SLEEP |
Mark Brown | e2c17bc | 2012-01-10 15:09:22 -0800 | [diff] [blame] | 278 | static int pwm_backlight_suspend(struct device *dev) |
eric miao | 42796d3 | 2008-04-14 09:35:08 +0100 | [diff] [blame] | 279 | { |
Mark Brown | e2c17bc | 2012-01-10 15:09:22 -0800 | [diff] [blame] | 280 | struct backlight_device *bl = dev_get_drvdata(dev); |
Jingoo Han | e6e3dbf | 2013-02-21 16:43:46 -0800 | [diff] [blame] | 281 | struct pwm_bl_data *pb = bl_get_data(bl); |
eric miao | 42796d3 | 2008-04-14 09:35:08 +0100 | [diff] [blame] | 282 | |
Marc Zyngier | 82e8b54 | 2009-06-19 10:50:54 +0200 | [diff] [blame] | 283 | if (pb->notify) |
Ben Dooks | cfc3899 | 2009-11-10 17:20:40 +0000 | [diff] [blame] | 284 | pb->notify(pb->dev, 0); |
eric miao | 42796d3 | 2008-04-14 09:35:08 +0100 | [diff] [blame] | 285 | pwm_config(pb->pwm, 0, pb->period); |
| 286 | pwm_disable(pb->pwm); |
Dilan Lee | cc7993f | 2011-08-25 15:59:17 -0700 | [diff] [blame] | 287 | if (pb->notify_after) |
| 288 | pb->notify_after(pb->dev, 0); |
eric miao | 42796d3 | 2008-04-14 09:35:08 +0100 | [diff] [blame] | 289 | return 0; |
| 290 | } |
| 291 | |
Mark Brown | e2c17bc | 2012-01-10 15:09:22 -0800 | [diff] [blame] | 292 | static int pwm_backlight_resume(struct device *dev) |
eric miao | 42796d3 | 2008-04-14 09:35:08 +0100 | [diff] [blame] | 293 | { |
Mark Brown | e2c17bc | 2012-01-10 15:09:22 -0800 | [diff] [blame] | 294 | struct backlight_device *bl = dev_get_drvdata(dev); |
eric miao | 42796d3 | 2008-04-14 09:35:08 +0100 | [diff] [blame] | 295 | |
| 296 | backlight_update_status(bl); |
| 297 | return 0; |
| 298 | } |
Jingoo Han | c791126 | 2013-02-25 17:15:47 +0900 | [diff] [blame] | 299 | #endif |
Mark Brown | e2c17bc | 2012-01-10 15:09:22 -0800 | [diff] [blame] | 300 | |
| 301 | static SIMPLE_DEV_PM_OPS(pwm_backlight_pm_ops, pwm_backlight_suspend, |
| 302 | pwm_backlight_resume); |
| 303 | |
eric miao | 42796d3 | 2008-04-14 09:35:08 +0100 | [diff] [blame] | 304 | static struct platform_driver pwm_backlight_driver = { |
| 305 | .driver = { |
Thierry Reding | 3e3ed6c | 2011-12-16 21:25:29 +0100 | [diff] [blame] | 306 | .name = "pwm-backlight", |
| 307 | .owner = THIS_MODULE, |
Thierry Reding | 3e3ed6c | 2011-12-16 21:25:29 +0100 | [diff] [blame] | 308 | .pm = &pwm_backlight_pm_ops, |
Thierry Reding | 3e3ed6c | 2011-12-16 21:25:29 +0100 | [diff] [blame] | 309 | .of_match_table = of_match_ptr(pwm_backlight_of_match), |
eric miao | 42796d3 | 2008-04-14 09:35:08 +0100 | [diff] [blame] | 310 | }, |
| 311 | .probe = pwm_backlight_probe, |
| 312 | .remove = pwm_backlight_remove, |
eric miao | 42796d3 | 2008-04-14 09:35:08 +0100 | [diff] [blame] | 313 | }; |
| 314 | |
Axel Lin | 81178e0 | 2012-01-10 15:09:11 -0800 | [diff] [blame] | 315 | module_platform_driver(pwm_backlight_driver); |
eric miao | 42796d3 | 2008-04-14 09:35:08 +0100 | [diff] [blame] | 316 | |
| 317 | MODULE_DESCRIPTION("PWM based Backlight Driver"); |
| 318 | MODULE_LICENSE("GPL"); |
Ben Dooks | 8cd6819 | 2008-08-05 13:01:24 -0700 | [diff] [blame] | 319 | MODULE_ALIAS("platform:pwm-backlight"); |
| 320 | |