diff --git a/drivers/media/platform/apple/isp/isp-cam-test.c b/drivers/media/platform/apple/isp/isp-cam-test.c index ffa7c9f1ad490..933ead1e9ef97 100644 --- a/drivers/media/platform/apple/isp/isp-cam-test.c +++ b/drivers/media/platform/apple/isp/isp-cam-test.c @@ -1008,6 +1008,115 @@ static void isp_test_failed_fw_stop_lights_off(struct kunit *test) KUNIT_EXPECT_EQ(test, trace.set_off_at, 1); } +static void isp_test_permodule_lsc_math(struct kunit *test) +{ + /* IEEE-754 binary32 coordinates: 0.0, 0.25, 0.5, 0.75, 1.0 */ + static const u32 coords[5] = { + 0x00000000, 0x3e800000, 0x3f000000, 0x3f400000, 0x3f800000, + }; + enum { GW = 5, GH = 5, LSC = 0x54 + 4 * GW * GH * 2, FLAG = 8 }; + struct apple_isp *isp = isp_test_capture_alloc(test); + size_t len = 0x14 + 12 + LSC + 12 + LSC + 12 + FLAG; + u8 *data = kunit_kzalloc(test, len, GFP_KERNEL); + u8 *lsc_sec, *dsc_sec, *flag_sec; + u8 nvm8[4 * 25]; + __le16 nvm10[4 * 25]; + struct isp_permodule_lsc lsc = {}; + struct isp_lsc_params p = { + .ideal_pct = 84, .max_gain = U16_MAX, + .flip_permodule = false, .avg_green = true, + }; + + KUNIT_ASSERT_NOT_NULL(test, data); + KUNIT_EXPECT_EQ(test, isp_f32_to_q24(0x00000000), 0); + KUNIT_EXPECT_EQ(test, isp_f32_to_q24(0x3f000000), LSC_ONE >> 1); + KUNIT_EXPECT_EQ(test, isp_f32_to_q24(0x3f800000), LSC_ONE); + KUNIT_EXPECT_LT(test, isp_f32_to_q24(0xbf000000), 0); + KUNIT_EXPECT_LT(test, isp_f32_to_q24(0x3fc00000), 0); + KUNIT_EXPECT_LT(test, isp_f32_to_q24(0x7fc00000), 0); + + lsc_sec = data + 0x14 + 12; + dsc_sec = lsc_sec + LSC + 12; + flag_sec = dsc_sec + LSC + 12; + put_unaligned_le32(8 + LSC, lsc_sec - 12); + put_unaligned_le32(0xc003, lsc_sec - 4); + put_unaligned_le32(8 + LSC, dsc_sec - 12); + put_unaligned_le32(0xc021, dsc_sec - 4); + put_unaligned_le32(8 + FLAG, flag_sec - 12); + put_unaligned_le32(0x0a2a, flag_sec - 4); + put_unaligned_le16(1024, lsc_sec + 6); + put_unaligned_le16(1024, lsc_sec + 8); + put_unaligned_le16(4 * GW * GH, lsc_sec + 0x52); + put_unaligned_le16(4 * GW * GH, dsc_sec + 0x52); + for (int t = 0; t < 4; t++) { + u8 *rec = lsc_sec + 0xa + 18 * t; + + put_unaligned_le16(128, rec + 2); + put_unaligned_le16(128, rec + 4); + put_unaligned_le16(t * GW * GH, rec + 6); + put_unaligned_le16(GW, rec + 14); + put_unaligned_le16(GH, rec + 16); + } + for (int i = 0; i < 4 * GW * GH; i++) + put_unaligned_le16(SNF_ONE + 1024, dsc_sec + 0x54 + 2 * i); + + /* 8-bit asymmetric vignetting: plane 0 darker than plane 3 at corner (0,0). */ + for (int t = 0; t < 4; t++) { + for (int y = 0; y < 5; y++) { + for (int x = 0; x < 5; x++) { + int r2 = (x - 2) * (x - 2) + (y - 2) * (y - 2); + int v = 250 - r2 * (15 + 3 * t); + + nvm8[t * 25 + y * 5 + x] = max(v, 50); + nvm10[t * 25 + y * 5 + x] = cpu_to_le16(max(v, 50) * 4); + } + } + } + + lsc.valid = true; + lsc.num_x = 5; + lsc.num_y = 5; + lsc.bit_depth = 8; + lsc.data_size = sizeof(nvm8); + lsc.data = nvm8; + memcpy(lsc.x_coords, coords, sizeof(coords)); + memcpy(lsc.y_coords, coords, sizeof(coords)); + + KUNIT_EXPECT_TRUE(test, isp_setfile_apply_lsc(isp, data, len, &lsc, &p, 0)); + KUNIT_EXPECT_EQ(test, flag_sec[4], 1); + /* Center (2,2) has measured=1.0 and ideal=1.0 -> gain ~4096. */ + KUNIT_EXPECT_EQ(test, get_unaligned_le16(lsc_sec + 0x54 + 2 * 12), 4096); + /* Legacy green averaging makes ch 0 (Gr) and ch 3 (Gb) match. */ + KUNIT_EXPECT_EQ(test, get_unaligned_le16(lsc_sec + 0x54), + get_unaligned_le16(lsc_sec + 0x54 + 2 * (3 * 25))); + /* Corners get boost > 1.0x and max_gain recorded in channel header. */ + KUNIT_EXPECT_GT(test, get_unaligned_le16(lsc_sec + 0x54), 5000); + KUNIT_EXPECT_GE(test, get_unaligned_le16(lsc_sec + 0xa), + get_unaligned_le16(lsc_sec + 0x54)); + + /* 10-bit H17 path without green averaging, clamped to 0x4fff, then DSC *1.25. */ + lsc.bit_depth = 10; + lsc.data_size = sizeof(nvm10); + lsc.data = (u8 *)nvm10; + p.avg_green = false; + p.max_gain = 0x4fff; + KUNIT_EXPECT_TRUE(test, isp_setfile_apply_lsc(isp, data, len, &lsc, &p, -156)); + KUNIT_EXPECT_NE(test, get_unaligned_le16(lsc_sec + 0x54), + get_unaligned_le16(lsc_sec + 0x54 + 2 * (3 * 25))); + { + u16 before = get_unaligned_le16(lsc_sec + 0x54 + 2 * 12); + + isp_setfile_apply_dsc(data, len); + KUNIT_EXPECT_EQ(test, get_unaligned_le16(lsc_sec + 0x54 + 2 * 12), + DIV_ROUND_CLOSEST((u32)before * (SNF_ONE + 1024), + SNF_ONE)); + } + + /* Non-monotonic coordinates are rejected cleanly. */ + lsc.x_coords[2] = lsc.x_coords[1]; + KUNIT_EXPECT_FALSE(test, isp_setfile_apply_lsc(isp, data, len, &lsc, &p, 0)); +} + struct isp_test_lsc_wire { bool h17; u8 info_calls; @@ -1134,6 +1243,7 @@ static struct kunit_case isp_cam_test_cases[] = { KUNIT_CASE(isp_test_temporal_filter_off), KUNIT_CASE(isp_test_power_line_frequency), KUNIT_CASE(isp_test_snf_radial), + KUNIT_CASE(isp_test_permodule_lsc_math), KUNIT_CASE(isp_test_permodule_lsc_wire), KUNIT_CASE(isp_test_indicator_set_fails_still_streams), KUNIT_CASE(isp_test_dcp_bound_at_stream_start), diff --git a/drivers/media/platform/apple/isp/isp-cam.c b/drivers/media/platform/apple/isp/isp-cam.c index 6505791b863b2..79834d7dc7c3a 100644 --- a/drivers/media/platform/apple/isp/isp-cam.c +++ b/drivers/media/platform/apple/isp/isp-cam.c @@ -844,6 +844,9 @@ static void isp_setfile_snf_radial(struct apple_isp *isp, void *data, dev_warn_once(isp->dev, "setfile: noise filter gains not computed, keeping the stock ones\n"); } +#define LSC_Q 24 +#define LSC_ONE ((s64)BIT_ULL(LSC_Q)) + struct isp_lsc_params { u16 ideal_pct; u16 max_gain; @@ -878,6 +881,338 @@ static struct isp_lsc_params isp_sensor_lsc_params(const struct apple_isp *isp, return p; } +/* Convert a non-negative IEEE-754 binary32 float in [0, 1] to Q24, or -1. */ +static s64 isp_f32_to_q24(u32 bits) +{ + u32 exp_raw = (bits >> 23) & 0xff; + u32 mant; + int exp, shift; + + if ((bits >> 31) || exp_raw == 0xff || bits > 0x3f800000U) + return -1; + if (!exp_raw) + return 0; + + exp = (int)exp_raw - 127; + mant = (1U << 23) | (bits & 0x7fffffU); + shift = exp + (LSC_Q - 23); + if (shift >= 0) + return (s64)mant << shift; + if (-shift >= 32) + return 0; + return (s64)((mant + (1U << (-shift - 1))) >> -shift); +} + +/* Non-uniform Bessel-Hermite cubic interpolation in Q24 on knots t0..t3. */ +static s64 isp_lsc_cubic(const s64 v[4], const s64 t[4], s64 s) +{ + s64 h0 = t[1] - t[0], h1 = t[2] - t[1], h2 = t[3] - t[2]; + s64 m1, m2, c2, c3, p; + + m1 = (v[2] - v[1]) + div64_s64((v[1] - v[0]) * h1, h0) - + div64_s64((v[2] - v[0]) * h1, h0 + h1); + m2 = (v[2] - v[1]) + div64_s64((v[3] - v[2]) * h1, h2) - + div64_s64((v[3] - v[1]) * h1, h1 + h2); + c2 = 3 * (v[2] - v[1]) - 2 * m1 - m2; + c3 = 2 * (v[1] - v[2]) + m1 + m2; + + p = div64_s64(c3 * s, LSC_ONE) + c2; + p = div64_s64(p * s, LSC_ONE) + m1; + return div64_s64(p * s, LSC_ONE) + v[1]; +} + +static s64 isp_lsc_nvm_val(const struct isp_permodule_lsc *lsc, int plane, + int x, int y, bool flip, u32 norm) +{ + int n = (int)lsc->num_x * lsc->num_y; + int idx = y * lsc->num_x + x; + u32 raw; + + if (flip) + idx = n - 1 - idx; + idx += plane * n; + if (lsc->bit_depth >= 9) + raw = get_unaligned_le16(lsc->data + 2 * idx) & + GENMASK(lsc->bit_depth - 1, 0); + else + raw = lsc->data[idx]; + + return div64_u64((u64)raw << LSC_Q, norm); +} + +static s64 isp_lsc_sample(const struct isp_permodule_lsc *lsc, int plane, + const s64 *xq, const s64 *yq, u32 plane_w, + u32 plane_h, u32 px, u32 py, int ux, int uy, + bool flip, u32 norm) +{ + if (ux >= 2 && uy >= 2 && ux - 1 <= lsc->num_x - 3 && + uy - 1 <= lsc->num_y - 3) { + s64 sy_t = div64_s64((div64_s64((s64)py << LSC_Q, plane_h) - + yq[uy - 1]) * LSC_ONE, + yq[uy] - yq[uy - 1]); + s64 sx_t = div64_s64((div64_s64((s64)px << LSC_Q, plane_w) - + xq[ux - 1]) * LSC_ONE, + xq[ux] - xq[ux - 1]); + s64 col_vals[4]; + + for (int kx = 0; kx < 4; kx++) { + s64 col[4]; + + for (int ky = 0; ky < 4; ky++) + col[ky] = isp_lsc_nvm_val(lsc, plane, + ux - 2 + kx, + uy - 2 + ky, flip, + norm); + col_vals[kx] = clamp_t(s64, + isp_lsc_cubic(col, &yq[uy - 2], + sy_t), + 0, 2 * LSC_ONE); + } + return isp_lsc_cubic(col_vals, &xq[ux - 2], sx_t); + } + + int bx = ux ? min(ux - 1, lsc->num_x - 2) : 0; + int by = uy ? min(uy - 1, lsc->num_y - 2) : 0; + s64 v00 = isp_lsc_nvm_val(lsc, plane, bx, by, flip, norm); + s64 v10 = isp_lsc_nvm_val(lsc, plane, bx + 1, by, flip, norm); + s64 v01 = isp_lsc_nvm_val(lsc, plane, bx, by + 1, flip, norm); + s64 v11 = isp_lsc_nvm_val(lsc, plane, bx + 1, by + 1, flip, norm); + s64 x0 = (xq[bx] * plane_w + (LSC_ONE >> 1)) >> LSC_Q; + s64 x1 = (xq[bx + 1] * plane_w + (LSC_ONE >> 1)) >> LSC_Q; + s64 y0 = (yq[by] * plane_h + (LSC_ONE >> 1)) >> LSC_Q; + s64 y1 = (yq[by + 1] * plane_h + (LSC_ONE >> 1)) >> LSC_Q; + s64 top = x1 > x0 ? div64_s64(v00 * (x1 - px) + v10 * (px - x0), + x1 - x0) : v00; + s64 bot = x1 > x0 ? div64_s64(v01 * (x1 - px) + v11 * (px - x0), + x1 - x0) : v01; + + return y1 > y0 ? div64_s64(top * (y1 - py) + bot * (py - y0), + y1 - y0) : top; +} + +/* + * Replace section 0xc003's generic lens shading tables with per-unit gains + * computed from the sensor NVM grid against an ideal cos^4 illumination + * curve (corner falloff @p->ideal_pct %). Interior points use non-uniform + * bicubic interpolation; border cells fall back to bilinear. Marks section + * 0x0a2a when present so firmware knows per-module shading was applied. + */ +static bool isp_setfile_apply_lsc(struct apple_isp *isp, void *data, size_t len, + const struct isp_permodule_lsc *lsc, + const struct isp_lsc_params *p, int centre_dy) +{ + u32 lsc_size, flag_size, w, h, plane_w, plane_h, norm; + size_t sec = isp_setfile_find(data, len, 0xc003, &lsc_size); + size_t flag_sec = isp_setfile_find(data, len, 0x0a2a, &flag_size); + s64 xq[ISP_LSC_MAX_GRID_X], yq[ISP_LSC_MAX_GRID_Y]; + s64 cx2, cy2, min_meas; + u64 k_q30 = 0; + int t; + + if (!sec || !lsc->valid || !lsc->data || p->ideal_pct < 10 || + p->ideal_pct > 100 || lsc_size < 0x54) + return false; + + w = get_unaligned_le16(data + sec + 6); + h = get_unaligned_le16(data + sec + 8); + plane_w = w >> 1; + plane_h = h >> 1; + if (!plane_w || !plane_h || w > 16384 || h > 16384) + return false; + + for (int i = 0; i < lsc->num_x; i++) { + xq[i] = isp_f32_to_q24(lsc->x_coords[i]); + if (xq[i] < 0 || (i && xq[i] - xq[i - 1] < (LSC_ONE >> 10))) + return false; + } + for (int j = 0; j < lsc->num_y; j++) { + yq[j] = isp_f32_to_q24(lsc->y_coords[j]); + if (yq[j] < 0 || (j && yq[j] - yq[j - 1] < (LSC_ONE >> 10))) + return false; + } + + /* Validate all four channel records before mutating the setfile. */ + for (t = 0; t < 4; t++) { + const void *rec = data + sec + 0xa + 18 * t; + u64 sx = get_unaligned_le16(rec + 2); + u64 sy = get_unaligned_le16(rec + 4); + u64 tab = 0x54 + 2ULL * get_unaligned_le16(rec + 6); + u64 ox = get_unaligned_le16(rec + 10); + u64 oy = get_unaligned_le16(rec + 12); + u64 gw = get_unaligned_le16(rec + 14); + u64 gh = get_unaligned_le16(rec + 16); + + if (!sx || !sy || sx > 1024 || sy > 1024 || + gw < 2 || gh < 2 || tab + 2 * gw * gh > lsc_size || + ox + (gw - 1) * sx > 16384 || oy + (gh - 1) * sy > 16384) + return false; + } + + norm = 250U << (lsc->bit_depth - 8); + min_meas = DIV_ROUND_CLOSEST_ULL(LSC_ONE, norm); + cx2 = (s64)plane_w + 1; + cy2 = (s64)plane_h + 1 + 2LL * (centre_dy / 2); + + if (p->ideal_pct < 100) { + u64 p_q56 = div_u64((u64)p->ideal_pct << 56, 100); + u64 sqrt_p = int_sqrt64(p_q56); + + if (!sqrt_p) + return false; + k_q30 = div64_u64(1ULL << 58, sqrt_p) - (1ULL << 30); + } + + for (t = 0; t < 4; t++) { + void *rec = data + sec + 0xa + 18 * t; + u32 sx = get_unaligned_le16(rec + 2); + u32 sy = get_unaligned_le16(rec + 4); + u32 ox = get_unaligned_le16(rec + 10); + u32 oy = get_unaligned_le16(rec + 12); + u32 gw = get_unaligned_le16(rec + 14); + u32 gh = get_unaligned_le16(rec + 16); + void *tab = data + sec + 0x54 + 2 * get_unaligned_le16(rec + 6); + u64 rmax2 = 0; + u16 ch_max = 0; + + for (u32 iy = 0; iy < gh; iy++) { + s64 dy = 2LL * (oy + iy * sy) - cy2; + + for (u32 ix = 0; ix < gw; ix++) { + s64 dx = 2LL * (ox + ix * sx) - cx2; + u64 r2 = (u64)(dx * dx + dy * dy); + + rmax2 = max(rmax2, r2); + } + } + + for (u32 iy = 0; iy < gh; iy++) { + u32 py = oy + iy * sy; + s64 dy = 2LL * py - cy2; + int uy = lsc->num_y; + + for (int j = 0; j < lsc->num_y; j++) { + if (((s64)py << LSC_Q) < yq[j] * plane_h) { + uy = j; + break; + } + } + + for (u32 ix = 0; ix < gw; ix++) { + u32 px = ox + ix * sx; + s64 dx = 2LL * px - cx2; + u64 r2 = (u64)(dx * dx + dy * dy); + u64 ideal_q24 = LSC_ONE; + s64 meas; + int ux = lsc->num_x; + u16 gain; + + for (int i = 0; i < lsc->num_x; i++) { + if (((s64)px << LSC_Q) < xq[i] * plane_w) { + ux = i; + break; + } + } + + if (k_q30 && rmax2) { + u64 denom = (1ULL << 30) + + div64_u64(r2 * k_q30, rmax2); + u64 denom2 = (denom * denom) >> 30; + + ideal_q24 = div64_u64(1ULL << 54, denom2); + } + + meas = isp_lsc_sample(lsc, t, xq, yq, plane_w, + plane_h, px, py, ux, uy, + p->flip_permodule, norm); + + meas = clamp_t(s64, meas, min_meas, LSC_ONE); + gain = min_t(u64, div64_u64((ideal_q24 << 12) + (meas >> 1), + meas), + p->max_gain); + put_unaligned_le16(gain, tab + 2 * (iy * gw + ix)); + ch_max = max(ch_max, gain); + } + } + put_unaligned_le16(ch_max, rec); + } + + /* Legacy ISP generations average the Gr (ch 0) and Gb (ch 3) shading planes. */ + if (p->avg_green) { + void *rec0 = data + sec + 0xa; + void *rec3 = data + sec + 0xa + 18 * 3; + u32 gw = get_unaligned_le16(rec0 + 14); + u32 gh = get_unaligned_le16(rec0 + 16); + + if (gw == get_unaligned_le16(rec3 + 14) && + gh == get_unaligned_le16(rec3 + 16)) { + void *gr = data + sec + 0x54 + 2 * get_unaligned_le16(rec0 + 6); + void *gb = data + sec + 0x54 + 2 * get_unaligned_le16(rec3 + 6); + u16 avg_max = 0; + + for (u32 i = 0; i < gw * gh; i++) { + u16 avg = ((u32)get_unaligned_le16(gr + 2 * i) + + get_unaligned_le16(gb + 2 * i)) >> 1; + + put_unaligned_le16(avg, gr + 2 * i); + put_unaligned_le16(avg, gb + 2 * i); + avg_max = max(avg_max, avg); + } + put_unaligned_le16(avg_max, rec0); + put_unaligned_le16(avg_max, rec3); + } + } + + if (flag_sec && flag_size >= 5) + *((u8 *)data + flag_sec + 4) = 1; + + dev_dbg(isp->dev, "setfile: applied per-module LSC (%ux%u, ideal %u%%, flip %u)\n", + lsc->num_x, lsc->num_y, p->ideal_pct, p->flip_permodule); + return true; +} + +/* + * Section 0xc021, when present (e.g. 1925_03XX), holds a Q12 differential + * shading multiplier table of the same size as section 0xc003. + */ +static void isp_setfile_apply_dsc(void *data, size_t len) +{ + u32 lsc_size, dsc_size; + size_t lsc = isp_setfile_find(data, len, 0xc003, &lsc_size); + size_t dsc = isp_setfile_find(data, len, 0xc021, &dsc_size); + u32 count; + + if (!lsc || !dsc || lsc_size < 0x54 || dsc_size < 0x54) + return; + + count = get_unaligned_le16(data + lsc + 0x52); + if (!count || count != get_unaligned_le16(data + dsc + 0x52) || + 0x54 + 2ULL * count > lsc_size || 0x54 + 2ULL * count > dsc_size) + return; + + for (u32 i = 0; i < count; i++) { + u32 a = get_unaligned_le16(data + lsc + 0x54 + 2 * i); + u32 b = get_unaligned_le16(data + dsc + 0x54 + 2 * i); + u32 v = DIV_ROUND_CLOSEST(a * b, SNF_ONE); + + put_unaligned_le16(min_t(u32, v, U16_MAX), + data + lsc + 0x54 + 2 * i); + } + for (int t = 0; t < 4; t++) { + void *rec = data + lsc + 0xa + 18 * t; + u64 off = get_unaligned_le16(rec + 6); + u64 n = (u64)get_unaligned_le16(rec + 14) * + get_unaligned_le16(rec + 16); + u16 ch_max = 0; + + if (off + n > count) + continue; + for (u64 i = 0; i < n; i++) + ch_max = max(ch_max, get_unaligned_le16(data + lsc + 0x54 + + 2 * (off + i))); + put_unaligned_le16(ch_max, rec); + } +} + static int isp_ch_load_setfile(struct apple_isp *isp, u32 ch) { struct isp_format *fmt = isp_get_format(isp, ch); @@ -937,6 +1272,12 @@ static int isp_ch_load_setfile(struct apple_isp *isp, u32 ch) if (isp->hw->gen == ISP_GEN_T6040 || isp->hw->setfile_stride_fixup) isp_setfile_fixup_h17(isp->data_surf->virt, len); + if (isp->lsc.valid && lsc_params.ideal_pct && + isp_setfile_apply_lsc(isp, isp->data_surf->virt, len, + &isp->lsc, &lsc_params, + setfile->snf_centre_dy)) + isp_setfile_apply_dsc(isp->data_surf->virt, len); + isp_setfile_snf_radial(isp, isp->data_surf->virt, len, setfile->snf_centre_dy);