[media] as3645a: Fix compilation by including slab.h
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / media / video / as3645a.c
blobf241702a0f36442aa7e3ab1c8d9fe55213e00d78
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>
32 #include <linux/slab.h>
34 #include <media/as3645a.h>
35 #include <media/v4l2-ctrls.h>
36 #include <media/v4l2-device.h>
38 #define AS_TIMER_MS_TO_CODE(t) (((t) - 100) / 50)
39 #define AS_TIMER_CODE_TO_MS(c) (50 * (c) + 100)
41 /* Register definitions */
43 /* Read-only Design info register: Reset state: xxxx 0001 */
44 #define AS_DESIGN_INFO_REG 0x00
45 #define AS_DESIGN_INFO_FACTORY(x) (((x) >> 4))
46 #define AS_DESIGN_INFO_MODEL(x) ((x) & 0x0f)
48 /* Read-only Version control register: Reset state: 0000 0000
49 * for first engineering samples
51 #define AS_VERSION_CONTROL_REG 0x01
52 #define AS_VERSION_CONTROL_RFU(x) (((x) >> 4))
53 #define AS_VERSION_CONTROL_VERSION(x) ((x) & 0x0f)
55 /* Read / Write (Indicator and timer register): Reset state: 0000 1111 */
56 #define AS_INDICATOR_AND_TIMER_REG 0x02
57 #define AS_INDICATOR_AND_TIMER_TIMEOUT_SHIFT 0
58 #define AS_INDICATOR_AND_TIMER_VREF_SHIFT 4
59 #define AS_INDICATOR_AND_TIMER_INDICATOR_SHIFT 6
61 /* Read / Write (Current set register): Reset state: 0110 1001 */
62 #define AS_CURRENT_SET_REG 0x03
63 #define AS_CURRENT_ASSIST_LIGHT_SHIFT 0
64 #define AS_CURRENT_LED_DET_ON (1 << 3)
65 #define AS_CURRENT_FLASH_CURRENT_SHIFT 4
67 /* Read / Write (Control register): Reset state: 1011 0100 */
68 #define AS_CONTROL_REG 0x04
69 #define AS_CONTROL_MODE_SETTING_SHIFT 0
70 #define AS_CONTROL_STROBE_ON (1 << 2)
71 #define AS_CONTROL_OUT_ON (1 << 3)
72 #define AS_CONTROL_EXT_TORCH_ON (1 << 4)
73 #define AS_CONTROL_STROBE_TYPE_EDGE (0 << 5)
74 #define AS_CONTROL_STROBE_TYPE_LEVEL (1 << 5)
75 #define AS_CONTROL_COIL_PEAK_SHIFT 6
77 /* Read only (D3 is read / write) (Fault and info): Reset state: 0000 x000 */
78 #define AS_FAULT_INFO_REG 0x05
79 #define AS_FAULT_INFO_INDUCTOR_PEAK_LIMIT (1 << 1)
80 #define AS_FAULT_INFO_INDICATOR_LED (1 << 2)
81 #define AS_FAULT_INFO_LED_AMOUNT (1 << 3)
82 #define AS_FAULT_INFO_TIMEOUT (1 << 4)
83 #define AS_FAULT_INFO_OVER_TEMPERATURE (1 << 5)
84 #define AS_FAULT_INFO_SHORT_CIRCUIT (1 << 6)
85 #define AS_FAULT_INFO_OVER_VOLTAGE (1 << 7)
87 /* Boost register */
88 #define AS_BOOST_REG 0x0d
89 #define AS_BOOST_CURRENT_DISABLE (0 << 0)
90 #define AS_BOOST_CURRENT_ENABLE (1 << 0)
92 /* Password register is used to unlock boost register writing */
93 #define AS_PASSWORD_REG 0x0f
94 #define AS_PASSWORD_UNLOCK_VALUE 0x55
96 enum as_mode {
97 AS_MODE_EXT_TORCH = 0 << AS_CONTROL_MODE_SETTING_SHIFT,
98 AS_MODE_INDICATOR = 1 << AS_CONTROL_MODE_SETTING_SHIFT,
99 AS_MODE_ASSIST = 2 << AS_CONTROL_MODE_SETTING_SHIFT,
100 AS_MODE_FLASH = 3 << AS_CONTROL_MODE_SETTING_SHIFT,
104 * struct as3645a
106 * @subdev: V4L2 subdev
107 * @pdata: Flash platform data
108 * @power_lock: Protects power_count
109 * @power_count: Power reference count
110 * @led_mode: V4L2 flash LED mode
111 * @timeout: Flash timeout in microseconds
112 * @flash_current: Flash current (0=200mA ... 15=500mA). Maximum
113 * values are 400mA for two LEDs and 500mA for one LED.
114 * @assist_current: Torch/Assist light current (0=20mA, 1=40mA ... 7=160mA)
115 * @indicator_current: Indicator LED current (0=0mA, 1=2.5mA ... 4=10mA)
116 * @strobe_source: Flash strobe source (software or external)
118 struct as3645a {
119 struct v4l2_subdev subdev;
120 const struct as3645a_platform_data *pdata;
122 struct mutex power_lock;
123 int power_count;
125 /* Controls */
126 struct v4l2_ctrl_handler ctrls;
128 enum v4l2_flash_led_mode led_mode;
129 unsigned int timeout;
130 u8 flash_current;
131 u8 assist_current;
132 u8 indicator_current;
133 enum v4l2_flash_strobe_source strobe_source;
136 #define to_as3645a(sd) container_of(sd, struct as3645a, subdev)
138 /* Return negative errno else zero on success */
139 static int as3645a_write(struct as3645a *flash, u8 addr, u8 val)
141 struct i2c_client *client = v4l2_get_subdevdata(&flash->subdev);
142 int rval;
144 rval = i2c_smbus_write_byte_data(client, addr, val);
146 dev_dbg(&client->dev, "Write Addr:%02X Val:%02X %s\n", addr, val,
147 rval < 0 ? "fail" : "ok");
149 return rval;
152 /* Return negative errno else a data byte received from the device. */
153 static int as3645a_read(struct as3645a *flash, u8 addr)
155 struct i2c_client *client = v4l2_get_subdevdata(&flash->subdev);
156 int rval;
158 rval = i2c_smbus_read_byte_data(client, addr);
160 dev_dbg(&client->dev, "Read Addr:%02X Val:%02X %s\n", addr, rval,
161 rval < 0 ? "fail" : "ok");
163 return rval;
166 /* -----------------------------------------------------------------------------
167 * Hardware configuration and trigger
171 * as3645a_set_config - Set flash configuration registers
172 * @flash: The flash
174 * Configure the hardware with flash, assist and indicator currents, as well as
175 * flash timeout.
177 * Return 0 on success, or a negative error code if an I2C communication error
178 * occurred.
180 static int as3645a_set_config(struct as3645a *flash)
182 int ret;
183 u8 val;
185 val = (flash->flash_current << AS_CURRENT_FLASH_CURRENT_SHIFT)
186 | (flash->assist_current << AS_CURRENT_ASSIST_LIGHT_SHIFT)
187 | AS_CURRENT_LED_DET_ON;
189 ret = as3645a_write(flash, AS_CURRENT_SET_REG, val);
190 if (ret < 0)
191 return ret;
193 val = AS_TIMER_MS_TO_CODE(flash->timeout / 1000)
194 << AS_INDICATOR_AND_TIMER_TIMEOUT_SHIFT;
196 val |= (flash->pdata->vref << AS_INDICATOR_AND_TIMER_VREF_SHIFT)
197 | ((flash->indicator_current ? flash->indicator_current - 1 : 0)
198 << AS_INDICATOR_AND_TIMER_INDICATOR_SHIFT);
200 return as3645a_write(flash, AS_INDICATOR_AND_TIMER_REG, val);
204 * as3645a_set_control - Set flash control register
205 * @flash: The flash
206 * @mode: Desired output mode
207 * @on: Desired output state
209 * Configure the hardware with output mode and state.
211 * Return 0 on success, or a negative error code if an I2C communication error
212 * occurred.
214 static int
215 as3645a_set_control(struct as3645a *flash, enum as_mode mode, bool on)
217 u8 reg;
219 /* Configure output parameters and operation mode. */
220 reg = (flash->pdata->peak << AS_CONTROL_COIL_PEAK_SHIFT)
221 | (on ? AS_CONTROL_OUT_ON : 0)
222 | mode;
224 if (flash->led_mode == V4L2_FLASH_LED_MODE_FLASH &&
225 flash->strobe_source == V4L2_FLASH_STROBE_SOURCE_EXTERNAL) {
226 reg |= AS_CONTROL_STROBE_TYPE_LEVEL
227 | AS_CONTROL_STROBE_ON;
230 return as3645a_write(flash, AS_CONTROL_REG, reg);
234 * as3645a_set_output - Configure output and operation mode
235 * @flash: Flash controller
236 * @strobe: Strobe the flash (only valid in flash mode)
238 * Turn the LEDs output on/off and set the operation mode based on the current
239 * parameters.
241 * The AS3645A can't control the indicator LED independently of the flash/torch
242 * LED. If the flash controller is in V4L2_FLASH_LED_MODE_NONE mode, set the
243 * chip to indicator mode. Otherwise set it to assist light (torch) or flash
244 * mode.
246 * In indicator and assist modes, turn the output on/off based on the indicator
247 * and torch currents. In software strobe flash mode, turn the output on/off
248 * based on the strobe parameter.
250 static int as3645a_set_output(struct as3645a *flash, bool strobe)
252 enum as_mode mode;
253 bool on;
255 switch (flash->led_mode) {
256 case V4L2_FLASH_LED_MODE_NONE:
257 on = flash->indicator_current != 0;
258 mode = AS_MODE_INDICATOR;
259 break;
260 case V4L2_FLASH_LED_MODE_TORCH:
261 on = true;
262 mode = AS_MODE_ASSIST;
263 break;
264 case V4L2_FLASH_LED_MODE_FLASH:
265 on = strobe;
266 mode = AS_MODE_FLASH;
267 break;
268 default:
269 BUG();
272 /* Configure output parameters and operation mode. */
273 return as3645a_set_control(flash, mode, on);
276 /* -----------------------------------------------------------------------------
277 * V4L2 controls
280 static int as3645a_is_active(struct as3645a *flash)
282 int ret;
284 ret = as3645a_read(flash, AS_CONTROL_REG);
285 return ret < 0 ? ret : !!(ret & AS_CONTROL_OUT_ON);
288 static int as3645a_read_fault(struct as3645a *flash)
290 struct i2c_client *client = v4l2_get_subdevdata(&flash->subdev);
291 int rval;
293 /* NOTE: reading register clear fault status */
294 rval = as3645a_read(flash, AS_FAULT_INFO_REG);
295 if (rval < 0)
296 return rval;
298 if (rval & AS_FAULT_INFO_INDUCTOR_PEAK_LIMIT)
299 dev_dbg(&client->dev, "Inductor Peak limit fault\n");
301 if (rval & AS_FAULT_INFO_INDICATOR_LED)
302 dev_dbg(&client->dev, "Indicator LED fault: "
303 "Short circuit or open loop\n");
305 dev_dbg(&client->dev, "%u connected LEDs\n",
306 rval & AS_FAULT_INFO_LED_AMOUNT ? 2 : 1);
308 if (rval & AS_FAULT_INFO_TIMEOUT)
309 dev_dbg(&client->dev, "Timeout fault\n");
311 if (rval & AS_FAULT_INFO_OVER_TEMPERATURE)
312 dev_dbg(&client->dev, "Over temperature fault\n");
314 if (rval & AS_FAULT_INFO_SHORT_CIRCUIT)
315 dev_dbg(&client->dev, "Short circuit fault\n");
317 if (rval & AS_FAULT_INFO_OVER_VOLTAGE)
318 dev_dbg(&client->dev, "Over voltage fault: "
319 "Indicates missing capacitor or open connection\n");
321 return rval;
324 static int as3645a_get_ctrl(struct v4l2_ctrl *ctrl)
326 struct as3645a *flash =
327 container_of(ctrl->handler, struct as3645a, ctrls);
328 struct i2c_client *client = v4l2_get_subdevdata(&flash->subdev);
329 int value;
331 switch (ctrl->id) {
332 case V4L2_CID_FLASH_FAULT:
333 value = as3645a_read_fault(flash);
334 if (value < 0)
335 return value;
337 ctrl->cur.val = 0;
338 if (value & AS_FAULT_INFO_SHORT_CIRCUIT)
339 ctrl->cur.val |= V4L2_FLASH_FAULT_SHORT_CIRCUIT;
340 if (value & AS_FAULT_INFO_OVER_TEMPERATURE)
341 ctrl->cur.val |= V4L2_FLASH_FAULT_OVER_TEMPERATURE;
342 if (value & AS_FAULT_INFO_TIMEOUT)
343 ctrl->cur.val |= V4L2_FLASH_FAULT_TIMEOUT;
344 if (value & AS_FAULT_INFO_OVER_VOLTAGE)
345 ctrl->cur.val |= V4L2_FLASH_FAULT_OVER_VOLTAGE;
346 if (value & AS_FAULT_INFO_INDUCTOR_PEAK_LIMIT)
347 ctrl->cur.val |= V4L2_FLASH_FAULT_OVER_CURRENT;
348 if (value & AS_FAULT_INFO_INDICATOR_LED)
349 ctrl->cur.val |= V4L2_FLASH_FAULT_INDICATOR;
350 break;
352 case V4L2_CID_FLASH_STROBE_STATUS:
353 if (flash->led_mode != V4L2_FLASH_LED_MODE_FLASH) {
354 ctrl->cur.val = 0;
355 break;
358 value = as3645a_is_active(flash);
359 if (value < 0)
360 return value;
362 ctrl->cur.val = value;
363 break;
366 dev_dbg(&client->dev, "G_CTRL %08x:%d\n", ctrl->id, ctrl->cur.val);
368 return 0;
371 static int as3645a_set_ctrl(struct v4l2_ctrl *ctrl)
373 struct as3645a *flash =
374 container_of(ctrl->handler, struct as3645a, ctrls);
375 struct i2c_client *client = v4l2_get_subdevdata(&flash->subdev);
376 int ret;
378 dev_dbg(&client->dev, "S_CTRL %08x:%d\n", ctrl->id, ctrl->val);
380 /* If a control that doesn't apply to the current mode is modified,
381 * we store the value and return immediately. The setting will be
382 * applied when the LED mode is changed. Otherwise we apply the setting
383 * immediately.
386 switch (ctrl->id) {
387 case V4L2_CID_FLASH_LED_MODE:
388 if (flash->indicator_current)
389 return -EBUSY;
391 ret = as3645a_set_config(flash);
392 if (ret < 0)
393 return ret;
395 flash->led_mode = ctrl->val;
396 return as3645a_set_output(flash, false);
398 case V4L2_CID_FLASH_STROBE_SOURCE:
399 flash->strobe_source = ctrl->val;
401 /* Applies to flash mode only. */
402 if (flash->led_mode != V4L2_FLASH_LED_MODE_FLASH)
403 break;
405 return as3645a_set_output(flash, false);
407 case V4L2_CID_FLASH_STROBE:
408 if (flash->led_mode != V4L2_FLASH_LED_MODE_FLASH)
409 return -EBUSY;
411 return as3645a_set_output(flash, true);
413 case V4L2_CID_FLASH_STROBE_STOP:
414 if (flash->led_mode != V4L2_FLASH_LED_MODE_FLASH)
415 return -EBUSY;
417 return as3645a_set_output(flash, false);
419 case V4L2_CID_FLASH_TIMEOUT:
420 flash->timeout = ctrl->val;
422 /* Applies to flash mode only. */
423 if (flash->led_mode != V4L2_FLASH_LED_MODE_FLASH)
424 break;
426 return as3645a_set_config(flash);
428 case V4L2_CID_FLASH_INTENSITY:
429 flash->flash_current = (ctrl->val - AS3645A_FLASH_INTENSITY_MIN)
430 / AS3645A_FLASH_INTENSITY_STEP;
432 /* Applies to flash mode only. */
433 if (flash->led_mode != V4L2_FLASH_LED_MODE_FLASH)
434 break;
436 return as3645a_set_config(flash);
438 case V4L2_CID_FLASH_TORCH_INTENSITY:
439 flash->assist_current =
440 (ctrl->val - AS3645A_TORCH_INTENSITY_MIN)
441 / AS3645A_TORCH_INTENSITY_STEP;
443 /* Applies to torch mode only. */
444 if (flash->led_mode != V4L2_FLASH_LED_MODE_TORCH)
445 break;
447 return as3645a_set_config(flash);
449 case V4L2_CID_FLASH_INDICATOR_INTENSITY:
450 if (flash->led_mode != V4L2_FLASH_LED_MODE_NONE)
451 return -EBUSY;
453 flash->indicator_current =
454 (ctrl->val - AS3645A_INDICATOR_INTENSITY_MIN)
455 / AS3645A_INDICATOR_INTENSITY_STEP;
457 ret = as3645a_set_config(flash);
458 if (ret < 0)
459 return ret;
461 if ((ctrl->val == 0) == (ctrl->cur.val == 0))
462 break;
464 return as3645a_set_output(flash, false);
467 return 0;
470 static const struct v4l2_ctrl_ops as3645a_ctrl_ops = {
471 .g_volatile_ctrl = as3645a_get_ctrl,
472 .s_ctrl = as3645a_set_ctrl,
475 /* -----------------------------------------------------------------------------
476 * V4L2 subdev core operations
479 /* Put device into know state. */
480 static int as3645a_setup(struct as3645a *flash)
482 struct i2c_client *client = v4l2_get_subdevdata(&flash->subdev);
483 int ret;
485 /* clear errors */
486 ret = as3645a_read(flash, AS_FAULT_INFO_REG);
487 if (ret < 0)
488 return ret;
490 dev_dbg(&client->dev, "Fault info: %02x\n", ret);
492 ret = as3645a_set_config(flash);
493 if (ret < 0)
494 return ret;
496 ret = as3645a_set_output(flash, false);
497 if (ret < 0)
498 return ret;
500 /* read status */
501 ret = as3645a_read_fault(flash);
502 if (ret < 0)
503 return ret;
505 dev_dbg(&client->dev, "AS_INDICATOR_AND_TIMER_REG: %02x\n",
506 as3645a_read(flash, AS_INDICATOR_AND_TIMER_REG));
507 dev_dbg(&client->dev, "AS_CURRENT_SET_REG: %02x\n",
508 as3645a_read(flash, AS_CURRENT_SET_REG));
509 dev_dbg(&client->dev, "AS_CONTROL_REG: %02x\n",
510 as3645a_read(flash, AS_CONTROL_REG));
512 return ret & ~AS_FAULT_INFO_LED_AMOUNT ? -EIO : 0;
515 static int __as3645a_set_power(struct as3645a *flash, int on)
517 int ret;
519 if (!on)
520 as3645a_set_control(flash, AS_MODE_EXT_TORCH, false);
522 if (flash->pdata->set_power) {
523 ret = flash->pdata->set_power(&flash->subdev, on);
524 if (ret < 0)
525 return ret;
528 if (!on)
529 return 0;
531 ret = as3645a_setup(flash);
532 if (ret < 0) {
533 if (flash->pdata->set_power)
534 flash->pdata->set_power(&flash->subdev, 0);
537 return ret;
540 static int as3645a_set_power(struct v4l2_subdev *sd, int on)
542 struct as3645a *flash = to_as3645a(sd);
543 int ret = 0;
545 mutex_lock(&flash->power_lock);
547 if (flash->power_count == !on) {
548 ret = __as3645a_set_power(flash, !!on);
549 if (ret < 0)
550 goto done;
553 flash->power_count += on ? 1 : -1;
554 WARN_ON(flash->power_count < 0);
556 done:
557 mutex_unlock(&flash->power_lock);
558 return ret;
561 static int as3645a_registered(struct v4l2_subdev *sd)
563 struct as3645a *flash = to_as3645a(sd);
564 struct i2c_client *client = v4l2_get_subdevdata(sd);
565 int rval, man, model, rfu, version;
566 const char *vendor;
568 /* Power up the flash driver and read manufacturer ID, model ID, RFU
569 * and version.
571 rval = as3645a_set_power(&flash->subdev, 1);
572 if (rval < 0)
573 return rval;
575 rval = as3645a_read(flash, AS_DESIGN_INFO_REG);
576 if (rval < 0)
577 goto power_off;
579 man = AS_DESIGN_INFO_FACTORY(rval);
580 model = AS_DESIGN_INFO_MODEL(rval);
582 rval = as3645a_read(flash, AS_VERSION_CONTROL_REG);
583 if (rval < 0)
584 goto power_off;
586 rfu = AS_VERSION_CONTROL_RFU(rval);
587 version = AS_VERSION_CONTROL_VERSION(rval);
589 /* Verify the chip model and version. */
590 if (model != 0x01 || rfu != 0x00) {
591 dev_err(&client->dev, "AS3645A not detected "
592 "(model %d rfu %d)\n", model, rfu);
593 rval = -ENODEV;
594 goto power_off;
597 switch (man) {
598 case 1:
599 vendor = "AMS, Austria Micro Systems";
600 break;
601 case 2:
602 vendor = "ADI, Analog Devices Inc.";
603 break;
604 case 3:
605 vendor = "NSC, National Semiconductor";
606 break;
607 case 4:
608 vendor = "NXP";
609 break;
610 case 5:
611 vendor = "TI, Texas Instrument";
612 break;
613 default:
614 vendor = "Unknown";
617 dev_info(&client->dev, "Chip vendor: %s (%d) Version: %d\n", vendor,
618 man, version);
620 rval = as3645a_write(flash, AS_PASSWORD_REG, AS_PASSWORD_UNLOCK_VALUE);
621 if (rval < 0)
622 goto power_off;
624 rval = as3645a_write(flash, AS_BOOST_REG, AS_BOOST_CURRENT_DISABLE);
625 if (rval < 0)
626 goto power_off;
628 /* Setup default values. This makes sure that the chip is in a known
629 * state, in case the power rail can't be controlled.
631 rval = as3645a_setup(flash);
633 power_off:
634 as3645a_set_power(&flash->subdev, 0);
636 return rval;
639 static int as3645a_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
641 return as3645a_set_power(sd, 1);
644 static int as3645a_close(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
646 return as3645a_set_power(sd, 0);
649 static const struct v4l2_subdev_core_ops as3645a_core_ops = {
650 .s_power = as3645a_set_power,
653 static const struct v4l2_subdev_ops as3645a_ops = {
654 .core = &as3645a_core_ops,
657 static const struct v4l2_subdev_internal_ops as3645a_internal_ops = {
658 .registered = as3645a_registered,
659 .open = as3645a_open,
660 .close = as3645a_close,
663 /* -----------------------------------------------------------------------------
664 * I2C driver
666 #ifdef CONFIG_PM
668 static int as3645a_suspend(struct device *dev)
670 struct i2c_client *client = to_i2c_client(dev);
671 struct v4l2_subdev *subdev = i2c_get_clientdata(client);
672 struct as3645a *flash = to_as3645a(subdev);
673 int rval;
675 if (flash->power_count == 0)
676 return 0;
678 rval = __as3645a_set_power(flash, 0);
680 dev_dbg(&client->dev, "Suspend %s\n", rval < 0 ? "failed" : "ok");
682 return rval;
685 static int as3645a_resume(struct device *dev)
687 struct i2c_client *client = to_i2c_client(dev);
688 struct v4l2_subdev *subdev = i2c_get_clientdata(client);
689 struct as3645a *flash = to_as3645a(subdev);
690 int rval;
692 if (flash->power_count == 0)
693 return 0;
695 rval = __as3645a_set_power(flash, 1);
697 dev_dbg(&client->dev, "Resume %s\n", rval < 0 ? "fail" : "ok");
699 return rval;
702 #else
704 #define as3645a_suspend NULL
705 #define as3645a_resume NULL
707 #endif /* CONFIG_PM */
710 * as3645a_init_controls - Create controls
711 * @flash: The flash
713 * The number of LEDs reported in platform data is used to compute default
714 * limits. Parameters passed through platform data can override those limits.
716 static int as3645a_init_controls(struct as3645a *flash)
718 const struct as3645a_platform_data *pdata = flash->pdata;
719 struct v4l2_ctrl *ctrl;
720 int maximum;
722 v4l2_ctrl_handler_init(&flash->ctrls, 10);
724 /* V4L2_CID_FLASH_LED_MODE */
725 v4l2_ctrl_new_std_menu(&flash->ctrls, &as3645a_ctrl_ops,
726 V4L2_CID_FLASH_LED_MODE, 2, ~7,
727 V4L2_FLASH_LED_MODE_NONE);
729 /* V4L2_CID_FLASH_STROBE_SOURCE */
730 v4l2_ctrl_new_std_menu(&flash->ctrls, &as3645a_ctrl_ops,
731 V4L2_CID_FLASH_STROBE_SOURCE,
732 pdata->ext_strobe ? 1 : 0,
733 pdata->ext_strobe ? ~3 : ~1,
734 V4L2_FLASH_STROBE_SOURCE_SOFTWARE);
736 flash->strobe_source = V4L2_FLASH_STROBE_SOURCE_SOFTWARE;
738 /* V4L2_CID_FLASH_STROBE */
739 v4l2_ctrl_new_std(&flash->ctrls, &as3645a_ctrl_ops,
740 V4L2_CID_FLASH_STROBE, 0, 0, 0, 0);
742 /* V4L2_CID_FLASH_STROBE_STOP */
743 v4l2_ctrl_new_std(&flash->ctrls, &as3645a_ctrl_ops,
744 V4L2_CID_FLASH_STROBE_STOP, 0, 0, 0, 0);
746 /* V4L2_CID_FLASH_STROBE_STATUS */
747 ctrl = v4l2_ctrl_new_std(&flash->ctrls, &as3645a_ctrl_ops,
748 V4L2_CID_FLASH_STROBE_STATUS, 0, 1, 1, 1);
749 if (ctrl != NULL)
750 ctrl->flags |= V4L2_CTRL_FLAG_VOLATILE;
752 /* V4L2_CID_FLASH_TIMEOUT */
753 maximum = pdata->timeout_max;
755 v4l2_ctrl_new_std(&flash->ctrls, &as3645a_ctrl_ops,
756 V4L2_CID_FLASH_TIMEOUT, AS3645A_FLASH_TIMEOUT_MIN,
757 maximum, AS3645A_FLASH_TIMEOUT_STEP, maximum);
759 flash->timeout = maximum;
761 /* V4L2_CID_FLASH_INTENSITY */
762 maximum = pdata->flash_max_current;
764 v4l2_ctrl_new_std(&flash->ctrls, &as3645a_ctrl_ops,
765 V4L2_CID_FLASH_INTENSITY, AS3645A_FLASH_INTENSITY_MIN,
766 maximum, AS3645A_FLASH_INTENSITY_STEP, maximum);
768 flash->flash_current = (maximum - AS3645A_FLASH_INTENSITY_MIN)
769 / AS3645A_FLASH_INTENSITY_STEP;
771 /* V4L2_CID_FLASH_TORCH_INTENSITY */
772 maximum = pdata->torch_max_current;
774 v4l2_ctrl_new_std(&flash->ctrls, &as3645a_ctrl_ops,
775 V4L2_CID_FLASH_TORCH_INTENSITY,
776 AS3645A_TORCH_INTENSITY_MIN, maximum,
777 AS3645A_TORCH_INTENSITY_STEP,
778 AS3645A_TORCH_INTENSITY_MIN);
780 flash->assist_current = 0;
782 /* V4L2_CID_FLASH_INDICATOR_INTENSITY */
783 v4l2_ctrl_new_std(&flash->ctrls, &as3645a_ctrl_ops,
784 V4L2_CID_FLASH_INDICATOR_INTENSITY,
785 AS3645A_INDICATOR_INTENSITY_MIN,
786 AS3645A_INDICATOR_INTENSITY_MAX,
787 AS3645A_INDICATOR_INTENSITY_STEP,
788 AS3645A_INDICATOR_INTENSITY_MIN);
790 flash->indicator_current = 0;
792 /* V4L2_CID_FLASH_FAULT */
793 ctrl = v4l2_ctrl_new_std(&flash->ctrls, &as3645a_ctrl_ops,
794 V4L2_CID_FLASH_FAULT, 0,
795 V4L2_FLASH_FAULT_OVER_VOLTAGE |
796 V4L2_FLASH_FAULT_TIMEOUT |
797 V4L2_FLASH_FAULT_OVER_TEMPERATURE |
798 V4L2_FLASH_FAULT_SHORT_CIRCUIT, 0, 0);
799 if (ctrl != NULL)
800 ctrl->flags |= V4L2_CTRL_FLAG_VOLATILE;
802 flash->subdev.ctrl_handler = &flash->ctrls;
804 return flash->ctrls.error;
807 static int as3645a_probe(struct i2c_client *client,
808 const struct i2c_device_id *devid)
810 struct as3645a *flash;
811 int ret;
813 if (client->dev.platform_data == NULL)
814 return -ENODEV;
816 flash = kzalloc(sizeof(*flash), GFP_KERNEL);
817 if (flash == NULL)
818 return -ENOMEM;
820 flash->pdata = client->dev.platform_data;
822 v4l2_i2c_subdev_init(&flash->subdev, client, &as3645a_ops);
823 flash->subdev.internal_ops = &as3645a_internal_ops;
824 flash->subdev.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
826 ret = as3645a_init_controls(flash);
827 if (ret < 0)
828 goto done;
830 ret = media_entity_init(&flash->subdev.entity, 0, NULL, 0);
831 if (ret < 0)
832 goto done;
834 flash->subdev.entity.type = MEDIA_ENT_T_V4L2_SUBDEV_FLASH;
836 mutex_init(&flash->power_lock);
838 flash->led_mode = V4L2_FLASH_LED_MODE_NONE;
840 done:
841 if (ret < 0) {
842 v4l2_ctrl_handler_free(&flash->ctrls);
843 kfree(flash);
846 return ret;
849 static int __exit as3645a_remove(struct i2c_client *client)
851 struct v4l2_subdev *subdev = i2c_get_clientdata(client);
852 struct as3645a *flash = to_as3645a(subdev);
854 v4l2_device_unregister_subdev(subdev);
855 v4l2_ctrl_handler_free(&flash->ctrls);
856 media_entity_cleanup(&flash->subdev.entity);
857 mutex_destroy(&flash->power_lock);
858 kfree(flash);
860 return 0;
863 static const struct i2c_device_id as3645a_id_table[] = {
864 { AS3645A_NAME, 0 },
865 { },
867 MODULE_DEVICE_TABLE(i2c, as3645a_id_table);
869 static const struct dev_pm_ops as3645a_pm_ops = {
870 .suspend = as3645a_suspend,
871 .resume = as3645a_resume,
874 static struct i2c_driver as3645a_i2c_driver = {
875 .driver = {
876 .name = AS3645A_NAME,
877 .pm = &as3645a_pm_ops,
879 .probe = as3645a_probe,
880 .remove = __exit_p(as3645a_remove),
881 .id_table = as3645a_id_table,
884 static int __init as3645a_init(void)
886 int rval;
888 rval = i2c_add_driver(&as3645a_i2c_driver);
889 if (rval)
890 pr_err("%s: Failed to register the driver\n", AS3645A_NAME);
892 return rval;
895 static void __exit as3645a_exit(void)
897 i2c_del_driver(&as3645a_i2c_driver);
900 module_init(as3645a_init);
901 module_exit(as3645a_exit);
903 MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
904 MODULE_DESCRIPTION("LED flash driver for AS3645A, LM3555 and their clones");
905 MODULE_LICENSE("GPL");