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