1 /* 2 * drivers/media/i2c/adp1653.c 3 * 4 * Copyright (C) 2008--2011 Nokia Corporation 5 * 6 * Contact: Sakari Ailus <sakari.ailus@iki.fi> 7 * 8 * Contributors: 9 * Sakari Ailus <sakari.ailus@iki.fi> 10 * Tuukka Toivonen <tuukkat76@gmail.com> 11 * Pavel Machek <pavel@ucw.cz> 12 * 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. 16 * 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. 21 * 22 * TODO: 23 * - fault interrupt handling 24 * - hardware strobe 25 * - power doesn't need to be ON if all lights are off 26 * 27 */ 28 29 #include <linux/delay.h> 30 #include <linux/module.h> 31 #include <linux/i2c.h> 32 #include <linux/slab.h> 33 #include <linux/of.h> 34 #include <linux/gpio/consumer.h> 35 #include <media/i2c/adp1653.h> 36 #include <media/v4l2-device.h> 37 38 #define TIMEOUT_MAX 820000 39 #define TIMEOUT_STEP 54600 40 #define TIMEOUT_MIN (TIMEOUT_MAX - ADP1653_REG_CONFIG_TMR_SET_MAX \ 41 * TIMEOUT_STEP) 42 #define TIMEOUT_US_TO_CODE(t) ((TIMEOUT_MAX + (TIMEOUT_STEP / 2) - (t)) \ 43 / TIMEOUT_STEP) 44 #define TIMEOUT_CODE_TO_US(c) (TIMEOUT_MAX - (c) * TIMEOUT_STEP) 45 46 /* Write values into ADP1653 registers. */ 47 static int adp1653_update_hw(struct adp1653_flash *flash) 48 { 49 struct i2c_client *client = v4l2_get_subdevdata(&flash->subdev); 50 u8 out_sel; 51 u8 config = 0; 52 int rval; 53 54 out_sel = ADP1653_INDICATOR_INTENSITY_uA_TO_REG( 55 flash->indicator_intensity->val) 56 << ADP1653_REG_OUT_SEL_ILED_SHIFT; 57 58 switch (flash->led_mode->val) { 59 case V4L2_FLASH_LED_MODE_NONE: 60 break; 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; 66 break; 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; 72 break; 73 } 74 75 rval = i2c_smbus_write_byte_data(client, ADP1653_REG_OUT_SEL, out_sel); 76 if (rval < 0) 77 return rval; 78 79 rval = i2c_smbus_write_byte_data(client, ADP1653_REG_CONFIG, config); 80 if (rval < 0) 81 return rval; 82 83 return 0; 84 } 85 86 static int adp1653_get_fault(struct adp1653_flash *flash) 87 { 88 struct i2c_client *client = v4l2_get_subdevdata(&flash->subdev); 89 int fault; 90 int rval; 91 92 fault = i2c_smbus_read_byte_data(client, ADP1653_REG_FAULT); 93 if (fault < 0) 94 return fault; 95 96 flash->fault |= fault; 97 98 if (!flash->fault) 99 return 0; 100 101 /* Clear faults. */ 102 rval = i2c_smbus_write_byte_data(client, ADP1653_REG_OUT_SEL, 0); 103 if (rval < 0) 104 return rval; 105 106 flash->led_mode->val = V4L2_FLASH_LED_MODE_NONE; 107 108 rval = adp1653_update_hw(flash); 109 if (rval) 110 return rval; 111 112 return flash->fault; 113 } 114 115 static int adp1653_strobe(struct adp1653_flash *flash, int enable) 116 { 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; 121 int rval; 122 123 if (flash->led_mode->val != V4L2_FLASH_LED_MODE_FLASH) 124 return -EBUSY; 125 126 if (!enable) 127 return i2c_smbus_write_byte_data(client, ADP1653_REG_OUT_SEL, 128 out_sel); 129 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); 134 if (rval) 135 return rval; 136 137 /* Software strobe using i2c */ 138 rval = i2c_smbus_write_byte_data(client, ADP1653_REG_SW_STROBE, 139 ADP1653_REG_SW_STROBE_SW_STROBE); 140 if (rval) 141 return rval; 142 return i2c_smbus_write_byte_data(client, ADP1653_REG_SW_STROBE, 0); 143 } 144 145 /* -------------------------------------------------------------------------- 146 * V4L2 controls 147 */ 148 149 static int adp1653_get_ctrl(struct v4l2_ctrl *ctrl) 150 { 151 struct adp1653_flash *flash = 152 container_of(ctrl->handler, struct adp1653_flash, ctrls); 153 int rval; 154 155 rval = adp1653_get_fault(flash); 156 if (rval) 157 return rval; 158 159 ctrl->cur.val = 0; 160 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; 169 170 flash->fault = 0; 171 172 return 0; 173 } 174 175 static int adp1653_set_ctrl(struct v4l2_ctrl *ctrl) 176 { 177 struct adp1653_flash *flash = 178 container_of(ctrl->handler, struct adp1653_flash, ctrls); 179 int rval; 180 181 rval = adp1653_get_fault(flash); 182 if (rval) 183 return rval; 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)) 190 return -EBUSY; 191 192 switch (ctrl->id) { 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); 197 } 198 199 return adp1653_update_hw(flash); 200 } 201 202 static const struct v4l2_ctrl_ops adp1653_ctrl_ops = { 203 .g_volatile_ctrl = adp1653_get_ctrl, 204 .s_ctrl = adp1653_set_ctrl, 205 }; 206 207 static int adp1653_init_controls(struct adp1653_flash *flash) 208 { 209 struct v4l2_ctrl *fault; 210 211 v4l2_ctrl_handler_init(&flash->ctrls, 9); 212 213 flash->led_mode = 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, 228 TIMEOUT_STEP, 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); 255 256 if (flash->ctrls.error) 257 return flash->ctrls.error; 258 259 fault->flags |= V4L2_CTRL_FLAG_VOLATILE; 260 261 flash->subdev.ctrl_handler = &flash->ctrls; 262 return 0; 263 } 264 265 /* -------------------------------------------------------------------------- 266 * V4L2 subdev operations 267 */ 268 269 static int 270 adp1653_init_device(struct adp1653_flash *flash) 271 { 272 struct i2c_client *client = v4l2_get_subdevdata(&flash->subdev); 273 int rval; 274 275 /* Clear FAULT register by writing zero to OUT_SEL */ 276 rval = i2c_smbus_write_byte_data(client, ADP1653_REG_OUT_SEL, 0); 277 if (rval < 0) { 278 dev_err(&client->dev, "failed writing fault register\n"); 279 return -EIO; 280 } 281 282 mutex_lock(flash->ctrls.lock); 283 /* Reset faults before reading new ones. */ 284 flash->fault = 0; 285 rval = adp1653_get_fault(flash); 286 mutex_unlock(flash->ctrls.lock); 287 if (rval > 0) { 288 dev_err(&client->dev, "faults detected: 0x%1.1x\n", rval); 289 return -EIO; 290 } 291 292 mutex_lock(flash->ctrls.lock); 293 rval = adp1653_update_hw(flash); 294 mutex_unlock(flash->ctrls.lock); 295 if (rval) { 296 dev_err(&client->dev, 297 "adp1653_update_hw failed at %s\n", __func__); 298 return -EIO; 299 } 300 301 return 0; 302 } 303 304 static int 305 __adp1653_set_power(struct adp1653_flash *flash, int on) 306 { 307 int ret; 308 309 if (flash->platform_data->power) { 310 ret = flash->platform_data->power(&flash->subdev, on); 311 if (ret < 0) 312 return ret; 313 } else { 314 gpiod_set_value(flash->platform_data->enable_gpio, on); 315 if (on) 316 /* Some delay is apparently required. */ 317 udelay(20); 318 } 319 320 if (!on) 321 return 0; 322 323 ret = adp1653_init_device(flash); 324 if (ret >= 0) 325 return ret; 326 327 if (flash->platform_data->power) 328 flash->platform_data->power(&flash->subdev, 0); 329 else 330 gpiod_set_value(flash->platform_data->enable_gpio, 0); 331 332 return ret; 333 } 334 335 static int 336 adp1653_set_power(struct v4l2_subdev *subdev, int on) 337 { 338 struct adp1653_flash *flash = to_adp1653_flash(subdev); 339 int ret = 0; 340 341 mutex_lock(&flash->power_lock); 342 343 /* If the power count is modified from 0 to != 0 or from != 0 to 0, 344 * update the power state. 345 */ 346 if (flash->power_count == !on) { 347 ret = __adp1653_set_power(flash, !!on); 348 if (ret < 0) 349 goto done; 350 } 351 352 /* Update the power count. */ 353 flash->power_count += on ? 1 : -1; 354 WARN_ON(flash->power_count < 0); 355 356 done: 357 mutex_unlock(&flash->power_lock); 358 return ret; 359 } 360 361 static int adp1653_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh) 362 { 363 return adp1653_set_power(sd, 1); 364 } 365 366 static int adp1653_close(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh) 367 { 368 return adp1653_set_power(sd, 0); 369 } 370 371 static const struct v4l2_subdev_core_ops adp1653_core_ops = { 372 .s_power = adp1653_set_power, 373 }; 374 375 static const struct v4l2_subdev_ops adp1653_ops = { 376 .core = &adp1653_core_ops, 377 }; 378 379 static const struct v4l2_subdev_internal_ops adp1653_internal_ops = { 380 .open = adp1653_open, 381 .close = adp1653_close, 382 }; 383 384 /* -------------------------------------------------------------------------- 385 * I2C driver 386 */ 387 #ifdef CONFIG_PM 388 389 static int adp1653_suspend(struct device *dev) 390 { 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); 394 395 if (!flash->power_count) 396 return 0; 397 398 return __adp1653_set_power(flash, 0); 399 } 400 401 static int adp1653_resume(struct device *dev) 402 { 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); 406 407 if (!flash->power_count) 408 return 0; 409 410 return __adp1653_set_power(flash, 1); 411 } 412 413 #else 414 415 #define adp1653_suspend NULL 416 #define adp1653_resume NULL 417 418 #endif /* CONFIG_PM */ 419 420 static int adp1653_of_init(struct i2c_client *client, 421 struct adp1653_flash *flash, 422 struct device_node *node) 423 { 424 struct adp1653_platform_data *pd; 425 struct device_node *child; 426 427 pd = devm_kzalloc(&client->dev, sizeof(*pd), GFP_KERNEL); 428 if (!pd) 429 return -ENOMEM; 430 flash->platform_data = pd; 431 432 child = of_get_child_by_name(node, "flash"); 433 if (!child) 434 return -EINVAL; 435 436 if (of_property_read_u32(child, "flash-timeout-us", 437 &pd->max_flash_timeout)) 438 goto err; 439 440 if (of_property_read_u32(child, "flash-max-microamp", 441 &pd->max_flash_intensity)) 442 goto err; 443 444 pd->max_flash_intensity /= 1000; 445 446 if (of_property_read_u32(child, "led-max-microamp", 447 &pd->max_torch_intensity)) 448 goto err; 449 450 pd->max_torch_intensity /= 1000; 451 of_node_put(child); 452 453 child = of_get_child_by_name(node, "indicator"); 454 if (!child) 455 return -EINVAL; 456 457 if (of_property_read_u32(child, "led-max-microamp", 458 &pd->max_indicator_intensity)) 459 goto err; 460 461 of_node_put(child); 462 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); 467 } 468 469 return 0; 470 err: 471 dev_err(&client->dev, "Required property not found\n"); 472 of_node_put(child); 473 return -EINVAL; 474 } 475 476 477 static int adp1653_probe(struct i2c_client *client, 478 const struct i2c_device_id *devid) 479 { 480 struct adp1653_flash *flash; 481 int ret; 482 483 flash = devm_kzalloc(&client->dev, sizeof(*flash), GFP_KERNEL); 484 if (flash == NULL) 485 return -ENOMEM; 486 487 if (client->dev.of_node) { 488 ret = adp1653_of_init(client, flash, client->dev.of_node); 489 if (ret) 490 return ret; 491 } else { 492 if (!client->dev.platform_data) { 493 dev_err(&client->dev, 494 "Neither DT not platform data provided\n"); 495 return -EINVAL; 496 } 497 flash->platform_data = client->dev.platform_data; 498 } 499 500 mutex_init(&flash->power_lock); 501 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; 505 506 ret = adp1653_init_controls(flash); 507 if (ret) 508 goto free_and_quit; 509 510 ret = media_entity_pads_init(&flash->subdev.entity, 0, NULL); 511 if (ret < 0) 512 goto free_and_quit; 513 514 flash->subdev.entity.function = MEDIA_ENT_F_FLASH; 515 516 return 0; 517 518 free_and_quit: 519 dev_err(&client->dev, "adp1653: failed to register device\n"); 520 v4l2_ctrl_handler_free(&flash->ctrls); 521 return ret; 522 } 523 524 static int adp1653_remove(struct i2c_client *client) 525 { 526 struct v4l2_subdev *subdev = i2c_get_clientdata(client); 527 struct adp1653_flash *flash = to_adp1653_flash(subdev); 528 529 v4l2_device_unregister_subdev(&flash->subdev); 530 v4l2_ctrl_handler_free(&flash->ctrls); 531 media_entity_cleanup(&flash->subdev.entity); 532 533 return 0; 534 } 535 536 static const struct i2c_device_id adp1653_id_table[] = { 537 { ADP1653_NAME, 0 }, 538 { } 539 }; 540 MODULE_DEVICE_TABLE(i2c, adp1653_id_table); 541 542 static const struct dev_pm_ops adp1653_pm_ops = { 543 .suspend = adp1653_suspend, 544 .resume = adp1653_resume, 545 }; 546 547 static struct i2c_driver adp1653_i2c_driver = { 548 .driver = { 549 .name = ADP1653_NAME, 550 .pm = &adp1653_pm_ops, 551 }, 552 .probe = adp1653_probe, 553 .remove = adp1653_remove, 554 .id_table = adp1653_id_table, 555 }; 556 557 module_i2c_driver(adp1653_i2c_driver); 558 559 MODULE_AUTHOR("Sakari Ailus <sakari.ailus@nokia.com>"); 560 MODULE_DESCRIPTION("Analog Devices ADP1653 LED flash driver"); 561 MODULE_LICENSE("GPL"); 562