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