Skip to content

Commit

Permalink
media: mtk-jpeg: refactor some variables
Browse files Browse the repository at this point in the history
Refactor some variables to reduce redundancy
and optimize the initialization process.

Signed-off-by: kyrie wu <kyrie.wu@mediatek.com>
Signed-off-by: irui wang <irui.wang@mediatek.com>
Signed-off-by: Hans Verkuil <hverkuil-cisco@xs4all.nl>
Signed-off-by: Mauro Carvalho Chehab <mchehab@kernel.org>
  • Loading branch information
kyriewu-hw authored and mchehab committed Apr 10, 2023
1 parent 6a7a883 commit 09aea13
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 44 deletions.
31 changes: 23 additions & 8 deletions drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -1004,8 +1004,8 @@ static void mtk_jpegenc_worker(struct work_struct *work)
retry_select:
hw_id = mtk_jpegenc_get_hw(ctx);
if (hw_id < 0) {
ret = wait_event_interruptible(jpeg->enc_hw_wq,
atomic_read(&jpeg->enchw_rdy) > 0);
ret = wait_event_interruptible(jpeg->hw_wq,
atomic_read(&jpeg->hw_rdy) > 0);
if (ret != 0 || (i++ > MTK_JPEG_MAX_RETRY_TIME)) {
dev_err(jpeg->dev, "%s : %d, all HW are busy\n",
__func__, __LINE__);
Expand All @@ -1016,7 +1016,7 @@ static void mtk_jpegenc_worker(struct work_struct *work)
goto retry_select;
}

atomic_dec(&jpeg->enchw_rdy);
atomic_dec(&jpeg->hw_rdy);
src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
if (!src_buf)
goto getbuf_fail;
Expand Down Expand Up @@ -1073,7 +1073,7 @@ static void mtk_jpegenc_worker(struct work_struct *work)
v4l2_m2m_buf_done(src_buf, buf_state);
v4l2_m2m_buf_done(dst_buf, buf_state);
getbuf_fail:
atomic_inc(&jpeg->enchw_rdy);
atomic_inc(&jpeg->hw_rdy);
mtk_jpegenc_put_hw(jpeg, hw_id);
v4l2_m2m_job_finish(jpeg->m2m_dev, ctx->fh.m2m_ctx);
}
Expand Down Expand Up @@ -1198,8 +1198,8 @@ static void mtk_jpegdec_worker(struct work_struct *work)
retry_select:
hw_id = mtk_jpegdec_get_hw(ctx);
if (hw_id < 0) {
ret = wait_event_interruptible_timeout(jpeg->dec_hw_wq,
atomic_read(&jpeg->dechw_rdy) > 0,
ret = wait_event_interruptible_timeout(jpeg->hw_wq,
atomic_read(&jpeg->hw_rdy) > 0,
MTK_JPEG_HW_TIMEOUT_MSEC);
if (ret != 0 || (i++ > MTK_JPEG_MAX_RETRY_TIME)) {
dev_err(jpeg->dev, "%s : %d, all HW are busy\n",
Expand All @@ -1211,7 +1211,7 @@ static void mtk_jpegdec_worker(struct work_struct *work)
goto retry_select;
}

atomic_dec(&jpeg->dechw_rdy);
atomic_dec(&jpeg->hw_rdy);
src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
if (!src_buf)
goto getbuf_fail;
Expand Down Expand Up @@ -1290,7 +1290,7 @@ static void mtk_jpegdec_worker(struct work_struct *work)
v4l2_m2m_buf_done(src_buf, buf_state);
v4l2_m2m_buf_done(dst_buf, buf_state);
getbuf_fail:
atomic_inc(&jpeg->dechw_rdy);
atomic_inc(&jpeg->hw_rdy);
mtk_jpegdec_put_hw(jpeg, hw_id);
v4l2_m2m_job_finish(jpeg->m2m_dev, ctx->fh.m2m_ctx);
}
Expand Down Expand Up @@ -1710,6 +1710,8 @@ static int mtk_jpeg_single_core_init(struct platform_device *pdev,
static int mtk_jpeg_probe(struct platform_device *pdev)
{
struct mtk_jpeg_dev *jpeg;
struct device_node *child;
int num_child = 0;
int ret;

jpeg = devm_kzalloc(&pdev->dev, sizeof(*jpeg), GFP_KERNEL);
Expand All @@ -1733,6 +1735,19 @@ static int mtk_jpeg_probe(struct platform_device *pdev)
v4l2_err(&jpeg->v4l2_dev, "mtk_jpeg_single_core_init failed.");
return -EINVAL;
}
} else {
init_waitqueue_head(&jpeg->hw_wq);

for_each_child_of_node(pdev->dev.of_node, child)
num_child++;

atomic_set(&jpeg->hw_rdy, num_child);

jpeg->workqueue = alloc_ordered_workqueue(MTK_JPEG_NAME,
WQ_MEM_RECLAIM
| WQ_FREEZABLE);
if (!jpeg->workqueue)
return -EINVAL;
}

ret = v4l2_device_register(&pdev->dev, &jpeg->v4l2_dev);
Expand Down
18 changes: 6 additions & 12 deletions drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -203,14 +203,11 @@ struct mtk_jpegdec_comp_dev {
* @job_timeout_work: IRQ timeout structure
* @variant: driver variant to be used
* @reg_encbase: jpg encode register base addr
* @enc_hw_dev: jpg encode hardware device
* @enc_hw_wq: jpg encode wait queue
* @enchw_rdy: jpg encode hw ready flag
* @enc_hw_dev: jpg encode hardware device
* @hw_wq: jpg wait queue
* @hw_rdy: jpg hw ready flag
* @reg_decbase: jpg decode register base addr
* @dec_hw_dev: jpg decode hardware device
* @dec_hw_wq: jpg decode wait queue
* @dec_workqueue: jpg decode work queue
* @dechw_rdy: jpg decode hw ready flag
* @dec_hw_dev: jpg decode hardware device
*/
struct mtk_jpeg_dev {
struct mutex lock;
Expand All @@ -227,14 +224,11 @@ struct mtk_jpeg_dev {

void __iomem *reg_encbase[MTK_JPEGENC_HW_MAX];
struct mtk_jpegenc_comp_dev *enc_hw_dev[MTK_JPEGENC_HW_MAX];
wait_queue_head_t enc_hw_wq;
atomic_t enchw_rdy;
wait_queue_head_t hw_wq;
atomic_t hw_rdy;

void __iomem *reg_decbase[MTK_JPEGDEC_HW_MAX];
struct mtk_jpegdec_comp_dev *dec_hw_dev[MTK_JPEGDEC_HW_MAX];
wait_queue_head_t dec_hw_wq;
struct workqueue_struct *dec_workqueue;
atomic_t dechw_rdy;
};

/**
Expand Down
16 changes: 4 additions & 12 deletions drivers/media/platform/mediatek/jpeg/mtk_jpeg_dec_hw.c
Original file line number Diff line number Diff line change
Expand Up @@ -503,8 +503,8 @@ static void mtk_jpegdec_timeout_work(struct work_struct *work)
clk_disable_unprepare(cjpeg->jdec_clk.clks->clk);
pm_runtime_put(cjpeg->dev);
cjpeg->hw_state = MTK_JPEG_HW_IDLE;
atomic_inc(&master_jpeg->dechw_rdy);
wake_up(&master_jpeg->dec_hw_wq);
atomic_inc(&master_jpeg->hw_rdy);
wake_up(&master_jpeg->hw_wq);
v4l2_m2m_buf_done(src_buf, buf_state);
mtk_jpegdec_put_buf(cjpeg);
}
Expand Down Expand Up @@ -551,8 +551,8 @@ static irqreturn_t mtk_jpegdec_hw_irq_handler(int irq, void *priv)
clk_disable_unprepare(jpeg->jdec_clk.clks->clk);

jpeg->hw_state = MTK_JPEG_HW_IDLE;
wake_up(&master_jpeg->dec_hw_wq);
atomic_inc(&master_jpeg->dechw_rdy);
wake_up(&master_jpeg->hw_wq);
atomic_inc(&master_jpeg->hw_rdy);

return IRQ_HANDLED;
}
Expand Down Expand Up @@ -608,20 +608,12 @@ static int mtk_jpegdec_hw_probe(struct platform_device *pdev)
dev->plat_dev = pdev;
dev->dev = &pdev->dev;

init_waitqueue_head(&master_dev->dec_hw_wq);
master_dev->workqueue = alloc_ordered_workqueue(MTK_JPEG_NAME,
WQ_MEM_RECLAIM
| WQ_FREEZABLE);
if (!master_dev->workqueue)
return -EINVAL;

ret = devm_add_action_or_reset(&pdev->dev,
mtk_jpegdec_destroy_workqueue,
master_dev->workqueue);
if (ret)
return ret;

atomic_set(&master_dev->dechw_rdy, MTK_JPEGDEC_HW_MAX);
spin_lock_init(&dev->hw_lock);
dev->hw_state = MTK_JPEG_HW_IDLE;

Expand Down
16 changes: 4 additions & 12 deletions drivers/media/platform/mediatek/jpeg/mtk_jpeg_enc_hw.c
Original file line number Diff line number Diff line change
Expand Up @@ -248,8 +248,8 @@ static void mtk_jpegenc_timeout_work(struct work_struct *work)
clk_disable_unprepare(cjpeg->venc_clk.clks->clk);
pm_runtime_put(cjpeg->dev);
cjpeg->hw_state = MTK_JPEG_HW_IDLE;
atomic_inc(&master_jpeg->enchw_rdy);
wake_up(&master_jpeg->enc_hw_wq);
atomic_inc(&master_jpeg->hw_rdy);
wake_up(&master_jpeg->hw_wq);
v4l2_m2m_buf_done(src_buf, buf_state);
mtk_jpegenc_put_buf(cjpeg);
}
Expand Down Expand Up @@ -288,8 +288,8 @@ static irqreturn_t mtk_jpegenc_hw_irq_handler(int irq, void *priv)
clk_disable_unprepare(jpeg->venc_clk.clks->clk);

jpeg->hw_state = MTK_JPEG_HW_IDLE;
wake_up(&master_jpeg->enc_hw_wq);
atomic_inc(&master_jpeg->enchw_rdy);
wake_up(&master_jpeg->hw_wq);
atomic_inc(&master_jpeg->hw_rdy);

return IRQ_HANDLED;
}
Expand Down Expand Up @@ -340,14 +340,6 @@ static int mtk_jpegenc_hw_probe(struct platform_device *pdev)
dev->plat_dev = pdev;
dev->dev = &pdev->dev;

init_waitqueue_head(&master_dev->enc_hw_wq);
master_dev->workqueue = alloc_ordered_workqueue(MTK_JPEG_NAME,
WQ_MEM_RECLAIM
| WQ_FREEZABLE);
if (!master_dev->workqueue)
return -EINVAL;

atomic_set(&master_dev->enchw_rdy, MTK_JPEGENC_HW_MAX);
spin_lock_init(&dev->hw_lock);
dev->hw_state = MTK_JPEG_HW_IDLE;

Expand Down

0 comments on commit 09aea13

Please sign in to comment.