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
;
35 static int max8925_backlight_set(struct backlight_device
*bl
, int brightness
)
37 struct max8925_backlight_data
*data
= bl_get_data(bl
);
38 struct max8925_chip
*chip
= data
->chip
;
42 if (brightness
> MAX_BRIGHTNESS
)
43 value
= MAX_BRIGHTNESS
;
47 ret
= max8925_reg_write(chip
->i2c
, data
->reg_cntl
, value
);
51 if (!data
->current_brightness
&& brightness
)
52 /* enable WLED output */
53 ret
= max8925_set_bits(chip
->i2c
, data
->reg_mode_cntl
, 1, 1);
55 /* disable WLED output */
56 ret
= max8925_set_bits(chip
->i2c
, data
->reg_mode_cntl
, 1, 0);
59 dev_dbg(chip
->dev
, "set brightness %d\n", value
);
60 data
->current_brightness
= value
;
63 dev_dbg(chip
->dev
, "set brightness %d failure with return value:%d\n",
68 static int max8925_backlight_update_status(struct backlight_device
*bl
)
70 int brightness
= bl
->props
.brightness
;
72 if (bl
->props
.power
!= FB_BLANK_UNBLANK
)
75 if (bl
->props
.fb_blank
!= FB_BLANK_UNBLANK
)
78 if (bl
->props
.state
& BL_CORE_SUSPENDED
)
81 return max8925_backlight_set(bl
, brightness
);
84 static int max8925_backlight_get_brightness(struct backlight_device
*bl
)
86 struct max8925_backlight_data
*data
= bl_get_data(bl
);
87 struct max8925_chip
*chip
= data
->chip
;
90 ret
= max8925_reg_read(chip
->i2c
, data
->reg_cntl
);
93 data
->current_brightness
= ret
;
94 dev_dbg(chip
->dev
, "get brightness %d\n", data
->current_brightness
);
98 static const struct backlight_ops max8925_backlight_ops
= {
99 .options
= BL_CORE_SUSPENDRESUME
,
100 .update_status
= max8925_backlight_update_status
,
101 .get_brightness
= max8925_backlight_get_brightness
,
105 static int max8925_backlight_dt_init(struct platform_device
*pdev
,
106 struct max8925_backlight_pdata
*pdata
)
108 struct device_node
*nproot
= pdev
->dev
.parent
->of_node
, *np
;
113 np
= of_find_node_by_name(nproot
, "backlight");
115 dev_err(&pdev
->dev
, "failed to find backlight node\n");
119 of_property_read_u32(np
, "maxim,max8925-dual-string", &dual_string
);
120 pdata
->dual_string
= dual_string
;
124 #define max8925_backlight_dt_init(x, y) (-1)
127 static int max8925_backlight_probe(struct platform_device
*pdev
)
129 struct max8925_chip
*chip
= dev_get_drvdata(pdev
->dev
.parent
);
130 struct max8925_backlight_pdata
*pdata
= pdev
->dev
.platform_data
;
131 struct max8925_backlight_data
*data
;
132 struct backlight_device
*bl
;
133 struct backlight_properties props
;
134 struct resource
*res
;
138 data
= devm_kzalloc(&pdev
->dev
, sizeof(struct max8925_backlight_data
),
143 res
= platform_get_resource(pdev
, IORESOURCE_REG
, 0);
145 dev_err(&pdev
->dev
, "No REG resource for mode control!\n");
148 data
->reg_mode_cntl
= res
->start
;
149 res
= platform_get_resource(pdev
, IORESOURCE_REG
, 1);
151 dev_err(&pdev
->dev
, "No REG resource for control!\n");
154 data
->reg_cntl
= res
->start
;
157 data
->current_brightness
= 0;
159 memset(&props
, 0, sizeof(struct backlight_properties
));
160 props
.type
= BACKLIGHT_RAW
;
161 props
.max_brightness
= MAX_BRIGHTNESS
;
162 bl
= backlight_device_register("max8925-backlight", &pdev
->dev
, data
,
163 &max8925_backlight_ops
, &props
);
165 dev_err(&pdev
->dev
, "failed to register backlight\n");
168 bl
->props
.brightness
= MAX_BRIGHTNESS
;
170 platform_set_drvdata(pdev
, bl
);
173 if (pdev
->dev
.parent
->of_node
&& !pdata
) {
174 pdata
= devm_kzalloc(&pdev
->dev
,
175 sizeof(struct max8925_backlight_pdata
),
177 max8925_backlight_dt_init(pdev
, pdata
);
184 value
|= (LWX_FREQ(pdata
->lxw_freq
) << 4);
185 if (pdata
->dual_string
)
188 ret
= max8925_set_bits(chip
->i2c
, data
->reg_mode_cntl
, 0xfe, value
);
191 backlight_update_status(bl
);
194 backlight_device_unregister(bl
);
198 static int max8925_backlight_remove(struct platform_device
*pdev
)
200 struct backlight_device
*bl
= platform_get_drvdata(pdev
);
202 backlight_device_unregister(bl
);
206 static struct platform_driver max8925_backlight_driver
= {
208 .name
= "max8925-backlight",
209 .owner
= THIS_MODULE
,
211 .probe
= max8925_backlight_probe
,
212 .remove
= max8925_backlight_remove
,
215 module_platform_driver(max8925_backlight_driver
);
217 MODULE_DESCRIPTION("Backlight Driver for Maxim MAX8925");
218 MODULE_AUTHOR("Haojian Zhuang <haojian.zhuang@marvell.com>");
219 MODULE_LICENSE("GPL");
220 MODULE_ALIAS("platform:max8925-backlight");