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