Lines Matching +full:vga +full:- +full:format

1 // SPDX-License-Identifier: GPL-2.0-only
18 #include <media/v4l2-device.h>
19 #include <media/v4l2-ioctl.h>
20 #include <media/v4l2-ctrls.h>
21 #include <media/v4l2-event.h>
22 #include <media/v4l2-image-sizes.h>
24 #include <media/videobuf2-dma-sg.h>
26 #include <linux/dma-mapping.h>
28 #include <linux/via-core.h>
29 #include <linux/via-gpio.h>
38 #include "via-camera.h"
40 MODULE_ALIAS("platform:viafb-camera");
42 MODULE_DESCRIPTION("VIA framebuffer-based camera controller driver");
53 …efuse to load if the XO 1.5 serial port is enabled. Set this option to force-enable the camera.");
88 u8 __iomem *cb_addrs[3]; /* Kernel-space addresses */
94 * Video format information. sensor_format is kept in a form
96 * sensor in VGA resolution, though, and let the controller
107 /* common v4l buffer stuff -- must be first */
129 v4l2_subdev_call(cam->sensor, optype, func, ##args)
135 dev_err(&(cam)->platdev->dev, fmt, ##arg)
137 dev_warn(&(cam)->platdev->dev, fmt, ##arg)
139 dev_dbg(&(cam)->platdev->dev, fmt, ##arg)
142 * Format handling. This is ripped almost directly from Hans's changes
144 * didn't need to know anything about the format except its byte depth;
171 /* Not found? Then return the first format. */ in via_find_format()
176 /*--------------------------------------------------------------------------*/
178 * Sensor power/reset management. This piece is OLPC-specific for
185 cam->power_gpio = viafb_gpio_lookup("VGPIO3"); in via_sensor_power_setup()
186 cam->reset_gpio = viafb_gpio_lookup("VGPIO2"); in via_sensor_power_setup()
187 if (!gpio_is_valid(cam->power_gpio) || !gpio_is_valid(cam->reset_gpio)) { in via_sensor_power_setup()
188 dev_err(&cam->platdev->dev, "Unable to find GPIO lines\n"); in via_sensor_power_setup()
189 return -EINVAL; in via_sensor_power_setup()
191 ret = gpio_request(cam->power_gpio, "viafb-camera"); in via_sensor_power_setup()
193 dev_err(&cam->platdev->dev, "Unable to request power GPIO\n"); in via_sensor_power_setup()
196 ret = gpio_request(cam->reset_gpio, "viafb-camera"); in via_sensor_power_setup()
198 dev_err(&cam->platdev->dev, "Unable to request reset GPIO\n"); in via_sensor_power_setup()
199 gpio_free(cam->power_gpio); in via_sensor_power_setup()
202 gpio_direction_output(cam->power_gpio, 0); in via_sensor_power_setup()
203 gpio_direction_output(cam->reset_gpio, 0); in via_sensor_power_setup()
212 gpio_set_value(cam->power_gpio, 1); in via_sensor_power_up()
213 gpio_set_value(cam->reset_gpio, 0); in via_sensor_power_up()
215 gpio_set_value(cam->reset_gpio, 1); in via_sensor_power_up()
221 gpio_set_value(cam->power_gpio, 0); in via_sensor_power_down()
222 gpio_set_value(cam->reset_gpio, 0); in via_sensor_power_down()
229 gpio_free(cam->power_gpio); in via_sensor_power_release()
230 gpio_free(cam->reset_gpio); in via_sensor_power_release()
233 /* --------------------------------------------------------------------------*/
246 return v4l2_s_ctrl(NULL, cam->sensor->ctrl_handler, &ctrl); in viacam_set_flip()
255 struct v4l2_subdev_format format = { in viacam_configure_sensor() local
260 v4l2_fill_mbus_format(&format.format, &cam->sensor_format, cam->mbus_code); in viacam_configure_sensor()
263 ret = sensor_call(cam, pad, set_fmt, NULL, &format); in viacam_configure_sensor()
265 * OV7670 does weird things if flip is set *before* format... in viacam_configure_sensor()
274 /* --------------------------------------------------------------------------*/
280 * 0x300-350 range.
285 iowrite32(value, cam->mmio + reg); in viacam_write_reg()
290 return ioread32(cam->mmio + reg); in viacam_read_reg()
303 /* --------------------------------------------------------------------------*/
316 spin_lock(&cam->viadev->reg_lock); in viacam_quick_irq()
323 spin_unlock(&cam->viadev->reg_lock); in viacam_quick_irq()
332 if (cam->opstate != S_RUNNING) in viacam_next_buffer()
334 if (list_empty(&cam->buffer_queue)) in viacam_next_buffer()
336 return list_entry(cam->buffer_queue.next, struct via_buffer, queue); in viacam_next_buffer()
349 mutex_lock(&cam->lock); in viacam_irq()
361 bufn -= 1; in viacam_irq()
363 bufn = cam->n_cap_bufs - 1; in viacam_irq()
367 sgt = vb2_dma_sg_plane_desc(&vb->vbuf.vb2_buf, 0); in viacam_irq()
368 vb->vbuf.vb2_buf.timestamp = ktime_get_ns(); in viacam_irq()
369 viafb_dma_copy_out_sg(cam->cb_offsets[bufn], sgt->sgl, sgt->nents); in viacam_irq()
370 vb->vbuf.sequence = cam->sequence++; in viacam_irq()
371 vb->vbuf.field = V4L2_FIELD_NONE; in viacam_irq()
372 list_del(&vb->queue); in viacam_irq()
373 vb2_buffer_done(&vb->vbuf.vb2_buf, VB2_BUF_STATE_DONE); in viacam_irq()
375 mutex_unlock(&cam->lock); in viacam_irq()
385 * the via-core level.
402 /* --------------------------------------------------------------------------*/
410 int nbuf = cam->viadev->camera_fbmem_size/cam->sensor_format.sizeimage; in viacam_ctlr_cbufs()
418 cam->n_cap_bufs = 3; in viacam_ctlr_cbufs()
422 cam->n_cap_bufs = 2; in viacam_ctlr_cbufs()
426 return -ENOMEM; in viacam_ctlr_cbufs()
431 offset = cam->fb_offset; in viacam_ctlr_cbufs()
432 for (i = 0; i < cam->n_cap_bufs; i++) { in viacam_ctlr_cbufs()
433 cam->cb_offsets[i] = offset; in viacam_ctlr_cbufs()
434 cam->cb_addrs[i] = cam->fbmem + offset; in viacam_ctlr_cbufs()
436 offset += cam->sensor_format.sizeimage; in viacam_ctlr_cbufs()
455 * the 3x4 VGA scaling factor, the image will distort. We
463 if (cam->user_format.width == VGA_WIDTH) in viacam_set_scale()
466 sf = (cam->user_format.width*2048)/VGA_WIDTH; in viacam_set_scale()
469 if (cam->user_format.height < VGA_HEIGHT) { in viacam_set_scale()
470 sf = (1024*cam->user_format.height)/VGA_HEIGHT; in viacam_set_scale()
478 * Configure image-related information into the capture engine.
485 * Disable clock before messing with stuff - from the via in viacam_ctlr_image()
490 * Set up the controller for VGA resolution, modulo magic in viacam_ctlr_image()
500 (cam->sensor_format.height << 16) | in viacam_ctlr_image()
501 (cam->sensor_format.bytesperline >> 3)); in viacam_ctlr_image()
504 cam->user_format.bytesperline & VCR_VS_STRIDE); in viacam_ctlr_image()
514 VCR_CI_FLDINV | /* OLPC-specific? */ in viacam_ctlr_image()
515 VCR_CI_VREFINV | /* OLPC-specific? */ in viacam_ctlr_image()
518 if (cam->n_cap_bufs == 3) in viacam_ctlr_image()
523 if (cam->user_format.pixelformat == V4L2_PIX_FMT_YUYV) in viacam_ctlr_image()
536 spin_lock_irqsave(&cam->viadev->reg_lock, flags); in viacam_config_controller()
540 spin_unlock_irqrestore(&cam->viadev->reg_lock, flags); in viacam_config_controller()
541 clear_bit(CF_CONFIG_NEEDED, &cam->flags); in viacam_config_controller()
550 spin_lock_irq(&cam->viadev->reg_lock); in viacam_start_engine()
554 cam->opstate = S_RUNNING; in viacam_start_engine()
555 spin_unlock_irq(&cam->viadev->reg_lock); in viacam_start_engine()
561 spin_lock_irq(&cam->viadev->reg_lock); in viacam_stop_engine()
565 cam->opstate = S_IDLE; in viacam_stop_engine()
566 spin_unlock_irq(&cam->viadev->reg_lock); in viacam_stop_engine()
570 /* --------------------------------------------------------------------------*/
582 struct via_camera *cam = vb2_get_drv_priv(vb->vb2_queue); in viacam_vb2_queue()
585 list_add_tail(&via->queue, &cam->buffer_queue); in viacam_vb2_queue()
590 struct via_camera *cam = vb2_get_drv_priv(vb->vb2_queue); in viacam_vb2_prepare()
592 if (vb2_plane_size(vb, 0) < cam->user_format.sizeimage) { in viacam_vb2_prepare()
596 cam->user_format.sizeimage); in viacam_vb2_prepare()
597 return -EINVAL; in viacam_vb2_prepare()
600 vb2_set_plane_payload(vb, 0, cam->user_format.sizeimage); in viacam_vb2_prepare()
611 int size = cam->user_format.sizeimage; in viacam_vb2_queue_setup()
614 return sizes[0] < size ? -EINVAL : 0; in viacam_vb2_queue_setup()
627 if (cam->opstate != S_IDLE) { in viacam_vb2_start_streaming()
628 ret = -EBUSY; in viacam_vb2_start_streaming()
634 if (test_bit(CF_CONFIG_NEEDED, &cam->flags)) { in viacam_vb2_start_streaming()
642 cam->sequence = 0; in viacam_vb2_start_streaming()
649 cpu_latency_qos_add_request(&cam->qos_request, 50); in viacam_vb2_start_streaming()
653 list_for_each_entry_safe(buf, tmp, &cam->buffer_queue, queue) { in viacam_vb2_start_streaming()
654 list_del(&buf->queue); in viacam_vb2_start_streaming()
655 vb2_buffer_done(&buf->vbuf.vb2_buf, VB2_BUF_STATE_QUEUED); in viacam_vb2_start_streaming()
665 cpu_latency_qos_remove_request(&cam->qos_request); in viacam_vb2_stop_streaming()
668 list_for_each_entry_safe(buf, tmp, &cam->buffer_queue, queue) { in viacam_vb2_stop_streaming()
669 list_del(&buf->queue); in viacam_vb2_stop_streaming()
670 vb2_buffer_done(&buf->vbuf.vb2_buf, VB2_BUF_STATE_ERROR); in viacam_vb2_stop_streaming()
684 /* --------------------------------------------------------------------------*/
696 mutex_lock(&cam->lock); in viacam_open()
708 set_bit(CF_CONFIG_NEEDED, &cam->flags); in viacam_open()
711 mutex_unlock(&cam->lock); in viacam_open()
720 mutex_lock(&cam->lock); in viacam_release()
730 mutex_unlock(&cam->lock); in viacam_release()
744 /*----------------------------------------------------------------------------*/
755 if (input->index != 0) in viacam_enum_input()
756 return -EINVAL; in viacam_enum_input()
758 input->type = V4L2_INPUT_TYPE_CAMERA; in viacam_enum_input()
759 strscpy(input->name, "Camera", sizeof(input->name)); in viacam_enum_input()
772 return -EINVAL; in viacam_s_input()
777 * Video format stuff. Here is our default format until
795 if (fmt->index >= N_VIA_FMTS) in viacam_enum_fmt_vid_cap()
796 return -EINVAL; in viacam_enum_fmt_vid_cap()
797 fmt->pixelformat = via_formats[fmt->index].pixelformat; in viacam_enum_fmt_vid_cap()
803 * sensor to VGA.
809 if (userfmt->width < QCIF_WIDTH || userfmt->height < QCIF_HEIGHT) { in viacam_fmt_pre()
810 userfmt->width = QCIF_WIDTH; in viacam_fmt_pre()
811 userfmt->height = QCIF_HEIGHT; in viacam_fmt_pre()
813 if (userfmt->width > VGA_WIDTH || userfmt->height > VGA_HEIGHT) { in viacam_fmt_pre()
814 userfmt->width = VGA_WIDTH; in viacam_fmt_pre()
815 userfmt->height = VGA_HEIGHT; in viacam_fmt_pre()
817 sensorfmt->width = VGA_WIDTH; in viacam_fmt_pre()
818 sensorfmt->height = VGA_HEIGHT; in viacam_fmt_pre()
824 struct via_format *f = via_find_format(userfmt->pixelformat); in viacam_fmt_post()
826 sensorfmt->bytesperline = sensorfmt->width * f->bpp; in viacam_fmt_post()
827 sensorfmt->sizeimage = sensorfmt->height * sensorfmt->bytesperline; in viacam_fmt_post()
828 userfmt->pixelformat = sensorfmt->pixelformat; in viacam_fmt_post()
829 userfmt->field = sensorfmt->field; in viacam_fmt_post()
830 userfmt->bytesperline = 2 * userfmt->width; in viacam_fmt_post()
831 userfmt->sizeimage = userfmt->bytesperline * userfmt->height; in viacam_fmt_post()
832 userfmt->colorspace = sensorfmt->colorspace; in viacam_fmt_post()
833 userfmt->ycbcr_enc = sensorfmt->ycbcr_enc; in viacam_fmt_post()
834 userfmt->quantization = sensorfmt->quantization; in viacam_fmt_post()
835 userfmt->xfer_func = sensorfmt->xfer_func; in viacam_fmt_post()
840 * The real work of figuring out a workable format.
850 struct v4l2_subdev_format format = { in viacam_do_try_fmt() local
853 struct via_format *f = via_find_format(upix->pixelformat); in viacam_do_try_fmt()
855 upix->pixelformat = f->pixelformat; in viacam_do_try_fmt()
857 v4l2_fill_mbus_format(&format.format, spix, f->mbus_code); in viacam_do_try_fmt()
858 ret = sensor_call(cam, pad, set_fmt, &pad_state, &format); in viacam_do_try_fmt()
859 v4l2_fill_pix_format(spix, &format.format); in viacam_do_try_fmt()
872 return viacam_do_try_fmt(cam, &fmt->fmt.pix, &sfmt.fmt.pix); in viacam_try_fmt_vid_cap()
881 fmt->fmt.pix = cam->user_format; in viacam_g_fmt_vid_cap()
891 struct via_format *f = via_find_format(fmt->fmt.pix.pixelformat); in viacam_s_fmt_vid_cap()
897 if (cam->opstate != S_IDLE) in viacam_s_fmt_vid_cap()
898 return -EBUSY; in viacam_s_fmt_vid_cap()
903 ret = viacam_do_try_fmt(cam, &fmt->fmt.pix, &sfmt.fmt.pix); in viacam_s_fmt_vid_cap()
907 * OK, let's commit to the new format. in viacam_s_fmt_vid_cap()
909 cam->user_format = fmt->fmt.pix; in viacam_s_fmt_vid_cap()
910 cam->sensor_format = sfmt.fmt.pix; in viacam_s_fmt_vid_cap()
911 cam->mbus_code = f->mbus_code; in viacam_s_fmt_vid_cap()
921 strscpy(cap->driver, "via-camera", sizeof(cap->driver)); in viacam_querycap()
922 strscpy(cap->card, "via-camera", sizeof(cap->card)); in viacam_querycap()
923 strscpy(cap->bus_info, "platform:via-camera", sizeof(cap->bus_info)); in viacam_querycap()
934 return v4l2_g_parm_cap(video_devdata(filp), cam->sensor, parm); in viacam_g_parm()
942 return v4l2_s_parm_cap(video_devdata(filp), cam->sensor, parm); in viacam_s_parm()
950 if (sizes->index != 0) in viacam_enum_framesizes()
951 return -EINVAL; in viacam_enum_framesizes()
953 if (sizes->pixel_format == via_formats[i].pixelformat) in viacam_enum_framesizes()
956 return -EINVAL; in viacam_enum_framesizes()
957 sizes->type = V4L2_FRMSIZE_TYPE_CONTINUOUS; in viacam_enum_framesizes()
958 sizes->stepwise.min_width = QCIF_WIDTH; in viacam_enum_framesizes()
959 sizes->stepwise.min_height = QCIF_HEIGHT; in viacam_enum_framesizes()
960 sizes->stepwise.max_width = VGA_WIDTH; in viacam_enum_framesizes()
961 sizes->stepwise.max_height = VGA_HEIGHT; in viacam_enum_framesizes()
962 sizes->stepwise.step_width = sizes->stepwise.step_height = 1; in viacam_enum_framesizes()
971 .index = interval->index, in viacam_enum_frameintervals()
972 .code = cam->mbus_code, in viacam_enum_frameintervals()
973 .width = cam->sensor_format.width, in viacam_enum_frameintervals()
974 .height = cam->sensor_format.height, in viacam_enum_frameintervals()
981 if (interval->pixel_format == via_formats[i].pixelformat) in viacam_enum_frameintervals()
984 return -EINVAL; in viacam_enum_frameintervals()
985 if (interval->width < QCIF_WIDTH || interval->width > VGA_WIDTH || in viacam_enum_frameintervals()
986 interval->height < QCIF_HEIGHT || interval->height > VGA_HEIGHT) in viacam_enum_frameintervals()
987 return -EINVAL; in viacam_enum_frameintervals()
991 interval->type = V4L2_FRMIVAL_TYPE_DISCRETE; in viacam_enum_frameintervals()
992 interval->discrete = fie.interval; in viacam_enum_frameintervals()
1022 /*----------------------------------------------------------------------------*/
1032 enum viacam_opstate state = cam->opstate; in viacam_suspend()
1034 if (cam->opstate != S_IDLE) { in viacam_suspend()
1036 cam->opstate = state; /* So resume restarts */ in viacam_suspend()
1053 set_bit(CF_CONFIG_NEEDED, &cam->flags); in viacam_resume()
1057 if (!list_empty(&cam->vdev.fh_list)) in viacam_resume()
1064 if (cam->opstate != S_IDLE) { in viacam_resume()
1065 mutex_lock(&cam->lock); in viacam_resume()
1069 mutex_unlock(&cam->lock); in viacam_resume()
1089 .name = "via-camera",
1090 .minor = -1,
1133 /* The XO-1.5 (only known user) clocks the camera at 90MHz. */
1141 struct viafb_dev *viadev = pdev->dev.platform_data; in viacam_probe()
1160 * with less than two frames of VGA 16-bit data. in viacam_probe()
1165 if (viadev->camera_fbmem_size < (VGA_HEIGHT*VGA_WIDTH*4)) { in viacam_probe()
1167 return -ENOMEM; in viacam_probe()
1169 if (viadev->engine_mmio == NULL) { in viacam_probe()
1171 return -ENOMEM; in viacam_probe()
1175 return -EBUSY; in viacam_probe()
1182 return -ENOMEM; in viacam_probe()
1184 cam->platdev = pdev; in viacam_probe()
1185 cam->viadev = viadev; in viacam_probe()
1186 cam->opstate = S_IDLE; in viacam_probe()
1187 cam->user_format = cam->sensor_format = viacam_def_pix_format; in viacam_probe()
1188 mutex_init(&cam->lock); in viacam_probe()
1189 INIT_LIST_HEAD(&cam->buffer_queue); in viacam_probe()
1190 cam->mmio = viadev->engine_mmio; in viacam_probe()
1191 cam->fbmem = viadev->fbmem; in viacam_probe()
1192 cam->fb_offset = viadev->camera_fbmem_offset; in viacam_probe()
1193 cam->flags = 1 << CF_CONFIG_NEEDED; in viacam_probe()
1194 cam->mbus_code = via_def_mbus_code; in viacam_probe()
1198 ret = v4l2_device_register(&pdev->dev, &cam->v4l2_dev); in viacam_probe()
1200 dev_err(&pdev->dev, "Unable to register v4l2 device\n"); in viacam_probe()
1203 ret = v4l2_ctrl_handler_init(&cam->ctrl_handler, 10); in viacam_probe()
1206 cam->v4l2_dev.ctrl_handler = &cam->ctrl_handler; in viacam_probe()
1210 pdev->dev.dma_mask = &viadev->pdev->dma_mask; in viacam_probe()
1211 dma_set_mask(&pdev->dev, 0xffffffff); in viacam_probe()
1228 * is OLPC-specific. 0x42 assumption is ov7670-specific. in viacam_probe()
1231 cam->sensor = v4l2_i2c_new_subdev_board(&cam->v4l2_dev, sensor_adapter, in viacam_probe()
1233 if (cam->sensor == NULL) { in viacam_probe()
1234 dev_err(&pdev->dev, "Unable to find the sensor!\n"); in viacam_probe()
1235 ret = -ENODEV; in viacam_probe()
1242 ret = request_threaded_irq(viadev->pdev->irq, viacam_quick_irq, in viacam_probe()
1243 viacam_irq, IRQF_SHARED, "via-camera", cam); in viacam_probe()
1247 vq = &cam->vq; in viacam_probe()
1248 vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; in viacam_probe()
1249 vq->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF | VB2_READ; in viacam_probe()
1250 vq->drv_priv = cam; in viacam_probe()
1251 vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC; in viacam_probe()
1252 vq->buf_struct_size = sizeof(struct via_buffer); in viacam_probe()
1253 vq->dev = cam->v4l2_dev.dev; in viacam_probe()
1255 vq->ops = &viacam_vb2_ops; in viacam_probe()
1256 vq->mem_ops = &vb2_dma_sg_memops; in viacam_probe()
1257 vq->lock = &cam->lock; in viacam_probe()
1263 cam->vdev = viacam_v4l_template; in viacam_probe()
1264 cam->vdev.v4l2_dev = &cam->v4l2_dev; in viacam_probe()
1265 cam->vdev.lock = &cam->lock; in viacam_probe()
1266 cam->vdev.queue = vq; in viacam_probe()
1267 video_set_drvdata(&cam->vdev, cam); in viacam_probe()
1268 ret = video_register_device(&cam->vdev, VFL_TYPE_VIDEO, -1); in viacam_probe()
1285 free_irq(viadev->pdev->irq, cam); in viacam_probe()
1289 v4l2_ctrl_handler_free(&cam->ctrl_handler); in viacam_probe()
1291 v4l2_device_unregister(&cam->v4l2_dev); in viacam_probe()
1300 struct viafb_dev *viadev = pdev->dev.platform_data; in viacam_remove()
1302 video_unregister_device(&cam->vdev); in viacam_remove()
1303 v4l2_device_unregister(&cam->v4l2_dev); in viacam_remove()
1307 free_irq(viadev->pdev->irq, cam); in viacam_remove()
1309 v4l2_ctrl_handler_free(&cam->ctrl_handler); in viacam_remove()
1317 .name = "viafb-camera",