Skip to content
Open
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
157 changes: 157 additions & 0 deletions drivers/media/platform/apple/isp/isp-cam-test.c
Original file line number Diff line number Diff line change
Expand Up @@ -1100,6 +1100,162 @@ static void isp_test_permodule_lsc_wire(struct kunit *test)
KUNIT_EXPECT_EQ(test, w.grid_calls, 2);
}

enum {
ISP_TEST_US_G = 5,
ISP_TEST_US_N = ISP_TEST_US_G * ISP_TEST_US_G,
ISP_TEST_US_LSC = 0x54 + 4 * ISP_TEST_US_N * 2,
};

/* Module shading of plane @t at grid point (@x, @y); @tilt shades plane 1. */
static u32 isp_test_us_nvm(int t, int x, int y, int tilt)
{
int r2 = (x - 2) * (x - 2) + (y - 2) * (y - 2);

return 200 - 12 * r2 - (t == 1 ? tilt * (4 - x) : 0) -
(t == 2 ? 3 * y : 0);
}

/*
* A 1024x1024 setfile whose 5x5 tables sit on the 5x5 NVM points. The
* stock tables correct a typical module with half this module's tilt;
* @turned tables see the module turned by 180 degrees, so the NVM grid
* must be read turned to match them.
*/
static u8 *isp_test_us_setfile(struct kunit *test, size_t *len, bool turned)
{
u8 *data, *sec;

*len = 0x14 + 12 + ISP_TEST_US_LSC;
data = kunit_kzalloc(test, *len, GFP_KERNEL);
KUNIT_ASSERT_NOT_NULL(test, data);
sec = data + 0x14 + 12;
put_unaligned_le32(8 + ISP_TEST_US_LSC, sec - 12);
put_unaligned_le32(0xc003, sec - 4);
put_unaligned_le16(1024, sec + 6);
put_unaligned_le16(1024, sec + 8);
for (int t = 0; t < 4; t++) {
u8 *rec = sec + 0xa + 18 * t;

put_unaligned_le16(128, rec + 2);
put_unaligned_le16(128, rec + 4);
put_unaligned_le16(t * ISP_TEST_US_N, rec + 6);
put_unaligned_le16(ISP_TEST_US_G, rec + 14);
put_unaligned_le16(ISP_TEST_US_G, rec + 16);
}
for (int k = 0; k < ISP_TEST_US_N; k++) {
int x = k % ISP_TEST_US_G, y = k / ISP_TEST_US_G;
int nx = turned ? 4 - x : x, ny = turned ? 4 - y : y;
int r2 = (x - 2) * (x - 2) + (y - 2) * (y - 2);
u32 m[4], sum = 0;

for (int t = 0; t < 4; t++) {
m[t] = isp_test_us_nvm(t, nx, ny, 2);
sum += m[t];
}
for (int t = 0; t < 4; t++)
put_unaligned_le16((4096 + 512 * r2) * sum / (4 * m[t]),
sec + 0x54 + 2 * (t * ISP_TEST_US_N + k));
}
return data;
}

/* Gain times module shading of plane @t at table point @k. */
static u64 isp_test_us_out(const u8 *sec, int t, int k, bool turned)
{
int x = k % ISP_TEST_US_G, y = k / ISP_TEST_US_G;

if (turned) {
x = 4 - x;
y = 4 - y;
}
return (u64)get_unaligned_le16(sec + 0x54 + 2 * (t * ISP_TEST_US_N + k)) *
isp_test_us_nvm(t, x, y, 4);
}

static void isp_test_unit_shading(struct kunit *test)
{
/* binary32 0.0, 0.25, 0.5, 0.75, 1.0 */
static const u32 coords[ISP_TEST_US_G] = {
0x00000000, 0x3e800000, 0x3f000000, 0x3f400000, 0x3f800000,
};
struct apple_isp *isp = isp_test_capture_alloc(test);
struct isp_permodule_lsc lsc = {
.valid = true, .num_x = ISP_TEST_US_G, .num_y = ISP_TEST_US_G,
.bit_depth = 10, .data_size = 4 * ISP_TEST_US_N * 2,
};
__le16 *nvm = kunit_kzalloc(test, lsc.data_size, GFP_KERNEL);
const int ref = 2 * ISP_TEST_US_G + 2;

KUNIT_ASSERT_NOT_NULL(test, nvm);
KUNIT_EXPECT_EQ(test, isp_lsc_coord(0x3e800000), LSC_ONE / 4);
KUNIT_EXPECT_EQ(test, isp_lsc_coord(0x3f800000), LSC_ONE);
KUNIT_EXPECT_EQ(test, isp_lsc_coord(0x00000001), 0);
KUNIT_EXPECT_LT(test, isp_lsc_coord(0xbf000000), 0);
KUNIT_EXPECT_LT(test, isp_lsc_coord(0x3f800001), 0);
KUNIT_EXPECT_LT(test, isp_lsc_coord(0x7fc00000), 0);

/* 10-bit samples, stored upright; garbage above bit 9 is masked. */
for (int t = 0; t < 4; t++)
for (int k = 0; k < ISP_TEST_US_N; k++)
nvm[t * ISP_TEST_US_N + k] = cpu_to_le16(0xfc00 |
4 * isp_test_us_nvm(t, k % ISP_TEST_US_G,
k / ISP_TEST_US_G, 4));
lsc.data = (u8 *)nvm;
memcpy(lsc.x_coords, coords, sizeof(coords));
memcpy(lsc.y_coords, coords, sizeof(coords));

for (int turned = 0; turned < 2; turned++) {
size_t len;
u8 *data = isp_test_us_setfile(test, &len, turned);
u8 *sec = data + 0x14 + 12;
u16 stock_ref[4];

for (int t = 0; t < 4; t++)
stock_ref[t] = get_unaligned_le16(sec + 0x54 +
2 * (t * ISP_TEST_US_N + ref));
KUNIT_ASSERT_TRUE(test, isp_setfile_unit_shading(isp, data, len, &lsc, 0));

for (int t = 0; t < 4; t++) {
u16 top = 0;

/* the centre keeps its stock gains */
KUNIT_EXPECT_EQ(test, get_unaligned_le16(sec + 0x54 +
2 * (t * ISP_TEST_US_N + ref)), stock_ref[t]);
for (int k = 0; k < ISP_TEST_US_N; k++) {
u64 a = isp_test_us_out(sec, t, k, turned) *
isp_test_us_out(sec, 0, ref, turned);
u64 b = isp_test_us_out(sec, 0, k, turned) *
isp_test_us_out(sec, t, ref, turned);

/* every plane leaves the same shape: grey stays grey */
KUNIT_EXPECT_LE_MSG(test, abs_diff(a, b), b / 200,
"turned %d plane %d point %d", turned, t, k);
top = max(top, get_unaligned_le16(sec + 0x54 +
2 * (t * ISP_TEST_US_N + k)));
}
KUNIT_EXPECT_EQ(test, get_unaligned_le16(sec + 0xa + 18 * t), top);
}
}

/* Bad grids leave the stock tables alone. */
{
size_t len;
u8 *data = isp_test_us_setfile(test, &len, false);
u8 *copy = kunit_kmalloc(test, len, GFP_KERNEL);

KUNIT_ASSERT_NOT_NULL(test, copy);
memcpy(copy, data, len);
lsc.x_coords[2] = lsc.x_coords[1];
KUNIT_EXPECT_FALSE(test, isp_setfile_unit_shading(isp, data, len, &lsc, 0));
lsc.x_coords[2] = coords[2];
/* a shading centre below the frame */
KUNIT_EXPECT_FALSE(test, isp_setfile_unit_shading(isp, data, len, &lsc, 600));
nvm[3 * ISP_TEST_US_N + 7] = cpu_to_le16(0xfc00);
KUNIT_EXPECT_FALSE(test, isp_setfile_unit_shading(isp, data, len, &lsc, 0));
KUNIT_EXPECT_MEMEQ(test, data, copy, len);
}
}

static void isp_test_failed_fw_stop_lights_off(struct kunit *test)
{
struct apple_isp *isp = isp_test_indicator_alloc(test);
Expand Down Expand Up @@ -1181,6 +1337,7 @@ static struct kunit_case isp_cam_test_cases[] = {
KUNIT_CASE(isp_test_stream_failure_lights_off),
KUNIT_CASE(isp_test_failed_fw_stop_lights_off),
KUNIT_CASE(isp_test_permodule_lsc_wire),
KUNIT_CASE(isp_test_unit_shading),
{}
};

Expand Down
Loading
Loading