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 6 * 2) platform_data being correctly configured 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 #include <linux/slab.h> 23 24 struct pwm_bl_data { 25 struct pwm_device *pwm; 26 struct device *dev; 27 unsigned int period; 28 unsigned int lth_brightness; 29 int (*notify)(struct device *, 30 int brightness); 31 void (*notify_after)(struct device *, 32 int brightness); 33 int (*check_fb)(struct device *, struct fb_info *); 34 }; 35 36 static int pwm_backlight_update_status(struct backlight_device *bl) 37 { 38 struct pwm_bl_data *pb = dev_get_drvdata(&bl->dev); 39 int brightness = bl->props.brightness; 40 int max = bl->props.max_brightness; 41 42 if (bl->props.power != FB_BLANK_UNBLANK) 43 brightness = 0; 44 45 if (bl->props.fb_blank != FB_BLANK_UNBLANK) 46 brightness = 0; 47 48 if (pb->notify) 49 brightness = pb->notify(pb->dev, brightness); 50 51 if (brightness == 0) { 52 pwm_config(pb->pwm, 0, pb->period); 53 pwm_disable(pb->pwm); 54 } else { 55 brightness = pb->lth_brightness + 56 (brightness * (pb->period - pb->lth_brightness) / max); 57 pwm_config(pb->pwm, brightness, pb->period); 58 pwm_enable(pb->pwm); 59 } 60 61 if (pb->notify_after) 62 pb->notify_after(pb->dev, brightness); 63 64 return 0; 65 } 66 67 static int pwm_backlight_get_brightness(struct backlight_device *bl) 68 { 69 return bl->props.brightness; 70 } 71 72 static int pwm_backlight_check_fb(struct backlight_device *bl, 73 struct fb_info *info) 74 { 75 struct pwm_bl_data *pb = dev_get_drvdata(&bl->dev); 76 77 return !pb->check_fb || pb->check_fb(pb->dev, info); 78 } 79 80 static const struct backlight_ops pwm_backlight_ops = { 81 .update_status = pwm_backlight_update_status, 82 .get_brightness = pwm_backlight_get_brightness, 83 .check_fb = pwm_backlight_check_fb, 84 }; 85 86 static int pwm_backlight_probe(struct platform_device *pdev) 87 { 88 struct backlight_properties props; 89 struct platform_pwm_backlight_data *data = pdev->dev.platform_data; 90 struct backlight_device *bl; 91 struct pwm_bl_data *pb; 92 int ret; 93 94 if (!data) { 95 dev_err(&pdev->dev, "failed to find platform data\n"); 96 return -EINVAL; 97 } 98 99 if (data->init) { 100 ret = data->init(&pdev->dev); 101 if (ret < 0) 102 return ret; 103 } 104 105 pb = kzalloc(sizeof(*pb), GFP_KERNEL); 106 if (!pb) { 107 dev_err(&pdev->dev, "no memory for state\n"); 108 ret = -ENOMEM; 109 goto err_alloc; 110 } 111 112 pb->period = data->pwm_period_ns; 113 pb->notify = data->notify; 114 pb->notify_after = data->notify_after; 115 pb->check_fb = data->check_fb; 116 pb->lth_brightness = data->lth_brightness * 117 (data->pwm_period_ns / data->max_brightness); 118 pb->dev = &pdev->dev; 119 120 pb->pwm = pwm_request(data->pwm_id, "backlight"); 121 if (IS_ERR(pb->pwm)) { 122 dev_err(&pdev->dev, "unable to request PWM for backlight\n"); 123 ret = PTR_ERR(pb->pwm); 124 goto err_pwm; 125 } else 126 dev_dbg(&pdev->dev, "got pwm for backlight\n"); 127 128 memset(&props, 0, sizeof(struct backlight_properties)); 129 props.type = BACKLIGHT_RAW; 130 props.max_brightness = data->max_brightness; 131 bl = backlight_device_register(dev_name(&pdev->dev), &pdev->dev, pb, 132 &pwm_backlight_ops, &props); 133 if (IS_ERR(bl)) { 134 dev_err(&pdev->dev, "failed to register backlight\n"); 135 ret = PTR_ERR(bl); 136 goto err_bl; 137 } 138 139 bl->props.brightness = data->dft_brightness; 140 backlight_update_status(bl); 141 142 platform_set_drvdata(pdev, bl); 143 return 0; 144 145 err_bl: 146 pwm_free(pb->pwm); 147 err_pwm: 148 kfree(pb); 149 err_alloc: 150 if (data->exit) 151 data->exit(&pdev->dev); 152 return ret; 153 } 154 155 static int pwm_backlight_remove(struct platform_device *pdev) 156 { 157 struct platform_pwm_backlight_data *data = pdev->dev.platform_data; 158 struct backlight_device *bl = platform_get_drvdata(pdev); 159 struct pwm_bl_data *pb = dev_get_drvdata(&bl->dev); 160 161 backlight_device_unregister(bl); 162 pwm_config(pb->pwm, 0, pb->period); 163 pwm_disable(pb->pwm); 164 pwm_free(pb->pwm); 165 kfree(pb); 166 if (data->exit) 167 data->exit(&pdev->dev); 168 return 0; 169 } 170 171 #ifdef CONFIG_PM 172 static int pwm_backlight_suspend(struct platform_device *pdev, 173 pm_message_t state) 174 { 175 struct backlight_device *bl = platform_get_drvdata(pdev); 176 struct pwm_bl_data *pb = dev_get_drvdata(&bl->dev); 177 178 if (pb->notify) 179 pb->notify(pb->dev, 0); 180 pwm_config(pb->pwm, 0, pb->period); 181 pwm_disable(pb->pwm); 182 if (pb->notify_after) 183 pb->notify_after(pb->dev, 0); 184 return 0; 185 } 186 187 static int pwm_backlight_resume(struct platform_device *pdev) 188 { 189 struct backlight_device *bl = platform_get_drvdata(pdev); 190 191 backlight_update_status(bl); 192 return 0; 193 } 194 #else 195 #define pwm_backlight_suspend NULL 196 #define pwm_backlight_resume NULL 197 #endif 198 199 static struct platform_driver pwm_backlight_driver = { 200 .driver = { 201 .name = "pwm-backlight", 202 .owner = THIS_MODULE, 203 }, 204 .probe = pwm_backlight_probe, 205 .remove = pwm_backlight_remove, 206 .suspend = pwm_backlight_suspend, 207 .resume = pwm_backlight_resume, 208 }; 209 210 static int __init pwm_backlight_init(void) 211 { 212 return platform_driver_register(&pwm_backlight_driver); 213 } 214 module_init(pwm_backlight_init); 215 216 static void __exit pwm_backlight_exit(void) 217 { 218 platform_driver_unregister(&pwm_backlight_driver); 219 } 220 module_exit(pwm_backlight_exit); 221 222 MODULE_DESCRIPTION("PWM based Backlight Driver"); 223 MODULE_LICENSE("GPL"); 224 MODULE_ALIAS("platform:pwm-backlight"); 225 226