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