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