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; |
Ben Dooks | cfc3899 | 2009-11-10 17:20:40 +0000 | [diff] [blame] | 29 | int (*notify)(struct device *, |
| 30 | int brightness); |
Dilan Lee | cc7993f | 2011-08-25 15:59:17 -0700 | [diff] [blame] | 31 | void (*notify_after)(struct device *, |
| 32 | int brightness); |
Robert Morell | ef0a5e8 | 2011-03-22 16:30:31 -0700 | [diff] [blame] | 33 | int (*check_fb)(struct device *, struct fb_info *); |
eric miao | 42796d3 | 2008-04-14 09:35:08 +0100 | [diff] [blame] | 34 | }; |
| 35 | |
| 36 | static int pwm_backlight_update_status(struct backlight_device *bl) |
| 37 | { |
| 38 | struct pwm_bl_data *pb = dev_get_drvdata(&bl->dev); |
| 39 | int brightness = bl->props.brightness; |
| 40 | int max = bl->props.max_brightness; |
| 41 | |
| 42 | if (bl->props.power != FB_BLANK_UNBLANK) |
| 43 | brightness = 0; |
| 44 | |
| 45 | if (bl->props.fb_blank != FB_BLANK_UNBLANK) |
| 46 | brightness = 0; |
| 47 | |
Philipp Zabel | 3b73125 | 2008-05-22 14:18:40 +0100 | [diff] [blame] | 48 | if (pb->notify) |
Ben Dooks | cfc3899 | 2009-11-10 17:20:40 +0000 | [diff] [blame] | 49 | brightness = pb->notify(pb->dev, brightness); |
Philipp Zabel | 3b73125 | 2008-05-22 14:18:40 +0100 | [diff] [blame] | 50 | |
eric miao | 42796d3 | 2008-04-14 09:35:08 +0100 | [diff] [blame] | 51 | if (brightness == 0) { |
| 52 | pwm_config(pb->pwm, 0, pb->period); |
| 53 | pwm_disable(pb->pwm); |
| 54 | } else { |
Arun Murthy | fef7764 | 2010-11-11 14:05:28 -0800 | [diff] [blame] | 55 | brightness = pb->lth_brightness + |
| 56 | (brightness * (pb->period - pb->lth_brightness) / max); |
| 57 | pwm_config(pb->pwm, brightness, pb->period); |
eric miao | 42796d3 | 2008-04-14 09:35:08 +0100 | [diff] [blame] | 58 | pwm_enable(pb->pwm); |
| 59 | } |
Dilan Lee | cc7993f | 2011-08-25 15:59:17 -0700 | [diff] [blame] | 60 | |
| 61 | if (pb->notify_after) |
| 62 | pb->notify_after(pb->dev, brightness); |
| 63 | |
eric miao | 42796d3 | 2008-04-14 09:35:08 +0100 | [diff] [blame] | 64 | return 0; |
| 65 | } |
| 66 | |
| 67 | static int pwm_backlight_get_brightness(struct backlight_device *bl) |
| 68 | { |
| 69 | return bl->props.brightness; |
| 70 | } |
| 71 | |
Robert Morell | ef0a5e8 | 2011-03-22 16:30:31 -0700 | [diff] [blame] | 72 | static int pwm_backlight_check_fb(struct backlight_device *bl, |
| 73 | struct fb_info *info) |
| 74 | { |
| 75 | struct pwm_bl_data *pb = dev_get_drvdata(&bl->dev); |
| 76 | |
| 77 | return !pb->check_fb || pb->check_fb(pb->dev, info); |
| 78 | } |
| 79 | |
Emese Revfy | 9905a43 | 2009-12-14 00:58:57 +0100 | [diff] [blame] | 80 | static const struct backlight_ops pwm_backlight_ops = { |
eric miao | 42796d3 | 2008-04-14 09:35:08 +0100 | [diff] [blame] | 81 | .update_status = pwm_backlight_update_status, |
| 82 | .get_brightness = pwm_backlight_get_brightness, |
Robert Morell | ef0a5e8 | 2011-03-22 16:30:31 -0700 | [diff] [blame] | 83 | .check_fb = pwm_backlight_check_fb, |
eric miao | 42796d3 | 2008-04-14 09:35:08 +0100 | [diff] [blame] | 84 | }; |
| 85 | |
| 86 | static int pwm_backlight_probe(struct platform_device *pdev) |
| 87 | { |
Matthew Garrett | a19a6ee | 2010-02-17 16:39:44 -0500 | [diff] [blame] | 88 | struct backlight_properties props; |
eric miao | 42796d3 | 2008-04-14 09:35:08 +0100 | [diff] [blame] | 89 | struct platform_pwm_backlight_data *data = pdev->dev.platform_data; |
| 90 | struct backlight_device *bl; |
| 91 | struct pwm_bl_data *pb; |
Philipp Zabel | 3b73125 | 2008-05-22 14:18:40 +0100 | [diff] [blame] | 92 | int ret; |
eric miao | 42796d3 | 2008-04-14 09:35:08 +0100 | [diff] [blame] | 93 | |
Ben Dooks | 14563a4 | 2008-08-05 13:01:22 -0700 | [diff] [blame] | 94 | if (!data) { |
| 95 | dev_err(&pdev->dev, "failed to find platform data\n"); |
eric miao | 42796d3 | 2008-04-14 09:35:08 +0100 | [diff] [blame] | 96 | return -EINVAL; |
Ben Dooks | 14563a4 | 2008-08-05 13:01:22 -0700 | [diff] [blame] | 97 | } |
eric miao | 42796d3 | 2008-04-14 09:35:08 +0100 | [diff] [blame] | 98 | |
Philipp Zabel | 3b73125 | 2008-05-22 14:18:40 +0100 | [diff] [blame] | 99 | if (data->init) { |
| 100 | ret = data->init(&pdev->dev); |
| 101 | if (ret < 0) |
| 102 | return ret; |
| 103 | } |
| 104 | |
Julia Lawall | ce96922 | 2012-03-23 15:02:00 -0700 | [diff] [blame] | 105 | pb = devm_kzalloc(&pdev->dev, sizeof(*pb), GFP_KERNEL); |
Philipp Zabel | 3b73125 | 2008-05-22 14:18:40 +0100 | [diff] [blame] | 106 | if (!pb) { |
Ben Dooks | 14563a4 | 2008-08-05 13:01:22 -0700 | [diff] [blame] | 107 | dev_err(&pdev->dev, "no memory for state\n"); |
Philipp Zabel | 3b73125 | 2008-05-22 14:18:40 +0100 | [diff] [blame] | 108 | ret = -ENOMEM; |
| 109 | goto err_alloc; |
| 110 | } |
eric miao | 42796d3 | 2008-04-14 09:35:08 +0100 | [diff] [blame] | 111 | |
| 112 | pb->period = data->pwm_period_ns; |
Philipp Zabel | 3b73125 | 2008-05-22 14:18:40 +0100 | [diff] [blame] | 113 | pb->notify = data->notify; |
Dilan Lee | cc7993f | 2011-08-25 15:59:17 -0700 | [diff] [blame] | 114 | pb->notify_after = data->notify_after; |
Robert Morell | ef0a5e8 | 2011-03-22 16:30:31 -0700 | [diff] [blame] | 115 | pb->check_fb = data->check_fb; |
Arun Murthy | fef7764 | 2010-11-11 14:05:28 -0800 | [diff] [blame] | 116 | pb->lth_brightness = data->lth_brightness * |
| 117 | (data->pwm_period_ns / data->max_brightness); |
Ben Dooks | cfc3899 | 2009-11-10 17:20:40 +0000 | [diff] [blame] | 118 | pb->dev = &pdev->dev; |
eric miao | 42796d3 | 2008-04-14 09:35:08 +0100 | [diff] [blame] | 119 | |
| 120 | pb->pwm = pwm_request(data->pwm_id, "backlight"); |
Ben Dooks | 43bda1a | 2008-07-01 14:18:27 +0100 | [diff] [blame] | 121 | if (IS_ERR(pb->pwm)) { |
eric miao | 42796d3 | 2008-04-14 09:35:08 +0100 | [diff] [blame] | 122 | dev_err(&pdev->dev, "unable to request PWM for backlight\n"); |
Ben Dooks | 43bda1a | 2008-07-01 14:18:27 +0100 | [diff] [blame] | 123 | ret = PTR_ERR(pb->pwm); |
Julia Lawall | ce96922 | 2012-03-23 15:02:00 -0700 | [diff] [blame] | 124 | goto err_alloc; |
Ben Dooks | 14563a4 | 2008-08-05 13:01:22 -0700 | [diff] [blame] | 125 | } else |
| 126 | dev_dbg(&pdev->dev, "got pwm for backlight\n"); |
eric miao | 42796d3 | 2008-04-14 09:35:08 +0100 | [diff] [blame] | 127 | |
Matthew Garrett | a19a6ee | 2010-02-17 16:39:44 -0500 | [diff] [blame] | 128 | memset(&props, 0, sizeof(struct backlight_properties)); |
Matthew Garrett | bb7ca74 | 2011-03-22 16:30:21 -0700 | [diff] [blame] | 129 | props.type = BACKLIGHT_RAW; |
Matthew Garrett | a19a6ee | 2010-02-17 16:39:44 -0500 | [diff] [blame] | 130 | props.max_brightness = data->max_brightness; |
| 131 | bl = backlight_device_register(dev_name(&pdev->dev), &pdev->dev, pb, |
| 132 | &pwm_backlight_ops, &props); |
eric miao | 42796d3 | 2008-04-14 09:35:08 +0100 | [diff] [blame] | 133 | if (IS_ERR(bl)) { |
| 134 | dev_err(&pdev->dev, "failed to register backlight\n"); |
Philipp Zabel | 3b73125 | 2008-05-22 14:18:40 +0100 | [diff] [blame] | 135 | ret = PTR_ERR(bl); |
| 136 | goto err_bl; |
eric miao | 42796d3 | 2008-04-14 09:35:08 +0100 | [diff] [blame] | 137 | } |
| 138 | |
eric miao | 42796d3 | 2008-04-14 09:35:08 +0100 | [diff] [blame] | 139 | bl->props.brightness = data->dft_brightness; |
| 140 | backlight_update_status(bl); |
| 141 | |
| 142 | platform_set_drvdata(pdev, bl); |
| 143 | return 0; |
Philipp Zabel | 3b73125 | 2008-05-22 14:18:40 +0100 | [diff] [blame] | 144 | |
| 145 | err_bl: |
| 146 | pwm_free(pb->pwm); |
Philipp Zabel | 3b73125 | 2008-05-22 14:18:40 +0100 | [diff] [blame] | 147 | err_alloc: |
| 148 | if (data->exit) |
| 149 | data->exit(&pdev->dev); |
| 150 | return ret; |
eric miao | 42796d3 | 2008-04-14 09:35:08 +0100 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | static int pwm_backlight_remove(struct platform_device *pdev) |
| 154 | { |
Philipp Zabel | 3b73125 | 2008-05-22 14:18:40 +0100 | [diff] [blame] | 155 | struct platform_pwm_backlight_data *data = pdev->dev.platform_data; |
eric miao | 42796d3 | 2008-04-14 09:35:08 +0100 | [diff] [blame] | 156 | struct backlight_device *bl = platform_get_drvdata(pdev); |
| 157 | struct pwm_bl_data *pb = dev_get_drvdata(&bl->dev); |
| 158 | |
| 159 | backlight_device_unregister(bl); |
| 160 | pwm_config(pb->pwm, 0, pb->period); |
| 161 | pwm_disable(pb->pwm); |
| 162 | pwm_free(pb->pwm); |
Philipp Zabel | 3b73125 | 2008-05-22 14:18:40 +0100 | [diff] [blame] | 163 | if (data->exit) |
| 164 | data->exit(&pdev->dev); |
eric miao | 42796d3 | 2008-04-14 09:35:08 +0100 | [diff] [blame] | 165 | return 0; |
| 166 | } |
| 167 | |
| 168 | #ifdef CONFIG_PM |
Mark Brown | e2c17bc | 2012-01-10 15:09:22 -0800 | [diff] [blame] | 169 | static int pwm_backlight_suspend(struct device *dev) |
eric miao | 42796d3 | 2008-04-14 09:35:08 +0100 | [diff] [blame] | 170 | { |
Mark Brown | e2c17bc | 2012-01-10 15:09:22 -0800 | [diff] [blame] | 171 | struct backlight_device *bl = dev_get_drvdata(dev); |
eric miao | 42796d3 | 2008-04-14 09:35:08 +0100 | [diff] [blame] | 172 | struct pwm_bl_data *pb = dev_get_drvdata(&bl->dev); |
| 173 | |
Marc Zyngier | 82e8b54 | 2009-06-19 10:50:54 +0200 | [diff] [blame] | 174 | if (pb->notify) |
Ben Dooks | cfc3899 | 2009-11-10 17:20:40 +0000 | [diff] [blame] | 175 | pb->notify(pb->dev, 0); |
eric miao | 42796d3 | 2008-04-14 09:35:08 +0100 | [diff] [blame] | 176 | pwm_config(pb->pwm, 0, pb->period); |
| 177 | pwm_disable(pb->pwm); |
Dilan Lee | cc7993f | 2011-08-25 15:59:17 -0700 | [diff] [blame] | 178 | if (pb->notify_after) |
| 179 | pb->notify_after(pb->dev, 0); |
eric miao | 42796d3 | 2008-04-14 09:35:08 +0100 | [diff] [blame] | 180 | return 0; |
| 181 | } |
| 182 | |
Mark Brown | e2c17bc | 2012-01-10 15:09:22 -0800 | [diff] [blame] | 183 | static int pwm_backlight_resume(struct device *dev) |
eric miao | 42796d3 | 2008-04-14 09:35:08 +0100 | [diff] [blame] | 184 | { |
Mark Brown | e2c17bc | 2012-01-10 15:09:22 -0800 | [diff] [blame] | 185 | struct backlight_device *bl = dev_get_drvdata(dev); |
eric miao | 42796d3 | 2008-04-14 09:35:08 +0100 | [diff] [blame] | 186 | |
| 187 | backlight_update_status(bl); |
| 188 | return 0; |
| 189 | } |
Mark Brown | e2c17bc | 2012-01-10 15:09:22 -0800 | [diff] [blame] | 190 | |
| 191 | static SIMPLE_DEV_PM_OPS(pwm_backlight_pm_ops, pwm_backlight_suspend, |
| 192 | pwm_backlight_resume); |
| 193 | |
eric miao | 42796d3 | 2008-04-14 09:35:08 +0100 | [diff] [blame] | 194 | #endif |
| 195 | |
| 196 | static struct platform_driver pwm_backlight_driver = { |
| 197 | .driver = { |
| 198 | .name = "pwm-backlight", |
| 199 | .owner = THIS_MODULE, |
Mark Brown | e2c17bc | 2012-01-10 15:09:22 -0800 | [diff] [blame] | 200 | #ifdef CONFIG_PM |
| 201 | .pm = &pwm_backlight_pm_ops, |
| 202 | #endif |
eric miao | 42796d3 | 2008-04-14 09:35:08 +0100 | [diff] [blame] | 203 | }, |
| 204 | .probe = pwm_backlight_probe, |
| 205 | .remove = pwm_backlight_remove, |
eric miao | 42796d3 | 2008-04-14 09:35:08 +0100 | [diff] [blame] | 206 | }; |
| 207 | |
Axel Lin | 81178e0 | 2012-01-10 15:09:11 -0800 | [diff] [blame] | 208 | module_platform_driver(pwm_backlight_driver); |
eric miao | 42796d3 | 2008-04-14 09:35:08 +0100 | [diff] [blame] | 209 | |
| 210 | MODULE_DESCRIPTION("PWM based Backlight Driver"); |
| 211 | MODULE_LICENSE("GPL"); |
Ben Dooks | 8cd6819 | 2008-08-05 13:01:24 -0700 | [diff] [blame] | 212 | MODULE_ALIAS("platform:pwm-backlight"); |
| 213 | |