perf record: Fix segfault with --no-mmap-pages
[linux/fpc-iii.git] / drivers / video / backlight / pandora_bl.c
blob633b0a22fd64446db61d63755069963e75f1aff0
1 /*
2 * Backlight driver for Pandora handheld.
3 * Pandora uses TWL4030 PWM0 -> TPS61161 combo for control backlight.
4 * Based on pwm_bl.c
6 * Copyright 2009,2012 Gražvydas Ignotas <notasas@gmail.com>
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.
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>
22 #define TWL_PWM0_ON 0x00
23 #define TWL_PWM0_OFF 0x01
25 #define TWL_INTBR_GPBR1 0x0c
26 #define TWL_INTBR_PMBR1 0x0d
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)
33 /* range accepted by hardware */
34 #define MIN_VALUE 9
35 #define MAX_VALUE 63
36 #define MAX_USER_VALUE (MAX_VALUE - MIN_VALUE)
38 #define PANDORABL_WAS_OFF BL_CORE_DRIVER1
40 static int pandora_backlight_update_status(struct backlight_device *bl)
42 int brightness = bl->props.brightness;
43 u8 r;
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;
52 if ((unsigned int)brightness > MAX_USER_VALUE)
53 brightness = MAX_USER_VALUE;
55 if (brightness == 0) {
56 if (bl->props.state & PANDORABL_WAS_OFF)
57 goto done;
59 /* first disable PWM0 output, then clock */
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);
66 goto done;
69 if (bl->props.state & PANDORABL_WAS_OFF) {
71 * set PWM duty cycle to max. TPS61161 seems to use this
72 * to calibrate it's PWM sensitivity when it starts.
74 twl_i2c_write_u8(TWL_MODULE_PWM, MAX_VALUE, TWL_PWM0_OFF);
76 /* first enable clock, then PWM0 out */
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);
85 * TI made it very easy to enable digital control, so easy that
86 * it often triggers unintentionally and disabes PWM control,
87 * so wait until 1 wire mode detection window ends.
89 usleep_range(2000, 10000);
92 twl_i2c_write_u8(TWL_MODULE_PWM, MIN_VALUE + brightness, TWL_PWM0_OFF);
94 done:
95 if (brightness != 0)
96 bl->props.state &= ~PANDORABL_WAS_OFF;
97 else
98 bl->props.state |= PANDORABL_WAS_OFF;
100 return 0;
103 static int pandora_backlight_get_brightness(struct backlight_device *bl)
105 return bl->props.brightness;
108 static const struct backlight_ops pandora_backlight_ops = {
109 .options = BL_CORE_SUSPENDRESUME,
110 .update_status = pandora_backlight_update_status,
111 .get_brightness = pandora_backlight_get_brightness,
114 static int pandora_backlight_probe(struct platform_device *pdev)
116 struct backlight_properties props;
117 struct backlight_device *bl;
118 u8 r;
120 memset(&props, 0, sizeof(props));
121 props.max_brightness = MAX_USER_VALUE;
122 props.type = BACKLIGHT_RAW;
123 bl = backlight_device_register(pdev->name, &pdev->dev,
124 NULL, &pandora_backlight_ops, &props);
125 if (IS_ERR(bl)) {
126 dev_err(&pdev->dev, "failed to register backlight\n");
127 return PTR_ERR(bl);
130 platform_set_drvdata(pdev, bl);
132 /* 64 cycle period, ON position 0 */
133 twl_i2c_write_u8(TWL_MODULE_PWM, 0x80, TWL_PWM0_ON);
135 bl->props.state |= PANDORABL_WAS_OFF;
136 bl->props.brightness = MAX_USER_VALUE;
137 backlight_update_status(bl);
139 /* enable PWM function in pin mux */
140 twl_i2c_read_u8(TWL4030_MODULE_INTBR, &r, TWL_INTBR_PMBR1);
141 r &= ~TWL_PMBR1_PWM0_MUXMASK;
142 r |= TWL_PMBR1_PWM0;
143 twl_i2c_write_u8(TWL4030_MODULE_INTBR, r, TWL_INTBR_PMBR1);
145 return 0;
148 static int pandora_backlight_remove(struct platform_device *pdev)
150 struct backlight_device *bl = platform_get_drvdata(pdev);
151 backlight_device_unregister(bl);
152 return 0;
155 static struct platform_driver pandora_backlight_driver = {
156 .driver = {
157 .name = "pandora-backlight",
158 .owner = THIS_MODULE,
160 .probe = pandora_backlight_probe,
161 .remove = pandora_backlight_remove,
164 module_platform_driver(pandora_backlight_driver);
166 MODULE_AUTHOR("Gražvydas Ignotas <notasas@gmail.com>");
167 MODULE_DESCRIPTION("Pandora Backlight Driver");
168 MODULE_LICENSE("GPL");
169 MODULE_ALIAS("platform:pandora-backlight");