[media] s5p-g2d: fixed a bug in controls setting function
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / media / video / as3645a.c
blobec859a580651e327475f0ce57f7faba7f5a1d26b
1 /*
2 * drivers/media/video/as3645a.c - AS3645A and LM3555 flash controllers driver
4 * Copyright (C) 2008-2011 Nokia Corporation
5 * Copyright (c) 2011, Intel Corporation.
7 * Contact: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * version 2 as published by the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
21 * 02110-1301 USA
23 * TODO:
24 * - Check hardware FSTROBE control when sensor driver add support for this
28 #include <linux/delay.h>
29 #include <linux/i2c.h>
30 #include <linux/module.h>
31 #include <linux/mutex.h>
33 #include <media/as3645a.h>
34 #include <media/v4l2-ctrls.h>
35 #include <media/v4l2-device.h>
37 #define AS_TIMER_MS_TO_CODE(t) (((t) - 100) / 50)
38 #define AS_TIMER_CODE_TO_MS(c) (50 * (c) + 100)
40 /* Register definitions */
42 /* Read-only Design info register: Reset state: xxxx 0001 */
43 #define AS_DESIGN_INFO_REG 0x00
44 #define AS_DESIGN_INFO_FACTORY(x) (((x) >> 4))
45 #define AS_DESIGN_INFO_MODEL(x) ((x) & 0x0f)
47 /* Read-only Version control register: Reset state: 0000 0000
48 * for first engineering samples
50 #define AS_VERSION_CONTROL_REG 0x01
51 #define AS_VERSION_CONTROL_RFU(x) (((x) >> 4))
52 #define AS_VERSION_CONTROL_VERSION(x) ((x) & 0x0f)
54 /* Read / Write (Indicator and timer register): Reset state: 0000 1111 */
55 #define AS_INDICATOR_AND_TIMER_REG 0x02
56 #define AS_INDICATOR_AND_TIMER_TIMEOUT_SHIFT 0
57 #define AS_INDICATOR_AND_TIMER_VREF_SHIFT 4
58 #define AS_INDICATOR_AND_TIMER_INDICATOR_SHIFT 6
60 /* Read / Write (Current set register): Reset state: 0110 1001 */
61 #define AS_CURRENT_SET_REG 0x03
62 #define AS_CURRENT_ASSIST_LIGHT_SHIFT 0
63 #define AS_CURRENT_LED_DET_ON (1 << 3)
64 #define AS_CURRENT_FLASH_CURRENT_SHIFT 4
66 /* Read / Write (Control register): Reset state: 1011 0100 */
67 #define AS_CONTROL_REG 0x04
68 #define AS_CONTROL_MODE_SETTING_SHIFT 0
69 #define AS_CONTROL_STROBE_ON (1 << 2)
70 #define AS_CONTROL_OUT_ON (1 << 3)
71 #define AS_CONTROL_EXT_TORCH_ON (1 << 4)
72 #define AS_CONTROL_STROBE_TYPE_EDGE (0 << 5)
73 #define AS_CONTROL_STROBE_TYPE_LEVEL (1 << 5)
74 #define AS_CONTROL_COIL_PEAK_SHIFT 6
76 /* Read only (D3 is read / write) (Fault and info): Reset state: 0000 x000 */
77 #define AS_FAULT_INFO_REG 0x05
78 #define AS_FAULT_INFO_INDUCTOR_PEAK_LIMIT (1 << 1)
79 #define AS_FAULT_INFO_INDICATOR_LED (1 << 2)
80 #define AS_FAULT_INFO_LED_AMOUNT (1 << 3)
81 #define AS_FAULT_INFO_TIMEOUT (1 << 4)
82 #define AS_FAULT_INFO_OVER_TEMPERATURE (1 << 5)
83 #define AS_FAULT_INFO_SHORT_CIRCUIT (1 << 6)
84 #define AS_FAULT_INFO_OVER_VOLTAGE (1 << 7)
86 /* Boost register */
87 #define AS_BOOST_REG 0x0d
88 #define AS_BOOST_CURRENT_DISABLE (0 << 0)
89 #define AS_BOOST_CURRENT_ENABLE (1 << 0)
91 /* Password register is used to unlock boost register writing */
92 #define AS_PASSWORD_REG 0x0f
93 #define AS_PASSWORD_UNLOCK_VALUE 0x55
95 enum as_mode {
96 AS_MODE_EXT_TORCH = 0 << AS_CONTROL_MODE_SETTING_SHIFT,
97 AS_MODE_INDICATOR = 1 << AS_CONTROL_MODE_SETTING_SHIFT,
98 AS_MODE_ASSIST = 2 << AS_CONTROL_MODE_SETTING_SHIFT,
99 AS_MODE_FLASH = 3 << AS_CONTROL_MODE_SETTING_SHIFT,
103 * struct as3645a
105 * @subdev: V4L2 subdev
106 * @pdata: Flash platform data
107 * @power_lock: Protects power_count
108 * @power_count: Power reference count
109 * @led_mode: V4L2 flash LED mode
110 * @timeout: Flash timeout in microseconds
111 * @flash_current: Flash current (0=200mA ... 15=500mA). Maximum
112 * values are 400mA for two LEDs and 500mA for one LED.
113 * @assist_current: Torch/Assist light current (0=20mA, 1=40mA ... 7=160mA)
114 * @indicator_current: Indicator LED current (0=0mA, 1=2.5mA ... 4=10mA)
115 * @strobe_source: Flash strobe source (software or external)
117 struct as3645a {
118 struct v4l2_subdev subdev;
119 const struct as3645a_platform_data *pdata;
121 struct mutex power_lock;
122 int power_count;
124 /* Controls */
125 struct v4l2_ctrl_handler ctrls;
127 enum v4l2_flash_led_mode led_mode;
128 unsigned int timeout;
129 u8 flash_current;
130 u8 assist_current;
131 u8 indicator_current;
132 enum v4l2_flash_strobe_source strobe_source;
135 #define to_as3645a(sd) container_of(sd, struct as3645a, subdev)
137 /* Return negative errno else zero on success */
138 static int as3645a_write(struct as3645a *flash, u8 addr, u8 val)
140 struct i2c_client *client = v4l2_get_subdevdata(&flash->subdev);
141 int rval;
143 rval = i2c_smbus_write_byte_data(client, addr, val);
145 dev_dbg(&client->dev, "Write Addr:%02X Val:%02X %s\n", addr, val,
146 rval < 0 ? "fail" : "ok");
148 return rval;
151 /* Return negative errno else a data byte received from the device. */
152 static int as3645a_read(struct as3645a *flash, u8 addr)
154 struct i2c_client *client = v4l2_get_subdevdata(&flash->subdev);
155 int rval;
157 rval = i2c_smbus_read_byte_data(client, addr);
159 dev_dbg(&client->dev, "Read Addr:%02X Val:%02X %s\n", addr, rval,
160 rval < 0 ? "fail" : "ok");
162 return rval;
165 /* -----------------------------------------------------------------------------
166 * Hardware configuration and trigger
170 * as3645a_set_config - Set flash configuration registers
171 * @flash: The flash
173 * Configure the hardware with flash, assist and indicator currents, as well as
174 * flash timeout.
176 * Return 0 on success, or a negative error code if an I2C communication error
177 * occurred.
179 static int as3645a_set_config(struct as3645a *flash)
181 int ret;
182 u8 val;
184 val = (flash->flash_current << AS_CURRENT_FLASH_CURRENT_SHIFT)
185 | (flash->assist_current << AS_CURRENT_ASSIST_LIGHT_SHIFT)
186 | AS_CURRENT_LED_DET_ON;
188 ret = as3645a_write(flash, AS_CURRENT_SET_REG, val);
189 if (ret < 0)
190 return ret;
192 val = AS_TIMER_MS_TO_CODE(flash->timeout / 1000)
193 << AS_INDICATOR_AND_TIMER_TIMEOUT_SHIFT;
195 val |= (flash->pdata->vref << AS_INDICATOR_AND_TIMER_VREF_SHIFT)
196 | ((flash->indicator_current ? flash->indicator_current - 1 : 0)
197 << AS_INDICATOR_AND_TIMER_INDICATOR_SHIFT);
199 return as3645a_write(flash, AS_INDICATOR_AND_TIMER_REG, val);
203 * as3645a_set_control - Set flash control register
204 * @flash: The flash
205 * @mode: Desired output mode
206 * @on: Desired output state
208 * Configure the hardware with output mode and state.
210 * Return 0 on success, or a negative error code if an I2C communication error
211 * occurred.
213 static int
214 as3645a_set_control(struct as3645a *flash, enum as_mode mode, bool on)
216 u8 reg;
218 /* Configure output parameters and operation mode. */
219 reg = (flash->pdata->peak << AS_CONTROL_COIL_PEAK_SHIFT)
220 | (on ? AS_CONTROL_OUT_ON : 0)
221 | mode;
223 if (flash->led_mode == V4L2_FLASH_LED_MODE_FLASH &&
224 flash->strobe_source == V4L2_FLASH_STROBE_SOURCE_EXTERNAL) {
225 reg |= AS_CONTROL_STROBE_TYPE_LEVEL
226 | AS_CONTROL_STROBE_ON;
229 return as3645a_write(flash, AS_CONTROL_REG, reg);
233 * as3645a_set_output - Configure output and operation mode
234 * @flash: Flash controller
235 * @strobe: Strobe the flash (only valid in flash mode)
237 * Turn the LEDs output on/off and set the operation mode based on the current
238 * parameters.
240 * The AS3645A can't control the indicator LED independently of the flash/torch
241 * LED. If the flash controller is in V4L2_FLASH_LED_MODE_NONE mode, set the
242 * chip to indicator mode. Otherwise set it to assist light (torch) or flash
243 * mode.
245 * In indicator and assist modes, turn the output on/off based on the indicator
246 * and torch currents. In software strobe flash mode, turn the output on/off
247 * based on the strobe parameter.
249 static int as3645a_set_output(struct as3645a *flash, bool strobe)
251 enum as_mode mode;
252 bool on;
254 switch (flash->led_mode) {
255 case V4L2_FLASH_LED_MODE_NONE:
256 on = flash->indicator_current != 0;
257 mode = AS_MODE_INDICATOR;
258 break;
259 case V4L2_FLASH_LED_MODE_TORCH:
260 on = true;
261 mode = AS_MODE_ASSIST;
262 break;
263 case V4L2_FLASH_LED_MODE_FLASH:
264 on = strobe;
265 mode = AS_MODE_FLASH;
266 break;
267 default:
268 BUG();
271 /* Configure output parameters and operation mode. */
272 return as3645a_set_control(flash, mode, on);
275 /* -----------------------------------------------------------------------------
276 * V4L2 controls
279 static int as3645a_is_active(struct as3645a *flash)
281 int ret;
283 ret = as3645a_read(flash, AS_CONTROL_REG);
284 return ret < 0 ? ret : !!(ret & AS_CONTROL_OUT_ON);
287 static int as3645a_read_fault(struct as3645a *flash)
289 struct i2c_client *client = v4l2_get_subdevdata(&flash->subdev);
290 int rval;
292 /* NOTE: reading register clear fault status */
293 rval = as3645a_read(flash, AS_FAULT_INFO_REG);
294 if (rval < 0)
295 return rval;
297 if (rval & AS_FAULT_INFO_INDUCTOR_PEAK_LIMIT)
298 dev_dbg(&client->dev, "Inductor Peak limit fault\n");
300 if (rval & AS_FAULT_INFO_INDICATOR_LED)
301 dev_dbg(&client->dev, "Indicator LED fault: "
302 "Short circuit or open loop\n");
304 dev_dbg(&client->dev, "%u connected LEDs\n",
305 rval & AS_FAULT_INFO_LED_AMOUNT ? 2 : 1);
307 if (rval & AS_FAULT_INFO_TIMEOUT)
308 dev_dbg(&client->dev, "Timeout fault\n");
310 if (rval & AS_FAULT_INFO_OVER_TEMPERATURE)
311 dev_dbg(&client->dev, "Over temperature fault\n");
313 if (rval & AS_FAULT_INFO_SHORT_CIRCUIT)
314 dev_dbg(&client->dev, "Short circuit fault\n");
316 if (rval & AS_FAULT_INFO_OVER_VOLTAGE)
317 dev_dbg(&client->dev, "Over voltage fault: "
318 "Indicates missing capacitor or open connection\n");
320 return rval;
323 static int as3645a_get_ctrl(struct v4l2_ctrl *ctrl)
325 struct as3645a *flash =
326 container_of(ctrl->handler, struct as3645a, ctrls);
327 struct i2c_client *client = v4l2_get_subdevdata(&flash->subdev);
328 int value;
330 switch (ctrl->id) {
331 case V4L2_CID_FLASH_FAULT:
332 value = as3645a_read_fault(flash);
333 if (value < 0)
334 return value;
336 ctrl->cur.val = 0;
337 if (value & AS_FAULT_INFO_SHORT_CIRCUIT)
338 ctrl->cur.val |= V4L2_FLASH_FAULT_SHORT_CIRCUIT;
339 if (value & AS_FAULT_INFO_OVER_TEMPERATURE)
340 ctrl->cur.val |= V4L2_FLASH_FAULT_OVER_TEMPERATURE;
341 if (value & AS_FAULT_INFO_TIMEOUT)
342 ctrl->cur.val |= V4L2_FLASH_FAULT_TIMEOUT;
343 if (value & AS_FAULT_INFO_OVER_VOLTAGE)
344 ctrl->cur.val |= V4L2_FLASH_FAULT_OVER_VOLTAGE;
345 if (value & AS_FAULT_INFO_INDUCTOR_PEAK_LIMIT)
346 ctrl->cur.val |= V4L2_FLASH_FAULT_OVER_CURRENT;
347 if (value & AS_FAULT_INFO_INDICATOR_LED)
348 ctrl->cur.val |= V4L2_FLASH_FAULT_INDICATOR;
349 break;
351 case V4L2_CID_FLASH_STROBE_STATUS:
352 if (flash->led_mode != V4L2_FLASH_LED_MODE_FLASH) {
353 ctrl->cur.val = 0;
354 break;
357 value = as3645a_is_active(flash);
358 if (value < 0)
359 return value;
361 ctrl->cur.val = value;
362 break;
365 dev_dbg(&client->dev, "G_CTRL %08x:%d\n", ctrl->id, ctrl->cur.val);
367 return 0;
370 static int as3645a_set_ctrl(struct v4l2_ctrl *ctrl)
372 struct as3645a *flash =
373 container_of(ctrl->handler, struct as3645a, ctrls);
374 struct i2c_client *client = v4l2_get_subdevdata(&flash->subdev);
375 int ret;
377 dev_dbg(&client->dev, "S_CTRL %08x:%d\n", ctrl->id, ctrl->val);
379 /* If a control that doesn't apply to the current mode is modified,
380 * we store the value and return immediately. The setting will be
381 * applied when the LED mode is changed. Otherwise we apply the setting
382 * immediately.
385 switch (ctrl->id) {
386 case V4L2_CID_FLASH_LED_MODE:
387 if (flash->indicator_current)
388 return -EBUSY;
390 ret = as3645a_set_config(flash);
391 if (ret < 0)
392 return ret;
394 flash->led_mode = ctrl->val;
395 return as3645a_set_output(flash, false);
397 case V4L2_CID_FLASH_STROBE_SOURCE:
398 flash->strobe_source = ctrl->val;
400 /* Applies to flash mode only. */
401 if (flash->led_mode != V4L2_FLASH_LED_MODE_FLASH)
402 break;
404 return as3645a_set_output(flash, false);
406 case V4L2_CID_FLASH_STROBE:
407 if (flash->led_mode != V4L2_FLASH_LED_MODE_FLASH)
408 return -EBUSY;
410 return as3645a_set_output(flash, true);
412 case V4L2_CID_FLASH_STROBE_STOP:
413 if (flash->led_mode != V4L2_FLASH_LED_MODE_FLASH)
414 return -EBUSY;
416 return as3645a_set_output(flash, false);
418 case V4L2_CID_FLASH_TIMEOUT:
419 flash->timeout = ctrl->val;
421 /* Applies to flash mode only. */
422 if (flash->led_mode != V4L2_FLASH_LED_MODE_FLASH)
423 break;
425 return as3645a_set_config(flash);
427 case V4L2_CID_FLASH_INTENSITY:
428 flash->flash_current = (ctrl->val - AS3645A_FLASH_INTENSITY_MIN)
429 / AS3645A_FLASH_INTENSITY_STEP;
431 /* Applies to flash mode only. */
432 if (flash->led_mode != V4L2_FLASH_LED_MODE_FLASH)
433 break;
435 return as3645a_set_config(flash);
437 case V4L2_CID_FLASH_TORCH_INTENSITY:
438 flash->assist_current =
439 (ctrl->val - AS3645A_TORCH_INTENSITY_MIN)
440 / AS3645A_TORCH_INTENSITY_STEP;
442 /* Applies to torch mode only. */
443 if (flash->led_mode != V4L2_FLASH_LED_MODE_TORCH)
444 break;
446 return as3645a_set_config(flash);
448 case V4L2_CID_FLASH_INDICATOR_INTENSITY:
449 if (flash->led_mode != V4L2_FLASH_LED_MODE_NONE)
450 return -EBUSY;
452 flash->indicator_current =
453 (ctrl->val - AS3645A_INDICATOR_INTENSITY_MIN)
454 / AS3645A_INDICATOR_INTENSITY_STEP;
456 ret = as3645a_set_config(flash);
457 if (ret < 0)
458 return ret;
460 if ((ctrl->val == 0) == (ctrl->cur.val == 0))
461 break;
463 return as3645a_set_output(flash, false);
466 return 0;
469 static const struct v4l2_ctrl_ops as3645a_ctrl_ops = {
470 .g_volatile_ctrl = as3645a_get_ctrl,
471 .s_ctrl = as3645a_set_ctrl,
474 /* -----------------------------------------------------------------------------
475 * V4L2 subdev core operations
478 /* Put device into know state. */
479 static int as3645a_setup(struct as3645a *flash)
481 struct i2c_client *client = v4l2_get_subdevdata(&flash->subdev);
482 int ret;
484 /* clear errors */
485 ret = as3645a_read(flash, AS_FAULT_INFO_REG);
486 if (ret < 0)
487 return ret;
489 dev_dbg(&client->dev, "Fault info: %02x\n", ret);
491 ret = as3645a_set_config(flash);
492 if (ret < 0)
493 return ret;
495 ret = as3645a_set_output(flash, false);
496 if (ret < 0)
497 return ret;
499 /* read status */
500 ret = as3645a_read_fault(flash);
501 if (ret < 0)
502 return ret;
504 dev_dbg(&client->dev, "AS_INDICATOR_AND_TIMER_REG: %02x\n",
505 as3645a_read(flash, AS_INDICATOR_AND_TIMER_REG));
506 dev_dbg(&client->dev, "AS_CURRENT_SET_REG: %02x\n",
507 as3645a_read(flash, AS_CURRENT_SET_REG));
508 dev_dbg(&client->dev, "AS_CONTROL_REG: %02x\n",
509 as3645a_read(flash, AS_CONTROL_REG));
511 return ret & ~AS_FAULT_INFO_LED_AMOUNT ? -EIO : 0;
514 static int __as3645a_set_power(struct as3645a *flash, int on)
516 int ret;
518 if (!on)
519 as3645a_set_control(flash, AS_MODE_EXT_TORCH, false);
521 if (flash->pdata->set_power) {
522 ret = flash->pdata->set_power(&flash->subdev, on);
523 if (ret < 0)
524 return ret;
527 if (!on)
528 return 0;
530 ret = as3645a_setup(flash);
531 if (ret < 0) {
532 if (flash->pdata->set_power)
533 flash->pdata->set_power(&flash->subdev, 0);
536 return ret;
539 static int as3645a_set_power(struct v4l2_subdev *sd, int on)
541 struct as3645a *flash = to_as3645a(sd);
542 int ret = 0;
544 mutex_lock(&flash->power_lock);
546 if (flash->power_count == !on) {
547 ret = __as3645a_set_power(flash, !!on);
548 if (ret < 0)
549 goto done;
552 flash->power_count += on ? 1 : -1;
553 WARN_ON(flash->power_count < 0);
555 done:
556 mutex_unlock(&flash->power_lock);
557 return ret;
560 static int as3645a_registered(struct v4l2_subdev *sd)
562 struct as3645a *flash = to_as3645a(sd);
563 struct i2c_client *client = v4l2_get_subdevdata(sd);
564 int rval, man, model, rfu, version;
565 const char *vendor;
567 /* Power up the flash driver and read manufacturer ID, model ID, RFU
568 * and version.
570 rval = as3645a_set_power(&flash->subdev, 1);
571 if (rval < 0)
572 return rval;
574 rval = as3645a_read(flash, AS_DESIGN_INFO_REG);
575 if (rval < 0)
576 goto power_off;
578 man = AS_DESIGN_INFO_FACTORY(rval);
579 model = AS_DESIGN_INFO_MODEL(rval);
581 rval = as3645a_read(flash, AS_VERSION_CONTROL_REG);
582 if (rval < 0)
583 goto power_off;
585 rfu = AS_VERSION_CONTROL_RFU(rval);
586 version = AS_VERSION_CONTROL_VERSION(rval);
588 /* Verify the chip model and version. */
589 if (model != 0x01 || rfu != 0x00) {
590 dev_err(&client->dev, "AS3645A not detected "
591 "(model %d rfu %d)\n", model, rfu);
592 rval = -ENODEV;
593 goto power_off;
596 switch (man) {
597 case 1:
598 vendor = "AMS, Austria Micro Systems";
599 break;
600 case 2:
601 vendor = "ADI, Analog Devices Inc.";
602 break;
603 case 3:
604 vendor = "NSC, National Semiconductor";
605 break;
606 case 4:
607 vendor = "NXP";
608 break;
609 case 5:
610 vendor = "TI, Texas Instrument";
611 break;
612 default:
613 vendor = "Unknown";
616 dev_info(&client->dev, "Chip vendor: %s (%d) Version: %d\n", vendor,
617 man, version);
619 rval = as3645a_write(flash, AS_PASSWORD_REG, AS_PASSWORD_UNLOCK_VALUE);
620 if (rval < 0)
621 goto power_off;
623 rval = as3645a_write(flash, AS_BOOST_REG, AS_BOOST_CURRENT_DISABLE);
624 if (rval < 0)
625 goto power_off;
627 /* Setup default values. This makes sure that the chip is in a known
628 * state, in case the power rail can't be controlled.
630 rval = as3645a_setup(flash);
632 power_off:
633 as3645a_set_power(&flash->subdev, 0);
635 return rval;
638 static int as3645a_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
640 return as3645a_set_power(sd, 1);
643 static int as3645a_close(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
645 return as3645a_set_power(sd, 0);
648 static const struct v4l2_subdev_core_ops as3645a_core_ops = {
649 .s_power = as3645a_set_power,
652 static const struct v4l2_subdev_ops as3645a_ops = {
653 .core = &as3645a_core_ops,
656 static const struct v4l2_subdev_internal_ops as3645a_internal_ops = {
657 .registered = as3645a_registered,
658 .open = as3645a_open,
659 .close = as3645a_close,
662 /* -----------------------------------------------------------------------------
663 * I2C driver
665 #ifdef CONFIG_PM
667 static int as3645a_suspend(struct device *dev)
669 struct i2c_client *client = to_i2c_client(dev);
670 struct v4l2_subdev *subdev = i2c_get_clientdata(client);
671 struct as3645a *flash = to_as3645a(subdev);
672 int rval;
674 if (flash->power_count == 0)
675 return 0;
677 rval = __as3645a_set_power(flash, 0);
679 dev_dbg(&client->dev, "Suspend %s\n", rval < 0 ? "failed" : "ok");
681 return rval;
684 static int as3645a_resume(struct device *dev)
686 struct i2c_client *client = to_i2c_client(dev);
687 struct v4l2_subdev *subdev = i2c_get_clientdata(client);
688 struct as3645a *flash = to_as3645a(subdev);
689 int rval;
691 if (flash->power_count == 0)
692 return 0;
694 rval = __as3645a_set_power(flash, 1);
696 dev_dbg(&client->dev, "Resume %s\n", rval < 0 ? "fail" : "ok");
698 return rval;
701 #else
703 #define as3645a_suspend NULL
704 #define as3645a_resume NULL
706 #endif /* CONFIG_PM */
709 * as3645a_init_controls - Create controls
710 * @flash: The flash
712 * The number of LEDs reported in platform data is used to compute default
713 * limits. Parameters passed through platform data can override those limits.
715 static int as3645a_init_controls(struct as3645a *flash)
717 const struct as3645a_platform_data *pdata = flash->pdata;
718 struct v4l2_ctrl *ctrl;
719 int maximum;
721 v4l2_ctrl_handler_init(&flash->ctrls, 10);
723 /* V4L2_CID_FLASH_LED_MODE */
724 v4l2_ctrl_new_std_menu(&flash->ctrls, &as3645a_ctrl_ops,
725 V4L2_CID_FLASH_LED_MODE, 2, ~7,
726 V4L2_FLASH_LED_MODE_NONE);
728 /* V4L2_CID_FLASH_STROBE_SOURCE */
729 v4l2_ctrl_new_std_menu(&flash->ctrls, &as3645a_ctrl_ops,
730 V4L2_CID_FLASH_STROBE_SOURCE,
731 pdata->ext_strobe ? 1 : 0,
732 pdata->ext_strobe ? ~3 : ~1,
733 V4L2_FLASH_STROBE_SOURCE_SOFTWARE);
735 flash->strobe_source = V4L2_FLASH_STROBE_SOURCE_SOFTWARE;
737 /* V4L2_CID_FLASH_STROBE */
738 v4l2_ctrl_new_std(&flash->ctrls, &as3645a_ctrl_ops,
739 V4L2_CID_FLASH_STROBE, 0, 0, 0, 0);
741 /* V4L2_CID_FLASH_STROBE_STOP */
742 v4l2_ctrl_new_std(&flash->ctrls, &as3645a_ctrl_ops,
743 V4L2_CID_FLASH_STROBE_STOP, 0, 0, 0, 0);
745 /* V4L2_CID_FLASH_STROBE_STATUS */
746 ctrl = v4l2_ctrl_new_std(&flash->ctrls, &as3645a_ctrl_ops,
747 V4L2_CID_FLASH_STROBE_STATUS, 0, 1, 1, 1);
748 if (ctrl != NULL)
749 ctrl->flags |= V4L2_CTRL_FLAG_VOLATILE;
751 /* V4L2_CID_FLASH_TIMEOUT */
752 maximum = pdata->timeout_max;
754 v4l2_ctrl_new_std(&flash->ctrls, &as3645a_ctrl_ops,
755 V4L2_CID_FLASH_TIMEOUT, AS3645A_FLASH_TIMEOUT_MIN,
756 maximum, AS3645A_FLASH_TIMEOUT_STEP, maximum);
758 flash->timeout = maximum;
760 /* V4L2_CID_FLASH_INTENSITY */
761 maximum = pdata->flash_max_current;
763 v4l2_ctrl_new_std(&flash->ctrls, &as3645a_ctrl_ops,
764 V4L2_CID_FLASH_INTENSITY, AS3645A_FLASH_INTENSITY_MIN,
765 maximum, AS3645A_FLASH_INTENSITY_STEP, maximum);
767 flash->flash_current = (maximum - AS3645A_FLASH_INTENSITY_MIN)
768 / AS3645A_FLASH_INTENSITY_STEP;
770 /* V4L2_CID_FLASH_TORCH_INTENSITY */
771 maximum = pdata->torch_max_current;
773 v4l2_ctrl_new_std(&flash->ctrls, &as3645a_ctrl_ops,
774 V4L2_CID_FLASH_TORCH_INTENSITY,
775 AS3645A_TORCH_INTENSITY_MIN, maximum,
776 AS3645A_TORCH_INTENSITY_STEP,
777 AS3645A_TORCH_INTENSITY_MIN);
779 flash->assist_current = 0;
781 /* V4L2_CID_FLASH_INDICATOR_INTENSITY */
782 v4l2_ctrl_new_std(&flash->ctrls, &as3645a_ctrl_ops,
783 V4L2_CID_FLASH_INDICATOR_INTENSITY,
784 AS3645A_INDICATOR_INTENSITY_MIN,
785 AS3645A_INDICATOR_INTENSITY_MAX,
786 AS3645A_INDICATOR_INTENSITY_STEP,
787 AS3645A_INDICATOR_INTENSITY_MIN);
789 flash->indicator_current = 0;
791 /* V4L2_CID_FLASH_FAULT */
792 ctrl = v4l2_ctrl_new_std(&flash->ctrls, &as3645a_ctrl_ops,
793 V4L2_CID_FLASH_FAULT, 0,
794 V4L2_FLASH_FAULT_OVER_VOLTAGE |
795 V4L2_FLASH_FAULT_TIMEOUT |
796 V4L2_FLASH_FAULT_OVER_TEMPERATURE |
797 V4L2_FLASH_FAULT_SHORT_CIRCUIT, 0, 0);
798 if (ctrl != NULL)
799 ctrl->flags |= V4L2_CTRL_FLAG_VOLATILE;
801 flash->subdev.ctrl_handler = &flash->ctrls;
803 return flash->ctrls.error;
806 static int as3645a_probe(struct i2c_client *client,
807 const struct i2c_device_id *devid)
809 struct as3645a *flash;
810 int ret;
812 if (client->dev.platform_data == NULL)
813 return -ENODEV;
815 flash = kzalloc(sizeof(*flash), GFP_KERNEL);
816 if (flash == NULL)
817 return -ENOMEM;
819 flash->pdata = client->dev.platform_data;
821 v4l2_i2c_subdev_init(&flash->subdev, client, &as3645a_ops);
822 flash->subdev.internal_ops = &as3645a_internal_ops;
823 flash->subdev.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
825 ret = as3645a_init_controls(flash);
826 if (ret < 0)
827 goto done;
829 ret = media_entity_init(&flash->subdev.entity, 0, NULL, 0);
830 if (ret < 0)
831 goto done;
833 flash->subdev.entity.type = MEDIA_ENT_T_V4L2_SUBDEV_FLASH;
835 mutex_init(&flash->power_lock);
837 flash->led_mode = V4L2_FLASH_LED_MODE_NONE;
839 done:
840 if (ret < 0) {
841 v4l2_ctrl_handler_free(&flash->ctrls);
842 kfree(flash);
845 return ret;
848 static int __exit as3645a_remove(struct i2c_client *client)
850 struct v4l2_subdev *subdev = i2c_get_clientdata(client);
851 struct as3645a *flash = to_as3645a(subdev);
853 v4l2_device_unregister_subdev(subdev);
854 v4l2_ctrl_handler_free(&flash->ctrls);
855 media_entity_cleanup(&flash->subdev.entity);
856 mutex_destroy(&flash->power_lock);
857 kfree(flash);
859 return 0;
862 static const struct i2c_device_id as3645a_id_table[] = {
863 { AS3645A_NAME, 0 },
864 { },
866 MODULE_DEVICE_TABLE(i2c, as3645a_id_table);
868 static const struct dev_pm_ops as3645a_pm_ops = {
869 .suspend = as3645a_suspend,
870 .resume = as3645a_resume,
873 static struct i2c_driver as3645a_i2c_driver = {
874 .driver = {
875 .name = AS3645A_NAME,
876 .pm = &as3645a_pm_ops,
878 .probe = as3645a_probe,
879 .remove = __exit_p(as3645a_remove),
880 .id_table = as3645a_id_table,
883 static int __init as3645a_init(void)
885 int rval;
887 rval = i2c_add_driver(&as3645a_i2c_driver);
888 if (rval)
889 pr_err("%s: Failed to register the driver\n", AS3645A_NAME);
891 return rval;
894 static void __exit as3645a_exit(void)
896 i2c_del_driver(&as3645a_i2c_driver);
899 module_init(as3645a_init);
900 module_exit(as3645a_exit);
902 MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
903 MODULE_DESCRIPTION("LED flash driver for AS3645A, LM3555 and their clones");
904 MODULE_LICENSE("GPL");