Merge tag 'trace-printf-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[drm/drm-misc.git] / drivers / video / backlight / platform_lcd.c
blobc9fe50f4d8ed20487231f9d9b490ee99f060a21c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /* drivers/video/backlight/platform_lcd.c
4 * Copyright 2008 Simtec Electronics
5 * Ben Dooks <ben@simtec.co.uk>
7 * Generic platform-device LCD power control interface.
8 */
10 #include <linux/module.h>
11 #include <linux/platform_device.h>
12 #include <linux/lcd.h>
13 #include <linux/slab.h>
15 #include <video/platform_lcd.h>
17 struct platform_lcd {
18 struct device *us;
19 struct lcd_device *lcd;
20 struct plat_lcd_data *pdata;
22 unsigned int power;
23 unsigned int suspended:1;
26 static inline struct platform_lcd *to_our_lcd(struct lcd_device *lcd)
28 return lcd_get_data(lcd);
31 static int platform_lcd_get_power(struct lcd_device *lcd)
33 struct platform_lcd *plcd = to_our_lcd(lcd);
35 return plcd->power;
38 static int platform_lcd_set_power(struct lcd_device *lcd, int power)
40 struct platform_lcd *plcd = to_our_lcd(lcd);
41 int lcd_power = 1;
43 if (power == LCD_POWER_OFF || plcd->suspended)
44 lcd_power = 0;
46 plcd->pdata->set_power(plcd->pdata, lcd_power);
47 plcd->power = power;
49 return 0;
52 static bool platform_lcd_controls_device(struct lcd_device *lcd, struct device *display_device)
54 struct platform_lcd *plcd = to_our_lcd(lcd);
56 return plcd->us->parent == display_device;
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,
65 static int platform_lcd_probe(struct platform_device *pdev)
67 struct plat_lcd_data *pdata;
68 struct platform_lcd *plcd;
69 struct device *dev = &pdev->dev;
70 int err;
72 pdata = dev_get_platdata(&pdev->dev);
73 if (!pdata) {
74 dev_err(dev, "no platform data supplied\n");
75 return -EINVAL;
78 if (pdata->probe) {
79 err = pdata->probe(pdata);
80 if (err)
81 return err;
84 plcd = devm_kzalloc(&pdev->dev, sizeof(struct platform_lcd),
85 GFP_KERNEL);
86 if (!plcd)
87 return -ENOMEM;
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);
98 platform_set_drvdata(pdev, plcd);
99 platform_lcd_set_power(plcd->lcd, LCD_POWER_REDUCED);
101 return 0;
104 #ifdef CONFIG_PM_SLEEP
105 static int platform_lcd_suspend(struct device *dev)
107 struct platform_lcd *plcd = dev_get_drvdata(dev);
109 plcd->suspended = 1;
110 platform_lcd_set_power(plcd->lcd, plcd->power);
112 return 0;
115 static int platform_lcd_resume(struct device *dev)
117 struct platform_lcd *plcd = dev_get_drvdata(dev);
119 plcd->suspended = 0;
120 platform_lcd_set_power(plcd->lcd, plcd->power);
122 return 0;
124 #endif
126 static SIMPLE_DEV_PM_OPS(platform_lcd_pm_ops, platform_lcd_suspend,
127 platform_lcd_resume);
129 static struct platform_driver platform_lcd_driver = {
130 .driver = {
131 .name = "platform-lcd",
132 .pm = &platform_lcd_pm_ops,
134 .probe = platform_lcd_probe,
137 module_platform_driver(platform_lcd_driver);
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");