Home Home > GIT Browse > stable-xen
summaryrefslogtreecommitdiff
blob: f0f28c4428070a8f8bb881500be5f00e5d7e024d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/*
 * Copyright (c) 2017 Sebastian Reichel <sre@kernel.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 or
 * later as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

#include <linux/leds.h>
#include <linux/mfd/motorola-cpcap.h>
#include <linux/module.h>
#include <linux/mutex.h>
#include <linux/of_device.h>
#include <linux/platform_device.h>
#include <linux/regmap.h>
#include <linux/regulator/consumer.h>

#define CPCAP_LED_NO_CURRENT 0x0001

struct cpcap_led_info {
	u16 reg;
	u16 mask;
	u16 limit;
	u16 init_mask;
	u16 init_val;
};

static const struct cpcap_led_info cpcap_led_red = {
	.reg	= CPCAP_REG_REDC,
	.mask	= 0x03FF,
	.limit	= 31,
};

static const struct cpcap_led_info cpcap_led_green = {
	.reg	= CPCAP_REG_GREENC,
	.mask	= 0x03FF,
	.limit	= 31,
};

static const struct cpcap_led_info cpcap_led_blue = {
	.reg	= CPCAP_REG_BLUEC,
	.mask	= 0x03FF,
	.limit	= 31,
};

/* aux display light */
static const struct cpcap_led_info cpcap_led_adl = {
	.reg		= CPCAP_REG_ADLC,
	.mask		= 0x000F,
	.limit		= 1,
	.init_mask	= 0x7FFF,
	.init_val	= 0x5FF0,
};

/* camera privacy led */
static const struct cpcap_led_info cpcap_led_cp = {
	.reg		= CPCAP_REG_CLEDC,
	.mask		= 0x0007,
	.limit		= 1,
	.init_mask	= 0x03FF,
	.init_val	= 0x0008,
};

struct cpcap_led {
	struct led_classdev led;
	const struct cpcap_led_info *info;
	struct device *dev;
	struct regmap *regmap;
	struct mutex update_lock;
	struct regulator *vdd;
	bool powered;

	u32 current_limit;
};

static u16 cpcap_led_val(u8 current_limit, u8 duty_cycle)
{
	current_limit &= 0x1f; /* 5 bit */
	duty_cycle &= 0x0f; /* 4 bit */

	return current_limit << 4 | duty_cycle;
}

static int cpcap_led_set_power(struct cpcap_led *led, bool status)
{
	int err;

	if (status == led->powered)
		return 0;

	if (status)
		err = regulator_enable(led->vdd);
	else
		err = regulator_disable(led->vdd);

	if (err) {
		dev_err(led->dev, "regulator failure: %d", err);
		return err;
	}

	led->powered = status;

	return 0;
}

static int cpcap_led_set(struct led_classdev *ledc, enum led_brightness value)
{
	struct cpcap_led *led = container_of(ledc, struct cpcap_led, led);
	int brightness;
	int err;

	mutex_lock(&led->update_lock);

	if (value > LED_OFF) {
		err = cpcap_led_set_power(led, true);
		if (err)
			goto exit;
	}

	if (value == LED_OFF) {
		/* Avoid HW issue by turning off current before duty cycle */
		err = regmap_update_bits(led->regmap,
			led->info->reg, led->info->mask, CPCAP_LED_NO_CURRENT);
		if (err) {
			dev_err(led->dev, "regmap failed: %d", err);
			goto exit;
		}

		brightness = cpcap_led_val(value, LED_OFF);
	} else {
		brightness = cpcap_led_val(value, LED_ON);
	}

	err = regmap_update_bits(led->regmap, led->info->reg, led->info->mask,
		brightness);
	if (err) {
		dev_err(led->dev, "regmap failed: %d", err);
		goto exit;
	}

	if (value == LED_OFF) {
		err = cpcap_led_set_power(led, false);
		if (err)
			goto exit;
	}

exit:
	mutex_unlock(&led->update_lock);
	return err;
}

static const struct of_device_id cpcap_led_of_match[] = {
	{ .compatible = "motorola,cpcap-led-red", .data = &cpcap_led_red },
	{ .compatible = "motorola,cpcap-led-green", .data = &cpcap_led_green },
	{ .compatible = "motorola,cpcap-led-blue",  .data = &cpcap_led_blue },
	{ .compatible = "motorola,cpcap-led-adl", .data = &cpcap_led_adl },
	{ .compatible = "motorola,cpcap-led-cp", .data = &cpcap_led_cp },
	{},
};
MODULE_DEVICE_TABLE(of, cpcap_led_of_match);

static int cpcap_led_probe(struct platform_device *pdev)
{
	const struct of_device_id *match;
	struct cpcap_led *led;
	int err;

	match = of_match_device(of_match_ptr(cpcap_led_of_match), &pdev->dev);
	if (!match || !match->data)
		return -EINVAL;

	led = devm_kzalloc(&pdev->dev, sizeof(*led), GFP_KERNEL);
	if (!led)
		return -ENOMEM;
	platform_set_drvdata(pdev, led);
	led->info = match->data;
	led->dev = &pdev->dev;

	if (led->info->reg == 0x0000) {
		dev_err(led->dev, "Unsupported LED");
		return -ENODEV;
	}

	led->regmap = dev_get_regmap(pdev->dev.parent, NULL);
	if (!led->regmap)
		return -ENODEV;

	led->vdd = devm_regulator_get(&pdev->dev, "vdd");
	if (IS_ERR(led->vdd)) {
		err = PTR_ERR(led->vdd);
		dev_err(led->dev, "Couldn't get regulator: %d", err);
		return err;
	}

	err = device_property_read_string(&pdev->dev, "label", &led->led.name);
	if (err) {
		dev_err(led->dev, "Couldn't read LED label: %d", err);
		return err;
	}

	if (led->info->init_mask) {
		err = regmap_update_bits(led->regmap, led->info->reg,
			led->info->init_mask, led->info->init_val);
		if (err) {
			dev_err(led->dev, "regmap failed: %d", err);
			return err;
		}
	}

	mutex_init(&led->update_lock);

	led->led.max_brightness = led->info->limit;
	led->led.brightness_set_blocking = cpcap_led_set;
	err = devm_led_classdev_register(&pdev->dev, &led->led);
	if (err) {
		dev_err(led->dev, "Couldn't register LED: %d", err);
		return err;
	}

	return 0;
}

static struct platform_driver cpcap_led_driver = {
	.probe = cpcap_led_probe,
	.driver = {
		.name = "cpcap-led",
		.of_match_table = cpcap_led_of_match,
	},
};
module_platform_driver(cpcap_led_driver);

MODULE_DESCRIPTION("CPCAP LED driver");
MODULE_AUTHOR("Sebastian Reichel <sre@kernel.org>");
MODULE_LICENSE("GPL");