2 * Simple driver for Texas Instruments LM3642 LED Flash driver chip
3 * Copyright (C) 2012 Texas Instruments
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.
10 #include <linux/module.h>
11 #include <linux/delay.h>
12 #include <linux/i2c.h>
13 #include <linux/leds.h>
14 #include <linux/slab.h>
15 #include <linux/platform_device.h>
17 #include <linux/regmap.h>
18 #include <linux/platform_data/leds-lm3642.h>
20 #define REG_FILT_TIME (0x0)
21 #define REG_IVFM_MODE (0x1)
22 #define REG_TORCH_TIME (0x6)
23 #define REG_FLASH (0x8)
24 #define REG_I_CTRL (0x9)
25 #define REG_ENABLE (0xA)
26 #define REG_FLAG (0xB)
29 #define UVLO_EN_SHIFT (7)
30 #define IVM_D_TH_SHIFT (2)
31 #define TORCH_RAMP_UP_TIME_SHIFT (3)
32 #define TORCH_RAMP_DN_TIME_SHIFT (0)
33 #define INDUCTOR_I_LIMIT_SHIFT (6)
34 #define FLASH_RAMP_TIME_SHIFT (3)
35 #define FLASH_TOUT_TIME_SHIFT (0)
36 #define TORCH_I_SHIFT (4)
37 #define FLASH_I_SHIFT (0)
38 #define IVFM_SHIFT (7)
39 #define TX_PIN_EN_SHIFT (6)
40 #define STROBE_PIN_EN_SHIFT (5)
41 #define TORCH_PIN_EN_SHIFT (4)
42 #define MODE_BITS_SHIFT (0)
44 #define UVLO_EN_MASK (0x1)
45 #define IVM_D_TH_MASK (0x7)
46 #define TORCH_RAMP_UP_TIME_MASK (0x7)
47 #define TORCH_RAMP_DN_TIME_MASK (0x7)
48 #define INDUCTOR_I_LIMIT_MASK (0x1)
49 #define FLASH_RAMP_TIME_MASK (0x7)
50 #define FLASH_TOUT_TIME_MASK (0x7)
51 #define TORCH_I_MASK (0x7)
52 #define FLASH_I_MASK (0xF)
53 #define IVFM_MASK (0x1)
54 #define TX_PIN_EN_MASK (0x1)
55 #define STROBE_PIN_EN_MASK (0x1)
56 #define TORCH_PIN_EN_MASK (0x1)
57 #define MODE_BITS_MASK (0x73)
58 #define EX_PIN_CONTROL_MASK (0x71)
59 #define EX_PIN_ENABLE_MASK (0x70)
68 struct lm3642_chip_data
{
71 struct led_classdev cdev_flash
;
72 struct led_classdev cdev_torch
;
73 struct led_classdev cdev_indicator
;
79 enum lm3642_torch_pin_enable torch_pin
;
80 enum lm3642_strobe_pin_enable strobe_pin
;
81 enum lm3642_tx_pin_enable tx_pin
;
83 struct lm3642_platform_data
*pdata
;
84 struct regmap
*regmap
;
87 unsigned int last_flag
;
91 static int lm3642_chip_init(struct lm3642_chip_data
*chip
)
94 struct lm3642_platform_data
*pdata
= chip
->pdata
;
96 /* set enable register */
97 ret
= regmap_update_bits(chip
->regmap
, REG_ENABLE
, EX_PIN_ENABLE_MASK
,
100 dev_err(chip
->dev
, "Failed to update REG_ENABLE Register\n");
105 static int lm3642_control(struct lm3642_chip_data
*chip
,
106 u8 brightness
, enum lm3642_mode opmode
)
110 ret
= regmap_read(chip
->regmap
, REG_FLAG
, &chip
->last_flag
);
112 dev_err(chip
->dev
, "Failed to read REG_FLAG Register\n");
117 dev_info(chip
->dev
, "Last FLAG is 0x%x\n", chip
->last_flag
);
119 /* brightness 0 means off-state */
121 opmode
= MODES_STASNDBY
;
125 ret
= regmap_update_bits(chip
->regmap
, REG_I_CTRL
,
126 TORCH_I_MASK
<< TORCH_I_SHIFT
,
127 (brightness
- 1) << TORCH_I_SHIFT
);
130 opmode
|= (TORCH_PIN_EN_MASK
<< TORCH_PIN_EN_SHIFT
);
134 ret
= regmap_update_bits(chip
->regmap
, REG_I_CTRL
,
135 FLASH_I_MASK
<< FLASH_I_SHIFT
,
136 (brightness
- 1) << FLASH_I_SHIFT
);
138 if (chip
->strobe_pin
)
139 opmode
|= (STROBE_PIN_EN_MASK
<< STROBE_PIN_EN_SHIFT
);
143 ret
= regmap_update_bits(chip
->regmap
, REG_I_CTRL
,
144 TORCH_I_MASK
<< TORCH_I_SHIFT
,
145 (brightness
- 1) << TORCH_I_SHIFT
);
156 dev_err(chip
->dev
, "Failed to write REG_I_CTRL Register\n");
161 opmode
|= (TX_PIN_EN_MASK
<< TX_PIN_EN_SHIFT
);
163 ret
= regmap_update_bits(chip
->regmap
, REG_ENABLE
,
164 MODE_BITS_MASK
<< MODE_BITS_SHIFT
,
165 opmode
<< MODE_BITS_SHIFT
);
172 /* torch pin config for lm3642*/
173 static ssize_t
lm3642_torch_pin_store(struct device
*dev
,
174 struct device_attribute
*attr
,
175 const char *buf
, size_t size
)
178 struct led_classdev
*led_cdev
= dev_get_drvdata(dev
);
179 struct lm3642_chip_data
*chip
=
180 container_of(led_cdev
, struct lm3642_chip_data
, cdev_indicator
);
183 ret
= kstrtouint(buf
, 10, &state
);
187 state
= 0x01 << TORCH_PIN_EN_SHIFT
;
189 chip
->torch_pin
= state
;
190 ret
= regmap_update_bits(chip
->regmap
, REG_ENABLE
,
191 TORCH_PIN_EN_MASK
<< TORCH_PIN_EN_SHIFT
,
198 dev_err(chip
->dev
, "%s:i2c access fail to register\n", __func__
);
201 dev_err(chip
->dev
, "%s: fail to change str to int\n", __func__
);
205 static DEVICE_ATTR(torch_pin
, S_IWUSR
, NULL
, lm3642_torch_pin_store
);
207 static int lm3642_torch_brightness_set(struct led_classdev
*cdev
,
208 enum led_brightness brightness
)
210 struct lm3642_chip_data
*chip
=
211 container_of(cdev
, struct lm3642_chip_data
, cdev_torch
);
214 mutex_lock(&chip
->lock
);
215 chip
->br_torch
= brightness
;
216 ret
= lm3642_control(chip
, chip
->br_torch
, MODES_TORCH
);
217 mutex_unlock(&chip
->lock
);
223 /* strobe pin config for lm3642*/
224 static ssize_t
lm3642_strobe_pin_store(struct device
*dev
,
225 struct device_attribute
*attr
,
226 const char *buf
, size_t size
)
229 struct led_classdev
*led_cdev
= dev_get_drvdata(dev
);
230 struct lm3642_chip_data
*chip
=
231 container_of(led_cdev
, struct lm3642_chip_data
, cdev_indicator
);
234 ret
= kstrtouint(buf
, 10, &state
);
238 state
= 0x01 << STROBE_PIN_EN_SHIFT
;
240 chip
->strobe_pin
= state
;
241 ret
= regmap_update_bits(chip
->regmap
, REG_ENABLE
,
242 STROBE_PIN_EN_MASK
<< STROBE_PIN_EN_SHIFT
,
249 dev_err(chip
->dev
, "%s:i2c access fail to register\n", __func__
);
252 dev_err(chip
->dev
, "%s: fail to change str to int\n", __func__
);
256 static DEVICE_ATTR(strobe_pin
, S_IWUSR
, NULL
, lm3642_strobe_pin_store
);
258 static int lm3642_strobe_brightness_set(struct led_classdev
*cdev
,
259 enum led_brightness brightness
)
261 struct lm3642_chip_data
*chip
=
262 container_of(cdev
, struct lm3642_chip_data
, cdev_flash
);
265 mutex_lock(&chip
->lock
);
266 chip
->br_flash
= brightness
;
267 ret
= lm3642_control(chip
, chip
->br_flash
, MODES_FLASH
);
268 mutex_unlock(&chip
->lock
);
273 static int lm3642_indicator_brightness_set(struct led_classdev
*cdev
,
274 enum led_brightness brightness
)
276 struct lm3642_chip_data
*chip
=
277 container_of(cdev
, struct lm3642_chip_data
, cdev_indicator
);
280 mutex_lock(&chip
->lock
);
281 chip
->br_indicator
= brightness
;
282 ret
= lm3642_control(chip
, chip
->br_indicator
, MODES_INDIC
);
283 mutex_unlock(&chip
->lock
);
287 static const struct regmap_config lm3642_regmap
= {
290 .max_register
= REG_MAX
,
293 static struct attribute
*lm3642_flash_attrs
[] = {
294 &dev_attr_strobe_pin
.attr
,
297 ATTRIBUTE_GROUPS(lm3642_flash
);
299 static struct attribute
*lm3642_torch_attrs
[] = {
300 &dev_attr_torch_pin
.attr
,
303 ATTRIBUTE_GROUPS(lm3642_torch
);
305 static int lm3642_probe(struct i2c_client
*client
,
306 const struct i2c_device_id
*id
)
308 struct lm3642_platform_data
*pdata
= dev_get_platdata(&client
->dev
);
309 struct lm3642_chip_data
*chip
;
313 if (!i2c_check_functionality(client
->adapter
, I2C_FUNC_I2C
)) {
314 dev_err(&client
->dev
, "i2c functionality check fail.\n");
319 dev_err(&client
->dev
, "needs Platform Data.\n");
323 chip
= devm_kzalloc(&client
->dev
,
324 sizeof(struct lm3642_chip_data
), GFP_KERNEL
);
328 chip
->dev
= &client
->dev
;
331 chip
->tx_pin
= pdata
->tx_pin
;
332 chip
->torch_pin
= pdata
->torch_pin
;
333 chip
->strobe_pin
= pdata
->strobe_pin
;
335 chip
->regmap
= devm_regmap_init_i2c(client
, &lm3642_regmap
);
336 if (IS_ERR(chip
->regmap
)) {
337 err
= PTR_ERR(chip
->regmap
);
338 dev_err(&client
->dev
, "Failed to allocate register map: %d\n",
343 mutex_init(&chip
->lock
);
344 i2c_set_clientdata(client
, chip
);
346 err
= lm3642_chip_init(chip
);
351 chip
->cdev_flash
.name
= "flash";
352 chip
->cdev_flash
.max_brightness
= 16;
353 chip
->cdev_flash
.brightness_set_blocking
= lm3642_strobe_brightness_set
;
354 chip
->cdev_flash
.default_trigger
= "flash";
355 chip
->cdev_flash
.groups
= lm3642_flash_groups
,
356 err
= led_classdev_register((struct device
*)
357 &client
->dev
, &chip
->cdev_flash
);
359 dev_err(chip
->dev
, "failed to register flash\n");
364 chip
->cdev_torch
.name
= "torch";
365 chip
->cdev_torch
.max_brightness
= 8;
366 chip
->cdev_torch
.brightness_set_blocking
= lm3642_torch_brightness_set
;
367 chip
->cdev_torch
.default_trigger
= "torch";
368 chip
->cdev_torch
.groups
= lm3642_torch_groups
,
369 err
= led_classdev_register((struct device
*)
370 &client
->dev
, &chip
->cdev_torch
);
372 dev_err(chip
->dev
, "failed to register torch\n");
373 goto err_create_torch_file
;
377 chip
->cdev_indicator
.name
= "indicator";
378 chip
->cdev_indicator
.max_brightness
= 8;
379 chip
->cdev_indicator
.brightness_set_blocking
=
380 lm3642_indicator_brightness_set
;
381 err
= led_classdev_register((struct device
*)
382 &client
->dev
, &chip
->cdev_indicator
);
384 dev_err(chip
->dev
, "failed to register indicator\n");
385 goto err_create_indicator_file
;
388 dev_info(&client
->dev
, "LM3642 is initialized\n");
391 err_create_indicator_file
:
392 led_classdev_unregister(&chip
->cdev_torch
);
393 err_create_torch_file
:
394 led_classdev_unregister(&chip
->cdev_flash
);
399 static int lm3642_remove(struct i2c_client
*client
)
401 struct lm3642_chip_data
*chip
= i2c_get_clientdata(client
);
403 led_classdev_unregister(&chip
->cdev_indicator
);
404 led_classdev_unregister(&chip
->cdev_torch
);
405 led_classdev_unregister(&chip
->cdev_flash
);
406 regmap_write(chip
->regmap
, REG_ENABLE
, 0);
410 static const struct i2c_device_id lm3642_id
[] = {
415 MODULE_DEVICE_TABLE(i2c
, lm3642_id
);
417 static struct i2c_driver lm3642_i2c_driver
= {
422 .probe
= lm3642_probe
,
423 .remove
= lm3642_remove
,
424 .id_table
= lm3642_id
,
427 module_i2c_driver(lm3642_i2c_driver
);
429 MODULE_DESCRIPTION("Texas Instruments Flash Lighting driver for LM3642");
430 MODULE_AUTHOR("Daniel Jeong <daniel.jeong@ti.com>");
431 MODULE_AUTHOR("G.Shark Jeong <gshark.jeong@gmail.com>");
432 MODULE_LICENSE("GPL v2");