• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 * Simple driver for Texas Instruments LM355x LED Flash driver chip
3 * Copyright (C) 2012 Texas Instruments
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 */
9 
10 #include <linux/module.h>
11 #include <linux/delay.h>
12 #include <linux/i2c.h>
13 #include <linux/gpio.h>
14 #include <linux/leds.h>
15 #include <linux/slab.h>
16 #include <linux/platform_device.h>
17 #include <linux/fs.h>
18 #include <linux/regmap.h>
19 #include <linux/workqueue.h>
20 #include <linux/platform_data/leds-lm355x.h>
21 
22 enum lm355x_type {
23 	CHIP_LM3554 = 0,
24 	CHIP_LM3556,
25 };
26 
27 enum lm355x_regs {
28 	REG_FLAG = 0,
29 	REG_TORCH_CFG,
30 	REG_TORCH_CTRL,
31 	REG_STROBE_CFG,
32 	REG_FLASH_CTRL,
33 	REG_INDI_CFG,
34 	REG_INDI_CTRL,
35 	REG_OPMODE,
36 	REG_MAX,
37 };
38 
39 /* operation mode */
40 enum lm355x_mode {
41 	MODE_SHDN = 0,
42 	MODE_INDIC,
43 	MODE_TORCH,
44 	MODE_FLASH
45 };
46 
47 /* register map info. */
48 struct lm355x_reg_data {
49 	u8 regno;
50 	u8 mask;
51 	u8 shift;
52 };
53 
54 struct lm355x_chip_data {
55 	struct device *dev;
56 	enum lm355x_type type;
57 
58 	struct led_classdev cdev_flash;
59 	struct led_classdev cdev_torch;
60 	struct led_classdev cdev_indicator;
61 
62 	struct work_struct work_flash;
63 	struct work_struct work_torch;
64 	struct work_struct work_indicator;
65 
66 	u8 br_flash;
67 	u8 br_torch;
68 	u8 br_indicator;
69 
70 	struct lm355x_platform_data *pdata;
71 	struct regmap *regmap;
72 	struct mutex lock;
73 
74 	unsigned int last_flag;
75 	struct lm355x_reg_data *regs;
76 };
77 
78 /* specific indicator function for lm3556 */
79 enum lm3556_indic_pulse_time {
80 	PULSE_TIME_0_MS = 0,
81 	PULSE_TIME_32_MS,
82 	PULSE_TIME_64_MS,
83 	PULSE_TIME_92_MS,
84 	PULSE_TIME_128_MS,
85 	PULSE_TIME_160_MS,
86 	PULSE_TIME_196_MS,
87 	PULSE_TIME_224_MS,
88 	PULSE_TIME_256_MS,
89 	PULSE_TIME_288_MS,
90 	PULSE_TIME_320_MS,
91 	PULSE_TIME_352_MS,
92 	PULSE_TIME_384_MS,
93 	PULSE_TIME_416_MS,
94 	PULSE_TIME_448_MS,
95 	PULSE_TIME_480_MS,
96 };
97 
98 enum lm3556_indic_n_blank {
99 	INDIC_N_BLANK_0 = 0,
100 	INDIC_N_BLANK_1,
101 	INDIC_N_BLANK_2,
102 	INDIC_N_BLANK_3,
103 	INDIC_N_BLANK_4,
104 	INDIC_N_BLANK_5,
105 	INDIC_N_BLANK_6,
106 	INDIC_N_BLANK_7,
107 	INDIC_N_BLANK_8,
108 	INDIC_N_BLANK_9,
109 	INDIC_N_BLANK_10,
110 	INDIC_N_BLANK_11,
111 	INDIC_N_BLANK_12,
112 	INDIC_N_BLANK_13,
113 	INDIC_N_BLANK_14,
114 	INDIC_N_BLANK_15,
115 };
116 
117 enum lm3556_indic_period {
118 	INDIC_PERIOD_0 = 0,
119 	INDIC_PERIOD_1,
120 	INDIC_PERIOD_2,
121 	INDIC_PERIOD_3,
122 	INDIC_PERIOD_4,
123 	INDIC_PERIOD_5,
124 	INDIC_PERIOD_6,
125 	INDIC_PERIOD_7,
126 };
127 
128 #define INDIC_PATTERN_SIZE 4
129 
130 struct indicator {
131 	u8 blinking;
132 	u8 period_cnt;
133 };
134 
135 /* indicator pattern data only for lm3556 */
136 static struct indicator indicator_pattern[INDIC_PATTERN_SIZE] = {
137 	[0] = {(INDIC_N_BLANK_1 << 4) | PULSE_TIME_32_MS, INDIC_PERIOD_1},
138 	[1] = {(INDIC_N_BLANK_15 << 4) | PULSE_TIME_32_MS, INDIC_PERIOD_2},
139 	[2] = {(INDIC_N_BLANK_10 << 4) | PULSE_TIME_32_MS, INDIC_PERIOD_4},
140 	[3] = {(INDIC_N_BLANK_5 << 4) | PULSE_TIME_32_MS, INDIC_PERIOD_7},
141 };
142 
143 static struct lm355x_reg_data lm3554_regs[REG_MAX] = {
144 	[REG_FLAG] = {0xD0, 0xBF, 0},
145 	[REG_TORCH_CFG] = {0xE0, 0x80, 7},
146 	[REG_TORCH_CTRL] = {0xA0, 0x38, 3},
147 	[REG_STROBE_CFG] = {0xE0, 0x04, 2},
148 	[REG_FLASH_CTRL] = {0xB0, 0x78, 3},
149 	[REG_INDI_CFG] = {0xE0, 0x08, 3},
150 	[REG_INDI_CTRL] = {0xA0, 0xC0, 6},
151 	[REG_OPMODE] = {0xA0, 0x03, 0},
152 };
153 
154 static struct lm355x_reg_data lm3556_regs[REG_MAX] = {
155 	[REG_FLAG] = {0x0B, 0xFF, 0},
156 	[REG_TORCH_CFG] = {0x0A, 0x10, 4},
157 	[REG_TORCH_CTRL] = {0x09, 0x70, 4},
158 	[REG_STROBE_CFG] = {0x0A, 0x20, 5},
159 	[REG_FLASH_CTRL] = {0x09, 0x0F, 0},
160 	[REG_INDI_CFG] = {0xFF, 0xFF, 0},
161 	[REG_INDI_CTRL] = {0x09, 0x70, 4},
162 	[REG_OPMODE] = {0x0A, 0x03, 0},
163 };
164 
165 static char lm355x_name[][I2C_NAME_SIZE] = {
166 	[CHIP_LM3554] = LM3554_NAME,
167 	[CHIP_LM3556] = LM3556_NAME,
168 };
169 
170 /* chip initialize */
lm355x_chip_init(struct lm355x_chip_data * chip)171 static int lm355x_chip_init(struct lm355x_chip_data *chip)
172 {
173 	int ret;
174 	unsigned int reg_val;
175 	struct lm355x_platform_data *pdata = chip->pdata;
176 
177 	/* input and output pins configuration */
178 	switch (chip->type) {
179 	case CHIP_LM3554:
180 		reg_val = (u32)pdata->pin_tx2 | (u32)pdata->ntc_pin;
181 		ret = regmap_update_bits(chip->regmap, 0xE0, 0x28, reg_val);
182 		if (ret < 0)
183 			goto out;
184 		reg_val = (u32)pdata->pass_mode;
185 		ret = regmap_update_bits(chip->regmap, 0xA0, 0x04, reg_val);
186 		if (ret < 0)
187 			goto out;
188 		break;
189 
190 	case CHIP_LM3556:
191 		reg_val = (u32)pdata->pin_tx2 | (u32)pdata->ntc_pin |
192 		          (u32)pdata->pass_mode;
193 		ret = regmap_update_bits(chip->regmap, 0x0A, 0xC4, reg_val);
194 		if (ret < 0)
195 			goto out;
196 		break;
197 	default:
198 		return -ENODATA;
199 	}
200 
201 	return ret;
202 out:
203 	dev_err(chip->dev, "%s:i2c access fail to register\n", __func__);
204 	return ret;
205 }
206 
207 /* chip control */
lm355x_control(struct lm355x_chip_data * chip,u8 brightness,enum lm355x_mode opmode)208 static void lm355x_control(struct lm355x_chip_data *chip,
209 			   u8 brightness, enum lm355x_mode opmode)
210 {
211 	int ret;
212 	unsigned int reg_val;
213 	struct lm355x_platform_data *pdata = chip->pdata;
214 	struct lm355x_reg_data *preg = chip->regs;
215 
216 	ret = regmap_read(chip->regmap, preg[REG_FLAG].regno, &chip->last_flag);
217 	if (ret < 0)
218 		goto out;
219 	if (chip->last_flag & preg[REG_FLAG].mask)
220 		dev_info(chip->dev, "%s Last FLAG is 0x%x\n",
221 			 lm355x_name[chip->type],
222 			 chip->last_flag & preg[REG_FLAG].mask);
223 	/* brightness 0 means shutdown */
224 	if (!brightness)
225 		opmode = MODE_SHDN;
226 
227 	switch (opmode) {
228 	case MODE_TORCH:
229 		ret =
230 		    regmap_update_bits(chip->regmap, preg[REG_TORCH_CTRL].regno,
231 				       preg[REG_TORCH_CTRL].mask,
232 				       (brightness - 1)
233 				       << preg[REG_TORCH_CTRL].shift);
234 		if (ret < 0)
235 			goto out;
236 
237 		if (pdata->pin_tx1 != LM355x_PIN_TORCH_DISABLE) {
238 			ret =
239 			    regmap_update_bits(chip->regmap,
240 					       preg[REG_TORCH_CFG].regno,
241 					       preg[REG_TORCH_CFG].mask,
242 					       0x01 <<
243 					       preg[REG_TORCH_CFG].shift);
244 			if (ret < 0)
245 				goto out;
246 			opmode = MODE_SHDN;
247 			dev_info(chip->dev,
248 				 "torch brt is set - ext. torch pin mode\n");
249 		}
250 		break;
251 
252 	case MODE_FLASH:
253 
254 		ret =
255 		    regmap_update_bits(chip->regmap, preg[REG_FLASH_CTRL].regno,
256 				       preg[REG_FLASH_CTRL].mask,
257 				       (brightness - 1)
258 				       << preg[REG_FLASH_CTRL].shift);
259 		if (ret < 0)
260 			goto out;
261 
262 		if (pdata->pin_strobe != LM355x_PIN_STROBE_DISABLE) {
263 			if (chip->type == CHIP_LM3554)
264 				reg_val = 0x00;
265 			else
266 				reg_val = 0x01;
267 			ret =
268 			    regmap_update_bits(chip->regmap,
269 					       preg[REG_STROBE_CFG].regno,
270 					       preg[REG_STROBE_CFG].mask,
271 					       reg_val <<
272 					       preg[REG_STROBE_CFG].shift);
273 			if (ret < 0)
274 				goto out;
275 			opmode = MODE_SHDN;
276 			dev_info(chip->dev,
277 				 "flash brt is set - ext. strobe pin mode\n");
278 		}
279 		break;
280 
281 	case MODE_INDIC:
282 		ret =
283 		    regmap_update_bits(chip->regmap, preg[REG_INDI_CTRL].regno,
284 				       preg[REG_INDI_CTRL].mask,
285 				       (brightness - 1)
286 				       << preg[REG_INDI_CTRL].shift);
287 		if (ret < 0)
288 			goto out;
289 
290 		if (pdata->pin_tx2 != LM355x_PIN_TX_DISABLE) {
291 			ret =
292 			    regmap_update_bits(chip->regmap,
293 					       preg[REG_INDI_CFG].regno,
294 					       preg[REG_INDI_CFG].mask,
295 					       0x01 <<
296 					       preg[REG_INDI_CFG].shift);
297 			if (ret < 0)
298 				goto out;
299 			opmode = MODE_SHDN;
300 		}
301 		break;
302 	case MODE_SHDN:
303 		break;
304 	default:
305 		return;
306 	}
307 	/* operation mode control */
308 	ret = regmap_update_bits(chip->regmap, preg[REG_OPMODE].regno,
309 				 preg[REG_OPMODE].mask,
310 				 opmode << preg[REG_OPMODE].shift);
311 	if (ret < 0)
312 		goto out;
313 	return;
314 out:
315 	dev_err(chip->dev, "%s:i2c access fail to register\n", __func__);
316 	return;
317 }
318 
319 /* torch */
lm355x_deferred_torch_brightness_set(struct work_struct * work)320 static void lm355x_deferred_torch_brightness_set(struct work_struct *work)
321 {
322 	struct lm355x_chip_data *chip =
323 	    container_of(work, struct lm355x_chip_data, work_torch);
324 
325 	mutex_lock(&chip->lock);
326 	lm355x_control(chip, chip->br_torch, MODE_TORCH);
327 	mutex_unlock(&chip->lock);
328 }
329 
lm355x_torch_brightness_set(struct led_classdev * cdev,enum led_brightness brightness)330 static void lm355x_torch_brightness_set(struct led_classdev *cdev,
331 					enum led_brightness brightness)
332 {
333 	struct lm355x_chip_data *chip =
334 	    container_of(cdev, struct lm355x_chip_data, cdev_torch);
335 
336 	chip->br_torch = brightness;
337 	schedule_work(&chip->work_torch);
338 }
339 
340 /* flash */
lm355x_deferred_strobe_brightness_set(struct work_struct * work)341 static void lm355x_deferred_strobe_brightness_set(struct work_struct *work)
342 {
343 	struct lm355x_chip_data *chip =
344 	    container_of(work, struct lm355x_chip_data, work_flash);
345 
346 	mutex_lock(&chip->lock);
347 	lm355x_control(chip, chip->br_flash, MODE_FLASH);
348 	mutex_unlock(&chip->lock);
349 }
350 
lm355x_strobe_brightness_set(struct led_classdev * cdev,enum led_brightness brightness)351 static void lm355x_strobe_brightness_set(struct led_classdev *cdev,
352 					 enum led_brightness brightness)
353 {
354 	struct lm355x_chip_data *chip =
355 	    container_of(cdev, struct lm355x_chip_data, cdev_flash);
356 
357 	chip->br_flash = brightness;
358 	schedule_work(&chip->work_flash);
359 }
360 
361 /* indicator */
lm355x_deferred_indicator_brightness_set(struct work_struct * work)362 static void lm355x_deferred_indicator_brightness_set(struct work_struct *work)
363 {
364 	struct lm355x_chip_data *chip =
365 	    container_of(work, struct lm355x_chip_data, work_indicator);
366 
367 	mutex_lock(&chip->lock);
368 	lm355x_control(chip, chip->br_indicator, MODE_INDIC);
369 	mutex_unlock(&chip->lock);
370 }
371 
lm355x_indicator_brightness_set(struct led_classdev * cdev,enum led_brightness brightness)372 static void lm355x_indicator_brightness_set(struct led_classdev *cdev,
373 					    enum led_brightness brightness)
374 {
375 	struct lm355x_chip_data *chip =
376 	    container_of(cdev, struct lm355x_chip_data, cdev_indicator);
377 
378 	chip->br_indicator = brightness;
379 	schedule_work(&chip->work_indicator);
380 }
381 
382 /* indicator pattern only for lm3556*/
lm3556_indicator_pattern_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t size)383 static ssize_t lm3556_indicator_pattern_store(struct device *dev,
384 					      struct device_attribute *attr,
385 					      const char *buf, size_t size)
386 {
387 	ssize_t ret;
388 	struct led_classdev *led_cdev = dev_get_drvdata(dev);
389 	struct lm355x_chip_data *chip =
390 	    container_of(led_cdev, struct lm355x_chip_data, cdev_indicator);
391 	unsigned int state;
392 
393 	ret = kstrtouint(buf, 10, &state);
394 	if (ret)
395 		goto out;
396 	if (state > INDIC_PATTERN_SIZE - 1)
397 		state = INDIC_PATTERN_SIZE - 1;
398 
399 	ret = regmap_write(chip->regmap, 0x04,
400 			   indicator_pattern[state].blinking);
401 	if (ret < 0)
402 		goto out;
403 
404 	ret = regmap_write(chip->regmap, 0x05,
405 			   indicator_pattern[state].period_cnt);
406 	if (ret < 0)
407 		goto out;
408 
409 	return size;
410 out:
411 	dev_err(chip->dev, "%s:i2c access fail to register\n", __func__);
412 	return ret;
413 }
414 
415 static DEVICE_ATTR(pattern, S_IWUSR, NULL, lm3556_indicator_pattern_store);
416 
417 static struct attribute *lm355x_indicator_attrs[] = {
418 	&dev_attr_pattern.attr,
419 	NULL
420 };
421 ATTRIBUTE_GROUPS(lm355x_indicator);
422 
423 static const struct regmap_config lm355x_regmap = {
424 	.reg_bits = 8,
425 	.val_bits = 8,
426 	.max_register = 0xFF,
427 };
428 
429 /* module initialize */
lm355x_probe(struct i2c_client * client,const struct i2c_device_id * id)430 static int lm355x_probe(struct i2c_client *client,
431 				  const struct i2c_device_id *id)
432 {
433 	struct lm355x_platform_data *pdata = dev_get_platdata(&client->dev);
434 	struct lm355x_chip_data *chip;
435 
436 	int err;
437 
438 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
439 		dev_err(&client->dev, "i2c functionality check fail.\n");
440 		return -EOPNOTSUPP;
441 	}
442 
443 	if (pdata == NULL) {
444 		dev_err(&client->dev, "needs Platform Data.\n");
445 		return -ENODATA;
446 	}
447 
448 	chip = devm_kzalloc(&client->dev,
449 			    sizeof(struct lm355x_chip_data), GFP_KERNEL);
450 	if (!chip)
451 		return -ENOMEM;
452 
453 	chip->dev = &client->dev;
454 	chip->type = id->driver_data;
455 	switch (id->driver_data) {
456 	case CHIP_LM3554:
457 		chip->regs = lm3554_regs;
458 		break;
459 	case CHIP_LM3556:
460 		chip->regs = lm3556_regs;
461 		break;
462 	default:
463 		return -ENOSYS;
464 	}
465 	chip->pdata = pdata;
466 
467 	chip->regmap = devm_regmap_init_i2c(client, &lm355x_regmap);
468 	if (IS_ERR(chip->regmap)) {
469 		err = PTR_ERR(chip->regmap);
470 		dev_err(&client->dev,
471 			"Failed to allocate register map: %d\n", err);
472 		return err;
473 	}
474 
475 	mutex_init(&chip->lock);
476 	i2c_set_clientdata(client, chip);
477 
478 	err = lm355x_chip_init(chip);
479 	if (err < 0)
480 		goto err_out;
481 
482 	/* flash */
483 	INIT_WORK(&chip->work_flash, lm355x_deferred_strobe_brightness_set);
484 	chip->cdev_flash.name = "flash";
485 	chip->cdev_flash.max_brightness = 16;
486 	chip->cdev_flash.brightness_set = lm355x_strobe_brightness_set;
487 	chip->cdev_flash.default_trigger = "flash";
488 	err = led_classdev_register((struct device *)
489 				    &client->dev, &chip->cdev_flash);
490 	if (err < 0)
491 		goto err_out;
492 	/* torch */
493 	INIT_WORK(&chip->work_torch, lm355x_deferred_torch_brightness_set);
494 	chip->cdev_torch.name = "torch";
495 	chip->cdev_torch.max_brightness = 8;
496 	chip->cdev_torch.brightness_set = lm355x_torch_brightness_set;
497 	chip->cdev_torch.default_trigger = "torch";
498 	err = led_classdev_register((struct device *)
499 				    &client->dev, &chip->cdev_torch);
500 	if (err < 0)
501 		goto err_create_torch_file;
502 	/* indicator */
503 	INIT_WORK(&chip->work_indicator,
504 		  lm355x_deferred_indicator_brightness_set);
505 	chip->cdev_indicator.name = "indicator";
506 	if (id->driver_data == CHIP_LM3554)
507 		chip->cdev_indicator.max_brightness = 4;
508 	else
509 		chip->cdev_indicator.max_brightness = 8;
510 	chip->cdev_indicator.brightness_set = lm355x_indicator_brightness_set;
511 	/* indicator pattern control only for LM3556 */
512 	if (id->driver_data == CHIP_LM3556)
513 		chip->cdev_indicator.groups = lm355x_indicator_groups;
514 	err = led_classdev_register((struct device *)
515 				    &client->dev, &chip->cdev_indicator);
516 	if (err < 0)
517 		goto err_create_indicator_file;
518 
519 	dev_info(&client->dev, "%s is initialized\n",
520 		 lm355x_name[id->driver_data]);
521 	return 0;
522 
523 err_create_indicator_file:
524 	led_classdev_unregister(&chip->cdev_torch);
525 err_create_torch_file:
526 	led_classdev_unregister(&chip->cdev_flash);
527 err_out:
528 	return err;
529 }
530 
lm355x_remove(struct i2c_client * client)531 static int lm355x_remove(struct i2c_client *client)
532 {
533 	struct lm355x_chip_data *chip = i2c_get_clientdata(client);
534 	struct lm355x_reg_data *preg = chip->regs;
535 
536 	regmap_write(chip->regmap, preg[REG_OPMODE].regno, 0);
537 	led_classdev_unregister(&chip->cdev_indicator);
538 	flush_work(&chip->work_indicator);
539 	led_classdev_unregister(&chip->cdev_torch);
540 	flush_work(&chip->work_torch);
541 	led_classdev_unregister(&chip->cdev_flash);
542 	flush_work(&chip->work_flash);
543 	dev_info(&client->dev, "%s is removed\n", lm355x_name[chip->type]);
544 
545 	return 0;
546 }
547 
548 static const struct i2c_device_id lm355x_id[] = {
549 	{LM3554_NAME, CHIP_LM3554},
550 	{LM3556_NAME, CHIP_LM3556},
551 	{}
552 };
553 
554 MODULE_DEVICE_TABLE(i2c, lm355x_id);
555 
556 static struct i2c_driver lm355x_i2c_driver = {
557 	.driver = {
558 		   .name = LM355x_NAME,
559 		   .pm = NULL,
560 		   },
561 	.probe = lm355x_probe,
562 	.remove = lm355x_remove,
563 	.id_table = lm355x_id,
564 };
565 
566 module_i2c_driver(lm355x_i2c_driver);
567 
568 MODULE_DESCRIPTION("Texas Instruments Flash Lighting driver for LM355x");
569 MODULE_AUTHOR("Daniel Jeong <daniel.jeong@ti.com>");
570 MODULE_AUTHOR("G.Shark Jeong <gshark.jeong@gmail.com>");
571 MODULE_LICENSE("GPL v2");
572