blob: 480575442ed876216a7eeec07cf78383532601c1 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
G.Shark Jeong8b7cfbe2012-09-12 20:05:50 +08002/*
3* Simple driver for Texas Instruments LM3642 LED Flash driver chip
4* Copyright (C) 2012 Texas Instruments
G.Shark Jeong8b7cfbe2012-09-12 20:05:50 +08005*/
6#include <linux/module.h>
7#include <linux/delay.h>
8#include <linux/i2c.h>
9#include <linux/leds.h>
10#include <linux/slab.h>
11#include <linux/platform_device.h>
12#include <linux/fs.h>
13#include <linux/regmap.h>
G.Shark Jeong8b7cfbe2012-09-12 20:05:50 +080014#include <linux/platform_data/leds-lm3642.h>
15
16#define REG_FILT_TIME (0x0)
17#define REG_IVFM_MODE (0x1)
18#define REG_TORCH_TIME (0x6)
19#define REG_FLASH (0x8)
20#define REG_I_CTRL (0x9)
21#define REG_ENABLE (0xA)
22#define REG_FLAG (0xB)
23#define REG_MAX (0xB)
24
25#define UVLO_EN_SHIFT (7)
26#define IVM_D_TH_SHIFT (2)
27#define TORCH_RAMP_UP_TIME_SHIFT (3)
28#define TORCH_RAMP_DN_TIME_SHIFT (0)
29#define INDUCTOR_I_LIMIT_SHIFT (6)
30#define FLASH_RAMP_TIME_SHIFT (3)
31#define FLASH_TOUT_TIME_SHIFT (0)
32#define TORCH_I_SHIFT (4)
33#define FLASH_I_SHIFT (0)
34#define IVFM_SHIFT (7)
35#define TX_PIN_EN_SHIFT (6)
36#define STROBE_PIN_EN_SHIFT (5)
37#define TORCH_PIN_EN_SHIFT (4)
38#define MODE_BITS_SHIFT (0)
39
40#define UVLO_EN_MASK (0x1)
41#define IVM_D_TH_MASK (0x7)
42#define TORCH_RAMP_UP_TIME_MASK (0x7)
43#define TORCH_RAMP_DN_TIME_MASK (0x7)
44#define INDUCTOR_I_LIMIT_MASK (0x1)
45#define FLASH_RAMP_TIME_MASK (0x7)
46#define FLASH_TOUT_TIME_MASK (0x7)
47#define TORCH_I_MASK (0x7)
48#define FLASH_I_MASK (0xF)
49#define IVFM_MASK (0x1)
50#define TX_PIN_EN_MASK (0x1)
51#define STROBE_PIN_EN_MASK (0x1)
52#define TORCH_PIN_EN_MASK (0x1)
53#define MODE_BITS_MASK (0x73)
54#define EX_PIN_CONTROL_MASK (0x71)
55#define EX_PIN_ENABLE_MASK (0x70)
56
57enum lm3642_mode {
58 MODES_STASNDBY = 0,
59 MODES_INDIC,
60 MODES_TORCH,
61 MODES_FLASH
62};
63
64struct lm3642_chip_data {
65 struct device *dev;
66
67 struct led_classdev cdev_flash;
68 struct led_classdev cdev_torch;
69 struct led_classdev cdev_indicator;
70
G.Shark Jeong8b7cfbe2012-09-12 20:05:50 +080071 u8 br_flash;
72 u8 br_torch;
73 u8 br_indicator;
74
75 enum lm3642_torch_pin_enable torch_pin;
76 enum lm3642_strobe_pin_enable strobe_pin;
77 enum lm3642_tx_pin_enable tx_pin;
78
79 struct lm3642_platform_data *pdata;
80 struct regmap *regmap;
81 struct mutex lock;
82
83 unsigned int last_flag;
84};
85
86/* chip initialize */
Bill Pemberton98ea1ea2012-11-19 13:23:02 -050087static int lm3642_chip_init(struct lm3642_chip_data *chip)
G.Shark Jeong8b7cfbe2012-09-12 20:05:50 +080088{
G.Shark Jeong8b7cfbe2012-09-12 20:05:50 +080089 int ret;
90 struct lm3642_platform_data *pdata = chip->pdata;
91
92 /* set enable register */
Axel Line76a3222012-09-22 14:40:14 +080093 ret = regmap_update_bits(chip->regmap, REG_ENABLE, EX_PIN_ENABLE_MASK,
94 pdata->tx_pin);
G.Shark Jeong8b7cfbe2012-09-12 20:05:50 +080095 if (ret < 0)
Axel Line76a3222012-09-22 14:40:14 +080096 dev_err(chip->dev, "Failed to update REG_ENABLE Register\n");
G.Shark Jeong8b7cfbe2012-09-12 20:05:50 +080097 return ret;
98}
99
100/* chip control */
101static int lm3642_control(struct lm3642_chip_data *chip,
102 u8 brightness, enum lm3642_mode opmode)
103{
104 int ret;
105
106 ret = regmap_read(chip->regmap, REG_FLAG, &chip->last_flag);
107 if (ret < 0) {
108 dev_err(chip->dev, "Failed to read REG_FLAG Register\n");
109 goto out;
110 }
111
112 if (chip->last_flag)
113 dev_info(chip->dev, "Last FLAG is 0x%x\n", chip->last_flag);
114
115 /* brightness 0 means off-state */
116 if (!brightness)
117 opmode = MODES_STASNDBY;
118
119 switch (opmode) {
120 case MODES_TORCH:
121 ret = regmap_update_bits(chip->regmap, REG_I_CTRL,
122 TORCH_I_MASK << TORCH_I_SHIFT,
123 (brightness - 1) << TORCH_I_SHIFT);
124
125 if (chip->torch_pin)
126 opmode |= (TORCH_PIN_EN_MASK << TORCH_PIN_EN_SHIFT);
127 break;
128
129 case MODES_FLASH:
130 ret = regmap_update_bits(chip->regmap, REG_I_CTRL,
131 FLASH_I_MASK << FLASH_I_SHIFT,
132 (brightness - 1) << FLASH_I_SHIFT);
133
134 if (chip->strobe_pin)
135 opmode |= (STROBE_PIN_EN_MASK << STROBE_PIN_EN_SHIFT);
136 break;
137
138 case MODES_INDIC:
139 ret = regmap_update_bits(chip->regmap, REG_I_CTRL,
140 TORCH_I_MASK << TORCH_I_SHIFT,
141 (brightness - 1) << TORCH_I_SHIFT);
142 break;
143
144 case MODES_STASNDBY:
145
146 break;
147
148 default:
149 return ret;
150 }
151 if (ret < 0) {
152 dev_err(chip->dev, "Failed to write REG_I_CTRL Register\n");
153 goto out;
154 }
155
156 if (chip->tx_pin)
157 opmode |= (TX_PIN_EN_MASK << TX_PIN_EN_SHIFT);
158
159 ret = regmap_update_bits(chip->regmap, REG_ENABLE,
160 MODE_BITS_MASK << MODE_BITS_SHIFT,
161 opmode << MODE_BITS_SHIFT);
162out:
163 return ret;
164}
165
166/* torch */
167
168/* torch pin config for lm3642*/
169static ssize_t lm3642_torch_pin_store(struct device *dev,
Jingoo Han85b4b752013-01-21 21:57:20 -0800170 struct device_attribute *attr,
G.Shark Jeong8b7cfbe2012-09-12 20:05:50 +0800171 const char *buf, size_t size)
172{
173 ssize_t ret;
174 struct led_classdev *led_cdev = dev_get_drvdata(dev);
175 struct lm3642_chip_data *chip =
176 container_of(led_cdev, struct lm3642_chip_data, cdev_indicator);
177 unsigned int state;
178
179 ret = kstrtouint(buf, 10, &state);
180 if (ret)
181 goto out_strtoint;
182 if (state != 0)
183 state = 0x01 << TORCH_PIN_EN_SHIFT;
184
185 chip->torch_pin = state;
186 ret = regmap_update_bits(chip->regmap, REG_ENABLE,
187 TORCH_PIN_EN_MASK << TORCH_PIN_EN_SHIFT,
188 state);
189 if (ret < 0)
190 goto out;
191
192 return size;
193out:
194 dev_err(chip->dev, "%s:i2c access fail to register\n", __func__);
Axel Lineccb6632012-10-07 20:54:23 -0700195 return ret;
G.Shark Jeong8b7cfbe2012-09-12 20:05:50 +0800196out_strtoint:
197 dev_err(chip->dev, "%s: fail to change str to int\n", __func__);
Axel Lineccb6632012-10-07 20:54:23 -0700198 return ret;
G.Shark Jeong8b7cfbe2012-09-12 20:05:50 +0800199}
200
Axel Lin5cce0102012-10-29 01:41:46 -0700201static DEVICE_ATTR(torch_pin, S_IWUSR, NULL, lm3642_torch_pin_store);
G.Shark Jeong8b7cfbe2012-09-12 20:05:50 +0800202
Andrew Lunnbb58cc82015-08-20 12:05:54 +0200203static int lm3642_torch_brightness_set(struct led_classdev *cdev,
G.Shark Jeong8b7cfbe2012-09-12 20:05:50 +0800204 enum led_brightness brightness)
205{
206 struct lm3642_chip_data *chip =
207 container_of(cdev, struct lm3642_chip_data, cdev_torch);
Andrew Lunnbb58cc82015-08-20 12:05:54 +0200208 int ret;
G.Shark Jeong8b7cfbe2012-09-12 20:05:50 +0800209
Andrew Lunnbb58cc82015-08-20 12:05:54 +0200210 mutex_lock(&chip->lock);
G.Shark Jeong8b7cfbe2012-09-12 20:05:50 +0800211 chip->br_torch = brightness;
Andrew Lunnbb58cc82015-08-20 12:05:54 +0200212 ret = lm3642_control(chip, chip->br_torch, MODES_TORCH);
213 mutex_unlock(&chip->lock);
214 return ret;
G.Shark Jeong8b7cfbe2012-09-12 20:05:50 +0800215}
216
217/* flash */
218
219/* strobe pin config for lm3642*/
220static ssize_t lm3642_strobe_pin_store(struct device *dev,
Jingoo Han85b4b752013-01-21 21:57:20 -0800221 struct device_attribute *attr,
G.Shark Jeong8b7cfbe2012-09-12 20:05:50 +0800222 const char *buf, size_t size)
223{
224 ssize_t ret;
225 struct led_classdev *led_cdev = dev_get_drvdata(dev);
226 struct lm3642_chip_data *chip =
227 container_of(led_cdev, struct lm3642_chip_data, cdev_indicator);
228 unsigned int state;
229
230 ret = kstrtouint(buf, 10, &state);
231 if (ret)
232 goto out_strtoint;
233 if (state != 0)
234 state = 0x01 << STROBE_PIN_EN_SHIFT;
235
236 chip->strobe_pin = state;
237 ret = regmap_update_bits(chip->regmap, REG_ENABLE,
238 STROBE_PIN_EN_MASK << STROBE_PIN_EN_SHIFT,
239 state);
240 if (ret < 0)
241 goto out;
242
243 return size;
244out:
245 dev_err(chip->dev, "%s:i2c access fail to register\n", __func__);
Axel Lineccb6632012-10-07 20:54:23 -0700246 return ret;
G.Shark Jeong8b7cfbe2012-09-12 20:05:50 +0800247out_strtoint:
248 dev_err(chip->dev, "%s: fail to change str to int\n", __func__);
Axel Lineccb6632012-10-07 20:54:23 -0700249 return ret;
G.Shark Jeong8b7cfbe2012-09-12 20:05:50 +0800250}
251
Axel Lin5cce0102012-10-29 01:41:46 -0700252static DEVICE_ATTR(strobe_pin, S_IWUSR, NULL, lm3642_strobe_pin_store);
G.Shark Jeong8b7cfbe2012-09-12 20:05:50 +0800253
Andrew Lunnbb58cc82015-08-20 12:05:54 +0200254static int lm3642_strobe_brightness_set(struct led_classdev *cdev,
G.Shark Jeong8b7cfbe2012-09-12 20:05:50 +0800255 enum led_brightness brightness)
256{
257 struct lm3642_chip_data *chip =
258 container_of(cdev, struct lm3642_chip_data, cdev_flash);
Andrew Lunnbb58cc82015-08-20 12:05:54 +0200259 int ret;
G.Shark Jeong8b7cfbe2012-09-12 20:05:50 +0800260
Andrew Lunnbb58cc82015-08-20 12:05:54 +0200261 mutex_lock(&chip->lock);
G.Shark Jeong8b7cfbe2012-09-12 20:05:50 +0800262 chip->br_flash = brightness;
Andrew Lunnbb58cc82015-08-20 12:05:54 +0200263 ret = lm3642_control(chip, chip->br_flash, MODES_FLASH);
264 mutex_unlock(&chip->lock);
265 return ret;
G.Shark Jeong8b7cfbe2012-09-12 20:05:50 +0800266}
267
268/* indicator */
Andrew Lunnbb58cc82015-08-20 12:05:54 +0200269static int lm3642_indicator_brightness_set(struct led_classdev *cdev,
G.Shark Jeong8b7cfbe2012-09-12 20:05:50 +0800270 enum led_brightness brightness)
271{
272 struct lm3642_chip_data *chip =
273 container_of(cdev, struct lm3642_chip_data, cdev_indicator);
Andrew Lunnbb58cc82015-08-20 12:05:54 +0200274 int ret;
G.Shark Jeong8b7cfbe2012-09-12 20:05:50 +0800275
Andrew Lunnbb58cc82015-08-20 12:05:54 +0200276 mutex_lock(&chip->lock);
G.Shark Jeong8b7cfbe2012-09-12 20:05:50 +0800277 chip->br_indicator = brightness;
Andrew Lunnbb58cc82015-08-20 12:05:54 +0200278 ret = lm3642_control(chip, chip->br_indicator, MODES_INDIC);
279 mutex_unlock(&chip->lock);
280 return ret;
G.Shark Jeong8b7cfbe2012-09-12 20:05:50 +0800281}
282
283static const struct regmap_config lm3642_regmap = {
284 .reg_bits = 8,
285 .val_bits = 8,
286 .max_register = REG_MAX,
287};
288
Johan Hovoldfe29a3b2014-06-25 10:08:48 -0700289static struct attribute *lm3642_flash_attrs[] = {
290 &dev_attr_strobe_pin.attr,
291 NULL
292};
293ATTRIBUTE_GROUPS(lm3642_flash);
294
295static struct attribute *lm3642_torch_attrs[] = {
296 &dev_attr_torch_pin.attr,
297 NULL
298};
299ATTRIBUTE_GROUPS(lm3642_torch);
300
Bill Pemberton98ea1ea2012-11-19 13:23:02 -0500301static int lm3642_probe(struct i2c_client *client,
G.Shark Jeong8b7cfbe2012-09-12 20:05:50 +0800302 const struct i2c_device_id *id)
303{
Jingoo Han87aae1e2013-07-30 01:07:35 -0700304 struct lm3642_platform_data *pdata = dev_get_platdata(&client->dev);
G.Shark Jeong8b7cfbe2012-09-12 20:05:50 +0800305 struct lm3642_chip_data *chip;
306
307 int err;
308
309 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
310 dev_err(&client->dev, "i2c functionality check fail.\n");
311 return -EOPNOTSUPP;
312 }
313
314 if (pdata == NULL) {
315 dev_err(&client->dev, "needs Platform Data.\n");
316 return -ENODATA;
317 }
318
319 chip = devm_kzalloc(&client->dev,
320 sizeof(struct lm3642_chip_data), GFP_KERNEL);
321 if (!chip)
322 return -ENOMEM;
323
324 chip->dev = &client->dev;
325 chip->pdata = pdata;
326
327 chip->tx_pin = pdata->tx_pin;
328 chip->torch_pin = pdata->torch_pin;
329 chip->strobe_pin = pdata->strobe_pin;
330
331 chip->regmap = devm_regmap_init_i2c(client, &lm3642_regmap);
332 if (IS_ERR(chip->regmap)) {
333 err = PTR_ERR(chip->regmap);
334 dev_err(&client->dev, "Failed to allocate register map: %d\n",
335 err);
336 return err;
337 }
338
339 mutex_init(&chip->lock);
340 i2c_set_clientdata(client, chip);
341
342 err = lm3642_chip_init(chip);
343 if (err < 0)
344 goto err_out;
345
346 /* flash */
G.Shark Jeong8b7cfbe2012-09-12 20:05:50 +0800347 chip->cdev_flash.name = "flash";
348 chip->cdev_flash.max_brightness = 16;
Andrew Lunnbb58cc82015-08-20 12:05:54 +0200349 chip->cdev_flash.brightness_set_blocking = lm3642_strobe_brightness_set;
Kim, Milo313bf0b2013-03-14 04:29:26 -0700350 chip->cdev_flash.default_trigger = "flash";
Johan Hovoldfe29a3b2014-06-25 10:08:48 -0700351 chip->cdev_flash.groups = lm3642_flash_groups,
G.Shark Jeong8b7cfbe2012-09-12 20:05:50 +0800352 err = led_classdev_register((struct device *)
353 &client->dev, &chip->cdev_flash);
354 if (err < 0) {
355 dev_err(chip->dev, "failed to register flash\n");
356 goto err_out;
357 }
G.Shark Jeong8b7cfbe2012-09-12 20:05:50 +0800358
359 /* torch */
G.Shark Jeong8b7cfbe2012-09-12 20:05:50 +0800360 chip->cdev_torch.name = "torch";
361 chip->cdev_torch.max_brightness = 8;
Andrew Lunnbb58cc82015-08-20 12:05:54 +0200362 chip->cdev_torch.brightness_set_blocking = lm3642_torch_brightness_set;
Kim, Milo313bf0b2013-03-14 04:29:26 -0700363 chip->cdev_torch.default_trigger = "torch";
Johan Hovoldfe29a3b2014-06-25 10:08:48 -0700364 chip->cdev_torch.groups = lm3642_torch_groups,
G.Shark Jeong8b7cfbe2012-09-12 20:05:50 +0800365 err = led_classdev_register((struct device *)
366 &client->dev, &chip->cdev_torch);
367 if (err < 0) {
368 dev_err(chip->dev, "failed to register torch\n");
369 goto err_create_torch_file;
370 }
G.Shark Jeong8b7cfbe2012-09-12 20:05:50 +0800371
372 /* indicator */
G.Shark Jeong8b7cfbe2012-09-12 20:05:50 +0800373 chip->cdev_indicator.name = "indicator";
374 chip->cdev_indicator.max_brightness = 8;
Andrew Lunnbb58cc82015-08-20 12:05:54 +0200375 chip->cdev_indicator.brightness_set_blocking =
376 lm3642_indicator_brightness_set;
G.Shark Jeong8b7cfbe2012-09-12 20:05:50 +0800377 err = led_classdev_register((struct device *)
378 &client->dev, &chip->cdev_indicator);
379 if (err < 0) {
380 dev_err(chip->dev, "failed to register indicator\n");
381 goto err_create_indicator_file;
382 }
383
384 dev_info(&client->dev, "LM3642 is initialized\n");
385 return 0;
386
387err_create_indicator_file:
G.Shark Jeong8b7cfbe2012-09-12 20:05:50 +0800388 led_classdev_unregister(&chip->cdev_torch);
389err_create_torch_file:
G.Shark Jeong8b7cfbe2012-09-12 20:05:50 +0800390 led_classdev_unregister(&chip->cdev_flash);
391err_out:
392 return err;
393}
394
Bill Pemberton678e8a62012-11-19 13:26:00 -0500395static int lm3642_remove(struct i2c_client *client)
G.Shark Jeong8b7cfbe2012-09-12 20:05:50 +0800396{
397 struct lm3642_chip_data *chip = i2c_get_clientdata(client);
398
399 led_classdev_unregister(&chip->cdev_indicator);
G.Shark Jeong8b7cfbe2012-09-12 20:05:50 +0800400 led_classdev_unregister(&chip->cdev_torch);
G.Shark Jeong8b7cfbe2012-09-12 20:05:50 +0800401 led_classdev_unregister(&chip->cdev_flash);
G.Shark Jeong8b7cfbe2012-09-12 20:05:50 +0800402 regmap_write(chip->regmap, REG_ENABLE, 0);
403 return 0;
404}
405
406static const struct i2c_device_id lm3642_id[] = {
407 {LM3642_NAME, 0},
408 {}
409};
410
411MODULE_DEVICE_TABLE(i2c, lm3642_id);
412
413static struct i2c_driver lm3642_i2c_driver = {
414 .driver = {
415 .name = LM3642_NAME,
G.Shark Jeong8b7cfbe2012-09-12 20:05:50 +0800416 .pm = NULL,
417 },
418 .probe = lm3642_probe,
Bill Pembertondf07cf82012-11-19 13:20:20 -0500419 .remove = lm3642_remove,
G.Shark Jeong8b7cfbe2012-09-12 20:05:50 +0800420 .id_table = lm3642_id,
421};
422
423module_i2c_driver(lm3642_i2c_driver);
424
425MODULE_DESCRIPTION("Texas Instruments Flash Lighting driver for LM3642");
426MODULE_AUTHOR("Daniel Jeong <daniel.jeong@ti.com>");
427MODULE_AUTHOR("G.Shark Jeong <gshark.jeong@gmail.com>");
428MODULE_LICENSE("GPL v2");