1
2
3
4
5
6
7
8
9
10
11
12
13#include <linux/module.h>
14#include <linux/kernel.h>
15#include <linux/platform_device.h>
16#include <linux/delay.h>
17#include <linux/fb.h>
18#include <linux/backlight.h>
19#include <linux/i2c/twl.h>
20#include <linux/err.h>
21
22#define TWL_PWM0_ON 0x00
23#define TWL_PWM0_OFF 0x01
24
25#define TWL_INTBR_GPBR1 0x0c
26#define TWL_INTBR_PMBR1 0x0d
27
28#define TWL_PMBR1_PWM0_MUXMASK 0x0c
29#define TWL_PMBR1_PWM0 0x04
30#define PWM0_CLK_ENABLE BIT(0)
31#define PWM0_ENABLE BIT(2)
32
33
34#define MIN_VALUE 9
35#define MAX_VALUE 63
36#define MAX_USER_VALUE (MAX_VALUE - MIN_VALUE)
37
38#define PANDORABL_WAS_OFF BL_CORE_DRIVER1
39
40static int pandora_backlight_update_status(struct backlight_device *bl)
41{
42 int brightness = bl->props.brightness;
43 u8 r;
44
45 if (bl->props.power != FB_BLANK_UNBLANK)
46 brightness = 0;
47 if (bl->props.state & BL_CORE_FBBLANK)
48 brightness = 0;
49 if (bl->props.state & BL_CORE_SUSPENDED)
50 brightness = 0;
51
52 if ((unsigned int)brightness > MAX_USER_VALUE)
53 brightness = MAX_USER_VALUE;
54
55 if (brightness == 0) {
56 if (bl->props.state & PANDORABL_WAS_OFF)
57 goto done;
58
59
60 twl_i2c_read_u8(TWL4030_MODULE_INTBR, &r, TWL_INTBR_GPBR1);
61 r &= ~PWM0_ENABLE;
62 twl_i2c_write_u8(TWL4030_MODULE_INTBR, r, TWL_INTBR_GPBR1);
63 r &= ~PWM0_CLK_ENABLE;
64 twl_i2c_write_u8(TWL4030_MODULE_INTBR, r, TWL_INTBR_GPBR1);
65
66 goto done;
67 }
68
69 if (bl->props.state & PANDORABL_WAS_OFF) {
70
71
72
73
74 twl_i2c_write_u8(TWL_MODULE_PWM, MAX_VALUE, TWL_PWM0_OFF);
75
76
77 twl_i2c_read_u8(TWL4030_MODULE_INTBR, &r, TWL_INTBR_GPBR1);
78 r &= ~PWM0_ENABLE;
79 r |= PWM0_CLK_ENABLE;
80 twl_i2c_write_u8(TWL4030_MODULE_INTBR, r, TWL_INTBR_GPBR1);
81 r |= PWM0_ENABLE;
82 twl_i2c_write_u8(TWL4030_MODULE_INTBR, r, TWL_INTBR_GPBR1);
83
84
85
86
87
88
89 usleep_range(2000, 10000);
90 }
91
92 twl_i2c_write_u8(TWL_MODULE_PWM, MIN_VALUE + brightness, TWL_PWM0_OFF);
93
94done:
95 if (brightness != 0)
96 bl->props.state &= ~PANDORABL_WAS_OFF;
97 else
98 bl->props.state |= PANDORABL_WAS_OFF;
99
100 return 0;
101}
102
103static const struct backlight_ops pandora_backlight_ops = {
104 .options = BL_CORE_SUSPENDRESUME,
105 .update_status = pandora_backlight_update_status,
106};
107
108static int pandora_backlight_probe(struct platform_device *pdev)
109{
110 struct backlight_properties props;
111 struct backlight_device *bl;
112 u8 r;
113
114 memset(&props, 0, sizeof(props));
115 props.max_brightness = MAX_USER_VALUE;
116 props.type = BACKLIGHT_RAW;
117 bl = devm_backlight_device_register(&pdev->dev, pdev->name, &pdev->dev,
118 NULL, &pandora_backlight_ops, &props);
119 if (IS_ERR(bl)) {
120 dev_err(&pdev->dev, "failed to register backlight\n");
121 return PTR_ERR(bl);
122 }
123
124 platform_set_drvdata(pdev, bl);
125
126
127 twl_i2c_write_u8(TWL_MODULE_PWM, 0x80, TWL_PWM0_ON);
128
129 bl->props.state |= PANDORABL_WAS_OFF;
130 bl->props.brightness = MAX_USER_VALUE;
131 backlight_update_status(bl);
132
133
134 twl_i2c_read_u8(TWL4030_MODULE_INTBR, &r, TWL_INTBR_PMBR1);
135 r &= ~TWL_PMBR1_PWM0_MUXMASK;
136 r |= TWL_PMBR1_PWM0;
137 twl_i2c_write_u8(TWL4030_MODULE_INTBR, r, TWL_INTBR_PMBR1);
138
139 return 0;
140}
141
142static struct platform_driver pandora_backlight_driver = {
143 .driver = {
144 .name = "pandora-backlight",
145 },
146 .probe = pandora_backlight_probe,
147};
148
149module_platform_driver(pandora_backlight_driver);
150
151MODULE_AUTHOR("Gražvydas Ignotas <notasas@gmail.com>");
152MODULE_DESCRIPTION("Pandora Backlight Driver");
153MODULE_LICENSE("GPL");
154MODULE_ALIAS("platform:pandora-backlight");
155