18 #include <linux/kernel.h>
19 #include <linux/module.h>
22 #include <linux/slab.h>
24 #include <linux/string.h>
41 .name =
"YUV 4:2:2 packed, YCbYCr",
55 #define NUM_FORMATS_ENC ARRAY_SIZE(formats_enc)
59 .name =
"YUV 4:2:0 planar, YCbCr",
68 .name =
"YUV 4:2:2 packed, YCbYCr",
83 #define NUM_FORMATS_DEC ARRAY_SIZE(formats_dec)
85 static const unsigned char qtbl_luminance[4][64] = {
87 8, 6, 6, 8, 12, 14, 16, 17,
88 6, 6, 6, 8, 10, 13, 12, 15,
89 6, 6, 7, 8, 13, 14, 18, 24,
90 8, 8, 8, 14, 13, 19, 24, 35,
91 12, 10, 13, 13, 20, 26, 34, 39,
92 14, 13, 14, 19, 26, 34, 39, 39,
93 16, 12, 18, 24, 34, 39, 39, 39,
94 17, 15, 24, 35, 39, 39, 39, 39
97 12, 8, 8, 12, 17, 21, 24, 23,
98 8, 9, 9, 11, 15, 19, 18, 23,
99 8, 9, 10, 12, 19, 20, 27, 36,
100 12, 11, 12, 21, 20, 28, 36, 53,
101 17, 15, 19, 20, 30, 39, 51, 59,
102 21, 19, 20, 28, 39, 51, 59, 59,
103 24, 18, 27, 36, 51, 59, 59, 59,
104 23, 23, 36, 53, 59, 59, 59, 59
107 16, 11, 11, 16, 23, 27, 31, 30,
108 11, 12, 12, 15, 20, 23, 23, 30,
109 11, 12, 13, 16, 23, 26, 35, 47,
110 16, 15, 16, 23, 26, 37, 47, 64,
111 23, 20, 23, 26, 39, 51, 64, 64,
112 27, 23, 26, 37, 51, 64, 64, 64,
113 31, 23, 35, 47, 64, 64, 64, 64,
114 30, 30, 47, 64, 64, 64, 64, 64
117 20, 16, 25, 39, 50, 46, 62, 68,
118 16, 18, 23, 38, 38, 53, 65, 68,
119 25, 23, 31, 38, 53, 65, 68, 68,
120 39, 38, 38, 53, 65, 68, 68, 68,
121 50, 38, 53, 65, 68, 68, 68, 68,
122 46, 53, 65, 68, 68, 68, 68, 68,
123 62, 65, 68, 68, 68, 68, 68, 68,
124 68, 68, 68, 68, 68, 68, 68, 68
128 static const unsigned char qtbl_chrominance[4][64] = {
130 9, 8, 9, 11, 14, 17, 19, 24,
131 8, 10, 9, 11, 14, 13, 17, 22,
132 9, 9, 13, 14, 13, 15, 23, 26,
133 11, 11, 14, 14, 15, 20, 26, 33,
134 14, 14, 13, 15, 20, 24, 33, 39,
135 17, 13, 15, 20, 24, 32, 39, 39,
136 19, 17, 23, 26, 33, 39, 39, 39,
137 24, 22, 26, 33, 39, 39, 39, 39
140 13, 11, 13, 16, 20, 20, 29, 37,
141 11, 14, 14, 14, 16, 20, 26, 32,
142 13, 14, 15, 17, 20, 23, 35, 40,
143 16, 14, 17, 21, 23, 30, 40, 50,
144 20, 16, 20, 23, 30, 37, 50, 59,
145 20, 20, 23, 30, 37, 48, 59, 59,
146 29, 26, 35, 40, 50, 59, 59, 59,
147 37, 32, 40, 50, 59, 59, 59, 59
150 17, 15, 17, 21, 20, 26, 38, 48,
151 15, 19, 18, 17, 20, 26, 35, 43,
152 17, 18, 20, 22, 26, 30, 46, 53,
153 21, 17, 22, 28, 30, 39, 53, 64,
154 20, 20, 26, 30, 39, 48, 64, 64,
155 26, 26, 30, 39, 48, 63, 64, 64,
156 38, 35, 46, 53, 64, 64, 64, 64,
157 48, 43, 53, 64, 64, 64, 64, 64
160 21, 25, 32, 38, 54, 68, 68, 68,
161 25, 28, 24, 38, 54, 68, 68, 68,
162 32, 24, 32, 43, 66, 68, 68, 68,
163 38, 38, 43, 53, 68, 68, 68, 68,
164 54, 54, 66, 68, 68, 68, 68, 68,
165 68, 68, 68, 68, 68, 68, 68, 68,
166 68, 68, 68, 68, 68, 68, 68, 68,
167 68, 68, 68, 68, 68, 68, 68, 68
171 static const unsigned char hdctbl0[16] = {
172 0, 1, 5, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0
175 static const unsigned char hdctblg0[12] = {
176 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0xa, 0xb
178 static const unsigned char hactbl0[16] = {
179 0, 2, 1, 3, 3, 2, 4, 3, 5, 5, 4, 4, 0, 0, 1, 0x7d
181 static const unsigned char hactblg0[162] = {
182 0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12,
183 0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07,
184 0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08,
185 0x23, 0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0,
186 0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0a, 0x16,
187 0x17, 0x18, 0x19, 0x1a, 0x25, 0x26, 0x27, 0x28,
188 0x29, 0x2a, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
189 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49,
190 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
191 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
192 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79,
193 0x7a, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89,
194 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
195 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
196 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6,
197 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5,
198 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4,
199 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2,
200 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea,
201 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
215 static inline void jpeg_set_qtbl(
void __iomem *
regs,
const unsigned char *qtbl,
216 unsigned long tab,
int len)
220 for (i = 0; i < len; i++)
221 writel((
unsigned int)qtbl[i], regs + tab + (i * 0x04));
224 static inline void jpeg_set_qtbl_lum(
void __iomem *regs,
int quality)
231 static inline void jpeg_set_qtbl_chr(
void __iomem *regs,
int quality)
238 static inline void jpeg_set_htbl(
void __iomem *regs,
const unsigned char *htbl,
239 unsigned long tab,
int len)
243 for (i = 0; i < len; i++)
244 writel((
unsigned int)htbl[i], regs + tab + (i * 0x04));
247 static inline void jpeg_set_hdctbl(
void __iomem *regs)
253 static inline void jpeg_set_hdctblg(
void __iomem *regs)
259 static inline void jpeg_set_hactbl(
void __iomem *regs)
265 static inline void jpeg_set_hactblg(
void __iomem *regs)
283 static int s5p_jpeg_open(
struct file *
file)
285 struct s5p_jpeg *jpeg = video_drvdata(file);
315 ret = s5p_jpeg_controls_create(ctx);
325 ctx->
out_q.fmt = out_fmt;
339 static int s5p_jpeg_release(
struct file *file)
341 struct s5p_jpeg *jpeg = video_drvdata(file);
355 static unsigned int s5p_jpeg_poll(
struct file *file,
358 struct s5p_jpeg *jpeg = video_drvdata(file);
368 static int s5p_jpeg_mmap(
struct file *file,
struct vm_area_struct *vma)
370 struct s5p_jpeg *jpeg = video_drvdata(file);
383 .open = s5p_jpeg_open,
384 .release = s5p_jpeg_release,
385 .poll = s5p_jpeg_poll,
387 .mmap = s5p_jpeg_mmap,
401 return ((
unsigned char *)buf->
data)[buf->
curr++];
416 *word = (
unsigned int)byte | temp;
432 int c, components, notfound;
438 jpeg_buffer.data =
buffer;
439 jpeg_buffer.curr = 0;
459 if (get_word_be(&jpeg_buffer, &word))
463 if (get_word_be(&jpeg_buffer, &height))
465 if (get_word_be(&jpeg_buffer, &width))
467 components =
get_byte(&jpeg_buffer);
468 if (components == -1)
472 skip(&jpeg_buffer, components * 3);
484 if (get_word_be(&jpeg_buffer, &word))
486 length = (
long)word - 2;
487 skip(&jpeg_buffer, length);
493 result->
size = components;
497 static int s5p_jpeg_querycap(
struct file *file,
void *
priv,
529 for (i = 0; i <
n; ++
i) {
530 if (formats[i].types & type) {
550 static int s5p_jpeg_enum_fmt_vid_cap(
struct file *file,
void *priv,
562 static int s5p_jpeg_enum_fmt_vid_out(
struct file *file,
void *priv,
585 static int s5p_jpeg_g_fmt(
struct file *file,
void *priv,
struct v4l2_format *f)
599 q_data = get_q_data(ct, f->
type);
609 if (q_data->
fmt->colplanes == 1)
610 bpl = (bpl * q_data->
fmt->depth) >> 3;
626 formats = formats_enc;
629 formats = formats_dec;
633 for (k = 0; k <
n; k++) {
635 if (fmt->
fourcc == pixelformat)
643 static void jpeg_bound_align_image(
u32 *
w,
unsigned int wmin,
unsigned int wmax,
645 u32 *
h,
unsigned int hmin,
unsigned int hmax,
653 w_step = 1 << walign;
654 h_step = 1 << halign;
657 if (*w < width && (*w + w_step) < wmax)
659 if (*h < height && (*h + h_step) < hmax)
694 if (fmt->
colplanes > 1 && bpl < pix->width)
708 static int s5p_jpeg_try_fmt_vid_cap(
struct file *file,
void *priv,
714 fmt = s5p_jpeg_find_format(ctx->
mode, f->
fmt.
pix.pixelformat);
717 "Fourcc format (0x%08x) invalid.\n",
725 static int s5p_jpeg_try_fmt_vid_out(
struct file *file,
void *priv,
731 fmt = s5p_jpeg_find_format(ctx->
mode, f->
fmt.
pix.pixelformat);
734 "Fourcc format (0x%08x) invalid.\n",
752 q_data = get_q_data(ct, f->
type);
755 if (vb2_is_busy(vq)) {
756 v4l2_err(&ct->
jpeg->v4l2_dev,
"%s queue busy\n", __func__);
764 q_data->
size = q_data->
w * q_data->
h * q_data->
fmt->depth >> 3;
771 static int s5p_jpeg_s_fmt_vid_cap(
struct file *file,
void *priv,
776 ret = s5p_jpeg_try_fmt_vid_cap(file, priv, f);
780 return s5p_jpeg_s_fmt(
fh_to_ctx(priv), f);
783 static int s5p_jpeg_s_fmt_vid_out(
struct file *file,
void *priv,
788 ret = s5p_jpeg_try_fmt_vid_out(file, priv, f);
792 return s5p_jpeg_s_fmt(
fh_to_ctx(priv), f);
795 static int s5p_jpeg_reqbufs(
struct file *file,
void *priv,
803 static int s5p_jpeg_querybuf(
struct file *file,
void *priv,
811 static int s5p_jpeg_qbuf(
struct file *file,
void *priv,
struct v4l2_buffer *buf)
818 static int s5p_jpeg_dqbuf(
struct file *file,
void *priv,
826 static int s5p_jpeg_streamon(
struct file *file,
void *priv,
834 static int s5p_jpeg_streamoff(
struct file *file,
void *priv,
842 static int s5p_jpeg_g_selection(
struct file *file,
void *priv,
858 s->
r.width = ctx->
out_q.w;
859 s->
r.height = ctx->
out_q.h;
863 s->
r.width = ctx->
cap_q.w;
864 s->
r.height = ctx->
cap_q.h;
893 spin_unlock_irqrestore(&jpeg->
slock, flags);
900 static int s5p_jpeg_s_ctrl(
struct v4l2_ctrl *ctrl)
919 spin_unlock_irqrestore(&ctx->
jpeg->slock, flags);
924 .g_volatile_ctrl = s5p_jpeg_g_volatile_ctrl,
925 .s_ctrl = s5p_jpeg_s_ctrl,
928 static int s5p_jpeg_controls_create(
struct s5p_jpeg_ctx *ctx)
930 unsigned int mask = ~0x27;
961 .vidioc_querycap = s5p_jpeg_querycap,
963 .vidioc_enum_fmt_vid_cap = s5p_jpeg_enum_fmt_vid_cap,
964 .vidioc_enum_fmt_vid_out = s5p_jpeg_enum_fmt_vid_out,
966 .vidioc_g_fmt_vid_cap = s5p_jpeg_g_fmt,
967 .vidioc_g_fmt_vid_out = s5p_jpeg_g_fmt,
969 .vidioc_try_fmt_vid_cap = s5p_jpeg_try_fmt_vid_cap,
970 .vidioc_try_fmt_vid_out = s5p_jpeg_try_fmt_vid_out,
972 .vidioc_s_fmt_vid_cap = s5p_jpeg_s_fmt_vid_cap,
973 .vidioc_s_fmt_vid_out = s5p_jpeg_s_fmt_vid_out,
975 .vidioc_reqbufs = s5p_jpeg_reqbufs,
976 .vidioc_querybuf = s5p_jpeg_querybuf,
978 .vidioc_qbuf = s5p_jpeg_qbuf,
979 .vidioc_dqbuf = s5p_jpeg_dqbuf,
981 .vidioc_streamon = s5p_jpeg_streamon,
982 .vidioc_streamoff = s5p_jpeg_streamoff,
984 .vidioc_g_selection = s5p_jpeg_g_selection,
993 static void s5p_jpeg_device_run(
void *priv)
1000 src_buf = v4l2_m2m_next_src_buf(ctx->
m2m_ctx);
1001 dst_buf = v4l2_m2m_next_dst_buf(ctx->
m2m_ctx);
1002 src_addr = vb2_dma_contig_plane_dma_addr(src_buf, 0);
1003 dst_addr = vb2_dma_contig_plane_dma_addr(dst_buf, 0);
1005 jpeg_reset(jpeg->
regs);
1006 jpeg_poweron(jpeg->
regs);
1007 jpeg_proc_mode(jpeg->
regs, ctx->
mode);
1017 jpeg_imgadr(jpeg->
regs, src_addr);
1018 jpeg_jpgadr(jpeg->
regs, dst_addr);
1021 jpeg_enc_stream_int(jpeg->
regs, ctx->
cap_q.size);
1041 jpeg_qtbl(jpeg->
regs, 1, 0);
1043 jpeg_qtbl(jpeg->
regs, 2, 1);
1044 jpeg_qtbl(jpeg->
regs, 3, 1);
1047 jpeg_htbl_ac(jpeg->
regs, 1);
1048 jpeg_htbl_dc(jpeg->
regs, 1);
1049 jpeg_htbl_ac(jpeg->
regs, 2);
1050 jpeg_htbl_dc(jpeg->
regs, 2);
1051 jpeg_htbl_ac(jpeg->
regs, 3);
1052 jpeg_htbl_dc(jpeg->
regs, 3);
1054 jpeg_rst_int_enable(jpeg->
regs,
true);
1055 jpeg_data_num_int_enable(jpeg->
regs,
true);
1056 jpeg_final_mcu_num_int_enable(jpeg->
regs,
true);
1061 jpeg_jpgadr(jpeg->
regs, src_addr);
1062 jpeg_imgadr(jpeg->
regs, dst_addr);
1068 static int s5p_jpeg_job_ready(
void *priv)
1077 static void s5p_jpeg_job_abort(
void *priv)
1082 .device_run = s5p_jpeg_device_run,
1083 .job_ready = s5p_jpeg_job_ready,
1084 .job_abort = s5p_jpeg_job_abort,
1093 static int s5p_jpeg_queue_setup(
struct vb2_queue *vq,
1095 unsigned int *nbuffers,
unsigned int *nplanes,
1096 unsigned int sizes[],
void *alloc_ctxs[])
1102 q_data = get_q_data(ctx, vq->
type);
1105 size = q_data->
size;
1117 alloc_ctxs[0] = ctx->
jpeg->alloc_ctx;
1122 static int s5p_jpeg_buf_prepare(
struct vb2_buffer *vb)
1127 q_data = get_q_data(ctx, vb->
vb2_queue->type);
1130 if (vb2_plane_size(vb, 0) < q_data->
size) {
1131 pr_err(
"%s data will not fit into plane (%lu < %lu)\n",
1132 __func__, vb2_plane_size(vb, 0),
1133 (
long)q_data->
size);
1137 vb2_set_plane_payload(vb, 0, q_data->
size);
1142 static void s5p_jpeg_buf_queue(
struct vb2_buffer *vb)
1151 min((
unsigned long)ctx->
out_q.size,
1152 vb2_get_plane_payload(vb, 0)));
1158 q_data = &ctx->
out_q;
1162 q_data = &ctx->
cap_q;
1171 q_data->
size = q_data->
w * q_data->
h * q_data->
fmt->depth >> 3;
1177 static void s5p_jpeg_wait_prepare(
struct vb2_queue *vq)
1184 static void s5p_jpeg_wait_finish(
struct vb2_queue *vq)
1191 static int s5p_jpeg_start_streaming(
struct vb2_queue *
q,
unsigned int count)
1196 ret = pm_runtime_get_sync(ctx->
jpeg->dev);
1198 return ret > 0 ? 0 :
ret;
1201 static int s5p_jpeg_stop_streaming(
struct vb2_queue *
q)
1205 pm_runtime_put(ctx->
jpeg->dev);
1210 static struct vb2_ops s5p_jpeg_qops = {
1211 .queue_setup = s5p_jpeg_queue_setup,
1212 .buf_prepare = s5p_jpeg_buf_prepare,
1213 .buf_queue = s5p_jpeg_buf_queue,
1214 .wait_prepare = s5p_jpeg_wait_prepare,
1215 .wait_finish = s5p_jpeg_wait_finish,
1216 .start_streaming = s5p_jpeg_start_streaming,
1217 .stop_streaming = s5p_jpeg_stop_streaming,
1220 static int queue_init(
void *priv,
struct vb2_queue *src_vq,
1230 src_vq->
ops = &s5p_jpeg_qops;
1241 dst_vq->
ops = &s5p_jpeg_qops;
1260 bool enc_jpeg_too_large =
false;
1261 bool timer_elapsed =
false;
1262 bool op_completed =
false;
1264 spin_lock(&jpeg->
slock);
1268 src_buf = v4l2_m2m_src_buf_remove(curr_ctx->
m2m_ctx);
1269 dst_buf = v4l2_m2m_dst_buf_remove(curr_ctx->
m2m_ctx);
1272 enc_jpeg_too_large = jpeg_enc_stream_stat(jpeg->
regs);
1273 timer_elapsed = jpeg_timer_stat(jpeg->
regs);
1274 op_completed = jpeg_result_stat_ok(jpeg->
regs);
1276 op_completed = op_completed && jpeg_stream_stat_ok(jpeg->
regs);
1278 if (enc_jpeg_too_large) {
1280 jpeg_clear_enc_stream_stat(jpeg->
regs);
1281 }
else if (timer_elapsed) {
1283 jpeg_clear_timer_stat(jpeg->
regs);
1284 }
else if (!op_completed) {
1287 payload_size = jpeg_compressed_size(jpeg->
regs);
1290 v4l2_m2m_buf_done(src_buf, state);
1292 vb2_set_plane_payload(dst_buf, 0, payload_size);
1293 v4l2_m2m_buf_done(dst_buf, state);
1297 spin_unlock(&jpeg->
slock);
1299 jpeg_clear_int(jpeg->
regs);
1330 dev_err(&pdev->
dev,
"Failed to obtain io memory\n");
1341 ret = devm_request_irq(&pdev->
dev, jpeg->
irq, s5p_jpeg_irq, 0,
1342 dev_name(&pdev->
dev), jpeg);
1350 if (IS_ERR(jpeg->
clk)) {
1352 ret = PTR_ERR(jpeg->
clk);
1356 clk_prepare_enable(jpeg->
clk);
1361 dev_err(&pdev->
dev,
"Failed to register v4l2 device\n");
1362 goto clk_get_rollback;
1370 goto device_register_rollback;
1377 goto m2m_init_rollback;
1385 goto vb2_allocator_rollback;
1390 jpeg->
vfd_encoder->ioctl_ops = &s5p_jpeg_ioctl_ops;
1400 goto enc_vdev_alloc_rollback;
1405 "encoder device registered as /dev/video%d\n",
1413 goto enc_vdev_register_rollback;
1418 jpeg->
vfd_decoder->ioctl_ops = &s5p_jpeg_ioctl_ops;
1427 goto dec_vdev_alloc_rollback;
1432 "decoder device registered as /dev/video%d\n",
1436 platform_set_drvdata(pdev, jpeg);
1444 dec_vdev_alloc_rollback:
1447 enc_vdev_register_rollback:
1450 enc_vdev_alloc_rollback:
1453 vb2_allocator_rollback:
1459 device_register_rollback:
1463 clk_disable_unprepare(jpeg->
clk);
1471 struct s5p_jpeg *jpeg = platform_get_drvdata(pdev);
1473 pm_runtime_disable(jpeg->
dev);
1483 clk_disable_unprepare(jpeg->
clk);
1489 static int s5p_jpeg_runtime_suspend(
struct device *
dev)
1494 static int s5p_jpeg_runtime_resume(
struct device *
dev)
1501 jpeg_set_hdctbl(jpeg->
regs);
1502 jpeg_set_hdctblg(jpeg->
regs);
1503 jpeg_set_hactbl(jpeg->
regs);
1504 jpeg_set_hactblg(jpeg->
regs);
1508 static const struct dev_pm_ops s5p_jpeg_pm_ops = {
1509 .runtime_suspend = s5p_jpeg_runtime_suspend,
1510 .runtime_resume = s5p_jpeg_runtime_resume,
1514 .probe = s5p_jpeg_probe,
1515 .remove = s5p_jpeg_remove,
1519 .pm = &s5p_jpeg_pm_ops,