Files
bananarchy/src/msm8905-cam/msm8905-cam.c
T

1013 lines
31 KiB
C
Raw Normal View History

// SPDX-License-Identifier: GPL-2.0-only
/*
* Qualcomm MSM8905 / Nokia 8110 4G (nokia-argon) Rear Camera V4L2 Driver
*
* Drives the GalaxyCore GC2395 2MP MIPI CSI-2 sensor 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 <linux/delay.h>
#include <linux/dma-mapping.h>
#include <linux/fs.h>
#include <linux/io.h>
#include <linux/kernel.h>
#include <linux/mm.h>
#include <linux/module.h>
#include <linux/mutex.h>
#include <linux/of.h>
#include <linux/of_platform.h>
#include <linux/platform_device.h>
#include <linux/poll.h>
#include <linux/regmap.h>
#include <linux/slab.h>
#include <linux/videodev2.h>
#include <linux/vmalloc.h>
#include <media/v4l2-common.h>
#include <media/v4l2-device.h>
#include <media/v4l2-fh.h>
#include <media/v4l2-ioctl.h>
#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 LDO6 (1.8V VIO/CSI) and LDO17 (2.85V VANA) enable registers */
#define PM8909_LDO6_EN_CTL 0x4546U
#define PM8909_LDO17_EN_CTL 0x5046U
#define PM8909_REG_EN_BIT 0x80U
/* 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
struct gc2395_reg {
u8 reg;
u8 val;
};
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, 0x00 }, { 0x08, 0x20 }, { 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 },
};
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;
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;
bool streaming;
bool hw_initialized;
};
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 int msm8905_cam_hw_init(struct msm8905_cam_dev *cam)
{
size_t i;
u16 chip_id;
u32 r;
/* 1. Ensure PM8909 LDO6 (1.8V) and LDO17 (2.85V) are enabled */
if (cam->usid1_regmap) {
regmap_update_bits(cam->usid1_regmap, PM8909_LDO6_EN_CTL,
PM8909_REG_EN_BIT, PM8909_REG_EN_BIT);
regmap_update_bits(cam->usid1_regmap, PM8909_LDO17_EN_CTL,
PM8909_REG_EN_BIT, PM8909_REG_EN_BIT);
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 GC2395 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 != 0x2395U) {
dev_err(&cam->pdev->dev, "GC2395 chip ID mismatch: 0x%04x\n", chip_id);
return -ENODEV;
}
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)
{
u32 y, x;
u32 idx = 0;
for (y = 0; y < CAM_OUT_HEIGHT; y++) {
const u8 *r0 = raw + (2U * y) * CAM_RAW_STRIDE;
const u8 *r1 = raw + (2U * y + 1U) * CAM_RAW_STRIDE;
for (x = 0; x < CAM_OUT_WIDTH; x += 2U) {
u32 g_off = (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];
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;
}
/* 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 CID0 on frame boundary and start GC2395 MIPI stream */
cam_w32(cam->camss_base, ISPIF_BASE + 0x248U, 0xfffffdffU);
wmb();
gc2395_write_reg(cam, 0xfeU, 0x00U);
gc2395_write_reg(cam, 0xedU, 0x90U);
gc2395_write_reg(cam, 0xfeU, 0x00U);
/* Wait for WM0 PING_PONG_IRQ (bit 6 = 0x40) */
irq0 = 0;
for (poll_cnt = 0; poll_cnt < 150; poll_cnt++) {
irq0 = cam_r32(cam->camss_base, VFE0_BASE + 0x02cU);
if (irq0 & 0x00000040U)
break;
usleep_range(2000, 3000);
}
/* Immediately stop WM0, ISPIF RDI0, and GC2395 stream to prevent overflow */
cam_w32(cam->camss_base, ISPIF_BASE + 0x248U, 0xaaaaaaaaU);
cam_w32(cam->camss_base, VFE0_BASE + 0x04cU, 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();
gc2395_write_reg(cam, 0xfeU, 0x00U);
gc2395_write_reg(cam, 0xedU, 0x00U);
if (!(irq0 & 0x00000040U)) {
cam->hw_initialized = false;
return -ETIMEDOUT;
}
msm8905_cam_demosaic(cam->raw_dma_buf, dst_rgb);
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)
{
if (inp->index > 0)
return -EINVAL;
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->length = CAM_OUT_FRAME_BYTES;
ktime_get_ts64((struct timespec64 *)&b->timestamp);
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;
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 const struct v4l2_file_operations msm8905_cam_fops = {
.owner = THIS_MODULE,
.open = v4l2_fh_open,
.release = v4l2_fh_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);
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 800x600 RGB24, dma=0x%08llx)\n",
cam->vdev.num, (unsigned long long)cam->raw_dma_phys);
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);
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 V4L2 Camera Driver");
MODULE_AUTHOR("Bananarchy Project");
MODULE_LICENSE("GPL");