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 | |
Alexandre Courbot | 257462d | 2014-02-27 14:53:34 +0900 | [diff] [blame] | 13 | #include <linux/gpio/consumer.h> |
Thierry Reding | 8265b2e | 2013-08-30 12:32:18 +0200 | [diff] [blame] | 14 | #include <linux/gpio.h> |
eric miao | 42796d3 | 2008-04-14 09:35:08 +0100 | [diff] [blame] | 15 | #include <linux/module.h> |
| 16 | #include <linux/kernel.h> |
| 17 | #include <linux/init.h> |
| 18 | #include <linux/platform_device.h> |
| 19 | #include <linux/fb.h> |
| 20 | #include <linux/backlight.h> |
| 21 | #include <linux/err.h> |
| 22 | #include <linux/pwm.h> |
| 23 | #include <linux/pwm_backlight.h> |
Thierry Reding | 22ceeee | 2013-08-30 12:38:34 +0200 | [diff] [blame] | 24 | #include <linux/regulator/consumer.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 25 | #include <linux/slab.h> |
eric miao | 42796d3 | 2008-04-14 09:35:08 +0100 | [diff] [blame] | 26 | |
| 27 | struct pwm_bl_data { |
| 28 | struct pwm_device *pwm; |
Ben Dooks | cfc3899 | 2009-11-10 17:20:40 +0000 | [diff] [blame] | 29 | struct device *dev; |
eric miao | 42796d3 | 2008-04-14 09:35:08 +0100 | [diff] [blame] | 30 | unsigned int period; |
Arun Murthy | fef7764 | 2010-11-11 14:05:28 -0800 | [diff] [blame] | 31 | unsigned int lth_brightness; |
Thierry Reding | 3e3ed6c | 2011-12-16 21:25:29 +0100 | [diff] [blame] | 32 | unsigned int *levels; |
Thierry Reding | 97c3843 | 2013-10-02 18:01:02 +0200 | [diff] [blame] | 33 | bool enabled; |
Thierry Reding | 22ceeee | 2013-08-30 12:38:34 +0200 | [diff] [blame] | 34 | struct regulator *power_supply; |
Alexandre Courbot | 257462d | 2014-02-27 14:53:34 +0900 | [diff] [blame] | 35 | struct gpio_desc *enable_gpio; |
Mike Dunn | 8f43e18 | 2013-09-22 09:59:56 -0700 | [diff] [blame] | 36 | unsigned int scale; |
Vladimir Zapolskiy | edf387b | 2014-10-11 16:46:26 +0300 | [diff] [blame] | 37 | bool legacy; |
Ben Dooks | cfc3899 | 2009-11-10 17:20:40 +0000 | [diff] [blame] | 38 | int (*notify)(struct device *, |
| 39 | int brightness); |
Dilan Lee | cc7993f | 2011-08-25 15:59:17 -0700 | [diff] [blame] | 40 | void (*notify_after)(struct device *, |
| 41 | int brightness); |
Robert Morell | ef0a5e8 | 2011-03-22 16:30:31 -0700 | [diff] [blame] | 42 | int (*check_fb)(struct device *, struct fb_info *); |
Thierry Reding | 3e3ed6c | 2011-12-16 21:25:29 +0100 | [diff] [blame] | 43 | void (*exit)(struct device *); |
eric miao | 42796d3 | 2008-04-14 09:35:08 +0100 | [diff] [blame] | 44 | }; |
| 45 | |
Mike Dunn | 8f43e18 | 2013-09-22 09:59:56 -0700 | [diff] [blame] | 46 | static void pwm_backlight_power_on(struct pwm_bl_data *pb, int brightness) |
Thierry Reding | 62b744a | 2013-10-07 11:32:02 +0200 | [diff] [blame] | 47 | { |
Thierry Reding | 73d4e2b | 2013-10-22 09:37:05 +0200 | [diff] [blame] | 48 | int err; |
Thierry Reding | 62b744a | 2013-10-07 11:32:02 +0200 | [diff] [blame] | 49 | |
Thierry Reding | 97c3843 | 2013-10-02 18:01:02 +0200 | [diff] [blame] | 50 | if (pb->enabled) |
| 51 | return; |
| 52 | |
Thierry Reding | 22ceeee | 2013-08-30 12:38:34 +0200 | [diff] [blame] | 53 | err = regulator_enable(pb->power_supply); |
| 54 | if (err < 0) |
| 55 | dev_err(pb->dev, "failed to enable power supply\n"); |
| 56 | |
Alexandre Courbot | 257462d | 2014-02-27 14:53:34 +0900 | [diff] [blame] | 57 | if (pb->enable_gpio) |
| 58 | gpiod_set_value(pb->enable_gpio, 1); |
Thierry Reding | 8265b2e | 2013-08-30 12:32:18 +0200 | [diff] [blame] | 59 | |
Thierry Reding | 62b744a | 2013-10-07 11:32:02 +0200 | [diff] [blame] | 60 | pwm_enable(pb->pwm); |
Thierry Reding | 97c3843 | 2013-10-02 18:01:02 +0200 | [diff] [blame] | 61 | pb->enabled = true; |
Thierry Reding | 62b744a | 2013-10-07 11:32:02 +0200 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | static void pwm_backlight_power_off(struct pwm_bl_data *pb) |
| 65 | { |
Thierry Reding | 97c3843 | 2013-10-02 18:01:02 +0200 | [diff] [blame] | 66 | if (!pb->enabled) |
| 67 | return; |
| 68 | |
Thierry Reding | 62b744a | 2013-10-07 11:32:02 +0200 | [diff] [blame] | 69 | pwm_config(pb->pwm, 0, pb->period); |
| 70 | pwm_disable(pb->pwm); |
Thierry Reding | 97c3843 | 2013-10-02 18:01:02 +0200 | [diff] [blame] | 71 | |
Alexandre Courbot | 257462d | 2014-02-27 14:53:34 +0900 | [diff] [blame] | 72 | if (pb->enable_gpio) |
| 73 | gpiod_set_value(pb->enable_gpio, 0); |
Thierry Reding | 8265b2e | 2013-08-30 12:32:18 +0200 | [diff] [blame] | 74 | |
Thierry Reding | 22ceeee | 2013-08-30 12:38:34 +0200 | [diff] [blame] | 75 | regulator_disable(pb->power_supply); |
Thierry Reding | 97c3843 | 2013-10-02 18:01:02 +0200 | [diff] [blame] | 76 | pb->enabled = false; |
Thierry Reding | 62b744a | 2013-10-07 11:32:02 +0200 | [diff] [blame] | 77 | } |
| 78 | |
Thierry Reding | e4bfeda | 2013-10-18 10:46:24 +0200 | [diff] [blame] | 79 | static int compute_duty_cycle(struct pwm_bl_data *pb, int brightness) |
| 80 | { |
| 81 | unsigned int lth = pb->lth_brightness; |
| 82 | int duty_cycle; |
| 83 | |
| 84 | if (pb->levels) |
| 85 | duty_cycle = pb->levels[brightness]; |
| 86 | else |
| 87 | duty_cycle = brightness; |
| 88 | |
| 89 | return (duty_cycle * (pb->period - lth) / pb->scale) + lth; |
| 90 | } |
| 91 | |
eric miao | 42796d3 | 2008-04-14 09:35:08 +0100 | [diff] [blame] | 92 | static int pwm_backlight_update_status(struct backlight_device *bl) |
| 93 | { |
Jingoo Han | e6e3dbf | 2013-02-21 16:43:46 -0800 | [diff] [blame] | 94 | struct pwm_bl_data *pb = bl_get_data(bl); |
eric miao | 42796d3 | 2008-04-14 09:35:08 +0100 | [diff] [blame] | 95 | int brightness = bl->props.brightness; |
Thierry Reding | e4bfeda | 2013-10-18 10:46:24 +0200 | [diff] [blame] | 96 | int duty_cycle; |
eric miao | 42796d3 | 2008-04-14 09:35:08 +0100 | [diff] [blame] | 97 | |
Alexandre Courbot | 0132267 | 2013-01-30 15:19:15 +0900 | [diff] [blame] | 98 | if (bl->props.power != FB_BLANK_UNBLANK || |
| 99 | bl->props.fb_blank != FB_BLANK_UNBLANK || |
| 100 | bl->props.state & BL_CORE_FBBLANK) |
eric miao | 42796d3 | 2008-04-14 09:35:08 +0100 | [diff] [blame] | 101 | brightness = 0; |
| 102 | |
Philipp Zabel | 3b73125 | 2008-05-22 14:18:40 +0100 | [diff] [blame] | 103 | if (pb->notify) |
Ben Dooks | cfc3899 | 2009-11-10 17:20:40 +0000 | [diff] [blame] | 104 | brightness = pb->notify(pb->dev, brightness); |
Philipp Zabel | 3b73125 | 2008-05-22 14:18:40 +0100 | [diff] [blame] | 105 | |
Thierry Reding | e4bfeda | 2013-10-18 10:46:24 +0200 | [diff] [blame] | 106 | if (brightness > 0) { |
| 107 | duty_cycle = compute_duty_cycle(pb, brightness); |
Alexandre Courbot | 9fb978b | 2012-07-09 15:04:23 +0900 | [diff] [blame] | 108 | pwm_config(pb->pwm, duty_cycle, pb->period); |
Mike Dunn | 8f43e18 | 2013-09-22 09:59:56 -0700 | [diff] [blame] | 109 | pwm_backlight_power_on(pb, brightness); |
Thierry Reding | e4bfeda | 2013-10-18 10:46:24 +0200 | [diff] [blame] | 110 | } else |
Thierry Reding | 62b744a | 2013-10-07 11:32:02 +0200 | [diff] [blame] | 111 | pwm_backlight_power_off(pb); |
Dilan Lee | cc7993f | 2011-08-25 15:59:17 -0700 | [diff] [blame] | 112 | |
| 113 | if (pb->notify_after) |
| 114 | pb->notify_after(pb->dev, brightness); |
| 115 | |
eric miao | 42796d3 | 2008-04-14 09:35:08 +0100 | [diff] [blame] | 116 | return 0; |
| 117 | } |
| 118 | |
Robert Morell | ef0a5e8 | 2011-03-22 16:30:31 -0700 | [diff] [blame] | 119 | static int pwm_backlight_check_fb(struct backlight_device *bl, |
| 120 | struct fb_info *info) |
| 121 | { |
Jingoo Han | e6e3dbf | 2013-02-21 16:43:46 -0800 | [diff] [blame] | 122 | struct pwm_bl_data *pb = bl_get_data(bl); |
Robert Morell | ef0a5e8 | 2011-03-22 16:30:31 -0700 | [diff] [blame] | 123 | |
| 124 | return !pb->check_fb || pb->check_fb(pb->dev, info); |
| 125 | } |
| 126 | |
Emese Revfy | 9905a43 | 2009-12-14 00:58:57 +0100 | [diff] [blame] | 127 | static const struct backlight_ops pwm_backlight_ops = { |
eric miao | 42796d3 | 2008-04-14 09:35:08 +0100 | [diff] [blame] | 128 | .update_status = pwm_backlight_update_status, |
Robert Morell | ef0a5e8 | 2011-03-22 16:30:31 -0700 | [diff] [blame] | 129 | .check_fb = pwm_backlight_check_fb, |
eric miao | 42796d3 | 2008-04-14 09:35:08 +0100 | [diff] [blame] | 130 | }; |
| 131 | |
Thierry Reding | 3e3ed6c | 2011-12-16 21:25:29 +0100 | [diff] [blame] | 132 | #ifdef CONFIG_OF |
| 133 | static int pwm_backlight_parse_dt(struct device *dev, |
| 134 | struct platform_pwm_backlight_data *data) |
| 135 | { |
| 136 | struct device_node *node = dev->of_node; |
| 137 | struct property *prop; |
| 138 | int length; |
| 139 | u32 value; |
| 140 | int ret; |
| 141 | |
| 142 | if (!node) |
| 143 | return -ENODEV; |
| 144 | |
| 145 | memset(data, 0, sizeof(*data)); |
| 146 | |
| 147 | /* determine the number of brightness levels */ |
| 148 | prop = of_find_property(node, "brightness-levels", &length); |
| 149 | if (!prop) |
| 150 | return -EINVAL; |
| 151 | |
| 152 | data->max_brightness = length / sizeof(u32); |
| 153 | |
| 154 | /* read brightness levels from DT property */ |
| 155 | if (data->max_brightness > 0) { |
| 156 | size_t size = sizeof(*data->levels) * data->max_brightness; |
| 157 | |
| 158 | data->levels = devm_kzalloc(dev, size, GFP_KERNEL); |
| 159 | if (!data->levels) |
| 160 | return -ENOMEM; |
| 161 | |
| 162 | ret = of_property_read_u32_array(node, "brightness-levels", |
| 163 | data->levels, |
| 164 | data->max_brightness); |
| 165 | if (ret < 0) |
| 166 | return ret; |
| 167 | |
| 168 | ret = of_property_read_u32(node, "default-brightness-level", |
| 169 | &value); |
| 170 | if (ret < 0) |
| 171 | return ret; |
| 172 | |
Thierry Reding | 3e3ed6c | 2011-12-16 21:25:29 +0100 | [diff] [blame] | 173 | data->dft_brightness = value; |
| 174 | data->max_brightness--; |
| 175 | } |
| 176 | |
Lothar Waßmann | 937222c | 2014-08-20 08:38:36 +0200 | [diff] [blame] | 177 | data->enable_gpio = -EINVAL; |
Thierry Reding | 3e3ed6c | 2011-12-16 21:25:29 +0100 | [diff] [blame] | 178 | return 0; |
| 179 | } |
| 180 | |
| 181 | static struct of_device_id pwm_backlight_of_match[] = { |
| 182 | { .compatible = "pwm-backlight" }, |
| 183 | { } |
| 184 | }; |
| 185 | |
| 186 | MODULE_DEVICE_TABLE(of, pwm_backlight_of_match); |
| 187 | #else |
| 188 | static int pwm_backlight_parse_dt(struct device *dev, |
| 189 | struct platform_pwm_backlight_data *data) |
| 190 | { |
| 191 | return -ENODEV; |
| 192 | } |
| 193 | #endif |
| 194 | |
eric miao | 42796d3 | 2008-04-14 09:35:08 +0100 | [diff] [blame] | 195 | static int pwm_backlight_probe(struct platform_device *pdev) |
| 196 | { |
Jingoo Han | c512794c | 2013-11-12 15:09:04 -0800 | [diff] [blame] | 197 | struct platform_pwm_backlight_data *data = dev_get_platdata(&pdev->dev); |
Thierry Reding | 3e3ed6c | 2011-12-16 21:25:29 +0100 | [diff] [blame] | 198 | struct platform_pwm_backlight_data defdata; |
| 199 | struct backlight_properties props; |
eric miao | 42796d3 | 2008-04-14 09:35:08 +0100 | [diff] [blame] | 200 | struct backlight_device *bl; |
| 201 | struct pwm_bl_data *pb; |
Philipp Zabel | 3b73125 | 2008-05-22 14:18:40 +0100 | [diff] [blame] | 202 | int ret; |
eric miao | 42796d3 | 2008-04-14 09:35:08 +0100 | [diff] [blame] | 203 | |
Ben Dooks | 14563a4 | 2008-08-05 13:01:22 -0700 | [diff] [blame] | 204 | if (!data) { |
Thierry Reding | 3e3ed6c | 2011-12-16 21:25:29 +0100 | [diff] [blame] | 205 | ret = pwm_backlight_parse_dt(&pdev->dev, &defdata); |
| 206 | if (ret < 0) { |
| 207 | dev_err(&pdev->dev, "failed to find platform data\n"); |
| 208 | return ret; |
| 209 | } |
| 210 | |
| 211 | data = &defdata; |
Ben Dooks | 14563a4 | 2008-08-05 13:01:22 -0700 | [diff] [blame] | 212 | } |
eric miao | 42796d3 | 2008-04-14 09:35:08 +0100 | [diff] [blame] | 213 | |
Philipp Zabel | 3b73125 | 2008-05-22 14:18:40 +0100 | [diff] [blame] | 214 | if (data->init) { |
| 215 | ret = data->init(&pdev->dev); |
| 216 | if (ret < 0) |
| 217 | return ret; |
| 218 | } |
| 219 | |
Julia Lawall | ce96922 | 2012-03-23 15:02:00 -0700 | [diff] [blame] | 220 | pb = devm_kzalloc(&pdev->dev, sizeof(*pb), GFP_KERNEL); |
Philipp Zabel | 3b73125 | 2008-05-22 14:18:40 +0100 | [diff] [blame] | 221 | if (!pb) { |
| 222 | ret = -ENOMEM; |
| 223 | goto err_alloc; |
| 224 | } |
eric miao | 42796d3 | 2008-04-14 09:35:08 +0100 | [diff] [blame] | 225 | |
Thierry Reding | 3e3ed6c | 2011-12-16 21:25:29 +0100 | [diff] [blame] | 226 | if (data->levels) { |
Mike Dunn | 8f43e18 | 2013-09-22 09:59:56 -0700 | [diff] [blame] | 227 | unsigned int i; |
| 228 | |
| 229 | for (i = 0; i <= data->max_brightness; i++) |
| 230 | if (data->levels[i] > pb->scale) |
| 231 | pb->scale = data->levels[i]; |
| 232 | |
Thierry Reding | 3e3ed6c | 2011-12-16 21:25:29 +0100 | [diff] [blame] | 233 | pb->levels = data->levels; |
| 234 | } else |
Mike Dunn | 8f43e18 | 2013-09-22 09:59:56 -0700 | [diff] [blame] | 235 | pb->scale = data->max_brightness; |
Thierry Reding | 3e3ed6c | 2011-12-16 21:25:29 +0100 | [diff] [blame] | 236 | |
Philipp Zabel | 3b73125 | 2008-05-22 14:18:40 +0100 | [diff] [blame] | 237 | pb->notify = data->notify; |
Dilan Lee | cc7993f | 2011-08-25 15:59:17 -0700 | [diff] [blame] | 238 | pb->notify_after = data->notify_after; |
Robert Morell | ef0a5e8 | 2011-03-22 16:30:31 -0700 | [diff] [blame] | 239 | pb->check_fb = data->check_fb; |
Thierry Reding | 3e3ed6c | 2011-12-16 21:25:29 +0100 | [diff] [blame] | 240 | pb->exit = data->exit; |
Ben Dooks | cfc3899 | 2009-11-10 17:20:40 +0000 | [diff] [blame] | 241 | pb->dev = &pdev->dev; |
Thierry Reding | 97c3843 | 2013-10-02 18:01:02 +0200 | [diff] [blame] | 242 | pb->enabled = false; |
eric miao | 42796d3 | 2008-04-14 09:35:08 +0100 | [diff] [blame] | 243 | |
Alexandre Courbot | ff9c542 | 2014-06-25 18:18:18 +0900 | [diff] [blame] | 244 | pb->enable_gpio = devm_gpiod_get_optional(&pdev->dev, "enable"); |
Alexandre Courbot | 257462d | 2014-02-27 14:53:34 +0900 | [diff] [blame] | 245 | if (IS_ERR(pb->enable_gpio)) { |
| 246 | ret = PTR_ERR(pb->enable_gpio); |
Alexandre Courbot | ff9c542 | 2014-06-25 18:18:18 +0900 | [diff] [blame] | 247 | goto err_alloc; |
Alexandre Courbot | 257462d | 2014-02-27 14:53:34 +0900 | [diff] [blame] | 248 | } |
Thierry Reding | 8265b2e | 2013-08-30 12:32:18 +0200 | [diff] [blame] | 249 | |
Alexandre Courbot | 257462d | 2014-02-27 14:53:34 +0900 | [diff] [blame] | 250 | /* |
| 251 | * Compatibility fallback for drivers still using the integer GPIO |
| 252 | * platform data. Must go away soon. |
| 253 | */ |
| 254 | if (!pb->enable_gpio && gpio_is_valid(data->enable_gpio)) { |
| 255 | ret = devm_gpio_request_one(&pdev->dev, data->enable_gpio, |
| 256 | GPIOF_OUT_INIT_HIGH, "enable"); |
Thierry Reding | 8265b2e | 2013-08-30 12:32:18 +0200 | [diff] [blame] | 257 | if (ret < 0) { |
| 258 | dev_err(&pdev->dev, "failed to request GPIO#%d: %d\n", |
Alexandre Courbot | 257462d | 2014-02-27 14:53:34 +0900 | [diff] [blame] | 259 | data->enable_gpio, ret); |
Thierry Reding | 8265b2e | 2013-08-30 12:32:18 +0200 | [diff] [blame] | 260 | goto err_alloc; |
| 261 | } |
Alexandre Courbot | 257462d | 2014-02-27 14:53:34 +0900 | [diff] [blame] | 262 | |
| 263 | pb->enable_gpio = gpio_to_desc(data->enable_gpio); |
Thierry Reding | 8265b2e | 2013-08-30 12:32:18 +0200 | [diff] [blame] | 264 | } |
| 265 | |
Alexandre Courbot | 257462d | 2014-02-27 14:53:34 +0900 | [diff] [blame] | 266 | if (pb->enable_gpio) |
| 267 | gpiod_direction_output(pb->enable_gpio, 1); |
| 268 | |
Thierry Reding | 22ceeee | 2013-08-30 12:38:34 +0200 | [diff] [blame] | 269 | pb->power_supply = devm_regulator_get(&pdev->dev, "power"); |
| 270 | if (IS_ERR(pb->power_supply)) { |
| 271 | ret = PTR_ERR(pb->power_supply); |
Alexandre Courbot | 257462d | 2014-02-27 14:53:34 +0900 | [diff] [blame] | 272 | goto err_alloc; |
Thierry Reding | 22ceeee | 2013-08-30 12:38:34 +0200 | [diff] [blame] | 273 | } |
eric miao | 42796d3 | 2008-04-14 09:35:08 +0100 | [diff] [blame] | 274 | |
Sachin Kamat | 60ce702 | 2012-09-17 11:50:47 +0530 | [diff] [blame] | 275 | pb->pwm = devm_pwm_get(&pdev->dev, NULL); |
Ben Dooks | 43bda1a | 2008-07-01 14:18:27 +0100 | [diff] [blame] | 276 | if (IS_ERR(pb->pwm)) { |
Thierry Reding | 3e3ed6c | 2011-12-16 21:25:29 +0100 | [diff] [blame] | 277 | dev_err(&pdev->dev, "unable to request PWM, trying legacy API\n"); |
Vladimir Zapolskiy | edf387b | 2014-10-11 16:46:26 +0300 | [diff] [blame] | 278 | pb->legacy = true; |
Thierry Reding | 3e3ed6c | 2011-12-16 21:25:29 +0100 | [diff] [blame] | 279 | pb->pwm = pwm_request(data->pwm_id, "pwm-backlight"); |
| 280 | if (IS_ERR(pb->pwm)) { |
| 281 | dev_err(&pdev->dev, "unable to request legacy PWM\n"); |
| 282 | ret = PTR_ERR(pb->pwm); |
Alexandre Courbot | 257462d | 2014-02-27 14:53:34 +0900 | [diff] [blame] | 283 | goto err_alloc; |
Thierry Reding | 3e3ed6c | 2011-12-16 21:25:29 +0100 | [diff] [blame] | 284 | } |
| 285 | } |
| 286 | |
| 287 | dev_dbg(&pdev->dev, "got pwm for backlight\n"); |
| 288 | |
| 289 | /* |
| 290 | * The DT case will set the pwm_period_ns field to 0 and store the |
| 291 | * period, parsed from the DT, in the PWM device. For the non-DT case, |
Alexandre Belloni | 16fc335 | 2014-05-19 22:42:42 +0200 | [diff] [blame] | 292 | * set the period from platform data if it has not already been set |
| 293 | * via the PWM lookup table. |
Thierry Reding | 3e3ed6c | 2011-12-16 21:25:29 +0100 | [diff] [blame] | 294 | */ |
Thierry Reding | 3e3ed6c | 2011-12-16 21:25:29 +0100 | [diff] [blame] | 295 | pb->period = pwm_get_period(pb->pwm); |
Alexandre Belloni | 16fc335 | 2014-05-19 22:42:42 +0200 | [diff] [blame] | 296 | if (!pb->period && (data->pwm_period_ns > 0)) { |
| 297 | pb->period = data->pwm_period_ns; |
| 298 | pwm_set_period(pb->pwm, data->pwm_period_ns); |
| 299 | } |
| 300 | |
Mike Dunn | 8f43e18 | 2013-09-22 09:59:56 -0700 | [diff] [blame] | 301 | pb->lth_brightness = data->lth_brightness * (pb->period / pb->scale); |
eric miao | 42796d3 | 2008-04-14 09:35:08 +0100 | [diff] [blame] | 302 | |
Matthew Garrett | a19a6ee | 2010-02-17 16:39:44 -0500 | [diff] [blame] | 303 | memset(&props, 0, sizeof(struct backlight_properties)); |
Matthew Garrett | bb7ca74 | 2011-03-22 16:30:21 -0700 | [diff] [blame] | 304 | props.type = BACKLIGHT_RAW; |
Matthew Garrett | a19a6ee | 2010-02-17 16:39:44 -0500 | [diff] [blame] | 305 | props.max_brightness = data->max_brightness; |
| 306 | bl = backlight_device_register(dev_name(&pdev->dev), &pdev->dev, pb, |
| 307 | &pwm_backlight_ops, &props); |
eric miao | 42796d3 | 2008-04-14 09:35:08 +0100 | [diff] [blame] | 308 | if (IS_ERR(bl)) { |
| 309 | dev_err(&pdev->dev, "failed to register backlight\n"); |
Philipp Zabel | 3b73125 | 2008-05-22 14:18:40 +0100 | [diff] [blame] | 310 | ret = PTR_ERR(bl); |
Alexandre Courbot | 257462d | 2014-02-27 14:53:34 +0900 | [diff] [blame] | 311 | goto err_alloc; |
eric miao | 42796d3 | 2008-04-14 09:35:08 +0100 | [diff] [blame] | 312 | } |
| 313 | |
Peter Ujfalusi | 83cfd72 | 2013-01-22 14:39:54 +0100 | [diff] [blame] | 314 | if (data->dft_brightness > data->max_brightness) { |
| 315 | dev_warn(&pdev->dev, |
| 316 | "invalid default brightness level: %u, using %u\n", |
| 317 | data->dft_brightness, data->max_brightness); |
| 318 | data->dft_brightness = data->max_brightness; |
| 319 | } |
| 320 | |
eric miao | 42796d3 | 2008-04-14 09:35:08 +0100 | [diff] [blame] | 321 | bl->props.brightness = data->dft_brightness; |
| 322 | backlight_update_status(bl); |
| 323 | |
| 324 | platform_set_drvdata(pdev, bl); |
| 325 | return 0; |
Philipp Zabel | 3b73125 | 2008-05-22 14:18:40 +0100 | [diff] [blame] | 326 | |
Philipp Zabel | 3b73125 | 2008-05-22 14:18:40 +0100 | [diff] [blame] | 327 | err_alloc: |
| 328 | if (data->exit) |
| 329 | data->exit(&pdev->dev); |
| 330 | return ret; |
eric miao | 42796d3 | 2008-04-14 09:35:08 +0100 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | static int pwm_backlight_remove(struct platform_device *pdev) |
| 334 | { |
| 335 | struct backlight_device *bl = platform_get_drvdata(pdev); |
Jingoo Han | e6e3dbf | 2013-02-21 16:43:46 -0800 | [diff] [blame] | 336 | struct pwm_bl_data *pb = bl_get_data(bl); |
eric miao | 42796d3 | 2008-04-14 09:35:08 +0100 | [diff] [blame] | 337 | |
| 338 | backlight_device_unregister(bl); |
Thierry Reding | 62b744a | 2013-10-07 11:32:02 +0200 | [diff] [blame] | 339 | pwm_backlight_power_off(pb); |
Thierry Reding | 668e63c | 2013-10-07 11:30:50 +0200 | [diff] [blame] | 340 | |
Thierry Reding | 3e3ed6c | 2011-12-16 21:25:29 +0100 | [diff] [blame] | 341 | if (pb->exit) |
| 342 | pb->exit(&pdev->dev); |
Vladimir Zapolskiy | edf387b | 2014-10-11 16:46:26 +0300 | [diff] [blame] | 343 | if (pb->legacy) |
| 344 | pwm_free(pb->pwm); |
Thierry Reding | 668e63c | 2013-10-07 11:30:50 +0200 | [diff] [blame] | 345 | |
eric miao | 42796d3 | 2008-04-14 09:35:08 +0100 | [diff] [blame] | 346 | return 0; |
| 347 | } |
| 348 | |
Thierry Reding | 5f33b89 | 2014-04-29 17:28:59 +0200 | [diff] [blame] | 349 | static void pwm_backlight_shutdown(struct platform_device *pdev) |
| 350 | { |
| 351 | struct backlight_device *bl = platform_get_drvdata(pdev); |
| 352 | struct pwm_bl_data *pb = bl_get_data(bl); |
| 353 | |
| 354 | pwm_backlight_power_off(pb); |
| 355 | } |
| 356 | |
Jingoo Han | c791126 | 2013-02-25 17:15:47 +0900 | [diff] [blame] | 357 | #ifdef CONFIG_PM_SLEEP |
Mark Brown | e2c17bc | 2012-01-10 15:09:22 -0800 | [diff] [blame] | 358 | static int pwm_backlight_suspend(struct device *dev) |
eric miao | 42796d3 | 2008-04-14 09:35:08 +0100 | [diff] [blame] | 359 | { |
Mark Brown | e2c17bc | 2012-01-10 15:09:22 -0800 | [diff] [blame] | 360 | struct backlight_device *bl = dev_get_drvdata(dev); |
Jingoo Han | e6e3dbf | 2013-02-21 16:43:46 -0800 | [diff] [blame] | 361 | struct pwm_bl_data *pb = bl_get_data(bl); |
eric miao | 42796d3 | 2008-04-14 09:35:08 +0100 | [diff] [blame] | 362 | |
Marc Zyngier | 82e8b54 | 2009-06-19 10:50:54 +0200 | [diff] [blame] | 363 | if (pb->notify) |
Ben Dooks | cfc3899 | 2009-11-10 17:20:40 +0000 | [diff] [blame] | 364 | pb->notify(pb->dev, 0); |
Thierry Reding | 668e63c | 2013-10-07 11:30:50 +0200 | [diff] [blame] | 365 | |
Thierry Reding | 62b744a | 2013-10-07 11:32:02 +0200 | [diff] [blame] | 366 | pwm_backlight_power_off(pb); |
Thierry Reding | 668e63c | 2013-10-07 11:30:50 +0200 | [diff] [blame] | 367 | |
Dilan Lee | cc7993f | 2011-08-25 15:59:17 -0700 | [diff] [blame] | 368 | if (pb->notify_after) |
| 369 | pb->notify_after(pb->dev, 0); |
Thierry Reding | 668e63c | 2013-10-07 11:30:50 +0200 | [diff] [blame] | 370 | |
eric miao | 42796d3 | 2008-04-14 09:35:08 +0100 | [diff] [blame] | 371 | return 0; |
| 372 | } |
| 373 | |
Mark Brown | e2c17bc | 2012-01-10 15:09:22 -0800 | [diff] [blame] | 374 | static int pwm_backlight_resume(struct device *dev) |
eric miao | 42796d3 | 2008-04-14 09:35:08 +0100 | [diff] [blame] | 375 | { |
Mark Brown | e2c17bc | 2012-01-10 15:09:22 -0800 | [diff] [blame] | 376 | struct backlight_device *bl = dev_get_drvdata(dev); |
eric miao | 42796d3 | 2008-04-14 09:35:08 +0100 | [diff] [blame] | 377 | |
| 378 | backlight_update_status(bl); |
Thierry Reding | 668e63c | 2013-10-07 11:30:50 +0200 | [diff] [blame] | 379 | |
eric miao | 42796d3 | 2008-04-14 09:35:08 +0100 | [diff] [blame] | 380 | return 0; |
| 381 | } |
Jingoo Han | c791126 | 2013-02-25 17:15:47 +0900 | [diff] [blame] | 382 | #endif |
Mark Brown | e2c17bc | 2012-01-10 15:09:22 -0800 | [diff] [blame] | 383 | |
Huayi Li | 1dea1fd | 2013-10-09 10:33:02 +0800 | [diff] [blame] | 384 | static const struct dev_pm_ops pwm_backlight_pm_ops = { |
| 385 | #ifdef CONFIG_PM_SLEEP |
| 386 | .suspend = pwm_backlight_suspend, |
| 387 | .resume = pwm_backlight_resume, |
| 388 | .poweroff = pwm_backlight_suspend, |
| 389 | .restore = pwm_backlight_resume, |
| 390 | #endif |
| 391 | }; |
Mark Brown | e2c17bc | 2012-01-10 15:09:22 -0800 | [diff] [blame] | 392 | |
eric miao | 42796d3 | 2008-04-14 09:35:08 +0100 | [diff] [blame] | 393 | static struct platform_driver pwm_backlight_driver = { |
| 394 | .driver = { |
Thierry Reding | 3e3ed6c | 2011-12-16 21:25:29 +0100 | [diff] [blame] | 395 | .name = "pwm-backlight", |
Thierry Reding | 3e3ed6c | 2011-12-16 21:25:29 +0100 | [diff] [blame] | 396 | .pm = &pwm_backlight_pm_ops, |
Thierry Reding | 3e3ed6c | 2011-12-16 21:25:29 +0100 | [diff] [blame] | 397 | .of_match_table = of_match_ptr(pwm_backlight_of_match), |
eric miao | 42796d3 | 2008-04-14 09:35:08 +0100 | [diff] [blame] | 398 | }, |
| 399 | .probe = pwm_backlight_probe, |
| 400 | .remove = pwm_backlight_remove, |
Thierry Reding | 5f33b89 | 2014-04-29 17:28:59 +0200 | [diff] [blame] | 401 | .shutdown = pwm_backlight_shutdown, |
eric miao | 42796d3 | 2008-04-14 09:35:08 +0100 | [diff] [blame] | 402 | }; |
| 403 | |
Axel Lin | 81178e0 | 2012-01-10 15:09:11 -0800 | [diff] [blame] | 404 | module_platform_driver(pwm_backlight_driver); |
eric miao | 42796d3 | 2008-04-14 09:35:08 +0100 | [diff] [blame] | 405 | |
| 406 | MODULE_DESCRIPTION("PWM based Backlight Driver"); |
| 407 | MODULE_LICENSE("GPL"); |
Ben Dooks | 8cd6819 | 2008-08-05 13:01:24 -0700 | [diff] [blame] | 408 | MODULE_ALIAS("platform:pwm-backlight"); |