blob: 0067931821c677b0bcb542d3aa3004633f2479ac [file] [log] [blame]
H Hartley Sweeten08b39242010-05-05 12:13:23 -05001/*
2 * Driver for the Cirrus EP93xx lcd backlight
3 *
4 * Copyright (c) 2010 H Hartley Sweeten <hsweeten@visionengravers.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * This driver controls the pulse width modulated brightness control output,
11 * BRIGHT, on the Cirrus EP9307, EP9312, and EP9315 processors.
12 */
13
Axel Lin15b1a8f2011-08-25 15:59:14 -070014#include <linux/module.h>
H Hartley Sweeten08b39242010-05-05 12:13:23 -050015#include <linux/platform_device.h>
16#include <linux/io.h>
17#include <linux/fb.h>
18#include <linux/backlight.h>
19
H Hartley Sweeten08b39242010-05-05 12:13:23 -050020#define EP93XX_MAX_COUNT 255
21#define EP93XX_MAX_BRIGHT 255
22#define EP93XX_DEF_BRIGHT 128
23
24struct ep93xxbl {
25 void __iomem *mmio;
26 int brightness;
27};
28
29static int ep93xxbl_set(struct backlight_device *bl, int brightness)
30{
31 struct ep93xxbl *ep93xxbl = bl_get_data(bl);
32
Ryan Mallon0fd19582012-01-22 20:31:32 +110033 writel((brightness << 8) | EP93XX_MAX_COUNT, ep93xxbl->mmio);
H Hartley Sweeten08b39242010-05-05 12:13:23 -050034
35 ep93xxbl->brightness = brightness;
36
37 return 0;
38}
39
40static int ep93xxbl_update_status(struct backlight_device *bl)
41{
42 int brightness = bl->props.brightness;
43
44 if (bl->props.power != FB_BLANK_UNBLANK ||
45 bl->props.fb_blank != FB_BLANK_UNBLANK)
46 brightness = 0;
47
48 return ep93xxbl_set(bl, brightness);
49}
50
51static int ep93xxbl_get_brightness(struct backlight_device *bl)
52{
53 struct ep93xxbl *ep93xxbl = bl_get_data(bl);
54
55 return ep93xxbl->brightness;
56}
57
58static const struct backlight_ops ep93xxbl_ops = {
59 .update_status = ep93xxbl_update_status,
60 .get_brightness = ep93xxbl_get_brightness,
61};
62
Jingoo Hancb1fbb82013-04-29 16:17:27 -070063static int ep93xxbl_probe(struct platform_device *dev)
H Hartley Sweeten08b39242010-05-05 12:13:23 -050064{
65 struct ep93xxbl *ep93xxbl;
66 struct backlight_device *bl;
67 struct backlight_properties props;
Ryan Mallon0fd19582012-01-22 20:31:32 +110068 struct resource *res;
H Hartley Sweeten08b39242010-05-05 12:13:23 -050069
70 ep93xxbl = devm_kzalloc(&dev->dev, sizeof(*ep93xxbl), GFP_KERNEL);
71 if (!ep93xxbl)
72 return -ENOMEM;
73
Ryan Mallon0fd19582012-01-22 20:31:32 +110074 res = platform_get_resource(dev, IORESOURCE_MEM, 0);
75 if (!res)
76 return -ENXIO;
77
H Hartley Sweeten08b39242010-05-05 12:13:23 -050078 /*
Ryan Mallon0fd19582012-01-22 20:31:32 +110079 * FIXME - We don't do a request_mem_region here because we are
80 * sharing the register space with the framebuffer driver (see
81 * drivers/video/ep93xx-fb.c) and doing so will cause the second
82 * loaded driver to return -EBUSY.
H Hartley Sweeten08b39242010-05-05 12:13:23 -050083 *
84 * NOTE: No locking is required; the framebuffer does not touch
85 * this register.
86 */
Ryan Mallon0fd19582012-01-22 20:31:32 +110087 ep93xxbl->mmio = devm_ioremap(&dev->dev, res->start,
88 resource_size(res));
89 if (!ep93xxbl->mmio)
90 return -ENXIO;
H Hartley Sweeten08b39242010-05-05 12:13:23 -050091
92 memset(&props, 0, sizeof(struct backlight_properties));
Matthew Garrettbb7ca742011-03-22 16:30:21 -070093 props.type = BACKLIGHT_RAW;
H Hartley Sweeten08b39242010-05-05 12:13:23 -050094 props.max_brightness = EP93XX_MAX_BRIGHT;
Jingoo Han23741ad2013-11-12 15:09:14 -080095 bl = devm_backlight_device_register(&dev->dev, dev->name, &dev->dev,
96 ep93xxbl, &ep93xxbl_ops, &props);
H Hartley Sweeten08b39242010-05-05 12:13:23 -050097 if (IS_ERR(bl))
98 return PTR_ERR(bl);
99
100 bl->props.brightness = EP93XX_DEF_BRIGHT;
101
102 platform_set_drvdata(dev, bl);
103
104 ep93xxbl_update_status(bl);
105
106 return 0;
107}
108
Jingoo Hand7696aa2013-04-29 16:17:42 -0700109#ifdef CONFIG_PM_SLEEP
110static int ep93xxbl_suspend(struct device *dev)
H Hartley Sweeten08b39242010-05-05 12:13:23 -0500111{
Jingoo Hand7696aa2013-04-29 16:17:42 -0700112 struct backlight_device *bl = dev_get_drvdata(dev);
H Hartley Sweeten08b39242010-05-05 12:13:23 -0500113
114 return ep93xxbl_set(bl, 0);
115}
116
Jingoo Hand7696aa2013-04-29 16:17:42 -0700117static int ep93xxbl_resume(struct device *dev)
H Hartley Sweeten08b39242010-05-05 12:13:23 -0500118{
Jingoo Hand7696aa2013-04-29 16:17:42 -0700119 struct backlight_device *bl = dev_get_drvdata(dev);
H Hartley Sweeten08b39242010-05-05 12:13:23 -0500120
121 backlight_update_status(bl);
122 return 0;
123}
H Hartley Sweeten08b39242010-05-05 12:13:23 -0500124#endif
125
Jingoo Hand7696aa2013-04-29 16:17:42 -0700126static SIMPLE_DEV_PM_OPS(ep93xxbl_pm_ops, ep93xxbl_suspend, ep93xxbl_resume);
127
H Hartley Sweeten08b39242010-05-05 12:13:23 -0500128static struct platform_driver ep93xxbl_driver = {
129 .driver = {
130 .name = "ep93xx-bl",
Jingoo Hand7696aa2013-04-29 16:17:42 -0700131 .pm = &ep93xxbl_pm_ops,
H Hartley Sweeten08b39242010-05-05 12:13:23 -0500132 },
133 .probe = ep93xxbl_probe,
H Hartley Sweeten08b39242010-05-05 12:13:23 -0500134};
135
Axel Lin81178e02012-01-10 15:09:11 -0800136module_platform_driver(ep93xxbl_driver);
H Hartley Sweeten08b39242010-05-05 12:13:23 -0500137
138MODULE_DESCRIPTION("EP93xx Backlight Driver");
139MODULE_AUTHOR("H Hartley Sweeten <hsweeten@visionengravers.com>");
140MODULE_LICENSE("GPL");
141MODULE_ALIAS("platform:ep93xx-bl");