blob: c3d2e209fc8f5b2ed148c30deeb5bffe970756a2 [file] [log] [blame]
Ben Dooksc25826a2008-07-23 21:31:38 -07001/* 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>
Jingoo Han52e84242012-10-04 17:13:01 -070019#include <linux/of.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/slab.h>
Ben Dooksc25826a2008-07-23 21:31:38 -070021
22#include <video/platform_lcd.h>
23
24struct platform_lcd {
25 struct device *us;
26 struct lcd_device *lcd;
27 struct plat_lcd_data *pdata;
28
29 unsigned int power;
Jingoo Han8690c702012-12-17 16:00:31 -080030 unsigned int suspended:1;
Ben Dooksc25826a2008-07-23 21:31:38 -070031};
32
33static inline struct platform_lcd *to_our_lcd(struct lcd_device *lcd)
34{
35 return lcd_get_data(lcd);
36}
37
38static int platform_lcd_get_power(struct lcd_device *lcd)
39{
40 struct platform_lcd *plcd = to_our_lcd(lcd);
41
42 return plcd->power;
43}
44
45static int platform_lcd_set_power(struct lcd_device *lcd, int power)
46{
47 struct platform_lcd *plcd = to_our_lcd(lcd);
48 int lcd_power = 1;
49
50 if (power == FB_BLANK_POWERDOWN || plcd->suspended)
51 lcd_power = 0;
52
53 plcd->pdata->set_power(plcd->pdata, lcd_power);
54 plcd->power = power;
55
56 return 0;
57}
58
59static int platform_lcd_match(struct lcd_device *lcd, struct fb_info *info)
60{
61 struct platform_lcd *plcd = to_our_lcd(lcd);
62 struct plat_lcd_data *pdata = plcd->pdata;
63
64 if (pdata->match_fb)
65 return pdata->match_fb(pdata, info);
66
67 return plcd->us->parent == info->device;
68}
69
70static struct lcd_ops platform_lcd_ops = {
71 .get_power = platform_lcd_get_power,
72 .set_power = platform_lcd_set_power,
73 .check_fb = platform_lcd_match,
74};
75
Bill Pemberton1b9e4502012-11-19 13:21:46 -050076static int platform_lcd_probe(struct platform_device *pdev)
Ben Dooksc25826a2008-07-23 21:31:38 -070077{
78 struct plat_lcd_data *pdata;
79 struct platform_lcd *plcd;
80 struct device *dev = &pdev->dev;
81 int err;
82
Jingoo Hanc512794c2013-11-12 15:09:04 -080083 pdata = dev_get_platdata(&pdev->dev);
Ben Dooksc25826a2008-07-23 21:31:38 -070084 if (!pdata) {
85 dev_err(dev, "no platform data supplied\n");
86 return -EINVAL;
87 }
88
Andrew Bresticker46e19152013-04-29 16:17:58 -070089 if (pdata->probe) {
90 err = pdata->probe(pdata);
91 if (err)
92 return err;
93 }
94
Mark Brown48e78e82012-01-10 15:09:21 -080095 plcd = devm_kzalloc(&pdev->dev, sizeof(struct platform_lcd),
96 GFP_KERNEL);
Jingoo Hanc6915be2014-04-03 14:49:05 -070097 if (!plcd)
Ben Dooksc25826a2008-07-23 21:31:38 -070098 return -ENOMEM;
Ben Dooksc25826a2008-07-23 21:31:38 -070099
100 plcd->us = dev;
101 plcd->pdata = pdata;
Jingoo Han1f9ca1e2013-11-12 15:09:31 -0800102 plcd->lcd = devm_lcd_device_register(&pdev->dev, dev_name(dev), dev,
103 plcd, &platform_lcd_ops);
Ben Dooksc25826a2008-07-23 21:31:38 -0700104 if (IS_ERR(plcd->lcd)) {
105 dev_err(dev, "cannot register lcd device\n");
Jingoo Han1f9ca1e2013-11-12 15:09:31 -0800106 return PTR_ERR(plcd->lcd);
Ben Dooksc25826a2008-07-23 21:31:38 -0700107 }
108
109 platform_set_drvdata(pdev, plcd);
Ben Dooks126ed362008-07-29 22:33:25 -0700110 platform_lcd_set_power(plcd->lcd, FB_BLANK_NORMAL);
111
Ben Dooksc25826a2008-07-23 21:31:38 -0700112 return 0;
Ben Dooksc25826a2008-07-23 21:31:38 -0700113}
114
Jingoo Han1d80d0f2013-04-29 16:17:26 -0700115#ifdef CONFIG_PM_SLEEP
Jingoo Han67a67272012-03-23 15:02:02 -0700116static int platform_lcd_suspend(struct device *dev)
Ben Dooksc25826a2008-07-23 21:31:38 -0700117{
Jingoo Han67a67272012-03-23 15:02:02 -0700118 struct platform_lcd *plcd = dev_get_drvdata(dev);
Ben Dooksc25826a2008-07-23 21:31:38 -0700119
120 plcd->suspended = 1;
121 platform_lcd_set_power(plcd->lcd, plcd->power);
122
123 return 0;
124}
125
Jingoo Han67a67272012-03-23 15:02:02 -0700126static int platform_lcd_resume(struct device *dev)
Ben Dooksc25826a2008-07-23 21:31:38 -0700127{
Jingoo Han67a67272012-03-23 15:02:02 -0700128 struct platform_lcd *plcd = dev_get_drvdata(dev);
Ben Dooksc25826a2008-07-23 21:31:38 -0700129
130 plcd->suspended = 0;
131 platform_lcd_set_power(plcd->lcd, plcd->power);
132
133 return 0;
134}
Jingoo Han1d80d0f2013-04-29 16:17:26 -0700135#endif
Jingoo Han67a67272012-03-23 15:02:02 -0700136
137static SIMPLE_DEV_PM_OPS(platform_lcd_pm_ops, platform_lcd_suspend,
138 platform_lcd_resume);
Ben Dooksc25826a2008-07-23 21:31:38 -0700139
Jingoo Han52e84242012-10-04 17:13:01 -0700140#ifdef CONFIG_OF
141static const struct of_device_id platform_lcd_of_match[] = {
142 { .compatible = "platform-lcd" },
143 {},
144};
145MODULE_DEVICE_TABLE(of, platform_lcd_of_match);
146#endif
147
Ben Dooksc25826a2008-07-23 21:31:38 -0700148static struct platform_driver platform_lcd_driver = {
149 .driver = {
150 .name = "platform-lcd",
151 .owner = THIS_MODULE,
Jingoo Han67a67272012-03-23 15:02:02 -0700152 .pm = &platform_lcd_pm_ops,
Jingoo Han52e84242012-10-04 17:13:01 -0700153 .of_match_table = of_match_ptr(platform_lcd_of_match),
Ben Dooksc25826a2008-07-23 21:31:38 -0700154 },
155 .probe = platform_lcd_probe,
Ben Dooksc25826a2008-07-23 21:31:38 -0700156};
157
Axel Lin81178e02012-01-10 15:09:11 -0800158module_platform_driver(platform_lcd_driver);
Ben Dooksc25826a2008-07-23 21:31:38 -0700159
160MODULE_AUTHOR("Ben Dooks <ben-linux@fluff.org>");
161MODULE_LICENSE("GPL v2");
162MODULE_ALIAS("platform:platform-lcd");