summaryrefslogtreecommitdiff
path: root/drivers/leds/rtfled.c
blob: fa9802bb95e1b548c09780fc5fc6702ddbb278ff (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
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
/* drivers/leds/rtfled.c
 * Richtek Flash LED Universal Architecture
 *
 * Copyright (C) 2013 Richtek Technology Corp.
 * Author: Patrick Chang <patrick_chang@richtek.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 */

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/leds/rtfled.h>
#include <linux/init.h>
#include <linux/version.h>

#define RTFLED_INFO(format, args...) \
	printk(KERN_INFO "%s:%s() line-%d: " format, \
			ALIAS_NAME, __FUNCTION__, __LINE__, ## args)
#define RTFLED_WARN(format, args...) \
	printk(KERN_WARNING "%s:%s() line-%d: " format, \
			ALIAS_NAME, __FUNCTION__, __LINE__, ## args)
#define RTFLED_ERR(format, args...) \
	printk(KERN_ERR "%s:%s() line-%d: " format, \
			ALIAS_NAME, __FUNCTION__, __LINE__, ## args)

#define RT_FLED_DEVICE  "rt-flash-led"
#define ALIAS_NAME RT_FLED_DEVICE



rt_fled_info_t *rt_fled_get_info_by_name(char *name)
{
	struct flashlight_device *flashlight_dev;
	flashlight_dev = find_flashlight_by_name(name ? name : RT_FLED_DEVICE);
	if (flashlight_dev == NULL)
		return (rt_fled_info_t *)NULL;
	return flashlight_get_data(flashlight_dev);
}
EXPORT_SYMBOL(rt_fled_get_info_by_name);


static int rtfled_set_torch_brightness(struct flashlight_device *flashlight_dev,
		int brightness_sel)
{
	rt_fled_info_t *info = flashlight_get_data(flashlight_dev);
	return info->hal->fled_set_torch_current_sel(info, brightness_sel);
}

static int rtfled_set_strobe_brightness(struct flashlight_device
		*flashlight_dev,
		int brightness_sel)
{
	rt_fled_info_t *info = flashlight_get_data(flashlight_dev);
	return info->hal->fled_set_strobe_current_sel(info, brightness_sel);
}

static int rtfled_set_strobe_timeout(struct flashlight_device *flashlight_dev,
		int timeout)
{
	rt_fled_info_t *info = flashlight_get_data(flashlight_dev);
	int sel;

	return info->hal->fled_set_strobe_timeout(info, timeout,
			timeout, &sel);
}

static int rtfled_list_strobe_timeout(struct flashlight_device *flashlight_dev,
		int selector)
{
	rt_fled_info_t *info = flashlight_get_data(flashlight_dev);
	return info->hal->fled_strobe_timeout_list(info, selector);
}

static int rtfled_set_mode(struct flashlight_device *flashlight_dev, int mode)
{
	rt_fled_info_t *info = flashlight_get_data(flashlight_dev);
	return info->hal->fled_set_mode(info, mode);
}

static int rtfled_strobe(struct flashlight_device *flashlight_dev)
{
	rt_fled_info_t *info = flashlight_get_data(flashlight_dev);
	return info->hal->fled_strobe(info);
}

static int rtfled_set_color_temperature(struct flashlight_device
		*flashlight_dev,
		int color_temp)
{
	/* Doesn't support color temperature */
	return -EINVAL;
}

static int rtfled_list_color_temperature(struct flashlight_device
		*flashlight_dev,
		int selector)
{
	/* Doesn't support color temperature */
	return -EINVAL;
}
static int rtfled_suspend(struct flashlight_device *flashlight_dev,
		pm_message_t state)
{
	rt_fled_info_t *info = flashlight_get_data(flashlight_dev);
	if (info->hal->fled_suspend)
		return info->hal->fled_suspend(info, state);
	return 0;
}
static int rtfled_resume(struct flashlight_device *flashlight_dev)
{
	rt_fled_info_t *info = flashlight_get_data(flashlight_dev);
	if (info->hal->fled_resume)
		return info->hal->fled_resume(info);
	return 0;
}

static struct flashlight_ops rtfled_impl_ops = {
	.set_torch_brightness = rtfled_set_torch_brightness,
	.set_strobe_brightness = rtfled_set_strobe_brightness,
	.set_strobe_timeout = rtfled_set_strobe_timeout,
	.list_strobe_timeout = rtfled_list_strobe_timeout,
	.set_mode = rtfled_set_mode,
	.strobe = rtfled_strobe,
	.set_color_temperature = rtfled_set_color_temperature,
	.list_color_temperature = rtfled_list_color_temperature,
	.suspend = rtfled_suspend,
	.resume = rtfled_resume,
};


static void rfled_shutdown(struct platform_device *pdev)
{
	struct rt_fled_info *info = platform_get_drvdata(pdev);
	if (info->hal->fled_shutdown)
		info->hal->fled_shutdown(info);
}

static int rtled_impl_set_torch_current(struct rt_fled_info *info,
		int min_uA, int max_uA, int *selector)
{
	int sel = 0;
	int rc;
	for (sel = 0; ; sel++) {
		rc = info->hal->fled_troch_current_list(info, sel);
		if (rc < 0)
			return rc;
		if (rc >= min_uA && rc <= max_uA) {
			*selector = sel;
			return info->hal->fled_set_torch_current_sel(info, sel);
		}
	}
	return -EINVAL;
}

static int rtled_impl_set_strobe_current(struct rt_fled_info *info,
		int min_uA, int max_uA, int *selector)
{
	int sel = 0;
	int rc;
	for (sel = 0; ; sel++) {
		rc = info->hal->fled_strobe_current_list(info, sel);
		if (rc < 0)
			return rc;
		if (rc >= min_uA && rc <= max_uA) {
			*selector = sel;
			return info->hal->fled_set_strobe_current_sel(info, sel);
		}
	}
	return -EINVAL;
}

static int rtled_impl_set_timeout_level(struct rt_fled_info *info,
		int min_uA, int max_uA, int *selector)
{
	int sel = 0;
	int rc;
	for (sel = 0; ; sel++) {
		rc = info->hal->fled_timeout_level_list(info, sel);
		if (rc < 0)
			return rc;
		if (rc >= min_uA && rc <= max_uA) {
			*selector = sel;
			return info->hal->fled_set_timeout_level_sel(info, sel);
		}
	}
	return -EINVAL;
}

static int rtled_impl_set_lv_protection(struct rt_fled_info *info,
		int min_mV, int max_mV, int *selector)
{
	int sel = 0;
	int rc;
	for (sel = 0; ; sel++) {
		rc = info->hal->fled_lv_protection_list(info, sel);
		if (rc < 0)
			return rc;
		if (rc >= min_mV && rc <= max_mV) {
			*selector = sel;
			return info->hal->fled_set_lv_protection_sel(info, sel);
		}
	}
	return -EINVAL;
}

static int rtled_impl_set_strobe_timeout(struct rt_fled_info *info,
		int min_ms, int max_ms, int *selector)
{
	int sel = 0;
	int rc;
	for (sel = 0; ; sel++) {
		rc = info->hal->fled_strobe_timeout_list(info, sel);
		if (rc < 0)
			return rc;
		if (rc >= min_ms && rc <= max_ms) {
			*selector = sel;
			return info->hal->fled_set_strobe_timeout_sel(info, sel);
		}
	}
	return -EINVAL;
}

static int rtled_impl_get_torch_current(struct rt_fled_info *info)
{
	int sel = info->hal->fled_get_torch_current_sel(info);
	if (sel < 0)
		return sel;
	return info->hal->fled_troch_current_list(info, sel);
}

static int rtled_impl_get_strobe_current(struct rt_fled_info *info)
{
	int sel = info->hal->fled_get_strobe_current_sel(info);
	if (sel < 0)
		return sel;
	return info->hal->fled_strobe_current_list(info, sel);
}

static int rtled_impl_get_timeout_level(struct rt_fled_info *info)
{
	int sel = info->hal->fled_get_timeout_level_sel(info);
	if (sel < 0)
		return sel;
	return info->hal->fled_timeout_level_list(info, sel);
}

static int rtled_impl_get_lv_protection(struct rt_fled_info *info)
{
	int sel = info->hal->fled_get_lv_protection_sel(info);
	if (sel < 0)
		return sel;
	return info->hal->fled_lv_protection_list(info, sel);
}

static int rtled_impl_get_strobe_timeout(struct rt_fled_info *info)
{
	int sel = info->hal->fled_get_strobe_timeout_sel(info);
	if (sel < 0)
		return sel;
	return info->hal->fled_strobe_timeout_list(info, sel);
}

#define HAL_NOT_IMPLEMENTED(x) (hal->x == NULL)
#define CHECK_HAL_IMPLEMENTED(x) if (hal->x == NULL) return -EINVAL

static int rtfled_check_hal_implement(struct rt_fled_hal *hal)
{
	if (HAL_NOT_IMPLEMENTED(fled_set_torch_current))
		hal->fled_set_torch_current = rtled_impl_set_torch_current;
	if (HAL_NOT_IMPLEMENTED(fled_set_strobe_current))
		hal->fled_set_strobe_current = rtled_impl_set_strobe_current;
	if (HAL_NOT_IMPLEMENTED(fled_set_timeout_level))
		hal->fled_set_timeout_level = rtled_impl_set_timeout_level;
	if (HAL_NOT_IMPLEMENTED(fled_set_lv_protection))
		hal->fled_set_lv_protection = rtled_impl_set_lv_protection;
	if (HAL_NOT_IMPLEMENTED(fled_set_strobe_timeout))
		hal->fled_set_strobe_timeout = rtled_impl_set_strobe_timeout;
	if (HAL_NOT_IMPLEMENTED(fled_get_torch_current))
		hal->fled_get_torch_current = rtled_impl_get_torch_current;
	if (HAL_NOT_IMPLEMENTED(fled_get_strobe_current))
		hal->fled_get_strobe_current = rtled_impl_get_strobe_current;
	if (HAL_NOT_IMPLEMENTED(fled_get_timeout_level))
		hal->fled_get_timeout_level = rtled_impl_get_timeout_level;
	if (HAL_NOT_IMPLEMENTED(fled_get_lv_protection))
		hal->fled_get_lv_protection = rtled_impl_get_lv_protection;
	if (HAL_NOT_IMPLEMENTED(fled_get_strobe_timeout))
		hal->fled_get_strobe_timeout = rtled_impl_get_strobe_timeout;
	CHECK_HAL_IMPLEMENTED(fled_set_mode);
	CHECK_HAL_IMPLEMENTED(fled_get_mode);
	CHECK_HAL_IMPLEMENTED(fled_strobe);
	CHECK_HAL_IMPLEMENTED(fled_troch_current_list);
	CHECK_HAL_IMPLEMENTED(fled_strobe_current_list);
	CHECK_HAL_IMPLEMENTED(fled_timeout_level_list);
	CHECK_HAL_IMPLEMENTED(fled_lv_protection_list);
	CHECK_HAL_IMPLEMENTED(fled_strobe_timeout_list);
	CHECK_HAL_IMPLEMENTED(fled_set_torch_current_sel);
	CHECK_HAL_IMPLEMENTED(fled_set_strobe_current_sel);
	CHECK_HAL_IMPLEMENTED(fled_set_timeout_level_sel);
	CHECK_HAL_IMPLEMENTED(fled_set_lv_protection_sel);
	CHECK_HAL_IMPLEMENTED(fled_set_strobe_timeout_sel);
	CHECK_HAL_IMPLEMENTED(fled_get_torch_current_sel);
	CHECK_HAL_IMPLEMENTED(fled_get_strobe_current_sel);
	CHECK_HAL_IMPLEMENTED(fled_get_timeout_level_sel);
	CHECK_HAL_IMPLEMENTED(fled_get_lv_protection_sel);
	CHECK_HAL_IMPLEMENTED(fled_get_strobe_timeout_sel);
	return 0;
}


static int rtfled_probe(struct platform_device *pdev)
{
	rt_fled_info_t *info = dev_get_drvdata(pdev->dev.parent);
	int rc;
	BUG_ON(info == NULL);
	BUG_ON(info->hal == NULL);

	RTFLED_INFO("Richtek FlashLED Driver is probing\n");
	rc = rtfled_check_hal_implement(info->hal);
	if (rc < 0) {
		RTFLED_ERR("HAL implemented incompletely\n");
		goto err_check_hal;
	}
	platform_set_drvdata(pdev, info);
	info->flashlight_dev = flashlight_device_register(
			info->name ? info->name : RT_FLED_DEVICE,
			&pdev->dev, info, &rtfled_impl_ops,
			info->init_props);
	if (info->hal->fled_init) {
		rc = info->hal->fled_init(info);
		if (rc < 0) {
			RTFLED_ERR("Initialization failed\n");
			goto err_init;
		}
	}
	RTFLED_INFO("Richtek FlashLED Driver initialized successfully\n");
	return 0;
err_init:
	flashlight_device_unregister(info->flashlight_dev);
err_check_hal:
	return rc;
}

static int rtfled_remove(struct platform_device *pdev)
{
	rt_fled_info_t *info = platform_get_drvdata(pdev);
	platform_set_drvdata(pdev, NULL);
	flashlight_device_unregister(info->flashlight_dev);
	return 0;
}

static struct platform_driver rt_flash_led_driver = {
	.driver		= {
		.name	= RT_FLED_DEVICE,
		.owner	= THIS_MODULE,
	},
	.shutdown   = rfled_shutdown,
	.probe		= rtfled_probe,
	.remove		= rtfled_remove,
};

static int __init rtfled_init(void)
{
	return platform_driver_register(&rt_flash_led_driver);
}
subsys_initcall(rtfled_init);

static void __exit rtfled_exit(void)
{
	platform_driver_unregister(&rt_flash_led_driver);
}
module_exit(rtfled_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Patrick Chang <patrick_chang@richtek.com");
MODULE_VERSION("1.0.0_G");
MODULE_DESCRIPTION("Richtek Flash LED Driver");