leds-twl4030-pwm, pandora_bl: fix a race between the two
[pandora-kernel.git] / drivers / video / backlight / pandora_bl.c
1 /*
2  * Backlight driver for Pandora handheld.
3  * Pandora uses TWL4030 PWM0 -> TPS61161 combo for control backlight.
4  * Based on pwm_bl.c
5  *
6  * Copyright 2009,2012 Gražvydas Ignotas <notasas@gmail.com>
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 #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 /* range accepted by hardware */
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
40 static int pandora_backlight_update_status(struct backlight_device *bl)
41 {
42         int brightness = bl->props.brightness;
43
44         if (bl->props.power != FB_BLANK_UNBLANK)
45                 brightness = 0;
46         if (bl->props.state & BL_CORE_FBBLANK)
47                 brightness = 0;
48         if (bl->props.state & BL_CORE_SUSPENDED)
49                 brightness = 0;
50
51         if ((unsigned int)brightness > MAX_USER_VALUE)
52                 brightness = MAX_USER_VALUE;
53
54         if (brightness == 0) {
55                 if (bl->props.state & PANDORABL_WAS_OFF)
56                         goto done;
57
58                 /* first disable PWM0 output, then clock */
59                 twl_i2c_rmw_u8(TWL4030_MODULE_INTBR, PWM0_ENABLE, 0,
60                         TWL_INTBR_GPBR1);
61                 twl_i2c_rmw_u8(TWL4030_MODULE_INTBR, PWM0_CLK_ENABLE, 0,
62                         TWL_INTBR_GPBR1);
63                 goto done;
64         }
65
66         if (bl->props.state & PANDORABL_WAS_OFF) {
67                 /*
68                  * set PWM duty cycle to max. TPS61161 seems to use this
69                  * to calibrate it's PWM sensitivity when it starts.
70                  */
71                 twl_i2c_write_u8(TWL4030_MODULE_PWM0, MAX_VALUE,
72                                         TWL_PWM0_OFF);
73
74                 /* first enable clock, then PWM0 out */
75                 twl_i2c_rmw_u8(TWL4030_MODULE_INTBR,
76                         PWM0_ENABLE, PWM0_CLK_ENABLE, TWL_INTBR_GPBR1);
77                 twl_i2c_rmw_u8(TWL4030_MODULE_INTBR,
78                         0, PWM0_ENABLE, TWL_INTBR_GPBR1);
79
80                 /*
81                  * TI made it very easy to enable digital control, so easy that
82                  * it often triggers unintentionally and disabes PWM control,
83                  * so wait until 1 wire mode detection window ends.
84                  */
85                 usleep_range(2000, 10000);
86         }
87
88         twl_i2c_write_u8(TWL4030_MODULE_PWM0, MIN_VALUE + brightness,
89                                 TWL_PWM0_OFF);
90
91 done:
92         if (brightness != 0)
93                 bl->props.state &= ~PANDORABL_WAS_OFF;
94         else
95                 bl->props.state |= PANDORABL_WAS_OFF;
96
97         return 0;
98 }
99
100 static int pandora_backlight_get_brightness(struct backlight_device *bl)
101 {
102         return bl->props.brightness;
103 }
104
105 static const struct backlight_ops pandora_backlight_ops = {
106         .options        = BL_CORE_SUSPENDRESUME,
107         .update_status  = pandora_backlight_update_status,
108         .get_brightness = pandora_backlight_get_brightness,
109 };
110
111 static int pandora_backlight_probe(struct platform_device *pdev)
112 {
113         struct backlight_properties props;
114         struct backlight_device *bl;
115
116         memset(&props, 0, sizeof(props));
117         props.max_brightness = MAX_USER_VALUE;
118         props.type = BACKLIGHT_RAW;
119         bl = backlight_device_register(pdev->name, &pdev->dev,
120                         NULL, &pandora_backlight_ops, &props);
121         if (IS_ERR(bl)) {
122                 dev_err(&pdev->dev, "failed to register backlight\n");
123                 return PTR_ERR(bl);
124         }
125
126         platform_set_drvdata(pdev, bl);
127
128         /* 64 cycle period, ON position 0 */
129         twl_i2c_write_u8(TWL4030_MODULE_PWM0, 0x80, TWL_PWM0_ON);
130
131         bl->props.state |= PANDORABL_WAS_OFF;
132         bl->props.brightness = MAX_USER_VALUE;
133         backlight_update_status(bl);
134
135         /* enable PWM function in pin mux */
136         twl_i2c_rmw_u8(TWL4030_MODULE_INTBR,
137                 TWL_PMBR1_PWM0_MUXMASK, TWL_PMBR1_PWM0, TWL_INTBR_PMBR1);
138
139         return 0;
140 }
141
142 static int pandora_backlight_remove(struct platform_device *pdev)
143 {
144         struct backlight_device *bl = platform_get_drvdata(pdev);
145         backlight_device_unregister(bl);
146         return 0;
147 }
148
149 static struct platform_driver pandora_backlight_driver = {
150         .driver         = {
151                 .name   = "pandora-backlight",
152                 .owner  = THIS_MODULE,
153         },
154         .probe          = pandora_backlight_probe,
155         .remove         = pandora_backlight_remove,
156 };
157
158 module_platform_driver(pandora_backlight_driver);
159
160 MODULE_AUTHOR("Gražvydas Ignotas <notasas@gmail.com>");
161 MODULE_DESCRIPTION("Pandora Backlight Driver");
162 MODULE_LICENSE("GPL");
163 MODULE_ALIAS("platform:pandora-backlight");