/*
* imx219.c - imx219 sensor driver
*
* Copyright (c) 2015-2016, NVIDIA CORPORATION. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <dt-bindings/gpio/tegra-gpio.h>
#include <linux/slab.h>
#include <linux/uaccess.h>
#include <linux/gpio.h>
#include <linux/module.h>
#include <linux/seq_file.h>
#include <linux/of.h>
#include <linux/of_device.h>
#include <linux/of_gpio.h>
#include <media/camera_common.h>
#include <media/soc_camera.h>
#include <media/imx219.h>
#include "imx219_mode_tbls.h"
#define IMX219_MAX_COARSE_DIFF 4
#define IMX219_GAIN_SHIFT (8)
#define IMX219_MIN_GAIN (1 << IMX219_GAIN_SHIFT)
#define IMX219_MAX_GAIN (16 << IMX219_GAIN_SHIFT)
#define IMX219_MIN_FRAME_LENGTH (0x0)
#define IMX219_MAX_FRAME_LENGTH (0x7FFF)
#define IMX219_MIN_EXPOSURE_COARSE (0x0001)
#define IMX219_MAX_EXPOSURE_COARSE \
(IMX219_MAX_FRAME_LENGTH-IMX219_MAX_COARSE_DIFF)
#define IMX219_DEFAULT_GAIN (IMX219_MIN_GAIN)
#define IMX219_DEFAULT_FRAME_LENGTH (1766)
#define IMX219_DEFAULT_EXPOSURE_COARSE \
(IMX219_DEFAULT_FRAME_LENGTH-IMX219_MAX_COARSE_DIFF)
#define IMX219_DEFAULT_MODE IMX219_MODE_1920X1080
#define IMX219_DEFAULT_WIDTH 1920
#define IMX219_DEFAULT_HEIGHT 1080
#define IMX219_DEFAULT_DATAFMT MEDIA_BUS_FMT_SRGGB10_1X10
#define IMX219_DEFAULT_CLK_FREQ 24000000
struct imx219 {
struct camera_common_power_rail power;
int num_ctrls;
struct v4l2_ctrl_handler ctrl_handler;
struct i2c_client *i2c_client;
struct v4l2_subdev *subdev;
struct media_pad pad;
struct regmap *regmap;
struct camera_common_data *s_data;
struct camera_common_pdata *pdata;
struct v4l2_ctrl *ctrls[];
};
static const struct regmap_config sensor_regmap_config = {
.reg_bits = 16,
.val_bits = 8,
.cache_type = REGCACHE_RBTREE,
};
static int imx219_g_volatile_ctrl(struct v4l2_ctrl *ctrl);
static int imx219_s_ctrl(struct v4l2_ctrl *ctrl);
static const struct v4l2_ctrl_ops imx219_ctrl_ops = {
.g_volatile_ctrl = imx219_g_volatile_ctrl,
.s_ctrl = imx219_s_ctrl,
};
static struct v4l2_ctrl_config ctrl_config_list[] = {
/* Do not change the name field for the controls! */
{
.ops = &imx219_ctrl_ops,
.id = V4L2_CID_GAIN,
.name = "Gain",
.type = V4L2_CTRL_TYPE_INTEGER,
.flags = V4L2_CTRL_FLAG_SLIDER,
.min = IMX219_MIN_GAIN,
.max = IMX219_MAX_GAIN,
.def = IMX219_DEFAULT_GAIN,
.step = 1,
},
{
.ops = &imx219_ctrl_ops,
.id = V4L2_CID_FRAME_LENGTH,
.name = "Frame Length",
.type = V4L2_CTRL_TYPE_INTEGER,
.flags = V4L2_CTRL_FLAG_SLIDER,
.min = IMX219_MIN_FRAME_LENGTH,
.max = IMX219_MAX_FRAME_LENGTH,
.def = IMX219_DEFAULT_FRAME_LENGTH,
.step = 1,
},
{
.ops = &imx219_ctrl_ops,
.id = V4L2_CID_COARSE_TIME,
.name = "Coarse Time",
.type = V4L2_CTRL_TYPE_INTEGER,
.flags = V4L2_CTRL_FLAG_SLIDER,
.min = IMX219_MIN_EXPOSURE_COARSE,
.max = IMX219_MAX_EXPOSURE_COARSE,
.def = IMX219_DEFAULT_EXPOSURE_COARSE,
.step = 1,
},
{
.ops = &imx219_ctrl_ops,
.id = V4L2_CID_GROUP_HOLD,
.name = "Group Hold",
.type = V4L2_CTRL_TYPE_INTEGER_MENU,
.min = 0,
.max = ARRAY_SIZE(switch_ctrl_qmenu) - 1,
.menu_skip_mask = 0,
.def = 0,
.qmenu_int = switch_ctrl_qmenu,
},
{
.ops = &imx219_ctrl_ops,
.id = V4L2_CID_HDR_EN,
.name = "HDR enable",
.type = V4L2_CTRL_TYPE_INTEGER_MENU,
.min = 0,
.max = ARRAY_SIZE(switch_ctrl_qmenu) - 1,
.menu_skip_mask = 0,
.def = 0,
.qmenu_int = switch_ctrl_qmenu,
},
{
.ops = &imx219_ctrl_ops,
.id = V4L2_CID_FUSE_ID,
.name = "Fuse ID",
.type = V4L2_CTRL_TYPE_STRING,
.flags = V4L2_CTRL_FLAG_READ_ONLY,
.min = 0,
.max = IMX219_FUSE_ID_STR_SIZE,
.step = 2,
},
};
static inline void imx219_get_frame_length_regs(struct reg_8 *regs, u16 frame_length)
{
regs->addr = IMX219_FRAME_LENGTH_ADDR_MSB;
regs->val = (frame_length >> 8) & 0xff;
(regs + 1)->addr = IMX219_FRAME_LENGTH_ADDR_LSB;
(regs + 1)->val = (frame_length) & 0xff;
}
static inline void imx219_get_coarse_time_regs(struct reg_8 *regs, u16 coarse_time)
{
regs->addr = IMX219_COARSE_TIME_ADDR_MSB;
regs->val = (coarse_time >> 8) & 0xff;
(regs + 1)->addr = IMX219_COARSE_TIME_ADDR_LSB;
(regs + 1)->val = (coarse_time) & 0xff;
}
static inline void imx219_get_gain_reg(struct reg_8 *regs, u8 gain)
{
regs->addr = IMX219_GAIN_ADDR;
regs->val = gain & 0xff;
}
static int test_mode;
module_param(test_mode, int, 0644);
static inline int imx219_read_reg(struct camera_common_data *s_data, u16 addr, u8 *val)
{
struct imx219 *priv = (struct imx219 *)s_data->priv;
return regmap_read(priv->regmap, addr, (unsigned int *) val);
}
static int imx219_write_reg(struct camera_common_data *s_data, u16 addr, u8 val)
{
int err;
struct imx219 *priv = (struct imx219 *)s_data->priv;
err = regmap_write(priv->regmap, addr, val);
if (err)
pr_err("%s:i2c write failed, %x = %x\n",
__func__, addr, val);
return err;
}
static int imx219_write_table(struct imx219 *priv, const struct reg_8 table[])
{
return regmap_util_write_table_8(priv->regmap,
table,
NULL, 0,
IMX219_TABLE_WAIT_MS,
IMX219_TABLE_END);
}
static void imx219_mclk_disable(struct camera_common_power_rail *pw)
{
clk_disable_unprepare(pw->mclk);
}
static void imx219_mclk_enable(struct camera_common_power_rail *pw)
{
clk_set_rate(pw->mclk, IMX219_DEFAULT_CLK_FREQ);
clk_prepare_enable(pw->mclk);
}
static int imx219_power_on(struct camera_common_data *s_data)
{
//int err = 0;
struct imx219 *priv = (struct imx219 *)s_data->priv;
struct camera_common_power_rail *pw = &priv->power;
dev_info(&priv->i2c_client->dev, "%s: power on\n", __func__);
if (gpio_cansleep(pw->reset_gpio))
gpio_set_value_cansleep(pw->reset_gpio, 0);
else
gpio_set_value(pw->reset_gpio, 0);
usleep_range(10, 20);
/*
if (pw->avdd)
err = regulator_enable(pw->avdd);
if (err)
goto imx219_avdd_fail;
if (pw->iovdd)
err = regulator_enable(pw->iovdd);
if (err)
goto imx219_iovdd_fail;
if (pw->dvdd)
err = regulator_enable(pw->dvdd);
if (err)
goto imx219_dvdd_fail;
*/
//usleep_range(1, 2);
usleep_range(10, 20);
if (gpio_cansleep(pw->reset_gpio))
gpio_set_value_cansleep(pw->reset_gpio, 1);
else
gpio_set_value(pw->reset_gpio, 1);
//usleep_range(1, 2);
usleep_range(10, 20);
imx219_mclk_enable(pw);
/* Need to wait for t4 + t5 + t9 time as per the data sheet */
/* t4 - 200us, t5 - 6ms, t9 - 1.2ms */
usleep_range(7400, 7410);
pw->state = SWITCH_ON;
return 0;
/*
imx219_dvdd_fail:
regulator_disable(pw->iovdd);
imx219_iovdd_fail:
regulator_disable(pw->avdd);
imx219_avdd_fail:
return -ENODEV;
*/
}
static int imx219_power_off(struct camera_common_data *s_data)
{
struct imx219 *priv = (struct imx219 *)s_data->priv;
struct camera_common_power_rail *pw = &priv->power;
dev_info(&priv->i2c_client->dev, "%s: power off\n", __func__);
usleep_range(1, 2);
if (gpio_cansleep(pw->reset_gpio))
gpio_set_value_cansleep(pw->reset_gpio, 0);
else
gpio_set_value(pw->reset_gpio, 0);
//usleep_range(1, 2);
usleep_range(10, 20);
/*
if (pw->dvdd)
regulator_disable(pw->dvdd);
if (pw->iovdd)
regulator_disable(pw->iovdd);