/*
* ox08b40.c - OX08B40 sensor driver
*
* Copyright (c) 2022, Zipline International. All rights reserved.
* Based on Copyright (c) 2020, NVIDIA CORPORATION. All rights reserved.
* (Mostly based on the imx334 driver.)
*
* 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 .
*/
#include
#include
#include
#include
#include "ox08b40_mode_tbls.h"
#define OX08B40_DCG_EXP_MIN (2) // double row time, so this means 4 rows
#define OX08B40_SPD_EXP_MIN (2)
#define OX08B40_VS_EXP_MIN (0) // 0.5 double row time, or 1 row
#define OX08B40_CHIP_ID (0x580841)
#define OX08B40_GROUP_HOLD_START (0x00)
#define OX08B40_GROUP_HOLD_END (0x10)
#define OX08B40_GROUP_HOLD_LAUNCH_VBLANK (0xA0)
#define OX08B40_GROUP_HOLD_BANK_0 (0x00)
static const struct of_device_id ox08b40_of_match[] = {
{
.compatible = "nvidia,ox08b40",
},
{},
};
MODULE_DEVICE_TABLE(of, ox08b40_of_match);
static const u32 ctrl_cid_list[] = {
TEGRA_CAMERA_CID_GAIN,
TEGRA_CAMERA_CID_FRAME_RATE,
TEGRA_CAMERA_CID_EXPOSURE,
TEGRA_CAMERA_CID_SENSOR_MODE_ID,
};
struct ox08b40
{
int cam_sid_gpio;
struct i2c_client *i2c_client;
struct v4l2_subdev *subdev;
s64 frame_length; /* "vmax," units: "lines" */
s64 exposure_us; /* microseconds */
struct camera_common_data *s_data;
struct tegracam_device *tc_dev;
s32 group_hold_prev;
bool group_hold_en;
};
static const struct regmap_config sensor_regmap_config = {
.reg_bits = 16,
.val_bits = 8,
.cache_type = REGCACHE_RBTREE, // ??
};
static int ox08b40_write_reg(struct camera_common_data *s_data, u16 addr, u8 val)
{
int err;
struct device *dev = s_data->dev;
err = regmap_write(s_data->regmap, addr, val);
if (err)
{
dev_err(dev, "%s: i2c write failed, 0x%x = %x\n", __func__, addr, val);
}
return err;
}
static inline int ox08b40_read_reg(struct camera_common_data *s_data, u16 addr, u8 *val)
{
int err = 0;
u32 reg_val = 0;
err = regmap_read(s_data->regmap, addr, ®_val);
*val = reg_val & 0xFF;
return err;
}
static int ox08b40_write_table(struct ox08b40 *priv, const ox08b40_reg table[])
{
return regmap_util_write_table_8(priv->s_data->regmap, table, NULL, 0, OX08B40_TABLE_WAIT_MS, OX08B40_TABLE_END);
}
static int ox08b40_set_group_hold(struct tegracam_device *tc_dev, bool val)
{
struct ox08b40 *priv = tc_dev->priv;
int gh_prev = switch_ctrl_qmenu[priv->group_hold_prev];
int err;
if (val && gh_prev == SWITCH_OFF)
{
/* group hold start */
// dev_dbg(&priv->i2c_client->dev, "%s: entering\n", __func__);
err = ox08b40_write_reg(
priv->s_data, OX08B40_GRP_HOLD_CTRL_ADDR, (OX08B40_GROUP_HOLD_START | OX08B40_GROUP_HOLD_BANK_0));
if (err) goto fail;
priv->group_hold_prev = 1;
}
else if (!val && gh_prev == SWITCH_ON)
{
/* group hold end */
// dev_dbg(&priv->i2c_client->dev, "%s: exiting\n", __func__);
err = ox08b40_write_reg(
priv->s_data, OX08B40_GRP_HOLD_CTRL_ADDR, (OX08B40_GROUP_HOLD_END | OX08B40_GROUP_HOLD_BANK_0));
/* quick launch */
err |= ox08b40_write_reg(
priv->s_data, OX08B40_GRP_HOLD_CTRL_ADDR, (OX08B40_GROUP_HOLD_LAUNCH_VBLANK | OX08B40_GROUP_HOLD_BANK_0));
if (err) goto fail;
priv->group_hold_prev = 0;
}
return 0;
fail:
dev_dbg(&priv->i2c_client->dev, "%s: Group hold control error\n", __func__);
return err;
}
static int ox08b40_set_frame_rate(struct tegracam_device *tc_dev, s64 val)
{
struct device *dev = tc_dev->dev;
dev_dbg(dev, "%s: refuse frame rate change\n", __func__);
return 0;
}
static int ox08b40_set_exposure(struct tegracam_device *tc_dev, s64 val)
{
struct ox08b40 *priv = (struct ox08b40 *)tc_dev->priv;
struct device *dev = tc_dev->dev;
struct camera_common_data *s_data = tc_dev->s_data;
const struct sensor_mode_properties *mode = &s_data->sensor_props.sensor_modes[s_data->mode_prop_idx];
int err = 0;
s64 exposure_lines;
ox08b40_reg regs[4];
if (val < mode->control_properties.min_exp_time.val)
{
dev_warn(
dev, "%s: clamping requested exposure of %lld to %lli\n", __func__, val,
mode->control_properties.min_exp_time.val);
val = mode->control_properties.min_exp_time.val;
}
else if (val > mode->control_properties.max_exp_time.val)
{
dev_warn(
dev, "%s: clamping requested exposure of %lld to %lli\n", __func__, val,
mode->control_properties.max_exp_time.val);
val = mode->control_properties.max_exp_time.val;
}
dev_dbg(dev, "%s: requested exposure time: %lld us\n", __func__, val);
/*
* We must set the DCG and SPD exposures to be identical to avoid ghosting issues.
*
* The resolution of our control is 2372 (total HTS) / 81 MHz (SCLK double-row).
* We want to divide by that, so we multiply by its inverse.
* >>> fractions.Fraction(81000, 2372000)
* Fraction(23, 799)
*/
exposure_lines = (val * 23) / 799;
dev_dbg(dev, "%s: exposure time (double rows): %lld\n", __func__, exposure_lines);
/* The exposure registers listed in DS section 3.5 table 3-6 are confusingly named. */
/* AEC_HCG_CTRL_01/02 are described as "DCG exposure", but DCG contains HCG and LCG. */
/* There are no LCG exposure registers, so we assume HCG is used for both. */
regs[0].addr = OX08B40_AEC_HCG_EXPOSURE_ADDR_MSB;
regs[0].val = (exposure_lines >> 8) & 0xFF;
regs[1].addr = OX08B40_AEC_HCG_EXPOSURE_ADDR_LSB;
regs[1].val = (exposure_lines >> 0) & 0xFF;
regs[2].addr = OX08B40_AEC_SPD_EXPOSURE_ADDR_MSB;
regs[2].val = (exposure_lines >> 8) & 0xFF;
regs[3].addr = OX08B40_AEC_SPD_EXPOSURE_ADDR_LSB;
regs[3].val = (exposure_lines >> 0) & 0xFF;
err = ox08b40_write_table(priv, regs);
return err;
}
static int ox08b40_set_gain(struct tegracam_device *tc_dev, s64 val)
{
struct ox08b40 *priv = (struct ox08b40 *)tc_dev->priv;
struct device *dev = tc_dev->dev;
struct camera_common_data *s_data = tc_dev->s_data;
const struct sensor_mode_properties *mode = &s_data->sensor_props.sensor_modes[s_data->mode_prop_idx];
int err = 0;
u8 spd_gain_int;
u8 spd_gain_fraction;
s64 lcg_gain;
u8 lcg_gain_int;
u8 lcg_gain_fraction;
s64 hcg_gain;
u8 hcg_gain_int;
u8 hcg_gain_fraction;
ox08b40_reg regs[6];
if (val < mode->control_properties.min_gain_val)
{
dev_warn(
dev, "%s: clamping requested gain of %lld to %i\n", __func__, val, mode->control_properties.min_gain_val);
val = mode->control_properties.min_gain_val;
}
else if (val > mode->control_properties.max_gain_val)
{
dev_warn(
dev, "%s: clamping requested gain of %lld to %i\n", __func__, val, mode->control_properties.max_gain_val);
val = mode->control_properties.max_gain_val;
}
/* DS section 3.5
*/
spd_gain_int = val >> 4;
spd_gain_fraction = val & 0x0F;
lcg_gain = val * 3;
if (lcg_gain > 248)
{
lcg_gain = 248;
}
lcg_gain_int = lcg_gain >> 4;
lcg_gain_fraction = lcg_gain & 0x0F;
hcg_gain = 4 * lcg_gain;
if (hcg_gain > 248)
{
hcg_gain = 248;
}
hcg_gain_int = hcg_gain >> 4;
hcg_gain_fraction = hcg_gain & 0x0F;
dev_dbg(
dev, "%s: gain requested: hcg {%u, %u}, lcg {%u, %u}, spd {%u, %u}\n", __func__, hcg_gain_int,
hcg_gain_fraction, lcg_gain_int, lcg_gain_fraction, spd_gain_int, spd_gain_fraction);
/* The fractional bits end up in the MSBs of the next register. */
hcg_gain_fraction <<= 4;
lcg_gain_fraction <<= 4;
spd_gain_fraction <<= 4;
// HCG
regs[0].addr = OX08B40_AEC_HCG_REAL_GAIN_ADDR_MSB;
regs[0].val = hcg_gain_int;
regs[1].addr = OX08B40_AEC_HCG_REAL_GAIN_ADDR_LSB;
regs[1].val = hcg_gain_fraction;
// SPD
regs[2].addr = OX08B40_AEC_SPD_REAL_GAIN_ADDR_MSB;
regs[2].val = spd_gain_int;
regs[3].addr = OX08B40_AEC_SPD_REAL_GAIN_ADDR_LSB;
regs[3].val = spd_gain_fraction;
// LCG
regs[4].addr = OX08B40_AEC_LCG_REAL_GAIN_ADDR_MSB;
regs[4].val = lcg_gain_int;
regs[5].addr = OX08B40_AEC_LCG_REAL_GAIN_ADDR_LSB;
regs[5].val = lcg_gain_fraction;
err = ox08b40_write_table(priv, regs);
if (err)
{
dev_err(dev, "%s: GAIN control error\n", __func__);
}
return err;
}
static int ox08b40_software_reset(struct ox08b40 *priv, u8 i2c_addr)
{
struct device *dev = &priv->i2c_client->dev;
struct i2c_msg msg;
unsigned char data[3];
data[0] = (OX08B40_SOFTWARE_RESET_ADDR >> 8) & 0xff;
data[1] = OX08B40_SOFTWARE_RESET_ADDR & 0xff;
data[2] = 1;
msg.addr = i2c_addr;
msg.flags = 0;
msg.len = 3;
msg.buf = data;
dev_dbg(dev, "%s: Sending first software reset command\n", __func__);
if (i2c_transfer(priv->i2c_client->adapter, &msg, 1) != 1)
{
dev_err(dev, "%s: Error sending first software reset signal to 0x%02x\n", __func__, i2c_addr);
return -EIO;
}
data[0] = (OX08B40_SOFTWARE_RESET_2_ADDR >> 8) & 0xff;
data[1] = OX08B40_SOFTWARE_RESET_2_ADDR & 0xff;
data[2] = 1;
msg.addr = i2c_addr;
msg.flags = 0;
msg.len = 3;
msg.buf = data;
dev_dbg(dev, "%s: Sending second software reset command\n", __func__);
if (i2c_transfer(priv->i2c_client->adapter, &msg, 1) != 1)
{
dev_err(dev, "Error sending second software reset signal to 0x%02x\n", i2c_addr);
return -EIO;
}
msleep_range(10);
return 0;
}
/* Adapted from ov10823.c */
static int ox08b40_i2c_addr_assign(struct ox08b40 *priv, u8 i2c_addr)
{
struct device *dev = &priv->i2c_client->dev;
struct i2c_msg msg;
unsigned char data[3];
int err = 0;
/*
* DS section 1.4 figure 1-2
* SCCB/I2C ID of sensor is controlled by SID pin at power up
* and after RESETB is driven low.
*
* If SID is low/0, ID = 0x6c
* If SID is high/1, ID = 0x20
*
* Address 0x6c is programmable via register 0x3035, and
* address 0x20 is programmable via register 0x3037.
*
* So, the scheme to assign addresses to an (almost) arbitrary
* number of sensors is to consider 0x6c to be the "off" address.
* Start each sensor with SID as 0 so that they appear to be off.
*
* Then, to assign an address to one sensor:
*
* 0. Set corresponding SID to 1 (now only that sensor responds
* to 0x20).
* 1. Use 0x20 to program address 0x6c to the new address.
* 2. Set corresponding SID back to 0 (so it no longer responds
* to 0x20, but instead responds to the new address).
*/
// Rather than deal with cameras using default addresses that could cause issues with initialization
// of other cameras, we decide not to support them. Unique addresses must be used.
if (i2c_addr == OX08B40_DEFAULT_I2C_ADDRESS_6C || i2c_addr == OX08B40_DEFAULT_I2C_ADDRESS_20)
{
dev_err(
dev, "Default addresses are not supported. Pick an address that is not 0x%02x or 0x%02x\n",
OX08B40_DEFAULT_I2C_ADDRESS_6C, OX08B40_DEFAULT_I2C_ADDRESS_20);
}
if (!gpio_is_valid(priv->cam_sid_gpio))
{
dev_err(dev, "Missing cam-sid-gpio, cannot program I2C addr. SID is %i\n", priv->cam_sid_gpio);
return -EINVAL;
}
// Set the camera's SID pin to high, which will set it's I2C address to OX08B40_DEFAULT_I2C_ADDRESS_20
gpio_set_value_cansleep(priv->cam_sid_gpio, 1);
msleep_range(1);
err = ox08b40_software_reset(priv, OX08B40_DEFAULT_I2C_ADDRESS_20);
if (err)
{
dev_err(dev, "Failed to software reset with error %i\n", err);
}
/*
* Have to make the I2C message manually because we are using a
* different I2C slave address for this transaction, rather than
* the one in the device tree for this device.
*/
data[0] = (OX08B40_SC_SCCB_ID_SID_LOW_ADDR >> 8) & 0xff;
data[1] = OX08B40_SC_SCCB_ID_SID_LOW_ADDR & 0xff;
data[2] = ((i2c_addr) << 1) & 0xff;
/*
* Use the programmable default I2C slave address so that if we have
* multiple sensors of this same kind, when we change one sensor's
* address, the next sensor address change message won't go to that
* same sensor.
*/
msg.addr = OX08B40_DEFAULT_I2C_ADDRESS_20;
msg.flags = 0;
msg.len = 3;
msg.buf = data;
if (i2c_transfer(priv->i2c_client->adapter, &msg, 1) != 1)
{
dev_err(dev, "Error assigning I2C address to 0x%02x\n", i2c_addr);
err = -EIO;
}
gpio_set_value_cansleep(priv->cam_sid_gpio, 0);
msleep_range(1);
return err;
}
static struct tegracam_ctrl_ops ox08b40_ctrl_ops = {
.numctrls = ARRAY_SIZE(ctrl_cid_list),
.ctrl_cid_list = ctrl_cid_list,
.set_gain = ox08b40_set_gain,
.set_exposure = ox08b40_set_exposure,
.set_frame_rate = ox08b40_set_frame_rate,
.set_group_hold = ox08b40_set_group_hold,
};
// Adapted from ov10823.c
static int ox08b40_power_on(struct camera_common_data *s_data)
{
struct camera_common_power_rail *pw = s_data->power;
struct camera_common_pdata *pdata = s_data->pdata;
struct ox08b40 *priv = (struct ox08b40 *)s_data->priv;
struct device *dev = &priv->i2c_client->dev;
int err = 0;
dev_dbg(dev, "%s: power on\n", __func__);
if (pdata->power_on)
{
err = pdata->power_on(pw);
if (err)
dev_err(dev, "%s failed.\n", __func__);
else
pw->state = SWITCH_ON;
return err;
}
/* In case the 12v supply was just powered on, we wait the max stabilization time. */
/* Stabilization time of the 12v supply on the tri-camera board in practice is ~6.5ms. */
/* We wait ~2x that just to be safe. */
usleep_range(12000, 12500);
/* Take camera out of reset */
if (pw->reset_gpio)
{
gpio_set_value_cansleep(pw->reset_gpio, 1);
}
/* Section 1.5 figure 1-3 */
/* Wait t1 = 0.5 ms after setting reset to high, and an additional */
/* 6ms before SCCB/I2C transactions are reliable. */
/* We take 0.5 + 6 ms = 6.5 ms and give ourselves a 2x safety factor with 13 ms */
usleep_range(13000, 14000);
err = ox08b40_i2c_addr_assign(priv, priv->i2c_client->addr);
if (err)
{
return err;
}
pw->state = SWITCH_ON;
return 0;
}
static int ox08b40_power_off(struct camera_common_data *s_data)
{
struct camera_common_power_rail *pw = s_data->power;
struct device *dev = s_data->dev;
dev_dbg(dev, "%s: power off\n", __func__);
/* One might expect to see the 12v regulator disabled here. Since it is shared between all cameras */
/* on the tri-camera board, we cannot turn it off without affecting other cameras. */
if (pw->reset_gpio)
{
gpio_set_value_cansleep(pw->reset_gpio, 0);
}
/* Even if we didn't actually do anything reflect that we're "off" as much as possible. */
pw->state = SWITCH_OFF;
return 0;
}
static int ox08b40_power_get(struct tegracam_device *tc_dev)
{
struct device *dev = tc_dev->dev;
struct camera_common_data *s_data = tc_dev->s_data;
struct camera_common_power_rail *pw = s_data->power;
struct camera_common_pdata *pdata = s_data->pdata;
const char *mclk_name;
struct clk *parent;
int err = 0;
mclk_name = pdata->mclk_name ? pdata->mclk_name : "extperiph1";
pw->mclk = devm_clk_get(dev, mclk_name);
if (IS_ERR(pw->mclk))
{
dev_err(dev, "unable to get clock %s\n", mclk_name);
return PTR_ERR(pw->mclk);
}
parent = devm_clk_get(dev, "pllp_grtba");
if (IS_ERR(parent))
dev_err(dev, "devm_clk_get failed for pllp_grtba");
else
clk_set_parent(pw->mclk, parent);
pw->reset_gpio = pdata->reset_gpio;
pw->state = SWITCH_OFF;
return err;
}
// TODO: Mostly unconfirmed, copied from imx334, but it seems reasonable
static int ox08b40_power_put(struct tegracam_device *tc_dev)
{
struct camera_common_data *s_data = tc_dev->s_data;
struct camera_common_power_rail *pw = s_data->power;
if (!pw)
{
return -EFAULT;
}
return 0;
}
static struct camera_common_pdata *ox08b40_parse_dt(struct tegracam_device *tc_dev)
{
struct device *dev = tc_dev->dev;
struct device_node *np = dev->of_node;
struct camera_common_pdata *board_priv_pdata;
const struct of_device_id *match;
int err;
int gpio;
if (!np)
{
dev_err(dev, "No device node provided hello\n");
return NULL;
}
match = of_match_device(ox08b40_of_match, dev);
if (!match)
{
dev_err(dev, "Failed to find matching device tree id\n");
return NULL;
}
board_priv_pdata = devm_kzalloc(dev, sizeof(*board_priv_pdata), GFP_KERNEL);
if (!board_priv_pdata)
{
return NULL;
}
err = camera_common_parse_clocks(dev, board_priv_pdata);
if (err)
{
dev_err(dev, "Failed to find clocks\n");
return NULL;
}
gpio = of_get_named_gpio(np, "reset-gpios", 0);
if (gpio > 0)
{
board_priv_pdata->reset_gpio = (unsigned int)gpio;
}
return board_priv_pdata;
}
static int ox08b40_set_mode(struct tegracam_device *tc_dev)
{
struct ox08b40 *priv = (struct ox08b40 *)tegracam_get_privdata(tc_dev);
struct camera_common_data *s_data = tc_dev->s_data;
struct device *dev = tc_dev->dev;
const struct of_device_id *match;
int err;
dev_dbg(dev, "%s: selecting mode %u\n", __func__, (u32)s_data->mode_prop_idx);
match = of_match_device(ox08b40_of_match, dev);
if (!match)
{
dev_err(dev, "Failed to find matching device tree id\n");
return -EINVAL;
}
if (err)
{
dev_err(dev, "Failed to software reset prior to mode table\n");
return err;
}
err = ox08b40_write_table(priv, mode_table[s_data->mode_prop_idx]);
if (err)
{
dev_err(dev, "Failed to write table from mode index %i with error %i\n", s_data->mode_prop_idx, err);
return err;
}
// TODO: Unconfirmed for ox08b40
/* Reflect the default state (per the mode table). */
// priv->frame_length = OX08B40_VMAX_MIN;
// priv->exposure_us = 33332;
return 0;
}
static int ox08b40_start_streaming(struct tegracam_device *tc_dev)
{
struct device *dev = tc_dev->dev;
struct ox08b40 *priv = (struct ox08b40 *)tegracam_get_privdata(tc_dev);
int err;
bool frame_sync_leader = false;
const char *str;
dev_dbg(dev, "%s\n", __func__);
err = of_property_read_string(dev->of_node, "frame_sync_leader", &str);
if (!err && !strcmp(str, "true"))
{
frame_sync_leader = true;
}
if (frame_sync_leader)
{
dev_dbg(dev, "%s: setting frame sync leader\n", __func__);
err = ox08b40_write_table(priv, ox08b40_mode_vsync_output);
}
else
{
dev_dbg(dev, "%s: setting frame sync follower\n", __func__);
err = ox08b40_write_table(priv, ox08b40_mode_fsync_input);
}
err = ox08b40_write_table(priv, mode_table[OX08B40_MODE_START_STREAM]);
if (err)
{
dev_err(dev, "%s: failed to write mode table\n", __func__);
}
return err;
}
static int ox08b40_stop_streaming(struct tegracam_device *tc_dev)
{
struct device *dev = tc_dev->dev;
struct ox08b40 *priv = (struct ox08b40 *)tegracam_get_privdata(tc_dev);
int err;
dev_dbg(dev, "%s\n", __func__);
err = ox08b40_write_table(priv, mode_table[OX08B40_MODE_STOP_STREAM]);
if (err)
{
dev_err(dev, "Error %d sending stop stream sequence\n", err);
return err;
}
// TODO: Below unconfirmed for OX08B40
/* Wait for a frame's worth of time to go by so we know things are done.
* Approximate a "line" here as 10 us, which gives us some buffer. */
usleep_range(priv->frame_length * 10, priv->frame_length * 10 * 2);
return 0;
}
static struct camera_common_sensor_ops ox08b40_common_ops = {
.numfrmfmts = ARRAY_SIZE(ox08b40_frmfmt),
.frmfmt_table = ox08b40_frmfmt,
.power_on = ox08b40_power_on,
.power_off = ox08b40_power_off,
.write_reg = ox08b40_write_reg,
.read_reg = ox08b40_read_reg,
.parse_dt = ox08b40_parse_dt,
.power_get = ox08b40_power_get,
.power_put = ox08b40_power_put,
.set_mode = ox08b40_set_mode,
.start_streaming = ox08b40_start_streaming,
.stop_streaming = ox08b40_stop_streaming,
};
static int ox08b40_verify_chip_id(struct ox08b40 *priv)
{
struct i2c_client *client = priv->i2c_client;
struct camera_common_data *s_data = priv->s_data;
u8 chip_id_hi, chip_id_mid, chip_id_lo;
u32 chip_id;
int err;
err = ox08b40_read_reg(s_data, OX08B40_SENSOR_ID_ADDR_MSB, &chip_id_hi);
if (err)
{
dev_err(&client->dev, "Failed to read chip ID high\n");
return err;
}
err = ox08b40_read_reg(s_data, OX08B40_SENSOR_ID_ADDR_MID, &chip_id_mid);
if (err)
{
dev_err(&client->dev, "Failed to read chip ID mid\n");
return err;
}
err = ox08b40_read_reg(s_data, OX08B40_SENSOR_ID_ADDR_LSB, &chip_id_lo);
if (err)
{
dev_err(&client->dev, "Failed to read chip ID low\n");
return err;
}
chip_id = (chip_id_hi << 16) | (chip_id_mid << 8) | chip_id_lo;
if (chip_id != OX08B40_CHIP_ID)
{
dev_err(&client->dev, "Read unknown chip ID 0x%04x\n", chip_id);
return -EINVAL;
}
return 0;
}
static int ox08b40_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
{
struct i2c_client *client = v4l2_get_subdevdata(sd);
dev_dbg(&client->dev, "%s:\n", __func__);
return 0;
}
static const struct v4l2_subdev_internal_ops ox08b40_subdev_internal_ops = {
.open = ox08b40_open,
};
static int ox08b40_probe(struct i2c_client *client, const struct i2c_device_id *id)
{
struct device *dev = &client->dev;
struct tegracam_device *tc_dev;
struct ox08b40 *priv;
int err;
dev_dbg(dev, "probing OX08B40 v4l2 sensor\n");
if (!IS_ENABLED(CONFIG_OF) || !client->dev.of_node) return -EINVAL;
priv = devm_kzalloc(dev, sizeof(struct ox08b40), GFP_KERNEL);
if (!priv) return -ENOMEM;
tc_dev = devm_kzalloc(dev, sizeof(struct tegracam_device), GFP_KERNEL);
if (!tc_dev) return -ENOMEM;
priv->i2c_client = tc_dev->client = client;
tc_dev->dev = dev;
strncpy(tc_dev->name, "ox08b40", sizeof(tc_dev->name));
tc_dev->dev_regmap_config = &sensor_regmap_config;
tc_dev->sensor_ops = &ox08b40_common_ops;
tc_dev->v4l2sd_internal_ops = &ox08b40_subdev_internal_ops;
tc_dev->tcctrl_ops = &ox08b40_ctrl_ops;
err = tegracam_device_register(tc_dev);
if (err)
{
dev_err(dev, "tegra camera driver registration failed\n");
return err;
}
priv->tc_dev = tc_dev;
priv->s_data = tc_dev->s_data;
priv->subdev = &tc_dev->s_data->subdev;
priv->cam_sid_gpio = of_get_named_gpio(dev->of_node, "addr-gpios", 0);
tegracam_set_privdata(tc_dev, (void *)priv);
err = camera_common_mclk_enable(priv->s_data);
if (err)
{
dev_err(dev, "Error %d turning on mclk\n", err);
goto error;
}
err = ox08b40_power_on(priv->s_data);
if (err)
{
dev_err(dev, "Error %d powering on sensor\n", err);
goto error;
}
err = ox08b40_verify_chip_id(priv);
if (err)
{
dev_err(dev, "verify chip ID failed\n");
goto error;
}
err = tegracam_v4l2subdev_register(tc_dev, true);
if (err)
{
dev_err(dev, "tegra camera subdev registration failed\n");
goto error;
}
dev_info(dev, "Detected OX08B40 sensor\n");
return 0;
error:
tegracam_device_unregister(priv->tc_dev);
return err;
}
static int ox08b40_remove(struct i2c_client *client)
{
struct camera_common_data *s_data = to_camera_common_data(&client->dev);
struct ox08b40 *priv = (struct ox08b40 *)s_data->priv;
tegracam_v4l2subdev_unregister(priv->tc_dev);
tegracam_device_unregister(priv->tc_dev);
return 0;
}
static const struct i2c_device_id ox08b40_id[] = {{"ox08b40", 0}, {}};
MODULE_DEVICE_TABLE(i2c, ox08b40_id);
static struct i2c_driver ox08b40_i2c_driver = {
.driver =
{
.name = "ox08b40",
.owner = THIS_MODULE,
.of_match_table = of_match_ptr(ox08b40_of_match),
},
.probe = ox08b40_probe,
.remove = ox08b40_remove,
.id_table = ox08b40_id,
};
module_i2c_driver(ox08b40_i2c_driver);
MODULE_DESCRIPTION("Media Controller driver for Omnivision OX08B40");
MODULE_AUTHOR("Blair Hagen ");
MODULE_LICENSE("GPL v2");