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