2 * drivers/media/i2c/adp1653.c
4 * Copyright (C) 2008--2011 Nokia Corporation
6 * Contact: Sakari Ailus <sakari.ailus@iki.fi>
9 * Sakari Ailus <sakari.ailus@iki.fi>
10 * Tuukka Toivonen <tuukkat76@gmail.com>
11 * Pavel Machek <pavel@ucw.cz>
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * version 2 as published by the Free Software Foundation.
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
28 * - fault interrupt handling
30 * - power doesn't need to be ON if all lights are off
34 #include <linux/delay.h>
35 #include <linux/module.h>
36 #include <linux/i2c.h>
37 #include <linux/slab.h>
38 #include <linux/of_gpio.h>
39 #include <linux/gpio.h>
40 #include <media/adp1653.h>
41 #include <media/v4l2-device.h>
43 #define TIMEOUT_MAX 820000
44 #define TIMEOUT_STEP 54600
45 #define TIMEOUT_MIN (TIMEOUT_MAX - ADP1653_REG_CONFIG_TMR_SET_MAX \
47 #define TIMEOUT_US_TO_CODE(t) ((TIMEOUT_MAX + (TIMEOUT_STEP / 2) - (t)) \
49 #define TIMEOUT_CODE_TO_US(c) (TIMEOUT_MAX - (c) * TIMEOUT_STEP)
51 /* Write values into ADP1653 registers. */
52 static int adp1653_update_hw(struct adp1653_flash *flash)
54 struct i2c_client *client = v4l2_get_subdevdata(&flash->subdev);
59 out_sel = ADP1653_INDICATOR_INTENSITY_uA_TO_REG(
60 flash->indicator_intensity->val)
61 << ADP1653_REG_OUT_SEL_ILED_SHIFT;
63 switch (flash->led_mode->val) {
64 case V4L2_FLASH_LED_MODE_NONE:
66 case V4L2_FLASH_LED_MODE_FLASH:
67 /* Flash mode, light on with strobe, duration from timer */
68 config = ADP1653_REG_CONFIG_TMR_CFG;
69 config |= TIMEOUT_US_TO_CODE(flash->flash_timeout->val)
70 << ADP1653_REG_CONFIG_TMR_SET_SHIFT;
72 case V4L2_FLASH_LED_MODE_TORCH:
73 /* Torch mode, light immediately on, duration indefinite */
74 out_sel |= ADP1653_FLASH_INTENSITY_mA_TO_REG(
75 flash->torch_intensity->val)
76 << ADP1653_REG_OUT_SEL_HPLED_SHIFT;
80 rval = i2c_smbus_write_byte_data(client, ADP1653_REG_OUT_SEL, out_sel);
84 rval = i2c_smbus_write_byte_data(client, ADP1653_REG_CONFIG, config);
91 static int adp1653_get_fault(struct adp1653_flash *flash)
93 struct i2c_client *client = v4l2_get_subdevdata(&flash->subdev);
97 fault = i2c_smbus_read_byte_data(client, ADP1653_REG_FAULT);
98 if (IS_ERR_VALUE(fault))
101 flash->fault |= fault;
107 rval = i2c_smbus_write_byte_data(client, ADP1653_REG_OUT_SEL, 0);
108 if (IS_ERR_VALUE(rval))
111 flash->led_mode->val = V4L2_FLASH_LED_MODE_NONE;
113 rval = adp1653_update_hw(flash);
114 if (IS_ERR_VALUE(rval))
120 static int adp1653_strobe(struct adp1653_flash *flash, int enable)
122 struct i2c_client *client = v4l2_get_subdevdata(&flash->subdev);
123 u8 out_sel = ADP1653_INDICATOR_INTENSITY_uA_TO_REG(
124 flash->indicator_intensity->val)
125 << ADP1653_REG_OUT_SEL_ILED_SHIFT;
128 if (flash->led_mode->val != V4L2_FLASH_LED_MODE_FLASH)
132 return i2c_smbus_write_byte_data(client, ADP1653_REG_OUT_SEL,
135 out_sel |= ADP1653_FLASH_INTENSITY_mA_TO_REG(
136 flash->flash_intensity->val)
137 << ADP1653_REG_OUT_SEL_HPLED_SHIFT;
138 rval = i2c_smbus_write_byte_data(client, ADP1653_REG_OUT_SEL, out_sel);
142 /* Software strobe using i2c */
143 rval = i2c_smbus_write_byte_data(client, ADP1653_REG_SW_STROBE,
144 ADP1653_REG_SW_STROBE_SW_STROBE);
147 return i2c_smbus_write_byte_data(client, ADP1653_REG_SW_STROBE, 0);
150 /* --------------------------------------------------------------------------
154 static int adp1653_get_ctrl(struct v4l2_ctrl *ctrl)
156 struct adp1653_flash *flash =
157 container_of(ctrl->handler, struct adp1653_flash, ctrls);
160 rval = adp1653_get_fault(flash);
161 if (IS_ERR_VALUE(rval))
166 if (flash->fault & ADP1653_REG_FAULT_FLT_SCP)
167 ctrl->cur.val |= V4L2_FLASH_FAULT_SHORT_CIRCUIT;
168 if (flash->fault & ADP1653_REG_FAULT_FLT_OT)
169 ctrl->cur.val |= V4L2_FLASH_FAULT_OVER_TEMPERATURE;
170 if (flash->fault & ADP1653_REG_FAULT_FLT_TMR)
171 ctrl->cur.val |= V4L2_FLASH_FAULT_TIMEOUT;
172 if (flash->fault & ADP1653_REG_FAULT_FLT_OV)
173 ctrl->cur.val |= V4L2_FLASH_FAULT_OVER_VOLTAGE;
180 static int adp1653_set_ctrl(struct v4l2_ctrl *ctrl)
182 struct adp1653_flash *flash =
183 container_of(ctrl->handler, struct adp1653_flash, ctrls);
186 rval = adp1653_get_fault(flash);
187 if (IS_ERR_VALUE(rval))
189 if ((rval & (ADP1653_REG_FAULT_FLT_SCP |
190 ADP1653_REG_FAULT_FLT_OT |
191 ADP1653_REG_FAULT_FLT_OV)) &&
192 (ctrl->id == V4L2_CID_FLASH_STROBE ||
193 ctrl->id == V4L2_CID_FLASH_TORCH_INTENSITY ||
194 ctrl->id == V4L2_CID_FLASH_LED_MODE))
198 case V4L2_CID_FLASH_STROBE:
199 return adp1653_strobe(flash, 1);
200 case V4L2_CID_FLASH_STROBE_STOP:
201 return adp1653_strobe(flash, 0);
204 return adp1653_update_hw(flash);
207 static const struct v4l2_ctrl_ops adp1653_ctrl_ops = {
208 .g_volatile_ctrl = adp1653_get_ctrl,
209 .s_ctrl = adp1653_set_ctrl,
212 static int adp1653_init_controls(struct adp1653_flash *flash)
214 struct v4l2_ctrl *fault;
216 v4l2_ctrl_handler_init(&flash->ctrls, 9);
219 v4l2_ctrl_new_std_menu(&flash->ctrls, &adp1653_ctrl_ops,
220 V4L2_CID_FLASH_LED_MODE,
221 V4L2_FLASH_LED_MODE_TORCH, ~0x7, 0);
222 v4l2_ctrl_new_std_menu(&flash->ctrls, &adp1653_ctrl_ops,
223 V4L2_CID_FLASH_STROBE_SOURCE,
224 V4L2_FLASH_STROBE_SOURCE_SOFTWARE, ~0x1, 0);
225 v4l2_ctrl_new_std(&flash->ctrls, &adp1653_ctrl_ops,
226 V4L2_CID_FLASH_STROBE, 0, 0, 0, 0);
227 v4l2_ctrl_new_std(&flash->ctrls, &adp1653_ctrl_ops,
228 V4L2_CID_FLASH_STROBE_STOP, 0, 0, 0, 0);
229 flash->flash_timeout =
230 v4l2_ctrl_new_std(&flash->ctrls, &adp1653_ctrl_ops,
231 V4L2_CID_FLASH_TIMEOUT, TIMEOUT_MIN,
232 flash->platform_data->max_flash_timeout,
234 flash->platform_data->max_flash_timeout);
235 flash->flash_intensity =
236 v4l2_ctrl_new_std(&flash->ctrls, &adp1653_ctrl_ops,
237 V4L2_CID_FLASH_INTENSITY,
238 ADP1653_FLASH_INTENSITY_MIN,
239 flash->platform_data->max_flash_intensity,
240 1, flash->platform_data->max_flash_intensity);
241 flash->torch_intensity =
242 v4l2_ctrl_new_std(&flash->ctrls, &adp1653_ctrl_ops,
243 V4L2_CID_FLASH_TORCH_INTENSITY,
244 ADP1653_TORCH_INTENSITY_MIN,
245 flash->platform_data->max_torch_intensity,
246 ADP1653_FLASH_INTENSITY_STEP,
247 flash->platform_data->max_torch_intensity);
248 flash->indicator_intensity =
249 v4l2_ctrl_new_std(&flash->ctrls, &adp1653_ctrl_ops,
250 V4L2_CID_FLASH_INDICATOR_INTENSITY,
251 ADP1653_INDICATOR_INTENSITY_MIN,
252 flash->platform_data->max_indicator_intensity,
253 ADP1653_INDICATOR_INTENSITY_STEP,
254 ADP1653_INDICATOR_INTENSITY_MIN);
255 fault = v4l2_ctrl_new_std(&flash->ctrls, &adp1653_ctrl_ops,
256 V4L2_CID_FLASH_FAULT, 0,
257 V4L2_FLASH_FAULT_OVER_VOLTAGE
258 | V4L2_FLASH_FAULT_OVER_TEMPERATURE
259 | V4L2_FLASH_FAULT_SHORT_CIRCUIT, 0, 0);
261 if (flash->ctrls.error)
262 return flash->ctrls.error;
264 fault->flags |= V4L2_CTRL_FLAG_VOLATILE;
266 flash->subdev.ctrl_handler = &flash->ctrls;
270 /* --------------------------------------------------------------------------
271 * V4L2 subdev operations
275 adp1653_init_device(struct adp1653_flash *flash)
277 struct i2c_client *client = v4l2_get_subdevdata(&flash->subdev);
280 /* Clear FAULT register by writing zero to OUT_SEL */
281 rval = i2c_smbus_write_byte_data(client, ADP1653_REG_OUT_SEL, 0);
283 dev_err(&client->dev, "failed writing fault register\n");
287 mutex_lock(flash->ctrls.lock);
288 /* Reset faults before reading new ones. */
290 rval = adp1653_get_fault(flash);
291 mutex_unlock(flash->ctrls.lock);
293 dev_err(&client->dev, "faults detected: 0x%1.1x\n", rval);
297 mutex_lock(flash->ctrls.lock);
298 rval = adp1653_update_hw(flash);
299 mutex_unlock(flash->ctrls.lock);
301 dev_err(&client->dev,
302 "adp1653_update_hw failed at %s\n", __func__);
310 __adp1653_set_power(struct adp1653_flash *flash, int on)
314 if (flash->platform_data->power) {
315 ret = flash->platform_data->power(&flash->subdev, on);
317 gpio_set_value(flash->platform_data->power_gpio, on);
319 /* Some delay is apparently required. */
329 ret = adp1653_init_device(flash);
333 if (flash->platform_data->power)
334 flash->platform_data->power(&flash->subdev, 0);
336 gpio_set_value(flash->platform_data->power_gpio, 0);
342 adp1653_set_power(struct v4l2_subdev *subdev, int on)
344 struct adp1653_flash *flash = to_adp1653_flash(subdev);
347 mutex_lock(&flash->power_lock);
349 /* If the power count is modified from 0 to != 0 or from != 0 to 0,
350 * update the power state.
352 if (flash->power_count == !on) {
353 ret = __adp1653_set_power(flash, !!on);
358 /* Update the power count. */
359 flash->power_count += on ? 1 : -1;
360 WARN_ON(flash->power_count < 0);
363 mutex_unlock(&flash->power_lock);
367 static int adp1653_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
369 return adp1653_set_power(sd, 1);
372 static int adp1653_close(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
374 return adp1653_set_power(sd, 0);
377 static const struct v4l2_subdev_core_ops adp1653_core_ops = {
378 .s_power = adp1653_set_power,
381 static const struct v4l2_subdev_ops adp1653_ops = {
382 .core = &adp1653_core_ops,
385 static const struct v4l2_subdev_internal_ops adp1653_internal_ops = {
386 .open = adp1653_open,
387 .close = adp1653_close,
390 /* --------------------------------------------------------------------------
395 static int adp1653_suspend(struct device *dev)
397 struct i2c_client *client = to_i2c_client(dev);
398 struct v4l2_subdev *subdev = i2c_get_clientdata(client);
399 struct adp1653_flash *flash = to_adp1653_flash(subdev);
401 if (!flash->power_count)
404 return __adp1653_set_power(flash, 0);
407 static int adp1653_resume(struct device *dev)
409 struct i2c_client *client = to_i2c_client(dev);
410 struct v4l2_subdev *subdev = i2c_get_clientdata(client);
411 struct adp1653_flash *flash = to_adp1653_flash(subdev);
413 if (!flash->power_count)
416 return __adp1653_set_power(flash, 1);
421 #define adp1653_suspend NULL
422 #define adp1653_resume NULL
424 #endif /* CONFIG_PM */
426 static int adp1653_of_init(struct i2c_client *client,
427 struct adp1653_flash *flash,
428 struct device_node *node)
431 struct adp1653_platform_data *pd;
432 enum of_gpio_flags flags;
434 struct device_node *child;
439 pd = devm_kzalloc(&client->dev, sizeof(*pd), GFP_KERNEL);
442 flash->platform_data = pd;
444 child = of_get_child_by_name(node, "flash");
447 if (of_property_read_u32(child, "flash-timeout-microsec", &val))
450 pd->max_flash_timeout = val;
451 if (of_property_read_u32(child, "flash-max-microamp", &val))
453 pd->max_flash_intensity = val/1000;
455 if (of_property_read_u32(child, "max-microamp", &val))
457 pd->max_torch_intensity = val/1000;
459 child = of_get_child_by_name(node, "indicator");
462 if (of_property_read_u32(child, "max-microamp", &val))
464 pd->max_indicator_intensity = val;
466 if (!of_find_property(node, "gpios", NULL)) {
467 dev_err(&client->dev, "No gpio node\n");
471 pd->power_gpio = of_get_gpio_flags(node, 0, &flags);
472 if (pd->power_gpio < 0) {
473 dev_err(&client->dev, "Error getting GPIO\n");
481 static int adp1653_probe(struct i2c_client *client,
482 const struct i2c_device_id *devid)
484 struct adp1653_flash *flash;
487 flash = devm_kzalloc(&client->dev, sizeof(*flash), GFP_KERNEL);
491 flash->platform_data = client->dev.platform_data;
492 if (!flash->platform_data) {
493 ret = adp1653_of_init(client, flash, client->dev.of_node);
498 mutex_init(&flash->power_lock);
500 v4l2_i2c_subdev_init(&flash->subdev, client, &adp1653_ops);
501 flash->subdev.internal_ops = &adp1653_internal_ops;
502 flash->subdev.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
504 ret = adp1653_init_controls(flash);
508 ret = media_entity_init(&flash->subdev.entity, 0, NULL, 0);
512 flash->subdev.entity.type = MEDIA_ENT_T_V4L2_SUBDEV_FLASH;
516 dev_err(&client->dev, "adp1653: failed to register device\n");
517 v4l2_ctrl_handler_free(&flash->ctrls);
521 static int adp1653_remove(struct i2c_client *client)
523 struct v4l2_subdev *subdev = i2c_get_clientdata(client);
524 struct adp1653_flash *flash = to_adp1653_flash(subdev);
526 v4l2_device_unregister_subdev(&flash->subdev);
527 v4l2_ctrl_handler_free(&flash->ctrls);
528 media_entity_cleanup(&flash->subdev.entity);
533 static const struct i2c_device_id adp1653_id_table[] = {
537 MODULE_DEVICE_TABLE(i2c, adp1653_id_table);
539 static const struct dev_pm_ops adp1653_pm_ops = {
540 .suspend = adp1653_suspend,
541 .resume = adp1653_resume,
544 static struct i2c_driver adp1653_i2c_driver = {
546 .name = ADP1653_NAME,
547 .pm = &adp1653_pm_ops,
549 .probe = adp1653_probe,
550 .remove = adp1653_remove,
551 .id_table = adp1653_id_table,
554 module_i2c_driver(adp1653_i2c_driver);
556 MODULE_AUTHOR("Sakari Ailus <sakari.ailus@nokia.com>");
557 MODULE_DESCRIPTION("Analog Devices ADP1653 LED flash driver");
558 MODULE_LICENSE("GPL");