1 /* drivers/video/backlight/platform_lcd.c 2 * 3 * Copyright 2008 Simtec Electronics 4 * Ben Dooks <ben@simtec.co.uk> 5 * 6 * Generic platform-device LCD power control interface. 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 14 #include <linux/module.h> 15 #include <linux/platform_device.h> 16 #include <linux/fb.h> 17 #include <linux/backlight.h> 18 #include <linux/lcd.h> 19 #include <linux/slab.h> 20 21 #include <video/platform_lcd.h> 22 23 struct platform_lcd { 24 struct device *us; 25 struct lcd_device *lcd; 26 struct plat_lcd_data *pdata; 27 28 unsigned int power; 29 unsigned int suspended : 1; 30 }; 31 32 static inline struct platform_lcd *to_our_lcd(struct lcd_device *lcd) 33 { 34 return lcd_get_data(lcd); 35 } 36 37 static int platform_lcd_get_power(struct lcd_device *lcd) 38 { 39 struct platform_lcd *plcd = to_our_lcd(lcd); 40 41 return plcd->power; 42 } 43 44 static int platform_lcd_set_power(struct lcd_device *lcd, int power) 45 { 46 struct platform_lcd *plcd = to_our_lcd(lcd); 47 int lcd_power = 1; 48 49 if (power == FB_BLANK_POWERDOWN || plcd->suspended) 50 lcd_power = 0; 51 52 plcd->pdata->set_power(plcd->pdata, lcd_power); 53 plcd->power = power; 54 55 return 0; 56 } 57 58 static int platform_lcd_match(struct lcd_device *lcd, struct fb_info *info) 59 { 60 struct platform_lcd *plcd = to_our_lcd(lcd); 61 struct plat_lcd_data *pdata = plcd->pdata; 62 63 if (pdata->match_fb) 64 return pdata->match_fb(pdata, info); 65 66 return plcd->us->parent == info->device; 67 } 68 69 static struct lcd_ops platform_lcd_ops = { 70 .get_power = platform_lcd_get_power, 71 .set_power = platform_lcd_set_power, 72 .check_fb = platform_lcd_match, 73 }; 74 75 static int __devinit platform_lcd_probe(struct platform_device *pdev) 76 { 77 struct plat_lcd_data *pdata; 78 struct platform_lcd *plcd; 79 struct device *dev = &pdev->dev; 80 int err; 81 82 pdata = pdev->dev.platform_data; 83 if (!pdata) { 84 dev_err(dev, "no platform data supplied\n"); 85 return -EINVAL; 86 } 87 88 plcd = kzalloc(sizeof(struct platform_lcd), GFP_KERNEL); 89 if (!plcd) { 90 dev_err(dev, "no memory for state\n"); 91 return -ENOMEM; 92 } 93 94 plcd->us = dev; 95 plcd->pdata = pdata; 96 plcd->lcd = lcd_device_register(dev_name(dev), dev, 97 plcd, &platform_lcd_ops); 98 if (IS_ERR(plcd->lcd)) { 99 dev_err(dev, "cannot register lcd device\n"); 100 err = PTR_ERR(plcd->lcd); 101 goto err_mem; 102 } 103 104 platform_set_drvdata(pdev, plcd); 105 platform_lcd_set_power(plcd->lcd, FB_BLANK_NORMAL); 106 107 return 0; 108 109 err_mem: 110 kfree(plcd); 111 return err; 112 } 113 114 static int __devexit platform_lcd_remove(struct platform_device *pdev) 115 { 116 struct platform_lcd *plcd = platform_get_drvdata(pdev); 117 118 lcd_device_unregister(plcd->lcd); 119 kfree(plcd); 120 121 return 0; 122 } 123 124 #ifdef CONFIG_PM 125 static int platform_lcd_suspend(struct platform_device *pdev, pm_message_t st) 126 { 127 struct platform_lcd *plcd = platform_get_drvdata(pdev); 128 129 plcd->suspended = 1; 130 platform_lcd_set_power(plcd->lcd, plcd->power); 131 132 return 0; 133 } 134 135 static int platform_lcd_resume(struct platform_device *pdev) 136 { 137 struct platform_lcd *plcd = platform_get_drvdata(pdev); 138 139 plcd->suspended = 0; 140 platform_lcd_set_power(plcd->lcd, plcd->power); 141 142 return 0; 143 } 144 #else 145 #define platform_lcd_suspend NULL 146 #define platform_lcd_resume NULL 147 #endif 148 149 static struct platform_driver platform_lcd_driver = { 150 .driver = { 151 .name = "platform-lcd", 152 .owner = THIS_MODULE, 153 }, 154 .probe = platform_lcd_probe, 155 .remove = __devexit_p(platform_lcd_remove), 156 .suspend = platform_lcd_suspend, 157 .resume = platform_lcd_resume, 158 }; 159 160 static int __init platform_lcd_init(void) 161 { 162 return platform_driver_register(&platform_lcd_driver); 163 } 164 165 static void __exit platform_lcd_cleanup(void) 166 { 167 platform_driver_unregister(&platform_lcd_driver); 168 } 169 170 module_init(platform_lcd_init); 171 module_exit(platform_lcd_cleanup); 172 173 MODULE_AUTHOR("Ben Dooks <ben-linux@fluff.org>"); 174 MODULE_LICENSE("GPL v2"); 175 MODULE_ALIAS("platform:platform-lcd"); 176