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>
16 #include <linux/i2c.h>
17 #include <linux/backlight.h>
18 #include <linux/mfd/max8925.h>
19 #include <linux/slab.h>
20 #include <linux/module.h>
22 #define MAX_BRIGHTNESS (0xff)
23 #define MIN_BRIGHTNESS (0)
25 #define LWX_FREQ(x) (((x - 601) / 100) & 0x7)
27 struct max8925_backlight_data
{
28 struct max8925_chip
*chip
;
30 int current_brightness
;
33 static int max8925_backlight_set(struct backlight_device
*bl
, int brightness
)
35 struct max8925_backlight_data
*data
= bl_get_data(bl
);
36 struct max8925_chip
*chip
= data
->chip
;
40 if (brightness
> MAX_BRIGHTNESS
)
41 value
= MAX_BRIGHTNESS
;
45 ret
= max8925_reg_write(chip
->i2c
, MAX8925_WLED_CNTL
, value
);
49 if (!data
->current_brightness
&& brightness
)
50 /* enable WLED output */
51 ret
= max8925_set_bits(chip
->i2c
, MAX8925_WLED_MODE_CNTL
, 1, 1);
53 /* disable WLED output */
54 ret
= max8925_set_bits(chip
->i2c
, MAX8925_WLED_MODE_CNTL
, 1, 0);
57 dev_dbg(chip
->dev
, "set brightness %d\n", value
);
58 data
->current_brightness
= value
;
61 dev_dbg(chip
->dev
, "set brightness %d failure with return value:%d\n",
66 static int max8925_backlight_update_status(struct backlight_device
*bl
)
68 int brightness
= bl
->props
.brightness
;
70 if (bl
->props
.power
!= FB_BLANK_UNBLANK
)
73 if (bl
->props
.fb_blank
!= FB_BLANK_UNBLANK
)
76 if (bl
->props
.state
& BL_CORE_SUSPENDED
)
79 return max8925_backlight_set(bl
, brightness
);
82 static int max8925_backlight_get_brightness(struct backlight_device
*bl
)
84 struct max8925_backlight_data
*data
= bl_get_data(bl
);
85 struct max8925_chip
*chip
= data
->chip
;
88 ret
= max8925_reg_read(chip
->i2c
, MAX8925_WLED_CNTL
);
91 data
->current_brightness
= ret
;
92 dev_dbg(chip
->dev
, "get brightness %d\n", data
->current_brightness
);
96 static const struct backlight_ops max8925_backlight_ops
= {
97 .options
= BL_CORE_SUSPENDRESUME
,
98 .update_status
= max8925_backlight_update_status
,
99 .get_brightness
= max8925_backlight_get_brightness
,
102 static int __devinit
max8925_backlight_probe(struct platform_device
*pdev
)
104 struct max8925_chip
*chip
= dev_get_drvdata(pdev
->dev
.parent
);
105 struct max8925_platform_data
*max8925_pdata
;
106 struct max8925_backlight_pdata
*pdata
= NULL
;
107 struct max8925_backlight_data
*data
;
108 struct backlight_device
*bl
;
109 struct backlight_properties props
;
110 struct resource
*res
;
111 char name
[MAX8925_NAME_SIZE
];
115 res
= platform_get_resource(pdev
, IORESOURCE_IO
, 0);
117 dev_err(&pdev
->dev
, "No I/O resource!\n");
121 if (pdev
->dev
.parent
->platform_data
) {
122 max8925_pdata
= pdev
->dev
.parent
->platform_data
;
123 pdata
= max8925_pdata
->backlight
;
127 dev_err(&pdev
->dev
, "platform data isn't assigned to "
132 data
= kzalloc(sizeof(struct max8925_backlight_data
), GFP_KERNEL
);
135 strncpy(name
, res
->name
, MAX8925_NAME_SIZE
);
137 data
->current_brightness
= 0;
139 memset(&props
, 0, sizeof(struct backlight_properties
));
140 props
.type
= BACKLIGHT_RAW
;
141 props
.max_brightness
= MAX_BRIGHTNESS
;
142 bl
= backlight_device_register(name
, &pdev
->dev
, data
,
143 &max8925_backlight_ops
, &props
);
145 dev_err(&pdev
->dev
, "failed to register backlight\n");
149 bl
->props
.brightness
= MAX_BRIGHTNESS
;
151 platform_set_drvdata(pdev
, bl
);
157 value
|= (LWX_FREQ(pdata
->lxw_freq
) << 4);
158 if (pdata
->dual_string
)
160 ret
= max8925_set_bits(chip
->i2c
, MAX8925_WLED_MODE_CNTL
, 0xfe, value
);
164 backlight_update_status(bl
);
167 backlight_device_unregister(bl
);
172 static int __devexit
max8925_backlight_remove(struct platform_device
*pdev
)
174 struct backlight_device
*bl
= platform_get_drvdata(pdev
);
175 struct max8925_backlight_data
*data
= bl_get_data(bl
);
177 backlight_device_unregister(bl
);
182 static struct platform_driver max8925_backlight_driver
= {
184 .name
= "max8925-backlight",
185 .owner
= THIS_MODULE
,
187 .probe
= max8925_backlight_probe
,
188 .remove
= __devexit_p(max8925_backlight_remove
),
191 static int __init
max8925_backlight_init(void)
193 return platform_driver_register(&max8925_backlight_driver
);
195 module_init(max8925_backlight_init
);
197 static void __exit
max8925_backlight_exit(void)
199 platform_driver_unregister(&max8925_backlight_driver
);
201 module_exit(max8925_backlight_exit
);
203 MODULE_DESCRIPTION("Backlight Driver for Maxim MAX8925");
204 MODULE_AUTHOR("Haojian Zhuang <haojian.zhuang@marvell.com>");
205 MODULE_LICENSE("GPL");
206 MODULE_ALIAS("platform:max8925-backlight");