summaryrefslogtreecommitdiff
path: root/drivers/leds/flash/leds-lm3601x.c
blob: 37e1d6e6868777517054f0e778961a4ddee7ecfb (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
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
484
// SPDX-License-Identifier: GPL-2.0
// Flash and torch driver for Texas Instruments LM3601X LED
// Flash driver chip family
// Copyright (C) 2018 Texas Instruments Incorporated - https://www.ti.com/

#include <linux/delay.h>
#include <linux/i2c.h>
#include <linux/leds.h>
#include <linux/led-class-flash.h>
#include <linux/module.h>
#include <linux/regmap.h>
#include <linux/slab.h>

#define LM3601X_LED_IR		0x0
#define LM3601X_LED_TORCH	0x1

/* Registers */
#define LM3601X_ENABLE_REG	0x01
#define LM3601X_CFG_REG		0x02
#define LM3601X_LED_FLASH_REG	0x03
#define LM3601X_LED_TORCH_REG	0x04
#define LM3601X_FLAGS_REG	0x05
#define LM3601X_DEV_ID_REG	0x06

#define LM3601X_SW_RESET	BIT(7)

/* Enable Mode bits */
#define LM3601X_MODE_STANDBY	0x00
#define LM3601X_MODE_IR_DRV	BIT(0)
#define LM3601X_MODE_TORCH	BIT(1)
#define LM3601X_MODE_STROBE	(BIT(0) | BIT(1))
#define LM3601X_STRB_EN		BIT(2)
#define LM3601X_STRB_EDGE_TRIG	BIT(3)
#define LM3601X_IVFM_EN		BIT(4)

#define LM36010_BOOST_LIMIT_28	BIT(5)
#define LM36010_BOOST_FREQ_4MHZ	BIT(6)
#define LM36010_BOOST_MODE_PASS	BIT(7)

/* Flag Mask */
#define LM3601X_FLASH_TIME_OUT	BIT(0)
#define LM3601X_UVLO_FAULT	BIT(1)
#define LM3601X_THERM_SHUTDOWN	BIT(2)
#define LM3601X_THERM_CURR	BIT(3)
#define LM36010_CURR_LIMIT	BIT(4)
#define LM3601X_SHORT_FAULT	BIT(5)
#define LM3601X_IVFM_TRIP	BIT(6)
#define LM36010_OVP_FAULT	BIT(7)

#define LM3601X_MAX_TORCH_I_UA	376000
#define LM3601X_MIN_TORCH_I_UA	2400
#define LM3601X_TORCH_REG_DIV	2965

#define LM3601X_MAX_STROBE_I_UA	1500000
#define LM3601X_MIN_STROBE_I_UA	11000
#define LM3601X_STROBE_REG_DIV	11800

#define LM3601X_TIMEOUT_MASK	0x1e
#define LM3601X_ENABLE_MASK	(LM3601X_MODE_IR_DRV | LM3601X_MODE_TORCH)

#define LM3601X_LOWER_STEP_US	40000
#define LM3601X_UPPER_STEP_US	200000
#define LM3601X_MIN_TIMEOUT_US	40000
#define LM3601X_MAX_TIMEOUT_US	1600000
#define LM3601X_TIMEOUT_XOVER_US 400000

enum lm3601x_type {
	CHIP_LM36010 = 0,
	CHIP_LM36011,
};

/**
 * struct lm3601x_led -
 * @fled_cdev: flash LED class device pointer
 * @client: Pointer to the I2C client
 * @regmap: Devices register map
 * @lock: Lock for reading/writing the device
 * @led_name: LED label for the Torch or IR LED
 * @flash_timeout: the timeout for the flash
 * @last_flag: last known flags register value
 * @torch_current_max: maximum current for the torch
 * @flash_current_max: maximum current for the flash
 * @max_flash_timeout: maximum timeout for the flash
 * @led_mode: The mode to enable either IR or Torch
 */
struct lm3601x_led {
	struct led_classdev_flash fled_cdev;
	struct i2c_client *client;
	struct regmap *regmap;
	struct mutex lock;

	unsigned int flash_timeout;
	unsigned int last_flag;

	u32 torch_current_max;
	u32 flash_current_max;
	u32 max_flash_timeout;

	u32 led_mode;
};

static const struct reg_default lm3601x_regmap_defs[] = {
	{ LM3601X_ENABLE_REG, 0x20 },
	{ LM3601X_CFG_REG, 0x15 },
	{ LM3601X_LED_FLASH_REG, 0x00 },
	{ LM3601X_LED_TORCH_REG, 0x00 },
};

static bool lm3601x_volatile_reg(struct device *dev, unsigned int reg)
{
	switch (reg) {
	case LM3601X_FLAGS_REG:
		return true;
	default:
		return false;
	}
}

static const struct regmap_config lm3601x_regmap = {
	.reg_bits = 8,
	.val_bits = 8,

	.max_register = LM3601X_DEV_ID_REG,
	.reg_defaults = lm3601x_regmap_defs,
	.num_reg_defaults = ARRAY_SIZE(lm3601x_regmap_defs),
	.cache_type = REGCACHE_RBTREE,
	.volatile_reg = lm3601x_volatile_reg,
};

static struct lm3601x_led *fled_cdev_to_led(struct led_classdev_flash *fled_cdev)
{
	return container_of(fled_cdev, struct lm3601x_led, fled_cdev);
}

static int lm3601x_read_faults(struct lm3601x_led *led)
{
	int flags_val;
	int ret;

	ret = regmap_read(led->regmap, LM3601X_FLAGS_REG, &flags_val);
	if (ret < 0)
		return -EIO;

	led->last_flag = 0;

	if (flags_val & LM36010_OVP_FAULT)
		led->last_flag |= LED_FAULT_OVER_VOLTAGE;

	if (flags_val & (LM3601X_THERM_SHUTDOWN | LM3601X_THERM_CURR))
		led->last_flag |= LED_FAULT_OVER_TEMPERATURE;

	if (flags_val & LM3601X_SHORT_FAULT)
		led->last_flag |= LED_FAULT_SHORT_CIRCUIT;

	if (flags_val & LM36010_CURR_LIMIT)
		led->last_flag |= LED_FAULT_OVER_CURRENT;

	if (flags_val & LM3601X_UVLO_FAULT)
		led->last_flag |= LED_FAULT_UNDER_VOLTAGE;

	if (flags_val & LM3601X_IVFM_TRIP)
		led->last_flag |= LED_FAULT_INPUT_VOLTAGE;

	if (flags_val & LM3601X_THERM_SHUTDOWN)
		led->last_flag |= LED_FAULT_LED_OVER_TEMPERATURE;

	return led->last_flag;
}

static int lm3601x_brightness_set(struct led_classdev *cdev,
					enum led_brightness brightness)
{
	struct led_classdev_flash *fled_cdev = lcdev_to_flcdev(cdev);
	struct lm3601x_led *led = fled_cdev_to_led(fled_cdev);
	int ret, led_mode_val;

	mutex_lock(&led->lock);

	ret = lm3601x_read_faults(led);
	if (ret < 0)
		goto out;

	if (led->led_mode == LM3601X_LED_TORCH)
		led_mode_val = LM3601X_MODE_TORCH;
	else
		led_mode_val = LM3601X_MODE_IR_DRV;

	if (brightness == LED_OFF) {
		ret = regmap_update_bits(led->regmap, LM3601X_ENABLE_REG,
					led_mode_val, LED_OFF);
		goto out;
	}

	ret = regmap_write(led->regmap, LM3601X_LED_TORCH_REG, brightness);
	if (ret < 0)
		goto out;

	ret = regmap_update_bits(led->regmap, LM3601X_ENABLE_REG,
				LM3601X_MODE_TORCH | LM3601X_MODE_IR_DRV,
				led_mode_val);
out:
	mutex_unlock(&led->lock);
	return ret;
}

static int lm3601x_strobe_set(struct led_classdev_flash *fled_cdev,
				bool state)
{
	struct lm3601x_led *led = fled_cdev_to_led(fled_cdev);
	int timeout_reg_val;
	int current_timeout;
	int ret;

	mutex_lock(&led->lock);

	ret = regmap_read(led->regmap, LM3601X_CFG_REG, &current_timeout);
	if (ret < 0)
		goto out;

	if (led->flash_timeout >= LM3601X_TIMEOUT_XOVER_US)
		timeout_reg_val = led->flash_timeout / LM3601X_UPPER_STEP_US + 0x07;
	else
		timeout_reg_val = led->flash_timeout / LM3601X_LOWER_STEP_US - 0x01;

	if (led->flash_timeout != current_timeout)
		ret = regmap_update_bits(led->regmap, LM3601X_CFG_REG,
					LM3601X_TIMEOUT_MASK, timeout_reg_val);

	if (state)
		ret = regmap_update_bits(led->regmap, LM3601X_ENABLE_REG,
					LM3601X_MODE_TORCH | LM3601X_MODE_IR_DRV,
					LM3601X_MODE_STROBE);
	else
		ret = regmap_update_bits(led->regmap, LM3601X_ENABLE_REG,
					LM3601X_MODE_STROBE, LED_OFF);

	ret = lm3601x_read_faults(led);
out:
	mutex_unlock(&led->lock);
	return ret;
}

static int lm3601x_flash_brightness_set(struct led_classdev_flash *fled_cdev,
					u32 brightness)
{
	struct lm3601x_led *led = fled_cdev_to_led(fled_cdev);
	u8 brightness_val;
	int ret;

	mutex_lock(&led->lock);
	ret = lm3601x_read_faults(led);
	if (ret < 0)
		goto out;

	if (brightness == LED_OFF) {
		ret = regmap_update_bits(led->regmap, LM3601X_ENABLE_REG,
					LM3601X_MODE_STROBE, LED_OFF);
		goto out;
	}

	brightness_val = brightness / LM3601X_STROBE_REG_DIV;

	ret = regmap_write(led->regmap, LM3601X_LED_FLASH_REG, brightness_val);
out:
	mutex_unlock(&led->lock);
	return ret;
}

static int lm3601x_flash_timeout_set(struct led_classdev_flash *fled_cdev,
				u32 timeout)
{
	struct lm3601x_led *led = fled_cdev_to_led(fled_cdev);

	mutex_lock(&led->lock);

	led->flash_timeout = timeout;

	mutex_unlock(&led->lock);

	return 0;
}

static int lm3601x_strobe_get(struct led_classdev_flash *fled_cdev, bool *state)
{
	struct lm3601x_led *led = fled_cdev_to_led(fled_cdev);
	int strobe_state;
	int ret;

	mutex_lock(&led->lock);

	ret = regmap_read(led->regmap, LM3601X_ENABLE_REG, &strobe_state);
	if (ret < 0)
		goto out;

	*state = strobe_state & LM3601X_MODE_STROBE;

out:
	mutex_unlock(&led->lock);
	return ret;
}

static int lm3601x_flash_fault_get(struct led_classdev_flash *fled_cdev,
				u32 *fault)
{
	struct lm3601x_led *led = fled_cdev_to_led(fled_cdev);

	lm3601x_read_faults(led);

	*fault = led->last_flag;

	return 0;
}

static const struct led_flash_ops flash_ops = {
	.flash_brightness_set	= lm3601x_flash_brightness_set,
	.strobe_set		= lm3601x_strobe_set,
	.strobe_get		= lm3601x_strobe_get,
	.timeout_set		= lm3601x_flash_timeout_set,
	.fault_get		= lm3601x_flash_fault_get,
};

static int lm3601x_register_leds(struct lm3601x_led *led,
				 struct fwnode_handle *fwnode)
{
	struct led_classdev *led_cdev;
	struct led_flash_setting *setting;
	struct led_init_data init_data = {};

	led->fled_cdev.ops = &flash_ops;

	setting = &led->fled_cdev.timeout;
	setting->min = LM3601X_MIN_TIMEOUT_US;
	setting->max = led->max_flash_timeout;
	setting->step = LM3601X_LOWER_STEP_US;
	setting->val = led->max_flash_timeout;

	setting = &led->fled_cdev.brightness;
	setting->min = LM3601X_MIN_STROBE_I_UA;
	setting->max = led->flash_current_max;
	setting->step = LM3601X_TORCH_REG_DIV;
	setting->val = led->flash_current_max;

	led_cdev = &led->fled_cdev.led_cdev;
	led_cdev->brightness_set_blocking = lm3601x_brightness_set;
	led_cdev->max_brightness = DIV_ROUND_UP(led->torch_current_max,
						LM3601X_TORCH_REG_DIV);
	led_cdev->flags |= LED_DEV_CAP_FLASH;

	init_data.fwnode = fwnode;
	init_data.devicename = led->client->name;
	init_data.default_label = (led->led_mode == LM3601X_LED_TORCH) ?
					"torch" : "infrared";
	return devm_led_classdev_flash_register_ext(&led->client->dev,
						&led->fled_cdev, &init_data);
}

static int lm3601x_parse_node(struct lm3601x_led *led,
			      struct fwnode_handle **fwnode)
{
	struct fwnode_handle *child = NULL;
	int ret = -ENODEV;

	child = device_get_next_child_node(&led->client->dev, child);
	if (!child) {
		dev_err(&led->client->dev, "No LED Child node\n");
		return ret;
	}

	ret = fwnode_property_read_u32(child, "reg", &led->led_mode);
	if (ret) {
		dev_err(&led->client->dev, "reg DT property missing\n");
		goto out_err;
	}

	if (led->led_mode > LM3601X_LED_TORCH ||
	    led->led_mode < LM3601X_LED_IR) {
		dev_warn(&led->client->dev, "Invalid led mode requested\n");
		ret = -EINVAL;
		goto out_err;
	}

	ret = fwnode_property_read_u32(child, "led-max-microamp",
					&led->torch_current_max);
	if (ret) {
		dev_warn(&led->client->dev,
			"led-max-microamp DT property missing\n");
		goto out_err;
	}

	ret = fwnode_property_read_u32(child, "flash-max-microamp",
				&led->flash_current_max);
	if (ret) {
		dev_warn(&led->client->dev,
			 "flash-max-microamp DT property missing\n");
		goto out_err;
	}

	ret = fwnode_property_read_u32(child, "flash-max-timeout-us",
				&led->max_flash_timeout);
	if (ret) {
		dev_warn(&led->client->dev,
			 "flash-max-timeout-us DT property missing\n");
		goto out_err;
	}

	*fwnode = child;

out_err:
	fwnode_handle_put(child);
	return ret;
}

static int lm3601x_probe(struct i2c_client *client)
{
	struct lm3601x_led *led;
	struct fwnode_handle *fwnode;
	int ret;

	led = devm_kzalloc(&client->dev, sizeof(*led), GFP_KERNEL);
	if (!led)
		return -ENOMEM;

	led->client = client;
	i2c_set_clientdata(client, led);

	ret = lm3601x_parse_node(led, &fwnode);
	if (ret)
		return -ENODEV;

	led->regmap = devm_regmap_init_i2c(client, &lm3601x_regmap);
	if (IS_ERR(led->regmap)) {
		ret = PTR_ERR(led->regmap);
		dev_err(&client->dev,
			"Failed to allocate register map: %d\n", ret);
		return ret;
	}

	mutex_init(&led->lock);

	return lm3601x_register_leds(led, fwnode);
}

static int lm3601x_remove(struct i2c_client *client)
{
	struct lm3601x_led *led = i2c_get_clientdata(client);
	int ret;

	ret = regmap_update_bits(led->regmap, LM3601X_ENABLE_REG,
				 LM3601X_ENABLE_MASK, LM3601X_MODE_STANDBY);
	if (ret)
		dev_warn(&client->dev,
			 "Failed to put into standby (%pe)\n", ERR_PTR(ret));

	return 0;
}

static const struct i2c_device_id lm3601x_id[] = {
	{ "LM36010", CHIP_LM36010 },
	{ "LM36011", CHIP_LM36011 },
	{ }
};
MODULE_DEVICE_TABLE(i2c, lm3601x_id);

static const struct of_device_id of_lm3601x_leds_match[] = {
	{ .compatible = "ti,lm36010", },
	{ .compatible = "ti,lm36011", },
	{ }
};
MODULE_DEVICE_TABLE(of, of_lm3601x_leds_match);

static struct i2c_driver lm3601x_i2c_driver = {
	.driver = {
		.name = "lm3601x",
		.of_match_table = of_lm3601x_leds_match,
	},
	.probe_new = lm3601x_probe,
	.remove = lm3601x_remove,
	.id_table = lm3601x_id,
};
module_i2c_driver(lm3601x_i2c_driver);

MODULE_DESCRIPTION("Texas Instruments Flash Lighting driver for LM3601X");
MODULE_AUTHOR("Dan Murphy <dmurphy@ti.com>");
MODULE_LICENSE("GPL v2");