Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
171 changes: 170 additions & 1 deletion drivers/media/platform/apple/isp/isp-cam-test.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ struct isp_test_command_trace {
unsigned int fail_at;
int setfile_error;
unsigned int setfile_calls;
u32 last_opcode;
};

static int isp_test_send(struct apple_isp *isp, void *args, u32 insize,
Expand Down Expand Up @@ -190,6 +191,19 @@ static void isp_test_setfile_failure_stops_setup(struct kunit *test)
}
}

/* Count NV12 setup commands and fail the chosen one, without a fixture. */
static int isp_test_count_send(struct apple_isp *isp, void *args, u32 insize,
u32 outsize, int timeout)
{
struct kunit *test = kunit_get_current_test();
struct isp_test_command_trace *trace = test->priv;

trace->last_opcode = ((const u32 *)args)[1];
if (++trace->count == trace->fail_at)
return -ETIME;
return 0;
}

static void isp_test_optional_setfile_continues_setup(struct kunit *test)
{
static const int results[] = { 0, -ENOENT };
Expand All @@ -198,18 +212,170 @@ static void isp_test_optional_setfile_continues_setup(struct kunit *test)

isp_get_current_format(isp)->pixelformat = V4L2_PIX_FMT_NV12;
kunit_activate_static_stub(test, isp_ch_load_setfile, isp_test_load_setfile);
kunit_activate_static_stub(test, isp_cmd_send, isp_test_count_send);
test->priv = &trace;
for (unsigned int i = 0; i < ARRAY_SIZE(results); i++) {
trace = (struct isp_test_command_trace){
.setfile_error = results[i], .fail_at = 2,
};
/* The SBS command after tuning, rather than tuning, fails. */
/* The mains frequency after tuning, rather than tuning, fails. */
KUNIT_EXPECT_EQ(test, isp_ch_configure_capture(isp, 0), -ETIME);
KUNIT_EXPECT_EQ(test, trace.count, 2);
KUNIT_EXPECT_EQ(test, trace.last_opcode, 0x8212);
KUNIT_EXPECT_EQ(test, trace.setfile_calls, 1);
}
}

struct isp_test_nv12_trace {
unsigned int count;
u32 opcode[64];
u32 size[64];
u32 word[64][3];
};

/* Accept every NV12 setup command and record its opcode and first fields. */
static int isp_test_record(struct apple_isp *isp, void *args, u32 insize,
u32 outsize, int timeout)
{
struct kunit *test = kunit_get_current_test();
struct isp_test_nv12_trace *trace = test->priv;
const u32 *words = args;
unsigned int i = trace->count;

if (i >= ARRAY_SIZE(trace->opcode)) {
KUNIT_FAIL(test, "unexpected extra command");
return -EIO;
}
trace->opcode[i] = words[1];
trace->size[i] = insize;
for (unsigned int j = 0; j < 3 && 12 + 4 * j < insize; j++)
trace->word[i][j] = words[3 + j];
trace->count++;
return 0;
}

static int isp_test_find(const struct isp_test_nv12_trace *trace, u32 opcode)
{
for (unsigned int i = 0; i < trace->count; i++)
if (trace->opcode[i] == opcode)
return i;
return -1;
}

static int isp_test_no_setfile(struct apple_isp *isp, u32 ch)
{
return 0;
}

static struct apple_isp *isp_test_nv12_alloc(struct kunit *test,
enum isp_sensor_id id)
{
struct apple_isp *isp = isp_test_capture_alloc(test);
struct isp_format *fmt = isp_get_current_format(isp);

fmt->pixelformat = V4L2_PIX_FMT_NV12;
fmt->id = id;
fmt->version = isp_setfiles[id].version;
isp->temporal_filter = 1;
kunit_activate_static_stub(test, isp_ch_load_setfile, isp_test_no_setfile);
kunit_activate_static_stub(test, isp_cmd_send, isp_test_record);
return isp;
}

/* Expect exactly @n commands, in this order, from the first of them on. */
static void isp_test_expect_run(struct kunit *test,
const struct isp_test_nv12_trace *trace,
const u32 (*run)[2], unsigned int n)
{
int first = isp_test_find(trace, run[0][0]);

KUNIT_ASSERT_GE(test, first, 0);
KUNIT_ASSERT_LE(test, first + n, trace->count);
for (unsigned int i = 0; i < n; i++) {
KUNIT_EXPECT_EQ(test, trace->opcode[first + i], run[i][0]);
KUNIT_EXPECT_EQ(test, trace->size[first + i], run[i][1]);
}
}

static void isp_test_temporal_filter_band0(struct kunit *test)
{
static const u32 run[][2] = {
{ 0xc100, 0x18 }, { 0xc120, 0x10 }, { 0xc11c, 0x10 },
{ 0xc11d, 0x14 }, { 0xc102, 0x0c }, { 0xc113, 0x0c },
};
struct apple_isp *isp = isp_test_nv12_alloc(test, ISP_IMX958_2224_01);
struct isp_test_nv12_trace *trace;
int i;

trace = kunit_kzalloc(test, sizeof(*trace), GFP_KERNEL);
KUNIT_ASSERT_NOT_NULL(test, trace);
test->priv = trace;
KUNIT_EXPECT_EQ(test, isp_ch_configure_capture(isp, 0), 0);
isp_test_expect_run(test, trace, run, ARRAY_SIZE(run));
/* The start record is (1, 0, 0) whatever the DT property says. */
i = isp_test_find(trace, 0xc100);
KUNIT_EXPECT_EQ(test, trace->word[i][0], 1);
KUNIT_EXPECT_EQ(test, trace->word[i][1], 0);
KUNIT_EXPECT_EQ(test, trace->word[i][2], 0);
i = isp_test_find(trace, 0xc11d);
KUNIT_EXPECT_EQ(test, trace->word[i][0], 0x7800);
KUNIT_EXPECT_EQ(test, trace->word[i][1], 0x6400);
KUNIT_EXPECT_LT(test, isp_test_find(trace, 0xc114), 0);
}

static void isp_test_temporal_filter_off(struct kunit *test)
{
static const u32 run[][2] = {
{ 0xc114, 0x0c }, { 0xc103, 0x0c }, { 0xc101, 0x0c },
};
struct apple_isp *isp = isp_test_nv12_alloc(test, ISP_IMX558_1925_03);
struct isp_test_nv12_trace *trace;

trace = kunit_kzalloc(test, sizeof(*trace), GFP_KERNEL);
KUNIT_ASSERT_NOT_NULL(test, trace);
test->priv = trace;
KUNIT_EXPECT_EQ(test, isp_ch_configure_capture(isp, 0), 0);
isp_test_expect_run(test, trace, run, ARRAY_SIZE(run));
KUNIT_EXPECT_LT(test, isp_test_find(trace, 0xc100), 0);
KUNIT_EXPECT_LT(test, isp_test_find(trace, 0xc102), 0);
KUNIT_EXPECT_LT(test, isp_test_find(trace, 0xc113), 0);
}

static void isp_test_power_line_frequency(struct kunit *test)
{
struct apple_isp *isp = isp_test_nv12_alloc(test, ISP_IMX958_2224_01);
struct isp_test_nv12_trace *trace;
int i;

trace = kunit_kzalloc(test, sizeof(*trace), GFP_KERNEL);
KUNIT_ASSERT_NOT_NULL(test, trace);
test->priv = trace;

/* No control: 50 Hz, in Hz 16.16 */
KUNIT_EXPECT_EQ(test, isp_ch_configure_capture(isp, 0), 0);
i = isp_test_find(trace, 0x8212);
KUNIT_ASSERT_GE(test, i, 0);
KUNIT_EXPECT_EQ(test, trace->size[i], 0x10);
KUNIT_EXPECT_EQ(test, trace->word[i][0], 0x00320000);
/* After the tuning, before the sensor configuration is selected */
KUNIT_EXPECT_LT(test, i, isp_test_find(trace, 0x107));

isp->power_line_frequency =
v4l2_ctrl_new_std_menu(&isp->ctrl_handler, NULL,
V4L2_CID_POWER_LINE_FREQUENCY,
V4L2_CID_POWER_LINE_FREQUENCY_60HZ,
BIT(V4L2_CID_POWER_LINE_FREQUENCY_DISABLED),
V4L2_CID_POWER_LINE_FREQUENCY_50HZ);
KUNIT_ASSERT_NOT_NULL(test, isp->power_line_frequency);
KUNIT_ASSERT_EQ(test, v4l2_ctrl_s_ctrl(isp->power_line_frequency,
V4L2_CID_POWER_LINE_FREQUENCY_60HZ), 0);
memset(trace, 0, sizeof(*trace));
KUNIT_EXPECT_EQ(test, isp_ch_configure_capture(isp, 0), 0);
i = isp_test_find(trace, 0x8212);
KUNIT_ASSERT_GE(test, i, 0);
KUNIT_EXPECT_EQ(test, trace->word[i][0], 0x003c0000);
}

static int isp_test_config_reply(struct apple_isp *isp, void *args, u32 insize,
u32 outsize, int timeout)
{
Expand Down Expand Up @@ -500,6 +666,9 @@ static struct kunit_case isp_cam_test_cases[] = {
KUNIT_CASE(isp_test_sensor_config_failure),
KUNIT_CASE(isp_test_preview_stream),
KUNIT_CASE(isp_test_preview_command_failures),
KUNIT_CASE(isp_test_temporal_filter_band0),
KUNIT_CASE(isp_test_temporal_filter_off),
KUNIT_CASE(isp_test_power_line_frequency),
{}
};

Expand Down
107 changes: 96 additions & 11 deletions drivers/media/platform/apple/isp/isp-cam.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,26 @@
#include "isp-ipc.h"
#include "isp-regs.h"

/*
* How the ISP's own temporal noise filter is set up for a sensor. macOS
* picks this per sensor on the H17 interface: the IMX558 (J700) runs
* without it and the IMX958 (J616s) runs it with explicit strength
* controls. Everything else keeps the original start sequence.
*/
enum isp_temporal_filter {
ISP_TEMPORAL_FILTER_START,
ISP_TEMPORAL_FILTER_OFF,
ISP_TEMPORAL_FILTER_BAND0,
};

struct isp_setfile {
u32 version;
u32 magic;
const char *path;
size_t size;
/* bytes present in the file if fewer than size; the rest is zero */
size_t file_size;
enum isp_temporal_filter temporal_filter;
};

// clang-format off
Expand Down Expand Up @@ -49,7 +62,8 @@ static const struct isp_setfile isp_setfiles[] = {
[ISP_IMX514_2820_04] = {0x514, 0x28200405, "apple/isp_2820_04XX.dat", 0xa198},
[ISP_IMX558_1921_01] = {0x558, 0x19210106, "apple/isp_1921_01XX.dat", 0xad40},
[ISP_IMX558_1922_02] = {0x558, 0x19220201, "apple/isp_1922_02XX.dat", 0xad40},
[ISP_IMX558_1925_03] = {0x558, 0x19250306, "apple/isp_1925_03XX.dat", 0xcbc0, 0xcba6},
[ISP_IMX558_1925_03] = {0x558, 0x19250306, "apple/isp_1925_03XX.dat", 0xcbc0, 0xcba6,
ISP_TEMPORAL_FILTER_OFF},
[ISP_IMX603_7920_01] = {0x603, 0x79200109, "apple/isp_7920_01XX.dat", 0xad2c},
[ISP_IMX603_7920_02] = {0x603, 0x79200205, "apple/isp_7920_02XX.dat", 0xad2c},
[ISP_IMX603_7921_01] = {0x603, 0x79210104, "apple/isp_7921_01XX.dat", 0xad90},
Expand All @@ -71,7 +85,8 @@ static const struct isp_setfile isp_setfiles[] = {
[ISP_IMX814_2123_01] = {0x814, 0x21230101, "apple/isp_2123_01XX.dat", 0xed54},
[ISP_IMX853_7622_01] = {0x853, 0x76220112, "apple/isp_7622_01XX.dat", 0x247f8},
[ISP_IMX913_7523_01] = {0x913, 0x75230107, "apple/isp_7523_01XX.dat", 0x247f8},
[ISP_IMX958_2224_01] = {0x958, 0x22240106, "apple/isp_2224_01XX.dat", 0x6ff4},
[ISP_IMX958_2224_01] = {0x958, 0x22240106, "apple/isp_2224_01XX.dat", 0x6ff4, 0,
ISP_TEMPORAL_FILTER_BAND0},
[ISP_VD56G0_6221_01] = {0xd56, 0x62210102, "apple/isp_6221_01XX.dat", 0x1b80},
[ISP_VD56G0_6222_01] = {0xd56, 0x62220102, "apple/isp_6222_01XX.dat", 0x1b80},
};
Expand Down Expand Up @@ -689,6 +704,80 @@ static int isp_ch_configure_frame_rate(struct apple_isp *isp, u32 ch)
return isp_cmd_ch_ae_frame_rate_min_set(isp, ch, min_rate);
}

/*
* Tell auto exposure the mains frequency, so that it picks exposure times
* that do not beat with lights on that supply. The firmware takes it in Hz,
* 16.16 fixed point. macOS sends it on the H17 interface for every camera.
*/
static int isp_ch_configure_power_line(struct apple_isp *isp, u32 ch)
{
u32 hz = 50;

if (isp->hw->fw_abi != ISP_FW_ABI_H17)
return 0;

if (isp->power_line_frequency &&
v4l2_ctrl_g_ctrl(isp->power_line_frequency) ==
V4L2_CID_POWER_LINE_FREQUENCY_60HZ)
hz = 60;

return isp_cmd_apple_ch_ae_flicker_freq_update_current_set(isp, ch,
hz << 16);
}

static int isp_ch_configure_temporal_filter(struct apple_isp *isp, u32 ch)
{
struct isp_format *fmt = isp_get_format(isp, ch);
int err;

switch (isp_setfiles[fmt->id].temporal_filter) {
case ISP_TEMPORAL_FILTER_OFF:
/* The firmware keeps its state between streams; stop it. */
err = isp_cmd_apple_ch_temporal_filter_disable(isp, ch);
if (err)
return err;

err = isp_cmd_apple_ch_motion_history_stop(isp, ch);
if (err)
return err;

return isp_cmd_apple_ch_temporal_filter_stop(isp, ch);

case ISP_TEMPORAL_FILTER_BAND0:
err = isp_cmd_apple_ch_tnr_start_h17(isp, ch);
if (err)
return err;

err = isp_cmd_apple_ch_tnr_high_strength_set(isp, ch, 0);
if (err)
return err;

err = isp_cmd_apple_ch_tnr_band0_mod_set(isp, ch, 0);
if (err)
return err;

/* 120 and 100, in 1/256 steps */
err = isp_cmd_apple_ch_tnr_band0_threshold_set(isp, ch, 120 << 8,
100 << 8);
if (err)
return err;
break;

default:
err = isp_cmd_apple_ch_temporal_filter_start(isp, ch,
isp->temporal_filter);
if (err)
return err;
break;
}

err = isp_cmd_apple_ch_motion_history_start(isp, ch);
if (err)
return err;

return isp_cmd_apple_ch_temporal_filter_enable(isp, ch);
}

static int isp_ch_configure_pools(struct apple_isp *isp, u32 ch)
{
struct isp_format *fmt = isp_get_format(isp, ch);
Expand Down Expand Up @@ -747,6 +836,10 @@ static int isp_ch_configure_capture(struct apple_isp *isp, u32 ch)
return err;
}

err = isp_ch_configure_power_line(isp, ch);
if (err)
return err;

select_config:
err = isp_cmd_ch_sbs_enable(isp, ch, 1);
if (err)
Expand Down Expand Up @@ -845,15 +938,7 @@ static int isp_ch_configure_capture(struct apple_isp *isp, u32 ch)
return err;
}

err = isp_cmd_apple_ch_temporal_filter_start(isp, ch, isp->temporal_filter);
if (err)
return err;

err = isp_cmd_apple_ch_motion_history_start(isp, ch);
if (err)
return err;

err = isp_cmd_apple_ch_temporal_filter_enable(isp, ch);
err = isp_ch_configure_temporal_filter(isp, ch);
if (err)
return err;

Expand Down
Loading