blob: cada0848db7b2623f511541b679cfaa74ae3a538 [file] [log] [blame]
G.Shark Jeong8b7cfbe2012-09-12 20:05:50 +08001/*
2* Simple driver for Texas Instruments LM3642 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/leds.h>
14#include <linux/slab.h>
15#include <linux/platform_device.h>
16#include <linux/fs.h>
17#include <linux/regmap.h>
G.Shark Jeong8b7cfbe2012-09-12 20:05:50 +080018#include <linux/platform_data/leds-lm3642.h>
19
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)
28
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)
43
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)
60
61enum lm3642_mode {
62 MODES_STASNDBY = 0,
63 MODES_INDIC,
64 MODES_TORCH,
65 MODES_FLASH
66};
67
68struct lm3642_chip_data {
69 struct device *dev;
70
71 struct led_classdev cdev_flash;
72 struct led_classdev cdev_torch;
73 struct led_classdev cdev_indicator;
74
G.Shark Jeong8b7cfbe2012-09-12 20:05:50 +080075 u8 br_flash;
76 u8 br_torch;
77 u8 br_indicator;
78
79 enum lm3642_torch_pin_enable torch_pin;
80 enum lm3642_strobe_pin_enable strobe_pin;
81 enum lm3642_tx_pin_enable tx_pin;
82
83 struct lm3642_platform_data *pdata;
84 struct regmap *regmap;
85 struct mutex lock;
86
87 unsigned int last_flag;
88};
89
90/* chip initialize */
Bill Pemberton98ea1ea2012-11-19 13:23:02 -050091static int lm3642_chip_init(struct lm3642_chip_data *chip)
G.Shark Jeong8b7cfbe2012-09-12 20:05:50 +080092{
G.Shark Jeong8b7cfbe2012-09-12 20:05:50 +080093 int ret;
94 struct lm3642_platform_data *pdata = chip->pdata;
95
96 /* set enable register */
Axel Line76a3222012-09-22 14:40:14 +080097 ret = regmap_update_bits(chip->regmap, REG_ENABLE, EX_PIN_ENABLE_MASK,
98 pdata->tx_pin);
G.Shark Jeong8b7cfbe2012-09-12 20:05:50 +080099 if (ret < 0)
Axel Line76a3222012-09-22 14:40:14 +0800100 dev_err(chip->dev, "Failed to update REG_ENABLE Register\n");
G.Shark Jeong8b7cfbe2012-09-12 20:05:50 +0800101 return ret;
102}
103
104/* chip control */
105static int lm3642_control(struct lm3642_chip_data *chip,
106 u8 brightness, enum lm3642_mode opmode)
107{
108 int ret;
109
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;
114 }
115
116 if (chip->last_flag)
117 dev_info(chip->dev, "Last FLAG is 0x%x\n", chip->last_flag);
118
119 /* brightness 0 means off-state */
120 if (!brightness)
121 opmode = MODES_STASNDBY;
122
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);
128
129 if (chip->torch_pin)
130 opmode |= (TORCH_PIN_EN_MASK << TORCH_PIN_EN_SHIFT);
131 break;
132
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);
137
138 if (chip->strobe_pin)
139 opmode |= (STROBE_PIN_EN_MASK << STROBE_PIN_EN_SHIFT);
140 break;
141
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;
147
148 case MODES_STASNDBY:
149
150 break;
151
152 default:
153 return ret;
154 }
155 if (ret < 0) {
156 dev_err(chip->dev, "Failed to write REG_I_CTRL Register\n");
157 goto out;
158 }
159
160 if (chip->tx_pin)
161 opmode |= (TX_PIN_EN_MASK << TX_PIN_EN_SHIFT);
162
163 ret = regmap_update_bits(chip->regmap, REG_ENABLE,
164 MODE_BITS_MASK << MODE_BITS_SHIFT,
165 opmode << MODE_BITS_SHIFT);
166out:
167 return ret;
168}
169
170/* torch */
171
172/* torch pin config for lm3642*/
173static ssize_t lm3642_torch_pin_store(struct device *dev,
Jingoo Han85b4b752013-01-21 21:57:20 -0800174 struct device_attribute *attr,
G.Shark Jeong8b7cfbe2012-09-12 20:05:50 +0800175 const char *buf, size_t size)
176{
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;
182
183 ret = kstrtouint(buf, 10, &state);
184 if (ret)
185 goto out_strtoint;
186 if (state != 0)
187 state = 0x01 << TORCH_PIN_EN_SHIFT;
188
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;
195
196 return size;
197out:
198 dev_err(chip->dev, "%s:i2c access fail to register\n", __func__);
Axel Lineccb6632012-10-07 20:54:23 -0700199 return ret;
G.Shark Jeong8b7cfbe2012-09-12 20:05:50 +0800200out_strtoint:
201 dev_err(chip->dev, "%s: fail to change str to int\n", __func__);
Axel Lineccb6632012-10-07 20:54:23 -0700202 return ret;
G.Shark Jeong8b7cfbe2012-09-12 20:05:50 +0800203}
204
Axel Lin5cce0102012-10-29 01:41:46 -0700205static DEVICE_ATTR(torch_pin, S_IWUSR, NULL, lm3642_torch_pin_store);
G.Shark Jeong8b7cfbe2012-09-12 20:05:50 +0800206
Andrew Lunnbb58cc82015-08-20 12:05:54 +0200207static int lm3642_torch_brightness_set(struct led_classdev *cdev,
G.Shark Jeong8b7cfbe2012-09-12 20:05:50 +0800208 enum led_brightness brightness)
209{
210 struct lm3642_chip_data *chip =
211 container_of(cdev, struct lm3642_chip_data, cdev_torch);
Andrew Lunnbb58cc82015-08-20 12:05:54 +0200212 int ret;
G.Shark Jeong8b7cfbe2012-09-12 20:05:50 +0800213
Andrew Lunnbb58cc82015-08-20 12:05:54 +0200214 mutex_lock(&chip->lock);
G.Shark Jeong8b7cfbe2012-09-12 20:05:50 +0800215 chip->br_torch = brightness;
Andrew Lunnbb58cc82015-08-20 12:05:54 +0200216 ret = lm3642_control(chip, chip->br_torch, MODES_TORCH);
217 mutex_unlock(&chip->lock);
218 return ret;
G.Shark Jeong8b7cfbe2012-09-12 20:05:50 +0800219}
220
221/* flash */
222
223/* strobe pin config for lm3642*/
224static ssize_t lm3642_strobe_pin_store(struct device *dev,
Jingoo Han85b4b752013-01-21 21:57:20 -0800225 struct device_attribute *attr,
G.Shark Jeong8b7cfbe2012-09-12 20:05:50 +0800226 const char *buf, size_t size)
227{
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;
233
234 ret = kstrtouint(buf, 10, &state);
235 if (ret)
236 goto out_strtoint;
237 if (state != 0)
238 state = 0x01 << STROBE_PIN_EN_SHIFT;
239
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;
246
247 return size;
248out:
249 dev_err(chip->dev, "%s:i2c access fail to register\n", __func__);
Axel Lineccb6632012-10-07 20:54:23 -0700250 return ret;
G.Shark Jeong8b7cfbe2012-09-12 20:05:50 +0800251out_strtoint:
252 dev_err(chip->dev, "%s: fail to change str to int\n", __func__);
Axel Lineccb6632012-10-07 20:54:23 -0700253 return ret;
G.Shark Jeong8b7cfbe2012-09-12 20:05:50 +0800254}
255
Axel Lin5cce0102012-10-29 01:41:46 -0700256static DEVICE_ATTR(strobe_pin, S_IWUSR, NULL, lm3642_strobe_pin_store);
G.Shark Jeong8b7cfbe2012-09-12 20:05:50 +0800257
Andrew Lunnbb58cc82015-08-20 12:05:54 +0200258static int lm3642_strobe_brightness_set(struct led_classdev *cdev,
G.Shark Jeong8b7cfbe2012-09-12 20:05:50 +0800259 enum led_brightness brightness)
260{
261 struct lm3642_chip_data *chip =
262 container_of(cdev, struct lm3642_chip_data, cdev_flash);
Andrew Lunnbb58cc82015-08-20 12:05:54 +0200263 int ret;
G.Shark Jeong8b7cfbe2012-09-12 20:05:50 +0800264
Andrew Lunnbb58cc82015-08-20 12:05:54 +0200265 mutex_lock(&chip->lock);
G.Shark Jeong8b7cfbe2012-09-12 20:05:50 +0800266 chip->br_flash = brightness;
Andrew Lunnbb58cc82015-08-20 12:05:54 +0200267 ret = lm3642_control(chip, chip->br_flash, MODES_FLASH);
268 mutex_unlock(&chip->lock);
269 return ret;
G.Shark Jeong8b7cfbe2012-09-12 20:05:50 +0800270}
271
272/* indicator */
Andrew Lunnbb58cc82015-08-20 12:05:54 +0200273static int lm3642_indicator_brightness_set(struct led_classdev *cdev,
G.Shark Jeong8b7cfbe2012-09-12 20:05:50 +0800274 enum led_brightness brightness)
275{
276 struct lm3642_chip_data *chip =
277 container_of(cdev, struct lm3642_chip_data, cdev_indicator);
Andrew Lunnbb58cc82015-08-20 12:05:54 +0200278 int ret;
G.Shark Jeong8b7cfbe2012-09-12 20:05:50 +0800279
Andrew Lunnbb58cc82015-08-20 12:05:54 +0200280 mutex_lock(&chip->lock);
G.Shark Jeong8b7cfbe2012-09-12 20:05:50 +0800281 chip->br_indicator = brightness;
Andrew Lunnbb58cc82015-08-20 12:05:54 +0200282 ret = lm3642_control(chip, chip->br_indicator, MODES_INDIC);
283 mutex_unlock(&chip->lock);
284 return ret;
G.Shark Jeong8b7cfbe2012-09-12 20:05:50 +0800285}
286
287static const struct regmap_config lm3642_regmap = {
288 .reg_bits = 8,
289 .val_bits = 8,
290 .max_register = REG_MAX,
291};
292
Johan Hovoldfe29a3b2014-06-25 10:08:48 -0700293static struct attribute *lm3642_flash_attrs[] = {
294 &dev_attr_strobe_pin.attr,
295 NULL
296};
297ATTRIBUTE_GROUPS(lm3642_flash);
298
299static struct attribute *lm3642_torch_attrs[] = {
300 &dev_attr_torch_pin.attr,
301 NULL
302};
303ATTRIBUTE_GROUPS(lm3642_torch);
304
Bill Pemberton98ea1ea2012-11-19 13:23:02 -0500305static int lm3642_probe(struct i2c_client *client,
G.Shark Jeong8b7cfbe2012-09-12 20:05:50 +0800306 const struct i2c_device_id *id)
307{
Jingoo Han87aae1e2013-07-30 01:07:35 -0700308 struct lm3642_platform_data *pdata = dev_get_platdata(&client->dev);
G.Shark Jeong8b7cfbe2012-09-12 20:05:50 +0800309 struct lm3642_chip_data *chip;
310
311 int err;
312
313 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
314 dev_err(&client->dev, "i2c functionality check fail.\n");
315 return -EOPNOTSUPP;
316 }
317
318 if (pdata == NULL) {
319 dev_err(&client->dev, "needs Platform Data.\n");
320 return -ENODATA;
321 }
322
323 chip = devm_kzalloc(&client->dev,
324 sizeof(struct lm3642_chip_data), GFP_KERNEL);
325 if (!chip)
326 return -ENOMEM;
327
328 chip->dev = &client->dev;
329 chip->pdata = pdata;
330
331 chip->tx_pin = pdata->tx_pin;
332 chip->torch_pin = pdata->torch_pin;
333 chip->strobe_pin = pdata->strobe_pin;
334
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;
341 }
342
343 mutex_init(&chip->lock);
344 i2c_set_clientdata(client, chip);
345
346 err = lm3642_chip_init(chip);
347 if (err < 0)
348 goto err_out;
349
350 /* flash */
G.Shark Jeong8b7cfbe2012-09-12 20:05:50 +0800351 chip->cdev_flash.name = "flash";
352 chip->cdev_flash.max_brightness = 16;
Andrew Lunnbb58cc82015-08-20 12:05:54 +0200353 chip->cdev_flash.brightness_set_blocking = lm3642_strobe_brightness_set;
Kim, Milo313bf0b2013-03-14 04:29:26 -0700354 chip->cdev_flash.default_trigger = "flash";
Johan Hovoldfe29a3b2014-06-25 10:08:48 -0700355 chip->cdev_flash.groups = lm3642_flash_groups,
G.Shark Jeong8b7cfbe2012-09-12 20:05:50 +0800356 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;
361 }
G.Shark Jeong8b7cfbe2012-09-12 20:05:50 +0800362
363 /* torch */
G.Shark Jeong8b7cfbe2012-09-12 20:05:50 +0800364 chip->cdev_torch.name = "torch";
365 chip->cdev_torch.max_brightness = 8;
Andrew Lunnbb58cc82015-08-20 12:05:54 +0200366 chip->cdev_torch.brightness_set_blocking = lm3642_torch_brightness_set;
Kim, Milo313bf0b2013-03-14 04:29:26 -0700367 chip->cdev_torch.default_trigger = "torch";
Johan Hovoldfe29a3b2014-06-25 10:08:48 -0700368 chip->cdev_torch.groups = lm3642_torch_groups,
G.Shark Jeong8b7cfbe2012-09-12 20:05:50 +0800369 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;
374 }
G.Shark Jeong8b7cfbe2012-09-12 20:05:50 +0800375
376 /* indicator */
G.Shark Jeong8b7cfbe2012-09-12 20:05:50 +0800377 chip->cdev_indicator.name = "indicator";
378 chip->cdev_indicator.max_brightness = 8;
Andrew Lunnbb58cc82015-08-20 12:05:54 +0200379 chip->cdev_indicator.brightness_set_blocking =
380 lm3642_indicator_brightness_set;
G.Shark Jeong8b7cfbe2012-09-12 20:05:50 +0800381 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;
386 }
387
388 dev_info(&client->dev, "LM3642 is initialized\n");
389 return 0;
390
391err_create_indicator_file:
G.Shark Jeong8b7cfbe2012-09-12 20:05:50 +0800392 led_classdev_unregister(&chip->cdev_torch);
393err_create_torch_file:
G.Shark Jeong8b7cfbe2012-09-12 20:05:50 +0800394 led_classdev_unregister(&chip->cdev_flash);
395err_out:
396 return err;
397}
398
Bill Pemberton678e8a62012-11-19 13:26:00 -0500399static int lm3642_remove(struct i2c_client *client)
G.Shark Jeong8b7cfbe2012-09-12 20:05:50 +0800400{
401 struct lm3642_chip_data *chip = i2c_get_clientdata(client);
402
403 led_classdev_unregister(&chip->cdev_indicator);
G.Shark Jeong8b7cfbe2012-09-12 20:05:50 +0800404 led_classdev_unregister(&chip->cdev_torch);
G.Shark Jeong8b7cfbe2012-09-12 20:05:50 +0800405 led_classdev_unregister(&chip->cdev_flash);
G.Shark Jeong8b7cfbe2012-09-12 20:05:50 +0800406 regmap_write(chip->regmap, REG_ENABLE, 0);
407 return 0;
408}
409
410static const struct i2c_device_id lm3642_id[] = {
411 {LM3642_NAME, 0},
412 {}
413};
414
415MODULE_DEVICE_TABLE(i2c, lm3642_id);
416
417static struct i2c_driver lm3642_i2c_driver = {
418 .driver = {
419 .name = LM3642_NAME,
G.Shark Jeong8b7cfbe2012-09-12 20:05:50 +0800420 .pm = NULL,
421 },
422 .probe = lm3642_probe,
Bill Pembertondf07cf82012-11-19 13:20:20 -0500423 .remove = lm3642_remove,
G.Shark Jeong8b7cfbe2012-09-12 20:05:50 +0800424 .id_table = lm3642_id,
425};
426
427module_i2c_driver(lm3642_i2c_driver);
428
429MODULE_DESCRIPTION("Texas Instruments Flash Lighting driver for LM3642");
430MODULE_AUTHOR("Daniel Jeong <daniel.jeong@ti.com>");
431MODULE_AUTHOR("G.Shark Jeong <gshark.jeong@gmail.com>");
432MODULE_LICENSE("GPL v2");