linux/drivers/staging/media/atomisp/i2c/atomisp-lm3554.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0
   2/*
   3 * LED flash driver for LM3554
   4 *
   5 * Copyright (c) 2010-2012 Intel Corporation. All Rights Reserved.
   6 *
   7 * This program is free software; you can redistribute it and/or
   8 * modify it under the terms of the GNU General Public License version
   9 * 2 as published by the Free Software Foundation.
  10 *
  11 * This program is distributed in the hope that it will be useful,
  12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14 * GNU General Public License for more details.
  15 *
  16 *
  17 */
  18#include <linux/module.h>
  19#include <linux/i2c.h>
  20#include <linux/mutex.h>
  21#include <linux/delay.h>
  22#include <linux/gpio.h>
  23#include <linux/slab.h>
  24
  25#include "../include/media/lm3554.h"
  26#include <media/v4l2-ctrls.h>
  27#include <media/v4l2-device.h>
  28#include <linux/acpi.h>
  29#include <linux/gpio/consumer.h>
  30#include "../include/linux/atomisp_gmin_platform.h"
  31#include "../include/linux/atomisp.h"
  32
  33/* Registers */
  34
  35#define LM3554_TORCH_BRIGHTNESS_REG     0xA0
  36#define LM3554_TORCH_MODE_SHIFT         0
  37#define LM3554_TORCH_CURRENT_SHIFT      3
  38#define LM3554_INDICATOR_CURRENT_SHIFT  6
  39
  40#define LM3554_FLASH_BRIGHTNESS_REG     0xB0
  41#define LM3554_FLASH_MODE_SHIFT         0
  42#define LM3554_FLASH_CURRENT_SHIFT      3
  43#define LM3554_STROBE_SENSITIVITY_SHIFT 7
  44
  45#define LM3554_FLASH_DURATION_REG       0xC0
  46#define LM3554_FLASH_TIMEOUT_SHIFT      0
  47#define LM3554_CURRENT_LIMIT_SHIFT      5
  48
  49#define LM3554_FLAGS_REG                0xD0
  50#define LM3554_FLAG_TIMEOUT             BIT(0)
  51#define LM3554_FLAG_THERMAL_SHUTDOWN    BIT(1)
  52#define LM3554_FLAG_LED_FAULT           BIT(2)
  53#define LM3554_FLAG_TX1_INTERRUPT       BIT(3)
  54#define LM3554_FLAG_TX2_INTERRUPT       BIT(4)
  55#define LM3554_FLAG_LED_THERMAL_FAULT   BIT(5)
  56#define LM3554_FLAG_UNUSED              BIT(6)
  57#define LM3554_FLAG_INPUT_VOLTAGE_LOW   BIT(7)
  58
  59#define LM3554_CONFIG_REG_1             0xE0
  60#define LM3554_ENVM_TX2_SHIFT           5
  61#define LM3554_TX2_POLARITY_SHIFT       6
  62
  63struct lm3554 {
  64        struct v4l2_subdev sd;
  65
  66        struct mutex power_lock;
  67        struct v4l2_ctrl_handler ctrl_handler;
  68        int power_count;
  69
  70        unsigned int mode;
  71        int timeout;
  72        u8 torch_current;
  73        u8 indicator_current;
  74        u8 flash_current;
  75
  76        struct timer_list flash_off_delay;
  77        struct lm3554_platform_data *pdata;
  78};
  79
  80#define to_lm3554(p_sd) container_of(p_sd, struct lm3554, sd)
  81
  82/* Return negative errno else zero on success */
  83static int lm3554_write(struct lm3554 *flash, u8 addr, u8 val)
  84{
  85        struct i2c_client *client = v4l2_get_subdevdata(&flash->sd);
  86        int ret;
  87
  88        ret = i2c_smbus_write_byte_data(client, addr, val);
  89
  90        dev_dbg(&client->dev, "Write Addr:%02X Val:%02X %s\n", addr, val,
  91                ret < 0 ? "fail" : "ok");
  92
  93        return ret;
  94}
  95
  96/* Return negative errno else a data byte received from the device. */
  97static int lm3554_read(struct lm3554 *flash, u8 addr)
  98{
  99        struct i2c_client *client = v4l2_get_subdevdata(&flash->sd);
 100        int ret;
 101
 102        ret = i2c_smbus_read_byte_data(client, addr);
 103
 104        dev_dbg(&client->dev, "Read Addr:%02X Val:%02X %s\n", addr, ret,
 105                ret < 0 ? "fail" : "ok");
 106
 107        return ret;
 108}
 109
 110/* -----------------------------------------------------------------------------
 111 * Hardware configuration
 112 */
 113
 114static int lm3554_set_mode(struct lm3554 *flash, unsigned int mode)
 115{
 116        u8 val;
 117        int ret;
 118
 119        val = (mode << LM3554_FLASH_MODE_SHIFT) |
 120              (flash->flash_current << LM3554_FLASH_CURRENT_SHIFT);
 121
 122        ret = lm3554_write(flash, LM3554_FLASH_BRIGHTNESS_REG, val);
 123        if (ret == 0)
 124                flash->mode = mode;
 125        return ret;
 126}
 127
 128static int lm3554_set_torch(struct lm3554 *flash)
 129{
 130        u8 val;
 131
 132        val = (flash->mode << LM3554_TORCH_MODE_SHIFT) |
 133              (flash->torch_current << LM3554_TORCH_CURRENT_SHIFT) |
 134              (flash->indicator_current << LM3554_INDICATOR_CURRENT_SHIFT);
 135
 136        return lm3554_write(flash, LM3554_TORCH_BRIGHTNESS_REG, val);
 137}
 138
 139static int lm3554_set_flash(struct lm3554 *flash)
 140{
 141        u8 val;
 142
 143        val = (flash->mode << LM3554_FLASH_MODE_SHIFT) |
 144              (flash->flash_current << LM3554_FLASH_CURRENT_SHIFT);
 145
 146        return lm3554_write(flash, LM3554_FLASH_BRIGHTNESS_REG, val);
 147}
 148
 149static int lm3554_set_duration(struct lm3554 *flash)
 150{
 151        u8 val;
 152
 153        val = (flash->timeout << LM3554_FLASH_TIMEOUT_SHIFT) |
 154              (flash->pdata->current_limit << LM3554_CURRENT_LIMIT_SHIFT);
 155
 156        return lm3554_write(flash, LM3554_FLASH_DURATION_REG, val);
 157}
 158
 159static int lm3554_set_config1(struct lm3554 *flash)
 160{
 161        u8 val;
 162
 163        val = (flash->pdata->envm_tx2 << LM3554_ENVM_TX2_SHIFT) |
 164              (flash->pdata->tx2_polarity << LM3554_TX2_POLARITY_SHIFT);
 165        return lm3554_write(flash, LM3554_CONFIG_REG_1, val);
 166}
 167
 168/* -----------------------------------------------------------------------------
 169 * Hardware trigger
 170 */
 171static void lm3554_flash_off_delay(struct timer_list *t)
 172{
 173        struct lm3554 *flash = from_timer(flash, t, flash_off_delay);
 174        struct lm3554_platform_data *pdata = flash->pdata;
 175
 176        gpio_set_value(pdata->gpio_strobe, 0);
 177}
 178
 179static int lm3554_hw_strobe(struct i2c_client *client, bool strobe)
 180{
 181        int ret, timer_pending;
 182        struct v4l2_subdev *sd = i2c_get_clientdata(client);
 183        struct lm3554 *flash = to_lm3554(sd);
 184        struct lm3554_platform_data *pdata = flash->pdata;
 185
 186        /*
 187         * An abnormal high flash current is observed when strobe off the
 188         * flash. Workaround here is firstly set flash current to lower level,
 189         * wait a short moment, and then strobe off the flash.
 190         */
 191
 192        timer_pending = del_timer_sync(&flash->flash_off_delay);
 193
 194        /* Flash off */
 195        if (!strobe) {
 196                /* set current to 70mA and wait a while */
 197                ret = lm3554_write(flash, LM3554_FLASH_BRIGHTNESS_REG, 0);
 198                if (ret < 0)
 199                        goto err;
 200                mod_timer(&flash->flash_off_delay,
 201                          jiffies + msecs_to_jiffies(LM3554_TIMER_DELAY));
 202                return 0;
 203        }
 204
 205        /* Flash on */
 206
 207        /*
 208         * If timer is killed before run, flash is not strobe off,
 209         * so must strobe off here
 210         */
 211        if (timer_pending)
 212                gpio_set_value(pdata->gpio_strobe, 0);
 213
 214        /* Restore flash current settings */
 215        ret = lm3554_set_flash(flash);
 216        if (ret < 0)
 217                goto err;
 218
 219        /* Strobe on Flash */
 220        gpio_set_value(pdata->gpio_strobe, 1);
 221
 222        return 0;
 223err:
 224        dev_err(&client->dev, "failed to %s flash strobe (%d)\n",
 225                strobe ? "on" : "off", ret);
 226        return ret;
 227}
 228
 229/* -----------------------------------------------------------------------------
 230 * V4L2 controls
 231 */
 232
 233static int lm3554_read_status(struct lm3554 *flash)
 234{
 235        int ret;
 236        struct i2c_client *client = v4l2_get_subdevdata(&flash->sd);
 237
 238        /* NOTE: reading register clear fault status */
 239        ret = lm3554_read(flash, LM3554_FLAGS_REG);
 240        if (ret < 0)
 241                return ret;
 242
 243        /*
 244         * Accordingly to datasheet we read back '1' in bit 6.
 245         * Clear it first.
 246         */
 247        ret &= ~LM3554_FLAG_UNUSED;
 248
 249        /*
 250         * Do not take TX1/TX2 signal as an error
 251         * because MSIC will not turn off flash, but turn to
 252         * torch mode according to gsm modem signal by hardware.
 253         */
 254        ret &= ~(LM3554_FLAG_TX1_INTERRUPT | LM3554_FLAG_TX2_INTERRUPT);
 255
 256        if (ret > 0)
 257                dev_dbg(&client->dev, "LM3554 flag status: %02x\n", ret);
 258
 259        return ret;
 260}
 261
 262static int lm3554_s_flash_timeout(struct v4l2_subdev *sd, u32 val)
 263{
 264        struct lm3554 *flash = to_lm3554(sd);
 265
 266        val = clamp(val, LM3554_MIN_TIMEOUT, LM3554_MAX_TIMEOUT);
 267        val = val / LM3554_TIMEOUT_STEPSIZE - 1;
 268
 269        flash->timeout = val;
 270
 271        return lm3554_set_duration(flash);
 272}
 273
 274static int lm3554_g_flash_timeout(struct v4l2_subdev *sd, s32 *val)
 275{
 276        struct lm3554 *flash = to_lm3554(sd);
 277
 278        *val = (u32)(flash->timeout + 1) * LM3554_TIMEOUT_STEPSIZE;
 279
 280        return 0;
 281}
 282
 283static int lm3554_s_flash_intensity(struct v4l2_subdev *sd, u32 intensity)
 284{
 285        struct lm3554 *flash = to_lm3554(sd);
 286
 287        intensity = LM3554_CLAMP_PERCENTAGE(intensity);
 288        intensity = LM3554_PERCENT_TO_VALUE(intensity, LM3554_FLASH_STEP);
 289
 290        flash->flash_current = intensity;
 291
 292        return lm3554_set_flash(flash);
 293}
 294
 295static int lm3554_g_flash_intensity(struct v4l2_subdev *sd, s32 *val)
 296{
 297        struct lm3554 *flash = to_lm3554(sd);
 298
 299        *val = LM3554_VALUE_TO_PERCENT((u32)flash->flash_current,
 300                                       LM3554_FLASH_STEP);
 301
 302        return 0;
 303}
 304
 305static int lm3554_s_torch_intensity(struct v4l2_subdev *sd, u32 intensity)
 306{
 307        struct lm3554 *flash = to_lm3554(sd);
 308
 309        intensity = LM3554_CLAMP_PERCENTAGE(intensity);
 310        intensity = LM3554_PERCENT_TO_VALUE(intensity, LM3554_TORCH_STEP);
 311
 312        flash->torch_current = intensity;
 313
 314        return lm3554_set_torch(flash);
 315}
 316
 317static int lm3554_g_torch_intensity(struct v4l2_subdev *sd, s32 *val)
 318{
 319        struct lm3554 *flash = to_lm3554(sd);
 320
 321        *val = LM3554_VALUE_TO_PERCENT((u32)flash->torch_current,
 322                                       LM3554_TORCH_STEP);
 323
 324        return 0;
 325}
 326
 327static int lm3554_s_indicator_intensity(struct v4l2_subdev *sd, u32 intensity)
 328{
 329        struct lm3554 *flash = to_lm3554(sd);
 330
 331        intensity = LM3554_CLAMP_PERCENTAGE(intensity);
 332        intensity = LM3554_PERCENT_TO_VALUE(intensity, LM3554_INDICATOR_STEP);
 333
 334        flash->indicator_current = intensity;
 335
 336        return lm3554_set_torch(flash);
 337}
 338
 339static int lm3554_g_indicator_intensity(struct v4l2_subdev *sd, s32 *val)
 340{
 341        struct lm3554 *flash = to_lm3554(sd);
 342
 343        *val = LM3554_VALUE_TO_PERCENT((u32)flash->indicator_current,
 344                                       LM3554_INDICATOR_STEP);
 345
 346        return 0;
 347}
 348
 349static int lm3554_s_flash_strobe(struct v4l2_subdev *sd, u32 val)
 350{
 351        struct i2c_client *client = v4l2_get_subdevdata(sd);
 352
 353        return lm3554_hw_strobe(client, val);
 354}
 355
 356static int lm3554_s_flash_mode(struct v4l2_subdev *sd, u32 new_mode)
 357{
 358        struct lm3554 *flash = to_lm3554(sd);
 359        unsigned int mode;
 360
 361        switch (new_mode) {
 362        case ATOMISP_FLASH_MODE_OFF:
 363                mode = LM3554_MODE_SHUTDOWN;
 364                break;
 365        case ATOMISP_FLASH_MODE_FLASH:
 366                mode = LM3554_MODE_FLASH;
 367                break;
 368        case ATOMISP_FLASH_MODE_INDICATOR:
 369                mode = LM3554_MODE_INDICATOR;
 370                break;
 371        case ATOMISP_FLASH_MODE_TORCH:
 372                mode = LM3554_MODE_TORCH;
 373                break;
 374        default:
 375                return -EINVAL;
 376        }
 377
 378        return lm3554_set_mode(flash, mode);
 379}
 380
 381static int lm3554_g_flash_mode(struct v4l2_subdev *sd, s32 *val)
 382{
 383        struct lm3554 *flash = to_lm3554(sd);
 384        *val = flash->mode;
 385        return 0;
 386}
 387
 388static int lm3554_g_flash_status(struct v4l2_subdev *sd, s32 *val)
 389{
 390        struct lm3554 *flash = to_lm3554(sd);
 391        int value;
 392
 393        value = lm3554_read_status(flash);
 394        if (value < 0)
 395                return value;
 396
 397        if (value & LM3554_FLAG_TIMEOUT)
 398                *val = ATOMISP_FLASH_STATUS_TIMEOUT;
 399        else if (value > 0)
 400                *val = ATOMISP_FLASH_STATUS_HW_ERROR;
 401        else
 402                *val = ATOMISP_FLASH_STATUS_OK;
 403
 404        return 0;
 405}
 406
 407static int lm3554_g_flash_status_register(struct v4l2_subdev *sd, s32 *val)
 408{
 409        struct lm3554 *flash = to_lm3554(sd);
 410        int ret;
 411
 412        ret = lm3554_read(flash, LM3554_FLAGS_REG);
 413
 414        if (ret < 0)
 415                return ret;
 416
 417        *val = ret;
 418        return 0;
 419}
 420
 421static int lm3554_s_ctrl(struct v4l2_ctrl *ctrl)
 422{
 423        struct lm3554 *dev =
 424            container_of(ctrl->handler, struct lm3554, ctrl_handler);
 425        int ret = 0;
 426
 427        switch (ctrl->id) {
 428        case V4L2_CID_FLASH_TIMEOUT:
 429                ret = lm3554_s_flash_timeout(&dev->sd, ctrl->val);
 430                break;
 431        case V4L2_CID_FLASH_INTENSITY:
 432                ret = lm3554_s_flash_intensity(&dev->sd, ctrl->val);
 433                break;
 434        case V4L2_CID_FLASH_TORCH_INTENSITY:
 435                ret = lm3554_s_torch_intensity(&dev->sd, ctrl->val);
 436                break;
 437        case V4L2_CID_FLASH_INDICATOR_INTENSITY:
 438                ret = lm3554_s_indicator_intensity(&dev->sd, ctrl->val);
 439                break;
 440        case V4L2_CID_FLASH_STROBE:
 441                ret = lm3554_s_flash_strobe(&dev->sd, ctrl->val);
 442                break;
 443        case V4L2_CID_FLASH_MODE:
 444                ret = lm3554_s_flash_mode(&dev->sd, ctrl->val);
 445                break;
 446        default:
 447                ret = -EINVAL;
 448        }
 449        return ret;
 450}
 451
 452static int lm3554_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
 453{
 454        struct lm3554 *dev =
 455            container_of(ctrl->handler, struct lm3554, ctrl_handler);
 456        int ret = 0;
 457
 458        switch (ctrl->id) {
 459        case V4L2_CID_FLASH_TIMEOUT:
 460                ret = lm3554_g_flash_timeout(&dev->sd, &ctrl->val);
 461                break;
 462        case V4L2_CID_FLASH_INTENSITY:
 463                ret = lm3554_g_flash_intensity(&dev->sd, &ctrl->val);
 464                break;
 465        case V4L2_CID_FLASH_TORCH_INTENSITY:
 466                ret = lm3554_g_torch_intensity(&dev->sd, &ctrl->val);
 467                break;
 468        case V4L2_CID_FLASH_INDICATOR_INTENSITY:
 469                ret = lm3554_g_indicator_intensity(&dev->sd, &ctrl->val);
 470                break;
 471        case V4L2_CID_FLASH_MODE:
 472                ret = lm3554_g_flash_mode(&dev->sd, &ctrl->val);
 473                break;
 474        case V4L2_CID_FLASH_STATUS:
 475                ret = lm3554_g_flash_status(&dev->sd, &ctrl->val);
 476                break;
 477        case V4L2_CID_FLASH_STATUS_REGISTER:
 478                ret = lm3554_g_flash_status_register(&dev->sd, &ctrl->val);
 479                break;
 480        default:
 481                ret = -EINVAL;
 482        }
 483
 484        return ret;
 485}
 486
 487static const struct v4l2_ctrl_ops ctrl_ops = {
 488        .s_ctrl = lm3554_s_ctrl,
 489        .g_volatile_ctrl = lm3554_g_volatile_ctrl
 490};
 491
 492static const struct v4l2_ctrl_config lm3554_controls[] = {
 493        {
 494                .ops = &ctrl_ops,
 495                .id = V4L2_CID_FLASH_TIMEOUT,
 496                .type = V4L2_CTRL_TYPE_INTEGER,
 497                .name = "Flash Timeout",
 498                .min = 0x0,
 499                .max = LM3554_MAX_TIMEOUT,
 500                .step = 0x01,
 501                .def = LM3554_DEFAULT_TIMEOUT,
 502                .flags = 0,
 503        },
 504        {
 505                .ops = &ctrl_ops,
 506                .id = V4L2_CID_FLASH_INTENSITY,
 507                .type = V4L2_CTRL_TYPE_INTEGER,
 508                .name = "Flash Intensity",
 509                .min = LM3554_MIN_PERCENT,
 510                .max = LM3554_MAX_PERCENT,
 511                .step = 0x01,
 512                .def = LM3554_FLASH_DEFAULT_BRIGHTNESS,
 513                .flags = 0,
 514        },
 515        {
 516                .ops = &ctrl_ops,
 517                .id = V4L2_CID_FLASH_TORCH_INTENSITY,
 518                .type = V4L2_CTRL_TYPE_INTEGER,
 519                .name = "Torch Intensity",
 520                .min = LM3554_MIN_PERCENT,
 521                .max = LM3554_MAX_PERCENT,
 522                .step = 0x01,
 523                .def = LM3554_TORCH_DEFAULT_BRIGHTNESS,
 524                .flags = 0,
 525        },
 526        {
 527                .ops = &ctrl_ops,
 528                .id = V4L2_CID_FLASH_INDICATOR_INTENSITY,
 529                .type = V4L2_CTRL_TYPE_INTEGER,
 530                .name = "Indicator Intensity",
 531                .min = LM3554_MIN_PERCENT,
 532                .max = LM3554_MAX_PERCENT,
 533                .step = 0x01,
 534                .def = LM3554_INDICATOR_DEFAULT_BRIGHTNESS,
 535                .flags = 0,
 536        },
 537        {
 538                .ops = &ctrl_ops,
 539                .id = V4L2_CID_FLASH_STROBE,
 540                .type = V4L2_CTRL_TYPE_BOOLEAN,
 541                .name = "Flash Strobe",
 542                .min = 0,
 543                .max = 1,
 544                .step = 1,
 545                .def = 0,
 546                .flags = 0,
 547        },
 548        {
 549                .ops = &ctrl_ops,
 550                .id = V4L2_CID_FLASH_MODE,
 551                .type = V4L2_CTRL_TYPE_INTEGER,
 552                .name = "Flash Mode",
 553                .min = 0,
 554                .max = 100,
 555                .step = 1,
 556                .def = ATOMISP_FLASH_MODE_OFF,
 557                .flags = 0,
 558        },
 559        {
 560                .ops = &ctrl_ops,
 561                .id = V4L2_CID_FLASH_STATUS,
 562                .type = V4L2_CTRL_TYPE_INTEGER,
 563                .name = "Flash Status",
 564                .min = ATOMISP_FLASH_STATUS_OK,
 565                .max = ATOMISP_FLASH_STATUS_TIMEOUT,
 566                .step = 1,
 567                .def = ATOMISP_FLASH_STATUS_OK,
 568                .flags = 0,
 569        },
 570        {
 571                .ops = &ctrl_ops,
 572                .id = V4L2_CID_FLASH_STATUS_REGISTER,
 573                .type = V4L2_CTRL_TYPE_INTEGER,
 574                .name = "Flash Status Register",
 575                .min = 0,
 576                .max = 255,
 577                .step = 1,
 578                .def = 0,
 579                .flags = 0,
 580        },
 581};
 582
 583/* -----------------------------------------------------------------------------
 584 * V4L2 subdev core operations
 585 */
 586
 587/* Put device into known state. */
 588static int lm3554_setup(struct lm3554 *flash)
 589{
 590        struct i2c_client *client = v4l2_get_subdevdata(&flash->sd);
 591        int ret;
 592
 593        /* clear the flags register */
 594        ret = lm3554_read(flash, LM3554_FLAGS_REG);
 595        if (ret < 0)
 596                return ret;
 597
 598        dev_dbg(&client->dev, "Fault info: %02x\n", ret);
 599
 600        ret = lm3554_set_config1(flash);
 601        if (ret < 0)
 602                return ret;
 603
 604        ret = lm3554_set_duration(flash);
 605        if (ret < 0)
 606                return ret;
 607
 608        ret = lm3554_set_torch(flash);
 609        if (ret < 0)
 610                return ret;
 611
 612        ret = lm3554_set_flash(flash);
 613        if (ret < 0)
 614                return ret;
 615
 616        /* read status */
 617        ret = lm3554_read_status(flash);
 618        if (ret < 0)
 619                return ret;
 620
 621        return ret ? -EIO : 0;
 622}
 623
 624static int __lm3554_s_power(struct lm3554 *flash, int power)
 625{
 626        struct lm3554_platform_data *pdata = flash->pdata;
 627        int ret;
 628
 629        /*initialize flash driver*/
 630        gpio_set_value(pdata->gpio_reset, power);
 631        usleep_range(100, 100 + 1);
 632
 633        if (power) {
 634                /* Setup default values. This makes sure that the chip
 635                 * is in a known state.
 636                 */
 637                ret = lm3554_setup(flash);
 638                if (ret < 0) {
 639                        __lm3554_s_power(flash, 0);
 640                        return ret;
 641                }
 642        }
 643
 644        return 0;
 645}
 646
 647static int lm3554_s_power(struct v4l2_subdev *sd, int power)
 648{
 649        struct lm3554 *flash = to_lm3554(sd);
 650        int ret = 0;
 651
 652        mutex_lock(&flash->power_lock);
 653
 654        if (flash->power_count == !power) {
 655                ret = __lm3554_s_power(flash, !!power);
 656                if (ret < 0)
 657                        goto done;
 658        }
 659
 660        flash->power_count += power ? 1 : -1;
 661        WARN_ON(flash->power_count < 0);
 662
 663done:
 664        mutex_unlock(&flash->power_lock);
 665        return ret;
 666}
 667
 668static const struct v4l2_subdev_core_ops lm3554_core_ops = {
 669        .s_power = lm3554_s_power,
 670};
 671
 672static const struct v4l2_subdev_ops lm3554_ops = {
 673        .core = &lm3554_core_ops,
 674};
 675
 676static int lm3554_detect(struct v4l2_subdev *sd)
 677{
 678        struct i2c_client *client = v4l2_get_subdevdata(sd);
 679        struct i2c_adapter *adapter = client->adapter;
 680        struct lm3554 *flash = to_lm3554(sd);
 681        int ret;
 682
 683        if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
 684                dev_err(&client->dev, "lm3554_detect i2c error\n");
 685                return -ENODEV;
 686        }
 687
 688        /* Power up the flash driver and reset it */
 689        ret = lm3554_s_power(&flash->sd, 1);
 690        if (ret < 0) {
 691                dev_err(&client->dev, "Failed to power on lm3554 LED flash\n");
 692        } else {
 693                dev_dbg(&client->dev, "Successfully detected lm3554 LED flash\n");
 694                lm3554_s_power(&flash->sd, 0);
 695        }
 696
 697        return ret;
 698}
 699
 700static int lm3554_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
 701{
 702        return lm3554_s_power(sd, 1);
 703}
 704
 705static int lm3554_close(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
 706{
 707        return lm3554_s_power(sd, 0);
 708}
 709
 710static const struct v4l2_subdev_internal_ops lm3554_internal_ops = {
 711        .registered = lm3554_detect,
 712        .open = lm3554_open,
 713        .close = lm3554_close,
 714};
 715
 716/* -----------------------------------------------------------------------------
 717 *  I2C driver
 718 */
 719#ifdef CONFIG_PM
 720
 721static int lm3554_suspend(struct device *dev)
 722{
 723        struct i2c_client *client = to_i2c_client(dev);
 724        struct v4l2_subdev *subdev = i2c_get_clientdata(client);
 725        struct lm3554 *flash = to_lm3554(subdev);
 726        int rval;
 727
 728        if (flash->power_count == 0)
 729                return 0;
 730
 731        rval = __lm3554_s_power(flash, 0);
 732
 733        dev_dbg(&client->dev, "Suspend %s\n", rval < 0 ? "failed" : "ok");
 734
 735        return rval;
 736}
 737
 738static int lm3554_resume(struct device *dev)
 739{
 740        struct i2c_client *client = to_i2c_client(dev);
 741        struct v4l2_subdev *subdev = i2c_get_clientdata(client);
 742        struct lm3554 *flash = to_lm3554(subdev);
 743        int rval;
 744
 745        if (flash->power_count == 0)
 746                return 0;
 747
 748        rval = __lm3554_s_power(flash, 1);
 749
 750        dev_dbg(&client->dev, "Resume %s\n", rval < 0 ? "fail" : "ok");
 751
 752        return rval;
 753}
 754
 755#else
 756
 757#define lm3554_suspend NULL
 758#define lm3554_resume  NULL
 759
 760#endif /* CONFIG_PM */
 761
 762static int lm3554_gpio_init(struct i2c_client *client)
 763{
 764        struct v4l2_subdev *sd = i2c_get_clientdata(client);
 765        struct lm3554 *flash = to_lm3554(sd);
 766        struct lm3554_platform_data *pdata = flash->pdata;
 767        int ret;
 768
 769        if (!gpio_is_valid(pdata->gpio_reset))
 770                return -EINVAL;
 771
 772        ret = gpio_direction_output(pdata->gpio_reset, 0);
 773        if (ret < 0)
 774                goto err_gpio_reset;
 775        dev_info(&client->dev, "flash led reset successfully\n");
 776
 777        if (!gpio_is_valid(pdata->gpio_strobe)) {
 778                ret = -EINVAL;
 779                goto err_gpio_dir_reset;
 780        }
 781
 782        ret = gpio_direction_output(pdata->gpio_strobe, 0);
 783        if (ret < 0)
 784                goto err_gpio_strobe;
 785
 786        return 0;
 787
 788err_gpio_strobe:
 789        gpio_free(pdata->gpio_strobe);
 790err_gpio_dir_reset:
 791        gpio_direction_output(pdata->gpio_reset, 0);
 792err_gpio_reset:
 793        gpio_free(pdata->gpio_reset);
 794
 795        return ret;
 796}
 797
 798static int lm3554_gpio_uninit(struct i2c_client *client)
 799{
 800        struct v4l2_subdev *sd = i2c_get_clientdata(client);
 801        struct lm3554 *flash = to_lm3554(sd);
 802        struct lm3554_platform_data *pdata = flash->pdata;
 803        int ret;
 804
 805        ret = gpio_direction_output(pdata->gpio_strobe, 0);
 806        if (ret < 0)
 807                return ret;
 808
 809        ret = gpio_direction_output(pdata->gpio_reset, 0);
 810        if (ret < 0)
 811                return ret;
 812
 813        gpio_free(pdata->gpio_strobe);
 814        gpio_free(pdata->gpio_reset);
 815        return 0;
 816}
 817
 818static void *lm3554_platform_data_func(struct i2c_client *client)
 819{
 820        static struct lm3554_platform_data platform_data;
 821
 822        platform_data.gpio_reset =
 823            desc_to_gpio(gpiod_get_index(&client->dev,
 824                                         NULL, 2, GPIOD_OUT_LOW));
 825        platform_data.gpio_strobe =
 826            desc_to_gpio(gpiod_get_index(&client->dev,
 827                                         NULL, 0, GPIOD_OUT_LOW));
 828        platform_data.gpio_torch =
 829            desc_to_gpio(gpiod_get_index(&client->dev,
 830                                         NULL, 1, GPIOD_OUT_LOW));
 831        dev_info(&client->dev, "camera pdata: lm3554: reset: %d strobe %d torch %d\n",
 832                 platform_data.gpio_reset, platform_data.gpio_strobe,
 833                 platform_data.gpio_torch);
 834
 835        /* Set to TX2 mode, then ENVM/TX2 pin is a power amplifier sync input:
 836         * ENVM/TX pin asserted, flash forced into torch;
 837         * ENVM/TX pin desserted, flash set back;
 838         */
 839        platform_data.envm_tx2 = 1;
 840        platform_data.tx2_polarity = 0;
 841
 842        /* set peak current limit to be 1000mA */
 843        platform_data.current_limit = 0;
 844
 845        return &platform_data;
 846}
 847
 848static int lm3554_probe(struct i2c_client *client)
 849{
 850        int err = 0;
 851        struct lm3554 *flash;
 852        unsigned int i;
 853        int ret;
 854
 855        flash = kzalloc(sizeof(*flash), GFP_KERNEL);
 856        if (!flash)
 857                return -ENOMEM;
 858
 859        flash->pdata = lm3554_platform_data_func(client);
 860
 861        v4l2_i2c_subdev_init(&flash->sd, client, &lm3554_ops);
 862        flash->sd.internal_ops = &lm3554_internal_ops;
 863        flash->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
 864        flash->mode = ATOMISP_FLASH_MODE_OFF;
 865        flash->timeout = LM3554_MAX_TIMEOUT / LM3554_TIMEOUT_STEPSIZE - 1;
 866        ret =
 867            v4l2_ctrl_handler_init(&flash->ctrl_handler,
 868                                   ARRAY_SIZE(lm3554_controls));
 869        if (ret) {
 870                dev_err(&client->dev, "error initialize a ctrl_handler.\n");
 871                goto fail2;
 872        }
 873
 874        for (i = 0; i < ARRAY_SIZE(lm3554_controls); i++)
 875                v4l2_ctrl_new_custom(&flash->ctrl_handler, &lm3554_controls[i],
 876                                     NULL);
 877
 878        if (flash->ctrl_handler.error) {
 879                dev_err(&client->dev, "ctrl_handler error.\n");
 880                goto fail2;
 881        }
 882
 883        flash->sd.ctrl_handler = &flash->ctrl_handler;
 884        err = media_entity_pads_init(&flash->sd.entity, 0, NULL);
 885        if (err) {
 886                dev_err(&client->dev, "error initialize a media entity.\n");
 887                goto fail1;
 888        }
 889
 890        flash->sd.entity.function = MEDIA_ENT_F_FLASH;
 891
 892        mutex_init(&flash->power_lock);
 893
 894        timer_setup(&flash->flash_off_delay, lm3554_flash_off_delay, 0);
 895
 896        err = lm3554_gpio_init(client);
 897        if (err) {
 898                dev_err(&client->dev, "gpio request/direction_output fail");
 899                goto fail2;
 900        }
 901        return atomisp_register_i2c_module(&flash->sd, NULL, LED_FLASH);
 902fail2:
 903        media_entity_cleanup(&flash->sd.entity);
 904        v4l2_ctrl_handler_free(&flash->ctrl_handler);
 905fail1:
 906        v4l2_device_unregister_subdev(&flash->sd);
 907        kfree(flash);
 908
 909        return err;
 910}
 911
 912static int lm3554_remove(struct i2c_client *client)
 913{
 914        struct v4l2_subdev *sd = i2c_get_clientdata(client);
 915        struct lm3554 *flash = to_lm3554(sd);
 916        int ret;
 917
 918        media_entity_cleanup(&flash->sd.entity);
 919        v4l2_ctrl_handler_free(&flash->ctrl_handler);
 920        v4l2_device_unregister_subdev(sd);
 921
 922        atomisp_gmin_remove_subdev(sd);
 923
 924        del_timer_sync(&flash->flash_off_delay);
 925
 926        ret = lm3554_gpio_uninit(client);
 927        if (ret < 0)
 928                goto fail;
 929
 930        kfree(flash);
 931
 932        return 0;
 933fail:
 934        dev_err(&client->dev, "gpio request/direction_output fail");
 935        return ret;
 936}
 937
 938static const struct dev_pm_ops lm3554_pm_ops = {
 939        .suspend = lm3554_suspend,
 940        .resume = lm3554_resume,
 941};
 942
 943static const struct acpi_device_id lm3554_acpi_match[] = {
 944        { "INTCF1C" },
 945        {},
 946};
 947MODULE_DEVICE_TABLE(acpi, lm3554_acpi_match);
 948
 949static struct i2c_driver lm3554_driver = {
 950        .driver = {
 951                .name = "lm3554",
 952                .pm   = &lm3554_pm_ops,
 953                .acpi_match_table = lm3554_acpi_match,
 954        },
 955        .probe_new = lm3554_probe,
 956        .remove = lm3554_remove,
 957};
 958module_i2c_driver(lm3554_driver);
 959
 960MODULE_AUTHOR("Jing Tao <jing.tao@intel.com>");
 961MODULE_DESCRIPTION("LED flash driver for LM3554");
 962MODULE_LICENSE("GPL");
 963