ti-ss-flash.c correct missed value reference
[mlp-open-source/kernel.git] / drivers / media / i2c / ti-ss-flash.c
1 /*
2  * drivers/media/i2c/ti-ss-flash.c
3  * General device driver for Single String FLASH LED Drivers of TI
4  * It covers lm3556, lm3561, lm3642, lm3646 and lm3648.
5  *
6  * Copyright (C) 2015 Texas Instruments
7  *
8  * Contact: Daniel Jeong <gshark.jeong@gmail.com>
9  *                      Ldd-Mlp <ldd-mlp@list.ti.com>
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * version 2 as published by the Free Software Foundation.
14  *
15  */
17 #include <linux/delay.h>
18 #include <linux/i2c.h>
19 #include <linux/module.h>
20 #include <linux/of_device.h>
21 #include <linux/regmap.h>
22 #include <linux/slab.h>
23 #include <linux/videodev2.h>
24 #include <media/ti-ss-flash.h>
25 #include <media/v4l2-ctrls.h>
26 #include <media/v4l2-device.h>
28 /* operation mode */
29 enum led_opmode {
30         OPMODE_SHDN = 0x0,
31         OPMODE_INDI_IR,
32         OPMODE_TORCH,
33         OPMODE_FLASH,
34 };
36 /*
37  * register data
38  * @reg : register
39  * @mask : mask bits
40  * @shift : bit shift of data
41  */
42 struct ctrl_reg {
43         unsigned int reg;
44         unsigned int mask;
45         unsigned int shift;
46 };
48 /*
49  * unit data
50  * @min : min value of brightness or timeout
51  *        brightness : uA
52  *                timeout    : ms
53  * @step : step value of brightness or timeout
54  *        brightness : uA
55  *                timeout    : ms
56  * @knee: knee point of step of brightness/timeout
57  *        brightness : uA
58  *                timeout    : ms
59  * @knee_step : step value of brightness or timeout after knee point
60  *        brightness : uA
61  *                timeout    : ms
62  * @max : max value of brightness or timeout
63  *        brightness : uA
64  *                timeout    : ms
65  * @ctrl : register info to control brightness or timeout
66  */
67 struct ssflash_config {
68         unsigned int min;
69         unsigned int step;
70         unsigned int knee;
71         unsigned int knee_step;
72         unsigned int max;
73         struct ctrl_reg ctrl;
74 };
76 /*
77  * @reg : fault register
78  * @mask : fault mask bit
79  * @v4l2_fault : bit mapping to V4L2_FLASH_FAULT_
80  *               refer to include//uapi/linux/v4l2-controls.h
81  */
82 struct ssflash_fault {
83         unsigned int reg;
84         unsigned int mask;
85         unsigned int v4l2_fault;
86 };
88 #define NUM_V4L2_FAULT 9
90 /*
91  * ssflash data
92  * @name: device name
93  * @mode: operation mode control data
94  * @flash_br: flash brightness register and bit data
95  * @timeout: timeout control data
96  * @strobe: strobe control data
97  * @torch_br: torch brightness register and bit data
98  * @fault: fault data
99  * @func: initialize function
100  */
101 struct ssflash_data {
102         char *name;
103         struct ctrl_reg mode;
104         struct ssflash_config flash_br;
105         struct ssflash_config timeout;
106         struct ctrl_reg strobe;
108         struct ssflash_config torch_br;
109         struct ssflash_fault fault[NUM_V4L2_FAULT];
111         int (*func)(struct regmap *regmap);
112 };
114 /*
115  * struct ssflash_flash
116  * @dev: device
117  * @regmap: reg map for interface
118  * @ctrls_led: V4L2 contols
119  * @subdev_led: V4L2 subdev
120  * @data : chip control data
121  * @mode_reg: value of mode register
122  */
123 struct ssflash_flash {
124         struct device *dev;
125         struct regmap *regmap;
127         struct v4l2_ctrl_handler ctrls_led;
128         struct v4l2_subdev subdev_led;
129         const struct ssflash_data *data;
130         u8 mode_reg;
131 };
133 #define to_ssflash_flash(_ctrl) \
134         container_of(_ctrl->handler, struct ssflash_flash, ctrls_led)
136 static const struct ssflash_data flash_lm3556 = {
137         .name = LM3556_NAME,
138         .mode = {
139                 .reg = 0x0a, .mask = 0x03, .shift = 0
140         },
141         .flash_br = {
142                 .min = 93750, .step = 93750,
143                 .max = 1500000,
144                 .ctrl = {
145                         .reg = 0x09, .mask = 0xf, .shift = 0
146                 },
147         },
148         .timeout = {
149                 .min = 100,     .step = 100,
150                 .max = 800,
151                 .ctrl = {
152                         .reg = 0x08, .mask = 0x07, .shift = 0
153                 }
154         },
155         .torch_br = {
156                 .min = 46880, .step = 46880,
157                 .max = 375000,
158                 .ctrl = {
159                         .reg = 0x09, .mask = 0x70, .shift = 4
160                 }
161         },
162         .strobe = {
163                 .reg = 0x0a, .mask = 0x20, .shift = 5
164         },
165         .fault = {
166                 [0] = {
167                         .reg = 0x0b, .mask = 0x01,
168                         .v4l2_fault = V4L2_FLASH_FAULT_TIMEOUT,
169                 },
170                 [1] = {
171                         .reg = 0x0b, .mask = 0x02,
172                         .v4l2_fault = V4L2_FLASH_FAULT_OVER_TEMPERATURE,
173                 },
174                 [2] = {
175                         .reg = 0x0b, .mask = 0x04,
176                         .v4l2_fault = V4L2_FLASH_FAULT_SHORT_CIRCUIT,
177                 },
178                 [3] = {
179                         .reg = 0x0b, .mask = 0x08,
180                         .v4l2_fault = V4L2_FLASH_FAULT_OVER_VOLTAGE,
181                 },
183                 [4] = {
184                         .reg = 0x0b, .mask = 0x10,
185                         .v4l2_fault = V4L2_FLASH_FAULT_UNDER_VOLTAGE,
186                 },
187                 [5] = {
188                         .reg = 0x0b, .mask = 0x20,
189                         .v4l2_fault = V4L2_FLASH_FAULT_INPUT_VOLTAGE,
190                 },
191         },
192         .func = NULL
193 };
195 static const struct ssflash_data flash_lm3561 = {
196         .name = LM3561_NAME,
197         .mode = {
198                 .reg = 0x10, .mask = 0x03, .shift = 0
199         },
200         .flash_br = {
201                 .min = 36000, .step = 37600,
202                 .max = 600000,
203                 .ctrl = {
204                         .reg = 0xb0, .mask = 0xf, .shift = 0
205                 },
206         },
207         .timeout = {
208                 .min = 32,      .step = 32,
209                 .max = 1024,
210                 .ctrl = {
211                         .reg = 0xc0, .mask = 0x1f, .shift = 0
212                 }
213         },
214         .torch_br = {
215                 .min = 18000, .step = 18800,
216                 .max = 149600,
217                 .ctrl = {
218                         .reg = 0xa0, .mask = 0x07, .shift = 0
219                 }
220         },
221         .strobe = {
222                 .reg = 0xe0, .mask = 0x04, .shift = 2
223         },
224         .fault = {
225                 [0] = {
226                         .reg = 0xd0, .mask = 0x01,
227                         .v4l2_fault = V4L2_FLASH_FAULT_TIMEOUT,
228                 },
229                 [1] = {
230                         .reg = 0xd0, .mask = 0x02,
231                         .v4l2_fault = V4L2_FLASH_FAULT_OVER_TEMPERATURE,
232                 },
233                 [2] = {
234                         .reg = 0xd0, .mask = 0x04,
235                         .v4l2_fault = V4L2_FLASH_FAULT_SHORT_CIRCUIT,
236                 },
237                 [3] = {
238                         .reg = 0xd0, .mask = 0x20,
239                         .v4l2_fault = V4L2_FLASH_FAULT_LED_OVER_TEMPERATURE,
240                 },
241                 [4] = {
242                         .reg = 0xd0, .mask = 0x80,
243                         .v4l2_fault = V4L2_FLASH_FAULT_INPUT_VOLTAGE,
244                 },
245         },
246         .func = NULL
247 };
249 static const struct ssflash_data flash_lm3642 = {
250         .name = LM3642_NAME,
251         .mode = {
252                 .reg = 0x0a, .mask = 0x03, .shift = 0
253         },
254         .flash_br = {
255                 .min = 93750, .step = 93750,
256                 .max = 1500000,
257                 .ctrl = {
258                         .reg = 0x09, .mask = 0xf, .shift = 0
259                 },
260         },
261         .timeout = {
262                 .min = 100,     .step = 100,
263                 .max = 800,
264                 .ctrl = {
265                         .reg = 0x08, .mask = 0x07, .shift = 0
266                 }
267         },
268         .torch_br = {
269                 .min = 46880, .step = 46880,
270                 .max = 375000,
271                 .ctrl = {
272                         .reg = 0x09, .mask = 0x70, .shift = 4
273                 }
274         },
275         .strobe = {
276                 .reg = 0x0a, .mask = 0x20, .shift = 5
277         },
278         .fault = {
279                 [0] = {
280                         .reg = 0x0b, .mask = 0x01,
281                         .v4l2_fault = V4L2_FLASH_FAULT_TIMEOUT,
282                 },
283                 [1] = {
284                         .reg = 0x0b, .mask = 0x02,
285                         .v4l2_fault = V4L2_FLASH_FAULT_OVER_TEMPERATURE,
286                 },
287                 [2] = {
288                         .reg = 0x0b, .mask = 0x04,
289                         .v4l2_fault = V4L2_FLASH_FAULT_SHORT_CIRCUIT,
290                 },
291                 [3] = {
292                         .reg = 0x0b, .mask = 0x20,
293                         .v4l2_fault = V4L2_FLASH_FAULT_INPUT_VOLTAGE,
294                 },
295         },
296         .func = NULL
297 };
299 /*
300  * LM3646 is a dual string flash LED driver
301  * but there are no regiters to control LED2 directly.
302  * LED1 and LED2 are controlled through LED1 registers.
303  */
304 static int ssflash_lm3646_init(struct regmap *regmap)
306         int rval;
308         /*
309          *max flash & torch current
310          * LED2 = max current value - LED1 Current value
311          * if max current value < LED1 current value
312          *    LED1 = max current value
313          *    LED2 = off
314          */
315         rval = regmap_update_bits(regmap, 0x05, 0x0f, 0x0f);
316         rval |= regmap_update_bits(regmap, 0x05, 0x70, 0x07 << 4);
317         if(rval < 0)
318                 return rval;
319         return 0;
322 static const struct ssflash_data flash_lm3646 = {
323         .name = LM3646_NAME,
324         .mode = {
325                 .reg = 0x01, .mask = 0x03, .shift = 0
326         },
327         .flash_br = {
328                 .min = 0, .step = 23100,
329                 .knee = 23100, .knee_step = 11700,
330                 .max = 1500000,
331                 .ctrl = {
332                         .reg = 0x06, .mask = 0x7f, .shift = 0
333                 },
334         },
335         .timeout = {
336                 .min = 50,      .step = 100,
337                 .max = 400,
338                 .ctrl = {
339                         .reg = 0x04, .mask = 0x0f, .shift = 0
340                 }
341         },
342         .torch_br = {
343                 .min = 0, .step = 2600,
344                 .knee = 2600, .knee_step = 1400,
345                 .max = 187100,
346                 .ctrl = {
347                         .reg = 0x7, .mask = 0x7f, .shift = 0
348                 }
349         },
350         .strobe = {
351                 .reg = 0x6, .mask = 0x80, .shift = 7
352         },
353         .fault = {
354                 [0] = {
355                         .reg = 0x08, .mask = 0x01,
356                         .v4l2_fault = V4L2_FLASH_FAULT_TIMEOUT,
357                 },
358                 [1] = {
359                         .reg = 0x08, .mask = 0x02,
360                         .v4l2_fault = V4L2_FLASH_FAULT_SHORT_CIRCUIT,
361                 },
362                 [2] = {
363                         .reg = 0x08, .mask = 0x04,
364                         .v4l2_fault = V4L2_FLASH_FAULT_UNDER_VOLTAGE,
365                 },
366                 [3] = {
367                         .reg = 0x08, .mask = 0x08,
368                         .v4l2_fault = V4L2_FLASH_FAULT_INPUT_VOLTAGE,
369                 },
370                 [4] = {
371                         .reg = 0x08, .mask = 0x10,
372                         .v4l2_fault = V4L2_FLASH_FAULT_OVER_CURRENT,
373                 },
374                 [5] = {
375                         .reg = 0x08, .mask = 0x20,
376                         .v4l2_fault =V4L2_FLASH_FAULT_OVER_TEMPERATURE,
377                 },
378                 [6] = {
379                         .reg = 0x08, .mask = 0x40,
380                         .v4l2_fault = V4L2_FLASH_FAULT_LED_OVER_TEMPERATURE,
381                 },
382                 [7] = {
383                         .reg = 0x08, .mask = 0x80,
384                         .v4l2_fault = V4L2_FLASH_FAULT_OVER_VOLTAGE,
385                 },
386         },
387         .func = ssflash_lm3646_init
388 };
390 static int ssflash_lm3648_init(struct regmap *regmap)
392         int rval;
394         /* enable LED */
395         rval = regmap_update_bits(regmap, 0x01, 0x03, 0x03);
396         /* bit[7:6] must be set to 10b for proper operation */
397         rval |= regmap_update_bits(regmap, 0x03, 0xc0, 0x80);
398         /* bit[7] must be set to 1b for proper operation */
399         rval |= regmap_update_bits(regmap, 0x05, 0x80, 0x80);
400         if (rval < 0)
401                 return rval;
402         return 0;
405 static const struct ssflash_data flash_lm3648 = {
406         .name = LM3648_NAME,
407         .mode = {
408                 .reg = 0x01, .mask = 0x0c, .shift = 2
409         },
410         .flash_br = {
411                 .min = 21800, .step = 23450,
412                 .max = 1500000,
413                 .ctrl = {
414                         .reg = 0x03, .mask = 0x3f, .shift = 0
415                 },
416         },
417         .timeout = {
418                 .min = 10,      .step = 10,
419                 .knee = 100, .knee_step = 50,
420                 .max = 1024,
421                 .ctrl = {
422                         .reg = 0x08, .mask = 0x0f, .shift = 0
423                 }
424         },
425         .torch_br = {
426                 .min = 1954, .step = 2800,
427                 .max = 357600,
428                 .ctrl = {
429                         .reg = 0x5, .mask = 0x7f, .shift = 0
430                 }
431         },
432         .strobe = {
433                 .reg = 0x1, .mask = 0x20, .shift = 5
434         },
435         .fault = {
436                 [0] = {
437                         .reg = 0x0a, .mask = 0x01,
438                         .v4l2_fault = V4L2_FLASH_FAULT_TIMEOUT,
439                 },
440                 [1] = {
441                         .reg = 0x0a, .mask = 0x04,
442                         .v4l2_fault = V4L2_FLASH_FAULT_OVER_TEMPERATURE,
443                 },
444                 [2] = {
445                         .reg = 0x0a, .mask = 0x70,
446                         .v4l2_fault = V4L2_FLASH_FAULT_SHORT_CIRCUIT,
447                 },
448                 [3] = {
449                         .reg = 0x0b, .mask = 0x04,
450                         .v4l2_fault = V4L2_FLASH_FAULT_INPUT_VOLTAGE,
451                 },
452                 [4] = {
453                         .reg = 0x0b, .mask = 0x01,
454                         .v4l2_fault = V4L2_FLASH_FAULT_LED_OVER_TEMPERATURE,
455                 },
456         },
457         .func = ssflash_lm3648_init
458 };
460 static u8 ssflash_conv_val_to_reg(unsigned int val,
461         unsigned int min, unsigned int step,
462         unsigned int knee, unsigned int knee_step,
463         unsigned int max)
465         if (val >= max)
466                 return 0xff;
468         if (knee <= min)
469                 return (u8)(val < min ? 0 : (val - min)/step);
470         return (u8)val < min ? 0 :
471                         (val < knee ? (val-min)/step :
472                                 (val-knee)/knee_step + (knee-min)/step);
475 /* mode control */
476 static int ssflash_mode_ctrl(struct ssflash_flash *flash,
477                             enum v4l2_flash_led_mode v4l_led_mode)
479         const struct ssflash_data *data = flash->data;
480         enum led_opmode opmode;
482         switch (v4l_led_mode) {
483         case V4L2_FLASH_LED_MODE_NONE:
484                 opmode = OPMODE_SHDN;
485         break;
486         case V4L2_FLASH_LED_MODE_TORCH:
487                 opmode = OPMODE_TORCH;
488         break;
489         case V4L2_FLASH_LED_MODE_FLASH:
490                 opmode = OPMODE_FLASH;
491         break;
492         default:
493                 return -EINVAL;
494         }
495         return regmap_update_bits(flash->regmap, data->mode.reg,
496                                 data->mode.mask, opmode << data->mode.shift);
499 /* torch brightness control */
500 static int ssflash_torch_brt_ctrl
501         (struct ssflash_flash *flash, unsigned int brt)
503         const struct ssflash_data *data = flash->data;
504         int rval;
505         u8 br_bits;
507         br_bits = ssflash_conv_val_to_reg(brt,
508                                 data->torch_br.min, data->torch_br.step,
509                                 data->torch_br.knee, data->torch_br.knee_step,
510                                 data->torch_br.max);
512         rval = regmap_update_bits(flash->regmap,
513                         data->torch_br.ctrl.reg,
514                         data->torch_br.ctrl.mask,
515                         br_bits << data->torch_br.ctrl.shift);
517         return rval;
520 /* flash brightness control */
521 static int ssflash_flash_brt_ctrl(struct ssflash_flash *flash,
522                                  unsigned int brt)
524         const struct ssflash_data *data = flash->data;
525         int rval;
526         u8 br_bits;
528         br_bits = ssflash_conv_val_to_reg(brt,
529                                         data->flash_br.min,
530                                         data->flash_br.step,
531                                         data->flash_br.knee,
532                                         data->flash_br.knee_step,
533                                         data->flash_br.max);
535         rval = regmap_update_bits(flash->regmap,
536                                 data->flash_br.ctrl.reg,
537                                 data->flash_br.ctrl.mask,
538                                 br_bits << data->flash_br.ctrl.shift);
540         return rval;
543 /* fault status */
544 static int ssflash_get_ctrl(struct v4l2_ctrl *ctrl)
546         struct ssflash_flash *flash = to_ssflash_flash(ctrl);
547         const struct ssflash_data *data = flash->data;
548         int rval = -EINVAL;
550         if (ctrl->id == V4L2_CID_FLASH_FAULT) {
551                 int icnt;
552                 s32 fault = 0;
553                 unsigned int reg_val;
554                 unsigned int reg = 0xfff;
556                 for (icnt = 0; icnt < NUM_V4L2_FAULT; icnt++) {
557                         if (data->fault[icnt].mask == 0x0)
558                                 continue;
559                         if (data->fault[icnt].reg != reg) {
560                                 reg = data->fault[icnt].reg;
561                                 rval = regmap_read(
562                                                 flash->regmap, reg, &reg_val);
563                                 if (rval < 0)
564                                         goto out;
565                         }
566                         if (reg_val & data->fault[icnt].mask)
567                                 fault |= data->fault[icnt].v4l2_fault;
568                 }
569                 ctrl->cur.val = fault;
570         }
572 out:
573         return rval;
576 static int ssflash_set_ctrl(struct v4l2_ctrl *ctrl)
578         struct ssflash_flash *flash = to_ssflash_flash(ctrl);
579         const struct ssflash_data *data = flash->data;
580         u8 tout_bits;
581         unsigned int reg_val;
582         int rval = -EINVAL;
584         switch (ctrl->id) {
585         case V4L2_CID_FLASH_LED_MODE:
586                 if (ctrl->val != V4L2_FLASH_LED_MODE_FLASH)
587                         return ssflash_mode_ctrl(flash, ctrl->val);
588                 /* switch to SHDN mode before flash strobe on */
589                 return ssflash_mode_ctrl(flash, V4L2_FLASH_LED_MODE_NONE);
591         case V4L2_CID_FLASH_TORCH_INTENSITY:
592                 return ssflash_torch_brt_ctrl(flash, ctrl->val);
594         case V4L2_CID_FLASH_INTENSITY:
595                 return ssflash_flash_brt_ctrl(flash, ctrl->val);
597         case V4L2_CID_FLASH_STROBE_SOURCE:
598                 return regmap_update_bits(flash->regmap,
599                                         data->strobe.reg, data->strobe.mask,
600                                         (ctrl->val) << data->strobe.shift);
602         case V4L2_CID_FLASH_STROBE:
603                 /* read and check current mode of chip to start flash */
604                 rval = regmap_read(flash->regmap, data->mode.reg, &reg_val);
605                 if (rval < 0 ||
606                                 (((reg_val & data->mode.mask)>>data->mode.shift)
607                                 != OPMODE_SHDN))
608                         return rval;
609                 /* flash on */
610                 return ssflash_mode_ctrl(flash,
611                                         V4L2_FLASH_LED_MODE_FLASH);
613         case V4L2_CID_FLASH_STROBE_STOP:
614                 /*
615                  * flash mode will be turned automatically
616                  * from FLASH mode to SHDN mode after flash duration timeout
617                  * read and check current mode of chip to stop flash
618                  */
619                 rval = regmap_read(flash->regmap, data->mode.reg, &reg_val);
620                 if (rval < 0)
621                         return rval;
622                 if (((reg_val & data->mode.mask)
623                                 >> data->mode.shift) == OPMODE_FLASH)
624                         return ssflash_mode_ctrl(flash,
625                                                 V4L2_FLASH_LED_MODE_NONE);
626                 return rval;
628         case V4L2_CID_FLASH_TIMEOUT:
629                 tout_bits = ssflash_conv_val_to_reg(ctrl->val,
630                                                 data->timeout.min,
631                                                 data->timeout.step,
632                                                 data->timeout.knee,
633                                                 data->timeout.knee_step,
634                                                 data->timeout.max);
636                 return regmap_update_bits(flash->regmap,
637                                         data->timeout.ctrl.reg,
638                                         data->timeout.ctrl.mask,
639                                         tout_bits << data->timeout.ctrl.shift);
640         }
642         return rval;
645 static int ssflash_led_get_ctrl(struct v4l2_ctrl *ctrl)
647         return ssflash_get_ctrl(ctrl);
650 static int ssflash_led_set_ctrl(struct v4l2_ctrl *ctrl)
652         return ssflash_set_ctrl(ctrl);
655 static const struct v4l2_ctrl_ops ssflash_led_ctrl_ops = {
656          .g_volatile_ctrl = ssflash_led_get_ctrl,
657          .s_ctrl = ssflash_led_set_ctrl,
658 };
660 static int ssflash_init_controls(struct ssflash_flash *flash)
662         struct v4l2_ctrl *fault;
663         struct v4l2_ctrl_handler *hdl = &flash->ctrls_led;
664         const struct v4l2_ctrl_ops *ops = &ssflash_led_ctrl_ops;
665         const struct ssflash_data *data = flash->data;
666         s64 fault_max = 0;
667         int icnt;
669         v4l2_ctrl_handler_init(hdl, 8);
670         /* flash mode */
671         v4l2_ctrl_new_std_menu(hdl, ops, V4L2_CID_FLASH_LED_MODE,
672                                V4L2_FLASH_LED_MODE_TORCH, ~0x7,
673                                V4L2_FLASH_LED_MODE_NONE);
674         /* flash source */
675         v4l2_ctrl_new_std_menu(hdl, ops, V4L2_CID_FLASH_STROBE_SOURCE,
676                                0x1, ~0x3, V4L2_FLASH_STROBE_SOURCE_SOFTWARE);
677         /* flash strobe */
678         v4l2_ctrl_new_std(hdl, ops, V4L2_CID_FLASH_STROBE, 0, 0, 0, 0);
679         /* flash strobe stop */
680         v4l2_ctrl_new_std(hdl, ops, V4L2_CID_FLASH_STROBE_STOP, 0, 0, 0, 0);
681         /* flash strobe timeout */
682         v4l2_ctrl_new_std(hdl, ops, V4L2_CID_FLASH_TIMEOUT,
683                           data->timeout.min,
684                           data->timeout.max,
685                           data->timeout.step,
686                           data->timeout.max);
687         /* flash brt */
688         v4l2_ctrl_new_std(hdl, ops, V4L2_CID_FLASH_INTENSITY,
689                           data->flash_br.min,
690                           data->flash_br.max,
691                           data->flash_br.step,
692                           data->flash_br.max);
693         /* torch brt */
694         v4l2_ctrl_new_std(hdl, ops, V4L2_CID_FLASH_TORCH_INTENSITY,
695                           data->torch_br.min,
696                           data->torch_br.max,
697                           data->torch_br.step,
698                           data->torch_br.max);
699         /* fault */
700         for (icnt = 0; icnt < NUM_V4L2_FAULT; icnt++)
701                 fault_max |= data->fault[icnt].v4l2_fault;
702         fault = v4l2_ctrl_new_std(hdl,
703                                 ops, V4L2_CID_FLASH_FAULT, 0, fault_max, 0, 0);
704         if (fault != NULL)
705                 fault->flags |= V4L2_CTRL_FLAG_VOLATILE;
707         if (hdl->error)
708                 return hdl->error;
710         flash->subdev_led.ctrl_handler = hdl;
711         return 0;
714 /* initialize device */
715 static const struct v4l2_subdev_ops ssflash_ops = {
716         .core = NULL,
717 };
719 static const struct regmap_config ssflash_regmap = {
720         .reg_bits = 8,
721         .val_bits = 8,
722         .max_register = 0xff,
723 };
725 static int ssflash_subdev_init(struct ssflash_flash *flash)
727         struct i2c_client *client = to_i2c_client(flash->dev);
728         int rval;
730         v4l2_i2c_subdev_init(&flash->subdev_led, client, &ssflash_ops);
731         flash->subdev_led.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
732         strcpy(flash->subdev_led.name, flash->data->name);
734         rval = ssflash_init_controls(flash);
735         if (rval)
736                 goto err_out;
738         rval = media_entity_init(&flash->subdev_led.entity, 0, NULL, 0);
739         if (rval < 0)
740                 goto err_out;
742         flash->subdev_led.entity.type = MEDIA_ENT_T_V4L2_SUBDEV_FLASH;
743         return rval;
745 err_out:
746         v4l2_ctrl_handler_free(&flash->ctrls_led);
747         return rval;
750 static int ssflash_init_device(struct ssflash_flash *flash)
752         int rval;
754         /* output disable */
755         rval = ssflash_mode_ctrl(flash, V4L2_FLASH_LED_MODE_NONE);
756         if (rval < 0)
757                 return rval;
759         if (flash->data->func != NULL) {
760                 rval = flash->data->func(flash->regmap);
761                 if (rval < 0)
762                         return rval;
763         }
765         return rval;
768 static int ssflash_probe(struct i2c_client *client,
769                         const struct i2c_device_id *devid)
771         struct ssflash_flash *flash;
772         int rval;
774         flash = devm_kzalloc(&client->dev, sizeof(*flash), GFP_KERNEL);
775         if (flash == NULL)
776                 return -ENOMEM;
778         flash->regmap = devm_regmap_init_i2c(client, &ssflash_regmap);
779         if (IS_ERR(flash->regmap)) {
780                 rval = PTR_ERR(flash->regmap);
781                 return rval;
782         }
784         flash->dev = &client->dev;
785         flash->data = (const struct ssflash_data *)devid->driver_data;
787         rval = ssflash_subdev_init(flash);
788         if (rval < 0)
789                 return rval;
791         rval = ssflash_init_device(flash);
792         if (rval < 0)
793                 return rval;
795         i2c_set_clientdata(client, flash);
797         return 0;
800 static int ssflash_remove(struct i2c_client *client)
802         struct ssflash_flash *flash = i2c_get_clientdata(client);
804         v4l2_device_unregister_subdev(&flash->subdev_led);
805         v4l2_ctrl_handler_free(&flash->ctrls_led);
806         media_entity_cleanup(&flash->subdev_led.entity);
808         return 0;
811 static const struct i2c_device_id ssflash_id_table[] = {
812         {LM3556_NAME, (unsigned long)&flash_lm3556},
813         {LM3561_NAME, (unsigned long)&flash_lm3561},
814         {LM3642_NAME, (unsigned long)&flash_lm3642},
815         {LM3646_NAME, (unsigned long)&flash_lm3646},
816         {LM3648_NAME, (unsigned long)&flash_lm3648},
817         {}
818 };
820 MODULE_DEVICE_TABLE(i2c, ssflash_id_table);
822 static struct i2c_driver ssflash_i2c_driver = {
823         .driver = {
824                 .name = SINGLE_STRING_FLASH_NAME,
825         },
826         .probe = ssflash_probe,
827         .remove = ssflash_remove,
828         .id_table = ssflash_id_table,
829 };
831 module_i2c_driver(ssflash_i2c_driver);
833 MODULE_AUTHOR("Daniel Jeong <gshark.jeong@gmail.com>");
834 MODULE_AUTHOR("Ldd Mlp <ldd-mlp@list.ti.com>");
835 MODULE_DESCRIPTION("Texas Instruments single string LED flash driver");
836 MODULE_LICENSE("GPL");