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/leds.h>
11 #include <linux/slab.h>
12 #include <linux/platform_device.h>
14 #include <linux/regmap.h>
15 #include <linux/platform_data/leds-lm355x.h>
42 /* register map info. */
43 struct lm355x_reg_data {
49 struct lm355x_chip_data {
51 enum lm355x_type type;
53 struct led_classdev cdev_flash;
54 struct led_classdev cdev_torch;
55 struct led_classdev cdev_indicator;
57 struct lm355x_platform_data *pdata;
58 struct regmap *regmap;
61 unsigned int last_flag;
62 struct lm355x_reg_data *regs;
65 /* specific indicator function for lm3556 */
66 enum lm3556_indic_pulse_time {
85 enum lm3556_indic_n_blank {
104 enum lm3556_indic_period {
115 #define INDIC_PATTERN_SIZE 4
122 /* indicator pattern data only for lm3556 */
123 static struct indicator indicator_pattern[INDIC_PATTERN_SIZE] = {
124 [0] = {(INDIC_N_BLANK_1 << 4) | PULSE_TIME_32_MS, INDIC_PERIOD_1},
125 [1] = {(INDIC_N_BLANK_15 << 4) | PULSE_TIME_32_MS, INDIC_PERIOD_2},
126 [2] = {(INDIC_N_BLANK_10 << 4) | PULSE_TIME_32_MS, INDIC_PERIOD_4},
127 [3] = {(INDIC_N_BLANK_5 << 4) | PULSE_TIME_32_MS, INDIC_PERIOD_7},
130 static struct lm355x_reg_data lm3554_regs[REG_MAX] = {
131 [REG_FLAG] = {0xD0, 0xBF, 0},
132 [REG_TORCH_CFG] = {0xE0, 0x80, 7},
133 [REG_TORCH_CTRL] = {0xA0, 0x38, 3},
134 [REG_STROBE_CFG] = {0xE0, 0x04, 2},
135 [REG_FLASH_CTRL] = {0xB0, 0x78, 3},
136 [REG_INDI_CFG] = {0xE0, 0x08, 3},
137 [REG_INDI_CTRL] = {0xA0, 0xC0, 6},
138 [REG_OPMODE] = {0xA0, 0x03, 0},
141 static struct lm355x_reg_data lm3556_regs[REG_MAX] = {
142 [REG_FLAG] = {0x0B, 0xFF, 0},
143 [REG_TORCH_CFG] = {0x0A, 0x10, 4},
144 [REG_TORCH_CTRL] = {0x09, 0x70, 4},
145 [REG_STROBE_CFG] = {0x0A, 0x20, 5},
146 [REG_FLASH_CTRL] = {0x09, 0x0F, 0},
147 [REG_INDI_CFG] = {0xFF, 0xFF, 0},
148 [REG_INDI_CTRL] = {0x09, 0x70, 4},
149 [REG_OPMODE] = {0x0A, 0x03, 0},
152 static char lm355x_name[][I2C_NAME_SIZE] = {
153 [CHIP_LM3554] = LM3554_NAME,
154 [CHIP_LM3556] = LM3556_NAME,
157 /* chip initialize */
158 static int lm355x_chip_init(struct lm355x_chip_data *chip)
161 unsigned int reg_val;
162 struct lm355x_platform_data *pdata = chip->pdata;
164 /* input and output pins configuration */
165 switch (chip->type) {
167 reg_val = (u32)pdata->pin_tx2 | (u32)pdata->ntc_pin;
168 ret = regmap_update_bits(chip->regmap, 0xE0, 0x28, reg_val);
171 reg_val = (u32)pdata->pass_mode;
172 ret = regmap_update_bits(chip->regmap, 0xA0, 0x04, reg_val);
178 reg_val = (u32)pdata->pin_tx2 | (u32)pdata->ntc_pin |
179 (u32)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 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_WO(pattern);
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(&client->dev, &chip->cdev_flash);
460 chip->cdev_torch.name = "torch";
461 chip->cdev_torch.max_brightness = 8;
462 chip->cdev_torch.brightness_set_blocking = lm355x_torch_brightness_set;
463 chip->cdev_torch.default_trigger = "torch";
464 err = led_classdev_register(&client->dev, &chip->cdev_torch);
466 goto err_create_torch_file;
468 chip->cdev_indicator.name = "indicator";
469 if (id->driver_data == CHIP_LM3554)
470 chip->cdev_indicator.max_brightness = 4;
472 chip->cdev_indicator.max_brightness = 8;
473 chip->cdev_indicator.brightness_set_blocking =
474 lm355x_indicator_brightness_set;
475 /* indicator pattern control only for LM3556 */
476 if (id->driver_data == CHIP_LM3556)
477 chip->cdev_indicator.groups = lm355x_indicator_groups;
478 err = led_classdev_register(&client->dev, &chip->cdev_indicator);
480 goto err_create_indicator_file;
482 dev_info(&client->dev, "%s is initialized\n",
483 lm355x_name[id->driver_data]);
486 err_create_indicator_file:
487 led_classdev_unregister(&chip->cdev_torch);
488 err_create_torch_file:
489 led_classdev_unregister(&chip->cdev_flash);
494 static int lm355x_remove(struct i2c_client *client)
496 struct lm355x_chip_data *chip = i2c_get_clientdata(client);
497 struct lm355x_reg_data *preg = chip->regs;
499 regmap_write(chip->regmap, preg[REG_OPMODE].regno, 0);
500 led_classdev_unregister(&chip->cdev_indicator);
501 led_classdev_unregister(&chip->cdev_torch);
502 led_classdev_unregister(&chip->cdev_flash);
503 dev_info(&client->dev, "%s is removed\n", lm355x_name[chip->type]);
508 static const struct i2c_device_id lm355x_id[] = {
509 {LM3554_NAME, CHIP_LM3554},
510 {LM3556_NAME, CHIP_LM3556},
514 MODULE_DEVICE_TABLE(i2c, lm355x_id);
516 static struct i2c_driver lm355x_i2c_driver = {
521 .probe = lm355x_probe,
522 .remove = lm355x_remove,
523 .id_table = lm355x_id,
526 module_i2c_driver(lm355x_i2c_driver);
528 MODULE_DESCRIPTION("Texas Instruments Flash Lighting driver for LM355x");
529 MODULE_AUTHOR("Daniel Jeong <daniel.jeong@ti.com>");
530 MODULE_AUTHOR("G.Shark Jeong <gshark.jeong@gmail.com>");
531 MODULE_LICENSE("GPL v2");