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.
23 * - fault interrupt handling
25 * - power doesn't need to be ON if all lights are off
29 #include <linux/delay.h>
30 #include <linux/module.h>
31 #include <linux/i2c.h>
32 #include <linux/slab.h>
34 #include <linux/gpio/consumer.h>
35 #include <media/i2c/adp1653.h>
36 #include <media/v4l2-device.h>
38 #define TIMEOUT_MAX 820000
39 #define TIMEOUT_STEP 54600
40 #define TIMEOUT_MIN (TIMEOUT_MAX - ADP1653_REG_CONFIG_TMR_SET_MAX \
42 #define TIMEOUT_US_TO_CODE(t) ((TIMEOUT_MAX + (TIMEOUT_STEP / 2) - (t)) \
44 #define TIMEOUT_CODE_TO_US(c) (TIMEOUT_MAX - (c) * TIMEOUT_STEP)
46 /* Write values into ADP1653 registers. */
47 static int adp1653_update_hw(struct adp1653_flash *flash)
49 struct i2c_client *client = v4l2_get_subdevdata(&flash->subdev);
54 out_sel = ADP1653_INDICATOR_INTENSITY_uA_TO_REG(
55 flash->indicator_intensity->val)
56 << ADP1653_REG_OUT_SEL_ILED_SHIFT;
58 switch (flash->led_mode->val) {
59 case V4L2_FLASH_LED_MODE_NONE:
61 case V4L2_FLASH_LED_MODE_FLASH:
62 /* Flash mode, light on with strobe, duration from timer */
63 config = ADP1653_REG_CONFIG_TMR_CFG;
64 config |= TIMEOUT_US_TO_CODE(flash->flash_timeout->val)
65 << ADP1653_REG_CONFIG_TMR_SET_SHIFT;
67 case V4L2_FLASH_LED_MODE_TORCH:
68 /* Torch mode, light immediately on, duration indefinite */
69 out_sel |= ADP1653_FLASH_INTENSITY_mA_TO_REG(
70 flash->torch_intensity->val)
71 << ADP1653_REG_OUT_SEL_HPLED_SHIFT;
75 rval = i2c_smbus_write_byte_data(client, ADP1653_REG_OUT_SEL, out_sel);
79 rval = i2c_smbus_write_byte_data(client, ADP1653_REG_CONFIG, config);
86 static int adp1653_get_fault(struct adp1653_flash *flash)
88 struct i2c_client *client = v4l2_get_subdevdata(&flash->subdev);
92 fault = i2c_smbus_read_byte_data(client, ADP1653_REG_FAULT);
96 flash->fault |= fault;
102 rval = i2c_smbus_write_byte_data(client, ADP1653_REG_OUT_SEL, 0);
106 flash->led_mode->val = V4L2_FLASH_LED_MODE_NONE;
108 rval = adp1653_update_hw(flash);
115 static int adp1653_strobe(struct adp1653_flash *flash, int enable)
117 struct i2c_client *client = v4l2_get_subdevdata(&flash->subdev);
118 u8 out_sel = ADP1653_INDICATOR_INTENSITY_uA_TO_REG(
119 flash->indicator_intensity->val)
120 << ADP1653_REG_OUT_SEL_ILED_SHIFT;
123 if (flash->led_mode->val != V4L2_FLASH_LED_MODE_FLASH)
127 return i2c_smbus_write_byte_data(client, ADP1653_REG_OUT_SEL,
130 out_sel |= ADP1653_FLASH_INTENSITY_mA_TO_REG(
131 flash->flash_intensity->val)
132 << ADP1653_REG_OUT_SEL_HPLED_SHIFT;
133 rval = i2c_smbus_write_byte_data(client, ADP1653_REG_OUT_SEL, out_sel);
137 /* Software strobe using i2c */
138 rval = i2c_smbus_write_byte_data(client, ADP1653_REG_SW_STROBE,
139 ADP1653_REG_SW_STROBE_SW_STROBE);
142 return i2c_smbus_write_byte_data(client, ADP1653_REG_SW_STROBE, 0);
145 /* --------------------------------------------------------------------------
149 static int adp1653_get_ctrl(struct v4l2_ctrl *ctrl)
151 struct adp1653_flash *flash =
152 container_of(ctrl->handler, struct adp1653_flash, ctrls);
155 rval = adp1653_get_fault(flash);
161 if (flash->fault & ADP1653_REG_FAULT_FLT_SCP)
162 ctrl->cur.val |= V4L2_FLASH_FAULT_SHORT_CIRCUIT;
163 if (flash->fault & ADP1653_REG_FAULT_FLT_OT)
164 ctrl->cur.val |= V4L2_FLASH_FAULT_OVER_TEMPERATURE;
165 if (flash->fault & ADP1653_REG_FAULT_FLT_TMR)
166 ctrl->cur.val |= V4L2_FLASH_FAULT_TIMEOUT;
167 if (flash->fault & ADP1653_REG_FAULT_FLT_OV)
168 ctrl->cur.val |= V4L2_FLASH_FAULT_OVER_VOLTAGE;
175 static int adp1653_set_ctrl(struct v4l2_ctrl *ctrl)
177 struct adp1653_flash *flash =
178 container_of(ctrl->handler, struct adp1653_flash, ctrls);
181 rval = adp1653_get_fault(flash);
184 if ((rval & (ADP1653_REG_FAULT_FLT_SCP |
185 ADP1653_REG_FAULT_FLT_OT |
186 ADP1653_REG_FAULT_FLT_OV)) &&
187 (ctrl->id == V4L2_CID_FLASH_STROBE ||
188 ctrl->id == V4L2_CID_FLASH_TORCH_INTENSITY ||
189 ctrl->id == V4L2_CID_FLASH_LED_MODE))
193 case V4L2_CID_FLASH_STROBE:
194 return adp1653_strobe(flash, 1);
195 case V4L2_CID_FLASH_STROBE_STOP:
196 return adp1653_strobe(flash, 0);
199 return adp1653_update_hw(flash);
202 static const struct v4l2_ctrl_ops adp1653_ctrl_ops = {
203 .g_volatile_ctrl = adp1653_get_ctrl,
204 .s_ctrl = adp1653_set_ctrl,
207 static int adp1653_init_controls(struct adp1653_flash *flash)
209 struct v4l2_ctrl *fault;
211 v4l2_ctrl_handler_init(&flash->ctrls, 9);
214 v4l2_ctrl_new_std_menu(&flash->ctrls, &adp1653_ctrl_ops,
215 V4L2_CID_FLASH_LED_MODE,
216 V4L2_FLASH_LED_MODE_TORCH, ~0x7, 0);
217 v4l2_ctrl_new_std_menu(&flash->ctrls, &adp1653_ctrl_ops,
218 V4L2_CID_FLASH_STROBE_SOURCE,
219 V4L2_FLASH_STROBE_SOURCE_SOFTWARE, ~0x1, 0);
220 v4l2_ctrl_new_std(&flash->ctrls, &adp1653_ctrl_ops,
221 V4L2_CID_FLASH_STROBE, 0, 0, 0, 0);
222 v4l2_ctrl_new_std(&flash->ctrls, &adp1653_ctrl_ops,
223 V4L2_CID_FLASH_STROBE_STOP, 0, 0, 0, 0);
224 flash->flash_timeout =
225 v4l2_ctrl_new_std(&flash->ctrls, &adp1653_ctrl_ops,
226 V4L2_CID_FLASH_TIMEOUT, TIMEOUT_MIN,
227 flash->platform_data->max_flash_timeout,
229 flash->platform_data->max_flash_timeout);
230 flash->flash_intensity =
231 v4l2_ctrl_new_std(&flash->ctrls, &adp1653_ctrl_ops,
232 V4L2_CID_FLASH_INTENSITY,
233 ADP1653_FLASH_INTENSITY_MIN,
234 flash->platform_data->max_flash_intensity,
235 1, flash->platform_data->max_flash_intensity);
236 flash->torch_intensity =
237 v4l2_ctrl_new_std(&flash->ctrls, &adp1653_ctrl_ops,
238 V4L2_CID_FLASH_TORCH_INTENSITY,
239 ADP1653_TORCH_INTENSITY_MIN,
240 flash->platform_data->max_torch_intensity,
241 ADP1653_FLASH_INTENSITY_STEP,
242 flash->platform_data->max_torch_intensity);
243 flash->indicator_intensity =
244 v4l2_ctrl_new_std(&flash->ctrls, &adp1653_ctrl_ops,
245 V4L2_CID_FLASH_INDICATOR_INTENSITY,
246 ADP1653_INDICATOR_INTENSITY_MIN,
247 flash->platform_data->max_indicator_intensity,
248 ADP1653_INDICATOR_INTENSITY_STEP,
249 ADP1653_INDICATOR_INTENSITY_MIN);
250 fault = v4l2_ctrl_new_std(&flash->ctrls, &adp1653_ctrl_ops,
251 V4L2_CID_FLASH_FAULT, 0,
252 V4L2_FLASH_FAULT_OVER_VOLTAGE
253 | V4L2_FLASH_FAULT_OVER_TEMPERATURE
254 | V4L2_FLASH_FAULT_SHORT_CIRCUIT, 0, 0);
256 if (flash->ctrls.error)
257 return flash->ctrls.error;
259 fault->flags |= V4L2_CTRL_FLAG_VOLATILE;
261 flash->subdev.ctrl_handler = &flash->ctrls;
265 /* --------------------------------------------------------------------------
266 * V4L2 subdev operations
270 adp1653_init_device(struct adp1653_flash *flash)
272 struct i2c_client *client = v4l2_get_subdevdata(&flash->subdev);
275 /* Clear FAULT register by writing zero to OUT_SEL */
276 rval = i2c_smbus_write_byte_data(client, ADP1653_REG_OUT_SEL, 0);
278 dev_err(&client->dev, "failed writing fault register\n");
282 mutex_lock(flash->ctrls.lock);
283 /* Reset faults before reading new ones. */
285 rval = adp1653_get_fault(flash);
286 mutex_unlock(flash->ctrls.lock);
288 dev_err(&client->dev, "faults detected: 0x%1.1x\n", rval);
292 mutex_lock(flash->ctrls.lock);
293 rval = adp1653_update_hw(flash);
294 mutex_unlock(flash->ctrls.lock);
296 dev_err(&client->dev,
297 "adp1653_update_hw failed at %s\n", __func__);
305 __adp1653_set_power(struct adp1653_flash *flash, int on)
309 if (flash->platform_data->power) {
310 ret = flash->platform_data->power(&flash->subdev, on);
314 gpiod_set_value(flash->platform_data->enable_gpio, on);
316 /* Some delay is apparently required. */
323 ret = adp1653_init_device(flash);
327 if (flash->platform_data->power)
328 flash->platform_data->power(&flash->subdev, 0);
330 gpiod_set_value(flash->platform_data->enable_gpio, 0);
336 adp1653_set_power(struct v4l2_subdev *subdev, int on)
338 struct adp1653_flash *flash = to_adp1653_flash(subdev);
341 mutex_lock(&flash->power_lock);
343 /* If the power count is modified from 0 to != 0 or from != 0 to 0,
344 * update the power state.
346 if (flash->power_count == !on) {
347 ret = __adp1653_set_power(flash, !!on);
352 /* Update the power count. */
353 flash->power_count += on ? 1 : -1;
354 WARN_ON(flash->power_count < 0);
357 mutex_unlock(&flash->power_lock);
361 static int adp1653_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
363 return adp1653_set_power(sd, 1);
366 static int adp1653_close(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
368 return adp1653_set_power(sd, 0);
371 static const struct v4l2_subdev_core_ops adp1653_core_ops = {
372 .s_power = adp1653_set_power,
375 static const struct v4l2_subdev_ops adp1653_ops = {
376 .core = &adp1653_core_ops,
379 static const struct v4l2_subdev_internal_ops adp1653_internal_ops = {
380 .open = adp1653_open,
381 .close = adp1653_close,
384 /* --------------------------------------------------------------------------
389 static int adp1653_suspend(struct device *dev)
391 struct i2c_client *client = to_i2c_client(dev);
392 struct v4l2_subdev *subdev = i2c_get_clientdata(client);
393 struct adp1653_flash *flash = to_adp1653_flash(subdev);
395 if (!flash->power_count)
398 return __adp1653_set_power(flash, 0);
401 static int adp1653_resume(struct device *dev)
403 struct i2c_client *client = to_i2c_client(dev);
404 struct v4l2_subdev *subdev = i2c_get_clientdata(client);
405 struct adp1653_flash *flash = to_adp1653_flash(subdev);
407 if (!flash->power_count)
410 return __adp1653_set_power(flash, 1);
415 #define adp1653_suspend NULL
416 #define adp1653_resume NULL
418 #endif /* CONFIG_PM */
420 static int adp1653_of_init(struct i2c_client *client,
421 struct adp1653_flash *flash,
422 struct device_node *node)
424 struct adp1653_platform_data *pd;
425 struct device_node *child;
427 pd = devm_kzalloc(&client->dev, sizeof(*pd), GFP_KERNEL);
430 flash->platform_data = pd;
432 child = of_get_child_by_name(node, "flash");
436 if (of_property_read_u32(child, "flash-timeout-us",
437 &pd->max_flash_timeout))
440 if (of_property_read_u32(child, "flash-max-microamp",
441 &pd->max_flash_intensity))
444 pd->max_flash_intensity /= 1000;
446 if (of_property_read_u32(child, "led-max-microamp",
447 &pd->max_torch_intensity))
450 pd->max_torch_intensity /= 1000;
453 child = of_get_child_by_name(node, "indicator");
457 if (of_property_read_u32(child, "led-max-microamp",
458 &pd->max_indicator_intensity))
463 pd->enable_gpio = devm_gpiod_get(&client->dev, "enable", GPIOD_OUT_LOW);
464 if (IS_ERR(pd->enable_gpio)) {
465 dev_err(&client->dev, "Error getting GPIO\n");
466 return PTR_ERR(pd->enable_gpio);
471 dev_err(&client->dev, "Required property not found\n");
477 static int adp1653_probe(struct i2c_client *client,
478 const struct i2c_device_id *devid)
480 struct adp1653_flash *flash;
483 flash = devm_kzalloc(&client->dev, sizeof(*flash), GFP_KERNEL);
487 if (client->dev.of_node) {
488 ret = adp1653_of_init(client, flash, client->dev.of_node);
492 if (!client->dev.platform_data) {
493 dev_err(&client->dev,
494 "Neither DT not platform data provided\n");
497 flash->platform_data = client->dev.platform_data;
500 mutex_init(&flash->power_lock);
502 v4l2_i2c_subdev_init(&flash->subdev, client, &adp1653_ops);
503 flash->subdev.internal_ops = &adp1653_internal_ops;
504 flash->subdev.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
506 ret = adp1653_init_controls(flash);
510 ret = media_entity_pads_init(&flash->subdev.entity, 0, NULL);
514 flash->subdev.entity.function = MEDIA_ENT_F_FLASH;
519 dev_err(&client->dev, "adp1653: failed to register device\n");
520 v4l2_ctrl_handler_free(&flash->ctrls);
524 static int adp1653_remove(struct i2c_client *client)
526 struct v4l2_subdev *subdev = i2c_get_clientdata(client);
527 struct adp1653_flash *flash = to_adp1653_flash(subdev);
529 v4l2_device_unregister_subdev(&flash->subdev);
530 v4l2_ctrl_handler_free(&flash->ctrls);
531 media_entity_cleanup(&flash->subdev.entity);
536 static const struct i2c_device_id adp1653_id_table[] = {
540 MODULE_DEVICE_TABLE(i2c, adp1653_id_table);
542 static const struct dev_pm_ops adp1653_pm_ops = {
543 .suspend = adp1653_suspend,
544 .resume = adp1653_resume,
547 static struct i2c_driver adp1653_i2c_driver = {
549 .name = ADP1653_NAME,
550 .pm = &adp1653_pm_ops,
552 .probe = adp1653_probe,
553 .remove = adp1653_remove,
554 .id_table = adp1653_id_table,
557 module_i2c_driver(adp1653_i2c_driver);
559 MODULE_AUTHOR("Sakari Ailus <sakari.ailus@nokia.com>");
560 MODULE_DESCRIPTION("Analog Devices ADP1653 LED flash driver");
561 MODULE_LICENSE("GPL");