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