Source
9
9
10
10
#include <linux/module.h>
11
11
#include <linux/delay.h>
12
12
#include <linux/i2c.h>
13
13
#include <linux/gpio.h>
14
14
#include <linux/leds.h>
15
15
#include <linux/slab.h>
16
16
#include <linux/platform_device.h>
17
17
#include <linux/fs.h>
18
18
#include <linux/regmap.h>
19
-
#include <linux/workqueue.h>
20
19
#include <linux/platform_data/leds-lm355x.h>
21
20
22
21
enum lm355x_type {
23
22
CHIP_LM3554 = 0,
24
23
CHIP_LM3556,
25
24
};
26
25
27
26
enum lm355x_regs {
28
27
REG_FLAG = 0,
29
28
REG_TORCH_CFG,
52
51
};
53
52
54
53
struct lm355x_chip_data {
55
54
struct device *dev;
56
55
enum lm355x_type type;
57
56
58
57
struct led_classdev cdev_flash;
59
58
struct led_classdev cdev_torch;
60
59
struct led_classdev cdev_indicator;
61
60
62
-
struct work_struct work_flash;
63
-
struct work_struct work_torch;
64
-
struct work_struct work_indicator;
65
-
66
-
u8 br_flash;
67
-
u8 br_torch;
68
-
u8 br_indicator;
69
-
70
61
struct lm355x_platform_data *pdata;
71
62
struct regmap *regmap;
72
63
struct mutex lock;
73
64
74
65
unsigned int last_flag;
75
66
struct lm355x_reg_data *regs;
76
67
};
77
68
78
69
/* specific indicator function for lm3556 */
79
70
enum lm3556_indic_pulse_time {
197
188
return -ENODATA;
198
189
}
199
190
200
191
return ret;
201
192
out:
202
193
dev_err(chip->dev, "%s:i2c access fail to register\n", __func__);
203
194
return ret;
204
195
}
205
196
206
197
/* chip control */
207
-
static void lm355x_control(struct lm355x_chip_data *chip,
198
+
static int lm355x_control(struct lm355x_chip_data *chip,
208
199
u8 brightness, enum lm355x_mode opmode)
209
200
{
210
201
int ret;
211
202
unsigned int reg_val;
212
203
struct lm355x_platform_data *pdata = chip->pdata;
213
204
struct lm355x_reg_data *preg = chip->regs;
214
205
215
206
ret = regmap_read(chip->regmap, preg[REG_FLAG].regno, &chip->last_flag);
216
207
if (ret < 0)
217
208
goto out;
294
285
0x01 <<
295
286
preg[REG_INDI_CFG].shift);
296
287
if (ret < 0)
297
288
goto out;
298
289
opmode = MODE_SHDN;
299
290
}
300
291
break;
301
292
case MODE_SHDN:
302
293
break;
303
294
default:
304
-
return;
295
+
return -EINVAL;
305
296
}
306
297
/* operation mode control */
307
298
ret = regmap_update_bits(chip->regmap, preg[REG_OPMODE].regno,
308
299
preg[REG_OPMODE].mask,
309
300
opmode << preg[REG_OPMODE].shift);
310
301
if (ret < 0)
311
302
goto out;
312
-
return;
303
+
return ret;
313
304
out:
314
305
dev_err(chip->dev, "%s:i2c access fail to register\n", __func__);
315
-
return;
306
+
return ret;
316
307
}
317
308
318
309
/* torch */
319
-
static void lm355x_deferred_torch_brightness_set(struct work_struct *work)
320
-
{
321
-
struct lm355x_chip_data *chip =
322
-
container_of(work, struct lm355x_chip_data, work_torch);
323
310
324
-
mutex_lock(&chip->lock);
325
-
lm355x_control(chip, chip->br_torch, MODE_TORCH);
326
-
mutex_unlock(&chip->lock);
327
-
}
328
-
329
-
static void lm355x_torch_brightness_set(struct led_classdev *cdev,
311
+
static int lm355x_torch_brightness_set(struct led_classdev *cdev,
330
312
enum led_brightness brightness)
331
313
{
332
314
struct lm355x_chip_data *chip =
333
315
container_of(cdev, struct lm355x_chip_data, cdev_torch);
334
-
335
-
chip->br_torch = brightness;
336
-
schedule_work(&chip->work_torch);
337
-
}
338
-
339
-
/* flash */
340
-
static void lm355x_deferred_strobe_brightness_set(struct work_struct *work)
341
-
{
342
-
struct lm355x_chip_data *chip =
343
-
container_of(work, struct lm355x_chip_data, work_flash);
316
+
int ret;
344
317
345
318
mutex_lock(&chip->lock);
346
-
lm355x_control(chip, chip->br_flash, MODE_FLASH);
319
+
ret = lm355x_control(chip, brightness, MODE_TORCH);
347
320
mutex_unlock(&chip->lock);
321
+
return ret;
348
322
}
349
323
350
-
static void lm355x_strobe_brightness_set(struct led_classdev *cdev,
324
+
/* flash */
325
+
326
+
static int lm355x_strobe_brightness_set(struct led_classdev *cdev,
351
327
enum led_brightness brightness)
352
328
{
353
329
struct lm355x_chip_data *chip =
354
330
container_of(cdev, struct lm355x_chip_data, cdev_flash);
355
-
356
-
chip->br_flash = brightness;
357
-
schedule_work(&chip->work_flash);
358
-
}
359
-
360
-
/* indicator */
361
-
static void lm355x_deferred_indicator_brightness_set(struct work_struct *work)
362
-
{
363
-
struct lm355x_chip_data *chip =
364
-
container_of(work, struct lm355x_chip_data, work_indicator);
331
+
int ret;
365
332
366
333
mutex_lock(&chip->lock);
367
-
lm355x_control(chip, chip->br_indicator, MODE_INDIC);
334
+
ret = lm355x_control(chip, brightness, MODE_FLASH);
368
335
mutex_unlock(&chip->lock);
336
+
return ret;
369
337
}
370
338
371
-
static void lm355x_indicator_brightness_set(struct led_classdev *cdev,
339
+
/* indicator */
340
+
341
+
static int lm355x_indicator_brightness_set(struct led_classdev *cdev,
372
342
enum led_brightness brightness)
373
343
{
374
344
struct lm355x_chip_data *chip =
375
345
container_of(cdev, struct lm355x_chip_data, cdev_indicator);
346
+
int ret;
376
347
377
-
chip->br_indicator = brightness;
378
-
schedule_work(&chip->work_indicator);
348
+
mutex_lock(&chip->lock);
349
+
ret = lm355x_control(chip, brightness, MODE_INDIC);
350
+
mutex_unlock(&chip->lock);
351
+
return ret;
379
352
}
380
353
381
354
/* indicator pattern only for lm3556*/
382
355
static ssize_t lm3556_indicator_pattern_store(struct device *dev,
383
356
struct device_attribute *attr,
384
357
const char *buf, size_t size)
385
358
{
386
359
ssize_t ret;
387
360
struct led_classdev *led_cdev = dev_get_drvdata(dev);
388
361
struct lm355x_chip_data *chip =
472
445
}
473
446
474
447
mutex_init(&chip->lock);
475
448
i2c_set_clientdata(client, chip);
476
449
477
450
err = lm355x_chip_init(chip);
478
451
if (err < 0)
479
452
goto err_out;
480
453
481
454
/* flash */
482
-
INIT_WORK(&chip->work_flash, lm355x_deferred_strobe_brightness_set);
483
455
chip->cdev_flash.name = "flash";
484
456
chip->cdev_flash.max_brightness = 16;
485
-
chip->cdev_flash.brightness_set = lm355x_strobe_brightness_set;
457
+
chip->cdev_flash.brightness_set_blocking = lm355x_strobe_brightness_set;
486
458
chip->cdev_flash.default_trigger = "flash";
487
459
err = led_classdev_register((struct device *)
488
460
&client->dev, &chip->cdev_flash);
489
461
if (err < 0)
490
462
goto err_out;
491
463
/* torch */
492
-
INIT_WORK(&chip->work_torch, lm355x_deferred_torch_brightness_set);
493
464
chip->cdev_torch.name = "torch";
494
465
chip->cdev_torch.max_brightness = 8;
495
-
chip->cdev_torch.brightness_set = lm355x_torch_brightness_set;
466
+
chip->cdev_torch.brightness_set_blocking = lm355x_torch_brightness_set;
496
467
chip->cdev_torch.default_trigger = "torch";
497
468
err = led_classdev_register((struct device *)
498
469
&client->dev, &chip->cdev_torch);
499
470
if (err < 0)
500
471
goto err_create_torch_file;
501
472
/* indicator */
502
-
INIT_WORK(&chip->work_indicator,
503
-
lm355x_deferred_indicator_brightness_set);
504
473
chip->cdev_indicator.name = "indicator";
505
474
if (id->driver_data == CHIP_LM3554)
506
475
chip->cdev_indicator.max_brightness = 4;
507
476
else
508
477
chip->cdev_indicator.max_brightness = 8;
509
-
chip->cdev_indicator.brightness_set = lm355x_indicator_brightness_set;
478
+
chip->cdev_indicator.brightness_set_blocking =
479
+
lm355x_indicator_brightness_set;
510
480
/* indicator pattern control only for LM3556 */
511
481
if (id->driver_data == CHIP_LM3556)
512
482
chip->cdev_indicator.groups = lm355x_indicator_groups;
513
483
err = led_classdev_register((struct device *)
514
484
&client->dev, &chip->cdev_indicator);
515
485
if (err < 0)
516
486
goto err_create_indicator_file;
517
487
518
488
dev_info(&client->dev, "%s is initialized\n",
519
489
lm355x_name[id->driver_data]);
527
497
return err;
528
498
}
529
499
530
500
static int lm355x_remove(struct i2c_client *client)
531
501
{
532
502
struct lm355x_chip_data *chip = i2c_get_clientdata(client);
533
503
struct lm355x_reg_data *preg = chip->regs;
534
504
535
505
regmap_write(chip->regmap, preg[REG_OPMODE].regno, 0);
536
506
led_classdev_unregister(&chip->cdev_indicator);
537
-
flush_work(&chip->work_indicator);
538
507
led_classdev_unregister(&chip->cdev_torch);
539
-
flush_work(&chip->work_torch);
540
508
led_classdev_unregister(&chip->cdev_flash);
541
-
flush_work(&chip->work_flash);
542
509
dev_info(&client->dev, "%s is removed\n", lm355x_name[chip->type]);
543
510
544
511
return 0;
545
512
}
546
513
547
514
static const struct i2c_device_id lm355x_id[] = {
548
515
{LM3554_NAME, CHIP_LM3554},
549
516
{LM3556_NAME, CHIP_LM3556},
550
517
{}
551
518
};