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