// SPDX-License-Identifier: GPL-2.0-only /* * Qualcomm MSM8905 / Nokia 8110 4G (nokia-argon) Rear Camera V4L2 Driver * * Drives the GalaxyCore GC2395 (TA-1048), GC2385 (TA-1059), and GC2365 2MP * MIPI CSI-2 sensors on BLSP1 I2C3 (GPIO 29/30) * through MSM8909 CAMSS (CSIPHY0 -> CSID0 -> ISPIF RDI0 -> VFE0 WM0 -> VBIF) * and exposes /dev/video0 (800x600 RGB24 V4L2 capture device). */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define MSM8905_GCC_PHYS 0x01800000UL #define MSM8905_GCC_SIZE 0x00080000UL #define MSM8905_TLMM_PHYS 0x01000000UL #define MSM8905_TLMM_SIZE 0x00070000UL #define MSM8905_CAMSS_PHYS 0x01b00000UL #define MSM8905_CAMSS_SIZE 0x00020000UL #define MSM8905_VBIF_PHYS 0x01b40000UL #define MSM8905_VBIF_SIZE 0x00001000UL #define CAM_RAW_WIDTH 1600U #define CAM_RAW_HEIGHT 1200U #define CAM_RAW_STRIDE 2000U #define CAM_RAW_FRAME_BYTES (CAM_RAW_STRIDE * CAM_RAW_HEIGHT) #define CAM_DMA_ALLOC_BYTES 0x00280000UL #define CAM_OUT_WIDTH 800U #define CAM_OUT_HEIGHT 600U #define CAM_OUT_BPP 3U #define CAM_OUT_STRIDE (CAM_OUT_WIDTH * CAM_OUT_BPP) #define CAM_OUT_FRAME_BYTES (CAM_OUT_STRIDE * CAM_OUT_HEIGHT) #define CAM_MAX_MMAP_BUFS 4U /* PM8909 USID 1 WCD analog codec micbias1 (2.8V) and earpiece PA (+6 dB) */ #define PM8909_CDC_A_MICB_1_VAL 0xf141U #define PM8909_CDC_A_RX_EAR_CTL 0xf1c0U #define GPIO_CAM_MCLK0 26U #define GPIO_CAM_I2C_SDA 29U #define GPIO_CAM_I2C_SCL 30U #define GPIO_CAM_STANDBY 34U #define GPIO_CAM_RESET 35U #define GPIO_CAM_VDIG 58U #define CSIPHY0_BASE 0x0ac00U #define CSID0_BASE 0x08000U #define ISPIF_BASE 0x0a000U #define VFE0_BASE 0x10000U #define GC2395_CHIP_ID 0x2395U #define GC2385_CHIP_ID 0x2385U #define GC2365_CHIP_ID 0x2365U struct gc2395_reg { u8 reg; u8 val; }; /* Shared 1600x1200 RAW10 MIPI CSI-2 init table for GC2395 (TA-1048) and GC2385 (TA-1059) */ static const struct gc2395_reg gc2395_init_regs[] = { { 0xfe, 0x00 }, { 0xfe, 0x00 }, { 0xfe, 0x00 }, { 0xf2, 0x02 }, { 0xf4, 0x03 }, { 0xf7, 0x01 }, { 0xf8, 0x28 }, { 0xf9, 0x02 }, { 0xfa, 0x08 }, { 0xfc, 0x8e }, { 0xe7, 0xcc }, { 0x88, 0x03 }, { 0x03, 0x04 }, { 0x04, 0x80 }, { 0x05, 0x02 }, { 0x06, 0x86 }, { 0x07, 0x02 }, { 0x08, 0x00 }, { 0x09, 0x00 }, { 0x0a, 0x04 }, { 0x0b, 0x00 }, { 0x0c, 0x02 }, { 0x17, 0xd7 }, { 0x18, 0x02 }, { 0x19, 0x17 }, { 0x1c, 0x18 }, { 0x20, 0x73 }, { 0x22, 0xa2 }, { 0x21, 0x38 }, { 0x29, 0x20 }, { 0x2f, 0x14 }, { 0x3f, 0x40 }, { 0xcd, 0x94 }, { 0xce, 0x45 }, { 0xd1, 0x0c }, { 0xd7, 0x9b }, { 0xd8, 0x99 }, { 0xda, 0x3b }, { 0xd9, 0xb5 }, { 0xdb, 0x75 }, { 0xe3, 0x1b }, { 0xe4, 0xf8 }, { 0x40, 0x22 }, { 0x43, 0x07 }, { 0x4e, 0x00 }, { 0x4f, 0x3c }, { 0x68, 0x00 }, { 0xb0, 0x60 }, { 0xb1, 0x02 }, { 0xb2, 0x80 }, { 0xb6, 0x00 }, { 0x90, 0x01 }, { 0x92, 0x02 }, { 0x94, 0x06 }, { 0x95, 0x04 }, { 0x96, 0xb0 }, { 0x97, 0x06 }, { 0x98, 0x40 }, { 0xfe, 0x00 }, { 0xed, 0x00 }, { 0xfe, 0x03 }, { 0x01, 0x03 }, { 0x02, 0x82 }, { 0x03, 0xd0 }, { 0x04, 0x04 }, { 0x05, 0x00 }, { 0x15, 0x02 }, { 0x06, 0x80 }, { 0x11, 0x2b }, { 0x12, 0xd0 }, { 0x13, 0x07 }, { 0x15, 0x00 }, { 0x1b, 0x10 }, { 0x1c, 0x10 }, { 0x21, 0x08 }, { 0x22, 0x05 }, { 0x23, 0x13 }, { 0x24, 0x02 }, { 0x25, 0x13 }, { 0x26, 0x06 }, { 0x29, 0x06 }, { 0x2a, 0x08 }, { 0x2b, 0x06 }, { 0xfe, 0x00 }, }; /* 1600x1200 RAW10 MIPI CSI-2 init table for GC2365 */ static const struct gc2395_reg gc2365_init_regs[] = { { 0xfe, 0x00 }, { 0xfe, 0x00 }, { 0xfe, 0x00 }, { 0xf7, 0x01 }, { 0xf8, 0x0d }, { 0xf9, 0x4a }, { 0xfa, 0x80 }, { 0xfc, 0x8e }, { 0xfe, 0x00 }, { 0xfe, 0x00 }, { 0xfe, 0x00 }, { 0x88, 0x03 }, { 0xfe, 0x00 }, { 0x03, 0x04 }, { 0x04, 0x62 }, { 0x05, 0x02 }, { 0x06, 0xbc }, { 0x07, 0x02 }, { 0x08, 0x00 }, { 0x0a, 0x0a }, { 0x0c, 0x12 }, { 0x0d, 0x04 }, { 0x0e, 0xb8 }, { 0x0f, 0x06 }, { 0x10, 0x50 }, { 0x17, 0xc0 }, { 0x1b, 0x11 }, { 0x1d, 0x0f }, { 0x1f, 0x16 }, { 0x22, 0x50 }, { 0x27, 0x76 }, { 0x2b, 0x28 }, { 0x2c, 0x40 }, { 0x2e, 0x8f }, { 0x2f, 0xd5 }, { 0x32, 0x05 }, { 0x33, 0x50 }, { 0x37, 0x28 }, { 0x38, 0x50 }, { 0x39, 0x80 }, { 0x3d, 0x18 }, { 0x3e, 0x1d }, { 0x3f, 0x50 }, { 0x75, 0x8e }, { 0x76, 0x08 }, { 0x77, 0x5d }, { 0x78, 0x09 }, { 0x79, 0xb3 }, { 0x7a, 0x24 }, { 0x8a, 0x3f }, { 0xc0, 0xb0 }, { 0xc1, 0x13 }, { 0xc2, 0x00 }, { 0xc3, 0x2c }, { 0xc4, 0x4b }, { 0xe0, 0x02 }, { 0xe1, 0x02 }, { 0xe2, 0x02 }, { 0xe3, 0x02 }, { 0xe4, 0x02 }, { 0xe5, 0x02 }, { 0xe6, 0x02 }, { 0xe7, 0x02 }, { 0xe8, 0x07 }, { 0x28, 0xf0 }, { 0x90, 0x01 }, { 0x92, 0x03 }, { 0x94, 0x02 }, { 0x95, 0x04 }, { 0x96, 0xb0 }, { 0x97, 0x06 }, { 0x98, 0x40 }, { 0x18, 0x02 }, { 0x40, 0x22 }, { 0x41, 0x01 }, { 0x43, 0x07 }, { 0x4c, 0x00 }, { 0x4d, 0x03 }, { 0x4e, 0x3c }, { 0x4f, 0x3c }, { 0x60, 0x00 }, { 0x61, 0x80 }, { 0x84, 0x00 }, { 0x85, 0x20 }, { 0x87, 0x30 }, { 0xa0, 0x01 }, { 0xa1, 0x02 }, { 0xbd, 0x01 }, { 0xbe, 0x02 }, { 0xb0, 0x60 }, { 0xb1, 0x02 }, { 0xb2, 0x80 }, { 0xb3, 0x40 }, { 0xb4, 0x40 }, { 0xb5, 0x40 }, { 0xb6, 0x00 }, { 0x66, 0xc7 }, { 0x72, 0xff }, { 0x74, 0xff }, { 0xfe, 0x03 }, { 0x10, 0x00 }, { 0x01, 0x03 }, { 0x02, 0x32 }, { 0x03, 0x90 }, { 0x04, 0x04 }, { 0x05, 0x00 }, { 0x06, 0x80 }, { 0x11, 0x2b }, { 0x12, 0xd0 }, { 0x13, 0x07 }, { 0x15, 0x00 }, { 0x21, 0x08 }, { 0x22, 0x05 }, { 0x23, 0x13 }, { 0x24, 0x02 }, { 0x25, 0x13 }, { 0x26, 0x08 }, { 0x29, 0x04 }, { 0x2a, 0x08 }, { 0x2b, 0x08 }, { 0xfe, 0x00 }, }; static const u32 camss_cbcr_offsets[] = { 0x56004U, 0x5a014U, 0x50004U, 0x58038U, 0x58044U, 0x58048U, 0x58050U, 0x4e040U, 0x4e03cU, 0x4e048U, 0x4e050U, 0x4e058U, 0x4f040U, 0x4f03cU, 0x4f048U, 0x4f050U, 0x4f058U, 0x4e01cU, 0x52018U, 0x53018U, }; struct msm8905_cam_dev { struct platform_device *pdev; struct v4l2_device v4l2_dev; struct video_device vdev; struct mutex lock; void __iomem *gcc_base; void __iomem *tlmm_base; void __iomem *camss_base; void __iomem *vbif_base; struct regmap *usid1_regmap; struct regulator *vdd_l6; struct regulator *vdd_l17; u8 *raw_dma_buf; dma_addr_t raw_dma_phys; u8 *mmap_bufs[CAM_MAX_MMAP_BUFS]; bool buf_queued[CAM_MAX_MMAP_BUFS]; u32 num_bufs; u32 sequence; u16 chip_id; bool streaming; bool hw_initialized; bool rotate_180; bool l6_enabled; bool l17_enabled; bool spmi_saved; bool micb1_val_saved; u32 saved_ear_ctl; u32 saved_micb1_val; }; static struct msm8905_cam_dev *g_cam; static inline u32 cam_r32(void __iomem *base, u32 off) { return readl_relaxed(base + off); } static inline void cam_w32(void __iomem *base, u32 off, u32 val) { writel_relaxed(val, base + off); } static void cam_set_rcg(struct msm8905_cam_dev *cam, u32 cmd_off, u32 cfg_val, u32 m, u32 n, u32 d) { int i; cam_w32(cam->gcc_base, cmd_off + 4U, cfg_val); if (m) { cam_w32(cam->gcc_base, cmd_off + 8U, m); cam_w32(cam->gcc_base, cmd_off + 12U, n); cam_w32(cam->gcc_base, cmd_off + 16U, d); } cam_w32(cam->gcc_base, cmd_off, cam_r32(cam->gcc_base, cmd_off) | 1U); wmb(); for (i = 0; i < 1000; i++) { if ((cam_r32(cam->gcc_base, cmd_off) & 1U) == 0U) break; udelay(1); } } static void cam_enable_cbcr(struct msm8905_cam_dev *cam, u32 off) { int i; cam_w32(cam->gcc_base, off, cam_r32(cam->gcc_base, off) | 1U); wmb(); for (i = 0; i < 1000; i++) { if ((cam_r32(cam->gcc_base, off) & 0x80000000U) == 0U) break; udelay(1); } } static void cam_gpio_cfg(struct msm8905_cam_dev *cam, u32 gpio, u32 val) { cam_w32(cam->tlmm_base, gpio * 0x1000U, val); } static void cam_gpio_out(struct msm8905_cam_dev *cam, u32 gpio, bool high) { cam_w32(cam->tlmm_base, gpio * 0x1000U + 4U, high ? 2U : 0U); } static bool cam_gpio_in(struct msm8905_cam_dev *cam, u32 gpio) { return (cam_r32(cam->tlmm_base, gpio * 0x1000U + 4U) & 1U) != 0U; } static void cam_i2c_sda_hi(struct msm8905_cam_dev *cam) { cam_gpio_cfg(cam, GPIO_CAM_I2C_SDA, 0x003U); } static void cam_i2c_sda_lo(struct msm8905_cam_dev *cam) { cam_gpio_out(cam, GPIO_CAM_I2C_SDA, false); cam_gpio_cfg(cam, GPIO_CAM_I2C_SDA, 0x203U); } static void cam_i2c_scl_hi(struct msm8905_cam_dev *cam) { cam_gpio_cfg(cam, GPIO_CAM_I2C_SCL, 0x003U); } static void cam_i2c_scl_lo(struct msm8905_cam_dev *cam) { cam_gpio_out(cam, GPIO_CAM_I2C_SCL, false); cam_gpio_cfg(cam, GPIO_CAM_I2C_SCL, 0x203U); } static void cam_i2c_delay(struct msm8905_cam_dev *cam) { udelay(2); } static void cam_i2c_start(struct msm8905_cam_dev *cam) { cam_i2c_sda_hi(cam); cam_i2c_scl_hi(cam); cam_i2c_delay(cam); cam_i2c_sda_lo(cam); cam_i2c_delay(cam); cam_i2c_scl_lo(cam); cam_i2c_delay(cam); } static void cam_i2c_stop(struct msm8905_cam_dev *cam) { cam_i2c_sda_lo(cam); cam_i2c_delay(cam); cam_i2c_scl_hi(cam); cam_i2c_delay(cam); cam_i2c_sda_hi(cam); cam_i2c_delay(cam); } static bool cam_i2c_tx(struct msm8905_cam_dev *cam, u8 byte) { int i; bool ack; for (i = 7; i >= 0; i--) { if ((byte >> i) & 1U) cam_i2c_sda_hi(cam); else cam_i2c_sda_lo(cam); cam_i2c_delay(cam); cam_i2c_scl_hi(cam); cam_i2c_delay(cam); cam_i2c_scl_lo(cam); } cam_i2c_sda_hi(cam); cam_i2c_delay(cam); cam_i2c_scl_hi(cam); cam_i2c_delay(cam); ack = !cam_gpio_in(cam, GPIO_CAM_I2C_SDA); cam_i2c_scl_lo(cam); cam_i2c_delay(cam); return ack; } static u8 cam_i2c_rx(struct msm8905_cam_dev *cam, bool ack) { int i; u8 val = 0; cam_i2c_sda_hi(cam); for (i = 7; i >= 0; i--) { cam_i2c_delay(cam); cam_i2c_scl_hi(cam); cam_i2c_delay(cam); if (cam_gpio_in(cam, GPIO_CAM_I2C_SDA)) val |= (u8)(1U << i); cam_i2c_scl_lo(cam); } if (ack) cam_i2c_sda_lo(cam); else cam_i2c_sda_hi(cam); cam_i2c_delay(cam); cam_i2c_scl_hi(cam); cam_i2c_delay(cam); cam_i2c_scl_lo(cam); cam_i2c_delay(cam); cam_i2c_sda_hi(cam); return val; } static u8 gc2395_read_reg(struct msm8905_cam_dev *cam, u8 reg) { u8 val; cam_i2c_start(cam); cam_i2c_tx(cam, 0x6eU); cam_i2c_tx(cam, reg); cam_i2c_start(cam); cam_i2c_tx(cam, 0x6fU); val = cam_i2c_rx(cam, false); cam_i2c_stop(cam); return val; } static void gc2395_write_reg(struct msm8905_cam_dev *cam, u8 reg, u8 val) { cam_i2c_start(cam); cam_i2c_tx(cam, 0x6eU); cam_i2c_tx(cam, reg); cam_i2c_tx(cam, val); cam_i2c_stop(cam); } static void gc23xx_stream_on(struct msm8905_cam_dev *cam) { if (cam->chip_id == GC2365_CHIP_ID) { gc2395_write_reg(cam, 0xfeU, 0x03U); gc2395_write_reg(cam, 0x10U, 0x90U); gc2395_write_reg(cam, 0xfeU, 0x00U); } else { gc2395_write_reg(cam, 0xfeU, 0x00U); gc2395_write_reg(cam, 0xedU, 0x90U); gc2395_write_reg(cam, 0xfeU, 0x00U); } } static void gc23xx_stream_off(struct msm8905_cam_dev *cam) { if (cam->chip_id == GC2365_CHIP_ID) { gc2395_write_reg(cam, 0xfeU, 0x03U); gc2395_write_reg(cam, 0x10U, 0x00U); gc2395_write_reg(cam, 0xfeU, 0x00U); } else { gc2395_write_reg(cam, 0xfeU, 0x00U); gc2395_write_reg(cam, 0xedU, 0x00U); } } static void msm8905_cam_enable_clocks(struct msm8905_cam_dev *cam) { size_t i; if (cam_r32(cam->gcc_base, 0x58034U) & 1U) { cam_w32(cam->gcc_base, 0x58034U, cam_r32(cam->gcc_base, 0x58034U) & ~1U); usleep_range(1000, 2000); } if ((cam_r32(cam->gcc_base, 0x4500cU) & 0x3202U) != 0x3202U) { cam_w32(cam->gcc_base, 0x4500cU, cam_r32(cam->gcc_base, 0x4500cU) | 0x3202U); usleep_range(1000, 2000); } for (i = 0; i < ARRAY_SIZE(camss_cbcr_offsets); i++) { if ((cam_r32(cam->gcc_base, camss_cbcr_offsets[i]) & 1U) == 0U || (cam_r32(cam->gcc_base, camss_cbcr_offsets[i]) & 0x80000000U) != 0U) cam_enable_cbcr(cam, camss_cbcr_offsets[i]); } } static void msm8905_cam_ensure_regulators(struct msm8905_cam_dev *cam) { struct regulator *reg; if (!cam->vdd_l6) { reg = regulator_get_optional(&cam->pdev->dev, "l6"); if (!IS_ERR(reg)) cam->vdd_l6 = reg; } if (!cam->vdd_l17) { reg = regulator_get_optional(&cam->pdev->dev, "l17"); if (!IS_ERR(reg)) cam->vdd_l17 = reg; } } static int msm8905_cam_regulators_enable(struct msm8905_cam_dev *cam) { int ret; msm8905_cam_ensure_regulators(cam); if (cam->vdd_l6 && !cam->l6_enabled) { ret = regulator_enable(cam->vdd_l6); if (ret) return ret; cam->l6_enabled = true; } if (cam->vdd_l17 && !cam->l17_enabled) { ret = regulator_enable(cam->vdd_l17); if (ret) { if (cam->l6_enabled) { regulator_disable(cam->vdd_l6); cam->l6_enabled = false; } return ret; } cam->l17_enabled = true; } return 0; } static void msm8905_cam_regulators_disable(struct msm8905_cam_dev *cam) { if (cam->vdd_l17 && cam->l17_enabled) { regulator_disable(cam->vdd_l17); cam->l17_enabled = false; } if (cam->vdd_l6 && cam->l6_enabled) { regulator_disable(cam->vdd_l6); cam->l6_enabled = false; } } static void msm8905_cam_spmi_restore(struct msm8905_cam_dev *cam) { if (!cam->usid1_regmap || !cam->spmi_saved) return; if (cam->micb1_val_saved) { regmap_write(cam->usid1_regmap, PM8909_CDC_A_MICB_1_VAL, cam->saved_micb1_val); cam->micb1_val_saved = false; } regmap_update_bits(cam->usid1_regmap, PM8909_CDC_A_RX_EAR_CTL, 0x20U, cam->saved_ear_ctl); cam->spmi_saved = false; } /* Shared power-down path for both hw_deinit and early hw_init failure */ static void msm8905_cam_hw_teardown_common(struct msm8905_cam_dev *cam) { ssize_t i; /* Place sensor in hardware power-down and disable MCLK0 output */ cam_gpio_out(cam, GPIO_CAM_RESET, false); cam_gpio_out(cam, GPIO_CAM_STANDBY, false); cam_gpio_out(cam, GPIO_CAM_VDIG, false); cam_gpio_cfg(cam, GPIO_CAM_MCLK0, 0x0001U); /* Gate CAMSS branch clocks in reverse order */ for (i = (ssize_t)ARRAY_SIZE(camss_cbcr_offsets) - 1; i >= 0; i--) { u32 off = camss_cbcr_offsets[i]; cam_w32(cam->gcc_base, off, cam_r32(cam->gcc_base, off) & ~1U); } wmb(); /* Gate CAMSS RCG roots (MCLK0 and CSI/VFE RCGs) */ cam_w32(cam->gcc_base, 0x52000U, 0x0U); cam_w32(cam->gcc_base, 0x4e000U, 0x0U); cam_w32(cam->gcc_base, 0x4f020U, 0x0U); cam_w32(cam->gcc_base, 0x4e020U, 0x0U); cam_w32(cam->gcc_base, 0x58000U, 0x0U); cam_w32(cam->gcc_base, 0x5a000U, 0x0U); /* Release SMMU vote clocks and power down VFE GDSC */ cam_w32(cam->gcc_base, 0x4500cU, cam_r32(cam->gcc_base, 0x4500cU) & ~0x3202U); cam_w32(cam->gcc_base, 0x58034U, cam_r32(cam->gcc_base, 0x58034U) | 1U); wmb(); msm8905_cam_spmi_restore(cam); msm8905_cam_regulators_disable(cam); cam->hw_initialized = false; cam->streaming = false; } static void msm8905_cam_hw_deinit(struct msm8905_cam_dev *cam) { if (!cam->hw_initialized) return; /* 1. Stop ISPIF and sensor MIPI CSI-2 stream */ cam_w32(cam->camss_base, ISPIF_BASE + 0x248U, 0xaaaaaaaaU); wmb(); gc23xx_stream_off(cam); usleep_range(600, 900); /* 2. Power down GPIOs, clocks, VFE GDSC, and RPM/SPMI regulators */ msm8905_cam_hw_teardown_common(cam); } static int msm8905_cam_hw_init(struct msm8905_cam_dev *cam) { size_t i; u16 chip_id; u32 r; int ret; if (cam->hw_initialized) { msm8905_cam_enable_clocks(cam); return 0; } /* 1. Enable RPM-owned PM8909 LDO6 (1.8V) and LDO17 (2.85V) plus codec rails */ ret = msm8905_cam_regulators_enable(cam); if (ret) return ret; if (cam->usid1_regmap) { unsigned int val = 0; cam->saved_ear_ctl = (regmap_read(cam->usid1_regmap, PM8909_CDC_A_RX_EAR_CTL, &val) == 0) ? (val & 0x20U) : 0U; if (regmap_read(cam->usid1_regmap, PM8909_CDC_A_MICB_1_VAL, &val) == 0) { cam->saved_micb1_val = val; cam->micb1_val_saved = true; } else { cam->micb1_val_saved = false; } cam->spmi_saved = true; regmap_write(cam->usid1_regmap, PM8909_CDC_A_MICB_1_VAL, 0x60U); regmap_update_bits(cam->usid1_regmap, PM8909_CDC_A_RX_EAR_CTL, 0x20U, 0x20U); } usleep_range(2000, 3000); /* 2. Power on VFE GDSC and SMMU vote clocks */ cam_w32(cam->gcc_base, 0x58034U, cam_r32(cam->gcc_base, 0x58034U) & ~1U); usleep_range(1000, 2000); cam_w32(cam->gcc_base, 0x4500cU, cam_r32(cam->gcc_base, 0x4500cU) | 0x3202U); usleep_range(1000, 2000); /* 3. Configure CAMSS root clock generators */ cam_set_rcg(cam, 0x5a000U, 0x0113U, 0, 0, 0); cam_set_rcg(cam, 0x58000U, 0x0105U, 0, 0, 0); cam_set_rcg(cam, 0x4e020U, 0x0107U, 0, 0, 0); cam_set_rcg(cam, 0x4f020U, 0x0107U, 0, 0, 0); cam_set_rcg(cam, 0x4e000U, 0x0107U, 0, 0, 0); cam_set_rcg(cam, 0x52000U, 0x2101U, 1U, 0xffdfU, 0xffdeU); for (i = 0; i < ARRAY_SIZE(camss_cbcr_offsets); i++) cam_enable_cbcr(cam, camss_cbcr_offsets[i]); /* 4. Power on GalaxyCore GC2395 / GC2385 / GC2365 sensor */ cam_gpio_cfg(cam, GPIO_CAM_STANDBY, 0x200U); cam_gpio_cfg(cam, GPIO_CAM_RESET, 0x200U); cam_gpio_cfg(cam, GPIO_CAM_VDIG, 0x200U); cam_gpio_out(cam, GPIO_CAM_STANDBY, false); cam_gpio_out(cam, GPIO_CAM_RESET, false); cam_gpio_out(cam, GPIO_CAM_VDIG, true); cam_gpio_cfg(cam, GPIO_CAM_MCLK0, 0x00c4U); usleep_range(10000, 12000); cam_gpio_out(cam, GPIO_CAM_STANDBY, true); cam_gpio_out(cam, GPIO_CAM_RESET, true); usleep_range(15000, 18000); chip_id = ((u16)gc2395_read_reg(cam, 0xf0U) << 8) | gc2395_read_reg(cam, 0xf1U); if (chip_id != GC2395_CHIP_ID && chip_id != GC2385_CHIP_ID && chip_id != GC2365_CHIP_ID) { dev_err(&cam->pdev->dev, "GalaxyCore chip ID mismatch: 0x%04x (expected 0x2395/0x2385/0x2365)\n", chip_id); msm8905_cam_hw_teardown_common(cam); return -ENODEV; } cam->chip_id = chip_id; dev_info(&cam->pdev->dev, "detected GalaxyCore GC%04x sensor\n", chip_id); if (chip_id == GC2365_CHIP_ID) { for (i = 0; i < ARRAY_SIZE(gc2365_init_regs); i++) gc2395_write_reg(cam, gc2365_init_regs[i].reg, gc2365_init_regs[i].val); } else { for (i = 0; i < ARRAY_SIZE(gc2395_init_regs); i++) gc2395_write_reg(cam, gc2395_init_regs[i].reg, gc2395_init_regs[i].val); } /* 5. CSI_CLK_MUX */ cam_w32(cam->camss_base, 0x0030U, 0x0U); cam_w32(cam->camss_base, 0x0020U, 0x0U); cam_w32(cam->camss_base, 0x0024U, 0x0U); cam_w32(cam->camss_base, 0x0028U, 0x0U); /* 6. CSIPHY0 (qcom,csiphy-v3.1) */ cam_w32(cam->camss_base, CSIPHY0_BASE + 0x140U, 0x1U); usleep_range(5000, 6000); cam_w32(cam->camss_base, CSIPHY0_BASE + 0x140U, 0x0U); cam_w32(cam->camss_base, CSIPHY0_BASE + 0x1ecU, 0x1U); cam_w32(cam->camss_base, CSIPHY0_BASE + 0x1f4U, 0x1U); cam_w32(cam->camss_base, CSIPHY0_BASE + 0x144U, 0x7U); cam_w32(cam->camss_base, CSIPHY0_BASE + 0x004U, 0x10U); cam_w32(cam->camss_base, CSIPHY0_BASE + 0x008U, 0x14U); cam_w32(cam->camss_base, CSIPHY0_BASE + 0x1acU, 0x3fU); cam_w32(cam->camss_base, CSIPHY0_BASE + 0x1ccU, 0x3fU); cam_w32(cam->camss_base, CSIPHY0_BASE + 0x044U, 0x10U); cam_w32(cam->camss_base, CSIPHY0_BASE + 0x048U, 0x14U); cam_w32(cam->camss_base, CSIPHY0_BASE + 0x1b0U, 0x3fU); cam_w32(cam->camss_base, CSIPHY0_BASE + 0x1d0U, 0x3fU); for (i = 0; i < 4; i++) cam_w32(cam->camss_base, CSIPHY0_BASE + 0x1ccU + 4U * i, 0xffU); cam_w32(cam->camss_base, CSIPHY0_BASE + 0x164U, 0x1U); cam_w32(cam->camss_base, CSIPHY0_BASE + 0x164U, 0x0U); for (i = 0; i < 4; i++) cam_w32(cam->camss_base, CSIPHY0_BASE + 0x1ccU + 4U * i, 0x0U); /* 7. CSID0 (qcom,csid-v3.1) */ cam_w32(cam->camss_base, CSID0_BASE + 0x64U, 0xffffffffU); cam_w32(cam->camss_base, CSID0_BASE + 0x0cU, 0x7fffU); usleep_range(5000, 6000); cam_w32(cam->camss_base, CSID0_BASE + 0x60U, 0xffffffffU); cam_w32(cam->camss_base, CSID0_BASE + 0x64U, 0xffffffffU); cam_w32(cam->camss_base, CSID0_BASE + 0x04U, 0x00043200U); cam_w32(cam->camss_base, CSID0_BASE + 0x08U, 0x0000000fU); cam_w32(cam->camss_base, CSID0_BASE + 0x10U, 0x0000002bU); cam_w32(cam->camss_base, CSID0_BASE + 0x20U, (2U << 4) | 0x3U); /* 8. VFE0 (qcom,vfe32 v3.6.0) + VBIF */ cam_w32(cam->camss_base, VFE0_BASE + 0x01cU, 0x00000000U); cam_w32(cam->camss_base, VFE0_BASE + 0x020U, 0x00800000U); cam_w32(cam->camss_base, VFE0_BASE + 0x024U, 0xffffffffU); cam_w32(cam->camss_base, VFE0_BASE + 0x028U, 0xffffffffU); cam_w32(cam->camss_base, VFE0_BASE + 0x018U, 0x00000001U); cam_w32(cam->camss_base, VFE0_BASE + 0x004U, 0x000003ffU); usleep_range(5000, 6000); for (r = 0x7bcU; r <= 0x7d4U; r += 4U) cam_w32(cam->camss_base, VFE0_BASE + r, 0xaaa5aaa5U); cam_w32(cam->camss_base, VFE0_BASE + 0x798U, 0x00010000U); for (r = 0x7d8U; r < 0x810U; r += 4U) cam_w32(cam->camss_base, VFE0_BASE + r, 0xcccc1111U); cam_w32(cam->camss_base, VFE0_BASE + 0x810U, 0x00000103U); cam_w32(cam->vbif_base, 0x004U, 0x00000001U); cam_w32(cam->camss_base, VFE0_BASE + 0x00cU, 0x07ffffffU); cam_w32(cam->camss_base, VFE0_BASE + 0x03cU, 0x00000009U); cam_w32(cam->camss_base, VFE0_BASE + 0x024U, 0xffffffffU); cam_w32(cam->camss_base, VFE0_BASE + 0x028U, 0xffffffffU); cam_w32(cam->camss_base, VFE0_BASE + 0x018U, 0x00000001U); cam_w32(cam->camss_base, VFE0_BASE + 0x01cU, 0xffffffffU); cam_w32(cam->camss_base, VFE0_BASE + 0x020U, 0xffffffffU); /* 9. ISPIF */ cam_w32(cam->camss_base, ISPIF_BASE + 0x008U, 0x00001ff9U); usleep_range(5000, 6000); cam_w32(cam->camss_base, ISPIF_BASE + 0x008U, 0x00000000U); usleep_range(5000, 6000); cam_w32(cam->camss_base, ISPIF_BASE + 0x200U, 0x00000040U); cam_w32(cam->camss_base, ISPIF_BASE + 0x230U, 0xffffffffU); cam_w32(cam->camss_base, ISPIF_BASE + 0x234U, 0xffffffffU); cam_w32(cam->camss_base, ISPIF_BASE + 0x238U, 0xffffffffU); cam_w32(cam->camss_base, ISPIF_BASE + 0x01cU, 0x00000001U); cam_w32(cam->camss_base, ISPIF_BASE + 0x208U, 0xffffffffU); cam_w32(cam->camss_base, ISPIF_BASE + 0x20cU, 0xffffffffU); cam_w32(cam->camss_base, ISPIF_BASE + 0x210U, 0xffffffffU); cam_w32(cam->camss_base, ISPIF_BASE + 0x244U, 0x00000000U); cam_w32(cam->camss_base, ISPIF_BASE + 0x264U, 0x00000001U); cam_w32(cam->camss_base, ISPIF_BASE + 0x248U, 0xaaaaaaaaU); wmb(); cam->hw_initialized = true; return 0; } static void msm8905_cam_demosaic(const u8 *raw, u8 *rgb, bool rotate_180) { u32 y, x; u32 idx = 0; for (y = 0; y < CAM_OUT_HEIGHT; y++) { u32 src_y = rotate_180 ? CAM_OUT_HEIGHT - 1U - y : y; const u8 *r0 = raw + (2U * src_y) * CAM_RAW_STRIDE; const u8 *r1 = raw + (2U * src_y + 1U) * CAM_RAW_STRIDE; for (x = 0; x < CAM_OUT_WIDTH; x += 2U) { u32 src_x = rotate_180 ? CAM_OUT_WIDTH - 2U - x : x; u32 g_off = (src_x >> 1) * 5U; u8 b0 = r0[g_off + 0U]; u8 g0 = (u8)(((u32)r0[g_off + 1U] + (u32)r1[g_off + 0U]) >> 1); u8 r_0 = r1[g_off + 1U]; u8 b1 = r0[g_off + 2U]; u8 g1 = (u8)(((u32)r0[g_off + 3U] + (u32)r1[g_off + 2U]) >> 1); u8 r_1 = r1[g_off + 3U]; if (rotate_180) { rgb[idx + 0U] = r_1; rgb[idx + 1U] = g1; rgb[idx + 2U] = b1; rgb[idx + 3U] = r_0; rgb[idx + 4U] = g0; rgb[idx + 5U] = b0; } else { rgb[idx + 0U] = r_0; rgb[idx + 1U] = g0; rgb[idx + 2U] = b0; rgb[idx + 3U] = r_1; rgb[idx + 4U] = g1; rgb[idx + 5U] = b1; } idx += 6U; } } } static int msm8905_cam_capture_rgb(struct msm8905_cam_dev *cam, u8 *dst_rgb) { int ret; int poll_cnt; u32 irq0; if (!cam->hw_initialized) { ret = msm8905_cam_hw_init(cam); if (ret) return ret; } else { msm8905_cam_enable_clocks(cam); } /* Ensure sensor MIPI stream and ISPIF/VFE0 are idle before arming next frame */ cam_w32(cam->camss_base, ISPIF_BASE + 0x248U, 0xaaaaaaaaU); wmb(); gc23xx_stream_off(cam); usleep_range(600, 900); cam_w32(cam->camss_base, VFE0_BASE + 0x04cU, 0x00000000U); cam_w32(cam->camss_base, VFE0_BASE + 0x6fcU, 0x00000000U); wmb(); /* Reset ISPIF cleanly for this frame */ cam_w32(cam->camss_base, ISPIF_BASE + 0x008U, 0x00001ff9U); usleep_range(1000, 1500); cam_w32(cam->camss_base, ISPIF_BASE + 0x008U, 0x00000000U); usleep_range(1000, 1500); cam_w32(cam->camss_base, ISPIF_BASE + 0x200U, 0x00000040U); cam_w32(cam->camss_base, ISPIF_BASE + 0x230U, 0xffffffffU); cam_w32(cam->camss_base, ISPIF_BASE + 0x234U, 0xffffffffU); cam_w32(cam->camss_base, ISPIF_BASE + 0x238U, 0xffffffffU); cam_w32(cam->camss_base, ISPIF_BASE + 0x01cU, 0x00000001U); cam_w32(cam->camss_base, ISPIF_BASE + 0x208U, 0xffffffffU); cam_w32(cam->camss_base, ISPIF_BASE + 0x20cU, 0xffffffffU); cam_w32(cam->camss_base, ISPIF_BASE + 0x210U, 0xffffffffU); cam_w32(cam->camss_base, ISPIF_BASE + 0x244U, 0x00000000U); cam_w32(cam->camss_base, ISPIF_BASE + 0x264U, 0x00000001U); /* Program WM0 PING & PONG to raw_dma_phys and arm single frame */ cam_w32(cam->camss_base, VFE0_BASE + 0x050U, (u32)cam->raw_dma_phys); cam_w32(cam->camss_base, VFE0_BASE + 0x054U, (u32)cam->raw_dma_phys); cam_w32(cam->camss_base, VFE0_BASE + 0x058U, 0x00000317U); cam_w32(cam->camss_base, VFE0_BASE + 0x060U, 0x00000033U); cam_w32(cam->camss_base, VFE0_BASE + 0x040U, 0x000000a0U); cam_w32(cam->camss_base, VFE0_BASE + 0x034U, 0x00000001U); cam_w32(cam->camss_base, VFE0_BASE + 0x04cU, 0x00000003U); cam_w32(cam->camss_base, VFE0_BASE + 0x038U, 0x00000001U); cam_w32(cam->camss_base, VFE0_BASE + 0x6fcU, 0x00010004U); cam_w32(cam->camss_base, VFE0_BASE + 0x024U, 0xffffffffU); cam_w32(cam->camss_base, VFE0_BASE + 0x028U, 0xffffffffU); cam_w32(cam->camss_base, VFE0_BASE + 0x018U, 0x00000001U); cam_w32(cam->camss_base, VFE0_BASE + 0x260U, 0x00000002U); /* Enable ISPIF RDI0 on frame boundary and start sensor MIPI stream */ cam_w32(cam->camss_base, ISPIF_BASE + 0x248U, 0xfffffdffU); wmb(); gc23xx_stream_on(cam); /* * Poll for WM0 PING_PONG_IRQ (bit 6 = 0x40) with sub-millisecond sleep so we * catch EOF right inside the 14 ms vertical blanking window before Frame 2. */ irq0 = 0; for (poll_cnt = 0; poll_cnt < 400; poll_cnt++) { irq0 = cam_r32(cam->camss_base, VFE0_BASE + 0x02cU); if (irq0 & 0x00000040U) break; usleep_range(400, 600); } /* * 1. Gate ISPIF at frame boundary FIRST so VFE0 never receives a partial frame. * 2. Stop sensor MIPI stream during VBLANK (LP-11 state). * 3. Wait for VFE0 WM0 / VBIF AXI write bursts to drain completely before * clearing WM0_CFG and RDI0_CFG. */ cam_w32(cam->camss_base, ISPIF_BASE + 0x248U, 0xaaaaaaaaU); wmb(); gc23xx_stream_off(cam); usleep_range(800, 1200); cam_w32(cam->camss_base, VFE0_BASE + 0x04cU, 0x00000000U); cam_w32(cam->camss_base, VFE0_BASE + 0x6fcU, 0x00000000U); cam_w32(cam->camss_base, VFE0_BASE + 0x024U, 0xffffffffU); cam_w32(cam->camss_base, VFE0_BASE + 0x028U, 0xffffffffU); cam_w32(cam->camss_base, VFE0_BASE + 0x018U, 0x00000001U); wmb(); if (!(irq0 & 0x00000040U)) { cam->hw_initialized = false; return -ETIMEDOUT; } msm8905_cam_demosaic(cam->raw_dma_buf, dst_rgb, cam->rotate_180); cam->sequence++; return 0; } /* V4L2 ioctl operations */ static int msm8905_cam_querycap(struct file *file, void *priv, struct v4l2_capability *cap) { strscpy(cap->driver, "msm8905-cam", sizeof(cap->driver)); strscpy(cap->card, "Nokia 8110 4G Rear Camera", sizeof(cap->card)); strscpy(cap->bus_info, "platform:msm8905-cam", sizeof(cap->bus_info)); return 0; } static int msm8905_cam_enum_fmt_vid_cap(struct file *file, void *priv, struct v4l2_fmtdesc *f) { if (f->index > 0) return -EINVAL; f->pixelformat = V4L2_PIX_FMT_RGB24; return 0; } static void msm8905_cam_fill_pix_format(struct v4l2_pix_format *pix) { pix->width = CAM_OUT_WIDTH; pix->height = CAM_OUT_HEIGHT; pix->pixelformat = V4L2_PIX_FMT_RGB24; pix->field = V4L2_FIELD_NONE; pix->bytesperline = CAM_OUT_STRIDE; pix->sizeimage = CAM_OUT_FRAME_BYTES; pix->colorspace = V4L2_COLORSPACE_SRGB; } static int msm8905_cam_g_fmt_vid_cap(struct file *file, void *priv, struct v4l2_format *f) { msm8905_cam_fill_pix_format(&f->fmt.pix); return 0; } static int msm8905_cam_try_fmt_vid_cap(struct file *file, void *priv, struct v4l2_format *f) { msm8905_cam_fill_pix_format(&f->fmt.pix); return 0; } static int msm8905_cam_s_fmt_vid_cap(struct file *file, void *priv, struct v4l2_format *f) { msm8905_cam_fill_pix_format(&f->fmt.pix); return 0; } static int msm8905_cam_enum_framesizes(struct file *file, void *priv, struct v4l2_frmsizeenum *fsize) { if (fsize->index > 0 || fsize->pixel_format != V4L2_PIX_FMT_RGB24) return -EINVAL; fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE; fsize->discrete.width = CAM_OUT_WIDTH; fsize->discrete.height = CAM_OUT_HEIGHT; return 0; } static int msm8905_cam_enum_frameintervals(struct file *file, void *priv, struct v4l2_frmivalenum *fival) { if (fival->index > 0 || fival->pixel_format != V4L2_PIX_FMT_RGB24 || fival->width != CAM_OUT_WIDTH || fival->height != CAM_OUT_HEIGHT) return -EINVAL; fival->type = V4L2_FRMIVAL_TYPE_DISCRETE; fival->discrete.numerator = 1; fival->discrete.denominator = 15; return 0; } static int msm8905_cam_enum_input(struct file *file, void *priv, struct v4l2_input *inp) { struct msm8905_cam_dev *cam = video_drvdata(file); if (inp->index > 0) return -EINVAL; if (cam->chip_id == GC2385_CHIP_ID) strscpy(inp->name, "GC2385 Rear Camera", sizeof(inp->name)); else if (cam->chip_id == GC2365_CHIP_ID) strscpy(inp->name, "GC2365 Rear Camera", sizeof(inp->name)); else strscpy(inp->name, "GC2395 Rear Camera", sizeof(inp->name)); inp->type = V4L2_INPUT_TYPE_CAMERA; return 0; } static int msm8905_cam_g_input(struct file *file, void *priv, unsigned int *i) { *i = 0; return 0; } static int msm8905_cam_s_input(struct file *file, void *priv, unsigned int i) { return (i == 0) ? 0 : -EINVAL; } static int msm8905_cam_g_parm(struct file *file, void *priv, struct v4l2_streamparm *sp) { if (sp->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) return -EINVAL; sp->parm.capture.capability = V4L2_CAP_TIMEPERFRAME; sp->parm.capture.readbuffers = 2; sp->parm.capture.timeperframe.numerator = 1; sp->parm.capture.timeperframe.denominator = 15; return 0; } static int msm8905_cam_s_parm(struct file *file, void *priv, struct v4l2_streamparm *sp) { return msm8905_cam_g_parm(file, priv, sp); } static int msm8905_cam_reqbufs(struct file *file, void *priv, struct v4l2_requestbuffers *rb) { struct msm8905_cam_dev *cam = video_drvdata(file); u32 i; if (rb->type != V4L2_BUF_TYPE_VIDEO_CAPTURE || rb->memory != V4L2_MEMORY_MMAP) return -EINVAL; if (rb->count == 0) { cam->num_bufs = 0; for (i = 0; i < CAM_MAX_MMAP_BUFS; i++) cam->buf_queued[i] = false; return 0; } rb->count = clamp_t(u32, rb->count, 1U, CAM_MAX_MMAP_BUFS); cam->num_bufs = rb->count; for (i = 0; i < CAM_MAX_MMAP_BUFS; i++) cam->buf_queued[i] = false; return 0; } static int msm8905_cam_querybuf(struct file *file, void *priv, struct v4l2_buffer *b) { struct msm8905_cam_dev *cam = video_drvdata(file); u32 aligned_size = PAGE_ALIGN(CAM_OUT_FRAME_BYTES); if (b->type != V4L2_BUF_TYPE_VIDEO_CAPTURE || b->index >= cam->num_bufs) return -EINVAL; b->bytesused = CAM_OUT_FRAME_BYTES; b->flags = V4L2_BUF_FLAG_MAPPED | V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC; if (cam->buf_queued[b->index]) b->flags |= V4L2_BUF_FLAG_QUEUED | V4L2_BUF_FLAG_DONE; b->field = V4L2_FIELD_NONE; b->memory = V4L2_MEMORY_MMAP; b->m.offset = b->index * aligned_size; b->length = CAM_OUT_FRAME_BYTES; return 0; } static int msm8905_cam_qbuf(struct file *file, void *priv, struct v4l2_buffer *b) { struct msm8905_cam_dev *cam = video_drvdata(file); if (b->type != V4L2_BUF_TYPE_VIDEO_CAPTURE || b->memory != V4L2_MEMORY_MMAP || b->index >= cam->num_bufs) return -EINVAL; cam->buf_queued[b->index] = true; b->flags = V4L2_BUF_FLAG_MAPPED | V4L2_BUF_FLAG_QUEUED | V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC; return 0; } static int msm8905_cam_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b) { struct msm8905_cam_dev *cam = video_drvdata(file); u32 idx = 0; int ret; if (b->type != V4L2_BUF_TYPE_VIDEO_CAPTURE || b->memory != V4L2_MEMORY_MMAP) return -EINVAL; for (idx = 0; idx < cam->num_bufs; idx++) { if (cam->buf_queued[idx]) break; } if (idx >= cam->num_bufs) return -EINVAL; ret = msm8905_cam_capture_rgb(cam, cam->mmap_bufs[idx]); if (ret) return ret; cam->buf_queued[idx] = false; b->index = idx; b->bytesused = CAM_OUT_FRAME_BYTES; b->flags = V4L2_BUF_FLAG_MAPPED | V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC; b->field = V4L2_FIELD_NONE; b->sequence = cam->sequence; b->memory = V4L2_MEMORY_MMAP; b->m.offset = idx * PAGE_ALIGN(CAM_OUT_FRAME_BYTES); b->length = CAM_OUT_FRAME_BYTES; v4l2_buffer_set_timestamp(b, ktime_get_ns()); return 0; } static int msm8905_cam_streamon(struct file *file, void *priv, enum v4l2_buf_type i) { struct msm8905_cam_dev *cam = video_drvdata(file); if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE) return -EINVAL; cam->streaming = true; return msm8905_cam_hw_init(cam); } static int msm8905_cam_streamoff(struct file *file, void *priv, enum v4l2_buf_type i) { struct msm8905_cam_dev *cam = video_drvdata(file); u32 idx; if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE) return -EINVAL; cam->streaming = false; for (idx = 0; idx < CAM_MAX_MMAP_BUFS; idx++) cam->buf_queued[idx] = false; msm8905_cam_hw_deinit(cam); return 0; } static const struct v4l2_ioctl_ops msm8905_cam_ioctl_ops = { .vidioc_querycap = msm8905_cam_querycap, .vidioc_enum_fmt_vid_cap = msm8905_cam_enum_fmt_vid_cap, .vidioc_g_fmt_vid_cap = msm8905_cam_g_fmt_vid_cap, .vidioc_try_fmt_vid_cap = msm8905_cam_try_fmt_vid_cap, .vidioc_s_fmt_vid_cap = msm8905_cam_s_fmt_vid_cap, .vidioc_enum_framesizes = msm8905_cam_enum_framesizes, .vidioc_enum_frameintervals = msm8905_cam_enum_frameintervals, .vidioc_enum_input = msm8905_cam_enum_input, .vidioc_g_input = msm8905_cam_g_input, .vidioc_s_input = msm8905_cam_s_input, .vidioc_g_parm = msm8905_cam_g_parm, .vidioc_s_parm = msm8905_cam_s_parm, .vidioc_reqbufs = msm8905_cam_reqbufs, .vidioc_querybuf = msm8905_cam_querybuf, .vidioc_qbuf = msm8905_cam_qbuf, .vidioc_dqbuf = msm8905_cam_dqbuf, .vidioc_streamon = msm8905_cam_streamon, .vidioc_streamoff = msm8905_cam_streamoff, }; static ssize_t msm8905_cam_read(struct file *file, char __user *buf, size_t count, loff_t *ppos) { struct msm8905_cam_dev *cam = video_drvdata(file); int ret; if ( mutex_lock_interruptible(&cam->lock) ) return -ERESTARTSYS; if (*ppos == 0 || *ppos >= CAM_OUT_FRAME_BYTES) { ret = msm8905_cam_capture_rgb(cam, cam->mmap_bufs[0]); if (ret) { mutex_unlock(&cam->lock); return ret; } *ppos = 0; } if (count > CAM_OUT_FRAME_BYTES - *ppos) count = CAM_OUT_FRAME_BYTES - *ppos; if (copy_to_user(buf, cam->mmap_bufs[0] + *ppos, count)) { mutex_unlock(&cam->lock); return -EFAULT; } *ppos += count; mutex_unlock(&cam->lock); return count; } static int msm8905_cam_mmap(struct file *file, struct vm_area_struct *vma) { struct msm8905_cam_dev *cam = video_drvdata(file); u32 aligned_size = PAGE_ALIGN(CAM_OUT_FRAME_BYTES); unsigned long offset = vma->vm_pgoff << PAGE_SHIFT; u32 idx = offset / aligned_size; if (idx >= CAM_MAX_MMAP_BUFS || (offset % aligned_size) != 0) return -EINVAL; return remap_vmalloc_range(vma, cam->mmap_bufs[idx], 0); } static __poll_t msm8905_cam_poll(struct file *file, poll_table *wait) { return EPOLLIN | EPOLLRDNORM; } static int msm8905_cam_release(struct file *file) { struct msm8905_cam_dev *cam = video_drvdata(file); bool is_last; u32 idx; int ret; mutex_lock(&cam->lock); is_last = v4l2_fh_is_singular_file(file); ret = v4l2_fh_release(file); if (is_last) { for (idx = 0; idx < CAM_MAX_MMAP_BUFS; idx++) cam->buf_queued[idx] = false; msm8905_cam_hw_deinit(cam); } mutex_unlock(&cam->lock); return ret; } static const struct v4l2_file_operations msm8905_cam_fops = { .owner = THIS_MODULE, .open = v4l2_fh_open, .release = msm8905_cam_release, .read = msm8905_cam_read, .mmap = msm8905_cam_mmap, .poll = msm8905_cam_poll, .unlocked_ioctl = video_ioctl2, }; static void msm8905_cam_lookup_usid1_regmap(struct msm8905_cam_dev *cam) { struct device_node *np; struct platform_device *codec_pdev; np = of_find_compatible_node(NULL, NULL, "qcom,pm8916-wcd-analog-codec"); if (!np) return; codec_pdev = of_find_device_by_node(np); of_node_put(np); if (!codec_pdev) return; if (codec_pdev->dev.parent) cam->usid1_regmap = dev_get_regmap(codec_pdev->dev.parent, NULL); put_device(&codec_pdev->dev); } static int __init msm8905_cam_init(void) { struct msm8905_cam_dev *cam; u32 aligned_size = PAGE_ALIGN(CAM_OUT_FRAME_BYTES); int ret; u32 i; if (!of_machine_is_compatible("qcom,msm8905") && !of_machine_is_compatible("qcom,msm8909") && !of_machine_is_compatible("nokia,argon")) { return -ENODEV; } cam = kzalloc(sizeof(*cam), GFP_KERNEL); if (!cam) return -ENOMEM; mutex_init(&cam->lock); /* The 2720 Flip sensor is mounted 180 degrees from the other Nokia targets. */ cam->rotate_180 = of_machine_is_compatible("nokia,beatles"); cam->pdev = platform_device_register_simple("msm8905-cam", -1, NULL, 0); if (IS_ERR(cam->pdev)) { ret = PTR_ERR(cam->pdev); kfree(cam); return ret; } dma_coerce_mask_and_coherent(&cam->pdev->dev, DMA_BIT_MASK(32)); cam->raw_dma_buf = dma_alloc_coherent(&cam->pdev->dev, CAM_DMA_ALLOC_BYTES, &cam->raw_dma_phys, GFP_KERNEL); if (!cam->raw_dma_buf) { ret = -ENOMEM; goto err_pdev; } for (i = 0; i < CAM_MAX_MMAP_BUFS; i++) { cam->mmap_bufs[i] = vmalloc_user(aligned_size); if (!cam->mmap_bufs[i]) { ret = -ENOMEM; goto err_vmalloc; } } cam->gcc_base = ioremap(MSM8905_GCC_PHYS, MSM8905_GCC_SIZE); cam->tlmm_base = ioremap(MSM8905_TLMM_PHYS, MSM8905_TLMM_SIZE); cam->camss_base = ioremap(MSM8905_CAMSS_PHYS, MSM8905_CAMSS_SIZE); cam->vbif_base = ioremap(MSM8905_VBIF_PHYS, MSM8905_VBIF_SIZE); if (!cam->gcc_base || !cam->tlmm_base || !cam->camss_base || !cam->vbif_base) { ret = -ENOMEM; goto err_iomap; } msm8905_cam_lookup_usid1_regmap(cam); strscpy(cam->v4l2_dev.name, "msm8905-cam", sizeof(cam->v4l2_dev.name)); ret = v4l2_device_register(&cam->pdev->dev, &cam->v4l2_dev); if (ret) goto err_iomap; strscpy(cam->vdev.name, "msm8905-cam", sizeof(cam->vdev.name)); cam->vdev.v4l2_dev = &cam->v4l2_dev; cam->vdev.fops = &msm8905_cam_fops; cam->vdev.ioctl_ops = &msm8905_cam_ioctl_ops; cam->vdev.release = video_device_release_empty; cam->vdev.lock = &cam->lock; cam->vdev.device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE | V4L2_CAP_STREAMING; video_set_drvdata(&cam->vdev, cam); ret = video_register_device(&cam->vdev, VFL_TYPE_VIDEO, 0); if (ret) goto err_v4l2; g_cam = cam; pr_info("msm8905-cam: registered /dev/video%d (GC2395/GC2385 800x600 RGB24, dma=0x%08llx, rotate180=%u)\n", cam->vdev.num, (unsigned long long)cam->raw_dma_phys, cam->rotate_180); return 0; err_v4l2: v4l2_device_unregister(&cam->v4l2_dev); err_iomap: if (cam->vbif_base) iounmap(cam->vbif_base); if (cam->camss_base) iounmap(cam->camss_base); if (cam->tlmm_base) iounmap(cam->tlmm_base); if (cam->gcc_base) iounmap(cam->gcc_base); err_vmalloc: for (i = 0; i < CAM_MAX_MMAP_BUFS; i++) { if (cam->mmap_bufs[i]) vfree(cam->mmap_bufs[i]); } dma_free_coherent(&cam->pdev->dev, CAM_DMA_ALLOC_BYTES, cam->raw_dma_buf, cam->raw_dma_phys); err_pdev: platform_device_unregister(cam->pdev); kfree(cam); return ret; } static void __exit msm8905_cam_exit(void) { struct msm8905_cam_dev *cam = g_cam; u32 i; if (!cam) return; video_unregister_device(&cam->vdev); v4l2_device_unregister(&cam->v4l2_dev); msm8905_cam_hw_deinit(cam); msm8905_cam_regulators_disable(cam); if (cam->vdd_l17) regulator_put(cam->vdd_l17); if (cam->vdd_l6) regulator_put(cam->vdd_l6); iounmap(cam->vbif_base); iounmap(cam->camss_base); iounmap(cam->tlmm_base); iounmap(cam->gcc_base); for (i = 0; i < CAM_MAX_MMAP_BUFS; i++) vfree(cam->mmap_bufs[i]); dma_free_coherent(&cam->pdev->dev, CAM_DMA_ALLOC_BYTES, cam->raw_dma_buf, cam->raw_dma_phys); platform_device_unregister(cam->pdev); kfree(cam); g_cam = NULL; } module_init(msm8905_cam_init); module_exit(msm8905_cam_exit); MODULE_DESCRIPTION("Qualcomm MSM8905 / Nokia 8110 4G GC2395/GC2385/GC2365 V4L2 Camera Driver"); MODULE_AUTHOR("Bananarchy Project"); MODULE_LICENSE("GPL");