blob: 62540e4bdedbadde24e94d769ef1152bc8838cfd [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Mike Rapoport923ae482008-10-13 09:03:44 +01002/*
3 * Backlight driver for Dialog Semiconductor DA9030/DA9034
4 *
5 * Copyright (C) 2008 Compulab, Ltd.
Jingoo Han685746d2012-12-17 16:00:15 -08006 * Mike Rapoport <mike@compulab.co.il>
Mike Rapoport923ae482008-10-13 09:03:44 +01007 *
8 * Copyright (C) 2006-2008 Marvell International Ltd.
Jingoo Han685746d2012-12-17 16:00:15 -08009 * Eric Miao <eric.miao@marvell.com>
Mike Rapoport923ae482008-10-13 09:03:44 +010010 */
11
12#include <linux/kernel.h>
13#include <linux/init.h>
14#include <linux/platform_device.h>
15#include <linux/fb.h>
16#include <linux/backlight.h>
17#include <linux/mfd/da903x.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/slab.h>
Paul Gortmaker355b2002011-07-03 16:17:28 -040019#include <linux/module.h>
Mike Rapoport923ae482008-10-13 09:03:44 +010020
21#define DA9030_WLED_CONTROL 0x25
22#define DA9030_WLED_CP_EN (1 << 6)
23#define DA9030_WLED_TRIM(x) ((x) & 0x7)
24
25#define DA9034_WLED_CONTROL1 0x3C
26#define DA9034_WLED_CONTROL2 0x3D
Haojian Zhuangb3a85492009-11-05 10:27:13 -050027#define DA9034_WLED_ISET(x) ((x) & 0x1f)
Mike Rapoport923ae482008-10-13 09:03:44 +010028
29#define DA9034_WLED_BOOST_EN (1 << 5)
30
31#define DA9030_MAX_BRIGHTNESS 7
32#define DA9034_MAX_BRIGHTNESS 0x7f
33
34struct da903x_backlight_data {
35 struct device *da903x_dev;
36 int id;
37 int current_brightness;
38};
39
40static int da903x_backlight_set(struct backlight_device *bl, int brightness)
41{
42 struct da903x_backlight_data *data = bl_get_data(bl);
43 struct device *dev = data->da903x_dev;
44 uint8_t val;
45 int ret = 0;
46
47 switch (data->id) {
48 case DA9034_ID_WLED:
49 ret = da903x_update(dev, DA9034_WLED_CONTROL1,
50 brightness, 0x7f);
51 if (ret)
52 return ret;
53
54 if (data->current_brightness && brightness == 0)
55 ret = da903x_clr_bits(dev,
56 DA9034_WLED_CONTROL2,
57 DA9034_WLED_BOOST_EN);
58
59 if (data->current_brightness == 0 && brightness)
60 ret = da903x_set_bits(dev,
61 DA9034_WLED_CONTROL2,
62 DA9034_WLED_BOOST_EN);
63 break;
64 case DA9030_ID_WLED:
65 val = DA9030_WLED_TRIM(brightness);
66 val |= brightness ? DA9030_WLED_CP_EN : 0;
67 ret = da903x_write(dev, DA9030_WLED_CONTROL, val);
68 break;
69 }
70
71 if (ret)
72 return ret;
73
74 data->current_brightness = brightness;
75 return 0;
76}
77
78static int da903x_backlight_update_status(struct backlight_device *bl)
79{
80 int brightness = bl->props.brightness;
81
82 if (bl->props.power != FB_BLANK_UNBLANK)
83 brightness = 0;
84
85 if (bl->props.fb_blank != FB_BLANK_UNBLANK)
86 brightness = 0;
87
Jingoo Hand1720b52013-04-29 16:17:51 -070088 if (bl->props.state & BL_CORE_SUSPENDED)
89 brightness = 0;
90
Mike Rapoport923ae482008-10-13 09:03:44 +010091 return da903x_backlight_set(bl, brightness);
92}
93
94static int da903x_backlight_get_brightness(struct backlight_device *bl)
95{
96 struct da903x_backlight_data *data = bl_get_data(bl);
Jingoo Hand1720b52013-04-29 16:17:51 -070097
Mike Rapoport923ae482008-10-13 09:03:44 +010098 return data->current_brightness;
99}
100
Emese Revfy9905a432009-12-14 00:58:57 +0100101static const struct backlight_ops da903x_backlight_ops = {
Jingoo Hand1720b52013-04-29 16:17:51 -0700102 .options = BL_CORE_SUSPENDRESUME,
Mike Rapoport923ae482008-10-13 09:03:44 +0100103 .update_status = da903x_backlight_update_status,
104 .get_brightness = da903x_backlight_get_brightness,
105};
106
107static int da903x_backlight_probe(struct platform_device *pdev)
108{
Jingoo Hanc512794c2013-11-12 15:09:04 -0800109 struct da9034_backlight_pdata *pdata = dev_get_platdata(&pdev->dev);
Mike Rapoport923ae482008-10-13 09:03:44 +0100110 struct da903x_backlight_data *data;
111 struct backlight_device *bl;
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500112 struct backlight_properties props;
Mike Rapoport923ae482008-10-13 09:03:44 +0100113 int max_brightness;
114
Julia Lawallce969222012-03-23 15:02:00 -0700115 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
Mike Rapoport923ae482008-10-13 09:03:44 +0100116 if (data == NULL)
117 return -ENOMEM;
118
119 switch (pdev->id) {
120 case DA9030_ID_WLED:
121 max_brightness = DA9030_MAX_BRIGHTNESS;
122 break;
123 case DA9034_ID_WLED:
124 max_brightness = DA9034_MAX_BRIGHTNESS;
125 break;
126 default:
127 dev_err(&pdev->dev, "invalid backlight device ID(%d)\n",
128 pdev->id);
Mike Rapoport923ae482008-10-13 09:03:44 +0100129 return -EINVAL;
130 }
131
132 data->id = pdev->id;
133 data->da903x_dev = pdev->dev.parent;
134 data->current_brightness = 0;
135
Haojian Zhuangb3a85492009-11-05 10:27:13 -0500136 /* adjust the WLED output current */
137 if (pdata)
138 da903x_write(data->da903x_dev, DA9034_WLED_CONTROL2,
139 DA9034_WLED_ISET(pdata->output_current));
140
Corentin Charyf5f4fd42012-05-29 15:07:20 -0700141 memset(&props, 0, sizeof(props));
Matthew Garrettbb7ca742011-03-22 16:30:21 -0700142 props.type = BACKLIGHT_RAW;
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500143 props.max_brightness = max_brightness;
Jingoo Han56445292013-11-12 15:09:12 -0800144 bl = devm_backlight_device_register(&pdev->dev, pdev->name,
145 data->da903x_dev, data,
146 &da903x_backlight_ops, &props);
Mike Rapoport923ae482008-10-13 09:03:44 +0100147 if (IS_ERR(bl)) {
148 dev_err(&pdev->dev, "failed to register backlight\n");
Mike Rapoport923ae482008-10-13 09:03:44 +0100149 return PTR_ERR(bl);
150 }
151
Mike Rapoport923ae482008-10-13 09:03:44 +0100152 bl->props.brightness = max_brightness;
153
154 platform_set_drvdata(pdev, bl);
155 backlight_update_status(bl);
156 return 0;
157}
158
Mike Rapoport923ae482008-10-13 09:03:44 +0100159static struct platform_driver da903x_backlight_driver = {
160 .driver = {
161 .name = "da903x-backlight",
Mike Rapoport923ae482008-10-13 09:03:44 +0100162 },
163 .probe = da903x_backlight_probe,
Mike Rapoport923ae482008-10-13 09:03:44 +0100164};
165
Axel Lin81178e02012-01-10 15:09:11 -0800166module_platform_driver(da903x_backlight_driver);
Mike Rapoport923ae482008-10-13 09:03:44 +0100167
168MODULE_DESCRIPTION("Backlight Driver for Dialog Semiconductor DA9030/DA9034");
Jingoo Han685746d2012-12-17 16:00:15 -0800169MODULE_AUTHOR("Eric Miao <eric.miao@marvell.com>");
170MODULE_AUTHOR("Mike Rapoport <mike@compulab.co.il>");
Mike Rapoport923ae482008-10-13 09:03:44 +0100171MODULE_LICENSE("GPL");
172MODULE_ALIAS("platform:da903x-backlight");