/*
* LED Flash class driver for the flash cell of max77693 mfd.
*
* Copyright (C) 2015, Samsung Electronics Co., Ltd.
*
* Authors: Jacek Anaszewski <j.anaszewski@samsung.com>
* Andrzej Hajda <a.hajda@samsung.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* version 2 as published by the Free Software Foundation.
*/
#include <linux/led-class-flash.h>
#include <linux/mfd/max77693.h>
#include <linux/mfd/max77693-common.h>
#include <linux/mfd/max77693-private.h>
#include <linux/module.h>
#include <linux/mutex.h>
#include <linux/platform_device.h>
#include <linux/regmap.h>
#include <linux/slab.h>
#include <media/v4l2-flash-led-class.h>
#define MODE_OFF 0
#define MODE_FLASH(a) (1 << (a))
#define MODE_TORCH(a) (1 << (2 + (a)))
#define MODE_FLASH_EXTERNAL(a) (1 << (4 + (a)))
#define MODE_FLASH_MASK (MODE_FLASH(FLED1) | MODE_FLASH(FLED2) | \
MODE_FLASH_EXTERNAL(FLED1) | \
MODE_FLASH_EXTERNAL(FLED2))
#define MODE_TORCH_MASK (MODE_TORCH(FLED1) | MODE_TORCH(FLED2))
#define FLED1_IOUT (1 << 0)
#define FLED2_IOUT (1 << 1)
enum max77693_fled {
FLED1,
FLED2,
};
enum max77693_led_mode {
FLASH,
TORCH,
};
struct max77693_led_config_data {
const char *label[2];
u32 iout_torch_max[2];
u32 iout_flash_max[2];
u32 flash_timeout_max[2];
u32 num_leds;
u32 boost_mode;
u32 boost_vout;
u32 low_vsys;
};
struct max77693_sub_led {
/* corresponding FLED output identifier */
int fled_id;
/* corresponding LED Flash class device */
struct led_classdev_flash fled_cdev;
/* V4L2 Flash device */
struct v4l2_flash *v4l2_flash;
/* brightness cache */
unsigned int torch_brightness;
/* flash timeout cache */
unsigned int flash_timeout;
/* flash faults that may have occurred */
u32 flash_faults;
};
struct max77693_led_device {
/* parent mfd regmap */
struct regmap *regmap;
/* platform device data */
struct platform_device *pdev;
/* secures access to the device */
struct mutex lock;
/* sub led data */
struct max77693_sub_led sub_leds[2];
/* maximum torch current values for FLED outputs */
u32 iout_torch_max[2];
/* maximum flash current values for FLED outputs */
u32 iout_flash_max[2];
/* current flash timeout cache */
unsigned int current_flash_timeout;
/* ITORCH register cache */
u8 torch_iout_reg;
/* mode of fled outputs */
unsigned int mode_flags;
/* recently strobed fled */
int strobing_sub_led_id;
/* bitmask of FLED outputs use state (bit 0. - FLED1, bit 1. - FLED2) */
u8 fled_mask;
/* FLED modes that can be set */
u8 allowed_modes;
/* arrangement of current outputs */
bool iout_joint;
};
static u8 max77693_led_iout_to_reg(u32 ua)
{
if (ua < FLASH_IOUT_MIN)
ua = FLASH_IOUT_MIN;
return (ua - FLASH_IOUT_MIN) / FLASH_IOUT_STEP;
}
static u8 max77693_flash_timeout_to_reg(u32 us)
{
return (us - FLASH_TIMEOUT_MIN) / FLASH_TIMEOUT_STEP;
}
static inline struct max77693_sub_led *flcdev_to_sub_led(
struct led_classdev_flash *fled_cdev)
{
return container_of(fled_cdev, struct max77693_sub_led, fled_cdev);
}
static inline struct max77693_led_device *sub_led_to_led(
struct max77693_sub_led *sub_led)
{
return container_of(sub_led, struct max77693_led_device,
sub_leds[sub_led->fled_id]);
}
static inline u8 max77693_led_vsys_to_reg(u32 mv)
{
return ((mv - MAX_FLASH1_VSYS_MIN) / MAX_FLASH1_VSYS_STEP) << 2;
}
static inline u8 max77693_led_vout_to_reg(u32 mv)
{
return (mv - FLASH_VOUT_MIN) / FLASH_VOUT_STEP + FLASH_VOUT_RMIN;
}
static inline bool max77693_fled_used(struct max77693_led_device *led,
int fled_id)
{
u8 fled_bit = (fled_id == FLED1) ? FLED1_IOUT : FLED2_IOUT;
return led->fled_mask & fled_bit;
}
static int max77693_set_mode_reg(struct max77693_led_device *led, u8 mode)
{
struct regmap *rmap = led->regmap;
int ret, v = 0, i;
for (i = FLED1; i <= FLED2; ++i) {
if (mode & MODE_TORCH(i))
v |= FLASH_EN_ON << TORCH_EN_SHIFT(i);
if (mode & MODE_FLASH(i)) {
v |= FLASH_EN_ON << FLASH_EN_SHIFT(i);
} else if (mode & MODE_FLASH_EXTERNAL(i)) {
v |= FLASH_EN_FLASH << FLASH_EN_SHIFT(i);
/*
* Enable hw triggering also for torch mode, as some
* camera sensors use torch led to fathom ambient light
* conditions before strobing the flash.
*/
v |= FLASH_EN_TORCH << TORCH_EN_SHIFT(i);
}
}
/* Reset the register only prior setting flash modes */
if (mode & ~(MODE_TORCH(FLED1) | MODE_TORCH(FLED2))) {
ret = regmap_write(rmap, MAX77693_LED_REG_FLASH_EN, 0);
if (ret < 0)
return ret;
}
return regmap_write(rmap, MAX77693_LED_REG_FLASH_EN, v);
}
static int max77693_add_mode(struct max77693_led_device *led, u8 mode)
{
u8 new_mode_flags;
int i, ret;
if (led->iout_joint)
/* Span the mode on FLED2 for joint iouts case */
mode |= (mode << 1);
/*
* FLASH_EXTERNAL mode activates FLASHEN and TORCHEN pins in the device.
* Corresponding register bit fields interfere with SW triggered modes,
* thus clear them to ensure proper device configuration.
*/
for (i = FLED1; i <= FLED2; ++i)
if (mode & MODE_FLASH_EXTERNAL(i))
led->mode_flags &= (~MODE_TORCH(i) & ~MODE_FLASH(i));
new_mode_flags = mode | led->mode_flags;
new_mode_flags &= led->allowed_modes;
if (new_mode_flags<