xref: /linux/drivers/video/backlight/platform_lcd.c (revision a1ff5a7d78a036d6c2178ee5acd6ba4946243800)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /* drivers/video/backlight/platform_lcd.c
3  *
4  * Copyright 2008 Simtec Electronics
5  *	Ben Dooks <ben@simtec.co.uk>
6  *
7  * Generic platform-device LCD power control interface.
8 */
9 
10 #include <linux/module.h>
11 #include <linux/platform_device.h>
12 #include <linux/fb.h>
13 #include <linux/backlight.h>
14 #include <linux/lcd.h>
15 #include <linux/slab.h>
16 
17 #include <video/platform_lcd.h>
18 
19 struct platform_lcd {
20 	struct device		*us;
21 	struct lcd_device	*lcd;
22 	struct plat_lcd_data	*pdata;
23 
24 	unsigned int		 power;
25 	unsigned int		 suspended:1;
26 };
27 
to_our_lcd(struct lcd_device * lcd)28 static inline struct platform_lcd *to_our_lcd(struct lcd_device *lcd)
29 {
30 	return lcd_get_data(lcd);
31 }
32 
platform_lcd_get_power(struct lcd_device * lcd)33 static int platform_lcd_get_power(struct lcd_device *lcd)
34 {
35 	struct platform_lcd *plcd = to_our_lcd(lcd);
36 
37 	return plcd->power;
38 }
39 
platform_lcd_set_power(struct lcd_device * lcd,int power)40 static int platform_lcd_set_power(struct lcd_device *lcd, int power)
41 {
42 	struct platform_lcd *plcd = to_our_lcd(lcd);
43 	int lcd_power = 1;
44 
45 	if (power == FB_BLANK_POWERDOWN || plcd->suspended)
46 		lcd_power = 0;
47 
48 	plcd->pdata->set_power(plcd->pdata, lcd_power);
49 	plcd->power = power;
50 
51 	return 0;
52 }
53 
platform_lcd_match(struct lcd_device * lcd,struct fb_info * info)54 static int platform_lcd_match(struct lcd_device *lcd, struct fb_info *info)
55 {
56 	struct platform_lcd *plcd = to_our_lcd(lcd);
57 	struct plat_lcd_data *pdata = plcd->pdata;
58 
59 	if (pdata->match_fb)
60 		return pdata->match_fb(pdata, info);
61 
62 	return plcd->us->parent == info->device;
63 }
64 
65 static const struct lcd_ops platform_lcd_ops = {
66 	.get_power	= platform_lcd_get_power,
67 	.set_power	= platform_lcd_set_power,
68 	.check_fb	= platform_lcd_match,
69 };
70 
platform_lcd_probe(struct platform_device * pdev)71 static int platform_lcd_probe(struct platform_device *pdev)
72 {
73 	struct plat_lcd_data *pdata;
74 	struct platform_lcd *plcd;
75 	struct device *dev = &pdev->dev;
76 	int err;
77 
78 	pdata = dev_get_platdata(&pdev->dev);
79 	if (!pdata) {
80 		dev_err(dev, "no platform data supplied\n");
81 		return -EINVAL;
82 	}
83 
84 	if (pdata->probe) {
85 		err = pdata->probe(pdata);
86 		if (err)
87 			return err;
88 	}
89 
90 	plcd = devm_kzalloc(&pdev->dev, sizeof(struct platform_lcd),
91 			    GFP_KERNEL);
92 	if (!plcd)
93 		return -ENOMEM;
94 
95 	plcd->us = dev;
96 	plcd->pdata = pdata;
97 	plcd->lcd = devm_lcd_device_register(&pdev->dev, dev_name(dev), dev,
98 						plcd, &platform_lcd_ops);
99 	if (IS_ERR(plcd->lcd)) {
100 		dev_err(dev, "cannot register lcd device\n");
101 		return PTR_ERR(plcd->lcd);
102 	}
103 
104 	platform_set_drvdata(pdev, plcd);
105 	platform_lcd_set_power(plcd->lcd, FB_BLANK_NORMAL);
106 
107 	return 0;
108 }
109 
110 #ifdef CONFIG_PM_SLEEP
platform_lcd_suspend(struct device * dev)111 static int platform_lcd_suspend(struct device *dev)
112 {
113 	struct platform_lcd *plcd = dev_get_drvdata(dev);
114 
115 	plcd->suspended = 1;
116 	platform_lcd_set_power(plcd->lcd, plcd->power);
117 
118 	return 0;
119 }
120 
platform_lcd_resume(struct device * dev)121 static int platform_lcd_resume(struct device *dev)
122 {
123 	struct platform_lcd *plcd = dev_get_drvdata(dev);
124 
125 	plcd->suspended = 0;
126 	platform_lcd_set_power(plcd->lcd, plcd->power);
127 
128 	return 0;
129 }
130 #endif
131 
132 static SIMPLE_DEV_PM_OPS(platform_lcd_pm_ops, platform_lcd_suspend,
133 			platform_lcd_resume);
134 
135 static struct platform_driver platform_lcd_driver = {
136 	.driver		= {
137 		.name	= "platform-lcd",
138 		.pm	= &platform_lcd_pm_ops,
139 	},
140 	.probe		= platform_lcd_probe,
141 };
142 
143 module_platform_driver(platform_lcd_driver);
144 
145 MODULE_AUTHOR("Ben Dooks <ben-linux@fluff.org>");
146 MODULE_DESCRIPTION("Generic platform-device LCD power control interface");
147 MODULE_LICENSE("GPL v2");
148 MODULE_ALIAS("platform:platform-lcd");
149