Linux 2.6.34-rc3
[pohmelfs.git] / drivers / video / backlight / max8925_bl.c
blobc91adaf492cf9eb52fb596516d128f43beb1d0a5
1 /*
2 * Backlight driver for Maxim MAX8925
4 * Copyright (C) 2009 Marvell International Ltd.
5 * Haojian Zhuang <haojian.zhuang@marvell.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
12 #include <linux/init.h>
13 #include <linux/kernel.h>
14 #include <linux/platform_device.h>
15 #include <linux/fb.h>
16 #include <linux/i2c.h>
17 #include <linux/backlight.h>
18 #include <linux/mfd/max8925.h>
20 #define MAX_BRIGHTNESS (0xff)
21 #define MIN_BRIGHTNESS (0)
23 #define LWX_FREQ(x) (((x - 601) / 100) & 0x7)
25 struct max8925_backlight_data {
26 struct max8925_chip *chip;
28 int current_brightness;
31 static int max8925_backlight_set(struct backlight_device *bl, int brightness)
33 struct max8925_backlight_data *data = bl_get_data(bl);
34 struct max8925_chip *chip = data->chip;
35 unsigned char value;
36 int ret;
38 if (brightness > MAX_BRIGHTNESS)
39 value = MAX_BRIGHTNESS;
40 else
41 value = brightness;
43 ret = max8925_reg_write(chip->i2c, MAX8925_WLED_CNTL, value);
44 if (ret < 0)
45 goto out;
47 if (!data->current_brightness && brightness)
48 /* enable WLED output */
49 ret = max8925_set_bits(chip->i2c, MAX8925_WLED_MODE_CNTL, 1, 1);
50 else if (!brightness)
51 /* disable WLED output */
52 ret = max8925_set_bits(chip->i2c, MAX8925_WLED_MODE_CNTL, 1, 0);
53 if (ret < 0)
54 goto out;
55 dev_dbg(chip->dev, "set brightness %d\n", value);
56 data->current_brightness = value;
57 return 0;
58 out:
59 dev_dbg(chip->dev, "set brightness %d failure with return value:%d\n",
60 value, ret);
61 return ret;
64 static int max8925_backlight_update_status(struct backlight_device *bl)
66 int brightness = bl->props.brightness;
68 if (bl->props.power != FB_BLANK_UNBLANK)
69 brightness = 0;
71 if (bl->props.fb_blank != FB_BLANK_UNBLANK)
72 brightness = 0;
74 if (bl->props.state & BL_CORE_SUSPENDED)
75 brightness = 0;
77 return max8925_backlight_set(bl, brightness);
80 static int max8925_backlight_get_brightness(struct backlight_device *bl)
82 struct max8925_backlight_data *data = bl_get_data(bl);
83 struct max8925_chip *chip = data->chip;
84 int ret;
86 ret = max8925_reg_read(chip->i2c, MAX8925_WLED_CNTL);
87 if (ret < 0)
88 return -EINVAL;
89 data->current_brightness = ret;
90 dev_dbg(chip->dev, "get brightness %d\n", data->current_brightness);
91 return ret;
94 static struct backlight_ops max8925_backlight_ops = {
95 .options = BL_CORE_SUSPENDRESUME,
96 .update_status = max8925_backlight_update_status,
97 .get_brightness = max8925_backlight_get_brightness,
100 static int __devinit max8925_backlight_probe(struct platform_device *pdev)
102 struct max8925_chip *chip = dev_get_drvdata(pdev->dev.parent);
103 struct max8925_platform_data *max8925_pdata;
104 struct max8925_backlight_pdata *pdata = NULL;
105 struct max8925_backlight_data *data;
106 struct backlight_device *bl;
107 struct backlight_properties props;
108 struct resource *res;
109 char name[MAX8925_NAME_SIZE];
110 unsigned char value;
111 int ret;
113 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
114 if (res == NULL) {
115 dev_err(&pdev->dev, "No I/O resource!\n");
116 return -EINVAL;
119 if (pdev->dev.parent->platform_data) {
120 max8925_pdata = pdev->dev.parent->platform_data;
121 pdata = max8925_pdata->backlight;
124 if (!pdata) {
125 dev_err(&pdev->dev, "platform data isn't assigned to "
126 "backlight\n");
127 return -EINVAL;
130 data = kzalloc(sizeof(struct max8925_backlight_data), GFP_KERNEL);
131 if (data == NULL)
132 return -ENOMEM;
133 strncpy(name, res->name, MAX8925_NAME_SIZE);
134 data->chip = chip;
135 data->current_brightness = 0;
137 memset(&props, 0, sizeof(struct backlight_properties));
138 props.max_brightness = MAX_BRIGHTNESS;
139 bl = backlight_device_register(name, &pdev->dev, data,
140 &max8925_backlight_ops, &props);
141 if (IS_ERR(bl)) {
142 dev_err(&pdev->dev, "failed to register backlight\n");
143 kfree(data);
144 return PTR_ERR(bl);
146 bl->props.brightness = MAX_BRIGHTNESS;
148 platform_set_drvdata(pdev, bl);
150 value = 0;
151 if (pdata->lxw_scl)
152 value |= (1 << 7);
153 if (pdata->lxw_freq)
154 value |= (LWX_FREQ(pdata->lxw_freq) << 4);
155 if (pdata->dual_string)
156 value |= (1 << 1);
157 ret = max8925_set_bits(chip->i2c, MAX8925_WLED_MODE_CNTL, 0xfe, value);
158 if (ret < 0)
159 goto out;
161 backlight_update_status(bl);
162 return 0;
163 out:
164 kfree(data);
165 return ret;
168 static int __devexit max8925_backlight_remove(struct platform_device *pdev)
170 struct backlight_device *bl = platform_get_drvdata(pdev);
171 struct max8925_backlight_data *data = bl_get_data(bl);
173 backlight_device_unregister(bl);
174 kfree(data);
175 return 0;
178 static struct platform_driver max8925_backlight_driver = {
179 .driver = {
180 .name = "max8925-backlight",
181 .owner = THIS_MODULE,
183 .probe = max8925_backlight_probe,
184 .remove = __devexit_p(max8925_backlight_remove),
187 static int __init max8925_backlight_init(void)
189 return platform_driver_register(&max8925_backlight_driver);
191 module_init(max8925_backlight_init);
193 static void __exit max8925_backlight_exit(void)
195 platform_driver_unregister(&max8925_backlight_driver);
197 module_exit(max8925_backlight_exit);
199 MODULE_DESCRIPTION("Backlight Driver for Maxim MAX8925");
200 MODULE_AUTHOR("Haojian Zhuang <haojian.zhuang@marvell.com>");
201 MODULE_LICENSE("GPL");
202 MODULE_ALIAS("platform:max8925-backlight");