diff --git a/Documentation/devicetree/bindings/media/apple,t8140-isp.yaml b/Documentation/devicetree/bindings/media/apple,t8140-isp.yaml index f06c6998e2dc5..f66d9145a2574 100644 --- a/Documentation/devicetree/bindings/media/apple,t8140-isp.yaml +++ b/Documentation/devicetree/bindings/media/apple,t8140-isp.yaml @@ -98,10 +98,27 @@ properties: camera streams whether or not the indicator is shown. The indicator is software, not a privacy guarantee. + apple,module-tuning: + $ref: /schemas/types.yaml#/definitions/string + pattern: "^[0-9]{4}_[0-9]{2}$" + description: + Name of the camera module's own tuning (setfile), loaded in place of + the sensor's default one, e.g. "2226_02" for apple/isp_2226_02XX.dat. + sensor-presets: type: object description: Sensor firmware presets and the host output geometry associated with them. + properties: + apple,lens-circle: + $ref: /schemas/types.yaml#/definitions/uint32 + minimum: 1 + maximum: 1000 + description: + Radius of the image circle the lens draws on the sensor, in per mille + of each preset's input width, about the input's centre. Outside it + the picture is black, so each crop is scaled about its centre, + aspect kept, until its corners are inside. patternProperties: "^preset[0-9]+$": type: object diff --git a/arch/arm64/boot/dts/apple/t8142-j813-isp.dtsi b/arch/arm64/boot/dts/apple/t8142-j813-isp.dtsi index 44d4d1eabeef5..f573edc9c2147 100644 --- a/arch/arm64/boot/dts/apple/t8142-j813-isp.dtsi +++ b/arch/arm64/boot/dts/apple/t8142-j813-isp.dtsi @@ -228,6 +228,12 @@ * crops the driver derived in config 7, halved. */ sensor-presets { + /* + * Measured on a J815 (2026-11-21); the J813 is assumed to have the + * same ultra-wide lens. + */ + apple,lens-circle = <470>; + /* 1280x720 */ preset0 { apple,config-index = <8>; diff --git a/arch/arm64/boot/dts/apple/t8142-j815.dts b/arch/arm64/boot/dts/apple/t8142-j815.dts index 82e73d6dafc2b..7a8a7ead3692c 100644 --- a/arch/arm64/boot/dts/apple/t8142-j815.dts +++ b/arch/arm64/boot/dts/apple/t8142-j815.dts @@ -154,6 +154,11 @@ &isp { /* J815 ADT /arm-io/isp sensor-type */ apple,platform-id = <0x72>; + /* + * The J815's IMX958 module tuning: with the sensor's default 2224_01 the + * corners read half the centre's brightness, with 2226_02 0.85. + */ + apple,module-tuning = "2226_02"; }; &aop_audio { diff --git a/drivers/media/platform/apple/isp/isp-cam-test.c b/drivers/media/platform/apple/isp/isp-cam-test.c index a98b38a16fd78..fd0ffa53b7320 100644 --- a/drivers/media/platform/apple/isp/isp-cam-test.c +++ b/drivers/media/platform/apple/isp/isp-cam-test.c @@ -312,10 +312,10 @@ static void isp_test_temporal_filter_band0(struct kunit *test) 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. */ + /* The start record is (1, 1, 0) on band-0 setfiles, as macOS 27.2 sends it. */ 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][1], 1); KUNIT_EXPECT_EQ(test, trace->word[i][2], 0); i = isp_test_find(trace, 0xc11d); KUNIT_EXPECT_EQ(test, trace->word[i][0], 0x7800); @@ -732,6 +732,55 @@ static void isp_test_sensor_names(struct kunit *test) KUNIT_EXPECT_STREQ(test, apple_isp_sensor_name(isp), "imx958-uw"); } +static void isp_test_module_tuning(struct kunit *test) +{ + struct apple_isp *isp = isp_test_capture_alloc(test); + static const char *const keep[] = { + "9999_99", /* no such setfile */ + "7523_01", /* another sensor's */ + "2224", /* not a whole name */ + }; + + /* No property: the sensor's default. */ + KUNIT_EXPECT_EQ(test, isp_module_tuning(isp, ISP_IMX958_2224_01), ISP_IMX958_2224_01); + isp->module_tuning = "2226_02"; + KUNIT_EXPECT_EQ(test, isp_module_tuning(isp, ISP_IMX958_2224_01), ISP_IMX958_2226_02); + isp->module_tuning = "2226_01"; + KUNIT_EXPECT_EQ(test, isp_module_tuning(isp, ISP_IMX958_2224_01), ISP_IMX958_2226_01); + isp->module_tuning = "2224_01"; + KUNIT_EXPECT_EQ(test, isp_module_tuning(isp, ISP_IMX958_2224_01), ISP_IMX958_2224_01); + for (unsigned int i = 0; i < ARRAY_SIZE(keep); i++) { + isp->module_tuning = keep[i]; + KUNIT_EXPECT_EQ_MSG(test, isp_module_tuning(isp, ISP_IMX958_2224_01), + ISP_IMX958_2224_01, "%s", keep[i]); + } +} + +/* + * A setfile missing from the system is replaced by its fallback after the + * size check against the data surface, so a fallback must be the same + * sensor, no larger, and final. + */ +static void isp_test_setfile_fallbacks(struct kunit *test) +{ + unsigned int n = 0; + + for (unsigned int i = 0; i < ARRAY_SIZE(isp_setfiles); i++) { + const struct isp_setfile *s = &isp_setfiles[i], *f = s->fallback; + + if (!f) + continue; + n++; + KUNIT_EXPECT_EQ_MSG(test, f->version, s->version, "setfile %u", i); + KUNIT_EXPECT_LE_MSG(test, f->size, s->size, "setfile %u", i); + KUNIT_EXPECT_NULL_MSG(test, f->fallback, "setfile %u", i); + KUNIT_EXPECT_STRNEQ_MSG(test, f->path, s->path, "setfile %u", i); + } + KUNIT_EXPECT_EQ(test, n, 2); + KUNIT_EXPECT_PTR_EQ(test, isp_setfiles[ISP_IMX958_2226_02].fallback, + &isp_setfiles[ISP_IMX958_2224_01]); +} + static void isp_test_snf_flatten(u8 *snf) { int k; @@ -1019,6 +1068,8 @@ static struct kunit_case isp_cam_test_cases[] = { KUNIT_CASE(isp_test_stop_key_on_debug), KUNIT_CASE(isp_test_sensor_geometry), KUNIT_CASE(isp_test_sensor_names), + KUNIT_CASE(isp_test_module_tuning), + KUNIT_CASE(isp_test_setfile_fallbacks), KUNIT_CASE(isp_test_sensor_config_list), KUNIT_CASE(isp_test_sensor_config_failure), KUNIT_CASE(isp_test_preview_stream), diff --git a/drivers/media/platform/apple/isp/isp-cam.c b/drivers/media/platform/apple/isp/isp-cam.c index 544a347c7c90a..a6ea3dec8d3b8 100644 --- a/drivers/media/platform/apple/isp/isp-cam.c +++ b/drivers/media/platform/apple/isp/isp-cam.c @@ -41,6 +41,8 @@ struct isp_setfile { * noise filter gains. The setfile does not carry it. */ int snf_centre_dy; + /* used instead when this file is not installed (module tunings missing from vendorfw) */ + const struct isp_setfile *fallback; }; // clang-format off @@ -97,11 +99,76 @@ static const struct isp_setfile isp_setfiles[] = { [ISP_IMX913_7523_01] = {0x913, "imx913", 0x75230107, "apple/isp_7523_01XX.dat", 0x247f8}, [ISP_IMX958_2224_01] = {0x958, "imx958-uw", 0x22240106, "apple/isp_2224_01XX.dat", 0x6ff4, 0, ISP_TEMPORAL_FILTER_BAND0, -156}, + /* + * J815's own module tunings, H17-native (lens-shading records without + * the xx24 stride). The optical centre is 2224_01's: same sensor, not + * measured for this module. + */ + [ISP_IMX958_2226_01] = {0x958, "imx958-uw", 0x22260101, "apple/isp_2226_01XX.dat", 0x7040, + 0, ISP_TEMPORAL_FILTER_BAND0, -156, + &isp_setfiles[ISP_IMX958_2224_01]}, + [ISP_IMX958_2226_02] = {0x958, "imx958-uw", 0x22260201, "apple/isp_2226_02XX.dat", 0x7000, + 0, ISP_TEMPORAL_FILTER_BAND0, -156, + &isp_setfiles[ISP_IMX958_2224_01]}, [ISP_VD56G0_6221_01] = {0xd56, "vd56g0", 0x62210102, "apple/isp_6221_01XX.dat", 0x1b80}, [ISP_VD56G0_6222_01] = {0xd56, "vd56g0", 0x62220102, "apple/isp_6222_01XX.dat", 0x1b80}, }; // clang-format on +/* + * What macOS 27.2 sends the same H17 firmware on the J815, from a hypervisor + * IPC trace (2026-11-20): + * - the green camera LED's brightness, 529 at each start in a lit room (only + * on T8142, where it was traced: hw->led_brightness). It is clamped to the + * range the firmware reports (CH_STATUS_LED_THR_GET, 204..1023 on the + * J815), so the privacy LED can never be set dark; + * - AE stability 16 / to-stable 8 (other firmware: 32 / 20) and an AE gain + * cap of 96.0; + * - on band-0 setfiles, TNR started with (1, 1, 0) and high strength on. + * macOS ramps the LED with ambient light; the driver keeps the start value. + */ + +static void isp_ch_configure_led(struct apple_isp *isp, u32 ch) +{ + u16 low, high, value; + int err; + + if (!isp->hw->led_brightness) + return; + err = isp_cmd_ch_status_led_thr_get(isp, ch, &low, &high); + if (err || !low || high < low) { + dev_warn(isp->dev, "camera LED: no brightness range (%d, %u..%u): left as is\n", + err, low, high); + return; + } + value = clamp_t(unsigned int, isp->hw->led_brightness, low, high); + err = isp_cmd_ch_status_led_mbrightness_set(isp, ch, value); + dev_info(isp->dev, "camera LED brightness %u (range %u..%u)%s\n", value, + low, high, err ? ": firmware refused" : ""); +} + +/* + * The board's camera module tuning, named in the DT (apple,module-tuning, + * e.g. "2226_02"), in place of the sensor's default setfile. Unknown names + * and other sensors' setfiles keep the default. + */ +static enum isp_sensor_id isp_module_tuning(struct apple_isp *isp, enum isp_sensor_id def) +{ + char path[48]; + unsigned int i; + + if (!isp->module_tuning) + return def; + snprintf(path, sizeof(path), "apple/isp_%sXX.dat", isp->module_tuning); + for (i = 0; i < ARRAY_SIZE(isp_setfiles); i++) + if (isp_setfiles[i].path && isp_setfiles[i].version == isp_setfiles[def].version && + !strcmp(isp_setfiles[i].path, path)) + return i; + dev_warn(isp->dev, "module tuning %s: no such setfile for sensor 0x%x, using %s\n", + isp->module_tuning, isp_setfiles[def].version, isp_setfiles[def].path); + return def; +} + static int isp_ch_get_sensor_id(struct apple_isp *isp, u32 ch) { struct isp_format *fmt = isp_get_format(isp, ch); @@ -187,7 +254,7 @@ static int isp_ch_get_sensor_id(struct apple_isp *isp, u32 ch) break; case 0x958: /* Section 0x50f of 2224 names sensor 0x958; 2924's names 0x614. */ - id = ISP_IMX958_2224_01; + id = isp_module_tuning(isp, ISP_IMX958_2224_01); break; case 0xd56: id = ISP_VD56G0_6221_01; @@ -495,7 +562,8 @@ static int isp_ch_cache_sensor_info(struct apple_isp *isp, u32 ch) if (err || (fmt->id != ISP_IMX248_1820_01 && fmt->id != ISP_IMX558_1921_01 && fmt->id != ISP_IMX558_1925_03 && fmt->id != ISP_IMX364_8720_01 && - fmt->id != ISP_IMX958_2224_01)) { + fmt->id != ISP_IMX958_2224_01 && fmt->id != ISP_IMX958_2226_01 && + fmt->id != ISP_IMX958_2226_02)) { dev_err(isp->dev, "ch %d: unsupported sensor. Please file a bug report with hardware info & dmesg trace.\n", ch); @@ -821,6 +889,13 @@ static int isp_ch_load_setfile(struct apple_isp *isp, u32 ch) return -ENOSPC; err = firmware_request_nowarn(&fw, setfile->path, isp->dev); + if (err == -ENOENT && setfile->fallback) { + dev_info_once(isp->dev, "setfile '%s' not installed, using '%s'\n", + setfile->path, setfile->fallback->path); + setfile = setfile->fallback; + len = setfile->file_size ?: setfile->size; + err = firmware_request_nowarn(&fw, setfile->path, isp->dev); + } if (err) { if (err == -ENOENT) dev_warn_once(isp->dev, @@ -929,11 +1004,12 @@ static int isp_ch_configure_temporal_filter(struct apple_isp *isp, u32 ch) 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); + /* (1, 1, 0) and high strength, as macOS 27.2 */ + err = isp_cmd_apple_ch_tnr_start_h17(isp, ch, true); if (err) return err; - err = isp_cmd_apple_ch_tnr_high_strength_set(isp, ch, 0); + err = isp_cmd_apple_ch_tnr_high_strength_set(isp, ch, true); if (err) return err; @@ -1034,6 +1110,9 @@ static int isp_ch_configure_capture(struct apple_isp *isp, u32 ch) if (err) return err; + if (!isp_is_p010(isp)) + isp_ch_configure_led(isp, ch); + err = isp_cmd_ch_buffer_recycle_mode_set( isp, ch, CISP_BUFFER_RECYCLE_MODE_EMPTY_ONLY); if (err) @@ -1105,14 +1184,22 @@ static int isp_ch_configure_capture(struct apple_isp *isp, u32 ch) if (err) return err; - err = isp_cmd_ch_ae_stability_set(isp, ch, 32); + err = isp_cmd_ch_ae_stability_set(isp, ch, h17 ? 16 : 32); if (err) return err; - err = isp_cmd_ch_ae_stability_to_stable_set(isp, ch, 20); + err = isp_cmd_ch_ae_stability_to_stable_set(isp, ch, + h17 ? 8 : 20); if (err) return err; + /* 96.0 in 8.8 fixed point, the gain cap macOS sets */ + if (h17) { + err = isp_cmd_ch_ae_gain_cap_set(isp, ch, 96 << 8); + if (err) + return err; + } + err = isp_cmd_ch_sif_pixel_format_set(isp, ch); if (err) return err; diff --git a/drivers/media/platform/apple/isp/isp-cmd.c b/drivers/media/platform/apple/isp/isp-cmd.c index 7fb5203e71aea..1e1436057a8b3 100644 --- a/drivers/media/platform/apple/isp/isp-cmd.c +++ b/drivers/media/platform/apple/isp/isp-cmd.c @@ -655,12 +655,13 @@ int isp_cmd_apple_ch_temporal_filter_disable(struct apple_isp *isp, u32 chan) return CISP_SEND_IN(isp, args); } -int isp_cmd_apple_ch_tnr_start_h17(struct apple_isp *isp, u32 chan) +int isp_cmd_apple_ch_tnr_start_h17(struct apple_isp *isp, u32 chan, u32 unk_10) { struct cmd_apple_ch_temporal_filter_start_h17 args = { .opcode = CISP_OPCODE(CISP_CMD_APPLE_CH_TEMPORAL_FILTER_START), .chan = chan, .unk_c = 1, + .unk_10 = unk_10, /* macOS 27.2 on the J815: 1 */ }; return CISP_SEND_IN(isp, args); } @@ -846,3 +847,41 @@ int isp_cmd_ch_property_read(struct apple_isp *isp, u32 chan, u32 prop, u32 *val return ret; } + +int isp_cmd_ch_ae_gain_cap_set(struct apple_isp *isp, u32 chan, u32 gain) +{ + struct cmd_ch_ae_gain_cap_set args = { + .opcode = CISP_OPCODE(CISP_CMD_CH_AE_GAIN_CAP_SET), + .chan = chan, + .gain = gain, + }; + return CISP_SEND_IN(isp, args); +} + +int isp_cmd_ch_status_led_mbrightness_set(struct apple_isp *isp, u32 chan, + u16 brightness) +{ + struct cmd_ch_status_led_mbrightness_set args = { + .opcode = CISP_OPCODE(CISP_CMD_CH_STATUS_LED_MBRIGHTNESS_SET), + .chan = chan, + .brightness = brightness, + }; + return CISP_SEND_IN(isp, args); +} + +int isp_cmd_ch_status_led_thr_get(struct apple_isp *isp, u32 chan, u16 *low, + u16 *high) +{ + struct cmd_ch_status_led_thr_get args = { + .opcode = CISP_OPCODE(CISP_CMD_CH_STATUS_LED_THR_GET), + .chan = chan, + }; + int err = CISP_SEND_OUT(isp, &args); + + if (err) + return err; + *low = args.low; + *high = args.high; + return 0; +} + diff --git a/drivers/media/platform/apple/isp/isp-cmd.h b/drivers/media/platform/apple/isp/isp-cmd.h index ab98f3447d6f7..414725b04df91 100644 --- a/drivers/media/platform/apple/isp/isp-cmd.h +++ b/drivers/media/platform/apple/isp/isp-cmd.h @@ -55,10 +55,13 @@ #define CISP_CMD_CH_AE_FRAME_RATE_MIN_SET 0x020a #define CISP_CMD_CH_AE_STABILITY_SET 0x021a #define CISP_CMD_CH_AE_STABILITY_TO_STABLE_SET 0x0229 +#define CISP_CMD_CH_AE_GAIN_CAP_SET 0x020c #define CISP_CMD_CH_SENSOR_NVM_GET 0x0501 #define CISP_CMD_CH_SENSOR_PERMODULE_LSC_INFO_GET 0x0507 #define CISP_CMD_CH_SENSOR_PERMODULE_LSC_GRID_GET 0x0511 #define CISP_CMD_CH_LPDP_HS_RECEIVER_TUNING_SET 0x051b +#define CISP_CMD_CH_STATUS_LED_MBRIGHTNESS_SET 0x060f +#define CISP_CMD_CH_STATUS_LED_THR_GET 0x0632 #define CISP_CMD_CH_FOCUS_LIMITS_GET 0x0701 #define CISP_CMD_CH_CROP_GET 0x0800 #define CISP_CMD_CH_CROP_SET 0x0801 @@ -726,7 +729,7 @@ int isp_cmd_apple_ch_motion_history_start(struct apple_isp *isp, u32 chan); int isp_cmd_apple_ch_motion_history_stop(struct apple_isp *isp, u32 chan); int isp_cmd_apple_ch_temporal_filter_enable(struct apple_isp *isp, u32 chan); int isp_cmd_apple_ch_temporal_filter_disable(struct apple_isp *isp, u32 chan); -int isp_cmd_apple_ch_tnr_start_h17(struct apple_isp *isp, u32 chan); +int isp_cmd_apple_ch_tnr_start_h17(struct apple_isp *isp, u32 chan, u32 unk_10); int isp_cmd_apple_ch_tnr_high_strength_set(struct apple_isp *isp, u32 chan, u32 enable); int isp_cmd_apple_ch_tnr_band0_mod_set(struct apple_isp *isp, u32 chan, @@ -831,4 +834,40 @@ int isp_cmd_ch_semantic_video_enable(struct apple_isp *isp, u32 chan, u32 enable); int isp_cmd_ch_semantic_awb_enable(struct apple_isp *isp, u32 chan, u32 enable); +/* + * Commands macOS 27.2 sends on the J815 (IMX958), named as the firmware + * prints them in its log when it receives them; layouts from the payloads + * in the hypervisor trace of 2026-11-20. + */ +struct cmd_ch_ae_gain_cap_set { + u64 opcode; + u32 chan; + u32 gain; /* 8.8 fixed point; macOS: 96.0 */ +} __packed; +static_assert(sizeof(struct cmd_ch_ae_gain_cap_set) == 0x10); + +/* The firmware reads the value as a u16 at 0xc. */ +struct cmd_ch_status_led_mbrightness_set { + u64 opcode; + u32 chan; + u16 brightness; + u16 reserved; +} __packed; +static_assert(sizeof(struct cmd_ch_status_led_mbrightness_set) == 0x10); + +/* Reply: the range the brightness may take (J815: 204..1023). */ +struct cmd_ch_status_led_thr_get { + u64 opcode; + u32 chan; + u16 low; + u16 high; +} __packed; +static_assert(sizeof(struct cmd_ch_status_led_thr_get) == 0x10); + +int isp_cmd_ch_ae_gain_cap_set(struct apple_isp *isp, u32 chan, u32 gain); +int isp_cmd_ch_status_led_mbrightness_set(struct apple_isp *isp, u32 chan, + u16 brightness); +int isp_cmd_ch_status_led_thr_get(struct apple_isp *isp, u32 chan, u16 *low, + u16 *high); + #endif /* __ISP_CMD_H__ */ diff --git a/drivers/media/platform/apple/isp/isp-drv.c b/drivers/media/platform/apple/isp/isp-drv.c index 758200422d68d..0d98b44c574f6 100644 --- a/drivers/media/platform/apple/isp/isp-drv.c +++ b/drivers/media/platform/apple/isp/isp-drv.c @@ -627,10 +627,70 @@ static int isp_of_read_coord(struct device *dev, struct device_node *np, return 0; } +/* + * A camera property of the sensor-presets node (DT), optional. The presets + * keep the DT's binned mode: on the J815 its noise was 1.29 against about + * 3.0 for full resolution (2026-11-21). + * + * apple,lens-circle = : the lens draws an image circle smaller than + * the sensor, black outside. macOS never shows that part (it frames a 1.5x + * zoom and dewarps); crops that take the full sensor width show black + * corners. On a J815 (config 8 frames, 2026-11-21) the 1520x856 16:9 crop + * went dark at 0.855 and 0.895 of its half-diagonal at the bottom corners and + * 0.96 at the top: the circle's centre sits about 5 % of the height above + * the sensor's, radius ~0.53 of the width. The radius is per mille of the + * input width, about the input's centre. A crop whose corners leave the + * circle is scaled about its centre, aspect kept, until they fit. + */ +static bool isp_crop_in_circle(s64 ox, s64 oy, u32 w, u32 h, s64 r) +{ + s64 dx = abs(ox) + w / 2, dy = abs(oy) + h / 2; + + return dx * dx + dy * dy <= r * r; +} + +static void isp_preset_fit_lens(struct device *dev, struct isp_preset *p, u32 lens_circle) +{ + u32 in_w = p->input_dim.x, in_h = p->input_dim.y; + u32 w = p->crop_size.x, h = p->crop_size.y, lo = 0, hi = 1024, s, nw, nh; + s64 r, ccx, ccy, ox, oy; + + if (!lens_circle) + return; + r = (s64)lens_circle * in_w / 1000; + ccx = in_w / 2; + ccy = in_h / 2; + /* crop centre relative to the circle's, doubled to stay integer */ + ox = 2 * (s64)p->crop_offset.x + w - 2 * ccx; + oy = 2 * (s64)p->crop_offset.y + h - 2 * ccy; + if (isp_crop_in_circle(ox, oy, 2 * w, 2 * h, 2 * r)) + return; + /* largest scale s/1024 of the crop whose corners are inside */ + while (hi - lo > 1) { + s = (lo + hi) / 2; + if (isp_crop_in_circle(ox, oy, 2 * w * s / 1024, 2 * h * s / 1024, 2 * r)) + lo = s; + else + hi = s; + } + nw = round_down(w * lo / 1024, 2); + nh = round_down(h * lo / 1024, 2); + if (nw < 64 || nh < 64) + return; + dev_info(dev, "%ux%u preset: crop %ux%u+%u+%u -> %ux%u (inside the lens circle)\n", + p->output_dim.x, p->output_dim.y, w, h, p->crop_offset.x, + p->crop_offset.y, nw, nh); + p->crop_offset.x = round_down(p->crop_offset.x + (w - nw) / 2, 2); + p->crop_offset.y = round_down(p->crop_offset.y + (h - nh) / 2, 2); + p->crop_size.x = nw; + p->crop_size.y = nh; +} + static int apple_isp_init_presets(struct apple_isp *isp) { struct device *dev = isp->dev; struct isp_preset *preset; + u32 lens_circle; int err = 0; struct device_node *np __free(device_node) = @@ -651,6 +711,9 @@ static int apple_isp_init_presets(struct apple_isp *isp) if (!isp->presets) return -ENOMEM; + if (of_property_read_u32(np, "apple,lens-circle", &lens_circle)) + lens_circle = 0; + preset = isp->presets; for_each_child_of_node_scoped(np, child) { u32 xywh[4]; @@ -681,6 +744,8 @@ static int apple_isp_init_presets(struct apple_isp *isp) preset->crop_size.x = xywh[2]; preset->crop_size.y = xywh[3]; + isp_preset_fit_lens(dev, preset, lens_circle); + preset++; } @@ -876,6 +941,10 @@ static int apple_isp_probe(struct platform_device *pdev) if (err) isp->temporal_filter = 0; + /* the camera module's own tuning, where the board names one */ + of_property_read_string(dev->of_node, "apple,module-tuning", + &isp->module_tuning); + /* the sensor connection layout, for firmware that takes it at boot */ of_property_read_u32(dev->of_node, "apple,cam-connections-scheme", &isp->cam_connections_scheme); @@ -1530,6 +1599,7 @@ static const struct apple_isp_hw apple_isp_hw_t8142 = { .coproc_quiesce = ISP_COPROC_QUIESCE_T8140, .fw_mmio = apple_isp_fw_mmio_t8142, .num_fw_mmio = ARRAY_SIZE(apple_isp_fw_mmio_t8142), + .led_brightness = 529, .boot_mode = 1, .capture_meta_size = ISP_CAPTURE_META_SIZE_T8140, .resident_fw = true, diff --git a/drivers/media/platform/apple/isp/isp-drv.h b/drivers/media/platform/apple/isp/isp-drv.h index 411041d8e82fc..652412132a240 100644 --- a/drivers/media/platform/apple/isp/isp-drv.h +++ b/drivers/media/platform/apple/isp/isp-drv.h @@ -161,6 +161,8 @@ struct isp_preset { struct apple_isp_hw { enum isp_generation gen; + /* camera LED brightness at each start (macOS's value), 0 = firmware default */ + u16 led_brightness; enum isp_fw_abi fw_abi; u64 pmu_base; @@ -334,6 +336,8 @@ enum isp_sensor_id { ISP_IMX853_7622_01, ISP_IMX913_7523_01, ISP_IMX958_2224_01, + ISP_IMX958_2226_01, /* the J815's macOS module tunings */ + ISP_IMX958_2226_02, ISP_VD56G0_6221_01, ISP_VD56G0_6222_01, }; @@ -357,6 +361,8 @@ struct isp_format { size_t total_size; }; +struct dentry; + struct apple_isp { struct device *dev; const struct apple_isp_hw *hw; @@ -369,6 +375,8 @@ struct apple_isp { enum isp_firmware_version fw_compat; u32 platform_id; u32 temporal_filter; + /* setfile name of the board's camera module (DT apple,module-tuning), or NULL */ + const char *module_tuning; u32 cam_connections_scheme; struct isp_preset *presets; int num_presets; @@ -439,6 +447,12 @@ struct apple_isp { /* firmware that starts again, idling until fw_idle_work stops it */ bool fw_idle; struct delayed_work fw_idle_work; + /* + * Firmware that crashed cannot power the camera module down (its + * status light stays on): it is started once more and stopped cleanly. + */ + struct delayed_work fw_recover_work; + bool fw_recovering; /* the camera was detected; until then a deferred start is pending */ bool camera_detected; /* configurations the firmware has, once a deferred start detected them */ diff --git a/drivers/media/platform/apple/isp/isp-fw.c b/drivers/media/platform/apple/isp/isp-fw.c index c9f65aeb10105..8bba35d530829 100644 --- a/drivers/media/platform/apple/isp/isp-fw.c +++ b/drivers/media/platform/apple/isp/isp-fw.c @@ -1424,14 +1424,46 @@ static void apple_isp_fw_idle_work(struct work_struct *work) mutex_unlock(&isp->video_lock); } +/* + * A crashed firmware was forced off (an ISP assert, 2026-11-21: the stop + * command timed out, "coproc not in WFI"), so it never sent the camera module + * its power-down: the sensor stayed powered and its green light on until the + * next camera use. Firmware that starts again (hw->fw_restart) is booted once + * more and stopped the normal way, which powers the module down. Only once: + * a second crash is left as it is. + */ +static void apple_isp_fw_recover_work(struct work_struct *work) +{ + struct apple_isp *isp = container_of(to_delayed_work(work), + struct apple_isp, fw_recover_work); + int err; + + mutex_lock(&isp->video_lock); + if (isp->fw_state == ISP_FW_OFF) { + isp->fw_recovering = true; + err = apple_isp_firmware_boot(isp); + if (!err) + apple_isp_firmware_halt(isp); + isp->fw_recovering = false; + if (err) + dev_warn(isp->dev, "crashed firmware: restart to power the camera down failed (%d); the camera light may stay on until the next camera start\n", + err); + else + dev_info(isp->dev, "crashed firmware: restarted and stopped it to power the camera down\n"); + } + mutex_unlock(&isp->video_lock); +} + void apple_isp_firmware_init(struct apple_isp *isp) { INIT_DELAYED_WORK(&isp->fw_idle_work, apple_isp_fw_idle_work); + INIT_DELAYED_WORK(&isp->fw_recover_work, apple_isp_fw_recover_work); } /* Stop idling firmware now, and wait for a stop that was due. */ void apple_isp_firmware_flush(struct apple_isp *isp) { + cancel_delayed_work_sync(&isp->fw_recover_work); if (!isp->hw->fw_idle_ms) return; @@ -1445,16 +1477,25 @@ void apple_isp_firmware_flush(struct apple_isp *isp) /* Stop the firmware; resident firmware without hw->fw_restart stays down. */ void apple_isp_firmware_halt(struct apple_isp *isp) { + bool crashed; + /* Only a capture services the watchdog, but be sure. */ apple_isp_wdt_stop(isp); if (isp->fw_state != ISP_FW_RUNNING) return; + /* Before the shutdown, which marks every stopped firmware as failed. */ + crashed = READ_ONCE(isp->ipc_error) && isp->hw->fw_restart && + !isp->fw_recovering; + isp_firmware_shutdown(isp); pm_runtime_put_sync(isp->dev); isp->ipc_stale = NULL; isp->fw_idle = false; isp->fw_state = isp->hw->resident_fw && !isp->hw->fw_restart ? ISP_FW_DEAD : ISP_FW_OFF; + if (crashed) + mod_delayed_work(system_dfl_wq, &isp->fw_recover_work, + msecs_to_jiffies(500)); } diff --git a/tools/asahi/isp-preview-flow-tests.py b/tools/asahi/isp-preview-flow-tests.py index 4e06b21f67d32..1ea0bc2d93d82 100644 --- a/tools/asahi/isp-preview-flow-tests.py +++ b/tools/asahi/isp-preview-flow-tests.py @@ -18,7 +18,7 @@ struct preset {int index;struct coord crop_offset,crop_size,output_dim;}; struct isp_format {struct preset *preset;int strides[2];}; struct hw {int fw_abi;bool lpdp;}; -struct apple_isp {struct hw *hw;int temporal_filter;}; +struct apple_isp {void *dev;struct hw *hw;int temporal_filter;}; static struct preset preset; static struct isp_format fmt={.preset=&preset}; static struct isp_format *isp_get_format(struct apple_isp *i,u32 ch){return &fmt;} @@ -35,6 +35,9 @@ effect={'isp_cmd_apple_ch_temporal_filter_start':'temporal_start++;','isp_cmd_apple_ch_motion_history_start':'history++;','isp_cmd_apple_ch_temporal_filter_enable':'enable++;','isp_cmd_apple_ch_temporal_filter_disable':'disable++;if(fail_disable)return -EIO;','isp_ch_configure_pools':'pools++;'}.get(name,'') code+='static int '+name+'(struct apple_isp *i,...){'+effect+'return 0;}\n' code += 'static bool isp_is_p010(struct apple_isp *i){return false;}\n' +# Knobs and helpers outside the order checked here (macOS stream settings, camera LED). +code += 'static void isp_ch_configure_led(struct apple_isp *i,u32 ch){}\n' +code += '#define dev_warn(dev, ...) ((void)(dev))\n' code += 'static int isp_ch_configure_temporal_filter(struct apple_isp *i,u32 ch){temporal_start++;history++;enable++;return 0;}\n' code+=body+r''' int main(void){ diff --git a/tools/asahi/isp/session.c b/tools/asahi/isp/session.c index ddf524257c135..1c2005438f1bc 100644 --- a/tools/asahi/isp/session.c +++ b/tools/asahi/isp/session.c @@ -69,6 +69,9 @@ struct apple_isp { bool ipc_quiescing; bool fw_idle; struct delayed_work fw_idle_work; + /* Restart after a firmware crash (only with fw_restart; never here). */ + struct delayed_work fw_recover_work; + bool fw_recovering; struct isp_channel *chan_io, *chan_sm, *chan_tm, *chan_bt; struct isp_channel *ipc_stale; bool ipc_failed; @@ -317,6 +320,7 @@ static unsigned int idle_schedules; static unsigned int msecs_to_jiffies(unsigned int ms) { return ms; } static bool mod_delayed_work(void *wq, struct delayed_work *work, unsigned int delay) { + /* The crash recovery is scheduled only for fw_restart firmware. */ CHECK(wq == system_dfl_wq && delay == 1000); CHECK(!irq_disabled && !synchronizing->ipc_quiescing); work->scheduled++;