blob: f3aa6088f1d97805f23b9780a1db893cf1a46b00 [file] [log] [blame]
Haojian Zhuang4f811ef2009-12-18 10:01:45 -05001/*
2 * Backlight driver for Maxim MAX8925
3 *
4 * Copyright (C) 2009 Marvell International Ltd.
5 * Haojian Zhuang <haojian.zhuang@marvell.com>
6 *
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.
10 */
11
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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090019#include <linux/slab.h>
Paul Gortmaker355b2002011-07-03 16:17:28 -040020#include <linux/module.h>
Haojian Zhuang4f811ef2009-12-18 10:01:45 -050021
22#define MAX_BRIGHTNESS (0xff)
23#define MIN_BRIGHTNESS (0)
24
25#define LWX_FREQ(x) (((x - 601) / 100) & 0x7)
26
27struct max8925_backlight_data {
28 struct max8925_chip *chip;
29
Haojian Zhuang63b501e2012-09-17 12:19:04 +080030 int current_brightness;
31 int reg_mode_cntl;
32 int reg_cntl;
Haojian Zhuang4f811ef2009-12-18 10:01:45 -050033};
34
35static int max8925_backlight_set(struct backlight_device *bl, int brightness)
36{
37 struct max8925_backlight_data *data = bl_get_data(bl);
38 struct max8925_chip *chip = data->chip;
39 unsigned char value;
40 int ret;
41
42 if (brightness > MAX_BRIGHTNESS)
43 value = MAX_BRIGHTNESS;
44 else
45 value = brightness;
46
Haojian Zhuang63b501e2012-09-17 12:19:04 +080047 ret = max8925_reg_write(chip->i2c, data->reg_cntl, value);
Haojian Zhuang4f811ef2009-12-18 10:01:45 -050048 if (ret < 0)
49 goto out;
50
51 if (!data->current_brightness && brightness)
52 /* enable WLED output */
Haojian Zhuang63b501e2012-09-17 12:19:04 +080053 ret = max8925_set_bits(chip->i2c, data->reg_mode_cntl, 1, 1);
Haojian Zhuang4f811ef2009-12-18 10:01:45 -050054 else if (!brightness)
55 /* disable WLED output */
Haojian Zhuang63b501e2012-09-17 12:19:04 +080056 ret = max8925_set_bits(chip->i2c, data->reg_mode_cntl, 1, 0);
Haojian Zhuang4f811ef2009-12-18 10:01:45 -050057 if (ret < 0)
58 goto out;
59 dev_dbg(chip->dev, "set brightness %d\n", value);
60 data->current_brightness = value;
61 return 0;
62out:
63 dev_dbg(chip->dev, "set brightness %d failure with return value:%d\n",
64 value, ret);
65 return ret;
66}
67
68static int max8925_backlight_update_status(struct backlight_device *bl)
69{
70 int brightness = bl->props.brightness;
71
72 if (bl->props.power != FB_BLANK_UNBLANK)
73 brightness = 0;
74
75 if (bl->props.fb_blank != FB_BLANK_UNBLANK)
76 brightness = 0;
77
78 if (bl->props.state & BL_CORE_SUSPENDED)
79 brightness = 0;
80
81 return max8925_backlight_set(bl, brightness);
82}
83
84static int max8925_backlight_get_brightness(struct backlight_device *bl)
85{
86 struct max8925_backlight_data *data = bl_get_data(bl);
87 struct max8925_chip *chip = data->chip;
88 int ret;
89
Haojian Zhuang63b501e2012-09-17 12:19:04 +080090 ret = max8925_reg_read(chip->i2c, data->reg_cntl);
Haojian Zhuang4f811ef2009-12-18 10:01:45 -050091 if (ret < 0)
92 return -EINVAL;
93 data->current_brightness = ret;
94 dev_dbg(chip->dev, "get brightness %d\n", data->current_brightness);
95 return ret;
96}
97
Lionel Debrouxacc24722010-11-16 14:14:02 +010098static const struct backlight_ops max8925_backlight_ops = {
Haojian Zhuang4f811ef2009-12-18 10:01:45 -050099 .options = BL_CORE_SUSPENDRESUME,
100 .update_status = max8925_backlight_update_status,
101 .get_brightness = max8925_backlight_get_brightness,
102};
103
Olof Johansson515c0962013-07-24 08:42:27 -0700104static void max8925_backlight_dt_init(struct platform_device *pdev)
Qing Xu47ec3402013-02-04 23:40:45 +0800105{
106 struct device_node *nproot = pdev->dev.parent->of_node, *np;
Olof Johansson515c0962013-07-24 08:42:27 -0700107 struct max8925_backlight_pdata *pdata;
108 u32 val;
Qing Xu47ec3402013-02-04 23:40:45 +0800109
Olof Johansson515c0962013-07-24 08:42:27 -0700110 if (!nproot || !IS_ENABLED(CONFIG_OF))
111 return;
112
113 pdata = devm_kzalloc(&pdev->dev,
114 sizeof(struct max8925_backlight_pdata),
115 GFP_KERNEL);
116 if (!pdata)
117 return;
118
Johan Hovolda89e5962017-11-20 11:45:45 +0100119 np = of_get_child_by_name(nproot, "backlight");
Qing Xu47ec3402013-02-04 23:40:45 +0800120 if (!np) {
121 dev_err(&pdev->dev, "failed to find backlight node\n");
Olof Johansson515c0962013-07-24 08:42:27 -0700122 return;
Qing Xu47ec3402013-02-04 23:40:45 +0800123 }
124
Olof Johansson515c0962013-07-24 08:42:27 -0700125 if (!of_property_read_u32(np, "maxim,max8925-dual-string", &val))
126 pdata->dual_string = val;
127
Johan Hovolda89e5962017-11-20 11:45:45 +0100128 of_node_put(np);
129
Olof Johansson515c0962013-07-24 08:42:27 -0700130 pdev->dev.platform_data = pdata;
Qing Xu47ec3402013-02-04 23:40:45 +0800131}
Qing Xu47ec3402013-02-04 23:40:45 +0800132
Bill Pemberton1b9e4502012-11-19 13:21:46 -0500133static int max8925_backlight_probe(struct platform_device *pdev)
Haojian Zhuang4f811ef2009-12-18 10:01:45 -0500134{
135 struct max8925_chip *chip = dev_get_drvdata(pdev->dev.parent);
Olof Johansson515c0962013-07-24 08:42:27 -0700136 struct max8925_backlight_pdata *pdata;
Haojian Zhuang4f811ef2009-12-18 10:01:45 -0500137 struct max8925_backlight_data *data;
138 struct backlight_device *bl;
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500139 struct backlight_properties props;
Haojian Zhuang4f811ef2009-12-18 10:01:45 -0500140 struct resource *res;
Haojian Zhuang4f811ef2009-12-18 10:01:45 -0500141 unsigned char value;
Haojian Zhuang63b501e2012-09-17 12:19:04 +0800142 int ret = 0;
Haojian Zhuang4f811ef2009-12-18 10:01:45 -0500143
Julia Lawallce969222012-03-23 15:02:00 -0700144 data = devm_kzalloc(&pdev->dev, sizeof(struct max8925_backlight_data),
145 GFP_KERNEL);
Haojian Zhuang4f811ef2009-12-18 10:01:45 -0500146 if (data == NULL)
147 return -ENOMEM;
Haojian Zhuang63b501e2012-09-17 12:19:04 +0800148
149 res = platform_get_resource(pdev, IORESOURCE_REG, 0);
150 if (!res) {
151 dev_err(&pdev->dev, "No REG resource for mode control!\n");
Jingoo Han2c114cb2012-12-17 16:00:58 -0800152 return -ENXIO;
Haojian Zhuang63b501e2012-09-17 12:19:04 +0800153 }
154 data->reg_mode_cntl = res->start;
155 res = platform_get_resource(pdev, IORESOURCE_REG, 1);
156 if (!res) {
157 dev_err(&pdev->dev, "No REG resource for control!\n");
Jingoo Han2c114cb2012-12-17 16:00:58 -0800158 return -ENXIO;
Haojian Zhuang63b501e2012-09-17 12:19:04 +0800159 }
160 data->reg_cntl = res->start;
161
Haojian Zhuang4f811ef2009-12-18 10:01:45 -0500162 data->chip = chip;
163 data->current_brightness = 0;
164
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500165 memset(&props, 0, sizeof(struct backlight_properties));
Matthew Garrettbb7ca742011-03-22 16:30:21 -0700166 props.type = BACKLIGHT_RAW;
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500167 props.max_brightness = MAX_BRIGHTNESS;
Jingoo Han18d9dbc2013-11-12 15:09:21 -0800168 bl = devm_backlight_device_register(&pdev->dev, "max8925-backlight",
169 &pdev->dev, data,
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500170 &max8925_backlight_ops, &props);
Haojian Zhuang4f811ef2009-12-18 10:01:45 -0500171 if (IS_ERR(bl)) {
172 dev_err(&pdev->dev, "failed to register backlight\n");
Jingoo Han2c114cb2012-12-17 16:00:58 -0800173 return PTR_ERR(bl);
Haojian Zhuang4f811ef2009-12-18 10:01:45 -0500174 }
Haojian Zhuang4f811ef2009-12-18 10:01:45 -0500175 bl->props.brightness = MAX_BRIGHTNESS;
176
177 platform_set_drvdata(pdev, bl);
178
179 value = 0;
Olof Johansson515c0962013-07-24 08:42:27 -0700180 if (!pdev->dev.platform_data)
181 max8925_backlight_dt_init(pdev);
Qing Xu47ec3402013-02-04 23:40:45 +0800182
Olof Johansson515c0962013-07-24 08:42:27 -0700183 pdata = pdev->dev.platform_data;
Haojian Zhuang63b501e2012-09-17 12:19:04 +0800184 if (pdata) {
185 if (pdata->lxw_scl)
186 value |= (1 << 7);
187 if (pdata->lxw_freq)
188 value |= (LWX_FREQ(pdata->lxw_freq) << 4);
189 if (pdata->dual_string)
190 value |= (1 << 1);
191 }
192 ret = max8925_set_bits(chip->i2c, data->reg_mode_cntl, 0xfe, value);
Haojian Zhuang4f811ef2009-12-18 10:01:45 -0500193 if (ret < 0)
Jingoo Han18d9dbc2013-11-12 15:09:21 -0800194 return ret;
Haojian Zhuang4f811ef2009-12-18 10:01:45 -0500195 backlight_update_status(bl);
196 return 0;
Haojian Zhuang4f811ef2009-12-18 10:01:45 -0500197}
198
199static struct platform_driver max8925_backlight_driver = {
200 .driver = {
201 .name = "max8925-backlight",
Haojian Zhuang4f811ef2009-12-18 10:01:45 -0500202 },
203 .probe = max8925_backlight_probe,
Haojian Zhuang4f811ef2009-12-18 10:01:45 -0500204};
205
Axel Lin81178e02012-01-10 15:09:11 -0800206module_platform_driver(max8925_backlight_driver);
Haojian Zhuang4f811ef2009-12-18 10:01:45 -0500207
208MODULE_DESCRIPTION("Backlight Driver for Maxim MAX8925");
209MODULE_AUTHOR("Haojian Zhuang <haojian.zhuang@marvell.com>");
210MODULE_LICENSE("GPL");
211MODULE_ALIAS("platform:max8925-backlight");