s390: include: Remove redundant license text
[linux/fpc-iii.git] / drivers / leds / leds-lm3642.c
blobcada0848db7b2623f511541b679cfaa74ae3a538
1 /*
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.
9 */
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>
16 #include <linux/fs.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)
27 #define REG_MAX (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)
61 enum lm3642_mode {
62 MODES_STASNDBY = 0,
63 MODES_INDIC,
64 MODES_TORCH,
65 MODES_FLASH
68 struct lm3642_chip_data {
69 struct device *dev;
71 struct led_classdev cdev_flash;
72 struct led_classdev cdev_torch;
73 struct led_classdev cdev_indicator;
75 u8 br_flash;
76 u8 br_torch;
77 u8 br_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;
85 struct mutex lock;
87 unsigned int last_flag;
90 /* chip initialize */
91 static int lm3642_chip_init(struct lm3642_chip_data *chip)
93 int ret;
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,
98 pdata->tx_pin);
99 if (ret < 0)
100 dev_err(chip->dev, "Failed to update REG_ENABLE Register\n");
101 return ret;
104 /* chip control */
105 static int lm3642_control(struct lm3642_chip_data *chip,
106 u8 brightness, enum lm3642_mode opmode)
108 int ret;
110 ret = regmap_read(chip->regmap, REG_FLAG, &chip->last_flag);
111 if (ret < 0) {
112 dev_err(chip->dev, "Failed to read REG_FLAG Register\n");
113 goto out;
116 if (chip->last_flag)
117 dev_info(chip->dev, "Last FLAG is 0x%x\n", chip->last_flag);
119 /* brightness 0 means off-state */
120 if (!brightness)
121 opmode = MODES_STASNDBY;
123 switch (opmode) {
124 case MODES_TORCH:
125 ret = regmap_update_bits(chip->regmap, REG_I_CTRL,
126 TORCH_I_MASK << TORCH_I_SHIFT,
127 (brightness - 1) << TORCH_I_SHIFT);
129 if (chip->torch_pin)
130 opmode |= (TORCH_PIN_EN_MASK << TORCH_PIN_EN_SHIFT);
131 break;
133 case MODES_FLASH:
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);
140 break;
142 case MODES_INDIC:
143 ret = regmap_update_bits(chip->regmap, REG_I_CTRL,
144 TORCH_I_MASK << TORCH_I_SHIFT,
145 (brightness - 1) << TORCH_I_SHIFT);
146 break;
148 case MODES_STASNDBY:
150 break;
152 default:
153 return ret;
155 if (ret < 0) {
156 dev_err(chip->dev, "Failed to write REG_I_CTRL Register\n");
157 goto out;
160 if (chip->tx_pin)
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);
166 out:
167 return ret;
170 /* torch */
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)
177 ssize_t ret;
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);
181 unsigned int state;
183 ret = kstrtouint(buf, 10, &state);
184 if (ret)
185 goto out_strtoint;
186 if (state != 0)
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,
192 state);
193 if (ret < 0)
194 goto out;
196 return size;
197 out:
198 dev_err(chip->dev, "%s:i2c access fail to register\n", __func__);
199 return ret;
200 out_strtoint:
201 dev_err(chip->dev, "%s: fail to change str to int\n", __func__);
202 return ret;
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);
212 int ret;
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);
218 return ret;
221 /* flash */
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)
228 ssize_t ret;
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);
232 unsigned int state;
234 ret = kstrtouint(buf, 10, &state);
235 if (ret)
236 goto out_strtoint;
237 if (state != 0)
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,
243 state);
244 if (ret < 0)
245 goto out;
247 return size;
248 out:
249 dev_err(chip->dev, "%s:i2c access fail to register\n", __func__);
250 return ret;
251 out_strtoint:
252 dev_err(chip->dev, "%s: fail to change str to int\n", __func__);
253 return ret;
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);
263 int ret;
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);
269 return ret;
272 /* indicator */
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);
278 int ret;
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);
284 return ret;
287 static const struct regmap_config lm3642_regmap = {
288 .reg_bits = 8,
289 .val_bits = 8,
290 .max_register = REG_MAX,
293 static struct attribute *lm3642_flash_attrs[] = {
294 &dev_attr_strobe_pin.attr,
295 NULL
297 ATTRIBUTE_GROUPS(lm3642_flash);
299 static struct attribute *lm3642_torch_attrs[] = {
300 &dev_attr_torch_pin.attr,
301 NULL
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;
311 int err;
313 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
314 dev_err(&client->dev, "i2c functionality check fail.\n");
315 return -EOPNOTSUPP;
318 if (pdata == NULL) {
319 dev_err(&client->dev, "needs Platform Data.\n");
320 return -ENODATA;
323 chip = devm_kzalloc(&client->dev,
324 sizeof(struct lm3642_chip_data), GFP_KERNEL);
325 if (!chip)
326 return -ENOMEM;
328 chip->dev = &client->dev;
329 chip->pdata = pdata;
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",
339 err);
340 return err;
343 mutex_init(&chip->lock);
344 i2c_set_clientdata(client, chip);
346 err = lm3642_chip_init(chip);
347 if (err < 0)
348 goto err_out;
350 /* flash */
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);
358 if (err < 0) {
359 dev_err(chip->dev, "failed to register flash\n");
360 goto err_out;
363 /* torch */
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);
371 if (err < 0) {
372 dev_err(chip->dev, "failed to register torch\n");
373 goto err_create_torch_file;
376 /* indicator */
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);
383 if (err < 0) {
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");
389 return 0;
391 err_create_indicator_file:
392 led_classdev_unregister(&chip->cdev_torch);
393 err_create_torch_file:
394 led_classdev_unregister(&chip->cdev_flash);
395 err_out:
396 return err;
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);
407 return 0;
410 static const struct i2c_device_id lm3642_id[] = {
411 {LM3642_NAME, 0},
415 MODULE_DEVICE_TABLE(i2c, lm3642_id);
417 static struct i2c_driver lm3642_i2c_driver = {
418 .driver = {
419 .name = LM3642_NAME,
420 .pm = NULL,
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");