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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
|
/*
* LED driver for SEP
*
* Copyright (C) 2018 Samsung Electronics
* Author: Hyuk Kang, HyungJae Im
*
*/
#define pr_fmt(fmt) "sled: " fmt
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/device.h>
#include <linux/platform_device.h>
#include <linux/sec_class.h>
#include "led-sep.h"
static struct led_sep *led_sep_one;
static int calc_color(int old, int new, int color)
{
int r, g, b;
int rgb = 0;
if (old == new)
return color;
r = LED_R(color);
g = LED_G(color);
b = LED_B(color);
pr_info("calc_color1: (0x%x, 0x%x) R(0x%x) G(0x%x) B(0x%x)\n",
old, new, r, g, b);
r = (r * 100 / old) * new / 100;
g = (g * 100 / old) * new / 100;
b = (b * 100 / old) * new / 100;
pr_info("calc_color1: new R(0x%x) G(0x%x) B(0x%x)\n",
r, g, b);
rgb = COLOR(r, g, b);
pr_info("calc_color1: 0x%x -> 0x%x\n", color, rgb);
return rgb;
}
int led_sep_register_device(struct led_sep_ops *ops)
{
int max, low;
if (led_sep_one) {
led_sep_one->ops = ops;
max = ops->current_max;
led_sep_one->maxpower = max ? max : COLOR_MAX;
if (led_sep_one->maxpower > COLOR_MAX)
led_sep_one->maxpower = COLOR_MAX;
low = ops->current_lowpower;
led_sep_one->lowpower_current = (low > (max / 3)) ? low : max;
if (led_sep_one->lowpower_current > led_sep_one->maxpower)
led_sep_one->lowpower_current = led_sep_one->maxpower;
pr_info("New LED Added: %s, maxpower 0x%x, lowpower 0x%x\n",
ops->name,
led_sep_one->maxpower,
led_sep_one->lowpower_current);
} else
return -ENODEV;
return 0;
}
EXPORT_SYMBOL(led_sep_register_device);
static int led_sep_calc_color(struct led_sep *sled, int color)
{
int ret = color;
if (color) {
if (sled->lowpower_mode)
ret = calc_color(COLOR_MAX,
sled->lowpower_current, color);
else if (sled->maxpower != COLOR_MAX)
ret = calc_color(COLOR_MAX, sled->maxpower, color);
}
pr_info("calc_color2: 0x%x -> 0x%x\n", color, ret);
return ret;
}
#define check_capability(led, cap) \
do { \
if (!led_sep_check_capability(led, cap)) { \
pr_info("%s: check failed, 0x%x\n", \
__func__, cap); \
return -EPERM; \
} \
} while (0)
static inline int led_sep_check_capability(struct led_sep *sled,
unsigned long cap)
{
if (sled && sled->ops && (sled->ops->capability & cap))
return true;
return false;
}
static int led_sep_light(struct led_sep *sled,
int id, int on, int color)
{
int ret = 0;
int col = led_sep_calc_color(sled, color);
if (sled && sled->ops && sled->ops->light)
ret = sled->ops->light(sled->ops, id, on, col);
else {
pr_err("NO LED?\n");
ret = -ENODEV;
}
return ret;
}
static int led_sep_blink(struct led_sep *sled,
int id, int color, int on, int off, int blink_count)
{
int ret = 0;
int col = led_sep_calc_color(sled, color);
if (sled && sled->ops && sled->ops->blink)
ret = sled->ops->blink(sled->ops,
id, col, on, off, blink_count);
else {
pr_info("sw blink is not ready.\n");
//TODO
}
return ret;
}
static int led_sep_test(struct led_sep *sled,
int reg, int value)
{
int ret = 0;
if (sled && sled->ops && sled->ops->test)
ret = sled->ops->test(sled->ops, reg, value);
else {
pr_info("sw test is not supported.\n");
ret = -ENODEV;
}
return ret;
}
static int led_sep_reset(struct led_sep *sled)
{
int ret = 0;
if (sled && sled->ops && sled->ops->reset)
ret = sled->ops->reset(sled->ops);
else {
pr_info("sw reset is not supported.\n");
ret = -ENODEV;
}
return ret;
}
static int led_sep_pattern(struct led_sep *sled, int mode)
{
int ret = 0;
if (sled && sled->ops && sled->ops->pattern) {
ret = sled->ops->pattern(sled->ops, mode);
if (ret > 0) {
pr_info("%s ops->pattern Done\n", __func__);
return ret;
}
}
// TODO
// Common patterns for all LED
switch (mode) {
case PATTERN_OFF:
led_sep_light(sled, LED_ALL, 0, 0);
break;
case CHARGING:
break;
case CHARGING_ERR:
break;
case MISSED_NOTI:
break;
case LOW_BATTERY:
break;
case FULLY_CHARGED:
break;
case POWERING:
break;
case POWERING_OFF:
break;
case FOTA:
break;
default:
break;
}
pr_info("%s pattern Done\n", __func__);
return ret;
}
static ssize_t store_led_sep_lowpower(struct device *dev,
struct device_attribute *devattr,
const char *buf, size_t count)
{
struct led_sep *sled = dev_get_drvdata(dev);
u8 led_lowpower;
int ret;
check_capability(sled, SEP_NODE_LOWPOWER);
ret = kstrtou8(buf, 0, &led_lowpower);
if (ret != 0) {
dev_err(dev, "fail to get led_lowpower.\n");
return -EINVAL;
}
if (sled->maxpower < led_lowpower) {
dev_err(dev, "lowpower is higher than maxpower.\n");
return -EINVAL;
}
sled->lowpower_mode = led_lowpower;
pr_info("led_lowpower mode set to %i\n", led_lowpower);
return count;
}
static ssize_t store_led_sep_pattern(struct device *dev,
struct device_attribute *devattr,
const char *buf, size_t count)
{
struct led_sep *sled = dev_get_drvdata(dev);
unsigned int mode = 0;
int ret;
check_capability(sled, SEP_NODE_PATTERN);
ret = sscanf(buf, "%1d", &mode);
if (ret == 0) {
dev_err(dev, "fail to get led_pattern mode.\n");
return -EINVAL;
}
pr_info("%s: %s(%d) lowpower: %d current: %d\n", __func__,
pattern_string(mode), mode,
sled->lowpower_mode,
sled->lowpower_current);
led_sep_pattern(sled, mode);
return count;
}
/*
* turn on : color 1 0
* turn off : color 0 0
* blink : color 500 500
*
* Command format: color on off
*/
static ssize_t store_led_sep_blink(struct device *dev,
struct device_attribute *devattr,
const char *buf, size_t count)
{
struct led_sep *sled = dev_get_drvdata(dev);
int id, color, on_time, off_time;
int ret = 0;
color = on_time = off_time = 0;
id = LED_ALL;
pr_info("%s: len %d: %s", __func__, count, buf);
check_capability(sled, SEP_NODE_BLINK);
ret = sscanf(buf, "0x%8x %d %d", &color,
&on_time, &off_time);
if (ret == 0) {
dev_err(dev, "fail to get led_blink value.\n");
return -EINVAL;
}
pr_info("blink %d: 0x%x %d %d\n", ret, color,
on_time, off_time);
if (!on_time || on_time == 1)
led_sep_light(sled, id, on_time, color);
else
led_sep_blink(sled, id, color, on_time, off_time, 0);
return count;
}
/*
* New API for SEP LED
*
* turn on : id color 1 0
* turn off : id color 0 0
* blink : id color 500 500 3(TBD: count)
*
* Command format: id color on off count
*/
static ssize_t store_led_sep_control(struct device *dev,
struct device_attribute *devattr,
const char *buf, size_t count)
{
struct led_sep *sled = dev_get_drvdata(dev);
int id, color, on_time, off_time, blink_count;
int ret = 0;
id = color = on_time = off_time = blink_count = 0;
pr_info("%s: len %d: %s", __func__, count, buf);
check_capability(sled, SEP_NODE_CONTROL);
ret = sscanf(buf, "%d 0x%8x %d %d %d", &id, &color,
&on_time, &off_time, &blink_count);
if (ret < 3) {
dev_err(dev, "fail to get led_control value. %d\n", ret);
return -EINVAL;
}
pr_info("control %d: %d 0x%x %d %d %d\n", ret, id, color,
on_time, off_time, blink_count);
if (!on_time || on_time == 1)
led_sep_light(sled, id, on_time, color);
else
led_sep_blink(sled, id, color,
on_time, off_time, blink_count);
return count;
}
static ssize_t store_led_sep_test(struct device *dev,
struct device_attribute *devattr,
const char *buf, size_t count)
{
struct led_sep *sled = dev_get_drvdata(dev);
int ret = 0;
int reg, val;
pr_info("%s: %s (%d)\n", __func__, buf, count);
check_capability(sled, SEP_NODE_TEST);
ret = sscanf(buf, "%2x %2x", ®, &val);
if (ret == 0) {
dev_err(dev, "fail to get led_test value.\n");
return -EINVAL;
}
pr_info("test reg: %2x val: %2x\n", reg, val);
led_sep_test(sled, reg, val);
return count;
}
static ssize_t store_led_sep_reset(struct device *dev,
struct device_attribute *devattr,
const char *buf, size_t count)
{
struct led_sep *sled = dev_get_drvdata(dev);
int ret = 0;
int val;
pr_info("%s: %s (%d)\n", __func__, buf, count);
check_capability(sled, SEP_NODE_RESET);
ret = kstrtoint(buf, 0, &val);
if (ret < 0) {
dev_err(dev, "fail to get led_reset value.\n");
return -EINVAL;
}
led_sep_reset(sled);
return count;
}
static DEVICE_ATTR(led_lowpower, 0660, NULL, store_led_sep_lowpower);
static DEVICE_ATTR(led_pattern, 0660, NULL, store_led_sep_pattern);
static DEVICE_ATTR(led_blink, 0660, NULL, store_led_sep_blink);
static DEVICE_ATTR(led_control, 0660, NULL, store_led_sep_control);
static DEVICE_ATTR(led_test, 0660, NULL, store_led_sep_test);
static DEVICE_ATTR(led_reset, 0660, NULL, store_led_sep_reset);
static struct attribute *sec_led_attributes[] = {
&dev_attr_led_lowpower.attr,
&dev_attr_led_pattern.attr,
&dev_attr_led_blink.attr,
&dev_attr_led_control.attr,
&dev_attr_led_test.attr,
&dev_attr_led_reset.attr,
NULL,
};
static struct attribute_group sec_led_attr_group = {
.attrs = sec_led_attributes,
};
static int led_sep_probe(struct platform_device *pdev)
{
struct led_sep *sled;
int ret = 0;
dev_info(&pdev->dev, "%s\n", __func__);
sled = devm_kzalloc(&pdev->dev, sizeof(*sled), GFP_KERNEL);
if (sled == NULL)
return -ENOMEM;
sled->sdev = sec_device_create(sled, "led");
if (sled->sdev == NULL) {
dev_err(&pdev->dev, "Failed to create device for SEP led\n");
return -ENOMEM;
}
ret = (int)sysfs_create_group(&sled->sdev->kobj, &sec_led_attr_group);
if (ret < 0) {
dev_err(&pdev->dev, "Failed to create sysfs group for SEP led\n");
sec_device_destroy(sled->sdev->devt);
return -ENOMEM;
}
led_sep_one = sled;
platform_set_drvdata(pdev, sled);
dev_info(&pdev->dev, "%s done\n", __func__);
return ret;
}
static int led_sep_remove(struct platform_device *pdev)
{
struct led_sep *sled = platform_get_drvdata(pdev);
dev_info(&pdev->dev, "%s\n", __func__);
sysfs_remove_group(&sled->sdev->kobj, &sec_led_attr_group);
sec_device_destroy(sled->sdev->devt);
dev_info(&pdev->dev, "%s done\n", __func__);
return 0;
}
static const struct of_device_id led_sep_dt_ids[] = {
{ .compatible = "samsung,led-sep",
},
{ },
};
MODULE_DEVICE_TABLE(of, led_sep_dt_ids);
static struct platform_driver led_sep_driver = {
.probe = led_sep_probe,
.remove = led_sep_remove,
.driver = {
.name = "led_sep",
.owner = THIS_MODULE,
.of_match_table = of_match_ptr(led_sep_dt_ids),
},
};
static int __init led_sep_init(void)
{
return platform_driver_register(&led_sep_driver);
}
static void __init led_sep_exit(void)
{
platform_driver_unregister(&led_sep_driver);
}
subsys_initcall(led_sep_init);
module_exit(led_sep_exit);
MODULE_AUTHOR("Samsung EIF Team");
|