1 // SPDX-License-Identifier: GPL-2.0-only
3 * Simple driver for Texas Instruments LM355x LED Flash driver chip
4 * Copyright (C) 2012 Texas Instruments
7 #include <linux/module.h>
8 #include <linux/delay.h>
10 #include <linux/gpio.h>
11 #include <linux/leds.h>
12 #include <linux/slab.h>
13 #include <linux/platform_device.h>
15 #include <linux/regmap.h>
16 #include <linux/platform_data/leds-lm355x.h>
43 /* register map info. */
44 struct lm355x_reg_data
{
50 struct lm355x_chip_data
{
52 enum lm355x_type type
;
54 struct led_classdev cdev_flash
;
55 struct led_classdev cdev_torch
;
56 struct led_classdev cdev_indicator
;
58 struct lm355x_platform_data
*pdata
;
59 struct regmap
*regmap
;
62 unsigned int last_flag
;
63 struct lm355x_reg_data
*regs
;
66 /* specific indicator function for lm3556 */
67 enum lm3556_indic_pulse_time
{
86 enum lm3556_indic_n_blank
{
105 enum lm3556_indic_period
{
116 #define INDIC_PATTERN_SIZE 4
123 /* indicator pattern data only for lm3556 */
124 static struct indicator indicator_pattern
[INDIC_PATTERN_SIZE
] = {
125 [0] = {(INDIC_N_BLANK_1
<< 4) | PULSE_TIME_32_MS
, INDIC_PERIOD_1
},
126 [1] = {(INDIC_N_BLANK_15
<< 4) | PULSE_TIME_32_MS
, INDIC_PERIOD_2
},
127 [2] = {(INDIC_N_BLANK_10
<< 4) | PULSE_TIME_32_MS
, INDIC_PERIOD_4
},
128 [3] = {(INDIC_N_BLANK_5
<< 4) | PULSE_TIME_32_MS
, INDIC_PERIOD_7
},
131 static struct lm355x_reg_data lm3554_regs
[REG_MAX
] = {
132 [REG_FLAG
] = {0xD0, 0xBF, 0},
133 [REG_TORCH_CFG
] = {0xE0, 0x80, 7},
134 [REG_TORCH_CTRL
] = {0xA0, 0x38, 3},
135 [REG_STROBE_CFG
] = {0xE0, 0x04, 2},
136 [REG_FLASH_CTRL
] = {0xB0, 0x78, 3},
137 [REG_INDI_CFG
] = {0xE0, 0x08, 3},
138 [REG_INDI_CTRL
] = {0xA0, 0xC0, 6},
139 [REG_OPMODE
] = {0xA0, 0x03, 0},
142 static struct lm355x_reg_data lm3556_regs
[REG_MAX
] = {
143 [REG_FLAG
] = {0x0B, 0xFF, 0},
144 [REG_TORCH_CFG
] = {0x0A, 0x10, 4},
145 [REG_TORCH_CTRL
] = {0x09, 0x70, 4},
146 [REG_STROBE_CFG
] = {0x0A, 0x20, 5},
147 [REG_FLASH_CTRL
] = {0x09, 0x0F, 0},
148 [REG_INDI_CFG
] = {0xFF, 0xFF, 0},
149 [REG_INDI_CTRL
] = {0x09, 0x70, 4},
150 [REG_OPMODE
] = {0x0A, 0x03, 0},
153 static char lm355x_name
[][I2C_NAME_SIZE
] = {
154 [CHIP_LM3554
] = LM3554_NAME
,
155 [CHIP_LM3556
] = LM3556_NAME
,
158 /* chip initialize */
159 static int lm355x_chip_init(struct lm355x_chip_data
*chip
)
162 unsigned int reg_val
;
163 struct lm355x_platform_data
*pdata
= chip
->pdata
;
165 /* input and output pins configuration */
166 switch (chip
->type
) {
168 reg_val
= pdata
->pin_tx2
| pdata
->ntc_pin
;
169 ret
= regmap_update_bits(chip
->regmap
, 0xE0, 0x28, reg_val
);
172 reg_val
= pdata
->pass_mode
;
173 ret
= regmap_update_bits(chip
->regmap
, 0xA0, 0x04, reg_val
);
179 reg_val
= pdata
->pin_tx2
| pdata
->ntc_pin
| pdata
->pass_mode
;
180 ret
= regmap_update_bits(chip
->regmap
, 0x0A, 0xC4, reg_val
);
190 dev_err(chip
->dev
, "%s:i2c access fail to register\n", __func__
);
195 static int lm355x_control(struct lm355x_chip_data
*chip
,
196 u8 brightness
, enum lm355x_mode opmode
)
199 unsigned int reg_val
;
200 struct lm355x_platform_data
*pdata
= chip
->pdata
;
201 struct lm355x_reg_data
*preg
= chip
->regs
;
203 ret
= regmap_read(chip
->regmap
, preg
[REG_FLAG
].regno
, &chip
->last_flag
);
206 if (chip
->last_flag
& preg
[REG_FLAG
].mask
)
207 dev_info(chip
->dev
, "%s Last FLAG is 0x%x\n",
208 lm355x_name
[chip
->type
],
209 chip
->last_flag
& preg
[REG_FLAG
].mask
);
210 /* brightness 0 means shutdown */
217 regmap_update_bits(chip
->regmap
, preg
[REG_TORCH_CTRL
].regno
,
218 preg
[REG_TORCH_CTRL
].mask
,
220 << preg
[REG_TORCH_CTRL
].shift
);
224 if (pdata
->pin_tx1
!= LM355x_PIN_TORCH_DISABLE
) {
226 regmap_update_bits(chip
->regmap
,
227 preg
[REG_TORCH_CFG
].regno
,
228 preg
[REG_TORCH_CFG
].mask
,
230 preg
[REG_TORCH_CFG
].shift
);
235 "torch brt is set - ext. torch pin mode\n");
242 regmap_update_bits(chip
->regmap
, preg
[REG_FLASH_CTRL
].regno
,
243 preg
[REG_FLASH_CTRL
].mask
,
245 << preg
[REG_FLASH_CTRL
].shift
);
249 if (pdata
->pin_strobe
!= LM355x_PIN_STROBE_DISABLE
) {
250 if (chip
->type
== CHIP_LM3554
)
255 regmap_update_bits(chip
->regmap
,
256 preg
[REG_STROBE_CFG
].regno
,
257 preg
[REG_STROBE_CFG
].mask
,
259 preg
[REG_STROBE_CFG
].shift
);
264 "flash brt is set - ext. strobe pin mode\n");
270 regmap_update_bits(chip
->regmap
, preg
[REG_INDI_CTRL
].regno
,
271 preg
[REG_INDI_CTRL
].mask
,
273 << preg
[REG_INDI_CTRL
].shift
);
277 if (pdata
->pin_tx2
!= LM355x_PIN_TX_DISABLE
) {
279 regmap_update_bits(chip
->regmap
,
280 preg
[REG_INDI_CFG
].regno
,
281 preg
[REG_INDI_CFG
].mask
,
283 preg
[REG_INDI_CFG
].shift
);
294 /* operation mode control */
295 ret
= regmap_update_bits(chip
->regmap
, preg
[REG_OPMODE
].regno
,
296 preg
[REG_OPMODE
].mask
,
297 opmode
<< preg
[REG_OPMODE
].shift
);
302 dev_err(chip
->dev
, "%s:i2c access fail to register\n", __func__
);
308 static int lm355x_torch_brightness_set(struct led_classdev
*cdev
,
309 enum led_brightness brightness
)
311 struct lm355x_chip_data
*chip
=
312 container_of(cdev
, struct lm355x_chip_data
, cdev_torch
);
315 mutex_lock(&chip
->lock
);
316 ret
= lm355x_control(chip
, brightness
, MODE_TORCH
);
317 mutex_unlock(&chip
->lock
);
323 static int lm355x_strobe_brightness_set(struct led_classdev
*cdev
,
324 enum led_brightness brightness
)
326 struct lm355x_chip_data
*chip
=
327 container_of(cdev
, struct lm355x_chip_data
, cdev_flash
);
330 mutex_lock(&chip
->lock
);
331 ret
= lm355x_control(chip
, brightness
, MODE_FLASH
);
332 mutex_unlock(&chip
->lock
);
338 static int lm355x_indicator_brightness_set(struct led_classdev
*cdev
,
339 enum led_brightness brightness
)
341 struct lm355x_chip_data
*chip
=
342 container_of(cdev
, struct lm355x_chip_data
, cdev_indicator
);
345 mutex_lock(&chip
->lock
);
346 ret
= lm355x_control(chip
, brightness
, MODE_INDIC
);
347 mutex_unlock(&chip
->lock
);
351 /* indicator pattern only for lm3556*/
352 static ssize_t
lm3556_indicator_pattern_store(struct device
*dev
,
353 struct device_attribute
*attr
,
354 const char *buf
, size_t size
)
357 struct led_classdev
*led_cdev
= dev_get_drvdata(dev
);
358 struct lm355x_chip_data
*chip
=
359 container_of(led_cdev
, struct lm355x_chip_data
, cdev_indicator
);
362 ret
= kstrtouint(buf
, 10, &state
);
365 if (state
> INDIC_PATTERN_SIZE
- 1)
366 state
= INDIC_PATTERN_SIZE
- 1;
368 ret
= regmap_write(chip
->regmap
, 0x04,
369 indicator_pattern
[state
].blinking
);
373 ret
= regmap_write(chip
->regmap
, 0x05,
374 indicator_pattern
[state
].period_cnt
);
380 dev_err(chip
->dev
, "%s:i2c access fail to register\n", __func__
);
384 static DEVICE_ATTR(pattern
, S_IWUSR
, NULL
, lm3556_indicator_pattern_store
);
386 static struct attribute
*lm355x_indicator_attrs
[] = {
387 &dev_attr_pattern
.attr
,
390 ATTRIBUTE_GROUPS(lm355x_indicator
);
392 static const struct regmap_config lm355x_regmap
= {
395 .max_register
= 0xFF,
398 /* module initialize */
399 static int lm355x_probe(struct i2c_client
*client
,
400 const struct i2c_device_id
*id
)
402 struct lm355x_platform_data
*pdata
= dev_get_platdata(&client
->dev
);
403 struct lm355x_chip_data
*chip
;
407 if (!i2c_check_functionality(client
->adapter
, I2C_FUNC_I2C
)) {
408 dev_err(&client
->dev
, "i2c functionality check fail.\n");
413 dev_err(&client
->dev
, "needs Platform Data.\n");
417 chip
= devm_kzalloc(&client
->dev
,
418 sizeof(struct lm355x_chip_data
), GFP_KERNEL
);
422 chip
->dev
= &client
->dev
;
423 chip
->type
= id
->driver_data
;
424 switch (id
->driver_data
) {
426 chip
->regs
= lm3554_regs
;
429 chip
->regs
= lm3556_regs
;
436 chip
->regmap
= devm_regmap_init_i2c(client
, &lm355x_regmap
);
437 if (IS_ERR(chip
->regmap
)) {
438 err
= PTR_ERR(chip
->regmap
);
439 dev_err(&client
->dev
,
440 "Failed to allocate register map: %d\n", err
);
444 mutex_init(&chip
->lock
);
445 i2c_set_clientdata(client
, chip
);
447 err
= lm355x_chip_init(chip
);
452 chip
->cdev_flash
.name
= "flash";
453 chip
->cdev_flash
.max_brightness
= 16;
454 chip
->cdev_flash
.brightness_set_blocking
= lm355x_strobe_brightness_set
;
455 chip
->cdev_flash
.default_trigger
= "flash";
456 err
= led_classdev_register((struct device
*)
457 &client
->dev
, &chip
->cdev_flash
);
461 chip
->cdev_torch
.name
= "torch";
462 chip
->cdev_torch
.max_brightness
= 8;
463 chip
->cdev_torch
.brightness_set_blocking
= lm355x_torch_brightness_set
;
464 chip
->cdev_torch
.default_trigger
= "torch";
465 err
= led_classdev_register((struct device
*)
466 &client
->dev
, &chip
->cdev_torch
);
468 goto err_create_torch_file
;
470 chip
->cdev_indicator
.name
= "indicator";
471 if (id
->driver_data
== CHIP_LM3554
)
472 chip
->cdev_indicator
.max_brightness
= 4;
474 chip
->cdev_indicator
.max_brightness
= 8;
475 chip
->cdev_indicator
.brightness_set_blocking
=
476 lm355x_indicator_brightness_set
;
477 /* indicator pattern control only for LM3556 */
478 if (id
->driver_data
== CHIP_LM3556
)
479 chip
->cdev_indicator
.groups
= lm355x_indicator_groups
;
480 err
= led_classdev_register((struct device
*)
481 &client
->dev
, &chip
->cdev_indicator
);
483 goto err_create_indicator_file
;
485 dev_info(&client
->dev
, "%s is initialized\n",
486 lm355x_name
[id
->driver_data
]);
489 err_create_indicator_file
:
490 led_classdev_unregister(&chip
->cdev_torch
);
491 err_create_torch_file
:
492 led_classdev_unregister(&chip
->cdev_flash
);
497 static int lm355x_remove(struct i2c_client
*client
)
499 struct lm355x_chip_data
*chip
= i2c_get_clientdata(client
);
500 struct lm355x_reg_data
*preg
= chip
->regs
;
502 regmap_write(chip
->regmap
, preg
[REG_OPMODE
].regno
, 0);
503 led_classdev_unregister(&chip
->cdev_indicator
);
504 led_classdev_unregister(&chip
->cdev_torch
);
505 led_classdev_unregister(&chip
->cdev_flash
);
506 dev_info(&client
->dev
, "%s is removed\n", lm355x_name
[chip
->type
]);
511 static const struct i2c_device_id lm355x_id
[] = {
512 {LM3554_NAME
, CHIP_LM3554
},
513 {LM3556_NAME
, CHIP_LM3556
},
517 MODULE_DEVICE_TABLE(i2c
, lm355x_id
);
519 static struct i2c_driver lm355x_i2c_driver
= {
524 .probe
= lm355x_probe
,
525 .remove
= lm355x_remove
,
526 .id_table
= lm355x_id
,
529 module_i2c_driver(lm355x_i2c_driver
);
531 MODULE_DESCRIPTION("Texas Instruments Flash Lighting driver for LM355x");
532 MODULE_AUTHOR("Daniel Jeong <daniel.jeong@ti.com>");
533 MODULE_AUTHOR("G.Shark Jeong <gshark.jeong@gmail.com>");
534 MODULE_LICENSE("GPL v2");