aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@linux-foundation.org>2016-07-26 21:59:59 -0400
committerLinus Torvalds <torvalds@linux-foundation.org>2016-07-26 21:59:59 -0400
commit9c1958fc326a0a0a533ec8e86ea6fa30977207de (patch)
tree9d76e6a146c2622fad0eefbb8ff0503caefe33cc
parent1b3fc0bef8859268d542230172f80e85553fdab4 (diff)
parent009a620848218d521f008141c62f56bf19294dd9 (diff)
Merge tag 'media/v4.8-1' of git://git.kernel.org/pub/scm/linux/kernel/git/mchehab/linux-media
Pull media updates from Mauro Carvalho Chehab: - new framework support for HDMI CEC and remote control support - new encoding codec driver for Mediatek SoC - new frontend driver: helene tuner - added support for NetUp almost universal devices, with supports DVB-C/S/S2/T/T2 and ISDB-T - the mn88472 frontend driver got promoted from staging - a new driver for RCar video input - some soc_camera legacy drivers got removed: timb, omap1, mx2, mx3 - lots of driver cleanups, improvements and fixups * tag 'media/v4.8-1' of git://git.kernel.org/pub/scm/linux/kernel/git/mchehab/linux-media: (377 commits) [media] cec: always check all_device_types and features [media] cec: poll should check if there is room in the tx queue [media] vivid: support monitor all mode [media] cec: fix test for unconfigured adapter in main message loop [media] cec: limit the size of the transmit queue [media] cec: zero unused msg part after msg->len [media] cec: don't set fh to NULL in CEC_TRANSMIT [media] cec: clear all status fields before transmit and always fill in sequence [media] cec: CEC_RECEIVE overwrote the timeout field [media] cxd2841er: Reading SNR for DVB-C added [media] cxd2841er: Reading BER and UCB for DVB-C added [media] cxd2841er: fix switch-case for DVB-C [media] cxd2841er: fix signal strength scale for ISDB-T [media] cxd2841er: adjust the dB scale for DVB-C [media] cxd2841er: provide signal strength for DVB-C [media] cxd2841er: fix BER report via DVBv5 stats API [media] mb86a20s: apply mask to val after checking for read failure [media] airspy: fix error logic during device register [media] s5p-cec/TODO: add TODO item [media] cec/TODO: drop comment about sphinx documentation ...
-rw-r--r--Documentation/DocBook/device-drivers.tmpl3
-rw-r--r--Documentation/DocBook/media/Makefile2
-rw-r--r--Documentation/DocBook/media/v4l/biblio.xml10
-rw-r--r--Documentation/DocBook/media/v4l/cec-api.xml75
-rw-r--r--Documentation/DocBook/media/v4l/cec-func-close.xml64
-rw-r--r--Documentation/DocBook/media/v4l/cec-func-ioctl.xml78
-rw-r--r--Documentation/DocBook/media/v4l/cec-func-open.xml104
-rw-r--r--Documentation/DocBook/media/v4l/cec-func-poll.xml94
-rw-r--r--Documentation/DocBook/media/v4l/cec-ioc-adap-g-caps.xml151
-rw-r--r--Documentation/DocBook/media/v4l/cec-ioc-adap-g-log-addrs.xml329
-rw-r--r--Documentation/DocBook/media/v4l/cec-ioc-adap-g-phys-addr.xml86
-rw-r--r--Documentation/DocBook/media/v4l/cec-ioc-dqevent.xml202
-rw-r--r--Documentation/DocBook/media/v4l/cec-ioc-g-mode.xml255
-rw-r--r--Documentation/DocBook/media/v4l/cec-ioc-receive.xml274
-rw-r--r--Documentation/DocBook/media/v4l/io.xml4
-rw-r--r--Documentation/DocBook/media/v4l/lirc_device_interface.xml2
-rw-r--r--Documentation/DocBook/media/v4l/media-types.xml64
-rw-r--r--Documentation/DocBook/media/v4l/pixfmt-z16.xml2
-rw-r--r--Documentation/DocBook/media/v4l/vidioc-reqbufs.xml2
-rw-r--r--Documentation/DocBook/media_api.tmpl6
-rw-r--r--Documentation/cec.txt267
-rw-r--r--Documentation/devicetree/bindings/media/mediatek-vcodec.txt59
-rw-r--r--Documentation/devicetree/bindings/media/mediatek-vpu.txt31
-rw-r--r--Documentation/devicetree/bindings/media/renesas,fcp.txt32
-rw-r--r--Documentation/devicetree/bindings/media/renesas,vsp1.txt5
-rw-r--r--Documentation/devicetree/bindings/media/s5p-cec.txt31
-rw-r--r--Documentation/devicetree/bindings/media/s5p-mfc.txt39
-rw-r--r--Documentation/video4linux/CARDLIST.cx238851
-rw-r--r--Documentation/video4linux/v4l2-controls.txt15
-rw-r--r--Documentation/video4linux/vivid.txt36
-rw-r--r--MAINTAINERS74
-rw-r--r--arch/arm64/boot/dts/mediatek/mt8173.dtsi62
-rw-r--r--arch/blackfin/mach-bf609/boards/ezkit.c2
-rw-r--r--drivers/gpu/drm/rcar-du/rcar_du_vsp.c45
-rw-r--r--drivers/gpu/drm/rcar-du/rcar_du_vsp.h2
-rw-r--r--drivers/input/touchscreen/sur40.c21
-rw-r--r--drivers/media/Kconfig3
-rw-r--r--drivers/media/Makefile4
-rw-r--r--drivers/media/cec-edid.c168
-rw-r--r--drivers/media/common/v4l2-tpg/v4l2-tpg-core.c4
-rw-r--r--drivers/media/dvb-core/demux.h2
-rw-r--r--drivers/media/dvb-core/dmxdev.c2
-rw-r--r--drivers/media/dvb-core/dvb_ca_en50221.c39
-rw-r--r--drivers/media/dvb-core/dvb_demux.c17
-rw-r--r--drivers/media/dvb-core/dvb_demux.h4
-rw-r--r--drivers/media/dvb-core/dvb_frontend.c33
-rw-r--r--drivers/media/dvb-core/dvb_net.c2
-rw-r--r--drivers/media/dvb-core/dvb_ringbuffer.c74
-rw-r--r--drivers/media/dvb-frontends/Kconfig15
-rw-r--r--drivers/media/dvb-frontends/Makefile2
-rw-r--r--drivers/media/dvb-frontends/af9033.c327
-rw-r--r--drivers/media/dvb-frontends/ascot2e.c2
-rw-r--r--drivers/media/dvb-frontends/cxd2841er.c1927
-rw-r--r--drivers/media/dvb-frontends/cxd2841er.h24
-rw-r--r--drivers/media/dvb-frontends/cxd2841er_priv.h1
-rw-r--r--drivers/media/dvb-frontends/dib0090.c6
-rw-r--r--drivers/media/dvb-frontends/drx39xyj/drxj.c3
-rw-r--r--drivers/media/dvb-frontends/ds3000.c9
-rw-r--r--drivers/media/dvb-frontends/helene.c1042
-rw-r--r--drivers/media/dvb-frontends/helene.h79
-rw-r--r--drivers/media/dvb-frontends/horus3a.c26
-rw-r--r--drivers/media/dvb-frontends/m88ds3103.c144
-rw-r--r--drivers/media/dvb-frontends/m88ds3103_priv.h3
-rw-r--r--drivers/media/dvb-frontends/m88rs2000.c2
-rw-r--r--drivers/media/dvb-frontends/mb86a20s.c3
-rw-r--r--drivers/media/dvb-frontends/mn88472.c (renamed from drivers/staging/media/mn88472/mn88472.c)519
-rw-r--r--drivers/media/dvb-frontends/mn88472.h45
-rw-r--r--drivers/media/dvb-frontends/mn88472_priv.h (renamed from drivers/staging/media/mn88472/mn88472_priv.h)11
-rw-r--r--drivers/media/dvb-frontends/mn88473.c7
-rw-r--r--drivers/media/dvb-frontends/rtl2830.c203
-rw-r--r--drivers/media/dvb-frontends/rtl2830_priv.h2
-rw-r--r--drivers/media/dvb-frontends/rtl2832.c26
-rw-r--r--drivers/media/dvb-frontends/rtl2832_priv.h1
-rw-r--r--drivers/media/dvb-frontends/rtl2832_sdr.c2
-rw-r--r--drivers/media/dvb-frontends/si2168.c127
-rw-r--r--drivers/media/dvb-frontends/si2168_priv.h8
-rw-r--r--drivers/media/i2c/Kconfig24
-rw-r--r--drivers/media/i2c/adv7511.c445
-rw-r--r--drivers/media/i2c/adv7604.c393
-rw-r--r--drivers/media/i2c/adv7842.c413
-rw-r--r--drivers/media/i2c/cs53l32a.c7
-rw-r--r--drivers/media/i2c/cx25840/cx25840-core.c7
-rw-r--r--drivers/media/i2c/msp3400-driver.c7
-rw-r--r--drivers/media/i2c/mt9t001.c17
-rw-r--r--drivers/media/i2c/mt9v032.c279
-rw-r--r--drivers/media/i2c/saa7115.c7
-rw-r--r--drivers/media/i2c/smiapp/smiapp-core.c4
-rw-r--r--drivers/media/i2c/tc358743.c15
-rw-r--r--drivers/media/i2c/tvaudio.c7
-rw-r--r--drivers/media/i2c/wm8775.c7
-rw-r--r--drivers/media/media-device.c47
-rw-r--r--drivers/media/media-devnode.c149
-rw-r--r--drivers/media/pci/bt8xx/dst_ca.c2
-rw-r--r--drivers/media/pci/cobalt/cobalt-driver.c11
-rw-r--r--drivers/media/pci/cobalt/cobalt-driver.h1
-rw-r--r--drivers/media/pci/cobalt/cobalt-v4l2.c4
-rw-r--r--drivers/media/pci/cx18/cx18-alsa-mixer.c6
-rw-r--r--drivers/media/pci/cx18/cx18-driver.c2
-rw-r--r--drivers/media/pci/cx18/cx18-driver.h6
-rw-r--r--drivers/media/pci/cx18/cx18-ioctl.c2
-rw-r--r--drivers/media/pci/cx18/cx18-streams.c12
-rw-r--r--drivers/media/pci/cx18/cx18-vbi.c6
-rw-r--r--drivers/media/pci/cx23885/cx23885-417.c3
-rw-r--r--drivers/media/pci/cx23885/cx23885-cards.c59
-rw-r--r--drivers/media/pci/cx23885/cx23885-core.c10
-rw-r--r--drivers/media/pci/cx23885/cx23885-dvb.c104
-rw-r--r--drivers/media/pci/cx23885/cx23885-vbi.c3
-rw-r--r--drivers/media/pci/cx23885/cx23885-video.c5
-rw-r--r--drivers/media/pci/cx23885/cx23885.h2
-rw-r--r--drivers/media/pci/cx25821/cx25821-alsa.c2
-rw-r--r--drivers/media/pci/cx25821/cx25821-core.c10
-rw-r--r--drivers/media/pci/cx25821/cx25821-video.c5
-rw-r--r--drivers/media/pci/cx25821/cx25821.h1
-rw-r--r--drivers/media/pci/cx88/cx88-alsa.c8
-rw-r--r--drivers/media/pci/cx88/cx88-blackbird.c4
-rw-r--r--drivers/media/pci/cx88/cx88-dvb.c4
-rw-r--r--drivers/media/pci/cx88/cx88-mpeg.c10
-rw-r--r--drivers/media/pci/cx88/cx88-vbi.c3
-rw-r--r--drivers/media/pci/cx88/cx88-video.c13
-rw-r--r--drivers/media/pci/cx88/cx88.h2
-rw-r--r--drivers/media/pci/ddbridge/ddbridge-core.c3
-rw-r--r--drivers/media/pci/dt3155/dt3155.c15
-rw-r--r--drivers/media/pci/dt3155/dt3155.h2
-rw-r--r--drivers/media/pci/ivtv/ivtv-alsa-mixer.c6
-rw-r--r--drivers/media/pci/netup_unidvb/Kconfig7
-rw-r--r--drivers/media/pci/netup_unidvb/netup_unidvb.h10
-rw-r--r--drivers/media/pci/netup_unidvb/netup_unidvb_ci.c4
-rw-r--r--drivers/media/pci/netup_unidvb/netup_unidvb_core.c174
-rw-r--r--drivers/media/pci/saa7134/saa7134-core.c22
-rw-r--r--drivers/media/pci/saa7134/saa7134-ts.c3
-rw-r--r--drivers/media/pci/saa7134/saa7134-vbi.c3
-rw-r--r--drivers/media/pci/saa7134/saa7134-video.c5
-rw-r--r--drivers/media/pci/saa7134/saa7134.h3
-rw-r--r--drivers/media/pci/saa7164/saa7164-encoder.c6
-rw-r--r--drivers/media/pci/saa7164/saa7164.h4
-rw-r--r--drivers/media/pci/solo6x10/solo6x10-v4l2-enc.c15
-rw-r--r--drivers/media/pci/solo6x10/solo6x10-v4l2.c44
-rw-r--r--drivers/media/pci/solo6x10/solo6x10.h2
-rw-r--r--drivers/media/pci/sta2x11/sta2x11_vip.c20
-rw-r--r--drivers/media/pci/tw68/tw68-core.c15
-rw-r--r--drivers/media/pci/tw68/tw68-video.c4
-rw-r--r--drivers/media/pci/tw68/tw68.h1
-rw-r--r--drivers/media/pci/tw686x/Kconfig2
-rw-r--r--drivers/media/pci/tw686x/tw686x-audio.c92
-rw-r--r--drivers/media/pci/tw686x/tw686x-core.c56
-rw-r--r--drivers/media/pci/tw686x/tw686x-regs.h9
-rw-r--r--drivers/media/pci/tw686x/tw686x-video.c595
-rw-r--r--drivers/media/pci/tw686x/tw686x.h42
-rw-r--r--drivers/media/pci/zoran/zr36016.c4
-rw-r--r--drivers/media/platform/Kconfig45
-rw-r--r--drivers/media/platform/Makefile7
-rw-r--r--drivers/media/platform/am437x/am437x-vpfe.c14
-rw-r--r--drivers/media/platform/am437x/am437x-vpfe.h2
-rw-r--r--drivers/media/platform/blackfin/bfin_capture.c17
-rw-r--r--drivers/media/platform/coda/coda-common.c20
-rw-r--r--drivers/media/platform/coda/coda.h1
-rw-r--r--drivers/media/platform/davinci/ccdc_hw_device.h7
-rw-r--r--drivers/media/platform/davinci/vpbe_display.c14
-rw-r--r--drivers/media/platform/davinci/vpif_capture.c15
-rw-r--r--drivers/media/platform/davinci/vpif_capture.h2
-rw-r--r--drivers/media/platform/davinci/vpif_display.c15
-rw-r--r--drivers/media/platform/davinci/vpif_display.h2
-rw-r--r--drivers/media/platform/exynos-gsc/gsc-core.c12
-rw-r--r--drivers/media/platform/exynos-gsc/gsc-core.h2
-rw-r--r--drivers/media/platform/exynos-gsc/gsc-m2m.c8
-rw-r--r--drivers/media/platform/exynos4-is/fimc-capture.c9
-rw-r--r--drivers/media/platform/exynos4-is/fimc-core.c12
-rw-r--r--drivers/media/platform/exynos4-is/fimc-core.h3
-rw-r--r--drivers/media/platform/exynos4-is/fimc-is.c15
-rw-r--r--drivers/media/platform/exynos4-is/fimc-is.h2
-rw-r--r--drivers/media/platform/exynos4-is/fimc-isp-video.c11
-rw-r--r--drivers/media/platform/exynos4-is/fimc-isp.h2
-rw-r--r--drivers/media/platform/exynos4-is/fimc-lite.c22
-rw-r--r--drivers/media/platform/exynos4-is/fimc-lite.h2
-rw-r--r--drivers/media/platform/exynos4-is/fimc-m2m.c32
-rw-r--r--drivers/media/platform/exynos4-is/mipi-csis.c17
-rw-r--r--drivers/media/platform/m2m-deinterlace.c17
-rw-r--r--drivers/media/platform/marvell-ccic/mcam-core.c28
-rw-r--r--drivers/media/platform/marvell-ccic/mcam-core.h2
-rw-r--r--drivers/media/platform/mtk-vcodec/Makefile19
-rw-r--r--drivers/media/platform/mtk-vcodec/mtk_vcodec_drv.h335
-rw-r--r--drivers/media/platform/mtk-vcodec/mtk_vcodec_enc.c1292
-rw-r--r--drivers/media/platform/mtk-vcodec/mtk_vcodec_enc.h58
-rw-r--r--drivers/media/platform/mtk-vcodec/mtk_vcodec_enc_drv.c439
-rw-r--r--drivers/media/platform/mtk-vcodec/mtk_vcodec_enc_pm.c137
-rw-r--r--drivers/media/platform/mtk-vcodec/mtk_vcodec_enc_pm.h26
-rw-r--r--drivers/media/platform/mtk-vcodec/mtk_vcodec_intr.c54
-rw-r--r--drivers/media/platform/mtk-vcodec/mtk_vcodec_intr.h27
-rw-r--r--drivers/media/platform/mtk-vcodec/mtk_vcodec_util.c94
-rw-r--r--drivers/media/platform/mtk-vcodec/mtk_vcodec_util.h87
-rw-r--r--drivers/media/platform/mtk-vcodec/venc/venc_h264_if.c679
-rw-r--r--drivers/media/platform/mtk-vcodec/venc/venc_vp8_if.c486
-rw-r--r--drivers/media/platform/mtk-vcodec/venc_drv_base.h62
-rw-r--r--drivers/media/platform/mtk-vcodec/venc_drv_if.c113
-rw-r--r--drivers/media/platform/mtk-vcodec/venc_drv_if.h163
-rw-r--r--drivers/media/platform/mtk-vcodec/venc_ipi_msg.h210
-rw-r--r--drivers/media/platform/mtk-vcodec/venc_vpu_if.c238
-rw-r--r--drivers/media/platform/mtk-vcodec/venc_vpu_if.h61
-rw-r--r--drivers/media/platform/mtk-vpu/Makefile3
-rw-r--r--drivers/media/platform/mtk-vpu/mtk_vpu.c946
-rw-r--r--drivers/media/platform/mtk-vpu/mtk_vpu.h162
-rw-r--r--drivers/media/platform/mx2_emmaprp.c19
-rw-r--r--drivers/media/platform/omap/omap_vout.c109
-rw-r--r--drivers/media/platform/omap/omap_voutdef.h5
-rw-r--r--drivers/media/platform/omap3isp/ispvideo.c14
-rw-r--r--drivers/media/platform/omap3isp/ispvideo.h1
-rw-r--r--drivers/media/platform/rcar-fcp.c181
-rw-r--r--drivers/media/platform/rcar-vin/Kconfig11
-rw-r--r--drivers/media/platform/rcar-vin/Makefile3
-rw-r--r--drivers/media/platform/rcar-vin/rcar-core.c334
-rw-r--r--drivers/media/platform/rcar-vin/rcar-dma.c1187
-rw-r--r--drivers/media/platform/rcar-vin/rcar-v4l2.c874
-rw-r--r--drivers/media/platform/rcar-vin/rcar-vin.h163
-rw-r--r--drivers/media/platform/rcar_jpu.c24
-rw-r--r--drivers/media/platform/s3c-camif/camif-capture.c5
-rw-r--r--drivers/media/platform/s3c-camif/camif-core.c11
-rw-r--r--drivers/media/platform/s3c-camif/camif-core.h2
-rw-r--r--drivers/media/platform/s5p-g2d/g2d.c17
-rw-r--r--drivers/media/platform/s5p-g2d/g2d.h1
-rw-r--r--drivers/media/platform/s5p-jpeg/jpeg-core.c21
-rw-r--r--drivers/media/platform/s5p-jpeg/jpeg-core.h2
-rw-r--r--drivers/media/platform/s5p-mfc/s5p_mfc.c227
-rw-r--r--drivers/media/platform/s5p-mfc/s5p_mfc_common.h4
-rw-r--r--drivers/media/platform/s5p-mfc/s5p_mfc_dec.c28
-rw-r--r--drivers/media/platform/s5p-mfc/s5p_mfc_enc.c35
-rw-r--r--drivers/media/platform/s5p-mfc/s5p_mfc_iommu.h79
-rw-r--r--drivers/media/platform/s5p-mfc/s5p_mfc_pm.c13
-rw-r--r--drivers/media/platform/s5p-tv/mixer.h2
-rw-r--r--drivers/media/platform/s5p-tv/mixer_video.c19
-rw-r--r--drivers/media/platform/sh_veu.c19
-rw-r--r--drivers/media/platform/sh_vou.c16
-rw-r--r--drivers/media/platform/soc_camera/Kconfig4
-rw-r--r--drivers/media/platform/soc_camera/Makefile2
-rw-r--r--drivers/media/platform/soc_camera/atmel-isi.c15
-rw-r--r--drivers/media/platform/soc_camera/rcar_vin.c14
-rw-r--r--drivers/media/platform/soc_camera/sh_mobile_ceu_camera.c17
-rw-r--r--drivers/media/platform/sti/bdisp/bdisp-filter.h304
-rw-r--r--drivers/media/platform/sti/bdisp/bdisp-hw.c305
-rw-r--r--drivers/media/platform/sti/bdisp/bdisp-v4l2.c18
-rw-r--r--drivers/media/platform/sti/bdisp/bdisp.h2
-rw-r--r--drivers/media/platform/sti/c8sectpfe/c8sectpfe-core.c1
-rw-r--r--drivers/media/platform/ti-vpe/cal.c17
-rw-r--r--drivers/media/platform/ti-vpe/vpe.c22
-rw-r--r--drivers/media/platform/via-camera.c2
-rw-r--r--drivers/media/platform/vim2m.c7
-rw-r--r--drivers/media/platform/vivid/Kconfig8
-rw-r--r--drivers/media/platform/vivid/Makefile4
-rw-r--r--drivers/media/platform/vivid/vivid-cec.c241
-rw-r--r--drivers/media/platform/vivid/vivid-cec.h33
-rw-r--r--drivers/media/platform/vivid/vivid-core.c118
-rw-r--r--drivers/media/platform/vivid/vivid-core.h27
-rw-r--r--drivers/media/platform/vivid/vivid-kthread-cap.c13
-rw-r--r--drivers/media/platform/vivid/vivid-sdr-cap.c4
-rw-r--r--drivers/media/platform/vivid/vivid-vbi-cap.c2
-rw-r--r--drivers/media/platform/vivid/vivid-vbi-out.c2
-rw-r--r--drivers/media/platform/vivid/vivid-vid-cap.c34
-rw-r--r--drivers/media/platform/vivid/vivid-vid-common.c7
-rw-r--r--drivers/media/platform/vivid/vivid-vid-out.c7
-rw-r--r--drivers/media/platform/vsp1/Makefile3
-rw-r--r--drivers/media/platform/vsp1/vsp1.h11
-rw-r--r--drivers/media/platform/vsp1/vsp1_bru.c12
-rw-r--r--drivers/media/platform/vsp1/vsp1_clu.c292
-rw-r--r--drivers/media/platform/vsp1/vsp1_clu.h48
-rw-r--r--drivers/media/platform/vsp1/vsp1_dl.c72
-rw-r--r--drivers/media/platform/vsp1/vsp1_drm.c74
-rw-r--r--drivers/media/platform/vsp1/vsp1_drv.c191
-rw-r--r--drivers/media/platform/vsp1/vsp1_entity.c92
-rw-r--r--drivers/media/platform/vsp1/vsp1_entity.h17
-rw-r--r--drivers/media/platform/vsp1/vsp1_hsit.c14
-rw-r--r--drivers/media/platform/vsp1/vsp1_lif.c16
-rw-r--r--drivers/media/platform/vsp1/vsp1_lut.c101
-rw-r--r--drivers/media/platform/vsp1/vsp1_lut.h7
-rw-r--r--drivers/media/platform/vsp1/vsp1_pipe.c58
-rw-r--r--drivers/media/platform/vsp1/vsp1_pipe.h8
-rw-r--r--drivers/media/platform/vsp1/vsp1_regs.h24
-rw-r--r--drivers/media/platform/vsp1/vsp1_rpf.c38
-rw-r--r--drivers/media/platform/vsp1/vsp1_rwpf.c6
-rw-r--r--drivers/media/platform/vsp1/vsp1_rwpf.h14
-rw-r--r--drivers/media/platform/vsp1/vsp1_sru.c14
-rw-r--r--drivers/media/platform/vsp1/vsp1_uds.c16
-rw-r--r--drivers/media/platform/vsp1/vsp1_uds.h2
-rw-r--r--drivers/media/platform/vsp1/vsp1_video.c40
-rw-r--r--drivers/media/platform/vsp1/vsp1_video.h2
-rw-r--r--drivers/media/platform/vsp1/vsp1_wpf.c161
-rw-r--r--drivers/media/platform/xilinx/xilinx-dma.c13
-rw-r--r--drivers/media/platform/xilinx/xilinx-dma.h2
-rw-r--r--drivers/media/radio/radio-aztech.c1
-rw-r--r--drivers/media/radio/radio-maxiradio.c1
-rw-r--r--drivers/media/rc/ene_ir.c2
-rw-r--r--drivers/media/rc/iguanair.c2
-rw-r--r--drivers/media/rc/ir-lirc-codec.c5
-rw-r--r--drivers/media/rc/ir-rc5-decoder.c2
-rw-r--r--drivers/media/rc/keymaps/Makefile2
-rw-r--r--drivers/media/rc/keymaps/rc-cec.c182
-rw-r--r--drivers/media/rc/keymaps/rc-dtt200u.c59
-rw-r--r--drivers/media/rc/lirc_dev.c299
-rw-r--r--drivers/media/rc/mceusb.c8
-rw-r--r--drivers/media/rc/nuvoton-cir.c138
-rw-r--r--drivers/media/rc/nuvoton-cir.h25
-rw-r--r--drivers/media/rc/rc-main.c11
-rw-r--r--drivers/media/rc/redrat3.c84
-rw-r--r--drivers/media/rc/winbond-cir.c4
-rw-r--r--drivers/media/tuners/it913x.c1
-rw-r--r--drivers/media/tuners/mt2063.c30
-rw-r--r--drivers/media/tuners/r820t.c29
-rw-r--r--drivers/media/tuners/si2157.c3
-rw-r--r--drivers/media/usb/airspy/airspy.c2
-rw-r--r--drivers/media/usb/au0828/au0828-core.c4
-rw-r--r--drivers/media/usb/au0828/au0828-vbi.c2
-rw-r--r--drivers/media/usb/au0828/au0828-video.c2
-rw-r--r--drivers/media/usb/cx231xx/cx231xx-417.c4
-rw-r--r--drivers/media/usb/dvb-usb-v2/Kconfig13
-rw-r--r--drivers/media/usb/dvb-usb-v2/af9035.c275
-rw-r--r--drivers/media/usb/dvb-usb-v2/af9035.h3
-rw-r--r--drivers/media/usb/dvb-usb-v2/rtl28xxu.c2
-rw-r--r--drivers/media/usb/dvb-usb/dtt200u.c74
-rw-r--r--drivers/media/usb/dvb-usb/dvb-usb-dvb.c2
-rw-r--r--drivers/media/usb/dvb-usb/dw2102.c48
-rw-r--r--drivers/media/usb/em28xx/em28xx-dvb.c11
-rw-r--r--drivers/media/usb/em28xx/em28xx-i2c.c5
-rw-r--r--drivers/media/usb/em28xx/em28xx-vbi.c2
-rw-r--r--drivers/media/usb/em28xx/em28xx-video.c2
-rw-r--r--drivers/media/usb/go7007/go7007-v4l2.c2
-rw-r--r--drivers/media/usb/gspca/cpia1.c2
-rw-r--r--drivers/media/usb/gspca/gspca.c29
-rw-r--r--drivers/media/usb/gspca/konica.c2
-rw-r--r--drivers/media/usb/gspca/m5602/m5602_bridge.h15
-rw-r--r--drivers/media/usb/gspca/m5602/m5602_core.c15
-rw-r--r--drivers/media/usb/gspca/m5602/m5602_mt9m111.c144
-rw-r--r--drivers/media/usb/gspca/m5602/m5602_mt9m111.h144
-rw-r--r--drivers/media/usb/gspca/m5602/m5602_ov7660.c153
-rw-r--r--drivers/media/usb/gspca/m5602/m5602_ov7660.h153
-rw-r--r--drivers/media/usb/gspca/m5602/m5602_ov9650.c152
-rw-r--r--drivers/media/usb/gspca/m5602/m5602_ov9650.h150
-rw-r--r--drivers/media/usb/gspca/m5602/m5602_po1030.c104
-rw-r--r--drivers/media/usb/gspca/m5602/m5602_po1030.h104
-rw-r--r--drivers/media/usb/gspca/m5602/m5602_s5k4aa.c199
-rw-r--r--drivers/media/usb/gspca/m5602/m5602_s5k4aa.h197
-rw-r--r--drivers/media/usb/gspca/m5602/m5602_s5k83a.c124
-rw-r--r--drivers/media/usb/gspca/m5602/m5602_s5k83a.h124
-rw-r--r--drivers/media/usb/gspca/ov534.c7
-rw-r--r--drivers/media/usb/gspca/sn9c20x.c14
-rw-r--r--drivers/media/usb/gspca/t613.c2
-rw-r--r--drivers/media/usb/gspca/topro.c6
-rw-r--r--drivers/media/usb/gspca/zc3xx.c13
-rw-r--r--drivers/media/usb/hackrf/hackrf.c2
-rw-r--r--drivers/media/usb/hdpvr/hdpvr-core.c10
-rw-r--r--drivers/media/usb/hdpvr/hdpvr-video.c6
-rw-r--r--drivers/media/usb/hdpvr/hdpvr.h2
-rw-r--r--drivers/media/usb/msi2500/msi2500.c2
-rw-r--r--drivers/media/usb/pvrusb2/pvrusb2-hdw.c6
-rw-r--r--drivers/media/usb/pwc/pwc-if.c4
-rw-r--r--drivers/media/usb/s2255/s2255drv.c2
-rw-r--r--drivers/media/usb/stk1160/stk1160-v4l.c5
-rw-r--r--drivers/media/usb/usbtv/usbtv-audio.c33
-rw-r--r--drivers/media/usb/usbtv/usbtv-core.c40
-rw-r--r--drivers/media/usb/usbtv/usbtv-video.c61
-rw-r--r--drivers/media/usb/usbtv/usbtv.h22
-rw-r--r--drivers/media/usb/usbvision/usbvision-core.c5
-rw-r--r--drivers/media/usb/usbvision/usbvision-video.c40
-rw-r--r--drivers/media/usb/uvc/uvc_driver.c2
-rw-r--r--drivers/media/usb/uvc/uvc_queue.c2
-rw-r--r--drivers/media/usb/uvc/uvc_v4l2.c19
-rw-r--r--drivers/media/usb/uvc/uvc_video.c1
-rw-r--r--drivers/media/v4l2-core/v4l2-ctrls.c45
-rw-r--r--drivers/media/v4l2-core/v4l2-flash-led-class.c9
-rw-r--r--drivers/media/v4l2-core/v4l2-ioctl.c4
-rw-r--r--drivers/media/v4l2-core/videobuf2-core.c40
-rw-r--r--drivers/media/v4l2-core/videobuf2-dma-contig.c88
-rw-r--r--drivers/media/v4l2-core/videobuf2-dma-sg.c45
-rw-r--r--drivers/media/v4l2-core/videobuf2-v4l2.c53
-rw-r--r--drivers/media/v4l2-core/videobuf2-vmalloc.c9
-rw-r--r--drivers/of/of_reserved_mem.c83
-rw-r--r--drivers/staging/media/Kconfig14
-rw-r--r--drivers/staging/media/Makefile8
-rw-r--r--drivers/staging/media/cec/Kconfig15
-rw-r--r--drivers/staging/media/cec/Makefile5
-rw-r--r--drivers/staging/media/cec/TODO31
-rw-r--r--drivers/staging/media/cec/cec-adap.c1654
-rw-r--r--drivers/staging/media/cec/cec-api.c579
-rw-r--r--drivers/staging/media/cec/cec-core.c409
-rw-r--r--drivers/staging/media/cec/cec-priv.h56
-rw-r--r--drivers/staging/media/davinci_vpfe/vpfe_video.c14
-rw-r--r--drivers/staging/media/davinci_vpfe/vpfe_video.h2
-rw-r--r--drivers/staging/media/lirc/lirc_parallel.c8
-rw-r--r--drivers/staging/media/mn88472/Kconfig7
-rw-r--r--drivers/staging/media/mn88472/Makefile5
-rw-r--r--drivers/staging/media/mn88472/TODO21
-rw-r--r--drivers/staging/media/mx2/Kconfig15
-rw-r--r--drivers/staging/media/mx2/Makefile3
-rw-r--r--drivers/staging/media/mx2/TODO10
-rw-r--r--drivers/staging/media/mx2/mx2_camera.c1636
-rw-r--r--drivers/staging/media/mx3/Kconfig15
-rw-r--r--drivers/staging/media/mx3/Makefile3
-rw-r--r--drivers/staging/media/mx3/TODO10
-rw-r--r--drivers/staging/media/mx3/mx3_camera.c1264
-rw-r--r--drivers/staging/media/omap1/Kconfig13
-rw-r--r--drivers/staging/media/omap1/Makefile3
-rw-r--r--drivers/staging/media/omap1/TODO8
-rw-r--r--drivers/staging/media/omap1/omap1_camera.c1702
-rw-r--r--drivers/staging/media/omap4iss/iss_video.c12
-rw-r--r--drivers/staging/media/omap4iss/iss_video.h1
-rw-r--r--drivers/staging/media/pulse8-cec/Kconfig10
-rw-r--r--drivers/staging/media/pulse8-cec/Makefile1
-rw-r--r--drivers/staging/media/pulse8-cec/TODO52
-rw-r--r--drivers/staging/media/pulse8-cec/pulse8-cec.c505
-rw-r--r--drivers/staging/media/s5p-cec/Kconfig9
-rw-r--r--drivers/staging/media/s5p-cec/Makefile2
-rw-r--r--drivers/staging/media/s5p-cec/TODO7
-rw-r--r--drivers/staging/media/s5p-cec/exynos_hdmi_cec.h38
-rw-r--r--drivers/staging/media/s5p-cec/exynos_hdmi_cecctrl.c209
-rw-r--r--drivers/staging/media/s5p-cec/regs-cec.h96
-rw-r--r--drivers/staging/media/s5p-cec/s5p_cec.c294
-rw-r--r--drivers/staging/media/s5p-cec/s5p_cec.h76
-rw-r--r--drivers/staging/media/timb/Kconfig11
-rw-r--r--drivers/staging/media/timb/Makefile1
-rw-r--r--drivers/staging/media/timb/timblogiw.c870
-rw-r--r--drivers/staging/media/tw686x-kh/tw686x-kh-video.c12
-rw-r--r--drivers/staging/media/tw686x-kh/tw686x-kh.h1
-rw-r--r--drivers/usb/gadget/function/uvc_queue.c2
-rw-r--r--fs/compat_ioctl.c12
-rw-r--r--include/linux/cec-funcs.h1899
-rw-r--r--include/linux/cec.h1011
-rw-r--r--include/linux/of_reserved_mem.h25
-rw-r--r--include/media/cec-edid.h104
-rw-r--r--include/media/cec.h241
-rw-r--r--include/media/davinci/vpbe_display.h2
-rw-r--r--include/media/i2c/adv7511.h6
-rw-r--r--include/media/i2c/adv7604.h2
-rw-r--r--include/media/i2c/adv7842.h2
-rw-r--r--include/media/media-device.h5
-rw-r--r--include/media/media-devnode.h46
-rw-r--r--include/media/rc-core.h3
-rw-r--r--include/media/rc-map.h6
-rw-r--r--include/media/rcar-fcp.h37
-rw-r--r--include/media/v4l2-ctrls.h34
-rw-r--r--include/media/v4l2-subdev.h21
-rw-r--r--include/media/videobuf2-core.h24
-rw-r--r--include/media/videobuf2-dma-contig.h11
-rw-r--r--include/media/videobuf2-dma-sg.h3
-rw-r--r--include/media/vsp1.h29
-rw-r--r--include/uapi/linux/media.h10
-rw-r--r--include/uapi/linux/serio.h1
-rw-r--r--include/uapi/linux/videodev2.h14
-rw-r--r--include/uapi/linux/vsp1.h34
-rw-r--r--samples/v4l/v4l2-pci-skeleton.c17
446 files changed, 28773 insertions, 11174 deletions
diff --git a/Documentation/DocBook/device-drivers.tmpl b/Documentation/DocBook/device-drivers.tmpl
index 8c68768ebee5..58af32b01b90 100644
--- a/Documentation/DocBook/device-drivers.tmpl
+++ b/Documentation/DocBook/device-drivers.tmpl
@@ -300,6 +300,9 @@ X!Isound/sound_firmware.c
300!Iinclude/media/media-devnode.h 300!Iinclude/media/media-devnode.h
301!Iinclude/media/media-entity.h 301!Iinclude/media/media-entity.h
302 </sect1> 302 </sect1>
303 <sect1><title>Consumer Electronics Control devices</title>
304!Iinclude/media/cec-edid.h
305 </sect1>
303 306
304 </chapter> 307 </chapter>
305 308
diff --git a/Documentation/DocBook/media/Makefile b/Documentation/DocBook/media/Makefile
index 2840ff483d5a..fdc138624800 100644
--- a/Documentation/DocBook/media/Makefile
+++ b/Documentation/DocBook/media/Makefile
@@ -64,6 +64,7 @@ IOCTLS = \
64 $(shell perl -ne 'print "$$1 " if /\#define\s+([A-Z][^\s]+)\s+_IO/' $(srctree)/include/uapi/linux/dvb/net.h) \ 64 $(shell perl -ne 'print "$$1 " if /\#define\s+([A-Z][^\s]+)\s+_IO/' $(srctree)/include/uapi/linux/dvb/net.h) \
65 $(shell perl -ne 'print "$$1 " if /\#define\s+([^\s]+)\s+_IO/' $(srctree)/include/uapi/linux/dvb/video.h) \ 65 $(shell perl -ne 'print "$$1 " if /\#define\s+([^\s]+)\s+_IO/' $(srctree)/include/uapi/linux/dvb/video.h) \
66 $(shell perl -ne 'print "$$1 " if /\#define\s+([^\s]+)\s+_IO/' $(srctree)/include/uapi/linux/media.h) \ 66 $(shell perl -ne 'print "$$1 " if /\#define\s+([^\s]+)\s+_IO/' $(srctree)/include/uapi/linux/media.h) \
67 $(shell perl -ne 'print "$$1 " if /\#define\s+([^\s]+)\s+_IO/' $(srctree)/include/linux/cec.h) \
67 $(shell perl -ne 'print "$$1 " if /\#define\s+([^\s]+)\s+_IO/' $(srctree)/include/uapi/linux/v4l2-subdev.h) \ 68 $(shell perl -ne 'print "$$1 " if /\#define\s+([^\s]+)\s+_IO/' $(srctree)/include/uapi/linux/v4l2-subdev.h) \
68 69
69DEFINES = \ 70DEFINES = \
@@ -100,6 +101,7 @@ STRUCTS = \
100 $(shell perl -ne 'print "$$1 " if (/^struct\s+([^\s]+)\s+/ && !/_old/)' $(srctree)/include/uapi/linux/dvb/net.h) \ 101 $(shell perl -ne 'print "$$1 " if (/^struct\s+([^\s]+)\s+/ && !/_old/)' $(srctree)/include/uapi/linux/dvb/net.h) \
101 $(shell perl -ne 'print "$$1 " if (/^struct\s+([^\s]+)\s+/)' $(srctree)/include/uapi/linux/dvb/video.h) \ 102 $(shell perl -ne 'print "$$1 " if (/^struct\s+([^\s]+)\s+/)' $(srctree)/include/uapi/linux/dvb/video.h) \
102 $(shell perl -ne 'print "$$1 " if /^struct\s+([^\s]+)\s+/' $(srctree)/include/uapi/linux/media.h) \ 103 $(shell perl -ne 'print "$$1 " if /^struct\s+([^\s]+)\s+/' $(srctree)/include/uapi/linux/media.h) \
104 $(shell perl -ne 'print "$$1 " if /^struct\s+([^\s]+)\s+/' $(srctree)/include/linux/cec.h) \
103 $(shell perl -ne 'print "$$1 " if /^struct\s+([^\s]+)\s+/' $(srctree)/include/uapi/linux/v4l2-subdev.h) \ 105 $(shell perl -ne 'print "$$1 " if /^struct\s+([^\s]+)\s+/' $(srctree)/include/uapi/linux/v4l2-subdev.h) \
104 $(shell perl -ne 'print "$$1 " if /^struct\s+([^\s]+)\s+/' $(srctree)/include/uapi/linux/v4l2-mediabus.h) 106 $(shell perl -ne 'print "$$1 " if /^struct\s+([^\s]+)\s+/' $(srctree)/include/uapi/linux/v4l2-mediabus.h)
105 107
diff --git a/Documentation/DocBook/media/v4l/biblio.xml b/Documentation/DocBook/media/v4l/biblio.xml
index 9beb30f0071b..87f1d24958aa 100644
--- a/Documentation/DocBook/media/v4l/biblio.xml
+++ b/Documentation/DocBook/media/v4l/biblio.xml
@@ -342,6 +342,16 @@ in the frequency range from 87,5 to 108,0 MHz</title>
342 <subtitle>Specification Version 1.4a</subtitle> 342 <subtitle>Specification Version 1.4a</subtitle>
343 </biblioentry> 343 </biblioentry>
344 344
345 <biblioentry id="hdmi2">
346 <abbrev>HDMI2</abbrev>
347 <authorgroup>
348 <corpauthor>HDMI Licensing LLC
349(<ulink url="http://www.hdmi.org">http://www.hdmi.org</ulink>)</corpauthor>
350 </authorgroup>
351 <title>High-Definition Multimedia Interface</title>
352 <subtitle>Specification Version 2.0</subtitle>
353 </biblioentry>
354
345 <biblioentry id="dp"> 355 <biblioentry id="dp">
346 <abbrev>DP</abbrev> 356 <abbrev>DP</abbrev>
347 <authorgroup> 357 <authorgroup>
diff --git a/Documentation/DocBook/media/v4l/cec-api.xml b/Documentation/DocBook/media/v4l/cec-api.xml
new file mode 100644
index 000000000000..7062c1fa4904
--- /dev/null
+++ b/Documentation/DocBook/media/v4l/cec-api.xml
@@ -0,0 +1,75 @@
1<partinfo>
2 <authorgroup>
3 <author>
4 <firstname>Hans</firstname>
5 <surname>Verkuil</surname>
6 <affiliation><address><email>hans.verkuil@cisco.com</email></address></affiliation>
7 <contrib>Initial version.</contrib>
8 </author>
9 </authorgroup>
10 <copyright>
11 <year>2016</year>
12 <holder>Hans Verkuil</holder>
13 </copyright>
14
15 <revhistory>
16 <!-- Put document revisions here, newest first. -->
17 <revision>
18 <revnumber>1.0.0</revnumber>
19 <date>2016-03-17</date>
20 <authorinitials>hv</authorinitials>
21 <revremark>Initial revision</revremark>
22 </revision>
23 </revhistory>
24</partinfo>
25
26<title>CEC API</title>
27
28<chapter id="cec-api">
29 <title>CEC: Consumer Electronics Control</title>
30
31 <section id="cec-intro">
32 <title>Introduction</title>
33 <para>
34 Note: this documents the proposed CEC API. This API is not yet finalized and
35 is currently only available as a staging kernel module.
36 </para>
37 <para>HDMI connectors provide a single pin for use by the Consumer Electronics
38 Control protocol. This protocol allows different devices connected by an HDMI cable
39 to communicate. The protocol for CEC version 1.4 is defined in supplements 1 (CEC)
40 and 2 (HEAC or HDMI Ethernet and Audio Return Channel) of the HDMI 1.4a
41 (<xref linkend="hdmi" />) specification and the extensions added to CEC version 2.0
42 are defined in chapter 11 of the HDMI 2.0 (<xref linkend="hdmi2" />) specification.
43 </para>
44
45 <para>The bitrate is very slow (effectively no more than 36 bytes per second) and
46 is based on the ancient AV.link protocol used in old SCART connectors. The protocol
47 closely resembles a crazy Rube Goldberg contraption and is an unholy mix of low and
48 high level messages. Some messages, especially those part of the HEAC protocol layered
49 on top of CEC, need to be handled by the kernel, others can be handled either by the
50 kernel or by userspace.</para>
51
52 <para>In addition, CEC can be implemented in HDMI receivers, transmitters and in USB
53 devices that have an HDMI input and an HDMI output and that control just the CEC pin.</para>
54
55 <para>Drivers that support CEC will create a CEC device node (/dev/cecX)
56 to give userspace access to the CEC adapter. The &CEC-ADAP-G-CAPS; ioctl will tell userspace
57 what it is allowed to do.</para>
58 </section>
59</chapter>
60
61<appendix id="cec-user-func">
62 <title>Function Reference</title>
63 <!-- Keep this alphabetically sorted. -->
64 &sub-cec-func-open;
65 &sub-cec-func-close;
66 &sub-cec-func-ioctl;
67 &sub-cec-func-poll;
68 <!-- All ioctls go here. -->
69 &sub-cec-ioc-adap-g-caps;
70 &sub-cec-ioc-adap-g-log-addrs;
71 &sub-cec-ioc-adap-g-phys-addr;
72 &sub-cec-ioc-dqevent;
73 &sub-cec-ioc-g-mode;
74 &sub-cec-ioc-receive;
75</appendix>
diff --git a/Documentation/DocBook/media/v4l/cec-func-close.xml b/Documentation/DocBook/media/v4l/cec-func-close.xml
new file mode 100644
index 000000000000..0812c8cd9634
--- /dev/null
+++ b/Documentation/DocBook/media/v4l/cec-func-close.xml
@@ -0,0 +1,64 @@
1<refentry id="cec-func-close">
2 <refmeta>
3 <refentrytitle>cec close()</refentrytitle>
4 &manvol;
5 </refmeta>
6
7 <refnamediv>
8 <refname>cec-close</refname>
9 <refpurpose>Close a cec device</refpurpose>
10 </refnamediv>
11
12 <refsynopsisdiv>
13 <funcsynopsis>
14 <funcsynopsisinfo>#include &lt;unistd.h&gt;</funcsynopsisinfo>
15 <funcprototype>
16 <funcdef>int <function>close</function></funcdef>
17 <paramdef>int <parameter>fd</parameter></paramdef>
18 </funcprototype>
19 </funcsynopsis>
20 </refsynopsisdiv>
21
22 <refsect1>
23 <title>Arguments</title>
24
25 <variablelist>
26 <varlistentry>
27 <term><parameter>fd</parameter></term>
28 <listitem>
29 <para>&fd;</para>
30 </listitem>
31 </varlistentry>
32 </variablelist>
33 </refsect1>
34
35 <refsect1>
36 <title>Description</title>
37
38 <para>
39 Note: this documents the proposed CEC API. This API is not yet finalized and
40 is currently only available as a staging kernel module.
41 </para>
42
43 <para>Closes the cec device. Resources associated with the file descriptor
44 are freed. The device configuration remain unchanged.</para>
45 </refsect1>
46
47 <refsect1>
48 <title>Return Value</title>
49
50 <para><function>close</function> returns 0 on success. On error, -1 is
51 returned, and <varname>errno</varname> is set appropriately. Possible error
52 codes are:</para>
53
54 <variablelist>
55 <varlistentry>
56 <term><errorcode>EBADF</errorcode></term>
57 <listitem>
58 <para><parameter>fd</parameter> is not a valid open file descriptor.
59 </para>
60 </listitem>
61 </varlistentry>
62 </variablelist>
63 </refsect1>
64</refentry>
diff --git a/Documentation/DocBook/media/v4l/cec-func-ioctl.xml b/Documentation/DocBook/media/v4l/cec-func-ioctl.xml
new file mode 100644
index 000000000000..f92817a2dc80
--- /dev/null
+++ b/Documentation/DocBook/media/v4l/cec-func-ioctl.xml
@@ -0,0 +1,78 @@
1<refentry id="cec-func-ioctl">
2 <refmeta>
3 <refentrytitle>cec ioctl()</refentrytitle>
4 &manvol;
5 </refmeta>
6
7 <refnamediv>
8 <refname>cec-ioctl</refname>
9 <refpurpose>Control a cec device</refpurpose>
10 </refnamediv>
11
12 <refsynopsisdiv>
13 <funcsynopsis>
14 <funcsynopsisinfo>#include &lt;sys/ioctl.h&gt;</funcsynopsisinfo>
15 <funcprototype>
16 <funcdef>int <function>ioctl</function></funcdef>
17 <paramdef>int <parameter>fd</parameter></paramdef>
18 <paramdef>int <parameter>request</parameter></paramdef>
19 <paramdef>void *<parameter>argp</parameter></paramdef>
20 </funcprototype>
21 </funcsynopsis>
22 </refsynopsisdiv>
23
24 <refsect1>
25 <title>Arguments</title>
26
27 <variablelist>
28 <varlistentry>
29 <term><parameter>fd</parameter></term>
30 <listitem>
31 <para>&fd;</para>
32 </listitem>
33 </varlistentry>
34 <varlistentry>
35 <term><parameter>request</parameter></term>
36 <listitem>
37 <para>CEC ioctl request code as defined in the cec.h header file,
38 for example CEC_ADAP_G_CAPS.</para>
39 </listitem>
40 </varlistentry>
41 <varlistentry>
42 <term><parameter>argp</parameter></term>
43 <listitem>
44 <para>Pointer to a request-specific structure.</para>
45 </listitem>
46 </varlistentry>
47 </variablelist>
48 </refsect1>
49
50 <refsect1>
51 <title>Description</title>
52 <para>
53 Note: this documents the proposed CEC API. This API is not yet finalized and
54 is currently only available as a staging kernel module.
55 </para>
56
57 <para>The <function>ioctl()</function> function manipulates cec device
58 parameters. The argument <parameter>fd</parameter> must be an open file
59 descriptor.</para>
60 <para>The ioctl <parameter>request</parameter> code specifies the cec
61 function to be called. It has encoded in it whether the argument is an
62 input, output or read/write parameter, and the size of the argument
63 <parameter>argp</parameter> in bytes.</para>
64 <para>Macros and structures definitions specifying cec ioctl requests and
65 their parameters are located in the cec.h header file. All cec ioctl
66 requests, their respective function and parameters are specified in
67 <xref linkend="cec-user-func" />.</para>
68 </refsect1>
69
70 <refsect1>
71 &return-value;
72
73 <para>Request-specific error codes are listed in the
74 individual requests descriptions.</para>
75 <para>When an ioctl that takes an output or read/write parameter fails,
76 the parameter remains unmodified.</para>
77 </refsect1>
78</refentry>
diff --git a/Documentation/DocBook/media/v4l/cec-func-open.xml b/Documentation/DocBook/media/v4l/cec-func-open.xml
new file mode 100644
index 000000000000..2edc5555b81a
--- /dev/null
+++ b/Documentation/DocBook/media/v4l/cec-func-open.xml
@@ -0,0 +1,104 @@
1<refentry id="cec-func-open">
2 <refmeta>
3 <refentrytitle>cec open()</refentrytitle>
4 &manvol;
5 </refmeta>
6
7 <refnamediv>
8 <refname>cec-open</refname>
9 <refpurpose>Open a cec device</refpurpose>
10 </refnamediv>
11
12 <refsynopsisdiv>
13 <funcsynopsis>
14 <funcsynopsisinfo>#include &lt;fcntl.h&gt;</funcsynopsisinfo>
15 <funcprototype>
16 <funcdef>int <function>open</function></funcdef>
17 <paramdef>const char *<parameter>device_name</parameter></paramdef>
18 <paramdef>int <parameter>flags</parameter></paramdef>
19 </funcprototype>
20 </funcsynopsis>
21 </refsynopsisdiv>
22
23 <refsect1>
24 <title>Arguments</title>
25
26 <variablelist>
27 <varlistentry>
28 <term><parameter>device_name</parameter></term>
29 <listitem>
30 <para>Device to be opened.</para>
31 </listitem>
32 </varlistentry>
33 <varlistentry>
34 <term><parameter>flags</parameter></term>
35 <listitem>
36 <para>Open flags. Access mode must be <constant>O_RDWR</constant>.
37 </para>
38 <para>When the <constant>O_NONBLOCK</constant> flag is
39given, the &CEC-RECEIVE; ioctl will return &EAGAIN; when no message is
40available, and the &CEC-TRANSMIT;, &CEC-ADAP-S-PHYS-ADDR; and
41&CEC-ADAP-S-LOG-ADDRS; ioctls all act in non-blocking mode.</para>
42 <para>Other flags have no effect.</para>
43 </listitem>
44 </varlistentry>
45 </variablelist>
46 </refsect1>
47 <refsect1>
48 <title>Description</title>
49 <para>
50 Note: this documents the proposed CEC API. This API is not yet finalized and
51 is currently only available as a staging kernel module.
52 </para>
53
54 <para>To open a cec device applications call <function>open()</function>
55 with the desired device name. The function has no side effects; the device
56 configuration remain unchanged.</para>
57 <para>When the device is opened in read-only mode, attempts to modify its
58 configuration will result in an error, and <varname>errno</varname> will be
59 set to <errorcode>EBADF</errorcode>.</para>
60 </refsect1>
61 <refsect1>
62 <title>Return Value</title>
63
64 <para><function>open</function> returns the new file descriptor on success.
65 On error, -1 is returned, and <varname>errno</varname> is set appropriately.
66 Possible error codes include:</para>
67
68 <variablelist>
69 <varlistentry>
70 <term><errorcode>EACCES</errorcode></term>
71 <listitem>
72 <para>The requested access to the file is not allowed.</para>
73 </listitem>
74 </varlistentry>
75 <varlistentry>
76 <term><errorcode>EMFILE</errorcode></term>
77 <listitem>
78 <para>The process already has the maximum number of files open.
79 </para>
80 </listitem>
81 </varlistentry>
82 <varlistentry>
83 <term><errorcode>ENFILE</errorcode></term>
84 <listitem>
85 <para>The system limit on the total number of open files has been
86 reached.</para>
87 </listitem>
88 </varlistentry>
89 <varlistentry>
90 <term><errorcode>ENOMEM</errorcode></term>
91 <listitem>
92 <para>Insufficient kernel memory was available.</para>
93 </listitem>
94 </varlistentry>
95 <varlistentry>
96 <term><errorcode>ENXIO</errorcode></term>
97 <listitem>
98 <para>No device corresponding to this device special file exists.
99 </para>
100 </listitem>
101 </varlistentry>
102 </variablelist>
103 </refsect1>
104</refentry>
diff --git a/Documentation/DocBook/media/v4l/cec-func-poll.xml b/Documentation/DocBook/media/v4l/cec-func-poll.xml
new file mode 100644
index 000000000000..1bddbde0142d
--- /dev/null
+++ b/Documentation/DocBook/media/v4l/cec-func-poll.xml
@@ -0,0 +1,94 @@
1<refentry id="cec-func-poll">
2 <refmeta>
3 <refentrytitle>cec poll()</refentrytitle>
4 &manvol;
5 </refmeta>
6
7 <refnamediv>
8 <refname>cec-poll</refname>
9 <refpurpose>Wait for some event on a file descriptor</refpurpose>
10 </refnamediv>
11
12 <refsynopsisdiv>
13 <funcsynopsis>
14 <funcsynopsisinfo>#include &lt;sys/poll.h&gt;</funcsynopsisinfo>
15 <funcprototype>
16 <funcdef>int <function>poll</function></funcdef>
17 <paramdef>struct pollfd *<parameter>ufds</parameter></paramdef>
18 <paramdef>unsigned int <parameter>nfds</parameter></paramdef>
19 <paramdef>int <parameter>timeout</parameter></paramdef>
20 </funcprototype>
21 </funcsynopsis>
22 </refsynopsisdiv>
23
24 <refsect1>
25 <title>Description</title>
26
27 <para>
28 Note: this documents the proposed CEC API. This API is not yet finalized and
29 is currently only available as a staging kernel module.
30 </para>
31
32 <para>With the <function>poll()</function> function applications
33can wait for CEC events.</para>
34
35 <para>On success <function>poll()</function> returns the number of
36file descriptors that have been selected (that is, file descriptors
37for which the <structfield>revents</structfield> field of the
38respective <structname>pollfd</structname> structure is non-zero).
39CEC devices set the <constant>POLLIN</constant> and
40<constant>POLLRDNORM</constant> flags in the
41<structfield>revents</structfield> field if there are messages in the
42receive queue. If the transmit queue has room for new messages, the
43<constant>POLLOUT</constant> and <constant>POLLWRNORM</constant>
44flags are set. If there are events in the event queue, then the
45<constant>POLLPRI</constant> flag is set.
46When the function timed out it returns a value of zero, on
47failure it returns <returnvalue>-1</returnvalue> and the
48<varname>errno</varname> variable is set appropriately.
49</para>
50
51 <para>For more details see the
52<function>poll()</function> manual page.</para>
53 </refsect1>
54
55 <refsect1>
56 <title>Return Value</title>
57
58 <para>On success, <function>poll()</function> returns the number
59structures which have non-zero <structfield>revents</structfield>
60fields, or zero if the call timed out. On error
61<returnvalue>-1</returnvalue> is returned, and the
62<varname>errno</varname> variable is set appropriately:</para>
63
64 <variablelist>
65 <varlistentry>
66 <term><errorcode>EBADF</errorcode></term>
67 <listitem>
68 <para>One or more of the <parameter>ufds</parameter> members
69specify an invalid file descriptor.</para>
70 </listitem>
71 </varlistentry>
72 <varlistentry>
73 <term><errorcode>EFAULT</errorcode></term>
74 <listitem>
75 <para><parameter>ufds</parameter> references an inaccessible
76memory area.</para>
77 </listitem>
78 </varlistentry>
79 <varlistentry>
80 <term><errorcode>EINTR</errorcode></term>
81 <listitem>
82 <para>The call was interrupted by a signal.</para>
83 </listitem>
84 </varlistentry>
85 <varlistentry>
86 <term><errorcode>EINVAL</errorcode></term>
87 <listitem>
88 <para>The <parameter>nfds</parameter> argument is greater
89than <constant>OPEN_MAX</constant>.</para>
90 </listitem>
91 </varlistentry>
92 </variablelist>
93 </refsect1>
94</refentry>
diff --git a/Documentation/DocBook/media/v4l/cec-ioc-adap-g-caps.xml b/Documentation/DocBook/media/v4l/cec-ioc-adap-g-caps.xml
new file mode 100644
index 000000000000..3523ef2259b1
--- /dev/null
+++ b/Documentation/DocBook/media/v4l/cec-ioc-adap-g-caps.xml
@@ -0,0 +1,151 @@
1<refentry id="cec-ioc-adap-g-caps">
2 <refmeta>
3 <refentrytitle>ioctl CEC_ADAP_G_CAPS</refentrytitle>
4 &manvol;
5 </refmeta>
6
7 <refnamediv>
8 <refname>CEC_ADAP_G_CAPS</refname>
9 <refpurpose>Query device capabilities</refpurpose>
10 </refnamediv>
11
12 <refsynopsisdiv>
13 <funcsynopsis>
14 <funcprototype>
15 <funcdef>int <function>ioctl</function></funcdef>
16 <paramdef>int <parameter>fd</parameter></paramdef>
17 <paramdef>int <parameter>request</parameter></paramdef>
18 <paramdef>struct cec_caps *<parameter>argp</parameter></paramdef>
19 </funcprototype>
20 </funcsynopsis>
21 </refsynopsisdiv>
22
23 <refsect1>
24 <title>Arguments</title>
25
26 <variablelist>
27 <varlistentry>
28 <term><parameter>fd</parameter></term>
29 <listitem>
30 <para>File descriptor returned by
31 <link linkend='cec-func-open'><function>open()</function></link>.</para>
32 </listitem>
33 </varlistentry>
34 <varlistentry>
35 <term><parameter>request</parameter></term>
36 <listitem>
37 <para>CEC_ADAP_G_CAPS</para>
38 </listitem>
39 </varlistentry>
40 <varlistentry>
41 <term><parameter>argp</parameter></term>
42 <listitem>
43 <para></para>
44 </listitem>
45 </varlistentry>
46 </variablelist>
47 </refsect1>
48
49 <refsect1>
50 <title>Description</title>
51
52 <para>
53 Note: this documents the proposed CEC API. This API is not yet finalized and
54 is currently only available as a staging kernel module.
55 </para>
56
57 <para>All cec devices must support the <constant>CEC_ADAP_G_CAPS</constant>
58 ioctl. To query device information, applications call the ioctl with a
59 pointer to a &cec-caps;. The driver fills the structure and returns
60 the information to the application.
61 The ioctl never fails.</para>
62
63 <table pgwide="1" frame="none" id="cec-caps">
64 <title>struct <structname>cec_caps</structname></title>
65 <tgroup cols="3">
66 &cs-str;
67 <tbody valign="top">
68 <row>
69 <entry>char</entry>
70 <entry><structfield>driver[32]</structfield></entry>
71 <entry>The name of the cec adapter driver.</entry>
72 </row>
73 <row>
74 <entry>char</entry>
75 <entry><structfield>name[32]</structfield></entry>
76 <entry>The name of this CEC adapter. The combination <structfield>driver</structfield>
77 and <structfield>name</structfield> must be unique.</entry>
78 </row>
79 <row>
80 <entry>__u32</entry>
81 <entry><structfield>capabilities</structfield></entry>
82 <entry>The capabilities of the CEC adapter, see <xref
83 linkend="cec-capabilities" />.</entry>
84 </row>
85 <row>
86 <entry>__u32</entry>
87 <entry><structfield>version</structfield></entry>
88 <entry>CEC Framework API version, formatted with the
89 <constant>KERNEL_VERSION()</constant> macro.</entry>
90 </row>
91 </tbody>
92 </tgroup>
93 </table>
94
95 <table pgwide="1" frame="none" id="cec-capabilities">
96 <title>CEC Capabilities Flags</title>
97 <tgroup cols="3">
98 &cs-def;
99 <tbody valign="top">
100 <row>
101 <entry><constant>CEC_CAP_PHYS_ADDR</constant></entry>
102 <entry>0x00000001</entry>
103 <entry>Userspace has to configure the physical address by
104 calling &CEC-ADAP-S-PHYS-ADDR;. If this capability isn't set,
105 then setting the physical address is handled by the kernel
106 whenever the EDID is set (for an HDMI receiver) or read (for
107 an HDMI transmitter).</entry>
108 </row>
109 <row>
110 <entry><constant>CEC_CAP_LOG_ADDRS</constant></entry>
111 <entry>0x00000002</entry>
112 <entry>Userspace has to configure the logical addresses by
113 calling &CEC-ADAP-S-LOG-ADDRS;. If this capability isn't set,
114 then the kernel will have configured this.</entry>
115 </row>
116 <row>
117 <entry><constant>CEC_CAP_TRANSMIT</constant></entry>
118 <entry>0x00000004</entry>
119 <entry>Userspace can transmit CEC messages by calling &CEC-TRANSMIT;. This
120 implies that userspace can be a follower as well, since being able to
121 transmit messages is a prerequisite of becoming a follower. If this
122 capability isn't set, then the kernel will handle all CEC transmits
123 and process all CEC messages it receives.
124 </entry>
125 </row>
126 <row>
127 <entry><constant>CEC_CAP_PASSTHROUGH</constant></entry>
128 <entry>0x00000008</entry>
129 <entry>Userspace can use the passthrough mode by
130 calling &CEC-S-MODE;.</entry>
131 </row>
132 <row>
133 <entry><constant>CEC_CAP_RC</constant></entry>
134 <entry>0x00000010</entry>
135 <entry>This adapter supports the remote control protocol.</entry>
136 </row>
137 <row>
138 <entry><constant>CEC_CAP_MONITOR_ALL</constant></entry>
139 <entry>0x00000020</entry>
140 <entry>The CEC hardware can monitor all messages, not just directed and
141 broadcast messages.</entry>
142 </row>
143 </tbody>
144 </tgroup>
145 </table>
146 </refsect1>
147
148 <refsect1>
149 &return-value;
150 </refsect1>
151</refentry>
diff --git a/Documentation/DocBook/media/v4l/cec-ioc-adap-g-log-addrs.xml b/Documentation/DocBook/media/v4l/cec-ioc-adap-g-log-addrs.xml
new file mode 100644
index 000000000000..302b8294f7fc
--- /dev/null
+++ b/Documentation/DocBook/media/v4l/cec-ioc-adap-g-log-addrs.xml
@@ -0,0 +1,329 @@
1<refentry id="cec-ioc-adap-g-log-addrs">
2 <refmeta>
3 <refentrytitle>ioctl CEC_ADAP_G_LOG_ADDRS, CEC_ADAP_S_LOG_ADDRS</refentrytitle>
4 &manvol;
5 </refmeta>
6
7 <refnamediv>
8 <refname>CEC_ADAP_G_LOG_ADDRS</refname>
9 <refname>CEC_ADAP_S_LOG_ADDRS</refname>
10 <refpurpose>Get or set the logical addresses</refpurpose>
11 </refnamediv>
12
13 <refsynopsisdiv>
14 <funcsynopsis>
15 <funcprototype>
16 <funcdef>int <function>ioctl</function></funcdef>
17 <paramdef>int <parameter>fd</parameter></paramdef>
18 <paramdef>int <parameter>request</parameter></paramdef>
19 <paramdef>struct cec_log_addrs *<parameter>argp</parameter></paramdef>
20 </funcprototype>
21 </funcsynopsis>
22 </refsynopsisdiv>
23
24 <refsect1>
25 <title>Arguments</title>
26
27 <variablelist>
28 <varlistentry>
29 <term><parameter>fd</parameter></term>
30 <listitem>
31 <para>File descriptor returned by
32 <link linkend='cec-func-open'><function>open()</function></link>.</para>
33 </listitem>
34 </varlistentry>
35 <varlistentry>
36 <term><parameter>request</parameter></term>
37 <listitem>
38 <para>CEC_ADAP_G_LOG_ADDRS, CEC_ADAP_S_LOG_ADDRS</para>
39 </listitem>
40 </varlistentry>
41 <varlistentry>
42 <term><parameter>argp</parameter></term>
43 <listitem>
44 <para></para>
45 </listitem>
46 </varlistentry>
47 </variablelist>
48 </refsect1>
49
50 <refsect1>
51 <title>Description</title>
52
53 <para>
54 Note: this documents the proposed CEC API. This API is not yet finalized and
55 is currently only available as a staging kernel module.
56 </para>
57
58 <para>To query the current CEC logical addresses, applications call the
59<constant>CEC_ADAP_G_LOG_ADDRS</constant> ioctl with a pointer to a
60<structname>cec_log_addrs</structname> structure where the drivers stores the
61logical addresses.</para>
62
63 <para>To set new logical addresses, applications fill in struct <structname>cec_log_addrs</structname>
64and call the <constant>CEC_ADAP_S_LOG_ADDRS</constant> ioctl with a pointer to this struct.
65The <constant>CEC_ADAP_S_LOG_ADDRS</constant> ioctl is only available if
66<constant>CEC_CAP_LOG_ADDRS</constant> is set (&ENOTTY; is returned otherwise). This ioctl will block until all
67requested logical addresses have been claimed. <constant>CEC_ADAP_S_LOG_ADDRS</constant>
68can only be called by a file handle in initiator mode (see &CEC-S-MODE;).</para>
69
70 <table pgwide="1" frame="none" id="cec-log-addrs">
71 <title>struct <structname>cec_log_addrs</structname></title>
72 <tgroup cols="3">
73 &cs-str;
74 <tbody valign="top">
75 <row>
76 <entry>__u8</entry>
77 <entry><structfield>log_addr</structfield>[CEC_MAX_LOG_ADDRS]</entry>
78 <entry>The actual logical addresses that were claimed. This is set by the
79 driver. If no logical address could be claimed, then it is set to
80 <constant>CEC_LOG_ADDR_INVALID</constant>. If this adapter is Unregistered,
81 then <structfield>log_addr[0]</structfield> is set to 0xf and all others to
82 <constant>CEC_LOG_ADDR_INVALID</constant>.</entry>
83 </row>
84 <row>
85 <entry>__u16</entry>
86 <entry><structfield>log_addr_mask</structfield></entry>
87 <entry>The bitmask of all logical addresses this adapter has claimed.
88 If this adapter is Unregistered then <structfield>log_addr_mask</structfield>
89 sets bit 15 and clears all other bits. If this adapter is not configured at all, then
90 <structfield>log_addr_mask</structfield> is set to 0. Set by the driver.</entry>
91 </row>
92 <row>
93 <entry>__u8</entry>
94 <entry><structfield>cec_version</structfield></entry>
95 <entry>The CEC version that this adapter shall use. See
96 <xref linkend="cec-versions" />.
97 Used to implement the <constant>CEC_MSG_CEC_VERSION</constant> and
98 <constant>CEC_MSG_REPORT_FEATURES</constant> messages. Note that
99 <constant>CEC_OP_CEC_VERSION_1_3A</constant> is not allowed
100 by the CEC framework.
101 </entry>
102 </row>
103 <row>
104 <entry>__u8</entry>
105 <entry><structfield>num_log_addrs</structfield></entry>
106 <entry>Number of logical addresses to set up. Must be &le;
107 <structfield>available_log_addrs</structfield> as returned by
108 &CEC-ADAP-G-CAPS;. All arrays in this structure are only filled up to
109 index <structfield>available_log_addrs</structfield>-1. The remaining
110 array elements will be ignored. Note that the CEC 2.0 standard allows
111 for a maximum of 2 logical addresses, although some hardware has support
112 for more. <constant>CEC_MAX_LOG_ADDRS</constant> is 4. The driver will
113 return the actual number of logical addresses it could claim, which may
114 be less than what was requested. If this field is set to 0, then the
115 CEC adapter shall clear all claimed logical addresses and all other
116 fields will be ignored.</entry>
117 </row>
118 <row>
119 <entry>__u32</entry>
120 <entry><structfield>vendor_id</structfield></entry>
121 <entry>The vendor ID is a 24-bit number that identifies the specific
122 vendor or entity. Based on this ID vendor specific commands may be
123 defined. If you do not want a vendor ID then set it to
124 <constant>CEC_VENDOR_ID_NONE</constant>.</entry>
125 </row>
126 <row>
127 <entry>__u32</entry>
128 <entry><structfield>flags</structfield></entry>
129 <entry>Flags. No flags are defined yet, so set this to 0.</entry>
130 </row>
131 <row>
132 <entry>char</entry>
133 <entry><structfield>osd_name</structfield>[15]</entry>
134 <entry>The On-Screen Display name as is returned by the
135 <constant>CEC_MSG_SET_OSD_NAME</constant> message.</entry>
136 </row>
137 <row>
138 <entry>__u8</entry>
139 <entry><structfield>primary_device_type</structfield>[CEC_MAX_LOG_ADDRS]</entry>
140 <entry>Primary device type for each logical address. See
141 <xref linkend="cec-prim-dev-types" /> for possible types.</entry>
142 </row>
143 <row>
144 <entry>__u8</entry>
145 <entry><structfield>log_addr_type</structfield>[CEC_MAX_LOG_ADDRS]</entry>
146 <entry>Logical address types. See <xref linkend="cec-log-addr-types" /> for
147 possible types. The driver will update this with the actual logical address
148 type that it claimed (e.g. it may have to fallback to
149 <constant>CEC_LOG_ADDR_TYPE_UNREGISTERED</constant>).</entry>
150 </row>
151 <row>
152 <entry>__u8</entry>
153 <entry><structfield>all_device_types</structfield>[CEC_MAX_LOG_ADDRS]</entry>
154 <entry>CEC 2.0 specific: all device types. See <xref linkend="cec-all-dev-types-flags" />.
155 Used to implement the <constant>CEC_MSG_REPORT_FEATURES</constant> message.
156 This field is ignored if <structfield>cec_version</structfield> &lt;
157 <constant>CEC_OP_CEC_VERSION_2_0</constant>.</entry>
158 </row>
159 <row>
160 <entry>__u8</entry>
161 <entry><structfield>features</structfield>[CEC_MAX_LOG_ADDRS][12]</entry>
162 <entry>Features for each logical address. Used to implement the
163 <constant>CEC_MSG_REPORT_FEATURES</constant> message. The 12 bytes include
164 both the RC Profile and the Device Features.
165 This field is ignored if <structfield>cec_version</structfield> &lt;
166 <constant>CEC_OP_CEC_VERSION_2_0</constant>.</entry>
167 </row>
168 </tbody>
169 </tgroup>
170 </table>
171
172 <table pgwide="1" frame="none" id="cec-versions">
173 <title>CEC Versions</title>
174 <tgroup cols="3">
175 &cs-def;
176 <tbody valign="top">
177 <row>
178 <entry><constant>CEC_OP_CEC_VERSION_1_3A</constant></entry>
179 <entry>4</entry>
180 <entry>CEC version according to the HDMI 1.3a standard.</entry>
181 </row>
182 <row>
183 <entry><constant>CEC_OP_CEC_VERSION_1_4B</constant></entry>
184 <entry>5</entry>
185 <entry>CEC version according to the HDMI 1.4b standard.</entry>
186 </row>
187 <row>
188 <entry><constant>CEC_OP_CEC_VERSION_2_0</constant></entry>
189 <entry>6</entry>
190 <entry>CEC version according to the HDMI 2.0 standard.</entry>
191 </row>
192 </tbody>
193 </tgroup>
194 </table>
195
196 <table pgwide="1" frame="none" id="cec-prim-dev-types">
197 <title>CEC Primary Device Types</title>
198 <tgroup cols="3">
199 &cs-def;
200 <tbody valign="top">
201 <row>
202 <entry><constant>CEC_OP_PRIM_DEVTYPE_TV</constant></entry>
203 <entry>0</entry>
204 <entry>Use for a TV.</entry>
205 </row>
206 <row>
207 <entry><constant>CEC_OP_PRIM_DEVTYPE_RECORD</constant></entry>
208 <entry>1</entry>
209 <entry>Use for a recording device.</entry>
210 </row>
211 <row>
212 <entry><constant>CEC_OP_PRIM_DEVTYPE_TUNER</constant></entry>
213 <entry>3</entry>
214 <entry>Use for a device with a tuner.</entry>
215 </row>
216 <row>
217 <entry><constant>CEC_OP_PRIM_DEVTYPE_PLAYBACK</constant></entry>
218 <entry>4</entry>
219 <entry>Use for a playback device.</entry>
220 </row>
221 <row>
222 <entry><constant>CEC_OP_PRIM_DEVTYPE_AUDIOSYSTEM</constant></entry>
223 <entry>5</entry>
224 <entry>Use for an audio system (e.g. an audio/video receiver).</entry>
225 </row>
226 <row>
227 <entry><constant>CEC_OP_PRIM_DEVTYPE_SWITCH</constant></entry>
228 <entry>6</entry>
229 <entry>Use for a CEC switch.</entry>
230 </row>
231 <row>
232 <entry><constant>CEC_OP_PRIM_DEVTYPE_VIDEOPROC</constant></entry>
233 <entry>7</entry>
234 <entry>Use for a video processor device.</entry>
235 </row>
236 </tbody>
237 </tgroup>
238 </table>
239
240 <table pgwide="1" frame="none" id="cec-log-addr-types">
241 <title>CEC Logical Address Types</title>
242 <tgroup cols="3">
243 &cs-def;
244 <tbody valign="top">
245 <row>
246 <entry><constant>CEC_LOG_ADDR_TYPE_TV</constant></entry>
247 <entry>0</entry>
248 <entry>Use for a TV.</entry>
249 </row>
250 <row>
251 <entry><constant>CEC_LOG_ADDR_TYPE_RECORD</constant></entry>
252 <entry>1</entry>
253 <entry>Use for a recording device.</entry>
254 </row>
255 <row>
256 <entry><constant>CEC_LOG_ADDR_TYPE_TUNER</constant></entry>
257 <entry>2</entry>
258 <entry>Use for a tuner device.</entry>
259 </row>
260 <row>
261 <entry><constant>CEC_LOG_ADDR_TYPE_PLAYBACK</constant></entry>
262 <entry>3</entry>
263 <entry>Use for a playback device.</entry>
264 </row>
265 <row>
266 <entry><constant>CEC_LOG_ADDR_TYPE_AUDIOSYSTEM</constant></entry>
267 <entry>4</entry>
268 <entry>Use for an audio system device.</entry>
269 </row>
270 <row>
271 <entry><constant>CEC_LOG_ADDR_TYPE_SPECIFIC</constant></entry>
272 <entry>5</entry>
273 <entry>Use for a second TV or for a video processor device.</entry>
274 </row>
275 <row>
276 <entry><constant>CEC_LOG_ADDR_TYPE_UNREGISTERED</constant></entry>
277 <entry>6</entry>
278 <entry>Use this if you just want to remain unregistered.
279 Used for pure CEC switches or CDC-only devices (CDC:
280 Capability Discovery and Control).</entry>
281 </row>
282 </tbody>
283 </tgroup>
284 </table>
285
286 <table pgwide="1" frame="none" id="cec-all-dev-types-flags">
287 <title>CEC All Device Types Flags</title>
288 <tgroup cols="3">
289 &cs-def;
290 <tbody valign="top">
291 <row>
292 <entry><constant>CEC_OP_ALL_DEVTYPE_TV</constant></entry>
293 <entry>0x80</entry>
294 <entry>This supports the TV type.</entry>
295 </row>
296 <row>
297 <entry><constant>CEC_OP_ALL_DEVTYPE_RECORD</constant></entry>
298 <entry>0x40</entry>
299 <entry>This supports the Recording type.</entry>
300 </row>
301 <row>
302 <entry><constant>CEC_OP_ALL_DEVTYPE_TUNER</constant></entry>
303 <entry>0x20</entry>
304 <entry>This supports the Tuner type.</entry>
305 </row>
306 <row>
307 <entry><constant>CEC_OP_ALL_DEVTYPE_PLAYBACK</constant></entry>
308 <entry>0x10</entry>
309 <entry>This supports the Playback type.</entry>
310 </row>
311 <row>
312 <entry><constant>CEC_OP_ALL_DEVTYPE_AUDIOSYSTEM</constant></entry>
313 <entry>0x08</entry>
314 <entry>This supports the Audio System type.</entry>
315 </row>
316 <row>
317 <entry><constant>CEC_OP_ALL_DEVTYPE_SWITCH</constant></entry>
318 <entry>0x04</entry>
319 <entry>This supports the CEC Switch or Video Processing type.</entry>
320 </row>
321 </tbody>
322 </tgroup>
323 </table>
324 </refsect1>
325
326 <refsect1>
327 &return-value;
328 </refsect1>
329</refentry>
diff --git a/Documentation/DocBook/media/v4l/cec-ioc-adap-g-phys-addr.xml b/Documentation/DocBook/media/v4l/cec-ioc-adap-g-phys-addr.xml
new file mode 100644
index 000000000000..d95f1785080c
--- /dev/null
+++ b/Documentation/DocBook/media/v4l/cec-ioc-adap-g-phys-addr.xml
@@ -0,0 +1,86 @@
1<refentry id="cec-ioc-adap-g-phys-addr">
2 <refmeta>
3 <refentrytitle>ioctl CEC_ADAP_G_PHYS_ADDR, CEC_ADAP_S_PHYS_ADDR</refentrytitle>
4 &manvol;
5 </refmeta>
6
7 <refnamediv>
8 <refname>CEC_ADAP_G_PHYS_ADDR</refname>
9 <refname>CEC_ADAP_S_PHYS_ADDR</refname>
10 <refpurpose>Get or set the physical address</refpurpose>
11 </refnamediv>
12
13 <refsynopsisdiv>
14 <funcsynopsis>
15 <funcprototype>
16 <funcdef>int <function>ioctl</function></funcdef>
17 <paramdef>int <parameter>fd</parameter></paramdef>
18 <paramdef>int <parameter>request</parameter></paramdef>
19 <paramdef>__u16 *<parameter>argp</parameter></paramdef>
20 </funcprototype>
21 </funcsynopsis>
22 </refsynopsisdiv>
23
24 <refsect1>
25 <title>Arguments</title>
26
27 <variablelist>
28 <varlistentry>
29 <term><parameter>fd</parameter></term>
30 <listitem>
31 <para>File descriptor returned by
32 <link linkend='cec-func-open'><function>open()</function></link>.</para>
33 </listitem>
34 </varlistentry>
35 <varlistentry>
36 <term><parameter>request</parameter></term>
37 <listitem>
38 <para>CEC_ADAP_G_PHYS_ADDR, CEC_ADAP_S_PHYS_ADDR</para>
39 </listitem>
40 </varlistentry>
41 <varlistentry>
42 <term><parameter>argp</parameter></term>
43 <listitem>
44 <para></para>
45 </listitem>
46 </varlistentry>
47 </variablelist>
48 </refsect1>
49
50 <refsect1>
51 <title>Description</title>
52
53 <para>
54 Note: this documents the proposed CEC API. This API is not yet finalized and
55 is currently only available as a staging kernel module.
56 </para>
57
58 <para>To query the current physical address applications call the
59<constant>CEC_ADAP_G_PHYS_ADDR</constant> ioctl with a pointer to an __u16
60where the driver stores the physical address.</para>
61
62 <para>To set a new physical address applications store the physical address in
63an __u16 and call the <constant>CEC_ADAP_S_PHYS_ADDR</constant> ioctl with a
64pointer to this integer. <constant>CEC_ADAP_S_PHYS_ADDR</constant> is only
65available if <constant>CEC_CAP_PHYS_ADDR</constant> is set (&ENOTTY; will be returned
66otherwise). <constant>CEC_ADAP_S_PHYS_ADDR</constant>
67can only be called by a file handle in initiator mode (see &CEC-S-MODE;), if not
68&EBUSY; will be returned.</para>
69
70 <para>The physical address is a 16-bit number where each group of 4 bits
71represent a digit of the physical address a.b.c.d where the most significant
724 bits represent 'a'. The CEC root device (usually the TV) has address 0.0.0.0.
73Every device that is hooked up to an input of the TV has address a.0.0.0 (where
74'a' is &ge; 1), devices hooked up to those in turn have addresses a.b.0.0, etc.
75So a topology of up to 5 devices deep is supported. The physical address a
76device shall use is stored in the EDID of the sink.</para>
77
78<para>For example, the EDID for each HDMI input of the TV will have a different
79physical address of the form a.0.0.0 that the sources will read out and use as
80their physical address.</para>
81 </refsect1>
82
83 <refsect1>
84 &return-value;
85 </refsect1>
86</refentry>
diff --git a/Documentation/DocBook/media/v4l/cec-ioc-dqevent.xml b/Documentation/DocBook/media/v4l/cec-ioc-dqevent.xml
new file mode 100644
index 000000000000..697dde575cd4
--- /dev/null
+++ b/Documentation/DocBook/media/v4l/cec-ioc-dqevent.xml
@@ -0,0 +1,202 @@
1<refentry id="cec-ioc-g-event">
2 <refmeta>
3 <refentrytitle>ioctl CEC_DQEVENT</refentrytitle>
4 &manvol;
5 </refmeta>
6
7 <refnamediv>
8 <refname>CEC_DQEVENT</refname>
9 <refpurpose>Dequeue a CEC event</refpurpose>
10 </refnamediv>
11
12 <refsynopsisdiv>
13 <funcsynopsis>
14 <funcprototype>
15 <funcdef>int <function>ioctl</function></funcdef>
16 <paramdef>int <parameter>fd</parameter></paramdef>
17 <paramdef>int <parameter>request</parameter></paramdef>
18 <paramdef>struct cec_event *<parameter>argp</parameter></paramdef>
19 </funcprototype>
20 </funcsynopsis>
21 </refsynopsisdiv>
22
23 <refsect1>
24 <title>Arguments</title>
25
26 <variablelist>
27 <varlistentry>
28 <term><parameter>fd</parameter></term>
29 <listitem>
30 <para>File descriptor returned by
31 <link linkend='cec-func-open'><function>open()</function></link>.</para>
32 </listitem>
33 </varlistentry>
34 <varlistentry>
35 <term><parameter>request</parameter></term>
36 <listitem>
37 <para>CEC_DQEVENT</para>
38 </listitem>
39 </varlistentry>
40 <varlistentry>
41 <term><parameter>argp</parameter></term>
42 <listitem>
43 <para></para>
44 </listitem>
45 </varlistentry>
46 </variablelist>
47 </refsect1>
48
49 <refsect1>
50 <title>Description</title>
51
52 <para>
53 Note: this documents the proposed CEC API. This API is not yet finalized and
54 is currently only available as a staging kernel module.
55 </para>
56
57 <para>CEC devices can send asynchronous events. These can be retrieved by calling
58 the <constant>CEC_DQEVENT</constant> ioctl. If the file descriptor is in non-blocking
59 mode and no event is pending, then it will return -1 and set errno to the &EAGAIN;.</para>
60
61 <para>The internal event queues are per-filehandle and per-event type. If there is
62 no more room in a queue then the last event is overwritten with the new one. This
63 means that intermediate results can be thrown away but that the latest event is always
64 available. This also means that is it possible to read two successive events that have
65 the same value (e.g. two CEC_EVENT_STATE_CHANGE events with the same state). In that
66 case the intermediate state changes were lost but it is guaranteed that the state
67 did change in between the two events.</para>
68
69 <table pgwide="1" frame="none" id="cec-event-state-change">
70 <title>struct <structname>cec_event_state_change</structname></title>
71 <tgroup cols="3">
72 &cs-str;
73 <tbody valign="top">
74 <row>
75 <entry>__u16</entry>
76 <entry><structfield>phys_addr</structfield></entry>
77 <entry>The current physical address.</entry>
78 </row>
79 <row>
80 <entry>__u16</entry>
81 <entry><structfield>log_addr_mask</structfield></entry>
82 <entry>The current set of claimed logical addresses.</entry>
83 </row>
84 </tbody>
85 </tgroup>
86 </table>
87
88 <table pgwide="1" frame="none" id="cec-event-lost-msgs">
89 <title>struct <structname>cec_event_lost_msgs</structname></title>
90 <tgroup cols="3">
91 &cs-str;
92 <tbody valign="top">
93 <row>
94 <entry>__u32</entry>
95 <entry><structfield>lost_msgs</structfield></entry>
96 <entry>Set to the number of lost messages since the filehandle
97 was opened or since the last time this event was dequeued for
98 this filehandle. The messages lost are the oldest messages. So
99 when a new message arrives and there is no more room, then the
100 oldest message is discarded to make room for the new one. The
101 internal size of the message queue guarantees that all messages
102 received in the last two seconds will be stored. Since messages
103 should be replied to within a second according to the CEC
104 specification, this is more than enough.
105 </entry>
106 </row>
107 </tbody>
108 </tgroup>
109 </table>
110
111 <table pgwide="1" frame="none" id="cec-event">
112 <title>struct <structname>cec_event</structname></title>
113 <tgroup cols="4">
114 &cs-str;
115 <tbody valign="top">
116 <row>
117 <entry>__u64</entry>
118 <entry><structfield>ts</structfield></entry>
119 <entry>Timestamp of the event in ns.</entry>
120 <entry></entry>
121 </row>
122 <row>
123 <entry>__u32</entry>
124 <entry><structfield>event</structfield></entry>
125 <entry>The CEC event type, see <xref linkend="cec-events" />.</entry>
126 <entry></entry>
127 </row>
128 <row>
129 <entry>__u32</entry>
130 <entry><structfield>flags</structfield></entry>
131 <entry>Event flags, see <xref linkend="cec-event-flags" />.</entry>
132 <entry></entry>
133 </row>
134 <row>
135 <entry>union</entry>
136 <entry>(anonymous)</entry>
137 <entry></entry>
138 <entry></entry>
139 </row>
140 <row>
141 <entry></entry>
142 <entry>struct cec_event_state_change</entry>
143 <entry><structfield>state_change</structfield></entry>
144 <entry>The new adapter state as sent by the <constant>CEC_EVENT_STATE_CHANGE</constant>
145 event.</entry>
146 </row>
147 <row>
148 <entry></entry>
149 <entry>struct cec_event_lost_msgs</entry>
150 <entry><structfield>lost_msgs</structfield></entry>
151 <entry>The number of lost messages as sent by the <constant>CEC_EVENT_LOST_MSGS</constant>
152 event.</entry>
153 </row>
154 </tbody>
155 </tgroup>
156 </table>
157
158 <table pgwide="1" frame="none" id="cec-events">
159 <title>CEC Events Types</title>
160 <tgroup cols="3">
161 &cs-def;
162 <tbody valign="top">
163 <row>
164 <entry><constant>CEC_EVENT_STATE_CHANGE</constant></entry>
165 <entry>1</entry>
166 <entry>Generated when the CEC Adapter's state changes. When open() is
167 called an initial event will be generated for that filehandle with the
168 CEC Adapter's state at that time.
169 </entry>
170 </row>
171 <row>
172 <entry><constant>CEC_EVENT_LOST_MSGS</constant></entry>
173 <entry>2</entry>
174 <entry>Generated if one or more CEC messages were lost because the
175 application didn't dequeue CEC messages fast enough.</entry>
176 </row>
177 </tbody>
178 </tgroup>
179 </table>
180
181 <table pgwide="1" frame="none" id="cec-event-flags">
182 <title>CEC Event Flags</title>
183 <tgroup cols="3">
184 &cs-def;
185 <tbody valign="top">
186 <row>
187 <entry><constant>CEC_EVENT_FL_INITIAL_VALUE</constant></entry>
188 <entry>1</entry>
189 <entry>Set for the initial events that are generated when the device is
190 opened. See the table above for which events do this. This allows
191 applications to learn the initial state of the CEC adapter at open()
192 time.</entry>
193 </row>
194 </tbody>
195 </tgroup>
196 </table>
197 </refsect1>
198
199 <refsect1>
200 &return-value;
201 </refsect1>
202</refentry>
diff --git a/Documentation/DocBook/media/v4l/cec-ioc-g-mode.xml b/Documentation/DocBook/media/v4l/cec-ioc-g-mode.xml
new file mode 100644
index 000000000000..26b4282ad134
--- /dev/null
+++ b/Documentation/DocBook/media/v4l/cec-ioc-g-mode.xml
@@ -0,0 +1,255 @@
1<refentry id="cec-ioc-g-mode">
2 <refmeta>
3 <refentrytitle>ioctl CEC_G_MODE, CEC_S_MODE</refentrytitle>
4 &manvol;
5 </refmeta>
6
7 <refnamediv>
8 <refname>CEC_G_MODE</refname>
9 <refname>CEC_S_MODE</refname>
10 <refpurpose>Get or set exclusive use of the CEC adapter</refpurpose>
11 </refnamediv>
12
13 <refsynopsisdiv>
14 <funcsynopsis>
15 <funcprototype>
16 <funcdef>int <function>ioctl</function></funcdef>
17 <paramdef>int <parameter>fd</parameter></paramdef>
18 <paramdef>int <parameter>request</parameter></paramdef>
19 <paramdef>__u32 *<parameter>argp</parameter></paramdef>
20 </funcprototype>
21 </funcsynopsis>
22 </refsynopsisdiv>
23
24 <refsect1>
25 <title>Arguments</title>
26
27 <variablelist>
28 <varlistentry>
29 <term><parameter>fd</parameter></term>
30 <listitem>
31 <para>File descriptor returned by
32 <link linkend='cec-func-open'><function>open()</function></link>.</para>
33 </listitem>
34 </varlistentry>
35 <varlistentry>
36 <term><parameter>request</parameter></term>
37 <listitem>
38 <para>CEC_G_MODE, CEC_S_MODE</para>
39 </listitem>
40 </varlistentry>
41 <varlistentry>
42 <term><parameter>argp</parameter></term>
43 <listitem>
44 <para></para>
45 </listitem>
46 </varlistentry>
47 </variablelist>
48 </refsect1>
49
50 <refsect1>
51 <title>Description</title>
52
53 <para>
54 Note: this documents the proposed CEC API. This API is not yet finalized and
55 is currently only available as a staging kernel module.
56 </para>
57
58 <para>By default any filehandle can use &CEC-TRANSMIT; and &CEC-RECEIVE;, but
59in order to prevent applications from stepping on each others toes it must be possible
60to obtain exclusive access to the CEC adapter. This ioctl sets the filehandle
61to initiator and/or follower mode which can be exclusive depending on the chosen
62mode. The initiator is the filehandle that is used
63to initiate messages, i.e. it commands other CEC devices. The follower is the filehandle
64that receives messages sent to the CEC adapter and processes them. The same filehandle
65can be both initiator and follower, or this role can be taken by two different
66filehandles.</para>
67
68 <para>When a CEC message is received, then the CEC framework will decide how
69it will be processed. If the message is a reply to an earlier transmitted message,
70then the reply is sent back to the filehandle that is waiting for it. In addition
71the CEC framework will process it.</para>
72
73 <para>If the message is not a reply, then the CEC framework will process it
74first. If there is no follower, then the message is just discarded and a feature
75abort is sent back to the initiator if the framework couldn't process it. If there
76is a follower, then the message is passed on to the follower who will use
77&CEC-RECEIVE; to dequeue the new message. The framework expects the follower to
78make the right decisions.</para>
79
80 <para>The CEC framework will process core messages unless requested otherwise
81by the follower. The follower can enable the passthrough mode. In that case, the
82CEC framework will pass on most core messages without processing them and
83the follower will have to implement those messages. There are some messages
84that the core will always process, regardless of the passthrough mode. See
85<xref linkend="cec-core-processing" /> for details.</para>
86
87 <para>If there is no initiator, then any CEC filehandle can use &CEC-TRANSMIT;.
88If there is an exclusive initiator then only that initiator can call &CEC-TRANSMIT;.
89The follower can of course always call &CEC-TRANSMIT;.</para>
90
91 <para>Available initiator modes are:</para>
92
93 <table pgwide="1" frame="none" id="cec-mode-initiator">
94 <title>Initiator Modes</title>
95 <tgroup cols="3">
96 &cs-def;
97 <tbody valign="top">
98 <row>
99 <entry><constant>CEC_MODE_NO_INITIATOR</constant></entry>
100 <entry>0x0</entry>
101 <entry>This is not an initiator, i.e. it cannot transmit CEC messages
102 or make any other changes to the CEC adapter.</entry>
103 </row>
104 <row>
105 <entry><constant>CEC_MODE_INITIATOR</constant></entry>
106 <entry>0x1</entry>
107 <entry>This is an initiator (the default when the device is opened) and it
108 can transmit CEC messages and make changes to the CEC adapter, unless there
109 is an exclusive initiator.</entry>
110 </row>
111 <row>
112 <entry><constant>CEC_MODE_EXCL_INITIATOR</constant></entry>
113 <entry>0x2</entry>
114 <entry>This is an exclusive initiator and this file descriptor is the only one
115 that can transmit CEC messages and make changes to the CEC adapter. If someone
116 else is already the exclusive initiator then an attempt to become one will return
117 the &EBUSY; error.</entry>
118 </row>
119 </tbody>
120 </tgroup>
121 </table>
122
123 <para>Available follower modes are:</para>
124
125 <table pgwide="1" frame="none" id="cec-mode-follower">
126 <title>Follower Modes</title>
127 <tgroup cols="3">
128 &cs-def;
129 <tbody valign="top">
130 <row>
131 <entry><constant>CEC_MODE_NO_FOLLOWER</constant></entry>
132 <entry>0x00</entry>
133 <entry>This is not a follower (the default when the device is opened).</entry>
134 </row>
135 <row>
136 <entry><constant>CEC_MODE_FOLLOWER</constant></entry>
137 <entry>0x10</entry>
138 <entry>This is a follower and it will receive CEC messages unless there is
139 an exclusive follower. You cannot become a follower if <constant>CEC_CAP_TRANSMIT</constant>
140 is not set or if <constant>CEC_MODE_NO_INITIATOR</constant> was specified,
141 &EINVAL; is returned in that case.</entry>
142 </row>
143 <row>
144 <entry><constant>CEC_MODE_EXCL_FOLLOWER</constant></entry>
145 <entry>0x20</entry>
146 <entry>This is an exclusive follower and only this file descriptor will receive
147 CEC messages for processing. If someone else is already the exclusive follower
148 then an attempt to become one will return the &EBUSY; error. You cannot become
149 a follower if <constant>CEC_CAP_TRANSMIT</constant> is not set or if
150 <constant>CEC_MODE_NO_INITIATOR</constant> was specified, &EINVAL; is returned
151 in that case.</entry>
152 </row>
153 <row>
154 <entry><constant>CEC_MODE_EXCL_FOLLOWER_PASSTHRU</constant></entry>
155 <entry>0x30</entry>
156 <entry>This is an exclusive follower and only this file descriptor will receive
157 CEC messages for processing. In addition it will put the CEC device into
158 passthrough mode, allowing the exclusive follower to handle most core messages
159 instead of relying on the CEC framework for that. If someone else is already the
160 exclusive follower then an attempt to become one will return the &EBUSY; error.
161 You cannot become a follower if <constant>CEC_CAP_TRANSMIT</constant>
162 is not set or if <constant>CEC_MODE_NO_INITIATOR</constant> was specified,
163 &EINVAL; is returned in that case.</entry>
164 </row>
165 <row>
166 <entry><constant>CEC_MODE_MONITOR</constant></entry>
167 <entry>0xe0</entry>
168 <entry>Put the file descriptor into monitor mode. Can only be used in combination
169 with <constant>CEC_MODE_NO_INITIATOR</constant>, otherwise &EINVAL; will be
170 returned. In monitor mode all messages this CEC device transmits and all messages
171 it receives (both broadcast messages and directed messages for one its logical
172 addresses) will be reported. This is very useful for debugging. This is only
173 allowed if the process has the <constant>CAP_NET_ADMIN</constant>
174 capability. If that is not set, then &EPERM; is returned.</entry>
175 </row>
176 <row>
177 <entry><constant>CEC_MODE_MONITOR_ALL</constant></entry>
178 <entry>0xf0</entry>
179 <entry>Put the file descriptor into 'monitor all' mode. Can only be used in combination
180 with <constant>CEC_MODE_NO_INITIATOR</constant>, otherwise &EINVAL; will be
181 returned. In 'monitor all' mode all messages this CEC device transmits and all messages
182 it receives, including directed messages for other CEC devices will be reported. This
183 is very useful for debugging, but not all devices support this. This mode requires that
184 the <constant>CEC_CAP_MONITOR_ALL</constant> capability is set, otherwise &EINVAL; is
185 returned. This is only allowed if the process has the <constant>CAP_NET_ADMIN</constant>
186 capability. If that is not set, then &EPERM; is returned.</entry>
187 </row>
188 </tbody>
189 </tgroup>
190 </table>
191
192 <para>Core message processing details:</para>
193
194 <table pgwide="1" frame="none" id="cec-core-processing">
195 <title>Core Message Processing</title>
196 <tgroup cols="2">
197 &cs-def;
198 <tbody valign="top">
199 <row>
200 <entry><constant>CEC_MSG_GET_CEC_VERSION</constant></entry>
201 <entry>When in passthrough mode this message has to be handled by userspace,
202 otherwise the core will return the CEC version that was set with &CEC-ADAP-S-LOG-ADDRS;.</entry>
203 </row>
204 <row>
205 <entry><constant>CEC_MSG_GIVE_DEVICE_VENDOR_ID</constant></entry>
206 <entry>When in passthrough mode this message has to be handled by userspace,
207 otherwise the core will return the vendor ID that was set with &CEC-ADAP-S-LOG-ADDRS;.</entry>
208 </row>
209 <row>
210 <entry><constant>CEC_MSG_ABORT</constant></entry>
211 <entry>When in passthrough mode this message has to be handled by userspace,
212 otherwise the core will return a feature refused message as per the specification.</entry>
213 </row>
214 <row>
215 <entry><constant>CEC_MSG_GIVE_PHYSICAL_ADDR</constant></entry>
216 <entry>When in passthrough mode this message has to be handled by userspace,
217 otherwise the core will report the current physical address.</entry>
218 </row>
219 <row>
220 <entry><constant>CEC_MSG_GIVE_OSD_NAME</constant></entry>
221 <entry>When in passthrough mode this message has to be handled by userspace,
222 otherwise the core will report the current OSD name as was set with
223 &CEC-ADAP-S-LOG-ADDRS;.</entry>
224 </row>
225 <row>
226 <entry><constant>CEC_MSG_GIVE_FEATURES</constant></entry>
227 <entry>When in passthrough mode this message has to be handled by userspace,
228 otherwise the core will report the current features as was set with
229 &CEC-ADAP-S-LOG-ADDRS; or the message is ignore if the CEC version was
230 older than 2.0.</entry>
231 </row>
232 <row>
233 <entry><constant>CEC_MSG_USER_CONTROL_PRESSED</constant></entry>
234 <entry>If <constant>CEC_CAP_RC</constant> is set, then generate a remote control
235 key press. This message is always passed on to userspace.</entry>
236 </row>
237 <row>
238 <entry><constant>CEC_MSG_USER_CONTROL_RELEASED</constant></entry>
239 <entry>If <constant>CEC_CAP_RC</constant> is set, then generate a remote control
240 key release. This message is always passed on to userspace.</entry>
241 </row>
242 <row>
243 <entry><constant>CEC_MSG_REPORT_PHYSICAL_ADDR</constant></entry>
244 <entry>The CEC framework will make note of the reported physical address
245 and then just pass the message on to userspace.</entry>
246 </row>
247 </tbody>
248 </tgroup>
249 </table>
250 </refsect1>
251
252 <refsect1>
253 &return-value;
254 </refsect1>
255</refentry>
diff --git a/Documentation/DocBook/media/v4l/cec-ioc-receive.xml b/Documentation/DocBook/media/v4l/cec-ioc-receive.xml
new file mode 100644
index 000000000000..fde9f8678e67
--- /dev/null
+++ b/Documentation/DocBook/media/v4l/cec-ioc-receive.xml
@@ -0,0 +1,274 @@
1<refentry id="cec-ioc-receive">
2 <refmeta>
3 <refentrytitle>ioctl CEC_RECEIVE, CEC_TRANSMIT</refentrytitle>
4 &manvol;
5 </refmeta>
6
7 <refnamediv>
8 <refname>CEC_RECEIVE</refname>
9 <refname>CEC_TRANSMIT</refname>
10 <refpurpose>Receive or transmit a CEC message</refpurpose>
11 </refnamediv>
12
13 <refsynopsisdiv>
14 <funcsynopsis>
15 <funcprototype>
16 <funcdef>int <function>ioctl</function></funcdef>
17 <paramdef>int <parameter>fd</parameter></paramdef>
18 <paramdef>int <parameter>request</parameter></paramdef>
19 <paramdef>struct cec_msg *<parameter>argp</parameter></paramdef>
20 </funcprototype>
21 </funcsynopsis>
22 </refsynopsisdiv>
23
24 <refsect1>
25 <title>Arguments</title>
26
27 <variablelist>
28 <varlistentry>
29 <term><parameter>fd</parameter></term>
30 <listitem>
31 <para>File descriptor returned by
32 <link linkend='cec-func-open'><function>open()</function></link>.</para>
33 </listitem>
34 </varlistentry>
35 <varlistentry>
36 <term><parameter>request</parameter></term>
37 <listitem>
38 <para>CEC_RECEIVE, CEC_TRANSMIT</para>
39 </listitem>
40 </varlistentry>
41 <varlistentry>
42 <term><parameter>argp</parameter></term>
43 <listitem>
44 <para></para>
45 </listitem>
46 </varlistentry>
47 </variablelist>
48 </refsect1>
49
50 <refsect1>
51 <title>Description</title>
52
53 <para>
54 Note: this documents the proposed CEC API. This API is not yet finalized and
55 is currently only available as a staging kernel module.
56 </para>
57
58 <para>To receive a CEC message the application has to fill in the
59 <structname>cec_msg</structname> structure and pass it to the
60 <constant>CEC_RECEIVE</constant> ioctl. <constant>CEC_RECEIVE</constant> is
61 only available if <constant>CEC_CAP_RECEIVE</constant> is set. If the
62 file descriptor is in non-blocking mode and there are no received
63 messages pending, then it will return -1 and set errno to the &EAGAIN;.
64 If the file descriptor is in blocking mode and <structfield>timeout</structfield>
65 is non-zero and no message arrived within <structfield>timeout</structfield>
66 milliseconds, then it will return -1 and set errno to the &ETIMEDOUT;.</para>
67
68 <para>To send a CEC message the application has to fill in the
69 <structname>cec_msg</structname> structure and pass it to the
70 <constant>CEC_TRANSMIT</constant> ioctl. <constant>CEC_TRANSMIT</constant> is
71 only available if <constant>CEC_CAP_TRANSMIT</constant> is set.
72 If there is no more room in the transmit queue, then it will return
73 -1 and set errno to the &EBUSY;.</para>
74
75 <table pgwide="1" frame="none" id="cec-msg">
76 <title>struct <structname>cec_msg</structname></title>
77 <tgroup cols="3">
78 &cs-str;
79 <tbody valign="top">
80 <row>
81 <entry>__u64</entry>
82 <entry><structfield>ts</structfield></entry>
83 <entry>Timestamp of when the message was transmitted in ns in the case
84 of <constant>CEC_TRANSMIT</constant> with <structfield>reply</structfield>
85 set to 0, or the timestamp of the received message in all other cases.</entry>
86 </row>
87 <row>
88 <entry>__u32</entry>
89 <entry><structfield>len</structfield></entry>
90 <entry>The length of the message. For <constant>CEC_TRANSMIT</constant> this
91 is filled in by the application. The driver will fill this in for
92 <constant>CEC_RECEIVE</constant> and for <constant>CEC_TRANSMIT</constant>
93 it will be filled in with the length of the reply message if
94 <structfield>reply</structfield> was set.</entry>
95 </row>
96 <row>
97 <entry>__u32</entry>
98 <entry><structfield>timeout</structfield></entry>
99 <entry>The timeout in milliseconds. This is the time the device will wait for a message to
100 be received before timing out. If it is set to 0, then it will wait indefinitely when it
101 is called by <constant>CEC_RECEIVE</constant>. If it is 0 and it is called by
102 <constant>CEC_TRANSMIT</constant>, then it will be replaced by 1000 if the
103 <structfield>reply</structfield> is non-zero or ignored if <structfield>reply</structfield>
104 is 0.</entry>
105 </row>
106 <row>
107 <entry>__u32</entry>
108 <entry><structfield>sequence</structfield></entry>
109 <entry>The sequence number is automatically assigned by the CEC
110 framework for all transmitted messages. It can be later used by the
111 framework to generate an event if a reply for a message was
112 requested and the message was transmitted in a non-blocking mode.
113 </entry>
114 </row>
115 <row>
116 <entry>__u32</entry>
117 <entry><structfield>flags</structfield></entry>
118 <entry>Flags. No flags are defined yet, so set this to 0.</entry>
119 </row>
120 <row>
121 <entry>__u8</entry>
122 <entry><structfield>rx_status</structfield></entry>
123 <entry>The status bits of the received message. See <xref linkend="cec-rx-status" />
124 for the possible status values. It is 0 if this message was transmitted, not
125 received, unless this is the reply to a transmitted message. In that case both
126 <structfield>rx_status</structfield> and <structfield>tx_status</structfield>
127 are set.</entry>
128 </row>
129 <row>
130 <entry>__u8</entry>
131 <entry><structfield>tx_status</structfield></entry>
132 <entry>The status bits of the transmitted message. See <xref linkend="cec-tx-status" />
133 for the possible status values. It is 0 if this messages was received, not
134 transmitted.</entry>
135 </row>
136 <row>
137 <entry>__u8</entry>
138 <entry><structfield>msg</structfield>[16]</entry>
139 <entry>The message payload. For <constant>CEC_TRANSMIT</constant> this
140 is filled in by the application. The driver will fill this in for
141 <constant>CEC_RECEIVE</constant> and for <constant>CEC_TRANSMIT</constant>
142 it will be filled in with the payload of the reply message if
143 <structfield>reply</structfield> was set.</entry>
144 </row>
145 <row>
146 <entry>__u8</entry>
147 <entry><structfield>reply</structfield></entry>
148 <entry>Wait until this message is replied. If <structfield>reply</structfield>
149 is 0 and the <structfield>timeout</structfield> is 0, then don't wait for a reply but
150 return after transmitting the message. If there was an error as indicated by a non-zero
151 <structfield>tx_status</structfield> field, then <structfield>reply</structfield> and
152 <structfield>timeout</structfield> are both set to 0 by the driver. Ignored by
153 <constant>CEC_RECEIVE</constant>. The case where <structfield>reply</structfield> is 0
154 (this is the opcode for the Feature Abort message) and <structfield>timeout</structfield>
155 is non-zero is specifically allowed to send a message and wait up to <structfield>timeout</structfield>
156 milliseconds for a Feature Abort reply. In this case <structfield>rx_status</structfield>
157 will either be set to <constant>CEC_RX_STATUS_TIMEOUT</constant> or
158 <constant>CEC_RX_STATUS_FEATURE_ABORT</constant>.</entry>
159 </row>
160 <row>
161 <entry>__u8</entry>
162 <entry><structfield>tx_arb_lost_cnt</structfield></entry>
163 <entry>A counter of the number of transmit attempts that resulted in the
164 Arbitration Lost error. This is only set if the hardware supports this, otherwise
165 it is always 0. This counter is only valid if the <constant>CEC_TX_STATUS_ARB_LOST</constant>
166 status bit is set.</entry>
167 </row>
168 <row>
169 <entry>__u8</entry>
170 <entry><structfield>tx_nack_cnt</structfield></entry>
171 <entry>A counter of the number of transmit attempts that resulted in the
172 Not Acknowledged error. This is only set if the hardware supports this, otherwise
173 it is always 0. This counter is only valid if the <constant>CEC_TX_STATUS_NACK</constant>
174 status bit is set.</entry>
175 </row>
176 <row>
177 <entry>__u8</entry>
178 <entry><structfield>tx_low_drive_cnt</structfield></entry>
179 <entry>A counter of the number of transmit attempts that resulted in the
180 Arbitration Lost error. This is only set if the hardware supports this, otherwise
181 it is always 0. This counter is only valid if the <constant>CEC_TX_STATUS_LOW_DRIVE</constant>
182 status bit is set.</entry>
183 </row>
184 <row>
185 <entry>__u8</entry>
186 <entry><structfield>tx_error_cnt</structfield></entry>
187 <entry>A counter of the number of transmit errors other than Arbitration Lost
188 or Not Acknowledged. This is only set if the hardware supports this, otherwise
189 it is always 0. This counter is only valid if the <constant>CEC_TX_STATUS_ERROR</constant>
190 status bit is set.</entry>
191 </row>
192 </tbody>
193 </tgroup>
194 </table>
195
196 <table pgwide="1" frame="none" id="cec-tx-status">
197 <title>CEC Transmit Status</title>
198 <tgroup cols="3">
199 &cs-def;
200 <tbody valign="top">
201 <row>
202 <entry><constant>CEC_TX_STATUS_OK</constant></entry>
203 <entry>0x01</entry>
204 <entry>The message was transmitted successfully. This is mutually exclusive with
205 <constant>CEC_TX_STATUS_MAX_RETRIES</constant>. Other bits can still be set if
206 earlier attempts met with failure before the transmit was eventually successful.</entry>
207 </row>
208 <row>
209 <entry><constant>CEC_TX_STATUS_ARB_LOST</constant></entry>
210 <entry>0x02</entry>
211 <entry>CEC line arbitration was lost.</entry>
212 </row>
213 <row>
214 <entry><constant>CEC_TX_STATUS_NACK</constant></entry>
215 <entry>0x04</entry>
216 <entry>Message was not acknowledged.</entry>
217 </row>
218 <row>
219 <entry><constant>CEC_TX_STATUS_LOW_DRIVE</constant></entry>
220 <entry>0x08</entry>
221 <entry>Low drive was detected on the CEC bus. This indicates that a follower
222 detected an error on the bus and requests a retransmission.</entry>
223 </row>
224 <row>
225 <entry><constant>CEC_TX_STATUS_ERROR</constant></entry>
226 <entry>0x10</entry>
227 <entry>Some error occurred. This is used for any errors that do not
228 fit the previous two, either because the hardware could not tell
229 which error occurred, or because the hardware tested for other conditions
230 besides those two.</entry>
231 </row>
232 <row>
233 <entry><constant>CEC_TX_STATUS_MAX_RETRIES</constant></entry>
234 <entry>0x20</entry>
235 <entry>The transmit failed after one or more retries. This status bit is mutually
236 exclusive with <constant>CEC_TX_STATUS_OK</constant>. Other bits can still be set
237 to explain which failures were seen.</entry>
238 </row>
239 </tbody>
240 </tgroup>
241 </table>
242
243 <table pgwide="1" frame="none" id="cec-rx-status">
244 <title>CEC Receive Status</title>
245 <tgroup cols="3">
246 &cs-def;
247 <tbody valign="top">
248 <row>
249 <entry><constant>CEC_RX_STATUS_OK</constant></entry>
250 <entry>0x01</entry>
251 <entry>The message was received successfully.</entry>
252 </row>
253 <row>
254 <entry><constant>CEC_RX_STATUS_TIMEOUT</constant></entry>
255 <entry>0x02</entry>
256 <entry>The reply to an earlier transmitted message timed out.</entry>
257 </row>
258 <row>
259 <entry><constant>CEC_RX_STATUS_FEATURE_ABORT</constant></entry>
260 <entry>0x04</entry>
261 <entry>The message was received successfully but the reply was
262 <constant>CEC_MSG_FEATURE_ABORT</constant>. This status is only
263 set if this message was the reply to an earlier transmitted
264 message.</entry>
265 </row>
266 </tbody>
267 </tgroup>
268 </table>
269 </refsect1>
270
271 <refsect1>
272 &return-value;
273 </refsect1>
274</refentry>
diff --git a/Documentation/DocBook/media/v4l/io.xml b/Documentation/DocBook/media/v4l/io.xml
index e09025db92bd..21a3dde8f95d 100644
--- a/Documentation/DocBook/media/v4l/io.xml
+++ b/Documentation/DocBook/media/v4l/io.xml
@@ -88,7 +88,7 @@ function.<footnote>
88<structfield>capabilities</structfield> field of &v4l2-capability; 88<structfield>capabilities</structfield> field of &v4l2-capability;
89returned by the &VIDIOC-QUERYCAP; ioctl is set. There are two 89returned by the &VIDIOC-QUERYCAP; ioctl is set. There are two
90streaming methods, to determine if the memory mapping flavor is 90streaming methods, to determine if the memory mapping flavor is
91supported applications must call the &VIDIOC-REQBUFS; ioctl.</para> 91supported applications must call the &VIDIOC-REQBUFS; ioctl with the memory type set to <constant>V4L2_MEMORY_MMAP</constant>.</para>
92 92
93 <para>Streaming is an I/O method where only pointers to buffers 93 <para>Streaming is an I/O method where only pointers to buffers
94are exchanged between application and driver, the data itself is not 94are exchanged between application and driver, the data itself is not
@@ -369,7 +369,7 @@ rest should be evident.</para>
369<structfield>capabilities</structfield> field of &v4l2-capability; 369<structfield>capabilities</structfield> field of &v4l2-capability;
370returned by the &VIDIOC-QUERYCAP; ioctl is set. If the particular user 370returned by the &VIDIOC-QUERYCAP; ioctl is set. If the particular user
371pointer method (not only memory mapping) is supported must be 371pointer method (not only memory mapping) is supported must be
372determined by calling the &VIDIOC-REQBUFS; ioctl.</para> 372determined by calling the &VIDIOC-REQBUFS; ioctl with the memory type set to <constant>V4L2_MEMORY_USERPTR</constant>.</para>
373 373
374 <para>This I/O method combines advantages of the read/write and 374 <para>This I/O method combines advantages of the read/write and
375memory mapping methods. Buffers (planes) are allocated by the application 375memory mapping methods. Buffers (planes) are allocated by the application
diff --git a/Documentation/DocBook/media/v4l/lirc_device_interface.xml b/Documentation/DocBook/media/v4l/lirc_device_interface.xml
index 34cada2ca710..71f9dbb81ec7 100644
--- a/Documentation/DocBook/media/v4l/lirc_device_interface.xml
+++ b/Documentation/DocBook/media/v4l/lirc_device_interface.xml
@@ -157,7 +157,7 @@ on working with the default settings initially.</para>
157 <varlistentry> 157 <varlistentry>
158 <term>LIRC_SET_{SEND,REC}_CARRIER</term> 158 <term>LIRC_SET_{SEND,REC}_CARRIER</term>
159 <listitem> 159 <listitem>
160 <para>Set send/receive carrier (in Hz).</para> 160 <para>Set send/receive carrier (in Hz). Return 0 on success.</para>
161 </listitem> 161 </listitem>
162 </varlistentry> 162 </varlistentry>
163 <varlistentry> 163 <varlistentry>
diff --git a/Documentation/DocBook/media/v4l/media-types.xml b/Documentation/DocBook/media/v4l/media-types.xml
index 5e3f20fdcf17..95aa1f9c836a 100644
--- a/Documentation/DocBook/media/v4l/media-types.xml
+++ b/Documentation/DocBook/media/v4l/media-types.xml
@@ -121,6 +121,70 @@
121 <entry><constant>MEDIA_ENT_F_AUDIO_MIXER</constant></entry> 121 <entry><constant>MEDIA_ENT_F_AUDIO_MIXER</constant></entry>
122 <entry>Audio Mixer Function Entity.</entry> 122 <entry>Audio Mixer Function Entity.</entry>
123 </row> 123 </row>
124 <row>
125 <entry><constant>MEDIA_ENT_F_PROC_VIDEO_COMPOSER</constant></entry>
126 <entry>Video composer (blender). An entity capable of video
127 composing must have at least two sink pads and one source
128 pad, and composes input video frames onto output video
129 frames. Composition can be performed using alpha blending,
130 color keying, raster operations (ROP), stitching or any other
131 means.
132 </entry>
133 </row>
134 <row>
135 <entry><constant>MEDIA_ENT_F_PROC_VIDEO_PIXEL_FORMATTER</constant></entry>
136 <entry>Video pixel formatter. An entity capable of pixel formatting
137 must have at least one sink pad and one source pad. Read
138 pixel formatters read pixels from memory and perform a subset
139 of unpacking, cropping, color keying, alpha multiplication
140 and pixel encoding conversion. Write pixel formatters perform
141 a subset of dithering, pixel encoding conversion and packing
142 and write pixels to memory.
143 </entry>
144 </row>
145 <row>
146 <entry><constant>MEDIA_ENT_F_PROC_VIDEO_PIXEL_ENC_CONV</constant></entry>
147 <entry>Video pixel encoding converter. An entity capable of pixel
148 enconding conversion must have at least one sink pad and one
149 source pad, and convert the encoding of pixels received on
150 its sink pad(s) to a different encoding output on its source
151 pad(s). Pixel encoding conversion includes but isn't limited
152 to RGB to/from HSV, RGB to/from YUV and CFA (Bayer) to RGB
153 conversions.
154 </entry>
155 </row>
156 <row>
157 <entry><constant>MEDIA_ENT_F_PROC_VIDEO_LUT</constant></entry>
158 <entry>Video look-up table. An entity capable of video lookup table
159 processing must have one sink pad and one source pad. It uses
160 the values of the pixels received on its sink pad to look up
161 entries in internal tables and output them on its source pad.
162 The lookup processing can be performed on all components
163 separately or combine them for multi-dimensional table
164 lookups.
165 </entry>
166 </row>
167 <row>
168 <entry><constant>MEDIA_ENT_F_PROC_VIDEO_SCALER</constant></entry>
169 <entry>Video scaler. An entity capable of video scaling must have
170 at least one sink pad and one source pad, and scale the
171 video frame(s) received on its sink pad(s) to a different
172 resolution output on its source pad(s). The range of
173 supported scaling ratios is entity-specific and can differ
174 between the horizontal and vertical directions (in particular
175 scaling can be supported in one direction only). Binning and
176 skipping are considered as scaling.
177 </entry>
178 </row>
179 <row>
180 <entry><constant>MEDIA_ENT_F_PROC_VIDEO_STATISTICS</constant></entry>
181 <entry>Video statistics computation (histogram, 3A, ...). An entity
182 capable of statistics computation must have one sink pad and
183 one source pad. It computes statistics over the frames
184 received on its sink pad and outputs the statistics data on
185 its source pad.
186 </entry>
187 </row>
124 </tbody> 188 </tbody>
125 </tgroup> 189 </tgroup>
126 </table> 190 </table>
diff --git a/Documentation/DocBook/media/v4l/pixfmt-z16.xml b/Documentation/DocBook/media/v4l/pixfmt-z16.xml
index 3d87e4bf87b8..1d9cb1684bd3 100644
--- a/Documentation/DocBook/media/v4l/pixfmt-z16.xml
+++ b/Documentation/DocBook/media/v4l/pixfmt-z16.xml
@@ -5,7 +5,7 @@
5 </refmeta> 5 </refmeta>
6 <refnamediv> 6 <refnamediv>
7 <refname><constant>V4L2_PIX_FMT_Z16</constant></refname> 7 <refname><constant>V4L2_PIX_FMT_Z16</constant></refname>
8 <refpurpose>Interleaved grey-scale image, e.g. from a stereo-pair</refpurpose> 8 <refpurpose>16-bit depth data with distance values at each pixel</refpurpose>
9 </refnamediv> 9 </refnamediv>
10 <refsect1> 10 <refsect1>
11 <title>Description</title> 11 <title>Description</title>
diff --git a/Documentation/DocBook/media/v4l/vidioc-reqbufs.xml b/Documentation/DocBook/media/v4l/vidioc-reqbufs.xml
index 0f193fda0470..6f529e100ea4 100644
--- a/Documentation/DocBook/media/v4l/vidioc-reqbufs.xml
+++ b/Documentation/DocBook/media/v4l/vidioc-reqbufs.xml
@@ -6,7 +6,7 @@
6 6
7 <refnamediv> 7 <refnamediv>
8 <refname>VIDIOC_REQBUFS</refname> 8 <refname>VIDIOC_REQBUFS</refname>
9 <refpurpose>Initiate Memory Mapping or User Pointer I/O</refpurpose> 9 <refpurpose>Initiate Memory Mapping, User Pointer or DMA Buffer I/O</refpurpose>
10 </refnamediv> 10 </refnamediv>
11 11
12 <refsynopsisdiv> 12 <refsynopsisdiv>
diff --git a/Documentation/DocBook/media_api.tmpl b/Documentation/DocBook/media_api.tmpl
index 7b77e0f7b87d..a2765d8ad05c 100644
--- a/Documentation/DocBook/media_api.tmpl
+++ b/Documentation/DocBook/media_api.tmpl
@@ -75,7 +75,7 @@
75 </mediaobject> 75 </mediaobject>
76 </figure> 76 </figure>
77 <para>The media infrastructure API was designed to control such 77 <para>The media infrastructure API was designed to control such
78 devices. It is divided into four parts.</para> 78 devices. It is divided into five parts.</para>
79 <para>The first part covers radio, video capture and output, 79 <para>The first part covers radio, video capture and output,
80 cameras, analog TV devices and codecs.</para> 80 cameras, analog TV devices and codecs.</para>
81 <para>The second part covers the 81 <para>The second part covers the
@@ -87,6 +87,7 @@
87 <xref linkend="fe-delivery-system-t" />.</para> 87 <xref linkend="fe-delivery-system-t" />.</para>
88 <para>The third part covers the Remote Controller API.</para> 88 <para>The third part covers the Remote Controller API.</para>
89 <para>The fourth part covers the Media Controller API.</para> 89 <para>The fourth part covers the Media Controller API.</para>
90 <para>The fifth part covers the CEC (Consumer Electronics Control) API.</para>
90 <para>It should also be noted that a media device may also have audio 91 <para>It should also be noted that a media device may also have audio
91 components, like mixers, PCM capture, PCM playback, etc, which 92 components, like mixers, PCM capture, PCM playback, etc, which
92 are controlled via ALSA API.</para> 93 are controlled via ALSA API.</para>
@@ -107,6 +108,9 @@
107<part id="media_common"> 108<part id="media_common">
108&sub-media-controller; 109&sub-media-controller;
109</part> 110</part>
111<part id="cec">
112&sub-cec-api;
113</part>
110 114
111<chapter id="gen_errors"> 115<chapter id="gen_errors">
112&sub-gen-errors; 116&sub-gen-errors;
diff --git a/Documentation/cec.txt b/Documentation/cec.txt
new file mode 100644
index 000000000000..75155fe37153
--- /dev/null
+++ b/Documentation/cec.txt
@@ -0,0 +1,267 @@
1CEC Kernel Support
2==================
3
4The CEC framework provides a unified kernel interface for use with HDMI CEC
5hardware. It is designed to handle a multiple types of hardware (receivers,
6transmitters, USB dongles). The framework also gives the option to decide
7what to do in the kernel driver and what should be handled by userspace
8applications. In addition it integrates the remote control passthrough
9feature into the kernel's remote control framework.
10
11
12The CEC Protocol
13----------------
14
15The CEC protocol enables consumer electronic devices to communicate with each
16other through the HDMI connection. The protocol uses logical addresses in the
17communication. The logical address is strictly connected with the functionality
18provided by the device. The TV acting as the communication hub is always
19assigned address 0. The physical address is determined by the physical
20connection between devices.
21
22The CEC framework described here is up to date with the CEC 2.0 specification.
23It is documented in the HDMI 1.4 specification with the new 2.0 bits documented
24in the HDMI 2.0 specification. But for most of the features the freely available
25HDMI 1.3a specification is sufficient:
26
27http://www.microprocessor.org/HDMISpecification13a.pdf
28
29
30The Kernel Interface
31====================
32
33CEC Adapter
34-----------
35
36The struct cec_adapter represents the CEC adapter hardware. It is created by
37calling cec_allocate_adapter() and deleted by calling cec_delete_adapter():
38
39struct cec_adapter *cec_allocate_adapter(const struct cec_adap_ops *ops,
40 void *priv, const char *name, u32 caps, u8 available_las,
41 struct device *parent);
42void cec_delete_adapter(struct cec_adapter *adap);
43
44To create an adapter you need to pass the following information:
45
46ops: adapter operations which are called by the CEC framework and that you
47have to implement.
48
49priv: will be stored in adap->priv and can be used by the adapter ops.
50
51name: the name of the CEC adapter. Note: this name will be copied.
52
53caps: capabilities of the CEC adapter. These capabilities determine the
54 capabilities of the hardware and which parts are to be handled
55 by userspace and which parts are handled by kernelspace. The
56 capabilities are returned by CEC_ADAP_G_CAPS.
57
58available_las: the number of simultaneous logical addresses that this
59 adapter can handle. Must be 1 <= available_las <= CEC_MAX_LOG_ADDRS.
60
61parent: the parent device.
62
63
64To register the /dev/cecX device node and the remote control device (if
65CEC_CAP_RC is set) you call:
66
67int cec_register_adapter(struct cec_adapter *adap);
68
69To unregister the devices call:
70
71void cec_unregister_adapter(struct cec_adapter *adap);
72
73Note: if cec_register_adapter() fails, then call cec_delete_adapter() to
74clean up. But if cec_register_adapter() succeeded, then only call
75cec_unregister_adapter() to clean up, never cec_delete_adapter(). The
76unregister function will delete the adapter automatically once the last user
77of that /dev/cecX device has closed its file handle.
78
79
80Implementing the Low-Level CEC Adapter
81--------------------------------------
82
83The following low-level adapter operations have to be implemented in
84your driver:
85
86struct cec_adap_ops {
87 /* Low-level callbacks */
88 int (*adap_enable)(struct cec_adapter *adap, bool enable);
89 int (*adap_monitor_all_enable)(struct cec_adapter *adap, bool enable);
90 int (*adap_log_addr)(struct cec_adapter *adap, u8 logical_addr);
91 int (*adap_transmit)(struct cec_adapter *adap, u8 attempts,
92 u32 signal_free_time, struct cec_msg *msg);
93 void (*adap_log_status)(struct cec_adapter *adap);
94
95 /* High-level callbacks */
96 ...
97};
98
99The three low-level ops deal with various aspects of controlling the CEC adapter
100hardware:
101
102
103To enable/disable the hardware:
104
105 int (*adap_enable)(struct cec_adapter *adap, bool enable);
106
107This callback enables or disables the CEC hardware. Enabling the CEC hardware
108means powering it up in a state where no logical addresses are claimed. This
109op assumes that the physical address (adap->phys_addr) is valid when enable is
110true and will not change while the CEC adapter remains enabled. The initial
111state of the CEC adapter after calling cec_allocate_adapter() is disabled.
112
113Note that adap_enable must return 0 if enable is false.
114
115
116To enable/disable the 'monitor all' mode:
117
118 int (*adap_monitor_all_enable)(struct cec_adapter *adap, bool enable);
119
120If enabled, then the adapter should be put in a mode to also monitor messages
121that not for us. Not all hardware supports this and this function is only
122called if the CEC_CAP_MONITOR_ALL capability is set. This callback is optional
123(some hardware may always be in 'monitor all' mode).
124
125Note that adap_monitor_all_enable must return 0 if enable is false.
126
127
128To program a new logical address:
129
130 int (*adap_log_addr)(struct cec_adapter *adap, u8 logical_addr);
131
132If logical_addr == CEC_LOG_ADDR_INVALID then all programmed logical addresses
133are to be erased. Otherwise the given logical address should be programmed.
134If the maximum number of available logical addresses is exceeded, then it
135should return -ENXIO. Once a logical address is programmed the CEC hardware
136can receive directed messages to that address.
137
138Note that adap_log_addr must return 0 if logical_addr is CEC_LOG_ADDR_INVALID.
139
140
141To transmit a new message:
142
143 int (*adap_transmit)(struct cec_adapter *adap, u8 attempts,
144 u32 signal_free_time, struct cec_msg *msg);
145
146This transmits a new message. The attempts argument is the suggested number of
147attempts for the transmit.
148
149The signal_free_time is the number of data bit periods that the adapter should
150wait when the line is free before attempting to send a message. This value
151depends on whether this transmit is a retry, a message from a new initiator or
152a new message for the same initiator. Most hardware will handle this
153automatically, but in some cases this information is needed.
154
155The CEC_FREE_TIME_TO_USEC macro can be used to convert signal_free_time to
156microseconds (one data bit period is 2.4 ms).
157
158
159To log the current CEC hardware status:
160
161 void (*adap_status)(struct cec_adapter *adap, struct seq_file *file);
162
163This optional callback can be used to show the status of the CEC hardware.
164The status is available through debugfs: cat /sys/kernel/debug/cec/cecX/status
165
166
167Your adapter driver will also have to react to events (typically interrupt
168driven) by calling into the framework in the following situations:
169
170When a transmit finished (successfully or otherwise):
171
172void cec_transmit_done(struct cec_adapter *adap, u8 status, u8 arb_lost_cnt,
173 u8 nack_cnt, u8 low_drive_cnt, u8 error_cnt);
174
175The status can be one of:
176
177CEC_TX_STATUS_OK: the transmit was successful.
178CEC_TX_STATUS_ARB_LOST: arbitration was lost: another CEC initiator
179took control of the CEC line and you lost the arbitration.
180CEC_TX_STATUS_NACK: the message was nacked (for a directed message) or
181acked (for a broadcast message). A retransmission is needed.
182CEC_TX_STATUS_LOW_DRIVE: low drive was detected on the CEC bus. This
183indicates that a follower detected an error on the bus and requested a
184retransmission.
185CEC_TX_STATUS_ERROR: some unspecified error occurred: this can be one of
186the previous two if the hardware cannot differentiate or something else
187entirely.
188CEC_TX_STATUS_MAX_RETRIES: could not transmit the message after
189trying multiple times. Should only be set by the driver if it has hardware
190support for retrying messages. If set, then the framework assumes that it
191doesn't have to make another attempt to transmit the message since the
192hardware did that already.
193
194The *_cnt arguments are the number of error conditions that were seen.
195This may be 0 if no information is available. Drivers that do not support
196hardware retry can just set the counter corresponding to the transmit error
197to 1, if the hardware does support retry then either set these counters to
1980 if the hardware provides no feedback of which errors occurred and how many
199times, or fill in the correct values as reported by the hardware.
200
201When a CEC message was received:
202
203void cec_received_msg(struct cec_adapter *adap, struct cec_msg *msg);
204
205Speaks for itself.
206
207Implementing the High-Level CEC Adapter
208---------------------------------------
209
210The low-level operations drive the hardware, the high-level operations are
211CEC protocol driven. The following high-level callbacks are available:
212
213struct cec_adap_ops {
214 /* Low-level callbacks */
215 ...
216
217 /* High-level CEC message callback */
218 int (*received)(struct cec_adapter *adap, struct cec_msg *msg);
219};
220
221The received() callback allows the driver to optionally handle a newly
222received CEC message
223
224 int (*received)(struct cec_adapter *adap, struct cec_msg *msg);
225
226If the driver wants to process a CEC message, then it can implement this
227callback. If it doesn't want to handle this message, then it should return
228-ENOMSG, otherwise the CEC framework assumes it processed this message and
229it will not no anything with it.
230
231
232CEC framework functions
233-----------------------
234
235CEC Adapter drivers can call the following CEC framework functions:
236
237int cec_transmit_msg(struct cec_adapter *adap, struct cec_msg *msg,
238 bool block);
239
240Transmit a CEC message. If block is true, then wait until the message has been
241transmitted, otherwise just queue it and return.
242
243void cec_s_phys_addr(struct cec_adapter *adap, u16 phys_addr, bool block);
244
245Change the physical address. This function will set adap->phys_addr and
246send an event if it has changed. If cec_s_log_addrs() has been called and
247the physical address has become valid, then the CEC framework will start
248claiming the logical addresses. If block is true, then this function won't
249return until this process has finished.
250
251When the physical address is set to a valid value the CEC adapter will
252be enabled (see the adap_enable op). When it is set to CEC_PHYS_ADDR_INVALID,
253then the CEC adapter will be disabled. If you change a valid physical address
254to another valid physical address, then this function will first set the
255address to CEC_PHYS_ADDR_INVALID before enabling the new physical address.
256
257int cec_s_log_addrs(struct cec_adapter *adap,
258 struct cec_log_addrs *log_addrs, bool block);
259
260Claim the CEC logical addresses. Should never be called if CEC_CAP_LOG_ADDRS
261is set. If block is true, then wait until the logical addresses have been
262claimed, otherwise just queue it and return. To unconfigure all logical
263addresses call this function with log_addrs set to NULL or with
264log_addrs->num_log_addrs set to 0. The block argument is ignored when
265unconfiguring. This function will just return if the physical address is
266invalid. Once the physical address becomes valid, then the framework will
267attempt to claim these logical addresses.
diff --git a/Documentation/devicetree/bindings/media/mediatek-vcodec.txt b/Documentation/devicetree/bindings/media/mediatek-vcodec.txt
new file mode 100644
index 000000000000..59a47a5b924b
--- /dev/null
+++ b/Documentation/devicetree/bindings/media/mediatek-vcodec.txt
@@ -0,0 +1,59 @@
1Mediatek Video Codec
2
3Mediatek Video Codec is the video codec hw present in Mediatek SoCs which
4supports high resolution encoding functionalities.
5
6Required properties:
7- compatible : "mediatek,mt8173-vcodec-enc" for encoder
8- reg : Physical base address of the video codec registers and length of
9 memory mapped region.
10- interrupts : interrupt number to the cpu.
11- mediatek,larb : must contain the local arbiters in the current Socs.
12- clocks : list of clock specifiers, corresponding to entries in
13 the clock-names property.
14- clock-names: encoder must contain "venc_sel_src", "venc_sel",
15- "venc_lt_sel_src", "venc_lt_sel".
16- iommus : should point to the respective IOMMU block with master port as
17 argument, see Documentation/devicetree/bindings/iommu/mediatek,iommu.txt
18 for details.
19- mediatek,vpu : the node of video processor unit
20
21Example:
22vcodec_enc: vcodec@0x18002000 {
23 compatible = "mediatek,mt8173-vcodec-enc";
24 reg = <0 0x18002000 0 0x1000>, /*VENC_SYS*/
25 <0 0x19002000 0 0x1000>; /*VENC_LT_SYS*/
26 interrupts = <GIC_SPI 198 IRQ_TYPE_LEVEL_LOW>,
27 <GIC_SPI 202 IRQ_TYPE_LEVEL_LOW>;
28 mediatek,larb = <&larb3>,
29 <&larb5>;
30 iommus = <&iommu M4U_PORT_VENC_RCPU>,
31 <&iommu M4U_PORT_VENC_REC>,
32 <&iommu M4U_PORT_VENC_BSDMA>,
33 <&iommu M4U_PORT_VENC_SV_COMV>,
34 <&iommu M4U_PORT_VENC_RD_COMV>,
35 <&iommu M4U_PORT_VENC_CUR_LUMA>,
36 <&iommu M4U_PORT_VENC_CUR_CHROMA>,
37 <&iommu M4U_PORT_VENC_REF_LUMA>,
38 <&iommu M4U_PORT_VENC_REF_CHROMA>,
39 <&iommu M4U_PORT_VENC_NBM_RDMA>,
40 <&iommu M4U_PORT_VENC_NBM_WDMA>,
41 <&iommu M4U_PORT_VENC_RCPU_SET2>,
42 <&iommu M4U_PORT_VENC_REC_FRM_SET2>,
43 <&iommu M4U_PORT_VENC_BSDMA_SET2>,
44 <&iommu M4U_PORT_VENC_SV_COMA_SET2>,
45 <&iommu M4U_PORT_VENC_RD_COMA_SET2>,
46 <&iommu M4U_PORT_VENC_CUR_LUMA_SET2>,
47 <&iommu M4U_PORT_VENC_CUR_CHROMA_SET2>,
48 <&iommu M4U_PORT_VENC_REF_LUMA_SET2>,
49 <&iommu M4U_PORT_VENC_REC_CHROMA_SET2>;
50 mediatek,vpu = <&vpu>;
51 clocks = <&topckgen CLK_TOP_VENCPLL_D2>,
52 <&topckgen CLK_TOP_VENC_SEL>,
53 <&topckgen CLK_TOP_UNIVPLL1_D2>,
54 <&topckgen CLK_TOP_VENC_LT_SEL>;
55 clock-names = "venc_sel_src",
56 "venc_sel",
57 "venc_lt_sel_src",
58 "venc_lt_sel";
59 };
diff --git a/Documentation/devicetree/bindings/media/mediatek-vpu.txt b/Documentation/devicetree/bindings/media/mediatek-vpu.txt
new file mode 100644
index 000000000000..2a5bac37f9a2
--- /dev/null
+++ b/Documentation/devicetree/bindings/media/mediatek-vpu.txt
@@ -0,0 +1,31 @@
1* Mediatek Video Processor Unit
2
3Video Processor Unit is a HW video controller. It controls HW Codec including
4H.264/VP8/VP9 Decode, H.264/VP8 Encode and Image Processor (scale/rotate/color convert).
5
6Required properties:
7 - compatible: "mediatek,mt8173-vpu"
8 - reg: Must contain an entry for each entry in reg-names.
9 - reg-names: Must include the following entries:
10 "tcm": tcm base
11 "cfg_reg": Main configuration registers base
12 - interrupts: interrupt number to the cpu.
13 - clocks : clock name from clock manager
14 - clock-names: must be main. It is the main clock of VPU
15
16Optional properties:
17 - memory-region: phandle to a node describing memory (see
18 Documentation/devicetree/bindings/reserved-memory/reserved-memory.txt)
19 to be used for VPU extended memory; if not present, VPU may be located
20 anywhere in the memory
21
22Example:
23 vpu: vpu@10020000 {
24 compatible = "mediatek,mt8173-vpu";
25 reg = <0 0x10020000 0 0x30000>,
26 <0 0x10050000 0 0x100>;
27 reg-names = "tcm", "cfg_reg";
28 interrupts = <GIC_SPI 166 IRQ_TYPE_LEVEL_HIGH>;
29 clocks = <&topckgen TOP_SCP_SEL>;
30 clock-names = "main";
31 };
diff --git a/Documentation/devicetree/bindings/media/renesas,fcp.txt b/Documentation/devicetree/bindings/media/renesas,fcp.txt
new file mode 100644
index 000000000000..6a12960609d8
--- /dev/null
+++ b/Documentation/devicetree/bindings/media/renesas,fcp.txt
@@ -0,0 +1,32 @@
1Renesas R-Car Frame Compression Processor (FCP)
2-----------------------------------------------
3
4The FCP is a companion module of video processing modules in the Renesas R-Car
5Gen3 SoCs. It provides data compression and decompression, data caching, and
6conversion of AXI transactions in order to reduce the memory bandwidth.
7
8There are three types of FCP: FCP for Codec (FCPC), FCP for VSP (FCPV) and FCP
9for FDP (FCPF). Their configuration and behaviour depend on the module they
10are paired with. These DT bindings currently support the FCPV only.
11
12 - compatible: Must be one or more of the following
13
14 - "renesas,r8a7795-fcpv" for R8A7795 (R-Car H3) compatible 'FCP for VSP'
15 - "renesas,fcpv" for generic compatible 'FCP for VSP'
16
17 When compatible with the generic version, nodes must list the
18 SoC-specific version corresponding to the platform first, followed by the
19 family-specific and/or generic versions.
20
21 - reg: the register base and size for the device registers
22 - clocks: Reference to the functional clock
23
24
25Device node example
26-------------------
27
28 fcpvd1: fcp@fea2f000 {
29 compatible = "renesas,r8a7795-fcpv", "renesas,fcpv";
30 reg = <0 0xfea2f000 0 0x200>;
31 clocks = <&cpg CPG_MOD 602>;
32 };
diff --git a/Documentation/devicetree/bindings/media/renesas,vsp1.txt b/Documentation/devicetree/bindings/media/renesas,vsp1.txt
index 627405abd144..9b695bcbf219 100644
--- a/Documentation/devicetree/bindings/media/renesas,vsp1.txt
+++ b/Documentation/devicetree/bindings/media/renesas,vsp1.txt
@@ -14,6 +14,11 @@ Required properties:
14 - interrupts: VSP interrupt specifier. 14 - interrupts: VSP interrupt specifier.
15 - clocks: A phandle + clock-specifier pair for the VSP functional clock. 15 - clocks: A phandle + clock-specifier pair for the VSP functional clock.
16 16
17Optional properties:
18
19 - renesas,fcp: A phandle referencing the FCP that handles memory accesses
20 for the VSP. Not needed on Gen2, mandatory on Gen3.
21
17 22
18Example: R8A7790 (R-Car H2) VSP1-S node 23Example: R8A7790 (R-Car H2) VSP1-S node
19 24
diff --git a/Documentation/devicetree/bindings/media/s5p-cec.txt b/Documentation/devicetree/bindings/media/s5p-cec.txt
new file mode 100644
index 000000000000..925ab4d72eaa
--- /dev/null
+++ b/Documentation/devicetree/bindings/media/s5p-cec.txt
@@ -0,0 +1,31 @@
1* Samsung HDMI CEC driver
2
3The HDMI CEC module is present is Samsung SoCs and its purpose is to
4handle communication between HDMI connected devices over the CEC bus.
5
6Required properties:
7 - compatible : value should be following
8 "samsung,s5p-cec"
9
10 - reg : Physical base address of the IP registers and length of memory
11 mapped region.
12
13 - interrupts : HDMI CEC interrupt number to the CPU.
14 - clocks : from common clock binding: handle to HDMI CEC clock.
15 - clock-names : from common clock binding: must contain "hdmicec",
16 corresponding to entry in the clocks property.
17 - samsung,syscon-phandle - phandle to the PMU system controller
18
19Example:
20
21hdmicec: cec@100B0000 {
22 compatible = "samsung,s5p-cec";
23 reg = <0x100B0000 0x200>;
24 interrupts = <0 114 0>;
25 clocks = <&clock CLK_HDMI_CEC>;
26 clock-names = "hdmicec";
27 samsung,syscon-phandle = <&pmu_system_controller>;
28 pinctrl-names = "default";
29 pinctrl-0 = <&hdmi_cec>;
30 status = "okay";
31};
diff --git a/Documentation/devicetree/bindings/media/s5p-mfc.txt b/Documentation/devicetree/bindings/media/s5p-mfc.txt
index 2d5787eac91a..92c94f5ecbf1 100644
--- a/Documentation/devicetree/bindings/media/s5p-mfc.txt
+++ b/Documentation/devicetree/bindings/media/s5p-mfc.txt
@@ -21,15 +21,18 @@ Required properties:
21 - clock-names : from common clock binding: must contain "mfc", 21 - clock-names : from common clock binding: must contain "mfc",
22 corresponding to entry in the clocks property. 22 corresponding to entry in the clocks property.
23 23
24 - samsung,mfc-r : Base address of the first memory bank used by MFC
25 for DMA contiguous memory allocation and its size.
26
27 - samsung,mfc-l : Base address of the second memory bank used by MFC
28 for DMA contiguous memory allocation and its size.
29
30Optional properties: 24Optional properties:
31 - power-domains : power-domain property defined with a phandle 25 - power-domains : power-domain property defined with a phandle
32 to respective power domain. 26 to respective power domain.
27 - memory-region : from reserved memory binding: phandles to two reserved
28 memory regions, first is for "left" mfc memory bus interfaces,
29 second if for the "right" mfc memory bus, used when no SYSMMU
30 support is available
31
32Obsolete properties:
33 - samsung,mfc-r, samsung,mfc-l : support removed, please use memory-region
34 property instead
35
33 36
34Example: 37Example:
35SoC specific DT entry: 38SoC specific DT entry:
@@ -43,9 +46,29 @@ mfc: codec@13400000 {
43 clock-names = "mfc"; 46 clock-names = "mfc";
44}; 47};
45 48
49Reserved memory specific DT entry for given board (see reserved memory binding
50for more information):
51
52reserved-memory {
53 #address-cells = <1>;
54 #size-cells = <1>;
55 ranges;
56
57 mfc_left: region@51000000 {
58 compatible = "shared-dma-pool";
59 no-map;
60 reg = <0x51000000 0x800000>;
61 };
62
63 mfc_right: region@43000000 {
64 compatible = "shared-dma-pool";
65 no-map;
66 reg = <0x43000000 0x800000>;
67 };
68};
69
46Board specific DT entry: 70Board specific DT entry:
47 71
48codec@13400000 { 72codec@13400000 {
49 samsung,mfc-r = <0x43000000 0x800000>; 73 memory-region = <&mfc_left>, <&mfc_right>;
50 samsung,mfc-l = <0x51000000 0x800000>;
51}; 74};
diff --git a/Documentation/video4linux/CARDLIST.cx23885 b/Documentation/video4linux/CARDLIST.cx23885
index 85a8fdcfcdaa..c9b4959fd04e 100644
--- a/Documentation/video4linux/CARDLIST.cx23885
+++ b/Documentation/video4linux/CARDLIST.cx23885
@@ -54,3 +54,4 @@
54 53 -> Hauppauge WinTV Starburst [0070:c12a] 54 53 -> Hauppauge WinTV Starburst [0070:c12a]
55 54 -> ViewCast 260e [1576:0260] 55 54 -> ViewCast 260e [1576:0260]
56 55 -> ViewCast 460e [1576:0460] 56 55 -> ViewCast 460e [1576:0460]
57 56 -> Hauppauge WinTV-quadHD (DVB) [0070:6a28,0070:6b28]
diff --git a/Documentation/video4linux/v4l2-controls.txt b/Documentation/video4linux/v4l2-controls.txt
index 5e759cab4538..f930b80e9111 100644
--- a/Documentation/video4linux/v4l2-controls.txt
+++ b/Documentation/video4linux/v4l2-controls.txt
@@ -96,21 +96,6 @@ Basic usage for V4L2 and sub-device drivers
96 96
97 Where foo->sd is of type struct v4l2_subdev. 97 Where foo->sd is of type struct v4l2_subdev.
98 98
99 And set all core control ops in your struct v4l2_subdev_core_ops to these
100 helpers:
101
102 .queryctrl = v4l2_subdev_queryctrl,
103 .querymenu = v4l2_subdev_querymenu,
104 .g_ctrl = v4l2_subdev_g_ctrl,
105 .s_ctrl = v4l2_subdev_s_ctrl,
106 .g_ext_ctrls = v4l2_subdev_g_ext_ctrls,
107 .try_ext_ctrls = v4l2_subdev_try_ext_ctrls,
108 .s_ext_ctrls = v4l2_subdev_s_ext_ctrls,
109
110 Note: this is a temporary solution only. Once all V4L2 drivers that depend
111 on subdev drivers are converted to the control framework these helpers will
112 no longer be needed.
113
1141.4) Clean up the handler at the end: 991.4) Clean up the handler at the end:
115 100
116 v4l2_ctrl_handler_free(&foo->ctrl_handler); 101 v4l2_ctrl_handler_free(&foo->ctrl_handler);
diff --git a/Documentation/video4linux/vivid.txt b/Documentation/video4linux/vivid.txt
index 8da5d2a576bc..1b26519c6ddc 100644
--- a/Documentation/video4linux/vivid.txt
+++ b/Documentation/video4linux/vivid.txt
@@ -74,7 +74,8 @@ Section 11: Cropping, Composing, Scaling
74Section 12: Formats 74Section 12: Formats
75Section 13: Capture Overlay 75Section 13: Capture Overlay
76Section 14: Output Overlay 76Section 14: Output Overlay
77Section 15: Some Future Improvements 77Section 15: CEC (Consumer Electronics Control)
78Section 16: Some Future Improvements
78 79
79 80
80Section 1: Configuring the driver 81Section 1: Configuring the driver
@@ -364,7 +365,11 @@ For HDMI inputs it is possible to set the EDID. By default a simple EDID
364is provided. You can only set the EDID for HDMI inputs. Internally, however, 365is provided. You can only set the EDID for HDMI inputs. Internally, however,
365the EDID is shared between all HDMI inputs. 366the EDID is shared between all HDMI inputs.
366 367
367No interpretation is done of the EDID data. 368No interpretation is done of the EDID data with the exception of the
369physical address. See the CEC section for more details.
370
371There is a maximum of 15 HDMI inputs (if there are more, then they will be
372reduced to 15) since that's the limitation of the EDID physical address.
368 373
369 374
370Section 3: Video Output 375Section 3: Video Output
@@ -409,6 +414,9 @@ standard, and for all others a 1:1 pixel aspect ratio is returned.
409 414
410An HDMI output has a valid EDID which can be obtained through VIDIOC_G_EDID. 415An HDMI output has a valid EDID which can be obtained through VIDIOC_G_EDID.
411 416
417There is a maximum of 15 HDMI outputs (if there are more, then they will be
418reduced to 15) since that's the limitation of the EDID physical address. See
419also the CEC section for more details.
412 420
413Section 4: VBI Capture 421Section 4: VBI Capture
414---------------------- 422----------------------
@@ -1108,7 +1116,26 @@ capabilities will slow down the video loop considerably as a lot of checks have
1108to be done per pixel. 1116to be done per pixel.
1109 1117
1110 1118
1111Section 15: Some Future Improvements 1119Section 15: CEC (Consumer Electronics Control)
1120----------------------------------------------
1121
1122If there are HDMI inputs then a CEC adapter will be created that has
1123the same number of input ports. This is the equivalent of e.g. a TV that
1124has that number of inputs. Each HDMI output will also create a
1125CEC adapter that is hooked up to the corresponding input port, or (if there
1126are more outputs than inputs) is not hooked up at all. In other words,
1127this is the equivalent of hooking up each output device to an input port of
1128the TV. Any remaining output devices remain unconnected.
1129
1130The EDID that each output reads reports a unique CEC physical address that is
1131based on the physical address of the EDID of the input. So if the EDID of the
1132receiver has physical address A.B.0.0, then each output will see an EDID
1133containing physical address A.B.C.0 where C is 1 to the number of inputs. If
1134there are more outputs than inputs then the remaining outputs have a CEC adapter
1135that is disabled and reports an invalid physical address.
1136
1137
1138Section 16: Some Future Improvements
1112------------------------------------ 1139------------------------------------
1113 1140
1114Just as a reminder and in no particular order: 1141Just as a reminder and in no particular order:
@@ -1121,8 +1148,6 @@ Just as a reminder and in no particular order:
1121- Fix sequence/field numbering when looping of video with alternate fields 1148- Fix sequence/field numbering when looping of video with alternate fields
1122- Add support for V4L2_CID_BG_COLOR for video outputs 1149- Add support for V4L2_CID_BG_COLOR for video outputs
1123- Add ARGB888 overlay support: better testing of the alpha channel 1150- Add ARGB888 overlay support: better testing of the alpha channel
1124- Add custom DV timings support
1125- Add support for V4L2_DV_FL_REDUCED_FPS
1126- Improve pixel aspect support in the tpg code by passing a real v4l2_fract 1151- Improve pixel aspect support in the tpg code by passing a real v4l2_fract
1127- Use per-queue locks and/or per-device locks to improve throughput 1152- Use per-queue locks and/or per-device locks to improve throughput
1128- Add support to loop from a specific output to a specific input across 1153- Add support to loop from a specific output to a specific input across
@@ -1133,3 +1158,4 @@ Just as a reminder and in no particular order:
1133- Make a thread for the RDS generation, that would help in particular for the 1158- Make a thread for the RDS generation, that would help in particular for the
1134 "Controls" RDS Rx I/O Mode as the read-only RDS controls could be updated 1159 "Controls" RDS Rx I/O Mode as the read-only RDS controls could be updated
1135 in real-time. 1160 in real-time.
1161- Changing the EDID should cause hotplug detect emulation to happen.
diff --git a/MAINTAINERS b/MAINTAINERS
index 1cf1beb4b366..0a3827b722b0 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -1648,6 +1648,13 @@ L: linux-media@vger.kernel.org
1648S: Maintained 1648S: Maintained
1649F: drivers/media/platform/s5p-tv/ 1649F: drivers/media/platform/s5p-tv/
1650 1650
1651ARM/SAMSUNG S5P SERIES HDMI CEC SUBSYSTEM SUPPORT
1652M: Kyungmin Park <kyungmin.park@samsung.com>
1653L: linux-arm-kernel@lists.infradead.org
1654L: linux-media@vger.kernel.org
1655S: Maintained
1656F: drivers/staging/media/platform/s5p-cec/
1657
1651ARM/SAMSUNG S5P SERIES JPEG CODEC SUPPORT 1658ARM/SAMSUNG S5P SERIES JPEG CODEC SUPPORT
1652M: Andrzej Pietrasiewicz <andrzej.p@samsung.com> 1659M: Andrzej Pietrasiewicz <andrzej.p@samsung.com>
1653M: Jacek Anaszewski <j.anaszewski@samsung.com> 1660M: Jacek Anaszewski <j.anaszewski@samsung.com>
@@ -2851,6 +2858,22 @@ F: drivers/net/ieee802154/cc2520.c
2851F: include/linux/spi/cc2520.h 2858F: include/linux/spi/cc2520.h
2852F: Documentation/devicetree/bindings/net/ieee802154/cc2520.txt 2859F: Documentation/devicetree/bindings/net/ieee802154/cc2520.txt
2853 2860
2861CEC DRIVER
2862M: Hans Verkuil <hans.verkuil@cisco.com>
2863L: linux-media@vger.kernel.org
2864T: git git://linuxtv.org/media_tree.git
2865W: http://linuxtv.org
2866S: Supported
2867F: Documentation/cec.txt
2868F: Documentation/DocBook/media/v4l/cec*
2869F: drivers/staging/media/cec/
2870F: drivers/media/cec-edid.c
2871F: drivers/media/rc/keymaps/rc-cec.c
2872F: include/media/cec.h
2873F: include/media/cec-edid.h
2874F: include/linux/cec.h
2875F: include/linux/cec-funcs.h
2876
2854CELL BROADBAND ENGINE ARCHITECTURE 2877CELL BROADBAND ENGINE ARCHITECTURE
2855M: Arnd Bergmann <arnd@arndb.de> 2878M: Arnd Bergmann <arnd@arndb.de>
2856L: linuxppc-dev@lists.ozlabs.org 2879L: linuxppc-dev@lists.ozlabs.org
@@ -5177,10 +5200,10 @@ S: Maintained
5177F: drivers/media/usb/gspca/m5602/ 5200F: drivers/media/usb/gspca/m5602/
5178 5201
5179GSPCA PAC207 SONIXB SUBDRIVER 5202GSPCA PAC207 SONIXB SUBDRIVER
5180M: Hans de Goede <hdegoede@redhat.com> 5203M: Hans Verkuil <hverkuil@xs4all.nl>
5181L: linux-media@vger.kernel.org 5204L: linux-media@vger.kernel.org
5182T: git git://linuxtv.org/media_tree.git 5205T: git git://linuxtv.org/media_tree.git
5183S: Maintained 5206S: Odd Fixes
5184F: drivers/media/usb/gspca/pac207.c 5207F: drivers/media/usb/gspca/pac207.c
5185 5208
5186GSPCA SN9C20X SUBDRIVER 5209GSPCA SN9C20X SUBDRIVER
@@ -5198,10 +5221,10 @@ S: Maintained
5198F: drivers/media/usb/gspca/t613.c 5221F: drivers/media/usb/gspca/t613.c
5199 5222
5200GSPCA USB WEBCAM DRIVER 5223GSPCA USB WEBCAM DRIVER
5201M: Hans de Goede <hdegoede@redhat.com> 5224M: Hans Verkuil <hverkuil@xs4all.nl>
5202L: linux-media@vger.kernel.org 5225L: linux-media@vger.kernel.org
5203T: git git://linuxtv.org/media_tree.git 5226T: git git://linuxtv.org/media_tree.git
5204S: Maintained 5227S: Odd Fixes
5205F: drivers/media/usb/gspca/ 5228F: drivers/media/usb/gspca/
5206 5229
5207GUID PARTITION TABLE (GPT) 5230GUID PARTITION TABLE (GPT)
@@ -7344,6 +7367,16 @@ L: linux-iio@vger.kernel.org
7344S: Maintained 7367S: Maintained
7345F: drivers/iio/potentiometer/mcp4531.c 7368F: drivers/iio/potentiometer/mcp4531.c
7346 7369
7370MEDIA DRIVERS FOR RENESAS - FCP
7371M: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
7372L: linux-media@vger.kernel.org
7373L: linux-renesas-soc@vger.kernel.org
7374T: git git://linuxtv.org/media_tree.git
7375S: Supported
7376F: Documentation/devicetree/bindings/media/renesas,fcp.txt
7377F: drivers/media/platform/rcar-fcp.c
7378F: include/media/rcar-fcp.h
7379
7347MEDIA DRIVERS FOR RENESAS - VSP1 7380MEDIA DRIVERS FOR RENESAS - VSP1
7348M: Laurent Pinchart <laurent.pinchart@ideasonboard.com> 7381M: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
7349L: linux-media@vger.kernel.org 7382L: linux-media@vger.kernel.org
@@ -7353,8 +7386,18 @@ S: Supported
7353F: Documentation/devicetree/bindings/media/renesas,vsp1.txt 7386F: Documentation/devicetree/bindings/media/renesas,vsp1.txt
7354F: drivers/media/platform/vsp1/ 7387F: drivers/media/platform/vsp1/
7355 7388
7389MEDIA DRIVERS FOR HELENE
7390M: Abylay Ospan <aospan@netup.ru>
7391L: linux-media@vger.kernel.org
7392W: https://linuxtv.org
7393W: http://netup.tv/
7394T: git git://linuxtv.org/media_tree.git
7395S: Supported
7396F: drivers/media/dvb-frontends/helene*
7397
7356MEDIA DRIVERS FOR ASCOT2E 7398MEDIA DRIVERS FOR ASCOT2E
7357M: Sergey Kozlov <serjk@netup.ru> 7399M: Sergey Kozlov <serjk@netup.ru>
7400M: Abylay Ospan <aospan@netup.ru>
7358L: linux-media@vger.kernel.org 7401L: linux-media@vger.kernel.org
7359W: https://linuxtv.org 7402W: https://linuxtv.org
7360W: http://netup.tv/ 7403W: http://netup.tv/
@@ -7364,6 +7407,7 @@ F: drivers/media/dvb-frontends/ascot2e*
7364 7407
7365MEDIA DRIVERS FOR CXD2841ER 7408MEDIA DRIVERS FOR CXD2841ER
7366M: Sergey Kozlov <serjk@netup.ru> 7409M: Sergey Kozlov <serjk@netup.ru>
7410M: Abylay Ospan <aospan@netup.ru>
7367L: linux-media@vger.kernel.org 7411L: linux-media@vger.kernel.org
7368W: https://linuxtv.org 7412W: https://linuxtv.org
7369W: http://netup.tv/ 7413W: http://netup.tv/
@@ -7373,6 +7417,7 @@ F: drivers/media/dvb-frontends/cxd2841er*
7373 7417
7374MEDIA DRIVERS FOR HORUS3A 7418MEDIA DRIVERS FOR HORUS3A
7375M: Sergey Kozlov <serjk@netup.ru> 7419M: Sergey Kozlov <serjk@netup.ru>
7420M: Abylay Ospan <aospan@netup.ru>
7376L: linux-media@vger.kernel.org 7421L: linux-media@vger.kernel.org
7377W: https://linuxtv.org 7422W: https://linuxtv.org
7378W: http://netup.tv/ 7423W: http://netup.tv/
@@ -7382,6 +7427,7 @@ F: drivers/media/dvb-frontends/horus3a*
7382 7427
7383MEDIA DRIVERS FOR LNBH25 7428MEDIA DRIVERS FOR LNBH25
7384M: Sergey Kozlov <serjk@netup.ru> 7429M: Sergey Kozlov <serjk@netup.ru>
7430M: Abylay Ospan <aospan@netup.ru>
7385L: linux-media@vger.kernel.org 7431L: linux-media@vger.kernel.org
7386W: https://linuxtv.org 7432W: https://linuxtv.org
7387W: http://netup.tv/ 7433W: http://netup.tv/
@@ -7391,6 +7437,7 @@ F: drivers/media/dvb-frontends/lnbh25*
7391 7437
7392MEDIA DRIVERS FOR NETUP PCI UNIVERSAL DVB devices 7438MEDIA DRIVERS FOR NETUP PCI UNIVERSAL DVB devices
7393M: Sergey Kozlov <serjk@netup.ru> 7439M: Sergey Kozlov <serjk@netup.ru>
7440M: Abylay Ospan <aospan@netup.ru>
7394L: linux-media@vger.kernel.org 7441L: linux-media@vger.kernel.org
7395W: https://linuxtv.org 7442W: https://linuxtv.org
7396W: http://netup.tv/ 7443W: http://netup.tv/
@@ -7640,10 +7687,8 @@ L: linux-media@vger.kernel.org
7640W: https://linuxtv.org 7687W: https://linuxtv.org
7641W: http://palosaari.fi/linux/ 7688W: http://palosaari.fi/linux/
7642Q: http://patchwork.linuxtv.org/project/linux-media/list/ 7689Q: http://patchwork.linuxtv.org/project/linux-media/list/
7643T: git git://linuxtv.org/anttip/media_tree.git
7644S: Maintained 7690S: Maintained
7645F: drivers/staging/media/mn88472/ 7691F: drivers/media/dvb-frontends/mn88472*
7646F: drivers/media/dvb-frontends/mn88472.h
7647 7692
7648MN88473 MEDIA DRIVER 7693MN88473 MEDIA DRIVER
7649M: Antti Palosaari <crope@iki.fi> 7694M: Antti Palosaari <crope@iki.fi>
@@ -9255,6 +9300,13 @@ F: include/linux/tracehook.h
9255F: include/uapi/linux/ptrace.h 9300F: include/uapi/linux/ptrace.h
9256F: kernel/ptrace.c 9301F: kernel/ptrace.c
9257 9302
9303PULSE8-CEC DRIVER
9304M: Hans Verkuil <hverkuil@xs4all.nl>
9305L: linux-media@vger.kernel.org
9306T: git git://linuxtv.org/media_tree.git
9307S: Maintained
9308F: drivers/staging/media/pulse8-cec
9309
9258PVRUSB2 VIDEO4LINUX DRIVER 9310PVRUSB2 VIDEO4LINUX DRIVER
9259M: Mike Isely <isely@pobox.com> 9311M: Mike Isely <isely@pobox.com>
9260L: pvrusb2@isely.net (subscribers-only) 9312L: pvrusb2@isely.net (subscribers-only)
@@ -9266,10 +9318,10 @@ F: Documentation/video4linux/README.pvrusb2
9266F: drivers/media/usb/pvrusb2/ 9318F: drivers/media/usb/pvrusb2/
9267 9319
9268PWC WEBCAM DRIVER 9320PWC WEBCAM DRIVER
9269M: Hans de Goede <hdegoede@redhat.com> 9321M: Hans Verkuil <hverkuil@xs4all.nl>
9270L: linux-media@vger.kernel.org 9322L: linux-media@vger.kernel.org
9271T: git git://linuxtv.org/media_tree.git 9323T: git git://linuxtv.org/media_tree.git
9272S: Maintained 9324S: Odd Fixes
9273F: drivers/media/usb/pwc/* 9325F: drivers/media/usb/pwc/*
9274 9326
9275PWM FAN DRIVER 9327PWM FAN DRIVER
@@ -9485,14 +9537,14 @@ F: drivers/video/fbdev/aty/radeon*
9485F: include/uapi/linux/radeonfb.h 9537F: include/uapi/linux/radeonfb.h
9486 9538
9487RADIOSHARK RADIO DRIVER 9539RADIOSHARK RADIO DRIVER
9488M: Hans de Goede <hdegoede@redhat.com> 9540M: Hans Verkuil <hverkuil@xs4all.nl>
9489L: linux-media@vger.kernel.org 9541L: linux-media@vger.kernel.org
9490T: git git://linuxtv.org/media_tree.git 9542T: git git://linuxtv.org/media_tree.git
9491S: Maintained 9543S: Maintained
9492F: drivers/media/radio/radio-shark.c 9544F: drivers/media/radio/radio-shark.c
9493 9545
9494RADIOSHARK2 RADIO DRIVER 9546RADIOSHARK2 RADIO DRIVER
9495M: Hans de Goede <hdegoede@redhat.com> 9547M: Hans Verkuil <hverkuil@xs4all.nl>
9496L: linux-media@vger.kernel.org 9548L: linux-media@vger.kernel.org
9497T: git git://linuxtv.org/media_tree.git 9549T: git git://linuxtv.org/media_tree.git
9498S: Maintained 9550S: Maintained
diff --git a/arch/arm64/boot/dts/mediatek/mt8173.dtsi b/arch/arm64/boot/dts/mediatek/mt8173.dtsi
index 05f89c4a5413..77b8c4e388ca 100644
--- a/arch/arm64/boot/dts/mediatek/mt8173.dtsi
+++ b/arch/arm64/boot/dts/mediatek/mt8173.dtsi
@@ -168,6 +168,18 @@
168 }; 168 };
169 }; 169 };
170 170
171 reserved-memory {
172 #address-cells = <2>;
173 #size-cells = <2>;
174 ranges;
175 vpu_dma_reserved: vpu_dma_mem_region {
176 compatible = "shared-dma-pool";
177 reg = <0 0xb7000000 0 0x500000>;
178 alignment = <0x1000>;
179 no-map;
180 };
181 };
182
171 timer { 183 timer {
172 compatible = "arm,armv8-timer"; 184 compatible = "arm,armv8-timer";
173 interrupt-parent = <&gic>; 185 interrupt-parent = <&gic>;
@@ -312,6 +324,17 @@
312 clock-names = "spi", "wrap"; 324 clock-names = "spi", "wrap";
313 }; 325 };
314 326
327 vpu: vpu@10020000 {
328 compatible = "mediatek,mt8173-vpu";
329 reg = <0 0x10020000 0 0x30000>,
330 <0 0x10050000 0 0x100>;
331 reg-names = "tcm", "cfg_reg";
332 interrupts = <GIC_SPI 166 IRQ_TYPE_LEVEL_HIGH>;
333 clocks = <&topckgen CLK_TOP_SCP_SEL>;
334 clock-names = "main";
335 memory-region = <&vpu_dma_reserved>;
336 };
337
315 sysirq: intpol-controller@10200620 { 338 sysirq: intpol-controller@10200620 {
316 compatible = "mediatek,mt8173-sysirq", 339 compatible = "mediatek,mt8173-sysirq",
317 "mediatek,mt6577-sysirq"; 340 "mediatek,mt6577-sysirq";
@@ -754,6 +777,45 @@
754 clock-names = "apb", "smi"; 777 clock-names = "apb", "smi";
755 }; 778 };
756 779
780 vcodec_enc: vcodec@18002000 {
781 compatible = "mediatek,mt8173-vcodec-enc";
782 reg = <0 0x18002000 0 0x1000>, /* VENC_SYS */
783 <0 0x19002000 0 0x1000>; /* VENC_LT_SYS */
784 interrupts = <GIC_SPI 198 IRQ_TYPE_LEVEL_LOW>,
785 <GIC_SPI 202 IRQ_TYPE_LEVEL_LOW>;
786 mediatek,larb = <&larb3>,
787 <&larb5>;
788 iommus = <&iommu M4U_PORT_VENC_RCPU>,
789 <&iommu M4U_PORT_VENC_REC>,
790 <&iommu M4U_PORT_VENC_BSDMA>,
791 <&iommu M4U_PORT_VENC_SV_COMV>,
792 <&iommu M4U_PORT_VENC_RD_COMV>,
793 <&iommu M4U_PORT_VENC_CUR_LUMA>,
794 <&iommu M4U_PORT_VENC_CUR_CHROMA>,
795 <&iommu M4U_PORT_VENC_REF_LUMA>,
796 <&iommu M4U_PORT_VENC_REF_CHROMA>,
797 <&iommu M4U_PORT_VENC_NBM_RDMA>,
798 <&iommu M4U_PORT_VENC_NBM_WDMA>,
799 <&iommu M4U_PORT_VENC_RCPU_SET2>,
800 <&iommu M4U_PORT_VENC_REC_FRM_SET2>,
801 <&iommu M4U_PORT_VENC_BSDMA_SET2>,
802 <&iommu M4U_PORT_VENC_SV_COMA_SET2>,
803 <&iommu M4U_PORT_VENC_RD_COMA_SET2>,
804 <&iommu M4U_PORT_VENC_CUR_LUMA_SET2>,
805 <&iommu M4U_PORT_VENC_CUR_CHROMA_SET2>,
806 <&iommu M4U_PORT_VENC_REF_LUMA_SET2>,
807 <&iommu M4U_PORT_VENC_REC_CHROMA_SET2>;
808 mediatek,vpu = <&vpu>;
809 clocks = <&topckgen CLK_TOP_VENCPLL_D2>,
810 <&topckgen CLK_TOP_VENC_SEL>,
811 <&topckgen CLK_TOP_UNIVPLL1_D2>,
812 <&topckgen CLK_TOP_VENC_LT_SEL>;
813 clock-names = "venc_sel_src",
814 "venc_sel",
815 "venc_lt_sel_src",
816 "venc_lt_sel";
817 };
818
757 vencltsys: clock-controller@19000000 { 819 vencltsys: clock-controller@19000000 {
758 compatible = "mediatek,mt8173-vencltsys", "syscon"; 820 compatible = "mediatek,mt8173-vencltsys", "syscon";
759 reg = <0 0x19000000 0 0x1000>; 821 reg = <0 0x19000000 0 0x1000>;
diff --git a/arch/blackfin/mach-bf609/boards/ezkit.c b/arch/blackfin/mach-bf609/boards/ezkit.c
index aad5d7416886..9231e5a72b93 100644
--- a/arch/blackfin/mach-bf609/boards/ezkit.c
+++ b/arch/blackfin/mach-bf609/boards/ezkit.c
@@ -1002,14 +1002,12 @@ static struct adv7842_output_format adv7842_opf[] = {
1002 { 1002 {
1003 .op_ch_sel = ADV7842_OP_CH_SEL_BRG, 1003 .op_ch_sel = ADV7842_OP_CH_SEL_BRG,
1004 .op_format_sel = ADV7842_OP_FORMAT_SEL_SDR_ITU656_8, 1004 .op_format_sel = ADV7842_OP_FORMAT_SEL_SDR_ITU656_8,
1005 .op_656_range = 1,
1006 .blank_data = 1, 1005 .blank_data = 1,
1007 .insert_av_codes = 1, 1006 .insert_av_codes = 1,
1008 }, 1007 },
1009 { 1008 {
1010 .op_ch_sel = ADV7842_OP_CH_SEL_RGB, 1009 .op_ch_sel = ADV7842_OP_CH_SEL_RGB,
1011 .op_format_sel = ADV7842_OP_FORMAT_SEL_SDR_ITU656_16, 1010 .op_format_sel = ADV7842_OP_FORMAT_SEL_SDR_ITU656_16,
1012 .op_656_range = 1,
1013 .blank_data = 1, 1011 .blank_data = 1,
1014 }, 1012 },
1015}; 1013};
diff --git a/drivers/gpu/drm/rcar-du/rcar_du_vsp.c b/drivers/gpu/drm/rcar-du/rcar_du_vsp.c
index e671a7cd3463..6ac717f2056f 100644
--- a/drivers/gpu/drm/rcar-du/rcar_du_vsp.c
+++ b/drivers/gpu/drm/rcar-du/rcar_du_vsp.c
@@ -148,40 +148,39 @@ static void rcar_du_vsp_plane_setup(struct rcar_du_vsp_plane *plane)
148 struct rcar_du_vsp_plane_state *state = 148 struct rcar_du_vsp_plane_state *state =
149 to_rcar_vsp_plane_state(plane->plane.state); 149 to_rcar_vsp_plane_state(plane->plane.state);
150 struct drm_framebuffer *fb = plane->plane.state->fb; 150 struct drm_framebuffer *fb = plane->plane.state->fb;
151 struct v4l2_rect src; 151 struct vsp1_du_atomic_config cfg = {
152 struct v4l2_rect dst; 152 .pixelformat = 0,
153 dma_addr_t paddr[2] = { 0, }; 153 .pitch = fb->pitches[0],
154 u32 pixelformat = 0; 154 .alpha = state->alpha,
155 .zpos = state->zpos,
156 };
155 unsigned int i; 157 unsigned int i;
156 158
157 src.left = state->state.src_x >> 16; 159 cfg.src.left = state->state.src_x >> 16;
158 src.top = state->state.src_y >> 16; 160 cfg.src.top = state->state.src_y >> 16;
159 src.width = state->state.src_w >> 16; 161 cfg.src.width = state->state.src_w >> 16;
160 src.height = state->state.src_h >> 16; 162 cfg.src.height = state->state.src_h >> 16;
161 163
162 dst.left = state->state.crtc_x; 164 cfg.dst.left = state->state.crtc_x;
163 dst.top = state->state.crtc_y; 165 cfg.dst.top = state->state.crtc_y;
164 dst.width = state->state.crtc_w; 166 cfg.dst.width = state->state.crtc_w;
165 dst.height = state->state.crtc_h; 167 cfg.dst.height = state->state.crtc_h;
166 168
167 for (i = 0; i < state->format->planes; ++i) { 169 for (i = 0; i < state->format->planes; ++i) {
168 struct drm_gem_cma_object *gem; 170 struct drm_gem_cma_object *gem;
169 171
170 gem = drm_fb_cma_get_gem_obj(fb, i); 172 gem = drm_fb_cma_get_gem_obj(fb, i);
171 paddr[i] = gem->paddr + fb->offsets[i]; 173 cfg.mem[i] = gem->paddr + fb->offsets[i];
172 } 174 }
173 175
174 for (i = 0; i < ARRAY_SIZE(formats_kms); ++i) { 176 for (i = 0; i < ARRAY_SIZE(formats_kms); ++i) {
175 if (formats_kms[i] == state->format->fourcc) { 177 if (formats_kms[i] == state->format->fourcc) {
176 pixelformat = formats_v4l2[i]; 178 cfg.pixelformat = formats_v4l2[i];
177 break; 179 break;
178 } 180 }
179 } 181 }
180 182
181 WARN_ON(!pixelformat); 183 vsp1_du_atomic_update(plane->vsp->vsp, plane->index, &cfg);
182
183 vsp1_du_atomic_update(plane->vsp->vsp, plane->index, pixelformat,
184 fb->pitches[0], paddr, &src, &dst);
185} 184}
186 185
187static int rcar_du_vsp_plane_atomic_check(struct drm_plane *plane, 186static int rcar_du_vsp_plane_atomic_check(struct drm_plane *plane,
@@ -220,8 +219,7 @@ static void rcar_du_vsp_plane_atomic_update(struct drm_plane *plane,
220 if (plane->state->crtc) 219 if (plane->state->crtc)
221 rcar_du_vsp_plane_setup(rplane); 220 rcar_du_vsp_plane_setup(rplane);
222 else 221 else
223 vsp1_du_atomic_update(rplane->vsp->vsp, rplane->index, 0, 0, 0, 222 vsp1_du_atomic_update(rplane->vsp->vsp, rplane->index, NULL);
224 NULL, NULL);
225} 223}
226 224
227static const struct drm_plane_helper_funcs rcar_du_vsp_plane_helper_funcs = { 225static const struct drm_plane_helper_funcs rcar_du_vsp_plane_helper_funcs = {
@@ -269,6 +267,7 @@ static void rcar_du_vsp_plane_reset(struct drm_plane *plane)
269 return; 267 return;
270 268
271 state->alpha = 255; 269 state->alpha = 255;
270 state->zpos = plane->type == DRM_PLANE_TYPE_PRIMARY ? 0 : 1;
272 271
273 plane->state = &state->state; 272 plane->state = &state->state;
274 plane->state->plane = plane; 273 plane->state->plane = plane;
@@ -283,6 +282,8 @@ static int rcar_du_vsp_plane_atomic_set_property(struct drm_plane *plane,
283 282
284 if (property == rcdu->props.alpha) 283 if (property == rcdu->props.alpha)
285 rstate->alpha = val; 284 rstate->alpha = val;
285 else if (property == rcdu->props.zpos)
286 rstate->zpos = val;
286 else 287 else
287 return -EINVAL; 288 return -EINVAL;
288 289
@@ -299,6 +300,8 @@ static int rcar_du_vsp_plane_atomic_get_property(struct drm_plane *plane,
299 300
300 if (property == rcdu->props.alpha) 301 if (property == rcdu->props.alpha)
301 *val = rstate->alpha; 302 *val = rstate->alpha;
303 else if (property == rcdu->props.zpos)
304 *val = rstate->zpos;
302 else 305 else
303 return -EINVAL; 306 return -EINVAL;
304 307
@@ -378,6 +381,8 @@ int rcar_du_vsp_init(struct rcar_du_vsp *vsp)
378 381
379 drm_object_attach_property(&plane->plane.base, 382 drm_object_attach_property(&plane->plane.base,
380 rcdu->props.alpha, 255); 383 rcdu->props.alpha, 255);
384 drm_object_attach_property(&plane->plane.base,
385 rcdu->props.zpos, 1);
381 } 386 }
382 387
383 return 0; 388 return 0;
diff --git a/drivers/gpu/drm/rcar-du/rcar_du_vsp.h b/drivers/gpu/drm/rcar-du/rcar_du_vsp.h
index df3bf3805c69..510dcc9c6816 100644
--- a/drivers/gpu/drm/rcar-du/rcar_du_vsp.h
+++ b/drivers/gpu/drm/rcar-du/rcar_du_vsp.h
@@ -44,6 +44,7 @@ static inline struct rcar_du_vsp_plane *to_rcar_vsp_plane(struct drm_plane *p)
44 * @state: base DRM plane state 44 * @state: base DRM plane state
45 * @format: information about the pixel format used by the plane 45 * @format: information about the pixel format used by the plane
46 * @alpha: value of the plane alpha property 46 * @alpha: value of the plane alpha property
47 * @zpos: value of the plane zpos property
47 */ 48 */
48struct rcar_du_vsp_plane_state { 49struct rcar_du_vsp_plane_state {
49 struct drm_plane_state state; 50 struct drm_plane_state state;
@@ -51,6 +52,7 @@ struct rcar_du_vsp_plane_state {
51 const struct rcar_du_format_info *format; 52 const struct rcar_du_format_info *format;
52 53
53 unsigned int alpha; 54 unsigned int alpha;
55 unsigned int zpos;
54}; 56};
55 57
56static inline struct rcar_du_vsp_plane_state * 58static inline struct rcar_du_vsp_plane_state *
diff --git a/drivers/input/touchscreen/sur40.c b/drivers/input/touchscreen/sur40.c
index 880c40b23f66..4ea475775d58 100644
--- a/drivers/input/touchscreen/sur40.c
+++ b/drivers/input/touchscreen/sur40.c
@@ -126,7 +126,7 @@ struct sur40_image_header {
126#define VIDEO_PACKET_SIZE 16384 126#define VIDEO_PACKET_SIZE 16384
127 127
128/* polling interval (ms) */ 128/* polling interval (ms) */
129#define POLL_INTERVAL 4 129#define POLL_INTERVAL 1
130 130
131/* maximum number of contacts FIXME: this is a guess? */ 131/* maximum number of contacts FIXME: this is a guess? */
132#define MAX_CONTACTS 64 132#define MAX_CONTACTS 64
@@ -151,7 +151,6 @@ struct sur40_state {
151 struct mutex lock; 151 struct mutex lock;
152 152
153 struct vb2_queue queue; 153 struct vb2_queue queue;
154 struct vb2_alloc_ctx *alloc_ctx;
155 struct list_head buf_list; 154 struct list_head buf_list;
156 spinlock_t qlock; 155 spinlock_t qlock;
157 int sequence; 156 int sequence;
@@ -448,7 +447,7 @@ static void sur40_process_video(struct sur40_state *sur40)
448 447
449 /* return error if streaming was stopped in the meantime */ 448 /* return error if streaming was stopped in the meantime */
450 if (sur40->sequence == -1) 449 if (sur40->sequence == -1)
451 goto err_poll; 450 return;
452 451
453 /* mark as finished */ 452 /* mark as finished */
454 new_buf->vb.vb2_buf.timestamp = ktime_get_ns(); 453 new_buf->vb.vb2_buf.timestamp = ktime_get_ns();
@@ -580,19 +579,13 @@ static int sur40_probe(struct usb_interface *interface,
580 sur40->queue = sur40_queue; 579 sur40->queue = sur40_queue;
581 sur40->queue.drv_priv = sur40; 580 sur40->queue.drv_priv = sur40;
582 sur40->queue.lock = &sur40->lock; 581 sur40->queue.lock = &sur40->lock;
582 sur40->queue.dev = sur40->dev;
583 583
584 /* initialize the queue */ 584 /* initialize the queue */
585 error = vb2_queue_init(&sur40->queue); 585 error = vb2_queue_init(&sur40->queue);
586 if (error) 586 if (error)
587 goto err_unreg_v4l2; 587 goto err_unreg_v4l2;
588 588
589 sur40->alloc_ctx = vb2_dma_sg_init_ctx(sur40->dev);
590 if (IS_ERR(sur40->alloc_ctx)) {
591 dev_err(sur40->dev, "Can't allocate buffer context");
592 error = PTR_ERR(sur40->alloc_ctx);
593 goto err_unreg_v4l2;
594 }
595
596 sur40->vdev = sur40_video_device; 589 sur40->vdev = sur40_video_device;
597 sur40->vdev.v4l2_dev = &sur40->v4l2; 590 sur40->vdev.v4l2_dev = &sur40->v4l2;
598 sur40->vdev.lock = &sur40->lock; 591 sur40->vdev.lock = &sur40->lock;
@@ -633,7 +626,6 @@ static void sur40_disconnect(struct usb_interface *interface)
633 626
634 video_unregister_device(&sur40->vdev); 627 video_unregister_device(&sur40->vdev);
635 v4l2_device_unregister(&sur40->v4l2); 628 v4l2_device_unregister(&sur40->v4l2);
636 vb2_dma_sg_cleanup_ctx(sur40->alloc_ctx);
637 629
638 input_unregister_polled_device(sur40->input); 630 input_unregister_polled_device(sur40->input);
639 input_free_polled_device(sur40->input); 631 input_free_polled_device(sur40->input);
@@ -653,13 +645,10 @@ static void sur40_disconnect(struct usb_interface *interface)
653 */ 645 */
654static int sur40_queue_setup(struct vb2_queue *q, 646static int sur40_queue_setup(struct vb2_queue *q,
655 unsigned int *nbuffers, unsigned int *nplanes, 647 unsigned int *nbuffers, unsigned int *nplanes,
656 unsigned int sizes[], void *alloc_ctxs[]) 648 unsigned int sizes[], struct device *alloc_devs[])
657{ 649{
658 struct sur40_state *sur40 = vb2_get_drv_priv(q);
659
660 if (q->num_buffers + *nbuffers < 3) 650 if (q->num_buffers + *nbuffers < 3)
661 *nbuffers = 3 - q->num_buffers; 651 *nbuffers = 3 - q->num_buffers;
662 alloc_ctxs[0] = sur40->alloc_ctx;
663 652
664 if (*nplanes) 653 if (*nplanes)
665 return sizes[0] < sur40_video_format.sizeimage ? -EINVAL : 0; 654 return sizes[0] < sur40_video_format.sizeimage ? -EINVAL : 0;
@@ -736,6 +725,7 @@ static int sur40_start_streaming(struct vb2_queue *vq, unsigned int count)
736static void sur40_stop_streaming(struct vb2_queue *vq) 725static void sur40_stop_streaming(struct vb2_queue *vq)
737{ 726{
738 struct sur40_state *sur40 = vb2_get_drv_priv(vq); 727 struct sur40_state *sur40 = vb2_get_drv_priv(vq);
728 vb2_wait_for_all_buffers(vq);
739 sur40->sequence = -1; 729 sur40->sequence = -1;
740 730
741 /* Release all active buffers */ 731 /* Release all active buffers */
@@ -793,7 +783,6 @@ static int sur40_vidioc_enum_fmt(struct file *file, void *priv,
793{ 783{
794 if (f->index != 0) 784 if (f->index != 0)
795 return -EINVAL; 785 return -EINVAL;
796 strlcpy(f->description, "8-bit greyscale", sizeof(f->description));
797 f->pixelformat = V4L2_PIX_FMT_GREY; 786 f->pixelformat = V4L2_PIX_FMT_GREY;
798 f->flags = 0; 787 f->flags = 0;
799 return 0; 788 return 0;
diff --git a/drivers/media/Kconfig b/drivers/media/Kconfig
index a8518fb3bca7..962f2a9a6614 100644
--- a/drivers/media/Kconfig
+++ b/drivers/media/Kconfig
@@ -80,6 +80,9 @@ config MEDIA_RC_SUPPORT
80 80
81 Say Y when you have a TV or an IR device. 81 Say Y when you have a TV or an IR device.
82 82
83config MEDIA_CEC_EDID
84 bool
85
83# 86#
84# Media controller 87# Media controller
85# Selectable only for webcam/grabbers, as other drivers don't use it 88# Selectable only for webcam/grabbers, as other drivers don't use it
diff --git a/drivers/media/Makefile b/drivers/media/Makefile
index e608bbce0c35..081a7866fd44 100644
--- a/drivers/media/Makefile
+++ b/drivers/media/Makefile
@@ -2,6 +2,10 @@
2# Makefile for the kernel multimedia device drivers. 2# Makefile for the kernel multimedia device drivers.
3# 3#
4 4
5ifeq ($(CONFIG_MEDIA_CEC_EDID),y)
6 obj-$(CONFIG_MEDIA_SUPPORT) += cec-edid.o
7endif
8
5media-objs := media-device.o media-devnode.o media-entity.o 9media-objs := media-device.o media-devnode.o media-entity.o
6 10
7# 11#
diff --git a/drivers/media/cec-edid.c b/drivers/media/cec-edid.c
new file mode 100644
index 000000000000..70018247bdda
--- /dev/null
+++ b/drivers/media/cec-edid.c
@@ -0,0 +1,168 @@
1/*
2 * cec-edid - HDMI Consumer Electronics Control EDID & CEC helper functions
3 *
4 * Copyright 2016 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
5 *
6 * This program is free software; you may redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
11 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
12 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
13 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
14 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
15 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
16 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17 * SOFTWARE.
18 */
19
20#include <linux/module.h>
21#include <linux/kernel.h>
22#include <linux/types.h>
23#include <media/cec-edid.h>
24
25/*
26 * This EDID is expected to be a CEA-861 compliant, which means that there are
27 * at least two blocks and one or more of the extensions blocks are CEA-861
28 * blocks.
29 *
30 * The returned location is guaranteed to be < size - 1.
31 */
32static unsigned int cec_get_edid_spa_location(const u8 *edid, unsigned int size)
33{
34 unsigned int blocks = size / 128;
35 unsigned int block;
36 u8 d;
37
38 /* Sanity check: at least 2 blocks and a multiple of the block size */
39 if (blocks < 2 || size % 128)
40 return 0;
41
42 /*
43 * If there are fewer extension blocks than the size, then update
44 * 'blocks'. It is allowed to have more extension blocks than the size,
45 * since some hardware can only read e.g. 256 bytes of the EDID, even
46 * though more blocks are present. The first CEA-861 extension block
47 * should normally be in block 1 anyway.
48 */
49 if (edid[0x7e] + 1 < blocks)
50 blocks = edid[0x7e] + 1;
51
52 for (block = 1; block < blocks; block++) {
53 unsigned int offset = block * 128;
54
55 /* Skip any non-CEA-861 extension blocks */
56 if (edid[offset] != 0x02 || edid[offset + 1] != 0x03)
57 continue;
58
59 /* search Vendor Specific Data Block (tag 3) */
60 d = edid[offset + 2] & 0x7f;
61 /* Check if there are Data Blocks */
62 if (d <= 4)
63 continue;
64 if (d > 4) {
65 unsigned int i = offset + 4;
66 unsigned int end = offset + d;
67
68 /* Note: 'end' is always < 'size' */
69 do {
70 u8 tag = edid[i] >> 5;
71 u8 len = edid[i] & 0x1f;
72
73 if (tag == 3 && len >= 5 && i + len <= end)
74 return i + 4;
75 i += len + 1;
76 } while (i < end);
77 }
78 }
79 return 0;
80}
81
82u16 cec_get_edid_phys_addr(const u8 *edid, unsigned int size,
83 unsigned int *offset)
84{
85 unsigned int loc = cec_get_edid_spa_location(edid, size);
86
87 if (offset)
88 *offset = loc;
89 if (loc == 0)
90 return CEC_PHYS_ADDR_INVALID;
91 return (edid[loc] << 8) | edid[loc + 1];
92}
93EXPORT_SYMBOL_GPL(cec_get_edid_phys_addr);
94
95void cec_set_edid_phys_addr(u8 *edid, unsigned int size, u16 phys_addr)
96{
97 unsigned int loc = cec_get_edid_spa_location(edid, size);
98 u8 sum = 0;
99 unsigned int i;
100
101 if (loc == 0)
102 return;
103 edid[loc] = phys_addr >> 8;
104 edid[loc + 1] = phys_addr & 0xff;
105 loc &= ~0x7f;
106
107 /* update the checksum */
108 for (i = loc; i < loc + 127; i++)
109 sum += edid[i];
110 edid[i] = 256 - sum;
111}
112EXPORT_SYMBOL_GPL(cec_set_edid_phys_addr);
113
114u16 cec_phys_addr_for_input(u16 phys_addr, u8 input)
115{
116 /* Check if input is sane */
117 if (WARN_ON(input == 0 || input > 0xf))
118 return CEC_PHYS_ADDR_INVALID;
119
120 if (phys_addr == 0)
121 return input << 12;
122
123 if ((phys_addr & 0x0fff) == 0)
124 return phys_addr | (input << 8);
125
126 if ((phys_addr & 0x00ff) == 0)
127 return phys_addr | (input << 4);
128
129 if ((phys_addr & 0x000f) == 0)
130 return phys_addr | input;
131
132 /*
133 * All nibbles are used so no valid physical addresses can be assigned
134 * to the input.
135 */
136 return CEC_PHYS_ADDR_INVALID;
137}
138EXPORT_SYMBOL_GPL(cec_phys_addr_for_input);
139
140int cec_phys_addr_validate(u16 phys_addr, u16 *parent, u16 *port)
141{
142 int i;
143
144 if (parent)
145 *parent = phys_addr;
146 if (port)
147 *port = 0;
148 if (phys_addr == CEC_PHYS_ADDR_INVALID)
149 return 0;
150 for (i = 0; i < 16; i += 4)
151 if (phys_addr & (0xf << i))
152 break;
153 if (i == 16)
154 return 0;
155 if (parent)
156 *parent = phys_addr & (0xfff0 << i);
157 if (port)
158 *port = (phys_addr >> i) & 0xf;
159 for (i += 4; i < 16; i += 4)
160 if ((phys_addr & (0xf << i)) == 0)
161 return -EINVAL;
162 return 0;
163}
164EXPORT_SYMBOL_GPL(cec_phys_addr_validate);
165
166MODULE_AUTHOR("Hans Verkuil <hans.verkuil@cisco.com>");
167MODULE_DESCRIPTION("CEC EDID helper functions");
168MODULE_LICENSE("GPL");
diff --git a/drivers/media/common/v4l2-tpg/v4l2-tpg-core.c b/drivers/media/common/v4l2-tpg/v4l2-tpg-core.c
index cf1dadd0be9e..3ec3cebe62b9 100644
--- a/drivers/media/common/v4l2-tpg/v4l2-tpg-core.c
+++ b/drivers/media/common/v4l2-tpg/v4l2-tpg-core.c
@@ -777,7 +777,7 @@ static void precalculate_color(struct tpg_data *tpg, int k)
777 * Remember that r, g and b are still in the 0 - 0xff0 range. 777 * Remember that r, g and b are still in the 0 - 0xff0 range.
778 */ 778 */
779 if (tpg->real_rgb_range == V4L2_DV_RGB_RANGE_LIMITED && 779 if (tpg->real_rgb_range == V4L2_DV_RGB_RANGE_LIMITED &&
780 tpg->rgb_range == V4L2_DV_RGB_RANGE_FULL) { 780 tpg->rgb_range == V4L2_DV_RGB_RANGE_FULL && !tpg->is_yuv) {
781 /* 781 /*
782 * Convert from full range (which is what r, g and b are) 782 * Convert from full range (which is what r, g and b are)
783 * to limited range (which is the 'real' RGB range), which 783 * to limited range (which is the 'real' RGB range), which
@@ -787,7 +787,7 @@ static void precalculate_color(struct tpg_data *tpg, int k)
787 g = (g * 219) / 255 + (16 << 4); 787 g = (g * 219) / 255 + (16 << 4);
788 b = (b * 219) / 255 + (16 << 4); 788 b = (b * 219) / 255 + (16 << 4);
789 } else if (tpg->real_rgb_range != V4L2_DV_RGB_RANGE_LIMITED && 789 } else if (tpg->real_rgb_range != V4L2_DV_RGB_RANGE_LIMITED &&
790 tpg->rgb_range == V4L2_DV_RGB_RANGE_LIMITED) { 790 tpg->rgb_range == V4L2_DV_RGB_RANGE_LIMITED && !tpg->is_yuv) {
791 /* 791 /*
792 * Clamp r, g and b to the limited range and convert to full 792 * Clamp r, g and b to the limited range and convert to full
793 * range since that's what we deliver. 793 * range since that's what we deliver.
diff --git a/drivers/media/dvb-core/demux.h b/drivers/media/dvb-core/demux.h
index 6d3b95b8939d..7f1dffef4353 100644
--- a/drivers/media/dvb-core/demux.h
+++ b/drivers/media/dvb-core/demux.h
@@ -143,7 +143,7 @@ struct dmx_ts_feed {
143 int type, 143 int type,
144 enum dmx_ts_pes pes_type, 144 enum dmx_ts_pes pes_type,
145 size_t circular_buffer_size, 145 size_t circular_buffer_size,
146 struct timespec timeout); 146 ktime_t timeout);
147 int (*start_filtering)(struct dmx_ts_feed *feed); 147 int (*start_filtering)(struct dmx_ts_feed *feed);
148 int (*stop_filtering)(struct dmx_ts_feed *feed); 148 int (*stop_filtering)(struct dmx_ts_feed *feed);
149}; 149};
diff --git a/drivers/media/dvb-core/dmxdev.c b/drivers/media/dvb-core/dmxdev.c
index a168cbe1c998..7b67e1dd97fd 100644
--- a/drivers/media/dvb-core/dmxdev.c
+++ b/drivers/media/dvb-core/dmxdev.c
@@ -556,7 +556,7 @@ static int dvb_dmxdev_start_feed(struct dmxdev *dmxdev,
556 struct dmxdev_filter *filter, 556 struct dmxdev_filter *filter,
557 struct dmxdev_feed *feed) 557 struct dmxdev_feed *feed)
558{ 558{
559 struct timespec timeout = { 0 }; 559 ktime_t timeout = ktime_set(0, 0);
560 struct dmx_pes_filter_params *para = &filter->params.pes; 560 struct dmx_pes_filter_params *para = &filter->params.pes;
561 dmx_output_t otype; 561 dmx_output_t otype;
562 int ret; 562 int ret;
diff --git a/drivers/media/dvb-core/dvb_ca_en50221.c b/drivers/media/dvb-core/dvb_ca_en50221.c
index f82cd1ff4f3a..b5b5b195ea7f 100644
--- a/drivers/media/dvb-core/dvb_ca_en50221.c
+++ b/drivers/media/dvb-core/dvb_ca_en50221.c
@@ -123,6 +123,7 @@ struct dvb_ca_slot {
123 123
124/* Private CA-interface information */ 124/* Private CA-interface information */
125struct dvb_ca_private { 125struct dvb_ca_private {
126 struct kref refcount;
126 127
127 /* pointer back to the public data structure */ 128 /* pointer back to the public data structure */
128 struct dvb_ca_en50221 *pub; 129 struct dvb_ca_en50221 *pub;
@@ -161,6 +162,34 @@ struct dvb_ca_private {
161 struct mutex ioctl_mutex; 162 struct mutex ioctl_mutex;
162}; 163};
163 164
165static void dvb_ca_private_free(struct dvb_ca_private *ca)
166{
167 unsigned int i;
168
169 dvb_unregister_device(ca->dvbdev);
170 for (i = 0; i < ca->slot_count; i++)
171 vfree(ca->slot_info[i].rx_buffer.data);
172
173 kfree(ca->slot_info);
174 kfree(ca);
175}
176
177static void dvb_ca_private_release(struct kref *ref)
178{
179 struct dvb_ca_private *ca = container_of(ref, struct dvb_ca_private, refcount);
180 dvb_ca_private_free(ca);
181}
182
183static void dvb_ca_private_get(struct dvb_ca_private *ca)
184{
185 kref_get(&ca->refcount);
186}
187
188static void dvb_ca_private_put(struct dvb_ca_private *ca)
189{
190 kref_put(&ca->refcount, dvb_ca_private_release);
191}
192
164static void dvb_ca_en50221_thread_wakeup(struct dvb_ca_private *ca); 193static void dvb_ca_en50221_thread_wakeup(struct dvb_ca_private *ca);
165static int dvb_ca_en50221_read_data(struct dvb_ca_private *ca, int slot, u8 * ebuf, int ecount); 194static int dvb_ca_en50221_read_data(struct dvb_ca_private *ca, int slot, u8 * ebuf, int ecount);
166static int dvb_ca_en50221_write_data(struct dvb_ca_private *ca, int slot, u8 * ebuf, int ecount); 195static int dvb_ca_en50221_write_data(struct dvb_ca_private *ca, int slot, u8 * ebuf, int ecount);
@@ -1558,6 +1587,8 @@ static int dvb_ca_en50221_io_open(struct inode *inode, struct file *file)
1558 dvb_ca_en50221_thread_update_delay(ca); 1587 dvb_ca_en50221_thread_update_delay(ca);
1559 dvb_ca_en50221_thread_wakeup(ca); 1588 dvb_ca_en50221_thread_wakeup(ca);
1560 1589
1590 dvb_ca_private_get(ca);
1591
1561 return 0; 1592 return 0;
1562} 1593}
1563 1594
@@ -1586,6 +1617,8 @@ static int dvb_ca_en50221_io_release(struct inode *inode, struct file *file)
1586 1617
1587 module_put(ca->pub->owner); 1618 module_put(ca->pub->owner);
1588 1619
1620 dvb_ca_private_put(ca);
1621
1589 return err; 1622 return err;
1590} 1623}
1591 1624
@@ -1681,6 +1714,7 @@ int dvb_ca_en50221_init(struct dvb_adapter *dvb_adapter,
1681 ret = -ENOMEM; 1714 ret = -ENOMEM;
1682 goto exit; 1715 goto exit;
1683 } 1716 }
1717 kref_init(&ca->refcount);
1684 ca->pub = pubca; 1718 ca->pub = pubca;
1685 ca->flags = flags; 1719 ca->flags = flags;
1686 ca->slot_count = slot_count; 1720 ca->slot_count = slot_count;
@@ -1759,10 +1793,7 @@ void dvb_ca_en50221_release(struct dvb_ca_en50221 *pubca)
1759 1793
1760 for (i = 0; i < ca->slot_count; i++) { 1794 for (i = 0; i < ca->slot_count; i++) {
1761 dvb_ca_en50221_slot_shutdown(ca, i); 1795 dvb_ca_en50221_slot_shutdown(ca, i);
1762 vfree(ca->slot_info[i].rx_buffer.data);
1763 } 1796 }
1764 kfree(ca->slot_info); 1797 dvb_ca_private_put(ca);
1765 dvb_unregister_device(ca->dvbdev);
1766 kfree(ca);
1767 pubca->private = NULL; 1798 pubca->private = NULL;
1768} 1799}
diff --git a/drivers/media/dvb-core/dvb_demux.c b/drivers/media/dvb-core/dvb_demux.c
index 0cc5e935166c..a0cf7b0d03e8 100644
--- a/drivers/media/dvb-core/dvb_demux.c
+++ b/drivers/media/dvb-core/dvb_demux.c
@@ -398,28 +398,23 @@ static void dvb_dmx_swfilter_packet(struct dvb_demux *demux, const u8 *buf)
398 int dvr_done = 0; 398 int dvr_done = 0;
399 399
400 if (dvb_demux_speedcheck) { 400 if (dvb_demux_speedcheck) {
401 struct timespec cur_time, delta_time; 401 ktime_t cur_time;
402 u64 speed_bytes, speed_timedelta; 402 u64 speed_bytes, speed_timedelta;
403 403
404 demux->speed_pkts_cnt++; 404 demux->speed_pkts_cnt++;
405 405
406 /* show speed every SPEED_PKTS_INTERVAL packets */ 406 /* show speed every SPEED_PKTS_INTERVAL packets */
407 if (!(demux->speed_pkts_cnt % SPEED_PKTS_INTERVAL)) { 407 if (!(demux->speed_pkts_cnt % SPEED_PKTS_INTERVAL)) {
408 cur_time = current_kernel_time(); 408 cur_time = ktime_get();
409 409
410 if (demux->speed_last_time.tv_sec != 0 && 410 if (ktime_to_ns(demux->speed_last_time) != 0) {
411 demux->speed_last_time.tv_nsec != 0) {
412 delta_time = timespec_sub(cur_time,
413 demux->speed_last_time);
414 speed_bytes = (u64)demux->speed_pkts_cnt 411 speed_bytes = (u64)demux->speed_pkts_cnt
415 * 188 * 8; 412 * 188 * 8;
416 /* convert to 1024 basis */ 413 /* convert to 1024 basis */
417 speed_bytes = 1000 * div64_u64(speed_bytes, 414 speed_bytes = 1000 * div64_u64(speed_bytes,
418 1024); 415 1024);
419 speed_timedelta = 416 speed_timedelta = ktime_ms_delta(cur_time,
420 (u64)timespec_to_ns(&delta_time); 417 demux->speed_last_time);
421 speed_timedelta = div64_u64(speed_timedelta,
422 1000000); /* nsec -> usec */
423 printk(KERN_INFO "TS speed %llu Kbits/sec \n", 418 printk(KERN_INFO "TS speed %llu Kbits/sec \n",
424 div64_u64(speed_bytes, 419 div64_u64(speed_bytes,
425 speed_timedelta)); 420 speed_timedelta));
@@ -666,7 +661,7 @@ out:
666 661
667static int dmx_ts_feed_set(struct dmx_ts_feed *ts_feed, u16 pid, int ts_type, 662static int dmx_ts_feed_set(struct dmx_ts_feed *ts_feed, u16 pid, int ts_type,
668 enum dmx_ts_pes pes_type, 663 enum dmx_ts_pes pes_type,
669 size_t circular_buffer_size, struct timespec timeout) 664 size_t circular_buffer_size, ktime_t timeout)
670{ 665{
671 struct dvb_demux_feed *feed = (struct dvb_demux_feed *)ts_feed; 666 struct dvb_demux_feed *feed = (struct dvb_demux_feed *)ts_feed;
672 struct dvb_demux *demux = feed->demux; 667 struct dvb_demux *demux = feed->demux;
diff --git a/drivers/media/dvb-core/dvb_demux.h b/drivers/media/dvb-core/dvb_demux.h
index ae7fc33c3231..5ed3cab4ad28 100644
--- a/drivers/media/dvb-core/dvb_demux.h
+++ b/drivers/media/dvb-core/dvb_demux.h
@@ -83,7 +83,7 @@ struct dvb_demux_feed {
83 u8 *buffer; 83 u8 *buffer;
84 int buffer_size; 84 int buffer_size;
85 85
86 struct timespec timeout; 86 ktime_t timeout;
87 struct dvb_demux_filter *filter; 87 struct dvb_demux_filter *filter;
88 88
89 int ts_type; 89 int ts_type;
@@ -134,7 +134,7 @@ struct dvb_demux {
134 134
135 uint8_t *cnt_storage; /* for TS continuity check */ 135 uint8_t *cnt_storage; /* for TS continuity check */
136 136
137 struct timespec speed_last_time; /* for TS speed check */ 137 ktime_t speed_last_time; /* for TS speed check */
138 uint32_t speed_pkts_cnt; /* for TS speed check */ 138 uint32_t speed_pkts_cnt; /* for TS speed check */
139}; 139};
140 140
diff --git a/drivers/media/dvb-core/dvb_frontend.c b/drivers/media/dvb-core/dvb_frontend.c
index c0142614c408..be99c8dbc5f8 100644
--- a/drivers/media/dvb-core/dvb_frontend.c
+++ b/drivers/media/dvb-core/dvb_frontend.c
@@ -99,6 +99,7 @@ MODULE_PARM_DESC(dvb_mfe_wait_time, "Wait up to <mfe_wait_time> seconds on open(
99static DEFINE_MUTEX(frontend_mutex); 99static DEFINE_MUTEX(frontend_mutex);
100 100
101struct dvb_frontend_private { 101struct dvb_frontend_private {
102 struct kref refcount;
102 103
103 /* thread/frontend values */ 104 /* thread/frontend values */
104 struct dvb_device *dvbdev; 105 struct dvb_device *dvbdev;
@@ -137,6 +138,23 @@ struct dvb_frontend_private {
137#endif 138#endif
138}; 139};
139 140
141static void dvb_frontend_private_free(struct kref *ref)
142{
143 struct dvb_frontend_private *fepriv =
144 container_of(ref, struct dvb_frontend_private, refcount);
145 kfree(fepriv);
146}
147
148static void dvb_frontend_private_put(struct dvb_frontend_private *fepriv)
149{
150 kref_put(&fepriv->refcount, dvb_frontend_private_free);
151}
152
153static void dvb_frontend_private_get(struct dvb_frontend_private *fepriv)
154{
155 kref_get(&fepriv->refcount);
156}
157
140static void dvb_frontend_wakeup(struct dvb_frontend *fe); 158static void dvb_frontend_wakeup(struct dvb_frontend *fe);
141static int dtv_get_frontend(struct dvb_frontend *fe, 159static int dtv_get_frontend(struct dvb_frontend *fe,
142 struct dtv_frontend_properties *c, 160 struct dtv_frontend_properties *c,
@@ -2543,6 +2561,8 @@ static int dvb_frontend_open(struct inode *inode, struct file *file)
2543 fepriv->events.eventr = fepriv->events.eventw = 0; 2561 fepriv->events.eventr = fepriv->events.eventw = 0;
2544 } 2562 }
2545 2563
2564 dvb_frontend_private_get(fepriv);
2565
2546 if (adapter->mfe_shared) 2566 if (adapter->mfe_shared)
2547 mutex_unlock (&adapter->mfe_lock); 2567 mutex_unlock (&adapter->mfe_lock);
2548 return ret; 2568 return ret;
@@ -2591,6 +2611,8 @@ static int dvb_frontend_release(struct inode *inode, struct file *file)
2591 fe->ops.ts_bus_ctrl(fe, 0); 2611 fe->ops.ts_bus_ctrl(fe, 0);
2592 } 2612 }
2593 2613
2614 dvb_frontend_private_put(fepriv);
2615
2594 return ret; 2616 return ret;
2595} 2617}
2596 2618
@@ -2679,6 +2701,8 @@ int dvb_register_frontend(struct dvb_adapter* dvb,
2679 } 2701 }
2680 fepriv = fe->frontend_priv; 2702 fepriv = fe->frontend_priv;
2681 2703
2704 kref_init(&fepriv->refcount);
2705
2682 sema_init(&fepriv->sem, 1); 2706 sema_init(&fepriv->sem, 1);
2683 init_waitqueue_head (&fepriv->wait_queue); 2707 init_waitqueue_head (&fepriv->wait_queue);
2684 init_waitqueue_head (&fepriv->events.wait_queue); 2708 init_waitqueue_head (&fepriv->events.wait_queue);
@@ -2713,18 +2737,11 @@ int dvb_unregister_frontend(struct dvb_frontend* fe)
2713 2737
2714 mutex_lock(&frontend_mutex); 2738 mutex_lock(&frontend_mutex);
2715 dvb_frontend_stop (fe); 2739 dvb_frontend_stop (fe);
2716 mutex_unlock(&frontend_mutex);
2717
2718 if (fepriv->dvbdev->users < -1)
2719 wait_event(fepriv->dvbdev->wait_queue,
2720 fepriv->dvbdev->users==-1);
2721
2722 mutex_lock(&frontend_mutex);
2723 dvb_unregister_device (fepriv->dvbdev); 2740 dvb_unregister_device (fepriv->dvbdev);
2724 2741
2725 /* fe is invalid now */ 2742 /* fe is invalid now */
2726 kfree(fepriv);
2727 mutex_unlock(&frontend_mutex); 2743 mutex_unlock(&frontend_mutex);
2744 dvb_frontend_private_put(fepriv);
2728 return 0; 2745 return 0;
2729} 2746}
2730EXPORT_SYMBOL(dvb_unregister_frontend); 2747EXPORT_SYMBOL(dvb_unregister_frontend);
diff --git a/drivers/media/dvb-core/dvb_net.c b/drivers/media/dvb-core/dvb_net.c
index ce6a711b42d4..9914f69a4a02 100644
--- a/drivers/media/dvb-core/dvb_net.c
+++ b/drivers/media/dvb-core/dvb_net.c
@@ -997,7 +997,7 @@ static int dvb_net_feed_start(struct net_device *dev)
997 netdev_dbg(dev, "start filtering\n"); 997 netdev_dbg(dev, "start filtering\n");
998 priv->secfeed->start_filtering(priv->secfeed); 998 priv->secfeed->start_filtering(priv->secfeed);
999 } else if (priv->feedtype == DVB_NET_FEEDTYPE_ULE) { 999 } else if (priv->feedtype == DVB_NET_FEEDTYPE_ULE) {
1000 struct timespec timeout = { 0, 10000000 }; // 10 msec 1000 ktime_t timeout = ns_to_ktime(10 * NSEC_PER_MSEC);
1001 1001
1002 /* we have payloads encapsulated in TS */ 1002 /* we have payloads encapsulated in TS */
1003 netdev_dbg(dev, "alloc tsfeed\n"); 1003 netdev_dbg(dev, "alloc tsfeed\n");
diff --git a/drivers/media/dvb-core/dvb_ringbuffer.c b/drivers/media/dvb-core/dvb_ringbuffer.c
index 1100e98a7b1d..7df7fb3738a0 100644
--- a/drivers/media/dvb-core/dvb_ringbuffer.c
+++ b/drivers/media/dvb-core/dvb_ringbuffer.c
@@ -55,7 +55,13 @@ void dvb_ringbuffer_init(struct dvb_ringbuffer *rbuf, void *data, size_t len)
55 55
56int dvb_ringbuffer_empty(struct dvb_ringbuffer *rbuf) 56int dvb_ringbuffer_empty(struct dvb_ringbuffer *rbuf)
57{ 57{
58 return (rbuf->pread==rbuf->pwrite); 58 /* smp_load_acquire() to load write pointer on reader side
59 * this pairs with smp_store_release() in dvb_ringbuffer_write(),
60 * dvb_ringbuffer_write_user(), or dvb_ringbuffer_reset()
61 *
62 * for memory barriers also see Documentation/circular-buffers.txt
63 */
64 return (rbuf->pread == smp_load_acquire(&rbuf->pwrite));
59} 65}
60 66
61 67
@@ -64,7 +70,12 @@ ssize_t dvb_ringbuffer_free(struct dvb_ringbuffer *rbuf)
64{ 70{
65 ssize_t free; 71 ssize_t free;
66 72
67 free = rbuf->pread - rbuf->pwrite; 73 /* ACCESS_ONCE() to load read pointer on writer side
74 * this pairs with smp_store_release() in dvb_ringbuffer_read(),
75 * dvb_ringbuffer_read_user(), dvb_ringbuffer_flush(),
76 * or dvb_ringbuffer_reset()
77 */
78 free = ACCESS_ONCE(rbuf->pread) - rbuf->pwrite;
68 if (free <= 0) 79 if (free <= 0)
69 free += rbuf->size; 80 free += rbuf->size;
70 return free-1; 81 return free-1;
@@ -76,7 +87,11 @@ ssize_t dvb_ringbuffer_avail(struct dvb_ringbuffer *rbuf)
76{ 87{
77 ssize_t avail; 88 ssize_t avail;
78 89
79 avail = rbuf->pwrite - rbuf->pread; 90 /* smp_load_acquire() to load write pointer on reader side
91 * this pairs with smp_store_release() in dvb_ringbuffer_write(),
92 * dvb_ringbuffer_write_user(), or dvb_ringbuffer_reset()
93 */
94 avail = smp_load_acquire(&rbuf->pwrite) - rbuf->pread;
80 if (avail < 0) 95 if (avail < 0)
81 avail += rbuf->size; 96 avail += rbuf->size;
82 return avail; 97 return avail;
@@ -86,14 +101,25 @@ ssize_t dvb_ringbuffer_avail(struct dvb_ringbuffer *rbuf)
86 101
87void dvb_ringbuffer_flush(struct dvb_ringbuffer *rbuf) 102void dvb_ringbuffer_flush(struct dvb_ringbuffer *rbuf)
88{ 103{
89 rbuf->pread = rbuf->pwrite; 104 /* dvb_ringbuffer_flush() counts as read operation
105 * smp_load_acquire() to load write pointer
106 * smp_store_release() to update read pointer, this ensures that the
107 * correct pointer is visible for subsequent dvb_ringbuffer_free()
108 * calls on other cpu cores
109 */
110 smp_store_release(&rbuf->pread, smp_load_acquire(&rbuf->pwrite));
90 rbuf->error = 0; 111 rbuf->error = 0;
91} 112}
92EXPORT_SYMBOL(dvb_ringbuffer_flush); 113EXPORT_SYMBOL(dvb_ringbuffer_flush);
93 114
94void dvb_ringbuffer_reset(struct dvb_ringbuffer *rbuf) 115void dvb_ringbuffer_reset(struct dvb_ringbuffer *rbuf)
95{ 116{
96 rbuf->pread = rbuf->pwrite = 0; 117 /* dvb_ringbuffer_reset() counts as read and write operation
118 * smp_store_release() to update read pointer
119 */
120 smp_store_release(&rbuf->pread, 0);
121 /* smp_store_release() to update write pointer */
122 smp_store_release(&rbuf->pwrite, 0);
97 rbuf->error = 0; 123 rbuf->error = 0;
98} 124}
99 125
@@ -119,12 +145,17 @@ ssize_t dvb_ringbuffer_read_user(struct dvb_ringbuffer *rbuf, u8 __user *buf, si
119 return -EFAULT; 145 return -EFAULT;
120 buf += split; 146 buf += split;
121 todo -= split; 147 todo -= split;
122 rbuf->pread = 0; 148 /* smp_store_release() for read pointer update to ensure
149 * that buf is not overwritten until read is complete,
150 * this pairs with ACCESS_ONCE() in dvb_ringbuffer_free()
151 */
152 smp_store_release(&rbuf->pread, 0);
123 } 153 }
124 if (copy_to_user(buf, rbuf->data+rbuf->pread, todo)) 154 if (copy_to_user(buf, rbuf->data+rbuf->pread, todo))
125 return -EFAULT; 155 return -EFAULT;
126 156
127 rbuf->pread = (rbuf->pread + todo) % rbuf->size; 157 /* smp_store_release() to update read pointer, see above */
158 smp_store_release(&rbuf->pread, (rbuf->pread + todo) % rbuf->size);
128 159
129 return len; 160 return len;
130} 161}
@@ -139,11 +170,16 @@ void dvb_ringbuffer_read(struct dvb_ringbuffer *rbuf, u8 *buf, size_t len)
139 memcpy(buf, rbuf->data+rbuf->pread, split); 170 memcpy(buf, rbuf->data+rbuf->pread, split);
140 buf += split; 171 buf += split;
141 todo -= split; 172 todo -= split;
142 rbuf->pread = 0; 173 /* smp_store_release() for read pointer update to ensure
174 * that buf is not overwritten until read is complete,
175 * this pairs with ACCESS_ONCE() in dvb_ringbuffer_free()
176 */
177 smp_store_release(&rbuf->pread, 0);
143 } 178 }
144 memcpy(buf, rbuf->data+rbuf->pread, todo); 179 memcpy(buf, rbuf->data+rbuf->pread, todo);
145 180
146 rbuf->pread = (rbuf->pread + todo) % rbuf->size; 181 /* smp_store_release() to update read pointer, see above */
182 smp_store_release(&rbuf->pread, (rbuf->pread + todo) % rbuf->size);
147} 183}
148 184
149 185
@@ -158,10 +194,16 @@ ssize_t dvb_ringbuffer_write(struct dvb_ringbuffer *rbuf, const u8 *buf, size_t
158 memcpy(rbuf->data+rbuf->pwrite, buf, split); 194 memcpy(rbuf->data+rbuf->pwrite, buf, split);
159 buf += split; 195 buf += split;
160 todo -= split; 196 todo -= split;
161 rbuf->pwrite = 0; 197 /* smp_store_release() for write pointer update to ensure that
198 * written data is visible on other cpu cores before the pointer
199 * update, this pairs with smp_load_acquire() in
200 * dvb_ringbuffer_empty() or dvb_ringbuffer_avail()
201 */
202 smp_store_release(&rbuf->pwrite, 0);
162 } 203 }
163 memcpy(rbuf->data+rbuf->pwrite, buf, todo); 204 memcpy(rbuf->data+rbuf->pwrite, buf, todo);
164 rbuf->pwrite = (rbuf->pwrite + todo) % rbuf->size; 205 /* smp_store_release() for write pointer update, see above */
206 smp_store_release(&rbuf->pwrite, (rbuf->pwrite + todo) % rbuf->size);
165 207
166 return len; 208 return len;
167} 209}
@@ -181,12 +223,18 @@ ssize_t dvb_ringbuffer_write_user(struct dvb_ringbuffer *rbuf,
181 return len - todo; 223 return len - todo;
182 buf += split; 224 buf += split;
183 todo -= split; 225 todo -= split;
184 rbuf->pwrite = 0; 226 /* smp_store_release() for write pointer update to ensure that
227 * written data is visible on other cpu cores before the pointer
228 * update, this pairs with smp_load_acquire() in
229 * dvb_ringbuffer_empty() or dvb_ringbuffer_avail()
230 */
231 smp_store_release(&rbuf->pwrite, 0);
185 } 232 }
186 status = copy_from_user(rbuf->data+rbuf->pwrite, buf, todo); 233 status = copy_from_user(rbuf->data+rbuf->pwrite, buf, todo);
187 if (status) 234 if (status)
188 return len - todo; 235 return len - todo;
189 rbuf->pwrite = (rbuf->pwrite + todo) % rbuf->size; 236 /* smp_store_release() for write pointer update, see above */
237 smp_store_release(&rbuf->pwrite, (rbuf->pwrite + todo) % rbuf->size);
190 238
191 return len; 239 return len;
192} 240}
diff --git a/drivers/media/dvb-frontends/Kconfig b/drivers/media/dvb-frontends/Kconfig
index a82f77c49bd5..c645aa81f423 100644
--- a/drivers/media/dvb-frontends/Kconfig
+++ b/drivers/media/dvb-frontends/Kconfig
@@ -73,6 +73,14 @@ config DVB_SI2165
73 73
74 Say Y when you want to support this frontend. 74 Say Y when you want to support this frontend.
75 75
76config DVB_MN88472
77 tristate "Panasonic MN88472"
78 depends on DVB_CORE && I2C
79 select REGMAP_I2C
80 default m if !MEDIA_SUBDRV_AUTOSELECT
81 help
82 Say Y when you want to support this frontend.
83
76config DVB_MN88473 84config DVB_MN88473
77 tristate "Panasonic MN88473" 85 tristate "Panasonic MN88473"
78 depends on DVB_CORE && I2C 86 depends on DVB_CORE && I2C
@@ -853,6 +861,13 @@ config DVB_ASCOT2E
853 help 861 help
854 Say Y when you want to support this frontend. 862 Say Y when you want to support this frontend.
855 863
864config DVB_HELENE
865 tristate "Sony HELENE Sat/Ter tuner (CXD2858ER)"
866 depends on DVB_CORE && I2C
867 default m if !MEDIA_SUBDRV_AUTOSELECT
868 help
869 Say Y when you want to support this frontend.
870
856comment "Tools to develop new frontends" 871comment "Tools to develop new frontends"
857 872
858config DVB_DUMMY_FE 873config DVB_DUMMY_FE
diff --git a/drivers/media/dvb-frontends/Makefile b/drivers/media/dvb-frontends/Makefile
index eb7191f4219d..e90165ad361b 100644
--- a/drivers/media/dvb-frontends/Makefile
+++ b/drivers/media/dvb-frontends/Makefile
@@ -95,6 +95,7 @@ obj-$(CONFIG_DVB_STV0900) += stv0900.o
95obj-$(CONFIG_DVB_STV090x) += stv090x.o 95obj-$(CONFIG_DVB_STV090x) += stv090x.o
96obj-$(CONFIG_DVB_STV6110x) += stv6110x.o 96obj-$(CONFIG_DVB_STV6110x) += stv6110x.o
97obj-$(CONFIG_DVB_M88DS3103) += m88ds3103.o 97obj-$(CONFIG_DVB_M88DS3103) += m88ds3103.o
98obj-$(CONFIG_DVB_MN88472) += mn88472.o
98obj-$(CONFIG_DVB_MN88473) += mn88473.o 99obj-$(CONFIG_DVB_MN88473) += mn88473.o
99obj-$(CONFIG_DVB_ISL6423) += isl6423.o 100obj-$(CONFIG_DVB_ISL6423) += isl6423.o
100obj-$(CONFIG_DVB_EC100) += ec100.o 101obj-$(CONFIG_DVB_EC100) += ec100.o
@@ -123,3 +124,4 @@ obj-$(CONFIG_DVB_AS102_FE) += as102_fe.o
123obj-$(CONFIG_DVB_TC90522) += tc90522.o 124obj-$(CONFIG_DVB_TC90522) += tc90522.o
124obj-$(CONFIG_DVB_HORUS3A) += horus3a.o 125obj-$(CONFIG_DVB_HORUS3A) += horus3a.o
125obj-$(CONFIG_DVB_ASCOT2E) += ascot2e.o 126obj-$(CONFIG_DVB_ASCOT2E) += ascot2e.o
127obj-$(CONFIG_DVB_HELENE) += helene.o
diff --git a/drivers/media/dvb-frontends/af9033.c b/drivers/media/dvb-frontends/af9033.c
index efebe5ce2429..9a8157a5f49d 100644
--- a/drivers/media/dvb-frontends/af9033.c
+++ b/drivers/media/dvb-frontends/af9033.c
@@ -41,7 +41,6 @@ struct af9033_dev {
41 u64 post_bit_count; 41 u64 post_bit_count;
42 u64 error_block_count; 42 u64 error_block_count;
43 u64 total_block_count; 43 u64 total_block_count;
44 struct delayed_work stat_work;
45}; 44};
46 45
47/* write multiple registers */ 46/* write multiple registers */
@@ -468,8 +467,6 @@ static int af9033_init(struct dvb_frontend *fe)
468 c->post_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE; 467 c->post_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
469 c->post_bit_error.len = 1; 468 c->post_bit_error.len = 1;
470 c->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE; 469 c->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
471 /* start statistics polling */
472 schedule_delayed_work(&dev->stat_work, msecs_to_jiffies(2000));
473 470
474 return 0; 471 return 0;
475 472
@@ -485,9 +482,6 @@ static int af9033_sleep(struct dvb_frontend *fe)
485 int ret, i; 482 int ret, i;
486 u8 tmp; 483 u8 tmp;
487 484
488 /* stop statistics polling */
489 cancel_delayed_work_sync(&dev->stat_work);
490
491 ret = af9033_wr_reg(dev, 0x80004c, 1); 485 ret = af9033_wr_reg(dev, 0x80004c, 1);
492 if (ret < 0) 486 if (ret < 0)
493 goto err; 487 goto err;
@@ -821,36 +815,39 @@ err:
821static int af9033_read_status(struct dvb_frontend *fe, enum fe_status *status) 815static int af9033_read_status(struct dvb_frontend *fe, enum fe_status *status)
822{ 816{
823 struct af9033_dev *dev = fe->demodulator_priv; 817 struct af9033_dev *dev = fe->demodulator_priv;
824 int ret; 818 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
825 u8 tmp; 819 int ret, i, tmp = 0;
820 u8 u8tmp, buf[7];
821
822 dev_dbg(&dev->client->dev, "\n");
826 823
827 *status = 0; 824 *status = 0;
828 825
829 /* radio channel status, 0=no result, 1=has signal, 2=no signal */ 826 /* radio channel status, 0=no result, 1=has signal, 2=no signal */
830 ret = af9033_rd_reg(dev, 0x800047, &tmp); 827 ret = af9033_rd_reg(dev, 0x800047, &u8tmp);
831 if (ret < 0) 828 if (ret < 0)
832 goto err; 829 goto err;
833 830
834 /* has signal */ 831 /* has signal */
835 if (tmp == 0x01) 832 if (u8tmp == 0x01)
836 *status |= FE_HAS_SIGNAL; 833 *status |= FE_HAS_SIGNAL;
837 834
838 if (tmp != 0x02) { 835 if (u8tmp != 0x02) {
839 /* TPS lock */ 836 /* TPS lock */
840 ret = af9033_rd_reg_mask(dev, 0x80f5a9, &tmp, 0x01); 837 ret = af9033_rd_reg_mask(dev, 0x80f5a9, &u8tmp, 0x01);
841 if (ret < 0) 838 if (ret < 0)
842 goto err; 839 goto err;
843 840
844 if (tmp) 841 if (u8tmp)
845 *status |= FE_HAS_SIGNAL | FE_HAS_CARRIER | 842 *status |= FE_HAS_SIGNAL | FE_HAS_CARRIER |
846 FE_HAS_VITERBI; 843 FE_HAS_VITERBI;
847 844
848 /* full lock */ 845 /* full lock */
849 ret = af9033_rd_reg_mask(dev, 0x80f999, &tmp, 0x01); 846 ret = af9033_rd_reg_mask(dev, 0x80f999, &u8tmp, 0x01);
850 if (ret < 0) 847 if (ret < 0)
851 goto err; 848 goto err;
852 849
853 if (tmp) 850 if (u8tmp)
854 *status |= FE_HAS_SIGNAL | FE_HAS_CARRIER | 851 *status |= FE_HAS_SIGNAL | FE_HAS_CARRIER |
855 FE_HAS_VITERBI | FE_HAS_SYNC | 852 FE_HAS_VITERBI | FE_HAS_SYNC |
856 FE_HAS_LOCK; 853 FE_HAS_LOCK;
@@ -858,6 +855,148 @@ static int af9033_read_status(struct dvb_frontend *fe, enum fe_status *status)
858 855
859 dev->fe_status = *status; 856 dev->fe_status = *status;
860 857
858 /* signal strength */
859 if (dev->fe_status & FE_HAS_SIGNAL) {
860 if (dev->is_af9035) {
861 ret = af9033_rd_reg(dev, 0x80004a, &u8tmp);
862 if (ret)
863 goto err;
864 tmp = -u8tmp * 1000;
865 } else {
866 ret = af9033_rd_reg(dev, 0x8000f7, &u8tmp);
867 if (ret)
868 goto err;
869 tmp = (u8tmp - 100) * 1000;
870 }
871
872 c->strength.len = 1;
873 c->strength.stat[0].scale = FE_SCALE_DECIBEL;
874 c->strength.stat[0].svalue = tmp;
875 } else {
876 c->strength.len = 1;
877 c->strength.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
878 }
879
880 /* CNR */
881 if (dev->fe_status & FE_HAS_VITERBI) {
882 u32 snr_val, snr_lut_size;
883 const struct val_snr *snr_lut = NULL;
884
885 /* read value */
886 ret = af9033_rd_regs(dev, 0x80002c, buf, 3);
887 if (ret)
888 goto err;
889
890 snr_val = (buf[2] << 16) | (buf[1] << 8) | (buf[0] << 0);
891
892 /* read superframe number */
893 ret = af9033_rd_reg(dev, 0x80f78b, &u8tmp);
894 if (ret)
895 goto err;
896
897 if (u8tmp)
898 snr_val /= u8tmp;
899
900 /* read current transmission mode */
901 ret = af9033_rd_reg(dev, 0x80f900, &u8tmp);
902 if (ret)
903 goto err;
904
905 switch ((u8tmp >> 0) & 3) {
906 case 0:
907 snr_val *= 4;
908 break;
909 case 1:
910 snr_val *= 1;
911 break;
912 case 2:
913 snr_val *= 2;
914 break;
915 default:
916 snr_val *= 0;
917 break;
918 }
919
920 /* read current modulation */
921 ret = af9033_rd_reg(dev, 0x80f903, &u8tmp);
922 if (ret)
923 goto err;
924
925 switch ((u8tmp >> 0) & 3) {
926 case 0:
927 snr_lut_size = ARRAY_SIZE(qpsk_snr_lut);
928 snr_lut = qpsk_snr_lut;
929 break;
930 case 1:
931 snr_lut_size = ARRAY_SIZE(qam16_snr_lut);
932 snr_lut = qam16_snr_lut;
933 break;
934 case 2:
935 snr_lut_size = ARRAY_SIZE(qam64_snr_lut);
936 snr_lut = qam64_snr_lut;
937 break;
938 default:
939 snr_lut_size = 0;
940 tmp = 0;
941 break;
942 }
943
944 for (i = 0; i < snr_lut_size; i++) {
945 tmp = snr_lut[i].snr * 1000;
946 if (snr_val < snr_lut[i].val)
947 break;
948 }
949
950 c->cnr.len = 1;
951 c->cnr.stat[0].scale = FE_SCALE_DECIBEL;
952 c->cnr.stat[0].svalue = tmp;
953 } else {
954 c->cnr.len = 1;
955 c->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
956 }
957
958 /* UCB/PER/BER */
959 if (dev->fe_status & FE_HAS_LOCK) {
960 /* outer FEC, 204 byte packets */
961 u16 abort_packet_count, rsd_packet_count;
962 /* inner FEC, bits */
963 u32 rsd_bit_err_count;
964
965 /*
966 * Packet count used for measurement is 10000
967 * (rsd_packet_count). Maybe it should be increased?
968 */
969
970 ret = af9033_rd_regs(dev, 0x800032, buf, 7);
971 if (ret)
972 goto err;
973
974 abort_packet_count = (buf[1] << 8) | (buf[0] << 0);
975 rsd_bit_err_count = (buf[4] << 16) | (buf[3] << 8) | buf[2];
976 rsd_packet_count = (buf[6] << 8) | (buf[5] << 0);
977
978 dev->error_block_count += abort_packet_count;
979 dev->total_block_count += rsd_packet_count;
980 dev->post_bit_error += rsd_bit_err_count;
981 dev->post_bit_count += rsd_packet_count * 204 * 8;
982
983 c->block_count.len = 1;
984 c->block_count.stat[0].scale = FE_SCALE_COUNTER;
985 c->block_count.stat[0].uvalue = dev->total_block_count;
986
987 c->block_error.len = 1;
988 c->block_error.stat[0].scale = FE_SCALE_COUNTER;
989 c->block_error.stat[0].uvalue = dev->error_block_count;
990
991 c->post_bit_count.len = 1;
992 c->post_bit_count.stat[0].scale = FE_SCALE_COUNTER;
993 c->post_bit_count.stat[0].uvalue = dev->post_bit_count;
994
995 c->post_bit_error.len = 1;
996 c->post_bit_error.stat[0].scale = FE_SCALE_COUNTER;
997 c->post_bit_error.stat[0].uvalue = dev->post_bit_error;
998 }
999
861 return 0; 1000 return 0;
862 1001
863err: 1002err:
@@ -1059,159 +1198,6 @@ err:
1059 return ret; 1198 return ret;
1060} 1199}
1061 1200
1062static void af9033_stat_work(struct work_struct *work)
1063{
1064 struct af9033_dev *dev = container_of(work, struct af9033_dev, stat_work.work);
1065 struct dtv_frontend_properties *c = &dev->fe.dtv_property_cache;
1066 int ret, tmp, i, len;
1067 u8 u8tmp, buf[7];
1068
1069 dev_dbg(&dev->client->dev, "\n");
1070
1071 /* signal strength */
1072 if (dev->fe_status & FE_HAS_SIGNAL) {
1073 if (dev->is_af9035) {
1074 ret = af9033_rd_reg(dev, 0x80004a, &u8tmp);
1075 tmp = -u8tmp * 1000;
1076 } else {
1077 ret = af9033_rd_reg(dev, 0x8000f7, &u8tmp);
1078 tmp = (u8tmp - 100) * 1000;
1079 }
1080 if (ret)
1081 goto err;
1082
1083 c->strength.len = 1;
1084 c->strength.stat[0].scale = FE_SCALE_DECIBEL;
1085 c->strength.stat[0].svalue = tmp;
1086 } else {
1087 c->strength.len = 1;
1088 c->strength.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
1089 }
1090
1091 /* CNR */
1092 if (dev->fe_status & FE_HAS_VITERBI) {
1093 u32 snr_val;
1094 const struct val_snr *snr_lut;
1095
1096 /* read value */
1097 ret = af9033_rd_regs(dev, 0x80002c, buf, 3);
1098 if (ret)
1099 goto err;
1100
1101 snr_val = (buf[2] << 16) | (buf[1] << 8) | (buf[0] << 0);
1102
1103 /* read superframe number */
1104 ret = af9033_rd_reg(dev, 0x80f78b, &u8tmp);
1105 if (ret)
1106 goto err;
1107
1108 if (u8tmp)
1109 snr_val /= u8tmp;
1110
1111 /* read current transmission mode */
1112 ret = af9033_rd_reg(dev, 0x80f900, &u8tmp);
1113 if (ret)
1114 goto err;
1115
1116 switch ((u8tmp >> 0) & 3) {
1117 case 0:
1118 snr_val *= 4;
1119 break;
1120 case 1:
1121 snr_val *= 1;
1122 break;
1123 case 2:
1124 snr_val *= 2;
1125 break;
1126 default:
1127 goto err_schedule_delayed_work;
1128 }
1129
1130 /* read current modulation */
1131 ret = af9033_rd_reg(dev, 0x80f903, &u8tmp);
1132 if (ret)
1133 goto err;
1134
1135 switch ((u8tmp >> 0) & 3) {
1136 case 0:
1137 len = ARRAY_SIZE(qpsk_snr_lut);
1138 snr_lut = qpsk_snr_lut;
1139 break;
1140 case 1:
1141 len = ARRAY_SIZE(qam16_snr_lut);
1142 snr_lut = qam16_snr_lut;
1143 break;
1144 case 2:
1145 len = ARRAY_SIZE(qam64_snr_lut);
1146 snr_lut = qam64_snr_lut;
1147 break;
1148 default:
1149 goto err_schedule_delayed_work;
1150 }
1151
1152 for (i = 0; i < len; i++) {
1153 tmp = snr_lut[i].snr * 1000;
1154 if (snr_val < snr_lut[i].val)
1155 break;
1156 }
1157
1158 c->cnr.len = 1;
1159 c->cnr.stat[0].scale = FE_SCALE_DECIBEL;
1160 c->cnr.stat[0].svalue = tmp;
1161 } else {
1162 c->cnr.len = 1;
1163 c->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
1164 }
1165
1166 /* UCB/PER/BER */
1167 if (dev->fe_status & FE_HAS_LOCK) {
1168 /* outer FEC, 204 byte packets */
1169 u16 abort_packet_count, rsd_packet_count;
1170 /* inner FEC, bits */
1171 u32 rsd_bit_err_count;
1172
1173 /*
1174 * Packet count used for measurement is 10000
1175 * (rsd_packet_count). Maybe it should be increased?
1176 */
1177
1178 ret = af9033_rd_regs(dev, 0x800032, buf, 7);
1179 if (ret)
1180 goto err;
1181
1182 abort_packet_count = (buf[1] << 8) | (buf[0] << 0);
1183 rsd_bit_err_count = (buf[4] << 16) | (buf[3] << 8) | buf[2];
1184 rsd_packet_count = (buf[6] << 8) | (buf[5] << 0);
1185
1186 dev->error_block_count += abort_packet_count;
1187 dev->total_block_count += rsd_packet_count;
1188 dev->post_bit_error += rsd_bit_err_count;
1189 dev->post_bit_count += rsd_packet_count * 204 * 8;
1190
1191 c->block_count.len = 1;
1192 c->block_count.stat[0].scale = FE_SCALE_COUNTER;
1193 c->block_count.stat[0].uvalue = dev->total_block_count;
1194
1195 c->block_error.len = 1;
1196 c->block_error.stat[0].scale = FE_SCALE_COUNTER;
1197 c->block_error.stat[0].uvalue = dev->error_block_count;
1198
1199 c->post_bit_count.len = 1;
1200 c->post_bit_count.stat[0].scale = FE_SCALE_COUNTER;
1201 c->post_bit_count.stat[0].uvalue = dev->post_bit_count;
1202
1203 c->post_bit_error.len = 1;
1204 c->post_bit_error.stat[0].scale = FE_SCALE_COUNTER;
1205 c->post_bit_error.stat[0].uvalue = dev->post_bit_error;
1206 }
1207
1208err_schedule_delayed_work:
1209 schedule_delayed_work(&dev->stat_work, msecs_to_jiffies(2000));
1210 return;
1211err:
1212 dev_dbg(&dev->client->dev, "failed=%d\n", ret);
1213}
1214
1215static struct dvb_frontend_ops af9033_ops = { 1201static struct dvb_frontend_ops af9033_ops = {
1216 .delsys = { SYS_DVBT }, 1202 .delsys = { SYS_DVBT },
1217 .info = { 1203 .info = {
@@ -1272,7 +1258,6 @@ static int af9033_probe(struct i2c_client *client,
1272 1258
1273 /* setup the state */ 1259 /* setup the state */
1274 dev->client = client; 1260 dev->client = client;
1275 INIT_DELAYED_WORK(&dev->stat_work, af9033_stat_work);
1276 memcpy(&dev->cfg, cfg, sizeof(struct af9033_config)); 1261 memcpy(&dev->cfg, cfg, sizeof(struct af9033_config));
1277 1262
1278 if (dev->cfg.clock != 12000000) { 1263 if (dev->cfg.clock != 12000000) {
@@ -1372,9 +1357,6 @@ static int af9033_remove(struct i2c_client *client)
1372 1357
1373 dev_dbg(&dev->client->dev, "\n"); 1358 dev_dbg(&dev->client->dev, "\n");
1374 1359
1375 /* stop statistics polling */
1376 cancel_delayed_work_sync(&dev->stat_work);
1377
1378 dev->fe.ops.release = NULL; 1360 dev->fe.ops.release = NULL;
1379 dev->fe.demodulator_priv = NULL; 1361 dev->fe.demodulator_priv = NULL;
1380 kfree(dev); 1362 kfree(dev);
@@ -1391,6 +1373,7 @@ MODULE_DEVICE_TABLE(i2c, af9033_id_table);
1391static struct i2c_driver af9033_driver = { 1373static struct i2c_driver af9033_driver = {
1392 .driver = { 1374 .driver = {
1393 .name = "af9033", 1375 .name = "af9033",
1376 .suppress_bind_attrs = true,
1394 }, 1377 },
1395 .probe = af9033_probe, 1378 .probe = af9033_probe,
1396 .remove = af9033_remove, 1379 .remove = af9033_remove,
diff --git a/drivers/media/dvb-frontends/ascot2e.c b/drivers/media/dvb-frontends/ascot2e.c
index f770f6a2c987..8cc8c4597b6a 100644
--- a/drivers/media/dvb-frontends/ascot2e.c
+++ b/drivers/media/dvb-frontends/ascot2e.c
@@ -132,7 +132,7 @@ static int ascot2e_write_regs(struct ascot2e_priv *priv,
132 } 132 }
133 }; 133 };
134 134
135 if (len + 1 >= sizeof(buf)) { 135 if (len + 1 > sizeof(buf)) {
136 dev_warn(&priv->i2c->dev,"wr reg=%04x: len=%d is too big!\n", 136 dev_warn(&priv->i2c->dev,"wr reg=%04x: len=%d is too big!\n",
137 reg, len + 1); 137 reg, len + 1);
138 return -E2BIG; 138 return -E2BIG;
diff --git a/drivers/media/dvb-frontends/cxd2841er.c b/drivers/media/dvb-frontends/cxd2841er.c
index 900186ba8e62..09c39346167f 100644
--- a/drivers/media/dvb-frontends/cxd2841er.c
+++ b/drivers/media/dvb-frontends/cxd2841er.c
@@ -1,7 +1,9 @@
1/* 1/*
2 * cxd2841er.c 2 * cxd2841er.c
3 * 3 *
4 * Sony CXD2441ER digital demodulator driver 4 * Sony digital demodulator driver for
5 * CXD2841ER - DVB-S/S2/T/T2/C/C2
6 * CXD2854ER - DVB-S/S2/T/T2/C/C2, ISDB-T/S
5 * 7 *
6 * Copyright 2012 Sony Corporation 8 * Copyright 2012 Sony Corporation
7 * Copyright (C) 2014 NetUP Inc. 9 * Copyright (C) 2014 NetUP Inc.
@@ -34,6 +36,16 @@
34#include "cxd2841er_priv.h" 36#include "cxd2841er_priv.h"
35 37
36#define MAX_WRITE_REGSIZE 16 38#define MAX_WRITE_REGSIZE 16
39#define LOG2_E_100X 144
40
41/* DVB-C constellation */
42enum sony_dvbc_constellation_t {
43 SONY_DVBC_CONSTELLATION_16QAM,
44 SONY_DVBC_CONSTELLATION_32QAM,
45 SONY_DVBC_CONSTELLATION_64QAM,
46 SONY_DVBC_CONSTELLATION_128QAM,
47 SONY_DVBC_CONSTELLATION_256QAM
48};
37 49
38enum cxd2841er_state { 50enum cxd2841er_state {
39 STATE_SHUTDOWN = 0, 51 STATE_SHUTDOWN = 0,
@@ -51,6 +63,8 @@ struct cxd2841er_priv {
51 const struct cxd2841er_config *config; 63 const struct cxd2841er_config *config;
52 enum cxd2841er_state state; 64 enum cxd2841er_state state;
53 u8 system; 65 u8 system;
66 enum cxd2841er_xtal xtal;
67 enum fe_caps caps;
54}; 68};
55 69
56static const struct cxd2841er_cnr_data s_cn_data[] = { 70static const struct cxd2841er_cnr_data s_cn_data[] = {
@@ -188,6 +202,9 @@ static const struct cxd2841er_cnr_data s2_cn_data[] = {
188}; 202};
189 203
190#define MAKE_IFFREQ_CONFIG(iffreq) ((u32)(((iffreq)/41.0)*16777216.0 + 0.5)) 204#define MAKE_IFFREQ_CONFIG(iffreq) ((u32)(((iffreq)/41.0)*16777216.0 + 0.5))
205#define MAKE_IFFREQ_CONFIG_XTAL(xtal, iffreq) ((xtal == SONY_XTAL_24000) ? \
206 (u32)(((iffreq)/48.0)*16777216.0 + 0.5) : \
207 (u32)(((iffreq)/41.0)*16777216.0 + 0.5))
191 208
192static void cxd2841er_i2c_debug(struct cxd2841er_priv *priv, 209static void cxd2841er_i2c_debug(struct cxd2841er_priv *priv,
193 u8 addr, u8 reg, u8 write, 210 u8 addr, u8 reg, u8 write,
@@ -217,7 +234,7 @@ static int cxd2841er_write_regs(struct cxd2841er_priv *priv,
217 }; 234 };
218 235
219 if (len + 1 >= sizeof(buf)) { 236 if (len + 1 >= sizeof(buf)) {
220 dev_warn(&priv->i2c->dev,"wr reg=%04x: len=%d is too big!\n", 237 dev_warn(&priv->i2c->dev, "wr reg=%04x: len=%d is too big!\n",
221 reg, len + 1); 238 reg, len + 1);
222 return -E2BIG; 239 return -E2BIG;
223 } 240 }
@@ -282,6 +299,7 @@ static int cxd2841er_read_regs(struct cxd2841er_priv *priv,
282 KBUILD_MODNAME, ret, i2c_addr, reg); 299 KBUILD_MODNAME, ret, i2c_addr, reg);
283 return ret; 300 return ret;
284 } 301 }
302 cxd2841er_i2c_debug(priv, i2c_addr, reg, 0, val, len);
285 return 0; 303 return 0;
286} 304}
287 305
@@ -427,6 +445,15 @@ static int cxd2841er_sleep_tc_to_active_t2_band(struct cxd2841er_priv *priv,
427static int cxd2841er_sleep_tc_to_active_c_band(struct cxd2841er_priv *priv, 445static int cxd2841er_sleep_tc_to_active_c_band(struct cxd2841er_priv *priv,
428 u32 bandwidth); 446 u32 bandwidth);
429 447
448static int cxd2841er_sleep_tc_to_active_i(struct cxd2841er_priv *priv,
449 u32 bandwidth);
450
451static int cxd2841er_active_i_to_sleep_tc(struct cxd2841er_priv *priv);
452
453static int cxd2841er_sleep_tc_to_shutdown(struct cxd2841er_priv *priv);
454
455static int cxd2841er_shutdown_to_sleep_tc(struct cxd2841er_priv *priv);
456
430static int cxd2841er_retune_active(struct cxd2841er_priv *priv, 457static int cxd2841er_retune_active(struct cxd2841er_priv *priv,
431 struct dtv_frontend_properties *p) 458 struct dtv_frontend_properties *p)
432{ 459{
@@ -454,7 +481,13 @@ static int cxd2841er_retune_active(struct cxd2841er_priv *priv,
454 priv, p->bandwidth_hz); 481 priv, p->bandwidth_hz);
455 case SYS_DVBC_ANNEX_A: 482 case SYS_DVBC_ANNEX_A:
456 return cxd2841er_sleep_tc_to_active_c_band( 483 return cxd2841er_sleep_tc_to_active_c_band(
457 priv, 8000000); 484 priv, p->bandwidth_hz);
485 case SYS_ISDBT:
486 cxd2841er_active_i_to_sleep_tc(priv);
487 cxd2841er_sleep_tc_to_shutdown(priv);
488 cxd2841er_shutdown_to_sleep_tc(priv);
489 return cxd2841er_sleep_tc_to_active_i(
490 priv, p->bandwidth_hz);
458 } 491 }
459 } 492 }
460 dev_dbg(&priv->i2c->dev, "%s(): invalid delivery system %d\n", 493 dev_dbg(&priv->i2c->dev, "%s(): invalid delivery system %d\n",
@@ -669,6 +702,45 @@ static int cxd2841er_active_c_to_sleep_tc(struct cxd2841er_priv *priv)
669 return 0; 702 return 0;
670} 703}
671 704
705static int cxd2841er_active_i_to_sleep_tc(struct cxd2841er_priv *priv)
706{
707 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
708 if (priv->state != STATE_ACTIVE_TC) {
709 dev_err(&priv->i2c->dev, "%s(): invalid state %d\n",
710 __func__, priv->state);
711 return -EINVAL;
712 }
713 /* Set SLV-T Bank : 0x00 */
714 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
715 /* disable TS output */
716 cxd2841er_write_reg(priv, I2C_SLVT, 0xc3, 0x01);
717 /* enable Hi-Z setting 1 */
718 cxd2841er_write_reg(priv, I2C_SLVT, 0x80, 0x3f);
719 /* enable Hi-Z setting 2 */
720 cxd2841er_write_reg(priv, I2C_SLVT, 0x81, 0xff);
721
722 /* TODO: Cancel demod parameter */
723
724 /* Set SLV-X Bank : 0x00 */
725 cxd2841er_write_reg(priv, I2C_SLVX, 0x00, 0x00);
726 /* disable ADC 1 */
727 cxd2841er_write_reg(priv, I2C_SLVX, 0x18, 0x01);
728 /* Set SLV-T Bank : 0x00 */
729 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
730 /* Disable ADC 2 */
731 cxd2841er_write_reg(priv, I2C_SLVT, 0x43, 0x0a);
732 /* Disable ADC 3 */
733 cxd2841er_write_reg(priv, I2C_SLVT, 0x41, 0x0a);
734 /* Disable ADC clock */
735 cxd2841er_write_reg(priv, I2C_SLVT, 0x30, 0x00);
736 /* Disable RF level monitor */
737 cxd2841er_write_reg(priv, I2C_SLVT, 0x2f, 0x00);
738 /* Disable demod clock */
739 cxd2841er_write_reg(priv, I2C_SLVT, 0x2c, 0x00);
740 priv->state = STATE_SLEEP_TC;
741 return 0;
742}
743
672static int cxd2841er_shutdown_to_sleep_s(struct cxd2841er_priv *priv) 744static int cxd2841er_shutdown_to_sleep_s(struct cxd2841er_priv *priv)
673{ 745{
674 dev_dbg(&priv->i2c->dev, "%s()\n", __func__); 746 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
@@ -686,8 +758,25 @@ static int cxd2841er_shutdown_to_sleep_s(struct cxd2841er_priv *priv)
686 cxd2841er_write_reg(priv, I2C_SLVX, 0x00, 0x00); 758 cxd2841er_write_reg(priv, I2C_SLVX, 0x00, 0x00);
687 /* Set demod SW reset */ 759 /* Set demod SW reset */
688 cxd2841er_write_reg(priv, I2C_SLVX, 0x10, 0x01); 760 cxd2841er_write_reg(priv, I2C_SLVX, 0x10, 0x01);
689 /* Set X'tal clock to 20.5Mhz */ 761
690 cxd2841er_write_reg(priv, I2C_SLVX, 0x14, 0x00); 762 switch (priv->xtal) {
763 case SONY_XTAL_20500:
764 cxd2841er_write_reg(priv, I2C_SLVX, 0x14, 0x00);
765 break;
766 case SONY_XTAL_24000:
767 /* Select demod frequency */
768 cxd2841er_write_reg(priv, I2C_SLVX, 0x12, 0x00);
769 cxd2841er_write_reg(priv, I2C_SLVX, 0x14, 0x03);
770 break;
771 case SONY_XTAL_41000:
772 cxd2841er_write_reg(priv, I2C_SLVX, 0x14, 0x01);
773 break;
774 default:
775 dev_dbg(&priv->i2c->dev, "%s(): invalid demod xtal %d\n",
776 __func__, priv->xtal);
777 return -EINVAL;
778 }
779
691 /* Set demod mode */ 780 /* Set demod mode */
692 cxd2841er_write_reg(priv, I2C_SLVX, 0x17, 0x0a); 781 cxd2841er_write_reg(priv, I2C_SLVX, 0x17, 0x0a);
693 /* Clear demod SW reset */ 782 /* Clear demod SW reset */
@@ -712,6 +801,8 @@ static int cxd2841er_shutdown_to_sleep_s(struct cxd2841er_priv *priv)
712 801
713static int cxd2841er_shutdown_to_sleep_tc(struct cxd2841er_priv *priv) 802static int cxd2841er_shutdown_to_sleep_tc(struct cxd2841er_priv *priv)
714{ 803{
804 u8 data = 0;
805
715 dev_dbg(&priv->i2c->dev, "%s()\n", __func__); 806 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
716 if (priv->state != STATE_SHUTDOWN) { 807 if (priv->state != STATE_SHUTDOWN) {
717 dev_dbg(&priv->i2c->dev, "%s(): invalid demod state %d\n", 808 dev_dbg(&priv->i2c->dev, "%s(): invalid demod state %d\n",
@@ -727,9 +818,24 @@ static int cxd2841er_shutdown_to_sleep_tc(struct cxd2841er_priv *priv)
727 cxd2841er_write_reg(priv, I2C_SLVX, 0x00, 0x00); 818 cxd2841er_write_reg(priv, I2C_SLVX, 0x00, 0x00);
728 /* Set demod SW reset */ 819 /* Set demod SW reset */
729 cxd2841er_write_reg(priv, I2C_SLVX, 0x10, 0x01); 820 cxd2841er_write_reg(priv, I2C_SLVX, 0x10, 0x01);
730 /* Set X'tal clock to 20.5Mhz */ 821 /* Select ADC clock mode */
731 cxd2841er_write_reg(priv, I2C_SLVX, 0x13, 0x00); 822 cxd2841er_write_reg(priv, I2C_SLVX, 0x13, 0x00);
732 cxd2841er_write_reg(priv, I2C_SLVX, 0x14, 0x00); 823
824 switch (priv->xtal) {
825 case SONY_XTAL_20500:
826 data = 0x0;
827 break;
828 case SONY_XTAL_24000:
829 /* Select demod frequency */
830 cxd2841er_write_reg(priv, I2C_SLVX, 0x12, 0x00);
831 data = 0x3;
832 break;
833 case SONY_XTAL_41000:
834 cxd2841er_write_reg(priv, I2C_SLVX, 0x12, 0x00);
835 data = 0x1;
836 break;
837 }
838 cxd2841er_write_reg(priv, I2C_SLVX, 0x14, data);
733 /* Clear demod SW reset */ 839 /* Clear demod SW reset */
734 cxd2841er_write_reg(priv, I2C_SLVX, 0x10, 0x00); 840 cxd2841er_write_reg(priv, I2C_SLVX, 0x10, 0x00);
735 usleep_range(1000, 2000); 841 usleep_range(1000, 2000);
@@ -809,11 +915,14 @@ static void cxd2841er_set_ts_clock_mode(struct cxd2841er_priv *priv,
809 915
810static u8 cxd2841er_chip_id(struct cxd2841er_priv *priv) 916static u8 cxd2841er_chip_id(struct cxd2841er_priv *priv)
811{ 917{
812 u8 chip_id; 918 u8 chip_id = 0;
813 919
814 dev_dbg(&priv->i2c->dev, "%s()\n", __func__); 920 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
815 cxd2841er_write_reg(priv, I2C_SLVT, 0, 0); 921 if (cxd2841er_write_reg(priv, I2C_SLVT, 0, 0) == 0)
816 cxd2841er_read_reg(priv, I2C_SLVT, 0xfd, &chip_id); 922 cxd2841er_read_reg(priv, I2C_SLVT, 0xfd, &chip_id);
923 else if (cxd2841er_write_reg(priv, I2C_SLVX, 0, 0) == 0)
924 cxd2841er_read_reg(priv, I2C_SLVX, 0xfd, &chip_id);
925
817 return chip_id; 926 return chip_id;
818} 927}
819 928
@@ -896,6 +1005,25 @@ static int cxd2841er_read_status_c(struct cxd2841er_priv *priv, u8 *tslock)
896 return 0; 1005 return 0;
897} 1006}
898 1007
1008static int cxd2841er_read_status_i(struct cxd2841er_priv *priv,
1009 u8 *sync, u8 *tslock, u8 *unlock)
1010{
1011 u8 data = 0;
1012
1013 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
1014 if (priv->state != STATE_ACTIVE_TC)
1015 return -EINVAL;
1016 /* Set SLV-T Bank : 0x60 */
1017 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x60);
1018 cxd2841er_read_reg(priv, I2C_SLVT, 0x10, &data);
1019 dev_dbg(&priv->i2c->dev,
1020 "%s(): lock=0x%x\n", __func__, data);
1021 *sync = ((data & 0x02) ? 1 : 0);
1022 *tslock = ((data & 0x01) ? 1 : 0);
1023 *unlock = ((data & 0x10) ? 1 : 0);
1024 return 0;
1025}
1026
899static int cxd2841er_read_status_tc(struct dvb_frontend *fe, 1027static int cxd2841er_read_status_tc(struct dvb_frontend *fe,
900 enum fe_status *status) 1028 enum fe_status *status)
901{ 1029{
@@ -921,6 +1049,20 @@ static int cxd2841er_read_status_tc(struct dvb_frontend *fe,
921 FE_HAS_SYNC; 1049 FE_HAS_SYNC;
922 if (tslock) 1050 if (tslock)
923 *status |= FE_HAS_LOCK; 1051 *status |= FE_HAS_LOCK;
1052 } else if (priv->system == SYS_ISDBT) {
1053 ret = cxd2841er_read_status_i(
1054 priv, &sync, &tslock, &unlock);
1055 if (ret)
1056 goto done;
1057 if (unlock)
1058 goto done;
1059 if (sync)
1060 *status = FE_HAS_SIGNAL |
1061 FE_HAS_CARRIER |
1062 FE_HAS_VITERBI |
1063 FE_HAS_SYNC;
1064 if (tslock)
1065 *status |= FE_HAS_LOCK;
924 } else if (priv->system == SYS_DVBC_ANNEX_A) { 1066 } else if (priv->system == SYS_DVBC_ANNEX_A) {
925 ret = cxd2841er_read_status_c(priv, &tslock); 1067 ret = cxd2841er_read_status_c(priv, &tslock);
926 if (ret) 1068 if (ret)
@@ -997,6 +1139,76 @@ static int cxd2841er_get_carrier_offset_s_s2(struct cxd2841er_priv *priv,
997 return 0; 1139 return 0;
998} 1140}
999 1141
1142static int cxd2841er_get_carrier_offset_i(struct cxd2841er_priv *priv,
1143 u32 bandwidth, int *offset)
1144{
1145 u8 data[4];
1146
1147 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
1148 if (priv->state != STATE_ACTIVE_TC) {
1149 dev_dbg(&priv->i2c->dev, "%s(): invalid state %d\n",
1150 __func__, priv->state);
1151 return -EINVAL;
1152 }
1153 if (priv->system != SYS_ISDBT) {
1154 dev_dbg(&priv->i2c->dev, "%s(): invalid delivery system %d\n",
1155 __func__, priv->system);
1156 return -EINVAL;
1157 }
1158 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x60);
1159 cxd2841er_read_regs(priv, I2C_SLVT, 0x4c, data, sizeof(data));
1160 *offset = -1 * sign_extend32(
1161 ((u32)(data[0] & 0x1F) << 24) | ((u32)data[1] << 16) |
1162 ((u32)data[2] << 8) | (u32)data[3], 29);
1163
1164 switch (bandwidth) {
1165 case 6000000:
1166 *offset = -1 * ((*offset) * 8/264);
1167 break;
1168 case 7000000:
1169 *offset = -1 * ((*offset) * 8/231);
1170 break;
1171 case 8000000:
1172 *offset = -1 * ((*offset) * 8/198);
1173 break;
1174 default:
1175 dev_dbg(&priv->i2c->dev, "%s(): invalid bandwidth %d\n",
1176 __func__, bandwidth);
1177 return -EINVAL;
1178 }
1179
1180 dev_dbg(&priv->i2c->dev, "%s(): bandwidth %d offset %d\n",
1181 __func__, bandwidth, *offset);
1182
1183 return 0;
1184}
1185
1186static int cxd2841er_get_carrier_offset_t(struct cxd2841er_priv *priv,
1187 u32 bandwidth, int *offset)
1188{
1189 u8 data[4];
1190
1191 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
1192 if (priv->state != STATE_ACTIVE_TC) {
1193 dev_dbg(&priv->i2c->dev, "%s(): invalid state %d\n",
1194 __func__, priv->state);
1195 return -EINVAL;
1196 }
1197 if (priv->system != SYS_DVBT) {
1198 dev_dbg(&priv->i2c->dev, "%s(): invalid delivery system %d\n",
1199 __func__, priv->system);
1200 return -EINVAL;
1201 }
1202 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
1203 cxd2841er_read_regs(priv, I2C_SLVT, 0x4c, data, sizeof(data));
1204 *offset = -1 * sign_extend32(
1205 ((u32)(data[0] & 0x1F) << 24) | ((u32)data[1] << 16) |
1206 ((u32)data[2] << 8) | (u32)data[3], 29);
1207 *offset *= (bandwidth / 1000000);
1208 *offset /= 235;
1209 return 0;
1210}
1211
1000static int cxd2841er_get_carrier_offset_t2(struct cxd2841er_priv *priv, 1212static int cxd2841er_get_carrier_offset_t2(struct cxd2841er_priv *priv,
1001 u32 bandwidth, int *offset) 1213 u32 bandwidth, int *offset)
1002{ 1214{
@@ -1060,6 +1272,24 @@ static int cxd2841er_get_carrier_offset_c(struct cxd2841er_priv *priv,
1060 return 0; 1272 return 0;
1061} 1273}
1062 1274
1275static int cxd2841er_read_packet_errors_c(
1276 struct cxd2841er_priv *priv, u32 *penum)
1277{
1278 u8 data[3];
1279
1280 *penum = 0;
1281 if (priv->state != STATE_ACTIVE_TC) {
1282 dev_dbg(&priv->i2c->dev, "%s(): invalid state %d\n",
1283 __func__, priv->state);
1284 return -EINVAL;
1285 }
1286 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x40);
1287 cxd2841er_read_regs(priv, I2C_SLVT, 0xea, data, sizeof(data));
1288 if (data[2] & 0x01)
1289 *penum = ((u32)data[0] << 8) | (u32)data[1];
1290 return 0;
1291}
1292
1063static int cxd2841er_read_packet_errors_t( 1293static int cxd2841er_read_packet_errors_t(
1064 struct cxd2841er_priv *priv, u32 *penum) 1294 struct cxd2841er_priv *priv, u32 *penum)
1065{ 1295{
@@ -1096,11 +1326,85 @@ static int cxd2841er_read_packet_errors_t2(
1096 return 0; 1326 return 0;
1097} 1327}
1098 1328
1099static u32 cxd2841er_mon_read_ber_s(struct cxd2841er_priv *priv) 1329static int cxd2841er_read_packet_errors_i(
1330 struct cxd2841er_priv *priv, u32 *penum)
1331{
1332 u8 data[2];
1333
1334 *penum = 0;
1335 if (priv->state != STATE_ACTIVE_TC) {
1336 dev_dbg(&priv->i2c->dev, "%s(): invalid state %d\n",
1337 __func__, priv->state);
1338 return -EINVAL;
1339 }
1340 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x60);
1341 cxd2841er_read_regs(priv, I2C_SLVT, 0xA1, data, 1);
1342
1343 if (!(data[0] & 0x01))
1344 return 0;
1345
1346 /* Layer A */
1347 cxd2841er_read_regs(priv, I2C_SLVT, 0xA2, data, sizeof(data));
1348 *penum = ((u32)data[0] << 8) | (u32)data[1];
1349
1350 /* Layer B */
1351 cxd2841er_read_regs(priv, I2C_SLVT, 0xA4, data, sizeof(data));
1352 *penum += ((u32)data[0] << 8) | (u32)data[1];
1353
1354 /* Layer C */
1355 cxd2841er_read_regs(priv, I2C_SLVT, 0xA6, data, sizeof(data));
1356 *penum += ((u32)data[0] << 8) | (u32)data[1];
1357
1358 return 0;
1359}
1360
1361static int cxd2841er_read_ber_c(struct cxd2841er_priv *priv,
1362 u32 *bit_error, u32 *bit_count)
1363{
1364 u8 data[3];
1365 u32 bit_err, period_exp;
1366
1367 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
1368 if (priv->state != STATE_ACTIVE_TC) {
1369 dev_dbg(&priv->i2c->dev, "%s(): invalid state %d\n",
1370 __func__, priv->state);
1371 return -EINVAL;
1372 }
1373 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x40);
1374 cxd2841er_read_regs(priv, I2C_SLVT, 0x62, data, sizeof(data));
1375 if (!(data[0] & 0x80)) {
1376 dev_dbg(&priv->i2c->dev,
1377 "%s(): no valid BER data\n", __func__);
1378 return -EINVAL;
1379 }
1380 bit_err = ((u32)(data[0] & 0x3f) << 16) |
1381 ((u32)data[1] << 8) |
1382 (u32)data[2];
1383 cxd2841er_read_reg(priv, I2C_SLVT, 0x60, data);
1384 period_exp = data[0] & 0x1f;
1385
1386 if ((period_exp <= 11) && (bit_err > (1 << period_exp) * 204 * 8)) {
1387 dev_dbg(&priv->i2c->dev,
1388 "%s(): period_exp(%u) or bit_err(%u) not in range. no valid BER data\n",
1389 __func__, period_exp, bit_err);
1390 return -EINVAL;
1391 }
1392
1393 dev_dbg(&priv->i2c->dev,
1394 "%s(): period_exp(%u) or bit_err(%u) count=%d\n",
1395 __func__, period_exp, bit_err,
1396 ((1 << period_exp) * 204 * 8));
1397
1398 *bit_error = bit_err;
1399 *bit_count = ((1 << period_exp) * 204 * 8);
1400
1401 return 0;
1402}
1403
1404static int cxd2841er_mon_read_ber_s(struct cxd2841er_priv *priv,
1405 u32 *bit_error, u32 *bit_count)
1100{ 1406{
1101 u8 data[11]; 1407 u8 data[11];
1102 u32 bit_error, bit_count;
1103 u32 temp_q, temp_r;
1104 1408
1105 /* Set SLV-T Bank : 0xA0 */ 1409 /* Set SLV-T Bank : 0xA0 */
1106 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0xa0); 1410 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0xa0);
@@ -1116,40 +1420,30 @@ static u32 cxd2841er_mon_read_ber_s(struct cxd2841er_priv *priv)
1116 */ 1420 */
1117 cxd2841er_read_regs(priv, I2C_SLVT, 0x35, data, 11); 1421 cxd2841er_read_regs(priv, I2C_SLVT, 0x35, data, 11);
1118 if (data[0] & 0x01) { 1422 if (data[0] & 0x01) {
1119 bit_error = ((u32)(data[1] & 0x3F) << 16) | 1423 *bit_error = ((u32)(data[1] & 0x3F) << 16) |
1120 ((u32)(data[2] & 0xFF) << 8) | 1424 ((u32)(data[2] & 0xFF) << 8) |
1121 (u32)(data[3] & 0xFF); 1425 (u32)(data[3] & 0xFF);
1122 bit_count = ((u32)(data[8] & 0x3F) << 16) | 1426 *bit_count = ((u32)(data[8] & 0x3F) << 16) |
1123 ((u32)(data[9] & 0xFF) << 8) | 1427 ((u32)(data[9] & 0xFF) << 8) |
1124 (u32)(data[10] & 0xFF); 1428 (u32)(data[10] & 0xFF);
1125 /* 1429 if ((*bit_count == 0) || (*bit_error > *bit_count)) {
1126 * BER = bitError / bitCount
1127 * = (bitError * 10^7) / bitCount
1128 * = ((bitError * 625 * 125 * 128) / bitCount
1129 */
1130 if ((bit_count == 0) || (bit_error > bit_count)) {
1131 dev_dbg(&priv->i2c->dev, 1430 dev_dbg(&priv->i2c->dev,
1132 "%s(): invalid bit_error %d, bit_count %d\n", 1431 "%s(): invalid bit_error %d, bit_count %d\n",
1133 __func__, bit_error, bit_count); 1432 __func__, *bit_error, *bit_count);
1134 return 0; 1433 return -EINVAL;
1135 } 1434 }
1136 temp_q = div_u64_rem(10000000ULL * bit_error, 1435 return 0;
1137 bit_count, &temp_r);
1138 if (bit_count != 1 && temp_r >= bit_count / 2)
1139 temp_q++;
1140 return temp_q;
1141 } 1436 }
1142 dev_dbg(&priv->i2c->dev, "%s(): no data available\n", __func__); 1437 dev_dbg(&priv->i2c->dev, "%s(): no data available\n", __func__);
1143 return 0; 1438 return -EINVAL;
1144} 1439}
1145 1440
1146 1441
1147static u32 cxd2841er_mon_read_ber_s2(struct cxd2841er_priv *priv) 1442static int cxd2841er_mon_read_ber_s2(struct cxd2841er_priv *priv,
1443 u32 *bit_error, u32 *bit_count)
1148{ 1444{
1149 u8 data[5]; 1445 u8 data[5];
1150 u32 bit_error, period; 1446 u32 period;
1151 u32 temp_q, temp_r;
1152 u32 result = 0;
1153 1447
1154 /* Set SLV-T Bank : 0xB2 */ 1448 /* Set SLV-T Bank : 0xB2 */
1155 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0xb2); 1449 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0xb2);
@@ -1164,10 +1458,10 @@ static u32 cxd2841er_mon_read_ber_s2(struct cxd2841er_priv *priv)
1164 cxd2841er_read_regs(priv, I2C_SLVT, 0x30, data, 5); 1458 cxd2841er_read_regs(priv, I2C_SLVT, 0x30, data, 5);
1165 if (data[0] & 0x01) { 1459 if (data[0] & 0x01) {
1166 /* Bit error count */ 1460 /* Bit error count */
1167 bit_error = ((u32)(data[1] & 0x0F) << 24) | 1461 *bit_error = ((u32)(data[1] & 0x0F) << 24) |
1168 ((u32)(data[2] & 0xFF) << 16) | 1462 ((u32)(data[2] & 0xFF) << 16) |
1169 ((u32)(data[3] & 0xFF) << 8) | 1463 ((u32)(data[3] & 0xFF) << 8) |
1170 (u32)(data[4] & 0xFF); 1464 (u32)(data[4] & 0xFF);
1171 1465
1172 /* Set SLV-T Bank : 0xA0 */ 1466 /* Set SLV-T Bank : 0xA0 */
1173 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0xa0); 1467 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0xa0);
@@ -1177,40 +1471,30 @@ static u32 cxd2841er_mon_read_ber_s2(struct cxd2841er_priv *priv)
1177 if (period == 0) { 1471 if (period == 0) {
1178 dev_dbg(&priv->i2c->dev, 1472 dev_dbg(&priv->i2c->dev,
1179 "%s(): period is 0\n", __func__); 1473 "%s(): period is 0\n", __func__);
1180 return 0; 1474 return -EINVAL;
1181 } 1475 }
1182 if (bit_error > (period * 64800)) { 1476 if (*bit_error > (period * 64800)) {
1183 dev_dbg(&priv->i2c->dev, 1477 dev_dbg(&priv->i2c->dev,
1184 "%s(): invalid bit_err 0x%x period 0x%x\n", 1478 "%s(): invalid bit_err 0x%x period 0x%x\n",
1185 __func__, bit_error, period); 1479 __func__, *bit_error, period);
1186 return 0; 1480 return -EINVAL;
1187 } 1481 }
1188 /* 1482 *bit_count = period * 64800;
1189 * BER = bitError / (period * 64800) 1483
1190 * = (bitError * 10^7) / (period * 64800) 1484 return 0;
1191 * = (bitError * 10^5) / (period * 648)
1192 * = (bitError * 12500) / (period * 81)
1193 * = (bitError * 10) * 1250 / (period * 81)
1194 */
1195 temp_q = div_u64_rem(12500ULL * bit_error,
1196 period * 81, &temp_r);
1197 if (temp_r >= period * 40)
1198 temp_q++;
1199 result = temp_q;
1200 } else { 1485 } else {
1201 dev_dbg(&priv->i2c->dev, 1486 dev_dbg(&priv->i2c->dev,
1202 "%s(): no data available\n", __func__); 1487 "%s(): no data available\n", __func__);
1203 } 1488 }
1204 return result; 1489 return -EINVAL;
1205} 1490}
1206 1491
1207static int cxd2841er_read_ber_t2(struct cxd2841er_priv *priv, u32 *ber) 1492static int cxd2841er_read_ber_t2(struct cxd2841er_priv *priv,
1493 u32 *bit_error, u32 *bit_count)
1208{ 1494{
1209 u8 data[4]; 1495 u8 data[4];
1210 u32 div, q, r; 1496 u32 period_exp, n_ldpc;
1211 u32 bit_err, period_exp, n_ldpc;
1212 1497
1213 *ber = 0;
1214 if (priv->state != STATE_ACTIVE_TC) { 1498 if (priv->state != STATE_ACTIVE_TC) {
1215 dev_dbg(&priv->i2c->dev, 1499 dev_dbg(&priv->i2c->dev,
1216 "%s(): invalid state %d\n", __func__, priv->state); 1500 "%s(): invalid state %d\n", __func__, priv->state);
@@ -1221,40 +1505,44 @@ static int cxd2841er_read_ber_t2(struct cxd2841er_priv *priv, u32 *ber)
1221 if (!(data[0] & 0x10)) { 1505 if (!(data[0] & 0x10)) {
1222 dev_dbg(&priv->i2c->dev, 1506 dev_dbg(&priv->i2c->dev,
1223 "%s(): no valid BER data\n", __func__); 1507 "%s(): no valid BER data\n", __func__);
1224 return 0; 1508 return -EINVAL;
1225 } 1509 }
1226 bit_err = ((u32)(data[0] & 0x0f) << 24) | 1510 *bit_error = ((u32)(data[0] & 0x0f) << 24) |
1227 ((u32)data[1] << 16) | 1511 ((u32)data[1] << 16) |
1228 ((u32)data[2] << 8) | 1512 ((u32)data[2] << 8) |
1229 (u32)data[3]; 1513 (u32)data[3];
1230 cxd2841er_read_reg(priv, I2C_SLVT, 0x6f, data); 1514 cxd2841er_read_reg(priv, I2C_SLVT, 0x6f, data);
1231 period_exp = data[0] & 0x0f; 1515 period_exp = data[0] & 0x0f;
1232 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x22); 1516 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x22);
1233 cxd2841er_read_reg(priv, I2C_SLVT, 0x5e, data); 1517 cxd2841er_read_reg(priv, I2C_SLVT, 0x5e, data);
1234 n_ldpc = ((data[0] & 0x03) == 0 ? 16200 : 64800); 1518 n_ldpc = ((data[0] & 0x03) == 0 ? 16200 : 64800);
1235 if (bit_err > ((1U << period_exp) * n_ldpc)) { 1519 if (*bit_error > ((1U << period_exp) * n_ldpc)) {
1236 dev_dbg(&priv->i2c->dev, 1520 dev_dbg(&priv->i2c->dev,
1237 "%s(): invalid BER value\n", __func__); 1521 "%s(): invalid BER value\n", __func__);
1238 return -EINVAL; 1522 return -EINVAL;
1239 } 1523 }
1524
1525 /*
1526 * FIXME: the right thing would be to return bit_error untouched,
1527 * but, as we don't know the scale returned by the counters, let's
1528 * at least preserver BER = bit_error/bit_count.
1529 */
1240 if (period_exp >= 4) { 1530 if (period_exp >= 4) {
1241 div = (1U << (period_exp - 4)) * (n_ldpc / 200); 1531 *bit_count = (1U << (period_exp - 4)) * (n_ldpc / 200);
1242 q = div_u64_rem(3125ULL * bit_err, div, &r); 1532 *bit_error *= 3125ULL;
1243 } else { 1533 } else {
1244 div = (1U << period_exp) * (n_ldpc / 200); 1534 *bit_count = (1U << period_exp) * (n_ldpc / 200);
1245 q = div_u64_rem(50000ULL * bit_err, div, &r); 1535 *bit_error *= 50000ULL;
1246 } 1536 }
1247 *ber = (r >= div / 2) ? q + 1 : q;
1248 return 0; 1537 return 0;
1249} 1538}
1250 1539
1251static int cxd2841er_read_ber_t(struct cxd2841er_priv *priv, u32 *ber) 1540static int cxd2841er_read_ber_t(struct cxd2841er_priv *priv,
1541 u32 *bit_error, u32 *bit_count)
1252{ 1542{
1253 u8 data[2]; 1543 u8 data[2];
1254 u32 div, q, r; 1544 u32 period;
1255 u32 bit_err, period;
1256 1545
1257 *ber = 0;
1258 if (priv->state != STATE_ACTIVE_TC) { 1546 if (priv->state != STATE_ACTIVE_TC) {
1259 dev_dbg(&priv->i2c->dev, 1547 dev_dbg(&priv->i2c->dev,
1260 "%s(): invalid state %d\n", __func__, priv->state); 1548 "%s(): invalid state %d\n", __func__, priv->state);
@@ -1268,16 +1556,22 @@ static int cxd2841er_read_ber_t(struct cxd2841er_priv *priv, u32 *ber)
1268 return 0; 1556 return 0;
1269 } 1557 }
1270 cxd2841er_read_regs(priv, I2C_SLVT, 0x22, data, sizeof(data)); 1558 cxd2841er_read_regs(priv, I2C_SLVT, 0x22, data, sizeof(data));
1271 bit_err = ((u32)data[0] << 8) | (u32)data[1]; 1559 *bit_error = ((u32)data[0] << 8) | (u32)data[1];
1272 cxd2841er_read_reg(priv, I2C_SLVT, 0x6f, data); 1560 cxd2841er_read_reg(priv, I2C_SLVT, 0x6f, data);
1273 period = ((data[0] & 0x07) == 0) ? 256 : (4096 << (data[0] & 0x07)); 1561 period = ((data[0] & 0x07) == 0) ? 256 : (4096 << (data[0] & 0x07));
1274 div = period / 128; 1562
1275 q = div_u64_rem(78125ULL * bit_err, div, &r); 1563 /*
1276 *ber = (r >= div / 2) ? q + 1 : q; 1564 * FIXME: the right thing would be to return bit_error untouched,
1565 * but, as we don't know the scale returned by the counters, let's
1566 * at least preserver BER = bit_error/bit_count.
1567 */
1568 *bit_count = period / 128;
1569 *bit_error *= 78125ULL;
1277 return 0; 1570 return 0;
1278} 1571}
1279 1572
1280static u32 cxd2841er_dvbs_read_snr(struct cxd2841er_priv *priv, u8 delsys) 1573static u32 cxd2841er_dvbs_read_snr(struct cxd2841er_priv *priv,
1574 u8 delsys, u32 *snr)
1281{ 1575{
1282 u8 data[3]; 1576 u8 data[3];
1283 u32 res = 0, value; 1577 u32 res = 0, value;
@@ -1335,9 +1629,71 @@ static u32 cxd2841er_dvbs_read_snr(struct cxd2841er_priv *priv, u8 delsys)
1335 } else { 1629 } else {
1336 dev_dbg(&priv->i2c->dev, 1630 dev_dbg(&priv->i2c->dev,
1337 "%s(): no data available\n", __func__); 1631 "%s(): no data available\n", __func__);
1632 return -EINVAL;
1338 } 1633 }
1339done: 1634done:
1340 return res; 1635 *snr = res;
1636 return 0;
1637}
1638
1639static uint32_t sony_log(uint32_t x)
1640{
1641 return (((10000>>8)*(intlog2(x)>>16) + LOG2_E_100X/2)/LOG2_E_100X);
1642}
1643
1644static int cxd2841er_read_snr_c(struct cxd2841er_priv *priv, u32 *snr)
1645{
1646 u32 reg;
1647 u8 data[2];
1648 enum sony_dvbc_constellation_t qam = SONY_DVBC_CONSTELLATION_16QAM;
1649
1650 *snr = 0;
1651 if (priv->state != STATE_ACTIVE_TC) {
1652 dev_dbg(&priv->i2c->dev,
1653 "%s(): invalid state %d\n",
1654 __func__, priv->state);
1655 return -EINVAL;
1656 }
1657
1658 /*
1659 * Freeze registers: ensure multiple separate register reads
1660 * are from the same snapshot
1661 */
1662 cxd2841er_write_reg(priv, I2C_SLVT, 0x01, 0x01);
1663
1664 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x40);
1665 cxd2841er_read_regs(priv, I2C_SLVT, 0x19, data, 1);
1666 qam = (enum sony_dvbc_constellation_t) (data[0] & 0x07);
1667 cxd2841er_read_regs(priv, I2C_SLVT, 0x4C, data, 2);
1668
1669 reg = ((u32)(data[0]&0x1f) << 8) | (u32)data[1];
1670 if (reg == 0) {
1671 dev_dbg(&priv->i2c->dev,
1672 "%s(): reg value out of range\n", __func__);
1673 return 0;
1674 }
1675
1676 switch (qam) {
1677 case SONY_DVBC_CONSTELLATION_16QAM:
1678 case SONY_DVBC_CONSTELLATION_64QAM:
1679 case SONY_DVBC_CONSTELLATION_256QAM:
1680 /* SNR(dB) = -9.50 * ln(IREG_SNR_ESTIMATE / (24320)) */
1681 if (reg < 126)
1682 reg = 126;
1683 *snr = -95 * (int32_t)sony_log(reg) + 95941;
1684 break;
1685 case SONY_DVBC_CONSTELLATION_32QAM:
1686 case SONY_DVBC_CONSTELLATION_128QAM:
1687 /* SNR(dB) = -8.75 * ln(IREG_SNR_ESTIMATE / (20800)) */
1688 if (reg < 69)
1689 reg = 69;
1690 *snr = -88 * (int32_t)sony_log(reg) + 86999;
1691 break;
1692 default:
1693 return -EINVAL;
1694 }
1695
1696 return 0;
1341} 1697}
1342 1698
1343static int cxd2841er_read_snr_t(struct cxd2841er_priv *priv, u32 *snr) 1699static int cxd2841er_read_snr_t(struct cxd2841er_priv *priv, u32 *snr)
@@ -1391,6 +1747,52 @@ static int cxd2841er_read_snr_t2(struct cxd2841er_priv *priv, u32 *snr)
1391 return 0; 1747 return 0;
1392} 1748}
1393 1749
1750static int cxd2841er_read_snr_i(struct cxd2841er_priv *priv, u32 *snr)
1751{
1752 u32 reg;
1753 u8 data[2];
1754
1755 *snr = 0;
1756 if (priv->state != STATE_ACTIVE_TC) {
1757 dev_dbg(&priv->i2c->dev,
1758 "%s(): invalid state %d\n", __func__,
1759 priv->state);
1760 return -EINVAL;
1761 }
1762
1763 /* Freeze all registers */
1764 cxd2841er_write_reg(priv, I2C_SLVT, 0x01, 0x01);
1765
1766
1767 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x60);
1768 cxd2841er_read_regs(priv, I2C_SLVT, 0x28, data, sizeof(data));
1769 reg = ((u32)data[0] << 8) | (u32)data[1];
1770 if (reg == 0) {
1771 dev_dbg(&priv->i2c->dev,
1772 "%s(): reg value out of range\n", __func__);
1773 return 0;
1774 }
1775 if (reg > 4996)
1776 reg = 4996;
1777 *snr = 100 * intlog10(reg) - 9031;
1778 return 0;
1779}
1780
1781static u16 cxd2841er_read_agc_gain_c(struct cxd2841er_priv *priv,
1782 u8 delsys)
1783{
1784 u8 data[2];
1785
1786 cxd2841er_write_reg(
1787 priv, I2C_SLVT, 0x00, 0x40);
1788 cxd2841er_read_regs(priv, I2C_SLVT, 0x49, data, 2);
1789 dev_dbg(&priv->i2c->dev,
1790 "%s(): AGC value=%u\n",
1791 __func__, (((u16)data[0] & 0x0F) << 8) |
1792 (u16)(data[1] & 0xFF));
1793 return ((((u16)data[0] & 0x0F) << 8) | (u16)(data[1] & 0xFF)) << 4;
1794}
1795
1394static u16 cxd2841er_read_agc_gain_t_t2(struct cxd2841er_priv *priv, 1796static u16 cxd2841er_read_agc_gain_t_t2(struct cxd2841er_priv *priv,
1395 u8 delsys) 1797 u8 delsys)
1396{ 1798{
@@ -1399,6 +1801,26 @@ static u16 cxd2841er_read_agc_gain_t_t2(struct cxd2841er_priv *priv,
1399 cxd2841er_write_reg( 1801 cxd2841er_write_reg(
1400 priv, I2C_SLVT, 0x00, (delsys == SYS_DVBT ? 0x10 : 0x20)); 1802 priv, I2C_SLVT, 0x00, (delsys == SYS_DVBT ? 0x10 : 0x20));
1401 cxd2841er_read_regs(priv, I2C_SLVT, 0x26, data, 2); 1803 cxd2841er_read_regs(priv, I2C_SLVT, 0x26, data, 2);
1804 dev_dbg(&priv->i2c->dev,
1805 "%s(): AGC value=%u\n",
1806 __func__, (((u16)data[0] & 0x0F) << 8) |
1807 (u16)(data[1] & 0xFF));
1808 return ((((u16)data[0] & 0x0F) << 8) | (u16)(data[1] & 0xFF)) << 4;
1809}
1810
1811static u16 cxd2841er_read_agc_gain_i(struct cxd2841er_priv *priv,
1812 u8 delsys)
1813{
1814 u8 data[2];
1815
1816 cxd2841er_write_reg(
1817 priv, I2C_SLVT, 0x00, 0x60);
1818 cxd2841er_read_regs(priv, I2C_SLVT, 0x26, data, 2);
1819
1820 dev_dbg(&priv->i2c->dev,
1821 "%s(): AGC value=%u\n",
1822 __func__, (((u16)data[0] & 0x0F) << 8) |
1823 (u16)(data[1] & 0xFF));
1402 return ((((u16)data[0] & 0x0F) << 8) | (u16)(data[1] & 0xFF)) << 4; 1824 return ((((u16)data[0] & 0x0F) << 8) | (u16)(data[1] & 0xFF)) << 4;
1403} 1825}
1404 1826
@@ -1417,101 +1839,170 @@ static u16 cxd2841er_read_agc_gain_s(struct cxd2841er_priv *priv)
1417 return ((((u16)data[0] & 0x1F) << 8) | (u16)(data[1] & 0xFF)) << 3; 1839 return ((((u16)data[0] & 0x1F) << 8) | (u16)(data[1] & 0xFF)) << 3;
1418} 1840}
1419 1841
1420static int cxd2841er_read_ber(struct dvb_frontend *fe, u32 *ber) 1842static void cxd2841er_read_ber(struct dvb_frontend *fe)
1421{ 1843{
1422 struct dtv_frontend_properties *p = &fe->dtv_property_cache; 1844 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
1423 struct cxd2841er_priv *priv = fe->demodulator_priv; 1845 struct cxd2841er_priv *priv = fe->demodulator_priv;
1846 u32 ret, bit_error = 0, bit_count = 0;
1424 1847
1425 dev_dbg(&priv->i2c->dev, "%s()\n", __func__); 1848 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
1426 *ber = 0;
1427 switch (p->delivery_system) { 1849 switch (p->delivery_system) {
1850 case SYS_DVBC_ANNEX_A:
1851 case SYS_DVBC_ANNEX_B:
1852 case SYS_DVBC_ANNEX_C:
1853 ret = cxd2841er_read_ber_c(priv, &bit_error, &bit_count);
1854 break;
1428 case SYS_DVBS: 1855 case SYS_DVBS:
1429 *ber = cxd2841er_mon_read_ber_s(priv); 1856 ret = cxd2841er_mon_read_ber_s(priv, &bit_error, &bit_count);
1430 break; 1857 break;
1431 case SYS_DVBS2: 1858 case SYS_DVBS2:
1432 *ber = cxd2841er_mon_read_ber_s2(priv); 1859 ret = cxd2841er_mon_read_ber_s2(priv, &bit_error, &bit_count);
1433 break; 1860 break;
1434 case SYS_DVBT: 1861 case SYS_DVBT:
1435 return cxd2841er_read_ber_t(priv, ber); 1862 ret = cxd2841er_read_ber_t(priv, &bit_error, &bit_count);
1863 break;
1436 case SYS_DVBT2: 1864 case SYS_DVBT2:
1437 return cxd2841er_read_ber_t2(priv, ber); 1865 ret = cxd2841er_read_ber_t2(priv, &bit_error, &bit_count);
1438 default:
1439 *ber = 0;
1440 break; 1866 break;
1867 default:
1868 p->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
1869 p->post_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
1870 return;
1871 }
1872
1873 if (!ret) {
1874 p->post_bit_error.stat[0].scale = FE_SCALE_COUNTER;
1875 p->post_bit_error.stat[0].uvalue += bit_error;
1876 p->post_bit_count.stat[0].scale = FE_SCALE_COUNTER;
1877 p->post_bit_count.stat[0].uvalue += bit_count;
1878 } else {
1879 p->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
1880 p->post_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
1441 } 1881 }
1442 return 0;
1443} 1882}
1444 1883
1445static int cxd2841er_read_signal_strength(struct dvb_frontend *fe, 1884static void cxd2841er_read_signal_strength(struct dvb_frontend *fe)
1446 u16 *strength)
1447{ 1885{
1448 struct dtv_frontend_properties *p = &fe->dtv_property_cache; 1886 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
1449 struct cxd2841er_priv *priv = fe->demodulator_priv; 1887 struct cxd2841er_priv *priv = fe->demodulator_priv;
1888 s32 strength;
1450 1889
1451 dev_dbg(&priv->i2c->dev, "%s()\n", __func__); 1890 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
1452 switch (p->delivery_system) { 1891 switch (p->delivery_system) {
1453 case SYS_DVBT: 1892 case SYS_DVBT:
1454 case SYS_DVBT2: 1893 case SYS_DVBT2:
1455 *strength = 65535 - cxd2841er_read_agc_gain_t_t2( 1894 strength = cxd2841er_read_agc_gain_t_t2(priv,
1456 priv, p->delivery_system); 1895 p->delivery_system);
1896 p->strength.stat[0].scale = FE_SCALE_DECIBEL;
1897 /* Formula was empirically determinated @ 410 MHz */
1898 p->strength.stat[0].uvalue = strength * 366 / 100 - 89520;
1899 break; /* Code moved out of the function */
1900 case SYS_DVBC_ANNEX_A:
1901 case SYS_DVBC_ANNEX_B:
1902 case SYS_DVBC_ANNEX_C:
1903 strength = cxd2841er_read_agc_gain_c(priv,
1904 p->delivery_system);
1905 p->strength.stat[0].scale = FE_SCALE_DECIBEL;
1906 /*
1907 * Formula was empirically determinated via linear regression,
1908 * using frequencies: 175 MHz, 410 MHz and 800 MHz, and a
1909 * stream modulated with QAM64
1910 */
1911 p->strength.stat[0].uvalue = strength * 4045 / 1000 - 85224;
1912 break;
1913 case SYS_ISDBT:
1914 strength = cxd2841er_read_agc_gain_i(priv, p->delivery_system);
1915 p->strength.stat[0].scale = FE_SCALE_DECIBEL;
1916 /*
1917 * Formula was empirically determinated via linear regression,
1918 * using frequencies: 175 MHz, 410 MHz and 800 MHz.
1919 */
1920 p->strength.stat[0].uvalue = strength * 3775 / 1000 - 90185;
1457 break; 1921 break;
1458 case SYS_DVBS: 1922 case SYS_DVBS:
1459 case SYS_DVBS2: 1923 case SYS_DVBS2:
1460 *strength = 65535 - cxd2841er_read_agc_gain_s(priv); 1924 strength = 65535 - cxd2841er_read_agc_gain_s(priv);
1925 p->strength.stat[0].scale = FE_SCALE_RELATIVE;
1926 p->strength.stat[0].uvalue = strength;
1461 break; 1927 break;
1462 default: 1928 default:
1463 *strength = 0; 1929 p->strength.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
1464 break; 1930 break;
1465 } 1931 }
1466 return 0;
1467} 1932}
1468 1933
1469static int cxd2841er_read_snr(struct dvb_frontend *fe, u16 *snr) 1934static void cxd2841er_read_snr(struct dvb_frontend *fe)
1470{ 1935{
1471 u32 tmp = 0; 1936 u32 tmp = 0;
1937 int ret = 0;
1472 struct dtv_frontend_properties *p = &fe->dtv_property_cache; 1938 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
1473 struct cxd2841er_priv *priv = fe->demodulator_priv; 1939 struct cxd2841er_priv *priv = fe->demodulator_priv;
1474 1940
1475 dev_dbg(&priv->i2c->dev, "%s()\n", __func__); 1941 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
1476 switch (p->delivery_system) { 1942 switch (p->delivery_system) {
1943 case SYS_DVBC_ANNEX_A:
1944 case SYS_DVBC_ANNEX_B:
1945 case SYS_DVBC_ANNEX_C:
1946 ret = cxd2841er_read_snr_c(priv, &tmp);
1947 break;
1477 case SYS_DVBT: 1948 case SYS_DVBT:
1478 cxd2841er_read_snr_t(priv, &tmp); 1949 ret = cxd2841er_read_snr_t(priv, &tmp);
1479 break; 1950 break;
1480 case SYS_DVBT2: 1951 case SYS_DVBT2:
1481 cxd2841er_read_snr_t2(priv, &tmp); 1952 ret = cxd2841er_read_snr_t2(priv, &tmp);
1953 break;
1954 case SYS_ISDBT:
1955 ret = cxd2841er_read_snr_i(priv, &tmp);
1482 break; 1956 break;
1483 case SYS_DVBS: 1957 case SYS_DVBS:
1484 case SYS_DVBS2: 1958 case SYS_DVBS2:
1485 tmp = cxd2841er_dvbs_read_snr(priv, p->delivery_system); 1959 ret = cxd2841er_dvbs_read_snr(priv, p->delivery_system, &tmp);
1486 break; 1960 break;
1487 default: 1961 default:
1488 dev_dbg(&priv->i2c->dev, "%s(): unknown delivery system %d\n", 1962 dev_dbg(&priv->i2c->dev, "%s(): unknown delivery system %d\n",
1489 __func__, p->delivery_system); 1963 __func__, p->delivery_system);
1490 break; 1964 p->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
1965 return;
1966 }
1967
1968 if (!ret) {
1969 p->cnr.stat[0].scale = FE_SCALE_DECIBEL;
1970 p->cnr.stat[0].svalue = tmp;
1971 } else {
1972 p->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
1491 } 1973 }
1492 *snr = tmp & 0xffff;
1493 return 0;
1494} 1974}
1495 1975
1496static int cxd2841er_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks) 1976static void cxd2841er_read_ucblocks(struct dvb_frontend *fe)
1497{ 1977{
1498 struct dtv_frontend_properties *p = &fe->dtv_property_cache; 1978 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
1499 struct cxd2841er_priv *priv = fe->demodulator_priv; 1979 struct cxd2841er_priv *priv = fe->demodulator_priv;
1980 u32 ucblocks;
1500 1981
1501 dev_dbg(&priv->i2c->dev, "%s()\n", __func__); 1982 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
1502 switch (p->delivery_system) { 1983 switch (p->delivery_system) {
1984 case SYS_DVBC_ANNEX_A:
1985 case SYS_DVBC_ANNEX_B:
1986 case SYS_DVBC_ANNEX_C:
1987 cxd2841er_read_packet_errors_c(priv, &ucblocks);
1988 break;
1503 case SYS_DVBT: 1989 case SYS_DVBT:
1504 cxd2841er_read_packet_errors_t(priv, ucblocks); 1990 cxd2841er_read_packet_errors_t(priv, &ucblocks);
1505 break; 1991 break;
1506 case SYS_DVBT2: 1992 case SYS_DVBT2:
1507 cxd2841er_read_packet_errors_t2(priv, ucblocks); 1993 cxd2841er_read_packet_errors_t2(priv, &ucblocks);
1508 break; 1994 break;
1509 default: 1995 case SYS_ISDBT:
1510 *ucblocks = 0; 1996 cxd2841er_read_packet_errors_i(priv, &ucblocks);
1511 break; 1997 break;
1998 default:
1999 p->block_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
2000 return;
1512 } 2001 }
1513 dev_dbg(&priv->i2c->dev, "%s()\n", __func__); 2002 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
1514 return 0; 2003
2004 p->block_error.stat[0].scale = FE_SCALE_COUNTER;
2005 p->block_error.stat[0].uvalue = ucblocks;
1515} 2006}
1516 2007
1517static int cxd2841er_dvbt2_set_profile( 2008static int cxd2841er_dvbt2_set_profile(
@@ -1524,15 +2015,18 @@ static int cxd2841er_dvbt2_set_profile(
1524 switch (profile) { 2015 switch (profile) {
1525 case DVBT2_PROFILE_BASE: 2016 case DVBT2_PROFILE_BASE:
1526 tune_mode = 0x01; 2017 tune_mode = 0x01;
1527 seq_not2d_time = 12; 2018 /* Set early unlock time */
2019 seq_not2d_time = (priv->xtal == SONY_XTAL_24000)?0x0E:0x0C;
1528 break; 2020 break;
1529 case DVBT2_PROFILE_LITE: 2021 case DVBT2_PROFILE_LITE:
1530 tune_mode = 0x05; 2022 tune_mode = 0x05;
1531 seq_not2d_time = 40; 2023 /* Set early unlock time */
2024 seq_not2d_time = (priv->xtal == SONY_XTAL_24000)?0x2E:0x28;
1532 break; 2025 break;
1533 case DVBT2_PROFILE_ANY: 2026 case DVBT2_PROFILE_ANY:
1534 tune_mode = 0x00; 2027 tune_mode = 0x00;
1535 seq_not2d_time = 40; 2028 /* Set early unlock time */
2029 seq_not2d_time = (priv->xtal == SONY_XTAL_24000)?0x2E:0x28;
1536 break; 2030 break;
1537 default: 2031 default:
1538 return -EINVAL; 2032 return -EINVAL;
@@ -1574,254 +2068,617 @@ static int cxd2841er_sleep_tc_to_active_t2_band(struct cxd2841er_priv *priv,
1574 u32 bandwidth) 2068 u32 bandwidth)
1575{ 2069{
1576 u32 iffreq; 2070 u32 iffreq;
1577 u8 b20_9f[5]; 2071 u8 data[MAX_WRITE_REGSIZE];
1578 u8 b10_a6[14]; 2072
1579 u8 b10_b6[3]; 2073 const uint8_t nominalRate8bw[3][5] = {
1580 u8 b10_d7; 2074 /* TRCG Nominal Rate [37:0] */
2075 {0x11, 0xF0, 0x00, 0x00, 0x00}, /* 20.5MHz XTal */
2076 {0x15, 0x00, 0x00, 0x00, 0x00}, /* 24MHz XTal */
2077 {0x11, 0xF0, 0x00, 0x00, 0x00} /* 41MHz XTal */
2078 };
2079
2080 const uint8_t nominalRate7bw[3][5] = {
2081 /* TRCG Nominal Rate [37:0] */
2082 {0x14, 0x80, 0x00, 0x00, 0x00}, /* 20.5MHz XTal */
2083 {0x18, 0x00, 0x00, 0x00, 0x00}, /* 24MHz XTal */
2084 {0x14, 0x80, 0x00, 0x00, 0x00} /* 41MHz XTal */
2085 };
2086
2087 const uint8_t nominalRate6bw[3][5] = {
2088 /* TRCG Nominal Rate [37:0] */
2089 {0x17, 0xEA, 0xAA, 0xAA, 0xAA}, /* 20.5MHz XTal */
2090 {0x1C, 0x00, 0x00, 0x00, 0x00}, /* 24MHz XTal */
2091 {0x17, 0xEA, 0xAA, 0xAA, 0xAA} /* 41MHz XTal */
2092 };
2093
2094 const uint8_t nominalRate5bw[3][5] = {
2095 /* TRCG Nominal Rate [37:0] */
2096 {0x1C, 0xB3, 0x33, 0x33, 0x33}, /* 20.5MHz XTal */
2097 {0x21, 0x99, 0x99, 0x99, 0x99}, /* 24MHz XTal */
2098 {0x1C, 0xB3, 0x33, 0x33, 0x33} /* 41MHz XTal */
2099 };
2100
2101 const uint8_t nominalRate17bw[3][5] = {
2102 /* TRCG Nominal Rate [37:0] */
2103 {0x58, 0xE2, 0xAF, 0xE0, 0xBC}, /* 20.5MHz XTal */
2104 {0x68, 0x0F, 0xA2, 0x32, 0xD0}, /* 24MHz XTal */
2105 {0x58, 0xE2, 0xAF, 0xE0, 0xBC} /* 41MHz XTal */
2106 };
2107
2108 const uint8_t itbCoef8bw[3][14] = {
2109 {0x26, 0xAF, 0x06, 0xCD, 0x13, 0xBB, 0x28, 0xBA,
2110 0x23, 0xA9, 0x1F, 0xA8, 0x2C, 0xC8}, /* 20.5MHz XTal */
2111 {0x2F, 0xBA, 0x28, 0x9B, 0x28, 0x9D, 0x28, 0xA1,
2112 0x29, 0xA5, 0x2A, 0xAC, 0x29, 0xB5}, /* 24MHz XTal */
2113 {0x26, 0xAF, 0x06, 0xCD, 0x13, 0xBB, 0x28, 0xBA,
2114 0x23, 0xA9, 0x1F, 0xA8, 0x2C, 0xC8} /* 41MHz XTal */
2115 };
2116
2117 const uint8_t itbCoef7bw[3][14] = {
2118 {0x2C, 0xBD, 0x02, 0xCF, 0x04, 0xF8, 0x23, 0xA6,
2119 0x29, 0xB0, 0x26, 0xA9, 0x21, 0xA5}, /* 20.5MHz XTal */
2120 {0x30, 0xB1, 0x29, 0x9A, 0x28, 0x9C, 0x28, 0xA0,
2121 0x29, 0xA2, 0x2B, 0xA6, 0x2B, 0xAD}, /* 24MHz XTal */
2122 {0x2C, 0xBD, 0x02, 0xCF, 0x04, 0xF8, 0x23, 0xA6,
2123 0x29, 0xB0, 0x26, 0xA9, 0x21, 0xA5} /* 41MHz XTal */
2124 };
2125
2126 const uint8_t itbCoef6bw[3][14] = {
2127 {0x27, 0xA7, 0x28, 0xB3, 0x02, 0xF0, 0x01, 0xE8,
2128 0x00, 0xCF, 0x00, 0xE6, 0x23, 0xA4}, /* 20.5MHz XTal */
2129 {0x31, 0xA8, 0x29, 0x9B, 0x27, 0x9C, 0x28, 0x9E,
2130 0x29, 0xA4, 0x29, 0xA2, 0x29, 0xA8}, /* 24MHz XTal */
2131 {0x27, 0xA7, 0x28, 0xB3, 0x02, 0xF0, 0x01, 0xE8,
2132 0x00, 0xCF, 0x00, 0xE6, 0x23, 0xA4} /* 41MHz XTal */
2133 };
2134
2135 const uint8_t itbCoef5bw[3][14] = {
2136 {0x27, 0xA7, 0x28, 0xB3, 0x02, 0xF0, 0x01, 0xE8,
2137 0x00, 0xCF, 0x00, 0xE6, 0x23, 0xA4}, /* 20.5MHz XTal */
2138 {0x31, 0xA8, 0x29, 0x9B, 0x27, 0x9C, 0x28, 0x9E,
2139 0x29, 0xA4, 0x29, 0xA2, 0x29, 0xA8}, /* 24MHz XTal */
2140 {0x27, 0xA7, 0x28, 0xB3, 0x02, 0xF0, 0x01, 0xE8,
2141 0x00, 0xCF, 0x00, 0xE6, 0x23, 0xA4} /* 41MHz XTal */
2142 };
2143
2144 const uint8_t itbCoef17bw[3][14] = {
2145 {0x25, 0xA0, 0x36, 0x8D, 0x2E, 0x94, 0x28, 0x9B,
2146 0x32, 0x90, 0x2C, 0x9D, 0x29, 0x99}, /* 20.5MHz XTal */
2147 {0x33, 0x8E, 0x2B, 0x97, 0x2D, 0x95, 0x37, 0x8B,
2148 0x30, 0x97, 0x2D, 0x9A, 0x21, 0xA4}, /* 24MHz XTal */
2149 {0x25, 0xA0, 0x36, 0x8D, 0x2E, 0x94, 0x28, 0x9B,
2150 0x32, 0x90, 0x2C, 0x9D, 0x29, 0x99} /* 41MHz XTal */
2151 };
2152
2153 /* Set SLV-T Bank : 0x20 */
2154 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x20);
1581 2155
1582 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
1583 switch (bandwidth) { 2156 switch (bandwidth) {
1584 case 8000000: 2157 case 8000000:
1585 /* bank 0x20, reg 0x9f */ 2158 /* <Timing Recovery setting> */
1586 b20_9f[0] = 0x11; 2159 cxd2841er_write_regs(priv, I2C_SLVT,
1587 b20_9f[1] = 0xf0; 2160 0x9F, nominalRate8bw[priv->xtal], 5);
1588 b20_9f[2] = 0x00; 2161
1589 b20_9f[3] = 0x00; 2162 /* Set SLV-T Bank : 0x27 */
1590 b20_9f[4] = 0x00; 2163 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x27);
1591 /* bank 0x10, reg 0xa6 */ 2164 cxd2841er_set_reg_bits(priv, I2C_SLVT,
1592 b10_a6[0] = 0x26; 2165 0x7a, 0x00, 0x0f);
1593 b10_a6[1] = 0xaf; 2166
1594 b10_a6[2] = 0x06; 2167 /* Set SLV-T Bank : 0x10 */
1595 b10_a6[3] = 0xcd; 2168 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
1596 b10_a6[4] = 0x13; 2169
1597 b10_a6[5] = 0xbb; 2170 /* Group delay equaliser settings for
1598 b10_a6[6] = 0x28; 2171 * ASCOT2D, ASCOT2E and ASCOT3 tuners
1599 b10_a6[7] = 0xba; 2172 */
1600 b10_a6[8] = 0x23; 2173 cxd2841er_write_regs(priv, I2C_SLVT,
1601 b10_a6[9] = 0xa9; 2174 0xA6, itbCoef8bw[priv->xtal], 14);
1602 b10_a6[10] = 0x1f; 2175 /* <IF freq setting> */
1603 b10_a6[11] = 0xa8; 2176 iffreq = MAKE_IFFREQ_CONFIG_XTAL(priv->xtal, 4.80);
1604 b10_a6[12] = 0x2c; 2177 data[0] = (u8) ((iffreq >> 16) & 0xff);
1605 b10_a6[13] = 0xc8; 2178 data[1] = (u8)((iffreq >> 8) & 0xff);
1606 iffreq = MAKE_IFFREQ_CONFIG(4.80); 2179 data[2] = (u8)(iffreq & 0xff);
1607 b10_d7 = 0x00; 2180 cxd2841er_write_regs(priv, I2C_SLVT, 0xB6, data, 3);
2181 /* System bandwidth setting */
2182 cxd2841er_set_reg_bits(
2183 priv, I2C_SLVT, 0xD7, 0x00, 0x07);
1608 break; 2184 break;
1609 case 7000000: 2185 case 7000000:
1610 /* bank 0x20, reg 0x9f */ 2186 /* <Timing Recovery setting> */
1611 b20_9f[0] = 0x14; 2187 cxd2841er_write_regs(priv, I2C_SLVT,
1612 b20_9f[1] = 0x80; 2188 0x9F, nominalRate7bw[priv->xtal], 5);
1613 b20_9f[2] = 0x00; 2189
1614 b20_9f[3] = 0x00; 2190 /* Set SLV-T Bank : 0x27 */
1615 b20_9f[4] = 0x00; 2191 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x27);
1616 /* bank 0x10, reg 0xa6 */ 2192 cxd2841er_set_reg_bits(priv, I2C_SLVT,
1617 b10_a6[0] = 0x2C; 2193 0x7a, 0x00, 0x0f);
1618 b10_a6[1] = 0xBD; 2194
1619 b10_a6[2] = 0x02; 2195 /* Set SLV-T Bank : 0x10 */
1620 b10_a6[3] = 0xCF; 2196 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
1621 b10_a6[4] = 0x04; 2197
1622 b10_a6[5] = 0xF8; 2198 /* Group delay equaliser settings for
1623 b10_a6[6] = 0x23; 2199 * ASCOT2D, ASCOT2E and ASCOT3 tuners
1624 b10_a6[7] = 0xA6; 2200 */
1625 b10_a6[8] = 0x29; 2201 cxd2841er_write_regs(priv, I2C_SLVT,
1626 b10_a6[9] = 0xB0; 2202 0xA6, itbCoef7bw[priv->xtal], 14);
1627 b10_a6[10] = 0x26; 2203 /* <IF freq setting> */
1628 b10_a6[11] = 0xA9; 2204 iffreq = MAKE_IFFREQ_CONFIG_XTAL(priv->xtal, 4.20);
1629 b10_a6[12] = 0x21; 2205 data[0] = (u8) ((iffreq >> 16) & 0xff);
1630 b10_a6[13] = 0xA5; 2206 data[1] = (u8)((iffreq >> 8) & 0xff);
1631 iffreq = MAKE_IFFREQ_CONFIG(4.2); 2207 data[2] = (u8)(iffreq & 0xff);
1632 b10_d7 = 0x02; 2208 cxd2841er_write_regs(priv, I2C_SLVT, 0xB6, data, 3);
2209 /* System bandwidth setting */
2210 cxd2841er_set_reg_bits(
2211 priv, I2C_SLVT, 0xD7, 0x02, 0x07);
1633 break; 2212 break;
1634 case 6000000: 2213 case 6000000:
1635 /* bank 0x20, reg 0x9f */ 2214 /* <Timing Recovery setting> */
1636 b20_9f[0] = 0x17; 2215 cxd2841er_write_regs(priv, I2C_SLVT,
1637 b20_9f[1] = 0xEA; 2216 0x9F, nominalRate6bw[priv->xtal], 5);
1638 b20_9f[2] = 0xAA; 2217
1639 b20_9f[3] = 0xAA; 2218 /* Set SLV-T Bank : 0x27 */
1640 b20_9f[4] = 0xAA; 2219 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x27);
1641 /* bank 0x10, reg 0xa6 */ 2220 cxd2841er_set_reg_bits(priv, I2C_SLVT,
1642 b10_a6[0] = 0x27; 2221 0x7a, 0x00, 0x0f);
1643 b10_a6[1] = 0xA7; 2222
1644 b10_a6[2] = 0x28; 2223 /* Set SLV-T Bank : 0x10 */
1645 b10_a6[3] = 0xB3; 2224 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
1646 b10_a6[4] = 0x02; 2225
1647 b10_a6[5] = 0xF0; 2226 /* Group delay equaliser settings for
1648 b10_a6[6] = 0x01; 2227 * ASCOT2D, ASCOT2E and ASCOT3 tuners
1649 b10_a6[7] = 0xE8; 2228 */
1650 b10_a6[8] = 0x00; 2229 cxd2841er_write_regs(priv, I2C_SLVT,
1651 b10_a6[9] = 0xCF; 2230 0xA6, itbCoef6bw[priv->xtal], 14);
1652 b10_a6[10] = 0x00; 2231 /* <IF freq setting> */
1653 b10_a6[11] = 0xE6; 2232 iffreq = MAKE_IFFREQ_CONFIG_XTAL(priv->xtal, 3.60);
1654 b10_a6[12] = 0x23; 2233 data[0] = (u8) ((iffreq >> 16) & 0xff);
1655 b10_a6[13] = 0xA4; 2234 data[1] = (u8)((iffreq >> 8) & 0xff);
1656 iffreq = MAKE_IFFREQ_CONFIG(3.6); 2235 data[2] = (u8)(iffreq & 0xff);
1657 b10_d7 = 0x04; 2236 cxd2841er_write_regs(priv, I2C_SLVT, 0xB6, data, 3);
2237 /* System bandwidth setting */
2238 cxd2841er_set_reg_bits(
2239 priv, I2C_SLVT, 0xD7, 0x04, 0x07);
1658 break; 2240 break;
1659 case 5000000: 2241 case 5000000:
1660 /* bank 0x20, reg 0x9f */ 2242 /* <Timing Recovery setting> */
1661 b20_9f[0] = 0x1C; 2243 cxd2841er_write_regs(priv, I2C_SLVT,
1662 b20_9f[1] = 0xB3; 2244 0x9F, nominalRate5bw[priv->xtal], 5);
1663 b20_9f[2] = 0x33; 2245
1664 b20_9f[3] = 0x33; 2246 /* Set SLV-T Bank : 0x27 */
1665 b20_9f[4] = 0x33; 2247 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x27);
1666 /* bank 0x10, reg 0xa6 */ 2248 cxd2841er_set_reg_bits(priv, I2C_SLVT,
1667 b10_a6[0] = 0x27; 2249 0x7a, 0x00, 0x0f);
1668 b10_a6[1] = 0xA7; 2250
1669 b10_a6[2] = 0x28; 2251 /* Set SLV-T Bank : 0x10 */
1670 b10_a6[3] = 0xB3; 2252 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
1671 b10_a6[4] = 0x02; 2253
1672 b10_a6[5] = 0xF0; 2254 /* Group delay equaliser settings for
1673 b10_a6[6] = 0x01; 2255 * ASCOT2D, ASCOT2E and ASCOT3 tuners
1674 b10_a6[7] = 0xE8; 2256 */
1675 b10_a6[8] = 0x00; 2257 cxd2841er_write_regs(priv, I2C_SLVT,
1676 b10_a6[9] = 0xCF; 2258 0xA6, itbCoef5bw[priv->xtal], 14);
1677 b10_a6[10] = 0x00; 2259 /* <IF freq setting> */
1678 b10_a6[11] = 0xE6; 2260 iffreq = MAKE_IFFREQ_CONFIG_XTAL(priv->xtal, 3.60);
1679 b10_a6[12] = 0x23; 2261 data[0] = (u8) ((iffreq >> 16) & 0xff);
1680 b10_a6[13] = 0xA4; 2262 data[1] = (u8)((iffreq >> 8) & 0xff);
1681 iffreq = MAKE_IFFREQ_CONFIG(3.6); 2263 data[2] = (u8)(iffreq & 0xff);
1682 b10_d7 = 0x06; 2264 cxd2841er_write_regs(priv, I2C_SLVT, 0xB6, data, 3);
2265 /* System bandwidth setting */
2266 cxd2841er_set_reg_bits(
2267 priv, I2C_SLVT, 0xD7, 0x06, 0x07);
1683 break; 2268 break;
1684 case 1712000: 2269 case 1712000:
1685 /* bank 0x20, reg 0x9f */ 2270 /* <Timing Recovery setting> */
1686 b20_9f[0] = 0x58; 2271 cxd2841er_write_regs(priv, I2C_SLVT,
1687 b20_9f[1] = 0xE2; 2272 0x9F, nominalRate17bw[priv->xtal], 5);
1688 b20_9f[2] = 0xAF; 2273
1689 b20_9f[3] = 0xE0; 2274 /* Set SLV-T Bank : 0x27 */
1690 b20_9f[4] = 0xBC; 2275 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x27);
1691 /* bank 0x10, reg 0xa6 */ 2276 cxd2841er_set_reg_bits(priv, I2C_SLVT,
1692 b10_a6[0] = 0x25; 2277 0x7a, 0x03, 0x0f);
1693 b10_a6[1] = 0xA0; 2278
1694 b10_a6[2] = 0x36; 2279 /* Set SLV-T Bank : 0x10 */
1695 b10_a6[3] = 0x8D; 2280 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
1696 b10_a6[4] = 0x2E; 2281
1697 b10_a6[5] = 0x94; 2282 /* Group delay equaliser settings for
1698 b10_a6[6] = 0x28; 2283 * ASCOT2D, ASCOT2E and ASCOT3 tuners
1699 b10_a6[7] = 0x9B; 2284 */
1700 b10_a6[8] = 0x32; 2285 cxd2841er_write_regs(priv, I2C_SLVT,
1701 b10_a6[9] = 0x90; 2286 0xA6, itbCoef17bw[priv->xtal], 14);
1702 b10_a6[10] = 0x2C; 2287 /* <IF freq setting> */
1703 b10_a6[11] = 0x9D; 2288 iffreq = MAKE_IFFREQ_CONFIG_XTAL(priv->xtal, 3.50);
1704 b10_a6[12] = 0x29; 2289 data[0] = (u8) ((iffreq >> 16) & 0xff);
1705 b10_a6[13] = 0x99; 2290 data[1] = (u8)((iffreq >> 8) & 0xff);
1706 iffreq = MAKE_IFFREQ_CONFIG(3.5); 2291 data[2] = (u8)(iffreq & 0xff);
1707 b10_d7 = 0x03; 2292 cxd2841er_write_regs(priv, I2C_SLVT, 0xB6, data, 3);
2293 /* System bandwidth setting */
2294 cxd2841er_set_reg_bits(
2295 priv, I2C_SLVT, 0xD7, 0x03, 0x07);
1708 break; 2296 break;
1709 default: 2297 default:
1710 return -EINVAL; 2298 return -EINVAL;
1711 } 2299 }
1712 /* Set SLV-T Bank : 0x20 */
1713 cxd2841er_write_reg(priv, I2C_SLVX, 0x00, 0x20);
1714 cxd2841er_write_regs(priv, I2C_SLVT, 0x9f, b20_9f, sizeof(b20_9f));
1715 /* Set SLV-T Bank : 0x27 */
1716 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x27);
1717 cxd2841er_set_reg_bits(
1718 priv, I2C_SLVT, 0x7a,
1719 (bandwidth == 1712000 ? 0x03 : 0x00), 0x0f);
1720 /* Set SLV-T Bank : 0x10 */
1721 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
1722 /* Group delay equaliser sett. for ASCOT2E */
1723 cxd2841er_write_regs(priv, I2C_SLVT, 0xa6, b10_a6, sizeof(b10_a6));
1724 /* <IF freq setting> */
1725 b10_b6[0] = (u8) ((iffreq >> 16) & 0xff);
1726 b10_b6[1] = (u8)((iffreq >> 8) & 0xff);
1727 b10_b6[2] = (u8)(iffreq & 0xff);
1728 cxd2841er_write_regs(priv, I2C_SLVT, 0xb6, b10_b6, sizeof(b10_b6));
1729 /* System bandwidth setting */
1730 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xd7, b10_d7, 0x07);
1731 return 0; 2300 return 0;
1732} 2301}
1733 2302
1734static int cxd2841er_sleep_tc_to_active_t_band( 2303static int cxd2841er_sleep_tc_to_active_t_band(
1735 struct cxd2841er_priv *priv, u32 bandwidth) 2304 struct cxd2841er_priv *priv, u32 bandwidth)
1736{ 2305{
1737 u8 b13_9c[2] = { 0x01, 0x14 }; 2306 u8 data[MAX_WRITE_REGSIZE];
1738 u8 bw8mhz_b10_9f[] = { 0x11, 0xF0, 0x00, 0x00, 0x00 };
1739 u8 bw8mhz_b10_a6[] = { 0x26, 0xAF, 0x06, 0xCD, 0x13, 0xBB,
1740 0x28, 0xBA, 0x23, 0xA9, 0x1F, 0xA8, 0x2C, 0xC8 };
1741 u8 bw8mhz_b10_d9[] = { 0x01, 0xE0 };
1742 u8 bw8mhz_b17_38[] = { 0x01, 0x02 };
1743 u8 bw7mhz_b10_9f[] = { 0x14, 0x80, 0x00, 0x00, 0x00 };
1744 u8 bw7mhz_b10_a6[] = { 0x2C, 0xBD, 0x02, 0xCF, 0x04, 0xF8,
1745 0x23, 0xA6, 0x29, 0xB0, 0x26, 0xA9, 0x21, 0xA5 };
1746 u8 bw7mhz_b10_d9[] = { 0x12, 0xF8 };
1747 u8 bw7mhz_b17_38[] = { 0x00, 0x03 };
1748 u8 bw6mhz_b10_9f[] = { 0x17, 0xEA, 0xAA, 0xAA, 0xAA };
1749 u8 bw6mhz_b10_a6[] = { 0x27, 0xA7, 0x28, 0xB3, 0x02, 0xF0,
1750 0x01, 0xE8, 0x00, 0xCF, 0x00, 0xE6, 0x23, 0xA4 };
1751 u8 bw6mhz_b10_d9[] = { 0x1F, 0xDC };
1752 u8 bw6mhz_b17_38[] = { 0x00, 0x03 };
1753 u8 bw5mhz_b10_9f[] = { 0x1C, 0xB3, 0x33, 0x33, 0x33 };
1754 u8 bw5mhz_b10_a6[] = { 0x27, 0xA7, 0x28, 0xB3, 0x02, 0xF0,
1755 0x01, 0xE8, 0x00, 0xCF, 0x00, 0xE6, 0x23, 0xA4 };
1756 u8 bw5mhz_b10_d9[] = { 0x26, 0x3C };
1757 u8 bw5mhz_b17_38[] = { 0x00, 0x03 };
1758 u8 b10_b6[3];
1759 u8 d7val;
1760 u32 iffreq; 2307 u32 iffreq;
1761 u8 *b10_9f; 2308 u8 nominalRate8bw[3][5] = {
1762 u8 *b10_a6; 2309 /* TRCG Nominal Rate [37:0] */
1763 u8 *b10_d9; 2310 {0x11, 0xF0, 0x00, 0x00, 0x00}, /* 20.5MHz XTal */
1764 u8 *b17_38; 2311 {0x15, 0x00, 0x00, 0x00, 0x00}, /* 24MHz XTal */
2312 {0x11, 0xF0, 0x00, 0x00, 0x00} /* 41MHz XTal */
2313 };
2314 u8 nominalRate7bw[3][5] = {
2315 /* TRCG Nominal Rate [37:0] */
2316 {0x14, 0x80, 0x00, 0x00, 0x00}, /* 20.5MHz XTal */
2317 {0x18, 0x00, 0x00, 0x00, 0x00}, /* 24MHz XTal */
2318 {0x14, 0x80, 0x00, 0x00, 0x00} /* 41MHz XTal */
2319 };
2320 u8 nominalRate6bw[3][5] = {
2321 /* TRCG Nominal Rate [37:0] */
2322 {0x17, 0xEA, 0xAA, 0xAA, 0xAA}, /* 20.5MHz XTal */
2323 {0x1C, 0x00, 0x00, 0x00, 0x00}, /* 24MHz XTal */
2324 {0x17, 0xEA, 0xAA, 0xAA, 0xAA} /* 41MHz XTal */
2325 };
2326 u8 nominalRate5bw[3][5] = {
2327 /* TRCG Nominal Rate [37:0] */
2328 {0x1C, 0xB3, 0x33, 0x33, 0x33}, /* 20.5MHz XTal */
2329 {0x21, 0x99, 0x99, 0x99, 0x99}, /* 24MHz XTal */
2330 {0x1C, 0xB3, 0x33, 0x33, 0x33} /* 41MHz XTal */
2331 };
1765 2332
1766 dev_dbg(&priv->i2c->dev, "%s()\n", __func__); 2333 u8 itbCoef8bw[3][14] = {
2334 {0x26, 0xAF, 0x06, 0xCD, 0x13, 0xBB, 0x28, 0xBA, 0x23, 0xA9,
2335 0x1F, 0xA8, 0x2C, 0xC8}, /* 20.5MHz XTal */
2336 {0x2F, 0xBA, 0x28, 0x9B, 0x28, 0x9D, 0x28, 0xA1, 0x29, 0xA5,
2337 0x2A, 0xAC, 0x29, 0xB5}, /* 24MHz XTal */
2338 {0x26, 0xAF, 0x06, 0xCD, 0x13, 0xBB, 0x28, 0xBA, 0x23, 0xA9,
2339 0x1F, 0xA8, 0x2C, 0xC8} /* 41MHz XTal */
2340 };
2341 u8 itbCoef7bw[3][14] = {
2342 {0x2C, 0xBD, 0x02, 0xCF, 0x04, 0xF8, 0x23, 0xA6, 0x29, 0xB0,
2343 0x26, 0xA9, 0x21, 0xA5}, /* 20.5MHz XTal */
2344 {0x30, 0xB1, 0x29, 0x9A, 0x28, 0x9C, 0x28, 0xA0, 0x29, 0xA2,
2345 0x2B, 0xA6, 0x2B, 0xAD}, /* 24MHz XTal */
2346 {0x2C, 0xBD, 0x02, 0xCF, 0x04, 0xF8, 0x23, 0xA6, 0x29, 0xB0,
2347 0x26, 0xA9, 0x21, 0xA5} /* 41MHz XTal */
2348 };
2349 u8 itbCoef6bw[3][14] = {
2350 {0x27, 0xA7, 0x28, 0xB3, 0x02, 0xF0, 0x01, 0xE8, 0x00, 0xCF,
2351 0x00, 0xE6, 0x23, 0xA4}, /* 20.5MHz XTal */
2352 {0x31, 0xA8, 0x29, 0x9B, 0x27, 0x9C, 0x28, 0x9E, 0x29, 0xA4,
2353 0x29, 0xA2, 0x29, 0xA8}, /* 24MHz XTal */
2354 {0x27, 0xA7, 0x28, 0xB3, 0x02, 0xF0, 0x01, 0xE8, 0x00, 0xCF,
2355 0x00, 0xE6, 0x23, 0xA4} /* 41MHz XTal */
2356 };
2357 u8 itbCoef5bw[3][14] = {
2358 {0x27, 0xA7, 0x28, 0xB3, 0x02, 0xF0, 0x01, 0xE8, 0x00, 0xCF,
2359 0x00, 0xE6, 0x23, 0xA4}, /* 20.5MHz XTal */
2360 {0x31, 0xA8, 0x29, 0x9B, 0x27, 0x9C, 0x28, 0x9E, 0x29, 0xA4,
2361 0x29, 0xA2, 0x29, 0xA8}, /* 24MHz XTal */
2362 {0x27, 0xA7, 0x28, 0xB3, 0x02, 0xF0, 0x01, 0xE8, 0x00, 0xCF,
2363 0x00, 0xE6, 0x23, 0xA4} /* 41MHz XTal */
2364 };
2365
2366 /* Set SLV-T Bank : 0x13 */
1767 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x13); 2367 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x13);
1768 /* Echo performance optimization setting */ 2368 /* Echo performance optimization setting */
1769 cxd2841er_write_regs(priv, I2C_SLVT, 0x9c, b13_9c, sizeof(b13_9c)); 2369 data[0] = 0x01;
2370 data[1] = 0x14;
2371 cxd2841er_write_regs(priv, I2C_SLVT, 0x9C, data, 2);
2372
2373 /* Set SLV-T Bank : 0x10 */
1770 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10); 2374 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
1771 2375
1772 switch (bandwidth) { 2376 switch (bandwidth) {
1773 case 8000000: 2377 case 8000000:
1774 b10_9f = bw8mhz_b10_9f; 2378 /* <Timing Recovery setting> */
1775 b10_a6 = bw8mhz_b10_a6; 2379 cxd2841er_write_regs(priv, I2C_SLVT,
1776 b10_d9 = bw8mhz_b10_d9; 2380 0x9F, nominalRate8bw[priv->xtal], 5);
1777 b17_38 = bw8mhz_b17_38; 2381 /* Group delay equaliser settings for
1778 d7val = 0; 2382 * ASCOT2D, ASCOT2E and ASCOT3 tuners
1779 iffreq = MAKE_IFFREQ_CONFIG(4.80); 2383 */
2384 cxd2841er_write_regs(priv, I2C_SLVT,
2385 0xA6, itbCoef8bw[priv->xtal], 14);
2386 /* <IF freq setting> */
2387 iffreq = MAKE_IFFREQ_CONFIG_XTAL(priv->xtal, 4.80);
2388 data[0] = (u8) ((iffreq >> 16) & 0xff);
2389 data[1] = (u8)((iffreq >> 8) & 0xff);
2390 data[2] = (u8)(iffreq & 0xff);
2391 cxd2841er_write_regs(priv, I2C_SLVT, 0xB6, data, 3);
2392 /* System bandwidth setting */
2393 cxd2841er_set_reg_bits(
2394 priv, I2C_SLVT, 0xD7, 0x00, 0x07);
2395
2396 /* Demod core latency setting */
2397 if (priv->xtal == SONY_XTAL_24000) {
2398 data[0] = 0x15;
2399 data[1] = 0x28;
2400 } else {
2401 data[0] = 0x01;
2402 data[1] = 0xE0;
2403 }
2404 cxd2841er_write_regs(priv, I2C_SLVT, 0xD9, data, 2);
2405
2406 /* Notch filter setting */
2407 data[0] = 0x01;
2408 data[1] = 0x02;
2409 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x17);
2410 cxd2841er_write_regs(priv, I2C_SLVT, 0x38, data, 2);
1780 break; 2411 break;
1781 case 7000000: 2412 case 7000000:
1782 b10_9f = bw7mhz_b10_9f; 2413 /* <Timing Recovery setting> */
1783 b10_a6 = bw7mhz_b10_a6; 2414 cxd2841er_write_regs(priv, I2C_SLVT,
1784 b10_d9 = bw7mhz_b10_d9; 2415 0x9F, nominalRate7bw[priv->xtal], 5);
1785 b17_38 = bw7mhz_b17_38; 2416 /* Group delay equaliser settings for
1786 d7val = 2; 2417 * ASCOT2D, ASCOT2E and ASCOT3 tuners
1787 iffreq = MAKE_IFFREQ_CONFIG(4.20); 2418 */
2419 cxd2841er_write_regs(priv, I2C_SLVT,
2420 0xA6, itbCoef7bw[priv->xtal], 14);
2421 /* <IF freq setting> */
2422 iffreq = MAKE_IFFREQ_CONFIG_XTAL(priv->xtal, 4.20);
2423 data[0] = (u8) ((iffreq >> 16) & 0xff);
2424 data[1] = (u8)((iffreq >> 8) & 0xff);
2425 data[2] = (u8)(iffreq & 0xff);
2426 cxd2841er_write_regs(priv, I2C_SLVT, 0xB6, data, 3);
2427 /* System bandwidth setting */
2428 cxd2841er_set_reg_bits(
2429 priv, I2C_SLVT, 0xD7, 0x02, 0x07);
2430
2431 /* Demod core latency setting */
2432 if (priv->xtal == SONY_XTAL_24000) {
2433 data[0] = 0x1F;
2434 data[1] = 0xF8;
2435 } else {
2436 data[0] = 0x12;
2437 data[1] = 0xF8;
2438 }
2439 cxd2841er_write_regs(priv, I2C_SLVT, 0xD9, data, 2);
2440
2441 /* Notch filter setting */
2442 data[0] = 0x00;
2443 data[1] = 0x03;
2444 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x17);
2445 cxd2841er_write_regs(priv, I2C_SLVT, 0x38, data, 2);
1788 break; 2446 break;
1789 case 6000000: 2447 case 6000000:
1790 b10_9f = bw6mhz_b10_9f; 2448 /* <Timing Recovery setting> */
1791 b10_a6 = bw6mhz_b10_a6; 2449 cxd2841er_write_regs(priv, I2C_SLVT,
1792 b10_d9 = bw6mhz_b10_d9; 2450 0x9F, nominalRate6bw[priv->xtal], 5);
1793 b17_38 = bw6mhz_b17_38; 2451 /* Group delay equaliser settings for
1794 d7val = 4; 2452 * ASCOT2D, ASCOT2E and ASCOT3 tuners
1795 iffreq = MAKE_IFFREQ_CONFIG(3.60); 2453 */
2454 cxd2841er_write_regs(priv, I2C_SLVT,
2455 0xA6, itbCoef6bw[priv->xtal], 14);
2456 /* <IF freq setting> */
2457 iffreq = MAKE_IFFREQ_CONFIG_XTAL(priv->xtal, 3.60);
2458 data[0] = (u8) ((iffreq >> 16) & 0xff);
2459 data[1] = (u8)((iffreq >> 8) & 0xff);
2460 data[2] = (u8)(iffreq & 0xff);
2461 cxd2841er_write_regs(priv, I2C_SLVT, 0xB6, data, 3);
2462 /* System bandwidth setting */
2463 cxd2841er_set_reg_bits(
2464 priv, I2C_SLVT, 0xD7, 0x04, 0x07);
2465
2466 /* Demod core latency setting */
2467 if (priv->xtal == SONY_XTAL_24000) {
2468 data[0] = 0x25;
2469 data[1] = 0x4C;
2470 } else {
2471 data[0] = 0x1F;
2472 data[1] = 0xDC;
2473 }
2474 cxd2841er_write_regs(priv, I2C_SLVT, 0xD9, data, 2);
2475
2476 /* Notch filter setting */
2477 data[0] = 0x00;
2478 data[1] = 0x03;
2479 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x17);
2480 cxd2841er_write_regs(priv, I2C_SLVT, 0x38, data, 2);
1796 break; 2481 break;
1797 case 5000000: 2482 case 5000000:
1798 b10_9f = bw5mhz_b10_9f; 2483 /* <Timing Recovery setting> */
1799 b10_a6 = bw5mhz_b10_a6; 2484 cxd2841er_write_regs(priv, I2C_SLVT,
1800 b10_d9 = bw5mhz_b10_d9; 2485 0x9F, nominalRate5bw[priv->xtal], 5);
1801 b17_38 = bw5mhz_b17_38; 2486 /* Group delay equaliser settings for
1802 d7val = 6; 2487 * ASCOT2D, ASCOT2E and ASCOT3 tuners
1803 iffreq = MAKE_IFFREQ_CONFIG(3.60); 2488 */
2489 cxd2841er_write_regs(priv, I2C_SLVT,
2490 0xA6, itbCoef5bw[priv->xtal], 14);
2491 /* <IF freq setting> */
2492 iffreq = MAKE_IFFREQ_CONFIG_XTAL(priv->xtal, 3.60);
2493 data[0] = (u8) ((iffreq >> 16) & 0xff);
2494 data[1] = (u8)((iffreq >> 8) & 0xff);
2495 data[2] = (u8)(iffreq & 0xff);
2496 cxd2841er_write_regs(priv, I2C_SLVT, 0xB6, data, 3);
2497 /* System bandwidth setting */
2498 cxd2841er_set_reg_bits(
2499 priv, I2C_SLVT, 0xD7, 0x06, 0x07);
2500
2501 /* Demod core latency setting */
2502 if (priv->xtal == SONY_XTAL_24000) {
2503 data[0] = 0x2C;
2504 data[1] = 0xC2;
2505 } else {
2506 data[0] = 0x26;
2507 data[1] = 0x3C;
2508 }
2509 cxd2841er_write_regs(priv, I2C_SLVT, 0xD9, data, 2);
2510
2511 /* Notch filter setting */
2512 data[0] = 0x00;
2513 data[1] = 0x03;
2514 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x17);
2515 cxd2841er_write_regs(priv, I2C_SLVT, 0x38, data, 2);
2516 break;
2517 }
2518
2519 return 0;
2520}
2521
2522static int cxd2841er_sleep_tc_to_active_i_band(
2523 struct cxd2841er_priv *priv, u32 bandwidth)
2524{
2525 u32 iffreq;
2526 u8 data[3];
2527
2528 /* TRCG Nominal Rate */
2529 u8 nominalRate8bw[3][5] = {
2530 {0x00, 0x00, 0x00, 0x00, 0x00}, /* 20.5MHz XTal */
2531 {0x11, 0xB8, 0x00, 0x00, 0x00}, /* 24MHz XTal */
2532 {0x00, 0x00, 0x00, 0x00, 0x00} /* 41MHz XTal */
2533 };
2534
2535 u8 nominalRate7bw[3][5] = {
2536 {0x00, 0x00, 0x00, 0x00, 0x00}, /* 20.5MHz XTal */
2537 {0x14, 0x40, 0x00, 0x00, 0x00}, /* 24MHz XTal */
2538 {0x00, 0x00, 0x00, 0x00, 0x00} /* 41MHz XTal */
2539 };
2540
2541 u8 nominalRate6bw[3][5] = {
2542 {0x14, 0x2E, 0x00, 0x00, 0x00}, /* 20.5MHz XTal */
2543 {0x17, 0xA0, 0x00, 0x00, 0x00}, /* 24MHz XTal */
2544 {0x14, 0x2E, 0x00, 0x00, 0x00} /* 41MHz XTal */
2545 };
2546
2547 u8 itbCoef8bw[3][14] = {
2548 {0x00}, /* 20.5MHz XTal */
2549 {0x2F, 0xBA, 0x28, 0x9B, 0x28, 0x9D, 0x28, 0xA1, 0x29,
2550 0xA5, 0x2A, 0xAC, 0x29, 0xB5}, /* 24MHz Xtal */
2551 {0x0}, /* 41MHz XTal */
2552 };
2553
2554 u8 itbCoef7bw[3][14] = {
2555 {0x00}, /* 20.5MHz XTal */
2556 {0x30, 0xB1, 0x29, 0x9A, 0x28, 0x9C, 0x28, 0xA0, 0x29,
2557 0xA2, 0x2B, 0xA6, 0x2B, 0xAD}, /* 24MHz Xtal */
2558 {0x00}, /* 41MHz XTal */
2559 };
2560
2561 u8 itbCoef6bw[3][14] = {
2562 {0x27, 0xA7, 0x28, 0xB3, 0x02, 0xF0, 0x01, 0xE8, 0x00,
2563 0xCF, 0x00, 0xE6, 0x23, 0xA4}, /* 20.5MHz XTal */
2564 {0x31, 0xA8, 0x29, 0x9B, 0x27, 0x9C, 0x28, 0x9E, 0x29,
2565 0xA4, 0x29, 0xA2, 0x29, 0xA8}, /* 24MHz Xtal */
2566 {0x27, 0xA7, 0x28, 0xB3, 0x02, 0xF0, 0x01, 0xE8, 0x00,
2567 0xCF, 0x00, 0xE6, 0x23, 0xA4}, /* 41MHz XTal */
2568 };
2569
2570 dev_dbg(&priv->i2c->dev, "%s() bandwidth=%u\n", __func__, bandwidth);
2571 /* Set SLV-T Bank : 0x10 */
2572 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
2573
2574 /* 20.5/41MHz Xtal support is not available
2575 * on ISDB-T 7MHzBW and 8MHzBW
2576 */
2577 if (priv->xtal != SONY_XTAL_24000 && bandwidth > 6000000) {
2578 dev_err(&priv->i2c->dev,
2579 "%s(): bandwidth %d supported only for 24MHz xtal\n",
2580 __func__, bandwidth);
2581 return -EINVAL;
2582 }
2583
2584 switch (bandwidth) {
2585 case 8000000:
2586 /* TRCG Nominal Rate */
2587 cxd2841er_write_regs(priv, I2C_SLVT,
2588 0x9F, nominalRate8bw[priv->xtal], 5);
2589 /* Group delay equaliser settings for ASCOT tuners optimized */
2590 cxd2841er_write_regs(priv, I2C_SLVT,
2591 0xA6, itbCoef8bw[priv->xtal], 14);
2592
2593 /* IF freq setting */
2594 iffreq = MAKE_IFFREQ_CONFIG_XTAL(priv->xtal, 4.75);
2595 data[0] = (u8) ((iffreq >> 16) & 0xff);
2596 data[1] = (u8)((iffreq >> 8) & 0xff);
2597 data[2] = (u8)(iffreq & 0xff);
2598 cxd2841er_write_regs(priv, I2C_SLVT, 0xB6, data, 3);
2599
2600 /* System bandwidth setting */
2601 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xd7, 0x0, 0x7);
2602
2603 /* Demod core latency setting */
2604 data[0] = 0x13;
2605 data[1] = 0xFC;
2606 cxd2841er_write_regs(priv, I2C_SLVT, 0xD9, data, 2);
2607
2608 /* Acquisition optimization setting */
2609 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x12);
2610 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x71, 0x03, 0x07);
2611 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x15);
2612 cxd2841er_write_reg(priv, I2C_SLVT, 0xBE, 0x03);
2613 break;
2614 case 7000000:
2615 /* TRCG Nominal Rate */
2616 cxd2841er_write_regs(priv, I2C_SLVT,
2617 0x9F, nominalRate7bw[priv->xtal], 5);
2618 /* Group delay equaliser settings for ASCOT tuners optimized */
2619 cxd2841er_write_regs(priv, I2C_SLVT,
2620 0xA6, itbCoef7bw[priv->xtal], 14);
2621
2622 /* IF freq setting */
2623 iffreq = MAKE_IFFREQ_CONFIG_XTAL(priv->xtal, 4.15);
2624 data[0] = (u8) ((iffreq >> 16) & 0xff);
2625 data[1] = (u8)((iffreq >> 8) & 0xff);
2626 data[2] = (u8)(iffreq & 0xff);
2627 cxd2841er_write_regs(priv, I2C_SLVT, 0xB6, data, 3);
2628
2629 /* System bandwidth setting */
2630 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xd7, 0x02, 0x7);
2631
2632 /* Demod core latency setting */
2633 data[0] = 0x1A;
2634 data[1] = 0xFA;
2635 cxd2841er_write_regs(priv, I2C_SLVT, 0xD9, data, 2);
2636
2637 /* Acquisition optimization setting */
2638 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x12);
2639 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x71, 0x03, 0x07);
2640 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x15);
2641 cxd2841er_write_reg(priv, I2C_SLVT, 0xBE, 0x02);
2642 break;
2643 case 6000000:
2644 /* TRCG Nominal Rate */
2645 cxd2841er_write_regs(priv, I2C_SLVT,
2646 0x9F, nominalRate6bw[priv->xtal], 5);
2647 /* Group delay equaliser settings for ASCOT tuners optimized */
2648 cxd2841er_write_regs(priv, I2C_SLVT,
2649 0xA6, itbCoef6bw[priv->xtal], 14);
2650
2651 /* IF freq setting */
2652 iffreq = MAKE_IFFREQ_CONFIG_XTAL(priv->xtal, 3.55);
2653 data[0] = (u8) ((iffreq >> 16) & 0xff);
2654 data[1] = (u8)((iffreq >> 8) & 0xff);
2655 data[2] = (u8)(iffreq & 0xff);
2656 cxd2841er_write_regs(priv, I2C_SLVT, 0xB6, data, 3);
2657
2658 /* System bandwidth setting */
2659 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xd7, 0x04, 0x7);
2660
2661 /* Demod core latency setting */
2662 if (priv->xtal == SONY_XTAL_24000) {
2663 data[0] = 0x1F;
2664 data[1] = 0x79;
2665 } else {
2666 data[0] = 0x1A;
2667 data[1] = 0xE2;
2668 }
2669 cxd2841er_write_regs(priv, I2C_SLVT, 0xD9, data, 2);
2670
2671 /* Acquisition optimization setting */
2672 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x12);
2673 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x71, 0x07, 0x07);
2674 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x15);
2675 cxd2841er_write_reg(priv, I2C_SLVT, 0xBE, 0x02);
1804 break; 2676 break;
1805 default: 2677 default:
1806 dev_dbg(&priv->i2c->dev, "%s(): invalid bandwidth %d\n", 2678 dev_dbg(&priv->i2c->dev, "%s(): invalid bandwidth %d\n",
1807 __func__, bandwidth); 2679 __func__, bandwidth);
1808 return -EINVAL; 2680 return -EINVAL;
1809 } 2681 }
1810 /* <IF freq setting> */
1811 b10_b6[0] = (u8) ((iffreq >> 16) & 0xff);
1812 b10_b6[1] = (u8)((iffreq >> 8) & 0xff);
1813 b10_b6[2] = (u8)(iffreq & 0xff);
1814 cxd2841er_write_regs(
1815 priv, I2C_SLVT, 0x9f, b10_9f, sizeof(bw8mhz_b10_9f));
1816 cxd2841er_write_regs(
1817 priv, I2C_SLVT, 0xa6, b10_a6, sizeof(bw8mhz_b10_a6));
1818 cxd2841er_write_regs(priv, I2C_SLVT, 0xb6, b10_b6, sizeof(b10_b6));
1819 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xd7, d7val, 0x7);
1820 cxd2841er_write_regs(
1821 priv, I2C_SLVT, 0xd9, b10_d9, sizeof(bw8mhz_b10_d9));
1822 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x17);
1823 cxd2841er_write_regs(
1824 priv, I2C_SLVT, 0x38, b17_38, sizeof(bw8mhz_b17_38));
1825 return 0; 2682 return 0;
1826} 2683}
1827 2684
@@ -1837,7 +2694,7 @@ static int cxd2841er_sleep_tc_to_active_c_band(struct cxd2841er_priv *priv,
1837 u8 b10_b6[3]; 2694 u8 b10_b6[3];
1838 u32 iffreq; 2695 u32 iffreq;
1839 2696
1840 dev_dbg(&priv->i2c->dev, "%s()\n", __func__); 2697 dev_dbg(&priv->i2c->dev, "%s() bw=%d\n", __func__, bandwidth);
1841 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10); 2698 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
1842 switch (bandwidth) { 2699 switch (bandwidth) {
1843 case 8000000: 2700 case 8000000:
@@ -1854,7 +2711,7 @@ static int cxd2841er_sleep_tc_to_active_c_band(struct cxd2841er_priv *priv,
1854 iffreq = MAKE_IFFREQ_CONFIG(3.7); 2711 iffreq = MAKE_IFFREQ_CONFIG(3.7);
1855 break; 2712 break;
1856 default: 2713 default:
1857 dev_dbg(&priv->i2c->dev, "%s(): unsupported bandwidth %d\n", 2714 dev_err(&priv->i2c->dev, "%s(): unsupported bandwidth %d\n",
1858 __func__, bandwidth); 2715 __func__, bandwidth);
1859 return -EINVAL; 2716 return -EINVAL;
1860 } 2717 }
@@ -1902,6 +2759,7 @@ static int cxd2841er_sleep_tc_to_active_t(struct cxd2841er_priv *priv,
1902 u32 bandwidth) 2759 u32 bandwidth)
1903{ 2760{
1904 u8 data[2] = { 0x09, 0x54 }; 2761 u8 data[2] = { 0x09, 0x54 };
2762 u8 data24m[3] = {0xDC, 0x6C, 0x00};
1905 2763
1906 dev_dbg(&priv->i2c->dev, "%s()\n", __func__); 2764 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
1907 cxd2841er_set_ts_clock_mode(priv, SYS_DVBT); 2765 cxd2841er_set_ts_clock_mode(priv, SYS_DVBT);
@@ -1919,7 +2777,11 @@ static int cxd2841er_sleep_tc_to_active_t(struct cxd2841er_priv *priv,
1919 cxd2841er_write_reg(priv, I2C_SLVT, 0x30, 0x00); 2777 cxd2841er_write_reg(priv, I2C_SLVT, 0x30, 0x00);
1920 /* Enable ADC 1 */ 2778 /* Enable ADC 1 */
1921 cxd2841er_write_reg(priv, I2C_SLVT, 0x41, 0x1a); 2779 cxd2841er_write_reg(priv, I2C_SLVT, 0x41, 0x1a);
1922 /* xtal freq 20.5MHz */ 2780 /* Enable ADC 2 & 3 */
2781 if (priv->xtal == SONY_XTAL_41000) {
2782 data[0] = 0x0A;
2783 data[1] = 0xD4;
2784 }
1923 cxd2841er_write_regs(priv, I2C_SLVT, 0x43, data, 2); 2785 cxd2841er_write_regs(priv, I2C_SLVT, 0x43, data, 2);
1924 /* Enable ADC 4 */ 2786 /* Enable ADC 4 */
1925 cxd2841er_write_reg(priv, I2C_SLVX, 0x18, 0x00); 2787 cxd2841er_write_reg(priv, I2C_SLVX, 0x18, 0x00);
@@ -1947,6 +2809,15 @@ static int cxd2841er_sleep_tc_to_active_t(struct cxd2841er_priv *priv,
1947 /* TSIF setting */ 2809 /* TSIF setting */
1948 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xce, 0x01, 0x01); 2810 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xce, 0x01, 0x01);
1949 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xcf, 0x01, 0x01); 2811 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xcf, 0x01, 0x01);
2812
2813 if (priv->xtal == SONY_XTAL_24000) {
2814 /* Set SLV-T Bank : 0x10 */
2815 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
2816 cxd2841er_write_reg(priv, I2C_SLVT, 0xBF, 0x60);
2817 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x18);
2818 cxd2841er_write_regs(priv, I2C_SLVT, 0x24, data24m, 3);
2819 }
2820
1950 cxd2841er_sleep_tc_to_active_t_band(priv, bandwidth); 2821 cxd2841er_sleep_tc_to_active_t_band(priv, bandwidth);
1951 /* Set SLV-T Bank : 0x00 */ 2822 /* Set SLV-T Bank : 0x00 */
1952 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00); 2823 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
@@ -1961,7 +2832,7 @@ static int cxd2841er_sleep_tc_to_active_t(struct cxd2841er_priv *priv,
1961static int cxd2841er_sleep_tc_to_active_t2(struct cxd2841er_priv *priv, 2832static int cxd2841er_sleep_tc_to_active_t2(struct cxd2841er_priv *priv,
1962 u32 bandwidth) 2833 u32 bandwidth)
1963{ 2834{
1964 u8 data[2] = { 0x09, 0x54 }; 2835 u8 data[MAX_WRITE_REGSIZE];
1965 2836
1966 dev_dbg(&priv->i2c->dev, "%s()\n", __func__); 2837 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
1967 cxd2841er_set_ts_clock_mode(priv, SYS_DVBT2); 2838 cxd2841er_set_ts_clock_mode(priv, SYS_DVBT2);
@@ -1974,12 +2845,21 @@ static int cxd2841er_sleep_tc_to_active_t2(struct cxd2841er_priv *priv,
1974 /* Enable demod clock */ 2845 /* Enable demod clock */
1975 cxd2841er_write_reg(priv, I2C_SLVT, 0x2c, 0x01); 2846 cxd2841er_write_reg(priv, I2C_SLVT, 0x2c, 0x01);
1976 /* Disable RF level monitor */ 2847 /* Disable RF level monitor */
2848 cxd2841er_write_reg(priv, I2C_SLVT, 0x59, 0x00);
1977 cxd2841er_write_reg(priv, I2C_SLVT, 0x2f, 0x00); 2849 cxd2841er_write_reg(priv, I2C_SLVT, 0x2f, 0x00);
1978 /* Enable ADC clock */ 2850 /* Enable ADC clock */
1979 cxd2841er_write_reg(priv, I2C_SLVT, 0x30, 0x00); 2851 cxd2841er_write_reg(priv, I2C_SLVT, 0x30, 0x00);
1980 /* Enable ADC 1 */ 2852 /* Enable ADC 1 */
1981 cxd2841er_write_reg(priv, I2C_SLVT, 0x41, 0x1a); 2853 cxd2841er_write_reg(priv, I2C_SLVT, 0x41, 0x1a);
1982 /* xtal freq 20.5MHz */ 2854
2855 if (priv->xtal == SONY_XTAL_41000) {
2856 data[0] = 0x0A;
2857 data[1] = 0xD4;
2858 } else {
2859 data[0] = 0x09;
2860 data[1] = 0x54;
2861 }
2862
1983 cxd2841er_write_regs(priv, I2C_SLVT, 0x43, data, 2); 2863 cxd2841er_write_regs(priv, I2C_SLVT, 0x43, data, 2);
1984 /* Enable ADC 4 */ 2864 /* Enable ADC 4 */
1985 cxd2841er_write_reg(priv, I2C_SLVX, 0x18, 0x00); 2865 cxd2841er_write_reg(priv, I2C_SLVX, 0x18, 0x00);
@@ -2002,6 +2882,10 @@ static int cxd2841er_sleep_tc_to_active_t2(struct cxd2841er_priv *priv,
2002 /* Set SLV-T Bank : 0x2b */ 2882 /* Set SLV-T Bank : 0x2b */
2003 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x2b); 2883 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x2b);
2004 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x76, 0x20, 0x70); 2884 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x76, 0x20, 0x70);
2885 /* Set SLV-T Bank : 0x23 */
2886 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x23);
2887 /* L1 Control setting */
2888 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xE6, 0x00, 0x03);
2005 /* Set SLV-T Bank : 0x00 */ 2889 /* Set SLV-T Bank : 0x00 */
2006 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00); 2890 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
2007 /* TSIF setting */ 2891 /* TSIF setting */
@@ -2020,6 +2904,72 @@ static int cxd2841er_sleep_tc_to_active_t2(struct cxd2841er_priv *priv,
2020 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x2b); 2904 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x2b);
2021 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x11, 0x20, 0x3f); 2905 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x11, 0x20, 0x3f);
2022 2906
2907 /* 24MHz Xtal setting */
2908 if (priv->xtal == SONY_XTAL_24000) {
2909 /* Set SLV-T Bank : 0x11 */
2910 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x11);
2911 data[0] = 0xEB;
2912 data[1] = 0x03;
2913 data[2] = 0x3B;
2914 cxd2841er_write_regs(priv, I2C_SLVT, 0x33, data, 3);
2915
2916 /* Set SLV-T Bank : 0x20 */
2917 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x20);
2918 data[0] = 0x5E;
2919 data[1] = 0x5E;
2920 data[2] = 0x47;
2921 cxd2841er_write_regs(priv, I2C_SLVT, 0x95, data, 3);
2922
2923 cxd2841er_write_reg(priv, I2C_SLVT, 0x99, 0x18);
2924
2925 data[0] = 0x3F;
2926 data[1] = 0xFF;
2927 cxd2841er_write_regs(priv, I2C_SLVT, 0xD9, data, 2);
2928
2929 /* Set SLV-T Bank : 0x24 */
2930 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x24);
2931 data[0] = 0x0B;
2932 data[1] = 0x72;
2933 cxd2841er_write_regs(priv, I2C_SLVT, 0x34, data, 2);
2934
2935 data[0] = 0x93;
2936 data[1] = 0xF3;
2937 data[2] = 0x00;
2938 cxd2841er_write_regs(priv, I2C_SLVT, 0xD2, data, 3);
2939
2940 data[0] = 0x05;
2941 data[1] = 0xB8;
2942 data[2] = 0xD8;
2943 cxd2841er_write_regs(priv, I2C_SLVT, 0xDD, data, 3);
2944
2945 cxd2841er_write_reg(priv, I2C_SLVT, 0xE0, 0x00);
2946
2947 /* Set SLV-T Bank : 0x25 */
2948 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x25);
2949 cxd2841er_write_reg(priv, I2C_SLVT, 0xED, 0x60);
2950
2951 /* Set SLV-T Bank : 0x27 */
2952 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x27);
2953 cxd2841er_write_reg(priv, I2C_SLVT, 0xFA, 0x34);
2954
2955 /* Set SLV-T Bank : 0x2B */
2956 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x2B);
2957 cxd2841er_write_reg(priv, I2C_SLVT, 0x4B, 0x2F);
2958 cxd2841er_write_reg(priv, I2C_SLVT, 0x9E, 0x0E);
2959
2960 /* Set SLV-T Bank : 0x2D */
2961 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x2D);
2962 data[0] = 0x89;
2963 data[1] = 0x89;
2964 cxd2841er_write_regs(priv, I2C_SLVT, 0x24, data, 2);
2965
2966 /* Set SLV-T Bank : 0x5E */
2967 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x5E);
2968 data[0] = 0x24;
2969 data[1] = 0x95;
2970 cxd2841er_write_regs(priv, I2C_SLVT, 0x8C, data, 2);
2971 }
2972
2023 cxd2841er_sleep_tc_to_active_t2_band(priv, bandwidth); 2973 cxd2841er_sleep_tc_to_active_t2_band(priv, bandwidth);
2024 2974
2025 /* Set SLV-T Bank : 0x00 */ 2975 /* Set SLV-T Bank : 0x00 */
@@ -2032,6 +2982,84 @@ static int cxd2841er_sleep_tc_to_active_t2(struct cxd2841er_priv *priv,
2032 return 0; 2982 return 0;
2033} 2983}
2034 2984
2985/* ISDB-Tb part */
2986static int cxd2841er_sleep_tc_to_active_i(struct cxd2841er_priv *priv,
2987 u32 bandwidth)
2988{
2989 u8 data[2] = { 0x09, 0x54 };
2990 u8 data24m[2] = {0x60, 0x00};
2991 u8 data24m2[3] = {0xB7, 0x1B, 0x00};
2992
2993 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
2994 cxd2841er_set_ts_clock_mode(priv, SYS_DVBT);
2995 /* Set SLV-X Bank : 0x00 */
2996 cxd2841er_write_reg(priv, I2C_SLVX, 0x00, 0x00);
2997 /* Set demod mode */
2998 cxd2841er_write_reg(priv, I2C_SLVX, 0x17, 0x06);
2999 /* Set SLV-T Bank : 0x00 */
3000 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
3001 /* Enable demod clock */
3002 cxd2841er_write_reg(priv, I2C_SLVT, 0x2c, 0x01);
3003 /* Enable RF level monitor */
3004 cxd2841er_write_reg(priv, I2C_SLVT, 0x2f, 0x01);
3005 cxd2841er_write_reg(priv, I2C_SLVT, 0x59, 0x01);
3006 /* Enable ADC clock */
3007 cxd2841er_write_reg(priv, I2C_SLVT, 0x30, 0x00);
3008 /* Enable ADC 1 */
3009 cxd2841er_write_reg(priv, I2C_SLVT, 0x41, 0x1a);
3010 /* xtal freq 20.5MHz or 24M */
3011 cxd2841er_write_regs(priv, I2C_SLVT, 0x43, data, 2);
3012 /* Enable ADC 4 */
3013 cxd2841er_write_reg(priv, I2C_SLVX, 0x18, 0x00);
3014 /* ASCOT setting ON */
3015 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xa5, 0x01, 0x01);
3016 /* FEC Auto Recovery setting */
3017 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x30, 0x01, 0x01);
3018 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x31, 0x00, 0x01);
3019 /* ISDB-T initial setting */
3020 /* Set SLV-T Bank : 0x00 */
3021 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
3022 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xce, 0x00, 0x01);
3023 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xcf, 0x00, 0x01);
3024 /* Set SLV-T Bank : 0x10 */
3025 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
3026 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x69, 0x04, 0x07);
3027 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x6B, 0x03, 0x07);
3028 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x9D, 0x50, 0xFF);
3029 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xD3, 0x06, 0x1F);
3030 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xED, 0x00, 0x01);
3031 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xE2, 0xCE, 0x80);
3032 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xF2, 0x13, 0x10);
3033 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xDE, 0x2E, 0x3F);
3034 /* Set SLV-T Bank : 0x15 */
3035 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x15);
3036 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xDE, 0x02, 0x03);
3037 /* Set SLV-T Bank : 0x1E */
3038 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x1E);
3039 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x73, 0x68, 0xFF);
3040 /* Set SLV-T Bank : 0x63 */
3041 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x63);
3042 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x81, 0x00, 0x01);
3043
3044 /* for xtal 24MHz */
3045 /* Set SLV-T Bank : 0x10 */
3046 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
3047 cxd2841er_write_regs(priv, I2C_SLVT, 0xBF, data24m, 2);
3048 /* Set SLV-T Bank : 0x60 */
3049 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x60);
3050 cxd2841er_write_regs(priv, I2C_SLVT, 0xA8, data24m2, 3);
3051
3052 cxd2841er_sleep_tc_to_active_i_band(priv, bandwidth);
3053 /* Set SLV-T Bank : 0x00 */
3054 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
3055 /* Disable HiZ Setting 1 */
3056 cxd2841er_write_reg(priv, I2C_SLVT, 0x80, 0x28);
3057 /* Disable HiZ Setting 2 */
3058 cxd2841er_write_reg(priv, I2C_SLVT, 0x81, 0x00);
3059 priv->state = STATE_ACTIVE_TC;
3060 return 0;
3061}
3062
2035static int cxd2841er_sleep_tc_to_active_c(struct cxd2841er_priv *priv, 3063static int cxd2841er_sleep_tc_to_active_c(struct cxd2841er_priv *priv,
2036 u32 bandwidth) 3064 u32 bandwidth)
2037{ 3065{
@@ -2079,7 +3107,7 @@ static int cxd2841er_sleep_tc_to_active_c(struct cxd2841er_priv *priv,
2079 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xce, 0x01, 0x01); 3107 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xce, 0x01, 0x01);
2080 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xcf, 0x01, 0x01); 3108 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xcf, 0x01, 0x01);
2081 3109
2082 cxd2841er_sleep_tc_to_active_c_band(priv, 8000000); 3110 cxd2841er_sleep_tc_to_active_c_band(priv, bandwidth);
2083 /* Set SLV-T Bank : 0x00 */ 3111 /* Set SLV-T Bank : 0x00 */
2084 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00); 3112 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
2085 /* Disable HiZ Setting 1 */ 3113 /* Disable HiZ Setting 1 */
@@ -2094,8 +3122,6 @@ static int cxd2841er_get_frontend(struct dvb_frontend *fe,
2094 struct dtv_frontend_properties *p) 3122 struct dtv_frontend_properties *p)
2095{ 3123{
2096 enum fe_status status = 0; 3124 enum fe_status status = 0;
2097 u16 strength = 0, snr = 0;
2098 u32 errors = 0, ber = 0;
2099 struct cxd2841er_priv *priv = fe->demodulator_priv; 3125 struct cxd2841er_priv *priv = fe->demodulator_priv;
2100 3126
2101 dev_dbg(&priv->i2c->dev, "%s()\n", __func__); 3127 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
@@ -2104,32 +3130,18 @@ static int cxd2841er_get_frontend(struct dvb_frontend *fe,
2104 else if (priv->state == STATE_ACTIVE_TC) 3130 else if (priv->state == STATE_ACTIVE_TC)
2105 cxd2841er_read_status_tc(fe, &status); 3131 cxd2841er_read_status_tc(fe, &status);
2106 3132
3133 cxd2841er_read_signal_strength(fe);
3134
2107 if (status & FE_HAS_LOCK) { 3135 if (status & FE_HAS_LOCK) {
2108 cxd2841er_read_signal_strength(fe, &strength); 3136 cxd2841er_read_snr(fe);
2109 p->strength.len = 1; 3137 cxd2841er_read_ucblocks(fe);
2110 p->strength.stat[0].scale = FE_SCALE_RELATIVE; 3138
2111 p->strength.stat[0].uvalue = strength; 3139 cxd2841er_read_ber(fe);
2112 cxd2841er_read_snr(fe, &snr);
2113 p->cnr.len = 1;
2114 p->cnr.stat[0].scale = FE_SCALE_DECIBEL;
2115 p->cnr.stat[0].svalue = snr;
2116 cxd2841er_read_ucblocks(fe, &errors);
2117 p->block_error.len = 1;
2118 p->block_error.stat[0].scale = FE_SCALE_COUNTER;
2119 p->block_error.stat[0].uvalue = errors;
2120 cxd2841er_read_ber(fe, &ber);
2121 p->post_bit_error.len = 1;
2122 p->post_bit_error.stat[0].scale = FE_SCALE_COUNTER;
2123 p->post_bit_error.stat[0].uvalue = ber;
2124 } else { 3140 } else {
2125 p->strength.len = 1;
2126 p->strength.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
2127 p->cnr.len = 1;
2128 p->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE; 3141 p->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
2129 p->block_error.len = 1;
2130 p->block_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE; 3142 p->block_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
2131 p->post_bit_error.len = 1;
2132 p->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE; 3143 p->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
3144 p->post_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
2133 } 3145 }
2134 return 0; 3146 return 0;
2135} 3147}
@@ -2142,10 +3154,10 @@ static int cxd2841er_set_frontend_s(struct dvb_frontend *fe)
2142 struct dtv_frontend_properties *p = &fe->dtv_property_cache; 3154 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
2143 u32 symbol_rate = p->symbol_rate/1000; 3155 u32 symbol_rate = p->symbol_rate/1000;
2144 3156
2145 dev_dbg(&priv->i2c->dev, "%s(): %s frequency=%d symbol_rate=%d\n", 3157 dev_dbg(&priv->i2c->dev, "%s(): %s frequency=%d symbol_rate=%d xtal=%d\n",
2146 __func__, 3158 __func__,
2147 (p->delivery_system == SYS_DVBS ? "DVB-S" : "DVB-S2"), 3159 (p->delivery_system == SYS_DVBS ? "DVB-S" : "DVB-S2"),
2148 p->frequency, symbol_rate); 3160 p->frequency, symbol_rate, priv->xtal);
2149 switch (priv->state) { 3161 switch (priv->state) {
2150 case STATE_SLEEP_S: 3162 case STATE_SLEEP_S:
2151 ret = cxd2841er_sleep_s_to_active_s( 3163 ret = cxd2841er_sleep_s_to_active_s(
@@ -2189,6 +3201,13 @@ static int cxd2841er_set_frontend_s(struct dvb_frontend *fe)
2189 __func__, carr_offset); 3201 __func__, carr_offset);
2190 } 3202 }
2191done: 3203done:
3204 /* Reset stats */
3205 p->strength.stat[0].scale = FE_SCALE_RELATIVE;
3206 p->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
3207 p->block_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
3208 p->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
3209 p->post_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
3210
2192 return ret; 3211 return ret;
2193} 3212}
2194 3213
@@ -2199,7 +3218,8 @@ static int cxd2841er_set_frontend_tc(struct dvb_frontend *fe)
2199 struct cxd2841er_priv *priv = fe->demodulator_priv; 3218 struct cxd2841er_priv *priv = fe->demodulator_priv;
2200 struct dtv_frontend_properties *p = &fe->dtv_property_cache; 3219 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
2201 3220
2202 dev_dbg(&priv->i2c->dev, "%s()\n", __func__); 3221 dev_dbg(&priv->i2c->dev, "%s() delivery_system=%d bandwidth_hz=%d\n",
3222 __func__, p->delivery_system, p->bandwidth_hz);
2203 if (p->delivery_system == SYS_DVBT) { 3223 if (p->delivery_system == SYS_DVBT) {
2204 priv->system = SYS_DVBT; 3224 priv->system = SYS_DVBT;
2205 switch (priv->state) { 3225 switch (priv->state) {
@@ -2233,9 +3253,33 @@ static int cxd2841er_set_frontend_tc(struct dvb_frontend *fe)
2233 __func__, priv->state); 3253 __func__, priv->state);
2234 ret = -EINVAL; 3254 ret = -EINVAL;
2235 } 3255 }
3256 } else if (p->delivery_system == SYS_ISDBT) {
3257 priv->system = SYS_ISDBT;
3258 switch (priv->state) {
3259 case STATE_SLEEP_TC:
3260 ret = cxd2841er_sleep_tc_to_active_i(
3261 priv, p->bandwidth_hz);
3262 break;
3263 case STATE_ACTIVE_TC:
3264 ret = cxd2841er_retune_active(priv, p);
3265 break;
3266 default:
3267 dev_dbg(&priv->i2c->dev, "%s(): invalid state %d\n",
3268 __func__, priv->state);
3269 ret = -EINVAL;
3270 }
2236 } else if (p->delivery_system == SYS_DVBC_ANNEX_A || 3271 } else if (p->delivery_system == SYS_DVBC_ANNEX_A ||
2237 p->delivery_system == SYS_DVBC_ANNEX_C) { 3272 p->delivery_system == SYS_DVBC_ANNEX_C) {
2238 priv->system = SYS_DVBC_ANNEX_A; 3273 priv->system = SYS_DVBC_ANNEX_A;
3274 /* correct bandwidth */
3275 if (p->bandwidth_hz != 6000000 &&
3276 p->bandwidth_hz != 7000000 &&
3277 p->bandwidth_hz != 8000000) {
3278 p->bandwidth_hz = 8000000;
3279 dev_dbg(&priv->i2c->dev, "%s(): forcing bandwidth to %d\n",
3280 __func__, p->bandwidth_hz);
3281 }
3282
2239 switch (priv->state) { 3283 switch (priv->state) {
2240 case STATE_SLEEP_TC: 3284 case STATE_SLEEP_TC:
2241 ret = cxd2841er_sleep_tc_to_active_c( 3285 ret = cxd2841er_sleep_tc_to_active_c(
@@ -2321,7 +3365,8 @@ static int cxd2841er_tune_tc(struct dvb_frontend *fe,
2321 struct cxd2841er_priv *priv = fe->demodulator_priv; 3365 struct cxd2841er_priv *priv = fe->demodulator_priv;
2322 struct dtv_frontend_properties *p = &fe->dtv_property_cache; 3366 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
2323 3367
2324 dev_dbg(&priv->i2c->dev, "%s(): re_tune %d\n", __func__, re_tune); 3368 dev_dbg(&priv->i2c->dev, "%s(): re_tune %d bandwidth=%d\n", __func__,
3369 re_tune, p->bandwidth_hz);
2325 if (re_tune) { 3370 if (re_tune) {
2326 ret = cxd2841er_set_frontend_tc(fe); 3371 ret = cxd2841er_set_frontend_tc(fe);
2327 if (ret) 3372 if (ret)
@@ -2329,7 +3374,16 @@ static int cxd2841er_tune_tc(struct dvb_frontend *fe,
2329 cxd2841er_read_status_tc(fe, status); 3374 cxd2841er_read_status_tc(fe, status);
2330 if (*status & FE_HAS_LOCK) { 3375 if (*status & FE_HAS_LOCK) {
2331 switch (priv->system) { 3376 switch (priv->system) {
3377 case SYS_ISDBT:
3378 ret = cxd2841er_get_carrier_offset_i(
3379 priv, p->bandwidth_hz,
3380 &carrier_offset);
3381 break;
2332 case SYS_DVBT: 3382 case SYS_DVBT:
3383 ret = cxd2841er_get_carrier_offset_t(
3384 priv, p->bandwidth_hz,
3385 &carrier_offset);
3386 break;
2333 case SYS_DVBT2: 3387 case SYS_DVBT2:
2334 ret = cxd2841er_get_carrier_offset_t2( 3388 ret = cxd2841er_get_carrier_offset_t2(
2335 priv, p->bandwidth_hz, 3389 priv, p->bandwidth_hz,
@@ -2382,6 +3436,9 @@ static int cxd2841er_sleep_tc(struct dvb_frontend *fe)
2382 case SYS_DVBT2: 3436 case SYS_DVBT2:
2383 cxd2841er_active_t2_to_sleep_tc(priv); 3437 cxd2841er_active_t2_to_sleep_tc(priv);
2384 break; 3438 break;
3439 case SYS_ISDBT:
3440 cxd2841er_active_i_to_sleep_tc(priv);
3441 break;
2385 case SYS_DVBC_ANNEX_A: 3442 case SYS_DVBC_ANNEX_A:
2386 cxd2841er_active_c_to_sleep_tc(priv); 3443 cxd2841er_active_c_to_sleep_tc(priv);
2387 break; 3444 break;
@@ -2512,23 +3569,57 @@ static enum dvbfe_algo cxd2841er_get_algo(struct dvb_frontend *fe)
2512 return DVBFE_ALGO_HW; 3569 return DVBFE_ALGO_HW;
2513} 3570}
2514 3571
3572static void cxd2841er_init_stats(struct dvb_frontend *fe)
3573{
3574 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
3575
3576 p->strength.len = 1;
3577 p->strength.stat[0].scale = FE_SCALE_RELATIVE;
3578 p->cnr.len = 1;
3579 p->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
3580 p->block_error.len = 1;
3581 p->block_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
3582 p->post_bit_error.len = 1;
3583 p->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
3584 p->post_bit_count.len = 1;
3585 p->post_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
3586}
3587
3588
2515static int cxd2841er_init_s(struct dvb_frontend *fe) 3589static int cxd2841er_init_s(struct dvb_frontend *fe)
2516{ 3590{
2517 struct cxd2841er_priv *priv = fe->demodulator_priv; 3591 struct cxd2841er_priv *priv = fe->demodulator_priv;
2518 3592
3593 /* sanity. force demod to SHUTDOWN state */
3594 if (priv->state == STATE_SLEEP_S) {
3595 dev_dbg(&priv->i2c->dev, "%s() forcing sleep->shutdown\n",
3596 __func__);
3597 cxd2841er_sleep_s_to_shutdown(priv);
3598 } else if (priv->state == STATE_ACTIVE_S) {
3599 dev_dbg(&priv->i2c->dev, "%s() forcing active->sleep->shutdown\n",
3600 __func__);
3601 cxd2841er_active_s_to_sleep_s(priv);
3602 cxd2841er_sleep_s_to_shutdown(priv);
3603 }
3604
2519 dev_dbg(&priv->i2c->dev, "%s()\n", __func__); 3605 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
2520 cxd2841er_shutdown_to_sleep_s(priv); 3606 cxd2841er_shutdown_to_sleep_s(priv);
2521 /* SONY_DEMOD_CONFIG_SAT_IFAGCNEG set to 1 */ 3607 /* SONY_DEMOD_CONFIG_SAT_IFAGCNEG set to 1 */
2522 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0xa0); 3608 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0xa0);
2523 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xb9, 0x01, 0x01); 3609 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xb9, 0x01, 0x01);
3610
3611 cxd2841er_init_stats(fe);
3612
2524 return 0; 3613 return 0;
2525} 3614}
2526 3615
2527static int cxd2841er_init_tc(struct dvb_frontend *fe) 3616static int cxd2841er_init_tc(struct dvb_frontend *fe)
2528{ 3617{
2529 struct cxd2841er_priv *priv = fe->demodulator_priv; 3618 struct cxd2841er_priv *priv = fe->demodulator_priv;
3619 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
2530 3620
2531 dev_dbg(&priv->i2c->dev, "%s()\n", __func__); 3621 dev_dbg(&priv->i2c->dev, "%s() bandwidth_hz=%d\n",
3622 __func__, p->bandwidth_hz);
2532 cxd2841er_shutdown_to_sleep_tc(priv); 3623 cxd2841er_shutdown_to_sleep_tc(priv);
2533 /* SONY_DEMOD_CONFIG_IFAGCNEG = 1 */ 3624 /* SONY_DEMOD_CONFIG_IFAGCNEG = 1 */
2534 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10); 3625 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
@@ -2538,12 +3629,14 @@ static int cxd2841er_init_tc(struct dvb_frontend *fe)
2538 /* SONY_DEMOD_CONFIG_PARALLEL_SEL = 1 */ 3629 /* SONY_DEMOD_CONFIG_PARALLEL_SEL = 1 */
2539 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00); 3630 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
2540 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xc4, 0x00, 0x80); 3631 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xc4, 0x00, 0x80);
3632
3633 cxd2841er_init_stats(fe);
3634
2541 return 0; 3635 return 0;
2542} 3636}
2543 3637
2544static struct dvb_frontend_ops cxd2841er_dvbs_s2_ops; 3638static struct dvb_frontend_ops cxd2841er_dvbs_s2_ops;
2545static struct dvb_frontend_ops cxd2841er_dvbt_t2_ops; 3639static struct dvb_frontend_ops cxd2841er_t_c_ops;
2546static struct dvb_frontend_ops cxd2841er_dvbc_ops;
2547 3640
2548static struct dvb_frontend *cxd2841er_attach(struct cxd2841er_config *cfg, 3641static struct dvb_frontend *cxd2841er_attach(struct cxd2841er_config *cfg,
2549 struct i2c_adapter *i2c, 3642 struct i2c_adapter *i2c,
@@ -2551,6 +3644,7 @@ static struct dvb_frontend *cxd2841er_attach(struct cxd2841er_config *cfg,
2551{ 3644{
2552 u8 chip_id = 0; 3645 u8 chip_id = 0;
2553 const char *type; 3646 const char *type;
3647 const char *name;
2554 struct cxd2841er_priv *priv = NULL; 3648 struct cxd2841er_priv *priv = NULL;
2555 3649
2556 /* allocate memory for the internal state */ 3650 /* allocate memory for the internal state */
@@ -2561,46 +3655,49 @@ static struct dvb_frontend *cxd2841er_attach(struct cxd2841er_config *cfg,
2561 priv->config = cfg; 3655 priv->config = cfg;
2562 priv->i2c_addr_slvx = (cfg->i2c_addr + 4) >> 1; 3656 priv->i2c_addr_slvx = (cfg->i2c_addr + 4) >> 1;
2563 priv->i2c_addr_slvt = (cfg->i2c_addr) >> 1; 3657 priv->i2c_addr_slvt = (cfg->i2c_addr) >> 1;
2564 /* create dvb_frontend */ 3658 priv->xtal = cfg->xtal;
2565 switch (system) {
2566 case SYS_DVBS:
2567 memcpy(&priv->frontend.ops,
2568 &cxd2841er_dvbs_s2_ops,
2569 sizeof(struct dvb_frontend_ops));
2570 type = "S/S2";
2571 break;
2572 case SYS_DVBT:
2573 memcpy(&priv->frontend.ops,
2574 &cxd2841er_dvbt_t2_ops,
2575 sizeof(struct dvb_frontend_ops));
2576 type = "T/T2";
2577 break;
2578 case SYS_DVBC_ANNEX_A:
2579 memcpy(&priv->frontend.ops,
2580 &cxd2841er_dvbc_ops,
2581 sizeof(struct dvb_frontend_ops));
2582 type = "C/C2";
2583 break;
2584 default:
2585 kfree(priv);
2586 return NULL;
2587 }
2588 priv->frontend.demodulator_priv = priv; 3659 priv->frontend.demodulator_priv = priv;
2589 dev_info(&priv->i2c->dev, 3660 dev_info(&priv->i2c->dev,
2590 "%s(): attaching CXD2841ER DVB-%s frontend\n",
2591 __func__, type);
2592 dev_info(&priv->i2c->dev,
2593 "%s(): I2C adapter %p SLVX addr %x SLVT addr %x\n", 3661 "%s(): I2C adapter %p SLVX addr %x SLVT addr %x\n",
2594 __func__, priv->i2c, 3662 __func__, priv->i2c,
2595 priv->i2c_addr_slvx, priv->i2c_addr_slvt); 3663 priv->i2c_addr_slvx, priv->i2c_addr_slvt);
2596 chip_id = cxd2841er_chip_id(priv); 3664 chip_id = cxd2841er_chip_id(priv);
2597 if (chip_id != CXD2841ER_CHIP_ID) { 3665 switch (chip_id) {
3666 case CXD2841ER_CHIP_ID:
3667 snprintf(cxd2841er_t_c_ops.info.name, 128,
3668 "Sony CXD2841ER DVB-T/T2/C demodulator");
3669 name = "CXD2841ER";
3670 break;
3671 case CXD2854ER_CHIP_ID:
3672 snprintf(cxd2841er_t_c_ops.info.name, 128,
3673 "Sony CXD2854ER DVB-T/T2/C and ISDB-T demodulator");
3674 cxd2841er_t_c_ops.delsys[3] = SYS_ISDBT;
3675 name = "CXD2854ER";
3676 break;
3677 default:
2598 dev_err(&priv->i2c->dev, "%s(): invalid chip ID 0x%02x\n", 3678 dev_err(&priv->i2c->dev, "%s(): invalid chip ID 0x%02x\n",
2599 __func__, chip_id); 3679 __func__, chip_id);
2600 priv->frontend.demodulator_priv = NULL; 3680 priv->frontend.demodulator_priv = NULL;
2601 kfree(priv); 3681 kfree(priv);
2602 return NULL; 3682 return NULL;
2603 } 3683 }
3684
3685 /* create dvb_frontend */
3686 if (system == SYS_DVBS) {
3687 memcpy(&priv->frontend.ops,
3688 &cxd2841er_dvbs_s2_ops,
3689 sizeof(struct dvb_frontend_ops));
3690 type = "S/S2";
3691 } else {
3692 memcpy(&priv->frontend.ops,
3693 &cxd2841er_t_c_ops,
3694 sizeof(struct dvb_frontend_ops));
3695 type = "T/T2/C/ISDB-T";
3696 }
3697
3698 dev_info(&priv->i2c->dev,
3699 "%s(): attaching %s DVB-%s frontend\n",
3700 __func__, name, type);
2604 dev_info(&priv->i2c->dev, "%s(): chip ID 0x%02x OK.\n", 3701 dev_info(&priv->i2c->dev, "%s(): chip ID 0x%02x OK.\n",
2605 __func__, chip_id); 3702 __func__, chip_id);
2606 return &priv->frontend; 3703 return &priv->frontend;
@@ -2613,19 +3710,12 @@ struct dvb_frontend *cxd2841er_attach_s(struct cxd2841er_config *cfg,
2613} 3710}
2614EXPORT_SYMBOL(cxd2841er_attach_s); 3711EXPORT_SYMBOL(cxd2841er_attach_s);
2615 3712
2616struct dvb_frontend *cxd2841er_attach_t(struct cxd2841er_config *cfg, 3713struct dvb_frontend *cxd2841er_attach_t_c(struct cxd2841er_config *cfg,
2617 struct i2c_adapter *i2c) 3714 struct i2c_adapter *i2c)
2618{ 3715{
2619 return cxd2841er_attach(cfg, i2c, SYS_DVBT); 3716 return cxd2841er_attach(cfg, i2c, 0);
2620} 3717}
2621EXPORT_SYMBOL(cxd2841er_attach_t); 3718EXPORT_SYMBOL(cxd2841er_attach_t_c);
2622
2623struct dvb_frontend *cxd2841er_attach_c(struct cxd2841er_config *cfg,
2624 struct i2c_adapter *i2c)
2625{
2626 return cxd2841er_attach(cfg, i2c, SYS_DVBC_ANNEX_A);
2627}
2628EXPORT_SYMBOL(cxd2841er_attach_c);
2629 3719
2630static struct dvb_frontend_ops cxd2841er_dvbs_s2_ops = { 3720static struct dvb_frontend_ops cxd2841er_dvbs_s2_ops = {
2631 .delsys = { SYS_DVBS, SYS_DVBS2 }, 3721 .delsys = { SYS_DVBS, SYS_DVBS2 },
@@ -2655,10 +3745,10 @@ static struct dvb_frontend_ops cxd2841er_dvbs_s2_ops = {
2655 .tune = cxd2841er_tune_s 3745 .tune = cxd2841er_tune_s
2656}; 3746};
2657 3747
2658static struct dvb_frontend_ops cxd2841er_dvbt_t2_ops = { 3748static struct dvb_frontend_ops cxd2841er_t_c_ops = {
2659 .delsys = { SYS_DVBT, SYS_DVBT2 }, 3749 .delsys = { SYS_DVBT, SYS_DVBT2, SYS_DVBC_ANNEX_A },
2660 .info = { 3750 .info = {
2661 .name = "Sony CXD2841ER DVB-T/T2 demodulator", 3751 .name = "", /* will set in attach function */
2662 .caps = FE_CAN_FEC_1_2 | 3752 .caps = FE_CAN_FEC_1_2 |
2663 FE_CAN_FEC_2_3 | 3753 FE_CAN_FEC_2_3 |
2664 FE_CAN_FEC_3_4 | 3754 FE_CAN_FEC_3_4 |
@@ -2691,37 +3781,6 @@ static struct dvb_frontend_ops cxd2841er_dvbt_t2_ops = {
2691 .get_frontend_algo = cxd2841er_get_algo 3781 .get_frontend_algo = cxd2841er_get_algo
2692}; 3782};
2693 3783
2694static struct dvb_frontend_ops cxd2841er_dvbc_ops = { 3784MODULE_DESCRIPTION("Sony CXD2841ER/CXD2854ER DVB-C/C2/T/T2/S/S2 demodulator driver");
2695 .delsys = { SYS_DVBC_ANNEX_A }, 3785MODULE_AUTHOR("Sergey Kozlov <serjk@netup.ru>, Abylay Ospan <aospan@netup.ru>");
2696 .info = {
2697 .name = "Sony CXD2841ER DVB-C demodulator",
2698 .caps = FE_CAN_FEC_1_2 |
2699 FE_CAN_FEC_2_3 |
2700 FE_CAN_FEC_3_4 |
2701 FE_CAN_FEC_5_6 |
2702 FE_CAN_FEC_7_8 |
2703 FE_CAN_FEC_AUTO |
2704 FE_CAN_QAM_16 |
2705 FE_CAN_QAM_32 |
2706 FE_CAN_QAM_64 |
2707 FE_CAN_QAM_128 |
2708 FE_CAN_QAM_256 |
2709 FE_CAN_QAM_AUTO |
2710 FE_CAN_INVERSION_AUTO,
2711 .frequency_min = 42000000,
2712 .frequency_max = 1002000000
2713 },
2714 .init = cxd2841er_init_tc,
2715 .sleep = cxd2841er_sleep_tc,
2716 .release = cxd2841er_release,
2717 .set_frontend = cxd2841er_set_frontend_tc,
2718 .get_frontend = cxd2841er_get_frontend,
2719 .read_status = cxd2841er_read_status_tc,
2720 .tune = cxd2841er_tune_tc,
2721 .i2c_gate_ctrl = cxd2841er_i2c_gate_ctrl,
2722 .get_frontend_algo = cxd2841er_get_algo,
2723};
2724
2725MODULE_DESCRIPTION("Sony CXD2841ER DVB-C/C2/T/T2/S/S2 demodulator driver");
2726MODULE_AUTHOR("Sergey Kozlov <serjk@netup.ru>");
2727MODULE_LICENSE("GPL"); 3786MODULE_LICENSE("GPL");
diff --git a/drivers/media/dvb-frontends/cxd2841er.h b/drivers/media/dvb-frontends/cxd2841er.h
index 3472bdd58949..62ad5f07390b 100644
--- a/drivers/media/dvb-frontends/cxd2841er.h
+++ b/drivers/media/dvb-frontends/cxd2841er.h
@@ -25,41 +25,39 @@
25#include <linux/kconfig.h> 25#include <linux/kconfig.h>
26#include <linux/dvb/frontend.h> 26#include <linux/dvb/frontend.h>
27 27
28enum cxd2841er_xtal {
29 SONY_XTAL_20500, /* 20.5 MHz */
30 SONY_XTAL_24000, /* 24 MHz */
31 SONY_XTAL_41000 /* 41 MHz */
32};
33
28struct cxd2841er_config { 34struct cxd2841er_config {
29 u8 i2c_addr; 35 u8 i2c_addr;
36 enum cxd2841er_xtal xtal;
30}; 37};
31 38
32#if IS_REACHABLE(CONFIG_DVB_CXD2841ER) 39#if IS_REACHABLE(CONFIG_DVB_CXD2841ER)
33extern struct dvb_frontend *cxd2841er_attach_s(struct cxd2841er_config *cfg, 40extern struct dvb_frontend *cxd2841er_attach_s(struct cxd2841er_config *cfg,
34 struct i2c_adapter *i2c); 41 struct i2c_adapter *i2c);
35 42
36extern struct dvb_frontend *cxd2841er_attach_t(struct cxd2841er_config *cfg, 43extern struct dvb_frontend *cxd2841er_attach_t_c(struct cxd2841er_config *cfg,
37 struct i2c_adapter *i2c);
38
39extern struct dvb_frontend *cxd2841er_attach_c(struct cxd2841er_config *cfg,
40 struct i2c_adapter *i2c); 44 struct i2c_adapter *i2c);
41#else 45#else
42static inline struct dvb_frontend *cxd2841er_attach_s( 46static inline struct dvb_frontend *cxd2841er_attach_s(
43 struct cxd2841er_config *cfg, 47 struct cxd2841er_config *cfg,
44 struct i2c_adapter *i2c) 48 struct i2c_adapter *i2c)
45{ 49{
46 printk(KERN_WARNING "%s: driver disabled by Kconfig\n", __func__); 50 pr_warn("%s: driver disabled by Kconfig\n", __func__);
47 return NULL; 51 return NULL;
48} 52}
49 53
50static inline struct dvb_frontend *cxd2841er_attach_t( 54static inline struct dvb_frontend *cxd2841er_attach_t_c(
51 struct cxd2841er_config *cfg, struct i2c_adapter *i2c) 55 struct cxd2841er_config *cfg, struct i2c_adapter *i2c)
52{ 56{
53 printk(KERN_WARNING "%s: driver disabled by Kconfig\n", __func__); 57 pr_warn("%s: driver disabled by Kconfig\n", __func__);
54 return NULL; 58 return NULL;
55} 59}
56 60
57static inline struct dvb_frontend *cxd2841er_attach_c(
58 struct cxd2841er_config *cfg, struct i2c_adapter *i2c)
59{
60 printk(KERN_WARNING "%s: driver disabled by Kconfig\n", __func__);
61 return NULL;
62}
63#endif 61#endif
64 62
65#endif 63#endif
diff --git a/drivers/media/dvb-frontends/cxd2841er_priv.h b/drivers/media/dvb-frontends/cxd2841er_priv.h
index 33e2f495277b..0bbce451149f 100644
--- a/drivers/media/dvb-frontends/cxd2841er_priv.h
+++ b/drivers/media/dvb-frontends/cxd2841er_priv.h
@@ -26,6 +26,7 @@
26#define I2C_SLVT 1 26#define I2C_SLVT 1
27 27
28#define CXD2841ER_CHIP_ID 0xa7 28#define CXD2841ER_CHIP_ID 0xa7
29#define CXD2854ER_CHIP_ID 0xc1
29 30
30#define CXD2841ER_DVBS_POLLING_INVL 10 31#define CXD2841ER_DVBS_POLLING_INVL 10
31 32
diff --git a/drivers/media/dvb-frontends/dib0090.c b/drivers/media/dvb-frontends/dib0090.c
index d879dc0607f4..14c403254fe0 100644
--- a/drivers/media/dvb-frontends/dib0090.c
+++ b/drivers/media/dvb-frontends/dib0090.c
@@ -797,6 +797,8 @@ static const u16 bb_ramp_pwm_normal[] = {
797 (0 << 9) | 400, /* BB_RAMP6 */ 797 (0 << 9) | 400, /* BB_RAMP6 */
798}; 798};
799 799
800#if 0
801/* Currently unused */
800static const u16 bb_ramp_pwm_boost[] = { 802static const u16 bb_ramp_pwm_boost[] = {
801 550, /* max BB gain in 10th of dB */ 803 550, /* max BB gain in 10th of dB */
802 8, /* ramp_slope = 1dB of gain -> clock_ticks_per_db = clk_khz / ramp_slope -> BB_RAMP2 */ 804 8, /* ramp_slope = 1dB of gain -> clock_ticks_per_db = clk_khz / ramp_slope -> BB_RAMP2 */
@@ -806,6 +808,7 @@ static const u16 bb_ramp_pwm_boost[] = {
806 (2 << 9) | 208, /* BB_RAMP5 = 29dB */ 808 (2 << 9) | 208, /* BB_RAMP5 = 29dB */
807 (0 << 9) | 440, /* BB_RAMP6 */ 809 (0 << 9) | 440, /* BB_RAMP6 */
808}; 810};
811#endif
809 812
810static const u16 rf_ramp_pwm_cband[] = { 813static const u16 rf_ramp_pwm_cband[] = {
811 314, /* max RF gain in 10th of dB */ 814 314, /* max RF gain in 10th of dB */
@@ -849,6 +852,8 @@ static const u16 rf_ramp_pwm_uhf[] = {
849 (0 << 10) | 580, /* GAIN_4_2, LNA 4 */ 852 (0 << 10) | 580, /* GAIN_4_2, LNA 4 */
850}; 853};
851 854
855#if 0
856/* Currently unused */
852static const u16 rf_ramp_pwm_sband[] = { 857static const u16 rf_ramp_pwm_sband[] = {
853 253, /* max RF gain in 10th of dB */ 858 253, /* max RF gain in 10th of dB */
854 38, /* ramp_slope = 1dB of gain -> clock_ticks_per_db = clk_khz / ramp_slope -> RF_RAMP2 */ 859 38, /* ramp_slope = 1dB of gain -> clock_ticks_per_db = clk_khz / ramp_slope -> RF_RAMP2 */
@@ -862,6 +867,7 @@ static const u16 rf_ramp_pwm_sband[] = {
862 (0 << 10) | 0, /* GAIN_4_1, LNA 4 = 0dB */ 867 (0 << 10) | 0, /* GAIN_4_1, LNA 4 = 0dB */
863 (0 << 10) | 0, /* GAIN_4_2, LNA 4 */ 868 (0 << 10) | 0, /* GAIN_4_2, LNA 4 */
864}; 869};
870#endif
865 871
866struct slope { 872struct slope {
867 s16 range; 873 s16 range;
diff --git a/drivers/media/dvb-frontends/drx39xyj/drxj.c b/drivers/media/dvb-frontends/drx39xyj/drxj.c
index e48b741d439e..bd6d2ee0f7c9 100644
--- a/drivers/media/dvb-frontends/drx39xyj/drxj.c
+++ b/drivers/media/dvb-frontends/drx39xyj/drxj.c
@@ -1240,12 +1240,15 @@ static u32 frac_times1e6(u32 N, u32 D)
1240* and rounded. For calc used formula: 16*10^(prescaleGain[dB]/20). 1240* and rounded. For calc used formula: 16*10^(prescaleGain[dB]/20).
1241* 1241*
1242*/ 1242*/
1243#if 0
1244/* Currently, unused as we lack support for analog TV */
1243static const u16 nicam_presc_table_val[43] = { 1245static const u16 nicam_presc_table_val[43] = {
1244 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 4, 4, 1246 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 4, 4,
1245 5, 5, 6, 6, 7, 8, 9, 10, 11, 13, 14, 16, 1247 5, 5, 6, 6, 7, 8, 9, 10, 11, 13, 14, 16,
1246 18, 20, 23, 25, 28, 32, 36, 40, 45, 1248 18, 20, 23, 25, 28, 32, 36, 40, 45,
1247 51, 57, 64, 71, 80, 90, 101, 113, 127 1249 51, 57, 64, 71, 80, 90, 101, 113, 127
1248}; 1250};
1251#endif
1249 1252
1250/*============================================================================*/ 1253/*============================================================================*/
1251/*== END HELPER FUNCTIONS ==*/ 1254/*== END HELPER FUNCTIONS ==*/
diff --git a/drivers/media/dvb-frontends/ds3000.c b/drivers/media/dvb-frontends/ds3000.c
index addffc33993a..447b518e287a 100644
--- a/drivers/media/dvb-frontends/ds3000.c
+++ b/drivers/media/dvb-frontends/ds3000.c
@@ -959,6 +959,15 @@ static int ds3000_set_frontend(struct dvb_frontend *fe)
959 /* enable ac coupling */ 959 /* enable ac coupling */
960 ds3000_writereg(state, 0x25, 0x8a); 960 ds3000_writereg(state, 0x25, 0x8a);
961 961
962 if ((c->symbol_rate < ds3000_ops.info.symbol_rate_min) ||
963 (c->symbol_rate > ds3000_ops.info.symbol_rate_max)) {
964 dprintk("%s() symbol_rate %u out of range (%u ... %u)\n",
965 __func__, c->symbol_rate,
966 ds3000_ops.info.symbol_rate_min,
967 ds3000_ops.info.symbol_rate_max);
968 return -EINVAL;
969 }
970
962 /* enhance symbol rate performance */ 971 /* enhance symbol rate performance */
963 if ((c->symbol_rate / 1000) <= 5000) { 972 if ((c->symbol_rate / 1000) <= 5000) {
964 value = 29777 / (c->symbol_rate / 1000) + 1; 973 value = 29777 / (c->symbol_rate / 1000) + 1;
diff --git a/drivers/media/dvb-frontends/helene.c b/drivers/media/dvb-frontends/helene.c
new file mode 100644
index 000000000000..97a8982740a6
--- /dev/null
+++ b/drivers/media/dvb-frontends/helene.c
@@ -0,0 +1,1042 @@
1/*
2 * helene.c
3 *
4 * Sony HELENE DVB-S/S2 DVB-T/T2 DVB-C/C2 ISDB-T/S tuner driver (CXD2858ER)
5 *
6 * Copyright 2012 Sony Corporation
7 * Copyright (C) 2014 NetUP Inc.
8 * Copyright (C) 2014 Abylay Ospan <aospan@netup.ru>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 */
20
21#include <linux/slab.h>
22#include <linux/module.h>
23#include <linux/dvb/frontend.h>
24#include <linux/types.h>
25#include "helene.h"
26#include "dvb_frontend.h"
27
28#define MAX_WRITE_REGSIZE 20
29
30enum helene_state {
31 STATE_UNKNOWN,
32 STATE_SLEEP,
33 STATE_ACTIVE
34};
35
36struct helene_priv {
37 u32 frequency;
38 u8 i2c_address;
39 struct i2c_adapter *i2c;
40 enum helene_state state;
41 void *set_tuner_data;
42 int (*set_tuner)(void *, int);
43 enum helene_xtal xtal;
44};
45
46#define TERR_INTERNAL_LOOPFILTER_AVAILABLE(tv_system) \
47 (((tv_system) != SONY_HELENE_DTV_DVBC_6) && \
48 ((tv_system) != SONY_HELENE_DTV_DVBC_8)\
49 && ((tv_system) != SONY_HELENE_DTV_DVBC2_6) && \
50 ((tv_system) != SONY_HELENE_DTV_DVBC2_8))
51
52#define HELENE_AUTO 0xff
53#define HELENE_OFFSET(ofs) ((u8)(ofs) & 0x1F)
54#define HELENE_BW_6 0x00
55#define HELENE_BW_7 0x01
56#define HELENE_BW_8 0x02
57#define HELENE_BW_1_7 0x03
58
59enum helene_tv_system_t {
60 SONY_HELENE_TV_SYSTEM_UNKNOWN,
61 /* Terrestrial Analog */
62 SONY_HELENE_ATV_MN_EIAJ,
63 /**< System-M (Japan) (IF: Fp=5.75MHz in default) */
64 SONY_HELENE_ATV_MN_SAP,
65 /**< System-M (US) (IF: Fp=5.75MHz in default) */
66 SONY_HELENE_ATV_MN_A2,
67 /**< System-M (Korea) (IF: Fp=5.9MHz in default) */
68 SONY_HELENE_ATV_BG,
69 /**< System-B/G (IF: Fp=7.3MHz in default) */
70 SONY_HELENE_ATV_I,
71 /**< System-I (IF: Fp=7.85MHz in default) */
72 SONY_HELENE_ATV_DK,
73 /**< System-D/K (IF: Fp=7.85MHz in default) */
74 SONY_HELENE_ATV_L,
75 /**< System-L (IF: Fp=7.85MHz in default) */
76 SONY_HELENE_ATV_L_DASH,
77 /**< System-L DASH (IF: Fp=2.2MHz in default) */
78 /* Terrestrial/Cable Digital */
79 SONY_HELENE_DTV_8VSB,
80 /**< ATSC 8VSB (IF: Fc=3.7MHz in default) */
81 SONY_HELENE_DTV_QAM,
82 /**< US QAM (IF: Fc=3.7MHz in default) */
83 SONY_HELENE_DTV_ISDBT_6,
84 /**< ISDB-T 6MHzBW (IF: Fc=3.55MHz in default) */
85 SONY_HELENE_DTV_ISDBT_7,
86 /**< ISDB-T 7MHzBW (IF: Fc=4.15MHz in default) */
87 SONY_HELENE_DTV_ISDBT_8,
88 /**< ISDB-T 8MHzBW (IF: Fc=4.75MHz in default) */
89 SONY_HELENE_DTV_DVBT_5,
90 /**< DVB-T 5MHzBW (IF: Fc=3.6MHz in default) */
91 SONY_HELENE_DTV_DVBT_6,
92 /**< DVB-T 6MHzBW (IF: Fc=3.6MHz in default) */
93 SONY_HELENE_DTV_DVBT_7,
94 /**< DVB-T 7MHzBW (IF: Fc=4.2MHz in default) */
95 SONY_HELENE_DTV_DVBT_8,
96 /**< DVB-T 8MHzBW (IF: Fc=4.8MHz in default) */
97 SONY_HELENE_DTV_DVBT2_1_7,
98 /**< DVB-T2 1.7MHzBW (IF: Fc=3.5MHz in default) */
99 SONY_HELENE_DTV_DVBT2_5,
100 /**< DVB-T2 5MHzBW (IF: Fc=3.6MHz in default) */
101 SONY_HELENE_DTV_DVBT2_6,
102 /**< DVB-T2 6MHzBW (IF: Fc=3.6MHz in default) */
103 SONY_HELENE_DTV_DVBT2_7,
104 /**< DVB-T2 7MHzBW (IF: Fc=4.2MHz in default) */
105 SONY_HELENE_DTV_DVBT2_8,
106 /**< DVB-T2 8MHzBW (IF: Fc=4.8MHz in default) */
107 SONY_HELENE_DTV_DVBC_6,
108 /**< DVB-C 6MHzBW (IF: Fc=3.7MHz in default) */
109 SONY_HELENE_DTV_DVBC_8,
110 /**< DVB-C 8MHzBW (IF: Fc=4.9MHz in default) */
111 SONY_HELENE_DTV_DVBC2_6,
112 /**< DVB-C2 6MHzBW (IF: Fc=3.7MHz in default) */
113 SONY_HELENE_DTV_DVBC2_8,
114 /**< DVB-C2 8MHzBW (IF: Fc=4.9MHz in default) */
115 SONY_HELENE_DTV_DTMB,
116 /**< DTMB (IF: Fc=5.1MHz in default) */
117 /* Satellite */
118 SONY_HELENE_STV_ISDBS,
119 /**< ISDB-S */
120 SONY_HELENE_STV_DVBS,
121 /**< DVB-S */
122 SONY_HELENE_STV_DVBS2,
123 /**< DVB-S2 */
124
125 SONY_HELENE_ATV_MIN = SONY_HELENE_ATV_MN_EIAJ,
126 /**< Minimum analog terrestrial system */
127 SONY_HELENE_ATV_MAX = SONY_HELENE_ATV_L_DASH,
128 /**< Maximum analog terrestrial system */
129 SONY_HELENE_DTV_MIN = SONY_HELENE_DTV_8VSB,
130 /**< Minimum digital terrestrial system */
131 SONY_HELENE_DTV_MAX = SONY_HELENE_DTV_DTMB,
132 /**< Maximum digital terrestrial system */
133 SONY_HELENE_TERR_TV_SYSTEM_NUM,
134 /**< Number of supported terrestrial broadcasting system */
135 SONY_HELENE_STV_MIN = SONY_HELENE_STV_ISDBS,
136 /**< Minimum satellite system */
137 SONY_HELENE_STV_MAX = SONY_HELENE_STV_DVBS2
138 /**< Maximum satellite system */
139};
140
141struct helene_terr_adjust_param_t {
142 /* < Addr:0x69 Bit[6:4] : RFVGA gain.
143 * 0xFF means Auto. (RF_GAIN_SEL = 1)
144 */
145 uint8_t RF_GAIN;
146 /* < Addr:0x69 Bit[3:0] : IF_BPF gain.
147 */
148 uint8_t IF_BPF_GC;
149 /* < Addr:0x6B Bit[3:0] : RF overload
150 * RF input detect level. (FRF <= 172MHz)
151 */
152 uint8_t RFOVLD_DET_LV1_VL;
153 /* < Addr:0x6B Bit[3:0] : RF overload
154 * RF input detect level. (172MHz < FRF <= 464MHz)
155 */
156 uint8_t RFOVLD_DET_LV1_VH;
157 /* < Addr:0x6B Bit[3:0] : RF overload
158 * RF input detect level. (FRF > 464MHz)
159 */
160 uint8_t RFOVLD_DET_LV1_U;
161 /* < Addr:0x6C Bit[2:0] :
162 * Internal RFAGC detect level. (FRF <= 172MHz)
163 */
164 uint8_t IFOVLD_DET_LV_VL;
165 /* < Addr:0x6C Bit[2:0] :
166 * Internal RFAGC detect level. (172MHz < FRF <= 464MHz)
167 */
168 uint8_t IFOVLD_DET_LV_VH;
169 /* < Addr:0x6C Bit[2:0] :
170 * Internal RFAGC detect level. (FRF > 464MHz)
171 */
172 uint8_t IFOVLD_DET_LV_U;
173 /* < Addr:0x6D Bit[5:4] :
174 * IF filter center offset.
175 */
176 uint8_t IF_BPF_F0;
177 /* < Addr:0x6D Bit[1:0] :
178 * 6MHzBW(0x00) or 7MHzBW(0x01)
179 * or 8MHzBW(0x02) or 1.7MHzBW(0x03)
180 */
181 uint8_t BW;
182 /* < Addr:0x6E Bit[4:0] :
183 * 5bit signed. IF offset (kHz) = FIF_OFFSET x 50
184 */
185 uint8_t FIF_OFFSET;
186 /* < Addr:0x6F Bit[4:0] :
187 * 5bit signed. BW offset (kHz) =
188 * BW_OFFSET x 50 (BW_OFFSET x 10 in 1.7MHzBW)
189 */
190 uint8_t BW_OFFSET;
191 /* < Addr:0x9C Bit[0] :
192 * Local polarity. (0: Upper Local, 1: Lower Local)
193 */
194 uint8_t IS_LOWERLOCAL;
195};
196
197static const struct helene_terr_adjust_param_t
198terr_params[SONY_HELENE_TERR_TV_SYSTEM_NUM] = {
199 /*< SONY_HELENE_TV_SYSTEM_UNKNOWN */
200 {HELENE_AUTO, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
201 HELENE_BW_6, HELENE_OFFSET(0), HELENE_OFFSET(0), 0x00},
202 /* Analog */
203 /**< SONY_HELENE_ATV_MN_EIAJ (System-M (Japan)) */
204 {HELENE_AUTO, 0x05, 0x03, 0x06, 0x03, 0x01, 0x01, 0x01, 0x00,
205 HELENE_BW_6, HELENE_OFFSET(0), HELENE_OFFSET(1), 0x00},
206 /**< SONY_HELENE_ATV_MN_SAP (System-M (US)) */
207 {HELENE_AUTO, 0x05, 0x03, 0x06, 0x03, 0x01, 0x01, 0x01, 0x00,
208 HELENE_BW_6, HELENE_OFFSET(0), HELENE_OFFSET(1), 0x00},
209 {HELENE_AUTO, 0x05, 0x03, 0x06, 0x03, 0x01, 0x01, 0x01, 0x00,
210 HELENE_BW_6, HELENE_OFFSET(3), HELENE_OFFSET(1), 0x00},
211 /**< SONY_HELENE_ATV_MN_A2 (System-M (Korea)) */
212 {HELENE_AUTO, 0x05, 0x03, 0x06, 0x03, 0x01, 0x01, 0x01, 0x00,
213 HELENE_BW_7, HELENE_OFFSET(11), HELENE_OFFSET(5), 0x00},
214 /**< SONY_HELENE_ATV_BG (System-B/G) */
215 {HELENE_AUTO, 0x05, 0x03, 0x06, 0x03, 0x01, 0x01, 0x01, 0x00,
216 HELENE_BW_8, HELENE_OFFSET(2), HELENE_OFFSET(-3), 0x00},
217 /**< SONY_HELENE_ATV_I (System-I) */
218 {HELENE_AUTO, 0x05, 0x03, 0x06, 0x03, 0x01, 0x01, 0x01, 0x00,
219 HELENE_BW_8, HELENE_OFFSET(2), HELENE_OFFSET(-3), 0x00},
220 /**< SONY_HELENE_ATV_DK (System-D/K) */
221 {HELENE_AUTO, 0x03, 0x04, 0x0A, 0x04, 0x04, 0x04, 0x04, 0x00,
222 HELENE_BW_8, HELENE_OFFSET(2), HELENE_OFFSET(-3), 0x00},
223 /**< SONY_HELENE_ATV_L (System-L) */
224 {HELENE_AUTO, 0x03, 0x04, 0x0A, 0x04, 0x04, 0x04, 0x04, 0x00,
225 HELENE_BW_8, HELENE_OFFSET(-1), HELENE_OFFSET(4), 0x00},
226 /**< SONY_HELENE_ATV_L_DASH (System-L DASH) */
227 /* Digital */
228 {HELENE_AUTO, 0x09, 0x0B, 0x0B, 0x0B, 0x03, 0x03, 0x03, 0x00,
229 HELENE_BW_6, HELENE_OFFSET(-6), HELENE_OFFSET(-3), 0x00},
230 /**< SONY_HELENE_DTV_8VSB (ATSC 8VSB) */
231 {HELENE_AUTO, 0x09, 0x0B, 0x0B, 0x0B, 0x02, 0x02, 0x02, 0x00,
232 HELENE_BW_6, HELENE_OFFSET(-6), HELENE_OFFSET(-3), 0x00},
233 /**< SONY_HELENE_DTV_QAM (US QAM) */
234 {HELENE_AUTO, 0x09, 0x0B, 0x0B, 0x0B, 0x02, 0x02, 0x02, 0x00,
235 HELENE_BW_6, HELENE_OFFSET(-9), HELENE_OFFSET(-5), 0x00},
236 /**< SONY_HELENE_DTV_ISDBT_6 (ISDB-T 6MHzBW) */
237 {HELENE_AUTO, 0x09, 0x0B, 0x0B, 0x0B, 0x02, 0x02, 0x02, 0x00,
238 HELENE_BW_7, HELENE_OFFSET(-7), HELENE_OFFSET(-6), 0x00},
239 /**< SONY_HELENE_DTV_ISDBT_7 (ISDB-T 7MHzBW) */
240 {HELENE_AUTO, 0x09, 0x0B, 0x0B, 0x0B, 0x02, 0x02, 0x02, 0x00,
241 HELENE_BW_8, HELENE_OFFSET(-5), HELENE_OFFSET(-7), 0x00},
242 /**< SONY_HELENE_DTV_ISDBT_8 (ISDB-T 8MHzBW) */
243 {HELENE_AUTO, 0x09, 0x0B, 0x0B, 0x0B, 0x02, 0x02, 0x02, 0x00,
244 HELENE_BW_6, HELENE_OFFSET(-8), HELENE_OFFSET(-3), 0x00},
245 /**< SONY_HELENE_DTV_DVBT_5 (DVB-T 5MHzBW) */
246 {HELENE_AUTO, 0x09, 0x0B, 0x0B, 0x0B, 0x02, 0x02, 0x02, 0x00,
247 HELENE_BW_6, HELENE_OFFSET(-8), HELENE_OFFSET(-3), 0x00},
248 /**< SONY_HELENE_DTV_DVBT_6 (DVB-T 6MHzBW) */
249 {HELENE_AUTO, 0x09, 0x0B, 0x0B, 0x0B, 0x02, 0x02, 0x02, 0x00,
250 HELENE_BW_7, HELENE_OFFSET(-6), HELENE_OFFSET(-5), 0x00},
251 /**< SONY_HELENE_DTV_DVBT_7 (DVB-T 7MHzBW) */
252 {HELENE_AUTO, 0x09, 0x0B, 0x0B, 0x0B, 0x02, 0x02, 0x02, 0x00,
253 HELENE_BW_8, HELENE_OFFSET(-4), HELENE_OFFSET(-6), 0x00},
254 /**< SONY_HELENE_DTV_DVBT_8 (DVB-T 8MHzBW) */
255 {HELENE_AUTO, 0x09, 0x0B, 0x0B, 0x0B, 0x02, 0x02, 0x02, 0x00,
256 HELENE_BW_1_7, HELENE_OFFSET(-10), HELENE_OFFSET(-10), 0x00},
257 /**< SONY_HELENE_DTV_DVBT2_1_7 (DVB-T2 1.7MHzBW) */
258 {HELENE_AUTO, 0x09, 0x0B, 0x0B, 0x0B, 0x02, 0x02, 0x02, 0x00,
259 HELENE_BW_6, HELENE_OFFSET(-8), HELENE_OFFSET(-3), 0x00},
260 /**< SONY_HELENE_DTV_DVBT2_5 (DVB-T2 5MHzBW) */
261 {HELENE_AUTO, 0x09, 0x0B, 0x0B, 0x0B, 0x02, 0x02, 0x02, 0x00,
262 HELENE_BW_6, HELENE_OFFSET(-8), HELENE_OFFSET(-3), 0x00},
263 /**< SONY_HELENE_DTV_DVBT2_6 (DVB-T2 6MHzBW) */
264 {HELENE_AUTO, 0x09, 0x0B, 0x0B, 0x0B, 0x02, 0x02, 0x02, 0x00,
265 HELENE_BW_7, HELENE_OFFSET(-6), HELENE_OFFSET(-5), 0x00},
266 /**< SONY_HELENE_DTV_DVBT2_7 (DVB-T2 7MHzBW) */
267 {HELENE_AUTO, 0x09, 0x0B, 0x0B, 0x0B, 0x02, 0x02, 0x02, 0x00,
268 HELENE_BW_8, HELENE_OFFSET(-4), HELENE_OFFSET(-6), 0x00},
269 /**< SONY_HELENE_DTV_DVBT2_8 (DVB-T2 8MHzBW) */
270 {HELENE_AUTO, 0x05, 0x02, 0x02, 0x02, 0x01, 0x01, 0x01, 0x00,
271 HELENE_BW_6, HELENE_OFFSET(-6), HELENE_OFFSET(-4), 0x00},
272 /**< SONY_HELENE_DTV_DVBC_6 (DVB-C 6MHzBW) */
273 {HELENE_AUTO, 0x05, 0x02, 0x02, 0x02, 0x01, 0x01, 0x01, 0x00,
274 HELENE_BW_8, HELENE_OFFSET(-2), HELENE_OFFSET(-3), 0x00},
275 /**< SONY_HELENE_DTV_DVBC_8 (DVB-C 8MHzBW) */
276 {HELENE_AUTO, 0x03, 0x09, 0x09, 0x09, 0x02, 0x02, 0x02, 0x00,
277 HELENE_BW_6, HELENE_OFFSET(-6), HELENE_OFFSET(-2), 0x00},
278 /**< SONY_HELENE_DTV_DVBC2_6 (DVB-C2 6MHzBW) */
279 {HELENE_AUTO, 0x03, 0x09, 0x09, 0x09, 0x02, 0x02, 0x02, 0x00,
280 HELENE_BW_8, HELENE_OFFSET(-2), HELENE_OFFSET(0), 0x00},
281 /**< SONY_HELENE_DTV_DVBC2_8 (DVB-C2 8MHzBW) */
282 {HELENE_AUTO, 0x04, 0x0B, 0x0B, 0x0B, 0x02, 0x02, 0x02, 0x00,
283 HELENE_BW_8, HELENE_OFFSET(2), HELENE_OFFSET(1), 0x00}
284 /**< SONY_HELENE_DTV_DTMB (DTMB) */
285};
286
287static void helene_i2c_debug(struct helene_priv *priv,
288 u8 reg, u8 write, const u8 *data, u32 len)
289{
290 dev_dbg(&priv->i2c->dev, "helene: I2C %s reg 0x%02x size %d\n",
291 (write == 0 ? "read" : "write"), reg, len);
292 print_hex_dump_bytes("helene: I2C data: ",
293 DUMP_PREFIX_OFFSET, data, len);
294}
295
296static int helene_write_regs(struct helene_priv *priv,
297 u8 reg, const u8 *data, u32 len)
298{
299 int ret;
300 u8 buf[MAX_WRITE_REGSIZE + 1];
301 struct i2c_msg msg[1] = {
302 {
303 .addr = priv->i2c_address,
304 .flags = 0,
305 .len = len + 1,
306 .buf = buf,
307 }
308 };
309
310 if (len + 1 > sizeof(buf)) {
311 dev_warn(&priv->i2c->dev,
312 "wr reg=%04x: len=%d vs %Zu is too big!\n",
313 reg, len + 1, sizeof(buf));
314 return -E2BIG;
315 }
316
317 helene_i2c_debug(priv, reg, 1, data, len);
318 buf[0] = reg;
319 memcpy(&buf[1], data, len);
320 ret = i2c_transfer(priv->i2c, msg, 1);
321 if (ret >= 0 && ret != 1)
322 ret = -EREMOTEIO;
323 if (ret < 0) {
324 dev_warn(&priv->i2c->dev,
325 "%s: i2c wr failed=%d reg=%02x len=%d\n",
326 KBUILD_MODNAME, ret, reg, len);
327 return ret;
328 }
329 return 0;
330}
331
332static int helene_write_reg(struct helene_priv *priv, u8 reg, u8 val)
333{
334 return helene_write_regs(priv, reg, &val, 1);
335}
336
337static int helene_read_regs(struct helene_priv *priv,
338 u8 reg, u8 *val, u32 len)
339{
340 int ret;
341 struct i2c_msg msg[2] = {
342 {
343 .addr = priv->i2c_address,
344 .flags = 0,
345 .len = 1,
346 .buf = &reg,
347 }, {
348 .addr = priv->i2c_address,
349 .flags = I2C_M_RD,
350 .len = len,
351 .buf = val,
352 }
353 };
354
355 ret = i2c_transfer(priv->i2c, &msg[0], 1);
356 if (ret >= 0 && ret != 1)
357 ret = -EREMOTEIO;
358 if (ret < 0) {
359 dev_warn(&priv->i2c->dev,
360 "%s: I2C rw failed=%d addr=%02x reg=%02x\n",
361 KBUILD_MODNAME, ret, priv->i2c_address, reg);
362 return ret;
363 }
364 ret = i2c_transfer(priv->i2c, &msg[1], 1);
365 if (ret >= 0 && ret != 1)
366 ret = -EREMOTEIO;
367 if (ret < 0) {
368 dev_warn(&priv->i2c->dev,
369 "%s: i2c rd failed=%d addr=%02x reg=%02x\n",
370 KBUILD_MODNAME, ret, priv->i2c_address, reg);
371 return ret;
372 }
373 helene_i2c_debug(priv, reg, 0, val, len);
374 return 0;
375}
376
377static int helene_read_reg(struct helene_priv *priv, u8 reg, u8 *val)
378{
379 return helene_read_regs(priv, reg, val, 1);
380}
381
382static int helene_set_reg_bits(struct helene_priv *priv,
383 u8 reg, u8 data, u8 mask)
384{
385 int res;
386 u8 rdata;
387
388 if (mask != 0xff) {
389 res = helene_read_reg(priv, reg, &rdata);
390 if (res != 0)
391 return res;
392 data = ((data & mask) | (rdata & (mask ^ 0xFF)));
393 }
394 return helene_write_reg(priv, reg, data);
395}
396
397static int helene_enter_power_save(struct helene_priv *priv)
398{
399 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
400 if (priv->state == STATE_SLEEP)
401 return 0;
402
403 /* Standby setting for CPU */
404 helene_write_reg(priv, 0x88, 0x0);
405
406 /* Standby setting for internal logic block */
407 helene_write_reg(priv, 0x87, 0xC0);
408
409 priv->state = STATE_SLEEP;
410 return 0;
411}
412
413static int helene_leave_power_save(struct helene_priv *priv)
414{
415 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
416 if (priv->state == STATE_ACTIVE)
417 return 0;
418
419 /* Standby setting for internal logic block */
420 helene_write_reg(priv, 0x87, 0xC4);
421
422 /* Standby setting for CPU */
423 helene_write_reg(priv, 0x88, 0x40);
424
425 priv->state = STATE_ACTIVE;
426 return 0;
427}
428
429static int helene_init(struct dvb_frontend *fe)
430{
431 struct helene_priv *priv = fe->tuner_priv;
432
433 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
434 return helene_leave_power_save(priv);
435}
436
437static int helene_release(struct dvb_frontend *fe)
438{
439 struct helene_priv *priv = fe->tuner_priv;
440
441 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
442 kfree(fe->tuner_priv);
443 fe->tuner_priv = NULL;
444 return 0;
445}
446
447static int helene_sleep(struct dvb_frontend *fe)
448{
449 struct helene_priv *priv = fe->tuner_priv;
450
451 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
452 helene_enter_power_save(priv);
453 return 0;
454}
455
456static enum helene_tv_system_t helene_get_tv_system(struct dvb_frontend *fe)
457{
458 enum helene_tv_system_t system = SONY_HELENE_TV_SYSTEM_UNKNOWN;
459 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
460 struct helene_priv *priv = fe->tuner_priv;
461
462 if (p->delivery_system == SYS_DVBT) {
463 if (p->bandwidth_hz <= 5000000)
464 system = SONY_HELENE_DTV_DVBT_5;
465 else if (p->bandwidth_hz <= 6000000)
466 system = SONY_HELENE_DTV_DVBT_6;
467 else if (p->bandwidth_hz <= 7000000)
468 system = SONY_HELENE_DTV_DVBT_7;
469 else if (p->bandwidth_hz <= 8000000)
470 system = SONY_HELENE_DTV_DVBT_8;
471 else {
472 system = SONY_HELENE_DTV_DVBT_8;
473 p->bandwidth_hz = 8000000;
474 }
475 } else if (p->delivery_system == SYS_DVBT2) {
476 if (p->bandwidth_hz <= 5000000)
477 system = SONY_HELENE_DTV_DVBT2_5;
478 else if (p->bandwidth_hz <= 6000000)
479 system = SONY_HELENE_DTV_DVBT2_6;
480 else if (p->bandwidth_hz <= 7000000)
481 system = SONY_HELENE_DTV_DVBT2_7;
482 else if (p->bandwidth_hz <= 8000000)
483 system = SONY_HELENE_DTV_DVBT2_8;
484 else {
485 system = SONY_HELENE_DTV_DVBT2_8;
486 p->bandwidth_hz = 8000000;
487 }
488 } else if (p->delivery_system == SYS_DVBS) {
489 system = SONY_HELENE_STV_DVBS;
490 } else if (p->delivery_system == SYS_DVBS2) {
491 system = SONY_HELENE_STV_DVBS2;
492 } else if (p->delivery_system == SYS_ISDBS) {
493 system = SONY_HELENE_STV_ISDBS;
494 } else if (p->delivery_system == SYS_ISDBT) {
495 if (p->bandwidth_hz <= 6000000)
496 system = SONY_HELENE_DTV_ISDBT_6;
497 else if (p->bandwidth_hz <= 7000000)
498 system = SONY_HELENE_DTV_ISDBT_7;
499 else if (p->bandwidth_hz <= 8000000)
500 system = SONY_HELENE_DTV_ISDBT_8;
501 else {
502 system = SONY_HELENE_DTV_ISDBT_8;
503 p->bandwidth_hz = 8000000;
504 }
505 } else if (p->delivery_system == SYS_DVBC_ANNEX_A) {
506 if (p->bandwidth_hz <= 6000000)
507 system = SONY_HELENE_DTV_DVBC_6;
508 else if (p->bandwidth_hz <= 8000000)
509 system = SONY_HELENE_DTV_DVBC_8;
510 }
511 dev_dbg(&priv->i2c->dev,
512 "%s(): HELENE DTV system %d (delsys %d, bandwidth %d)\n",
513 __func__, (int)system, p->delivery_system,
514 p->bandwidth_hz);
515 return system;
516}
517
518static int helene_set_params_s(struct dvb_frontend *fe)
519{
520 u8 data[MAX_WRITE_REGSIZE];
521 u32 frequency;
522 enum helene_tv_system_t tv_system;
523 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
524 struct helene_priv *priv = fe->tuner_priv;
525 int frequencykHz = p->frequency;
526 uint32_t frequency4kHz = 0;
527 u32 symbol_rate = p->symbol_rate/1000;
528
529 dev_dbg(&priv->i2c->dev, "%s(): tune frequency %dkHz sr=%uKsps\n",
530 __func__, frequencykHz, symbol_rate);
531 tv_system = helene_get_tv_system(fe);
532
533 if (tv_system == SONY_HELENE_TV_SYSTEM_UNKNOWN) {
534 dev_err(&priv->i2c->dev, "%s(): unknown DTV system\n",
535 __func__);
536 return -EINVAL;
537 }
538 /* RF switch turn to satellite */
539 if (priv->set_tuner)
540 priv->set_tuner(priv->set_tuner_data, 0);
541 frequency = roundup(p->frequency / 1000, 1);
542
543 /* Disable IF signal output */
544 helene_write_reg(priv, 0x15, 0x02);
545
546 /* RFIN matching in power save (Sat) reset */
547 helene_write_reg(priv, 0x43, 0x06);
548
549 /* Analog block setting (0x6A, 0x6B) */
550 data[0] = 0x00;
551 data[1] = 0x00;
552 helene_write_regs(priv, 0x6A, data, 2);
553 helene_write_reg(priv, 0x75, 0x99);
554 helene_write_reg(priv, 0x9D, 0x00);
555
556 /* Tuning setting for CPU (0x61) */
557 helene_write_reg(priv, 0x61, 0x07);
558
559 /* Satellite mode select (0x01) */
560 helene_write_reg(priv, 0x01, 0x01);
561
562 /* Clock enable for internal logic block, CPU wake-up (0x04, 0x05) */
563 data[0] = 0xC4;
564 data[1] = 0x40;
565
566 switch (priv->xtal) {
567 case SONY_HELENE_XTAL_16000:
568 data[2] = 0x02;
569 break;
570 case SONY_HELENE_XTAL_20500:
571 data[2] = 0x02;
572 break;
573 case SONY_HELENE_XTAL_24000:
574 data[2] = 0x03;
575 break;
576 case SONY_HELENE_XTAL_41000:
577 data[2] = 0x05;
578 break;
579 default:
580 dev_err(&priv->i2c->dev, "%s(): unknown xtal %d\n",
581 __func__, priv->xtal);
582 return -EINVAL;
583 }
584
585 /* Setting for analog block (0x07). LOOPFILTER INTERNAL */
586 data[3] = 0x80;
587
588 /* Tuning setting for analog block
589 * (0x08, 0x09, 0x0A, 0x0B). LOOPFILTER INTERNAL
590 */
591 if (priv->xtal == SONY_HELENE_XTAL_20500)
592 data[4] = 0x58;
593 else
594 data[4] = 0x70;
595
596 data[5] = 0x1E;
597 data[6] = 0x02;
598 data[7] = 0x24;
599
600 /* Enable for analog block (0x0C, 0x0D, 0x0E). SAT LNA ON */
601 data[8] = 0x0F;
602 data[8] |= 0xE0; /* POWERSAVE_TERR_RF_ACTIVE */
603 data[9] = 0x02;
604 data[10] = 0x1E;
605
606 /* Setting for LPF cutoff frequency (0x0F) */
607 switch (tv_system) {
608 case SONY_HELENE_STV_ISDBS:
609 data[11] = 0x22; /* 22MHz */
610 break;
611 case SONY_HELENE_STV_DVBS:
612 if (symbol_rate <= 4000)
613 data[11] = 0x05;
614 else if (symbol_rate <= 10000)
615 data[11] = (uint8_t)((symbol_rate * 47
616 + (40000-1)) / 40000);
617 else
618 data[11] = (uint8_t)((symbol_rate * 27
619 + (40000-1)) / 40000 + 5);
620
621 if (data[11] > 36)
622 data[11] = 36; /* 5 <= lpf_cutoff <= 36 is valid */
623 break;
624 case SONY_HELENE_STV_DVBS2:
625 if (symbol_rate <= 4000)
626 data[11] = 0x05;
627 else if (symbol_rate <= 10000)
628 data[11] = (uint8_t)((symbol_rate * 11
629 + (10000-1)) / 10000);
630 else
631 data[11] = (uint8_t)((symbol_rate * 3
632 + (5000-1)) / 5000 + 5);
633
634 if (data[11] > 36)
635 data[11] = 36; /* 5 <= lpf_cutoff <= 36 is valid */
636 break;
637 default:
638 dev_err(&priv->i2c->dev, "%s(): unknown standard %d\n",
639 __func__, tv_system);
640 return -EINVAL;
641 }
642
643 /* RF tuning frequency setting (0x10, 0x11, 0x12) */
644 frequency4kHz = (frequencykHz + 2) / 4;
645 data[12] = (uint8_t)(frequency4kHz & 0xFF); /* FRF_L */
646 data[13] = (uint8_t)((frequency4kHz >> 8) & 0xFF); /* FRF_M */
647 /* FRF_H (bit[3:0]) */
648 data[14] = (uint8_t)((frequency4kHz >> 16) & 0x0F);
649
650 /* Tuning command (0x13) */
651 data[15] = 0xFF;
652
653 /* Setting for IQOUT_LIMIT (0x14) 0.75Vpp */
654 data[16] = 0x00;
655
656 /* Enable IQ output (0x15) */
657 data[17] = 0x01;
658
659 helene_write_regs(priv, 0x04, data, 18);
660
661 dev_dbg(&priv->i2c->dev, "%s(): tune done\n",
662 __func__);
663
664 priv->frequency = frequency;
665 return 0;
666}
667
668static int helene_set_params(struct dvb_frontend *fe)
669{
670 u8 data[MAX_WRITE_REGSIZE];
671 u32 frequency;
672 enum helene_tv_system_t tv_system;
673 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
674 struct helene_priv *priv = fe->tuner_priv;
675 int frequencykHz = p->frequency / 1000;
676
677 dev_dbg(&priv->i2c->dev, "%s(): tune frequency %dkHz\n",
678 __func__, frequencykHz);
679 tv_system = helene_get_tv_system(fe);
680
681 if (tv_system == SONY_HELENE_TV_SYSTEM_UNKNOWN) {
682 dev_dbg(&priv->i2c->dev, "%s(): unknown DTV system\n",
683 __func__);
684 return -EINVAL;
685 }
686 if (priv->set_tuner)
687 priv->set_tuner(priv->set_tuner_data, 1);
688 frequency = roundup(p->frequency / 1000, 25);
689
690 /* mode select */
691 helene_write_reg(priv, 0x01, 0x00);
692
693 /* Disable IF signal output */
694 helene_write_reg(priv, 0x74, 0x02);
695
696 if (priv->state == STATE_SLEEP)
697 helene_leave_power_save(priv);
698
699 /* Initial setting for internal analog block (0x91, 0x92) */
700 if ((tv_system == SONY_HELENE_DTV_DVBC_6) ||
701 (tv_system == SONY_HELENE_DTV_DVBC_8)) {
702 data[0] = 0x16;
703 data[1] = 0x26;
704 } else {
705 data[0] = 0x10;
706 data[1] = 0x20;
707 }
708 helene_write_regs(priv, 0x91, data, 2);
709
710 /* Setting for analog block */
711 if (TERR_INTERNAL_LOOPFILTER_AVAILABLE(tv_system))
712 data[0] = 0x90;
713 else
714 data[0] = 0x00;
715
716 /* Setting for local polarity (0x9D) */
717 data[1] = (uint8_t)(terr_params[tv_system].IS_LOWERLOCAL & 0x01);
718 helene_write_regs(priv, 0x9C, data, 2);
719
720 /* Enable for analog block */
721 data[0] = 0xEE;
722 data[1] = 0x02;
723 data[2] = 0x1E;
724 data[3] = 0x67; /* Tuning setting for CPU */
725
726 /* Setting for PLL reference divider for xtal=24MHz */
727 if ((tv_system == SONY_HELENE_DTV_DVBC_6) ||
728 (tv_system == SONY_HELENE_DTV_DVBC_8))
729 data[4] = 0x18;
730 else
731 data[4] = 0x03;
732
733 /* Tuning setting for analog block */
734 if (TERR_INTERNAL_LOOPFILTER_AVAILABLE(tv_system)) {
735 data[5] = 0x38;
736 data[6] = 0x1E;
737 data[7] = 0x02;
738 data[8] = 0x24;
739 } else if ((tv_system == SONY_HELENE_DTV_DVBC_6) ||
740 (tv_system == SONY_HELENE_DTV_DVBC_8)) {
741 data[5] = 0x1C;
742 data[6] = 0x78;
743 data[7] = 0x08;
744 data[8] = 0x1C;
745 } else {
746 data[5] = 0xB4;
747 data[6] = 0x78;
748 data[7] = 0x08;
749 data[8] = 0x30;
750 }
751 helene_write_regs(priv, 0x5E, data, 9);
752
753 /* LT_AMP_EN should be 0 */
754 helene_set_reg_bits(priv, 0x67, 0x0, 0x02);
755
756 /* Setting for IFOUT_LIMIT */
757 data[0] = 0x00; /* 1.5Vpp */
758
759 /* RF_GAIN setting */
760 if (terr_params[tv_system].RF_GAIN == HELENE_AUTO)
761 data[1] = 0x80; /* RF_GAIN_SEL = 1 */
762 else
763 data[1] = (uint8_t)((terr_params[tv_system].RF_GAIN
764 << 4) & 0x70);
765
766 /* IF_BPF_GC setting */
767 data[1] |= (uint8_t)(terr_params[tv_system].IF_BPF_GC & 0x0F);
768
769 /* Setting for internal RFAGC (0x6A, 0x6B, 0x6C) */
770 data[2] = 0x00;
771 if (frequencykHz <= 172000) {
772 data[3] = (uint8_t)(terr_params[tv_system].RFOVLD_DET_LV1_VL
773 & 0x0F);
774 data[4] = (uint8_t)(terr_params[tv_system].IFOVLD_DET_LV_VL
775 & 0x07);
776 } else if (frequencykHz <= 464000) {
777 data[3] = (uint8_t)(terr_params[tv_system].RFOVLD_DET_LV1_VH
778 & 0x0F);
779 data[4] = (uint8_t)(terr_params[tv_system].IFOVLD_DET_LV_VH
780 & 0x07);
781 } else {
782 data[3] = (uint8_t)(terr_params[tv_system].RFOVLD_DET_LV1_U
783 & 0x0F);
784 data[4] = (uint8_t)(terr_params[tv_system].IFOVLD_DET_LV_U
785 & 0x07);
786 }
787 data[4] |= 0x20;
788
789 /* Setting for IF frequency and bandwidth */
790
791 /* IF filter center frequency offset (IF_BPF_F0) (0x6D) */
792 data[5] = (uint8_t)((terr_params[tv_system].IF_BPF_F0 << 4) & 0x30);
793
794 /* IF filter band width (BW) (0x6D) */
795 data[5] |= (uint8_t)(terr_params[tv_system].BW & 0x03);
796
797 /* IF frequency offset value (FIF_OFFSET) (0x6E) */
798 data[6] = (uint8_t)(terr_params[tv_system].FIF_OFFSET & 0x1F);
799
800 /* IF band width offset value (BW_OFFSET) (0x6F) */
801 data[7] = (uint8_t)(terr_params[tv_system].BW_OFFSET & 0x1F);
802
803 /* RF tuning frequency setting (0x70, 0x71, 0x72) */
804 data[8] = (uint8_t)(frequencykHz & 0xFF); /* FRF_L */
805 data[9] = (uint8_t)((frequencykHz >> 8) & 0xFF); /* FRF_M */
806 data[10] = (uint8_t)((frequencykHz >> 16)
807 & 0x0F); /* FRF_H (bit[3:0]) */
808
809 /* Tuning command */
810 data[11] = 0xFF;
811
812 /* Enable IF output, AGC and IFOUT pin selection (0x74) */
813 data[12] = 0x01;
814
815 if ((tv_system == SONY_HELENE_DTV_DVBC_6) ||
816 (tv_system == SONY_HELENE_DTV_DVBC_8)) {
817 data[13] = 0xD9;
818 data[14] = 0x0F;
819 data[15] = 0x24;
820 data[16] = 0x87;
821 } else {
822 data[13] = 0x99;
823 data[14] = 0x00;
824 data[15] = 0x24;
825 data[16] = 0x87;
826 }
827
828 helene_write_regs(priv, 0x68, data, 17);
829
830 dev_dbg(&priv->i2c->dev, "%s(): tune done\n",
831 __func__);
832
833 priv->frequency = frequency;
834 return 0;
835}
836
837static int helene_get_frequency(struct dvb_frontend *fe, u32 *frequency)
838{
839 struct helene_priv *priv = fe->tuner_priv;
840
841 *frequency = priv->frequency * 1000;
842 return 0;
843}
844
845static struct dvb_tuner_ops helene_tuner_ops = {
846 .info = {
847 .name = "Sony HELENE Ter tuner",
848 .frequency_min = 1000000,
849 .frequency_max = 1200000000,
850 .frequency_step = 25000,
851 },
852 .init = helene_init,
853 .release = helene_release,
854 .sleep = helene_sleep,
855 .set_params = helene_set_params,
856 .get_frequency = helene_get_frequency,
857};
858
859static struct dvb_tuner_ops helene_tuner_ops_s = {
860 .info = {
861 .name = "Sony HELENE Sat tuner",
862 .frequency_min = 500000,
863 .frequency_max = 2500000,
864 .frequency_step = 1000,
865 },
866 .init = helene_init,
867 .release = helene_release,
868 .sleep = helene_sleep,
869 .set_params = helene_set_params_s,
870 .get_frequency = helene_get_frequency,
871};
872
873/* power-on tuner
874 * call once after reset
875 */
876static int helene_x_pon(struct helene_priv *priv)
877{
878 /* RFIN matching in power save (terrestrial) = ACTIVE */
879 /* RFIN matching in power save (satellite) = ACTIVE */
880 u8 dataT[] = { 0x06, 0x00, 0x02, 0x00 };
881 /* SAT_RF_ACTIVE = true, lnaOff = false, terrRfActive = true */
882 u8 dataS[] = { 0x05, 0x06 };
883 u8 cdata[] = {0x7A, 0x01};
884 u8 data[20];
885 u8 rdata[2];
886
887 /* mode select */
888 helene_write_reg(priv, 0x01, 0x00);
889
890 helene_write_reg(priv, 0x67, dataT[3]);
891 helene_write_reg(priv, 0x43, dataS[1]);
892 helene_write_regs(priv, 0x5E, dataT, 3);
893 helene_write_reg(priv, 0x0C, dataS[0]);
894
895 /* Initial setting for internal logic block */
896 helene_write_regs(priv, 0x99, cdata, sizeof(cdata));
897
898 /* 0x81 - 0x94 */
899 data[0] = 0x18; /* xtal 24 MHz */
900 data[1] = (uint8_t)(0x80 | (0x04 & 0x1F)); /* 4 x 25 = 100uA */
901 data[2] = (uint8_t)(0x80 | (0x26 & 0x7F)); /* 38 x 0.25 = 9.5pF */
902 data[3] = 0x80; /* REFOUT signal output 500mVpp */
903 data[4] = 0x00; /* GPIO settings */
904 data[5] = 0x00; /* GPIO settings */
905 data[6] = 0xC4; /* Clock enable for internal logic block */
906 data[7] = 0x40; /* Start CPU boot-up */
907 data[8] = 0x10; /* For burst-write */
908
909 /* Setting for internal RFAGC */
910 data[9] = 0x00;
911 data[10] = 0x45;
912 data[11] = 0x75;
913
914 data[12] = 0x07; /* Setting for analog block */
915
916 /* Initial setting for internal analog block */
917 data[13] = 0x1C;
918 data[14] = 0x3F;
919 data[15] = 0x02;
920 data[16] = 0x10;
921 data[17] = 0x20;
922 data[18] = 0x0A;
923 data[19] = 0x00;
924
925 helene_write_regs(priv, 0x81, data, sizeof(data));
926
927 /* Setting for internal RFAGC */
928 helene_write_reg(priv, 0x9B, 0x00);
929
930 msleep(20);
931
932 /* Check CPU_STT/CPU_ERR */
933 helene_read_regs(priv, 0x1A, rdata, sizeof(rdata));
934
935 if (rdata[0] != 0x00) {
936 dev_err(&priv->i2c->dev,
937 "HELENE tuner CPU error 0x%x\n", rdata[0]);
938 return -EIO;
939 }
940
941 /* VCO current setting */
942 cdata[0] = 0x90;
943 cdata[1] = 0x06;
944 helene_write_regs(priv, 0x17, cdata, sizeof(cdata));
945 msleep(20);
946 helene_read_reg(priv, 0x19, data);
947 helene_write_reg(priv, 0x95, (uint8_t)((data[0] >> 4) & 0x0F));
948
949 /* Disable IF signal output */
950 helene_write_reg(priv, 0x74, 0x02);
951
952 /* Standby setting for CPU */
953 helene_write_reg(priv, 0x88, 0x00);
954
955 /* Standby setting for internal logic block */
956 helene_write_reg(priv, 0x87, 0xC0);
957
958 /* Load capacitance control setting for crystal oscillator */
959 helene_write_reg(priv, 0x80, 0x01);
960
961 /* Satellite initial setting */
962 cdata[0] = 0x07;
963 cdata[1] = 0x00;
964 helene_write_regs(priv, 0x41, cdata, sizeof(cdata));
965
966 dev_info(&priv->i2c->dev,
967 "HELENE tuner x_pon done\n");
968
969 return 0;
970}
971
972struct dvb_frontend *helene_attach_s(struct dvb_frontend *fe,
973 const struct helene_config *config,
974 struct i2c_adapter *i2c)
975{
976 struct helene_priv *priv = NULL;
977
978 priv = kzalloc(sizeof(struct helene_priv), GFP_KERNEL);
979 if (priv == NULL)
980 return NULL;
981 priv->i2c_address = (config->i2c_address >> 1);
982 priv->i2c = i2c;
983 priv->set_tuner_data = config->set_tuner_priv;
984 priv->set_tuner = config->set_tuner_callback;
985 priv->xtal = config->xtal;
986
987 if (fe->ops.i2c_gate_ctrl)
988 fe->ops.i2c_gate_ctrl(fe, 1);
989
990 if (helene_x_pon(priv) != 0)
991 return NULL;
992
993 if (fe->ops.i2c_gate_ctrl)
994 fe->ops.i2c_gate_ctrl(fe, 0);
995
996 memcpy(&fe->ops.tuner_ops, &helene_tuner_ops_s,
997 sizeof(struct dvb_tuner_ops));
998 fe->tuner_priv = priv;
999 dev_info(&priv->i2c->dev,
1000 "Sony HELENE Sat attached on addr=%x at I2C adapter %p\n",
1001 priv->i2c_address, priv->i2c);
1002 return fe;
1003}
1004EXPORT_SYMBOL(helene_attach_s);
1005
1006struct dvb_frontend *helene_attach(struct dvb_frontend *fe,
1007 const struct helene_config *config,
1008 struct i2c_adapter *i2c)
1009{
1010 struct helene_priv *priv = NULL;
1011
1012 priv = kzalloc(sizeof(struct helene_priv), GFP_KERNEL);
1013 if (priv == NULL)
1014 return NULL;
1015 priv->i2c_address = (config->i2c_address >> 1);
1016 priv->i2c = i2c;
1017 priv->set_tuner_data = config->set_tuner_priv;
1018 priv->set_tuner = config->set_tuner_callback;
1019 priv->xtal = config->xtal;
1020
1021 if (fe->ops.i2c_gate_ctrl)
1022 fe->ops.i2c_gate_ctrl(fe, 1);
1023
1024 if (helene_x_pon(priv) != 0)
1025 return NULL;
1026
1027 if (fe->ops.i2c_gate_ctrl)
1028 fe->ops.i2c_gate_ctrl(fe, 0);
1029
1030 memcpy(&fe->ops.tuner_ops, &helene_tuner_ops,
1031 sizeof(struct dvb_tuner_ops));
1032 fe->tuner_priv = priv;
1033 dev_info(&priv->i2c->dev,
1034 "Sony HELENE Ter attached on addr=%x at I2C adapter %p\n",
1035 priv->i2c_address, priv->i2c);
1036 return fe;
1037}
1038EXPORT_SYMBOL(helene_attach);
1039
1040MODULE_DESCRIPTION("Sony HELENE Sat/Ter tuner driver");
1041MODULE_AUTHOR("Abylay Ospan <aospan@netup.ru>");
1042MODULE_LICENSE("GPL");
diff --git a/drivers/media/dvb-frontends/helene.h b/drivers/media/dvb-frontends/helene.h
new file mode 100644
index 000000000000..e1b9224cfc55
--- /dev/null
+++ b/drivers/media/dvb-frontends/helene.h
@@ -0,0 +1,79 @@
1/*
2 * helene.h
3 *
4 * Sony HELENE DVB-S/S2/T/T2/C/C2/ISDB-T/S tuner driver (CXD2858ER)
5 *
6 * Copyright 2012 Sony Corporation
7 * Copyright (C) 2014 NetUP Inc.
8 * Copyright (C) 2014 Abylay Ospan <aospan@netup.ru>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 */
20
21#ifndef __DVB_HELENE_H__
22#define __DVB_HELENE_H__
23
24#include <linux/kconfig.h>
25#include <linux/dvb/frontend.h>
26#include <linux/i2c.h>
27
28enum helene_xtal {
29 SONY_HELENE_XTAL_16000, /* 16 MHz */
30 SONY_HELENE_XTAL_20500, /* 20.5 MHz */
31 SONY_HELENE_XTAL_24000, /* 24 MHz */
32 SONY_HELENE_XTAL_41000 /* 41 MHz */
33};
34
35/**
36 * struct helene_config - the configuration of 'Helene' tuner driver
37 * @i2c_address: I2C address of the tuner
38 * @xtal_freq_mhz: Oscillator frequency, MHz
39 * @set_tuner_priv: Callback function private context
40 * @set_tuner_callback: Callback function that notifies the parent driver
41 * which tuner is active now
42 */
43struct helene_config {
44 u8 i2c_address;
45 u8 xtal_freq_mhz;
46 void *set_tuner_priv;
47 int (*set_tuner_callback)(void *, int);
48 enum helene_xtal xtal;
49};
50
51#if IS_REACHABLE(CONFIG_DVB_HELENE)
52extern struct dvb_frontend *helene_attach(struct dvb_frontend *fe,
53 const struct helene_config *config,
54 struct i2c_adapter *i2c);
55#else
56static inline struct dvb_frontend *helene_attach(struct dvb_frontend *fe,
57 const struct helene_config *config,
58 struct i2c_adapter *i2c)
59{
60 pr_warn("%s: driver disabled by Kconfig\n", __func__);
61 return NULL;
62}
63#endif
64
65#if IS_REACHABLE(CONFIG_DVB_HELENE)
66extern struct dvb_frontend *helene_attach_s(struct dvb_frontend *fe,
67 const struct helene_config *config,
68 struct i2c_adapter *i2c);
69#else
70static inline struct dvb_frontend *helene_attach_s(struct dvb_frontend *fe,
71 const struct helene_config *config,
72 struct i2c_adapter *i2c)
73{
74 pr_warn("%s: driver disabled by Kconfig\n", __func__);
75 return NULL;
76}
77#endif
78
79#endif
diff --git a/drivers/media/dvb-frontends/horus3a.c b/drivers/media/dvb-frontends/horus3a.c
index 000606af70f7..a98bca5270d9 100644
--- a/drivers/media/dvb-frontends/horus3a.c
+++ b/drivers/media/dvb-frontends/horus3a.c
@@ -66,7 +66,7 @@ static int horus3a_write_regs(struct horus3a_priv *priv,
66 } 66 }
67 }; 67 };
68 68
69 if (len + 1 >= sizeof(buf)) { 69 if (len + 1 > sizeof(buf)) {
70 dev_warn(&priv->i2c->dev,"wr reg=%04x: len=%d is too big!\n", 70 dev_warn(&priv->i2c->dev,"wr reg=%04x: len=%d is too big!\n",
71 reg, len + 1); 71 reg, len + 1);
72 return -E2BIG; 72 return -E2BIG;
@@ -272,24 +272,6 @@ static int horus3a_set_params(struct dvb_frontend *fe)
272 if (fc_lpf > 36) 272 if (fc_lpf > 36)
273 fc_lpf = 36; 273 fc_lpf = 36;
274 } else if (p->delivery_system == SYS_DVBS2) { 274 } else if (p->delivery_system == SYS_DVBS2) {
275 int rolloff;
276
277 switch (p->rolloff) {
278 case ROLLOFF_35:
279 rolloff = 35;
280 break;
281 case ROLLOFF_25:
282 rolloff = 25;
283 break;
284 case ROLLOFF_20:
285 rolloff = 20;
286 break;
287 case ROLLOFF_AUTO:
288 default:
289 dev_err(&priv->i2c->dev,
290 "horus3a: auto roll-off is not supported\n");
291 return -EINVAL;
292 }
293 /* 275 /*
294 * SR <= 4.5: 276 * SR <= 4.5:
295 * fc_lpf = 5 277 * fc_lpf = 5
@@ -302,11 +284,9 @@ static int horus3a_set_params(struct dvb_frontend *fe)
302 if (symbol_rate <= 4500) 284 if (symbol_rate <= 4500)
303 fc_lpf = 5; 285 fc_lpf = 5;
304 else if (symbol_rate <= 10000) 286 else if (symbol_rate <= 10000)
305 fc_lpf = (u8)DIV_ROUND_UP( 287 fc_lpf = (u8)((symbol_rate * 11 + (10000-1)) / 10000);
306 symbol_rate * (200 + rolloff), 200000);
307 else 288 else
308 fc_lpf = (u8)DIV_ROUND_UP( 289 fc_lpf = (u8)((symbol_rate * 3 + (5000-1)) / 5000 + 5);
309 symbol_rate * (100 + rolloff), 200000) + 5;
310 /* 5 <= fc_lpf <= 36 is valid */ 290 /* 5 <= fc_lpf <= 36 is valid */
311 if (fc_lpf > 36) 291 if (fc_lpf > 36)
312 fc_lpf = 36; 292 fc_lpf = 36;
diff --git a/drivers/media/dvb-frontends/m88ds3103.c b/drivers/media/dvb-frontends/m88ds3103.c
index 5557ef8fc704..e0fe5bc9dbce 100644
--- a/drivers/media/dvb-frontends/m88ds3103.c
+++ b/drivers/media/dvb-frontends/m88ds3103.c
@@ -306,8 +306,8 @@ static int m88ds3103_set_frontend(struct dvb_frontend *fe)
306 const struct m88ds3103_reg_val *init; 306 const struct m88ds3103_reg_val *init;
307 u8 u8tmp, u8tmp1 = 0, u8tmp2 = 0; /* silence compiler warning */ 307 u8 u8tmp, u8tmp1 = 0, u8tmp2 = 0; /* silence compiler warning */
308 u8 buf[3]; 308 u8 buf[3];
309 u16 u16tmp, divide_ratio = 0; 309 u16 u16tmp;
310 u32 tuner_frequency, target_mclk; 310 u32 tuner_frequency_khz, target_mclk;
311 s32 s32tmp; 311 s32 s32tmp;
312 312
313 dev_dbg(&client->dev, 313 dev_dbg(&client->dev,
@@ -344,7 +344,7 @@ static int m88ds3103_set_frontend(struct dvb_frontend *fe)
344 } 344 }
345 345
346 if (fe->ops.tuner_ops.get_frequency) { 346 if (fe->ops.tuner_ops.get_frequency) {
347 ret = fe->ops.tuner_ops.get_frequency(fe, &tuner_frequency); 347 ret = fe->ops.tuner_ops.get_frequency(fe, &tuner_frequency_khz);
348 if (ret) 348 if (ret)
349 goto err; 349 goto err;
350 } else { 350 } else {
@@ -353,20 +353,20 @@ static int m88ds3103_set_frontend(struct dvb_frontend *fe)
353 * actual frequency used. Carrier offset calculation is not 353 * actual frequency used. Carrier offset calculation is not
354 * valid. 354 * valid.
355 */ 355 */
356 tuner_frequency = c->frequency; 356 tuner_frequency_khz = c->frequency;
357 } 357 }
358 358
359 /* select M88RS6000 demod main mclk and ts mclk from tuner die. */ 359 /* select M88RS6000 demod main mclk and ts mclk from tuner die. */
360 if (dev->chip_id == M88RS6000_CHIP_ID) { 360 if (dev->chip_id == M88RS6000_CHIP_ID) {
361 if (c->symbol_rate > 45010000) 361 if (c->symbol_rate > 45010000)
362 dev->mclk_khz = 110250; 362 dev->mclk = 110250000;
363 else 363 else
364 dev->mclk_khz = 96000; 364 dev->mclk = 96000000;
365 365
366 if (c->delivery_system == SYS_DVBS) 366 if (c->delivery_system == SYS_DVBS)
367 target_mclk = 96000; 367 target_mclk = 96000000;
368 else 368 else
369 target_mclk = 144000; 369 target_mclk = 144000000;
370 370
371 /* Enable demod clock path */ 371 /* Enable demod clock path */
372 ret = regmap_write(dev->regmap, 0x06, 0x00); 372 ret = regmap_write(dev->regmap, 0x06, 0x00);
@@ -375,7 +375,7 @@ static int m88ds3103_set_frontend(struct dvb_frontend *fe)
375 usleep_range(10000, 20000); 375 usleep_range(10000, 20000);
376 } else { 376 } else {
377 /* set M88DS3103 mclk and ts mclk. */ 377 /* set M88DS3103 mclk and ts mclk. */
378 dev->mclk_khz = 96000; 378 dev->mclk = 96000000;
379 379
380 switch (dev->cfg->ts_mode) { 380 switch (dev->cfg->ts_mode) {
381 case M88DS3103_TS_SERIAL: 381 case M88DS3103_TS_SERIAL:
@@ -385,14 +385,14 @@ static int m88ds3103_set_frontend(struct dvb_frontend *fe)
385 case M88DS3103_TS_PARALLEL: 385 case M88DS3103_TS_PARALLEL:
386 case M88DS3103_TS_CI: 386 case M88DS3103_TS_CI:
387 if (c->delivery_system == SYS_DVBS) 387 if (c->delivery_system == SYS_DVBS)
388 target_mclk = 96000; 388 target_mclk = 96000000;
389 else { 389 else {
390 if (c->symbol_rate < 18000000) 390 if (c->symbol_rate < 18000000)
391 target_mclk = 96000; 391 target_mclk = 96000000;
392 else if (c->symbol_rate < 28000000) 392 else if (c->symbol_rate < 28000000)
393 target_mclk = 144000; 393 target_mclk = 144000000;
394 else 394 else
395 target_mclk = 192000; 395 target_mclk = 192000000;
396 } 396 }
397 break; 397 break;
398 default: 398 default:
@@ -402,15 +402,15 @@ static int m88ds3103_set_frontend(struct dvb_frontend *fe)
402 } 402 }
403 403
404 switch (target_mclk) { 404 switch (target_mclk) {
405 case 96000: 405 case 96000000:
406 u8tmp1 = 0x02; /* 0b10 */ 406 u8tmp1 = 0x02; /* 0b10 */
407 u8tmp2 = 0x01; /* 0b01 */ 407 u8tmp2 = 0x01; /* 0b01 */
408 break; 408 break;
409 case 144000: 409 case 144000000:
410 u8tmp1 = 0x00; /* 0b00 */ 410 u8tmp1 = 0x00; /* 0b00 */
411 u8tmp2 = 0x01; /* 0b01 */ 411 u8tmp2 = 0x01; /* 0b01 */
412 break; 412 break;
413 case 192000: 413 case 192000000:
414 u8tmp1 = 0x03; /* 0b11 */ 414 u8tmp1 = 0x03; /* 0b11 */
415 u8tmp2 = 0x00; /* 0b00 */ 415 u8tmp2 = 0x00; /* 0b00 */
416 break; 416 break;
@@ -464,8 +464,8 @@ static int m88ds3103_set_frontend(struct dvb_frontend *fe)
464 } 464 }
465 465
466 if (dev->chip_id == M88RS6000_CHIP_ID) { 466 if (dev->chip_id == M88RS6000_CHIP_ID) {
467 if ((c->delivery_system == SYS_DVBS2) 467 if (c->delivery_system == SYS_DVBS2 &&
468 && ((c->symbol_rate / 1000) <= 5000)) { 468 c->symbol_rate <= 5000000) {
469 ret = regmap_write(dev->regmap, 0xc0, 0x04); 469 ret = regmap_write(dev->regmap, 0xc0, 0x04);
470 if (ret) 470 if (ret)
471 goto err; 471 goto err;
@@ -522,37 +522,25 @@ static int m88ds3103_set_frontend(struct dvb_frontend *fe)
522 ret = m88ds3103_update_bits(dev, 0x29, 0x20, u8tmp1); 522 ret = m88ds3103_update_bits(dev, 0x29, 0x20, u8tmp1);
523 if (ret) 523 if (ret)
524 goto err; 524 goto err;
525 u8tmp1 = 0; 525 u16tmp = 0;
526 u8tmp2 = 0; 526 u8tmp1 = 0x3f;
527 u8tmp2 = 0x3f;
527 break; 528 break;
528 default: 529 default:
529 if (dev->cfg->ts_clk) { 530 u16tmp = DIV_ROUND_UP(target_mclk, dev->cfg->ts_clk);
530 divide_ratio = DIV_ROUND_UP(target_mclk, dev->cfg->ts_clk); 531 u8tmp1 = u16tmp / 2 - 1;
531 u8tmp1 = divide_ratio / 2; 532 u8tmp2 = DIV_ROUND_UP(u16tmp, 2) - 1;
532 u8tmp2 = DIV_ROUND_UP(divide_ratio, 2);
533 }
534 } 533 }
535 534
536 dev_dbg(&client->dev, 535 dev_dbg(&client->dev, "target_mclk=%u ts_clk=%u ts_clk_divide_ratio=%u\n",
537 "target_mclk=%d ts_clk=%d divide_ratio=%d\n", 536 target_mclk, dev->cfg->ts_clk, u16tmp);
538 target_mclk, dev->cfg->ts_clk, divide_ratio);
539 537
540 u8tmp1--;
541 u8tmp2--;
542 /* u8tmp1[5:2] => fe[3:0], u8tmp1[1:0] => ea[7:6] */ 538 /* u8tmp1[5:2] => fe[3:0], u8tmp1[1:0] => ea[7:6] */
543 u8tmp1 &= 0x3f;
544 /* u8tmp2[5:0] => ea[5:0] */ 539 /* u8tmp2[5:0] => ea[5:0] */
545 u8tmp2 &= 0x3f; 540 u8tmp = (u8tmp1 >> 2) & 0x0f;
546 541 ret = regmap_update_bits(dev->regmap, 0xfe, 0x0f, u8tmp);
547 ret = regmap_bulk_read(dev->regmap, 0xfe, &u8tmp, 1);
548 if (ret) 542 if (ret)
549 goto err; 543 goto err;
550
551 u8tmp = ((u8tmp & 0xf0) << 0) | u8tmp1 >> 2;
552 ret = regmap_write(dev->regmap, 0xfe, u8tmp);
553 if (ret)
554 goto err;
555
556 u8tmp = ((u8tmp1 & 0x03) << 6) | u8tmp2 >> 0; 544 u8tmp = ((u8tmp1 & 0x03) << 6) | u8tmp2 >> 0;
557 ret = regmap_write(dev->regmap, 0xea, u8tmp); 545 ret = regmap_write(dev->regmap, 0xea, u8tmp);
558 if (ret) 546 if (ret)
@@ -581,7 +569,7 @@ static int m88ds3103_set_frontend(struct dvb_frontend *fe)
581 if (ret) 569 if (ret)
582 goto err; 570 goto err;
583 571
584 u16tmp = DIV_ROUND_CLOSEST((c->symbol_rate / 1000) << 15, dev->mclk_khz / 2); 572 u16tmp = DIV_ROUND_CLOSEST_ULL((u64)c->symbol_rate * 0x10000, dev->mclk);
585 buf[0] = (u16tmp >> 0) & 0xff; 573 buf[0] = (u16tmp >> 0) & 0xff;
586 buf[1] = (u16tmp >> 8) & 0xff; 574 buf[1] = (u16tmp >> 8) & 0xff;
587 ret = regmap_bulk_write(dev->regmap, 0x61, buf, 2); 575 ret = regmap_bulk_write(dev->regmap, 0x61, buf, 2);
@@ -601,13 +589,11 @@ static int m88ds3103_set_frontend(struct dvb_frontend *fe)
601 goto err; 589 goto err;
602 590
603 dev_dbg(&client->dev, "carrier offset=%d\n", 591 dev_dbg(&client->dev, "carrier offset=%d\n",
604 (tuner_frequency - c->frequency)); 592 (tuner_frequency_khz - c->frequency));
605
606 s32tmp = 0x10000 * (tuner_frequency - c->frequency);
607 s32tmp = DIV_ROUND_CLOSEST(s32tmp, dev->mclk_khz);
608 if (s32tmp < 0)
609 s32tmp += 0x10000;
610 593
594 /* Use 32-bit calc as there is no s64 version of DIV_ROUND_CLOSEST() */
595 s32tmp = 0x10000 * (tuner_frequency_khz - c->frequency);
596 s32tmp = DIV_ROUND_CLOSEST(s32tmp, dev->mclk / 1000);
611 buf[0] = (s32tmp >> 0) & 0xff; 597 buf[0] = (s32tmp >> 0) & 0xff;
612 buf[1] = (s32tmp >> 8) & 0xff; 598 buf[1] = (s32tmp >> 8) & 0xff;
613 ret = regmap_bulk_write(dev->regmap, 0x5e, buf, 2); 599 ret = regmap_bulk_write(dev->regmap, 0x5e, buf, 2);
@@ -635,10 +621,10 @@ static int m88ds3103_init(struct dvb_frontend *fe)
635 struct m88ds3103_dev *dev = fe->demodulator_priv; 621 struct m88ds3103_dev *dev = fe->demodulator_priv;
636 struct i2c_client *client = dev->client; 622 struct i2c_client *client = dev->client;
637 struct dtv_frontend_properties *c = &fe->dtv_property_cache; 623 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
638 int ret, len, remaining; 624 int ret, len, rem;
639 unsigned int utmp; 625 unsigned int utmp;
640 const struct firmware *fw = NULL; 626 const struct firmware *firmware;
641 u8 *fw_file; 627 const char *name;
642 628
643 dev_dbg(&client->dev, "\n"); 629 dev_dbg(&client->dev, "\n");
644 630
@@ -664,7 +650,7 @@ static int m88ds3103_init(struct dvb_frontend *fe)
664 dev_dbg(&client->dev, "firmware=%02x\n", utmp); 650 dev_dbg(&client->dev, "firmware=%02x\n", utmp);
665 651
666 if (utmp) 652 if (utmp)
667 goto skip_fw_download; 653 goto warm;
668 654
669 /* global reset, global diseqc reset, golbal fec reset */ 655 /* global reset, global diseqc reset, golbal fec reset */
670 ret = regmap_write(dev->regmap, 0x07, 0xe0); 656 ret = regmap_write(dev->regmap, 0x07, 0xe0);
@@ -679,52 +665,47 @@ static int m88ds3103_init(struct dvb_frontend *fe)
679 m88ds3103_ops.info.name); 665 m88ds3103_ops.info.name);
680 666
681 if (dev->chip_id == M88RS6000_CHIP_ID) 667 if (dev->chip_id == M88RS6000_CHIP_ID)
682 fw_file = M88RS6000_FIRMWARE; 668 name = M88RS6000_FIRMWARE;
683 else 669 else
684 fw_file = M88DS3103_FIRMWARE; 670 name = M88DS3103_FIRMWARE;
685 /* request the firmware, this will block and timeout */ 671 /* request the firmware, this will block and timeout */
686 ret = request_firmware(&fw, fw_file, &client->dev); 672 ret = request_firmware(&firmware, name, &client->dev);
687 if (ret) { 673 if (ret) {
688 dev_err(&client->dev, "firmware file '%s' not found\n", fw_file); 674 dev_err(&client->dev, "firmware file '%s' not found\n", name);
689 goto err; 675 goto err;
690 } 676 }
691 677
692 dev_info(&client->dev, "downloading firmware from file '%s'\n", 678 dev_info(&client->dev, "downloading firmware from file '%s'\n", name);
693 fw_file);
694 679
695 ret = regmap_write(dev->regmap, 0xb2, 0x01); 680 ret = regmap_write(dev->regmap, 0xb2, 0x01);
696 if (ret) 681 if (ret)
697 goto error_fw_release; 682 goto err_release_firmware;
698
699 for (remaining = fw->size; remaining > 0;
700 remaining -= (dev->cfg->i2c_wr_max - 1)) {
701 len = remaining;
702 if (len > (dev->cfg->i2c_wr_max - 1))
703 len = (dev->cfg->i2c_wr_max - 1);
704 683
684 for (rem = firmware->size; rem > 0; rem -= (dev->cfg->i2c_wr_max - 1)) {
685 len = min(dev->cfg->i2c_wr_max - 1, rem);
705 ret = regmap_bulk_write(dev->regmap, 0xb0, 686 ret = regmap_bulk_write(dev->regmap, 0xb0,
706 &fw->data[fw->size - remaining], len); 687 &firmware->data[firmware->size - rem],
688 len);
707 if (ret) { 689 if (ret) {
708 dev_err(&client->dev, "firmware download failed=%d\n", 690 dev_err(&client->dev, "firmware download failed %d\n",
709 ret); 691 ret);
710 goto error_fw_release; 692 goto err_release_firmware;
711 } 693 }
712 } 694 }
713 695
714 ret = regmap_write(dev->regmap, 0xb2, 0x00); 696 ret = regmap_write(dev->regmap, 0xb2, 0x00);
715 if (ret) 697 if (ret)
716 goto error_fw_release; 698 goto err_release_firmware;
717 699
718 release_firmware(fw); 700 release_firmware(firmware);
719 fw = NULL;
720 701
721 ret = regmap_read(dev->regmap, 0xb9, &utmp); 702 ret = regmap_read(dev->regmap, 0xb9, &utmp);
722 if (ret) 703 if (ret)
723 goto err; 704 goto err;
724 705
725 if (!utmp) { 706 if (!utmp) {
707 ret = -EINVAL;
726 dev_info(&client->dev, "firmware did not run\n"); 708 dev_info(&client->dev, "firmware did not run\n");
727 ret = -EFAULT;
728 goto err; 709 goto err;
729 } 710 }
730 711
@@ -733,7 +714,7 @@ static int m88ds3103_init(struct dvb_frontend *fe)
733 dev_info(&client->dev, "firmware version: %X.%X\n", 714 dev_info(&client->dev, "firmware version: %X.%X\n",
734 (utmp >> 4) & 0xf, (utmp >> 0 & 0xf)); 715 (utmp >> 4) & 0xf, (utmp >> 0 & 0xf));
735 716
736skip_fw_download: 717warm:
737 /* warm state */ 718 /* warm state */
738 dev->warm = true; 719 dev->warm = true;
739 720
@@ -746,8 +727,8 @@ skip_fw_download:
746 c->post_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE; 727 c->post_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
747 728
748 return 0; 729 return 0;
749error_fw_release: 730err_release_firmware:
750 release_firmware(fw); 731 release_firmware(firmware);
751err: 732err:
752 dev_dbg(&client->dev, "failed=%d\n", ret); 733 dev_dbg(&client->dev, "failed=%d\n", ret);
753 return ret; 734 return ret;
@@ -952,8 +933,7 @@ static int m88ds3103_get_frontend(struct dvb_frontend *fe,
952 if (ret) 933 if (ret)
953 goto err; 934 goto err;
954 935
955 c->symbol_rate = 1ull * ((buf[1] << 8) | (buf[0] << 0)) * 936 c->symbol_rate = DIV_ROUND_CLOSEST_ULL((u64)(buf[1] << 8 | buf[0] << 0) * dev->mclk, 0x10000);
956 dev->mclk_khz * 1000 / 0x10000;
957 937
958 return 0; 938 return 0;
959err: 939err:
@@ -1119,8 +1099,9 @@ static int m88ds3103_diseqc_send_master_cmd(struct dvb_frontend *fe,
1119 #define SEND_MASTER_CMD_TIMEOUT 120 1099 #define SEND_MASTER_CMD_TIMEOUT 120
1120 timeout = jiffies + msecs_to_jiffies(SEND_MASTER_CMD_TIMEOUT); 1100 timeout = jiffies + msecs_to_jiffies(SEND_MASTER_CMD_TIMEOUT);
1121 1101
1122 /* DiSEqC message typical period is 54 ms */ 1102 /* DiSEqC message period is 13.5 ms per byte */
1123 usleep_range(50000, 54000); 1103 utmp = diseqc_cmd->msg_len * 13500;
1104 usleep_range(utmp - 4000, utmp);
1124 1105
1125 for (utmp = 1; !time_after(jiffies, timeout) && utmp;) { 1106 for (utmp = 1; !time_after(jiffies, timeout) && utmp;) {
1126 ret = regmap_read(dev->regmap, 0xa1, &utmp); 1107 ret = regmap_read(dev->regmap, 0xa1, &utmp);
@@ -1395,7 +1376,7 @@ static int m88ds3103_probe(struct i2c_client *client,
1395 dev->config.clock = pdata->clk; 1376 dev->config.clock = pdata->clk;
1396 dev->config.i2c_wr_max = pdata->i2c_wr_max; 1377 dev->config.i2c_wr_max = pdata->i2c_wr_max;
1397 dev->config.ts_mode = pdata->ts_mode; 1378 dev->config.ts_mode = pdata->ts_mode;
1398 dev->config.ts_clk = pdata->ts_clk; 1379 dev->config.ts_clk = pdata->ts_clk * 1000;
1399 dev->config.ts_clk_pol = pdata->ts_clk_pol; 1380 dev->config.ts_clk_pol = pdata->ts_clk_pol;
1400 dev->config.spec_inv = pdata->spec_inv; 1381 dev->config.spec_inv = pdata->spec_inv;
1401 dev->config.agc_inv = pdata->agc_inv; 1382 dev->config.agc_inv = pdata->agc_inv;
@@ -1446,6 +1427,11 @@ static int m88ds3103_probe(struct i2c_client *client,
1446 goto err_kfree; 1427 goto err_kfree;
1447 } 1428 }
1448 1429
1430 if (!pdata->ts_clk) {
1431 ret = -EINVAL;
1432 goto err_kfree;
1433 }
1434
1449 /* 0x29 register is defined differently for m88rs6000. */ 1435 /* 0x29 register is defined differently for m88rs6000. */
1450 /* set internal tuner address to 0x21 */ 1436 /* set internal tuner address to 0x21 */
1451 if (dev->chip_id == M88RS6000_CHIP_ID) 1437 if (dev->chip_id == M88RS6000_CHIP_ID)
diff --git a/drivers/media/dvb-frontends/m88ds3103_priv.h b/drivers/media/dvb-frontends/m88ds3103_priv.h
index d78e467295d2..07f20c269c67 100644
--- a/drivers/media/dvb-frontends/m88ds3103_priv.h
+++ b/drivers/media/dvb-frontends/m88ds3103_priv.h
@@ -27,7 +27,6 @@
27 27
28#define M88DS3103_FIRMWARE "dvb-demod-m88ds3103.fw" 28#define M88DS3103_FIRMWARE "dvb-demod-m88ds3103.fw"
29#define M88RS6000_FIRMWARE "dvb-demod-m88rs6000.fw" 29#define M88RS6000_FIRMWARE "dvb-demod-m88rs6000.fw"
30#define M88DS3103_MCLK_KHZ 96000
31#define M88RS6000_CHIP_ID 0x74 30#define M88RS6000_CHIP_ID 0x74
32#define M88DS3103_CHIP_ID 0x70 31#define M88DS3103_CHIP_ID 0x70
33 32
@@ -46,7 +45,7 @@ struct m88ds3103_dev {
46 /* auto detect chip id to do different config */ 45 /* auto detect chip id to do different config */
47 u8 chip_id; 46 u8 chip_id;
48 /* main mclk is calculated for M88RS6000 dynamically */ 47 /* main mclk is calculated for M88RS6000 dynamically */
49 s32 mclk_khz; 48 s32 mclk;
50 u64 post_bit_error; 49 u64 post_bit_error;
51 u64 post_bit_count; 50 u64 post_bit_count;
52}; 51};
diff --git a/drivers/media/dvb-frontends/m88rs2000.c b/drivers/media/dvb-frontends/m88rs2000.c
index a09b12313a73..ef79a4ec31e2 100644
--- a/drivers/media/dvb-frontends/m88rs2000.c
+++ b/drivers/media/dvb-frontends/m88rs2000.c
@@ -609,7 +609,7 @@ static int m88rs2000_set_frontend(struct dvb_frontend *fe)
609{ 609{
610 struct m88rs2000_state *state = fe->demodulator_priv; 610 struct m88rs2000_state *state = fe->demodulator_priv;
611 struct dtv_frontend_properties *c = &fe->dtv_property_cache; 611 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
612 enum fe_status status; 612 enum fe_status status = 0;
613 int i, ret = 0; 613 int i, ret = 0;
614 u32 tuner_freq; 614 u32 tuner_freq;
615 s16 offset = 0; 615 s16 offset = 0;
diff --git a/drivers/media/dvb-frontends/mb86a20s.c b/drivers/media/dvb-frontends/mb86a20s.c
index fb88dddaf3a3..41325328a22e 100644
--- a/drivers/media/dvb-frontends/mb86a20s.c
+++ b/drivers/media/dvb-frontends/mb86a20s.c
@@ -301,10 +301,11 @@ static int mb86a20s_read_status(struct dvb_frontend *fe, enum fe_status *status)
301 301
302 *status = 0; 302 *status = 0;
303 303
304 val = mb86a20s_readreg(state, 0x0a) & 0xf; 304 val = mb86a20s_readreg(state, 0x0a);
305 if (val < 0) 305 if (val < 0)
306 return val; 306 return val;
307 307
308 val &= 0xf;
308 if (val >= 2) 309 if (val >= 2)
309 *status |= FE_HAS_SIGNAL; 310 *status |= FE_HAS_SIGNAL;
310 311
diff --git a/drivers/staging/media/mn88472/mn88472.c b/drivers/media/dvb-frontends/mn88472.c
index 7ea749cf19f9..18fb2df1e2bd 100644
--- a/drivers/staging/media/mn88472/mn88472.c
+++ b/drivers/media/dvb-frontends/mn88472.c
@@ -17,28 +17,90 @@
17#include "mn88472_priv.h" 17#include "mn88472_priv.h"
18 18
19static int mn88472_get_tune_settings(struct dvb_frontend *fe, 19static int mn88472_get_tune_settings(struct dvb_frontend *fe,
20 struct dvb_frontend_tune_settings *s) 20 struct dvb_frontend_tune_settings *s)
21{ 21{
22 s->min_delay_ms = 800; 22 s->min_delay_ms = 1000;
23 return 0; 23 return 0;
24} 24}
25 25
26static int mn88472_read_status(struct dvb_frontend *fe, enum fe_status *status)
27{
28 struct i2c_client *client = fe->demodulator_priv;
29 struct mn88472_dev *dev = i2c_get_clientdata(client);
30 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
31 int ret;
32 unsigned int utmp;
33
34 if (!dev->active) {
35 ret = -EAGAIN;
36 goto err;
37 }
38
39 switch (c->delivery_system) {
40 case SYS_DVBT:
41 ret = regmap_read(dev->regmap[0], 0x7f, &utmp);
42 if (ret)
43 goto err;
44 if ((utmp & 0x0f) >= 0x09)
45 *status = FE_HAS_SIGNAL | FE_HAS_CARRIER |
46 FE_HAS_VITERBI | FE_HAS_SYNC | FE_HAS_LOCK;
47 else
48 *status = 0;
49 break;
50 case SYS_DVBT2:
51 ret = regmap_read(dev->regmap[2], 0x92, &utmp);
52 if (ret)
53 goto err;
54 if ((utmp & 0x0f) >= 0x0d)
55 *status = FE_HAS_SIGNAL | FE_HAS_CARRIER |
56 FE_HAS_VITERBI | FE_HAS_SYNC | FE_HAS_LOCK;
57 else if ((utmp & 0x0f) >= 0x0a)
58 *status = FE_HAS_SIGNAL | FE_HAS_CARRIER |
59 FE_HAS_VITERBI;
60 else if ((utmp & 0x0f) >= 0x07)
61 *status = FE_HAS_SIGNAL | FE_HAS_CARRIER;
62 else
63 *status = 0;
64 break;
65 case SYS_DVBC_ANNEX_A:
66 ret = regmap_read(dev->regmap[1], 0x84, &utmp);
67 if (ret)
68 goto err;
69 if ((utmp & 0x0f) >= 0x08)
70 *status = FE_HAS_SIGNAL | FE_HAS_CARRIER |
71 FE_HAS_VITERBI | FE_HAS_SYNC | FE_HAS_LOCK;
72 else
73 *status = 0;
74 break;
75 default:
76 ret = -EINVAL;
77 goto err;
78 }
79
80 return 0;
81err:
82 dev_dbg(&client->dev, "failed=%d\n", ret);
83 return ret;
84}
85
26static int mn88472_set_frontend(struct dvb_frontend *fe) 86static int mn88472_set_frontend(struct dvb_frontend *fe)
27{ 87{
28 struct i2c_client *client = fe->demodulator_priv; 88 struct i2c_client *client = fe->demodulator_priv;
29 struct mn88472_dev *dev = i2c_get_clientdata(client); 89 struct mn88472_dev *dev = i2c_get_clientdata(client);
30 struct dtv_frontend_properties *c = &fe->dtv_property_cache; 90 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
31 int ret, i; 91 int ret, i;
32 u32 if_frequency = 0; 92 unsigned int utmp;
33 u64 tmp; 93 u32 if_frequency;
34 u8 delivery_system_val, if_val[3], bw_val[7], bw_val2; 94 u8 buf[3], delivery_system_val, bandwidth_val, *bandwidth_vals_ptr;
95 u8 reg_bank0_b4_val, reg_bank0_cd_val, reg_bank0_d4_val;
96 u8 reg_bank0_d6_val;
35 97
36 dev_dbg(&client->dev, 98 dev_dbg(&client->dev,
37 "delivery_system=%d modulation=%d frequency=%d symbol_rate=%d inversion=%d\n", 99 "delivery_system=%u modulation=%u frequency=%u bandwidth_hz=%u symbol_rate=%u inversion=%d stream_id=%d\n",
38 c->delivery_system, c->modulation, 100 c->delivery_system, c->modulation, c->frequency,
39 c->frequency, c->symbol_rate, c->inversion); 101 c->bandwidth_hz, c->symbol_rate, c->inversion, c->stream_id);
40 102
41 if (!dev->warm) { 103 if (!dev->active) {
42 ret = -EAGAIN; 104 ret = -EAGAIN;
43 goto err; 105 goto err;
44 } 106 }
@@ -46,39 +108,64 @@ static int mn88472_set_frontend(struct dvb_frontend *fe)
46 switch (c->delivery_system) { 108 switch (c->delivery_system) {
47 case SYS_DVBT: 109 case SYS_DVBT:
48 delivery_system_val = 0x02; 110 delivery_system_val = 0x02;
111 reg_bank0_b4_val = 0x00;
112 reg_bank0_cd_val = 0x1f;
113 reg_bank0_d4_val = 0x0a;
114 reg_bank0_d6_val = 0x48;
49 break; 115 break;
50 case SYS_DVBT2: 116 case SYS_DVBT2:
51 delivery_system_val = 0x03; 117 delivery_system_val = 0x03;
118 reg_bank0_b4_val = 0xf6;
119 reg_bank0_cd_val = 0x01;
120 reg_bank0_d4_val = 0x09;
121 reg_bank0_d6_val = 0x46;
52 break; 122 break;
53 case SYS_DVBC_ANNEX_A: 123 case SYS_DVBC_ANNEX_A:
54 delivery_system_val = 0x04; 124 delivery_system_val = 0x04;
125 reg_bank0_b4_val = 0x00;
126 reg_bank0_cd_val = 0x17;
127 reg_bank0_d4_val = 0x09;
128 reg_bank0_d6_val = 0x48;
55 break; 129 break;
56 default: 130 default:
57 ret = -EINVAL; 131 ret = -EINVAL;
58 goto err; 132 goto err;
59 } 133 }
60 134
61 if (c->bandwidth_hz <= 5000000) { 135 switch (c->delivery_system) {
62 memcpy(bw_val, "\xe5\x99\x9a\x1b\xa9\x1b\xa9", 7); 136 case SYS_DVBT:
63 bw_val2 = 0x03; 137 case SYS_DVBT2:
64 } else if (c->bandwidth_hz <= 6000000) { 138 switch (c->bandwidth_hz) {
65 /* IF 3570000 Hz, BW 6000000 Hz */ 139 case 5000000:
66 memcpy(bw_val, "\xbf\x55\x55\x15\x6b\x15\x6b", 7); 140 bandwidth_vals_ptr = "\xe5\x99\x9a\x1b\xa9\x1b\xa9";
67 bw_val2 = 0x02; 141 bandwidth_val = 0x03;
68 } else if (c->bandwidth_hz <= 7000000) { 142 break;
69 /* IF 4570000 Hz, BW 7000000 Hz */ 143 case 6000000:
70 memcpy(bw_val, "\xa4\x00\x00\x0f\x2c\x0f\x2c", 7); 144 bandwidth_vals_ptr = "\xbf\x55\x55\x15\x6b\x15\x6b";
71 bw_val2 = 0x01; 145 bandwidth_val = 0x02;
72 } else if (c->bandwidth_hz <= 8000000) { 146 break;
73 /* IF 4570000 Hz, BW 8000000 Hz */ 147 case 7000000:
74 memcpy(bw_val, "\x8f\x80\x00\x08\xee\x08\xee", 7); 148 bandwidth_vals_ptr = "\xa4\x00\x00\x0f\x2c\x0f\x2c";
75 bw_val2 = 0x00; 149 bandwidth_val = 0x01;
76 } else { 150 break;
77 ret = -EINVAL; 151 case 8000000:
78 goto err; 152 bandwidth_vals_ptr = "\x8f\x80\x00\x08\xee\x08\xee";
153 bandwidth_val = 0x00;
154 break;
155 default:
156 ret = -EINVAL;
157 goto err;
158 }
159 break;
160 case SYS_DVBC_ANNEX_A:
161 bandwidth_vals_ptr = NULL;
162 bandwidth_val = 0x00;
163 break;
164 default:
165 break;
79 } 166 }
80 167
81 /* program tuner */ 168 /* Program tuner */
82 if (fe->ops.tuner_ops.set_params) { 169 if (fe->ops.tuner_ops.set_params) {
83 ret = fe->ops.tuner_ops.set_params(fe); 170 ret = fe->ops.tuner_ops.set_params(fe);
84 if (ret) 171 if (ret)
@@ -91,20 +178,10 @@ static int mn88472_set_frontend(struct dvb_frontend *fe)
91 goto err; 178 goto err;
92 179
93 dev_dbg(&client->dev, "get_if_frequency=%d\n", if_frequency); 180 dev_dbg(&client->dev, "get_if_frequency=%d\n", if_frequency);
94 } 181 } else {
95 182 ret = -EINVAL;
96 /* Calculate IF registers ( (1<<24)*IF / Xtal ) */
97 tmp = div_u64(if_frequency * (u64)(1<<24) + (dev->xtal / 2),
98 dev->xtal);
99 if_val[0] = (tmp >> 16) & 0xff;
100 if_val[1] = (tmp >> 8) & 0xff;
101 if_val[2] = (tmp >> 0) & 0xff;
102
103 ret = regmap_write(dev->regmap[2], 0xfb, 0x13);
104 ret = regmap_write(dev->regmap[2], 0xef, 0x13);
105 ret = regmap_write(dev->regmap[2], 0xf9, 0x13);
106 if (ret)
107 goto err; 183 goto err;
184 }
108 185
109 ret = regmap_write(dev->regmap[2], 0x00, 0x66); 186 ret = regmap_write(dev->regmap[2], 0x00, 0x66);
110 if (ret) 187 if (ret)
@@ -118,157 +195,81 @@ static int mn88472_set_frontend(struct dvb_frontend *fe)
118 ret = regmap_write(dev->regmap[2], 0x03, delivery_system_val); 195 ret = regmap_write(dev->regmap[2], 0x03, delivery_system_val);
119 if (ret) 196 if (ret)
120 goto err; 197 goto err;
121 ret = regmap_write(dev->regmap[2], 0x04, bw_val2); 198 ret = regmap_write(dev->regmap[2], 0x04, bandwidth_val);
122 if (ret) 199 if (ret)
123 goto err; 200 goto err;
124 201
125 for (i = 0; i < sizeof(if_val); i++) { 202 /* IF */
126 ret = regmap_write(dev->regmap[2], 0x10 + i, if_val[i]); 203 utmp = DIV_ROUND_CLOSEST_ULL((u64)if_frequency * 0x1000000, dev->clk);
204 buf[0] = (utmp >> 16) & 0xff;
205 buf[1] = (utmp >> 8) & 0xff;
206 buf[2] = (utmp >> 0) & 0xff;
207 for (i = 0; i < 3; i++) {
208 ret = regmap_write(dev->regmap[2], 0x10 + i, buf[i]);
127 if (ret) 209 if (ret)
128 goto err; 210 goto err;
129 } 211 }
130 212
131 for (i = 0; i < sizeof(bw_val); i++) { 213 /* Bandwidth */
132 ret = regmap_write(dev->regmap[2], 0x13 + i, bw_val[i]); 214 if (bandwidth_vals_ptr) {
133 if (ret) 215 for (i = 0; i < 7; i++) {
134 goto err; 216 ret = regmap_write(dev->regmap[2], 0x13 + i,
217 bandwidth_vals_ptr[i]);
218 if (ret)
219 goto err;
220 }
135 } 221 }
136 222
223 ret = regmap_write(dev->regmap[0], 0xb4, reg_bank0_b4_val);
224 if (ret)
225 goto err;
226 ret = regmap_write(dev->regmap[0], 0xcd, reg_bank0_cd_val);
227 if (ret)
228 goto err;
229 ret = regmap_write(dev->regmap[0], 0xd4, reg_bank0_d4_val);
230 if (ret)
231 goto err;
232 ret = regmap_write(dev->regmap[0], 0xd6, reg_bank0_d6_val);
233 if (ret)
234 goto err;
235
137 switch (c->delivery_system) { 236 switch (c->delivery_system) {
138 case SYS_DVBT: 237 case SYS_DVBT:
139 ret = regmap_write(dev->regmap[0], 0x07, 0x26); 238 ret = regmap_write(dev->regmap[0], 0x07, 0x26);
140 ret = regmap_write(dev->regmap[0], 0xb0, 0x0a); 239 if (ret)
141 ret = regmap_write(dev->regmap[0], 0xb4, 0x00); 240 goto err;
142 ret = regmap_write(dev->regmap[0], 0xcd, 0x1f);
143 ret = regmap_write(dev->regmap[0], 0xd4, 0x0a);
144 ret = regmap_write(dev->regmap[0], 0xd6, 0x48);
145 ret = regmap_write(dev->regmap[0], 0x00, 0xba); 241 ret = regmap_write(dev->regmap[0], 0x00, 0xba);
242 if (ret)
243 goto err;
146 ret = regmap_write(dev->regmap[0], 0x01, 0x13); 244 ret = regmap_write(dev->regmap[0], 0x01, 0x13);
147 if (ret) 245 if (ret)
148 goto err; 246 goto err;
149 break; 247 break;
150 case SYS_DVBT2: 248 case SYS_DVBT2:
151 ret = regmap_write(dev->regmap[2], 0x2b, 0x13); 249 ret = regmap_write(dev->regmap[2], 0x2b, 0x13);
250 if (ret)
251 goto err;
152 ret = regmap_write(dev->regmap[2], 0x4f, 0x05); 252 ret = regmap_write(dev->regmap[2], 0x4f, 0x05);
253 if (ret)
254 goto err;
153 ret = regmap_write(dev->regmap[1], 0xf6, 0x05); 255 ret = regmap_write(dev->regmap[1], 0xf6, 0x05);
154 ret = regmap_write(dev->regmap[0], 0xb0, 0x0a);
155 ret = regmap_write(dev->regmap[0], 0xb4, 0xf6);
156 ret = regmap_write(dev->regmap[0], 0xcd, 0x01);
157 ret = regmap_write(dev->regmap[0], 0xd4, 0x09);
158 ret = regmap_write(dev->regmap[0], 0xd6, 0x46);
159 ret = regmap_write(dev->regmap[2], 0x30, 0x80);
160 ret = regmap_write(dev->regmap[2], 0x32, 0x00);
161 if (ret) 256 if (ret)
162 goto err; 257 goto err;
163 break; 258 ret = regmap_write(dev->regmap[2], 0x32, c->stream_id);
164 case SYS_DVBC_ANNEX_A:
165 ret = regmap_write(dev->regmap[0], 0xb0, 0x0b);
166 ret = regmap_write(dev->regmap[0], 0xb4, 0x00);
167 ret = regmap_write(dev->regmap[0], 0xcd, 0x17);
168 ret = regmap_write(dev->regmap[0], 0xd4, 0x09);
169 ret = regmap_write(dev->regmap[0], 0xd6, 0x48);
170 ret = regmap_write(dev->regmap[1], 0x00, 0xb0);
171 if (ret) 259 if (ret)
172 goto err; 260 goto err;
173 break; 261 break;
174 default: 262 case SYS_DVBC_ANNEX_A:
175 ret = -EINVAL;
176 goto err;
177 }
178
179 ret = regmap_write(dev->regmap[0], 0x46, 0x00);
180 ret = regmap_write(dev->regmap[0], 0xae, 0x00);
181
182 switch (dev->ts_mode) {
183 case SERIAL_TS_MODE:
184 ret = regmap_write(dev->regmap[2], 0x08, 0x1d);
185 break;
186 case PARALLEL_TS_MODE:
187 ret = regmap_write(dev->regmap[2], 0x08, 0x00);
188 break; 263 break;
189 default: 264 default:
190 dev_dbg(&client->dev, "ts_mode error: %d\n", dev->ts_mode);
191 ret = -EINVAL;
192 goto err;
193 }
194
195 switch (dev->ts_clock) {
196 case VARIABLE_TS_CLOCK:
197 ret = regmap_write(dev->regmap[0], 0xd9, 0xe3);
198 break; 265 break;
199 case FIXED_TS_CLOCK:
200 ret = regmap_write(dev->regmap[0], 0xd9, 0xe1);
201 break;
202 default:
203 dev_dbg(&client->dev, "ts_clock error: %d\n", dev->ts_clock);
204 ret = -EINVAL;
205 goto err;
206 } 266 }
207 267
208 /* Reset demod */ 268 /* Reset FSM */
209 ret = regmap_write(dev->regmap[2], 0xf8, 0x9f); 269 ret = regmap_write(dev->regmap[2], 0xf8, 0x9f);
210 if (ret) 270 if (ret)
211 goto err; 271 goto err;
212 272
213 dev->delivery_system = c->delivery_system;
214
215 return 0;
216err:
217 dev_dbg(&client->dev, "failed=%d\n", ret);
218 return ret;
219}
220
221static int mn88472_read_status(struct dvb_frontend *fe, enum fe_status *status)
222{
223 struct i2c_client *client = fe->demodulator_priv;
224 struct mn88472_dev *dev = i2c_get_clientdata(client);
225 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
226 int ret;
227 unsigned int utmp;
228 int lock = 0;
229
230 *status = 0;
231
232 if (!dev->warm) {
233 ret = -EAGAIN;
234 goto err;
235 }
236
237 switch (c->delivery_system) {
238 case SYS_DVBT:
239 ret = regmap_read(dev->regmap[0], 0x7F, &utmp);
240 if (ret)
241 goto err;
242 if ((utmp & 0xF) >= 0x09)
243 lock = 1;
244 break;
245 case SYS_DVBT2:
246 ret = regmap_read(dev->regmap[2], 0x92, &utmp);
247 if (ret)
248 goto err;
249 if ((utmp & 0xF) >= 0x07)
250 *status |= FE_HAS_SIGNAL;
251 if ((utmp & 0xF) >= 0x0a)
252 *status |= FE_HAS_CARRIER;
253 if ((utmp & 0xF) >= 0x0d)
254 *status |= FE_HAS_VITERBI | FE_HAS_SYNC | FE_HAS_LOCK;
255 break;
256 case SYS_DVBC_ANNEX_A:
257 ret = regmap_read(dev->regmap[1], 0x84, &utmp);
258 if (ret)
259 goto err;
260 if ((utmp & 0xF) >= 0x08)
261 lock = 1;
262 break;
263 default:
264 ret = -EINVAL;
265 goto err;
266 }
267
268 if (lock)
269 *status = FE_HAS_SIGNAL | FE_HAS_CARRIER | FE_HAS_VITERBI |
270 FE_HAS_SYNC | FE_HAS_LOCK;
271
272 return 0; 273 return 0;
273err: 274err:
274 dev_dbg(&client->dev, "failed=%d\n", ret); 275 dev_dbg(&client->dev, "failed=%d\n", ret);
@@ -279,93 +280,107 @@ static int mn88472_init(struct dvb_frontend *fe)
279{ 280{
280 struct i2c_client *client = fe->demodulator_priv; 281 struct i2c_client *client = fe->demodulator_priv;
281 struct mn88472_dev *dev = i2c_get_clientdata(client); 282 struct mn88472_dev *dev = i2c_get_clientdata(client);
282 int ret, len, remaining; 283 int ret, len, rem;
283 const struct firmware *fw = NULL; 284 unsigned int utmp;
284 u8 *fw_file = MN88472_FIRMWARE; 285 const struct firmware *firmware;
285 unsigned int tmp; 286 const char *name = MN88472_FIRMWARE;
286 287
287 dev_dbg(&client->dev, "\n"); 288 dev_dbg(&client->dev, "\n");
288 289
289 /* set cold state by default */ 290 /* Power up */
290 dev->warm = false;
291
292 /* power on */
293 ret = regmap_write(dev->regmap[2], 0x05, 0x00); 291 ret = regmap_write(dev->regmap[2], 0x05, 0x00);
294 if (ret) 292 if (ret)
295 goto err; 293 goto err;
296 294 ret = regmap_write(dev->regmap[2], 0x0b, 0x00);
297 ret = regmap_bulk_write(dev->regmap[2], 0x0b, "\x00\x00", 2);
298 if (ret) 295 if (ret)
299 goto err; 296 goto err;
300 297 ret = regmap_write(dev->regmap[2], 0x0c, 0x00);
301 /* check if firmware is already running */
302 ret = regmap_read(dev->regmap[0], 0xf5, &tmp);
303 if (ret) 298 if (ret)
304 goto err; 299 goto err;
305 300
306 if (!(tmp & 0x1)) { 301 /* Check if firmware is already running */
307 dev_info(&client->dev, "firmware already running\n"); 302 ret = regmap_read(dev->regmap[0], 0xf5, &utmp);
308 dev->warm = true; 303 if (ret)
309 return 0; 304 goto err;
310 } 305 if (!(utmp & 0x01))
306 goto warm;
311 307
312 /* request the firmware, this will block and timeout */ 308 ret = request_firmware(&firmware, name, &client->dev);
313 ret = request_firmware(&fw, fw_file, &client->dev);
314 if (ret) { 309 if (ret) {
315 dev_err(&client->dev, "firmare file '%s' not found\n", 310 dev_err(&client->dev, "firmware file '%s' not found\n", name);
316 fw_file);
317 goto err; 311 goto err;
318 } 312 }
319 313
320 dev_info(&client->dev, "downloading firmware from file '%s'\n", 314 dev_info(&client->dev, "downloading firmware from file '%s'\n", name);
321 fw_file);
322 315
323 ret = regmap_write(dev->regmap[0], 0xf5, 0x03); 316 ret = regmap_write(dev->regmap[0], 0xf5, 0x03);
324 if (ret) 317 if (ret)
325 goto firmware_release; 318 goto err_release_firmware;
326
327 for (remaining = fw->size; remaining > 0;
328 remaining -= (dev->i2c_wr_max - 1)) {
329 len = remaining;
330 if (len > (dev->i2c_wr_max - 1))
331 len = dev->i2c_wr_max - 1;
332 319
320 for (rem = firmware->size; rem > 0; rem -= (dev->i2c_write_max - 1)) {
321 len = min(dev->i2c_write_max - 1, rem);
333 ret = regmap_bulk_write(dev->regmap[0], 0xf6, 322 ret = regmap_bulk_write(dev->regmap[0], 0xf6,
334 &fw->data[fw->size - remaining], len); 323 &firmware->data[firmware->size - rem],
324 len);
335 if (ret) { 325 if (ret) {
336 dev_err(&client->dev, 326 dev_err(&client->dev, "firmware download failed %d\n",
337 "firmware download failed=%d\n", ret); 327 ret);
338 goto firmware_release; 328 goto err_release_firmware;
339 } 329 }
340 } 330 }
341 331
342 /* parity check of firmware */ 332 /* Parity check of firmware */
343 ret = regmap_read(dev->regmap[0], 0xf8, &tmp); 333 ret = regmap_read(dev->regmap[0], 0xf8, &utmp);
344 if (ret) { 334 if (ret)
345 dev_err(&client->dev, 335 goto err_release_firmware;
346 "parity reg read failed=%d\n", ret); 336 if (utmp & 0x10) {
347 goto firmware_release; 337 ret = -EINVAL;
348 } 338 dev_err(&client->dev, "firmware did not run\n");
349 if (tmp & 0x10) { 339 goto err_release_firmware;
350 dev_err(&client->dev,
351 "firmware parity check failed=0x%x\n", tmp);
352 goto firmware_release;
353 } 340 }
354 dev_err(&client->dev, "firmware parity check succeeded=0x%x\n", tmp);
355 341
356 ret = regmap_write(dev->regmap[0], 0xf5, 0x00); 342 ret = regmap_write(dev->regmap[0], 0xf5, 0x00);
357 if (ret) 343 if (ret)
358 goto firmware_release; 344 goto err_release_firmware;
345
346 release_firmware(firmware);
347warm:
348 /* TS config */
349 switch (dev->ts_mode) {
350 case SERIAL_TS_MODE:
351 utmp = 0x1d;
352 break;
353 case PARALLEL_TS_MODE:
354 utmp = 0x00;
355 break;
356 default:
357 ret = -EINVAL;
358 goto err;
359 }
360 ret = regmap_write(dev->regmap[2], 0x08, utmp);
361 if (ret)
362 goto err;
359 363
360 release_firmware(fw); 364 switch (dev->ts_clk) {
361 fw = NULL; 365 case VARIABLE_TS_CLOCK:
366 utmp = 0xe3;
367 break;
368 case FIXED_TS_CLOCK:
369 utmp = 0xe1;
370 break;
371 default:
372 ret = -EINVAL;
373 goto err;
374 }
375 ret = regmap_write(dev->regmap[0], 0xd9, utmp);
376 if (ret)
377 goto err;
362 378
363 /* warm state */ 379 dev->active = true;
364 dev->warm = true;
365 380
366 return 0; 381 return 0;
367firmware_release: 382err_release_firmware:
368 release_firmware(fw); 383 release_firmware(firmware);
369err: 384err:
370 dev_dbg(&client->dev, "failed=%d\n", ret); 385 dev_dbg(&client->dev, "failed=%d\n", ret);
371 return ret; 386 return ret;
@@ -379,18 +394,17 @@ static int mn88472_sleep(struct dvb_frontend *fe)
379 394
380 dev_dbg(&client->dev, "\n"); 395 dev_dbg(&client->dev, "\n");
381 396
382 /* power off */ 397 /* Power down */
398 ret = regmap_write(dev->regmap[2], 0x0c, 0x30);
399 if (ret)
400 goto err;
383 ret = regmap_write(dev->regmap[2], 0x0b, 0x30); 401 ret = regmap_write(dev->regmap[2], 0x0b, 0x30);
384
385 if (ret) 402 if (ret)
386 goto err; 403 goto err;
387
388 ret = regmap_write(dev->regmap[2], 0x05, 0x3e); 404 ret = regmap_write(dev->regmap[2], 0x05, 0x3e);
389 if (ret) 405 if (ret)
390 goto err; 406 goto err;
391 407
392 dev->delivery_system = SYS_UNDEFINED;
393
394 return 0; 408 return 0;
395err: 409err:
396 dev_dbg(&client->dev, "failed=%d\n", ret); 410 dev_dbg(&client->dev, "failed=%d\n", ret);
@@ -434,10 +448,19 @@ static struct dvb_frontend_ops mn88472_ops = {
434 .read_status = mn88472_read_status, 448 .read_status = mn88472_read_status,
435}; 449};
436 450
451static struct dvb_frontend *mn88472_get_dvb_frontend(struct i2c_client *client)
452{
453 struct mn88472_dev *dev = i2c_get_clientdata(client);
454
455 dev_dbg(&client->dev, "\n");
456
457 return &dev->fe;
458}
459
437static int mn88472_probe(struct i2c_client *client, 460static int mn88472_probe(struct i2c_client *client,
438 const struct i2c_device_id *id) 461 const struct i2c_device_id *id)
439{ 462{
440 struct mn88472_config *config = client->dev.platform_data; 463 struct mn88472_config *pdata = client->dev.platform_data;
441 struct mn88472_dev *dev; 464 struct mn88472_dev *dev;
442 int ret; 465 int ret;
443 unsigned int utmp; 466 unsigned int utmp;
@@ -448,23 +471,16 @@ static int mn88472_probe(struct i2c_client *client,
448 471
449 dev_dbg(&client->dev, "\n"); 472 dev_dbg(&client->dev, "\n");
450 473
451 /* Caller really need to provide pointer for frontend we create. */
452 if (config->fe == NULL) {
453 dev_err(&client->dev, "frontend pointer not defined\n");
454 ret = -EINVAL;
455 goto err;
456 }
457
458 dev = kzalloc(sizeof(*dev), GFP_KERNEL); 474 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
459 if (!dev) { 475 if (!dev) {
460 ret = -ENOMEM; 476 ret = -ENOMEM;
461 goto err; 477 goto err;
462 } 478 }
463 479
464 dev->i2c_wr_max = config->i2c_wr_max; 480 dev->i2c_write_max = pdata->i2c_wr_max ? pdata->i2c_wr_max : ~0;
465 dev->xtal = config->xtal; 481 dev->clk = pdata->xtal;
466 dev->ts_mode = config->ts_mode; 482 dev->ts_mode = pdata->ts_mode;
467 dev->ts_clock = config->ts_clock; 483 dev->ts_clk = pdata->ts_clock;
468 dev->client[0] = client; 484 dev->client[0] = client;
469 dev->regmap[0] = regmap_init_i2c(dev->client[0], &regmap_config); 485 dev->regmap[0] = regmap_init_i2c(dev->client[0], &regmap_config);
470 if (IS_ERR(dev->regmap[0])) { 486 if (IS_ERR(dev->regmap[0])) {
@@ -472,15 +488,25 @@ static int mn88472_probe(struct i2c_client *client,
472 goto err_kfree; 488 goto err_kfree;
473 } 489 }
474 490
475 /* check demod answers to I2C */ 491 /* Check demod answers with correct chip id */
476 ret = regmap_read(dev->regmap[0], 0x00, &utmp); 492 ret = regmap_read(dev->regmap[0], 0xff, &utmp);
477 if (ret) 493 if (ret)
478 goto err_regmap_0_regmap_exit; 494 goto err_regmap_0_regmap_exit;
479 495
496 dev_dbg(&client->dev, "chip id=%02x\n", utmp);
497
498 if (utmp != 0x02) {
499 ret = -ENODEV;
500 goto err_regmap_0_regmap_exit;
501 }
502
480 /* 503 /*
481 * Chip has three I2C addresses for different register pages. Used 504 * Chip has three I2C addresses for different register banks. Used
482 * addresses are 0x18, 0x1a and 0x1c. We register two dummy clients, 505 * addresses are 0x18, 0x1a and 0x1c. We register two dummy clients,
483 * 0x1a and 0x1c, in order to get own I2C client for each register page. 506 * 0x1a and 0x1c, in order to get own I2C client for each register bank.
507 *
508 * Also, register bank 2 do not support sequential I/O. Only single
509 * register write or read is allowed to that bank.
484 */ 510 */
485 dev->client[1] = i2c_new_dummy(client->adapter, 0x1a); 511 dev->client[1] = i2c_new_dummy(client->adapter, 0x1a);
486 if (!dev->client[1]) { 512 if (!dev->client[1]) {
@@ -510,15 +536,25 @@ static int mn88472_probe(struct i2c_client *client,
510 } 536 }
511 i2c_set_clientdata(dev->client[2], dev); 537 i2c_set_clientdata(dev->client[2], dev);
512 538
513 /* create dvb_frontend */ 539 /* Sleep because chip is active by default */
540 ret = regmap_write(dev->regmap[2], 0x05, 0x3e);
541 if (ret)
542 goto err_regmap_2_regmap_exit;
543
544 /* Create dvb frontend */
514 memcpy(&dev->fe.ops, &mn88472_ops, sizeof(struct dvb_frontend_ops)); 545 memcpy(&dev->fe.ops, &mn88472_ops, sizeof(struct dvb_frontend_ops));
515 dev->fe.demodulator_priv = client; 546 dev->fe.demodulator_priv = client;
516 *config->fe = &dev->fe; 547 *pdata->fe = &dev->fe;
517 i2c_set_clientdata(client, dev); 548 i2c_set_clientdata(client, dev);
518 549
519 dev_info(&client->dev, "Panasonic MN88472 successfully attached\n"); 550 /* Setup callbacks */
520 return 0; 551 pdata->get_dvb_frontend = mn88472_get_dvb_frontend;
521 552
553 dev_info(&client->dev, "Panasonic MN88472 successfully identified\n");
554
555 return 0;
556err_regmap_2_regmap_exit:
557 regmap_exit(dev->regmap[2]);
522err_client_2_i2c_unregister_device: 558err_client_2_i2c_unregister_device:
523 i2c_unregister_device(dev->client[2]); 559 i2c_unregister_device(dev->client[2]);
524err_regmap_1_regmap_exit: 560err_regmap_1_regmap_exit:
@@ -561,11 +597,12 @@ MODULE_DEVICE_TABLE(i2c, mn88472_id_table);
561 597
562static struct i2c_driver mn88472_driver = { 598static struct i2c_driver mn88472_driver = {
563 .driver = { 599 .driver = {
564 .name = "mn88472", 600 .name = "mn88472",
601 .suppress_bind_attrs = true,
565 }, 602 },
566 .probe = mn88472_probe, 603 .probe = mn88472_probe,
567 .remove = mn88472_remove, 604 .remove = mn88472_remove,
568 .id_table = mn88472_id_table, 605 .id_table = mn88472_id_table,
569}; 606};
570 607
571module_i2c_driver(mn88472_driver); 608module_i2c_driver(mn88472_driver);
diff --git a/drivers/media/dvb-frontends/mn88472.h b/drivers/media/dvb-frontends/mn88472.h
index 095294d292f3..323632523876 100644
--- a/drivers/media/dvb-frontends/mn88472.h
+++ b/drivers/media/dvb-frontends/mn88472.h
@@ -19,23 +19,33 @@
19 19
20#include <linux/dvb/frontend.h> 20#include <linux/dvb/frontend.h>
21 21
22enum ts_clock { 22/**
23 VARIABLE_TS_CLOCK, 23 * struct mn88472_config - Platform data for the mn88472 driver
24 FIXED_TS_CLOCK, 24 * @xtal: Clock frequency.
25}; 25 * @ts_mode: TS mode.
26 * @ts_clock: TS clock config.
27 * @i2c_wr_max: Max number of bytes driver writes to I2C at once.
28 * @get_dvb_frontend: Get DVB frontend.
29 */
26 30
27enum ts_mode { 31/* Define old names for backward compatibility */
28 SERIAL_TS_MODE, 32#define VARIABLE_TS_CLOCK MN88472_TS_CLK_VARIABLE
29 PARALLEL_TS_MODE, 33#define FIXED_TS_CLOCK MN88472_TS_CLK_FIXED
30}; 34#define SERIAL_TS_MODE MN88472_TS_MODE_SERIAL
35#define PARALLEL_TS_MODE MN88472_TS_MODE_PARALLEL
31 36
32struct mn88472_config { 37struct mn88472_config {
33 /* 38 unsigned int xtal;
34 * Max num of bytes given I2C adapter could write at once. 39
35 * Default: none 40#define MN88472_TS_MODE_SERIAL 0
36 */ 41#define MN88472_TS_MODE_PARALLEL 1
37 u16 i2c_wr_max; 42 int ts_mode;
38 43
44#define MN88472_TS_CLK_FIXED 0
45#define MN88472_TS_CLK_VARIABLE 1
46 int ts_clock;
47
48 u16 i2c_wr_max;
39 49
40 /* Everything after that is returned by the driver. */ 50 /* Everything after that is returned by the driver. */
41 51
@@ -43,14 +53,7 @@ struct mn88472_config {
43 * DVB frontend. 53 * DVB frontend.
44 */ 54 */
45 struct dvb_frontend **fe; 55 struct dvb_frontend **fe;
46 56 struct dvb_frontend* (*get_dvb_frontend)(struct i2c_client *);
47 /*
48 * Xtal frequency.
49 * Hz
50 */
51 u32 xtal;
52 int ts_mode;
53 int ts_clock;
54}; 57};
55 58
56#endif 59#endif
diff --git a/drivers/staging/media/mn88472/mn88472_priv.h b/drivers/media/dvb-frontends/mn88472_priv.h
index 1a0de9e46b66..cdf2597a25d1 100644
--- a/drivers/staging/media/mn88472/mn88472_priv.h
+++ b/drivers/media/dvb-frontends/mn88472_priv.h
@@ -28,12 +28,11 @@ struct mn88472_dev {
28 struct i2c_client *client[3]; 28 struct i2c_client *client[3];
29 struct regmap *regmap[3]; 29 struct regmap *regmap[3];
30 struct dvb_frontend fe; 30 struct dvb_frontend fe;
31 u16 i2c_wr_max; 31 u16 i2c_write_max;
32 enum fe_delivery_system delivery_system; 32 unsigned int clk;
33 bool warm; /* FW running */ 33 unsigned int active:1;
34 u32 xtal; 34 unsigned int ts_mode:1;
35 int ts_mode; 35 unsigned int ts_clk:1;
36 int ts_clock;
37}; 36};
38 37
39#endif 38#endif
diff --git a/drivers/media/dvb-frontends/mn88473.c b/drivers/media/dvb-frontends/mn88473.c
index 6c5d592161d4..451974a1d7ed 100644
--- a/drivers/media/dvb-frontends/mn88473.c
+++ b/drivers/media/dvb-frontends/mn88473.c
@@ -330,7 +330,7 @@ static int mn88473_init(struct dvb_frontend *fe)
330 /* Request the firmware, this will block and timeout */ 330 /* Request the firmware, this will block and timeout */
331 ret = request_firmware(&fw, name, &client->dev); 331 ret = request_firmware(&fw, name, &client->dev);
332 if (ret) { 332 if (ret) {
333 dev_err(&client->dev, "firmare file '%s' not found\n", name); 333 dev_err(&client->dev, "firmware file '%s' not found\n", name);
334 goto err; 334 goto err;
335 } 335 }
336 336
@@ -536,7 +536,7 @@ static int mn88473_probe(struct i2c_client *client,
536 /* Sleep because chip is active by default */ 536 /* Sleep because chip is active by default */
537 ret = regmap_write(dev->regmap[2], 0x05, 0x3e); 537 ret = regmap_write(dev->regmap[2], 0x05, 0x3e);
538 if (ret) 538 if (ret)
539 goto err_client_2_i2c_unregister_device; 539 goto err_regmap_2_regmap_exit;
540 540
541 /* Create dvb frontend */ 541 /* Create dvb frontend */
542 memcpy(&dev->frontend.ops, &mn88473_ops, sizeof(dev->frontend.ops)); 542 memcpy(&dev->frontend.ops, &mn88473_ops, sizeof(dev->frontend.ops));
@@ -547,7 +547,8 @@ static int mn88473_probe(struct i2c_client *client,
547 dev_info(&client->dev, "Panasonic MN88473 successfully identified\n"); 547 dev_info(&client->dev, "Panasonic MN88473 successfully identified\n");
548 548
549 return 0; 549 return 0;
550 550err_regmap_2_regmap_exit:
551 regmap_exit(dev->regmap[2]);
551err_client_2_i2c_unregister_device: 552err_client_2_i2c_unregister_device:
552 i2c_unregister_device(dev->client[2]); 553 i2c_unregister_device(dev->client[2]);
553err_regmap_1_regmap_exit: 554err_regmap_1_regmap_exit:
diff --git a/drivers/media/dvb-frontends/rtl2830.c b/drivers/media/dvb-frontends/rtl2830.c
index d25d1e0cd4ca..87226056f226 100644
--- a/drivers/media/dvb-frontends/rtl2830.c
+++ b/drivers/media/dvb-frontends/rtl2830.c
@@ -135,8 +135,6 @@ static int rtl2830_init(struct dvb_frontend *fe)
135 c->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE; 135 c->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
136 c->post_bit_count.len = 1; 136 c->post_bit_count.len = 1;
137 c->post_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE; 137 c->post_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
138 /* start statistics polling */
139 schedule_delayed_work(&dev->stat_work, msecs_to_jiffies(2000));
140 138
141 dev->sleeping = false; 139 dev->sleeping = false;
142 140
@@ -152,8 +150,6 @@ static int rtl2830_sleep(struct dvb_frontend *fe)
152 struct rtl2830_dev *dev = i2c_get_clientdata(client); 150 struct rtl2830_dev *dev = i2c_get_clientdata(client);
153 151
154 dev->sleeping = true; 152 dev->sleeping = true;
155 /* stop statistics polling */
156 cancel_delayed_work_sync(&dev->stat_work);
157 dev->fe_status = 0; 153 dev->fe_status = 0;
158 154
159 return 0; 155 return 0;
@@ -396,8 +392,10 @@ static int rtl2830_read_status(struct dvb_frontend *fe, enum fe_status *status)
396{ 392{
397 struct i2c_client *client = fe->demodulator_priv; 393 struct i2c_client *client = fe->demodulator_priv;
398 struct rtl2830_dev *dev = i2c_get_clientdata(client); 394 struct rtl2830_dev *dev = i2c_get_clientdata(client);
399 int ret; 395 struct dtv_frontend_properties *c = &dev->fe.dtv_property_cache;
400 u8 u8tmp; 396 int ret, stmp;
397 unsigned int utmp;
398 u8 u8tmp, buf[2];
401 399
402 *status = 0; 400 *status = 0;
403 401
@@ -419,6 +417,89 @@ static int rtl2830_read_status(struct dvb_frontend *fe, enum fe_status *status)
419 417
420 dev->fe_status = *status; 418 dev->fe_status = *status;
421 419
420 /* Signal strength */
421 if (dev->fe_status & FE_HAS_SIGNAL) {
422 /* Read IF AGC */
423 ret = rtl2830_bulk_read(client, 0x359, buf, 2);
424 if (ret)
425 goto err;
426
427 stmp = buf[0] << 8 | buf[1] << 0;
428 stmp = sign_extend32(stmp, 13);
429 utmp = clamp_val(-4 * stmp + 32767, 0x0000, 0xffff);
430
431 dev_dbg(&client->dev, "IF AGC=%d\n", stmp);
432
433 c->strength.stat[0].scale = FE_SCALE_RELATIVE;
434 c->strength.stat[0].uvalue = utmp;
435 } else {
436 c->strength.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
437 }
438
439 /* CNR */
440 if (dev->fe_status & FE_HAS_VITERBI) {
441 unsigned int hierarchy, constellation;
442 #define CONSTELLATION_NUM 3
443 #define HIERARCHY_NUM 4
444 static const u32 constant[CONSTELLATION_NUM][HIERARCHY_NUM] = {
445 {70705899, 70705899, 70705899, 70705899},
446 {82433173, 82433173, 87483115, 94445660},
447 {92888734, 92888734, 95487525, 99770748},
448 };
449
450 ret = rtl2830_bulk_read(client, 0x33c, &u8tmp, 1);
451 if (ret)
452 goto err;
453
454 constellation = (u8tmp >> 2) & 0x03; /* [3:2] */
455 if (constellation > CONSTELLATION_NUM - 1)
456 goto err;
457
458 hierarchy = (u8tmp >> 4) & 0x07; /* [6:4] */
459 if (hierarchy > HIERARCHY_NUM - 1)
460 goto err;
461
462 ret = rtl2830_bulk_read(client, 0x40c, buf, 2);
463 if (ret)
464 goto err;
465
466 utmp = buf[0] << 8 | buf[1] << 0;
467 if (utmp)
468 stmp = (constant[constellation][hierarchy] -
469 intlog10(utmp)) / ((1 << 24) / 10000);
470 else
471 stmp = 0;
472
473 dev_dbg(&client->dev, "CNR raw=%u\n", utmp);
474
475 c->cnr.stat[0].scale = FE_SCALE_DECIBEL;
476 c->cnr.stat[0].svalue = stmp;
477 } else {
478 c->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
479 }
480
481 /* BER */
482 if (dev->fe_status & FE_HAS_LOCK) {
483 ret = rtl2830_bulk_read(client, 0x34e, buf, 2);
484 if (ret)
485 goto err;
486
487 utmp = buf[0] << 8 | buf[1] << 0;
488 dev->post_bit_error += utmp;
489 dev->post_bit_count += 1000000;
490
491 dev_dbg(&client->dev, "BER errors=%u total=1000000\n", utmp);
492
493 c->post_bit_error.stat[0].scale = FE_SCALE_COUNTER;
494 c->post_bit_error.stat[0].uvalue = dev->post_bit_error;
495 c->post_bit_count.stat[0].scale = FE_SCALE_COUNTER;
496 c->post_bit_count.stat[0].uvalue = dev->post_bit_count;
497 } else {
498 c->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
499 c->post_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
500 }
501
502
422 return ret; 503 return ret;
423err: 504err:
424 dev_dbg(&client->dev, "failed=%d\n", ret); 505 dev_dbg(&client->dev, "failed=%d\n", ret);
@@ -503,109 +584,6 @@ static struct dvb_frontend_ops rtl2830_ops = {
503 .read_signal_strength = rtl2830_read_signal_strength, 584 .read_signal_strength = rtl2830_read_signal_strength,
504}; 585};
505 586
506static void rtl2830_stat_work(struct work_struct *work)
507{
508 struct rtl2830_dev *dev = container_of(work, struct rtl2830_dev, stat_work.work);
509 struct i2c_client *client = dev->client;
510 struct dtv_frontend_properties *c = &dev->fe.dtv_property_cache;
511 int ret, tmp;
512 u8 u8tmp, buf[2];
513 u16 u16tmp;
514
515 dev_dbg(&client->dev, "\n");
516
517 /* signal strength */
518 if (dev->fe_status & FE_HAS_SIGNAL) {
519 struct {signed int x:14; } s;
520
521 /* read IF AGC */
522 ret = rtl2830_bulk_read(client, 0x359, buf, 2);
523 if (ret)
524 goto err;
525
526 u16tmp = buf[0] << 8 | buf[1] << 0;
527 u16tmp &= 0x3fff; /* [13:0] */
528 tmp = s.x = u16tmp; /* 14-bit bin to 2 complement */
529 u16tmp = clamp_val(-4 * tmp + 32767, 0x0000, 0xffff);
530
531 dev_dbg(&client->dev, "IF AGC=%d\n", tmp);
532
533 c->strength.stat[0].scale = FE_SCALE_RELATIVE;
534 c->strength.stat[0].uvalue = u16tmp;
535 } else {
536 c->strength.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
537 }
538
539 /* CNR */
540 if (dev->fe_status & FE_HAS_VITERBI) {
541 unsigned hierarchy, constellation;
542 #define CONSTELLATION_NUM 3
543 #define HIERARCHY_NUM 4
544 static const u32 constant[CONSTELLATION_NUM][HIERARCHY_NUM] = {
545 {70705899, 70705899, 70705899, 70705899},
546 {82433173, 82433173, 87483115, 94445660},
547 {92888734, 92888734, 95487525, 99770748},
548 };
549
550 ret = rtl2830_bulk_read(client, 0x33c, &u8tmp, 1);
551 if (ret)
552 goto err;
553
554 constellation = (u8tmp >> 2) & 0x03; /* [3:2] */
555 if (constellation > CONSTELLATION_NUM - 1)
556 goto err_schedule_delayed_work;
557
558 hierarchy = (u8tmp >> 4) & 0x07; /* [6:4] */
559 if (hierarchy > HIERARCHY_NUM - 1)
560 goto err_schedule_delayed_work;
561
562 ret = rtl2830_bulk_read(client, 0x40c, buf, 2);
563 if (ret)
564 goto err;
565
566 u16tmp = buf[0] << 8 | buf[1] << 0;
567 if (u16tmp)
568 tmp = (constant[constellation][hierarchy] -
569 intlog10(u16tmp)) / ((1 << 24) / 10000);
570 else
571 tmp = 0;
572
573 dev_dbg(&client->dev, "CNR raw=%u\n", u16tmp);
574
575 c->cnr.stat[0].scale = FE_SCALE_DECIBEL;
576 c->cnr.stat[0].svalue = tmp;
577 } else {
578 c->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
579 }
580
581 /* BER */
582 if (dev->fe_status & FE_HAS_LOCK) {
583 ret = rtl2830_bulk_read(client, 0x34e, buf, 2);
584 if (ret)
585 goto err;
586
587 u16tmp = buf[0] << 8 | buf[1] << 0;
588 dev->post_bit_error += u16tmp;
589 dev->post_bit_count += 1000000;
590
591 dev_dbg(&client->dev, "BER errors=%u total=1000000\n", u16tmp);
592
593 c->post_bit_error.stat[0].scale = FE_SCALE_COUNTER;
594 c->post_bit_error.stat[0].uvalue = dev->post_bit_error;
595 c->post_bit_count.stat[0].scale = FE_SCALE_COUNTER;
596 c->post_bit_count.stat[0].uvalue = dev->post_bit_count;
597 } else {
598 c->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
599 c->post_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
600 }
601
602err_schedule_delayed_work:
603 schedule_delayed_work(&dev->stat_work, msecs_to_jiffies(2000));
604 return;
605err:
606 dev_dbg(&client->dev, "failed=%d\n", ret);
607}
608
609static int rtl2830_pid_filter_ctrl(struct dvb_frontend *fe, int onoff) 587static int rtl2830_pid_filter_ctrl(struct dvb_frontend *fe, int onoff)
610{ 588{
611 struct i2c_client *client = fe->demodulator_priv; 589 struct i2c_client *client = fe->demodulator_priv;
@@ -851,7 +829,6 @@ static int rtl2830_probe(struct i2c_client *client,
851 dev->client = client; 829 dev->client = client;
852 dev->pdata = client->dev.platform_data; 830 dev->pdata = client->dev.platform_data;
853 dev->sleeping = true; 831 dev->sleeping = true;
854 INIT_DELAYED_WORK(&dev->stat_work, rtl2830_stat_work);
855 dev->regmap = regmap_init(&client->dev, &regmap_bus, client, 832 dev->regmap = regmap_init(&client->dev, &regmap_bus, client,
856 &regmap_config); 833 &regmap_config);
857 if (IS_ERR(dev->regmap)) { 834 if (IS_ERR(dev->regmap)) {
@@ -904,9 +881,6 @@ static int rtl2830_remove(struct i2c_client *client)
904 881
905 dev_dbg(&client->dev, "\n"); 882 dev_dbg(&client->dev, "\n");
906 883
907 /* stop statistics polling */
908 cancel_delayed_work_sync(&dev->stat_work);
909
910 i2c_mux_del_adapters(dev->muxc); 884 i2c_mux_del_adapters(dev->muxc);
911 regmap_exit(dev->regmap); 885 regmap_exit(dev->regmap);
912 kfree(dev); 886 kfree(dev);
@@ -922,7 +896,8 @@ MODULE_DEVICE_TABLE(i2c, rtl2830_id_table);
922 896
923static struct i2c_driver rtl2830_driver = { 897static struct i2c_driver rtl2830_driver = {
924 .driver = { 898 .driver = {
925 .name = "rtl2830", 899 .name = "rtl2830",
900 .suppress_bind_attrs = true,
926 }, 901 },
927 .probe = rtl2830_probe, 902 .probe = rtl2830_probe,
928 .remove = rtl2830_remove, 903 .remove = rtl2830_remove,
diff --git a/drivers/media/dvb-frontends/rtl2830_priv.h b/drivers/media/dvb-frontends/rtl2830_priv.h
index da4909543da2..8ec4721d79ac 100644
--- a/drivers/media/dvb-frontends/rtl2830_priv.h
+++ b/drivers/media/dvb-frontends/rtl2830_priv.h
@@ -24,6 +24,7 @@
24#include <linux/i2c-mux.h> 24#include <linux/i2c-mux.h>
25#include <linux/math64.h> 25#include <linux/math64.h>
26#include <linux/regmap.h> 26#include <linux/regmap.h>
27#include <linux/bitops.h>
27 28
28struct rtl2830_dev { 29struct rtl2830_dev {
29 struct rtl2830_platform_data *pdata; 30 struct rtl2830_platform_data *pdata;
@@ -33,7 +34,6 @@ struct rtl2830_dev {
33 struct dvb_frontend fe; 34 struct dvb_frontend fe;
34 bool sleeping; 35 bool sleeping;
35 unsigned long filters; 36 unsigned long filters;
36 struct delayed_work stat_work;
37 enum fe_status fe_status; 37 enum fe_status fe_status;
38 u64 post_bit_error_prev; /* for old DVBv3 read_ber() calculation */ 38 u64 post_bit_error_prev; /* for old DVBv3 read_ber() calculation */
39 u64 post_bit_error; 39 u64 post_bit_error;
diff --git a/drivers/media/dvb-frontends/rtl2832.c b/drivers/media/dvb-frontends/rtl2832.c
index bfb6beedd40b..0ced01f1012e 100644
--- a/drivers/media/dvb-frontends/rtl2832.c
+++ b/drivers/media/dvb-frontends/rtl2832.c
@@ -947,6 +947,8 @@ static int rtl2832_slave_ts_ctrl(struct i2c_client *client, bool enable)
947 goto err; 947 goto err;
948 } 948 }
949 949
950 dev->slave_ts = enable;
951
950 return 0; 952 return 0;
951err: 953err:
952 dev_dbg(&client->dev, "failed=%d\n", ret); 954 dev_dbg(&client->dev, "failed=%d\n", ret);
@@ -960,7 +962,7 @@ static int rtl2832_pid_filter_ctrl(struct dvb_frontend *fe, int onoff)
960 int ret; 962 int ret;
961 u8 u8tmp; 963 u8 u8tmp;
962 964
963 dev_dbg(&client->dev, "onoff=%d\n", onoff); 965 dev_dbg(&client->dev, "onoff=%d, slave_ts=%d\n", onoff, dev->slave_ts);
964 966
965 /* enable / disable PID filter */ 967 /* enable / disable PID filter */
966 if (onoff) 968 if (onoff)
@@ -968,7 +970,10 @@ static int rtl2832_pid_filter_ctrl(struct dvb_frontend *fe, int onoff)
968 else 970 else
969 u8tmp = 0x00; 971 u8tmp = 0x00;
970 972
971 ret = regmap_update_bits(dev->regmap, 0x061, 0xc0, u8tmp); 973 if (dev->slave_ts)
974 ret = regmap_update_bits(dev->regmap, 0x021, 0xc0, u8tmp);
975 else
976 ret = regmap_update_bits(dev->regmap, 0x061, 0xc0, u8tmp);
972 if (ret) 977 if (ret)
973 goto err; 978 goto err;
974 979
@@ -986,8 +991,8 @@ static int rtl2832_pid_filter(struct dvb_frontend *fe, u8 index, u16 pid,
986 int ret; 991 int ret;
987 u8 buf[4]; 992 u8 buf[4];
988 993
989 dev_dbg(&client->dev, "index=%d pid=%04x onoff=%d\n", 994 dev_dbg(&client->dev, "index=%d pid=%04x onoff=%d slave_ts=%d\n",
990 index, pid, onoff); 995 index, pid, onoff, dev->slave_ts);
991 996
992 /* skip invalid PIDs (0x2000) */ 997 /* skip invalid PIDs (0x2000) */
993 if (pid > 0x1fff || index > 32) 998 if (pid > 0x1fff || index > 32)
@@ -1003,14 +1008,22 @@ static int rtl2832_pid_filter(struct dvb_frontend *fe, u8 index, u16 pid,
1003 buf[1] = (dev->filters >> 8) & 0xff; 1008 buf[1] = (dev->filters >> 8) & 0xff;
1004 buf[2] = (dev->filters >> 16) & 0xff; 1009 buf[2] = (dev->filters >> 16) & 0xff;
1005 buf[3] = (dev->filters >> 24) & 0xff; 1010 buf[3] = (dev->filters >> 24) & 0xff;
1006 ret = regmap_bulk_write(dev->regmap, 0x062, buf, 4); 1011
1012 if (dev->slave_ts)
1013 ret = regmap_bulk_write(dev->regmap, 0x022, buf, 4);
1014 else
1015 ret = regmap_bulk_write(dev->regmap, 0x062, buf, 4);
1007 if (ret) 1016 if (ret)
1008 goto err; 1017 goto err;
1009 1018
1010 /* add PID */ 1019 /* add PID */
1011 buf[0] = (pid >> 8) & 0xff; 1020 buf[0] = (pid >> 8) & 0xff;
1012 buf[1] = (pid >> 0) & 0xff; 1021 buf[1] = (pid >> 0) & 0xff;
1013 ret = regmap_bulk_write(dev->regmap, 0x066 + 2 * index, buf, 2); 1022
1023 if (dev->slave_ts)
1024 ret = regmap_bulk_write(dev->regmap, 0x026 + 2 * index, buf, 2);
1025 else
1026 ret = regmap_bulk_write(dev->regmap, 0x066 + 2 * index, buf, 2);
1014 if (ret) 1027 if (ret)
1015 goto err; 1028 goto err;
1016 1029
@@ -1135,6 +1148,7 @@ MODULE_DEVICE_TABLE(i2c, rtl2832_id_table);
1135static struct i2c_driver rtl2832_driver = { 1148static struct i2c_driver rtl2832_driver = {
1136 .driver = { 1149 .driver = {
1137 .name = "rtl2832", 1150 .name = "rtl2832",
1151 .suppress_bind_attrs = true,
1138 }, 1152 },
1139 .probe = rtl2832_probe, 1153 .probe = rtl2832_probe,
1140 .remove = rtl2832_remove, 1154 .remove = rtl2832_remove,
diff --git a/drivers/media/dvb-frontends/rtl2832_priv.h b/drivers/media/dvb-frontends/rtl2832_priv.h
index c1a8a69e9015..9a6d01a9c690 100644
--- a/drivers/media/dvb-frontends/rtl2832_priv.h
+++ b/drivers/media/dvb-frontends/rtl2832_priv.h
@@ -44,6 +44,7 @@ struct rtl2832_dev {
44 bool sleeping; 44 bool sleeping;
45 struct delayed_work i2c_gate_work; 45 struct delayed_work i2c_gate_work;
46 unsigned long filters; /* PID filter */ 46 unsigned long filters; /* PID filter */
47 bool slave_ts;
47}; 48};
48 49
49struct rtl2832_reg_entry { 50struct rtl2832_reg_entry {
diff --git a/drivers/media/dvb-frontends/rtl2832_sdr.c b/drivers/media/dvb-frontends/rtl2832_sdr.c
index 47a480a7d46c..6e22af36b637 100644
--- a/drivers/media/dvb-frontends/rtl2832_sdr.c
+++ b/drivers/media/dvb-frontends/rtl2832_sdr.c
@@ -452,7 +452,7 @@ static int rtl2832_sdr_querycap(struct file *file, void *fh,
452/* Videobuf2 operations */ 452/* Videobuf2 operations */
453static int rtl2832_sdr_queue_setup(struct vb2_queue *vq, 453static int rtl2832_sdr_queue_setup(struct vb2_queue *vq,
454 unsigned int *nbuffers, 454 unsigned int *nbuffers,
455 unsigned int *nplanes, unsigned int sizes[], void *alloc_ctxs[]) 455 unsigned int *nplanes, unsigned int sizes[], struct device *alloc_devs[])
456{ 456{
457 struct rtl2832_sdr_dev *dev = vb2_get_drv_priv(vq); 457 struct rtl2832_sdr_dev *dev = vb2_get_drv_priv(vq);
458 struct platform_device *pdev = dev->pdev; 458 struct platform_device *pdev = dev->pdev;
diff --git a/drivers/media/dvb-frontends/si2168.c b/drivers/media/dvb-frontends/si2168.c
index 108a069fa1ae..20b4a659e2e4 100644
--- a/drivers/media/dvb-frontends/si2168.c
+++ b/drivers/media/dvb-frontends/si2168.c
@@ -357,9 +357,7 @@ static int si2168_init(struct dvb_frontend *fe)
357 struct si2168_dev *dev = i2c_get_clientdata(client); 357 struct si2168_dev *dev = i2c_get_clientdata(client);
358 int ret, len, remaining; 358 int ret, len, remaining;
359 const struct firmware *fw; 359 const struct firmware *fw;
360 const char *fw_name;
361 struct si2168_cmd cmd; 360 struct si2168_cmd cmd;
362 unsigned int chip_id;
363 361
364 dev_dbg(&client->dev, "\n"); 362 dev_dbg(&client->dev, "\n");
365 363
@@ -371,7 +369,7 @@ static int si2168_init(struct dvb_frontend *fe)
371 if (ret) 369 if (ret)
372 goto err; 370 goto err;
373 371
374 if (dev->fw_loaded) { 372 if (dev->warm) {
375 /* resume */ 373 /* resume */
376 memcpy(cmd.args, "\xc0\x06\x08\x0f\x00\x20\x21\x01", 8); 374 memcpy(cmd.args, "\xc0\x06\x08\x0f\x00\x20\x21\x01", 8);
377 cmd.wlen = 8; 375 cmd.wlen = 8;
@@ -398,49 +396,14 @@ static int si2168_init(struct dvb_frontend *fe)
398 if (ret) 396 if (ret)
399 goto err; 397 goto err;
400 398
401 /* query chip revision */
402 memcpy(cmd.args, "\x02", 1);
403 cmd.wlen = 1;
404 cmd.rlen = 13;
405 ret = si2168_cmd_execute(client, &cmd);
406 if (ret)
407 goto err;
408
409 chip_id = cmd.args[1] << 24 | cmd.args[2] << 16 | cmd.args[3] << 8 |
410 cmd.args[4] << 0;
411
412 #define SI2168_A20 ('A' << 24 | 68 << 16 | '2' << 8 | '0' << 0)
413 #define SI2168_A30 ('A' << 24 | 68 << 16 | '3' << 8 | '0' << 0)
414 #define SI2168_B40 ('B' << 24 | 68 << 16 | '4' << 8 | '0' << 0)
415
416 switch (chip_id) {
417 case SI2168_A20:
418 fw_name = SI2168_A20_FIRMWARE;
419 break;
420 case SI2168_A30:
421 fw_name = SI2168_A30_FIRMWARE;
422 break;
423 case SI2168_B40:
424 fw_name = SI2168_B40_FIRMWARE;
425 break;
426 default:
427 dev_err(&client->dev, "unknown chip version Si21%d-%c%c%c\n",
428 cmd.args[2], cmd.args[1],
429 cmd.args[3], cmd.args[4]);
430 ret = -EINVAL;
431 goto err;
432 }
433
434 dev_info(&client->dev, "found a 'Silicon Labs Si21%d-%c%c%c'\n",
435 cmd.args[2], cmd.args[1], cmd.args[3], cmd.args[4]);
436
437 /* request the firmware, this will block and timeout */ 399 /* request the firmware, this will block and timeout */
438 ret = request_firmware(&fw, fw_name, &client->dev); 400 ret = request_firmware(&fw, dev->firmware_name, &client->dev);
439 if (ret) { 401 if (ret) {
440 /* fallback mechanism to handle old name for Si2168 B40 fw */ 402 /* fallback mechanism to handle old name for Si2168 B40 fw */
441 if (chip_id == SI2168_B40) { 403 if (dev->chip_id == SI2168_CHIP_ID_B40) {
442 fw_name = SI2168_B40_FIRMWARE_FALLBACK; 404 dev->firmware_name = SI2168_B40_FIRMWARE_FALLBACK;
443 ret = request_firmware(&fw, fw_name, &client->dev); 405 ret = request_firmware(&fw, dev->firmware_name,
406 &client->dev);
444 } 407 }
445 408
446 if (ret == 0) { 409 if (ret == 0) {
@@ -450,13 +413,13 @@ static int si2168_init(struct dvb_frontend *fe)
450 } else { 413 } else {
451 dev_err(&client->dev, 414 dev_err(&client->dev,
452 "firmware file '%s' not found\n", 415 "firmware file '%s' not found\n",
453 fw_name); 416 dev->firmware_name);
454 goto err_release_firmware; 417 goto err_release_firmware;
455 } 418 }
456 } 419 }
457 420
458 dev_info(&client->dev, "downloading firmware from file '%s'\n", 421 dev_info(&client->dev, "downloading firmware from file '%s'\n",
459 fw_name); 422 dev->firmware_name);
460 423
461 if ((fw->size % 17 == 0) && (fw->data[0] > 5)) { 424 if ((fw->size % 17 == 0) && (fw->data[0] > 5)) {
462 /* firmware is in the new format */ 425 /* firmware is in the new format */
@@ -511,8 +474,11 @@ static int si2168_init(struct dvb_frontend *fe)
511 if (ret) 474 if (ret)
512 goto err; 475 goto err;
513 476
514 dev_info(&client->dev, "firmware version: %c.%c.%d\n", 477 dev->version = (cmd.args[9] + '@') << 24 | (cmd.args[6] - '0') << 16 |
515 cmd.args[6], cmd.args[7], cmd.args[8]); 478 (cmd.args[7] - '0') << 8 | (cmd.args[8]) << 0;
479 dev_info(&client->dev, "firmware version: %c %d.%d.%d\n",
480 dev->version >> 24 & 0xff, dev->version >> 16 & 0xff,
481 dev->version >> 8 & 0xff, dev->version >> 0 & 0xff);
516 482
517 /* set ts mode */ 483 /* set ts mode */
518 memcpy(cmd.args, "\x14\x00\x01\x10\x10\x00", 6); 484 memcpy(cmd.args, "\x14\x00\x01\x10\x10\x00", 6);
@@ -525,7 +491,7 @@ static int si2168_init(struct dvb_frontend *fe)
525 if (ret) 491 if (ret)
526 goto err; 492 goto err;
527 493
528 dev->fw_loaded = true; 494 dev->warm = true;
529warm: 495warm:
530 dev->active = true; 496 dev->active = true;
531 497
@@ -549,6 +515,10 @@ static int si2168_sleep(struct dvb_frontend *fe)
549 515
550 dev->active = false; 516 dev->active = false;
551 517
518 /* Firmware B 4.0-11 or later loses warm state during sleep */
519 if (dev->version > ('B' << 24 | 4 << 16 | 0 << 8 | 11 << 0))
520 dev->warm = false;
521
552 memcpy(cmd.args, "\x13", 1); 522 memcpy(cmd.args, "\x13", 1);
553 cmd.wlen = 1; 523 cmd.wlen = 1;
554 cmd.rlen = 0; 524 cmd.rlen = 0;
@@ -653,6 +623,7 @@ static int si2168_probe(struct i2c_client *client,
653 struct si2168_config *config = client->dev.platform_data; 623 struct si2168_config *config = client->dev.platform_data;
654 struct si2168_dev *dev; 624 struct si2168_dev *dev;
655 int ret; 625 int ret;
626 struct si2168_cmd cmd;
656 627
657 dev_dbg(&client->dev, "\n"); 628 dev_dbg(&client->dev, "\n");
658 629
@@ -663,8 +634,56 @@ static int si2168_probe(struct i2c_client *client,
663 goto err; 634 goto err;
664 } 635 }
665 636
637 i2c_set_clientdata(client, dev);
666 mutex_init(&dev->i2c_mutex); 638 mutex_init(&dev->i2c_mutex);
667 639
640 /* Initialize */
641 memcpy(cmd.args, "\xc0\x12\x00\x0c\x00\x0d\x16\x00\x00\x00\x00\x00\x00", 13);
642 cmd.wlen = 13;
643 cmd.rlen = 0;
644 ret = si2168_cmd_execute(client, &cmd);
645 if (ret)
646 goto err_kfree;
647
648 /* Power up */
649 memcpy(cmd.args, "\xc0\x06\x01\x0f\x00\x20\x20\x01", 8);
650 cmd.wlen = 8;
651 cmd.rlen = 1;
652 ret = si2168_cmd_execute(client, &cmd);
653 if (ret)
654 goto err_kfree;
655
656 /* Query chip revision */
657 memcpy(cmd.args, "\x02", 1);
658 cmd.wlen = 1;
659 cmd.rlen = 13;
660 ret = si2168_cmd_execute(client, &cmd);
661 if (ret)
662 goto err_kfree;
663
664 dev->chip_id = cmd.args[1] << 24 | cmd.args[2] << 16 |
665 cmd.args[3] << 8 | cmd.args[4] << 0;
666
667 switch (dev->chip_id) {
668 case SI2168_CHIP_ID_A20:
669 dev->firmware_name = SI2168_A20_FIRMWARE;
670 break;
671 case SI2168_CHIP_ID_A30:
672 dev->firmware_name = SI2168_A30_FIRMWARE;
673 break;
674 case SI2168_CHIP_ID_B40:
675 dev->firmware_name = SI2168_B40_FIRMWARE;
676 break;
677 default:
678 dev_dbg(&client->dev, "unknown chip version Si21%d-%c%c%c\n",
679 cmd.args[2], cmd.args[1], cmd.args[3], cmd.args[4]);
680 ret = -ENODEV;
681 goto err_kfree;
682 }
683
684 dev->version = (cmd.args[1]) << 24 | (cmd.args[3] - '0') << 16 |
685 (cmd.args[4] - '0') << 8 | (cmd.args[5]) << 0;
686
668 /* create mux i2c adapter for tuner */ 687 /* create mux i2c adapter for tuner */
669 dev->muxc = i2c_mux_alloc(client->adapter, &client->dev, 688 dev->muxc = i2c_mux_alloc(client->adapter, &client->dev,
670 1, 0, I2C_MUX_LOCKED, 689 1, 0, I2C_MUX_LOCKED,
@@ -686,11 +705,14 @@ static int si2168_probe(struct i2c_client *client,
686 dev->ts_mode = config->ts_mode; 705 dev->ts_mode = config->ts_mode;
687 dev->ts_clock_inv = config->ts_clock_inv; 706 dev->ts_clock_inv = config->ts_clock_inv;
688 dev->ts_clock_gapped = config->ts_clock_gapped; 707 dev->ts_clock_gapped = config->ts_clock_gapped;
689 dev->fw_loaded = false;
690 708
691 i2c_set_clientdata(client, dev); 709 dev_info(&client->dev, "Silicon Labs Si2168-%c%d%d successfully identified\n",
710 dev->version >> 24 & 0xff, dev->version >> 16 & 0xff,
711 dev->version >> 8 & 0xff);
712 dev_info(&client->dev, "firmware version: %c %d.%d.%d\n",
713 dev->version >> 24 & 0xff, dev->version >> 16 & 0xff,
714 dev->version >> 8 & 0xff, dev->version >> 0 & 0xff);
692 715
693 dev_info(&client->dev, "Silicon Labs Si2168 successfully attached\n");
694 return 0; 716 return 0;
695err_kfree: 717err_kfree:
696 kfree(dev); 718 kfree(dev);
@@ -723,7 +745,8 @@ MODULE_DEVICE_TABLE(i2c, si2168_id_table);
723 745
724static struct i2c_driver si2168_driver = { 746static struct i2c_driver si2168_driver = {
725 .driver = { 747 .driver = {
726 .name = "si2168", 748 .name = "si2168",
749 .suppress_bind_attrs = true,
727 }, 750 },
728 .probe = si2168_probe, 751 .probe = si2168_probe,
729 .remove = si2168_remove, 752 .remove = si2168_remove,
diff --git a/drivers/media/dvb-frontends/si2168_priv.h b/drivers/media/dvb-frontends/si2168_priv.h
index 8a1f36d2014d..7843ccb448a0 100644
--- a/drivers/media/dvb-frontends/si2168_priv.h
+++ b/drivers/media/dvb-frontends/si2168_priv.h
@@ -34,8 +34,14 @@ struct si2168_dev {
34 struct dvb_frontend fe; 34 struct dvb_frontend fe;
35 enum fe_delivery_system delivery_system; 35 enum fe_delivery_system delivery_system;
36 enum fe_status fe_status; 36 enum fe_status fe_status;
37 #define SI2168_CHIP_ID_A20 ('A' << 24 | 68 << 16 | '2' << 8 | '0' << 0)
38 #define SI2168_CHIP_ID_A30 ('A' << 24 | 68 << 16 | '3' << 8 | '0' << 0)
39 #define SI2168_CHIP_ID_B40 ('B' << 24 | 68 << 16 | '4' << 8 | '0' << 0)
40 unsigned int chip_id;
41 unsigned int version;
42 const char *firmware_name;
37 bool active; 43 bool active;
38 bool fw_loaded; 44 bool warm;
39 u8 ts_mode; 45 u8 ts_mode;
40 bool ts_clock_inv; 46 bool ts_clock_inv;
41 bool ts_clock_gapped; 47 bool ts_clock_gapped;
diff --git a/drivers/media/i2c/Kconfig b/drivers/media/i2c/Kconfig
index 993dc50c12db..ce9006e10a30 100644
--- a/drivers/media/i2c/Kconfig
+++ b/drivers/media/i2c/Kconfig
@@ -209,6 +209,7 @@ config VIDEO_ADV7604
209 depends on VIDEO_V4L2 && I2C && VIDEO_V4L2_SUBDEV_API 209 depends on VIDEO_V4L2 && I2C && VIDEO_V4L2_SUBDEV_API
210 depends on GPIOLIB || COMPILE_TEST 210 depends on GPIOLIB || COMPILE_TEST
211 select HDMI 211 select HDMI
212 select MEDIA_CEC_EDID
212 ---help--- 213 ---help---
213 Support for the Analog Devices ADV7604 video decoder. 214 Support for the Analog Devices ADV7604 video decoder.
214 215
@@ -218,10 +219,18 @@ config VIDEO_ADV7604
218 To compile this driver as a module, choose M here: the 219 To compile this driver as a module, choose M here: the
219 module will be called adv7604. 220 module will be called adv7604.
220 221
222config VIDEO_ADV7604_CEC
223 bool "Enable Analog Devices ADV7604 CEC support"
224 depends on VIDEO_ADV7604 && MEDIA_CEC
225 ---help---
226 When selected the adv7604 will support the optional
227 HDMI CEC feature.
228
221config VIDEO_ADV7842 229config VIDEO_ADV7842
222 tristate "Analog Devices ADV7842 decoder" 230 tristate "Analog Devices ADV7842 decoder"
223 depends on VIDEO_V4L2 && I2C && VIDEO_V4L2_SUBDEV_API 231 depends on VIDEO_V4L2 && I2C && VIDEO_V4L2_SUBDEV_API
224 select HDMI 232 select HDMI
233 select MEDIA_CEC_EDID
225 ---help--- 234 ---help---
226 Support for the Analog Devices ADV7842 video decoder. 235 Support for the Analog Devices ADV7842 video decoder.
227 236
@@ -231,6 +240,13 @@ config VIDEO_ADV7842
231 To compile this driver as a module, choose M here: the 240 To compile this driver as a module, choose M here: the
232 module will be called adv7842. 241 module will be called adv7842.
233 242
243config VIDEO_ADV7842_CEC
244 bool "Enable Analog Devices ADV7842 CEC support"
245 depends on VIDEO_ADV7842 && MEDIA_CEC
246 ---help---
247 When selected the adv7842 will support the optional
248 HDMI CEC feature.
249
234config VIDEO_BT819 250config VIDEO_BT819
235 tristate "BT819A VideoStream decoder" 251 tristate "BT819A VideoStream decoder"
236 depends on VIDEO_V4L2 && I2C 252 depends on VIDEO_V4L2 && I2C
@@ -447,6 +463,7 @@ config VIDEO_ADV7511
447 tristate "Analog Devices ADV7511 encoder" 463 tristate "Analog Devices ADV7511 encoder"
448 depends on VIDEO_V4L2 && I2C && VIDEO_V4L2_SUBDEV_API 464 depends on VIDEO_V4L2 && I2C && VIDEO_V4L2_SUBDEV_API
449 select HDMI 465 select HDMI
466 select MEDIA_CEC_EDID
450 ---help--- 467 ---help---
451 Support for the Analog Devices ADV7511 video encoder. 468 Support for the Analog Devices ADV7511 video encoder.
452 469
@@ -455,6 +472,13 @@ config VIDEO_ADV7511
455 To compile this driver as a module, choose M here: the 472 To compile this driver as a module, choose M here: the
456 module will be called adv7511. 473 module will be called adv7511.
457 474
475config VIDEO_ADV7511_CEC
476 bool "Enable Analog Devices ADV7511 CEC support"
477 depends on VIDEO_ADV7511 && MEDIA_CEC
478 ---help---
479 When selected the adv7511 will support the optional
480 HDMI CEC feature.
481
458config VIDEO_AD9389B 482config VIDEO_AD9389B
459 tristate "Analog Devices AD9389B encoder" 483 tristate "Analog Devices AD9389B encoder"
460 depends on VIDEO_V4L2 && I2C && VIDEO_V4L2_SUBDEV_API 484 depends on VIDEO_V4L2 && I2C && VIDEO_V4L2_SUBDEV_API
diff --git a/drivers/media/i2c/adv7511.c b/drivers/media/i2c/adv7511.c
index 39271c35da48..6d7cad54a65d 100644
--- a/drivers/media/i2c/adv7511.c
+++ b/drivers/media/i2c/adv7511.c
@@ -33,6 +33,7 @@
33#include <media/v4l2-ctrls.h> 33#include <media/v4l2-ctrls.h>
34#include <media/v4l2-dv-timings.h> 34#include <media/v4l2-dv-timings.h>
35#include <media/i2c/adv7511.h> 35#include <media/i2c/adv7511.h>
36#include <media/cec.h>
36 37
37static int debug; 38static int debug;
38module_param(debug, int, 0644); 39module_param(debug, int, 0644);
@@ -59,6 +60,8 @@ MODULE_LICENSE("GPL v2");
59#define ADV7511_MIN_PIXELCLOCK 20000000 60#define ADV7511_MIN_PIXELCLOCK 20000000
60#define ADV7511_MAX_PIXELCLOCK 225000000 61#define ADV7511_MAX_PIXELCLOCK 225000000
61 62
63#define ADV7511_MAX_ADDRS (3)
64
62/* 65/*
63********************************************************************** 66**********************************************************************
64* 67*
@@ -90,12 +93,20 @@ struct adv7511_state {
90 struct v4l2_ctrl_handler hdl; 93 struct v4l2_ctrl_handler hdl;
91 int chip_revision; 94 int chip_revision;
92 u8 i2c_edid_addr; 95 u8 i2c_edid_addr;
93 u8 i2c_cec_addr;
94 u8 i2c_pktmem_addr; 96 u8 i2c_pktmem_addr;
97 u8 i2c_cec_addr;
98
99 struct i2c_client *i2c_cec;
100 struct cec_adapter *cec_adap;
101 u8 cec_addr[ADV7511_MAX_ADDRS];
102 u8 cec_valid_addrs;
103 bool cec_enabled_adap;
104
95 /* Is the adv7511 powered on? */ 105 /* Is the adv7511 powered on? */
96 bool power_on; 106 bool power_on;
97 /* Did we receive hotplug and rx-sense signals? */ 107 /* Did we receive hotplug and rx-sense signals? */
98 bool have_monitor; 108 bool have_monitor;
109 bool enabled_irq;
99 /* timings from s_dv_timings */ 110 /* timings from s_dv_timings */
100 struct v4l2_dv_timings dv_timings; 111 struct v4l2_dv_timings dv_timings;
101 u32 fmt_code; 112 u32 fmt_code;
@@ -227,7 +238,7 @@ static int adv_smbus_read_i2c_block_data(struct i2c_client *client,
227 return ret; 238 return ret;
228} 239}
229 240
230static inline void adv7511_edid_rd(struct v4l2_subdev *sd, u16 len, u8 *buf) 241static void adv7511_edid_rd(struct v4l2_subdev *sd, uint16_t len, uint8_t *buf)
231{ 242{
232 struct adv7511_state *state = get_adv7511_state(sd); 243 struct adv7511_state *state = get_adv7511_state(sd);
233 int i; 244 int i;
@@ -242,6 +253,34 @@ static inline void adv7511_edid_rd(struct v4l2_subdev *sd, u16 len, u8 *buf)
242 v4l2_err(sd, "%s: i2c read error\n", __func__); 253 v4l2_err(sd, "%s: i2c read error\n", __func__);
243} 254}
244 255
256static inline int adv7511_cec_read(struct v4l2_subdev *sd, u8 reg)
257{
258 struct adv7511_state *state = get_adv7511_state(sd);
259
260 return i2c_smbus_read_byte_data(state->i2c_cec, reg);
261}
262
263static int adv7511_cec_write(struct v4l2_subdev *sd, u8 reg, u8 val)
264{
265 struct adv7511_state *state = get_adv7511_state(sd);
266 int ret;
267 int i;
268
269 for (i = 0; i < 3; i++) {
270 ret = i2c_smbus_write_byte_data(state->i2c_cec, reg, val);
271 if (ret == 0)
272 return 0;
273 }
274 v4l2_err(sd, "%s: I2C Write Problem\n", __func__);
275 return ret;
276}
277
278static inline int adv7511_cec_write_and_or(struct v4l2_subdev *sd, u8 reg, u8 mask,
279 u8 val)
280{
281 return adv7511_cec_write(sd, reg, (adv7511_cec_read(sd, reg) & mask) | val);
282}
283
245static int adv7511_pktmem_rd(struct v4l2_subdev *sd, u8 reg) 284static int adv7511_pktmem_rd(struct v4l2_subdev *sd, u8 reg)
246{ 285{
247 struct adv7511_state *state = get_adv7511_state(sd); 286 struct adv7511_state *state = get_adv7511_state(sd);
@@ -343,28 +382,20 @@ static void adv7511_csc_rgb_full2limit(struct v4l2_subdev *sd, bool enable)
343 } 382 }
344} 383}
345 384
346static void adv7511_set_IT_content_AVI_InfoFrame(struct v4l2_subdev *sd) 385static void adv7511_set_rgb_quantization_mode(struct v4l2_subdev *sd, struct v4l2_ctrl *ctrl)
347{ 386{
348 struct adv7511_state *state = get_adv7511_state(sd); 387 struct adv7511_state *state = get_adv7511_state(sd);
349 if (state->dv_timings.bt.flags & V4L2_DV_FL_IS_CE_VIDEO) { 388
350 /* CE format, not IT */ 389 /* Only makes sense for RGB formats */
351 adv7511_wr_and_or(sd, 0x57, 0x7f, 0x00); 390 if (state->fmt_code != MEDIA_BUS_FMT_RGB888_1X24) {
352 } else { 391 /* so just keep quantization */
353 /* IT format */ 392 adv7511_csc_rgb_full2limit(sd, false);
354 adv7511_wr_and_or(sd, 0x57, 0x7f, 0x80); 393 return;
355 } 394 }
356}
357 395
358static int adv7511_set_rgb_quantization_mode(struct v4l2_subdev *sd, struct v4l2_ctrl *ctrl)
359{
360 switch (ctrl->val) { 396 switch (ctrl->val) {
361 default: 397 case V4L2_DV_RGB_RANGE_AUTO:
362 return -EINVAL;
363 break;
364 case V4L2_DV_RGB_RANGE_AUTO: {
365 /* automatic */ 398 /* automatic */
366 struct adv7511_state *state = get_adv7511_state(sd);
367
368 if (state->dv_timings.bt.flags & V4L2_DV_FL_IS_CE_VIDEO) { 399 if (state->dv_timings.bt.flags & V4L2_DV_FL_IS_CE_VIDEO) {
369 /* CE format, RGB limited range (16-235) */ 400 /* CE format, RGB limited range (16-235) */
370 adv7511_csc_rgb_full2limit(sd, true); 401 adv7511_csc_rgb_full2limit(sd, true);
@@ -372,7 +403,6 @@ static int adv7511_set_rgb_quantization_mode(struct v4l2_subdev *sd, struct v4l2
372 /* not CE format, RGB full range (0-255) */ 403 /* not CE format, RGB full range (0-255) */
373 adv7511_csc_rgb_full2limit(sd, false); 404 adv7511_csc_rgb_full2limit(sd, false);
374 } 405 }
375 }
376 break; 406 break;
377 case V4L2_DV_RGB_RANGE_LIMITED: 407 case V4L2_DV_RGB_RANGE_LIMITED:
378 /* RGB limited range (16-235) */ 408 /* RGB limited range (16-235) */
@@ -383,7 +413,6 @@ static int adv7511_set_rgb_quantization_mode(struct v4l2_subdev *sd, struct v4l2
383 adv7511_csc_rgb_full2limit(sd, false); 413 adv7511_csc_rgb_full2limit(sd, false);
384 break; 414 break;
385 } 415 }
386 return 0;
387} 416}
388 417
389/* ------------------------------ CTRL OPS ------------------------------ */ 418/* ------------------------------ CTRL OPS ------------------------------ */
@@ -400,8 +429,10 @@ static int adv7511_s_ctrl(struct v4l2_ctrl *ctrl)
400 adv7511_wr_and_or(sd, 0xaf, 0xfd, ctrl->val == V4L2_DV_TX_MODE_HDMI ? 0x02 : 0x00); 429 adv7511_wr_and_or(sd, 0xaf, 0xfd, ctrl->val == V4L2_DV_TX_MODE_HDMI ? 0x02 : 0x00);
401 return 0; 430 return 0;
402 } 431 }
403 if (state->rgb_quantization_range_ctrl == ctrl) 432 if (state->rgb_quantization_range_ctrl == ctrl) {
404 return adv7511_set_rgb_quantization_mode(sd, ctrl); 433 adv7511_set_rgb_quantization_mode(sd, ctrl);
434 return 0;
435 }
405 if (state->content_type_ctrl == ctrl) { 436 if (state->content_type_ctrl == ctrl) {
406 u8 itc, cn; 437 u8 itc, cn;
407 438
@@ -425,16 +456,28 @@ static const struct v4l2_ctrl_ops adv7511_ctrl_ops = {
425#ifdef CONFIG_VIDEO_ADV_DEBUG 456#ifdef CONFIG_VIDEO_ADV_DEBUG
426static void adv7511_inv_register(struct v4l2_subdev *sd) 457static void adv7511_inv_register(struct v4l2_subdev *sd)
427{ 458{
459 struct adv7511_state *state = get_adv7511_state(sd);
460
428 v4l2_info(sd, "0x000-0x0ff: Main Map\n"); 461 v4l2_info(sd, "0x000-0x0ff: Main Map\n");
462 if (state->i2c_cec)
463 v4l2_info(sd, "0x100-0x1ff: CEC Map\n");
429} 464}
430 465
431static int adv7511_g_register(struct v4l2_subdev *sd, struct v4l2_dbg_register *reg) 466static int adv7511_g_register(struct v4l2_subdev *sd, struct v4l2_dbg_register *reg)
432{ 467{
468 struct adv7511_state *state = get_adv7511_state(sd);
469
433 reg->size = 1; 470 reg->size = 1;
434 switch (reg->reg >> 8) { 471 switch (reg->reg >> 8) {
435 case 0: 472 case 0:
436 reg->val = adv7511_rd(sd, reg->reg & 0xff); 473 reg->val = adv7511_rd(sd, reg->reg & 0xff);
437 break; 474 break;
475 case 1:
476 if (state->i2c_cec) {
477 reg->val = adv7511_cec_read(sd, reg->reg & 0xff);
478 break;
479 }
480 /* fall through */
438 default: 481 default:
439 v4l2_info(sd, "Register %03llx not supported\n", reg->reg); 482 v4l2_info(sd, "Register %03llx not supported\n", reg->reg);
440 adv7511_inv_register(sd); 483 adv7511_inv_register(sd);
@@ -445,10 +488,18 @@ static int adv7511_g_register(struct v4l2_subdev *sd, struct v4l2_dbg_register *
445 488
446static int adv7511_s_register(struct v4l2_subdev *sd, const struct v4l2_dbg_register *reg) 489static int adv7511_s_register(struct v4l2_subdev *sd, const struct v4l2_dbg_register *reg)
447{ 490{
491 struct adv7511_state *state = get_adv7511_state(sd);
492
448 switch (reg->reg >> 8) { 493 switch (reg->reg >> 8) {
449 case 0: 494 case 0:
450 adv7511_wr(sd, reg->reg & 0xff, reg->val & 0xff); 495 adv7511_wr(sd, reg->reg & 0xff, reg->val & 0xff);
451 break; 496 break;
497 case 1:
498 if (state->i2c_cec) {
499 adv7511_cec_write(sd, reg->reg & 0xff, reg->val & 0xff);
500 break;
501 }
502 /* fall through */
452 default: 503 default:
453 v4l2_info(sd, "Register %03llx not supported\n", reg->reg); 504 v4l2_info(sd, "Register %03llx not supported\n", reg->reg);
454 adv7511_inv_register(sd); 505 adv7511_inv_register(sd);
@@ -536,6 +587,7 @@ static int adv7511_log_status(struct v4l2_subdev *sd)
536{ 587{
537 struct adv7511_state *state = get_adv7511_state(sd); 588 struct adv7511_state *state = get_adv7511_state(sd);
538 struct adv7511_state_edid *edid = &state->edid; 589 struct adv7511_state_edid *edid = &state->edid;
590 int i;
539 591
540 static const char * const states[] = { 592 static const char * const states[] = {
541 "in reset", 593 "in reset",
@@ -605,7 +657,23 @@ static int adv7511_log_status(struct v4l2_subdev *sd)
605 else 657 else
606 v4l2_info(sd, "no timings set\n"); 658 v4l2_info(sd, "no timings set\n");
607 v4l2_info(sd, "i2c edid addr: 0x%x\n", state->i2c_edid_addr); 659 v4l2_info(sd, "i2c edid addr: 0x%x\n", state->i2c_edid_addr);
660
661 if (state->i2c_cec == NULL)
662 return 0;
663
608 v4l2_info(sd, "i2c cec addr: 0x%x\n", state->i2c_cec_addr); 664 v4l2_info(sd, "i2c cec addr: 0x%x\n", state->i2c_cec_addr);
665
666 v4l2_info(sd, "CEC: %s\n", state->cec_enabled_adap ?
667 "enabled" : "disabled");
668 if (state->cec_enabled_adap) {
669 for (i = 0; i < ADV7511_MAX_ADDRS; i++) {
670 bool is_valid = state->cec_valid_addrs & (1 << i);
671
672 if (is_valid)
673 v4l2_info(sd, "CEC Logical Address: 0x%x\n",
674 state->cec_addr[i]);
675 }
676 }
609 v4l2_info(sd, "i2c pktmem addr: 0x%x\n", state->i2c_pktmem_addr); 677 v4l2_info(sd, "i2c pktmem addr: 0x%x\n", state->i2c_pktmem_addr);
610 return 0; 678 return 0;
611} 679}
@@ -663,15 +731,197 @@ static int adv7511_s_power(struct v4l2_subdev *sd, int on)
663 return true; 731 return true;
664} 732}
665 733
734#if IS_ENABLED(CONFIG_VIDEO_ADV7511_CEC)
735static int adv7511_cec_adap_enable(struct cec_adapter *adap, bool enable)
736{
737 struct adv7511_state *state = adap->priv;
738 struct v4l2_subdev *sd = &state->sd;
739
740 if (state->i2c_cec == NULL)
741 return -EIO;
742
743 if (!state->cec_enabled_adap && enable) {
744 /* power up cec section */
745 adv7511_cec_write_and_or(sd, 0x4e, 0xfc, 0x01);
746 /* legacy mode and clear all rx buffers */
747 adv7511_cec_write(sd, 0x4a, 0x07);
748 adv7511_cec_write(sd, 0x4a, 0);
749 adv7511_cec_write_and_or(sd, 0x11, 0xfe, 0); /* initially disable tx */
750 /* enabled irqs: */
751 /* tx: ready */
752 /* tx: arbitration lost */
753 /* tx: retry timeout */
754 /* rx: ready 1 */
755 if (state->enabled_irq)
756 adv7511_wr_and_or(sd, 0x95, 0xc0, 0x39);
757 } else if (state->cec_enabled_adap && !enable) {
758 if (state->enabled_irq)
759 adv7511_wr_and_or(sd, 0x95, 0xc0, 0x00);
760 /* disable address mask 1-3 */
761 adv7511_cec_write_and_or(sd, 0x4b, 0x8f, 0x00);
762 /* power down cec section */
763 adv7511_cec_write_and_or(sd, 0x4e, 0xfc, 0x00);
764 state->cec_valid_addrs = 0;
765 }
766 state->cec_enabled_adap = enable;
767 return 0;
768}
769
770static int adv7511_cec_adap_log_addr(struct cec_adapter *adap, u8 addr)
771{
772 struct adv7511_state *state = adap->priv;
773 struct v4l2_subdev *sd = &state->sd;
774 unsigned int i, free_idx = ADV7511_MAX_ADDRS;
775
776 if (!state->cec_enabled_adap)
777 return addr == CEC_LOG_ADDR_INVALID ? 0 : -EIO;
778
779 if (addr == CEC_LOG_ADDR_INVALID) {
780 adv7511_cec_write_and_or(sd, 0x4b, 0x8f, 0);
781 state->cec_valid_addrs = 0;
782 return 0;
783 }
784
785 for (i = 0; i < ADV7511_MAX_ADDRS; i++) {
786 bool is_valid = state->cec_valid_addrs & (1 << i);
787
788 if (free_idx == ADV7511_MAX_ADDRS && !is_valid)
789 free_idx = i;
790 if (is_valid && state->cec_addr[i] == addr)
791 return 0;
792 }
793 if (i == ADV7511_MAX_ADDRS) {
794 i = free_idx;
795 if (i == ADV7511_MAX_ADDRS)
796 return -ENXIO;
797 }
798 state->cec_addr[i] = addr;
799 state->cec_valid_addrs |= 1 << i;
800
801 switch (i) {
802 case 0:
803 /* enable address mask 0 */
804 adv7511_cec_write_and_or(sd, 0x4b, 0xef, 0x10);
805 /* set address for mask 0 */
806 adv7511_cec_write_and_or(sd, 0x4c, 0xf0, addr);
807 break;
808 case 1:
809 /* enable address mask 1 */
810 adv7511_cec_write_and_or(sd, 0x4b, 0xdf, 0x20);
811 /* set address for mask 1 */
812 adv7511_cec_write_and_or(sd, 0x4c, 0x0f, addr << 4);
813 break;
814 case 2:
815 /* enable address mask 2 */
816 adv7511_cec_write_and_or(sd, 0x4b, 0xbf, 0x40);
817 /* set address for mask 1 */
818 adv7511_cec_write_and_or(sd, 0x4d, 0xf0, addr);
819 break;
820 }
821 return 0;
822}
823
824static int adv7511_cec_adap_transmit(struct cec_adapter *adap, u8 attempts,
825 u32 signal_free_time, struct cec_msg *msg)
826{
827 struct adv7511_state *state = adap->priv;
828 struct v4l2_subdev *sd = &state->sd;
829 u8 len = msg->len;
830 unsigned int i;
831
832 v4l2_dbg(1, debug, sd, "%s: len %d\n", __func__, len);
833
834 if (len > 16) {
835 v4l2_err(sd, "%s: len exceeded 16 (%d)\n", __func__, len);
836 return -EINVAL;
837 }
838
839 /*
840 * The number of retries is the number of attempts - 1, but retry
841 * at least once. It's not clear if a value of 0 is allowed, so
842 * let's do at least one retry.
843 */
844 adv7511_cec_write_and_or(sd, 0x12, ~0x70, max(1, attempts - 1) << 4);
845
846 /* blocking, clear cec tx irq status */
847 adv7511_wr_and_or(sd, 0x97, 0xc7, 0x38);
848
849 /* write data */
850 for (i = 0; i < len; i++)
851 adv7511_cec_write(sd, i, msg->msg[i]);
852
853 /* set length (data + header) */
854 adv7511_cec_write(sd, 0x10, len);
855 /* start transmit, enable tx */
856 adv7511_cec_write(sd, 0x11, 0x01);
857 return 0;
858}
859
860static void adv_cec_tx_raw_status(struct v4l2_subdev *sd, u8 tx_raw_status)
861{
862 struct adv7511_state *state = get_adv7511_state(sd);
863
864 if ((adv7511_cec_read(sd, 0x11) & 0x01) == 0) {
865 v4l2_dbg(1, debug, sd, "%s: tx raw: tx disabled\n", __func__);
866 return;
867 }
868
869 if (tx_raw_status & 0x10) {
870 v4l2_dbg(1, debug, sd,
871 "%s: tx raw: arbitration lost\n", __func__);
872 cec_transmit_done(state->cec_adap, CEC_TX_STATUS_ARB_LOST,
873 1, 0, 0, 0);
874 return;
875 }
876 if (tx_raw_status & 0x08) {
877 u8 status;
878 u8 nack_cnt;
879 u8 low_drive_cnt;
880
881 v4l2_dbg(1, debug, sd, "%s: tx raw: retry failed\n", __func__);
882 /*
883 * We set this status bit since this hardware performs
884 * retransmissions.
885 */
886 status = CEC_TX_STATUS_MAX_RETRIES;
887 nack_cnt = adv7511_cec_read(sd, 0x14) & 0xf;
888 if (nack_cnt)
889 status |= CEC_TX_STATUS_NACK;
890 low_drive_cnt = adv7511_cec_read(sd, 0x14) >> 4;
891 if (low_drive_cnt)
892 status |= CEC_TX_STATUS_LOW_DRIVE;
893 cec_transmit_done(state->cec_adap, status,
894 0, nack_cnt, low_drive_cnt, 0);
895 return;
896 }
897 if (tx_raw_status & 0x20) {
898 v4l2_dbg(1, debug, sd, "%s: tx raw: ready ok\n", __func__);
899 cec_transmit_done(state->cec_adap, CEC_TX_STATUS_OK, 0, 0, 0, 0);
900 return;
901 }
902}
903
904static const struct cec_adap_ops adv7511_cec_adap_ops = {
905 .adap_enable = adv7511_cec_adap_enable,
906 .adap_log_addr = adv7511_cec_adap_log_addr,
907 .adap_transmit = adv7511_cec_adap_transmit,
908};
909#endif
910
666/* Enable interrupts */ 911/* Enable interrupts */
667static void adv7511_set_isr(struct v4l2_subdev *sd, bool enable) 912static void adv7511_set_isr(struct v4l2_subdev *sd, bool enable)
668{ 913{
914 struct adv7511_state *state = get_adv7511_state(sd);
669 u8 irqs = MASK_ADV7511_HPD_INT | MASK_ADV7511_MSEN_INT; 915 u8 irqs = MASK_ADV7511_HPD_INT | MASK_ADV7511_MSEN_INT;
670 u8 irqs_rd; 916 u8 irqs_rd;
671 int retries = 100; 917 int retries = 100;
672 918
673 v4l2_dbg(2, debug, sd, "%s: %s\n", __func__, enable ? "enable" : "disable"); 919 v4l2_dbg(2, debug, sd, "%s: %s\n", __func__, enable ? "enable" : "disable");
674 920
921 if (state->enabled_irq == enable)
922 return;
923 state->enabled_irq = enable;
924
675 /* The datasheet says that the EDID ready interrupt should be 925 /* The datasheet says that the EDID ready interrupt should be
676 disabled if there is no hotplug. */ 926 disabled if there is no hotplug. */
677 if (!enable) 927 if (!enable)
@@ -679,6 +929,9 @@ static void adv7511_set_isr(struct v4l2_subdev *sd, bool enable)
679 else if (adv7511_have_hotplug(sd)) 929 else if (adv7511_have_hotplug(sd))
680 irqs |= MASK_ADV7511_EDID_RDY_INT; 930 irqs |= MASK_ADV7511_EDID_RDY_INT;
681 931
932 adv7511_wr_and_or(sd, 0x95, 0xc0,
933 (state->cec_enabled_adap && enable) ? 0x39 : 0x00);
934
682 /* 935 /*
683 * This i2c write can fail (approx. 1 in 1000 writes). But it 936 * This i2c write can fail (approx. 1 in 1000 writes). But it
684 * is essential that this register is correct, so retry it 937 * is essential that this register is correct, so retry it
@@ -701,20 +954,53 @@ static void adv7511_set_isr(struct v4l2_subdev *sd, bool enable)
701static int adv7511_isr(struct v4l2_subdev *sd, u32 status, bool *handled) 954static int adv7511_isr(struct v4l2_subdev *sd, u32 status, bool *handled)
702{ 955{
703 u8 irq_status; 956 u8 irq_status;
957 u8 cec_irq;
704 958
705 /* disable interrupts to prevent a race condition */ 959 /* disable interrupts to prevent a race condition */
706 adv7511_set_isr(sd, false); 960 adv7511_set_isr(sd, false);
707 irq_status = adv7511_rd(sd, 0x96); 961 irq_status = adv7511_rd(sd, 0x96);
962 cec_irq = adv7511_rd(sd, 0x97);
708 /* clear detected interrupts */ 963 /* clear detected interrupts */
709 adv7511_wr(sd, 0x96, irq_status); 964 adv7511_wr(sd, 0x96, irq_status);
965 adv7511_wr(sd, 0x97, cec_irq);
710 966
711 v4l2_dbg(1, debug, sd, "%s: irq 0x%x\n", __func__, irq_status); 967 v4l2_dbg(1, debug, sd, "%s: irq 0x%x, cec-irq 0x%x\n", __func__,
968 irq_status, cec_irq);
712 969
713 if (irq_status & (MASK_ADV7511_HPD_INT | MASK_ADV7511_MSEN_INT)) 970 if (irq_status & (MASK_ADV7511_HPD_INT | MASK_ADV7511_MSEN_INT))
714 adv7511_check_monitor_present_status(sd); 971 adv7511_check_monitor_present_status(sd);
715 if (irq_status & MASK_ADV7511_EDID_RDY_INT) 972 if (irq_status & MASK_ADV7511_EDID_RDY_INT)
716 adv7511_check_edid_status(sd); 973 adv7511_check_edid_status(sd);
717 974
975#if IS_ENABLED(CONFIG_VIDEO_ADV7511_CEC)
976 if (cec_irq & 0x38)
977 adv_cec_tx_raw_status(sd, cec_irq);
978
979 if (cec_irq & 1) {
980 struct adv7511_state *state = get_adv7511_state(sd);
981 struct cec_msg msg;
982
983 msg.len = adv7511_cec_read(sd, 0x25) & 0x1f;
984
985 v4l2_dbg(1, debug, sd, "%s: cec msg len %d\n", __func__,
986 msg.len);
987
988 if (msg.len > 16)
989 msg.len = 16;
990
991 if (msg.len) {
992 u8 i;
993
994 for (i = 0; i < msg.len; i++)
995 msg.msg[i] = adv7511_cec_read(sd, i + 0x15);
996
997 adv7511_cec_write(sd, 0x4a, 1); /* toggle to re-enable rx 1 */
998 adv7511_cec_write(sd, 0x4a, 0);
999 cec_received_msg(state->cec_adap, &msg);
1000 }
1001 }
1002#endif
1003
718 /* enable interrupts */ 1004 /* enable interrupts */
719 adv7511_set_isr(sd, true); 1005 adv7511_set_isr(sd, true);
720 1006
@@ -771,12 +1057,14 @@ static int adv7511_s_dv_timings(struct v4l2_subdev *sd,
771 /* save timings */ 1057 /* save timings */
772 state->dv_timings = *timings; 1058 state->dv_timings = *timings;
773 1059
1060 /* set h/vsync polarities */
1061 adv7511_wr_and_or(sd, 0x17, 0x9f,
1062 ((timings->bt.polarities & V4L2_DV_VSYNC_POS_POL) ? 0 : 0x40) |
1063 ((timings->bt.polarities & V4L2_DV_HSYNC_POS_POL) ? 0 : 0x20));
1064
774 /* update quantization range based on new dv_timings */ 1065 /* update quantization range based on new dv_timings */
775 adv7511_set_rgb_quantization_mode(sd, state->rgb_quantization_range_ctrl); 1066 adv7511_set_rgb_quantization_mode(sd, state->rgb_quantization_range_ctrl);
776 1067
777 /* update AVI infoframe */
778 adv7511_set_IT_content_AVI_InfoFrame(sd);
779
780 return 0; 1068 return 0;
781} 1069}
782 1070
@@ -956,8 +1244,6 @@ static int adv7511_enum_mbus_code(struct v4l2_subdev *sd,
956static void adv7511_fill_format(struct adv7511_state *state, 1244static void adv7511_fill_format(struct adv7511_state *state,
957 struct v4l2_mbus_framefmt *format) 1245 struct v4l2_mbus_framefmt *format)
958{ 1246{
959 memset(format, 0, sizeof(*format));
960
961 format->width = state->dv_timings.bt.width; 1247 format->width = state->dv_timings.bt.width;
962 format->height = state->dv_timings.bt.height; 1248 format->height = state->dv_timings.bt.height;
963 format->field = V4L2_FIELD_NONE; 1249 format->field = V4L2_FIELD_NONE;
@@ -972,6 +1258,7 @@ static int adv7511_get_fmt(struct v4l2_subdev *sd,
972 if (format->pad != 0) 1258 if (format->pad != 0)
973 return -EINVAL; 1259 return -EINVAL;
974 1260
1261 memset(&format->format, 0, sizeof(format->format));
975 adv7511_fill_format(state, &format->format); 1262 adv7511_fill_format(state, &format->format);
976 1263
977 if (format->which == V4L2_SUBDEV_FORMAT_TRY) { 1264 if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
@@ -1132,6 +1419,7 @@ static int adv7511_set_fmt(struct v4l2_subdev *sd,
1132 adv7511_wr_and_or(sd, 0x57, 0x83, (ec << 4) | (q << 2) | (itc << 7)); 1419 adv7511_wr_and_or(sd, 0x57, 0x83, (ec << 4) | (q << 2) | (itc << 7));
1133 adv7511_wr_and_or(sd, 0x59, 0x0f, (yq << 6) | (cn << 4)); 1420 adv7511_wr_and_or(sd, 0x59, 0x0f, (yq << 6) | (cn << 4));
1134 adv7511_wr_and_or(sd, 0x4a, 0xff, 1); 1421 adv7511_wr_and_or(sd, 0x4a, 0xff, 1);
1422 adv7511_set_rgb_quantization_mode(sd, state->rgb_quantization_range_ctrl);
1135 1423
1136 return 0; 1424 return 0;
1137} 1425}
@@ -1183,6 +1471,8 @@ static void adv7511_notify_no_edid(struct v4l2_subdev *sd)
1183 /* We failed to read the EDID, so send an event for this. */ 1471 /* We failed to read the EDID, so send an event for this. */
1184 ed.present = false; 1472 ed.present = false;
1185 ed.segment = adv7511_rd(sd, 0xc4); 1473 ed.segment = adv7511_rd(sd, 0xc4);
1474 ed.phys_addr = CEC_PHYS_ADDR_INVALID;
1475 cec_s_phys_addr(state->cec_adap, ed.phys_addr, false);
1186 v4l2_subdev_notify(sd, ADV7511_EDID_DETECT, (void *)&ed); 1476 v4l2_subdev_notify(sd, ADV7511_EDID_DETECT, (void *)&ed);
1187 v4l2_ctrl_s_ctrl(state->have_edid0_ctrl, 0x0); 1477 v4l2_ctrl_s_ctrl(state->have_edid0_ctrl, 0x0);
1188} 1478}
@@ -1406,13 +1696,16 @@ static bool adv7511_check_edid_status(struct v4l2_subdev *sd)
1406 1696
1407 v4l2_dbg(1, debug, sd, "%s: edid complete with %d segment(s)\n", __func__, state->edid.segments); 1697 v4l2_dbg(1, debug, sd, "%s: edid complete with %d segment(s)\n", __func__, state->edid.segments);
1408 state->edid.complete = true; 1698 state->edid.complete = true;
1409 1699 ed.phys_addr = cec_get_edid_phys_addr(state->edid.data,
1700 state->edid.segments * 256,
1701 NULL);
1410 /* report when we have all segments 1702 /* report when we have all segments
1411 but report only for segment 0 1703 but report only for segment 0
1412 */ 1704 */
1413 ed.present = true; 1705 ed.present = true;
1414 ed.segment = 0; 1706 ed.segment = 0;
1415 state->edid_detect_counter++; 1707 state->edid_detect_counter++;
1708 cec_s_phys_addr(state->cec_adap, ed.phys_addr, false);
1416 v4l2_subdev_notify(sd, ADV7511_EDID_DETECT, (void *)&ed); 1709 v4l2_subdev_notify(sd, ADV7511_EDID_DETECT, (void *)&ed);
1417 return ed.present; 1710 return ed.present;
1418 } 1711 }
@@ -1420,17 +1713,43 @@ static bool adv7511_check_edid_status(struct v4l2_subdev *sd)
1420 return false; 1713 return false;
1421} 1714}
1422 1715
1716static int adv7511_registered(struct v4l2_subdev *sd)
1717{
1718 struct adv7511_state *state = get_adv7511_state(sd);
1719 int err;
1720
1721 err = cec_register_adapter(state->cec_adap);
1722 if (err)
1723 cec_delete_adapter(state->cec_adap);
1724 return err;
1725}
1726
1727static void adv7511_unregistered(struct v4l2_subdev *sd)
1728{
1729 struct adv7511_state *state = get_adv7511_state(sd);
1730
1731 cec_unregister_adapter(state->cec_adap);
1732}
1733
1734static const struct v4l2_subdev_internal_ops adv7511_int_ops = {
1735 .registered = adv7511_registered,
1736 .unregistered = adv7511_unregistered,
1737};
1738
1423/* ----------------------------------------------------------------------- */ 1739/* ----------------------------------------------------------------------- */
1424/* Setup ADV7511 */ 1740/* Setup ADV7511 */
1425static void adv7511_init_setup(struct v4l2_subdev *sd) 1741static void adv7511_init_setup(struct v4l2_subdev *sd)
1426{ 1742{
1427 struct adv7511_state *state = get_adv7511_state(sd); 1743 struct adv7511_state *state = get_adv7511_state(sd);
1428 struct adv7511_state_edid *edid = &state->edid; 1744 struct adv7511_state_edid *edid = &state->edid;
1745 u32 cec_clk = state->pdata.cec_clk;
1746 u8 ratio;
1429 1747
1430 v4l2_dbg(1, debug, sd, "%s\n", __func__); 1748 v4l2_dbg(1, debug, sd, "%s\n", __func__);
1431 1749
1432 /* clear all interrupts */ 1750 /* clear all interrupts */
1433 adv7511_wr(sd, 0x96, 0xff); 1751 adv7511_wr(sd, 0x96, 0xff);
1752 adv7511_wr(sd, 0x97, 0xff);
1434 /* 1753 /*
1435 * Stop HPD from resetting a lot of registers. 1754 * Stop HPD from resetting a lot of registers.
1436 * It might leave the chip in a partly un-initialized state, 1755 * It might leave the chip in a partly un-initialized state,
@@ -1442,6 +1761,25 @@ static void adv7511_init_setup(struct v4l2_subdev *sd)
1442 adv7511_set_isr(sd, false); 1761 adv7511_set_isr(sd, false);
1443 adv7511_s_stream(sd, false); 1762 adv7511_s_stream(sd, false);
1444 adv7511_s_audio_stream(sd, false); 1763 adv7511_s_audio_stream(sd, false);
1764
1765 if (state->i2c_cec == NULL)
1766 return;
1767
1768 v4l2_dbg(1, debug, sd, "%s: cec_clk %d\n", __func__, cec_clk);
1769
1770 /* cec soft reset */
1771 adv7511_cec_write(sd, 0x50, 0x01);
1772 adv7511_cec_write(sd, 0x50, 0x00);
1773
1774 /* legacy mode */
1775 adv7511_cec_write(sd, 0x4a, 0x00);
1776
1777 if (cec_clk % 750000 != 0)
1778 v4l2_err(sd, "%s: cec_clk %d, not multiple of 750 Khz\n",
1779 __func__, cec_clk);
1780
1781 ratio = (cec_clk / 750000) - 1;
1782 adv7511_cec_write(sd, 0x4e, ratio << 2);
1445} 1783}
1446 1784
1447static int adv7511_probe(struct i2c_client *client, const struct i2c_device_id *id) 1785static int adv7511_probe(struct i2c_client *client, const struct i2c_device_id *id)
@@ -1476,6 +1814,7 @@ static int adv7511_probe(struct i2c_client *client, const struct i2c_device_id *
1476 client->addr << 1); 1814 client->addr << 1);
1477 1815
1478 v4l2_i2c_subdev_init(sd, client, &adv7511_ops); 1816 v4l2_i2c_subdev_init(sd, client, &adv7511_ops);
1817 sd->internal_ops = &adv7511_int_ops;
1479 1818
1480 hdl = &state->hdl; 1819 hdl = &state->hdl;
1481 v4l2_ctrl_handler_init(hdl, 10); 1820 v4l2_ctrl_handler_init(hdl, 10);
@@ -1516,26 +1855,47 @@ static int adv7511_probe(struct i2c_client *client, const struct i2c_device_id *
1516 chip_id[0] = adv7511_rd(sd, 0xf5); 1855 chip_id[0] = adv7511_rd(sd, 0xf5);
1517 chip_id[1] = adv7511_rd(sd, 0xf6); 1856 chip_id[1] = adv7511_rd(sd, 0xf6);
1518 if (chip_id[0] != 0x75 || chip_id[1] != 0x11) { 1857 if (chip_id[0] != 0x75 || chip_id[1] != 0x11) {
1519 v4l2_err(sd, "chip_id != 0x7511, read 0x%02x%02x\n", chip_id[0], chip_id[1]); 1858 v4l2_err(sd, "chip_id != 0x7511, read 0x%02x%02x\n", chip_id[0],
1859 chip_id[1]);
1520 err = -EIO; 1860 err = -EIO;
1521 goto err_entity; 1861 goto err_entity;
1522 } 1862 }
1523 1863
1524 state->i2c_edid = i2c_new_dummy(client->adapter, state->i2c_edid_addr >> 1); 1864 state->i2c_edid = i2c_new_dummy(client->adapter,
1865 state->i2c_edid_addr >> 1);
1525 if (state->i2c_edid == NULL) { 1866 if (state->i2c_edid == NULL) {
1526 v4l2_err(sd, "failed to register edid i2c client\n"); 1867 v4l2_err(sd, "failed to register edid i2c client\n");
1527 err = -ENOMEM; 1868 err = -ENOMEM;
1528 goto err_entity; 1869 goto err_entity;
1529 } 1870 }
1530 1871
1872 adv7511_wr(sd, 0xe1, state->i2c_cec_addr);
1873 if (state->pdata.cec_clk < 3000000 ||
1874 state->pdata.cec_clk > 100000000) {
1875 v4l2_err(sd, "%s: cec_clk %u outside range, disabling cec\n",
1876 __func__, state->pdata.cec_clk);
1877 state->pdata.cec_clk = 0;
1878 }
1879
1880 if (state->pdata.cec_clk) {
1881 state->i2c_cec = i2c_new_dummy(client->adapter,
1882 state->i2c_cec_addr >> 1);
1883 if (state->i2c_cec == NULL) {
1884 v4l2_err(sd, "failed to register cec i2c client\n");
1885 goto err_unreg_edid;
1886 }
1887 adv7511_wr(sd, 0xe2, 0x00); /* power up cec section */
1888 } else {
1889 adv7511_wr(sd, 0xe2, 0x01); /* power down cec section */
1890 }
1891
1531 state->i2c_pktmem = i2c_new_dummy(client->adapter, state->i2c_pktmem_addr >> 1); 1892 state->i2c_pktmem = i2c_new_dummy(client->adapter, state->i2c_pktmem_addr >> 1);
1532 if (state->i2c_pktmem == NULL) { 1893 if (state->i2c_pktmem == NULL) {
1533 v4l2_err(sd, "failed to register pktmem i2c client\n"); 1894 v4l2_err(sd, "failed to register pktmem i2c client\n");
1534 err = -ENOMEM; 1895 err = -ENOMEM;
1535 goto err_unreg_edid; 1896 goto err_unreg_cec;
1536 } 1897 }
1537 1898
1538 adv7511_wr(sd, 0xe2, 0x01); /* power down cec section */
1539 state->work_queue = create_singlethread_workqueue(sd->name); 1899 state->work_queue = create_singlethread_workqueue(sd->name);
1540 if (state->work_queue == NULL) { 1900 if (state->work_queue == NULL) {
1541 v4l2_err(sd, "could not create workqueue\n"); 1901 v4l2_err(sd, "could not create workqueue\n");
@@ -1546,6 +1906,19 @@ static int adv7511_probe(struct i2c_client *client, const struct i2c_device_id *
1546 INIT_DELAYED_WORK(&state->edid_handler, adv7511_edid_handler); 1906 INIT_DELAYED_WORK(&state->edid_handler, adv7511_edid_handler);
1547 1907
1548 adv7511_init_setup(sd); 1908 adv7511_init_setup(sd);
1909
1910#if IS_ENABLED(CONFIG_VIDEO_ADV7511_CEC)
1911 state->cec_adap = cec_allocate_adapter(&adv7511_cec_adap_ops,
1912 state, dev_name(&client->dev), CEC_CAP_TRANSMIT |
1913 CEC_CAP_LOG_ADDRS | CEC_CAP_PASSTHROUGH | CEC_CAP_RC,
1914 ADV7511_MAX_ADDRS, &client->dev);
1915 err = PTR_ERR_OR_ZERO(state->cec_adap);
1916 if (err) {
1917 destroy_workqueue(state->work_queue);
1918 goto err_unreg_pktmem;
1919 }
1920#endif
1921
1549 adv7511_set_isr(sd, true); 1922 adv7511_set_isr(sd, true);
1550 adv7511_check_monitor_present_status(sd); 1923 adv7511_check_monitor_present_status(sd);
1551 1924
@@ -1555,6 +1928,9 @@ static int adv7511_probe(struct i2c_client *client, const struct i2c_device_id *
1555 1928
1556err_unreg_pktmem: 1929err_unreg_pktmem:
1557 i2c_unregister_device(state->i2c_pktmem); 1930 i2c_unregister_device(state->i2c_pktmem);
1931err_unreg_cec:
1932 if (state->i2c_cec)
1933 i2c_unregister_device(state->i2c_cec);
1558err_unreg_edid: 1934err_unreg_edid:
1559 i2c_unregister_device(state->i2c_edid); 1935 i2c_unregister_device(state->i2c_edid);
1560err_entity: 1936err_entity:
@@ -1576,9 +1952,12 @@ static int adv7511_remove(struct i2c_client *client)
1576 v4l2_dbg(1, debug, sd, "%s removed @ 0x%x (%s)\n", client->name, 1952 v4l2_dbg(1, debug, sd, "%s removed @ 0x%x (%s)\n", client->name,
1577 client->addr << 1, client->adapter->name); 1953 client->addr << 1, client->adapter->name);
1578 1954
1955 adv7511_set_isr(sd, false);
1579 adv7511_init_setup(sd); 1956 adv7511_init_setup(sd);
1580 cancel_delayed_work(&state->edid_handler); 1957 cancel_delayed_work(&state->edid_handler);
1581 i2c_unregister_device(state->i2c_edid); 1958 i2c_unregister_device(state->i2c_edid);
1959 if (state->i2c_cec)
1960 i2c_unregister_device(state->i2c_cec);
1582 i2c_unregister_device(state->i2c_pktmem); 1961 i2c_unregister_device(state->i2c_pktmem);
1583 destroy_workqueue(state->work_queue); 1962 destroy_workqueue(state->work_queue);
1584 v4l2_device_unregister_subdev(sd); 1963 v4l2_device_unregister_subdev(sd);
diff --git a/drivers/media/i2c/adv7604.c b/drivers/media/i2c/adv7604.c
index 3f1ab4986cfc..4003831de712 100644
--- a/drivers/media/i2c/adv7604.c
+++ b/drivers/media/i2c/adv7604.c
@@ -40,6 +40,7 @@
40#include <linux/regmap.h> 40#include <linux/regmap.h>
41 41
42#include <media/i2c/adv7604.h> 42#include <media/i2c/adv7604.h>
43#include <media/cec.h>
43#include <media/v4l2-ctrls.h> 44#include <media/v4l2-ctrls.h>
44#include <media/v4l2-device.h> 45#include <media/v4l2-device.h>
45#include <media/v4l2-event.h> 46#include <media/v4l2-event.h>
@@ -80,6 +81,8 @@ MODULE_LICENSE("GPL");
80 81
81#define ADV76XX_OP_SWAP_CB_CR (1 << 0) 82#define ADV76XX_OP_SWAP_CB_CR (1 << 0)
82 83
84#define ADV76XX_MAX_ADDRS (3)
85
83enum adv76xx_type { 86enum adv76xx_type {
84 ADV7604, 87 ADV7604,
85 ADV7611, 88 ADV7611,
@@ -164,6 +167,7 @@ struct adv76xx_state {
164 struct adv76xx_platform_data pdata; 167 struct adv76xx_platform_data pdata;
165 168
166 struct gpio_desc *hpd_gpio[4]; 169 struct gpio_desc *hpd_gpio[4];
170 struct gpio_desc *reset_gpio;
167 171
168 struct v4l2_subdev sd; 172 struct v4l2_subdev sd;
169 struct media_pad pads[ADV76XX_PAD_MAX]; 173 struct media_pad pads[ADV76XX_PAD_MAX];
@@ -184,10 +188,15 @@ struct adv76xx_state {
184 u16 spa_port_a[2]; 188 u16 spa_port_a[2];
185 struct v4l2_fract aspect_ratio; 189 struct v4l2_fract aspect_ratio;
186 u32 rgb_quantization_range; 190 u32 rgb_quantization_range;
187 struct workqueue_struct *work_queues;
188 struct delayed_work delayed_work_enable_hotplug; 191 struct delayed_work delayed_work_enable_hotplug;
189 bool restart_stdi_once; 192 bool restart_stdi_once;
190 193
194 /* CEC */
195 struct cec_adapter *cec_adap;
196 u8 cec_addr[ADV76XX_MAX_ADDRS];
197 u8 cec_valid_addrs;
198 bool cec_enabled_adap;
199
191 /* i2c clients */ 200 /* i2c clients */
192 struct i2c_client *i2c_clients[ADV76XX_PAGE_MAX]; 201 struct i2c_client *i2c_clients[ADV76XX_PAGE_MAX];
193 202
@@ -381,7 +390,8 @@ static inline int io_write(struct v4l2_subdev *sd, u8 reg, u8 val)
381 return regmap_write(state->regmap[ADV76XX_PAGE_IO], reg, val); 390 return regmap_write(state->regmap[ADV76XX_PAGE_IO], reg, val);
382} 391}
383 392
384static inline int io_write_clr_set(struct v4l2_subdev *sd, u8 reg, u8 mask, u8 val) 393static inline int io_write_clr_set(struct v4l2_subdev *sd, u8 reg, u8 mask,
394 u8 val)
385{ 395{
386 return io_write(sd, reg, (io_read(sd, reg) & ~mask) | val); 396 return io_write(sd, reg, (io_read(sd, reg) & ~mask) | val);
387} 397}
@@ -414,6 +424,12 @@ static inline int cec_write(struct v4l2_subdev *sd, u8 reg, u8 val)
414 return regmap_write(state->regmap[ADV76XX_PAGE_CEC], reg, val); 424 return regmap_write(state->regmap[ADV76XX_PAGE_CEC], reg, val);
415} 425}
416 426
427static inline int cec_write_clr_set(struct v4l2_subdev *sd, u8 reg, u8 mask,
428 u8 val)
429{
430 return cec_write(sd, reg, (cec_read(sd, reg) & ~mask) | val);
431}
432
417static inline int infoframe_read(struct v4l2_subdev *sd, u8 reg) 433static inline int infoframe_read(struct v4l2_subdev *sd, u8 reg)
418{ 434{
419 struct adv76xx_state *state = to_state(sd); 435 struct adv76xx_state *state = to_state(sd);
@@ -892,9 +908,9 @@ static int adv76xx_s_detect_tx_5v_ctrl(struct v4l2_subdev *sd)
892{ 908{
893 struct adv76xx_state *state = to_state(sd); 909 struct adv76xx_state *state = to_state(sd);
894 const struct adv76xx_chip_info *info = state->info; 910 const struct adv76xx_chip_info *info = state->info;
911 u16 cable_det = info->read_cable_det(sd);
895 912
896 return v4l2_ctrl_s_ctrl(state->detect_tx_5v_ctrl, 913 return v4l2_ctrl_s_ctrl(state->detect_tx_5v_ctrl, cable_det);
897 info->read_cable_det(sd));
898} 914}
899 915
900static int find_and_set_predefined_video_timings(struct v4l2_subdev *sd, 916static int find_and_set_predefined_video_timings(struct v4l2_subdev *sd,
@@ -1086,6 +1102,10 @@ static void set_rgb_quantization_range(struct v4l2_subdev *sd)
1086 struct adv76xx_state *state = to_state(sd); 1102 struct adv76xx_state *state = to_state(sd);
1087 bool rgb_output = io_read(sd, 0x02) & 0x02; 1103 bool rgb_output = io_read(sd, 0x02) & 0x02;
1088 bool hdmi_signal = hdmi_read(sd, 0x05) & 0x80; 1104 bool hdmi_signal = hdmi_read(sd, 0x05) & 0x80;
1105 u8 y = HDMI_COLORSPACE_RGB;
1106
1107 if (hdmi_signal && (io_read(sd, 0x60) & 1))
1108 y = infoframe_read(sd, 0x01) >> 5;
1089 1109
1090 v4l2_dbg(2, debug, sd, "%s: RGB quantization range: %d, RGB out: %d, HDMI: %d\n", 1110 v4l2_dbg(2, debug, sd, "%s: RGB quantization range: %d, RGB out: %d, HDMI: %d\n",
1091 __func__, state->rgb_quantization_range, 1111 __func__, state->rgb_quantization_range,
@@ -1093,6 +1113,7 @@ static void set_rgb_quantization_range(struct v4l2_subdev *sd)
1093 1113
1094 adv76xx_set_gain(sd, true, 0x0, 0x0, 0x0); 1114 adv76xx_set_gain(sd, true, 0x0, 0x0, 0x0);
1095 adv76xx_set_offset(sd, true, 0x0, 0x0, 0x0); 1115 adv76xx_set_offset(sd, true, 0x0, 0x0, 0x0);
1116 io_write_clr_set(sd, 0x02, 0x04, rgb_output ? 0 : 4);
1096 1117
1097 switch (state->rgb_quantization_range) { 1118 switch (state->rgb_quantization_range) {
1098 case V4L2_DV_RGB_RANGE_AUTO: 1119 case V4L2_DV_RGB_RANGE_AUTO:
@@ -1142,6 +1163,9 @@ static void set_rgb_quantization_range(struct v4l2_subdev *sd)
1142 break; 1163 break;
1143 } 1164 }
1144 1165
1166 if (y != HDMI_COLORSPACE_RGB)
1167 break;
1168
1145 /* RGB limited range (16-235) */ 1169 /* RGB limited range (16-235) */
1146 io_write_clr_set(sd, 0x02, 0xf0, 0x00); 1170 io_write_clr_set(sd, 0x02, 0xf0, 0x00);
1147 1171
@@ -1153,6 +1177,9 @@ static void set_rgb_quantization_range(struct v4l2_subdev *sd)
1153 break; 1177 break;
1154 } 1178 }
1155 1179
1180 if (y != HDMI_COLORSPACE_RGB)
1181 break;
1182
1156 /* RGB full range (0-255) */ 1183 /* RGB full range (0-255) */
1157 io_write_clr_set(sd, 0x02, 0xf0, 0x10); 1184 io_write_clr_set(sd, 0x02, 0xf0, 0x10);
1158 1185
@@ -1849,6 +1876,7 @@ static void adv76xx_setup_format(struct adv76xx_state *state)
1849 io_write_clr_set(sd, 0x04, 0xe0, adv76xx_op_ch_sel(state)); 1876 io_write_clr_set(sd, 0x04, 0xe0, adv76xx_op_ch_sel(state));
1850 io_write_clr_set(sd, 0x05, 0x01, 1877 io_write_clr_set(sd, 0x05, 0x01,
1851 state->format->swap_cb_cr ? ADV76XX_OP_SWAP_CB_CR : 0); 1878 state->format->swap_cb_cr ? ADV76XX_OP_SWAP_CB_CR : 0);
1879 set_rgb_quantization_range(sd);
1852} 1880}
1853 1881
1854static int adv76xx_get_format(struct v4l2_subdev *sd, 1882static int adv76xx_get_format(struct v4l2_subdev *sd,
@@ -1924,6 +1952,210 @@ static int adv76xx_set_format(struct v4l2_subdev *sd,
1924 return 0; 1952 return 0;
1925} 1953}
1926 1954
1955#if IS_ENABLED(CONFIG_VIDEO_ADV7604_CEC)
1956static void adv76xx_cec_tx_raw_status(struct v4l2_subdev *sd, u8 tx_raw_status)
1957{
1958 struct adv76xx_state *state = to_state(sd);
1959
1960 if ((cec_read(sd, 0x11) & 0x01) == 0) {
1961 v4l2_dbg(1, debug, sd, "%s: tx raw: tx disabled\n", __func__);
1962 return;
1963 }
1964
1965 if (tx_raw_status & 0x02) {
1966 v4l2_dbg(1, debug, sd, "%s: tx raw: arbitration lost\n",
1967 __func__);
1968 cec_transmit_done(state->cec_adap, CEC_TX_STATUS_ARB_LOST,
1969 1, 0, 0, 0);
1970 }
1971 if (tx_raw_status & 0x04) {
1972 u8 status;
1973 u8 nack_cnt;
1974 u8 low_drive_cnt;
1975
1976 v4l2_dbg(1, debug, sd, "%s: tx raw: retry failed\n", __func__);
1977 /*
1978 * We set this status bit since this hardware performs
1979 * retransmissions.
1980 */
1981 status = CEC_TX_STATUS_MAX_RETRIES;
1982 nack_cnt = cec_read(sd, 0x14) & 0xf;
1983 if (nack_cnt)
1984 status |= CEC_TX_STATUS_NACK;
1985 low_drive_cnt = cec_read(sd, 0x14) >> 4;
1986 if (low_drive_cnt)
1987 status |= CEC_TX_STATUS_LOW_DRIVE;
1988 cec_transmit_done(state->cec_adap, status,
1989 0, nack_cnt, low_drive_cnt, 0);
1990 return;
1991 }
1992 if (tx_raw_status & 0x01) {
1993 v4l2_dbg(1, debug, sd, "%s: tx raw: ready ok\n", __func__);
1994 cec_transmit_done(state->cec_adap, CEC_TX_STATUS_OK, 0, 0, 0, 0);
1995 return;
1996 }
1997}
1998
1999static void adv76xx_cec_isr(struct v4l2_subdev *sd, bool *handled)
2000{
2001 struct adv76xx_state *state = to_state(sd);
2002 u8 cec_irq;
2003
2004 /* cec controller */
2005 cec_irq = io_read(sd, 0x4d) & 0x0f;
2006 if (!cec_irq)
2007 return;
2008
2009 v4l2_dbg(1, debug, sd, "%s: cec: irq 0x%x\n", __func__, cec_irq);
2010 adv76xx_cec_tx_raw_status(sd, cec_irq);
2011 if (cec_irq & 0x08) {
2012 struct cec_msg msg;
2013
2014 msg.len = cec_read(sd, 0x25) & 0x1f;
2015 if (msg.len > 16)
2016 msg.len = 16;
2017
2018 if (msg.len) {
2019 u8 i;
2020
2021 for (i = 0; i < msg.len; i++)
2022 msg.msg[i] = cec_read(sd, i + 0x15);
2023 cec_write(sd, 0x26, 0x01); /* re-enable rx */
2024 cec_received_msg(state->cec_adap, &msg);
2025 }
2026 }
2027
2028 /* note: the bit order is swapped between 0x4d and 0x4e */
2029 cec_irq = ((cec_irq & 0x08) >> 3) | ((cec_irq & 0x04) >> 1) |
2030 ((cec_irq & 0x02) << 1) | ((cec_irq & 0x01) << 3);
2031 io_write(sd, 0x4e, cec_irq);
2032
2033 if (handled)
2034 *handled = true;
2035}
2036
2037static int adv76xx_cec_adap_enable(struct cec_adapter *adap, bool enable)
2038{
2039 struct adv76xx_state *state = adap->priv;
2040 struct v4l2_subdev *sd = &state->sd;
2041
2042 if (!state->cec_enabled_adap && enable) {
2043 cec_write_clr_set(sd, 0x2a, 0x01, 0x01); /* power up cec */
2044 cec_write(sd, 0x2c, 0x01); /* cec soft reset */
2045 cec_write_clr_set(sd, 0x11, 0x01, 0); /* initially disable tx */
2046 /* enabled irqs: */
2047 /* tx: ready */
2048 /* tx: arbitration lost */
2049 /* tx: retry timeout */
2050 /* rx: ready */
2051 io_write_clr_set(sd, 0x50, 0x0f, 0x0f);
2052 cec_write(sd, 0x26, 0x01); /* enable rx */
2053 } else if (state->cec_enabled_adap && !enable) {
2054 /* disable cec interrupts */
2055 io_write_clr_set(sd, 0x50, 0x0f, 0x00);
2056 /* disable address mask 1-3 */
2057 cec_write_clr_set(sd, 0x27, 0x70, 0x00);
2058 /* power down cec section */
2059 cec_write_clr_set(sd, 0x2a, 0x01, 0x00);
2060 state->cec_valid_addrs = 0;
2061 }
2062 state->cec_enabled_adap = enable;
2063 adv76xx_s_detect_tx_5v_ctrl(sd);
2064 return 0;
2065}
2066
2067static int adv76xx_cec_adap_log_addr(struct cec_adapter *adap, u8 addr)
2068{
2069 struct adv76xx_state *state = adap->priv;
2070 struct v4l2_subdev *sd = &state->sd;
2071 unsigned int i, free_idx = ADV76XX_MAX_ADDRS;
2072
2073 if (!state->cec_enabled_adap)
2074 return addr == CEC_LOG_ADDR_INVALID ? 0 : -EIO;
2075
2076 if (addr == CEC_LOG_ADDR_INVALID) {
2077 cec_write_clr_set(sd, 0x27, 0x70, 0);
2078 state->cec_valid_addrs = 0;
2079 return 0;
2080 }
2081
2082 for (i = 0; i < ADV76XX_MAX_ADDRS; i++) {
2083 bool is_valid = state->cec_valid_addrs & (1 << i);
2084
2085 if (free_idx == ADV76XX_MAX_ADDRS && !is_valid)
2086 free_idx = i;
2087 if (is_valid && state->cec_addr[i] == addr)
2088 return 0;
2089 }
2090 if (i == ADV76XX_MAX_ADDRS) {
2091 i = free_idx;
2092 if (i == ADV76XX_MAX_ADDRS)
2093 return -ENXIO;
2094 }
2095 state->cec_addr[i] = addr;
2096 state->cec_valid_addrs |= 1 << i;
2097
2098 switch (i) {
2099 case 0:
2100 /* enable address mask 0 */
2101 cec_write_clr_set(sd, 0x27, 0x10, 0x10);
2102 /* set address for mask 0 */
2103 cec_write_clr_set(sd, 0x28, 0x0f, addr);
2104 break;
2105 case 1:
2106 /* enable address mask 1 */
2107 cec_write_clr_set(sd, 0x27, 0x20, 0x20);
2108 /* set address for mask 1 */
2109 cec_write_clr_set(sd, 0x28, 0xf0, addr << 4);
2110 break;
2111 case 2:
2112 /* enable address mask 2 */
2113 cec_write_clr_set(sd, 0x27, 0x40, 0x40);
2114 /* set address for mask 1 */
2115 cec_write_clr_set(sd, 0x29, 0x0f, addr);
2116 break;
2117 }
2118 return 0;
2119}
2120
2121static int adv76xx_cec_adap_transmit(struct cec_adapter *adap, u8 attempts,
2122 u32 signal_free_time, struct cec_msg *msg)
2123{
2124 struct adv76xx_state *state = adap->priv;
2125 struct v4l2_subdev *sd = &state->sd;
2126 u8 len = msg->len;
2127 unsigned int i;
2128
2129 /*
2130 * The number of retries is the number of attempts - 1, but retry
2131 * at least once. It's not clear if a value of 0 is allowed, so
2132 * let's do at least one retry.
2133 */
2134 cec_write_clr_set(sd, 0x12, 0x70, max(1, attempts - 1) << 4);
2135
2136 if (len > 16) {
2137 v4l2_err(sd, "%s: len exceeded 16 (%d)\n", __func__, len);
2138 return -EINVAL;
2139 }
2140
2141 /* write data */
2142 for (i = 0; i < len; i++)
2143 cec_write(sd, i, msg->msg[i]);
2144
2145 /* set length (data + header) */
2146 cec_write(sd, 0x10, len);
2147 /* start transmit, enable tx */
2148 cec_write(sd, 0x11, 0x01);
2149 return 0;
2150}
2151
2152static const struct cec_adap_ops adv76xx_cec_adap_ops = {
2153 .adap_enable = adv76xx_cec_adap_enable,
2154 .adap_log_addr = adv76xx_cec_adap_log_addr,
2155 .adap_transmit = adv76xx_cec_adap_transmit,
2156};
2157#endif
2158
1927static int adv76xx_isr(struct v4l2_subdev *sd, u32 status, bool *handled) 2159static int adv76xx_isr(struct v4l2_subdev *sd, u32 status, bool *handled)
1928{ 2160{
1929 struct adv76xx_state *state = to_state(sd); 2161 struct adv76xx_state *state = to_state(sd);
@@ -1969,6 +2201,11 @@ static int adv76xx_isr(struct v4l2_subdev *sd, u32 status, bool *handled)
1969 *handled = true; 2201 *handled = true;
1970 } 2202 }
1971 2203
2204#if IS_ENABLED(CONFIG_VIDEO_ADV7604_CEC)
2205 /* cec */
2206 adv76xx_cec_isr(sd, handled);
2207#endif
2208
1972 /* tx 5v detect */ 2209 /* tx 5v detect */
1973 tx_5v = irq_reg_0x70 & info->cable_det_mask; 2210 tx_5v = irq_reg_0x70 & info->cable_det_mask;
1974 if (tx_5v) { 2211 if (tx_5v) {
@@ -2018,39 +2255,12 @@ static int adv76xx_get_edid(struct v4l2_subdev *sd, struct v4l2_edid *edid)
2018 return 0; 2255 return 0;
2019} 2256}
2020 2257
2021static int get_edid_spa_location(const u8 *edid)
2022{
2023 u8 d;
2024
2025 if ((edid[0x7e] != 1) ||
2026 (edid[0x80] != 0x02) ||
2027 (edid[0x81] != 0x03)) {
2028 return -1;
2029 }
2030
2031 /* search Vendor Specific Data Block (tag 3) */
2032 d = edid[0x82] & 0x7f;
2033 if (d > 4) {
2034 int i = 0x84;
2035 int end = 0x80 + d;
2036
2037 do {
2038 u8 tag = edid[i] >> 5;
2039 u8 len = edid[i] & 0x1f;
2040
2041 if ((tag == 3) && (len >= 5))
2042 return i + 4;
2043 i += len + 1;
2044 } while (i < end);
2045 }
2046 return -1;
2047}
2048
2049static int adv76xx_set_edid(struct v4l2_subdev *sd, struct v4l2_edid *edid) 2258static int adv76xx_set_edid(struct v4l2_subdev *sd, struct v4l2_edid *edid)
2050{ 2259{
2051 struct adv76xx_state *state = to_state(sd); 2260 struct adv76xx_state *state = to_state(sd);
2052 const struct adv76xx_chip_info *info = state->info; 2261 const struct adv76xx_chip_info *info = state->info;
2053 int spa_loc; 2262 unsigned int spa_loc;
2263 u16 pa;
2054 int err; 2264 int err;
2055 int i; 2265 int i;
2056 2266
@@ -2081,6 +2291,10 @@ static int adv76xx_set_edid(struct v4l2_subdev *sd, struct v4l2_edid *edid)
2081 edid->blocks = 2; 2291 edid->blocks = 2;
2082 return -E2BIG; 2292 return -E2BIG;
2083 } 2293 }
2294 pa = cec_get_edid_phys_addr(edid->edid, edid->blocks * 128, &spa_loc);
2295 err = cec_phys_addr_validate(pa, &pa, NULL);
2296 if (err)
2297 return err;
2084 2298
2085 v4l2_dbg(2, debug, sd, "%s: write EDID pad %d, edid.present = 0x%x\n", 2299 v4l2_dbg(2, debug, sd, "%s: write EDID pad %d, edid.present = 0x%x\n",
2086 __func__, edid->pad, state->edid.present); 2300 __func__, edid->pad, state->edid.present);
@@ -2090,9 +2304,12 @@ static int adv76xx_set_edid(struct v4l2_subdev *sd, struct v4l2_edid *edid)
2090 adv76xx_set_hpd(state, 0); 2304 adv76xx_set_hpd(state, 0);
2091 rep_write_clr_set(sd, info->edid_enable_reg, 0x0f, 0x00); 2305 rep_write_clr_set(sd, info->edid_enable_reg, 0x0f, 0x00);
2092 2306
2093 spa_loc = get_edid_spa_location(edid->edid); 2307 /*
2094 if (spa_loc < 0) 2308 * Return an error if no location of the source physical address
2095 spa_loc = 0xc0; /* Default value [REF_02, p. 116] */ 2309 * was found.
2310 */
2311 if (spa_loc == 0)
2312 return -EINVAL;
2096 2313
2097 switch (edid->pad) { 2314 switch (edid->pad) {
2098 case ADV76XX_PAD_HDMI_PORT_A: 2315 case ADV76XX_PAD_HDMI_PORT_A:
@@ -2152,10 +2369,10 @@ static int adv76xx_set_edid(struct v4l2_subdev *sd, struct v4l2_edid *edid)
2152 v4l2_err(sd, "error enabling edid (0x%x)\n", state->edid.present); 2369 v4l2_err(sd, "error enabling edid (0x%x)\n", state->edid.present);
2153 return -EIO; 2370 return -EIO;
2154 } 2371 }
2372 cec_s_phys_addr(state->cec_adap, pa, false);
2155 2373
2156 /* enable hotplug after 100 ms */ 2374 /* enable hotplug after 100 ms */
2157 queue_delayed_work(state->work_queues, 2375 schedule_delayed_work(&state->delayed_work_enable_hotplug, HZ / 10);
2158 &state->delayed_work_enable_hotplug, HZ / 10);
2159 return 0; 2376 return 0;
2160} 2377}
2161 2378
@@ -2276,8 +2493,19 @@ static int adv76xx_log_status(struct v4l2_subdev *sd)
2276 ((edid_enabled & 0x02) ? "Yes" : "No"), 2493 ((edid_enabled & 0x02) ? "Yes" : "No"),
2277 ((edid_enabled & 0x04) ? "Yes" : "No"), 2494 ((edid_enabled & 0x04) ? "Yes" : "No"),
2278 ((edid_enabled & 0x08) ? "Yes" : "No")); 2495 ((edid_enabled & 0x08) ? "Yes" : "No"));
2279 v4l2_info(sd, "CEC: %s\n", !!(cec_read(sd, 0x2a) & 0x01) ? 2496 v4l2_info(sd, "CEC: %s\n", state->cec_enabled_adap ?
2280 "enabled" : "disabled"); 2497 "enabled" : "disabled");
2498 if (state->cec_enabled_adap) {
2499 int i;
2500
2501 for (i = 0; i < ADV76XX_MAX_ADDRS; i++) {
2502 bool is_valid = state->cec_valid_addrs & (1 << i);
2503
2504 if (is_valid)
2505 v4l2_info(sd, "CEC Logical Address: 0x%x\n",
2506 state->cec_addr[i]);
2507 }
2508 }
2281 2509
2282 v4l2_info(sd, "-----Signal status-----\n"); 2510 v4l2_info(sd, "-----Signal status-----\n");
2283 cable_det = info->read_cable_det(sd); 2511 cable_det = info->read_cable_det(sd);
@@ -2323,11 +2551,10 @@ static int adv76xx_log_status(struct v4l2_subdev *sd)
2323 rgb_quantization_range_txt[state->rgb_quantization_range]); 2551 rgb_quantization_range_txt[state->rgb_quantization_range]);
2324 v4l2_info(sd, "Input color space: %s\n", 2552 v4l2_info(sd, "Input color space: %s\n",
2325 input_color_space_txt[reg_io_0x02 >> 4]); 2553 input_color_space_txt[reg_io_0x02 >> 4]);
2326 v4l2_info(sd, "Output color space: %s %s, saturator %s, alt-gamma %s\n", 2554 v4l2_info(sd, "Output color space: %s %s, alt-gamma %s\n",
2327 (reg_io_0x02 & 0x02) ? "RGB" : "YCbCr", 2555 (reg_io_0x02 & 0x02) ? "RGB" : "YCbCr",
2328 (reg_io_0x02 & 0x04) ? "(16-235)" : "(0-255)",
2329 (((reg_io_0x02 >> 2) & 0x01) ^ (reg_io_0x02 & 0x01)) ? 2556 (((reg_io_0x02 >> 2) & 0x01) ^ (reg_io_0x02 & 0x01)) ?
2330 "enabled" : "disabled", 2557 "(16-235)" : "(0-255)",
2331 (reg_io_0x02 & 0x08) ? "enabled" : "disabled"); 2558 (reg_io_0x02 & 0x08) ? "enabled" : "disabled");
2332 v4l2_info(sd, "Color space conversion: %s\n", 2559 v4l2_info(sd, "Color space conversion: %s\n",
2333 csc_coeff_sel_rb[cp_read(sd, info->cp_csc) >> 4]); 2560 csc_coeff_sel_rb[cp_read(sd, info->cp_csc) >> 4]);
@@ -2387,6 +2614,24 @@ static int adv76xx_subscribe_event(struct v4l2_subdev *sd,
2387 } 2614 }
2388} 2615}
2389 2616
2617static int adv76xx_registered(struct v4l2_subdev *sd)
2618{
2619 struct adv76xx_state *state = to_state(sd);
2620 int err;
2621
2622 err = cec_register_adapter(state->cec_adap);
2623 if (err)
2624 cec_delete_adapter(state->cec_adap);
2625 return err;
2626}
2627
2628static void adv76xx_unregistered(struct v4l2_subdev *sd)
2629{
2630 struct adv76xx_state *state = to_state(sd);
2631
2632 cec_unregister_adapter(state->cec_adap);
2633}
2634
2390/* ----------------------------------------------------------------------- */ 2635/* ----------------------------------------------------------------------- */
2391 2636
2392static const struct v4l2_ctrl_ops adv76xx_ctrl_ops = { 2637static const struct v4l2_ctrl_ops adv76xx_ctrl_ops = {
@@ -2430,6 +2675,11 @@ static const struct v4l2_subdev_ops adv76xx_ops = {
2430 .pad = &adv76xx_pad_ops, 2675 .pad = &adv76xx_pad_ops,
2431}; 2676};
2432 2677
2678static const struct v4l2_subdev_internal_ops adv76xx_int_ops = {
2679 .registered = adv76xx_registered,
2680 .unregistered = adv76xx_unregistered,
2681};
2682
2433/* -------------------------- custom ctrls ---------------------------------- */ 2683/* -------------------------- custom ctrls ---------------------------------- */
2434 2684
2435static const struct v4l2_ctrl_config adv7604_ctrl_analog_sampling_phase = { 2685static const struct v4l2_ctrl_config adv7604_ctrl_analog_sampling_phase = {
@@ -2492,10 +2742,7 @@ static int adv76xx_core_init(struct v4l2_subdev *sd)
2492 cp_write(sd, 0xcf, 0x01); /* Power down macrovision */ 2742 cp_write(sd, 0xcf, 0x01); /* Power down macrovision */
2493 2743
2494 /* video format */ 2744 /* video format */
2495 io_write_clr_set(sd, 0x02, 0x0f, 2745 io_write_clr_set(sd, 0x02, 0x0f, pdata->alt_gamma << 3);
2496 pdata->alt_gamma << 3 |
2497 pdata->op_656_range << 2 |
2498 pdata->alt_data_sat << 0);
2499 io_write_clr_set(sd, 0x05, 0x0e, pdata->blank_data << 3 | 2746 io_write_clr_set(sd, 0x05, 0x0e, pdata->blank_data << 3 |
2500 pdata->insert_av_codes << 2 | 2747 pdata->insert_av_codes << 2 |
2501 pdata->replicate_av_codes << 1); 2748 pdata->replicate_av_codes << 1);
@@ -2845,10 +3092,8 @@ static int adv76xx_parse_dt(struct adv76xx_state *state)
2845 if (flags & V4L2_MBUS_PCLK_SAMPLE_RISING) 3092 if (flags & V4L2_MBUS_PCLK_SAMPLE_RISING)
2846 state->pdata.inv_llc_pol = 1; 3093 state->pdata.inv_llc_pol = 1;
2847 3094
2848 if (bus_cfg.bus_type == V4L2_MBUS_BT656) { 3095 if (bus_cfg.bus_type == V4L2_MBUS_BT656)
2849 state->pdata.insert_av_codes = 1; 3096 state->pdata.insert_av_codes = 1;
2850 state->pdata.op_656_range = 1;
2851 }
2852 3097
2853 /* Disable the interrupt for now as no DT-based board uses it. */ 3098 /* Disable the interrupt for now as no DT-based board uses it. */
2854 state->pdata.int1_config = ADV76XX_INT1_CONFIG_DISABLED; 3099 state->pdata.int1_config = ADV76XX_INT1_CONFIG_DISABLED;
@@ -2871,7 +3116,6 @@ static int adv76xx_parse_dt(struct adv76xx_state *state)
2871 state->pdata.disable_pwrdnb = 0; 3116 state->pdata.disable_pwrdnb = 0;
2872 state->pdata.disable_cable_det_rst = 0; 3117 state->pdata.disable_cable_det_rst = 0;
2873 state->pdata.blank_data = 1; 3118 state->pdata.blank_data = 1;
2874 state->pdata.alt_data_sat = 1;
2875 state->pdata.op_format_mode_sel = ADV7604_OP_FORMAT_MODE0; 3119 state->pdata.op_format_mode_sel = ADV7604_OP_FORMAT_MODE0;
2876 state->pdata.bus_order = ADV7604_BUS_ORDER_RGB; 3120 state->pdata.bus_order = ADV7604_BUS_ORDER_RGB;
2877 3121
@@ -3020,6 +3264,19 @@ static int configure_regmaps(struct adv76xx_state *state)
3020 return 0; 3264 return 0;
3021} 3265}
3022 3266
3267static void adv76xx_reset(struct adv76xx_state *state)
3268{
3269 if (state->reset_gpio) {
3270 /* ADV76XX can be reset by a low reset pulse of minimum 5 ms. */
3271 gpiod_set_value_cansleep(state->reset_gpio, 0);
3272 usleep_range(5000, 10000);
3273 gpiod_set_value_cansleep(state->reset_gpio, 1);
3274 /* It is recommended to wait 5 ms after the low pulse before */
3275 /* an I2C write is performed to the ADV76XX. */
3276 usleep_range(5000, 10000);
3277 }
3278}
3279
3023static int adv76xx_probe(struct i2c_client *client, 3280static int adv76xx_probe(struct i2c_client *client,
3024 const struct i2c_device_id *id) 3281 const struct i2c_device_id *id)
3025{ 3282{
@@ -3083,6 +3340,12 @@ static int adv76xx_probe(struct i2c_client *client,
3083 if (state->hpd_gpio[i]) 3340 if (state->hpd_gpio[i])
3084 v4l_info(client, "Handling HPD %u GPIO\n", i); 3341 v4l_info(client, "Handling HPD %u GPIO\n", i);
3085 } 3342 }
3343 state->reset_gpio = devm_gpiod_get_optional(&client->dev, "reset",
3344 GPIOD_OUT_HIGH);
3345 if (IS_ERR(state->reset_gpio))
3346 return PTR_ERR(state->reset_gpio);
3347
3348 adv76xx_reset(state);
3086 3349
3087 state->timings = cea640x480; 3350 state->timings = cea640x480;
3088 state->format = adv76xx_format_info(state, MEDIA_BUS_FMT_YUYV8_2X8); 3351 state->format = adv76xx_format_info(state, MEDIA_BUS_FMT_YUYV8_2X8);
@@ -3093,6 +3356,7 @@ static int adv76xx_probe(struct i2c_client *client,
3093 id->name, i2c_adapter_id(client->adapter), 3356 id->name, i2c_adapter_id(client->adapter),
3094 client->addr); 3357 client->addr);
3095 sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE | V4L2_SUBDEV_FL_HAS_EVENTS; 3358 sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE | V4L2_SUBDEV_FL_HAS_EVENTS;
3359 sd->internal_ops = &adv76xx_int_ops;
3096 3360
3097 /* Configure IO Regmap region */ 3361 /* Configure IO Regmap region */
3098 err = configure_regmap(state, ADV76XX_PAGE_IO); 3362 err = configure_regmap(state, ADV76XX_PAGE_IO);
@@ -3206,14 +3470,6 @@ static int adv76xx_probe(struct i2c_client *client,
3206 } 3470 }
3207 } 3471 }
3208 3472
3209 /* work queues */
3210 state->work_queues = create_singlethread_workqueue(client->name);
3211 if (!state->work_queues) {
3212 v4l2_err(sd, "Could not create work queue\n");
3213 err = -ENOMEM;
3214 goto err_i2c;
3215 }
3216
3217 INIT_DELAYED_WORK(&state->delayed_work_enable_hotplug, 3473 INIT_DELAYED_WORK(&state->delayed_work_enable_hotplug,
3218 adv76xx_delayed_work_enable_hotplug); 3474 adv76xx_delayed_work_enable_hotplug);
3219 3475
@@ -3236,6 +3492,18 @@ static int adv76xx_probe(struct i2c_client *client,
3236 err = adv76xx_core_init(sd); 3492 err = adv76xx_core_init(sd);
3237 if (err) 3493 if (err)
3238 goto err_entity; 3494 goto err_entity;
3495
3496#if IS_ENABLED(CONFIG_VIDEO_ADV7604_CEC)
3497 state->cec_adap = cec_allocate_adapter(&adv76xx_cec_adap_ops,
3498 state, dev_name(&client->dev),
3499 CEC_CAP_TRANSMIT | CEC_CAP_LOG_ADDRS |
3500 CEC_CAP_PASSTHROUGH | CEC_CAP_RC, ADV76XX_MAX_ADDRS,
3501 &client->dev);
3502 err = PTR_ERR_OR_ZERO(state->cec_adap);
3503 if (err)
3504 goto err_entity;
3505#endif
3506
3239 v4l2_info(sd, "%s found @ 0x%x (%s)\n", client->name, 3507 v4l2_info(sd, "%s found @ 0x%x (%s)\n", client->name,
3240 client->addr << 1, client->adapter->name); 3508 client->addr << 1, client->adapter->name);
3241 3509
@@ -3249,7 +3517,6 @@ err_entity:
3249 media_entity_cleanup(&sd->entity); 3517 media_entity_cleanup(&sd->entity);
3250err_work_queues: 3518err_work_queues:
3251 cancel_delayed_work(&state->delayed_work_enable_hotplug); 3519 cancel_delayed_work(&state->delayed_work_enable_hotplug);
3252 destroy_workqueue(state->work_queues);
3253err_i2c: 3520err_i2c:
3254 adv76xx_unregister_clients(state); 3521 adv76xx_unregister_clients(state);
3255err_hdl: 3522err_hdl:
@@ -3264,8 +3531,14 @@ static int adv76xx_remove(struct i2c_client *client)
3264 struct v4l2_subdev *sd = i2c_get_clientdata(client); 3531 struct v4l2_subdev *sd = i2c_get_clientdata(client);
3265 struct adv76xx_state *state = to_state(sd); 3532 struct adv76xx_state *state = to_state(sd);
3266 3533
3534 /* disable interrupts */
3535 io_write(sd, 0x40, 0);
3536 io_write(sd, 0x41, 0);
3537 io_write(sd, 0x46, 0);
3538 io_write(sd, 0x6e, 0);
3539 io_write(sd, 0x73, 0);
3540
3267 cancel_delayed_work(&state->delayed_work_enable_hotplug); 3541 cancel_delayed_work(&state->delayed_work_enable_hotplug);
3268 destroy_workqueue(state->work_queues);
3269 v4l2_async_unregister_subdev(sd); 3542 v4l2_async_unregister_subdev(sd);
3270 media_entity_cleanup(&sd->entity); 3543 media_entity_cleanup(&sd->entity);
3271 adv76xx_unregister_clients(to_state(sd)); 3544 adv76xx_unregister_clients(to_state(sd));
diff --git a/drivers/media/i2c/adv7842.c b/drivers/media/i2c/adv7842.c
index ecaacb0a6fa1..8c2a52e280af 100644
--- a/drivers/media/i2c/adv7842.c
+++ b/drivers/media/i2c/adv7842.c
@@ -39,6 +39,7 @@
39#include <linux/workqueue.h> 39#include <linux/workqueue.h>
40#include <linux/v4l2-dv-timings.h> 40#include <linux/v4l2-dv-timings.h>
41#include <linux/hdmi.h> 41#include <linux/hdmi.h>
42#include <media/cec.h>
42#include <media/v4l2-device.h> 43#include <media/v4l2-device.h>
43#include <media/v4l2-event.h> 44#include <media/v4l2-event.h>
44#include <media/v4l2-ctrls.h> 45#include <media/v4l2-ctrls.h>
@@ -79,6 +80,8 @@ MODULE_LICENSE("GPL");
79 80
80#define ADV7842_OP_SWAP_CB_CR (1 << 0) 81#define ADV7842_OP_SWAP_CB_CR (1 << 0)
81 82
83#define ADV7842_MAX_ADDRS (3)
84
82/* 85/*
83********************************************************************** 86**********************************************************************
84* 87*
@@ -118,7 +121,6 @@ struct adv7842_state {
118 struct v4l2_fract aspect_ratio; 121 struct v4l2_fract aspect_ratio;
119 u32 rgb_quantization_range; 122 u32 rgb_quantization_range;
120 bool is_cea_format; 123 bool is_cea_format;
121 struct workqueue_struct *work_queues;
122 struct delayed_work delayed_work_enable_hotplug; 124 struct delayed_work delayed_work_enable_hotplug;
123 bool restart_stdi_once; 125 bool restart_stdi_once;
124 bool hdmi_port_a; 126 bool hdmi_port_a;
@@ -142,6 +144,11 @@ struct adv7842_state {
142 struct v4l2_ctrl *free_run_color_ctrl_manual; 144 struct v4l2_ctrl *free_run_color_ctrl_manual;
143 struct v4l2_ctrl *free_run_color_ctrl; 145 struct v4l2_ctrl *free_run_color_ctrl;
144 struct v4l2_ctrl *rgb_quantization_range_ctrl; 146 struct v4l2_ctrl *rgb_quantization_range_ctrl;
147
148 struct cec_adapter *cec_adap;
149 u8 cec_addr[ADV7842_MAX_ADDRS];
150 u8 cec_valid_addrs;
151 bool cec_enabled_adap;
145}; 152};
146 153
147/* Unsupported timings. This device cannot support 720p30. */ 154/* Unsupported timings. This device cannot support 720p30. */
@@ -418,9 +425,9 @@ static inline int cec_write(struct v4l2_subdev *sd, u8 reg, u8 val)
418 return adv_smbus_write_byte_data(state->i2c_cec, reg, val); 425 return adv_smbus_write_byte_data(state->i2c_cec, reg, val);
419} 426}
420 427
421static inline int cec_write_and_or(struct v4l2_subdev *sd, u8 reg, u8 mask, u8 val) 428static inline int cec_write_clr_set(struct v4l2_subdev *sd, u8 reg, u8 mask, u8 val)
422{ 429{
423 return cec_write(sd, reg, (cec_read(sd, reg) & mask) | val); 430 return cec_write(sd, reg, (cec_read(sd, reg) & ~mask) | val);
424} 431}
425 432
426static inline int infoframe_read(struct v4l2_subdev *sd, u8 reg) 433static inline int infoframe_read(struct v4l2_subdev *sd, u8 reg)
@@ -696,6 +703,18 @@ adv7842_get_dv_timings_cap(struct v4l2_subdev *sd)
696 703
697/* ----------------------------------------------------------------------- */ 704/* ----------------------------------------------------------------------- */
698 705
706static u16 adv7842_read_cable_det(struct v4l2_subdev *sd)
707{
708 u8 reg = io_read(sd, 0x6f);
709 u16 val = 0;
710
711 if (reg & 0x02)
712 val |= 1; /* port A */
713 if (reg & 0x01)
714 val |= 2; /* port B */
715 return val;
716}
717
699static void adv7842_delayed_work_enable_hotplug(struct work_struct *work) 718static void adv7842_delayed_work_enable_hotplug(struct work_struct *work)
700{ 719{
701 struct delayed_work *dwork = to_delayed_work(work); 720 struct delayed_work *dwork = to_delayed_work(work);
@@ -756,56 +775,23 @@ static int edid_write_vga_segment(struct v4l2_subdev *sd)
756 } 775 }
757 776
758 /* enable hotplug after 200 ms */ 777 /* enable hotplug after 200 ms */
759 queue_delayed_work(state->work_queues, 778 schedule_delayed_work(&state->delayed_work_enable_hotplug, HZ / 5);
760 &state->delayed_work_enable_hotplug, HZ / 5);
761 779
762 return 0; 780 return 0;
763} 781}
764 782
765static int edid_spa_location(const u8 *edid)
766{
767 u8 d;
768
769 /*
770 * TODO, improve and update for other CEA extensions
771 * currently only for 1 segment (256 bytes),
772 * i.e. 1 extension block and CEA revision 3.
773 */
774 if ((edid[0x7e] != 1) ||
775 (edid[0x80] != 0x02) ||
776 (edid[0x81] != 0x03)) {
777 return -EINVAL;
778 }
779 /*
780 * search Vendor Specific Data Block (tag 3)
781 */
782 d = edid[0x82] & 0x7f;
783 if (d > 4) {
784 int i = 0x84;
785 int end = 0x80 + d;
786 do {
787 u8 tag = edid[i]>>5;
788 u8 len = edid[i] & 0x1f;
789
790 if ((tag == 3) && (len >= 5))
791 return i + 4;
792 i += len + 1;
793 } while (i < end);
794 }
795 return -EINVAL;
796}
797
798static int edid_write_hdmi_segment(struct v4l2_subdev *sd, u8 port) 783static int edid_write_hdmi_segment(struct v4l2_subdev *sd, u8 port)
799{ 784{
800 struct i2c_client *client = v4l2_get_subdevdata(sd); 785 struct i2c_client *client = v4l2_get_subdevdata(sd);
801 struct adv7842_state *state = to_state(sd); 786 struct adv7842_state *state = to_state(sd);
802 const u8 *val = state->hdmi_edid.edid; 787 const u8 *edid = state->hdmi_edid.edid;
803 int spa_loc = edid_spa_location(val); 788 int spa_loc;
789 u16 pa;
804 int err = 0; 790 int err = 0;
805 int i; 791 int i;
806 792
807 v4l2_dbg(2, debug, sd, "%s: write EDID on port %c (spa at 0x%x)\n", 793 v4l2_dbg(2, debug, sd, "%s: write EDID on port %c\n",
808 __func__, (port == ADV7842_EDID_PORT_A) ? 'A' : 'B', spa_loc); 794 __func__, (port == ADV7842_EDID_PORT_A) ? 'A' : 'B');
809 795
810 /* HPA disable on port A and B */ 796 /* HPA disable on port A and B */
811 io_write_and_or(sd, 0x20, 0xcf, 0x00); 797 io_write_and_or(sd, 0x20, 0xcf, 0x00);
@@ -816,24 +802,33 @@ static int edid_write_hdmi_segment(struct v4l2_subdev *sd, u8 port)
816 if (!state->hdmi_edid.present) 802 if (!state->hdmi_edid.present)
817 return 0; 803 return 0;
818 804
805 pa = cec_get_edid_phys_addr(edid, 256, &spa_loc);
806 err = cec_phys_addr_validate(pa, &pa, NULL);
807 if (err)
808 return err;
809
810 /*
811 * Return an error if no location of the source physical address
812 * was found.
813 */
814 if (spa_loc == 0)
815 return -EINVAL;
816
819 /* edid segment pointer '0' for HDMI ports */ 817 /* edid segment pointer '0' for HDMI ports */
820 rep_write_and_or(sd, 0x77, 0xef, 0x00); 818 rep_write_and_or(sd, 0x77, 0xef, 0x00);
821 819
822 for (i = 0; !err && i < 256; i += I2C_SMBUS_BLOCK_MAX) 820 for (i = 0; !err && i < 256; i += I2C_SMBUS_BLOCK_MAX)
823 err = adv_smbus_write_i2c_block_data(state->i2c_edid, i, 821 err = adv_smbus_write_i2c_block_data(state->i2c_edid, i,
824 I2C_SMBUS_BLOCK_MAX, val + i); 822 I2C_SMBUS_BLOCK_MAX, edid + i);
825 if (err) 823 if (err)
826 return err; 824 return err;
827 825
828 if (spa_loc < 0)
829 spa_loc = 0xc0; /* Default value [REF_02, p. 199] */
830
831 if (port == ADV7842_EDID_PORT_A) { 826 if (port == ADV7842_EDID_PORT_A) {
832 rep_write(sd, 0x72, val[spa_loc]); 827 rep_write(sd, 0x72, edid[spa_loc]);
833 rep_write(sd, 0x73, val[spa_loc + 1]); 828 rep_write(sd, 0x73, edid[spa_loc + 1]);
834 } else { 829 } else {
835 rep_write(sd, 0x74, val[spa_loc]); 830 rep_write(sd, 0x74, edid[spa_loc]);
836 rep_write(sd, 0x75, val[spa_loc + 1]); 831 rep_write(sd, 0x75, edid[spa_loc + 1]);
837 } 832 }
838 rep_write(sd, 0x76, spa_loc & 0xff); 833 rep_write(sd, 0x76, spa_loc & 0xff);
839 rep_write_and_or(sd, 0x77, 0xbf, (spa_loc >> 2) & 0x40); 834 rep_write_and_or(sd, 0x77, 0xbf, (spa_loc >> 2) & 0x40);
@@ -853,10 +848,10 @@ static int edid_write_hdmi_segment(struct v4l2_subdev *sd, u8 port)
853 (port == ADV7842_EDID_PORT_A) ? 'A' : 'B'); 848 (port == ADV7842_EDID_PORT_A) ? 'A' : 'B');
854 return -EIO; 849 return -EIO;
855 } 850 }
851 cec_s_phys_addr(state->cec_adap, pa, false);
856 852
857 /* enable hotplug after 200 ms */ 853 /* enable hotplug after 200 ms */
858 queue_delayed_work(state->work_queues, 854 schedule_delayed_work(&state->delayed_work_enable_hotplug, HZ / 5);
859 &state->delayed_work_enable_hotplug, HZ / 5);
860 855
861 return 0; 856 return 0;
862} 857}
@@ -983,20 +978,11 @@ static int adv7842_s_register(struct v4l2_subdev *sd,
983static int adv7842_s_detect_tx_5v_ctrl(struct v4l2_subdev *sd) 978static int adv7842_s_detect_tx_5v_ctrl(struct v4l2_subdev *sd)
984{ 979{
985 struct adv7842_state *state = to_state(sd); 980 struct adv7842_state *state = to_state(sd);
986 int prev = v4l2_ctrl_g_ctrl(state->detect_tx_5v_ctrl); 981 u16 cable_det = adv7842_read_cable_det(sd);
987 u8 reg_io_6f = io_read(sd, 0x6f);
988 int val = 0;
989 982
990 if (reg_io_6f & 0x02) 983 v4l2_dbg(1, debug, sd, "%s: 0x%x\n", __func__, cable_det);
991 val |= 1; /* port A */
992 if (reg_io_6f & 0x01)
993 val |= 2; /* port B */
994
995 v4l2_dbg(1, debug, sd, "%s: 0x%x -> 0x%x\n", __func__, prev, val);
996 984
997 if (val != prev) 985 return v4l2_ctrl_s_ctrl(state->detect_tx_5v_ctrl, cable_det);
998 return v4l2_ctrl_s_ctrl(state->detect_tx_5v_ctrl, val);
999 return 0;
1000} 986}
1001 987
1002static int find_and_set_predefined_video_timings(struct v4l2_subdev *sd, 988static int find_and_set_predefined_video_timings(struct v4l2_subdev *sd,
@@ -1198,6 +1184,10 @@ static void set_rgb_quantization_range(struct v4l2_subdev *sd)
1198 struct adv7842_state *state = to_state(sd); 1184 struct adv7842_state *state = to_state(sd);
1199 bool rgb_output = io_read(sd, 0x02) & 0x02; 1185 bool rgb_output = io_read(sd, 0x02) & 0x02;
1200 bool hdmi_signal = hdmi_read(sd, 0x05) & 0x80; 1186 bool hdmi_signal = hdmi_read(sd, 0x05) & 0x80;
1187 u8 y = HDMI_COLORSPACE_RGB;
1188
1189 if (hdmi_signal && (io_read(sd, 0x60) & 1))
1190 y = infoframe_read(sd, 0x01) >> 5;
1201 1191
1202 v4l2_dbg(2, debug, sd, "%s: RGB quantization range: %d, RGB out: %d, HDMI: %d\n", 1192 v4l2_dbg(2, debug, sd, "%s: RGB quantization range: %d, RGB out: %d, HDMI: %d\n",
1203 __func__, state->rgb_quantization_range, 1193 __func__, state->rgb_quantization_range,
@@ -1205,6 +1195,7 @@ static void set_rgb_quantization_range(struct v4l2_subdev *sd)
1205 1195
1206 adv7842_set_gain(sd, true, 0x0, 0x0, 0x0); 1196 adv7842_set_gain(sd, true, 0x0, 0x0, 0x0);
1207 adv7842_set_offset(sd, true, 0x0, 0x0, 0x0); 1197 adv7842_set_offset(sd, true, 0x0, 0x0, 0x0);
1198 io_write_clr_set(sd, 0x02, 0x04, rgb_output ? 0 : 4);
1208 1199
1209 switch (state->rgb_quantization_range) { 1200 switch (state->rgb_quantization_range) {
1210 case V4L2_DV_RGB_RANGE_AUTO: 1201 case V4L2_DV_RGB_RANGE_AUTO:
@@ -1254,6 +1245,9 @@ static void set_rgb_quantization_range(struct v4l2_subdev *sd)
1254 break; 1245 break;
1255 } 1246 }
1256 1247
1248 if (y != HDMI_COLORSPACE_RGB)
1249 break;
1250
1257 /* RGB limited range (16-235) */ 1251 /* RGB limited range (16-235) */
1258 io_write_and_or(sd, 0x02, 0x0f, 0x00); 1252 io_write_and_or(sd, 0x02, 0x0f, 0x00);
1259 1253
@@ -1265,6 +1259,9 @@ static void set_rgb_quantization_range(struct v4l2_subdev *sd)
1265 break; 1259 break;
1266 } 1260 }
1267 1261
1262 if (y != HDMI_COLORSPACE_RGB)
1263 break;
1264
1268 /* RGB full range (0-255) */ 1265 /* RGB full range (0-255) */
1269 io_write_and_or(sd, 0x02, 0x0f, 0x10); 1266 io_write_and_or(sd, 0x02, 0x0f, 0x10);
1270 1267
@@ -2072,6 +2069,7 @@ static void adv7842_setup_format(struct adv7842_state *state)
2072 io_write_clr_set(sd, 0x04, 0xe0, adv7842_op_ch_sel(state)); 2069 io_write_clr_set(sd, 0x04, 0xe0, adv7842_op_ch_sel(state));
2073 io_write_clr_set(sd, 0x05, 0x01, 2070 io_write_clr_set(sd, 0x05, 0x01,
2074 state->format->swap_cb_cr ? ADV7842_OP_SWAP_CB_CR : 0); 2071 state->format->swap_cb_cr ? ADV7842_OP_SWAP_CB_CR : 0);
2072 set_rgb_quantization_range(sd);
2075} 2073}
2076 2074
2077static int adv7842_get_format(struct v4l2_subdev *sd, 2075static int adv7842_get_format(struct v4l2_subdev *sd,
@@ -2170,6 +2168,207 @@ static void adv7842_irq_enable(struct v4l2_subdev *sd, bool enable)
2170 } 2168 }
2171} 2169}
2172 2170
2171#if IS_ENABLED(CONFIG_VIDEO_ADV7842_CEC)
2172static void adv7842_cec_tx_raw_status(struct v4l2_subdev *sd, u8 tx_raw_status)
2173{
2174 struct adv7842_state *state = to_state(sd);
2175
2176 if ((cec_read(sd, 0x11) & 0x01) == 0) {
2177 v4l2_dbg(1, debug, sd, "%s: tx raw: tx disabled\n", __func__);
2178 return;
2179 }
2180
2181 if (tx_raw_status & 0x02) {
2182 v4l2_dbg(1, debug, sd, "%s: tx raw: arbitration lost\n",
2183 __func__);
2184 cec_transmit_done(state->cec_adap, CEC_TX_STATUS_ARB_LOST,
2185 1, 0, 0, 0);
2186 return;
2187 }
2188 if (tx_raw_status & 0x04) {
2189 u8 status;
2190 u8 nack_cnt;
2191 u8 low_drive_cnt;
2192
2193 v4l2_dbg(1, debug, sd, "%s: tx raw: retry failed\n", __func__);
2194 /*
2195 * We set this status bit since this hardware performs
2196 * retransmissions.
2197 */
2198 status = CEC_TX_STATUS_MAX_RETRIES;
2199 nack_cnt = cec_read(sd, 0x14) & 0xf;
2200 if (nack_cnt)
2201 status |= CEC_TX_STATUS_NACK;
2202 low_drive_cnt = cec_read(sd, 0x14) >> 4;
2203 if (low_drive_cnt)
2204 status |= CEC_TX_STATUS_LOW_DRIVE;
2205 cec_transmit_done(state->cec_adap, status,
2206 0, nack_cnt, low_drive_cnt, 0);
2207 return;
2208 }
2209 if (tx_raw_status & 0x01) {
2210 v4l2_dbg(1, debug, sd, "%s: tx raw: ready ok\n", __func__);
2211 cec_transmit_done(state->cec_adap, CEC_TX_STATUS_OK, 0, 0, 0, 0);
2212 return;
2213 }
2214}
2215
2216static void adv7842_cec_isr(struct v4l2_subdev *sd, bool *handled)
2217{
2218 u8 cec_irq;
2219
2220 /* cec controller */
2221 cec_irq = io_read(sd, 0x93) & 0x0f;
2222 if (!cec_irq)
2223 return;
2224
2225 v4l2_dbg(1, debug, sd, "%s: cec: irq 0x%x\n", __func__, cec_irq);
2226 adv7842_cec_tx_raw_status(sd, cec_irq);
2227 if (cec_irq & 0x08) {
2228 struct adv7842_state *state = to_state(sd);
2229 struct cec_msg msg;
2230
2231 msg.len = cec_read(sd, 0x25) & 0x1f;
2232 if (msg.len > 16)
2233 msg.len = 16;
2234
2235 if (msg.len) {
2236 u8 i;
2237
2238 for (i = 0; i < msg.len; i++)
2239 msg.msg[i] = cec_read(sd, i + 0x15);
2240 cec_write(sd, 0x26, 0x01); /* re-enable rx */
2241 cec_received_msg(state->cec_adap, &msg);
2242 }
2243 }
2244
2245 io_write(sd, 0x94, cec_irq);
2246
2247 if (handled)
2248 *handled = true;
2249}
2250
2251static int adv7842_cec_adap_enable(struct cec_adapter *adap, bool enable)
2252{
2253 struct adv7842_state *state = adap->priv;
2254 struct v4l2_subdev *sd = &state->sd;
2255
2256 if (!state->cec_enabled_adap && enable) {
2257 cec_write_clr_set(sd, 0x2a, 0x01, 0x01); /* power up cec */
2258 cec_write(sd, 0x2c, 0x01); /* cec soft reset */
2259 cec_write_clr_set(sd, 0x11, 0x01, 0); /* initially disable tx */
2260 /* enabled irqs: */
2261 /* tx: ready */
2262 /* tx: arbitration lost */
2263 /* tx: retry timeout */
2264 /* rx: ready */
2265 io_write_clr_set(sd, 0x96, 0x0f, 0x0f);
2266 cec_write(sd, 0x26, 0x01); /* enable rx */
2267 } else if (state->cec_enabled_adap && !enable) {
2268 /* disable cec interrupts */
2269 io_write_clr_set(sd, 0x96, 0x0f, 0x00);
2270 /* disable address mask 1-3 */
2271 cec_write_clr_set(sd, 0x27, 0x70, 0x00);
2272 /* power down cec section */
2273 cec_write_clr_set(sd, 0x2a, 0x01, 0x00);
2274 state->cec_valid_addrs = 0;
2275 }
2276 state->cec_enabled_adap = enable;
2277 return 0;
2278}
2279
2280static int adv7842_cec_adap_log_addr(struct cec_adapter *adap, u8 addr)
2281{
2282 struct adv7842_state *state = adap->priv;
2283 struct v4l2_subdev *sd = &state->sd;
2284 unsigned int i, free_idx = ADV7842_MAX_ADDRS;
2285
2286 if (!state->cec_enabled_adap)
2287 return addr == CEC_LOG_ADDR_INVALID ? 0 : -EIO;
2288
2289 if (addr == CEC_LOG_ADDR_INVALID) {
2290 cec_write_clr_set(sd, 0x27, 0x70, 0);
2291 state->cec_valid_addrs = 0;
2292 return 0;
2293 }
2294
2295 for (i = 0; i < ADV7842_MAX_ADDRS; i++) {
2296 bool is_valid = state->cec_valid_addrs & (1 << i);
2297
2298 if (free_idx == ADV7842_MAX_ADDRS && !is_valid)
2299 free_idx = i;
2300 if (is_valid && state->cec_addr[i] == addr)
2301 return 0;
2302 }
2303 if (i == ADV7842_MAX_ADDRS) {
2304 i = free_idx;
2305 if (i == ADV7842_MAX_ADDRS)
2306 return -ENXIO;
2307 }
2308 state->cec_addr[i] = addr;
2309 state->cec_valid_addrs |= 1 << i;
2310
2311 switch (i) {
2312 case 0:
2313 /* enable address mask 0 */
2314 cec_write_clr_set(sd, 0x27, 0x10, 0x10);
2315 /* set address for mask 0 */
2316 cec_write_clr_set(sd, 0x28, 0x0f, addr);
2317 break;
2318 case 1:
2319 /* enable address mask 1 */
2320 cec_write_clr_set(sd, 0x27, 0x20, 0x20);
2321 /* set address for mask 1 */
2322 cec_write_clr_set(sd, 0x28, 0xf0, addr << 4);
2323 break;
2324 case 2:
2325 /* enable address mask 2 */
2326 cec_write_clr_set(sd, 0x27, 0x40, 0x40);
2327 /* set address for mask 1 */
2328 cec_write_clr_set(sd, 0x29, 0x0f, addr);
2329 break;
2330 }
2331 return 0;
2332}
2333
2334static int adv7842_cec_adap_transmit(struct cec_adapter *adap, u8 attempts,
2335 u32 signal_free_time, struct cec_msg *msg)
2336{
2337 struct adv7842_state *state = adap->priv;
2338 struct v4l2_subdev *sd = &state->sd;
2339 u8 len = msg->len;
2340 unsigned int i;
2341
2342 /*
2343 * The number of retries is the number of attempts - 1, but retry
2344 * at least once. It's not clear if a value of 0 is allowed, so
2345 * let's do at least one retry.
2346 */
2347 cec_write_clr_set(sd, 0x12, 0x70, max(1, attempts - 1) << 4);
2348
2349 if (len > 16) {
2350 v4l2_err(sd, "%s: len exceeded 16 (%d)\n", __func__, len);
2351 return -EINVAL;
2352 }
2353
2354 /* write data */
2355 for (i = 0; i < len; i++)
2356 cec_write(sd, i, msg->msg[i]);
2357
2358 /* set length (data + header) */
2359 cec_write(sd, 0x10, len);
2360 /* start transmit, enable tx */
2361 cec_write(sd, 0x11, 0x01);
2362 return 0;
2363}
2364
2365static const struct cec_adap_ops adv7842_cec_adap_ops = {
2366 .adap_enable = adv7842_cec_adap_enable,
2367 .adap_log_addr = adv7842_cec_adap_log_addr,
2368 .adap_transmit = adv7842_cec_adap_transmit,
2369};
2370#endif
2371
2173static int adv7842_isr(struct v4l2_subdev *sd, u32 status, bool *handled) 2372static int adv7842_isr(struct v4l2_subdev *sd, u32 status, bool *handled)
2174{ 2373{
2175 struct adv7842_state *state = to_state(sd); 2374 struct adv7842_state *state = to_state(sd);
@@ -2241,6 +2440,11 @@ static int adv7842_isr(struct v4l2_subdev *sd, u32 status, bool *handled)
2241 *handled = true; 2440 *handled = true;
2242 } 2441 }
2243 2442
2443#if IS_ENABLED(CONFIG_VIDEO_ADV7842_CEC)
2444 /* cec */
2445 adv7842_cec_isr(sd, handled);
2446#endif
2447
2244 /* tx 5v detect */ 2448 /* tx 5v detect */
2245 if (irq_status[2] & 0x3) { 2449 if (irq_status[2] & 0x3) {
2246 v4l2_dbg(1, debug, sd, "%s: irq tx_5v\n", __func__); 2450 v4l2_dbg(1, debug, sd, "%s: irq tx_5v\n", __func__);
@@ -2321,10 +2525,12 @@ static int adv7842_set_edid(struct v4l2_subdev *sd, struct v4l2_edid *e)
2321 case ADV7842_EDID_PORT_A: 2525 case ADV7842_EDID_PORT_A:
2322 case ADV7842_EDID_PORT_B: 2526 case ADV7842_EDID_PORT_B:
2323 memset(&state->hdmi_edid.edid, 0, 256); 2527 memset(&state->hdmi_edid.edid, 0, 256);
2324 if (e->blocks) 2528 if (e->blocks) {
2325 state->hdmi_edid.present |= 0x04 << e->pad; 2529 state->hdmi_edid.present |= 0x04 << e->pad;
2326 else 2530 } else {
2327 state->hdmi_edid.present &= ~(0x04 << e->pad); 2531 state->hdmi_edid.present &= ~(0x04 << e->pad);
2532 adv7842_s_detect_tx_5v_ctrl(sd);
2533 }
2328 memcpy(&state->hdmi_edid.edid, e->edid, 128 * e->blocks); 2534 memcpy(&state->hdmi_edid.edid, e->edid, 128 * e->blocks);
2329 err = edid_write_hdmi_segment(sd, e->pad); 2535 err = edid_write_hdmi_segment(sd, e->pad);
2330 break; 2536 break;
@@ -2397,6 +2603,8 @@ static void adv7842_log_infoframes(struct v4l2_subdev *sd)
2397 log_infoframe(sd, &cri[i]); 2603 log_infoframe(sd, &cri[i]);
2398} 2604}
2399 2605
2606#if 0
2607/* Let's keep it here for now, as it could be useful for debug */
2400static const char * const prim_mode_txt[] = { 2608static const char * const prim_mode_txt[] = {
2401 "SDP", 2609 "SDP",
2402 "Component", 2610 "Component",
@@ -2415,6 +2623,7 @@ static const char * const prim_mode_txt[] = {
2415 "Reserved", 2623 "Reserved",
2416 "Reserved", 2624 "Reserved",
2417}; 2625};
2626#endif
2418 2627
2419static int adv7842_sdp_log_status(struct v4l2_subdev *sd) 2628static int adv7842_sdp_log_status(struct v4l2_subdev *sd)
2420{ 2629{
@@ -2509,8 +2718,19 @@ static int adv7842_cp_log_status(struct v4l2_subdev *sd)
2509 v4l2_info(sd, "HPD A %s, B %s\n", 2718 v4l2_info(sd, "HPD A %s, B %s\n",
2510 reg_io_0x21 & 0x02 ? "enabled" : "disabled", 2719 reg_io_0x21 & 0x02 ? "enabled" : "disabled",
2511 reg_io_0x21 & 0x01 ? "enabled" : "disabled"); 2720 reg_io_0x21 & 0x01 ? "enabled" : "disabled");
2512 v4l2_info(sd, "CEC %s\n", !!(cec_read(sd, 0x2a) & 0x01) ? 2721 v4l2_info(sd, "CEC: %s\n", state->cec_enabled_adap ?
2513 "enabled" : "disabled"); 2722 "enabled" : "disabled");
2723 if (state->cec_enabled_adap) {
2724 int i;
2725
2726 for (i = 0; i < ADV7842_MAX_ADDRS; i++) {
2727 bool is_valid = state->cec_valid_addrs & (1 << i);
2728
2729 if (is_valid)
2730 v4l2_info(sd, "CEC Logical Address: 0x%x\n",
2731 state->cec_addr[i]);
2732 }
2733 }
2514 2734
2515 v4l2_info(sd, "-----Signal status-----\n"); 2735 v4l2_info(sd, "-----Signal status-----\n");
2516 if (state->hdmi_port_a) { 2736 if (state->hdmi_port_a) {
@@ -2569,11 +2789,11 @@ static int adv7842_cp_log_status(struct v4l2_subdev *sd)
2569 rgb_quantization_range_txt[state->rgb_quantization_range]); 2789 rgb_quantization_range_txt[state->rgb_quantization_range]);
2570 v4l2_info(sd, "Input color space: %s\n", 2790 v4l2_info(sd, "Input color space: %s\n",
2571 input_color_space_txt[reg_io_0x02 >> 4]); 2791 input_color_space_txt[reg_io_0x02 >> 4]);
2572 v4l2_info(sd, "Output color space: %s %s, saturator %s\n", 2792 v4l2_info(sd, "Output color space: %s %s, alt-gamma %s\n",
2573 (reg_io_0x02 & 0x02) ? "RGB" : "YCbCr", 2793 (reg_io_0x02 & 0x02) ? "RGB" : "YCbCr",
2574 (reg_io_0x02 & 0x04) ? "(16-235)" : "(0-255)", 2794 (((reg_io_0x02 >> 2) & 0x01) ^ (reg_io_0x02 & 0x01)) ?
2575 ((reg_io_0x02 & 0x04) ^ (reg_io_0x02 & 0x01)) ? 2795 "(16-235)" : "(0-255)",
2576 "enabled" : "disabled"); 2796 (reg_io_0x02 & 0x08) ? "enabled" : "disabled");
2577 v4l2_info(sd, "Color space conversion: %s\n", 2797 v4l2_info(sd, "Color space conversion: %s\n",
2578 csc_coeff_sel_rb[cp_read(sd, 0xf4) >> 4]); 2798 csc_coeff_sel_rb[cp_read(sd, 0xf4) >> 4]);
2579 2799
@@ -2777,11 +2997,7 @@ static int adv7842_core_init(struct v4l2_subdev *sd)
2777 io_write(sd, 0x15, 0x80); /* Power up pads */ 2997 io_write(sd, 0x15, 0x80); /* Power up pads */
2778 2998
2779 /* video format */ 2999 /* video format */
2780 io_write(sd, 0x02, 3000 io_write(sd, 0x02, 0xf0 | pdata->alt_gamma << 3);
2781 0xf0 |
2782 pdata->alt_gamma << 3 |
2783 pdata->op_656_range << 2 |
2784 pdata->alt_data_sat << 0);
2785 io_write_and_or(sd, 0x05, 0xf0, pdata->blank_data << 3 | 3001 io_write_and_or(sd, 0x05, 0xf0, pdata->blank_data << 3 |
2786 pdata->insert_av_codes << 2 | 3002 pdata->insert_av_codes << 2 |
2787 pdata->replicate_av_codes << 1); 3003 pdata->replicate_av_codes << 1);
@@ -3031,6 +3247,24 @@ static int adv7842_subscribe_event(struct v4l2_subdev *sd,
3031 } 3247 }
3032} 3248}
3033 3249
3250static int adv7842_registered(struct v4l2_subdev *sd)
3251{
3252 struct adv7842_state *state = to_state(sd);
3253 int err;
3254
3255 err = cec_register_adapter(state->cec_adap);
3256 if (err)
3257 cec_delete_adapter(state->cec_adap);
3258 return err;
3259}
3260
3261static void adv7842_unregistered(struct v4l2_subdev *sd)
3262{
3263 struct adv7842_state *state = to_state(sd);
3264
3265 cec_unregister_adapter(state->cec_adap);
3266}
3267
3034/* ----------------------------------------------------------------------- */ 3268/* ----------------------------------------------------------------------- */
3035 3269
3036static const struct v4l2_ctrl_ops adv7842_ctrl_ops = { 3270static const struct v4l2_ctrl_ops adv7842_ctrl_ops = {
@@ -3077,6 +3311,11 @@ static const struct v4l2_subdev_ops adv7842_ops = {
3077 .pad = &adv7842_pad_ops, 3311 .pad = &adv7842_pad_ops,
3078}; 3312};
3079 3313
3314static const struct v4l2_subdev_internal_ops adv7842_int_ops = {
3315 .registered = adv7842_registered,
3316 .unregistered = adv7842_unregistered,
3317};
3318
3080/* -------------------------- custom ctrls ---------------------------------- */ 3319/* -------------------------- custom ctrls ---------------------------------- */
3081 3320
3082static const struct v4l2_ctrl_config adv7842_ctrl_analog_sampling_phase = { 3321static const struct v4l2_ctrl_config adv7842_ctrl_analog_sampling_phase = {
@@ -3241,6 +3480,7 @@ static int adv7842_probe(struct i2c_client *client,
3241 sd = &state->sd; 3480 sd = &state->sd;
3242 v4l2_i2c_subdev_init(sd, client, &adv7842_ops); 3481 v4l2_i2c_subdev_init(sd, client, &adv7842_ops);
3243 sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE | V4L2_SUBDEV_FL_HAS_EVENTS; 3482 sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE | V4L2_SUBDEV_FL_HAS_EVENTS;
3483 sd->internal_ops = &adv7842_int_ops;
3244 state->mode = pdata->mode; 3484 state->mode = pdata->mode;
3245 3485
3246 state->hdmi_port_a = pdata->input == ADV7842_SELECT_HDMI_PORT_A; 3486 state->hdmi_port_a = pdata->input == ADV7842_SELECT_HDMI_PORT_A;
@@ -3311,13 +3551,6 @@ static int adv7842_probe(struct i2c_client *client,
3311 goto err_i2c; 3551 goto err_i2c;
3312 } 3552 }
3313 3553
3314 /* work queues */
3315 state->work_queues = create_singlethread_workqueue(client->name);
3316 if (!state->work_queues) {
3317 v4l2_err(sd, "Could not create work queue\n");
3318 err = -ENOMEM;
3319 goto err_i2c;
3320 }
3321 3554
3322 INIT_DELAYED_WORK(&state->delayed_work_enable_hotplug, 3555 INIT_DELAYED_WORK(&state->delayed_work_enable_hotplug,
3323 adv7842_delayed_work_enable_hotplug); 3556 adv7842_delayed_work_enable_hotplug);
@@ -3331,6 +3564,17 @@ static int adv7842_probe(struct i2c_client *client,
3331 if (err) 3564 if (err)
3332 goto err_entity; 3565 goto err_entity;
3333 3566
3567#if IS_ENABLED(CONFIG_VIDEO_ADV7842_CEC)
3568 state->cec_adap = cec_allocate_adapter(&adv7842_cec_adap_ops,
3569 state, dev_name(&client->dev),
3570 CEC_CAP_TRANSMIT | CEC_CAP_LOG_ADDRS |
3571 CEC_CAP_PASSTHROUGH | CEC_CAP_RC, ADV7842_MAX_ADDRS,
3572 &client->dev);
3573 err = PTR_ERR_OR_ZERO(state->cec_adap);
3574 if (err)
3575 goto err_entity;
3576#endif
3577
3334 v4l2_info(sd, "%s found @ 0x%x (%s)\n", client->name, 3578 v4l2_info(sd, "%s found @ 0x%x (%s)\n", client->name,
3335 client->addr << 1, client->adapter->name); 3579 client->addr << 1, client->adapter->name);
3336 return 0; 3580 return 0;
@@ -3339,7 +3583,6 @@ err_entity:
3339 media_entity_cleanup(&sd->entity); 3583 media_entity_cleanup(&sd->entity);
3340err_work_queues: 3584err_work_queues:
3341 cancel_delayed_work(&state->delayed_work_enable_hotplug); 3585 cancel_delayed_work(&state->delayed_work_enable_hotplug);
3342 destroy_workqueue(state->work_queues);
3343err_i2c: 3586err_i2c:
3344 adv7842_unregister_clients(sd); 3587 adv7842_unregister_clients(sd);
3345err_hdl: 3588err_hdl:
@@ -3355,9 +3598,7 @@ static int adv7842_remove(struct i2c_client *client)
3355 struct adv7842_state *state = to_state(sd); 3598 struct adv7842_state *state = to_state(sd);
3356 3599
3357 adv7842_irq_enable(sd, false); 3600 adv7842_irq_enable(sd, false);
3358
3359 cancel_delayed_work(&state->delayed_work_enable_hotplug); 3601 cancel_delayed_work(&state->delayed_work_enable_hotplug);
3360 destroy_workqueue(state->work_queues);
3361 v4l2_device_unregister_subdev(sd); 3602 v4l2_device_unregister_subdev(sd);
3362 media_entity_cleanup(&sd->entity); 3603 media_entity_cleanup(&sd->entity);
3363 adv7842_unregister_clients(sd); 3604 adv7842_unregister_clients(sd);
diff --git a/drivers/media/i2c/cs53l32a.c b/drivers/media/i2c/cs53l32a.c
index b7e87e38642a..e4b3cf49dd38 100644
--- a/drivers/media/i2c/cs53l32a.c
+++ b/drivers/media/i2c/cs53l32a.c
@@ -121,13 +121,6 @@ static const struct v4l2_ctrl_ops cs53l32a_ctrl_ops = {
121 121
122static const struct v4l2_subdev_core_ops cs53l32a_core_ops = { 122static const struct v4l2_subdev_core_ops cs53l32a_core_ops = {
123 .log_status = cs53l32a_log_status, 123 .log_status = cs53l32a_log_status,
124 .g_ext_ctrls = v4l2_subdev_g_ext_ctrls,
125 .try_ext_ctrls = v4l2_subdev_try_ext_ctrls,
126 .s_ext_ctrls = v4l2_subdev_s_ext_ctrls,
127 .g_ctrl = v4l2_subdev_g_ctrl,
128 .s_ctrl = v4l2_subdev_s_ctrl,
129 .queryctrl = v4l2_subdev_queryctrl,
130 .querymenu = v4l2_subdev_querymenu,
131}; 124};
132 125
133static const struct v4l2_subdev_audio_ops cs53l32a_audio_ops = { 126static const struct v4l2_subdev_audio_ops cs53l32a_audio_ops = {
diff --git a/drivers/media/i2c/cx25840/cx25840-core.c b/drivers/media/i2c/cx25840/cx25840-core.c
index 07a3e7173144..142ae28803bb 100644
--- a/drivers/media/i2c/cx25840/cx25840-core.c
+++ b/drivers/media/i2c/cx25840/cx25840-core.c
@@ -5042,13 +5042,6 @@ static const struct v4l2_ctrl_ops cx25840_ctrl_ops = {
5042 5042
5043static const struct v4l2_subdev_core_ops cx25840_core_ops = { 5043static const struct v4l2_subdev_core_ops cx25840_core_ops = {
5044 .log_status = cx25840_log_status, 5044 .log_status = cx25840_log_status,
5045 .g_ctrl = v4l2_subdev_g_ctrl,
5046 .s_ctrl = v4l2_subdev_s_ctrl,
5047 .s_ext_ctrls = v4l2_subdev_s_ext_ctrls,
5048 .try_ext_ctrls = v4l2_subdev_try_ext_ctrls,
5049 .g_ext_ctrls = v4l2_subdev_g_ext_ctrls,
5050 .queryctrl = v4l2_subdev_queryctrl,
5051 .querymenu = v4l2_subdev_querymenu,
5052 .reset = cx25840_reset, 5045 .reset = cx25840_reset,
5053 .load_fw = cx25840_load_fw, 5046 .load_fw = cx25840_load_fw,
5054 .s_io_pin_config = common_s_io_pin_config, 5047 .s_io_pin_config = common_s_io_pin_config,
diff --git a/drivers/media/i2c/msp3400-driver.c b/drivers/media/i2c/msp3400-driver.c
index e016626ebf89..503b7c4f0a9b 100644
--- a/drivers/media/i2c/msp3400-driver.c
+++ b/drivers/media/i2c/msp3400-driver.c
@@ -642,13 +642,6 @@ static const struct v4l2_ctrl_ops msp_ctrl_ops = {
642 642
643static const struct v4l2_subdev_core_ops msp_core_ops = { 643static const struct v4l2_subdev_core_ops msp_core_ops = {
644 .log_status = msp_log_status, 644 .log_status = msp_log_status,
645 .g_ext_ctrls = v4l2_subdev_g_ext_ctrls,
646 .try_ext_ctrls = v4l2_subdev_try_ext_ctrls,
647 .s_ext_ctrls = v4l2_subdev_s_ext_ctrls,
648 .g_ctrl = v4l2_subdev_g_ctrl,
649 .s_ctrl = v4l2_subdev_s_ctrl,
650 .queryctrl = v4l2_subdev_queryctrl,
651 .querymenu = v4l2_subdev_querymenu,
652}; 645};
653 646
654static const struct v4l2_subdev_video_ops msp_video_ops = { 647static const struct v4l2_subdev_video_ops msp_video_ops = {
diff --git a/drivers/media/i2c/mt9t001.c b/drivers/media/i2c/mt9t001.c
index 702d562f8e39..842017fa4aab 100644
--- a/drivers/media/i2c/mt9t001.c
+++ b/drivers/media/i2c/mt9t001.c
@@ -233,10 +233,21 @@ static int __mt9t001_set_power(struct mt9t001 *mt9t001, bool on)
233 ret = mt9t001_reset(mt9t001); 233 ret = mt9t001_reset(mt9t001);
234 if (ret < 0) { 234 if (ret < 0) {
235 dev_err(&client->dev, "Failed to reset the camera\n"); 235 dev_err(&client->dev, "Failed to reset the camera\n");
236 return ret; 236 goto e_power;
237 } 237 }
238 238
239 return v4l2_ctrl_handler_setup(&mt9t001->ctrls); 239 ret = v4l2_ctrl_handler_setup(&mt9t001->ctrls);
240 if (ret < 0) {
241 dev_err(&client->dev, "Failed to set up control handlers\n");
242 goto e_power;
243 }
244
245 return 0;
246
247e_power:
248 mt9t001_power_off(mt9t001);
249
250 return ret;
240} 251}
241 252
242/* ----------------------------------------------------------------------------- 253/* -----------------------------------------------------------------------------
@@ -834,7 +845,7 @@ static struct v4l2_subdev_ops mt9t001_subdev_ops = {
834 .pad = &mt9t001_subdev_pad_ops, 845 .pad = &mt9t001_subdev_pad_ops,
835}; 846};
836 847
837static struct v4l2_subdev_internal_ops mt9t001_subdev_internal_ops = { 848static const struct v4l2_subdev_internal_ops mt9t001_subdev_internal_ops = {
838 .registered = mt9t001_registered, 849 .registered = mt9t001_registered,
839 .open = mt9t001_open, 850 .open = mt9t001_open,
840 .close = mt9t001_close, 851 .close = mt9t001_close,
diff --git a/drivers/media/i2c/mt9v032.c b/drivers/media/i2c/mt9v032.c
index 501b37039449..58eb62f1ba21 100644
--- a/drivers/media/i2c/mt9v032.c
+++ b/drivers/media/i2c/mt9v032.c
@@ -19,7 +19,6 @@
19#include <linux/log2.h> 19#include <linux/log2.h>
20#include <linux/mutex.h> 20#include <linux/mutex.h>
21#include <linux/of.h> 21#include <linux/of.h>
22#include <linux/of_gpio.h>
23#include <linux/regmap.h> 22#include <linux/regmap.h>
24#include <linux/slab.h> 23#include <linux/slab.h>
25#include <linux/videodev2.h> 24#include <linux/videodev2.h>
@@ -133,9 +132,16 @@
133#define MT9V032_TEST_PATTERN_GRAY_DIAGONAL (3 << 11) 132#define MT9V032_TEST_PATTERN_GRAY_DIAGONAL (3 << 11)
134#define MT9V032_TEST_PATTERN_ENABLE (1 << 13) 133#define MT9V032_TEST_PATTERN_ENABLE (1 << 13)
135#define MT9V032_TEST_PATTERN_FLIP (1 << 14) 134#define MT9V032_TEST_PATTERN_FLIP (1 << 14)
135#define MT9V032_AEGC_DESIRED_BIN 0xa5
136#define MT9V032_AEC_UPDATE_FREQUENCY 0xa6
137#define MT9V032_AEC_LPF 0xa8
138#define MT9V032_AGC_UPDATE_FREQUENCY 0xa9
139#define MT9V032_AGC_LPF 0xaa
136#define MT9V032_AEC_AGC_ENABLE 0xaf 140#define MT9V032_AEC_AGC_ENABLE 0xaf
137#define MT9V032_AEC_ENABLE (1 << 0) 141#define MT9V032_AEC_ENABLE (1 << 0)
138#define MT9V032_AGC_ENABLE (1 << 1) 142#define MT9V032_AGC_ENABLE (1 << 1)
143#define MT9V034_AEC_MAX_SHUTTER_WIDTH 0xad
144#define MT9V032_AEC_MAX_SHUTTER_WIDTH 0xbd
139#define MT9V032_THERMAL_INFO 0xc1 145#define MT9V032_THERMAL_INFO 0xc1
140 146
141enum mt9v032_model { 147enum mt9v032_model {
@@ -162,6 +168,8 @@ struct mt9v032_model_data {
162 unsigned int min_shutter; 168 unsigned int min_shutter;
163 unsigned int max_shutter; 169 unsigned int max_shutter;
164 unsigned int pclk_reg; 170 unsigned int pclk_reg;
171 unsigned int aec_max_shutter_reg;
172 const struct v4l2_ctrl_config * const aec_max_shutter_v4l2_ctrl;
165}; 173};
166 174
167struct mt9v032_model_info { 175struct mt9v032_model_info {
@@ -175,63 +183,6 @@ static const struct mt9v032_model_version mt9v032_versions[] = {
175 { MT9V034_CHIP_ID_REV1, "MT9V024/MT9V034 rev1" }, 183 { MT9V034_CHIP_ID_REV1, "MT9V024/MT9V034 rev1" },
176}; 184};
177 185
178static const struct mt9v032_model_data mt9v032_model_data[] = {
179 {
180 /* MT9V022, MT9V032 revisions 1/2/3 */
181 .min_row_time = 660,
182 .min_hblank = MT9V032_HORIZONTAL_BLANKING_MIN,
183 .min_vblank = MT9V032_VERTICAL_BLANKING_MIN,
184 .max_vblank = MT9V032_VERTICAL_BLANKING_MAX,
185 .min_shutter = MT9V032_TOTAL_SHUTTER_WIDTH_MIN,
186 .max_shutter = MT9V032_TOTAL_SHUTTER_WIDTH_MAX,
187 .pclk_reg = MT9V032_PIXEL_CLOCK,
188 }, {
189 /* MT9V024, MT9V034 */
190 .min_row_time = 690,
191 .min_hblank = MT9V034_HORIZONTAL_BLANKING_MIN,
192 .min_vblank = MT9V034_VERTICAL_BLANKING_MIN,
193 .max_vblank = MT9V034_VERTICAL_BLANKING_MAX,
194 .min_shutter = MT9V034_TOTAL_SHUTTER_WIDTH_MIN,
195 .max_shutter = MT9V034_TOTAL_SHUTTER_WIDTH_MAX,
196 .pclk_reg = MT9V034_PIXEL_CLOCK,
197 },
198};
199
200static const struct mt9v032_model_info mt9v032_models[] = {
201 [MT9V032_MODEL_V022_COLOR] = {
202 .data = &mt9v032_model_data[0],
203 .color = true,
204 },
205 [MT9V032_MODEL_V022_MONO] = {
206 .data = &mt9v032_model_data[0],
207 .color = false,
208 },
209 [MT9V032_MODEL_V024_COLOR] = {
210 .data = &mt9v032_model_data[1],
211 .color = true,
212 },
213 [MT9V032_MODEL_V024_MONO] = {
214 .data = &mt9v032_model_data[1],
215 .color = false,
216 },
217 [MT9V032_MODEL_V032_COLOR] = {
218 .data = &mt9v032_model_data[0],
219 .color = true,
220 },
221 [MT9V032_MODEL_V032_MONO] = {
222 .data = &mt9v032_model_data[0],
223 .color = false,
224 },
225 [MT9V032_MODEL_V034_COLOR] = {
226 .data = &mt9v032_model_data[1],
227 .color = true,
228 },
229 [MT9V032_MODEL_V034_MONO] = {
230 .data = &mt9v032_model_data[1],
231 .color = false,
232 },
233};
234
235struct mt9v032 { 186struct mt9v032 {
236 struct v4l2_subdev subdev; 187 struct v4l2_subdev subdev;
237 struct media_pad pad; 188 struct media_pad pad;
@@ -349,7 +300,8 @@ static int mt9v032_power_on(struct mt9v032 *mt9v032)
349 if (ret < 0) 300 if (ret < 0)
350 return ret; 301 return ret;
351 302
352 return regmap_write(map, MT9V032_CHIP_CONTROL, 0); 303 return regmap_write(map, MT9V032_CHIP_CONTROL,
304 MT9V032_CHIP_CONTROL_MASTER_MODE);
353} 305}
354 306
355static void mt9v032_power_off(struct mt9v032 *mt9v032) 307static void mt9v032_power_off(struct mt9v032 *mt9v032)
@@ -421,8 +373,7 @@ __mt9v032_get_pad_crop(struct mt9v032 *mt9v032, struct v4l2_subdev_pad_config *c
421 373
422static int mt9v032_s_stream(struct v4l2_subdev *subdev, int enable) 374static int mt9v032_s_stream(struct v4l2_subdev *subdev, int enable)
423{ 375{
424 const u16 mode = MT9V032_CHIP_CONTROL_MASTER_MODE 376 const u16 mode = MT9V032_CHIP_CONTROL_DOUT_ENABLE
425 | MT9V032_CHIP_CONTROL_DOUT_ENABLE
426 | MT9V032_CHIP_CONTROL_SEQUENTIAL; 377 | MT9V032_CHIP_CONTROL_SEQUENTIAL;
427 struct mt9v032 *mt9v032 = to_mt9v032(subdev); 378 struct mt9v032 *mt9v032 = to_mt9v032(subdev);
428 struct v4l2_rect *crop = &mt9v032->crop; 379 struct v4l2_rect *crop = &mt9v032->crop;
@@ -647,6 +598,34 @@ static int mt9v032_set_selection(struct v4l2_subdev *subdev,
647 */ 598 */
648 599
649#define V4L2_CID_TEST_PATTERN_COLOR (V4L2_CID_USER_BASE | 0x1001) 600#define V4L2_CID_TEST_PATTERN_COLOR (V4L2_CID_USER_BASE | 0x1001)
601/*
602 * Value between 1 and 64 to set the desired bin. This is effectively a measure
603 * of how bright the image is supposed to be. Both AGC and AEC try to reach
604 * this.
605 */
606#define V4L2_CID_AEGC_DESIRED_BIN (V4L2_CID_USER_BASE | 0x1002)
607/*
608 * LPF is the low pass filter capability of the chip. Both AEC and AGC have
609 * this setting. This limits the speed in which AGC/AEC adjust their settings.
610 * Possible values are 0-2. 0 means no LPF. For 1 and 2 this equation is used:
611 *
612 * if |(calculated new exp - current exp)| > (current exp / 4)
613 * next exp = calculated new exp
614 * else
615 * next exp = current exp + ((calculated new exp - current exp) / 2^LPF)
616 */
617#define V4L2_CID_AEC_LPF (V4L2_CID_USER_BASE | 0x1003)
618#define V4L2_CID_AGC_LPF (V4L2_CID_USER_BASE | 0x1004)
619/*
620 * Value between 0 and 15. This is the number of frames being skipped before
621 * updating the auto exposure/gain.
622 */
623#define V4L2_CID_AEC_UPDATE_INTERVAL (V4L2_CID_USER_BASE | 0x1005)
624#define V4L2_CID_AGC_UPDATE_INTERVAL (V4L2_CID_USER_BASE | 0x1006)
625/*
626 * Maximum shutter width used for AEC.
627 */
628#define V4L2_CID_AEC_MAX_SHUTTER_WIDTH (V4L2_CID_USER_BASE | 0x1007)
650 629
651static int mt9v032_s_ctrl(struct v4l2_ctrl *ctrl) 630static int mt9v032_s_ctrl(struct v4l2_ctrl *ctrl)
652{ 631{
@@ -716,6 +695,28 @@ static int mt9v032_s_ctrl(struct v4l2_ctrl *ctrl)
716 break; 695 break;
717 } 696 }
718 return regmap_write(map, MT9V032_TEST_PATTERN, data); 697 return regmap_write(map, MT9V032_TEST_PATTERN, data);
698
699 case V4L2_CID_AEGC_DESIRED_BIN:
700 return regmap_write(map, MT9V032_AEGC_DESIRED_BIN, ctrl->val);
701
702 case V4L2_CID_AEC_LPF:
703 return regmap_write(map, MT9V032_AEC_LPF, ctrl->val);
704
705 case V4L2_CID_AGC_LPF:
706 return regmap_write(map, MT9V032_AGC_LPF, ctrl->val);
707
708 case V4L2_CID_AEC_UPDATE_INTERVAL:
709 return regmap_write(map, MT9V032_AEC_UPDATE_FREQUENCY,
710 ctrl->val);
711
712 case V4L2_CID_AGC_UPDATE_INTERVAL:
713 return regmap_write(map, MT9V032_AGC_UPDATE_FREQUENCY,
714 ctrl->val);
715
716 case V4L2_CID_AEC_MAX_SHUTTER_WIDTH:
717 return regmap_write(map,
718 mt9v032->model->data->aec_max_shutter_reg,
719 ctrl->val);
719 } 720 }
720 721
721 return 0; 722 return 0;
@@ -745,6 +746,84 @@ static const struct v4l2_ctrl_config mt9v032_test_pattern_color = {
745 .flags = 0, 746 .flags = 0,
746}; 747};
747 748
749static const struct v4l2_ctrl_config mt9v032_aegc_controls[] = {
750 {
751 .ops = &mt9v032_ctrl_ops,
752 .id = V4L2_CID_AEGC_DESIRED_BIN,
753 .type = V4L2_CTRL_TYPE_INTEGER,
754 .name = "AEC/AGC Desired Bin",
755 .min = 1,
756 .max = 64,
757 .step = 1,
758 .def = 58,
759 .flags = 0,
760 }, {
761 .ops = &mt9v032_ctrl_ops,
762 .id = V4L2_CID_AEC_LPF,
763 .type = V4L2_CTRL_TYPE_INTEGER,
764 .name = "AEC Low Pass Filter",
765 .min = 0,
766 .max = 2,
767 .step = 1,
768 .def = 0,
769 .flags = 0,
770 }, {
771 .ops = &mt9v032_ctrl_ops,
772 .id = V4L2_CID_AGC_LPF,
773 .type = V4L2_CTRL_TYPE_INTEGER,
774 .name = "AGC Low Pass Filter",
775 .min = 0,
776 .max = 2,
777 .step = 1,
778 .def = 2,
779 .flags = 0,
780 }, {
781 .ops = &mt9v032_ctrl_ops,
782 .id = V4L2_CID_AEC_UPDATE_INTERVAL,
783 .type = V4L2_CTRL_TYPE_INTEGER,
784 .name = "AEC Update Interval",
785 .min = 0,
786 .max = 16,
787 .step = 1,
788 .def = 2,
789 .flags = 0,
790 }, {
791 .ops = &mt9v032_ctrl_ops,
792 .id = V4L2_CID_AGC_UPDATE_INTERVAL,
793 .type = V4L2_CTRL_TYPE_INTEGER,
794 .name = "AGC Update Interval",
795 .min = 0,
796 .max = 16,
797 .step = 1,
798 .def = 2,
799 .flags = 0,
800 }
801};
802
803static const struct v4l2_ctrl_config mt9v032_aec_max_shutter_width = {
804 .ops = &mt9v032_ctrl_ops,
805 .id = V4L2_CID_AEC_MAX_SHUTTER_WIDTH,
806 .type = V4L2_CTRL_TYPE_INTEGER,
807 .name = "AEC Max Shutter Width",
808 .min = 1,
809 .max = 2047,
810 .step = 1,
811 .def = 480,
812 .flags = 0,
813};
814
815static const struct v4l2_ctrl_config mt9v034_aec_max_shutter_width = {
816 .ops = &mt9v032_ctrl_ops,
817 .id = V4L2_CID_AEC_MAX_SHUTTER_WIDTH,
818 .type = V4L2_CTRL_TYPE_INTEGER,
819 .name = "AEC Max Shutter Width",
820 .min = 1,
821 .max = 32765,
822 .step = 1,
823 .def = 480,
824 .flags = 0,
825};
826
748/* ----------------------------------------------------------------------------- 827/* -----------------------------------------------------------------------------
749 * V4L2 subdev core operations 828 * V4L2 subdev core operations
750 */ 829 */
@@ -953,13 +1032,6 @@ static int mt9v032_probe(struct i2c_client *client,
953 unsigned int i; 1032 unsigned int i;
954 int ret; 1033 int ret;
955 1034
956 if (!i2c_check_functionality(client->adapter,
957 I2C_FUNC_SMBUS_WORD_DATA)) {
958 dev_warn(&client->adapter->dev,
959 "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n");
960 return -EIO;
961 }
962
963 mt9v032 = devm_kzalloc(&client->dev, sizeof(*mt9v032), GFP_KERNEL); 1035 mt9v032 = devm_kzalloc(&client->dev, sizeof(*mt9v032), GFP_KERNEL);
964 if (!mt9v032) 1036 if (!mt9v032)
965 return -ENOMEM; 1037 return -ENOMEM;
@@ -986,7 +1058,8 @@ static int mt9v032_probe(struct i2c_client *client,
986 mt9v032->pdata = pdata; 1058 mt9v032->pdata = pdata;
987 mt9v032->model = (const void *)did->driver_data; 1059 mt9v032->model = (const void *)did->driver_data;
988 1060
989 v4l2_ctrl_handler_init(&mt9v032->ctrls, 10); 1061 v4l2_ctrl_handler_init(&mt9v032->ctrls, 11 +
1062 ARRAY_SIZE(mt9v032_aegc_controls));
990 1063
991 v4l2_ctrl_new_std(&mt9v032->ctrls, &mt9v032_ctrl_ops, 1064 v4l2_ctrl_new_std(&mt9v032->ctrls, &mt9v032_ctrl_ops,
992 V4L2_CID_AUTOGAIN, 0, 1, 1, 1); 1065 V4L2_CID_AUTOGAIN, 0, 1, 1, 1);
@@ -1015,6 +1088,13 @@ static int mt9v032_probe(struct i2c_client *client,
1015 mt9v032->test_pattern_color = v4l2_ctrl_new_custom(&mt9v032->ctrls, 1088 mt9v032->test_pattern_color = v4l2_ctrl_new_custom(&mt9v032->ctrls,
1016 &mt9v032_test_pattern_color, NULL); 1089 &mt9v032_test_pattern_color, NULL);
1017 1090
1091 v4l2_ctrl_new_custom(&mt9v032->ctrls,
1092 mt9v032->model->data->aec_max_shutter_v4l2_ctrl,
1093 NULL);
1094 for (i = 0; i < ARRAY_SIZE(mt9v032_aegc_controls); ++i)
1095 v4l2_ctrl_new_custom(&mt9v032->ctrls, &mt9v032_aegc_controls[i],
1096 NULL);
1097
1018 v4l2_ctrl_cluster(2, &mt9v032->test_pattern); 1098 v4l2_ctrl_cluster(2, &mt9v032->test_pattern);
1019 1099
1020 mt9v032->pixel_rate = 1100 mt9v032->pixel_rate =
@@ -1103,6 +1183,67 @@ static int mt9v032_remove(struct i2c_client *client)
1103 return 0; 1183 return 0;
1104} 1184}
1105 1185
1186static const struct mt9v032_model_data mt9v032_model_data[] = {
1187 {
1188 /* MT9V022, MT9V032 revisions 1/2/3 */
1189 .min_row_time = 660,
1190 .min_hblank = MT9V032_HORIZONTAL_BLANKING_MIN,
1191 .min_vblank = MT9V032_VERTICAL_BLANKING_MIN,
1192 .max_vblank = MT9V032_VERTICAL_BLANKING_MAX,
1193 .min_shutter = MT9V032_TOTAL_SHUTTER_WIDTH_MIN,
1194 .max_shutter = MT9V032_TOTAL_SHUTTER_WIDTH_MAX,
1195 .pclk_reg = MT9V032_PIXEL_CLOCK,
1196 .aec_max_shutter_reg = MT9V032_AEC_MAX_SHUTTER_WIDTH,
1197 .aec_max_shutter_v4l2_ctrl = &mt9v032_aec_max_shutter_width,
1198 }, {
1199 /* MT9V024, MT9V034 */
1200 .min_row_time = 690,
1201 .min_hblank = MT9V034_HORIZONTAL_BLANKING_MIN,
1202 .min_vblank = MT9V034_VERTICAL_BLANKING_MIN,
1203 .max_vblank = MT9V034_VERTICAL_BLANKING_MAX,
1204 .min_shutter = MT9V034_TOTAL_SHUTTER_WIDTH_MIN,
1205 .max_shutter = MT9V034_TOTAL_SHUTTER_WIDTH_MAX,
1206 .pclk_reg = MT9V034_PIXEL_CLOCK,
1207 .aec_max_shutter_reg = MT9V034_AEC_MAX_SHUTTER_WIDTH,
1208 .aec_max_shutter_v4l2_ctrl = &mt9v034_aec_max_shutter_width,
1209 },
1210};
1211
1212static const struct mt9v032_model_info mt9v032_models[] = {
1213 [MT9V032_MODEL_V022_COLOR] = {
1214 .data = &mt9v032_model_data[0],
1215 .color = true,
1216 },
1217 [MT9V032_MODEL_V022_MONO] = {
1218 .data = &mt9v032_model_data[0],
1219 .color = false,
1220 },
1221 [MT9V032_MODEL_V024_COLOR] = {
1222 .data = &mt9v032_model_data[1],
1223 .color = true,
1224 },
1225 [MT9V032_MODEL_V024_MONO] = {
1226 .data = &mt9v032_model_data[1],
1227 .color = false,
1228 },
1229 [MT9V032_MODEL_V032_COLOR] = {
1230 .data = &mt9v032_model_data[0],
1231 .color = true,
1232 },
1233 [MT9V032_MODEL_V032_MONO] = {
1234 .data = &mt9v032_model_data[0],
1235 .color = false,
1236 },
1237 [MT9V032_MODEL_V034_COLOR] = {
1238 .data = &mt9v032_model_data[1],
1239 .color = true,
1240 },
1241 [MT9V032_MODEL_V034_MONO] = {
1242 .data = &mt9v032_model_data[1],
1243 .color = false,
1244 },
1245};
1246
1106static const struct i2c_device_id mt9v032_id[] = { 1247static const struct i2c_device_id mt9v032_id[] = {
1107 { "mt9v022", (kernel_ulong_t)&mt9v032_models[MT9V032_MODEL_V022_COLOR] }, 1248 { "mt9v022", (kernel_ulong_t)&mt9v032_models[MT9V032_MODEL_V022_COLOR] },
1108 { "mt9v022m", (kernel_ulong_t)&mt9v032_models[MT9V032_MODEL_V022_MONO] }, 1249 { "mt9v022m", (kernel_ulong_t)&mt9v032_models[MT9V032_MODEL_V022_MONO] },
diff --git a/drivers/media/i2c/saa7115.c b/drivers/media/i2c/saa7115.c
index bd3526bdd539..58062b41c923 100644
--- a/drivers/media/i2c/saa7115.c
+++ b/drivers/media/i2c/saa7115.c
@@ -1585,13 +1585,6 @@ static const struct v4l2_ctrl_ops saa711x_ctrl_ops = {
1585 1585
1586static const struct v4l2_subdev_core_ops saa711x_core_ops = { 1586static const struct v4l2_subdev_core_ops saa711x_core_ops = {
1587 .log_status = saa711x_log_status, 1587 .log_status = saa711x_log_status,
1588 .g_ext_ctrls = v4l2_subdev_g_ext_ctrls,
1589 .try_ext_ctrls = v4l2_subdev_try_ext_ctrls,
1590 .s_ext_ctrls = v4l2_subdev_s_ext_ctrls,
1591 .g_ctrl = v4l2_subdev_g_ctrl,
1592 .s_ctrl = v4l2_subdev_s_ctrl,
1593 .queryctrl = v4l2_subdev_queryctrl,
1594 .querymenu = v4l2_subdev_querymenu,
1595 .reset = saa711x_reset, 1588 .reset = saa711x_reset,
1596 .s_gpio = saa711x_s_gpio, 1589 .s_gpio = saa711x_s_gpio,
1597#ifdef CONFIG_VIDEO_ADV_DEBUG 1590#ifdef CONFIG_VIDEO_ADV_DEBUG
diff --git a/drivers/media/i2c/smiapp/smiapp-core.c b/drivers/media/i2c/smiapp/smiapp-core.c
index 3dfe387abf6e..d08ab6c8357c 100644
--- a/drivers/media/i2c/smiapp/smiapp-core.c
+++ b/drivers/media/i2c/smiapp/smiapp-core.c
@@ -3044,10 +3044,8 @@ static struct smiapp_platform_data *smiapp_get_pdata(struct device *dev)
3044 pdata->op_sys_clock = devm_kcalloc( 3044 pdata->op_sys_clock = devm_kcalloc(
3045 dev, bus_cfg->nr_of_link_frequencies + 1 /* guardian */, 3045 dev, bus_cfg->nr_of_link_frequencies + 1 /* guardian */,
3046 sizeof(*pdata->op_sys_clock), GFP_KERNEL); 3046 sizeof(*pdata->op_sys_clock), GFP_KERNEL);
3047 if (!pdata->op_sys_clock) { 3047 if (!pdata->op_sys_clock)
3048 rval = -ENOMEM;
3049 goto out_err; 3048 goto out_err;
3050 }
3051 3049
3052 for (i = 0; i < bus_cfg->nr_of_link_frequencies; i++) { 3050 for (i = 0; i < bus_cfg->nr_of_link_frequencies; i++) {
3053 pdata->op_sys_clock[i] = bus_cfg->link_frequencies[i]; 3051 pdata->op_sys_clock[i] = bus_cfg->link_frequencies[i];
diff --git a/drivers/media/i2c/tc358743.c b/drivers/media/i2c/tc358743.c
index 6cf6d06737a5..1e3a0dd2238c 100644
--- a/drivers/media/i2c/tc358743.c
+++ b/drivers/media/i2c/tc358743.c
@@ -89,8 +89,6 @@ struct tc358743_state {
89 struct v4l2_ctrl *audio_sampling_rate_ctrl; 89 struct v4l2_ctrl *audio_sampling_rate_ctrl;
90 struct v4l2_ctrl *audio_present_ctrl; 90 struct v4l2_ctrl *audio_present_ctrl;
91 91
92 /* work queues */
93 struct workqueue_struct *work_queues;
94 struct delayed_work delayed_work_enable_hotplug; 92 struct delayed_work delayed_work_enable_hotplug;
95 93
96 /* edid */ 94 /* edid */
@@ -425,8 +423,7 @@ static void tc358743_enable_edid(struct v4l2_subdev *sd)
425 423
426 /* Enable hotplug after 100 ms. DDC access to EDID is also enabled when 424 /* Enable hotplug after 100 ms. DDC access to EDID is also enabled when
427 * hotplug is enabled. See register DDC_CTL */ 425 * hotplug is enabled. See register DDC_CTL */
428 queue_delayed_work(state->work_queues, 426 schedule_delayed_work(&state->delayed_work_enable_hotplug, HZ / 10);
429 &state->delayed_work_enable_hotplug, HZ / 10);
430 427
431 tc358743_enable_interrupts(sd, true); 428 tc358743_enable_interrupts(sd, true);
432 tc358743_s_ctrl_detect_tx_5v(sd); 429 tc358743_s_ctrl_detect_tx_5v(sd);
@@ -1884,14 +1881,6 @@ static int tc358743_probe(struct i2c_client *client,
1884 goto err_hdl; 1881 goto err_hdl;
1885 } 1882 }
1886 1883
1887 /* work queues */
1888 state->work_queues = create_singlethread_workqueue(client->name);
1889 if (!state->work_queues) {
1890 v4l2_err(sd, "Could not create work queue\n");
1891 err = -ENOMEM;
1892 goto err_hdl;
1893 }
1894
1895 state->pad.flags = MEDIA_PAD_FL_SOURCE; 1884 state->pad.flags = MEDIA_PAD_FL_SOURCE;
1896 err = media_entity_pads_init(&sd->entity, 1, &state->pad); 1885 err = media_entity_pads_init(&sd->entity, 1, &state->pad);
1897 if (err < 0) 1886 if (err < 0)
@@ -1940,7 +1929,6 @@ static int tc358743_probe(struct i2c_client *client,
1940 1929
1941err_work_queues: 1930err_work_queues:
1942 cancel_delayed_work(&state->delayed_work_enable_hotplug); 1931 cancel_delayed_work(&state->delayed_work_enable_hotplug);
1943 destroy_workqueue(state->work_queues);
1944 mutex_destroy(&state->confctl_mutex); 1932 mutex_destroy(&state->confctl_mutex);
1945err_hdl: 1933err_hdl:
1946 media_entity_cleanup(&sd->entity); 1934 media_entity_cleanup(&sd->entity);
@@ -1954,7 +1942,6 @@ static int tc358743_remove(struct i2c_client *client)
1954 struct tc358743_state *state = to_state(sd); 1942 struct tc358743_state *state = to_state(sd);
1955 1943
1956 cancel_delayed_work(&state->delayed_work_enable_hotplug); 1944 cancel_delayed_work(&state->delayed_work_enable_hotplug);
1957 destroy_workqueue(state->work_queues);
1958 v4l2_async_unregister_subdev(sd); 1945 v4l2_async_unregister_subdev(sd);
1959 v4l2_device_unregister_subdev(sd); 1946 v4l2_device_unregister_subdev(sd);
1960 mutex_destroy(&state->confctl_mutex); 1947 mutex_destroy(&state->confctl_mutex);
diff --git a/drivers/media/i2c/tvaudio.c b/drivers/media/i2c/tvaudio.c
index fece2a4339a1..42d1e26e581c 100644
--- a/drivers/media/i2c/tvaudio.c
+++ b/drivers/media/i2c/tvaudio.c
@@ -1855,13 +1855,6 @@ static const struct v4l2_ctrl_ops tvaudio_ctrl_ops = {
1855 1855
1856static const struct v4l2_subdev_core_ops tvaudio_core_ops = { 1856static const struct v4l2_subdev_core_ops tvaudio_core_ops = {
1857 .log_status = tvaudio_log_status, 1857 .log_status = tvaudio_log_status,
1858 .g_ext_ctrls = v4l2_subdev_g_ext_ctrls,
1859 .try_ext_ctrls = v4l2_subdev_try_ext_ctrls,
1860 .s_ext_ctrls = v4l2_subdev_s_ext_ctrls,
1861 .g_ctrl = v4l2_subdev_g_ctrl,
1862 .s_ctrl = v4l2_subdev_s_ctrl,
1863 .queryctrl = v4l2_subdev_queryctrl,
1864 .querymenu = v4l2_subdev_querymenu,
1865}; 1858};
1866 1859
1867static const struct v4l2_subdev_tuner_ops tvaudio_tuner_ops = { 1860static const struct v4l2_subdev_tuner_ops tvaudio_tuner_ops = {
diff --git a/drivers/media/i2c/wm8775.c b/drivers/media/i2c/wm8775.c
index 6e00f145b948..5581f4db02af 100644
--- a/drivers/media/i2c/wm8775.c
+++ b/drivers/media/i2c/wm8775.c
@@ -178,13 +178,6 @@ static const struct v4l2_ctrl_ops wm8775_ctrl_ops = {
178 178
179static const struct v4l2_subdev_core_ops wm8775_core_ops = { 179static const struct v4l2_subdev_core_ops wm8775_core_ops = {
180 .log_status = wm8775_log_status, 180 .log_status = wm8775_log_status,
181 .g_ext_ctrls = v4l2_subdev_g_ext_ctrls,
182 .try_ext_ctrls = v4l2_subdev_try_ext_ctrls,
183 .s_ext_ctrls = v4l2_subdev_s_ext_ctrls,
184 .g_ctrl = v4l2_subdev_g_ctrl,
185 .s_ctrl = v4l2_subdev_s_ctrl,
186 .queryctrl = v4l2_subdev_queryctrl,
187 .querymenu = v4l2_subdev_querymenu,
188}; 181};
189 182
190static const struct v4l2_subdev_tuner_ops wm8775_tuner_ops = { 183static const struct v4l2_subdev_tuner_ops wm8775_tuner_ops = {
diff --git a/drivers/media/media-device.c b/drivers/media/media-device.c
index a1cd50f331f1..1795abeda658 100644
--- a/drivers/media/media-device.c
+++ b/drivers/media/media-device.c
@@ -423,7 +423,7 @@ static long media_device_ioctl(struct file *filp, unsigned int cmd,
423 unsigned long arg) 423 unsigned long arg)
424{ 424{
425 struct media_devnode *devnode = media_devnode_data(filp); 425 struct media_devnode *devnode = media_devnode_data(filp);
426 struct media_device *dev = to_media_device(devnode); 426 struct media_device *dev = devnode->media_dev;
427 long ret; 427 long ret;
428 428
429 mutex_lock(&dev->graph_mutex); 429 mutex_lock(&dev->graph_mutex);
@@ -495,7 +495,7 @@ static long media_device_compat_ioctl(struct file *filp, unsigned int cmd,
495 unsigned long arg) 495 unsigned long arg)
496{ 496{
497 struct media_devnode *devnode = media_devnode_data(filp); 497 struct media_devnode *devnode = media_devnode_data(filp);
498 struct media_device *dev = to_media_device(devnode); 498 struct media_device *dev = devnode->media_dev;
499 long ret; 499 long ret;
500 500
501 switch (cmd) { 501 switch (cmd) {
@@ -531,7 +531,8 @@ static const struct media_file_operations media_device_fops = {
531static ssize_t show_model(struct device *cd, 531static ssize_t show_model(struct device *cd,
532 struct device_attribute *attr, char *buf) 532 struct device_attribute *attr, char *buf)
533{ 533{
534 struct media_device *mdev = to_media_device(to_media_devnode(cd)); 534 struct media_devnode *devnode = to_media_devnode(cd);
535 struct media_device *mdev = devnode->media_dev;
535 536
536 return sprintf(buf, "%.*s\n", (int)sizeof(mdev->model), mdev->model); 537 return sprintf(buf, "%.*s\n", (int)sizeof(mdev->model), mdev->model);
537} 538}
@@ -704,23 +705,35 @@ EXPORT_SYMBOL_GPL(media_device_cleanup);
704int __must_check __media_device_register(struct media_device *mdev, 705int __must_check __media_device_register(struct media_device *mdev,
705 struct module *owner) 706 struct module *owner)
706{ 707{
708 struct media_devnode *devnode;
707 int ret; 709 int ret;
708 710
711 devnode = kzalloc(sizeof(*devnode), GFP_KERNEL);
712 if (!devnode)
713 return -ENOMEM;
714
709 /* Register the device node. */ 715 /* Register the device node. */
710 mdev->devnode.fops = &media_device_fops; 716 mdev->devnode = devnode;
711 mdev->devnode.parent = mdev->dev; 717 devnode->fops = &media_device_fops;
712 mdev->devnode.release = media_device_release; 718 devnode->parent = mdev->dev;
719 devnode->release = media_device_release;
713 720
714 /* Set version 0 to indicate user-space that the graph is static */ 721 /* Set version 0 to indicate user-space that the graph is static */
715 mdev->topology_version = 0; 722 mdev->topology_version = 0;
716 723
717 ret = media_devnode_register(&mdev->devnode, owner); 724 ret = media_devnode_register(mdev, devnode, owner);
718 if (ret < 0) 725 if (ret < 0) {
726 /* devnode free is handled in media_devnode_*() */
727 mdev->devnode = NULL;
719 return ret; 728 return ret;
729 }
720 730
721 ret = device_create_file(&mdev->devnode.dev, &dev_attr_model); 731 ret = device_create_file(&devnode->dev, &dev_attr_model);
722 if (ret < 0) { 732 if (ret < 0) {
723 media_devnode_unregister(&mdev->devnode); 733 /* devnode free is handled in media_devnode_*() */
734 mdev->devnode = NULL;
735 media_devnode_unregister_prepare(devnode);
736 media_devnode_unregister(devnode);
724 return ret; 737 return ret;
725 } 738 }
726 739
@@ -771,11 +784,14 @@ void media_device_unregister(struct media_device *mdev)
771 mutex_lock(&mdev->graph_mutex); 784 mutex_lock(&mdev->graph_mutex);
772 785
773 /* Check if mdev was ever registered at all */ 786 /* Check if mdev was ever registered at all */
774 if (!media_devnode_is_registered(&mdev->devnode)) { 787 if (!media_devnode_is_registered(mdev->devnode)) {
775 mutex_unlock(&mdev->graph_mutex); 788 mutex_unlock(&mdev->graph_mutex);
776 return; 789 return;
777 } 790 }
778 791
792 /* Clear the devnode register bit to avoid races with media dev open */
793 media_devnode_unregister_prepare(mdev->devnode);
794
779 /* Remove all entities from the media device */ 795 /* Remove all entities from the media device */
780 list_for_each_entry_safe(entity, next, &mdev->entities, graph_obj.list) 796 list_for_each_entry_safe(entity, next, &mdev->entities, graph_obj.list)
781 __media_device_unregister_entity(entity); 797 __media_device_unregister_entity(entity);
@@ -794,9 +810,12 @@ void media_device_unregister(struct media_device *mdev)
794 810
795 mutex_unlock(&mdev->graph_mutex); 811 mutex_unlock(&mdev->graph_mutex);
796 812
797 device_remove_file(&mdev->devnode.dev, &dev_attr_model); 813 dev_dbg(mdev->dev, "Media device unregistered\n");
798 dev_dbg(mdev->dev, "Media device unregistering\n"); 814
799 media_devnode_unregister(&mdev->devnode); 815 device_remove_file(&mdev->devnode->dev, &dev_attr_model);
816 media_devnode_unregister(mdev->devnode);
817 /* devnode free is handled in media_devnode_*() */
818 mdev->devnode = NULL;
800} 819}
801EXPORT_SYMBOL_GPL(media_device_unregister); 820EXPORT_SYMBOL_GPL(media_device_unregister);
802 821
diff --git a/drivers/media/media-devnode.c b/drivers/media/media-devnode.c
index b66dc9d0766b..f2772ba6f611 100644
--- a/drivers/media/media-devnode.c
+++ b/drivers/media/media-devnode.c
@@ -44,6 +44,7 @@
44#include <linux/uaccess.h> 44#include <linux/uaccess.h>
45 45
46#include <media/media-devnode.h> 46#include <media/media-devnode.h>
47#include <media/media-device.h>
47 48
48#define MEDIA_NUM_DEVICES 256 49#define MEDIA_NUM_DEVICES 256
49#define MEDIA_NAME "media" 50#define MEDIA_NAME "media"
@@ -59,21 +60,19 @@ static DECLARE_BITMAP(media_devnode_nums, MEDIA_NUM_DEVICES);
59/* Called when the last user of the media device exits. */ 60/* Called when the last user of the media device exits. */
60static void media_devnode_release(struct device *cd) 61static void media_devnode_release(struct device *cd)
61{ 62{
62 struct media_devnode *mdev = to_media_devnode(cd); 63 struct media_devnode *devnode = to_media_devnode(cd);
63 64
64 mutex_lock(&media_devnode_lock); 65 mutex_lock(&media_devnode_lock);
65
66 /* Delete the cdev on this minor as well */
67 cdev_del(&mdev->cdev);
68
69 /* Mark device node number as free */ 66 /* Mark device node number as free */
70 clear_bit(mdev->minor, media_devnode_nums); 67 clear_bit(devnode->minor, media_devnode_nums);
71
72 mutex_unlock(&media_devnode_lock); 68 mutex_unlock(&media_devnode_lock);
73 69
74 /* Release media_devnode and perform other cleanups as needed. */ 70 /* Release media_devnode and perform other cleanups as needed. */
75 if (mdev->release) 71 if (devnode->release)
76 mdev->release(mdev); 72 devnode->release(devnode);
73
74 kfree(devnode);
75 pr_debug("%s: Media Devnode Deallocated\n", __func__);
77} 76}
78 77
79static struct bus_type media_bus_type = { 78static struct bus_type media_bus_type = {
@@ -83,37 +82,37 @@ static struct bus_type media_bus_type = {
83static ssize_t media_read(struct file *filp, char __user *buf, 82static ssize_t media_read(struct file *filp, char __user *buf,
84 size_t sz, loff_t *off) 83 size_t sz, loff_t *off)
85{ 84{
86 struct media_devnode *mdev = media_devnode_data(filp); 85 struct media_devnode *devnode = media_devnode_data(filp);
87 86
88 if (!mdev->fops->read) 87 if (!devnode->fops->read)
89 return -EINVAL; 88 return -EINVAL;
90 if (!media_devnode_is_registered(mdev)) 89 if (!media_devnode_is_registered(devnode))
91 return -EIO; 90 return -EIO;
92 return mdev->fops->read(filp, buf, sz, off); 91 return devnode->fops->read(filp, buf, sz, off);
93} 92}
94 93
95static ssize_t media_write(struct file *filp, const char __user *buf, 94static ssize_t media_write(struct file *filp, const char __user *buf,
96 size_t sz, loff_t *off) 95 size_t sz, loff_t *off)
97{ 96{
98 struct media_devnode *mdev = media_devnode_data(filp); 97 struct media_devnode *devnode = media_devnode_data(filp);
99 98
100 if (!mdev->fops->write) 99 if (!devnode->fops->write)
101 return -EINVAL; 100 return -EINVAL;
102 if (!media_devnode_is_registered(mdev)) 101 if (!media_devnode_is_registered(devnode))
103 return -EIO; 102 return -EIO;
104 return mdev->fops->write(filp, buf, sz, off); 103 return devnode->fops->write(filp, buf, sz, off);
105} 104}
106 105
107static unsigned int media_poll(struct file *filp, 106static unsigned int media_poll(struct file *filp,
108 struct poll_table_struct *poll) 107 struct poll_table_struct *poll)
109{ 108{
110 struct media_devnode *mdev = media_devnode_data(filp); 109 struct media_devnode *devnode = media_devnode_data(filp);
111 110
112 if (!media_devnode_is_registered(mdev)) 111 if (!media_devnode_is_registered(devnode))
113 return POLLERR | POLLHUP; 112 return POLLERR | POLLHUP;
114 if (!mdev->fops->poll) 113 if (!devnode->fops->poll)
115 return DEFAULT_POLLMASK; 114 return DEFAULT_POLLMASK;
116 return mdev->fops->poll(filp, poll); 115 return devnode->fops->poll(filp, poll);
117} 116}
118 117
119static long 118static long
@@ -121,12 +120,12 @@ __media_ioctl(struct file *filp, unsigned int cmd, unsigned long arg,
121 long (*ioctl_func)(struct file *filp, unsigned int cmd, 120 long (*ioctl_func)(struct file *filp, unsigned int cmd,
122 unsigned long arg)) 121 unsigned long arg))
123{ 122{
124 struct media_devnode *mdev = media_devnode_data(filp); 123 struct media_devnode *devnode = media_devnode_data(filp);
125 124
126 if (!ioctl_func) 125 if (!ioctl_func)
127 return -ENOTTY; 126 return -ENOTTY;
128 127
129 if (!media_devnode_is_registered(mdev)) 128 if (!media_devnode_is_registered(devnode))
130 return -EIO; 129 return -EIO;
131 130
132 return ioctl_func(filp, cmd, arg); 131 return ioctl_func(filp, cmd, arg);
@@ -134,9 +133,9 @@ __media_ioctl(struct file *filp, unsigned int cmd, unsigned long arg,
134 133
135static long media_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) 134static long media_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
136{ 135{
137 struct media_devnode *mdev = media_devnode_data(filp); 136 struct media_devnode *devnode = media_devnode_data(filp);
138 137
139 return __media_ioctl(filp, cmd, arg, mdev->fops->ioctl); 138 return __media_ioctl(filp, cmd, arg, devnode->fops->ioctl);
140} 139}
141 140
142#ifdef CONFIG_COMPAT 141#ifdef CONFIG_COMPAT
@@ -144,9 +143,9 @@ static long media_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
144static long media_compat_ioctl(struct file *filp, unsigned int cmd, 143static long media_compat_ioctl(struct file *filp, unsigned int cmd,
145 unsigned long arg) 144 unsigned long arg)
146{ 145{
147 struct media_devnode *mdev = media_devnode_data(filp); 146 struct media_devnode *devnode = media_devnode_data(filp);
148 147
149 return __media_ioctl(filp, cmd, arg, mdev->fops->compat_ioctl); 148 return __media_ioctl(filp, cmd, arg, devnode->fops->compat_ioctl);
150} 149}
151 150
152#endif /* CONFIG_COMPAT */ 151#endif /* CONFIG_COMPAT */
@@ -154,7 +153,7 @@ static long media_compat_ioctl(struct file *filp, unsigned int cmd,
154/* Override for the open function */ 153/* Override for the open function */
155static int media_open(struct inode *inode, struct file *filp) 154static int media_open(struct inode *inode, struct file *filp)
156{ 155{
157 struct media_devnode *mdev; 156 struct media_devnode *devnode;
158 int ret; 157 int ret;
159 158
160 /* Check if the media device is available. This needs to be done with 159 /* Check if the media device is available. This needs to be done with
@@ -164,23 +163,23 @@ static int media_open(struct inode *inode, struct file *filp)
164 * a crash. 163 * a crash.
165 */ 164 */
166 mutex_lock(&media_devnode_lock); 165 mutex_lock(&media_devnode_lock);
167 mdev = container_of(inode->i_cdev, struct media_devnode, cdev); 166 devnode = container_of(inode->i_cdev, struct media_devnode, cdev);
168 /* return ENXIO if the media device has been removed 167 /* return ENXIO if the media device has been removed
169 already or if it is not registered anymore. */ 168 already or if it is not registered anymore. */
170 if (!media_devnode_is_registered(mdev)) { 169 if (!media_devnode_is_registered(devnode)) {
171 mutex_unlock(&media_devnode_lock); 170 mutex_unlock(&media_devnode_lock);
172 return -ENXIO; 171 return -ENXIO;
173 } 172 }
174 /* and increase the device refcount */ 173 /* and increase the device refcount */
175 get_device(&mdev->dev); 174 get_device(&devnode->dev);
176 mutex_unlock(&media_devnode_lock); 175 mutex_unlock(&media_devnode_lock);
177 176
178 filp->private_data = mdev; 177 filp->private_data = devnode;
179 178
180 if (mdev->fops->open) { 179 if (devnode->fops->open) {
181 ret = mdev->fops->open(filp); 180 ret = devnode->fops->open(filp);
182 if (ret) { 181 if (ret) {
183 put_device(&mdev->dev); 182 put_device(&devnode->dev);
184 filp->private_data = NULL; 183 filp->private_data = NULL;
185 return ret; 184 return ret;
186 } 185 }
@@ -192,16 +191,18 @@ static int media_open(struct inode *inode, struct file *filp)
192/* Override for the release function */ 191/* Override for the release function */
193static int media_release(struct inode *inode, struct file *filp) 192static int media_release(struct inode *inode, struct file *filp)
194{ 193{
195 struct media_devnode *mdev = media_devnode_data(filp); 194 struct media_devnode *devnode = media_devnode_data(filp);
196 195
197 if (mdev->fops->release) 196 if (devnode->fops->release)
198 mdev->fops->release(filp); 197 devnode->fops->release(filp);
199 198
200 filp->private_data = NULL; 199 filp->private_data = NULL;
201 200
202 /* decrease the refcount unconditionally since the release() 201 /* decrease the refcount unconditionally since the release()
203 return value is ignored. */ 202 return value is ignored. */
204 put_device(&mdev->dev); 203 put_device(&devnode->dev);
204
205 pr_debug("%s: Media Release\n", __func__);
205 return 0; 206 return 0;
206} 207}
207 208
@@ -219,7 +220,8 @@ static const struct file_operations media_devnode_fops = {
219 .llseek = no_llseek, 220 .llseek = no_llseek,
220}; 221};
221 222
222int __must_check media_devnode_register(struct media_devnode *mdev, 223int __must_check media_devnode_register(struct media_device *mdev,
224 struct media_devnode *devnode,
223 struct module *owner) 225 struct module *owner)
224{ 226{
225 int minor; 227 int minor;
@@ -231,61 +233,80 @@ int __must_check media_devnode_register(struct media_devnode *mdev,
231 if (minor == MEDIA_NUM_DEVICES) { 233 if (minor == MEDIA_NUM_DEVICES) {
232 mutex_unlock(&media_devnode_lock); 234 mutex_unlock(&media_devnode_lock);
233 pr_err("could not get a free minor\n"); 235 pr_err("could not get a free minor\n");
236 kfree(devnode);
234 return -ENFILE; 237 return -ENFILE;
235 } 238 }
236 239
237 set_bit(minor, media_devnode_nums); 240 set_bit(minor, media_devnode_nums);
238 mutex_unlock(&media_devnode_lock); 241 mutex_unlock(&media_devnode_lock);
239 242
240 mdev->minor = minor; 243 devnode->minor = minor;
244 devnode->media_dev = mdev;
245
246 /* Part 1: Initialize dev now to use dev.kobj for cdev.kobj.parent */
247 devnode->dev.bus = &media_bus_type;
248 devnode->dev.devt = MKDEV(MAJOR(media_dev_t), devnode->minor);
249 devnode->dev.release = media_devnode_release;
250 if (devnode->parent)
251 devnode->dev.parent = devnode->parent;
252 dev_set_name(&devnode->dev, "media%d", devnode->minor);
253 device_initialize(&devnode->dev);
241 254
242 /* Part 2: Initialize and register the character device */ 255 /* Part 2: Initialize and register the character device */
243 cdev_init(&mdev->cdev, &media_devnode_fops); 256 cdev_init(&devnode->cdev, &media_devnode_fops);
244 mdev->cdev.owner = owner; 257 devnode->cdev.owner = owner;
258 devnode->cdev.kobj.parent = &devnode->dev.kobj;
245 259
246 ret = cdev_add(&mdev->cdev, MKDEV(MAJOR(media_dev_t), mdev->minor), 1); 260 ret = cdev_add(&devnode->cdev, MKDEV(MAJOR(media_dev_t), devnode->minor), 1);
247 if (ret < 0) { 261 if (ret < 0) {
248 pr_err("%s: cdev_add failed\n", __func__); 262 pr_err("%s: cdev_add failed\n", __func__);
249 goto error; 263 goto cdev_add_error;
250 } 264 }
251 265
252 /* Part 3: Register the media device */ 266 /* Part 3: Add the media device */
253 mdev->dev.bus = &media_bus_type; 267 ret = device_add(&devnode->dev);
254 mdev->dev.devt = MKDEV(MAJOR(media_dev_t), mdev->minor);
255 mdev->dev.release = media_devnode_release;
256 if (mdev->parent)
257 mdev->dev.parent = mdev->parent;
258 dev_set_name(&mdev->dev, "media%d", mdev->minor);
259 ret = device_register(&mdev->dev);
260 if (ret < 0) { 268 if (ret < 0) {
261 pr_err("%s: device_register failed\n", __func__); 269 pr_err("%s: device_add failed\n", __func__);
262 goto error; 270 goto device_add_error;
263 } 271 }
264 272
265 /* Part 4: Activate this minor. The char device can now be used. */ 273 /* Part 4: Activate this minor. The char device can now be used. */
266 set_bit(MEDIA_FLAG_REGISTERED, &mdev->flags); 274 set_bit(MEDIA_FLAG_REGISTERED, &devnode->flags);
267 275
268 return 0; 276 return 0;
269 277
270error: 278device_add_error:
279 cdev_del(&devnode->cdev);
280cdev_add_error:
271 mutex_lock(&media_devnode_lock); 281 mutex_lock(&media_devnode_lock);
272 cdev_del(&mdev->cdev); 282 clear_bit(devnode->minor, media_devnode_nums);
273 clear_bit(mdev->minor, media_devnode_nums); 283 devnode->media_dev = NULL;
274 mutex_unlock(&media_devnode_lock); 284 mutex_unlock(&media_devnode_lock);
275 285
286 put_device(&devnode->dev);
276 return ret; 287 return ret;
277} 288}
278 289
279void media_devnode_unregister(struct media_devnode *mdev) 290void media_devnode_unregister_prepare(struct media_devnode *devnode)
280{ 291{
281 /* Check if mdev was ever registered at all */ 292 /* Check if devnode was ever registered at all */
282 if (!media_devnode_is_registered(mdev)) 293 if (!media_devnode_is_registered(devnode))
283 return; 294 return;
284 295
285 mutex_lock(&media_devnode_lock); 296 mutex_lock(&media_devnode_lock);
286 clear_bit(MEDIA_FLAG_REGISTERED, &mdev->flags); 297 clear_bit(MEDIA_FLAG_REGISTERED, &devnode->flags);
298 mutex_unlock(&media_devnode_lock);
299}
300
301void media_devnode_unregister(struct media_devnode *devnode)
302{
303 mutex_lock(&media_devnode_lock);
304 /* Delete the cdev on this minor as well */
305 cdev_del(&devnode->cdev);
287 mutex_unlock(&media_devnode_lock); 306 mutex_unlock(&media_devnode_lock);
288 device_unregister(&mdev->dev); 307 device_del(&devnode->dev);
308 devnode->media_dev = NULL;
309 put_device(&devnode->dev);
289} 310}
290 311
291/* 312/*
diff --git a/drivers/media/pci/bt8xx/dst_ca.c b/drivers/media/pci/bt8xx/dst_ca.c
index da8b414fd824..8681b9143a35 100644
--- a/drivers/media/pci/bt8xx/dst_ca.c
+++ b/drivers/media/pci/bt8xx/dst_ca.c
@@ -655,7 +655,6 @@ static long dst_ca_ioctl(struct file *file, unsigned int cmd, unsigned long ioct
655static int dst_ca_open(struct inode *inode, struct file *file) 655static int dst_ca_open(struct inode *inode, struct file *file)
656{ 656{
657 dprintk(verbose, DST_CA_DEBUG, 1, " Device opened [%p] ", file); 657 dprintk(verbose, DST_CA_DEBUG, 1, " Device opened [%p] ", file);
658 try_module_get(THIS_MODULE);
659 658
660 return 0; 659 return 0;
661} 660}
@@ -663,7 +662,6 @@ static int dst_ca_open(struct inode *inode, struct file *file)
663static int dst_ca_release(struct inode *inode, struct file *file) 662static int dst_ca_release(struct inode *inode, struct file *file)
664{ 663{
665 dprintk(verbose, DST_CA_DEBUG, 1, " Device closed."); 664 dprintk(verbose, DST_CA_DEBUG, 1, " Device closed.");
666 module_put(THIS_MODULE);
667 665
668 return 0; 666 return 0;
669} 667}
diff --git a/drivers/media/pci/cobalt/cobalt-driver.c b/drivers/media/pci/cobalt/cobalt-driver.c
index 8d6f04fc8013..476f7f0dcf81 100644
--- a/drivers/media/pci/cobalt/cobalt-driver.c
+++ b/drivers/media/pci/cobalt/cobalt-driver.c
@@ -492,7 +492,6 @@ static int cobalt_subdevs_init(struct cobalt *cobalt)
492 .ain_sel = ADV7604_AIN7_8_9_NC_SYNC_3_1, 492 .ain_sel = ADV7604_AIN7_8_9_NC_SYNC_3_1,
493 .bus_order = ADV7604_BUS_ORDER_BRG, 493 .bus_order = ADV7604_BUS_ORDER_BRG,
494 .blank_data = 1, 494 .blank_data = 1,
495 .op_656_range = 1,
496 .op_format_mode_sel = ADV7604_OP_FORMAT_MODE0, 495 .op_format_mode_sel = ADV7604_OP_FORMAT_MODE0,
497 .int1_config = ADV76XX_INT1_CONFIG_ACTIVE_HIGH, 496 .int1_config = ADV76XX_INT1_CONFIG_ACTIVE_HIGH,
498 .dr_str_data = ADV76XX_DR_STR_HIGH, 497 .dr_str_data = ADV76XX_DR_STR_HIGH,
@@ -571,7 +570,6 @@ static int cobalt_subdevs_hsma_init(struct cobalt *cobalt)
571 .bus_order = ADV7842_BUS_ORDER_RBG, 570 .bus_order = ADV7842_BUS_ORDER_RBG,
572 .op_format_mode_sel = ADV7842_OP_FORMAT_MODE0, 571 .op_format_mode_sel = ADV7842_OP_FORMAT_MODE0,
573 .blank_data = 1, 572 .blank_data = 1,
574 .op_656_range = 1,
575 .dr_str_data = 3, 573 .dr_str_data = 3,
576 .dr_str_clk = 3, 574 .dr_str_clk = 3,
577 .dr_str_sync = 3, 575 .dr_str_sync = 3,
@@ -691,17 +689,10 @@ static int cobalt_probe(struct pci_dev *pci_dev,
691 cobalt->pci_dev = pci_dev; 689 cobalt->pci_dev = pci_dev;
692 cobalt->instance = i; 690 cobalt->instance = i;
693 691
694 cobalt->alloc_ctx = vb2_dma_sg_init_ctx(&pci_dev->dev);
695 if (IS_ERR(cobalt->alloc_ctx)) {
696 kfree(cobalt);
697 return -ENOMEM;
698 }
699
700 retval = v4l2_device_register(&pci_dev->dev, &cobalt->v4l2_dev); 692 retval = v4l2_device_register(&pci_dev->dev, &cobalt->v4l2_dev);
701 if (retval) { 693 if (retval) {
702 pr_err("cobalt: v4l2_device_register of card %d failed\n", 694 pr_err("cobalt: v4l2_device_register of card %d failed\n",
703 cobalt->instance); 695 cobalt->instance);
704 vb2_dma_sg_cleanup_ctx(cobalt->alloc_ctx);
705 kfree(cobalt); 696 kfree(cobalt);
706 return retval; 697 return retval;
707 } 698 }
@@ -782,7 +773,6 @@ err:
782 cobalt_err("error %d on initialization\n", retval); 773 cobalt_err("error %d on initialization\n", retval);
783 774
784 v4l2_device_unregister(&cobalt->v4l2_dev); 775 v4l2_device_unregister(&cobalt->v4l2_dev);
785 vb2_dma_sg_cleanup_ctx(cobalt->alloc_ctx);
786 kfree(cobalt); 776 kfree(cobalt);
787 return retval; 777 return retval;
788} 778}
@@ -818,7 +808,6 @@ static void cobalt_remove(struct pci_dev *pci_dev)
818 cobalt_info("removed cobalt card\n"); 808 cobalt_info("removed cobalt card\n");
819 809
820 v4l2_device_unregister(v4l2_dev); 810 v4l2_device_unregister(v4l2_dev);
821 vb2_dma_sg_cleanup_ctx(cobalt->alloc_ctx);
822 kfree(cobalt); 811 kfree(cobalt);
823} 812}
824 813
diff --git a/drivers/media/pci/cobalt/cobalt-driver.h b/drivers/media/pci/cobalt/cobalt-driver.h
index b2f08e4a68bf..ed00dc9d9399 100644
--- a/drivers/media/pci/cobalt/cobalt-driver.h
+++ b/drivers/media/pci/cobalt/cobalt-driver.h
@@ -262,7 +262,6 @@ struct cobalt {
262 int instance; 262 int instance;
263 struct pci_dev *pci_dev; 263 struct pci_dev *pci_dev;
264 struct v4l2_device v4l2_dev; 264 struct v4l2_device v4l2_dev;
265 void *alloc_ctx;
266 265
267 void __iomem *bar0, *bar1; 266 void __iomem *bar0, *bar1;
268 267
diff --git a/drivers/media/pci/cobalt/cobalt-v4l2.c b/drivers/media/pci/cobalt/cobalt-v4l2.c
index c0ba458f6cf3..d05672fe9ff9 100644
--- a/drivers/media/pci/cobalt/cobalt-v4l2.c
+++ b/drivers/media/pci/cobalt/cobalt-v4l2.c
@@ -45,7 +45,7 @@ static const struct v4l2_dv_timings cea1080p60 = V4L2_DV_BT_CEA_1920X1080P60;
45 45
46static int cobalt_queue_setup(struct vb2_queue *q, 46static int cobalt_queue_setup(struct vb2_queue *q,
47 unsigned int *num_buffers, unsigned int *num_planes, 47 unsigned int *num_buffers, unsigned int *num_planes,
48 unsigned int sizes[], void *alloc_ctxs[]) 48 unsigned int sizes[], struct device *alloc_devs[])
49{ 49{
50 struct cobalt_stream *s = q->drv_priv; 50 struct cobalt_stream *s = q->drv_priv;
51 unsigned size = s->stride * s->height; 51 unsigned size = s->stride * s->height;
@@ -54,7 +54,6 @@ static int cobalt_queue_setup(struct vb2_queue *q,
54 *num_buffers = 3; 54 *num_buffers = 3;
55 if (*num_buffers > NR_BUFS) 55 if (*num_buffers > NR_BUFS)
56 *num_buffers = NR_BUFS; 56 *num_buffers = NR_BUFS;
57 alloc_ctxs[0] = s->cobalt->alloc_ctx;
58 if (*num_planes) 57 if (*num_planes)
59 return sizes[0] < size ? -EINVAL : 0; 58 return sizes[0] < size ? -EINVAL : 0;
60 *num_planes = 1; 59 *num_planes = 1;
@@ -1224,6 +1223,7 @@ static int cobalt_node_register(struct cobalt *cobalt, int node)
1224 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC; 1223 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1225 q->min_buffers_needed = 2; 1224 q->min_buffers_needed = 2;
1226 q->lock = &s->lock; 1225 q->lock = &s->lock;
1226 q->dev = &cobalt->pci_dev->dev;
1227 vdev->queue = q; 1227 vdev->queue = q;
1228 1228
1229 video_set_drvdata(vdev, s); 1229 video_set_drvdata(vdev, s);
diff --git a/drivers/media/pci/cx18/cx18-alsa-mixer.c b/drivers/media/pci/cx18/cx18-alsa-mixer.c
index 341bddc00b77..284275270f1b 100644
--- a/drivers/media/pci/cx18/cx18-alsa-mixer.c
+++ b/drivers/media/pci/cx18/cx18-alsa-mixer.c
@@ -93,7 +93,7 @@ static int snd_cx18_mixer_tv_vol_get(struct snd_kcontrol *kctl,
93 vctrl.value = dB_to_cx18_av_vol(uctl->value.integer.value[0]); 93 vctrl.value = dB_to_cx18_av_vol(uctl->value.integer.value[0]);
94 94
95 snd_cx18_lock(cxsc); 95 snd_cx18_lock(cxsc);
96 ret = v4l2_subdev_call(cx->sd_av, core, g_ctrl, &vctrl); 96 ret = v4l2_g_ctrl(cx->sd_av->ctrl_handler, &vctrl);
97 snd_cx18_unlock(cxsc); 97 snd_cx18_unlock(cxsc);
98 98
99 if (!ret) 99 if (!ret)
@@ -115,14 +115,14 @@ static int snd_cx18_mixer_tv_vol_put(struct snd_kcontrol *kctl,
115 snd_cx18_lock(cxsc); 115 snd_cx18_lock(cxsc);
116 116
117 /* Fetch current state */ 117 /* Fetch current state */
118 ret = v4l2_subdev_call(cx->sd_av, core, g_ctrl, &vctrl); 118 ret = v4l2_g_ctrl(cx->sd_av->ctrl_handler, &vctrl);
119 119
120 if (ret || 120 if (ret ||
121 (cx18_av_vol_to_dB(vctrl.value) != uctl->value.integer.value[0])) { 121 (cx18_av_vol_to_dB(vctrl.value) != uctl->value.integer.value[0])) {
122 122
123 /* Set, if needed */ 123 /* Set, if needed */
124 vctrl.value = dB_to_cx18_av_vol(uctl->value.integer.value[0]); 124 vctrl.value = dB_to_cx18_av_vol(uctl->value.integer.value[0]);
125 ret = v4l2_subdev_call(cx->sd_av, core, s_ctrl, &vctrl); 125 ret = v4l2_s_ctrl(cx->sd_av->ctrl_handler, &vctrl);
126 if (!ret) 126 if (!ret)
127 ret = 1; /* Indicate control was changed w/o error */ 127 ret = 1; /* Indicate control was changed w/o error */
128 } 128 }
diff --git a/drivers/media/pci/cx18/cx18-driver.c b/drivers/media/pci/cx18/cx18-driver.c
index 260e462d91b4..2f23b26b16c0 100644
--- a/drivers/media/pci/cx18/cx18-driver.c
+++ b/drivers/media/pci/cx18/cx18-driver.c
@@ -560,7 +560,7 @@ static void cx18_process_options(struct cx18 *cx)
560 cx->stream_buf_size[CX18_ENC_STREAM_TYPE_MPG] = enc_mpg_bufsize; 560 cx->stream_buf_size[CX18_ENC_STREAM_TYPE_MPG] = enc_mpg_bufsize;
561 cx->stream_buf_size[CX18_ENC_STREAM_TYPE_IDX] = enc_idx_bufsize; 561 cx->stream_buf_size[CX18_ENC_STREAM_TYPE_IDX] = enc_idx_bufsize;
562 cx->stream_buf_size[CX18_ENC_STREAM_TYPE_YUV] = enc_yuv_bufsize; 562 cx->stream_buf_size[CX18_ENC_STREAM_TYPE_YUV] = enc_yuv_bufsize;
563 cx->stream_buf_size[CX18_ENC_STREAM_TYPE_VBI] = vbi_active_samples * 36; 563 cx->stream_buf_size[CX18_ENC_STREAM_TYPE_VBI] = VBI_ACTIVE_SAMPLES * 36;
564 cx->stream_buf_size[CX18_ENC_STREAM_TYPE_PCM] = enc_pcm_bufsize; 564 cx->stream_buf_size[CX18_ENC_STREAM_TYPE_PCM] = enc_pcm_bufsize;
565 cx->stream_buf_size[CX18_ENC_STREAM_TYPE_RAD] = 0; /* control no data */ 565 cx->stream_buf_size[CX18_ENC_STREAM_TYPE_RAD] = 0; /* control no data */
566 566
diff --git a/drivers/media/pci/cx18/cx18-driver.h b/drivers/media/pci/cx18/cx18-driver.h
index 47ce80fa73b9..ef308a10e870 100644
--- a/drivers/media/pci/cx18/cx18-driver.h
+++ b/drivers/media/pci/cx18/cx18-driver.h
@@ -492,9 +492,9 @@ struct cx18_card;
492 * (1/15.625 kHz) * 2 * 13.5 MHz = 1728 samples/line = 492 * (1/15.625 kHz) * 2 * 13.5 MHz = 1728 samples/line =
493 * 4 bytes SAV + 280 bytes anc data + 4 bytes SAV + 1440 active samples 493 * 4 bytes SAV + 280 bytes anc data + 4 bytes SAV + 1440 active samples
494 */ 494 */
495static const u32 vbi_active_samples = 1444; /* 4 byte SAV + 720 Y + 720 U/V */ 495#define VBI_ACTIVE_SAMPLES 1444 /* 4 byte SAV + 720 Y + 720 U/V */
496static const u32 vbi_hblank_samples_60Hz = 272; /* 4 byte EAV + 268 anc/fill */ 496#define VBI_HBLANK_SAMPLES_60HZ 272 /* 4 byte EAV + 268 anc/fill */
497static const u32 vbi_hblank_samples_50Hz = 284; /* 4 byte EAV + 280 anc/fill */ 497#define VBI_HBLANK_SAMPLES_50HZ 284 /* 4 byte EAV + 280 anc/fill */
498 498
499#define CX18_VBI_FRAMES 32 499#define CX18_VBI_FRAMES 32
500 500
diff --git a/drivers/media/pci/cx18/cx18-ioctl.c b/drivers/media/pci/cx18/cx18-ioctl.c
index eeb741c7db1b..fecca2a63891 100644
--- a/drivers/media/pci/cx18/cx18-ioctl.c
+++ b/drivers/media/pci/cx18/cx18-ioctl.c
@@ -177,7 +177,7 @@ static int cx18_g_fmt_vbi_cap(struct file *file, void *fh,
177 177
178 vbifmt->sampling_rate = 27000000; 178 vbifmt->sampling_rate = 27000000;
179 vbifmt->offset = 248; /* FIXME - slightly wrong for both 50 & 60 Hz */ 179 vbifmt->offset = 248; /* FIXME - slightly wrong for both 50 & 60 Hz */
180 vbifmt->samples_per_line = vbi_active_samples - 4; 180 vbifmt->samples_per_line = VBI_ACTIVE_SAMPLES - 4;
181 vbifmt->sample_format = V4L2_PIX_FMT_GREY; 181 vbifmt->sample_format = V4L2_PIX_FMT_GREY;
182 vbifmt->start[0] = cx->vbi.start[0]; 182 vbifmt->start[0] = cx->vbi.start[0];
183 vbifmt->start[1] = cx->vbi.start[1]; 183 vbifmt->start[1] = cx->vbi.start[1];
diff --git a/drivers/media/pci/cx18/cx18-streams.c b/drivers/media/pci/cx18/cx18-streams.c
index c9860845264f..f3802ec1b383 100644
--- a/drivers/media/pci/cx18/cx18-streams.c
+++ b/drivers/media/pci/cx18/cx18-streams.c
@@ -605,9 +605,9 @@ static void cx18_vbi_setup(struct cx18_stream *s)
605 /* Lines per field */ 605 /* Lines per field */
606 data[1] = (lines / 2) | ((lines / 2) << 16); 606 data[1] = (lines / 2) | ((lines / 2) << 16);
607 /* bytes per line */ 607 /* bytes per line */
608 data[2] = (raw ? vbi_active_samples 608 data[2] = (raw ? VBI_ACTIVE_SAMPLES
609 : (cx->is_60hz ? vbi_hblank_samples_60Hz 609 : (cx->is_60hz ? VBI_HBLANK_SAMPLES_60HZ
610 : vbi_hblank_samples_50Hz)); 610 : VBI_HBLANK_SAMPLES_50HZ));
611 /* Every X number of frames a VBI interrupt arrives 611 /* Every X number of frames a VBI interrupt arrives
612 (frames as in 25 or 30 fps) */ 612 (frames as in 25 or 30 fps) */
613 data[3] = 1; 613 data[3] = 1;
@@ -761,7 +761,7 @@ static void cx18_stream_configure_mdls(struct cx18_stream *s)
761 s->bufs_per_mdl = 1; 761 s->bufs_per_mdl = 1;
762 if (cx18_raw_vbi(s->cx)) { 762 if (cx18_raw_vbi(s->cx)) {
763 s->mdl_size = (s->cx->is_60hz ? 12 : 18) 763 s->mdl_size = (s->cx->is_60hz ? 12 : 18)
764 * 2 * vbi_active_samples; 764 * 2 * VBI_ACTIVE_SAMPLES;
765 } else { 765 } else {
766 /* 766 /*
767 * See comment in cx18_vbi_setup() below about the 767 * See comment in cx18_vbi_setup() below about the
@@ -769,8 +769,8 @@ static void cx18_stream_configure_mdls(struct cx18_stream *s)
769 * the lines on which EAV RP codes toggle. 769 * the lines on which EAV RP codes toggle.
770 */ 770 */
771 s->mdl_size = s->cx->is_60hz 771 s->mdl_size = s->cx->is_60hz
772 ? (21 - 4 + 1) * 2 * vbi_hblank_samples_60Hz 772 ? (21 - 4 + 1) * 2 * VBI_HBLANK_SAMPLES_60HZ
773 : (23 - 2 + 1) * 2 * vbi_hblank_samples_50Hz; 773 : (23 - 2 + 1) * 2 * VBI_HBLANK_SAMPLES_50HZ;
774 } 774 }
775 break; 775 break;
776 default: 776 default:
diff --git a/drivers/media/pci/cx18/cx18-vbi.c b/drivers/media/pci/cx18/cx18-vbi.c
index add99642f1e2..43360cbcf24b 100644
--- a/drivers/media/pci/cx18/cx18-vbi.c
+++ b/drivers/media/pci/cx18/cx18-vbi.c
@@ -108,7 +108,7 @@ static void copy_vbi_data(struct cx18 *cx, int lines, u32 pts_stamp)
108/* FIXME - this function ignores the input size. */ 108/* FIXME - this function ignores the input size. */
109static u32 compress_raw_buf(struct cx18 *cx, u8 *buf, u32 size, u32 hdr_size) 109static u32 compress_raw_buf(struct cx18 *cx, u8 *buf, u32 size, u32 hdr_size)
110{ 110{
111 u32 line_size = vbi_active_samples; 111 u32 line_size = VBI_ACTIVE_SAMPLES;
112 u32 lines = cx->vbi.count * 2; 112 u32 lines = cx->vbi.count * 2;
113 u8 *q = buf; 113 u8 *q = buf;
114 u8 *p; 114 u8 *p;
@@ -145,8 +145,8 @@ static u32 compress_sliced_buf(struct cx18 *cx, u8 *buf, u32 size,
145 struct v4l2_decode_vbi_line vbi; 145 struct v4l2_decode_vbi_line vbi;
146 int i; 146 int i;
147 u32 line = 0; 147 u32 line = 0;
148 u32 line_size = cx->is_60hz ? vbi_hblank_samples_60Hz 148 u32 line_size = cx->is_60hz ? VBI_HBLANK_SAMPLES_60HZ
149 : vbi_hblank_samples_50Hz; 149 : VBI_HBLANK_SAMPLES_50HZ;
150 150
151 /* find the first valid line */ 151 /* find the first valid line */
152 for (i = hdr_size, buf += hdr_size; i < size; i++, buf++) { 152 for (i = hdr_size, buf += hdr_size; i < size; i++, buf++) {
diff --git a/drivers/media/pci/cx23885/cx23885-417.c b/drivers/media/pci/cx23885/cx23885-417.c
index bd333875a1f7..efec2d1a7afd 100644
--- a/drivers/media/pci/cx23885/cx23885-417.c
+++ b/drivers/media/pci/cx23885/cx23885-417.c
@@ -1140,7 +1140,7 @@ static int cx23885_initialize_codec(struct cx23885_dev *dev, int startencoder)
1140 1140
1141static int queue_setup(struct vb2_queue *q, 1141static int queue_setup(struct vb2_queue *q,
1142 unsigned int *num_buffers, unsigned int *num_planes, 1142 unsigned int *num_buffers, unsigned int *num_planes,
1143 unsigned int sizes[], void *alloc_ctxs[]) 1143 unsigned int sizes[], struct device *alloc_devs[])
1144{ 1144{
1145 struct cx23885_dev *dev = q->drv_priv; 1145 struct cx23885_dev *dev = q->drv_priv;
1146 1146
@@ -1148,7 +1148,6 @@ static int queue_setup(struct vb2_queue *q,
1148 dev->ts1.ts_packet_count = mpeglines; 1148 dev->ts1.ts_packet_count = mpeglines;
1149 *num_planes = 1; 1149 *num_planes = 1;
1150 sizes[0] = mpeglinesize * mpeglines; 1150 sizes[0] = mpeglinesize * mpeglines;
1151 alloc_ctxs[0] = dev->alloc_ctx;
1152 *num_buffers = mpegbufs; 1151 *num_buffers = mpegbufs;
1153 return 0; 1152 return 0;
1154} 1153}
diff --git a/drivers/media/pci/cx23885/cx23885-cards.c b/drivers/media/pci/cx23885/cx23885-cards.c
index 310ee769aed4..4abf50f2694f 100644
--- a/drivers/media/pci/cx23885/cx23885-cards.c
+++ b/drivers/media/pci/cx23885/cx23885-cards.c
@@ -765,6 +765,11 @@ struct cx23885_board cx23885_boards[] = {
765 .amux = CX25840_AUDIO7, 765 .amux = CX25840_AUDIO7,
766 } }, 766 } },
767 }, 767 },
768 [CX23885_BOARD_HAUPPAUGE_QUADHD_DVB] = {
769 .name = "Hauppauge WinTV-QuadHD-DVB",
770 .portb = CX23885_MPEG_DVB,
771 .portc = CX23885_MPEG_DVB,
772 },
768}; 773};
769const unsigned int cx23885_bcount = ARRAY_SIZE(cx23885_boards); 774const unsigned int cx23885_bcount = ARRAY_SIZE(cx23885_boards);
770 775
@@ -1060,6 +1065,14 @@ struct cx23885_subid cx23885_subids[] = {
1060 .subvendor = 0x1576, 1065 .subvendor = 0x1576,
1061 .subdevice = 0x0460, 1066 .subdevice = 0x0460,
1062 .card = CX23885_BOARD_VIEWCAST_460E, 1067 .card = CX23885_BOARD_VIEWCAST_460E,
1068 }, {
1069 .subvendor = 0x0070,
1070 .subdevice = 0x6a28,
1071 .card = CX23885_BOARD_HAUPPAUGE_QUADHD_DVB, /* Tuner Pair 1 */
1072 }, {
1073 .subvendor = 0x0070,
1074 .subdevice = 0x6b28,
1075 .card = CX23885_BOARD_HAUPPAUGE_QUADHD_DVB, /* Tuner Pair 2 */
1063 }, 1076 },
1064}; 1077};
1065const unsigned int cx23885_idcount = ARRAY_SIZE(cx23885_subids); 1078const unsigned int cx23885_idcount = ARRAY_SIZE(cx23885_subids);
@@ -1257,6 +1270,14 @@ static void hauppauge_eeprom(struct cx23885_dev *dev, u8 *eeprom_data)
1257 case 150329: 1270 case 150329:
1258 /* WinTV-HVR5525 (PCIe, DVB-S/S2, DVB-T/T2/C) */ 1271 /* WinTV-HVR5525 (PCIe, DVB-S/S2, DVB-T/T2/C) */
1259 break; 1272 break;
1273 case 166100:
1274 /* WinTV-QuadHD (DVB) Tuner Pair 1 (PCIe, IR, half height,
1275 DVB-T/T2/C, DVB-T/T2/C */
1276 break;
1277 case 166101:
1278 /* WinTV-QuadHD (DVB) Tuner Pair 2 (PCIe, IR, half height,
1279 DVB-T/T2/C, DVB-T/T2/C */
1280 break;
1260 default: 1281 default:
1261 printk(KERN_WARNING "%s: warning: " 1282 printk(KERN_WARNING "%s: warning: "
1262 "unknown hauppauge model #%d\n", 1283 "unknown hauppauge model #%d\n",
@@ -1729,20 +1750,22 @@ void cx23885_gpio_setup(struct cx23885_dev *dev)
1729 cx23885_gpio_set(dev, GPIO_2); 1750 cx23885_gpio_set(dev, GPIO_2);
1730 break; 1751 break;
1731 case CX23885_BOARD_HAUPPAUGE_HVR5525: 1752 case CX23885_BOARD_HAUPPAUGE_HVR5525:
1753 case CX23885_BOARD_HAUPPAUGE_QUADHD_DVB:
1732 /* 1754 /*
1733 * GPIO-00 IR_WIDE 1755 * HVR5525 GPIO Details:
1734 * GPIO-02 wake# 1756 * GPIO-00 IR_WIDE
1735 * GPIO-03 VAUX Pres. 1757 * GPIO-02 wake#
1736 * GPIO-07 PROG# 1758 * GPIO-03 VAUX Pres.
1737 * GPIO-08 SAT_RESN 1759 * GPIO-07 PROG#
1738 * GPIO-09 TER_RESN 1760 * GPIO-08 SAT_RESN
1739 * GPIO-10 B2_SENSE 1761 * GPIO-09 TER_RESN
1740 * GPIO-11 B1_SENSE 1762 * GPIO-10 B2_SENSE
1741 * GPIO-15 IR_LED_STATUS 1763 * GPIO-11 B1_SENSE
1742 * GPIO-19 IR_NARROW 1764 * GPIO-15 IR_LED_STATUS
1743 * GPIO-20 Blauster1 1765 * GPIO-19 IR_NARROW
1744 * ALTGPIO VAUX_SWITCH 1766 * GPIO-20 Blauster1
1745 * AUX_PLL_CLK : Blaster2 1767 * ALTGPIO VAUX_SWITCH
1768 * AUX_PLL_CLK : Blaster2
1746 */ 1769 */
1747 /* Put the parts into reset and back */ 1770 /* Put the parts into reset and back */
1748 cx23885_gpio_enable(dev, GPIO_8 | GPIO_9, 1); 1771 cx23885_gpio_enable(dev, GPIO_8 | GPIO_9, 1);
@@ -1802,6 +1825,7 @@ int cx23885_ir_init(struct cx23885_dev *dev)
1802 case CX23885_BOARD_HAUPPAUGE_HVR1255: 1825 case CX23885_BOARD_HAUPPAUGE_HVR1255:
1803 case CX23885_BOARD_HAUPPAUGE_HVR1255_22111: 1826 case CX23885_BOARD_HAUPPAUGE_HVR1255_22111:
1804 case CX23885_BOARD_HAUPPAUGE_HVR1210: 1827 case CX23885_BOARD_HAUPPAUGE_HVR1210:
1828 case CX23885_BOARD_HAUPPAUGE_QUADHD_DVB:
1805 /* FIXME: Implement me */ 1829 /* FIXME: Implement me */
1806 break; 1830 break;
1807 case CX23885_BOARD_HAUPPAUGE_HVR1270: 1831 case CX23885_BOARD_HAUPPAUGE_HVR1270:
@@ -2000,6 +2024,7 @@ void cx23885_card_setup(struct cx23885_dev *dev)
2000 case CX23885_BOARD_HAUPPAUGE_STARBURST: 2024 case CX23885_BOARD_HAUPPAUGE_STARBURST:
2001 case CX23885_BOARD_HAUPPAUGE_IMPACTVCBE: 2025 case CX23885_BOARD_HAUPPAUGE_IMPACTVCBE:
2002 case CX23885_BOARD_HAUPPAUGE_HVR5525: 2026 case CX23885_BOARD_HAUPPAUGE_HVR5525:
2027 case CX23885_BOARD_HAUPPAUGE_QUADHD_DVB:
2003 if (dev->i2c_bus[0].i2c_rc == 0) 2028 if (dev->i2c_bus[0].i2c_rc == 0)
2004 hauppauge_eeprom(dev, eeprom+0xc0); 2029 hauppauge_eeprom(dev, eeprom+0xc0);
2005 break; 2030 break;
@@ -2145,6 +2170,14 @@ void cx23885_card_setup(struct cx23885_dev *dev)
2145 ts2->ts_clk_en_val = 0x1; /* Enable TS_CLK */ 2170 ts2->ts_clk_en_val = 0x1; /* Enable TS_CLK */
2146 ts2->src_sel_val = CX23885_SRC_SEL_PARALLEL_MPEG_VIDEO; 2171 ts2->src_sel_val = CX23885_SRC_SEL_PARALLEL_MPEG_VIDEO;
2147 break; 2172 break;
2173 case CX23885_BOARD_HAUPPAUGE_QUADHD_DVB:
2174 ts1->gen_ctrl_val = 0xc; /* Serial bus + punctured clock */
2175 ts1->ts_clk_en_val = 0x1; /* Enable TS_CLK */
2176 ts1->src_sel_val = CX23885_SRC_SEL_PARALLEL_MPEG_VIDEO;
2177 ts2->gen_ctrl_val = 0xc; /* Serial bus + punctured clock */
2178 ts2->ts_clk_en_val = 0x1; /* Enable TS_CLK */
2179 ts2->src_sel_val = CX23885_SRC_SEL_PARALLEL_MPEG_VIDEO;
2180 break;
2148 case CX23885_BOARD_HAUPPAUGE_HVR1250: 2181 case CX23885_BOARD_HAUPPAUGE_HVR1250:
2149 case CX23885_BOARD_HAUPPAUGE_HVR1500: 2182 case CX23885_BOARD_HAUPPAUGE_HVR1500:
2150 case CX23885_BOARD_HAUPPAUGE_HVR1500Q: 2183 case CX23885_BOARD_HAUPPAUGE_HVR1500Q:
diff --git a/drivers/media/pci/cx23885/cx23885-core.c b/drivers/media/pci/cx23885/cx23885-core.c
index 813c217b5e1a..c86b1093ab99 100644
--- a/drivers/media/pci/cx23885/cx23885-core.c
+++ b/drivers/media/pci/cx23885/cx23885-core.c
@@ -2005,14 +2005,9 @@ static int cx23885_initdev(struct pci_dev *pci_dev,
2005 err = pci_set_dma_mask(pci_dev, 0xffffffff); 2005 err = pci_set_dma_mask(pci_dev, 0xffffffff);
2006 if (err) { 2006 if (err) {
2007 printk("%s/0: Oops: no 32bit PCI DMA ???\n", dev->name); 2007 printk("%s/0: Oops: no 32bit PCI DMA ???\n", dev->name);
2008 goto fail_context; 2008 goto fail_ctrl;
2009 } 2009 }
2010 2010
2011 dev->alloc_ctx = vb2_dma_sg_init_ctx(&pci_dev->dev);
2012 if (IS_ERR(dev->alloc_ctx)) {
2013 err = PTR_ERR(dev->alloc_ctx);
2014 goto fail_context;
2015 }
2016 err = request_irq(pci_dev->irq, cx23885_irq, 2011 err = request_irq(pci_dev->irq, cx23885_irq,
2017 IRQF_SHARED, dev->name, dev); 2012 IRQF_SHARED, dev->name, dev);
2018 if (err < 0) { 2013 if (err < 0) {
@@ -2041,8 +2036,6 @@ static int cx23885_initdev(struct pci_dev *pci_dev,
2041 return 0; 2036 return 0;
2042 2037
2043fail_irq: 2038fail_irq:
2044 vb2_dma_sg_cleanup_ctx(dev->alloc_ctx);
2045fail_context:
2046 cx23885_dev_unregister(dev); 2039 cx23885_dev_unregister(dev);
2047fail_ctrl: 2040fail_ctrl:
2048 v4l2_ctrl_handler_free(hdl); 2041 v4l2_ctrl_handler_free(hdl);
@@ -2068,7 +2061,6 @@ static void cx23885_finidev(struct pci_dev *pci_dev)
2068 pci_disable_device(pci_dev); 2061 pci_disable_device(pci_dev);
2069 2062
2070 cx23885_dev_unregister(dev); 2063 cx23885_dev_unregister(dev);
2071 vb2_dma_sg_cleanup_ctx(dev->alloc_ctx);
2072 v4l2_ctrl_handler_free(&dev->ctrl_handler); 2064 v4l2_ctrl_handler_free(&dev->ctrl_handler);
2073 v4l2_device_unregister(v4l2_dev); 2065 v4l2_device_unregister(v4l2_dev);
2074 kfree(dev); 2066 kfree(dev);
diff --git a/drivers/media/pci/cx23885/cx23885-dvb.c b/drivers/media/pci/cx23885/cx23885-dvb.c
index f041b6931ba8..e5748a93c479 100644
--- a/drivers/media/pci/cx23885/cx23885-dvb.c
+++ b/drivers/media/pci/cx23885/cx23885-dvb.c
@@ -94,7 +94,7 @@ DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
94 94
95static int queue_setup(struct vb2_queue *q, 95static int queue_setup(struct vb2_queue *q,
96 unsigned int *num_buffers, unsigned int *num_planes, 96 unsigned int *num_buffers, unsigned int *num_planes,
97 unsigned int sizes[], void *alloc_ctxs[]) 97 unsigned int sizes[], struct device *alloc_devs[])
98{ 98{
99 struct cx23885_tsport *port = q->drv_priv; 99 struct cx23885_tsport *port = q->drv_priv;
100 100
@@ -102,7 +102,6 @@ static int queue_setup(struct vb2_queue *q,
102 port->ts_packet_count = 32; 102 port->ts_packet_count = 32;
103 *num_planes = 1; 103 *num_planes = 1;
104 sizes[0] = port->ts_packet_size * port->ts_packet_count; 104 sizes[0] = port->ts_packet_size * port->ts_packet_count;
105 alloc_ctxs[0] = port->dev->alloc_ctx;
106 *num_buffers = 32; 105 *num_buffers = 32;
107 return 0; 106 return 0;
108} 107}
@@ -2269,9 +2268,107 @@ static int dvb_register(struct cx23885_tsport *port)
2269 } 2268 }
2270 break; 2269 break;
2271 } 2270 }
2271 case CX23885_BOARD_HAUPPAUGE_QUADHD_DVB:
2272 switch (port->nr) {
2273 /* port b - Terrestrial/cable */
2274 case 1:
2275 /* attach frontend */
2276 memset(&si2168_config, 0, sizeof(si2168_config));
2277 si2168_config.i2c_adapter = &adapter;
2278 si2168_config.fe = &fe0->dvb.frontend;
2279 si2168_config.ts_mode = SI2168_TS_SERIAL;
2280 memset(&info, 0, sizeof(struct i2c_board_info));
2281 strlcpy(info.type, "si2168", I2C_NAME_SIZE);
2282 info.addr = 0x64;
2283 info.platform_data = &si2168_config;
2284 request_module("%s", info.type);
2285 client_demod = i2c_new_device(&dev->i2c_bus[0].i2c_adap, &info);
2286 if (!client_demod || !client_demod->dev.driver)
2287 goto frontend_detach;
2288 if (!try_module_get(client_demod->dev.driver->owner)) {
2289 i2c_unregister_device(client_demod);
2290 goto frontend_detach;
2291 }
2292 port->i2c_client_demod = client_demod;
2293
2294 /* attach tuner */
2295 memset(&si2157_config, 0, sizeof(si2157_config));
2296 si2157_config.fe = fe0->dvb.frontend;
2297 si2157_config.if_port = 1;
2298 memset(&info, 0, sizeof(struct i2c_board_info));
2299 strlcpy(info.type, "si2157", I2C_NAME_SIZE);
2300 info.addr = 0x60;
2301 info.platform_data = &si2157_config;
2302 request_module("%s", info.type);
2303 client_tuner = i2c_new_device(&dev->i2c_bus[1].i2c_adap, &info);
2304 if (!client_tuner || !client_tuner->dev.driver) {
2305 module_put(client_demod->dev.driver->owner);
2306 i2c_unregister_device(client_demod);
2307 port->i2c_client_demod = NULL;
2308 goto frontend_detach;
2309 }
2310 if (!try_module_get(client_tuner->dev.driver->owner)) {
2311 i2c_unregister_device(client_tuner);
2312 module_put(client_demod->dev.driver->owner);
2313 i2c_unregister_device(client_demod);
2314 port->i2c_client_demod = NULL;
2315 goto frontend_detach;
2316 }
2317 port->i2c_client_tuner = client_tuner;
2318 break;
2319
2320 /* port c - terrestrial/cable */
2321 case 2:
2322 /* attach frontend */
2323 memset(&si2168_config, 0, sizeof(si2168_config));
2324 si2168_config.i2c_adapter = &adapter;
2325 si2168_config.fe = &fe0->dvb.frontend;
2326 si2168_config.ts_mode = SI2168_TS_SERIAL;
2327 memset(&info, 0, sizeof(struct i2c_board_info));
2328 strlcpy(info.type, "si2168", I2C_NAME_SIZE);
2329 info.addr = 0x66;
2330 info.platform_data = &si2168_config;
2331 request_module("%s", info.type);
2332 client_demod = i2c_new_device(&dev->i2c_bus[0].i2c_adap, &info);
2333 if (!client_demod || !client_demod->dev.driver)
2334 goto frontend_detach;
2335 if (!try_module_get(client_demod->dev.driver->owner)) {
2336 i2c_unregister_device(client_demod);
2337 goto frontend_detach;
2338 }
2339 port->i2c_client_demod = client_demod;
2340
2341 /* attach tuner */
2342 memset(&si2157_config, 0, sizeof(si2157_config));
2343 si2157_config.fe = fe0->dvb.frontend;
2344 si2157_config.if_port = 1;
2345 memset(&info, 0, sizeof(struct i2c_board_info));
2346 strlcpy(info.type, "si2157", I2C_NAME_SIZE);
2347 info.addr = 0x62;
2348 info.platform_data = &si2157_config;
2349 request_module("%s", info.type);
2350 client_tuner = i2c_new_device(&dev->i2c_bus[1].i2c_adap, &info);
2351 if (!client_tuner || !client_tuner->dev.driver) {
2352 module_put(client_demod->dev.driver->owner);
2353 i2c_unregister_device(client_demod);
2354 port->i2c_client_demod = NULL;
2355 goto frontend_detach;
2356 }
2357 if (!try_module_get(client_tuner->dev.driver->owner)) {
2358 i2c_unregister_device(client_tuner);
2359 module_put(client_demod->dev.driver->owner);
2360 i2c_unregister_device(client_demod);
2361 port->i2c_client_demod = NULL;
2362 goto frontend_detach;
2363 }
2364 port->i2c_client_tuner = client_tuner;
2365 break;
2366 }
2367 break;
2368
2272 default: 2369 default:
2273 printk(KERN_INFO "%s: The frontend of your DVB/ATSC card " 2370 printk(KERN_INFO "%s: The frontend of your DVB/ATSC card "
2274 " isn't supported yet\n", 2371 " isn't supported yet\n",
2275 dev->name); 2372 dev->name);
2276 break; 2373 break;
2277 } 2374 }
@@ -2397,6 +2494,7 @@ int cx23885_dvb_register(struct cx23885_tsport *port)
2397 q->mem_ops = &vb2_dma_sg_memops; 2494 q->mem_ops = &vb2_dma_sg_memops;
2398 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC; 2495 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
2399 q->lock = &dev->lock; 2496 q->lock = &dev->lock;
2497 q->dev = &dev->pci->dev;
2400 2498
2401 err = vb2_queue_init(q); 2499 err = vb2_queue_init(q);
2402 if (err < 0) 2500 if (err < 0)
diff --git a/drivers/media/pci/cx23885/cx23885-vbi.c b/drivers/media/pci/cx23885/cx23885-vbi.c
index 39750ebcc04c..75e7fa7b1121 100644
--- a/drivers/media/pci/cx23885/cx23885-vbi.c
+++ b/drivers/media/pci/cx23885/cx23885-vbi.c
@@ -122,7 +122,7 @@ static int cx23885_start_vbi_dma(struct cx23885_dev *dev,
122 122
123static int queue_setup(struct vb2_queue *q, 123static int queue_setup(struct vb2_queue *q,
124 unsigned int *num_buffers, unsigned int *num_planes, 124 unsigned int *num_buffers, unsigned int *num_planes,
125 unsigned int sizes[], void *alloc_ctxs[]) 125 unsigned int sizes[], struct device *alloc_devs[])
126{ 126{
127 struct cx23885_dev *dev = q->drv_priv; 127 struct cx23885_dev *dev = q->drv_priv;
128 unsigned lines = VBI_PAL_LINE_COUNT; 128 unsigned lines = VBI_PAL_LINE_COUNT;
@@ -131,7 +131,6 @@ static int queue_setup(struct vb2_queue *q,
131 lines = VBI_NTSC_LINE_COUNT; 131 lines = VBI_NTSC_LINE_COUNT;
132 *num_planes = 1; 132 *num_planes = 1;
133 sizes[0] = lines * VBI_LINE_LENGTH * 2; 133 sizes[0] = lines * VBI_LINE_LENGTH * 2;
134 alloc_ctxs[0] = dev->alloc_ctx;
135 return 0; 134 return 0;
136} 135}
137 136
diff --git a/drivers/media/pci/cx23885/cx23885-video.c b/drivers/media/pci/cx23885/cx23885-video.c
index e1d7d0847167..6d735222a958 100644
--- a/drivers/media/pci/cx23885/cx23885-video.c
+++ b/drivers/media/pci/cx23885/cx23885-video.c
@@ -335,13 +335,12 @@ static int cx23885_start_video_dma(struct cx23885_dev *dev,
335 335
336static int queue_setup(struct vb2_queue *q, 336static int queue_setup(struct vb2_queue *q,
337 unsigned int *num_buffers, unsigned int *num_planes, 337 unsigned int *num_buffers, unsigned int *num_planes,
338 unsigned int sizes[], void *alloc_ctxs[]) 338 unsigned int sizes[], struct device *alloc_devs[])
339{ 339{
340 struct cx23885_dev *dev = q->drv_priv; 340 struct cx23885_dev *dev = q->drv_priv;
341 341
342 *num_planes = 1; 342 *num_planes = 1;
343 sizes[0] = (dev->fmt->depth * dev->width * dev->height) >> 3; 343 sizes[0] = (dev->fmt->depth * dev->width * dev->height) >> 3;
344 alloc_ctxs[0] = dev->alloc_ctx;
345 return 0; 344 return 0;
346} 345}
347 346
@@ -1268,6 +1267,7 @@ int cx23885_video_register(struct cx23885_dev *dev)
1268 q->mem_ops = &vb2_dma_sg_memops; 1267 q->mem_ops = &vb2_dma_sg_memops;
1269 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC; 1268 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1270 q->lock = &dev->lock; 1269 q->lock = &dev->lock;
1270 q->dev = &dev->pci->dev;
1271 1271
1272 err = vb2_queue_init(q); 1272 err = vb2_queue_init(q);
1273 if (err < 0) 1273 if (err < 0)
@@ -1284,6 +1284,7 @@ int cx23885_video_register(struct cx23885_dev *dev)
1284 q->mem_ops = &vb2_dma_sg_memops; 1284 q->mem_ops = &vb2_dma_sg_memops;
1285 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC; 1285 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1286 q->lock = &dev->lock; 1286 q->lock = &dev->lock;
1287 q->dev = &dev->pci->dev;
1287 1288
1288 err = vb2_queue_init(q); 1289 err = vb2_queue_init(q);
1289 if (err < 0) 1290 if (err < 0)
diff --git a/drivers/media/pci/cx23885/cx23885.h b/drivers/media/pci/cx23885/cx23885.h
index b1a5409408c7..24a0a6c5b501 100644
--- a/drivers/media/pci/cx23885/cx23885.h
+++ b/drivers/media/pci/cx23885/cx23885.h
@@ -103,6 +103,7 @@
103#define CX23885_BOARD_HAUPPAUGE_STARBURST 53 103#define CX23885_BOARD_HAUPPAUGE_STARBURST 53
104#define CX23885_BOARD_VIEWCAST_260E 54 104#define CX23885_BOARD_VIEWCAST_260E 54
105#define CX23885_BOARD_VIEWCAST_460E 55 105#define CX23885_BOARD_VIEWCAST_460E 55
106#define CX23885_BOARD_HAUPPAUGE_QUADHD_DVB 56
106 107
107#define GPIO_0 0x00000001 108#define GPIO_0 0x00000001
108#define GPIO_1 0x00000002 109#define GPIO_1 0x00000002
@@ -430,7 +431,6 @@ struct cx23885_dev {
430 struct vb2_queue vb2_vidq; 431 struct vb2_queue vb2_vidq;
431 struct cx23885_dmaqueue vbiq; 432 struct cx23885_dmaqueue vbiq;
432 struct vb2_queue vb2_vbiq; 433 struct vb2_queue vb2_vbiq;
433 void *alloc_ctx;
434 434
435 spinlock_t slock; 435 spinlock_t slock;
436 436
diff --git a/drivers/media/pci/cx25821/cx25821-alsa.c b/drivers/media/pci/cx25821/cx25821-alsa.c
index b602eba2b601..df189b16af12 100644
--- a/drivers/media/pci/cx25821/cx25821-alsa.c
+++ b/drivers/media/pci/cx25821/cx25821-alsa.c
@@ -693,7 +693,7 @@ static int snd_cx25821_pcm(struct cx25821_audio_dev *chip, int device,
693 * Only boards with eeprom and byte 1 at eeprom=1 have it 693 * Only boards with eeprom and byte 1 at eeprom=1 have it
694 */ 694 */
695 695
696static const struct pci_device_id cx25821_audio_pci_tbl[] = { 696static const struct pci_device_id __maybe_unused cx25821_audio_pci_tbl[] = {
697 {0x14f1, 0x0920, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0}, 697 {0x14f1, 0x0920, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
698 {0,} 698 {0,}
699}; 699};
diff --git a/drivers/media/pci/cx25821/cx25821-core.c b/drivers/media/pci/cx25821/cx25821-core.c
index 0042803a9de7..9a5f912ca859 100644
--- a/drivers/media/pci/cx25821/cx25821-core.c
+++ b/drivers/media/pci/cx25821/cx25821-core.c
@@ -1301,15 +1301,10 @@ static int cx25821_initdev(struct pci_dev *pci_dev,
1301 1301
1302 goto fail_unregister_device; 1302 goto fail_unregister_device;
1303 } 1303 }
1304 dev->alloc_ctx = vb2_dma_sg_init_ctx(&pci_dev->dev);
1305 if (IS_ERR(dev->alloc_ctx)) {
1306 err = PTR_ERR(dev->alloc_ctx);
1307 goto fail_unregister_pci;
1308 }
1309 1304
1310 err = cx25821_dev_setup(dev); 1305 err = cx25821_dev_setup(dev);
1311 if (err) 1306 if (err)
1312 goto fail_free_ctx; 1307 goto fail_unregister_pci;
1313 1308
1314 /* print pci info */ 1309 /* print pci info */
1315 pci_read_config_byte(pci_dev, PCI_CLASS_REVISION, &dev->pci_rev); 1310 pci_read_config_byte(pci_dev, PCI_CLASS_REVISION, &dev->pci_rev);
@@ -1340,8 +1335,6 @@ fail_irq:
1340 pr_info("cx25821_initdev() can't get IRQ !\n"); 1335 pr_info("cx25821_initdev() can't get IRQ !\n");
1341 cx25821_dev_unregister(dev); 1336 cx25821_dev_unregister(dev);
1342 1337
1343fail_free_ctx:
1344 vb2_dma_sg_cleanup_ctx(dev->alloc_ctx);
1345fail_unregister_pci: 1338fail_unregister_pci:
1346 pci_disable_device(pci_dev); 1339 pci_disable_device(pci_dev);
1347fail_unregister_device: 1340fail_unregister_device:
@@ -1365,7 +1358,6 @@ static void cx25821_finidev(struct pci_dev *pci_dev)
1365 free_irq(pci_dev->irq, dev); 1358 free_irq(pci_dev->irq, dev);
1366 1359
1367 cx25821_dev_unregister(dev); 1360 cx25821_dev_unregister(dev);
1368 vb2_dma_sg_cleanup_ctx(dev->alloc_ctx);
1369 v4l2_device_unregister(v4l2_dev); 1361 v4l2_device_unregister(v4l2_dev);
1370 kfree(dev); 1362 kfree(dev);
1371} 1363}
diff --git a/drivers/media/pci/cx25821/cx25821-video.c b/drivers/media/pci/cx25821/cx25821-video.c
index c48bba9daf1f..adcd09be347d 100644
--- a/drivers/media/pci/cx25821/cx25821-video.c
+++ b/drivers/media/pci/cx25821/cx25821-video.c
@@ -143,13 +143,11 @@ int cx25821_video_irq(struct cx25821_dev *dev, int chan_num, u32 status)
143 143
144static int cx25821_queue_setup(struct vb2_queue *q, 144static int cx25821_queue_setup(struct vb2_queue *q,
145 unsigned int *num_buffers, unsigned int *num_planes, 145 unsigned int *num_buffers, unsigned int *num_planes,
146 unsigned int sizes[], void *alloc_ctxs[]) 146 unsigned int sizes[], struct device *alloc_devs[])
147{ 147{
148 struct cx25821_channel *chan = q->drv_priv; 148 struct cx25821_channel *chan = q->drv_priv;
149 unsigned size = (chan->fmt->depth * chan->width * chan->height) >> 3; 149 unsigned size = (chan->fmt->depth * chan->width * chan->height) >> 3;
150 150
151 alloc_ctxs[0] = chan->dev->alloc_ctx;
152
153 if (*num_planes) 151 if (*num_planes)
154 return sizes[0] < size ? -EINVAL : 0; 152 return sizes[0] < size ? -EINVAL : 0;
155 153
@@ -759,6 +757,7 @@ int cx25821_video_register(struct cx25821_dev *dev)
759 q->mem_ops = &vb2_dma_sg_memops; 757 q->mem_ops = &vb2_dma_sg_memops;
760 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC; 758 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
761 q->lock = &dev->lock; 759 q->lock = &dev->lock;
760 q->dev = &dev->pci->dev;
762 761
763 if (!is_output) { 762 if (!is_output) {
764 err = vb2_queue_init(q); 763 err = vb2_queue_init(q);
diff --git a/drivers/media/pci/cx25821/cx25821.h b/drivers/media/pci/cx25821/cx25821.h
index a513b68be0fa..35c7375e4617 100644
--- a/drivers/media/pci/cx25821/cx25821.h
+++ b/drivers/media/pci/cx25821/cx25821.h
@@ -249,7 +249,6 @@ struct cx25821_dev {
249 int hwrevision; 249 int hwrevision;
250 /* used by cx25821-alsa */ 250 /* used by cx25821-alsa */
251 struct snd_card *card; 251 struct snd_card *card;
252 void *alloc_ctx;
253 252
254 u32 clk_freq; 253 u32 clk_freq;
255 254
diff --git a/drivers/media/pci/cx88/cx88-alsa.c b/drivers/media/pci/cx88/cx88-alsa.c
index e158a1da1d41..f3f13eb0c16e 100644
--- a/drivers/media/pci/cx88/cx88-alsa.c
+++ b/drivers/media/pci/cx88/cx88-alsa.c
@@ -799,13 +799,9 @@ static int snd_cx88_alc_put(struct snd_kcontrol *kcontrol,
799{ 799{
800 snd_cx88_card_t *chip = snd_kcontrol_chip(kcontrol); 800 snd_cx88_card_t *chip = snd_kcontrol_chip(kcontrol);
801 struct cx88_core *core = chip->core; 801 struct cx88_core *core = chip->core;
802 struct v4l2_control client_ctl;
803
804 memset(&client_ctl, 0, sizeof(client_ctl));
805 client_ctl.value = 0 != value->value.integer.value[0];
806 client_ctl.id = V4L2_CID_AUDIO_LOUDNESS;
807 call_hw(core, WM8775_GID, core, s_ctrl, &client_ctl);
808 802
803 wm8775_s_ctrl(core, V4L2_CID_AUDIO_LOUDNESS,
804 value->value.integer.value[0] != 0);
809 return 0; 805 return 0;
810} 806}
811 807
diff --git a/drivers/media/pci/cx88/cx88-blackbird.c b/drivers/media/pci/cx88/cx88-blackbird.c
index 3233d45d1e5b..04fe9af2a802 100644
--- a/drivers/media/pci/cx88/cx88-blackbird.c
+++ b/drivers/media/pci/cx88/cx88-blackbird.c
@@ -639,7 +639,7 @@ static int blackbird_stop_codec(struct cx8802_dev *dev)
639 639
640static int queue_setup(struct vb2_queue *q, 640static int queue_setup(struct vb2_queue *q,
641 unsigned int *num_buffers, unsigned int *num_planes, 641 unsigned int *num_buffers, unsigned int *num_planes,
642 unsigned int sizes[], void *alloc_ctxs[]) 642 unsigned int sizes[], struct device *alloc_devs[])
643{ 643{
644 struct cx8802_dev *dev = q->drv_priv; 644 struct cx8802_dev *dev = q->drv_priv;
645 645
@@ -647,7 +647,6 @@ static int queue_setup(struct vb2_queue *q,
647 dev->ts_packet_size = 188 * 4; 647 dev->ts_packet_size = 188 * 4;
648 dev->ts_packet_count = 32; 648 dev->ts_packet_count = 32;
649 sizes[0] = dev->ts_packet_size * dev->ts_packet_count; 649 sizes[0] = dev->ts_packet_size * dev->ts_packet_count;
650 alloc_ctxs[0] = dev->alloc_ctx;
651 return 0; 650 return 0;
652} 651}
653 652
@@ -1183,6 +1182,7 @@ static int cx8802_blackbird_probe(struct cx8802_driver *drv)
1183 q->mem_ops = &vb2_dma_sg_memops; 1182 q->mem_ops = &vb2_dma_sg_memops;
1184 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC; 1183 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1185 q->lock = &core->lock; 1184 q->lock = &core->lock;
1185 q->dev = &dev->pci->dev;
1186 1186
1187 err = vb2_queue_init(q); 1187 err = vb2_queue_init(q);
1188 if (err < 0) 1188 if (err < 0)
diff --git a/drivers/media/pci/cx88/cx88-dvb.c b/drivers/media/pci/cx88/cx88-dvb.c
index 851d2a9caed3..5bb63e7a5691 100644
--- a/drivers/media/pci/cx88/cx88-dvb.c
+++ b/drivers/media/pci/cx88/cx88-dvb.c
@@ -84,7 +84,7 @@ DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
84 84
85static int queue_setup(struct vb2_queue *q, 85static int queue_setup(struct vb2_queue *q,
86 unsigned int *num_buffers, unsigned int *num_planes, 86 unsigned int *num_buffers, unsigned int *num_planes,
87 unsigned int sizes[], void *alloc_ctxs[]) 87 unsigned int sizes[], struct device *alloc_devs[])
88{ 88{
89 struct cx8802_dev *dev = q->drv_priv; 89 struct cx8802_dev *dev = q->drv_priv;
90 90
@@ -92,7 +92,6 @@ static int queue_setup(struct vb2_queue *q,
92 dev->ts_packet_size = 188 * 4; 92 dev->ts_packet_size = 188 * 4;
93 dev->ts_packet_count = dvb_buf_tscnt; 93 dev->ts_packet_count = dvb_buf_tscnt;
94 sizes[0] = dev->ts_packet_size * dev->ts_packet_count; 94 sizes[0] = dev->ts_packet_size * dev->ts_packet_count;
95 alloc_ctxs[0] = dev->alloc_ctx;
96 *num_buffers = dvb_buf_tscnt; 95 *num_buffers = dvb_buf_tscnt;
97 return 0; 96 return 0;
98} 97}
@@ -1793,6 +1792,7 @@ static int cx8802_dvb_probe(struct cx8802_driver *drv)
1793 q->mem_ops = &vb2_dma_sg_memops; 1792 q->mem_ops = &vb2_dma_sg_memops;
1794 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC; 1793 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1795 q->lock = &core->lock; 1794 q->lock = &core->lock;
1795 q->dev = &dev->pci->dev;
1796 1796
1797 err = vb2_queue_init(q); 1797 err = vb2_queue_init(q);
1798 if (err < 0) 1798 if (err < 0)
diff --git a/drivers/media/pci/cx88/cx88-mpeg.c b/drivers/media/pci/cx88/cx88-mpeg.c
index f34c229f9b37..245357adbc25 100644
--- a/drivers/media/pci/cx88/cx88-mpeg.c
+++ b/drivers/media/pci/cx88/cx88-mpeg.c
@@ -726,11 +726,6 @@ static int cx8802_probe(struct pci_dev *pci_dev,
726 if (NULL == dev) 726 if (NULL == dev)
727 goto fail_core; 727 goto fail_core;
728 dev->pci = pci_dev; 728 dev->pci = pci_dev;
729 dev->alloc_ctx = vb2_dma_sg_init_ctx(&pci_dev->dev);
730 if (IS_ERR(dev->alloc_ctx)) {
731 err = PTR_ERR(dev->alloc_ctx);
732 goto fail_dev;
733 }
734 dev->core = core; 729 dev->core = core;
735 730
736 /* Maintain a reference so cx88-video can query the 8802 device. */ 731 /* Maintain a reference so cx88-video can query the 8802 device. */
@@ -738,7 +733,7 @@ static int cx8802_probe(struct pci_dev *pci_dev,
738 733
739 err = cx8802_init_common(dev); 734 err = cx8802_init_common(dev);
740 if (err != 0) 735 if (err != 0)
741 goto fail_free; 736 goto fail_dev;
742 737
743 INIT_LIST_HEAD(&dev->drvlist); 738 INIT_LIST_HEAD(&dev->drvlist);
744 mutex_lock(&cx8802_mutex); 739 mutex_lock(&cx8802_mutex);
@@ -749,8 +744,6 @@ static int cx8802_probe(struct pci_dev *pci_dev,
749 request_modules(dev); 744 request_modules(dev);
750 return 0; 745 return 0;
751 746
752 fail_free:
753 vb2_dma_sg_cleanup_ctx(dev->alloc_ctx);
754 fail_dev: 747 fail_dev:
755 kfree(dev); 748 kfree(dev);
756 fail_core: 749 fail_core:
@@ -798,7 +791,6 @@ static void cx8802_remove(struct pci_dev *pci_dev)
798 /* common */ 791 /* common */
799 cx8802_fini_common(dev); 792 cx8802_fini_common(dev);
800 cx88_core_put(dev->core,dev->pci); 793 cx88_core_put(dev->core,dev->pci);
801 vb2_dma_sg_cleanup_ctx(dev->alloc_ctx);
802 kfree(dev); 794 kfree(dev);
803} 795}
804 796
diff --git a/drivers/media/pci/cx88/cx88-vbi.c b/drivers/media/pci/cx88/cx88-vbi.c
index ccc646d819f2..d3237cf8ffa3 100644
--- a/drivers/media/pci/cx88/cx88-vbi.c
+++ b/drivers/media/pci/cx88/cx88-vbi.c
@@ -109,7 +109,7 @@ int cx8800_restart_vbi_queue(struct cx8800_dev *dev,
109 109
110static int queue_setup(struct vb2_queue *q, 110static int queue_setup(struct vb2_queue *q,
111 unsigned int *num_buffers, unsigned int *num_planes, 111 unsigned int *num_buffers, unsigned int *num_planes,
112 unsigned int sizes[], void *alloc_ctxs[]) 112 unsigned int sizes[], struct device *alloc_devs[])
113{ 113{
114 struct cx8800_dev *dev = q->drv_priv; 114 struct cx8800_dev *dev = q->drv_priv;
115 115
@@ -118,7 +118,6 @@ static int queue_setup(struct vb2_queue *q,
118 sizes[0] = VBI_LINE_NTSC_COUNT * VBI_LINE_LENGTH * 2; 118 sizes[0] = VBI_LINE_NTSC_COUNT * VBI_LINE_LENGTH * 2;
119 else 119 else
120 sizes[0] = VBI_LINE_PAL_COUNT * VBI_LINE_LENGTH * 2; 120 sizes[0] = VBI_LINE_PAL_COUNT * VBI_LINE_LENGTH * 2;
121 alloc_ctxs[0] = dev->alloc_ctx;
122 return 0; 121 return 0;
123} 122}
124 123
diff --git a/drivers/media/pci/cx88/cx88-video.c b/drivers/media/pci/cx88/cx88-video.c
index 5f331df65fb9..5dc1e3f08d50 100644
--- a/drivers/media/pci/cx88/cx88-video.c
+++ b/drivers/media/pci/cx88/cx88-video.c
@@ -431,14 +431,13 @@ static int restart_video_queue(struct cx8800_dev *dev,
431 431
432static int queue_setup(struct vb2_queue *q, 432static int queue_setup(struct vb2_queue *q,
433 unsigned int *num_buffers, unsigned int *num_planes, 433 unsigned int *num_buffers, unsigned int *num_planes,
434 unsigned int sizes[], void *alloc_ctxs[]) 434 unsigned int sizes[], struct device *alloc_devs[])
435{ 435{
436 struct cx8800_dev *dev = q->drv_priv; 436 struct cx8800_dev *dev = q->drv_priv;
437 struct cx88_core *core = dev->core; 437 struct cx88_core *core = dev->core;
438 438
439 *num_planes = 1; 439 *num_planes = 1;
440 sizes[0] = (dev->fmt->depth * core->width * core->height) >> 3; 440 sizes[0] = (dev->fmt->depth * core->width * core->height) >> 3;
441 alloc_ctxs[0] = dev->alloc_ctx;
442 return 0; 441 return 0;
443} 442}
444 443
@@ -1319,12 +1318,6 @@ static int cx8800_initdev(struct pci_dev *pci_dev,
1319 printk("%s/0: Oops: no 32bit PCI DMA ???\n",core->name); 1318 printk("%s/0: Oops: no 32bit PCI DMA ???\n",core->name);
1320 goto fail_core; 1319 goto fail_core;
1321 } 1320 }
1322 dev->alloc_ctx = vb2_dma_sg_init_ctx(&pci_dev->dev);
1323 if (IS_ERR(dev->alloc_ctx)) {
1324 err = PTR_ERR(dev->alloc_ctx);
1325 goto fail_core;
1326 }
1327
1328 1321
1329 /* initialize driver struct */ 1322 /* initialize driver struct */
1330 spin_lock_init(&dev->slock); 1323 spin_lock_init(&dev->slock);
@@ -1445,6 +1438,7 @@ static int cx8800_initdev(struct pci_dev *pci_dev,
1445 q->mem_ops = &vb2_dma_sg_memops; 1438 q->mem_ops = &vb2_dma_sg_memops;
1446 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC; 1439 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1447 q->lock = &core->lock; 1440 q->lock = &core->lock;
1441 q->dev = &dev->pci->dev;
1448 1442
1449 err = vb2_queue_init(q); 1443 err = vb2_queue_init(q);
1450 if (err < 0) 1444 if (err < 0)
@@ -1461,6 +1455,7 @@ static int cx8800_initdev(struct pci_dev *pci_dev,
1461 q->mem_ops = &vb2_dma_sg_memops; 1455 q->mem_ops = &vb2_dma_sg_memops;
1462 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC; 1456 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1463 q->lock = &core->lock; 1457 q->lock = &core->lock;
1458 q->dev = &dev->pci->dev;
1464 1459
1465 err = vb2_queue_init(q); 1460 err = vb2_queue_init(q);
1466 if (err < 0) 1461 if (err < 0)
@@ -1530,7 +1525,6 @@ fail_unreg:
1530 free_irq(pci_dev->irq, dev); 1525 free_irq(pci_dev->irq, dev);
1531 mutex_unlock(&core->lock); 1526 mutex_unlock(&core->lock);
1532fail_core: 1527fail_core:
1533 vb2_dma_sg_cleanup_ctx(dev->alloc_ctx);
1534 core->v4ldev = NULL; 1528 core->v4ldev = NULL;
1535 cx88_core_put(core,dev->pci); 1529 cx88_core_put(core,dev->pci);
1536fail_free: 1530fail_free:
@@ -1564,7 +1558,6 @@ static void cx8800_finidev(struct pci_dev *pci_dev)
1564 1558
1565 /* free memory */ 1559 /* free memory */
1566 cx88_core_put(core,dev->pci); 1560 cx88_core_put(core,dev->pci);
1567 vb2_dma_sg_cleanup_ctx(dev->alloc_ctx);
1568 kfree(dev); 1561 kfree(dev);
1569} 1562}
1570 1563
diff --git a/drivers/media/pci/cx88/cx88.h b/drivers/media/pci/cx88/cx88.h
index 78f817ee7e41..ecd4b7bece99 100644
--- a/drivers/media/pci/cx88/cx88.h
+++ b/drivers/media/pci/cx88/cx88.h
@@ -485,7 +485,6 @@ struct cx8800_dev {
485 /* pci i/o */ 485 /* pci i/o */
486 struct pci_dev *pci; 486 struct pci_dev *pci;
487 unsigned char pci_rev,pci_lat; 487 unsigned char pci_rev,pci_lat;
488 void *alloc_ctx;
489 488
490 const struct cx8800_fmt *fmt; 489 const struct cx8800_fmt *fmt;
491 490
@@ -549,7 +548,6 @@ struct cx8802_dev {
549 /* pci i/o */ 548 /* pci i/o */
550 struct pci_dev *pci; 549 struct pci_dev *pci;
551 unsigned char pci_rev,pci_lat; 550 unsigned char pci_rev,pci_lat;
552 void *alloc_ctx;
553 551
554 /* dma queues */ 552 /* dma queues */
555 struct cx88_dmaqueue mpegq; 553 struct cx88_dmaqueue mpegq;
diff --git a/drivers/media/pci/ddbridge/ddbridge-core.c b/drivers/media/pci/ddbridge/ddbridge-core.c
index 6e995ef8c37e..47def73b3502 100644
--- a/drivers/media/pci/ddbridge/ddbridge-core.c
+++ b/drivers/media/pci/ddbridge/ddbridge-core.c
@@ -1569,10 +1569,9 @@ static int ddb_probe(struct pci_dev *pdev, const struct pci_device_id *id)
1569 if (pci_enable_device(pdev) < 0) 1569 if (pci_enable_device(pdev) < 0)
1570 return -ENODEV; 1570 return -ENODEV;
1571 1571
1572 dev = vmalloc(sizeof(struct ddb)); 1572 dev = vzalloc(sizeof(struct ddb));
1573 if (dev == NULL) 1573 if (dev == NULL)
1574 return -ENOMEM; 1574 return -ENOMEM;
1575 memset(dev, 0, sizeof(struct ddb));
1576 1575
1577 dev->pdev = pdev; 1576 dev->pdev = pdev;
1578 pci_set_drvdata(pdev, dev); 1577 pci_set_drvdata(pdev, dev);
diff --git a/drivers/media/pci/dt3155/dt3155.c b/drivers/media/pci/dt3155/dt3155.c
index 568c0c8fb2dc..6a219694b225 100644
--- a/drivers/media/pci/dt3155/dt3155.c
+++ b/drivers/media/pci/dt3155/dt3155.c
@@ -133,7 +133,7 @@ static int wait_i2c_reg(void __iomem *addr)
133static int 133static int
134dt3155_queue_setup(struct vb2_queue *vq, 134dt3155_queue_setup(struct vb2_queue *vq,
135 unsigned int *nbuffers, unsigned int *num_planes, 135 unsigned int *nbuffers, unsigned int *num_planes,
136 unsigned int sizes[], void *alloc_ctxs[]) 136 unsigned int sizes[], struct device *alloc_devs[])
137 137
138{ 138{
139 struct dt3155_priv *pd = vb2_get_drv_priv(vq); 139 struct dt3155_priv *pd = vb2_get_drv_priv(vq);
@@ -141,7 +141,6 @@ dt3155_queue_setup(struct vb2_queue *vq,
141 141
142 if (vq->num_buffers + *nbuffers < 2) 142 if (vq->num_buffers + *nbuffers < 2)
143 *nbuffers = 2 - vq->num_buffers; 143 *nbuffers = 2 - vq->num_buffers;
144 alloc_ctxs[0] = pd->alloc_ctx;
145 if (*num_planes) 144 if (*num_planes)
146 return sizes[0] < size ? -EINVAL : 0; 145 return sizes[0] < size ? -EINVAL : 0;
147 *num_planes = 1; 146 *num_planes = 1;
@@ -544,21 +543,16 @@ static int dt3155_probe(struct pci_dev *pdev, const struct pci_device_id *id)
544 pd->vidq.min_buffers_needed = 2; 543 pd->vidq.min_buffers_needed = 2;
545 pd->vidq.gfp_flags = GFP_DMA32; 544 pd->vidq.gfp_flags = GFP_DMA32;
546 pd->vidq.lock = &pd->mux; /* for locking v4l2_file_operations */ 545 pd->vidq.lock = &pd->mux; /* for locking v4l2_file_operations */
546 pd->vidq.dev = &pdev->dev;
547 pd->vdev.queue = &pd->vidq; 547 pd->vdev.queue = &pd->vidq;
548 err = vb2_queue_init(&pd->vidq); 548 err = vb2_queue_init(&pd->vidq);
549 if (err < 0) 549 if (err < 0)
550 goto err_v4l2_dev_unreg; 550 goto err_v4l2_dev_unreg;
551 pd->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
552 if (IS_ERR(pd->alloc_ctx)) {
553 dev_err(&pdev->dev, "Can't allocate buffer context");
554 err = PTR_ERR(pd->alloc_ctx);
555 goto err_v4l2_dev_unreg;
556 }
557 spin_lock_init(&pd->lock); 551 spin_lock_init(&pd->lock);
558 pd->config = ACQ_MODE_EVEN; 552 pd->config = ACQ_MODE_EVEN;
559 err = pci_enable_device(pdev); 553 err = pci_enable_device(pdev);
560 if (err) 554 if (err)
561 goto err_free_ctx; 555 goto err_v4l2_dev_unreg;
562 err = pci_request_region(pdev, 0, pci_name(pdev)); 556 err = pci_request_region(pdev, 0, pci_name(pdev));
563 if (err) 557 if (err)
564 goto err_pci_disable; 558 goto err_pci_disable;
@@ -588,8 +582,6 @@ err_free_reg:
588 pci_release_region(pdev, 0); 582 pci_release_region(pdev, 0);
589err_pci_disable: 583err_pci_disable:
590 pci_disable_device(pdev); 584 pci_disable_device(pdev);
591err_free_ctx:
592 vb2_dma_contig_cleanup_ctx(pd->alloc_ctx);
593err_v4l2_dev_unreg: 585err_v4l2_dev_unreg:
594 v4l2_device_unregister(&pd->v4l2_dev); 586 v4l2_device_unregister(&pd->v4l2_dev);
595 return err; 587 return err;
@@ -608,7 +600,6 @@ static void dt3155_remove(struct pci_dev *pdev)
608 pci_iounmap(pdev, pd->regs); 600 pci_iounmap(pdev, pd->regs);
609 pci_release_region(pdev, 0); 601 pci_release_region(pdev, 0);
610 pci_disable_device(pdev); 602 pci_disable_device(pdev);
611 vb2_dma_contig_cleanup_ctx(pd->alloc_ctx);
612} 603}
613 604
614static const struct pci_device_id pci_ids[] = { 605static const struct pci_device_id pci_ids[] = {
diff --git a/drivers/media/pci/dt3155/dt3155.h b/drivers/media/pci/dt3155/dt3155.h
index b3531e0bc733..39442e58919d 100644
--- a/drivers/media/pci/dt3155/dt3155.h
+++ b/drivers/media/pci/dt3155/dt3155.h
@@ -161,7 +161,6 @@
161 * @vdev: video_device structure 161 * @vdev: video_device structure
162 * @pdev: pointer to pci_dev structure 162 * @pdev: pointer to pci_dev structure
163 * @vidq: vb2_queue structure 163 * @vidq: vb2_queue structure
164 * @alloc_ctx: dma_contig allocation context
165 * @curr_buf: pointer to curren buffer 164 * @curr_buf: pointer to curren buffer
166 * @mux: mutex to protect the instance 165 * @mux: mutex to protect the instance
167 * @dmaq: queue for dma buffers 166 * @dmaq: queue for dma buffers
@@ -181,7 +180,6 @@ struct dt3155_priv {
181 struct video_device vdev; 180 struct video_device vdev;
182 struct pci_dev *pdev; 181 struct pci_dev *pdev;
183 struct vb2_queue vidq; 182 struct vb2_queue vidq;
184 struct vb2_alloc_ctx *alloc_ctx;
185 struct vb2_v4l2_buffer *curr_buf; 183 struct vb2_v4l2_buffer *curr_buf;
186 struct mutex mux; 184 struct mutex mux;
187 struct list_head dmaq; 185 struct list_head dmaq;
diff --git a/drivers/media/pci/ivtv/ivtv-alsa-mixer.c b/drivers/media/pci/ivtv/ivtv-alsa-mixer.c
index 33ec05b09af3..79b24bde4a39 100644
--- a/drivers/media/pci/ivtv/ivtv-alsa-mixer.c
+++ b/drivers/media/pci/ivtv/ivtv-alsa-mixer.c
@@ -93,7 +93,7 @@ static int snd_ivtv_mixer_tv_vol_get(struct snd_kcontrol *kctl,
93 vctrl.value = dB_to_cx25840_vol(uctl->value.integer.value[0]); 93 vctrl.value = dB_to_cx25840_vol(uctl->value.integer.value[0]);
94 94
95 snd_ivtv_lock(itvsc); 95 snd_ivtv_lock(itvsc);
96 ret = v4l2_subdev_call(itv->sd_audio, core, g_ctrl, &vctrl); 96 ret = v4l2_g_ctrl(itv->sd_audio->ctrl_handler, &vctrl);
97 snd_ivtv_unlock(itvsc); 97 snd_ivtv_unlock(itvsc);
98 98
99 if (!ret) 99 if (!ret)
@@ -115,14 +115,14 @@ static int snd_ivtv_mixer_tv_vol_put(struct snd_kcontrol *kctl,
115 snd_ivtv_lock(itvsc); 115 snd_ivtv_lock(itvsc);
116 116
117 /* Fetch current state */ 117 /* Fetch current state */
118 ret = v4l2_subdev_call(itv->sd_audio, core, g_ctrl, &vctrl); 118 ret = v4l2_g_ctrl(itv->sd_audio->ctrl_handler, &vctrl);
119 119
120 if (ret || 120 if (ret ||
121 (cx25840_vol_to_dB(vctrl.value) != uctl->value.integer.value[0])) { 121 (cx25840_vol_to_dB(vctrl.value) != uctl->value.integer.value[0])) {
122 122
123 /* Set, if needed */ 123 /* Set, if needed */
124 vctrl.value = dB_to_cx25840_vol(uctl->value.integer.value[0]); 124 vctrl.value = dB_to_cx25840_vol(uctl->value.integer.value[0]);
125 ret = v4l2_subdev_call(itv->sd_audio, core, s_ctrl, &vctrl); 125 ret = v4l2_s_ctrl(itv->sd_audio->ctrl_handler, &vctrl);
126 if (!ret) 126 if (!ret)
127 ret = 1; /* Indicate control was changed w/o error */ 127 ret = 1; /* Indicate control was changed w/o error */
128 } 128 }
diff --git a/drivers/media/pci/netup_unidvb/Kconfig b/drivers/media/pci/netup_unidvb/Kconfig
index f277b0b10c2d..0ad37714c7fd 100644
--- a/drivers/media/pci/netup_unidvb/Kconfig
+++ b/drivers/media/pci/netup_unidvb/Kconfig
@@ -5,8 +5,13 @@ config DVB_NETUP_UNIDVB
5 select VIDEOBUF2_VMALLOC 5 select VIDEOBUF2_VMALLOC
6 select DVB_HORUS3A if MEDIA_SUBDRV_AUTOSELECT 6 select DVB_HORUS3A if MEDIA_SUBDRV_AUTOSELECT
7 select DVB_ASCOT2E if MEDIA_SUBDRV_AUTOSELECT 7 select DVB_ASCOT2E if MEDIA_SUBDRV_AUTOSELECT
8 select DVB_HELENE if MEDIA_SUBDRV_AUTOSELECT
8 select DVB_LNBH25 if MEDIA_SUBDRV_AUTOSELECT 9 select DVB_LNBH25 if MEDIA_SUBDRV_AUTOSELECT
9 select DVB_CXD2841ER if MEDIA_SUBDRV_AUTOSELECT 10 select DVB_CXD2841ER if MEDIA_SUBDRV_AUTOSELECT
10 ---help--- 11 ---help---
11 Support for NetUP PCI express Universal DVB card. 12 Support for NetUP PCI express Universal DVB card.
12 13 help
14 Say Y when you want to support NetUP Dual Universal DVB card
15 Card can receive two independent streams in following standards:
16 DVB-S/S2, T/T2, C/C2
17 Two CI slots available for CAM modules.
diff --git a/drivers/media/pci/netup_unidvb/netup_unidvb.h b/drivers/media/pci/netup_unidvb/netup_unidvb.h
index a67b28111905..39b08ecda1fc 100644
--- a/drivers/media/pci/netup_unidvb/netup_unidvb.h
+++ b/drivers/media/pci/netup_unidvb/netup_unidvb.h
@@ -50,6 +50,15 @@
50#define NETUP_UNIDVB_IRQ_CAM0 (1 << 11) 50#define NETUP_UNIDVB_IRQ_CAM0 (1 << 11)
51#define NETUP_UNIDVB_IRQ_CAM1 (1 << 12) 51#define NETUP_UNIDVB_IRQ_CAM1 (1 << 12)
52 52
53/* NetUP Universal DVB card hardware revisions and it's PCI device id's:
54 * 1.3 - CXD2841ER demod, ASCOT2E and HORUS3A tuners
55 * 1.4 - CXD2854ER demod, HELENE tuner
56*/
57enum netup_hw_rev {
58 NETUP_HW_REV_1_3 = 0x18F6,
59 NETUP_HW_REV_1_4 = 0x18F7
60};
61
53struct netup_dma { 62struct netup_dma {
54 u8 num; 63 u8 num;
55 spinlock_t lock; 64 spinlock_t lock;
@@ -119,6 +128,7 @@ struct netup_unidvb_dev {
119 struct netup_dma dma[2]; 128 struct netup_dma dma[2];
120 struct netup_ci_state ci[2]; 129 struct netup_ci_state ci[2];
121 struct netup_spi *spi; 130 struct netup_spi *spi;
131 enum netup_hw_rev rev;
122}; 132};
123 133
124int netup_i2c_register(struct netup_unidvb_dev *ndev); 134int netup_i2c_register(struct netup_unidvb_dev *ndev);
diff --git a/drivers/media/pci/netup_unidvb/netup_unidvb_ci.c b/drivers/media/pci/netup_unidvb/netup_unidvb_ci.c
index f46ffac66ee9..f535270c2116 100644
--- a/drivers/media/pci/netup_unidvb/netup_unidvb_ci.c
+++ b/drivers/media/pci/netup_unidvb/netup_unidvb_ci.c
@@ -147,7 +147,7 @@ static int netup_unidvb_ci_read_attribute_mem(struct dvb_ca_en50221 *en50221,
147{ 147{
148 struct netup_ci_state *state = en50221->data; 148 struct netup_ci_state *state = en50221->data;
149 struct netup_unidvb_dev *dev = state->dev; 149 struct netup_unidvb_dev *dev = state->dev;
150 u8 val = *((u8 __force *)state->membase8_io + addr); 150 u8 val = *((u8 __force *)state->membase8_config + addr);
151 151
152 dev_dbg(&dev->pci_dev->dev, 152 dev_dbg(&dev->pci_dev->dev,
153 "%s(): addr=0x%x val=0x%x\n", __func__, addr, val); 153 "%s(): addr=0x%x val=0x%x\n", __func__, addr, val);
@@ -162,7 +162,7 @@ static int netup_unidvb_ci_write_attribute_mem(struct dvb_ca_en50221 *en50221,
162 162
163 dev_dbg(&dev->pci_dev->dev, 163 dev_dbg(&dev->pci_dev->dev,
164 "%s(): addr=0x%x data=0x%x\n", __func__, addr, data); 164 "%s(): addr=0x%x data=0x%x\n", __func__, addr, data);
165 *((u8 __force *)state->membase8_io + addr) = data; 165 *((u8 __force *)state->membase8_config + addr) = data;
166 return 0; 166 return 0;
167} 167}
168 168
diff --git a/drivers/media/pci/netup_unidvb/netup_unidvb_core.c b/drivers/media/pci/netup_unidvb/netup_unidvb_core.c
index 2b667b315913..ac547cb84de8 100644
--- a/drivers/media/pci/netup_unidvb/netup_unidvb_core.c
+++ b/drivers/media/pci/netup_unidvb/netup_unidvb_core.c
@@ -34,6 +34,7 @@
34#include "cxd2841er.h" 34#include "cxd2841er.h"
35#include "horus3a.h" 35#include "horus3a.h"
36#include "ascot2e.h" 36#include "ascot2e.h"
37#include "helene.h"
37#include "lnbh25.h" 38#include "lnbh25.h"
38 39
39static int spi_enable; 40static int spi_enable;
@@ -120,7 +121,8 @@ static int netup_unidvb_tuner_ctrl(void *priv, int is_dvb_tc);
120static void netup_unidvb_queue_cleanup(struct netup_dma *dma); 121static void netup_unidvb_queue_cleanup(struct netup_dma *dma);
121 122
122static struct cxd2841er_config demod_config = { 123static struct cxd2841er_config demod_config = {
123 .i2c_addr = 0xc8 124 .i2c_addr = 0xc8,
125 .xtal = SONY_XTAL_24000
124}; 126};
125 127
126static struct horus3a_config horus3a_conf = { 128static struct horus3a_config horus3a_conf = {
@@ -134,6 +136,12 @@ static struct ascot2e_config ascot2e_conf = {
134 .set_tuner_callback = netup_unidvb_tuner_ctrl 136 .set_tuner_callback = netup_unidvb_tuner_ctrl
135}; 137};
136 138
139static struct helene_config helene_conf = {
140 .i2c_address = 0xc0,
141 .xtal = SONY_HELENE_XTAL_24000,
142 .set_tuner_callback = netup_unidvb_tuner_ctrl
143};
144
137static struct lnbh25_config lnbh25_conf = { 145static struct lnbh25_config lnbh25_conf = {
138 .i2c_address = 0x10, 146 .i2c_address = 0x10,
139 .data2_config = LNBH25_TEN | LNBH25_EXTM 147 .data2_config = LNBH25_TEN | LNBH25_EXTM
@@ -152,6 +160,11 @@ static int netup_unidvb_tuner_ctrl(void *priv, int is_dvb_tc)
152 __func__, dma->num, is_dvb_tc); 160 __func__, dma->num, is_dvb_tc);
153 reg = readb(ndev->bmmio0 + GPIO_REG_IO); 161 reg = readb(ndev->bmmio0 + GPIO_REG_IO);
154 mask = (dma->num == 0) ? GPIO_RFA_CTL : GPIO_RFB_CTL; 162 mask = (dma->num == 0) ? GPIO_RFA_CTL : GPIO_RFB_CTL;
163
164 /* inverted tuner control in hw rev. 1.4 */
165 if (ndev->rev == NETUP_HW_REV_1_4)
166 is_dvb_tc = !is_dvb_tc;
167
155 if (!is_dvb_tc) 168 if (!is_dvb_tc)
156 reg |= mask; 169 reg |= mask;
157 else 170 else
@@ -280,7 +293,7 @@ static int netup_unidvb_queue_setup(struct vb2_queue *vq,
280 unsigned int *nbuffers, 293 unsigned int *nbuffers,
281 unsigned int *nplanes, 294 unsigned int *nplanes,
282 unsigned int sizes[], 295 unsigned int sizes[],
283 void *alloc_ctxs[]) 296 struct device *alloc_devs[])
284{ 297{
285 struct netup_dma *dma = vb2_get_drv_priv(vq); 298 struct netup_dma *dma = vb2_get_drv_priv(vq);
286 299
@@ -372,7 +385,15 @@ static int netup_unidvb_queue_init(struct netup_dma *dma,
372static int netup_unidvb_dvb_init(struct netup_unidvb_dev *ndev, 385static int netup_unidvb_dvb_init(struct netup_unidvb_dev *ndev,
373 int num) 386 int num)
374{ 387{
375 struct vb2_dvb_frontend *fe0, *fe1, *fe2; 388 int fe_count = 2;
389 int i = 0;
390 struct vb2_dvb_frontend *fes[2];
391 u8 fe_name[32];
392
393 if (ndev->rev == NETUP_HW_REV_1_3)
394 demod_config.xtal = SONY_XTAL_20500;
395 else
396 demod_config.xtal = SONY_XTAL_24000;
376 397
377 if (num < 0 || num > 1) { 398 if (num < 0 || num > 1) {
378 dev_dbg(&ndev->pci_dev->dev, 399 dev_dbg(&ndev->pci_dev->dev,
@@ -381,84 +402,96 @@ static int netup_unidvb_dvb_init(struct netup_unidvb_dev *ndev,
381 } 402 }
382 mutex_init(&ndev->frontends[num].lock); 403 mutex_init(&ndev->frontends[num].lock);
383 INIT_LIST_HEAD(&ndev->frontends[num].felist); 404 INIT_LIST_HEAD(&ndev->frontends[num].felist);
384 if (vb2_dvb_alloc_frontend(&ndev->frontends[num], 1) == NULL || 405
385 vb2_dvb_alloc_frontend( 406 for (i = 0; i < fe_count; i++) {
386 &ndev->frontends[num], 2) == NULL || 407 if (vb2_dvb_alloc_frontend(&ndev->frontends[num], i+1)
387 vb2_dvb_alloc_frontend( 408 == NULL) {
388 &ndev->frontends[num], 3) == NULL) { 409 dev_err(&ndev->pci_dev->dev,
389 dev_dbg(&ndev->pci_dev->dev, 410 "%s(): unable to allocate vb2_dvb_frontend\n",
390 "%s(): unable to allocate vb2_dvb_frontend\n", 411 __func__);
391 __func__); 412 return -ENOMEM;
392 return -ENOMEM; 413 }
393 } 414 }
394 fe0 = vb2_dvb_get_frontend(&ndev->frontends[num], 1); 415
395 fe1 = vb2_dvb_get_frontend(&ndev->frontends[num], 2); 416 for (i = 0; i < fe_count; i++) {
396 fe2 = vb2_dvb_get_frontend(&ndev->frontends[num], 3); 417 fes[i] = vb2_dvb_get_frontend(&ndev->frontends[num], i+1);
397 if (fe0 == NULL || fe1 == NULL || fe2 == NULL) { 418 if (fes[i] == NULL) {
398 dev_dbg(&ndev->pci_dev->dev, 419 dev_err(&ndev->pci_dev->dev,
399 "%s(): frontends has not been allocated\n", __func__); 420 "%s(): frontends has not been allocated\n",
400 return -EINVAL; 421 __func__);
422 return -EINVAL;
423 }
424 }
425
426 for (i = 0; i < fe_count; i++) {
427 netup_unidvb_queue_init(&ndev->dma[num], &fes[i]->dvb.dvbq);
428 snprintf(fe_name, sizeof(fe_name), "netup_fe%d", i);
429 fes[i]->dvb.name = fe_name;
401 } 430 }
402 netup_unidvb_queue_init(&ndev->dma[num], &fe0->dvb.dvbq); 431
403 netup_unidvb_queue_init(&ndev->dma[num], &fe1->dvb.dvbq); 432 fes[0]->dvb.frontend = dvb_attach(cxd2841er_attach_s,
404 netup_unidvb_queue_init(&ndev->dma[num], &fe2->dvb.dvbq);
405 fe0->dvb.name = "netup_fe0";
406 fe1->dvb.name = "netup_fe1";
407 fe2->dvb.name = "netup_fe2";
408 fe0->dvb.frontend = dvb_attach(cxd2841er_attach_s,
409 &demod_config, &ndev->i2c[num].adap); 433 &demod_config, &ndev->i2c[num].adap);
410 if (fe0->dvb.frontend == NULL) { 434 if (fes[0]->dvb.frontend == NULL) {
411 dev_dbg(&ndev->pci_dev->dev, 435 dev_dbg(&ndev->pci_dev->dev,
412 "%s(): unable to attach DVB-S/S2 frontend\n", 436 "%s(): unable to attach DVB-S/S2 frontend\n",
413 __func__); 437 __func__);
414 goto frontend_detach; 438 goto frontend_detach;
415 } 439 }
416 horus3a_conf.set_tuner_priv = &ndev->dma[num]; 440
417 if (!dvb_attach(horus3a_attach, fe0->dvb.frontend, 441 if (ndev->rev == NETUP_HW_REV_1_3) {
418 &horus3a_conf, &ndev->i2c[num].adap)) { 442 horus3a_conf.set_tuner_priv = &ndev->dma[num];
419 dev_dbg(&ndev->pci_dev->dev, 443 if (!dvb_attach(horus3a_attach, fes[0]->dvb.frontend,
420 "%s(): unable to attach DVB-S/S2 tuner frontend\n", 444 &horus3a_conf, &ndev->i2c[num].adap)) {
421 __func__); 445 dev_dbg(&ndev->pci_dev->dev,
422 goto frontend_detach; 446 "%s(): unable to attach HORUS3A DVB-S/S2 tuner frontend\n",
447 __func__);
448 goto frontend_detach;
449 }
450 } else {
451 helene_conf.set_tuner_priv = &ndev->dma[num];
452 if (!dvb_attach(helene_attach_s, fes[0]->dvb.frontend,
453 &helene_conf, &ndev->i2c[num].adap)) {
454 dev_err(&ndev->pci_dev->dev,
455 "%s(): unable to attach HELENE DVB-S/S2 tuner frontend\n",
456 __func__);
457 goto frontend_detach;
458 }
423 } 459 }
424 if (!dvb_attach(lnbh25_attach, fe0->dvb.frontend, 460
461 if (!dvb_attach(lnbh25_attach, fes[0]->dvb.frontend,
425 &lnbh25_conf, &ndev->i2c[num].adap)) { 462 &lnbh25_conf, &ndev->i2c[num].adap)) {
426 dev_dbg(&ndev->pci_dev->dev, 463 dev_dbg(&ndev->pci_dev->dev,
427 "%s(): unable to attach SEC frontend\n", __func__); 464 "%s(): unable to attach SEC frontend\n", __func__);
428 goto frontend_detach; 465 goto frontend_detach;
429 } 466 }
467
430 /* DVB-T/T2 frontend */ 468 /* DVB-T/T2 frontend */
431 fe1->dvb.frontend = dvb_attach(cxd2841er_attach_t, 469 fes[1]->dvb.frontend = dvb_attach(cxd2841er_attach_t_c,
432 &demod_config, &ndev->i2c[num].adap); 470 &demod_config, &ndev->i2c[num].adap);
433 if (fe1->dvb.frontend == NULL) { 471 if (fes[1]->dvb.frontend == NULL) {
434 dev_dbg(&ndev->pci_dev->dev,
435 "%s(): unable to attach DVB-T frontend\n", __func__);
436 goto frontend_detach;
437 }
438 fe1->dvb.frontend->id = 1;
439 ascot2e_conf.set_tuner_priv = &ndev->dma[num];
440 if (!dvb_attach(ascot2e_attach, fe1->dvb.frontend,
441 &ascot2e_conf, &ndev->i2c[num].adap)) {
442 dev_dbg(&ndev->pci_dev->dev,
443 "%s(): unable to attach DVB-T tuner frontend\n",
444 __func__);
445 goto frontend_detach;
446 }
447 /* DVB-C/C2 frontend */
448 fe2->dvb.frontend = dvb_attach(cxd2841er_attach_c,
449 &demod_config, &ndev->i2c[num].adap);
450 if (fe2->dvb.frontend == NULL) {
451 dev_dbg(&ndev->pci_dev->dev, 472 dev_dbg(&ndev->pci_dev->dev,
452 "%s(): unable to attach DVB-C frontend\n", __func__); 473 "%s(): unable to attach Ter frontend\n", __func__);
453 goto frontend_detach; 474 goto frontend_detach;
454 } 475 }
455 fe2->dvb.frontend->id = 2; 476 fes[1]->dvb.frontend->id = 1;
456 if (!dvb_attach(ascot2e_attach, fe2->dvb.frontend, 477 if (ndev->rev == NETUP_HW_REV_1_3) {
457 &ascot2e_conf, &ndev->i2c[num].adap)) { 478 ascot2e_conf.set_tuner_priv = &ndev->dma[num];
458 dev_dbg(&ndev->pci_dev->dev, 479 if (!dvb_attach(ascot2e_attach, fes[1]->dvb.frontend,
459 "%s(): unable to attach DVB-T/C tuner frontend\n", 480 &ascot2e_conf, &ndev->i2c[num].adap)) {
460 __func__); 481 dev_dbg(&ndev->pci_dev->dev,
461 goto frontend_detach; 482 "%s(): unable to attach Ter tuner frontend\n",
483 __func__);
484 goto frontend_detach;
485 }
486 } else {
487 helene_conf.set_tuner_priv = &ndev->dma[num];
488 if (!dvb_attach(helene_attach, fes[1]->dvb.frontend,
489 &helene_conf, &ndev->i2c[num].adap)) {
490 dev_err(&ndev->pci_dev->dev,
491 "%s(): unable to attach HELENE Ter tuner frontend\n",
492 __func__);
493 goto frontend_detach;
494 }
462 } 495 }
463 496
464 if (vb2_dvb_register_bus(&ndev->frontends[num], 497 if (vb2_dvb_register_bus(&ndev->frontends[num],
@@ -730,7 +763,7 @@ static int netup_unidvb_request_mmio(struct pci_dev *pci_dev)
730static int netup_unidvb_request_modules(struct device *dev) 763static int netup_unidvb_request_modules(struct device *dev)
731{ 764{
732 static const char * const modules[] = { 765 static const char * const modules[] = {
733 "lnbh25", "ascot2e", "horus3a", "cxd2841er", NULL 766 "lnbh25", "ascot2e", "horus3a", "cxd2841er", "helene", NULL
734 }; 767 };
735 const char * const *curr_mod = modules; 768 const char * const *curr_mod = modules;
736 int err; 769 int err;
@@ -774,6 +807,16 @@ static int netup_unidvb_initdev(struct pci_dev *pci_dev,
774 if (!ndev) 807 if (!ndev)
775 goto dev_alloc_err; 808 goto dev_alloc_err;
776 809
810 /* detect hardware revision */
811 if (pci_dev->device == NETUP_HW_REV_1_3)
812 ndev->rev = NETUP_HW_REV_1_3;
813 else
814 ndev->rev = NETUP_HW_REV_1_4;
815
816 dev_info(&pci_dev->dev,
817 "%s(): board (0x%x) hardware revision 0x%x\n",
818 __func__, pci_dev->device, ndev->rev);
819
777 ndev->old_fw = old_firmware; 820 ndev->old_fw = old_firmware;
778 ndev->wq = create_singlethread_workqueue(NETUP_UNIDVB_NAME); 821 ndev->wq = create_singlethread_workqueue(NETUP_UNIDVB_NAME);
779 if (!ndev->wq) { 822 if (!ndev->wq) {
@@ -932,7 +975,7 @@ wq_create_err:
932 kfree(ndev); 975 kfree(ndev);
933dev_alloc_err: 976dev_alloc_err:
934 dev_err(&pci_dev->dev, 977 dev_err(&pci_dev->dev,
935 "%s(): failed to initizalize device\n", __func__); 978 "%s(): failed to initialize device\n", __func__);
936 return -EIO; 979 return -EIO;
937} 980}
938 981
@@ -972,7 +1015,8 @@ static void netup_unidvb_finidev(struct pci_dev *pci_dev)
972 1015
973 1016
974static struct pci_device_id netup_unidvb_pci_tbl[] = { 1017static struct pci_device_id netup_unidvb_pci_tbl[] = {
975 { PCI_DEVICE(0x1b55, 0x18f6) }, 1018 { PCI_DEVICE(0x1b55, 0x18f6) }, /* hw rev. 1.3 */
1019 { PCI_DEVICE(0x1b55, 0x18f7) }, /* hw rev. 1.4 */
976 { 0, } 1020 { 0, }
977}; 1021};
978MODULE_DEVICE_TABLE(pci, netup_unidvb_pci_tbl); 1022MODULE_DEVICE_TABLE(pci, netup_unidvb_pci_tbl);
diff --git a/drivers/media/pci/saa7134/saa7134-core.c b/drivers/media/pci/saa7134/saa7134-core.c
index c0e1780ec831..ffb66a9ae23e 100644
--- a/drivers/media/pci/saa7134/saa7134-core.c
+++ b/drivers/media/pci/saa7134/saa7134-core.c
@@ -1164,18 +1164,13 @@ static int saa7134_initdev(struct pci_dev *pci_dev,
1164 saa7134_board_init1(dev); 1164 saa7134_board_init1(dev);
1165 saa7134_hwinit1(dev); 1165 saa7134_hwinit1(dev);
1166 1166
1167 dev->alloc_ctx = vb2_dma_sg_init_ctx(&pci_dev->dev);
1168 if (IS_ERR(dev->alloc_ctx)) {
1169 err = PTR_ERR(dev->alloc_ctx);
1170 goto fail3;
1171 }
1172 /* get irq */ 1167 /* get irq */
1173 err = request_irq(pci_dev->irq, saa7134_irq, 1168 err = request_irq(pci_dev->irq, saa7134_irq,
1174 IRQF_SHARED, dev->name, dev); 1169 IRQF_SHARED, dev->name, dev);
1175 if (err < 0) { 1170 if (err < 0) {
1176 pr_err("%s: can't get IRQ %d\n", 1171 pr_err("%s: can't get IRQ %d\n",
1177 dev->name,pci_dev->irq); 1172 dev->name,pci_dev->irq);
1178 goto fail4; 1173 goto fail3;
1179 } 1174 }
1180 1175
1181 /* wait a bit, register i2c bus */ 1176 /* wait a bit, register i2c bus */
@@ -1233,7 +1228,7 @@ static int saa7134_initdev(struct pci_dev *pci_dev,
1233 if (err < 0) { 1228 if (err < 0) {
1234 pr_info("%s: can't register video device\n", 1229 pr_info("%s: can't register video device\n",
1235 dev->name); 1230 dev->name);
1236 goto fail5; 1231 goto fail4;
1237 } 1232 }
1238 pr_info("%s: registered device %s [v4l2]\n", 1233 pr_info("%s: registered device %s [v4l2]\n",
1239 dev->name, video_device_node_name(dev->video_dev)); 1234 dev->name, video_device_node_name(dev->video_dev));
@@ -1246,7 +1241,7 @@ static int saa7134_initdev(struct pci_dev *pci_dev,
1246 err = video_register_device(dev->vbi_dev,VFL_TYPE_VBI, 1241 err = video_register_device(dev->vbi_dev,VFL_TYPE_VBI,
1247 vbi_nr[dev->nr]); 1242 vbi_nr[dev->nr]);
1248 if (err < 0) 1243 if (err < 0)
1249 goto fail5; 1244 goto fail4;
1250 pr_info("%s: registered device %s\n", 1245 pr_info("%s: registered device %s\n",
1251 dev->name, video_device_node_name(dev->vbi_dev)); 1246 dev->name, video_device_node_name(dev->vbi_dev));
1252 1247
@@ -1257,7 +1252,7 @@ static int saa7134_initdev(struct pci_dev *pci_dev,
1257 err = video_register_device(dev->radio_dev,VFL_TYPE_RADIO, 1252 err = video_register_device(dev->radio_dev,VFL_TYPE_RADIO,
1258 radio_nr[dev->nr]); 1253 radio_nr[dev->nr]);
1259 if (err < 0) 1254 if (err < 0)
1260 goto fail5; 1255 goto fail4;
1261 pr_info("%s: registered device %s\n", 1256 pr_info("%s: registered device %s\n",
1262 dev->name, video_device_node_name(dev->radio_dev)); 1257 dev->name, video_device_node_name(dev->radio_dev));
1263 } 1258 }
@@ -1268,7 +1263,7 @@ static int saa7134_initdev(struct pci_dev *pci_dev,
1268 err = v4l2_mc_create_media_graph(dev->media_dev); 1263 err = v4l2_mc_create_media_graph(dev->media_dev);
1269 if (err) { 1264 if (err) {
1270 pr_err("failed to create media graph\n"); 1265 pr_err("failed to create media graph\n");
1271 goto fail5; 1266 goto fail4;
1272 } 1267 }
1273#endif 1268#endif
1274 /* everything worked */ 1269 /* everything worked */
@@ -1287,17 +1282,15 @@ static int saa7134_initdev(struct pci_dev *pci_dev,
1287#ifdef CONFIG_MEDIA_CONTROLLER 1282#ifdef CONFIG_MEDIA_CONTROLLER
1288 err = media_device_register(dev->media_dev); 1283 err = media_device_register(dev->media_dev);
1289 if (err) 1284 if (err)
1290 goto fail5; 1285 goto fail4;
1291#endif 1286#endif
1292 1287
1293 return 0; 1288 return 0;
1294 1289
1295 fail5: 1290 fail4:
1296 saa7134_unregister_video(dev); 1291 saa7134_unregister_video(dev);
1297 saa7134_i2c_unregister(dev); 1292 saa7134_i2c_unregister(dev);
1298 free_irq(pci_dev->irq, dev); 1293 free_irq(pci_dev->irq, dev);
1299 fail4:
1300 vb2_dma_sg_cleanup_ctx(dev->alloc_ctx);
1301 fail3: 1294 fail3:
1302 saa7134_hwfini(dev); 1295 saa7134_hwfini(dev);
1303 iounmap(dev->lmmio); 1296 iounmap(dev->lmmio);
@@ -1367,7 +1360,6 @@ static void saa7134_finidev(struct pci_dev *pci_dev)
1367 1360
1368 /* release resources */ 1361 /* release resources */
1369 free_irq(pci_dev->irq, dev); 1362 free_irq(pci_dev->irq, dev);
1370 vb2_dma_sg_cleanup_ctx(dev->alloc_ctx);
1371 iounmap(dev->lmmio); 1363 iounmap(dev->lmmio);
1372 release_mem_region(pci_resource_start(pci_dev,0), 1364 release_mem_region(pci_resource_start(pci_dev,0),
1373 pci_resource_len(pci_dev,0)); 1365 pci_resource_len(pci_dev,0));
diff --git a/drivers/media/pci/saa7134/saa7134-ts.c b/drivers/media/pci/saa7134/saa7134-ts.c
index 0584a2adbe99..7eaf36a41db9 100644
--- a/drivers/media/pci/saa7134/saa7134-ts.c
+++ b/drivers/media/pci/saa7134/saa7134-ts.c
@@ -118,7 +118,7 @@ EXPORT_SYMBOL_GPL(saa7134_ts_buffer_prepare);
118 118
119int saa7134_ts_queue_setup(struct vb2_queue *q, 119int saa7134_ts_queue_setup(struct vb2_queue *q,
120 unsigned int *nbuffers, unsigned int *nplanes, 120 unsigned int *nbuffers, unsigned int *nplanes,
121 unsigned int sizes[], void *alloc_ctxs[]) 121 unsigned int sizes[], struct device *alloc_devs[])
122{ 122{
123 struct saa7134_dmaqueue *dmaq = q->drv_priv; 123 struct saa7134_dmaqueue *dmaq = q->drv_priv;
124 struct saa7134_dev *dev = dmaq->dev; 124 struct saa7134_dev *dev = dmaq->dev;
@@ -131,7 +131,6 @@ int saa7134_ts_queue_setup(struct vb2_queue *q,
131 *nbuffers = 3; 131 *nbuffers = 3;
132 *nplanes = 1; 132 *nplanes = 1;
133 sizes[0] = size; 133 sizes[0] = size;
134 alloc_ctxs[0] = dev->alloc_ctx;
135 return 0; 134 return 0;
136} 135}
137EXPORT_SYMBOL_GPL(saa7134_ts_queue_setup); 136EXPORT_SYMBOL_GPL(saa7134_ts_queue_setup);
diff --git a/drivers/media/pci/saa7134/saa7134-vbi.c b/drivers/media/pci/saa7134/saa7134-vbi.c
index e76da37c4a8a..cf9a31e0a390 100644
--- a/drivers/media/pci/saa7134/saa7134-vbi.c
+++ b/drivers/media/pci/saa7134/saa7134-vbi.c
@@ -140,7 +140,7 @@ static int buffer_prepare(struct vb2_buffer *vb2)
140 140
141static int queue_setup(struct vb2_queue *q, 141static int queue_setup(struct vb2_queue *q,
142 unsigned int *nbuffers, unsigned int *nplanes, 142 unsigned int *nbuffers, unsigned int *nplanes,
143 unsigned int sizes[], void *alloc_ctxs[]) 143 unsigned int sizes[], struct device *alloc_devs[])
144{ 144{
145 struct saa7134_dmaqueue *dmaq = q->drv_priv; 145 struct saa7134_dmaqueue *dmaq = q->drv_priv;
146 struct saa7134_dev *dev = dmaq->dev; 146 struct saa7134_dev *dev = dmaq->dev;
@@ -155,7 +155,6 @@ static int queue_setup(struct vb2_queue *q,
155 *nbuffers = saa7134_buffer_count(size, *nbuffers); 155 *nbuffers = saa7134_buffer_count(size, *nbuffers);
156 *nplanes = 1; 156 *nplanes = 1;
157 sizes[0] = size; 157 sizes[0] = size;
158 alloc_ctxs[0] = dev->alloc_ctx;
159 return 0; 158 return 0;
160} 159}
161 160
diff --git a/drivers/media/pci/saa7134/saa7134-video.c b/drivers/media/pci/saa7134/saa7134-video.c
index ffa39543eb65..8a6ebd087889 100644
--- a/drivers/media/pci/saa7134/saa7134-video.c
+++ b/drivers/media/pci/saa7134/saa7134-video.c
@@ -963,7 +963,7 @@ static int buffer_prepare(struct vb2_buffer *vb2)
963 963
964static int queue_setup(struct vb2_queue *q, 964static int queue_setup(struct vb2_queue *q,
965 unsigned int *nbuffers, unsigned int *nplanes, 965 unsigned int *nbuffers, unsigned int *nplanes,
966 unsigned int sizes[], void *alloc_ctxs[]) 966 unsigned int sizes[], struct device *alloc_devs[])
967{ 967{
968 struct saa7134_dmaqueue *dmaq = q->drv_priv; 968 struct saa7134_dmaqueue *dmaq = q->drv_priv;
969 struct saa7134_dev *dev = dmaq->dev; 969 struct saa7134_dev *dev = dmaq->dev;
@@ -980,7 +980,6 @@ static int queue_setup(struct vb2_queue *q,
980 *nbuffers = saa7134_buffer_count(size, *nbuffers); 980 *nbuffers = saa7134_buffer_count(size, *nbuffers);
981 *nplanes = 1; 981 *nplanes = 1;
982 sizes[0] = size; 982 sizes[0] = size;
983 alloc_ctxs[0] = dev->alloc_ctx;
984 983
985 saa7134_enable_analog_tuner(dev); 984 saa7134_enable_analog_tuner(dev);
986 985
@@ -2173,6 +2172,7 @@ int saa7134_video_init1(struct saa7134_dev *dev)
2173 q->buf_struct_size = sizeof(struct saa7134_buf); 2172 q->buf_struct_size = sizeof(struct saa7134_buf);
2174 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC; 2173 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
2175 q->lock = &dev->lock; 2174 q->lock = &dev->lock;
2175 q->dev = &dev->pci->dev;
2176 ret = vb2_queue_init(q); 2176 ret = vb2_queue_init(q);
2177 if (ret) 2177 if (ret)
2178 return ret; 2178 return ret;
@@ -2191,6 +2191,7 @@ int saa7134_video_init1(struct saa7134_dev *dev)
2191 q->buf_struct_size = sizeof(struct saa7134_buf); 2191 q->buf_struct_size = sizeof(struct saa7134_buf);
2192 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC; 2192 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
2193 q->lock = &dev->lock; 2193 q->lock = &dev->lock;
2194 q->dev = &dev->pci->dev;
2194 ret = vb2_queue_init(q); 2195 ret = vb2_queue_init(q);
2195 if (ret) 2196 if (ret)
2196 return ret; 2197 return ret;
diff --git a/drivers/media/pci/saa7134/saa7134.h b/drivers/media/pci/saa7134/saa7134.h
index 69a9bbf22d4d..3849083526a7 100644
--- a/drivers/media/pci/saa7134/saa7134.h
+++ b/drivers/media/pci/saa7134/saa7134.h
@@ -610,7 +610,6 @@ struct saa7134_dev {
610 610
611 611
612 /* video+ts+vbi capture */ 612 /* video+ts+vbi capture */
613 void *alloc_ctx;
614 struct saa7134_dmaqueue video_q; 613 struct saa7134_dmaqueue video_q;
615 struct vb2_queue video_vbq; 614 struct vb2_queue video_vbq;
616 struct saa7134_dmaqueue vbi_q; 615 struct saa7134_dmaqueue vbi_q;
@@ -854,7 +853,7 @@ int saa7134_ts_buffer_init(struct vb2_buffer *vb2);
854int saa7134_ts_buffer_prepare(struct vb2_buffer *vb2); 853int saa7134_ts_buffer_prepare(struct vb2_buffer *vb2);
855int saa7134_ts_queue_setup(struct vb2_queue *q, 854int saa7134_ts_queue_setup(struct vb2_queue *q,
856 unsigned int *nbuffers, unsigned int *nplanes, 855 unsigned int *nbuffers, unsigned int *nplanes,
857 unsigned int sizes[], void *alloc_ctxs[]); 856 unsigned int sizes[], struct device *alloc_devs[]);
858int saa7134_ts_start_streaming(struct vb2_queue *vq, unsigned int count); 857int saa7134_ts_start_streaming(struct vb2_queue *vq, unsigned int count);
859void saa7134_ts_stop_streaming(struct vb2_queue *vq); 858void saa7134_ts_stop_streaming(struct vb2_queue *vq);
860 859
diff --git a/drivers/media/pci/saa7164/saa7164-encoder.c b/drivers/media/pci/saa7164/saa7164-encoder.c
index 1b184c39ba97..32a353d162e7 100644
--- a/drivers/media/pci/saa7164/saa7164-encoder.c
+++ b/drivers/media/pci/saa7164/saa7164-encoder.c
@@ -1022,8 +1022,7 @@ int saa7164_encoder_register(struct saa7164_port *port)
1022 1022
1023 dprintk(DBGLVL_ENC, "%s()\n", __func__); 1023 dprintk(DBGLVL_ENC, "%s()\n", __func__);
1024 1024
1025 if (port->type != SAA7164_MPEG_ENCODER) 1025 BUG_ON(port->type != SAA7164_MPEG_ENCODER);
1026 BUG();
1027 1026
1028 /* Sanity check that the PCI configuration space is active */ 1027 /* Sanity check that the PCI configuration space is active */
1029 if (port->hwcfg.BARLocation == 0) { 1028 if (port->hwcfg.BARLocation == 0) {
@@ -1151,8 +1150,7 @@ void saa7164_encoder_unregister(struct saa7164_port *port)
1151 1150
1152 dprintk(DBGLVL_ENC, "%s(port=%d)\n", __func__, port->nr); 1151 dprintk(DBGLVL_ENC, "%s(port=%d)\n", __func__, port->nr);
1153 1152
1154 if (port->type != SAA7164_MPEG_ENCODER) 1153 BUG_ON(port->type != SAA7164_MPEG_ENCODER);
1155 BUG();
1156 1154
1157 if (port->v4l_device) { 1155 if (port->v4l_device) {
1158 if (port->v4l_device->minor != -1) 1156 if (port->v4l_device->minor != -1)
diff --git a/drivers/media/pci/saa7164/saa7164.h b/drivers/media/pci/saa7164/saa7164.h
index 8337524bfb8c..97411b0384c1 100644
--- a/drivers/media/pci/saa7164/saa7164.h
+++ b/drivers/media/pci/saa7164/saa7164.h
@@ -263,10 +263,6 @@ struct saa7164_i2c {
263 u32 i2c_rc; 263 u32 i2c_rc;
264}; 264};
265 265
266struct saa7164_ctrl {
267 struct v4l2_queryctrl v;
268};
269
270struct saa7164_tvnorm { 266struct saa7164_tvnorm {
271 char *name; 267 char *name;
272 v4l2_std_id id; 268 v4l2_std_id id;
diff --git a/drivers/media/pci/solo6x10/solo6x10-v4l2-enc.c b/drivers/media/pci/solo6x10/solo6x10-v4l2-enc.c
index 67a14c41c227..399164314c28 100644
--- a/drivers/media/pci/solo6x10/solo6x10-v4l2-enc.c
+++ b/drivers/media/pci/solo6x10/solo6x10-v4l2-enc.c
@@ -33,7 +33,7 @@
33#include "solo6x10-jpeg.h" 33#include "solo6x10-jpeg.h"
34 34
35#define MIN_VID_BUFFERS 2 35#define MIN_VID_BUFFERS 2
36#define FRAME_BUF_SIZE (196 * 1024) 36#define FRAME_BUF_SIZE (400 * 1024)
37#define MP4_QS 16 37#define MP4_QS 16
38#define DMA_ALIGN 4096 38#define DMA_ALIGN 4096
39 39
@@ -664,12 +664,9 @@ static int solo_ring_thread(void *data)
664static int solo_enc_queue_setup(struct vb2_queue *q, 664static int solo_enc_queue_setup(struct vb2_queue *q,
665 unsigned int *num_buffers, 665 unsigned int *num_buffers,
666 unsigned int *num_planes, unsigned int sizes[], 666 unsigned int *num_planes, unsigned int sizes[],
667 void *alloc_ctxs[]) 667 struct device *alloc_devs[])
668{ 668{
669 struct solo_enc_dev *solo_enc = vb2_get_drv_priv(q);
670
671 sizes[0] = FRAME_BUF_SIZE; 669 sizes[0] = FRAME_BUF_SIZE;
672 alloc_ctxs[0] = solo_enc->alloc_ctx;
673 *num_planes = 1; 670 *num_planes = 1;
674 671
675 if (*num_buffers < MIN_VID_BUFFERS) 672 if (*num_buffers < MIN_VID_BUFFERS)
@@ -1239,11 +1236,6 @@ static struct solo_enc_dev *solo_enc_alloc(struct solo_dev *solo_dev,
1239 return ERR_PTR(-ENOMEM); 1236 return ERR_PTR(-ENOMEM);
1240 1237
1241 hdl = &solo_enc->hdl; 1238 hdl = &solo_enc->hdl;
1242 solo_enc->alloc_ctx = vb2_dma_sg_init_ctx(&solo_dev->pdev->dev);
1243 if (IS_ERR(solo_enc->alloc_ctx)) {
1244 ret = PTR_ERR(solo_enc->alloc_ctx);
1245 goto hdl_free;
1246 }
1247 v4l2_ctrl_handler_init(hdl, 10); 1239 v4l2_ctrl_handler_init(hdl, 10);
1248 v4l2_ctrl_new_std(hdl, &solo_ctrl_ops, 1240 v4l2_ctrl_new_std(hdl, &solo_ctrl_ops,
1249 V4L2_CID_BRIGHTNESS, 0, 255, 1, 128); 1241 V4L2_CID_BRIGHTNESS, 0, 255, 1, 128);
@@ -1299,6 +1291,7 @@ static struct solo_enc_dev *solo_enc_alloc(struct solo_dev *solo_dev,
1299 solo_enc->vidq.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC; 1291 solo_enc->vidq.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1300 solo_enc->vidq.buf_struct_size = sizeof(struct solo_vb2_buf); 1292 solo_enc->vidq.buf_struct_size = sizeof(struct solo_vb2_buf);
1301 solo_enc->vidq.lock = &solo_enc->lock; 1293 solo_enc->vidq.lock = &solo_enc->lock;
1294 solo_enc->vidq.dev = &solo_dev->pdev->dev;
1302 ret = vb2_queue_init(&solo_enc->vidq); 1295 ret = vb2_queue_init(&solo_enc->vidq);
1303 if (ret) 1296 if (ret)
1304 goto hdl_free; 1297 goto hdl_free;
@@ -1347,7 +1340,6 @@ pci_free:
1347 solo_enc->desc_items, solo_enc->desc_dma); 1340 solo_enc->desc_items, solo_enc->desc_dma);
1348hdl_free: 1341hdl_free:
1349 v4l2_ctrl_handler_free(hdl); 1342 v4l2_ctrl_handler_free(hdl);
1350 vb2_dma_sg_cleanup_ctx(solo_enc->alloc_ctx);
1351 kfree(solo_enc); 1343 kfree(solo_enc);
1352 return ERR_PTR(ret); 1344 return ERR_PTR(ret);
1353} 1345}
@@ -1362,7 +1354,6 @@ static void solo_enc_free(struct solo_enc_dev *solo_enc)
1362 solo_enc->desc_items, solo_enc->desc_dma); 1354 solo_enc->desc_items, solo_enc->desc_dma);
1363 video_unregister_device(solo_enc->vfd); 1355 video_unregister_device(solo_enc->vfd);
1364 v4l2_ctrl_handler_free(&solo_enc->hdl); 1356 v4l2_ctrl_handler_free(&solo_enc->hdl);
1365 vb2_dma_sg_cleanup_ctx(solo_enc->alloc_ctx);
1366 kfree(solo_enc); 1357 kfree(solo_enc);
1367} 1358}
1368 1359
diff --git a/drivers/media/pci/solo6x10/solo6x10-v4l2.c b/drivers/media/pci/solo6x10/solo6x10-v4l2.c
index 721ff5320de7..b4be47969b6b 100644
--- a/drivers/media/pci/solo6x10/solo6x10-v4l2.c
+++ b/drivers/media/pci/solo6x10/solo6x10-v4l2.c
@@ -315,12 +315,11 @@ static void solo_stop_thread(struct solo_dev *solo_dev)
315 315
316static int solo_queue_setup(struct vb2_queue *q, 316static int solo_queue_setup(struct vb2_queue *q,
317 unsigned int *num_buffers, unsigned int *num_planes, 317 unsigned int *num_buffers, unsigned int *num_planes,
318 unsigned int sizes[], void *alloc_ctxs[]) 318 unsigned int sizes[], struct device *alloc_devs[])
319{ 319{
320 struct solo_dev *solo_dev = vb2_get_drv_priv(q); 320 struct solo_dev *solo_dev = vb2_get_drv_priv(q);
321 321
322 sizes[0] = solo_image_size(solo_dev); 322 sizes[0] = solo_image_size(solo_dev);
323 alloc_ctxs[0] = solo_dev->alloc_ctx;
324 *num_planes = 1; 323 *num_planes = 1;
325 324
326 if (*num_buffers < MIN_VID_BUFFERS) 325 if (*num_buffers < MIN_VID_BUFFERS)
@@ -386,26 +385,24 @@ static int solo_querycap(struct file *file, void *priv,
386static int solo_enum_ext_input(struct solo_dev *solo_dev, 385static int solo_enum_ext_input(struct solo_dev *solo_dev,
387 struct v4l2_input *input) 386 struct v4l2_input *input)
388{ 387{
389 static const char * const dispnames_1[] = { "4UP" }; 388 int ext = input->index - solo_dev->nr_chans;
390 static const char * const dispnames_2[] = { "4UP-1", "4UP-2" }; 389 unsigned int nup, first;
391 static const char * const dispnames_5[] = {
392 "4UP-1", "4UP-2", "4UP-3", "4UP-4", "16UP"
393 };
394 const char * const *dispnames;
395 390
396 if (input->index >= (solo_dev->nr_chans + solo_dev->nr_ext)) 391 if (ext >= solo_dev->nr_ext)
397 return -EINVAL; 392 return -EINVAL;
398 393
399 if (solo_dev->nr_ext == 5) 394 nup = (ext == 4) ? 16 : 4;
400 dispnames = dispnames_5; 395 first = (ext & 3) << 2; /* first channel in the n-up */
401 else if (solo_dev->nr_ext == 2) 396 snprintf(input->name, sizeof(input->name),
402 dispnames = dispnames_2; 397 "Multi %d-up (cameras %d-%d)",
403 else 398 nup, first + 1, first + nup);
404 dispnames = dispnames_1; 399 /* Possible outputs:
405 400 * Multi 4-up (cameras 1-4)
406 snprintf(input->name, sizeof(input->name), "Multi %s", 401 * Multi 4-up (cameras 5-8)
407 dispnames[input->index - solo_dev->nr_chans]); 402 * Multi 4-up (cameras 9-12)
408 403 * Multi 4-up (cameras 13-16)
404 * Multi 16-up (cameras 1-16)
405 */
409 return 0; 406 return 0;
410} 407}
411 408
@@ -681,16 +678,11 @@ int solo_v4l2_init(struct solo_dev *solo_dev, unsigned nr)
681 solo_dev->vidq.gfp_flags = __GFP_DMA32 | __GFP_KSWAPD_RECLAIM; 678 solo_dev->vidq.gfp_flags = __GFP_DMA32 | __GFP_KSWAPD_RECLAIM;
682 solo_dev->vidq.buf_struct_size = sizeof(struct solo_vb2_buf); 679 solo_dev->vidq.buf_struct_size = sizeof(struct solo_vb2_buf);
683 solo_dev->vidq.lock = &solo_dev->lock; 680 solo_dev->vidq.lock = &solo_dev->lock;
681 solo_dev->vidq.dev = &solo_dev->pdev->dev;
684 ret = vb2_queue_init(&solo_dev->vidq); 682 ret = vb2_queue_init(&solo_dev->vidq);
685 if (ret < 0) 683 if (ret < 0)
686 goto fail; 684 goto fail;
687 685
688 solo_dev->alloc_ctx = vb2_dma_contig_init_ctx(&solo_dev->pdev->dev);
689 if (IS_ERR(solo_dev->alloc_ctx)) {
690 dev_err(&solo_dev->pdev->dev, "Can't allocate buffer context");
691 return PTR_ERR(solo_dev->alloc_ctx);
692 }
693
694 /* Cycle all the channels and clear */ 686 /* Cycle all the channels and clear */
695 for (i = 0; i < solo_dev->nr_chans; i++) { 687 for (i = 0; i < solo_dev->nr_chans; i++) {
696 solo_v4l2_set_ch(solo_dev, i); 688 solo_v4l2_set_ch(solo_dev, i);
@@ -718,7 +710,6 @@ int solo_v4l2_init(struct solo_dev *solo_dev, unsigned nr)
718 710
719fail: 711fail:
720 video_device_release(solo_dev->vfd); 712 video_device_release(solo_dev->vfd);
721 vb2_dma_contig_cleanup_ctx(solo_dev->alloc_ctx);
722 v4l2_ctrl_handler_free(&solo_dev->disp_hdl); 713 v4l2_ctrl_handler_free(&solo_dev->disp_hdl);
723 solo_dev->vfd = NULL; 714 solo_dev->vfd = NULL;
724 return ret; 715 return ret;
@@ -730,7 +721,6 @@ void solo_v4l2_exit(struct solo_dev *solo_dev)
730 return; 721 return;
731 722
732 video_unregister_device(solo_dev->vfd); 723 video_unregister_device(solo_dev->vfd);
733 vb2_dma_contig_cleanup_ctx(solo_dev->alloc_ctx);
734 v4l2_ctrl_handler_free(&solo_dev->disp_hdl); 724 v4l2_ctrl_handler_free(&solo_dev->disp_hdl);
735 solo_dev->vfd = NULL; 725 solo_dev->vfd = NULL;
736} 726}
diff --git a/drivers/media/pci/solo6x10/solo6x10.h b/drivers/media/pci/solo6x10/solo6x10.h
index 4ab6586c0467..5bd498735a66 100644
--- a/drivers/media/pci/solo6x10/solo6x10.h
+++ b/drivers/media/pci/solo6x10/solo6x10.h
@@ -178,7 +178,6 @@ struct solo_enc_dev {
178 u32 sequence; 178 u32 sequence;
179 struct vb2_queue vidq; 179 struct vb2_queue vidq;
180 struct list_head vidq_active; 180 struct list_head vidq_active;
181 void *alloc_ctx;
182 int desc_count; 181 int desc_count;
183 int desc_nelts; 182 int desc_nelts;
184 struct solo_p2m_desc *desc_items; 183 struct solo_p2m_desc *desc_items;
@@ -269,7 +268,6 @@ struct solo_dev {
269 268
270 /* Buffer handling */ 269 /* Buffer handling */
271 struct vb2_queue vidq; 270 struct vb2_queue vidq;
272 struct vb2_alloc_ctx *alloc_ctx;
273 u32 sequence; 271 u32 sequence;
274 struct task_struct *kthread; 272 struct task_struct *kthread;
275 struct mutex lock; 273 struct mutex lock;
diff --git a/drivers/media/pci/sta2x11/sta2x11_vip.c b/drivers/media/pci/sta2x11/sta2x11_vip.c
index 1fc195f89686..aeb2b4e2db35 100644
--- a/drivers/media/pci/sta2x11/sta2x11_vip.c
+++ b/drivers/media/pci/sta2x11/sta2x11_vip.c
@@ -111,7 +111,6 @@ static inline struct vip_buffer *to_vip_buffer(struct vb2_v4l2_buffer *vb2)
111 * @input: input line for video signal ( 0 or 1 ) 111 * @input: input line for video signal ( 0 or 1 )
112 * @disabled: Device is in power down state 112 * @disabled: Device is in power down state
113 * @slock: for excluse acces of registers 113 * @slock: for excluse acces of registers
114 * @alloc_ctx: context for videobuf2
115 * @vb_vidq: queue maintained by videobuf2 layer 114 * @vb_vidq: queue maintained by videobuf2 layer
116 * @buffer_list: list of buffer in use 115 * @buffer_list: list of buffer in use
117 * @sequence: sequence number of acquired buffer 116 * @sequence: sequence number of acquired buffer
@@ -141,7 +140,6 @@ struct sta2x11_vip {
141 int disabled; 140 int disabled;
142 spinlock_t slock; 141 spinlock_t slock;
143 142
144 struct vb2_alloc_ctx *alloc_ctx;
145 struct vb2_queue vb_vidq; 143 struct vb2_queue vb_vidq;
146 struct list_head buffer_list; 144 struct list_head buffer_list;
147 unsigned int sequence; 145 unsigned int sequence;
@@ -267,7 +265,7 @@ static void vip_active_buf_next(struct sta2x11_vip *vip)
267/* Videobuf2 Operations */ 265/* Videobuf2 Operations */
268static int queue_setup(struct vb2_queue *vq, 266static int queue_setup(struct vb2_queue *vq,
269 unsigned int *nbuffers, unsigned int *nplanes, 267 unsigned int *nbuffers, unsigned int *nplanes,
270 unsigned int sizes[], void *alloc_ctxs[]) 268 unsigned int sizes[], struct device *alloc_devs[])
271{ 269{
272 struct sta2x11_vip *vip = vb2_get_drv_priv(vq); 270 struct sta2x11_vip *vip = vb2_get_drv_priv(vq);
273 271
@@ -276,7 +274,6 @@ static int queue_setup(struct vb2_queue *vq,
276 274
277 *nplanes = 1; 275 *nplanes = 1;
278 sizes[0] = vip->format.sizeimage; 276 sizes[0] = vip->format.sizeimage;
279 alloc_ctxs[0] = vip->alloc_ctx;
280 277
281 vip->sequence = 0; 278 vip->sequence = 0;
282 vip->active = NULL; 279 vip->active = NULL;
@@ -861,25 +858,15 @@ static int sta2x11_vip_init_buffer(struct sta2x11_vip *vip)
861 vip->vb_vidq.ops = &vip_video_qops; 858 vip->vb_vidq.ops = &vip_video_qops;
862 vip->vb_vidq.mem_ops = &vb2_dma_contig_memops; 859 vip->vb_vidq.mem_ops = &vb2_dma_contig_memops;
863 vip->vb_vidq.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC; 860 vip->vb_vidq.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
861 vip->vb_vidq.dev = &vip->pdev->dev;
864 err = vb2_queue_init(&vip->vb_vidq); 862 err = vb2_queue_init(&vip->vb_vidq);
865 if (err) 863 if (err)
866 return err; 864 return err;
867 INIT_LIST_HEAD(&vip->buffer_list); 865 INIT_LIST_HEAD(&vip->buffer_list);
868 spin_lock_init(&vip->lock); 866 spin_lock_init(&vip->lock);
869
870
871 vip->alloc_ctx = vb2_dma_contig_init_ctx(&vip->pdev->dev);
872 if (IS_ERR(vip->alloc_ctx)) {
873 v4l2_err(&vip->v4l2_dev, "Can't allocate buffer context");
874 return PTR_ERR(vip->alloc_ctx);
875 }
876
877 return 0; 867 return 0;
878} 868}
879static void sta2x11_vip_release_buffer(struct sta2x11_vip *vip) 869
880{
881 vb2_dma_contig_cleanup_ctx(vip->alloc_ctx);
882}
883static int sta2x11_vip_init_controls(struct sta2x11_vip *vip) 870static int sta2x11_vip_init_controls(struct sta2x11_vip *vip)
884{ 871{
885 /* 872 /*
@@ -1120,7 +1107,6 @@ vrelease:
1120 video_unregister_device(&vip->video_dev); 1107 video_unregister_device(&vip->video_dev);
1121 free_irq(pdev->irq, vip); 1108 free_irq(pdev->irq, vip);
1122release_buf: 1109release_buf:
1123 sta2x11_vip_release_buffer(vip);
1124 pci_disable_msi(pdev); 1110 pci_disable_msi(pdev);
1125unmap: 1111unmap:
1126 vb2_queue_release(&vip->vb_vidq); 1112 vb2_queue_release(&vip->vb_vidq);
diff --git a/drivers/media/pci/tw68/tw68-core.c b/drivers/media/pci/tw68/tw68-core.c
index 4e77618fbb2b..8474528be91e 100644
--- a/drivers/media/pci/tw68/tw68-core.c
+++ b/drivers/media/pci/tw68/tw68-core.c
@@ -305,19 +305,13 @@ static int tw68_initdev(struct pci_dev *pci_dev,
305 /* Then do any initialisation wanted before interrupts are on */ 305 /* Then do any initialisation wanted before interrupts are on */
306 tw68_hw_init1(dev); 306 tw68_hw_init1(dev);
307 307
308 dev->alloc_ctx = vb2_dma_sg_init_ctx(&pci_dev->dev);
309 if (IS_ERR(dev->alloc_ctx)) {
310 err = PTR_ERR(dev->alloc_ctx);
311 goto fail3;
312 }
313
314 /* get irq */ 308 /* get irq */
315 err = devm_request_irq(&pci_dev->dev, pci_dev->irq, tw68_irq, 309 err = devm_request_irq(&pci_dev->dev, pci_dev->irq, tw68_irq,
316 IRQF_SHARED, dev->name, dev); 310 IRQF_SHARED, dev->name, dev);
317 if (err < 0) { 311 if (err < 0) {
318 pr_err("%s: can't get IRQ %d\n", 312 pr_err("%s: can't get IRQ %d\n",
319 dev->name, pci_dev->irq); 313 dev->name, pci_dev->irq);
320 goto fail4; 314 goto fail3;
321 } 315 }
322 316
323 /* 317 /*
@@ -331,7 +325,7 @@ static int tw68_initdev(struct pci_dev *pci_dev,
331 if (err < 0) { 325 if (err < 0) {
332 pr_err("%s: can't register video device\n", 326 pr_err("%s: can't register video device\n",
333 dev->name); 327 dev->name);
334 goto fail5; 328 goto fail4;
335 } 329 }
336 tw_setl(TW68_INTMASK, dev->pci_irqmask); 330 tw_setl(TW68_INTMASK, dev->pci_irqmask);
337 331
@@ -340,10 +334,8 @@ static int tw68_initdev(struct pci_dev *pci_dev,
340 334
341 return 0; 335 return 0;
342 336
343fail5:
344 video_unregister_device(&dev->vdev);
345fail4: 337fail4:
346 vb2_dma_sg_cleanup_ctx(dev->alloc_ctx); 338 video_unregister_device(&dev->vdev);
347fail3: 339fail3:
348 iounmap(dev->lmmio); 340 iounmap(dev->lmmio);
349fail2: 341fail2:
@@ -367,7 +359,6 @@ static void tw68_finidev(struct pci_dev *pci_dev)
367 /* unregister */ 359 /* unregister */
368 video_unregister_device(&dev->vdev); 360 video_unregister_device(&dev->vdev);
369 v4l2_ctrl_handler_free(&dev->hdl); 361 v4l2_ctrl_handler_free(&dev->hdl);
370 vb2_dma_sg_cleanup_ctx(dev->alloc_ctx);
371 362
372 /* release resources */ 363 /* release resources */
373 iounmap(dev->lmmio); 364 iounmap(dev->lmmio);
diff --git a/drivers/media/pci/tw68/tw68-video.c b/drivers/media/pci/tw68/tw68-video.c
index 07116a87a57b..5e8212845c87 100644
--- a/drivers/media/pci/tw68/tw68-video.c
+++ b/drivers/media/pci/tw68/tw68-video.c
@@ -378,7 +378,7 @@ static int tw68_buffer_count(unsigned int size, unsigned int count)
378 378
379static int tw68_queue_setup(struct vb2_queue *q, 379static int tw68_queue_setup(struct vb2_queue *q,
380 unsigned int *num_buffers, unsigned int *num_planes, 380 unsigned int *num_buffers, unsigned int *num_planes,
381 unsigned int sizes[], void *alloc_ctxs[]) 381 unsigned int sizes[], struct device *alloc_devs[])
382{ 382{
383 struct tw68_dev *dev = vb2_get_drv_priv(q); 383 struct tw68_dev *dev = vb2_get_drv_priv(q);
384 unsigned tot_bufs = q->num_buffers + *num_buffers; 384 unsigned tot_bufs = q->num_buffers + *num_buffers;
@@ -388,7 +388,6 @@ static int tw68_queue_setup(struct vb2_queue *q,
388 tot_bufs = 2; 388 tot_bufs = 2;
389 tot_bufs = tw68_buffer_count(size, tot_bufs); 389 tot_bufs = tw68_buffer_count(size, tot_bufs);
390 *num_buffers = tot_bufs - q->num_buffers; 390 *num_buffers = tot_bufs - q->num_buffers;
391 alloc_ctxs[0] = dev->alloc_ctx;
392 /* 391 /*
393 * We allow create_bufs, but only if the sizeimage is >= as the 392 * We allow create_bufs, but only if the sizeimage is >= as the
394 * current sizeimage. The tw68_buffer_count calculation becomes quite 393 * current sizeimage. The tw68_buffer_count calculation becomes quite
@@ -983,6 +982,7 @@ int tw68_video_init2(struct tw68_dev *dev, int video_nr)
983 dev->vidq.buf_struct_size = sizeof(struct tw68_buf); 982 dev->vidq.buf_struct_size = sizeof(struct tw68_buf);
984 dev->vidq.lock = &dev->lock; 983 dev->vidq.lock = &dev->lock;
985 dev->vidq.min_buffers_needed = 2; 984 dev->vidq.min_buffers_needed = 2;
985 dev->vidq.dev = &dev->pci->dev;
986 ret = vb2_queue_init(&dev->vidq); 986 ret = vb2_queue_init(&dev->vidq);
987 if (ret) 987 if (ret)
988 return ret; 988 return ret;
diff --git a/drivers/media/pci/tw68/tw68.h b/drivers/media/pci/tw68/tw68.h
index 6c7dcb300f34..5585c7ee23f2 100644
--- a/drivers/media/pci/tw68/tw68.h
+++ b/drivers/media/pci/tw68/tw68.h
@@ -165,7 +165,6 @@ struct tw68_dev {
165 unsigned field; 165 unsigned field;
166 struct vb2_queue vidq; 166 struct vb2_queue vidq;
167 struct list_head active; 167 struct list_head active;
168 void *alloc_ctx;
169 168
170 /* various v4l controls */ 169 /* various v4l controls */
171 const struct tw68_tvnorm *tvnorm; /* video */ 170 const struct tw68_tvnorm *tvnorm; /* video */
diff --git a/drivers/media/pci/tw686x/Kconfig b/drivers/media/pci/tw686x/Kconfig
index fb8536974052..34ff37712313 100644
--- a/drivers/media/pci/tw686x/Kconfig
+++ b/drivers/media/pci/tw686x/Kconfig
@@ -3,6 +3,8 @@ config VIDEO_TW686X
3 depends on PCI && VIDEO_DEV && VIDEO_V4L2 && SND 3 depends on PCI && VIDEO_DEV && VIDEO_V4L2 && SND
4 depends on HAS_DMA 4 depends on HAS_DMA
5 select VIDEOBUF2_VMALLOC 5 select VIDEOBUF2_VMALLOC
6 select VIDEOBUF2_DMA_CONTIG
7 select VIDEOBUF2_DMA_SG
6 select SND_PCM 8 select SND_PCM
7 help 9 help
8 Support for Intersil/Techwell TW686x-based frame grabber cards. 10 Support for Intersil/Techwell TW686x-based frame grabber cards.
diff --git a/drivers/media/pci/tw686x/tw686x-audio.c b/drivers/media/pci/tw686x/tw686x-audio.c
index 91459ab715b2..96e444c49173 100644
--- a/drivers/media/pci/tw686x/tw686x-audio.c
+++ b/drivers/media/pci/tw686x/tw686x-audio.c
@@ -62,12 +62,22 @@ void tw686x_audio_irq(struct tw686x_dev *dev, unsigned long requests,
62 } 62 }
63 spin_unlock_irqrestore(&ac->lock, flags); 63 spin_unlock_irqrestore(&ac->lock, flags);
64 64
65 if (!done || !next)
66 continue;
67 /*
68 * Checking for a non-nil dma_desc[pb]->virt buffer is
69 * the same as checking for memcpy DMA mode.
70 */
65 desc = &ac->dma_descs[pb]; 71 desc = &ac->dma_descs[pb];
66 if (done && next && desc->virt) { 72 if (desc->virt) {
67 memcpy(done->virt, desc->virt, desc->size); 73 memcpy(done->virt, desc->virt,
68 ac->ptr = done->dma - ac->buf[0].dma; 74 dev->period_size);
69 snd_pcm_period_elapsed(ac->ss); 75 } else {
76 u32 reg = pb ? ADMA_B_ADDR[ch] : ADMA_P_ADDR[ch];
77 reg_write(dev, reg, next->dma);
70 } 78 }
79 ac->ptr = done->dma - ac->buf[0].dma;
80 snd_pcm_period_elapsed(ac->ss);
71 } 81 }
72} 82}
73 83
@@ -83,10 +93,9 @@ static int tw686x_pcm_hw_free(struct snd_pcm_substream *ss)
83} 93}
84 94
85/* 95/*
86 * The audio device rate is global and shared among all 96 * Audio parameters are global and shared among all
87 * capture channels. The driver makes no effort to prevent 97 * capture channels. The driver prevents changes to
88 * rate modifications. User is free change the rate, but it 98 * the parameters if any audio channel is capturing.
89 * means changing the rate for all capture sub-devices.
90 */ 99 */
91static const struct snd_pcm_hardware tw686x_capture_hw = { 100static const struct snd_pcm_hardware tw686x_capture_hw = {
92 .info = (SNDRV_PCM_INFO_MMAP | 101 .info = (SNDRV_PCM_INFO_MMAP |
@@ -99,9 +108,9 @@ static const struct snd_pcm_hardware tw686x_capture_hw = {
99 .rate_max = 48000, 108 .rate_max = 48000,
100 .channels_min = 1, 109 .channels_min = 1,
101 .channels_max = 1, 110 .channels_max = 1,
102 .buffer_bytes_max = TW686X_AUDIO_PAGE_MAX * TW686X_AUDIO_PAGE_SZ, 111 .buffer_bytes_max = TW686X_AUDIO_PAGE_MAX * AUDIO_DMA_SIZE_MAX,
103 .period_bytes_min = TW686X_AUDIO_PAGE_SZ, 112 .period_bytes_min = AUDIO_DMA_SIZE_MIN,
104 .period_bytes_max = TW686X_AUDIO_PAGE_SZ, 113 .period_bytes_max = AUDIO_DMA_SIZE_MAX,
105 .periods_min = TW686X_AUDIO_PERIODS_MIN, 114 .periods_min = TW686X_AUDIO_PERIODS_MIN,
106 .periods_max = TW686X_AUDIO_PERIODS_MAX, 115 .periods_max = TW686X_AUDIO_PERIODS_MAX,
107}; 116};
@@ -143,6 +152,14 @@ static int tw686x_pcm_prepare(struct snd_pcm_substream *ss)
143 int i; 152 int i;
144 153
145 spin_lock_irqsave(&dev->lock, flags); 154 spin_lock_irqsave(&dev->lock, flags);
155 /*
156 * Given the audio parameters are global (i.e. shared across
157 * DMA channels), we need to check new params are allowed.
158 */
159 if (((dev->audio_rate != rt->rate) ||
160 (dev->period_size != period_size)) && dev->audio_enabled)
161 goto err_audio_busy;
162
146 tw686x_disable_channel(dev, AUDIO_CHANNEL_OFFSET + ac->ch); 163 tw686x_disable_channel(dev, AUDIO_CHANNEL_OFFSET + ac->ch);
147 spin_unlock_irqrestore(&dev->lock, flags); 164 spin_unlock_irqrestore(&dev->lock, flags);
148 165
@@ -156,12 +173,21 @@ static int tw686x_pcm_prepare(struct snd_pcm_substream *ss)
156 reg_write(dev, AUDIO_CONTROL2, reg); 173 reg_write(dev, AUDIO_CONTROL2, reg);
157 } 174 }
158 175
159 if (period_size != TW686X_AUDIO_PAGE_SZ || 176 if (dev->period_size != period_size) {
160 rt->periods < TW686X_AUDIO_PERIODS_MIN || 177 u32 reg;
161 rt->periods > TW686X_AUDIO_PERIODS_MAX) { 178
162 return -EINVAL; 179 dev->period_size = period_size;
180 reg = reg_read(dev, AUDIO_CONTROL1);
181 reg &= ~(AUDIO_DMA_SIZE_MASK << AUDIO_DMA_SIZE_SHIFT);
182 reg |= period_size << AUDIO_DMA_SIZE_SHIFT;
183
184 reg_write(dev, AUDIO_CONTROL1, reg);
163 } 185 }
164 186
187 if (rt->periods < TW686X_AUDIO_PERIODS_MIN ||
188 rt->periods > TW686X_AUDIO_PERIODS_MAX)
189 return -EINVAL;
190
165 spin_lock_irqsave(&ac->lock, flags); 191 spin_lock_irqsave(&ac->lock, flags);
166 INIT_LIST_HEAD(&ac->buf_list); 192 INIT_LIST_HEAD(&ac->buf_list);
167 193
@@ -181,9 +207,19 @@ static int tw686x_pcm_prepare(struct snd_pcm_substream *ss)
181 ac->curr_bufs[0] = p_buf; 207 ac->curr_bufs[0] = p_buf;
182 ac->curr_bufs[1] = b_buf; 208 ac->curr_bufs[1] = b_buf;
183 ac->ptr = 0; 209 ac->ptr = 0;
210
211 if (dev->dma_mode != TW686X_DMA_MODE_MEMCPY) {
212 reg_write(dev, ADMA_P_ADDR[ac->ch], p_buf->dma);
213 reg_write(dev, ADMA_B_ADDR[ac->ch], b_buf->dma);
214 }
215
184 spin_unlock_irqrestore(&ac->lock, flags); 216 spin_unlock_irqrestore(&ac->lock, flags);
185 217
186 return 0; 218 return 0;
219
220err_audio_busy:
221 spin_unlock_irqrestore(&dev->lock, flags);
222 return -EBUSY;
187} 223}
188 224
189static int tw686x_pcm_trigger(struct snd_pcm_substream *ss, int cmd) 225static int tw686x_pcm_trigger(struct snd_pcm_substream *ss, int cmd)
@@ -197,6 +233,7 @@ static int tw686x_pcm_trigger(struct snd_pcm_substream *ss, int cmd)
197 case SNDRV_PCM_TRIGGER_START: 233 case SNDRV_PCM_TRIGGER_START:
198 if (ac->curr_bufs[0] && ac->curr_bufs[1]) { 234 if (ac->curr_bufs[0] && ac->curr_bufs[1]) {
199 spin_lock_irqsave(&dev->lock, flags); 235 spin_lock_irqsave(&dev->lock, flags);
236 dev->audio_enabled = 1;
200 tw686x_enable_channel(dev, 237 tw686x_enable_channel(dev,
201 AUDIO_CHANNEL_OFFSET + ac->ch); 238 AUDIO_CHANNEL_OFFSET + ac->ch);
202 spin_unlock_irqrestore(&dev->lock, flags); 239 spin_unlock_irqrestore(&dev->lock, flags);
@@ -209,6 +246,7 @@ static int tw686x_pcm_trigger(struct snd_pcm_substream *ss, int cmd)
209 break; 246 break;
210 case SNDRV_PCM_TRIGGER_STOP: 247 case SNDRV_PCM_TRIGGER_STOP:
211 spin_lock_irqsave(&dev->lock, flags); 248 spin_lock_irqsave(&dev->lock, flags);
249 dev->audio_enabled = 0;
212 tw686x_disable_channel(dev, AUDIO_CHANNEL_OFFSET + ac->ch); 250 tw686x_disable_channel(dev, AUDIO_CHANNEL_OFFSET + ac->ch);
213 spin_unlock_irqrestore(&dev->lock, flags); 251 spin_unlock_irqrestore(&dev->lock, flags);
214 252
@@ -266,8 +304,8 @@ static int tw686x_snd_pcm_init(struct tw686x_dev *dev)
266 return snd_pcm_lib_preallocate_pages_for_all(pcm, 304 return snd_pcm_lib_preallocate_pages_for_all(pcm,
267 SNDRV_DMA_TYPE_DEV, 305 SNDRV_DMA_TYPE_DEV,
268 snd_dma_pci_data(dev->pci_dev), 306 snd_dma_pci_data(dev->pci_dev),
269 TW686X_AUDIO_PAGE_MAX * TW686X_AUDIO_PAGE_SZ, 307 TW686X_AUDIO_PAGE_MAX * AUDIO_DMA_SIZE_MAX,
270 TW686X_AUDIO_PAGE_MAX * TW686X_AUDIO_PAGE_SZ); 308 TW686X_AUDIO_PAGE_MAX * AUDIO_DMA_SIZE_MAX);
271} 309}
272 310
273static void tw686x_audio_dma_free(struct tw686x_dev *dev, 311static void tw686x_audio_dma_free(struct tw686x_dev *dev,
@@ -290,11 +328,19 @@ static int tw686x_audio_dma_alloc(struct tw686x_dev *dev,
290{ 328{
291 int pb; 329 int pb;
292 330
331 /*
332 * In the memcpy DMA mode we allocate a consistent buffer
333 * and use it for the DMA capture. Otherwise, DMA
334 * acts on the ALSA buffers as received in pcm_prepare.
335 */
336 if (dev->dma_mode != TW686X_DMA_MODE_MEMCPY)
337 return 0;
338
293 for (pb = 0; pb < 2; pb++) { 339 for (pb = 0; pb < 2; pb++) {
294 u32 reg = pb ? ADMA_B_ADDR[ac->ch] : ADMA_P_ADDR[ac->ch]; 340 u32 reg = pb ? ADMA_B_ADDR[ac->ch] : ADMA_P_ADDR[ac->ch];
295 void *virt; 341 void *virt;
296 342
297 virt = pci_alloc_consistent(dev->pci_dev, TW686X_AUDIO_PAGE_SZ, 343 virt = pci_alloc_consistent(dev->pci_dev, AUDIO_DMA_SIZE_MAX,
298 &ac->dma_descs[pb].phys); 344 &ac->dma_descs[pb].phys);
299 if (!virt) { 345 if (!virt) {
300 dev_err(&dev->pci_dev->dev, 346 dev_err(&dev->pci_dev->dev,
@@ -303,7 +349,7 @@ static int tw686x_audio_dma_alloc(struct tw686x_dev *dev,
303 return -ENOMEM; 349 return -ENOMEM;
304 } 350 }
305 ac->dma_descs[pb].virt = virt; 351 ac->dma_descs[pb].virt = virt;
306 ac->dma_descs[pb].size = TW686X_AUDIO_PAGE_SZ; 352 ac->dma_descs[pb].size = AUDIO_DMA_SIZE_MAX;
307 reg_write(dev, reg, ac->dma_descs[pb].phys); 353 reg_write(dev, reg, ac->dma_descs[pb].phys);
308 } 354 }
309 return 0; 355 return 0;
@@ -334,12 +380,8 @@ int tw686x_audio_init(struct tw686x_dev *dev)
334 struct snd_card *card; 380 struct snd_card *card;
335 int err, ch; 381 int err, ch;
336 382
337 /* 383 /* Enable external audio */
338 * AUDIO_CONTROL1 384 reg_write(dev, AUDIO_CONTROL1, BIT(0));
339 * DMA byte length [31:19] = 4096 (i.e. ALSA period)
340 * External audio enable [0] = enabled
341 */
342 reg_write(dev, AUDIO_CONTROL1, 0x80000001);
343 385
344 err = snd_card_new(&pci_dev->dev, SNDRV_DEFAULT_IDX1, 386 err = snd_card_new(&pci_dev->dev, SNDRV_DEFAULT_IDX1,
345 SNDRV_DEFAULT_STR1, 387 SNDRV_DEFAULT_STR1,
diff --git a/drivers/media/pci/tw686x/tw686x-core.c b/drivers/media/pci/tw686x/tw686x-core.c
index cf53b0e97be2..71a0453b1af1 100644
--- a/drivers/media/pci/tw686x/tw686x-core.c
+++ b/drivers/media/pci/tw686x/tw686x-core.c
@@ -21,12 +21,14 @@
21 * under stress testings it has been found that the machine can 21 * under stress testings it has been found that the machine can
22 * freeze completely if DMA registers are programmed while streaming 22 * freeze completely if DMA registers are programmed while streaming
23 * is active. 23 * is active.
24 * This driver tries to access hardware registers as infrequently 24 *
25 * as possible by: 25 * Therefore, driver implements a dma_mode called 'memcpy' which
26 * i. allocating fixed DMA buffers and memcpy'ing into 26 * avoids cycling the DMA buffers, and insteads allocates extra DMA buffers
27 * vmalloc'ed buffers 27 * and then copies into vmalloc'ed user buffers.
28 * ii. using a timer to mitigate the rate of DMA reset operations, 28 *
29 * on DMA channels error. 29 * In addition to this, when streaming is on, the driver tries to access
30 * hardware registers as infrequently as possible. This is done by using
31 * a timer to limit the rate at which DMA is reset on DMA channels error.
30 */ 32 */
31 33
32#include <linux/init.h> 34#include <linux/init.h>
@@ -55,6 +57,42 @@ static u32 dma_interval = 0x00098968;
55module_param(dma_interval, int, 0444); 57module_param(dma_interval, int, 0444);
56MODULE_PARM_DESC(dma_interval, "Minimum time span for DMA interrupting host"); 58MODULE_PARM_DESC(dma_interval, "Minimum time span for DMA interrupting host");
57 59
60static unsigned int dma_mode = TW686X_DMA_MODE_MEMCPY;
61static const char *dma_mode_name(unsigned int mode)
62{
63 switch (mode) {
64 case TW686X_DMA_MODE_MEMCPY:
65 return "memcpy";
66 case TW686X_DMA_MODE_CONTIG:
67 return "contig";
68 case TW686X_DMA_MODE_SG:
69 return "sg";
70 default:
71 return "unknown";
72 }
73}
74
75static int tw686x_dma_mode_get(char *buffer, struct kernel_param *kp)
76{
77 return sprintf(buffer, dma_mode_name(dma_mode));
78}
79
80static int tw686x_dma_mode_set(const char *val, struct kernel_param *kp)
81{
82 if (!strcasecmp(val, dma_mode_name(TW686X_DMA_MODE_MEMCPY)))
83 dma_mode = TW686X_DMA_MODE_MEMCPY;
84 else if (!strcasecmp(val, dma_mode_name(TW686X_DMA_MODE_CONTIG)))
85 dma_mode = TW686X_DMA_MODE_CONTIG;
86 else if (!strcasecmp(val, dma_mode_name(TW686X_DMA_MODE_SG)))
87 dma_mode = TW686X_DMA_MODE_SG;
88 else
89 return -EINVAL;
90 return 0;
91}
92module_param_call(dma_mode, tw686x_dma_mode_set, tw686x_dma_mode_get,
93 &dma_mode, S_IRUGO|S_IWUSR);
94MODULE_PARM_DESC(dma_mode, "DMA operation mode (memcpy/contig/sg, default=memcpy)");
95
58void tw686x_disable_channel(struct tw686x_dev *dev, unsigned int channel) 96void tw686x_disable_channel(struct tw686x_dev *dev, unsigned int channel)
59{ 97{
60 u32 dma_en = reg_read(dev, DMA_CHANNEL_ENABLE); 98 u32 dma_en = reg_read(dev, DMA_CHANNEL_ENABLE);
@@ -212,6 +250,7 @@ static int tw686x_probe(struct pci_dev *pci_dev,
212 if (!dev) 250 if (!dev)
213 return -ENOMEM; 251 return -ENOMEM;
214 dev->type = pci_id->driver_data; 252 dev->type = pci_id->driver_data;
253 dev->dma_mode = dma_mode;
215 sprintf(dev->name, "tw%04X", pci_dev->device); 254 sprintf(dev->name, "tw%04X", pci_dev->device);
216 255
217 dev->video_channels = kcalloc(max_channels(dev), 256 dev->video_channels = kcalloc(max_channels(dev),
@@ -228,9 +267,10 @@ static int tw686x_probe(struct pci_dev *pci_dev,
228 goto free_video; 267 goto free_video;
229 } 268 }
230 269
231 pr_info("%s: PCI %s, IRQ %d, MMIO 0x%lx\n", dev->name, 270 pr_info("%s: PCI %s, IRQ %d, MMIO 0x%lx (%s mode)\n", dev->name,
232 pci_name(pci_dev), pci_dev->irq, 271 pci_name(pci_dev), pci_dev->irq,
233 (unsigned long)pci_resource_start(pci_dev, 0)); 272 (unsigned long)pci_resource_start(pci_dev, 0),
273 dma_mode_name(dma_mode));
234 274
235 dev->pci_dev = pci_dev; 275 dev->pci_dev = pci_dev;
236 if (pci_enable_device(pci_dev)) { 276 if (pci_enable_device(pci_dev)) {
diff --git a/drivers/media/pci/tw686x/tw686x-regs.h b/drivers/media/pci/tw686x/tw686x-regs.h
index fcef586a4c8c..15a956642ef4 100644
--- a/drivers/media/pci/tw686x/tw686x-regs.h
+++ b/drivers/media/pci/tw686x/tw686x-regs.h
@@ -105,6 +105,10 @@
105 0x2d0, 0x2d1, 0x2d2, 0x2d3 }) 105 0x2d0, 0x2d1, 0x2d2, 0x2d3 })
106 106
107#define SYS_MODE_DMA_SHIFT 13 107#define SYS_MODE_DMA_SHIFT 13
108#define AUDIO_DMA_SIZE_SHIFT 19
109#define AUDIO_DMA_SIZE_MIN SZ_512
110#define AUDIO_DMA_SIZE_MAX SZ_4K
111#define AUDIO_DMA_SIZE_MASK (SZ_8K - 1)
108 112
109#define DMA_CMD_ENABLE BIT(31) 113#define DMA_CMD_ENABLE BIT(31)
110#define INT_STATUS_DMA_TOUT BIT(17) 114#define INT_STATUS_DMA_TOUT BIT(17)
@@ -119,4 +123,9 @@
119#define TW686X_STD_PAL_CN 5 123#define TW686X_STD_PAL_CN 5
120#define TW686X_STD_PAL_60 6 124#define TW686X_STD_PAL_60 6
121 125
126#define TW686X_FIELD_MODE 0x3
127#define TW686X_FRAME_MODE 0x2
128/* 0x1 is reserved */
129#define TW686X_SG_MODE 0x0
130
122#define TW686X_FIFO_ERROR(x) (x & ~(0xff)) 131#define TW686X_FIFO_ERROR(x) (x & ~(0xff))
diff --git a/drivers/media/pci/tw686x/tw686x-video.c b/drivers/media/pci/tw686x/tw686x-video.c
index 253e10823ba3..cdb16de770fe 100644
--- a/drivers/media/pci/tw686x/tw686x-video.c
+++ b/drivers/media/pci/tw686x/tw686x-video.c
@@ -19,6 +19,8 @@
19#include <linux/slab.h> 19#include <linux/slab.h>
20#include <media/v4l2-common.h> 20#include <media/v4l2-common.h>
21#include <media/v4l2-event.h> 21#include <media/v4l2-event.h>
22#include <media/videobuf2-dma-contig.h>
23#include <media/videobuf2-dma-sg.h>
22#include <media/videobuf2-vmalloc.h> 24#include <media/videobuf2-vmalloc.h>
23#include "tw686x.h" 25#include "tw686x.h"
24#include "tw686x-regs.h" 26#include "tw686x-regs.h"
@@ -26,6 +28,11 @@
26#define TW686X_INPUTS_PER_CH 4 28#define TW686X_INPUTS_PER_CH 4
27#define TW686X_VIDEO_WIDTH 720 29#define TW686X_VIDEO_WIDTH 720
28#define TW686X_VIDEO_HEIGHT(id) ((id & V4L2_STD_525_60) ? 480 : 576) 30#define TW686X_VIDEO_HEIGHT(id) ((id & V4L2_STD_525_60) ? 480 : 576)
31#define TW686X_MAX_FPS(id) ((id & V4L2_STD_525_60) ? 30 : 25)
32
33#define TW686X_MAX_SG_ENTRY_SIZE 4096
34#define TW686X_MAX_SG_DESC_COUNT 256 /* PAL 720x576 needs 203 4-KB pages */
35#define TW686X_SG_TABLE_SIZE (TW686X_MAX_SG_DESC_COUNT * sizeof(struct tw686x_sg_desc))
29 36
30static const struct tw686x_format formats[] = { 37static const struct tw686x_format formats[] = {
31 { 38 {
@@ -43,53 +50,367 @@ static const struct tw686x_format formats[] = {
43 } 50 }
44}; 51};
45 52
46static unsigned int tw686x_fields_map(v4l2_std_id std, unsigned int fps) 53static void tw686x_buf_done(struct tw686x_video_channel *vc,
54 unsigned int pb)
47{ 55{
48 static const unsigned int map[15] = { 56 struct tw686x_dma_desc *desc = &vc->dma_descs[pb];
49 0x00000000, 0x00000001, 0x00004001, 0x00104001, 0x00404041, 57 struct tw686x_dev *dev = vc->dev;
50 0x01041041, 0x01104411, 0x01111111, 0x04444445, 0x04511445, 58 struct vb2_v4l2_buffer *vb;
51 0x05145145, 0x05151515, 0x05515455, 0x05551555, 0x05555555 59 struct vb2_buffer *vb2_buf;
52 };
53 60
54 static const unsigned int std_625_50[26] = { 61 if (vc->curr_bufs[pb]) {
55 0, 1, 1, 2, 3, 3, 4, 4, 5, 5, 6, 7, 7, 62 vb = &vc->curr_bufs[pb]->vb;
56 8, 8, 9, 10, 10, 11, 11, 12, 13, 13, 14, 14, 0
57 };
58 63
59 static const unsigned int std_525_60[31] = { 64 vb->field = dev->dma_ops->field;
60 0, 1, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 65 vb->sequence = vc->sequence++;
61 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13, 14, 0, 0 66 vb2_buf = &vb->vb2_buf;
62 };
63 67
64 unsigned int i; 68 if (dev->dma_mode == TW686X_DMA_MODE_MEMCPY)
69 memcpy(vb2_plane_vaddr(vb2_buf, 0), desc->virt,
70 desc->size);
71 vb2_buf->timestamp = ktime_get_ns();
72 vb2_buffer_done(vb2_buf, VB2_BUF_STATE_DONE);
73 }
74
75 vc->pb = !pb;
76}
77
78/*
79 * We can call this even when alloc_dma failed for the given channel
80 */
81static void tw686x_memcpy_dma_free(struct tw686x_video_channel *vc,
82 unsigned int pb)
83{
84 struct tw686x_dma_desc *desc = &vc->dma_descs[pb];
85 struct tw686x_dev *dev = vc->dev;
86 struct pci_dev *pci_dev;
87 unsigned long flags;
88
89 /* Check device presence. Shouldn't really happen! */
90 spin_lock_irqsave(&dev->lock, flags);
91 pci_dev = dev->pci_dev;
92 spin_unlock_irqrestore(&dev->lock, flags);
93 if (!pci_dev) {
94 WARN(1, "trying to deallocate on missing device\n");
95 return;
96 }
97
98 if (desc->virt) {
99 pci_free_consistent(dev->pci_dev, desc->size,
100 desc->virt, desc->phys);
101 desc->virt = NULL;
102 }
103}
104
105static int tw686x_memcpy_dma_alloc(struct tw686x_video_channel *vc,
106 unsigned int pb)
107{
108 struct tw686x_dev *dev = vc->dev;
109 u32 reg = pb ? VDMA_B_ADDR[vc->ch] : VDMA_P_ADDR[vc->ch];
110 unsigned int len;
111 void *virt;
112
113 WARN(vc->dma_descs[pb].virt,
114 "Allocating buffer but previous still here\n");
115
116 len = (vc->width * vc->height * vc->format->depth) >> 3;
117 virt = pci_alloc_consistent(dev->pci_dev, len,
118 &vc->dma_descs[pb].phys);
119 if (!virt) {
120 v4l2_err(&dev->v4l2_dev,
121 "dma%d: unable to allocate %s-buffer\n",
122 vc->ch, pb ? "B" : "P");
123 return -ENOMEM;
124 }
125 vc->dma_descs[pb].size = len;
126 vc->dma_descs[pb].virt = virt;
127 reg_write(dev, reg, vc->dma_descs[pb].phys);
128
129 return 0;
130}
131
132static void tw686x_memcpy_buf_refill(struct tw686x_video_channel *vc,
133 unsigned int pb)
134{
135 struct tw686x_v4l2_buf *buf;
136
137 while (!list_empty(&vc->vidq_queued)) {
138
139 buf = list_first_entry(&vc->vidq_queued,
140 struct tw686x_v4l2_buf, list);
141 list_del(&buf->list);
142
143 vc->curr_bufs[pb] = buf;
144 return;
145 }
146 vc->curr_bufs[pb] = NULL;
147}
65 148
66 if (std & V4L2_STD_525_60) { 149static const struct tw686x_dma_ops memcpy_dma_ops = {
67 if (fps >= ARRAY_SIZE(std_525_60)) 150 .alloc = tw686x_memcpy_dma_alloc,
68 fps = 30; 151 .free = tw686x_memcpy_dma_free,
69 i = std_525_60[fps]; 152 .buf_refill = tw686x_memcpy_buf_refill,
153 .mem_ops = &vb2_vmalloc_memops,
154 .hw_dma_mode = TW686X_FRAME_MODE,
155 .field = V4L2_FIELD_INTERLACED,
156};
157
158static void tw686x_contig_buf_refill(struct tw686x_video_channel *vc,
159 unsigned int pb)
160{
161 struct tw686x_v4l2_buf *buf;
162
163 while (!list_empty(&vc->vidq_queued)) {
164 u32 reg = pb ? VDMA_B_ADDR[vc->ch] : VDMA_P_ADDR[vc->ch];
165 dma_addr_t phys;
166
167 buf = list_first_entry(&vc->vidq_queued,
168 struct tw686x_v4l2_buf, list);
169 list_del(&buf->list);
170
171 phys = vb2_dma_contig_plane_dma_addr(&buf->vb.vb2_buf, 0);
172 reg_write(vc->dev, reg, phys);
173
174 buf->vb.vb2_buf.state = VB2_BUF_STATE_ACTIVE;
175 vc->curr_bufs[pb] = buf;
176 return;
177 }
178 vc->curr_bufs[pb] = NULL;
179}
180
181static const struct tw686x_dma_ops contig_dma_ops = {
182 .buf_refill = tw686x_contig_buf_refill,
183 .mem_ops = &vb2_dma_contig_memops,
184 .hw_dma_mode = TW686X_FRAME_MODE,
185 .field = V4L2_FIELD_INTERLACED,
186};
187
188static int tw686x_sg_desc_fill(struct tw686x_sg_desc *descs,
189 struct tw686x_v4l2_buf *buf,
190 unsigned int buf_len)
191{
192 struct sg_table *vbuf = vb2_dma_sg_plane_desc(&buf->vb.vb2_buf, 0);
193 unsigned int len, entry_len;
194 struct scatterlist *sg;
195 int i, count;
196
197 /* Clear the scatter-gather table */
198 memset(descs, 0, TW686X_SG_TABLE_SIZE);
199
200 count = 0;
201 for_each_sg(vbuf->sgl, sg, vbuf->nents, i) {
202 dma_addr_t phys = sg_dma_address(sg);
203 len = sg_dma_len(sg);
204
205 while (len && buf_len) {
206
207 if (count == TW686X_MAX_SG_DESC_COUNT)
208 return -ENOMEM;
209
210 entry_len = min_t(unsigned int, len,
211 TW686X_MAX_SG_ENTRY_SIZE);
212 entry_len = min_t(unsigned int, entry_len, buf_len);
213 descs[count].phys = cpu_to_le32(phys);
214 descs[count++].flags_length =
215 cpu_to_le32(BIT(30) | entry_len);
216 phys += entry_len;
217 len -= entry_len;
218 buf_len -= entry_len;
219 }
220
221 if (!buf_len)
222 return 0;
223 }
224
225 return -ENOMEM;
226}
227
228static void tw686x_sg_buf_refill(struct tw686x_video_channel *vc,
229 unsigned int pb)
230{
231 struct tw686x_dev *dev = vc->dev;
232 struct tw686x_v4l2_buf *buf;
233
234 while (!list_empty(&vc->vidq_queued)) {
235 unsigned int buf_len;
236
237 buf = list_first_entry(&vc->vidq_queued,
238 struct tw686x_v4l2_buf, list);
239 list_del(&buf->list);
240
241 buf_len = (vc->width * vc->height * vc->format->depth) >> 3;
242 if (tw686x_sg_desc_fill(vc->sg_descs[pb], buf, buf_len)) {
243 v4l2_err(&dev->v4l2_dev,
244 "dma%d: unable to fill %s-buffer\n",
245 vc->ch, pb ? "B" : "P");
246 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
247 continue;
248 }
249
250 buf->vb.vb2_buf.state = VB2_BUF_STATE_ACTIVE;
251 vc->curr_bufs[pb] = buf;
252 return;
253 }
254
255 vc->curr_bufs[pb] = NULL;
256}
257
258static void tw686x_sg_dma_free(struct tw686x_video_channel *vc,
259 unsigned int pb)
260{
261 struct tw686x_dma_desc *desc = &vc->dma_descs[pb];
262 struct tw686x_dev *dev = vc->dev;
263
264 if (desc->size) {
265 pci_free_consistent(dev->pci_dev, desc->size,
266 desc->virt, desc->phys);
267 desc->virt = NULL;
268 }
269
270 vc->sg_descs[pb] = NULL;
271}
272
273static int tw686x_sg_dma_alloc(struct tw686x_video_channel *vc,
274 unsigned int pb)
275{
276 struct tw686x_dma_desc *desc = &vc->dma_descs[pb];
277 struct tw686x_dev *dev = vc->dev;
278 u32 reg = pb ? DMA_PAGE_TABLE1_ADDR[vc->ch] :
279 DMA_PAGE_TABLE0_ADDR[vc->ch];
280 void *virt;
281
282 if (desc->size) {
283
284 virt = pci_alloc_consistent(dev->pci_dev, desc->size,
285 &desc->phys);
286 if (!virt) {
287 v4l2_err(&dev->v4l2_dev,
288 "dma%d: unable to allocate %s-buffer\n",
289 vc->ch, pb ? "B" : "P");
290 return -ENOMEM;
291 }
292 desc->virt = virt;
293 reg_write(dev, reg, desc->phys);
70 } else { 294 } else {
71 if (fps >= ARRAY_SIZE(std_625_50)) 295 virt = dev->video_channels[0].dma_descs[pb].virt +
72 fps = 25; 296 vc->ch * TW686X_SG_TABLE_SIZE;
73 i = std_625_50[fps];
74 } 297 }
75 298
76 return map[i]; 299 vc->sg_descs[pb] = virt;
300 return 0;
301}
302
303static int tw686x_sg_setup(struct tw686x_dev *dev)
304{
305 unsigned int sg_table_size, pb, ch, channels;
306
307 if (is_second_gen(dev)) {
308 /*
309 * TW6865/TW6869: each channel needs a pair of
310 * P-B descriptor tables.
311 */
312 channels = max_channels(dev);
313 sg_table_size = TW686X_SG_TABLE_SIZE;
314 } else {
315 /*
316 * TW6864/TW6868: we need to allocate a pair of
317 * P-B descriptor tables, common for all channels.
318 * Each table will be bigger than 4 KB.
319 */
320 channels = 1;
321 sg_table_size = max_channels(dev) * TW686X_SG_TABLE_SIZE;
322 }
323
324 for (ch = 0; ch < channels; ch++) {
325 struct tw686x_video_channel *vc = &dev->video_channels[ch];
326
327 for (pb = 0; pb < 2; pb++)
328 vc->dma_descs[pb].size = sg_table_size;
329 }
330
331 return 0;
332}
333
334static const struct tw686x_dma_ops sg_dma_ops = {
335 .setup = tw686x_sg_setup,
336 .alloc = tw686x_sg_dma_alloc,
337 .free = tw686x_sg_dma_free,
338 .buf_refill = tw686x_sg_buf_refill,
339 .mem_ops = &vb2_dma_sg_memops,
340 .hw_dma_mode = TW686X_SG_MODE,
341 .field = V4L2_FIELD_SEQ_TB,
342};
343
344static const unsigned int fps_map[15] = {
345 /*
346 * bit 31 enables selecting the field control register
347 * bits 0-29 are a bitmask with fields that will be output.
348 * For NTSC (and PAL-M, PAL-60), all 30 bits are used.
349 * For other PAL standards, only the first 25 bits are used.
350 */
351 0x00000000, /* output all fields */
352 0x80000006, /* 2 fps (60Hz), 2 fps (50Hz) */
353 0x80018006, /* 4 fps (60Hz), 4 fps (50Hz) */
354 0x80618006, /* 6 fps (60Hz), 6 fps (50Hz) */
355 0x81818186, /* 8 fps (60Hz), 8 fps (50Hz) */
356 0x86186186, /* 10 fps (60Hz), 8 fps (50Hz) */
357 0x86619866, /* 12 fps (60Hz), 10 fps (50Hz) */
358 0x86666666, /* 14 fps (60Hz), 12 fps (50Hz) */
359 0x9999999e, /* 16 fps (60Hz), 14 fps (50Hz) */
360 0x99e6799e, /* 18 fps (60Hz), 16 fps (50Hz) */
361 0x9e79e79e, /* 20 fps (60Hz), 16 fps (50Hz) */
362 0x9e7e7e7e, /* 22 fps (60Hz), 18 fps (50Hz) */
363 0x9fe7f9fe, /* 24 fps (60Hz), 20 fps (50Hz) */
364 0x9ffe7ffe, /* 26 fps (60Hz), 22 fps (50Hz) */
365 0x9ffffffe, /* 28 fps (60Hz), 24 fps (50Hz) */
366};
367
368static unsigned int tw686x_real_fps(unsigned int index, unsigned int max_fps)
369{
370 unsigned long mask;
371
372 if (!index || index >= ARRAY_SIZE(fps_map))
373 return max_fps;
374
375 mask = GENMASK(max_fps - 1, 0);
376 return hweight_long(fps_map[index] & mask);
377}
378
379static unsigned int tw686x_fps_idx(unsigned int fps, unsigned int max_fps)
380{
381 unsigned int idx, real_fps;
382 int delta;
383
384 /* First guess */
385 idx = (12 + 15 * fps) / max_fps;
386
387 /* Minimal possible framerate is 2 frames per second */
388 if (!idx)
389 return 1;
390
391 /* Check if the difference is bigger than abs(1) and adjust */
392 real_fps = tw686x_real_fps(idx, max_fps);
393 delta = real_fps - fps;
394 if (delta < -1)
395 idx++;
396 else if (delta > 1)
397 idx--;
398
399 /* Max framerate */
400 if (idx >= 15)
401 return 0;
402
403 return idx;
77} 404}
78 405
79static void tw686x_set_framerate(struct tw686x_video_channel *vc, 406static void tw686x_set_framerate(struct tw686x_video_channel *vc,
80 unsigned int fps) 407 unsigned int fps)
81{ 408{
82 unsigned int map; 409 unsigned int i;
83
84 if (vc->fps == fps)
85 return;
86 410
87 map = tw686x_fields_map(vc->video_standard, fps) << 1; 411 i = tw686x_fps_idx(fps, TW686X_MAX_FPS(vc->video_standard));
88 map |= map << 1; 412 reg_write(vc->dev, VIDEO_FIELD_CTRL[vc->ch], fps_map[i]);
89 if (map > 0) 413 vc->fps = tw686x_real_fps(i, TW686X_MAX_FPS(vc->video_standard));
90 map |= BIT(31);
91 reg_write(vc->dev, VIDEO_FIELD_CTRL[vc->ch], map);
92 vc->fps = fps;
93} 414}
94 415
95static const struct tw686x_format *format_by_fourcc(unsigned int fourcc) 416static const struct tw686x_format *format_by_fourcc(unsigned int fourcc)
@@ -104,7 +425,7 @@ static const struct tw686x_format *format_by_fourcc(unsigned int fourcc)
104 425
105static int tw686x_queue_setup(struct vb2_queue *vq, 426static int tw686x_queue_setup(struct vb2_queue *vq,
106 unsigned int *nbuffers, unsigned int *nplanes, 427 unsigned int *nbuffers, unsigned int *nplanes,
107 unsigned int sizes[], void *alloc_ctxs[]) 428 unsigned int sizes[], struct device *alloc_devs[])
108{ 429{
109 struct tw686x_video_channel *vc = vb2_get_drv_priv(vq); 430 struct tw686x_video_channel *vc = vb2_get_drv_priv(vq);
110 unsigned int szimage = 431 unsigned int szimage =
@@ -152,75 +473,6 @@ static void tw686x_buf_queue(struct vb2_buffer *vb)
152 spin_unlock_irqrestore(&vc->qlock, flags); 473 spin_unlock_irqrestore(&vc->qlock, flags);
153} 474}
154 475
155/*
156 * We can call this even when alloc_dma failed for the given channel
157 */
158static void tw686x_free_dma(struct tw686x_video_channel *vc, unsigned int pb)
159{
160 struct tw686x_dma_desc *desc = &vc->dma_descs[pb];
161 struct tw686x_dev *dev = vc->dev;
162 struct pci_dev *pci_dev;
163 unsigned long flags;
164
165 /* Check device presence. Shouldn't really happen! */
166 spin_lock_irqsave(&dev->lock, flags);
167 pci_dev = dev->pci_dev;
168 spin_unlock_irqrestore(&dev->lock, flags);
169 if (!pci_dev) {
170 WARN(1, "trying to deallocate on missing device\n");
171 return;
172 }
173
174 if (desc->virt) {
175 pci_free_consistent(dev->pci_dev, desc->size,
176 desc->virt, desc->phys);
177 desc->virt = NULL;
178 }
179}
180
181static int tw686x_alloc_dma(struct tw686x_video_channel *vc, unsigned int pb)
182{
183 struct tw686x_dev *dev = vc->dev;
184 u32 reg = pb ? VDMA_B_ADDR[vc->ch] : VDMA_P_ADDR[vc->ch];
185 unsigned int len;
186 void *virt;
187
188 WARN(vc->dma_descs[pb].virt,
189 "Allocating buffer but previous still here\n");
190
191 len = (vc->width * vc->height * vc->format->depth) >> 3;
192 virt = pci_alloc_consistent(dev->pci_dev, len,
193 &vc->dma_descs[pb].phys);
194 if (!virt) {
195 v4l2_err(&dev->v4l2_dev,
196 "dma%d: unable to allocate %s-buffer\n",
197 vc->ch, pb ? "B" : "P");
198 return -ENOMEM;
199 }
200 vc->dma_descs[pb].size = len;
201 vc->dma_descs[pb].virt = virt;
202 reg_write(dev, reg, vc->dma_descs[pb].phys);
203
204 return 0;
205}
206
207static void tw686x_buffer_refill(struct tw686x_video_channel *vc,
208 unsigned int pb)
209{
210 struct tw686x_v4l2_buf *buf;
211
212 while (!list_empty(&vc->vidq_queued)) {
213
214 buf = list_first_entry(&vc->vidq_queued,
215 struct tw686x_v4l2_buf, list);
216 list_del(&buf->list);
217
218 vc->curr_bufs[pb] = buf;
219 return;
220 }
221 vc->curr_bufs[pb] = NULL;
222}
223
224static void tw686x_clear_queue(struct tw686x_video_channel *vc, 476static void tw686x_clear_queue(struct tw686x_video_channel *vc,
225 enum vb2_buffer_state state) 477 enum vb2_buffer_state state)
226{ 478{
@@ -262,7 +514,8 @@ static int tw686x_start_streaming(struct vb2_queue *vq, unsigned int count)
262 spin_lock_irqsave(&vc->qlock, flags); 514 spin_lock_irqsave(&vc->qlock, flags);
263 515
264 /* Sanity check */ 516 /* Sanity check */
265 if (!vc->dma_descs[0].virt || !vc->dma_descs[1].virt) { 517 if (dev->dma_mode == TW686X_DMA_MODE_MEMCPY &&
518 (!vc->dma_descs[0].virt || !vc->dma_descs[1].virt)) {
266 spin_unlock_irqrestore(&vc->qlock, flags); 519 spin_unlock_irqrestore(&vc->qlock, flags);
267 v4l2_err(&dev->v4l2_dev, 520 v4l2_err(&dev->v4l2_dev,
268 "video%d: refusing to start without DMA buffers\n", 521 "video%d: refusing to start without DMA buffers\n",
@@ -272,7 +525,7 @@ static int tw686x_start_streaming(struct vb2_queue *vq, unsigned int count)
272 } 525 }
273 526
274 for (pb = 0; pb < 2; pb++) 527 for (pb = 0; pb < 2; pb++)
275 tw686x_buffer_refill(vc, pb); 528 dev->dma_ops->buf_refill(vc, pb);
276 spin_unlock_irqrestore(&vc->qlock, flags); 529 spin_unlock_irqrestore(&vc->qlock, flags);
277 530
278 vc->sequence = 0; 531 vc->sequence = 0;
@@ -375,10 +628,11 @@ static int tw686x_g_fmt_vid_cap(struct file *file, void *priv,
375 struct v4l2_format *f) 628 struct v4l2_format *f)
376{ 629{
377 struct tw686x_video_channel *vc = video_drvdata(file); 630 struct tw686x_video_channel *vc = video_drvdata(file);
631 struct tw686x_dev *dev = vc->dev;
378 632
379 f->fmt.pix.width = vc->width; 633 f->fmt.pix.width = vc->width;
380 f->fmt.pix.height = vc->height; 634 f->fmt.pix.height = vc->height;
381 f->fmt.pix.field = V4L2_FIELD_INTERLACED; 635 f->fmt.pix.field = dev->dma_ops->field;
382 f->fmt.pix.pixelformat = vc->format->fourcc; 636 f->fmt.pix.pixelformat = vc->format->fourcc;
383 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M; 637 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
384 f->fmt.pix.bytesperline = (f->fmt.pix.width * vc->format->depth) / 8; 638 f->fmt.pix.bytesperline = (f->fmt.pix.width * vc->format->depth) / 8;
@@ -390,6 +644,7 @@ static int tw686x_try_fmt_vid_cap(struct file *file, void *priv,
390 struct v4l2_format *f) 644 struct v4l2_format *f)
391{ 645{
392 struct tw686x_video_channel *vc = video_drvdata(file); 646 struct tw686x_video_channel *vc = video_drvdata(file);
647 struct tw686x_dev *dev = vc->dev;
393 unsigned int video_height = TW686X_VIDEO_HEIGHT(vc->video_standard); 648 unsigned int video_height = TW686X_VIDEO_HEIGHT(vc->video_standard);
394 const struct tw686x_format *format; 649 const struct tw686x_format *format;
395 650
@@ -412,7 +667,7 @@ static int tw686x_try_fmt_vid_cap(struct file *file, void *priv,
412 f->fmt.pix.bytesperline = (f->fmt.pix.width * format->depth) / 8; 667 f->fmt.pix.bytesperline = (f->fmt.pix.width * format->depth) / 8;
413 f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline; 668 f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
414 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M; 669 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
415 f->fmt.pix.field = V4L2_FIELD_INTERLACED; 670 f->fmt.pix.field = dev->dma_ops->field;
416 671
417 return 0; 672 return 0;
418} 673}
@@ -421,6 +676,7 @@ static int tw686x_s_fmt_vid_cap(struct file *file, void *priv,
421 struct v4l2_format *f) 676 struct v4l2_format *f)
422{ 677{
423 struct tw686x_video_channel *vc = video_drvdata(file); 678 struct tw686x_video_channel *vc = video_drvdata(file);
679 struct tw686x_dev *dev = vc->dev;
424 u32 val, width, line_width, height; 680 u32 val, width, line_width, height;
425 unsigned long bitsperframe; 681 unsigned long bitsperframe;
426 int err, pb; 682 int err, pb;
@@ -438,15 +694,16 @@ static int tw686x_s_fmt_vid_cap(struct file *file, void *priv,
438 vc->height = f->fmt.pix.height; 694 vc->height = f->fmt.pix.height;
439 695
440 /* We need new DMA buffers if the framesize has changed */ 696 /* We need new DMA buffers if the framesize has changed */
441 if (bitsperframe != vc->width * vc->height * vc->format->depth) { 697 if (dev->dma_ops->alloc &&
698 bitsperframe != vc->width * vc->height * vc->format->depth) {
442 for (pb = 0; pb < 2; pb++) 699 for (pb = 0; pb < 2; pb++)
443 tw686x_free_dma(vc, pb); 700 dev->dma_ops->free(vc, pb);
444 701
445 for (pb = 0; pb < 2; pb++) { 702 for (pb = 0; pb < 2; pb++) {
446 err = tw686x_alloc_dma(vc, pb); 703 err = dev->dma_ops->alloc(vc, pb);
447 if (err) { 704 if (err) {
448 if (pb > 0) 705 if (pb > 0)
449 tw686x_free_dma(vc, 0); 706 dev->dma_ops->free(vc, 0);
450 return err; 707 return err;
451 } 708 }
452 } 709 }
@@ -464,6 +721,19 @@ static int tw686x_s_fmt_vid_cap(struct file *file, void *priv,
464 else 721 else
465 val &= ~BIT(24); 722 val &= ~BIT(24);
466 723
724 val &= ~0x7ffff;
725
726 /* Program the DMA scatter-gather */
727 if (dev->dma_mode == TW686X_DMA_MODE_SG) {
728 u32 start_idx, end_idx;
729
730 start_idx = is_second_gen(dev) ?
731 0 : vc->ch * TW686X_MAX_SG_DESC_COUNT;
732 end_idx = start_idx + TW686X_MAX_SG_DESC_COUNT - 1;
733
734 val |= (end_idx << 10) | start_idx;
735 }
736
467 val &= ~(0x7 << 20); 737 val &= ~(0x7 << 20);
468 val |= vc->format->mode << 20; 738 val |= vc->format->mode << 20;
469 reg_write(vc->dev, VDMA_CHANNEL_CONFIG[vc->ch], val); 739 reg_write(vc->dev, VDMA_CHANNEL_CONFIG[vc->ch], val);
@@ -540,6 +810,12 @@ static int tw686x_s_std(struct file *file, void *priv, v4l2_std_id id)
540 ret = tw686x_g_fmt_vid_cap(file, priv, &f); 810 ret = tw686x_g_fmt_vid_cap(file, priv, &f);
541 if (!ret) 811 if (!ret)
542 tw686x_s_fmt_vid_cap(file, priv, &f); 812 tw686x_s_fmt_vid_cap(file, priv, &f);
813
814 /*
815 * Frame decimation depends on the chosen standard,
816 * so reset it to the current value.
817 */
818 tw686x_set_framerate(vc, vc->fps);
543 return 0; 819 return 0;
544} 820}
545 821
@@ -609,6 +885,40 @@ static int tw686x_g_std(struct file *file, void *priv, v4l2_std_id *id)
609 return 0; 885 return 0;
610} 886}
611 887
888static int tw686x_g_parm(struct file *file, void *priv,
889 struct v4l2_streamparm *sp)
890{
891 struct tw686x_video_channel *vc = video_drvdata(file);
892 struct v4l2_captureparm *cp = &sp->parm.capture;
893
894 if (sp->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
895 return -EINVAL;
896 sp->parm.capture.readbuffers = 3;
897
898 cp->capability = V4L2_CAP_TIMEPERFRAME;
899 cp->timeperframe.numerator = 1;
900 cp->timeperframe.denominator = vc->fps;
901 return 0;
902}
903
904static int tw686x_s_parm(struct file *file, void *priv,
905 struct v4l2_streamparm *sp)
906{
907 struct tw686x_video_channel *vc = video_drvdata(file);
908 struct v4l2_captureparm *cp = &sp->parm.capture;
909 unsigned int denominator = cp->timeperframe.denominator;
910 unsigned int numerator = cp->timeperframe.numerator;
911 unsigned int fps;
912
913 if (vb2_is_busy(&vc->vidq))
914 return -EBUSY;
915
916 fps = (!numerator || !denominator) ? 0 : denominator / numerator;
917 if (vc->fps != fps)
918 tw686x_set_framerate(vc, fps);
919 return tw686x_g_parm(file, priv, sp);
920}
921
612static int tw686x_enum_fmt_vid_cap(struct file *file, void *priv, 922static int tw686x_enum_fmt_vid_cap(struct file *file, void *priv,
613 struct v4l2_fmtdesc *f) 923 struct v4l2_fmtdesc *f)
614{ 924{
@@ -695,6 +1005,9 @@ static const struct v4l2_ioctl_ops tw686x_video_ioctl_ops = {
695 .vidioc_g_std = tw686x_g_std, 1005 .vidioc_g_std = tw686x_g_std,
696 .vidioc_s_std = tw686x_s_std, 1006 .vidioc_s_std = tw686x_s_std,
697 1007
1008 .vidioc_g_parm = tw686x_g_parm,
1009 .vidioc_s_parm = tw686x_s_parm,
1010
698 .vidioc_enum_input = tw686x_enum_input, 1011 .vidioc_enum_input = tw686x_enum_input,
699 .vidioc_g_input = tw686x_g_input, 1012 .vidioc_g_input = tw686x_g_input,
700 .vidioc_s_input = tw686x_s_input, 1013 .vidioc_s_input = tw686x_s_input,
@@ -713,26 +1026,11 @@ static const struct v4l2_ioctl_ops tw686x_video_ioctl_ops = {
713 .vidioc_unsubscribe_event = v4l2_event_unsubscribe, 1026 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
714}; 1027};
715 1028
716static void tw686x_buffer_copy(struct tw686x_video_channel *vc,
717 unsigned int pb, struct vb2_v4l2_buffer *vb)
718{
719 struct tw686x_dma_desc *desc = &vc->dma_descs[pb];
720 struct vb2_buffer *vb2_buf = &vb->vb2_buf;
721
722 vb->field = V4L2_FIELD_INTERLACED;
723 vb->sequence = vc->sequence++;
724
725 memcpy(vb2_plane_vaddr(vb2_buf, 0), desc->virt, desc->size);
726 vb2_buf->timestamp = ktime_get_ns();
727 vb2_buffer_done(vb2_buf, VB2_BUF_STATE_DONE);
728}
729
730void tw686x_video_irq(struct tw686x_dev *dev, unsigned long requests, 1029void tw686x_video_irq(struct tw686x_dev *dev, unsigned long requests,
731 unsigned int pb_status, unsigned int fifo_status, 1030 unsigned int pb_status, unsigned int fifo_status,
732 unsigned int *reset_ch) 1031 unsigned int *reset_ch)
733{ 1032{
734 struct tw686x_video_channel *vc; 1033 struct tw686x_video_channel *vc;
735 struct vb2_v4l2_buffer *vb;
736 unsigned long flags; 1034 unsigned long flags;
737 unsigned int ch, pb; 1035 unsigned int ch, pb;
738 1036
@@ -781,14 +1079,9 @@ void tw686x_video_irq(struct tw686x_dev *dev, unsigned long requests,
781 continue; 1079 continue;
782 } 1080 }
783 1081
784 /* handle video stream */
785 spin_lock_irqsave(&vc->qlock, flags); 1082 spin_lock_irqsave(&vc->qlock, flags);
786 if (vc->curr_bufs[pb]) { 1083 tw686x_buf_done(vc, pb);
787 vb = &vc->curr_bufs[pb]->vb; 1084 dev->dma_ops->buf_refill(vc, pb);
788 tw686x_buffer_copy(vc, pb, vb);
789 }
790 vc->pb = !pb;
791 tw686x_buffer_refill(vc, pb);
792 spin_unlock_irqrestore(&vc->qlock, flags); 1085 spin_unlock_irqrestore(&vc->qlock, flags);
793 } 1086 }
794} 1087}
@@ -803,8 +1096,9 @@ void tw686x_video_free(struct tw686x_dev *dev)
803 if (vc->device) 1096 if (vc->device)
804 video_unregister_device(vc->device); 1097 video_unregister_device(vc->device);
805 1098
806 for (pb = 0; pb < 2; pb++) 1099 if (dev->dma_ops->free)
807 tw686x_free_dma(vc, pb); 1100 for (pb = 0; pb < 2; pb++)
1101 dev->dma_ops->free(vc, pb);
808 } 1102 }
809} 1103}
810 1104
@@ -813,10 +1107,25 @@ int tw686x_video_init(struct tw686x_dev *dev)
813 unsigned int ch, val, pb; 1107 unsigned int ch, val, pb;
814 int err; 1108 int err;
815 1109
1110 if (dev->dma_mode == TW686X_DMA_MODE_MEMCPY)
1111 dev->dma_ops = &memcpy_dma_ops;
1112 else if (dev->dma_mode == TW686X_DMA_MODE_CONTIG)
1113 dev->dma_ops = &contig_dma_ops;
1114 else if (dev->dma_mode == TW686X_DMA_MODE_SG)
1115 dev->dma_ops = &sg_dma_ops;
1116 else
1117 return -EINVAL;
1118
816 err = v4l2_device_register(&dev->pci_dev->dev, &dev->v4l2_dev); 1119 err = v4l2_device_register(&dev->pci_dev->dev, &dev->v4l2_dev);
817 if (err) 1120 if (err)
818 return err; 1121 return err;
819 1122
1123 if (dev->dma_ops->setup) {
1124 err = dev->dma_ops->setup(dev);
1125 if (err)
1126 return err;
1127 }
1128
820 for (ch = 0; ch < max_channels(dev); ch++) { 1129 for (ch = 0; ch < max_channels(dev); ch++) {
821 struct tw686x_video_channel *vc = &dev->video_channels[ch]; 1130 struct tw686x_video_channel *vc = &dev->video_channels[ch];
822 struct video_device *vdev; 1131 struct video_device *vdev;
@@ -842,10 +1151,12 @@ int tw686x_video_init(struct tw686x_dev *dev)
842 reg_write(dev, HACTIVE_LO[ch], 0xd0); 1151 reg_write(dev, HACTIVE_LO[ch], 0xd0);
843 reg_write(dev, VIDEO_SIZE[ch], 0); 1152 reg_write(dev, VIDEO_SIZE[ch], 0);
844 1153
845 for (pb = 0; pb < 2; pb++) { 1154 if (dev->dma_ops->alloc) {
846 err = tw686x_alloc_dma(vc, pb); 1155 for (pb = 0; pb < 2; pb++) {
847 if (err) 1156 err = dev->dma_ops->alloc(vc, pb);
848 goto error; 1157 if (err)
1158 goto error;
1159 }
849 } 1160 }
850 1161
851 vc->vidq.io_modes = VB2_READ | VB2_MMAP | VB2_DMABUF; 1162 vc->vidq.io_modes = VB2_READ | VB2_MMAP | VB2_DMABUF;
@@ -853,11 +1164,12 @@ int tw686x_video_init(struct tw686x_dev *dev)
853 vc->vidq.drv_priv = vc; 1164 vc->vidq.drv_priv = vc;
854 vc->vidq.buf_struct_size = sizeof(struct tw686x_v4l2_buf); 1165 vc->vidq.buf_struct_size = sizeof(struct tw686x_v4l2_buf);
855 vc->vidq.ops = &tw686x_video_qops; 1166 vc->vidq.ops = &tw686x_video_qops;
856 vc->vidq.mem_ops = &vb2_vmalloc_memops; 1167 vc->vidq.mem_ops = dev->dma_ops->mem_ops;
857 vc->vidq.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC; 1168 vc->vidq.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
858 vc->vidq.min_buffers_needed = 2; 1169 vc->vidq.min_buffers_needed = 2;
859 vc->vidq.lock = &vc->vb_mutex; 1170 vc->vidq.lock = &vc->vb_mutex;
860 vc->vidq.gfp_flags = GFP_DMA32; 1171 vc->vidq.gfp_flags = GFP_DMA32;
1172 vc->vidq.dev = &dev->pci_dev->dev;
861 1173
862 err = vb2_queue_init(&vc->vidq); 1174 err = vb2_queue_init(&vc->vidq);
863 if (err) { 1175 if (err) {
@@ -915,10 +1227,9 @@ int tw686x_video_init(struct tw686x_dev *dev)
915 vc->num = vdev->num; 1227 vc->num = vdev->num;
916 } 1228 }
917 1229
918 /* Set DMA frame mode on all channels. Only supported mode for now. */
919 val = TW686X_DEF_PHASE_REF; 1230 val = TW686X_DEF_PHASE_REF;
920 for (ch = 0; ch < max_channels(dev); ch++) 1231 for (ch = 0; ch < max_channels(dev); ch++)
921 val |= TW686X_FRAME_MODE << (16 + ch * 2); 1232 val |= dev->dma_ops->hw_dma_mode << (16 + ch * 2);
922 reg_write(dev, PHASE_REF, val); 1233 reg_write(dev, PHASE_REF, val);
923 1234
924 reg_write(dev, MISC2[0], 0xe7); 1235 reg_write(dev, MISC2[0], 0xe7);
diff --git a/drivers/media/pci/tw686x/tw686x.h b/drivers/media/pci/tw686x/tw686x.h
index 44b5755acf02..f24a2a9bcdb2 100644
--- a/drivers/media/pci/tw686x/tw686x.h
+++ b/drivers/media/pci/tw686x/tw686x.h
@@ -27,16 +27,14 @@
27#define TYPE_SECOND_GEN 0x10 27#define TYPE_SECOND_GEN 0x10
28#define TW686X_DEF_PHASE_REF 0x1518 28#define TW686X_DEF_PHASE_REF 0x1518
29 29
30#define TW686X_FIELD_MODE 0x3
31#define TW686X_FRAME_MODE 0x2
32/* 0x1 is reserved */
33#define TW686X_SG_MODE 0x0
34
35#define TW686X_AUDIO_PAGE_SZ 4096
36#define TW686X_AUDIO_PAGE_MAX 16 30#define TW686X_AUDIO_PAGE_MAX 16
37#define TW686X_AUDIO_PERIODS_MIN 2 31#define TW686X_AUDIO_PERIODS_MIN 2
38#define TW686X_AUDIO_PERIODS_MAX TW686X_AUDIO_PAGE_MAX 32#define TW686X_AUDIO_PERIODS_MAX TW686X_AUDIO_PAGE_MAX
39 33
34#define TW686X_DMA_MODE_MEMCPY 0
35#define TW686X_DMA_MODE_CONTIG 1
36#define TW686X_DMA_MODE_SG 2
37
40struct tw686x_format { 38struct tw686x_format {
41 char *name; 39 char *name;
42 unsigned int fourcc; 40 unsigned int fourcc;
@@ -50,6 +48,12 @@ struct tw686x_dma_desc {
50 unsigned int size; 48 unsigned int size;
51}; 49};
52 50
51struct tw686x_sg_desc {
52 /* 3 MSBits for flags, 13 LSBits for length */
53 __le32 flags_length;
54 __le32 phys;
55};
56
53struct tw686x_audio_buf { 57struct tw686x_audio_buf {
54 dma_addr_t dma; 58 dma_addr_t dma;
55 void *virt; 59 void *virt;
@@ -82,6 +86,7 @@ struct tw686x_video_channel {
82 struct video_device *device; 86 struct video_device *device;
83 struct tw686x_v4l2_buf *curr_bufs[2]; 87 struct tw686x_v4l2_buf *curr_bufs[2];
84 struct tw686x_dma_desc dma_descs[2]; 88 struct tw686x_dma_desc dma_descs[2];
89 struct tw686x_sg_desc *sg_descs[2];
85 90
86 struct v4l2_ctrl_handler ctrl_handler; 91 struct v4l2_ctrl_handler ctrl_handler;
87 const struct tw686x_format *format; 92 const struct tw686x_format *format;
@@ -99,6 +104,16 @@ struct tw686x_video_channel {
99 bool no_signal; 104 bool no_signal;
100}; 105};
101 106
107struct tw686x_dma_ops {
108 int (*setup)(struct tw686x_dev *dev);
109 int (*alloc)(struct tw686x_video_channel *vc, unsigned int pb);
110 void (*free)(struct tw686x_video_channel *vc, unsigned int pb);
111 void (*buf_refill)(struct tw686x_video_channel *vc, unsigned int pb);
112 const struct vb2_mem_ops *mem_ops;
113 enum v4l2_field field;
114 u32 hw_dma_mode;
115};
116
102/** 117/**
103 * struct tw686x_dev - global device status 118 * struct tw686x_dev - global device status
104 * @lock: spinlock controlling access to the 119 * @lock: spinlock controlling access to the
@@ -112,15 +127,18 @@ struct tw686x_dev {
112 127
113 char name[32]; 128 char name[32];
114 unsigned int type; 129 unsigned int type;
130 unsigned int dma_mode;
115 struct pci_dev *pci_dev; 131 struct pci_dev *pci_dev;
116 __u32 __iomem *mmio; 132 __u32 __iomem *mmio;
117 133
118 void *alloc_ctx; 134 const struct tw686x_dma_ops *dma_ops;
119
120 struct tw686x_video_channel *video_channels; 135 struct tw686x_video_channel *video_channels;
121 struct tw686x_audio_channel *audio_channels; 136 struct tw686x_audio_channel *audio_channels;
122 137
123 int audio_rate; /* per-device value */ 138 /* Per-device audio parameters */
139 int audio_rate;
140 int period_size;
141 int audio_enabled;
124 142
125 struct timer_list dma_delay_timer; 143 struct timer_list dma_delay_timer;
126 u32 pending_dma_en; /* must be protected by lock */ 144 u32 pending_dma_en; /* must be protected by lock */
@@ -143,6 +161,12 @@ static inline unsigned int max_channels(struct tw686x_dev *dev)
143 return dev->type & TYPE_MAX_CHANNELS; /* 4 or 8 channels */ 161 return dev->type & TYPE_MAX_CHANNELS; /* 4 or 8 channels */
144} 162}
145 163
164static inline unsigned is_second_gen(struct tw686x_dev *dev)
165{
166 /* each channel has its own DMA SG table */
167 return dev->type & TYPE_SECOND_GEN;
168}
169
146void tw686x_enable_channel(struct tw686x_dev *dev, unsigned int channel); 170void tw686x_enable_channel(struct tw686x_dev *dev, unsigned int channel);
147void tw686x_disable_channel(struct tw686x_dev *dev, unsigned int channel); 171void tw686x_disable_channel(struct tw686x_dev *dev, unsigned int channel);
148 172
diff --git a/drivers/media/pci/zoran/zr36016.c b/drivers/media/pci/zoran/zr36016.c
index b87ddba8608f..c12ca9f96bac 100644
--- a/drivers/media/pci/zoran/zr36016.c
+++ b/drivers/media/pci/zoran/zr36016.c
@@ -246,10 +246,6 @@ static int zr36016_pushit (struct zr36016 *ptr,
246 //TODO// 246 //TODO//
247 ========================================================================= */ 247 ========================================================================= */
248 248
249// needed offset values PAL NTSC SECAM
250static const int zr016_xoff[] = { 20, 20, 20 };
251static const int zr016_yoff[] = { 8, 9, 7 };
252
253static void 249static void
254zr36016_init (struct zr36016 *ptr) 250zr36016_init (struct zr36016 *ptr)
255{ 251{
diff --git a/drivers/media/platform/Kconfig b/drivers/media/platform/Kconfig
index 84e041c0a70e..f25344bc7912 100644
--- a/drivers/media/platform/Kconfig
+++ b/drivers/media/platform/Kconfig
@@ -110,6 +110,7 @@ source "drivers/media/platform/exynos4-is/Kconfig"
110source "drivers/media/platform/s5p-tv/Kconfig" 110source "drivers/media/platform/s5p-tv/Kconfig"
111source "drivers/media/platform/am437x/Kconfig" 111source "drivers/media/platform/am437x/Kconfig"
112source "drivers/media/platform/xilinx/Kconfig" 112source "drivers/media/platform/xilinx/Kconfig"
113source "drivers/media/platform/rcar-vin/Kconfig"
113 114
114config VIDEO_TI_CAL 115config VIDEO_TI_CAL
115 tristate "TI CAL (Camera Adaptation Layer) driver" 116 tristate "TI CAL (Camera Adaptation Layer) driver"
@@ -152,6 +153,36 @@ config VIDEO_CODA
152 Coda is a range of video codec IPs that supports 153 Coda is a range of video codec IPs that supports
153 H.264, MPEG-4, and other video formats. 154 H.264, MPEG-4, and other video formats.
154 155
156config VIDEO_MEDIATEK_VPU
157 tristate "Mediatek Video Processor Unit"
158 depends on VIDEO_DEV && VIDEO_V4L2
159 depends on ARCH_MEDIATEK || COMPILE_TEST
160 ---help---
161 This driver provides downloading VPU firmware and
162 communicating with VPU. This driver for hw video
163 codec embedded in Mediatek's MT8173 SOCs. It is able
164 to handle video decoding/encoding in a range of formats.
165
166 To compile this driver as a module, choose M here: the
167 module will be called mtk-vpu.
168
169config VIDEO_MEDIATEK_VCODEC
170 tristate "Mediatek Video Codec driver"
171 depends on MTK_IOMMU || COMPILE_TEST
172 depends on VIDEO_DEV && VIDEO_V4L2
173 depends on ARCH_MEDIATEK || COMPILE_TEST
174 select VIDEOBUF2_DMA_CONTIG
175 select V4L2_MEM2MEM_DEV
176 select VIDEO_MEDIATEK_VPU
177 default n
178 ---help---
179 Mediatek video codec driver provides HW capability to
180 encode and decode in a range of video formats
181 This driver rely on VPU driver to communicate with VPU.
182
183 To compile this driver as a module, choose M here: the
184 module will be called mtk-vcodec
185
155config VIDEO_MEM2MEM_DEINTERLACE 186config VIDEO_MEM2MEM_DEINTERLACE
156 tristate "Deinterlace support" 187 tristate "Deinterlace support"
157 depends on VIDEO_DEV && VIDEO_V4L2 && DMA_ENGINE 188 depends on VIDEO_DEV && VIDEO_V4L2 && DMA_ENGINE
@@ -247,10 +278,24 @@ config VIDEO_RENESAS_JPU
247 To compile this driver as a module, choose M here: the module 278 To compile this driver as a module, choose M here: the module
248 will be called rcar_jpu. 279 will be called rcar_jpu.
249 280
281config VIDEO_RENESAS_FCP
282 tristate "Renesas Frame Compression Processor"
283 depends on ARCH_RENESAS || COMPILE_TEST
284 depends on OF
285 ---help---
286 This is a driver for the Renesas Frame Compression Processor (FCP).
287 The FCP is a companion module of video processing modules in the
288 Renesas R-Car Gen3 SoCs. It handles memory access for the codec,
289 VSP and FDP modules.
290
291 To compile this driver as a module, choose M here: the module
292 will be called rcar-fcp.
293
250config VIDEO_RENESAS_VSP1 294config VIDEO_RENESAS_VSP1
251 tristate "Renesas VSP1 Video Processing Engine" 295 tristate "Renesas VSP1 Video Processing Engine"
252 depends on VIDEO_V4L2 && VIDEO_V4L2_SUBDEV_API && HAS_DMA 296 depends on VIDEO_V4L2 && VIDEO_V4L2_SUBDEV_API && HAS_DMA
253 depends on (ARCH_RENESAS && OF) || COMPILE_TEST 297 depends on (ARCH_RENESAS && OF) || COMPILE_TEST
298 depends on (!ARM64 && !VIDEO_RENESAS_FCP) || VIDEO_RENESAS_FCP
254 select VIDEOBUF2_DMA_CONTIG 299 select VIDEOBUF2_DMA_CONTIG
255 ---help--- 300 ---help---
256 This is a V4L2 driver for the Renesas VSP1 video processing engine. 301 This is a V4L2 driver for the Renesas VSP1 video processing engine.
diff --git a/drivers/media/platform/Makefile b/drivers/media/platform/Makefile
index bbb7bd1eb268..21771c1a13fb 100644
--- a/drivers/media/platform/Makefile
+++ b/drivers/media/platform/Makefile
@@ -46,6 +46,7 @@ obj-$(CONFIG_VIDEO_SH_VOU) += sh_vou.o
46 46
47obj-$(CONFIG_SOC_CAMERA) += soc_camera/ 47obj-$(CONFIG_SOC_CAMERA) += soc_camera/
48 48
49obj-$(CONFIG_VIDEO_RENESAS_FCP) += rcar-fcp.o
49obj-$(CONFIG_VIDEO_RENESAS_JPU) += rcar_jpu.o 50obj-$(CONFIG_VIDEO_RENESAS_JPU) += rcar_jpu.o
50obj-$(CONFIG_VIDEO_RENESAS_VSP1) += vsp1/ 51obj-$(CONFIG_VIDEO_RENESAS_VSP1) += vsp1/
51 52
@@ -55,4 +56,10 @@ obj-$(CONFIG_VIDEO_AM437X_VPFE) += am437x/
55 56
56obj-$(CONFIG_VIDEO_XILINX) += xilinx/ 57obj-$(CONFIG_VIDEO_XILINX) += xilinx/
57 58
59obj-$(CONFIG_VIDEO_RCAR_VIN) += rcar-vin/
60
58ccflags-y += -I$(srctree)/drivers/media/i2c 61ccflags-y += -I$(srctree)/drivers/media/i2c
62
63obj-$(CONFIG_VIDEO_MEDIATEK_VPU) += mtk-vpu/
64
65obj-$(CONFIG_VIDEO_MEDIATEK_VCODEC) += mtk-vcodec/
diff --git a/drivers/media/platform/am437x/am437x-vpfe.c b/drivers/media/platform/am437x/am437x-vpfe.c
index e749eb7c3be9..b33b9e35e60e 100644
--- a/drivers/media/platform/am437x/am437x-vpfe.c
+++ b/drivers/media/platform/am437x/am437x-vpfe.c
@@ -1901,21 +1901,20 @@ static void vpfe_calculate_offsets(struct vpfe_device *vpfe)
1901 * @nbuffers: ptr to number of buffers requested by application 1901 * @nbuffers: ptr to number of buffers requested by application
1902 * @nplanes:: contains number of distinct video planes needed to hold a frame 1902 * @nplanes:: contains number of distinct video planes needed to hold a frame
1903 * @sizes[]: contains the size (in bytes) of each plane. 1903 * @sizes[]: contains the size (in bytes) of each plane.
1904 * @alloc_ctxs: ptr to allocation context 1904 * @alloc_devs: ptr to allocation context
1905 * 1905 *
1906 * This callback function is called when reqbuf() is called to adjust 1906 * This callback function is called when reqbuf() is called to adjust
1907 * the buffer count and buffer size 1907 * the buffer count and buffer size
1908 */ 1908 */
1909static int vpfe_queue_setup(struct vb2_queue *vq, 1909static int vpfe_queue_setup(struct vb2_queue *vq,
1910 unsigned int *nbuffers, unsigned int *nplanes, 1910 unsigned int *nbuffers, unsigned int *nplanes,
1911 unsigned int sizes[], void *alloc_ctxs[]) 1911 unsigned int sizes[], struct device *alloc_devs[])
1912{ 1912{
1913 struct vpfe_device *vpfe = vb2_get_drv_priv(vq); 1913 struct vpfe_device *vpfe = vb2_get_drv_priv(vq);
1914 unsigned size = vpfe->fmt.fmt.pix.sizeimage; 1914 unsigned size = vpfe->fmt.fmt.pix.sizeimage;
1915 1915
1916 if (vq->num_buffers + *nbuffers < 3) 1916 if (vq->num_buffers + *nbuffers < 3)
1917 *nbuffers = 3 - vq->num_buffers; 1917 *nbuffers = 3 - vq->num_buffers;
1918 alloc_ctxs[0] = vpfe->alloc_ctx;
1919 1918
1920 if (*nplanes) { 1919 if (*nplanes) {
1921 if (sizes[0] < size) 1920 if (sizes[0] < size)
@@ -2364,13 +2363,6 @@ static int vpfe_probe_complete(struct vpfe_device *vpfe)
2364 goto probe_out; 2363 goto probe_out;
2365 2364
2366 /* Initialize videobuf2 queue as per the buffer type */ 2365 /* Initialize videobuf2 queue as per the buffer type */
2367 vpfe->alloc_ctx = vb2_dma_contig_init_ctx(vpfe->pdev);
2368 if (IS_ERR(vpfe->alloc_ctx)) {
2369 vpfe_err(vpfe, "Failed to get the context\n");
2370 err = PTR_ERR(vpfe->alloc_ctx);
2371 goto probe_out;
2372 }
2373
2374 q = &vpfe->buffer_queue; 2366 q = &vpfe->buffer_queue;
2375 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; 2367 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2376 q->io_modes = VB2_MMAP | VB2_DMABUF | VB2_READ; 2368 q->io_modes = VB2_MMAP | VB2_DMABUF | VB2_READ;
@@ -2381,11 +2373,11 @@ static int vpfe_probe_complete(struct vpfe_device *vpfe)
2381 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC; 2373 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
2382 q->lock = &vpfe->lock; 2374 q->lock = &vpfe->lock;
2383 q->min_buffers_needed = 1; 2375 q->min_buffers_needed = 1;
2376 q->dev = vpfe->pdev;
2384 2377
2385 err = vb2_queue_init(q); 2378 err = vb2_queue_init(q);
2386 if (err) { 2379 if (err) {
2387 vpfe_err(vpfe, "vb2_queue_init() failed\n"); 2380 vpfe_err(vpfe, "vb2_queue_init() failed\n");
2388 vb2_dma_contig_cleanup_ctx(vpfe->alloc_ctx);
2389 goto probe_out; 2381 goto probe_out;
2390 } 2382 }
2391 2383
diff --git a/drivers/media/platform/am437x/am437x-vpfe.h b/drivers/media/platform/am437x/am437x-vpfe.h
index 777bf97fea57..17d7aa426788 100644
--- a/drivers/media/platform/am437x/am437x-vpfe.h
+++ b/drivers/media/platform/am437x/am437x-vpfe.h
@@ -264,8 +264,6 @@ struct vpfe_device {
264 struct v4l2_rect crop; 264 struct v4l2_rect crop;
265 /* Buffer queue used in video-buf */ 265 /* Buffer queue used in video-buf */
266 struct vb2_queue buffer_queue; 266 struct vb2_queue buffer_queue;
267 /* Allocator-specific contexts for each plane */
268 struct vb2_alloc_ctx *alloc_ctx;
269 /* Queue of filled frames */ 267 /* Queue of filled frames */
270 struct list_head dma_queue; 268 struct list_head dma_queue;
271 /* IRQ lock for DMA queue */ 269 /* IRQ lock for DMA queue */
diff --git a/drivers/media/platform/blackfin/bfin_capture.c b/drivers/media/platform/blackfin/bfin_capture.c
index d0092dae7a57..8eb03397d736 100644
--- a/drivers/media/platform/blackfin/bfin_capture.c
+++ b/drivers/media/platform/blackfin/bfin_capture.c
@@ -91,8 +91,6 @@ struct bcap_device {
91 struct bcap_buffer *cur_frm; 91 struct bcap_buffer *cur_frm;
92 /* buffer queue used in videobuf2 */ 92 /* buffer queue used in videobuf2 */
93 struct vb2_queue buffer_queue; 93 struct vb2_queue buffer_queue;
94 /* allocator-specific contexts for each plane */
95 struct vb2_alloc_ctx *alloc_ctx;
96 /* queue of filled frames */ 94 /* queue of filled frames */
97 struct list_head dma_queue; 95 struct list_head dma_queue;
98 /* used in videobuf2 callback */ 96 /* used in videobuf2 callback */
@@ -203,13 +201,12 @@ static void bcap_free_sensor_formats(struct bcap_device *bcap_dev)
203 201
204static int bcap_queue_setup(struct vb2_queue *vq, 202static int bcap_queue_setup(struct vb2_queue *vq,
205 unsigned int *nbuffers, unsigned int *nplanes, 203 unsigned int *nbuffers, unsigned int *nplanes,
206 unsigned int sizes[], void *alloc_ctxs[]) 204 unsigned int sizes[], struct device *alloc_devs[])
207{ 205{
208 struct bcap_device *bcap_dev = vb2_get_drv_priv(vq); 206 struct bcap_device *bcap_dev = vb2_get_drv_priv(vq);
209 207
210 if (vq->num_buffers + *nbuffers < 2) 208 if (vq->num_buffers + *nbuffers < 2)
211 *nbuffers = 2; 209 *nbuffers = 2;
212 alloc_ctxs[0] = bcap_dev->alloc_ctx;
213 210
214 if (*nplanes) 211 if (*nplanes)
215 return sizes[0] < bcap_dev->fmt.sizeimage ? -EINVAL : 0; 212 return sizes[0] < bcap_dev->fmt.sizeimage ? -EINVAL : 0;
@@ -820,12 +817,6 @@ static int bcap_probe(struct platform_device *pdev)
820 } 817 }
821 bcap_dev->ppi->priv = bcap_dev; 818 bcap_dev->ppi->priv = bcap_dev;
822 819
823 bcap_dev->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
824 if (IS_ERR(bcap_dev->alloc_ctx)) {
825 ret = PTR_ERR(bcap_dev->alloc_ctx);
826 goto err_free_ppi;
827 }
828
829 vfd = &bcap_dev->video_dev; 820 vfd = &bcap_dev->video_dev;
830 /* initialize field of video device */ 821 /* initialize field of video device */
831 vfd->release = video_device_release_empty; 822 vfd->release = video_device_release_empty;
@@ -839,7 +830,7 @@ static int bcap_probe(struct platform_device *pdev)
839 if (ret) { 830 if (ret) {
840 v4l2_err(pdev->dev.driver, 831 v4l2_err(pdev->dev.driver,
841 "Unable to register v4l2 device\n"); 832 "Unable to register v4l2 device\n");
842 goto err_cleanup_ctx; 833 goto err_free_ppi;
843 } 834 }
844 v4l2_info(&bcap_dev->v4l2_dev, "v4l2 device registered\n"); 835 v4l2_info(&bcap_dev->v4l2_dev, "v4l2 device registered\n");
845 836
@@ -863,6 +854,7 @@ static int bcap_probe(struct platform_device *pdev)
863 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC; 854 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
864 q->lock = &bcap_dev->mutex; 855 q->lock = &bcap_dev->mutex;
865 q->min_buffers_needed = 1; 856 q->min_buffers_needed = 1;
857 q->dev = &pdev->dev;
866 858
867 ret = vb2_queue_init(q); 859 ret = vb2_queue_init(q);
868 if (ret) 860 if (ret)
@@ -967,8 +959,6 @@ err_free_handler:
967 v4l2_ctrl_handler_free(&bcap_dev->ctrl_handler); 959 v4l2_ctrl_handler_free(&bcap_dev->ctrl_handler);
968err_unreg_v4l2: 960err_unreg_v4l2:
969 v4l2_device_unregister(&bcap_dev->v4l2_dev); 961 v4l2_device_unregister(&bcap_dev->v4l2_dev);
970err_cleanup_ctx:
971 vb2_dma_contig_cleanup_ctx(bcap_dev->alloc_ctx);
972err_free_ppi: 962err_free_ppi:
973 ppi_delete_instance(bcap_dev->ppi); 963 ppi_delete_instance(bcap_dev->ppi);
974err_free_dev: 964err_free_dev:
@@ -986,7 +976,6 @@ static int bcap_remove(struct platform_device *pdev)
986 video_unregister_device(&bcap_dev->video_dev); 976 video_unregister_device(&bcap_dev->video_dev);
987 v4l2_ctrl_handler_free(&bcap_dev->ctrl_handler); 977 v4l2_ctrl_handler_free(&bcap_dev->ctrl_handler);
988 v4l2_device_unregister(v4l2_dev); 978 v4l2_device_unregister(v4l2_dev);
989 vb2_dma_contig_cleanup_ctx(bcap_dev->alloc_ctx);
990 ppi_delete_instance(bcap_dev->ppi); 979 ppi_delete_instance(bcap_dev->ppi);
991 kfree(bcap_dev); 980 kfree(bcap_dev);
992 return 0; 981 return 0;
diff --git a/drivers/media/platform/coda/coda-common.c b/drivers/media/platform/coda/coda-common.c
index 133ab9f70f85..c39718a63e5e 100644
--- a/drivers/media/platform/coda/coda-common.c
+++ b/drivers/media/platform/coda/coda-common.c
@@ -1139,7 +1139,7 @@ static void set_default_params(struct coda_ctx *ctx)
1139 */ 1139 */
1140static int coda_queue_setup(struct vb2_queue *vq, 1140static int coda_queue_setup(struct vb2_queue *vq,
1141 unsigned int *nbuffers, unsigned int *nplanes, 1141 unsigned int *nbuffers, unsigned int *nplanes,
1142 unsigned int sizes[], void *alloc_ctxs[]) 1142 unsigned int sizes[], struct device *alloc_devs[])
1143{ 1143{
1144 struct coda_ctx *ctx = vb2_get_drv_priv(vq); 1144 struct coda_ctx *ctx = vb2_get_drv_priv(vq);
1145 struct coda_q_data *q_data; 1145 struct coda_q_data *q_data;
@@ -1151,9 +1151,6 @@ static int coda_queue_setup(struct vb2_queue *vq,
1151 *nplanes = 1; 1151 *nplanes = 1;
1152 sizes[0] = size; 1152 sizes[0] = size;
1153 1153
1154 /* Set to vb2-dma-contig allocator context, ignored by vb2-vmalloc */
1155 alloc_ctxs[0] = ctx->dev->alloc_ctx;
1156
1157 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev, 1154 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1158 "get %d buffer(s) of size %d each.\n", *nbuffers, size); 1155 "get %d buffer(s) of size %d each.\n", *nbuffers, size);
1159 1156
@@ -1599,6 +1596,7 @@ static int coda_queue_init(struct coda_ctx *ctx, struct vb2_queue *vq)
1599 * that videobuf2 will keep the value of bytesused intact. 1596 * that videobuf2 will keep the value of bytesused intact.
1600 */ 1597 */
1601 vq->allow_zero_bytesused = 1; 1598 vq->allow_zero_bytesused = 1;
1599 vq->dev = &ctx->dev->plat_dev->dev;
1602 1600
1603 return vb2_queue_init(vq); 1601 return vb2_queue_init(vq);
1604} 1602}
@@ -2040,16 +2038,10 @@ static void coda_fw_callback(const struct firmware *fw, void *context)
2040 if (ret < 0) 2038 if (ret < 0)
2041 goto put_pm; 2039 goto put_pm;
2042 2040
2043 dev->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
2044 if (IS_ERR(dev->alloc_ctx)) {
2045 v4l2_err(&dev->v4l2_dev, "Failed to alloc vb2 context\n");
2046 goto put_pm;
2047 }
2048
2049 dev->m2m_dev = v4l2_m2m_init(&coda_m2m_ops); 2041 dev->m2m_dev = v4l2_m2m_init(&coda_m2m_ops);
2050 if (IS_ERR(dev->m2m_dev)) { 2042 if (IS_ERR(dev->m2m_dev)) {
2051 v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem device\n"); 2043 v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem device\n");
2052 goto rel_ctx; 2044 goto put_pm;
2053 } 2045 }
2054 2046
2055 for (i = 0; i < dev->devtype->num_vdevs; i++) { 2047 for (i = 0; i < dev->devtype->num_vdevs; i++) {
@@ -2072,8 +2064,6 @@ rel_vfd:
2072 while (--i >= 0) 2064 while (--i >= 0)
2073 video_unregister_device(&dev->vfd[i]); 2065 video_unregister_device(&dev->vfd[i]);
2074 v4l2_m2m_release(dev->m2m_dev); 2066 v4l2_m2m_release(dev->m2m_dev);
2075rel_ctx:
2076 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
2077put_pm: 2067put_pm:
2078 pm_runtime_put_sync(&pdev->dev); 2068 pm_runtime_put_sync(&pdev->dev);
2079} 2069}
@@ -2226,7 +2216,7 @@ static int coda_probe(struct platform_device *pdev)
2226 dev->rstc = devm_reset_control_get_optional(&pdev->dev, NULL); 2216 dev->rstc = devm_reset_control_get_optional(&pdev->dev, NULL);
2227 if (IS_ERR(dev->rstc)) { 2217 if (IS_ERR(dev->rstc)) {
2228 ret = PTR_ERR(dev->rstc); 2218 ret = PTR_ERR(dev->rstc);
2229 if (ret == -ENOENT || ret == -ENOSYS) { 2219 if (ret == -ENOENT || ret == -ENOTSUPP) {
2230 dev->rstc = NULL; 2220 dev->rstc = NULL;
2231 } else { 2221 } else {
2232 dev_err(&pdev->dev, "failed get reset control: %d\n", 2222 dev_err(&pdev->dev, "failed get reset control: %d\n",
@@ -2324,8 +2314,6 @@ static int coda_remove(struct platform_device *pdev)
2324 if (dev->m2m_dev) 2314 if (dev->m2m_dev)
2325 v4l2_m2m_release(dev->m2m_dev); 2315 v4l2_m2m_release(dev->m2m_dev);
2326 pm_runtime_disable(&pdev->dev); 2316 pm_runtime_disable(&pdev->dev);
2327 if (dev->alloc_ctx)
2328 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
2329 v4l2_device_unregister(&dev->v4l2_dev); 2317 v4l2_device_unregister(&dev->v4l2_dev);
2330 destroy_workqueue(dev->workqueue); 2318 destroy_workqueue(dev->workqueue);
2331 if (dev->iram.vaddr) 2319 if (dev->iram.vaddr)
diff --git a/drivers/media/platform/coda/coda.h b/drivers/media/platform/coda/coda.h
index 8f2c71e06966..53f96661683c 100644
--- a/drivers/media/platform/coda/coda.h
+++ b/drivers/media/platform/coda/coda.h
@@ -92,7 +92,6 @@ struct coda_dev {
92 struct mutex coda_mutex; 92 struct mutex coda_mutex;
93 struct workqueue_struct *workqueue; 93 struct workqueue_struct *workqueue;
94 struct v4l2_m2m_dev *m2m_dev; 94 struct v4l2_m2m_dev *m2m_dev;
95 struct vb2_alloc_ctx *alloc_ctx;
96 struct list_head instances; 95 struct list_head instances;
97 unsigned long instance_mask; 96 unsigned long instance_mask;
98 struct dentry *debugfs_root; 97 struct dentry *debugfs_root;
diff --git a/drivers/media/platform/davinci/ccdc_hw_device.h b/drivers/media/platform/davinci/ccdc_hw_device.h
index 86b9b3518965..ae5605de7679 100644
--- a/drivers/media/platform/davinci/ccdc_hw_device.h
+++ b/drivers/media/platform/davinci/ccdc_hw_device.h
@@ -80,13 +80,6 @@ struct ccdc_hw_ops {
80 /* Pointer to function to get line length */ 80 /* Pointer to function to get line length */
81 unsigned int (*get_line_length) (void); 81 unsigned int (*get_line_length) (void);
82 82
83 /* Query CCDC control IDs */
84 int (*queryctrl)(struct v4l2_queryctrl *qctrl);
85 /* Set CCDC control */
86 int (*set_control)(struct v4l2_control *ctrl);
87 /* Get CCDC control */
88 int (*get_control)(struct v4l2_control *ctrl);
89
90 /* Pointer to function to set frame buffer address */ 83 /* Pointer to function to set frame buffer address */
91 void (*setfbaddr) (unsigned long addr); 84 void (*setfbaddr) (unsigned long addr);
92 /* Pointer to function to get field id */ 85 /* Pointer to function to get field id */
diff --git a/drivers/media/platform/davinci/vpbe_display.c b/drivers/media/platform/davinci/vpbe_display.c
index 0abcdfe97a6c..0b1709e96673 100644
--- a/drivers/media/platform/davinci/vpbe_display.c
+++ b/drivers/media/platform/davinci/vpbe_display.c
@@ -230,7 +230,7 @@ static int vpbe_buffer_prepare(struct vb2_buffer *vb)
230static int 230static int
231vpbe_buffer_queue_setup(struct vb2_queue *vq, 231vpbe_buffer_queue_setup(struct vb2_queue *vq,
232 unsigned int *nbuffers, unsigned int *nplanes, 232 unsigned int *nbuffers, unsigned int *nplanes,
233 unsigned int sizes[], void *alloc_ctxs[]) 233 unsigned int sizes[], struct device *alloc_devs[])
234 234
235{ 235{
236 /* Get the file handle object and layer object */ 236 /* Get the file handle object and layer object */
@@ -242,7 +242,6 @@ vpbe_buffer_queue_setup(struct vb2_queue *vq,
242 /* Store number of buffers allocated in numbuffer member */ 242 /* Store number of buffers allocated in numbuffer member */
243 if (vq->num_buffers + *nbuffers < VPBE_DEFAULT_NUM_BUFS) 243 if (vq->num_buffers + *nbuffers < VPBE_DEFAULT_NUM_BUFS)
244 *nbuffers = VPBE_DEFAULT_NUM_BUFS - vq->num_buffers; 244 *nbuffers = VPBE_DEFAULT_NUM_BUFS - vq->num_buffers;
245 alloc_ctxs[0] = layer->alloc_ctx;
246 245
247 if (*nplanes) 246 if (*nplanes)
248 return sizes[0] < layer->pix_fmt.sizeimage ? -EINVAL : 0; 247 return sizes[0] < layer->pix_fmt.sizeimage ? -EINVAL : 0;
@@ -1451,20 +1450,13 @@ static int vpbe_display_probe(struct platform_device *pdev)
1451 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC; 1450 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1452 q->min_buffers_needed = 1; 1451 q->min_buffers_needed = 1;
1453 q->lock = &disp_dev->dev[i]->opslock; 1452 q->lock = &disp_dev->dev[i]->opslock;
1453 q->dev = disp_dev->vpbe_dev->pdev;
1454 err = vb2_queue_init(q); 1454 err = vb2_queue_init(q);
1455 if (err) { 1455 if (err) {
1456 v4l2_err(v4l2_dev, "vb2_queue_init() failed\n"); 1456 v4l2_err(v4l2_dev, "vb2_queue_init() failed\n");
1457 goto probe_out; 1457 goto probe_out;
1458 } 1458 }
1459 1459
1460 disp_dev->dev[i]->alloc_ctx =
1461 vb2_dma_contig_init_ctx(disp_dev->vpbe_dev->pdev);
1462 if (IS_ERR(disp_dev->dev[i]->alloc_ctx)) {
1463 v4l2_err(v4l2_dev, "Failed to get the context\n");
1464 err = PTR_ERR(disp_dev->dev[i]->alloc_ctx);
1465 goto probe_out;
1466 }
1467
1468 INIT_LIST_HEAD(&disp_dev->dev[i]->dma_queue); 1460 INIT_LIST_HEAD(&disp_dev->dev[i]->dma_queue);
1469 1461
1470 if (register_device(disp_dev->dev[i], disp_dev, pdev)) { 1462 if (register_device(disp_dev->dev[i], disp_dev, pdev)) {
@@ -1482,7 +1474,6 @@ probe_out:
1482 for (k = 0; k < VPBE_DISPLAY_MAX_DEVICES; k++) { 1474 for (k = 0; k < VPBE_DISPLAY_MAX_DEVICES; k++) {
1483 /* Unregister video device */ 1475 /* Unregister video device */
1484 if (disp_dev->dev[k] != NULL) { 1476 if (disp_dev->dev[k] != NULL) {
1485 vb2_dma_contig_cleanup_ctx(disp_dev->dev[k]->alloc_ctx);
1486 video_unregister_device(&disp_dev->dev[k]->video_dev); 1477 video_unregister_device(&disp_dev->dev[k]->video_dev);
1487 kfree(disp_dev->dev[k]); 1478 kfree(disp_dev->dev[k]);
1488 } 1479 }
@@ -1510,7 +1501,6 @@ static int vpbe_display_remove(struct platform_device *pdev)
1510 for (i = 0; i < VPBE_DISPLAY_MAX_DEVICES; i++) { 1501 for (i = 0; i < VPBE_DISPLAY_MAX_DEVICES; i++) {
1511 /* Get the pointer to the layer object */ 1502 /* Get the pointer to the layer object */
1512 vpbe_display_layer = disp_dev->dev[i]; 1503 vpbe_display_layer = disp_dev->dev[i];
1513 vb2_dma_contig_cleanup_ctx(vpbe_display_layer->alloc_ctx);
1514 /* Unregister video device */ 1504 /* Unregister video device */
1515 video_unregister_device(&vpbe_display_layer->video_dev); 1505 video_unregister_device(&vpbe_display_layer->video_dev);
1516 1506
diff --git a/drivers/media/platform/davinci/vpif_capture.c b/drivers/media/platform/davinci/vpif_capture.c
index 08f7028c7560..5104cc0ee40e 100644
--- a/drivers/media/platform/davinci/vpif_capture.c
+++ b/drivers/media/platform/davinci/vpif_capture.c
@@ -107,14 +107,14 @@ static int vpif_buffer_prepare(struct vb2_buffer *vb)
107 * @nbuffers: ptr to number of buffers requested by application 107 * @nbuffers: ptr to number of buffers requested by application
108 * @nplanes:: contains number of distinct video planes needed to hold a frame 108 * @nplanes:: contains number of distinct video planes needed to hold a frame
109 * @sizes[]: contains the size (in bytes) of each plane. 109 * @sizes[]: contains the size (in bytes) of each plane.
110 * @alloc_ctxs: ptr to allocation context 110 * @alloc_devs: ptr to allocation context
111 * 111 *
112 * This callback function is called when reqbuf() is called to adjust 112 * This callback function is called when reqbuf() is called to adjust
113 * the buffer count and buffer size 113 * the buffer count and buffer size
114 */ 114 */
115static int vpif_buffer_queue_setup(struct vb2_queue *vq, 115static int vpif_buffer_queue_setup(struct vb2_queue *vq,
116 unsigned int *nbuffers, unsigned int *nplanes, 116 unsigned int *nbuffers, unsigned int *nplanes,
117 unsigned int sizes[], void *alloc_ctxs[]) 117 unsigned int sizes[], struct device *alloc_devs[])
118{ 118{
119 struct channel_obj *ch = vb2_get_drv_priv(vq); 119 struct channel_obj *ch = vb2_get_drv_priv(vq);
120 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX]; 120 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
@@ -133,7 +133,6 @@ static int vpif_buffer_queue_setup(struct vb2_queue *vq,
133 133
134 *nplanes = 1; 134 *nplanes = 1;
135 sizes[0] = size; 135 sizes[0] = size;
136 alloc_ctxs[0] = common->alloc_ctx;
137 136
138 /* Calculate the offset for Y and C data in the buffer */ 137 /* Calculate the offset for Y and C data in the buffer */
139 vpif_calculate_offsets(ch); 138 vpif_calculate_offsets(ch);
@@ -1371,6 +1370,7 @@ static int vpif_probe_complete(void)
1371 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC; 1370 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1372 q->min_buffers_needed = 1; 1371 q->min_buffers_needed = 1;
1373 q->lock = &common->lock; 1372 q->lock = &common->lock;
1373 q->dev = vpif_dev;
1374 1374
1375 err = vb2_queue_init(q); 1375 err = vb2_queue_init(q);
1376 if (err) { 1376 if (err) {
@@ -1378,13 +1378,6 @@ static int vpif_probe_complete(void)
1378 goto probe_out; 1378 goto probe_out;
1379 } 1379 }
1380 1380
1381 common->alloc_ctx = vb2_dma_contig_init_ctx(vpif_dev);
1382 if (IS_ERR(common->alloc_ctx)) {
1383 vpif_err("Failed to get the context\n");
1384 err = PTR_ERR(common->alloc_ctx);
1385 goto probe_out;
1386 }
1387
1388 INIT_LIST_HEAD(&common->dma_queue); 1381 INIT_LIST_HEAD(&common->dma_queue);
1389 1382
1390 /* Initialize the video_device structure */ 1383 /* Initialize the video_device structure */
@@ -1412,7 +1405,6 @@ probe_out:
1412 /* Get the pointer to the channel object */ 1405 /* Get the pointer to the channel object */
1413 ch = vpif_obj.dev[k]; 1406 ch = vpif_obj.dev[k];
1414 common = &ch->common[k]; 1407 common = &ch->common[k];
1415 vb2_dma_contig_cleanup_ctx(common->alloc_ctx);
1416 /* Unregister video device */ 1408 /* Unregister video device */
1417 video_unregister_device(&ch->video_dev); 1409 video_unregister_device(&ch->video_dev);
1418 } 1410 }
@@ -1546,7 +1538,6 @@ static int vpif_remove(struct platform_device *device)
1546 /* Get the pointer to the channel object */ 1538 /* Get the pointer to the channel object */
1547 ch = vpif_obj.dev[i]; 1539 ch = vpif_obj.dev[i];
1548 common = &ch->common[VPIF_VIDEO_INDEX]; 1540 common = &ch->common[VPIF_VIDEO_INDEX];
1549 vb2_dma_contig_cleanup_ctx(common->alloc_ctx);
1550 /* Unregister video device */ 1541 /* Unregister video device */
1551 video_unregister_device(&ch->video_dev); 1542 video_unregister_device(&ch->video_dev);
1552 kfree(vpif_obj.dev[i]); 1543 kfree(vpif_obj.dev[i]);
diff --git a/drivers/media/platform/davinci/vpif_capture.h b/drivers/media/platform/davinci/vpif_capture.h
index 4a7600929b61..9e35b6771d22 100644
--- a/drivers/media/platform/davinci/vpif_capture.h
+++ b/drivers/media/platform/davinci/vpif_capture.h
@@ -65,8 +65,6 @@ struct common_obj {
65 struct v4l2_format fmt; 65 struct v4l2_format fmt;
66 /* Buffer queue used in video-buf */ 66 /* Buffer queue used in video-buf */
67 struct vb2_queue buffer_queue; 67 struct vb2_queue buffer_queue;
68 /* allocator-specific contexts for each plane */
69 struct vb2_alloc_ctx *alloc_ctx;
70 /* Queue of filled frames */ 68 /* Queue of filled frames */
71 struct list_head dma_queue; 69 struct list_head dma_queue;
72 /* Used in video-buf */ 70 /* Used in video-buf */
diff --git a/drivers/media/platform/davinci/vpif_display.c b/drivers/media/platform/davinci/vpif_display.c
index f40755cf1bf2..75b27233ec2f 100644
--- a/drivers/media/platform/davinci/vpif_display.c
+++ b/drivers/media/platform/davinci/vpif_display.c
@@ -102,14 +102,14 @@ static int vpif_buffer_prepare(struct vb2_buffer *vb)
102 * @nbuffers: ptr to number of buffers requested by application 102 * @nbuffers: ptr to number of buffers requested by application
103 * @nplanes:: contains number of distinct video planes needed to hold a frame 103 * @nplanes:: contains number of distinct video planes needed to hold a frame
104 * @sizes[]: contains the size (in bytes) of each plane. 104 * @sizes[]: contains the size (in bytes) of each plane.
105 * @alloc_ctxs: ptr to allocation context 105 * @alloc_devs: ptr to allocation context
106 * 106 *
107 * This callback function is called when reqbuf() is called to adjust 107 * This callback function is called when reqbuf() is called to adjust
108 * the buffer count and buffer size 108 * the buffer count and buffer size
109 */ 109 */
110static int vpif_buffer_queue_setup(struct vb2_queue *vq, 110static int vpif_buffer_queue_setup(struct vb2_queue *vq,
111 unsigned int *nbuffers, unsigned int *nplanes, 111 unsigned int *nbuffers, unsigned int *nplanes,
112 unsigned int sizes[], void *alloc_ctxs[]) 112 unsigned int sizes[], struct device *alloc_devs[])
113{ 113{
114 struct channel_obj *ch = vb2_get_drv_priv(vq); 114 struct channel_obj *ch = vb2_get_drv_priv(vq);
115 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX]; 115 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
@@ -126,7 +126,6 @@ static int vpif_buffer_queue_setup(struct vb2_queue *vq,
126 126
127 *nplanes = 1; 127 *nplanes = 1;
128 sizes[0] = size; 128 sizes[0] = size;
129 alloc_ctxs[0] = common->alloc_ctx;
130 129
131 /* Calculate the offset for Y and C data in the buffer */ 130 /* Calculate the offset for Y and C data in the buffer */
132 vpif_calculate_offsets(ch); 131 vpif_calculate_offsets(ch);
@@ -1191,19 +1190,13 @@ static int vpif_probe_complete(void)
1191 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC; 1190 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1192 q->min_buffers_needed = 1; 1191 q->min_buffers_needed = 1;
1193 q->lock = &common->lock; 1192 q->lock = &common->lock;
1193 q->dev = vpif_dev;
1194 err = vb2_queue_init(q); 1194 err = vb2_queue_init(q);
1195 if (err) { 1195 if (err) {
1196 vpif_err("vpif_display: vb2_queue_init() failed\n"); 1196 vpif_err("vpif_display: vb2_queue_init() failed\n");
1197 goto probe_out; 1197 goto probe_out;
1198 } 1198 }
1199 1199
1200 common->alloc_ctx = vb2_dma_contig_init_ctx(vpif_dev);
1201 if (IS_ERR(common->alloc_ctx)) {
1202 vpif_err("Failed to get the context\n");
1203 err = PTR_ERR(common->alloc_ctx);
1204 goto probe_out;
1205 }
1206
1207 INIT_LIST_HEAD(&common->dma_queue); 1200 INIT_LIST_HEAD(&common->dma_queue);
1208 1201
1209 /* register video device */ 1202 /* register video device */
@@ -1233,7 +1226,6 @@ probe_out:
1233 for (k = 0; k < j; k++) { 1226 for (k = 0; k < j; k++) {
1234 ch = vpif_obj.dev[k]; 1227 ch = vpif_obj.dev[k];
1235 common = &ch->common[k]; 1228 common = &ch->common[k];
1236 vb2_dma_contig_cleanup_ctx(common->alloc_ctx);
1237 video_unregister_device(&ch->video_dev); 1229 video_unregister_device(&ch->video_dev);
1238 } 1230 }
1239 return err; 1231 return err;
@@ -1355,7 +1347,6 @@ static int vpif_remove(struct platform_device *device)
1355 /* Get the pointer to the channel object */ 1347 /* Get the pointer to the channel object */
1356 ch = vpif_obj.dev[i]; 1348 ch = vpif_obj.dev[i];
1357 common = &ch->common[VPIF_VIDEO_INDEX]; 1349 common = &ch->common[VPIF_VIDEO_INDEX];
1358 vb2_dma_contig_cleanup_ctx(common->alloc_ctx);
1359 /* Unregister video device */ 1350 /* Unregister video device */
1360 video_unregister_device(&ch->video_dev); 1351 video_unregister_device(&ch->video_dev);
1361 kfree(vpif_obj.dev[i]); 1352 kfree(vpif_obj.dev[i]);
diff --git a/drivers/media/platform/davinci/vpif_display.h b/drivers/media/platform/davinci/vpif_display.h
index e7a1723a1b7a..af2765fdcea8 100644
--- a/drivers/media/platform/davinci/vpif_display.h
+++ b/drivers/media/platform/davinci/vpif_display.h
@@ -74,8 +74,6 @@ struct common_obj {
74 struct v4l2_format fmt; /* Used to store the format */ 74 struct v4l2_format fmt; /* Used to store the format */
75 struct vb2_queue buffer_queue; /* Buffer queue used in 75 struct vb2_queue buffer_queue; /* Buffer queue used in
76 * video-buf */ 76 * video-buf */
77 /* allocator-specific contexts for each plane */
78 struct vb2_alloc_ctx *alloc_ctx;
79 77
80 struct list_head dma_queue; /* Queue of filled frames */ 78 struct list_head dma_queue; /* Queue of filled frames */
81 spinlock_t irqlock; /* Used in video-buf */ 79 spinlock_t irqlock; /* Used in video-buf */
diff --git a/drivers/media/platform/exynos-gsc/gsc-core.c b/drivers/media/platform/exynos-gsc/gsc-core.c
index c04973669a47..787bd16c19e5 100644
--- a/drivers/media/platform/exynos-gsc/gsc-core.c
+++ b/drivers/media/platform/exynos-gsc/gsc-core.c
@@ -1123,19 +1123,13 @@ static int gsc_probe(struct platform_device *pdev)
1123 if (ret < 0) 1123 if (ret < 0)
1124 goto err_m2m; 1124 goto err_m2m;
1125 1125
1126 /* Initialize continious memory allocator */ 1126 vb2_dma_contig_set_max_seg_size(dev, DMA_BIT_MASK(32));
1127 gsc->alloc_ctx = vb2_dma_contig_init_ctx(dev);
1128 if (IS_ERR(gsc->alloc_ctx)) {
1129 ret = PTR_ERR(gsc->alloc_ctx);
1130 goto err_pm;
1131 }
1132 1127
1133 dev_dbg(dev, "gsc-%d registered successfully\n", gsc->id); 1128 dev_dbg(dev, "gsc-%d registered successfully\n", gsc->id);
1134 1129
1135 pm_runtime_put(dev); 1130 pm_runtime_put(dev);
1136 return 0; 1131 return 0;
1137err_pm: 1132
1138 pm_runtime_put(dev);
1139err_m2m: 1133err_m2m:
1140 gsc_unregister_m2m_device(gsc); 1134 gsc_unregister_m2m_device(gsc);
1141err_v4l2: 1135err_v4l2:
@@ -1152,7 +1146,7 @@ static int gsc_remove(struct platform_device *pdev)
1152 gsc_unregister_m2m_device(gsc); 1146 gsc_unregister_m2m_device(gsc);
1153 v4l2_device_unregister(&gsc->v4l2_dev); 1147 v4l2_device_unregister(&gsc->v4l2_dev);
1154 1148
1155 vb2_dma_contig_cleanup_ctx(gsc->alloc_ctx); 1149 vb2_dma_contig_clear_max_seg_size(&pdev->dev);
1156 pm_runtime_disable(&pdev->dev); 1150 pm_runtime_disable(&pdev->dev);
1157 gsc_clk_put(gsc); 1151 gsc_clk_put(gsc);
1158 1152
diff --git a/drivers/media/platform/exynos-gsc/gsc-core.h b/drivers/media/platform/exynos-gsc/gsc-core.h
index ec4000c72172..7ad7b9dc2243 100644
--- a/drivers/media/platform/exynos-gsc/gsc-core.h
+++ b/drivers/media/platform/exynos-gsc/gsc-core.h
@@ -327,7 +327,6 @@ struct gsc_driverdata {
327 * @irq_queue: interrupt handler waitqueue 327 * @irq_queue: interrupt handler waitqueue
328 * @m2m: memory-to-memory V4L2 device information 328 * @m2m: memory-to-memory V4L2 device information
329 * @state: flags used to synchronize m2m and capture mode operation 329 * @state: flags used to synchronize m2m and capture mode operation
330 * @alloc_ctx: videobuf2 memory allocator context
331 * @vdev: video device for G-Scaler instance 330 * @vdev: video device for G-Scaler instance
332 */ 331 */
333struct gsc_dev { 332struct gsc_dev {
@@ -341,7 +340,6 @@ struct gsc_dev {
341 wait_queue_head_t irq_queue; 340 wait_queue_head_t irq_queue;
342 struct gsc_m2m_device m2m; 341 struct gsc_m2m_device m2m;
343 unsigned long state; 342 unsigned long state;
344 struct vb2_alloc_ctx *alloc_ctx;
345 struct video_device vdev; 343 struct video_device vdev;
346 struct v4l2_device v4l2_dev; 344 struct v4l2_device v4l2_dev;
347}; 345};
diff --git a/drivers/media/platform/exynos-gsc/gsc-m2m.c b/drivers/media/platform/exynos-gsc/gsc-m2m.c
index a600e32e2543..ec6494cbdd45 100644
--- a/drivers/media/platform/exynos-gsc/gsc-m2m.c
+++ b/drivers/media/platform/exynos-gsc/gsc-m2m.c
@@ -213,7 +213,7 @@ put_device:
213 213
214static int gsc_m2m_queue_setup(struct vb2_queue *vq, 214static int gsc_m2m_queue_setup(struct vb2_queue *vq,
215 unsigned int *num_buffers, unsigned int *num_planes, 215 unsigned int *num_buffers, unsigned int *num_planes,
216 unsigned int sizes[], void *allocators[]) 216 unsigned int sizes[], struct device *alloc_devs[])
217{ 217{
218 struct gsc_ctx *ctx = vb2_get_drv_priv(vq); 218 struct gsc_ctx *ctx = vb2_get_drv_priv(vq);
219 struct gsc_frame *frame; 219 struct gsc_frame *frame;
@@ -227,10 +227,8 @@ static int gsc_m2m_queue_setup(struct vb2_queue *vq,
227 return -EINVAL; 227 return -EINVAL;
228 228
229 *num_planes = frame->fmt->num_planes; 229 *num_planes = frame->fmt->num_planes;
230 for (i = 0; i < frame->fmt->num_planes; i++) { 230 for (i = 0; i < frame->fmt->num_planes; i++)
231 sizes[i] = frame->payload[i]; 231 sizes[i] = frame->payload[i];
232 allocators[i] = ctx->gsc_dev->alloc_ctx;
233 }
234 return 0; 232 return 0;
235} 233}
236 234
@@ -591,6 +589,7 @@ static int queue_init(void *priv, struct vb2_queue *src_vq,
591 src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer); 589 src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
592 src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY; 590 src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
593 src_vq->lock = &ctx->gsc_dev->lock; 591 src_vq->lock = &ctx->gsc_dev->lock;
592 src_vq->dev = &ctx->gsc_dev->pdev->dev;
594 593
595 ret = vb2_queue_init(src_vq); 594 ret = vb2_queue_init(src_vq);
596 if (ret) 595 if (ret)
@@ -605,6 +604,7 @@ static int queue_init(void *priv, struct vb2_queue *src_vq,
605 dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer); 604 dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
606 dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY; 605 dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
607 dst_vq->lock = &ctx->gsc_dev->lock; 606 dst_vq->lock = &ctx->gsc_dev->lock;
607 dst_vq->dev = &ctx->gsc_dev->pdev->dev;
608 608
609 return vb2_queue_init(dst_vq); 609 return vb2_queue_init(dst_vq);
610} 610}
diff --git a/drivers/media/platform/exynos4-is/fimc-capture.c b/drivers/media/platform/exynos4-is/fimc-capture.c
index bf47d3b9cbe7..fdec499fbbda 100644
--- a/drivers/media/platform/exynos4-is/fimc-capture.c
+++ b/drivers/media/platform/exynos4-is/fimc-capture.c
@@ -340,7 +340,7 @@ int fimc_capture_resume(struct fimc_dev *fimc)
340 340
341static int queue_setup(struct vb2_queue *vq, 341static int queue_setup(struct vb2_queue *vq,
342 unsigned int *num_buffers, unsigned int *num_planes, 342 unsigned int *num_buffers, unsigned int *num_planes,
343 unsigned int sizes[], void *allocators[]) 343 unsigned int sizes[], struct device *alloc_devs[])
344{ 344{
345 struct fimc_ctx *ctx = vq->drv_priv; 345 struct fimc_ctx *ctx = vq->drv_priv;
346 struct fimc_frame *frame = &ctx->d_frame; 346 struct fimc_frame *frame = &ctx->d_frame;
@@ -354,11 +354,9 @@ static int queue_setup(struct vb2_queue *vq,
354 if (*num_planes) { 354 if (*num_planes) {
355 if (*num_planes != fmt->memplanes) 355 if (*num_planes != fmt->memplanes)
356 return -EINVAL; 356 return -EINVAL;
357 for (i = 0; i < *num_planes; i++) { 357 for (i = 0; i < *num_planes; i++)
358 if (sizes[i] < (wh * fmt->depth[i]) / 8) 358 if (sizes[i] < (wh * fmt->depth[i]) / 8)
359 return -EINVAL; 359 return -EINVAL;
360 allocators[i] = ctx->fimc_dev->alloc_ctx;
361 }
362 return 0; 360 return 0;
363 } 361 }
364 362
@@ -371,8 +369,6 @@ static int queue_setup(struct vb2_queue *vq,
371 sizes[i] = frame->payload[i]; 369 sizes[i] = frame->payload[i];
372 else 370 else
373 sizes[i] = max_t(u32, size, frame->payload[i]); 371 sizes[i] = max_t(u32, size, frame->payload[i]);
374
375 allocators[i] = ctx->fimc_dev->alloc_ctx;
376 } 372 }
377 373
378 return 0; 374 return 0;
@@ -1779,6 +1775,7 @@ static int fimc_register_capture_device(struct fimc_dev *fimc,
1779 q->buf_struct_size = sizeof(struct fimc_vid_buffer); 1775 q->buf_struct_size = sizeof(struct fimc_vid_buffer);
1780 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC; 1776 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1781 q->lock = &fimc->lock; 1777 q->lock = &fimc->lock;
1778 q->dev = &fimc->pdev->dev;
1782 1779
1783 ret = vb2_queue_init(q); 1780 ret = vb2_queue_init(q);
1784 if (ret) 1781 if (ret)
diff --git a/drivers/media/platform/exynos4-is/fimc-core.c b/drivers/media/platform/exynos4-is/fimc-core.c
index b1c1cea82a27..8f89ca21b631 100644
--- a/drivers/media/platform/exynos4-is/fimc-core.c
+++ b/drivers/media/platform/exynos4-is/fimc-core.c
@@ -1018,19 +1018,11 @@ static int fimc_probe(struct platform_device *pdev)
1018 goto err_sd; 1018 goto err_sd;
1019 } 1019 }
1020 1020
1021 /* Initialize contiguous memory allocator */ 1021 vb2_dma_contig_set_max_seg_size(dev, DMA_BIT_MASK(32));
1022 fimc->alloc_ctx = vb2_dma_contig_init_ctx(dev);
1023 if (IS_ERR(fimc->alloc_ctx)) {
1024 ret = PTR_ERR(fimc->alloc_ctx);
1025 goto err_gclk;
1026 }
1027 1022
1028 dev_dbg(dev, "FIMC.%d registered successfully\n", fimc->id); 1023 dev_dbg(dev, "FIMC.%d registered successfully\n", fimc->id);
1029 return 0; 1024 return 0;
1030 1025
1031err_gclk:
1032 if (!pm_runtime_enabled(dev))
1033 clk_disable(fimc->clock[CLK_GATE]);
1034err_sd: 1026err_sd:
1035 fimc_unregister_capture_subdev(fimc); 1027 fimc_unregister_capture_subdev(fimc);
1036err_sclk: 1028err_sclk:
@@ -1123,7 +1115,7 @@ static int fimc_remove(struct platform_device *pdev)
1123 pm_runtime_set_suspended(&pdev->dev); 1115 pm_runtime_set_suspended(&pdev->dev);
1124 1116
1125 fimc_unregister_capture_subdev(fimc); 1117 fimc_unregister_capture_subdev(fimc);
1126 vb2_dma_contig_cleanup_ctx(fimc->alloc_ctx); 1118 vb2_dma_contig_clear_max_seg_size(&pdev->dev);
1127 1119
1128 clk_disable(fimc->clock[CLK_BUS]); 1120 clk_disable(fimc->clock[CLK_BUS]);
1129 fimc_clk_put(fimc); 1121 fimc_clk_put(fimc);
diff --git a/drivers/media/platform/exynos4-is/fimc-core.h b/drivers/media/platform/exynos4-is/fimc-core.h
index 6b7435453d2a..5615fefbf7af 100644
--- a/drivers/media/platform/exynos4-is/fimc-core.h
+++ b/drivers/media/platform/exynos4-is/fimc-core.h
@@ -307,7 +307,6 @@ struct fimc_m2m_device {
307 */ 307 */
308struct fimc_vid_cap { 308struct fimc_vid_cap {
309 struct fimc_ctx *ctx; 309 struct fimc_ctx *ctx;
310 struct vb2_alloc_ctx *alloc_ctx;
311 struct v4l2_subdev subdev; 310 struct v4l2_subdev subdev;
312 struct exynos_video_entity ve; 311 struct exynos_video_entity ve;
313 struct media_pad vd_pad; 312 struct media_pad vd_pad;
@@ -417,7 +416,6 @@ struct fimc_ctx;
417 * @m2m: memory-to-memory V4L2 device information 416 * @m2m: memory-to-memory V4L2 device information
418 * @vid_cap: camera capture device information 417 * @vid_cap: camera capture device information
419 * @state: flags used to synchronize m2m and capture mode operation 418 * @state: flags used to synchronize m2m and capture mode operation
420 * @alloc_ctx: videobuf2 memory allocator context
421 * @pipeline: fimc video capture pipeline data structure 419 * @pipeline: fimc video capture pipeline data structure
422 */ 420 */
423struct fimc_dev { 421struct fimc_dev {
@@ -436,7 +434,6 @@ struct fimc_dev {
436 struct fimc_m2m_device m2m; 434 struct fimc_m2m_device m2m;
437 struct fimc_vid_cap vid_cap; 435 struct fimc_vid_cap vid_cap;
438 unsigned long state; 436 unsigned long state;
439 struct vb2_alloc_ctx *alloc_ctx;
440}; 437};
441 438
442/** 439/**
diff --git a/drivers/media/platform/exynos4-is/fimc-is.c b/drivers/media/platform/exynos4-is/fimc-is.c
index 979c388ebf60..32ca55f16677 100644
--- a/drivers/media/platform/exynos4-is/fimc-is.c
+++ b/drivers/media/platform/exynos4-is/fimc-is.c
@@ -204,9 +204,6 @@ static int fimc_is_register_subdevs(struct fimc_is *is)
204 if (ret < 0) 204 if (ret < 0)
205 return ret; 205 return ret;
206 206
207 /* Initialize memory allocator context for the ISP DMA. */
208 is->isp.alloc_ctx = is->alloc_ctx;
209
210 for_each_compatible_node(i2c_bus, NULL, FIMC_IS_I2C_COMPATIBLE) { 207 for_each_compatible_node(i2c_bus, NULL, FIMC_IS_I2C_COMPATIBLE) {
211 for_each_available_child_of_node(i2c_bus, child) { 208 for_each_available_child_of_node(i2c_bus, child) {
212 ret = fimc_is_parse_sensor_config(is, index, child); 209 ret = fimc_is_parse_sensor_config(is, index, child);
@@ -847,18 +844,14 @@ static int fimc_is_probe(struct platform_device *pdev)
847 if (ret < 0) 844 if (ret < 0)
848 goto err_pm; 845 goto err_pm;
849 846
850 is->alloc_ctx = vb2_dma_contig_init_ctx(dev); 847 vb2_dma_contig_set_max_seg_size(dev, DMA_BIT_MASK(32));
851 if (IS_ERR(is->alloc_ctx)) {
852 ret = PTR_ERR(is->alloc_ctx);
853 goto err_pm;
854 }
855 /* 848 /*
856 * Register FIMC-IS V4L2 subdevs to this driver. The video nodes 849 * Register FIMC-IS V4L2 subdevs to this driver. The video nodes
857 * will be created within the subdev's registered() callback. 850 * will be created within the subdev's registered() callback.
858 */ 851 */
859 ret = fimc_is_register_subdevs(is); 852 ret = fimc_is_register_subdevs(is);
860 if (ret < 0) 853 if (ret < 0)
861 goto err_vb; 854 goto err_pm;
862 855
863 ret = fimc_is_debugfs_create(is); 856 ret = fimc_is_debugfs_create(is);
864 if (ret < 0) 857 if (ret < 0)
@@ -877,8 +870,6 @@ err_dfs:
877 fimc_is_debugfs_remove(is); 870 fimc_is_debugfs_remove(is);
878err_sd: 871err_sd:
879 fimc_is_unregister_subdevs(is); 872 fimc_is_unregister_subdevs(is);
880err_vb:
881 vb2_dma_contig_cleanup_ctx(is->alloc_ctx);
882err_pm: 873err_pm:
883 if (!pm_runtime_enabled(dev)) 874 if (!pm_runtime_enabled(dev))
884 fimc_is_runtime_suspend(dev); 875 fimc_is_runtime_suspend(dev);
@@ -939,7 +930,7 @@ static int fimc_is_remove(struct platform_device *pdev)
939 fimc_is_runtime_suspend(dev); 930 fimc_is_runtime_suspend(dev);
940 free_irq(is->irq, is); 931 free_irq(is->irq, is);
941 fimc_is_unregister_subdevs(is); 932 fimc_is_unregister_subdevs(is);
942 vb2_dma_contig_cleanup_ctx(is->alloc_ctx); 933 vb2_dma_contig_clear_max_seg_size(dev);
943 fimc_is_put_clocks(is); 934 fimc_is_put_clocks(is);
944 fimc_is_debugfs_remove(is); 935 fimc_is_debugfs_remove(is);
945 release_firmware(is->fw.f_w); 936 release_firmware(is->fw.f_w);
diff --git a/drivers/media/platform/exynos4-is/fimc-is.h b/drivers/media/platform/exynos4-is/fimc-is.h
index 386eb49ece7e..3a82c6a214c7 100644
--- a/drivers/media/platform/exynos4-is/fimc-is.h
+++ b/drivers/media/platform/exynos4-is/fimc-is.h
@@ -233,7 +233,6 @@ struct chain_config {
233 * @pdev: pointer to FIMC-IS platform device 233 * @pdev: pointer to FIMC-IS platform device
234 * @pctrl: pointer to pinctrl structure for this device 234 * @pctrl: pointer to pinctrl structure for this device
235 * @v4l2_dev: pointer to top the level v4l2_device 235 * @v4l2_dev: pointer to top the level v4l2_device
236 * @alloc_ctx: videobuf2 memory allocator context
237 * @lock: mutex serializing video device and the subdev operations 236 * @lock: mutex serializing video device and the subdev operations
238 * @slock: spinlock protecting this data structure and the hw registers 237 * @slock: spinlock protecting this data structure and the hw registers
239 * @clocks: FIMC-LITE gate clock 238 * @clocks: FIMC-LITE gate clock
@@ -256,7 +255,6 @@ struct fimc_is {
256 struct fimc_is_sensor sensor[FIMC_IS_SENSORS_NUM]; 255 struct fimc_is_sensor sensor[FIMC_IS_SENSORS_NUM];
257 struct fimc_is_setfile setfile; 256 struct fimc_is_setfile setfile;
258 257
259 struct vb2_alloc_ctx *alloc_ctx;
260 struct v4l2_ctrl_handler ctrl_handler; 258 struct v4l2_ctrl_handler ctrl_handler;
261 259
262 struct mutex lock; 260 struct mutex lock;
diff --git a/drivers/media/platform/exynos4-is/fimc-isp-video.c b/drivers/media/platform/exynos4-is/fimc-isp-video.c
index c0816728cbfe..400ce0cb0c0d 100644
--- a/drivers/media/platform/exynos4-is/fimc-isp-video.c
+++ b/drivers/media/platform/exynos4-is/fimc-isp-video.c
@@ -40,7 +40,7 @@
40 40
41static int isp_video_capture_queue_setup(struct vb2_queue *vq, 41static int isp_video_capture_queue_setup(struct vb2_queue *vq,
42 unsigned int *num_buffers, unsigned int *num_planes, 42 unsigned int *num_buffers, unsigned int *num_planes,
43 unsigned int sizes[], void *allocators[]) 43 unsigned int sizes[], struct device *alloc_devs[])
44{ 44{
45 struct fimc_isp *isp = vb2_get_drv_priv(vq); 45 struct fimc_isp *isp = vb2_get_drv_priv(vq);
46 struct v4l2_pix_format_mplane *vid_fmt = &isp->video_capture.pixfmt; 46 struct v4l2_pix_format_mplane *vid_fmt = &isp->video_capture.pixfmt;
@@ -57,20 +57,16 @@ static int isp_video_capture_queue_setup(struct vb2_queue *vq,
57 if (*num_planes) { 57 if (*num_planes) {
58 if (*num_planes != fmt->memplanes) 58 if (*num_planes != fmt->memplanes)
59 return -EINVAL; 59 return -EINVAL;
60 for (i = 0; i < *num_planes; i++) { 60 for (i = 0; i < *num_planes; i++)
61 if (sizes[i] < (wh * fmt->depth[i]) / 8) 61 if (sizes[i] < (wh * fmt->depth[i]) / 8)
62 return -EINVAL; 62 return -EINVAL;
63 allocators[i] = isp->alloc_ctx;
64 }
65 return 0; 63 return 0;
66 } 64 }
67 65
68 *num_planes = fmt->memplanes; 66 *num_planes = fmt->memplanes;
69 67
70 for (i = 0; i < fmt->memplanes; i++) { 68 for (i = 0; i < fmt->memplanes; i++)
71 sizes[i] = (wh * fmt->depth[i]) / 8; 69 sizes[i] = (wh * fmt->depth[i]) / 8;
72 allocators[i] = isp->alloc_ctx;
73 }
74 70
75 return 0; 71 return 0;
76} 72}
@@ -597,6 +593,7 @@ int fimc_isp_video_device_register(struct fimc_isp *isp,
597 q->drv_priv = isp; 593 q->drv_priv = isp;
598 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC; 594 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
599 q->lock = &isp->video_lock; 595 q->lock = &isp->video_lock;
596 q->dev = &isp->pdev->dev;
600 597
601 ret = vb2_queue_init(q); 598 ret = vb2_queue_init(q);
602 if (ret < 0) 599 if (ret < 0)
diff --git a/drivers/media/platform/exynos4-is/fimc-isp.h b/drivers/media/platform/exynos4-is/fimc-isp.h
index e0686b5f1bf8..3cdd52491294 100644
--- a/drivers/media/platform/exynos4-is/fimc-isp.h
+++ b/drivers/media/platform/exynos4-is/fimc-isp.h
@@ -148,7 +148,6 @@ struct fimc_is_video {
148/** 148/**
149 * struct fimc_isp - FIMC-IS ISP data structure 149 * struct fimc_isp - FIMC-IS ISP data structure
150 * @pdev: pointer to FIMC-IS platform device 150 * @pdev: pointer to FIMC-IS platform device
151 * @alloc_ctx: videobuf2 memory allocator context
152 * @subdev: ISP v4l2_subdev 151 * @subdev: ISP v4l2_subdev
153 * @subdev_pads: the ISP subdev media pads 152 * @subdev_pads: the ISP subdev media pads
154 * @test_pattern: test pattern controls 153 * @test_pattern: test pattern controls
@@ -161,7 +160,6 @@ struct fimc_is_video {
161 */ 160 */
162struct fimc_isp { 161struct fimc_isp {
163 struct platform_device *pdev; 162 struct platform_device *pdev;
164 struct vb2_alloc_ctx *alloc_ctx;
165 struct v4l2_subdev subdev; 163 struct v4l2_subdev subdev;
166 struct media_pad subdev_pads[FIMC_ISP_SD_PADS_NUM]; 164 struct media_pad subdev_pads[FIMC_ISP_SD_PADS_NUM];
167 struct v4l2_mbus_framefmt src_fmt; 165 struct v4l2_mbus_framefmt src_fmt;
diff --git a/drivers/media/platform/exynos4-is/fimc-lite.c b/drivers/media/platform/exynos4-is/fimc-lite.c
index dc1b929f7a33..a0f149fb88e1 100644
--- a/drivers/media/platform/exynos4-is/fimc-lite.c
+++ b/drivers/media/platform/exynos4-is/fimc-lite.c
@@ -357,7 +357,7 @@ static void stop_streaming(struct vb2_queue *q)
357 357
358static int queue_setup(struct vb2_queue *vq, 358static int queue_setup(struct vb2_queue *vq,
359 unsigned int *num_buffers, unsigned int *num_planes, 359 unsigned int *num_buffers, unsigned int *num_planes,
360 unsigned int sizes[], void *allocators[]) 360 unsigned int sizes[], struct device *alloc_devs[])
361{ 361{
362 struct fimc_lite *fimc = vq->drv_priv; 362 struct fimc_lite *fimc = vq->drv_priv;
363 struct flite_frame *frame = &fimc->out_frame; 363 struct flite_frame *frame = &fimc->out_frame;
@@ -371,20 +371,16 @@ static int queue_setup(struct vb2_queue *vq,
371 if (*num_planes) { 371 if (*num_planes) {
372 if (*num_planes != fmt->memplanes) 372 if (*num_planes != fmt->memplanes)
373 return -EINVAL; 373 return -EINVAL;
374 for (i = 0; i < *num_planes; i++) { 374 for (i = 0; i < *num_planes; i++)
375 if (sizes[i] < (wh * fmt->depth[i]) / 8) 375 if (sizes[i] < (wh * fmt->depth[i]) / 8)
376 return -EINVAL; 376 return -EINVAL;
377 allocators[i] = fimc->alloc_ctx;
378 }
379 return 0; 377 return 0;
380 } 378 }
381 379
382 *num_planes = fmt->memplanes; 380 *num_planes = fmt->memplanes;
383 381
384 for (i = 0; i < fmt->memplanes; i++) { 382 for (i = 0; i < fmt->memplanes; i++)
385 sizes[i] = (wh * fmt->depth[i]) / 8; 383 sizes[i] = (wh * fmt->depth[i]) / 8;
386 allocators[i] = fimc->alloc_ctx;
387 }
388 384
389 return 0; 385 return 0;
390} 386}
@@ -1300,6 +1296,7 @@ static int fimc_lite_subdev_registered(struct v4l2_subdev *sd)
1300 q->drv_priv = fimc; 1296 q->drv_priv = fimc;
1301 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC; 1297 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1302 q->lock = &fimc->lock; 1298 q->lock = &fimc->lock;
1299 q->dev = &fimc->pdev->dev;
1303 1300
1304 ret = vb2_queue_init(q); 1301 ret = vb2_queue_init(q);
1305 if (ret < 0) 1302 if (ret < 0)
@@ -1551,11 +1548,7 @@ static int fimc_lite_probe(struct platform_device *pdev)
1551 goto err_sd; 1548 goto err_sd;
1552 } 1549 }
1553 1550
1554 fimc->alloc_ctx = vb2_dma_contig_init_ctx(dev); 1551 vb2_dma_contig_set_max_seg_size(dev, DMA_BIT_MASK(32));
1555 if (IS_ERR(fimc->alloc_ctx)) {
1556 ret = PTR_ERR(fimc->alloc_ctx);
1557 goto err_clk_dis;
1558 }
1559 1552
1560 fimc_lite_set_default_config(fimc); 1553 fimc_lite_set_default_config(fimc);
1561 1554
@@ -1563,9 +1556,6 @@ static int fimc_lite_probe(struct platform_device *pdev)
1563 fimc->index); 1556 fimc->index);
1564 return 0; 1557 return 0;
1565 1558
1566err_clk_dis:
1567 if (!pm_runtime_enabled(dev))
1568 clk_disable(fimc->clock);
1569err_sd: 1559err_sd:
1570 fimc_lite_unregister_capture_subdev(fimc); 1560 fimc_lite_unregister_capture_subdev(fimc);
1571err_clk_put: 1561err_clk_put:
@@ -1651,7 +1641,7 @@ static int fimc_lite_remove(struct platform_device *pdev)
1651 pm_runtime_disable(dev); 1641 pm_runtime_disable(dev);
1652 pm_runtime_set_suspended(dev); 1642 pm_runtime_set_suspended(dev);
1653 fimc_lite_unregister_capture_subdev(fimc); 1643 fimc_lite_unregister_capture_subdev(fimc);
1654 vb2_dma_contig_cleanup_ctx(fimc->alloc_ctx); 1644 vb2_dma_contig_clear_max_seg_size(dev);
1655 fimc_lite_clk_put(fimc); 1645 fimc_lite_clk_put(fimc);
1656 1646
1657 dev_info(dev, "Driver unloaded\n"); 1647 dev_info(dev, "Driver unloaded\n");
diff --git a/drivers/media/platform/exynos4-is/fimc-lite.h b/drivers/media/platform/exynos4-is/fimc-lite.h
index 11690d563e06..9ae1e96a1bc7 100644
--- a/drivers/media/platform/exynos4-is/fimc-lite.h
+++ b/drivers/media/platform/exynos4-is/fimc-lite.h
@@ -113,7 +113,6 @@ struct flite_buffer {
113 * @ve: exynos video device entity structure 113 * @ve: exynos video device entity structure
114 * @v4l2_dev: pointer to top the level v4l2_device 114 * @v4l2_dev: pointer to top the level v4l2_device
115 * @fh: v4l2 file handle 115 * @fh: v4l2 file handle
116 * @alloc_ctx: videobuf2 memory allocator context
117 * @subdev: FIMC-LITE subdev 116 * @subdev: FIMC-LITE subdev
118 * @vd_pad: media (sink) pad for the capture video node 117 * @vd_pad: media (sink) pad for the capture video node
119 * @subdev_pads: the subdev media pads 118 * @subdev_pads: the subdev media pads
@@ -148,7 +147,6 @@ struct fimc_lite {
148 struct exynos_video_entity ve; 147 struct exynos_video_entity ve;
149 struct v4l2_device *v4l2_dev; 148 struct v4l2_device *v4l2_dev;
150 struct v4l2_fh fh; 149 struct v4l2_fh fh;
151 struct vb2_alloc_ctx *alloc_ctx;
152 struct v4l2_subdev subdev; 150 struct v4l2_subdev subdev;
153 struct media_pad vd_pad; 151 struct media_pad vd_pad;
154 struct media_pad subdev_pads[FLITE_SD_PADS_NUM]; 152 struct media_pad subdev_pads[FLITE_SD_PADS_NUM];
diff --git a/drivers/media/platform/exynos4-is/fimc-m2m.c b/drivers/media/platform/exynos4-is/fimc-m2m.c
index 55ec4c99d484..b1309e114edb 100644
--- a/drivers/media/platform/exynos4-is/fimc-m2m.c
+++ b/drivers/media/platform/exynos4-is/fimc-m2m.c
@@ -50,30 +50,28 @@ void fimc_m2m_job_finish(struct fimc_ctx *ctx, int vb_state)
50 src_vb = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx); 50 src_vb = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
51 dst_vb = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx); 51 dst_vb = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
52 52
53 if (src_vb && dst_vb) { 53 if (src_vb)
54 v4l2_m2m_buf_done(src_vb, vb_state); 54 v4l2_m2m_buf_done(src_vb, vb_state);
55 if (dst_vb)
55 v4l2_m2m_buf_done(dst_vb, vb_state); 56 v4l2_m2m_buf_done(dst_vb, vb_state);
57 if (src_vb && dst_vb)
56 v4l2_m2m_job_finish(ctx->fimc_dev->m2m.m2m_dev, 58 v4l2_m2m_job_finish(ctx->fimc_dev->m2m.m2m_dev,
57 ctx->fh.m2m_ctx); 59 ctx->fh.m2m_ctx);
58 }
59} 60}
60 61
61/* Complete the transaction which has been scheduled for execution. */ 62/* Complete the transaction which has been scheduled for execution. */
62static int fimc_m2m_shutdown(struct fimc_ctx *ctx) 63static void fimc_m2m_shutdown(struct fimc_ctx *ctx)
63{ 64{
64 struct fimc_dev *fimc = ctx->fimc_dev; 65 struct fimc_dev *fimc = ctx->fimc_dev;
65 int ret;
66 66
67 if (!fimc_m2m_pending(fimc)) 67 if (!fimc_m2m_pending(fimc))
68 return 0; 68 return;
69 69
70 fimc_ctx_state_set(FIMC_CTX_SHUT, ctx); 70 fimc_ctx_state_set(FIMC_CTX_SHUT, ctx);
71 71
72 ret = wait_event_timeout(fimc->irq_queue, 72 wait_event_timeout(fimc->irq_queue,
73 !fimc_ctx_state_is_set(FIMC_CTX_SHUT, ctx), 73 !fimc_ctx_state_is_set(FIMC_CTX_SHUT, ctx),
74 FIMC_SHUTDOWN_TIMEOUT); 74 FIMC_SHUTDOWN_TIMEOUT);
75
76 return ret == 0 ? -ETIMEDOUT : ret;
77} 75}
78 76
79static int start_streaming(struct vb2_queue *q, unsigned int count) 77static int start_streaming(struct vb2_queue *q, unsigned int count)
@@ -88,12 +86,10 @@ static int start_streaming(struct vb2_queue *q, unsigned int count)
88static void stop_streaming(struct vb2_queue *q) 86static void stop_streaming(struct vb2_queue *q)
89{ 87{
90 struct fimc_ctx *ctx = q->drv_priv; 88 struct fimc_ctx *ctx = q->drv_priv;
91 int ret;
92 89
93 ret = fimc_m2m_shutdown(ctx);
94 if (ret == -ETIMEDOUT)
95 fimc_m2m_job_finish(ctx, VB2_BUF_STATE_ERROR);
96 90
91 fimc_m2m_shutdown(ctx);
92 fimc_m2m_job_finish(ctx, VB2_BUF_STATE_ERROR);
97 pm_runtime_put(&ctx->fimc_dev->pdev->dev); 93 pm_runtime_put(&ctx->fimc_dev->pdev->dev);
98} 94}
99 95
@@ -178,7 +174,7 @@ static void fimc_job_abort(void *priv)
178 174
179static int fimc_queue_setup(struct vb2_queue *vq, 175static int fimc_queue_setup(struct vb2_queue *vq,
180 unsigned int *num_buffers, unsigned int *num_planes, 176 unsigned int *num_buffers, unsigned int *num_planes,
181 unsigned int sizes[], void *allocators[]) 177 unsigned int sizes[], struct device *alloc_devs[])
182{ 178{
183 struct fimc_ctx *ctx = vb2_get_drv_priv(vq); 179 struct fimc_ctx *ctx = vb2_get_drv_priv(vq);
184 struct fimc_frame *f; 180 struct fimc_frame *f;
@@ -195,10 +191,8 @@ static int fimc_queue_setup(struct vb2_queue *vq,
195 return -EINVAL; 191 return -EINVAL;
196 192
197 *num_planes = f->fmt->memplanes; 193 *num_planes = f->fmt->memplanes;
198 for (i = 0; i < f->fmt->memplanes; i++) { 194 for (i = 0; i < f->fmt->memplanes; i++)
199 sizes[i] = f->payload[i]; 195 sizes[i] = f->payload[i];
200 allocators[i] = ctx->fimc_dev->alloc_ctx;
201 }
202 return 0; 196 return 0;
203} 197}
204 198
@@ -562,6 +556,7 @@ static int queue_init(void *priv, struct vb2_queue *src_vq,
562 src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer); 556 src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
563 src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY; 557 src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
564 src_vq->lock = &ctx->fimc_dev->lock; 558 src_vq->lock = &ctx->fimc_dev->lock;
559 src_vq->dev = &ctx->fimc_dev->pdev->dev;
565 560
566 ret = vb2_queue_init(src_vq); 561 ret = vb2_queue_init(src_vq);
567 if (ret) 562 if (ret)
@@ -575,6 +570,7 @@ static int queue_init(void *priv, struct vb2_queue *src_vq,
575 dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer); 570 dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
576 dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY; 571 dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
577 dst_vq->lock = &ctx->fimc_dev->lock; 572 dst_vq->lock = &ctx->fimc_dev->lock;
573 dst_vq->dev = &ctx->fimc_dev->pdev->dev;
578 574
579 return vb2_queue_init(dst_vq); 575 return vb2_queue_init(dst_vq);
580} 576}
diff --git a/drivers/media/platform/exynos4-is/mipi-csis.c b/drivers/media/platform/exynos4-is/mipi-csis.c
index bf954424e7be..86e681daa89d 100644
--- a/drivers/media/platform/exynos4-is/mipi-csis.c
+++ b/drivers/media/platform/exynos4-is/mipi-csis.c
@@ -649,23 +649,6 @@ static int s5pcsis_log_status(struct v4l2_subdev *sd)
649 return 0; 649 return 0;
650} 650}
651 651
652static int s5pcsis_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
653{
654 struct v4l2_mbus_framefmt *format = v4l2_subdev_get_try_format(sd, fh->pad, 0);
655
656 format->colorspace = V4L2_COLORSPACE_JPEG;
657 format->code = s5pcsis_formats[0].code;
658 format->width = S5PCSIS_DEF_PIX_WIDTH;
659 format->height = S5PCSIS_DEF_PIX_HEIGHT;
660 format->field = V4L2_FIELD_NONE;
661
662 return 0;
663}
664
665static const struct v4l2_subdev_internal_ops s5pcsis_sd_internal_ops = {
666 .open = s5pcsis_open,
667};
668
669static struct v4l2_subdev_core_ops s5pcsis_core_ops = { 652static struct v4l2_subdev_core_ops s5pcsis_core_ops = {
670 .s_power = s5pcsis_s_power, 653 .s_power = s5pcsis_s_power,
671 .log_status = s5pcsis_log_status, 654 .log_status = s5pcsis_log_status,
diff --git a/drivers/media/platform/m2m-deinterlace.c b/drivers/media/platform/m2m-deinterlace.c
index 7383818c2be6..0fcb5c78031d 100644
--- a/drivers/media/platform/m2m-deinterlace.c
+++ b/drivers/media/platform/m2m-deinterlace.c
@@ -136,7 +136,6 @@ struct deinterlace_dev {
136 struct dma_chan *dma_chan; 136 struct dma_chan *dma_chan;
137 137
138 struct v4l2_m2m_dev *m2m_dev; 138 struct v4l2_m2m_dev *m2m_dev;
139 struct vb2_alloc_ctx *alloc_ctx;
140}; 139};
141 140
142struct deinterlace_ctx { 141struct deinterlace_ctx {
@@ -799,7 +798,7 @@ struct vb2_dc_conf {
799 798
800static int deinterlace_queue_setup(struct vb2_queue *vq, 799static int deinterlace_queue_setup(struct vb2_queue *vq,
801 unsigned int *nbuffers, unsigned int *nplanes, 800 unsigned int *nbuffers, unsigned int *nplanes,
802 unsigned int sizes[], void *alloc_ctxs[]) 801 unsigned int sizes[], struct device *alloc_devs[])
803{ 802{
804 struct deinterlace_ctx *ctx = vb2_get_drv_priv(vq); 803 struct deinterlace_ctx *ctx = vb2_get_drv_priv(vq);
805 struct deinterlace_q_data *q_data; 804 struct deinterlace_q_data *q_data;
@@ -820,8 +819,6 @@ static int deinterlace_queue_setup(struct vb2_queue *vq,
820 *nbuffers = count; 819 *nbuffers = count;
821 sizes[0] = size; 820 sizes[0] = size;
822 821
823 alloc_ctxs[0] = ctx->dev->alloc_ctx;
824
825 dprintk(ctx->dev, "get %d buffer(s) of size %d each.\n", count, size); 822 dprintk(ctx->dev, "get %d buffer(s) of size %d each.\n", count, size);
826 823
827 return 0; 824 return 0;
@@ -874,6 +871,7 @@ static int queue_init(void *priv, struct vb2_queue *src_vq,
874 src_vq->ops = &deinterlace_qops; 871 src_vq->ops = &deinterlace_qops;
875 src_vq->mem_ops = &vb2_dma_contig_memops; 872 src_vq->mem_ops = &vb2_dma_contig_memops;
876 src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY; 873 src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
874 src_vq->dev = ctx->dev->v4l2_dev.dev;
877 q_data[V4L2_M2M_SRC].fmt = &formats[0]; 875 q_data[V4L2_M2M_SRC].fmt = &formats[0];
878 q_data[V4L2_M2M_SRC].width = 640; 876 q_data[V4L2_M2M_SRC].width = 640;
879 q_data[V4L2_M2M_SRC].height = 480; 877 q_data[V4L2_M2M_SRC].height = 480;
@@ -891,6 +889,7 @@ static int queue_init(void *priv, struct vb2_queue *src_vq,
891 dst_vq->ops = &deinterlace_qops; 889 dst_vq->ops = &deinterlace_qops;
892 dst_vq->mem_ops = &vb2_dma_contig_memops; 890 dst_vq->mem_ops = &vb2_dma_contig_memops;
893 dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY; 891 dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
892 dst_vq->dev = ctx->dev->v4l2_dev.dev;
894 q_data[V4L2_M2M_DST].fmt = &formats[0]; 893 q_data[V4L2_M2M_DST].fmt = &formats[0];
895 q_data[V4L2_M2M_DST].width = 640; 894 q_data[V4L2_M2M_DST].width = 640;
896 q_data[V4L2_M2M_DST].height = 480; 895 q_data[V4L2_M2M_DST].height = 480;
@@ -1046,13 +1045,6 @@ static int deinterlace_probe(struct platform_device *pdev)
1046 1045
1047 platform_set_drvdata(pdev, pcdev); 1046 platform_set_drvdata(pdev, pcdev);
1048 1047
1049 pcdev->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
1050 if (IS_ERR(pcdev->alloc_ctx)) {
1051 v4l2_err(&pcdev->v4l2_dev, "Failed to alloc vb2 context\n");
1052 ret = PTR_ERR(pcdev->alloc_ctx);
1053 goto err_ctx;
1054 }
1055
1056 pcdev->m2m_dev = v4l2_m2m_init(&m2m_ops); 1048 pcdev->m2m_dev = v4l2_m2m_init(&m2m_ops);
1057 if (IS_ERR(pcdev->m2m_dev)) { 1049 if (IS_ERR(pcdev->m2m_dev)) {
1058 v4l2_err(&pcdev->v4l2_dev, "Failed to init mem2mem device\n"); 1050 v4l2_err(&pcdev->v4l2_dev, "Failed to init mem2mem device\n");
@@ -1064,8 +1056,6 @@ static int deinterlace_probe(struct platform_device *pdev)
1064 1056
1065err_m2m: 1057err_m2m:
1066 video_unregister_device(&pcdev->vfd); 1058 video_unregister_device(&pcdev->vfd);
1067err_ctx:
1068 vb2_dma_contig_cleanup_ctx(pcdev->alloc_ctx);
1069unreg_dev: 1059unreg_dev:
1070 v4l2_device_unregister(&pcdev->v4l2_dev); 1060 v4l2_device_unregister(&pcdev->v4l2_dev);
1071rel_dma: 1061rel_dma:
@@ -1082,7 +1072,6 @@ static int deinterlace_remove(struct platform_device *pdev)
1082 v4l2_m2m_release(pcdev->m2m_dev); 1072 v4l2_m2m_release(pcdev->m2m_dev);
1083 video_unregister_device(&pcdev->vfd); 1073 video_unregister_device(&pcdev->vfd);
1084 v4l2_device_unregister(&pcdev->v4l2_dev); 1074 v4l2_device_unregister(&pcdev->v4l2_dev);
1085 vb2_dma_contig_cleanup_ctx(pcdev->alloc_ctx);
1086 dma_release_channel(pcdev->dma_chan); 1075 dma_release_channel(pcdev->dma_chan);
1087 1076
1088 return 0; 1077 return 0;
diff --git a/drivers/media/platform/marvell-ccic/mcam-core.c b/drivers/media/platform/marvell-ccic/mcam-core.c
index 9b878deb1437..af59bf4dca2d 100644
--- a/drivers/media/platform/marvell-ccic/mcam-core.c
+++ b/drivers/media/platform/marvell-ccic/mcam-core.c
@@ -973,7 +973,7 @@ static int mcam_cam_set_flip(struct mcam_camera *cam)
973 memset(&ctrl, 0, sizeof(ctrl)); 973 memset(&ctrl, 0, sizeof(ctrl));
974 ctrl.id = V4L2_CID_VFLIP; 974 ctrl.id = V4L2_CID_VFLIP;
975 ctrl.value = flip; 975 ctrl.value = flip;
976 return sensor_call(cam, core, s_ctrl, &ctrl); 976 return v4l2_s_ctrl(NULL, cam->sensor->ctrl_handler, &ctrl);
977} 977}
978 978
979 979
@@ -1051,7 +1051,7 @@ static int mcam_read_setup(struct mcam_camera *cam)
1051static int mcam_vb_queue_setup(struct vb2_queue *vq, 1051static int mcam_vb_queue_setup(struct vb2_queue *vq,
1052 unsigned int *nbufs, 1052 unsigned int *nbufs,
1053 unsigned int *num_planes, unsigned int sizes[], 1053 unsigned int *num_planes, unsigned int sizes[],
1054 void *alloc_ctxs[]) 1054 struct device *alloc_devs[])
1055{ 1055{
1056 struct mcam_camera *cam = vb2_get_drv_priv(vq); 1056 struct mcam_camera *cam = vb2_get_drv_priv(vq);
1057 int minbufs = (cam->buffer_mode == B_DMA_contig) ? 3 : 2; 1057 int minbufs = (cam->buffer_mode == B_DMA_contig) ? 3 : 2;
@@ -1059,10 +1059,6 @@ static int mcam_vb_queue_setup(struct vb2_queue *vq,
1059 1059
1060 if (*nbufs < minbufs) 1060 if (*nbufs < minbufs)
1061 *nbufs = minbufs; 1061 *nbufs = minbufs;
1062 if (cam->buffer_mode == B_DMA_contig)
1063 alloc_ctxs[0] = cam->vb_alloc_ctx;
1064 else if (cam->buffer_mode == B_DMA_sg)
1065 alloc_ctxs[0] = cam->vb_alloc_ctx_sg;
1066 1062
1067 if (*num_planes) 1063 if (*num_planes)
1068 return sizes[0] < size ? -EINVAL : 0; 1064 return sizes[0] < size ? -EINVAL : 0;
@@ -1271,6 +1267,7 @@ static int mcam_setup_vb2(struct mcam_camera *cam)
1271 vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC; 1267 vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1272 vq->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF | VB2_READ; 1268 vq->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF | VB2_READ;
1273 vq->buf_struct_size = sizeof(struct mcam_vb_buffer); 1269 vq->buf_struct_size = sizeof(struct mcam_vb_buffer);
1270 vq->dev = cam->dev;
1274 INIT_LIST_HEAD(&cam->buffers); 1271 INIT_LIST_HEAD(&cam->buffers);
1275 switch (cam->buffer_mode) { 1272 switch (cam->buffer_mode) {
1276 case B_DMA_contig: 1273 case B_DMA_contig:
@@ -1279,9 +1276,6 @@ static int mcam_setup_vb2(struct mcam_camera *cam)
1279 vq->mem_ops = &vb2_dma_contig_memops; 1276 vq->mem_ops = &vb2_dma_contig_memops;
1280 cam->dma_setup = mcam_ctlr_dma_contig; 1277 cam->dma_setup = mcam_ctlr_dma_contig;
1281 cam->frame_complete = mcam_dma_contig_done; 1278 cam->frame_complete = mcam_dma_contig_done;
1282 cam->vb_alloc_ctx = vb2_dma_contig_init_ctx(cam->dev);
1283 if (IS_ERR(cam->vb_alloc_ctx))
1284 return PTR_ERR(cam->vb_alloc_ctx);
1285#endif 1279#endif
1286 break; 1280 break;
1287 case B_DMA_sg: 1281 case B_DMA_sg:
@@ -1290,9 +1284,6 @@ static int mcam_setup_vb2(struct mcam_camera *cam)
1290 vq->mem_ops = &vb2_dma_sg_memops; 1284 vq->mem_ops = &vb2_dma_sg_memops;
1291 cam->dma_setup = mcam_ctlr_dma_sg; 1285 cam->dma_setup = mcam_ctlr_dma_sg;
1292 cam->frame_complete = mcam_dma_sg_done; 1286 cam->frame_complete = mcam_dma_sg_done;
1293 cam->vb_alloc_ctx_sg = vb2_dma_sg_init_ctx(cam->dev);
1294 if (IS_ERR(cam->vb_alloc_ctx_sg))
1295 return PTR_ERR(cam->vb_alloc_ctx_sg);
1296#endif 1287#endif
1297 break; 1288 break;
1298 case B_vmalloc: 1289 case B_vmalloc:
@@ -1309,18 +1300,6 @@ static int mcam_setup_vb2(struct mcam_camera *cam)
1309 return vb2_queue_init(vq); 1300 return vb2_queue_init(vq);
1310} 1301}
1311 1302
1312static void mcam_cleanup_vb2(struct mcam_camera *cam)
1313{
1314#ifdef MCAM_MODE_DMA_CONTIG
1315 if (cam->buffer_mode == B_DMA_contig)
1316 vb2_dma_contig_cleanup_ctx(cam->vb_alloc_ctx);
1317#endif
1318#ifdef MCAM_MODE_DMA_SG
1319 if (cam->buffer_mode == B_DMA_sg)
1320 vb2_dma_sg_cleanup_ctx(cam->vb_alloc_ctx_sg);
1321#endif
1322}
1323
1324 1303
1325/* ---------------------------------------------------------------------- */ 1304/* ---------------------------------------------------------------------- */
1326/* 1305/*
@@ -1875,7 +1854,6 @@ void mccic_shutdown(struct mcam_camera *cam)
1875 cam_warn(cam, "Removing a device with users!\n"); 1854 cam_warn(cam, "Removing a device with users!\n");
1876 mcam_ctlr_power_down(cam); 1855 mcam_ctlr_power_down(cam);
1877 } 1856 }
1878 mcam_cleanup_vb2(cam);
1879 if (cam->buffer_mode == B_vmalloc) 1857 if (cam->buffer_mode == B_vmalloc)
1880 mcam_free_dma_bufs(cam); 1858 mcam_free_dma_bufs(cam);
1881 video_unregister_device(&cam->vdev); 1859 video_unregister_device(&cam->vdev);
diff --git a/drivers/media/platform/marvell-ccic/mcam-core.h b/drivers/media/platform/marvell-ccic/mcam-core.h
index 35cd9e5aedf8..beb339f5561f 100644
--- a/drivers/media/platform/marvell-ccic/mcam-core.h
+++ b/drivers/media/platform/marvell-ccic/mcam-core.h
@@ -176,8 +176,6 @@ struct mcam_camera {
176 176
177 /* DMA buffers - DMA modes */ 177 /* DMA buffers - DMA modes */
178 struct mcam_vb_buffer *vb_bufs[MAX_DMA_BUFS]; 178 struct mcam_vb_buffer *vb_bufs[MAX_DMA_BUFS];
179 struct vb2_alloc_ctx *vb_alloc_ctx;
180 struct vb2_alloc_ctx *vb_alloc_ctx_sg;
181 179
182 /* Mode-specific ops, set at open time */ 180 /* Mode-specific ops, set at open time */
183 void (*dma_setup)(struct mcam_camera *cam); 181 void (*dma_setup)(struct mcam_camera *cam);
diff --git a/drivers/media/platform/mtk-vcodec/Makefile b/drivers/media/platform/mtk-vcodec/Makefile
new file mode 100644
index 000000000000..dc5cb006d600
--- /dev/null
+++ b/drivers/media/platform/mtk-vcodec/Makefile
@@ -0,0 +1,19 @@
1
2
3obj-$(CONFIG_VIDEO_MEDIATEK_VCODEC) += mtk-vcodec-enc.o mtk-vcodec-common.o
4
5
6
7mtk-vcodec-enc-y := venc/venc_vp8_if.o \
8 venc/venc_h264_if.o \
9 mtk_vcodec_enc.o \
10 mtk_vcodec_enc_drv.o \
11 mtk_vcodec_enc_pm.o \
12 venc_drv_if.o \
13 venc_vpu_if.o \
14
15
16mtk-vcodec-common-y := mtk_vcodec_intr.o \
17 mtk_vcodec_util.o\
18
19ccflags-y += -I$(srctree)/drivers/media/platform/mtk-vpu
diff --git a/drivers/media/platform/mtk-vcodec/mtk_vcodec_drv.h b/drivers/media/platform/mtk-vcodec/mtk_vcodec_drv.h
new file mode 100644
index 000000000000..94f0a425be42
--- /dev/null
+++ b/drivers/media/platform/mtk-vcodec/mtk_vcodec_drv.h
@@ -0,0 +1,335 @@
1/*
2* Copyright (c) 2016 MediaTek Inc.
3* Author: PC Chen <pc.chen@mediatek.com>
4* Tiffany Lin <tiffany.lin@mediatek.com>
5*
6* This program is free software; you can redistribute it and/or modify
7* it under the terms of the GNU General Public License version 2 as
8* published by the Free Software Foundation.
9*
10* This program is distributed in the hope that it will be useful,
11* but WITHOUT ANY WARRANTY; without even the implied warranty of
12* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13* GNU General Public License for more details.
14*/
15
16#ifndef _MTK_VCODEC_DRV_H_
17#define _MTK_VCODEC_DRV_H_
18
19#include <linux/platform_device.h>
20#include <linux/videodev2.h>
21#include <media/v4l2-ctrls.h>
22#include <media/v4l2-device.h>
23#include <media/v4l2-ioctl.h>
24#include <media/videobuf2-core.h>
25
26#include "mtk_vcodec_util.h"
27
28#define MTK_VCODEC_DRV_NAME "mtk_vcodec_drv"
29#define MTK_VCODEC_ENC_NAME "mtk-vcodec-enc"
30#define MTK_PLATFORM_STR "platform:mt8173"
31
32
33#define MTK_VCODEC_MAX_PLANES 3
34#define MTK_V4L2_BENCHMARK 0
35#define WAIT_INTR_TIMEOUT_MS 1000
36
37/**
38 * enum mtk_hw_reg_idx - MTK hw register base index
39 */
40enum mtk_hw_reg_idx {
41 VDEC_SYS,
42 VDEC_MISC,
43 VDEC_LD,
44 VDEC_TOP,
45 VDEC_CM,
46 VDEC_AD,
47 VDEC_AV,
48 VDEC_PP,
49 VDEC_HWD,
50 VDEC_HWQ,
51 VDEC_HWB,
52 VDEC_HWG,
53 NUM_MAX_VDEC_REG_BASE,
54 /* h264 encoder */
55 VENC_SYS = NUM_MAX_VDEC_REG_BASE,
56 /* vp8 encoder */
57 VENC_LT_SYS,
58 NUM_MAX_VCODEC_REG_BASE
59};
60
61/**
62 * enum mtk_instance_type - The type of an MTK Vcodec instance.
63 */
64enum mtk_instance_type {
65 MTK_INST_DECODER = 0,
66 MTK_INST_ENCODER = 1,
67};
68
69/**
70 * enum mtk_instance_state - The state of an MTK Vcodec instance.
71 * @MTK_STATE_FREE - default state when instance is created
72 * @MTK_STATE_INIT - vcodec instance is initialized
73 * @MTK_STATE_HEADER - vdec had sps/pps header parsed or venc
74 * had sps/pps header encoded
75 * @MTK_STATE_FLUSH - vdec is flushing. Only used by decoder
76 * @MTK_STATE_ABORT - vcodec should be aborted
77 */
78enum mtk_instance_state {
79 MTK_STATE_FREE = 0,
80 MTK_STATE_INIT = 1,
81 MTK_STATE_HEADER = 2,
82 MTK_STATE_FLUSH = 3,
83 MTK_STATE_ABORT = 4,
84};
85
86/**
87 * struct mtk_encode_param - General encoding parameters type
88 */
89enum mtk_encode_param {
90 MTK_ENCODE_PARAM_NONE = 0,
91 MTK_ENCODE_PARAM_BITRATE = (1 << 0),
92 MTK_ENCODE_PARAM_FRAMERATE = (1 << 1),
93 MTK_ENCODE_PARAM_INTRA_PERIOD = (1 << 2),
94 MTK_ENCODE_PARAM_FORCE_INTRA = (1 << 3),
95 MTK_ENCODE_PARAM_GOP_SIZE = (1 << 4),
96};
97
98enum mtk_fmt_type {
99 MTK_FMT_DEC = 0,
100 MTK_FMT_ENC = 1,
101 MTK_FMT_FRAME = 2,
102};
103
104/**
105 * struct mtk_video_fmt - Structure used to store information about pixelformats
106 */
107struct mtk_video_fmt {
108 u32 fourcc;
109 enum mtk_fmt_type type;
110 u32 num_planes;
111};
112
113/**
114 * struct mtk_codec_framesizes - Structure used to store information about
115 * framesizes
116 */
117struct mtk_codec_framesizes {
118 u32 fourcc;
119 struct v4l2_frmsize_stepwise stepwise;
120};
121
122/**
123 * struct mtk_q_type - Type of queue
124 */
125enum mtk_q_type {
126 MTK_Q_DATA_SRC = 0,
127 MTK_Q_DATA_DST = 1,
128};
129
130/**
131 * struct mtk_q_data - Structure used to store information about queue
132 */
133struct mtk_q_data {
134 unsigned int visible_width;
135 unsigned int visible_height;
136 unsigned int coded_width;
137 unsigned int coded_height;
138 enum v4l2_field field;
139 unsigned int bytesperline[MTK_VCODEC_MAX_PLANES];
140 unsigned int sizeimage[MTK_VCODEC_MAX_PLANES];
141 struct mtk_video_fmt *fmt;
142};
143
144/**
145 * struct mtk_enc_params - General encoding parameters
146 * @bitrate: target bitrate in bits per second
147 * @num_b_frame: number of b frames between p-frame
148 * @rc_frame: frame based rate control
149 * @rc_mb: macroblock based rate control
150 * @seq_hdr_mode: H.264 sequence header is encoded separately or joined
151 * with the first frame
152 * @intra_period: I frame period
153 * @gop_size: group of picture size, it's used as the intra frame period
154 * @framerate_num: frame rate numerator. ex: framerate_num=30 and
155 * framerate_denom=1 menas FPS is 30
156 * @framerate_denom: frame rate denominator. ex: framerate_num=30 and
157 * framerate_denom=1 menas FPS is 30
158 * @h264_max_qp: Max value for H.264 quantization parameter
159 * @h264_profile: V4L2 defined H.264 profile
160 * @h264_level: V4L2 defined H.264 level
161 * @force_intra: force/insert intra frame
162 */
163struct mtk_enc_params {
164 unsigned int bitrate;
165 unsigned int num_b_frame;
166 unsigned int rc_frame;
167 unsigned int rc_mb;
168 unsigned int seq_hdr_mode;
169 unsigned int intra_period;
170 unsigned int gop_size;
171 unsigned int framerate_num;
172 unsigned int framerate_denom;
173 unsigned int h264_max_qp;
174 unsigned int h264_profile;
175 unsigned int h264_level;
176 unsigned int force_intra;
177};
178
179/**
180 * struct mtk_vcodec_pm - Power management data structure
181 */
182struct mtk_vcodec_pm {
183 struct clk *vcodecpll;
184 struct clk *univpll_d2;
185 struct clk *clk_cci400_sel;
186 struct clk *vdecpll;
187 struct clk *vdec_sel;
188 struct clk *vencpll_d2;
189 struct clk *venc_sel;
190 struct clk *univpll1_d2;
191 struct clk *venc_lt_sel;
192 struct device *larbvdec;
193 struct device *larbvenc;
194 struct device *larbvenclt;
195 struct device *dev;
196 struct mtk_vcodec_dev *mtkdev;
197};
198
199/**
200 * struct mtk_vcodec_ctx - Context (instance) private data.
201 *
202 * @type: type of the instance - decoder or encoder
203 * @dev: pointer to the mtk_vcodec_dev of the device
204 * @list: link to ctx_list of mtk_vcodec_dev
205 * @fh: struct v4l2_fh
206 * @m2m_ctx: pointer to the v4l2_m2m_ctx of the context
207 * @q_data: store information of input and output queue
208 * of the context
209 * @id: index of the context that this structure describes
210 * @state: state of the context
211 * @param_change: indicate encode parameter type
212 * @enc_params: encoding parameters
213 * @enc_if: hoooked encoder driver interface
214 * @drv_handle: driver handle for specific decode/encode instance
215 *
216 * @int_cond: variable used by the waitqueue
217 * @int_type: type of the last interrupt
218 * @queue: waitqueue that can be used to wait for this context to
219 * finish
220 * @irq_status: irq status
221 *
222 * @ctrl_hdl: handler for v4l2 framework
223 * @encode_work: worker for the encoding
224 *
225 * @colorspace: enum v4l2_colorspace; supplemental to pixelformat
226 * @ycbcr_enc: enum v4l2_ycbcr_encoding, Y'CbCr encoding
227 * @quantization: enum v4l2_quantization, colorspace quantization
228 * @xfer_func: enum v4l2_xfer_func, colorspace transfer function
229 */
230struct mtk_vcodec_ctx {
231 enum mtk_instance_type type;
232 struct mtk_vcodec_dev *dev;
233 struct list_head list;
234
235 struct v4l2_fh fh;
236 struct v4l2_m2m_ctx *m2m_ctx;
237 struct mtk_q_data q_data[2];
238 int id;
239 enum mtk_instance_state state;
240 enum mtk_encode_param param_change;
241 struct mtk_enc_params enc_params;
242
243 struct venc_common_if *enc_if;
244 unsigned long drv_handle;
245
246 int int_cond;
247 int int_type;
248 wait_queue_head_t queue;
249 unsigned int irq_status;
250
251 struct v4l2_ctrl_handler ctrl_hdl;
252 struct work_struct encode_work;
253
254 enum v4l2_colorspace colorspace;
255 enum v4l2_ycbcr_encoding ycbcr_enc;
256 enum v4l2_quantization quantization;
257 enum v4l2_xfer_func xfer_func;
258};
259
260/**
261 * struct mtk_vcodec_dev - driver data
262 * @v4l2_dev: V4L2 device to register video devices for.
263 * @vfd_enc: Video device for encoder.
264 *
265 * @m2m_dev_enc: m2m device for encoder.
266 * @plat_dev: platform device
267 * @vpu_plat_dev: mtk vpu platform device
268 * @ctx_list: list of struct mtk_vcodec_ctx
269 * @irqlock: protect data access by irq handler and work thread
270 * @curr_ctx: The context that is waiting for codec hardware
271 *
272 * @reg_base: Mapped address of MTK Vcodec registers.
273 *
274 * @id_counter: used to identify current opened instance
275 * @num_instances: counter of active MTK Vcodec instances
276 *
277 * @encode_workqueue: encode work queue
278 *
279 * @int_cond: used to identify interrupt condition happen
280 * @int_type: used to identify what kind of interrupt condition happen
281 * @dev_mutex: video_device lock
282 * @queue: waitqueue for waiting for completion of device commands
283 *
284 * @enc_irq: h264 encoder irq resource
285 * @enc_lt_irq: vp8 encoder irq resource
286 *
287 * @enc_mutex: encoder hardware lock.
288 *
289 * @pm: power management control
290 * @dec_capability: used to identify decode capability, ex: 4k
291 * @enc_capability: used to identify encode capability
292 */
293struct mtk_vcodec_dev {
294 struct v4l2_device v4l2_dev;
295 struct video_device *vfd_enc;
296
297 struct v4l2_m2m_dev *m2m_dev_enc;
298 struct platform_device *plat_dev;
299 struct platform_device *vpu_plat_dev;
300 struct list_head ctx_list;
301 spinlock_t irqlock;
302 struct mtk_vcodec_ctx *curr_ctx;
303 void __iomem *reg_base[NUM_MAX_VCODEC_REG_BASE];
304
305 unsigned long id_counter;
306 int num_instances;
307
308 struct workqueue_struct *encode_workqueue;
309
310 int int_cond;
311 int int_type;
312 struct mutex dev_mutex;
313 wait_queue_head_t queue;
314
315 int enc_irq;
316 int enc_lt_irq;
317
318 struct mutex enc_mutex;
319
320 struct mtk_vcodec_pm pm;
321 unsigned int dec_capability;
322 unsigned int enc_capability;
323};
324
325static inline struct mtk_vcodec_ctx *fh_to_ctx(struct v4l2_fh *fh)
326{
327 return container_of(fh, struct mtk_vcodec_ctx, fh);
328}
329
330static inline struct mtk_vcodec_ctx *ctrl_to_ctx(struct v4l2_ctrl *ctrl)
331{
332 return container_of(ctrl->handler, struct mtk_vcodec_ctx, ctrl_hdl);
333}
334
335#endif /* _MTK_VCODEC_DRV_H_ */
diff --git a/drivers/media/platform/mtk-vcodec/mtk_vcodec_enc.c b/drivers/media/platform/mtk-vcodec/mtk_vcodec_enc.c
new file mode 100644
index 000000000000..3ed3f2d31df5
--- /dev/null
+++ b/drivers/media/platform/mtk-vcodec/mtk_vcodec_enc.c
@@ -0,0 +1,1292 @@
1/*
2* Copyright (c) 2016 MediaTek Inc.
3* Author: PC Chen <pc.chen@mediatek.com>
4* Tiffany Lin <tiffany.lin@mediatek.com>
5*
6* This program is free software; you can redistribute it and/or modify
7* it under the terms of the GNU General Public License version 2 as
8* published by the Free Software Foundation.
9*
10* This program is distributed in the hope that it will be useful,
11* but WITHOUT ANY WARRANTY; without even the implied warranty of
12* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13* GNU General Public License for more details.
14*/
15
16#include <media/v4l2-event.h>
17#include <media/v4l2-mem2mem.h>
18#include <media/videobuf2-dma-contig.h>
19#include <soc/mediatek/smi.h>
20
21#include "mtk_vcodec_drv.h"
22#include "mtk_vcodec_enc.h"
23#include "mtk_vcodec_intr.h"
24#include "mtk_vcodec_util.h"
25#include "venc_drv_if.h"
26
27#define MTK_VENC_MIN_W 160U
28#define MTK_VENC_MIN_H 128U
29#define MTK_VENC_MAX_W 1920U
30#define MTK_VENC_MAX_H 1088U
31#define DFT_CFG_WIDTH MTK_VENC_MIN_W
32#define DFT_CFG_HEIGHT MTK_VENC_MIN_H
33#define MTK_MAX_CTRLS_HINT 20
34#define OUT_FMT_IDX 0
35#define CAP_FMT_IDX 4
36
37
38static void mtk_venc_worker(struct work_struct *work);
39
40static struct mtk_video_fmt mtk_video_formats[] = {
41 {
42 .fourcc = V4L2_PIX_FMT_NV12M,
43 .type = MTK_FMT_FRAME,
44 .num_planes = 2,
45 },
46 {
47 .fourcc = V4L2_PIX_FMT_NV21M,
48 .type = MTK_FMT_FRAME,
49 .num_planes = 2,
50 },
51 {
52 .fourcc = V4L2_PIX_FMT_YUV420M,
53 .type = MTK_FMT_FRAME,
54 .num_planes = 3,
55 },
56 {
57 .fourcc = V4L2_PIX_FMT_YVU420M,
58 .type = MTK_FMT_FRAME,
59 .num_planes = 3,
60 },
61 {
62 .fourcc = V4L2_PIX_FMT_H264,
63 .type = MTK_FMT_ENC,
64 .num_planes = 1,
65 },
66 {
67 .fourcc = V4L2_PIX_FMT_VP8,
68 .type = MTK_FMT_ENC,
69 .num_planes = 1,
70 },
71};
72
73#define NUM_FORMATS ARRAY_SIZE(mtk_video_formats)
74
75static const struct mtk_codec_framesizes mtk_venc_framesizes[] = {
76 {
77 .fourcc = V4L2_PIX_FMT_H264,
78 .stepwise = { MTK_VENC_MIN_W, MTK_VENC_MAX_W, 16,
79 MTK_VENC_MIN_H, MTK_VENC_MAX_H, 16 },
80 },
81 {
82 .fourcc = V4L2_PIX_FMT_VP8,
83 .stepwise = { MTK_VENC_MIN_W, MTK_VENC_MAX_W, 16,
84 MTK_VENC_MIN_H, MTK_VENC_MAX_H, 16 },
85 },
86};
87
88#define NUM_SUPPORTED_FRAMESIZE ARRAY_SIZE(mtk_venc_framesizes)
89
90static int vidioc_venc_s_ctrl(struct v4l2_ctrl *ctrl)
91{
92 struct mtk_vcodec_ctx *ctx = ctrl_to_ctx(ctrl);
93 struct mtk_enc_params *p = &ctx->enc_params;
94 int ret = 0;
95
96 switch (ctrl->id) {
97 case V4L2_CID_MPEG_VIDEO_BITRATE:
98 mtk_v4l2_debug(2, "V4L2_CID_MPEG_VIDEO_BITRATE val = %d",
99 ctrl->val);
100 p->bitrate = ctrl->val;
101 ctx->param_change |= MTK_ENCODE_PARAM_BITRATE;
102 break;
103 case V4L2_CID_MPEG_VIDEO_B_FRAMES:
104 mtk_v4l2_debug(2, "V4L2_CID_MPEG_VIDEO_B_FRAMES val = %d",
105 ctrl->val);
106 p->num_b_frame = ctrl->val;
107 break;
108 case V4L2_CID_MPEG_VIDEO_FRAME_RC_ENABLE:
109 mtk_v4l2_debug(2, "V4L2_CID_MPEG_VIDEO_FRAME_RC_ENABLE val = %d",
110 ctrl->val);
111 p->rc_frame = ctrl->val;
112 break;
113 case V4L2_CID_MPEG_VIDEO_H264_MAX_QP:
114 mtk_v4l2_debug(2, "V4L2_CID_MPEG_VIDEO_H264_MAX_QP val = %d",
115 ctrl->val);
116 p->h264_max_qp = ctrl->val;
117 break;
118 case V4L2_CID_MPEG_VIDEO_HEADER_MODE:
119 mtk_v4l2_debug(2, "V4L2_CID_MPEG_VIDEO_HEADER_MODE val = %d",
120 ctrl->val);
121 p->seq_hdr_mode = ctrl->val;
122 break;
123 case V4L2_CID_MPEG_VIDEO_MB_RC_ENABLE:
124 mtk_v4l2_debug(2, "V4L2_CID_MPEG_VIDEO_MB_RC_ENABLE val = %d",
125 ctrl->val);
126 p->rc_mb = ctrl->val;
127 break;
128 case V4L2_CID_MPEG_VIDEO_H264_PROFILE:
129 mtk_v4l2_debug(2, "V4L2_CID_MPEG_VIDEO_H264_PROFILE val = %d",
130 ctrl->val);
131 p->h264_profile = ctrl->val;
132 break;
133 case V4L2_CID_MPEG_VIDEO_H264_LEVEL:
134 mtk_v4l2_debug(2, "V4L2_CID_MPEG_VIDEO_H264_LEVEL val = %d",
135 ctrl->val);
136 p->h264_level = ctrl->val;
137 break;
138 case V4L2_CID_MPEG_VIDEO_H264_I_PERIOD:
139 mtk_v4l2_debug(2, "V4L2_CID_MPEG_VIDEO_H264_I_PERIOD val = %d",
140 ctrl->val);
141 p->intra_period = ctrl->val;
142 ctx->param_change |= MTK_ENCODE_PARAM_INTRA_PERIOD;
143 break;
144 case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
145 mtk_v4l2_debug(2, "V4L2_CID_MPEG_VIDEO_GOP_SIZE val = %d",
146 ctrl->val);
147 p->gop_size = ctrl->val;
148 ctx->param_change |= MTK_ENCODE_PARAM_GOP_SIZE;
149 break;
150 case V4L2_CID_MPEG_VIDEO_FORCE_KEY_FRAME:
151 mtk_v4l2_debug(2, "V4L2_CID_MPEG_VIDEO_FORCE_KEY_FRAME");
152 p->force_intra = 1;
153 ctx->param_change |= MTK_ENCODE_PARAM_FORCE_INTRA;
154 break;
155 default:
156 ret = -EINVAL;
157 break;
158 }
159
160 return ret;
161}
162
163static const struct v4l2_ctrl_ops mtk_vcodec_enc_ctrl_ops = {
164 .s_ctrl = vidioc_venc_s_ctrl,
165};
166
167static int vidioc_enum_fmt(struct v4l2_fmtdesc *f, bool output_queue)
168{
169 struct mtk_video_fmt *fmt;
170 int i, j = 0;
171
172 for (i = 0; i < NUM_FORMATS; ++i) {
173 if (output_queue && mtk_video_formats[i].type != MTK_FMT_FRAME)
174 continue;
175 if (!output_queue && mtk_video_formats[i].type != MTK_FMT_ENC)
176 continue;
177
178 if (j == f->index) {
179 fmt = &mtk_video_formats[i];
180 f->pixelformat = fmt->fourcc;
181 memset(f->reserved, 0, sizeof(f->reserved));
182 return 0;
183 }
184 ++j;
185 }
186
187 return -EINVAL;
188}
189
190static int vidioc_enum_framesizes(struct file *file, void *fh,
191 struct v4l2_frmsizeenum *fsize)
192{
193 int i = 0;
194
195 if (fsize->index != 0)
196 return -EINVAL;
197
198 for (i = 0; i < NUM_SUPPORTED_FRAMESIZE; ++i) {
199 if (fsize->pixel_format != mtk_venc_framesizes[i].fourcc)
200 continue;
201
202 fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
203 fsize->stepwise = mtk_venc_framesizes[i].stepwise;
204 return 0;
205 }
206
207 return -EINVAL;
208}
209
210static int vidioc_enum_fmt_vid_cap_mplane(struct file *file, void *pirv,
211 struct v4l2_fmtdesc *f)
212{
213 return vidioc_enum_fmt(f, false);
214}
215
216static int vidioc_enum_fmt_vid_out_mplane(struct file *file, void *prov,
217 struct v4l2_fmtdesc *f)
218{
219 return vidioc_enum_fmt(f, true);
220}
221
222static int vidioc_venc_querycap(struct file *file, void *priv,
223 struct v4l2_capability *cap)
224{
225 strlcpy(cap->driver, MTK_VCODEC_ENC_NAME, sizeof(cap->driver));
226 strlcpy(cap->bus_info, MTK_PLATFORM_STR, sizeof(cap->bus_info));
227 strlcpy(cap->card, MTK_PLATFORM_STR, sizeof(cap->card));
228
229 return 0;
230}
231
232static int vidioc_venc_s_parm(struct file *file, void *priv,
233 struct v4l2_streamparm *a)
234{
235 struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
236
237 if (a->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
238 return -EINVAL;
239
240 ctx->enc_params.framerate_num =
241 a->parm.output.timeperframe.denominator;
242 ctx->enc_params.framerate_denom =
243 a->parm.output.timeperframe.numerator;
244 ctx->param_change |= MTK_ENCODE_PARAM_FRAMERATE;
245
246 return 0;
247}
248
249static int vidioc_venc_g_parm(struct file *file, void *priv,
250 struct v4l2_streamparm *a)
251{
252 struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
253
254 if (a->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
255 return -EINVAL;
256
257 a->parm.output.timeperframe.denominator =
258 ctx->enc_params.framerate_num;
259 a->parm.output.timeperframe.numerator =
260 ctx->enc_params.framerate_denom;
261
262 return 0;
263}
264
265static struct mtk_q_data *mtk_venc_get_q_data(struct mtk_vcodec_ctx *ctx,
266 enum v4l2_buf_type type)
267{
268 if (V4L2_TYPE_IS_OUTPUT(type))
269 return &ctx->q_data[MTK_Q_DATA_SRC];
270
271 return &ctx->q_data[MTK_Q_DATA_DST];
272}
273
274static struct mtk_video_fmt *mtk_venc_find_format(struct v4l2_format *f)
275{
276 struct mtk_video_fmt *fmt;
277 unsigned int k;
278
279 for (k = 0; k < NUM_FORMATS; k++) {
280 fmt = &mtk_video_formats[k];
281 if (fmt->fourcc == f->fmt.pix.pixelformat)
282 return fmt;
283 }
284
285 return NULL;
286}
287
288/* V4L2 specification suggests the driver corrects the format struct if any of
289 * the dimensions is unsupported
290 */
291static int vidioc_try_fmt(struct v4l2_format *f, struct mtk_video_fmt *fmt)
292{
293 struct v4l2_pix_format_mplane *pix_fmt_mp = &f->fmt.pix_mp;
294 int i;
295
296 pix_fmt_mp->field = V4L2_FIELD_NONE;
297
298 if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
299 pix_fmt_mp->num_planes = 1;
300 pix_fmt_mp->plane_fmt[0].bytesperline = 0;
301 } else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
302 int tmp_w, tmp_h;
303
304 pix_fmt_mp->height = clamp(pix_fmt_mp->height,
305 MTK_VENC_MIN_H,
306 MTK_VENC_MAX_H);
307 pix_fmt_mp->width = clamp(pix_fmt_mp->width,
308 MTK_VENC_MIN_W,
309 MTK_VENC_MAX_W);
310
311 /* find next closer width align 16, heign align 32, size align
312 * 64 rectangle
313 */
314 tmp_w = pix_fmt_mp->width;
315 tmp_h = pix_fmt_mp->height;
316 v4l_bound_align_image(&pix_fmt_mp->width,
317 MTK_VENC_MIN_W,
318 MTK_VENC_MAX_W, 4,
319 &pix_fmt_mp->height,
320 MTK_VENC_MIN_H,
321 MTK_VENC_MAX_H, 5, 6);
322
323 if (pix_fmt_mp->width < tmp_w &&
324 (pix_fmt_mp->width + 16) <= MTK_VENC_MAX_W)
325 pix_fmt_mp->width += 16;
326 if (pix_fmt_mp->height < tmp_h &&
327 (pix_fmt_mp->height + 32) <= MTK_VENC_MAX_H)
328 pix_fmt_mp->height += 32;
329
330 mtk_v4l2_debug(0,
331 "before resize width=%d, height=%d, after resize width=%d, height=%d, sizeimage=%d %d",
332 tmp_w, tmp_h, pix_fmt_mp->width,
333 pix_fmt_mp->height,
334 pix_fmt_mp->plane_fmt[0].sizeimage,
335 pix_fmt_mp->plane_fmt[1].sizeimage);
336
337 pix_fmt_mp->num_planes = fmt->num_planes;
338 pix_fmt_mp->plane_fmt[0].sizeimage =
339 pix_fmt_mp->width * pix_fmt_mp->height +
340 ((ALIGN(pix_fmt_mp->width, 16) * 2) * 16);
341 pix_fmt_mp->plane_fmt[0].bytesperline = pix_fmt_mp->width;
342
343 if (pix_fmt_mp->num_planes == 2) {
344 pix_fmt_mp->plane_fmt[1].sizeimage =
345 (pix_fmt_mp->width * pix_fmt_mp->height) / 2 +
346 (ALIGN(pix_fmt_mp->width, 16) * 16);
347 pix_fmt_mp->plane_fmt[2].sizeimage = 0;
348 pix_fmt_mp->plane_fmt[1].bytesperline =
349 pix_fmt_mp->width;
350 pix_fmt_mp->plane_fmt[2].bytesperline = 0;
351 } else if (pix_fmt_mp->num_planes == 3) {
352 pix_fmt_mp->plane_fmt[1].sizeimage =
353 pix_fmt_mp->plane_fmt[2].sizeimage =
354 (pix_fmt_mp->width * pix_fmt_mp->height) / 4 +
355 ((ALIGN(pix_fmt_mp->width, 16) / 2) * 16);
356 pix_fmt_mp->plane_fmt[1].bytesperline =
357 pix_fmt_mp->plane_fmt[2].bytesperline =
358 pix_fmt_mp->width / 2;
359 }
360 }
361
362 for (i = 0; i < pix_fmt_mp->num_planes; i++)
363 memset(&(pix_fmt_mp->plane_fmt[i].reserved[0]), 0x0,
364 sizeof(pix_fmt_mp->plane_fmt[0].reserved));
365
366 pix_fmt_mp->flags = 0;
367 memset(&pix_fmt_mp->reserved, 0x0,
368 sizeof(pix_fmt_mp->reserved));
369
370 return 0;
371}
372
373static void mtk_venc_set_param(struct mtk_vcodec_ctx *ctx,
374 struct venc_enc_param *param)
375{
376 struct mtk_q_data *q_data_src = &ctx->q_data[MTK_Q_DATA_SRC];
377 struct mtk_enc_params *enc_params = &ctx->enc_params;
378
379 switch (q_data_src->fmt->fourcc) {
380 case V4L2_PIX_FMT_YUV420M:
381 param->input_yuv_fmt = VENC_YUV_FORMAT_I420;
382 break;
383 case V4L2_PIX_FMT_YVU420M:
384 param->input_yuv_fmt = VENC_YUV_FORMAT_YV12;
385 break;
386 case V4L2_PIX_FMT_NV12M:
387 param->input_yuv_fmt = VENC_YUV_FORMAT_NV12;
388 break;
389 case V4L2_PIX_FMT_NV21M:
390 param->input_yuv_fmt = VENC_YUV_FORMAT_NV21;
391 break;
392 default:
393 mtk_v4l2_err("Unsupport fourcc =%d", q_data_src->fmt->fourcc);
394 break;
395 }
396 param->h264_profile = enc_params->h264_profile;
397 param->h264_level = enc_params->h264_level;
398
399 /* Config visible resolution */
400 param->width = q_data_src->visible_width;
401 param->height = q_data_src->visible_height;
402 /* Config coded resolution */
403 param->buf_width = q_data_src->coded_width;
404 param->buf_height = q_data_src->coded_height;
405 param->frm_rate = enc_params->framerate_num /
406 enc_params->framerate_denom;
407 param->intra_period = enc_params->intra_period;
408 param->gop_size = enc_params->gop_size;
409 param->bitrate = enc_params->bitrate;
410
411 mtk_v4l2_debug(0,
412 "fmt 0x%x, P/L %d/%d, w/h %d/%d, buf %d/%d, fps/bps %d/%d, gop %d, i_period %d",
413 param->input_yuv_fmt, param->h264_profile,
414 param->h264_level, param->width, param->height,
415 param->buf_width, param->buf_height,
416 param->frm_rate, param->bitrate,
417 param->gop_size, param->intra_period);
418}
419
420static int vidioc_venc_s_fmt_cap(struct file *file, void *priv,
421 struct v4l2_format *f)
422{
423 struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
424 struct vb2_queue *vq;
425 struct mtk_q_data *q_data;
426 int i, ret;
427 struct mtk_video_fmt *fmt;
428
429 vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
430 if (!vq) {
431 mtk_v4l2_err("fail to get vq");
432 return -EINVAL;
433 }
434
435 if (vb2_is_busy(vq)) {
436 mtk_v4l2_err("queue busy");
437 return -EBUSY;
438 }
439
440 q_data = mtk_venc_get_q_data(ctx, f->type);
441 if (!q_data) {
442 mtk_v4l2_err("fail to get q data");
443 return -EINVAL;
444 }
445
446 fmt = mtk_venc_find_format(f);
447 if (!fmt) {
448 f->fmt.pix.pixelformat = mtk_video_formats[CAP_FMT_IDX].fourcc;
449 fmt = mtk_venc_find_format(f);
450 }
451
452 q_data->fmt = fmt;
453 ret = vidioc_try_fmt(f, q_data->fmt);
454 if (ret)
455 return ret;
456
457 q_data->coded_width = f->fmt.pix_mp.width;
458 q_data->coded_height = f->fmt.pix_mp.height;
459 q_data->field = f->fmt.pix_mp.field;
460
461 for (i = 0; i < f->fmt.pix_mp.num_planes; i++) {
462 struct v4l2_plane_pix_format *plane_fmt;
463
464 plane_fmt = &f->fmt.pix_mp.plane_fmt[i];
465 q_data->bytesperline[i] = plane_fmt->bytesperline;
466 q_data->sizeimage[i] = plane_fmt->sizeimage;
467 }
468
469 if (ctx->state == MTK_STATE_FREE) {
470 ret = venc_if_init(ctx, q_data->fmt->fourcc);
471 if (ret) {
472 mtk_v4l2_err("venc_if_init failed=%d, codec type=%x",
473 ret, q_data->fmt->fourcc);
474 return -EBUSY;
475 }
476 ctx->state = MTK_STATE_INIT;
477 }
478
479 return 0;
480}
481
482static int vidioc_venc_s_fmt_out(struct file *file, void *priv,
483 struct v4l2_format *f)
484{
485 struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
486 struct vb2_queue *vq;
487 struct mtk_q_data *q_data;
488 int ret, i;
489 struct mtk_video_fmt *fmt;
490 unsigned int pitch_w_div16;
491 struct v4l2_pix_format_mplane *pix_fmt_mp = &f->fmt.pix_mp;
492
493 vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
494 if (!vq) {
495 mtk_v4l2_err("fail to get vq");
496 return -EINVAL;
497 }
498
499 if (vb2_is_busy(vq)) {
500 mtk_v4l2_err("queue busy");
501 return -EBUSY;
502 }
503
504 q_data = mtk_venc_get_q_data(ctx, f->type);
505 if (!q_data) {
506 mtk_v4l2_err("fail to get q data");
507 return -EINVAL;
508 }
509
510 fmt = mtk_venc_find_format(f);
511 if (!fmt) {
512 f->fmt.pix.pixelformat = mtk_video_formats[OUT_FMT_IDX].fourcc;
513 fmt = mtk_venc_find_format(f);
514 }
515
516 pix_fmt_mp->height = clamp(pix_fmt_mp->height,
517 MTK_VENC_MIN_H,
518 MTK_VENC_MAX_H);
519 pix_fmt_mp->width = clamp(pix_fmt_mp->width,
520 MTK_VENC_MIN_W,
521 MTK_VENC_MAX_W);
522
523 q_data->visible_width = f->fmt.pix_mp.width;
524 q_data->visible_height = f->fmt.pix_mp.height;
525 q_data->fmt = fmt;
526 ret = vidioc_try_fmt(f, q_data->fmt);
527 if (ret)
528 return ret;
529
530 q_data->coded_width = f->fmt.pix_mp.width;
531 q_data->coded_height = f->fmt.pix_mp.height;
532
533 pitch_w_div16 = DIV_ROUND_UP(q_data->visible_width, 16);
534 if (pitch_w_div16 % 8 != 0) {
535 /* Adjust returned width/height, so application could correctly
536 * allocate hw required memory
537 */
538 q_data->visible_height += 32;
539 vidioc_try_fmt(f, q_data->fmt);
540 }
541
542 q_data->field = f->fmt.pix_mp.field;
543 ctx->colorspace = f->fmt.pix_mp.colorspace;
544 ctx->ycbcr_enc = f->fmt.pix_mp.ycbcr_enc;
545 ctx->quantization = f->fmt.pix_mp.quantization;
546 ctx->xfer_func = f->fmt.pix_mp.xfer_func;
547
548 for (i = 0; i < f->fmt.pix_mp.num_planes; i++) {
549 struct v4l2_plane_pix_format *plane_fmt;
550
551 plane_fmt = &f->fmt.pix_mp.plane_fmt[i];
552 q_data->bytesperline[i] = plane_fmt->bytesperline;
553 q_data->sizeimage[i] = plane_fmt->sizeimage;
554 }
555
556 return 0;
557}
558
559static int vidioc_venc_g_fmt(struct file *file, void *priv,
560 struct v4l2_format *f)
561{
562 struct v4l2_pix_format_mplane *pix = &f->fmt.pix_mp;
563 struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
564 struct vb2_queue *vq;
565 struct mtk_q_data *q_data;
566 int i;
567
568 vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
569 if (!vq)
570 return -EINVAL;
571
572 q_data = mtk_venc_get_q_data(ctx, f->type);
573
574 pix->width = q_data->coded_width;
575 pix->height = q_data->coded_height;
576 pix->pixelformat = q_data->fmt->fourcc;
577 pix->field = q_data->field;
578 pix->num_planes = q_data->fmt->num_planes;
579 for (i = 0; i < pix->num_planes; i++) {
580 pix->plane_fmt[i].bytesperline = q_data->bytesperline[i];
581 pix->plane_fmt[i].sizeimage = q_data->sizeimage[i];
582 memset(&(pix->plane_fmt[i].reserved[0]), 0x0,
583 sizeof(pix->plane_fmt[i].reserved));
584 }
585
586 pix->flags = 0;
587 pix->colorspace = ctx->colorspace;
588 pix->ycbcr_enc = ctx->ycbcr_enc;
589 pix->quantization = ctx->quantization;
590 pix->xfer_func = ctx->xfer_func;
591
592 return 0;
593}
594
595static int vidioc_try_fmt_vid_cap_mplane(struct file *file, void *priv,
596 struct v4l2_format *f)
597{
598 struct mtk_video_fmt *fmt;
599 struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
600
601 fmt = mtk_venc_find_format(f);
602 if (!fmt) {
603 f->fmt.pix.pixelformat = mtk_video_formats[CAP_FMT_IDX].fourcc;
604 fmt = mtk_venc_find_format(f);
605 }
606 f->fmt.pix_mp.colorspace = ctx->colorspace;
607 f->fmt.pix_mp.ycbcr_enc = ctx->ycbcr_enc;
608 f->fmt.pix_mp.quantization = ctx->quantization;
609 f->fmt.pix_mp.xfer_func = ctx->xfer_func;
610
611 return vidioc_try_fmt(f, fmt);
612}
613
614static int vidioc_try_fmt_vid_out_mplane(struct file *file, void *priv,
615 struct v4l2_format *f)
616{
617 struct mtk_video_fmt *fmt;
618
619 fmt = mtk_venc_find_format(f);
620 if (!fmt) {
621 f->fmt.pix.pixelformat = mtk_video_formats[OUT_FMT_IDX].fourcc;
622 fmt = mtk_venc_find_format(f);
623 }
624 if (!f->fmt.pix_mp.colorspace) {
625 f->fmt.pix_mp.colorspace = V4L2_COLORSPACE_REC709;
626 f->fmt.pix_mp.ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
627 f->fmt.pix_mp.quantization = V4L2_QUANTIZATION_DEFAULT;
628 f->fmt.pix_mp.xfer_func = V4L2_XFER_FUNC_DEFAULT;
629 }
630
631 return vidioc_try_fmt(f, fmt);
632}
633
634static int vidioc_venc_qbuf(struct file *file, void *priv,
635 struct v4l2_buffer *buf)
636{
637 struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
638
639 if (ctx->state == MTK_STATE_ABORT) {
640 mtk_v4l2_err("[%d] Call on QBUF after unrecoverable error",
641 ctx->id);
642 return -EIO;
643 }
644
645 return v4l2_m2m_qbuf(file, ctx->m2m_ctx, buf);
646}
647
648static int vidioc_venc_dqbuf(struct file *file, void *priv,
649 struct v4l2_buffer *buf)
650{
651 struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
652
653 if (ctx->state == MTK_STATE_ABORT) {
654 mtk_v4l2_err("[%d] Call on QBUF after unrecoverable error",
655 ctx->id);
656 return -EIO;
657 }
658
659 return v4l2_m2m_dqbuf(file, ctx->m2m_ctx, buf);
660}
661
662const struct v4l2_ioctl_ops mtk_venc_ioctl_ops = {
663 .vidioc_streamon = v4l2_m2m_ioctl_streamon,
664 .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
665
666 .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
667 .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
668 .vidioc_qbuf = vidioc_venc_qbuf,
669 .vidioc_dqbuf = vidioc_venc_dqbuf,
670
671 .vidioc_querycap = vidioc_venc_querycap,
672 .vidioc_enum_fmt_vid_cap_mplane = vidioc_enum_fmt_vid_cap_mplane,
673 .vidioc_enum_fmt_vid_out_mplane = vidioc_enum_fmt_vid_out_mplane,
674 .vidioc_enum_framesizes = vidioc_enum_framesizes,
675
676 .vidioc_try_fmt_vid_cap_mplane = vidioc_try_fmt_vid_cap_mplane,
677 .vidioc_try_fmt_vid_out_mplane = vidioc_try_fmt_vid_out_mplane,
678 .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
679 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
680 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
681
682 .vidioc_s_parm = vidioc_venc_s_parm,
683 .vidioc_g_parm = vidioc_venc_g_parm,
684 .vidioc_s_fmt_vid_cap_mplane = vidioc_venc_s_fmt_cap,
685 .vidioc_s_fmt_vid_out_mplane = vidioc_venc_s_fmt_out,
686
687 .vidioc_g_fmt_vid_cap_mplane = vidioc_venc_g_fmt,
688 .vidioc_g_fmt_vid_out_mplane = vidioc_venc_g_fmt,
689
690 .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
691 .vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf,
692};
693
694static int vb2ops_venc_queue_setup(struct vb2_queue *vq,
695 unsigned int *nbuffers,
696 unsigned int *nplanes,
697 unsigned int sizes[],
698 struct device *alloc_devs[])
699{
700 struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(vq);
701 struct mtk_q_data *q_data;
702 unsigned int i;
703
704 q_data = mtk_venc_get_q_data(ctx, vq->type);
705
706 if (q_data == NULL)
707 return -EINVAL;
708
709 if (*nplanes) {
710 for (i = 0; i < *nplanes; i++)
711 if (sizes[i] < q_data->sizeimage[i])
712 return -EINVAL;
713 } else {
714 *nplanes = q_data->fmt->num_planes;
715 for (i = 0; i < *nplanes; i++)
716 sizes[i] = q_data->sizeimage[i];
717 }
718
719 return 0;
720}
721
722static int vb2ops_venc_buf_prepare(struct vb2_buffer *vb)
723{
724 struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
725 struct mtk_q_data *q_data;
726 int i;
727
728 q_data = mtk_venc_get_q_data(ctx, vb->vb2_queue->type);
729
730 for (i = 0; i < q_data->fmt->num_planes; i++) {
731 if (vb2_plane_size(vb, i) < q_data->sizeimage[i]) {
732 mtk_v4l2_err("data will not fit into plane %d (%lu < %d)",
733 i, vb2_plane_size(vb, i),
734 q_data->sizeimage[i]);
735 return -EINVAL;
736 }
737 }
738
739 return 0;
740}
741
742static void vb2ops_venc_buf_queue(struct vb2_buffer *vb)
743{
744 struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
745 struct vb2_v4l2_buffer *vb2_v4l2 =
746 container_of(vb, struct vb2_v4l2_buffer, vb2_buf);
747
748 struct mtk_video_enc_buf *mtk_buf =
749 container_of(vb2_v4l2, struct mtk_video_enc_buf, vb);
750
751 if ((vb->vb2_queue->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) &&
752 (ctx->param_change != MTK_ENCODE_PARAM_NONE)) {
753 mtk_v4l2_debug(1, "[%d] Before id=%d encode parameter change %x",
754 ctx->id,
755 mtk_buf->vb.vb2_buf.index,
756 ctx->param_change);
757 mtk_buf->param_change = ctx->param_change;
758 mtk_buf->enc_params = ctx->enc_params;
759 ctx->param_change = MTK_ENCODE_PARAM_NONE;
760 }
761
762 v4l2_m2m_buf_queue(ctx->m2m_ctx, to_vb2_v4l2_buffer(vb));
763}
764
765static int vb2ops_venc_start_streaming(struct vb2_queue *q, unsigned int count)
766{
767 struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(q);
768 struct venc_enc_param param;
769 int ret;
770 int i;
771
772 /* Once state turn into MTK_STATE_ABORT, we need stop_streaming
773 * to clear it
774 */
775 if ((ctx->state == MTK_STATE_ABORT) || (ctx->state == MTK_STATE_FREE)) {
776 ret = -EIO;
777 goto err_set_param;
778 }
779
780 /* Do the initialization when both start_streaming have been called */
781 if (V4L2_TYPE_IS_OUTPUT(q->type)) {
782 if (!vb2_start_streaming_called(&ctx->m2m_ctx->cap_q_ctx.q))
783 return 0;
784 } else {
785 if (!vb2_start_streaming_called(&ctx->m2m_ctx->out_q_ctx.q))
786 return 0;
787 }
788
789 mtk_venc_set_param(ctx, &param);
790 ret = venc_if_set_param(ctx, VENC_SET_PARAM_ENC, &param);
791 if (ret) {
792 mtk_v4l2_err("venc_if_set_param failed=%d", ret);
793 ctx->state = MTK_STATE_ABORT;
794 goto err_set_param;
795 }
796 ctx->param_change = MTK_ENCODE_PARAM_NONE;
797
798 if ((ctx->q_data[MTK_Q_DATA_DST].fmt->fourcc == V4L2_PIX_FMT_H264) &&
799 (ctx->enc_params.seq_hdr_mode !=
800 V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE)) {
801 ret = venc_if_set_param(ctx,
802 VENC_SET_PARAM_PREPEND_HEADER,
803 NULL);
804 if (ret) {
805 mtk_v4l2_err("venc_if_set_param failed=%d", ret);
806 ctx->state = MTK_STATE_ABORT;
807 goto err_set_param;
808 }
809 ctx->state = MTK_STATE_HEADER;
810 }
811
812 return 0;
813
814err_set_param:
815 for (i = 0; i < q->num_buffers; ++i) {
816 if (q->bufs[i]->state == VB2_BUF_STATE_ACTIVE) {
817 mtk_v4l2_debug(0, "[%d] id=%d, type=%d, %d -> VB2_BUF_STATE_QUEUED",
818 ctx->id, i, q->type,
819 (int)q->bufs[i]->state);
820 v4l2_m2m_buf_done(to_vb2_v4l2_buffer(q->bufs[i]),
821 VB2_BUF_STATE_QUEUED);
822 }
823 }
824
825 return ret;
826}
827
828static void vb2ops_venc_stop_streaming(struct vb2_queue *q)
829{
830 struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(q);
831 struct vb2_buffer *src_buf, *dst_buf;
832 int ret;
833
834 mtk_v4l2_debug(2, "[%d]-> type=%d", ctx->id, q->type);
835
836 if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
837 while ((dst_buf = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx))) {
838 dst_buf->planes[0].bytesused = 0;
839 v4l2_m2m_buf_done(to_vb2_v4l2_buffer(dst_buf),
840 VB2_BUF_STATE_ERROR);
841 }
842 } else {
843 while ((src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx)))
844 v4l2_m2m_buf_done(to_vb2_v4l2_buffer(src_buf),
845 VB2_BUF_STATE_ERROR);
846 }
847
848 if ((q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE &&
849 vb2_is_streaming(&ctx->m2m_ctx->out_q_ctx.q)) ||
850 (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE &&
851 vb2_is_streaming(&ctx->m2m_ctx->cap_q_ctx.q))) {
852 mtk_v4l2_debug(1, "[%d]-> q type %d out=%d cap=%d",
853 ctx->id, q->type,
854 vb2_is_streaming(&ctx->m2m_ctx->out_q_ctx.q),
855 vb2_is_streaming(&ctx->m2m_ctx->cap_q_ctx.q));
856 return;
857 }
858
859 /* Release the encoder if both streams are stopped. */
860 ret = venc_if_deinit(ctx);
861 if (ret)
862 mtk_v4l2_err("venc_if_deinit failed=%d", ret);
863
864 ctx->state = MTK_STATE_FREE;
865}
866
867static struct vb2_ops mtk_venc_vb2_ops = {
868 .queue_setup = vb2ops_venc_queue_setup,
869 .buf_prepare = vb2ops_venc_buf_prepare,
870 .buf_queue = vb2ops_venc_buf_queue,
871 .wait_prepare = vb2_ops_wait_prepare,
872 .wait_finish = vb2_ops_wait_finish,
873 .start_streaming = vb2ops_venc_start_streaming,
874 .stop_streaming = vb2ops_venc_stop_streaming,
875};
876
877static int mtk_venc_encode_header(void *priv)
878{
879 struct mtk_vcodec_ctx *ctx = priv;
880 int ret;
881 struct vb2_buffer *dst_buf;
882 struct mtk_vcodec_mem bs_buf;
883 struct venc_done_result enc_result;
884
885 dst_buf = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx);
886 if (!dst_buf) {
887 mtk_v4l2_debug(1, "No dst buffer");
888 return -EINVAL;
889 }
890
891 bs_buf.va = vb2_plane_vaddr(dst_buf, 0);
892 bs_buf.dma_addr = vb2_dma_contig_plane_dma_addr(dst_buf, 0);
893 bs_buf.size = (size_t)dst_buf->planes[0].length;
894
895 mtk_v4l2_debug(1,
896 "[%d] buf id=%d va=0x%p dma_addr=0x%llx size=%zu",
897 ctx->id,
898 dst_buf->index, bs_buf.va,
899 (u64)bs_buf.dma_addr,
900 bs_buf.size);
901
902 ret = venc_if_encode(ctx,
903 VENC_START_OPT_ENCODE_SEQUENCE_HEADER,
904 NULL, &bs_buf, &enc_result);
905
906 if (ret) {
907 dst_buf->planes[0].bytesused = 0;
908 ctx->state = MTK_STATE_ABORT;
909 v4l2_m2m_buf_done(to_vb2_v4l2_buffer(dst_buf),
910 VB2_BUF_STATE_ERROR);
911 mtk_v4l2_err("venc_if_encode failed=%d", ret);
912 return -EINVAL;
913 }
914
915 ctx->state = MTK_STATE_HEADER;
916 dst_buf->planes[0].bytesused = enc_result.bs_size;
917 v4l2_m2m_buf_done(to_vb2_v4l2_buffer(dst_buf), VB2_BUF_STATE_DONE);
918
919 return 0;
920}
921
922static int mtk_venc_param_change(struct mtk_vcodec_ctx *ctx)
923{
924 struct venc_enc_param enc_prm;
925 struct vb2_buffer *vb = v4l2_m2m_next_src_buf(ctx->m2m_ctx);
926 struct vb2_v4l2_buffer *vb2_v4l2 =
927 container_of(vb, struct vb2_v4l2_buffer, vb2_buf);
928 struct mtk_video_enc_buf *mtk_buf =
929 container_of(vb2_v4l2, struct mtk_video_enc_buf, vb);
930
931 int ret = 0;
932
933 memset(&enc_prm, 0, sizeof(enc_prm));
934 if (mtk_buf->param_change == MTK_ENCODE_PARAM_NONE)
935 return 0;
936
937 if (mtk_buf->param_change & MTK_ENCODE_PARAM_BITRATE) {
938 enc_prm.bitrate = mtk_buf->enc_params.bitrate;
939 mtk_v4l2_debug(1, "[%d] id=%d, change param br=%d",
940 ctx->id,
941 mtk_buf->vb.vb2_buf.index,
942 enc_prm.bitrate);
943 ret |= venc_if_set_param(ctx,
944 VENC_SET_PARAM_ADJUST_BITRATE,
945 &enc_prm);
946 }
947 if (!ret && mtk_buf->param_change & MTK_ENCODE_PARAM_FRAMERATE) {
948 enc_prm.frm_rate = mtk_buf->enc_params.framerate_num /
949 mtk_buf->enc_params.framerate_denom;
950 mtk_v4l2_debug(1, "[%d] id=%d, change param fr=%d",
951 ctx->id,
952 mtk_buf->vb.vb2_buf.index,
953 enc_prm.frm_rate);
954 ret |= venc_if_set_param(ctx,
955 VENC_SET_PARAM_ADJUST_FRAMERATE,
956 &enc_prm);
957 }
958 if (!ret && mtk_buf->param_change & MTK_ENCODE_PARAM_GOP_SIZE) {
959 enc_prm.gop_size = mtk_buf->enc_params.gop_size;
960 mtk_v4l2_debug(1, "change param intra period=%d",
961 enc_prm.gop_size);
962 ret |= venc_if_set_param(ctx,
963 VENC_SET_PARAM_GOP_SIZE,
964 &enc_prm);
965 }
966 if (!ret && mtk_buf->param_change & MTK_ENCODE_PARAM_FORCE_INTRA) {
967 mtk_v4l2_debug(1, "[%d] id=%d, change param force I=%d",
968 ctx->id,
969 mtk_buf->vb.vb2_buf.index,
970 mtk_buf->enc_params.force_intra);
971 if (mtk_buf->enc_params.force_intra)
972 ret |= venc_if_set_param(ctx,
973 VENC_SET_PARAM_FORCE_INTRA,
974 NULL);
975 }
976
977 mtk_buf->param_change = MTK_ENCODE_PARAM_NONE;
978
979 if (ret) {
980 ctx->state = MTK_STATE_ABORT;
981 mtk_v4l2_err("venc_if_set_param %d failed=%d",
982 mtk_buf->param_change, ret);
983 return -1;
984 }
985
986 return 0;
987}
988
989/*
990 * v4l2_m2m_streamoff() holds dev_mutex and waits mtk_venc_worker()
991 * to call v4l2_m2m_job_finish().
992 * If mtk_venc_worker() tries to acquire dev_mutex, it will deadlock.
993 * So this function must not try to acquire dev->dev_mutex.
994 * This means v4l2 ioctls and mtk_venc_worker() can run at the same time.
995 * mtk_venc_worker() should be carefully implemented to avoid bugs.
996 */
997static void mtk_venc_worker(struct work_struct *work)
998{
999 struct mtk_vcodec_ctx *ctx = container_of(work, struct mtk_vcodec_ctx,
1000 encode_work);
1001 struct vb2_buffer *src_buf, *dst_buf;
1002 struct venc_frm_buf frm_buf;
1003 struct mtk_vcodec_mem bs_buf;
1004 struct venc_done_result enc_result;
1005 int ret, i;
1006 struct vb2_v4l2_buffer *vb2_v4l2;
1007
1008 /* check dst_buf, dst_buf may be removed in device_run
1009 * to stored encdoe header so we need check dst_buf and
1010 * call job_finish here to prevent recursion
1011 */
1012 dst_buf = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx);
1013 if (!dst_buf) {
1014 v4l2_m2m_job_finish(ctx->dev->m2m_dev_enc, ctx->m2m_ctx);
1015 return;
1016 }
1017
1018 src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
1019 memset(&frm_buf, 0, sizeof(frm_buf));
1020 for (i = 0; i < src_buf->num_planes ; i++) {
1021 frm_buf.fb_addr[i].va = vb2_plane_vaddr(src_buf, i);
1022 frm_buf.fb_addr[i].dma_addr =
1023 vb2_dma_contig_plane_dma_addr(src_buf, i);
1024 frm_buf.fb_addr[i].size =
1025 (size_t)src_buf->planes[i].length;
1026 }
1027 bs_buf.va = vb2_plane_vaddr(dst_buf, 0);
1028 bs_buf.dma_addr = vb2_dma_contig_plane_dma_addr(dst_buf, 0);
1029 bs_buf.size = (size_t)dst_buf->planes[0].length;
1030
1031 mtk_v4l2_debug(2,
1032 "Framebuf VA=%p PA=%llx Size=0x%zx;VA=%p PA=0x%llx Size=0x%zx;VA=%p PA=0x%llx Size=%zu",
1033 frm_buf.fb_addr[0].va,
1034 (u64)frm_buf.fb_addr[0].dma_addr,
1035 frm_buf.fb_addr[0].size,
1036 frm_buf.fb_addr[1].va,
1037 (u64)frm_buf.fb_addr[1].dma_addr,
1038 frm_buf.fb_addr[1].size,
1039 frm_buf.fb_addr[2].va,
1040 (u64)frm_buf.fb_addr[2].dma_addr,
1041 frm_buf.fb_addr[2].size);
1042
1043 ret = venc_if_encode(ctx, VENC_START_OPT_ENCODE_FRAME,
1044 &frm_buf, &bs_buf, &enc_result);
1045
1046 vb2_v4l2 = container_of(dst_buf, struct vb2_v4l2_buffer, vb2_buf);
1047 if (enc_result.is_key_frm)
1048 vb2_v4l2->flags |= V4L2_BUF_FLAG_KEYFRAME;
1049
1050 if (ret) {
1051 v4l2_m2m_buf_done(to_vb2_v4l2_buffer(src_buf),
1052 VB2_BUF_STATE_ERROR);
1053 dst_buf->planes[0].bytesused = 0;
1054 v4l2_m2m_buf_done(to_vb2_v4l2_buffer(dst_buf),
1055 VB2_BUF_STATE_ERROR);
1056 mtk_v4l2_err("venc_if_encode failed=%d", ret);
1057 } else {
1058 v4l2_m2m_buf_done(to_vb2_v4l2_buffer(src_buf),
1059 VB2_BUF_STATE_DONE);
1060 dst_buf->planes[0].bytesused = enc_result.bs_size;
1061 v4l2_m2m_buf_done(to_vb2_v4l2_buffer(dst_buf),
1062 VB2_BUF_STATE_DONE);
1063 mtk_v4l2_debug(2, "venc_if_encode bs size=%d",
1064 enc_result.bs_size);
1065 }
1066
1067 v4l2_m2m_job_finish(ctx->dev->m2m_dev_enc, ctx->m2m_ctx);
1068
1069 mtk_v4l2_debug(1, "<=== src_buf[%d] dst_buf[%d] venc_if_encode ret=%d Size=%u===>",
1070 src_buf->index, dst_buf->index, ret,
1071 enc_result.bs_size);
1072}
1073
1074static void m2mops_venc_device_run(void *priv)
1075{
1076 struct mtk_vcodec_ctx *ctx = priv;
1077
1078 if ((ctx->q_data[MTK_Q_DATA_DST].fmt->fourcc == V4L2_PIX_FMT_H264) &&
1079 (ctx->state != MTK_STATE_HEADER)) {
1080 /* encode h264 sps/pps header */
1081 mtk_venc_encode_header(ctx);
1082 queue_work(ctx->dev->encode_workqueue, &ctx->encode_work);
1083 return;
1084 }
1085
1086 mtk_venc_param_change(ctx);
1087 queue_work(ctx->dev->encode_workqueue, &ctx->encode_work);
1088}
1089
1090static int m2mops_venc_job_ready(void *m2m_priv)
1091{
1092 struct mtk_vcodec_ctx *ctx = m2m_priv;
1093
1094 if (ctx->state == MTK_STATE_ABORT || ctx->state == MTK_STATE_FREE) {
1095 mtk_v4l2_debug(3, "[%d]Not ready: state=0x%x.",
1096 ctx->id, ctx->state);
1097 return 0;
1098 }
1099
1100 return 1;
1101}
1102
1103static void m2mops_venc_job_abort(void *priv)
1104{
1105 struct mtk_vcodec_ctx *ctx = priv;
1106
1107 ctx->state = MTK_STATE_ABORT;
1108}
1109
1110static void m2mops_venc_lock(void *m2m_priv)
1111{
1112 struct mtk_vcodec_ctx *ctx = m2m_priv;
1113
1114 mutex_lock(&ctx->dev->dev_mutex);
1115}
1116
1117static void m2mops_venc_unlock(void *m2m_priv)
1118{
1119 struct mtk_vcodec_ctx *ctx = m2m_priv;
1120
1121 mutex_unlock(&ctx->dev->dev_mutex);
1122}
1123
1124const struct v4l2_m2m_ops mtk_venc_m2m_ops = {
1125 .device_run = m2mops_venc_device_run,
1126 .job_ready = m2mops_venc_job_ready,
1127 .job_abort = m2mops_venc_job_abort,
1128 .lock = m2mops_venc_lock,
1129 .unlock = m2mops_venc_unlock,
1130};
1131
1132void mtk_vcodec_enc_set_default_params(struct mtk_vcodec_ctx *ctx)
1133{
1134 struct mtk_q_data *q_data;
1135
1136 ctx->m2m_ctx->q_lock = &ctx->dev->dev_mutex;
1137 ctx->fh.m2m_ctx = ctx->m2m_ctx;
1138 ctx->fh.ctrl_handler = &ctx->ctrl_hdl;
1139 INIT_WORK(&ctx->encode_work, mtk_venc_worker);
1140
1141 ctx->colorspace = V4L2_COLORSPACE_REC709;
1142 ctx->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
1143 ctx->quantization = V4L2_QUANTIZATION_DEFAULT;
1144 ctx->xfer_func = V4L2_XFER_FUNC_DEFAULT;
1145
1146 q_data = &ctx->q_data[MTK_Q_DATA_SRC];
1147 memset(q_data, 0, sizeof(struct mtk_q_data));
1148 q_data->visible_width = DFT_CFG_WIDTH;
1149 q_data->visible_height = DFT_CFG_HEIGHT;
1150 q_data->coded_width = DFT_CFG_WIDTH;
1151 q_data->coded_height = DFT_CFG_HEIGHT;
1152 q_data->field = V4L2_FIELD_NONE;
1153
1154 q_data->fmt = &mtk_video_formats[OUT_FMT_IDX];
1155
1156 v4l_bound_align_image(&q_data->coded_width,
1157 MTK_VENC_MIN_W,
1158 MTK_VENC_MAX_W, 4,
1159 &q_data->coded_height,
1160 MTK_VENC_MIN_H,
1161 MTK_VENC_MAX_H, 5, 6);
1162
1163 if (q_data->coded_width < DFT_CFG_WIDTH &&
1164 (q_data->coded_width + 16) <= MTK_VENC_MAX_W)
1165 q_data->coded_width += 16;
1166 if (q_data->coded_height < DFT_CFG_HEIGHT &&
1167 (q_data->coded_height + 32) <= MTK_VENC_MAX_H)
1168 q_data->coded_height += 32;
1169
1170 q_data->sizeimage[0] =
1171 q_data->coded_width * q_data->coded_height+
1172 ((ALIGN(q_data->coded_width, 16) * 2) * 16);
1173 q_data->bytesperline[0] = q_data->coded_width;
1174 q_data->sizeimage[1] =
1175 (q_data->coded_width * q_data->coded_height) / 2 +
1176 (ALIGN(q_data->coded_width, 16) * 16);
1177 q_data->bytesperline[1] = q_data->coded_width;
1178
1179 q_data = &ctx->q_data[MTK_Q_DATA_DST];
1180 memset(q_data, 0, sizeof(struct mtk_q_data));
1181 q_data->coded_width = DFT_CFG_WIDTH;
1182 q_data->coded_height = DFT_CFG_HEIGHT;
1183 q_data->fmt = &mtk_video_formats[CAP_FMT_IDX];
1184 q_data->field = V4L2_FIELD_NONE;
1185 ctx->q_data[MTK_Q_DATA_DST].sizeimage[0] =
1186 DFT_CFG_WIDTH * DFT_CFG_HEIGHT;
1187 ctx->q_data[MTK_Q_DATA_DST].bytesperline[0] = 0;
1188
1189}
1190
1191int mtk_vcodec_enc_ctrls_setup(struct mtk_vcodec_ctx *ctx)
1192{
1193 const struct v4l2_ctrl_ops *ops = &mtk_vcodec_enc_ctrl_ops;
1194 struct v4l2_ctrl_handler *handler = &ctx->ctrl_hdl;
1195
1196 v4l2_ctrl_handler_init(handler, MTK_MAX_CTRLS_HINT);
1197
1198 v4l2_ctrl_new_std(handler, ops, V4L2_CID_MPEG_VIDEO_BITRATE,
1199 1, 4000000, 1, 4000000);
1200 v4l2_ctrl_new_std(handler, ops, V4L2_CID_MPEG_VIDEO_B_FRAMES,
1201 0, 2, 1, 0);
1202 v4l2_ctrl_new_std(handler, ops, V4L2_CID_MPEG_VIDEO_FRAME_RC_ENABLE,
1203 0, 1, 1, 1);
1204 v4l2_ctrl_new_std(handler, ops, V4L2_CID_MPEG_VIDEO_H264_MAX_QP,
1205 0, 51, 1, 51);
1206 v4l2_ctrl_new_std(handler, ops, V4L2_CID_MPEG_VIDEO_H264_I_PERIOD,
1207 0, 65535, 1, 0);
1208 v4l2_ctrl_new_std(handler, ops, V4L2_CID_MPEG_VIDEO_GOP_SIZE,
1209 0, 65535, 1, 0);
1210 v4l2_ctrl_new_std(handler, ops, V4L2_CID_MPEG_VIDEO_MB_RC_ENABLE,
1211 0, 1, 1, 0);
1212 v4l2_ctrl_new_std(handler, ops, V4L2_CID_MPEG_VIDEO_FORCE_KEY_FRAME,
1213 0, 0, 0, 0);
1214 v4l2_ctrl_new_std_menu(handler, ops,
1215 V4L2_CID_MPEG_VIDEO_HEADER_MODE,
1216 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME,
1217 0, V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE);
1218 v4l2_ctrl_new_std_menu(handler, ops, V4L2_CID_MPEG_VIDEO_H264_PROFILE,
1219 V4L2_MPEG_VIDEO_H264_PROFILE_HIGH,
1220 0, V4L2_MPEG_VIDEO_H264_PROFILE_MAIN);
1221 v4l2_ctrl_new_std_menu(handler, ops, V4L2_CID_MPEG_VIDEO_H264_LEVEL,
1222 V4L2_MPEG_VIDEO_H264_LEVEL_4_2,
1223 0, V4L2_MPEG_VIDEO_H264_LEVEL_4_0);
1224 if (handler->error) {
1225 mtk_v4l2_err("Init control handler fail %d",
1226 handler->error);
1227 return handler->error;
1228 }
1229
1230 v4l2_ctrl_handler_setup(&ctx->ctrl_hdl);
1231
1232 return 0;
1233}
1234
1235int mtk_vcodec_enc_queue_init(void *priv, struct vb2_queue *src_vq,
1236 struct vb2_queue *dst_vq)
1237{
1238 struct mtk_vcodec_ctx *ctx = priv;
1239 int ret;
1240
1241 /* Note: VB2_USERPTR works with dma-contig because mt8173
1242 * support iommu
1243 * https://patchwork.kernel.org/patch/8335461/
1244 * https://patchwork.kernel.org/patch/7596181/
1245 */
1246 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
1247 src_vq->io_modes = VB2_DMABUF | VB2_MMAP | VB2_USERPTR;
1248 src_vq->drv_priv = ctx;
1249 src_vq->buf_struct_size = sizeof(struct mtk_video_enc_buf);
1250 src_vq->ops = &mtk_venc_vb2_ops;
1251 src_vq->mem_ops = &vb2_dma_contig_memops;
1252 src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1253 src_vq->lock = &ctx->dev->dev_mutex;
1254 src_vq->dev = &ctx->dev->plat_dev->dev;
1255
1256 ret = vb2_queue_init(src_vq);
1257 if (ret)
1258 return ret;
1259
1260 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
1261 dst_vq->io_modes = VB2_DMABUF | VB2_MMAP | VB2_USERPTR;
1262 dst_vq->drv_priv = ctx;
1263 dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
1264 dst_vq->ops = &mtk_venc_vb2_ops;
1265 dst_vq->mem_ops = &vb2_dma_contig_memops;
1266 dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1267 dst_vq->lock = &ctx->dev->dev_mutex;
1268 dst_vq->dev = &ctx->dev->plat_dev->dev;
1269
1270 return vb2_queue_init(dst_vq);
1271}
1272
1273int mtk_venc_unlock(struct mtk_vcodec_ctx *ctx)
1274{
1275 struct mtk_vcodec_dev *dev = ctx->dev;
1276
1277 mutex_unlock(&dev->enc_mutex);
1278 return 0;
1279}
1280
1281int mtk_venc_lock(struct mtk_vcodec_ctx *ctx)
1282{
1283 struct mtk_vcodec_dev *dev = ctx->dev;
1284
1285 mutex_lock(&dev->enc_mutex);
1286 return 0;
1287}
1288
1289void mtk_vcodec_enc_release(struct mtk_vcodec_ctx *ctx)
1290{
1291 venc_if_deinit(ctx);
1292}
diff --git a/drivers/media/platform/mtk-vcodec/mtk_vcodec_enc.h b/drivers/media/platform/mtk-vcodec/mtk_vcodec_enc.h
new file mode 100644
index 000000000000..d7a154a97510
--- /dev/null
+++ b/drivers/media/platform/mtk-vcodec/mtk_vcodec_enc.h
@@ -0,0 +1,58 @@
1/*
2* Copyright (c) 2016 MediaTek Inc.
3* Author: PC Chen <pc.chen@mediatek.com>
4* Tiffany Lin <tiffany.lin@mediatek.com>
5*
6* This program is free software; you can redistribute it and/or modify
7* it under the terms of the GNU General Public License version 2 as
8* published by the Free Software Foundation.
9*
10* This program is distributed in the hope that it will be useful,
11* but WITHOUT ANY WARRANTY; without even the implied warranty of
12* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13* GNU General Public License for more details.
14*/
15
16#ifndef _MTK_VCODEC_ENC_H_
17#define _MTK_VCODEC_ENC_H_
18
19#include <media/videobuf2-core.h>
20#include <media/videobuf2-v4l2.h>
21
22#define MTK_VENC_IRQ_STATUS_SPS 0x1
23#define MTK_VENC_IRQ_STATUS_PPS 0x2
24#define MTK_VENC_IRQ_STATUS_FRM 0x4
25#define MTK_VENC_IRQ_STATUS_DRAM 0x8
26#define MTK_VENC_IRQ_STATUS_PAUSE 0x10
27#define MTK_VENC_IRQ_STATUS_SWITCH 0x20
28
29#define MTK_VENC_IRQ_STATUS_OFFSET 0x05C
30#define MTK_VENC_IRQ_ACK_OFFSET 0x060
31
32/**
33 * struct mtk_video_enc_buf - Private data related to each VB2 buffer.
34 * @vb: Pointer to related VB2 buffer.
35 * @list: list that buffer link to
36 * @param_change: Types of encode parameter change before encoding this
37 * buffer
38 * @enc_params: Encode parameters changed before encode this buffer
39 */
40struct mtk_video_enc_buf {
41 struct vb2_v4l2_buffer vb;
42 struct list_head list;
43 u32 param_change;
44 struct mtk_enc_params enc_params;
45};
46
47extern const struct v4l2_ioctl_ops mtk_venc_ioctl_ops;
48extern const struct v4l2_m2m_ops mtk_venc_m2m_ops;
49
50int mtk_venc_unlock(struct mtk_vcodec_ctx *ctx);
51int mtk_venc_lock(struct mtk_vcodec_ctx *ctx);
52int mtk_vcodec_enc_queue_init(void *priv, struct vb2_queue *src_vq,
53 struct vb2_queue *dst_vq);
54void mtk_vcodec_enc_release(struct mtk_vcodec_ctx *ctx);
55int mtk_vcodec_enc_ctrls_setup(struct mtk_vcodec_ctx *ctx);
56void mtk_vcodec_enc_set_default_params(struct mtk_vcodec_ctx *ctx);
57
58#endif /* _MTK_VCODEC_ENC_H_ */
diff --git a/drivers/media/platform/mtk-vcodec/mtk_vcodec_enc_drv.c b/drivers/media/platform/mtk-vcodec/mtk_vcodec_enc_drv.c
new file mode 100644
index 000000000000..e277b7c23516
--- /dev/null
+++ b/drivers/media/platform/mtk-vcodec/mtk_vcodec_enc_drv.c
@@ -0,0 +1,439 @@
1/*
2* Copyright (c) 2016 MediaTek Inc.
3* Author: PC Chen <pc.chen@mediatek.com>
4* Tiffany Lin <tiffany.lin@mediatek.com>
5*
6* This program is free software; you can redistribute it and/or modify
7* it under the terms of the GNU General Public License version 2 as
8* published by the Free Software Foundation.
9*
10* This program is distributed in the hope that it will be useful,
11* but WITHOUT ANY WARRANTY; without even the implied warranty of
12* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13* GNU General Public License for more details.
14*/
15
16#include <linux/slab.h>
17#include <linux/interrupt.h>
18#include <linux/irq.h>
19#include <linux/module.h>
20#include <linux/of_device.h>
21#include <linux/of.h>
22#include <media/v4l2-event.h>
23#include <media/v4l2-mem2mem.h>
24#include <media/videobuf2-dma-contig.h>
25#include <linux/pm_runtime.h>
26
27#include "mtk_vcodec_drv.h"
28#include "mtk_vcodec_enc.h"
29#include "mtk_vcodec_enc_pm.h"
30#include "mtk_vcodec_intr.h"
31#include "mtk_vcodec_util.h"
32#include "mtk_vpu.h"
33
34module_param(mtk_v4l2_dbg_level, int, S_IRUGO | S_IWUSR);
35module_param(mtk_vcodec_dbg, bool, S_IRUGO | S_IWUSR);
36
37/* Wake up context wait_queue */
38static void wake_up_ctx(struct mtk_vcodec_ctx *ctx, unsigned int reason)
39{
40 ctx->int_cond = 1;
41 ctx->int_type = reason;
42 wake_up_interruptible(&ctx->queue);
43}
44
45static void clean_irq_status(unsigned int irq_status, void __iomem *addr)
46{
47 if (irq_status & MTK_VENC_IRQ_STATUS_PAUSE)
48 writel(MTK_VENC_IRQ_STATUS_PAUSE, addr);
49
50 if (irq_status & MTK_VENC_IRQ_STATUS_SWITCH)
51 writel(MTK_VENC_IRQ_STATUS_SWITCH, addr);
52
53 if (irq_status & MTK_VENC_IRQ_STATUS_DRAM)
54 writel(MTK_VENC_IRQ_STATUS_DRAM, addr);
55
56 if (irq_status & MTK_VENC_IRQ_STATUS_SPS)
57 writel(MTK_VENC_IRQ_STATUS_SPS, addr);
58
59 if (irq_status & MTK_VENC_IRQ_STATUS_PPS)
60 writel(MTK_VENC_IRQ_STATUS_PPS, addr);
61
62 if (irq_status & MTK_VENC_IRQ_STATUS_FRM)
63 writel(MTK_VENC_IRQ_STATUS_FRM, addr);
64
65}
66static irqreturn_t mtk_vcodec_enc_irq_handler(int irq, void *priv)
67{
68 struct mtk_vcodec_dev *dev = priv;
69 struct mtk_vcodec_ctx *ctx;
70 unsigned long flags;
71 void __iomem *addr;
72
73 spin_lock_irqsave(&dev->irqlock, flags);
74 ctx = dev->curr_ctx;
75 spin_unlock_irqrestore(&dev->irqlock, flags);
76
77 mtk_v4l2_debug(1, "id=%d", ctx->id);
78 addr = dev->reg_base[VENC_SYS] + MTK_VENC_IRQ_ACK_OFFSET;
79
80 ctx->irq_status = readl(dev->reg_base[VENC_SYS] +
81 (MTK_VENC_IRQ_STATUS_OFFSET));
82
83 clean_irq_status(ctx->irq_status, addr);
84
85 wake_up_ctx(ctx, MTK_INST_IRQ_RECEIVED);
86 return IRQ_HANDLED;
87}
88
89static irqreturn_t mtk_vcodec_enc_lt_irq_handler(int irq, void *priv)
90{
91 struct mtk_vcodec_dev *dev = priv;
92 struct mtk_vcodec_ctx *ctx;
93 unsigned long flags;
94 void __iomem *addr;
95
96 spin_lock_irqsave(&dev->irqlock, flags);
97 ctx = dev->curr_ctx;
98 spin_unlock_irqrestore(&dev->irqlock, flags);
99
100 mtk_v4l2_debug(1, "id=%d", ctx->id);
101 ctx->irq_status = readl(dev->reg_base[VENC_LT_SYS] +
102 (MTK_VENC_IRQ_STATUS_OFFSET));
103
104 addr = dev->reg_base[VENC_LT_SYS] + MTK_VENC_IRQ_ACK_OFFSET;
105
106 clean_irq_status(ctx->irq_status, addr);
107
108 wake_up_ctx(ctx, MTK_INST_IRQ_RECEIVED);
109 return IRQ_HANDLED;
110}
111
112static void mtk_vcodec_enc_reset_handler(void *priv)
113{
114 struct mtk_vcodec_dev *dev = priv;
115 struct mtk_vcodec_ctx *ctx;
116
117 mtk_v4l2_debug(0, "Watchdog timeout!!");
118
119 mutex_lock(&dev->dev_mutex);
120 list_for_each_entry(ctx, &dev->ctx_list, list) {
121 ctx->state = MTK_STATE_ABORT;
122 mtk_v4l2_debug(0, "[%d] Change to state MTK_STATE_ABORT",
123 ctx->id);
124 }
125 mutex_unlock(&dev->dev_mutex);
126}
127
128static int fops_vcodec_open(struct file *file)
129{
130 struct mtk_vcodec_dev *dev = video_drvdata(file);
131 struct mtk_vcodec_ctx *ctx = NULL;
132 int ret = 0;
133
134 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
135 if (!ctx)
136 return -ENOMEM;
137
138 mutex_lock(&dev->dev_mutex);
139 /*
140 * Use simple counter to uniquely identify this context. Only
141 * used for logging.
142 */
143 ctx->id = dev->id_counter++;
144 v4l2_fh_init(&ctx->fh, video_devdata(file));
145 file->private_data = &ctx->fh;
146 v4l2_fh_add(&ctx->fh);
147 INIT_LIST_HEAD(&ctx->list);
148 ctx->dev = dev;
149 init_waitqueue_head(&ctx->queue);
150
151 ctx->type = MTK_INST_ENCODER;
152 ret = mtk_vcodec_enc_ctrls_setup(ctx);
153 if (ret) {
154 mtk_v4l2_err("Failed to setup controls() (%d)",
155 ret);
156 goto err_ctrls_setup;
157 }
158 ctx->m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev_enc, ctx,
159 &mtk_vcodec_enc_queue_init);
160 if (IS_ERR((__force void *)ctx->m2m_ctx)) {
161 ret = PTR_ERR((__force void *)ctx->m2m_ctx);
162 mtk_v4l2_err("Failed to v4l2_m2m_ctx_init() (%d)",
163 ret);
164 goto err_m2m_ctx_init;
165 }
166 mtk_vcodec_enc_set_default_params(ctx);
167
168 if (v4l2_fh_is_singular(&ctx->fh)) {
169 /*
170 * vpu_load_firmware checks if it was loaded already and
171 * does nothing in that case
172 */
173 ret = vpu_load_firmware(dev->vpu_plat_dev);
174 if (ret < 0) {
175 /*
176 * Return 0 if downloading firmware successfully,
177 * otherwise it is failed
178 */
179 mtk_v4l2_err("vpu_load_firmware failed!");
180 goto err_load_fw;
181 }
182
183 dev->enc_capability =
184 vpu_get_venc_hw_capa(dev->vpu_plat_dev);
185 mtk_v4l2_debug(0, "encoder capability %x", dev->enc_capability);
186 }
187
188 mtk_v4l2_debug(2, "Create instance [%d]@%p m2m_ctx=%p ",
189 ctx->id, ctx, ctx->m2m_ctx);
190
191 dev->num_instances++;
192 list_add(&ctx->list, &dev->ctx_list);
193
194 mutex_unlock(&dev->dev_mutex);
195 mtk_v4l2_debug(0, "%s encoder [%d]", dev_name(&dev->plat_dev->dev),
196 ctx->id);
197 return ret;
198
199 /* Deinit when failure occurred */
200err_load_fw:
201 v4l2_m2m_ctx_release(ctx->m2m_ctx);
202err_m2m_ctx_init:
203 v4l2_ctrl_handler_free(&ctx->ctrl_hdl);
204err_ctrls_setup:
205 v4l2_fh_del(&ctx->fh);
206 v4l2_fh_exit(&ctx->fh);
207 kfree(ctx);
208 mutex_unlock(&dev->dev_mutex);
209
210 return ret;
211}
212
213static int fops_vcodec_release(struct file *file)
214{
215 struct mtk_vcodec_dev *dev = video_drvdata(file);
216 struct mtk_vcodec_ctx *ctx = fh_to_ctx(file->private_data);
217
218 mtk_v4l2_debug(1, "[%d] encoder", ctx->id);
219 mutex_lock(&dev->dev_mutex);
220
221 mtk_vcodec_enc_release(ctx);
222 v4l2_fh_del(&ctx->fh);
223 v4l2_fh_exit(&ctx->fh);
224 v4l2_ctrl_handler_free(&ctx->ctrl_hdl);
225 v4l2_m2m_ctx_release(ctx->m2m_ctx);
226
227 list_del_init(&ctx->list);
228 dev->num_instances--;
229 kfree(ctx);
230 mutex_unlock(&dev->dev_mutex);
231 return 0;
232}
233
234static const struct v4l2_file_operations mtk_vcodec_fops = {
235 .owner = THIS_MODULE,
236 .open = fops_vcodec_open,
237 .release = fops_vcodec_release,
238 .poll = v4l2_m2m_fop_poll,
239 .unlocked_ioctl = video_ioctl2,
240 .mmap = v4l2_m2m_fop_mmap,
241};
242
243static int mtk_vcodec_probe(struct platform_device *pdev)
244{
245 struct mtk_vcodec_dev *dev;
246 struct video_device *vfd_enc;
247 struct resource *res;
248 int i, j, ret;
249 DEFINE_DMA_ATTRS(attrs);
250
251 dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
252 if (!dev)
253 return -ENOMEM;
254
255 INIT_LIST_HEAD(&dev->ctx_list);
256 dev->plat_dev = pdev;
257
258 dev->vpu_plat_dev = vpu_get_plat_device(dev->plat_dev);
259 if (dev->vpu_plat_dev == NULL) {
260 mtk_v4l2_err("[VPU] vpu device in not ready");
261 return -EPROBE_DEFER;
262 }
263
264 vpu_wdt_reg_handler(dev->vpu_plat_dev, mtk_vcodec_enc_reset_handler,
265 dev, VPU_RST_ENC);
266
267 ret = mtk_vcodec_init_enc_pm(dev);
268 if (ret < 0) {
269 dev_err(&pdev->dev, "Failed to get mt vcodec clock source!");
270 return ret;
271 }
272
273 for (i = VENC_SYS, j = 0; i < NUM_MAX_VCODEC_REG_BASE; i++, j++) {
274 res = platform_get_resource(pdev, IORESOURCE_MEM, j);
275 if (res == NULL) {
276 dev_err(&pdev->dev, "get memory resource failed.");
277 ret = -ENXIO;
278 goto err_res;
279 }
280 dev->reg_base[i] = devm_ioremap_resource(&pdev->dev, res);
281 if (IS_ERR((__force void *)dev->reg_base[i])) {
282 ret = PTR_ERR((__force void *)dev->reg_base[i]);
283 goto err_res;
284 }
285 mtk_v4l2_debug(2, "reg[%d] base=0x%p", i, dev->reg_base[i]);
286 }
287
288 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
289 if (res == NULL) {
290 dev_err(&pdev->dev, "failed to get irq resource");
291 ret = -ENOENT;
292 goto err_res;
293 }
294
295 dev->enc_irq = platform_get_irq(pdev, 0);
296 ret = devm_request_irq(&pdev->dev, dev->enc_irq,
297 mtk_vcodec_enc_irq_handler,
298 0, pdev->name, dev);
299 if (ret) {
300 dev_err(&pdev->dev, "Failed to install dev->enc_irq %d (%d)",
301 dev->enc_irq,
302 ret);
303 ret = -EINVAL;
304 goto err_res;
305 }
306
307 dev->enc_lt_irq = platform_get_irq(pdev, 1);
308 ret = devm_request_irq(&pdev->dev,
309 dev->enc_lt_irq, mtk_vcodec_enc_lt_irq_handler,
310 0, pdev->name, dev);
311 if (ret) {
312 dev_err(&pdev->dev,
313 "Failed to install dev->enc_lt_irq %d (%d)",
314 dev->enc_lt_irq, ret);
315 ret = -EINVAL;
316 goto err_res;
317 }
318
319 disable_irq(dev->enc_irq);
320 disable_irq(dev->enc_lt_irq); /* VENC_LT */
321 mutex_init(&dev->enc_mutex);
322 mutex_init(&dev->dev_mutex);
323 spin_lock_init(&dev->irqlock);
324
325 snprintf(dev->v4l2_dev.name, sizeof(dev->v4l2_dev.name), "%s",
326 "[MTK_V4L2_VENC]");
327
328 ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
329 if (ret) {
330 mtk_v4l2_err("v4l2_device_register err=%d", ret);
331 goto err_res;
332 }
333
334 init_waitqueue_head(&dev->queue);
335
336 /* allocate video device for encoder and register it */
337 vfd_enc = video_device_alloc();
338 if (!vfd_enc) {
339 mtk_v4l2_err("Failed to allocate video device");
340 ret = -ENOMEM;
341 goto err_enc_alloc;
342 }
343 vfd_enc->fops = &mtk_vcodec_fops;
344 vfd_enc->ioctl_ops = &mtk_venc_ioctl_ops;
345 vfd_enc->release = video_device_release;
346 vfd_enc->lock = &dev->dev_mutex;
347 vfd_enc->v4l2_dev = &dev->v4l2_dev;
348 vfd_enc->vfl_dir = VFL_DIR_M2M;
349 vfd_enc->device_caps = V4L2_CAP_VIDEO_M2M_MPLANE |
350 V4L2_CAP_STREAMING;
351
352 snprintf(vfd_enc->name, sizeof(vfd_enc->name), "%s",
353 MTK_VCODEC_ENC_NAME);
354 video_set_drvdata(vfd_enc, dev);
355 dev->vfd_enc = vfd_enc;
356 platform_set_drvdata(pdev, dev);
357
358 dev->m2m_dev_enc = v4l2_m2m_init(&mtk_venc_m2m_ops);
359 if (IS_ERR((__force void *)dev->m2m_dev_enc)) {
360 mtk_v4l2_err("Failed to init mem2mem enc device");
361 ret = PTR_ERR((__force void *)dev->m2m_dev_enc);
362 goto err_enc_mem_init;
363 }
364
365 dev->encode_workqueue =
366 alloc_ordered_workqueue(MTK_VCODEC_ENC_NAME,
367 WQ_MEM_RECLAIM |
368 WQ_FREEZABLE);
369 if (!dev->encode_workqueue) {
370 mtk_v4l2_err("Failed to create encode workqueue");
371 ret = -EINVAL;
372 goto err_event_workq;
373 }
374
375 ret = video_register_device(vfd_enc, VFL_TYPE_GRABBER, 1);
376 if (ret) {
377 mtk_v4l2_err("Failed to register video device");
378 goto err_enc_reg;
379 }
380
381 /* Avoid the iommu eat big hunks */
382 dma_set_attr(DMA_ATTR_ALLOC_SINGLE_PAGES, &attrs);
383
384 mtk_v4l2_debug(0, "encoder registered as /dev/video%d",
385 vfd_enc->num);
386
387 return 0;
388
389err_enc_reg:
390 destroy_workqueue(dev->encode_workqueue);
391err_event_workq:
392 v4l2_m2m_release(dev->m2m_dev_enc);
393err_enc_mem_init:
394 video_unregister_device(vfd_enc);
395err_enc_alloc:
396 v4l2_device_unregister(&dev->v4l2_dev);
397err_res:
398 mtk_vcodec_release_enc_pm(dev);
399 return ret;
400}
401
402static const struct of_device_id mtk_vcodec_enc_match[] = {
403 {.compatible = "mediatek,mt8173-vcodec-enc",},
404 {},
405};
406MODULE_DEVICE_TABLE(of, mtk_vcodec_enc_match);
407
408static int mtk_vcodec_enc_remove(struct platform_device *pdev)
409{
410 struct mtk_vcodec_dev *dev = platform_get_drvdata(pdev);
411
412 mtk_v4l2_debug_enter();
413 flush_workqueue(dev->encode_workqueue);
414 destroy_workqueue(dev->encode_workqueue);
415 if (dev->m2m_dev_enc)
416 v4l2_m2m_release(dev->m2m_dev_enc);
417
418 if (dev->vfd_enc)
419 video_unregister_device(dev->vfd_enc);
420
421 v4l2_device_unregister(&dev->v4l2_dev);
422 mtk_vcodec_release_enc_pm(dev);
423 return 0;
424}
425
426static struct platform_driver mtk_vcodec_enc_driver = {
427 .probe = mtk_vcodec_probe,
428 .remove = mtk_vcodec_enc_remove,
429 .driver = {
430 .name = MTK_VCODEC_ENC_NAME,
431 .of_match_table = mtk_vcodec_enc_match,
432 },
433};
434
435module_platform_driver(mtk_vcodec_enc_driver);
436
437
438MODULE_LICENSE("GPL v2");
439MODULE_DESCRIPTION("Mediatek video codec V4L2 encoder driver");
diff --git a/drivers/media/platform/mtk-vcodec/mtk_vcodec_enc_pm.c b/drivers/media/platform/mtk-vcodec/mtk_vcodec_enc_pm.c
new file mode 100644
index 000000000000..3e73e9db781f
--- /dev/null
+++ b/drivers/media/platform/mtk-vcodec/mtk_vcodec_enc_pm.c
@@ -0,0 +1,137 @@
1/*
2* Copyright (c) 2016 MediaTek Inc.
3* Author: Tiffany Lin <tiffany.lin@mediatek.com>
4*
5* This program is free software; you can redistribute it and/or modify
6* it under the terms of the GNU General Public License version 2 as
7* published by the Free Software Foundation.
8*
9* This program is distributed in the hope that it will be useful,
10* but WITHOUT ANY WARRANTY; without even the implied warranty of
11* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12* GNU General Public License for more details.
13*/
14
15#include <linux/clk.h>
16#include <linux/of_address.h>
17#include <linux/of_platform.h>
18#include <linux/pm_runtime.h>
19#include <soc/mediatek/smi.h>
20
21#include "mtk_vcodec_enc_pm.h"
22#include "mtk_vcodec_util.h"
23#include "mtk_vpu.h"
24
25
26int mtk_vcodec_init_enc_pm(struct mtk_vcodec_dev *mtkdev)
27{
28 struct device_node *node;
29 struct platform_device *pdev;
30 struct device *dev;
31 struct mtk_vcodec_pm *pm;
32 int ret = 0;
33
34 pdev = mtkdev->plat_dev;
35 pm = &mtkdev->pm;
36 memset(pm, 0, sizeof(struct mtk_vcodec_pm));
37 pm->mtkdev = mtkdev;
38 pm->dev = &pdev->dev;
39 dev = &pdev->dev;
40
41 node = of_parse_phandle(dev->of_node, "mediatek,larb", 0);
42 if (!node) {
43 mtk_v4l2_err("no mediatek,larb found");
44 return -1;
45 }
46 pdev = of_find_device_by_node(node);
47 if (!pdev) {
48 mtk_v4l2_err("no mediatek,larb device found");
49 return -1;
50 }
51 pm->larbvenc = &pdev->dev;
52
53 node = of_parse_phandle(dev->of_node, "mediatek,larb", 1);
54 if (!node) {
55 mtk_v4l2_err("no mediatek,larb found");
56 return -1;
57 }
58
59 pdev = of_find_device_by_node(node);
60 if (!pdev) {
61 mtk_v4l2_err("no mediatek,larb device found");
62 return -1;
63 }
64
65 pm->larbvenclt = &pdev->dev;
66 pdev = mtkdev->plat_dev;
67 pm->dev = &pdev->dev;
68
69 pm->vencpll_d2 = devm_clk_get(&pdev->dev, "venc_sel_src");
70 if (IS_ERR(pm->vencpll_d2)) {
71 mtk_v4l2_err("devm_clk_get vencpll_d2 fail");
72 ret = PTR_ERR(pm->vencpll_d2);
73 }
74
75 pm->venc_sel = devm_clk_get(&pdev->dev, "venc_sel");
76 if (IS_ERR(pm->venc_sel)) {
77 mtk_v4l2_err("devm_clk_get venc_sel fail");
78 ret = PTR_ERR(pm->venc_sel);
79 }
80
81 pm->univpll1_d2 = devm_clk_get(&pdev->dev, "venc_lt_sel_src");
82 if (IS_ERR(pm->univpll1_d2)) {
83 mtk_v4l2_err("devm_clk_get univpll1_d2 fail");
84 ret = PTR_ERR(pm->univpll1_d2);
85 }
86
87 pm->venc_lt_sel = devm_clk_get(&pdev->dev, "venc_lt_sel");
88 if (IS_ERR(pm->venc_lt_sel)) {
89 mtk_v4l2_err("devm_clk_get venc_lt_sel fail");
90 ret = PTR_ERR(pm->venc_lt_sel);
91 }
92
93 return ret;
94}
95
96void mtk_vcodec_release_enc_pm(struct mtk_vcodec_dev *mtkdev)
97{
98}
99
100
101void mtk_vcodec_enc_clock_on(struct mtk_vcodec_pm *pm)
102{
103 int ret;
104
105 ret = clk_prepare_enable(pm->venc_sel);
106 if (ret)
107 mtk_v4l2_err("clk_prepare_enable fail %d", ret);
108
109 ret = clk_set_parent(pm->venc_sel, pm->vencpll_d2);
110 if (ret)
111 mtk_v4l2_err("clk_set_parent fail %d", ret);
112
113 ret = clk_prepare_enable(pm->venc_lt_sel);
114 if (ret)
115 mtk_v4l2_err("clk_prepare_enable fail %d", ret);
116
117 ret = clk_set_parent(pm->venc_lt_sel, pm->univpll1_d2);
118 if (ret)
119 mtk_v4l2_err("clk_set_parent fail %d", ret);
120
121 ret = mtk_smi_larb_get(pm->larbvenc);
122 if (ret)
123 mtk_v4l2_err("mtk_smi_larb_get larb3 fail %d", ret);
124
125 ret = mtk_smi_larb_get(pm->larbvenclt);
126 if (ret)
127 mtk_v4l2_err("mtk_smi_larb_get larb4 fail %d", ret);
128
129}
130
131void mtk_vcodec_enc_clock_off(struct mtk_vcodec_pm *pm)
132{
133 mtk_smi_larb_put(pm->larbvenc);
134 mtk_smi_larb_put(pm->larbvenclt);
135 clk_disable_unprepare(pm->venc_lt_sel);
136 clk_disable_unprepare(pm->venc_sel);
137}
diff --git a/drivers/media/platform/mtk-vcodec/mtk_vcodec_enc_pm.h b/drivers/media/platform/mtk-vcodec/mtk_vcodec_enc_pm.h
new file mode 100644
index 000000000000..f32167138976
--- /dev/null
+++ b/drivers/media/platform/mtk-vcodec/mtk_vcodec_enc_pm.h
@@ -0,0 +1,26 @@
1/*
2* Copyright (c) 2016 MediaTek Inc.
3* Author: Tiffany Lin <tiffany.lin@mediatek.com>
4*
5* This program is free software; you can redistribute it and/or modify
6* it under the terms of the GNU General Public License version 2 as
7* published by the Free Software Foundation.
8*
9* This program is distributed in the hope that it will be useful,
10* but WITHOUT ANY WARRANTY; without even the implied warranty of
11* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12* GNU General Public License for more details.
13*/
14
15#ifndef _MTK_VCODEC_ENC_PM_H_
16#define _MTK_VCODEC_ENC_PM_H_
17
18#include "mtk_vcodec_drv.h"
19
20int mtk_vcodec_init_enc_pm(struct mtk_vcodec_dev *dev);
21void mtk_vcodec_release_enc_pm(struct mtk_vcodec_dev *dev);
22
23void mtk_vcodec_enc_clock_on(struct mtk_vcodec_pm *pm);
24void mtk_vcodec_enc_clock_off(struct mtk_vcodec_pm *pm);
25
26#endif /* _MTK_VCODEC_ENC_PM_H_ */
diff --git a/drivers/media/platform/mtk-vcodec/mtk_vcodec_intr.c b/drivers/media/platform/mtk-vcodec/mtk_vcodec_intr.c
new file mode 100644
index 000000000000..52e7e5c9afa0
--- /dev/null
+++ b/drivers/media/platform/mtk-vcodec/mtk_vcodec_intr.c
@@ -0,0 +1,54 @@
1/*
2* Copyright (c) 2016 MediaTek Inc.
3* Author: Tiffany Lin <tiffany.lin@mediatek.com>
4*
5* This program is free software; you can redistribute it and/or modify
6* it under the terms of the GNU General Public License version 2 as
7* published by the Free Software Foundation.
8*
9* This program is distributed in the hope that it will be useful,
10* but WITHOUT ANY WARRANTY; without even the implied warranty of
11* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12* GNU General Public License for more details.
13*/
14
15#include <linux/errno.h>
16#include <linux/wait.h>
17
18#include "mtk_vcodec_drv.h"
19#include "mtk_vcodec_intr.h"
20#include "mtk_vcodec_util.h"
21
22int mtk_vcodec_wait_for_done_ctx(struct mtk_vcodec_ctx *ctx, int command,
23 unsigned int timeout_ms)
24{
25 wait_queue_head_t *waitqueue;
26 long timeout_jiff, ret;
27 int status = 0;
28
29 waitqueue = (wait_queue_head_t *)&ctx->queue;
30 timeout_jiff = msecs_to_jiffies(timeout_ms);
31
32 ret = wait_event_interruptible_timeout(*waitqueue,
33 (ctx->int_cond &&
34 (ctx->int_type == command)),
35 timeout_jiff);
36
37 if (!ret) {
38 status = -1; /* timeout */
39 mtk_v4l2_err("[%d] cmd=%d, ctx->type=%d, wait_event_interruptible_timeout time=%ums out %d %d!",
40 ctx->id, ctx->type, command, timeout_ms,
41 ctx->int_cond, ctx->int_type);
42 } else if (-ERESTARTSYS == ret) {
43 mtk_v4l2_err("[%d] cmd=%d, ctx->type=%d, wait_event_interruptible_timeout interrupted by a signal %d %d",
44 ctx->id, ctx->type, command, ctx->int_cond,
45 ctx->int_type);
46 status = -1;
47 }
48
49 ctx->int_cond = 0;
50 ctx->int_type = 0;
51
52 return status;
53}
54EXPORT_SYMBOL(mtk_vcodec_wait_for_done_ctx);
diff --git a/drivers/media/platform/mtk-vcodec/mtk_vcodec_intr.h b/drivers/media/platform/mtk-vcodec/mtk_vcodec_intr.h
new file mode 100644
index 000000000000..33e890f5aa9c
--- /dev/null
+++ b/drivers/media/platform/mtk-vcodec/mtk_vcodec_intr.h
@@ -0,0 +1,27 @@
1/*
2* Copyright (c) 2016 MediaTek Inc.
3* Author: Tiffany Lin <tiffany.lin@mediatek.com>
4*
5* This program is free software; you can redistribute it and/or modify
6* it under the terms of the GNU General Public License version 2 as
7* published by the Free Software Foundation.
8*
9* This program is distributed in the hope that it will be useful,
10* but WITHOUT ANY WARRANTY; without even the implied warranty of
11* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12* GNU General Public License for more details.
13*/
14
15#ifndef _MTK_VCODEC_INTR_H_
16#define _MTK_VCODEC_INTR_H_
17
18#define MTK_INST_IRQ_RECEIVED 0x1
19#define MTK_INST_WORK_THREAD_ABORT_DONE 0x2
20
21struct mtk_vcodec_ctx;
22
23/* timeout is ms */
24int mtk_vcodec_wait_for_done_ctx(struct mtk_vcodec_ctx *data, int command,
25 unsigned int timeout_ms);
26
27#endif /* _MTK_VCODEC_INTR_H_ */
diff --git a/drivers/media/platform/mtk-vcodec/mtk_vcodec_util.c b/drivers/media/platform/mtk-vcodec/mtk_vcodec_util.c
new file mode 100644
index 000000000000..5e3651372a3c
--- /dev/null
+++ b/drivers/media/platform/mtk-vcodec/mtk_vcodec_util.c
@@ -0,0 +1,94 @@
1/*
2* Copyright (c) 2016 MediaTek Inc.
3* Author: PC Chen <pc.chen@mediatek.com>
4* Tiffany Lin <tiffany.lin@mediatek.com>
5*
6* This program is free software; you can redistribute it and/or modify
7* it under the terms of the GNU General Public License version 2 as
8* published by the Free Software Foundation.
9*
10* This program is distributed in the hope that it will be useful,
11* but WITHOUT ANY WARRANTY; without even the implied warranty of
12* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13* GNU General Public License for more details.
14*/
15
16#include <linux/module.h>
17
18#include "mtk_vcodec_drv.h"
19#include "mtk_vcodec_util.h"
20#include "mtk_vpu.h"
21
22/* For encoder, this will enable logs in venc/*/
23bool mtk_vcodec_dbg;
24EXPORT_SYMBOL(mtk_vcodec_dbg);
25
26/* The log level of v4l2 encoder or decoder driver.
27 * That is, files under mtk-vcodec/.
28 */
29int mtk_v4l2_dbg_level;
30EXPORT_SYMBOL(mtk_v4l2_dbg_level);
31
32void __iomem *mtk_vcodec_get_reg_addr(struct mtk_vcodec_ctx *data,
33 unsigned int reg_idx)
34{
35 struct mtk_vcodec_ctx *ctx = (struct mtk_vcodec_ctx *)data;
36
37 if (!data || reg_idx >= NUM_MAX_VCODEC_REG_BASE) {
38 mtk_v4l2_err("Invalid arguments, reg_idx=%d", reg_idx);
39 return NULL;
40 }
41 return ctx->dev->reg_base[reg_idx];
42}
43EXPORT_SYMBOL(mtk_vcodec_get_reg_addr);
44
45int mtk_vcodec_mem_alloc(struct mtk_vcodec_ctx *data,
46 struct mtk_vcodec_mem *mem)
47{
48 unsigned long size = mem->size;
49 struct mtk_vcodec_ctx *ctx = (struct mtk_vcodec_ctx *)data;
50 struct device *dev = &ctx->dev->plat_dev->dev;
51
52 mem->va = dma_alloc_coherent(dev, size, &mem->dma_addr, GFP_KERNEL);
53
54 if (!mem->va) {
55 mtk_v4l2_err("%s dma_alloc size=%ld failed!", dev_name(dev),
56 size);
57 return -ENOMEM;
58 }
59
60 memset(mem->va, 0, size);
61
62 mtk_v4l2_debug(3, "[%d] - va = %p", ctx->id, mem->va);
63 mtk_v4l2_debug(3, "[%d] - dma = 0x%lx", ctx->id,
64 (unsigned long)mem->dma_addr);
65 mtk_v4l2_debug(3, "[%d] size = 0x%lx", ctx->id, size);
66
67 return 0;
68}
69EXPORT_SYMBOL(mtk_vcodec_mem_alloc);
70
71void mtk_vcodec_mem_free(struct mtk_vcodec_ctx *data,
72 struct mtk_vcodec_mem *mem)
73{
74 unsigned long size = mem->size;
75 struct mtk_vcodec_ctx *ctx = (struct mtk_vcodec_ctx *)data;
76 struct device *dev = &ctx->dev->plat_dev->dev;
77
78 if (!mem->va) {
79 mtk_v4l2_err("%s dma_free size=%ld failed!", dev_name(dev),
80 size);
81 return;
82 }
83
84 dma_free_coherent(dev, size, mem->va, mem->dma_addr);
85 mem->va = NULL;
86 mem->dma_addr = 0;
87 mem->size = 0;
88
89 mtk_v4l2_debug(3, "[%d] - va = %p", ctx->id, mem->va);
90 mtk_v4l2_debug(3, "[%d] - dma = 0x%lx", ctx->id,
91 (unsigned long)mem->dma_addr);
92 mtk_v4l2_debug(3, "[%d] size = 0x%lx", ctx->id, size);
93}
94EXPORT_SYMBOL(mtk_vcodec_mem_free);
diff --git a/drivers/media/platform/mtk-vcodec/mtk_vcodec_util.h b/drivers/media/platform/mtk-vcodec/mtk_vcodec_util.h
new file mode 100644
index 000000000000..d6345fc04840
--- /dev/null
+++ b/drivers/media/platform/mtk-vcodec/mtk_vcodec_util.h
@@ -0,0 +1,87 @@
1/*
2* Copyright (c) 2016 MediaTek Inc.
3* Author: PC Chen <pc.chen@mediatek.com>
4* Tiffany Lin <tiffany.lin@mediatek.com>
5*
6* This program is free software; you can redistribute it and/or modify
7* it under the terms of the GNU General Public License version 2 as
8* published by the Free Software Foundation.
9*
10* This program is distributed in the hope that it will be useful,
11* but WITHOUT ANY WARRANTY; without even the implied warranty of
12* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13* GNU General Public License for more details.
14*/
15
16#ifndef _MTK_VCODEC_UTIL_H_
17#define _MTK_VCODEC_UTIL_H_
18
19#include <linux/types.h>
20#include <linux/dma-direction.h>
21
22struct mtk_vcodec_mem {
23 size_t size;
24 void *va;
25 dma_addr_t dma_addr;
26};
27
28struct mtk_vcodec_ctx;
29
30extern int mtk_v4l2_dbg_level;
31extern bool mtk_vcodec_dbg;
32
33#define DEBUG 1
34
35#if defined(DEBUG)
36
37#define mtk_v4l2_debug(level, fmt, args...) \
38 do { \
39 if (mtk_v4l2_dbg_level >= level) \
40 pr_info("[MTK_V4L2] level=%d %s(),%d: " fmt "\n",\
41 level, __func__, __LINE__, ##args); \
42 } while (0)
43
44#define mtk_v4l2_err(fmt, args...) \
45 pr_err("[MTK_V4L2][ERROR] %s:%d: " fmt "\n", __func__, __LINE__, \
46 ##args)
47
48
49#define mtk_v4l2_debug_enter() mtk_v4l2_debug(3, "+")
50#define mtk_v4l2_debug_leave() mtk_v4l2_debug(3, "-")
51
52#define mtk_vcodec_debug(h, fmt, args...) \
53 do { \
54 if (mtk_vcodec_dbg) \
55 pr_info("[MTK_VCODEC][%d]: %s() " fmt "\n", \
56 ((struct mtk_vcodec_ctx *)h->ctx)->id, \
57 __func__, ##args); \
58 } while (0)
59
60#define mtk_vcodec_err(h, fmt, args...) \
61 pr_err("[MTK_VCODEC][ERROR][%d]: %s() " fmt "\n", \
62 ((struct mtk_vcodec_ctx *)h->ctx)->id, __func__, ##args)
63
64#define mtk_vcodec_debug_enter(h) mtk_vcodec_debug(h, "+")
65#define mtk_vcodec_debug_leave(h) mtk_vcodec_debug(h, "-")
66
67#else
68
69#define mtk_v4l2_debug(level, fmt, args...)
70#define mtk_v4l2_err(fmt, args...)
71#define mtk_v4l2_debug_enter()
72#define mtk_v4l2_debug_leave()
73
74#define mtk_vcodec_debug(h, fmt, args...)
75#define mtk_vcodec_err(h, fmt, args...)
76#define mtk_vcodec_debug_enter(h)
77#define mtk_vcodec_debug_leave(h)
78
79#endif
80
81void __iomem *mtk_vcodec_get_reg_addr(struct mtk_vcodec_ctx *data,
82 unsigned int reg_idx);
83int mtk_vcodec_mem_alloc(struct mtk_vcodec_ctx *data,
84 struct mtk_vcodec_mem *mem);
85void mtk_vcodec_mem_free(struct mtk_vcodec_ctx *data,
86 struct mtk_vcodec_mem *mem);
87#endif /* _MTK_VCODEC_UTIL_H_ */
diff --git a/drivers/media/platform/mtk-vcodec/venc/venc_h264_if.c b/drivers/media/platform/mtk-vcodec/venc/venc_h264_if.c
new file mode 100644
index 000000000000..9a600525b3c1
--- /dev/null
+++ b/drivers/media/platform/mtk-vcodec/venc/venc_h264_if.c
@@ -0,0 +1,679 @@
1/*
2 * Copyright (c) 2016 MediaTek Inc.
3 * Author: Jungchang Tsao <jungchang.tsao@mediatek.com>
4 * Daniel Hsiao <daniel.hsiao@mediatek.com>
5 * PoChun Lin <pochun.lin@mediatek.com>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 */
17
18#include <linux/interrupt.h>
19#include <linux/kernel.h>
20#include <linux/slab.h>
21
22#include "../mtk_vcodec_drv.h"
23#include "../mtk_vcodec_util.h"
24#include "../mtk_vcodec_intr.h"
25#include "../mtk_vcodec_enc.h"
26#include "../mtk_vcodec_enc_pm.h"
27#include "../venc_drv_base.h"
28#include "../venc_ipi_msg.h"
29#include "../venc_vpu_if.h"
30#include "mtk_vpu.h"
31
32static const char h264_filler_marker[] = {0x0, 0x0, 0x0, 0x1, 0xc};
33
34#define H264_FILLER_MARKER_SIZE ARRAY_SIZE(h264_filler_marker)
35#define VENC_PIC_BITSTREAM_BYTE_CNT 0x0098
36
37/**
38 * enum venc_h264_vpu_work_buf - h264 encoder buffer index
39 */
40enum venc_h264_vpu_work_buf {
41 VENC_H264_VPU_WORK_BUF_RC_INFO,
42 VENC_H264_VPU_WORK_BUF_RC_CODE,
43 VENC_H264_VPU_WORK_BUF_REC_LUMA,
44 VENC_H264_VPU_WORK_BUF_REC_CHROMA,
45 VENC_H264_VPU_WORK_BUF_REF_LUMA,
46 VENC_H264_VPU_WORK_BUF_REF_CHROMA,
47 VENC_H264_VPU_WORK_BUF_MV_INFO_1,
48 VENC_H264_VPU_WORK_BUF_MV_INFO_2,
49 VENC_H264_VPU_WORK_BUF_SKIP_FRAME,
50 VENC_H264_VPU_WORK_BUF_MAX,
51};
52
53/**
54 * enum venc_h264_bs_mode - for bs_mode argument in h264_enc_vpu_encode
55 */
56enum venc_h264_bs_mode {
57 H264_BS_MODE_SPS,
58 H264_BS_MODE_PPS,
59 H264_BS_MODE_FRAME,
60};
61
62/*
63 * struct venc_h264_vpu_config - Structure for h264 encoder configuration
64 * @input_fourcc: input fourcc
65 * @bitrate: target bitrate (in bps)
66 * @pic_w: picture width. Picture size is visible stream resolution, in pixels,
67 * to be used for display purposes; must be smaller or equal to buffer
68 * size.
69 * @pic_h: picture height
70 * @buf_w: buffer width. Buffer size is stream resolution in pixels aligned to
71 * hardware requirements.
72 * @buf_h: buffer height
73 * @gop_size: group of picture size (idr frame)
74 * @intra_period: intra frame period
75 * @framerate: frame rate in fps
76 * @profile: as specified in standard
77 * @level: as specified in standard
78 * @wfd: WFD mode 1:on, 0:off
79 */
80struct venc_h264_vpu_config {
81 u32 input_fourcc;
82 u32 bitrate;
83 u32 pic_w;
84 u32 pic_h;
85 u32 buf_w;
86 u32 buf_h;
87 u32 gop_size;
88 u32 intra_period;
89 u32 framerate;
90 u32 profile;
91 u32 level;
92 u32 wfd;
93};
94
95/*
96 * struct venc_h264_vpu_buf - Structure for buffer information
97 * @align: buffer alignment (in bytes)
98 * @iova: IO virtual address
99 * @vpua: VPU side memory addr which is used by RC_CODE
100 * @size: buffer size (in bytes)
101 */
102struct venc_h264_vpu_buf {
103 u32 align;
104 u32 iova;
105 u32 vpua;
106 u32 size;
107};
108
109/*
110 * struct venc_h264_vsi - Structure for VPU driver control and info share
111 * This structure is allocated in VPU side and shared to AP side.
112 * @config: h264 encoder configuration
113 * @work_bufs: working buffer information in VPU side
114 * The work_bufs here is for storing the 'size' info shared to AP side.
115 * The similar item in struct venc_h264_inst is for memory allocation
116 * in AP side. The AP driver will copy the 'size' from here to the one in
117 * struct mtk_vcodec_mem, then invoke mtk_vcodec_mem_alloc to allocate
118 * the buffer. After that, bypass the 'dma_addr' to the 'iova' field here for
119 * register setting in VPU side.
120 */
121struct venc_h264_vsi {
122 struct venc_h264_vpu_config config;
123 struct venc_h264_vpu_buf work_bufs[VENC_H264_VPU_WORK_BUF_MAX];
124};
125
126/*
127 * struct venc_h264_inst - h264 encoder AP driver instance
128 * @hw_base: h264 encoder hardware register base
129 * @work_bufs: working buffer
130 * @pps_buf: buffer to store the pps bitstream
131 * @work_buf_allocated: working buffer allocated flag
132 * @frm_cnt: encoded frame count
133 * @prepend_hdr: when the v4l2 layer send VENC_SET_PARAM_PREPEND_HEADER cmd
134 * through h264_enc_set_param interface, it will set this flag and prepend the
135 * sps/pps in h264_enc_encode function.
136 * @vpu_inst: VPU instance to exchange information between AP and VPU
137 * @vsi: driver structure allocated by VPU side and shared to AP side for
138 * control and info share
139 * @ctx: context for v4l2 layer integration
140 */
141struct venc_h264_inst {
142 void __iomem *hw_base;
143 struct mtk_vcodec_mem work_bufs[VENC_H264_VPU_WORK_BUF_MAX];
144 struct mtk_vcodec_mem pps_buf;
145 bool work_buf_allocated;
146 unsigned int frm_cnt;
147 unsigned int prepend_hdr;
148 struct venc_vpu_inst vpu_inst;
149 struct venc_h264_vsi *vsi;
150 struct mtk_vcodec_ctx *ctx;
151};
152
153static inline void h264_write_reg(struct venc_h264_inst *inst, u32 addr,
154 u32 val)
155{
156 writel(val, inst->hw_base + addr);
157}
158
159static inline u32 h264_read_reg(struct venc_h264_inst *inst, u32 addr)
160{
161 return readl(inst->hw_base + addr);
162}
163
164static unsigned int h264_get_profile(struct venc_h264_inst *inst,
165 unsigned int profile)
166{
167 switch (profile) {
168 case V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE:
169 return 66;
170 case V4L2_MPEG_VIDEO_H264_PROFILE_MAIN:
171 return 77;
172 case V4L2_MPEG_VIDEO_H264_PROFILE_HIGH:
173 return 100;
174 case V4L2_MPEG_VIDEO_H264_PROFILE_CONSTRAINED_BASELINE:
175 mtk_vcodec_err(inst, "unsupported CONSTRAINED_BASELINE");
176 return 0;
177 case V4L2_MPEG_VIDEO_H264_PROFILE_EXTENDED:
178 mtk_vcodec_err(inst, "unsupported EXTENDED");
179 return 0;
180 default:
181 mtk_vcodec_debug(inst, "unsupported profile %d", profile);
182 return 100;
183 }
184}
185
186static unsigned int h264_get_level(struct venc_h264_inst *inst,
187 unsigned int level)
188{
189 switch (level) {
190 case V4L2_MPEG_VIDEO_H264_LEVEL_1B:
191 mtk_vcodec_err(inst, "unsupported 1B");
192 return 0;
193 case V4L2_MPEG_VIDEO_H264_LEVEL_1_0:
194 return 10;
195 case V4L2_MPEG_VIDEO_H264_LEVEL_1_1:
196 return 11;
197 case V4L2_MPEG_VIDEO_H264_LEVEL_1_2:
198 return 12;
199 case V4L2_MPEG_VIDEO_H264_LEVEL_1_3:
200 return 13;
201 case V4L2_MPEG_VIDEO_H264_LEVEL_2_0:
202 return 20;
203 case V4L2_MPEG_VIDEO_H264_LEVEL_2_1:
204 return 21;
205 case V4L2_MPEG_VIDEO_H264_LEVEL_2_2:
206 return 22;
207 case V4L2_MPEG_VIDEO_H264_LEVEL_3_0:
208 return 30;
209 case V4L2_MPEG_VIDEO_H264_LEVEL_3_1:
210 return 31;
211 case V4L2_MPEG_VIDEO_H264_LEVEL_3_2:
212 return 32;
213 case V4L2_MPEG_VIDEO_H264_LEVEL_4_0:
214 return 40;
215 case V4L2_MPEG_VIDEO_H264_LEVEL_4_1:
216 return 41;
217 default:
218 mtk_vcodec_debug(inst, "unsupported level %d", level);
219 return 31;
220 }
221}
222
223static void h264_enc_free_work_buf(struct venc_h264_inst *inst)
224{
225 int i;
226
227 mtk_vcodec_debug_enter(inst);
228
229 /* Except the SKIP_FRAME buffers,
230 * other buffers need to be freed by AP.
231 */
232 for (i = 0; i < VENC_H264_VPU_WORK_BUF_MAX; i++) {
233 if (i != VENC_H264_VPU_WORK_BUF_SKIP_FRAME)
234 mtk_vcodec_mem_free(inst->ctx, &inst->work_bufs[i]);
235 }
236
237 mtk_vcodec_mem_free(inst->ctx, &inst->pps_buf);
238
239 mtk_vcodec_debug_leave(inst);
240}
241
242static int h264_enc_alloc_work_buf(struct venc_h264_inst *inst)
243{
244 int i;
245 int ret = 0;
246 struct venc_h264_vpu_buf *wb = inst->vsi->work_bufs;
247
248 mtk_vcodec_debug_enter(inst);
249
250 for (i = 0; i < VENC_H264_VPU_WORK_BUF_MAX; i++) {
251 /*
252 * This 'wb' structure is set by VPU side and shared to AP for
253 * buffer allocation and IO virtual addr mapping. For most of
254 * the buffers, AP will allocate the buffer according to 'size'
255 * field and store the IO virtual addr in 'iova' field. There
256 * are two exceptions:
257 * (1) RC_CODE buffer, it's pre-allocated in the VPU side, and
258 * save the VPU addr in the 'vpua' field. The AP will translate
259 * the VPU addr to the corresponding IO virtual addr and store
260 * in 'iova' field for reg setting in VPU side.
261 * (2) SKIP_FRAME buffer, it's pre-allocated in the VPU side,
262 * and save the VPU addr in the 'vpua' field. The AP will
263 * translate the VPU addr to the corresponding AP side virtual
264 * address and do some memcpy access to move to bitstream buffer
265 * assigned by v4l2 layer.
266 */
267 inst->work_bufs[i].size = wb[i].size;
268 if (i == VENC_H264_VPU_WORK_BUF_SKIP_FRAME) {
269 inst->work_bufs[i].va = vpu_mapping_dm_addr(
270 inst->vpu_inst.dev, wb[i].vpua);
271 inst->work_bufs[i].dma_addr = 0;
272 } else {
273 ret = mtk_vcodec_mem_alloc(inst->ctx,
274 &inst->work_bufs[i]);
275 if (ret) {
276 mtk_vcodec_err(inst,
277 "cannot allocate buf %d", i);
278 goto err_alloc;
279 }
280 /*
281 * This RC_CODE is pre-allocated by VPU and saved in VPU
282 * addr. So we need use memcpy to copy RC_CODE from VPU
283 * addr into IO virtual addr in 'iova' field for reg
284 * setting in VPU side.
285 */
286 if (i == VENC_H264_VPU_WORK_BUF_RC_CODE) {
287 void *tmp_va;
288
289 tmp_va = vpu_mapping_dm_addr(inst->vpu_inst.dev,
290 wb[i].vpua);
291 memcpy(inst->work_bufs[i].va, tmp_va,
292 wb[i].size);
293 }
294 }
295 wb[i].iova = inst->work_bufs[i].dma_addr;
296
297 mtk_vcodec_debug(inst,
298 "work_buf[%d] va=0x%p iova=%pad size=%zu",
299 i, inst->work_bufs[i].va,
300 &inst->work_bufs[i].dma_addr,
301 inst->work_bufs[i].size);
302 }
303
304 /* the pps_buf is used by AP side only */
305 inst->pps_buf.size = 128;
306 ret = mtk_vcodec_mem_alloc(inst->ctx, &inst->pps_buf);
307 if (ret) {
308 mtk_vcodec_err(inst, "cannot allocate pps_buf");
309 goto err_alloc;
310 }
311
312 mtk_vcodec_debug_leave(inst);
313
314 return ret;
315
316err_alloc:
317 h264_enc_free_work_buf(inst);
318
319 return ret;
320}
321
322static unsigned int h264_enc_wait_venc_done(struct venc_h264_inst *inst)
323{
324 unsigned int irq_status = 0;
325 struct mtk_vcodec_ctx *ctx = (struct mtk_vcodec_ctx *)inst->ctx;
326
327 if (!mtk_vcodec_wait_for_done_ctx(ctx, MTK_INST_IRQ_RECEIVED,
328 WAIT_INTR_TIMEOUT_MS)) {
329 irq_status = ctx->irq_status;
330 mtk_vcodec_debug(inst, "irq_status %x <-", irq_status);
331 }
332 return irq_status;
333}
334
335static int h264_encode_sps(struct venc_h264_inst *inst,
336 struct mtk_vcodec_mem *bs_buf,
337 unsigned int *bs_size)
338{
339 int ret = 0;
340 unsigned int irq_status;
341
342 mtk_vcodec_debug_enter(inst);
343
344 ret = vpu_enc_encode(&inst->vpu_inst, H264_BS_MODE_SPS, NULL,
345 bs_buf, bs_size);
346 if (ret)
347 return ret;
348
349 irq_status = h264_enc_wait_venc_done(inst);
350 if (irq_status != MTK_VENC_IRQ_STATUS_SPS) {
351 mtk_vcodec_err(inst, "expect irq status %d",
352 MTK_VENC_IRQ_STATUS_SPS);
353 return -EINVAL;
354 }
355
356 *bs_size = h264_read_reg(inst, VENC_PIC_BITSTREAM_BYTE_CNT);
357 mtk_vcodec_debug(inst, "bs size %d <-", *bs_size);
358
359 return ret;
360}
361
362static int h264_encode_pps(struct venc_h264_inst *inst,
363 struct mtk_vcodec_mem *bs_buf,
364 unsigned int *bs_size)
365{
366 int ret = 0;
367 unsigned int irq_status;
368
369 mtk_vcodec_debug_enter(inst);
370
371 ret = vpu_enc_encode(&inst->vpu_inst, H264_BS_MODE_PPS, NULL,
372 bs_buf, bs_size);
373 if (ret)
374 return ret;
375
376 irq_status = h264_enc_wait_venc_done(inst);
377 if (irq_status != MTK_VENC_IRQ_STATUS_PPS) {
378 mtk_vcodec_err(inst, "expect irq status %d",
379 MTK_VENC_IRQ_STATUS_PPS);
380 return -EINVAL;
381 }
382
383 *bs_size = h264_read_reg(inst, VENC_PIC_BITSTREAM_BYTE_CNT);
384 mtk_vcodec_debug(inst, "bs size %d <-", *bs_size);
385
386 return ret;
387}
388
389static int h264_encode_header(struct venc_h264_inst *inst,
390 struct mtk_vcodec_mem *bs_buf,
391 unsigned int *bs_size)
392{
393 int ret = 0;
394 unsigned int bs_size_sps;
395 unsigned int bs_size_pps;
396
397 ret = h264_encode_sps(inst, bs_buf, &bs_size_sps);
398 if (ret)
399 return ret;
400
401 ret = h264_encode_pps(inst, &inst->pps_buf, &bs_size_pps);
402 if (ret)
403 return ret;
404
405 memcpy(bs_buf->va + bs_size_sps, inst->pps_buf.va, bs_size_pps);
406 *bs_size = bs_size_sps + bs_size_pps;
407
408 return ret;
409}
410
411static int h264_encode_frame(struct venc_h264_inst *inst,
412 struct venc_frm_buf *frm_buf,
413 struct mtk_vcodec_mem *bs_buf,
414 unsigned int *bs_size)
415{
416 int ret = 0;
417 unsigned int irq_status;
418
419 mtk_vcodec_debug_enter(inst);
420
421 ret = vpu_enc_encode(&inst->vpu_inst, H264_BS_MODE_FRAME, frm_buf,
422 bs_buf, bs_size);
423 if (ret)
424 return ret;
425
426 /*
427 * skip frame case: The skip frame buffer is composed by vpu side only,
428 * it does not trigger the hw, so skip the wait interrupt operation.
429 */
430 if (inst->vpu_inst.state == VEN_IPI_MSG_ENC_STATE_SKIP) {
431 *bs_size = inst->vpu_inst.bs_size;
432 memcpy(bs_buf->va,
433 inst->work_bufs[VENC_H264_VPU_WORK_BUF_SKIP_FRAME].va,
434 *bs_size);
435 ++inst->frm_cnt;
436 return ret;
437 }
438
439 irq_status = h264_enc_wait_venc_done(inst);
440 if (irq_status != MTK_VENC_IRQ_STATUS_FRM) {
441 mtk_vcodec_err(inst, "irq_status=%d failed", irq_status);
442 return -EIO;
443 }
444
445 *bs_size = h264_read_reg(inst, VENC_PIC_BITSTREAM_BYTE_CNT);
446
447 ++inst->frm_cnt;
448 mtk_vcodec_debug(inst, "frm %d bs_size %d key_frm %d <-",
449 inst->frm_cnt, *bs_size, inst->vpu_inst.is_key_frm);
450
451 return ret;
452}
453
454static void h264_encode_filler(struct venc_h264_inst *inst, void *buf,
455 int size)
456{
457 unsigned char *p = buf;
458
459 if (size < H264_FILLER_MARKER_SIZE) {
460 mtk_vcodec_err(inst, "filler size too small %d", size);
461 return;
462 }
463
464 memcpy(p, h264_filler_marker, ARRAY_SIZE(h264_filler_marker));
465 size -= H264_FILLER_MARKER_SIZE;
466 p += H264_FILLER_MARKER_SIZE;
467 memset(p, 0xff, size);
468}
469
470static int h264_enc_init(struct mtk_vcodec_ctx *ctx, unsigned long *handle)
471{
472 int ret = 0;
473 struct venc_h264_inst *inst;
474
475 inst = kzalloc(sizeof(*inst), GFP_KERNEL);
476 if (!inst)
477 return -ENOMEM;
478
479 inst->ctx = ctx;
480 inst->vpu_inst.ctx = ctx;
481 inst->vpu_inst.dev = ctx->dev->vpu_plat_dev;
482 inst->vpu_inst.id = IPI_VENC_H264;
483 inst->hw_base = mtk_vcodec_get_reg_addr(inst->ctx, VENC_SYS);
484
485 mtk_vcodec_debug_enter(inst);
486
487 ret = vpu_enc_init(&inst->vpu_inst);
488
489 inst->vsi = (struct venc_h264_vsi *)inst->vpu_inst.vsi;
490
491 mtk_vcodec_debug_leave(inst);
492
493 if (ret)
494 kfree(inst);
495 else
496 (*handle) = (unsigned long)inst;
497
498 return ret;
499}
500
501static int h264_enc_encode(unsigned long handle,
502 enum venc_start_opt opt,
503 struct venc_frm_buf *frm_buf,
504 struct mtk_vcodec_mem *bs_buf,
505 struct venc_done_result *result)
506{
507 int ret = 0;
508 struct venc_h264_inst *inst = (struct venc_h264_inst *)handle;
509 struct mtk_vcodec_ctx *ctx = inst->ctx;
510
511 mtk_vcodec_debug(inst, "opt %d ->", opt);
512
513 enable_irq(ctx->dev->enc_irq);
514
515 switch (opt) {
516 case VENC_START_OPT_ENCODE_SEQUENCE_HEADER: {
517 unsigned int bs_size_hdr;
518
519 ret = h264_encode_header(inst, bs_buf, &bs_size_hdr);
520 if (ret)
521 goto encode_err;
522
523 result->bs_size = bs_size_hdr;
524 result->is_key_frm = false;
525 break;
526 }
527
528 case VENC_START_OPT_ENCODE_FRAME: {
529 int hdr_sz;
530 int hdr_sz_ext;
531 int filler_sz = 0;
532 const int bs_alignment = 128;
533 struct mtk_vcodec_mem tmp_bs_buf;
534 unsigned int bs_size_hdr;
535 unsigned int bs_size_frm;
536
537 if (!inst->prepend_hdr) {
538 ret = h264_encode_frame(inst, frm_buf, bs_buf,
539 &result->bs_size);
540 if (ret)
541 goto encode_err;
542 result->is_key_frm = inst->vpu_inst.is_key_frm;
543 break;
544 }
545
546 mtk_vcodec_debug(inst, "h264_encode_frame prepend SPS/PPS");
547
548 ret = h264_encode_header(inst, bs_buf, &bs_size_hdr);
549 if (ret)
550 goto encode_err;
551
552 hdr_sz = bs_size_hdr;
553 hdr_sz_ext = (hdr_sz & (bs_alignment - 1));
554 if (hdr_sz_ext) {
555 filler_sz = bs_alignment - hdr_sz_ext;
556 if (hdr_sz_ext + H264_FILLER_MARKER_SIZE > bs_alignment)
557 filler_sz += bs_alignment;
558 h264_encode_filler(inst, bs_buf->va + hdr_sz,
559 filler_sz);
560 }
561
562 tmp_bs_buf.va = bs_buf->va + hdr_sz + filler_sz;
563 tmp_bs_buf.dma_addr = bs_buf->dma_addr + hdr_sz + filler_sz;
564 tmp_bs_buf.size = bs_buf->size - (hdr_sz + filler_sz);
565
566 ret = h264_encode_frame(inst, frm_buf, &tmp_bs_buf,
567 &bs_size_frm);
568 if (ret)
569 goto encode_err;
570
571 result->bs_size = hdr_sz + filler_sz + bs_size_frm;
572
573 mtk_vcodec_debug(inst, "hdr %d filler %d frame %d bs %d",
574 hdr_sz, filler_sz, bs_size_frm,
575 result->bs_size);
576
577 inst->prepend_hdr = 0;
578 result->is_key_frm = inst->vpu_inst.is_key_frm;
579 break;
580 }
581
582 default:
583 mtk_vcodec_err(inst, "venc_start_opt %d not supported", opt);
584 ret = -EINVAL;
585 break;
586 }
587
588encode_err:
589
590 disable_irq(ctx->dev->enc_irq);
591 mtk_vcodec_debug(inst, "opt %d <-", opt);
592
593 return ret;
594}
595
596static int h264_enc_set_param(unsigned long handle,
597 enum venc_set_param_type type,
598 struct venc_enc_param *enc_prm)
599{
600 int ret = 0;
601 struct venc_h264_inst *inst = (struct venc_h264_inst *)handle;
602
603 mtk_vcodec_debug(inst, "->type=%d", type);
604
605 switch (type) {
606 case VENC_SET_PARAM_ENC:
607 inst->vsi->config.input_fourcc = enc_prm->input_yuv_fmt;
608 inst->vsi->config.bitrate = enc_prm->bitrate;
609 inst->vsi->config.pic_w = enc_prm->width;
610 inst->vsi->config.pic_h = enc_prm->height;
611 inst->vsi->config.buf_w = enc_prm->buf_width;
612 inst->vsi->config.buf_h = enc_prm->buf_height;
613 inst->vsi->config.gop_size = enc_prm->gop_size;
614 inst->vsi->config.framerate = enc_prm->frm_rate;
615 inst->vsi->config.intra_period = enc_prm->intra_period;
616 inst->vsi->config.profile =
617 h264_get_profile(inst, enc_prm->h264_profile);
618 inst->vsi->config.level =
619 h264_get_level(inst, enc_prm->h264_level);
620 inst->vsi->config.wfd = 0;
621 ret = vpu_enc_set_param(&inst->vpu_inst, type, enc_prm);
622 if (ret)
623 break;
624 if (inst->work_buf_allocated) {
625 h264_enc_free_work_buf(inst);
626 inst->work_buf_allocated = false;
627 }
628 ret = h264_enc_alloc_work_buf(inst);
629 if (ret)
630 break;
631 inst->work_buf_allocated = true;
632 break;
633
634 case VENC_SET_PARAM_PREPEND_HEADER:
635 inst->prepend_hdr = 1;
636 mtk_vcodec_debug(inst, "set prepend header mode");
637 break;
638
639 default:
640 ret = vpu_enc_set_param(&inst->vpu_inst, type, enc_prm);
641 break;
642 }
643
644 mtk_vcodec_debug_leave(inst);
645
646 return ret;
647}
648
649static int h264_enc_deinit(unsigned long handle)
650{
651 int ret = 0;
652 struct venc_h264_inst *inst = (struct venc_h264_inst *)handle;
653
654 mtk_vcodec_debug_enter(inst);
655
656 ret = vpu_enc_deinit(&inst->vpu_inst);
657
658 if (inst->work_buf_allocated)
659 h264_enc_free_work_buf(inst);
660
661 mtk_vcodec_debug_leave(inst);
662 kfree(inst);
663
664 return ret;
665}
666
667static struct venc_common_if venc_h264_if = {
668 h264_enc_init,
669 h264_enc_encode,
670 h264_enc_set_param,
671 h264_enc_deinit,
672};
673
674struct venc_common_if *get_h264_enc_comm_if(void);
675
676struct venc_common_if *get_h264_enc_comm_if(void)
677{
678 return &venc_h264_if;
679}
diff --git a/drivers/media/platform/mtk-vcodec/venc/venc_vp8_if.c b/drivers/media/platform/mtk-vcodec/venc/venc_vp8_if.c
new file mode 100644
index 000000000000..60bbcd2a0510
--- /dev/null
+++ b/drivers/media/platform/mtk-vcodec/venc/venc_vp8_if.c
@@ -0,0 +1,486 @@
1/*
2 * Copyright (c) 2016 MediaTek Inc.
3 * Author: Daniel Hsiao <daniel.hsiao@mediatek.com>
4 * PoChun Lin <pochun.lin@mediatek.com>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 */
16
17#include <linux/interrupt.h>
18#include <linux/kernel.h>
19#include <linux/slab.h>
20
21#include "../mtk_vcodec_drv.h"
22#include "../mtk_vcodec_util.h"
23#include "../mtk_vcodec_intr.h"
24#include "../mtk_vcodec_enc.h"
25#include "../mtk_vcodec_enc_pm.h"
26#include "../venc_drv_base.h"
27#include "../venc_ipi_msg.h"
28#include "../venc_vpu_if.h"
29#include "mtk_vpu.h"
30
31#define VENC_BITSTREAM_FRAME_SIZE 0x0098
32#define VENC_BITSTREAM_HEADER_LEN 0x00e8
33
34/* This ac_tag is vp8 frame tag. */
35#define MAX_AC_TAG_SIZE 10
36
37/**
38 * enum venc_vp8_vpu_work_buf - vp8 encoder buffer index
39 */
40enum venc_vp8_vpu_work_buf {
41 VENC_VP8_VPU_WORK_BUF_LUMA,
42 VENC_VP8_VPU_WORK_BUF_LUMA2,
43 VENC_VP8_VPU_WORK_BUF_LUMA3,
44 VENC_VP8_VPU_WORK_BUF_CHROMA,
45 VENC_VP8_VPU_WORK_BUF_CHROMA2,
46 VENC_VP8_VPU_WORK_BUF_CHROMA3,
47 VENC_VP8_VPU_WORK_BUF_MV_INFO,
48 VENC_VP8_VPU_WORK_BUF_BS_HEADER,
49 VENC_VP8_VPU_WORK_BUF_PROB_BUF,
50 VENC_VP8_VPU_WORK_BUF_RC_INFO,
51 VENC_VP8_VPU_WORK_BUF_RC_CODE,
52 VENC_VP8_VPU_WORK_BUF_RC_CODE2,
53 VENC_VP8_VPU_WORK_BUF_RC_CODE3,
54 VENC_VP8_VPU_WORK_BUF_MAX,
55};
56
57/*
58 * struct venc_vp8_vpu_config - Structure for vp8 encoder configuration
59 * @input_fourcc: input fourcc
60 * @bitrate: target bitrate (in bps)
61 * @pic_w: picture width. Picture size is visible stream resolution, in pixels,
62 * to be used for display purposes; must be smaller or equal to buffer
63 * size.
64 * @pic_h: picture height
65 * @buf_w: buffer width (with 16 alignment). Buffer size is stream resolution
66 * in pixels aligned to hardware requirements.
67 * @buf_h: buffer height (with 16 alignment)
68 * @gop_size: group of picture size (key frame)
69 * @framerate: frame rate in fps
70 * @ts_mode: temporal scalability mode (0: disable, 1: enable)
71 * support three temporal layers - 0: 7.5fps 1: 7.5fps 2: 15fps.
72 */
73struct venc_vp8_vpu_config {
74 u32 input_fourcc;
75 u32 bitrate;
76 u32 pic_w;
77 u32 pic_h;
78 u32 buf_w;
79 u32 buf_h;
80 u32 gop_size;
81 u32 framerate;
82 u32 ts_mode;
83};
84
85/*
86 * struct venc_vp8_vpu_buf -Structure for buffer information
87 * @align: buffer alignment (in bytes)
88 * @iova: IO virtual address
89 * @vpua: VPU side memory addr which is used by RC_CODE
90 * @size: buffer size (in bytes)
91 */
92struct venc_vp8_vpu_buf {
93 u32 align;
94 u32 iova;
95 u32 vpua;
96 u32 size;
97};
98
99/*
100 * struct venc_vp8_vsi - Structure for VPU driver control and info share
101 * This structure is allocated in VPU side and shared to AP side.
102 * @config: vp8 encoder configuration
103 * @work_bufs: working buffer information in VPU side
104 * The work_bufs here is for storing the 'size' info shared to AP side.
105 * The similar item in struct venc_vp8_inst is for memory allocation
106 * in AP side. The AP driver will copy the 'size' from here to the one in
107 * struct mtk_vcodec_mem, then invoke mtk_vcodec_mem_alloc to allocate
108 * the buffer. After that, bypass the 'dma_addr' to the 'iova' field here for
109 * register setting in VPU side.
110 */
111struct venc_vp8_vsi {
112 struct venc_vp8_vpu_config config;
113 struct venc_vp8_vpu_buf work_bufs[VENC_VP8_VPU_WORK_BUF_MAX];
114};
115
116/*
117 * struct venc_vp8_inst - vp8 encoder AP driver instance
118 * @hw_base: vp8 encoder hardware register base
119 * @work_bufs: working buffer
120 * @work_buf_allocated: working buffer allocated flag
121 * @frm_cnt: encoded frame count, it's used for I-frame judgement and
122 * reset when force intra cmd received.
123 * @ts_mode: temporal scalability mode (0: disable, 1: enable)
124 * support three temporal layers - 0: 7.5fps 1: 7.5fps 2: 15fps.
125 * @vpu_inst: VPU instance to exchange information between AP and VPU
126 * @vsi: driver structure allocated by VPU side and shared to AP side for
127 * control and info share
128 * @ctx: context for v4l2 layer integration
129 */
130struct venc_vp8_inst {
131 void __iomem *hw_base;
132 struct mtk_vcodec_mem work_bufs[VENC_VP8_VPU_WORK_BUF_MAX];
133 bool work_buf_allocated;
134 unsigned int frm_cnt;
135 unsigned int ts_mode;
136 struct venc_vpu_inst vpu_inst;
137 struct venc_vp8_vsi *vsi;
138 struct mtk_vcodec_ctx *ctx;
139};
140
141static inline void vp8_enc_write_reg(struct venc_vp8_inst *inst, u32 addr,
142 u32 val)
143{
144 writel(val, inst->hw_base + addr);
145}
146
147static inline u32 vp8_enc_read_reg(struct venc_vp8_inst *inst, u32 addr)
148{
149 return readl(inst->hw_base + addr);
150}
151
152static void vp8_enc_free_work_buf(struct venc_vp8_inst *inst)
153{
154 int i;
155
156 mtk_vcodec_debug_enter(inst);
157
158 /* Buffers need to be freed by AP. */
159 for (i = 0; i < VENC_VP8_VPU_WORK_BUF_MAX; i++) {
160 if ((inst->work_bufs[i].size == 0))
161 continue;
162 mtk_vcodec_mem_free(inst->ctx, &inst->work_bufs[i]);
163 }
164
165 mtk_vcodec_debug_leave(inst);
166}
167
168static int vp8_enc_alloc_work_buf(struct venc_vp8_inst *inst)
169{
170 int i;
171 int ret = 0;
172 struct venc_vp8_vpu_buf *wb = inst->vsi->work_bufs;
173
174 mtk_vcodec_debug_enter(inst);
175
176 for (i = 0; i < VENC_VP8_VPU_WORK_BUF_MAX; i++) {
177 if ((wb[i].size == 0))
178 continue;
179 /*
180 * This 'wb' structure is set by VPU side and shared to AP for
181 * buffer allocation and IO virtual addr mapping. For most of
182 * the buffers, AP will allocate the buffer according to 'size'
183 * field and store the IO virtual addr in 'iova' field. For the
184 * RC_CODEx buffers, they are pre-allocated in the VPU side
185 * because they are inside VPU SRAM, and save the VPU addr in
186 * the 'vpua' field. The AP will translate the VPU addr to the
187 * corresponding IO virtual addr and store in 'iova' field.
188 */
189 inst->work_bufs[i].size = wb[i].size;
190 ret = mtk_vcodec_mem_alloc(inst->ctx, &inst->work_bufs[i]);
191 if (ret) {
192 mtk_vcodec_err(inst,
193 "cannot alloc work_bufs[%d]", i);
194 goto err_alloc;
195 }
196 /*
197 * This RC_CODEx is pre-allocated by VPU and saved in VPU addr.
198 * So we need use memcpy to copy RC_CODEx from VPU addr into IO
199 * virtual addr in 'iova' field for reg setting in VPU side.
200 */
201 if (i == VENC_VP8_VPU_WORK_BUF_RC_CODE ||
202 i == VENC_VP8_VPU_WORK_BUF_RC_CODE2 ||
203 i == VENC_VP8_VPU_WORK_BUF_RC_CODE3) {
204 void *tmp_va;
205
206 tmp_va = vpu_mapping_dm_addr(inst->vpu_inst.dev,
207 wb[i].vpua);
208 memcpy(inst->work_bufs[i].va, tmp_va, wb[i].size);
209 }
210 wb[i].iova = inst->work_bufs[i].dma_addr;
211
212 mtk_vcodec_debug(inst,
213 "work_bufs[%d] va=0x%p,iova=%pad,size=%zu",
214 i, inst->work_bufs[i].va,
215 &inst->work_bufs[i].dma_addr,
216 inst->work_bufs[i].size);
217 }
218
219 mtk_vcodec_debug_leave(inst);
220
221 return ret;
222
223err_alloc:
224 vp8_enc_free_work_buf(inst);
225
226 return ret;
227}
228
229static unsigned int vp8_enc_wait_venc_done(struct venc_vp8_inst *inst)
230{
231 unsigned int irq_status = 0;
232 struct mtk_vcodec_ctx *ctx = (struct mtk_vcodec_ctx *)inst->ctx;
233
234 if (!mtk_vcodec_wait_for_done_ctx(ctx, MTK_INST_IRQ_RECEIVED,
235 WAIT_INTR_TIMEOUT_MS)) {
236 irq_status = ctx->irq_status;
237 mtk_vcodec_debug(inst, "isr return %x", irq_status);
238 }
239 return irq_status;
240}
241
242/*
243 * Compose ac_tag, bitstream header and bitstream payload into
244 * one bitstream buffer.
245 */
246static int vp8_enc_compose_one_frame(struct venc_vp8_inst *inst,
247 struct mtk_vcodec_mem *bs_buf,
248 unsigned int *bs_size)
249{
250 unsigned int not_key;
251 u32 bs_frm_size;
252 u32 bs_hdr_len;
253 unsigned int ac_tag_size;
254 u8 ac_tag[MAX_AC_TAG_SIZE];
255 u32 tag;
256
257 bs_frm_size = vp8_enc_read_reg(inst, VENC_BITSTREAM_FRAME_SIZE);
258 bs_hdr_len = vp8_enc_read_reg(inst, VENC_BITSTREAM_HEADER_LEN);
259
260 /* if a frame is key frame, not_key is 0 */
261 not_key = !inst->vpu_inst.is_key_frm;
262 tag = (bs_hdr_len << 5) | 0x10 | not_key;
263 ac_tag[0] = tag & 0xff;
264 ac_tag[1] = (tag >> 8) & 0xff;
265 ac_tag[2] = (tag >> 16) & 0xff;
266
267 /* key frame */
268 if (not_key == 0) {
269 ac_tag_size = MAX_AC_TAG_SIZE;
270 ac_tag[3] = 0x9d;
271 ac_tag[4] = 0x01;
272 ac_tag[5] = 0x2a;
273 ac_tag[6] = inst->vsi->config.pic_w;
274 ac_tag[7] = inst->vsi->config.pic_w >> 8;
275 ac_tag[8] = inst->vsi->config.pic_h;
276 ac_tag[9] = inst->vsi->config.pic_h >> 8;
277 } else {
278 ac_tag_size = 3;
279 }
280
281 if (bs_buf->size < bs_hdr_len + bs_frm_size + ac_tag_size) {
282 mtk_vcodec_err(inst, "bitstream buf size is too small(%zu)",
283 bs_buf->size);
284 return -EINVAL;
285 }
286
287 /*
288 * (1) The vp8 bitstream header and body are generated by the HW vp8
289 * encoder separately at the same time. We cannot know the bitstream
290 * header length in advance.
291 * (2) From the vp8 spec, there is no stuffing byte allowed between the
292 * ac tag, bitstream header and bitstream body.
293 */
294 memmove(bs_buf->va + bs_hdr_len + ac_tag_size,
295 bs_buf->va, bs_frm_size);
296 memcpy(bs_buf->va + ac_tag_size,
297 inst->work_bufs[VENC_VP8_VPU_WORK_BUF_BS_HEADER].va,
298 bs_hdr_len);
299 memcpy(bs_buf->va, ac_tag, ac_tag_size);
300 *bs_size = bs_frm_size + bs_hdr_len + ac_tag_size;
301
302 return 0;
303}
304
305static int vp8_enc_encode_frame(struct venc_vp8_inst *inst,
306 struct venc_frm_buf *frm_buf,
307 struct mtk_vcodec_mem *bs_buf,
308 unsigned int *bs_size)
309{
310 int ret = 0;
311 unsigned int irq_status;
312
313 mtk_vcodec_debug(inst, "->frm_cnt=%d", inst->frm_cnt);
314
315 ret = vpu_enc_encode(&inst->vpu_inst, 0, frm_buf, bs_buf, bs_size);
316 if (ret)
317 return ret;
318
319 irq_status = vp8_enc_wait_venc_done(inst);
320 if (irq_status != MTK_VENC_IRQ_STATUS_FRM) {
321 mtk_vcodec_err(inst, "irq_status=%d failed", irq_status);
322 return -EIO;
323 }
324
325 if (vp8_enc_compose_one_frame(inst, bs_buf, bs_size)) {
326 mtk_vcodec_err(inst, "vp8_enc_compose_one_frame failed");
327 return -EINVAL;
328 }
329
330 inst->frm_cnt++;
331 mtk_vcodec_debug(inst, "<-size=%d key_frm=%d", *bs_size,
332 inst->vpu_inst.is_key_frm);
333
334 return ret;
335}
336
337static int vp8_enc_init(struct mtk_vcodec_ctx *ctx, unsigned long *handle)
338{
339 int ret = 0;
340 struct venc_vp8_inst *inst;
341
342 inst = kzalloc(sizeof(*inst), GFP_KERNEL);
343 if (!inst)
344 return -ENOMEM;
345
346 inst->ctx = ctx;
347 inst->vpu_inst.ctx = ctx;
348 inst->vpu_inst.dev = ctx->dev->vpu_plat_dev;
349 inst->vpu_inst.id = IPI_VENC_VP8;
350 inst->hw_base = mtk_vcodec_get_reg_addr(inst->ctx, VENC_LT_SYS);
351
352 mtk_vcodec_debug_enter(inst);
353
354 ret = vpu_enc_init(&inst->vpu_inst);
355
356 inst->vsi = (struct venc_vp8_vsi *)inst->vpu_inst.vsi;
357
358 mtk_vcodec_debug_leave(inst);
359
360 if (ret)
361 kfree(inst);
362 else
363 (*handle) = (unsigned long)inst;
364
365 return ret;
366}
367
368static int vp8_enc_encode(unsigned long handle,
369 enum venc_start_opt opt,
370 struct venc_frm_buf *frm_buf,
371 struct mtk_vcodec_mem *bs_buf,
372 struct venc_done_result *result)
373{
374 int ret = 0;
375 struct venc_vp8_inst *inst = (struct venc_vp8_inst *)handle;
376 struct mtk_vcodec_ctx *ctx = inst->ctx;
377
378 mtk_vcodec_debug_enter(inst);
379
380 enable_irq(ctx->dev->enc_lt_irq);
381
382 switch (opt) {
383 case VENC_START_OPT_ENCODE_FRAME:
384 ret = vp8_enc_encode_frame(inst, frm_buf, bs_buf,
385 &result->bs_size);
386 if (ret)
387 goto encode_err;
388 result->is_key_frm = inst->vpu_inst.is_key_frm;
389 break;
390
391 default:
392 mtk_vcodec_err(inst, "opt not support:%d", opt);
393 ret = -EINVAL;
394 break;
395 }
396
397encode_err:
398
399 disable_irq(ctx->dev->enc_lt_irq);
400 mtk_vcodec_debug_leave(inst);
401
402 return ret;
403}
404
405static int vp8_enc_set_param(unsigned long handle,
406 enum venc_set_param_type type,
407 struct venc_enc_param *enc_prm)
408{
409 int ret = 0;
410 struct venc_vp8_inst *inst = (struct venc_vp8_inst *)handle;
411
412 mtk_vcodec_debug(inst, "->type=%d", type);
413
414 switch (type) {
415 case VENC_SET_PARAM_ENC:
416 inst->vsi->config.input_fourcc = enc_prm->input_yuv_fmt;
417 inst->vsi->config.bitrate = enc_prm->bitrate;
418 inst->vsi->config.pic_w = enc_prm->width;
419 inst->vsi->config.pic_h = enc_prm->height;
420 inst->vsi->config.buf_w = enc_prm->buf_width;
421 inst->vsi->config.buf_h = enc_prm->buf_height;
422 inst->vsi->config.gop_size = enc_prm->gop_size;
423 inst->vsi->config.framerate = enc_prm->frm_rate;
424 inst->vsi->config.ts_mode = inst->ts_mode;
425 ret = vpu_enc_set_param(&inst->vpu_inst, type, enc_prm);
426 if (ret)
427 break;
428 if (inst->work_buf_allocated) {
429 vp8_enc_free_work_buf(inst);
430 inst->work_buf_allocated = false;
431 }
432 ret = vp8_enc_alloc_work_buf(inst);
433 if (ret)
434 break;
435 inst->work_buf_allocated = true;
436 break;
437
438 /*
439 * VENC_SET_PARAM_TS_MODE must be called before VENC_SET_PARAM_ENC
440 */
441 case VENC_SET_PARAM_TS_MODE:
442 inst->ts_mode = 1;
443 mtk_vcodec_debug(inst, "set ts_mode");
444 break;
445
446 default:
447 ret = vpu_enc_set_param(&inst->vpu_inst, type, enc_prm);
448 break;
449 }
450
451 mtk_vcodec_debug_leave(inst);
452
453 return ret;
454}
455
456static int vp8_enc_deinit(unsigned long handle)
457{
458 int ret = 0;
459 struct venc_vp8_inst *inst = (struct venc_vp8_inst *)handle;
460
461 mtk_vcodec_debug_enter(inst);
462
463 ret = vpu_enc_deinit(&inst->vpu_inst);
464
465 if (inst->work_buf_allocated)
466 vp8_enc_free_work_buf(inst);
467
468 mtk_vcodec_debug_leave(inst);
469 kfree(inst);
470
471 return ret;
472}
473
474static struct venc_common_if venc_vp8_if = {
475 vp8_enc_init,
476 vp8_enc_encode,
477 vp8_enc_set_param,
478 vp8_enc_deinit,
479};
480
481struct venc_common_if *get_vp8_enc_comm_if(void);
482
483struct venc_common_if *get_vp8_enc_comm_if(void)
484{
485 return &venc_vp8_if;
486}
diff --git a/drivers/media/platform/mtk-vcodec/venc_drv_base.h b/drivers/media/platform/mtk-vcodec/venc_drv_base.h
new file mode 100644
index 000000000000..6308d44dedf6
--- /dev/null
+++ b/drivers/media/platform/mtk-vcodec/venc_drv_base.h
@@ -0,0 +1,62 @@
1/*
2 * Copyright (c) 2016 MediaTek Inc.
3 * Author: Daniel Hsiao <daniel.hsiao@mediatek.com>
4 * Jungchang Tsao <jungchang.tsao@mediatek.com>
5 * Tiffany Lin <tiffany.lin@mediatek.com>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 */
17
18#ifndef _VENC_DRV_BASE_
19#define _VENC_DRV_BASE_
20
21#include "mtk_vcodec_drv.h"
22
23#include "venc_drv_if.h"
24
25struct venc_common_if {
26 /**
27 * (*init)() - initialize driver
28 * @ctx: [in] mtk v4l2 context
29 * @handle: [out] driver handle
30 */
31 int (*init)(struct mtk_vcodec_ctx *ctx, unsigned long *handle);
32
33 /**
34 * (*encode)() - trigger encode
35 * @handle: [in] driver handle
36 * @opt: [in] encode option
37 * @frm_buf: [in] frame buffer to store input frame
38 * @bs_buf: [in] bitstream buffer to store output bitstream
39 * @result: [out] encode result
40 */
41 int (*encode)(unsigned long handle, enum venc_start_opt opt,
42 struct venc_frm_buf *frm_buf,
43 struct mtk_vcodec_mem *bs_buf,
44 struct venc_done_result *result);
45
46 /**
47 * (*set_param)() - set driver's parameter
48 * @handle: [in] driver handle
49 * @type: [in] parameter type
50 * @in: [in] buffer to store the parameter
51 */
52 int (*set_param)(unsigned long handle, enum venc_set_param_type type,
53 struct venc_enc_param *in);
54
55 /**
56 * (*deinit)() - deinitialize driver.
57 * @handle: [in] driver handle
58 */
59 int (*deinit)(unsigned long handle);
60};
61
62#endif
diff --git a/drivers/media/platform/mtk-vcodec/venc_drv_if.c b/drivers/media/platform/mtk-vcodec/venc_drv_if.c
new file mode 100644
index 000000000000..c4c83e7189c3
--- /dev/null
+++ b/drivers/media/platform/mtk-vcodec/venc_drv_if.c
@@ -0,0 +1,113 @@
1/*
2 * Copyright (c) 2016 MediaTek Inc.
3 * Author: Daniel Hsiao <daniel.hsiao@mediatek.com>
4 * Jungchang Tsao <jungchang.tsao@mediatek.com>
5 * Tiffany Lin <tiffany.lin@mediatek.com>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 */
17
18#include <linux/interrupt.h>
19#include <linux/kernel.h>
20#include <linux/slab.h>
21
22#include "venc_drv_base.h"
23#include "venc_drv_if.h"
24
25#include "mtk_vcodec_enc.h"
26#include "mtk_vcodec_enc_pm.h"
27#include "mtk_vpu.h"
28
29struct venc_common_if *get_h264_enc_comm_if(void);
30struct venc_common_if *get_vp8_enc_comm_if(void);
31
32int venc_if_init(struct mtk_vcodec_ctx *ctx, unsigned int fourcc)
33{
34 int ret = 0;
35
36 switch (fourcc) {
37 case V4L2_PIX_FMT_VP8:
38 ctx->enc_if = get_vp8_enc_comm_if();
39 break;
40 case V4L2_PIX_FMT_H264:
41 ctx->enc_if = get_h264_enc_comm_if();
42 break;
43 default:
44 return -EINVAL;
45 }
46
47 mtk_venc_lock(ctx);
48 mtk_vcodec_enc_clock_on(&ctx->dev->pm);
49 ret = ctx->enc_if->init(ctx, (unsigned long *)&ctx->drv_handle);
50 mtk_vcodec_enc_clock_off(&ctx->dev->pm);
51 mtk_venc_unlock(ctx);
52
53 return ret;
54}
55
56int venc_if_set_param(struct mtk_vcodec_ctx *ctx,
57 enum venc_set_param_type type, struct venc_enc_param *in)
58{
59 int ret = 0;
60
61 mtk_venc_lock(ctx);
62 mtk_vcodec_enc_clock_on(&ctx->dev->pm);
63 ret = ctx->enc_if->set_param(ctx->drv_handle, type, in);
64 mtk_vcodec_enc_clock_off(&ctx->dev->pm);
65 mtk_venc_unlock(ctx);
66
67 return ret;
68}
69
70int venc_if_encode(struct mtk_vcodec_ctx *ctx,
71 enum venc_start_opt opt, struct venc_frm_buf *frm_buf,
72 struct mtk_vcodec_mem *bs_buf,
73 struct venc_done_result *result)
74{
75 int ret = 0;
76 unsigned long flags;
77
78 mtk_venc_lock(ctx);
79
80 spin_lock_irqsave(&ctx->dev->irqlock, flags);
81 ctx->dev->curr_ctx = ctx;
82 spin_unlock_irqrestore(&ctx->dev->irqlock, flags);
83
84 mtk_vcodec_enc_clock_on(&ctx->dev->pm);
85 ret = ctx->enc_if->encode(ctx->drv_handle, opt, frm_buf,
86 bs_buf, result);
87 mtk_vcodec_enc_clock_off(&ctx->dev->pm);
88
89 spin_lock_irqsave(&ctx->dev->irqlock, flags);
90 ctx->dev->curr_ctx = NULL;
91 spin_unlock_irqrestore(&ctx->dev->irqlock, flags);
92
93 mtk_venc_unlock(ctx);
94 return ret;
95}
96
97int venc_if_deinit(struct mtk_vcodec_ctx *ctx)
98{
99 int ret = 0;
100
101 if (ctx->drv_handle == 0)
102 return 0;
103
104 mtk_venc_lock(ctx);
105 mtk_vcodec_enc_clock_on(&ctx->dev->pm);
106 ret = ctx->enc_if->deinit(ctx->drv_handle);
107 mtk_vcodec_enc_clock_off(&ctx->dev->pm);
108 mtk_venc_unlock(ctx);
109
110 ctx->drv_handle = 0;
111
112 return ret;
113}
diff --git a/drivers/media/platform/mtk-vcodec/venc_drv_if.h b/drivers/media/platform/mtk-vcodec/venc_drv_if.h
new file mode 100644
index 000000000000..a6e7d32e55cb
--- /dev/null
+++ b/drivers/media/platform/mtk-vcodec/venc_drv_if.h
@@ -0,0 +1,163 @@
1/*
2 * Copyright (c) 2016 MediaTek Inc.
3 * Author: Daniel Hsiao <daniel.hsiao@mediatek.com>
4 * Jungchang Tsao <jungchang.tsao@mediatek.com>
5 * Tiffany Lin <tiffany.lin@mediatek.com>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 */
17
18#ifndef _VENC_DRV_IF_H_
19#define _VENC_DRV_IF_H_
20
21#include "mtk_vcodec_drv.h"
22#include "mtk_vcodec_util.h"
23
24/*
25 * enum venc_yuv_fmt - The type of input yuv format
26 * (VPU related: If you change the order, you must also update the VPU codes.)
27 * @VENC_YUV_FORMAT_I420: I420 YUV format
28 * @VENC_YUV_FORMAT_YV12: YV12 YUV format
29 * @VENC_YUV_FORMAT_NV12: NV12 YUV format
30 * @VENC_YUV_FORMAT_NV21: NV21 YUV format
31 */
32enum venc_yuv_fmt {
33 VENC_YUV_FORMAT_I420 = 3,
34 VENC_YUV_FORMAT_YV12 = 5,
35 VENC_YUV_FORMAT_NV12 = 6,
36 VENC_YUV_FORMAT_NV21 = 7,
37};
38
39/*
40 * enum venc_start_opt - encode frame option used in venc_if_encode()
41 * @VENC_START_OPT_ENCODE_SEQUENCE_HEADER: encode SPS/PPS for H264
42 * @VENC_START_OPT_ENCODE_FRAME: encode normal frame
43 */
44enum venc_start_opt {
45 VENC_START_OPT_ENCODE_SEQUENCE_HEADER,
46 VENC_START_OPT_ENCODE_FRAME,
47};
48
49/*
50 * enum venc_set_param_type - The type of set parameter used in
51 * venc_if_set_param()
52 * (VPU related: If you change the order, you must also update the VPU codes.)
53 * @VENC_SET_PARAM_ENC: set encoder parameters
54 * @VENC_SET_PARAM_FORCE_INTRA: force an intra frame
55 * @VENC_SET_PARAM_ADJUST_BITRATE: adjust bitrate (in bps)
56 * @VENC_SET_PARAM_ADJUST_FRAMERATE: set frame rate
57 * @VENC_SET_PARAM_GOP_SIZE: set IDR interval
58 * @VENC_SET_PARAM_INTRA_PERIOD: set I frame interval
59 * @VENC_SET_PARAM_SKIP_FRAME: set H264 skip one frame
60 * @VENC_SET_PARAM_PREPEND_HEADER: set H264 prepend SPS/PPS before IDR
61 * @VENC_SET_PARAM_TS_MODE: set VP8 temporal scalability mode
62 */
63enum venc_set_param_type {
64 VENC_SET_PARAM_ENC,
65 VENC_SET_PARAM_FORCE_INTRA,
66 VENC_SET_PARAM_ADJUST_BITRATE,
67 VENC_SET_PARAM_ADJUST_FRAMERATE,
68 VENC_SET_PARAM_GOP_SIZE,
69 VENC_SET_PARAM_INTRA_PERIOD,
70 VENC_SET_PARAM_SKIP_FRAME,
71 VENC_SET_PARAM_PREPEND_HEADER,
72 VENC_SET_PARAM_TS_MODE,
73};
74
75/*
76 * struct venc_enc_prm - encoder settings for VENC_SET_PARAM_ENC used in
77 * venc_if_set_param()
78 * @input_fourcc: input yuv format
79 * @h264_profile: V4L2 defined H.264 profile
80 * @h264_level: V4L2 defined H.264 level
81 * @width: image width
82 * @height: image height
83 * @buf_width: buffer width
84 * @buf_height: buffer height
85 * @frm_rate: frame rate in fps
86 * @intra_period: intra frame period
87 * @bitrate: target bitrate in bps
88 * @gop_size: group of picture size
89 */
90struct venc_enc_param {
91 enum venc_yuv_fmt input_yuv_fmt;
92 unsigned int h264_profile;
93 unsigned int h264_level;
94 unsigned int width;
95 unsigned int height;
96 unsigned int buf_width;
97 unsigned int buf_height;
98 unsigned int frm_rate;
99 unsigned int intra_period;
100 unsigned int bitrate;
101 unsigned int gop_size;
102};
103
104/*
105 * struct venc_frm_buf - frame buffer information used in venc_if_encode()
106 * @fb_addr: plane frame buffer addresses
107 */
108struct venc_frm_buf {
109 struct mtk_vcodec_mem fb_addr[MTK_VCODEC_MAX_PLANES];
110};
111
112/*
113 * struct venc_done_result - This is return information used in venc_if_encode()
114 * @bs_size: output bitstream size
115 * @is_key_frm: output is key frame or not
116 */
117struct venc_done_result {
118 unsigned int bs_size;
119 bool is_key_frm;
120};
121
122/*
123 * venc_if_init - Create the driver handle
124 * @ctx: device context
125 * @fourcc: encoder input format
126 * Return: 0 if creating handle successfully, otherwise it is failed.
127 */
128int venc_if_init(struct mtk_vcodec_ctx *ctx, unsigned int fourcc);
129
130/*
131 * venc_if_deinit - Release the driver handle
132 * @ctx: device context
133 * Return: 0 if releasing handle successfully, otherwise it is failed.
134 */
135int venc_if_deinit(struct mtk_vcodec_ctx *ctx);
136
137/*
138 * venc_if_set_param - Set parameter to driver
139 * @ctx: device context
140 * @type: parameter type
141 * @in: input parameter
142 * Return: 0 if setting param successfully, otherwise it is failed.
143 */
144int venc_if_set_param(struct mtk_vcodec_ctx *ctx,
145 enum venc_set_param_type type,
146 struct venc_enc_param *in);
147
148/*
149 * venc_if_encode - Encode one frame
150 * @ctx: device context
151 * @opt: encode frame option
152 * @frm_buf: input frame buffer information
153 * @bs_buf: output bitstream buffer infomraiton
154 * @result: encode result
155 * Return: 0 if encoding frame successfully, otherwise it is failed.
156 */
157int venc_if_encode(struct mtk_vcodec_ctx *ctx,
158 enum venc_start_opt opt,
159 struct venc_frm_buf *frm_buf,
160 struct mtk_vcodec_mem *bs_buf,
161 struct venc_done_result *result);
162
163#endif /* _VENC_DRV_IF_H_ */
diff --git a/drivers/media/platform/mtk-vcodec/venc_ipi_msg.h b/drivers/media/platform/mtk-vcodec/venc_ipi_msg.h
new file mode 100644
index 000000000000..4c869cb6fbf7
--- /dev/null
+++ b/drivers/media/platform/mtk-vcodec/venc_ipi_msg.h
@@ -0,0 +1,210 @@
1/*
2 * Copyright (c) 2016 MediaTek Inc.
3 * Author: Jungchang Tsao <jungchang.tsao@mediatek.com>
4 * Daniel Hsiao <daniel.hsiao@mediatek.com>
5 * Tiffany Lin <tiffany.lin@mediatek.com>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 */
17
18#ifndef _VENC_IPI_MSG_H_
19#define _VENC_IPI_MSG_H_
20
21#define AP_IPIMSG_VENC_BASE 0xC000
22#define VPU_IPIMSG_VENC_BASE 0xD000
23
24/**
25 * enum venc_ipi_msg_id - message id between AP and VPU
26 * (ipi stands for inter-processor interrupt)
27 * @AP_IPIMSG_ENC_XXX: AP to VPU cmd message id
28 * @VPU_IPIMSG_ENC_XXX_DONE: VPU ack AP cmd message id
29 */
30enum venc_ipi_msg_id {
31 AP_IPIMSG_ENC_INIT = AP_IPIMSG_VENC_BASE,
32 AP_IPIMSG_ENC_SET_PARAM,
33 AP_IPIMSG_ENC_ENCODE,
34 AP_IPIMSG_ENC_DEINIT,
35
36 VPU_IPIMSG_ENC_INIT_DONE = VPU_IPIMSG_VENC_BASE,
37 VPU_IPIMSG_ENC_SET_PARAM_DONE,
38 VPU_IPIMSG_ENC_ENCODE_DONE,
39 VPU_IPIMSG_ENC_DEINIT_DONE,
40};
41
42/**
43 * struct venc_ap_ipi_msg_init - AP to VPU init cmd structure
44 * @msg_id: message id (AP_IPIMSG_XXX_ENC_INIT)
45 * @reserved: reserved for future use. vpu is running in 32bit. Without
46 * this reserved field, if kernel run in 64bit. this struct size
47 * will be different between kernel and vpu
48 * @venc_inst: AP encoder instance
49 * (struct venc_vp8_inst/venc_h264_inst *)
50 */
51struct venc_ap_ipi_msg_init {
52 uint32_t msg_id;
53 uint32_t reserved;
54 uint64_t venc_inst;
55};
56
57/**
58 * struct venc_ap_ipi_msg_set_param - AP to VPU set_param cmd structure
59 * @msg_id: message id (AP_IPIMSG_XXX_ENC_SET_PARAM)
60 * @vpu_inst_addr: VPU encoder instance addr
61 * (struct venc_vp8_vsi/venc_h264_vsi *)
62 * @param_id: parameter id (venc_set_param_type)
63 * @data_item: number of items in the data array
64 * @data[8]: data array to store the set parameters
65 */
66struct venc_ap_ipi_msg_set_param {
67 uint32_t msg_id;
68 uint32_t vpu_inst_addr;
69 uint32_t param_id;
70 uint32_t data_item;
71 uint32_t data[8];
72};
73
74/**
75 * struct venc_ap_ipi_msg_enc - AP to VPU enc cmd structure
76 * @msg_id: message id (AP_IPIMSG_XXX_ENC_ENCODE)
77 * @vpu_inst_addr: VPU encoder instance addr
78 * (struct venc_vp8_vsi/venc_h264_vsi *)
79 * @bs_mode: bitstream mode for h264
80 * (H264_BS_MODE_SPS/H264_BS_MODE_PPS/H264_BS_MODE_FRAME)
81 * @input_addr: pointer to input image buffer plane
82 * @bs_addr: pointer to output bit stream buffer
83 * @bs_size: bit stream buffer size
84 */
85struct venc_ap_ipi_msg_enc {
86 uint32_t msg_id;
87 uint32_t vpu_inst_addr;
88 uint32_t bs_mode;
89 uint32_t input_addr[3];
90 uint32_t bs_addr;
91 uint32_t bs_size;
92};
93
94/**
95 * struct venc_ap_ipi_msg_deinit - AP to VPU deinit cmd structure
96 * @msg_id: message id (AP_IPIMSG_XXX_ENC_DEINIT)
97 * @vpu_inst_addr: VPU encoder instance addr
98 * (struct venc_vp8_vsi/venc_h264_vsi *)
99 */
100struct venc_ap_ipi_msg_deinit {
101 uint32_t msg_id;
102 uint32_t vpu_inst_addr;
103};
104
105/**
106 * enum venc_ipi_msg_status - VPU ack AP cmd status
107 */
108enum venc_ipi_msg_status {
109 VENC_IPI_MSG_STATUS_OK,
110 VENC_IPI_MSG_STATUS_FAIL,
111};
112
113/**
114 * struct venc_vpu_ipi_msg_common - VPU ack AP cmd common structure
115 * @msg_id: message id (VPU_IPIMSG_XXX_DONE)
116 * @status: cmd status (venc_ipi_msg_status)
117 * @venc_inst: AP encoder instance (struct venc_vp8_inst/venc_h264_inst *)
118 */
119struct venc_vpu_ipi_msg_common {
120 uint32_t msg_id;
121 uint32_t status;
122 uint64_t venc_inst;
123};
124
125/**
126 * struct venc_vpu_ipi_msg_init - VPU ack AP init cmd structure
127 * @msg_id: message id (VPU_IPIMSG_XXX_ENC_SET_PARAM_DONE)
128 * @status: cmd status (venc_ipi_msg_status)
129 * @venc_inst: AP encoder instance (struct venc_vp8_inst/venc_h264_inst *)
130 * @vpu_inst_addr: VPU encoder instance addr
131 * (struct venc_vp8_vsi/venc_h264_vsi *)
132 * @reserved: reserved for future use. vpu is running in 32bit. Without
133 * this reserved field, if kernel run in 64bit. this struct size
134 * will be different between kernel and vpu
135 */
136struct venc_vpu_ipi_msg_init {
137 uint32_t msg_id;
138 uint32_t status;
139 uint64_t venc_inst;
140 uint32_t vpu_inst_addr;
141 uint32_t reserved;
142};
143
144/**
145 * struct venc_vpu_ipi_msg_set_param - VPU ack AP set_param cmd structure
146 * @msg_id: message id (VPU_IPIMSG_XXX_ENC_SET_PARAM_DONE)
147 * @status: cmd status (venc_ipi_msg_status)
148 * @venc_inst: AP encoder instance (struct venc_vp8_inst/venc_h264_inst *)
149 * @param_id: parameter id (venc_set_param_type)
150 * @data_item: number of items in the data array
151 * @data[6]: data array to store the return result
152 */
153struct venc_vpu_ipi_msg_set_param {
154 uint32_t msg_id;
155 uint32_t status;
156 uint64_t venc_inst;
157 uint32_t param_id;
158 uint32_t data_item;
159 uint32_t data[6];
160};
161
162/**
163 * enum venc_ipi_msg_enc_state - Type of encode state
164 * VEN_IPI_MSG_ENC_STATE_FRAME: one frame being encoded
165 * VEN_IPI_MSG_ENC_STATE_PART: bit stream buffer full
166 * VEN_IPI_MSG_ENC_STATE_SKIP: encoded skip frame
167 * VEN_IPI_MSG_ENC_STATE_ERROR: encounter error
168 */
169enum venc_ipi_msg_enc_state {
170 VEN_IPI_MSG_ENC_STATE_FRAME,
171 VEN_IPI_MSG_ENC_STATE_PART,
172 VEN_IPI_MSG_ENC_STATE_SKIP,
173 VEN_IPI_MSG_ENC_STATE_ERROR,
174};
175
176/**
177 * struct venc_vpu_ipi_msg_enc - VPU ack AP enc cmd structure
178 * @msg_id: message id (VPU_IPIMSG_XXX_ENC_ENCODE_DONE)
179 * @status: cmd status (venc_ipi_msg_status)
180 * @venc_inst: AP encoder instance (struct venc_vp8_inst/venc_h264_inst *)
181 * @state: encode state (venc_ipi_msg_enc_state)
182 * @is_key_frm: whether the encoded frame is key frame
183 * @bs_size: encoded bitstream size
184 * @reserved: reserved for future use. vpu is running in 32bit. Without
185 * this reserved field, if kernel run in 64bit. this struct size
186 * will be different between kernel and vpu
187 */
188struct venc_vpu_ipi_msg_enc {
189 uint32_t msg_id;
190 uint32_t status;
191 uint64_t venc_inst;
192 uint32_t state;
193 uint32_t is_key_frm;
194 uint32_t bs_size;
195 uint32_t reserved;
196};
197
198/**
199 * struct venc_vpu_ipi_msg_deinit - VPU ack AP deinit cmd structure
200 * @msg_id: message id (VPU_IPIMSG_XXX_ENC_DEINIT_DONE)
201 * @status: cmd status (venc_ipi_msg_status)
202 * @venc_inst: AP encoder instance (struct venc_vp8_inst/venc_h264_inst *)
203 */
204struct venc_vpu_ipi_msg_deinit {
205 uint32_t msg_id;
206 uint32_t status;
207 uint64_t venc_inst;
208};
209
210#endif /* _VENC_IPI_MSG_H_ */
diff --git a/drivers/media/platform/mtk-vcodec/venc_vpu_if.c b/drivers/media/platform/mtk-vcodec/venc_vpu_if.c
new file mode 100644
index 000000000000..a01c7599b510
--- /dev/null
+++ b/drivers/media/platform/mtk-vcodec/venc_vpu_if.c
@@ -0,0 +1,238 @@
1/*
2 * Copyright (c) 2016 MediaTek Inc.
3 * Author: PoChun Lin <pochun.lin@mediatek.com>
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 */
15
16#include "mtk_vpu.h"
17#include "venc_ipi_msg.h"
18#include "venc_vpu_if.h"
19
20static void handle_enc_init_msg(struct venc_vpu_inst *vpu, void *data)
21{
22 struct venc_vpu_ipi_msg_init *msg = data;
23
24 vpu->inst_addr = msg->vpu_inst_addr;
25 vpu->vsi = vpu_mapping_dm_addr(vpu->dev, msg->vpu_inst_addr);
26}
27
28static void handle_enc_encode_msg(struct venc_vpu_inst *vpu, void *data)
29{
30 struct venc_vpu_ipi_msg_enc *msg = data;
31
32 vpu->state = msg->state;
33 vpu->bs_size = msg->bs_size;
34 vpu->is_key_frm = msg->is_key_frm;
35}
36
37static void vpu_enc_ipi_handler(void *data, unsigned int len, void *priv)
38{
39 struct venc_vpu_ipi_msg_common *msg = data;
40 struct venc_vpu_inst *vpu =
41 (struct venc_vpu_inst *)(unsigned long)msg->venc_inst;
42
43 mtk_vcodec_debug(vpu, "msg_id %x inst %p status %d",
44 msg->msg_id, vpu, msg->status);
45
46 switch (msg->msg_id) {
47 case VPU_IPIMSG_ENC_INIT_DONE:
48 handle_enc_init_msg(vpu, data);
49 break;
50 case VPU_IPIMSG_ENC_SET_PARAM_DONE:
51 break;
52 case VPU_IPIMSG_ENC_ENCODE_DONE:
53 handle_enc_encode_msg(vpu, data);
54 break;
55 case VPU_IPIMSG_ENC_DEINIT_DONE:
56 break;
57 default:
58 mtk_vcodec_err(vpu, "unknown msg id %x", msg->msg_id);
59 break;
60 }
61
62 vpu->signaled = 1;
63 vpu->failure = (msg->status != VENC_IPI_MSG_STATUS_OK);
64
65 mtk_vcodec_debug_leave(vpu);
66}
67
68static int vpu_enc_send_msg(struct venc_vpu_inst *vpu, void *msg,
69 int len)
70{
71 int status;
72
73 mtk_vcodec_debug_enter(vpu);
74
75 if (!vpu->dev) {
76 mtk_vcodec_err(vpu, "inst dev is NULL");
77 return -EINVAL;
78 }
79
80 status = vpu_ipi_send(vpu->dev, vpu->id, msg, len);
81 if (status) {
82 uint32_t msg_id = *(uint32_t *)msg;
83
84 mtk_vcodec_err(vpu, "vpu_ipi_send msg_id %x len %d fail %d",
85 msg_id, len, status);
86 return -EINVAL;
87 }
88 if (vpu->failure)
89 return -EINVAL;
90
91 mtk_vcodec_debug_leave(vpu);
92
93 return 0;
94}
95
96int vpu_enc_init(struct venc_vpu_inst *vpu)
97{
98 int status;
99 struct venc_ap_ipi_msg_init out;
100
101 mtk_vcodec_debug_enter(vpu);
102
103 init_waitqueue_head(&vpu->wq_hd);
104 vpu->signaled = 0;
105 vpu->failure = 0;
106
107 status = vpu_ipi_register(vpu->dev, vpu->id, vpu_enc_ipi_handler,
108 NULL, NULL);
109 if (status) {
110 mtk_vcodec_err(vpu, "vpu_ipi_register fail %d", status);
111 return -EINVAL;
112 }
113
114 memset(&out, 0, sizeof(out));
115 out.msg_id = AP_IPIMSG_ENC_INIT;
116 out.venc_inst = (unsigned long)vpu;
117 if (vpu_enc_send_msg(vpu, &out, sizeof(out))) {
118 mtk_vcodec_err(vpu, "AP_IPIMSG_ENC_INIT fail");
119 return -EINVAL;
120 }
121
122 mtk_vcodec_debug_leave(vpu);
123
124 return 0;
125}
126
127int vpu_enc_set_param(struct venc_vpu_inst *vpu,
128 enum venc_set_param_type id,
129 struct venc_enc_param *enc_param)
130{
131 struct venc_ap_ipi_msg_set_param out;
132
133 mtk_vcodec_debug(vpu, "id %d ->", id);
134
135 memset(&out, 0, sizeof(out));
136 out.msg_id = AP_IPIMSG_ENC_SET_PARAM;
137 out.vpu_inst_addr = vpu->inst_addr;
138 out.param_id = id;
139 switch (id) {
140 case VENC_SET_PARAM_ENC:
141 out.data_item = 0;
142 break;
143 case VENC_SET_PARAM_FORCE_INTRA:
144 out.data_item = 0;
145 break;
146 case VENC_SET_PARAM_ADJUST_BITRATE:
147 out.data_item = 1;
148 out.data[0] = enc_param->bitrate;
149 break;
150 case VENC_SET_PARAM_ADJUST_FRAMERATE:
151 out.data_item = 1;
152 out.data[0] = enc_param->frm_rate;
153 break;
154 case VENC_SET_PARAM_GOP_SIZE:
155 out.data_item = 1;
156 out.data[0] = enc_param->gop_size;
157 break;
158 case VENC_SET_PARAM_INTRA_PERIOD:
159 out.data_item = 1;
160 out.data[0] = enc_param->intra_period;
161 break;
162 case VENC_SET_PARAM_SKIP_FRAME:
163 out.data_item = 0;
164 break;
165 default:
166 mtk_vcodec_err(vpu, "id %d not supported", id);
167 return -EINVAL;
168 }
169 if (vpu_enc_send_msg(vpu, &out, sizeof(out))) {
170 mtk_vcodec_err(vpu,
171 "AP_IPIMSG_ENC_SET_PARAM %d fail", id);
172 return -EINVAL;
173 }
174
175 mtk_vcodec_debug(vpu, "id %d <-", id);
176
177 return 0;
178}
179
180int vpu_enc_encode(struct venc_vpu_inst *vpu, unsigned int bs_mode,
181 struct venc_frm_buf *frm_buf,
182 struct mtk_vcodec_mem *bs_buf,
183 unsigned int *bs_size)
184{
185 struct venc_ap_ipi_msg_enc out;
186
187 mtk_vcodec_debug(vpu, "bs_mode %d ->", bs_mode);
188
189 memset(&out, 0, sizeof(out));
190 out.msg_id = AP_IPIMSG_ENC_ENCODE;
191 out.vpu_inst_addr = vpu->inst_addr;
192 out.bs_mode = bs_mode;
193 if (frm_buf) {
194 if ((frm_buf->fb_addr[0].dma_addr % 16 == 0) &&
195 (frm_buf->fb_addr[1].dma_addr % 16 == 0) &&
196 (frm_buf->fb_addr[2].dma_addr % 16 == 0)) {
197 out.input_addr[0] = frm_buf->fb_addr[0].dma_addr;
198 out.input_addr[1] = frm_buf->fb_addr[1].dma_addr;
199 out.input_addr[2] = frm_buf->fb_addr[2].dma_addr;
200 } else {
201 mtk_vcodec_err(vpu, "dma_addr not align to 16");
202 return -EINVAL;
203 }
204 }
205 if (bs_buf) {
206 out.bs_addr = bs_buf->dma_addr;
207 out.bs_size = bs_buf->size;
208 }
209 if (vpu_enc_send_msg(vpu, &out, sizeof(out))) {
210 mtk_vcodec_err(vpu, "AP_IPIMSG_ENC_ENCODE %d fail",
211 bs_mode);
212 return -EINVAL;
213 }
214
215 mtk_vcodec_debug(vpu, "bs_mode %d state %d size %d key_frm %d <-",
216 bs_mode, vpu->state, vpu->bs_size, vpu->is_key_frm);
217
218 return 0;
219}
220
221int vpu_enc_deinit(struct venc_vpu_inst *vpu)
222{
223 struct venc_ap_ipi_msg_deinit out;
224
225 mtk_vcodec_debug_enter(vpu);
226
227 memset(&out, 0, sizeof(out));
228 out.msg_id = AP_IPIMSG_ENC_DEINIT;
229 out.vpu_inst_addr = vpu->inst_addr;
230 if (vpu_enc_send_msg(vpu, &out, sizeof(out))) {
231 mtk_vcodec_err(vpu, "AP_IPIMSG_ENC_DEINIT fail");
232 return -EINVAL;
233 }
234
235 mtk_vcodec_debug_leave(vpu);
236
237 return 0;
238}
diff --git a/drivers/media/platform/mtk-vcodec/venc_vpu_if.h b/drivers/media/platform/mtk-vcodec/venc_vpu_if.h
new file mode 100644
index 000000000000..215d1e01362e
--- /dev/null
+++ b/drivers/media/platform/mtk-vcodec/venc_vpu_if.h
@@ -0,0 +1,61 @@
1/*
2 * Copyright (c) 2016 MediaTek Inc.
3 * Author: PoChun Lin <pochun.lin@mediatek.com>
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 */
15
16#ifndef _VENC_VPU_IF_H_
17#define _VENC_VPU_IF_H_
18
19#include "mtk_vpu.h"
20#include "venc_drv_if.h"
21
22/*
23 * struct venc_vpu_inst - encoder VPU driver instance
24 * @wq_hd: wait queue used for vpu cmd trigger then wait vpu interrupt done
25 * @signaled: flag used for checking vpu interrupt done
26 * @failure: flag to show vpu cmd succeeds or not
27 * @state: enum venc_ipi_msg_enc_state
28 * @bs_size: bitstream size for skip frame case usage
29 * @is_key_frm: key frame flag
30 * @inst_addr: VPU instance addr
31 * @vsi: driver structure allocated by VPU side and shared to AP side for
32 * control and info share
33 * @id: the id of inter-processor interrupt
34 * @ctx: context for v4l2 layer integration
35 * @dev: device for v4l2 layer integration
36 */
37struct venc_vpu_inst {
38 wait_queue_head_t wq_hd;
39 int signaled;
40 int failure;
41 int state;
42 int bs_size;
43 int is_key_frm;
44 unsigned int inst_addr;
45 void *vsi;
46 enum ipi_id id;
47 struct mtk_vcodec_ctx *ctx;
48 struct platform_device *dev;
49};
50
51int vpu_enc_init(struct venc_vpu_inst *vpu);
52int vpu_enc_set_param(struct venc_vpu_inst *vpu,
53 enum venc_set_param_type id,
54 struct venc_enc_param *param);
55int vpu_enc_encode(struct venc_vpu_inst *vpu, unsigned int bs_mode,
56 struct venc_frm_buf *frm_buf,
57 struct mtk_vcodec_mem *bs_buf,
58 unsigned int *bs_size);
59int vpu_enc_deinit(struct venc_vpu_inst *vpu);
60
61#endif
diff --git a/drivers/media/platform/mtk-vpu/Makefile b/drivers/media/platform/mtk-vpu/Makefile
new file mode 100644
index 000000000000..58cc1b4bc9f2
--- /dev/null
+++ b/drivers/media/platform/mtk-vpu/Makefile
@@ -0,0 +1,3 @@
1mtk-vpu-y += mtk_vpu.o
2
3obj-$(CONFIG_VIDEO_MEDIATEK_VPU) += mtk-vpu.o
diff --git a/drivers/media/platform/mtk-vpu/mtk_vpu.c b/drivers/media/platform/mtk-vpu/mtk_vpu.c
new file mode 100644
index 000000000000..c9bf58c97878
--- /dev/null
+++ b/drivers/media/platform/mtk-vpu/mtk_vpu.c
@@ -0,0 +1,946 @@
1/*
2* Copyright (c) 2016 MediaTek Inc.
3* Author: Andrew-CT Chen <andrew-ct.chen@mediatek.com>
4*
5* This program is free software; you can redistribute it and/or modify
6* it under the terms of the GNU General Public License version 2 as
7* published by the Free Software Foundation.
8*
9* This program is distributed in the hope that it will be useful,
10* but WITHOUT ANY WARRANTY; without even the implied warranty of
11* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12* GNU General Public License for more details.
13*/
14#include <linux/clk.h>
15#include <linux/debugfs.h>
16#include <linux/firmware.h>
17#include <linux/interrupt.h>
18#include <linux/iommu.h>
19#include <linux/module.h>
20#include <linux/of_address.h>
21#include <linux/of_irq.h>
22#include <linux/of_platform.h>
23#include <linux/of_reserved_mem.h>
24#include <linux/sched.h>
25#include <linux/sizes.h>
26
27#include "mtk_vpu.h"
28
29/**
30 * VPU (video processor unit) is a tiny processor controlling video hardware
31 * related to video codec, scaling and color format converting.
32 * VPU interfaces with other blocks by share memory and interrupt.
33 **/
34
35#define INIT_TIMEOUT_MS 2000U
36#define IPI_TIMEOUT_MS 2000U
37#define VPU_FW_VER_LEN 16
38
39/* maximum program/data TCM (Tightly-Coupled Memory) size */
40#define VPU_PTCM_SIZE (96 * SZ_1K)
41#define VPU_DTCM_SIZE (32 * SZ_1K)
42/* the offset to get data tcm address */
43#define VPU_DTCM_OFFSET 0x18000UL
44/* daynamic allocated maximum extended memory size */
45#define VPU_EXT_P_SIZE SZ_1M
46#define VPU_EXT_D_SIZE SZ_4M
47/* maximum binary firmware size */
48#define VPU_P_FW_SIZE (VPU_PTCM_SIZE + VPU_EXT_P_SIZE)
49#define VPU_D_FW_SIZE (VPU_DTCM_SIZE + VPU_EXT_D_SIZE)
50/* the size of share buffer between Host and VPU */
51#define SHARE_BUF_SIZE 48
52
53/* binary firmware name */
54#define VPU_P_FW "vpu_p.bin"
55#define VPU_D_FW "vpu_d.bin"
56
57#define VPU_RESET 0x0
58#define VPU_TCM_CFG 0x0008
59#define VPU_PMEM_EXT0_ADDR 0x000C
60#define VPU_PMEM_EXT1_ADDR 0x0010
61#define VPU_TO_HOST 0x001C
62#define VPU_DMEM_EXT0_ADDR 0x0014
63#define VPU_DMEM_EXT1_ADDR 0x0018
64#define HOST_TO_VPU 0x0024
65#define VPU_PC_REG 0x0060
66#define VPU_WDT_REG 0x0084
67
68/* vpu inter-processor communication interrupt */
69#define VPU_IPC_INT BIT(8)
70
71/**
72 * enum vpu_fw_type - VPU firmware type
73 *
74 * @P_FW: program firmware
75 * @D_FW: data firmware
76 *
77 */
78enum vpu_fw_type {
79 P_FW,
80 D_FW,
81};
82
83/**
84 * struct vpu_mem - VPU extended program/data memory information
85 *
86 * @va: the kernel virtual memory address of VPU extended memory
87 * @pa: the physical memory address of VPU extended memory
88 *
89 */
90struct vpu_mem {
91 void *va;
92 dma_addr_t pa;
93};
94
95/**
96 * struct vpu_regs - VPU TCM and configuration registers
97 *
98 * @tcm: the register for VPU Tightly-Coupled Memory
99 * @cfg: the register for VPU configuration
100 * @irq: the irq number for VPU interrupt
101 */
102struct vpu_regs {
103 void __iomem *tcm;
104 void __iomem *cfg;
105 int irq;
106};
107
108/**
109 * struct vpu_wdt_handler - VPU watchdog reset handler
110 *
111 * @reset_func: reset handler
112 * @priv: private data
113 */
114struct vpu_wdt_handler {
115 void (*reset_func)(void *);
116 void *priv;
117};
118
119/**
120 * struct vpu_wdt - VPU watchdog workqueue
121 *
122 * @handler: VPU watchdog reset handler
123 * @ws: workstruct for VPU watchdog
124 * @wq: workqueue for VPU watchdog
125 */
126struct vpu_wdt {
127 struct vpu_wdt_handler handler[VPU_RST_MAX];
128 struct work_struct ws;
129 struct workqueue_struct *wq;
130};
131
132/**
133 * struct vpu_run - VPU initialization status
134 *
135 * @signaled: the signal of vpu initialization completed
136 * @fw_ver: VPU firmware version
137 * @enc_capability: encoder capability which is not used for now and
138 * the value is reserved for future use
139 * @wq: wait queue for VPU initialization status
140 */
141struct vpu_run {
142 u32 signaled;
143 char fw_ver[VPU_FW_VER_LEN];
144 unsigned int enc_capability;
145 wait_queue_head_t wq;
146};
147
148/**
149 * struct vpu_ipi_desc - VPU IPI descriptor
150 *
151 * @handler: IPI handler
152 * @name: the name of IPI handler
153 * @priv: the private data of IPI handler
154 */
155struct vpu_ipi_desc {
156 ipi_handler_t handler;
157 const char *name;
158 void *priv;
159};
160
161/**
162 * struct share_obj - DTCM (Data Tightly-Coupled Memory) buffer shared with
163 * AP and VPU
164 *
165 * @id: IPI id
166 * @len: share buffer length
167 * @share_buf: share buffer data
168 */
169struct share_obj {
170 s32 id;
171 u32 len;
172 unsigned char share_buf[SHARE_BUF_SIZE];
173};
174
175/**
176 * struct mtk_vpu - vpu driver data
177 * @extmem: VPU extended memory information
178 * @reg: VPU TCM and configuration registers
179 * @run: VPU initialization status
180 * @ipi_desc: VPU IPI descriptor
181 * @recv_buf: VPU DTCM share buffer for receiving. The
182 * receive buffer is only accessed in interrupt context.
183 * @send_buf: VPU DTCM share buffer for sending
184 * @dev: VPU struct device
185 * @clk: VPU clock on/off
186 * @fw_loaded: indicate VPU firmware loaded
187 * @enable_4GB: VPU 4GB mode on/off
188 * @vpu_mutex: protect mtk_vpu (except recv_buf) and ensure only
189 * one client to use VPU service at a time. For example,
190 * suppose a client is using VPU to decode VP8.
191 * If the other client wants to encode VP8,
192 * it has to wait until VP8 decode completes.
193 * @wdt_refcnt WDT reference count to make sure the watchdog can be
194 * disabled if no other client is using VPU service
195 * @ack_wq: The wait queue for each codec and mdp. When sleeping
196 * processes wake up, they will check the condition
197 * "ipi_id_ack" to run the corresponding action or
198 * go back to sleep.
199 * @ipi_id_ack: The ACKs for registered IPI function sending
200 * interrupt to VPU
201 *
202 */
203struct mtk_vpu {
204 struct vpu_mem extmem[2];
205 struct vpu_regs reg;
206 struct vpu_run run;
207 struct vpu_wdt wdt;
208 struct vpu_ipi_desc ipi_desc[IPI_MAX];
209 struct share_obj *recv_buf;
210 struct share_obj *send_buf;
211 struct device *dev;
212 struct clk *clk;
213 bool fw_loaded;
214 bool enable_4GB;
215 struct mutex vpu_mutex; /* for protecting vpu data data structure */
216 u32 wdt_refcnt;
217 wait_queue_head_t ack_wq;
218 bool ipi_id_ack[IPI_MAX];
219};
220
221static inline void vpu_cfg_writel(struct mtk_vpu *vpu, u32 val, u32 offset)
222{
223 writel(val, vpu->reg.cfg + offset);
224}
225
226static inline u32 vpu_cfg_readl(struct mtk_vpu *vpu, u32 offset)
227{
228 return readl(vpu->reg.cfg + offset);
229}
230
231static inline bool vpu_running(struct mtk_vpu *vpu)
232{
233 return vpu_cfg_readl(vpu, VPU_RESET) & BIT(0);
234}
235
236static void vpu_clock_disable(struct mtk_vpu *vpu)
237{
238 /* Disable VPU watchdog */
239 mutex_lock(&vpu->vpu_mutex);
240 if (!--vpu->wdt_refcnt)
241 vpu_cfg_writel(vpu,
242 vpu_cfg_readl(vpu, VPU_WDT_REG) & ~(1L << 31),
243 VPU_WDT_REG);
244 mutex_unlock(&vpu->vpu_mutex);
245
246 clk_disable(vpu->clk);
247}
248
249static int vpu_clock_enable(struct mtk_vpu *vpu)
250{
251 int ret;
252
253 ret = clk_enable(vpu->clk);
254 if (ret)
255 return ret;
256 /* Enable VPU watchdog */
257 mutex_lock(&vpu->vpu_mutex);
258 if (!vpu->wdt_refcnt++)
259 vpu_cfg_writel(vpu,
260 vpu_cfg_readl(vpu, VPU_WDT_REG) | (1L << 31),
261 VPU_WDT_REG);
262 mutex_unlock(&vpu->vpu_mutex);
263
264 return ret;
265}
266
267int vpu_ipi_register(struct platform_device *pdev,
268 enum ipi_id id, ipi_handler_t handler,
269 const char *name, void *priv)
270{
271 struct mtk_vpu *vpu = platform_get_drvdata(pdev);
272 struct vpu_ipi_desc *ipi_desc;
273
274 if (!vpu) {
275 dev_err(&pdev->dev, "vpu device in not ready\n");
276 return -EPROBE_DEFER;
277 }
278
279 if (id >= 0 && id < IPI_MAX && handler) {
280 ipi_desc = vpu->ipi_desc;
281 ipi_desc[id].name = name;
282 ipi_desc[id].handler = handler;
283 ipi_desc[id].priv = priv;
284 return 0;
285 }
286
287 dev_err(&pdev->dev, "register vpu ipi id %d with invalid arguments\n",
288 id);
289 return -EINVAL;
290}
291EXPORT_SYMBOL_GPL(vpu_ipi_register);
292
293int vpu_ipi_send(struct platform_device *pdev,
294 enum ipi_id id, void *buf,
295 unsigned int len)
296{
297 struct mtk_vpu *vpu = platform_get_drvdata(pdev);
298 struct share_obj *send_obj = vpu->send_buf;
299 unsigned long timeout;
300 int ret = 0;
301
302 if (id <= IPI_VPU_INIT || id >= IPI_MAX ||
303 len > sizeof(send_obj->share_buf) || !buf) {
304 dev_err(vpu->dev, "failed to send ipi message\n");
305 return -EINVAL;
306 }
307
308 ret = vpu_clock_enable(vpu);
309 if (ret) {
310 dev_err(vpu->dev, "failed to enable vpu clock\n");
311 return ret;
312 }
313 if (!vpu_running(vpu)) {
314 dev_err(vpu->dev, "vpu_ipi_send: VPU is not running\n");
315 ret = -EINVAL;
316 goto clock_disable;
317 }
318
319 mutex_lock(&vpu->vpu_mutex);
320
321 /* Wait until VPU receives the last command */
322 timeout = jiffies + msecs_to_jiffies(IPI_TIMEOUT_MS);
323 do {
324 if (time_after(jiffies, timeout)) {
325 dev_err(vpu->dev, "vpu_ipi_send: IPI timeout!\n");
326 ret = -EIO;
327 goto mut_unlock;
328 }
329 } while (vpu_cfg_readl(vpu, HOST_TO_VPU));
330
331 memcpy((void *)send_obj->share_buf, buf, len);
332 send_obj->len = len;
333 send_obj->id = id;
334
335 vpu->ipi_id_ack[id] = false;
336 /* send the command to VPU */
337 vpu_cfg_writel(vpu, 0x1, HOST_TO_VPU);
338
339 mutex_unlock(&vpu->vpu_mutex);
340
341 /* wait for VPU's ACK */
342 timeout = msecs_to_jiffies(IPI_TIMEOUT_MS);
343 ret = wait_event_timeout(vpu->ack_wq, vpu->ipi_id_ack[id], timeout);
344 vpu->ipi_id_ack[id] = false;
345 if (ret == 0) {
346 dev_err(vpu->dev, "vpu ipi %d ack time out !", id);
347 ret = -EIO;
348 goto clock_disable;
349 }
350 vpu_clock_disable(vpu);
351
352 return 0;
353
354mut_unlock:
355 mutex_unlock(&vpu->vpu_mutex);
356clock_disable:
357 vpu_clock_disable(vpu);
358
359 return ret;
360}
361EXPORT_SYMBOL_GPL(vpu_ipi_send);
362
363static void vpu_wdt_reset_func(struct work_struct *ws)
364{
365 struct vpu_wdt *wdt = container_of(ws, struct vpu_wdt, ws);
366 struct mtk_vpu *vpu = container_of(wdt, struct mtk_vpu, wdt);
367 struct vpu_wdt_handler *handler = wdt->handler;
368 int index, ret;
369
370 dev_info(vpu->dev, "vpu reset\n");
371 ret = vpu_clock_enable(vpu);
372 if (ret) {
373 dev_err(vpu->dev, "[VPU] wdt enables clock failed %d\n", ret);
374 return;
375 }
376 mutex_lock(&vpu->vpu_mutex);
377 vpu_cfg_writel(vpu, 0x0, VPU_RESET);
378 vpu->fw_loaded = false;
379 mutex_unlock(&vpu->vpu_mutex);
380 vpu_clock_disable(vpu);
381
382 for (index = 0; index < VPU_RST_MAX; index++) {
383 if (handler[index].reset_func) {
384 handler[index].reset_func(handler[index].priv);
385 dev_dbg(vpu->dev, "wdt handler func %d\n", index);
386 }
387 }
388}
389
390int vpu_wdt_reg_handler(struct platform_device *pdev,
391 void wdt_reset(void *),
392 void *priv, enum rst_id id)
393{
394 struct mtk_vpu *vpu = platform_get_drvdata(pdev);
395 struct vpu_wdt_handler *handler;
396
397 if (!vpu) {
398 dev_err(&pdev->dev, "vpu device in not ready\n");
399 return -EPROBE_DEFER;
400 }
401
402 handler = vpu->wdt.handler;
403
404 if (id >= 0 && id < VPU_RST_MAX && wdt_reset) {
405 dev_dbg(vpu->dev, "wdt register id %d\n", id);
406 mutex_lock(&vpu->vpu_mutex);
407 handler[id].reset_func = wdt_reset;
408 handler[id].priv = priv;
409 mutex_unlock(&vpu->vpu_mutex);
410 return 0;
411 }
412
413 dev_err(vpu->dev, "register vpu wdt handler failed\n");
414 return -EINVAL;
415}
416EXPORT_SYMBOL_GPL(vpu_wdt_reg_handler);
417
418unsigned int vpu_get_venc_hw_capa(struct platform_device *pdev)
419{
420 struct mtk_vpu *vpu = platform_get_drvdata(pdev);
421
422 return vpu->run.enc_capability;
423}
424EXPORT_SYMBOL_GPL(vpu_get_venc_hw_capa);
425
426void *vpu_mapping_dm_addr(struct platform_device *pdev,
427 u32 dtcm_dmem_addr)
428{
429 struct mtk_vpu *vpu = platform_get_drvdata(pdev);
430
431 if (!dtcm_dmem_addr ||
432 (dtcm_dmem_addr > (VPU_DTCM_SIZE + VPU_EXT_D_SIZE))) {
433 dev_err(vpu->dev, "invalid virtual data memory address\n");
434 return ERR_PTR(-EINVAL);
435 }
436
437 if (dtcm_dmem_addr < VPU_DTCM_SIZE)
438 return (__force void *)(dtcm_dmem_addr + vpu->reg.tcm +
439 VPU_DTCM_OFFSET);
440
441 return vpu->extmem[D_FW].va + (dtcm_dmem_addr - VPU_DTCM_SIZE);
442}
443EXPORT_SYMBOL_GPL(vpu_mapping_dm_addr);
444
445struct platform_device *vpu_get_plat_device(struct platform_device *pdev)
446{
447 struct device *dev = &pdev->dev;
448 struct device_node *vpu_node;
449 struct platform_device *vpu_pdev;
450
451 vpu_node = of_parse_phandle(dev->of_node, "mediatek,vpu", 0);
452 if (!vpu_node) {
453 dev_err(dev, "can't get vpu node\n");
454 return NULL;
455 }
456
457 vpu_pdev = of_find_device_by_node(vpu_node);
458 if (WARN_ON(!vpu_pdev)) {
459 dev_err(dev, "vpu pdev failed\n");
460 of_node_put(vpu_node);
461 return NULL;
462 }
463
464 return vpu_pdev;
465}
466EXPORT_SYMBOL_GPL(vpu_get_plat_device);
467
468/* load vpu program/data memory */
469static int load_requested_vpu(struct mtk_vpu *vpu,
470 const struct firmware *vpu_fw,
471 u8 fw_type)
472{
473 size_t tcm_size = fw_type ? VPU_DTCM_SIZE : VPU_PTCM_SIZE;
474 size_t fw_size = fw_type ? VPU_D_FW_SIZE : VPU_P_FW_SIZE;
475 char *fw_name = fw_type ? VPU_D_FW : VPU_P_FW;
476 size_t dl_size = 0;
477 size_t extra_fw_size = 0;
478 void *dest;
479 int ret;
480
481 ret = request_firmware(&vpu_fw, fw_name, vpu->dev);
482 if (ret < 0) {
483 dev_err(vpu->dev, "Failed to load %s, %d\n", fw_name, ret);
484 return ret;
485 }
486 dl_size = vpu_fw->size;
487 if (dl_size > fw_size) {
488 dev_err(vpu->dev, "fw %s size %zu is abnormal\n", fw_name,
489 dl_size);
490 release_firmware(vpu_fw);
491 return -EFBIG;
492 }
493 dev_dbg(vpu->dev, "Downloaded fw %s size: %zu.\n",
494 fw_name,
495 dl_size);
496 /* reset VPU */
497 vpu_cfg_writel(vpu, 0x0, VPU_RESET);
498
499 /* handle extended firmware size */
500 if (dl_size > tcm_size) {
501 dev_dbg(vpu->dev, "fw size %zu > limited fw size %zu\n",
502 dl_size, tcm_size);
503 extra_fw_size = dl_size - tcm_size;
504 dev_dbg(vpu->dev, "extra_fw_size %zu\n", extra_fw_size);
505 dl_size = tcm_size;
506 }
507 dest = (__force void *)vpu->reg.tcm;
508 if (fw_type == D_FW)
509 dest += VPU_DTCM_OFFSET;
510 memcpy(dest, vpu_fw->data, dl_size);
511 /* download to extended memory if need */
512 if (extra_fw_size > 0) {
513 dest = vpu->extmem[fw_type].va;
514 dev_dbg(vpu->dev, "download extended memory type %x\n",
515 fw_type);
516 memcpy(dest, vpu_fw->data + tcm_size, extra_fw_size);
517 }
518
519 release_firmware(vpu_fw);
520
521 return 0;
522}
523
524int vpu_load_firmware(struct platform_device *pdev)
525{
526 struct mtk_vpu *vpu = platform_get_drvdata(pdev);
527 struct device *dev = &pdev->dev;
528 struct vpu_run *run = &vpu->run;
529 const struct firmware *vpu_fw = NULL;
530 int ret;
531
532 if (!pdev) {
533 dev_err(dev, "VPU platform device is invalid\n");
534 return -EINVAL;
535 }
536
537 mutex_lock(&vpu->vpu_mutex);
538 if (vpu->fw_loaded) {
539 mutex_unlock(&vpu->vpu_mutex);
540 return 0;
541 }
542 mutex_unlock(&vpu->vpu_mutex);
543
544 ret = vpu_clock_enable(vpu);
545 if (ret) {
546 dev_err(dev, "enable clock failed %d\n", ret);
547 return ret;
548 }
549
550 mutex_lock(&vpu->vpu_mutex);
551
552 run->signaled = false;
553 dev_dbg(vpu->dev, "firmware request\n");
554 /* Downloading program firmware to device*/
555 ret = load_requested_vpu(vpu, vpu_fw, P_FW);
556 if (ret < 0) {
557 dev_err(dev, "Failed to request %s, %d\n", VPU_P_FW, ret);
558 goto OUT_LOAD_FW;
559 }
560
561 /* Downloading data firmware to device */
562 ret = load_requested_vpu(vpu, vpu_fw, D_FW);
563 if (ret < 0) {
564 dev_err(dev, "Failed to request %s, %d\n", VPU_D_FW, ret);
565 goto OUT_LOAD_FW;
566 }
567
568 vpu->fw_loaded = true;
569 /* boot up vpu */
570 vpu_cfg_writel(vpu, 0x1, VPU_RESET);
571
572 ret = wait_event_interruptible_timeout(run->wq,
573 run->signaled,
574 msecs_to_jiffies(INIT_TIMEOUT_MS)
575 );
576 if (ret == 0) {
577 ret = -ETIME;
578 dev_err(dev, "wait vpu initialization timout!\n");
579 goto OUT_LOAD_FW;
580 } else if (-ERESTARTSYS == ret) {
581 dev_err(dev, "wait vpu interrupted by a signal!\n");
582 goto OUT_LOAD_FW;
583 }
584
585 ret = 0;
586 dev_info(dev, "vpu is ready. Fw version %s\n", run->fw_ver);
587
588OUT_LOAD_FW:
589 mutex_unlock(&vpu->vpu_mutex);
590 vpu_clock_disable(vpu);
591
592 return ret;
593}
594EXPORT_SYMBOL_GPL(vpu_load_firmware);
595
596static void vpu_init_ipi_handler(void *data, unsigned int len, void *priv)
597{
598 struct mtk_vpu *vpu = (struct mtk_vpu *)priv;
599 struct vpu_run *run = (struct vpu_run *)data;
600
601 vpu->run.signaled = run->signaled;
602 strncpy(vpu->run.fw_ver, run->fw_ver, VPU_FW_VER_LEN);
603 vpu->run.enc_capability = run->enc_capability;
604 wake_up_interruptible(&vpu->run.wq);
605}
606
607#ifdef CONFIG_DEBUG_FS
608static ssize_t vpu_debug_read(struct file *file, char __user *user_buf,
609 size_t count, loff_t *ppos)
610{
611 char buf[256];
612 unsigned int len;
613 unsigned int running, pc, vpu_to_host, host_to_vpu, wdt;
614 int ret;
615 struct device *dev = file->private_data;
616 struct mtk_vpu *vpu = dev_get_drvdata(dev);
617
618 ret = vpu_clock_enable(vpu);
619 if (ret) {
620 dev_err(vpu->dev, "[VPU] enable clock failed %d\n", ret);
621 return 0;
622 }
623
624 /* vpu register status */
625 running = vpu_running(vpu);
626 pc = vpu_cfg_readl(vpu, VPU_PC_REG);
627 wdt = vpu_cfg_readl(vpu, VPU_WDT_REG);
628 host_to_vpu = vpu_cfg_readl(vpu, HOST_TO_VPU);
629 vpu_to_host = vpu_cfg_readl(vpu, VPU_TO_HOST);
630 vpu_clock_disable(vpu);
631
632 if (running) {
633 len = snprintf(buf, sizeof(buf), "VPU is running\n\n"
634 "FW Version: %s\n"
635 "PC: 0x%x\n"
636 "WDT: 0x%x\n"
637 "Host to VPU: 0x%x\n"
638 "VPU to Host: 0x%x\n",
639 vpu->run.fw_ver, pc, wdt,
640 host_to_vpu, vpu_to_host);
641 } else {
642 len = snprintf(buf, sizeof(buf), "VPU not running\n");
643 }
644
645 return simple_read_from_buffer(user_buf, count, ppos, buf, len);
646}
647
648static const struct file_operations vpu_debug_fops = {
649 .open = simple_open,
650 .read = vpu_debug_read,
651};
652#endif /* CONFIG_DEBUG_FS */
653
654static void vpu_free_ext_mem(struct mtk_vpu *vpu, u8 fw_type)
655{
656 struct device *dev = vpu->dev;
657 size_t fw_ext_size = fw_type ? VPU_EXT_D_SIZE : VPU_EXT_P_SIZE;
658
659 dma_free_coherent(dev, fw_ext_size, vpu->extmem[fw_type].va,
660 vpu->extmem[fw_type].pa);
661}
662
663static int vpu_alloc_ext_mem(struct mtk_vpu *vpu, u32 fw_type)
664{
665 struct device *dev = vpu->dev;
666 size_t fw_ext_size = fw_type ? VPU_EXT_D_SIZE : VPU_EXT_P_SIZE;
667 u32 vpu_ext_mem0 = fw_type ? VPU_DMEM_EXT0_ADDR : VPU_PMEM_EXT0_ADDR;
668 u32 vpu_ext_mem1 = fw_type ? VPU_DMEM_EXT1_ADDR : VPU_PMEM_EXT1_ADDR;
669 u32 offset_4gb = vpu->enable_4GB ? 0x40000000 : 0;
670
671 vpu->extmem[fw_type].va = dma_alloc_coherent(dev,
672 fw_ext_size,
673 &vpu->extmem[fw_type].pa,
674 GFP_KERNEL);
675 if (!vpu->extmem[fw_type].va) {
676 dev_err(dev, "Failed to allocate the extended program memory\n");
677 return PTR_ERR(vpu->extmem[fw_type].va);
678 }
679
680 /* Disable extend0. Enable extend1 */
681 vpu_cfg_writel(vpu, 0x1, vpu_ext_mem0);
682 vpu_cfg_writel(vpu, (vpu->extmem[fw_type].pa & 0xFFFFF000) + offset_4gb,
683 vpu_ext_mem1);
684
685 dev_info(dev, "%s extend memory phy=0x%llx virt=0x%p\n",
686 fw_type ? "Data" : "Program",
687 (unsigned long long)vpu->extmem[fw_type].pa,
688 vpu->extmem[fw_type].va);
689
690 return 0;
691}
692
693static void vpu_ipi_handler(struct mtk_vpu *vpu)
694{
695 struct share_obj *rcv_obj = vpu->recv_buf;
696 struct vpu_ipi_desc *ipi_desc = vpu->ipi_desc;
697
698 if (rcv_obj->id < IPI_MAX && ipi_desc[rcv_obj->id].handler) {
699 ipi_desc[rcv_obj->id].handler(rcv_obj->share_buf,
700 rcv_obj->len,
701 ipi_desc[rcv_obj->id].priv);
702 if (rcv_obj->id > IPI_VPU_INIT) {
703 vpu->ipi_id_ack[rcv_obj->id] = true;
704 wake_up(&vpu->ack_wq);
705 }
706 } else {
707 dev_err(vpu->dev, "No such ipi id = %d\n", rcv_obj->id);
708 }
709}
710
711static int vpu_ipi_init(struct mtk_vpu *vpu)
712{
713 /* Disable VPU to host interrupt */
714 vpu_cfg_writel(vpu, 0x0, VPU_TO_HOST);
715
716 /* shared buffer initialization */
717 vpu->recv_buf = (__force struct share_obj *)(vpu->reg.tcm +
718 VPU_DTCM_OFFSET);
719 vpu->send_buf = vpu->recv_buf + 1;
720 memset(vpu->recv_buf, 0, sizeof(struct share_obj));
721 memset(vpu->send_buf, 0, sizeof(struct share_obj));
722
723 return 0;
724}
725
726static irqreturn_t vpu_irq_handler(int irq, void *priv)
727{
728 struct mtk_vpu *vpu = priv;
729 u32 vpu_to_host;
730 int ret;
731
732 /*
733 * Clock should have been enabled already.
734 * Enable again in case vpu_ipi_send times out
735 * and has disabled the clock.
736 */
737 ret = clk_enable(vpu->clk);
738 if (ret) {
739 dev_err(vpu->dev, "[VPU] enable clock failed %d\n", ret);
740 return IRQ_NONE;
741 }
742 vpu_to_host = vpu_cfg_readl(vpu, VPU_TO_HOST);
743 if (vpu_to_host & VPU_IPC_INT) {
744 vpu_ipi_handler(vpu);
745 } else {
746 dev_err(vpu->dev, "vpu watchdog timeout! 0x%x", vpu_to_host);
747 queue_work(vpu->wdt.wq, &vpu->wdt.ws);
748 }
749
750 /* VPU won't send another interrupt until we set VPU_TO_HOST to 0. */
751 vpu_cfg_writel(vpu, 0x0, VPU_TO_HOST);
752 clk_disable(vpu->clk);
753
754 return IRQ_HANDLED;
755}
756
757#ifdef CONFIG_DEBUG_FS
758static struct dentry *vpu_debugfs;
759#endif
760static int mtk_vpu_probe(struct platform_device *pdev)
761{
762 struct mtk_vpu *vpu;
763 struct device *dev;
764 struct resource *res;
765 int ret = 0;
766
767 dev_dbg(&pdev->dev, "initialization\n");
768
769 dev = &pdev->dev;
770 vpu = devm_kzalloc(dev, sizeof(*vpu), GFP_KERNEL);
771 if (!vpu)
772 return -ENOMEM;
773
774 vpu->dev = &pdev->dev;
775 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "tcm");
776 vpu->reg.tcm = devm_ioremap_resource(dev, res);
777 if (IS_ERR((__force void *)vpu->reg.tcm))
778 return PTR_ERR((__force void *)vpu->reg.tcm);
779
780 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "cfg_reg");
781 vpu->reg.cfg = devm_ioremap_resource(dev, res);
782 if (IS_ERR((__force void *)vpu->reg.cfg))
783 return PTR_ERR((__force void *)vpu->reg.cfg);
784
785 /* Get VPU clock */
786 vpu->clk = devm_clk_get(dev, "main");
787 if (IS_ERR(vpu->clk)) {
788 dev_err(dev, "get vpu clock failed\n");
789 return PTR_ERR(vpu->clk);
790 }
791
792 platform_set_drvdata(pdev, vpu);
793
794 ret = clk_prepare(vpu->clk);
795 if (ret) {
796 dev_err(dev, "prepare vpu clock failed\n");
797 return ret;
798 }
799
800 /* VPU watchdog */
801 vpu->wdt.wq = create_singlethread_workqueue("vpu_wdt");
802 if (!vpu->wdt.wq) {
803 dev_err(dev, "initialize wdt workqueue failed\n");
804 return -ENOMEM;
805 }
806 INIT_WORK(&vpu->wdt.ws, vpu_wdt_reset_func);
807 mutex_init(&vpu->vpu_mutex);
808
809 ret = vpu_clock_enable(vpu);
810 if (ret) {
811 dev_err(dev, "enable vpu clock failed\n");
812 goto workqueue_destroy;
813 }
814
815 dev_dbg(dev, "vpu ipi init\n");
816 ret = vpu_ipi_init(vpu);
817 if (ret) {
818 dev_err(dev, "Failed to init ipi\n");
819 goto disable_vpu_clk;
820 }
821
822 /* register vpu initialization IPI */
823 ret = vpu_ipi_register(pdev, IPI_VPU_INIT, vpu_init_ipi_handler,
824 "vpu_init", vpu);
825 if (ret) {
826 dev_err(dev, "Failed to register IPI_VPU_INIT\n");
827 goto vpu_mutex_destroy;
828 }
829
830#ifdef CONFIG_DEBUG_FS
831 vpu_debugfs = debugfs_create_file("mtk_vpu", S_IRUGO, NULL, (void *)dev,
832 &vpu_debug_fops);
833 if (!vpu_debugfs) {
834 ret = -ENOMEM;
835 goto cleanup_ipi;
836 }
837#endif
838
839 /* Set PTCM to 96K and DTCM to 32K */
840 vpu_cfg_writel(vpu, 0x2, VPU_TCM_CFG);
841
842 vpu->enable_4GB = !!(totalram_pages > (SZ_2G >> PAGE_SHIFT));
843 dev_info(dev, "4GB mode %u\n", vpu->enable_4GB);
844
845 if (vpu->enable_4GB) {
846 ret = of_reserved_mem_device_init(dev);
847 if (ret)
848 dev_info(dev, "init reserved memory failed\n");
849 /* continue to use dynamic allocation if failed */
850 }
851
852 ret = vpu_alloc_ext_mem(vpu, D_FW);
853 if (ret) {
854 dev_err(dev, "Allocate DM failed\n");
855 goto remove_debugfs;
856 }
857
858 ret = vpu_alloc_ext_mem(vpu, P_FW);
859 if (ret) {
860 dev_err(dev, "Allocate PM failed\n");
861 goto free_d_mem;
862 }
863
864 init_waitqueue_head(&vpu->run.wq);
865 init_waitqueue_head(&vpu->ack_wq);
866
867 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
868 if (!res) {
869 dev_err(dev, "get IRQ resource failed.\n");
870 ret = -ENXIO;
871 goto free_p_mem;
872 }
873 vpu->reg.irq = platform_get_irq(pdev, 0);
874 ret = devm_request_irq(dev, vpu->reg.irq, vpu_irq_handler, 0,
875 pdev->name, vpu);
876 if (ret) {
877 dev_err(dev, "failed to request irq\n");
878 goto free_p_mem;
879 }
880
881 vpu_clock_disable(vpu);
882 dev_dbg(dev, "initialization completed\n");
883
884 return 0;
885
886free_p_mem:
887 vpu_free_ext_mem(vpu, P_FW);
888free_d_mem:
889 vpu_free_ext_mem(vpu, D_FW);
890remove_debugfs:
891 of_reserved_mem_device_release(dev);
892#ifdef CONFIG_DEBUG_FS
893 debugfs_remove(vpu_debugfs);
894cleanup_ipi:
895#endif
896 memset(vpu->ipi_desc, 0, sizeof(struct vpu_ipi_desc) * IPI_MAX);
897vpu_mutex_destroy:
898 mutex_destroy(&vpu->vpu_mutex);
899disable_vpu_clk:
900 vpu_clock_disable(vpu);
901workqueue_destroy:
902 destroy_workqueue(vpu->wdt.wq);
903
904 return ret;
905}
906
907static const struct of_device_id mtk_vpu_match[] = {
908 {
909 .compatible = "mediatek,mt8173-vpu",
910 },
911 {},
912};
913MODULE_DEVICE_TABLE(of, mtk_vpu_match);
914
915static int mtk_vpu_remove(struct platform_device *pdev)
916{
917 struct mtk_vpu *vpu = platform_get_drvdata(pdev);
918
919#ifdef CONFIG_DEBUG_FS
920 debugfs_remove(vpu_debugfs);
921#endif
922 if (vpu->wdt.wq) {
923 flush_workqueue(vpu->wdt.wq);
924 destroy_workqueue(vpu->wdt.wq);
925 }
926 vpu_free_ext_mem(vpu, P_FW);
927 vpu_free_ext_mem(vpu, D_FW);
928 mutex_destroy(&vpu->vpu_mutex);
929 clk_unprepare(vpu->clk);
930
931 return 0;
932}
933
934static struct platform_driver mtk_vpu_driver = {
935 .probe = mtk_vpu_probe,
936 .remove = mtk_vpu_remove,
937 .driver = {
938 .name = "mtk_vpu",
939 .of_match_table = mtk_vpu_match,
940 },
941};
942
943module_platform_driver(mtk_vpu_driver);
944
945MODULE_LICENSE("GPL v2");
946MODULE_DESCRIPTION("Mediatek Video Prosessor Unit driver");
diff --git a/drivers/media/platform/mtk-vpu/mtk_vpu.h b/drivers/media/platform/mtk-vpu/mtk_vpu.h
new file mode 100644
index 000000000000..5ab37f04bdfd
--- /dev/null
+++ b/drivers/media/platform/mtk-vpu/mtk_vpu.h
@@ -0,0 +1,162 @@
1/*
2* Copyright (c) 2016 MediaTek Inc.
3* Author: Andrew-CT Chen <andrew-ct.chen@mediatek.com>
4*
5* This program is free software; you can redistribute it and/or modify
6* it under the terms of the GNU General Public License version 2 as
7* published by the Free Software Foundation.
8*
9* This program is distributed in the hope that it will be useful,
10* but WITHOUT ANY WARRANTY; without even the implied warranty of
11* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12* GNU General Public License for more details.
13*/
14
15#ifndef _MTK_VPU_H
16#define _MTK_VPU_H
17
18#include <linux/platform_device.h>
19
20/**
21 * VPU (video processor unit) is a tiny processor controlling video hardware
22 * related to video codec, scaling and color format converting.
23 * VPU interfaces with other blocks by share memory and interrupt.
24 **/
25
26typedef void (*ipi_handler_t) (void *data,
27 unsigned int len,
28 void *priv);
29
30/**
31 * enum ipi_id - the id of inter-processor interrupt
32 *
33 * @IPI_VPU_INIT: The interrupt from vpu is to notfiy kernel
34 VPU initialization completed.
35 IPI_VPU_INIT is sent from VPU when firmware is
36 loaded. AP doesn't need to send IPI_VPU_INIT
37 command to VPU.
38 For other IPI below, AP should send the request
39 to VPU to trigger the interrupt.
40 * @IPI_VENC_H264: The interrupt from vpu is to notify kernel to
41 handle H264 video encoder job, and vice versa.
42 * @IPI_VENC_VP8: The interrupt fro vpu is to notify kernel to
43 handle VP8 video encoder job,, and vice versa.
44 * @IPI_MAX: The maximum IPI number
45 */
46
47enum ipi_id {
48 IPI_VPU_INIT = 0,
49 IPI_VENC_H264,
50 IPI_VENC_VP8,
51 IPI_MAX,
52};
53
54/**
55 * enum rst_id - reset id to register reset function for VPU watchdog timeout
56 *
57 * @VPU_RST_ENC: encoder reset id
58 * @VPU_RST_MAX: maximum reset id
59 */
60enum rst_id {
61 VPU_RST_ENC,
62 VPU_RST_MAX,
63};
64
65/**
66 * vpu_ipi_register - register an ipi function
67 *
68 * @pdev: VPU platform device
69 * @id: IPI ID
70 * @handler: IPI handler
71 * @name: IPI name
72 * @priv: private data for IPI handler
73 *
74 * Register an ipi function to receive ipi interrupt from VPU.
75 *
76 * Return: Return 0 if ipi registers successfully, otherwise it is failed.
77 */
78int vpu_ipi_register(struct platform_device *pdev, enum ipi_id id,
79 ipi_handler_t handler, const char *name, void *priv);
80
81/**
82 * vpu_ipi_send - send data from AP to vpu.
83 *
84 * @pdev: VPU platform device
85 * @id: IPI ID
86 * @buf: the data buffer
87 * @len: the data buffer length
88 *
89 * This function is thread-safe. When this function returns,
90 * VPU has received the data and starts the processing.
91 * When the processing completes, IPI handler registered
92 * by vpu_ipi_register will be called in interrupt context.
93 *
94 * Return: Return 0 if sending data successfully, otherwise it is failed.
95 **/
96int vpu_ipi_send(struct platform_device *pdev,
97 enum ipi_id id, void *buf,
98 unsigned int len);
99
100/**
101 * vpu_get_plat_device - get VPU's platform device
102 *
103 * @pdev: the platform device of the module requesting VPU platform
104 * device for using VPU API.
105 *
106 * Return: Return NULL if it is failed.
107 * otherwise it is VPU's platform device
108 **/
109struct platform_device *vpu_get_plat_device(struct platform_device *pdev);
110
111/**
112 * vpu_wdt_reg_handler - register a VPU watchdog handler
113 *
114 * @pdev: VPU platform device
115 * @vpu_wdt_reset_func: the callback reset function
116 * @private_data: the private data for reset function
117 * @rst_id: reset id
118 *
119 * Register a handler performing own tasks when vpu reset by watchdog
120 *
121 * Return: Return 0 if the handler is added successfully,
122 * otherwise it is failed.
123 *
124 **/
125int vpu_wdt_reg_handler(struct platform_device *pdev,
126 void vpu_wdt_reset_func(void *),
127 void *priv, enum rst_id id);
128/**
129 * vpu_get_venc_hw_capa - get video encoder hardware capability
130 *
131 * @pdev: VPU platform device
132 *
133 * Return: video encoder hardware capability
134 **/
135unsigned int vpu_get_venc_hw_capa(struct platform_device *pdev);
136
137/**
138 * vpu_load_firmware - download VPU firmware and boot it
139 *
140 * @pdev: VPU platform device
141 *
142 * Return: Return 0 if downloading firmware successfully,
143 * otherwise it is failed
144 **/
145int vpu_load_firmware(struct platform_device *pdev);
146
147/**
148 * vpu_mapping_dm_addr - Mapping DTCM/DMEM to kernel virtual address
149 *
150 * @pdev: VPU platform device
151 * @dmem_addr: VPU's data memory address
152 *
153 * Mapping the VPU's DTCM (Data Tightly-Coupled Memory) /
154 * DMEM (Data Extended Memory) memory address to
155 * kernel virtual address.
156 *
157 * Return: Return ERR_PTR(-EINVAL) if mapping failed,
158 * otherwise the mapped kernel virtual address
159 **/
160void *vpu_mapping_dm_addr(struct platform_device *pdev,
161 u32 dtcm_dmem_addr);
162#endif /* _MTK_VPU_H */
diff --git a/drivers/media/platform/mx2_emmaprp.c b/drivers/media/platform/mx2_emmaprp.c
index 3c4012d42d69..c639406fe72e 100644
--- a/drivers/media/platform/mx2_emmaprp.c
+++ b/drivers/media/platform/mx2_emmaprp.c
@@ -211,7 +211,6 @@ struct emmaprp_dev {
211 struct clk *clk_emma_ahb, *clk_emma_ipg; 211 struct clk *clk_emma_ahb, *clk_emma_ipg;
212 212
213 struct v4l2_m2m_dev *m2m_dev; 213 struct v4l2_m2m_dev *m2m_dev;
214 struct vb2_alloc_ctx *alloc_ctx;
215}; 214};
216 215
217struct emmaprp_ctx { 216struct emmaprp_ctx {
@@ -690,7 +689,7 @@ static const struct v4l2_ioctl_ops emmaprp_ioctl_ops = {
690 */ 689 */
691static int emmaprp_queue_setup(struct vb2_queue *vq, 690static int emmaprp_queue_setup(struct vb2_queue *vq,
692 unsigned int *nbuffers, unsigned int *nplanes, 691 unsigned int *nbuffers, unsigned int *nplanes,
693 unsigned int sizes[], void *alloc_ctxs[]) 692 unsigned int sizes[], struct device *alloc_devs[])
694{ 693{
695 struct emmaprp_ctx *ctx = vb2_get_drv_priv(vq); 694 struct emmaprp_ctx *ctx = vb2_get_drv_priv(vq);
696 struct emmaprp_q_data *q_data; 695 struct emmaprp_q_data *q_data;
@@ -710,8 +709,6 @@ static int emmaprp_queue_setup(struct vb2_queue *vq,
710 *nbuffers = count; 709 *nbuffers = count;
711 sizes[0] = size; 710 sizes[0] = size;
712 711
713 alloc_ctxs[0] = ctx->dev->alloc_ctx;
714
715 dprintk(ctx->dev, "get %d buffer(s) of size %d each.\n", count, size); 712 dprintk(ctx->dev, "get %d buffer(s) of size %d each.\n", count, size);
716 713
717 return 0; 714 return 0;
@@ -765,6 +762,7 @@ static int queue_init(void *priv, struct vb2_queue *src_vq,
765 src_vq->ops = &emmaprp_qops; 762 src_vq->ops = &emmaprp_qops;
766 src_vq->mem_ops = &vb2_dma_contig_memops; 763 src_vq->mem_ops = &vb2_dma_contig_memops;
767 src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY; 764 src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
765 src_vq->dev = ctx->dev->v4l2_dev.dev;
768 766
769 ret = vb2_queue_init(src_vq); 767 ret = vb2_queue_init(src_vq);
770 if (ret) 768 if (ret)
@@ -777,6 +775,7 @@ static int queue_init(void *priv, struct vb2_queue *src_vq,
777 dst_vq->ops = &emmaprp_qops; 775 dst_vq->ops = &emmaprp_qops;
778 dst_vq->mem_ops = &vb2_dma_contig_memops; 776 dst_vq->mem_ops = &vb2_dma_contig_memops;
779 dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY; 777 dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
778 dst_vq->dev = ctx->dev->v4l2_dev.dev;
780 779
781 return vb2_queue_init(dst_vq); 780 return vb2_queue_init(dst_vq);
782} 781}
@@ -948,18 +947,11 @@ static int emmaprp_probe(struct platform_device *pdev)
948 if (ret) 947 if (ret)
949 goto rel_vdev; 948 goto rel_vdev;
950 949
951 pcdev->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
952 if (IS_ERR(pcdev->alloc_ctx)) {
953 v4l2_err(&pcdev->v4l2_dev, "Failed to alloc vb2 context\n");
954 ret = PTR_ERR(pcdev->alloc_ctx);
955 goto rel_vdev;
956 }
957
958 pcdev->m2m_dev = v4l2_m2m_init(&m2m_ops); 950 pcdev->m2m_dev = v4l2_m2m_init(&m2m_ops);
959 if (IS_ERR(pcdev->m2m_dev)) { 951 if (IS_ERR(pcdev->m2m_dev)) {
960 v4l2_err(&pcdev->v4l2_dev, "Failed to init mem2mem device\n"); 952 v4l2_err(&pcdev->v4l2_dev, "Failed to init mem2mem device\n");
961 ret = PTR_ERR(pcdev->m2m_dev); 953 ret = PTR_ERR(pcdev->m2m_dev);
962 goto rel_ctx; 954 goto rel_vdev;
963 } 955 }
964 956
965 ret = video_register_device(vfd, VFL_TYPE_GRABBER, 0); 957 ret = video_register_device(vfd, VFL_TYPE_GRABBER, 0);
@@ -973,8 +965,6 @@ static int emmaprp_probe(struct platform_device *pdev)
973 965
974rel_m2m: 966rel_m2m:
975 v4l2_m2m_release(pcdev->m2m_dev); 967 v4l2_m2m_release(pcdev->m2m_dev);
976rel_ctx:
977 vb2_dma_contig_cleanup_ctx(pcdev->alloc_ctx);
978rel_vdev: 968rel_vdev:
979 video_device_release(vfd); 969 video_device_release(vfd);
980unreg_dev: 970unreg_dev:
@@ -993,7 +983,6 @@ static int emmaprp_remove(struct platform_device *pdev)
993 983
994 video_unregister_device(pcdev->vfd); 984 video_unregister_device(pcdev->vfd);
995 v4l2_m2m_release(pcdev->m2m_dev); 985 v4l2_m2m_release(pcdev->m2m_dev);
996 vb2_dma_contig_cleanup_ctx(pcdev->alloc_ctx);
997 v4l2_device_unregister(&pcdev->v4l2_dev); 986 v4l2_device_unregister(&pcdev->v4l2_dev);
998 mutex_destroy(&pcdev->dev_mutex); 987 mutex_destroy(&pcdev->dev_mutex);
999 988
diff --git a/drivers/media/platform/omap/omap_vout.c b/drivers/media/platform/omap/omap_vout.c
index 70c28d19ea04..4afc999c0780 100644
--- a/drivers/media/platform/omap/omap_vout.c
+++ b/drivers/media/platform/omap/omap_vout.c
@@ -1318,71 +1318,16 @@ s_crop_err:
1318 return ret; 1318 return ret;
1319} 1319}
1320 1320
1321static int vidioc_queryctrl(struct file *file, void *fh, 1321static int omap_vout_s_ctrl(struct v4l2_ctrl *ctrl)
1322 struct v4l2_queryctrl *ctrl)
1323{ 1322{
1323 struct omap_vout_device *vout =
1324 container_of(ctrl->handler, struct omap_vout_device, ctrl_handler);
1324 int ret = 0; 1325 int ret = 0;
1325 1326
1326 switch (ctrl->id) { 1327 switch (ctrl->id) {
1327 case V4L2_CID_ROTATE: 1328 case V4L2_CID_ROTATE: {
1328 ret = v4l2_ctrl_query_fill(ctrl, 0, 270, 90, 0);
1329 break;
1330 case V4L2_CID_BG_COLOR:
1331 ret = v4l2_ctrl_query_fill(ctrl, 0, 0xFFFFFF, 1, 0);
1332 break;
1333 case V4L2_CID_VFLIP:
1334 ret = v4l2_ctrl_query_fill(ctrl, 0, 1, 1, 0);
1335 break;
1336 default:
1337 ctrl->name[0] = '\0';
1338 ret = -EINVAL;
1339 }
1340 return ret;
1341}
1342
1343static int vidioc_g_ctrl(struct file *file, void *fh, struct v4l2_control *ctrl)
1344{
1345 int ret = 0;
1346 struct omap_vout_device *vout = fh;
1347
1348 switch (ctrl->id) {
1349 case V4L2_CID_ROTATE:
1350 ctrl->value = vout->control[0].value;
1351 break;
1352 case V4L2_CID_BG_COLOR:
1353 {
1354 struct omap_overlay_manager_info info;
1355 struct omap_overlay *ovl;
1356
1357 ovl = vout->vid_info.overlays[0];
1358 if (!ovl->manager || !ovl->manager->get_manager_info) {
1359 ret = -EINVAL;
1360 break;
1361 }
1362
1363 ovl->manager->get_manager_info(ovl->manager, &info);
1364 ctrl->value = info.default_color;
1365 break;
1366 }
1367 case V4L2_CID_VFLIP:
1368 ctrl->value = vout->control[2].value;
1369 break;
1370 default:
1371 ret = -EINVAL;
1372 }
1373 return ret;
1374}
1375
1376static int vidioc_s_ctrl(struct file *file, void *fh, struct v4l2_control *a)
1377{
1378 int ret = 0;
1379 struct omap_vout_device *vout = fh;
1380
1381 switch (a->id) {
1382 case V4L2_CID_ROTATE:
1383 {
1384 struct omapvideo_info *ovid; 1329 struct omapvideo_info *ovid;
1385 int rotation = a->value; 1330 int rotation = ctrl->val;
1386 1331
1387 ovid = &vout->vid_info; 1332 ovid = &vout->vid_info;
1388 1333
@@ -1405,15 +1350,13 @@ static int vidioc_s_ctrl(struct file *file, void *fh, struct v4l2_control *a)
1405 ret = -EINVAL; 1350 ret = -EINVAL;
1406 break; 1351 break;
1407 } 1352 }
1408
1409 vout->control[0].value = rotation;
1410 mutex_unlock(&vout->lock); 1353 mutex_unlock(&vout->lock);
1411 break; 1354 break;
1412 } 1355 }
1413 case V4L2_CID_BG_COLOR: 1356 case V4L2_CID_BG_COLOR:
1414 { 1357 {
1415 struct omap_overlay *ovl; 1358 struct omap_overlay *ovl;
1416 unsigned int color = a->value; 1359 unsigned int color = ctrl->val;
1417 struct omap_overlay_manager_info info; 1360 struct omap_overlay_manager_info info;
1418 1361
1419 ovl = vout->vid_info.overlays[0]; 1362 ovl = vout->vid_info.overlays[0];
@@ -1432,15 +1375,13 @@ static int vidioc_s_ctrl(struct file *file, void *fh, struct v4l2_control *a)
1432 ret = -EINVAL; 1375 ret = -EINVAL;
1433 break; 1376 break;
1434 } 1377 }
1435
1436 vout->control[1].value = color;
1437 mutex_unlock(&vout->lock); 1378 mutex_unlock(&vout->lock);
1438 break; 1379 break;
1439 } 1380 }
1440 case V4L2_CID_VFLIP: 1381 case V4L2_CID_VFLIP:
1441 { 1382 {
1442 struct omapvideo_info *ovid; 1383 struct omapvideo_info *ovid;
1443 unsigned int mirror = a->value; 1384 unsigned int mirror = ctrl->val;
1444 1385
1445 ovid = &vout->vid_info; 1386 ovid = &vout->vid_info;
1446 1387
@@ -1457,16 +1398,19 @@ static int vidioc_s_ctrl(struct file *file, void *fh, struct v4l2_control *a)
1457 break; 1398 break;
1458 } 1399 }
1459 vout->mirror = mirror; 1400 vout->mirror = mirror;
1460 vout->control[2].value = mirror;
1461 mutex_unlock(&vout->lock); 1401 mutex_unlock(&vout->lock);
1462 break; 1402 break;
1463 } 1403 }
1464 default: 1404 default:
1465 ret = -EINVAL; 1405 return -EINVAL;
1466 } 1406 }
1467 return ret; 1407 return ret;
1468} 1408}
1469 1409
1410static const struct v4l2_ctrl_ops omap_vout_ctrl_ops = {
1411 .s_ctrl = omap_vout_s_ctrl,
1412};
1413
1470static int vidioc_reqbufs(struct file *file, void *fh, 1414static int vidioc_reqbufs(struct file *file, void *fh,
1471 struct v4l2_requestbuffers *req) 1415 struct v4l2_requestbuffers *req)
1472{ 1416{
@@ -1831,11 +1775,8 @@ static const struct v4l2_ioctl_ops vout_ioctl_ops = {
1831 .vidioc_g_fmt_vid_out = vidioc_g_fmt_vid_out, 1775 .vidioc_g_fmt_vid_out = vidioc_g_fmt_vid_out,
1832 .vidioc_try_fmt_vid_out = vidioc_try_fmt_vid_out, 1776 .vidioc_try_fmt_vid_out = vidioc_try_fmt_vid_out,
1833 .vidioc_s_fmt_vid_out = vidioc_s_fmt_vid_out, 1777 .vidioc_s_fmt_vid_out = vidioc_s_fmt_vid_out,
1834 .vidioc_queryctrl = vidioc_queryctrl,
1835 .vidioc_g_ctrl = vidioc_g_ctrl,
1836 .vidioc_s_fbuf = vidioc_s_fbuf, 1778 .vidioc_s_fbuf = vidioc_s_fbuf,
1837 .vidioc_g_fbuf = vidioc_g_fbuf, 1779 .vidioc_g_fbuf = vidioc_g_fbuf,
1838 .vidioc_s_ctrl = vidioc_s_ctrl,
1839 .vidioc_try_fmt_vid_out_overlay = vidioc_try_fmt_vid_overlay, 1780 .vidioc_try_fmt_vid_out_overlay = vidioc_try_fmt_vid_overlay,
1840 .vidioc_s_fmt_vid_out_overlay = vidioc_s_fmt_vid_overlay, 1781 .vidioc_s_fmt_vid_out_overlay = vidioc_s_fmt_vid_overlay,
1841 .vidioc_g_fmt_vid_out_overlay = vidioc_g_fmt_vid_overlay, 1782 .vidioc_g_fmt_vid_out_overlay = vidioc_g_fmt_vid_overlay,
@@ -1865,9 +1806,9 @@ static int __init omap_vout_setup_video_data(struct omap_vout_device *vout)
1865{ 1806{
1866 struct video_device *vfd; 1807 struct video_device *vfd;
1867 struct v4l2_pix_format *pix; 1808 struct v4l2_pix_format *pix;
1868 struct v4l2_control *control;
1869 struct omap_overlay *ovl = vout->vid_info.overlays[0]; 1809 struct omap_overlay *ovl = vout->vid_info.overlays[0];
1870 struct omap_dss_device *display = ovl->get_device(ovl); 1810 struct omap_dss_device *display = ovl->get_device(ovl);
1811 struct v4l2_ctrl_handler *hdl;
1871 1812
1872 /* set the default pix */ 1813 /* set the default pix */
1873 pix = &vout->pix; 1814 pix = &vout->pix;
@@ -1896,29 +1837,32 @@ static int __init omap_vout_setup_video_data(struct omap_vout_device *vout)
1896 1837
1897 omap_vout_new_format(pix, &vout->fbuf, &vout->crop, &vout->win); 1838 omap_vout_new_format(pix, &vout->fbuf, &vout->crop, &vout->win);
1898 1839
1899 /*Initialize the control variables for 1840 hdl = &vout->ctrl_handler;
1900 rotation, flipping and background color. */ 1841 v4l2_ctrl_handler_init(hdl, 3);
1901 control = vout->control; 1842 v4l2_ctrl_new_std(hdl, &omap_vout_ctrl_ops,
1902 control[0].id = V4L2_CID_ROTATE; 1843 V4L2_CID_ROTATE, 0, 270, 90, 0);
1903 control[0].value = 0; 1844 v4l2_ctrl_new_std(hdl, &omap_vout_ctrl_ops,
1845 V4L2_CID_BG_COLOR, 0, 0xffffff, 1, 0);
1846 v4l2_ctrl_new_std(hdl, &omap_vout_ctrl_ops,
1847 V4L2_CID_VFLIP, 0, 1, 1, 0);
1848 if (hdl->error)
1849 return hdl->error;
1850
1904 vout->rotation = 0; 1851 vout->rotation = 0;
1905 vout->mirror = false; 1852 vout->mirror = false;
1906 vout->control[2].id = V4L2_CID_HFLIP;
1907 vout->control[2].value = 0;
1908 if (vout->vid_info.rotation_type == VOUT_ROT_VRFB) 1853 if (vout->vid_info.rotation_type == VOUT_ROT_VRFB)
1909 vout->vrfb_bpp = 2; 1854 vout->vrfb_bpp = 2;
1910 1855
1911 control[1].id = V4L2_CID_BG_COLOR;
1912 control[1].value = 0;
1913
1914 /* initialize the video_device struct */ 1856 /* initialize the video_device struct */
1915 vfd = vout->vfd = video_device_alloc(); 1857 vfd = vout->vfd = video_device_alloc();
1916 1858
1917 if (!vfd) { 1859 if (!vfd) {
1918 printk(KERN_ERR VOUT_NAME ": could not allocate" 1860 printk(KERN_ERR VOUT_NAME ": could not allocate"
1919 " video device struct\n"); 1861 " video device struct\n");
1862 v4l2_ctrl_handler_free(hdl);
1920 return -ENOMEM; 1863 return -ENOMEM;
1921 } 1864 }
1865 vfd->ctrl_handler = hdl;
1922 vfd->release = video_device_release; 1866 vfd->release = video_device_release;
1923 vfd->ioctl_ops = &vout_ioctl_ops; 1867 vfd->ioctl_ops = &vout_ioctl_ops;
1924 1868
@@ -2092,6 +2036,7 @@ static void omap_vout_cleanup_device(struct omap_vout_device *vout)
2092 video_unregister_device(vfd); 2036 video_unregister_device(vfd);
2093 } 2037 }
2094 } 2038 }
2039 v4l2_ctrl_handler_free(&vout->ctrl_handler);
2095 if (ovid->rotation_type == VOUT_ROT_VRFB) { 2040 if (ovid->rotation_type == VOUT_ROT_VRFB) {
2096 omap_vout_release_vrfb(vout); 2041 omap_vout_release_vrfb(vout);
2097 /* Free the VRFB buffer if allocated 2042 /* Free the VRFB buffer if allocated
diff --git a/drivers/media/platform/omap/omap_voutdef.h b/drivers/media/platform/omap/omap_voutdef.h
index 9ccfe1f475a4..49de1475e473 100644
--- a/drivers/media/platform/omap/omap_voutdef.h
+++ b/drivers/media/platform/omap/omap_voutdef.h
@@ -11,6 +11,7 @@
11#ifndef OMAP_VOUTDEF_H 11#ifndef OMAP_VOUTDEF_H
12#define OMAP_VOUTDEF_H 12#define OMAP_VOUTDEF_H
13 13
14#include <media/v4l2-ctrls.h>
14#include <video/omapdss.h> 15#include <video/omapdss.h>
15#include <video/omapvrfb.h> 16#include <video/omapvrfb.h>
16 17
@@ -116,6 +117,7 @@ struct omap_vout_device {
116 struct omapvideo_info vid_info; 117 struct omapvideo_info vid_info;
117 struct video_device *vfd; 118 struct video_device *vfd;
118 struct omap2video_device *vid_dev; 119 struct omap2video_device *vid_dev;
120 struct v4l2_ctrl_handler ctrl_handler;
119 int vid; 121 int vid;
120 int opened; 122 int opened;
121 123
@@ -149,12 +151,9 @@ struct omap_vout_device {
149 /* Lock to protect the shared data structures in ioctl */ 151 /* Lock to protect the shared data structures in ioctl */
150 struct mutex lock; 152 struct mutex lock;
151 153
152 /* V4L2 control structure for different control id */
153 struct v4l2_control control[MAX_CID];
154 enum dss_rotation rotation; 154 enum dss_rotation rotation;
155 bool mirror; 155 bool mirror;
156 int flicker_filter; 156 int flicker_filter;
157 /* V4L2 control structure for different control id */
158 157
159 int bpp; /* bytes per pixel */ 158 int bpp; /* bytes per pixel */
160 int vrfb_bpp; /* bytes per pixel with respect to VRFB */ 159 int vrfb_bpp; /* bytes per pixel with respect to VRFB */
diff --git a/drivers/media/platform/omap3isp/ispvideo.c b/drivers/media/platform/omap3isp/ispvideo.c
index 1b1a95d546f6..7d9f35976d18 100644
--- a/drivers/media/platform/omap3isp/ispvideo.c
+++ b/drivers/media/platform/omap3isp/ispvideo.c
@@ -331,7 +331,7 @@ isp_video_check_format(struct isp_video *video, struct isp_video_fh *vfh)
331 331
332static int isp_video_queue_setup(struct vb2_queue *queue, 332static int isp_video_queue_setup(struct vb2_queue *queue,
333 unsigned int *count, unsigned int *num_planes, 333 unsigned int *count, unsigned int *num_planes,
334 unsigned int sizes[], void *alloc_ctxs[]) 334 unsigned int sizes[], struct device *alloc_devs[])
335{ 335{
336 struct isp_video_fh *vfh = vb2_get_drv_priv(queue); 336 struct isp_video_fh *vfh = vb2_get_drv_priv(queue);
337 struct isp_video *video = vfh->video; 337 struct isp_video *video = vfh->video;
@@ -342,8 +342,6 @@ static int isp_video_queue_setup(struct vb2_queue *queue,
342 if (sizes[0] == 0) 342 if (sizes[0] == 0)
343 return -EINVAL; 343 return -EINVAL;
344 344
345 alloc_ctxs[0] = video->alloc_ctx;
346
347 *count = min(*count, video->capture_mem / PAGE_ALIGN(sizes[0])); 345 *count = min(*count, video->capture_mem / PAGE_ALIGN(sizes[0]));
348 346
349 return 0; 347 return 0;
@@ -1308,6 +1306,7 @@ static int isp_video_open(struct file *file)
1308 queue->mem_ops = &vb2_dma_contig_memops; 1306 queue->mem_ops = &vb2_dma_contig_memops;
1309 queue->buf_struct_size = sizeof(struct isp_buffer); 1307 queue->buf_struct_size = sizeof(struct isp_buffer);
1310 queue->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC; 1308 queue->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1309 queue->dev = video->isp->dev;
1311 1310
1312 ret = vb2_queue_init(&handle->queue); 1311 ret = vb2_queue_init(&handle->queue);
1313 if (ret < 0) { 1312 if (ret < 0) {
@@ -1414,15 +1413,9 @@ int omap3isp_video_init(struct isp_video *video, const char *name)
1414 return -EINVAL; 1413 return -EINVAL;
1415 } 1414 }
1416 1415
1417 video->alloc_ctx = vb2_dma_contig_init_ctx(video->isp->dev);
1418 if (IS_ERR(video->alloc_ctx))
1419 return PTR_ERR(video->alloc_ctx);
1420
1421 ret = media_entity_pads_init(&video->video.entity, 1, &video->pad); 1416 ret = media_entity_pads_init(&video->video.entity, 1, &video->pad);
1422 if (ret < 0) { 1417 if (ret < 0)
1423 vb2_dma_contig_cleanup_ctx(video->alloc_ctx);
1424 return ret; 1418 return ret;
1425 }
1426 1419
1427 mutex_init(&video->mutex); 1420 mutex_init(&video->mutex);
1428 atomic_set(&video->active, 0); 1421 atomic_set(&video->active, 0);
@@ -1451,7 +1444,6 @@ int omap3isp_video_init(struct isp_video *video, const char *name)
1451 1444
1452void omap3isp_video_cleanup(struct isp_video *video) 1445void omap3isp_video_cleanup(struct isp_video *video)
1453{ 1446{
1454 vb2_dma_contig_cleanup_ctx(video->alloc_ctx);
1455 media_entity_cleanup(&video->video.entity); 1447 media_entity_cleanup(&video->video.entity);
1456 mutex_destroy(&video->queue_lock); 1448 mutex_destroy(&video->queue_lock);
1457 mutex_destroy(&video->stream_lock); 1449 mutex_destroy(&video->stream_lock);
diff --git a/drivers/media/platform/omap3isp/ispvideo.h b/drivers/media/platform/omap3isp/ispvideo.h
index 6a48d5879c56..f6a2082b4a0a 100644
--- a/drivers/media/platform/omap3isp/ispvideo.h
+++ b/drivers/media/platform/omap3isp/ispvideo.h
@@ -171,7 +171,6 @@ struct isp_video {
171 bool error; 171 bool error;
172 172
173 /* Video buffers queue */ 173 /* Video buffers queue */
174 void *alloc_ctx;
175 struct vb2_queue *queue; 174 struct vb2_queue *queue;
176 struct mutex queue_lock; /* protects the queue */ 175 struct mutex queue_lock; /* protects the queue */
177 spinlock_t irqlock; /* protects dmaqueue */ 176 spinlock_t irqlock; /* protects dmaqueue */
diff --git a/drivers/media/platform/rcar-fcp.c b/drivers/media/platform/rcar-fcp.c
new file mode 100644
index 000000000000..6a7bcc3028b1
--- /dev/null
+++ b/drivers/media/platform/rcar-fcp.c
@@ -0,0 +1,181 @@
1/*
2 * rcar-fcp.c -- R-Car Frame Compression Processor Driver
3 *
4 * Copyright (C) 2016 Renesas Electronics Corporation
5 *
6 * Contact: Laurent Pinchart (laurent.pinchart@ideasonboard.com)
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 */
13
14#include <linux/device.h>
15#include <linux/list.h>
16#include <linux/module.h>
17#include <linux/mutex.h>
18#include <linux/platform_device.h>
19#include <linux/pm_runtime.h>
20#include <linux/slab.h>
21
22#include <media/rcar-fcp.h>
23
24struct rcar_fcp_device {
25 struct list_head list;
26 struct device *dev;
27};
28
29static LIST_HEAD(fcp_devices);
30static DEFINE_MUTEX(fcp_lock);
31
32/* -----------------------------------------------------------------------------
33 * Public API
34 */
35
36/**
37 * rcar_fcp_get - Find and acquire a reference to an FCP instance
38 * @np: Device node of the FCP instance
39 *
40 * Search the list of registered FCP instances for the instance corresponding to
41 * the given device node.
42 *
43 * Return a pointer to the FCP instance, or an ERR_PTR if the instance can't be
44 * found.
45 */
46struct rcar_fcp_device *rcar_fcp_get(const struct device_node *np)
47{
48 struct rcar_fcp_device *fcp;
49
50 mutex_lock(&fcp_lock);
51
52 list_for_each_entry(fcp, &fcp_devices, list) {
53 if (fcp->dev->of_node != np)
54 continue;
55
56 /*
57 * Make sure the module won't be unloaded behind our back. This
58 * is a poor man's safety net, the module should really not be
59 * unloaded while FCP users can be active.
60 */
61 if (!try_module_get(fcp->dev->driver->owner))
62 fcp = NULL;
63
64 goto done;
65 }
66
67 fcp = ERR_PTR(-EPROBE_DEFER);
68
69done:
70 mutex_unlock(&fcp_lock);
71 return fcp;
72}
73EXPORT_SYMBOL_GPL(rcar_fcp_get);
74
75/**
76 * rcar_fcp_put - Release a reference to an FCP instance
77 * @fcp: The FCP instance
78 *
79 * Release the FCP instance acquired by a call to rcar_fcp_get().
80 */
81void rcar_fcp_put(struct rcar_fcp_device *fcp)
82{
83 if (fcp)
84 module_put(fcp->dev->driver->owner);
85}
86EXPORT_SYMBOL_GPL(rcar_fcp_put);
87
88/**
89 * rcar_fcp_enable - Enable an FCP
90 * @fcp: The FCP instance
91 *
92 * Before any memory access through an FCP is performed by a module, the FCP
93 * must be enabled by a call to this function. The enable calls are reference
94 * counted, each successful call must be followed by one rcar_fcp_disable()
95 * call when no more memory transfer can occur through the FCP.
96 *
97 * Return 0 on success or a negative error code if an error occurs. The enable
98 * reference count isn't increased when this function returns an error.
99 */
100int rcar_fcp_enable(struct rcar_fcp_device *fcp)
101{
102 if (!fcp)
103 return 0;
104
105 return pm_runtime_get_sync(fcp->dev);
106}
107EXPORT_SYMBOL_GPL(rcar_fcp_enable);
108
109/**
110 * rcar_fcp_disable - Disable an FCP
111 * @fcp: The FCP instance
112 *
113 * This function is the counterpart of rcar_fcp_enable(). As enable calls are
114 * reference counted a disable call may not disable the FCP synchronously.
115 */
116void rcar_fcp_disable(struct rcar_fcp_device *fcp)
117{
118 if (fcp)
119 pm_runtime_put(fcp->dev);
120}
121EXPORT_SYMBOL_GPL(rcar_fcp_disable);
122
123/* -----------------------------------------------------------------------------
124 * Platform Driver
125 */
126
127static int rcar_fcp_probe(struct platform_device *pdev)
128{
129 struct rcar_fcp_device *fcp;
130
131 fcp = devm_kzalloc(&pdev->dev, sizeof(*fcp), GFP_KERNEL);
132 if (fcp == NULL)
133 return -ENOMEM;
134
135 fcp->dev = &pdev->dev;
136
137 pm_runtime_enable(&pdev->dev);
138
139 mutex_lock(&fcp_lock);
140 list_add_tail(&fcp->list, &fcp_devices);
141 mutex_unlock(&fcp_lock);
142
143 platform_set_drvdata(pdev, fcp);
144
145 return 0;
146}
147
148static int rcar_fcp_remove(struct platform_device *pdev)
149{
150 struct rcar_fcp_device *fcp = platform_get_drvdata(pdev);
151
152 mutex_lock(&fcp_lock);
153 list_del(&fcp->list);
154 mutex_unlock(&fcp_lock);
155
156 pm_runtime_disable(&pdev->dev);
157
158 return 0;
159}
160
161static const struct of_device_id rcar_fcp_of_match[] = {
162 { .compatible = "renesas,fcpv" },
163 { },
164};
165
166static struct platform_driver rcar_fcp_platform_driver = {
167 .probe = rcar_fcp_probe,
168 .remove = rcar_fcp_remove,
169 .driver = {
170 .name = "rcar-fcp",
171 .of_match_table = rcar_fcp_of_match,
172 .suppress_bind_attrs = true,
173 },
174};
175
176module_platform_driver(rcar_fcp_platform_driver);
177
178MODULE_ALIAS("rcar-fcp");
179MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
180MODULE_DESCRIPTION("Renesas FCP Driver");
181MODULE_LICENSE("GPL");
diff --git a/drivers/media/platform/rcar-vin/Kconfig b/drivers/media/platform/rcar-vin/Kconfig
new file mode 100644
index 000000000000..b2ff2d4e8bb1
--- /dev/null
+++ b/drivers/media/platform/rcar-vin/Kconfig
@@ -0,0 +1,11 @@
1config VIDEO_RCAR_VIN
2 tristate "R-Car Video Input (VIN) Driver"
3 depends on VIDEO_V4L2 && VIDEO_V4L2_SUBDEV_API && OF && HAS_DMA
4 depends on ARCH_RENESAS || COMPILE_TEST
5 select VIDEOBUF2_DMA_CONTIG
6 ---help---
7 Support for Renesas R-Car Video Input (VIN) driver.
8 Supports R-Car Gen2 SoCs.
9
10 To compile this driver as a module, choose M here: the
11 module will be called rcar-vin.
diff --git a/drivers/media/platform/rcar-vin/Makefile b/drivers/media/platform/rcar-vin/Makefile
new file mode 100644
index 000000000000..48c5632c21dc
--- /dev/null
+++ b/drivers/media/platform/rcar-vin/Makefile
@@ -0,0 +1,3 @@
1rcar-vin-objs = rcar-core.o rcar-dma.o rcar-v4l2.o
2
3obj-$(CONFIG_VIDEO_RCAR_VIN) += rcar-vin.o
diff --git a/drivers/media/platform/rcar-vin/rcar-core.c b/drivers/media/platform/rcar-vin/rcar-core.c
new file mode 100644
index 000000000000..4b2007b73463
--- /dev/null
+++ b/drivers/media/platform/rcar-vin/rcar-core.c
@@ -0,0 +1,334 @@
1/*
2 * Driver for Renesas R-Car VIN
3 *
4 * Copyright (C) 2016 Renesas Electronics Corp.
5 * Copyright (C) 2011-2013 Renesas Solutions Corp.
6 * Copyright (C) 2013 Cogent Embedded, Inc., <source@cogentembedded.com>
7 * Copyright (C) 2008 Magnus Damm
8 *
9 * Based on the soc-camera rcar_vin driver
10 *
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the
13 * Free Software Foundation; either version 2 of the License, or (at your
14 * option) any later version.
15 */
16
17#include <linux/module.h>
18#include <linux/of.h>
19#include <linux/of_device.h>
20#include <linux/of_graph.h>
21#include <linux/platform_device.h>
22#include <linux/pm_runtime.h>
23
24#include <media/v4l2-of.h>
25
26#include "rcar-vin.h"
27
28/* -----------------------------------------------------------------------------
29 * Async notifier
30 */
31
32#define notifier_to_vin(n) container_of(n, struct rvin_dev, notifier)
33
34static int rvin_mbus_supported(struct rvin_dev *vin)
35{
36 struct v4l2_subdev *sd;
37 struct v4l2_subdev_mbus_code_enum code = {
38 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
39 };
40
41 sd = vin_to_source(vin);
42
43 code.index = 0;
44 while (!v4l2_subdev_call(sd, pad, enum_mbus_code, NULL, &code)) {
45 code.index++;
46 switch (code.code) {
47 case MEDIA_BUS_FMT_YUYV8_1X16:
48 case MEDIA_BUS_FMT_YUYV8_2X8:
49 case MEDIA_BUS_FMT_YUYV10_2X10:
50 case MEDIA_BUS_FMT_RGB888_1X24:
51 vin->source.code = code.code;
52 vin_dbg(vin, "Found supported media bus format: %d\n",
53 vin->source.code);
54 return true;
55 default:
56 break;
57 }
58 }
59
60 return false;
61}
62
63static int rvin_graph_notify_complete(struct v4l2_async_notifier *notifier)
64{
65 struct rvin_dev *vin = notifier_to_vin(notifier);
66 int ret;
67
68 ret = v4l2_device_register_subdev_nodes(&vin->v4l2_dev);
69 if (ret < 0) {
70 vin_err(vin, "Failed to register subdev nodes\n");
71 return ret;
72 }
73
74 if (!rvin_mbus_supported(vin)) {
75 vin_err(vin, "No supported mediabus format found\n");
76 return -EINVAL;
77 }
78
79 return rvin_v4l2_probe(vin);
80}
81
82static void rvin_graph_notify_unbind(struct v4l2_async_notifier *notifier,
83 struct v4l2_subdev *sd,
84 struct v4l2_async_subdev *asd)
85{
86 struct rvin_dev *vin = notifier_to_vin(notifier);
87
88 rvin_v4l2_remove(vin);
89}
90
91static int rvin_graph_notify_bound(struct v4l2_async_notifier *notifier,
92 struct v4l2_subdev *subdev,
93 struct v4l2_async_subdev *asd)
94{
95 struct rvin_dev *vin = notifier_to_vin(notifier);
96
97 vin_dbg(vin, "subdev %s bound\n", subdev->name);
98
99 vin->entity.entity = &subdev->entity;
100 vin->entity.subdev = subdev;
101
102 return 0;
103}
104
105static int rvin_graph_parse(struct rvin_dev *vin,
106 struct device_node *node)
107{
108 struct device_node *remote;
109 struct device_node *ep = NULL;
110 struct device_node *next;
111 int ret = 0;
112
113 while (1) {
114 next = of_graph_get_next_endpoint(node, ep);
115 if (!next)
116 break;
117
118 of_node_put(ep);
119 ep = next;
120
121 remote = of_graph_get_remote_port_parent(ep);
122 if (!remote) {
123 ret = -EINVAL;
124 break;
125 }
126
127 /* Skip entities that we have already processed. */
128 if (remote == vin->dev->of_node) {
129 of_node_put(remote);
130 continue;
131 }
132
133 /* Remote node to connect */
134 if (!vin->entity.node) {
135 vin->entity.node = remote;
136 vin->entity.asd.match_type = V4L2_ASYNC_MATCH_OF;
137 vin->entity.asd.match.of.node = remote;
138 ret++;
139 }
140 }
141
142 of_node_put(ep);
143
144 return ret;
145}
146
147static int rvin_graph_init(struct rvin_dev *vin)
148{
149 struct v4l2_async_subdev **subdevs = NULL;
150 int ret;
151
152 /* Parse the graph to extract a list of subdevice DT nodes. */
153 ret = rvin_graph_parse(vin, vin->dev->of_node);
154 if (ret < 0) {
155 vin_err(vin, "Graph parsing failed\n");
156 goto done;
157 }
158
159 if (!ret) {
160 vin_err(vin, "No subdev found in graph\n");
161 goto done;
162 }
163
164 if (ret != 1) {
165 vin_err(vin, "More then one subdev found in graph\n");
166 goto done;
167 }
168
169 /* Register the subdevices notifier. */
170 subdevs = devm_kzalloc(vin->dev, sizeof(*subdevs), GFP_KERNEL);
171 if (subdevs == NULL) {
172 ret = -ENOMEM;
173 goto done;
174 }
175
176 subdevs[0] = &vin->entity.asd;
177
178 vin->notifier.subdevs = subdevs;
179 vin->notifier.num_subdevs = 1;
180 vin->notifier.bound = rvin_graph_notify_bound;
181 vin->notifier.unbind = rvin_graph_notify_unbind;
182 vin->notifier.complete = rvin_graph_notify_complete;
183
184 ret = v4l2_async_notifier_register(&vin->v4l2_dev, &vin->notifier);
185 if (ret < 0) {
186 vin_err(vin, "Notifier registration failed\n");
187 goto done;
188 }
189
190 ret = 0;
191
192done:
193 if (ret < 0) {
194 v4l2_async_notifier_unregister(&vin->notifier);
195 of_node_put(vin->entity.node);
196 }
197
198 return ret;
199}
200
201/* -----------------------------------------------------------------------------
202 * Platform Device Driver
203 */
204
205static const struct of_device_id rvin_of_id_table[] = {
206 { .compatible = "renesas,vin-r8a7794", .data = (void *)RCAR_GEN2 },
207 { .compatible = "renesas,vin-r8a7793", .data = (void *)RCAR_GEN2 },
208 { .compatible = "renesas,vin-r8a7791", .data = (void *)RCAR_GEN2 },
209 { .compatible = "renesas,vin-r8a7790", .data = (void *)RCAR_GEN2 },
210 { .compatible = "renesas,vin-r8a7779", .data = (void *)RCAR_H1 },
211 { .compatible = "renesas,vin-r8a7778", .data = (void *)RCAR_M1 },
212 { },
213};
214MODULE_DEVICE_TABLE(of, rvin_of_id_table);
215
216static int rvin_parse_dt(struct rvin_dev *vin)
217{
218 const struct of_device_id *match;
219 struct v4l2_of_endpoint ep;
220 struct device_node *np;
221 int ret;
222
223 match = of_match_device(of_match_ptr(rvin_of_id_table), vin->dev);
224 if (!match)
225 return -ENODEV;
226
227 vin->chip = (enum chip_id)match->data;
228
229 np = of_graph_get_next_endpoint(vin->dev->of_node, NULL);
230 if (!np) {
231 vin_err(vin, "Could not find endpoint\n");
232 return -EINVAL;
233 }
234
235 ret = v4l2_of_parse_endpoint(np, &ep);
236 if (ret) {
237 vin_err(vin, "Could not parse endpoint\n");
238 return ret;
239 }
240
241 of_node_put(np);
242
243 vin->mbus_cfg.type = ep.bus_type;
244
245 switch (vin->mbus_cfg.type) {
246 case V4L2_MBUS_PARALLEL:
247 vin->mbus_cfg.flags = ep.bus.parallel.flags;
248 break;
249 case V4L2_MBUS_BT656:
250 vin->mbus_cfg.flags = 0;
251 break;
252 default:
253 vin_err(vin, "Unknown media bus type\n");
254 return -EINVAL;
255 }
256
257 return 0;
258}
259
260static int rcar_vin_probe(struct platform_device *pdev)
261{
262 struct rvin_dev *vin;
263 struct resource *mem;
264 int irq, ret;
265
266 vin = devm_kzalloc(&pdev->dev, sizeof(*vin), GFP_KERNEL);
267 if (!vin)
268 return -ENOMEM;
269
270 vin->dev = &pdev->dev;
271
272 ret = rvin_parse_dt(vin);
273 if (ret)
274 return ret;
275
276 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
277 if (mem == NULL)
278 return -EINVAL;
279
280 vin->base = devm_ioremap_resource(vin->dev, mem);
281 if (IS_ERR(vin->base))
282 return PTR_ERR(vin->base);
283
284 irq = platform_get_irq(pdev, 0);
285 if (irq <= 0)
286 return ret;
287
288 ret = rvin_dma_probe(vin, irq);
289 if (ret)
290 return ret;
291
292 ret = rvin_graph_init(vin);
293 if (ret < 0)
294 goto error;
295
296 pm_suspend_ignore_children(&pdev->dev, true);
297 pm_runtime_enable(&pdev->dev);
298
299 platform_set_drvdata(pdev, vin);
300
301 return 0;
302error:
303 rvin_dma_remove(vin);
304
305 return ret;
306}
307
308static int rcar_vin_remove(struct platform_device *pdev)
309{
310 struct rvin_dev *vin = platform_get_drvdata(pdev);
311
312 pm_runtime_disable(&pdev->dev);
313
314 v4l2_async_notifier_unregister(&vin->notifier);
315
316 rvin_dma_remove(vin);
317
318 return 0;
319}
320
321static struct platform_driver rcar_vin_driver = {
322 .driver = {
323 .name = "rcar-vin",
324 .of_match_table = rvin_of_id_table,
325 },
326 .probe = rcar_vin_probe,
327 .remove = rcar_vin_remove,
328};
329
330module_platform_driver(rcar_vin_driver);
331
332MODULE_AUTHOR("Niklas Söderlund <niklas.soderlund@ragnatech.se>");
333MODULE_DESCRIPTION("Renesas R-Car VIN camera host driver");
334MODULE_LICENSE("GPL v2");
diff --git a/drivers/media/platform/rcar-vin/rcar-dma.c b/drivers/media/platform/rcar-vin/rcar-dma.c
new file mode 100644
index 000000000000..496aa97b6400
--- /dev/null
+++ b/drivers/media/platform/rcar-vin/rcar-dma.c
@@ -0,0 +1,1187 @@
1/*
2 * Driver for Renesas R-Car VIN
3 *
4 * Copyright (C) 2016 Renesas Electronics Corp.
5 * Copyright (C) 2011-2013 Renesas Solutions Corp.
6 * Copyright (C) 2013 Cogent Embedded, Inc., <source@cogentembedded.com>
7 * Copyright (C) 2008 Magnus Damm
8 *
9 * Based on the soc-camera rcar_vin driver
10 *
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the
13 * Free Software Foundation; either version 2 of the License, or (at your
14 * option) any later version.
15 */
16
17#include <linux/delay.h>
18#include <linux/interrupt.h>
19
20#include <media/videobuf2-dma-contig.h>
21
22#include "rcar-vin.h"
23
24/* -----------------------------------------------------------------------------
25 * HW Functions
26 */
27
28/* Register offsets for R-Car VIN */
29#define VNMC_REG 0x00 /* Video n Main Control Register */
30#define VNMS_REG 0x04 /* Video n Module Status Register */
31#define VNFC_REG 0x08 /* Video n Frame Capture Register */
32#define VNSLPRC_REG 0x0C /* Video n Start Line Pre-Clip Register */
33#define VNELPRC_REG 0x10 /* Video n End Line Pre-Clip Register */
34#define VNSPPRC_REG 0x14 /* Video n Start Pixel Pre-Clip Register */
35#define VNEPPRC_REG 0x18 /* Video n End Pixel Pre-Clip Register */
36#define VNSLPOC_REG 0x1C /* Video n Start Line Post-Clip Register */
37#define VNELPOC_REG 0x20 /* Video n End Line Post-Clip Register */
38#define VNSPPOC_REG 0x24 /* Video n Start Pixel Post-Clip Register */
39#define VNEPPOC_REG 0x28 /* Video n End Pixel Post-Clip Register */
40#define VNIS_REG 0x2C /* Video n Image Stride Register */
41#define VNMB_REG(m) (0x30 + ((m) << 2)) /* Video n Memory Base m Register */
42#define VNIE_REG 0x40 /* Video n Interrupt Enable Register */
43#define VNINTS_REG 0x44 /* Video n Interrupt Status Register */
44#define VNSI_REG 0x48 /* Video n Scanline Interrupt Register */
45#define VNMTC_REG 0x4C /* Video n Memory Transfer Control Register */
46#define VNYS_REG 0x50 /* Video n Y Scale Register */
47#define VNXS_REG 0x54 /* Video n X Scale Register */
48#define VNDMR_REG 0x58 /* Video n Data Mode Register */
49#define VNDMR2_REG 0x5C /* Video n Data Mode Register 2 */
50#define VNUVAOF_REG 0x60 /* Video n UV Address Offset Register */
51#define VNC1A_REG 0x80 /* Video n Coefficient Set C1A Register */
52#define VNC1B_REG 0x84 /* Video n Coefficient Set C1B Register */
53#define VNC1C_REG 0x88 /* Video n Coefficient Set C1C Register */
54#define VNC2A_REG 0x90 /* Video n Coefficient Set C2A Register */
55#define VNC2B_REG 0x94 /* Video n Coefficient Set C2B Register */
56#define VNC2C_REG 0x98 /* Video n Coefficient Set C2C Register */
57#define VNC3A_REG 0xA0 /* Video n Coefficient Set C3A Register */
58#define VNC3B_REG 0xA4 /* Video n Coefficient Set C3B Register */
59#define VNC3C_REG 0xA8 /* Video n Coefficient Set C3C Register */
60#define VNC4A_REG 0xB0 /* Video n Coefficient Set C4A Register */
61#define VNC4B_REG 0xB4 /* Video n Coefficient Set C4B Register */
62#define VNC4C_REG 0xB8 /* Video n Coefficient Set C4C Register */
63#define VNC5A_REG 0xC0 /* Video n Coefficient Set C5A Register */
64#define VNC5B_REG 0xC4 /* Video n Coefficient Set C5B Register */
65#define VNC5C_REG 0xC8 /* Video n Coefficient Set C5C Register */
66#define VNC6A_REG 0xD0 /* Video n Coefficient Set C6A Register */
67#define VNC6B_REG 0xD4 /* Video n Coefficient Set C6B Register */
68#define VNC6C_REG 0xD8 /* Video n Coefficient Set C6C Register */
69#define VNC7A_REG 0xE0 /* Video n Coefficient Set C7A Register */
70#define VNC7B_REG 0xE4 /* Video n Coefficient Set C7B Register */
71#define VNC7C_REG 0xE8 /* Video n Coefficient Set C7C Register */
72#define VNC8A_REG 0xF0 /* Video n Coefficient Set C8A Register */
73#define VNC8B_REG 0xF4 /* Video n Coefficient Set C8B Register */
74#define VNC8C_REG 0xF8 /* Video n Coefficient Set C8C Register */
75
76
77/* Register bit fields for R-Car VIN */
78/* Video n Main Control Register bits */
79#define VNMC_FOC (1 << 21)
80#define VNMC_YCAL (1 << 19)
81#define VNMC_INF_YUV8_BT656 (0 << 16)
82#define VNMC_INF_YUV8_BT601 (1 << 16)
83#define VNMC_INF_YUV10_BT656 (2 << 16)
84#define VNMC_INF_YUV10_BT601 (3 << 16)
85#define VNMC_INF_YUV16 (5 << 16)
86#define VNMC_INF_RGB888 (6 << 16)
87#define VNMC_VUP (1 << 10)
88#define VNMC_IM_ODD (0 << 3)
89#define VNMC_IM_ODD_EVEN (1 << 3)
90#define VNMC_IM_EVEN (2 << 3)
91#define VNMC_IM_FULL (3 << 3)
92#define VNMC_BPS (1 << 1)
93#define VNMC_ME (1 << 0)
94
95/* Video n Module Status Register bits */
96#define VNMS_FBS_MASK (3 << 3)
97#define VNMS_FBS_SHIFT 3
98#define VNMS_AV (1 << 1)
99#define VNMS_CA (1 << 0)
100
101/* Video n Frame Capture Register bits */
102#define VNFC_C_FRAME (1 << 1)
103#define VNFC_S_FRAME (1 << 0)
104
105/* Video n Interrupt Enable Register bits */
106#define VNIE_FIE (1 << 4)
107#define VNIE_EFE (1 << 1)
108
109/* Video n Data Mode Register bits */
110#define VNDMR_EXRGB (1 << 8)
111#define VNDMR_BPSM (1 << 4)
112#define VNDMR_DTMD_YCSEP (1 << 1)
113#define VNDMR_DTMD_ARGB1555 (1 << 0)
114
115/* Video n Data Mode Register 2 bits */
116#define VNDMR2_VPS (1 << 30)
117#define VNDMR2_HPS (1 << 29)
118#define VNDMR2_FTEV (1 << 17)
119#define VNDMR2_VLV(n) ((n & 0xf) << 12)
120
121static void rvin_write(struct rvin_dev *vin, u32 value, u32 offset)
122{
123 iowrite32(value, vin->base + offset);
124}
125
126static u32 rvin_read(struct rvin_dev *vin, u32 offset)
127{
128 return ioread32(vin->base + offset);
129}
130
131static int rvin_setup(struct rvin_dev *vin)
132{
133 u32 vnmc, dmr, dmr2, interrupts;
134 bool progressive = false, output_is_yuv = false, input_is_yuv = false;
135
136 switch (vin->format.field) {
137 case V4L2_FIELD_TOP:
138 vnmc = VNMC_IM_ODD;
139 break;
140 case V4L2_FIELD_BOTTOM:
141 vnmc = VNMC_IM_EVEN;
142 break;
143 case V4L2_FIELD_INTERLACED:
144 case V4L2_FIELD_INTERLACED_TB:
145 vnmc = VNMC_IM_FULL;
146 break;
147 case V4L2_FIELD_INTERLACED_BT:
148 vnmc = VNMC_IM_FULL | VNMC_FOC;
149 break;
150 case V4L2_FIELD_NONE:
151 if (vin->continuous) {
152 vnmc = VNMC_IM_ODD_EVEN;
153 progressive = true;
154 } else {
155 vnmc = VNMC_IM_ODD;
156 }
157 break;
158 default:
159 vnmc = VNMC_IM_ODD;
160 break;
161 }
162
163 /*
164 * Input interface
165 */
166 switch (vin->source.code) {
167 case MEDIA_BUS_FMT_YUYV8_1X16:
168 /* BT.601/BT.1358 16bit YCbCr422 */
169 vnmc |= VNMC_INF_YUV16;
170 input_is_yuv = true;
171 break;
172 case MEDIA_BUS_FMT_YUYV8_2X8:
173 /* BT.656 8bit YCbCr422 or BT.601 8bit YCbCr422 */
174 vnmc |= vin->mbus_cfg.type == V4L2_MBUS_BT656 ?
175 VNMC_INF_YUV8_BT656 : VNMC_INF_YUV8_BT601;
176 input_is_yuv = true;
177 break;
178 case MEDIA_BUS_FMT_RGB888_1X24:
179 vnmc |= VNMC_INF_RGB888;
180 break;
181 case MEDIA_BUS_FMT_YUYV10_2X10:
182 /* BT.656 10bit YCbCr422 or BT.601 10bit YCbCr422 */
183 vnmc |= vin->mbus_cfg.type == V4L2_MBUS_BT656 ?
184 VNMC_INF_YUV10_BT656 : VNMC_INF_YUV10_BT601;
185 input_is_yuv = true;
186 break;
187 default:
188 break;
189 }
190
191 /* Enable VSYNC Field Toogle mode after one VSYNC input */
192 dmr2 = VNDMR2_FTEV | VNDMR2_VLV(1);
193
194 /* Hsync Signal Polarity Select */
195 if (!(vin->mbus_cfg.flags & V4L2_MBUS_HSYNC_ACTIVE_LOW))
196 dmr2 |= VNDMR2_HPS;
197
198 /* Vsync Signal Polarity Select */
199 if (!(vin->mbus_cfg.flags & V4L2_MBUS_VSYNC_ACTIVE_LOW))
200 dmr2 |= VNDMR2_VPS;
201
202 /*
203 * Output format
204 */
205 switch (vin->format.pixelformat) {
206 case V4L2_PIX_FMT_NV16:
207 rvin_write(vin,
208 ALIGN(vin->format.width * vin->format.height, 0x80),
209 VNUVAOF_REG);
210 dmr = VNDMR_DTMD_YCSEP;
211 output_is_yuv = true;
212 break;
213 case V4L2_PIX_FMT_YUYV:
214 dmr = VNDMR_BPSM;
215 output_is_yuv = true;
216 break;
217 case V4L2_PIX_FMT_UYVY:
218 dmr = 0;
219 output_is_yuv = true;
220 break;
221 case V4L2_PIX_FMT_XRGB555:
222 dmr = VNDMR_DTMD_ARGB1555;
223 break;
224 case V4L2_PIX_FMT_RGB565:
225 dmr = 0;
226 break;
227 case V4L2_PIX_FMT_XBGR32:
228 if (vin->chip == RCAR_GEN2 || vin->chip == RCAR_H1) {
229 dmr = VNDMR_EXRGB;
230 break;
231 }
232 /* fall through */
233 default:
234 vin_err(vin, "Invalid pixelformat (0x%x)\n",
235 vin->format.pixelformat);
236 return -EINVAL;
237 }
238
239 /* Always update on field change */
240 vnmc |= VNMC_VUP;
241
242 /* If input and output use the same colorspace, use bypass mode */
243 if (input_is_yuv == output_is_yuv)
244 vnmc |= VNMC_BPS;
245
246 /* Progressive or interlaced mode */
247 interrupts = progressive ? VNIE_FIE : VNIE_EFE;
248
249 /* Ack interrupts */
250 rvin_write(vin, interrupts, VNINTS_REG);
251 /* Enable interrupts */
252 rvin_write(vin, interrupts, VNIE_REG);
253 /* Start capturing */
254 rvin_write(vin, dmr, VNDMR_REG);
255 rvin_write(vin, dmr2, VNDMR2_REG);
256
257 /* Enable module */
258 rvin_write(vin, vnmc | VNMC_ME, VNMC_REG);
259
260 return 0;
261}
262
263static void rvin_capture_on(struct rvin_dev *vin)
264{
265 vin_dbg(vin, "Capture on in %s mode\n",
266 vin->continuous ? "continuous" : "single");
267
268 if (vin->continuous)
269 /* Continuous Frame Capture Mode */
270 rvin_write(vin, VNFC_C_FRAME, VNFC_REG);
271 else
272 /* Single Frame Capture Mode */
273 rvin_write(vin, VNFC_S_FRAME, VNFC_REG);
274}
275
276static void rvin_capture_off(struct rvin_dev *vin)
277{
278 /* Set continuous & single transfer off */
279 rvin_write(vin, 0, VNFC_REG);
280}
281
282static int rvin_capture_start(struct rvin_dev *vin)
283{
284 int ret;
285
286 rvin_crop_scale_comp(vin);
287
288 ret = rvin_setup(vin);
289 if (ret)
290 return ret;
291
292 rvin_capture_on(vin);
293
294 return 0;
295}
296
297static void rvin_capture_stop(struct rvin_dev *vin)
298{
299 rvin_capture_off(vin);
300
301 /* Disable module */
302 rvin_write(vin, rvin_read(vin, VNMC_REG) & ~VNMC_ME, VNMC_REG);
303}
304
305static void rvin_disable_interrupts(struct rvin_dev *vin)
306{
307 rvin_write(vin, 0, VNIE_REG);
308}
309
310static u32 rvin_get_interrupt_status(struct rvin_dev *vin)
311{
312 return rvin_read(vin, VNINTS_REG);
313}
314
315static void rvin_ack_interrupt(struct rvin_dev *vin)
316{
317 rvin_write(vin, rvin_read(vin, VNINTS_REG), VNINTS_REG);
318}
319
320static bool rvin_capture_active(struct rvin_dev *vin)
321{
322 return rvin_read(vin, VNMS_REG) & VNMS_CA;
323}
324
325static int rvin_get_active_slot(struct rvin_dev *vin)
326{
327 if (vin->continuous)
328 return (rvin_read(vin, VNMS_REG) & VNMS_FBS_MASK)
329 >> VNMS_FBS_SHIFT;
330
331 return 0;
332}
333
334static void rvin_set_slot_addr(struct rvin_dev *vin, int slot, dma_addr_t addr)
335{
336 const struct rvin_video_format *fmt;
337 int offsetx, offsety;
338 dma_addr_t offset;
339
340 fmt = rvin_format_from_pixel(vin->format.pixelformat);
341
342 /*
343 * There is no HW support for composition do the beast we can
344 * by modifying the buffer offset
345 */
346 offsetx = vin->compose.left * fmt->bpp;
347 offsety = vin->compose.top * vin->format.bytesperline;
348 offset = addr + offsetx + offsety;
349
350 /*
351 * The address needs to be 128 bytes aligned. Driver should never accept
352 * settings that do not satisfy this in the first place...
353 */
354 if (WARN_ON((offsetx | offsety | offset) & HW_BUFFER_MASK))
355 return;
356
357 rvin_write(vin, offset, VNMB_REG(slot));
358}
359
360/* -----------------------------------------------------------------------------
361 * Crop and Scaling Gen2
362 */
363
364struct vin_coeff {
365 unsigned short xs_value;
366 u32 coeff_set[24];
367};
368
369static const struct vin_coeff vin_coeff_set[] = {
370 { 0x0000, {
371 0x00000000, 0x00000000, 0x00000000,
372 0x00000000, 0x00000000, 0x00000000,
373 0x00000000, 0x00000000, 0x00000000,
374 0x00000000, 0x00000000, 0x00000000,
375 0x00000000, 0x00000000, 0x00000000,
376 0x00000000, 0x00000000, 0x00000000,
377 0x00000000, 0x00000000, 0x00000000,
378 0x00000000, 0x00000000, 0x00000000 },
379 },
380 { 0x1000, {
381 0x000fa400, 0x000fa400, 0x09625902,
382 0x000003f8, 0x00000403, 0x3de0d9f0,
383 0x001fffed, 0x00000804, 0x3cc1f9c3,
384 0x001003de, 0x00000c01, 0x3cb34d7f,
385 0x002003d2, 0x00000c00, 0x3d24a92d,
386 0x00200bca, 0x00000bff, 0x3df600d2,
387 0x002013cc, 0x000007ff, 0x3ed70c7e,
388 0x00100fde, 0x00000000, 0x3f87c036 },
389 },
390 { 0x1200, {
391 0x002ffff1, 0x002ffff1, 0x02a0a9c8,
392 0x002003e7, 0x001ffffa, 0x000185bc,
393 0x002007dc, 0x000003ff, 0x3e52859c,
394 0x00200bd4, 0x00000002, 0x3d53996b,
395 0x00100fd0, 0x00000403, 0x3d04ad2d,
396 0x00000bd5, 0x00000403, 0x3d35ace7,
397 0x3ff003e4, 0x00000801, 0x3dc674a1,
398 0x3fffe800, 0x00000800, 0x3e76f461 },
399 },
400 { 0x1400, {
401 0x00100be3, 0x00100be3, 0x04d1359a,
402 0x00000fdb, 0x002003ed, 0x0211fd93,
403 0x00000fd6, 0x002003f4, 0x0002d97b,
404 0x000007d6, 0x002ffffb, 0x3e93b956,
405 0x3ff003da, 0x001003ff, 0x3db49926,
406 0x3fffefe9, 0x00100001, 0x3d655cee,
407 0x3fffd400, 0x00000003, 0x3d65f4b6,
408 0x000fb421, 0x00000402, 0x3dc6547e },
409 },
410 { 0x1600, {
411 0x00000bdd, 0x00000bdd, 0x06519578,
412 0x3ff007da, 0x00000be3, 0x03c24973,
413 0x3ff003d9, 0x00000be9, 0x01b30d5f,
414 0x3ffff7df, 0x001003f1, 0x0003c542,
415 0x000fdfec, 0x001003f7, 0x3ec4711d,
416 0x000fc400, 0x002ffffd, 0x3df504f1,
417 0x001fa81a, 0x002ffc00, 0x3d957cc2,
418 0x002f8c3c, 0x00100000, 0x3db5c891 },
419 },
420 { 0x1800, {
421 0x3ff003dc, 0x3ff003dc, 0x0791e558,
422 0x000ff7dd, 0x3ff007de, 0x05328554,
423 0x000fe7e3, 0x3ff00be2, 0x03232546,
424 0x000fd7ee, 0x000007e9, 0x0143bd30,
425 0x001fb800, 0x000007ee, 0x00044511,
426 0x002fa015, 0x000007f4, 0x3ef4bcee,
427 0x002f8832, 0x001003f9, 0x3e4514c7,
428 0x001f7853, 0x001003fd, 0x3de54c9f },
429 },
430 { 0x1a00, {
431 0x000fefe0, 0x000fefe0, 0x08721d3c,
432 0x001fdbe7, 0x000ffbde, 0x0652a139,
433 0x001fcbf0, 0x000003df, 0x0463292e,
434 0x002fb3ff, 0x3ff007e3, 0x0293a91d,
435 0x002f9c12, 0x3ff00be7, 0x01241905,
436 0x001f8c29, 0x000007ed, 0x3fe470eb,
437 0x000f7c46, 0x000007f2, 0x3f04b8ca,
438 0x3fef7865, 0x000007f6, 0x3e74e4a8 },
439 },
440 { 0x1c00, {
441 0x001fd3e9, 0x001fd3e9, 0x08f23d26,
442 0x002fbff3, 0x001fe3e4, 0x0712ad23,
443 0x002fa800, 0x000ff3e0, 0x05631d1b,
444 0x001f9810, 0x000ffbe1, 0x03b3890d,
445 0x000f8c23, 0x000003e3, 0x0233e8fa,
446 0x3fef843b, 0x000003e7, 0x00f430e4,
447 0x3fbf8456, 0x3ff00bea, 0x00046cc8,
448 0x3f8f8c72, 0x3ff00bef, 0x3f3490ac },
449 },
450 { 0x1e00, {
451 0x001fbbf4, 0x001fbbf4, 0x09425112,
452 0x001fa800, 0x002fc7ed, 0x0792b110,
453 0x000f980e, 0x001fdbe6, 0x0613110a,
454 0x3fff8c20, 0x001fe7e3, 0x04a368fd,
455 0x3fcf8c33, 0x000ff7e2, 0x0343b8ed,
456 0x3f9f8c4a, 0x000fffe3, 0x0203f8da,
457 0x3f5f9c61, 0x000003e6, 0x00e428c5,
458 0x3f1fb07b, 0x000003eb, 0x3fe440af },
459 },
460 { 0x2000, {
461 0x000fa400, 0x000fa400, 0x09625902,
462 0x3fff980c, 0x001fb7f5, 0x0812b0ff,
463 0x3fdf901c, 0x001fc7ed, 0x06b2fcfa,
464 0x3faf902d, 0x001fd3e8, 0x055348f1,
465 0x3f7f983f, 0x001fe3e5, 0x04038ce3,
466 0x3f3fa454, 0x001fefe3, 0x02e3c8d1,
467 0x3f0fb86a, 0x001ff7e4, 0x01c3e8c0,
468 0x3ecfd880, 0x000fffe6, 0x00c404ac },
469 },
470 { 0x2200, {
471 0x3fdf9c0b, 0x3fdf9c0b, 0x09725cf4,
472 0x3fbf9818, 0x3fffa400, 0x0842a8f1,
473 0x3f8f9827, 0x000fb3f7, 0x0702f0ec,
474 0x3f5fa037, 0x000fc3ef, 0x05d330e4,
475 0x3f2fac49, 0x001fcfea, 0x04a364d9,
476 0x3effc05c, 0x001fdbe7, 0x038394ca,
477 0x3ecfdc6f, 0x001fe7e6, 0x0273b0bb,
478 0x3ea00083, 0x001fefe6, 0x0183c0a9 },
479 },
480 { 0x2400, {
481 0x3f9fa014, 0x3f9fa014, 0x098260e6,
482 0x3f7f9c23, 0x3fcf9c0a, 0x08629ce5,
483 0x3f4fa431, 0x3fefa400, 0x0742d8e1,
484 0x3f1fb440, 0x3fffb3f8, 0x062310d9,
485 0x3eefc850, 0x000fbbf2, 0x050340d0,
486 0x3ecfe062, 0x000fcbec, 0x041364c2,
487 0x3ea00073, 0x001fd3ea, 0x03037cb5,
488 0x3e902086, 0x001fdfe8, 0x022388a5 },
489 },
490 { 0x2600, {
491 0x3f5fa81e, 0x3f5fa81e, 0x096258da,
492 0x3f3fac2b, 0x3f8fa412, 0x088290d8,
493 0x3f0fbc38, 0x3fafa408, 0x0772c8d5,
494 0x3eefcc47, 0x3fcfa800, 0x0672f4ce,
495 0x3ecfe456, 0x3fefaffa, 0x05531cc6,
496 0x3eb00066, 0x3fffbbf3, 0x047334bb,
497 0x3ea01c77, 0x000fc7ee, 0x039348ae,
498 0x3ea04486, 0x000fd3eb, 0x02b350a1 },
499 },
500 { 0x2800, {
501 0x3f2fb426, 0x3f2fb426, 0x094250ce,
502 0x3f0fc032, 0x3f4fac1b, 0x086284cd,
503 0x3eefd040, 0x3f7fa811, 0x0782acc9,
504 0x3ecfe84c, 0x3f9fa807, 0x06a2d8c4,
505 0x3eb0005b, 0x3fbfac00, 0x05b2f4bc,
506 0x3eb0186a, 0x3fdfb3fa, 0x04c308b4,
507 0x3eb04077, 0x3fefbbf4, 0x03f31ca8,
508 0x3ec06884, 0x000fbff2, 0x03031c9e },
509 },
510 { 0x2a00, {
511 0x3f0fc42d, 0x3f0fc42d, 0x090240c4,
512 0x3eefd439, 0x3f2fb822, 0x08526cc2,
513 0x3edfe845, 0x3f4fb018, 0x078294bf,
514 0x3ec00051, 0x3f6fac0f, 0x06b2b4bb,
515 0x3ec0185f, 0x3f8fac07, 0x05e2ccb4,
516 0x3ec0386b, 0x3fafac00, 0x0502e8ac,
517 0x3ed05c77, 0x3fcfb3fb, 0x0432f0a3,
518 0x3ef08482, 0x3fdfbbf6, 0x0372f898 },
519 },
520 { 0x2c00, {
521 0x3eefdc31, 0x3eefdc31, 0x08e238b8,
522 0x3edfec3d, 0x3f0fc828, 0x082258b9,
523 0x3ed00049, 0x3f1fc01e, 0x077278b6,
524 0x3ed01455, 0x3f3fb815, 0x06c294b2,
525 0x3ed03460, 0x3f5fb40d, 0x0602acac,
526 0x3ef0506c, 0x3f7fb006, 0x0542c0a4,
527 0x3f107476, 0x3f9fb400, 0x0472c89d,
528 0x3f309c80, 0x3fbfb7fc, 0x03b2cc94 },
529 },
530 { 0x2e00, {
531 0x3eefec37, 0x3eefec37, 0x088220b0,
532 0x3ee00041, 0x3effdc2d, 0x07f244ae,
533 0x3ee0144c, 0x3f0fd023, 0x07625cad,
534 0x3ef02c57, 0x3f1fc81a, 0x06c274a9,
535 0x3f004861, 0x3f3fbc13, 0x060288a6,
536 0x3f20686b, 0x3f5fb80c, 0x05529c9e,
537 0x3f408c74, 0x3f6fb805, 0x04b2ac96,
538 0x3f80ac7e, 0x3f8fb800, 0x0402ac8e },
539 },
540 { 0x3000, {
541 0x3ef0003a, 0x3ef0003a, 0x084210a6,
542 0x3ef01045, 0x3effec32, 0x07b228a7,
543 0x3f00284e, 0x3f0fdc29, 0x073244a4,
544 0x3f104058, 0x3f0fd420, 0x06a258a2,
545 0x3f305c62, 0x3f2fc818, 0x0612689d,
546 0x3f508069, 0x3f3fc011, 0x05728496,
547 0x3f80a072, 0x3f4fc00a, 0x04d28c90,
548 0x3fc0c07b, 0x3f6fbc04, 0x04429088 },
549 },
550 { 0x3200, {
551 0x3f00103e, 0x3f00103e, 0x07f1fc9e,
552 0x3f102447, 0x3f000035, 0x0782149d,
553 0x3f203c4f, 0x3f0ff02c, 0x07122c9c,
554 0x3f405458, 0x3f0fe424, 0x06924099,
555 0x3f607061, 0x3f1fd41d, 0x06024c97,
556 0x3f909068, 0x3f2fcc16, 0x05726490,
557 0x3fc0b070, 0x3f3fc80f, 0x04f26c8a,
558 0x0000d077, 0x3f4fc409, 0x04627484 },
559 },
560 { 0x3400, {
561 0x3f202040, 0x3f202040, 0x07a1e898,
562 0x3f303449, 0x3f100c38, 0x0741fc98,
563 0x3f504c50, 0x3f10002f, 0x06e21495,
564 0x3f706459, 0x3f1ff028, 0x06722492,
565 0x3fa08060, 0x3f1fe421, 0x05f2348f,
566 0x3fd09c67, 0x3f1fdc19, 0x05824c89,
567 0x0000bc6e, 0x3f2fd014, 0x04f25086,
568 0x0040dc74, 0x3f3fcc0d, 0x04825c7f },
569 },
570 { 0x3600, {
571 0x3f403042, 0x3f403042, 0x0761d890,
572 0x3f504848, 0x3f301c3b, 0x0701f090,
573 0x3f805c50, 0x3f200c33, 0x06a2008f,
574 0x3fa07458, 0x3f10002b, 0x06520c8d,
575 0x3fd0905e, 0x3f1ff424, 0x05e22089,
576 0x0000ac65, 0x3f1fe81d, 0x05823483,
577 0x0030cc6a, 0x3f2fdc18, 0x04f23c81,
578 0x0080e871, 0x3f2fd412, 0x0482407c },
579 },
580 { 0x3800, {
581 0x3f604043, 0x3f604043, 0x0721c88a,
582 0x3f80544a, 0x3f502c3c, 0x06d1d88a,
583 0x3fb06851, 0x3f301c35, 0x0681e889,
584 0x3fd08456, 0x3f30082f, 0x0611fc88,
585 0x00009c5d, 0x3f200027, 0x05d20884,
586 0x0030b863, 0x3f2ff421, 0x05621880,
587 0x0070d468, 0x3f2fe81b, 0x0502247c,
588 0x00c0ec6f, 0x3f2fe015, 0x04a22877 },
589 },
590 { 0x3a00, {
591 0x3f904c44, 0x3f904c44, 0x06e1b884,
592 0x3fb0604a, 0x3f70383e, 0x0691c885,
593 0x3fe07451, 0x3f502c36, 0x0661d483,
594 0x00009055, 0x3f401831, 0x0601ec81,
595 0x0030a85b, 0x3f300c2a, 0x05b1f480,
596 0x0070c061, 0x3f300024, 0x0562047a,
597 0x00b0d867, 0x3f3ff41e, 0x05020c77,
598 0x00f0f46b, 0x3f2fec19, 0x04a21474 },
599 },
600 { 0x3c00, {
601 0x3fb05c43, 0x3fb05c43, 0x06c1b07e,
602 0x3fe06c4b, 0x3f902c3f, 0x0681c081,
603 0x0000844f, 0x3f703838, 0x0631cc7d,
604 0x00309855, 0x3f602433, 0x05d1d47e,
605 0x0060b459, 0x3f50142e, 0x0581e47b,
606 0x00a0c85f, 0x3f400828, 0x0531f078,
607 0x00e0e064, 0x3f300021, 0x0501fc73,
608 0x00b0fc6a, 0x3f3ff41d, 0x04a20873 },
609 },
610 { 0x3e00, {
611 0x3fe06444, 0x3fe06444, 0x0681a07a,
612 0x00007849, 0x3fc0503f, 0x0641b07a,
613 0x0020904d, 0x3fa0403a, 0x05f1c07a,
614 0x0060a453, 0x3f803034, 0x05c1c878,
615 0x0090b858, 0x3f70202f, 0x0571d477,
616 0x00d0d05d, 0x3f501829, 0x0531e073,
617 0x0110e462, 0x3f500825, 0x04e1e471,
618 0x01510065, 0x3f40001f, 0x04a1f06d },
619 },
620 { 0x4000, {
621 0x00007044, 0x00007044, 0x06519476,
622 0x00208448, 0x3fe05c3f, 0x0621a476,
623 0x0050984d, 0x3fc04c3a, 0x05e1b075,
624 0x0080ac52, 0x3fa03c35, 0x05a1b875,
625 0x00c0c056, 0x3f803030, 0x0561c473,
626 0x0100d45b, 0x3f70202b, 0x0521d46f,
627 0x0140e860, 0x3f601427, 0x04d1d46e,
628 0x01810064, 0x3f500822, 0x0491dc6b },
629 },
630 { 0x5000, {
631 0x0110a442, 0x0110a442, 0x0551545e,
632 0x0140b045, 0x00e0983f, 0x0531585f,
633 0x0160c047, 0x00c08c3c, 0x0511645e,
634 0x0190cc4a, 0x00908039, 0x04f1685f,
635 0x01c0dc4c, 0x00707436, 0x04d1705e,
636 0x0200e850, 0x00506833, 0x04b1785b,
637 0x0230f453, 0x00305c30, 0x0491805a,
638 0x02710056, 0x0010542d, 0x04718059 },
639 },
640 { 0x6000, {
641 0x01c0bc40, 0x01c0bc40, 0x04c13052,
642 0x01e0c841, 0x01a0b43d, 0x04c13851,
643 0x0210cc44, 0x0180a83c, 0x04a13453,
644 0x0230d845, 0x0160a03a, 0x04913c52,
645 0x0260e047, 0x01409838, 0x04714052,
646 0x0280ec49, 0x01208c37, 0x04514c50,
647 0x02b0f44b, 0x01008435, 0x04414c50,
648 0x02d1004c, 0x00e07c33, 0x0431544f },
649 },
650 { 0x7000, {
651 0x0230c83e, 0x0230c83e, 0x04711c4c,
652 0x0250d03f, 0x0210c43c, 0x0471204b,
653 0x0270d840, 0x0200b83c, 0x0451244b,
654 0x0290dc42, 0x01e0b43a, 0x0441244c,
655 0x02b0e443, 0x01c0b038, 0x0441284b,
656 0x02d0ec44, 0x01b0a438, 0x0421304a,
657 0x02f0f445, 0x0190a036, 0x04213449,
658 0x0310f847, 0x01709c34, 0x04213848 },
659 },
660 { 0x8000, {
661 0x0280d03d, 0x0280d03d, 0x04310c48,
662 0x02a0d43e, 0x0270c83c, 0x04311047,
663 0x02b0dc3e, 0x0250c83a, 0x04311447,
664 0x02d0e040, 0x0240c03a, 0x04211446,
665 0x02e0e840, 0x0220bc39, 0x04111847,
666 0x0300e842, 0x0210b438, 0x04012445,
667 0x0310f043, 0x0200b037, 0x04012045,
668 0x0330f444, 0x01e0ac36, 0x03f12445 },
669 },
670 { 0xefff, {
671 0x0340dc3a, 0x0340dc3a, 0x03b0ec40,
672 0x0340e03a, 0x0330e039, 0x03c0f03e,
673 0x0350e03b, 0x0330dc39, 0x03c0ec3e,
674 0x0350e43a, 0x0320dc38, 0x03c0f43e,
675 0x0360e43b, 0x0320d839, 0x03b0f03e,
676 0x0360e83b, 0x0310d838, 0x03c0fc3b,
677 0x0370e83b, 0x0310d439, 0x03a0f83d,
678 0x0370e83c, 0x0300d438, 0x03b0fc3c },
679 }
680};
681
682static void rvin_set_coeff(struct rvin_dev *vin, unsigned short xs)
683{
684 int i;
685 const struct vin_coeff *p_prev_set = NULL;
686 const struct vin_coeff *p_set = NULL;
687
688 /* Look for suitable coefficient values */
689 for (i = 0; i < ARRAY_SIZE(vin_coeff_set); i++) {
690 p_prev_set = p_set;
691 p_set = &vin_coeff_set[i];
692
693 if (xs < p_set->xs_value)
694 break;
695 }
696
697 /* Use previous value if its XS value is closer */
698 if (p_prev_set && p_set &&
699 xs - p_prev_set->xs_value < p_set->xs_value - xs)
700 p_set = p_prev_set;
701
702 /* Set coefficient registers */
703 rvin_write(vin, p_set->coeff_set[0], VNC1A_REG);
704 rvin_write(vin, p_set->coeff_set[1], VNC1B_REG);
705 rvin_write(vin, p_set->coeff_set[2], VNC1C_REG);
706
707 rvin_write(vin, p_set->coeff_set[3], VNC2A_REG);
708 rvin_write(vin, p_set->coeff_set[4], VNC2B_REG);
709 rvin_write(vin, p_set->coeff_set[5], VNC2C_REG);
710
711 rvin_write(vin, p_set->coeff_set[6], VNC3A_REG);
712 rvin_write(vin, p_set->coeff_set[7], VNC3B_REG);
713 rvin_write(vin, p_set->coeff_set[8], VNC3C_REG);
714
715 rvin_write(vin, p_set->coeff_set[9], VNC4A_REG);
716 rvin_write(vin, p_set->coeff_set[10], VNC4B_REG);
717 rvin_write(vin, p_set->coeff_set[11], VNC4C_REG);
718
719 rvin_write(vin, p_set->coeff_set[12], VNC5A_REG);
720 rvin_write(vin, p_set->coeff_set[13], VNC5B_REG);
721 rvin_write(vin, p_set->coeff_set[14], VNC5C_REG);
722
723 rvin_write(vin, p_set->coeff_set[15], VNC6A_REG);
724 rvin_write(vin, p_set->coeff_set[16], VNC6B_REG);
725 rvin_write(vin, p_set->coeff_set[17], VNC6C_REG);
726
727 rvin_write(vin, p_set->coeff_set[18], VNC7A_REG);
728 rvin_write(vin, p_set->coeff_set[19], VNC7B_REG);
729 rvin_write(vin, p_set->coeff_set[20], VNC7C_REG);
730
731 rvin_write(vin, p_set->coeff_set[21], VNC8A_REG);
732 rvin_write(vin, p_set->coeff_set[22], VNC8B_REG);
733 rvin_write(vin, p_set->coeff_set[23], VNC8C_REG);
734}
735
736void rvin_crop_scale_comp(struct rvin_dev *vin)
737{
738 u32 xs, ys;
739
740 /* Set Start/End Pixel/Line Pre-Clip */
741 rvin_write(vin, vin->crop.left, VNSPPRC_REG);
742 rvin_write(vin, vin->crop.left + vin->crop.width - 1, VNEPPRC_REG);
743 switch (vin->format.field) {
744 case V4L2_FIELD_INTERLACED:
745 case V4L2_FIELD_INTERLACED_TB:
746 case V4L2_FIELD_INTERLACED_BT:
747 rvin_write(vin, vin->crop.top / 2, VNSLPRC_REG);
748 rvin_write(vin, (vin->crop.top + vin->crop.height) / 2 - 1,
749 VNELPRC_REG);
750 break;
751 default:
752 rvin_write(vin, vin->crop.top, VNSLPRC_REG);
753 rvin_write(vin, vin->crop.top + vin->crop.height - 1,
754 VNELPRC_REG);
755 break;
756 }
757
758 /* Set scaling coefficient */
759 ys = 0;
760 if (vin->crop.height != vin->compose.height)
761 ys = (4096 * vin->crop.height) / vin->compose.height;
762 rvin_write(vin, ys, VNYS_REG);
763
764 xs = 0;
765 if (vin->crop.width != vin->compose.width)
766 xs = (4096 * vin->crop.width) / vin->compose.width;
767
768 /* Horizontal upscaling is up to double size */
769 if (xs > 0 && xs < 2048)
770 xs = 2048;
771
772 rvin_write(vin, xs, VNXS_REG);
773
774 /* Horizontal upscaling is done out by scaling down from double size */
775 if (xs < 4096)
776 xs *= 2;
777
778 rvin_set_coeff(vin, xs);
779
780 /* Set Start/End Pixel/Line Post-Clip */
781 rvin_write(vin, 0, VNSPPOC_REG);
782 rvin_write(vin, 0, VNSLPOC_REG);
783 rvin_write(vin, vin->format.width - 1, VNEPPOC_REG);
784 switch (vin->format.field) {
785 case V4L2_FIELD_INTERLACED:
786 case V4L2_FIELD_INTERLACED_TB:
787 case V4L2_FIELD_INTERLACED_BT:
788 rvin_write(vin, vin->format.height / 2 - 1, VNELPOC_REG);
789 break;
790 default:
791 rvin_write(vin, vin->format.height - 1, VNELPOC_REG);
792 break;
793 }
794
795 if (vin->format.pixelformat == V4L2_PIX_FMT_NV16)
796 rvin_write(vin, ALIGN(vin->format.width, 0x20), VNIS_REG);
797 else
798 rvin_write(vin, ALIGN(vin->format.width, 0x10), VNIS_REG);
799
800 vin_dbg(vin,
801 "Pre-Clip: %ux%u@%u:%u YS: %d XS: %d Post-Clip: %ux%u@%u:%u\n",
802 vin->crop.width, vin->crop.height, vin->crop.left,
803 vin->crop.top, ys, xs, vin->format.width, vin->format.height,
804 0, 0);
805}
806
807void rvin_scale_try(struct rvin_dev *vin, struct v4l2_pix_format *pix,
808 u32 width, u32 height)
809{
810 /* All VIN channels on Gen2 have scalers */
811 pix->width = width;
812 pix->height = height;
813}
814
815/* -----------------------------------------------------------------------------
816 * DMA Functions
817 */
818
819#define RVIN_TIMEOUT_MS 100
820#define RVIN_RETRIES 10
821
822struct rvin_buffer {
823 struct vb2_v4l2_buffer vb;
824 struct list_head list;
825};
826
827#define to_buf_list(vb2_buffer) (&container_of(vb2_buffer, \
828 struct rvin_buffer, \
829 vb)->list)
830
831/* Moves a buffer from the queue to the HW slots */
832static bool rvin_fill_hw_slot(struct rvin_dev *vin, int slot)
833{
834 struct rvin_buffer *buf;
835 struct vb2_v4l2_buffer *vbuf;
836 dma_addr_t phys_addr_top;
837
838 if (vin->queue_buf[slot] != NULL)
839 return true;
840
841 if (list_empty(&vin->buf_list))
842 return false;
843
844 vin_dbg(vin, "Filling HW slot: %d\n", slot);
845
846 /* Keep track of buffer we give to HW */
847 buf = list_entry(vin->buf_list.next, struct rvin_buffer, list);
848 vbuf = &buf->vb;
849 list_del_init(to_buf_list(vbuf));
850 vin->queue_buf[slot] = vbuf;
851
852 /* Setup DMA */
853 phys_addr_top = vb2_dma_contig_plane_dma_addr(&vbuf->vb2_buf, 0);
854 rvin_set_slot_addr(vin, slot, phys_addr_top);
855
856 return true;
857}
858
859static bool rvin_fill_hw(struct rvin_dev *vin)
860{
861 int slot, limit;
862
863 limit = vin->continuous ? HW_BUFFER_NUM : 1;
864
865 for (slot = 0; slot < limit; slot++)
866 if (!rvin_fill_hw_slot(vin, slot))
867 return false;
868 return true;
869}
870
871static irqreturn_t rvin_irq(int irq, void *data)
872{
873 struct rvin_dev *vin = data;
874 u32 int_status;
875 int slot;
876 unsigned int sequence, handled = 0;
877 unsigned long flags;
878
879 spin_lock_irqsave(&vin->qlock, flags);
880
881 int_status = rvin_get_interrupt_status(vin);
882 if (!int_status)
883 goto done;
884
885 rvin_ack_interrupt(vin);
886 handled = 1;
887
888 /* Nothing to do if capture status is 'STOPPED' */
889 if (vin->state == STOPPED) {
890 vin_dbg(vin, "IRQ while state stopped\n");
891 goto done;
892 }
893
894 /* Nothing to do if capture status is 'STOPPING' */
895 if (vin->state == STOPPING) {
896 vin_dbg(vin, "IRQ while state stopping\n");
897 goto done;
898 }
899
900 /* Prepare for capture and update state */
901 slot = rvin_get_active_slot(vin);
902 sequence = vin->sequence++;
903
904 vin_dbg(vin, "IRQ %02d: %d\tbuf0: %c buf1: %c buf2: %c\tmore: %d\n",
905 sequence, slot,
906 slot == 0 ? 'x' : vin->queue_buf[0] != NULL ? '1' : '0',
907 slot == 1 ? 'x' : vin->queue_buf[1] != NULL ? '1' : '0',
908 slot == 2 ? 'x' : vin->queue_buf[2] != NULL ? '1' : '0',
909 !list_empty(&vin->buf_list));
910
911 /* HW have written to a slot that is not prepared we are in trouble */
912 if (WARN_ON((vin->queue_buf[slot] == NULL)))
913 goto done;
914
915 /* Capture frame */
916 vin->queue_buf[slot]->field = vin->format.field;
917 vin->queue_buf[slot]->sequence = sequence;
918 vin->queue_buf[slot]->vb2_buf.timestamp = ktime_get_ns();
919 vb2_buffer_done(&vin->queue_buf[slot]->vb2_buf, VB2_BUF_STATE_DONE);
920 vin->queue_buf[slot] = NULL;
921
922 /* Prepare for next frame */
923 if (!rvin_fill_hw(vin)) {
924
925 /*
926 * Can't supply HW with new buffers fast enough. Halt
927 * capture until more buffers are available.
928 */
929 vin->state = STALLED;
930
931 /*
932 * The continuous capturing requires an explicit stop
933 * operation when there is no buffer to be set into
934 * the VnMBm registers.
935 */
936 if (vin->continuous) {
937 rvin_capture_off(vin);
938 vin_dbg(vin, "IRQ %02d: hw not ready stop\n", sequence);
939 }
940 } else {
941 /*
942 * The single capturing requires an explicit capture
943 * operation to fetch the next frame.
944 */
945 if (!vin->continuous)
946 rvin_capture_on(vin);
947 }
948done:
949 spin_unlock_irqrestore(&vin->qlock, flags);
950
951 return IRQ_RETVAL(handled);
952}
953
954/* Need to hold qlock before calling */
955static void return_all_buffers(struct rvin_dev *vin,
956 enum vb2_buffer_state state)
957{
958 struct rvin_buffer *buf, *node;
959 int i;
960
961 for (i = 0; i < HW_BUFFER_NUM; i++) {
962 if (vin->queue_buf[i]) {
963 vb2_buffer_done(&vin->queue_buf[i]->vb2_buf,
964 state);
965 vin->queue_buf[i] = NULL;
966 }
967 }
968
969 list_for_each_entry_safe(buf, node, &vin->buf_list, list) {
970 vb2_buffer_done(&buf->vb.vb2_buf, state);
971 list_del(&buf->list);
972 }
973}
974
975static int rvin_queue_setup(struct vb2_queue *vq, unsigned int *nbuffers,
976 unsigned int *nplanes, unsigned int sizes[],
977 struct device *alloc_devs[])
978
979{
980 struct rvin_dev *vin = vb2_get_drv_priv(vq);
981
982 /* Make sure the image size is large enough. */
983 if (*nplanes)
984 return sizes[0] < vin->format.sizeimage ? -EINVAL : 0;
985
986 *nplanes = 1;
987 sizes[0] = vin->format.sizeimage;
988
989 return 0;
990};
991
992static int rvin_buffer_prepare(struct vb2_buffer *vb)
993{
994 struct rvin_dev *vin = vb2_get_drv_priv(vb->vb2_queue);
995 unsigned long size = vin->format.sizeimage;
996
997 if (vb2_plane_size(vb, 0) < size) {
998 vin_err(vin, "buffer too small (%lu < %lu)\n",
999 vb2_plane_size(vb, 0), size);
1000 return -EINVAL;
1001 }
1002
1003 vb2_set_plane_payload(vb, 0, size);
1004
1005 return 0;
1006}
1007
1008static void rvin_buffer_queue(struct vb2_buffer *vb)
1009{
1010 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1011 struct rvin_dev *vin = vb2_get_drv_priv(vb->vb2_queue);
1012 unsigned long flags;
1013
1014 spin_lock_irqsave(&vin->qlock, flags);
1015
1016 list_add_tail(to_buf_list(vbuf), &vin->buf_list);
1017
1018 /*
1019 * If capture is stalled add buffer to HW and restart
1020 * capturing if HW is ready to continue.
1021 */
1022 if (vin->state == STALLED)
1023 if (rvin_fill_hw(vin))
1024 rvin_capture_on(vin);
1025
1026 spin_unlock_irqrestore(&vin->qlock, flags);
1027}
1028
1029static int rvin_start_streaming(struct vb2_queue *vq, unsigned int count)
1030{
1031 struct rvin_dev *vin = vb2_get_drv_priv(vq);
1032 struct v4l2_subdev *sd;
1033 unsigned long flags;
1034 int ret;
1035
1036 sd = vin_to_source(vin);
1037 v4l2_subdev_call(sd, video, s_stream, 1);
1038
1039 spin_lock_irqsave(&vin->qlock, flags);
1040
1041 vin->state = RUNNING;
1042 vin->sequence = 0;
1043
1044 /* Continuous capture requires more buffers then there are HW slots */
1045 vin->continuous = count > HW_BUFFER_NUM;
1046
1047 /*
1048 * This should never happen but if we don't have enough
1049 * buffers for HW bail out
1050 */
1051 if (!rvin_fill_hw(vin)) {
1052 vin_err(vin, "HW not ready to start, not enough buffers available\n");
1053 ret = -EINVAL;
1054 goto out;
1055 }
1056
1057 ret = rvin_capture_start(vin);
1058out:
1059 /* Return all buffers if something went wrong */
1060 if (ret) {
1061 return_all_buffers(vin, VB2_BUF_STATE_QUEUED);
1062 v4l2_subdev_call(sd, video, s_stream, 0);
1063 }
1064
1065 spin_unlock_irqrestore(&vin->qlock, flags);
1066
1067 return ret;
1068}
1069
1070static void rvin_stop_streaming(struct vb2_queue *vq)
1071{
1072 struct rvin_dev *vin = vb2_get_drv_priv(vq);
1073 struct v4l2_subdev *sd;
1074 unsigned long flags;
1075 int retries = 0;
1076
1077 spin_lock_irqsave(&vin->qlock, flags);
1078
1079 vin->state = STOPPING;
1080
1081 /* Wait for streaming to stop */
1082 while (retries++ < RVIN_RETRIES) {
1083
1084 rvin_capture_stop(vin);
1085
1086 /* Check if HW is stopped */
1087 if (!rvin_capture_active(vin)) {
1088 vin->state = STOPPED;
1089 break;
1090 }
1091
1092 spin_unlock_irqrestore(&vin->qlock, flags);
1093 msleep(RVIN_TIMEOUT_MS);
1094 spin_lock_irqsave(&vin->qlock, flags);
1095 }
1096
1097 if (vin->state != STOPPED) {
1098 /*
1099 * If this happens something have gone horribly wrong.
1100 * Set state to stopped to prevent the interrupt handler
1101 * to make things worse...
1102 */
1103 vin_err(vin, "Failed stop HW, something is seriously broken\n");
1104 vin->state = STOPPED;
1105 }
1106
1107 /* Release all active buffers */
1108 return_all_buffers(vin, VB2_BUF_STATE_ERROR);
1109
1110 spin_unlock_irqrestore(&vin->qlock, flags);
1111
1112 sd = vin_to_source(vin);
1113 v4l2_subdev_call(sd, video, s_stream, 0);
1114
1115 /* disable interrupts */
1116 rvin_disable_interrupts(vin);
1117}
1118
1119static struct vb2_ops rvin_qops = {
1120 .queue_setup = rvin_queue_setup,
1121 .buf_prepare = rvin_buffer_prepare,
1122 .buf_queue = rvin_buffer_queue,
1123 .start_streaming = rvin_start_streaming,
1124 .stop_streaming = rvin_stop_streaming,
1125 .wait_prepare = vb2_ops_wait_prepare,
1126 .wait_finish = vb2_ops_wait_finish,
1127};
1128
1129void rvin_dma_remove(struct rvin_dev *vin)
1130{
1131 mutex_destroy(&vin->lock);
1132
1133 v4l2_device_unregister(&vin->v4l2_dev);
1134}
1135
1136int rvin_dma_probe(struct rvin_dev *vin, int irq)
1137{
1138 struct vb2_queue *q = &vin->queue;
1139 int i, ret;
1140
1141 /* Initialize the top-level structure */
1142 ret = v4l2_device_register(vin->dev, &vin->v4l2_dev);
1143 if (ret)
1144 return ret;
1145
1146 mutex_init(&vin->lock);
1147 INIT_LIST_HEAD(&vin->buf_list);
1148
1149 spin_lock_init(&vin->qlock);
1150
1151 vin->state = STOPPED;
1152
1153 for (i = 0; i < HW_BUFFER_NUM; i++)
1154 vin->queue_buf[i] = NULL;
1155
1156 /* buffer queue */
1157 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1158 q->io_modes = VB2_MMAP | VB2_READ | VB2_DMABUF;
1159 q->lock = &vin->lock;
1160 q->drv_priv = vin;
1161 q->buf_struct_size = sizeof(struct rvin_buffer);
1162 q->ops = &rvin_qops;
1163 q->mem_ops = &vb2_dma_contig_memops;
1164 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1165 q->min_buffers_needed = 2;
1166 q->dev = vin->dev;
1167
1168 ret = vb2_queue_init(q);
1169 if (ret < 0) {
1170 vin_err(vin, "failed to initialize VB2 queue\n");
1171 goto error;
1172 }
1173
1174 /* irq */
1175 ret = devm_request_irq(vin->dev, irq, rvin_irq, IRQF_SHARED,
1176 KBUILD_MODNAME, vin);
1177 if (ret) {
1178 vin_err(vin, "failed to request irq\n");
1179 goto error;
1180 }
1181
1182 return 0;
1183error:
1184 rvin_dma_remove(vin);
1185
1186 return ret;
1187}
diff --git a/drivers/media/platform/rcar-vin/rcar-v4l2.c b/drivers/media/platform/rcar-vin/rcar-v4l2.c
new file mode 100644
index 000000000000..10a5c107e8b9
--- /dev/null
+++ b/drivers/media/platform/rcar-vin/rcar-v4l2.c
@@ -0,0 +1,874 @@
1/*
2 * Driver for Renesas R-Car VIN
3 *
4 * Copyright (C) 2016 Renesas Electronics Corp.
5 * Copyright (C) 2011-2013 Renesas Solutions Corp.
6 * Copyright (C) 2013 Cogent Embedded, Inc., <source@cogentembedded.com>
7 * Copyright (C) 2008 Magnus Damm
8 *
9 * Based on the soc-camera rcar_vin driver
10 *
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the
13 * Free Software Foundation; either version 2 of the License, or (at your
14 * option) any later version.
15 */
16
17#include <linux/pm_runtime.h>
18
19#include <media/v4l2-event.h>
20#include <media/v4l2-ioctl.h>
21#include <media/v4l2-rect.h>
22
23#include "rcar-vin.h"
24
25#define RVIN_DEFAULT_FORMAT V4L2_PIX_FMT_YUYV
26#define RVIN_MAX_WIDTH 2048
27#define RVIN_MAX_HEIGHT 2048
28
29/* -----------------------------------------------------------------------------
30 * Format Conversions
31 */
32
33static const struct rvin_video_format rvin_formats[] = {
34 {
35 .fourcc = V4L2_PIX_FMT_NV16,
36 .bpp = 1,
37 },
38 {
39 .fourcc = V4L2_PIX_FMT_YUYV,
40 .bpp = 2,
41 },
42 {
43 .fourcc = V4L2_PIX_FMT_UYVY,
44 .bpp = 2,
45 },
46 {
47 .fourcc = V4L2_PIX_FMT_RGB565,
48 .bpp = 2,
49 },
50 {
51 .fourcc = V4L2_PIX_FMT_XRGB555,
52 .bpp = 2,
53 },
54 {
55 .fourcc = V4L2_PIX_FMT_XBGR32,
56 .bpp = 4,
57 },
58};
59
60const struct rvin_video_format *rvin_format_from_pixel(u32 pixelformat)
61{
62 int i;
63
64 for (i = 0; i < ARRAY_SIZE(rvin_formats); i++)
65 if (rvin_formats[i].fourcc == pixelformat)
66 return rvin_formats + i;
67
68 return NULL;
69}
70
71static u32 rvin_format_bytesperline(struct v4l2_pix_format *pix)
72{
73 const struct rvin_video_format *fmt;
74
75 fmt = rvin_format_from_pixel(pix->pixelformat);
76
77 if (WARN_ON(!fmt))
78 return -EINVAL;
79
80 return pix->width * fmt->bpp;
81}
82
83static u32 rvin_format_sizeimage(struct v4l2_pix_format *pix)
84{
85 if (pix->pixelformat == V4L2_PIX_FMT_NV16)
86 return pix->bytesperline * pix->height * 2;
87
88 return pix->bytesperline * pix->height;
89}
90
91/* -----------------------------------------------------------------------------
92 * V4L2
93 */
94
95static int __rvin_try_format_source(struct rvin_dev *vin,
96 u32 which,
97 struct v4l2_pix_format *pix,
98 struct rvin_source_fmt *source)
99{
100 struct v4l2_subdev *sd;
101 struct v4l2_subdev_pad_config *pad_cfg;
102 struct v4l2_subdev_format format = {
103 .which = which,
104 };
105 int ret;
106
107 sd = vin_to_source(vin);
108
109 v4l2_fill_mbus_format(&format.format, pix, vin->source.code);
110
111 pad_cfg = v4l2_subdev_alloc_pad_config(sd);
112 if (pad_cfg == NULL)
113 return -ENOMEM;
114
115 format.pad = vin->src_pad_idx;
116
117 ret = v4l2_device_call_until_err(sd->v4l2_dev, 0, pad, set_fmt,
118 pad_cfg, &format);
119 if (ret < 0)
120 goto cleanup;
121
122 v4l2_fill_pix_format(pix, &format.format);
123
124 source->width = pix->width;
125 source->height = pix->height;
126
127 vin_dbg(vin, "Source resolution: %ux%u\n", source->width,
128 source->height);
129
130cleanup:
131 v4l2_subdev_free_pad_config(pad_cfg);
132 return 0;
133}
134
135static int __rvin_try_format(struct rvin_dev *vin,
136 u32 which,
137 struct v4l2_pix_format *pix,
138 struct rvin_source_fmt *source)
139{
140 const struct rvin_video_format *info;
141 u32 rwidth, rheight, walign;
142
143 /* Requested */
144 rwidth = pix->width;
145 rheight = pix->height;
146
147 /*
148 * Retrieve format information and select the current format if the
149 * requested format isn't supported.
150 */
151 info = rvin_format_from_pixel(pix->pixelformat);
152 if (!info) {
153 vin_dbg(vin, "Format %x not found, keeping %x\n",
154 pix->pixelformat, vin->format.pixelformat);
155 *pix = vin->format;
156 pix->width = rwidth;
157 pix->height = rheight;
158 }
159
160 /* Always recalculate */
161 pix->bytesperline = 0;
162 pix->sizeimage = 0;
163
164 /* Limit to source capabilities */
165 __rvin_try_format_source(vin, which, pix, source);
166
167 /* If source can't match format try if VIN can scale */
168 if (source->width != rwidth || source->height != rheight)
169 rvin_scale_try(vin, pix, rwidth, rheight);
170
171 /* HW limit width to a multiple of 32 (2^5) for NV16 else 2 (2^1) */
172 walign = vin->format.pixelformat == V4L2_PIX_FMT_NV16 ? 5 : 1;
173
174 /* Limit to VIN capabilities */
175 v4l_bound_align_image(&pix->width, 2, RVIN_MAX_WIDTH, walign,
176 &pix->height, 4, RVIN_MAX_HEIGHT, 2, 0);
177
178 switch (pix->field) {
179 case V4L2_FIELD_NONE:
180 case V4L2_FIELD_TOP:
181 case V4L2_FIELD_BOTTOM:
182 case V4L2_FIELD_INTERLACED_TB:
183 case V4L2_FIELD_INTERLACED_BT:
184 case V4L2_FIELD_INTERLACED:
185 break;
186 default:
187 pix->field = V4L2_FIELD_NONE;
188 break;
189 }
190
191 pix->bytesperline = max_t(u32, pix->bytesperline,
192 rvin_format_bytesperline(pix));
193 pix->sizeimage = max_t(u32, pix->sizeimage,
194 rvin_format_sizeimage(pix));
195
196 vin_dbg(vin, "Requested %ux%u Got %ux%u bpl: %d size: %d\n",
197 rwidth, rheight, pix->width, pix->height,
198 pix->bytesperline, pix->sizeimage);
199
200 return 0;
201}
202
203static int rvin_querycap(struct file *file, void *priv,
204 struct v4l2_capability *cap)
205{
206 struct rvin_dev *vin = video_drvdata(file);
207
208 strlcpy(cap->driver, KBUILD_MODNAME, sizeof(cap->driver));
209 strlcpy(cap->card, "R_Car_VIN", sizeof(cap->card));
210 snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
211 dev_name(vin->dev));
212 return 0;
213}
214
215static int rvin_try_fmt_vid_cap(struct file *file, void *priv,
216 struct v4l2_format *f)
217{
218 struct rvin_dev *vin = video_drvdata(file);
219 struct rvin_source_fmt source;
220
221 return __rvin_try_format(vin, V4L2_SUBDEV_FORMAT_TRY, &f->fmt.pix,
222 &source);
223}
224
225static int rvin_s_fmt_vid_cap(struct file *file, void *priv,
226 struct v4l2_format *f)
227{
228 struct rvin_dev *vin = video_drvdata(file);
229 struct rvin_source_fmt source;
230 int ret;
231
232 if (vb2_is_busy(&vin->queue))
233 return -EBUSY;
234
235 ret = __rvin_try_format(vin, V4L2_SUBDEV_FORMAT_ACTIVE, &f->fmt.pix,
236 &source);
237 if (ret)
238 return ret;
239
240 vin->source.width = source.width;
241 vin->source.height = source.height;
242
243 vin->format = f->fmt.pix;
244
245 return 0;
246}
247
248static int rvin_g_fmt_vid_cap(struct file *file, void *priv,
249 struct v4l2_format *f)
250{
251 struct rvin_dev *vin = video_drvdata(file);
252
253 f->fmt.pix = vin->format;
254
255 return 0;
256}
257
258static int rvin_enum_fmt_vid_cap(struct file *file, void *priv,
259 struct v4l2_fmtdesc *f)
260{
261 if (f->index >= ARRAY_SIZE(rvin_formats))
262 return -EINVAL;
263
264 f->pixelformat = rvin_formats[f->index].fourcc;
265
266 return 0;
267}
268
269static int rvin_g_selection(struct file *file, void *fh,
270 struct v4l2_selection *s)
271{
272 struct rvin_dev *vin = video_drvdata(file);
273
274 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
275 return -EINVAL;
276
277 switch (s->target) {
278 case V4L2_SEL_TGT_CROP_BOUNDS:
279 case V4L2_SEL_TGT_CROP_DEFAULT:
280 s->r.left = s->r.top = 0;
281 s->r.width = vin->source.width;
282 s->r.height = vin->source.height;
283 break;
284 case V4L2_SEL_TGT_CROP:
285 s->r = vin->crop;
286 break;
287 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
288 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
289 s->r.left = s->r.top = 0;
290 s->r.width = vin->format.width;
291 s->r.height = vin->format.height;
292 break;
293 case V4L2_SEL_TGT_COMPOSE:
294 s->r = vin->compose;
295 break;
296 default:
297 return -EINVAL;
298 }
299
300 return 0;
301}
302
303static int rvin_s_selection(struct file *file, void *fh,
304 struct v4l2_selection *s)
305{
306 struct rvin_dev *vin = video_drvdata(file);
307 const struct rvin_video_format *fmt;
308 struct v4l2_rect r = s->r;
309 struct v4l2_rect max_rect;
310 struct v4l2_rect min_rect = {
311 .width = 6,
312 .height = 2,
313 };
314
315 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
316 return -EINVAL;
317
318 v4l2_rect_set_min_size(&r, &min_rect);
319
320 switch (s->target) {
321 case V4L2_SEL_TGT_CROP:
322 /* Can't crop outside of source input */
323 max_rect.top = max_rect.left = 0;
324 max_rect.width = vin->source.width;
325 max_rect.height = vin->source.height;
326 v4l2_rect_map_inside(&r, &max_rect);
327
328 v4l_bound_align_image(&r.width, 2, vin->source.width, 1,
329 &r.height, 4, vin->source.height, 2, 0);
330
331 r.top = clamp_t(s32, r.top, 0, vin->source.height - r.height);
332 r.left = clamp_t(s32, r.left, 0, vin->source.width - r.width);
333
334 vin->crop = s->r = r;
335
336 vin_dbg(vin, "Cropped %dx%d@%d:%d of %dx%d\n",
337 r.width, r.height, r.left, r.top,
338 vin->source.width, vin->source.height);
339 break;
340 case V4L2_SEL_TGT_COMPOSE:
341 /* Make sure compose rect fits inside output format */
342 max_rect.top = max_rect.left = 0;
343 max_rect.width = vin->format.width;
344 max_rect.height = vin->format.height;
345 v4l2_rect_map_inside(&r, &max_rect);
346
347 /*
348 * Composing is done by adding a offset to the buffer address,
349 * the HW wants this address to be aligned to HW_BUFFER_MASK.
350 * Make sure the top and left values meets this requirement.
351 */
352 while ((r.top * vin->format.bytesperline) & HW_BUFFER_MASK)
353 r.top--;
354
355 fmt = rvin_format_from_pixel(vin->format.pixelformat);
356 while ((r.left * fmt->bpp) & HW_BUFFER_MASK)
357 r.left--;
358
359 vin->compose = s->r = r;
360
361 vin_dbg(vin, "Compose %dx%d@%d:%d in %dx%d\n",
362 r.width, r.height, r.left, r.top,
363 vin->format.width, vin->format.height);
364 break;
365 default:
366 return -EINVAL;
367 }
368
369 /* HW supports modifying configuration while running */
370 rvin_crop_scale_comp(vin);
371
372 return 0;
373}
374
375static int rvin_cropcap(struct file *file, void *priv,
376 struct v4l2_cropcap *crop)
377{
378 struct rvin_dev *vin = video_drvdata(file);
379 struct v4l2_subdev *sd = vin_to_source(vin);
380
381 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
382 return -EINVAL;
383
384 return v4l2_subdev_call(sd, video, cropcap, crop);
385}
386
387static int rvin_enum_input(struct file *file, void *priv,
388 struct v4l2_input *i)
389{
390 struct rvin_dev *vin = video_drvdata(file);
391 struct v4l2_subdev *sd = vin_to_source(vin);
392 int ret;
393
394 if (i->index != 0)
395 return -EINVAL;
396
397 ret = v4l2_subdev_call(sd, video, g_input_status, &i->status);
398 if (ret < 0 && ret != -ENOIOCTLCMD && ret != -ENODEV)
399 return ret;
400
401 i->type = V4L2_INPUT_TYPE_CAMERA;
402 i->std = vin->vdev.tvnorms;
403
404 if (v4l2_subdev_has_op(sd, pad, dv_timings_cap))
405 i->capabilities = V4L2_IN_CAP_DV_TIMINGS;
406
407 strlcpy(i->name, "Camera", sizeof(i->name));
408
409 return 0;
410}
411
412static int rvin_g_input(struct file *file, void *priv, unsigned int *i)
413{
414 *i = 0;
415 return 0;
416}
417
418static int rvin_s_input(struct file *file, void *priv, unsigned int i)
419{
420 if (i > 0)
421 return -EINVAL;
422 return 0;
423}
424
425static int rvin_querystd(struct file *file, void *priv, v4l2_std_id *a)
426{
427 struct rvin_dev *vin = video_drvdata(file);
428 struct v4l2_subdev *sd = vin_to_source(vin);
429
430 return v4l2_subdev_call(sd, video, querystd, a);
431}
432
433static int rvin_s_std(struct file *file, void *priv, v4l2_std_id a)
434{
435 struct rvin_dev *vin = video_drvdata(file);
436 struct v4l2_subdev *sd = vin_to_source(vin);
437 struct v4l2_subdev_format fmt = {
438 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
439 };
440 struct v4l2_mbus_framefmt *mf = &fmt.format;
441 int ret = v4l2_subdev_call(sd, video, s_std, a);
442
443 if (ret < 0)
444 return ret;
445
446 /* Changing the standard will change the width/height */
447 ret = v4l2_subdev_call(sd, pad, get_fmt, NULL, &fmt);
448 if (ret) {
449 vin_err(vin, "Failed to get initial format\n");
450 return ret;
451 }
452
453 vin->format.width = mf->width;
454 vin->format.height = mf->height;
455
456 vin->crop.top = vin->crop.left = 0;
457 vin->crop.width = mf->width;
458 vin->crop.height = mf->height;
459
460 vin->compose.top = vin->compose.left = 0;
461 vin->compose.width = mf->width;
462 vin->compose.height = mf->height;
463
464 return 0;
465}
466
467static int rvin_g_std(struct file *file, void *priv, v4l2_std_id *a)
468{
469 struct rvin_dev *vin = video_drvdata(file);
470 struct v4l2_subdev *sd = vin_to_source(vin);
471
472 return v4l2_subdev_call(sd, video, g_std, a);
473}
474
475static int rvin_subscribe_event(struct v4l2_fh *fh,
476 const struct v4l2_event_subscription *sub)
477{
478 switch (sub->type) {
479 case V4L2_EVENT_SOURCE_CHANGE:
480 return v4l2_event_subscribe(fh, sub, 4, NULL);
481 }
482 return v4l2_ctrl_subscribe_event(fh, sub);
483}
484
485static int rvin_enum_dv_timings(struct file *file, void *priv_fh,
486 struct v4l2_enum_dv_timings *timings)
487{
488 struct rvin_dev *vin = video_drvdata(file);
489 struct v4l2_subdev *sd = vin_to_source(vin);
490 int pad, ret;
491
492 pad = timings->pad;
493 timings->pad = vin->src_pad_idx;
494
495 ret = v4l2_subdev_call(sd, pad, enum_dv_timings, timings);
496
497 timings->pad = pad;
498
499 return ret;
500}
501
502static int rvin_s_dv_timings(struct file *file, void *priv_fh,
503 struct v4l2_dv_timings *timings)
504{
505 struct rvin_dev *vin = video_drvdata(file);
506 struct v4l2_subdev *sd = vin_to_source(vin);
507 int err;
508
509 err = v4l2_subdev_call(sd,
510 video, s_dv_timings, timings);
511 if (!err) {
512 vin->source.width = timings->bt.width;
513 vin->source.height = timings->bt.height;
514 vin->format.width = timings->bt.width;
515 vin->format.height = timings->bt.height;
516 }
517 return err;
518}
519
520static int rvin_g_dv_timings(struct file *file, void *priv_fh,
521 struct v4l2_dv_timings *timings)
522{
523 struct rvin_dev *vin = video_drvdata(file);
524 struct v4l2_subdev *sd = vin_to_source(vin);
525
526 return v4l2_subdev_call(sd,
527 video, g_dv_timings, timings);
528}
529
530static int rvin_query_dv_timings(struct file *file, void *priv_fh,
531 struct v4l2_dv_timings *timings)
532{
533 struct rvin_dev *vin = video_drvdata(file);
534 struct v4l2_subdev *sd = vin_to_source(vin);
535
536 return v4l2_subdev_call(sd,
537 video, query_dv_timings, timings);
538}
539
540static int rvin_dv_timings_cap(struct file *file, void *priv_fh,
541 struct v4l2_dv_timings_cap *cap)
542{
543 struct rvin_dev *vin = video_drvdata(file);
544 struct v4l2_subdev *sd = vin_to_source(vin);
545 int pad, ret;
546
547 pad = cap->pad;
548 cap->pad = vin->src_pad_idx;
549
550 ret = v4l2_subdev_call(sd, pad, dv_timings_cap, cap);
551
552 cap->pad = pad;
553
554 return ret;
555}
556
557static const struct v4l2_ioctl_ops rvin_ioctl_ops = {
558 .vidioc_querycap = rvin_querycap,
559 .vidioc_try_fmt_vid_cap = rvin_try_fmt_vid_cap,
560 .vidioc_g_fmt_vid_cap = rvin_g_fmt_vid_cap,
561 .vidioc_s_fmt_vid_cap = rvin_s_fmt_vid_cap,
562 .vidioc_enum_fmt_vid_cap = rvin_enum_fmt_vid_cap,
563
564 .vidioc_g_selection = rvin_g_selection,
565 .vidioc_s_selection = rvin_s_selection,
566
567 .vidioc_cropcap = rvin_cropcap,
568
569 .vidioc_enum_input = rvin_enum_input,
570 .vidioc_g_input = rvin_g_input,
571 .vidioc_s_input = rvin_s_input,
572
573 .vidioc_dv_timings_cap = rvin_dv_timings_cap,
574 .vidioc_enum_dv_timings = rvin_enum_dv_timings,
575 .vidioc_g_dv_timings = rvin_g_dv_timings,
576 .vidioc_s_dv_timings = rvin_s_dv_timings,
577 .vidioc_query_dv_timings = rvin_query_dv_timings,
578
579 .vidioc_querystd = rvin_querystd,
580 .vidioc_g_std = rvin_g_std,
581 .vidioc_s_std = rvin_s_std,
582
583 .vidioc_reqbufs = vb2_ioctl_reqbufs,
584 .vidioc_create_bufs = vb2_ioctl_create_bufs,
585 .vidioc_querybuf = vb2_ioctl_querybuf,
586 .vidioc_qbuf = vb2_ioctl_qbuf,
587 .vidioc_dqbuf = vb2_ioctl_dqbuf,
588 .vidioc_expbuf = vb2_ioctl_expbuf,
589 .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
590 .vidioc_streamon = vb2_ioctl_streamon,
591 .vidioc_streamoff = vb2_ioctl_streamoff,
592
593 .vidioc_log_status = v4l2_ctrl_log_status,
594 .vidioc_subscribe_event = rvin_subscribe_event,
595 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
596};
597
598/* -----------------------------------------------------------------------------
599 * File Operations
600 */
601
602static int rvin_power_on(struct rvin_dev *vin)
603{
604 int ret;
605 struct v4l2_subdev *sd = vin_to_source(vin);
606
607 pm_runtime_get_sync(vin->v4l2_dev.dev);
608
609 ret = v4l2_subdev_call(sd, core, s_power, 1);
610 if (ret < 0 && ret != -ENOIOCTLCMD && ret != -ENODEV)
611 return ret;
612 return 0;
613}
614
615static int rvin_power_off(struct rvin_dev *vin)
616{
617 int ret;
618 struct v4l2_subdev *sd = vin_to_source(vin);
619
620 ret = v4l2_subdev_call(sd, core, s_power, 0);
621
622 pm_runtime_put(vin->v4l2_dev.dev);
623
624 if (ret < 0 && ret != -ENOIOCTLCMD && ret != -ENODEV)
625 return ret;
626
627 return 0;
628}
629
630static int rvin_initialize_device(struct file *file)
631{
632 struct rvin_dev *vin = video_drvdata(file);
633 int ret;
634
635 struct v4l2_format f = {
636 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
637 .fmt.pix = {
638 .width = vin->format.width,
639 .height = vin->format.height,
640 .field = vin->format.field,
641 .colorspace = vin->format.colorspace,
642 .pixelformat = vin->format.pixelformat,
643 },
644 };
645
646 ret = rvin_power_on(vin);
647 if (ret < 0)
648 return ret;
649
650 pm_runtime_enable(&vin->vdev.dev);
651 ret = pm_runtime_resume(&vin->vdev.dev);
652 if (ret < 0 && ret != -ENOSYS)
653 goto eresume;
654
655 /*
656 * Try to configure with default parameters. Notice: this is the
657 * very first open, so, we cannot race against other calls,
658 * apart from someone else calling open() simultaneously, but
659 * .host_lock is protecting us against it.
660 */
661 ret = rvin_s_fmt_vid_cap(file, NULL, &f);
662 if (ret < 0)
663 goto esfmt;
664
665 v4l2_ctrl_handler_setup(&vin->ctrl_handler);
666
667 return 0;
668esfmt:
669 pm_runtime_disable(&vin->vdev.dev);
670eresume:
671 rvin_power_off(vin);
672
673 return ret;
674}
675
676static int rvin_open(struct file *file)
677{
678 struct rvin_dev *vin = video_drvdata(file);
679 int ret;
680
681 mutex_lock(&vin->lock);
682
683 file->private_data = vin;
684
685 ret = v4l2_fh_open(file);
686 if (ret)
687 goto unlock;
688
689 if (!v4l2_fh_is_singular_file(file))
690 goto unlock;
691
692 if (rvin_initialize_device(file)) {
693 v4l2_fh_release(file);
694 ret = -ENODEV;
695 }
696
697unlock:
698 mutex_unlock(&vin->lock);
699 return ret;
700}
701
702static int rvin_release(struct file *file)
703{
704 struct rvin_dev *vin = video_drvdata(file);
705 bool fh_singular;
706 int ret;
707
708 mutex_lock(&vin->lock);
709
710 /* Save the singular status before we call the clean-up helper */
711 fh_singular = v4l2_fh_is_singular_file(file);
712
713 /* the release helper will cleanup any on-going streaming */
714 ret = _vb2_fop_release(file, NULL);
715
716 /*
717 * If this was the last open file.
718 * Then de-initialize hw module.
719 */
720 if (fh_singular) {
721 pm_runtime_suspend(&vin->vdev.dev);
722 pm_runtime_disable(&vin->vdev.dev);
723 rvin_power_off(vin);
724 }
725
726 mutex_unlock(&vin->lock);
727
728 return ret;
729}
730
731static const struct v4l2_file_operations rvin_fops = {
732 .owner = THIS_MODULE,
733 .unlocked_ioctl = video_ioctl2,
734 .open = rvin_open,
735 .release = rvin_release,
736 .poll = vb2_fop_poll,
737 .mmap = vb2_fop_mmap,
738 .read = vb2_fop_read,
739};
740
741void rvin_v4l2_remove(struct rvin_dev *vin)
742{
743 v4l2_info(&vin->v4l2_dev, "Removing %s\n",
744 video_device_node_name(&vin->vdev));
745
746 /* Checks internaly if handlers have been init or not */
747 v4l2_ctrl_handler_free(&vin->ctrl_handler);
748
749 /* Checks internaly if vdev have been init or not */
750 video_unregister_device(&vin->vdev);
751}
752
753static void rvin_notify(struct v4l2_subdev *sd,
754 unsigned int notification, void *arg)
755{
756 struct rvin_dev *vin =
757 container_of(sd->v4l2_dev, struct rvin_dev, v4l2_dev);
758
759 switch (notification) {
760 case V4L2_DEVICE_NOTIFY_EVENT:
761 v4l2_event_queue(&vin->vdev, arg);
762 break;
763 default:
764 break;
765 }
766}
767
768int rvin_v4l2_probe(struct rvin_dev *vin)
769{
770 struct v4l2_subdev_format fmt = {
771 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
772 };
773 struct v4l2_mbus_framefmt *mf = &fmt.format;
774 struct video_device *vdev = &vin->vdev;
775 struct v4l2_subdev *sd = vin_to_source(vin);
776#if defined(CONFIG_MEDIA_CONTROLLER)
777 int pad_idx;
778#endif
779 int ret;
780
781 v4l2_set_subdev_hostdata(sd, vin);
782
783 vin->v4l2_dev.notify = rvin_notify;
784
785 ret = v4l2_subdev_call(sd, video, g_tvnorms, &vin->vdev.tvnorms);
786 if (ret < 0 && ret != -ENOIOCTLCMD && ret != -ENODEV)
787 return ret;
788
789 if (vin->vdev.tvnorms == 0) {
790 /* Disable the STD API if there are no tvnorms defined */
791 v4l2_disable_ioctl(&vin->vdev, VIDIOC_G_STD);
792 v4l2_disable_ioctl(&vin->vdev, VIDIOC_S_STD);
793 v4l2_disable_ioctl(&vin->vdev, VIDIOC_QUERYSTD);
794 v4l2_disable_ioctl(&vin->vdev, VIDIOC_ENUMSTD);
795 }
796
797 /* Add the controls */
798 /*
799 * Currently the subdev with the largest number of controls (13) is
800 * ov6550. So let's pick 16 as a hint for the control handler. Note
801 * that this is a hint only: too large and you waste some memory, too
802 * small and there is a (very) small performance hit when looking up
803 * controls in the internal hash.
804 */
805 ret = v4l2_ctrl_handler_init(&vin->ctrl_handler, 16);
806 if (ret < 0)
807 return ret;
808
809 ret = v4l2_ctrl_add_handler(&vin->ctrl_handler, sd->ctrl_handler, NULL);
810 if (ret < 0)
811 return ret;
812
813 /* video node */
814 vdev->fops = &rvin_fops;
815 vdev->v4l2_dev = &vin->v4l2_dev;
816 vdev->queue = &vin->queue;
817 strlcpy(vdev->name, KBUILD_MODNAME, sizeof(vdev->name));
818 vdev->release = video_device_release_empty;
819 vdev->ioctl_ops = &rvin_ioctl_ops;
820 vdev->lock = &vin->lock;
821 vdev->ctrl_handler = &vin->ctrl_handler;
822 vdev->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING |
823 V4L2_CAP_READWRITE;
824
825 vin->src_pad_idx = 0;
826#if defined(CONFIG_MEDIA_CONTROLLER)
827 for (pad_idx = 0; pad_idx < sd->entity.num_pads; pad_idx++)
828 if (sd->entity.pads[pad_idx].flags
829 == MEDIA_PAD_FL_SOURCE)
830 break;
831 if (pad_idx >= sd->entity.num_pads)
832 return -EINVAL;
833
834 vin->src_pad_idx = pad_idx;
835#endif
836 fmt.pad = vin->src_pad_idx;
837
838 /* Try to improve our guess of a reasonable window format */
839 ret = v4l2_subdev_call(sd, pad, get_fmt, NULL, &fmt);
840 if (ret) {
841 vin_err(vin, "Failed to get initial format\n");
842 return ret;
843 }
844
845 /* Set default format */
846 vin->format.width = mf->width;
847 vin->format.height = mf->height;
848 vin->format.colorspace = mf->colorspace;
849 vin->format.field = mf->field;
850 vin->format.pixelformat = RVIN_DEFAULT_FORMAT;
851
852
853 /* Set initial crop and compose */
854 vin->crop.top = vin->crop.left = 0;
855 vin->crop.width = mf->width;
856 vin->crop.height = mf->height;
857
858 vin->compose.top = vin->compose.left = 0;
859 vin->compose.width = mf->width;
860 vin->compose.height = mf->height;
861
862 ret = video_register_device(&vin->vdev, VFL_TYPE_GRABBER, -1);
863 if (ret) {
864 vin_err(vin, "Failed to register video device\n");
865 return ret;
866 }
867
868 video_set_drvdata(&vin->vdev, vin);
869
870 v4l2_info(&vin->v4l2_dev, "Device registered as %s\n",
871 video_device_node_name(&vin->vdev));
872
873 return ret;
874}
diff --git a/drivers/media/platform/rcar-vin/rcar-vin.h b/drivers/media/platform/rcar-vin/rcar-vin.h
new file mode 100644
index 000000000000..31ad39a39937
--- /dev/null
+++ b/drivers/media/platform/rcar-vin/rcar-vin.h
@@ -0,0 +1,163 @@
1/*
2 * Driver for Renesas R-Car VIN
3 *
4 * Copyright (C) 2016 Renesas Electronics Corp.
5 * Copyright (C) 2011-2013 Renesas Solutions Corp.
6 * Copyright (C) 2013 Cogent Embedded, Inc., <source@cogentembedded.com>
7 * Copyright (C) 2008 Magnus Damm
8 *
9 * Based on the soc-camera rcar_vin driver
10 *
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the
13 * Free Software Foundation; either version 2 of the License, or (at your
14 * option) any later version.
15 */
16
17#ifndef __RCAR_VIN__
18#define __RCAR_VIN__
19
20#include <media/v4l2-async.h>
21#include <media/v4l2-ctrls.h>
22#include <media/v4l2-dev.h>
23#include <media/v4l2-device.h>
24#include <media/videobuf2-v4l2.h>
25
26/* Number of HW buffers */
27#define HW_BUFFER_NUM 3
28
29/* Address alignment mask for HW buffers */
30#define HW_BUFFER_MASK 0x7f
31
32enum chip_id {
33 RCAR_GEN2,
34 RCAR_H1,
35 RCAR_M1,
36};
37
38/**
39 * STOPPED - No operation in progress
40 * RUNNING - Operation in progress have buffers
41 * STALLED - No operation in progress have no buffers
42 * STOPPING - Stopping operation
43 */
44enum rvin_dma_state {
45 STOPPED = 0,
46 RUNNING,
47 STALLED,
48 STOPPING,
49};
50
51/**
52 * struct rvin_source_fmt - Source information
53 * @code: Media bus format from source
54 * @width: Width from source
55 * @height: Height from source
56 */
57struct rvin_source_fmt {
58 u32 code;
59 u32 width;
60 u32 height;
61};
62
63/**
64 * struct rvin_video_format - Data format stored in memory
65 * @fourcc: Pixelformat
66 * @bpp: Bytes per pixel
67 */
68struct rvin_video_format {
69 u32 fourcc;
70 u8 bpp;
71};
72
73struct rvin_graph_entity {
74 struct device_node *node;
75 struct media_entity *entity;
76
77 struct v4l2_async_subdev asd;
78 struct v4l2_subdev *subdev;
79};
80
81/**
82 * struct rvin_dev - Renesas VIN device structure
83 * @dev: (OF) device
84 * @base: device I/O register space remapped to virtual memory
85 * @chip: type of VIN chip
86 * @mbus_cfg media bus configuration
87 *
88 * @vdev: V4L2 video device associated with VIN
89 * @v4l2_dev: V4L2 device
90 * @src_pad_idx: source pad index for media controller drivers
91 * @ctrl_handler: V4L2 control handler
92 * @notifier: V4L2 asynchronous subdevs notifier
93 * @entity: entity in the DT for subdevice
94 *
95 * @lock: protects @queue
96 * @queue: vb2 buffers queue
97 *
98 * @qlock: protects @queue_buf, @buf_list, @continuous, @sequence
99 * @state
100 * @queue_buf: Keeps track of buffers given to HW slot
101 * @buf_list: list of queued buffers
102 * @continuous: tracks if active operation is continuous or single mode
103 * @sequence: V4L2 buffers sequence number
104 * @state: keeps track of operation state
105 *
106 * @source: active format from the video source
107 * @format: active V4L2 pixel format
108 *
109 * @crop: active cropping
110 * @compose: active composing
111 */
112struct rvin_dev {
113 struct device *dev;
114 void __iomem *base;
115 enum chip_id chip;
116 struct v4l2_mbus_config mbus_cfg;
117
118 struct video_device vdev;
119 struct v4l2_device v4l2_dev;
120 int src_pad_idx;
121 struct v4l2_ctrl_handler ctrl_handler;
122 struct v4l2_async_notifier notifier;
123 struct rvin_graph_entity entity;
124
125 struct mutex lock;
126 struct vb2_queue queue;
127
128 spinlock_t qlock;
129 struct vb2_v4l2_buffer *queue_buf[HW_BUFFER_NUM];
130 struct list_head buf_list;
131 bool continuous;
132 unsigned int sequence;
133 enum rvin_dma_state state;
134
135 struct rvin_source_fmt source;
136 struct v4l2_pix_format format;
137
138 struct v4l2_rect crop;
139 struct v4l2_rect compose;
140};
141
142#define vin_to_source(vin) vin->entity.subdev
143
144/* Debug */
145#define vin_dbg(d, fmt, arg...) dev_dbg(d->dev, fmt, ##arg)
146#define vin_info(d, fmt, arg...) dev_info(d->dev, fmt, ##arg)
147#define vin_warn(d, fmt, arg...) dev_warn(d->dev, fmt, ##arg)
148#define vin_err(d, fmt, arg...) dev_err(d->dev, fmt, ##arg)
149
150int rvin_dma_probe(struct rvin_dev *vin, int irq);
151void rvin_dma_remove(struct rvin_dev *vin);
152
153int rvin_v4l2_probe(struct rvin_dev *vin);
154void rvin_v4l2_remove(struct rvin_dev *vin);
155
156const struct rvin_video_format *rvin_format_from_pixel(u32 pixelformat);
157
158/* Cropping, composing and scaling */
159void rvin_scale_try(struct rvin_dev *vin, struct v4l2_pix_format *pix,
160 u32 width, u32 height);
161void rvin_crop_scale_comp(struct rvin_dev *vin);
162
163#endif
diff --git a/drivers/media/platform/rcar_jpu.c b/drivers/media/platform/rcar_jpu.c
index 552789a69c86..16782ceb29c3 100644
--- a/drivers/media/platform/rcar_jpu.c
+++ b/drivers/media/platform/rcar_jpu.c
@@ -203,7 +203,6 @@
203 * @irq: JPEG IP irq 203 * @irq: JPEG IP irq
204 * @clk: JPEG IP clock 204 * @clk: JPEG IP clock
205 * @dev: JPEG IP struct device 205 * @dev: JPEG IP struct device
206 * @alloc_ctx: videobuf2 memory allocator's context
207 * @ref_count: reference counter 206 * @ref_count: reference counter
208 */ 207 */
209struct jpu { 208struct jpu {
@@ -220,7 +219,6 @@ struct jpu {
220 unsigned int irq; 219 unsigned int irq;
221 struct clk *clk; 220 struct clk *clk;
222 struct device *dev; 221 struct device *dev;
223 void *alloc_ctx;
224 int ref_count; 222 int ref_count;
225}; 223};
226 224
@@ -1016,7 +1014,7 @@ error_free:
1016 */ 1014 */
1017static int jpu_queue_setup(struct vb2_queue *vq, 1015static int jpu_queue_setup(struct vb2_queue *vq,
1018 unsigned int *nbuffers, unsigned int *nplanes, 1016 unsigned int *nbuffers, unsigned int *nplanes,
1019 unsigned int sizes[], void *alloc_ctxs[]) 1017 unsigned int sizes[], struct device *alloc_devs[])
1020{ 1018{
1021 struct jpu_ctx *ctx = vb2_get_drv_priv(vq); 1019 struct jpu_ctx *ctx = vb2_get_drv_priv(vq);
1022 struct jpu_q_data *q_data; 1020 struct jpu_q_data *q_data;
@@ -1033,17 +1031,14 @@ static int jpu_queue_setup(struct vb2_queue *vq,
1033 1031
1034 if (sizes[i] < q_size) 1032 if (sizes[i] < q_size)
1035 return -EINVAL; 1033 return -EINVAL;
1036 alloc_ctxs[i] = ctx->jpu->alloc_ctx;
1037 } 1034 }
1038 return 0; 1035 return 0;
1039 } 1036 }
1040 1037
1041 *nplanes = q_data->format.num_planes; 1038 *nplanes = q_data->format.num_planes;
1042 1039
1043 for (i = 0; i < *nplanes; i++) { 1040 for (i = 0; i < *nplanes; i++)
1044 sizes[i] = q_data->format.plane_fmt[i].sizeimage; 1041 sizes[i] = q_data->format.plane_fmt[i].sizeimage;
1045 alloc_ctxs[i] = ctx->jpu->alloc_ctx;
1046 }
1047 1042
1048 return 0; 1043 return 0;
1049} 1044}
@@ -1214,6 +1209,7 @@ static int jpu_queue_init(void *priv, struct vb2_queue *src_vq,
1214 src_vq->mem_ops = &vb2_dma_contig_memops; 1209 src_vq->mem_ops = &vb2_dma_contig_memops;
1215 src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY; 1210 src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1216 src_vq->lock = &ctx->jpu->mutex; 1211 src_vq->lock = &ctx->jpu->mutex;
1212 src_vq->dev = ctx->jpu->v4l2_dev.dev;
1217 1213
1218 ret = vb2_queue_init(src_vq); 1214 ret = vb2_queue_init(src_vq);
1219 if (ret) 1215 if (ret)
@@ -1228,6 +1224,7 @@ static int jpu_queue_init(void *priv, struct vb2_queue *src_vq,
1228 dst_vq->mem_ops = &vb2_dma_contig_memops; 1224 dst_vq->mem_ops = &vb2_dma_contig_memops;
1229 dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY; 1225 dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1230 dst_vq->lock = &ctx->jpu->mutex; 1226 dst_vq->lock = &ctx->jpu->mutex;
1227 dst_vq->dev = ctx->jpu->v4l2_dev.dev;
1231 1228
1232 return vb2_queue_init(dst_vq); 1229 return vb2_queue_init(dst_vq);
1233} 1230}
@@ -1676,13 +1673,6 @@ static int jpu_probe(struct platform_device *pdev)
1676 goto device_register_rollback; 1673 goto device_register_rollback;
1677 } 1674 }
1678 1675
1679 jpu->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
1680 if (IS_ERR(jpu->alloc_ctx)) {
1681 v4l2_err(&jpu->v4l2_dev, "Failed to init memory allocator\n");
1682 ret = PTR_ERR(jpu->alloc_ctx);
1683 goto m2m_init_rollback;
1684 }
1685
1686 /* fill in qantization and Huffman tables for encoder */ 1676 /* fill in qantization and Huffman tables for encoder */
1687 for (i = 0; i < JPU_MAX_QUALITY; i++) 1677 for (i = 0; i < JPU_MAX_QUALITY; i++)
1688 jpu_generate_hdr(i, (unsigned char *)jpeg_hdrs[i]); 1678 jpu_generate_hdr(i, (unsigned char *)jpeg_hdrs[i]);
@@ -1699,7 +1689,7 @@ static int jpu_probe(struct platform_device *pdev)
1699 ret = video_register_device(&jpu->vfd_encoder, VFL_TYPE_GRABBER, -1); 1689 ret = video_register_device(&jpu->vfd_encoder, VFL_TYPE_GRABBER, -1);
1700 if (ret) { 1690 if (ret) {
1701 v4l2_err(&jpu->v4l2_dev, "Failed to register video device\n"); 1691 v4l2_err(&jpu->v4l2_dev, "Failed to register video device\n");
1702 goto vb2_allocator_rollback; 1692 goto m2m_init_rollback;
1703 } 1693 }
1704 1694
1705 video_set_drvdata(&jpu->vfd_encoder, jpu); 1695 video_set_drvdata(&jpu->vfd_encoder, jpu);
@@ -1732,9 +1722,6 @@ static int jpu_probe(struct platform_device *pdev)
1732enc_vdev_register_rollback: 1722enc_vdev_register_rollback:
1733 video_unregister_device(&jpu->vfd_encoder); 1723 video_unregister_device(&jpu->vfd_encoder);
1734 1724
1735vb2_allocator_rollback:
1736 vb2_dma_contig_cleanup_ctx(jpu->alloc_ctx);
1737
1738m2m_init_rollback: 1725m2m_init_rollback:
1739 v4l2_m2m_release(jpu->m2m_dev); 1726 v4l2_m2m_release(jpu->m2m_dev);
1740 1727
@@ -1750,7 +1737,6 @@ static int jpu_remove(struct platform_device *pdev)
1750 1737
1751 video_unregister_device(&jpu->vfd_decoder); 1738 video_unregister_device(&jpu->vfd_decoder);
1752 video_unregister_device(&jpu->vfd_encoder); 1739 video_unregister_device(&jpu->vfd_encoder);
1753 vb2_dma_contig_cleanup_ctx(jpu->alloc_ctx);
1754 v4l2_m2m_release(jpu->m2m_dev); 1740 v4l2_m2m_release(jpu->m2m_dev);
1755 v4l2_device_unregister(&jpu->v4l2_dev); 1741 v4l2_device_unregister(&jpu->v4l2_dev);
1756 1742
diff --git a/drivers/media/platform/s3c-camif/camif-capture.c b/drivers/media/platform/s3c-camif/camif-capture.c
index bd060ef5d1e1..0413a861a59a 100644
--- a/drivers/media/platform/s3c-camif/camif-capture.c
+++ b/drivers/media/platform/s3c-camif/camif-capture.c
@@ -437,10 +437,9 @@ static void stop_streaming(struct vb2_queue *vq)
437 437
438static int queue_setup(struct vb2_queue *vq, 438static int queue_setup(struct vb2_queue *vq,
439 unsigned int *num_buffers, unsigned int *num_planes, 439 unsigned int *num_buffers, unsigned int *num_planes,
440 unsigned int sizes[], void *allocators[]) 440 unsigned int sizes[], struct device *alloc_devs[])
441{ 441{
442 struct camif_vp *vp = vb2_get_drv_priv(vq); 442 struct camif_vp *vp = vb2_get_drv_priv(vq);
443 struct camif_dev *camif = vp->camif;
444 struct camif_frame *frame = &vp->out_frame; 443 struct camif_frame *frame = &vp->out_frame;
445 const struct camif_fmt *fmt = vp->out_fmt; 444 const struct camif_fmt *fmt = vp->out_fmt;
446 unsigned int size; 445 unsigned int size;
@@ -449,7 +448,6 @@ static int queue_setup(struct vb2_queue *vq,
449 return -EINVAL; 448 return -EINVAL;
450 449
451 size = (frame->f_width * frame->f_height * fmt->depth) / 8; 450 size = (frame->f_width * frame->f_height * fmt->depth) / 8;
452 allocators[0] = camif->alloc_ctx;
453 451
454 if (*num_planes) 452 if (*num_planes)
455 return sizes[0] < size ? -EINVAL : 0; 453 return sizes[0] < size ? -EINVAL : 0;
@@ -1138,6 +1136,7 @@ int s3c_camif_register_video_node(struct camif_dev *camif, int idx)
1138 q->drv_priv = vp; 1136 q->drv_priv = vp;
1139 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC; 1137 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1140 q->lock = &vp->camif->lock; 1138 q->lock = &vp->camif->lock;
1139 q->dev = camif->v4l2_dev.dev;
1141 1140
1142 ret = vb2_queue_init(q); 1141 ret = vb2_queue_init(q);
1143 if (ret) 1142 if (ret)
diff --git a/drivers/media/platform/s3c-camif/camif-core.c b/drivers/media/platform/s3c-camif/camif-core.c
index af237af204e2..ec4001970313 100644
--- a/drivers/media/platform/s3c-camif/camif-core.c
+++ b/drivers/media/platform/s3c-camif/camif-core.c
@@ -474,16 +474,9 @@ static int s3c_camif_probe(struct platform_device *pdev)
474 if (ret < 0) 474 if (ret < 0)
475 goto err_pm; 475 goto err_pm;
476 476
477 /* Initialize contiguous memory allocator */
478 camif->alloc_ctx = vb2_dma_contig_init_ctx(dev);
479 if (IS_ERR(camif->alloc_ctx)) {
480 ret = PTR_ERR(camif->alloc_ctx);
481 goto err_alloc;
482 }
483
484 ret = camif_media_dev_init(camif); 477 ret = camif_media_dev_init(camif);
485 if (ret < 0) 478 if (ret < 0)
486 goto err_mdev; 479 goto err_alloc;
487 480
488 ret = camif_register_sensor(camif); 481 ret = camif_register_sensor(camif);
489 if (ret < 0) 482 if (ret < 0)
@@ -517,8 +510,6 @@ err_sens:
517 media_device_unregister(&camif->media_dev); 510 media_device_unregister(&camif->media_dev);
518 media_device_cleanup(&camif->media_dev); 511 media_device_cleanup(&camif->media_dev);
519 camif_unregister_media_entities(camif); 512 camif_unregister_media_entities(camif);
520err_mdev:
521 vb2_dma_contig_cleanup_ctx(camif->alloc_ctx);
522err_alloc: 513err_alloc:
523 pm_runtime_put(dev); 514 pm_runtime_put(dev);
524 pm_runtime_disable(dev); 515 pm_runtime_disable(dev);
diff --git a/drivers/media/platform/s3c-camif/camif-core.h b/drivers/media/platform/s3c-camif/camif-core.h
index 57cbc3d9725d..1f5c8c94ce89 100644
--- a/drivers/media/platform/s3c-camif/camif-core.h
+++ b/drivers/media/platform/s3c-camif/camif-core.h
@@ -254,7 +254,6 @@ struct camif_vp {
254 * @ctrl_handler: v4l2 control handler (owned by @subdev) 254 * @ctrl_handler: v4l2 control handler (owned by @subdev)
255 * @test_pattern: test pattern controls 255 * @test_pattern: test pattern controls
256 * @vp: video path (DMA) description (codec/preview) 256 * @vp: video path (DMA) description (codec/preview)
257 * @alloc_ctx: memory buffer allocator context
258 * @variant: variant information for this device 257 * @variant: variant information for this device
259 * @dev: pointer to the CAMIF device struct 258 * @dev: pointer to the CAMIF device struct
260 * @pdata: a copy of the driver's platform data 259 * @pdata: a copy of the driver's platform data
@@ -291,7 +290,6 @@ struct camif_dev {
291 u8 colorfx_cr; 290 u8 colorfx_cr;
292 291
293 struct camif_vp vp[CAMIF_VP_NUM]; 292 struct camif_vp vp[CAMIF_VP_NUM];
294 struct vb2_alloc_ctx *alloc_ctx;
295 293
296 const struct s3c_camif_variant *variant; 294 const struct s3c_camif_variant *variant;
297 struct device *dev; 295 struct device *dev;
diff --git a/drivers/media/platform/s5p-g2d/g2d.c b/drivers/media/platform/s5p-g2d/g2d.c
index 612d1ea514f1..391dd7a7b362 100644
--- a/drivers/media/platform/s5p-g2d/g2d.c
+++ b/drivers/media/platform/s5p-g2d/g2d.c
@@ -103,7 +103,7 @@ static struct g2d_frame *get_frame(struct g2d_ctx *ctx,
103 103
104static int g2d_queue_setup(struct vb2_queue *vq, 104static int g2d_queue_setup(struct vb2_queue *vq,
105 unsigned int *nbuffers, unsigned int *nplanes, 105 unsigned int *nbuffers, unsigned int *nplanes,
106 unsigned int sizes[], void *alloc_ctxs[]) 106 unsigned int sizes[], struct device *alloc_devs[])
107{ 107{
108 struct g2d_ctx *ctx = vb2_get_drv_priv(vq); 108 struct g2d_ctx *ctx = vb2_get_drv_priv(vq);
109 struct g2d_frame *f = get_frame(ctx, vq->type); 109 struct g2d_frame *f = get_frame(ctx, vq->type);
@@ -113,7 +113,6 @@ static int g2d_queue_setup(struct vb2_queue *vq,
113 113
114 sizes[0] = f->size; 114 sizes[0] = f->size;
115 *nplanes = 1; 115 *nplanes = 1;
116 alloc_ctxs[0] = ctx->dev->alloc_ctx;
117 116
118 if (*nbuffers == 0) 117 if (*nbuffers == 0)
119 *nbuffers = 1; 118 *nbuffers = 1;
@@ -159,6 +158,7 @@ static int queue_init(void *priv, struct vb2_queue *src_vq,
159 src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer); 158 src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
160 src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY; 159 src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
161 src_vq->lock = &ctx->dev->mutex; 160 src_vq->lock = &ctx->dev->mutex;
161 src_vq->dev = ctx->dev->v4l2_dev.dev;
162 162
163 ret = vb2_queue_init(src_vq); 163 ret = vb2_queue_init(src_vq);
164 if (ret) 164 if (ret)
@@ -172,6 +172,7 @@ static int queue_init(void *priv, struct vb2_queue *src_vq,
172 dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer); 172 dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
173 dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY; 173 dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
174 dst_vq->lock = &ctx->dev->mutex; 174 dst_vq->lock = &ctx->dev->mutex;
175 dst_vq->dev = ctx->dev->v4l2_dev.dev;
175 176
176 return vb2_queue_init(dst_vq); 177 return vb2_queue_init(dst_vq);
177} 178}
@@ -681,15 +682,11 @@ static int g2d_probe(struct platform_device *pdev)
681 goto put_clk_gate; 682 goto put_clk_gate;
682 } 683 }
683 684
684 dev->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev); 685 vb2_dma_contig_set_max_seg_size(&pdev->dev, DMA_BIT_MASK(32));
685 if (IS_ERR(dev->alloc_ctx)) {
686 ret = PTR_ERR(dev->alloc_ctx);
687 goto unprep_clk_gate;
688 }
689 686
690 ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev); 687 ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
691 if (ret) 688 if (ret)
692 goto alloc_ctx_cleanup; 689 goto unprep_clk_gate;
693 vfd = video_device_alloc(); 690 vfd = video_device_alloc();
694 if (!vfd) { 691 if (!vfd) {
695 v4l2_err(&dev->v4l2_dev, "Failed to allocate video device\n"); 692 v4l2_err(&dev->v4l2_dev, "Failed to allocate video device\n");
@@ -734,8 +731,6 @@ rel_vdev:
734 video_device_release(vfd); 731 video_device_release(vfd);
735unreg_v4l2_dev: 732unreg_v4l2_dev:
736 v4l2_device_unregister(&dev->v4l2_dev); 733 v4l2_device_unregister(&dev->v4l2_dev);
737alloc_ctx_cleanup:
738 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
739unprep_clk_gate: 734unprep_clk_gate:
740 clk_unprepare(dev->gate); 735 clk_unprepare(dev->gate);
741put_clk_gate: 736put_clk_gate:
@@ -756,7 +751,7 @@ static int g2d_remove(struct platform_device *pdev)
756 v4l2_m2m_release(dev->m2m_dev); 751 v4l2_m2m_release(dev->m2m_dev);
757 video_unregister_device(dev->vfd); 752 video_unregister_device(dev->vfd);
758 v4l2_device_unregister(&dev->v4l2_dev); 753 v4l2_device_unregister(&dev->v4l2_dev);
759 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx); 754 vb2_dma_contig_clear_max_seg_size(&pdev->dev);
760 clk_unprepare(dev->gate); 755 clk_unprepare(dev->gate);
761 clk_put(dev->gate); 756 clk_put(dev->gate);
762 clk_unprepare(dev->clk); 757 clk_unprepare(dev->clk);
diff --git a/drivers/media/platform/s5p-g2d/g2d.h b/drivers/media/platform/s5p-g2d/g2d.h
index e31df541aa62..dd812b557e87 100644
--- a/drivers/media/platform/s5p-g2d/g2d.h
+++ b/drivers/media/platform/s5p-g2d/g2d.h
@@ -25,7 +25,6 @@ struct g2d_dev {
25 struct mutex mutex; 25 struct mutex mutex;
26 spinlock_t ctrl_lock; 26 spinlock_t ctrl_lock;
27 atomic_t num_inst; 27 atomic_t num_inst;
28 struct vb2_alloc_ctx *alloc_ctx;
29 void __iomem *regs; 28 void __iomem *regs;
30 struct clk *clk; 29 struct clk *clk;
31 struct clk *gate; 30 struct clk *gate;
diff --git a/drivers/media/platform/s5p-jpeg/jpeg-core.c b/drivers/media/platform/s5p-jpeg/jpeg-core.c
index caa19b408551..785e6936c881 100644
--- a/drivers/media/platform/s5p-jpeg/jpeg-core.c
+++ b/drivers/media/platform/s5p-jpeg/jpeg-core.c
@@ -2436,7 +2436,7 @@ static struct v4l2_m2m_ops exynos4_jpeg_m2m_ops = {
2436 2436
2437static int s5p_jpeg_queue_setup(struct vb2_queue *vq, 2437static int s5p_jpeg_queue_setup(struct vb2_queue *vq,
2438 unsigned int *nbuffers, unsigned int *nplanes, 2438 unsigned int *nbuffers, unsigned int *nplanes,
2439 unsigned int sizes[], void *alloc_ctxs[]) 2439 unsigned int sizes[], struct device *alloc_devs[])
2440{ 2440{
2441 struct s5p_jpeg_ctx *ctx = vb2_get_drv_priv(vq); 2441 struct s5p_jpeg_ctx *ctx = vb2_get_drv_priv(vq);
2442 struct s5p_jpeg_q_data *q_data = NULL; 2442 struct s5p_jpeg_q_data *q_data = NULL;
@@ -2457,7 +2457,6 @@ static int s5p_jpeg_queue_setup(struct vb2_queue *vq,
2457 *nbuffers = count; 2457 *nbuffers = count;
2458 *nplanes = 1; 2458 *nplanes = 1;
2459 sizes[0] = size; 2459 sizes[0] = size;
2460 alloc_ctxs[0] = ctx->jpeg->alloc_ctx;
2461 2460
2462 return 0; 2461 return 0;
2463} 2462}
@@ -2563,6 +2562,7 @@ static int queue_init(void *priv, struct vb2_queue *src_vq,
2563 src_vq->mem_ops = &vb2_dma_contig_memops; 2562 src_vq->mem_ops = &vb2_dma_contig_memops;
2564 src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY; 2563 src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
2565 src_vq->lock = &ctx->jpeg->lock; 2564 src_vq->lock = &ctx->jpeg->lock;
2565 src_vq->dev = ctx->jpeg->dev;
2566 2566
2567 ret = vb2_queue_init(src_vq); 2567 ret = vb2_queue_init(src_vq);
2568 if (ret) 2568 if (ret)
@@ -2576,6 +2576,7 @@ static int queue_init(void *priv, struct vb2_queue *src_vq,
2576 dst_vq->mem_ops = &vb2_dma_contig_memops; 2576 dst_vq->mem_ops = &vb2_dma_contig_memops;
2577 dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY; 2577 dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
2578 dst_vq->lock = &ctx->jpeg->lock; 2578 dst_vq->lock = &ctx->jpeg->lock;
2579 dst_vq->dev = ctx->jpeg->dev;
2579 2580
2580 return vb2_queue_init(dst_vq); 2581 return vb2_queue_init(dst_vq);
2581} 2582}
@@ -2843,19 +2844,14 @@ static int s5p_jpeg_probe(struct platform_device *pdev)
2843 goto device_register_rollback; 2844 goto device_register_rollback;
2844 } 2845 }
2845 2846
2846 jpeg->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev); 2847 vb2_dma_contig_set_max_seg_size(&pdev->dev, DMA_BIT_MASK(32));
2847 if (IS_ERR(jpeg->alloc_ctx)) {
2848 v4l2_err(&jpeg->v4l2_dev, "Failed to init memory allocator\n");
2849 ret = PTR_ERR(jpeg->alloc_ctx);
2850 goto m2m_init_rollback;
2851 }
2852 2848
2853 /* JPEG encoder /dev/videoX node */ 2849 /* JPEG encoder /dev/videoX node */
2854 jpeg->vfd_encoder = video_device_alloc(); 2850 jpeg->vfd_encoder = video_device_alloc();
2855 if (!jpeg->vfd_encoder) { 2851 if (!jpeg->vfd_encoder) {
2856 v4l2_err(&jpeg->v4l2_dev, "Failed to allocate video device\n"); 2852 v4l2_err(&jpeg->v4l2_dev, "Failed to allocate video device\n");
2857 ret = -ENOMEM; 2853 ret = -ENOMEM;
2858 goto vb2_allocator_rollback; 2854 goto m2m_init_rollback;
2859 } 2855 }
2860 snprintf(jpeg->vfd_encoder->name, sizeof(jpeg->vfd_encoder->name), 2856 snprintf(jpeg->vfd_encoder->name, sizeof(jpeg->vfd_encoder->name),
2861 "%s-enc", S5P_JPEG_M2M_NAME); 2857 "%s-enc", S5P_JPEG_M2M_NAME);
@@ -2871,7 +2867,7 @@ static int s5p_jpeg_probe(struct platform_device *pdev)
2871 if (ret) { 2867 if (ret) {
2872 v4l2_err(&jpeg->v4l2_dev, "Failed to register video device\n"); 2868 v4l2_err(&jpeg->v4l2_dev, "Failed to register video device\n");
2873 video_device_release(jpeg->vfd_encoder); 2869 video_device_release(jpeg->vfd_encoder);
2874 goto vb2_allocator_rollback; 2870 goto m2m_init_rollback;
2875 } 2871 }
2876 2872
2877 video_set_drvdata(jpeg->vfd_encoder, jpeg); 2873 video_set_drvdata(jpeg->vfd_encoder, jpeg);
@@ -2920,9 +2916,6 @@ static int s5p_jpeg_probe(struct platform_device *pdev)
2920enc_vdev_register_rollback: 2916enc_vdev_register_rollback:
2921 video_unregister_device(jpeg->vfd_encoder); 2917 video_unregister_device(jpeg->vfd_encoder);
2922 2918
2923vb2_allocator_rollback:
2924 vb2_dma_contig_cleanup_ctx(jpeg->alloc_ctx);
2925
2926m2m_init_rollback: 2919m2m_init_rollback:
2927 v4l2_m2m_release(jpeg->m2m_dev); 2920 v4l2_m2m_release(jpeg->m2m_dev);
2928 2921
@@ -2941,7 +2934,7 @@ static int s5p_jpeg_remove(struct platform_device *pdev)
2941 2934
2942 video_unregister_device(jpeg->vfd_decoder); 2935 video_unregister_device(jpeg->vfd_decoder);
2943 video_unregister_device(jpeg->vfd_encoder); 2936 video_unregister_device(jpeg->vfd_encoder);
2944 vb2_dma_contig_cleanup_ctx(jpeg->alloc_ctx); 2937 vb2_dma_contig_clear_max_seg_size(&pdev->dev);
2945 v4l2_m2m_release(jpeg->m2m_dev); 2938 v4l2_m2m_release(jpeg->m2m_dev);
2946 v4l2_device_unregister(&jpeg->v4l2_dev); 2939 v4l2_device_unregister(&jpeg->v4l2_dev);
2947 2940
diff --git a/drivers/media/platform/s5p-jpeg/jpeg-core.h b/drivers/media/platform/s5p-jpeg/jpeg-core.h
index 9b1db0934909..4492a3535df5 100644
--- a/drivers/media/platform/s5p-jpeg/jpeg-core.h
+++ b/drivers/media/platform/s5p-jpeg/jpeg-core.h
@@ -110,7 +110,6 @@ enum exynos4_jpeg_img_quality_level {
110 * @irq: JPEG IP irq 110 * @irq: JPEG IP irq
111 * @clocks: JPEG IP clock(s) 111 * @clocks: JPEG IP clock(s)
112 * @dev: JPEG IP struct device 112 * @dev: JPEG IP struct device
113 * @alloc_ctx: videobuf2 memory allocator's context
114 * @variant: driver variant to be used 113 * @variant: driver variant to be used
115 * @irq_status interrupt flags set during single encode/decode 114 * @irq_status interrupt flags set during single encode/decode
116 operation 115 operation
@@ -130,7 +129,6 @@ struct s5p_jpeg {
130 enum exynos4_jpeg_result irq_ret; 129 enum exynos4_jpeg_result irq_ret;
131 struct clk *clocks[JPEG_MAX_CLOCKS]; 130 struct clk *clocks[JPEG_MAX_CLOCKS];
132 struct device *dev; 131 struct device *dev;
133 void *alloc_ctx;
134 struct s5p_jpeg_variant *variant; 132 struct s5p_jpeg_variant *variant;
135 u32 irq_status; 133 u32 irq_status;
136}; 134};
diff --git a/drivers/media/platform/s5p-mfc/s5p_mfc.c b/drivers/media/platform/s5p-mfc/s5p_mfc.c
index b16466fe35ee..e3f104fafd0a 100644
--- a/drivers/media/platform/s5p-mfc/s5p_mfc.c
+++ b/drivers/media/platform/s5p-mfc/s5p_mfc.c
@@ -22,6 +22,7 @@
22#include <media/v4l2-event.h> 22#include <media/v4l2-event.h>
23#include <linux/workqueue.h> 23#include <linux/workqueue.h>
24#include <linux/of.h> 24#include <linux/of.h>
25#include <linux/of_reserved_mem.h>
25#include <media/videobuf2-v4l2.h> 26#include <media/videobuf2-v4l2.h>
26#include "s5p_mfc_common.h" 27#include "s5p_mfc_common.h"
27#include "s5p_mfc_ctrl.h" 28#include "s5p_mfc_ctrl.h"
@@ -29,11 +30,11 @@
29#include "s5p_mfc_dec.h" 30#include "s5p_mfc_dec.h"
30#include "s5p_mfc_enc.h" 31#include "s5p_mfc_enc.h"
31#include "s5p_mfc_intr.h" 32#include "s5p_mfc_intr.h"
33#include "s5p_mfc_iommu.h"
32#include "s5p_mfc_opr.h" 34#include "s5p_mfc_opr.h"
33#include "s5p_mfc_cmd.h" 35#include "s5p_mfc_cmd.h"
34#include "s5p_mfc_pm.h" 36#include "s5p_mfc_pm.h"
35 37
36#define S5P_MFC_NAME "s5p-mfc"
37#define S5P_MFC_DEC_NAME "s5p-mfc-dec" 38#define S5P_MFC_DEC_NAME "s5p-mfc-dec"
38#define S5P_MFC_ENC_NAME "s5p-mfc-enc" 39#define S5P_MFC_ENC_NAME "s5p-mfc-enc"
39 40
@@ -1043,55 +1044,94 @@ static const struct v4l2_file_operations s5p_mfc_fops = {
1043 .mmap = s5p_mfc_mmap, 1044 .mmap = s5p_mfc_mmap,
1044}; 1045};
1045 1046
1046static int match_child(struct device *dev, void *data) 1047/* DMA memory related helper functions */
1048static void s5p_mfc_memdev_release(struct device *dev)
1047{ 1049{
1048 if (!dev_name(dev)) 1050 of_reserved_mem_device_release(dev);
1049 return 0;
1050 return !strcmp(dev_name(dev), (char *)data);
1051} 1051}
1052 1052
1053static void *mfc_get_drv_data(struct platform_device *pdev); 1053static struct device *s5p_mfc_alloc_memdev(struct device *dev,
1054 1054 const char *name, unsigned int idx)
1055static int s5p_mfc_alloc_memdevs(struct s5p_mfc_dev *dev)
1056{ 1055{
1057 unsigned int mem_info[2] = { }; 1056 struct device *child;
1057 int ret;
1058 1058
1059 dev->mem_dev_l = devm_kzalloc(&dev->plat_dev->dev, 1059 child = devm_kzalloc(dev, sizeof(struct device), GFP_KERNEL);
1060 sizeof(struct device), GFP_KERNEL); 1060 if (!child)
1061 if (!dev->mem_dev_l) { 1061 return NULL;
1062 mfc_err("Not enough memory\n"); 1062
1063 return -ENOMEM; 1063 device_initialize(child);
1064 } 1064 dev_set_name(child, "%s:%s", dev_name(dev), name);
1065 device_initialize(dev->mem_dev_l); 1065 child->parent = dev;
1066 of_property_read_u32_array(dev->plat_dev->dev.of_node, 1066 child->bus = dev->bus;
1067 "samsung,mfc-l", mem_info, 2); 1067 child->coherent_dma_mask = dev->coherent_dma_mask;
1068 if (dma_declare_coherent_memory(dev->mem_dev_l, mem_info[0], 1068 child->dma_mask = dev->dma_mask;
1069 mem_info[0], mem_info[1], 1069 child->release = s5p_mfc_memdev_release;
1070 DMA_MEMORY_MAP | DMA_MEMORY_EXCLUSIVE) == 0) { 1070
1071 mfc_err("Failed to declare coherent memory for\n" 1071 if (device_add(child) == 0) {
1072 "MFC device\n"); 1072 ret = of_reserved_mem_device_init_by_idx(child, dev->of_node,
1073 return -ENOMEM; 1073 idx);
1074 if (ret == 0)
1075 return child;
1074 } 1076 }
1075 1077
1076 dev->mem_dev_r = devm_kzalloc(&dev->plat_dev->dev, 1078 put_device(child);
1077 sizeof(struct device), GFP_KERNEL); 1079 return NULL;
1078 if (!dev->mem_dev_r) { 1080}
1079 mfc_err("Not enough memory\n"); 1081
1080 return -ENOMEM; 1082static int s5p_mfc_configure_dma_memory(struct s5p_mfc_dev *mfc_dev)
1083{
1084 struct device *dev = &mfc_dev->plat_dev->dev;
1085
1086 /*
1087 * When IOMMU is available, we cannot use the default configuration,
1088 * because of MFC firmware requirements: address space limited to
1089 * 256M and non-zero default start address.
1090 * This is still simplified, not optimal configuration, but for now
1091 * IOMMU core doesn't allow to configure device's IOMMUs channel
1092 * separately.
1093 */
1094 if (exynos_is_iommu_available(dev)) {
1095 int ret = exynos_configure_iommu(dev, S5P_MFC_IOMMU_DMA_BASE,
1096 S5P_MFC_IOMMU_DMA_SIZE);
1097 if (ret == 0)
1098 mfc_dev->mem_dev_l = mfc_dev->mem_dev_r = dev;
1099 return ret;
1081 } 1100 }
1082 device_initialize(dev->mem_dev_r); 1101
1083 of_property_read_u32_array(dev->plat_dev->dev.of_node, 1102 /*
1084 "samsung,mfc-r", mem_info, 2); 1103 * Create and initialize virtual devices for accessing
1085 if (dma_declare_coherent_memory(dev->mem_dev_r, mem_info[0], 1104 * reserved memory regions.
1086 mem_info[0], mem_info[1], 1105 */
1087 DMA_MEMORY_MAP | DMA_MEMORY_EXCLUSIVE) == 0) { 1106 mfc_dev->mem_dev_l = s5p_mfc_alloc_memdev(dev, "left",
1088 pr_err("Failed to declare coherent memory for\n" 1107 MFC_BANK1_ALLOC_CTX);
1089 "MFC device\n"); 1108 if (!mfc_dev->mem_dev_l)
1090 return -ENOMEM; 1109 return -ENODEV;
1110 mfc_dev->mem_dev_r = s5p_mfc_alloc_memdev(dev, "right",
1111 MFC_BANK2_ALLOC_CTX);
1112 if (!mfc_dev->mem_dev_r) {
1113 device_unregister(mfc_dev->mem_dev_l);
1114 return -ENODEV;
1091 } 1115 }
1116
1092 return 0; 1117 return 0;
1093} 1118}
1094 1119
1120static void s5p_mfc_unconfigure_dma_memory(struct s5p_mfc_dev *mfc_dev)
1121{
1122 struct device *dev = &mfc_dev->plat_dev->dev;
1123
1124 if (exynos_is_iommu_available(dev)) {
1125 exynos_unconfigure_iommu(dev);
1126 return;
1127 }
1128
1129 device_unregister(mfc_dev->mem_dev_l);
1130 device_unregister(mfc_dev->mem_dev_r);
1131}
1132
1133static void *mfc_get_drv_data(struct platform_device *pdev);
1134
1095/* MFC probe function */ 1135/* MFC probe function */
1096static int s5p_mfc_probe(struct platform_device *pdev) 1136static int s5p_mfc_probe(struct platform_device *pdev)
1097{ 1137{
@@ -1117,14 +1157,11 @@ static int s5p_mfc_probe(struct platform_device *pdev)
1117 1157
1118 dev->variant = mfc_get_drv_data(pdev); 1158 dev->variant = mfc_get_drv_data(pdev);
1119 1159
1120 ret = s5p_mfc_init_pm(dev);
1121 if (ret < 0) {
1122 dev_err(&pdev->dev, "failed to get mfc clock source\n");
1123 return ret;
1124 }
1125
1126 res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 1160 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1127 1161 if (res == NULL) {
1162 dev_err(&pdev->dev, "failed to get io resource\n");
1163 return -ENOENT;
1164 }
1128 dev->regs_base = devm_ioremap_resource(&pdev->dev, res); 1165 dev->regs_base = devm_ioremap_resource(&pdev->dev, res);
1129 if (IS_ERR(dev->regs_base)) 1166 if (IS_ERR(dev->regs_base))
1130 return PTR_ERR(dev->regs_base); 1167 return PTR_ERR(dev->regs_base);
@@ -1132,54 +1169,36 @@ static int s5p_mfc_probe(struct platform_device *pdev)
1132 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0); 1169 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1133 if (res == NULL) { 1170 if (res == NULL) {
1134 dev_err(&pdev->dev, "failed to get irq resource\n"); 1171 dev_err(&pdev->dev, "failed to get irq resource\n");
1135 ret = -ENOENT; 1172 return -ENOENT;
1136 goto err_res;
1137 } 1173 }
1138 dev->irq = res->start; 1174 dev->irq = res->start;
1139 ret = devm_request_irq(&pdev->dev, dev->irq, s5p_mfc_irq, 1175 ret = devm_request_irq(&pdev->dev, dev->irq, s5p_mfc_irq,
1140 0, pdev->name, dev); 1176 0, pdev->name, dev);
1141 if (ret) { 1177 if (ret) {
1142 dev_err(&pdev->dev, "Failed to install irq (%d)\n", ret); 1178 dev_err(&pdev->dev, "Failed to install irq (%d)\n", ret);
1143 goto err_res; 1179 return ret;
1144 } 1180 }
1145 1181
1146 if (pdev->dev.of_node) { 1182 ret = s5p_mfc_configure_dma_memory(dev);
1147 ret = s5p_mfc_alloc_memdevs(dev); 1183 if (ret < 0) {
1148 if (ret < 0) 1184 dev_err(&pdev->dev, "failed to configure DMA memory\n");
1149 goto err_res; 1185 return ret;
1150 } else {
1151 dev->mem_dev_l = device_find_child(&dev->plat_dev->dev,
1152 "s5p-mfc-l", match_child);
1153 if (!dev->mem_dev_l) {
1154 mfc_err("Mem child (L) device get failed\n");
1155 ret = -ENODEV;
1156 goto err_res;
1157 }
1158 dev->mem_dev_r = device_find_child(&dev->plat_dev->dev,
1159 "s5p-mfc-r", match_child);
1160 if (!dev->mem_dev_r) {
1161 mfc_err("Mem child (R) device get failed\n");
1162 ret = -ENODEV;
1163 goto err_res;
1164 }
1165 } 1186 }
1166 1187
1167 dev->alloc_ctx[0] = vb2_dma_contig_init_ctx(dev->mem_dev_l); 1188 ret = s5p_mfc_init_pm(dev);
1168 if (IS_ERR(dev->alloc_ctx[0])) { 1189 if (ret < 0) {
1169 ret = PTR_ERR(dev->alloc_ctx[0]); 1190 dev_err(&pdev->dev, "failed to get mfc clock source\n");
1170 goto err_res; 1191 goto err_dma;
1171 }
1172 dev->alloc_ctx[1] = vb2_dma_contig_init_ctx(dev->mem_dev_r);
1173 if (IS_ERR(dev->alloc_ctx[1])) {
1174 ret = PTR_ERR(dev->alloc_ctx[1]);
1175 goto err_mem_init_ctx_1;
1176 } 1192 }
1177 1193
1194 vb2_dma_contig_set_max_seg_size(dev->mem_dev_l, DMA_BIT_MASK(32));
1195 vb2_dma_contig_set_max_seg_size(dev->mem_dev_r, DMA_BIT_MASK(32));
1196
1178 mutex_init(&dev->mfc_mutex); 1197 mutex_init(&dev->mfc_mutex);
1179 1198
1180 ret = s5p_mfc_alloc_firmware(dev); 1199 ret = s5p_mfc_alloc_firmware(dev);
1181 if (ret) 1200 if (ret)
1182 goto err_alloc_fw; 1201 goto err_res;
1183 1202
1184 ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev); 1203 ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
1185 if (ret) 1204 if (ret)
@@ -1201,14 +1220,6 @@ static int s5p_mfc_probe(struct platform_device *pdev)
1201 vfd->vfl_dir = VFL_DIR_M2M; 1220 vfd->vfl_dir = VFL_DIR_M2M;
1202 snprintf(vfd->name, sizeof(vfd->name), "%s", S5P_MFC_DEC_NAME); 1221 snprintf(vfd->name, sizeof(vfd->name), "%s", S5P_MFC_DEC_NAME);
1203 dev->vfd_dec = vfd; 1222 dev->vfd_dec = vfd;
1204 ret = video_register_device(vfd, VFL_TYPE_GRABBER, 0);
1205 if (ret) {
1206 v4l2_err(&dev->v4l2_dev, "Failed to register video device\n");
1207 video_device_release(vfd);
1208 goto err_dec_reg;
1209 }
1210 v4l2_info(&dev->v4l2_dev,
1211 "decoder registered as /dev/video%d\n", vfd->num);
1212 video_set_drvdata(vfd, dev); 1223 video_set_drvdata(vfd, dev);
1213 1224
1214 /* encoder */ 1225 /* encoder */
@@ -1226,14 +1237,6 @@ static int s5p_mfc_probe(struct platform_device *pdev)
1226 vfd->vfl_dir = VFL_DIR_M2M; 1237 vfd->vfl_dir = VFL_DIR_M2M;
1227 snprintf(vfd->name, sizeof(vfd->name), "%s", S5P_MFC_ENC_NAME); 1238 snprintf(vfd->name, sizeof(vfd->name), "%s", S5P_MFC_ENC_NAME);
1228 dev->vfd_enc = vfd; 1239 dev->vfd_enc = vfd;
1229 ret = video_register_device(vfd, VFL_TYPE_GRABBER, 0);
1230 if (ret) {
1231 v4l2_err(&dev->v4l2_dev, "Failed to register video device\n");
1232 video_device_release(vfd);
1233 goto err_enc_reg;
1234 }
1235 v4l2_info(&dev->v4l2_dev,
1236 "encoder registered as /dev/video%d\n", vfd->num);
1237 video_set_drvdata(vfd, dev); 1240 video_set_drvdata(vfd, dev);
1238 platform_set_drvdata(pdev, dev); 1241 platform_set_drvdata(pdev, dev);
1239 1242
@@ -1250,26 +1253,41 @@ static int s5p_mfc_probe(struct platform_device *pdev)
1250 s5p_mfc_init_hw_cmds(dev); 1253 s5p_mfc_init_hw_cmds(dev);
1251 s5p_mfc_init_regs(dev); 1254 s5p_mfc_init_regs(dev);
1252 1255
1256 /* Register decoder and encoder */
1257 ret = video_register_device(dev->vfd_dec, VFL_TYPE_GRABBER, 0);
1258 if (ret) {
1259 v4l2_err(&dev->v4l2_dev, "Failed to register video device\n");
1260 goto err_dec_reg;
1261 }
1262 v4l2_info(&dev->v4l2_dev,
1263 "decoder registered as /dev/video%d\n", dev->vfd_dec->num);
1264
1265 ret = video_register_device(dev->vfd_enc, VFL_TYPE_GRABBER, 0);
1266 if (ret) {
1267 v4l2_err(&dev->v4l2_dev, "Failed to register video device\n");
1268 goto err_enc_reg;
1269 }
1270 v4l2_info(&dev->v4l2_dev,
1271 "encoder registered as /dev/video%d\n", dev->vfd_enc->num);
1272
1253 pr_debug("%s--\n", __func__); 1273 pr_debug("%s--\n", __func__);
1254 return 0; 1274 return 0;
1255 1275
1256/* Deinit MFC if probe had failed */ 1276/* Deinit MFC if probe had failed */
1257err_enc_reg: 1277err_enc_reg:
1258 video_device_release(dev->vfd_enc);
1259err_enc_alloc:
1260 video_unregister_device(dev->vfd_dec); 1278 video_unregister_device(dev->vfd_dec);
1261err_dec_reg: 1279err_dec_reg:
1280 video_device_release(dev->vfd_enc);
1281err_enc_alloc:
1262 video_device_release(dev->vfd_dec); 1282 video_device_release(dev->vfd_dec);
1263err_dec_alloc: 1283err_dec_alloc:
1264 v4l2_device_unregister(&dev->v4l2_dev); 1284 v4l2_device_unregister(&dev->v4l2_dev);
1265err_v4l2_dev_reg: 1285err_v4l2_dev_reg:
1266 s5p_mfc_release_firmware(dev); 1286 s5p_mfc_release_firmware(dev);
1267err_alloc_fw:
1268 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx[1]);
1269err_mem_init_ctx_1:
1270 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx[0]);
1271err_res: 1287err_res:
1272 s5p_mfc_final_pm(dev); 1288 s5p_mfc_final_pm(dev);
1289err_dma:
1290 s5p_mfc_unconfigure_dma_memory(dev);
1273 1291
1274 pr_debug("%s-- with error\n", __func__); 1292 pr_debug("%s-- with error\n", __func__);
1275 return ret; 1293 return ret;
@@ -1289,14 +1307,13 @@ static int s5p_mfc_remove(struct platform_device *pdev)
1289 1307
1290 video_unregister_device(dev->vfd_enc); 1308 video_unregister_device(dev->vfd_enc);
1291 video_unregister_device(dev->vfd_dec); 1309 video_unregister_device(dev->vfd_dec);
1310 video_device_release(dev->vfd_enc);
1311 video_device_release(dev->vfd_dec);
1292 v4l2_device_unregister(&dev->v4l2_dev); 1312 v4l2_device_unregister(&dev->v4l2_dev);
1293 s5p_mfc_release_firmware(dev); 1313 s5p_mfc_release_firmware(dev);
1294 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx[0]); 1314 s5p_mfc_unconfigure_dma_memory(dev);
1295 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx[1]); 1315 vb2_dma_contig_clear_max_seg_size(dev->mem_dev_l);
1296 if (pdev->dev.of_node) { 1316 vb2_dma_contig_clear_max_seg_size(dev->mem_dev_r);
1297 put_device(dev->mem_dev_l);
1298 put_device(dev->mem_dev_r);
1299 }
1300 1317
1301 s5p_mfc_final_pm(dev); 1318 s5p_mfc_final_pm(dev);
1302 return 0; 1319 return 0;
diff --git a/drivers/media/platform/s5p-mfc/s5p_mfc_common.h b/drivers/media/platform/s5p-mfc/s5p_mfc_common.h
index 9eb2481ec292..373e346fce3e 100644
--- a/drivers/media/platform/s5p-mfc/s5p_mfc_common.h
+++ b/drivers/media/platform/s5p-mfc/s5p_mfc_common.h
@@ -25,6 +25,8 @@
25#include "regs-mfc.h" 25#include "regs-mfc.h"
26#include "regs-mfc-v8.h" 26#include "regs-mfc-v8.h"
27 27
28#define S5P_MFC_NAME "s5p-mfc"
29
28/* Definitions related to MFC memory */ 30/* Definitions related to MFC memory */
29 31
30/* Offset base used to differentiate between CAPTURE and OUTPUT 32/* Offset base used to differentiate between CAPTURE and OUTPUT
@@ -285,7 +287,6 @@ struct s5p_mfc_priv_buf {
285 * @watchdog_cnt: counter for the watchdog 287 * @watchdog_cnt: counter for the watchdog
286 * @watchdog_workqueue: workqueue for the watchdog 288 * @watchdog_workqueue: workqueue for the watchdog
287 * @watchdog_work: worker for the watchdog 289 * @watchdog_work: worker for the watchdog
288 * @alloc_ctx: videobuf2 allocator contexts for two memory banks
289 * @enter_suspend: flag set when entering suspend 290 * @enter_suspend: flag set when entering suspend
290 * @ctx_buf: common context memory (MFCv6) 291 * @ctx_buf: common context memory (MFCv6)
291 * @warn_start: hardware error code from which warnings start 292 * @warn_start: hardware error code from which warnings start
@@ -328,7 +329,6 @@ struct s5p_mfc_dev {
328 struct timer_list watchdog_timer; 329 struct timer_list watchdog_timer;
329 struct workqueue_struct *watchdog_workqueue; 330 struct workqueue_struct *watchdog_workqueue;
330 struct work_struct watchdog_work; 331 struct work_struct watchdog_work;
331 void *alloc_ctx[2];
332 unsigned long enter_suspend; 332 unsigned long enter_suspend;
333 333
334 struct s5p_mfc_priv_buf ctx_buf; 334 struct s5p_mfc_priv_buf ctx_buf;
diff --git a/drivers/media/platform/s5p-mfc/s5p_mfc_dec.c b/drivers/media/platform/s5p-mfc/s5p_mfc_dec.c
index f2d6376ce618..47c997d9e8cb 100644
--- a/drivers/media/platform/s5p-mfc/s5p_mfc_dec.c
+++ b/drivers/media/platform/s5p-mfc/s5p_mfc_dec.c
@@ -265,9 +265,10 @@ static int vidioc_querycap(struct file *file, void *priv,
265{ 265{
266 struct s5p_mfc_dev *dev = video_drvdata(file); 266 struct s5p_mfc_dev *dev = video_drvdata(file);
267 267
268 strncpy(cap->driver, dev->plat_dev->name, sizeof(cap->driver) - 1); 268 strncpy(cap->driver, S5P_MFC_NAME, sizeof(cap->driver) - 1);
269 strncpy(cap->card, dev->plat_dev->name, sizeof(cap->card) - 1); 269 strncpy(cap->card, dev->vfd_dec->name, sizeof(cap->card) - 1);
270 cap->bus_info[0] = 0; 270 snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
271 dev_name(&dev->plat_dev->dev));
271 /* 272 /*
272 * This is only a mem-to-mem video device. The capture and output 273 * This is only a mem-to-mem video device. The capture and output
273 * device capability flags are left only for backward compatibility 274 * device capability flags are left only for backward compatibility
@@ -423,7 +424,7 @@ static int vidioc_s_fmt(struct file *file, void *priv, struct v4l2_format *f)
423 pix_mp = &f->fmt.pix_mp; 424 pix_mp = &f->fmt.pix_mp;
424 if (ret) 425 if (ret)
425 return ret; 426 return ret;
426 if (ctx->vq_src.streaming || ctx->vq_dst.streaming) { 427 if (vb2_is_streaming(&ctx->vq_src) || vb2_is_streaming(&ctx->vq_dst)) {
427 v4l2_err(&dev->v4l2_dev, "%s queue busy\n", __func__); 428 v4l2_err(&dev->v4l2_dev, "%s queue busy\n", __func__);
428 ret = -EBUSY; 429 ret = -EBUSY;
429 goto out; 430 goto out;
@@ -474,7 +475,6 @@ static int reqbufs_output(struct s5p_mfc_dev *dev, struct s5p_mfc_ctx *ctx,
474 ret = vb2_reqbufs(&ctx->vq_src, reqbufs); 475 ret = vb2_reqbufs(&ctx->vq_src, reqbufs);
475 if (ret) 476 if (ret)
476 goto out; 477 goto out;
477 s5p_mfc_close_mfc_inst(dev, ctx);
478 ctx->src_bufs_cnt = 0; 478 ctx->src_bufs_cnt = 0;
479 ctx->output_state = QUEUE_FREE; 479 ctx->output_state = QUEUE_FREE;
480 } else if (ctx->output_state == QUEUE_FREE) { 480 } else if (ctx->output_state == QUEUE_FREE) {
@@ -565,7 +565,7 @@ out:
565 return ret; 565 return ret;
566} 566}
567 567
568/* Reqeust buffers */ 568/* Request buffers */
569static int vidioc_reqbufs(struct file *file, void *priv, 569static int vidioc_reqbufs(struct file *file, void *priv,
570 struct v4l2_requestbuffers *reqbufs) 570 struct v4l2_requestbuffers *reqbufs)
571{ 571{
@@ -573,7 +573,7 @@ static int vidioc_reqbufs(struct file *file, void *priv,
573 struct s5p_mfc_ctx *ctx = fh_to_ctx(priv); 573 struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
574 574
575 if (reqbufs->memory != V4L2_MEMORY_MMAP) { 575 if (reqbufs->memory != V4L2_MEMORY_MMAP) {
576 mfc_err("Only V4L2_MEMORY_MAP is supported\n"); 576 mfc_debug(2, "Only V4L2_MEMORY_MMAP is supported\n");
577 return -EINVAL; 577 return -EINVAL;
578 } 578 }
579 579
@@ -821,7 +821,7 @@ static int vidioc_decoder_cmd(struct file *file, void *priv,
821 if (cmd->flags != 0) 821 if (cmd->flags != 0)
822 return -EINVAL; 822 return -EINVAL;
823 823
824 if (!ctx->vq_src.streaming) 824 if (!vb2_is_streaming(&ctx->vq_src))
825 return -EINVAL; 825 return -EINVAL;
826 826
827 spin_lock_irqsave(&dev->irqlock, flags); 827 spin_lock_irqsave(&dev->irqlock, flags);
@@ -890,7 +890,7 @@ static const struct v4l2_ioctl_ops s5p_mfc_dec_ioctl_ops = {
890static int s5p_mfc_queue_setup(struct vb2_queue *vq, 890static int s5p_mfc_queue_setup(struct vb2_queue *vq,
891 unsigned int *buf_count, 891 unsigned int *buf_count,
892 unsigned int *plane_count, unsigned int psize[], 892 unsigned int *plane_count, unsigned int psize[],
893 void *allocators[]) 893 struct device *alloc_devs[])
894{ 894{
895 struct s5p_mfc_ctx *ctx = fh_to_ctx(vq->drv_priv); 895 struct s5p_mfc_ctx *ctx = fh_to_ctx(vq->drv_priv);
896 struct s5p_mfc_dev *dev = ctx->dev; 896 struct s5p_mfc_dev *dev = ctx->dev;
@@ -931,16 +931,14 @@ static int s5p_mfc_queue_setup(struct vb2_queue *vq,
931 psize[1] = ctx->chroma_size; 931 psize[1] = ctx->chroma_size;
932 932
933 if (IS_MFCV6_PLUS(dev)) 933 if (IS_MFCV6_PLUS(dev))
934 allocators[0] = 934 alloc_devs[0] = ctx->dev->mem_dev_l;
935 ctx->dev->alloc_ctx[MFC_BANK1_ALLOC_CTX];
936 else 935 else
937 allocators[0] = 936 alloc_devs[0] = ctx->dev->mem_dev_r;
938 ctx->dev->alloc_ctx[MFC_BANK2_ALLOC_CTX]; 937 alloc_devs[1] = ctx->dev->mem_dev_l;
939 allocators[1] = ctx->dev->alloc_ctx[MFC_BANK1_ALLOC_CTX];
940 } else if (vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE && 938 } else if (vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE &&
941 ctx->state == MFCINST_INIT) { 939 ctx->state == MFCINST_INIT) {
942 psize[0] = ctx->dec_src_buf_size; 940 psize[0] = ctx->dec_src_buf_size;
943 allocators[0] = ctx->dev->alloc_ctx[MFC_BANK1_ALLOC_CTX]; 941 alloc_devs[0] = ctx->dev->mem_dev_l;
944 } else { 942 } else {
945 mfc_err("This video node is dedicated to decoding. Decoding not initialized\n"); 943 mfc_err("This video node is dedicated to decoding. Decoding not initialized\n");
946 return -EINVAL; 944 return -EINVAL;
diff --git a/drivers/media/platform/s5p-mfc/s5p_mfc_enc.c b/drivers/media/platform/s5p-mfc/s5p_mfc_enc.c
index 034b5c1d35a1..fcc2e054c61f 100644
--- a/drivers/media/platform/s5p-mfc/s5p_mfc_enc.c
+++ b/drivers/media/platform/s5p-mfc/s5p_mfc_enc.c
@@ -943,9 +943,10 @@ static int vidioc_querycap(struct file *file, void *priv,
943{ 943{
944 struct s5p_mfc_dev *dev = video_drvdata(file); 944 struct s5p_mfc_dev *dev = video_drvdata(file);
945 945
946 strncpy(cap->driver, dev->plat_dev->name, sizeof(cap->driver) - 1); 946 strncpy(cap->driver, S5P_MFC_NAME, sizeof(cap->driver) - 1);
947 strncpy(cap->card, dev->plat_dev->name, sizeof(cap->card) - 1); 947 strncpy(cap->card, dev->vfd_enc->name, sizeof(cap->card) - 1);
948 cap->bus_info[0] = 0; 948 snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
949 dev_name(&dev->plat_dev->dev));
949 /* 950 /*
950 * This is only a mem-to-mem video device. The capture and output 951 * This is only a mem-to-mem video device. The capture and output
951 * device capability flags are left only for backward compatibility 952 * device capability flags are left only for backward compatibility
@@ -1043,10 +1044,6 @@ static int vidioc_try_fmt(struct file *file, void *priv, struct v4l2_format *f)
1043 mfc_err("failed to try output format\n"); 1044 mfc_err("failed to try output format\n");
1044 return -EINVAL; 1045 return -EINVAL;
1045 } 1046 }
1046 if (pix_fmt_mp->plane_fmt[0].sizeimage == 0) {
1047 mfc_err("must be set encoding output size\n");
1048 return -EINVAL;
1049 }
1050 if ((dev->variant->version_bit & fmt->versions) == 0) { 1047 if ((dev->variant->version_bit & fmt->versions) == 0) {
1051 mfc_err("Unsupported format by this MFC version.\n"); 1048 mfc_err("Unsupported format by this MFC version.\n");
1052 return -EINVAL; 1049 return -EINVAL;
@@ -1060,11 +1057,6 @@ static int vidioc_try_fmt(struct file *file, void *priv, struct v4l2_format *f)
1060 mfc_err("failed to try output format\n"); 1057 mfc_err("failed to try output format\n");
1061 return -EINVAL; 1058 return -EINVAL;
1062 } 1059 }
1063
1064 if (fmt->num_planes != pix_fmt_mp->num_planes) {
1065 mfc_err("failed to try output format\n");
1066 return -EINVAL;
1067 }
1068 if ((dev->variant->version_bit & fmt->versions) == 0) { 1060 if ((dev->variant->version_bit & fmt->versions) == 0) {
1069 mfc_err("Unsupported format by this MFC version.\n"); 1061 mfc_err("Unsupported format by this MFC version.\n");
1070 return -EINVAL; 1062 return -EINVAL;
@@ -1144,7 +1136,10 @@ static int vidioc_reqbufs(struct file *file, void *priv,
1144 return -EINVAL; 1136 return -EINVAL;
1145 if (reqbufs->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) { 1137 if (reqbufs->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1146 if (reqbufs->count == 0) { 1138 if (reqbufs->count == 0) {
1139 mfc_debug(2, "Freeing buffers\n");
1147 ret = vb2_reqbufs(&ctx->vq_dst, reqbufs); 1140 ret = vb2_reqbufs(&ctx->vq_dst, reqbufs);
1141 s5p_mfc_hw_call(dev->mfc_ops, release_codec_buffers,
1142 ctx);
1148 ctx->capture_state = QUEUE_FREE; 1143 ctx->capture_state = QUEUE_FREE;
1149 return ret; 1144 return ret;
1150 } 1145 }
@@ -1817,7 +1812,7 @@ static int check_vb_with_fmt(struct s5p_mfc_fmt *fmt, struct vb2_buffer *vb)
1817 1812
1818static int s5p_mfc_queue_setup(struct vb2_queue *vq, 1813static int s5p_mfc_queue_setup(struct vb2_queue *vq,
1819 unsigned int *buf_count, unsigned int *plane_count, 1814 unsigned int *buf_count, unsigned int *plane_count,
1820 unsigned int psize[], void *allocators[]) 1815 unsigned int psize[], struct device *alloc_devs[])
1821{ 1816{
1822 struct s5p_mfc_ctx *ctx = fh_to_ctx(vq->drv_priv); 1817 struct s5p_mfc_ctx *ctx = fh_to_ctx(vq->drv_priv);
1823 struct s5p_mfc_dev *dev = ctx->dev; 1818 struct s5p_mfc_dev *dev = ctx->dev;
@@ -1837,7 +1832,7 @@ static int s5p_mfc_queue_setup(struct vb2_queue *vq,
1837 if (*buf_count > MFC_MAX_BUFFERS) 1832 if (*buf_count > MFC_MAX_BUFFERS)
1838 *buf_count = MFC_MAX_BUFFERS; 1833 *buf_count = MFC_MAX_BUFFERS;
1839 psize[0] = ctx->enc_dst_buf_size; 1834 psize[0] = ctx->enc_dst_buf_size;
1840 allocators[0] = ctx->dev->alloc_ctx[MFC_BANK1_ALLOC_CTX]; 1835 alloc_devs[0] = ctx->dev->mem_dev_l;
1841 } else if (vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) { 1836 } else if (vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1842 if (ctx->src_fmt) 1837 if (ctx->src_fmt)
1843 *plane_count = ctx->src_fmt->num_planes; 1838 *plane_count = ctx->src_fmt->num_planes;
@@ -1853,15 +1848,11 @@ static int s5p_mfc_queue_setup(struct vb2_queue *vq,
1853 psize[1] = ctx->chroma_size; 1848 psize[1] = ctx->chroma_size;
1854 1849
1855 if (IS_MFCV6_PLUS(dev)) { 1850 if (IS_MFCV6_PLUS(dev)) {
1856 allocators[0] = 1851 alloc_devs[0] = ctx->dev->mem_dev_l;
1857 ctx->dev->alloc_ctx[MFC_BANK1_ALLOC_CTX]; 1852 alloc_devs[1] = ctx->dev->mem_dev_l;
1858 allocators[1] =
1859 ctx->dev->alloc_ctx[MFC_BANK1_ALLOC_CTX];
1860 } else { 1853 } else {
1861 allocators[0] = 1854 alloc_devs[0] = ctx->dev->mem_dev_r;
1862 ctx->dev->alloc_ctx[MFC_BANK2_ALLOC_CTX]; 1855 alloc_devs[1] = ctx->dev->mem_dev_r;
1863 allocators[1] =
1864 ctx->dev->alloc_ctx[MFC_BANK2_ALLOC_CTX];
1865 } 1856 }
1866 } else { 1857 } else {
1867 mfc_err("invalid queue type: %d\n", vq->type); 1858 mfc_err("invalid queue type: %d\n", vq->type);
diff --git a/drivers/media/platform/s5p-mfc/s5p_mfc_iommu.h b/drivers/media/platform/s5p-mfc/s5p_mfc_iommu.h
new file mode 100644
index 000000000000..6962132ae8fa
--- /dev/null
+++ b/drivers/media/platform/s5p-mfc/s5p_mfc_iommu.h
@@ -0,0 +1,79 @@
1/*
2 * Copyright (C) 2015 Samsung Electronics Co.Ltd
3 * Authors: Marek Szyprowski <m.szyprowski@samsung.com>
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version.
9 */
10
11#ifndef S5P_MFC_IOMMU_H_
12#define S5P_MFC_IOMMU_H_
13
14#define S5P_MFC_IOMMU_DMA_BASE 0x20000000lu
15#define S5P_MFC_IOMMU_DMA_SIZE SZ_256M
16
17#if defined(CONFIG_EXYNOS_IOMMU) && defined(CONFIG_ARM_DMA_USE_IOMMU)
18
19#include <asm/dma-iommu.h>
20
21static inline bool exynos_is_iommu_available(struct device *dev)
22{
23 return dev->archdata.iommu != NULL;
24}
25
26static inline void exynos_unconfigure_iommu(struct device *dev)
27{
28 struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
29
30 arm_iommu_detach_device(dev);
31 arm_iommu_release_mapping(mapping);
32}
33
34static inline int exynos_configure_iommu(struct device *dev,
35 unsigned int base, unsigned int size)
36{
37 struct dma_iommu_mapping *mapping = NULL;
38 int ret;
39
40 /* Disable the default mapping created by device core */
41 if (to_dma_iommu_mapping(dev))
42 exynos_unconfigure_iommu(dev);
43
44 mapping = arm_iommu_create_mapping(dev->bus, base, size);
45 if (IS_ERR(mapping)) {
46 pr_warn("Failed to create IOMMU mapping for device %s\n",
47 dev_name(dev));
48 return PTR_ERR(mapping);
49 }
50
51 ret = arm_iommu_attach_device(dev, mapping);
52 if (ret) {
53 pr_warn("Failed to attached device %s to IOMMU_mapping\n",
54 dev_name(dev));
55 arm_iommu_release_mapping(mapping);
56 return ret;
57 }
58
59 return 0;
60}
61
62#else
63
64static inline bool exynos_is_iommu_available(struct device *dev)
65{
66 return false;
67}
68
69static inline int exynos_configure_iommu(struct device *dev,
70 unsigned int base, unsigned int size)
71{
72 return -ENOSYS;
73}
74
75static inline void exynos_unconfigure_iommu(struct device *dev) { }
76
77#endif
78
79#endif /* S5P_MFC_IOMMU_H_ */
diff --git a/drivers/media/platform/s5p-mfc/s5p_mfc_pm.c b/drivers/media/platform/s5p-mfc/s5p_mfc_pm.c
index 5f97a3398c11..930dc2dddae6 100644
--- a/drivers/media/platform/s5p-mfc/s5p_mfc_pm.c
+++ b/drivers/media/platform/s5p-mfc/s5p_mfc_pm.c
@@ -54,6 +54,7 @@ int s5p_mfc_init_pm(struct s5p_mfc_dev *dev)
54 pm->clock = clk_get(&dev->plat_dev->dev, MFC_SCLK_NAME); 54 pm->clock = clk_get(&dev->plat_dev->dev, MFC_SCLK_NAME);
55 if (IS_ERR(pm->clock)) { 55 if (IS_ERR(pm->clock)) {
56 mfc_info("Failed to get MFC special clock control\n"); 56 mfc_info("Failed to get MFC special clock control\n");
57 pm->clock = NULL;
57 } else { 58 } else {
58 clk_set_rate(pm->clock, MFC_SCLK_RATE); 59 clk_set_rate(pm->clock, MFC_SCLK_RATE);
59 ret = clk_prepare_enable(pm->clock); 60 ret = clk_prepare_enable(pm->clock);
@@ -76,8 +77,10 @@ int s5p_mfc_init_pm(struct s5p_mfc_dev *dev)
76 77
77err_s_clk: 78err_s_clk:
78 clk_put(pm->clock); 79 clk_put(pm->clock);
80 pm->clock = NULL;
79err_p_ip_clk: 81err_p_ip_clk:
80 clk_put(pm->clock_gate); 82 clk_put(pm->clock_gate);
83 pm->clock_gate = NULL;
81err_g_ip_clk: 84err_g_ip_clk:
82 return ret; 85 return ret;
83} 86}
@@ -88,9 +91,11 @@ void s5p_mfc_final_pm(struct s5p_mfc_dev *dev)
88 pm->clock) { 91 pm->clock) {
89 clk_disable_unprepare(pm->clock); 92 clk_disable_unprepare(pm->clock);
90 clk_put(pm->clock); 93 clk_put(pm->clock);
94 pm->clock = NULL;
91 } 95 }
92 clk_unprepare(pm->clock_gate); 96 clk_unprepare(pm->clock_gate);
93 clk_put(pm->clock_gate); 97 clk_put(pm->clock_gate);
98 pm->clock_gate = NULL;
94#ifdef CONFIG_PM 99#ifdef CONFIG_PM
95 pm_runtime_disable(pm->device); 100 pm_runtime_disable(pm->device);
96#endif 101#endif
@@ -98,12 +103,13 @@ void s5p_mfc_final_pm(struct s5p_mfc_dev *dev)
98 103
99int s5p_mfc_clock_on(void) 104int s5p_mfc_clock_on(void)
100{ 105{
101 int ret; 106 int ret = 0;
102#ifdef CLK_DEBUG 107#ifdef CLK_DEBUG
103 atomic_inc(&clk_ref); 108 atomic_inc(&clk_ref);
104 mfc_debug(3, "+ %d\n", atomic_read(&clk_ref)); 109 mfc_debug(3, "+ %d\n", atomic_read(&clk_ref));
105#endif 110#endif
106 ret = clk_enable(pm->clock_gate); 111 if (!IS_ERR_OR_NULL(pm->clock_gate))
112 ret = clk_enable(pm->clock_gate);
107 return ret; 113 return ret;
108} 114}
109 115
@@ -113,7 +119,8 @@ void s5p_mfc_clock_off(void)
113 atomic_dec(&clk_ref); 119 atomic_dec(&clk_ref);
114 mfc_debug(3, "- %d\n", atomic_read(&clk_ref)); 120 mfc_debug(3, "- %d\n", atomic_read(&clk_ref));
115#endif 121#endif
116 clk_disable(pm->clock_gate); 122 if (!IS_ERR_OR_NULL(pm->clock_gate))
123 clk_disable(pm->clock_gate);
117} 124}
118 125
119int s5p_mfc_power_on(void) 126int s5p_mfc_power_on(void)
diff --git a/drivers/media/platform/s5p-tv/mixer.h b/drivers/media/platform/s5p-tv/mixer.h
index 4dd62a918fcf..869f0ce86f6e 100644
--- a/drivers/media/platform/s5p-tv/mixer.h
+++ b/drivers/media/platform/s5p-tv/mixer.h
@@ -245,8 +245,6 @@ struct mxr_device {
245 245
246 /** V4L2 device */ 246 /** V4L2 device */
247 struct v4l2_device v4l2_dev; 247 struct v4l2_device v4l2_dev;
248 /** context of allocator */
249 void *alloc_ctx;
250 /** event wait queue */ 248 /** event wait queue */
251 wait_queue_head_t event_queue; 249 wait_queue_head_t event_queue;
252 /** state flags */ 250 /** state flags */
diff --git a/drivers/media/platform/s5p-tv/mixer_video.c b/drivers/media/platform/s5p-tv/mixer_video.c
index 7ab5578a0405..ee74e2b44d69 100644
--- a/drivers/media/platform/s5p-tv/mixer_video.c
+++ b/drivers/media/platform/s5p-tv/mixer_video.c
@@ -80,12 +80,7 @@ int mxr_acquire_video(struct mxr_device *mdev,
80 goto fail; 80 goto fail;
81 } 81 }
82 82
83 mdev->alloc_ctx = vb2_dma_contig_init_ctx(mdev->dev); 83 vb2_dma_contig_set_max_seg_size(mdev->dev, DMA_BIT_MASK(32));
84 if (IS_ERR(mdev->alloc_ctx)) {
85 mxr_err(mdev, "could not acquire vb2 allocator\n");
86 ret = PTR_ERR(mdev->alloc_ctx);
87 goto fail_v4l2_dev;
88 }
89 84
90 /* registering outputs */ 85 /* registering outputs */
91 mdev->output_cnt = 0; 86 mdev->output_cnt = 0;
@@ -120,7 +115,7 @@ int mxr_acquire_video(struct mxr_device *mdev,
120 mxr_err(mdev, "failed to register any output\n"); 115 mxr_err(mdev, "failed to register any output\n");
121 ret = -ENODEV; 116 ret = -ENODEV;
122 /* skipping fail_output because there is nothing to free */ 117 /* skipping fail_output because there is nothing to free */
123 goto fail_vb2_allocator; 118 goto fail_v4l2_dev;
124 } 119 }
125 120
126 return 0; 121 return 0;
@@ -131,10 +126,6 @@ fail_output:
131 kfree(mdev->output[i]); 126 kfree(mdev->output[i]);
132 memset(mdev->output, 0, sizeof(mdev->output)); 127 memset(mdev->output, 0, sizeof(mdev->output));
133 128
134fail_vb2_allocator:
135 /* freeing allocator context */
136 vb2_dma_contig_cleanup_ctx(mdev->alloc_ctx);
137
138fail_v4l2_dev: 129fail_v4l2_dev:
139 /* NOTE: automatically unregister all subdevs */ 130 /* NOTE: automatically unregister all subdevs */
140 v4l2_device_unregister(v4l2_dev); 131 v4l2_device_unregister(v4l2_dev);
@@ -151,7 +142,7 @@ void mxr_release_video(struct mxr_device *mdev)
151 for (i = 0; i < mdev->output_cnt; ++i) 142 for (i = 0; i < mdev->output_cnt; ++i)
152 kfree(mdev->output[i]); 143 kfree(mdev->output[i]);
153 144
154 vb2_dma_contig_cleanup_ctx(mdev->alloc_ctx); 145 vb2_dma_contig_clear_max_seg_size(mdev->dev);
155 v4l2_device_unregister(&mdev->v4l2_dev); 146 v4l2_device_unregister(&mdev->v4l2_dev);
156} 147}
157 148
@@ -883,7 +874,7 @@ static const struct v4l2_file_operations mxr_fops = {
883 874
884static int queue_setup(struct vb2_queue *vq, 875static int queue_setup(struct vb2_queue *vq,
885 unsigned int *nbuffers, unsigned int *nplanes, unsigned int sizes[], 876 unsigned int *nbuffers, unsigned int *nplanes, unsigned int sizes[],
886 void *alloc_ctxs[]) 877 struct device *alloc_devs[])
887{ 878{
888 struct mxr_layer *layer = vb2_get_drv_priv(vq); 879 struct mxr_layer *layer = vb2_get_drv_priv(vq);
889 const struct mxr_format *fmt = layer->fmt; 880 const struct mxr_format *fmt = layer->fmt;
@@ -901,7 +892,6 @@ static int queue_setup(struct vb2_queue *vq,
901 892
902 *nplanes = fmt->num_subframes; 893 *nplanes = fmt->num_subframes;
903 for (i = 0; i < fmt->num_subframes; ++i) { 894 for (i = 0; i < fmt->num_subframes; ++i) {
904 alloc_ctxs[i] = layer->mdev->alloc_ctx;
905 sizes[i] = planes[i].sizeimage; 895 sizes[i] = planes[i].sizeimage;
906 mxr_dbg(mdev, "size[%d] = %08x\n", i, sizes[i]); 896 mxr_dbg(mdev, "size[%d] = %08x\n", i, sizes[i]);
907 } 897 }
@@ -1110,6 +1100,7 @@ struct mxr_layer *mxr_base_layer_create(struct mxr_device *mdev,
1110 .min_buffers_needed = 1, 1100 .min_buffers_needed = 1,
1111 .mem_ops = &vb2_dma_contig_memops, 1101 .mem_ops = &vb2_dma_contig_memops,
1112 .lock = &layer->mutex, 1102 .lock = &layer->mutex,
1103 .dev = mdev->dev,
1113 }; 1104 };
1114 1105
1115 return layer; 1106 return layer;
diff --git a/drivers/media/platform/sh_veu.c b/drivers/media/platform/sh_veu.c
index 82b5d69b87fa..15a562af13c7 100644
--- a/drivers/media/platform/sh_veu.c
+++ b/drivers/media/platform/sh_veu.c
@@ -118,7 +118,6 @@ struct sh_veu_dev {
118 struct sh_veu_file *output; 118 struct sh_veu_file *output;
119 struct mutex fop_lock; 119 struct mutex fop_lock;
120 void __iomem *base; 120 void __iomem *base;
121 struct vb2_alloc_ctx *alloc_ctx;
122 spinlock_t lock; 121 spinlock_t lock;
123 bool is_2h; 122 bool is_2h;
124 unsigned int xaction; 123 unsigned int xaction;
@@ -866,7 +865,7 @@ static const struct v4l2_ioctl_ops sh_veu_ioctl_ops = {
866 865
867static int sh_veu_queue_setup(struct vb2_queue *vq, 866static int sh_veu_queue_setup(struct vb2_queue *vq,
868 unsigned int *nbuffers, unsigned int *nplanes, 867 unsigned int *nbuffers, unsigned int *nplanes,
869 unsigned int sizes[], void *alloc_ctxs[]) 868 unsigned int sizes[], struct device *alloc_devs[])
870{ 869{
871 struct sh_veu_dev *veu = vb2_get_drv_priv(vq); 870 struct sh_veu_dev *veu = vb2_get_drv_priv(vq);
872 struct sh_veu_vfmt *vfmt = sh_veu_get_vfmt(veu, vq->type); 871 struct sh_veu_vfmt *vfmt = sh_veu_get_vfmt(veu, vq->type);
@@ -882,14 +881,11 @@ static int sh_veu_queue_setup(struct vb2_queue *vq,
882 *nbuffers = count; 881 *nbuffers = count;
883 } 882 }
884 883
885 if (*nplanes) { 884 if (*nplanes)
886 alloc_ctxs[0] = veu->alloc_ctx;
887 return sizes[0] < size ? -EINVAL : 0; 885 return sizes[0] < size ? -EINVAL : 0;
888 }
889 886
890 *nplanes = 1; 887 *nplanes = 1;
891 sizes[0] = size; 888 sizes[0] = size;
892 alloc_ctxs[0] = veu->alloc_ctx;
893 889
894 dev_dbg(veu->dev, "get %d buffer(s) of size %d each.\n", count, size); 890 dev_dbg(veu->dev, "get %d buffer(s) of size %d each.\n", count, size);
895 891
@@ -948,6 +944,7 @@ static int sh_veu_queue_init(void *priv, struct vb2_queue *src_vq,
948 src_vq->mem_ops = &vb2_dma_contig_memops; 944 src_vq->mem_ops = &vb2_dma_contig_memops;
949 src_vq->lock = &veu->fop_lock; 945 src_vq->lock = &veu->fop_lock;
950 src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY; 946 src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
947 src_vq->dev = veu->v4l2_dev.dev;
951 948
952 ret = vb2_queue_init(src_vq); 949 ret = vb2_queue_init(src_vq);
953 if (ret < 0) 950 if (ret < 0)
@@ -962,6 +959,7 @@ static int sh_veu_queue_init(void *priv, struct vb2_queue *src_vq,
962 dst_vq->mem_ops = &vb2_dma_contig_memops; 959 dst_vq->mem_ops = &vb2_dma_contig_memops;
963 dst_vq->lock = &veu->fop_lock; 960 dst_vq->lock = &veu->fop_lock;
964 dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY; 961 dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
962 dst_vq->dev = veu->v4l2_dev.dev;
965 963
966 return vb2_queue_init(dst_vq); 964 return vb2_queue_init(dst_vq);
967} 965}
@@ -1148,12 +1146,6 @@ static int sh_veu_probe(struct platform_device *pdev)
1148 1146
1149 vdev = &veu->vdev; 1147 vdev = &veu->vdev;
1150 1148
1151 veu->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
1152 if (IS_ERR(veu->alloc_ctx)) {
1153 ret = PTR_ERR(veu->alloc_ctx);
1154 goto einitctx;
1155 }
1156
1157 *vdev = sh_veu_videodev; 1149 *vdev = sh_veu_videodev;
1158 vdev->v4l2_dev = &veu->v4l2_dev; 1150 vdev->v4l2_dev = &veu->v4l2_dev;
1159 spin_lock_init(&veu->lock); 1151 spin_lock_init(&veu->lock);
@@ -1187,8 +1179,6 @@ evidreg:
1187 pm_runtime_disable(&pdev->dev); 1179 pm_runtime_disable(&pdev->dev);
1188 v4l2_m2m_release(veu->m2m_dev); 1180 v4l2_m2m_release(veu->m2m_dev);
1189em2minit: 1181em2minit:
1190 vb2_dma_contig_cleanup_ctx(veu->alloc_ctx);
1191einitctx:
1192 v4l2_device_unregister(&veu->v4l2_dev); 1182 v4l2_device_unregister(&veu->v4l2_dev);
1193 return ret; 1183 return ret;
1194} 1184}
@@ -1202,7 +1192,6 @@ static int sh_veu_remove(struct platform_device *pdev)
1202 video_unregister_device(&veu->vdev); 1192 video_unregister_device(&veu->vdev);
1203 pm_runtime_disable(&pdev->dev); 1193 pm_runtime_disable(&pdev->dev);
1204 v4l2_m2m_release(veu->m2m_dev); 1194 v4l2_m2m_release(veu->m2m_dev);
1205 vb2_dma_contig_cleanup_ctx(veu->alloc_ctx);
1206 v4l2_device_unregister(&veu->v4l2_dev); 1195 v4l2_device_unregister(&veu->v4l2_dev);
1207 1196
1208 return 0; 1197 return 0;
diff --git a/drivers/media/platform/sh_vou.c b/drivers/media/platform/sh_vou.c
index 115740498274..e1f39b4cf1cd 100644
--- a/drivers/media/platform/sh_vou.c
+++ b/drivers/media/platform/sh_vou.c
@@ -86,7 +86,6 @@ struct sh_vou_device {
86 v4l2_std_id std; 86 v4l2_std_id std;
87 int pix_idx; 87 int pix_idx;
88 struct vb2_queue queue; 88 struct vb2_queue queue;
89 struct vb2_alloc_ctx *alloc_ctx;
90 struct sh_vou_buffer *active; 89 struct sh_vou_buffer *active;
91 enum sh_vou_status status; 90 enum sh_vou_status status;
92 unsigned sequence; 91 unsigned sequence;
@@ -245,7 +244,7 @@ static void sh_vou_stream_config(struct sh_vou_device *vou_dev)
245/* Locking: caller holds fop_lock mutex */ 244/* Locking: caller holds fop_lock mutex */
246static int sh_vou_queue_setup(struct vb2_queue *vq, 245static int sh_vou_queue_setup(struct vb2_queue *vq,
247 unsigned int *nbuffers, unsigned int *nplanes, 246 unsigned int *nbuffers, unsigned int *nplanes,
248 unsigned int sizes[], void *alloc_ctxs[]) 247 unsigned int sizes[], struct device *alloc_devs[])
249{ 248{
250 struct sh_vou_device *vou_dev = vb2_get_drv_priv(vq); 249 struct sh_vou_device *vou_dev = vb2_get_drv_priv(vq);
251 struct v4l2_pix_format *pix = &vou_dev->pix; 250 struct v4l2_pix_format *pix = &vou_dev->pix;
@@ -253,7 +252,6 @@ static int sh_vou_queue_setup(struct vb2_queue *vq,
253 252
254 dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__); 253 dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
255 254
256 alloc_ctxs[0] = vou_dev->alloc_ctx;
257 if (*nplanes) 255 if (*nplanes)
258 return sizes[0] < pix->height * bytes_per_line ? -EINVAL : 0; 256 return sizes[0] < pix->height * bytes_per_line ? -EINVAL : 0;
259 *nplanes = 1; 257 *nplanes = 1;
@@ -1304,16 +1302,11 @@ static int sh_vou_probe(struct platform_device *pdev)
1304 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC; 1302 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1305 q->min_buffers_needed = 2; 1303 q->min_buffers_needed = 2;
1306 q->lock = &vou_dev->fop_lock; 1304 q->lock = &vou_dev->fop_lock;
1305 q->dev = &pdev->dev;
1307 ret = vb2_queue_init(q); 1306 ret = vb2_queue_init(q);
1308 if (ret) 1307 if (ret)
1309 goto einitctx; 1308 goto ei2cgadap;
1310 1309
1311 vou_dev->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
1312 if (IS_ERR(vou_dev->alloc_ctx)) {
1313 dev_err(&pdev->dev, "Can't allocate buffer context");
1314 ret = PTR_ERR(vou_dev->alloc_ctx);
1315 goto einitctx;
1316 }
1317 vdev->queue = q; 1310 vdev->queue = q;
1318 INIT_LIST_HEAD(&vou_dev->buf_list); 1311 INIT_LIST_HEAD(&vou_dev->buf_list);
1319 1312
@@ -1348,8 +1341,6 @@ ei2cnd:
1348ereset: 1341ereset:
1349 i2c_put_adapter(i2c_adap); 1342 i2c_put_adapter(i2c_adap);
1350ei2cgadap: 1343ei2cgadap:
1351 vb2_dma_contig_cleanup_ctx(vou_dev->alloc_ctx);
1352einitctx:
1353 pm_runtime_disable(&pdev->dev); 1344 pm_runtime_disable(&pdev->dev);
1354 v4l2_device_unregister(&vou_dev->v4l2_dev); 1345 v4l2_device_unregister(&vou_dev->v4l2_dev);
1355 return ret; 1346 return ret;
@@ -1367,7 +1358,6 @@ static int sh_vou_remove(struct platform_device *pdev)
1367 pm_runtime_disable(&pdev->dev); 1358 pm_runtime_disable(&pdev->dev);
1368 video_unregister_device(&vou_dev->vdev); 1359 video_unregister_device(&vou_dev->vdev);
1369 i2c_put_adapter(client->adapter); 1360 i2c_put_adapter(client->adapter);
1370 vb2_dma_contig_cleanup_ctx(vou_dev->alloc_ctx);
1371 v4l2_device_unregister(&vou_dev->v4l2_dev); 1361 v4l2_device_unregister(&vou_dev->v4l2_dev);
1372 return 0; 1362 return 0;
1373} 1363}
diff --git a/drivers/media/platform/soc_camera/Kconfig b/drivers/media/platform/soc_camera/Kconfig
index 83029a4854ae..39f66414f621 100644
--- a/drivers/media/platform/soc_camera/Kconfig
+++ b/drivers/media/platform/soc_camera/Kconfig
@@ -25,8 +25,8 @@ config VIDEO_PXA27x
25 ---help--- 25 ---help---
26 This is a v4l2 driver for the PXA27x Quick Capture Interface 26 This is a v4l2 driver for the PXA27x Quick Capture Interface
27 27
28config VIDEO_RCAR_VIN 28config VIDEO_RCAR_VIN_OLD
29 tristate "R-Car Video Input (VIN) support" 29 tristate "R-Car Video Input (VIN) support (DEPRECATED)"
30 depends on VIDEO_DEV && SOC_CAMERA 30 depends on VIDEO_DEV && SOC_CAMERA
31 depends on ARCH_RENESAS || COMPILE_TEST 31 depends on ARCH_RENESAS || COMPILE_TEST
32 depends on HAS_DMA 32 depends on HAS_DMA
diff --git a/drivers/media/platform/soc_camera/Makefile b/drivers/media/platform/soc_camera/Makefile
index 7ee71ae231c7..7703cb7ce456 100644
--- a/drivers/media/platform/soc_camera/Makefile
+++ b/drivers/media/platform/soc_camera/Makefile
@@ -10,4 +10,4 @@ obj-$(CONFIG_VIDEO_ATMEL_ISI) += atmel-isi.o
10obj-$(CONFIG_VIDEO_PXA27x) += pxa_camera.o 10obj-$(CONFIG_VIDEO_PXA27x) += pxa_camera.o
11obj-$(CONFIG_VIDEO_SH_MOBILE_CEU) += sh_mobile_ceu_camera.o 11obj-$(CONFIG_VIDEO_SH_MOBILE_CEU) += sh_mobile_ceu_camera.o
12obj-$(CONFIG_VIDEO_SH_MOBILE_CSI2) += sh_mobile_csi2.o 12obj-$(CONFIG_VIDEO_SH_MOBILE_CSI2) += sh_mobile_csi2.o
13obj-$(CONFIG_VIDEO_RCAR_VIN) += rcar_vin.o 13obj-$(CONFIG_VIDEO_RCAR_VIN_OLD) += rcar_vin.o
diff --git a/drivers/media/platform/soc_camera/atmel-isi.c b/drivers/media/platform/soc_camera/atmel-isi.c
index ab2d9b9b1f5d..30211f6b4483 100644
--- a/drivers/media/platform/soc_camera/atmel-isi.c
+++ b/drivers/media/platform/soc_camera/atmel-isi.c
@@ -72,8 +72,6 @@ struct atmel_isi {
72 72
73 int sequence; 73 int sequence;
74 74
75 struct vb2_alloc_ctx *alloc_ctx;
76
77 /* Allocate descriptors for dma buffer use */ 75 /* Allocate descriptors for dma buffer use */
78 struct fbd *p_fb_descriptors; 76 struct fbd *p_fb_descriptors;
79 dma_addr_t fb_descriptors_phys; 77 dma_addr_t fb_descriptors_phys;
@@ -305,7 +303,7 @@ static int atmel_isi_wait_status(struct atmel_isi *isi, int wait_reset)
305 ------------------------------------------------------------------*/ 303 ------------------------------------------------------------------*/
306static int queue_setup(struct vb2_queue *vq, 304static int queue_setup(struct vb2_queue *vq,
307 unsigned int *nbuffers, unsigned int *nplanes, 305 unsigned int *nbuffers, unsigned int *nplanes,
308 unsigned int sizes[], void *alloc_ctxs[]) 306 unsigned int sizes[], struct device *alloc_devs[])
309{ 307{
310 struct soc_camera_device *icd = soc_camera_from_vb2q(vq); 308 struct soc_camera_device *icd = soc_camera_from_vb2q(vq);
311 struct soc_camera_host *ici = to_soc_camera_host(icd->parent); 309 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
@@ -322,7 +320,6 @@ static int queue_setup(struct vb2_queue *vq,
322 320
323 *nplanes = 1; 321 *nplanes = 1;
324 sizes[0] = size; 322 sizes[0] = size;
325 alloc_ctxs[0] = isi->alloc_ctx;
326 323
327 isi->sequence = 0; 324 isi->sequence = 0;
328 isi->active = NULL; 325 isi->active = NULL;
@@ -567,6 +564,7 @@ static int isi_camera_init_videobuf(struct vb2_queue *q,
567 q->mem_ops = &vb2_dma_contig_memops; 564 q->mem_ops = &vb2_dma_contig_memops;
568 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC; 565 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
569 q->lock = &ici->host_lock; 566 q->lock = &ici->host_lock;
567 q->dev = ici->v4l2_dev.dev;
570 568
571 return vb2_queue_init(q); 569 return vb2_queue_init(q);
572} 570}
@@ -963,7 +961,6 @@ static int atmel_isi_remove(struct platform_device *pdev)
963 struct atmel_isi, soc_host); 961 struct atmel_isi, soc_host);
964 962
965 soc_camera_host_unregister(soc_host); 963 soc_camera_host_unregister(soc_host);
966 vb2_dma_contig_cleanup_ctx(isi->alloc_ctx);
967 dma_free_coherent(&pdev->dev, 964 dma_free_coherent(&pdev->dev,
968 sizeof(struct fbd) * MAX_BUFFER_NUM, 965 sizeof(struct fbd) * MAX_BUFFER_NUM,
969 isi->p_fb_descriptors, 966 isi->p_fb_descriptors,
@@ -1067,12 +1064,6 @@ static int atmel_isi_probe(struct platform_device *pdev)
1067 list_add(&isi->dma_desc[i].list, &isi->dma_desc_head); 1064 list_add(&isi->dma_desc[i].list, &isi->dma_desc_head);
1068 } 1065 }
1069 1066
1070 isi->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
1071 if (IS_ERR(isi->alloc_ctx)) {
1072 ret = PTR_ERR(isi->alloc_ctx);
1073 goto err_alloc_ctx;
1074 }
1075
1076 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0); 1067 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1077 isi->regs = devm_ioremap_resource(&pdev->dev, regs); 1068 isi->regs = devm_ioremap_resource(&pdev->dev, regs);
1078 if (IS_ERR(isi->regs)) { 1069 if (IS_ERR(isi->regs)) {
@@ -1119,8 +1110,6 @@ err_register_soc_camera_host:
1119 pm_runtime_disable(&pdev->dev); 1110 pm_runtime_disable(&pdev->dev);
1120err_req_irq: 1111err_req_irq:
1121err_ioremap: 1112err_ioremap:
1122 vb2_dma_contig_cleanup_ctx(isi->alloc_ctx);
1123err_alloc_ctx:
1124 dma_free_coherent(&pdev->dev, 1113 dma_free_coherent(&pdev->dev,
1125 sizeof(struct fbd) * MAX_BUFFER_NUM, 1114 sizeof(struct fbd) * MAX_BUFFER_NUM,
1126 isi->p_fb_descriptors, 1115 isi->p_fb_descriptors,
diff --git a/drivers/media/platform/soc_camera/rcar_vin.c b/drivers/media/platform/soc_camera/rcar_vin.c
index 3f9c1b8456c3..9c137522c660 100644
--- a/drivers/media/platform/soc_camera/rcar_vin.c
+++ b/drivers/media/platform/soc_camera/rcar_vin.c
@@ -484,7 +484,6 @@ struct rcar_vin_priv {
484 struct list_head capture; 484 struct list_head capture;
485#define MAX_BUFFER_NUM 3 485#define MAX_BUFFER_NUM 3
486 struct vb2_v4l2_buffer *queue_buf[MAX_BUFFER_NUM]; 486 struct vb2_v4l2_buffer *queue_buf[MAX_BUFFER_NUM];
487 struct vb2_alloc_ctx *alloc_ctx;
488 enum v4l2_field field; 487 enum v4l2_field field;
489 unsigned int pdata_flags; 488 unsigned int pdata_flags;
490 unsigned int vb_count; 489 unsigned int vb_count;
@@ -534,14 +533,12 @@ struct rcar_vin_cam {
534static int rcar_vin_videobuf_setup(struct vb2_queue *vq, 533static int rcar_vin_videobuf_setup(struct vb2_queue *vq,
535 unsigned int *count, 534 unsigned int *count,
536 unsigned int *num_planes, 535 unsigned int *num_planes,
537 unsigned int sizes[], void *alloc_ctxs[]) 536 unsigned int sizes[], struct device *alloc_devs[])
538{ 537{
539 struct soc_camera_device *icd = soc_camera_from_vb2q(vq); 538 struct soc_camera_device *icd = soc_camera_from_vb2q(vq);
540 struct soc_camera_host *ici = to_soc_camera_host(icd->parent); 539 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
541 struct rcar_vin_priv *priv = ici->priv; 540 struct rcar_vin_priv *priv = ici->priv;
542 541
543 alloc_ctxs[0] = priv->alloc_ctx;
544
545 if (!vq->num_buffers) 542 if (!vq->num_buffers)
546 priv->sequence = 0; 543 priv->sequence = 0;
547 544
@@ -1816,6 +1813,7 @@ static int rcar_vin_init_videobuf2(struct vb2_queue *vq,
1816 vq->buf_struct_size = sizeof(struct rcar_vin_buffer); 1813 vq->buf_struct_size = sizeof(struct rcar_vin_buffer);
1817 vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC; 1814 vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1818 vq->lock = &ici->host_lock; 1815 vq->lock = &ici->host_lock;
1816 vq->dev = ici->v4l2_dev.dev;
1819 1817
1820 return vb2_queue_init(vq); 1818 return vb2_queue_init(vq);
1821} 1819}
@@ -1912,10 +1910,6 @@ static int rcar_vin_probe(struct platform_device *pdev)
1912 if (ret) 1910 if (ret)
1913 return ret; 1911 return ret;
1914 1912
1915 priv->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
1916 if (IS_ERR(priv->alloc_ctx))
1917 return PTR_ERR(priv->alloc_ctx);
1918
1919 priv->ici.priv = priv; 1913 priv->ici.priv = priv;
1920 priv->ici.v4l2_dev.dev = &pdev->dev; 1914 priv->ici.v4l2_dev.dev = &pdev->dev;
1921 priv->ici.drv_name = dev_name(&pdev->dev); 1915 priv->ici.drv_name = dev_name(&pdev->dev);
@@ -1946,7 +1940,6 @@ static int rcar_vin_probe(struct platform_device *pdev)
1946 1940
1947cleanup: 1941cleanup:
1948 pm_runtime_disable(&pdev->dev); 1942 pm_runtime_disable(&pdev->dev);
1949 vb2_dma_contig_cleanup_ctx(priv->alloc_ctx);
1950 1943
1951 return ret; 1944 return ret;
1952} 1945}
@@ -1954,12 +1947,9 @@ cleanup:
1954static int rcar_vin_remove(struct platform_device *pdev) 1947static int rcar_vin_remove(struct platform_device *pdev)
1955{ 1948{
1956 struct soc_camera_host *soc_host = to_soc_camera_host(&pdev->dev); 1949 struct soc_camera_host *soc_host = to_soc_camera_host(&pdev->dev);
1957 struct rcar_vin_priv *priv = container_of(soc_host,
1958 struct rcar_vin_priv, ici);
1959 1950
1960 soc_camera_host_unregister(soc_host); 1951 soc_camera_host_unregister(soc_host);
1961 pm_runtime_disable(&pdev->dev); 1952 pm_runtime_disable(&pdev->dev);
1962 vb2_dma_contig_cleanup_ctx(priv->alloc_ctx);
1963 1953
1964 return 0; 1954 return 0;
1965} 1955}
diff --git a/drivers/media/platform/soc_camera/sh_mobile_ceu_camera.c b/drivers/media/platform/soc_camera/sh_mobile_ceu_camera.c
index b9f369c0fb94..02b519dde42a 100644
--- a/drivers/media/platform/soc_camera/sh_mobile_ceu_camera.c
+++ b/drivers/media/platform/soc_camera/sh_mobile_ceu_camera.c
@@ -113,7 +113,6 @@ struct sh_mobile_ceu_dev {
113 spinlock_t lock; /* Protects video buffer lists */ 113 spinlock_t lock; /* Protects video buffer lists */
114 struct list_head capture; 114 struct list_head capture;
115 struct vb2_v4l2_buffer *active; 115 struct vb2_v4l2_buffer *active;
116 struct vb2_alloc_ctx *alloc_ctx;
117 116
118 struct sh_mobile_ceu_info *pdata; 117 struct sh_mobile_ceu_info *pdata;
119 struct completion complete; 118 struct completion complete;
@@ -211,14 +210,12 @@ static int sh_mobile_ceu_soft_reset(struct sh_mobile_ceu_dev *pcdev)
211 */ 210 */
212static int sh_mobile_ceu_videobuf_setup(struct vb2_queue *vq, 211static int sh_mobile_ceu_videobuf_setup(struct vb2_queue *vq,
213 unsigned int *count, unsigned int *num_planes, 212 unsigned int *count, unsigned int *num_planes,
214 unsigned int sizes[], void *alloc_ctxs[]) 213 unsigned int sizes[], struct device *alloc_devs[])
215{ 214{
216 struct soc_camera_device *icd = soc_camera_from_vb2q(vq); 215 struct soc_camera_device *icd = soc_camera_from_vb2q(vq);
217 struct soc_camera_host *ici = to_soc_camera_host(icd->parent); 216 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
218 struct sh_mobile_ceu_dev *pcdev = ici->priv; 217 struct sh_mobile_ceu_dev *pcdev = ici->priv;
219 218
220 alloc_ctxs[0] = pcdev->alloc_ctx;
221
222 if (!vq->num_buffers) 219 if (!vq->num_buffers)
223 pcdev->sequence = 0; 220 pcdev->sequence = 0;
224 221
@@ -1670,6 +1667,7 @@ static int sh_mobile_ceu_init_videobuf(struct vb2_queue *q,
1670 q->buf_struct_size = sizeof(struct sh_mobile_ceu_buffer); 1667 q->buf_struct_size = sizeof(struct sh_mobile_ceu_buffer);
1671 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC; 1668 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1672 q->lock = &ici->host_lock; 1669 q->lock = &ici->host_lock;
1670 q->dev = ici->v4l2_dev.dev;
1673 1671
1674 return vb2_queue_init(q); 1672 return vb2_queue_init(q);
1675} 1673}
@@ -1822,12 +1820,6 @@ static int sh_mobile_ceu_probe(struct platform_device *pdev)
1822 pcdev->ici.ops = &sh_mobile_ceu_host_ops; 1820 pcdev->ici.ops = &sh_mobile_ceu_host_ops;
1823 pcdev->ici.capabilities = SOCAM_HOST_CAP_STRIDE; 1821 pcdev->ici.capabilities = SOCAM_HOST_CAP_STRIDE;
1824 1822
1825 pcdev->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
1826 if (IS_ERR(pcdev->alloc_ctx)) {
1827 err = PTR_ERR(pcdev->alloc_ctx);
1828 goto exit_free_clk;
1829 }
1830
1831 if (pcdev->pdata && pcdev->pdata->asd_sizes) { 1823 if (pcdev->pdata && pcdev->pdata->asd_sizes) {
1832 struct v4l2_async_subdev **asd; 1824 struct v4l2_async_subdev **asd;
1833 char name[] = "sh-mobile-csi2"; 1825 char name[] = "sh-mobile-csi2";
@@ -1872,7 +1864,7 @@ static int sh_mobile_ceu_probe(struct platform_device *pdev)
1872 1864
1873 if (!csi2_pdev) { 1865 if (!csi2_pdev) {
1874 err = -ENOMEM; 1866 err = -ENOMEM;
1875 goto exit_free_ctx; 1867 goto exit_free_clk;
1876 } 1868 }
1877 1869
1878 pcdev->csi2_pdev = csi2_pdev; 1870 pcdev->csi2_pdev = csi2_pdev;
@@ -1955,8 +1947,6 @@ exit_pdev_put:
1955 pcdev->csi2_pdev->resource = NULL; 1947 pcdev->csi2_pdev->resource = NULL;
1956 platform_device_put(pcdev->csi2_pdev); 1948 platform_device_put(pcdev->csi2_pdev);
1957 } 1949 }
1958exit_free_ctx:
1959 vb2_dma_contig_cleanup_ctx(pcdev->alloc_ctx);
1960exit_free_clk: 1950exit_free_clk:
1961 pm_runtime_disable(&pdev->dev); 1951 pm_runtime_disable(&pdev->dev);
1962exit_release_mem: 1952exit_release_mem:
@@ -1976,7 +1966,6 @@ static int sh_mobile_ceu_remove(struct platform_device *pdev)
1976 pm_runtime_disable(&pdev->dev); 1966 pm_runtime_disable(&pdev->dev);
1977 if (platform_get_resource(pdev, IORESOURCE_MEM, 1)) 1967 if (platform_get_resource(pdev, IORESOURCE_MEM, 1))
1978 dma_release_declared_memory(&pdev->dev); 1968 dma_release_declared_memory(&pdev->dev);
1979 vb2_dma_contig_cleanup_ctx(pcdev->alloc_ctx);
1980 if (csi2_pdev && csi2_pdev->dev.driver) { 1969 if (csi2_pdev && csi2_pdev->dev.driver) {
1981 struct module *csi2_drv = csi2_pdev->dev.driver->owner; 1970 struct module *csi2_drv = csi2_pdev->dev.driver->owner;
1982 platform_device_del(csi2_pdev); 1971 platform_device_del(csi2_pdev);
diff --git a/drivers/media/platform/sti/bdisp/bdisp-filter.h b/drivers/media/platform/sti/bdisp/bdisp-filter.h
index fc8c54f725ad..53e52fb4127f 100644
--- a/drivers/media/platform/sti/bdisp/bdisp-filter.h
+++ b/drivers/media/platform/sti/bdisp/bdisp-filter.h
@@ -19,178 +19,6 @@ struct bdisp_filter_h_spec {
19 const u16 max; 19 const u16 max;
20 const u8 coef[BDISP_HF_NB]; 20 const u8 coef[BDISP_HF_NB];
21}; 21};
22
23static const struct bdisp_filter_h_spec bdisp_h_spec[] = {
24 {
25 .min = 0,
26 .max = 921,
27 .coef = {
28 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00,
29 0x00, 0x00, 0xff, 0x07, 0x3d, 0xfc, 0x01, 0x00,
30 0x00, 0x01, 0xfd, 0x11, 0x36, 0xf9, 0x02, 0x00,
31 0x00, 0x01, 0xfb, 0x1b, 0x2e, 0xf9, 0x02, 0x00,
32 0x00, 0x01, 0xf9, 0x26, 0x26, 0xf9, 0x01, 0x00,
33 0x00, 0x02, 0xf9, 0x30, 0x19, 0xfb, 0x01, 0x00,
34 0x00, 0x02, 0xf9, 0x39, 0x0e, 0xfd, 0x01, 0x00,
35 0x00, 0x01, 0xfc, 0x3e, 0x06, 0xff, 0x00, 0x00
36 }
37 },
38 {
39 .min = 921,
40 .max = 1024,
41 .coef = {
42 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00,
43 0xff, 0x03, 0xfd, 0x08, 0x3e, 0xf9, 0x04, 0xfe,
44 0xfd, 0x06, 0xf8, 0x13, 0x3b, 0xf4, 0x07, 0xfc,
45 0xfb, 0x08, 0xf5, 0x1f, 0x34, 0xf1, 0x09, 0xfb,
46 0xfb, 0x09, 0xf2, 0x2b, 0x2a, 0xf1, 0x09, 0xfb,
47 0xfb, 0x09, 0xf2, 0x35, 0x1e, 0xf4, 0x08, 0xfb,
48 0xfc, 0x07, 0xf5, 0x3c, 0x12, 0xf7, 0x06, 0xfd,
49 0xfe, 0x04, 0xfa, 0x3f, 0x07, 0xfc, 0x03, 0xff
50 }
51 },
52 {
53 .min = 1024,
54 .max = 1126,
55 .coef = {
56 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00,
57 0xff, 0x03, 0xfd, 0x08, 0x3e, 0xf9, 0x04, 0xfe,
58 0xfd, 0x06, 0xf8, 0x13, 0x3b, 0xf4, 0x07, 0xfc,
59 0xfb, 0x08, 0xf5, 0x1f, 0x34, 0xf1, 0x09, 0xfb,
60 0xfb, 0x09, 0xf2, 0x2b, 0x2a, 0xf1, 0x09, 0xfb,
61 0xfb, 0x09, 0xf2, 0x35, 0x1e, 0xf4, 0x08, 0xfb,
62 0xfc, 0x07, 0xf5, 0x3c, 0x12, 0xf7, 0x06, 0xfd,
63 0xfe, 0x04, 0xfa, 0x3f, 0x07, 0xfc, 0x03, 0xff
64 }
65 },
66 {
67 .min = 1126,
68 .max = 1228,
69 .coef = {
70 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00,
71 0xff, 0x03, 0xfd, 0x08, 0x3e, 0xf9, 0x04, 0xfe,
72 0xfd, 0x06, 0xf8, 0x13, 0x3b, 0xf4, 0x07, 0xfc,
73 0xfb, 0x08, 0xf5, 0x1f, 0x34, 0xf1, 0x09, 0xfb,
74 0xfb, 0x09, 0xf2, 0x2b, 0x2a, 0xf1, 0x09, 0xfb,
75 0xfb, 0x09, 0xf2, 0x35, 0x1e, 0xf4, 0x08, 0xfb,
76 0xfc, 0x07, 0xf5, 0x3c, 0x12, 0xf7, 0x06, 0xfd,
77 0xfe, 0x04, 0xfa, 0x3f, 0x07, 0xfc, 0x03, 0xff
78 }
79 },
80 {
81 .min = 1228,
82 .max = 1331,
83 .coef = {
84 0xfd, 0x04, 0xfc, 0x05, 0x39, 0x05, 0xfc, 0x04,
85 0xfc, 0x06, 0xf9, 0x0c, 0x39, 0xfe, 0x00, 0x02,
86 0xfb, 0x08, 0xf6, 0x17, 0x35, 0xf9, 0x02, 0x00,
87 0xfc, 0x08, 0xf4, 0x20, 0x30, 0xf4, 0x05, 0xff,
88 0xfd, 0x07, 0xf4, 0x29, 0x28, 0xf3, 0x07, 0xfd,
89 0xff, 0x05, 0xf5, 0x31, 0x1f, 0xf3, 0x08, 0xfc,
90 0x00, 0x02, 0xf9, 0x38, 0x14, 0xf6, 0x08, 0xfb,
91 0x02, 0x00, 0xff, 0x3a, 0x0b, 0xf8, 0x06, 0xfc
92 }
93 },
94 {
95 .min = 1331,
96 .max = 1433,
97 .coef = {
98 0xfc, 0x06, 0xf9, 0x09, 0x34, 0x09, 0xf9, 0x06,
99 0xfd, 0x07, 0xf7, 0x10, 0x32, 0x02, 0xfc, 0x05,
100 0xfe, 0x07, 0xf6, 0x17, 0x2f, 0xfc, 0xff, 0x04,
101 0xff, 0x06, 0xf5, 0x20, 0x2a, 0xf9, 0x01, 0x02,
102 0x00, 0x04, 0xf6, 0x27, 0x25, 0xf6, 0x04, 0x00,
103 0x02, 0x01, 0xf9, 0x2d, 0x1d, 0xf5, 0x06, 0xff,
104 0x04, 0xff, 0xfd, 0x31, 0x15, 0xf5, 0x07, 0xfe,
105 0x05, 0xfc, 0x02, 0x35, 0x0d, 0xf7, 0x07, 0xfd
106 }
107 },
108 {
109 .min = 1433,
110 .max = 1536,
111 .coef = {
112 0xfe, 0x06, 0xf8, 0x0b, 0x30, 0x0b, 0xf8, 0x06,
113 0xff, 0x06, 0xf7, 0x12, 0x2d, 0x05, 0xfa, 0x06,
114 0x00, 0x04, 0xf6, 0x18, 0x2c, 0x00, 0xfc, 0x06,
115 0x01, 0x02, 0xf7, 0x1f, 0x27, 0xfd, 0xff, 0x04,
116 0x03, 0x00, 0xf9, 0x24, 0x24, 0xf9, 0x00, 0x03,
117 0x04, 0xff, 0xfd, 0x29, 0x1d, 0xf7, 0x02, 0x01,
118 0x06, 0xfc, 0x00, 0x2d, 0x17, 0xf6, 0x04, 0x00,
119 0x06, 0xfa, 0x05, 0x30, 0x0f, 0xf7, 0x06, 0xff
120 }
121 },
122 {
123 .min = 1536,
124 .max = 2048,
125 .coef = {
126 0x05, 0xfd, 0xfb, 0x13, 0x25, 0x13, 0xfb, 0xfd,
127 0x05, 0xfc, 0xfd, 0x17, 0x24, 0x0f, 0xf9, 0xff,
128 0x04, 0xfa, 0xff, 0x1b, 0x24, 0x0b, 0xf9, 0x00,
129 0x03, 0xf9, 0x01, 0x1f, 0x23, 0x08, 0xf8, 0x01,
130 0x02, 0xf9, 0x04, 0x22, 0x20, 0x04, 0xf9, 0x02,
131 0x01, 0xf8, 0x08, 0x25, 0x1d, 0x01, 0xf9, 0x03,
132 0x00, 0xf9, 0x0c, 0x25, 0x1a, 0xfe, 0xfa, 0x04,
133 0xff, 0xf9, 0x10, 0x26, 0x15, 0xfc, 0xfc, 0x05
134 }
135 },
136 {
137 .min = 2048,
138 .max = 3072,
139 .coef = {
140 0xfc, 0xfd, 0x06, 0x13, 0x18, 0x13, 0x06, 0xfd,
141 0xfc, 0xfe, 0x08, 0x15, 0x17, 0x12, 0x04, 0xfc,
142 0xfb, 0xfe, 0x0a, 0x16, 0x18, 0x10, 0x03, 0xfc,
143 0xfb, 0x00, 0x0b, 0x18, 0x17, 0x0f, 0x01, 0xfb,
144 0xfb, 0x00, 0x0d, 0x19, 0x17, 0x0d, 0x00, 0xfb,
145 0xfb, 0x01, 0x0f, 0x19, 0x16, 0x0b, 0x00, 0xfb,
146 0xfc, 0x03, 0x11, 0x19, 0x15, 0x09, 0xfe, 0xfb,
147 0xfc, 0x04, 0x12, 0x1a, 0x12, 0x08, 0xfe, 0xfc
148 }
149 },
150 {
151 .min = 3072,
152 .max = 4096,
153 .coef = {
154 0xfe, 0x02, 0x09, 0x0f, 0x0e, 0x0f, 0x09, 0x02,
155 0xff, 0x02, 0x09, 0x0f, 0x10, 0x0e, 0x08, 0x01,
156 0xff, 0x03, 0x0a, 0x10, 0x10, 0x0d, 0x07, 0x00,
157 0x00, 0x04, 0x0b, 0x10, 0x0f, 0x0c, 0x06, 0x00,
158 0x00, 0x05, 0x0c, 0x10, 0x0e, 0x0c, 0x05, 0x00,
159 0x00, 0x06, 0x0c, 0x11, 0x0e, 0x0b, 0x04, 0x00,
160 0x00, 0x07, 0x0d, 0x11, 0x0f, 0x0a, 0x03, 0xff,
161 0x01, 0x08, 0x0e, 0x11, 0x0e, 0x09, 0x02, 0xff
162 }
163 },
164 {
165 .min = 4096,
166 .max = 5120,
167 .coef = {
168 0x00, 0x04, 0x09, 0x0c, 0x0e, 0x0c, 0x09, 0x04,
169 0x01, 0x05, 0x09, 0x0c, 0x0d, 0x0c, 0x08, 0x04,
170 0x01, 0x05, 0x0a, 0x0c, 0x0e, 0x0b, 0x08, 0x03,
171 0x02, 0x06, 0x0a, 0x0d, 0x0c, 0x0b, 0x07, 0x03,
172 0x02, 0x07, 0x0a, 0x0d, 0x0d, 0x0a, 0x07, 0x02,
173 0x03, 0x07, 0x0b, 0x0d, 0x0c, 0x0a, 0x06, 0x02,
174 0x03, 0x08, 0x0b, 0x0d, 0x0d, 0x0a, 0x05, 0x01,
175 0x04, 0x08, 0x0c, 0x0d, 0x0c, 0x09, 0x05, 0x01
176 }
177 },
178 {
179 .min = 5120,
180 .max = 65535,
181 .coef = {
182 0x03, 0x06, 0x09, 0x0b, 0x09, 0x0b, 0x09, 0x06,
183 0x03, 0x06, 0x09, 0x0b, 0x0c, 0x0a, 0x08, 0x05,
184 0x03, 0x06, 0x09, 0x0b, 0x0c, 0x0a, 0x08, 0x05,
185 0x04, 0x07, 0x09, 0x0b, 0x0b, 0x0a, 0x08, 0x04,
186 0x04, 0x07, 0x0a, 0x0b, 0x0b, 0x0a, 0x07, 0x04,
187 0x04, 0x08, 0x0a, 0x0b, 0x0b, 0x09, 0x07, 0x04,
188 0x05, 0x08, 0x0a, 0x0b, 0x0c, 0x09, 0x06, 0x03,
189 0x05, 0x08, 0x0a, 0x0b, 0x0c, 0x09, 0x06, 0x03
190 }
191 }
192};
193
194/** 22/**
195 * struct bdisp_filter_v_spec - Vertical filter specification 23 * struct bdisp_filter_v_spec - Vertical filter specification
196 * 24 *
@@ -204,138 +32,6 @@ struct bdisp_filter_v_spec {
204 const u8 coef[BDISP_VF_NB]; 32 const u8 coef[BDISP_VF_NB];
205}; 33};
206 34
207static const struct bdisp_filter_v_spec bdisp_v_spec[] = {
208 {
209 .min = 0,
210 .max = 1024,
211 .coef = {
212 0x00, 0x00, 0x40, 0x00, 0x00,
213 0x00, 0x06, 0x3d, 0xfd, 0x00,
214 0xfe, 0x0f, 0x38, 0xfb, 0x00,
215 0xfd, 0x19, 0x2f, 0xfb, 0x00,
216 0xfc, 0x24, 0x24, 0xfc, 0x00,
217 0xfb, 0x2f, 0x19, 0xfd, 0x00,
218 0xfb, 0x38, 0x0f, 0xfe, 0x00,
219 0xfd, 0x3d, 0x06, 0x00, 0x00
220 }
221 },
222 {
223 .min = 1024,
224 .max = 1331,
225 .coef = {
226 0xfc, 0x05, 0x3e, 0x05, 0xfc,
227 0xf8, 0x0e, 0x3b, 0xff, 0x00,
228 0xf5, 0x18, 0x38, 0xf9, 0x02,
229 0xf4, 0x21, 0x31, 0xf5, 0x05,
230 0xf4, 0x2a, 0x27, 0xf4, 0x07,
231 0xf6, 0x30, 0x1e, 0xf4, 0x08,
232 0xf9, 0x35, 0x15, 0xf6, 0x07,
233 0xff, 0x37, 0x0b, 0xf9, 0x06
234 }
235 },
236 {
237 .min = 1331,
238 .max = 1433,
239 .coef = {
240 0xf8, 0x0a, 0x3c, 0x0a, 0xf8,
241 0xf6, 0x12, 0x3b, 0x02, 0xfb,
242 0xf4, 0x1b, 0x35, 0xfd, 0xff,
243 0xf4, 0x23, 0x30, 0xf8, 0x01,
244 0xf6, 0x29, 0x27, 0xf6, 0x04,
245 0xf9, 0x2e, 0x1e, 0xf5, 0x06,
246 0xfd, 0x31, 0x16, 0xf6, 0x06,
247 0x02, 0x32, 0x0d, 0xf8, 0x07
248 }
249 },
250 {
251 .min = 1433,
252 .max = 1536,
253 .coef = {
254 0xf6, 0x0e, 0x38, 0x0e, 0xf6,
255 0xf5, 0x15, 0x38, 0x06, 0xf8,
256 0xf5, 0x1d, 0x33, 0x00, 0xfb,
257 0xf6, 0x23, 0x2d, 0xfc, 0xfe,
258 0xf9, 0x28, 0x26, 0xf9, 0x00,
259 0xfc, 0x2c, 0x1e, 0xf7, 0x03,
260 0x00, 0x2e, 0x18, 0xf6, 0x04,
261 0x05, 0x2e, 0x11, 0xf7, 0x05
262 }
263 },
264 {
265 .min = 1536,
266 .max = 2048,
267 .coef = {
268 0xfb, 0x13, 0x24, 0x13, 0xfb,
269 0xfd, 0x17, 0x23, 0x0f, 0xfa,
270 0xff, 0x1a, 0x23, 0x0b, 0xf9,
271 0x01, 0x1d, 0x22, 0x07, 0xf9,
272 0x04, 0x20, 0x1f, 0x04, 0xf9,
273 0x07, 0x22, 0x1c, 0x01, 0xfa,
274 0x0b, 0x24, 0x17, 0xff, 0xfb,
275 0x0f, 0x24, 0x14, 0xfd, 0xfc
276 }
277 },
278 {
279 .min = 2048,
280 .max = 3072,
281 .coef = {
282 0x05, 0x10, 0x16, 0x10, 0x05,
283 0x06, 0x11, 0x16, 0x0f, 0x04,
284 0x08, 0x13, 0x15, 0x0e, 0x02,
285 0x09, 0x14, 0x16, 0x0c, 0x01,
286 0x0b, 0x15, 0x15, 0x0b, 0x00,
287 0x0d, 0x16, 0x13, 0x0a, 0x00,
288 0x0f, 0x17, 0x13, 0x08, 0xff,
289 0x11, 0x18, 0x12, 0x07, 0xfe
290 }
291 },
292 {
293 .min = 3072,
294 .max = 4096,
295 .coef = {
296 0x09, 0x0f, 0x10, 0x0f, 0x09,
297 0x09, 0x0f, 0x12, 0x0e, 0x08,
298 0x0a, 0x10, 0x11, 0x0e, 0x07,
299 0x0b, 0x11, 0x11, 0x0d, 0x06,
300 0x0c, 0x11, 0x12, 0x0c, 0x05,
301 0x0d, 0x12, 0x11, 0x0c, 0x04,
302 0x0e, 0x12, 0x11, 0x0b, 0x04,
303 0x0f, 0x13, 0x11, 0x0a, 0x03
304 }
305 },
306 {
307 .min = 4096,
308 .max = 5120,
309 .coef = {
310 0x0a, 0x0e, 0x10, 0x0e, 0x0a,
311 0x0b, 0x0e, 0x0f, 0x0e, 0x0a,
312 0x0b, 0x0f, 0x10, 0x0d, 0x09,
313 0x0c, 0x0f, 0x10, 0x0d, 0x08,
314 0x0d, 0x0f, 0x0f, 0x0d, 0x08,
315 0x0d, 0x10, 0x10, 0x0c, 0x07,
316 0x0e, 0x10, 0x0f, 0x0c, 0x07,
317 0x0f, 0x10, 0x10, 0x0b, 0x06
318 }
319 },
320 {
321 .min = 5120,
322 .max = 65535,
323 .coef = {
324 0x0b, 0x0e, 0x0e, 0x0e, 0x0b,
325 0x0b, 0x0e, 0x0f, 0x0d, 0x0b,
326 0x0c, 0x0e, 0x0f, 0x0d, 0x0a,
327 0x0c, 0x0e, 0x0f, 0x0d, 0x0a,
328 0x0d, 0x0f, 0x0e, 0x0d, 0x09,
329 0x0d, 0x0f, 0x0f, 0x0c, 0x09,
330 0x0e, 0x0f, 0x0e, 0x0c, 0x09,
331 0x0e, 0x0f, 0x0f, 0x0c, 0x08
332 }
333 }
334};
335
336#define NB_H_FILTER ARRAY_SIZE(bdisp_h_spec)
337#define NB_V_FILTER ARRAY_SIZE(bdisp_v_spec)
338
339/* RGB YUV 601 standard conversion */ 35/* RGB YUV 601 standard conversion */
340static const u32 bdisp_rgb_to_yuv[] = { 36static const u32 bdisp_rgb_to_yuv[] = {
341 0x0e1e8bee, 0x08420419, 0xfb5ed471, 0x08004080, 37 0x0e1e8bee, 0x08420419, 0xfb5ed471, 0x08004080,
diff --git a/drivers/media/platform/sti/bdisp/bdisp-hw.c b/drivers/media/platform/sti/bdisp/bdisp-hw.c
index 052c932ac942..3df66d11c795 100644
--- a/drivers/media/platform/sti/bdisp/bdisp-hw.c
+++ b/drivers/media/platform/sti/bdisp/bdisp-hw.c
@@ -47,6 +47,311 @@ struct bdisp_filter_addr {
47 dma_addr_t paddr; /* Physical address for filter table */ 47 dma_addr_t paddr; /* Physical address for filter table */
48}; 48};
49 49
50static const struct bdisp_filter_h_spec bdisp_h_spec[] = {
51 {
52 .min = 0,
53 .max = 921,
54 .coef = {
55 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00,
56 0x00, 0x00, 0xff, 0x07, 0x3d, 0xfc, 0x01, 0x00,
57 0x00, 0x01, 0xfd, 0x11, 0x36, 0xf9, 0x02, 0x00,
58 0x00, 0x01, 0xfb, 0x1b, 0x2e, 0xf9, 0x02, 0x00,
59 0x00, 0x01, 0xf9, 0x26, 0x26, 0xf9, 0x01, 0x00,
60 0x00, 0x02, 0xf9, 0x30, 0x19, 0xfb, 0x01, 0x00,
61 0x00, 0x02, 0xf9, 0x39, 0x0e, 0xfd, 0x01, 0x00,
62 0x00, 0x01, 0xfc, 0x3e, 0x06, 0xff, 0x00, 0x00
63 }
64 },
65 {
66 .min = 921,
67 .max = 1024,
68 .coef = {
69 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00,
70 0xff, 0x03, 0xfd, 0x08, 0x3e, 0xf9, 0x04, 0xfe,
71 0xfd, 0x06, 0xf8, 0x13, 0x3b, 0xf4, 0x07, 0xfc,
72 0xfb, 0x08, 0xf5, 0x1f, 0x34, 0xf1, 0x09, 0xfb,
73 0xfb, 0x09, 0xf2, 0x2b, 0x2a, 0xf1, 0x09, 0xfb,
74 0xfb, 0x09, 0xf2, 0x35, 0x1e, 0xf4, 0x08, 0xfb,
75 0xfc, 0x07, 0xf5, 0x3c, 0x12, 0xf7, 0x06, 0xfd,
76 0xfe, 0x04, 0xfa, 0x3f, 0x07, 0xfc, 0x03, 0xff
77 }
78 },
79 {
80 .min = 1024,
81 .max = 1126,
82 .coef = {
83 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00,
84 0xff, 0x03, 0xfd, 0x08, 0x3e, 0xf9, 0x04, 0xfe,
85 0xfd, 0x06, 0xf8, 0x13, 0x3b, 0xf4, 0x07, 0xfc,
86 0xfb, 0x08, 0xf5, 0x1f, 0x34, 0xf1, 0x09, 0xfb,
87 0xfb, 0x09, 0xf2, 0x2b, 0x2a, 0xf1, 0x09, 0xfb,
88 0xfb, 0x09, 0xf2, 0x35, 0x1e, 0xf4, 0x08, 0xfb,
89 0xfc, 0x07, 0xf5, 0x3c, 0x12, 0xf7, 0x06, 0xfd,
90 0xfe, 0x04, 0xfa, 0x3f, 0x07, 0xfc, 0x03, 0xff
91 }
92 },
93 {
94 .min = 1126,
95 .max = 1228,
96 .coef = {
97 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00,
98 0xff, 0x03, 0xfd, 0x08, 0x3e, 0xf9, 0x04, 0xfe,
99 0xfd, 0x06, 0xf8, 0x13, 0x3b, 0xf4, 0x07, 0xfc,
100 0xfb, 0x08, 0xf5, 0x1f, 0x34, 0xf1, 0x09, 0xfb,
101 0xfb, 0x09, 0xf2, 0x2b, 0x2a, 0xf1, 0x09, 0xfb,
102 0xfb, 0x09, 0xf2, 0x35, 0x1e, 0xf4, 0x08, 0xfb,
103 0xfc, 0x07, 0xf5, 0x3c, 0x12, 0xf7, 0x06, 0xfd,
104 0xfe, 0x04, 0xfa, 0x3f, 0x07, 0xfc, 0x03, 0xff
105 }
106 },
107 {
108 .min = 1228,
109 .max = 1331,
110 .coef = {
111 0xfd, 0x04, 0xfc, 0x05, 0x39, 0x05, 0xfc, 0x04,
112 0xfc, 0x06, 0xf9, 0x0c, 0x39, 0xfe, 0x00, 0x02,
113 0xfb, 0x08, 0xf6, 0x17, 0x35, 0xf9, 0x02, 0x00,
114 0xfc, 0x08, 0xf4, 0x20, 0x30, 0xf4, 0x05, 0xff,
115 0xfd, 0x07, 0xf4, 0x29, 0x28, 0xf3, 0x07, 0xfd,
116 0xff, 0x05, 0xf5, 0x31, 0x1f, 0xf3, 0x08, 0xfc,
117 0x00, 0x02, 0xf9, 0x38, 0x14, 0xf6, 0x08, 0xfb,
118 0x02, 0x00, 0xff, 0x3a, 0x0b, 0xf8, 0x06, 0xfc
119 }
120 },
121 {
122 .min = 1331,
123 .max = 1433,
124 .coef = {
125 0xfc, 0x06, 0xf9, 0x09, 0x34, 0x09, 0xf9, 0x06,
126 0xfd, 0x07, 0xf7, 0x10, 0x32, 0x02, 0xfc, 0x05,
127 0xfe, 0x07, 0xf6, 0x17, 0x2f, 0xfc, 0xff, 0x04,
128 0xff, 0x06, 0xf5, 0x20, 0x2a, 0xf9, 0x01, 0x02,
129 0x00, 0x04, 0xf6, 0x27, 0x25, 0xf6, 0x04, 0x00,
130 0x02, 0x01, 0xf9, 0x2d, 0x1d, 0xf5, 0x06, 0xff,
131 0x04, 0xff, 0xfd, 0x31, 0x15, 0xf5, 0x07, 0xfe,
132 0x05, 0xfc, 0x02, 0x35, 0x0d, 0xf7, 0x07, 0xfd
133 }
134 },
135 {
136 .min = 1433,
137 .max = 1536,
138 .coef = {
139 0xfe, 0x06, 0xf8, 0x0b, 0x30, 0x0b, 0xf8, 0x06,
140 0xff, 0x06, 0xf7, 0x12, 0x2d, 0x05, 0xfa, 0x06,
141 0x00, 0x04, 0xf6, 0x18, 0x2c, 0x00, 0xfc, 0x06,
142 0x01, 0x02, 0xf7, 0x1f, 0x27, 0xfd, 0xff, 0x04,
143 0x03, 0x00, 0xf9, 0x24, 0x24, 0xf9, 0x00, 0x03,
144 0x04, 0xff, 0xfd, 0x29, 0x1d, 0xf7, 0x02, 0x01,
145 0x06, 0xfc, 0x00, 0x2d, 0x17, 0xf6, 0x04, 0x00,
146 0x06, 0xfa, 0x05, 0x30, 0x0f, 0xf7, 0x06, 0xff
147 }
148 },
149 {
150 .min = 1536,
151 .max = 2048,
152 .coef = {
153 0x05, 0xfd, 0xfb, 0x13, 0x25, 0x13, 0xfb, 0xfd,
154 0x05, 0xfc, 0xfd, 0x17, 0x24, 0x0f, 0xf9, 0xff,
155 0x04, 0xfa, 0xff, 0x1b, 0x24, 0x0b, 0xf9, 0x00,
156 0x03, 0xf9, 0x01, 0x1f, 0x23, 0x08, 0xf8, 0x01,
157 0x02, 0xf9, 0x04, 0x22, 0x20, 0x04, 0xf9, 0x02,
158 0x01, 0xf8, 0x08, 0x25, 0x1d, 0x01, 0xf9, 0x03,
159 0x00, 0xf9, 0x0c, 0x25, 0x1a, 0xfe, 0xfa, 0x04,
160 0xff, 0xf9, 0x10, 0x26, 0x15, 0xfc, 0xfc, 0x05
161 }
162 },
163 {
164 .min = 2048,
165 .max = 3072,
166 .coef = {
167 0xfc, 0xfd, 0x06, 0x13, 0x18, 0x13, 0x06, 0xfd,
168 0xfc, 0xfe, 0x08, 0x15, 0x17, 0x12, 0x04, 0xfc,
169 0xfb, 0xfe, 0x0a, 0x16, 0x18, 0x10, 0x03, 0xfc,
170 0xfb, 0x00, 0x0b, 0x18, 0x17, 0x0f, 0x01, 0xfb,
171 0xfb, 0x00, 0x0d, 0x19, 0x17, 0x0d, 0x00, 0xfb,
172 0xfb, 0x01, 0x0f, 0x19, 0x16, 0x0b, 0x00, 0xfb,
173 0xfc, 0x03, 0x11, 0x19, 0x15, 0x09, 0xfe, 0xfb,
174 0xfc, 0x04, 0x12, 0x1a, 0x12, 0x08, 0xfe, 0xfc
175 }
176 },
177 {
178 .min = 3072,
179 .max = 4096,
180 .coef = {
181 0xfe, 0x02, 0x09, 0x0f, 0x0e, 0x0f, 0x09, 0x02,
182 0xff, 0x02, 0x09, 0x0f, 0x10, 0x0e, 0x08, 0x01,
183 0xff, 0x03, 0x0a, 0x10, 0x10, 0x0d, 0x07, 0x00,
184 0x00, 0x04, 0x0b, 0x10, 0x0f, 0x0c, 0x06, 0x00,
185 0x00, 0x05, 0x0c, 0x10, 0x0e, 0x0c, 0x05, 0x00,
186 0x00, 0x06, 0x0c, 0x11, 0x0e, 0x0b, 0x04, 0x00,
187 0x00, 0x07, 0x0d, 0x11, 0x0f, 0x0a, 0x03, 0xff,
188 0x01, 0x08, 0x0e, 0x11, 0x0e, 0x09, 0x02, 0xff
189 }
190 },
191 {
192 .min = 4096,
193 .max = 5120,
194 .coef = {
195 0x00, 0x04, 0x09, 0x0c, 0x0e, 0x0c, 0x09, 0x04,
196 0x01, 0x05, 0x09, 0x0c, 0x0d, 0x0c, 0x08, 0x04,
197 0x01, 0x05, 0x0a, 0x0c, 0x0e, 0x0b, 0x08, 0x03,
198 0x02, 0x06, 0x0a, 0x0d, 0x0c, 0x0b, 0x07, 0x03,
199 0x02, 0x07, 0x0a, 0x0d, 0x0d, 0x0a, 0x07, 0x02,
200 0x03, 0x07, 0x0b, 0x0d, 0x0c, 0x0a, 0x06, 0x02,
201 0x03, 0x08, 0x0b, 0x0d, 0x0d, 0x0a, 0x05, 0x01,
202 0x04, 0x08, 0x0c, 0x0d, 0x0c, 0x09, 0x05, 0x01
203 }
204 },
205 {
206 .min = 5120,
207 .max = 65535,
208 .coef = {
209 0x03, 0x06, 0x09, 0x0b, 0x09, 0x0b, 0x09, 0x06,
210 0x03, 0x06, 0x09, 0x0b, 0x0c, 0x0a, 0x08, 0x05,
211 0x03, 0x06, 0x09, 0x0b, 0x0c, 0x0a, 0x08, 0x05,
212 0x04, 0x07, 0x09, 0x0b, 0x0b, 0x0a, 0x08, 0x04,
213 0x04, 0x07, 0x0a, 0x0b, 0x0b, 0x0a, 0x07, 0x04,
214 0x04, 0x08, 0x0a, 0x0b, 0x0b, 0x09, 0x07, 0x04,
215 0x05, 0x08, 0x0a, 0x0b, 0x0c, 0x09, 0x06, 0x03,
216 0x05, 0x08, 0x0a, 0x0b, 0x0c, 0x09, 0x06, 0x03
217 }
218 }
219};
220
221#define NB_H_FILTER ARRAY_SIZE(bdisp_h_spec)
222
223
224static const struct bdisp_filter_v_spec bdisp_v_spec[] = {
225 {
226 .min = 0,
227 .max = 1024,
228 .coef = {
229 0x00, 0x00, 0x40, 0x00, 0x00,
230 0x00, 0x06, 0x3d, 0xfd, 0x00,
231 0xfe, 0x0f, 0x38, 0xfb, 0x00,
232 0xfd, 0x19, 0x2f, 0xfb, 0x00,
233 0xfc, 0x24, 0x24, 0xfc, 0x00,
234 0xfb, 0x2f, 0x19, 0xfd, 0x00,
235 0xfb, 0x38, 0x0f, 0xfe, 0x00,
236 0xfd, 0x3d, 0x06, 0x00, 0x00
237 }
238 },
239 {
240 .min = 1024,
241 .max = 1331,
242 .coef = {
243 0xfc, 0x05, 0x3e, 0x05, 0xfc,
244 0xf8, 0x0e, 0x3b, 0xff, 0x00,
245 0xf5, 0x18, 0x38, 0xf9, 0x02,
246 0xf4, 0x21, 0x31, 0xf5, 0x05,
247 0xf4, 0x2a, 0x27, 0xf4, 0x07,
248 0xf6, 0x30, 0x1e, 0xf4, 0x08,
249 0xf9, 0x35, 0x15, 0xf6, 0x07,
250 0xff, 0x37, 0x0b, 0xf9, 0x06
251 }
252 },
253 {
254 .min = 1331,
255 .max = 1433,
256 .coef = {
257 0xf8, 0x0a, 0x3c, 0x0a, 0xf8,
258 0xf6, 0x12, 0x3b, 0x02, 0xfb,
259 0xf4, 0x1b, 0x35, 0xfd, 0xff,
260 0xf4, 0x23, 0x30, 0xf8, 0x01,
261 0xf6, 0x29, 0x27, 0xf6, 0x04,
262 0xf9, 0x2e, 0x1e, 0xf5, 0x06,
263 0xfd, 0x31, 0x16, 0xf6, 0x06,
264 0x02, 0x32, 0x0d, 0xf8, 0x07
265 }
266 },
267 {
268 .min = 1433,
269 .max = 1536,
270 .coef = {
271 0xf6, 0x0e, 0x38, 0x0e, 0xf6,
272 0xf5, 0x15, 0x38, 0x06, 0xf8,
273 0xf5, 0x1d, 0x33, 0x00, 0xfb,
274 0xf6, 0x23, 0x2d, 0xfc, 0xfe,
275 0xf9, 0x28, 0x26, 0xf9, 0x00,
276 0xfc, 0x2c, 0x1e, 0xf7, 0x03,
277 0x00, 0x2e, 0x18, 0xf6, 0x04,
278 0x05, 0x2e, 0x11, 0xf7, 0x05
279 }
280 },
281 {
282 .min = 1536,
283 .max = 2048,
284 .coef = {
285 0xfb, 0x13, 0x24, 0x13, 0xfb,
286 0xfd, 0x17, 0x23, 0x0f, 0xfa,
287 0xff, 0x1a, 0x23, 0x0b, 0xf9,
288 0x01, 0x1d, 0x22, 0x07, 0xf9,
289 0x04, 0x20, 0x1f, 0x04, 0xf9,
290 0x07, 0x22, 0x1c, 0x01, 0xfa,
291 0x0b, 0x24, 0x17, 0xff, 0xfb,
292 0x0f, 0x24, 0x14, 0xfd, 0xfc
293 }
294 },
295 {
296 .min = 2048,
297 .max = 3072,
298 .coef = {
299 0x05, 0x10, 0x16, 0x10, 0x05,
300 0x06, 0x11, 0x16, 0x0f, 0x04,
301 0x08, 0x13, 0x15, 0x0e, 0x02,
302 0x09, 0x14, 0x16, 0x0c, 0x01,
303 0x0b, 0x15, 0x15, 0x0b, 0x00,
304 0x0d, 0x16, 0x13, 0x0a, 0x00,
305 0x0f, 0x17, 0x13, 0x08, 0xff,
306 0x11, 0x18, 0x12, 0x07, 0xfe
307 }
308 },
309 {
310 .min = 3072,
311 .max = 4096,
312 .coef = {
313 0x09, 0x0f, 0x10, 0x0f, 0x09,
314 0x09, 0x0f, 0x12, 0x0e, 0x08,
315 0x0a, 0x10, 0x11, 0x0e, 0x07,
316 0x0b, 0x11, 0x11, 0x0d, 0x06,
317 0x0c, 0x11, 0x12, 0x0c, 0x05,
318 0x0d, 0x12, 0x11, 0x0c, 0x04,
319 0x0e, 0x12, 0x11, 0x0b, 0x04,
320 0x0f, 0x13, 0x11, 0x0a, 0x03
321 }
322 },
323 {
324 .min = 4096,
325 .max = 5120,
326 .coef = {
327 0x0a, 0x0e, 0x10, 0x0e, 0x0a,
328 0x0b, 0x0e, 0x0f, 0x0e, 0x0a,
329 0x0b, 0x0f, 0x10, 0x0d, 0x09,
330 0x0c, 0x0f, 0x10, 0x0d, 0x08,
331 0x0d, 0x0f, 0x0f, 0x0d, 0x08,
332 0x0d, 0x10, 0x10, 0x0c, 0x07,
333 0x0e, 0x10, 0x0f, 0x0c, 0x07,
334 0x0f, 0x10, 0x10, 0x0b, 0x06
335 }
336 },
337 {
338 .min = 5120,
339 .max = 65535,
340 .coef = {
341 0x0b, 0x0e, 0x0e, 0x0e, 0x0b,
342 0x0b, 0x0e, 0x0f, 0x0d, 0x0b,
343 0x0c, 0x0e, 0x0f, 0x0d, 0x0a,
344 0x0c, 0x0e, 0x0f, 0x0d, 0x0a,
345 0x0d, 0x0f, 0x0e, 0x0d, 0x09,
346 0x0d, 0x0f, 0x0f, 0x0c, 0x09,
347 0x0e, 0x0f, 0x0e, 0x0c, 0x09,
348 0x0e, 0x0f, 0x0f, 0x0c, 0x08
349 }
350 }
351};
352
353#define NB_V_FILTER ARRAY_SIZE(bdisp_v_spec)
354
50static struct bdisp_filter_addr bdisp_h_filter[NB_H_FILTER]; 355static struct bdisp_filter_addr bdisp_h_filter[NB_H_FILTER];
51static struct bdisp_filter_addr bdisp_v_filter[NB_V_FILTER]; 356static struct bdisp_filter_addr bdisp_v_filter[NB_V_FILTER];
52 357
diff --git a/drivers/media/platform/sti/bdisp/bdisp-v4l2.c b/drivers/media/platform/sti/bdisp/bdisp-v4l2.c
index d12a419c044a..3b1ac687d0df 100644
--- a/drivers/media/platform/sti/bdisp/bdisp-v4l2.c
+++ b/drivers/media/platform/sti/bdisp/bdisp-v4l2.c
@@ -439,7 +439,7 @@ static void bdisp_ctrls_delete(struct bdisp_ctx *ctx)
439 439
440static int bdisp_queue_setup(struct vb2_queue *vq, 440static int bdisp_queue_setup(struct vb2_queue *vq,
441 unsigned int *nb_buf, unsigned int *nb_planes, 441 unsigned int *nb_buf, unsigned int *nb_planes,
442 unsigned int sizes[], void *allocators[]) 442 unsigned int sizes[], struct device *alloc_devs[])
443{ 443{
444 struct bdisp_ctx *ctx = vb2_get_drv_priv(vq); 444 struct bdisp_ctx *ctx = vb2_get_drv_priv(vq);
445 struct bdisp_frame *frame = ctx_get_frame(ctx, vq->type); 445 struct bdisp_frame *frame = ctx_get_frame(ctx, vq->type);
@@ -453,7 +453,6 @@ static int bdisp_queue_setup(struct vb2_queue *vq,
453 dev_err(ctx->bdisp_dev->dev, "Invalid format\n"); 453 dev_err(ctx->bdisp_dev->dev, "Invalid format\n");
454 return -EINVAL; 454 return -EINVAL;
455 } 455 }
456 allocators[0] = ctx->bdisp_dev->alloc_ctx;
457 456
458 if (*nb_planes) 457 if (*nb_planes)
459 return sizes[0] < frame->sizeimage ? -EINVAL : 0; 458 return sizes[0] < frame->sizeimage ? -EINVAL : 0;
@@ -553,6 +552,7 @@ static int queue_init(void *priv,
553 src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer); 552 src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
554 src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY; 553 src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
555 src_vq->lock = &ctx->bdisp_dev->lock; 554 src_vq->lock = &ctx->bdisp_dev->lock;
555 src_vq->dev = ctx->bdisp_dev->v4l2_dev.dev;
556 556
557 ret = vb2_queue_init(src_vq); 557 ret = vb2_queue_init(src_vq);
558 if (ret) 558 if (ret)
@@ -567,6 +567,7 @@ static int queue_init(void *priv,
567 dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer); 567 dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
568 dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY; 568 dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
569 dst_vq->lock = &ctx->bdisp_dev->lock; 569 dst_vq->lock = &ctx->bdisp_dev->lock;
570 dst_vq->dev = ctx->bdisp_dev->v4l2_dev.dev;
570 571
571 return vb2_queue_init(dst_vq); 572 return vb2_queue_init(dst_vq);
572} 573}
@@ -1269,8 +1270,6 @@ static int bdisp_remove(struct platform_device *pdev)
1269 1270
1270 bdisp_hw_free_filters(bdisp->dev); 1271 bdisp_hw_free_filters(bdisp->dev);
1271 1272
1272 vb2_dma_contig_cleanup_ctx(bdisp->alloc_ctx);
1273
1274 pm_runtime_disable(&pdev->dev); 1273 pm_runtime_disable(&pdev->dev);
1275 1274
1276 bdisp_debugfs_remove(bdisp); 1275 bdisp_debugfs_remove(bdisp);
@@ -1371,18 +1370,11 @@ static int bdisp_probe(struct platform_device *pdev)
1371 goto err_dbg; 1370 goto err_dbg;
1372 } 1371 }
1373 1372
1374 /* Continuous memory allocator */
1375 bdisp->alloc_ctx = vb2_dma_contig_init_ctx(dev);
1376 if (IS_ERR(bdisp->alloc_ctx)) {
1377 ret = PTR_ERR(bdisp->alloc_ctx);
1378 goto err_pm;
1379 }
1380
1381 /* Filters */ 1373 /* Filters */
1382 if (bdisp_hw_alloc_filters(bdisp->dev)) { 1374 if (bdisp_hw_alloc_filters(bdisp->dev)) {
1383 dev_err(bdisp->dev, "no memory for filters\n"); 1375 dev_err(bdisp->dev, "no memory for filters\n");
1384 ret = -ENOMEM; 1376 ret = -ENOMEM;
1385 goto err_vb2_dma; 1377 goto err_pm;
1386 } 1378 }
1387 1379
1388 /* Register */ 1380 /* Register */
@@ -1401,8 +1393,6 @@ static int bdisp_probe(struct platform_device *pdev)
1401 1393
1402err_filter: 1394err_filter:
1403 bdisp_hw_free_filters(bdisp->dev); 1395 bdisp_hw_free_filters(bdisp->dev);
1404err_vb2_dma:
1405 vb2_dma_contig_cleanup_ctx(bdisp->alloc_ctx);
1406err_pm: 1396err_pm:
1407 pm_runtime_put(dev); 1397 pm_runtime_put(dev);
1408err_dbg: 1398err_dbg:
diff --git a/drivers/media/platform/sti/bdisp/bdisp.h b/drivers/media/platform/sti/bdisp/bdisp.h
index 0cf98577222c..b3fbf9902595 100644
--- a/drivers/media/platform/sti/bdisp/bdisp.h
+++ b/drivers/media/platform/sti/bdisp/bdisp.h
@@ -175,7 +175,6 @@ struct bdisp_dbg {
175 * @id: device index 175 * @id: device index
176 * @m2m: memory-to-memory V4L2 device information 176 * @m2m: memory-to-memory V4L2 device information
177 * @state: flags used to synchronize m2m and capture mode operation 177 * @state: flags used to synchronize m2m and capture mode operation
178 * @alloc_ctx: videobuf2 memory allocator context
179 * @clock: IP clock 178 * @clock: IP clock
180 * @regs: registers 179 * @regs: registers
181 * @irq_queue: interrupt handler waitqueue 180 * @irq_queue: interrupt handler waitqueue
@@ -193,7 +192,6 @@ struct bdisp_dev {
193 u16 id; 192 u16 id;
194 struct bdisp_m2m_device m2m; 193 struct bdisp_m2m_device m2m;
195 unsigned long state; 194 unsigned long state;
196 struct vb2_alloc_ctx *alloc_ctx;
197 struct clk *clock; 195 struct clk *clock;
198 void __iomem *regs; 196 void __iomem *regs;
199 wait_queue_head_t irq_queue; 197 wait_queue_head_t irq_queue;
diff --git a/drivers/media/platform/sti/c8sectpfe/c8sectpfe-core.c b/drivers/media/platform/sti/c8sectpfe/c8sectpfe-core.c
index 7dddf77a62cf..30c148b9d65e 100644
--- a/drivers/media/platform/sti/c8sectpfe/c8sectpfe-core.c
+++ b/drivers/media/platform/sti/c8sectpfe/c8sectpfe-core.c
@@ -1161,6 +1161,7 @@ static int load_c8sectpfe_fw(struct c8sectpfei *fei)
1161 if (err) { 1161 if (err) {
1162 dev_err(fei->dev, "c8sectpfe_elf_sanity_check failed err=(%d)\n" 1162 dev_err(fei->dev, "c8sectpfe_elf_sanity_check failed err=(%d)\n"
1163 , err); 1163 , err);
1164 release_firmware(fw);
1164 return err; 1165 return err;
1165 } 1166 }
1166 1167
diff --git a/drivers/media/platform/ti-vpe/cal.c b/drivers/media/platform/ti-vpe/cal.c
index 82001e6b5553..e967fcfdc1d8 100644
--- a/drivers/media/platform/ti-vpe/cal.c
+++ b/drivers/media/platform/ti-vpe/cal.c
@@ -287,7 +287,6 @@ struct cal_ctx {
287 /* Several counters */ 287 /* Several counters */
288 unsigned long jiffies; 288 unsigned long jiffies;
289 289
290 struct vb2_alloc_ctx *alloc_ctx;
291 struct cal_dmaqueue vidq; 290 struct cal_dmaqueue vidq;
292 291
293 /* Input Number */ 292 /* Input Number */
@@ -1226,14 +1225,13 @@ static int cal_enum_frameintervals(struct file *file, void *priv,
1226 */ 1225 */
1227static int cal_queue_setup(struct vb2_queue *vq, 1226static int cal_queue_setup(struct vb2_queue *vq,
1228 unsigned int *nbuffers, unsigned int *nplanes, 1227 unsigned int *nbuffers, unsigned int *nplanes,
1229 unsigned int sizes[], void *alloc_ctxs[]) 1228 unsigned int sizes[], struct device *alloc_devs[])
1230{ 1229{
1231 struct cal_ctx *ctx = vb2_get_drv_priv(vq); 1230 struct cal_ctx *ctx = vb2_get_drv_priv(vq);
1232 unsigned size = ctx->v_fmt.fmt.pix.sizeimage; 1231 unsigned size = ctx->v_fmt.fmt.pix.sizeimage;
1233 1232
1234 if (vq->num_buffers + *nbuffers < 3) 1233 if (vq->num_buffers + *nbuffers < 3)
1235 *nbuffers = 3 - vq->num_buffers; 1234 *nbuffers = 3 - vq->num_buffers;
1236 alloc_ctxs[0] = ctx->alloc_ctx;
1237 1235
1238 if (*nplanes) { 1236 if (*nplanes) {
1239 if (sizes[0] < size) 1237 if (sizes[0] < size)
@@ -1551,6 +1549,7 @@ static int cal_complete_ctx(struct cal_ctx *ctx)
1551 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC; 1549 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1552 q->lock = &ctx->mutex; 1550 q->lock = &ctx->mutex;
1553 q->min_buffers_needed = 3; 1551 q->min_buffers_needed = 3;
1552 q->dev = ctx->v4l2_dev.dev;
1554 1553
1555 ret = vb2_queue_init(q); 1554 ret = vb2_queue_init(q);
1556 if (ret) 1555 if (ret)
@@ -1578,18 +1577,7 @@ static int cal_complete_ctx(struct cal_ctx *ctx)
1578 v4l2_info(&ctx->v4l2_dev, "V4L2 device registered as %s\n", 1577 v4l2_info(&ctx->v4l2_dev, "V4L2 device registered as %s\n",
1579 video_device_node_name(vfd)); 1578 video_device_node_name(vfd));
1580 1579
1581 ctx->alloc_ctx = vb2_dma_contig_init_ctx(vfd->v4l2_dev->dev);
1582 if (IS_ERR(ctx->alloc_ctx)) {
1583 ctx_err(ctx, "Failed to alloc vb2 context\n");
1584 ret = PTR_ERR(ctx->alloc_ctx);
1585 goto vdev_unreg;
1586 }
1587
1588 return 0; 1580 return 0;
1589
1590vdev_unreg:
1591 video_unregister_device(vfd);
1592 return ret;
1593} 1581}
1594 1582
1595static struct device_node * 1583static struct device_node *
@@ -1914,7 +1902,6 @@ static int cal_remove(struct platform_device *pdev)
1914 video_device_node_name(&ctx->vdev)); 1902 video_device_node_name(&ctx->vdev));
1915 camerarx_phy_disable(ctx); 1903 camerarx_phy_disable(ctx);
1916 v4l2_async_notifier_unregister(&ctx->notifier); 1904 v4l2_async_notifier_unregister(&ctx->notifier);
1917 vb2_dma_contig_cleanup_ctx(ctx->alloc_ctx);
1918 v4l2_ctrl_handler_free(&ctx->ctrl_handler); 1905 v4l2_ctrl_handler_free(&ctx->ctrl_handler);
1919 v4l2_device_unregister(&ctx->v4l2_dev); 1906 v4l2_device_unregister(&ctx->v4l2_dev);
1920 video_unregister_device(&ctx->vdev); 1907 video_unregister_device(&ctx->vdev);
diff --git a/drivers/media/platform/ti-vpe/vpe.c b/drivers/media/platform/ti-vpe/vpe.c
index 1fa00c2cf3d7..55a1458ac783 100644
--- a/drivers/media/platform/ti-vpe/vpe.c
+++ b/drivers/media/platform/ti-vpe/vpe.c
@@ -362,7 +362,6 @@ struct vpe_dev {
362 void __iomem *base; 362 void __iomem *base;
363 struct resource *res; 363 struct resource *res;
364 364
365 struct vb2_alloc_ctx *alloc_ctx;
366 struct vpdma_data *vpdma; /* vpdma data handle */ 365 struct vpdma_data *vpdma; /* vpdma data handle */
367 struct sc_data *sc; /* scaler data handle */ 366 struct sc_data *sc; /* scaler data handle */
368 struct csc_data *csc; /* csc data handle */ 367 struct csc_data *csc; /* csc data handle */
@@ -1797,7 +1796,7 @@ static const struct v4l2_ioctl_ops vpe_ioctl_ops = {
1797 */ 1796 */
1798static int vpe_queue_setup(struct vb2_queue *vq, 1797static int vpe_queue_setup(struct vb2_queue *vq,
1799 unsigned int *nbuffers, unsigned int *nplanes, 1798 unsigned int *nbuffers, unsigned int *nplanes,
1800 unsigned int sizes[], void *alloc_ctxs[]) 1799 unsigned int sizes[], struct device *alloc_devs[])
1801{ 1800{
1802 int i; 1801 int i;
1803 struct vpe_ctx *ctx = vb2_get_drv_priv(vq); 1802 struct vpe_ctx *ctx = vb2_get_drv_priv(vq);
@@ -1807,10 +1806,8 @@ static int vpe_queue_setup(struct vb2_queue *vq,
1807 1806
1808 *nplanes = q_data->fmt->coplanar ? 2 : 1; 1807 *nplanes = q_data->fmt->coplanar ? 2 : 1;
1809 1808
1810 for (i = 0; i < *nplanes; i++) { 1809 for (i = 0; i < *nplanes; i++)
1811 sizes[i] = q_data->sizeimage[i]; 1810 sizes[i] = q_data->sizeimage[i];
1812 alloc_ctxs[i] = ctx->dev->alloc_ctx;
1813 }
1814 1811
1815 vpe_dbg(ctx->dev, "get %d buffer(s) of size %d", *nbuffers, 1812 vpe_dbg(ctx->dev, "get %d buffer(s) of size %d", *nbuffers,
1816 sizes[VPE_LUMA]); 1813 sizes[VPE_LUMA]);
@@ -1907,6 +1904,7 @@ static int queue_init(void *priv, struct vb2_queue *src_vq,
1907 src_vq->mem_ops = &vb2_dma_contig_memops; 1904 src_vq->mem_ops = &vb2_dma_contig_memops;
1908 src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY; 1905 src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1909 src_vq->lock = &dev->dev_mutex; 1906 src_vq->lock = &dev->dev_mutex;
1907 src_vq->dev = dev->v4l2_dev.dev;
1910 1908
1911 ret = vb2_queue_init(src_vq); 1909 ret = vb2_queue_init(src_vq);
1912 if (ret) 1910 if (ret)
@@ -1921,6 +1919,7 @@ static int queue_init(void *priv, struct vb2_queue *src_vq,
1921 dst_vq->mem_ops = &vb2_dma_contig_memops; 1919 dst_vq->mem_ops = &vb2_dma_contig_memops;
1922 dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY; 1920 dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1923 dst_vq->lock = &dev->dev_mutex; 1921 dst_vq->lock = &dev->dev_mutex;
1922 dst_vq->dev = dev->v4l2_dev.dev;
1924 1923
1925 return vb2_queue_init(dst_vq); 1924 return vb2_queue_init(dst_vq);
1926} 1925}
@@ -2161,7 +2160,6 @@ static void vpe_fw_cb(struct platform_device *pdev)
2161 vpe_runtime_put(pdev); 2160 vpe_runtime_put(pdev);
2162 pm_runtime_disable(&pdev->dev); 2161 pm_runtime_disable(&pdev->dev);
2163 v4l2_m2m_release(dev->m2m_dev); 2162 v4l2_m2m_release(dev->m2m_dev);
2164 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
2165 v4l2_device_unregister(&dev->v4l2_dev); 2163 v4l2_device_unregister(&dev->v4l2_dev);
2166 2164
2167 return; 2165 return;
@@ -2213,18 +2211,11 @@ static int vpe_probe(struct platform_device *pdev)
2213 2211
2214 platform_set_drvdata(pdev, dev); 2212 platform_set_drvdata(pdev, dev);
2215 2213
2216 dev->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
2217 if (IS_ERR(dev->alloc_ctx)) {
2218 vpe_err(dev, "Failed to alloc vb2 context\n");
2219 ret = PTR_ERR(dev->alloc_ctx);
2220 goto v4l2_dev_unreg;
2221 }
2222
2223 dev->m2m_dev = v4l2_m2m_init(&m2m_ops); 2214 dev->m2m_dev = v4l2_m2m_init(&m2m_ops);
2224 if (IS_ERR(dev->m2m_dev)) { 2215 if (IS_ERR(dev->m2m_dev)) {
2225 vpe_err(dev, "Failed to init mem2mem device\n"); 2216 vpe_err(dev, "Failed to init mem2mem device\n");
2226 ret = PTR_ERR(dev->m2m_dev); 2217 ret = PTR_ERR(dev->m2m_dev);
2227 goto rel_ctx; 2218 goto v4l2_dev_unreg;
2228 } 2219 }
2229 2220
2230 pm_runtime_enable(&pdev->dev); 2221 pm_runtime_enable(&pdev->dev);
@@ -2269,8 +2260,6 @@ runtime_put:
2269rel_m2m: 2260rel_m2m:
2270 pm_runtime_disable(&pdev->dev); 2261 pm_runtime_disable(&pdev->dev);
2271 v4l2_m2m_release(dev->m2m_dev); 2262 v4l2_m2m_release(dev->m2m_dev);
2272rel_ctx:
2273 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
2274v4l2_dev_unreg: 2263v4l2_dev_unreg:
2275 v4l2_device_unregister(&dev->v4l2_dev); 2264 v4l2_device_unregister(&dev->v4l2_dev);
2276 2265
@@ -2286,7 +2275,6 @@ static int vpe_remove(struct platform_device *pdev)
2286 v4l2_m2m_release(dev->m2m_dev); 2275 v4l2_m2m_release(dev->m2m_dev);
2287 video_unregister_device(&dev->vfd); 2276 video_unregister_device(&dev->vfd);
2288 v4l2_device_unregister(&dev->v4l2_dev); 2277 v4l2_device_unregister(&dev->v4l2_dev);
2289 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
2290 2278
2291 vpe_set_clock_enable(dev, 0); 2279 vpe_set_clock_enable(dev, 0);
2292 vpe_runtime_put(pdev); 2280 vpe_runtime_put(pdev);
diff --git a/drivers/media/platform/via-camera.c b/drivers/media/platform/via-camera.c
index 1254f7e4d732..7ca12deba89c 100644
--- a/drivers/media/platform/via-camera.c
+++ b/drivers/media/platform/via-camera.c
@@ -240,7 +240,7 @@ static int viacam_set_flip(struct via_camera *cam)
240 memset(&ctrl, 0, sizeof(ctrl)); 240 memset(&ctrl, 0, sizeof(ctrl));
241 ctrl.id = V4L2_CID_VFLIP; 241 ctrl.id = V4L2_CID_VFLIP;
242 ctrl.value = flip_image; 242 ctrl.value = flip_image;
243 return sensor_call(cam, core, s_ctrl, &ctrl); 243 return v4l2_s_ctrl(NULL, cam->sensor->ctrl_handler, &ctrl);
244} 244}
245 245
246/* 246/*
diff --git a/drivers/media/platform/vim2m.c b/drivers/media/platform/vim2m.c
index c4b5fab83666..6b17015048ae 100644
--- a/drivers/media/platform/vim2m.c
+++ b/drivers/media/platform/vim2m.c
@@ -711,7 +711,7 @@ static const struct v4l2_ioctl_ops vim2m_ioctl_ops = {
711 711
712static int vim2m_queue_setup(struct vb2_queue *vq, 712static int vim2m_queue_setup(struct vb2_queue *vq,
713 unsigned int *nbuffers, unsigned int *nplanes, 713 unsigned int *nbuffers, unsigned int *nplanes,
714 unsigned int sizes[], void *alloc_ctxs[]) 714 unsigned int sizes[], struct device *alloc_devs[])
715{ 715{
716 struct vim2m_ctx *ctx = vb2_get_drv_priv(vq); 716 struct vim2m_ctx *ctx = vb2_get_drv_priv(vq);
717 struct vim2m_q_data *q_data; 717 struct vim2m_q_data *q_data;
@@ -731,11 +731,6 @@ static int vim2m_queue_setup(struct vb2_queue *vq,
731 *nplanes = 1; 731 *nplanes = 1;
732 sizes[0] = size; 732 sizes[0] = size;
733 733
734 /*
735 * videobuf2-vmalloc allocator is context-less so no need to set
736 * alloc_ctxs array.
737 */
738
739 dprintk(ctx->dev, "get %d buffer(s) of size %d each.\n", count, size); 734 dprintk(ctx->dev, "get %d buffer(s) of size %d each.\n", count, size);
740 735
741 return 0; 736 return 0;
diff --git a/drivers/media/platform/vivid/Kconfig b/drivers/media/platform/vivid/Kconfig
index f535f576913d..8e6918c5c87c 100644
--- a/drivers/media/platform/vivid/Kconfig
+++ b/drivers/media/platform/vivid/Kconfig
@@ -6,6 +6,7 @@ config VIDEO_VIVID
6 select FB_CFB_FILLRECT 6 select FB_CFB_FILLRECT
7 select FB_CFB_COPYAREA 7 select FB_CFB_COPYAREA
8 select FB_CFB_IMAGEBLIT 8 select FB_CFB_IMAGEBLIT
9 select MEDIA_CEC_EDID
9 select VIDEOBUF2_VMALLOC 10 select VIDEOBUF2_VMALLOC
10 select VIDEO_V4L2_TPG 11 select VIDEO_V4L2_TPG
11 default n 12 default n
@@ -22,6 +23,13 @@ config VIDEO_VIVID
22 Say Y here if you want to test video apps or debug V4L devices. 23 Say Y here if you want to test video apps or debug V4L devices.
23 When in doubt, say N. 24 When in doubt, say N.
24 25
26config VIDEO_VIVID_CEC
27 bool "Enable CEC emulation support"
28 depends on VIDEO_VIVID && MEDIA_CEC
29 ---help---
30 When selected the vivid module will emulate the optional
31 HDMI CEC feature.
32
25config VIDEO_VIVID_MAX_DEVS 33config VIDEO_VIVID_MAX_DEVS
26 int "Maximum number of devices" 34 int "Maximum number of devices"
27 depends on VIDEO_VIVID 35 depends on VIDEO_VIVID
diff --git a/drivers/media/platform/vivid/Makefile b/drivers/media/platform/vivid/Makefile
index 633c8a1b2c27..29738810e3ee 100644
--- a/drivers/media/platform/vivid/Makefile
+++ b/drivers/media/platform/vivid/Makefile
@@ -3,4 +3,8 @@ vivid-objs := vivid-core.o vivid-ctrls.o vivid-vid-common.o vivid-vbi-gen.o \
3 vivid-radio-rx.o vivid-radio-tx.o vivid-radio-common.o \ 3 vivid-radio-rx.o vivid-radio-tx.o vivid-radio-common.o \
4 vivid-rds-gen.o vivid-sdr-cap.o vivid-vbi-cap.o vivid-vbi-out.o \ 4 vivid-rds-gen.o vivid-sdr-cap.o vivid-vbi-cap.o vivid-vbi-out.o \
5 vivid-osd.o 5 vivid-osd.o
6ifeq ($(CONFIG_VIDEO_VIVID_CEC),y)
7 vivid-objs += vivid-cec.o
8endif
9
6obj-$(CONFIG_VIDEO_VIVID) += vivid.o 10obj-$(CONFIG_VIDEO_VIVID) += vivid.o
diff --git a/drivers/media/platform/vivid/vivid-cec.c b/drivers/media/platform/vivid/vivid-cec.c
new file mode 100644
index 000000000000..66aa7292076b
--- /dev/null
+++ b/drivers/media/platform/vivid/vivid-cec.c
@@ -0,0 +1,241 @@
1/*
2 * vivid-cec.c - A Virtual Video Test Driver, cec emulation
3 *
4 * Copyright 2016 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
5 *
6 * This program is free software; you may redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
11 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
12 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
13 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
14 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
15 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
16 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17 * SOFTWARE.
18 */
19
20#include <media/cec.h>
21
22#include "vivid-core.h"
23#include "vivid-cec.h"
24
25void vivid_cec_bus_free_work(struct vivid_dev *dev)
26{
27 spin_lock(&dev->cec_slock);
28 while (!list_empty(&dev->cec_work_list)) {
29 struct vivid_cec_work *cw =
30 list_first_entry(&dev->cec_work_list,
31 struct vivid_cec_work, list);
32
33 spin_unlock(&dev->cec_slock);
34 cancel_delayed_work_sync(&cw->work);
35 spin_lock(&dev->cec_slock);
36 list_del(&cw->list);
37 cec_transmit_done(cw->adap, CEC_TX_STATUS_LOW_DRIVE, 0, 0, 1, 0);
38 kfree(cw);
39 }
40 spin_unlock(&dev->cec_slock);
41}
42
43static bool vivid_cec_find_dest_adap(struct vivid_dev *dev,
44 struct cec_adapter *adap, u8 dest)
45{
46 unsigned int i;
47
48 if (dest >= 0xf)
49 return false;
50
51 if (adap != dev->cec_rx_adap && dev->cec_rx_adap &&
52 dev->cec_rx_adap->is_configured &&
53 cec_has_log_addr(dev->cec_rx_adap, dest))
54 return true;
55
56 for (i = 0; i < MAX_OUTPUTS && dev->cec_tx_adap[i]; i++) {
57 if (adap == dev->cec_tx_adap[i])
58 continue;
59 if (!dev->cec_tx_adap[i]->is_configured)
60 continue;
61 if (cec_has_log_addr(dev->cec_tx_adap[i], dest))
62 return true;
63 }
64 return false;
65}
66
67static void vivid_cec_xfer_done_worker(struct work_struct *work)
68{
69 struct vivid_cec_work *cw =
70 container_of(work, struct vivid_cec_work, work.work);
71 struct vivid_dev *dev = cw->dev;
72 struct cec_adapter *adap = cw->adap;
73 u8 dest = cec_msg_destination(&cw->msg);
74 bool valid_dest;
75 unsigned int i;
76
77 valid_dest = cec_msg_is_broadcast(&cw->msg);
78 if (!valid_dest)
79 valid_dest = vivid_cec_find_dest_adap(dev, adap, dest);
80
81 cw->tx_status = valid_dest ? CEC_TX_STATUS_OK : CEC_TX_STATUS_NACK;
82 spin_lock(&dev->cec_slock);
83 dev->cec_xfer_time_jiffies = 0;
84 dev->cec_xfer_start_jiffies = 0;
85 list_del(&cw->list);
86 spin_unlock(&dev->cec_slock);
87 cec_transmit_done(cw->adap, cw->tx_status, 0, valid_dest ? 0 : 1, 0, 0);
88
89 /* Broadcast message */
90 if (adap != dev->cec_rx_adap)
91 cec_received_msg(dev->cec_rx_adap, &cw->msg);
92 for (i = 0; i < MAX_OUTPUTS && dev->cec_tx_adap[i]; i++)
93 if (adap != dev->cec_tx_adap[i])
94 cec_received_msg(dev->cec_tx_adap[i], &cw->msg);
95 kfree(cw);
96}
97
98static void vivid_cec_xfer_try_worker(struct work_struct *work)
99{
100 struct vivid_cec_work *cw =
101 container_of(work, struct vivid_cec_work, work.work);
102 struct vivid_dev *dev = cw->dev;
103
104 spin_lock(&dev->cec_slock);
105 if (dev->cec_xfer_time_jiffies) {
106 list_del(&cw->list);
107 spin_unlock(&dev->cec_slock);
108 cec_transmit_done(cw->adap, CEC_TX_STATUS_ARB_LOST, 1, 0, 0, 0);
109 kfree(cw);
110 } else {
111 INIT_DELAYED_WORK(&cw->work, vivid_cec_xfer_done_worker);
112 dev->cec_xfer_start_jiffies = jiffies;
113 dev->cec_xfer_time_jiffies = usecs_to_jiffies(cw->usecs);
114 spin_unlock(&dev->cec_slock);
115 schedule_delayed_work(&cw->work, dev->cec_xfer_time_jiffies);
116 }
117}
118
119static int vivid_cec_adap_enable(struct cec_adapter *adap, bool enable)
120{
121 return 0;
122}
123
124static int vivid_cec_adap_log_addr(struct cec_adapter *adap, u8 log_addr)
125{
126 return 0;
127}
128
129/*
130 * One data bit takes 2400 us, each byte needs 10 bits so that's 24000 us
131 * per byte.
132 */
133#define USECS_PER_BYTE 24000
134
135static int vivid_cec_adap_transmit(struct cec_adapter *adap, u8 attempts,
136 u32 signal_free_time, struct cec_msg *msg)
137{
138 struct vivid_dev *dev = adap->priv;
139 struct vivid_cec_work *cw = kzalloc(sizeof(*cw), GFP_KERNEL);
140 long delta_jiffies = 0;
141
142 if (cw == NULL)
143 return -ENOMEM;
144 cw->dev = dev;
145 cw->adap = adap;
146 cw->usecs = CEC_FREE_TIME_TO_USEC(signal_free_time) +
147 msg->len * USECS_PER_BYTE;
148 cw->msg = *msg;
149
150 spin_lock(&dev->cec_slock);
151 list_add(&cw->list, &dev->cec_work_list);
152 if (dev->cec_xfer_time_jiffies == 0) {
153 INIT_DELAYED_WORK(&cw->work, vivid_cec_xfer_done_worker);
154 dev->cec_xfer_start_jiffies = jiffies;
155 dev->cec_xfer_time_jiffies = usecs_to_jiffies(cw->usecs);
156 delta_jiffies = dev->cec_xfer_time_jiffies;
157 } else {
158 INIT_DELAYED_WORK(&cw->work, vivid_cec_xfer_try_worker);
159 delta_jiffies = dev->cec_xfer_start_jiffies +
160 dev->cec_xfer_time_jiffies - jiffies;
161 }
162 spin_unlock(&dev->cec_slock);
163 schedule_delayed_work(&cw->work, delta_jiffies < 0 ? 0 : delta_jiffies);
164 return 0;
165}
166
167static int vivid_received(struct cec_adapter *adap, struct cec_msg *msg)
168{
169 struct vivid_dev *dev = adap->priv;
170 struct cec_msg reply;
171 u8 dest = cec_msg_destination(msg);
172 u16 pa;
173 u8 disp_ctl;
174 char osd[14];
175
176 if (cec_msg_is_broadcast(msg))
177 dest = adap->log_addrs.log_addr[0];
178 cec_msg_init(&reply, dest, cec_msg_initiator(msg));
179
180 switch (cec_msg_opcode(msg)) {
181 case CEC_MSG_SET_STREAM_PATH:
182 if (cec_is_sink(adap))
183 return -ENOMSG;
184 cec_ops_set_stream_path(msg, &pa);
185 if (pa != adap->phys_addr)
186 return -ENOMSG;
187 cec_msg_active_source(&reply, adap->phys_addr);
188 cec_transmit_msg(adap, &reply, false);
189 break;
190 case CEC_MSG_SET_OSD_STRING:
191 if (!cec_is_sink(adap))
192 return -ENOMSG;
193 cec_ops_set_osd_string(msg, &disp_ctl, osd);
194 switch (disp_ctl) {
195 case CEC_OP_DISP_CTL_DEFAULT:
196 strcpy(dev->osd, osd);
197 dev->osd_jiffies = jiffies;
198 break;
199 case CEC_OP_DISP_CTL_UNTIL_CLEARED:
200 strcpy(dev->osd, osd);
201 dev->osd_jiffies = 0;
202 break;
203 case CEC_OP_DISP_CTL_CLEAR:
204 dev->osd[0] = 0;
205 dev->osd_jiffies = 0;
206 break;
207 default:
208 cec_msg_feature_abort(&reply, cec_msg_opcode(msg),
209 CEC_OP_ABORT_INVALID_OP);
210 cec_transmit_msg(adap, &reply, false);
211 break;
212 }
213 break;
214 default:
215 return -ENOMSG;
216 }
217 return 0;
218}
219
220static const struct cec_adap_ops vivid_cec_adap_ops = {
221 .adap_enable = vivid_cec_adap_enable,
222 .adap_log_addr = vivid_cec_adap_log_addr,
223 .adap_transmit = vivid_cec_adap_transmit,
224 .received = vivid_received,
225};
226
227struct cec_adapter *vivid_cec_alloc_adap(struct vivid_dev *dev,
228 unsigned int idx,
229 struct device *parent,
230 bool is_source)
231{
232 char name[sizeof(dev->vid_out_dev.name) + 2];
233 u32 caps = CEC_CAP_TRANSMIT | CEC_CAP_LOG_ADDRS |
234 CEC_CAP_PASSTHROUGH | CEC_CAP_RC | CEC_CAP_MONITOR_ALL;
235
236 snprintf(name, sizeof(name), "%s%d",
237 is_source ? dev->vid_out_dev.name : dev->vid_cap_dev.name,
238 idx);
239 return cec_allocate_adapter(&vivid_cec_adap_ops, dev,
240 name, caps, 1, parent);
241}
diff --git a/drivers/media/platform/vivid/vivid-cec.h b/drivers/media/platform/vivid/vivid-cec.h
new file mode 100644
index 000000000000..97892afa6b3b
--- /dev/null
+++ b/drivers/media/platform/vivid/vivid-cec.h
@@ -0,0 +1,33 @@
1/*
2 * vivid-cec.h - A Virtual Video Test Driver, cec emulation
3 *
4 * Copyright 2016 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
5 *
6 * This program is free software; you may redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
11 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
12 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
13 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
14 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
15 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
16 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17 * SOFTWARE.
18 */
19
20#ifdef CONFIG_VIDEO_VIVID_CEC
21struct cec_adapter *vivid_cec_alloc_adap(struct vivid_dev *dev,
22 unsigned int idx,
23 struct device *parent,
24 bool is_source);
25void vivid_cec_bus_free_work(struct vivid_dev *dev);
26
27#else
28
29static inline void vivid_cec_bus_free_work(struct vivid_dev *dev)
30{
31}
32
33#endif
diff --git a/drivers/media/platform/vivid/vivid-core.c b/drivers/media/platform/vivid/vivid-core.c
index c14da84af09b..7f937136c3f5 100644
--- a/drivers/media/platform/vivid/vivid-core.c
+++ b/drivers/media/platform/vivid/vivid-core.c
@@ -46,6 +46,7 @@
46#include "vivid-vbi-cap.h" 46#include "vivid-vbi-cap.h"
47#include "vivid-vbi-out.h" 47#include "vivid-vbi-out.h"
48#include "vivid-osd.h" 48#include "vivid-osd.h"
49#include "vivid-cec.h"
49#include "vivid-ctrls.h" 50#include "vivid-ctrls.h"
50 51
51#define VIVID_MODULE_NAME "vivid" 52#define VIVID_MODULE_NAME "vivid"
@@ -684,6 +685,11 @@ static int vivid_create_instance(struct platform_device *pdev, int inst)
684 dev->input_name_counter[i] = in_type_counter[dev->input_type[i]]++; 685 dev->input_name_counter[i] = in_type_counter[dev->input_type[i]]++;
685 } 686 }
686 dev->has_audio_inputs = in_type_counter[TV] && in_type_counter[SVID]; 687 dev->has_audio_inputs = in_type_counter[TV] && in_type_counter[SVID];
688 if (in_type_counter[HDMI] == 16) {
689 /* The CEC physical address only allows for max 15 inputs */
690 in_type_counter[HDMI]--;
691 dev->num_inputs--;
692 }
687 693
688 /* how many outputs do we have and of what type? */ 694 /* how many outputs do we have and of what type? */
689 dev->num_outputs = num_outputs[inst]; 695 dev->num_outputs = num_outputs[inst];
@@ -696,6 +702,15 @@ static int vivid_create_instance(struct platform_device *pdev, int inst)
696 dev->output_name_counter[i] = out_type_counter[dev->output_type[i]]++; 702 dev->output_name_counter[i] = out_type_counter[dev->output_type[i]]++;
697 } 703 }
698 dev->has_audio_outputs = out_type_counter[SVID]; 704 dev->has_audio_outputs = out_type_counter[SVID];
705 if (out_type_counter[HDMI] == 16) {
706 /*
707 * The CEC physical address only allows for max 15 inputs,
708 * so outputs are also limited to 15 to allow for easy
709 * CEC output to input mapping.
710 */
711 out_type_counter[HDMI]--;
712 dev->num_outputs--;
713 }
699 714
700 /* do we create a video capture device? */ 715 /* do we create a video capture device? */
701 dev->has_vid_cap = node_type & 0x0001; 716 dev->has_vid_cap = node_type & 0x0001;
@@ -1010,6 +1025,17 @@ static int vivid_create_instance(struct platform_device *pdev, int inst)
1010 INIT_LIST_HEAD(&dev->vbi_out_active); 1025 INIT_LIST_HEAD(&dev->vbi_out_active);
1011 INIT_LIST_HEAD(&dev->sdr_cap_active); 1026 INIT_LIST_HEAD(&dev->sdr_cap_active);
1012 1027
1028 INIT_LIST_HEAD(&dev->cec_work_list);
1029 spin_lock_init(&dev->cec_slock);
1030 /*
1031 * Same as create_singlethread_workqueue, but now I can use the
1032 * string formatting of alloc_ordered_workqueue.
1033 */
1034 dev->cec_workqueue =
1035 alloc_ordered_workqueue("vivid-%03d-cec", WQ_MEM_RECLAIM, inst);
1036 if (!dev->cec_workqueue)
1037 goto unreg_dev;
1038
1013 /* start creating the vb2 queues */ 1039 /* start creating the vb2 queues */
1014 if (dev->has_vid_cap) { 1040 if (dev->has_vid_cap) {
1015 /* initialize vid_cap queue */ 1041 /* initialize vid_cap queue */
@@ -1117,7 +1143,8 @@ static int vivid_create_instance(struct platform_device *pdev, int inst)
1117 /* finally start creating the device nodes */ 1143 /* finally start creating the device nodes */
1118 if (dev->has_vid_cap) { 1144 if (dev->has_vid_cap) {
1119 vfd = &dev->vid_cap_dev; 1145 vfd = &dev->vid_cap_dev;
1120 strlcpy(vfd->name, "vivid-vid-cap", sizeof(vfd->name)); 1146 snprintf(vfd->name, sizeof(vfd->name),
1147 "vivid-%03d-vid-cap", inst);
1121 vfd->fops = &vivid_fops; 1148 vfd->fops = &vivid_fops;
1122 vfd->ioctl_ops = &vivid_ioctl_ops; 1149 vfd->ioctl_ops = &vivid_ioctl_ops;
1123 vfd->device_caps = dev->vid_cap_caps; 1150 vfd->device_caps = dev->vid_cap_caps;
@@ -1133,6 +1160,27 @@ static int vivid_create_instance(struct platform_device *pdev, int inst)
1133 vfd->lock = &dev->mutex; 1160 vfd->lock = &dev->mutex;
1134 video_set_drvdata(vfd, dev); 1161 video_set_drvdata(vfd, dev);
1135 1162
1163#ifdef CONFIG_VIDEO_VIVID_CEC
1164 if (in_type_counter[HDMI]) {
1165 struct cec_adapter *adap;
1166
1167 adap = vivid_cec_alloc_adap(dev, 0, &pdev->dev, false);
1168 ret = PTR_ERR_OR_ZERO(adap);
1169 if (ret < 0)
1170 goto unreg_dev;
1171 dev->cec_rx_adap = adap;
1172 ret = cec_register_adapter(adap);
1173 if (ret < 0) {
1174 cec_delete_adapter(adap);
1175 dev->cec_rx_adap = NULL;
1176 goto unreg_dev;
1177 }
1178 cec_s_phys_addr(adap, 0, false);
1179 v4l2_info(&dev->v4l2_dev, "CEC adapter %s registered for HDMI input %d\n",
1180 dev_name(&adap->devnode.dev), i);
1181 }
1182#endif
1183
1136 ret = video_register_device(vfd, VFL_TYPE_GRABBER, vid_cap_nr[inst]); 1184 ret = video_register_device(vfd, VFL_TYPE_GRABBER, vid_cap_nr[inst]);
1137 if (ret < 0) 1185 if (ret < 0)
1138 goto unreg_dev; 1186 goto unreg_dev;
@@ -1141,8 +1189,13 @@ static int vivid_create_instance(struct platform_device *pdev, int inst)
1141 } 1189 }
1142 1190
1143 if (dev->has_vid_out) { 1191 if (dev->has_vid_out) {
1192#ifdef CONFIG_VIDEO_VIVID_CEC
1193 unsigned int bus_cnt = 0;
1194#endif
1195
1144 vfd = &dev->vid_out_dev; 1196 vfd = &dev->vid_out_dev;
1145 strlcpy(vfd->name, "vivid-vid-out", sizeof(vfd->name)); 1197 snprintf(vfd->name, sizeof(vfd->name),
1198 "vivid-%03d-vid-out", inst);
1146 vfd->vfl_dir = VFL_DIR_TX; 1199 vfd->vfl_dir = VFL_DIR_TX;
1147 vfd->fops = &vivid_fops; 1200 vfd->fops = &vivid_fops;
1148 vfd->ioctl_ops = &vivid_ioctl_ops; 1201 vfd->ioctl_ops = &vivid_ioctl_ops;
@@ -1159,6 +1212,35 @@ static int vivid_create_instance(struct platform_device *pdev, int inst)
1159 vfd->lock = &dev->mutex; 1212 vfd->lock = &dev->mutex;
1160 video_set_drvdata(vfd, dev); 1213 video_set_drvdata(vfd, dev);
1161 1214
1215#ifdef CONFIG_VIDEO_VIVID_CEC
1216 for (i = 0; i < dev->num_outputs; i++) {
1217 struct cec_adapter *adap;
1218
1219 if (dev->output_type[i] != HDMI)
1220 continue;
1221 dev->cec_output2bus_map[i] = bus_cnt;
1222 adap = vivid_cec_alloc_adap(dev, bus_cnt,
1223 &pdev->dev, true);
1224 ret = PTR_ERR_OR_ZERO(adap);
1225 if (ret < 0)
1226 goto unreg_dev;
1227 dev->cec_tx_adap[bus_cnt] = adap;
1228 ret = cec_register_adapter(adap);
1229 if (ret < 0) {
1230 cec_delete_adapter(adap);
1231 dev->cec_tx_adap[bus_cnt] = NULL;
1232 goto unreg_dev;
1233 }
1234 bus_cnt++;
1235 if (bus_cnt <= out_type_counter[HDMI])
1236 cec_s_phys_addr(adap, bus_cnt << 12, false);
1237 else
1238 cec_s_phys_addr(adap, 0x1000, false);
1239 v4l2_info(&dev->v4l2_dev, "CEC adapter %s registered for HDMI output %d\n",
1240 dev_name(&adap->devnode.dev), i);
1241 }
1242#endif
1243
1162 ret = video_register_device(vfd, VFL_TYPE_GRABBER, vid_out_nr[inst]); 1244 ret = video_register_device(vfd, VFL_TYPE_GRABBER, vid_out_nr[inst]);
1163 if (ret < 0) 1245 if (ret < 0)
1164 goto unreg_dev; 1246 goto unreg_dev;
@@ -1168,7 +1250,8 @@ static int vivid_create_instance(struct platform_device *pdev, int inst)
1168 1250
1169 if (dev->has_vbi_cap) { 1251 if (dev->has_vbi_cap) {
1170 vfd = &dev->vbi_cap_dev; 1252 vfd = &dev->vbi_cap_dev;
1171 strlcpy(vfd->name, "vivid-vbi-cap", sizeof(vfd->name)); 1253 snprintf(vfd->name, sizeof(vfd->name),
1254 "vivid-%03d-vbi-cap", inst);
1172 vfd->fops = &vivid_fops; 1255 vfd->fops = &vivid_fops;
1173 vfd->ioctl_ops = &vivid_ioctl_ops; 1256 vfd->ioctl_ops = &vivid_ioctl_ops;
1174 vfd->device_caps = dev->vbi_cap_caps; 1257 vfd->device_caps = dev->vbi_cap_caps;
@@ -1191,7 +1274,8 @@ static int vivid_create_instance(struct platform_device *pdev, int inst)
1191 1274
1192 if (dev->has_vbi_out) { 1275 if (dev->has_vbi_out) {
1193 vfd = &dev->vbi_out_dev; 1276 vfd = &dev->vbi_out_dev;
1194 strlcpy(vfd->name, "vivid-vbi-out", sizeof(vfd->name)); 1277 snprintf(vfd->name, sizeof(vfd->name),
1278 "vivid-%03d-vbi-out", inst);
1195 vfd->vfl_dir = VFL_DIR_TX; 1279 vfd->vfl_dir = VFL_DIR_TX;
1196 vfd->fops = &vivid_fops; 1280 vfd->fops = &vivid_fops;
1197 vfd->ioctl_ops = &vivid_ioctl_ops; 1281 vfd->ioctl_ops = &vivid_ioctl_ops;
@@ -1215,7 +1299,8 @@ static int vivid_create_instance(struct platform_device *pdev, int inst)
1215 1299
1216 if (dev->has_sdr_cap) { 1300 if (dev->has_sdr_cap) {
1217 vfd = &dev->sdr_cap_dev; 1301 vfd = &dev->sdr_cap_dev;
1218 strlcpy(vfd->name, "vivid-sdr-cap", sizeof(vfd->name)); 1302 snprintf(vfd->name, sizeof(vfd->name),
1303 "vivid-%03d-sdr-cap", inst);
1219 vfd->fops = &vivid_fops; 1304 vfd->fops = &vivid_fops;
1220 vfd->ioctl_ops = &vivid_ioctl_ops; 1305 vfd->ioctl_ops = &vivid_ioctl_ops;
1221 vfd->device_caps = dev->sdr_cap_caps; 1306 vfd->device_caps = dev->sdr_cap_caps;
@@ -1234,7 +1319,8 @@ static int vivid_create_instance(struct platform_device *pdev, int inst)
1234 1319
1235 if (dev->has_radio_rx) { 1320 if (dev->has_radio_rx) {
1236 vfd = &dev->radio_rx_dev; 1321 vfd = &dev->radio_rx_dev;
1237 strlcpy(vfd->name, "vivid-rad-rx", sizeof(vfd->name)); 1322 snprintf(vfd->name, sizeof(vfd->name),
1323 "vivid-%03d-rad-rx", inst);
1238 vfd->fops = &vivid_radio_fops; 1324 vfd->fops = &vivid_radio_fops;
1239 vfd->ioctl_ops = &vivid_ioctl_ops; 1325 vfd->ioctl_ops = &vivid_ioctl_ops;
1240 vfd->device_caps = dev->radio_rx_caps; 1326 vfd->device_caps = dev->radio_rx_caps;
@@ -1252,7 +1338,8 @@ static int vivid_create_instance(struct platform_device *pdev, int inst)
1252 1338
1253 if (dev->has_radio_tx) { 1339 if (dev->has_radio_tx) {
1254 vfd = &dev->radio_tx_dev; 1340 vfd = &dev->radio_tx_dev;
1255 strlcpy(vfd->name, "vivid-rad-tx", sizeof(vfd->name)); 1341 snprintf(vfd->name, sizeof(vfd->name),
1342 "vivid-%03d-rad-tx", inst);
1256 vfd->vfl_dir = VFL_DIR_TX; 1343 vfd->vfl_dir = VFL_DIR_TX;
1257 vfd->fops = &vivid_radio_fops; 1344 vfd->fops = &vivid_radio_fops;
1258 vfd->ioctl_ops = &vivid_ioctl_ops; 1345 vfd->ioctl_ops = &vivid_ioctl_ops;
@@ -1282,6 +1369,13 @@ unreg_dev:
1282 video_unregister_device(&dev->vbi_cap_dev); 1369 video_unregister_device(&dev->vbi_cap_dev);
1283 video_unregister_device(&dev->vid_out_dev); 1370 video_unregister_device(&dev->vid_out_dev);
1284 video_unregister_device(&dev->vid_cap_dev); 1371 video_unregister_device(&dev->vid_cap_dev);
1372 cec_unregister_adapter(dev->cec_rx_adap);
1373 for (i = 0; i < MAX_OUTPUTS; i++)
1374 cec_unregister_adapter(dev->cec_tx_adap[i]);
1375 if (dev->cec_workqueue) {
1376 vivid_cec_bus_free_work(dev);
1377 destroy_workqueue(dev->cec_workqueue);
1378 }
1285free_dev: 1379free_dev:
1286 v4l2_device_put(&dev->v4l2_dev); 1380 v4l2_device_put(&dev->v4l2_dev);
1287 return ret; 1381 return ret;
@@ -1331,8 +1425,7 @@ static int vivid_probe(struct platform_device *pdev)
1331static int vivid_remove(struct platform_device *pdev) 1425static int vivid_remove(struct platform_device *pdev)
1332{ 1426{
1333 struct vivid_dev *dev; 1427 struct vivid_dev *dev;
1334 unsigned i; 1428 unsigned int i, j;
1335
1336 1429
1337 for (i = 0; i < n_devs; i++) { 1430 for (i = 0; i < n_devs; i++) {
1338 dev = vivid_devs[i]; 1431 dev = vivid_devs[i];
@@ -1380,6 +1473,13 @@ static int vivid_remove(struct platform_device *pdev)
1380 unregister_framebuffer(&dev->fb_info); 1473 unregister_framebuffer(&dev->fb_info);
1381 vivid_fb_release_buffers(dev); 1474 vivid_fb_release_buffers(dev);
1382 } 1475 }
1476 cec_unregister_adapter(dev->cec_rx_adap);
1477 for (j = 0; j < MAX_OUTPUTS; j++)
1478 cec_unregister_adapter(dev->cec_tx_adap[j]);
1479 if (dev->cec_workqueue) {
1480 vivid_cec_bus_free_work(dev);
1481 destroy_workqueue(dev->cec_workqueue);
1482 }
1383 v4l2_device_put(&dev->v4l2_dev); 1483 v4l2_device_put(&dev->v4l2_dev);
1384 vivid_devs[i] = NULL; 1484 vivid_devs[i] = NULL;
1385 } 1485 }
diff --git a/drivers/media/platform/vivid/vivid-core.h b/drivers/media/platform/vivid/vivid-core.h
index 776783bec227..a7daa40d0a49 100644
--- a/drivers/media/platform/vivid/vivid-core.h
+++ b/drivers/media/platform/vivid/vivid-core.h
@@ -21,6 +21,8 @@
21#define _VIVID_CORE_H_ 21#define _VIVID_CORE_H_
22 22
23#include <linux/fb.h> 23#include <linux/fb.h>
24#include <linux/workqueue.h>
25#include <media/cec.h>
24#include <media/videobuf2-v4l2.h> 26#include <media/videobuf2-v4l2.h>
25#include <media/v4l2-device.h> 27#include <media/v4l2-device.h>
26#include <media/v4l2-dev.h> 28#include <media/v4l2-dev.h>
@@ -132,6 +134,17 @@ enum vivid_colorspace {
132#define VIVID_INVALID_SIGNAL(mode) \ 134#define VIVID_INVALID_SIGNAL(mode) \
133 ((mode) == NO_SIGNAL || (mode) == NO_LOCK || (mode) == OUT_OF_RANGE) 135 ((mode) == NO_SIGNAL || (mode) == NO_LOCK || (mode) == OUT_OF_RANGE)
134 136
137struct vivid_cec_work {
138 struct list_head list;
139 struct delayed_work work;
140 struct cec_adapter *adap;
141 struct vivid_dev *dev;
142 unsigned int usecs;
143 unsigned int timeout_ms;
144 u8 tx_status;
145 struct cec_msg msg;
146};
147
135struct vivid_dev { 148struct vivid_dev {
136 unsigned inst; 149 unsigned inst;
137 struct v4l2_device v4l2_dev; 150 struct v4l2_device v4l2_dev;
@@ -497,6 +510,20 @@ struct vivid_dev {
497 /* Shared between radio receiver and transmitter */ 510 /* Shared between radio receiver and transmitter */
498 bool radio_rds_loop; 511 bool radio_rds_loop;
499 struct timespec radio_rds_init_ts; 512 struct timespec radio_rds_init_ts;
513
514 /* CEC */
515 struct cec_adapter *cec_rx_adap;
516 struct cec_adapter *cec_tx_adap[MAX_OUTPUTS];
517 struct workqueue_struct *cec_workqueue;
518 spinlock_t cec_slock;
519 struct list_head cec_work_list;
520 unsigned int cec_xfer_time_jiffies;
521 unsigned long cec_xfer_start_jiffies;
522 u8 cec_output2bus_map[MAX_OUTPUTS];
523
524 /* CEC OSD String */
525 char osd[14];
526 unsigned long osd_jiffies;
500}; 527};
501 528
502static inline bool vivid_is_webcam(const struct vivid_dev *dev) 529static inline bool vivid_is_webcam(const struct vivid_dev *dev)
diff --git a/drivers/media/platform/vivid/vivid-kthread-cap.c b/drivers/media/platform/vivid/vivid-kthread-cap.c
index 3b8c10108dfa..6ca71aabb576 100644
--- a/drivers/media/platform/vivid/vivid-kthread-cap.c
+++ b/drivers/media/platform/vivid/vivid-kthread-cap.c
@@ -552,6 +552,19 @@ static void vivid_fillbuff(struct vivid_dev *dev, struct vivid_buffer *buf)
552 snprintf(str, sizeof(str), " button pressed!"); 552 snprintf(str, sizeof(str), " button pressed!");
553 tpg_gen_text(tpg, basep, line++ * line_height, 16, str); 553 tpg_gen_text(tpg, basep, line++ * line_height, 16, str);
554 } 554 }
555 if (dev->osd[0]) {
556 if (vivid_is_hdmi_cap(dev)) {
557 snprintf(str, sizeof(str),
558 " OSD \"%s\"", dev->osd);
559 tpg_gen_text(tpg, basep, line++ * line_height,
560 16, str);
561 }
562 if (dev->osd_jiffies &&
563 time_is_before_jiffies(dev->osd_jiffies + 5 * HZ)) {
564 dev->osd[0] = 0;
565 dev->osd_jiffies = 0;
566 }
567 }
555 } 568 }
556 569
557 /* 570 /*
diff --git a/drivers/media/platform/vivid/vivid-sdr-cap.c b/drivers/media/platform/vivid/vivid-sdr-cap.c
index 3d1604cb982f..ebd7b9c4dd83 100644
--- a/drivers/media/platform/vivid/vivid-sdr-cap.c
+++ b/drivers/media/platform/vivid/vivid-sdr-cap.c
@@ -51,8 +51,6 @@ static const struct vivid_format formats[] = {
51 }, 51 },
52}; 52};
53 53
54static const unsigned int NUM_FORMATS = ARRAY_SIZE(formats);
55
56static const struct v4l2_frequency_band bands_adc[] = { 54static const struct v4l2_frequency_band bands_adc[] = {
57 { 55 {
58 .tuner = 0, 56 .tuner = 0,
@@ -215,7 +213,7 @@ static int vivid_thread_sdr_cap(void *data)
215 213
216static int sdr_cap_queue_setup(struct vb2_queue *vq, 214static int sdr_cap_queue_setup(struct vb2_queue *vq,
217 unsigned *nbuffers, unsigned *nplanes, 215 unsigned *nbuffers, unsigned *nplanes,
218 unsigned sizes[], void *alloc_ctxs[]) 216 unsigned sizes[], struct device *alloc_devs[])
219{ 217{
220 /* 2 = max 16-bit sample returned */ 218 /* 2 = max 16-bit sample returned */
221 sizes[0] = SDR_CAP_SAMPLES_PER_BUF * 2; 219 sizes[0] = SDR_CAP_SAMPLES_PER_BUF * 2;
diff --git a/drivers/media/platform/vivid/vivid-vbi-cap.c b/drivers/media/platform/vivid/vivid-vbi-cap.c
index cda45a582bfe..d66ef95dd2b5 100644
--- a/drivers/media/platform/vivid/vivid-vbi-cap.c
+++ b/drivers/media/platform/vivid/vivid-vbi-cap.c
@@ -137,7 +137,7 @@ void vivid_sliced_vbi_cap_process(struct vivid_dev *dev,
137 137
138static int vbi_cap_queue_setup(struct vb2_queue *vq, 138static int vbi_cap_queue_setup(struct vb2_queue *vq,
139 unsigned *nbuffers, unsigned *nplanes, 139 unsigned *nbuffers, unsigned *nplanes,
140 unsigned sizes[], void *alloc_ctxs[]) 140 unsigned sizes[], struct device *alloc_devs[])
141{ 141{
142 struct vivid_dev *dev = vb2_get_drv_priv(vq); 142 struct vivid_dev *dev = vb2_get_drv_priv(vq);
143 bool is_60hz = dev->std_cap & V4L2_STD_525_60; 143 bool is_60hz = dev->std_cap & V4L2_STD_525_60;
diff --git a/drivers/media/platform/vivid/vivid-vbi-out.c b/drivers/media/platform/vivid/vivid-vbi-out.c
index 3c5a469e6f49..d2989195cf03 100644
--- a/drivers/media/platform/vivid/vivid-vbi-out.c
+++ b/drivers/media/platform/vivid/vivid-vbi-out.c
@@ -29,7 +29,7 @@
29 29
30static int vbi_out_queue_setup(struct vb2_queue *vq, 30static int vbi_out_queue_setup(struct vb2_queue *vq,
31 unsigned *nbuffers, unsigned *nplanes, 31 unsigned *nbuffers, unsigned *nplanes,
32 unsigned sizes[], void *alloc_ctxs[]) 32 unsigned sizes[], struct device *alloc_devs[])
33{ 33{
34 struct vivid_dev *dev = vb2_get_drv_priv(vq); 34 struct vivid_dev *dev = vb2_get_drv_priv(vq);
35 bool is_60hz = dev->std_out & V4L2_STD_525_60; 35 bool is_60hz = dev->std_out & V4L2_STD_525_60;
diff --git a/drivers/media/platform/vivid/vivid-vid-cap.c b/drivers/media/platform/vivid/vivid-vid-cap.c
index 4f730f355a17..d404a7ce33a4 100644
--- a/drivers/media/platform/vivid/vivid-vid-cap.c
+++ b/drivers/media/platform/vivid/vivid-vid-cap.c
@@ -36,8 +36,7 @@
36/* timeperframe: min/max and default */ 36/* timeperframe: min/max and default */
37static const struct v4l2_fract 37static const struct v4l2_fract
38 tpf_min = {.numerator = 1, .denominator = FPS_MAX}, 38 tpf_min = {.numerator = 1, .denominator = FPS_MAX},
39 tpf_max = {.numerator = FPS_MAX, .denominator = 1}, 39 tpf_max = {.numerator = FPS_MAX, .denominator = 1};
40 tpf_default = {.numerator = 1, .denominator = 30};
41 40
42static const struct vivid_fmt formats_ovl[] = { 41static const struct vivid_fmt formats_ovl[] = {
43 { 42 {
@@ -98,7 +97,7 @@ static const struct v4l2_discrete_probe webcam_probe = {
98 97
99static int vid_cap_queue_setup(struct vb2_queue *vq, 98static int vid_cap_queue_setup(struct vb2_queue *vq,
100 unsigned *nbuffers, unsigned *nplanes, 99 unsigned *nbuffers, unsigned *nplanes,
101 unsigned sizes[], void *alloc_ctxs[]) 100 unsigned sizes[], struct device *alloc_devs[])
102{ 101{
103 struct vivid_dev *dev = vb2_get_drv_priv(vq); 102 struct vivid_dev *dev = vb2_get_drv_priv(vq);
104 unsigned buffers = tpg_g_buffers(&dev->tpg); 103 unsigned buffers = tpg_g_buffers(&dev->tpg);
@@ -145,11 +144,6 @@ static int vid_cap_queue_setup(struct vb2_queue *vq,
145 144
146 *nplanes = buffers; 145 *nplanes = buffers;
147 146
148 /*
149 * videobuf2-vmalloc allocator is context-less so no need to set
150 * alloc_ctxs array.
151 */
152
153 dprintk(dev, 1, "%s: count=%d\n", __func__, *nbuffers); 147 dprintk(dev, 1, "%s: count=%d\n", __func__, *nbuffers);
154 for (p = 0; p < buffers; p++) 148 for (p = 0; p < buffers; p++)
155 dprintk(dev, 1, "%s: size[%u]=%u\n", __func__, p, sizes[p]); 149 dprintk(dev, 1, "%s: size[%u]=%u\n", __func__, p, sizes[p]);
@@ -1701,6 +1695,9 @@ int vidioc_s_edid(struct file *file, void *_fh,
1701 struct v4l2_edid *edid) 1695 struct v4l2_edid *edid)
1702{ 1696{
1703 struct vivid_dev *dev = video_drvdata(file); 1697 struct vivid_dev *dev = video_drvdata(file);
1698 u16 phys_addr;
1699 unsigned int i;
1700 int ret;
1704 1701
1705 memset(edid->reserved, 0, sizeof(edid->reserved)); 1702 memset(edid->reserved, 0, sizeof(edid->reserved));
1706 if (edid->pad >= dev->num_inputs) 1703 if (edid->pad >= dev->num_inputs)
@@ -1709,14 +1706,32 @@ int vidioc_s_edid(struct file *file, void *_fh,
1709 return -EINVAL; 1706 return -EINVAL;
1710 if (edid->blocks == 0) { 1707 if (edid->blocks == 0) {
1711 dev->edid_blocks = 0; 1708 dev->edid_blocks = 0;
1712 return 0; 1709 phys_addr = CEC_PHYS_ADDR_INVALID;
1710 goto set_phys_addr;
1713 } 1711 }
1714 if (edid->blocks > dev->edid_max_blocks) { 1712 if (edid->blocks > dev->edid_max_blocks) {
1715 edid->blocks = dev->edid_max_blocks; 1713 edid->blocks = dev->edid_max_blocks;
1716 return -E2BIG; 1714 return -E2BIG;
1717 } 1715 }
1716 phys_addr = cec_get_edid_phys_addr(edid->edid, edid->blocks * 128, NULL);
1717 ret = cec_phys_addr_validate(phys_addr, &phys_addr, NULL);
1718 if (ret)
1719 return ret;
1720
1721 if (vb2_is_busy(&dev->vb_vid_cap_q))
1722 return -EBUSY;
1723
1718 dev->edid_blocks = edid->blocks; 1724 dev->edid_blocks = edid->blocks;
1719 memcpy(dev->edid, edid->edid, edid->blocks * 128); 1725 memcpy(dev->edid, edid->edid, edid->blocks * 128);
1726
1727set_phys_addr:
1728 /* TODO: a proper hotplug detect cycle should be emulated here */
1729 cec_s_phys_addr(dev->cec_rx_adap, phys_addr, false);
1730
1731 for (i = 0; i < MAX_OUTPUTS && dev->cec_tx_adap[i]; i++)
1732 cec_s_phys_addr(dev->cec_tx_adap[i],
1733 cec_phys_addr_for_input(phys_addr, i + 1),
1734 false);
1720 return 0; 1735 return 0;
1721} 1736}
1722 1737
@@ -1836,6 +1851,7 @@ int vivid_vid_cap_s_parm(struct file *file, void *priv,
1836 /* resync the thread's timings */ 1851 /* resync the thread's timings */
1837 dev->cap_seq_resync = true; 1852 dev->cap_seq_resync = true;
1838 dev->timeperframe_vid_cap = tpf; 1853 dev->timeperframe_vid_cap = tpf;
1854 parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
1839 parm->parm.capture.timeperframe = tpf; 1855 parm->parm.capture.timeperframe = tpf;
1840 parm->parm.capture.readbuffers = 1; 1856 parm->parm.capture.readbuffers = 1;
1841 return 0; 1857 return 0;
diff --git a/drivers/media/platform/vivid/vivid-vid-common.c b/drivers/media/platform/vivid/vivid-vid-common.c
index 39ea2284789c..fcda3ae4e6b0 100644
--- a/drivers/media/platform/vivid/vivid-vid-common.c
+++ b/drivers/media/platform/vivid/vivid-vid-common.c
@@ -811,6 +811,7 @@ int vidioc_g_edid(struct file *file, void *_fh,
811{ 811{
812 struct vivid_dev *dev = video_drvdata(file); 812 struct vivid_dev *dev = video_drvdata(file);
813 struct video_device *vdev = video_devdata(file); 813 struct video_device *vdev = video_devdata(file);
814 struct cec_adapter *adap;
814 815
815 memset(edid->reserved, 0, sizeof(edid->reserved)); 816 memset(edid->reserved, 0, sizeof(edid->reserved));
816 if (vdev->vfl_dir == VFL_DIR_RX) { 817 if (vdev->vfl_dir == VFL_DIR_RX) {
@@ -818,11 +819,16 @@ int vidioc_g_edid(struct file *file, void *_fh,
818 return -EINVAL; 819 return -EINVAL;
819 if (dev->input_type[edid->pad] != HDMI) 820 if (dev->input_type[edid->pad] != HDMI)
820 return -EINVAL; 821 return -EINVAL;
822 adap = dev->cec_rx_adap;
821 } else { 823 } else {
824 unsigned int bus_idx;
825
822 if (edid->pad >= dev->num_outputs) 826 if (edid->pad >= dev->num_outputs)
823 return -EINVAL; 827 return -EINVAL;
824 if (dev->output_type[edid->pad] != HDMI) 828 if (dev->output_type[edid->pad] != HDMI)
825 return -EINVAL; 829 return -EINVAL;
830 bus_idx = dev->cec_output2bus_map[edid->pad];
831 adap = dev->cec_tx_adap[bus_idx];
826 } 832 }
827 if (edid->start_block == 0 && edid->blocks == 0) { 833 if (edid->start_block == 0 && edid->blocks == 0) {
828 edid->blocks = dev->edid_blocks; 834 edid->blocks = dev->edid_blocks;
@@ -835,5 +841,6 @@ int vidioc_g_edid(struct file *file, void *_fh,
835 if (edid->start_block + edid->blocks > dev->edid_blocks) 841 if (edid->start_block + edid->blocks > dev->edid_blocks)
836 edid->blocks = dev->edid_blocks - edid->start_block; 842 edid->blocks = dev->edid_blocks - edid->start_block;
837 memcpy(edid->edid, dev->edid, edid->blocks * 128); 843 memcpy(edid->edid, dev->edid, edid->blocks * 128);
844 cec_set_edid_phys_addr(edid->edid, edid->blocks * 128, adap->phys_addr);
838 return 0; 845 return 0;
839} 846}
diff --git a/drivers/media/platform/vivid/vivid-vid-out.c b/drivers/media/platform/vivid/vivid-vid-out.c
index f92f4496d527..dd609eea4753 100644
--- a/drivers/media/platform/vivid/vivid-vid-out.c
+++ b/drivers/media/platform/vivid/vivid-vid-out.c
@@ -34,7 +34,7 @@
34 34
35static int vid_out_queue_setup(struct vb2_queue *vq, 35static int vid_out_queue_setup(struct vb2_queue *vq,
36 unsigned *nbuffers, unsigned *nplanes, 36 unsigned *nbuffers, unsigned *nplanes,
37 unsigned sizes[], void *alloc_ctxs[]) 37 unsigned sizes[], struct device *alloc_devs[])
38{ 38{
39 struct vivid_dev *dev = vb2_get_drv_priv(vq); 39 struct vivid_dev *dev = vb2_get_drv_priv(vq);
40 const struct vivid_fmt *vfmt = dev->fmt_out; 40 const struct vivid_fmt *vfmt = dev->fmt_out;
@@ -87,11 +87,6 @@ static int vid_out_queue_setup(struct vb2_queue *vq,
87 87
88 *nplanes = planes; 88 *nplanes = planes;
89 89
90 /*
91 * videobuf2-vmalloc allocator is context-less so no need to set
92 * alloc_ctxs array.
93 */
94
95 dprintk(dev, 1, "%s: count=%d\n", __func__, *nbuffers); 90 dprintk(dev, 1, "%s: count=%d\n", __func__, *nbuffers);
96 for (p = 0; p < planes; p++) 91 for (p = 0; p < planes; p++)
97 dprintk(dev, 1, "%s: size[%u]=%u\n", __func__, p, sizes[p]); 92 dprintk(dev, 1, "%s: size[%u]=%u\n", __func__, p, sizes[p]);
diff --git a/drivers/media/platform/vsp1/Makefile b/drivers/media/platform/vsp1/Makefile
index 95b3ac2ea7ef..1328e1bd2143 100644
--- a/drivers/media/platform/vsp1/Makefile
+++ b/drivers/media/platform/vsp1/Makefile
@@ -1,7 +1,8 @@
1vsp1-y := vsp1_drv.o vsp1_entity.o vsp1_pipe.o 1vsp1-y := vsp1_drv.o vsp1_entity.o vsp1_pipe.o
2vsp1-y += vsp1_dl.o vsp1_drm.o vsp1_video.o 2vsp1-y += vsp1_dl.o vsp1_drm.o vsp1_video.o
3vsp1-y += vsp1_rpf.o vsp1_rwpf.o vsp1_wpf.o 3vsp1-y += vsp1_rpf.o vsp1_rwpf.o vsp1_wpf.o
4vsp1-y += vsp1_hsit.o vsp1_lif.o vsp1_lut.o 4vsp1-y += vsp1_clu.o vsp1_hsit.o vsp1_lut.o
5vsp1-y += vsp1_bru.o vsp1_sru.o vsp1_uds.o 5vsp1-y += vsp1_bru.o vsp1_sru.o vsp1_uds.o
6vsp1-y += vsp1_lif.o
6 7
7obj-$(CONFIG_VIDEO_RENESAS_VSP1) += vsp1.o 8obj-$(CONFIG_VIDEO_RENESAS_VSP1) += vsp1.o
diff --git a/drivers/media/platform/vsp1/vsp1.h b/drivers/media/platform/vsp1/vsp1.h
index 46738b6c5f72..06a2ec7e5ad4 100644
--- a/drivers/media/platform/vsp1/vsp1.h
+++ b/drivers/media/platform/vsp1/vsp1.h
@@ -25,11 +25,13 @@
25 25
26struct clk; 26struct clk;
27struct device; 27struct device;
28struct rcar_fcp_device;
28 29
29struct vsp1_drm; 30struct vsp1_drm;
30struct vsp1_entity; 31struct vsp1_entity;
31struct vsp1_platform_data; 32struct vsp1_platform_data;
32struct vsp1_bru; 33struct vsp1_bru;
34struct vsp1_clu;
33struct vsp1_hsit; 35struct vsp1_hsit;
34struct vsp1_lif; 36struct vsp1_lif;
35struct vsp1_lut; 37struct vsp1_lut;
@@ -45,6 +47,9 @@ struct vsp1_uds;
45#define VSP1_HAS_LUT (1 << 1) 47#define VSP1_HAS_LUT (1 << 1)
46#define VSP1_HAS_SRU (1 << 2) 48#define VSP1_HAS_SRU (1 << 2)
47#define VSP1_HAS_BRU (1 << 3) 49#define VSP1_HAS_BRU (1 << 3)
50#define VSP1_HAS_CLU (1 << 4)
51#define VSP1_HAS_WPF_VFLIP (1 << 5)
52#define VSP1_HAS_WPF_HFLIP (1 << 6)
48 53
49struct vsp1_device_info { 54struct vsp1_device_info {
50 u32 version; 55 u32 version;
@@ -62,12 +67,10 @@ struct vsp1_device {
62 const struct vsp1_device_info *info; 67 const struct vsp1_device_info *info;
63 68
64 void __iomem *mmio; 69 void __iomem *mmio;
65 struct clk *clock; 70 struct rcar_fcp_device *fcp;
66
67 struct mutex lock;
68 int ref_count;
69 71
70 struct vsp1_bru *bru; 72 struct vsp1_bru *bru;
73 struct vsp1_clu *clu;
71 struct vsp1_hsit *hsi; 74 struct vsp1_hsit *hsi;
72 struct vsp1_hsit *hst; 75 struct vsp1_hsit *hst;
73 struct vsp1_lif *lif; 76 struct vsp1_lif *lif;
diff --git a/drivers/media/platform/vsp1/vsp1_bru.c b/drivers/media/platform/vsp1/vsp1_bru.c
index b1068c018011..8268b87727a7 100644
--- a/drivers/media/platform/vsp1/vsp1_bru.c
+++ b/drivers/media/platform/vsp1/vsp1_bru.c
@@ -249,7 +249,7 @@ static int bru_set_selection(struct v4l2_subdev *subdev,
249 return 0; 249 return 0;
250} 250}
251 251
252static struct v4l2_subdev_pad_ops bru_pad_ops = { 252static const struct v4l2_subdev_pad_ops bru_pad_ops = {
253 .init_cfg = vsp1_entity_init_cfg, 253 .init_cfg = vsp1_entity_init_cfg,
254 .enum_mbus_code = bru_enum_mbus_code, 254 .enum_mbus_code = bru_enum_mbus_code,
255 .enum_frame_size = bru_enum_frame_size, 255 .enum_frame_size = bru_enum_frame_size,
@@ -259,7 +259,7 @@ static struct v4l2_subdev_pad_ops bru_pad_ops = {
259 .set_selection = bru_set_selection, 259 .set_selection = bru_set_selection,
260}; 260};
261 261
262static struct v4l2_subdev_ops bru_ops = { 262static const struct v4l2_subdev_ops bru_ops = {
263 .pad = &bru_pad_ops, 263 .pad = &bru_pad_ops,
264}; 264};
265 265
@@ -269,13 +269,16 @@ static struct v4l2_subdev_ops bru_ops = {
269 269
270static void bru_configure(struct vsp1_entity *entity, 270static void bru_configure(struct vsp1_entity *entity,
271 struct vsp1_pipeline *pipe, 271 struct vsp1_pipeline *pipe,
272 struct vsp1_dl_list *dl) 272 struct vsp1_dl_list *dl, bool full)
273{ 273{
274 struct vsp1_bru *bru = to_bru(&entity->subdev); 274 struct vsp1_bru *bru = to_bru(&entity->subdev);
275 struct v4l2_mbus_framefmt *format; 275 struct v4l2_mbus_framefmt *format;
276 unsigned int flags; 276 unsigned int flags;
277 unsigned int i; 277 unsigned int i;
278 278
279 if (!full)
280 return;
281
279 format = vsp1_entity_get_pad_format(&bru->entity, bru->entity.config, 282 format = vsp1_entity_get_pad_format(&bru->entity, bru->entity.config,
280 bru->entity.source_pad); 283 bru->entity.source_pad);
281 284
@@ -390,7 +393,8 @@ struct vsp1_bru *vsp1_bru_create(struct vsp1_device *vsp1)
390 bru->entity.type = VSP1_ENTITY_BRU; 393 bru->entity.type = VSP1_ENTITY_BRU;
391 394
392 ret = vsp1_entity_init(vsp1, &bru->entity, "bru", 395 ret = vsp1_entity_init(vsp1, &bru->entity, "bru",
393 vsp1->info->num_bru_inputs + 1, &bru_ops); 396 vsp1->info->num_bru_inputs + 1, &bru_ops,
397 MEDIA_ENT_F_PROC_VIDEO_COMPOSER);
394 if (ret < 0) 398 if (ret < 0)
395 return ERR_PTR(ret); 399 return ERR_PTR(ret);
396 400
diff --git a/drivers/media/platform/vsp1/vsp1_clu.c b/drivers/media/platform/vsp1/vsp1_clu.c
new file mode 100644
index 000000000000..b63d2dbe5ea3
--- /dev/null
+++ b/drivers/media/platform/vsp1/vsp1_clu.c
@@ -0,0 +1,292 @@
1/*
2 * vsp1_clu.c -- R-Car VSP1 Cubic Look-Up Table
3 *
4 * Copyright (C) 2015-2016 Renesas Electronics Corporation
5 *
6 * Contact: Laurent Pinchart (laurent.pinchart@ideasonboard.com)
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 */
13
14#include <linux/device.h>
15#include <linux/slab.h>
16
17#include <media/v4l2-subdev.h>
18
19#include "vsp1.h"
20#include "vsp1_clu.h"
21#include "vsp1_dl.h"
22
23#define CLU_MIN_SIZE 4U
24#define CLU_MAX_SIZE 8190U
25
26/* -----------------------------------------------------------------------------
27 * Device Access
28 */
29
30static inline void vsp1_clu_write(struct vsp1_clu *clu, struct vsp1_dl_list *dl,
31 u32 reg, u32 data)
32{
33 vsp1_dl_list_write(dl, reg, data);
34}
35
36/* -----------------------------------------------------------------------------
37 * Controls
38 */
39
40#define V4L2_CID_VSP1_CLU_TABLE (V4L2_CID_USER_BASE | 0x1001)
41#define V4L2_CID_VSP1_CLU_MODE (V4L2_CID_USER_BASE | 0x1002)
42#define V4L2_CID_VSP1_CLU_MODE_2D 0
43#define V4L2_CID_VSP1_CLU_MODE_3D 1
44
45static int clu_set_table(struct vsp1_clu *clu, struct v4l2_ctrl *ctrl)
46{
47 struct vsp1_dl_body *dlb;
48 unsigned int i;
49
50 dlb = vsp1_dl_fragment_alloc(clu->entity.vsp1, 1 + 17 * 17 * 17);
51 if (!dlb)
52 return -ENOMEM;
53
54 vsp1_dl_fragment_write(dlb, VI6_CLU_ADDR, 0);
55 for (i = 0; i < 17 * 17 * 17; ++i)
56 vsp1_dl_fragment_write(dlb, VI6_CLU_DATA, ctrl->p_new.p_u32[i]);
57
58 spin_lock_irq(&clu->lock);
59 swap(clu->clu, dlb);
60 spin_unlock_irq(&clu->lock);
61
62 vsp1_dl_fragment_free(dlb);
63 return 0;
64}
65
66static int clu_s_ctrl(struct v4l2_ctrl *ctrl)
67{
68 struct vsp1_clu *clu =
69 container_of(ctrl->handler, struct vsp1_clu, ctrls);
70
71 switch (ctrl->id) {
72 case V4L2_CID_VSP1_CLU_TABLE:
73 clu_set_table(clu, ctrl);
74 break;
75
76 case V4L2_CID_VSP1_CLU_MODE:
77 clu->mode = ctrl->val;
78 break;
79 }
80
81 return 0;
82}
83
84static const struct v4l2_ctrl_ops clu_ctrl_ops = {
85 .s_ctrl = clu_s_ctrl,
86};
87
88static const struct v4l2_ctrl_config clu_table_control = {
89 .ops = &clu_ctrl_ops,
90 .id = V4L2_CID_VSP1_CLU_TABLE,
91 .name = "Look-Up Table",
92 .type = V4L2_CTRL_TYPE_U32,
93 .min = 0x00000000,
94 .max = 0x00ffffff,
95 .step = 1,
96 .def = 0,
97 .dims = { 17, 17, 17 },
98};
99
100static const char * const clu_mode_menu[] = {
101 "2D",
102 "3D",
103 NULL,
104};
105
106static const struct v4l2_ctrl_config clu_mode_control = {
107 .ops = &clu_ctrl_ops,
108 .id = V4L2_CID_VSP1_CLU_MODE,
109 .name = "Mode",
110 .type = V4L2_CTRL_TYPE_MENU,
111 .min = 0,
112 .max = 1,
113 .def = 1,
114 .qmenu = clu_mode_menu,
115};
116
117/* -----------------------------------------------------------------------------
118 * V4L2 Subdevice Pad Operations
119 */
120
121static int clu_enum_mbus_code(struct v4l2_subdev *subdev,
122 struct v4l2_subdev_pad_config *cfg,
123 struct v4l2_subdev_mbus_code_enum *code)
124{
125 static const unsigned int codes[] = {
126 MEDIA_BUS_FMT_ARGB8888_1X32,
127 MEDIA_BUS_FMT_AHSV8888_1X32,
128 MEDIA_BUS_FMT_AYUV8_1X32,
129 };
130
131 return vsp1_subdev_enum_mbus_code(subdev, cfg, code, codes,
132 ARRAY_SIZE(codes));
133}
134
135static int clu_enum_frame_size(struct v4l2_subdev *subdev,
136 struct v4l2_subdev_pad_config *cfg,
137 struct v4l2_subdev_frame_size_enum *fse)
138{
139 return vsp1_subdev_enum_frame_size(subdev, cfg, fse, CLU_MIN_SIZE,
140 CLU_MIN_SIZE, CLU_MAX_SIZE,
141 CLU_MAX_SIZE);
142}
143
144static int clu_set_format(struct v4l2_subdev *subdev,
145 struct v4l2_subdev_pad_config *cfg,
146 struct v4l2_subdev_format *fmt)
147{
148 struct vsp1_clu *clu = to_clu(subdev);
149 struct v4l2_subdev_pad_config *config;
150 struct v4l2_mbus_framefmt *format;
151
152 config = vsp1_entity_get_pad_config(&clu->entity, cfg, fmt->which);
153 if (!config)
154 return -EINVAL;
155
156 /* Default to YUV if the requested format is not supported. */
157 if (fmt->format.code != MEDIA_BUS_FMT_ARGB8888_1X32 &&
158 fmt->format.code != MEDIA_BUS_FMT_AHSV8888_1X32 &&
159 fmt->format.code != MEDIA_BUS_FMT_AYUV8_1X32)
160 fmt->format.code = MEDIA_BUS_FMT_AYUV8_1X32;
161
162 format = vsp1_entity_get_pad_format(&clu->entity, config, fmt->pad);
163
164 if (fmt->pad == CLU_PAD_SOURCE) {
165 /* The CLU output format can't be modified. */
166 fmt->format = *format;
167 return 0;
168 }
169
170 format->code = fmt->format.code;
171 format->width = clamp_t(unsigned int, fmt->format.width,
172 CLU_MIN_SIZE, CLU_MAX_SIZE);
173 format->height = clamp_t(unsigned int, fmt->format.height,
174 CLU_MIN_SIZE, CLU_MAX_SIZE);
175 format->field = V4L2_FIELD_NONE;
176 format->colorspace = V4L2_COLORSPACE_SRGB;
177
178 fmt->format = *format;
179
180 /* Propagate the format to the source pad. */
181 format = vsp1_entity_get_pad_format(&clu->entity, config,
182 CLU_PAD_SOURCE);
183 *format = fmt->format;
184
185 return 0;
186}
187
188/* -----------------------------------------------------------------------------
189 * V4L2 Subdevice Operations
190 */
191
192static const struct v4l2_subdev_pad_ops clu_pad_ops = {
193 .init_cfg = vsp1_entity_init_cfg,
194 .enum_mbus_code = clu_enum_mbus_code,
195 .enum_frame_size = clu_enum_frame_size,
196 .get_fmt = vsp1_subdev_get_pad_format,
197 .set_fmt = clu_set_format,
198};
199
200static const struct v4l2_subdev_ops clu_ops = {
201 .pad = &clu_pad_ops,
202};
203
204/* -----------------------------------------------------------------------------
205 * VSP1 Entity Operations
206 */
207
208static void clu_configure(struct vsp1_entity *entity,
209 struct vsp1_pipeline *pipe,
210 struct vsp1_dl_list *dl, bool full)
211{
212 struct vsp1_clu *clu = to_clu(&entity->subdev);
213 struct vsp1_dl_body *dlb;
214 unsigned long flags;
215 u32 ctrl = VI6_CLU_CTRL_AAI | VI6_CLU_CTRL_MVS | VI6_CLU_CTRL_EN;
216
217 /* The format can't be changed during streaming, only verify it at
218 * stream start and store the information internally for future partial
219 * reconfiguration calls.
220 */
221 if (full) {
222 struct v4l2_mbus_framefmt *format;
223
224 format = vsp1_entity_get_pad_format(&clu->entity,
225 clu->entity.config,
226 CLU_PAD_SINK);
227 clu->yuv_mode = format->code == MEDIA_BUS_FMT_AYUV8_1X32;
228 return;
229 }
230
231 /* 2D mode can only be used with the YCbCr pixel encoding. */
232 if (clu->mode == V4L2_CID_VSP1_CLU_MODE_2D && clu->yuv_mode)
233 ctrl |= VI6_CLU_CTRL_AX1I_2D | VI6_CLU_CTRL_AX2I_2D
234 | VI6_CLU_CTRL_OS0_2D | VI6_CLU_CTRL_OS1_2D
235 | VI6_CLU_CTRL_OS2_2D | VI6_CLU_CTRL_M2D;
236
237 vsp1_clu_write(clu, dl, VI6_CLU_CTRL, ctrl);
238
239 spin_lock_irqsave(&clu->lock, flags);
240 dlb = clu->clu;
241 clu->clu = NULL;
242 spin_unlock_irqrestore(&clu->lock, flags);
243
244 if (dlb)
245 vsp1_dl_list_add_fragment(dl, dlb);
246}
247
248static const struct vsp1_entity_operations clu_entity_ops = {
249 .configure = clu_configure,
250};
251
252/* -----------------------------------------------------------------------------
253 * Initialization and Cleanup
254 */
255
256struct vsp1_clu *vsp1_clu_create(struct vsp1_device *vsp1)
257{
258 struct vsp1_clu *clu;
259 int ret;
260
261 clu = devm_kzalloc(vsp1->dev, sizeof(*clu), GFP_KERNEL);
262 if (clu == NULL)
263 return ERR_PTR(-ENOMEM);
264
265 spin_lock_init(&clu->lock);
266
267 clu->entity.ops = &clu_entity_ops;
268 clu->entity.type = VSP1_ENTITY_CLU;
269
270 ret = vsp1_entity_init(vsp1, &clu->entity, "clu", 2, &clu_ops,
271 MEDIA_ENT_F_PROC_VIDEO_LUT);
272 if (ret < 0)
273 return ERR_PTR(ret);
274
275 /* Initialize the control handler. */
276 v4l2_ctrl_handler_init(&clu->ctrls, 2);
277 v4l2_ctrl_new_custom(&clu->ctrls, &clu_table_control, NULL);
278 v4l2_ctrl_new_custom(&clu->ctrls, &clu_mode_control, NULL);
279
280 clu->entity.subdev.ctrl_handler = &clu->ctrls;
281
282 if (clu->ctrls.error) {
283 dev_err(vsp1->dev, "clu: failed to initialize controls\n");
284 ret = clu->ctrls.error;
285 vsp1_entity_destroy(&clu->entity);
286 return ERR_PTR(ret);
287 }
288
289 v4l2_ctrl_handler_setup(&clu->ctrls);
290
291 return clu;
292}
diff --git a/drivers/media/platform/vsp1/vsp1_clu.h b/drivers/media/platform/vsp1/vsp1_clu.h
new file mode 100644
index 000000000000..036e0a2f1a42
--- /dev/null
+++ b/drivers/media/platform/vsp1/vsp1_clu.h
@@ -0,0 +1,48 @@
1/*
2 * vsp1_clu.h -- R-Car VSP1 Cubic Look-Up Table
3 *
4 * Copyright (C) 2015 Renesas Corporation
5 *
6 * Contact: Laurent Pinchart (laurent.pinchart@ideasonboard.com)
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 */
13#ifndef __VSP1_CLU_H__
14#define __VSP1_CLU_H__
15
16#include <linux/spinlock.h>
17
18#include <media/media-entity.h>
19#include <media/v4l2-ctrls.h>
20#include <media/v4l2-subdev.h>
21
22#include "vsp1_entity.h"
23
24struct vsp1_device;
25struct vsp1_dl_body;
26
27#define CLU_PAD_SINK 0
28#define CLU_PAD_SOURCE 1
29
30struct vsp1_clu {
31 struct vsp1_entity entity;
32
33 struct v4l2_ctrl_handler ctrls;
34
35 bool yuv_mode;
36 spinlock_t lock;
37 unsigned int mode;
38 struct vsp1_dl_body *clu;
39};
40
41static inline struct vsp1_clu *to_clu(struct v4l2_subdev *subdev)
42{
43 return container_of(subdev, struct vsp1_clu, entity.subdev);
44}
45
46struct vsp1_clu *vsp1_clu_create(struct vsp1_device *vsp1);
47
48#endif /* __VSP1_CLU_H__ */
diff --git a/drivers/media/platform/vsp1/vsp1_dl.c b/drivers/media/platform/vsp1/vsp1_dl.c
index e238d9b9376b..37c3518aa2a8 100644
--- a/drivers/media/platform/vsp1/vsp1_dl.c
+++ b/drivers/media/platform/vsp1/vsp1_dl.c
@@ -15,6 +15,7 @@
15#include <linux/dma-mapping.h> 15#include <linux/dma-mapping.h>
16#include <linux/gfp.h> 16#include <linux/gfp.h>
17#include <linux/slab.h> 17#include <linux/slab.h>
18#include <linux/workqueue.h>
18 19
19#include "vsp1.h" 20#include "vsp1.h"
20#include "vsp1_dl.h" 21#include "vsp1_dl.h"
@@ -92,11 +93,13 @@ enum vsp1_dl_mode {
92 * @index: index of the related WPF 93 * @index: index of the related WPF
93 * @mode: display list operation mode (header or headerless) 94 * @mode: display list operation mode (header or headerless)
94 * @vsp1: the VSP1 device 95 * @vsp1: the VSP1 device
95 * @lock: protects the active, queued and pending lists 96 * @lock: protects the free, active, queued, pending and gc_fragments lists
96 * @free: array of all free display lists 97 * @free: array of all free display lists
97 * @active: list currently being processed (loaded) by hardware 98 * @active: list currently being processed (loaded) by hardware
98 * @queued: list queued to the hardware (written to the DL registers) 99 * @queued: list queued to the hardware (written to the DL registers)
99 * @pending: list waiting to be queued to the hardware 100 * @pending: list waiting to be queued to the hardware
101 * @gc_work: fragments garbage collector work struct
102 * @gc_fragments: array of display list fragments waiting to be freed
100 */ 103 */
101struct vsp1_dl_manager { 104struct vsp1_dl_manager {
102 unsigned int index; 105 unsigned int index;
@@ -108,6 +111,9 @@ struct vsp1_dl_manager {
108 struct vsp1_dl_list *active; 111 struct vsp1_dl_list *active;
109 struct vsp1_dl_list *queued; 112 struct vsp1_dl_list *queued;
110 struct vsp1_dl_list *pending; 113 struct vsp1_dl_list *pending;
114
115 struct work_struct gc_work;
116 struct list_head gc_fragments;
111}; 117};
112 118
113/* ----------------------------------------------------------------------------- 119/* -----------------------------------------------------------------------------
@@ -262,21 +268,10 @@ static struct vsp1_dl_list *vsp1_dl_list_alloc(struct vsp1_dl_manager *dlm)
262 return dl; 268 return dl;
263} 269}
264 270
265static void vsp1_dl_list_free_fragments(struct vsp1_dl_list *dl)
266{
267 struct vsp1_dl_body *dlb, *next;
268
269 list_for_each_entry_safe(dlb, next, &dl->fragments, list) {
270 list_del(&dlb->list);
271 vsp1_dl_body_cleanup(dlb);
272 kfree(dlb);
273 }
274}
275
276static void vsp1_dl_list_free(struct vsp1_dl_list *dl) 271static void vsp1_dl_list_free(struct vsp1_dl_list *dl)
277{ 272{
278 vsp1_dl_body_cleanup(&dl->body0); 273 vsp1_dl_body_cleanup(&dl->body0);
279 vsp1_dl_list_free_fragments(dl); 274 list_splice_init(&dl->fragments, &dl->dlm->gc_fragments);
280 kfree(dl); 275 kfree(dl);
281} 276}
282 277
@@ -311,7 +306,16 @@ static void __vsp1_dl_list_put(struct vsp1_dl_list *dl)
311 if (!dl) 306 if (!dl)
312 return; 307 return;
313 308
314 vsp1_dl_list_free_fragments(dl); 309 /* We can't free fragments here as DMA memory can only be freed in
310 * interruptible context. Move all fragments to the display list
311 * manager's list of fragments to be freed, they will be
312 * garbage-collected by the work queue.
313 */
314 if (!list_empty(&dl->fragments)) {
315 list_splice_init(&dl->fragments, &dl->dlm->gc_fragments);
316 schedule_work(&dl->dlm->gc_work);
317 }
318
315 dl->body0.num_entries = 0; 319 dl->body0.num_entries = 0;
316 320
317 list_add_tail(&dl->list, &dl->dlm->free); 321 list_add_tail(&dl->list, &dl->dlm->free);
@@ -550,6 +554,40 @@ void vsp1_dlm_reset(struct vsp1_dl_manager *dlm)
550 dlm->pending = NULL; 554 dlm->pending = NULL;
551} 555}
552 556
557/*
558 * Free all fragments awaiting to be garbage-collected.
559 *
560 * This function must be called without the display list manager lock held.
561 */
562static void vsp1_dlm_fragments_free(struct vsp1_dl_manager *dlm)
563{
564 unsigned long flags;
565
566 spin_lock_irqsave(&dlm->lock, flags);
567
568 while (!list_empty(&dlm->gc_fragments)) {
569 struct vsp1_dl_body *dlb;
570
571 dlb = list_first_entry(&dlm->gc_fragments, struct vsp1_dl_body,
572 list);
573 list_del(&dlb->list);
574
575 spin_unlock_irqrestore(&dlm->lock, flags);
576 vsp1_dl_fragment_free(dlb);
577 spin_lock_irqsave(&dlm->lock, flags);
578 }
579
580 spin_unlock_irqrestore(&dlm->lock, flags);
581}
582
583static void vsp1_dlm_garbage_collect(struct work_struct *work)
584{
585 struct vsp1_dl_manager *dlm =
586 container_of(work, struct vsp1_dl_manager, gc_work);
587
588 vsp1_dlm_fragments_free(dlm);
589}
590
553struct vsp1_dl_manager *vsp1_dlm_create(struct vsp1_device *vsp1, 591struct vsp1_dl_manager *vsp1_dlm_create(struct vsp1_device *vsp1,
554 unsigned int index, 592 unsigned int index,
555 unsigned int prealloc) 593 unsigned int prealloc)
@@ -568,6 +606,8 @@ struct vsp1_dl_manager *vsp1_dlm_create(struct vsp1_device *vsp1,
568 606
569 spin_lock_init(&dlm->lock); 607 spin_lock_init(&dlm->lock);
570 INIT_LIST_HEAD(&dlm->free); 608 INIT_LIST_HEAD(&dlm->free);
609 INIT_LIST_HEAD(&dlm->gc_fragments);
610 INIT_WORK(&dlm->gc_work, vsp1_dlm_garbage_collect);
571 611
572 for (i = 0; i < prealloc; ++i) { 612 for (i = 0; i < prealloc; ++i) {
573 struct vsp1_dl_list *dl; 613 struct vsp1_dl_list *dl;
@@ -589,8 +629,12 @@ void vsp1_dlm_destroy(struct vsp1_dl_manager *dlm)
589 if (!dlm) 629 if (!dlm)
590 return; 630 return;
591 631
632 cancel_work_sync(&dlm->gc_work);
633
592 list_for_each_entry_safe(dl, next, &dlm->free, list) { 634 list_for_each_entry_safe(dl, next, &dlm->free, list) {
593 list_del(&dl->list); 635 list_del(&dl->list);
594 vsp1_dl_list_free(dl); 636 vsp1_dl_list_free(dl);
595 } 637 }
638
639 vsp1_dlm_fragments_free(dlm);
596} 640}
diff --git a/drivers/media/platform/vsp1/vsp1_drm.c b/drivers/media/platform/vsp1/vsp1_drm.c
index fc4bbc401e67..fe9665e57b3b 100644
--- a/drivers/media/platform/vsp1/vsp1_drm.c
+++ b/drivers/media/platform/vsp1/vsp1_drm.c
@@ -230,42 +230,33 @@ EXPORT_SYMBOL_GPL(vsp1_du_atomic_begin);
230 * vsp1_du_atomic_update - Setup one RPF input of the VSP pipeline 230 * vsp1_du_atomic_update - Setup one RPF input of the VSP pipeline
231 * @dev: the VSP device 231 * @dev: the VSP device
232 * @rpf_index: index of the RPF to setup (0-based) 232 * @rpf_index: index of the RPF to setup (0-based)
233 * @pixelformat: V4L2 pixel format for the RPF memory input 233 * @cfg: the RPF configuration
234 * @pitch: number of bytes per line in the image stored in memory
235 * @mem: DMA addresses of the memory buffers (one per plane)
236 * @src: the source crop rectangle for the RPF
237 * @dst: the destination compose rectangle for the BRU input
238 * @alpha: global alpha value for the input
239 * @zpos: the Z-order position of the input
240 * 234 *
241 * Configure the VSP to perform composition of the image referenced by @mem 235 * Configure the VSP to perform image composition through RPF @rpf_index as
242 * through RPF @rpf_index, using the @src crop rectangle and the @dst 236 * described by the @cfg configuration. The image to compose is referenced by
237 * @cfg.mem and composed using the @cfg.src crop rectangle and the @cfg.dst
243 * composition rectangle. The Z-order is configurable with higher @zpos values 238 * composition rectangle. The Z-order is configurable with higher @zpos values
244 * displayed on top. 239 * displayed on top.
245 * 240 *
246 * Image format as stored in memory is expressed as a V4L2 @pixelformat value. 241 * If the @cfg configuration is NULL, the RPF will be disabled. Calling the
247 * As a special case, setting the pixel format to 0 will disable the RPF. The
248 * @pitch, @mem, @src and @dst parameters are ignored in that case. Calling the
249 * function on a disabled RPF is allowed. 242 * function on a disabled RPF is allowed.
250 * 243 *
251 * The memory pitch is configurable to allow for padding at end of lines, or 244 * Image format as stored in memory is expressed as a V4L2 @cfg.pixelformat
252 * simple for images that extend beyond the crop rectangle boundaries. The 245 * value. The memory pitch is configurable to allow for padding at end of lines,
253 * @pitch value is expressed in bytes and applies to all planes for multiplanar 246 * or simply for images that extend beyond the crop rectangle boundaries. The
254 * formats. 247 * @cfg.pitch value is expressed in bytes and applies to all planes for
248 * multiplanar formats.
255 * 249 *
256 * The source memory buffer is referenced by the DMA address of its planes in 250 * The source memory buffer is referenced by the DMA address of its planes in
257 * the @mem array. Up to two planes are supported. The second plane DMA address 251 * the @cfg.mem array. Up to two planes are supported. The second plane DMA
258 * is ignored for formats using a single plane. 252 * address is ignored for formats using a single plane.
259 * 253 *
260 * This function isn't reentrant, the caller needs to serialize calls. 254 * This function isn't reentrant, the caller needs to serialize calls.
261 * 255 *
262 * Return 0 on success or a negative error code on failure. 256 * Return 0 on success or a negative error code on failure.
263 */ 257 */
264int vsp1_du_atomic_update_ext(struct device *dev, unsigned int rpf_index, 258int vsp1_du_atomic_update(struct device *dev, unsigned int rpf_index,
265 u32 pixelformat, unsigned int pitch, 259 const struct vsp1_du_atomic_config *cfg)
266 dma_addr_t mem[2], const struct v4l2_rect *src,
267 const struct v4l2_rect *dst, unsigned int alpha,
268 unsigned int zpos)
269{ 260{
270 struct vsp1_device *vsp1 = dev_get_drvdata(dev); 261 struct vsp1_device *vsp1 = dev_get_drvdata(dev);
271 const struct vsp1_format_info *fmtinfo; 262 const struct vsp1_format_info *fmtinfo;
@@ -276,7 +267,7 @@ int vsp1_du_atomic_update_ext(struct device *dev, unsigned int rpf_index,
276 267
277 rpf = vsp1->rpf[rpf_index]; 268 rpf = vsp1->rpf[rpf_index];
278 269
279 if (pixelformat == 0) { 270 if (!cfg) {
280 dev_dbg(vsp1->dev, "%s: RPF%u: disable requested\n", __func__, 271 dev_dbg(vsp1->dev, "%s: RPF%u: disable requested\n", __func__,
281 rpf_index); 272 rpf_index);
282 273
@@ -287,38 +278,39 @@ int vsp1_du_atomic_update_ext(struct device *dev, unsigned int rpf_index,
287 dev_dbg(vsp1->dev, 278 dev_dbg(vsp1->dev,
288 "%s: RPF%u: (%u,%u)/%ux%u -> (%u,%u)/%ux%u (%08x), pitch %u dma { %pad, %pad } zpos %u\n", 279 "%s: RPF%u: (%u,%u)/%ux%u -> (%u,%u)/%ux%u (%08x), pitch %u dma { %pad, %pad } zpos %u\n",
289 __func__, rpf_index, 280 __func__, rpf_index,
290 src->left, src->top, src->width, src->height, 281 cfg->src.left, cfg->src.top, cfg->src.width, cfg->src.height,
291 dst->left, dst->top, dst->width, dst->height, 282 cfg->dst.left, cfg->dst.top, cfg->dst.width, cfg->dst.height,
292 pixelformat, pitch, &mem[0], &mem[1], zpos); 283 cfg->pixelformat, cfg->pitch, &cfg->mem[0], &cfg->mem[1],
284 cfg->zpos);
293 285
294 /* Store the format, stride, memory buffer address, crop and compose 286 /* Store the format, stride, memory buffer address, crop and compose
295 * rectangles and Z-order position and for the input. 287 * rectangles and Z-order position and for the input.
296 */ 288 */
297 fmtinfo = vsp1_get_format_info(pixelformat); 289 fmtinfo = vsp1_get_format_info(cfg->pixelformat);
298 if (!fmtinfo) { 290 if (!fmtinfo) {
299 dev_dbg(vsp1->dev, "Unsupport pixel format %08x for RPF\n", 291 dev_dbg(vsp1->dev, "Unsupport pixel format %08x for RPF\n",
300 pixelformat); 292 cfg->pixelformat);
301 return -EINVAL; 293 return -EINVAL;
302 } 294 }
303 295
304 rpf->fmtinfo = fmtinfo; 296 rpf->fmtinfo = fmtinfo;
305 rpf->format.num_planes = fmtinfo->planes; 297 rpf->format.num_planes = fmtinfo->planes;
306 rpf->format.plane_fmt[0].bytesperline = pitch; 298 rpf->format.plane_fmt[0].bytesperline = cfg->pitch;
307 rpf->format.plane_fmt[1].bytesperline = pitch; 299 rpf->format.plane_fmt[1].bytesperline = cfg->pitch;
308 rpf->alpha = alpha; 300 rpf->alpha = cfg->alpha;
309 301
310 rpf->mem.addr[0] = mem[0]; 302 rpf->mem.addr[0] = cfg->mem[0];
311 rpf->mem.addr[1] = mem[1]; 303 rpf->mem.addr[1] = cfg->mem[1];
312 rpf->mem.addr[2] = 0; 304 rpf->mem.addr[2] = 0;
313 305
314 vsp1->drm->inputs[rpf_index].crop = *src; 306 vsp1->drm->inputs[rpf_index].crop = cfg->src;
315 vsp1->drm->inputs[rpf_index].compose = *dst; 307 vsp1->drm->inputs[rpf_index].compose = cfg->dst;
316 vsp1->drm->inputs[rpf_index].zpos = zpos; 308 vsp1->drm->inputs[rpf_index].zpos = cfg->zpos;
317 vsp1->drm->inputs[rpf_index].enabled = true; 309 vsp1->drm->inputs[rpf_index].enabled = true;
318 310
319 return 0; 311 return 0;
320} 312}
321EXPORT_SYMBOL_GPL(vsp1_du_atomic_update_ext); 313EXPORT_SYMBOL_GPL(vsp1_du_atomic_update);
322 314
323static int vsp1_du_setup_rpf_pipe(struct vsp1_device *vsp1, 315static int vsp1_du_setup_rpf_pipe(struct vsp1_device *vsp1,
324 struct vsp1_rwpf *rpf, unsigned int bru_input) 316 struct vsp1_rwpf *rpf, unsigned int bru_input)
@@ -499,8 +491,10 @@ void vsp1_du_atomic_flush(struct device *dev)
499 491
500 vsp1_entity_route_setup(entity, pipe->dl); 492 vsp1_entity_route_setup(entity, pipe->dl);
501 493
502 if (entity->ops->configure) 494 if (entity->ops->configure) {
503 entity->ops->configure(entity, pipe, pipe->dl); 495 entity->ops->configure(entity, pipe, pipe->dl, true);
496 entity->ops->configure(entity, pipe, pipe->dl, false);
497 }
504 498
505 /* The memory buffer address must be applied after configuring 499 /* The memory buffer address must be applied after configuring
506 * the RPF to make sure the crop offset are computed. 500 * the RPF to make sure the crop offset are computed.
diff --git a/drivers/media/platform/vsp1/vsp1_drv.c b/drivers/media/platform/vsp1/vsp1_drv.c
index e2d779fac0eb..cc316d281687 100644
--- a/drivers/media/platform/vsp1/vsp1_drv.c
+++ b/drivers/media/platform/vsp1/vsp1_drv.c
@@ -19,12 +19,15 @@
19#include <linux/of.h> 19#include <linux/of.h>
20#include <linux/of_device.h> 20#include <linux/of_device.h>
21#include <linux/platform_device.h> 21#include <linux/platform_device.h>
22#include <linux/pm_runtime.h>
22#include <linux/videodev2.h> 23#include <linux/videodev2.h>
23 24
25#include <media/rcar-fcp.h>
24#include <media/v4l2-subdev.h> 26#include <media/v4l2-subdev.h>
25 27
26#include "vsp1.h" 28#include "vsp1.h"
27#include "vsp1_bru.h" 29#include "vsp1_bru.h"
30#include "vsp1_clu.h"
28#include "vsp1_dl.h" 31#include "vsp1_dl.h"
29#include "vsp1_drm.h" 32#include "vsp1_drm.h"
30#include "vsp1_hsit.h" 33#include "vsp1_hsit.h"
@@ -145,7 +148,7 @@ static int vsp1_uapi_create_links(struct vsp1_device *vsp1)
145 return ret; 148 return ret;
146 } 149 }
147 150
148 if (vsp1->info->features & VSP1_HAS_LIF) { 151 if (vsp1->lif) {
149 ret = media_create_pad_link(&vsp1->wpf[0]->entity.subdev.entity, 152 ret = media_create_pad_link(&vsp1->wpf[0]->entity.subdev.entity,
150 RWPF_PAD_SOURCE, 153 RWPF_PAD_SOURCE,
151 &vsp1->lif->entity.subdev.entity, 154 &vsp1->lif->entity.subdev.entity,
@@ -168,19 +171,15 @@ static int vsp1_uapi_create_links(struct vsp1_device *vsp1)
168 171
169 for (i = 0; i < vsp1->info->wpf_count; ++i) { 172 for (i = 0; i < vsp1->info->wpf_count; ++i) {
170 /* Connect the video device to the WPF. All connections are 173 /* Connect the video device to the WPF. All connections are
171 * immutable except for the WPF0 source link if a LIF is 174 * immutable.
172 * present.
173 */ 175 */
174 struct vsp1_rwpf *wpf = vsp1->wpf[i]; 176 struct vsp1_rwpf *wpf = vsp1->wpf[i];
175 unsigned int flags = MEDIA_LNK_FL_ENABLED;
176
177 if (!(vsp1->info->features & VSP1_HAS_LIF) || i != 0)
178 flags |= MEDIA_LNK_FL_IMMUTABLE;
179 177
180 ret = media_create_pad_link(&wpf->entity.subdev.entity, 178 ret = media_create_pad_link(&wpf->entity.subdev.entity,
181 RWPF_PAD_SOURCE, 179 RWPF_PAD_SOURCE,
182 &wpf->video->video.entity, 0, 180 &wpf->video->video.entity, 0,
183 flags); 181 MEDIA_LNK_FL_IMMUTABLE |
182 MEDIA_LNK_FL_ENABLED);
184 if (ret < 0) 183 if (ret < 0)
185 return ret; 184 return ret;
186 } 185 }
@@ -204,7 +203,8 @@ static void vsp1_destroy_entities(struct vsp1_device *vsp1)
204 } 203 }
205 204
206 v4l2_device_unregister(&vsp1->v4l2_dev); 205 v4l2_device_unregister(&vsp1->v4l2_dev);
207 media_device_unregister(&vsp1->media_dev); 206 if (vsp1->info->uapi)
207 media_device_unregister(&vsp1->media_dev);
208 media_device_cleanup(&vsp1->media_dev); 208 media_device_cleanup(&vsp1->media_dev);
209 209
210 if (!vsp1->info->uapi) 210 if (!vsp1->info->uapi)
@@ -252,6 +252,16 @@ static int vsp1_create_entities(struct vsp1_device *vsp1)
252 list_add_tail(&vsp1->bru->entity.list_dev, &vsp1->entities); 252 list_add_tail(&vsp1->bru->entity.list_dev, &vsp1->entities);
253 } 253 }
254 254
255 if (vsp1->info->features & VSP1_HAS_CLU) {
256 vsp1->clu = vsp1_clu_create(vsp1);
257 if (IS_ERR(vsp1->clu)) {
258 ret = PTR_ERR(vsp1->clu);
259 goto done;
260 }
261
262 list_add_tail(&vsp1->clu->entity.list_dev, &vsp1->entities);
263 }
264
255 vsp1->hsi = vsp1_hsit_create(vsp1, true); 265 vsp1->hsi = vsp1_hsit_create(vsp1, true);
256 if (IS_ERR(vsp1->hsi)) { 266 if (IS_ERR(vsp1->hsi)) {
257 ret = PTR_ERR(vsp1->hsi); 267 ret = PTR_ERR(vsp1->hsi);
@@ -268,7 +278,11 @@ static int vsp1_create_entities(struct vsp1_device *vsp1)
268 278
269 list_add_tail(&vsp1->hst->entity.list_dev, &vsp1->entities); 279 list_add_tail(&vsp1->hst->entity.list_dev, &vsp1->entities);
270 280
271 if (vsp1->info->features & VSP1_HAS_LIF) { 281 /* The LIF is only supported when used in conjunction with the DU, in
282 * which case the userspace API is disabled. If the userspace API is
283 * enabled skip the LIF, even when present.
284 */
285 if (vsp1->info->features & VSP1_HAS_LIF && !vsp1->info->uapi) {
272 vsp1->lif = vsp1_lif_create(vsp1); 286 vsp1->lif = vsp1_lif_create(vsp1);
273 if (IS_ERR(vsp1->lif)) { 287 if (IS_ERR(vsp1->lif)) {
274 ret = PTR_ERR(vsp1->lif); 288 ret = PTR_ERR(vsp1->lif);
@@ -379,14 +393,15 @@ static int vsp1_create_entities(struct vsp1_device *vsp1)
379 /* Register subdev nodes if the userspace API is enabled or initialize 393 /* Register subdev nodes if the userspace API is enabled or initialize
380 * the DRM pipeline otherwise. 394 * the DRM pipeline otherwise.
381 */ 395 */
382 if (vsp1->info->uapi) 396 if (vsp1->info->uapi) {
383 ret = v4l2_device_register_subdev_nodes(&vsp1->v4l2_dev); 397 ret = v4l2_device_register_subdev_nodes(&vsp1->v4l2_dev);
384 else 398 if (ret < 0)
385 ret = vsp1_drm_init(vsp1); 399 goto done;
386 if (ret < 0)
387 goto done;
388 400
389 ret = media_device_register(mdev); 401 ret = media_device_register(mdev);
402 } else {
403 ret = vsp1_drm_init(vsp1);
404 }
390 405
391done: 406done:
392 if (ret < 0) 407 if (ret < 0)
@@ -462,35 +477,16 @@ static int vsp1_device_init(struct vsp1_device *vsp1)
462/* 477/*
463 * vsp1_device_get - Acquire the VSP1 device 478 * vsp1_device_get - Acquire the VSP1 device
464 * 479 *
465 * Increment the VSP1 reference count and initialize the device if the first 480 * Make sure the device is not suspended and initialize it if needed.
466 * reference is taken.
467 * 481 *
468 * Return 0 on success or a negative error code otherwise. 482 * Return 0 on success or a negative error code otherwise.
469 */ 483 */
470int vsp1_device_get(struct vsp1_device *vsp1) 484int vsp1_device_get(struct vsp1_device *vsp1)
471{ 485{
472 int ret = 0; 486 int ret;
473
474 mutex_lock(&vsp1->lock);
475 if (vsp1->ref_count > 0)
476 goto done;
477
478 ret = clk_prepare_enable(vsp1->clock);
479 if (ret < 0)
480 goto done;
481
482 ret = vsp1_device_init(vsp1);
483 if (ret < 0) {
484 clk_disable_unprepare(vsp1->clock);
485 goto done;
486 }
487
488done:
489 if (!ret)
490 vsp1->ref_count++;
491 487
492 mutex_unlock(&vsp1->lock); 488 ret = pm_runtime_get_sync(vsp1->dev);
493 return ret; 489 return ret < 0 ? ret : 0;
494} 490}
495 491
496/* 492/*
@@ -501,54 +497,59 @@ done:
501 */ 497 */
502void vsp1_device_put(struct vsp1_device *vsp1) 498void vsp1_device_put(struct vsp1_device *vsp1)
503{ 499{
504 mutex_lock(&vsp1->lock); 500 pm_runtime_put_sync(vsp1->dev);
505
506 if (--vsp1->ref_count == 0)
507 clk_disable_unprepare(vsp1->clock);
508
509 mutex_unlock(&vsp1->lock);
510} 501}
511 502
512/* ----------------------------------------------------------------------------- 503/* -----------------------------------------------------------------------------
513 * Power Management 504 * Power Management
514 */ 505 */
515 506
516#ifdef CONFIG_PM_SLEEP 507static int __maybe_unused vsp1_pm_suspend(struct device *dev)
517static int vsp1_pm_suspend(struct device *dev)
518{ 508{
519 struct vsp1_device *vsp1 = dev_get_drvdata(dev); 509 struct vsp1_device *vsp1 = dev_get_drvdata(dev);
520 510
521 WARN_ON(mutex_is_locked(&vsp1->lock)); 511 vsp1_pipelines_suspend(vsp1);
512 pm_runtime_force_suspend(vsp1->dev);
522 513
523 if (vsp1->ref_count == 0) 514 return 0;
524 return 0; 515}
525 516
526 vsp1_pipelines_suspend(vsp1); 517static int __maybe_unused vsp1_pm_resume(struct device *dev)
518{
519 struct vsp1_device *vsp1 = dev_get_drvdata(dev);
527 520
528 clk_disable_unprepare(vsp1->clock); 521 pm_runtime_force_resume(vsp1->dev);
522 vsp1_pipelines_resume(vsp1);
529 523
530 return 0; 524 return 0;
531} 525}
532 526
533static int vsp1_pm_resume(struct device *dev) 527static int __maybe_unused vsp1_pm_runtime_suspend(struct device *dev)
534{ 528{
535 struct vsp1_device *vsp1 = dev_get_drvdata(dev); 529 struct vsp1_device *vsp1 = dev_get_drvdata(dev);
536 530
537 WARN_ON(mutex_is_locked(&vsp1->lock)); 531 rcar_fcp_disable(vsp1->fcp);
538 532
539 if (vsp1->ref_count == 0) 533 return 0;
540 return 0; 534}
541 535
542 clk_prepare_enable(vsp1->clock); 536static int __maybe_unused vsp1_pm_runtime_resume(struct device *dev)
537{
538 struct vsp1_device *vsp1 = dev_get_drvdata(dev);
539 int ret;
543 540
544 vsp1_pipelines_resume(vsp1); 541 if (vsp1->info) {
542 ret = vsp1_device_init(vsp1);
543 if (ret < 0)
544 return ret;
545 }
545 546
546 return 0; 547 return rcar_fcp_enable(vsp1->fcp);
547} 548}
548#endif
549 549
550static const struct dev_pm_ops vsp1_pm_ops = { 550static const struct dev_pm_ops vsp1_pm_ops = {
551 SET_SYSTEM_SLEEP_PM_OPS(vsp1_pm_suspend, vsp1_pm_resume) 551 SET_SYSTEM_SLEEP_PM_OPS(vsp1_pm_suspend, vsp1_pm_resume)
552 SET_RUNTIME_PM_OPS(vsp1_pm_runtime_suspend, vsp1_pm_runtime_resume, NULL)
552}; 553};
553 554
554/* ----------------------------------------------------------------------------- 555/* -----------------------------------------------------------------------------
@@ -559,7 +560,8 @@ static const struct vsp1_device_info vsp1_device_infos[] = {
559 { 560 {
560 .version = VI6_IP_VERSION_MODEL_VSPS_H2, 561 .version = VI6_IP_VERSION_MODEL_VSPS_H2,
561 .gen = 2, 562 .gen = 2,
562 .features = VSP1_HAS_BRU | VSP1_HAS_LUT | VSP1_HAS_SRU, 563 .features = VSP1_HAS_BRU | VSP1_HAS_CLU | VSP1_HAS_LUT
564 | VSP1_HAS_SRU | VSP1_HAS_WPF_VFLIP,
563 .rpf_count = 5, 565 .rpf_count = 5,
564 .uds_count = 3, 566 .uds_count = 3,
565 .wpf_count = 4, 567 .wpf_count = 4,
@@ -568,9 +570,9 @@ static const struct vsp1_device_info vsp1_device_infos[] = {
568 }, { 570 }, {
569 .version = VI6_IP_VERSION_MODEL_VSPR_H2, 571 .version = VI6_IP_VERSION_MODEL_VSPR_H2,
570 .gen = 2, 572 .gen = 2,
571 .features = VSP1_HAS_BRU | VSP1_HAS_SRU, 573 .features = VSP1_HAS_BRU | VSP1_HAS_SRU | VSP1_HAS_WPF_VFLIP,
572 .rpf_count = 5, 574 .rpf_count = 5,
573 .uds_count = 1, 575 .uds_count = 3,
574 .wpf_count = 4, 576 .wpf_count = 4,
575 .num_bru_inputs = 4, 577 .num_bru_inputs = 4,
576 .uapi = true, 578 .uapi = true,
@@ -580,22 +582,24 @@ static const struct vsp1_device_info vsp1_device_infos[] = {
580 .features = VSP1_HAS_BRU | VSP1_HAS_LIF | VSP1_HAS_LUT, 582 .features = VSP1_HAS_BRU | VSP1_HAS_LIF | VSP1_HAS_LUT,
581 .rpf_count = 4, 583 .rpf_count = 4,
582 .uds_count = 1, 584 .uds_count = 1,
583 .wpf_count = 4, 585 .wpf_count = 1,
584 .num_bru_inputs = 4, 586 .num_bru_inputs = 4,
585 .uapi = true, 587 .uapi = true,
586 }, { 588 }, {
587 .version = VI6_IP_VERSION_MODEL_VSPS_M2, 589 .version = VI6_IP_VERSION_MODEL_VSPS_M2,
588 .gen = 2, 590 .gen = 2,
589 .features = VSP1_HAS_BRU | VSP1_HAS_LUT | VSP1_HAS_SRU, 591 .features = VSP1_HAS_BRU | VSP1_HAS_CLU | VSP1_HAS_LUT
592 | VSP1_HAS_SRU | VSP1_HAS_WPF_VFLIP,
590 .rpf_count = 5, 593 .rpf_count = 5,
591 .uds_count = 3, 594 .uds_count = 1,
592 .wpf_count = 4, 595 .wpf_count = 4,
593 .num_bru_inputs = 4, 596 .num_bru_inputs = 4,
594 .uapi = true, 597 .uapi = true,
595 }, { 598 }, {
596 .version = VI6_IP_VERSION_MODEL_VSPI_GEN3, 599 .version = VI6_IP_VERSION_MODEL_VSPI_GEN3,
597 .gen = 3, 600 .gen = 3,
598 .features = VSP1_HAS_LUT | VSP1_HAS_SRU, 601 .features = VSP1_HAS_CLU | VSP1_HAS_LUT | VSP1_HAS_SRU
602 | VSP1_HAS_WPF_HFLIP | VSP1_HAS_WPF_VFLIP,
599 .rpf_count = 1, 603 .rpf_count = 1,
600 .uds_count = 1, 604 .uds_count = 1,
601 .wpf_count = 1, 605 .wpf_count = 1,
@@ -603,7 +607,7 @@ static const struct vsp1_device_info vsp1_device_infos[] = {
603 }, { 607 }, {
604 .version = VI6_IP_VERSION_MODEL_VSPBD_GEN3, 608 .version = VI6_IP_VERSION_MODEL_VSPBD_GEN3,
605 .gen = 3, 609 .gen = 3,
606 .features = VSP1_HAS_BRU, 610 .features = VSP1_HAS_BRU | VSP1_HAS_WPF_VFLIP,
607 .rpf_count = 5, 611 .rpf_count = 5,
608 .wpf_count = 1, 612 .wpf_count = 1,
609 .num_bru_inputs = 5, 613 .num_bru_inputs = 5,
@@ -611,7 +615,8 @@ static const struct vsp1_device_info vsp1_device_infos[] = {
611 }, { 615 }, {
612 .version = VI6_IP_VERSION_MODEL_VSPBC_GEN3, 616 .version = VI6_IP_VERSION_MODEL_VSPBC_GEN3,
613 .gen = 3, 617 .gen = 3,
614 .features = VSP1_HAS_BRU | VSP1_HAS_LUT, 618 .features = VSP1_HAS_BRU | VSP1_HAS_CLU | VSP1_HAS_LUT
619 | VSP1_HAS_WPF_VFLIP,
615 .rpf_count = 5, 620 .rpf_count = 5,
616 .wpf_count = 1, 621 .wpf_count = 1,
617 .num_bru_inputs = 5, 622 .num_bru_inputs = 5,
@@ -619,7 +624,7 @@ static const struct vsp1_device_info vsp1_device_infos[] = {
619 }, { 624 }, {
620 .version = VI6_IP_VERSION_MODEL_VSPD_GEN3, 625 .version = VI6_IP_VERSION_MODEL_VSPD_GEN3,
621 .gen = 3, 626 .gen = 3,
622 .features = VSP1_HAS_BRU | VSP1_HAS_LIF, 627 .features = VSP1_HAS_BRU | VSP1_HAS_LIF | VSP1_HAS_WPF_VFLIP,
623 .rpf_count = 5, 628 .rpf_count = 5,
624 .wpf_count = 2, 629 .wpf_count = 2,
625 .num_bru_inputs = 5, 630 .num_bru_inputs = 5,
@@ -629,6 +634,7 @@ static const struct vsp1_device_info vsp1_device_infos[] = {
629static int vsp1_probe(struct platform_device *pdev) 634static int vsp1_probe(struct platform_device *pdev)
630{ 635{
631 struct vsp1_device *vsp1; 636 struct vsp1_device *vsp1;
637 struct device_node *fcp_node;
632 struct resource *irq; 638 struct resource *irq;
633 struct resource *io; 639 struct resource *io;
634 unsigned int i; 640 unsigned int i;
@@ -640,22 +646,17 @@ static int vsp1_probe(struct platform_device *pdev)
640 return -ENOMEM; 646 return -ENOMEM;
641 647
642 vsp1->dev = &pdev->dev; 648 vsp1->dev = &pdev->dev;
643 mutex_init(&vsp1->lock);
644 INIT_LIST_HEAD(&vsp1->entities); 649 INIT_LIST_HEAD(&vsp1->entities);
645 INIT_LIST_HEAD(&vsp1->videos); 650 INIT_LIST_HEAD(&vsp1->videos);
646 651
647 /* I/O, IRQ and clock resources */ 652 platform_set_drvdata(pdev, vsp1);
653
654 /* I/O and IRQ resources (clock managed by the clock PM domain) */
648 io = platform_get_resource(pdev, IORESOURCE_MEM, 0); 655 io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
649 vsp1->mmio = devm_ioremap_resource(&pdev->dev, io); 656 vsp1->mmio = devm_ioremap_resource(&pdev->dev, io);
650 if (IS_ERR(vsp1->mmio)) 657 if (IS_ERR(vsp1->mmio))
651 return PTR_ERR(vsp1->mmio); 658 return PTR_ERR(vsp1->mmio);
652 659
653 vsp1->clock = devm_clk_get(&pdev->dev, NULL);
654 if (IS_ERR(vsp1->clock)) {
655 dev_err(&pdev->dev, "failed to get clock\n");
656 return PTR_ERR(vsp1->clock);
657 }
658
659 irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0); 660 irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
660 if (!irq) { 661 if (!irq) {
661 dev_err(&pdev->dev, "missing IRQ\n"); 662 dev_err(&pdev->dev, "missing IRQ\n");
@@ -669,13 +670,27 @@ static int vsp1_probe(struct platform_device *pdev)
669 return ret; 670 return ret;
670 } 671 }
671 672
673 /* FCP (optional) */
674 fcp_node = of_parse_phandle(pdev->dev.of_node, "renesas,fcp", 0);
675 if (fcp_node) {
676 vsp1->fcp = rcar_fcp_get(fcp_node);
677 of_node_put(fcp_node);
678 if (IS_ERR(vsp1->fcp)) {
679 dev_dbg(&pdev->dev, "FCP not found (%ld)\n",
680 PTR_ERR(vsp1->fcp));
681 return PTR_ERR(vsp1->fcp);
682 }
683 }
684
672 /* Configure device parameters based on the version register. */ 685 /* Configure device parameters based on the version register. */
673 ret = clk_prepare_enable(vsp1->clock); 686 pm_runtime_enable(&pdev->dev);
687
688 ret = pm_runtime_get_sync(&pdev->dev);
674 if (ret < 0) 689 if (ret < 0)
675 return ret; 690 goto done;
676 691
677 version = vsp1_read(vsp1, VI6_IP_VERSION); 692 version = vsp1_read(vsp1, VI6_IP_VERSION);
678 clk_disable_unprepare(vsp1->clock); 693 pm_runtime_put_sync(&pdev->dev);
679 694
680 for (i = 0; i < ARRAY_SIZE(vsp1_device_infos); ++i) { 695 for (i = 0; i < ARRAY_SIZE(vsp1_device_infos); ++i) {
681 if ((version & VI6_IP_VERSION_MODEL_MASK) == 696 if ((version & VI6_IP_VERSION_MODEL_MASK) ==
@@ -687,7 +702,8 @@ static int vsp1_probe(struct platform_device *pdev)
687 702
688 if (!vsp1->info) { 703 if (!vsp1->info) {
689 dev_err(&pdev->dev, "unsupported IP version 0x%08x\n", version); 704 dev_err(&pdev->dev, "unsupported IP version 0x%08x\n", version);
690 return -ENXIO; 705 ret = -ENXIO;
706 goto done;
691 } 707 }
692 708
693 dev_dbg(&pdev->dev, "IP version 0x%08x\n", version); 709 dev_dbg(&pdev->dev, "IP version 0x%08x\n", version);
@@ -696,12 +712,14 @@ static int vsp1_probe(struct platform_device *pdev)
696 ret = vsp1_create_entities(vsp1); 712 ret = vsp1_create_entities(vsp1);
697 if (ret < 0) { 713 if (ret < 0) {
698 dev_err(&pdev->dev, "failed to create entities\n"); 714 dev_err(&pdev->dev, "failed to create entities\n");
699 return ret; 715 goto done;
700 } 716 }
701 717
702 platform_set_drvdata(pdev, vsp1); 718done:
719 if (ret)
720 pm_runtime_disable(&pdev->dev);
703 721
704 return 0; 722 return ret;
705} 723}
706 724
707static int vsp1_remove(struct platform_device *pdev) 725static int vsp1_remove(struct platform_device *pdev)
@@ -709,6 +727,9 @@ static int vsp1_remove(struct platform_device *pdev)
709 struct vsp1_device *vsp1 = platform_get_drvdata(pdev); 727 struct vsp1_device *vsp1 = platform_get_drvdata(pdev);
710 728
711 vsp1_destroy_entities(vsp1); 729 vsp1_destroy_entities(vsp1);
730 rcar_fcp_put(vsp1->fcp);
731
732 pm_runtime_disable(&pdev->dev);
712 733
713 return 0; 734 return 0;
714} 735}
diff --git a/drivers/media/platform/vsp1/vsp1_entity.c b/drivers/media/platform/vsp1/vsp1_entity.c
index 3d070bcc6053..4cf6cc719c00 100644
--- a/drivers/media/platform/vsp1/vsp1_entity.c
+++ b/drivers/media/platform/vsp1/vsp1_entity.c
@@ -22,6 +22,12 @@
22#include "vsp1_dl.h" 22#include "vsp1_dl.h"
23#include "vsp1_entity.h" 23#include "vsp1_entity.h"
24 24
25static inline struct vsp1_entity *
26media_entity_to_vsp1_entity(struct media_entity *entity)
27{
28 return container_of(entity, struct vsp1_entity, subdev.entity);
29}
30
25void vsp1_entity_route_setup(struct vsp1_entity *source, 31void vsp1_entity_route_setup(struct vsp1_entity *source,
26 struct vsp1_dl_list *dl) 32 struct vsp1_dl_list *dl)
27{ 33{
@@ -30,7 +36,7 @@ void vsp1_entity_route_setup(struct vsp1_entity *source,
30 if (source->route->reg == 0) 36 if (source->route->reg == 0)
31 return; 37 return;
32 38
33 sink = container_of(source->sink, struct vsp1_entity, subdev.entity); 39 sink = media_entity_to_vsp1_entity(source->sink);
34 vsp1_dl_list_write(dl, source->route->reg, 40 vsp1_dl_list_write(dl, source->route->reg,
35 sink->route->inputs[source->sink_pad]); 41 sink->route->inputs[source->sink_pad]);
36} 42}
@@ -81,12 +87,30 @@ vsp1_entity_get_pad_format(struct vsp1_entity *entity,
81 return v4l2_subdev_get_try_format(&entity->subdev, cfg, pad); 87 return v4l2_subdev_get_try_format(&entity->subdev, cfg, pad);
82} 88}
83 89
90/**
91 * vsp1_entity_get_pad_selection - Get a pad selection from storage for entity
92 * @entity: the entity
93 * @cfg: the configuration storage
94 * @pad: the pad number
95 * @target: the selection target
96 *
97 * Return the selection rectangle stored in the given configuration for an
98 * entity's pad. The configuration can be an ACTIVE or TRY configuration. The
99 * selection target can be COMPOSE or CROP.
100 */
84struct v4l2_rect * 101struct v4l2_rect *
85vsp1_entity_get_pad_compose(struct vsp1_entity *entity, 102vsp1_entity_get_pad_selection(struct vsp1_entity *entity,
86 struct v4l2_subdev_pad_config *cfg, 103 struct v4l2_subdev_pad_config *cfg,
87 unsigned int pad) 104 unsigned int pad, unsigned int target)
88{ 105{
89 return v4l2_subdev_get_try_compose(&entity->subdev, cfg, pad); 106 switch (target) {
107 case V4L2_SEL_TGT_COMPOSE:
108 return v4l2_subdev_get_try_compose(&entity->subdev, cfg, pad);
109 case V4L2_SEL_TGT_CROP:
110 return v4l2_subdev_get_try_crop(&entity->subdev, cfg, pad);
111 default:
112 return NULL;
113 }
90} 114}
91 115
92/* 116/*
@@ -252,7 +276,7 @@ int vsp1_entity_link_setup(struct media_entity *entity,
252 if (!(local->flags & MEDIA_PAD_FL_SOURCE)) 276 if (!(local->flags & MEDIA_PAD_FL_SOURCE))
253 return 0; 277 return 0;
254 278
255 source = container_of(local->entity, struct vsp1_entity, subdev.entity); 279 source = media_entity_to_vsp1_entity(local->entity);
256 280
257 if (!source->route) 281 if (!source->route)
258 return 0; 282 return 0;
@@ -274,33 +298,50 @@ int vsp1_entity_link_setup(struct media_entity *entity,
274 * Initialization 298 * Initialization
275 */ 299 */
276 300
301#define VSP1_ENTITY_ROUTE(ent) \
302 { VSP1_ENTITY_##ent, 0, VI6_DPR_##ent##_ROUTE, \
303 { VI6_DPR_NODE_##ent }, VI6_DPR_NODE_##ent }
304
305#define VSP1_ENTITY_ROUTE_RPF(idx) \
306 { VSP1_ENTITY_RPF, idx, VI6_DPR_RPF_ROUTE(idx), \
307 { 0, }, VI6_DPR_NODE_RPF(idx) }
308
309#define VSP1_ENTITY_ROUTE_UDS(idx) \
310 { VSP1_ENTITY_UDS, idx, VI6_DPR_UDS_ROUTE(idx), \
311 { VI6_DPR_NODE_UDS(idx) }, VI6_DPR_NODE_UDS(idx) }
312
313#define VSP1_ENTITY_ROUTE_WPF(idx) \
314 { VSP1_ENTITY_WPF, idx, 0, \
315 { VI6_DPR_NODE_WPF(idx) }, VI6_DPR_NODE_WPF(idx) }
316
277static const struct vsp1_route vsp1_routes[] = { 317static const struct vsp1_route vsp1_routes[] = {
278 { VSP1_ENTITY_BRU, 0, VI6_DPR_BRU_ROUTE, 318 { VSP1_ENTITY_BRU, 0, VI6_DPR_BRU_ROUTE,
279 { VI6_DPR_NODE_BRU_IN(0), VI6_DPR_NODE_BRU_IN(1), 319 { VI6_DPR_NODE_BRU_IN(0), VI6_DPR_NODE_BRU_IN(1),
280 VI6_DPR_NODE_BRU_IN(2), VI6_DPR_NODE_BRU_IN(3), 320 VI6_DPR_NODE_BRU_IN(2), VI6_DPR_NODE_BRU_IN(3),
281 VI6_DPR_NODE_BRU_IN(4) } }, 321 VI6_DPR_NODE_BRU_IN(4) }, VI6_DPR_NODE_BRU_OUT },
282 { VSP1_ENTITY_HSI, 0, VI6_DPR_HSI_ROUTE, { VI6_DPR_NODE_HSI, } }, 322 VSP1_ENTITY_ROUTE(CLU),
283 { VSP1_ENTITY_HST, 0, VI6_DPR_HST_ROUTE, { VI6_DPR_NODE_HST, } }, 323 VSP1_ENTITY_ROUTE(HSI),
284 { VSP1_ENTITY_LIF, 0, 0, { VI6_DPR_NODE_LIF, } }, 324 VSP1_ENTITY_ROUTE(HST),
285 { VSP1_ENTITY_LUT, 0, VI6_DPR_LUT_ROUTE, { VI6_DPR_NODE_LUT, } }, 325 { VSP1_ENTITY_LIF, 0, 0, { VI6_DPR_NODE_LIF, }, VI6_DPR_NODE_LIF },
286 { VSP1_ENTITY_RPF, 0, VI6_DPR_RPF_ROUTE(0), { 0, } }, 326 VSP1_ENTITY_ROUTE(LUT),
287 { VSP1_ENTITY_RPF, 1, VI6_DPR_RPF_ROUTE(1), { 0, } }, 327 VSP1_ENTITY_ROUTE_RPF(0),
288 { VSP1_ENTITY_RPF, 2, VI6_DPR_RPF_ROUTE(2), { 0, } }, 328 VSP1_ENTITY_ROUTE_RPF(1),
289 { VSP1_ENTITY_RPF, 3, VI6_DPR_RPF_ROUTE(3), { 0, } }, 329 VSP1_ENTITY_ROUTE_RPF(2),
290 { VSP1_ENTITY_RPF, 4, VI6_DPR_RPF_ROUTE(4), { 0, } }, 330 VSP1_ENTITY_ROUTE_RPF(3),
291 { VSP1_ENTITY_SRU, 0, VI6_DPR_SRU_ROUTE, { VI6_DPR_NODE_SRU, } }, 331 VSP1_ENTITY_ROUTE_RPF(4),
292 { VSP1_ENTITY_UDS, 0, VI6_DPR_UDS_ROUTE(0), { VI6_DPR_NODE_UDS(0), } }, 332 VSP1_ENTITY_ROUTE(SRU),
293 { VSP1_ENTITY_UDS, 1, VI6_DPR_UDS_ROUTE(1), { VI6_DPR_NODE_UDS(1), } }, 333 VSP1_ENTITY_ROUTE_UDS(0),
294 { VSP1_ENTITY_UDS, 2, VI6_DPR_UDS_ROUTE(2), { VI6_DPR_NODE_UDS(2), } }, 334 VSP1_ENTITY_ROUTE_UDS(1),
295 { VSP1_ENTITY_WPF, 0, 0, { VI6_DPR_NODE_WPF(0), } }, 335 VSP1_ENTITY_ROUTE_UDS(2),
296 { VSP1_ENTITY_WPF, 1, 0, { VI6_DPR_NODE_WPF(1), } }, 336 VSP1_ENTITY_ROUTE_WPF(0),
297 { VSP1_ENTITY_WPF, 2, 0, { VI6_DPR_NODE_WPF(2), } }, 337 VSP1_ENTITY_ROUTE_WPF(1),
298 { VSP1_ENTITY_WPF, 3, 0, { VI6_DPR_NODE_WPF(3), } }, 338 VSP1_ENTITY_ROUTE_WPF(2),
339 VSP1_ENTITY_ROUTE_WPF(3),
299}; 340};
300 341
301int vsp1_entity_init(struct vsp1_device *vsp1, struct vsp1_entity *entity, 342int vsp1_entity_init(struct vsp1_device *vsp1, struct vsp1_entity *entity,
302 const char *name, unsigned int num_pads, 343 const char *name, unsigned int num_pads,
303 const struct v4l2_subdev_ops *ops) 344 const struct v4l2_subdev_ops *ops, u32 function)
304{ 345{
305 struct v4l2_subdev *subdev; 346 struct v4l2_subdev *subdev;
306 unsigned int i; 347 unsigned int i;
@@ -341,6 +382,7 @@ int vsp1_entity_init(struct vsp1_device *vsp1, struct vsp1_entity *entity,
341 subdev = &entity->subdev; 382 subdev = &entity->subdev;
342 v4l2_subdev_init(subdev, ops); 383 v4l2_subdev_init(subdev, ops);
343 384
385 subdev->entity.function = function;
344 subdev->entity.ops = &vsp1->media_ops; 386 subdev->entity.ops = &vsp1->media_ops;
345 subdev->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE; 387 subdev->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
346 388
diff --git a/drivers/media/platform/vsp1/vsp1_entity.h b/drivers/media/platform/vsp1/vsp1_entity.h
index 69eff4e17350..b43457fd2c43 100644
--- a/drivers/media/platform/vsp1/vsp1_entity.h
+++ b/drivers/media/platform/vsp1/vsp1_entity.h
@@ -24,6 +24,7 @@ struct vsp1_pipeline;
24 24
25enum vsp1_entity_type { 25enum vsp1_entity_type {
26 VSP1_ENTITY_BRU, 26 VSP1_ENTITY_BRU,
27 VSP1_ENTITY_CLU,
27 VSP1_ENTITY_HSI, 28 VSP1_ENTITY_HSI,
28 VSP1_ENTITY_HST, 29 VSP1_ENTITY_HST,
29 VSP1_ENTITY_LIF, 30 VSP1_ENTITY_LIF,
@@ -42,17 +43,21 @@ enum vsp1_entity_type {
42 * @index: Entity index this routing entry is associated with 43 * @index: Entity index this routing entry is associated with
43 * @reg: Output routing configuration register 44 * @reg: Output routing configuration register
44 * @inputs: Target node value for each input 45 * @inputs: Target node value for each input
46 * @output: Target node value for entity output
45 * 47 *
46 * Each $vsp1_route entry describes routing configuration for the entity 48 * Each $vsp1_route entry describes routing configuration for the entity
47 * specified by the entry's @type and @index. @reg indicates the register that 49 * specified by the entry's @type and @index. @reg indicates the register that
48 * holds output routing configuration for the entity, and the @inputs array 50 * holds output routing configuration for the entity, and the @inputs array
49 * store the target node value for each input of the entity. 51 * store the target node value for each input of the entity. The @output field
52 * stores the target node value of the entity output when used as a source for
53 * histogram generation.
50 */ 54 */
51struct vsp1_route { 55struct vsp1_route {
52 enum vsp1_entity_type type; 56 enum vsp1_entity_type type;
53 unsigned int index; 57 unsigned int index;
54 unsigned int reg; 58 unsigned int reg;
55 unsigned int inputs[VSP1_ENTITY_MAX_INPUTS]; 59 unsigned int inputs[VSP1_ENTITY_MAX_INPUTS];
60 unsigned int output;
56}; 61};
57 62
58/** 63/**
@@ -68,7 +73,7 @@ struct vsp1_entity_operations {
68 void (*destroy)(struct vsp1_entity *); 73 void (*destroy)(struct vsp1_entity *);
69 void (*set_memory)(struct vsp1_entity *, struct vsp1_dl_list *dl); 74 void (*set_memory)(struct vsp1_entity *, struct vsp1_dl_list *dl);
70 void (*configure)(struct vsp1_entity *, struct vsp1_pipeline *, 75 void (*configure)(struct vsp1_entity *, struct vsp1_pipeline *,
71 struct vsp1_dl_list *); 76 struct vsp1_dl_list *, bool);
72}; 77};
73 78
74struct vsp1_entity { 79struct vsp1_entity {
@@ -100,7 +105,7 @@ static inline struct vsp1_entity *to_vsp1_entity(struct v4l2_subdev *subdev)
100 105
101int vsp1_entity_init(struct vsp1_device *vsp1, struct vsp1_entity *entity, 106int vsp1_entity_init(struct vsp1_device *vsp1, struct vsp1_entity *entity,
102 const char *name, unsigned int num_pads, 107 const char *name, unsigned int num_pads,
103 const struct v4l2_subdev_ops *ops); 108 const struct v4l2_subdev_ops *ops, u32 function);
104void vsp1_entity_destroy(struct vsp1_entity *entity); 109void vsp1_entity_destroy(struct vsp1_entity *entity);
105 110
106extern const struct v4l2_subdev_internal_ops vsp1_subdev_internal_ops; 111extern const struct v4l2_subdev_internal_ops vsp1_subdev_internal_ops;
@@ -118,9 +123,9 @@ vsp1_entity_get_pad_format(struct vsp1_entity *entity,
118 struct v4l2_subdev_pad_config *cfg, 123 struct v4l2_subdev_pad_config *cfg,
119 unsigned int pad); 124 unsigned int pad);
120struct v4l2_rect * 125struct v4l2_rect *
121vsp1_entity_get_pad_compose(struct vsp1_entity *entity, 126vsp1_entity_get_pad_selection(struct vsp1_entity *entity,
122 struct v4l2_subdev_pad_config *cfg, 127 struct v4l2_subdev_pad_config *cfg,
123 unsigned int pad); 128 unsigned int pad, unsigned int target);
124int vsp1_entity_init_cfg(struct v4l2_subdev *subdev, 129int vsp1_entity_init_cfg(struct v4l2_subdev *subdev,
125 struct v4l2_subdev_pad_config *cfg); 130 struct v4l2_subdev_pad_config *cfg);
126 131
diff --git a/drivers/media/platform/vsp1/vsp1_hsit.c b/drivers/media/platform/vsp1/vsp1_hsit.c
index 68b8567b374d..6e5077beb38c 100644
--- a/drivers/media/platform/vsp1/vsp1_hsit.c
+++ b/drivers/media/platform/vsp1/vsp1_hsit.c
@@ -107,7 +107,7 @@ static int hsit_set_format(struct v4l2_subdev *subdev,
107 return 0; 107 return 0;
108} 108}
109 109
110static struct v4l2_subdev_pad_ops hsit_pad_ops = { 110static const struct v4l2_subdev_pad_ops hsit_pad_ops = {
111 .init_cfg = vsp1_entity_init_cfg, 111 .init_cfg = vsp1_entity_init_cfg,
112 .enum_mbus_code = hsit_enum_mbus_code, 112 .enum_mbus_code = hsit_enum_mbus_code,
113 .enum_frame_size = hsit_enum_frame_size, 113 .enum_frame_size = hsit_enum_frame_size,
@@ -115,7 +115,7 @@ static struct v4l2_subdev_pad_ops hsit_pad_ops = {
115 .set_fmt = hsit_set_format, 115 .set_fmt = hsit_set_format,
116}; 116};
117 117
118static struct v4l2_subdev_ops hsit_ops = { 118static const struct v4l2_subdev_ops hsit_ops = {
119 .pad = &hsit_pad_ops, 119 .pad = &hsit_pad_ops,
120}; 120};
121 121
@@ -125,10 +125,13 @@ static struct v4l2_subdev_ops hsit_ops = {
125 125
126static void hsit_configure(struct vsp1_entity *entity, 126static void hsit_configure(struct vsp1_entity *entity,
127 struct vsp1_pipeline *pipe, 127 struct vsp1_pipeline *pipe,
128 struct vsp1_dl_list *dl) 128 struct vsp1_dl_list *dl, bool full)
129{ 129{
130 struct vsp1_hsit *hsit = to_hsit(&entity->subdev); 130 struct vsp1_hsit *hsit = to_hsit(&entity->subdev);
131 131
132 if (!full)
133 return;
134
132 if (hsit->inverse) 135 if (hsit->inverse)
133 vsp1_hsit_write(hsit, dl, VI6_HSI_CTRL, VI6_HSI_CTRL_EN); 136 vsp1_hsit_write(hsit, dl, VI6_HSI_CTRL, VI6_HSI_CTRL_EN);
134 else 137 else
@@ -161,8 +164,9 @@ struct vsp1_hsit *vsp1_hsit_create(struct vsp1_device *vsp1, bool inverse)
161 else 164 else
162 hsit->entity.type = VSP1_ENTITY_HST; 165 hsit->entity.type = VSP1_ENTITY_HST;
163 166
164 ret = vsp1_entity_init(vsp1, &hsit->entity, inverse ? "hsi" : "hst", 2, 167 ret = vsp1_entity_init(vsp1, &hsit->entity, inverse ? "hsi" : "hst",
165 &hsit_ops); 168 2, &hsit_ops,
169 MEDIA_ENT_F_PROC_VIDEO_PIXEL_ENC_CONV);
166 if (ret < 0) 170 if (ret < 0)
167 return ERR_PTR(ret); 171 return ERR_PTR(ret);
168 172
diff --git a/drivers/media/platform/vsp1/vsp1_lif.c b/drivers/media/platform/vsp1/vsp1_lif.c
index 0217393f22df..a720063f38c5 100644
--- a/drivers/media/platform/vsp1/vsp1_lif.c
+++ b/drivers/media/platform/vsp1/vsp1_lif.c
@@ -104,7 +104,7 @@ static int lif_set_format(struct v4l2_subdev *subdev,
104 return 0; 104 return 0;
105} 105}
106 106
107static struct v4l2_subdev_pad_ops lif_pad_ops = { 107static const struct v4l2_subdev_pad_ops lif_pad_ops = {
108 .init_cfg = vsp1_entity_init_cfg, 108 .init_cfg = vsp1_entity_init_cfg,
109 .enum_mbus_code = lif_enum_mbus_code, 109 .enum_mbus_code = lif_enum_mbus_code,
110 .enum_frame_size = lif_enum_frame_size, 110 .enum_frame_size = lif_enum_frame_size,
@@ -112,7 +112,7 @@ static struct v4l2_subdev_pad_ops lif_pad_ops = {
112 .set_fmt = lif_set_format, 112 .set_fmt = lif_set_format,
113}; 113};
114 114
115static struct v4l2_subdev_ops lif_ops = { 115static const struct v4l2_subdev_ops lif_ops = {
116 .pad = &lif_pad_ops, 116 .pad = &lif_pad_ops,
117}; 117};
118 118
@@ -122,7 +122,7 @@ static struct v4l2_subdev_ops lif_ops = {
122 122
123static void lif_configure(struct vsp1_entity *entity, 123static void lif_configure(struct vsp1_entity *entity,
124 struct vsp1_pipeline *pipe, 124 struct vsp1_pipeline *pipe,
125 struct vsp1_dl_list *dl) 125 struct vsp1_dl_list *dl, bool full)
126{ 126{
127 const struct v4l2_mbus_framefmt *format; 127 const struct v4l2_mbus_framefmt *format;
128 struct vsp1_lif *lif = to_lif(&entity->subdev); 128 struct vsp1_lif *lif = to_lif(&entity->subdev);
@@ -130,6 +130,9 @@ static void lif_configure(struct vsp1_entity *entity,
130 unsigned int obth = 400; 130 unsigned int obth = 400;
131 unsigned int lbth = 200; 131 unsigned int lbth = 200;
132 132
133 if (!full)
134 return;
135
133 format = vsp1_entity_get_pad_format(&lif->entity, lif->entity.config, 136 format = vsp1_entity_get_pad_format(&lif->entity, lif->entity.config,
134 LIF_PAD_SOURCE); 137 LIF_PAD_SOURCE);
135 138
@@ -165,7 +168,12 @@ struct vsp1_lif *vsp1_lif_create(struct vsp1_device *vsp1)
165 lif->entity.ops = &lif_entity_ops; 168 lif->entity.ops = &lif_entity_ops;
166 lif->entity.type = VSP1_ENTITY_LIF; 169 lif->entity.type = VSP1_ENTITY_LIF;
167 170
168 ret = vsp1_entity_init(vsp1, &lif->entity, "lif", 2, &lif_ops); 171 /* The LIF is never exposed to userspace, but media entity registration
172 * requires a function to be set. Use PROC_VIDEO_PIXEL_FORMATTER just to
173 * avoid triggering a WARN_ON(), the value won't be seen anywhere.
174 */
175 ret = vsp1_entity_init(vsp1, &lif->entity, "lif", 2, &lif_ops,
176 MEDIA_ENT_F_PROC_VIDEO_PIXEL_FORMATTER);
169 if (ret < 0) 177 if (ret < 0)
170 return ERR_PTR(ret); 178 return ERR_PTR(ret);
171 179
diff --git a/drivers/media/platform/vsp1/vsp1_lut.c b/drivers/media/platform/vsp1/vsp1_lut.c
index aa09e59f0ab8..dc31de9602ba 100644
--- a/drivers/media/platform/vsp1/vsp1_lut.c
+++ b/drivers/media/platform/vsp1/vsp1_lut.c
@@ -13,7 +13,6 @@
13 13
14#include <linux/device.h> 14#include <linux/device.h>
15#include <linux/gfp.h> 15#include <linux/gfp.h>
16#include <linux/vsp1.h>
17 16
18#include <media/v4l2-subdev.h> 17#include <media/v4l2-subdev.h>
19 18
@@ -35,43 +34,62 @@ static inline void vsp1_lut_write(struct vsp1_lut *lut, struct vsp1_dl_list *dl,
35} 34}
36 35
37/* ----------------------------------------------------------------------------- 36/* -----------------------------------------------------------------------------
38 * V4L2 Subdevice Core Operations 37 * Controls
39 */ 38 */
40 39
41static int lut_set_table(struct vsp1_lut *lut, struct vsp1_lut_config *config) 40#define V4L2_CID_VSP1_LUT_TABLE (V4L2_CID_USER_BASE | 0x1001)
41
42static int lut_set_table(struct vsp1_lut *lut, struct v4l2_ctrl *ctrl)
42{ 43{
43 struct vsp1_dl_body *dlb; 44 struct vsp1_dl_body *dlb;
44 unsigned int i; 45 unsigned int i;
45 46
46 dlb = vsp1_dl_fragment_alloc(lut->entity.vsp1, ARRAY_SIZE(config->lut)); 47 dlb = vsp1_dl_fragment_alloc(lut->entity.vsp1, 256);
47 if (!dlb) 48 if (!dlb)
48 return -ENOMEM; 49 return -ENOMEM;
49 50
50 for (i = 0; i < ARRAY_SIZE(config->lut); ++i) 51 for (i = 0; i < 256; ++i)
51 vsp1_dl_fragment_write(dlb, VI6_LUT_TABLE + 4 * i, 52 vsp1_dl_fragment_write(dlb, VI6_LUT_TABLE + 4 * i,
52 config->lut[i]); 53 ctrl->p_new.p_u32[i]);
53 54
54 mutex_lock(&lut->lock); 55 spin_lock_irq(&lut->lock);
55 swap(lut->lut, dlb); 56 swap(lut->lut, dlb);
56 mutex_unlock(&lut->lock); 57 spin_unlock_irq(&lut->lock);
57 58
58 vsp1_dl_fragment_free(dlb); 59 vsp1_dl_fragment_free(dlb);
59 return 0; 60 return 0;
60} 61}
61 62
62static long lut_ioctl(struct v4l2_subdev *subdev, unsigned int cmd, void *arg) 63static int lut_s_ctrl(struct v4l2_ctrl *ctrl)
63{ 64{
64 struct vsp1_lut *lut = to_lut(subdev); 65 struct vsp1_lut *lut =
65 66 container_of(ctrl->handler, struct vsp1_lut, ctrls);
66 switch (cmd) {
67 case VIDIOC_VSP1_LUT_CONFIG:
68 return lut_set_table(lut, arg);
69 67
70 default: 68 switch (ctrl->id) {
71 return -ENOIOCTLCMD; 69 case V4L2_CID_VSP1_LUT_TABLE:
70 lut_set_table(lut, ctrl);
71 break;
72 } 72 }
73
74 return 0;
73} 75}
74 76
77static const struct v4l2_ctrl_ops lut_ctrl_ops = {
78 .s_ctrl = lut_s_ctrl,
79};
80
81static const struct v4l2_ctrl_config lut_table_control = {
82 .ops = &lut_ctrl_ops,
83 .id = V4L2_CID_VSP1_LUT_TABLE,
84 .name = "Look-Up Table",
85 .type = V4L2_CTRL_TYPE_U32,
86 .min = 0x00000000,
87 .max = 0x00ffffff,
88 .step = 1,
89 .def = 0,
90 .dims = { 256},
91};
92
75/* ----------------------------------------------------------------------------- 93/* -----------------------------------------------------------------------------
76 * V4L2 Subdevice Pad Operations 94 * V4L2 Subdevice Pad Operations
77 */ 95 */
@@ -147,11 +165,7 @@ static int lut_set_format(struct v4l2_subdev *subdev,
147 * V4L2 Subdevice Operations 165 * V4L2 Subdevice Operations
148 */ 166 */
149 167
150static struct v4l2_subdev_core_ops lut_core_ops = { 168static const struct v4l2_subdev_pad_ops lut_pad_ops = {
151 .ioctl = lut_ioctl,
152};
153
154static struct v4l2_subdev_pad_ops lut_pad_ops = {
155 .init_cfg = vsp1_entity_init_cfg, 169 .init_cfg = vsp1_entity_init_cfg,
156 .enum_mbus_code = lut_enum_mbus_code, 170 .enum_mbus_code = lut_enum_mbus_code,
157 .enum_frame_size = lut_enum_frame_size, 171 .enum_frame_size = lut_enum_frame_size,
@@ -159,8 +173,7 @@ static struct v4l2_subdev_pad_ops lut_pad_ops = {
159 .set_fmt = lut_set_format, 173 .set_fmt = lut_set_format,
160}; 174};
161 175
162static struct v4l2_subdev_ops lut_ops = { 176static const struct v4l2_subdev_ops lut_ops = {
163 .core = &lut_core_ops,
164 .pad = &lut_pad_ops, 177 .pad = &lut_pad_ops,
165}; 178};
166 179
@@ -170,18 +183,24 @@ static struct v4l2_subdev_ops lut_ops = {
170 183
171static void lut_configure(struct vsp1_entity *entity, 184static void lut_configure(struct vsp1_entity *entity,
172 struct vsp1_pipeline *pipe, 185 struct vsp1_pipeline *pipe,
173 struct vsp1_dl_list *dl) 186 struct vsp1_dl_list *dl, bool full)
174{ 187{
175 struct vsp1_lut *lut = to_lut(&entity->subdev); 188 struct vsp1_lut *lut = to_lut(&entity->subdev);
189 struct vsp1_dl_body *dlb;
190 unsigned long flags;
176 191
177 vsp1_lut_write(lut, dl, VI6_LUT_CTRL, VI6_LUT_CTRL_EN); 192 if (full) {
178 193 vsp1_lut_write(lut, dl, VI6_LUT_CTRL, VI6_LUT_CTRL_EN);
179 mutex_lock(&lut->lock); 194 return;
180 if (lut->lut) {
181 vsp1_dl_list_add_fragment(dl, lut->lut);
182 lut->lut = NULL;
183 } 195 }
184 mutex_unlock(&lut->lock); 196
197 spin_lock_irqsave(&lut->lock, flags);
198 dlb = lut->lut;
199 lut->lut = NULL;
200 spin_unlock_irqrestore(&lut->lock, flags);
201
202 if (dlb)
203 vsp1_dl_list_add_fragment(dl, dlb);
185} 204}
186 205
187static const struct vsp1_entity_operations lut_entity_ops = { 206static const struct vsp1_entity_operations lut_entity_ops = {
@@ -201,12 +220,30 @@ struct vsp1_lut *vsp1_lut_create(struct vsp1_device *vsp1)
201 if (lut == NULL) 220 if (lut == NULL)
202 return ERR_PTR(-ENOMEM); 221 return ERR_PTR(-ENOMEM);
203 222
223 spin_lock_init(&lut->lock);
224
204 lut->entity.ops = &lut_entity_ops; 225 lut->entity.ops = &lut_entity_ops;
205 lut->entity.type = VSP1_ENTITY_LUT; 226 lut->entity.type = VSP1_ENTITY_LUT;
206 227
207 ret = vsp1_entity_init(vsp1, &lut->entity, "lut", 2, &lut_ops); 228 ret = vsp1_entity_init(vsp1, &lut->entity, "lut", 2, &lut_ops,
229 MEDIA_ENT_F_PROC_VIDEO_LUT);
208 if (ret < 0) 230 if (ret < 0)
209 return ERR_PTR(ret); 231 return ERR_PTR(ret);
210 232
233 /* Initialize the control handler. */
234 v4l2_ctrl_handler_init(&lut->ctrls, 1);
235 v4l2_ctrl_new_custom(&lut->ctrls, &lut_table_control, NULL);
236
237 lut->entity.subdev.ctrl_handler = &lut->ctrls;
238
239 if (lut->ctrls.error) {
240 dev_err(vsp1->dev, "lut: failed to initialize controls\n");
241 ret = lut->ctrls.error;
242 vsp1_entity_destroy(&lut->entity);
243 return ERR_PTR(ret);
244 }
245
246 v4l2_ctrl_handler_setup(&lut->ctrls);
247
211 return lut; 248 return lut;
212} 249}
diff --git a/drivers/media/platform/vsp1/vsp1_lut.h b/drivers/media/platform/vsp1/vsp1_lut.h
index cef874f22b6a..f8c4e8f0a79d 100644
--- a/drivers/media/platform/vsp1/vsp1_lut.h
+++ b/drivers/media/platform/vsp1/vsp1_lut.h
@@ -13,9 +13,10 @@
13#ifndef __VSP1_LUT_H__ 13#ifndef __VSP1_LUT_H__
14#define __VSP1_LUT_H__ 14#define __VSP1_LUT_H__
15 15
16#include <linux/mutex.h> 16#include <linux/spinlock.h>
17 17
18#include <media/media-entity.h> 18#include <media/media-entity.h>
19#include <media/v4l2-ctrls.h>
19#include <media/v4l2-subdev.h> 20#include <media/v4l2-subdev.h>
20 21
21#include "vsp1_entity.h" 22#include "vsp1_entity.h"
@@ -28,7 +29,9 @@ struct vsp1_device;
28struct vsp1_lut { 29struct vsp1_lut {
29 struct vsp1_entity entity; 30 struct vsp1_entity entity;
30 31
31 struct mutex lock; 32 struct v4l2_ctrl_handler ctrls;
33
34 spinlock_t lock;
32 struct vsp1_dl_body *lut; 35 struct vsp1_dl_body *lut;
33}; 36};
34 37
diff --git a/drivers/media/platform/vsp1/vsp1_pipe.c b/drivers/media/platform/vsp1/vsp1_pipe.c
index 4f3b4a1d028a..3e75fb3fcace 100644
--- a/drivers/media/platform/vsp1/vsp1_pipe.c
+++ b/drivers/media/platform/vsp1/vsp1_pipe.c
@@ -172,13 +172,17 @@ void vsp1_pipeline_reset(struct vsp1_pipeline *pipe)
172 bru->inputs[i].rpf = NULL; 172 bru->inputs[i].rpf = NULL;
173 } 173 }
174 174
175 for (i = 0; i < pipe->num_inputs; ++i) { 175 for (i = 0; i < ARRAY_SIZE(pipe->inputs); ++i) {
176 pipe->inputs[i]->pipe = NULL; 176 if (pipe->inputs[i]) {
177 pipe->inputs[i] = NULL; 177 pipe->inputs[i]->pipe = NULL;
178 pipe->inputs[i] = NULL;
179 }
178 } 180 }
179 181
180 pipe->output->pipe = NULL; 182 if (pipe->output) {
181 pipe->output = NULL; 183 pipe->output->pipe = NULL;
184 pipe->output = NULL;
185 }
182 186
183 INIT_LIST_HEAD(&pipe->entities); 187 INIT_LIST_HEAD(&pipe->entities);
184 pipe->state = VSP1_PIPELINE_STOPPED; 188 pipe->state = VSP1_PIPELINE_STOPPED;
@@ -286,6 +290,8 @@ void vsp1_pipeline_frame_end(struct vsp1_pipeline *pipe)
286 290
287 if (pipe->frame_end) 291 if (pipe->frame_end)
288 pipe->frame_end(pipe); 292 pipe->frame_end(pipe);
293
294 pipe->sequence++;
289} 295}
290 296
291/* 297/*
@@ -295,42 +301,20 @@ void vsp1_pipeline_frame_end(struct vsp1_pipeline *pipe)
295 * to be scaled, we disable alpha scaling when the UDS input has a fixed alpha 301 * to be scaled, we disable alpha scaling when the UDS input has a fixed alpha
296 * value. The UDS then outputs a fixed alpha value which needs to be programmed 302 * value. The UDS then outputs a fixed alpha value which needs to be programmed
297 * from the input RPF alpha. 303 * from the input RPF alpha.
298 *
299 * This function can only be called from a subdev s_stream handler as it
300 * requires a valid display list context.
301 */ 304 */
302void vsp1_pipeline_propagate_alpha(struct vsp1_pipeline *pipe, 305void vsp1_pipeline_propagate_alpha(struct vsp1_pipeline *pipe,
303 struct vsp1_entity *input, 306 struct vsp1_dl_list *dl, unsigned int alpha)
304 struct vsp1_dl_list *dl,
305 unsigned int alpha)
306{ 307{
307 struct vsp1_entity *entity; 308 if (!pipe->uds)
308 struct media_pad *pad; 309 return;
309
310 pad = media_entity_remote_pad(&input->pads[RWPF_PAD_SOURCE]);
311
312 while (pad) {
313 if (!is_media_entity_v4l2_subdev(pad->entity))
314 break;
315
316 entity = to_vsp1_entity(media_entity_to_v4l2_subdev(pad->entity));
317
318 /* The BRU background color has a fixed alpha value set to 255,
319 * the output alpha value is thus always equal to 255.
320 */
321 if (entity->type == VSP1_ENTITY_BRU)
322 alpha = 255;
323
324 if (entity->type == VSP1_ENTITY_UDS) {
325 struct vsp1_uds *uds = to_uds(&entity->subdev);
326 310
327 vsp1_uds_set_alpha(uds, dl, alpha); 311 /* The BRU background color has a fixed alpha value set to 255, the
328 break; 312 * output alpha value is thus always equal to 255.
329 } 313 */
314 if (pipe->uds_input->type == VSP1_ENTITY_BRU)
315 alpha = 255;
330 316
331 pad = &entity->pads[entity->source_pad]; 317 vsp1_uds_set_alpha(pipe->uds, dl, alpha);
332 pad = media_entity_remote_pad(pad);
333 }
334} 318}
335 319
336void vsp1_pipelines_suspend(struct vsp1_device *vsp1) 320void vsp1_pipelines_suspend(struct vsp1_device *vsp1)
@@ -383,7 +367,7 @@ void vsp1_pipelines_resume(struct vsp1_device *vsp1)
383{ 367{
384 unsigned int i; 368 unsigned int i;
385 369
386 /* Resume pipeline all running pipelines. */ 370 /* Resume all running pipelines. */
387 for (i = 0; i < vsp1->info->wpf_count; ++i) { 371 for (i = 0; i < vsp1->info->wpf_count; ++i) {
388 struct vsp1_rwpf *wpf = vsp1->wpf[i]; 372 struct vsp1_rwpf *wpf = vsp1->wpf[i];
389 struct vsp1_pipeline *pipe; 373 struct vsp1_pipeline *pipe;
diff --git a/drivers/media/platform/vsp1/vsp1_pipe.h b/drivers/media/platform/vsp1/vsp1_pipe.h
index 7b56113511dd..d20d997b1fda 100644
--- a/drivers/media/platform/vsp1/vsp1_pipe.h
+++ b/drivers/media/platform/vsp1/vsp1_pipe.h
@@ -61,12 +61,13 @@ enum vsp1_pipeline_state {
61 * @pipe: the media pipeline 61 * @pipe: the media pipeline
62 * @irqlock: protects the pipeline state 62 * @irqlock: protects the pipeline state
63 * @state: current state 63 * @state: current state
64 * @wq: work queue to wait for state change completion 64 * @wq: wait queue to wait for state change completion
65 * @frame_end: frame end interrupt handler 65 * @frame_end: frame end interrupt handler
66 * @lock: protects the pipeline use count and stream count 66 * @lock: protects the pipeline use count and stream count
67 * @kref: pipeline reference count 67 * @kref: pipeline reference count
68 * @stream_count: number of streaming video nodes 68 * @stream_count: number of streaming video nodes
69 * @buffers_ready: bitmask of RPFs and WPFs with at least one buffer available 69 * @buffers_ready: bitmask of RPFs and WPFs with at least one buffer available
70 * @sequence: frame sequence number
70 * @num_inputs: number of RPFs 71 * @num_inputs: number of RPFs
71 * @inputs: array of RPFs in the pipeline (indexed by RPF index) 72 * @inputs: array of RPFs in the pipeline (indexed by RPF index)
72 * @output: WPF at the output of the pipeline 73 * @output: WPF at the output of the pipeline
@@ -90,6 +91,7 @@ struct vsp1_pipeline {
90 struct kref kref; 91 struct kref kref;
91 unsigned int stream_count; 92 unsigned int stream_count;
92 unsigned int buffers_ready; 93 unsigned int buffers_ready;
94 unsigned int sequence;
93 95
94 unsigned int num_inputs; 96 unsigned int num_inputs;
95 struct vsp1_rwpf *inputs[VSP1_MAX_RPF]; 97 struct vsp1_rwpf *inputs[VSP1_MAX_RPF];
@@ -115,9 +117,7 @@ bool vsp1_pipeline_ready(struct vsp1_pipeline *pipe);
115void vsp1_pipeline_frame_end(struct vsp1_pipeline *pipe); 117void vsp1_pipeline_frame_end(struct vsp1_pipeline *pipe);
116 118
117void vsp1_pipeline_propagate_alpha(struct vsp1_pipeline *pipe, 119void vsp1_pipeline_propagate_alpha(struct vsp1_pipeline *pipe,
118 struct vsp1_entity *input, 120 struct vsp1_dl_list *dl, unsigned int alpha);
119 struct vsp1_dl_list *dl,
120 unsigned int alpha);
121 121
122void vsp1_pipelines_suspend(struct vsp1_device *vsp1); 122void vsp1_pipelines_suspend(struct vsp1_device *vsp1);
123void vsp1_pipelines_resume(struct vsp1_device *vsp1); 123void vsp1_pipelines_resume(struct vsp1_device *vsp1);
diff --git a/drivers/media/platform/vsp1/vsp1_regs.h b/drivers/media/platform/vsp1/vsp1_regs.h
index 927b5fb94c48..3b03007ba625 100644
--- a/drivers/media/platform/vsp1/vsp1_regs.h
+++ b/drivers/media/platform/vsp1/vsp1_regs.h
@@ -154,10 +154,10 @@
154#define VI6_RPF_ALPH_SEL_AEXT_EXT (1 << 18) 154#define VI6_RPF_ALPH_SEL_AEXT_EXT (1 << 18)
155#define VI6_RPF_ALPH_SEL_AEXT_ONE (2 << 18) 155#define VI6_RPF_ALPH_SEL_AEXT_ONE (2 << 18)
156#define VI6_RPF_ALPH_SEL_AEXT_MASK (3 << 18) 156#define VI6_RPF_ALPH_SEL_AEXT_MASK (3 << 18)
157#define VI6_RPF_ALPH_SEL_ALPHA0_MASK (0xff << 8) 157#define VI6_RPF_ALPH_SEL_ALPHA1_MASK (0xff << 8)
158#define VI6_RPF_ALPH_SEL_ALPHA0_SHIFT 8 158#define VI6_RPF_ALPH_SEL_ALPHA1_SHIFT 8
159#define VI6_RPF_ALPH_SEL_ALPHA1_MASK (0xff << 0) 159#define VI6_RPF_ALPH_SEL_ALPHA0_MASK (0xff << 0)
160#define VI6_RPF_ALPH_SEL_ALPHA1_SHIFT 0 160#define VI6_RPF_ALPH_SEL_ALPHA0_SHIFT 0
161 161
162#define VI6_RPF_VRTCOL_SET 0x0318 162#define VI6_RPF_VRTCOL_SET 0x0318
163#define VI6_RPF_VRTCOL_SET_LAYA_MASK (0xff << 24) 163#define VI6_RPF_VRTCOL_SET_LAYA_MASK (0xff << 24)
@@ -255,6 +255,8 @@
255#define VI6_WPF_OUTFMT_PDV_MASK (0xff << 24) 255#define VI6_WPF_OUTFMT_PDV_MASK (0xff << 24)
256#define VI6_WPF_OUTFMT_PDV_SHIFT 24 256#define VI6_WPF_OUTFMT_PDV_SHIFT 24
257#define VI6_WPF_OUTFMT_PXA (1 << 23) 257#define VI6_WPF_OUTFMT_PXA (1 << 23)
258#define VI6_WPF_OUTFMT_ROT (1 << 18)
259#define VI6_WPF_OUTFMT_HFLP (1 << 17)
258#define VI6_WPF_OUTFMT_FLP (1 << 16) 260#define VI6_WPF_OUTFMT_FLP (1 << 16)
259#define VI6_WPF_OUTFMT_SPYCS (1 << 15) 261#define VI6_WPF_OUTFMT_SPYCS (1 << 15)
260#define VI6_WPF_OUTFMT_SPUVS (1 << 14) 262#define VI6_WPF_OUTFMT_SPUVS (1 << 14)
@@ -289,6 +291,11 @@
289#define VI6_WPF_RNDCTRL_CLMD_EXT (2 << 12) 291#define VI6_WPF_RNDCTRL_CLMD_EXT (2 << 12)
290#define VI6_WPF_RNDCTRL_CLMD_MASK (3 << 12) 292#define VI6_WPF_RNDCTRL_CLMD_MASK (3 << 12)
291 293
294#define VI6_WPF_ROT_CTRL 0x1018
295#define VI6_WPF_ROT_CTRL_LN16 (1 << 17)
296#define VI6_WPF_ROT_CTRL_LMEM_WD_MASK (0x1fff << 0)
297#define VI6_WPF_ROT_CTRL_LMEM_WD_SHIFT 0
298
292#define VI6_WPF_DSTM_STRIDE_Y 0x101c 299#define VI6_WPF_DSTM_STRIDE_Y 0x101c
293#define VI6_WPF_DSTM_STRIDE_C 0x1020 300#define VI6_WPF_DSTM_STRIDE_C 0x1020
294#define VI6_WPF_DSTM_ADDR_Y 0x1024 301#define VI6_WPF_DSTM_ADDR_Y 0x1024
@@ -444,6 +451,15 @@
444 */ 451 */
445 452
446#define VI6_CLU_CTRL 0x2900 453#define VI6_CLU_CTRL 0x2900
454#define VI6_CLU_CTRL_AAI (1 << 28)
455#define VI6_CLU_CTRL_MVS (1 << 24)
456#define VI6_CLU_CTRL_AX1I_2D (3 << 14)
457#define VI6_CLU_CTRL_AX2I_2D (1 << 12)
458#define VI6_CLU_CTRL_OS0_2D (3 << 8)
459#define VI6_CLU_CTRL_OS1_2D (1 << 6)
460#define VI6_CLU_CTRL_OS2_2D (3 << 4)
461#define VI6_CLU_CTRL_M2D (1 << 1)
462#define VI6_CLU_CTRL_EN (1 << 0)
447 463
448/* ----------------------------------------------------------------------------- 464/* -----------------------------------------------------------------------------
449 * HST Control Registers 465 * HST Control Registers
diff --git a/drivers/media/platform/vsp1/vsp1_rpf.c b/drivers/media/platform/vsp1/vsp1_rpf.c
index 49168db3f529..388838913205 100644
--- a/drivers/media/platform/vsp1/vsp1_rpf.c
+++ b/drivers/media/platform/vsp1/vsp1_rpf.c
@@ -38,7 +38,7 @@ static inline void vsp1_rpf_write(struct vsp1_rwpf *rpf,
38 * V4L2 Subdevice Operations 38 * V4L2 Subdevice Operations
39 */ 39 */
40 40
41static struct v4l2_subdev_ops rpf_ops = { 41static const struct v4l2_subdev_ops rpf_ops = {
42 .pad = &vsp1_rwpf_pad_ops, 42 .pad = &vsp1_rwpf_pad_ops,
43}; 43};
44 44
@@ -60,7 +60,7 @@ static void rpf_set_memory(struct vsp1_entity *entity, struct vsp1_dl_list *dl)
60 60
61static void rpf_configure(struct vsp1_entity *entity, 61static void rpf_configure(struct vsp1_entity *entity,
62 struct vsp1_pipeline *pipe, 62 struct vsp1_pipeline *pipe,
63 struct vsp1_dl_list *dl) 63 struct vsp1_dl_list *dl, bool full)
64{ 64{
65 struct vsp1_rwpf *rpf = to_rwpf(&entity->subdev); 65 struct vsp1_rwpf *rpf = to_rwpf(&entity->subdev);
66 const struct vsp1_format_info *fmtinfo = rpf->fmtinfo; 66 const struct vsp1_format_info *fmtinfo = rpf->fmtinfo;
@@ -73,6 +73,16 @@ static void rpf_configure(struct vsp1_entity *entity,
73 u32 pstride; 73 u32 pstride;
74 u32 infmt; 74 u32 infmt;
75 75
76 if (!full) {
77 vsp1_rpf_write(rpf, dl, VI6_RPF_VRTCOL_SET,
78 rpf->alpha << VI6_RPF_VRTCOL_SET_LAYA_SHIFT);
79 vsp1_rpf_write(rpf, dl, VI6_RPF_MULT_ALPHA, rpf->mult_alpha |
80 (rpf->alpha << VI6_RPF_MULT_ALPHA_RATIO_SHIFT));
81
82 vsp1_pipeline_propagate_alpha(pipe, dl, rpf->alpha);
83 return;
84 }
85
76 /* Source size, stride and crop offsets. 86 /* Source size, stride and crop offsets.
77 * 87 *
78 * The crop offsets correspond to the location of the crop rectangle top 88 * The crop offsets correspond to the location of the crop rectangle top
@@ -130,9 +140,10 @@ static void rpf_configure(struct vsp1_entity *entity,
130 if (pipe->bru) { 140 if (pipe->bru) {
131 const struct v4l2_rect *compose; 141 const struct v4l2_rect *compose;
132 142
133 compose = vsp1_entity_get_pad_compose(pipe->bru, 143 compose = vsp1_entity_get_pad_selection(pipe->bru,
134 pipe->bru->config, 144 pipe->bru->config,
135 rpf->bru_input); 145 rpf->bru_input,
146 V4L2_SEL_TGT_COMPOSE);
136 left = compose->left; 147 left = compose->left;
137 top = compose->top; 148 top = compose->top;
138 } 149 }
@@ -167,9 +178,6 @@ static void rpf_configure(struct vsp1_entity *entity,
167 (fmtinfo->alpha ? VI6_RPF_ALPH_SEL_ASEL_PACKED 178 (fmtinfo->alpha ? VI6_RPF_ALPH_SEL_ASEL_PACKED
168 : VI6_RPF_ALPH_SEL_ASEL_FIXED)); 179 : VI6_RPF_ALPH_SEL_ASEL_FIXED));
169 180
170 vsp1_rpf_write(rpf, dl, VI6_RPF_VRTCOL_SET,
171 rpf->alpha << VI6_RPF_VRTCOL_SET_LAYA_SHIFT);
172
173 if (entity->vsp1->info->gen == 3) { 181 if (entity->vsp1->info->gen == 3) {
174 u32 mult; 182 u32 mult;
175 183
@@ -187,8 +195,7 @@ static void rpf_configure(struct vsp1_entity *entity,
187 mult = VI6_RPF_MULT_ALPHA_A_MMD_RATIO 195 mult = VI6_RPF_MULT_ALPHA_A_MMD_RATIO
188 | (premultiplied ? 196 | (premultiplied ?
189 VI6_RPF_MULT_ALPHA_P_MMD_RATIO : 197 VI6_RPF_MULT_ALPHA_P_MMD_RATIO :
190 VI6_RPF_MULT_ALPHA_P_MMD_NONE) 198 VI6_RPF_MULT_ALPHA_P_MMD_NONE);
191 | (rpf->alpha << VI6_RPF_MULT_ALPHA_RATIO_SHIFT);
192 } else { 199 } else {
193 /* When the input doesn't contain an alpha channel the 200 /* When the input doesn't contain an alpha channel the
194 * global alpha value is applied in the unpacking unit, 201 * global alpha value is applied in the unpacking unit,
@@ -199,11 +206,9 @@ static void rpf_configure(struct vsp1_entity *entity,
199 | VI6_RPF_MULT_ALPHA_P_MMD_NONE; 206 | VI6_RPF_MULT_ALPHA_P_MMD_NONE;
200 } 207 }
201 208
202 vsp1_rpf_write(rpf, dl, VI6_RPF_MULT_ALPHA, mult); 209 rpf->mult_alpha = mult;
203 } 210 }
204 211
205 vsp1_pipeline_propagate_alpha(pipe, &rpf->entity, dl, rpf->alpha);
206
207 vsp1_rpf_write(rpf, dl, VI6_RPF_MSK_CTRL, 0); 212 vsp1_rpf_write(rpf, dl, VI6_RPF_MSK_CTRL, 0);
208 vsp1_rpf_write(rpf, dl, VI6_RPF_CKEY_CTRL, 0); 213 vsp1_rpf_write(rpf, dl, VI6_RPF_CKEY_CTRL, 0);
209 214
@@ -236,18 +241,21 @@ struct vsp1_rwpf *vsp1_rpf_create(struct vsp1_device *vsp1, unsigned int index)
236 rpf->entity.index = index; 241 rpf->entity.index = index;
237 242
238 sprintf(name, "rpf.%u", index); 243 sprintf(name, "rpf.%u", index);
239 ret = vsp1_entity_init(vsp1, &rpf->entity, name, 2, &rpf_ops); 244 ret = vsp1_entity_init(vsp1, &rpf->entity, name, 2, &rpf_ops,
245 MEDIA_ENT_F_PROC_VIDEO_PIXEL_FORMATTER);
240 if (ret < 0) 246 if (ret < 0)
241 return ERR_PTR(ret); 247 return ERR_PTR(ret);
242 248
243 /* Initialize the control handler. */ 249 /* Initialize the control handler. */
244 ret = vsp1_rwpf_init_ctrls(rpf); 250 ret = vsp1_rwpf_init_ctrls(rpf, 0);
245 if (ret < 0) { 251 if (ret < 0) {
246 dev_err(vsp1->dev, "rpf%u: failed to initialize controls\n", 252 dev_err(vsp1->dev, "rpf%u: failed to initialize controls\n",
247 index); 253 index);
248 goto error; 254 goto error;
249 } 255 }
250 256
257 v4l2_ctrl_handler_setup(&rpf->ctrls);
258
251 return rpf; 259 return rpf;
252 260
253error: 261error:
diff --git a/drivers/media/platform/vsp1/vsp1_rwpf.c b/drivers/media/platform/vsp1/vsp1_rwpf.c
index 3b6e032e7806..8d461b375e91 100644
--- a/drivers/media/platform/vsp1/vsp1_rwpf.c
+++ b/drivers/media/platform/vsp1/vsp1_rwpf.c
@@ -241,11 +241,9 @@ static const struct v4l2_ctrl_ops vsp1_rwpf_ctrl_ops = {
241 .s_ctrl = vsp1_rwpf_s_ctrl, 241 .s_ctrl = vsp1_rwpf_s_ctrl,
242}; 242};
243 243
244int vsp1_rwpf_init_ctrls(struct vsp1_rwpf *rwpf) 244int vsp1_rwpf_init_ctrls(struct vsp1_rwpf *rwpf, unsigned int ncontrols)
245{ 245{
246 rwpf->alpha = 255; 246 v4l2_ctrl_handler_init(&rwpf->ctrls, ncontrols + 1);
247
248 v4l2_ctrl_handler_init(&rwpf->ctrls, 1);
249 v4l2_ctrl_new_std(&rwpf->ctrls, &vsp1_rwpf_ctrl_ops, 247 v4l2_ctrl_new_std(&rwpf->ctrls, &vsp1_rwpf_ctrl_ops,
250 V4L2_CID_ALPHA_COMPONENT, 0, 255, 1, 255); 248 V4L2_CID_ALPHA_COMPONENT, 0, 255, 1, 255);
251 249
diff --git a/drivers/media/platform/vsp1/vsp1_rwpf.h b/drivers/media/platform/vsp1/vsp1_rwpf.h
index 9ff7c78f239e..cb20484e80da 100644
--- a/drivers/media/platform/vsp1/vsp1_rwpf.h
+++ b/drivers/media/platform/vsp1/vsp1_rwpf.h
@@ -13,6 +13,8 @@
13#ifndef __VSP1_RWPF_H__ 13#ifndef __VSP1_RWPF_H__
14#define __VSP1_RWPF_H__ 14#define __VSP1_RWPF_H__
15 15
16#include <linux/spinlock.h>
17
16#include <media/media-entity.h> 18#include <media/media-entity.h>
17#include <media/v4l2-ctrls.h> 19#include <media/v4l2-ctrls.h>
18#include <media/v4l2-subdev.h> 20#include <media/v4l2-subdev.h>
@@ -49,6 +51,16 @@ struct vsp1_rwpf {
49 51
50 unsigned int alpha; 52 unsigned int alpha;
51 53
54 u32 mult_alpha;
55 u32 outfmt;
56
57 struct {
58 spinlock_t lock;
59 struct v4l2_ctrl *ctrls[2];
60 unsigned int pending;
61 unsigned int active;
62 } flip;
63
52 unsigned int offsets[2]; 64 unsigned int offsets[2];
53 struct vsp1_rwpf_memory mem; 65 struct vsp1_rwpf_memory mem;
54 66
@@ -68,7 +80,7 @@ static inline struct vsp1_rwpf *entity_to_rwpf(struct vsp1_entity *entity)
68struct vsp1_rwpf *vsp1_rpf_create(struct vsp1_device *vsp1, unsigned int index); 80struct vsp1_rwpf *vsp1_rpf_create(struct vsp1_device *vsp1, unsigned int index);
69struct vsp1_rwpf *vsp1_wpf_create(struct vsp1_device *vsp1, unsigned int index); 81struct vsp1_rwpf *vsp1_wpf_create(struct vsp1_device *vsp1, unsigned int index);
70 82
71int vsp1_rwpf_init_ctrls(struct vsp1_rwpf *rwpf); 83int vsp1_rwpf_init_ctrls(struct vsp1_rwpf *rwpf, unsigned int ncontrols);
72 84
73extern const struct v4l2_subdev_pad_ops vsp1_rwpf_pad_ops; 85extern const struct v4l2_subdev_pad_ops vsp1_rwpf_pad_ops;
74 86
diff --git a/drivers/media/platform/vsp1/vsp1_sru.c b/drivers/media/platform/vsp1/vsp1_sru.c
index 97ef997ae735..47f5e0cea2ce 100644
--- a/drivers/media/platform/vsp1/vsp1_sru.c
+++ b/drivers/media/platform/vsp1/vsp1_sru.c
@@ -37,7 +37,7 @@ static inline void vsp1_sru_write(struct vsp1_sru *sru, struct vsp1_dl_list *dl,
37 * Controls 37 * Controls
38 */ 38 */
39 39
40#define V4L2_CID_VSP1_SRU_INTENSITY (V4L2_CID_USER_BASE + 1) 40#define V4L2_CID_VSP1_SRU_INTENSITY (V4L2_CID_USER_BASE | 0x1001)
41 41
42struct vsp1_sru_param { 42struct vsp1_sru_param {
43 u32 ctrl0; 43 u32 ctrl0;
@@ -239,7 +239,7 @@ static int sru_set_format(struct v4l2_subdev *subdev,
239 return 0; 239 return 0;
240} 240}
241 241
242static struct v4l2_subdev_pad_ops sru_pad_ops = { 242static const struct v4l2_subdev_pad_ops sru_pad_ops = {
243 .init_cfg = vsp1_entity_init_cfg, 243 .init_cfg = vsp1_entity_init_cfg,
244 .enum_mbus_code = sru_enum_mbus_code, 244 .enum_mbus_code = sru_enum_mbus_code,
245 .enum_frame_size = sru_enum_frame_size, 245 .enum_frame_size = sru_enum_frame_size,
@@ -247,7 +247,7 @@ static struct v4l2_subdev_pad_ops sru_pad_ops = {
247 .set_fmt = sru_set_format, 247 .set_fmt = sru_set_format,
248}; 248};
249 249
250static struct v4l2_subdev_ops sru_ops = { 250static const struct v4l2_subdev_ops sru_ops = {
251 .pad = &sru_pad_ops, 251 .pad = &sru_pad_ops,
252}; 252};
253 253
@@ -257,7 +257,7 @@ static struct v4l2_subdev_ops sru_ops = {
257 257
258static void sru_configure(struct vsp1_entity *entity, 258static void sru_configure(struct vsp1_entity *entity,
259 struct vsp1_pipeline *pipe, 259 struct vsp1_pipeline *pipe,
260 struct vsp1_dl_list *dl) 260 struct vsp1_dl_list *dl, bool full)
261{ 261{
262 const struct vsp1_sru_param *param; 262 const struct vsp1_sru_param *param;
263 struct vsp1_sru *sru = to_sru(&entity->subdev); 263 struct vsp1_sru *sru = to_sru(&entity->subdev);
@@ -265,6 +265,9 @@ static void sru_configure(struct vsp1_entity *entity,
265 struct v4l2_mbus_framefmt *output; 265 struct v4l2_mbus_framefmt *output;
266 u32 ctrl0; 266 u32 ctrl0;
267 267
268 if (!full)
269 return;
270
268 input = vsp1_entity_get_pad_format(&sru->entity, sru->entity.config, 271 input = vsp1_entity_get_pad_format(&sru->entity, sru->entity.config,
269 SRU_PAD_SINK); 272 SRU_PAD_SINK);
270 output = vsp1_entity_get_pad_format(&sru->entity, sru->entity.config, 273 output = vsp1_entity_get_pad_format(&sru->entity, sru->entity.config,
@@ -308,7 +311,8 @@ struct vsp1_sru *vsp1_sru_create(struct vsp1_device *vsp1)
308 sru->entity.ops = &sru_entity_ops; 311 sru->entity.ops = &sru_entity_ops;
309 sru->entity.type = VSP1_ENTITY_SRU; 312 sru->entity.type = VSP1_ENTITY_SRU;
310 313
311 ret = vsp1_entity_init(vsp1, &sru->entity, "sru", 2, &sru_ops); 314 ret = vsp1_entity_init(vsp1, &sru->entity, "sru", 2, &sru_ops,
315 MEDIA_ENT_F_PROC_VIDEO_SCALER);
312 if (ret < 0) 316 if (ret < 0)
313 return ERR_PTR(ret); 317 return ERR_PTR(ret);
314 318
diff --git a/drivers/media/platform/vsp1/vsp1_uds.c b/drivers/media/platform/vsp1/vsp1_uds.c
index 1875e29da184..652dcd895022 100644
--- a/drivers/media/platform/vsp1/vsp1_uds.c
+++ b/drivers/media/platform/vsp1/vsp1_uds.c
@@ -40,9 +40,11 @@ static inline void vsp1_uds_write(struct vsp1_uds *uds, struct vsp1_dl_list *dl,
40 * Scaling Computation 40 * Scaling Computation
41 */ 41 */
42 42
43void vsp1_uds_set_alpha(struct vsp1_uds *uds, struct vsp1_dl_list *dl, 43void vsp1_uds_set_alpha(struct vsp1_entity *entity, struct vsp1_dl_list *dl,
44 unsigned int alpha) 44 unsigned int alpha)
45{ 45{
46 struct vsp1_uds *uds = to_uds(&entity->subdev);
47
46 vsp1_uds_write(uds, dl, VI6_UDS_ALPVAL, 48 vsp1_uds_write(uds, dl, VI6_UDS_ALPVAL,
47 alpha << VI6_UDS_ALPVAL_VAL0_SHIFT); 49 alpha << VI6_UDS_ALPVAL_VAL0_SHIFT);
48} 50}
@@ -226,7 +228,7 @@ static int uds_set_format(struct v4l2_subdev *subdev,
226 * V4L2 Subdevice Operations 228 * V4L2 Subdevice Operations
227 */ 229 */
228 230
229static struct v4l2_subdev_pad_ops uds_pad_ops = { 231static const struct v4l2_subdev_pad_ops uds_pad_ops = {
230 .init_cfg = vsp1_entity_init_cfg, 232 .init_cfg = vsp1_entity_init_cfg,
231 .enum_mbus_code = uds_enum_mbus_code, 233 .enum_mbus_code = uds_enum_mbus_code,
232 .enum_frame_size = uds_enum_frame_size, 234 .enum_frame_size = uds_enum_frame_size,
@@ -234,7 +236,7 @@ static struct v4l2_subdev_pad_ops uds_pad_ops = {
234 .set_fmt = uds_set_format, 236 .set_fmt = uds_set_format,
235}; 237};
236 238
237static struct v4l2_subdev_ops uds_ops = { 239static const struct v4l2_subdev_ops uds_ops = {
238 .pad = &uds_pad_ops, 240 .pad = &uds_pad_ops,
239}; 241};
240 242
@@ -244,7 +246,7 @@ static struct v4l2_subdev_ops uds_ops = {
244 246
245static void uds_configure(struct vsp1_entity *entity, 247static void uds_configure(struct vsp1_entity *entity,
246 struct vsp1_pipeline *pipe, 248 struct vsp1_pipeline *pipe,
247 struct vsp1_dl_list *dl) 249 struct vsp1_dl_list *dl, bool full)
248{ 250{
249 struct vsp1_uds *uds = to_uds(&entity->subdev); 251 struct vsp1_uds *uds = to_uds(&entity->subdev);
250 const struct v4l2_mbus_framefmt *output; 252 const struct v4l2_mbus_framefmt *output;
@@ -253,6 +255,9 @@ static void uds_configure(struct vsp1_entity *entity,
253 unsigned int vscale; 255 unsigned int vscale;
254 bool multitap; 256 bool multitap;
255 257
258 if (!full)
259 return;
260
256 input = vsp1_entity_get_pad_format(&uds->entity, uds->entity.config, 261 input = vsp1_entity_get_pad_format(&uds->entity, uds->entity.config,
257 UDS_PAD_SINK); 262 UDS_PAD_SINK);
258 output = vsp1_entity_get_pad_format(&uds->entity, uds->entity.config, 263 output = vsp1_entity_get_pad_format(&uds->entity, uds->entity.config,
@@ -314,7 +319,8 @@ struct vsp1_uds *vsp1_uds_create(struct vsp1_device *vsp1, unsigned int index)
314 uds->entity.index = index; 319 uds->entity.index = index;
315 320
316 sprintf(name, "uds.%u", index); 321 sprintf(name, "uds.%u", index);
317 ret = vsp1_entity_init(vsp1, &uds->entity, name, 2, &uds_ops); 322 ret = vsp1_entity_init(vsp1, &uds->entity, name, 2, &uds_ops,
323 MEDIA_ENT_F_PROC_VIDEO_SCALER);
318 if (ret < 0) 324 if (ret < 0)
319 return ERR_PTR(ret); 325 return ERR_PTR(ret);
320 326
diff --git a/drivers/media/platform/vsp1/vsp1_uds.h b/drivers/media/platform/vsp1/vsp1_uds.h
index 5c8cbfcad4cc..7bf3cdcffc65 100644
--- a/drivers/media/platform/vsp1/vsp1_uds.h
+++ b/drivers/media/platform/vsp1/vsp1_uds.h
@@ -35,7 +35,7 @@ static inline struct vsp1_uds *to_uds(struct v4l2_subdev *subdev)
35 35
36struct vsp1_uds *vsp1_uds_create(struct vsp1_device *vsp1, unsigned int index); 36struct vsp1_uds *vsp1_uds_create(struct vsp1_device *vsp1, unsigned int index);
37 37
38void vsp1_uds_set_alpha(struct vsp1_uds *uds, struct vsp1_dl_list *dl, 38void vsp1_uds_set_alpha(struct vsp1_entity *uds, struct vsp1_dl_list *dl,
39 unsigned int alpha); 39 unsigned int alpha);
40 40
41#endif /* __VSP1_UDS_H__ */ 41#endif /* __VSP1_UDS_H__ */
diff --git a/drivers/media/platform/vsp1/vsp1_video.c b/drivers/media/platform/vsp1/vsp1_video.c
index a9aec5c0bec6..9fb4fc26a359 100644
--- a/drivers/media/platform/vsp1/vsp1_video.c
+++ b/drivers/media/platform/vsp1/vsp1_video.c
@@ -219,7 +219,7 @@ vsp1_video_complete_buffer(struct vsp1_video *video)
219 219
220 spin_unlock_irqrestore(&video->irqlock, flags); 220 spin_unlock_irqrestore(&video->irqlock, flags);
221 221
222 done->buf.sequence = video->sequence++; 222 done->buf.sequence = pipe->sequence;
223 done->buf.vb2_buf.timestamp = ktime_get_ns(); 223 done->buf.vb2_buf.timestamp = ktime_get_ns();
224 for (i = 0; i < done->buf.vb2_buf.num_planes; ++i) 224 for (i = 0; i < done->buf.vb2_buf.num_planes; ++i)
225 vb2_set_plane_payload(&done->buf.vb2_buf, i, 225 vb2_set_plane_payload(&done->buf.vb2_buf, i,
@@ -251,11 +251,17 @@ static void vsp1_video_frame_end(struct vsp1_pipeline *pipe,
251static void vsp1_video_pipeline_run(struct vsp1_pipeline *pipe) 251static void vsp1_video_pipeline_run(struct vsp1_pipeline *pipe)
252{ 252{
253 struct vsp1_device *vsp1 = pipe->output->entity.vsp1; 253 struct vsp1_device *vsp1 = pipe->output->entity.vsp1;
254 struct vsp1_entity *entity;
254 unsigned int i; 255 unsigned int i;
255 256
256 if (!pipe->dl) 257 if (!pipe->dl)
257 pipe->dl = vsp1_dl_list_get(pipe->output->dlm); 258 pipe->dl = vsp1_dl_list_get(pipe->output->dlm);
258 259
260 list_for_each_entry(entity, &pipe->entities, list_pipe) {
261 if (entity->ops->configure)
262 entity->ops->configure(entity, pipe, pipe->dl, false);
263 }
264
259 for (i = 0; i < vsp1->info->rpf_count; ++i) { 265 for (i = 0; i < vsp1->info->rpf_count; ++i) {
260 struct vsp1_rwpf *rwpf = pipe->inputs[i]; 266 struct vsp1_rwpf *rwpf = pipe->inputs[i];
261 267
@@ -519,8 +525,8 @@ static void vsp1_video_pipeline_put(struct vsp1_pipeline *pipe)
519 525
520static int 526static int
521vsp1_video_queue_setup(struct vb2_queue *vq, 527vsp1_video_queue_setup(struct vb2_queue *vq,
522 unsigned int *nbuffers, unsigned int *nplanes, 528 unsigned int *nbuffers, unsigned int *nplanes,
523 unsigned int sizes[], void *alloc_ctxs[]) 529 unsigned int sizes[], struct device *alloc_devs[])
524{ 530{
525 struct vsp1_video *video = vb2_get_drv_priv(vq); 531 struct vsp1_video *video = vb2_get_drv_priv(vq);
526 const struct v4l2_pix_format_mplane *format = &video->rwpf->format; 532 const struct v4l2_pix_format_mplane *format = &video->rwpf->format;
@@ -530,20 +536,16 @@ vsp1_video_queue_setup(struct vb2_queue *vq,
530 if (*nplanes != format->num_planes) 536 if (*nplanes != format->num_planes)
531 return -EINVAL; 537 return -EINVAL;
532 538
533 for (i = 0; i < *nplanes; i++) { 539 for (i = 0; i < *nplanes; i++)
534 if (sizes[i] < format->plane_fmt[i].sizeimage) 540 if (sizes[i] < format->plane_fmt[i].sizeimage)
535 return -EINVAL; 541 return -EINVAL;
536 alloc_ctxs[i] = video->alloc_ctx;
537 }
538 return 0; 542 return 0;
539 } 543 }
540 544
541 *nplanes = format->num_planes; 545 *nplanes = format->num_planes;
542 546
543 for (i = 0; i < format->num_planes; ++i) { 547 for (i = 0; i < format->num_planes; ++i)
544 sizes[i] = format->plane_fmt[i].sizeimage; 548 sizes[i] = format->plane_fmt[i].sizeimage;
545 alloc_ctxs[i] = video->alloc_ctx;
546 }
547 549
548 return 0; 550 return 0;
549} 551}
@@ -632,7 +634,7 @@ static int vsp1_video_setup_pipeline(struct vsp1_pipeline *pipe)
632 vsp1_entity_route_setup(entity, pipe->dl); 634 vsp1_entity_route_setup(entity, pipe->dl);
633 635
634 if (entity->ops->configure) 636 if (entity->ops->configure)
635 entity->ops->configure(entity, pipe, pipe->dl); 637 entity->ops->configure(entity, pipe, pipe->dl, true);
636 } 638 }
637 639
638 return 0; 640 return 0;
@@ -674,7 +676,7 @@ static void vsp1_video_stop_streaming(struct vb2_queue *vq)
674 int ret; 676 int ret;
675 677
676 mutex_lock(&pipe->lock); 678 mutex_lock(&pipe->lock);
677 if (--pipe->stream_count == 0) { 679 if (--pipe->stream_count == pipe->num_inputs) {
678 /* Stop the pipeline. */ 680 /* Stop the pipeline. */
679 ret = vsp1_pipeline_stop(pipe); 681 ret = vsp1_pipeline_stop(pipe);
680 if (ret == -ETIMEDOUT) 682 if (ret == -ETIMEDOUT)
@@ -696,7 +698,7 @@ static void vsp1_video_stop_streaming(struct vb2_queue *vq)
696 spin_unlock_irqrestore(&video->irqlock, flags); 698 spin_unlock_irqrestore(&video->irqlock, flags);
697} 699}
698 700
699static struct vb2_ops vsp1_video_queue_qops = { 701static const struct vb2_ops vsp1_video_queue_qops = {
700 .queue_setup = vsp1_video_queue_setup, 702 .queue_setup = vsp1_video_queue_setup,
701 .buf_prepare = vsp1_video_buffer_prepare, 703 .buf_prepare = vsp1_video_buffer_prepare,
702 .buf_queue = vsp1_video_buffer_queue, 704 .buf_queue = vsp1_video_buffer_queue,
@@ -805,8 +807,6 @@ vsp1_video_streamon(struct file *file, void *fh, enum v4l2_buf_type type)
805 if (video->queue.owner && video->queue.owner != file->private_data) 807 if (video->queue.owner && video->queue.owner != file->private_data)
806 return -EBUSY; 808 return -EBUSY;
807 809
808 video->sequence = 0;
809
810 /* Get a pipeline for the video node and start streaming on it. No link 810 /* Get a pipeline for the video node and start streaming on it. No link
811 * touching an entity in the pipeline can be activated or deactivated 811 * touching an entity in the pipeline can be activated or deactivated
812 * once streaming is started. 812 * once streaming is started.
@@ -915,7 +915,7 @@ static int vsp1_video_release(struct file *file)
915 return 0; 915 return 0;
916} 916}
917 917
918static struct v4l2_file_operations vsp1_video_fops = { 918static const struct v4l2_file_operations vsp1_video_fops = {
919 .owner = THIS_MODULE, 919 .owner = THIS_MODULE,
920 .unlocked_ioctl = video_ioctl2, 920 .unlocked_ioctl = video_ioctl2,
921 .open = vsp1_video_open, 921 .open = vsp1_video_open,
@@ -982,13 +982,6 @@ struct vsp1_video *vsp1_video_create(struct vsp1_device *vsp1,
982 982
983 video_set_drvdata(&video->video, video); 983 video_set_drvdata(&video->video, video);
984 984
985 /* ... and the buffers queue... */
986 video->alloc_ctx = vb2_dma_contig_init_ctx(video->vsp1->dev);
987 if (IS_ERR(video->alloc_ctx)) {
988 ret = PTR_ERR(video->alloc_ctx);
989 goto error;
990 }
991
992 video->queue.type = video->type; 985 video->queue.type = video->type;
993 video->queue.io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF; 986 video->queue.io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
994 video->queue.lock = &video->lock; 987 video->queue.lock = &video->lock;
@@ -997,6 +990,7 @@ struct vsp1_video *vsp1_video_create(struct vsp1_device *vsp1,
997 video->queue.ops = &vsp1_video_queue_qops; 990 video->queue.ops = &vsp1_video_queue_qops;
998 video->queue.mem_ops = &vb2_dma_contig_memops; 991 video->queue.mem_ops = &vb2_dma_contig_memops;
999 video->queue.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY; 992 video->queue.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
993 video->queue.dev = video->vsp1->dev;
1000 ret = vb2_queue_init(&video->queue); 994 ret = vb2_queue_init(&video->queue);
1001 if (ret < 0) { 995 if (ret < 0) {
1002 dev_err(video->vsp1->dev, "failed to initialize vb2 queue\n"); 996 dev_err(video->vsp1->dev, "failed to initialize vb2 queue\n");
@@ -1014,7 +1008,6 @@ struct vsp1_video *vsp1_video_create(struct vsp1_device *vsp1,
1014 return video; 1008 return video;
1015 1009
1016error: 1010error:
1017 vb2_dma_contig_cleanup_ctx(video->alloc_ctx);
1018 vsp1_video_cleanup(video); 1011 vsp1_video_cleanup(video);
1019 return ERR_PTR(ret); 1012 return ERR_PTR(ret);
1020} 1013}
@@ -1024,6 +1017,5 @@ void vsp1_video_cleanup(struct vsp1_video *video)
1024 if (video_is_registered(&video->video)) 1017 if (video_is_registered(&video->video))
1025 video_unregister_device(&video->video); 1018 video_unregister_device(&video->video);
1026 1019
1027 vb2_dma_contig_cleanup_ctx(video->alloc_ctx);
1028 media_entity_cleanup(&video->video.entity); 1020 media_entity_cleanup(&video->video.entity);
1029} 1021}
diff --git a/drivers/media/platform/vsp1/vsp1_video.h b/drivers/media/platform/vsp1/vsp1_video.h
index 867b00807c46..50ea7f02205f 100644
--- a/drivers/media/platform/vsp1/vsp1_video.h
+++ b/drivers/media/platform/vsp1/vsp1_video.h
@@ -46,10 +46,8 @@ struct vsp1_video {
46 unsigned int pipe_index; 46 unsigned int pipe_index;
47 47
48 struct vb2_queue queue; 48 struct vb2_queue queue;
49 void *alloc_ctx;
50 spinlock_t irqlock; 49 spinlock_t irqlock;
51 struct list_head irqqueue; 50 struct list_head irqqueue;
52 unsigned int sequence;
53}; 51};
54 52
55static inline struct vsp1_video *to_vsp1_video(struct video_device *vdev) 53static inline struct vsp1_video *to_vsp1_video(struct video_device *vdev)
diff --git a/drivers/media/platform/vsp1/vsp1_wpf.c b/drivers/media/platform/vsp1/vsp1_wpf.c
index 6c91eaa35e75..31983169c24a 100644
--- a/drivers/media/platform/vsp1/vsp1_wpf.c
+++ b/drivers/media/platform/vsp1/vsp1_wpf.c
@@ -37,6 +37,97 @@ static inline void vsp1_wpf_write(struct vsp1_rwpf *wpf,
37} 37}
38 38
39/* ----------------------------------------------------------------------------- 39/* -----------------------------------------------------------------------------
40 * Controls
41 */
42
43enum wpf_flip_ctrl {
44 WPF_CTRL_VFLIP = 0,
45 WPF_CTRL_HFLIP = 1,
46 WPF_CTRL_MAX,
47};
48
49static int vsp1_wpf_s_ctrl(struct v4l2_ctrl *ctrl)
50{
51 struct vsp1_rwpf *wpf =
52 container_of(ctrl->handler, struct vsp1_rwpf, ctrls);
53 unsigned int i;
54 u32 flip = 0;
55
56 switch (ctrl->id) {
57 case V4L2_CID_HFLIP:
58 case V4L2_CID_VFLIP:
59 for (i = 0; i < WPF_CTRL_MAX; ++i) {
60 if (wpf->flip.ctrls[i])
61 flip |= wpf->flip.ctrls[i]->val ? BIT(i) : 0;
62 }
63
64 spin_lock_irq(&wpf->flip.lock);
65 wpf->flip.pending = flip;
66 spin_unlock_irq(&wpf->flip.lock);
67 break;
68
69 default:
70 return -EINVAL;
71 }
72
73 return 0;
74}
75
76static const struct v4l2_ctrl_ops vsp1_wpf_ctrl_ops = {
77 .s_ctrl = vsp1_wpf_s_ctrl,
78};
79
80static int wpf_init_controls(struct vsp1_rwpf *wpf)
81{
82 struct vsp1_device *vsp1 = wpf->entity.vsp1;
83 unsigned int num_flip_ctrls;
84
85 spin_lock_init(&wpf->flip.lock);
86
87 if (wpf->entity.index != 0) {
88 /* Only WPF0 supports flipping. */
89 num_flip_ctrls = 0;
90 } else if (vsp1->info->features & VSP1_HAS_WPF_HFLIP) {
91 /* When horizontal flip is supported the WPF implements two
92 * controls (horizontal flip and vertical flip).
93 */
94 num_flip_ctrls = 2;
95 } else if (vsp1->info->features & VSP1_HAS_WPF_VFLIP) {
96 /* When only vertical flip is supported the WPF implements a
97 * single control (vertical flip).
98 */
99 num_flip_ctrls = 1;
100 } else {
101 /* Otherwise flipping is not supported. */
102 num_flip_ctrls = 0;
103 }
104
105 vsp1_rwpf_init_ctrls(wpf, num_flip_ctrls);
106
107 if (num_flip_ctrls >= 1) {
108 wpf->flip.ctrls[WPF_CTRL_VFLIP] =
109 v4l2_ctrl_new_std(&wpf->ctrls, &vsp1_wpf_ctrl_ops,
110 V4L2_CID_VFLIP, 0, 1, 1, 0);
111 }
112
113 if (num_flip_ctrls == 2) {
114 wpf->flip.ctrls[WPF_CTRL_HFLIP] =
115 v4l2_ctrl_new_std(&wpf->ctrls, &vsp1_wpf_ctrl_ops,
116 V4L2_CID_HFLIP, 0, 1, 1, 0);
117
118 v4l2_ctrl_cluster(2, wpf->flip.ctrls);
119 }
120
121 if (wpf->ctrls.error) {
122 dev_err(vsp1->dev, "wpf%u: failed to initialize controls\n",
123 wpf->entity.index);
124 return wpf->ctrls.error;
125 }
126
127 return 0;
128}
129
130/* -----------------------------------------------------------------------------
40 * V4L2 Subdevice Core Operations 131 * V4L2 Subdevice Core Operations
41 */ 132 */
42 133
@@ -62,11 +153,11 @@ static int wpf_s_stream(struct v4l2_subdev *subdev, int enable)
62 * V4L2 Subdevice Operations 153 * V4L2 Subdevice Operations
63 */ 154 */
64 155
65static struct v4l2_subdev_video_ops wpf_video_ops = { 156static const struct v4l2_subdev_video_ops wpf_video_ops = {
66 .s_stream = wpf_s_stream, 157 .s_stream = wpf_s_stream,
67}; 158};
68 159
69static struct v4l2_subdev_ops wpf_ops = { 160static const struct v4l2_subdev_ops wpf_ops = {
70 .video = &wpf_video_ops, 161 .video = &wpf_video_ops,
71 .pad = &vsp1_rwpf_pad_ops, 162 .pad = &vsp1_rwpf_pad_ops,
72}; 163};
@@ -85,15 +176,37 @@ static void vsp1_wpf_destroy(struct vsp1_entity *entity)
85static void wpf_set_memory(struct vsp1_entity *entity, struct vsp1_dl_list *dl) 176static void wpf_set_memory(struct vsp1_entity *entity, struct vsp1_dl_list *dl)
86{ 177{
87 struct vsp1_rwpf *wpf = entity_to_rwpf(entity); 178 struct vsp1_rwpf *wpf = entity_to_rwpf(entity);
179 const struct v4l2_pix_format_mplane *format = &wpf->format;
180 struct vsp1_rwpf_memory mem = wpf->mem;
181 unsigned int flip = wpf->flip.active;
182 unsigned int offset;
183
184 /* Update the memory offsets based on flipping configuration. The
185 * destination addresses point to the locations where the VSP starts
186 * writing to memory, which can be different corners of the image
187 * depending on vertical flipping. Horizontal flipping is handled
188 * through a line buffer and doesn't modify the start address.
189 */
190 if (flip & BIT(WPF_CTRL_VFLIP)) {
191 mem.addr[0] += (format->height - 1)
192 * format->plane_fmt[0].bytesperline;
193
194 if (format->num_planes > 1) {
195 offset = (format->height / wpf->fmtinfo->vsub - 1)
196 * format->plane_fmt[1].bytesperline;
197 mem.addr[1] += offset;
198 mem.addr[2] += offset;
199 }
200 }
88 201
89 vsp1_wpf_write(wpf, dl, VI6_WPF_DSTM_ADDR_Y, wpf->mem.addr[0]); 202 vsp1_wpf_write(wpf, dl, VI6_WPF_DSTM_ADDR_Y, mem.addr[0]);
90 vsp1_wpf_write(wpf, dl, VI6_WPF_DSTM_ADDR_C0, wpf->mem.addr[1]); 203 vsp1_wpf_write(wpf, dl, VI6_WPF_DSTM_ADDR_C0, mem.addr[1]);
91 vsp1_wpf_write(wpf, dl, VI6_WPF_DSTM_ADDR_C1, wpf->mem.addr[2]); 204 vsp1_wpf_write(wpf, dl, VI6_WPF_DSTM_ADDR_C1, mem.addr[2]);
92} 205}
93 206
94static void wpf_configure(struct vsp1_entity *entity, 207static void wpf_configure(struct vsp1_entity *entity,
95 struct vsp1_pipeline *pipe, 208 struct vsp1_pipeline *pipe,
96 struct vsp1_dl_list *dl) 209 struct vsp1_dl_list *dl, bool full)
97{ 210{
98 struct vsp1_rwpf *wpf = to_rwpf(&entity->subdev); 211 struct vsp1_rwpf *wpf = to_rwpf(&entity->subdev);
99 struct vsp1_device *vsp1 = wpf->entity.vsp1; 212 struct vsp1_device *vsp1 = wpf->entity.vsp1;
@@ -104,6 +217,26 @@ static void wpf_configure(struct vsp1_entity *entity,
104 u32 outfmt = 0; 217 u32 outfmt = 0;
105 u32 srcrpf = 0; 218 u32 srcrpf = 0;
106 219
220 if (!full) {
221 const unsigned int mask = BIT(WPF_CTRL_VFLIP)
222 | BIT(WPF_CTRL_HFLIP);
223
224 spin_lock(&wpf->flip.lock);
225 wpf->flip.active = (wpf->flip.active & ~mask)
226 | (wpf->flip.pending & mask);
227 spin_unlock(&wpf->flip.lock);
228
229 outfmt = (wpf->alpha << VI6_WPF_OUTFMT_PDV_SHIFT) | wpf->outfmt;
230
231 if (wpf->flip.active & BIT(WPF_CTRL_VFLIP))
232 outfmt |= VI6_WPF_OUTFMT_FLP;
233 if (wpf->flip.active & BIT(WPF_CTRL_HFLIP))
234 outfmt |= VI6_WPF_OUTFMT_HFLP;
235
236 vsp1_wpf_write(wpf, dl, VI6_WPF_OUTFMT, outfmt);
237 return;
238 }
239
107 /* Cropping */ 240 /* Cropping */
108 crop = vsp1_rwpf_get_crop(wpf, wpf->entity.config); 241 crop = vsp1_rwpf_get_crop(wpf, wpf->entity.config);
109 242
@@ -143,13 +276,18 @@ static void wpf_configure(struct vsp1_entity *entity,
143 format->plane_fmt[1].bytesperline); 276 format->plane_fmt[1].bytesperline);
144 277
145 vsp1_wpf_write(wpf, dl, VI6_WPF_DSWAP, fmtinfo->swap); 278 vsp1_wpf_write(wpf, dl, VI6_WPF_DSWAP, fmtinfo->swap);
279
280 if (vsp1->info->features & VSP1_HAS_WPF_HFLIP &&
281 wpf->entity.index == 0)
282 vsp1_wpf_write(wpf, dl, VI6_WPF_ROT_CTRL,
283 VI6_WPF_ROT_CTRL_LN16 |
284 (256 << VI6_WPF_ROT_CTRL_LMEM_WD_SHIFT));
146 } 285 }
147 286
148 if (sink_format->code != source_format->code) 287 if (sink_format->code != source_format->code)
149 outfmt |= VI6_WPF_OUTFMT_CSC; 288 outfmt |= VI6_WPF_OUTFMT_CSC;
150 289
151 outfmt |= wpf->alpha << VI6_WPF_OUTFMT_PDV_SHIFT; 290 wpf->outfmt = outfmt;
152 vsp1_wpf_write(wpf, dl, VI6_WPF_OUTFMT, outfmt);
153 291
154 vsp1_dl_list_write(dl, VI6_DPR_WPF_FPORCH(wpf->entity.index), 292 vsp1_dl_list_write(dl, VI6_DPR_WPF_FPORCH(wpf->entity.index),
155 VI6_DPR_WPF_FPORCH_FP_WPFN); 293 VI6_DPR_WPF_FPORCH_FP_WPFN);
@@ -216,7 +354,8 @@ struct vsp1_rwpf *vsp1_wpf_create(struct vsp1_device *vsp1, unsigned int index)
216 wpf->entity.index = index; 354 wpf->entity.index = index;
217 355
218 sprintf(name, "wpf.%u", index); 356 sprintf(name, "wpf.%u", index);
219 ret = vsp1_entity_init(vsp1, &wpf->entity, name, 2, &wpf_ops); 357 ret = vsp1_entity_init(vsp1, &wpf->entity, name, 2, &wpf_ops,
358 MEDIA_ENT_F_PROC_VIDEO_PIXEL_FORMATTER);
220 if (ret < 0) 359 if (ret < 0)
221 return ERR_PTR(ret); 360 return ERR_PTR(ret);
222 361
@@ -228,13 +367,15 @@ struct vsp1_rwpf *vsp1_wpf_create(struct vsp1_device *vsp1, unsigned int index)
228 } 367 }
229 368
230 /* Initialize the control handler. */ 369 /* Initialize the control handler. */
231 ret = vsp1_rwpf_init_ctrls(wpf); 370 ret = wpf_init_controls(wpf);
232 if (ret < 0) { 371 if (ret < 0) {
233 dev_err(vsp1->dev, "wpf%u: failed to initialize controls\n", 372 dev_err(vsp1->dev, "wpf%u: failed to initialize controls\n",
234 index); 373 index);
235 goto error; 374 goto error;
236 } 375 }
237 376
377 v4l2_ctrl_handler_setup(&wpf->ctrls);
378
238 return wpf; 379 return wpf;
239 380
240error: 381error:
diff --git a/drivers/media/platform/xilinx/xilinx-dma.c b/drivers/media/platform/xilinx/xilinx-dma.c
index 7f6898b13cac..7ae1a134b1ff 100644
--- a/drivers/media/platform/xilinx/xilinx-dma.c
+++ b/drivers/media/platform/xilinx/xilinx-dma.c
@@ -318,11 +318,10 @@ static void xvip_dma_complete(void *param)
318static int 318static int
319xvip_dma_queue_setup(struct vb2_queue *vq, 319xvip_dma_queue_setup(struct vb2_queue *vq,
320 unsigned int *nbuffers, unsigned int *nplanes, 320 unsigned int *nbuffers, unsigned int *nplanes,
321 unsigned int sizes[], void *alloc_ctxs[]) 321 unsigned int sizes[], struct device *alloc_devs[])
322{ 322{
323 struct xvip_dma *dma = vb2_get_drv_priv(vq); 323 struct xvip_dma *dma = vb2_get_drv_priv(vq);
324 324
325 alloc_ctxs[0] = dma->alloc_ctx;
326 /* Make sure the image size is large enough. */ 325 /* Make sure the image size is large enough. */
327 if (*nplanes) 326 if (*nplanes)
328 return sizes[0] < dma->format.sizeimage ? -EINVAL : 0; 327 return sizes[0] < dma->format.sizeimage ? -EINVAL : 0;
@@ -706,12 +705,6 @@ int xvip_dma_init(struct xvip_composite_device *xdev, struct xvip_dma *dma,
706 video_set_drvdata(&dma->video, dma); 705 video_set_drvdata(&dma->video, dma);
707 706
708 /* ... and the buffers queue... */ 707 /* ... and the buffers queue... */
709 dma->alloc_ctx = vb2_dma_contig_init_ctx(dma->xdev->dev);
710 if (IS_ERR(dma->alloc_ctx)) {
711 ret = PTR_ERR(dma->alloc_ctx);
712 goto error;
713 }
714
715 /* Don't enable VB2_READ and VB2_WRITE, as using the read() and write() 708 /* Don't enable VB2_READ and VB2_WRITE, as using the read() and write()
716 * V4L2 APIs would be inefficient. Testing on the command line with a 709 * V4L2 APIs would be inefficient. Testing on the command line with a
717 * 'cat /dev/video?' thus won't be possible, but given that the driver 710 * 'cat /dev/video?' thus won't be possible, but given that the driver
@@ -728,6 +721,7 @@ int xvip_dma_init(struct xvip_composite_device *xdev, struct xvip_dma *dma,
728 dma->queue.mem_ops = &vb2_dma_contig_memops; 721 dma->queue.mem_ops = &vb2_dma_contig_memops;
729 dma->queue.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC 722 dma->queue.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC
730 | V4L2_BUF_FLAG_TSTAMP_SRC_EOF; 723 | V4L2_BUF_FLAG_TSTAMP_SRC_EOF;
724 dma->queue.dev = dma->xdev->dev;
731 ret = vb2_queue_init(&dma->queue); 725 ret = vb2_queue_init(&dma->queue);
732 if (ret < 0) { 726 if (ret < 0) {
733 dev_err(dma->xdev->dev, "failed to initialize VB2 queue\n"); 727 dev_err(dma->xdev->dev, "failed to initialize VB2 queue\n");
@@ -766,9 +760,6 @@ void xvip_dma_cleanup(struct xvip_dma *dma)
766 if (dma->dma) 760 if (dma->dma)
767 dma_release_channel(dma->dma); 761 dma_release_channel(dma->dma);
768 762
769 if (!IS_ERR_OR_NULL(dma->alloc_ctx))
770 vb2_dma_contig_cleanup_ctx(dma->alloc_ctx);
771
772 media_entity_cleanup(&dma->video.entity); 763 media_entity_cleanup(&dma->video.entity);
773 764
774 mutex_destroy(&dma->lock); 765 mutex_destroy(&dma->lock);
diff --git a/drivers/media/platform/xilinx/xilinx-dma.h b/drivers/media/platform/xilinx/xilinx-dma.h
index 7a1621a2ef40..e95d136c153a 100644
--- a/drivers/media/platform/xilinx/xilinx-dma.h
+++ b/drivers/media/platform/xilinx/xilinx-dma.h
@@ -65,7 +65,6 @@ static inline struct xvip_pipeline *to_xvip_pipeline(struct media_entity *e)
65 * @format: active V4L2 pixel format 65 * @format: active V4L2 pixel format
66 * @fmtinfo: format information corresponding to the active @format 66 * @fmtinfo: format information corresponding to the active @format
67 * @queue: vb2 buffers queue 67 * @queue: vb2 buffers queue
68 * @alloc_ctx: allocation context for the vb2 @queue
69 * @sequence: V4L2 buffers sequence number 68 * @sequence: V4L2 buffers sequence number
70 * @queued_bufs: list of queued buffers 69 * @queued_bufs: list of queued buffers
71 * @queued_lock: protects the buf_queued list 70 * @queued_lock: protects the buf_queued list
@@ -88,7 +87,6 @@ struct xvip_dma {
88 const struct xvip_video_format *fmtinfo; 87 const struct xvip_video_format *fmtinfo;
89 88
90 struct vb2_queue queue; 89 struct vb2_queue queue;
91 void *alloc_ctx;
92 unsigned int sequence; 90 unsigned int sequence;
93 91
94 struct list_head queued_bufs; 92 struct list_head queued_bufs;
diff --git a/drivers/media/radio/radio-aztech.c b/drivers/media/radio/radio-aztech.c
index 705dd6f9162c..f445327f282d 100644
--- a/drivers/media/radio/radio-aztech.c
+++ b/drivers/media/radio/radio-aztech.c
@@ -43,7 +43,6 @@ MODULE_VERSION("1.0.0");
43static int io[AZTECH_MAX] = { [0] = CONFIG_RADIO_AZTECH_PORT, 43static int io[AZTECH_MAX] = { [0] = CONFIG_RADIO_AZTECH_PORT,
44 [1 ... (AZTECH_MAX - 1)] = -1 }; 44 [1 ... (AZTECH_MAX - 1)] = -1 };
45static int radio_nr[AZTECH_MAX] = { [0 ... (AZTECH_MAX - 1)] = -1 }; 45static int radio_nr[AZTECH_MAX] = { [0 ... (AZTECH_MAX - 1)] = -1 };
46static const int radio_wait_time = 1000;
47 46
48module_param_array(io, int, NULL, 0444); 47module_param_array(io, int, NULL, 0444);
49MODULE_PARM_DESC(io, "I/O addresses of the Aztech card (0x350 or 0x358)"); 48MODULE_PARM_DESC(io, "I/O addresses of the Aztech card (0x350 or 0x358)");
diff --git a/drivers/media/radio/radio-maxiradio.c b/drivers/media/radio/radio-maxiradio.c
index 70fd8e80198a..8253f79d5d75 100644
--- a/drivers/media/radio/radio-maxiradio.c
+++ b/drivers/media/radio/radio-maxiradio.c
@@ -183,6 +183,7 @@ static void maxiradio_remove(struct pci_dev *pdev)
183 outb(0, dev->io); 183 outb(0, dev->io);
184 v4l2_device_unregister(v4l2_dev); 184 v4l2_device_unregister(v4l2_dev);
185 release_region(pci_resource_start(pdev, 0), pci_resource_len(pdev, 0)); 185 release_region(pci_resource_start(pdev, 0), pci_resource_len(pdev, 0));
186 kfree(dev);
186} 187}
187 188
188static struct pci_device_id maxiradio_pci_tbl[] = { 189static struct pci_device_id maxiradio_pci_tbl[] = {
diff --git a/drivers/media/rc/ene_ir.c b/drivers/media/rc/ene_ir.c
index 8d77e1c4a141..d1c61cd035f6 100644
--- a/drivers/media/rc/ene_ir.c
+++ b/drivers/media/rc/ene_ir.c
@@ -904,7 +904,7 @@ static int ene_set_tx_carrier(struct rc_dev *rdev, u32 carrier)
904 904
905 dbg("TX: out of range %d-%d kHz carrier", 905 dbg("TX: out of range %d-%d kHz carrier",
906 2000 / ENE_CIRMOD_PRD_MIN, 2000 / ENE_CIRMOD_PRD_MAX); 906 2000 / ENE_CIRMOD_PRD_MIN, 2000 / ENE_CIRMOD_PRD_MAX);
907 return -1; 907 return -EINVAL;
908 } 908 }
909 909
910 dev->tx_period = period; 910 dev->tx_period = period;
diff --git a/drivers/media/rc/iguanair.c b/drivers/media/rc/iguanair.c
index ee60e17fba05..5f634545ddd8 100644
--- a/drivers/media/rc/iguanair.c
+++ b/drivers/media/rc/iguanair.c
@@ -330,7 +330,7 @@ static int iguanair_set_tx_carrier(struct rc_dev *dev, uint32_t carrier)
330 330
331 mutex_unlock(&ir->lock); 331 mutex_unlock(&ir->lock);
332 332
333 return carrier; 333 return 0;
334} 334}
335 335
336static int iguanair_set_tx_mask(struct rc_dev *dev, uint32_t mask) 336static int iguanair_set_tx_mask(struct rc_dev *dev, uint32_t mask)
diff --git a/drivers/media/rc/ir-lirc-codec.c b/drivers/media/rc/ir-lirc-codec.c
index 5effc65d2947..c3277308a70b 100644
--- a/drivers/media/rc/ir-lirc-codec.c
+++ b/drivers/media/rc/ir-lirc-codec.c
@@ -292,7 +292,10 @@ static long ir_lirc_ioctl(struct file *filep, unsigned int cmd,
292 tmp > dev->max_timeout) 292 tmp > dev->max_timeout)
293 return -EINVAL; 293 return -EINVAL;
294 294
295 dev->timeout = tmp; 295 if (dev->s_timeout)
296 ret = dev->s_timeout(dev, tmp);
297 if (!ret)
298 dev->timeout = tmp;
296 break; 299 break;
297 300
298 case LIRC_SET_REC_TIMEOUT_REPORTS: 301 case LIRC_SET_REC_TIMEOUT_REPORTS:
diff --git a/drivers/media/rc/ir-rc5-decoder.c b/drivers/media/rc/ir-rc5-decoder.c
index 6ffe776abf6b..a0fd4e6b2155 100644
--- a/drivers/media/rc/ir-rc5-decoder.c
+++ b/drivers/media/rc/ir-rc5-decoder.c
@@ -29,7 +29,7 @@
29#define RC5_BIT_START (1 * RC5_UNIT) 29#define RC5_BIT_START (1 * RC5_UNIT)
30#define RC5_BIT_END (1 * RC5_UNIT) 30#define RC5_BIT_END (1 * RC5_UNIT)
31#define RC5X_SPACE (4 * RC5_UNIT) 31#define RC5X_SPACE (4 * RC5_UNIT)
32#define RC5_TRAILER (10 * RC5_UNIT) /* In reality, approx 100 */ 32#define RC5_TRAILER (6 * RC5_UNIT) /* In reality, approx 100 */
33 33
34enum rc5_state { 34enum rc5_state {
35 STATE_INACTIVE, 35 STATE_INACTIVE,
diff --git a/drivers/media/rc/keymaps/Makefile b/drivers/media/rc/keymaps/Makefile
index fbbd3bbcd252..d7b13fae1267 100644
--- a/drivers/media/rc/keymaps/Makefile
+++ b/drivers/media/rc/keymaps/Makefile
@@ -18,6 +18,7 @@ obj-$(CONFIG_RC_MAP) += rc-adstech-dvb-t-pci.o \
18 rc-behold.o \ 18 rc-behold.o \
19 rc-behold-columbus.o \ 19 rc-behold-columbus.o \
20 rc-budget-ci-old.o \ 20 rc-budget-ci-old.o \
21 rc-cec.o \
21 rc-cinergy-1400.o \ 22 rc-cinergy-1400.o \
22 rc-cinergy.o \ 23 rc-cinergy.o \
23 rc-delock-61959.o \ 24 rc-delock-61959.o \
@@ -28,6 +29,7 @@ obj-$(CONFIG_RC_MAP) += rc-adstech-dvb-t-pci.o \
28 rc-dm1105-nec.o \ 29 rc-dm1105-nec.o \
29 rc-dntv-live-dvb-t.o \ 30 rc-dntv-live-dvb-t.o \
30 rc-dntv-live-dvbt-pro.o \ 31 rc-dntv-live-dvbt-pro.o \
32 rc-dtt200u.o \
31 rc-dvbsky.o \ 33 rc-dvbsky.o \
32 rc-em-terratec.o \ 34 rc-em-terratec.o \
33 rc-encore-enltv2.o \ 35 rc-encore-enltv2.o \
diff --git a/drivers/media/rc/keymaps/rc-cec.c b/drivers/media/rc/keymaps/rc-cec.c
new file mode 100644
index 000000000000..354c8e724b8e
--- /dev/null
+++ b/drivers/media/rc/keymaps/rc-cec.c
@@ -0,0 +1,182 @@
1/* Keytable for the CEC remote control
2 *
3 * Copyright (c) 2015 by Kamil Debski
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 */
10
11#include <media/rc-map.h>
12#include <linux/module.h>
13
14/*
15 * CEC Spec "High-Definition Multimedia Interface Specification" can be obtained
16 * here: http://xtreamerdev.googlecode.com/files/CEC_Specs.pdf
17 * The list of control codes is listed in Table 27: User Control Codes p. 95
18 */
19
20static struct rc_map_table cec[] = {
21 { 0x00, KEY_OK },
22 { 0x01, KEY_UP },
23 { 0x02, KEY_DOWN },
24 { 0x03, KEY_LEFT },
25 { 0x04, KEY_RIGHT },
26 { 0x05, KEY_RIGHT_UP },
27 { 0x06, KEY_RIGHT_DOWN },
28 { 0x07, KEY_LEFT_UP },
29 { 0x08, KEY_LEFT_DOWN },
30 { 0x09, KEY_ROOT_MENU }, /* CEC Spec: Device Root Menu - see Note 2 */
31 /*
32 * Note 2: This is the initial display that a device shows. It is
33 * device-dependent and can be, for example, a contents menu, setup
34 * menu, favorite menu or other menu. The actual menu displayed
35 * may also depend on the device's current state.
36 */
37 { 0x0a, KEY_SETUP },
38 { 0x0b, KEY_MENU }, /* CEC Spec: Contents Menu */
39 { 0x0c, KEY_FAVORITES }, /* CEC Spec: Favorite Menu */
40 { 0x0d, KEY_EXIT },
41 /* 0x0e-0x0f: Reserved */
42 { 0x10, KEY_MEDIA_TOP_MENU },
43 { 0x11, KEY_CONTEXT_MENU },
44 /* 0x12-0x1c: Reserved */
45 { 0x1d, KEY_DIGITS }, /* CEC Spec: select/toggle a Number Entry Mode */
46 { 0x1e, KEY_NUMERIC_11 },
47 { 0x1f, KEY_NUMERIC_12 },
48 /* 0x20-0x29: Keys 0 to 9 */
49 { 0x20, KEY_NUMERIC_0 },
50 { 0x21, KEY_NUMERIC_1 },
51 { 0x22, KEY_NUMERIC_2 },
52 { 0x23, KEY_NUMERIC_3 },
53 { 0x24, KEY_NUMERIC_4 },
54 { 0x25, KEY_NUMERIC_5 },
55 { 0x26, KEY_NUMERIC_6 },
56 { 0x27, KEY_NUMERIC_7 },
57 { 0x28, KEY_NUMERIC_8 },
58 { 0x29, KEY_NUMERIC_9 },
59 { 0x2a, KEY_DOT },
60 { 0x2b, KEY_ENTER },
61 { 0x2c, KEY_CLEAR },
62 /* 0x2d-0x2e: Reserved */
63 { 0x2f, KEY_NEXT_FAVORITE }, /* CEC Spec: Next Favorite */
64 { 0x30, KEY_CHANNELUP },
65 { 0x31, KEY_CHANNELDOWN },
66 { 0x32, KEY_PREVIOUS }, /* CEC Spec: Previous Channel */
67 { 0x33, KEY_SOUND }, /* CEC Spec: Sound Select */
68 { 0x34, KEY_VIDEO }, /* 0x34: CEC Spec: Input Select */
69 { 0x35, KEY_INFO }, /* CEC Spec: Display Information */
70 { 0x36, KEY_HELP },
71 { 0x37, KEY_PAGEUP },
72 { 0x38, KEY_PAGEDOWN },
73 /* 0x39-0x3f: Reserved */
74 { 0x40, KEY_POWER },
75 { 0x41, KEY_VOLUMEUP },
76 { 0x42, KEY_VOLUMEDOWN },
77 { 0x43, KEY_MUTE },
78 { 0x44, KEY_PLAYCD },
79 { 0x45, KEY_STOPCD },
80 { 0x46, KEY_PAUSECD },
81 { 0x47, KEY_RECORD },
82 { 0x48, KEY_REWIND },
83 { 0x49, KEY_FASTFORWARD },
84 { 0x4a, KEY_EJECTCD }, /* CEC Spec: Eject */
85 { 0x4b, KEY_FORWARD },
86 { 0x4c, KEY_BACK },
87 { 0x4d, KEY_STOP_RECORD }, /* CEC Spec: Stop-Record */
88 { 0x4e, KEY_PAUSE_RECORD }, /* CEC Spec: Pause-Record */
89 /* 0x4f: Reserved */
90 { 0x50, KEY_ANGLE },
91 { 0x51, KEY_TV2 },
92 { 0x52, KEY_VOD }, /* CEC Spec: Video on Demand */
93 { 0x53, KEY_EPG },
94 { 0x54, KEY_TIME }, /* CEC Spec: Timer */
95 { 0x55, KEY_CONFIG },
96 /*
97 * The following codes are hard to implement at this moment, as they
98 * carry an additional additional argument. Most likely changes to RC
99 * framework are necessary.
100 * For now they are interpreted by the CEC framework as non keycodes
101 * and are passed as messages enabling user application to parse them.
102 */
103 /* 0x56: CEC Spec: Select Broadcast Type */
104 /* 0x57: CEC Spec: Select Sound presentation */
105 { 0x58, KEY_AUDIO_DESC }, /* CEC 2.0 and up */
106 { 0x59, KEY_WWW }, /* CEC 2.0 and up */
107 { 0x5a, KEY_3D_MODE }, /* CEC 2.0 and up */
108 /* 0x5b-0x5f: Reserved */
109 { 0x60, KEY_PLAYCD }, /* CEC Spec: Play Function */
110 { 0x6005, KEY_FASTFORWARD },
111 { 0x6006, KEY_FASTFORWARD },
112 { 0x6007, KEY_FASTFORWARD },
113 { 0x6015, KEY_SLOW },
114 { 0x6016, KEY_SLOW },
115 { 0x6017, KEY_SLOW },
116 { 0x6009, KEY_FASTREVERSE },
117 { 0x600a, KEY_FASTREVERSE },
118 { 0x600b, KEY_FASTREVERSE },
119 { 0x6019, KEY_SLOWREVERSE },
120 { 0x601a, KEY_SLOWREVERSE },
121 { 0x601b, KEY_SLOWREVERSE },
122 { 0x6020, KEY_REWIND },
123 { 0x6024, KEY_PLAYCD },
124 { 0x6025, KEY_PAUSECD },
125 { 0x61, KEY_PLAYPAUSE }, /* CEC Spec: Pause-Play Function */
126 { 0x62, KEY_RECORD }, /* Spec: Record Function */
127 { 0x63, KEY_PAUSE_RECORD }, /* CEC Spec: Pause-Record Function */
128 { 0x64, KEY_STOPCD }, /* CEC Spec: Stop Function */
129 { 0x65, KEY_MUTE }, /* CEC Spec: Mute Function */
130 { 0x66, KEY_UNMUTE }, /* CEC Spec: Restore the volume */
131 /*
132 * The following codes are hard to implement at this moment, as they
133 * carry an additional additional argument. Most likely changes to RC
134 * framework are necessary.
135 * For now they are interpreted by the CEC framework as non keycodes
136 * and are passed as messages enabling user application to parse them.
137 */
138 /* 0x67: CEC Spec: Tune Function */
139 /* 0x68: CEC Spec: Seleect Media Function */
140 /* 0x69: CEC Spec: Select A/V Input Function */
141 /* 0x6a: CEC Spec: Select Audio Input Function */
142 { 0x6b, KEY_POWER }, /* CEC Spec: Power Toggle Function */
143 { 0x6c, KEY_SLEEP }, /* CEC Spec: Power Off Function */
144 { 0x6d, KEY_WAKEUP }, /* CEC Spec: Power On Function */
145 /* 0x6e-0x70: Reserved */
146 { 0x71, KEY_BLUE }, /* CEC Spec: F1 (Blue) */
147 { 0x72, KEY_RED }, /* CEC Spec: F2 (Red) */
148 { 0x73, KEY_GREEN }, /* CEC Spec: F3 (Green) */
149 { 0x74, KEY_YELLOW }, /* CEC Spec: F4 (Yellow) */
150 { 0x75, KEY_F5 },
151 { 0x76, KEY_DATA }, /* CEC Spec: Data - see Note 3 */
152 /*
153 * Note 3: This is used, for example, to enter or leave a digital TV
154 * data broadcast application.
155 */
156 /* 0x77-0xff: Reserved */
157};
158
159static struct rc_map_list cec_map = {
160 .map = {
161 .scan = cec,
162 .size = ARRAY_SIZE(cec),
163 .rc_type = RC_TYPE_CEC,
164 .name = RC_MAP_CEC,
165 }
166};
167
168static int __init init_rc_map_cec(void)
169{
170 return rc_map_register(&cec_map);
171}
172
173static void __exit exit_rc_map_cec(void)
174{
175 rc_map_unregister(&cec_map);
176}
177
178module_init(init_rc_map_cec);
179module_exit(exit_rc_map_cec);
180
181MODULE_LICENSE("GPL");
182MODULE_AUTHOR("Kamil Debski");
diff --git a/drivers/media/rc/keymaps/rc-dtt200u.c b/drivers/media/rc/keymaps/rc-dtt200u.c
new file mode 100644
index 000000000000..25650e9e4664
--- /dev/null
+++ b/drivers/media/rc/keymaps/rc-dtt200u.c
@@ -0,0 +1,59 @@
1/* Keytable for Wideview WT-220U.
2 *
3 * Copyright (c) 2016 Jonathan McDowell <noodles@earth.li>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 */
10
11#include <media/rc-map.h>
12#include <linux/module.h>
13
14/* key list for the tiny remote control (Yakumo, don't know about the others) */
15static struct rc_map_table dtt200u_table[] = {
16 { 0x8001, KEY_MUTE },
17 { 0x8002, KEY_CHANNELDOWN },
18 { 0x8003, KEY_VOLUMEDOWN },
19 { 0x8004, KEY_1 },
20 { 0x8005, KEY_2 },
21 { 0x8006, KEY_3 },
22 { 0x8007, KEY_4 },
23 { 0x8008, KEY_5 },
24 { 0x8009, KEY_6 },
25 { 0x800a, KEY_7 },
26 { 0x800c, KEY_ZOOM },
27 { 0x800d, KEY_0 },
28 { 0x800e, KEY_SELECT },
29 { 0x8012, KEY_POWER },
30 { 0x801a, KEY_CHANNELUP },
31 { 0x801b, KEY_8 },
32 { 0x801e, KEY_VOLUMEUP },
33 { 0x801f, KEY_9 },
34};
35
36static struct rc_map_list dtt200u_map = {
37 .map = {
38 .scan = dtt200u_table,
39 .size = ARRAY_SIZE(dtt200u_table),
40 .rc_type = RC_TYPE_NEC,
41 .name = RC_MAP_DTT200U,
42 }
43};
44
45static int __init init_rc_map_dtt200u(void)
46{
47 return rc_map_register(&dtt200u_map);
48}
49
50static void __exit exit_rc_map_dtt200u(void)
51{
52 rc_map_unregister(&dtt200u_map);
53}
54
55module_init(init_rc_map_dtt200u)
56module_exit(exit_rc_map_dtt200u)
57
58MODULE_LICENSE("GPL");
59MODULE_AUTHOR("Jonathan McDowell <noodles@earth.li>");
diff --git a/drivers/media/rc/lirc_dev.c b/drivers/media/rc/lirc_dev.c
index 92ae1903c010..91f9bb87ce68 100644
--- a/drivers/media/rc/lirc_dev.c
+++ b/drivers/media/rc/lirc_dev.c
@@ -19,6 +19,8 @@
19 * 19 *
20 */ 20 */
21 21
22#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
23
22#include <linux/module.h> 24#include <linux/module.h>
23#include <linux/kernel.h> 25#include <linux/kernel.h>
24#include <linux/sched.h> 26#include <linux/sched.h>
@@ -80,8 +82,6 @@ static void lirc_irctl_init(struct irctl *ir)
80 82
81static void lirc_irctl_cleanup(struct irctl *ir) 83static void lirc_irctl_cleanup(struct irctl *ir)
82{ 84{
83 dev_dbg(ir->d.dev, LOGHEAD "cleaning up\n", ir->d.name, ir->d.minor);
84
85 device_destroy(lirc_class, MKDEV(MAJOR(lirc_base_dev), ir->d.minor)); 85 device_destroy(lirc_class, MKDEV(MAJOR(lirc_base_dev), ir->d.minor));
86 86
87 if (ir->buf != ir->d.rbuf) { 87 if (ir->buf != ir->d.rbuf) {
@@ -97,28 +97,25 @@ static void lirc_irctl_cleanup(struct irctl *ir)
97 */ 97 */
98static int lirc_add_to_buf(struct irctl *ir) 98static int lirc_add_to_buf(struct irctl *ir)
99{ 99{
100 if (ir->d.add_to_buf) { 100 int res;
101 int res = -ENODATA; 101 int got_data = -1;
102 int got_data = 0;
103 102
104 /* 103 if (!ir->d.add_to_buf)
105 * service the device as long as it is returning 104 return 0;
106 * data and we have space
107 */
108get_data:
109 res = ir->d.add_to_buf(ir->d.data, ir->buf);
110 if (res == 0) {
111 got_data++;
112 goto get_data;
113 }
114 105
115 if (res == -ENODEV) 106 /*
116 kthread_stop(ir->task); 107 * service the device as long as it is returning
108 * data and we have space
109 */
110 do {
111 got_data++;
112 res = ir->d.add_to_buf(ir->d.data, ir->buf);
113 } while (!res);
117 114
118 return got_data ? 0 : res; 115 if (res == -ENODEV)
119 } 116 kthread_stop(ir->task);
120 117
121 return 0; 118 return got_data ? 0 : res;
122} 119}
123 120
124/* main function of the polling thread 121/* main function of the polling thread
@@ -127,9 +124,6 @@ static int lirc_thread(void *irctl)
127{ 124{
128 struct irctl *ir = irctl; 125 struct irctl *ir = irctl;
129 126
130 dev_dbg(ir->d.dev, LOGHEAD "poll thread started\n",
131 ir->d.name, ir->d.minor);
132
133 do { 127 do {
134 if (ir->open) { 128 if (ir->open) {
135 if (ir->jiffies_to_wait) { 129 if (ir->jiffies_to_wait) {
@@ -146,9 +140,6 @@ static int lirc_thread(void *irctl)
146 } 140 }
147 } while (!kthread_should_stop()); 141 } while (!kthread_should_stop());
148 142
149 dev_dbg(ir->d.dev, LOGHEAD "poll thread ended\n",
150 ir->d.name, ir->d.minor);
151
152 return 0; 143 return 0;
153} 144}
154 145
@@ -203,74 +194,86 @@ err_out:
203 return retval; 194 return retval;
204} 195}
205 196
206int lirc_register_driver(struct lirc_driver *d) 197static int lirc_allocate_buffer(struct irctl *ir)
207{ 198{
208 struct irctl *ir; 199 int err = 0;
209 int minor;
210 int bytes_in_key; 200 int bytes_in_key;
211 unsigned int chunk_size; 201 unsigned int chunk_size;
212 unsigned int buffer_size; 202 unsigned int buffer_size;
203 struct lirc_driver *d = &ir->d;
204
205 mutex_lock(&lirc_dev_lock);
206
207 bytes_in_key = BITS_TO_LONGS(d->code_length) +
208 (d->code_length % 8 ? 1 : 0);
209 buffer_size = d->buffer_size ? d->buffer_size : BUFLEN / bytes_in_key;
210 chunk_size = d->chunk_size ? d->chunk_size : bytes_in_key;
211
212 if (d->rbuf) {
213 ir->buf = d->rbuf;
214 } else {
215 ir->buf = kmalloc(sizeof(struct lirc_buffer), GFP_KERNEL);
216 if (!ir->buf) {
217 err = -ENOMEM;
218 goto out;
219 }
220
221 err = lirc_buffer_init(ir->buf, chunk_size, buffer_size);
222 if (err) {
223 kfree(ir->buf);
224 goto out;
225 }
226 }
227 ir->chunk_size = ir->buf->chunk_size;
228
229out:
230 mutex_unlock(&lirc_dev_lock);
231
232 return err;
233}
234
235static int lirc_allocate_driver(struct lirc_driver *d)
236{
237 struct irctl *ir;
238 int minor;
213 int err; 239 int err;
214 240
215 if (!d) { 241 if (!d) {
216 printk(KERN_ERR "lirc_dev: lirc_register_driver: " 242 pr_err("driver pointer must be not NULL!\n");
217 "driver pointer must be not NULL!\n"); 243 return -EBADRQC;
218 err = -EBADRQC;
219 goto out;
220 } 244 }
221 245
222 if (!d->dev) { 246 if (!d->dev) {
223 printk(KERN_ERR "%s: dev pointer not filled in!\n", __func__); 247 pr_err("dev pointer not filled in!\n");
224 err = -EINVAL; 248 return -EINVAL;
225 goto out;
226 } 249 }
227 250
228 if (MAX_IRCTL_DEVICES <= d->minor) { 251 if (d->minor >= MAX_IRCTL_DEVICES) {
229 dev_err(d->dev, "lirc_dev: lirc_register_driver: " 252 dev_err(d->dev, "minor must be between 0 and %d!\n",
230 "\"minor\" must be between 0 and %d (%d)!\n", 253 MAX_IRCTL_DEVICES - 1);
231 MAX_IRCTL_DEVICES - 1, d->minor); 254 return -EBADRQC;
232 err = -EBADRQC;
233 goto out;
234 } 255 }
235 256
236 if (1 > d->code_length || (BUFLEN * 8) < d->code_length) { 257 if (d->code_length < 1 || d->code_length > (BUFLEN * 8)) {
237 dev_err(d->dev, "lirc_dev: lirc_register_driver: " 258 dev_err(d->dev, "code length must be less than %d bits\n",
238 "code length in bits for minor (%d) " 259 BUFLEN * 8);
239 "must be less than %d!\n", 260 return -EBADRQC;
240 d->minor, BUFLEN * 8);
241 err = -EBADRQC;
242 goto out;
243 } 261 }
244 262
245 dev_dbg(d->dev, "lirc_dev: lirc_register_driver: sample_rate: %d\n",
246 d->sample_rate);
247 if (d->sample_rate) { 263 if (d->sample_rate) {
248 if (2 > d->sample_rate || HZ < d->sample_rate) { 264 if (2 > d->sample_rate || HZ < d->sample_rate) {
249 dev_err(d->dev, "lirc_dev: lirc_register_driver: " 265 dev_err(d->dev, "invalid %d sample rate\n",
250 "sample_rate must be between 2 and %d!\n", HZ); 266 d->sample_rate);
251 err = -EBADRQC; 267 return -EBADRQC;
252 goto out;
253 } 268 }
254 if (!d->add_to_buf) { 269 if (!d->add_to_buf) {
255 dev_err(d->dev, "lirc_dev: lirc_register_driver: " 270 dev_err(d->dev, "add_to_buf not set\n");
256 "add_to_buf cannot be NULL when " 271 return -EBADRQC;
257 "sample_rate is set\n");
258 err = -EBADRQC;
259 goto out;
260 }
261 } else if (!(d->fops && d->fops->read) && !d->rbuf) {
262 dev_err(d->dev, "lirc_dev: lirc_register_driver: "
263 "fops->read and rbuf cannot all be NULL!\n");
264 err = -EBADRQC;
265 goto out;
266 } else if (!d->rbuf) {
267 if (!(d->fops && d->fops->read && d->fops->poll &&
268 d->fops->unlocked_ioctl)) {
269 dev_err(d->dev, "lirc_dev: lirc_register_driver: "
270 "neither read, poll nor unlocked_ioctl can be NULL!\n");
271 err = -EBADRQC;
272 goto out;
273 } 272 }
273 } else if (!d->rbuf && !(d->fops && d->fops->read &&
274 d->fops->poll && d->fops->unlocked_ioctl)) {
275 dev_err(d->dev, "undefined read, poll, ioctl\n");
276 return -EBADRQC;
274 } 277 }
275 278
276 mutex_lock(&lirc_dev_lock); 279 mutex_lock(&lirc_dev_lock);
@@ -282,15 +285,13 @@ int lirc_register_driver(struct lirc_driver *d)
282 for (minor = 0; minor < MAX_IRCTL_DEVICES; minor++) 285 for (minor = 0; minor < MAX_IRCTL_DEVICES; minor++)
283 if (!irctls[minor]) 286 if (!irctls[minor])
284 break; 287 break;
285 if (MAX_IRCTL_DEVICES == minor) { 288 if (minor == MAX_IRCTL_DEVICES) {
286 dev_err(d->dev, "lirc_dev: lirc_register_driver: " 289 dev_err(d->dev, "no free slots for drivers!\n");
287 "no free slots for drivers!\n");
288 err = -ENOMEM; 290 err = -ENOMEM;
289 goto out_lock; 291 goto out_lock;
290 } 292 }
291 } else if (irctls[minor]) { 293 } else if (irctls[minor]) {
292 dev_err(d->dev, "lirc_dev: lirc_register_driver: " 294 dev_err(d->dev, "minor (%d) just registered!\n", minor);
293 "minor (%d) just registered!\n", minor);
294 err = -EBUSY; 295 err = -EBUSY;
295 goto out_lock; 296 goto out_lock;
296 } 297 }
@@ -304,37 +305,9 @@ int lirc_register_driver(struct lirc_driver *d)
304 irctls[minor] = ir; 305 irctls[minor] = ir;
305 d->minor = minor; 306 d->minor = minor;
306 307
307 if (d->sample_rate) {
308 ir->jiffies_to_wait = HZ / d->sample_rate;
309 } else {
310 /* it means - wait for external event in task queue */
311 ir->jiffies_to_wait = 0;
312 }
313
314 /* some safety check 8-) */ 308 /* some safety check 8-) */
315 d->name[sizeof(d->name)-1] = '\0'; 309 d->name[sizeof(d->name)-1] = '\0';
316 310
317 bytes_in_key = BITS_TO_LONGS(d->code_length) +
318 (d->code_length % 8 ? 1 : 0);
319 buffer_size = d->buffer_size ? d->buffer_size : BUFLEN / bytes_in_key;
320 chunk_size = d->chunk_size ? d->chunk_size : bytes_in_key;
321
322 if (d->rbuf) {
323 ir->buf = d->rbuf;
324 } else {
325 ir->buf = kmalloc(sizeof(struct lirc_buffer), GFP_KERNEL);
326 if (!ir->buf) {
327 err = -ENOMEM;
328 goto out_lock;
329 }
330 err = lirc_buffer_init(ir->buf, chunk_size, buffer_size);
331 if (err) {
332 kfree(ir->buf);
333 goto out_lock;
334 }
335 }
336 ir->chunk_size = ir->buf->chunk_size;
337
338 if (d->features == 0) 311 if (d->features == 0)
339 d->features = LIRC_CAN_REC_LIRCCODE; 312 d->features = LIRC_CAN_REC_LIRCCODE;
340 313
@@ -345,15 +318,19 @@ int lirc_register_driver(struct lirc_driver *d)
345 "lirc%u", ir->d.minor); 318 "lirc%u", ir->d.minor);
346 319
347 if (d->sample_rate) { 320 if (d->sample_rate) {
321 ir->jiffies_to_wait = HZ / d->sample_rate;
322
348 /* try to fire up polling thread */ 323 /* try to fire up polling thread */
349 ir->task = kthread_run(lirc_thread, (void *)ir, "lirc_dev"); 324 ir->task = kthread_run(lirc_thread, (void *)ir, "lirc_dev");
350 if (IS_ERR(ir->task)) { 325 if (IS_ERR(ir->task)) {
351 dev_err(d->dev, "lirc_dev: lirc_register_driver: " 326 dev_err(d->dev, "cannot run thread for minor = %d\n",
352 "cannot run poll thread for minor = %d\n", 327 d->minor);
353 d->minor);
354 err = -ECHILD; 328 err = -ECHILD;
355 goto out_sysfs; 329 goto out_sysfs;
356 } 330 }
331 } else {
332 /* it means - wait for external event in task queue */
333 ir->jiffies_to_wait = 0;
357 } 334 }
358 335
359 err = lirc_cdev_add(ir); 336 err = lirc_cdev_add(ir);
@@ -371,9 +348,26 @@ out_sysfs:
371 device_destroy(lirc_class, MKDEV(MAJOR(lirc_base_dev), ir->d.minor)); 348 device_destroy(lirc_class, MKDEV(MAJOR(lirc_base_dev), ir->d.minor));
372out_lock: 349out_lock:
373 mutex_unlock(&lirc_dev_lock); 350 mutex_unlock(&lirc_dev_lock);
374out: 351
375 return err; 352 return err;
376} 353}
354
355int lirc_register_driver(struct lirc_driver *d)
356{
357 int minor, err = 0;
358
359 minor = lirc_allocate_driver(d);
360 if (minor < 0)
361 return minor;
362
363 if (LIRC_CAN_REC(d->features)) {
364 err = lirc_allocate_buffer(irctls[minor]);
365 if (err)
366 lirc_unregister_driver(minor);
367 }
368
369 return err ? err : minor;
370}
377EXPORT_SYMBOL(lirc_register_driver); 371EXPORT_SYMBOL(lirc_register_driver);
378 372
379int lirc_unregister_driver(int minor) 373int lirc_unregister_driver(int minor)
@@ -382,15 +376,14 @@ int lirc_unregister_driver(int minor)
382 struct cdev *cdev; 376 struct cdev *cdev;
383 377
384 if (minor < 0 || minor >= MAX_IRCTL_DEVICES) { 378 if (minor < 0 || minor >= MAX_IRCTL_DEVICES) {
385 printk(KERN_ERR "lirc_dev: %s: minor (%d) must be between " 379 pr_err("minor (%d) must be between 0 and %d!\n",
386 "0 and %d!\n", __func__, minor, MAX_IRCTL_DEVICES - 1); 380 minor, MAX_IRCTL_DEVICES - 1);
387 return -EBADRQC; 381 return -EBADRQC;
388 } 382 }
389 383
390 ir = irctls[minor]; 384 ir = irctls[minor];
391 if (!ir) { 385 if (!ir) {
392 printk(KERN_ERR "lirc_dev: %s: failed to get irctl struct " 386 pr_err("failed to get irctl\n");
393 "for minor %d!\n", __func__, minor);
394 return -ENOENT; 387 return -ENOENT;
395 } 388 }
396 389
@@ -399,8 +392,8 @@ int lirc_unregister_driver(int minor)
399 mutex_lock(&lirc_dev_lock); 392 mutex_lock(&lirc_dev_lock);
400 393
401 if (ir->d.minor != minor) { 394 if (ir->d.minor != minor) {
402 printk(KERN_ERR "lirc_dev: %s: minor (%d) device not " 395 dev_err(ir->d.dev, "lirc_dev: minor %d device not registered\n",
403 "registered!\n", __func__, minor); 396 minor);
404 mutex_unlock(&lirc_dev_lock); 397 mutex_unlock(&lirc_dev_lock);
405 return -ENOENT; 398 return -ENOENT;
406 } 399 }
@@ -418,7 +411,10 @@ int lirc_unregister_driver(int minor)
418 ir->d.name, ir->d.minor); 411 ir->d.name, ir->d.minor);
419 wake_up_interruptible(&ir->buf->wait_poll); 412 wake_up_interruptible(&ir->buf->wait_poll);
420 mutex_lock(&ir->irctl_lock); 413 mutex_lock(&ir->irctl_lock);
421 ir->d.set_use_dec(ir->d.data); 414
415 if (ir->d.set_use_dec)
416 ir->d.set_use_dec(ir->d.data);
417
422 module_put(cdev->owner); 418 module_put(cdev->owner);
423 mutex_unlock(&ir->irctl_lock); 419 mutex_unlock(&ir->irctl_lock);
424 } else { 420 } else {
@@ -442,8 +438,7 @@ int lirc_dev_fop_open(struct inode *inode, struct file *file)
442 int retval = 0; 438 int retval = 0;
443 439
444 if (iminor(inode) >= MAX_IRCTL_DEVICES) { 440 if (iminor(inode) >= MAX_IRCTL_DEVICES) {
445 printk(KERN_WARNING "lirc_dev [%d]: open result = -ENODEV\n", 441 pr_err("open result for %d is -ENODEV\n", iminor(inode));
446 iminor(inode));
447 return -ENODEV; 442 return -ENODEV;
448 } 443 }
449 444
@@ -477,7 +472,8 @@ int lirc_dev_fop_open(struct inode *inode, struct file *file)
477 cdev = ir->cdev; 472 cdev = ir->cdev;
478 if (try_module_get(cdev->owner)) { 473 if (try_module_get(cdev->owner)) {
479 ir->open++; 474 ir->open++;
480 retval = ir->d.set_use_inc(ir->d.data); 475 if (ir->d.set_use_inc)
476 retval = ir->d.set_use_inc(ir->d.data);
481 477
482 if (retval) { 478 if (retval) {
483 module_put(cdev->owner); 479 module_put(cdev->owner);
@@ -490,10 +486,6 @@ int lirc_dev_fop_open(struct inode *inode, struct file *file)
490 } 486 }
491 487
492error: 488error:
493 if (ir)
494 dev_dbg(ir->d.dev, LOGHEAD "open result = %d\n",
495 ir->d.name, ir->d.minor, retval);
496
497 mutex_unlock(&lirc_dev_lock); 489 mutex_unlock(&lirc_dev_lock);
498 490
499 nonseekable_open(inode, file); 491 nonseekable_open(inode, file);
@@ -509,14 +501,12 @@ int lirc_dev_fop_close(struct inode *inode, struct file *file)
509 int ret; 501 int ret;
510 502
511 if (!ir) { 503 if (!ir) {
512 printk(KERN_ERR "%s: called with invalid irctl\n", __func__); 504 pr_err("called with invalid irctl\n");
513 return -EINVAL; 505 return -EINVAL;
514 } 506 }
515 507
516 cdev = ir->cdev; 508 cdev = ir->cdev;
517 509
518 dev_dbg(ir->d.dev, LOGHEAD "close called\n", ir->d.name, ir->d.minor);
519
520 ret = mutex_lock_killable(&lirc_dev_lock); 510 ret = mutex_lock_killable(&lirc_dev_lock);
521 WARN_ON(ret); 511 WARN_ON(ret);
522 512
@@ -524,7 +514,8 @@ int lirc_dev_fop_close(struct inode *inode, struct file *file)
524 514
525 ir->open--; 515 ir->open--;
526 if (ir->attached) { 516 if (ir->attached) {
527 ir->d.set_use_dec(ir->d.data); 517 if (ir->d.set_use_dec)
518 ir->d.set_use_dec(ir->d.data);
528 module_put(cdev->owner); 519 module_put(cdev->owner);
529 } else { 520 } else {
530 lirc_irctl_cleanup(ir); 521 lirc_irctl_cleanup(ir);
@@ -547,12 +538,10 @@ unsigned int lirc_dev_fop_poll(struct file *file, poll_table *wait)
547 unsigned int ret; 538 unsigned int ret;
548 539
549 if (!ir) { 540 if (!ir) {
550 printk(KERN_ERR "%s: called with invalid irctl\n", __func__); 541 pr_err("called with invalid irctl\n");
551 return POLLERR; 542 return POLLERR;
552 } 543 }
553 544
554 dev_dbg(ir->d.dev, LOGHEAD "poll called\n", ir->d.name, ir->d.minor);
555
556 if (!ir->attached) 545 if (!ir->attached)
557 return POLLERR; 546 return POLLERR;
558 547
@@ -580,7 +569,7 @@ long lirc_dev_fop_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
580 struct irctl *ir = irctls[iminor(file_inode(file))]; 569 struct irctl *ir = irctls[iminor(file_inode(file))];
581 570
582 if (!ir) { 571 if (!ir) {
583 printk(KERN_ERR "lirc_dev: %s: no irctl found!\n", __func__); 572 pr_err("no irctl found!\n");
584 return -ENODEV; 573 return -ENODEV;
585 } 574 }
586 575
@@ -588,7 +577,7 @@ long lirc_dev_fop_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
588 ir->d.name, ir->d.minor, cmd); 577 ir->d.name, ir->d.minor, cmd);
589 578
590 if (ir->d.minor == NOPLUG || !ir->attached) { 579 if (ir->d.minor == NOPLUG || !ir->attached) {
591 dev_dbg(ir->d.dev, LOGHEAD "ioctl result = -ENODEV\n", 580 dev_err(ir->d.dev, LOGHEAD "ioctl result = -ENODEV\n",
592 ir->d.name, ir->d.minor); 581 ir->d.name, ir->d.minor);
593 return -ENODEV; 582 return -ENODEV;
594 } 583 }
@@ -600,8 +589,8 @@ long lirc_dev_fop_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
600 result = put_user(ir->d.features, (__u32 __user *)arg); 589 result = put_user(ir->d.features, (__u32 __user *)arg);
601 break; 590 break;
602 case LIRC_GET_REC_MODE: 591 case LIRC_GET_REC_MODE:
603 if (!(ir->d.features & LIRC_CAN_REC_MASK)) { 592 if (LIRC_CAN_REC(ir->d.features)) {
604 result = -ENOSYS; 593 result = -ENOTTY;
605 break; 594 break;
606 } 595 }
607 596
@@ -610,8 +599,8 @@ long lirc_dev_fop_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
610 (__u32 __user *)arg); 599 (__u32 __user *)arg);
611 break; 600 break;
612 case LIRC_SET_REC_MODE: 601 case LIRC_SET_REC_MODE:
613 if (!(ir->d.features & LIRC_CAN_REC_MASK)) { 602 if (LIRC_CAN_REC(ir->d.features)) {
614 result = -ENOSYS; 603 result = -ENOTTY;
615 break; 604 break;
616 } 605 }
617 606
@@ -629,7 +618,7 @@ long lirc_dev_fop_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
629 case LIRC_GET_MIN_TIMEOUT: 618 case LIRC_GET_MIN_TIMEOUT:
630 if (!(ir->d.features & LIRC_CAN_SET_REC_TIMEOUT) || 619 if (!(ir->d.features & LIRC_CAN_SET_REC_TIMEOUT) ||
631 ir->d.min_timeout == 0) { 620 ir->d.min_timeout == 0) {
632 result = -ENOSYS; 621 result = -ENOTTY;
633 break; 622 break;
634 } 623 }
635 624
@@ -638,7 +627,7 @@ long lirc_dev_fop_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
638 case LIRC_GET_MAX_TIMEOUT: 627 case LIRC_GET_MAX_TIMEOUT:
639 if (!(ir->d.features & LIRC_CAN_SET_REC_TIMEOUT) || 628 if (!(ir->d.features & LIRC_CAN_SET_REC_TIMEOUT) ||
640 ir->d.max_timeout == 0) { 629 ir->d.max_timeout == 0) {
641 result = -ENOSYS; 630 result = -ENOTTY;
642 break; 631 break;
643 } 632 }
644 633
@@ -648,9 +637,6 @@ long lirc_dev_fop_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
648 result = -EINVAL; 637 result = -EINVAL;
649 } 638 }
650 639
651 dev_dbg(ir->d.dev, LOGHEAD "ioctl result = %d\n",
652 ir->d.name, ir->d.minor, result);
653
654 mutex_unlock(&ir->irctl_lock); 640 mutex_unlock(&ir->irctl_lock);
655 641
656 return result; 642 return result;
@@ -668,7 +654,7 @@ ssize_t lirc_dev_fop_read(struct file *file,
668 DECLARE_WAITQUEUE(wait, current); 654 DECLARE_WAITQUEUE(wait, current);
669 655
670 if (!ir) { 656 if (!ir) {
671 printk(KERN_ERR "%s: called with invalid irctl\n", __func__); 657 pr_err("called with invalid irctl\n");
672 return -ENODEV; 658 return -ENODEV;
673 } 659 }
674 660
@@ -709,7 +695,8 @@ ssize_t lirc_dev_fop_read(struct file *file,
709 /* According to the read(2) man page, 'written' can be 695 /* According to the read(2) man page, 'written' can be
710 * returned as less than 'length', instead of blocking 696 * returned as less than 'length', instead of blocking
711 * again, returning -EWOULDBLOCK, or returning 697 * again, returning -EWOULDBLOCK, or returning
712 * -ERESTARTSYS */ 698 * -ERESTARTSYS
699 */
713 if (written) 700 if (written)
714 break; 701 break;
715 if (file->f_flags & O_NONBLOCK) { 702 if (file->f_flags & O_NONBLOCK) {
@@ -755,8 +742,6 @@ out_locked:
755 742
756out_unlocked: 743out_unlocked:
757 kfree(buf); 744 kfree(buf);
758 dev_dbg(ir->d.dev, LOGHEAD "read result = %s (%d)\n",
759 ir->d.name, ir->d.minor, ret ? "<fail>" : "<ok>", ret);
760 745
761 return ret ? ret : written; 746 return ret ? ret : written;
762} 747}
@@ -775,12 +760,10 @@ ssize_t lirc_dev_fop_write(struct file *file, const char __user *buffer,
775 struct irctl *ir = irctls[iminor(file_inode(file))]; 760 struct irctl *ir = irctls[iminor(file_inode(file))];
776 761
777 if (!ir) { 762 if (!ir) {
778 printk(KERN_ERR "%s: called with invalid irctl\n", __func__); 763 pr_err("called with invalid irctl\n");
779 return -ENODEV; 764 return -ENODEV;
780 } 765 }
781 766
782 dev_dbg(ir->d.dev, LOGHEAD "write called\n", ir->d.name, ir->d.minor);
783
784 if (!ir->attached) 767 if (!ir->attached)
785 return -ENODEV; 768 return -ENODEV;
786 769
@@ -795,25 +778,23 @@ static int __init lirc_dev_init(void)
795 778
796 lirc_class = class_create(THIS_MODULE, "lirc"); 779 lirc_class = class_create(THIS_MODULE, "lirc");
797 if (IS_ERR(lirc_class)) { 780 if (IS_ERR(lirc_class)) {
798 retval = PTR_ERR(lirc_class); 781 pr_err("class_create failed\n");
799 printk(KERN_ERR "lirc_dev: class_create failed\n"); 782 return PTR_ERR(lirc_class);
800 goto error;
801 } 783 }
802 784
803 retval = alloc_chrdev_region(&lirc_base_dev, 0, MAX_IRCTL_DEVICES, 785 retval = alloc_chrdev_region(&lirc_base_dev, 0, MAX_IRCTL_DEVICES,
804 IRCTL_DEV_NAME); 786 IRCTL_DEV_NAME);
805 if (retval) { 787 if (retval) {
806 class_destroy(lirc_class); 788 class_destroy(lirc_class);
807 printk(KERN_ERR "lirc_dev: alloc_chrdev_region failed\n"); 789 pr_err("alloc_chrdev_region failed\n");
808 goto error; 790 return retval;
809 } 791 }
810 792
811 793
812 printk(KERN_INFO "lirc_dev: IR Remote Control driver registered, " 794 pr_info("IR Remote Control driver registered, major %d\n",
813 "major %d \n", MAJOR(lirc_base_dev)); 795 MAJOR(lirc_base_dev));
814 796
815error: 797 return 0;
816 return retval;
817} 798}
818 799
819 800
@@ -822,7 +803,7 @@ static void __exit lirc_dev_exit(void)
822{ 803{
823 class_destroy(lirc_class); 804 class_destroy(lirc_class);
824 unregister_chrdev_region(lirc_base_dev, MAX_IRCTL_DEVICES); 805 unregister_chrdev_region(lirc_base_dev, MAX_IRCTL_DEVICES);
825 printk(KERN_INFO "lirc_dev: module unloaded\n"); 806 pr_info("module unloaded\n");
826} 807}
827 808
828module_init(lirc_dev_init); 809module_init(lirc_dev_init);
diff --git a/drivers/media/rc/mceusb.c b/drivers/media/rc/mceusb.c
index 5cf2e749b9eb..4f8c7effdcee 100644
--- a/drivers/media/rc/mceusb.c
+++ b/drivers/media/rc/mceusb.c
@@ -887,6 +887,12 @@ static int mceusb_set_tx_mask(struct rc_dev *dev, u32 mask)
887{ 887{
888 struct mceusb_dev *ir = dev->priv; 888 struct mceusb_dev *ir = dev->priv;
889 889
890 /* return number of transmitters */
891 int emitters = ir->num_txports ? ir->num_txports : 2;
892
893 if (mask >= (1 << emitters))
894 return emitters;
895
890 if (ir->flags.tx_mask_normal) 896 if (ir->flags.tx_mask_normal)
891 ir->tx_mask = mask; 897 ir->tx_mask = mask;
892 else 898 else
@@ -936,7 +942,7 @@ static int mceusb_set_tx_carrier(struct rc_dev *dev, u32 carrier)
936 942
937 } 943 }
938 944
939 return carrier; 945 return 0;
940} 946}
941 947
942/* 948/*
diff --git a/drivers/media/rc/nuvoton-cir.c b/drivers/media/rc/nuvoton-cir.c
index 99b303b702ac..00215f343819 100644
--- a/drivers/media/rc/nuvoton-cir.c
+++ b/drivers/media/rc/nuvoton-cir.c
@@ -139,11 +139,7 @@ static inline void nvt_cir_reg_write(struct nvt_dev *nvt, u8 val, u8 offset)
139/* read val from cir config register */ 139/* read val from cir config register */
140static u8 nvt_cir_reg_read(struct nvt_dev *nvt, u8 offset) 140static u8 nvt_cir_reg_read(struct nvt_dev *nvt, u8 offset)
141{ 141{
142 u8 val; 142 return inb(nvt->cir_addr + offset);
143
144 val = inb(nvt->cir_addr + offset);
145
146 return val;
147} 143}
148 144
149/* write val to cir wake register */ 145/* write val to cir wake register */
@@ -156,11 +152,7 @@ static inline void nvt_cir_wake_reg_write(struct nvt_dev *nvt,
156/* read val from cir wake config register */ 152/* read val from cir wake config register */
157static u8 nvt_cir_wake_reg_read(struct nvt_dev *nvt, u8 offset) 153static u8 nvt_cir_wake_reg_read(struct nvt_dev *nvt, u8 offset)
158{ 154{
159 u8 val; 155 return inb(nvt->cir_wake_addr + offset);
160
161 val = inb(nvt->cir_wake_addr + offset);
162
163 return val;
164} 156}
165 157
166/* don't override io address if one is set already */ 158/* don't override io address if one is set already */
@@ -401,6 +393,7 @@ static int nvt_hw_detect(struct nvt_dev *nvt)
401 /* Check if we're wired for the alternate EFER setup */ 393 /* Check if we're wired for the alternate EFER setup */
402 nvt->chip_major = nvt_cr_read(nvt, CR_CHIP_ID_HI); 394 nvt->chip_major = nvt_cr_read(nvt, CR_CHIP_ID_HI);
403 if (nvt->chip_major == 0xff) { 395 if (nvt->chip_major == 0xff) {
396 nvt_efm_disable(nvt);
404 nvt->cr_efir = CR_EFIR2; 397 nvt->cr_efir = CR_EFIR2;
405 nvt->cr_efdr = CR_EFDR2; 398 nvt->cr_efdr = CR_EFDR2;
406 nvt_efm_enable(nvt); 399 nvt_efm_enable(nvt);
@@ -480,18 +473,14 @@ static void nvt_cir_wake_ldev_init(struct nvt_dev *nvt)
480 473
481 nvt_set_ioaddr(nvt, &nvt->cir_wake_addr); 474 nvt_set_ioaddr(nvt, &nvt->cir_wake_addr);
482 475
483 nvt_cr_write(nvt, nvt->cir_wake_irq, CR_CIR_IRQ_RSRC); 476 nvt_dbg("CIR Wake initialized, base io port address: 0x%lx",
484 477 nvt->cir_wake_addr);
485 nvt_dbg("CIR Wake initialized, base io port address: 0x%lx, irq: %d",
486 nvt->cir_wake_addr, nvt->cir_wake_irq);
487} 478}
488 479
489/* clear out the hardware's cir rx fifo */ 480/* clear out the hardware's cir rx fifo */
490static void nvt_clear_cir_fifo(struct nvt_dev *nvt) 481static void nvt_clear_cir_fifo(struct nvt_dev *nvt)
491{ 482{
492 u8 val; 483 u8 val = nvt_cir_reg_read(nvt, CIR_FIFOCON);
493
494 val = nvt_cir_reg_read(nvt, CIR_FIFOCON);
495 nvt_cir_reg_write(nvt, val | CIR_FIFOCON_RXFIFOCLR, CIR_FIFOCON); 484 nvt_cir_reg_write(nvt, val | CIR_FIFOCON_RXFIFOCLR, CIR_FIFOCON);
496} 485}
497 486
@@ -527,7 +516,7 @@ static void nvt_set_cir_iren(struct nvt_dev *nvt)
527{ 516{
528 u8 iren; 517 u8 iren;
529 518
530 iren = CIR_IREN_RTR | CIR_IREN_PE; 519 iren = CIR_IREN_RTR | CIR_IREN_PE | CIR_IREN_RFO;
531 nvt_cir_reg_write(nvt, iren, CIR_IREN); 520 nvt_cir_reg_write(nvt, iren, CIR_IREN);
532} 521}
533 522
@@ -566,34 +555,15 @@ static void nvt_cir_regs_init(struct nvt_dev *nvt)
566 555
567static void nvt_cir_wake_regs_init(struct nvt_dev *nvt) 556static void nvt_cir_wake_regs_init(struct nvt_dev *nvt)
568{ 557{
569 /* set number of bytes needed for wake from s3 (default 65) */
570 nvt_cir_wake_reg_write(nvt, CIR_WAKE_FIFO_CMP_BYTES,
571 CIR_WAKE_FIFO_CMP_DEEP);
572
573 /* set tolerance/variance allowed per byte during wake compare */
574 nvt_cir_wake_reg_write(nvt, CIR_WAKE_CMP_TOLERANCE,
575 CIR_WAKE_FIFO_CMP_TOL);
576
577 /* set sample limit count (PE interrupt raised when reached) */
578 nvt_cir_wake_reg_write(nvt, CIR_RX_LIMIT_COUNT >> 8, CIR_WAKE_SLCH);
579 nvt_cir_wake_reg_write(nvt, CIR_RX_LIMIT_COUNT & 0xff, CIR_WAKE_SLCL);
580
581 /* set cir wake fifo rx trigger level (currently 67) */
582 nvt_cir_wake_reg_write(nvt, CIR_WAKE_FIFOCON_RX_TRIGGER_LEV,
583 CIR_WAKE_FIFOCON);
584
585 /* 558 /*
586 * Enable TX and RX, specific carrier on = low, off = high, and set 559 * Disable RX, set specific carrier on = low, off = high,
587 * sample period (currently 50us) 560 * and sample period (currently 50us)
588 */ 561 */
589 nvt_cir_wake_reg_write(nvt, CIR_WAKE_IRCON_MODE0 | CIR_WAKE_IRCON_RXEN | 562 nvt_cir_wake_reg_write(nvt, CIR_WAKE_IRCON_MODE0 |
590 CIR_WAKE_IRCON_R | CIR_WAKE_IRCON_RXINV | 563 CIR_WAKE_IRCON_R | CIR_WAKE_IRCON_RXINV |
591 CIR_WAKE_IRCON_SAMPLE_PERIOD_SEL, 564 CIR_WAKE_IRCON_SAMPLE_PERIOD_SEL,
592 CIR_WAKE_IRCON); 565 CIR_WAKE_IRCON);
593 566
594 /* clear cir wake rx fifo */
595 nvt_clear_cir_wake_fifo(nvt);
596
597 /* clear any and all stray interrupts */ 567 /* clear any and all stray interrupts */
598 nvt_cir_wake_reg_write(nvt, 0xff, CIR_WAKE_IRSTS); 568 nvt_cir_wake_reg_write(nvt, 0xff, CIR_WAKE_IRSTS);
599 569
@@ -788,8 +758,6 @@ static void nvt_process_rx_ir_data(struct nvt_dev *nvt)
788 758
789 nvt_dbg_verbose("Processing buffer of len %d", nvt->pkts); 759 nvt_dbg_verbose("Processing buffer of len %d", nvt->pkts);
790 760
791 init_ir_raw_event(&rawir);
792
793 for (i = 0; i < nvt->pkts; i++) { 761 for (i = 0; i < nvt->pkts; i++) {
794 sample = nvt->buf[i]; 762 sample = nvt->buf[i];
795 763
@@ -835,19 +803,10 @@ static void nvt_get_rx_ir_data(struct nvt_dev *nvt)
835{ 803{
836 u8 fifocount, val; 804 u8 fifocount, val;
837 unsigned int b_idx; 805 unsigned int b_idx;
838 bool overrun = false;
839 int i; 806 int i;
840 807
841 /* Get count of how many bytes to read from RX FIFO */ 808 /* Get count of how many bytes to read from RX FIFO */
842 fifocount = nvt_cir_reg_read(nvt, CIR_RXFCONT); 809 fifocount = nvt_cir_reg_read(nvt, CIR_RXFCONT);
843 /* if we get 0xff, probably means the logical dev is disabled */
844 if (fifocount == 0xff)
845 return;
846 /* watch out for a fifo overrun condition */
847 else if (fifocount > RX_BUF_LEN) {
848 overrun = true;
849 fifocount = RX_BUF_LEN;
850 }
851 810
852 nvt_dbg("attempting to fetch %u bytes from hw rx fifo", fifocount); 811 nvt_dbg("attempting to fetch %u bytes from hw rx fifo", fifocount);
853 812
@@ -869,9 +828,6 @@ static void nvt_get_rx_ir_data(struct nvt_dev *nvt)
869 nvt_dbg("%s: pkts now %d", __func__, nvt->pkts); 828 nvt_dbg("%s: pkts now %d", __func__, nvt->pkts);
870 829
871 nvt_process_rx_ir_data(nvt); 830 nvt_process_rx_ir_data(nvt);
872
873 if (overrun)
874 nvt_handle_rx_fifo_overrun(nvt);
875} 831}
876 832
877static void nvt_cir_log_irqs(u8 status, u8 iren) 833static void nvt_cir_log_irqs(u8 status, u8 iren)
@@ -907,7 +863,7 @@ static bool nvt_cir_tx_inactive(struct nvt_dev *nvt)
907static irqreturn_t nvt_cir_isr(int irq, void *data) 863static irqreturn_t nvt_cir_isr(int irq, void *data)
908{ 864{
909 struct nvt_dev *nvt = data; 865 struct nvt_dev *nvt = data;
910 u8 status, iren, cur_state; 866 u8 status, iren;
911 unsigned long flags; 867 unsigned long flags;
912 868
913 nvt_dbg_verbose("%s firing", __func__); 869 nvt_dbg_verbose("%s firing", __func__);
@@ -945,23 +901,15 @@ static irqreturn_t nvt_cir_isr(int irq, void *data)
945 901
946 nvt_cir_log_irqs(status, iren); 902 nvt_cir_log_irqs(status, iren);
947 903
948 if (status & CIR_IRSTS_RTR) { 904 if (status & CIR_IRSTS_RFO)
949 /* FIXME: add code for study/learn mode */ 905 nvt_handle_rx_fifo_overrun(nvt);
906
907 else if (status & (CIR_IRSTS_RTR | CIR_IRSTS_PE)) {
950 /* We only do rx if not tx'ing */ 908 /* We only do rx if not tx'ing */
951 if (nvt_cir_tx_inactive(nvt)) 909 if (nvt_cir_tx_inactive(nvt))
952 nvt_get_rx_ir_data(nvt); 910 nvt_get_rx_ir_data(nvt);
953 } 911 }
954 912
955 if (status & CIR_IRSTS_PE) {
956 if (nvt_cir_tx_inactive(nvt))
957 nvt_get_rx_ir_data(nvt);
958
959 cur_state = nvt->study_state;
960
961 if (cur_state == ST_STUDY_NONE)
962 nvt_clear_cir_fifo(nvt);
963 }
964
965 spin_unlock_irqrestore(&nvt->nvt_lock, flags); 913 spin_unlock_irqrestore(&nvt->nvt_lock, flags);
966 914
967 if (status & CIR_IRSTS_TE) 915 if (status & CIR_IRSTS_TE)
@@ -1003,51 +951,6 @@ static irqreturn_t nvt_cir_isr(int irq, void *data)
1003 return IRQ_HANDLED; 951 return IRQ_HANDLED;
1004} 952}
1005 953
1006/* Interrupt service routine for CIR Wake */
1007static irqreturn_t nvt_cir_wake_isr(int irq, void *data)
1008{
1009 u8 status, iren, val;
1010 struct nvt_dev *nvt = data;
1011 unsigned long flags;
1012
1013 nvt_dbg_wake("%s firing", __func__);
1014
1015 spin_lock_irqsave(&nvt->nvt_lock, flags);
1016
1017 status = nvt_cir_wake_reg_read(nvt, CIR_WAKE_IRSTS);
1018 iren = nvt_cir_wake_reg_read(nvt, CIR_WAKE_IREN);
1019
1020 /* IRQ may be shared with CIR, therefore check for each
1021 * status bit whether the related interrupt source is enabled
1022 */
1023 if (!(status & iren)) {
1024 spin_unlock_irqrestore(&nvt->nvt_lock, flags);
1025 return IRQ_NONE;
1026 }
1027
1028 if (status & CIR_WAKE_IRSTS_IR_PENDING)
1029 nvt_clear_cir_wake_fifo(nvt);
1030
1031 nvt_cir_wake_reg_write(nvt, status, CIR_WAKE_IRSTS);
1032 nvt_cir_wake_reg_write(nvt, 0, CIR_WAKE_IRSTS);
1033
1034 if ((status & CIR_WAKE_IRSTS_PE) &&
1035 (nvt->wake_state == ST_WAKE_START)) {
1036 while (nvt_cir_wake_reg_read(nvt, CIR_WAKE_RD_FIFO_ONLY_IDX)) {
1037 val = nvt_cir_wake_reg_read(nvt, CIR_WAKE_RD_FIFO_ONLY);
1038 nvt_dbg("setting wake up key: 0x%x", val);
1039 }
1040
1041 nvt_cir_wake_reg_write(nvt, 0, CIR_WAKE_IREN);
1042 nvt->wake_state = ST_WAKE_FINISH;
1043 }
1044
1045 spin_unlock_irqrestore(&nvt->nvt_lock, flags);
1046
1047 nvt_dbg_wake("%s done", __func__);
1048 return IRQ_HANDLED;
1049}
1050
1051static void nvt_disable_cir(struct nvt_dev *nvt) 954static void nvt_disable_cir(struct nvt_dev *nvt)
1052{ 955{
1053 unsigned long flags; 956 unsigned long flags;
@@ -1151,8 +1054,6 @@ static int nvt_probe(struct pnp_dev *pdev, const struct pnp_device_id *dev_id)
1151 nvt->cir_irq = pnp_irq(pdev, 0); 1054 nvt->cir_irq = pnp_irq(pdev, 0);
1152 1055
1153 nvt->cir_wake_addr = pnp_port_start(pdev, 1); 1056 nvt->cir_wake_addr = pnp_port_start(pdev, 1);
1154 /* irq is always shared between cir and cir wake */
1155 nvt->cir_wake_irq = nvt->cir_irq;
1156 1057
1157 nvt->cr_efir = CR_EFIR; 1058 nvt->cr_efir = CR_EFIR;
1158 nvt->cr_efdr = CR_EFDR; 1059 nvt->cr_efdr = CR_EFDR;
@@ -1228,11 +1129,6 @@ static int nvt_probe(struct pnp_dev *pdev, const struct pnp_device_id *dev_id)
1228 CIR_IOREG_LENGTH, NVT_DRIVER_NAME "-wake")) 1129 CIR_IOREG_LENGTH, NVT_DRIVER_NAME "-wake"))
1229 goto exit_unregister_device; 1130 goto exit_unregister_device;
1230 1131
1231 if (devm_request_irq(&pdev->dev, nvt->cir_wake_irq,
1232 nvt_cir_wake_isr, IRQF_SHARED,
1233 NVT_DRIVER_NAME "-wake", (void *)nvt))
1234 goto exit_unregister_device;
1235
1236 ret = device_create_file(&rdev->dev, &dev_attr_wakeup_data); 1132 ret = device_create_file(&rdev->dev, &dev_attr_wakeup_data);
1237 if (ret) 1133 if (ret)
1238 goto exit_unregister_device; 1134 goto exit_unregister_device;
@@ -1283,10 +1179,6 @@ static int nvt_suspend(struct pnp_dev *pdev, pm_message_t state)
1283 1179
1284 spin_lock_irqsave(&nvt->nvt_lock, flags); 1180 spin_lock_irqsave(&nvt->nvt_lock, flags);
1285 1181
1286 /* zero out misc state tracking */
1287 nvt->study_state = ST_STUDY_NONE;
1288 nvt->wake_state = ST_WAKE_NONE;
1289
1290 /* disable all CIR interrupts */ 1182 /* disable all CIR interrupts */
1291 nvt_cir_reg_write(nvt, 0, CIR_IREN); 1183 nvt_cir_reg_write(nvt, 0, CIR_IREN);
1292 1184
diff --git a/drivers/media/rc/nuvoton-cir.h b/drivers/media/rc/nuvoton-cir.h
index c9c98ebb19ee..acf735fc7170 100644
--- a/drivers/media/rc/nuvoton-cir.h
+++ b/drivers/media/rc/nuvoton-cir.h
@@ -104,7 +104,6 @@ struct nvt_dev {
104 unsigned long cir_addr; 104 unsigned long cir_addr;
105 unsigned long cir_wake_addr; 105 unsigned long cir_wake_addr;
106 int cir_irq; 106 int cir_irq;
107 int cir_wake_irq;
108 107
109 enum nvt_chip_ver chip_ver; 108 enum nvt_chip_ver chip_ver;
110 /* hardware id */ 109 /* hardware id */
@@ -112,36 +111,12 @@ struct nvt_dev {
112 u8 chip_minor; 111 u8 chip_minor;
113 112
114 /* hardware features */ 113 /* hardware features */
115 bool hw_learning_capable;
116 bool hw_tx_capable; 114 bool hw_tx_capable;
117 115
118 /* rx settings */
119 bool learning_enabled;
120
121 /* track cir wake state */
122 u8 wake_state;
123 /* for study */
124 u8 study_state;
125 /* carrier period = 1 / frequency */ 116 /* carrier period = 1 / frequency */
126 u32 carrier; 117 u32 carrier;
127}; 118};
128 119
129/* study states */
130#define ST_STUDY_NONE 0x0
131#define ST_STUDY_START 0x1
132#define ST_STUDY_CARRIER 0x2
133#define ST_STUDY_ALL_RECV 0x4
134
135/* wake states */
136#define ST_WAKE_NONE 0x0
137#define ST_WAKE_START 0x1
138#define ST_WAKE_FINISH 0x2
139
140/* receive states */
141#define ST_RX_WAIT_7F 0x1
142#define ST_RX_WAIT_HEAD 0x2
143#define ST_RX_WAIT_SILENT_END 0x4
144
145/* send states */ 120/* send states */
146#define ST_TX_NONE 0x0 121#define ST_TX_NONE 0x0
147#define ST_TX_REQUEST 0x2 122#define ST_TX_REQUEST 0x2
diff --git a/drivers/media/rc/rc-main.c b/drivers/media/rc/rc-main.c
index 7dfc7c2188f0..8e7f2929fa6f 100644
--- a/drivers/media/rc/rc-main.c
+++ b/drivers/media/rc/rc-main.c
@@ -130,13 +130,18 @@ static struct rc_map_list empty_map = {
130static int ir_create_table(struct rc_map *rc_map, 130static int ir_create_table(struct rc_map *rc_map,
131 const char *name, u64 rc_type, size_t size) 131 const char *name, u64 rc_type, size_t size)
132{ 132{
133 rc_map->name = name; 133 rc_map->name = kstrdup(name, GFP_KERNEL);
134 if (!rc_map->name)
135 return -ENOMEM;
134 rc_map->rc_type = rc_type; 136 rc_map->rc_type = rc_type;
135 rc_map->alloc = roundup_pow_of_two(size * sizeof(struct rc_map_table)); 137 rc_map->alloc = roundup_pow_of_two(size * sizeof(struct rc_map_table));
136 rc_map->size = rc_map->alloc / sizeof(struct rc_map_table); 138 rc_map->size = rc_map->alloc / sizeof(struct rc_map_table);
137 rc_map->scan = kmalloc(rc_map->alloc, GFP_KERNEL); 139 rc_map->scan = kmalloc(rc_map->alloc, GFP_KERNEL);
138 if (!rc_map->scan) 140 if (!rc_map->scan) {
141 kfree(rc_map->name);
142 rc_map->name = NULL;
139 return -ENOMEM; 143 return -ENOMEM;
144 }
140 145
141 IR_dprintk(1, "Allocated space for %u keycode entries (%u bytes)\n", 146 IR_dprintk(1, "Allocated space for %u keycode entries (%u bytes)\n",
142 rc_map->size, rc_map->alloc); 147 rc_map->size, rc_map->alloc);
@@ -153,6 +158,7 @@ static int ir_create_table(struct rc_map *rc_map,
153static void ir_free_table(struct rc_map *rc_map) 158static void ir_free_table(struct rc_map *rc_map)
154{ 159{
155 rc_map->size = 0; 160 rc_map->size = 0;
161 kfree(rc_map->name);
156 kfree(rc_map->scan); 162 kfree(rc_map->scan);
157 rc_map->scan = NULL; 163 rc_map->scan = NULL;
158} 164}
@@ -804,6 +810,7 @@ static const struct {
804 { RC_BIT_SHARP, "sharp", "ir-sharp-decoder" }, 810 { RC_BIT_SHARP, "sharp", "ir-sharp-decoder" },
805 { RC_BIT_MCE_KBD, "mce_kbd", "ir-mce_kbd-decoder" }, 811 { RC_BIT_MCE_KBD, "mce_kbd", "ir-mce_kbd-decoder" },
806 { RC_BIT_XMP, "xmp", "ir-xmp-decoder" }, 812 { RC_BIT_XMP, "xmp", "ir-xmp-decoder" },
813 { RC_BIT_CEC, "cec", NULL },
807}; 814};
808 815
809/** 816/**
diff --git a/drivers/media/rc/redrat3.c b/drivers/media/rc/redrat3.c
index ec74244a3853..399f44d89a29 100644
--- a/drivers/media/rc/redrat3.c
+++ b/drivers/media/rc/redrat3.c
@@ -188,8 +188,7 @@ struct redrat3_dev {
188 /* usb dma */ 188 /* usb dma */
189 dma_addr_t dma_in; 189 dma_addr_t dma_in;
190 190
191 /* rx signal timeout timer */ 191 /* rx signal timeout */
192 struct timer_list rx_timeout;
193 u32 hw_timeout; 192 u32 hw_timeout;
194 193
195 /* Is the device currently transmitting?*/ 194 /* Is the device currently transmitting?*/
@@ -330,22 +329,11 @@ static u32 redrat3_us_to_len(u32 microsec)
330 return result ? result : 1; 329 return result ? result : 1;
331} 330}
332 331
333/* timer callback to send reset event */
334static void redrat3_rx_timeout(unsigned long data)
335{
336 struct redrat3_dev *rr3 = (struct redrat3_dev *)data;
337
338 dev_dbg(rr3->dev, "calling ir_raw_event_reset\n");
339 ir_raw_event_reset(rr3->rc);
340}
341
342static void redrat3_process_ir_data(struct redrat3_dev *rr3) 332static void redrat3_process_ir_data(struct redrat3_dev *rr3)
343{ 333{
344 DEFINE_IR_RAW_EVENT(rawir); 334 DEFINE_IR_RAW_EVENT(rawir);
345 struct device *dev; 335 struct device *dev;
346 unsigned i, trailer = 0; 336 unsigned int i, sig_size, single_len, offset, val;
347 unsigned sig_size, single_len, offset, val;
348 unsigned long delay;
349 u32 mod_freq; 337 u32 mod_freq;
350 338
351 if (!rr3) { 339 if (!rr3) {
@@ -355,10 +343,6 @@ static void redrat3_process_ir_data(struct redrat3_dev *rr3)
355 343
356 dev = rr3->dev; 344 dev = rr3->dev;
357 345
358 /* Make sure we reset the IR kfifo after a bit of inactivity */
359 delay = usecs_to_jiffies(rr3->hw_timeout);
360 mod_timer(&rr3->rx_timeout, jiffies + delay);
361
362 mod_freq = redrat3_val_to_mod_freq(&rr3->irdata); 346 mod_freq = redrat3_val_to_mod_freq(&rr3->irdata);
363 dev_dbg(dev, "Got mod_freq of %u\n", mod_freq); 347 dev_dbg(dev, "Got mod_freq of %u\n", mod_freq);
364 348
@@ -376,9 +360,6 @@ static void redrat3_process_ir_data(struct redrat3_dev *rr3)
376 rawir.pulse = true; 360 rawir.pulse = true;
377 361
378 rawir.duration = US_TO_NS(single_len); 362 rawir.duration = US_TO_NS(single_len);
379 /* Save initial pulse length to fudge trailer */
380 if (i == 0)
381 trailer = rawir.duration;
382 /* cap the value to IR_MAX_DURATION */ 363 /* cap the value to IR_MAX_DURATION */
383 rawir.duration = (rawir.duration > IR_MAX_DURATION) ? 364 rawir.duration = (rawir.duration > IR_MAX_DURATION) ?
384 IR_MAX_DURATION : rawir.duration; 365 IR_MAX_DURATION : rawir.duration;
@@ -388,18 +369,13 @@ static void redrat3_process_ir_data(struct redrat3_dev *rr3)
388 ir_raw_event_store_with_filter(rr3->rc, &rawir); 369 ir_raw_event_store_with_filter(rr3->rc, &rawir);
389 } 370 }
390 371
391 /* add a trailing space, if need be */ 372 /* add a trailing space */
392 if (i % 2) { 373 rawir.pulse = false;
393 rawir.pulse = false; 374 rawir.timeout = true;
394 /* this duration is made up, and may not be ideal... */ 375 rawir.duration = US_TO_NS(rr3->hw_timeout);
395 if (trailer < US_TO_NS(1000)) 376 dev_dbg(dev, "storing trailing timeout with duration %d\n",
396 rawir.duration = US_TO_NS(2800); 377 rawir.duration);
397 else 378 ir_raw_event_store_with_filter(rr3->rc, &rawir);
398 rawir.duration = trailer;
399 dev_dbg(dev, "storing trailing space with duration %d\n",
400 rawir.duration);
401 ir_raw_event_store_with_filter(rr3->rc, &rawir);
402 }
403 379
404 dev_dbg(dev, "calling ir_raw_event_handle\n"); 380 dev_dbg(dev, "calling ir_raw_event_handle\n");
405 ir_raw_event_handle(rr3->rc); 381 ir_raw_event_handle(rr3->rc);
@@ -499,6 +475,37 @@ static u32 redrat3_get_timeout(struct redrat3_dev *rr3)
499 return timeout; 475 return timeout;
500} 476}
501 477
478static int redrat3_set_timeout(struct rc_dev *rc_dev, unsigned int timeoutns)
479{
480 struct redrat3_dev *rr3 = rc_dev->priv;
481 struct usb_device *udev = rr3->udev;
482 struct device *dev = rr3->dev;
483 u32 *timeout;
484 int ret;
485
486 timeout = kmalloc(sizeof(*timeout), GFP_KERNEL);
487 if (!timeout)
488 return -ENOMEM;
489
490 *timeout = cpu_to_be32(redrat3_us_to_len(timeoutns / 1000));
491 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), RR3_SET_IR_PARAM,
492 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
493 RR3_IR_IO_SIG_TIMEOUT, 0, timeout, sizeof(*timeout),
494 HZ * 25);
495 dev_dbg(dev, "set ir parm timeout %d ret 0x%02x\n",
496 be32_to_cpu(*timeout), ret);
497
498 if (ret == sizeof(*timeout)) {
499 rr3->hw_timeout = timeoutns / 1000;
500 ret = 0;
501 } else if (ret >= 0)
502 ret = -EIO;
503
504 kfree(timeout);
505
506 return ret;
507}
508
502static void redrat3_reset(struct redrat3_dev *rr3) 509static void redrat3_reset(struct redrat3_dev *rr3)
503{ 510{
504 struct usb_device *udev = rr3->udev; 511 struct usb_device *udev = rr3->udev;
@@ -708,7 +715,7 @@ static int redrat3_set_tx_carrier(struct rc_dev *rcdev, u32 carrier)
708 715
709 rr3->carrier = carrier; 716 rr3->carrier = carrier;
710 717
711 return carrier; 718 return 0;
712} 719}
713 720
714static int redrat3_transmit_ir(struct rc_dev *rcdev, unsigned *txbuf, 721static int redrat3_transmit_ir(struct rc_dev *rcdev, unsigned *txbuf,
@@ -880,7 +887,10 @@ static struct rc_dev *redrat3_init_rc_dev(struct redrat3_dev *rr3)
880 rc->priv = rr3; 887 rc->priv = rr3;
881 rc->driver_type = RC_DRIVER_IR_RAW; 888 rc->driver_type = RC_DRIVER_IR_RAW;
882 rc->allowed_protocols = RC_BIT_ALL; 889 rc->allowed_protocols = RC_BIT_ALL;
883 rc->timeout = US_TO_NS(2750); 890 rc->min_timeout = MS_TO_NS(RR3_RX_MIN_TIMEOUT);
891 rc->max_timeout = MS_TO_NS(RR3_RX_MAX_TIMEOUT);
892 rc->timeout = US_TO_NS(rr3->hw_timeout);
893 rc->s_timeout = redrat3_set_timeout;
884 rc->tx_ir = redrat3_transmit_ir; 894 rc->tx_ir = redrat3_transmit_ir;
885 rc->s_tx_carrier = redrat3_set_tx_carrier; 895 rc->s_tx_carrier = redrat3_set_tx_carrier;
886 rc->driver_name = DRIVER_NAME; 896 rc->driver_name = DRIVER_NAME;
@@ -990,7 +1000,7 @@ static int redrat3_dev_probe(struct usb_interface *intf,
990 if (retval < 0) 1000 if (retval < 0)
991 goto error; 1001 goto error;
992 1002
993 /* store current hardware timeout, in us, will use for kfifo resets */ 1003 /* store current hardware timeout, in µs */
994 rr3->hw_timeout = redrat3_get_timeout(rr3); 1004 rr3->hw_timeout = redrat3_get_timeout(rr3);
995 1005
996 /* default.. will get overridden by any sends with a freq defined */ 1006 /* default.. will get overridden by any sends with a freq defined */
@@ -1026,7 +1036,6 @@ static int redrat3_dev_probe(struct usb_interface *intf,
1026 retval = -ENOMEM; 1036 retval = -ENOMEM;
1027 goto led_free_error; 1037 goto led_free_error;
1028 } 1038 }
1029 setup_timer(&rr3->rx_timeout, redrat3_rx_timeout, (unsigned long)rr3);
1030 1039
1031 /* we can register the device now, as it is ready */ 1040 /* we can register the device now, as it is ready */
1032 usb_set_intfdata(intf, rr3); 1041 usb_set_intfdata(intf, rr3);
@@ -1055,7 +1064,6 @@ static void redrat3_dev_disconnect(struct usb_interface *intf)
1055 usb_set_intfdata(intf, NULL); 1064 usb_set_intfdata(intf, NULL);
1056 rc_unregister_device(rr3->rc); 1065 rc_unregister_device(rr3->rc);
1057 led_classdev_unregister(&rr3->led); 1066 led_classdev_unregister(&rr3->led);
1058 del_timer_sync(&rr3->rx_timeout);
1059 redrat3_delete(rr3, udev); 1067 redrat3_delete(rr3, udev);
1060} 1068}
1061 1069
diff --git a/drivers/media/rc/winbond-cir.c b/drivers/media/rc/winbond-cir.c
index d839f73f6a05..95ae60e659a1 100644
--- a/drivers/media/rc/winbond-cir.c
+++ b/drivers/media/rc/winbond-cir.c
@@ -615,6 +615,10 @@ wbcir_txmask(struct rc_dev *dev, u32 mask)
615 unsigned long flags; 615 unsigned long flags;
616 u8 val; 616 u8 val;
617 617
618 /* return the number of transmitters */
619 if (mask > 15)
620 return 4;
621
618 /* Four outputs, only one output can be enabled at a time */ 622 /* Four outputs, only one output can be enabled at a time */
619 switch (mask) { 623 switch (mask) {
620 case 0x1: 624 case 0x1:
diff --git a/drivers/media/tuners/it913x.c b/drivers/media/tuners/it913x.c
index 5c96da693289..6c3ef2181fcd 100644
--- a/drivers/media/tuners/it913x.c
+++ b/drivers/media/tuners/it913x.c
@@ -464,6 +464,7 @@ MODULE_DEVICE_TABLE(i2c, it913x_id_table);
464static struct i2c_driver it913x_driver = { 464static struct i2c_driver it913x_driver = {
465 .driver = { 465 .driver = {
466 .name = "it913x", 466 .name = "it913x",
467 .suppress_bind_attrs = true,
467 }, 468 },
468 .probe = it913x_probe, 469 .probe = it913x_probe,
469 .remove = it913x_remove, 470 .remove = it913x_remove,
diff --git a/drivers/media/tuners/mt2063.c b/drivers/media/tuners/mt2063.c
index 6457ac91ef09..7f0b9d5940db 100644
--- a/drivers/media/tuners/mt2063.c
+++ b/drivers/media/tuners/mt2063.c
@@ -24,6 +24,7 @@
24#include <linux/module.h> 24#include <linux/module.h>
25#include <linux/string.h> 25#include <linux/string.h>
26#include <linux/videodev2.h> 26#include <linux/videodev2.h>
27#include <linux/gcd.h>
27 28
28#include "mt2063.h" 29#include "mt2063.h"
29 30
@@ -665,27 +666,6 @@ static u32 MT2063_ChooseFirstIF(struct MT2063_AvoidSpursData_t *pAS_Info)
665} 666}
666 667
667/** 668/**
668 * gcd() - Uses Euclid's algorithm
669 *
670 * @u, @v: Unsigned values whose GCD is desired.
671 *
672 * Returns THE greatest common divisor of u and v, if either value is 0,
673 * the other value is returned as the result.
674 */
675static u32 MT2063_gcd(u32 u, u32 v)
676{
677 u32 r;
678
679 while (v != 0) {
680 r = u % v;
681 u = v;
682 v = r;
683 }
684
685 return u;
686}
687
688/**
689 * IsSpurInBand() - Checks to see if a spur will be present within the IF's 669 * IsSpurInBand() - Checks to see if a spur will be present within the IF's
690 * bandwidth. (fIFOut +/- fIFBW, -fIFOut +/- fIFBW) 670 * bandwidth. (fIFOut +/- fIFBW, -fIFOut +/- fIFBW)
691 * 671 *
@@ -731,12 +711,12 @@ static u32 IsSpurInBand(struct MT2063_AvoidSpursData_t *pAS_Info,
731 ** of f_LO1, f_LO2 and the edge value. Use the larger of this 711 ** of f_LO1, f_LO2 and the edge value. Use the larger of this
732 ** gcd-based scale factor or f_Scale. 712 ** gcd-based scale factor or f_Scale.
733 */ 713 */
734 lo_gcd = MT2063_gcd(f_LO1, f_LO2); 714 lo_gcd = gcd(f_LO1, f_LO2);
735 gd_Scale = max((u32) MT2063_gcd(lo_gcd, d), f_Scale); 715 gd_Scale = max((u32) gcd(lo_gcd, d), f_Scale);
736 hgds = gd_Scale / 2; 716 hgds = gd_Scale / 2;
737 gc_Scale = max((u32) MT2063_gcd(lo_gcd, c), f_Scale); 717 gc_Scale = max((u32) gcd(lo_gcd, c), f_Scale);
738 hgcs = gc_Scale / 2; 718 hgcs = gc_Scale / 2;
739 gf_Scale = max((u32) MT2063_gcd(lo_gcd, f), f_Scale); 719 gf_Scale = max((u32) gcd(lo_gcd, f), f_Scale);
740 hgfs = gf_Scale / 2; 720 hgfs = gf_Scale / 2;
741 721
742 n0 = DIV_ROUND_UP(f_LO2 - d, f_LO1 - f_LO2); 722 n0 = DIV_ROUND_UP(f_LO2 - d, f_LO1 - f_LO2);
diff --git a/drivers/media/tuners/r820t.c b/drivers/media/tuners/r820t.c
index 6ab35e315fe7..08dca40356d2 100644
--- a/drivers/media/tuners/r820t.c
+++ b/drivers/media/tuners/r820t.c
@@ -337,20 +337,6 @@ static int r820t_xtal_capacitor[][2] = {
337}; 337};
338 338
339/* 339/*
340 * measured with a Racal 6103E GSM test set at 928 MHz with -60 dBm
341 * input power, for raw results see:
342 * http://steve-m.de/projects/rtl-sdr/gain_measurement/r820t/
343 */
344
345static const int r820t_lna_gain_steps[] = {
346 0, 9, 13, 40, 38, 13, 31, 22, 26, 31, 26, 14, 19, 5, 35, 13
347};
348
349static const int r820t_mixer_gain_steps[] = {
350 0, 5, 10, 10, 19, 9, 10, 25, 17, 10, 8, 16, 13, 6, 3, -8
351};
352
353/*
354 * I2C read/write code and shadow registers logic 340 * I2C read/write code and shadow registers logic
355 */ 341 */
356static void shadow_store(struct r820t_priv *priv, u8 reg, const u8 *val, 342static void shadow_store(struct r820t_priv *priv, u8 reg, const u8 *val,
@@ -1216,6 +1202,21 @@ static int r820t_read_gain(struct r820t_priv *priv)
1216 1202
1217#if 0 1203#if 0
1218/* FIXME: This routine requires more testing */ 1204/* FIXME: This routine requires more testing */
1205
1206/*
1207 * measured with a Racal 6103E GSM test set at 928 MHz with -60 dBm
1208 * input power, for raw results see:
1209 * http://steve-m.de/projects/rtl-sdr/gain_measurement/r820t/
1210 */
1211
1212static const int r820t_lna_gain_steps[] = {
1213 0, 9, 13, 40, 38, 13, 31, 22, 26, 31, 26, 14, 19, 5, 35, 13
1214};
1215
1216static const int r820t_mixer_gain_steps[] = {
1217 0, 5, 10, 10, 19, 9, 10, 25, 17, 10, 8, 16, 13, 6, 3, -8
1218};
1219
1219static int r820t_set_gain_mode(struct r820t_priv *priv, 1220static int r820t_set_gain_mode(struct r820t_priv *priv,
1220 bool set_manual_gain, 1221 bool set_manual_gain,
1221 int gain) 1222 int gain)
diff --git a/drivers/media/tuners/si2157.c b/drivers/media/tuners/si2157.c
index b07a681f3fbc..57b250847cd3 100644
--- a/drivers/media/tuners/si2157.c
+++ b/drivers/media/tuners/si2157.c
@@ -514,7 +514,8 @@ MODULE_DEVICE_TABLE(i2c, si2157_id_table);
514 514
515static struct i2c_driver si2157_driver = { 515static struct i2c_driver si2157_driver = {
516 .driver = { 516 .driver = {
517 .name = "si2157", 517 .name = "si2157",
518 .suppress_bind_attrs = true,
518 }, 519 },
519 .probe = si2157_probe, 520 .probe = si2157_probe,
520 .remove = si2157_remove, 521 .remove = si2157_remove,
diff --git a/drivers/media/usb/airspy/airspy.c b/drivers/media/usb/airspy/airspy.c
index 92d9d4214c3a..fe031b06935f 100644
--- a/drivers/media/usb/airspy/airspy.c
+++ b/drivers/media/usb/airspy/airspy.c
@@ -488,7 +488,7 @@ static void airspy_disconnect(struct usb_interface *intf)
488/* Videobuf2 operations */ 488/* Videobuf2 operations */
489static int airspy_queue_setup(struct vb2_queue *vq, 489static int airspy_queue_setup(struct vb2_queue *vq,
490 unsigned int *nbuffers, 490 unsigned int *nbuffers,
491 unsigned int *nplanes, unsigned int sizes[], void *alloc_ctxs[]) 491 unsigned int *nplanes, unsigned int sizes[], struct device *alloc_devs[])
492{ 492{
493 struct airspy *s = vb2_get_drv_priv(vq); 493 struct airspy *s = vb2_get_drv_priv(vq);
494 494
diff --git a/drivers/media/usb/au0828/au0828-core.c b/drivers/media/usb/au0828/au0828-core.c
index 321ea5cf1329..bf53553d2624 100644
--- a/drivers/media/usb/au0828/au0828-core.c
+++ b/drivers/media/usb/au0828/au0828-core.c
@@ -142,7 +142,7 @@ static void au0828_unregister_media_device(struct au0828_dev *dev)
142 struct media_device *mdev = dev->media_dev; 142 struct media_device *mdev = dev->media_dev;
143 struct media_entity_notify *notify, *nextp; 143 struct media_entity_notify *notify, *nextp;
144 144
145 if (!mdev || !media_devnode_is_registered(&mdev->devnode)) 145 if (!mdev || !media_devnode_is_registered(mdev->devnode))
146 return; 146 return;
147 147
148 /* Remove au0828 entity_notify callbacks */ 148 /* Remove au0828 entity_notify callbacks */
@@ -482,7 +482,7 @@ static int au0828_media_device_register(struct au0828_dev *dev,
482 if (!dev->media_dev) 482 if (!dev->media_dev)
483 return 0; 483 return 0;
484 484
485 if (!media_devnode_is_registered(&dev->media_dev->devnode)) { 485 if (!media_devnode_is_registered(dev->media_dev->devnode)) {
486 486
487 /* register media device */ 487 /* register media device */
488 ret = media_device_register(dev->media_dev); 488 ret = media_device_register(dev->media_dev);
diff --git a/drivers/media/usb/au0828/au0828-vbi.c b/drivers/media/usb/au0828/au0828-vbi.c
index b4efc103ae57..e0930ce59b8d 100644
--- a/drivers/media/usb/au0828/au0828-vbi.c
+++ b/drivers/media/usb/au0828/au0828-vbi.c
@@ -32,7 +32,7 @@
32 32
33static int vbi_queue_setup(struct vb2_queue *vq, 33static int vbi_queue_setup(struct vb2_queue *vq,
34 unsigned int *nbuffers, unsigned int *nplanes, 34 unsigned int *nbuffers, unsigned int *nplanes,
35 unsigned int sizes[], void *alloc_ctxs[]) 35 unsigned int sizes[], struct device *alloc_devs[])
36{ 36{
37 struct au0828_dev *dev = vb2_get_drv_priv(vq); 37 struct au0828_dev *dev = vb2_get_drv_priv(vq);
38 unsigned long size = dev->vbi_width * dev->vbi_height * 2; 38 unsigned long size = dev->vbi_width * dev->vbi_height * 2;
diff --git a/drivers/media/usb/au0828/au0828-video.c b/drivers/media/usb/au0828/au0828-video.c
index 7d0ec4cb248c..82b026985868 100644
--- a/drivers/media/usb/au0828/au0828-video.c
+++ b/drivers/media/usb/au0828/au0828-video.c
@@ -698,7 +698,7 @@ int au0828_v4l2_device_register(struct usb_interface *interface,
698 698
699static int queue_setup(struct vb2_queue *vq, 699static int queue_setup(struct vb2_queue *vq,
700 unsigned int *nbuffers, unsigned int *nplanes, 700 unsigned int *nbuffers, unsigned int *nplanes,
701 unsigned int sizes[], void *alloc_ctxs[]) 701 unsigned int sizes[], struct device *alloc_devs[])
702{ 702{
703 struct au0828_dev *dev = vb2_get_drv_priv(vq); 703 struct au0828_dev *dev = vb2_get_drv_priv(vq);
704 unsigned long size = dev->height * dev->bytesperline; 704 unsigned long size = dev->height * dev->bytesperline;
diff --git a/drivers/media/usb/cx231xx/cx231xx-417.c b/drivers/media/usb/cx231xx/cx231xx-417.c
index 00da024b47a6..29d450c15f29 100644
--- a/drivers/media/usb/cx231xx/cx231xx-417.c
+++ b/drivers/media/usb/cx231xx/cx231xx-417.c
@@ -1570,10 +1570,12 @@ static int vidioc_s_ctrl(struct file *file, void *priv,
1570{ 1570{
1571 struct cx231xx_fh *fh = file->private_data; 1571 struct cx231xx_fh *fh = file->private_data;
1572 struct cx231xx *dev = fh->dev; 1572 struct cx231xx *dev = fh->dev;
1573 struct v4l2_subdev *sd;
1573 1574
1574 dprintk(3, "enter vidioc_s_ctrl()\n"); 1575 dprintk(3, "enter vidioc_s_ctrl()\n");
1575 /* Update the A/V core */ 1576 /* Update the A/V core */
1576 call_all(dev, core, s_ctrl, ctl); 1577 v4l2_device_for_each_subdev(sd, &dev->v4l2_dev)
1578 v4l2_s_ctrl(NULL, sd->ctrl_handler, ctl);
1577 dprintk(3, "exit vidioc_s_ctrl()\n"); 1579 dprintk(3, "exit vidioc_s_ctrl()\n");
1578 return 0; 1580 return 0;
1579} 1581}
diff --git a/drivers/media/usb/dvb-usb-v2/Kconfig b/drivers/media/usb/dvb-usb-v2/Kconfig
index 3dc8ef004f8b..524533d3eb29 100644
--- a/drivers/media/usb/dvb-usb-v2/Kconfig
+++ b/drivers/media/usb/dvb-usb-v2/Kconfig
@@ -127,17 +127,22 @@ config DVB_USB_MXL111SF
127config DVB_USB_RTL28XXU 127config DVB_USB_RTL28XXU
128 tristate "Realtek RTL28xxU DVB USB support" 128 tristate "Realtek RTL28xxU DVB USB support"
129 depends on DVB_USB_V2 && I2C_MUX 129 depends on DVB_USB_V2 && I2C_MUX
130 select DVB_MN88472 if MEDIA_SUBDRV_AUTOSELECT
131 select DVB_MN88473 if MEDIA_SUBDRV_AUTOSELECT
130 select DVB_RTL2830 132 select DVB_RTL2830
131 select DVB_RTL2832 133 select DVB_RTL2832
132 select DVB_RTL2832_SDR if (MEDIA_SUBDRV_AUTOSELECT && MEDIA_SDR_SUPPORT) 134 select DVB_RTL2832_SDR if (MEDIA_SUBDRV_AUTOSELECT && MEDIA_SDR_SUPPORT)
133 select MEDIA_TUNER_QT1010 if MEDIA_SUBDRV_AUTOSELECT 135 select DVB_SI2168 if MEDIA_SUBDRV_AUTOSELECT
134 select MEDIA_TUNER_MT2060 if MEDIA_SUBDRV_AUTOSELECT 136 select MEDIA_TUNER_E4000 if MEDIA_SUBDRV_AUTOSELECT
135 select MEDIA_TUNER_MXL5005S if MEDIA_SUBDRV_AUTOSELECT
136 select MEDIA_TUNER_FC0012 if MEDIA_SUBDRV_AUTOSELECT 137 select MEDIA_TUNER_FC0012 if MEDIA_SUBDRV_AUTOSELECT
137 select MEDIA_TUNER_FC0013 if MEDIA_SUBDRV_AUTOSELECT 138 select MEDIA_TUNER_FC0013 if MEDIA_SUBDRV_AUTOSELECT
138 select MEDIA_TUNER_E4000 if MEDIA_SUBDRV_AUTOSELECT
139 select MEDIA_TUNER_FC2580 if MEDIA_SUBDRV_AUTOSELECT 139 select MEDIA_TUNER_FC2580 if MEDIA_SUBDRV_AUTOSELECT
140 select MEDIA_TUNER_MT2060 if MEDIA_SUBDRV_AUTOSELECT
141 select MEDIA_TUNER_MXL5005S if MEDIA_SUBDRV_AUTOSELECT
142 select MEDIA_TUNER_QT1010 if MEDIA_SUBDRV_AUTOSELECT
140 select MEDIA_TUNER_R820T if MEDIA_SUBDRV_AUTOSELECT 143 select MEDIA_TUNER_R820T if MEDIA_SUBDRV_AUTOSELECT
144 select MEDIA_TUNER_SI2157 if MEDIA_SUBDRV_AUTOSELECT
145 select MEDIA_TUNER_TUA9001 if MEDIA_SUBDRV_AUTOSELECT
141 help 146 help
142 Say Y here to support the Realtek RTL28xxU DVB USB receiver. 147 Say Y here to support the Realtek RTL28xxU DVB USB receiver.
143 148
diff --git a/drivers/media/usb/dvb-usb-v2/af9035.c b/drivers/media/usb/dvb-usb-v2/af9035.c
index 2638e3251f2a..ca018cd3fcd4 100644
--- a/drivers/media/usb/dvb-usb-v2/af9035.c
+++ b/drivers/media/usb/dvb-usb-v2/af9035.c
@@ -49,6 +49,7 @@ static int af9035_ctrl_msg(struct dvb_usb_device *d, struct usb_req *req)
49#define CHECKSUM_LEN 2 49#define CHECKSUM_LEN 2
50#define USB_TIMEOUT 2000 50#define USB_TIMEOUT 2000
51 struct state *state = d_to_priv(d); 51 struct state *state = d_to_priv(d);
52 struct usb_interface *intf = d->intf;
52 int ret, wlen, rlen; 53 int ret, wlen, rlen;
53 u16 checksum, tmp_checksum; 54 u16 checksum, tmp_checksum;
54 55
@@ -57,8 +58,8 @@ static int af9035_ctrl_msg(struct dvb_usb_device *d, struct usb_req *req)
57 /* buffer overflow check */ 58 /* buffer overflow check */
58 if (req->wlen > (BUF_LEN - REQ_HDR_LEN - CHECKSUM_LEN) || 59 if (req->wlen > (BUF_LEN - REQ_HDR_LEN - CHECKSUM_LEN) ||
59 req->rlen > (BUF_LEN - ACK_HDR_LEN - CHECKSUM_LEN)) { 60 req->rlen > (BUF_LEN - ACK_HDR_LEN - CHECKSUM_LEN)) {
60 dev_err(&d->udev->dev, "%s: too much data wlen=%d rlen=%d\n", 61 dev_err(&intf->dev, "too much data wlen=%d rlen=%d\n",
61 KBUILD_MODNAME, req->wlen, req->rlen); 62 req->wlen, req->rlen);
62 ret = -EINVAL; 63 ret = -EINVAL;
63 goto exit; 64 goto exit;
64 } 65 }
@@ -94,10 +95,8 @@ static int af9035_ctrl_msg(struct dvb_usb_device *d, struct usb_req *req)
94 checksum = af9035_checksum(state->buf, rlen - 2); 95 checksum = af9035_checksum(state->buf, rlen - 2);
95 tmp_checksum = (state->buf[rlen - 2] << 8) | state->buf[rlen - 1]; 96 tmp_checksum = (state->buf[rlen - 2] << 8) | state->buf[rlen - 1];
96 if (tmp_checksum != checksum) { 97 if (tmp_checksum != checksum) {
97 dev_err(&d->udev->dev, 98 dev_err(&intf->dev, "command=%02x checksum mismatch (%04x != %04x)\n",
98 "%s: command=%02x checksum mismatch (%04x != %04x)\n", 99 req->cmd, tmp_checksum, checksum);
99 KBUILD_MODNAME, req->cmd, tmp_checksum,
100 checksum);
101 ret = -EIO; 100 ret = -EIO;
102 goto exit; 101 goto exit;
103 } 102 }
@@ -110,8 +109,8 @@ static int af9035_ctrl_msg(struct dvb_usb_device *d, struct usb_req *req)
110 goto exit; 109 goto exit;
111 } 110 }
112 111
113 dev_dbg(&d->udev->dev, "%s: command=%02x failed fw error=%d\n", 112 dev_dbg(&intf->dev, "command=%02x failed fw error=%d\n",
114 __func__, req->cmd, state->buf[2]); 113 req->cmd, state->buf[2]);
115 ret = -EIO; 114 ret = -EIO;
116 goto exit; 115 goto exit;
117 } 116 }
@@ -122,20 +121,20 @@ static int af9035_ctrl_msg(struct dvb_usb_device *d, struct usb_req *req)
122exit: 121exit:
123 mutex_unlock(&d->usb_mutex); 122 mutex_unlock(&d->usb_mutex);
124 if (ret < 0) 123 if (ret < 0)
125 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret); 124 dev_dbg(&intf->dev, "failed=%d\n", ret);
126 return ret; 125 return ret;
127} 126}
128 127
129/* write multiple registers */ 128/* write multiple registers */
130static int af9035_wr_regs(struct dvb_usb_device *d, u32 reg, u8 *val, int len) 129static int af9035_wr_regs(struct dvb_usb_device *d, u32 reg, u8 *val, int len)
131{ 130{
131 struct usb_interface *intf = d->intf;
132 u8 wbuf[MAX_XFER_SIZE]; 132 u8 wbuf[MAX_XFER_SIZE];
133 u8 mbox = (reg >> 16) & 0xff; 133 u8 mbox = (reg >> 16) & 0xff;
134 struct usb_req req = { CMD_MEM_WR, mbox, 6 + len, wbuf, 0, NULL }; 134 struct usb_req req = { CMD_MEM_WR, mbox, 6 + len, wbuf, 0, NULL };
135 135
136 if (6 + len > sizeof(wbuf)) { 136 if (6 + len > sizeof(wbuf)) {
137 dev_warn(&d->udev->dev, "%s: i2c wr: len=%d is too big!\n", 137 dev_warn(&intf->dev, "i2c wr: len=%d is too big!\n", len);
138 KBUILD_MODNAME, len);
139 return -EOPNOTSUPP; 138 return -EOPNOTSUPP;
140 } 139 }
141 140
@@ -198,6 +197,7 @@ static int af9035_add_i2c_dev(struct dvb_usb_device *d, const char *type,
198{ 197{
199 int ret, num; 198 int ret, num;
200 struct state *state = d_to_priv(d); 199 struct state *state = d_to_priv(d);
200 struct usb_interface *intf = d->intf;
201 struct i2c_client *client; 201 struct i2c_client *client;
202 struct i2c_board_info board_info = { 202 struct i2c_board_info board_info = {
203 .addr = addr, 203 .addr = addr,
@@ -212,11 +212,10 @@ static int af9035_add_i2c_dev(struct dvb_usb_device *d, const char *type,
212 break; 212 break;
213 } 213 }
214 214
215 dev_dbg(&d->udev->dev, "%s: num=%d\n", __func__, num); 215 dev_dbg(&intf->dev, "num=%d\n", num);
216 216
217 if (num == AF9035_I2C_CLIENT_MAX) { 217 if (num == AF9035_I2C_CLIENT_MAX) {
218 dev_err(&d->udev->dev, "%s: I2C client out of index\n", 218 dev_err(&intf->dev, "I2C client out of index\n");
219 KBUILD_MODNAME);
220 ret = -ENODEV; 219 ret = -ENODEV;
221 goto err; 220 goto err;
222 } 221 }
@@ -240,7 +239,7 @@ static int af9035_add_i2c_dev(struct dvb_usb_device *d, const char *type,
240 state->i2c_client[num] = client; 239 state->i2c_client[num] = client;
241 return 0; 240 return 0;
242err: 241err:
243 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret); 242 dev_dbg(&intf->dev, "failed=%d\n", ret);
244 return ret; 243 return ret;
245} 244}
246 245
@@ -248,6 +247,7 @@ static void af9035_del_i2c_dev(struct dvb_usb_device *d)
248{ 247{
249 int num; 248 int num;
250 struct state *state = d_to_priv(d); 249 struct state *state = d_to_priv(d);
250 struct usb_interface *intf = d->intf;
251 struct i2c_client *client; 251 struct i2c_client *client;
252 252
253 /* find last used client */ 253 /* find last used client */
@@ -257,11 +257,10 @@ static void af9035_del_i2c_dev(struct dvb_usb_device *d)
257 break; 257 break;
258 } 258 }
259 259
260 dev_dbg(&d->udev->dev, "%s: num=%d\n", __func__, num); 260 dev_dbg(&intf->dev, "num=%d\n", num);
261 261
262 if (num == -1) { 262 if (num == -1) {
263 dev_err(&d->udev->dev, "%s: I2C client out of index\n", 263 dev_err(&intf->dev, "I2C client out of index\n");
264 KBUILD_MODNAME);
265 goto err; 264 goto err;
266 } 265 }
267 266
@@ -276,7 +275,7 @@ static void af9035_del_i2c_dev(struct dvb_usb_device *d)
276 state->i2c_client[num] = NULL; 275 state->i2c_client[num] = NULL;
277 return; 276 return;
278err: 277err:
279 dev_dbg(&d->udev->dev, "%s: failed\n", __func__); 278 dev_dbg(&intf->dev, "failed\n");
280} 279}
281 280
282static int af9035_i2c_master_xfer(struct i2c_adapter *adap, 281static int af9035_i2c_master_xfer(struct i2c_adapter *adap,
@@ -348,6 +347,9 @@ static int af9035_i2c_master_xfer(struct i2c_adapter *adap,
348 347
349 ret = af9035_rd_regs(d, reg, &msg[1].buf[0], 348 ret = af9035_rd_regs(d, reg, &msg[1].buf[0],
350 msg[1].len); 349 msg[1].len);
350 } else if (state->no_read) {
351 memset(msg[1].buf, 0, msg[1].len);
352 ret = 0;
351 } else { 353 } else {
352 /* I2C write + read */ 354 /* I2C write + read */
353 u8 buf[MAX_XFER_SIZE]; 355 u8 buf[MAX_XFER_SIZE];
@@ -367,10 +369,25 @@ static int af9035_i2c_master_xfer(struct i2c_adapter *adap,
367 memcpy(&buf[3], msg[0].buf, msg[0].len); 369 memcpy(&buf[3], msg[0].buf, msg[0].len);
368 } else { 370 } else {
369 buf[1] = msg[0].addr << 1; 371 buf[1] = msg[0].addr << 1;
370 buf[2] = 0x00; /* reg addr len */
371 buf[3] = 0x00; /* reg addr MSB */ 372 buf[3] = 0x00; /* reg addr MSB */
372 buf[4] = 0x00; /* reg addr LSB */ 373 buf[4] = 0x00; /* reg addr LSB */
373 memcpy(&buf[5], msg[0].buf, msg[0].len); 374
375 /* Keep prev behavior for write req len > 2*/
376 if (msg[0].len > 2) {
377 buf[2] = 0x00; /* reg addr len */
378 memcpy(&buf[5], msg[0].buf, msg[0].len);
379
380 /* Use reg addr fields if write req len <= 2 */
381 } else {
382 req.wlen = 5;
383 buf[2] = msg[0].len;
384 if (msg[0].len == 2) {
385 buf[3] = msg[0].buf[0];
386 buf[4] = msg[0].buf[1];
387 } else if (msg[0].len == 1) {
388 buf[4] = msg[0].buf[0];
389 }
390 }
374 } 391 }
375 ret = af9035_ctrl_msg(d, &req); 392 ret = af9035_ctrl_msg(d, &req);
376 } 393 }
@@ -421,6 +438,9 @@ static int af9035_i2c_master_xfer(struct i2c_adapter *adap,
421 if (msg[0].len > 40) { 438 if (msg[0].len > 40) {
422 /* TODO: correct limits > 40 */ 439 /* TODO: correct limits > 40 */
423 ret = -EOPNOTSUPP; 440 ret = -EOPNOTSUPP;
441 } else if (state->no_read) {
442 memset(msg[0].buf, 0, msg[0].len);
443 ret = 0;
424 } else { 444 } else {
425 /* I2C read */ 445 /* I2C read */
426 u8 buf[5]; 446 u8 buf[5];
@@ -475,7 +495,9 @@ static struct i2c_algorithm af9035_i2c_algo = {
475static int af9035_identify_state(struct dvb_usb_device *d, const char **name) 495static int af9035_identify_state(struct dvb_usb_device *d, const char **name)
476{ 496{
477 struct state *state = d_to_priv(d); 497 struct state *state = d_to_priv(d);
478 int ret; 498 struct usb_interface *intf = d->intf;
499 int ret, ts_mode_invalid;
500 u8 tmp;
479 u8 wbuf[1] = { 1 }; 501 u8 wbuf[1] = { 1 };
480 u8 rbuf[4]; 502 u8 rbuf[4];
481 struct usb_req req = { CMD_FW_QUERYINFO, 0, sizeof(wbuf), wbuf, 503 struct usb_req req = { CMD_FW_QUERYINFO, 0, sizeof(wbuf), wbuf,
@@ -492,10 +514,8 @@ static int af9035_identify_state(struct dvb_usb_device *d, const char **name)
492 if (ret < 0) 514 if (ret < 0)
493 goto err; 515 goto err;
494 516
495 dev_info(&d->udev->dev, 517 dev_info(&intf->dev, "prechip_version=%02x chip_version=%02x chip_type=%04x\n",
496 "%s: prechip_version=%02x chip_version=%02x chip_type=%04x\n", 518 state->prechip_version, state->chip_version, state->chip_type);
497 KBUILD_MODNAME, state->prechip_version,
498 state->chip_version, state->chip_type);
499 519
500 if (state->chip_type == 0x9135) { 520 if (state->chip_type == 0x9135) {
501 if (state->chip_version == 0x02) 521 if (state->chip_version == 0x02)
@@ -511,11 +531,41 @@ static int af9035_identify_state(struct dvb_usb_device *d, const char **name)
511 state->eeprom_addr = EEPROM_BASE_AF9035; 531 state->eeprom_addr = EEPROM_BASE_AF9035;
512 } 532 }
513 533
534
535 /* check for dual tuner mode */
536 ret = af9035_rd_reg(d, state->eeprom_addr + EEPROM_TS_MODE, &tmp);
537 if (ret < 0)
538 goto err;
539
540 ts_mode_invalid = 0;
541 switch (tmp) {
542 case 0:
543 break;
544 case 1:
545 case 3:
546 state->dual_mode = true;
547 break;
548 case 5:
549 if (state->chip_type != 0x9135 && state->chip_type != 0x9306)
550 state->dual_mode = true; /* AF9035 */
551 else
552 ts_mode_invalid = 1;
553 break;
554 default:
555 ts_mode_invalid = 1;
556 }
557
558 dev_dbg(&intf->dev, "ts mode=%d dual mode=%d\n", tmp, state->dual_mode);
559
560 if (ts_mode_invalid)
561 dev_info(&intf->dev, "ts mode=%d not supported, defaulting to single tuner mode!", tmp);
562
563
514 ret = af9035_ctrl_msg(d, &req); 564 ret = af9035_ctrl_msg(d, &req);
515 if (ret < 0) 565 if (ret < 0)
516 goto err; 566 goto err;
517 567
518 dev_dbg(&d->udev->dev, "%s: reply=%*ph\n", __func__, 4, rbuf); 568 dev_dbg(&intf->dev, "reply=%*ph\n", 4, rbuf);
519 if (rbuf[0] || rbuf[1] || rbuf[2] || rbuf[3]) 569 if (rbuf[0] || rbuf[1] || rbuf[2] || rbuf[3])
520 ret = WARM; 570 ret = WARM;
521 else 571 else
@@ -524,7 +574,7 @@ static int af9035_identify_state(struct dvb_usb_device *d, const char **name)
524 return ret; 574 return ret;
525 575
526err: 576err:
527 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret); 577 dev_dbg(&intf->dev, "failed=%d\n", ret);
528 578
529 return ret; 579 return ret;
530} 580}
@@ -532,6 +582,7 @@ err:
532static int af9035_download_firmware_old(struct dvb_usb_device *d, 582static int af9035_download_firmware_old(struct dvb_usb_device *d,
533 const struct firmware *fw) 583 const struct firmware *fw)
534{ 584{
585 struct usb_interface *intf = d->intf;
535 int ret, i, j, len; 586 int ret, i, j, len;
536 u8 wbuf[1]; 587 u8 wbuf[1];
537 struct usb_req req = { 0, 0, 0, NULL, 0, NULL }; 588 struct usb_req req = { 0, 0, 0, NULL, 0, NULL };
@@ -562,14 +613,12 @@ static int af9035_download_firmware_old(struct dvb_usb_device *d,
562 hdr_checksum = fw->data[fw->size - i + 5] << 8; 613 hdr_checksum = fw->data[fw->size - i + 5] << 8;
563 hdr_checksum |= fw->data[fw->size - i + 6] << 0; 614 hdr_checksum |= fw->data[fw->size - i + 6] << 0;
564 615
565 dev_dbg(&d->udev->dev, 616 dev_dbg(&intf->dev, "core=%d addr=%04x data_len=%d checksum=%04x\n",
566 "%s: core=%d addr=%04x data_len=%d checksum=%04x\n", 617 hdr_core, hdr_addr, hdr_data_len, hdr_checksum);
567 __func__, hdr_core, hdr_addr, hdr_data_len,
568 hdr_checksum);
569 618
570 if (((hdr_core != 1) && (hdr_core != 2)) || 619 if (((hdr_core != 1) && (hdr_core != 2)) ||
571 (hdr_data_len > i)) { 620 (hdr_data_len > i)) {
572 dev_dbg(&d->udev->dev, "%s: bad firmware\n", __func__); 621 dev_dbg(&intf->dev, "bad firmware\n");
573 break; 622 break;
574 } 623 }
575 624
@@ -600,18 +649,17 @@ static int af9035_download_firmware_old(struct dvb_usb_device *d,
600 649
601 i -= hdr_data_len + HDR_SIZE; 650 i -= hdr_data_len + HDR_SIZE;
602 651
603 dev_dbg(&d->udev->dev, "%s: data uploaded=%zu\n", 652 dev_dbg(&intf->dev, "data uploaded=%zu\n", fw->size - i);
604 __func__, fw->size - i);
605 } 653 }
606 654
607 /* print warn if firmware is bad, continue and see what happens */ 655 /* print warn if firmware is bad, continue and see what happens */
608 if (i) 656 if (i)
609 dev_warn(&d->udev->dev, "%s: bad firmware\n", KBUILD_MODNAME); 657 dev_warn(&intf->dev, "bad firmware\n");
610 658
611 return 0; 659 return 0;
612 660
613err: 661err:
614 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret); 662 dev_dbg(&intf->dev, "failed=%d\n", ret);
615 663
616 return ret; 664 return ret;
617} 665}
@@ -619,6 +667,7 @@ err:
619static int af9035_download_firmware_new(struct dvb_usb_device *d, 667static int af9035_download_firmware_new(struct dvb_usb_device *d,
620 const struct firmware *fw) 668 const struct firmware *fw)
621{ 669{
670 struct usb_interface *intf = d->intf;
622 int ret, i, i_prev; 671 int ret, i, i_prev;
623 struct usb_req req_fw_dl = { CMD_FW_SCATTER_WR, 0, 0, NULL, 0, NULL }; 672 struct usb_req req_fw_dl = { CMD_FW_SCATTER_WR, 0, 0, NULL, 0, NULL };
624 #define HDR_SIZE 7 673 #define HDR_SIZE 7
@@ -648,15 +697,14 @@ static int af9035_download_firmware_new(struct dvb_usb_device *d,
648 if (ret < 0) 697 if (ret < 0)
649 goto err; 698 goto err;
650 699
651 dev_dbg(&d->udev->dev, "%s: data uploaded=%d\n", 700 dev_dbg(&intf->dev, "data uploaded=%d\n", i);
652 __func__, i);
653 } 701 }
654 } 702 }
655 703
656 return 0; 704 return 0;
657 705
658err: 706err:
659 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret); 707 dev_dbg(&intf->dev, "failed=%d\n", ret);
660 708
661 return ret; 709 return ret;
662} 710}
@@ -664,6 +712,7 @@ err:
664static int af9035_download_firmware(struct dvb_usb_device *d, 712static int af9035_download_firmware(struct dvb_usb_device *d,
665 const struct firmware *fw) 713 const struct firmware *fw)
666{ 714{
715 struct usb_interface *intf = d->intf;
667 struct state *state = d_to_priv(d); 716 struct state *state = d_to_priv(d);
668 int ret; 717 int ret;
669 u8 wbuf[1]; 718 u8 wbuf[1];
@@ -672,7 +721,7 @@ static int af9035_download_firmware(struct dvb_usb_device *d,
672 struct usb_req req = { 0, 0, 0, NULL, 0, NULL }; 721 struct usb_req req = { 0, 0, 0, NULL, 0, NULL };
673 struct usb_req req_fw_ver = { CMD_FW_QUERYINFO, 0, 1, wbuf, 4, rbuf }; 722 struct usb_req req_fw_ver = { CMD_FW_QUERYINFO, 0, 1, wbuf, 4, rbuf };
674 723
675 dev_dbg(&d->udev->dev, "%s:\n", __func__); 724 dev_dbg(&intf->dev, "\n");
676 725
677 /* 726 /*
678 * In case of dual tuner configuration we need to do some extra 727 * In case of dual tuner configuration we need to do some extra
@@ -680,11 +729,7 @@ static int af9035_download_firmware(struct dvb_usb_device *d,
680 * which is done by master demod. 729 * which is done by master demod.
681 * Master feeds also clock and controls power via GPIO. 730 * Master feeds also clock and controls power via GPIO.
682 */ 731 */
683 ret = af9035_rd_reg(d, state->eeprom_addr + EEPROM_TS_MODE, &tmp); 732 if (state->dual_mode) {
684 if (ret < 0)
685 goto err;
686
687 if (tmp == 1 || tmp == 3 || tmp == 5) {
688 /* configure gpioh1, reset & power slave demod */ 733 /* configure gpioh1, reset & power slave demod */
689 ret = af9035_wr_reg_mask(d, 0x00d8b0, 0x01, 0x01); 734 ret = af9035_wr_reg_mask(d, 0x00d8b0, 0x01, 0x01);
690 if (ret < 0) 735 if (ret < 0)
@@ -752,25 +797,25 @@ static int af9035_download_firmware(struct dvb_usb_device *d,
752 goto err; 797 goto err;
753 798
754 if (!(rbuf[0] || rbuf[1] || rbuf[2] || rbuf[3])) { 799 if (!(rbuf[0] || rbuf[1] || rbuf[2] || rbuf[3])) {
755 dev_err(&d->udev->dev, "%s: firmware did not run\n", 800 dev_err(&intf->dev, "firmware did not run\n");
756 KBUILD_MODNAME);
757 ret = -ENODEV; 801 ret = -ENODEV;
758 goto err; 802 goto err;
759 } 803 }
760 804
761 dev_info(&d->udev->dev, "%s: firmware version=%d.%d.%d.%d", 805 dev_info(&intf->dev, "firmware version=%d.%d.%d.%d",
762 KBUILD_MODNAME, rbuf[0], rbuf[1], rbuf[2], rbuf[3]); 806 rbuf[0], rbuf[1], rbuf[2], rbuf[3]);
763 807
764 return 0; 808 return 0;
765 809
766err: 810err:
767 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret); 811 dev_dbg(&intf->dev, "failed=%d\n", ret);
768 812
769 return ret; 813 return ret;
770} 814}
771 815
772static int af9035_read_config(struct dvb_usb_device *d) 816static int af9035_read_config(struct dvb_usb_device *d)
773{ 817{
818 struct usb_interface *intf = d->intf;
774 struct state *state = d_to_priv(d); 819 struct state *state = d_to_priv(d);
775 int ret, i; 820 int ret, i;
776 u8 tmp; 821 u8 tmp;
@@ -805,7 +850,7 @@ static int af9035_read_config(struct dvb_usb_device *d)
805 goto err; 850 goto err;
806 851
807 if (tmp == 0x00) { 852 if (tmp == 0x00) {
808 dev_dbg(&d->udev->dev, "%s: no eeprom\n", __func__); 853 dev_dbg(&intf->dev, "no eeprom\n");
809 goto skip_eeprom; 854 goto skip_eeprom;
810 } 855 }
811 } else if (state->chip_type == 0x9306) { 856 } else if (state->chip_type == 0x9306) {
@@ -817,18 +862,6 @@ static int af9035_read_config(struct dvb_usb_device *d)
817 } 862 }
818 863
819 864
820
821 /* check if there is dual tuners */
822 ret = af9035_rd_reg(d, state->eeprom_addr + EEPROM_TS_MODE, &tmp);
823 if (ret < 0)
824 goto err;
825
826 if (tmp == 1 || tmp == 3 || tmp == 5)
827 state->dual_mode = true;
828
829 dev_dbg(&d->udev->dev, "%s: ts mode=%d dual mode=%d\n", __func__,
830 tmp, state->dual_mode);
831
832 if (state->dual_mode) { 865 if (state->dual_mode) {
833 /* read 2nd demodulator I2C address */ 866 /* read 2nd demodulator I2C address */
834 ret = af9035_rd_reg(d, 867 ret = af9035_rd_reg(d,
@@ -840,8 +873,7 @@ static int af9035_read_config(struct dvb_usb_device *d)
840 if (tmp) 873 if (tmp)
841 state->af9033_i2c_addr[1] = tmp; 874 state->af9033_i2c_addr[1] = tmp;
842 875
843 dev_dbg(&d->udev->dev, "%s: 2nd demod I2C addr=%02x\n", 876 dev_dbg(&intf->dev, "2nd demod I2C addr=%02x\n", tmp);
844 __func__, tmp);
845 } 877 }
846 878
847 addr = state->eeprom_addr; 879 addr = state->eeprom_addr;
@@ -852,8 +884,7 @@ static int af9035_read_config(struct dvb_usb_device *d)
852 if (ret < 0) 884 if (ret < 0)
853 goto err; 885 goto err;
854 886
855 dev_dbg(&d->udev->dev, "%s: [%d]tuner=%02x\n", 887 dev_dbg(&intf->dev, "[%d]tuner=%02x\n", i, tmp);
856 __func__, i, tmp);
857 888
858 /* tuner sanity check */ 889 /* tuner sanity check */
859 if (state->chip_type == 0x9135) { 890 if (state->chip_type == 0x9135) {
@@ -882,10 +913,8 @@ static int af9035_read_config(struct dvb_usb_device *d)
882 } 913 }
883 914
884 if (state->af9033_config[i].tuner != tmp) { 915 if (state->af9033_config[i].tuner != tmp) {
885 dev_info(&d->udev->dev, 916 dev_info(&intf->dev, "[%d] overriding tuner from %02x to %02x\n",
886 "%s: [%d] overriding tuner from %02x to %02x\n", 917 i, tmp, state->af9033_config[i].tuner);
887 KBUILD_MODNAME, i, tmp,
888 state->af9033_config[i].tuner);
889 } 918 }
890 919
891 switch (state->af9033_config[i].tuner) { 920 switch (state->af9033_config[i].tuner) {
@@ -905,9 +934,8 @@ static int af9035_read_config(struct dvb_usb_device *d)
905 case AF9033_TUNER_IT9135_62: 934 case AF9033_TUNER_IT9135_62:
906 break; 935 break;
907 default: 936 default:
908 dev_warn(&d->udev->dev, 937 dev_warn(&intf->dev, "tuner id=%02x not supported, please report!",
909 "%s: tuner id=%02x not supported, please report!", 938 tmp);
910 KBUILD_MODNAME, tmp);
911 } 939 }
912 940
913 /* disable dual mode if driver does not support it */ 941 /* disable dual mode if driver does not support it */
@@ -924,9 +952,7 @@ static int af9035_read_config(struct dvb_usb_device *d)
924 break; 952 break;
925 default: 953 default:
926 state->dual_mode = false; 954 state->dual_mode = false;
927 dev_info(&d->udev->dev, 955 dev_info(&intf->dev, "driver does not support 2nd tuner and will disable it");
928 "%s: driver does not support 2nd tuner and will disable it",
929 KBUILD_MODNAME);
930 } 956 }
931 957
932 /* tuner IF frequency */ 958 /* tuner IF frequency */
@@ -942,7 +968,7 @@ static int af9035_read_config(struct dvb_usb_device *d)
942 968
943 tmp16 |= tmp << 8; 969 tmp16 |= tmp << 8;
944 970
945 dev_dbg(&d->udev->dev, "%s: [%d]IF=%d\n", __func__, i, tmp16); 971 dev_dbg(&intf->dev, "[%d]IF=%d\n", i, tmp16);
946 972
947 addr += 0x10; /* shift for the 2nd tuner params */ 973 addr += 0x10; /* shift for the 2nd tuner params */
948 } 974 }
@@ -962,10 +988,24 @@ skip_eeprom:
962 state->af9033_config[i].clock = clock_lut_af9035[tmp]; 988 state->af9033_config[i].clock = clock_lut_af9035[tmp];
963 } 989 }
964 990
991 state->no_read = false;
992 /* Some MXL5007T devices cannot properly handle tuner I2C read ops. */
993 if (state->af9033_config[0].tuner == AF9033_TUNER_MXL5007T &&
994 le16_to_cpu(d->udev->descriptor.idVendor) == USB_VID_AVERMEDIA)
995
996 switch (le16_to_cpu(d->udev->descriptor.idProduct)) {
997 case USB_PID_AVERMEDIA_A867:
998 case USB_PID_AVERMEDIA_TWINSTAR:
999 dev_info(&intf->dev,
1000 "Device may have issues with I2C read operations. Enabling fix.\n");
1001 state->no_read = true;
1002 break;
1003 }
1004
965 return 0; 1005 return 0;
966 1006
967err: 1007err:
968 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret); 1008 dev_dbg(&intf->dev, "failed=%d\n", ret);
969 1009
970 return ret; 1010 return ret;
971} 1011}
@@ -973,10 +1013,11 @@ err:
973static int af9035_tua9001_tuner_callback(struct dvb_usb_device *d, 1013static int af9035_tua9001_tuner_callback(struct dvb_usb_device *d,
974 int cmd, int arg) 1014 int cmd, int arg)
975{ 1015{
1016 struct usb_interface *intf = d->intf;
976 int ret; 1017 int ret;
977 u8 val; 1018 u8 val;
978 1019
979 dev_dbg(&d->udev->dev, "%s: cmd=%d arg=%d\n", __func__, cmd, arg); 1020 dev_dbg(&intf->dev, "cmd=%d arg=%d\n", cmd, arg);
980 1021
981 /* 1022 /*
982 * CEN always enabled by hardware wiring 1023 * CEN always enabled by hardware wiring
@@ -1010,7 +1051,7 @@ static int af9035_tua9001_tuner_callback(struct dvb_usb_device *d,
1010 return 0; 1051 return 0;
1011 1052
1012err: 1053err:
1013 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret); 1054 dev_dbg(&intf->dev, "failed=%d\n", ret);
1014 1055
1015 return ret; 1056 return ret;
1016} 1057}
@@ -1019,6 +1060,7 @@ err:
1019static int af9035_fc0011_tuner_callback(struct dvb_usb_device *d, 1060static int af9035_fc0011_tuner_callback(struct dvb_usb_device *d,
1020 int cmd, int arg) 1061 int cmd, int arg)
1021{ 1062{
1063 struct usb_interface *intf = d->intf;
1022 int ret; 1064 int ret;
1023 1065
1024 switch (cmd) { 1066 switch (cmd) {
@@ -1076,7 +1118,7 @@ static int af9035_fc0011_tuner_callback(struct dvb_usb_device *d,
1076 return 0; 1118 return 0;
1077 1119
1078err: 1120err:
1079 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret); 1121 dev_dbg(&intf->dev, "failed=%d\n", ret);
1080 1122
1081 return ret; 1123 return ret;
1082} 1124}
@@ -1102,9 +1144,10 @@ static int af9035_frontend_callback(void *adapter_priv, int component,
1102{ 1144{
1103 struct i2c_adapter *adap = adapter_priv; 1145 struct i2c_adapter *adap = adapter_priv;
1104 struct dvb_usb_device *d = i2c_get_adapdata(adap); 1146 struct dvb_usb_device *d = i2c_get_adapdata(adap);
1147 struct usb_interface *intf = d->intf;
1105 1148
1106 dev_dbg(&d->udev->dev, "%s: component=%d cmd=%d arg=%d\n", 1149 dev_dbg(&intf->dev, "component=%d cmd=%d arg=%d\n",
1107 __func__, component, cmd, arg); 1150 component, cmd, arg);
1108 1151
1109 switch (component) { 1152 switch (component) {
1110 case DVB_FRONTEND_COMPONENT_TUNER: 1153 case DVB_FRONTEND_COMPONENT_TUNER:
@@ -1127,9 +1170,10 @@ static int af9035_frontend_attach(struct dvb_usb_adapter *adap)
1127{ 1170{
1128 struct state *state = adap_to_priv(adap); 1171 struct state *state = adap_to_priv(adap);
1129 struct dvb_usb_device *d = adap_to_d(adap); 1172 struct dvb_usb_device *d = adap_to_d(adap);
1173 struct usb_interface *intf = d->intf;
1130 int ret; 1174 int ret;
1131 1175
1132 dev_dbg(&d->udev->dev, "%s: adap->id=%d\n", __func__, adap->id); 1176 dev_dbg(&intf->dev, "adap->id=%d\n", adap->id);
1133 1177
1134 if (!state->af9033_config[adap->id].tuner) { 1178 if (!state->af9033_config[adap->id].tuner) {
1135 /* unsupported tuner */ 1179 /* unsupported tuner */
@@ -1156,7 +1200,7 @@ static int af9035_frontend_attach(struct dvb_usb_adapter *adap)
1156 return 0; 1200 return 0;
1157 1201
1158err: 1202err:
1159 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret); 1203 dev_dbg(&intf->dev, "failed=%d\n", ret);
1160 1204
1161 return ret; 1205 return ret;
1162} 1206}
@@ -1165,11 +1209,12 @@ static int it930x_frontend_attach(struct dvb_usb_adapter *adap)
1165{ 1209{
1166 struct state *state = adap_to_priv(adap); 1210 struct state *state = adap_to_priv(adap);
1167 struct dvb_usb_device *d = adap_to_d(adap); 1211 struct dvb_usb_device *d = adap_to_d(adap);
1212 struct usb_interface *intf = d->intf;
1168 int ret; 1213 int ret;
1169 struct si2168_config si2168_config; 1214 struct si2168_config si2168_config;
1170 struct i2c_adapter *adapter; 1215 struct i2c_adapter *adapter;
1171 1216
1172 dev_dbg(&d->udev->dev, "adap->id=%d\n", adap->id); 1217 dev_dbg(&intf->dev, "adap->id=%d\n", adap->id);
1173 1218
1174 memset(&si2168_config, 0, sizeof(si2168_config)); 1219 memset(&si2168_config, 0, sizeof(si2168_config));
1175 si2168_config.i2c_adapter = &adapter; 1220 si2168_config.i2c_adapter = &adapter;
@@ -1192,7 +1237,7 @@ static int it930x_frontend_attach(struct dvb_usb_adapter *adap)
1192 return 0; 1237 return 0;
1193 1238
1194err: 1239err:
1195 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret); 1240 dev_dbg(&intf->dev, "failed=%d\n", ret);
1196 1241
1197 return ret; 1242 return ret;
1198} 1243}
@@ -1201,9 +1246,10 @@ static int af9035_frontend_detach(struct dvb_usb_adapter *adap)
1201{ 1246{
1202 struct state *state = adap_to_priv(adap); 1247 struct state *state = adap_to_priv(adap);
1203 struct dvb_usb_device *d = adap_to_d(adap); 1248 struct dvb_usb_device *d = adap_to_d(adap);
1249 struct usb_interface *intf = d->intf;
1204 int demod2; 1250 int demod2;
1205 1251
1206 dev_dbg(&d->udev->dev, "%s: adap->id=%d\n", __func__, adap->id); 1252 dev_dbg(&intf->dev, "adap->id=%d\n", adap->id);
1207 1253
1208 /* 1254 /*
1209 * For dual tuner devices we have to resolve 2nd demod client, as there 1255 * For dual tuner devices we have to resolve 2nd demod client, as there
@@ -1279,12 +1325,13 @@ static int af9035_tuner_attach(struct dvb_usb_adapter *adap)
1279{ 1325{
1280 struct state *state = adap_to_priv(adap); 1326 struct state *state = adap_to_priv(adap);
1281 struct dvb_usb_device *d = adap_to_d(adap); 1327 struct dvb_usb_device *d = adap_to_d(adap);
1328 struct usb_interface *intf = d->intf;
1282 int ret; 1329 int ret;
1283 struct dvb_frontend *fe; 1330 struct dvb_frontend *fe;
1284 struct i2c_msg msg[1]; 1331 struct i2c_msg msg[1];
1285 u8 tuner_addr; 1332 u8 tuner_addr;
1286 1333
1287 dev_dbg(&d->udev->dev, "%s: adap->id=%d\n", __func__, adap->id); 1334 dev_dbg(&intf->dev, "adap->id=%d\n", adap->id);
1288 1335
1289 /* 1336 /*
1290 * XXX: Hack used in that function: we abuse unused I2C address bit [7] 1337 * XXX: Hack used in that function: we abuse unused I2C address bit [7]
@@ -1522,7 +1569,7 @@ static int af9035_tuner_attach(struct dvb_usb_adapter *adap)
1522 return 0; 1569 return 0;
1523 1570
1524err: 1571err:
1525 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret); 1572 dev_dbg(&intf->dev, "failed=%d\n", ret);
1526 1573
1527 return ret; 1574 return ret;
1528} 1575}
@@ -1531,10 +1578,11 @@ static int it930x_tuner_attach(struct dvb_usb_adapter *adap)
1531{ 1578{
1532 struct state *state = adap_to_priv(adap); 1579 struct state *state = adap_to_priv(adap);
1533 struct dvb_usb_device *d = adap_to_d(adap); 1580 struct dvb_usb_device *d = adap_to_d(adap);
1581 struct usb_interface *intf = d->intf;
1534 int ret; 1582 int ret;
1535 struct si2157_config si2157_config; 1583 struct si2157_config si2157_config;
1536 1584
1537 dev_dbg(&d->udev->dev, "%s: adap->id=%d\n", __func__, adap->id); 1585 dev_dbg(&intf->dev, "adap->id=%d\n", adap->id);
1538 1586
1539 /* I2C master bus 2 clock speed 300k */ 1587 /* I2C master bus 2 clock speed 300k */
1540 ret = af9035_wr_reg(d, 0x00f6a7, 0x07); 1588 ret = af9035_wr_reg(d, 0x00f6a7, 0x07);
@@ -1590,7 +1638,7 @@ static int it930x_tuner_attach(struct dvb_usb_adapter *adap)
1590 return 0; 1638 return 0;
1591 1639
1592err: 1640err:
1593 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret); 1641 dev_dbg(&intf->dev, "failed=%d\n", ret);
1594 1642
1595 return ret; 1643 return ret;
1596} 1644}
@@ -1600,8 +1648,9 @@ static int it930x_tuner_detach(struct dvb_usb_adapter *adap)
1600{ 1648{
1601 struct state *state = adap_to_priv(adap); 1649 struct state *state = adap_to_priv(adap);
1602 struct dvb_usb_device *d = adap_to_d(adap); 1650 struct dvb_usb_device *d = adap_to_d(adap);
1651 struct usb_interface *intf = d->intf;
1603 1652
1604 dev_dbg(&d->udev->dev, "adap->id=%d\n", adap->id); 1653 dev_dbg(&intf->dev, "adap->id=%d\n", adap->id);
1605 1654
1606 if (adap->id == 1) { 1655 if (adap->id == 1) {
1607 if (state->i2c_client[3]) 1656 if (state->i2c_client[3])
@@ -1619,8 +1668,9 @@ static int af9035_tuner_detach(struct dvb_usb_adapter *adap)
1619{ 1668{
1620 struct state *state = adap_to_priv(adap); 1669 struct state *state = adap_to_priv(adap);
1621 struct dvb_usb_device *d = adap_to_d(adap); 1670 struct dvb_usb_device *d = adap_to_d(adap);
1671 struct usb_interface *intf = d->intf;
1622 1672
1623 dev_dbg(&d->udev->dev, "%s: adap->id=%d\n", __func__, adap->id); 1673 dev_dbg(&intf->dev, "adap->id=%d\n", adap->id);
1624 1674
1625 switch (state->af9033_config[adap->id].tuner) { 1675 switch (state->af9033_config[adap->id].tuner) {
1626 case AF9033_TUNER_TUA9001: 1676 case AF9033_TUNER_TUA9001:
@@ -1646,6 +1696,7 @@ static int af9035_tuner_detach(struct dvb_usb_adapter *adap)
1646static int af9035_init(struct dvb_usb_device *d) 1696static int af9035_init(struct dvb_usb_device *d)
1647{ 1697{
1648 struct state *state = d_to_priv(d); 1698 struct state *state = d_to_priv(d);
1699 struct usb_interface *intf = d->intf;
1649 int ret, i; 1700 int ret, i;
1650 u16 frame_size = (d->udev->speed == USB_SPEED_FULL ? 5 : 87) * 188 / 4; 1701 u16 frame_size = (d->udev->speed == USB_SPEED_FULL ? 5 : 87) * 188 / 4;
1651 u8 packet_size = (d->udev->speed == USB_SPEED_FULL ? 64 : 512) / 4; 1702 u8 packet_size = (d->udev->speed == USB_SPEED_FULL ? 64 : 512) / 4;
@@ -1670,9 +1721,8 @@ static int af9035_init(struct dvb_usb_device *d)
1670 { 0x80f9a4, 0x00, 0x01 }, 1721 { 0x80f9a4, 0x00, 0x01 },
1671 }; 1722 };
1672 1723
1673 dev_dbg(&d->udev->dev, 1724 dev_dbg(&intf->dev, "USB speed=%d frame_size=%04x packet_size=%02x\n",
1674 "%s: USB speed=%d frame_size=%04x packet_size=%02x\n", 1725 d->udev->speed, frame_size, packet_size);
1675 __func__, d->udev->speed, frame_size, packet_size);
1676 1726
1677 /* init endpoints */ 1727 /* init endpoints */
1678 for (i = 0; i < ARRAY_SIZE(tab); i++) { 1728 for (i = 0; i < ARRAY_SIZE(tab); i++) {
@@ -1685,7 +1735,7 @@ static int af9035_init(struct dvb_usb_device *d)
1685 return 0; 1735 return 0;
1686 1736
1687err: 1737err:
1688 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret); 1738 dev_dbg(&intf->dev, "failed=%d\n", ret);
1689 1739
1690 return ret; 1740 return ret;
1691} 1741}
@@ -1693,6 +1743,7 @@ err:
1693static int it930x_init(struct dvb_usb_device *d) 1743static int it930x_init(struct dvb_usb_device *d)
1694{ 1744{
1695 struct state *state = d_to_priv(d); 1745 struct state *state = d_to_priv(d);
1746 struct usb_interface *intf = d->intf;
1696 int ret, i; 1747 int ret, i;
1697 u16 frame_size = (d->udev->speed == USB_SPEED_FULL ? 5 : 816) * 188 / 4; 1748 u16 frame_size = (d->udev->speed == USB_SPEED_FULL ? 5 : 816) * 188 / 4;
1698 u8 packet_size = (d->udev->speed == USB_SPEED_FULL ? 64 : 512) / 4; 1749 u8 packet_size = (d->udev->speed == USB_SPEED_FULL ? 64 : 512) / 4;
@@ -1752,9 +1803,8 @@ static int it930x_init(struct dvb_usb_device *d)
1752 { 0x00da5a, 0x1f, 0xff }, /* ts_fail_ignore */ 1803 { 0x00da5a, 0x1f, 0xff }, /* ts_fail_ignore */
1753 }; 1804 };
1754 1805
1755 dev_dbg(&d->udev->dev, 1806 dev_dbg(&intf->dev, "USB speed=%d frame_size=%04x packet_size=%02x\n",
1756 "%s: USB speed=%d frame_size=%04x packet_size=%02x\n", 1807 d->udev->speed, frame_size, packet_size);
1757 __func__, d->udev->speed, frame_size, packet_size);
1758 1808
1759 /* init endpoints */ 1809 /* init endpoints */
1760 for (i = 0; i < ARRAY_SIZE(tab); i++) { 1810 for (i = 0; i < ARRAY_SIZE(tab); i++) {
@@ -1767,7 +1817,7 @@ static int it930x_init(struct dvb_usb_device *d)
1767 1817
1768 return 0; 1818 return 0;
1769err: 1819err:
1770 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret); 1820 dev_dbg(&intf->dev, "failed=%d\n", ret);
1771 1821
1772 return ret; 1822 return ret;
1773} 1823}
@@ -1776,6 +1826,7 @@ err:
1776#if IS_ENABLED(CONFIG_RC_CORE) 1826#if IS_ENABLED(CONFIG_RC_CORE)
1777static int af9035_rc_query(struct dvb_usb_device *d) 1827static int af9035_rc_query(struct dvb_usb_device *d)
1778{ 1828{
1829 struct usb_interface *intf = d->intf;
1779 int ret; 1830 int ret;
1780 u32 key; 1831 u32 key;
1781 u8 buf[4]; 1832 u8 buf[4];
@@ -1801,14 +1852,14 @@ static int af9035_rc_query(struct dvb_usb_device *d)
1801 buf[2] << 8 | buf[3]); 1852 buf[2] << 8 | buf[3]);
1802 } 1853 }
1803 1854
1804 dev_dbg(&d->udev->dev, "%s: %*ph\n", __func__, 4, buf); 1855 dev_dbg(&intf->dev, "%*ph\n", 4, buf);
1805 1856
1806 rc_keydown(d->rc_dev, RC_TYPE_NEC, key, 0); 1857 rc_keydown(d->rc_dev, RC_TYPE_NEC, key, 0);
1807 1858
1808 return 0; 1859 return 0;
1809 1860
1810err: 1861err:
1811 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret); 1862 dev_dbg(&intf->dev, "failed=%d\n", ret);
1812 1863
1813 return ret; 1864 return ret;
1814} 1865}
@@ -1816,6 +1867,7 @@ err:
1816static int af9035_get_rc_config(struct dvb_usb_device *d, struct dvb_usb_rc *rc) 1867static int af9035_get_rc_config(struct dvb_usb_device *d, struct dvb_usb_rc *rc)
1817{ 1868{
1818 struct state *state = d_to_priv(d); 1869 struct state *state = d_to_priv(d);
1870 struct usb_interface *intf = d->intf;
1819 int ret; 1871 int ret;
1820 u8 tmp; 1872 u8 tmp;
1821 1873
@@ -1823,7 +1875,7 @@ static int af9035_get_rc_config(struct dvb_usb_device *d, struct dvb_usb_rc *rc)
1823 if (ret < 0) 1875 if (ret < 0)
1824 goto err; 1876 goto err;
1825 1877
1826 dev_dbg(&d->udev->dev, "%s: ir_mode=%02x\n", __func__, tmp); 1878 dev_dbg(&intf->dev, "ir_mode=%02x\n", tmp);
1827 1879
1828 /* don't activate rc if in HID mode or if not available */ 1880 /* don't activate rc if in HID mode or if not available */
1829 if (tmp == 5) { 1881 if (tmp == 5) {
@@ -1832,7 +1884,7 @@ static int af9035_get_rc_config(struct dvb_usb_device *d, struct dvb_usb_rc *rc)
1832 if (ret < 0) 1884 if (ret < 0)
1833 goto err; 1885 goto err;
1834 1886
1835 dev_dbg(&d->udev->dev, "%s: ir_type=%02x\n", __func__, tmp); 1887 dev_dbg(&intf->dev, "ir_type=%02x\n", tmp);
1836 1888
1837 switch (tmp) { 1889 switch (tmp) {
1838 case 0: /* NEC */ 1890 case 0: /* NEC */
@@ -1855,7 +1907,7 @@ static int af9035_get_rc_config(struct dvb_usb_device *d, struct dvb_usb_rc *rc)
1855 return 0; 1907 return 0;
1856 1908
1857err: 1909err:
1858 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret); 1910 dev_dbg(&intf->dev, "failed=%d\n", ret);
1859 1911
1860 return ret; 1912 return ret;
1861} 1913}
@@ -1867,8 +1919,9 @@ static int af9035_get_stream_config(struct dvb_frontend *fe, u8 *ts_type,
1867 struct usb_data_stream_properties *stream) 1919 struct usb_data_stream_properties *stream)
1868{ 1920{
1869 struct dvb_usb_device *d = fe_to_d(fe); 1921 struct dvb_usb_device *d = fe_to_d(fe);
1922 struct usb_interface *intf = d->intf;
1870 1923
1871 dev_dbg(&d->udev->dev, "%s: adap=%d\n", __func__, fe_to_adap(fe)->id); 1924 dev_dbg(&intf->dev, "adap=%d\n", fe_to_adap(fe)->id);
1872 1925
1873 if (d->udev->speed == USB_SPEED_FULL) 1926 if (d->udev->speed == USB_SPEED_FULL)
1874 stream->u.bulk.buffersize = 5 * 188; 1927 stream->u.bulk.buffersize = 5 * 188;
@@ -1920,7 +1973,7 @@ static int af9035_probe(struct usb_interface *intf,
1920 if ((le16_to_cpu(udev->descriptor.idVendor) == USB_VID_TERRATEC) && 1973 if ((le16_to_cpu(udev->descriptor.idVendor) == USB_VID_TERRATEC) &&
1921 (le16_to_cpu(udev->descriptor.idProduct) == 0x0099)) { 1974 (le16_to_cpu(udev->descriptor.idProduct) == 0x0099)) {
1922 if (!strcmp("Afatech", manufacturer)) { 1975 if (!strcmp("Afatech", manufacturer)) {
1923 dev_dbg(&udev->dev, "%s: rejecting device\n", __func__); 1976 dev_dbg(&udev->dev, "rejecting device\n");
1924 return -ENODEV; 1977 return -ENODEV;
1925 } 1978 }
1926 } 1979 }
diff --git a/drivers/media/usb/dvb-usb-v2/af9035.h b/drivers/media/usb/dvb-usb-v2/af9035.h
index 89e629a24aec..1f83c9218ad0 100644
--- a/drivers/media/usb/dvb-usb-v2/af9035.h
+++ b/drivers/media/usb/dvb-usb-v2/af9035.h
@@ -62,6 +62,7 @@ struct state {
62 u8 chip_version; 62 u8 chip_version;
63 u16 chip_type; 63 u16 chip_type;
64 u8 dual_mode:1; 64 u8 dual_mode:1;
65 u8 no_read:1;
65 u16 eeprom_addr; 66 u16 eeprom_addr;
66 u8 af9033_i2c_addr[2]; 67 u8 af9033_i2c_addr[2];
67 struct af9033_config af9033_config[2]; 68 struct af9033_config af9033_config[2];
@@ -112,7 +113,7 @@ static const u32 clock_lut_it9135[] = {
112 * 0 TS 113 * 0 TS
113 * 1 DCA + PIP 114 * 1 DCA + PIP
114 * 3 PIP 115 * 3 PIP
115 * 5 DCA + PIP 116 * 5 DCA + PIP (AF9035 only)
116 * n DCA 117 * n DCA
117 * 118 *
118 * Values 0, 3 and 5 are seen to this day. 0 for single TS and 3/5 for dual TS. 119 * Values 0, 3 and 5 are seen to this day. 0 for single TS and 3/5 for dual TS.
diff --git a/drivers/media/usb/dvb-usb-v2/rtl28xxu.c b/drivers/media/usb/dvb-usb-v2/rtl28xxu.c
index eb7af8cb8aca..6643762a9ff7 100644
--- a/drivers/media/usb/dvb-usb-v2/rtl28xxu.c
+++ b/drivers/media/usb/dvb-usb-v2/rtl28xxu.c
@@ -624,7 +624,7 @@ static int rtl28xxu_identify_state(struct dvb_usb_device *d, const char **name)
624 dev_dbg(&d->intf->dev, "chip_id=%u\n", dev->chip_id); 624 dev_dbg(&d->intf->dev, "chip_id=%u\n", dev->chip_id);
625 625
626 /* Retry failed I2C messages */ 626 /* Retry failed I2C messages */
627 d->i2c_adap.retries = 1; 627 d->i2c_adap.retries = 3;
628 d->i2c_adap.timeout = msecs_to_jiffies(10); 628 d->i2c_adap.timeout = msecs_to_jiffies(10);
629 629
630 return WARM; 630 return WARM;
diff --git a/drivers/media/usb/dvb-usb/dtt200u.c b/drivers/media/usb/dvb-usb/dtt200u.c
index ca3b69aa9688..be633ece4194 100644
--- a/drivers/media/usb/dvb-usb/dtt200u.c
+++ b/drivers/media/usb/dvb-usb/dtt200u.c
@@ -55,36 +55,36 @@ static int dtt200u_pid_filter(struct dvb_usb_adapter *adap, int index, u16 pid,
55 return dvb_usb_generic_write(adap->dev, b_pid, 4); 55 return dvb_usb_generic_write(adap->dev, b_pid, 4);
56} 56}
57 57
58/* remote control */ 58static int dtt200u_rc_query(struct dvb_usb_device *d)
59/* key list for the tiny remote control (Yakumo, don't know about the others) */
60static struct rc_map_table rc_map_dtt200u_table[] = {
61 { 0x8001, KEY_MUTE },
62 { 0x8002, KEY_CHANNELDOWN },
63 { 0x8003, KEY_VOLUMEDOWN },
64 { 0x8004, KEY_1 },
65 { 0x8005, KEY_2 },
66 { 0x8006, KEY_3 },
67 { 0x8007, KEY_4 },
68 { 0x8008, KEY_5 },
69 { 0x8009, KEY_6 },
70 { 0x800a, KEY_7 },
71 { 0x800c, KEY_ZOOM },
72 { 0x800d, KEY_0 },
73 { 0x800e, KEY_SELECT },
74 { 0x8012, KEY_POWER },
75 { 0x801a, KEY_CHANNELUP },
76 { 0x801b, KEY_8 },
77 { 0x801e, KEY_VOLUMEUP },
78 { 0x801f, KEY_9 },
79};
80
81static int dtt200u_rc_query(struct dvb_usb_device *d, u32 *event, int *state)
82{ 59{
83 u8 key[5],cmd = GET_RC_CODE; 60 u8 key[5],cmd = GET_RC_CODE;
61 u32 scancode;
62
84 dvb_usb_generic_rw(d,&cmd,1,key,5,0); 63 dvb_usb_generic_rw(d,&cmd,1,key,5,0);
85 dvb_usb_nec_rc_key_to_event(d,key,event,state); 64 if (key[0] == 1) {
65 scancode = key[1];
66 if ((u8) ~key[1] != key[2]) {
67 /* Extended NEC */
68 scancode = scancode << 8;
69 scancode |= key[2];
70 }
71 scancode = scancode << 8;
72 scancode |= key[3];
73
74 /* Check command checksum is ok */
75 if ((u8) ~key[3] == key[4])
76 rc_keydown(d->rc_dev, RC_TYPE_NEC, scancode, 0);
77 else
78 rc_keyup(d->rc_dev);
79 } else if (key[0] == 2) {
80 rc_repeat(d->rc_dev);
81 } else {
82 rc_keyup(d->rc_dev);
83 }
84
86 if (key[0] != 0) 85 if (key[0] != 0)
87 deb_info("key: %*ph\n", 5, key); 86 deb_info("key: %*ph\n", 5, key);
87
88 return 0; 88 return 0;
89} 89}
90 90
@@ -164,11 +164,11 @@ static struct dvb_usb_device_properties dtt200u_properties = {
164 }, 164 },
165 .power_ctrl = dtt200u_power_ctrl, 165 .power_ctrl = dtt200u_power_ctrl,
166 166
167 .rc.legacy = { 167 .rc.core = {
168 .rc_interval = 300, 168 .rc_interval = 300,
169 .rc_map_table = rc_map_dtt200u_table, 169 .rc_codes = RC_MAP_DTT200U,
170 .rc_map_size = ARRAY_SIZE(rc_map_dtt200u_table),
171 .rc_query = dtt200u_rc_query, 170 .rc_query = dtt200u_rc_query,
171 .allowed_protos = RC_BIT_NEC,
172 }, 172 },
173 173
174 .generic_bulk_ctrl_endpoint = 0x01, 174 .generic_bulk_ctrl_endpoint = 0x01,
@@ -214,11 +214,11 @@ static struct dvb_usb_device_properties wt220u_properties = {
214 }, 214 },
215 .power_ctrl = dtt200u_power_ctrl, 215 .power_ctrl = dtt200u_power_ctrl,
216 216
217 .rc.legacy = { 217 .rc.core = {
218 .rc_interval = 300, 218 .rc_interval = 300,
219 .rc_map_table = rc_map_dtt200u_table, 219 .rc_codes = RC_MAP_DTT200U,
220 .rc_map_size = ARRAY_SIZE(rc_map_dtt200u_table),
221 .rc_query = dtt200u_rc_query, 220 .rc_query = dtt200u_rc_query,
221 .allowed_protos = RC_BIT_NEC,
222 }, 222 },
223 223
224 .generic_bulk_ctrl_endpoint = 0x01, 224 .generic_bulk_ctrl_endpoint = 0x01,
@@ -264,11 +264,11 @@ static struct dvb_usb_device_properties wt220u_fc_properties = {
264 }, 264 },
265 .power_ctrl = dtt200u_power_ctrl, 265 .power_ctrl = dtt200u_power_ctrl,
266 266
267 .rc.legacy = { 267 .rc.core = {
268 .rc_interval = 300, 268 .rc_interval = 300,
269 .rc_map_table = rc_map_dtt200u_table, 269 .rc_codes = RC_MAP_DTT200U,
270 .rc_map_size = ARRAY_SIZE(rc_map_dtt200u_table),
271 .rc_query = dtt200u_rc_query, 270 .rc_query = dtt200u_rc_query,
271 .allowed_protos = RC_BIT_NEC,
272 }, 272 },
273 273
274 .generic_bulk_ctrl_endpoint = 0x01, 274 .generic_bulk_ctrl_endpoint = 0x01,
@@ -314,11 +314,11 @@ static struct dvb_usb_device_properties wt220u_zl0353_properties = {
314 }, 314 },
315 .power_ctrl = dtt200u_power_ctrl, 315 .power_ctrl = dtt200u_power_ctrl,
316 316
317 .rc.legacy = { 317 .rc.core = {
318 .rc_interval = 300, 318 .rc_interval = 300,
319 .rc_map_table = rc_map_dtt200u_table, 319 .rc_codes = RC_MAP_DTT200U,
320 .rc_map_size = ARRAY_SIZE(rc_map_dtt200u_table),
321 .rc_query = dtt200u_rc_query, 320 .rc_query = dtt200u_rc_query,
321 .allowed_protos = RC_BIT_NEC,
322 }, 322 },
323 323
324 .generic_bulk_ctrl_endpoint = 0x01, 324 .generic_bulk_ctrl_endpoint = 0x01,
diff --git a/drivers/media/usb/dvb-usb/dvb-usb-dvb.c b/drivers/media/usb/dvb-usb/dvb-usb-dvb.c
index 6477b04e95c7..a04c0a250625 100644
--- a/drivers/media/usb/dvb-usb/dvb-usb-dvb.c
+++ b/drivers/media/usb/dvb-usb/dvb-usb-dvb.c
@@ -320,8 +320,6 @@ int dvb_usb_adapter_frontend_init(struct dvb_usb_adapter *adap)
320 320
321 adap->num_frontends_initialized++; 321 adap->num_frontends_initialized++;
322 } 322 }
323 if (ret)
324 return ret;
325 323
326 ret = dvb_create_media_graph(&adap->dvb_adap, true); 324 ret = dvb_create_media_graph(&adap->dvb_adap, true);
327 if (ret) 325 if (ret)
diff --git a/drivers/media/usb/dvb-usb/dw2102.c b/drivers/media/usb/dvb-usb/dw2102.c
index 49b55d7069b1..5fb0c650926e 100644
--- a/drivers/media/usb/dvb-usb/dw2102.c
+++ b/drivers/media/usb/dvb-usb/dw2102.c
@@ -847,7 +847,7 @@ static int su3000_power_ctrl(struct dvb_usb_device *d, int i)
847 struct dw2102_state *state = (struct dw2102_state *)d->priv; 847 struct dw2102_state *state = (struct dw2102_state *)d->priv;
848 u8 obuf[] = {0xde, 0}; 848 u8 obuf[] = {0xde, 0};
849 849
850 info("%s: %d, initialized %d\n", __func__, i, state->initialized); 850 info("%s: %d, initialized %d", __func__, i, state->initialized);
851 851
852 if (i && !state->initialized) { 852 if (i && !state->initialized) {
853 state->initialized = 1; 853 state->initialized = 1;
@@ -894,7 +894,7 @@ static int su3000_identify_state(struct usb_device *udev,
894 struct dvb_usb_device_description **desc, 894 struct dvb_usb_device_description **desc,
895 int *cold) 895 int *cold)
896{ 896{
897 info("%s\n", __func__); 897 info("%s", __func__);
898 898
899 *cold = 0; 899 *cold = 0;
900 return 0; 900 return 0;
@@ -1132,7 +1132,7 @@ static int dw2104_frontend_attach(struct dvb_usb_adapter *d)
1132 tuner_ops->set_bandwidth = stb6100_set_bandw; 1132 tuner_ops->set_bandwidth = stb6100_set_bandw;
1133 tuner_ops->get_bandwidth = stb6100_get_bandw; 1133 tuner_ops->get_bandwidth = stb6100_get_bandw;
1134 d->fe_adap[0].fe->ops.set_voltage = dw210x_set_voltage; 1134 d->fe_adap[0].fe->ops.set_voltage = dw210x_set_voltage;
1135 info("Attached STV0900+STB6100!\n"); 1135 info("Attached STV0900+STB6100!");
1136 return 0; 1136 return 0;
1137 } 1137 }
1138 } 1138 }
@@ -1146,7 +1146,7 @@ static int dw2104_frontend_attach(struct dvb_usb_adapter *d)
1146 &dw2104_stv6110_config, 1146 &dw2104_stv6110_config,
1147 &d->dev->i2c_adap)) { 1147 &d->dev->i2c_adap)) {
1148 d->fe_adap[0].fe->ops.set_voltage = dw210x_set_voltage; 1148 d->fe_adap[0].fe->ops.set_voltage = dw210x_set_voltage;
1149 info("Attached STV0900+STV6110A!\n"); 1149 info("Attached STV0900+STV6110A!");
1150 return 0; 1150 return 0;
1151 } 1151 }
1152 } 1152 }
@@ -1157,7 +1157,7 @@ static int dw2104_frontend_attach(struct dvb_usb_adapter *d)
1157 &d->dev->i2c_adap); 1157 &d->dev->i2c_adap);
1158 if (d->fe_adap[0].fe != NULL) { 1158 if (d->fe_adap[0].fe != NULL) {
1159 d->fe_adap[0].fe->ops.set_voltage = dw210x_set_voltage; 1159 d->fe_adap[0].fe->ops.set_voltage = dw210x_set_voltage;
1160 info("Attached cx24116!\n"); 1160 info("Attached cx24116!");
1161 return 0; 1161 return 0;
1162 } 1162 }
1163 } 1163 }
@@ -1168,7 +1168,7 @@ static int dw2104_frontend_attach(struct dvb_usb_adapter *d)
1168 dvb_attach(ts2020_attach, d->fe_adap[0].fe, 1168 dvb_attach(ts2020_attach, d->fe_adap[0].fe,
1169 &dw2104_ts2020_config, &d->dev->i2c_adap); 1169 &dw2104_ts2020_config, &d->dev->i2c_adap);
1170 d->fe_adap[0].fe->ops.set_voltage = dw210x_set_voltage; 1170 d->fe_adap[0].fe->ops.set_voltage = dw210x_set_voltage;
1171 info("Attached DS3000!\n"); 1171 info("Attached DS3000!");
1172 return 0; 1172 return 0;
1173 } 1173 }
1174 1174
@@ -1187,7 +1187,7 @@ static int dw2102_frontend_attach(struct dvb_usb_adapter *d)
1187 &d->dev->i2c_adap); 1187 &d->dev->i2c_adap);
1188 if (d->fe_adap[0].fe != NULL) { 1188 if (d->fe_adap[0].fe != NULL) {
1189 d->fe_adap[0].fe->ops.set_voltage = dw210x_set_voltage; 1189 d->fe_adap[0].fe->ops.set_voltage = dw210x_set_voltage;
1190 info("Attached si21xx!\n"); 1190 info("Attached si21xx!");
1191 return 0; 1191 return 0;
1192 } 1192 }
1193 } 1193 }
@@ -1199,7 +1199,7 @@ static int dw2102_frontend_attach(struct dvb_usb_adapter *d)
1199 if (dvb_attach(stb6000_attach, d->fe_adap[0].fe, 0x61, 1199 if (dvb_attach(stb6000_attach, d->fe_adap[0].fe, 0x61,
1200 &d->dev->i2c_adap)) { 1200 &d->dev->i2c_adap)) {
1201 d->fe_adap[0].fe->ops.set_voltage = dw210x_set_voltage; 1201 d->fe_adap[0].fe->ops.set_voltage = dw210x_set_voltage;
1202 info("Attached stv0288!\n"); 1202 info("Attached stv0288!");
1203 return 0; 1203 return 0;
1204 } 1204 }
1205 } 1205 }
@@ -1211,7 +1211,7 @@ static int dw2102_frontend_attach(struct dvb_usb_adapter *d)
1211 &d->dev->i2c_adap); 1211 &d->dev->i2c_adap);
1212 if (d->fe_adap[0].fe != NULL) { 1212 if (d->fe_adap[0].fe != NULL) {
1213 d->fe_adap[0].fe->ops.set_voltage = dw210x_set_voltage; 1213 d->fe_adap[0].fe->ops.set_voltage = dw210x_set_voltage;
1214 info("Attached stv0299!\n"); 1214 info("Attached stv0299!");
1215 return 0; 1215 return 0;
1216 } 1216 }
1217 } 1217 }
@@ -1223,7 +1223,7 @@ static int dw3101_frontend_attach(struct dvb_usb_adapter *d)
1223 d->fe_adap[0].fe = dvb_attach(tda10023_attach, &dw3101_tda10023_config, 1223 d->fe_adap[0].fe = dvb_attach(tda10023_attach, &dw3101_tda10023_config,
1224 &d->dev->i2c_adap, 0x48); 1224 &d->dev->i2c_adap, 0x48);
1225 if (d->fe_adap[0].fe != NULL) { 1225 if (d->fe_adap[0].fe != NULL) {
1226 info("Attached tda10023!\n"); 1226 info("Attached tda10023!");
1227 return 0; 1227 return 0;
1228 } 1228 }
1229 return -EIO; 1229 return -EIO;
@@ -1237,7 +1237,7 @@ static int zl100313_frontend_attach(struct dvb_usb_adapter *d)
1237 if (dvb_attach(zl10039_attach, d->fe_adap[0].fe, 0x60, 1237 if (dvb_attach(zl10039_attach, d->fe_adap[0].fe, 0x60,
1238 &d->dev->i2c_adap)) { 1238 &d->dev->i2c_adap)) {
1239 d->fe_adap[0].fe->ops.set_voltage = dw210x_set_voltage; 1239 d->fe_adap[0].fe->ops.set_voltage = dw210x_set_voltage;
1240 info("Attached zl100313+zl10039!\n"); 1240 info("Attached zl100313+zl10039!");
1241 return 0; 1241 return 0;
1242 } 1242 }
1243 } 1243 }
@@ -1262,7 +1262,7 @@ static int stv0288_frontend_attach(struct dvb_usb_adapter *d)
1262 1262
1263 dw210x_op_rw(d->dev->udev, 0x8a, 0, 0, obuf, 2, DW210X_WRITE_MSG); 1263 dw210x_op_rw(d->dev->udev, 0x8a, 0, 0, obuf, 2, DW210X_WRITE_MSG);
1264 1264
1265 info("Attached stv0288+stb6000!\n"); 1265 info("Attached stv0288+stb6000!");
1266 1266
1267 return 0; 1267 return 0;
1268 1268
@@ -1287,7 +1287,7 @@ static int ds3000_frontend_attach(struct dvb_usb_adapter *d)
1287 1287
1288 dw210x_op_rw(d->dev->udev, 0x8a, 0, 0, obuf, 2, DW210X_WRITE_MSG); 1288 dw210x_op_rw(d->dev->udev, 0x8a, 0, 0, obuf, 2, DW210X_WRITE_MSG);
1289 1289
1290 info("Attached ds3000+ts2020!\n"); 1290 info("Attached ds3000+ts2020!");
1291 1291
1292 return 0; 1292 return 0;
1293} 1293}
@@ -1305,7 +1305,7 @@ static int prof_7500_frontend_attach(struct dvb_usb_adapter *d)
1305 1305
1306 dw210x_op_rw(d->dev->udev, 0x8a, 0, 0, obuf, 2, DW210X_WRITE_MSG); 1306 dw210x_op_rw(d->dev->udev, 0x8a, 0, 0, obuf, 2, DW210X_WRITE_MSG);
1307 1307
1308 info("Attached STV0900+STB6100A!\n"); 1308 info("Attached STV0900+STB6100A!");
1309 1309
1310 return 0; 1310 return 0;
1311} 1311}
@@ -1353,11 +1353,11 @@ static int su3000_frontend_attach(struct dvb_usb_adapter *d)
1353 if (dvb_attach(ts2020_attach, d->fe_adap[0].fe, 1353 if (dvb_attach(ts2020_attach, d->fe_adap[0].fe,
1354 &dw2104_ts2020_config, 1354 &dw2104_ts2020_config,
1355 &d->dev->i2c_adap)) { 1355 &d->dev->i2c_adap)) {
1356 info("Attached DS3000/TS2020!\n"); 1356 info("Attached DS3000/TS2020!");
1357 return 0; 1357 return 0;
1358 } 1358 }
1359 1359
1360 info("Failed to attach DS3000/TS2020!\n"); 1360 info("Failed to attach DS3000/TS2020!");
1361 return -EIO; 1361 return -EIO;
1362} 1362}
1363 1363
@@ -1402,12 +1402,12 @@ static int t220_frontend_attach(struct dvb_usb_adapter *d)
1402 if (d->fe_adap[0].fe != NULL) { 1402 if (d->fe_adap[0].fe != NULL) {
1403 if (dvb_attach(tda18271_attach, d->fe_adap[0].fe, 0x60, 1403 if (dvb_attach(tda18271_attach, d->fe_adap[0].fe, 0x60,
1404 &d->dev->i2c_adap, &tda18271_config)) { 1404 &d->dev->i2c_adap, &tda18271_config)) {
1405 info("Attached TDA18271HD/CXD2820R!\n"); 1405 info("Attached TDA18271HD/CXD2820R!");
1406 return 0; 1406 return 0;
1407 } 1407 }
1408 } 1408 }
1409 1409
1410 info("Failed to attach TDA18271HD/CXD2820R!\n"); 1410 info("Failed to attach TDA18271HD/CXD2820R!");
1411 return -EIO; 1411 return -EIO;
1412} 1412}
1413 1413
@@ -1428,11 +1428,11 @@ static int m88rs2000_frontend_attach(struct dvb_usb_adapter *d)
1428 if (dvb_attach(ts2020_attach, d->fe_adap[0].fe, 1428 if (dvb_attach(ts2020_attach, d->fe_adap[0].fe,
1429 &dw2104_ts2020_config, 1429 &dw2104_ts2020_config,
1430 &d->dev->i2c_adap)) { 1430 &d->dev->i2c_adap)) {
1431 info("Attached RS2000/TS2020!\n"); 1431 info("Attached RS2000/TS2020!");
1432 return 0; 1432 return 0;
1433 } 1433 }
1434 1434
1435 info("Failed to attach RS2000/TS2020!\n"); 1435 info("Failed to attach RS2000/TS2020!");
1436 return -EIO; 1436 return -EIO;
1437} 1437}
1438 1438
@@ -1641,6 +1641,7 @@ enum dw2102_table_entry {
1641 TEVII_S421, 1641 TEVII_S421,
1642 TEVII_S632, 1642 TEVII_S632,
1643 TERRATEC_CINERGY_S2_R2, 1643 TERRATEC_CINERGY_S2_R2,
1644 TERRATEC_CINERGY_S2_R3,
1644 GOTVIEW_SAT_HD, 1645 GOTVIEW_SAT_HD,
1645 GENIATECH_T220, 1646 GENIATECH_T220,
1646 TECHNOTREND_S2_4600, 1647 TECHNOTREND_S2_4600,
@@ -1669,6 +1670,7 @@ static struct usb_device_id dw2102_table[] = {
1669 [TEVII_S421] = {USB_DEVICE(0x9022, USB_PID_TEVII_S421)}, 1670 [TEVII_S421] = {USB_DEVICE(0x9022, USB_PID_TEVII_S421)},
1670 [TEVII_S632] = {USB_DEVICE(0x9022, USB_PID_TEVII_S632)}, 1671 [TEVII_S632] = {USB_DEVICE(0x9022, USB_PID_TEVII_S632)},
1671 [TERRATEC_CINERGY_S2_R2] = {USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_CINERGY_S2_R2)}, 1672 [TERRATEC_CINERGY_S2_R2] = {USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_CINERGY_S2_R2)},
1673 [TERRATEC_CINERGY_S2_R3] = {USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_CINERGY_S2_R3)},
1672 [GOTVIEW_SAT_HD] = {USB_DEVICE(0x1FE1, USB_PID_GOTVIEW_SAT_HD)}, 1674 [GOTVIEW_SAT_HD] = {USB_DEVICE(0x1FE1, USB_PID_GOTVIEW_SAT_HD)},
1673 [GENIATECH_T220] = {USB_DEVICE(0x1f4d, 0xD220)}, 1675 [GENIATECH_T220] = {USB_DEVICE(0x1f4d, 0xD220)},
1674 [TECHNOTREND_S2_4600] = {USB_DEVICE(USB_VID_TECHNOTREND, 1676 [TECHNOTREND_S2_4600] = {USB_DEVICE(USB_VID_TECHNOTREND,
@@ -2083,7 +2085,7 @@ static struct dvb_usb_device_properties su3000_properties = {
2083 }}, 2085 }},
2084 } 2086 }
2085 }, 2087 },
2086 .num_device_descs = 5, 2088 .num_device_descs = 6,
2087 .devices = { 2089 .devices = {
2088 { "SU3000HD DVB-S USB2.0", 2090 { "SU3000HD DVB-S USB2.0",
2089 { &dw2102_table[GENIATECH_SU3000], NULL }, 2091 { &dw2102_table[GENIATECH_SU3000], NULL },
@@ -2101,6 +2103,10 @@ static struct dvb_usb_device_properties su3000_properties = {
2101 { &dw2102_table[TERRATEC_CINERGY_S2_R2], NULL }, 2103 { &dw2102_table[TERRATEC_CINERGY_S2_R2], NULL },
2102 { NULL }, 2104 { NULL },
2103 }, 2105 },
2106 { "Terratec Cinergy S2 USB HD Rev.3",
2107 { &dw2102_table[TERRATEC_CINERGY_S2_R3], NULL },
2108 { NULL },
2109 },
2104 { "GOTVIEW Satellite HD", 2110 { "GOTVIEW Satellite HD",
2105 { &dw2102_table[GOTVIEW_SAT_HD], NULL }, 2111 { &dw2102_table[GOTVIEW_SAT_HD], NULL },
2106 { NULL }, 2112 { NULL },
diff --git a/drivers/media/usb/em28xx/em28xx-dvb.c b/drivers/media/usb/em28xx/em28xx-dvb.c
index 1a5c01202f73..8cedef0daae4 100644
--- a/drivers/media/usb/em28xx/em28xx-dvb.c
+++ b/drivers/media/usb/em28xx/em28xx-dvb.c
@@ -904,17 +904,6 @@ static struct tda18271_config c3tech_duo_tda18271_config = {
904 .small_i2c = TDA18271_03_BYTE_CHUNK_INIT, 904 .small_i2c = TDA18271_03_BYTE_CHUNK_INIT,
905}; 905};
906 906
907static const struct m88ds3103_config pctv_461e_m88ds3103_config = {
908 .i2c_addr = 0x68,
909 .clock = 27000000,
910 .i2c_wr_max = 33,
911 .clock_out = 0,
912 .ts_mode = M88DS3103_TS_PARALLEL,
913 .ts_clk = 16000,
914 .ts_clk_pol = 1,
915 .agc = 0x99,
916};
917
918static struct tda18271_std_map drx_j_std_map = { 907static struct tda18271_std_map drx_j_std_map = {
919 .atsc_6 = { .if_freq = 5000, .agc_mode = 3, .std = 0, .if_lvl = 1, 908 .atsc_6 = { .if_freq = 5000, .agc_mode = 3, .std = 0, .if_lvl = 1,
920 .rfagc_top = 0x37, }, 909 .rfagc_top = 0x37, },
diff --git a/drivers/media/usb/em28xx/em28xx-i2c.c b/drivers/media/usb/em28xx/em28xx-i2c.c
index a19b5c8b56ff..1a9e1e556706 100644
--- a/drivers/media/usb/em28xx/em28xx-i2c.c
+++ b/drivers/media/usb/em28xx/em28xx-i2c.c
@@ -507,9 +507,8 @@ static int em28xx_i2c_xfer(struct i2c_adapter *i2c_adap,
507 if (dev->disconnected) 507 if (dev->disconnected)
508 return -ENODEV; 508 return -ENODEV;
509 509
510 rc = rt_mutex_trylock(&dev->i2c_bus_lock); 510 if (!rt_mutex_trylock(&dev->i2c_bus_lock))
511 if (rc < 0) 511 return -EAGAIN;
512 return rc;
513 512
514 /* Switch I2C bus if needed */ 513 /* Switch I2C bus if needed */
515 if (bus != dev->cur_i2c_bus && 514 if (bus != dev->cur_i2c_bus &&
diff --git a/drivers/media/usb/em28xx/em28xx-vbi.c b/drivers/media/usb/em28xx/em28xx-vbi.c
index fe94c9225dd7..836c6b53b16c 100644
--- a/drivers/media/usb/em28xx/em28xx-vbi.c
+++ b/drivers/media/usb/em28xx/em28xx-vbi.c
@@ -33,7 +33,7 @@
33 33
34static int vbi_queue_setup(struct vb2_queue *vq, 34static int vbi_queue_setup(struct vb2_queue *vq,
35 unsigned int *nbuffers, unsigned int *nplanes, 35 unsigned int *nbuffers, unsigned int *nplanes,
36 unsigned int sizes[], void *alloc_ctxs[]) 36 unsigned int sizes[], struct device *alloc_devs[])
37{ 37{
38 struct em28xx *dev = vb2_get_drv_priv(vq); 38 struct em28xx *dev = vb2_get_drv_priv(vq);
39 struct em28xx_v4l2 *v4l2 = dev->v4l2; 39 struct em28xx_v4l2 *v4l2 = dev->v4l2;
diff --git a/drivers/media/usb/em28xx/em28xx-video.c b/drivers/media/usb/em28xx/em28xx-video.c
index 44834b2eff55..7968695217f3 100644
--- a/drivers/media/usb/em28xx/em28xx-video.c
+++ b/drivers/media/usb/em28xx/em28xx-video.c
@@ -1013,7 +1013,7 @@ static void em28xx_v4l2_create_entities(struct em28xx *dev)
1013 1013
1014static int queue_setup(struct vb2_queue *vq, 1014static int queue_setup(struct vb2_queue *vq,
1015 unsigned int *nbuffers, unsigned int *nplanes, 1015 unsigned int *nbuffers, unsigned int *nplanes,
1016 unsigned int sizes[], void *alloc_ctxs[]) 1016 unsigned int sizes[], struct device *alloc_devs[])
1017{ 1017{
1018 struct em28xx *dev = vb2_get_drv_priv(vq); 1018 struct em28xx *dev = vb2_get_drv_priv(vq);
1019 struct em28xx_v4l2 *v4l2 = dev->v4l2; 1019 struct em28xx_v4l2 *v4l2 = dev->v4l2;
diff --git a/drivers/media/usb/go7007/go7007-v4l2.c b/drivers/media/usb/go7007/go7007-v4l2.c
index ea01ee5df60a..af8458996d91 100644
--- a/drivers/media/usb/go7007/go7007-v4l2.c
+++ b/drivers/media/usb/go7007/go7007-v4l2.c
@@ -370,7 +370,7 @@ static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
370 370
371static int go7007_queue_setup(struct vb2_queue *q, 371static int go7007_queue_setup(struct vb2_queue *q,
372 unsigned int *num_buffers, unsigned int *num_planes, 372 unsigned int *num_buffers, unsigned int *num_planes,
373 unsigned int sizes[], void *alloc_ctxs[]) 373 unsigned int sizes[], struct device *alloc_devs[])
374{ 374{
375 sizes[0] = GO7007_BUF_SIZE; 375 sizes[0] = GO7007_BUF_SIZE;
376 *num_planes = 1; 376 *num_planes = 1;
diff --git a/drivers/media/usb/gspca/cpia1.c b/drivers/media/usb/gspca/cpia1.c
index f23df4a9d8c5..52b88e9e656b 100644
--- a/drivers/media/usb/gspca/cpia1.c
+++ b/drivers/media/usb/gspca/cpia1.c
@@ -1624,7 +1624,7 @@ static int sd_start(struct gspca_dev *gspca_dev)
1624 1624
1625static void sd_stopN(struct gspca_dev *gspca_dev) 1625static void sd_stopN(struct gspca_dev *gspca_dev)
1626{ 1626{
1627 struct sd *sd = (struct sd *) gspca_dev; 1627 struct sd *sd __maybe_unused = (struct sd *) gspca_dev;
1628 1628
1629 command_pause(gspca_dev); 1629 command_pause(gspca_dev);
1630 1630
diff --git a/drivers/media/usb/gspca/gspca.c b/drivers/media/usb/gspca/gspca.c
index af5cd8213e8b..b17bd7ebcb47 100644
--- a/drivers/media/usb/gspca/gspca.c
+++ b/drivers/media/usb/gspca/gspca.c
@@ -522,7 +522,7 @@ static int frame_alloc(struct gspca_dev *gspca_dev, struct file *file,
522 frame = &gspca_dev->frame[i]; 522 frame = &gspca_dev->frame[i];
523 frame->v4l2_buf.index = i; 523 frame->v4l2_buf.index = i;
524 frame->v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; 524 frame->v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
525 frame->v4l2_buf.flags = 0; 525 frame->v4l2_buf.flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
526 frame->v4l2_buf.field = V4L2_FIELD_NONE; 526 frame->v4l2_buf.field = V4L2_FIELD_NONE;
527 frame->v4l2_buf.length = frsz; 527 frame->v4l2_buf.length = frsz;
528 frame->v4l2_buf.memory = memory; 528 frame->v4l2_buf.memory = memory;
@@ -705,7 +705,7 @@ static int build_isoc_ep_tb(struct gspca_dev *gspca_dev,
705 psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3)); 705 psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
706 bandwidth = psize * 1000; 706 bandwidth = psize * 1000;
707 if (gspca_dev->dev->speed == USB_SPEED_HIGH 707 if (gspca_dev->dev->speed == USB_SPEED_HIGH
708 || gspca_dev->dev->speed == USB_SPEED_SUPER) 708 || gspca_dev->dev->speed >= USB_SPEED_SUPER)
709 bandwidth *= 8; 709 bandwidth *= 8;
710 bandwidth /= 1 << (ep->desc.bInterval - 1); 710 bandwidth /= 1 << (ep->desc.bInterval - 1);
711 if (bandwidth <= last_bw) 711 if (bandwidth <= last_bw)
@@ -996,6 +996,19 @@ static int wxh_to_mode(struct gspca_dev *gspca_dev,
996{ 996{
997 int i; 997 int i;
998 998
999 for (i = 0; i < gspca_dev->cam.nmodes; i++) {
1000 if (width == gspca_dev->cam.cam_mode[i].width
1001 && height == gspca_dev->cam.cam_mode[i].height)
1002 return i;
1003 }
1004 return -EINVAL;
1005}
1006
1007static int wxh_to_nearest_mode(struct gspca_dev *gspca_dev,
1008 int width, int height)
1009{
1010 int i;
1011
999 for (i = gspca_dev->cam.nmodes; --i > 0; ) { 1012 for (i = gspca_dev->cam.nmodes; --i > 0; ) {
1000 if (width >= gspca_dev->cam.cam_mode[i].width 1013 if (width >= gspca_dev->cam.cam_mode[i].width
1001 && height >= gspca_dev->cam.cam_mode[i].height) 1014 && height >= gspca_dev->cam.cam_mode[i].height)
@@ -1125,8 +1138,8 @@ static int try_fmt_vid_cap(struct gspca_dev *gspca_dev,
1125 PDEBUG_MODE(gspca_dev, D_CONF, "try fmt cap", 1138 PDEBUG_MODE(gspca_dev, D_CONF, "try fmt cap",
1126 fmt->fmt.pix.pixelformat, w, h); 1139 fmt->fmt.pix.pixelformat, w, h);
1127 1140
1128 /* search the closest mode for width and height */ 1141 /* search the nearest mode for width and height */
1129 mode = wxh_to_mode(gspca_dev, w, h); 1142 mode = wxh_to_nearest_mode(gspca_dev, w, h);
1130 1143
1131 /* OK if right palette */ 1144 /* OK if right palette */
1132 if (gspca_dev->cam.cam_mode[mode].pixelformat 1145 if (gspca_dev->cam.cam_mode[mode].pixelformat
@@ -1233,9 +1246,13 @@ static int vidioc_enum_frameintervals(struct file *filp, void *priv,
1233 struct v4l2_frmivalenum *fival) 1246 struct v4l2_frmivalenum *fival)
1234{ 1247{
1235 struct gspca_dev *gspca_dev = video_drvdata(filp); 1248 struct gspca_dev *gspca_dev = video_drvdata(filp);
1236 int mode = wxh_to_mode(gspca_dev, fival->width, fival->height); 1249 int mode;
1237 __u32 i; 1250 __u32 i;
1238 1251
1252 mode = wxh_to_mode(gspca_dev, fival->width, fival->height);
1253 if (mode < 0)
1254 return -EINVAL;
1255
1239 if (gspca_dev->cam.mode_framerates == NULL || 1256 if (gspca_dev->cam.mode_framerates == NULL ||
1240 gspca_dev->cam.mode_framerates[mode].nrates == 0) 1257 gspca_dev->cam.mode_framerates[mode].nrates == 0)
1241 return -EINVAL; 1258 return -EINVAL;
@@ -1246,7 +1263,7 @@ static int vidioc_enum_frameintervals(struct file *filp, void *priv,
1246 1263
1247 for (i = 0; i < gspca_dev->cam.mode_framerates[mode].nrates; i++) { 1264 for (i = 0; i < gspca_dev->cam.mode_framerates[mode].nrates; i++) {
1248 if (fival->index == i) { 1265 if (fival->index == i) {
1249 fival->type = V4L2_FRMSIZE_TYPE_DISCRETE; 1266 fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
1250 fival->discrete.numerator = 1; 1267 fival->discrete.numerator = 1;
1251 fival->discrete.denominator = 1268 fival->discrete.denominator =
1252 gspca_dev->cam.mode_framerates[mode].rates[i]; 1269 gspca_dev->cam.mode_framerates[mode].rates[i];
diff --git a/drivers/media/usb/gspca/konica.c b/drivers/media/usb/gspca/konica.c
index 39c96bb4c985..0712b1bc90b4 100644
--- a/drivers/media/usb/gspca/konica.c
+++ b/drivers/media/usb/gspca/konica.c
@@ -243,7 +243,7 @@ static int sd_start(struct gspca_dev *gspca_dev)
243 243
244static void sd_stopN(struct gspca_dev *gspca_dev) 244static void sd_stopN(struct gspca_dev *gspca_dev)
245{ 245{
246 struct sd *sd = (struct sd *) gspca_dev; 246 struct sd *sd __maybe_unused = (struct sd *) gspca_dev;
247 247
248 konica_stream_off(gspca_dev); 248 konica_stream_off(gspca_dev);
249#if IS_ENABLED(CONFIG_INPUT) 249#if IS_ENABLED(CONFIG_INPUT)
diff --git a/drivers/media/usb/gspca/m5602/m5602_bridge.h b/drivers/media/usb/gspca/m5602/m5602_bridge.h
index 19eb1a64f9d6..43ebc03d844d 100644
--- a/drivers/media/usb/gspca/m5602/m5602_bridge.h
+++ b/drivers/media/usb/gspca/m5602/m5602_bridge.h
@@ -115,21 +115,6 @@
115 115
116/*****************************************************************************/ 116/*****************************************************************************/
117 117
118/* A skeleton used for sending messages to the m5602 bridge */
119static const unsigned char bridge_urb_skeleton[] = {
120 0x13, 0x00, 0x81, 0x00
121};
122
123/* A skeleton used for sending messages to the sensor */
124static const unsigned char sensor_urb_skeleton[] = {
125 0x23, M5602_XB_GPIO_EN_H, 0x81, 0x06,
126 0x23, M5602_XB_MISC_CTRL, 0x81, 0x80,
127 0x13, M5602_XB_I2C_DEV_ADDR, 0x81, 0x00,
128 0x13, M5602_XB_I2C_REG_ADDR, 0x81, 0x00,
129 0x13, M5602_XB_I2C_DATA, 0x81, 0x00,
130 0x13, M5602_XB_I2C_CTRL, 0x81, 0x11
131};
132
133struct sd { 118struct sd {
134 struct gspca_dev gspca_dev; 119 struct gspca_dev gspca_dev;
135 120
diff --git a/drivers/media/usb/gspca/m5602/m5602_core.c b/drivers/media/usb/gspca/m5602/m5602_core.c
index d926e62cb80b..e4a0658e3f83 100644
--- a/drivers/media/usb/gspca/m5602/m5602_core.c
+++ b/drivers/media/usb/gspca/m5602/m5602_core.c
@@ -37,6 +37,21 @@ static const struct usb_device_id m5602_table[] = {
37 37
38MODULE_DEVICE_TABLE(usb, m5602_table); 38MODULE_DEVICE_TABLE(usb, m5602_table);
39 39
40/* A skeleton used for sending messages to the sensor */
41static const unsigned char sensor_urb_skeleton[] = {
42 0x23, M5602_XB_GPIO_EN_H, 0x81, 0x06,
43 0x23, M5602_XB_MISC_CTRL, 0x81, 0x80,
44 0x13, M5602_XB_I2C_DEV_ADDR, 0x81, 0x00,
45 0x13, M5602_XB_I2C_REG_ADDR, 0x81, 0x00,
46 0x13, M5602_XB_I2C_DATA, 0x81, 0x00,
47 0x13, M5602_XB_I2C_CTRL, 0x81, 0x11
48};
49
50/* A skeleton used for sending messages to the m5602 bridge */
51static const unsigned char bridge_urb_skeleton[] = {
52 0x13, 0x00, 0x81, 0x00
53};
54
40/* Reads a byte from the m5602 */ 55/* Reads a byte from the m5602 */
41int m5602_read_bridge(struct sd *sd, const u8 address, u8 *i2c_data) 56int m5602_read_bridge(struct sd *sd, const u8 address, u8 *i2c_data)
42{ 57{
diff --git a/drivers/media/usb/gspca/m5602/m5602_mt9m111.c b/drivers/media/usb/gspca/m5602/m5602_mt9m111.c
index 27fcef11aef4..7d01ddd7ed01 100644
--- a/drivers/media/usb/gspca/m5602/m5602_mt9m111.c
+++ b/drivers/media/usb/gspca/m5602/m5602_mt9m111.c
@@ -23,6 +23,150 @@
23static int mt9m111_s_ctrl(struct v4l2_ctrl *ctrl); 23static int mt9m111_s_ctrl(struct v4l2_ctrl *ctrl);
24static void mt9m111_dump_registers(struct sd *sd); 24static void mt9m111_dump_registers(struct sd *sd);
25 25
26static const unsigned char preinit_mt9m111[][4] = {
27 {BRIDGE, M5602_XB_MCU_CLK_DIV, 0x02, 0x00},
28 {BRIDGE, M5602_XB_MCU_CLK_CTRL, 0xb0, 0x00},
29 {BRIDGE, M5602_XB_SEN_CLK_DIV, 0x00, 0x00},
30 {BRIDGE, M5602_XB_SEN_CLK_CTRL, 0xb0, 0x00},
31 {BRIDGE, M5602_XB_SENSOR_TYPE, 0x0d, 0x00},
32 {BRIDGE, M5602_XB_SENSOR_CTRL, 0x00, 0x00},
33 {BRIDGE, M5602_XB_ADC_CTRL, 0xc0, 0x00},
34 {BRIDGE, M5602_XB_SENSOR_TYPE, 0x09, 0x00},
35
36 {SENSOR, MT9M111_PAGE_MAP, 0x00, 0x00},
37 {SENSOR, MT9M111_SC_RESET,
38 MT9M111_RESET |
39 MT9M111_RESTART |
40 MT9M111_ANALOG_STANDBY |
41 MT9M111_CHIP_DISABLE,
42 MT9M111_SHOW_BAD_FRAMES |
43 MT9M111_RESTART_BAD_FRAMES |
44 MT9M111_SYNCHRONIZE_CHANGES},
45
46 {BRIDGE, M5602_XB_GPIO_DIR, 0x05, 0x00},
47 {BRIDGE, M5602_XB_GPIO_DAT, 0x04, 0x00},
48 {BRIDGE, M5602_XB_GPIO_EN_H, 0x3e, 0x00},
49 {BRIDGE, M5602_XB_GPIO_DIR_H, 0x3e, 0x00},
50 {BRIDGE, M5602_XB_GPIO_DAT_H, 0x02, 0x00},
51 {BRIDGE, M5602_XB_GPIO_EN_L, 0xff, 0x00},
52 {BRIDGE, M5602_XB_GPIO_DIR_L, 0xff, 0x00},
53 {BRIDGE, M5602_XB_GPIO_DAT_L, 0x00, 0x00},
54
55 {BRIDGE, M5602_XB_SEN_CLK_DIV, 0x00, 0x00},
56 {BRIDGE, M5602_XB_SEN_CLK_CTRL, 0xb0, 0x00},
57 {BRIDGE, M5602_XB_GPIO_DIR, 0x07, 0x00},
58 {BRIDGE, M5602_XB_GPIO_DAT, 0x0b, 0x00},
59 {BRIDGE, M5602_XB_GPIO_EN_H, 0x06, 0x00},
60 {BRIDGE, M5602_XB_GPIO_EN_L, 0x00, 0x00},
61
62 {BRIDGE, M5602_XB_I2C_CLK_DIV, 0x0a, 0x00}
63};
64
65static const unsigned char init_mt9m111[][4] = {
66 {BRIDGE, M5602_XB_MCU_CLK_DIV, 0x02, 0x00},
67 {BRIDGE, M5602_XB_MCU_CLK_CTRL, 0xb0, 0x00},
68 {BRIDGE, M5602_XB_SEN_CLK_DIV, 0x00, 0x00},
69 {BRIDGE, M5602_XB_SEN_CLK_CTRL, 0xb0, 0x00},
70 {BRIDGE, M5602_XB_ADC_CTRL, 0xc0, 0x00},
71 {BRIDGE, M5602_XB_SENSOR_TYPE, 0x09, 0x00},
72
73 {BRIDGE, M5602_XB_GPIO_EN_H, 0x06, 0x00},
74 {BRIDGE, M5602_XB_GPIO_EN_L, 0x00, 0x00},
75 {BRIDGE, M5602_XB_GPIO_DAT, 0x04, 0x00},
76 {BRIDGE, M5602_XB_GPIO_DIR_H, 0x3e, 0x00},
77 {BRIDGE, M5602_XB_GPIO_DIR_L, 0xff, 0x00},
78 {BRIDGE, M5602_XB_GPIO_DAT_H, 0x02, 0x00},
79 {BRIDGE, M5602_XB_GPIO_DAT_L, 0x00, 0x00},
80 {BRIDGE, M5602_XB_GPIO_DIR, 0x07, 0x00},
81 {BRIDGE, M5602_XB_GPIO_DAT, 0x0b, 0x00},
82 {BRIDGE, M5602_XB_I2C_CLK_DIV, 0x0a, 0x00},
83
84 {SENSOR, MT9M111_SC_RESET, 0x00, 0x29},
85 {SENSOR, MT9M111_PAGE_MAP, 0x00, 0x00},
86 {SENSOR, MT9M111_SC_RESET, 0x00, 0x08},
87 {SENSOR, MT9M111_PAGE_MAP, 0x00, 0x01},
88 {SENSOR, MT9M111_CP_OPERATING_MODE_CTL, 0x00,
89 MT9M111_CP_OPERATING_MODE_CTL},
90 {SENSOR, MT9M111_CP_LENS_CORRECTION_1, 0x04, 0x2a},
91 {SENSOR, MT9M111_CP_DEFECT_CORR_CONTEXT_A, 0x00,
92 MT9M111_2D_DEFECT_CORRECTION_ENABLE},
93 {SENSOR, MT9M111_CP_DEFECT_CORR_CONTEXT_B, 0x00,
94 MT9M111_2D_DEFECT_CORRECTION_ENABLE},
95 {SENSOR, MT9M111_CP_LUMA_OFFSET, 0x00, 0x00},
96 {SENSOR, MT9M111_CP_LUMA_CLIP, 0xff, 0x00},
97 {SENSOR, MT9M111_CP_OUTPUT_FORMAT_CTL2_CONTEXT_A, 0x14, 0x00},
98 {SENSOR, MT9M111_CP_OUTPUT_FORMAT_CTL2_CONTEXT_B, 0x14, 0x00},
99 {SENSOR, 0xcd, 0x00, 0x0e},
100 {SENSOR, 0xd0, 0x00, 0x40},
101
102 {SENSOR, MT9M111_PAGE_MAP, 0x00, 0x02},
103 {SENSOR, MT9M111_CC_AUTO_EXPOSURE_PARAMETER_18, 0x00, 0x00},
104 {SENSOR, MT9M111_CC_AWB_PARAMETER_7, 0xef, 0x03},
105
106 {SENSOR, MT9M111_PAGE_MAP, 0x00, 0x00},
107 {SENSOR, 0x33, 0x03, 0x49},
108 {SENSOR, 0x34, 0xc0, 0x19},
109 {SENSOR, 0x3f, 0x20, 0x20},
110 {SENSOR, 0x40, 0x20, 0x20},
111 {SENSOR, 0x5a, 0xc0, 0x0a},
112 {SENSOR, 0x70, 0x7b, 0x0a},
113 {SENSOR, 0x71, 0xff, 0x00},
114 {SENSOR, 0x72, 0x19, 0x0e},
115 {SENSOR, 0x73, 0x18, 0x0f},
116 {SENSOR, 0x74, 0x57, 0x32},
117 {SENSOR, 0x75, 0x56, 0x34},
118 {SENSOR, 0x76, 0x73, 0x35},
119 {SENSOR, 0x77, 0x30, 0x12},
120 {SENSOR, 0x78, 0x79, 0x02},
121 {SENSOR, 0x79, 0x75, 0x06},
122 {SENSOR, 0x7a, 0x77, 0x0a},
123 {SENSOR, 0x7b, 0x78, 0x09},
124 {SENSOR, 0x7c, 0x7d, 0x06},
125 {SENSOR, 0x7d, 0x31, 0x10},
126 {SENSOR, 0x7e, 0x00, 0x7e},
127 {SENSOR, 0x80, 0x59, 0x04},
128 {SENSOR, 0x81, 0x59, 0x04},
129 {SENSOR, 0x82, 0x57, 0x0a},
130 {SENSOR, 0x83, 0x58, 0x0b},
131 {SENSOR, 0x84, 0x47, 0x0c},
132 {SENSOR, 0x85, 0x48, 0x0e},
133 {SENSOR, 0x86, 0x5b, 0x02},
134 {SENSOR, 0x87, 0x00, 0x5c},
135 {SENSOR, MT9M111_CONTEXT_CONTROL, 0x00, MT9M111_SEL_CONTEXT_B},
136 {SENSOR, 0x60, 0x00, 0x80},
137 {SENSOR, 0x61, 0x00, 0x00},
138 {SENSOR, 0x62, 0x00, 0x00},
139 {SENSOR, 0x63, 0x00, 0x00},
140 {SENSOR, 0x64, 0x00, 0x00},
141
142 {SENSOR, MT9M111_SC_ROWSTART, 0x00, 0x0d}, /* 13 */
143 {SENSOR, MT9M111_SC_COLSTART, 0x00, 0x12}, /* 18 */
144 {SENSOR, MT9M111_SC_WINDOW_HEIGHT, 0x04, 0x00}, /* 1024 */
145 {SENSOR, MT9M111_SC_WINDOW_WIDTH, 0x05, 0x10}, /* 1296 */
146 {SENSOR, MT9M111_SC_HBLANK_CONTEXT_B, 0x01, 0x60}, /* 352 */
147 {SENSOR, MT9M111_SC_VBLANK_CONTEXT_B, 0x00, 0x11}, /* 17 */
148 {SENSOR, MT9M111_SC_HBLANK_CONTEXT_A, 0x01, 0x60}, /* 352 */
149 {SENSOR, MT9M111_SC_VBLANK_CONTEXT_A, 0x00, 0x11}, /* 17 */
150 {SENSOR, MT9M111_SC_R_MODE_CONTEXT_A, 0x01, 0x0f}, /* 271 */
151 {SENSOR, 0x30, 0x04, 0x00},
152 /* Set number of blank rows chosen to 400 */
153 {SENSOR, MT9M111_SC_SHUTTER_WIDTH, 0x01, 0x90},
154};
155
156static const unsigned char start_mt9m111[][4] = {
157 {BRIDGE, M5602_XB_SEN_CLK_DIV, 0x06, 0x00},
158 {BRIDGE, M5602_XB_SEN_CLK_CTRL, 0xb0, 0x00},
159 {BRIDGE, M5602_XB_ADC_CTRL, 0xc0, 0x00},
160 {BRIDGE, M5602_XB_SENSOR_TYPE, 0x09, 0x00},
161 {BRIDGE, M5602_XB_LINE_OF_FRAME_H, 0x81, 0x00},
162 {BRIDGE, M5602_XB_PIX_OF_LINE_H, 0x82, 0x00},
163 {BRIDGE, M5602_XB_SIG_INI, 0x01, 0x00},
164 {BRIDGE, M5602_XB_VSYNC_PARA, 0x00, 0x00},
165 {BRIDGE, M5602_XB_VSYNC_PARA, 0x00, 0x00},
166 {BRIDGE, M5602_XB_VSYNC_PARA, 0x00, 0x00},
167 {BRIDGE, M5602_XB_VSYNC_PARA, 0x00, 0x00},
168};
169
26static struct v4l2_pix_format mt9m111_modes[] = { 170static struct v4l2_pix_format mt9m111_modes[] = {
27 { 171 {
28 640, 172 640,
diff --git a/drivers/media/usb/gspca/m5602/m5602_mt9m111.h b/drivers/media/usb/gspca/m5602/m5602_mt9m111.h
index 07448d35e3cd..781a16311822 100644
--- a/drivers/media/usb/gspca/m5602/m5602_mt9m111.h
+++ b/drivers/media/usb/gspca/m5602/m5602_mt9m111.h
@@ -126,148 +126,4 @@ static const struct m5602_sensor mt9m111 = {
126 .disconnect = mt9m111_disconnect, 126 .disconnect = mt9m111_disconnect,
127 .start = mt9m111_start, 127 .start = mt9m111_start,
128}; 128};
129
130static const unsigned char preinit_mt9m111[][4] = {
131 {BRIDGE, M5602_XB_MCU_CLK_DIV, 0x02, 0x00},
132 {BRIDGE, M5602_XB_MCU_CLK_CTRL, 0xb0, 0x00},
133 {BRIDGE, M5602_XB_SEN_CLK_DIV, 0x00, 0x00},
134 {BRIDGE, M5602_XB_SEN_CLK_CTRL, 0xb0, 0x00},
135 {BRIDGE, M5602_XB_SENSOR_TYPE, 0x0d, 0x00},
136 {BRIDGE, M5602_XB_SENSOR_CTRL, 0x00, 0x00},
137 {BRIDGE, M5602_XB_ADC_CTRL, 0xc0, 0x00},
138 {BRIDGE, M5602_XB_SENSOR_TYPE, 0x09, 0x00},
139
140 {SENSOR, MT9M111_PAGE_MAP, 0x00, 0x00},
141 {SENSOR, MT9M111_SC_RESET,
142 MT9M111_RESET |
143 MT9M111_RESTART |
144 MT9M111_ANALOG_STANDBY |
145 MT9M111_CHIP_DISABLE,
146 MT9M111_SHOW_BAD_FRAMES |
147 MT9M111_RESTART_BAD_FRAMES |
148 MT9M111_SYNCHRONIZE_CHANGES},
149
150 {BRIDGE, M5602_XB_GPIO_DIR, 0x05, 0x00},
151 {BRIDGE, M5602_XB_GPIO_DAT, 0x04, 0x00},
152 {BRIDGE, M5602_XB_GPIO_EN_H, 0x3e, 0x00},
153 {BRIDGE, M5602_XB_GPIO_DIR_H, 0x3e, 0x00},
154 {BRIDGE, M5602_XB_GPIO_DAT_H, 0x02, 0x00},
155 {BRIDGE, M5602_XB_GPIO_EN_L, 0xff, 0x00},
156 {BRIDGE, M5602_XB_GPIO_DIR_L, 0xff, 0x00},
157 {BRIDGE, M5602_XB_GPIO_DAT_L, 0x00, 0x00},
158
159 {BRIDGE, M5602_XB_SEN_CLK_DIV, 0x00, 0x00},
160 {BRIDGE, M5602_XB_SEN_CLK_CTRL, 0xb0, 0x00},
161 {BRIDGE, M5602_XB_GPIO_DIR, 0x07, 0x00},
162 {BRIDGE, M5602_XB_GPIO_DAT, 0x0b, 0x00},
163 {BRIDGE, M5602_XB_GPIO_EN_H, 0x06, 0x00},
164 {BRIDGE, M5602_XB_GPIO_EN_L, 0x00, 0x00},
165
166 {BRIDGE, M5602_XB_I2C_CLK_DIV, 0x0a, 0x00}
167};
168
169static const unsigned char init_mt9m111[][4] = {
170 {BRIDGE, M5602_XB_MCU_CLK_DIV, 0x02, 0x00},
171 {BRIDGE, M5602_XB_MCU_CLK_CTRL, 0xb0, 0x00},
172 {BRIDGE, M5602_XB_SEN_CLK_DIV, 0x00, 0x00},
173 {BRIDGE, M5602_XB_SEN_CLK_CTRL, 0xb0, 0x00},
174 {BRIDGE, M5602_XB_ADC_CTRL, 0xc0, 0x00},
175 {BRIDGE, M5602_XB_SENSOR_TYPE, 0x09, 0x00},
176
177 {BRIDGE, M5602_XB_GPIO_EN_H, 0x06, 0x00},
178 {BRIDGE, M5602_XB_GPIO_EN_L, 0x00, 0x00},
179 {BRIDGE, M5602_XB_GPIO_DAT, 0x04, 0x00},
180 {BRIDGE, M5602_XB_GPIO_DIR_H, 0x3e, 0x00},
181 {BRIDGE, M5602_XB_GPIO_DIR_L, 0xff, 0x00},
182 {BRIDGE, M5602_XB_GPIO_DAT_H, 0x02, 0x00},
183 {BRIDGE, M5602_XB_GPIO_DAT_L, 0x00, 0x00},
184 {BRIDGE, M5602_XB_GPIO_DIR, 0x07, 0x00},
185 {BRIDGE, M5602_XB_GPIO_DAT, 0x0b, 0x00},
186 {BRIDGE, M5602_XB_I2C_CLK_DIV, 0x0a, 0x00},
187
188 {SENSOR, MT9M111_SC_RESET, 0x00, 0x29},
189 {SENSOR, MT9M111_PAGE_MAP, 0x00, 0x00},
190 {SENSOR, MT9M111_SC_RESET, 0x00, 0x08},
191 {SENSOR, MT9M111_PAGE_MAP, 0x00, 0x01},
192 {SENSOR, MT9M111_CP_OPERATING_MODE_CTL, 0x00,
193 MT9M111_CP_OPERATING_MODE_CTL},
194 {SENSOR, MT9M111_CP_LENS_CORRECTION_1, 0x04, 0x2a},
195 {SENSOR, MT9M111_CP_DEFECT_CORR_CONTEXT_A, 0x00,
196 MT9M111_2D_DEFECT_CORRECTION_ENABLE},
197 {SENSOR, MT9M111_CP_DEFECT_CORR_CONTEXT_B, 0x00,
198 MT9M111_2D_DEFECT_CORRECTION_ENABLE},
199 {SENSOR, MT9M111_CP_LUMA_OFFSET, 0x00, 0x00},
200 {SENSOR, MT9M111_CP_LUMA_CLIP, 0xff, 0x00},
201 {SENSOR, MT9M111_CP_OUTPUT_FORMAT_CTL2_CONTEXT_A, 0x14, 0x00},
202 {SENSOR, MT9M111_CP_OUTPUT_FORMAT_CTL2_CONTEXT_B, 0x14, 0x00},
203 {SENSOR, 0xcd, 0x00, 0x0e},
204 {SENSOR, 0xd0, 0x00, 0x40},
205
206 {SENSOR, MT9M111_PAGE_MAP, 0x00, 0x02},
207 {SENSOR, MT9M111_CC_AUTO_EXPOSURE_PARAMETER_18, 0x00, 0x00},
208 {SENSOR, MT9M111_CC_AWB_PARAMETER_7, 0xef, 0x03},
209
210 {SENSOR, MT9M111_PAGE_MAP, 0x00, 0x00},
211 {SENSOR, 0x33, 0x03, 0x49},
212 {SENSOR, 0x34, 0xc0, 0x19},
213 {SENSOR, 0x3f, 0x20, 0x20},
214 {SENSOR, 0x40, 0x20, 0x20},
215 {SENSOR, 0x5a, 0xc0, 0x0a},
216 {SENSOR, 0x70, 0x7b, 0x0a},
217 {SENSOR, 0x71, 0xff, 0x00},
218 {SENSOR, 0x72, 0x19, 0x0e},
219 {SENSOR, 0x73, 0x18, 0x0f},
220 {SENSOR, 0x74, 0x57, 0x32},
221 {SENSOR, 0x75, 0x56, 0x34},
222 {SENSOR, 0x76, 0x73, 0x35},
223 {SENSOR, 0x77, 0x30, 0x12},
224 {SENSOR, 0x78, 0x79, 0x02},
225 {SENSOR, 0x79, 0x75, 0x06},
226 {SENSOR, 0x7a, 0x77, 0x0a},
227 {SENSOR, 0x7b, 0x78, 0x09},
228 {SENSOR, 0x7c, 0x7d, 0x06},
229 {SENSOR, 0x7d, 0x31, 0x10},
230 {SENSOR, 0x7e, 0x00, 0x7e},
231 {SENSOR, 0x80, 0x59, 0x04},
232 {SENSOR, 0x81, 0x59, 0x04},
233 {SENSOR, 0x82, 0x57, 0x0a},
234 {SENSOR, 0x83, 0x58, 0x0b},
235 {SENSOR, 0x84, 0x47, 0x0c},
236 {SENSOR, 0x85, 0x48, 0x0e},
237 {SENSOR, 0x86, 0x5b, 0x02},
238 {SENSOR, 0x87, 0x00, 0x5c},
239 {SENSOR, MT9M111_CONTEXT_CONTROL, 0x00, MT9M111_SEL_CONTEXT_B},
240 {SENSOR, 0x60, 0x00, 0x80},
241 {SENSOR, 0x61, 0x00, 0x00},
242 {SENSOR, 0x62, 0x00, 0x00},
243 {SENSOR, 0x63, 0x00, 0x00},
244 {SENSOR, 0x64, 0x00, 0x00},
245
246 {SENSOR, MT9M111_SC_ROWSTART, 0x00, 0x0d}, /* 13 */
247 {SENSOR, MT9M111_SC_COLSTART, 0x00, 0x12}, /* 18 */
248 {SENSOR, MT9M111_SC_WINDOW_HEIGHT, 0x04, 0x00}, /* 1024 */
249 {SENSOR, MT9M111_SC_WINDOW_WIDTH, 0x05, 0x10}, /* 1296 */
250 {SENSOR, MT9M111_SC_HBLANK_CONTEXT_B, 0x01, 0x60}, /* 352 */
251 {SENSOR, MT9M111_SC_VBLANK_CONTEXT_B, 0x00, 0x11}, /* 17 */
252 {SENSOR, MT9M111_SC_HBLANK_CONTEXT_A, 0x01, 0x60}, /* 352 */
253 {SENSOR, MT9M111_SC_VBLANK_CONTEXT_A, 0x00, 0x11}, /* 17 */
254 {SENSOR, MT9M111_SC_R_MODE_CONTEXT_A, 0x01, 0x0f}, /* 271 */
255 {SENSOR, 0x30, 0x04, 0x00},
256 /* Set number of blank rows chosen to 400 */
257 {SENSOR, MT9M111_SC_SHUTTER_WIDTH, 0x01, 0x90},
258};
259
260static const unsigned char start_mt9m111[][4] = {
261 {BRIDGE, M5602_XB_SEN_CLK_DIV, 0x06, 0x00},
262 {BRIDGE, M5602_XB_SEN_CLK_CTRL, 0xb0, 0x00},
263 {BRIDGE, M5602_XB_ADC_CTRL, 0xc0, 0x00},
264 {BRIDGE, M5602_XB_SENSOR_TYPE, 0x09, 0x00},
265 {BRIDGE, M5602_XB_LINE_OF_FRAME_H, 0x81, 0x00},
266 {BRIDGE, M5602_XB_PIX_OF_LINE_H, 0x82, 0x00},
267 {BRIDGE, M5602_XB_SIG_INI, 0x01, 0x00},
268 {BRIDGE, M5602_XB_VSYNC_PARA, 0x00, 0x00},
269 {BRIDGE, M5602_XB_VSYNC_PARA, 0x00, 0x00},
270 {BRIDGE, M5602_XB_VSYNC_PARA, 0x00, 0x00},
271 {BRIDGE, M5602_XB_VSYNC_PARA, 0x00, 0x00},
272};
273#endif 129#endif
diff --git a/drivers/media/usb/gspca/m5602/m5602_ov7660.c b/drivers/media/usb/gspca/m5602/m5602_ov7660.c
index 64b3b03a9141..672b7a520695 100644
--- a/drivers/media/usb/gspca/m5602/m5602_ov7660.c
+++ b/drivers/media/usb/gspca/m5602/m5602_ov7660.c
@@ -23,6 +23,159 @@
23static int ov7660_s_ctrl(struct v4l2_ctrl *ctrl); 23static int ov7660_s_ctrl(struct v4l2_ctrl *ctrl);
24static void ov7660_dump_registers(struct sd *sd); 24static void ov7660_dump_registers(struct sd *sd);
25 25
26static const unsigned char preinit_ov7660[][4] = {
27 {BRIDGE, M5602_XB_MCU_CLK_DIV, 0x02},
28 {BRIDGE, M5602_XB_MCU_CLK_CTRL, 0xb0},
29 {BRIDGE, M5602_XB_SEN_CLK_DIV, 0x00},
30 {BRIDGE, M5602_XB_SEN_CLK_CTRL, 0xb0},
31 {BRIDGE, M5602_XB_ADC_CTRL, 0xc0},
32 {BRIDGE, M5602_XB_SENSOR_TYPE, 0x0d},
33 {BRIDGE, M5602_XB_SENSOR_CTRL, 0x00},
34 {BRIDGE, M5602_XB_GPIO_DIR, 0x03},
35 {BRIDGE, M5602_XB_GPIO_DIR, 0x03},
36 {BRIDGE, M5602_XB_ADC_CTRL, 0xc0},
37 {BRIDGE, M5602_XB_SENSOR_TYPE, 0x0c},
38
39 {SENSOR, OV7660_OFON, 0x0c},
40 {SENSOR, OV7660_COM2, 0x11},
41 {SENSOR, OV7660_COM7, 0x05},
42
43 {BRIDGE, M5602_XB_GPIO_DIR, 0x01},
44 {BRIDGE, M5602_XB_GPIO_DAT, 0x04},
45 {BRIDGE, M5602_XB_GPIO_EN_H, 0x06},
46 {BRIDGE, M5602_XB_GPIO_DIR_H, 0x06},
47 {BRIDGE, M5602_XB_GPIO_DAT_H, 0x00},
48 {BRIDGE, M5602_XB_SEN_CLK_DIV, 0x08},
49 {BRIDGE, M5602_XB_SEN_CLK_CTRL, 0xb0},
50 {BRIDGE, M5602_XB_SEN_CLK_DIV, 0x00},
51 {BRIDGE, M5602_XB_SEN_CLK_CTRL, 0xb0},
52 {BRIDGE, M5602_XB_ADC_CTRL, 0xc0},
53 {BRIDGE, M5602_XB_SENSOR_TYPE, 0x0c},
54 {BRIDGE, M5602_XB_GPIO_DIR, 0x05},
55 {BRIDGE, M5602_XB_GPIO_DAT, 0x00},
56 {BRIDGE, M5602_XB_GPIO_EN_H, 0x06},
57 {BRIDGE, M5602_XB_GPIO_EN_L, 0x00}
58};
59
60static const unsigned char init_ov7660[][4] = {
61 {BRIDGE, M5602_XB_MCU_CLK_DIV, 0x02},
62 {BRIDGE, M5602_XB_MCU_CLK_CTRL, 0xb0},
63 {BRIDGE, M5602_XB_SEN_CLK_DIV, 0x00},
64 {BRIDGE, M5602_XB_SEN_CLK_CTRL, 0xb0},
65 {BRIDGE, M5602_XB_ADC_CTRL, 0xc0},
66 {BRIDGE, M5602_XB_SENSOR_TYPE, 0x0d},
67 {BRIDGE, M5602_XB_SENSOR_CTRL, 0x00},
68 {BRIDGE, M5602_XB_GPIO_DIR, 0x01},
69 {BRIDGE, M5602_XB_GPIO_DIR, 0x01},
70 {BRIDGE, M5602_XB_SEN_CLK_DIV, 0x00},
71 {BRIDGE, M5602_XB_SEN_CLK_CTRL, 0xb0},
72 {BRIDGE, M5602_XB_ADC_CTRL, 0xc0},
73 {BRIDGE, M5602_XB_SENSOR_TYPE, 0x0c},
74 {BRIDGE, M5602_XB_GPIO_DIR, 0x05},
75 {BRIDGE, M5602_XB_GPIO_DAT, 0x00},
76 {BRIDGE, M5602_XB_GPIO_EN_H, 0x06},
77 {BRIDGE, M5602_XB_GPIO_EN_L, 0x00},
78 {SENSOR, OV7660_COM7, 0x80},
79 {SENSOR, OV7660_CLKRC, 0x80},
80 {SENSOR, OV7660_COM9, 0x4c},
81 {SENSOR, OV7660_OFON, 0x43},
82 {SENSOR, OV7660_COM12, 0x28},
83 {SENSOR, OV7660_COM8, 0x00},
84 {SENSOR, OV7660_COM10, 0x40},
85 {SENSOR, OV7660_HSTART, 0x0c},
86 {SENSOR, OV7660_HSTOP, 0x61},
87 {SENSOR, OV7660_HREF, 0xa4},
88 {SENSOR, OV7660_PSHFT, 0x0b},
89 {SENSOR, OV7660_VSTART, 0x01},
90 {SENSOR, OV7660_VSTOP, 0x7a},
91 {SENSOR, OV7660_VSTOP, 0x00},
92 {SENSOR, OV7660_COM7, 0x05},
93 {SENSOR, OV7660_COM6, 0x42},
94 {SENSOR, OV7660_BBIAS, 0x94},
95 {SENSOR, OV7660_GbBIAS, 0x94},
96 {SENSOR, OV7660_RSVD29, 0x94},
97 {SENSOR, OV7660_RBIAS, 0x94},
98 {SENSOR, OV7660_COM1, 0x00},
99 {SENSOR, OV7660_AECH, 0x00},
100 {SENSOR, OV7660_AECHH, 0x00},
101 {SENSOR, OV7660_ADC, 0x05},
102 {SENSOR, OV7660_COM13, 0x00},
103 {SENSOR, OV7660_RSVDA1, 0x23},
104 {SENSOR, OV7660_TSLB, 0x0d},
105 {SENSOR, OV7660_HV, 0x80},
106 {SENSOR, OV7660_LCC1, 0x00},
107 {SENSOR, OV7660_LCC2, 0x00},
108 {SENSOR, OV7660_LCC3, 0x10},
109 {SENSOR, OV7660_LCC4, 0x40},
110 {SENSOR, OV7660_LCC5, 0x01},
111
112 {SENSOR, OV7660_AECH, 0x20},
113 {SENSOR, OV7660_COM1, 0x00},
114 {SENSOR, OV7660_OFON, 0x0c},
115 {SENSOR, OV7660_COM2, 0x11},
116 {SENSOR, OV7660_COM7, 0x05},
117 {BRIDGE, M5602_XB_GPIO_DIR, 0x01},
118 {BRIDGE, M5602_XB_GPIO_DAT, 0x04},
119 {BRIDGE, M5602_XB_GPIO_EN_H, 0x06},
120 {BRIDGE, M5602_XB_GPIO_DIR_H, 0x06},
121 {BRIDGE, M5602_XB_GPIO_DAT_H, 0x00},
122 {BRIDGE, M5602_XB_SEN_CLK_DIV, 0x08},
123 {BRIDGE, M5602_XB_SEN_CLK_CTRL, 0xb0},
124 {BRIDGE, M5602_XB_SEN_CLK_DIV, 0x00},
125 {BRIDGE, M5602_XB_SEN_CLK_CTRL, 0xb0},
126 {BRIDGE, M5602_XB_ADC_CTRL, 0xc0},
127 {BRIDGE, M5602_XB_SENSOR_TYPE, 0x0c},
128 {BRIDGE, M5602_XB_GPIO_DIR, 0x05},
129 {BRIDGE, M5602_XB_GPIO_DAT, 0x00},
130 {BRIDGE, M5602_XB_GPIO_EN_H, 0x06},
131 {BRIDGE, M5602_XB_GPIO_EN_L, 0x00},
132 {SENSOR, OV7660_AECH, 0x5f},
133 {SENSOR, OV7660_COM1, 0x03},
134 {SENSOR, OV7660_OFON, 0x0c},
135 {SENSOR, OV7660_COM2, 0x11},
136 {SENSOR, OV7660_COM7, 0x05},
137 {BRIDGE, M5602_XB_GPIO_DIR, 0x01},
138 {BRIDGE, M5602_XB_GPIO_DAT, 0x04},
139 {BRIDGE, M5602_XB_GPIO_EN_H, 0x06},
140 {BRIDGE, M5602_XB_GPIO_DIR_H, 0x06},
141 {BRIDGE, M5602_XB_GPIO_DAT_H, 0x00},
142 {BRIDGE, M5602_XB_SEN_CLK_DIV, 0x08},
143 {BRIDGE, M5602_XB_SEN_CLK_CTRL, 0xb0},
144 {BRIDGE, M5602_XB_SEN_CLK_DIV, 0x00},
145 {BRIDGE, M5602_XB_SEN_CLK_CTRL, 0xb0},
146 {BRIDGE, M5602_XB_ADC_CTRL, 0xc0},
147 {BRIDGE, M5602_XB_SENSOR_TYPE, 0x0c},
148 {BRIDGE, M5602_XB_GPIO_DIR, 0x05},
149 {BRIDGE, M5602_XB_GPIO_DAT, 0x00},
150 {BRIDGE, M5602_XB_GPIO_EN_H, 0x06},
151 {BRIDGE, M5602_XB_GPIO_EN_L, 0x00},
152
153 {BRIDGE, M5602_XB_SEN_CLK_DIV, 0x06},
154 {BRIDGE, M5602_XB_SEN_CLK_CTRL, 0xb0},
155 {BRIDGE, M5602_XB_ADC_CTRL, 0xc0},
156 {BRIDGE, M5602_XB_SENSOR_TYPE, 0x0c},
157 {BRIDGE, M5602_XB_LINE_OF_FRAME_H, 0x81},
158 {BRIDGE, M5602_XB_PIX_OF_LINE_H, 0x82},
159 {BRIDGE, M5602_XB_SIG_INI, 0x01},
160 {BRIDGE, M5602_XB_VSYNC_PARA, 0x00},
161 {BRIDGE, M5602_XB_VSYNC_PARA, 0x08},
162 {BRIDGE, M5602_XB_VSYNC_PARA, 0x00},
163 {BRIDGE, M5602_XB_VSYNC_PARA, 0x00},
164 {BRIDGE, M5602_XB_VSYNC_PARA, 0x01},
165 {BRIDGE, M5602_XB_VSYNC_PARA, 0xec},
166 {BRIDGE, M5602_XB_VSYNC_PARA, 0x00},
167 {BRIDGE, M5602_XB_VSYNC_PARA, 0x00},
168 {BRIDGE, M5602_XB_SIG_INI, 0x00},
169 {BRIDGE, M5602_XB_SIG_INI, 0x02},
170 {BRIDGE, M5602_XB_HSYNC_PARA, 0x00},
171 {BRIDGE, M5602_XB_HSYNC_PARA, 0x27},
172 {BRIDGE, M5602_XB_HSYNC_PARA, 0x02},
173 {BRIDGE, M5602_XB_HSYNC_PARA, 0xa7},
174 {BRIDGE, M5602_XB_SIG_INI, 0x00},
175 {BRIDGE, M5602_XB_SEN_CLK_DIV, 0x00},
176 {BRIDGE, M5602_XB_SEN_CLK_CTRL, 0xb0},
177};
178
26static struct v4l2_pix_format ov7660_modes[] = { 179static struct v4l2_pix_format ov7660_modes[] = {
27 { 180 {
28 640, 181 640,
diff --git a/drivers/media/usb/gspca/m5602/m5602_ov7660.h b/drivers/media/usb/gspca/m5602/m5602_ov7660.h
index 6fece1ce1232..72445d5df195 100644
--- a/drivers/media/usb/gspca/m5602/m5602_ov7660.h
+++ b/drivers/media/usb/gspca/m5602/m5602_ov7660.h
@@ -107,157 +107,4 @@ static const struct m5602_sensor ov7660 = {
107 .stop = ov7660_stop, 107 .stop = ov7660_stop,
108 .disconnect = ov7660_disconnect, 108 .disconnect = ov7660_disconnect,
109}; 109};
110
111static const unsigned char preinit_ov7660[][4] = {
112 {BRIDGE, M5602_XB_MCU_CLK_DIV, 0x02},
113 {BRIDGE, M5602_XB_MCU_CLK_CTRL, 0xb0},
114 {BRIDGE, M5602_XB_SEN_CLK_DIV, 0x00},
115 {BRIDGE, M5602_XB_SEN_CLK_CTRL, 0xb0},
116 {BRIDGE, M5602_XB_ADC_CTRL, 0xc0},
117 {BRIDGE, M5602_XB_SENSOR_TYPE, 0x0d},
118 {BRIDGE, M5602_XB_SENSOR_CTRL, 0x00},
119 {BRIDGE, M5602_XB_GPIO_DIR, 0x03},
120 {BRIDGE, M5602_XB_GPIO_DIR, 0x03},
121 {BRIDGE, M5602_XB_ADC_CTRL, 0xc0},
122 {BRIDGE, M5602_XB_SENSOR_TYPE, 0x0c},
123
124 {SENSOR, OV7660_OFON, 0x0c},
125 {SENSOR, OV7660_COM2, 0x11},
126 {SENSOR, OV7660_COM7, 0x05},
127
128 {BRIDGE, M5602_XB_GPIO_DIR, 0x01},
129 {BRIDGE, M5602_XB_GPIO_DAT, 0x04},
130 {BRIDGE, M5602_XB_GPIO_EN_H, 0x06},
131 {BRIDGE, M5602_XB_GPIO_DIR_H, 0x06},
132 {BRIDGE, M5602_XB_GPIO_DAT_H, 0x00},
133 {BRIDGE, M5602_XB_SEN_CLK_DIV, 0x08},
134 {BRIDGE, M5602_XB_SEN_CLK_CTRL, 0xb0},
135 {BRIDGE, M5602_XB_SEN_CLK_DIV, 0x00},
136 {BRIDGE, M5602_XB_SEN_CLK_CTRL, 0xb0},
137 {BRIDGE, M5602_XB_ADC_CTRL, 0xc0},
138 {BRIDGE, M5602_XB_SENSOR_TYPE, 0x0c},
139 {BRIDGE, M5602_XB_GPIO_DIR, 0x05},
140 {BRIDGE, M5602_XB_GPIO_DAT, 0x00},
141 {BRIDGE, M5602_XB_GPIO_EN_H, 0x06},
142 {BRIDGE, M5602_XB_GPIO_EN_L, 0x00}
143};
144
145static const unsigned char init_ov7660[][4] = {
146 {BRIDGE, M5602_XB_MCU_CLK_DIV, 0x02},
147 {BRIDGE, M5602_XB_MCU_CLK_CTRL, 0xb0},
148 {BRIDGE, M5602_XB_SEN_CLK_DIV, 0x00},
149 {BRIDGE, M5602_XB_SEN_CLK_CTRL, 0xb0},
150 {BRIDGE, M5602_XB_ADC_CTRL, 0xc0},
151 {BRIDGE, M5602_XB_SENSOR_TYPE, 0x0d},
152 {BRIDGE, M5602_XB_SENSOR_CTRL, 0x00},
153 {BRIDGE, M5602_XB_GPIO_DIR, 0x01},
154 {BRIDGE, M5602_XB_GPIO_DIR, 0x01},
155 {BRIDGE, M5602_XB_SEN_CLK_DIV, 0x00},
156 {BRIDGE, M5602_XB_SEN_CLK_CTRL, 0xb0},
157 {BRIDGE, M5602_XB_ADC_CTRL, 0xc0},
158 {BRIDGE, M5602_XB_SENSOR_TYPE, 0x0c},
159 {BRIDGE, M5602_XB_GPIO_DIR, 0x05},
160 {BRIDGE, M5602_XB_GPIO_DAT, 0x00},
161 {BRIDGE, M5602_XB_GPIO_EN_H, 0x06},
162 {BRIDGE, M5602_XB_GPIO_EN_L, 0x00},
163 {SENSOR, OV7660_COM7, 0x80},
164 {SENSOR, OV7660_CLKRC, 0x80},
165 {SENSOR, OV7660_COM9, 0x4c},
166 {SENSOR, OV7660_OFON, 0x43},
167 {SENSOR, OV7660_COM12, 0x28},
168 {SENSOR, OV7660_COM8, 0x00},
169 {SENSOR, OV7660_COM10, 0x40},
170 {SENSOR, OV7660_HSTART, 0x0c},
171 {SENSOR, OV7660_HSTOP, 0x61},
172 {SENSOR, OV7660_HREF, 0xa4},
173 {SENSOR, OV7660_PSHFT, 0x0b},
174 {SENSOR, OV7660_VSTART, 0x01},
175 {SENSOR, OV7660_VSTOP, 0x7a},
176 {SENSOR, OV7660_VSTOP, 0x00},
177 {SENSOR, OV7660_COM7, 0x05},
178 {SENSOR, OV7660_COM6, 0x42},
179 {SENSOR, OV7660_BBIAS, 0x94},
180 {SENSOR, OV7660_GbBIAS, 0x94},
181 {SENSOR, OV7660_RSVD29, 0x94},
182 {SENSOR, OV7660_RBIAS, 0x94},
183 {SENSOR, OV7660_COM1, 0x00},
184 {SENSOR, OV7660_AECH, 0x00},
185 {SENSOR, OV7660_AECHH, 0x00},
186 {SENSOR, OV7660_ADC, 0x05},
187 {SENSOR, OV7660_COM13, 0x00},
188 {SENSOR, OV7660_RSVDA1, 0x23},
189 {SENSOR, OV7660_TSLB, 0x0d},
190 {SENSOR, OV7660_HV, 0x80},
191 {SENSOR, OV7660_LCC1, 0x00},
192 {SENSOR, OV7660_LCC2, 0x00},
193 {SENSOR, OV7660_LCC3, 0x10},
194 {SENSOR, OV7660_LCC4, 0x40},
195 {SENSOR, OV7660_LCC5, 0x01},
196
197 {SENSOR, OV7660_AECH, 0x20},
198 {SENSOR, OV7660_COM1, 0x00},
199 {SENSOR, OV7660_OFON, 0x0c},
200 {SENSOR, OV7660_COM2, 0x11},
201 {SENSOR, OV7660_COM7, 0x05},
202 {BRIDGE, M5602_XB_GPIO_DIR, 0x01},
203 {BRIDGE, M5602_XB_GPIO_DAT, 0x04},
204 {BRIDGE, M5602_XB_GPIO_EN_H, 0x06},
205 {BRIDGE, M5602_XB_GPIO_DIR_H, 0x06},
206 {BRIDGE, M5602_XB_GPIO_DAT_H, 0x00},
207 {BRIDGE, M5602_XB_SEN_CLK_DIV, 0x08},
208 {BRIDGE, M5602_XB_SEN_CLK_CTRL, 0xb0},
209 {BRIDGE, M5602_XB_SEN_CLK_DIV, 0x00},
210 {BRIDGE, M5602_XB_SEN_CLK_CTRL, 0xb0},
211 {BRIDGE, M5602_XB_ADC_CTRL, 0xc0},
212 {BRIDGE, M5602_XB_SENSOR_TYPE, 0x0c},
213 {BRIDGE, M5602_XB_GPIO_DIR, 0x05},
214 {BRIDGE, M5602_XB_GPIO_DAT, 0x00},
215 {BRIDGE, M5602_XB_GPIO_EN_H, 0x06},
216 {BRIDGE, M5602_XB_GPIO_EN_L, 0x00},
217 {SENSOR, OV7660_AECH, 0x5f},
218 {SENSOR, OV7660_COM1, 0x03},
219 {SENSOR, OV7660_OFON, 0x0c},
220 {SENSOR, OV7660_COM2, 0x11},
221 {SENSOR, OV7660_COM7, 0x05},
222 {BRIDGE, M5602_XB_GPIO_DIR, 0x01},
223 {BRIDGE, M5602_XB_GPIO_DAT, 0x04},
224 {BRIDGE, M5602_XB_GPIO_EN_H, 0x06},
225 {BRIDGE, M5602_XB_GPIO_DIR_H, 0x06},
226 {BRIDGE, M5602_XB_GPIO_DAT_H, 0x00},
227 {BRIDGE, M5602_XB_SEN_CLK_DIV, 0x08},
228 {BRIDGE, M5602_XB_SEN_CLK_CTRL, 0xb0},
229 {BRIDGE, M5602_XB_SEN_CLK_DIV, 0x00},
230 {BRIDGE, M5602_XB_SEN_CLK_CTRL, 0xb0},
231 {BRIDGE, M5602_XB_ADC_CTRL, 0xc0},
232 {BRIDGE, M5602_XB_SENSOR_TYPE, 0x0c},
233 {BRIDGE, M5602_XB_GPIO_DIR, 0x05},
234 {BRIDGE, M5602_XB_GPIO_DAT, 0x00},
235 {BRIDGE, M5602_XB_GPIO_EN_H, 0x06},
236 {BRIDGE, M5602_XB_GPIO_EN_L, 0x00},
237
238 {BRIDGE, M5602_XB_SEN_CLK_DIV, 0x06},
239 {BRIDGE, M5602_XB_SEN_CLK_CTRL, 0xb0},
240 {BRIDGE, M5602_XB_ADC_CTRL, 0xc0},
241 {BRIDGE, M5602_XB_SENSOR_TYPE, 0x0c},
242 {BRIDGE, M5602_XB_LINE_OF_FRAME_H, 0x81},
243 {BRIDGE, M5602_XB_PIX_OF_LINE_H, 0x82},
244 {BRIDGE, M5602_XB_SIG_INI, 0x01},
245 {BRIDGE, M5602_XB_VSYNC_PARA, 0x00},
246 {BRIDGE, M5602_XB_VSYNC_PARA, 0x08},
247 {BRIDGE, M5602_XB_VSYNC_PARA, 0x00},
248 {BRIDGE, M5602_XB_VSYNC_PARA, 0x00},
249 {BRIDGE, M5602_XB_VSYNC_PARA, 0x01},
250 {BRIDGE, M5602_XB_VSYNC_PARA, 0xec},
251 {BRIDGE, M5602_XB_VSYNC_PARA, 0x00},
252 {BRIDGE, M5602_XB_VSYNC_PARA, 0x00},
253 {BRIDGE, M5602_XB_SIG_INI, 0x00},
254 {BRIDGE, M5602_XB_SIG_INI, 0x02},
255 {BRIDGE, M5602_XB_HSYNC_PARA, 0x00},
256 {BRIDGE, M5602_XB_HSYNC_PARA, 0x27},
257 {BRIDGE, M5602_XB_HSYNC_PARA, 0x02},
258 {BRIDGE, M5602_XB_HSYNC_PARA, 0xa7},
259 {BRIDGE, M5602_XB_SIG_INI, 0x00},
260 {BRIDGE, M5602_XB_SEN_CLK_DIV, 0x00},
261 {BRIDGE, M5602_XB_SEN_CLK_CTRL, 0xb0},
262};
263#endif 110#endif
diff --git a/drivers/media/usb/gspca/m5602/m5602_ov9650.c b/drivers/media/usb/gspca/m5602/m5602_ov9650.c
index 59bc62bfae26..4544d3a1ad58 100644
--- a/drivers/media/usb/gspca/m5602/m5602_ov9650.c
+++ b/drivers/media/usb/gspca/m5602/m5602_ov9650.c
@@ -1,3 +1,4 @@
1
1/* 2/*
2 * Driver for the ov9650 sensor 3 * Driver for the ov9650 sensor
3 * 4 *
@@ -23,6 +24,157 @@
23static int ov9650_s_ctrl(struct v4l2_ctrl *ctrl); 24static int ov9650_s_ctrl(struct v4l2_ctrl *ctrl);
24static void ov9650_dump_registers(struct sd *sd); 25static void ov9650_dump_registers(struct sd *sd);
25 26
27static const unsigned char preinit_ov9650[][3] = {
28 /* [INITCAM] */
29 {BRIDGE, M5602_XB_MCU_CLK_DIV, 0x02},
30 {BRIDGE, M5602_XB_MCU_CLK_CTRL, 0xb0},
31 {BRIDGE, M5602_XB_SEN_CLK_DIV, 0x00},
32 {BRIDGE, M5602_XB_SEN_CLK_CTRL, 0xb0},
33 {BRIDGE, M5602_XB_ADC_CTRL, 0xc0},
34 {BRIDGE, M5602_XB_SENSOR_CTRL, 0x00},
35
36 {BRIDGE, M5602_XB_SENSOR_TYPE, 0x08},
37 {BRIDGE, M5602_XB_GPIO_DIR, 0x05},
38 {BRIDGE, M5602_XB_GPIO_DAT, 0x04},
39 {BRIDGE, M5602_XB_GPIO_EN_H, 0x06},
40 {BRIDGE, M5602_XB_GPIO_DIR_H, 0x06},
41 {BRIDGE, M5602_XB_GPIO_DAT_H, 0x00},
42 {BRIDGE, M5602_XB_GPIO_DAT, 0x00},
43 {BRIDGE, M5602_XB_I2C_CLK_DIV, 0x0a},
44 /* Reset chip */
45 {SENSOR, OV9650_COM7, OV9650_REGISTER_RESET},
46 /* Enable double clock */
47 {SENSOR, OV9650_CLKRC, 0x80},
48 /* Do something out of spec with the power */
49 {SENSOR, OV9650_OFON, 0x40}
50};
51
52static const unsigned char init_ov9650[][3] = {
53 /* [INITCAM] */
54 {BRIDGE, M5602_XB_MCU_CLK_DIV, 0x02},
55 {BRIDGE, M5602_XB_MCU_CLK_CTRL, 0xb0},
56 {BRIDGE, M5602_XB_SEN_CLK_DIV, 0x00},
57 {BRIDGE, M5602_XB_SEN_CLK_CTRL, 0xb0},
58 {BRIDGE, M5602_XB_ADC_CTRL, 0xc0},
59 {BRIDGE, M5602_XB_SENSOR_CTRL, 0x00},
60
61 {BRIDGE, M5602_XB_SENSOR_TYPE, 0x08},
62 {BRIDGE, M5602_XB_GPIO_DIR, 0x05},
63 {BRIDGE, M5602_XB_GPIO_DAT, 0x04},
64 {BRIDGE, M5602_XB_GPIO_EN_H, 0x06},
65 {BRIDGE, M5602_XB_GPIO_DIR_H, 0x06},
66 {BRIDGE, M5602_XB_GPIO_DAT_H, 0x00},
67 {BRIDGE, M5602_XB_GPIO_DAT, 0x00},
68 {BRIDGE, M5602_XB_I2C_CLK_DIV, 0x0a},
69
70 /* Reset chip */
71 {SENSOR, OV9650_COM7, OV9650_REGISTER_RESET},
72 /* One extra reset is needed in order to make the sensor behave
73 properly when resuming from ram, could be a timing issue */
74 {SENSOR, OV9650_COM7, OV9650_REGISTER_RESET},
75
76 /* Enable double clock */
77 {SENSOR, OV9650_CLKRC, 0x80},
78 /* Do something out of spec with the power */
79 {SENSOR, OV9650_OFON, 0x40},
80
81 /* Set fast AGC/AEC algorithm with unlimited step size */
82 {SENSOR, OV9650_COM8, OV9650_FAST_AGC_AEC |
83 OV9650_AEC_UNLIM_STEP_SIZE},
84
85 {SENSOR, OV9650_CHLF, 0x10},
86 {SENSOR, OV9650_ARBLM, 0xbf},
87 {SENSOR, OV9650_ACOM38, 0x81},
88 /* Turn off color matrix coefficient double option */
89 {SENSOR, OV9650_COM16, 0x00},
90 /* Enable color matrix for RGB/YUV, Delay Y channel,
91 set output Y/UV delay to 1 */
92 {SENSOR, OV9650_COM13, 0x19},
93 /* Enable digital BLC, Set output mode to U Y V Y */
94 {SENSOR, OV9650_TSLB, 0x0c},
95 /* Limit the AGC/AEC stable upper region */
96 {SENSOR, OV9650_COM24, 0x00},
97 /* Enable HREF and some out of spec things */
98 {SENSOR, OV9650_COM12, 0x73},
99 /* Set all DBLC offset signs to positive and
100 do some out of spec stuff */
101 {SENSOR, OV9650_DBLC1, 0xdf},
102 {SENSOR, OV9650_COM21, 0x06},
103 {SENSOR, OV9650_RSVD35, 0x91},
104 /* Necessary, no camera stream without it */
105 {SENSOR, OV9650_RSVD16, 0x06},
106 {SENSOR, OV9650_RSVD94, 0x99},
107 {SENSOR, OV9650_RSVD95, 0x99},
108 {SENSOR, OV9650_RSVD96, 0x04},
109 /* Enable full range output */
110 {SENSOR, OV9650_COM15, 0x0},
111 /* Enable HREF at optical black, enable ADBLC bias,
112 enable ADBLC, reset timings at format change */
113 {SENSOR, OV9650_COM6, 0x4b},
114 /* Subtract 32 from the B channel bias */
115 {SENSOR, OV9650_BBIAS, 0xa0},
116 /* Subtract 32 from the Gb channel bias */
117 {SENSOR, OV9650_GbBIAS, 0xa0},
118 /* Do not bypass the analog BLC and to some out of spec stuff */
119 {SENSOR, OV9650_Gr_COM, 0x00},
120 /* Subtract 32 from the R channel bias */
121 {SENSOR, OV9650_RBIAS, 0xa0},
122 /* Subtract 32 from the R channel bias */
123 {SENSOR, OV9650_RBIAS, 0x0},
124 {SENSOR, OV9650_COM26, 0x80},
125 {SENSOR, OV9650_ACOMA9, 0x98},
126 /* Set the AGC/AEC stable region upper limit */
127 {SENSOR, OV9650_AEW, 0x68},
128 /* Set the AGC/AEC stable region lower limit */
129 {SENSOR, OV9650_AEB, 0x5c},
130 /* Set the high and low limit nibbles to 3 */
131 {SENSOR, OV9650_VPT, 0xc3},
132 /* Set the Automatic Gain Ceiling (AGC) to 128x,
133 drop VSYNC at frame drop,
134 limit exposure timing,
135 drop frame when the AEC step is larger than the exposure gap */
136 {SENSOR, OV9650_COM9, 0x6e},
137 /* Set VSYNC negative, Set RESET to SLHS (slave mode horizontal sync)
138 and set PWDN to SLVS (slave mode vertical sync) */
139 {SENSOR, OV9650_COM10, 0x42},
140 /* Set horizontal column start high to default value */
141 {SENSOR, OV9650_HSTART, 0x1a}, /* 210 */
142 /* Set horizontal column end */
143 {SENSOR, OV9650_HSTOP, 0xbf}, /* 1534 */
144 /* Complementing register to the two writes above */
145 {SENSOR, OV9650_HREF, 0xb2},
146 /* Set vertical row start high bits */
147 {SENSOR, OV9650_VSTRT, 0x02},
148 /* Set vertical row end low bits */
149 {SENSOR, OV9650_VSTOP, 0x7e},
150 /* Set complementing vertical frame control */
151 {SENSOR, OV9650_VREF, 0x10},
152 {SENSOR, OV9650_ADC, 0x04},
153 {SENSOR, OV9650_HV, 0x40},
154
155 /* Enable denoise, and white-pixel erase */
156 {SENSOR, OV9650_COM22, OV9650_DENOISE_ENABLE |
157 OV9650_WHITE_PIXEL_ENABLE |
158 OV9650_WHITE_PIXEL_OPTION},
159
160 /* Enable VARIOPIXEL */
161 {SENSOR, OV9650_COM3, OV9650_VARIOPIXEL},
162 {SENSOR, OV9650_COM4, OV9650_QVGA_VARIOPIXEL},
163
164 /* Put the sensor in soft sleep mode */
165 {SENSOR, OV9650_COM2, OV9650_SOFT_SLEEP | OV9650_OUTPUT_DRIVE_2X},
166};
167
168static const unsigned char res_init_ov9650[][3] = {
169 {SENSOR, OV9650_COM2, OV9650_OUTPUT_DRIVE_2X},
170
171 {BRIDGE, M5602_XB_LINE_OF_FRAME_H, 0x82},
172 {BRIDGE, M5602_XB_LINE_OF_FRAME_L, 0x00},
173 {BRIDGE, M5602_XB_PIX_OF_LINE_H, 0x82},
174 {BRIDGE, M5602_XB_PIX_OF_LINE_L, 0x00},
175 {BRIDGE, M5602_XB_SIG_INI, 0x01}
176};
177
26/* Vertically and horizontally flips the image if matched, needed for machines 178/* Vertically and horizontally flips the image if matched, needed for machines
27 where the sensor is mounted upside down */ 179 where the sensor is mounted upside down */
28static 180static
diff --git a/drivers/media/usb/gspca/m5602/m5602_ov9650.h b/drivers/media/usb/gspca/m5602/m5602_ov9650.h
index f9f5870da60f..ce3db062c740 100644
--- a/drivers/media/usb/gspca/m5602/m5602_ov9650.h
+++ b/drivers/media/usb/gspca/m5602/m5602_ov9650.h
@@ -156,154 +156,4 @@ static const struct m5602_sensor ov9650 = {
156 .disconnect = ov9650_disconnect, 156 .disconnect = ov9650_disconnect,
157}; 157};
158 158
159static const unsigned char preinit_ov9650[][3] = {
160 /* [INITCAM] */
161 {BRIDGE, M5602_XB_MCU_CLK_DIV, 0x02},
162 {BRIDGE, M5602_XB_MCU_CLK_CTRL, 0xb0},
163 {BRIDGE, M5602_XB_SEN_CLK_DIV, 0x00},
164 {BRIDGE, M5602_XB_SEN_CLK_CTRL, 0xb0},
165 {BRIDGE, M5602_XB_ADC_CTRL, 0xc0},
166 {BRIDGE, M5602_XB_SENSOR_CTRL, 0x00},
167
168 {BRIDGE, M5602_XB_SENSOR_TYPE, 0x08},
169 {BRIDGE, M5602_XB_GPIO_DIR, 0x05},
170 {BRIDGE, M5602_XB_GPIO_DAT, 0x04},
171 {BRIDGE, M5602_XB_GPIO_EN_H, 0x06},
172 {BRIDGE, M5602_XB_GPIO_DIR_H, 0x06},
173 {BRIDGE, M5602_XB_GPIO_DAT_H, 0x00},
174 {BRIDGE, M5602_XB_GPIO_DAT, 0x00},
175 {BRIDGE, M5602_XB_I2C_CLK_DIV, 0x0a},
176 /* Reset chip */
177 {SENSOR, OV9650_COM7, OV9650_REGISTER_RESET},
178 /* Enable double clock */
179 {SENSOR, OV9650_CLKRC, 0x80},
180 /* Do something out of spec with the power */
181 {SENSOR, OV9650_OFON, 0x40}
182};
183
184static const unsigned char init_ov9650[][3] = {
185 /* [INITCAM] */
186 {BRIDGE, M5602_XB_MCU_CLK_DIV, 0x02},
187 {BRIDGE, M5602_XB_MCU_CLK_CTRL, 0xb0},
188 {BRIDGE, M5602_XB_SEN_CLK_DIV, 0x00},
189 {BRIDGE, M5602_XB_SEN_CLK_CTRL, 0xb0},
190 {BRIDGE, M5602_XB_ADC_CTRL, 0xc0},
191 {BRIDGE, M5602_XB_SENSOR_CTRL, 0x00},
192
193 {BRIDGE, M5602_XB_SENSOR_TYPE, 0x08},
194 {BRIDGE, M5602_XB_GPIO_DIR, 0x05},
195 {BRIDGE, M5602_XB_GPIO_DAT, 0x04},
196 {BRIDGE, M5602_XB_GPIO_EN_H, 0x06},
197 {BRIDGE, M5602_XB_GPIO_DIR_H, 0x06},
198 {BRIDGE, M5602_XB_GPIO_DAT_H, 0x00},
199 {BRIDGE, M5602_XB_GPIO_DAT, 0x00},
200 {BRIDGE, M5602_XB_I2C_CLK_DIV, 0x0a},
201
202 /* Reset chip */
203 {SENSOR, OV9650_COM7, OV9650_REGISTER_RESET},
204 /* One extra reset is needed in order to make the sensor behave
205 properly when resuming from ram, could be a timing issue */
206 {SENSOR, OV9650_COM7, OV9650_REGISTER_RESET},
207
208 /* Enable double clock */
209 {SENSOR, OV9650_CLKRC, 0x80},
210 /* Do something out of spec with the power */
211 {SENSOR, OV9650_OFON, 0x40},
212
213 /* Set fast AGC/AEC algorithm with unlimited step size */
214 {SENSOR, OV9650_COM8, OV9650_FAST_AGC_AEC |
215 OV9650_AEC_UNLIM_STEP_SIZE},
216
217 {SENSOR, OV9650_CHLF, 0x10},
218 {SENSOR, OV9650_ARBLM, 0xbf},
219 {SENSOR, OV9650_ACOM38, 0x81},
220 /* Turn off color matrix coefficient double option */
221 {SENSOR, OV9650_COM16, 0x00},
222 /* Enable color matrix for RGB/YUV, Delay Y channel,
223 set output Y/UV delay to 1 */
224 {SENSOR, OV9650_COM13, 0x19},
225 /* Enable digital BLC, Set output mode to U Y V Y */
226 {SENSOR, OV9650_TSLB, 0x0c},
227 /* Limit the AGC/AEC stable upper region */
228 {SENSOR, OV9650_COM24, 0x00},
229 /* Enable HREF and some out of spec things */
230 {SENSOR, OV9650_COM12, 0x73},
231 /* Set all DBLC offset signs to positive and
232 do some out of spec stuff */
233 {SENSOR, OV9650_DBLC1, 0xdf},
234 {SENSOR, OV9650_COM21, 0x06},
235 {SENSOR, OV9650_RSVD35, 0x91},
236 /* Necessary, no camera stream without it */
237 {SENSOR, OV9650_RSVD16, 0x06},
238 {SENSOR, OV9650_RSVD94, 0x99},
239 {SENSOR, OV9650_RSVD95, 0x99},
240 {SENSOR, OV9650_RSVD96, 0x04},
241 /* Enable full range output */
242 {SENSOR, OV9650_COM15, 0x0},
243 /* Enable HREF at optical black, enable ADBLC bias,
244 enable ADBLC, reset timings at format change */
245 {SENSOR, OV9650_COM6, 0x4b},
246 /* Subtract 32 from the B channel bias */
247 {SENSOR, OV9650_BBIAS, 0xa0},
248 /* Subtract 32 from the Gb channel bias */
249 {SENSOR, OV9650_GbBIAS, 0xa0},
250 /* Do not bypass the analog BLC and to some out of spec stuff */
251 {SENSOR, OV9650_Gr_COM, 0x00},
252 /* Subtract 32 from the R channel bias */
253 {SENSOR, OV9650_RBIAS, 0xa0},
254 /* Subtract 32 from the R channel bias */
255 {SENSOR, OV9650_RBIAS, 0x0},
256 {SENSOR, OV9650_COM26, 0x80},
257 {SENSOR, OV9650_ACOMA9, 0x98},
258 /* Set the AGC/AEC stable region upper limit */
259 {SENSOR, OV9650_AEW, 0x68},
260 /* Set the AGC/AEC stable region lower limit */
261 {SENSOR, OV9650_AEB, 0x5c},
262 /* Set the high and low limit nibbles to 3 */
263 {SENSOR, OV9650_VPT, 0xc3},
264 /* Set the Automatic Gain Ceiling (AGC) to 128x,
265 drop VSYNC at frame drop,
266 limit exposure timing,
267 drop frame when the AEC step is larger than the exposure gap */
268 {SENSOR, OV9650_COM9, 0x6e},
269 /* Set VSYNC negative, Set RESET to SLHS (slave mode horizontal sync)
270 and set PWDN to SLVS (slave mode vertical sync) */
271 {SENSOR, OV9650_COM10, 0x42},
272 /* Set horizontal column start high to default value */
273 {SENSOR, OV9650_HSTART, 0x1a}, /* 210 */
274 /* Set horizontal column end */
275 {SENSOR, OV9650_HSTOP, 0xbf}, /* 1534 */
276 /* Complementing register to the two writes above */
277 {SENSOR, OV9650_HREF, 0xb2},
278 /* Set vertical row start high bits */
279 {SENSOR, OV9650_VSTRT, 0x02},
280 /* Set vertical row end low bits */
281 {SENSOR, OV9650_VSTOP, 0x7e},
282 /* Set complementing vertical frame control */
283 {SENSOR, OV9650_VREF, 0x10},
284 {SENSOR, OV9650_ADC, 0x04},
285 {SENSOR, OV9650_HV, 0x40},
286
287 /* Enable denoise, and white-pixel erase */
288 {SENSOR, OV9650_COM22, OV9650_DENOISE_ENABLE |
289 OV9650_WHITE_PIXEL_ENABLE |
290 OV9650_WHITE_PIXEL_OPTION},
291
292 /* Enable VARIOPIXEL */
293 {SENSOR, OV9650_COM3, OV9650_VARIOPIXEL},
294 {SENSOR, OV9650_COM4, OV9650_QVGA_VARIOPIXEL},
295
296 /* Put the sensor in soft sleep mode */
297 {SENSOR, OV9650_COM2, OV9650_SOFT_SLEEP | OV9650_OUTPUT_DRIVE_2X},
298};
299
300static const unsigned char res_init_ov9650[][3] = {
301 {SENSOR, OV9650_COM2, OV9650_OUTPUT_DRIVE_2X},
302
303 {BRIDGE, M5602_XB_LINE_OF_FRAME_H, 0x82},
304 {BRIDGE, M5602_XB_LINE_OF_FRAME_L, 0x00},
305 {BRIDGE, M5602_XB_PIX_OF_LINE_H, 0x82},
306 {BRIDGE, M5602_XB_PIX_OF_LINE_L, 0x00},
307 {BRIDGE, M5602_XB_SIG_INI, 0x01}
308};
309#endif 159#endif
diff --git a/drivers/media/usb/gspca/m5602/m5602_po1030.c b/drivers/media/usb/gspca/m5602/m5602_po1030.c
index 4bf5c43424b7..a0a90dd34ca8 100644
--- a/drivers/media/usb/gspca/m5602/m5602_po1030.c
+++ b/drivers/media/usb/gspca/m5602/m5602_po1030.c
@@ -23,6 +23,110 @@
23static int po1030_s_ctrl(struct v4l2_ctrl *ctrl); 23static int po1030_s_ctrl(struct v4l2_ctrl *ctrl);
24static void po1030_dump_registers(struct sd *sd); 24static void po1030_dump_registers(struct sd *sd);
25 25
26static const unsigned char preinit_po1030[][3] = {
27 {BRIDGE, M5602_XB_MCU_CLK_DIV, 0x02},
28 {BRIDGE, M5602_XB_MCU_CLK_CTRL, 0xb0},
29 {BRIDGE, M5602_XB_SEN_CLK_DIV, 0x00},
30 {BRIDGE, M5602_XB_SEN_CLK_CTRL, 0xb0},
31 {BRIDGE, M5602_XB_ADC_CTRL, 0xc0},
32 {BRIDGE, M5602_XB_SENSOR_CTRL, 0x00},
33 {BRIDGE, M5602_XB_SENSOR_TYPE, 0x0c},
34 {BRIDGE, M5602_XB_ADC_CTRL, 0xc0},
35 {BRIDGE, M5602_XB_GPIO_DIR, 0x05},
36 {BRIDGE, M5602_XB_GPIO_DAT, 0x04},
37 {BRIDGE, M5602_XB_GPIO_EN_H, 0x06},
38 {BRIDGE, M5602_XB_GPIO_DIR_H, 0x06},
39 {BRIDGE, M5602_XB_GPIO_DAT_H, 0x02},
40
41 {SENSOR, PO1030_AUTOCTRL2, PO1030_SENSOR_RESET | (1 << 2)},
42
43 {BRIDGE, M5602_XB_SEN_CLK_DIV, 0x04},
44 {BRIDGE, M5602_XB_SEN_CLK_CTRL, 0xb0},
45 {BRIDGE, M5602_XB_SEN_CLK_DIV, 0x00},
46 {BRIDGE, M5602_XB_SEN_CLK_CTRL, 0xb0},
47 {BRIDGE, M5602_XB_SENSOR_TYPE, 0x0c},
48 {BRIDGE, M5602_XB_GPIO_DIR, 0x05},
49 {BRIDGE, M5602_XB_GPIO_DAT, 0x00}
50};
51
52static const unsigned char init_po1030[][3] = {
53 {BRIDGE, M5602_XB_MCU_CLK_DIV, 0x02},
54 {BRIDGE, M5602_XB_MCU_CLK_CTRL, 0xb0},
55 {BRIDGE, M5602_XB_SEN_CLK_DIV, 0x00},
56 {BRIDGE, M5602_XB_SEN_CLK_CTRL, 0xb0},
57 {BRIDGE, M5602_XB_ADC_CTRL, 0xc0},
58 {BRIDGE, M5602_XB_SENSOR_CTRL, 0x00},
59 {BRIDGE, M5602_XB_SENSOR_TYPE, 0x0c},
60
61 {SENSOR, PO1030_AUTOCTRL2, PO1030_SENSOR_RESET | (1 << 2)},
62
63 {BRIDGE, M5602_XB_GPIO_DIR, 0x05},
64 {BRIDGE, M5602_XB_GPIO_DAT, 0x04},
65 {BRIDGE, M5602_XB_GPIO_EN_H, 0x06},
66 {BRIDGE, M5602_XB_GPIO_EN_L, 0x00},
67 {BRIDGE, M5602_XB_GPIO_DIR_H, 0x06},
68 {BRIDGE, M5602_XB_GPIO_DAT_H, 0x02},
69 {BRIDGE, M5602_XB_SEN_CLK_DIV, 0x04},
70 {BRIDGE, M5602_XB_SEN_CLK_CTRL, 0xb0},
71 {BRIDGE, M5602_XB_GPIO_DIR, 0x05},
72 {BRIDGE, M5602_XB_GPIO_DAT, 0x00},
73
74 {SENSOR, PO1030_AUTOCTRL2, 0x04},
75
76 {SENSOR, PO1030_OUTFORMCTRL2, PO1030_RAW_RGB_BAYER},
77 {SENSOR, PO1030_AUTOCTRL1, PO1030_WEIGHT_WIN_2X},
78
79 {SENSOR, PO1030_CONTROL2, 0x03},
80 {SENSOR, 0x21, 0x90},
81 {SENSOR, PO1030_YTARGET, 0x60},
82 {SENSOR, 0x59, 0x13},
83 {SENSOR, PO1030_OUTFORMCTRL1, PO1030_HREF_ENABLE},
84 {SENSOR, PO1030_EDGE_ENH_OFF, 0x00},
85 {SENSOR, PO1030_EGA, 0x80},
86 {SENSOR, 0x78, 0x14},
87 {SENSOR, 0x6f, 0x01},
88 {SENSOR, PO1030_GLOBALGAINMAX, 0x14},
89 {SENSOR, PO1030_Cb_U_GAIN, 0x38},
90 {SENSOR, PO1030_Cr_V_GAIN, 0x38},
91 {SENSOR, PO1030_CONTROL1, PO1030_SHUTTER_MODE |
92 PO1030_AUTO_SUBSAMPLING |
93 PO1030_FRAME_EQUAL},
94 {SENSOR, PO1030_GC0, 0x10},
95 {SENSOR, PO1030_GC1, 0x20},
96 {SENSOR, PO1030_GC2, 0x40},
97 {SENSOR, PO1030_GC3, 0x60},
98 {SENSOR, PO1030_GC4, 0x80},
99 {SENSOR, PO1030_GC5, 0xa0},
100 {SENSOR, PO1030_GC6, 0xc0},
101 {SENSOR, PO1030_GC7, 0xff},
102
103 /* Set the width to 751 */
104 {SENSOR, PO1030_FRAMEWIDTH_H, 0x02},
105 {SENSOR, PO1030_FRAMEWIDTH_L, 0xef},
106
107 /* Set the height to 540 */
108 {SENSOR, PO1030_FRAMEHEIGHT_H, 0x02},
109 {SENSOR, PO1030_FRAMEHEIGHT_L, 0x1c},
110
111 /* Set the x window to 1 */
112 {SENSOR, PO1030_WINDOWX_H, 0x00},
113 {SENSOR, PO1030_WINDOWX_L, 0x01},
114
115 /* Set the y window to 1 */
116 {SENSOR, PO1030_WINDOWY_H, 0x00},
117 {SENSOR, PO1030_WINDOWY_L, 0x01},
118
119 /* with a very low lighted environment increase the exposure but
120 * decrease the FPS (Frame Per Second) */
121 {BRIDGE, M5602_XB_SEN_CLK_DIV, 0x00},
122 {BRIDGE, M5602_XB_SEN_CLK_CTRL, 0xb0},
123
124 {BRIDGE, M5602_XB_GPIO_DIR, 0x05},
125 {BRIDGE, M5602_XB_GPIO_DAT, 0x00},
126 {BRIDGE, M5602_XB_GPIO_EN_H, 0x06},
127 {BRIDGE, M5602_XB_GPIO_EN_L, 0x00},
128};
129
26static struct v4l2_pix_format po1030_modes[] = { 130static struct v4l2_pix_format po1030_modes[] = {
27 { 131 {
28 640, 132 640,
diff --git a/drivers/media/usb/gspca/m5602/m5602_po1030.h b/drivers/media/usb/gspca/m5602/m5602_po1030.h
index a6ab76149bd0..981a91aa7450 100644
--- a/drivers/media/usb/gspca/m5602/m5602_po1030.h
+++ b/drivers/media/usb/gspca/m5602/m5602_po1030.h
@@ -167,108 +167,4 @@ static const struct m5602_sensor po1030 = {
167 .start = po1030_start, 167 .start = po1030_start,
168 .disconnect = po1030_disconnect, 168 .disconnect = po1030_disconnect,
169}; 169};
170
171static const unsigned char preinit_po1030[][3] = {
172 {BRIDGE, M5602_XB_MCU_CLK_DIV, 0x02},
173 {BRIDGE, M5602_XB_MCU_CLK_CTRL, 0xb0},
174 {BRIDGE, M5602_XB_SEN_CLK_DIV, 0x00},
175 {BRIDGE, M5602_XB_SEN_CLK_CTRL, 0xb0},
176 {BRIDGE, M5602_XB_ADC_CTRL, 0xc0},
177 {BRIDGE, M5602_XB_SENSOR_CTRL, 0x00},
178 {BRIDGE, M5602_XB_SENSOR_TYPE, 0x0c},
179 {BRIDGE, M5602_XB_ADC_CTRL, 0xc0},
180 {BRIDGE, M5602_XB_GPIO_DIR, 0x05},
181 {BRIDGE, M5602_XB_GPIO_DAT, 0x04},
182 {BRIDGE, M5602_XB_GPIO_EN_H, 0x06},
183 {BRIDGE, M5602_XB_GPIO_DIR_H, 0x06},
184 {BRIDGE, M5602_XB_GPIO_DAT_H, 0x02},
185
186 {SENSOR, PO1030_AUTOCTRL2, PO1030_SENSOR_RESET | (1 << 2)},
187
188 {BRIDGE, M5602_XB_SEN_CLK_DIV, 0x04},
189 {BRIDGE, M5602_XB_SEN_CLK_CTRL, 0xb0},
190 {BRIDGE, M5602_XB_SEN_CLK_DIV, 0x00},
191 {BRIDGE, M5602_XB_SEN_CLK_CTRL, 0xb0},
192 {BRIDGE, M5602_XB_SENSOR_TYPE, 0x0c},
193 {BRIDGE, M5602_XB_GPIO_DIR, 0x05},
194 {BRIDGE, M5602_XB_GPIO_DAT, 0x00}
195};
196
197static const unsigned char init_po1030[][3] = {
198 {BRIDGE, M5602_XB_MCU_CLK_DIV, 0x02},
199 {BRIDGE, M5602_XB_MCU_CLK_CTRL, 0xb0},
200 {BRIDGE, M5602_XB_SEN_CLK_DIV, 0x00},
201 {BRIDGE, M5602_XB_SEN_CLK_CTRL, 0xb0},
202 {BRIDGE, M5602_XB_ADC_CTRL, 0xc0},
203 {BRIDGE, M5602_XB_SENSOR_CTRL, 0x00},
204 {BRIDGE, M5602_XB_SENSOR_TYPE, 0x0c},
205
206 {SENSOR, PO1030_AUTOCTRL2, PO1030_SENSOR_RESET | (1 << 2)},
207
208 {BRIDGE, M5602_XB_GPIO_DIR, 0x05},
209 {BRIDGE, M5602_XB_GPIO_DAT, 0x04},
210 {BRIDGE, M5602_XB_GPIO_EN_H, 0x06},
211 {BRIDGE, M5602_XB_GPIO_EN_L, 0x00},
212 {BRIDGE, M5602_XB_GPIO_DIR_H, 0x06},
213 {BRIDGE, M5602_XB_GPIO_DAT_H, 0x02},
214 {BRIDGE, M5602_XB_SEN_CLK_DIV, 0x04},
215 {BRIDGE, M5602_XB_SEN_CLK_CTRL, 0xb0},
216 {BRIDGE, M5602_XB_GPIO_DIR, 0x05},
217 {BRIDGE, M5602_XB_GPIO_DAT, 0x00},
218
219 {SENSOR, PO1030_AUTOCTRL2, 0x04},
220
221 {SENSOR, PO1030_OUTFORMCTRL2, PO1030_RAW_RGB_BAYER},
222 {SENSOR, PO1030_AUTOCTRL1, PO1030_WEIGHT_WIN_2X},
223
224 {SENSOR, PO1030_CONTROL2, 0x03},
225 {SENSOR, 0x21, 0x90},
226 {SENSOR, PO1030_YTARGET, 0x60},
227 {SENSOR, 0x59, 0x13},
228 {SENSOR, PO1030_OUTFORMCTRL1, PO1030_HREF_ENABLE},
229 {SENSOR, PO1030_EDGE_ENH_OFF, 0x00},
230 {SENSOR, PO1030_EGA, 0x80},
231 {SENSOR, 0x78, 0x14},
232 {SENSOR, 0x6f, 0x01},
233 {SENSOR, PO1030_GLOBALGAINMAX, 0x14},
234 {SENSOR, PO1030_Cb_U_GAIN, 0x38},
235 {SENSOR, PO1030_Cr_V_GAIN, 0x38},
236 {SENSOR, PO1030_CONTROL1, PO1030_SHUTTER_MODE |
237 PO1030_AUTO_SUBSAMPLING |
238 PO1030_FRAME_EQUAL},
239 {SENSOR, PO1030_GC0, 0x10},
240 {SENSOR, PO1030_GC1, 0x20},
241 {SENSOR, PO1030_GC2, 0x40},
242 {SENSOR, PO1030_GC3, 0x60},
243 {SENSOR, PO1030_GC4, 0x80},
244 {SENSOR, PO1030_GC5, 0xa0},
245 {SENSOR, PO1030_GC6, 0xc0},
246 {SENSOR, PO1030_GC7, 0xff},
247
248 /* Set the width to 751 */
249 {SENSOR, PO1030_FRAMEWIDTH_H, 0x02},
250 {SENSOR, PO1030_FRAMEWIDTH_L, 0xef},
251
252 /* Set the height to 540 */
253 {SENSOR, PO1030_FRAMEHEIGHT_H, 0x02},
254 {SENSOR, PO1030_FRAMEHEIGHT_L, 0x1c},
255
256 /* Set the x window to 1 */
257 {SENSOR, PO1030_WINDOWX_H, 0x00},
258 {SENSOR, PO1030_WINDOWX_L, 0x01},
259
260 /* Set the y window to 1 */
261 {SENSOR, PO1030_WINDOWY_H, 0x00},
262 {SENSOR, PO1030_WINDOWY_L, 0x01},
263
264 /* with a very low lighted environment increase the exposure but
265 * decrease the FPS (Frame Per Second) */
266 {BRIDGE, M5602_XB_SEN_CLK_DIV, 0x00},
267 {BRIDGE, M5602_XB_SEN_CLK_CTRL, 0xb0},
268
269 {BRIDGE, M5602_XB_GPIO_DIR, 0x05},
270 {BRIDGE, M5602_XB_GPIO_DAT, 0x00},
271 {BRIDGE, M5602_XB_GPIO_EN_H, 0x06},
272 {BRIDGE, M5602_XB_GPIO_EN_L, 0x00},
273};
274#endif 170#endif
diff --git a/drivers/media/usb/gspca/m5602/m5602_s5k4aa.c b/drivers/media/usb/gspca/m5602/m5602_s5k4aa.c
index 7d12599458e2..8447b9c5f8e0 100644
--- a/drivers/media/usb/gspca/m5602/m5602_s5k4aa.c
+++ b/drivers/media/usb/gspca/m5602/m5602_s5k4aa.c
@@ -20,6 +20,205 @@
20 20
21#include "m5602_s5k4aa.h" 21#include "m5602_s5k4aa.h"
22 22
23static const unsigned char preinit_s5k4aa[][4] = {
24 {BRIDGE, M5602_XB_MCU_CLK_DIV, 0x02, 0x00},
25 {BRIDGE, M5602_XB_MCU_CLK_CTRL, 0xb0, 0x00},
26 {BRIDGE, M5602_XB_SEN_CLK_DIV, 0x00, 0x00},
27 {BRIDGE, M5602_XB_SEN_CLK_CTRL, 0xb0, 0x00},
28 {BRIDGE, M5602_XB_ADC_CTRL, 0xc0, 0x00},
29 {BRIDGE, M5602_XB_SENSOR_TYPE, 0x0d, 0x00},
30 {BRIDGE, M5602_XB_SENSOR_CTRL, 0x00, 0x00},
31
32 {BRIDGE, M5602_XB_GPIO_DIR, 0x1d, 0x00},
33 {BRIDGE, M5602_XB_GPIO_DAT, 0x08, 0x00},
34 {BRIDGE, M5602_XB_SEN_CLK_DIV, 0xb0, 0x00},
35 {BRIDGE, M5602_XB_SEN_CLK_CTRL, 0x80, 0x00},
36 {BRIDGE, M5602_XB_GPIO_EN_H, 0x3f, 0x00},
37 {BRIDGE, M5602_XB_GPIO_DIR_H, 0x3f, 0x00},
38 {BRIDGE, M5602_XB_GPIO_DAT_H, 0x00, 0x00},
39 {BRIDGE, M5602_XB_GPIO_DIR, 0x1d, 0x00},
40 {BRIDGE, M5602_XB_GPIO_DAT, 0x00, 0x00},
41 {BRIDGE, M5602_XB_GPIO_EN_L, 0xff, 0x00},
42 {BRIDGE, M5602_XB_GPIO_DIR_L, 0xff, 0x00},
43 {BRIDGE, M5602_XB_GPIO_DAT_L, 0x00, 0x00},
44 {BRIDGE, M5602_XB_SEN_CLK_DIV, 0x00, 0x00},
45 {BRIDGE, M5602_XB_SEN_CLK_CTRL, 0xb0, 0x00},
46 {BRIDGE, M5602_XB_ADC_CTRL, 0xc0, 0x00},
47 {BRIDGE, M5602_XB_SENSOR_TYPE, 0x08, 0x00},
48
49 {BRIDGE, M5602_XB_MCU_CLK_DIV, 0x02, 0x00},
50 {BRIDGE, M5602_XB_MCU_CLK_CTRL, 0xb0, 0x00},
51 {BRIDGE, M5602_XB_GPIO_DIR, 0x1d, 0x00},
52 {BRIDGE, M5602_XB_GPIO_DAT, 0x14, 0x00},
53 {BRIDGE, M5602_XB_SEN_CLK_DIV, 0x00, 0x00},
54 {BRIDGE, M5602_XB_SEN_CLK_CTRL, 0xf0, 0x00},
55 {BRIDGE, M5602_XB_GPIO_DIR, 0x1d, 0x00},
56 {BRIDGE, M5602_XB_GPIO_DAT, 0x1c, 0x00},
57 {BRIDGE, M5602_XB_GPIO_EN_H, 0x06, 0x00},
58 {BRIDGE, M5602_XB_GPIO_DIR_H, 0x06, 0x00},
59 {BRIDGE, M5602_XB_GPIO_DAT_H, 0x00, 0x00},
60 {BRIDGE, M5602_XB_GPIO_EN_L, 0x00, 0x00},
61 {BRIDGE, M5602_XB_I2C_CLK_DIV, 0x20, 0x00},
62
63 {SENSOR, S5K4AA_PAGE_MAP, 0x00, 0x00}
64};
65
66static const unsigned char init_s5k4aa[][4] = {
67 {BRIDGE, M5602_XB_MCU_CLK_DIV, 0x02, 0x00},
68 {BRIDGE, M5602_XB_MCU_CLK_CTRL, 0xb0, 0x00},
69 {BRIDGE, M5602_XB_SEN_CLK_DIV, 0x00, 0x00},
70 {BRIDGE, M5602_XB_SEN_CLK_CTRL, 0xb0, 0x00},
71 {BRIDGE, M5602_XB_ADC_CTRL, 0xc0, 0x00},
72 {BRIDGE, M5602_XB_SENSOR_TYPE, 0x0d, 0x00},
73 {BRIDGE, M5602_XB_SENSOR_CTRL, 0x00, 0x00},
74
75 {BRIDGE, M5602_XB_GPIO_DIR, 0x1d, 0x00},
76 {BRIDGE, M5602_XB_GPIO_DAT, 0x08, 0x00},
77 {BRIDGE, M5602_XB_SEN_CLK_DIV, 0xb0, 0x00},
78 {BRIDGE, M5602_XB_SEN_CLK_CTRL, 0x80, 0x00},
79 {BRIDGE, M5602_XB_GPIO_EN_H, 0x3f, 0x00},
80 {BRIDGE, M5602_XB_GPIO_DIR_H, 0x3f, 0x00},
81 {BRIDGE, M5602_XB_GPIO_DAT_H, 0x00, 0x00},
82 {BRIDGE, M5602_XB_GPIO_DIR, 0x1d, 0x00},
83 {BRIDGE, M5602_XB_GPIO_DAT, 0x00, 0x00},
84 {BRIDGE, M5602_XB_GPIO_EN_L, 0xff, 0x00},
85 {BRIDGE, M5602_XB_GPIO_DIR_L, 0xff, 0x00},
86 {BRIDGE, M5602_XB_GPIO_DAT_L, 0x00, 0x00},
87 {BRIDGE, M5602_XB_SEN_CLK_DIV, 0x00, 0x00},
88 {BRIDGE, M5602_XB_SEN_CLK_CTRL, 0xb0, 0x00},
89 {BRIDGE, M5602_XB_ADC_CTRL, 0xc0, 0x00},
90 {BRIDGE, M5602_XB_SENSOR_TYPE, 0x08, 0x00},
91
92 {BRIDGE, M5602_XB_MCU_CLK_DIV, 0x02, 0x00},
93 {BRIDGE, M5602_XB_MCU_CLK_CTRL, 0xb0, 0x00},
94 {BRIDGE, M5602_XB_GPIO_DIR, 0x1d, 0x00},
95 {BRIDGE, M5602_XB_GPIO_DAT, 0x14, 0x00},
96 {BRIDGE, M5602_XB_SEN_CLK_DIV, 0x00, 0x00},
97 {BRIDGE, M5602_XB_SEN_CLK_CTRL, 0xf0, 0x00},
98 {BRIDGE, M5602_XB_GPIO_DIR, 0x1d, 0x00},
99 {BRIDGE, M5602_XB_GPIO_DAT, 0x1c, 0x00},
100 {BRIDGE, M5602_XB_GPIO_EN_H, 0x06, 0x00},
101 {BRIDGE, M5602_XB_GPIO_DIR_H, 0x06, 0x00},
102 {BRIDGE, M5602_XB_GPIO_DAT_H, 0x00, 0x00},
103 {BRIDGE, M5602_XB_GPIO_EN_L, 0x00, 0x00},
104 {BRIDGE, M5602_XB_I2C_CLK_DIV, 0x20, 0x00},
105
106 {SENSOR, S5K4AA_PAGE_MAP, 0x07, 0x00},
107 {SENSOR, 0x36, 0x01, 0x00},
108 {SENSOR, S5K4AA_PAGE_MAP, 0x00, 0x00},
109 {SENSOR, 0x7b, 0xff, 0x00},
110 {SENSOR, S5K4AA_PAGE_MAP, 0x02, 0x00},
111 {SENSOR, 0x0c, 0x05, 0x00},
112 {SENSOR, 0x02, 0x0e, 0x00},
113 {SENSOR, S5K4AA_READ_MODE, 0xa0, 0x00},
114 {SENSOR, 0x37, 0x00, 0x00},
115};
116
117static const unsigned char VGA_s5k4aa[][4] = {
118 {BRIDGE, M5602_XB_SEN_CLK_DIV, 0x06, 0x00},
119 {BRIDGE, M5602_XB_SEN_CLK_CTRL, 0xb0, 0x00},
120 {BRIDGE, M5602_XB_ADC_CTRL, 0xc0, 0x00},
121 {BRIDGE, M5602_XB_SENSOR_TYPE, 0x08, 0x00},
122 {BRIDGE, M5602_XB_LINE_OF_FRAME_H, 0x81, 0x00},
123 {BRIDGE, M5602_XB_PIX_OF_LINE_H, 0x82, 0x00},
124 {BRIDGE, M5602_XB_SIG_INI, 0x01, 0x00},
125 {BRIDGE, M5602_XB_VSYNC_PARA, 0x00, 0x00},
126 {BRIDGE, M5602_XB_VSYNC_PARA, 0x00, 0x00},
127 {BRIDGE, M5602_XB_VSYNC_PARA, 0x00, 0x00},
128 {BRIDGE, M5602_XB_VSYNC_PARA, 0x00, 0x00},
129 /* VSYNC_PARA, VSYNC_PARA : img height 480 = 0x01e0 */
130 {BRIDGE, M5602_XB_VSYNC_PARA, 0x01, 0x00},
131 {BRIDGE, M5602_XB_VSYNC_PARA, 0xe0, 0x00},
132 {BRIDGE, M5602_XB_VSYNC_PARA, 0x00, 0x00},
133 {BRIDGE, M5602_XB_VSYNC_PARA, 0x00, 0x00},
134 {BRIDGE, M5602_XB_SIG_INI, 0x00, 0x00},
135 {BRIDGE, M5602_XB_SIG_INI, 0x02, 0x00},
136 {BRIDGE, M5602_XB_HSYNC_PARA, 0x00, 0x00},
137 {BRIDGE, M5602_XB_HSYNC_PARA, 0x00, 0x00},
138 /* HSYNC_PARA, HSYNC_PARA : img width 640 = 0x0280 */
139 {BRIDGE, M5602_XB_HSYNC_PARA, 0x02, 0x00},
140 {BRIDGE, M5602_XB_HSYNC_PARA, 0x80, 0x00},
141 {BRIDGE, M5602_XB_SIG_INI, 0x00, 0x00},
142 {BRIDGE, M5602_XB_SEN_CLK_DIV, 0x00, 0x00},
143 {BRIDGE, M5602_XB_SEN_CLK_CTRL, 0xa0, 0x00}, /* 48 MHz */
144
145 {SENSOR, S5K4AA_PAGE_MAP, 0x02, 0x00},
146 {SENSOR, S5K4AA_READ_MODE, S5K4AA_RM_H_FLIP | S5K4AA_RM_ROW_SKIP_2X
147 | S5K4AA_RM_COL_SKIP_2X, 0x00},
148 /* 0x37 : Fix image stability when light is too bright and improves
149 * image quality in 640x480, but worsens it in 1280x1024 */
150 {SENSOR, 0x37, 0x01, 0x00},
151 /* ROWSTART_HI, ROWSTART_LO : 10 + (1024-960)/2 = 42 = 0x002a */
152 {SENSOR, S5K4AA_ROWSTART_HI, 0x00, 0x00},
153 {SENSOR, S5K4AA_ROWSTART_LO, 0x29, 0x00},
154 {SENSOR, S5K4AA_COLSTART_HI, 0x00, 0x00},
155 {SENSOR, S5K4AA_COLSTART_LO, 0x0c, 0x00},
156 /* window_height_hi, window_height_lo : 960 = 0x03c0 */
157 {SENSOR, S5K4AA_WINDOW_HEIGHT_HI, 0x03, 0x00},
158 {SENSOR, S5K4AA_WINDOW_HEIGHT_LO, 0xc0, 0x00},
159 /* window_width_hi, window_width_lo : 1280 = 0x0500 */
160 {SENSOR, S5K4AA_WINDOW_WIDTH_HI, 0x05, 0x00},
161 {SENSOR, S5K4AA_WINDOW_WIDTH_LO, 0x00, 0x00},
162 {SENSOR, S5K4AA_H_BLANK_HI__, 0x00, 0x00},
163 {SENSOR, S5K4AA_H_BLANK_LO__, 0xa8, 0x00}, /* helps to sync... */
164 {SENSOR, S5K4AA_EXPOSURE_HI, 0x01, 0x00},
165 {SENSOR, S5K4AA_EXPOSURE_LO, 0x00, 0x00},
166 {SENSOR, 0x11, 0x04, 0x00},
167 {SENSOR, 0x12, 0xc3, 0x00},
168 {SENSOR, S5K4AA_PAGE_MAP, 0x02, 0x00},
169 {SENSOR, 0x02, 0x0e, 0x00},
170};
171
172static const unsigned char SXGA_s5k4aa[][4] = {
173 {BRIDGE, M5602_XB_SEN_CLK_DIV, 0x06, 0x00},
174 {BRIDGE, M5602_XB_SEN_CLK_CTRL, 0xb0, 0x00},
175 {BRIDGE, M5602_XB_ADC_CTRL, 0xc0, 0x00},
176 {BRIDGE, M5602_XB_SENSOR_TYPE, 0x08, 0x00},
177 {BRIDGE, M5602_XB_LINE_OF_FRAME_H, 0x81, 0x00},
178 {BRIDGE, M5602_XB_PIX_OF_LINE_H, 0x82, 0x00},
179 {BRIDGE, M5602_XB_SIG_INI, 0x01, 0x00},
180 {BRIDGE, M5602_XB_VSYNC_PARA, 0x00, 0x00},
181 {BRIDGE, M5602_XB_VSYNC_PARA, 0x00, 0x00},
182 {BRIDGE, M5602_XB_VSYNC_PARA, 0x00, 0x00},
183 {BRIDGE, M5602_XB_VSYNC_PARA, 0x00, 0x00},
184 /* VSYNC_PARA, VSYNC_PARA : img height 1024 = 0x0400 */
185 {BRIDGE, M5602_XB_VSYNC_PARA, 0x04, 0x00},
186 {BRIDGE, M5602_XB_VSYNC_PARA, 0x00, 0x00},
187 {BRIDGE, M5602_XB_VSYNC_PARA, 0x00, 0x00},
188 {BRIDGE, M5602_XB_VSYNC_PARA, 0x00, 0x00},
189 {BRIDGE, M5602_XB_SIG_INI, 0x00, 0x00},
190 {BRIDGE, M5602_XB_SIG_INI, 0x02, 0x00},
191 {BRIDGE, M5602_XB_HSYNC_PARA, 0x00, 0x00},
192 {BRIDGE, M5602_XB_HSYNC_PARA, 0x00, 0x00},
193 /* HSYNC_PARA, HSYNC_PARA : img width 1280 = 0x0500 */
194 {BRIDGE, M5602_XB_HSYNC_PARA, 0x05, 0x00},
195 {BRIDGE, M5602_XB_HSYNC_PARA, 0x00, 0x00},
196 {BRIDGE, M5602_XB_SIG_INI, 0x00, 0x00},
197 {BRIDGE, M5602_XB_SEN_CLK_DIV, 0x00, 0x00},
198 {BRIDGE, M5602_XB_SEN_CLK_CTRL, 0xa0, 0x00}, /* 48 MHz */
199
200 {SENSOR, S5K4AA_PAGE_MAP, 0x02, 0x00},
201 {SENSOR, S5K4AA_READ_MODE, S5K4AA_RM_H_FLIP, 0x00},
202 {SENSOR, 0x37, 0x01, 0x00},
203 {SENSOR, S5K4AA_ROWSTART_HI, 0x00, 0x00},
204 {SENSOR, S5K4AA_ROWSTART_LO, 0x09, 0x00},
205 {SENSOR, S5K4AA_COLSTART_HI, 0x00, 0x00},
206 {SENSOR, S5K4AA_COLSTART_LO, 0x0a, 0x00},
207 {SENSOR, S5K4AA_WINDOW_HEIGHT_HI, 0x04, 0x00},
208 {SENSOR, S5K4AA_WINDOW_HEIGHT_LO, 0x00, 0x00},
209 {SENSOR, S5K4AA_WINDOW_WIDTH_HI, 0x05, 0x00},
210 {SENSOR, S5K4AA_WINDOW_WIDTH_LO, 0x00, 0x00},
211 {SENSOR, S5K4AA_H_BLANK_HI__, 0x01, 0x00},
212 {SENSOR, S5K4AA_H_BLANK_LO__, 0xa8, 0x00},
213 {SENSOR, S5K4AA_EXPOSURE_HI, 0x01, 0x00},
214 {SENSOR, S5K4AA_EXPOSURE_LO, 0x00, 0x00},
215 {SENSOR, 0x11, 0x04, 0x00},
216 {SENSOR, 0x12, 0xc3, 0x00},
217 {SENSOR, S5K4AA_PAGE_MAP, 0x02, 0x00},
218 {SENSOR, 0x02, 0x0e, 0x00},
219};
220
221
23static int s5k4aa_s_ctrl(struct v4l2_ctrl *ctrl); 222static int s5k4aa_s_ctrl(struct v4l2_ctrl *ctrl);
24static void s5k4aa_dump_registers(struct sd *sd); 223static void s5k4aa_dump_registers(struct sd *sd);
25 224
diff --git a/drivers/media/usb/gspca/m5602/m5602_s5k4aa.h b/drivers/media/usb/gspca/m5602/m5602_s5k4aa.h
index 9953e9766954..8407682d6823 100644
--- a/drivers/media/usb/gspca/m5602/m5602_s5k4aa.h
+++ b/drivers/media/usb/gspca/m5602/m5602_s5k4aa.h
@@ -85,201 +85,4 @@ static const struct m5602_sensor s5k4aa = {
85 .disconnect = s5k4aa_disconnect, 85 .disconnect = s5k4aa_disconnect,
86}; 86};
87 87
88static const unsigned char preinit_s5k4aa[][4] = {
89 {BRIDGE, M5602_XB_MCU_CLK_DIV, 0x02, 0x00},
90 {BRIDGE, M5602_XB_MCU_CLK_CTRL, 0xb0, 0x00},
91 {BRIDGE, M5602_XB_SEN_CLK_DIV, 0x00, 0x00},
92 {BRIDGE, M5602_XB_SEN_CLK_CTRL, 0xb0, 0x00},
93 {BRIDGE, M5602_XB_ADC_CTRL, 0xc0, 0x00},
94 {BRIDGE, M5602_XB_SENSOR_TYPE, 0x0d, 0x00},
95 {BRIDGE, M5602_XB_SENSOR_CTRL, 0x00, 0x00},
96
97 {BRIDGE, M5602_XB_GPIO_DIR, 0x1d, 0x00},
98 {BRIDGE, M5602_XB_GPIO_DAT, 0x08, 0x00},
99 {BRIDGE, M5602_XB_SEN_CLK_DIV, 0xb0, 0x00},
100 {BRIDGE, M5602_XB_SEN_CLK_CTRL, 0x80, 0x00},
101 {BRIDGE, M5602_XB_GPIO_EN_H, 0x3f, 0x00},
102 {BRIDGE, M5602_XB_GPIO_DIR_H, 0x3f, 0x00},
103 {BRIDGE, M5602_XB_GPIO_DAT_H, 0x00, 0x00},
104 {BRIDGE, M5602_XB_GPIO_DIR, 0x1d, 0x00},
105 {BRIDGE, M5602_XB_GPIO_DAT, 0x00, 0x00},
106 {BRIDGE, M5602_XB_GPIO_EN_L, 0xff, 0x00},
107 {BRIDGE, M5602_XB_GPIO_DIR_L, 0xff, 0x00},
108 {BRIDGE, M5602_XB_GPIO_DAT_L, 0x00, 0x00},
109 {BRIDGE, M5602_XB_SEN_CLK_DIV, 0x00, 0x00},
110 {BRIDGE, M5602_XB_SEN_CLK_CTRL, 0xb0, 0x00},
111 {BRIDGE, M5602_XB_ADC_CTRL, 0xc0, 0x00},
112 {BRIDGE, M5602_XB_SENSOR_TYPE, 0x08, 0x00},
113
114 {BRIDGE, M5602_XB_MCU_CLK_DIV, 0x02, 0x00},
115 {BRIDGE, M5602_XB_MCU_CLK_CTRL, 0xb0, 0x00},
116 {BRIDGE, M5602_XB_GPIO_DIR, 0x1d, 0x00},
117 {BRIDGE, M5602_XB_GPIO_DAT, 0x14, 0x00},
118 {BRIDGE, M5602_XB_SEN_CLK_DIV, 0x00, 0x00},
119 {BRIDGE, M5602_XB_SEN_CLK_CTRL, 0xf0, 0x00},
120 {BRIDGE, M5602_XB_GPIO_DIR, 0x1d, 0x00},
121 {BRIDGE, M5602_XB_GPIO_DAT, 0x1c, 0x00},
122 {BRIDGE, M5602_XB_GPIO_EN_H, 0x06, 0x00},
123 {BRIDGE, M5602_XB_GPIO_DIR_H, 0x06, 0x00},
124 {BRIDGE, M5602_XB_GPIO_DAT_H, 0x00, 0x00},
125 {BRIDGE, M5602_XB_GPIO_EN_L, 0x00, 0x00},
126 {BRIDGE, M5602_XB_I2C_CLK_DIV, 0x20, 0x00},
127
128 {SENSOR, S5K4AA_PAGE_MAP, 0x00, 0x00}
129};
130
131static const unsigned char init_s5k4aa[][4] = {
132 {BRIDGE, M5602_XB_MCU_CLK_DIV, 0x02, 0x00},
133 {BRIDGE, M5602_XB_MCU_CLK_CTRL, 0xb0, 0x00},
134 {BRIDGE, M5602_XB_SEN_CLK_DIV, 0x00, 0x00},
135 {BRIDGE, M5602_XB_SEN_CLK_CTRL, 0xb0, 0x00},
136 {BRIDGE, M5602_XB_ADC_CTRL, 0xc0, 0x00},
137 {BRIDGE, M5602_XB_SENSOR_TYPE, 0x0d, 0x00},
138 {BRIDGE, M5602_XB_SENSOR_CTRL, 0x00, 0x00},
139
140 {BRIDGE, M5602_XB_GPIO_DIR, 0x1d, 0x00},
141 {BRIDGE, M5602_XB_GPIO_DAT, 0x08, 0x00},
142 {BRIDGE, M5602_XB_SEN_CLK_DIV, 0xb0, 0x00},
143 {BRIDGE, M5602_XB_SEN_CLK_CTRL, 0x80, 0x00},
144 {BRIDGE, M5602_XB_GPIO_EN_H, 0x3f, 0x00},
145 {BRIDGE, M5602_XB_GPIO_DIR_H, 0x3f, 0x00},
146 {BRIDGE, M5602_XB_GPIO_DAT_H, 0x00, 0x00},
147 {BRIDGE, M5602_XB_GPIO_DIR, 0x1d, 0x00},
148 {BRIDGE, M5602_XB_GPIO_DAT, 0x00, 0x00},
149 {BRIDGE, M5602_XB_GPIO_EN_L, 0xff, 0x00},
150 {BRIDGE, M5602_XB_GPIO_DIR_L, 0xff, 0x00},
151 {BRIDGE, M5602_XB_GPIO_DAT_L, 0x00, 0x00},
152 {BRIDGE, M5602_XB_SEN_CLK_DIV, 0x00, 0x00},
153 {BRIDGE, M5602_XB_SEN_CLK_CTRL, 0xb0, 0x00},
154 {BRIDGE, M5602_XB_ADC_CTRL, 0xc0, 0x00},
155 {BRIDGE, M5602_XB_SENSOR_TYPE, 0x08, 0x00},
156
157 {BRIDGE, M5602_XB_MCU_CLK_DIV, 0x02, 0x00},
158 {BRIDGE, M5602_XB_MCU_CLK_CTRL, 0xb0, 0x00},
159 {BRIDGE, M5602_XB_GPIO_DIR, 0x1d, 0x00},
160 {BRIDGE, M5602_XB_GPIO_DAT, 0x14, 0x00},
161 {BRIDGE, M5602_XB_SEN_CLK_DIV, 0x00, 0x00},
162 {BRIDGE, M5602_XB_SEN_CLK_CTRL, 0xf0, 0x00},
163 {BRIDGE, M5602_XB_GPIO_DIR, 0x1d, 0x00},
164 {BRIDGE, M5602_XB_GPIO_DAT, 0x1c, 0x00},
165 {BRIDGE, M5602_XB_GPIO_EN_H, 0x06, 0x00},
166 {BRIDGE, M5602_XB_GPIO_DIR_H, 0x06, 0x00},
167 {BRIDGE, M5602_XB_GPIO_DAT_H, 0x00, 0x00},
168 {BRIDGE, M5602_XB_GPIO_EN_L, 0x00, 0x00},
169 {BRIDGE, M5602_XB_I2C_CLK_DIV, 0x20, 0x00},
170
171 {SENSOR, S5K4AA_PAGE_MAP, 0x07, 0x00},
172 {SENSOR, 0x36, 0x01, 0x00},
173 {SENSOR, S5K4AA_PAGE_MAP, 0x00, 0x00},
174 {SENSOR, 0x7b, 0xff, 0x00},
175 {SENSOR, S5K4AA_PAGE_MAP, 0x02, 0x00},
176 {SENSOR, 0x0c, 0x05, 0x00},
177 {SENSOR, 0x02, 0x0e, 0x00},
178 {SENSOR, S5K4AA_READ_MODE, 0xa0, 0x00},
179 {SENSOR, 0x37, 0x00, 0x00},
180};
181
182static const unsigned char VGA_s5k4aa[][4] = {
183 {BRIDGE, M5602_XB_SEN_CLK_DIV, 0x06, 0x00},
184 {BRIDGE, M5602_XB_SEN_CLK_CTRL, 0xb0, 0x00},
185 {BRIDGE, M5602_XB_ADC_CTRL, 0xc0, 0x00},
186 {BRIDGE, M5602_XB_SENSOR_TYPE, 0x08, 0x00},
187 {BRIDGE, M5602_XB_LINE_OF_FRAME_H, 0x81, 0x00},
188 {BRIDGE, M5602_XB_PIX_OF_LINE_H, 0x82, 0x00},
189 {BRIDGE, M5602_XB_SIG_INI, 0x01, 0x00},
190 {BRIDGE, M5602_XB_VSYNC_PARA, 0x00, 0x00},
191 {BRIDGE, M5602_XB_VSYNC_PARA, 0x00, 0x00},
192 {BRIDGE, M5602_XB_VSYNC_PARA, 0x00, 0x00},
193 {BRIDGE, M5602_XB_VSYNC_PARA, 0x00, 0x00},
194 /* VSYNC_PARA, VSYNC_PARA : img height 480 = 0x01e0 */
195 {BRIDGE, M5602_XB_VSYNC_PARA, 0x01, 0x00},
196 {BRIDGE, M5602_XB_VSYNC_PARA, 0xe0, 0x00},
197 {BRIDGE, M5602_XB_VSYNC_PARA, 0x00, 0x00},
198 {BRIDGE, M5602_XB_VSYNC_PARA, 0x00, 0x00},
199 {BRIDGE, M5602_XB_SIG_INI, 0x00, 0x00},
200 {BRIDGE, M5602_XB_SIG_INI, 0x02, 0x00},
201 {BRIDGE, M5602_XB_HSYNC_PARA, 0x00, 0x00},
202 {BRIDGE, M5602_XB_HSYNC_PARA, 0x00, 0x00},
203 /* HSYNC_PARA, HSYNC_PARA : img width 640 = 0x0280 */
204 {BRIDGE, M5602_XB_HSYNC_PARA, 0x02, 0x00},
205 {BRIDGE, M5602_XB_HSYNC_PARA, 0x80, 0x00},
206 {BRIDGE, M5602_XB_SIG_INI, 0x00, 0x00},
207 {BRIDGE, M5602_XB_SEN_CLK_DIV, 0x00, 0x00},
208 {BRIDGE, M5602_XB_SEN_CLK_CTRL, 0xa0, 0x00}, /* 48 MHz */
209
210 {SENSOR, S5K4AA_PAGE_MAP, 0x02, 0x00},
211 {SENSOR, S5K4AA_READ_MODE, S5K4AA_RM_H_FLIP | S5K4AA_RM_ROW_SKIP_2X
212 | S5K4AA_RM_COL_SKIP_2X, 0x00},
213 /* 0x37 : Fix image stability when light is too bright and improves
214 * image quality in 640x480, but worsens it in 1280x1024 */
215 {SENSOR, 0x37, 0x01, 0x00},
216 /* ROWSTART_HI, ROWSTART_LO : 10 + (1024-960)/2 = 42 = 0x002a */
217 {SENSOR, S5K4AA_ROWSTART_HI, 0x00, 0x00},
218 {SENSOR, S5K4AA_ROWSTART_LO, 0x29, 0x00},
219 {SENSOR, S5K4AA_COLSTART_HI, 0x00, 0x00},
220 {SENSOR, S5K4AA_COLSTART_LO, 0x0c, 0x00},
221 /* window_height_hi, window_height_lo : 960 = 0x03c0 */
222 {SENSOR, S5K4AA_WINDOW_HEIGHT_HI, 0x03, 0x00},
223 {SENSOR, S5K4AA_WINDOW_HEIGHT_LO, 0xc0, 0x00},
224 /* window_width_hi, window_width_lo : 1280 = 0x0500 */
225 {SENSOR, S5K4AA_WINDOW_WIDTH_HI, 0x05, 0x00},
226 {SENSOR, S5K4AA_WINDOW_WIDTH_LO, 0x00, 0x00},
227 {SENSOR, S5K4AA_H_BLANK_HI__, 0x00, 0x00},
228 {SENSOR, S5K4AA_H_BLANK_LO__, 0xa8, 0x00}, /* helps to sync... */
229 {SENSOR, S5K4AA_EXPOSURE_HI, 0x01, 0x00},
230 {SENSOR, S5K4AA_EXPOSURE_LO, 0x00, 0x00},
231 {SENSOR, 0x11, 0x04, 0x00},
232 {SENSOR, 0x12, 0xc3, 0x00},
233 {SENSOR, S5K4AA_PAGE_MAP, 0x02, 0x00},
234 {SENSOR, 0x02, 0x0e, 0x00},
235};
236
237static const unsigned char SXGA_s5k4aa[][4] = {
238 {BRIDGE, M5602_XB_SEN_CLK_DIV, 0x06, 0x00},
239 {BRIDGE, M5602_XB_SEN_CLK_CTRL, 0xb0, 0x00},
240 {BRIDGE, M5602_XB_ADC_CTRL, 0xc0, 0x00},
241 {BRIDGE, M5602_XB_SENSOR_TYPE, 0x08, 0x00},
242 {BRIDGE, M5602_XB_LINE_OF_FRAME_H, 0x81, 0x00},
243 {BRIDGE, M5602_XB_PIX_OF_LINE_H, 0x82, 0x00},
244 {BRIDGE, M5602_XB_SIG_INI, 0x01, 0x00},
245 {BRIDGE, M5602_XB_VSYNC_PARA, 0x00, 0x00},
246 {BRIDGE, M5602_XB_VSYNC_PARA, 0x00, 0x00},
247 {BRIDGE, M5602_XB_VSYNC_PARA, 0x00, 0x00},
248 {BRIDGE, M5602_XB_VSYNC_PARA, 0x00, 0x00},
249 /* VSYNC_PARA, VSYNC_PARA : img height 1024 = 0x0400 */
250 {BRIDGE, M5602_XB_VSYNC_PARA, 0x04, 0x00},
251 {BRIDGE, M5602_XB_VSYNC_PARA, 0x00, 0x00},
252 {BRIDGE, M5602_XB_VSYNC_PARA, 0x00, 0x00},
253 {BRIDGE, M5602_XB_VSYNC_PARA, 0x00, 0x00},
254 {BRIDGE, M5602_XB_SIG_INI, 0x00, 0x00},
255 {BRIDGE, M5602_XB_SIG_INI, 0x02, 0x00},
256 {BRIDGE, M5602_XB_HSYNC_PARA, 0x00, 0x00},
257 {BRIDGE, M5602_XB_HSYNC_PARA, 0x00, 0x00},
258 /* HSYNC_PARA, HSYNC_PARA : img width 1280 = 0x0500 */
259 {BRIDGE, M5602_XB_HSYNC_PARA, 0x05, 0x00},
260 {BRIDGE, M5602_XB_HSYNC_PARA, 0x00, 0x00},
261 {BRIDGE, M5602_XB_SIG_INI, 0x00, 0x00},
262 {BRIDGE, M5602_XB_SEN_CLK_DIV, 0x00, 0x00},
263 {BRIDGE, M5602_XB_SEN_CLK_CTRL, 0xa0, 0x00}, /* 48 MHz */
264
265 {SENSOR, S5K4AA_PAGE_MAP, 0x02, 0x00},
266 {SENSOR, S5K4AA_READ_MODE, S5K4AA_RM_H_FLIP, 0x00},
267 {SENSOR, 0x37, 0x01, 0x00},
268 {SENSOR, S5K4AA_ROWSTART_HI, 0x00, 0x00},
269 {SENSOR, S5K4AA_ROWSTART_LO, 0x09, 0x00},
270 {SENSOR, S5K4AA_COLSTART_HI, 0x00, 0x00},
271 {SENSOR, S5K4AA_COLSTART_LO, 0x0a, 0x00},
272 {SENSOR, S5K4AA_WINDOW_HEIGHT_HI, 0x04, 0x00},
273 {SENSOR, S5K4AA_WINDOW_HEIGHT_LO, 0x00, 0x00},
274 {SENSOR, S5K4AA_WINDOW_WIDTH_HI, 0x05, 0x00},
275 {SENSOR, S5K4AA_WINDOW_WIDTH_LO, 0x00, 0x00},
276 {SENSOR, S5K4AA_H_BLANK_HI__, 0x01, 0x00},
277 {SENSOR, S5K4AA_H_BLANK_LO__, 0xa8, 0x00},
278 {SENSOR, S5K4AA_EXPOSURE_HI, 0x01, 0x00},
279 {SENSOR, S5K4AA_EXPOSURE_LO, 0x00, 0x00},
280 {SENSOR, 0x11, 0x04, 0x00},
281 {SENSOR, 0x12, 0xc3, 0x00},
282 {SENSOR, S5K4AA_PAGE_MAP, 0x02, 0x00},
283 {SENSOR, 0x02, 0x0e, 0x00},
284};
285#endif 88#endif
diff --git a/drivers/media/usb/gspca/m5602/m5602_s5k83a.c b/drivers/media/usb/gspca/m5602/m5602_s5k83a.c
index bf6b215438e3..be5e25d1a2e8 100644
--- a/drivers/media/usb/gspca/m5602/m5602_s5k83a.c
+++ b/drivers/media/usb/gspca/m5602/m5602_s5k83a.c
@@ -41,6 +41,130 @@ static struct v4l2_pix_format s5k83a_modes[] = {
41 } 41 }
42}; 42};
43 43
44static const unsigned char preinit_s5k83a[][4] = {
45 {BRIDGE, M5602_XB_MCU_CLK_DIV, 0x02, 0x00},
46 {BRIDGE, M5602_XB_MCU_CLK_CTRL, 0xb0, 0x00},
47 {BRIDGE, M5602_XB_SEN_CLK_DIV, 0x00, 0x00},
48 {BRIDGE, M5602_XB_SEN_CLK_CTRL, 0xb0, 0x00},
49 {BRIDGE, M5602_XB_ADC_CTRL, 0xc0, 0x00},
50 {BRIDGE, M5602_XB_SENSOR_TYPE, 0x0d, 0x00},
51 {BRIDGE, M5602_XB_SENSOR_CTRL, 0x00, 0x00},
52
53 {BRIDGE, M5602_XB_SIG_INI, 0x00, 0x00},
54 {BRIDGE, M5602_XB_GPIO_DIR, 0x1d, 0x00},
55 {BRIDGE, M5602_XB_GPIO_DAT, 0x08, 0x00},
56 {BRIDGE, M5602_XB_GPIO_EN_H, 0x3f, 0x00},
57 {BRIDGE, M5602_XB_GPIO_DIR_H, 0x3f, 0x00},
58 {BRIDGE, M5602_XB_GPIO_DAT_H, 0x00, 0x00},
59 {BRIDGE, M5602_XB_GPIO_EN_L, 0xff, 0x00},
60 {BRIDGE, M5602_XB_GPIO_DIR_L, 0xff, 0x00},
61 {BRIDGE, M5602_XB_GPIO_DAT_L, 0x00, 0x00},
62 {BRIDGE, M5602_XB_SEN_CLK_DIV, 0xb0, 0x00},
63 {BRIDGE, M5602_XB_SEN_CLK_CTRL, 0x80, 0x00},
64 {BRIDGE, M5602_XB_SEN_CLK_DIV, 0x00, 0x00},
65 {BRIDGE, M5602_XB_SEN_CLK_CTRL, 0xb0, 0x00},
66 {BRIDGE, M5602_XB_ADC_CTRL, 0xc0, 0x00},
67 {BRIDGE, M5602_XB_SENSOR_TYPE, 0x09, 0x00},
68 {BRIDGE, M5602_XB_MCU_CLK_DIV, 0x02, 0x00},
69 {BRIDGE, M5602_XB_MCU_CLK_CTRL, 0xb0, 0x00},
70 {BRIDGE, M5602_XB_SEN_CLK_DIV, 0x00, 0x00},
71 {BRIDGE, M5602_XB_SEN_CLK_CTRL, 0xf0, 0x00},
72 {BRIDGE, M5602_XB_GPIO_DIR, 0x1d, 0x00},
73 {BRIDGE, M5602_XB_GPIO_DAT, 0x1c, 0x00},
74 {BRIDGE, M5602_XB_GPIO_EN_H, 0x06, 0x00},
75 {BRIDGE, M5602_XB_GPIO_DIR_H, 0x06, 0x00},
76 {BRIDGE, M5602_XB_GPIO_DAT_H, 0x00, 0x00},
77 {BRIDGE, M5602_XB_GPIO_EN_L, 0x00, 0x00},
78 {BRIDGE, M5602_XB_I2C_CLK_DIV, 0x20, 0x00},
79};
80
81/* This could probably be considerably shortened.
82 I don't have the hardware to experiment with it, patches welcome
83*/
84static const unsigned char init_s5k83a[][4] = {
85 /* The following sequence is useless after a clean boot
86 but is necessary after resume from suspend */
87 {BRIDGE, M5602_XB_GPIO_DIR, 0x1d, 0x00},
88 {BRIDGE, M5602_XB_GPIO_DAT, 0x08, 0x00},
89 {BRIDGE, M5602_XB_GPIO_EN_H, 0x3f, 0x00},
90 {BRIDGE, M5602_XB_GPIO_DIR_H, 0x3f, 0x00},
91 {BRIDGE, M5602_XB_GPIO_DAT_H, 0x00, 0x00},
92 {BRIDGE, M5602_XB_GPIO_EN_L, 0xff, 0x00},
93 {BRIDGE, M5602_XB_GPIO_DIR_L, 0xff, 0x00},
94 {BRIDGE, M5602_XB_GPIO_DAT_L, 0x00, 0x00},
95 {BRIDGE, M5602_XB_SEN_CLK_DIV, 0xb0, 0x00},
96 {BRIDGE, M5602_XB_SEN_CLK_CTRL, 0x80, 0x00},
97 {BRIDGE, M5602_XB_SEN_CLK_DIV, 0x00, 0x00},
98 {BRIDGE, M5602_XB_SEN_CLK_CTRL, 0xb0, 0x00},
99 {BRIDGE, M5602_XB_ADC_CTRL, 0xc0, 0x00},
100 {BRIDGE, M5602_XB_SENSOR_TYPE, 0x09, 0x00},
101 {BRIDGE, M5602_XB_MCU_CLK_DIV, 0x02, 0x00},
102 {BRIDGE, M5602_XB_MCU_CLK_CTRL, 0xb0, 0x00},
103 {BRIDGE, M5602_XB_SEN_CLK_DIV, 0x00, 0x00},
104 {BRIDGE, M5602_XB_SEN_CLK_CTRL, 0xf0, 0x00},
105 {BRIDGE, M5602_XB_GPIO_DIR, 0x1d, 0x00},
106 {BRIDGE, M5602_XB_GPIO_DAT, 0x08, 0x00},
107 {BRIDGE, M5602_XB_GPIO_EN_H, 0x06, 0x00},
108 {BRIDGE, M5602_XB_GPIO_DIR_H, 0x06, 0x00},
109 {BRIDGE, M5602_XB_GPIO_DAT_H, 0x00, 0x00},
110 {BRIDGE, M5602_XB_GPIO_EN_L, 0x00, 0x00},
111 {BRIDGE, M5602_XB_I2C_CLK_DIV, 0x20, 0x00},
112
113 {SENSOR, S5K83A_PAGE_MAP, 0x04, 0x00},
114 {SENSOR, 0xaf, 0x01, 0x00},
115 {SENSOR, S5K83A_PAGE_MAP, 0x00, 0x00},
116 {SENSOR, 0x7b, 0xff, 0x00},
117 {SENSOR, S5K83A_PAGE_MAP, 0x05, 0x00},
118 {SENSOR, 0x01, 0x50, 0x00},
119 {SENSOR, 0x12, 0x20, 0x00},
120 {SENSOR, 0x17, 0x40, 0x00},
121 {SENSOR, 0x1c, 0x00, 0x00},
122 {SENSOR, 0x02, 0x70, 0x00},
123 {SENSOR, 0x03, 0x0b, 0x00},
124 {SENSOR, 0x04, 0xf0, 0x00},
125 {SENSOR, 0x05, 0x0b, 0x00},
126 {SENSOR, 0x06, 0x71, 0x00},
127 {SENSOR, 0x07, 0xe8, 0x00}, /* 488 */
128 {SENSOR, 0x08, 0x02, 0x00},
129 {SENSOR, 0x09, 0x88, 0x00}, /* 648 */
130 {SENSOR, 0x14, 0x00, 0x00},
131 {SENSOR, 0x15, 0x20, 0x00}, /* 32 */
132 {SENSOR, 0x19, 0x00, 0x00},
133 {SENSOR, 0x1a, 0x98, 0x00}, /* 152 */
134 {SENSOR, 0x0f, 0x02, 0x00},
135 {SENSOR, 0x10, 0xe5, 0x00}, /* 741 */
136 /* normal colors
137 (this is value after boot, but after tries can be different) */
138 {SENSOR, 0x00, 0x06, 0x00},
139};
140
141static const unsigned char start_s5k83a[][4] = {
142 {BRIDGE, M5602_XB_SEN_CLK_DIV, 0x06, 0x00},
143 {BRIDGE, M5602_XB_SEN_CLK_CTRL, 0xb0, 0x00},
144 {BRIDGE, M5602_XB_ADC_CTRL, 0xc0, 0x00},
145 {BRIDGE, M5602_XB_SENSOR_TYPE, 0x09, 0x00},
146 {BRIDGE, M5602_XB_LINE_OF_FRAME_H, 0x81, 0x00},
147 {BRIDGE, M5602_XB_PIX_OF_LINE_H, 0x82, 0x00},
148 {BRIDGE, M5602_XB_SIG_INI, 0x01, 0x00},
149 {BRIDGE, M5602_XB_VSYNC_PARA, 0x00, 0x00},
150 {BRIDGE, M5602_XB_VSYNC_PARA, 0x00, 0x00},
151 {BRIDGE, M5602_XB_VSYNC_PARA, 0x00, 0x00},
152 {BRIDGE, M5602_XB_VSYNC_PARA, 0x00, 0x00},
153 {BRIDGE, M5602_XB_VSYNC_PARA, 0x01, 0x00},
154 {BRIDGE, M5602_XB_VSYNC_PARA, 0xe4, 0x00}, /* 484 */
155 {BRIDGE, M5602_XB_VSYNC_PARA, 0x00, 0x00},
156 {BRIDGE, M5602_XB_VSYNC_PARA, 0x00, 0x00},
157 {BRIDGE, M5602_XB_SIG_INI, 0x00, 0x00},
158 {BRIDGE, M5602_XB_SIG_INI, 0x02, 0x00},
159 {BRIDGE, M5602_XB_HSYNC_PARA, 0x00, 0x00},
160 {BRIDGE, M5602_XB_HSYNC_PARA, 0x00, 0x00},
161 {BRIDGE, M5602_XB_HSYNC_PARA, 0x02, 0x00},
162 {BRIDGE, M5602_XB_HSYNC_PARA, 0x7f, 0x00}, /* 639 */
163 {BRIDGE, M5602_XB_SIG_INI, 0x00, 0x00},
164 {BRIDGE, M5602_XB_SEN_CLK_DIV, 0x00, 0x00},
165 {BRIDGE, M5602_XB_SEN_CLK_CTRL, 0xb0, 0x00},
166};
167
44static void s5k83a_dump_registers(struct sd *sd); 168static void s5k83a_dump_registers(struct sd *sd);
45static int s5k83a_get_rotation(struct sd *sd, u8 *reg_data); 169static int s5k83a_get_rotation(struct sd *sd, u8 *reg_data);
46static int s5k83a_set_led_indication(struct sd *sd, u8 val); 170static int s5k83a_set_led_indication(struct sd *sd, u8 val);
diff --git a/drivers/media/usb/gspca/m5602/m5602_s5k83a.h b/drivers/media/usb/gspca/m5602/m5602_s5k83a.h
index d61b918228df..3212bfe53d22 100644
--- a/drivers/media/usb/gspca/m5602/m5602_s5k83a.h
+++ b/drivers/media/usb/gspca/m5602/m5602_s5k83a.h
@@ -61,128 +61,4 @@ static const struct m5602_sensor s5k83a = {
61 .i2c_slave_id = 0x5a, 61 .i2c_slave_id = 0x5a,
62 .i2c_regW = 2, 62 .i2c_regW = 2,
63}; 63};
64
65static const unsigned char preinit_s5k83a[][4] = {
66 {BRIDGE, M5602_XB_MCU_CLK_DIV, 0x02, 0x00},
67 {BRIDGE, M5602_XB_MCU_CLK_CTRL, 0xb0, 0x00},
68 {BRIDGE, M5602_XB_SEN_CLK_DIV, 0x00, 0x00},
69 {BRIDGE, M5602_XB_SEN_CLK_CTRL, 0xb0, 0x00},
70 {BRIDGE, M5602_XB_ADC_CTRL, 0xc0, 0x00},
71 {BRIDGE, M5602_XB_SENSOR_TYPE, 0x0d, 0x00},
72 {BRIDGE, M5602_XB_SENSOR_CTRL, 0x00, 0x00},
73
74 {BRIDGE, M5602_XB_SIG_INI, 0x00, 0x00},
75 {BRIDGE, M5602_XB_GPIO_DIR, 0x1d, 0x00},
76 {BRIDGE, M5602_XB_GPIO_DAT, 0x08, 0x00},
77 {BRIDGE, M5602_XB_GPIO_EN_H, 0x3f, 0x00},
78 {BRIDGE, M5602_XB_GPIO_DIR_H, 0x3f, 0x00},
79 {BRIDGE, M5602_XB_GPIO_DAT_H, 0x00, 0x00},
80 {BRIDGE, M5602_XB_GPIO_EN_L, 0xff, 0x00},
81 {BRIDGE, M5602_XB_GPIO_DIR_L, 0xff, 0x00},
82 {BRIDGE, M5602_XB_GPIO_DAT_L, 0x00, 0x00},
83 {BRIDGE, M5602_XB_SEN_CLK_DIV, 0xb0, 0x00},
84 {BRIDGE, M5602_XB_SEN_CLK_CTRL, 0x80, 0x00},
85 {BRIDGE, M5602_XB_SEN_CLK_DIV, 0x00, 0x00},
86 {BRIDGE, M5602_XB_SEN_CLK_CTRL, 0xb0, 0x00},
87 {BRIDGE, M5602_XB_ADC_CTRL, 0xc0, 0x00},
88 {BRIDGE, M5602_XB_SENSOR_TYPE, 0x09, 0x00},
89 {BRIDGE, M5602_XB_MCU_CLK_DIV, 0x02, 0x00},
90 {BRIDGE, M5602_XB_MCU_CLK_CTRL, 0xb0, 0x00},
91 {BRIDGE, M5602_XB_SEN_CLK_DIV, 0x00, 0x00},
92 {BRIDGE, M5602_XB_SEN_CLK_CTRL, 0xf0, 0x00},
93 {BRIDGE, M5602_XB_GPIO_DIR, 0x1d, 0x00},
94 {BRIDGE, M5602_XB_GPIO_DAT, 0x1c, 0x00},
95 {BRIDGE, M5602_XB_GPIO_EN_H, 0x06, 0x00},
96 {BRIDGE, M5602_XB_GPIO_DIR_H, 0x06, 0x00},
97 {BRIDGE, M5602_XB_GPIO_DAT_H, 0x00, 0x00},
98 {BRIDGE, M5602_XB_GPIO_EN_L, 0x00, 0x00},
99 {BRIDGE, M5602_XB_I2C_CLK_DIV, 0x20, 0x00},
100};
101
102/* This could probably be considerably shortened.
103 I don't have the hardware to experiment with it, patches welcome
104*/
105static const unsigned char init_s5k83a[][4] = {
106 /* The following sequence is useless after a clean boot
107 but is necessary after resume from suspend */
108 {BRIDGE, M5602_XB_GPIO_DIR, 0x1d, 0x00},
109 {BRIDGE, M5602_XB_GPIO_DAT, 0x08, 0x00},
110 {BRIDGE, M5602_XB_GPIO_EN_H, 0x3f, 0x00},
111 {BRIDGE, M5602_XB_GPIO_DIR_H, 0x3f, 0x00},
112 {BRIDGE, M5602_XB_GPIO_DAT_H, 0x00, 0x00},
113 {BRIDGE, M5602_XB_GPIO_EN_L, 0xff, 0x00},
114 {BRIDGE, M5602_XB_GPIO_DIR_L, 0xff, 0x00},
115 {BRIDGE, M5602_XB_GPIO_DAT_L, 0x00, 0x00},
116 {BRIDGE, M5602_XB_SEN_CLK_DIV, 0xb0, 0x00},
117 {BRIDGE, M5602_XB_SEN_CLK_CTRL, 0x80, 0x00},
118 {BRIDGE, M5602_XB_SEN_CLK_DIV, 0x00, 0x00},
119 {BRIDGE, M5602_XB_SEN_CLK_CTRL, 0xb0, 0x00},
120 {BRIDGE, M5602_XB_ADC_CTRL, 0xc0, 0x00},
121 {BRIDGE, M5602_XB_SENSOR_TYPE, 0x09, 0x00},
122 {BRIDGE, M5602_XB_MCU_CLK_DIV, 0x02, 0x00},
123 {BRIDGE, M5602_XB_MCU_CLK_CTRL, 0xb0, 0x00},
124 {BRIDGE, M5602_XB_SEN_CLK_DIV, 0x00, 0x00},
125 {BRIDGE, M5602_XB_SEN_CLK_CTRL, 0xf0, 0x00},
126 {BRIDGE, M5602_XB_GPIO_DIR, 0x1d, 0x00},
127 {BRIDGE, M5602_XB_GPIO_DAT, 0x08, 0x00},
128 {BRIDGE, M5602_XB_GPIO_EN_H, 0x06, 0x00},
129 {BRIDGE, M5602_XB_GPIO_DIR_H, 0x06, 0x00},
130 {BRIDGE, M5602_XB_GPIO_DAT_H, 0x00, 0x00},
131 {BRIDGE, M5602_XB_GPIO_EN_L, 0x00, 0x00},
132 {BRIDGE, M5602_XB_I2C_CLK_DIV, 0x20, 0x00},
133
134 {SENSOR, S5K83A_PAGE_MAP, 0x04, 0x00},
135 {SENSOR, 0xaf, 0x01, 0x00},
136 {SENSOR, S5K83A_PAGE_MAP, 0x00, 0x00},
137 {SENSOR, 0x7b, 0xff, 0x00},
138 {SENSOR, S5K83A_PAGE_MAP, 0x05, 0x00},
139 {SENSOR, 0x01, 0x50, 0x00},
140 {SENSOR, 0x12, 0x20, 0x00},
141 {SENSOR, 0x17, 0x40, 0x00},
142 {SENSOR, 0x1c, 0x00, 0x00},
143 {SENSOR, 0x02, 0x70, 0x00},
144 {SENSOR, 0x03, 0x0b, 0x00},
145 {SENSOR, 0x04, 0xf0, 0x00},
146 {SENSOR, 0x05, 0x0b, 0x00},
147 {SENSOR, 0x06, 0x71, 0x00},
148 {SENSOR, 0x07, 0xe8, 0x00}, /* 488 */
149 {SENSOR, 0x08, 0x02, 0x00},
150 {SENSOR, 0x09, 0x88, 0x00}, /* 648 */
151 {SENSOR, 0x14, 0x00, 0x00},
152 {SENSOR, 0x15, 0x20, 0x00}, /* 32 */
153 {SENSOR, 0x19, 0x00, 0x00},
154 {SENSOR, 0x1a, 0x98, 0x00}, /* 152 */
155 {SENSOR, 0x0f, 0x02, 0x00},
156 {SENSOR, 0x10, 0xe5, 0x00}, /* 741 */
157 /* normal colors
158 (this is value after boot, but after tries can be different) */
159 {SENSOR, 0x00, 0x06, 0x00},
160};
161
162static const unsigned char start_s5k83a[][4] = {
163 {BRIDGE, M5602_XB_SEN_CLK_DIV, 0x06, 0x00},
164 {BRIDGE, M5602_XB_SEN_CLK_CTRL, 0xb0, 0x00},
165 {BRIDGE, M5602_XB_ADC_CTRL, 0xc0, 0x00},
166 {BRIDGE, M5602_XB_SENSOR_TYPE, 0x09, 0x00},
167 {BRIDGE, M5602_XB_LINE_OF_FRAME_H, 0x81, 0x00},
168 {BRIDGE, M5602_XB_PIX_OF_LINE_H, 0x82, 0x00},
169 {BRIDGE, M5602_XB_SIG_INI, 0x01, 0x00},
170 {BRIDGE, M5602_XB_VSYNC_PARA, 0x00, 0x00},
171 {BRIDGE, M5602_XB_VSYNC_PARA, 0x00, 0x00},
172 {BRIDGE, M5602_XB_VSYNC_PARA, 0x00, 0x00},
173 {BRIDGE, M5602_XB_VSYNC_PARA, 0x00, 0x00},
174 {BRIDGE, M5602_XB_VSYNC_PARA, 0x01, 0x00},
175 {BRIDGE, M5602_XB_VSYNC_PARA, 0xe4, 0x00}, /* 484 */
176 {BRIDGE, M5602_XB_VSYNC_PARA, 0x00, 0x00},
177 {BRIDGE, M5602_XB_VSYNC_PARA, 0x00, 0x00},
178 {BRIDGE, M5602_XB_SIG_INI, 0x00, 0x00},
179 {BRIDGE, M5602_XB_SIG_INI, 0x02, 0x00},
180 {BRIDGE, M5602_XB_HSYNC_PARA, 0x00, 0x00},
181 {BRIDGE, M5602_XB_HSYNC_PARA, 0x00, 0x00},
182 {BRIDGE, M5602_XB_HSYNC_PARA, 0x02, 0x00},
183 {BRIDGE, M5602_XB_HSYNC_PARA, 0x7f, 0x00}, /* 639 */
184 {BRIDGE, M5602_XB_SIG_INI, 0x00, 0x00},
185 {BRIDGE, M5602_XB_SEN_CLK_DIV, 0x00, 0x00},
186 {BRIDGE, M5602_XB_SEN_CLK_CTRL, 0xb0, 0x00},
187};
188#endif 64#endif
diff --git a/drivers/media/usb/gspca/ov534.c b/drivers/media/usb/gspca/ov534.c
index bfff1d1c70ab..9266a5c9abc5 100644
--- a/drivers/media/usb/gspca/ov534.c
+++ b/drivers/media/usb/gspca/ov534.c
@@ -51,6 +51,7 @@
51#define OV534_OP_READ_2 0xf9 51#define OV534_OP_READ_2 0xf9
52 52
53#define CTRL_TIMEOUT 500 53#define CTRL_TIMEOUT 500
54#define DEFAULT_FRAME_RATE 30
54 55
55MODULE_AUTHOR("Antonio Ospite <ospite@studenti.unina.it>"); 56MODULE_AUTHOR("Antonio Ospite <ospite@studenti.unina.it>");
56MODULE_DESCRIPTION("GSPCA/OV534 USB Camera Driver"); 57MODULE_DESCRIPTION("GSPCA/OV534 USB Camera Driver");
@@ -1061,7 +1062,7 @@ static int sd_config(struct gspca_dev *gspca_dev,
1061 cam->cam_mode = ov772x_mode; 1062 cam->cam_mode = ov772x_mode;
1062 cam->nmodes = ARRAY_SIZE(ov772x_mode); 1063 cam->nmodes = ARRAY_SIZE(ov772x_mode);
1063 1064
1064 sd->frame_rate = 30; 1065 sd->frame_rate = DEFAULT_FRAME_RATE;
1065 1066
1066 return 0; 1067 return 0;
1067} 1068}
@@ -1492,10 +1493,8 @@ static void sd_set_streamparm(struct gspca_dev *gspca_dev,
1492 struct sd *sd = (struct sd *) gspca_dev; 1493 struct sd *sd = (struct sd *) gspca_dev;
1493 1494
1494 if (tpf->numerator == 0 || tpf->denominator == 0) 1495 if (tpf->numerator == 0 || tpf->denominator == 0)
1495 /* Set default framerate */ 1496 sd->frame_rate = DEFAULT_FRAME_RATE;
1496 sd->frame_rate = 30;
1497 else 1497 else
1498 /* Set requested framerate */
1499 sd->frame_rate = tpf->denominator / tpf->numerator; 1498 sd->frame_rate = tpf->denominator / tpf->numerator;
1500 1499
1501 if (gspca_dev->streaming) 1500 if (gspca_dev->streaming)
diff --git a/drivers/media/usb/gspca/sn9c20x.c b/drivers/media/usb/gspca/sn9c20x.c
index d0ee899584a9..10269dad9d20 100644
--- a/drivers/media/usb/gspca/sn9c20x.c
+++ b/drivers/media/usb/gspca/sn9c20x.c
@@ -92,7 +92,6 @@ struct sd {
92 struct v4l2_ctrl *jpegqual; 92 struct v4l2_ctrl *jpegqual;
93 93
94 struct work_struct work; 94 struct work_struct work;
95 struct workqueue_struct *work_thread;
96 95
97 u32 pktsz; /* (used by pkt_scan) */ 96 u32 pktsz; /* (used by pkt_scan) */
98 u16 npkt; 97 u16 npkt;
@@ -2051,8 +2050,6 @@ static int sd_start(struct gspca_dev *gspca_dev)
2051 if (mode & MODE_JPEG) { 2050 if (mode & MODE_JPEG) {
2052 sd->pktsz = sd->npkt = 0; 2051 sd->pktsz = sd->npkt = 0;
2053 sd->nchg = 0; 2052 sd->nchg = 0;
2054 sd->work_thread =
2055 create_singlethread_workqueue(KBUILD_MODNAME);
2056 } 2053 }
2057 2054
2058 return gspca_dev->usb_err; 2055 return gspca_dev->usb_err;
@@ -2070,12 +2067,9 @@ static void sd_stop0(struct gspca_dev *gspca_dev)
2070{ 2067{
2071 struct sd *sd = (struct sd *) gspca_dev; 2068 struct sd *sd = (struct sd *) gspca_dev;
2072 2069
2073 if (sd->work_thread != NULL) { 2070 mutex_unlock(&gspca_dev->usb_lock);
2074 mutex_unlock(&gspca_dev->usb_lock); 2071 flush_work(&sd->work);
2075 destroy_workqueue(sd->work_thread); 2072 mutex_lock(&gspca_dev->usb_lock);
2076 mutex_lock(&gspca_dev->usb_lock);
2077 sd->work_thread = NULL;
2078 }
2079} 2073}
2080 2074
2081static void do_autoexposure(struct gspca_dev *gspca_dev, u16 avg_lum) 2075static void do_autoexposure(struct gspca_dev *gspca_dev, u16 avg_lum)
@@ -2228,7 +2222,7 @@ static void transfer_check(struct gspca_dev *gspca_dev,
2228 new_qual = sd->jpegqual->maximum; 2222 new_qual = sd->jpegqual->maximum;
2229 if (new_qual != curqual) { 2223 if (new_qual != curqual) {
2230 sd->jpegqual->cur.val = new_qual; 2224 sd->jpegqual->cur.val = new_qual;
2231 queue_work(sd->work_thread, &sd->work); 2225 schedule_work(&sd->work);
2232 } 2226 }
2233 } 2227 }
2234 } else { 2228 } else {
diff --git a/drivers/media/usb/gspca/t613.c b/drivers/media/usb/gspca/t613.c
index e2cc4e5a0ccb..bb52fc1fe598 100644
--- a/drivers/media/usb/gspca/t613.c
+++ b/drivers/media/usb/gspca/t613.c
@@ -837,7 +837,7 @@ static void sd_pkt_scan(struct gspca_dev *gspca_dev,
837 u8 *data, /* isoc packet */ 837 u8 *data, /* isoc packet */
838 int len) /* iso packet length */ 838 int len) /* iso packet length */
839{ 839{
840 struct sd *sd = (struct sd *) gspca_dev; 840 struct sd *sd __maybe_unused = (struct sd *) gspca_dev;
841 int pkt_type; 841 int pkt_type;
842 842
843 if (data[0] == 0x5a) { 843 if (data[0] == 0x5a) {
diff --git a/drivers/media/usb/gspca/topro.c b/drivers/media/usb/gspca/topro.c
index c028a5c2438e..15eb069ab60b 100644
--- a/drivers/media/usb/gspca/topro.c
+++ b/drivers/media/usb/gspca/topro.c
@@ -175,6 +175,8 @@ static const u8 jpeg_q[17] = {
175#error "USB buffer too small" 175#error "USB buffer too small"
176#endif 176#endif
177 177
178#define DEFAULT_FRAME_RATE 30
179
178static const u8 rates[] = {30, 20, 15, 10, 7, 5}; 180static const u8 rates[] = {30, 20, 15, 10, 7, 5};
179static const struct framerates framerates[] = { 181static const struct framerates framerates[] = {
180 { 182 {
@@ -4020,7 +4022,7 @@ static int sd_config(struct gspca_dev *gspca_dev,
4020 gspca_dev->cam.mode_framerates = sd->bridge == BRIDGE_TP6800 ? 4022 gspca_dev->cam.mode_framerates = sd->bridge == BRIDGE_TP6800 ?
4021 framerates : framerates_6810; 4023 framerates : framerates_6810;
4022 4024
4023 sd->framerate = 30; /* default: 30 fps */ 4025 sd->framerate = DEFAULT_FRAME_RATE;
4024 return 0; 4026 return 0;
4025} 4027}
4026 4028
@@ -4803,7 +4805,7 @@ static void sd_set_streamparm(struct gspca_dev *gspca_dev,
4803 int fr, i; 4805 int fr, i;
4804 4806
4805 if (tpf->numerator == 0 || tpf->denominator == 0) 4807 if (tpf->numerator == 0 || tpf->denominator == 0)
4806 sd->framerate = 30; 4808 sd->framerate = DEFAULT_FRAME_RATE;
4807 else 4809 else
4808 sd->framerate = tpf->denominator / tpf->numerator; 4810 sd->framerate = tpf->denominator / tpf->numerator;
4809 4811
diff --git a/drivers/media/usb/gspca/zc3xx.c b/drivers/media/usb/gspca/zc3xx.c
index c5d8ee6fa3c7..5f7254d2bc9a 100644
--- a/drivers/media/usb/gspca/zc3xx.c
+++ b/drivers/media/usb/gspca/zc3xx.c
@@ -53,7 +53,6 @@ struct sd {
53 struct v4l2_ctrl *jpegqual; 53 struct v4l2_ctrl *jpegqual;
54 54
55 struct work_struct work; 55 struct work_struct work;
56 struct workqueue_struct *work_thread;
57 56
58 u8 reg08; /* webcam compression quality */ 57 u8 reg08; /* webcam compression quality */
59 58
@@ -6826,8 +6825,7 @@ static int sd_start(struct gspca_dev *gspca_dev)
6826 return gspca_dev->usb_err; 6825 return gspca_dev->usb_err;
6827 6826
6828 /* Start the transfer parameters update thread */ 6827 /* Start the transfer parameters update thread */
6829 sd->work_thread = create_singlethread_workqueue(KBUILD_MODNAME); 6828 schedule_work(&sd->work);
6830 queue_work(sd->work_thread, &sd->work);
6831 6829
6832 return 0; 6830 return 0;
6833} 6831}
@@ -6838,12 +6836,9 @@ static void sd_stop0(struct gspca_dev *gspca_dev)
6838{ 6836{
6839 struct sd *sd = (struct sd *) gspca_dev; 6837 struct sd *sd = (struct sd *) gspca_dev;
6840 6838
6841 if (sd->work_thread != NULL) { 6839 mutex_unlock(&gspca_dev->usb_lock);
6842 mutex_unlock(&gspca_dev->usb_lock); 6840 flush_work(&sd->work);
6843 destroy_workqueue(sd->work_thread); 6841 mutex_lock(&gspca_dev->usb_lock);
6844 mutex_lock(&gspca_dev->usb_lock);
6845 sd->work_thread = NULL;
6846 }
6847 if (!gspca_dev->present) 6842 if (!gspca_dev->present)
6848 return; 6843 return;
6849 send_unknown(gspca_dev, sd->sensor); 6844 send_unknown(gspca_dev, sd->sensor);
diff --git a/drivers/media/usb/hackrf/hackrf.c b/drivers/media/usb/hackrf/hackrf.c
index 9e700caf0d66..b1e229a44192 100644
--- a/drivers/media/usb/hackrf/hackrf.c
+++ b/drivers/media/usb/hackrf/hackrf.c
@@ -760,7 +760,7 @@ static void hackrf_return_all_buffers(struct vb2_queue *vq,
760 760
761static int hackrf_queue_setup(struct vb2_queue *vq, 761static int hackrf_queue_setup(struct vb2_queue *vq,
762 unsigned int *nbuffers, 762 unsigned int *nbuffers,
763 unsigned int *nplanes, unsigned int sizes[], void *alloc_ctxs[]) 763 unsigned int *nplanes, unsigned int sizes[], struct device *alloc_devs[])
764{ 764{
765 struct hackrf_dev *dev = vb2_get_drv_priv(vq); 765 struct hackrf_dev *dev = vb2_get_drv_priv(vq);
766 766
diff --git a/drivers/media/usb/hdpvr/hdpvr-core.c b/drivers/media/usb/hdpvr/hdpvr-core.c
index 08f0ca7aa012..a61d8fd63c12 100644
--- a/drivers/media/usb/hdpvr/hdpvr-core.c
+++ b/drivers/media/usb/hdpvr/hdpvr-core.c
@@ -310,10 +310,6 @@ static int hdpvr_probe(struct usb_interface *interface,
310 init_waitqueue_head(&dev->wait_buffer); 310 init_waitqueue_head(&dev->wait_buffer);
311 init_waitqueue_head(&dev->wait_data); 311 init_waitqueue_head(&dev->wait_data);
312 312
313 dev->workqueue = create_singlethread_workqueue("hdpvr_buffer");
314 if (!dev->workqueue)
315 goto error;
316
317 dev->options = hdpvr_default_options; 313 dev->options = hdpvr_default_options;
318 314
319 if (default_video_input < HDPVR_VIDEO_INPUTS) 315 if (default_video_input < HDPVR_VIDEO_INPUTS)
@@ -404,9 +400,7 @@ reg_fail:
404#endif 400#endif
405error: 401error:
406 if (dev) { 402 if (dev) {
407 /* Destroy single thread */ 403 flush_work(&dev->worker);
408 if (dev->workqueue)
409 destroy_workqueue(dev->workqueue);
410 /* this frees allocated memory */ 404 /* this frees allocated memory */
411 hdpvr_delete(dev); 405 hdpvr_delete(dev);
412 } 406 }
@@ -427,7 +421,7 @@ static void hdpvr_disconnect(struct usb_interface *interface)
427 mutex_unlock(&dev->io_mutex); 421 mutex_unlock(&dev->io_mutex);
428 v4l2_device_disconnect(&dev->v4l2_dev); 422 v4l2_device_disconnect(&dev->v4l2_dev);
429 msleep(100); 423 msleep(100);
430 flush_workqueue(dev->workqueue); 424 flush_work(&dev->worker);
431 mutex_lock(&dev->io_mutex); 425 mutex_lock(&dev->io_mutex);
432 hdpvr_cancel_queue(dev); 426 hdpvr_cancel_queue(dev);
433 mutex_unlock(&dev->io_mutex); 427 mutex_unlock(&dev->io_mutex);
diff --git a/drivers/media/usb/hdpvr/hdpvr-video.c b/drivers/media/usb/hdpvr/hdpvr-video.c
index ba7f02270c83..2a3a8b470555 100644
--- a/drivers/media/usb/hdpvr/hdpvr-video.c
+++ b/drivers/media/usb/hdpvr/hdpvr-video.c
@@ -316,7 +316,7 @@ static int hdpvr_start_streaming(struct hdpvr_device *dev)
316 dev->status = STATUS_STREAMING; 316 dev->status = STATUS_STREAMING;
317 317
318 INIT_WORK(&dev->worker, hdpvr_transmit_buffers); 318 INIT_WORK(&dev->worker, hdpvr_transmit_buffers);
319 queue_work(dev->workqueue, &dev->worker); 319 schedule_work(&dev->worker);
320 320
321 v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev, 321 v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev,
322 "streaming started\n"); 322 "streaming started\n");
@@ -350,7 +350,7 @@ static int hdpvr_stop_streaming(struct hdpvr_device *dev)
350 wake_up_interruptible(&dev->wait_buffer); 350 wake_up_interruptible(&dev->wait_buffer);
351 msleep(50); 351 msleep(50);
352 352
353 flush_workqueue(dev->workqueue); 353 flush_work(&dev->worker);
354 354
355 mutex_lock(&dev->io_mutex); 355 mutex_lock(&dev->io_mutex);
356 /* kill the still outstanding urbs */ 356 /* kill the still outstanding urbs */
@@ -1123,7 +1123,7 @@ static void hdpvr_device_release(struct video_device *vdev)
1123 1123
1124 hdpvr_delete(dev); 1124 hdpvr_delete(dev);
1125 mutex_lock(&dev->io_mutex); 1125 mutex_lock(&dev->io_mutex);
1126 destroy_workqueue(dev->workqueue); 1126 flush_work(&dev->worker);
1127 mutex_unlock(&dev->io_mutex); 1127 mutex_unlock(&dev->io_mutex);
1128 1128
1129 v4l2_device_unregister(&dev->v4l2_dev); 1129 v4l2_device_unregister(&dev->v4l2_dev);
diff --git a/drivers/media/usb/hdpvr/hdpvr.h b/drivers/media/usb/hdpvr/hdpvr.h
index 78e815441f95..a12e0af1d4e1 100644
--- a/drivers/media/usb/hdpvr/hdpvr.h
+++ b/drivers/media/usb/hdpvr/hdpvr.h
@@ -107,8 +107,6 @@ struct hdpvr_device {
107 /* waitqueue for data */ 107 /* waitqueue for data */
108 wait_queue_head_t wait_data; 108 wait_queue_head_t wait_data;
109 /**/ 109 /**/
110 struct workqueue_struct *workqueue;
111 /**/
112 struct work_struct worker; 110 struct work_struct worker;
113 /* current stream owner */ 111 /* current stream owner */
114 struct v4l2_fh *owner; 112 struct v4l2_fh *owner;
diff --git a/drivers/media/usb/msi2500/msi2500.c b/drivers/media/usb/msi2500/msi2500.c
index 2d33033682af..e7f167d44c61 100644
--- a/drivers/media/usb/msi2500/msi2500.c
+++ b/drivers/media/usb/msi2500/msi2500.c
@@ -618,7 +618,7 @@ static int msi2500_querycap(struct file *file, void *fh,
618static int msi2500_queue_setup(struct vb2_queue *vq, 618static int msi2500_queue_setup(struct vb2_queue *vq,
619 unsigned int *nbuffers, 619 unsigned int *nbuffers,
620 unsigned int *nplanes, unsigned int sizes[], 620 unsigned int *nplanes, unsigned int sizes[],
621 void *alloc_ctxs[]) 621 struct device *alloc_devs[])
622{ 622{
623 struct msi2500_dev *dev = vb2_get_drv_priv(vq); 623 struct msi2500_dev *dev = vb2_get_drv_priv(vq);
624 624
diff --git a/drivers/media/usb/pvrusb2/pvrusb2-hdw.c b/drivers/media/usb/pvrusb2/pvrusb2-hdw.c
index 83e9a3eb3859..fe20fe4f2330 100644
--- a/drivers/media/usb/pvrusb2/pvrusb2-hdw.c
+++ b/drivers/media/usb/pvrusb2/pvrusb2-hdw.c
@@ -2856,11 +2856,15 @@ static void pvr2_subdev_set_control(struct pvr2_hdw *hdw, int id,
2856 const char *name, int val) 2856 const char *name, int val)
2857{ 2857{
2858 struct v4l2_control ctrl; 2858 struct v4l2_control ctrl;
2859 struct v4l2_subdev *sd;
2860
2859 pvr2_trace(PVR2_TRACE_CHIPS, "subdev v4l2 %s=%d", name, val); 2861 pvr2_trace(PVR2_TRACE_CHIPS, "subdev v4l2 %s=%d", name, val);
2860 memset(&ctrl, 0, sizeof(ctrl)); 2862 memset(&ctrl, 0, sizeof(ctrl));
2861 ctrl.id = id; 2863 ctrl.id = id;
2862 ctrl.value = val; 2864 ctrl.value = val;
2863 v4l2_device_call_all(&hdw->v4l2_dev, 0, core, s_ctrl, &ctrl); 2865
2866 v4l2_device_for_each_subdev(sd, &hdw->v4l2_dev)
2867 v4l2_s_ctrl(NULL, sd->ctrl_handler, &ctrl);
2864} 2868}
2865 2869
2866#define PVR2_SUBDEV_SET_CONTROL(hdw, id, lab) \ 2870#define PVR2_SUBDEV_SET_CONTROL(hdw, id, lab) \
diff --git a/drivers/media/usb/pwc/pwc-if.c b/drivers/media/usb/pwc/pwc-if.c
index 18aed5dd325e..b51b27a3fd61 100644
--- a/drivers/media/usb/pwc/pwc-if.c
+++ b/drivers/media/usb/pwc/pwc-if.c
@@ -573,7 +573,7 @@ static void pwc_video_release(struct v4l2_device *v)
573 573
574static int queue_setup(struct vb2_queue *vq, 574static int queue_setup(struct vb2_queue *vq,
575 unsigned int *nbuffers, unsigned int *nplanes, 575 unsigned int *nbuffers, unsigned int *nplanes,
576 unsigned int sizes[], void *alloc_ctxs[]) 576 unsigned int sizes[], struct device *alloc_devs[])
577{ 577{
578 struct pwc_device *pdev = vb2_get_drv_priv(vq); 578 struct pwc_device *pdev = vb2_get_drv_priv(vq);
579 int size; 579 int size;
@@ -1118,8 +1118,10 @@ static int usb_pwc_probe(struct usb_interface *intf, const struct usb_device_id
1118 1118
1119 return 0; 1119 return 0;
1120 1120
1121#ifdef CONFIG_USB_PWC_INPUT_EVDEV
1121err_video_unreg: 1122err_video_unreg:
1122 video_unregister_device(&pdev->vdev); 1123 video_unregister_device(&pdev->vdev);
1124#endif
1123err_unregister_v4l2_dev: 1125err_unregister_v4l2_dev:
1124 v4l2_device_unregister(&pdev->v4l2_dev); 1126 v4l2_device_unregister(&pdev->v4l2_dev);
1125err_free_controls: 1127err_free_controls:
diff --git a/drivers/media/usb/s2255/s2255drv.c b/drivers/media/usb/s2255/s2255drv.c
index 9acdaa3716fb..43ba71a7d02b 100644
--- a/drivers/media/usb/s2255/s2255drv.c
+++ b/drivers/media/usb/s2255/s2255drv.c
@@ -662,7 +662,7 @@ static void s2255_fillbuff(struct s2255_vc *vc,
662 662
663static int queue_setup(struct vb2_queue *vq, 663static int queue_setup(struct vb2_queue *vq,
664 unsigned int *nbuffers, unsigned int *nplanes, 664 unsigned int *nbuffers, unsigned int *nplanes,
665 unsigned int sizes[], void *alloc_ctxs[]) 665 unsigned int sizes[], struct device *alloc_devs[])
666{ 666{
667 struct s2255_vc *vc = vb2_get_drv_priv(vq); 667 struct s2255_vc *vc = vb2_get_drv_priv(vq);
668 if (*nbuffers < S2255_MIN_BUFS) 668 if (*nbuffers < S2255_MIN_BUFS)
diff --git a/drivers/media/usb/stk1160/stk1160-v4l.c b/drivers/media/usb/stk1160/stk1160-v4l.c
index 77131fd614a5..5fab3bee8c74 100644
--- a/drivers/media/usb/stk1160/stk1160-v4l.c
+++ b/drivers/media/usb/stk1160/stk1160-v4l.c
@@ -666,7 +666,7 @@ static const struct v4l2_ioctl_ops stk1160_ioctl_ops = {
666 */ 666 */
667static int queue_setup(struct vb2_queue *vq, 667static int queue_setup(struct vb2_queue *vq,
668 unsigned int *nbuffers, unsigned int *nplanes, 668 unsigned int *nbuffers, unsigned int *nplanes,
669 unsigned int sizes[], void *alloc_ctxs[]) 669 unsigned int sizes[], struct device *alloc_devs[])
670{ 670{
671 struct stk1160 *dev = vb2_get_drv_priv(vq); 671 struct stk1160 *dev = vb2_get_drv_priv(vq);
672 unsigned long size; 672 unsigned long size;
@@ -680,6 +680,9 @@ static int queue_setup(struct vb2_queue *vq,
680 *nbuffers = clamp_t(unsigned int, *nbuffers, 680 *nbuffers = clamp_t(unsigned int, *nbuffers,
681 STK1160_MIN_VIDEO_BUFFERS, STK1160_MAX_VIDEO_BUFFERS); 681 STK1160_MIN_VIDEO_BUFFERS, STK1160_MAX_VIDEO_BUFFERS);
682 682
683 if (*nplanes)
684 return sizes[0] < size ? -EINVAL : 0;
685
683 /* This means a packed colorformat */ 686 /* This means a packed colorformat */
684 *nplanes = 1; 687 *nplanes = 1;
685 688
diff --git a/drivers/media/usb/usbtv/usbtv-audio.c b/drivers/media/usb/usbtv/usbtv-audio.c
index 78c12d22dfbb..1965ff1b1f12 100644
--- a/drivers/media/usb/usbtv/usbtv-audio.c
+++ b/drivers/media/usb/usbtv/usbtv-audio.c
@@ -1,13 +1,6 @@
1/* 1/*
2 * Fushicai USBTV007 Audio-Video Grabber Driver
3 *
4 * Product web site:
5 * http://www.fushicai.com/products_detail/&productId=d05449ee-b690-42f9-a661-aa7353894bed.html
6 *
7 * Copyright (c) 2013 Federico Simoncelli 2 * Copyright (c) 2013 Federico Simoncelli
8 * All rights reserved. 3 * All rights reserved.
9 * No physical hardware was harmed running Windows during the
10 * reverse-engineering activity
11 * 4 *
12 * Redistribution and use in source and binary forms, with or without 5 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions 6 * modification, are permitted provided that the following conditions
@@ -20,6 +13,27 @@
20 * 13 *
21 * Alternatively, this software may be distributed under the terms of the 14 * Alternatively, this software may be distributed under the terms of the
22 * GNU General Public License ("GPL"). 15 * GNU General Public License ("GPL").
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29/*
30 * Fushicai USBTV007 Audio-Video Grabber Driver
31 *
32 * Product web site:
33 * http://www.fushicai.com/products_detail/&productId=d05449ee-b690-42f9-a661-aa7353894bed.html
34 *
35 * No physical hardware was harmed running Windows during the
36 * reverse-engineering activity
23 */ 37 */
24 38
25#include <sound/core.h> 39#include <sound/core.h>
@@ -278,6 +292,9 @@ static void snd_usbtv_trigger(struct work_struct *work)
278{ 292{
279 struct usbtv *chip = container_of(work, struct usbtv, snd_trigger); 293 struct usbtv *chip = container_of(work, struct usbtv, snd_trigger);
280 294
295 if (!chip->snd)
296 return;
297
281 if (atomic_read(&chip->snd_stream)) 298 if (atomic_read(&chip->snd_stream))
282 usbtv_audio_start(chip); 299 usbtv_audio_start(chip);
283 else 300 else
@@ -378,6 +395,8 @@ err:
378 395
379void usbtv_audio_free(struct usbtv *usbtv) 396void usbtv_audio_free(struct usbtv *usbtv)
380{ 397{
398 cancel_work_sync(&usbtv->snd_trigger);
399
381 if (usbtv->snd && usbtv->udev) { 400 if (usbtv->snd && usbtv->udev) {
382 snd_card_free(usbtv->snd); 401 snd_card_free(usbtv->snd);
383 usbtv->snd = NULL; 402 usbtv->snd = NULL;
diff --git a/drivers/media/usb/usbtv/usbtv-core.c b/drivers/media/usb/usbtv/usbtv-core.c
index 29428bef272c..dc76fd41e00f 100644
--- a/drivers/media/usb/usbtv/usbtv-core.c
+++ b/drivers/media/usb/usbtv/usbtv-core.c
@@ -1,19 +1,6 @@
1/* 1/*
2 * Fushicai USBTV007 Audio-Video Grabber Driver
3 *
4 * Product web site:
5 * http://www.fushicai.com/products_detail/&productId=d05449ee-b690-42f9-a661-aa7353894bed.html
6 *
7 * Following LWN articles were very useful in construction of this driver:
8 * Video4Linux2 API series: http://lwn.net/Articles/203924/
9 * videobuf2 API explanation: http://lwn.net/Articles/447435/
10 * Thanks go to Jonathan Corbet for providing this quality documentation.
11 * He is awesome.
12 *
13 * Copyright (c) 2013 Lubomir Rintel 2 * Copyright (c) 2013 Lubomir Rintel
14 * All rights reserved. 3 * All rights reserved.
15 * No physical hardware was harmed running Windows during the
16 * reverse-engineering activity
17 * 4 *
18 * Redistribution and use in source and binary forms, with or without 5 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions 6 * modification, are permitted provided that the following conditions
@@ -26,6 +13,33 @@
26 * 13 *
27 * Alternatively, this software may be distributed under the terms of the 14 * Alternatively, this software may be distributed under the terms of the
28 * GNU General Public License ("GPL"). 15 * GNU General Public License ("GPL").
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29/*
30 * Fushicai USBTV007 Audio-Video Grabber Driver
31 *
32 * Product web site:
33 * http://www.fushicai.com/products_detail/&productId=d05449ee-b690-42f9-a661-aa7353894bed.html
34 *
35 * Following LWN articles were very useful in construction of this driver:
36 * Video4Linux2 API series: http://lwn.net/Articles/203924/
37 * videobuf2 API explanation: http://lwn.net/Articles/447435/
38 * Thanks go to Jonathan Corbet for providing this quality documentation.
39 * He is awesome.
40 *
41 * No physical hardware was harmed running Windows during the
42 * reverse-engineering activity
29 */ 43 */
30 44
31#include "usbtv.h" 45#include "usbtv.h"
diff --git a/drivers/media/usb/usbtv/usbtv-video.c b/drivers/media/usb/usbtv/usbtv-video.c
index f6cfad46547e..2a089756c988 100644
--- a/drivers/media/usb/usbtv/usbtv-video.c
+++ b/drivers/media/usb/usbtv/usbtv-video.c
@@ -1,19 +1,6 @@
1/* 1/*
2 * Fushicai USBTV007 Audio-Video Grabber Driver
3 *
4 * Product web site:
5 * http://www.fushicai.com/products_detail/&productId=d05449ee-b690-42f9-a661-aa7353894bed.html
6 *
7 * Following LWN articles were very useful in construction of this driver:
8 * Video4Linux2 API series: http://lwn.net/Articles/203924/
9 * videobuf2 API explanation: http://lwn.net/Articles/447435/
10 * Thanks go to Jonathan Corbet for providing this quality documentation.
11 * He is awesome.
12 *
13 * Copyright (c) 2013 Lubomir Rintel 2 * Copyright (c) 2013 Lubomir Rintel
14 * All rights reserved. 3 * All rights reserved.
15 * No physical hardware was harmed running Windows during the
16 * reverse-engineering activity
17 * 4 *
18 * Redistribution and use in source and binary forms, with or without 5 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions 6 * modification, are permitted provided that the following conditions
@@ -26,6 +13,33 @@
26 * 13 *
27 * Alternatively, this software may be distributed under the terms of the 14 * Alternatively, this software may be distributed under the terms of the
28 * GNU General Public License ("GPL"). 15 * GNU General Public License ("GPL").
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29/*
30 * Fushicai USBTV007 Audio-Video Grabber Driver
31 *
32 * Product web site:
33 * http://www.fushicai.com/products_detail/&productId=d05449ee-b690-42f9-a661-aa7353894bed.html
34 *
35 * Following LWN articles were very useful in construction of this driver:
36 * Video4Linux2 API series: http://lwn.net/Articles/203924/
37 * videobuf2 API explanation: http://lwn.net/Articles/447435/
38 * Thanks go to Jonathan Corbet for providing this quality documentation.
39 * He is awesome.
40 *
41 * No physical hardware was harmed running Windows during the
42 * reverse-engineering activity
29 */ 43 */
30 44
31#include <media/v4l2-ioctl.h> 45#include <media/v4l2-ioctl.h>
@@ -251,8 +265,23 @@ static int usbtv_setup_capture(struct usbtv *usbtv)
251/* Copy data from chunk into a frame buffer, deinterlacing the data 265/* Copy data from chunk into a frame buffer, deinterlacing the data
252 * into every second line. Unfortunately, they don't align nicely into 266 * into every second line. Unfortunately, they don't align nicely into
253 * 720 pixel lines, as the chunk is 240 words long, which is 480 pixels. 267 * 720 pixel lines, as the chunk is 240 words long, which is 480 pixels.
254 * Therefore, we break down the chunk into two halves before copyting, 268 * Therefore, we break down the chunk into two halves before copying,
255 * so that we can interleave a line if needed. */ 269 * so that we can interleave a line if needed.
270 *
271 * Each "chunk" is 240 words; a word in this context equals 4 bytes.
272 * Image format is YUYV/YUV 4:2:2, consisting of Y Cr Y Cb, defining two
273 * pixels, the Cr and Cb shared between the two pixels, but each having
274 * separate Y values. Thus, the 240 words equal 480 pixels. It therefore,
275 * takes 1.5 chunks to make a 720 pixel-wide line for the frame.
276 * The image is interlaced, so there is a "scan" of odd lines, followed
277 * by "scan" of even numbered lines.
278 *
279 * Following code is writing the chunks in correct sequence, skipping
280 * the rows based on "odd" value.
281 * line 1: chunk[0][ 0..479] chunk[0][480..959] chunk[1][ 0..479]
282 * line 3: chunk[1][480..959] chunk[2][ 0..479] chunk[2][480..959]
283 * ...etc.
284 */
256static void usbtv_chunk_to_vbuf(u32 *frame, __be32 *src, int chunk_no, int odd) 285static void usbtv_chunk_to_vbuf(u32 *frame, __be32 *src, int chunk_no, int odd)
257{ 286{
258 int half; 287 int half;
@@ -608,7 +637,7 @@ static struct v4l2_file_operations usbtv_fops = {
608 637
609static int usbtv_queue_setup(struct vb2_queue *vq, 638static int usbtv_queue_setup(struct vb2_queue *vq,
610 unsigned int *nbuffers, 639 unsigned int *nbuffers,
611 unsigned int *nplanes, unsigned int sizes[], void *alloc_ctxs[]) 640 unsigned int *nplanes, unsigned int sizes[], struct device *alloc_devs[])
612{ 641{
613 struct usbtv *usbtv = vb2_get_drv_priv(vq); 642 struct usbtv *usbtv = vb2_get_drv_priv(vq);
614 unsigned size = USBTV_CHUNK * usbtv->n_chunks * 2 * sizeof(u32); 643 unsigned size = USBTV_CHUNK * usbtv->n_chunks * 2 * sizeof(u32);
diff --git a/drivers/media/usb/usbtv/usbtv.h b/drivers/media/usb/usbtv/usbtv.h
index 161b38d5cfa0..011f9fdc77a9 100644
--- a/drivers/media/usb/usbtv/usbtv.h
+++ b/drivers/media/usb/usbtv/usbtv.h
@@ -1,10 +1,6 @@
1/* 1/*
2 * Fushicai USBTV007 Audio-Video Grabber Driver
3 *
4 * Copyright (c) 2013 Lubomir Rintel 2 * Copyright (c) 2013 Lubomir Rintel
5 * All rights reserved. 3 * All rights reserved.
6 * No physical hardware was harmed running Windows during the
7 * reverse-engineering activity
8 * 4 *
9 * Redistribution and use in source and binary forms, with or without 5 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions 6 * modification, are permitted provided that the following conditions
@@ -17,6 +13,24 @@
17 * 13 *
18 * Alternatively, this software may be distributed under the terms of the 14 * Alternatively, this software may be distributed under the terms of the
19 * GNU General Public License ("GPL"). 15 * GNU General Public License ("GPL").
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29/*
30 * Fushicai USBTV007 Audio-Video Grabber Driver
31 *
32 * No physical hardware was harmed running Windows during the
33 * reverse-engineering activity
20 */ 34 */
21 35
22#include <linux/module.h> 36#include <linux/module.h>
diff --git a/drivers/media/usb/usbvision/usbvision-core.c b/drivers/media/usb/usbvision/usbvision-core.c
index 1ea04e75fb36..52ac4391582c 100644
--- a/drivers/media/usb/usbvision/usbvision-core.c
+++ b/drivers/media/usb/usbvision/usbvision-core.c
@@ -88,11 +88,6 @@ MODULE_PARM_DESC(adjust_y_offset, "adjust Y offset display [core]");
88#define DBG_SCRATCH (1 << 4) 88#define DBG_SCRATCH (1 << 4)
89#define DBG_FUNC (1 << 5) 89#define DBG_FUNC (1 << 5)
90 90
91static const int max_imgwidth = MAX_FRAME_WIDTH;
92static const int max_imgheight = MAX_FRAME_HEIGHT;
93static const int min_imgwidth = MIN_FRAME_WIDTH;
94static const int min_imgheight = MIN_FRAME_HEIGHT;
95
96/* The value of 'scratch_buf_size' affects quality of the picture 91/* The value of 'scratch_buf_size' affects quality of the picture
97 * in many ways. Shorter buffers may cause loss of data when client 92 * in many ways. Shorter buffers may cause loss of data when client
98 * is too slow. Larger buffers are memory-consuming and take longer 93 * is too slow. Larger buffers are memory-consuming and take longer
diff --git a/drivers/media/usb/usbvision/usbvision-video.c b/drivers/media/usb/usbvision/usbvision-video.c
index ad2f3d27b266..c8b4eb2ee7a2 100644
--- a/drivers/media/usb/usbvision/usbvision-video.c
+++ b/drivers/media/usb/usbvision/usbvision-video.c
@@ -188,12 +188,10 @@ static ssize_t show_hue(struct device *cd,
188{ 188{
189 struct video_device *vdev = to_video_device(cd); 189 struct video_device *vdev = to_video_device(cd);
190 struct usb_usbvision *usbvision = video_get_drvdata(vdev); 190 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
191 struct v4l2_control ctrl; 191 s32 val = v4l2_ctrl_g_ctrl(v4l2_ctrl_find(&usbvision->hdl,
192 ctrl.id = V4L2_CID_HUE; 192 V4L2_CID_HUE));
193 ctrl.value = 0; 193
194 if (usbvision->user) 194 return sprintf(buf, "%d\n", val);
195 call_all(usbvision, core, g_ctrl, &ctrl);
196 return sprintf(buf, "%d\n", ctrl.value);
197} 195}
198static DEVICE_ATTR(hue, S_IRUGO, show_hue, NULL); 196static DEVICE_ATTR(hue, S_IRUGO, show_hue, NULL);
199 197
@@ -202,12 +200,10 @@ static ssize_t show_contrast(struct device *cd,
202{ 200{
203 struct video_device *vdev = to_video_device(cd); 201 struct video_device *vdev = to_video_device(cd);
204 struct usb_usbvision *usbvision = video_get_drvdata(vdev); 202 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
205 struct v4l2_control ctrl; 203 s32 val = v4l2_ctrl_g_ctrl(v4l2_ctrl_find(&usbvision->hdl,
206 ctrl.id = V4L2_CID_CONTRAST; 204 V4L2_CID_CONTRAST));
207 ctrl.value = 0; 205
208 if (usbvision->user) 206 return sprintf(buf, "%d\n", val);
209 call_all(usbvision, core, g_ctrl, &ctrl);
210 return sprintf(buf, "%d\n", ctrl.value);
211} 207}
212static DEVICE_ATTR(contrast, S_IRUGO, show_contrast, NULL); 208static DEVICE_ATTR(contrast, S_IRUGO, show_contrast, NULL);
213 209
@@ -216,12 +212,10 @@ static ssize_t show_brightness(struct device *cd,
216{ 212{
217 struct video_device *vdev = to_video_device(cd); 213 struct video_device *vdev = to_video_device(cd);
218 struct usb_usbvision *usbvision = video_get_drvdata(vdev); 214 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
219 struct v4l2_control ctrl; 215 s32 val = v4l2_ctrl_g_ctrl(v4l2_ctrl_find(&usbvision->hdl,
220 ctrl.id = V4L2_CID_BRIGHTNESS; 216 V4L2_CID_BRIGHTNESS));
221 ctrl.value = 0; 217
222 if (usbvision->user) 218 return sprintf(buf, "%d\n", val);
223 call_all(usbvision, core, g_ctrl, &ctrl);
224 return sprintf(buf, "%d\n", ctrl.value);
225} 219}
226static DEVICE_ATTR(brightness, S_IRUGO, show_brightness, NULL); 220static DEVICE_ATTR(brightness, S_IRUGO, show_brightness, NULL);
227 221
@@ -230,12 +224,10 @@ static ssize_t show_saturation(struct device *cd,
230{ 224{
231 struct video_device *vdev = to_video_device(cd); 225 struct video_device *vdev = to_video_device(cd);
232 struct usb_usbvision *usbvision = video_get_drvdata(vdev); 226 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
233 struct v4l2_control ctrl; 227 s32 val = v4l2_ctrl_g_ctrl(v4l2_ctrl_find(&usbvision->hdl,
234 ctrl.id = V4L2_CID_SATURATION; 228 V4L2_CID_SATURATION));
235 ctrl.value = 0; 229
236 if (usbvision->user) 230 return sprintf(buf, "%d\n", val);
237 call_all(usbvision, core, g_ctrl, &ctrl);
238 return sprintf(buf, "%d\n", ctrl.value);
239} 231}
240static DEVICE_ATTR(saturation, S_IRUGO, show_saturation, NULL); 232static DEVICE_ATTR(saturation, S_IRUGO, show_saturation, NULL);
241 233
diff --git a/drivers/media/usb/uvc/uvc_driver.c b/drivers/media/usb/uvc/uvc_driver.c
index 451e84e962e2..302e284a95eb 100644
--- a/drivers/media/usb/uvc/uvc_driver.c
+++ b/drivers/media/usb/uvc/uvc_driver.c
@@ -1674,7 +1674,7 @@ static void uvc_delete(struct uvc_device *dev)
1674 if (dev->vdev.dev) 1674 if (dev->vdev.dev)
1675 v4l2_device_unregister(&dev->vdev); 1675 v4l2_device_unregister(&dev->vdev);
1676#ifdef CONFIG_MEDIA_CONTROLLER 1676#ifdef CONFIG_MEDIA_CONTROLLER
1677 if (media_devnode_is_registered(&dev->mdev.devnode)) 1677 if (media_devnode_is_registered(dev->mdev.devnode))
1678 media_device_unregister(&dev->mdev); 1678 media_device_unregister(&dev->mdev);
1679 media_device_cleanup(&dev->mdev); 1679 media_device_cleanup(&dev->mdev);
1680#endif 1680#endif
diff --git a/drivers/media/usb/uvc/uvc_queue.c b/drivers/media/usb/uvc/uvc_queue.c
index 54394722756f..773fefb52d7a 100644
--- a/drivers/media/usb/uvc/uvc_queue.c
+++ b/drivers/media/usb/uvc/uvc_queue.c
@@ -71,7 +71,7 @@ static void uvc_queue_return_buffers(struct uvc_video_queue *queue,
71 71
72static int uvc_queue_setup(struct vb2_queue *vq, 72static int uvc_queue_setup(struct vb2_queue *vq,
73 unsigned int *nbuffers, unsigned int *nplanes, 73 unsigned int *nbuffers, unsigned int *nplanes,
74 unsigned int sizes[], void *alloc_ctxs[]) 74 unsigned int sizes[], struct device *alloc_devs[])
75{ 75{
76 struct uvc_video_queue *queue = vb2_get_drv_priv(vq); 76 struct uvc_video_queue *queue = vb2_get_drv_priv(vq);
77 struct uvc_streaming *stream = uvc_queue_to_stream(queue); 77 struct uvc_streaming *stream = uvc_queue_to_stream(queue);
diff --git a/drivers/media/usb/uvc/uvc_v4l2.c b/drivers/media/usb/uvc/uvc_v4l2.c
index c04bc6afb965..05eed4be25df 100644
--- a/drivers/media/usb/uvc/uvc_v4l2.c
+++ b/drivers/media/usb/uvc/uvc_v4l2.c
@@ -142,6 +142,21 @@ static __u32 uvc_try_frame_interval(struct uvc_frame *frame, __u32 interval)
142 return interval; 142 return interval;
143} 143}
144 144
145static __u32 uvc_v4l2_get_bytesperline(const struct uvc_format *format,
146 const struct uvc_frame *frame)
147{
148 switch (format->fcc) {
149 case V4L2_PIX_FMT_NV12:
150 case V4L2_PIX_FMT_YVU420:
151 case V4L2_PIX_FMT_YUV420:
152 case V4L2_PIX_FMT_M420:
153 return frame->wWidth;
154
155 default:
156 return format->bpp * frame->wWidth / 8;
157 }
158}
159
145static int uvc_v4l2_try_format(struct uvc_streaming *stream, 160static int uvc_v4l2_try_format(struct uvc_streaming *stream,
146 struct v4l2_format *fmt, struct uvc_streaming_control *probe, 161 struct v4l2_format *fmt, struct uvc_streaming_control *probe,
147 struct uvc_format **uvc_format, struct uvc_frame **uvc_frame) 162 struct uvc_format **uvc_format, struct uvc_frame **uvc_frame)
@@ -245,7 +260,7 @@ static int uvc_v4l2_try_format(struct uvc_streaming *stream,
245 fmt->fmt.pix.width = frame->wWidth; 260 fmt->fmt.pix.width = frame->wWidth;
246 fmt->fmt.pix.height = frame->wHeight; 261 fmt->fmt.pix.height = frame->wHeight;
247 fmt->fmt.pix.field = V4L2_FIELD_NONE; 262 fmt->fmt.pix.field = V4L2_FIELD_NONE;
248 fmt->fmt.pix.bytesperline = format->bpp * frame->wWidth / 8; 263 fmt->fmt.pix.bytesperline = uvc_v4l2_get_bytesperline(format, frame);
249 fmt->fmt.pix.sizeimage = probe->dwMaxVideoFrameSize; 264 fmt->fmt.pix.sizeimage = probe->dwMaxVideoFrameSize;
250 fmt->fmt.pix.colorspace = format->colorspace; 265 fmt->fmt.pix.colorspace = format->colorspace;
251 fmt->fmt.pix.priv = 0; 266 fmt->fmt.pix.priv = 0;
@@ -282,7 +297,7 @@ static int uvc_v4l2_get_format(struct uvc_streaming *stream,
282 fmt->fmt.pix.width = frame->wWidth; 297 fmt->fmt.pix.width = frame->wWidth;
283 fmt->fmt.pix.height = frame->wHeight; 298 fmt->fmt.pix.height = frame->wHeight;
284 fmt->fmt.pix.field = V4L2_FIELD_NONE; 299 fmt->fmt.pix.field = V4L2_FIELD_NONE;
285 fmt->fmt.pix.bytesperline = format->bpp * frame->wWidth / 8; 300 fmt->fmt.pix.bytesperline = uvc_v4l2_get_bytesperline(format, frame);
286 fmt->fmt.pix.sizeimage = stream->ctrl.dwMaxVideoFrameSize; 301 fmt->fmt.pix.sizeimage = stream->ctrl.dwMaxVideoFrameSize;
287 fmt->fmt.pix.colorspace = format->colorspace; 302 fmt->fmt.pix.colorspace = format->colorspace;
288 fmt->fmt.pix.priv = 0; 303 fmt->fmt.pix.priv = 0;
diff --git a/drivers/media/usb/uvc/uvc_video.c b/drivers/media/usb/uvc/uvc_video.c
index 075a0fe77485..b5589d5f5da4 100644
--- a/drivers/media/usb/uvc/uvc_video.c
+++ b/drivers/media/usb/uvc/uvc_video.c
@@ -1470,6 +1470,7 @@ static unsigned int uvc_endpoint_max_bpi(struct usb_device *dev,
1470 1470
1471 switch (dev->speed) { 1471 switch (dev->speed) {
1472 case USB_SPEED_SUPER: 1472 case USB_SPEED_SUPER:
1473 case USB_SPEED_SUPER_PLUS:
1473 return le16_to_cpu(ep->ss_ep_comp.wBytesPerInterval); 1474 return le16_to_cpu(ep->ss_ep_comp.wBytesPerInterval);
1474 case USB_SPEED_HIGH: 1475 case USB_SPEED_HIGH:
1475 psize = usb_endpoint_maxp(&ep->desc); 1476 psize = usb_endpoint_maxp(&ep->desc);
diff --git a/drivers/media/v4l2-core/v4l2-ctrls.c b/drivers/media/v4l2-core/v4l2-ctrls.c
index 8b321e0aae62..f7abfad9ad23 100644
--- a/drivers/media/v4l2-core/v4l2-ctrls.c
+++ b/drivers/media/v4l2-core/v4l2-ctrls.c
@@ -2606,14 +2606,6 @@ int v4l2_queryctrl(struct v4l2_ctrl_handler *hdl, struct v4l2_queryctrl *qc)
2606} 2606}
2607EXPORT_SYMBOL(v4l2_queryctrl); 2607EXPORT_SYMBOL(v4l2_queryctrl);
2608 2608
2609int v4l2_subdev_queryctrl(struct v4l2_subdev *sd, struct v4l2_queryctrl *qc)
2610{
2611 if (qc->id & (V4L2_CTRL_FLAG_NEXT_CTRL | V4L2_CTRL_FLAG_NEXT_COMPOUND))
2612 return -EINVAL;
2613 return v4l2_queryctrl(sd->ctrl_handler, qc);
2614}
2615EXPORT_SYMBOL(v4l2_subdev_queryctrl);
2616
2617/* Implement VIDIOC_QUERYMENU */ 2609/* Implement VIDIOC_QUERYMENU */
2618int v4l2_querymenu(struct v4l2_ctrl_handler *hdl, struct v4l2_querymenu *qm) 2610int v4l2_querymenu(struct v4l2_ctrl_handler *hdl, struct v4l2_querymenu *qm)
2619{ 2611{
@@ -2657,13 +2649,6 @@ int v4l2_querymenu(struct v4l2_ctrl_handler *hdl, struct v4l2_querymenu *qm)
2657} 2649}
2658EXPORT_SYMBOL(v4l2_querymenu); 2650EXPORT_SYMBOL(v4l2_querymenu);
2659 2651
2660int v4l2_subdev_querymenu(struct v4l2_subdev *sd, struct v4l2_querymenu *qm)
2661{
2662 return v4l2_querymenu(sd->ctrl_handler, qm);
2663}
2664EXPORT_SYMBOL(v4l2_subdev_querymenu);
2665
2666
2667 2652
2668/* Some general notes on the atomic requirements of VIDIOC_G/TRY/S_EXT_CTRLS: 2653/* Some general notes on the atomic requirements of VIDIOC_G/TRY/S_EXT_CTRLS:
2669 2654
@@ -2890,12 +2875,6 @@ int v4l2_g_ext_ctrls(struct v4l2_ctrl_handler *hdl, struct v4l2_ext_controls *cs
2890} 2875}
2891EXPORT_SYMBOL(v4l2_g_ext_ctrls); 2876EXPORT_SYMBOL(v4l2_g_ext_ctrls);
2892 2877
2893int v4l2_subdev_g_ext_ctrls(struct v4l2_subdev *sd, struct v4l2_ext_controls *cs)
2894{
2895 return v4l2_g_ext_ctrls(sd->ctrl_handler, cs);
2896}
2897EXPORT_SYMBOL(v4l2_subdev_g_ext_ctrls);
2898
2899/* Helper function to get a single control */ 2878/* Helper function to get a single control */
2900static int get_ctrl(struct v4l2_ctrl *ctrl, struct v4l2_ext_control *c) 2879static int get_ctrl(struct v4l2_ctrl *ctrl, struct v4l2_ext_control *c)
2901{ 2880{
@@ -2941,12 +2920,6 @@ int v4l2_g_ctrl(struct v4l2_ctrl_handler *hdl, struct v4l2_control *control)
2941} 2920}
2942EXPORT_SYMBOL(v4l2_g_ctrl); 2921EXPORT_SYMBOL(v4l2_g_ctrl);
2943 2922
2944int v4l2_subdev_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *control)
2945{
2946 return v4l2_g_ctrl(sd->ctrl_handler, control);
2947}
2948EXPORT_SYMBOL(v4l2_subdev_g_ctrl);
2949
2950s32 v4l2_ctrl_g_ctrl(struct v4l2_ctrl *ctrl) 2923s32 v4l2_ctrl_g_ctrl(struct v4l2_ctrl *ctrl)
2951{ 2924{
2952 struct v4l2_ext_control c; 2925 struct v4l2_ext_control c;
@@ -3194,18 +3167,6 @@ int v4l2_s_ext_ctrls(struct v4l2_fh *fh, struct v4l2_ctrl_handler *hdl,
3194} 3167}
3195EXPORT_SYMBOL(v4l2_s_ext_ctrls); 3168EXPORT_SYMBOL(v4l2_s_ext_ctrls);
3196 3169
3197int v4l2_subdev_try_ext_ctrls(struct v4l2_subdev *sd, struct v4l2_ext_controls *cs)
3198{
3199 return try_set_ext_ctrls(NULL, sd->ctrl_handler, cs, false);
3200}
3201EXPORT_SYMBOL(v4l2_subdev_try_ext_ctrls);
3202
3203int v4l2_subdev_s_ext_ctrls(struct v4l2_subdev *sd, struct v4l2_ext_controls *cs)
3204{
3205 return try_set_ext_ctrls(NULL, sd->ctrl_handler, cs, true);
3206}
3207EXPORT_SYMBOL(v4l2_subdev_s_ext_ctrls);
3208
3209/* Helper function for VIDIOC_S_CTRL compatibility */ 3170/* Helper function for VIDIOC_S_CTRL compatibility */
3210static int set_ctrl(struct v4l2_fh *fh, struct v4l2_ctrl *ctrl, u32 ch_flags) 3171static int set_ctrl(struct v4l2_fh *fh, struct v4l2_ctrl *ctrl, u32 ch_flags)
3211{ 3172{
@@ -3268,12 +3229,6 @@ int v4l2_s_ctrl(struct v4l2_fh *fh, struct v4l2_ctrl_handler *hdl,
3268} 3229}
3269EXPORT_SYMBOL(v4l2_s_ctrl); 3230EXPORT_SYMBOL(v4l2_s_ctrl);
3270 3231
3271int v4l2_subdev_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *control)
3272{
3273 return v4l2_s_ctrl(NULL, sd->ctrl_handler, control);
3274}
3275EXPORT_SYMBOL(v4l2_subdev_s_ctrl);
3276
3277int __v4l2_ctrl_s_ctrl(struct v4l2_ctrl *ctrl, s32 val) 3232int __v4l2_ctrl_s_ctrl(struct v4l2_ctrl *ctrl, s32 val)
3278{ 3233{
3279 lockdep_assert_held(ctrl->handler->lock); 3234 lockdep_assert_held(ctrl->handler->lock);
diff --git a/drivers/media/v4l2-core/v4l2-flash-led-class.c b/drivers/media/v4l2-core/v4l2-flash-led-class.c
index fc5ff8b215f9..ae7544d5469a 100644
--- a/drivers/media/v4l2-core/v4l2-flash-led-class.c
+++ b/drivers/media/v4l2-core/v4l2-flash-led-class.c
@@ -609,14 +609,7 @@ static const struct v4l2_subdev_internal_ops v4l2_flash_subdev_internal_ops = {
609 .close = v4l2_flash_close, 609 .close = v4l2_flash_close,
610}; 610};
611 611
612static const struct v4l2_subdev_core_ops v4l2_flash_core_ops = { 612static const struct v4l2_subdev_ops v4l2_flash_subdev_ops;
613 .queryctrl = v4l2_subdev_queryctrl,
614 .querymenu = v4l2_subdev_querymenu,
615};
616
617static const struct v4l2_subdev_ops v4l2_flash_subdev_ops = {
618 .core = &v4l2_flash_core_ops,
619};
620 613
621struct v4l2_flash *v4l2_flash_init( 614struct v4l2_flash *v4l2_flash_init(
622 struct device *dev, struct device_node *of_node, 615 struct device *dev, struct device_node *of_node,
diff --git a/drivers/media/v4l2-core/v4l2-ioctl.c b/drivers/media/v4l2-core/v4l2-ioctl.c
index 528390f33b53..51a0fa144392 100644
--- a/drivers/media/v4l2-core/v4l2-ioctl.c
+++ b/drivers/media/v4l2-core/v4l2-ioctl.c
@@ -2541,14 +2541,14 @@ static struct v4l2_ioctl_info v4l2_ioctls[] = {
2541 IOCTL_INFO_FNC(VIDIOC_DBG_S_REGISTER, v4l_dbg_s_register, v4l_print_dbg_register, 0), 2541 IOCTL_INFO_FNC(VIDIOC_DBG_S_REGISTER, v4l_dbg_s_register, v4l_print_dbg_register, 0),
2542 IOCTL_INFO_FNC(VIDIOC_DBG_G_REGISTER, v4l_dbg_g_register, v4l_print_dbg_register, 0), 2542 IOCTL_INFO_FNC(VIDIOC_DBG_G_REGISTER, v4l_dbg_g_register, v4l_print_dbg_register, 0),
2543 IOCTL_INFO_FNC(VIDIOC_S_HW_FREQ_SEEK, v4l_s_hw_freq_seek, v4l_print_hw_freq_seek, INFO_FL_PRIO), 2543 IOCTL_INFO_FNC(VIDIOC_S_HW_FREQ_SEEK, v4l_s_hw_freq_seek, v4l_print_hw_freq_seek, INFO_FL_PRIO),
2544 IOCTL_INFO_STD(VIDIOC_S_DV_TIMINGS, vidioc_s_dv_timings, v4l_print_dv_timings, INFO_FL_PRIO), 2544 IOCTL_INFO_STD(VIDIOC_S_DV_TIMINGS, vidioc_s_dv_timings, v4l_print_dv_timings, INFO_FL_PRIO | INFO_FL_CLEAR(v4l2_dv_timings, bt.flags)),
2545 IOCTL_INFO_STD(VIDIOC_G_DV_TIMINGS, vidioc_g_dv_timings, v4l_print_dv_timings, 0), 2545 IOCTL_INFO_STD(VIDIOC_G_DV_TIMINGS, vidioc_g_dv_timings, v4l_print_dv_timings, 0),
2546 IOCTL_INFO_FNC(VIDIOC_DQEVENT, v4l_dqevent, v4l_print_event, 0), 2546 IOCTL_INFO_FNC(VIDIOC_DQEVENT, v4l_dqevent, v4l_print_event, 0),
2547 IOCTL_INFO_FNC(VIDIOC_SUBSCRIBE_EVENT, v4l_subscribe_event, v4l_print_event_subscription, 0), 2547 IOCTL_INFO_FNC(VIDIOC_SUBSCRIBE_EVENT, v4l_subscribe_event, v4l_print_event_subscription, 0),
2548 IOCTL_INFO_FNC(VIDIOC_UNSUBSCRIBE_EVENT, v4l_unsubscribe_event, v4l_print_event_subscription, 0), 2548 IOCTL_INFO_FNC(VIDIOC_UNSUBSCRIBE_EVENT, v4l_unsubscribe_event, v4l_print_event_subscription, 0),
2549 IOCTL_INFO_FNC(VIDIOC_CREATE_BUFS, v4l_create_bufs, v4l_print_create_buffers, INFO_FL_PRIO | INFO_FL_QUEUE), 2549 IOCTL_INFO_FNC(VIDIOC_CREATE_BUFS, v4l_create_bufs, v4l_print_create_buffers, INFO_FL_PRIO | INFO_FL_QUEUE),
2550 IOCTL_INFO_FNC(VIDIOC_PREPARE_BUF, v4l_prepare_buf, v4l_print_buffer, INFO_FL_QUEUE), 2550 IOCTL_INFO_FNC(VIDIOC_PREPARE_BUF, v4l_prepare_buf, v4l_print_buffer, INFO_FL_QUEUE),
2551 IOCTL_INFO_STD(VIDIOC_ENUM_DV_TIMINGS, vidioc_enum_dv_timings, v4l_print_enum_dv_timings, 0), 2551 IOCTL_INFO_STD(VIDIOC_ENUM_DV_TIMINGS, vidioc_enum_dv_timings, v4l_print_enum_dv_timings, INFO_FL_CLEAR(v4l2_enum_dv_timings, pad)),
2552 IOCTL_INFO_STD(VIDIOC_QUERY_DV_TIMINGS, vidioc_query_dv_timings, v4l_print_dv_timings, 0), 2552 IOCTL_INFO_STD(VIDIOC_QUERY_DV_TIMINGS, vidioc_query_dv_timings, v4l_print_dv_timings, 0),
2553 IOCTL_INFO_STD(VIDIOC_DV_TIMINGS_CAP, vidioc_dv_timings_cap, v4l_print_dv_timings_cap, INFO_FL_CLEAR(v4l2_dv_timings_cap, type)), 2553 IOCTL_INFO_STD(VIDIOC_DV_TIMINGS_CAP, vidioc_dv_timings_cap, v4l_print_dv_timings_cap, INFO_FL_CLEAR(v4l2_dv_timings_cap, type)),
2554 IOCTL_INFO_FNC(VIDIOC_ENUM_FREQ_BANDS, v4l_enum_freq_bands, v4l_print_freq_band, 0), 2554 IOCTL_INFO_FNC(VIDIOC_ENUM_FREQ_BANDS, v4l_enum_freq_bands, v4l_print_freq_band, 0),
diff --git a/drivers/media/v4l2-core/videobuf2-core.c b/drivers/media/v4l2-core/videobuf2-core.c
index 9fbcb67a9ee6..ca8ffeb56d72 100644
--- a/drivers/media/v4l2-core/videobuf2-core.c
+++ b/drivers/media/v4l2-core/videobuf2-core.c
@@ -206,8 +206,9 @@ static int __vb2_buf_mem_alloc(struct vb2_buffer *vb)
206 for (plane = 0; plane < vb->num_planes; ++plane) { 206 for (plane = 0; plane < vb->num_planes; ++plane) {
207 unsigned long size = PAGE_ALIGN(vb->planes[plane].length); 207 unsigned long size = PAGE_ALIGN(vb->planes[plane].length);
208 208
209 mem_priv = call_ptr_memop(vb, alloc, q->alloc_ctx[plane], 209 mem_priv = call_ptr_memop(vb, alloc,
210 size, dma_dir, q->gfp_flags); 210 q->alloc_devs[plane] ? : q->dev,
211 q->dma_attrs, size, dma_dir, q->gfp_flags);
211 if (IS_ERR_OR_NULL(mem_priv)) 212 if (IS_ERR_OR_NULL(mem_priv))
212 goto free; 213 goto free;
213 214
@@ -737,7 +738,7 @@ int vb2_core_reqbufs(struct vb2_queue *q, enum vb2_memory memory,
737 */ 738 */
738 num_buffers = min_t(unsigned int, *count, VB2_MAX_FRAME); 739 num_buffers = min_t(unsigned int, *count, VB2_MAX_FRAME);
739 num_buffers = max_t(unsigned int, num_buffers, q->min_buffers_needed); 740 num_buffers = max_t(unsigned int, num_buffers, q->min_buffers_needed);
740 memset(q->alloc_ctx, 0, sizeof(q->alloc_ctx)); 741 memset(q->alloc_devs, 0, sizeof(q->alloc_devs));
741 q->memory = memory; 742 q->memory = memory;
742 743
743 /* 744 /*
@@ -745,7 +746,7 @@ int vb2_core_reqbufs(struct vb2_queue *q, enum vb2_memory memory,
745 * Driver also sets the size and allocator context for each plane. 746 * Driver also sets the size and allocator context for each plane.
746 */ 747 */
747 ret = call_qop(q, queue_setup, q, &num_buffers, &num_planes, 748 ret = call_qop(q, queue_setup, q, &num_buffers, &num_planes,
748 plane_sizes, q->alloc_ctx); 749 plane_sizes, q->alloc_devs);
749 if (ret) 750 if (ret)
750 return ret; 751 return ret;
751 752
@@ -778,7 +779,7 @@ int vb2_core_reqbufs(struct vb2_queue *q, enum vb2_memory memory,
778 num_planes = 0; 779 num_planes = 0;
779 780
780 ret = call_qop(q, queue_setup, q, &num_buffers, 781 ret = call_qop(q, queue_setup, q, &num_buffers,
781 &num_planes, plane_sizes, q->alloc_ctx); 782 &num_planes, plane_sizes, q->alloc_devs);
782 783
783 if (!ret && allocated_buffers < num_buffers) 784 if (!ret && allocated_buffers < num_buffers)
784 ret = -ENOMEM; 785 ret = -ENOMEM;
@@ -844,7 +845,7 @@ int vb2_core_create_bufs(struct vb2_queue *q, enum vb2_memory memory,
844 } 845 }
845 846
846 if (!q->num_buffers) { 847 if (!q->num_buffers) {
847 memset(q->alloc_ctx, 0, sizeof(q->alloc_ctx)); 848 memset(q->alloc_devs, 0, sizeof(q->alloc_devs));
848 q->memory = memory; 849 q->memory = memory;
849 q->waiting_for_buffers = !q->is_output; 850 q->waiting_for_buffers = !q->is_output;
850 } 851 }
@@ -861,7 +862,7 @@ int vb2_core_create_bufs(struct vb2_queue *q, enum vb2_memory memory,
861 * buffer and their sizes are acceptable 862 * buffer and their sizes are acceptable
862 */ 863 */
863 ret = call_qop(q, queue_setup, q, &num_buffers, 864 ret = call_qop(q, queue_setup, q, &num_buffers,
864 &num_planes, plane_sizes, q->alloc_ctx); 865 &num_planes, plane_sizes, q->alloc_devs);
865 if (ret) 866 if (ret)
866 return ret; 867 return ret;
867 868
@@ -884,7 +885,7 @@ int vb2_core_create_bufs(struct vb2_queue *q, enum vb2_memory memory,
884 * queue driver has set up 885 * queue driver has set up
885 */ 886 */
886 ret = call_qop(q, queue_setup, q, &num_buffers, 887 ret = call_qop(q, queue_setup, q, &num_buffers,
887 &num_planes, plane_sizes, q->alloc_ctx); 888 &num_planes, plane_sizes, q->alloc_devs);
888 889
889 if (!ret && allocated_buffers < num_buffers) 890 if (!ret && allocated_buffers < num_buffers)
890 ret = -ENOMEM; 891 ret = -ENOMEM;
@@ -1131,9 +1132,10 @@ static int __qbuf_userptr(struct vb2_buffer *vb, const void *pb)
1131 vb->planes[plane].data_offset = 0; 1132 vb->planes[plane].data_offset = 0;
1132 1133
1133 /* Acquire each plane's memory */ 1134 /* Acquire each plane's memory */
1134 mem_priv = call_ptr_memop(vb, get_userptr, q->alloc_ctx[plane], 1135 mem_priv = call_ptr_memop(vb, get_userptr,
1135 planes[plane].m.userptr, 1136 q->alloc_devs[plane] ? : q->dev,
1136 planes[plane].length, dma_dir); 1137 planes[plane].m.userptr,
1138 planes[plane].length, dma_dir);
1137 if (IS_ERR_OR_NULL(mem_priv)) { 1139 if (IS_ERR_OR_NULL(mem_priv)) {
1138 dprintk(1, "failed acquiring userspace " 1140 dprintk(1, "failed acquiring userspace "
1139 "memory for plane %d\n", plane); 1141 "memory for plane %d\n", plane);
@@ -1256,8 +1258,8 @@ static int __qbuf_dmabuf(struct vb2_buffer *vb, const void *pb)
1256 1258
1257 /* Acquire each plane's memory */ 1259 /* Acquire each plane's memory */
1258 mem_priv = call_ptr_memop(vb, attach_dmabuf, 1260 mem_priv = call_ptr_memop(vb, attach_dmabuf,
1259 q->alloc_ctx[plane], dbuf, planes[plane].length, 1261 q->alloc_devs[plane] ? : q->dev,
1260 dma_dir); 1262 dbuf, planes[plane].length, dma_dir);
1261 if (IS_ERR(mem_priv)) { 1263 if (IS_ERR(mem_priv)) {
1262 dprintk(1, "failed to attach dmabuf\n"); 1264 dprintk(1, "failed to attach dmabuf\n");
1263 ret = PTR_ERR(mem_priv); 1265 ret = PTR_ERR(mem_priv);
@@ -1648,7 +1650,7 @@ static int __vb2_get_done_vb(struct vb2_queue *q, struct vb2_buffer **vb,
1648 void *pb, int nonblocking) 1650 void *pb, int nonblocking)
1649{ 1651{
1650 unsigned long flags; 1652 unsigned long flags;
1651 int ret; 1653 int ret = 0;
1652 1654
1653 /* 1655 /*
1654 * Wait for at least one buffer to become available on the done_list. 1656 * Wait for at least one buffer to become available on the done_list.
@@ -1664,10 +1666,12 @@ static int __vb2_get_done_vb(struct vb2_queue *q, struct vb2_buffer **vb,
1664 spin_lock_irqsave(&q->done_lock, flags); 1666 spin_lock_irqsave(&q->done_lock, flags);
1665 *vb = list_first_entry(&q->done_list, struct vb2_buffer, done_entry); 1667 *vb = list_first_entry(&q->done_list, struct vb2_buffer, done_entry);
1666 /* 1668 /*
1667 * Only remove the buffer from done_list if v4l2_buffer can handle all 1669 * Only remove the buffer from done_list if all planes can be
1668 * the planes. 1670 * handled. Some cases such as V4L2 file I/O and DVB have pb
1671 * == NULL; skip the check then as there's nothing to verify.
1669 */ 1672 */
1670 ret = call_bufop(q, verify_planes_array, *vb, pb); 1673 if (pb)
1674 ret = call_bufop(q, verify_planes_array, *vb, pb);
1671 if (!ret) 1675 if (!ret)
1672 list_del(&(*vb)->done_entry); 1676 list_del(&(*vb)->done_entry);
1673 spin_unlock_irqrestore(&q->done_lock, flags); 1677 spin_unlock_irqrestore(&q->done_lock, flags);
@@ -1843,7 +1847,7 @@ static void __vb2_queue_cancel(struct vb2_queue *q)
1843 * Make sure to call buf_finish for any queued buffers. Normally 1847 * Make sure to call buf_finish for any queued buffers. Normally
1844 * that's done in dqbuf, but that's not going to happen when we 1848 * that's done in dqbuf, but that's not going to happen when we
1845 * cancel the whole queue. Note: this code belongs here, not in 1849 * cancel the whole queue. Note: this code belongs here, not in
1846 * __vb2_dqbuf() since in vb2_internal_dqbuf() there is a critical 1850 * __vb2_dqbuf() since in vb2_core_dqbuf() there is a critical
1847 * call to __fill_user_buffer() after buf_finish(). That order can't 1851 * call to __fill_user_buffer() after buf_finish(). That order can't
1848 * be changed, so we can't move the buf_finish() to __vb2_dqbuf(). 1852 * be changed, so we can't move the buf_finish() to __vb2_dqbuf().
1849 */ 1853 */
diff --git a/drivers/media/v4l2-core/videobuf2-dma-contig.c b/drivers/media/v4l2-core/videobuf2-dma-contig.c
index 5361197f3e57..863f658a3fa1 100644
--- a/drivers/media/v4l2-core/videobuf2-dma-contig.c
+++ b/drivers/media/v4l2-core/videobuf2-dma-contig.c
@@ -21,11 +21,6 @@
21#include <media/videobuf2-dma-contig.h> 21#include <media/videobuf2-dma-contig.h>
22#include <media/videobuf2-memops.h> 22#include <media/videobuf2-memops.h>
23 23
24struct vb2_dc_conf {
25 struct device *dev;
26 struct dma_attrs attrs;
27};
28
29struct vb2_dc_buf { 24struct vb2_dc_buf {
30 struct device *dev; 25 struct device *dev;
31 void *vaddr; 26 void *vaddr;
@@ -140,18 +135,18 @@ static void vb2_dc_put(void *buf_priv)
140 kfree(buf); 135 kfree(buf);
141} 136}
142 137
143static void *vb2_dc_alloc(void *alloc_ctx, unsigned long size, 138static void *vb2_dc_alloc(struct device *dev, const struct dma_attrs *attrs,
144 enum dma_data_direction dma_dir, gfp_t gfp_flags) 139 unsigned long size, enum dma_data_direction dma_dir,
140 gfp_t gfp_flags)
145{ 141{
146 struct vb2_dc_conf *conf = alloc_ctx;
147 struct device *dev = conf->dev;
148 struct vb2_dc_buf *buf; 142 struct vb2_dc_buf *buf;
149 143
150 buf = kzalloc(sizeof *buf, GFP_KERNEL); 144 buf = kzalloc(sizeof *buf, GFP_KERNEL);
151 if (!buf) 145 if (!buf)
152 return ERR_PTR(-ENOMEM); 146 return ERR_PTR(-ENOMEM);
153 147
154 buf->attrs = conf->attrs; 148 if (attrs)
149 buf->attrs = *attrs;
155 buf->cookie = dma_alloc_attrs(dev, size, &buf->dma_addr, 150 buf->cookie = dma_alloc_attrs(dev, size, &buf->dma_addr,
156 GFP_KERNEL | gfp_flags, &buf->attrs); 151 GFP_KERNEL | gfp_flags, &buf->attrs);
157 if (!buf->cookie) { 152 if (!buf->cookie) {
@@ -478,10 +473,9 @@ static inline dma_addr_t vb2_dc_pfn_to_dma(struct device *dev, unsigned long pfn
478} 473}
479#endif 474#endif
480 475
481static void *vb2_dc_get_userptr(void *alloc_ctx, unsigned long vaddr, 476static void *vb2_dc_get_userptr(struct device *dev, unsigned long vaddr,
482 unsigned long size, enum dma_data_direction dma_dir) 477 unsigned long size, enum dma_data_direction dma_dir)
483{ 478{
484 struct vb2_dc_conf *conf = alloc_ctx;
485 struct vb2_dc_buf *buf; 479 struct vb2_dc_buf *buf;
486 struct frame_vector *vec; 480 struct frame_vector *vec;
487 unsigned long offset; 481 unsigned long offset;
@@ -509,7 +503,7 @@ static void *vb2_dc_get_userptr(void *alloc_ctx, unsigned long vaddr,
509 if (!buf) 503 if (!buf)
510 return ERR_PTR(-ENOMEM); 504 return ERR_PTR(-ENOMEM);
511 505
512 buf->dev = conf->dev; 506 buf->dev = dev;
513 buf->dma_dir = dma_dir; 507 buf->dma_dir = dma_dir;
514 508
515 offset = vaddr & ~PAGE_MASK; 509 offset = vaddr & ~PAGE_MASK;
@@ -676,10 +670,9 @@ static void vb2_dc_detach_dmabuf(void *mem_priv)
676 kfree(buf); 670 kfree(buf);
677} 671}
678 672
679static void *vb2_dc_attach_dmabuf(void *alloc_ctx, struct dma_buf *dbuf, 673static void *vb2_dc_attach_dmabuf(struct device *dev, struct dma_buf *dbuf,
680 unsigned long size, enum dma_data_direction dma_dir) 674 unsigned long size, enum dma_data_direction dma_dir)
681{ 675{
682 struct vb2_dc_conf *conf = alloc_ctx;
683 struct vb2_dc_buf *buf; 676 struct vb2_dc_buf *buf;
684 struct dma_buf_attachment *dba; 677 struct dma_buf_attachment *dba;
685 678
@@ -690,7 +683,7 @@ static void *vb2_dc_attach_dmabuf(void *alloc_ctx, struct dma_buf *dbuf,
690 if (!buf) 683 if (!buf)
691 return ERR_PTR(-ENOMEM); 684 return ERR_PTR(-ENOMEM);
692 685
693 buf->dev = conf->dev; 686 buf->dev = dev;
694 /* create attachment for the dmabuf with the user device */ 687 /* create attachment for the dmabuf with the user device */
695 dba = dma_buf_attach(dbuf, buf->dev); 688 dba = dma_buf_attach(dbuf, buf->dev);
696 if (IS_ERR(dba)) { 689 if (IS_ERR(dba)) {
@@ -729,29 +722,58 @@ const struct vb2_mem_ops vb2_dma_contig_memops = {
729}; 722};
730EXPORT_SYMBOL_GPL(vb2_dma_contig_memops); 723EXPORT_SYMBOL_GPL(vb2_dma_contig_memops);
731 724
732void *vb2_dma_contig_init_ctx_attrs(struct device *dev, 725/**
733 struct dma_attrs *attrs) 726 * vb2_dma_contig_set_max_seg_size() - configure DMA max segment size
727 * @dev: device for configuring DMA parameters
728 * @size: size of DMA max segment size to set
729 *
730 * To allow mapping the scatter-list into a single chunk in the DMA
731 * address space, the device is required to have the DMA max segment
732 * size parameter set to a value larger than the buffer size. Otherwise,
733 * the DMA-mapping subsystem will split the mapping into max segment
734 * size chunks. This function sets the DMA max segment size
735 * parameter to let DMA-mapping map a buffer as a single chunk in DMA
736 * address space.
737 * This code assumes that the DMA-mapping subsystem will merge all
738 * scatterlist segments if this is really possible (for example when
739 * an IOMMU is available and enabled).
740 * Ideally, this parameter should be set by the generic bus code, but it
741 * is left with the default 64KiB value due to historical litmiations in
742 * other subsystems (like limited USB host drivers) and there no good
743 * place to set it to the proper value.
744 * This function should be called from the drivers, which are known to
745 * operate on platforms with IOMMU and provide access to shared buffers
746 * (either USERPTR or DMABUF). This should be done before initializing
747 * videobuf2 queue.
748 */
749int vb2_dma_contig_set_max_seg_size(struct device *dev, unsigned int size)
734{ 750{
735 struct vb2_dc_conf *conf; 751 if (!dev->dma_parms) {
736 752 dev->dma_parms = kzalloc(sizeof(dev->dma_parms), GFP_KERNEL);
737 conf = kzalloc(sizeof *conf, GFP_KERNEL); 753 if (!dev->dma_parms)
738 if (!conf) 754 return -ENOMEM;
739 return ERR_PTR(-ENOMEM); 755 }
740 756 if (dma_get_max_seg_size(dev) < size)
741 conf->dev = dev; 757 return dma_set_max_seg_size(dev, size);
742 if (attrs)
743 conf->attrs = *attrs;
744 758
745 return conf; 759 return 0;
746} 760}
747EXPORT_SYMBOL_GPL(vb2_dma_contig_init_ctx_attrs); 761EXPORT_SYMBOL_GPL(vb2_dma_contig_set_max_seg_size);
748 762
749void vb2_dma_contig_cleanup_ctx(void *alloc_ctx) 763/*
764 * vb2_dma_contig_clear_max_seg_size() - release resources for DMA parameters
765 * @dev: device for configuring DMA parameters
766 *
767 * This function releases resources allocated to configure DMA parameters
768 * (see vb2_dma_contig_set_max_seg_size() function). It should be called from
769 * device drivers on driver remove.
770 */
771void vb2_dma_contig_clear_max_seg_size(struct device *dev)
750{ 772{
751 if (!IS_ERR_OR_NULL(alloc_ctx)) 773 kfree(dev->dma_parms);
752 kfree(alloc_ctx); 774 dev->dma_parms = NULL;
753} 775}
754EXPORT_SYMBOL_GPL(vb2_dma_contig_cleanup_ctx); 776EXPORT_SYMBOL_GPL(vb2_dma_contig_clear_max_seg_size);
755 777
756MODULE_DESCRIPTION("DMA-contig memory handling routines for videobuf2"); 778MODULE_DESCRIPTION("DMA-contig memory handling routines for videobuf2");
757MODULE_AUTHOR("Pawel Osciak <pawel@osciak.com>"); 779MODULE_AUTHOR("Pawel Osciak <pawel@osciak.com>");
diff --git a/drivers/media/v4l2-core/videobuf2-dma-sg.c b/drivers/media/v4l2-core/videobuf2-dma-sg.c
index 9985c89f0513..a39db8a6db7a 100644
--- a/drivers/media/v4l2-core/videobuf2-dma-sg.c
+++ b/drivers/media/v4l2-core/videobuf2-dma-sg.c
@@ -30,10 +30,6 @@ module_param(debug, int, 0644);
30 printk(KERN_DEBUG "vb2-dma-sg: " fmt, ## arg); \ 30 printk(KERN_DEBUG "vb2-dma-sg: " fmt, ## arg); \
31 } while (0) 31 } while (0)
32 32
33struct vb2_dma_sg_conf {
34 struct device *dev;
35};
36
37struct vb2_dma_sg_buf { 33struct vb2_dma_sg_buf {
38 struct device *dev; 34 struct device *dev;
39 void *vaddr; 35 void *vaddr;
@@ -99,10 +95,10 @@ static int vb2_dma_sg_alloc_compacted(struct vb2_dma_sg_buf *buf,
99 return 0; 95 return 0;
100} 96}
101 97
102static void *vb2_dma_sg_alloc(void *alloc_ctx, unsigned long size, 98static void *vb2_dma_sg_alloc(struct device *dev, const struct dma_attrs *dma_attrs,
103 enum dma_data_direction dma_dir, gfp_t gfp_flags) 99 unsigned long size, enum dma_data_direction dma_dir,
100 gfp_t gfp_flags)
104{ 101{
105 struct vb2_dma_sg_conf *conf = alloc_ctx;
106 struct vb2_dma_sg_buf *buf; 102 struct vb2_dma_sg_buf *buf;
107 struct sg_table *sgt; 103 struct sg_table *sgt;
108 int ret; 104 int ret;
@@ -111,7 +107,7 @@ static void *vb2_dma_sg_alloc(void *alloc_ctx, unsigned long size,
111 107
112 dma_set_attr(DMA_ATTR_SKIP_CPU_SYNC, &attrs); 108 dma_set_attr(DMA_ATTR_SKIP_CPU_SYNC, &attrs);
113 109
114 if (WARN_ON(alloc_ctx == NULL)) 110 if (WARN_ON(dev == NULL))
115 return NULL; 111 return NULL;
116 buf = kzalloc(sizeof *buf, GFP_KERNEL); 112 buf = kzalloc(sizeof *buf, GFP_KERNEL);
117 if (!buf) 113 if (!buf)
@@ -140,7 +136,7 @@ static void *vb2_dma_sg_alloc(void *alloc_ctx, unsigned long size,
140 goto fail_table_alloc; 136 goto fail_table_alloc;
141 137
142 /* Prevent the device from being released while the buffer is used */ 138 /* Prevent the device from being released while the buffer is used */
143 buf->dev = get_device(conf->dev); 139 buf->dev = get_device(dev);
144 140
145 sgt = &buf->sg_table; 141 sgt = &buf->sg_table;
146 /* 142 /*
@@ -226,11 +222,10 @@ static void vb2_dma_sg_finish(void *buf_priv)
226 dma_sync_sg_for_cpu(buf->dev, sgt->sgl, sgt->orig_nents, buf->dma_dir); 222 dma_sync_sg_for_cpu(buf->dev, sgt->sgl, sgt->orig_nents, buf->dma_dir);
227} 223}
228 224
229static void *vb2_dma_sg_get_userptr(void *alloc_ctx, unsigned long vaddr, 225static void *vb2_dma_sg_get_userptr(struct device *dev, unsigned long vaddr,
230 unsigned long size, 226 unsigned long size,
231 enum dma_data_direction dma_dir) 227 enum dma_data_direction dma_dir)
232{ 228{
233 struct vb2_dma_sg_conf *conf = alloc_ctx;
234 struct vb2_dma_sg_buf *buf; 229 struct vb2_dma_sg_buf *buf;
235 struct sg_table *sgt; 230 struct sg_table *sgt;
236 DEFINE_DMA_ATTRS(attrs); 231 DEFINE_DMA_ATTRS(attrs);
@@ -242,7 +237,7 @@ static void *vb2_dma_sg_get_userptr(void *alloc_ctx, unsigned long vaddr,
242 return NULL; 237 return NULL;
243 238
244 buf->vaddr = NULL; 239 buf->vaddr = NULL;
245 buf->dev = conf->dev; 240 buf->dev = dev;
246 buf->dma_dir = dma_dir; 241 buf->dma_dir = dma_dir;
247 buf->offset = vaddr & ~PAGE_MASK; 242 buf->offset = vaddr & ~PAGE_MASK;
248 buf->size = size; 243 buf->size = size;
@@ -616,10 +611,9 @@ static void vb2_dma_sg_detach_dmabuf(void *mem_priv)
616 kfree(buf); 611 kfree(buf);
617} 612}
618 613
619static void *vb2_dma_sg_attach_dmabuf(void *alloc_ctx, struct dma_buf *dbuf, 614static void *vb2_dma_sg_attach_dmabuf(struct device *dev, struct dma_buf *dbuf,
620 unsigned long size, enum dma_data_direction dma_dir) 615 unsigned long size, enum dma_data_direction dma_dir)
621{ 616{
622 struct vb2_dma_sg_conf *conf = alloc_ctx;
623 struct vb2_dma_sg_buf *buf; 617 struct vb2_dma_sg_buf *buf;
624 struct dma_buf_attachment *dba; 618 struct dma_buf_attachment *dba;
625 619
@@ -630,7 +624,7 @@ static void *vb2_dma_sg_attach_dmabuf(void *alloc_ctx, struct dma_buf *dbuf,
630 if (!buf) 624 if (!buf)
631 return ERR_PTR(-ENOMEM); 625 return ERR_PTR(-ENOMEM);
632 626
633 buf->dev = conf->dev; 627 buf->dev = dev;
634 /* create attachment for the dmabuf with the user device */ 628 /* create attachment for the dmabuf with the user device */
635 dba = dma_buf_attach(dbuf, buf->dev); 629 dba = dma_buf_attach(dbuf, buf->dev);
636 if (IS_ERR(dba)) { 630 if (IS_ERR(dba)) {
@@ -672,27 +666,6 @@ const struct vb2_mem_ops vb2_dma_sg_memops = {
672}; 666};
673EXPORT_SYMBOL_GPL(vb2_dma_sg_memops); 667EXPORT_SYMBOL_GPL(vb2_dma_sg_memops);
674 668
675void *vb2_dma_sg_init_ctx(struct device *dev)
676{
677 struct vb2_dma_sg_conf *conf;
678
679 conf = kzalloc(sizeof(*conf), GFP_KERNEL);
680 if (!conf)
681 return ERR_PTR(-ENOMEM);
682
683 conf->dev = dev;
684
685 return conf;
686}
687EXPORT_SYMBOL_GPL(vb2_dma_sg_init_ctx);
688
689void vb2_dma_sg_cleanup_ctx(void *alloc_ctx)
690{
691 if (!IS_ERR_OR_NULL(alloc_ctx))
692 kfree(alloc_ctx);
693}
694EXPORT_SYMBOL_GPL(vb2_dma_sg_cleanup_ctx);
695
696MODULE_DESCRIPTION("dma scatter/gather memory handling routines for videobuf2"); 669MODULE_DESCRIPTION("dma scatter/gather memory handling routines for videobuf2");
697MODULE_AUTHOR("Andrzej Pietrasiewicz"); 670MODULE_AUTHOR("Andrzej Pietrasiewicz");
698MODULE_LICENSE("GPL"); 671MODULE_LICENSE("GPL");
diff --git a/drivers/media/v4l2-core/videobuf2-v4l2.c b/drivers/media/v4l2-core/videobuf2-v4l2.c
index 0b1b8c7b6ce5..9cfbb6e4bc28 100644
--- a/drivers/media/v4l2-core/videobuf2-v4l2.c
+++ b/drivers/media/v4l2-core/videobuf2-v4l2.c
@@ -74,6 +74,11 @@ static int __verify_planes_array(struct vb2_buffer *vb, const struct v4l2_buffer
74 return 0; 74 return 0;
75} 75}
76 76
77static int __verify_planes_array_core(struct vb2_buffer *vb, const void *pb)
78{
79 return __verify_planes_array(vb, pb);
80}
81
77/** 82/**
78 * __verify_length() - Verify that the bytesused value for each plane fits in 83 * __verify_length() - Verify that the bytesused value for each plane fits in
79 * the plane length and that the data offset doesn't exceed the bytesused value. 84 * the plane length and that the data offset doesn't exceed the bytesused value.
@@ -422,7 +427,7 @@ static int __fill_vb2_buffer(struct vb2_buffer *vb,
422 if (V4L2_TYPE_IS_OUTPUT(b->type)) { 427 if (V4L2_TYPE_IS_OUTPUT(b->type)) {
423 /* 428 /*
424 * For output buffers mask out the timecode flag: 429 * For output buffers mask out the timecode flag:
425 * this will be handled later in vb2_internal_qbuf(). 430 * this will be handled later in vb2_qbuf().
426 * The 'field' is valid metadata for this output buffer 431 * The 'field' is valid metadata for this output buffer
427 * and so that needs to be copied here. 432 * and so that needs to be copied here.
428 */ 433 */
@@ -437,6 +442,7 @@ static int __fill_vb2_buffer(struct vb2_buffer *vb,
437} 442}
438 443
439static const struct vb2_buf_ops v4l2_buf_ops = { 444static const struct vb2_buf_ops v4l2_buf_ops = {
445 .verify_planes_array = __verify_planes_array_core,
440 .fill_user_buffer = __fill_v4l2_buffer, 446 .fill_user_buffer = __fill_v4l2_buffer,
441 .fill_vb2_buffer = __fill_vb2_buffer, 447 .fill_vb2_buffer = __fill_vb2_buffer,
442 .copy_timestamp = __copy_timestamp, 448 .copy_timestamp = __copy_timestamp,
@@ -580,13 +586,6 @@ int vb2_create_bufs(struct vb2_queue *q, struct v4l2_create_buffers *create)
580} 586}
581EXPORT_SYMBOL_GPL(vb2_create_bufs); 587EXPORT_SYMBOL_GPL(vb2_create_bufs);
582 588
583static int vb2_internal_qbuf(struct vb2_queue *q, struct v4l2_buffer *b)
584{
585 int ret = vb2_queue_or_prepare_buf(q, b, "qbuf");
586
587 return ret ? ret : vb2_core_qbuf(q, b->index, b);
588}
589
590/** 589/**
591 * vb2_qbuf() - Queue a buffer from userspace 590 * vb2_qbuf() - Queue a buffer from userspace
592 * @q: videobuf2 queue 591 * @q: videobuf2 queue
@@ -606,30 +605,18 @@ static int vb2_internal_qbuf(struct vb2_queue *q, struct v4l2_buffer *b)
606 */ 605 */
607int vb2_qbuf(struct vb2_queue *q, struct v4l2_buffer *b) 606int vb2_qbuf(struct vb2_queue *q, struct v4l2_buffer *b)
608{ 607{
608 int ret;
609
609 if (vb2_fileio_is_active(q)) { 610 if (vb2_fileio_is_active(q)) {
610 dprintk(1, "file io in progress\n"); 611 dprintk(1, "file io in progress\n");
611 return -EBUSY; 612 return -EBUSY;
612 } 613 }
613 614
614 return vb2_internal_qbuf(q, b); 615 ret = vb2_queue_or_prepare_buf(q, b, "qbuf");
616 return ret ? ret : vb2_core_qbuf(q, b->index, b);
615} 617}
616EXPORT_SYMBOL_GPL(vb2_qbuf); 618EXPORT_SYMBOL_GPL(vb2_qbuf);
617 619
618static int vb2_internal_dqbuf(struct vb2_queue *q, struct v4l2_buffer *b,
619 bool nonblocking)
620{
621 int ret;
622
623 if (b->type != q->type) {
624 dprintk(1, "invalid buffer type\n");
625 return -EINVAL;
626 }
627
628 ret = vb2_core_dqbuf(q, NULL, b, nonblocking);
629
630 return ret;
631}
632
633/** 620/**
634 * vb2_dqbuf() - Dequeue a buffer to the userspace 621 * vb2_dqbuf() - Dequeue a buffer to the userspace
635 * @q: videobuf2 queue 622 * @q: videobuf2 queue
@@ -653,11 +640,27 @@ static int vb2_internal_dqbuf(struct vb2_queue *q, struct v4l2_buffer *b,
653 */ 640 */
654int vb2_dqbuf(struct vb2_queue *q, struct v4l2_buffer *b, bool nonblocking) 641int vb2_dqbuf(struct vb2_queue *q, struct v4l2_buffer *b, bool nonblocking)
655{ 642{
643 int ret;
644
656 if (vb2_fileio_is_active(q)) { 645 if (vb2_fileio_is_active(q)) {
657 dprintk(1, "file io in progress\n"); 646 dprintk(1, "file io in progress\n");
658 return -EBUSY; 647 return -EBUSY;
659 } 648 }
660 return vb2_internal_dqbuf(q, b, nonblocking); 649
650 if (b->type != q->type) {
651 dprintk(1, "invalid buffer type\n");
652 return -EINVAL;
653 }
654
655 ret = vb2_core_dqbuf(q, NULL, b, nonblocking);
656
657 /*
658 * After calling the VIDIOC_DQBUF V4L2_BUF_FLAG_DONE must be
659 * cleared.
660 */
661 b->flags &= ~V4L2_BUF_FLAG_DONE;
662
663 return ret;
661} 664}
662EXPORT_SYMBOL_GPL(vb2_dqbuf); 665EXPORT_SYMBOL_GPL(vb2_dqbuf);
663 666
diff --git a/drivers/media/v4l2-core/videobuf2-vmalloc.c b/drivers/media/v4l2-core/videobuf2-vmalloc.c
index 1c302743a1fd..7e8a07ed8d82 100644
--- a/drivers/media/v4l2-core/videobuf2-vmalloc.c
+++ b/drivers/media/v4l2-core/videobuf2-vmalloc.c
@@ -33,8 +33,9 @@ struct vb2_vmalloc_buf {
33 33
34static void vb2_vmalloc_put(void *buf_priv); 34static void vb2_vmalloc_put(void *buf_priv);
35 35
36static void *vb2_vmalloc_alloc(void *alloc_ctx, unsigned long size, 36static void *vb2_vmalloc_alloc(struct device *dev, const struct dma_attrs *attrs,
37 enum dma_data_direction dma_dir, gfp_t gfp_flags) 37 unsigned long size, enum dma_data_direction dma_dir,
38 gfp_t gfp_flags)
38{ 39{
39 struct vb2_vmalloc_buf *buf; 40 struct vb2_vmalloc_buf *buf;
40 41
@@ -69,7 +70,7 @@ static void vb2_vmalloc_put(void *buf_priv)
69 } 70 }
70} 71}
71 72
72static void *vb2_vmalloc_get_userptr(void *alloc_ctx, unsigned long vaddr, 73static void *vb2_vmalloc_get_userptr(struct device *dev, unsigned long vaddr,
73 unsigned long size, 74 unsigned long size,
74 enum dma_data_direction dma_dir) 75 enum dma_data_direction dma_dir)
75{ 76{
@@ -403,7 +404,7 @@ static void vb2_vmalloc_detach_dmabuf(void *mem_priv)
403 kfree(buf); 404 kfree(buf);
404} 405}
405 406
406static void *vb2_vmalloc_attach_dmabuf(void *alloc_ctx, struct dma_buf *dbuf, 407static void *vb2_vmalloc_attach_dmabuf(struct device *dev, struct dma_buf *dbuf,
407 unsigned long size, enum dma_data_direction dma_dir) 408 unsigned long size, enum dma_data_direction dma_dir)
408{ 409{
409 struct vb2_vmalloc_buf *buf; 410 struct vb2_vmalloc_buf *buf;
diff --git a/drivers/of/of_reserved_mem.c b/drivers/of/of_reserved_mem.c
index 216648233874..06af99f64ad8 100644
--- a/drivers/of/of_reserved_mem.c
+++ b/drivers/of/of_reserved_mem.c
@@ -21,6 +21,7 @@
21#include <linux/sizes.h> 21#include <linux/sizes.h>
22#include <linux/of_reserved_mem.h> 22#include <linux/of_reserved_mem.h>
23#include <linux/sort.h> 23#include <linux/sort.h>
24#include <linux/slab.h>
24 25
25#define MAX_RESERVED_REGIONS 16 26#define MAX_RESERVED_REGIONS 16
26static struct reserved_mem reserved_mem[MAX_RESERVED_REGIONS]; 27static struct reserved_mem reserved_mem[MAX_RESERVED_REGIONS];
@@ -296,53 +297,95 @@ static inline struct reserved_mem *__find_rmem(struct device_node *node)
296 return NULL; 297 return NULL;
297} 298}
298 299
300struct rmem_assigned_device {
301 struct device *dev;
302 struct reserved_mem *rmem;
303 struct list_head list;
304};
305
306static LIST_HEAD(of_rmem_assigned_device_list);
307static DEFINE_MUTEX(of_rmem_assigned_device_mutex);
308
299/** 309/**
300 * of_reserved_mem_device_init() - assign reserved memory region to given device 310 * of_reserved_mem_device_init_by_idx() - assign reserved memory region to
311 * given device
312 * @dev: Pointer to the device to configure
313 * @np: Pointer to the device_node with 'reserved-memory' property
314 * @idx: Index of selected region
301 * 315 *
302 * This function assign memory region pointed by "memory-region" device tree 316 * This function assigns respective DMA-mapping operations based on reserved
303 * property to the given device. 317 * memory region specified by 'memory-region' property in @np node to the @dev
318 * device. When driver needs to use more than one reserved memory region, it
319 * should allocate child devices and initialize regions by name for each of
320 * child device.
321 *
322 * Returns error code or zero on success.
304 */ 323 */
305int of_reserved_mem_device_init(struct device *dev) 324int of_reserved_mem_device_init_by_idx(struct device *dev,
325 struct device_node *np, int idx)
306{ 326{
327 struct rmem_assigned_device *rd;
328 struct device_node *target;
307 struct reserved_mem *rmem; 329 struct reserved_mem *rmem;
308 struct device_node *np;
309 int ret; 330 int ret;
310 331
311 np = of_parse_phandle(dev->of_node, "memory-region", 0); 332 if (!np || !dev)
312 if (!np) 333 return -EINVAL;
334
335 target = of_parse_phandle(np, "memory-region", idx);
336 if (!target)
313 return -ENODEV; 337 return -ENODEV;
314 338
315 rmem = __find_rmem(np); 339 rmem = __find_rmem(target);
316 of_node_put(np); 340 of_node_put(target);
317 341
318 if (!rmem || !rmem->ops || !rmem->ops->device_init) 342 if (!rmem || !rmem->ops || !rmem->ops->device_init)
319 return -EINVAL; 343 return -EINVAL;
320 344
345 rd = kmalloc(sizeof(struct rmem_assigned_device), GFP_KERNEL);
346 if (!rd)
347 return -ENOMEM;
348
321 ret = rmem->ops->device_init(rmem, dev); 349 ret = rmem->ops->device_init(rmem, dev);
322 if (ret == 0) 350 if (ret == 0) {
351 rd->dev = dev;
352 rd->rmem = rmem;
353
354 mutex_lock(&of_rmem_assigned_device_mutex);
355 list_add(&rd->list, &of_rmem_assigned_device_list);
356 mutex_unlock(&of_rmem_assigned_device_mutex);
357
323 dev_info(dev, "assigned reserved memory node %s\n", rmem->name); 358 dev_info(dev, "assigned reserved memory node %s\n", rmem->name);
359 } else {
360 kfree(rd);
361 }
324 362
325 return ret; 363 return ret;
326} 364}
327EXPORT_SYMBOL_GPL(of_reserved_mem_device_init); 365EXPORT_SYMBOL_GPL(of_reserved_mem_device_init_by_idx);
328 366
329/** 367/**
330 * of_reserved_mem_device_release() - release reserved memory device structures 368 * of_reserved_mem_device_release() - release reserved memory device structures
369 * @dev: Pointer to the device to deconfigure
331 * 370 *
332 * This function releases structures allocated for memory region handling for 371 * This function releases structures allocated for memory region handling for
333 * the given device. 372 * the given device.
334 */ 373 */
335void of_reserved_mem_device_release(struct device *dev) 374void of_reserved_mem_device_release(struct device *dev)
336{ 375{
337 struct reserved_mem *rmem; 376 struct rmem_assigned_device *rd;
338 struct device_node *np; 377 struct reserved_mem *rmem = NULL;
339 378
340 np = of_parse_phandle(dev->of_node, "memory-region", 0); 379 mutex_lock(&of_rmem_assigned_device_mutex);
341 if (!np) 380 list_for_each_entry(rd, &of_rmem_assigned_device_list, list) {
342 return; 381 if (rd->dev == dev) {
343 382 rmem = rd->rmem;
344 rmem = __find_rmem(np); 383 list_del(&rd->list);
345 of_node_put(np); 384 kfree(rd);
385 break;
386 }
387 }
388 mutex_unlock(&of_rmem_assigned_device_mutex);
346 389
347 if (!rmem || !rmem->ops || !rmem->ops->device_release) 390 if (!rmem || !rmem->ops || !rmem->ops->device_release)
348 return; 391 return;
diff --git a/drivers/staging/media/Kconfig b/drivers/staging/media/Kconfig
index de7e9f52e7eb..cae42e56f270 100644
--- a/drivers/staging/media/Kconfig
+++ b/drivers/staging/media/Kconfig
@@ -21,24 +21,20 @@ if STAGING_MEDIA
21# Please keep them in alphabetic order 21# Please keep them in alphabetic order
22source "drivers/staging/media/bcm2048/Kconfig" 22source "drivers/staging/media/bcm2048/Kconfig"
23 23
24source "drivers/staging/media/cec/Kconfig"
25
24source "drivers/staging/media/cxd2099/Kconfig" 26source "drivers/staging/media/cxd2099/Kconfig"
25 27
26source "drivers/staging/media/davinci_vpfe/Kconfig" 28source "drivers/staging/media/davinci_vpfe/Kconfig"
27 29
28source "drivers/staging/media/mn88472/Kconfig"
29
30source "drivers/staging/media/mx2/Kconfig"
31
32source "drivers/staging/media/mx3/Kconfig"
33
34source "drivers/staging/media/omap1/Kconfig"
35
36source "drivers/staging/media/omap4iss/Kconfig" 30source "drivers/staging/media/omap4iss/Kconfig"
37 31
38source "drivers/staging/media/timb/Kconfig" 32source "drivers/staging/media/pulse8-cec/Kconfig"
39 33
40source "drivers/staging/media/tw686x-kh/Kconfig" 34source "drivers/staging/media/tw686x-kh/Kconfig"
41 35
36source "drivers/staging/media/s5p-cec/Kconfig"
37
42# Keep LIRC at the end, as it has sub-menus 38# Keep LIRC at the end, as it has sub-menus
43source "drivers/staging/media/lirc/Kconfig" 39source "drivers/staging/media/lirc/Kconfig"
44 40
diff --git a/drivers/staging/media/Makefile b/drivers/staging/media/Makefile
index 60a35b3a47e7..87ce8ad1e22a 100644
--- a/drivers/staging/media/Makefile
+++ b/drivers/staging/media/Makefile
@@ -1,11 +1,9 @@
1obj-$(CONFIG_I2C_BCM2048) += bcm2048/ 1obj-$(CONFIG_I2C_BCM2048) += bcm2048/
2obj-$(CONFIG_MEDIA_CEC) += cec/
3obj-$(CONFIG_VIDEO_SAMSUNG_S5P_CEC) += s5p-cec/
2obj-$(CONFIG_DVB_CXD2099) += cxd2099/ 4obj-$(CONFIG_DVB_CXD2099) += cxd2099/
3obj-$(CONFIG_LIRC_STAGING) += lirc/ 5obj-$(CONFIG_LIRC_STAGING) += lirc/
4obj-$(CONFIG_VIDEO_DM365_VPFE) += davinci_vpfe/ 6obj-$(CONFIG_VIDEO_DM365_VPFE) += davinci_vpfe/
5obj-$(CONFIG_VIDEO_MX2) += mx2/
6obj-$(CONFIG_VIDEO_MX3) += mx3/
7obj-$(CONFIG_VIDEO_OMAP1) += omap1/
8obj-$(CONFIG_VIDEO_OMAP4) += omap4iss/ 7obj-$(CONFIG_VIDEO_OMAP4) += omap4iss/
9obj-$(CONFIG_DVB_MN88472) += mn88472/ 8obj-$(CONFIG_USB_PULSE8_CEC) += pulse8-cec/
10obj-$(CONFIG_VIDEO_TIMBERDALE) += timb/
11obj-$(CONFIG_VIDEO_TW686X_KH) += tw686x-kh/ 9obj-$(CONFIG_VIDEO_TW686X_KH) += tw686x-kh/
diff --git a/drivers/staging/media/cec/Kconfig b/drivers/staging/media/cec/Kconfig
new file mode 100644
index 000000000000..21457a1f6c9f
--- /dev/null
+++ b/drivers/staging/media/cec/Kconfig
@@ -0,0 +1,15 @@
1config MEDIA_CEC
2 bool "CEC API (EXPERIMENTAL)"
3 depends on MEDIA_SUPPORT
4 select MEDIA_CEC_EDID
5 ---help---
6 Enable the CEC API.
7
8 To compile this driver as a module, choose M here: the
9 module will be called cec.
10
11config MEDIA_CEC_DEBUG
12 bool "CEC debugfs interface (EXPERIMENTAL)"
13 depends on MEDIA_CEC && DEBUG_FS
14 ---help---
15 Turns on the DebugFS interface for CEC devices.
diff --git a/drivers/staging/media/cec/Makefile b/drivers/staging/media/cec/Makefile
new file mode 100644
index 000000000000..bd7f3c593468
--- /dev/null
+++ b/drivers/staging/media/cec/Makefile
@@ -0,0 +1,5 @@
1cec-objs := cec-core.o cec-adap.o cec-api.o
2
3ifeq ($(CONFIG_MEDIA_CEC),y)
4 obj-$(CONFIG_MEDIA_SUPPORT) += cec.o
5endif
diff --git a/drivers/staging/media/cec/TODO b/drivers/staging/media/cec/TODO
new file mode 100644
index 000000000000..a10d4f82b954
--- /dev/null
+++ b/drivers/staging/media/cec/TODO
@@ -0,0 +1,31 @@
1The reason why cec.c is still in staging is that I would like
2to have a bit more confidence in the uABI. The kABI is fine,
3no problem there, but I would like to let the public API mature
4a bit.
5
6Once I'm confident that I didn't miss anything then the cec.c source
7can move to drivers/media and the linux/cec.h and linux/cec-funcs.h
8headers can move to uapi/linux and added to uapi/linux/Kbuild to make
9them public.
10
11Hopefully this will happen later in 2016.
12
13Other TODOs:
14
15- Add a flag to inhibit passing CEC RC messages to the rc subsystem.
16 Applications should be able to choose this when calling S_LOG_ADDRS.
17- If the reply field of cec_msg is set then when the reply arrives it
18 is only sent to the filehandle that transmitted the original message
19 and not to any followers. Should this behavior change or perhaps
20 controlled through a cec_msg flag?
21- Should CEC_LOG_ADDR_TYPE_SPECIFIC be replaced by TYPE_2ND_TV and TYPE_PROCESSOR?
22 And also TYPE_SWITCH and TYPE_CDC_ONLY in addition to the TYPE_UNREGISTERED?
23 This should give the framework more information about the device type
24 since SPECIFIC and UNREGISTERED give no useful information.
25- Once this is out of staging this should no longer be a separate
26 config option, instead it should be selected by drivers that want it.
27- Revisit the IS_REACHABLE(RC_CORE): perhaps the RC_CORE support should
28 be enabled through a separate config option in drivers/media/Kconfig
29 or rc/Kconfig?
30
31Hans Verkuil <hans.verkuil@cisco.com>
diff --git a/drivers/staging/media/cec/cec-adap.c b/drivers/staging/media/cec/cec-adap.c
new file mode 100644
index 000000000000..9fffddb7ac7e
--- /dev/null
+++ b/drivers/staging/media/cec/cec-adap.c
@@ -0,0 +1,1654 @@
1/*
2 * cec-adap.c - HDMI Consumer Electronics Control framework - CEC adapter
3 *
4 * Copyright 2016 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
5 *
6 * This program is free software; you may redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
11 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
12 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
13 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
14 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
15 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
16 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17 * SOFTWARE.
18 */
19
20#include <linux/errno.h>
21#include <linux/init.h>
22#include <linux/module.h>
23#include <linux/kernel.h>
24#include <linux/kmod.h>
25#include <linux/ktime.h>
26#include <linux/slab.h>
27#include <linux/mm.h>
28#include <linux/string.h>
29#include <linux/types.h>
30
31#include "cec-priv.h"
32
33static int cec_report_features(struct cec_adapter *adap, unsigned int la_idx);
34static int cec_report_phys_addr(struct cec_adapter *adap, unsigned int la_idx);
35
36/*
37 * 400 ms is the time it takes for one 16 byte message to be
38 * transferred and 5 is the maximum number of retries. Add
39 * another 100 ms as a margin. So if the transmit doesn't
40 * finish before that time something is really wrong and we
41 * have to time out.
42 *
43 * This is a sign that something it really wrong and a warning
44 * will be issued.
45 */
46#define CEC_XFER_TIMEOUT_MS (5 * 400 + 100)
47
48#define call_op(adap, op, arg...) \
49 (adap->ops->op ? adap->ops->op(adap, ## arg) : 0)
50
51#define call_void_op(adap, op, arg...) \
52 do { \
53 if (adap->ops->op) \
54 adap->ops->op(adap, ## arg); \
55 } while (0)
56
57static int cec_log_addr2idx(const struct cec_adapter *adap, u8 log_addr)
58{
59 int i;
60
61 for (i = 0; i < adap->log_addrs.num_log_addrs; i++)
62 if (adap->log_addrs.log_addr[i] == log_addr)
63 return i;
64 return -1;
65}
66
67static unsigned int cec_log_addr2dev(const struct cec_adapter *adap, u8 log_addr)
68{
69 int i = cec_log_addr2idx(adap, log_addr);
70
71 return adap->log_addrs.primary_device_type[i < 0 ? 0 : i];
72}
73
74/*
75 * Queue a new event for this filehandle. If ts == 0, then set it
76 * to the current time.
77 *
78 * The two events that are currently defined do not need to keep track
79 * of intermediate events, so no actual queue of events is needed,
80 * instead just store the latest state and the total number of lost
81 * messages.
82 *
83 * Should new events be added in the future that require intermediate
84 * results to be queued as well, then a proper queue data structure is
85 * required. But until then, just keep it simple.
86 */
87void cec_queue_event_fh(struct cec_fh *fh,
88 const struct cec_event *new_ev, u64 ts)
89{
90 struct cec_event *ev = &fh->events[new_ev->event - 1];
91
92 if (ts == 0)
93 ts = ktime_get_ns();
94
95 mutex_lock(&fh->lock);
96 if (new_ev->event == CEC_EVENT_LOST_MSGS &&
97 fh->pending_events & (1 << new_ev->event)) {
98 /*
99 * If there is already a lost_msgs event, then just
100 * update the lost_msgs count. This effectively
101 * merges the old and new events into one.
102 */
103 ev->lost_msgs.lost_msgs += new_ev->lost_msgs.lost_msgs;
104 goto unlock;
105 }
106
107 /*
108 * Intermediate states are not interesting, so just
109 * overwrite any older event.
110 */
111 *ev = *new_ev;
112 ev->ts = ts;
113 fh->pending_events |= 1 << new_ev->event;
114
115unlock:
116 mutex_unlock(&fh->lock);
117 wake_up_interruptible(&fh->wait);
118}
119
120/* Queue a new event for all open filehandles. */
121static void cec_queue_event(struct cec_adapter *adap,
122 const struct cec_event *ev)
123{
124 u64 ts = ktime_get_ns();
125 struct cec_fh *fh;
126
127 mutex_lock(&adap->devnode.fhs_lock);
128 list_for_each_entry(fh, &adap->devnode.fhs, list)
129 cec_queue_event_fh(fh, ev, ts);
130 mutex_unlock(&adap->devnode.fhs_lock);
131}
132
133/*
134 * Queue a new message for this filehandle. If there is no more room
135 * in the queue, then send the LOST_MSGS event instead.
136 */
137static void cec_queue_msg_fh(struct cec_fh *fh, const struct cec_msg *msg)
138{
139 static const struct cec_event ev_lost_msg = {
140 .ts = 0,
141 .event = CEC_EVENT_LOST_MSGS,
142 .flags = 0,
143 {
144 .lost_msgs.lost_msgs = 1,
145 },
146 };
147 struct cec_msg_entry *entry;
148
149 mutex_lock(&fh->lock);
150 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
151 if (!entry)
152 goto lost_msgs;
153
154 entry->msg = *msg;
155 /* Add new msg at the end of the queue */
156 list_add_tail(&entry->list, &fh->msgs);
157
158 /*
159 * if the queue now has more than CEC_MAX_MSG_RX_QUEUE_SZ
160 * messages, drop the oldest one and send a lost message event.
161 */
162 if (fh->queued_msgs == CEC_MAX_MSG_RX_QUEUE_SZ) {
163 list_del(&entry->list);
164 goto lost_msgs;
165 }
166 fh->queued_msgs++;
167 mutex_unlock(&fh->lock);
168 wake_up_interruptible(&fh->wait);
169 return;
170
171lost_msgs:
172 mutex_unlock(&fh->lock);
173 cec_queue_event_fh(fh, &ev_lost_msg, 0);
174}
175
176/*
177 * Queue the message for those filehandles that are in monitor mode.
178 * If valid_la is true (this message is for us or was sent by us),
179 * then pass it on to any monitoring filehandle. If this message
180 * isn't for us or from us, then only give it to filehandles that
181 * are in MONITOR_ALL mode.
182 *
183 * This can only happen if the CEC_CAP_MONITOR_ALL capability is
184 * set and the CEC adapter was placed in 'monitor all' mode.
185 */
186static void cec_queue_msg_monitor(struct cec_adapter *adap,
187 const struct cec_msg *msg,
188 bool valid_la)
189{
190 struct cec_fh *fh;
191 u32 monitor_mode = valid_la ? CEC_MODE_MONITOR :
192 CEC_MODE_MONITOR_ALL;
193
194 mutex_lock(&adap->devnode.fhs_lock);
195 list_for_each_entry(fh, &adap->devnode.fhs, list) {
196 if (fh->mode_follower >= monitor_mode)
197 cec_queue_msg_fh(fh, msg);
198 }
199 mutex_unlock(&adap->devnode.fhs_lock);
200}
201
202/*
203 * Queue the message for follower filehandles.
204 */
205static void cec_queue_msg_followers(struct cec_adapter *adap,
206 const struct cec_msg *msg)
207{
208 struct cec_fh *fh;
209
210 mutex_lock(&adap->devnode.fhs_lock);
211 list_for_each_entry(fh, &adap->devnode.fhs, list) {
212 if (fh->mode_follower == CEC_MODE_FOLLOWER)
213 cec_queue_msg_fh(fh, msg);
214 }
215 mutex_unlock(&adap->devnode.fhs_lock);
216}
217
218/* Notify userspace of an adapter state change. */
219static void cec_post_state_event(struct cec_adapter *adap)
220{
221 struct cec_event ev = {
222 .event = CEC_EVENT_STATE_CHANGE,
223 };
224
225 ev.state_change.phys_addr = adap->phys_addr;
226 ev.state_change.log_addr_mask = adap->log_addrs.log_addr_mask;
227 cec_queue_event(adap, &ev);
228}
229
230/*
231 * A CEC transmit (and a possible wait for reply) completed.
232 * If this was in blocking mode, then complete it, otherwise
233 * queue the message for userspace to dequeue later.
234 *
235 * This function is called with adap->lock held.
236 */
237static void cec_data_completed(struct cec_data *data)
238{
239 /*
240 * Delete this transmit from the filehandle's xfer_list since
241 * we're done with it.
242 *
243 * Note that if the filehandle is closed before this transmit
244 * finished, then the release() function will set data->fh to NULL.
245 * Without that we would be referring to a closed filehandle.
246 */
247 if (data->fh)
248 list_del(&data->xfer_list);
249
250 if (data->blocking) {
251 /*
252 * Someone is blocking so mark the message as completed
253 * and call complete.
254 */
255 data->completed = true;
256 complete(&data->c);
257 } else {
258 /*
259 * No blocking, so just queue the message if needed and
260 * free the memory.
261 */
262 if (data->fh)
263 cec_queue_msg_fh(data->fh, &data->msg);
264 kfree(data);
265 }
266}
267
268/*
269 * A pending CEC transmit needs to be cancelled, either because the CEC
270 * adapter is disabled or the transmit takes an impossibly long time to
271 * finish.
272 *
273 * This function is called with adap->lock held.
274 */
275static void cec_data_cancel(struct cec_data *data)
276{
277 /*
278 * It's either the current transmit, or it is a pending
279 * transmit. Take the appropriate action to clear it.
280 */
281 if (data->adap->transmitting == data) {
282 data->adap->transmitting = NULL;
283 } else {
284 list_del_init(&data->list);
285 if (!(data->msg.tx_status & CEC_TX_STATUS_OK))
286 data->adap->transmit_queue_sz--;
287 }
288
289 /* Mark it as an error */
290 data->msg.tx_ts = ktime_get_ns();
291 data->msg.tx_status = CEC_TX_STATUS_ERROR |
292 CEC_TX_STATUS_MAX_RETRIES;
293 data->attempts = 0;
294 data->msg.tx_error_cnt = 1;
295 /* Queue transmitted message for monitoring purposes */
296 cec_queue_msg_monitor(data->adap, &data->msg, 1);
297
298 cec_data_completed(data);
299}
300
301/*
302 * Main CEC state machine
303 *
304 * Wait until the thread should be stopped, or we are not transmitting and
305 * a new transmit message is queued up, in which case we start transmitting
306 * that message. When the adapter finished transmitting the message it will
307 * call cec_transmit_done().
308 *
309 * If the adapter is disabled, then remove all queued messages instead.
310 *
311 * If the current transmit times out, then cancel that transmit.
312 */
313int cec_thread_func(void *_adap)
314{
315 struct cec_adapter *adap = _adap;
316
317 for (;;) {
318 unsigned int signal_free_time;
319 struct cec_data *data;
320 bool timeout = false;
321 u8 attempts;
322
323 if (adap->transmitting) {
324 int err;
325
326 /*
327 * We are transmitting a message, so add a timeout
328 * to prevent the state machine to get stuck waiting
329 * for this message to finalize and add a check to
330 * see if the adapter is disabled in which case the
331 * transmit should be canceled.
332 */
333 err = wait_event_interruptible_timeout(adap->kthread_waitq,
334 kthread_should_stop() ||
335 (!adap->is_configured && !adap->is_configuring) ||
336 (!adap->transmitting &&
337 !list_empty(&adap->transmit_queue)),
338 msecs_to_jiffies(CEC_XFER_TIMEOUT_MS));
339 timeout = err == 0;
340 } else {
341 /* Otherwise we just wait for something to happen. */
342 wait_event_interruptible(adap->kthread_waitq,
343 kthread_should_stop() ||
344 (!adap->transmitting &&
345 !list_empty(&adap->transmit_queue)));
346 }
347
348 mutex_lock(&adap->lock);
349
350 if ((!adap->is_configured && !adap->is_configuring) ||
351 kthread_should_stop()) {
352 /*
353 * If the adapter is disabled, or we're asked to stop,
354 * then cancel any pending transmits.
355 */
356 while (!list_empty(&adap->transmit_queue)) {
357 data = list_first_entry(&adap->transmit_queue,
358 struct cec_data, list);
359 cec_data_cancel(data);
360 }
361 if (adap->transmitting)
362 cec_data_cancel(adap->transmitting);
363
364 /*
365 * Cancel the pending timeout work. We have to unlock
366 * the mutex when flushing the work since
367 * cec_wait_timeout() will take it. This is OK since
368 * no new entries can be added to wait_queue as long
369 * as adap->transmitting is NULL, which it is due to
370 * the cec_data_cancel() above.
371 */
372 while (!list_empty(&adap->wait_queue)) {
373 data = list_first_entry(&adap->wait_queue,
374 struct cec_data, list);
375
376 if (!cancel_delayed_work(&data->work)) {
377 mutex_unlock(&adap->lock);
378 flush_scheduled_work();
379 mutex_lock(&adap->lock);
380 }
381 cec_data_cancel(data);
382 }
383 goto unlock;
384 }
385
386 if (adap->transmitting && timeout) {
387 /*
388 * If we timeout, then log that. This really shouldn't
389 * happen and is an indication of a faulty CEC adapter
390 * driver, or the CEC bus is in some weird state.
391 */
392 dprintk(0, "message %*ph timed out!\n",
393 adap->transmitting->msg.len,
394 adap->transmitting->msg.msg);
395 /* Just give up on this. */
396 cec_data_cancel(adap->transmitting);
397 goto unlock;
398 }
399
400 /*
401 * If we are still transmitting, or there is nothing new to
402 * transmit, then just continue waiting.
403 */
404 if (adap->transmitting || list_empty(&adap->transmit_queue))
405 goto unlock;
406
407 /* Get a new message to transmit */
408 data = list_first_entry(&adap->transmit_queue,
409 struct cec_data, list);
410 list_del_init(&data->list);
411 adap->transmit_queue_sz--;
412 /* Make this the current transmitting message */
413 adap->transmitting = data;
414
415 /*
416 * Suggested number of attempts as per the CEC 2.0 spec:
417 * 4 attempts is the default, except for 'secondary poll
418 * messages', i.e. poll messages not sent during the adapter
419 * configuration phase when it allocates logical addresses.
420 */
421 if (data->msg.len == 1 && adap->is_configured)
422 attempts = 2;
423 else
424 attempts = 4;
425
426 /* Set the suggested signal free time */
427 if (data->attempts) {
428 /* should be >= 3 data bit periods for a retry */
429 signal_free_time = CEC_SIGNAL_FREE_TIME_RETRY;
430 } else if (data->new_initiator) {
431 /* should be >= 5 data bit periods for new initiator */
432 signal_free_time = CEC_SIGNAL_FREE_TIME_NEW_INITIATOR;
433 } else {
434 /*
435 * should be >= 7 data bit periods for sending another
436 * frame immediately after another.
437 */
438 signal_free_time = CEC_SIGNAL_FREE_TIME_NEXT_XFER;
439 }
440 if (data->attempts == 0)
441 data->attempts = attempts;
442
443 /* Tell the adapter to transmit, cancel on error */
444 if (adap->ops->adap_transmit(adap, data->attempts,
445 signal_free_time, &data->msg))
446 cec_data_cancel(data);
447
448unlock:
449 mutex_unlock(&adap->lock);
450
451 if (kthread_should_stop())
452 break;
453 }
454 return 0;
455}
456
457/*
458 * Called by the CEC adapter if a transmit finished.
459 */
460void cec_transmit_done(struct cec_adapter *adap, u8 status, u8 arb_lost_cnt,
461 u8 nack_cnt, u8 low_drive_cnt, u8 error_cnt)
462{
463 struct cec_data *data;
464 struct cec_msg *msg;
465 u64 ts = ktime_get_ns();
466
467 dprintk(2, "cec_transmit_done %02x\n", status);
468 mutex_lock(&adap->lock);
469 data = adap->transmitting;
470 if (!data) {
471 /*
472 * This can happen if a transmit was issued and the cable is
473 * unplugged while the transmit is ongoing. Ignore this
474 * transmit in that case.
475 */
476 dprintk(1, "cec_transmit_done without an ongoing transmit!\n");
477 goto unlock;
478 }
479
480 msg = &data->msg;
481
482 /* Drivers must fill in the status! */
483 WARN_ON(status == 0);
484 msg->tx_ts = ts;
485 msg->tx_status |= status;
486 msg->tx_arb_lost_cnt += arb_lost_cnt;
487 msg->tx_nack_cnt += nack_cnt;
488 msg->tx_low_drive_cnt += low_drive_cnt;
489 msg->tx_error_cnt += error_cnt;
490
491 /* Mark that we're done with this transmit */
492 adap->transmitting = NULL;
493
494 /*
495 * If there are still retry attempts left and there was an error and
496 * the hardware didn't signal that it retried itself (by setting
497 * CEC_TX_STATUS_MAX_RETRIES), then we will retry ourselves.
498 */
499 if (data->attempts > 1 &&
500 !(status & (CEC_TX_STATUS_MAX_RETRIES | CEC_TX_STATUS_OK))) {
501 /* Retry this message */
502 data->attempts--;
503 /* Add the message in front of the transmit queue */
504 list_add(&data->list, &adap->transmit_queue);
505 adap->transmit_queue_sz++;
506 goto wake_thread;
507 }
508
509 data->attempts = 0;
510
511 /* Always set CEC_TX_STATUS_MAX_RETRIES on error */
512 if (!(status & CEC_TX_STATUS_OK))
513 msg->tx_status |= CEC_TX_STATUS_MAX_RETRIES;
514
515 /* Queue transmitted message for monitoring purposes */
516 cec_queue_msg_monitor(adap, msg, 1);
517
518 if ((status & CEC_TX_STATUS_OK) && adap->is_configured &&
519 msg->timeout) {
520 /*
521 * Queue the message into the wait queue if we want to wait
522 * for a reply.
523 */
524 list_add_tail(&data->list, &adap->wait_queue);
525 schedule_delayed_work(&data->work,
526 msecs_to_jiffies(msg->timeout));
527 } else {
528 /* Otherwise we're done */
529 cec_data_completed(data);
530 }
531
532wake_thread:
533 /*
534 * Wake up the main thread to see if another message is ready
535 * for transmitting or to retry the current message.
536 */
537 wake_up_interruptible(&adap->kthread_waitq);
538unlock:
539 mutex_unlock(&adap->lock);
540}
541EXPORT_SYMBOL_GPL(cec_transmit_done);
542
543/*
544 * Called when waiting for a reply times out.
545 */
546static void cec_wait_timeout(struct work_struct *work)
547{
548 struct cec_data *data = container_of(work, struct cec_data, work.work);
549 struct cec_adapter *adap = data->adap;
550
551 mutex_lock(&adap->lock);
552 /*
553 * Sanity check in case the timeout and the arrival of the message
554 * happened at the same time.
555 */
556 if (list_empty(&data->list))
557 goto unlock;
558
559 /* Mark the message as timed out */
560 list_del_init(&data->list);
561 data->msg.rx_ts = ktime_get_ns();
562 data->msg.rx_status = CEC_RX_STATUS_TIMEOUT;
563 cec_data_completed(data);
564unlock:
565 mutex_unlock(&adap->lock);
566}
567
568/*
569 * Transmit a message. The fh argument may be NULL if the transmit is not
570 * associated with a specific filehandle.
571 *
572 * This function is called with adap->lock held.
573 */
574int cec_transmit_msg_fh(struct cec_adapter *adap, struct cec_msg *msg,
575 struct cec_fh *fh, bool block)
576{
577 struct cec_data *data;
578 u8 last_initiator = 0xff;
579 unsigned int timeout;
580 int res = 0;
581
582 msg->rx_ts = 0;
583 msg->tx_ts = 0;
584 msg->rx_status = 0;
585 msg->tx_status = 0;
586 msg->tx_arb_lost_cnt = 0;
587 msg->tx_nack_cnt = 0;
588 msg->tx_low_drive_cnt = 0;
589 msg->tx_error_cnt = 0;
590 msg->flags = 0;
591 msg->sequence = ++adap->sequence;
592 if (!msg->sequence)
593 msg->sequence = ++adap->sequence;
594
595 if (msg->reply && msg->timeout == 0) {
596 /* Make sure the timeout isn't 0. */
597 msg->timeout = 1000;
598 }
599
600 /* Sanity checks */
601 if (msg->len == 0 || msg->len > CEC_MAX_MSG_SIZE) {
602 dprintk(1, "cec_transmit_msg: invalid length %d\n", msg->len);
603 return -EINVAL;
604 }
605 if (msg->timeout && msg->len == 1) {
606 dprintk(1, "cec_transmit_msg: can't reply for poll msg\n");
607 return -EINVAL;
608 }
609 memset(msg->msg + msg->len, 0, sizeof(msg->msg) - msg->len);
610 if (msg->len == 1) {
611 if (cec_msg_initiator(msg) != 0xf ||
612 cec_msg_destination(msg) == 0xf) {
613 dprintk(1, "cec_transmit_msg: invalid poll message\n");
614 return -EINVAL;
615 }
616 if (cec_has_log_addr(adap, cec_msg_destination(msg))) {
617 /*
618 * If the destination is a logical address our adapter
619 * has already claimed, then just NACK this.
620 * It depends on the hardware what it will do with a
621 * POLL to itself (some OK this), so it is just as
622 * easy to handle it here so the behavior will be
623 * consistent.
624 */
625 msg->tx_ts = ktime_get_ns();
626 msg->tx_status = CEC_TX_STATUS_NACK |
627 CEC_TX_STATUS_MAX_RETRIES;
628 msg->tx_nack_cnt = 1;
629 return 0;
630 }
631 }
632 if (msg->len > 1 && !cec_msg_is_broadcast(msg) &&
633 cec_has_log_addr(adap, cec_msg_destination(msg))) {
634 dprintk(1, "cec_transmit_msg: destination is the adapter itself\n");
635 return -EINVAL;
636 }
637 if (cec_msg_initiator(msg) != 0xf &&
638 !cec_has_log_addr(adap, cec_msg_initiator(msg))) {
639 dprintk(1, "cec_transmit_msg: initiator has unknown logical address %d\n",
640 cec_msg_initiator(msg));
641 return -EINVAL;
642 }
643 if (!adap->is_configured && !adap->is_configuring)
644 return -ENONET;
645
646 if (adap->transmit_queue_sz >= CEC_MAX_MSG_TX_QUEUE_SZ)
647 return -EBUSY;
648
649 data = kzalloc(sizeof(*data), GFP_KERNEL);
650 if (!data)
651 return -ENOMEM;
652
653 if (msg->len > 1 && msg->msg[1] == CEC_MSG_CDC_MESSAGE) {
654 msg->msg[2] = adap->phys_addr >> 8;
655 msg->msg[3] = adap->phys_addr & 0xff;
656 }
657
658 if (msg->timeout)
659 dprintk(2, "cec_transmit_msg: %*ph (wait for 0x%02x%s)\n",
660 msg->len, msg->msg, msg->reply, !block ? ", nb" : "");
661 else
662 dprintk(2, "cec_transmit_msg: %*ph%s\n",
663 msg->len, msg->msg, !block ? " (nb)" : "");
664
665 data->msg = *msg;
666 data->fh = fh;
667 data->adap = adap;
668 data->blocking = block;
669
670 /*
671 * Determine if this message follows a message from the same
672 * initiator. Needed to determine the free signal time later on.
673 */
674 if (msg->len > 1) {
675 if (!(list_empty(&adap->transmit_queue))) {
676 const struct cec_data *last;
677
678 last = list_last_entry(&adap->transmit_queue,
679 const struct cec_data, list);
680 last_initiator = cec_msg_initiator(&last->msg);
681 } else if (adap->transmitting) {
682 last_initiator =
683 cec_msg_initiator(&adap->transmitting->msg);
684 }
685 }
686 data->new_initiator = last_initiator != cec_msg_initiator(msg);
687 init_completion(&data->c);
688 INIT_DELAYED_WORK(&data->work, cec_wait_timeout);
689
690 if (fh)
691 list_add_tail(&data->xfer_list, &fh->xfer_list);
692 list_add_tail(&data->list, &adap->transmit_queue);
693 adap->transmit_queue_sz++;
694 if (!adap->transmitting)
695 wake_up_interruptible(&adap->kthread_waitq);
696
697 /* All done if we don't need to block waiting for completion */
698 if (!block)
699 return 0;
700
701 /*
702 * If we don't get a completion before this time something is really
703 * wrong and we time out.
704 */
705 timeout = CEC_XFER_TIMEOUT_MS;
706 /* Add the requested timeout if we have to wait for a reply as well */
707 if (msg->timeout)
708 timeout += msg->timeout;
709
710 /*
711 * Release the lock and wait, retake the lock afterwards.
712 */
713 mutex_unlock(&adap->lock);
714 res = wait_for_completion_killable_timeout(&data->c,
715 msecs_to_jiffies(timeout));
716 mutex_lock(&adap->lock);
717
718 if (data->completed) {
719 /* The transmit completed (possibly with an error) */
720 *msg = data->msg;
721 kfree(data);
722 return 0;
723 }
724 /*
725 * The wait for completion timed out or was interrupted, so mark this
726 * as non-blocking and disconnect from the filehandle since it is
727 * still 'in flight'. When it finally completes it will just drop the
728 * result silently.
729 */
730 data->blocking = false;
731 if (data->fh)
732 list_del(&data->xfer_list);
733 data->fh = NULL;
734
735 if (res == 0) { /* timed out */
736 /* Check if the reply or the transmit failed */
737 if (msg->timeout && (msg->tx_status & CEC_TX_STATUS_OK))
738 msg->rx_status = CEC_RX_STATUS_TIMEOUT;
739 else
740 msg->tx_status = CEC_TX_STATUS_MAX_RETRIES;
741 }
742 return res > 0 ? 0 : res;
743}
744
745/* Helper function to be used by drivers and this framework. */
746int cec_transmit_msg(struct cec_adapter *adap, struct cec_msg *msg,
747 bool block)
748{
749 int ret;
750
751 mutex_lock(&adap->lock);
752 ret = cec_transmit_msg_fh(adap, msg, NULL, block);
753 mutex_unlock(&adap->lock);
754 return ret;
755}
756EXPORT_SYMBOL_GPL(cec_transmit_msg);
757
758/*
759 * I don't like forward references but without this the low-level
760 * cec_received_msg() function would come after a bunch of high-level
761 * CEC protocol handling functions. That was very confusing.
762 */
763static int cec_receive_notify(struct cec_adapter *adap, struct cec_msg *msg,
764 bool is_reply);
765
766/* Called by the CEC adapter if a message is received */
767void cec_received_msg(struct cec_adapter *adap, struct cec_msg *msg)
768{
769 struct cec_data *data;
770 u8 msg_init = cec_msg_initiator(msg);
771 u8 msg_dest = cec_msg_destination(msg);
772 bool is_reply = false;
773 bool valid_la = true;
774
775 if (WARN_ON(!msg->len || msg->len > CEC_MAX_MSG_SIZE))
776 return;
777
778 msg->rx_ts = ktime_get_ns();
779 msg->rx_status = CEC_RX_STATUS_OK;
780 msg->sequence = msg->reply = msg->timeout = 0;
781 msg->tx_status = 0;
782 msg->tx_ts = 0;
783 msg->flags = 0;
784 memset(msg->msg + msg->len, 0, sizeof(msg->msg) - msg->len);
785
786 mutex_lock(&adap->lock);
787 dprintk(2, "cec_received_msg: %*ph\n", msg->len, msg->msg);
788
789 /* Check if this message was for us (directed or broadcast). */
790 if (!cec_msg_is_broadcast(msg))
791 valid_la = cec_has_log_addr(adap, msg_dest);
792
793 /* It's a valid message and not a poll or CDC message */
794 if (valid_la && msg->len > 1 && msg->msg[1] != CEC_MSG_CDC_MESSAGE) {
795 u8 cmd = msg->msg[1];
796 bool abort = cmd == CEC_MSG_FEATURE_ABORT;
797
798 /* The aborted command is in msg[2] */
799 if (abort)
800 cmd = msg->msg[2];
801
802 /*
803 * Walk over all transmitted messages that are waiting for a
804 * reply.
805 */
806 list_for_each_entry(data, &adap->wait_queue, list) {
807 struct cec_msg *dst = &data->msg;
808
809 /* Does the command match? */
810 if ((abort && cmd != dst->msg[1]) ||
811 (!abort && cmd != dst->reply))
812 continue;
813
814 /* Does the addressing match? */
815 if (msg_init != cec_msg_destination(dst) &&
816 !cec_msg_is_broadcast(dst))
817 continue;
818
819 /* We got a reply */
820 memcpy(dst->msg, msg->msg, msg->len);
821 dst->len = msg->len;
822 dst->rx_ts = msg->rx_ts;
823 dst->rx_status = msg->rx_status;
824 if (abort)
825 dst->rx_status |= CEC_RX_STATUS_FEATURE_ABORT;
826 /* Remove it from the wait_queue */
827 list_del_init(&data->list);
828
829 /* Cancel the pending timeout work */
830 if (!cancel_delayed_work(&data->work)) {
831 mutex_unlock(&adap->lock);
832 flush_scheduled_work();
833 mutex_lock(&adap->lock);
834 }
835 /*
836 * Mark this as a reply, provided someone is still
837 * waiting for the answer.
838 */
839 if (data->fh)
840 is_reply = true;
841 cec_data_completed(data);
842 break;
843 }
844 }
845 mutex_unlock(&adap->lock);
846
847 /* Pass the message on to any monitoring filehandles */
848 cec_queue_msg_monitor(adap, msg, valid_la);
849
850 /* We're done if it is not for us or a poll message */
851 if (!valid_la || msg->len <= 1)
852 return;
853
854 /*
855 * Process the message on the protocol level. If is_reply is true,
856 * then cec_receive_notify() won't pass on the reply to the listener(s)
857 * since that was already done by cec_data_completed() above.
858 */
859 cec_receive_notify(adap, msg, is_reply);
860}
861EXPORT_SYMBOL_GPL(cec_received_msg);
862
863/* Logical Address Handling */
864
865/*
866 * Attempt to claim a specific logical address.
867 *
868 * This function is called with adap->lock held.
869 */
870static int cec_config_log_addr(struct cec_adapter *adap,
871 unsigned int idx,
872 unsigned int log_addr)
873{
874 struct cec_log_addrs *las = &adap->log_addrs;
875 struct cec_msg msg = { };
876 int err;
877
878 if (cec_has_log_addr(adap, log_addr))
879 return 0;
880
881 /* Send poll message */
882 msg.len = 1;
883 msg.msg[0] = 0xf0 | log_addr;
884 err = cec_transmit_msg_fh(adap, &msg, NULL, true);
885
886 /*
887 * While trying to poll the physical address was reset
888 * and the adapter was unconfigured, so bail out.
889 */
890 if (!adap->is_configuring)
891 return -EINTR;
892
893 if (err)
894 return err;
895
896 if (msg.tx_status & CEC_TX_STATUS_OK)
897 return 0;
898
899 /*
900 * Message not acknowledged, so this logical
901 * address is free to use.
902 */
903 err = adap->ops->adap_log_addr(adap, log_addr);
904 if (err)
905 return err;
906
907 las->log_addr[idx] = log_addr;
908 las->log_addr_mask |= 1 << log_addr;
909 adap->phys_addrs[log_addr] = adap->phys_addr;
910
911 dprintk(2, "claimed addr %d (%d)\n", log_addr,
912 las->primary_device_type[idx]);
913 return 1;
914}
915
916/*
917 * Unconfigure the adapter: clear all logical addresses and send
918 * the state changed event.
919 *
920 * This function is called with adap->lock held.
921 */
922static void cec_adap_unconfigure(struct cec_adapter *adap)
923{
924 WARN_ON(adap->ops->adap_log_addr(adap, CEC_LOG_ADDR_INVALID));
925 adap->log_addrs.log_addr_mask = 0;
926 adap->is_configuring = false;
927 adap->is_configured = false;
928 memset(adap->phys_addrs, 0xff, sizeof(adap->phys_addrs));
929 wake_up_interruptible(&adap->kthread_waitq);
930 cec_post_state_event(adap);
931}
932
933/*
934 * Attempt to claim the required logical addresses.
935 */
936static int cec_config_thread_func(void *arg)
937{
938 /* The various LAs for each type of device */
939 static const u8 tv_log_addrs[] = {
940 CEC_LOG_ADDR_TV, CEC_LOG_ADDR_SPECIFIC,
941 CEC_LOG_ADDR_INVALID
942 };
943 static const u8 record_log_addrs[] = {
944 CEC_LOG_ADDR_RECORD_1, CEC_LOG_ADDR_RECORD_2,
945 CEC_LOG_ADDR_RECORD_3,
946 CEC_LOG_ADDR_BACKUP_1, CEC_LOG_ADDR_BACKUP_2,
947 CEC_LOG_ADDR_INVALID
948 };
949 static const u8 tuner_log_addrs[] = {
950 CEC_LOG_ADDR_TUNER_1, CEC_LOG_ADDR_TUNER_2,
951 CEC_LOG_ADDR_TUNER_3, CEC_LOG_ADDR_TUNER_4,
952 CEC_LOG_ADDR_BACKUP_1, CEC_LOG_ADDR_BACKUP_2,
953 CEC_LOG_ADDR_INVALID
954 };
955 static const u8 playback_log_addrs[] = {
956 CEC_LOG_ADDR_PLAYBACK_1, CEC_LOG_ADDR_PLAYBACK_2,
957 CEC_LOG_ADDR_PLAYBACK_3,
958 CEC_LOG_ADDR_BACKUP_1, CEC_LOG_ADDR_BACKUP_2,
959 CEC_LOG_ADDR_INVALID
960 };
961 static const u8 audiosystem_log_addrs[] = {
962 CEC_LOG_ADDR_AUDIOSYSTEM,
963 CEC_LOG_ADDR_INVALID
964 };
965 static const u8 specific_use_log_addrs[] = {
966 CEC_LOG_ADDR_SPECIFIC,
967 CEC_LOG_ADDR_BACKUP_1, CEC_LOG_ADDR_BACKUP_2,
968 CEC_LOG_ADDR_INVALID
969 };
970 static const u8 *type2addrs[6] = {
971 [CEC_LOG_ADDR_TYPE_TV] = tv_log_addrs,
972 [CEC_LOG_ADDR_TYPE_RECORD] = record_log_addrs,
973 [CEC_LOG_ADDR_TYPE_TUNER] = tuner_log_addrs,
974 [CEC_LOG_ADDR_TYPE_PLAYBACK] = playback_log_addrs,
975 [CEC_LOG_ADDR_TYPE_AUDIOSYSTEM] = audiosystem_log_addrs,
976 [CEC_LOG_ADDR_TYPE_SPECIFIC] = specific_use_log_addrs,
977 };
978 static const u16 type2mask[] = {
979 [CEC_LOG_ADDR_TYPE_TV] = CEC_LOG_ADDR_MASK_TV,
980 [CEC_LOG_ADDR_TYPE_RECORD] = CEC_LOG_ADDR_MASK_RECORD,
981 [CEC_LOG_ADDR_TYPE_TUNER] = CEC_LOG_ADDR_MASK_TUNER,
982 [CEC_LOG_ADDR_TYPE_PLAYBACK] = CEC_LOG_ADDR_MASK_PLAYBACK,
983 [CEC_LOG_ADDR_TYPE_AUDIOSYSTEM] = CEC_LOG_ADDR_MASK_AUDIOSYSTEM,
984 [CEC_LOG_ADDR_TYPE_SPECIFIC] = CEC_LOG_ADDR_MASK_SPECIFIC,
985 };
986 struct cec_adapter *adap = arg;
987 struct cec_log_addrs *las = &adap->log_addrs;
988 int err;
989 int i, j;
990
991 mutex_lock(&adap->lock);
992 dprintk(1, "physical address: %x.%x.%x.%x, claim %d logical addresses\n",
993 cec_phys_addr_exp(adap->phys_addr), las->num_log_addrs);
994 las->log_addr_mask = 0;
995
996 if (las->log_addr_type[0] == CEC_LOG_ADDR_TYPE_UNREGISTERED)
997 goto configured;
998
999 for (i = 0; i < las->num_log_addrs; i++) {
1000 unsigned int type = las->log_addr_type[i];
1001 const u8 *la_list;
1002 u8 last_la;
1003
1004 /*
1005 * The TV functionality can only map to physical address 0.
1006 * For any other address, try the Specific functionality
1007 * instead as per the spec.
1008 */
1009 if (adap->phys_addr && type == CEC_LOG_ADDR_TYPE_TV)
1010 type = CEC_LOG_ADDR_TYPE_SPECIFIC;
1011
1012 la_list = type2addrs[type];
1013 last_la = las->log_addr[i];
1014 las->log_addr[i] = CEC_LOG_ADDR_INVALID;
1015 if (last_la == CEC_LOG_ADDR_INVALID ||
1016 last_la == CEC_LOG_ADDR_UNREGISTERED ||
1017 !(last_la & type2mask[type]))
1018 last_la = la_list[0];
1019
1020 err = cec_config_log_addr(adap, i, last_la);
1021 if (err > 0) /* Reused last LA */
1022 continue;
1023
1024 if (err < 0)
1025 goto unconfigure;
1026
1027 for (j = 0; la_list[j] != CEC_LOG_ADDR_INVALID; j++) {
1028 /* Tried this one already, skip it */
1029 if (la_list[j] == last_la)
1030 continue;
1031 /* The backup addresses are CEC 2.0 specific */
1032 if ((la_list[j] == CEC_LOG_ADDR_BACKUP_1 ||
1033 la_list[j] == CEC_LOG_ADDR_BACKUP_2) &&
1034 las->cec_version < CEC_OP_CEC_VERSION_2_0)
1035 continue;
1036
1037 err = cec_config_log_addr(adap, i, la_list[j]);
1038 if (err == 0) /* LA is in use */
1039 continue;
1040 if (err < 0)
1041 goto unconfigure;
1042 /* Done, claimed an LA */
1043 break;
1044 }
1045
1046 if (la_list[j] == CEC_LOG_ADDR_INVALID)
1047 dprintk(1, "could not claim LA %d\n", i);
1048 }
1049
1050configured:
1051 if (adap->log_addrs.log_addr_mask == 0) {
1052 /* Fall back to unregistered */
1053 las->log_addr[0] = CEC_LOG_ADDR_UNREGISTERED;
1054 las->log_addr_mask = 1 << las->log_addr[0];
1055 }
1056 adap->is_configured = true;
1057 adap->is_configuring = false;
1058 cec_post_state_event(adap);
1059 mutex_unlock(&adap->lock);
1060
1061 for (i = 0; i < las->num_log_addrs; i++) {
1062 if (las->log_addr[i] == CEC_LOG_ADDR_INVALID)
1063 continue;
1064
1065 /*
1066 * Report Features must come first according
1067 * to CEC 2.0
1068 */
1069 if (las->log_addr[i] != CEC_LOG_ADDR_UNREGISTERED)
1070 cec_report_features(adap, i);
1071 cec_report_phys_addr(adap, i);
1072 }
1073 mutex_lock(&adap->lock);
1074 adap->kthread_config = NULL;
1075 mutex_unlock(&adap->lock);
1076 complete(&adap->config_completion);
1077 return 0;
1078
1079unconfigure:
1080 for (i = 0; i < las->num_log_addrs; i++)
1081 las->log_addr[i] = CEC_LOG_ADDR_INVALID;
1082 cec_adap_unconfigure(adap);
1083 adap->kthread_config = NULL;
1084 mutex_unlock(&adap->lock);
1085 complete(&adap->config_completion);
1086 return 0;
1087}
1088
1089/*
1090 * Called from either __cec_s_phys_addr or __cec_s_log_addrs to claim the
1091 * logical addresses.
1092 *
1093 * This function is called with adap->lock held.
1094 */
1095static void cec_claim_log_addrs(struct cec_adapter *adap, bool block)
1096{
1097 if (WARN_ON(adap->is_configuring || adap->is_configured))
1098 return;
1099
1100 init_completion(&adap->config_completion);
1101
1102 /* Ready to kick off the thread */
1103 adap->is_configuring = true;
1104 adap->kthread_config = kthread_run(cec_config_thread_func, adap,
1105 "ceccfg-%s", adap->name);
1106 if (IS_ERR(adap->kthread_config)) {
1107 adap->kthread_config = NULL;
1108 } else if (block) {
1109 mutex_unlock(&adap->lock);
1110 wait_for_completion(&adap->config_completion);
1111 mutex_lock(&adap->lock);
1112 }
1113}
1114
1115/* Set a new physical address and send an event notifying userspace of this.
1116 *
1117 * This function is called with adap->lock held.
1118 */
1119void __cec_s_phys_addr(struct cec_adapter *adap, u16 phys_addr, bool block)
1120{
1121 if (phys_addr == adap->phys_addr || adap->devnode.unregistered)
1122 return;
1123
1124 if (phys_addr == CEC_PHYS_ADDR_INVALID ||
1125 adap->phys_addr != CEC_PHYS_ADDR_INVALID) {
1126 adap->phys_addr = CEC_PHYS_ADDR_INVALID;
1127 cec_post_state_event(adap);
1128 cec_adap_unconfigure(adap);
1129 /* Disabling monitor all mode should always succeed */
1130 if (adap->monitor_all_cnt)
1131 WARN_ON(call_op(adap, adap_monitor_all_enable, false));
1132 WARN_ON(adap->ops->adap_enable(adap, false));
1133 if (phys_addr == CEC_PHYS_ADDR_INVALID)
1134 return;
1135 }
1136
1137 if (adap->ops->adap_enable(adap, true))
1138 return;
1139
1140 if (adap->monitor_all_cnt &&
1141 call_op(adap, adap_monitor_all_enable, true)) {
1142 WARN_ON(adap->ops->adap_enable(adap, false));
1143 return;
1144 }
1145 adap->phys_addr = phys_addr;
1146 cec_post_state_event(adap);
1147 if (adap->log_addrs.num_log_addrs)
1148 cec_claim_log_addrs(adap, block);
1149}
1150
1151void cec_s_phys_addr(struct cec_adapter *adap, u16 phys_addr, bool block)
1152{
1153 if (IS_ERR_OR_NULL(adap))
1154 return;
1155
1156 if (WARN_ON(adap->capabilities & CEC_CAP_PHYS_ADDR))
1157 return;
1158 mutex_lock(&adap->lock);
1159 __cec_s_phys_addr(adap, phys_addr, block);
1160 mutex_unlock(&adap->lock);
1161}
1162EXPORT_SYMBOL_GPL(cec_s_phys_addr);
1163
1164/*
1165 * Called from either the ioctl or a driver to set the logical addresses.
1166 *
1167 * This function is called with adap->lock held.
1168 */
1169int __cec_s_log_addrs(struct cec_adapter *adap,
1170 struct cec_log_addrs *log_addrs, bool block)
1171{
1172 u16 type_mask = 0;
1173 int i;
1174
1175 if (adap->devnode.unregistered)
1176 return -ENODEV;
1177
1178 if (!log_addrs || log_addrs->num_log_addrs == 0) {
1179 adap->log_addrs.num_log_addrs = 0;
1180 cec_adap_unconfigure(adap);
1181 return 0;
1182 }
1183
1184 /* Ensure the osd name is 0-terminated */
1185 log_addrs->osd_name[sizeof(log_addrs->osd_name) - 1] = '\0';
1186
1187 /* Sanity checks */
1188 if (log_addrs->num_log_addrs > adap->available_log_addrs) {
1189 dprintk(1, "num_log_addrs > %d\n", adap->available_log_addrs);
1190 return -EINVAL;
1191 }
1192
1193 /*
1194 * Vendor ID is a 24 bit number, so check if the value is
1195 * within the correct range.
1196 */
1197 if (log_addrs->vendor_id != CEC_VENDOR_ID_NONE &&
1198 (log_addrs->vendor_id & 0xff000000) != 0)
1199 return -EINVAL;
1200
1201 if (log_addrs->cec_version != CEC_OP_CEC_VERSION_1_4 &&
1202 log_addrs->cec_version != CEC_OP_CEC_VERSION_2_0)
1203 return -EINVAL;
1204
1205 if (log_addrs->num_log_addrs > 1)
1206 for (i = 0; i < log_addrs->num_log_addrs; i++)
1207 if (log_addrs->log_addr_type[i] ==
1208 CEC_LOG_ADDR_TYPE_UNREGISTERED) {
1209 dprintk(1, "num_log_addrs > 1 can't be combined with unregistered LA\n");
1210 return -EINVAL;
1211 }
1212
1213 for (i = 0; i < log_addrs->num_log_addrs; i++) {
1214 const u8 feature_sz = ARRAY_SIZE(log_addrs->features[0]);
1215 u8 *features = log_addrs->features[i];
1216 bool op_is_dev_features = false;
1217
1218 log_addrs->log_addr[i] = CEC_LOG_ADDR_INVALID;
1219 if (type_mask & (1 << log_addrs->log_addr_type[i])) {
1220 dprintk(1, "duplicate logical address type\n");
1221 return -EINVAL;
1222 }
1223 type_mask |= 1 << log_addrs->log_addr_type[i];
1224 if ((type_mask & (1 << CEC_LOG_ADDR_TYPE_RECORD)) &&
1225 (type_mask & (1 << CEC_LOG_ADDR_TYPE_PLAYBACK))) {
1226 /* Record already contains the playback functionality */
1227 dprintk(1, "invalid record + playback combination\n");
1228 return -EINVAL;
1229 }
1230 if (log_addrs->primary_device_type[i] >
1231 CEC_OP_PRIM_DEVTYPE_PROCESSOR) {
1232 dprintk(1, "unknown primary device type\n");
1233 return -EINVAL;
1234 }
1235 if (log_addrs->primary_device_type[i] == 2) {
1236 dprintk(1, "invalid primary device type\n");
1237 return -EINVAL;
1238 }
1239 if (log_addrs->log_addr_type[i] > CEC_LOG_ADDR_TYPE_UNREGISTERED) {
1240 dprintk(1, "unknown logical address type\n");
1241 return -EINVAL;
1242 }
1243 for (i = 0; i < feature_sz; i++) {
1244 if ((features[i] & 0x80) == 0) {
1245 if (op_is_dev_features)
1246 break;
1247 op_is_dev_features = true;
1248 }
1249 }
1250 if (!op_is_dev_features || i == feature_sz) {
1251 dprintk(1, "malformed features\n");
1252 return -EINVAL;
1253 }
1254 /* Zero unused part of the feature array */
1255 memset(features + i, 0, feature_sz - i);
1256 }
1257
1258 if (log_addrs->cec_version >= CEC_OP_CEC_VERSION_2_0) {
1259 if (log_addrs->num_log_addrs > 2) {
1260 dprintk(1, "CEC 2.0 allows no more than 2 logical addresses\n");
1261 return -EINVAL;
1262 }
1263 if (log_addrs->num_log_addrs == 2) {
1264 if (!(type_mask & ((1 << CEC_LOG_ADDR_TYPE_AUDIOSYSTEM) |
1265 (1 << CEC_LOG_ADDR_TYPE_TV)))) {
1266 dprintk(1, "Two LAs is only allowed for audiosystem and TV\n");
1267 return -EINVAL;
1268 }
1269 if (!(type_mask & ((1 << CEC_LOG_ADDR_TYPE_PLAYBACK) |
1270 (1 << CEC_LOG_ADDR_TYPE_RECORD)))) {
1271 dprintk(1, "An audiosystem/TV can only be combined with record or playback\n");
1272 return -EINVAL;
1273 }
1274 }
1275 }
1276
1277 /* Zero unused LAs */
1278 for (i = log_addrs->num_log_addrs; i < CEC_MAX_LOG_ADDRS; i++) {
1279 log_addrs->primary_device_type[i] = 0;
1280 log_addrs->log_addr_type[i] = 0;
1281 log_addrs->all_device_types[i] = 0;
1282 memset(log_addrs->features[i], 0,
1283 sizeof(log_addrs->features[i]));
1284 }
1285
1286 log_addrs->log_addr_mask = adap->log_addrs.log_addr_mask;
1287 adap->log_addrs = *log_addrs;
1288 if (adap->phys_addr != CEC_PHYS_ADDR_INVALID)
1289 cec_claim_log_addrs(adap, block);
1290 return 0;
1291}
1292
1293int cec_s_log_addrs(struct cec_adapter *adap,
1294 struct cec_log_addrs *log_addrs, bool block)
1295{
1296 int err;
1297
1298 if (WARN_ON(adap->capabilities & CEC_CAP_LOG_ADDRS))
1299 return -EINVAL;
1300 mutex_lock(&adap->lock);
1301 err = __cec_s_log_addrs(adap, log_addrs, block);
1302 mutex_unlock(&adap->lock);
1303 return err;
1304}
1305EXPORT_SYMBOL_GPL(cec_s_log_addrs);
1306
1307/* High-level core CEC message handling */
1308
1309/* Transmit the Report Features message */
1310static int cec_report_features(struct cec_adapter *adap, unsigned int la_idx)
1311{
1312 struct cec_msg msg = { };
1313 const struct cec_log_addrs *las = &adap->log_addrs;
1314 const u8 *features = las->features[la_idx];
1315 bool op_is_dev_features = false;
1316 unsigned int idx;
1317
1318 /* This is 2.0 and up only */
1319 if (adap->log_addrs.cec_version < CEC_OP_CEC_VERSION_2_0)
1320 return 0;
1321
1322 /* Report Features */
1323 msg.msg[0] = (las->log_addr[la_idx] << 4) | 0x0f;
1324 msg.len = 4;
1325 msg.msg[1] = CEC_MSG_REPORT_FEATURES;
1326 msg.msg[2] = adap->log_addrs.cec_version;
1327 msg.msg[3] = las->all_device_types[la_idx];
1328
1329 /* Write RC Profiles first, then Device Features */
1330 for (idx = 0; idx < ARRAY_SIZE(las->features[0]); idx++) {
1331 msg.msg[msg.len++] = features[idx];
1332 if ((features[idx] & CEC_OP_FEAT_EXT) == 0) {
1333 if (op_is_dev_features)
1334 break;
1335 op_is_dev_features = true;
1336 }
1337 }
1338 return cec_transmit_msg(adap, &msg, false);
1339}
1340
1341/* Transmit the Report Physical Address message */
1342static int cec_report_phys_addr(struct cec_adapter *adap, unsigned int la_idx)
1343{
1344 const struct cec_log_addrs *las = &adap->log_addrs;
1345 struct cec_msg msg = { };
1346
1347 /* Report Physical Address */
1348 msg.msg[0] = (las->log_addr[la_idx] << 4) | 0x0f;
1349 cec_msg_report_physical_addr(&msg, adap->phys_addr,
1350 las->primary_device_type[la_idx]);
1351 dprintk(2, "config: la %d pa %x.%x.%x.%x\n",
1352 las->log_addr[la_idx],
1353 cec_phys_addr_exp(adap->phys_addr));
1354 return cec_transmit_msg(adap, &msg, false);
1355}
1356
1357/* Transmit the Feature Abort message */
1358static int cec_feature_abort_reason(struct cec_adapter *adap,
1359 struct cec_msg *msg, u8 reason)
1360{
1361 struct cec_msg tx_msg = { };
1362
1363 /*
1364 * Don't reply with CEC_MSG_FEATURE_ABORT to a CEC_MSG_FEATURE_ABORT
1365 * message!
1366 */
1367 if (msg->msg[1] == CEC_MSG_FEATURE_ABORT)
1368 return 0;
1369 cec_msg_set_reply_to(&tx_msg, msg);
1370 cec_msg_feature_abort(&tx_msg, msg->msg[1], reason);
1371 return cec_transmit_msg(adap, &tx_msg, false);
1372}
1373
1374static int cec_feature_abort(struct cec_adapter *adap, struct cec_msg *msg)
1375{
1376 return cec_feature_abort_reason(adap, msg,
1377 CEC_OP_ABORT_UNRECOGNIZED_OP);
1378}
1379
1380static int cec_feature_refused(struct cec_adapter *adap, struct cec_msg *msg)
1381{
1382 return cec_feature_abort_reason(adap, msg,
1383 CEC_OP_ABORT_REFUSED);
1384}
1385
1386/*
1387 * Called when a CEC message is received. This function will do any
1388 * necessary core processing. The is_reply bool is true if this message
1389 * is a reply to an earlier transmit.
1390 *
1391 * The message is either a broadcast message or a valid directed message.
1392 */
1393static int cec_receive_notify(struct cec_adapter *adap, struct cec_msg *msg,
1394 bool is_reply)
1395{
1396 bool is_broadcast = cec_msg_is_broadcast(msg);
1397 u8 dest_laddr = cec_msg_destination(msg);
1398 u8 init_laddr = cec_msg_initiator(msg);
1399 u8 devtype = cec_log_addr2dev(adap, dest_laddr);
1400 int la_idx = cec_log_addr2idx(adap, dest_laddr);
1401 bool is_directed = la_idx >= 0;
1402 bool from_unregistered = init_laddr == 0xf;
1403 struct cec_msg tx_cec_msg = { };
1404
1405 dprintk(1, "cec_receive_notify: %*ph\n", msg->len, msg->msg);
1406
1407 if (adap->ops->received) {
1408 /* Allow drivers to process the message first */
1409 if (adap->ops->received(adap, msg) != -ENOMSG)
1410 return 0;
1411 }
1412
1413 /*
1414 * REPORT_PHYSICAL_ADDR, CEC_MSG_USER_CONTROL_PRESSED and
1415 * CEC_MSG_USER_CONTROL_RELEASED messages always have to be
1416 * handled by the CEC core, even if the passthrough mode is on.
1417 * The others are just ignored if passthrough mode is on.
1418 */
1419 switch (msg->msg[1]) {
1420 case CEC_MSG_GET_CEC_VERSION:
1421 case CEC_MSG_GIVE_DEVICE_VENDOR_ID:
1422 case CEC_MSG_ABORT:
1423 case CEC_MSG_GIVE_DEVICE_POWER_STATUS:
1424 case CEC_MSG_GIVE_PHYSICAL_ADDR:
1425 case CEC_MSG_GIVE_OSD_NAME:
1426 case CEC_MSG_GIVE_FEATURES:
1427 /*
1428 * Skip processing these messages if the passthrough mode
1429 * is on.
1430 */
1431 if (adap->passthrough)
1432 goto skip_processing;
1433 /* Ignore if addressing is wrong */
1434 if (is_broadcast || from_unregistered)
1435 return 0;
1436 break;
1437
1438 case CEC_MSG_USER_CONTROL_PRESSED:
1439 case CEC_MSG_USER_CONTROL_RELEASED:
1440 /* Wrong addressing mode: don't process */
1441 if (is_broadcast || from_unregistered)
1442 goto skip_processing;
1443 break;
1444
1445 case CEC_MSG_REPORT_PHYSICAL_ADDR:
1446 /*
1447 * This message is always processed, regardless of the
1448 * passthrough setting.
1449 *
1450 * Exception: don't process if wrong addressing mode.
1451 */
1452 if (!is_broadcast)
1453 goto skip_processing;
1454 break;
1455
1456 default:
1457 break;
1458 }
1459
1460 cec_msg_set_reply_to(&tx_cec_msg, msg);
1461
1462 switch (msg->msg[1]) {
1463 /* The following messages are processed but still passed through */
1464 case CEC_MSG_REPORT_PHYSICAL_ADDR: {
1465 u16 pa = (msg->msg[2] << 8) | msg->msg[3];
1466
1467 if (!from_unregistered)
1468 adap->phys_addrs[init_laddr] = pa;
1469 dprintk(1, "Reported physical address %x.%x.%x.%x for logical address %d\n",
1470 cec_phys_addr_exp(pa), init_laddr);
1471 break;
1472 }
1473
1474 case CEC_MSG_USER_CONTROL_PRESSED:
1475 if (!(adap->capabilities & CEC_CAP_RC))
1476 break;
1477
1478#if IS_REACHABLE(CONFIG_RC_CORE)
1479 switch (msg->msg[2]) {
1480 /*
1481 * Play function, this message can have variable length
1482 * depending on the specific play function that is used.
1483 */
1484 case 0x60:
1485 if (msg->len == 2)
1486 rc_keydown(adap->rc, RC_TYPE_CEC,
1487 msg->msg[2], 0);
1488 else
1489 rc_keydown(adap->rc, RC_TYPE_CEC,
1490 msg->msg[2] << 8 | msg->msg[3], 0);
1491 break;
1492 /*
1493 * Other function messages that are not handled.
1494 * Currently the RC framework does not allow to supply an
1495 * additional parameter to a keypress. These "keys" contain
1496 * other information such as channel number, an input number
1497 * etc.
1498 * For the time being these messages are not processed by the
1499 * framework and are simply forwarded to the user space.
1500 */
1501 case 0x56: case 0x57:
1502 case 0x67: case 0x68: case 0x69: case 0x6a:
1503 break;
1504 default:
1505 rc_keydown(adap->rc, RC_TYPE_CEC, msg->msg[2], 0);
1506 break;
1507 }
1508#endif
1509 break;
1510
1511 case CEC_MSG_USER_CONTROL_RELEASED:
1512 if (!(adap->capabilities & CEC_CAP_RC))
1513 break;
1514#if IS_REACHABLE(CONFIG_RC_CORE)
1515 rc_keyup(adap->rc);
1516#endif
1517 break;
1518
1519 /*
1520 * The remaining messages are only processed if the passthrough mode
1521 * is off.
1522 */
1523 case CEC_MSG_GET_CEC_VERSION:
1524 cec_msg_cec_version(&tx_cec_msg, adap->log_addrs.cec_version);
1525 return cec_transmit_msg(adap, &tx_cec_msg, false);
1526
1527 case CEC_MSG_GIVE_PHYSICAL_ADDR:
1528 /* Do nothing for CEC switches using addr 15 */
1529 if (devtype == CEC_OP_PRIM_DEVTYPE_SWITCH && dest_laddr == 15)
1530 return 0;
1531 cec_msg_report_physical_addr(&tx_cec_msg, adap->phys_addr, devtype);
1532 return cec_transmit_msg(adap, &tx_cec_msg, false);
1533
1534 case CEC_MSG_GIVE_DEVICE_VENDOR_ID:
1535 if (adap->log_addrs.vendor_id == CEC_VENDOR_ID_NONE)
1536 return cec_feature_abort(adap, msg);
1537 cec_msg_device_vendor_id(&tx_cec_msg, adap->log_addrs.vendor_id);
1538 return cec_transmit_msg(adap, &tx_cec_msg, false);
1539
1540 case CEC_MSG_ABORT:
1541 /* Do nothing for CEC switches */
1542 if (devtype == CEC_OP_PRIM_DEVTYPE_SWITCH)
1543 return 0;
1544 return cec_feature_refused(adap, msg);
1545
1546 case CEC_MSG_GIVE_OSD_NAME: {
1547 if (adap->log_addrs.osd_name[0] == 0)
1548 return cec_feature_abort(adap, msg);
1549 cec_msg_set_osd_name(&tx_cec_msg, adap->log_addrs.osd_name);
1550 return cec_transmit_msg(adap, &tx_cec_msg, false);
1551 }
1552
1553 case CEC_MSG_GIVE_FEATURES:
1554 if (adap->log_addrs.cec_version >= CEC_OP_CEC_VERSION_2_0)
1555 return cec_report_features(adap, la_idx);
1556 return 0;
1557
1558 default:
1559 /*
1560 * Unprocessed messages are aborted if userspace isn't doing
1561 * any processing either.
1562 */
1563 if (is_directed && !is_reply && !adap->follower_cnt &&
1564 !adap->cec_follower && msg->msg[1] != CEC_MSG_FEATURE_ABORT)
1565 return cec_feature_abort(adap, msg);
1566 break;
1567 }
1568
1569skip_processing:
1570 /* If this was a reply, then we're done */
1571 if (is_reply)
1572 return 0;
1573
1574 /*
1575 * Send to the exclusive follower if there is one, otherwise send
1576 * to all followers.
1577 */
1578 if (adap->cec_follower)
1579 cec_queue_msg_fh(adap->cec_follower, msg);
1580 else
1581 cec_queue_msg_followers(adap, msg);
1582 return 0;
1583}
1584
1585/*
1586 * Helper functions to keep track of the 'monitor all' use count.
1587 *
1588 * These functions are called with adap->lock held.
1589 */
1590int cec_monitor_all_cnt_inc(struct cec_adapter *adap)
1591{
1592 int ret = 0;
1593
1594 if (adap->monitor_all_cnt == 0)
1595 ret = call_op(adap, adap_monitor_all_enable, 1);
1596 if (ret == 0)
1597 adap->monitor_all_cnt++;
1598 return ret;
1599}
1600
1601void cec_monitor_all_cnt_dec(struct cec_adapter *adap)
1602{
1603 adap->monitor_all_cnt--;
1604 if (adap->monitor_all_cnt == 0)
1605 WARN_ON(call_op(adap, adap_monitor_all_enable, 0));
1606}
1607
1608#ifdef CONFIG_MEDIA_CEC_DEBUG
1609/*
1610 * Log the current state of the CEC adapter.
1611 * Very useful for debugging.
1612 */
1613int cec_adap_status(struct seq_file *file, void *priv)
1614{
1615 struct cec_adapter *adap = dev_get_drvdata(file->private);
1616 struct cec_data *data;
1617
1618 mutex_lock(&adap->lock);
1619 seq_printf(file, "configured: %d\n", adap->is_configured);
1620 seq_printf(file, "configuring: %d\n", adap->is_configuring);
1621 seq_printf(file, "phys_addr: %x.%x.%x.%x\n",
1622 cec_phys_addr_exp(adap->phys_addr));
1623 seq_printf(file, "number of LAs: %d\n", adap->log_addrs.num_log_addrs);
1624 seq_printf(file, "LA mask: 0x%04x\n", adap->log_addrs.log_addr_mask);
1625 if (adap->cec_follower)
1626 seq_printf(file, "has CEC follower%s\n",
1627 adap->passthrough ? " (in passthrough mode)" : "");
1628 if (adap->cec_initiator)
1629 seq_puts(file, "has CEC initiator\n");
1630 if (adap->monitor_all_cnt)
1631 seq_printf(file, "file handles in Monitor All mode: %u\n",
1632 adap->monitor_all_cnt);
1633 data = adap->transmitting;
1634 if (data)
1635 seq_printf(file, "transmitting message: %*ph (reply: %02x, timeout: %ums)\n",
1636 data->msg.len, data->msg.msg, data->msg.reply,
1637 data->msg.timeout);
1638 seq_printf(file, "pending transmits: %u\n", adap->transmit_queue_sz);
1639 list_for_each_entry(data, &adap->transmit_queue, list) {
1640 seq_printf(file, "queued tx message: %*ph (reply: %02x, timeout: %ums)\n",
1641 data->msg.len, data->msg.msg, data->msg.reply,
1642 data->msg.timeout);
1643 }
1644 list_for_each_entry(data, &adap->wait_queue, list) {
1645 seq_printf(file, "message waiting for reply: %*ph (reply: %02x, timeout: %ums)\n",
1646 data->msg.len, data->msg.msg, data->msg.reply,
1647 data->msg.timeout);
1648 }
1649
1650 call_void_op(adap, adap_status, file);
1651 mutex_unlock(&adap->lock);
1652 return 0;
1653}
1654#endif
diff --git a/drivers/staging/media/cec/cec-api.c b/drivers/staging/media/cec/cec-api.c
new file mode 100644
index 000000000000..7be7615a0fdf
--- /dev/null
+++ b/drivers/staging/media/cec/cec-api.c
@@ -0,0 +1,579 @@
1/*
2 * cec-api.c - HDMI Consumer Electronics Control framework - API
3 *
4 * Copyright 2016 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
5 *
6 * This program is free software; you may redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
11 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
12 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
13 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
14 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
15 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
16 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17 * SOFTWARE.
18 */
19
20#include <linux/errno.h>
21#include <linux/init.h>
22#include <linux/module.h>
23#include <linux/kernel.h>
24#include <linux/kmod.h>
25#include <linux/ktime.h>
26#include <linux/slab.h>
27#include <linux/mm.h>
28#include <linux/string.h>
29#include <linux/types.h>
30#include <linux/uaccess.h>
31#include <linux/version.h>
32
33#include "cec-priv.h"
34
35static inline struct cec_devnode *cec_devnode_data(struct file *filp)
36{
37 struct cec_fh *fh = filp->private_data;
38
39 return &fh->adap->devnode;
40}
41
42/* CEC file operations */
43
44static unsigned int cec_poll(struct file *filp,
45 struct poll_table_struct *poll)
46{
47 struct cec_devnode *devnode = cec_devnode_data(filp);
48 struct cec_fh *fh = filp->private_data;
49 struct cec_adapter *adap = fh->adap;
50 unsigned int res = 0;
51
52 if (!devnode->registered)
53 return POLLERR | POLLHUP;
54 mutex_lock(&adap->lock);
55 if (adap->is_configured &&
56 adap->transmit_queue_sz < CEC_MAX_MSG_TX_QUEUE_SZ)
57 res |= POLLOUT | POLLWRNORM;
58 if (fh->queued_msgs)
59 res |= POLLIN | POLLRDNORM;
60 if (fh->pending_events)
61 res |= POLLPRI;
62 poll_wait(filp, &fh->wait, poll);
63 mutex_unlock(&adap->lock);
64 return res;
65}
66
67static bool cec_is_busy(const struct cec_adapter *adap,
68 const struct cec_fh *fh)
69{
70 bool valid_initiator = adap->cec_initiator && adap->cec_initiator == fh;
71 bool valid_follower = adap->cec_follower && adap->cec_follower == fh;
72
73 /*
74 * Exclusive initiators and followers can always access the CEC adapter
75 */
76 if (valid_initiator || valid_follower)
77 return false;
78 /*
79 * All others can only access the CEC adapter if there is no
80 * exclusive initiator and they are in INITIATOR mode.
81 */
82 return adap->cec_initiator ||
83 fh->mode_initiator == CEC_MODE_NO_INITIATOR;
84}
85
86static long cec_adap_g_caps(struct cec_adapter *adap,
87 struct cec_caps __user *parg)
88{
89 struct cec_caps caps = {};
90
91 strlcpy(caps.driver, adap->devnode.parent->driver->name,
92 sizeof(caps.driver));
93 strlcpy(caps.name, adap->name, sizeof(caps.name));
94 caps.available_log_addrs = adap->available_log_addrs;
95 caps.capabilities = adap->capabilities;
96 caps.version = LINUX_VERSION_CODE;
97 if (copy_to_user(parg, &caps, sizeof(caps)))
98 return -EFAULT;
99 return 0;
100}
101
102static long cec_adap_g_phys_addr(struct cec_adapter *adap,
103 __u16 __user *parg)
104{
105 u16 phys_addr;
106
107 mutex_lock(&adap->lock);
108 phys_addr = adap->phys_addr;
109 mutex_unlock(&adap->lock);
110 if (copy_to_user(parg, &phys_addr, sizeof(phys_addr)))
111 return -EFAULT;
112 return 0;
113}
114
115static long cec_adap_s_phys_addr(struct cec_adapter *adap, struct cec_fh *fh,
116 bool block, __u16 __user *parg)
117{
118 u16 phys_addr;
119 long err;
120
121 if (!(adap->capabilities & CEC_CAP_PHYS_ADDR))
122 return -ENOTTY;
123 if (copy_from_user(&phys_addr, parg, sizeof(phys_addr)))
124 return -EFAULT;
125
126 err = cec_phys_addr_validate(phys_addr, NULL, NULL);
127 if (err)
128 return err;
129 mutex_lock(&adap->lock);
130 if (cec_is_busy(adap, fh))
131 err = -EBUSY;
132 else
133 __cec_s_phys_addr(adap, phys_addr, block);
134 mutex_unlock(&adap->lock);
135 return err;
136}
137
138static long cec_adap_g_log_addrs(struct cec_adapter *adap,
139 struct cec_log_addrs __user *parg)
140{
141 struct cec_log_addrs log_addrs;
142
143 mutex_lock(&adap->lock);
144 log_addrs = adap->log_addrs;
145 if (!adap->is_configured)
146 memset(log_addrs.log_addr, CEC_LOG_ADDR_INVALID,
147 sizeof(log_addrs.log_addr));
148 mutex_unlock(&adap->lock);
149
150 if (copy_to_user(parg, &log_addrs, sizeof(log_addrs)))
151 return -EFAULT;
152 return 0;
153}
154
155static long cec_adap_s_log_addrs(struct cec_adapter *adap, struct cec_fh *fh,
156 bool block, struct cec_log_addrs __user *parg)
157{
158 struct cec_log_addrs log_addrs;
159 long err = -EBUSY;
160
161 if (!(adap->capabilities & CEC_CAP_LOG_ADDRS))
162 return -ENOTTY;
163 if (copy_from_user(&log_addrs, parg, sizeof(log_addrs)))
164 return -EFAULT;
165 log_addrs.flags = 0;
166 mutex_lock(&adap->lock);
167 if (!adap->is_configuring &&
168 (!log_addrs.num_log_addrs || !adap->is_configured) &&
169 !cec_is_busy(adap, fh)) {
170 err = __cec_s_log_addrs(adap, &log_addrs, block);
171 if (!err)
172 log_addrs = adap->log_addrs;
173 }
174 mutex_unlock(&adap->lock);
175 if (err)
176 return err;
177 if (copy_to_user(parg, &log_addrs, sizeof(log_addrs)))
178 return -EFAULT;
179 return 0;
180}
181
182static long cec_transmit(struct cec_adapter *adap, struct cec_fh *fh,
183 bool block, struct cec_msg __user *parg)
184{
185 struct cec_msg msg = {};
186 long err = 0;
187
188 if (!(adap->capabilities & CEC_CAP_TRANSMIT))
189 return -ENOTTY;
190 if (copy_from_user(&msg, parg, sizeof(msg)))
191 return -EFAULT;
192 mutex_lock(&adap->lock);
193 if (!adap->is_configured)
194 err = -ENONET;
195 else if (cec_is_busy(adap, fh))
196 err = -EBUSY;
197 else
198 err = cec_transmit_msg_fh(adap, &msg, fh, block);
199 mutex_unlock(&adap->lock);
200 if (err)
201 return err;
202 if (copy_to_user(parg, &msg, sizeof(msg)))
203 return -EFAULT;
204 return 0;
205}
206
207/* Called by CEC_RECEIVE: wait for a message to arrive */
208static int cec_receive_msg(struct cec_fh *fh, struct cec_msg *msg, bool block)
209{
210 u32 timeout = msg->timeout;
211 int res;
212
213 do {
214 mutex_lock(&fh->lock);
215 /* Are there received messages queued up? */
216 if (fh->queued_msgs) {
217 /* Yes, return the first one */
218 struct cec_msg_entry *entry =
219 list_first_entry(&fh->msgs,
220 struct cec_msg_entry, list);
221
222 list_del(&entry->list);
223 *msg = entry->msg;
224 kfree(entry);
225 fh->queued_msgs--;
226 mutex_unlock(&fh->lock);
227 /* restore original timeout value */
228 msg->timeout = timeout;
229 return 0;
230 }
231
232 /* No, return EAGAIN in non-blocking mode or wait */
233 mutex_unlock(&fh->lock);
234
235 /* Return when in non-blocking mode */
236 if (!block)
237 return -EAGAIN;
238
239 if (msg->timeout) {
240 /* The user specified a timeout */
241 res = wait_event_interruptible_timeout(fh->wait,
242 fh->queued_msgs,
243 msecs_to_jiffies(msg->timeout));
244 if (res == 0)
245 res = -ETIMEDOUT;
246 else if (res > 0)
247 res = 0;
248 } else {
249 /* Wait indefinitely */
250 res = wait_event_interruptible(fh->wait,
251 fh->queued_msgs);
252 }
253 /* Exit on error, otherwise loop to get the new message */
254 } while (!res);
255 return res;
256}
257
258static long cec_receive(struct cec_adapter *adap, struct cec_fh *fh,
259 bool block, struct cec_msg __user *parg)
260{
261 struct cec_msg msg = {};
262 long err = 0;
263
264 if (copy_from_user(&msg, parg, sizeof(msg)))
265 return -EFAULT;
266 mutex_lock(&adap->lock);
267 if (!adap->is_configured && fh->mode_follower < CEC_MODE_MONITOR)
268 err = -ENONET;
269 mutex_unlock(&adap->lock);
270 if (err)
271 return err;
272
273 err = cec_receive_msg(fh, &msg, block);
274 if (err)
275 return err;
276 if (copy_to_user(parg, &msg, sizeof(msg)))
277 return -EFAULT;
278 return 0;
279}
280
281static long cec_dqevent(struct cec_adapter *adap, struct cec_fh *fh,
282 bool block, struct cec_event __user *parg)
283{
284 struct cec_event *ev = NULL;
285 u64 ts = ~0ULL;
286 unsigned int i;
287 long err = 0;
288
289 mutex_lock(&fh->lock);
290 while (!fh->pending_events && block) {
291 mutex_unlock(&fh->lock);
292 err = wait_event_interruptible(fh->wait, fh->pending_events);
293 if (err)
294 return err;
295 mutex_lock(&fh->lock);
296 }
297
298 /* Find the oldest event */
299 for (i = 0; i < CEC_NUM_EVENTS; i++) {
300 if (fh->pending_events & (1 << (i + 1)) &&
301 fh->events[i].ts <= ts) {
302 ev = &fh->events[i];
303 ts = ev->ts;
304 }
305 }
306 if (!ev) {
307 err = -EAGAIN;
308 goto unlock;
309 }
310
311 if (copy_to_user(parg, ev, sizeof(*ev))) {
312 err = -EFAULT;
313 goto unlock;
314 }
315
316 fh->pending_events &= ~(1 << ev->event);
317
318unlock:
319 mutex_unlock(&fh->lock);
320 return err;
321}
322
323static long cec_g_mode(struct cec_adapter *adap, struct cec_fh *fh,
324 u32 __user *parg)
325{
326 u32 mode = fh->mode_initiator | fh->mode_follower;
327
328 if (copy_to_user(parg, &mode, sizeof(mode)))
329 return -EFAULT;
330 return 0;
331}
332
333static long cec_s_mode(struct cec_adapter *adap, struct cec_fh *fh,
334 u32 __user *parg)
335{
336 u32 mode;
337 u8 mode_initiator;
338 u8 mode_follower;
339 long err = 0;
340
341 if (copy_from_user(&mode, parg, sizeof(mode)))
342 return -EFAULT;
343 if (mode & ~(CEC_MODE_INITIATOR_MSK | CEC_MODE_FOLLOWER_MSK))
344 return -EINVAL;
345
346 mode_initiator = mode & CEC_MODE_INITIATOR_MSK;
347 mode_follower = mode & CEC_MODE_FOLLOWER_MSK;
348
349 if (mode_initiator > CEC_MODE_EXCL_INITIATOR ||
350 mode_follower > CEC_MODE_MONITOR_ALL)
351 return -EINVAL;
352
353 if (mode_follower == CEC_MODE_MONITOR_ALL &&
354 !(adap->capabilities & CEC_CAP_MONITOR_ALL))
355 return -EINVAL;
356
357 /* Follower modes should always be able to send CEC messages */
358 if ((mode_initiator == CEC_MODE_NO_INITIATOR ||
359 !(adap->capabilities & CEC_CAP_TRANSMIT)) &&
360 mode_follower >= CEC_MODE_FOLLOWER &&
361 mode_follower <= CEC_MODE_EXCL_FOLLOWER_PASSTHRU)
362 return -EINVAL;
363
364 /* Monitor modes require CEC_MODE_NO_INITIATOR */
365 if (mode_initiator && mode_follower >= CEC_MODE_MONITOR)
366 return -EINVAL;
367
368 /* Monitor modes require CAP_NET_ADMIN */
369 if (mode_follower >= CEC_MODE_MONITOR && !capable(CAP_NET_ADMIN))
370 return -EPERM;
371
372 mutex_lock(&adap->lock);
373 /*
374 * You can't become exclusive follower if someone else already
375 * has that job.
376 */
377 if ((mode_follower == CEC_MODE_EXCL_FOLLOWER ||
378 mode_follower == CEC_MODE_EXCL_FOLLOWER_PASSTHRU) &&
379 adap->cec_follower && adap->cec_follower != fh)
380 err = -EBUSY;
381 /*
382 * You can't become exclusive initiator if someone else already
383 * has that job.
384 */
385 if (mode_initiator == CEC_MODE_EXCL_INITIATOR &&
386 adap->cec_initiator && adap->cec_initiator != fh)
387 err = -EBUSY;
388
389 if (!err) {
390 bool old_mon_all = fh->mode_follower == CEC_MODE_MONITOR_ALL;
391 bool new_mon_all = mode_follower == CEC_MODE_MONITOR_ALL;
392
393 if (old_mon_all != new_mon_all) {
394 if (new_mon_all)
395 err = cec_monitor_all_cnt_inc(adap);
396 else
397 cec_monitor_all_cnt_dec(adap);
398 }
399 }
400
401 if (err) {
402 mutex_unlock(&adap->lock);
403 return err;
404 }
405
406 if (fh->mode_follower == CEC_MODE_FOLLOWER)
407 adap->follower_cnt--;
408 if (mode_follower == CEC_MODE_FOLLOWER)
409 adap->follower_cnt++;
410 if (mode_follower == CEC_MODE_EXCL_FOLLOWER ||
411 mode_follower == CEC_MODE_EXCL_FOLLOWER_PASSTHRU) {
412 adap->passthrough =
413 mode_follower == CEC_MODE_EXCL_FOLLOWER_PASSTHRU;
414 adap->cec_follower = fh;
415 } else if (adap->cec_follower == fh) {
416 adap->passthrough = false;
417 adap->cec_follower = NULL;
418 }
419 if (mode_initiator == CEC_MODE_EXCL_INITIATOR)
420 adap->cec_initiator = fh;
421 else if (adap->cec_initiator == fh)
422 adap->cec_initiator = NULL;
423 fh->mode_initiator = mode_initiator;
424 fh->mode_follower = mode_follower;
425 mutex_unlock(&adap->lock);
426 return 0;
427}
428
429static long cec_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
430{
431 struct cec_devnode *devnode = cec_devnode_data(filp);
432 struct cec_fh *fh = filp->private_data;
433 struct cec_adapter *adap = fh->adap;
434 bool block = !(filp->f_flags & O_NONBLOCK);
435 void __user *parg = (void __user *)arg;
436
437 if (!devnode->registered)
438 return -EIO;
439
440 switch (cmd) {
441 case CEC_ADAP_G_CAPS:
442 return cec_adap_g_caps(adap, parg);
443
444 case CEC_ADAP_G_PHYS_ADDR:
445 return cec_adap_g_phys_addr(adap, parg);
446
447 case CEC_ADAP_S_PHYS_ADDR:
448 return cec_adap_s_phys_addr(adap, fh, block, parg);
449
450 case CEC_ADAP_G_LOG_ADDRS:
451 return cec_adap_g_log_addrs(adap, parg);
452
453 case CEC_ADAP_S_LOG_ADDRS:
454 return cec_adap_s_log_addrs(adap, fh, block, parg);
455
456 case CEC_TRANSMIT:
457 return cec_transmit(adap, fh, block, parg);
458
459 case CEC_RECEIVE:
460 return cec_receive(adap, fh, block, parg);
461
462 case CEC_DQEVENT:
463 return cec_dqevent(adap, fh, block, parg);
464
465 case CEC_G_MODE:
466 return cec_g_mode(adap, fh, parg);
467
468 case CEC_S_MODE:
469 return cec_s_mode(adap, fh, parg);
470
471 default:
472 return -ENOTTY;
473 }
474}
475
476static int cec_open(struct inode *inode, struct file *filp)
477{
478 struct cec_devnode *devnode =
479 container_of(inode->i_cdev, struct cec_devnode, cdev);
480 struct cec_adapter *adap = to_cec_adapter(devnode);
481 struct cec_fh *fh = kzalloc(sizeof(*fh), GFP_KERNEL);
482 /*
483 * Initial events that are automatically sent when the cec device is
484 * opened.
485 */
486 struct cec_event ev_state = {
487 .event = CEC_EVENT_STATE_CHANGE,
488 .flags = CEC_EVENT_FL_INITIAL_STATE,
489 };
490 int err;
491
492 if (!fh)
493 return -ENOMEM;
494
495 INIT_LIST_HEAD(&fh->msgs);
496 INIT_LIST_HEAD(&fh->xfer_list);
497 mutex_init(&fh->lock);
498 init_waitqueue_head(&fh->wait);
499
500 fh->mode_initiator = CEC_MODE_INITIATOR;
501 fh->adap = adap;
502
503 err = cec_get_device(devnode);
504 if (err) {
505 kfree(fh);
506 return err;
507 }
508
509 filp->private_data = fh;
510
511 mutex_lock(&devnode->fhs_lock);
512 /* Queue up initial state events */
513 ev_state.state_change.phys_addr = adap->phys_addr;
514 ev_state.state_change.log_addr_mask = adap->log_addrs.log_addr_mask;
515 cec_queue_event_fh(fh, &ev_state, 0);
516
517 list_add(&fh->list, &devnode->fhs);
518 mutex_unlock(&devnode->fhs_lock);
519
520 return 0;
521}
522
523/* Override for the release function */
524static int cec_release(struct inode *inode, struct file *filp)
525{
526 struct cec_devnode *devnode = cec_devnode_data(filp);
527 struct cec_adapter *adap = to_cec_adapter(devnode);
528 struct cec_fh *fh = filp->private_data;
529
530 mutex_lock(&adap->lock);
531 if (adap->cec_initiator == fh)
532 adap->cec_initiator = NULL;
533 if (adap->cec_follower == fh) {
534 adap->cec_follower = NULL;
535 adap->passthrough = false;
536 }
537 if (fh->mode_follower == CEC_MODE_FOLLOWER)
538 adap->follower_cnt--;
539 if (fh->mode_follower == CEC_MODE_MONITOR_ALL)
540 cec_monitor_all_cnt_dec(adap);
541 mutex_unlock(&adap->lock);
542
543 mutex_lock(&devnode->fhs_lock);
544 list_del(&fh->list);
545 mutex_unlock(&devnode->fhs_lock);
546
547 /* Unhook pending transmits from this filehandle. */
548 mutex_lock(&adap->lock);
549 while (!list_empty(&fh->xfer_list)) {
550 struct cec_data *data =
551 list_first_entry(&fh->xfer_list, struct cec_data, xfer_list);
552
553 data->blocking = false;
554 data->fh = NULL;
555 list_del(&data->xfer_list);
556 }
557 mutex_unlock(&adap->lock);
558 while (!list_empty(&fh->msgs)) {
559 struct cec_msg_entry *entry =
560 list_first_entry(&fh->msgs, struct cec_msg_entry, list);
561
562 list_del(&entry->list);
563 kfree(entry);
564 }
565 kfree(fh);
566
567 cec_put_device(devnode);
568 filp->private_data = NULL;
569 return 0;
570}
571
572const struct file_operations cec_devnode_fops = {
573 .owner = THIS_MODULE,
574 .open = cec_open,
575 .unlocked_ioctl = cec_ioctl,
576 .release = cec_release,
577 .poll = cec_poll,
578 .llseek = no_llseek,
579};
diff --git a/drivers/staging/media/cec/cec-core.c b/drivers/staging/media/cec/cec-core.c
new file mode 100644
index 000000000000..112a5fae12f5
--- /dev/null
+++ b/drivers/staging/media/cec/cec-core.c
@@ -0,0 +1,409 @@
1/*
2 * cec-core.c - HDMI Consumer Electronics Control framework - Core
3 *
4 * Copyright 2016 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
5 *
6 * This program is free software; you may redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
11 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
12 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
13 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
14 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
15 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
16 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17 * SOFTWARE.
18 */
19
20#include <linux/errno.h>
21#include <linux/init.h>
22#include <linux/module.h>
23#include <linux/kernel.h>
24#include <linux/kmod.h>
25#include <linux/slab.h>
26#include <linux/mm.h>
27#include <linux/string.h>
28#include <linux/types.h>
29
30#include "cec-priv.h"
31
32#define CEC_NUM_DEVICES 256
33#define CEC_NAME "cec"
34
35int cec_debug;
36module_param_named(debug, cec_debug, int, 0644);
37MODULE_PARM_DESC(debug, "debug level (0-2)");
38
39static dev_t cec_dev_t;
40
41/* Active devices */
42static DEFINE_MUTEX(cec_devnode_lock);
43static DECLARE_BITMAP(cec_devnode_nums, CEC_NUM_DEVICES);
44
45static struct dentry *top_cec_dir;
46
47/* dev to cec_devnode */
48#define to_cec_devnode(cd) container_of(cd, struct cec_devnode, dev)
49
50int cec_get_device(struct cec_devnode *devnode)
51{
52 /*
53 * Check if the cec device is available. This needs to be done with
54 * the cec_devnode_lock held to prevent an open/unregister race:
55 * without the lock, the device could be unregistered and freed between
56 * the devnode->registered check and get_device() calls, leading to
57 * a crash.
58 */
59 mutex_lock(&cec_devnode_lock);
60 /*
61 * return ENXIO if the cec device has been removed
62 * already or if it is not registered anymore.
63 */
64 if (!devnode->registered) {
65 mutex_unlock(&cec_devnode_lock);
66 return -ENXIO;
67 }
68 /* and increase the device refcount */
69 get_device(&devnode->dev);
70 mutex_unlock(&cec_devnode_lock);
71 return 0;
72}
73
74void cec_put_device(struct cec_devnode *devnode)
75{
76 mutex_lock(&cec_devnode_lock);
77 put_device(&devnode->dev);
78 mutex_unlock(&cec_devnode_lock);
79}
80
81/* Called when the last user of the cec device exits. */
82static void cec_devnode_release(struct device *cd)
83{
84 struct cec_devnode *devnode = to_cec_devnode(cd);
85
86 mutex_lock(&cec_devnode_lock);
87
88 /* Mark device node number as free */
89 clear_bit(devnode->minor, cec_devnode_nums);
90
91 mutex_unlock(&cec_devnode_lock);
92 cec_delete_adapter(to_cec_adapter(devnode));
93}
94
95static struct bus_type cec_bus_type = {
96 .name = CEC_NAME,
97};
98
99/*
100 * Register a cec device node
101 *
102 * The registration code assigns minor numbers and registers the new device node
103 * with the kernel. An error is returned if no free minor number can be found,
104 * or if the registration of the device node fails.
105 *
106 * Zero is returned on success.
107 *
108 * Note that if the cec_devnode_register call fails, the release() callback of
109 * the cec_devnode structure is *not* called, so the caller is responsible for
110 * freeing any data.
111 */
112static int __must_check cec_devnode_register(struct cec_devnode *devnode,
113 struct module *owner)
114{
115 int minor;
116 int ret;
117
118 /* Initialization */
119 INIT_LIST_HEAD(&devnode->fhs);
120 mutex_init(&devnode->fhs_lock);
121
122 /* Part 1: Find a free minor number */
123 mutex_lock(&cec_devnode_lock);
124 minor = find_next_zero_bit(cec_devnode_nums, CEC_NUM_DEVICES, 0);
125 if (minor == CEC_NUM_DEVICES) {
126 mutex_unlock(&cec_devnode_lock);
127 pr_err("could not get a free minor\n");
128 return -ENFILE;
129 }
130
131 set_bit(minor, cec_devnode_nums);
132 mutex_unlock(&cec_devnode_lock);
133
134 devnode->minor = minor;
135 devnode->dev.bus = &cec_bus_type;
136 devnode->dev.devt = MKDEV(MAJOR(cec_dev_t), minor);
137 devnode->dev.release = cec_devnode_release;
138 devnode->dev.parent = devnode->parent;
139 dev_set_name(&devnode->dev, "cec%d", devnode->minor);
140 device_initialize(&devnode->dev);
141
142 /* Part 2: Initialize and register the character device */
143 cdev_init(&devnode->cdev, &cec_devnode_fops);
144 devnode->cdev.kobj.parent = &devnode->dev.kobj;
145 devnode->cdev.owner = owner;
146
147 ret = cdev_add(&devnode->cdev, devnode->dev.devt, 1);
148 if (ret < 0) {
149 pr_err("%s: cdev_add failed\n", __func__);
150 goto clr_bit;
151 }
152
153 ret = device_add(&devnode->dev);
154 if (ret)
155 goto cdev_del;
156
157 devnode->registered = true;
158 return 0;
159
160cdev_del:
161 cdev_del(&devnode->cdev);
162clr_bit:
163 clear_bit(devnode->minor, cec_devnode_nums);
164 return ret;
165}
166
167/*
168 * Unregister a cec device node
169 *
170 * This unregisters the passed device. Future open calls will be met with
171 * errors.
172 *
173 * This function can safely be called if the device node has never been
174 * registered or has already been unregistered.
175 */
176static void cec_devnode_unregister(struct cec_devnode *devnode)
177{
178 struct cec_fh *fh;
179
180 /* Check if devnode was never registered or already unregistered */
181 if (!devnode->registered || devnode->unregistered)
182 return;
183
184 mutex_lock(&devnode->fhs_lock);
185 list_for_each_entry(fh, &devnode->fhs, list)
186 wake_up_interruptible(&fh->wait);
187 mutex_unlock(&devnode->fhs_lock);
188
189 devnode->registered = false;
190 devnode->unregistered = true;
191 device_del(&devnode->dev);
192 cdev_del(&devnode->cdev);
193 put_device(&devnode->dev);
194}
195
196struct cec_adapter *cec_allocate_adapter(const struct cec_adap_ops *ops,
197 void *priv, const char *name, u32 caps,
198 u8 available_las, struct device *parent)
199{
200 struct cec_adapter *adap;
201 int res;
202
203 if (WARN_ON(!parent))
204 return ERR_PTR(-EINVAL);
205 if (WARN_ON(!caps))
206 return ERR_PTR(-EINVAL);
207 if (WARN_ON(!ops))
208 return ERR_PTR(-EINVAL);
209 if (WARN_ON(!available_las || available_las > CEC_MAX_LOG_ADDRS))
210 return ERR_PTR(-EINVAL);
211 adap = kzalloc(sizeof(*adap), GFP_KERNEL);
212 if (!adap)
213 return ERR_PTR(-ENOMEM);
214 adap->owner = parent->driver->owner;
215 adap->devnode.parent = parent;
216 strlcpy(adap->name, name, sizeof(adap->name));
217 adap->phys_addr = CEC_PHYS_ADDR_INVALID;
218 adap->log_addrs.cec_version = CEC_OP_CEC_VERSION_2_0;
219 adap->log_addrs.vendor_id = CEC_VENDOR_ID_NONE;
220 adap->capabilities = caps;
221 adap->available_log_addrs = available_las;
222 adap->sequence = 0;
223 adap->ops = ops;
224 adap->priv = priv;
225 memset(adap->phys_addrs, 0xff, sizeof(adap->phys_addrs));
226 mutex_init(&adap->lock);
227 INIT_LIST_HEAD(&adap->transmit_queue);
228 INIT_LIST_HEAD(&adap->wait_queue);
229 init_waitqueue_head(&adap->kthread_waitq);
230
231 adap->kthread = kthread_run(cec_thread_func, adap, "cec-%s", name);
232 if (IS_ERR(adap->kthread)) {
233 pr_err("cec-%s: kernel_thread() failed\n", name);
234 res = PTR_ERR(adap->kthread);
235 kfree(adap);
236 return ERR_PTR(res);
237 }
238
239 if (!(caps & CEC_CAP_RC))
240 return adap;
241
242#if IS_REACHABLE(CONFIG_RC_CORE)
243 /* Prepare the RC input device */
244 adap->rc = rc_allocate_device();
245 if (!adap->rc) {
246 pr_err("cec-%s: failed to allocate memory for rc_dev\n",
247 name);
248 kthread_stop(adap->kthread);
249 kfree(adap);
250 return ERR_PTR(-ENOMEM);
251 }
252
253 snprintf(adap->input_name, sizeof(adap->input_name),
254 "RC for %s", name);
255 snprintf(adap->input_phys, sizeof(adap->input_phys),
256 "%s/input0", name);
257
258 adap->rc->input_name = adap->input_name;
259 adap->rc->input_phys = adap->input_phys;
260 adap->rc->input_id.bustype = BUS_CEC;
261 adap->rc->input_id.vendor = 0;
262 adap->rc->input_id.product = 0;
263 adap->rc->input_id.version = 1;
264 adap->rc->dev.parent = parent;
265 adap->rc->driver_type = RC_DRIVER_SCANCODE;
266 adap->rc->driver_name = CEC_NAME;
267 adap->rc->allowed_protocols = RC_BIT_CEC;
268 adap->rc->priv = adap;
269 adap->rc->map_name = RC_MAP_CEC;
270 adap->rc->timeout = MS_TO_NS(100);
271#else
272 adap->capabilities &= ~CEC_CAP_RC;
273#endif
274 return adap;
275}
276EXPORT_SYMBOL_GPL(cec_allocate_adapter);
277
278int cec_register_adapter(struct cec_adapter *adap)
279{
280 int res;
281
282 if (IS_ERR_OR_NULL(adap))
283 return 0;
284
285#if IS_REACHABLE(CONFIG_RC_CORE)
286 if (adap->capabilities & CEC_CAP_RC) {
287 res = rc_register_device(adap->rc);
288
289 if (res) {
290 pr_err("cec-%s: failed to prepare input device\n",
291 adap->name);
292 rc_free_device(adap->rc);
293 adap->rc = NULL;
294 return res;
295 }
296 }
297#endif
298
299 res = cec_devnode_register(&adap->devnode, adap->owner);
300 if (res) {
301#if IS_REACHABLE(CONFIG_RC_CORE)
302 /* Note: rc_unregister also calls rc_free */
303 rc_unregister_device(adap->rc);
304 adap->rc = NULL;
305#endif
306 return res;
307 }
308
309 dev_set_drvdata(&adap->devnode.dev, adap);
310#ifdef CONFIG_MEDIA_CEC_DEBUG
311 if (!top_cec_dir)
312 return 0;
313
314 adap->cec_dir = debugfs_create_dir(dev_name(&adap->devnode.dev), top_cec_dir);
315 if (IS_ERR_OR_NULL(adap->cec_dir)) {
316 pr_warn("cec-%s: Failed to create debugfs dir\n", adap->name);
317 return 0;
318 }
319 adap->status_file = debugfs_create_devm_seqfile(&adap->devnode.dev,
320 "status", adap->cec_dir, cec_adap_status);
321 if (IS_ERR_OR_NULL(adap->status_file)) {
322 pr_warn("cec-%s: Failed to create status file\n", adap->name);
323 debugfs_remove_recursive(adap->cec_dir);
324 adap->cec_dir = NULL;
325 }
326#endif
327 return 0;
328}
329EXPORT_SYMBOL_GPL(cec_register_adapter);
330
331void cec_unregister_adapter(struct cec_adapter *adap)
332{
333 if (IS_ERR_OR_NULL(adap))
334 return;
335
336#if IS_REACHABLE(CONFIG_RC_CORE)
337 /* Note: rc_unregister also calls rc_free */
338 rc_unregister_device(adap->rc);
339 adap->rc = NULL;
340#endif
341 debugfs_remove_recursive(adap->cec_dir);
342 cec_devnode_unregister(&adap->devnode);
343}
344EXPORT_SYMBOL_GPL(cec_unregister_adapter);
345
346void cec_delete_adapter(struct cec_adapter *adap)
347{
348 if (IS_ERR_OR_NULL(adap))
349 return;
350 mutex_lock(&adap->lock);
351 __cec_s_phys_addr(adap, CEC_PHYS_ADDR_INVALID, false);
352 mutex_unlock(&adap->lock);
353 kthread_stop(adap->kthread);
354 if (adap->kthread_config)
355 kthread_stop(adap->kthread_config);
356#if IS_REACHABLE(CONFIG_RC_CORE)
357 if (adap->rc)
358 rc_free_device(adap->rc);
359#endif
360 kfree(adap);
361}
362EXPORT_SYMBOL_GPL(cec_delete_adapter);
363
364/*
365 * Initialise cec for linux
366 */
367static int __init cec_devnode_init(void)
368{
369 int ret;
370
371 pr_info("Linux cec interface: v0.10\n");
372 ret = alloc_chrdev_region(&cec_dev_t, 0, CEC_NUM_DEVICES,
373 CEC_NAME);
374 if (ret < 0) {
375 pr_warn("cec: unable to allocate major\n");
376 return ret;
377 }
378
379#ifdef CONFIG_MEDIA_CEC_DEBUG
380 top_cec_dir = debugfs_create_dir("cec", NULL);
381 if (IS_ERR_OR_NULL(top_cec_dir)) {
382 pr_warn("cec: Failed to create debugfs cec dir\n");
383 top_cec_dir = NULL;
384 }
385#endif
386
387 ret = bus_register(&cec_bus_type);
388 if (ret < 0) {
389 unregister_chrdev_region(cec_dev_t, CEC_NUM_DEVICES);
390 pr_warn("cec: bus_register failed\n");
391 return -EIO;
392 }
393
394 return 0;
395}
396
397static void __exit cec_devnode_exit(void)
398{
399 debugfs_remove_recursive(top_cec_dir);
400 bus_unregister(&cec_bus_type);
401 unregister_chrdev_region(cec_dev_t, CEC_NUM_DEVICES);
402}
403
404subsys_initcall(cec_devnode_init);
405module_exit(cec_devnode_exit)
406
407MODULE_AUTHOR("Hans Verkuil <hans.verkuil@cisco.com>");
408MODULE_DESCRIPTION("Device node registration for cec drivers");
409MODULE_LICENSE("GPL");
diff --git a/drivers/staging/media/cec/cec-priv.h b/drivers/staging/media/cec/cec-priv.h
new file mode 100644
index 000000000000..70767a7900f2
--- /dev/null
+++ b/drivers/staging/media/cec/cec-priv.h
@@ -0,0 +1,56 @@
1/*
2 * cec-priv.h - HDMI Consumer Electronics Control internal header
3 *
4 * Copyright 2016 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
5 *
6 * This program is free software; you may redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
11 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
12 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
13 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
14 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
15 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
16 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17 * SOFTWARE.
18 */
19
20#ifndef _CEC_PRIV_H
21#define _CEC_PRIV_H
22
23#include <linux/cec-funcs.h>
24#include <media/cec.h>
25
26#define dprintk(lvl, fmt, arg...) \
27 do { \
28 if (lvl <= cec_debug) \
29 pr_info("cec-%s: " fmt, adap->name, ## arg); \
30 } while (0)
31
32/* devnode to cec_adapter */
33#define to_cec_adapter(node) container_of(node, struct cec_adapter, devnode)
34
35/* cec-core.c */
36extern int cec_debug;
37int cec_get_device(struct cec_devnode *devnode);
38void cec_put_device(struct cec_devnode *devnode);
39
40/* cec-adap.c */
41int cec_monitor_all_cnt_inc(struct cec_adapter *adap);
42void cec_monitor_all_cnt_dec(struct cec_adapter *adap);
43int cec_adap_status(struct seq_file *file, void *priv);
44int cec_thread_func(void *_adap);
45void __cec_s_phys_addr(struct cec_adapter *adap, u16 phys_addr, bool block);
46int __cec_s_log_addrs(struct cec_adapter *adap,
47 struct cec_log_addrs *log_addrs, bool block);
48int cec_transmit_msg_fh(struct cec_adapter *adap, struct cec_msg *msg,
49 struct cec_fh *fh, bool block);
50void cec_queue_event_fh(struct cec_fh *fh,
51 const struct cec_event *new_ev, u64 ts);
52
53/* cec-api.c */
54extern const struct file_operations cec_devnode_fops;
55
56#endif
diff --git a/drivers/staging/media/davinci_vpfe/vpfe_video.c b/drivers/staging/media/davinci_vpfe/vpfe_video.c
index ea3ddec75806..3319fb8f7d01 100644
--- a/drivers/staging/media/davinci_vpfe/vpfe_video.c
+++ b/drivers/staging/media/davinci_vpfe/vpfe_video.c
@@ -542,7 +542,6 @@ static int vpfe_release(struct file *file)
542 video->io_usrs = 0; 542 video->io_usrs = 0;
543 /* Free buffers allocated */ 543 /* Free buffers allocated */
544 vb2_queue_release(&video->buffer_queue); 544 vb2_queue_release(&video->buffer_queue);
545 vb2_dma_contig_cleanup_ctx(video->alloc_ctx);
546 } 545 }
547 /* Decrement device users counter */ 546 /* Decrement device users counter */
548 video->usrs--; 547 video->usrs--;
@@ -1092,7 +1091,7 @@ vpfe_g_dv_timings(struct file *file, void *fh,
1092 * @nbuffers: ptr to number of buffers requested by application 1091 * @nbuffers: ptr to number of buffers requested by application
1093 * @nplanes:: contains number of distinct video planes needed to hold a frame 1092 * @nplanes:: contains number of distinct video planes needed to hold a frame
1094 * @sizes[]: contains the size (in bytes) of each plane. 1093 * @sizes[]: contains the size (in bytes) of each plane.
1095 * @alloc_ctxs: ptr to allocation context 1094 * @alloc_devs: ptr to allocation context
1096 * 1095 *
1097 * This callback function is called when reqbuf() is called to adjust 1096 * This callback function is called when reqbuf() is called to adjust
1098 * the buffer nbuffers and buffer size 1097 * the buffer nbuffers and buffer size
@@ -1100,7 +1099,7 @@ vpfe_g_dv_timings(struct file *file, void *fh,
1100static int 1099static int
1101vpfe_buffer_queue_setup(struct vb2_queue *vq, 1100vpfe_buffer_queue_setup(struct vb2_queue *vq,
1102 unsigned int *nbuffers, unsigned int *nplanes, 1101 unsigned int *nbuffers, unsigned int *nplanes,
1103 unsigned int sizes[], void *alloc_ctxs[]) 1102 unsigned int sizes[], struct device *alloc_devs[])
1104{ 1103{
1105 struct vpfe_fh *fh = vb2_get_drv_priv(vq); 1104 struct vpfe_fh *fh = vb2_get_drv_priv(vq);
1106 struct vpfe_video_device *video = fh->video; 1105 struct vpfe_video_device *video = fh->video;
@@ -1115,7 +1114,6 @@ vpfe_buffer_queue_setup(struct vb2_queue *vq,
1115 1114
1116 *nplanes = 1; 1115 *nplanes = 1;
1117 sizes[0] = size; 1116 sizes[0] = size;
1118 alloc_ctxs[0] = video->alloc_ctx;
1119 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, 1117 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev,
1120 "nbuffers=%d, size=%lu\n", *nbuffers, size); 1118 "nbuffers=%d, size=%lu\n", *nbuffers, size);
1121 return 0; 1119 return 0;
@@ -1350,12 +1348,6 @@ static int vpfe_reqbufs(struct file *file, void *priv,
1350 video->memory = req_buf->memory; 1348 video->memory = req_buf->memory;
1351 1349
1352 /* Initialize videobuf2 queue as per the buffer type */ 1350 /* Initialize videobuf2 queue as per the buffer type */
1353 video->alloc_ctx = vb2_dma_contig_init_ctx(vpfe_dev->pdev);
1354 if (IS_ERR(video->alloc_ctx)) {
1355 v4l2_err(&vpfe_dev->v4l2_dev, "Failed to get the context\n");
1356 return PTR_ERR(video->alloc_ctx);
1357 }
1358
1359 q = &video->buffer_queue; 1351 q = &video->buffer_queue;
1360 q->type = req_buf->type; 1352 q->type = req_buf->type;
1361 q->io_modes = VB2_MMAP | VB2_USERPTR; 1353 q->io_modes = VB2_MMAP | VB2_USERPTR;
@@ -1365,11 +1357,11 @@ static int vpfe_reqbufs(struct file *file, void *priv,
1365 q->mem_ops = &vb2_dma_contig_memops; 1357 q->mem_ops = &vb2_dma_contig_memops;
1366 q->buf_struct_size = sizeof(struct vpfe_cap_buffer); 1358 q->buf_struct_size = sizeof(struct vpfe_cap_buffer);
1367 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC; 1359 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1360 q->dev = vpfe_dev->pdev;
1368 1361
1369 ret = vb2_queue_init(q); 1362 ret = vb2_queue_init(q);
1370 if (ret) { 1363 if (ret) {
1371 v4l2_err(&vpfe_dev->v4l2_dev, "vb2_queue_init() failed\n"); 1364 v4l2_err(&vpfe_dev->v4l2_dev, "vb2_queue_init() failed\n");
1372 vb2_dma_contig_cleanup_ctx(vpfe_dev->pdev);
1373 return ret; 1365 return ret;
1374 } 1366 }
1375 1367
diff --git a/drivers/staging/media/davinci_vpfe/vpfe_video.h b/drivers/staging/media/davinci_vpfe/vpfe_video.h
index 653334d537d3..aaec4403df3b 100644
--- a/drivers/staging/media/davinci_vpfe/vpfe_video.h
+++ b/drivers/staging/media/davinci_vpfe/vpfe_video.h
@@ -123,8 +123,6 @@ struct vpfe_video_device {
123 /* Used to store pixel format */ 123 /* Used to store pixel format */
124 struct v4l2_format fmt; 124 struct v4l2_format fmt;
125 struct vb2_queue buffer_queue; 125 struct vb2_queue buffer_queue;
126 /* allocator-specific contexts for each plane */
127 struct vb2_alloc_ctx *alloc_ctx;
128 /* Queue of filled frames */ 126 /* Queue of filled frames */
129 struct list_head dma_queue; 127 struct list_head dma_queue;
130 spinlock_t irqlock; 128 spinlock_t irqlock;
diff --git a/drivers/staging/media/lirc/lirc_parallel.c b/drivers/staging/media/lirc/lirc_parallel.c
index 68ede6c56e6d..3906ac6e686d 100644
--- a/drivers/staging/media/lirc/lirc_parallel.c
+++ b/drivers/staging/media/lirc/lirc_parallel.c
@@ -305,9 +305,9 @@ static void lirc_lirc_irq_handler(void *blah)
305 305
306 /* enable interrupt */ 306 /* enable interrupt */
307 /* 307 /*
308 enable_irq(irq); 308 * enable_irq(irq);
309 out(LIRC_PORT_IRQ, in(LIRC_PORT_IRQ)|LP_PINTEN); 309 * out(LIRC_PORT_IRQ, in(LIRC_PORT_IRQ)|LP_PINTEN);
310 */ 310 */
311} 311}
312 312
313/*** file operations ***/ 313/*** file operations ***/
@@ -620,7 +620,7 @@ static void kf(void *handle)
620 lirc_off(); 620 lirc_off();
621 /* this is a bit annoying when you actually print...*/ 621 /* this is a bit annoying when you actually print...*/
622 /* 622 /*
623 printk(KERN_INFO "%s: reclaimed port\n", LIRC_DRIVER_NAME); 623 * printk(KERN_INFO "%s: reclaimed port\n", LIRC_DRIVER_NAME);
624 */ 624 */
625} 625}
626 626
diff --git a/drivers/staging/media/mn88472/Kconfig b/drivers/staging/media/mn88472/Kconfig
deleted file mode 100644
index a85c90a60bce..000000000000
--- a/drivers/staging/media/mn88472/Kconfig
+++ /dev/null
@@ -1,7 +0,0 @@
1config DVB_MN88472
2 tristate "Panasonic MN88472"
3 depends on DVB_CORE && I2C
4 select REGMAP_I2C
5 default m if !MEDIA_SUBDRV_AUTOSELECT
6 help
7 Say Y when you want to support this frontend.
diff --git a/drivers/staging/media/mn88472/Makefile b/drivers/staging/media/mn88472/Makefile
deleted file mode 100644
index 5987b7e6d82a..000000000000
--- a/drivers/staging/media/mn88472/Makefile
+++ /dev/null
@@ -1,5 +0,0 @@
1obj-$(CONFIG_DVB_MN88472) += mn88472.o
2
3ccflags-y += -Idrivers/media/dvb-core/
4ccflags-y += -Idrivers/media/dvb-frontends/
5ccflags-y += -Idrivers/media/tuners/
diff --git a/drivers/staging/media/mn88472/TODO b/drivers/staging/media/mn88472/TODO
deleted file mode 100644
index b90a14be3beb..000000000000
--- a/drivers/staging/media/mn88472/TODO
+++ /dev/null
@@ -1,21 +0,0 @@
1Driver general quality is not good enough for mainline. Also, other
2device drivers (USB-bridge, tuner) needed for Astrometa receiver in
3question could need some changes. However, if that driver is mainlined
4due to some other device than Astrometa, unrelated TODOs could be
5skipped. In that case rtl28xxu driver needs module parameter to prevent
6driver loading.
7
8Required TODOs:
9* missing lock flags
10* I2C errors
11* tuner sensitivity
12
13*Do not* send any patch fixing checkpatch.pl issues. Currently it passes
14checkpatch.pl tests. I don't want waste my time to review this kind of
15trivial stuff. *Do not* add missing register I/O error checks. Those are
16missing for the reason it is much easier to compare I2C data sniffs when
17there is less lines. Those error checks are about the last thing to be added.
18
19Patches should be submitted to:
20linux-media@vger.kernel.org and Antti Palosaari <crope@iki.fi>
21
diff --git a/drivers/staging/media/mx2/Kconfig b/drivers/staging/media/mx2/Kconfig
deleted file mode 100644
index beaa885cf104..000000000000
--- a/drivers/staging/media/mx2/Kconfig
+++ /dev/null
@@ -1,15 +0,0 @@
1config VIDEO_MX2
2 tristate "i.MX27 Camera Sensor Interface driver"
3 depends on VIDEO_DEV && SOC_CAMERA
4 depends on SOC_IMX27 || COMPILE_TEST
5 depends on HAS_DMA
6 select VIDEOBUF2_DMA_CONTIG
7 ---help---
8 This is a v4l2 driver for the i.MX27 Camera Sensor Interface
9
10 This driver is deprecated: it should become a stand-alone driver
11 instead of using the soc-camera framework.
12
13 Unless someone is willing to take this on (unlikely with such
14 ancient hardware) it is going to be removed from the kernel
15 soon.
diff --git a/drivers/staging/media/mx2/Makefile b/drivers/staging/media/mx2/Makefile
deleted file mode 100644
index fc5b2826a558..000000000000
--- a/drivers/staging/media/mx2/Makefile
+++ /dev/null
@@ -1,3 +0,0 @@
1# Makefile for i.MX27 Camera Sensor driver
2
3obj-$(CONFIG_VIDEO_MX2) += mx2_camera.o
diff --git a/drivers/staging/media/mx2/TODO b/drivers/staging/media/mx2/TODO
deleted file mode 100644
index bc68fa443a3e..000000000000
--- a/drivers/staging/media/mx2/TODO
+++ /dev/null
@@ -1,10 +0,0 @@
1This driver is deprecated: it should become a stand-alone driver instead of
2using the soc-camera framework.
3
4Unless someone is willing to take this on (unlikely with such ancient
5hardware) it is going to be removed from the kernel soon.
6
7Note that trivial patches will not be accepted anymore, only a full conversion.
8
9If you want to convert this driver, please contact the linux-media mailinglist
10(see http://linuxtv.org/lists.php).
diff --git a/drivers/staging/media/mx2/mx2_camera.c b/drivers/staging/media/mx2/mx2_camera.c
deleted file mode 100644
index 48dd5b7851b5..000000000000
--- a/drivers/staging/media/mx2/mx2_camera.c
+++ /dev/null
@@ -1,1636 +0,0 @@
1/*
2 * V4L2 Driver for i.MX27 camera host
3 *
4 * Copyright (C) 2008, Sascha Hauer, Pengutronix
5 * Copyright (C) 2010, Baruch Siach, Orex Computed Radiography
6 * Copyright (C) 2012, Javier Martin, Vista Silicon S.L.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 */
13
14#include <linux/init.h>
15#include <linux/module.h>
16#include <linux/io.h>
17#include <linux/delay.h>
18#include <linux/slab.h>
19#include <linux/dma-mapping.h>
20#include <linux/errno.h>
21#include <linux/fs.h>
22#include <linux/gcd.h>
23#include <linux/interrupt.h>
24#include <linux/kernel.h>
25#include <linux/math64.h>
26#include <linux/mm.h>
27#include <linux/moduleparam.h>
28#include <linux/time.h>
29#include <linux/device.h>
30#include <linux/platform_device.h>
31#include <linux/clk.h>
32
33#include <media/v4l2-common.h>
34#include <media/v4l2-dev.h>
35#include <media/videobuf2-v4l2.h>
36#include <media/videobuf2-dma-contig.h>
37#include <media/soc_camera.h>
38#include <media/drv-intf/soc_mediabus.h>
39
40#include <linux/videodev2.h>
41
42#include <linux/platform_data/media/camera-mx2.h>
43
44#include <asm/dma.h>
45
46#define MX2_CAM_DRV_NAME "mx2-camera"
47#define MX2_CAM_VERSION "0.0.6"
48#define MX2_CAM_DRIVER_DESCRIPTION "i.MX2x_Camera"
49
50/* reset values */
51#define CSICR1_RESET_VAL 0x40000800
52#define CSICR2_RESET_VAL 0x0
53#define CSICR3_RESET_VAL 0x0
54
55/* csi control reg 1 */
56#define CSICR1_SWAP16_EN (1 << 31)
57#define CSICR1_EXT_VSYNC (1 << 30)
58#define CSICR1_EOF_INTEN (1 << 29)
59#define CSICR1_PRP_IF_EN (1 << 28)
60#define CSICR1_CCIR_MODE (1 << 27)
61#define CSICR1_COF_INTEN (1 << 26)
62#define CSICR1_SF_OR_INTEN (1 << 25)
63#define CSICR1_RF_OR_INTEN (1 << 24)
64#define CSICR1_STATFF_LEVEL (3 << 22)
65#define CSICR1_STATFF_INTEN (1 << 21)
66#define CSICR1_RXFF_LEVEL(l) (((l) & 3) << 19)
67#define CSICR1_RXFF_INTEN (1 << 18)
68#define CSICR1_SOF_POL (1 << 17)
69#define CSICR1_SOF_INTEN (1 << 16)
70#define CSICR1_MCLKDIV(d) (((d) & 0xF) << 12)
71#define CSICR1_HSYNC_POL (1 << 11)
72#define CSICR1_CCIR_EN (1 << 10)
73#define CSICR1_MCLKEN (1 << 9)
74#define CSICR1_FCC (1 << 8)
75#define CSICR1_PACK_DIR (1 << 7)
76#define CSICR1_CLR_STATFIFO (1 << 6)
77#define CSICR1_CLR_RXFIFO (1 << 5)
78#define CSICR1_GCLK_MODE (1 << 4)
79#define CSICR1_INV_DATA (1 << 3)
80#define CSICR1_INV_PCLK (1 << 2)
81#define CSICR1_REDGE (1 << 1)
82#define CSICR1_FMT_MASK (CSICR1_PACK_DIR | CSICR1_SWAP16_EN)
83
84#define SHIFT_STATFF_LEVEL 22
85#define SHIFT_RXFF_LEVEL 19
86#define SHIFT_MCLKDIV 12
87
88#define SHIFT_FRMCNT 16
89
90#define CSICR1 0x00
91#define CSICR2 0x04
92#define CSISR 0x08
93#define CSISTATFIFO 0x0c
94#define CSIRFIFO 0x10
95#define CSIRXCNT 0x14
96#define CSICR3 0x1c
97#define CSIDMASA_STATFIFO 0x20
98#define CSIDMATA_STATFIFO 0x24
99#define CSIDMASA_FB1 0x28
100#define CSIDMASA_FB2 0x2c
101#define CSIFBUF_PARA 0x30
102#define CSIIMAG_PARA 0x34
103
104/* EMMA PrP */
105#define PRP_CNTL 0x00
106#define PRP_INTR_CNTL 0x04
107#define PRP_INTRSTATUS 0x08
108#define PRP_SOURCE_Y_PTR 0x0c
109#define PRP_SOURCE_CB_PTR 0x10
110#define PRP_SOURCE_CR_PTR 0x14
111#define PRP_DEST_RGB1_PTR 0x18
112#define PRP_DEST_RGB2_PTR 0x1c
113#define PRP_DEST_Y_PTR 0x20
114#define PRP_DEST_CB_PTR 0x24
115#define PRP_DEST_CR_PTR 0x28
116#define PRP_SRC_FRAME_SIZE 0x2c
117#define PRP_DEST_CH1_LINE_STRIDE 0x30
118#define PRP_SRC_PIXEL_FORMAT_CNTL 0x34
119#define PRP_CH1_PIXEL_FORMAT_CNTL 0x38
120#define PRP_CH1_OUT_IMAGE_SIZE 0x3c
121#define PRP_CH2_OUT_IMAGE_SIZE 0x40
122#define PRP_SRC_LINE_STRIDE 0x44
123#define PRP_CSC_COEF_012 0x48
124#define PRP_CSC_COEF_345 0x4c
125#define PRP_CSC_COEF_678 0x50
126#define PRP_CH1_RZ_HORI_COEF1 0x54
127#define PRP_CH1_RZ_HORI_COEF2 0x58
128#define PRP_CH1_RZ_HORI_VALID 0x5c
129#define PRP_CH1_RZ_VERT_COEF1 0x60
130#define PRP_CH1_RZ_VERT_COEF2 0x64
131#define PRP_CH1_RZ_VERT_VALID 0x68
132#define PRP_CH2_RZ_HORI_COEF1 0x6c
133#define PRP_CH2_RZ_HORI_COEF2 0x70
134#define PRP_CH2_RZ_HORI_VALID 0x74
135#define PRP_CH2_RZ_VERT_COEF1 0x78
136#define PRP_CH2_RZ_VERT_COEF2 0x7c
137#define PRP_CH2_RZ_VERT_VALID 0x80
138
139#define PRP_CNTL_CH1EN (1 << 0)
140#define PRP_CNTL_CH2EN (1 << 1)
141#define PRP_CNTL_CSIEN (1 << 2)
142#define PRP_CNTL_DATA_IN_YUV420 (0 << 3)
143#define PRP_CNTL_DATA_IN_YUV422 (1 << 3)
144#define PRP_CNTL_DATA_IN_RGB16 (2 << 3)
145#define PRP_CNTL_DATA_IN_RGB32 (3 << 3)
146#define PRP_CNTL_CH1_OUT_RGB8 (0 << 5)
147#define PRP_CNTL_CH1_OUT_RGB16 (1 << 5)
148#define PRP_CNTL_CH1_OUT_RGB32 (2 << 5)
149#define PRP_CNTL_CH1_OUT_YUV422 (3 << 5)
150#define PRP_CNTL_CH2_OUT_YUV420 (0 << 7)
151#define PRP_CNTL_CH2_OUT_YUV422 (1 << 7)
152#define PRP_CNTL_CH2_OUT_YUV444 (2 << 7)
153#define PRP_CNTL_CH1_LEN (1 << 9)
154#define PRP_CNTL_CH2_LEN (1 << 10)
155#define PRP_CNTL_SKIP_FRAME (1 << 11)
156#define PRP_CNTL_SWRST (1 << 12)
157#define PRP_CNTL_CLKEN (1 << 13)
158#define PRP_CNTL_WEN (1 << 14)
159#define PRP_CNTL_CH1BYP (1 << 15)
160#define PRP_CNTL_IN_TSKIP(x) ((x) << 16)
161#define PRP_CNTL_CH1_TSKIP(x) ((x) << 19)
162#define PRP_CNTL_CH2_TSKIP(x) ((x) << 22)
163#define PRP_CNTL_INPUT_FIFO_LEVEL(x) ((x) << 25)
164#define PRP_CNTL_RZ_FIFO_LEVEL(x) ((x) << 27)
165#define PRP_CNTL_CH2B1EN (1 << 29)
166#define PRP_CNTL_CH2B2EN (1 << 30)
167#define PRP_CNTL_CH2FEN (1 << 31)
168
169/* IRQ Enable and status register */
170#define PRP_INTR_RDERR (1 << 0)
171#define PRP_INTR_CH1WERR (1 << 1)
172#define PRP_INTR_CH2WERR (1 << 2)
173#define PRP_INTR_CH1FC (1 << 3)
174#define PRP_INTR_CH2FC (1 << 5)
175#define PRP_INTR_LBOVF (1 << 7)
176#define PRP_INTR_CH2OVF (1 << 8)
177
178/* Resizing registers */
179#define PRP_RZ_VALID_TBL_LEN(x) ((x) << 24)
180#define PRP_RZ_VALID_BILINEAR (1 << 31)
181
182#define MAX_VIDEO_MEM 16
183
184#define RESIZE_NUM_MIN 1
185#define RESIZE_NUM_MAX 20
186#define BC_COEF 3
187#define SZ_COEF (1 << BC_COEF)
188
189#define RESIZE_DIR_H 0
190#define RESIZE_DIR_V 1
191
192#define RESIZE_ALGO_BILINEAR 0
193#define RESIZE_ALGO_AVERAGING 1
194
195struct mx2_prp_cfg {
196 int channel;
197 u32 in_fmt;
198 u32 out_fmt;
199 u32 src_pixel;
200 u32 ch1_pixel;
201 u32 irq_flags;
202 u32 csicr1;
203};
204
205/* prp resizing parameters */
206struct emma_prp_resize {
207 int algo; /* type of algorithm used */
208 int len; /* number of coefficients */
209 unsigned char s[RESIZE_NUM_MAX]; /* table of coefficients */
210};
211
212/* prp configuration for a client-host fmt pair */
213struct mx2_fmt_cfg {
214 u32 in_fmt;
215 u32 out_fmt;
216 struct mx2_prp_cfg cfg;
217};
218
219struct mx2_buf_internal {
220 struct list_head queue;
221 int bufnum;
222 bool discard;
223};
224
225/* buffer for one video frame */
226struct mx2_buffer {
227 /* common v4l buffer stuff -- must be first */
228 struct vb2_v4l2_buffer vb;
229 struct mx2_buf_internal internal;
230};
231
232enum mx2_camera_type {
233 IMX27_CAMERA,
234};
235
236struct mx2_camera_dev {
237 struct device *dev;
238 struct soc_camera_host soc_host;
239 struct clk *clk_emma_ahb, *clk_emma_ipg;
240 struct clk *clk_csi_ahb, *clk_csi_per;
241
242 void __iomem *base_csi, *base_emma;
243
244 struct mx2_camera_platform_data *pdata;
245 unsigned long platform_flags;
246
247 struct list_head capture;
248 struct list_head active_bufs;
249 struct list_head discard;
250
251 spinlock_t lock;
252
253 int dma;
254 struct mx2_buffer *active;
255 struct mx2_buffer *fb1_active;
256 struct mx2_buffer *fb2_active;
257
258 u32 csicr1;
259 enum mx2_camera_type devtype;
260
261 struct mx2_buf_internal buf_discard[2];
262 void *discard_buffer;
263 dma_addr_t discard_buffer_dma;
264 size_t discard_size;
265 struct mx2_fmt_cfg *emma_prp;
266 struct emma_prp_resize resizing[2];
267 unsigned int s_width, s_height;
268 u32 frame_count;
269 struct vb2_alloc_ctx *alloc_ctx;
270};
271
272static struct platform_device_id mx2_camera_devtype[] = {
273 {
274 .name = "imx27-camera",
275 .driver_data = IMX27_CAMERA,
276 }, {
277 /* sentinel */
278 }
279};
280MODULE_DEVICE_TABLE(platform, mx2_camera_devtype);
281
282static struct mx2_buffer *mx2_ibuf_to_buf(struct mx2_buf_internal *int_buf)
283{
284 return container_of(int_buf, struct mx2_buffer, internal);
285}
286
287static struct mx2_fmt_cfg mx27_emma_prp_table[] = {
288 /*
289 * This is a generic configuration which is valid for most
290 * prp input-output format combinations.
291 * We set the incoming and outgoing pixelformat to a
292 * 16 Bit wide format and adjust the bytesperline
293 * accordingly. With this configuration the inputdata
294 * will not be changed by the emma and could be any type
295 * of 16 Bit Pixelformat.
296 */
297 {
298 .in_fmt = 0,
299 .out_fmt = 0,
300 .cfg = {
301 .channel = 1,
302 .in_fmt = PRP_CNTL_DATA_IN_RGB16,
303 .out_fmt = PRP_CNTL_CH1_OUT_RGB16,
304 .src_pixel = 0x2ca00565, /* RGB565 */
305 .ch1_pixel = 0x2ca00565, /* RGB565 */
306 .irq_flags = PRP_INTR_RDERR | PRP_INTR_CH1WERR |
307 PRP_INTR_CH1FC | PRP_INTR_LBOVF,
308 .csicr1 = 0,
309 }
310 },
311 {
312 .in_fmt = MEDIA_BUS_FMT_UYVY8_2X8,
313 .out_fmt = V4L2_PIX_FMT_YUYV,
314 .cfg = {
315 .channel = 1,
316 .in_fmt = PRP_CNTL_DATA_IN_YUV422,
317 .out_fmt = PRP_CNTL_CH1_OUT_YUV422,
318 .src_pixel = 0x22000888, /* YUV422 (YUYV) */
319 .ch1_pixel = 0x62000888, /* YUV422 (YUYV) */
320 .irq_flags = PRP_INTR_RDERR | PRP_INTR_CH1WERR |
321 PRP_INTR_CH1FC | PRP_INTR_LBOVF,
322 .csicr1 = CSICR1_SWAP16_EN,
323 }
324 },
325 {
326 .in_fmt = MEDIA_BUS_FMT_YUYV8_2X8,
327 .out_fmt = V4L2_PIX_FMT_YUYV,
328 .cfg = {
329 .channel = 1,
330 .in_fmt = PRP_CNTL_DATA_IN_YUV422,
331 .out_fmt = PRP_CNTL_CH1_OUT_YUV422,
332 .src_pixel = 0x22000888, /* YUV422 (YUYV) */
333 .ch1_pixel = 0x62000888, /* YUV422 (YUYV) */
334 .irq_flags = PRP_INTR_RDERR | PRP_INTR_CH1WERR |
335 PRP_INTR_CH1FC | PRP_INTR_LBOVF,
336 .csicr1 = CSICR1_PACK_DIR,
337 }
338 },
339 {
340 .in_fmt = MEDIA_BUS_FMT_YUYV8_2X8,
341 .out_fmt = V4L2_PIX_FMT_YUV420,
342 .cfg = {
343 .channel = 2,
344 .in_fmt = PRP_CNTL_DATA_IN_YUV422,
345 .out_fmt = PRP_CNTL_CH2_OUT_YUV420,
346 .src_pixel = 0x22000888, /* YUV422 (YUYV) */
347 .irq_flags = PRP_INTR_RDERR | PRP_INTR_CH2WERR |
348 PRP_INTR_CH2FC | PRP_INTR_LBOVF |
349 PRP_INTR_CH2OVF,
350 .csicr1 = CSICR1_PACK_DIR,
351 }
352 },
353 {
354 .in_fmt = MEDIA_BUS_FMT_UYVY8_2X8,
355 .out_fmt = V4L2_PIX_FMT_YUV420,
356 .cfg = {
357 .channel = 2,
358 .in_fmt = PRP_CNTL_DATA_IN_YUV422,
359 .out_fmt = PRP_CNTL_CH2_OUT_YUV420,
360 .src_pixel = 0x22000888, /* YUV422 (YUYV) */
361 .irq_flags = PRP_INTR_RDERR | PRP_INTR_CH2WERR |
362 PRP_INTR_CH2FC | PRP_INTR_LBOVF |
363 PRP_INTR_CH2OVF,
364 .csicr1 = CSICR1_SWAP16_EN,
365 }
366 },
367};
368
369static struct mx2_fmt_cfg *mx27_emma_prp_get_format(u32 in_fmt, u32 out_fmt)
370{
371 int i;
372
373 for (i = 1; i < ARRAY_SIZE(mx27_emma_prp_table); i++)
374 if ((mx27_emma_prp_table[i].in_fmt == in_fmt) &&
375 (mx27_emma_prp_table[i].out_fmt == out_fmt)) {
376 return &mx27_emma_prp_table[i];
377 }
378 /* If no match return the most generic configuration */
379 return &mx27_emma_prp_table[0];
380};
381
382static void mx27_update_emma_buf(struct mx2_camera_dev *pcdev,
383 unsigned long phys, int bufnum)
384{
385 struct mx2_fmt_cfg *prp = pcdev->emma_prp;
386
387 if (prp->cfg.channel == 1) {
388 writel(phys, pcdev->base_emma +
389 PRP_DEST_RGB1_PTR + 4 * bufnum);
390 } else {
391 writel(phys, pcdev->base_emma +
392 PRP_DEST_Y_PTR - 0x14 * bufnum);
393 if (prp->out_fmt == V4L2_PIX_FMT_YUV420) {
394 u32 imgsize = pcdev->soc_host.icd->user_height *
395 pcdev->soc_host.icd->user_width;
396
397 writel(phys + imgsize, pcdev->base_emma +
398 PRP_DEST_CB_PTR - 0x14 * bufnum);
399 writel(phys + ((5 * imgsize) / 4), pcdev->base_emma +
400 PRP_DEST_CR_PTR - 0x14 * bufnum);
401 }
402 }
403}
404
405static void mx2_camera_deactivate(struct mx2_camera_dev *pcdev)
406{
407 clk_disable_unprepare(pcdev->clk_csi_ahb);
408 clk_disable_unprepare(pcdev->clk_csi_per);
409 writel(0, pcdev->base_csi + CSICR1);
410 writel(0, pcdev->base_emma + PRP_CNTL);
411}
412
413static int mx2_camera_add_device(struct soc_camera_device *icd)
414{
415 dev_info(icd->parent, "Camera driver attached to camera %d\n",
416 icd->devnum);
417
418 return 0;
419}
420
421static void mx2_camera_remove_device(struct soc_camera_device *icd)
422{
423 dev_info(icd->parent, "Camera driver detached from camera %d\n",
424 icd->devnum);
425}
426
427/*
428 * The following two functions absolutely depend on the fact, that
429 * there can be only one camera on mx2 camera sensor interface
430 */
431static int mx2_camera_clock_start(struct soc_camera_host *ici)
432{
433 struct mx2_camera_dev *pcdev = ici->priv;
434 int ret;
435 u32 csicr1;
436
437 ret = clk_prepare_enable(pcdev->clk_csi_ahb);
438 if (ret < 0)
439 return ret;
440
441 ret = clk_prepare_enable(pcdev->clk_csi_per);
442 if (ret < 0)
443 goto exit_csi_ahb;
444
445 csicr1 = CSICR1_MCLKEN | CSICR1_PRP_IF_EN | CSICR1_FCC |
446 CSICR1_RXFF_LEVEL(0);
447
448 pcdev->csicr1 = csicr1;
449 writel(pcdev->csicr1, pcdev->base_csi + CSICR1);
450
451 pcdev->frame_count = 0;
452
453 return 0;
454
455exit_csi_ahb:
456 clk_disable_unprepare(pcdev->clk_csi_ahb);
457
458 return ret;
459}
460
461static void mx2_camera_clock_stop(struct soc_camera_host *ici)
462{
463 struct mx2_camera_dev *pcdev = ici->priv;
464
465 mx2_camera_deactivate(pcdev);
466}
467
468/*
469 * Videobuf operations
470 */
471static int mx2_videobuf_setup(struct vb2_queue *vq,
472 unsigned int *count, unsigned int *num_planes,
473 unsigned int sizes[], void *alloc_ctxs[])
474{
475 struct soc_camera_device *icd = soc_camera_from_vb2q(vq);
476 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
477 struct mx2_camera_dev *pcdev = ici->priv;
478
479 dev_dbg(icd->parent, "count=%d, size=%d\n", *count, sizes[0]);
480
481 alloc_ctxs[0] = pcdev->alloc_ctx;
482
483 sizes[0] = icd->sizeimage;
484
485 if (0 == *count)
486 *count = 32;
487 if (!*num_planes &&
488 sizes[0] * *count > MAX_VIDEO_MEM * 1024 * 1024)
489 *count = (MAX_VIDEO_MEM * 1024 * 1024) / sizes[0];
490
491 *num_planes = 1;
492
493 return 0;
494}
495
496static int mx2_videobuf_prepare(struct vb2_buffer *vb)
497{
498 struct soc_camera_device *icd = soc_camera_from_vb2q(vb->vb2_queue);
499 int ret = 0;
500
501 dev_dbg(icd->parent, "%s (vb=0x%p) 0x%p %lu\n", __func__,
502 vb, vb2_plane_vaddr(vb, 0), vb2_get_plane_payload(vb, 0));
503
504#ifdef DEBUG
505 /*
506 * This can be useful if you want to see if we actually fill
507 * the buffer with something
508 */
509 memset((void *)vb2_plane_vaddr(vb, 0),
510 0xaa, vb2_get_plane_payload(vb, 0));
511#endif
512
513 vb2_set_plane_payload(vb, 0, icd->sizeimage);
514 if (vb2_plane_vaddr(vb, 0) &&
515 vb2_get_plane_payload(vb, 0) > vb2_plane_size(vb, 0)) {
516 ret = -EINVAL;
517 goto out;
518 }
519
520 return 0;
521
522out:
523 return ret;
524}
525
526static void mx2_videobuf_queue(struct vb2_buffer *vb)
527{
528 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
529 struct soc_camera_device *icd = soc_camera_from_vb2q(vb->vb2_queue);
530 struct soc_camera_host *ici =
531 to_soc_camera_host(icd->parent);
532 struct mx2_camera_dev *pcdev = ici->priv;
533 struct mx2_buffer *buf = container_of(vbuf, struct mx2_buffer, vb);
534 unsigned long flags;
535
536 dev_dbg(icd->parent, "%s (vb=0x%p) 0x%p %lu\n", __func__,
537 vb, vb2_plane_vaddr(vb, 0), vb2_get_plane_payload(vb, 0));
538
539 spin_lock_irqsave(&pcdev->lock, flags);
540
541 list_add_tail(&buf->internal.queue, &pcdev->capture);
542
543 spin_unlock_irqrestore(&pcdev->lock, flags);
544}
545
546static void mx27_camera_emma_buf_init(struct soc_camera_device *icd,
547 int bytesperline)
548{
549 struct soc_camera_host *ici =
550 to_soc_camera_host(icd->parent);
551 struct mx2_camera_dev *pcdev = ici->priv;
552 struct mx2_fmt_cfg *prp = pcdev->emma_prp;
553
554 writel((pcdev->s_width << 16) | pcdev->s_height,
555 pcdev->base_emma + PRP_SRC_FRAME_SIZE);
556 writel(prp->cfg.src_pixel,
557 pcdev->base_emma + PRP_SRC_PIXEL_FORMAT_CNTL);
558 if (prp->cfg.channel == 1) {
559 writel((icd->user_width << 16) | icd->user_height,
560 pcdev->base_emma + PRP_CH1_OUT_IMAGE_SIZE);
561 writel(bytesperline,
562 pcdev->base_emma + PRP_DEST_CH1_LINE_STRIDE);
563 writel(prp->cfg.ch1_pixel,
564 pcdev->base_emma + PRP_CH1_PIXEL_FORMAT_CNTL);
565 } else { /* channel 2 */
566 writel((icd->user_width << 16) | icd->user_height,
567 pcdev->base_emma + PRP_CH2_OUT_IMAGE_SIZE);
568 }
569
570 /* Enable interrupts */
571 writel(prp->cfg.irq_flags, pcdev->base_emma + PRP_INTR_CNTL);
572}
573
574static void mx2_prp_resize_commit(struct mx2_camera_dev *pcdev)
575{
576 int dir;
577
578 for (dir = RESIZE_DIR_H; dir <= RESIZE_DIR_V; dir++) {
579 unsigned char *s = pcdev->resizing[dir].s;
580 int len = pcdev->resizing[dir].len;
581 unsigned int coeff[2] = {0, 0};
582 unsigned int valid = 0;
583 int i;
584
585 if (len == 0)
586 continue;
587
588 for (i = RESIZE_NUM_MAX - 1; i >= 0; i--) {
589 int j;
590
591 j = i > 9 ? 1 : 0;
592 coeff[j] = (coeff[j] << BC_COEF) |
593 (s[i] & (SZ_COEF - 1));
594
595 if (i == 5 || i == 15)
596 coeff[j] <<= 1;
597
598 valid = (valid << 1) | (s[i] >> BC_COEF);
599 }
600
601 valid |= PRP_RZ_VALID_TBL_LEN(len);
602
603 if (pcdev->resizing[dir].algo == RESIZE_ALGO_BILINEAR)
604 valid |= PRP_RZ_VALID_BILINEAR;
605
606 if (pcdev->emma_prp->cfg.channel == 1) {
607 if (dir == RESIZE_DIR_H) {
608 writel(coeff[0], pcdev->base_emma +
609 PRP_CH1_RZ_HORI_COEF1);
610 writel(coeff[1], pcdev->base_emma +
611 PRP_CH1_RZ_HORI_COEF2);
612 writel(valid, pcdev->base_emma +
613 PRP_CH1_RZ_HORI_VALID);
614 } else {
615 writel(coeff[0], pcdev->base_emma +
616 PRP_CH1_RZ_VERT_COEF1);
617 writel(coeff[1], pcdev->base_emma +
618 PRP_CH1_RZ_VERT_COEF2);
619 writel(valid, pcdev->base_emma +
620 PRP_CH1_RZ_VERT_VALID);
621 }
622 } else {
623 if (dir == RESIZE_DIR_H) {
624 writel(coeff[0], pcdev->base_emma +
625 PRP_CH2_RZ_HORI_COEF1);
626 writel(coeff[1], pcdev->base_emma +
627 PRP_CH2_RZ_HORI_COEF2);
628 writel(valid, pcdev->base_emma +
629 PRP_CH2_RZ_HORI_VALID);
630 } else {
631 writel(coeff[0], pcdev->base_emma +
632 PRP_CH2_RZ_VERT_COEF1);
633 writel(coeff[1], pcdev->base_emma +
634 PRP_CH2_RZ_VERT_COEF2);
635 writel(valid, pcdev->base_emma +
636 PRP_CH2_RZ_VERT_VALID);
637 }
638 }
639 }
640}
641
642static int mx2_start_streaming(struct vb2_queue *q, unsigned int count)
643{
644 struct soc_camera_device *icd = soc_camera_from_vb2q(q);
645 struct soc_camera_host *ici =
646 to_soc_camera_host(icd->parent);
647 struct mx2_camera_dev *pcdev = ici->priv;
648 struct mx2_fmt_cfg *prp = pcdev->emma_prp;
649 struct vb2_buffer *vb;
650 struct mx2_buffer *buf;
651 unsigned long phys;
652 int bytesperline;
653 unsigned long flags;
654
655 if (count < 2)
656 return -ENOBUFS;
657
658 spin_lock_irqsave(&pcdev->lock, flags);
659
660 buf = list_first_entry(&pcdev->capture, struct mx2_buffer,
661 internal.queue);
662 buf->internal.bufnum = 0;
663 vb = &buf->vb.vb2_buf;
664
665 phys = vb2_dma_contig_plane_dma_addr(vb, 0);
666 mx27_update_emma_buf(pcdev, phys, buf->internal.bufnum);
667 list_move_tail(pcdev->capture.next, &pcdev->active_bufs);
668
669 buf = list_first_entry(&pcdev->capture, struct mx2_buffer,
670 internal.queue);
671 buf->internal.bufnum = 1;
672 vb = &buf->vb.vb2_buf;
673
674 phys = vb2_dma_contig_plane_dma_addr(vb, 0);
675 mx27_update_emma_buf(pcdev, phys, buf->internal.bufnum);
676 list_move_tail(pcdev->capture.next, &pcdev->active_bufs);
677
678 bytesperline = soc_mbus_bytes_per_line(icd->user_width,
679 icd->current_fmt->host_fmt);
680 if (bytesperline < 0) {
681 spin_unlock_irqrestore(&pcdev->lock, flags);
682 return bytesperline;
683 }
684
685 /*
686 * I didn't manage to properly enable/disable the prp
687 * on a per frame basis during running transfers,
688 * thus we allocate a buffer here and use it to
689 * discard frames when no buffer is available.
690 * Feel free to work on this ;)
691 */
692 pcdev->discard_size = icd->user_height * bytesperline;
693 pcdev->discard_buffer = dma_alloc_coherent(ici->v4l2_dev.dev,
694 pcdev->discard_size,
695 &pcdev->discard_buffer_dma, GFP_ATOMIC);
696 if (!pcdev->discard_buffer) {
697 spin_unlock_irqrestore(&pcdev->lock, flags);
698 return -ENOMEM;
699 }
700
701 pcdev->buf_discard[0].discard = true;
702 list_add_tail(&pcdev->buf_discard[0].queue,
703 &pcdev->discard);
704
705 pcdev->buf_discard[1].discard = true;
706 list_add_tail(&pcdev->buf_discard[1].queue,
707 &pcdev->discard);
708
709 mx2_prp_resize_commit(pcdev);
710
711 mx27_camera_emma_buf_init(icd, bytesperline);
712
713 if (prp->cfg.channel == 1) {
714 writel(PRP_CNTL_CH1EN |
715 PRP_CNTL_CSIEN |
716 prp->cfg.in_fmt |
717 prp->cfg.out_fmt |
718 PRP_CNTL_CH1_LEN |
719 PRP_CNTL_CH1BYP |
720 PRP_CNTL_CH1_TSKIP(0) |
721 PRP_CNTL_IN_TSKIP(0),
722 pcdev->base_emma + PRP_CNTL);
723 } else {
724 writel(PRP_CNTL_CH2EN |
725 PRP_CNTL_CSIEN |
726 prp->cfg.in_fmt |
727 prp->cfg.out_fmt |
728 PRP_CNTL_CH2_LEN |
729 PRP_CNTL_CH2_TSKIP(0) |
730 PRP_CNTL_IN_TSKIP(0),
731 pcdev->base_emma + PRP_CNTL);
732 }
733 spin_unlock_irqrestore(&pcdev->lock, flags);
734
735 return 0;
736}
737
738static void mx2_stop_streaming(struct vb2_queue *q)
739{
740 struct soc_camera_device *icd = soc_camera_from_vb2q(q);
741 struct soc_camera_host *ici =
742 to_soc_camera_host(icd->parent);
743 struct mx2_camera_dev *pcdev = ici->priv;
744 struct mx2_fmt_cfg *prp = pcdev->emma_prp;
745 unsigned long flags;
746 void *b;
747 u32 cntl;
748
749 spin_lock_irqsave(&pcdev->lock, flags);
750
751 cntl = readl(pcdev->base_emma + PRP_CNTL);
752 if (prp->cfg.channel == 1) {
753 writel(cntl & ~PRP_CNTL_CH1EN,
754 pcdev->base_emma + PRP_CNTL);
755 } else {
756 writel(cntl & ~PRP_CNTL_CH2EN,
757 pcdev->base_emma + PRP_CNTL);
758 }
759 INIT_LIST_HEAD(&pcdev->capture);
760 INIT_LIST_HEAD(&pcdev->active_bufs);
761 INIT_LIST_HEAD(&pcdev->discard);
762
763 b = pcdev->discard_buffer;
764 pcdev->discard_buffer = NULL;
765
766 spin_unlock_irqrestore(&pcdev->lock, flags);
767
768 dma_free_coherent(ici->v4l2_dev.dev,
769 pcdev->discard_size, b, pcdev->discard_buffer_dma);
770}
771
772static struct vb2_ops mx2_videobuf_ops = {
773 .queue_setup = mx2_videobuf_setup,
774 .buf_prepare = mx2_videobuf_prepare,
775 .buf_queue = mx2_videobuf_queue,
776 .start_streaming = mx2_start_streaming,
777 .stop_streaming = mx2_stop_streaming,
778};
779
780static int mx2_camera_init_videobuf(struct vb2_queue *q,
781 struct soc_camera_device *icd)
782{
783 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
784 q->io_modes = VB2_MMAP | VB2_USERPTR;
785 q->drv_priv = icd;
786 q->ops = &mx2_videobuf_ops;
787 q->mem_ops = &vb2_dma_contig_memops;
788 q->buf_struct_size = sizeof(struct mx2_buffer);
789 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
790
791 return vb2_queue_init(q);
792}
793
794#define MX2_BUS_FLAGS (V4L2_MBUS_MASTER | \
795 V4L2_MBUS_VSYNC_ACTIVE_HIGH | \
796 V4L2_MBUS_VSYNC_ACTIVE_LOW | \
797 V4L2_MBUS_HSYNC_ACTIVE_HIGH | \
798 V4L2_MBUS_HSYNC_ACTIVE_LOW | \
799 V4L2_MBUS_PCLK_SAMPLE_RISING | \
800 V4L2_MBUS_PCLK_SAMPLE_FALLING | \
801 V4L2_MBUS_DATA_ACTIVE_HIGH | \
802 V4L2_MBUS_DATA_ACTIVE_LOW)
803
804static int mx27_camera_emma_prp_reset(struct mx2_camera_dev *pcdev)
805{
806 int count = 0;
807
808 readl(pcdev->base_emma + PRP_CNTL);
809 writel(PRP_CNTL_SWRST, pcdev->base_emma + PRP_CNTL);
810 while (count++ < 100) {
811 if (!(readl(pcdev->base_emma + PRP_CNTL) & PRP_CNTL_SWRST))
812 return 0;
813 barrier();
814 udelay(1);
815 }
816
817 return -ETIMEDOUT;
818}
819
820static int mx2_camera_set_bus_param(struct soc_camera_device *icd)
821{
822 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
823 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
824 struct mx2_camera_dev *pcdev = ici->priv;
825 struct v4l2_mbus_config cfg = {.type = V4L2_MBUS_PARALLEL,};
826 unsigned long common_flags;
827 int ret;
828 int bytesperline;
829 u32 csicr1 = pcdev->csicr1;
830
831 ret = v4l2_subdev_call(sd, video, g_mbus_config, &cfg);
832 if (!ret) {
833 common_flags = soc_mbus_config_compatible(&cfg, MX2_BUS_FLAGS);
834 if (!common_flags) {
835 dev_warn(icd->parent,
836 "Flags incompatible: camera 0x%x, host 0x%x\n",
837 cfg.flags, MX2_BUS_FLAGS);
838 return -EINVAL;
839 }
840 } else if (ret != -ENOIOCTLCMD) {
841 return ret;
842 } else {
843 common_flags = MX2_BUS_FLAGS;
844 }
845
846 if ((common_flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH) &&
847 (common_flags & V4L2_MBUS_HSYNC_ACTIVE_LOW)) {
848 if (pcdev->platform_flags & MX2_CAMERA_HSYNC_HIGH)
849 common_flags &= ~V4L2_MBUS_HSYNC_ACTIVE_LOW;
850 else
851 common_flags &= ~V4L2_MBUS_HSYNC_ACTIVE_HIGH;
852 }
853
854 if ((common_flags & V4L2_MBUS_PCLK_SAMPLE_RISING) &&
855 (common_flags & V4L2_MBUS_PCLK_SAMPLE_FALLING)) {
856 if (pcdev->platform_flags & MX2_CAMERA_PCLK_SAMPLE_RISING)
857 common_flags &= ~V4L2_MBUS_PCLK_SAMPLE_FALLING;
858 else
859 common_flags &= ~V4L2_MBUS_PCLK_SAMPLE_RISING;
860 }
861
862 cfg.flags = common_flags;
863 ret = v4l2_subdev_call(sd, video, s_mbus_config, &cfg);
864 if (ret < 0 && ret != -ENOIOCTLCMD) {
865 dev_dbg(icd->parent, "camera s_mbus_config(0x%lx) returned %d\n",
866 common_flags, ret);
867 return ret;
868 }
869
870 csicr1 = (csicr1 & ~CSICR1_FMT_MASK) | pcdev->emma_prp->cfg.csicr1;
871
872 if (common_flags & V4L2_MBUS_PCLK_SAMPLE_RISING)
873 csicr1 |= CSICR1_REDGE;
874 if (common_flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH)
875 csicr1 |= CSICR1_SOF_POL;
876 if (common_flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH)
877 csicr1 |= CSICR1_HSYNC_POL;
878 if (pcdev->platform_flags & MX2_CAMERA_EXT_VSYNC)
879 csicr1 |= CSICR1_EXT_VSYNC;
880 if (pcdev->platform_flags & MX2_CAMERA_CCIR)
881 csicr1 |= CSICR1_CCIR_EN;
882 if (pcdev->platform_flags & MX2_CAMERA_CCIR_INTERLACE)
883 csicr1 |= CSICR1_CCIR_MODE;
884 if (pcdev->platform_flags & MX2_CAMERA_GATED_CLOCK)
885 csicr1 |= CSICR1_GCLK_MODE;
886 if (pcdev->platform_flags & MX2_CAMERA_INV_DATA)
887 csicr1 |= CSICR1_INV_DATA;
888
889 pcdev->csicr1 = csicr1;
890
891 bytesperline = soc_mbus_bytes_per_line(icd->user_width,
892 icd->current_fmt->host_fmt);
893 if (bytesperline < 0)
894 return bytesperline;
895
896 ret = mx27_camera_emma_prp_reset(pcdev);
897 if (ret)
898 return ret;
899
900 writel(pcdev->csicr1, pcdev->base_csi + CSICR1);
901
902 return 0;
903}
904
905static int mx2_camera_set_crop(struct soc_camera_device *icd,
906 const struct v4l2_crop *a)
907{
908 struct v4l2_crop a_writable = *a;
909 struct v4l2_rect *rect = &a_writable.c;
910 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
911 struct v4l2_subdev_format fmt = {
912 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
913 };
914 struct v4l2_mbus_framefmt *mf = &fmt.format;
915 int ret;
916
917 soc_camera_limit_side(&rect->left, &rect->width, 0, 2, 4096);
918 soc_camera_limit_side(&rect->top, &rect->height, 0, 2, 4096);
919
920 ret = v4l2_subdev_call(sd, video, s_crop, a);
921 if (ret < 0)
922 return ret;
923
924 /* The capture device might have changed its output */
925 ret = v4l2_subdev_call(sd, pad, get_fmt, NULL, &fmt);
926 if (ret < 0)
927 return ret;
928
929 dev_dbg(icd->parent, "Sensor cropped %dx%d\n",
930 mf->width, mf->height);
931
932 icd->user_width = mf->width;
933 icd->user_height = mf->height;
934
935 return ret;
936}
937
938static int mx2_camera_get_formats(struct soc_camera_device *icd,
939 unsigned int idx,
940 struct soc_camera_format_xlate *xlate)
941{
942 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
943 const struct soc_mbus_pixelfmt *fmt;
944 struct device *dev = icd->parent;
945 struct v4l2_subdev_mbus_code_enum code = {
946 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
947 .index = idx,
948 };
949 int ret, formats = 0;
950
951 ret = v4l2_subdev_call(sd, pad, enum_mbus_code, NULL, &code);
952 if (ret < 0)
953 /* no more formats */
954 return 0;
955
956 fmt = soc_mbus_get_fmtdesc(code.code);
957 if (!fmt) {
958 dev_err(dev, "Invalid format code #%u: %d\n", idx, code.code);
959 return 0;
960 }
961
962 if (code.code == MEDIA_BUS_FMT_YUYV8_2X8 ||
963 code.code == MEDIA_BUS_FMT_UYVY8_2X8) {
964 formats++;
965 if (xlate) {
966 /*
967 * CH2 can output YUV420 which is a standard format in
968 * soc_mediabus.c
969 */
970 xlate->host_fmt =
971 soc_mbus_get_fmtdesc(MEDIA_BUS_FMT_YUYV8_1_5X8);
972 xlate->code = code.code;
973 dev_dbg(dev, "Providing host format %s for sensor code %d\n",
974 xlate->host_fmt->name, code.code);
975 xlate++;
976 }
977 }
978
979 if (code.code == MEDIA_BUS_FMT_UYVY8_2X8) {
980 formats++;
981 if (xlate) {
982 xlate->host_fmt =
983 soc_mbus_get_fmtdesc(MEDIA_BUS_FMT_YUYV8_2X8);
984 xlate->code = code.code;
985 dev_dbg(dev, "Providing host format %s for sensor code %d\n",
986 xlate->host_fmt->name, code.code);
987 xlate++;
988 }
989 }
990
991 /* Generic pass-trough */
992 formats++;
993 if (xlate) {
994 xlate->host_fmt = fmt;
995 xlate->code = code.code;
996 xlate++;
997 }
998 return formats;
999}
1000
1001static int mx2_emmaprp_resize(struct mx2_camera_dev *pcdev,
1002 struct v4l2_mbus_framefmt *mf_in,
1003 struct v4l2_pix_format *pix_out, bool apply)
1004{
1005 unsigned int num, den;
1006 unsigned long m;
1007 int i, dir;
1008
1009 for (dir = RESIZE_DIR_H; dir <= RESIZE_DIR_V; dir++) {
1010 struct emma_prp_resize tmprsz;
1011 unsigned char *s = tmprsz.s;
1012 int len = 0;
1013 int in, out;
1014
1015 if (dir == RESIZE_DIR_H) {
1016 in = mf_in->width;
1017 out = pix_out->width;
1018 } else {
1019 in = mf_in->height;
1020 out = pix_out->height;
1021 }
1022
1023 if (in < out)
1024 return -EINVAL;
1025 else if (in == out)
1026 continue;
1027
1028 /* Calculate ratio */
1029 m = gcd(in, out);
1030 num = in / m;
1031 den = out / m;
1032 if (num > RESIZE_NUM_MAX)
1033 return -EINVAL;
1034
1035 if ((num >= 2 * den) && (den == 1) &&
1036 (num < 9) && (!(num & 0x01))) {
1037 int sum = 0;
1038 int j;
1039
1040 /* Average scaling for >= 2:1 ratios */
1041 /* Support can be added for num >=9 and odd values */
1042
1043 tmprsz.algo = RESIZE_ALGO_AVERAGING;
1044 len = num;
1045
1046 for (i = 0; i < (len / 2); i++)
1047 s[i] = 8;
1048
1049 do {
1050 for (i = 0; i < (len / 2); i++) {
1051 s[i] = s[i] >> 1;
1052 sum = 0;
1053 for (j = 0; j < (len / 2); j++)
1054 sum += s[j];
1055 if (sum == 4)
1056 break;
1057 }
1058 } while (sum != 4);
1059
1060 for (i = (len / 2); i < len; i++)
1061 s[i] = s[len - i - 1];
1062
1063 s[len - 1] |= SZ_COEF;
1064 } else {
1065 /* bilinear scaling for < 2:1 ratios */
1066 int v; /* overflow counter */
1067 int coeff, nxt; /* table output */
1068 int in_pos_inc = 2 * den;
1069 int out_pos = num;
1070 int out_pos_inc = 2 * num;
1071 int init_carry = num - den;
1072 int carry = init_carry;
1073
1074 tmprsz.algo = RESIZE_ALGO_BILINEAR;
1075 v = den + in_pos_inc;
1076 do {
1077 coeff = v - out_pos;
1078 out_pos += out_pos_inc;
1079 carry += out_pos_inc;
1080 for (nxt = 0; v < out_pos; nxt++) {
1081 v += in_pos_inc;
1082 carry -= in_pos_inc;
1083 }
1084
1085 if (len > RESIZE_NUM_MAX)
1086 return -EINVAL;
1087
1088 coeff = ((coeff << BC_COEF) +
1089 (in_pos_inc >> 1)) / in_pos_inc;
1090
1091 if (coeff >= (SZ_COEF - 1))
1092 coeff--;
1093
1094 coeff |= SZ_COEF;
1095 s[len] = (unsigned char)coeff;
1096 len++;
1097
1098 for (i = 1; i < nxt; i++) {
1099 if (len >= RESIZE_NUM_MAX)
1100 return -EINVAL;
1101 s[len] = 0;
1102 len++;
1103 }
1104 } while (carry != init_carry);
1105 }
1106 tmprsz.len = len;
1107 if (dir == RESIZE_DIR_H)
1108 mf_in->width = pix_out->width;
1109 else
1110 mf_in->height = pix_out->height;
1111
1112 if (apply)
1113 memcpy(&pcdev->resizing[dir], &tmprsz, sizeof(tmprsz));
1114 }
1115 return 0;
1116}
1117
1118static int mx2_camera_set_fmt(struct soc_camera_device *icd,
1119 struct v4l2_format *f)
1120{
1121 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
1122 struct mx2_camera_dev *pcdev = ici->priv;
1123 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1124 const struct soc_camera_format_xlate *xlate;
1125 struct v4l2_pix_format *pix = &f->fmt.pix;
1126 struct v4l2_subdev_format format = {
1127 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
1128 };
1129 struct v4l2_mbus_framefmt *mf = &format.format;
1130 int ret;
1131
1132 dev_dbg(icd->parent, "%s: requested params: width = %d, height = %d\n",
1133 __func__, pix->width, pix->height);
1134
1135 xlate = soc_camera_xlate_by_fourcc(icd, pix->pixelformat);
1136 if (!xlate) {
1137 dev_warn(icd->parent, "Format %x not found\n",
1138 pix->pixelformat);
1139 return -EINVAL;
1140 }
1141
1142 mf->width = pix->width;
1143 mf->height = pix->height;
1144 mf->field = pix->field;
1145 mf->colorspace = pix->colorspace;
1146 mf->code = xlate->code;
1147
1148 ret = v4l2_subdev_call(sd, pad, set_fmt, NULL, &format);
1149 if (ret < 0 && ret != -ENOIOCTLCMD)
1150 return ret;
1151
1152 /* Store width and height returned by the sensor for resizing */
1153 pcdev->s_width = mf->width;
1154 pcdev->s_height = mf->height;
1155 dev_dbg(icd->parent, "%s: sensor params: width = %d, height = %d\n",
1156 __func__, pcdev->s_width, pcdev->s_height);
1157
1158 pcdev->emma_prp = mx27_emma_prp_get_format(xlate->code,
1159 xlate->host_fmt->fourcc);
1160
1161 memset(pcdev->resizing, 0, sizeof(pcdev->resizing));
1162 if ((mf->width != pix->width || mf->height != pix->height) &&
1163 pcdev->emma_prp->cfg.in_fmt == PRP_CNTL_DATA_IN_YUV422) {
1164 if (mx2_emmaprp_resize(pcdev, mf, pix, true) < 0)
1165 dev_dbg(icd->parent, "%s: can't resize\n", __func__);
1166 }
1167
1168 if (mf->code != xlate->code)
1169 return -EINVAL;
1170
1171 pix->width = mf->width;
1172 pix->height = mf->height;
1173 pix->field = mf->field;
1174 pix->colorspace = mf->colorspace;
1175 icd->current_fmt = xlate;
1176
1177 dev_dbg(icd->parent, "%s: returned params: width = %d, height = %d\n",
1178 __func__, pix->width, pix->height);
1179
1180 return 0;
1181}
1182
1183static int mx2_camera_try_fmt(struct soc_camera_device *icd,
1184 struct v4l2_format *f)
1185{
1186 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1187 const struct soc_camera_format_xlate *xlate;
1188 struct v4l2_pix_format *pix = &f->fmt.pix;
1189 struct v4l2_subdev_pad_config pad_cfg;
1190 struct v4l2_subdev_format format = {
1191 .which = V4L2_SUBDEV_FORMAT_TRY,
1192 };
1193 struct v4l2_mbus_framefmt *mf = &format.format;
1194 __u32 pixfmt = pix->pixelformat;
1195 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
1196 struct mx2_camera_dev *pcdev = ici->priv;
1197 struct mx2_fmt_cfg *emma_prp;
1198 int ret;
1199
1200 dev_dbg(icd->parent, "%s: requested params: width = %d, height = %d\n",
1201 __func__, pix->width, pix->height);
1202
1203 xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
1204 if (pixfmt && !xlate) {
1205 dev_warn(icd->parent, "Format %x not found\n", pixfmt);
1206 return -EINVAL;
1207 }
1208
1209 /*
1210 * limit to MX27 hardware capabilities: width must be a multiple of 8 as
1211 * requested by the CSI. (Table 39-2 in the i.MX27 Reference Manual).
1212 */
1213 pix->width &= ~0x7;
1214
1215 /* limit to sensor capabilities */
1216 mf->width = pix->width;
1217 mf->height = pix->height;
1218 mf->field = pix->field;
1219 mf->colorspace = pix->colorspace;
1220 mf->code = xlate->code;
1221
1222 ret = v4l2_subdev_call(sd, pad, set_fmt, &pad_cfg, &format);
1223 if (ret < 0)
1224 return ret;
1225
1226 dev_dbg(icd->parent, "%s: sensor params: width = %d, height = %d\n",
1227 __func__, pcdev->s_width, pcdev->s_height);
1228
1229 /* If the sensor does not support image size try PrP resizing */
1230 emma_prp = mx27_emma_prp_get_format(xlate->code,
1231 xlate->host_fmt->fourcc);
1232
1233 if ((mf->width != pix->width || mf->height != pix->height) &&
1234 emma_prp->cfg.in_fmt == PRP_CNTL_DATA_IN_YUV422) {
1235 if (mx2_emmaprp_resize(pcdev, mf, pix, false) < 0)
1236 dev_dbg(icd->parent, "%s: can't resize\n", __func__);
1237 }
1238
1239 if (mf->field == V4L2_FIELD_ANY)
1240 mf->field = V4L2_FIELD_NONE;
1241 /*
1242 * Driver supports interlaced images provided they have
1243 * both fields so that they can be processed as if they
1244 * were progressive.
1245 */
1246 if (mf->field != V4L2_FIELD_NONE && !V4L2_FIELD_HAS_BOTH(mf->field)) {
1247 dev_err(icd->parent, "Field type %d unsupported.\n",
1248 mf->field);
1249 return -EINVAL;
1250 }
1251
1252 pix->width = mf->width;
1253 pix->height = mf->height;
1254 pix->field = mf->field;
1255 pix->colorspace = mf->colorspace;
1256
1257 dev_dbg(icd->parent, "%s: returned params: width = %d, height = %d\n",
1258 __func__, pix->width, pix->height);
1259
1260 return 0;
1261}
1262
1263static int mx2_camera_querycap(struct soc_camera_host *ici,
1264 struct v4l2_capability *cap)
1265{
1266 /* cap->name is set by the friendly caller:-> */
1267 strlcpy(cap->card, MX2_CAM_DRIVER_DESCRIPTION, sizeof(cap->card));
1268 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
1269 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
1270
1271 return 0;
1272}
1273
1274static unsigned int mx2_camera_poll(struct file *file, poll_table *pt)
1275{
1276 struct soc_camera_device *icd = file->private_data;
1277
1278 return vb2_poll(&icd->vb2_vidq, file, pt);
1279}
1280
1281static struct soc_camera_host_ops mx2_soc_camera_host_ops = {
1282 .owner = THIS_MODULE,
1283 .add = mx2_camera_add_device,
1284 .remove = mx2_camera_remove_device,
1285 .clock_start = mx2_camera_clock_start,
1286 .clock_stop = mx2_camera_clock_stop,
1287 .set_fmt = mx2_camera_set_fmt,
1288 .set_crop = mx2_camera_set_crop,
1289 .get_formats = mx2_camera_get_formats,
1290 .try_fmt = mx2_camera_try_fmt,
1291 .init_videobuf2 = mx2_camera_init_videobuf,
1292 .poll = mx2_camera_poll,
1293 .querycap = mx2_camera_querycap,
1294 .set_bus_param = mx2_camera_set_bus_param,
1295};
1296
1297static void mx27_camera_frame_done_emma(struct mx2_camera_dev *pcdev,
1298 int bufnum, bool err)
1299{
1300#ifdef DEBUG
1301 struct mx2_fmt_cfg *prp = pcdev->emma_prp;
1302#endif
1303 struct mx2_buf_internal *ibuf;
1304 struct mx2_buffer *buf;
1305 struct vb2_buffer *vb;
1306 struct vb2_v4l2_buffer *vbuf;
1307 unsigned long phys;
1308
1309 ibuf = list_first_entry(&pcdev->active_bufs, struct mx2_buf_internal,
1310 queue);
1311
1312 BUG_ON(ibuf->bufnum != bufnum);
1313
1314 if (ibuf->discard) {
1315 /*
1316 * Discard buffer must not be returned to user space.
1317 * Just return it to the discard queue.
1318 */
1319 list_move_tail(pcdev->active_bufs.next, &pcdev->discard);
1320 } else {
1321 buf = mx2_ibuf_to_buf(ibuf);
1322
1323 vb = &buf->vb.vb2_buf;
1324 vbuf = to_vb2_v4l2_buffer(vb);
1325#ifdef DEBUG
1326 phys = vb2_dma_contig_plane_dma_addr(vb, 0);
1327 if (prp->cfg.channel == 1) {
1328 if (readl(pcdev->base_emma + PRP_DEST_RGB1_PTR +
1329 4 * bufnum) != phys) {
1330 dev_err(pcdev->dev, "%lx != %x\n", phys,
1331 readl(pcdev->base_emma +
1332 PRP_DEST_RGB1_PTR + 4 * bufnum));
1333 }
1334 } else {
1335 if (readl(pcdev->base_emma + PRP_DEST_Y_PTR -
1336 0x14 * bufnum) != phys) {
1337 dev_err(pcdev->dev, "%lx != %x\n", phys,
1338 readl(pcdev->base_emma +
1339 PRP_DEST_Y_PTR - 0x14 * bufnum));
1340 }
1341 }
1342#endif
1343 dev_dbg(pcdev->dev, "%s (vb=0x%p) 0x%p %lu\n", __func__, vb,
1344 vb2_plane_vaddr(vb, 0),
1345 vb2_get_plane_payload(vb, 0));
1346
1347 list_del_init(&buf->internal.queue);
1348 vb->timestamp = ktime_get_ns();
1349 vbuf->sequence = pcdev->frame_count;
1350 if (err)
1351 vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
1352 else
1353 vb2_buffer_done(vb, VB2_BUF_STATE_DONE);
1354 }
1355
1356 pcdev->frame_count++;
1357
1358 if (list_empty(&pcdev->capture)) {
1359 if (list_empty(&pcdev->discard)) {
1360 dev_warn(pcdev->dev, "%s: trying to access empty discard list\n",
1361 __func__);
1362 return;
1363 }
1364
1365 ibuf = list_first_entry(&pcdev->discard,
1366 struct mx2_buf_internal, queue);
1367 ibuf->bufnum = bufnum;
1368
1369 list_move_tail(pcdev->discard.next, &pcdev->active_bufs);
1370 mx27_update_emma_buf(pcdev, pcdev->discard_buffer_dma, bufnum);
1371 return;
1372 }
1373
1374 buf = list_first_entry(&pcdev->capture, struct mx2_buffer,
1375 internal.queue);
1376
1377 buf->internal.bufnum = bufnum;
1378
1379 list_move_tail(pcdev->capture.next, &pcdev->active_bufs);
1380
1381 vb = &buf->vb.vb2_buf;
1382
1383 phys = vb2_dma_contig_plane_dma_addr(vb, 0);
1384 mx27_update_emma_buf(pcdev, phys, bufnum);
1385}
1386
1387static irqreturn_t mx27_camera_emma_irq(int irq_emma, void *data)
1388{
1389 struct mx2_camera_dev *pcdev = data;
1390 unsigned int status = readl(pcdev->base_emma + PRP_INTRSTATUS);
1391 struct mx2_buf_internal *ibuf;
1392
1393 spin_lock(&pcdev->lock);
1394
1395 if (list_empty(&pcdev->active_bufs)) {
1396 dev_warn(pcdev->dev, "%s: called while active list is empty\n",
1397 __func__);
1398
1399 if (!status) {
1400 spin_unlock(&pcdev->lock);
1401 return IRQ_NONE;
1402 }
1403 }
1404
1405 if (status & (1 << 7)) { /* overflow */
1406 u32 cntl = readl(pcdev->base_emma + PRP_CNTL);
1407 writel(cntl & ~(PRP_CNTL_CH1EN | PRP_CNTL_CH2EN),
1408 pcdev->base_emma + PRP_CNTL);
1409 writel(cntl, pcdev->base_emma + PRP_CNTL);
1410
1411 ibuf = list_first_entry(&pcdev->active_bufs,
1412 struct mx2_buf_internal, queue);
1413 mx27_camera_frame_done_emma(pcdev,
1414 ibuf->bufnum, true);
1415
1416 status &= ~(1 << 7);
1417 } else if (((status & (3 << 5)) == (3 << 5)) ||
1418 ((status & (3 << 3)) == (3 << 3))) {
1419 /*
1420 * Both buffers have triggered, process the one we're expecting
1421 * to first
1422 */
1423 ibuf = list_first_entry(&pcdev->active_bufs,
1424 struct mx2_buf_internal, queue);
1425 mx27_camera_frame_done_emma(pcdev, ibuf->bufnum, false);
1426 status &= ~(1 << (6 - ibuf->bufnum)); /* mark processed */
1427 } else if ((status & (1 << 6)) || (status & (1 << 4))) {
1428 mx27_camera_frame_done_emma(pcdev, 0, false);
1429 } else if ((status & (1 << 5)) || (status & (1 << 3))) {
1430 mx27_camera_frame_done_emma(pcdev, 1, false);
1431 }
1432
1433 spin_unlock(&pcdev->lock);
1434 writel(status, pcdev->base_emma + PRP_INTRSTATUS);
1435
1436 return IRQ_HANDLED;
1437}
1438
1439static int mx27_camera_emma_init(struct platform_device *pdev)
1440{
1441 struct mx2_camera_dev *pcdev = platform_get_drvdata(pdev);
1442 struct resource *res_emma;
1443 int irq_emma;
1444 int err = 0;
1445
1446 res_emma = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1447 irq_emma = platform_get_irq(pdev, 1);
1448 if (!res_emma || !irq_emma) {
1449 dev_err(pcdev->dev, "no EMMA resources\n");
1450 err = -ENODEV;
1451 goto out;
1452 }
1453
1454 pcdev->base_emma = devm_ioremap_resource(pcdev->dev, res_emma);
1455 if (IS_ERR(pcdev->base_emma)) {
1456 err = PTR_ERR(pcdev->base_emma);
1457 goto out;
1458 }
1459
1460 err = devm_request_irq(pcdev->dev, irq_emma, mx27_camera_emma_irq, 0,
1461 MX2_CAM_DRV_NAME, pcdev);
1462 if (err) {
1463 dev_err(pcdev->dev, "Camera EMMA interrupt register failed\n");
1464 goto out;
1465 }
1466
1467 pcdev->clk_emma_ipg = devm_clk_get(pcdev->dev, "emma-ipg");
1468 if (IS_ERR(pcdev->clk_emma_ipg)) {
1469 err = PTR_ERR(pcdev->clk_emma_ipg);
1470 goto out;
1471 }
1472
1473 clk_prepare_enable(pcdev->clk_emma_ipg);
1474
1475 pcdev->clk_emma_ahb = devm_clk_get(pcdev->dev, "emma-ahb");
1476 if (IS_ERR(pcdev->clk_emma_ahb)) {
1477 err = PTR_ERR(pcdev->clk_emma_ahb);
1478 goto exit_clk_emma_ipg;
1479 }
1480
1481 clk_prepare_enable(pcdev->clk_emma_ahb);
1482
1483 err = mx27_camera_emma_prp_reset(pcdev);
1484 if (err)
1485 goto exit_clk_emma_ahb;
1486
1487 return err;
1488
1489exit_clk_emma_ahb:
1490 clk_disable_unprepare(pcdev->clk_emma_ahb);
1491exit_clk_emma_ipg:
1492 clk_disable_unprepare(pcdev->clk_emma_ipg);
1493out:
1494 return err;
1495}
1496
1497static int mx2_camera_probe(struct platform_device *pdev)
1498{
1499 struct mx2_camera_dev *pcdev;
1500 struct resource *res_csi;
1501 int irq_csi;
1502 int err = 0;
1503
1504 dev_dbg(&pdev->dev, "initialising\n");
1505
1506 res_csi = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1507 irq_csi = platform_get_irq(pdev, 0);
1508 if (res_csi == NULL || irq_csi < 0) {
1509 dev_err(&pdev->dev, "Missing platform resources data\n");
1510 err = -ENODEV;
1511 goto exit;
1512 }
1513
1514 pcdev = devm_kzalloc(&pdev->dev, sizeof(*pcdev), GFP_KERNEL);
1515 if (!pcdev) {
1516 dev_err(&pdev->dev, "Could not allocate pcdev\n");
1517 err = -ENOMEM;
1518 goto exit;
1519 }
1520
1521 pcdev->clk_csi_ahb = devm_clk_get(&pdev->dev, "ahb");
1522 if (IS_ERR(pcdev->clk_csi_ahb)) {
1523 dev_err(&pdev->dev, "Could not get csi ahb clock\n");
1524 err = PTR_ERR(pcdev->clk_csi_ahb);
1525 goto exit;
1526 }
1527
1528 pcdev->clk_csi_per = devm_clk_get(&pdev->dev, "per");
1529 if (IS_ERR(pcdev->clk_csi_per)) {
1530 dev_err(&pdev->dev, "Could not get csi per clock\n");
1531 err = PTR_ERR(pcdev->clk_csi_per);
1532 goto exit;
1533 }
1534
1535 pcdev->pdata = pdev->dev.platform_data;
1536 if (pcdev->pdata) {
1537 long rate;
1538
1539 pcdev->platform_flags = pcdev->pdata->flags;
1540
1541 rate = clk_round_rate(pcdev->clk_csi_per,
1542 pcdev->pdata->clk * 2);
1543 if (rate <= 0) {
1544 err = -ENODEV;
1545 goto exit;
1546 }
1547 err = clk_set_rate(pcdev->clk_csi_per, rate);
1548 if (err < 0)
1549 goto exit;
1550 }
1551
1552 INIT_LIST_HEAD(&pcdev->capture);
1553 INIT_LIST_HEAD(&pcdev->active_bufs);
1554 INIT_LIST_HEAD(&pcdev->discard);
1555 spin_lock_init(&pcdev->lock);
1556
1557 pcdev->base_csi = devm_ioremap_resource(&pdev->dev, res_csi);
1558 if (IS_ERR(pcdev->base_csi)) {
1559 err = PTR_ERR(pcdev->base_csi);
1560 goto exit;
1561 }
1562
1563 pcdev->dev = &pdev->dev;
1564 platform_set_drvdata(pdev, pcdev);
1565
1566 err = mx27_camera_emma_init(pdev);
1567 if (err)
1568 goto exit;
1569
1570 /*
1571 * We're done with drvdata here. Clear the pointer so that
1572 * v4l2 core can start using drvdata on its purpose.
1573 */
1574 platform_set_drvdata(pdev, NULL);
1575
1576 pcdev->soc_host.drv_name = MX2_CAM_DRV_NAME,
1577 pcdev->soc_host.ops = &mx2_soc_camera_host_ops,
1578 pcdev->soc_host.priv = pcdev;
1579 pcdev->soc_host.v4l2_dev.dev = &pdev->dev;
1580 pcdev->soc_host.nr = pdev->id;
1581
1582 pcdev->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
1583 if (IS_ERR(pcdev->alloc_ctx)) {
1584 err = PTR_ERR(pcdev->alloc_ctx);
1585 goto eallocctx;
1586 }
1587 err = soc_camera_host_register(&pcdev->soc_host);
1588 if (err)
1589 goto exit_free_emma;
1590
1591 dev_info(&pdev->dev, "MX2 Camera (CSI) driver probed, clock frequency: %ld\n",
1592 clk_get_rate(pcdev->clk_csi_per));
1593
1594 return 0;
1595
1596exit_free_emma:
1597 vb2_dma_contig_cleanup_ctx(pcdev->alloc_ctx);
1598eallocctx:
1599 clk_disable_unprepare(pcdev->clk_emma_ipg);
1600 clk_disable_unprepare(pcdev->clk_emma_ahb);
1601exit:
1602 return err;
1603}
1604
1605static int mx2_camera_remove(struct platform_device *pdev)
1606{
1607 struct soc_camera_host *soc_host = to_soc_camera_host(&pdev->dev);
1608 struct mx2_camera_dev *pcdev = container_of(soc_host,
1609 struct mx2_camera_dev, soc_host);
1610
1611 soc_camera_host_unregister(&pcdev->soc_host);
1612
1613 vb2_dma_contig_cleanup_ctx(pcdev->alloc_ctx);
1614
1615 clk_disable_unprepare(pcdev->clk_emma_ipg);
1616 clk_disable_unprepare(pcdev->clk_emma_ahb);
1617
1618 dev_info(&pdev->dev, "MX2 Camera driver unloaded\n");
1619
1620 return 0;
1621}
1622
1623static struct platform_driver mx2_camera_driver = {
1624 .driver = {
1625 .name = MX2_CAM_DRV_NAME,
1626 },
1627 .id_table = mx2_camera_devtype,
1628 .remove = mx2_camera_remove,
1629};
1630
1631module_platform_driver_probe(mx2_camera_driver, mx2_camera_probe);
1632
1633MODULE_DESCRIPTION("i.MX27 SoC Camera Host driver");
1634MODULE_AUTHOR("Sascha Hauer <sha@pengutronix.de>");
1635MODULE_LICENSE("GPL");
1636MODULE_VERSION(MX2_CAM_VERSION);
diff --git a/drivers/staging/media/mx3/Kconfig b/drivers/staging/media/mx3/Kconfig
deleted file mode 100644
index 595d5fe7cad1..000000000000
--- a/drivers/staging/media/mx3/Kconfig
+++ /dev/null
@@ -1,15 +0,0 @@
1config VIDEO_MX3
2 tristate "i.MX3x Camera Sensor Interface driver"
3 depends on VIDEO_DEV && MX3_IPU && SOC_CAMERA
4 depends on MX3_IPU || COMPILE_TEST
5 depends on HAS_DMA
6 select VIDEOBUF2_DMA_CONTIG
7 ---help---
8 This is a v4l2 driver for the i.MX3x Camera Sensor Interface
9
10 This driver is deprecated: it should become a stand-alone driver
11 instead of using the soc-camera framework.
12
13 Unless someone is willing to take this on (unlikely with such
14 ancient hardware) it is going to be removed from the kernel
15 soon.
diff --git a/drivers/staging/media/mx3/Makefile b/drivers/staging/media/mx3/Makefile
deleted file mode 100644
index 6d91dcd80c1d..000000000000
--- a/drivers/staging/media/mx3/Makefile
+++ /dev/null
@@ -1,3 +0,0 @@
1# Makefile for i.MX3x Camera Sensor driver
2
3obj-$(CONFIG_VIDEO_MX3) += mx3_camera.o
diff --git a/drivers/staging/media/mx3/TODO b/drivers/staging/media/mx3/TODO
deleted file mode 100644
index bc68fa443a3e..000000000000
--- a/drivers/staging/media/mx3/TODO
+++ /dev/null
@@ -1,10 +0,0 @@
1This driver is deprecated: it should become a stand-alone driver instead of
2using the soc-camera framework.
3
4Unless someone is willing to take this on (unlikely with such ancient
5hardware) it is going to be removed from the kernel soon.
6
7Note that trivial patches will not be accepted anymore, only a full conversion.
8
9If you want to convert this driver, please contact the linux-media mailinglist
10(see http://linuxtv.org/lists.php).
diff --git a/drivers/staging/media/mx3/mx3_camera.c b/drivers/staging/media/mx3/mx3_camera.c
deleted file mode 100644
index aa39e9569b1a..000000000000
--- a/drivers/staging/media/mx3/mx3_camera.c
+++ /dev/null
@@ -1,1264 +0,0 @@
1/*
2 * V4L2 Driver for i.MX3x camera host
3 *
4 * Copyright (C) 2008
5 * Guennadi Liakhovetski, DENX Software Engineering, <lg@denx.de>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11
12#include <linux/init.h>
13#include <linux/module.h>
14#include <linux/videodev2.h>
15#include <linux/platform_device.h>
16#include <linux/clk.h>
17#include <linux/vmalloc.h>
18#include <linux/interrupt.h>
19#include <linux/sched.h>
20#include <linux/dma/ipu-dma.h>
21
22#include <media/v4l2-common.h>
23#include <media/v4l2-dev.h>
24#include <media/videobuf2-dma-contig.h>
25#include <media/soc_camera.h>
26#include <media/drv-intf/soc_mediabus.h>
27
28#include <linux/platform_data/media/camera-mx3.h>
29#include <linux/platform_data/dma-imx.h>
30
31#define MX3_CAM_DRV_NAME "mx3-camera"
32
33/* CMOS Sensor Interface Registers */
34#define CSI_REG_START 0x60
35
36#define CSI_SENS_CONF (0x60 - CSI_REG_START)
37#define CSI_SENS_FRM_SIZE (0x64 - CSI_REG_START)
38#define CSI_ACT_FRM_SIZE (0x68 - CSI_REG_START)
39#define CSI_OUT_FRM_CTRL (0x6C - CSI_REG_START)
40#define CSI_TST_CTRL (0x70 - CSI_REG_START)
41#define CSI_CCIR_CODE_1 (0x74 - CSI_REG_START)
42#define CSI_CCIR_CODE_2 (0x78 - CSI_REG_START)
43#define CSI_CCIR_CODE_3 (0x7C - CSI_REG_START)
44#define CSI_FLASH_STROBE_1 (0x80 - CSI_REG_START)
45#define CSI_FLASH_STROBE_2 (0x84 - CSI_REG_START)
46
47#define CSI_SENS_CONF_VSYNC_POL_SHIFT 0
48#define CSI_SENS_CONF_HSYNC_POL_SHIFT 1
49#define CSI_SENS_CONF_DATA_POL_SHIFT 2
50#define CSI_SENS_CONF_PIX_CLK_POL_SHIFT 3
51#define CSI_SENS_CONF_SENS_PRTCL_SHIFT 4
52#define CSI_SENS_CONF_SENS_CLKSRC_SHIFT 7
53#define CSI_SENS_CONF_DATA_FMT_SHIFT 8
54#define CSI_SENS_CONF_DATA_WIDTH_SHIFT 10
55#define CSI_SENS_CONF_EXT_VSYNC_SHIFT 15
56#define CSI_SENS_CONF_DIVRATIO_SHIFT 16
57
58#define CSI_SENS_CONF_DATA_FMT_RGB_YUV444 (0UL << CSI_SENS_CONF_DATA_FMT_SHIFT)
59#define CSI_SENS_CONF_DATA_FMT_YUV422 (2UL << CSI_SENS_CONF_DATA_FMT_SHIFT)
60#define CSI_SENS_CONF_DATA_FMT_BAYER (3UL << CSI_SENS_CONF_DATA_FMT_SHIFT)
61
62#define MAX_VIDEO_MEM 16
63
64struct mx3_camera_buffer {
65 /* common v4l buffer stuff -- must be first */
66 struct vb2_v4l2_buffer vb;
67 struct list_head queue;
68
69 /* One descriptot per scatterlist (per frame) */
70 struct dma_async_tx_descriptor *txd;
71
72 /* We have to "build" a scatterlist ourselves - one element per frame */
73 struct scatterlist sg;
74};
75
76/**
77 * struct mx3_camera_dev - i.MX3x camera (CSI) object
78 * @dev: camera device, to which the coherent buffer is attached
79 * @icd: currently attached camera sensor
80 * @clk: pointer to clock
81 * @base: remapped register base address
82 * @pdata: platform data
83 * @platform_flags: platform flags
84 * @mclk: master clock frequency in Hz
85 * @capture: list of capture videobuffers
86 * @lock: protects video buffer lists
87 * @active: active video buffer
88 * @idmac_channel: array of pointers to IPU DMAC DMA channels
89 * @soc_host: embedded soc_host object
90 */
91struct mx3_camera_dev {
92 /*
93 * i.MX3x is only supposed to handle one camera on its Camera Sensor
94 * Interface. If anyone ever builds hardware to enable more than one
95 * camera _simultaneously_, they will have to modify this driver too
96 */
97 struct clk *clk;
98
99 void __iomem *base;
100
101 struct mx3_camera_pdata *pdata;
102
103 unsigned long platform_flags;
104 unsigned long mclk;
105 u16 width_flags; /* max 15 bits */
106
107 struct list_head capture;
108 spinlock_t lock; /* Protects video buffer lists */
109 struct mx3_camera_buffer *active;
110 size_t buf_total;
111 struct vb2_alloc_ctx *alloc_ctx;
112 enum v4l2_field field;
113 int sequence;
114
115 /* IDMAC / dmaengine interface */
116 struct idmac_channel *idmac_channel[1]; /* We need one channel */
117
118 struct soc_camera_host soc_host;
119};
120
121struct dma_chan_request {
122 struct mx3_camera_dev *mx3_cam;
123 enum ipu_channel id;
124};
125
126static u32 csi_reg_read(struct mx3_camera_dev *mx3, off_t reg)
127{
128 return __raw_readl(mx3->base + reg);
129}
130
131static void csi_reg_write(struct mx3_camera_dev *mx3, u32 value, off_t reg)
132{
133 __raw_writel(value, mx3->base + reg);
134}
135
136static struct mx3_camera_buffer *to_mx3_vb(struct vb2_v4l2_buffer *vb)
137{
138 return container_of(vb, struct mx3_camera_buffer, vb);
139}
140
141/* Called from the IPU IDMAC ISR */
142static void mx3_cam_dma_done(void *arg)
143{
144 struct idmac_tx_desc *desc = to_tx_desc(arg);
145 struct dma_chan *chan = desc->txd.chan;
146 struct idmac_channel *ichannel = to_idmac_chan(chan);
147 struct mx3_camera_dev *mx3_cam = ichannel->client;
148
149 dev_dbg(chan->device->dev, "callback cookie %d, active DMA %pad\n",
150 desc->txd.cookie, mx3_cam->active ? &sg_dma_address(&mx3_cam->active->sg) : NULL);
151
152 spin_lock(&mx3_cam->lock);
153 if (mx3_cam->active) {
154 struct vb2_v4l2_buffer *vb = &mx3_cam->active->vb;
155 struct mx3_camera_buffer *buf = to_mx3_vb(vb);
156
157 list_del_init(&buf->queue);
158 vb->vb2_buf.timestamp = ktime_get_ns();
159 vb->field = mx3_cam->field;
160 vb->sequence = mx3_cam->sequence++;
161 vb2_buffer_done(&vb->vb2_buf, VB2_BUF_STATE_DONE);
162 }
163
164 if (list_empty(&mx3_cam->capture)) {
165 mx3_cam->active = NULL;
166 spin_unlock(&mx3_cam->lock);
167
168 /*
169 * stop capture - without further buffers IPU_CHA_BUF0_RDY will
170 * not get updated
171 */
172 return;
173 }
174
175 mx3_cam->active = list_entry(mx3_cam->capture.next,
176 struct mx3_camera_buffer, queue);
177 spin_unlock(&mx3_cam->lock);
178}
179
180/*
181 * Videobuf operations
182 */
183
184/*
185 * Calculate the __buffer__ (not data) size and number of buffers.
186 */
187static int mx3_videobuf_setup(struct vb2_queue *vq,
188 unsigned int *count, unsigned int *num_planes,
189 unsigned int sizes[], void *alloc_ctxs[])
190{
191 struct soc_camera_device *icd = soc_camera_from_vb2q(vq);
192 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
193 struct mx3_camera_dev *mx3_cam = ici->priv;
194
195 if (!mx3_cam->idmac_channel[0])
196 return -EINVAL;
197
198 alloc_ctxs[0] = mx3_cam->alloc_ctx;
199
200 if (!vq->num_buffers)
201 mx3_cam->sequence = 0;
202
203 if (!*count)
204 *count = 2;
205
206 /* Called from VIDIOC_REQBUFS or in compatibility mode */
207 if (!*num_planes)
208 sizes[0] = icd->sizeimage;
209 else if (sizes[0] < icd->sizeimage)
210 return -EINVAL;
211
212 /* If *num_planes != 0, we have already verified *count. */
213 if (sizes[0] * *count + mx3_cam->buf_total > MAX_VIDEO_MEM * 1024 * 1024)
214 *count = (MAX_VIDEO_MEM * 1024 * 1024 - mx3_cam->buf_total) /
215 sizes[0];
216
217 *num_planes = 1;
218
219 return 0;
220}
221
222static enum pixel_fmt fourcc_to_ipu_pix(__u32 fourcc)
223{
224 /* Add more formats as need arises and test possibilities appear... */
225 switch (fourcc) {
226 case V4L2_PIX_FMT_RGB24:
227 return IPU_PIX_FMT_RGB24;
228 case V4L2_PIX_FMT_UYVY:
229 case V4L2_PIX_FMT_RGB565:
230 default:
231 return IPU_PIX_FMT_GENERIC;
232 }
233}
234
235static void mx3_videobuf_queue(struct vb2_buffer *vb)
236{
237 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
238 struct soc_camera_device *icd = soc_camera_from_vb2q(vb->vb2_queue);
239 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
240 struct mx3_camera_dev *mx3_cam = ici->priv;
241 struct mx3_camera_buffer *buf = to_mx3_vb(vbuf);
242 struct scatterlist *sg = &buf->sg;
243 struct dma_async_tx_descriptor *txd;
244 struct idmac_channel *ichan = mx3_cam->idmac_channel[0];
245 struct idmac_video_param *video = &ichan->params.video;
246 const struct soc_mbus_pixelfmt *host_fmt = icd->current_fmt->host_fmt;
247 dma_cookie_t cookie;
248 size_t new_size;
249
250 new_size = icd->sizeimage;
251
252 if (vb2_plane_size(vb, 0) < new_size) {
253 dev_err(icd->parent, "Buffer #%d too small (%lu < %zu)\n",
254 vbuf->vb2_buf.index, vb2_plane_size(vb, 0), new_size);
255 goto error;
256 }
257
258 if (!buf->txd) {
259 sg_dma_address(sg) = vb2_dma_contig_plane_dma_addr(vb, 0);
260 sg_dma_len(sg) = new_size;
261
262 txd = dmaengine_prep_slave_sg(
263 &ichan->dma_chan, sg, 1, DMA_DEV_TO_MEM,
264 DMA_PREP_INTERRUPT);
265 if (!txd)
266 goto error;
267
268 txd->callback_param = txd;
269 txd->callback = mx3_cam_dma_done;
270
271 buf->txd = txd;
272 } else {
273 txd = buf->txd;
274 }
275
276 vb2_set_plane_payload(vb, 0, new_size);
277
278 /* This is the configuration of one sg-element */
279 video->out_pixel_fmt = fourcc_to_ipu_pix(host_fmt->fourcc);
280
281 if (video->out_pixel_fmt == IPU_PIX_FMT_GENERIC) {
282 /*
283 * If the IPU DMA channel is configured to transfer generic
284 * 8-bit data, we have to set up the geometry parameters
285 * correctly, according to the current pixel format. The DMA
286 * horizontal parameters in this case are expressed in bytes,
287 * not in pixels.
288 */
289 video->out_width = icd->bytesperline;
290 video->out_height = icd->user_height;
291 video->out_stride = icd->bytesperline;
292 } else {
293 /*
294 * For IPU known formats the pixel unit will be managed
295 * successfully by the IPU code
296 */
297 video->out_width = icd->user_width;
298 video->out_height = icd->user_height;
299 video->out_stride = icd->user_width;
300 }
301
302#ifdef DEBUG
303 /* helps to see what DMA actually has written */
304 if (vb2_plane_vaddr(vb, 0))
305 memset(vb2_plane_vaddr(vb, 0), 0xaa, vb2_get_plane_payload(vb, 0));
306#endif
307
308 spin_lock_irq(&mx3_cam->lock);
309 list_add_tail(&buf->queue, &mx3_cam->capture);
310
311 if (!mx3_cam->active)
312 mx3_cam->active = buf;
313
314 spin_unlock_irq(&mx3_cam->lock);
315
316 cookie = txd->tx_submit(txd);
317 dev_dbg(icd->parent, "Submitted cookie %d DMA %pad\n",
318 cookie, &sg_dma_address(&buf->sg));
319
320 if (cookie >= 0)
321 return;
322
323 spin_lock_irq(&mx3_cam->lock);
324
325 /* Submit error */
326 list_del_init(&buf->queue);
327
328 if (mx3_cam->active == buf)
329 mx3_cam->active = NULL;
330
331 spin_unlock_irq(&mx3_cam->lock);
332error:
333 vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
334}
335
336static void mx3_videobuf_release(struct vb2_buffer *vb)
337{
338 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
339 struct soc_camera_device *icd = soc_camera_from_vb2q(vb->vb2_queue);
340 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
341 struct mx3_camera_dev *mx3_cam = ici->priv;
342 struct mx3_camera_buffer *buf = to_mx3_vb(vbuf);
343 struct dma_async_tx_descriptor *txd = buf->txd;
344 unsigned long flags;
345
346 dev_dbg(icd->parent,
347 "Release%s DMA %pad, queue %sempty\n",
348 mx3_cam->active == buf ? " active" : "", &sg_dma_address(&buf->sg),
349 list_empty(&buf->queue) ? "" : "not ");
350
351 spin_lock_irqsave(&mx3_cam->lock, flags);
352
353 if (mx3_cam->active == buf)
354 mx3_cam->active = NULL;
355
356 /* Doesn't hurt also if the list is empty */
357 list_del_init(&buf->queue);
358
359 if (txd) {
360 buf->txd = NULL;
361 if (mx3_cam->idmac_channel[0])
362 async_tx_ack(txd);
363 }
364
365 spin_unlock_irqrestore(&mx3_cam->lock, flags);
366
367 mx3_cam->buf_total -= vb2_plane_size(vb, 0);
368}
369
370static int mx3_videobuf_init(struct vb2_buffer *vb)
371{
372 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
373 struct soc_camera_device *icd = soc_camera_from_vb2q(vb->vb2_queue);
374 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
375 struct mx3_camera_dev *mx3_cam = ici->priv;
376 struct mx3_camera_buffer *buf = to_mx3_vb(vbuf);
377
378 if (!buf->txd) {
379 /* This is for locking debugging only */
380 INIT_LIST_HEAD(&buf->queue);
381 sg_init_table(&buf->sg, 1);
382
383 mx3_cam->buf_total += vb2_plane_size(vb, 0);
384 }
385
386 return 0;
387}
388
389static void mx3_stop_streaming(struct vb2_queue *q)
390{
391 struct soc_camera_device *icd = soc_camera_from_vb2q(q);
392 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
393 struct mx3_camera_dev *mx3_cam = ici->priv;
394 struct idmac_channel *ichan = mx3_cam->idmac_channel[0];
395 struct mx3_camera_buffer *buf, *tmp;
396 unsigned long flags;
397
398 if (ichan)
399 dmaengine_pause(&ichan->dma_chan);
400
401 spin_lock_irqsave(&mx3_cam->lock, flags);
402
403 mx3_cam->active = NULL;
404
405 list_for_each_entry_safe(buf, tmp, &mx3_cam->capture, queue) {
406 list_del_init(&buf->queue);
407 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
408 }
409
410 spin_unlock_irqrestore(&mx3_cam->lock, flags);
411}
412
413static struct vb2_ops mx3_videobuf_ops = {
414 .queue_setup = mx3_videobuf_setup,
415 .buf_queue = mx3_videobuf_queue,
416 .buf_cleanup = mx3_videobuf_release,
417 .buf_init = mx3_videobuf_init,
418 .wait_prepare = vb2_ops_wait_prepare,
419 .wait_finish = vb2_ops_wait_finish,
420 .stop_streaming = mx3_stop_streaming,
421};
422
423static int mx3_camera_init_videobuf(struct vb2_queue *q,
424 struct soc_camera_device *icd)
425{
426 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
427
428 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
429 q->io_modes = VB2_MMAP | VB2_USERPTR;
430 q->drv_priv = icd;
431 q->ops = &mx3_videobuf_ops;
432 q->mem_ops = &vb2_dma_contig_memops;
433 q->buf_struct_size = sizeof(struct mx3_camera_buffer);
434 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
435 q->lock = &ici->host_lock;
436
437 return vb2_queue_init(q);
438}
439
440/* First part of ipu_csi_init_interface() */
441static void mx3_camera_activate(struct mx3_camera_dev *mx3_cam)
442{
443 u32 conf;
444 long rate;
445
446 /* Set default size: ipu_csi_set_window_size() */
447 csi_reg_write(mx3_cam, (640 - 1) | ((480 - 1) << 16), CSI_ACT_FRM_SIZE);
448 /* ...and position to 0:0: ipu_csi_set_window_pos() */
449 conf = csi_reg_read(mx3_cam, CSI_OUT_FRM_CTRL) & 0xffff0000;
450 csi_reg_write(mx3_cam, conf, CSI_OUT_FRM_CTRL);
451
452 /* We use only gated clock synchronisation mode so far */
453 conf = 0 << CSI_SENS_CONF_SENS_PRTCL_SHIFT;
454
455 /* Set generic data, platform-biggest bus-width */
456 conf |= CSI_SENS_CONF_DATA_FMT_BAYER;
457
458 if (mx3_cam->platform_flags & MX3_CAMERA_DATAWIDTH_15)
459 conf |= 3 << CSI_SENS_CONF_DATA_WIDTH_SHIFT;
460 else if (mx3_cam->platform_flags & MX3_CAMERA_DATAWIDTH_10)
461 conf |= 2 << CSI_SENS_CONF_DATA_WIDTH_SHIFT;
462 else if (mx3_cam->platform_flags & MX3_CAMERA_DATAWIDTH_8)
463 conf |= 1 << CSI_SENS_CONF_DATA_WIDTH_SHIFT;
464 else/* if (mx3_cam->platform_flags & MX3_CAMERA_DATAWIDTH_4)*/
465 conf |= 0 << CSI_SENS_CONF_DATA_WIDTH_SHIFT;
466
467 if (mx3_cam->platform_flags & MX3_CAMERA_CLK_SRC)
468 conf |= 1 << CSI_SENS_CONF_SENS_CLKSRC_SHIFT;
469 if (mx3_cam->platform_flags & MX3_CAMERA_EXT_VSYNC)
470 conf |= 1 << CSI_SENS_CONF_EXT_VSYNC_SHIFT;
471 if (mx3_cam->platform_flags & MX3_CAMERA_DP)
472 conf |= 1 << CSI_SENS_CONF_DATA_POL_SHIFT;
473 if (mx3_cam->platform_flags & MX3_CAMERA_PCP)
474 conf |= 1 << CSI_SENS_CONF_PIX_CLK_POL_SHIFT;
475 if (mx3_cam->platform_flags & MX3_CAMERA_HSP)
476 conf |= 1 << CSI_SENS_CONF_HSYNC_POL_SHIFT;
477 if (mx3_cam->platform_flags & MX3_CAMERA_VSP)
478 conf |= 1 << CSI_SENS_CONF_VSYNC_POL_SHIFT;
479
480 /* ipu_csi_init_interface() */
481 csi_reg_write(mx3_cam, conf, CSI_SENS_CONF);
482
483 clk_prepare_enable(mx3_cam->clk);
484 rate = clk_round_rate(mx3_cam->clk, mx3_cam->mclk);
485 dev_dbg(mx3_cam->soc_host.v4l2_dev.dev, "Set SENS_CONF to %x, rate %ld\n", conf, rate);
486 if (rate)
487 clk_set_rate(mx3_cam->clk, rate);
488}
489
490static int mx3_camera_add_device(struct soc_camera_device *icd)
491{
492 dev_info(icd->parent, "MX3 Camera driver attached to camera %d\n",
493 icd->devnum);
494
495 return 0;
496}
497
498static void mx3_camera_remove_device(struct soc_camera_device *icd)
499{
500 dev_info(icd->parent, "MX3 Camera driver detached from camera %d\n",
501 icd->devnum);
502}
503
504/* Called with .host_lock held */
505static int mx3_camera_clock_start(struct soc_camera_host *ici)
506{
507 struct mx3_camera_dev *mx3_cam = ici->priv;
508
509 mx3_camera_activate(mx3_cam);
510
511 mx3_cam->buf_total = 0;
512
513 return 0;
514}
515
516/* Called with .host_lock held */
517static void mx3_camera_clock_stop(struct soc_camera_host *ici)
518{
519 struct mx3_camera_dev *mx3_cam = ici->priv;
520 struct idmac_channel **ichan = &mx3_cam->idmac_channel[0];
521
522 if (*ichan) {
523 dma_release_channel(&(*ichan)->dma_chan);
524 *ichan = NULL;
525 }
526
527 clk_disable_unprepare(mx3_cam->clk);
528}
529
530static int test_platform_param(struct mx3_camera_dev *mx3_cam,
531 unsigned char buswidth, unsigned long *flags)
532{
533 /*
534 * If requested data width is supported by the platform, use it or any
535 * possible lower value - i.MX31 is smart enough to shift bits
536 */
537 if (buswidth > fls(mx3_cam->width_flags))
538 return -EINVAL;
539
540 /*
541 * Platform specified synchronization and pixel clock polarities are
542 * only a recommendation and are only used during probing. MX3x
543 * camera interface only works in master mode, i.e., uses HSYNC and
544 * VSYNC signals from the sensor
545 */
546 *flags = V4L2_MBUS_MASTER |
547 V4L2_MBUS_HSYNC_ACTIVE_HIGH |
548 V4L2_MBUS_HSYNC_ACTIVE_LOW |
549 V4L2_MBUS_VSYNC_ACTIVE_HIGH |
550 V4L2_MBUS_VSYNC_ACTIVE_LOW |
551 V4L2_MBUS_PCLK_SAMPLE_RISING |
552 V4L2_MBUS_PCLK_SAMPLE_FALLING |
553 V4L2_MBUS_DATA_ACTIVE_HIGH |
554 V4L2_MBUS_DATA_ACTIVE_LOW;
555
556 return 0;
557}
558
559static int mx3_camera_try_bus_param(struct soc_camera_device *icd,
560 const unsigned int depth)
561{
562 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
563 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
564 struct mx3_camera_dev *mx3_cam = ici->priv;
565 struct v4l2_mbus_config cfg = {.type = V4L2_MBUS_PARALLEL,};
566 unsigned long bus_flags, common_flags;
567 int ret = test_platform_param(mx3_cam, depth, &bus_flags);
568
569 dev_dbg(icd->parent, "request bus width %d bit: %d\n", depth, ret);
570
571 if (ret < 0)
572 return ret;
573
574 ret = v4l2_subdev_call(sd, video, g_mbus_config, &cfg);
575 if (!ret) {
576 common_flags = soc_mbus_config_compatible(&cfg,
577 bus_flags);
578 if (!common_flags) {
579 dev_warn(icd->parent,
580 "Flags incompatible: camera 0x%x, host 0x%lx\n",
581 cfg.flags, bus_flags);
582 return -EINVAL;
583 }
584 } else if (ret != -ENOIOCTLCMD) {
585 return ret;
586 }
587
588 return 0;
589}
590
591static bool chan_filter(struct dma_chan *chan, void *arg)
592{
593 struct dma_chan_request *rq = arg;
594 struct mx3_camera_pdata *pdata;
595
596 if (!imx_dma_is_ipu(chan))
597 return false;
598
599 if (!rq)
600 return false;
601
602 pdata = rq->mx3_cam->soc_host.v4l2_dev.dev->platform_data;
603
604 return rq->id == chan->chan_id &&
605 pdata->dma_dev == chan->device->dev;
606}
607
608static const struct soc_mbus_pixelfmt mx3_camera_formats[] = {
609 {
610 .fourcc = V4L2_PIX_FMT_SBGGR8,
611 .name = "Bayer BGGR (sRGB) 8 bit",
612 .bits_per_sample = 8,
613 .packing = SOC_MBUS_PACKING_NONE,
614 .order = SOC_MBUS_ORDER_LE,
615 .layout = SOC_MBUS_LAYOUT_PACKED,
616 }, {
617 .fourcc = V4L2_PIX_FMT_GREY,
618 .name = "Monochrome 8 bit",
619 .bits_per_sample = 8,
620 .packing = SOC_MBUS_PACKING_NONE,
621 .order = SOC_MBUS_ORDER_LE,
622 .layout = SOC_MBUS_LAYOUT_PACKED,
623 },
624};
625
626/* This will be corrected as we get more formats */
627static bool mx3_camera_packing_supported(const struct soc_mbus_pixelfmt *fmt)
628{
629 return fmt->packing == SOC_MBUS_PACKING_NONE ||
630 (fmt->bits_per_sample == 8 &&
631 fmt->packing == SOC_MBUS_PACKING_2X8_PADHI) ||
632 (fmt->bits_per_sample > 8 &&
633 fmt->packing == SOC_MBUS_PACKING_EXTEND16);
634}
635
636static int mx3_camera_get_formats(struct soc_camera_device *icd, unsigned int idx,
637 struct soc_camera_format_xlate *xlate)
638{
639 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
640 struct device *dev = icd->parent;
641 int formats = 0, ret;
642 struct v4l2_subdev_mbus_code_enum code = {
643 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
644 .index = idx,
645 };
646 const struct soc_mbus_pixelfmt *fmt;
647
648 ret = v4l2_subdev_call(sd, pad, enum_mbus_code, NULL, &code);
649 if (ret < 0)
650 /* No more formats */
651 return 0;
652
653 fmt = soc_mbus_get_fmtdesc(code.code);
654 if (!fmt) {
655 dev_warn(icd->parent,
656 "Unsupported format code #%u: 0x%x\n", idx, code.code);
657 return 0;
658 }
659
660 /* This also checks support for the requested bits-per-sample */
661 ret = mx3_camera_try_bus_param(icd, fmt->bits_per_sample);
662 if (ret < 0)
663 return 0;
664
665 switch (code.code) {
666 case MEDIA_BUS_FMT_SBGGR10_1X10:
667 formats++;
668 if (xlate) {
669 xlate->host_fmt = &mx3_camera_formats[0];
670 xlate->code = code.code;
671 xlate++;
672 dev_dbg(dev, "Providing format %s using code 0x%x\n",
673 mx3_camera_formats[0].name, code.code);
674 }
675 break;
676 case MEDIA_BUS_FMT_Y10_1X10:
677 formats++;
678 if (xlate) {
679 xlate->host_fmt = &mx3_camera_formats[1];
680 xlate->code = code.code;
681 xlate++;
682 dev_dbg(dev, "Providing format %s using code 0x%x\n",
683 mx3_camera_formats[1].name, code.code);
684 }
685 break;
686 default:
687 if (!mx3_camera_packing_supported(fmt))
688 return 0;
689 }
690
691 /* Generic pass-through */
692 formats++;
693 if (xlate) {
694 xlate->host_fmt = fmt;
695 xlate->code = code.code;
696 dev_dbg(dev, "Providing format %c%c%c%c in pass-through mode\n",
697 (fmt->fourcc >> (0*8)) & 0xFF,
698 (fmt->fourcc >> (1*8)) & 0xFF,
699 (fmt->fourcc >> (2*8)) & 0xFF,
700 (fmt->fourcc >> (3*8)) & 0xFF);
701 xlate++;
702 }
703
704 return formats;
705}
706
707static void configure_geometry(struct mx3_camera_dev *mx3_cam,
708 unsigned int width, unsigned int height,
709 const struct soc_mbus_pixelfmt *fmt)
710{
711 u32 ctrl, width_field, height_field;
712
713 if (fourcc_to_ipu_pix(fmt->fourcc) == IPU_PIX_FMT_GENERIC) {
714 /*
715 * As the CSI will be configured to output BAYER, here
716 * the width parameter count the number of samples to
717 * capture to complete the whole image width.
718 */
719 unsigned int num, den;
720 int ret = soc_mbus_samples_per_pixel(fmt, &num, &den);
721 BUG_ON(ret < 0);
722 width = width * num / den;
723 }
724
725 /* Setup frame size - this cannot be changed on-the-fly... */
726 width_field = width - 1;
727 height_field = height - 1;
728 csi_reg_write(mx3_cam, width_field | (height_field << 16), CSI_SENS_FRM_SIZE);
729
730 csi_reg_write(mx3_cam, width_field << 16, CSI_FLASH_STROBE_1);
731 csi_reg_write(mx3_cam, (height_field << 16) | 0x22, CSI_FLASH_STROBE_2);
732
733 csi_reg_write(mx3_cam, width_field | (height_field << 16), CSI_ACT_FRM_SIZE);
734
735 /* ...and position */
736 ctrl = csi_reg_read(mx3_cam, CSI_OUT_FRM_CTRL) & 0xffff0000;
737 /* Sensor does the cropping */
738 csi_reg_write(mx3_cam, ctrl | 0 | (0 << 8), CSI_OUT_FRM_CTRL);
739}
740
741static int acquire_dma_channel(struct mx3_camera_dev *mx3_cam)
742{
743 dma_cap_mask_t mask;
744 struct dma_chan *chan;
745 struct idmac_channel **ichan = &mx3_cam->idmac_channel[0];
746 /* We have to use IDMAC_IC_7 for Bayer / generic data */
747 struct dma_chan_request rq = {.mx3_cam = mx3_cam,
748 .id = IDMAC_IC_7};
749
750 dma_cap_zero(mask);
751 dma_cap_set(DMA_SLAVE, mask);
752 dma_cap_set(DMA_PRIVATE, mask);
753 chan = dma_request_channel(mask, chan_filter, &rq);
754 if (!chan)
755 return -EBUSY;
756
757 *ichan = to_idmac_chan(chan);
758 (*ichan)->client = mx3_cam;
759
760 return 0;
761}
762
763/*
764 * FIXME: learn to use stride != width, then we can keep stride properly aligned
765 * and support arbitrary (even) widths.
766 */
767static inline void stride_align(__u32 *width)
768{
769 if (ALIGN(*width, 8) < 4096)
770 *width = ALIGN(*width, 8);
771 else
772 *width = *width & ~7;
773}
774
775/*
776 * As long as we don't implement host-side cropping and scaling, we can use
777 * default g_crop and cropcap from soc_camera.c
778 */
779static int mx3_camera_set_crop(struct soc_camera_device *icd,
780 const struct v4l2_crop *a)
781{
782 struct v4l2_crop a_writable = *a;
783 struct v4l2_rect *rect = &a_writable.c;
784 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
785 struct mx3_camera_dev *mx3_cam = ici->priv;
786 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
787 struct v4l2_subdev_format fmt = {
788 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
789 };
790 struct v4l2_mbus_framefmt *mf = &fmt.format;
791 int ret;
792
793 soc_camera_limit_side(&rect->left, &rect->width, 0, 2, 4096);
794 soc_camera_limit_side(&rect->top, &rect->height, 0, 2, 4096);
795
796 ret = v4l2_subdev_call(sd, video, s_crop, a);
797 if (ret < 0)
798 return ret;
799
800 /* The capture device might have changed its output sizes */
801 ret = v4l2_subdev_call(sd, pad, get_fmt, NULL, &fmt);
802 if (ret < 0)
803 return ret;
804
805 if (mf->code != icd->current_fmt->code)
806 return -EINVAL;
807
808 if (mf->width & 7) {
809 /* Ouch! We can only handle 8-byte aligned width... */
810 stride_align(&mf->width);
811 ret = v4l2_subdev_call(sd, pad, set_fmt, NULL, &fmt);
812 if (ret < 0)
813 return ret;
814 }
815
816 if (mf->width != icd->user_width || mf->height != icd->user_height)
817 configure_geometry(mx3_cam, mf->width, mf->height,
818 icd->current_fmt->host_fmt);
819
820 dev_dbg(icd->parent, "Sensor cropped %dx%d\n",
821 mf->width, mf->height);
822
823 icd->user_width = mf->width;
824 icd->user_height = mf->height;
825
826 return ret;
827}
828
829static int mx3_camera_set_fmt(struct soc_camera_device *icd,
830 struct v4l2_format *f)
831{
832 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
833 struct mx3_camera_dev *mx3_cam = ici->priv;
834 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
835 const struct soc_camera_format_xlate *xlate;
836 struct v4l2_pix_format *pix = &f->fmt.pix;
837 struct v4l2_subdev_format format = {
838 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
839 };
840 struct v4l2_mbus_framefmt *mf = &format.format;
841 int ret;
842
843 xlate = soc_camera_xlate_by_fourcc(icd, pix->pixelformat);
844 if (!xlate) {
845 dev_warn(icd->parent, "Format %x not found\n",
846 pix->pixelformat);
847 return -EINVAL;
848 }
849
850 stride_align(&pix->width);
851 dev_dbg(icd->parent, "Set format %dx%d\n", pix->width, pix->height);
852
853 /*
854 * Might have to perform a complete interface initialisation like in
855 * ipu_csi_init_interface() in mxc_v4l2_s_param(). Also consider
856 * mxc_v4l2_s_fmt()
857 */
858
859 configure_geometry(mx3_cam, pix->width, pix->height, xlate->host_fmt);
860
861 mf->width = pix->width;
862 mf->height = pix->height;
863 mf->field = pix->field;
864 mf->colorspace = pix->colorspace;
865 mf->code = xlate->code;
866
867 ret = v4l2_subdev_call(sd, pad, set_fmt, NULL, &format);
868 if (ret < 0)
869 return ret;
870
871 if (mf->code != xlate->code)
872 return -EINVAL;
873
874 if (!mx3_cam->idmac_channel[0]) {
875 ret = acquire_dma_channel(mx3_cam);
876 if (ret < 0)
877 return ret;
878 }
879
880 pix->width = mf->width;
881 pix->height = mf->height;
882 pix->field = mf->field;
883 mx3_cam->field = mf->field;
884 pix->colorspace = mf->colorspace;
885 icd->current_fmt = xlate;
886
887 dev_dbg(icd->parent, "Sensor set %dx%d\n", pix->width, pix->height);
888
889 return ret;
890}
891
892static int mx3_camera_try_fmt(struct soc_camera_device *icd,
893 struct v4l2_format *f)
894{
895 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
896 const struct soc_camera_format_xlate *xlate;
897 struct v4l2_pix_format *pix = &f->fmt.pix;
898 struct v4l2_subdev_pad_config pad_cfg;
899 struct v4l2_subdev_format format = {
900 .which = V4L2_SUBDEV_FORMAT_TRY,
901 };
902 struct v4l2_mbus_framefmt *mf = &format.format;
903 __u32 pixfmt = pix->pixelformat;
904 int ret;
905
906 xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
907 if (pixfmt && !xlate) {
908 dev_warn(icd->parent, "Format %x not found\n", pixfmt);
909 return -EINVAL;
910 }
911
912 /* limit to MX3 hardware capabilities */
913 if (pix->height > 4096)
914 pix->height = 4096;
915 if (pix->width > 4096)
916 pix->width = 4096;
917
918 /* limit to sensor capabilities */
919 mf->width = pix->width;
920 mf->height = pix->height;
921 mf->field = pix->field;
922 mf->colorspace = pix->colorspace;
923 mf->code = xlate->code;
924
925 ret = v4l2_subdev_call(sd, pad, set_fmt, &pad_cfg, &format);
926 if (ret < 0)
927 return ret;
928
929 pix->width = mf->width;
930 pix->height = mf->height;
931 pix->colorspace = mf->colorspace;
932
933 switch (mf->field) {
934 case V4L2_FIELD_ANY:
935 pix->field = V4L2_FIELD_NONE;
936 break;
937 case V4L2_FIELD_NONE:
938 break;
939 default:
940 dev_err(icd->parent, "Field type %d unsupported.\n",
941 mf->field);
942 ret = -EINVAL;
943 }
944
945 return ret;
946}
947
948static int mx3_camera_reqbufs(struct soc_camera_device *icd,
949 struct v4l2_requestbuffers *p)
950{
951 return 0;
952}
953
954static unsigned int mx3_camera_poll(struct file *file, poll_table *pt)
955{
956 struct soc_camera_device *icd = file->private_data;
957
958 return vb2_poll(&icd->vb2_vidq, file, pt);
959}
960
961static int mx3_camera_querycap(struct soc_camera_host *ici,
962 struct v4l2_capability *cap)
963{
964 /* cap->name is set by the firendly caller:-> */
965 strlcpy(cap->card, "i.MX3x Camera", sizeof(cap->card));
966 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
967 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
968
969 return 0;
970}
971
972static int mx3_camera_set_bus_param(struct soc_camera_device *icd)
973{
974 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
975 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
976 struct mx3_camera_dev *mx3_cam = ici->priv;
977 struct v4l2_mbus_config cfg = {.type = V4L2_MBUS_PARALLEL,};
978 u32 pixfmt = icd->current_fmt->host_fmt->fourcc;
979 unsigned long bus_flags, common_flags;
980 u32 dw, sens_conf;
981 const struct soc_mbus_pixelfmt *fmt;
982 int buswidth;
983 int ret;
984 const struct soc_camera_format_xlate *xlate;
985 struct device *dev = icd->parent;
986
987 fmt = soc_mbus_get_fmtdesc(icd->current_fmt->code);
988 if (!fmt)
989 return -EINVAL;
990
991 xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
992 if (!xlate) {
993 dev_warn(dev, "Format %x not found\n", pixfmt);
994 return -EINVAL;
995 }
996
997 buswidth = fmt->bits_per_sample;
998 ret = test_platform_param(mx3_cam, buswidth, &bus_flags);
999
1000 dev_dbg(dev, "requested bus width %d bit: %d\n", buswidth, ret);
1001
1002 if (ret < 0)
1003 return ret;
1004
1005 ret = v4l2_subdev_call(sd, video, g_mbus_config, &cfg);
1006 if (!ret) {
1007 common_flags = soc_mbus_config_compatible(&cfg,
1008 bus_flags);
1009 if (!common_flags) {
1010 dev_warn(icd->parent,
1011 "Flags incompatible: camera 0x%x, host 0x%lx\n",
1012 cfg.flags, bus_flags);
1013 return -EINVAL;
1014 }
1015 } else if (ret != -ENOIOCTLCMD) {
1016 return ret;
1017 } else {
1018 common_flags = bus_flags;
1019 }
1020
1021 dev_dbg(dev, "Flags cam: 0x%x host: 0x%lx common: 0x%lx\n",
1022 cfg.flags, bus_flags, common_flags);
1023
1024 /* Make choices, based on platform preferences */
1025 if ((common_flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH) &&
1026 (common_flags & V4L2_MBUS_HSYNC_ACTIVE_LOW)) {
1027 if (mx3_cam->platform_flags & MX3_CAMERA_HSP)
1028 common_flags &= ~V4L2_MBUS_HSYNC_ACTIVE_HIGH;
1029 else
1030 common_flags &= ~V4L2_MBUS_HSYNC_ACTIVE_LOW;
1031 }
1032
1033 if ((common_flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH) &&
1034 (common_flags & V4L2_MBUS_VSYNC_ACTIVE_LOW)) {
1035 if (mx3_cam->platform_flags & MX3_CAMERA_VSP)
1036 common_flags &= ~V4L2_MBUS_VSYNC_ACTIVE_HIGH;
1037 else
1038 common_flags &= ~V4L2_MBUS_VSYNC_ACTIVE_LOW;
1039 }
1040
1041 if ((common_flags & V4L2_MBUS_DATA_ACTIVE_HIGH) &&
1042 (common_flags & V4L2_MBUS_DATA_ACTIVE_LOW)) {
1043 if (mx3_cam->platform_flags & MX3_CAMERA_DP)
1044 common_flags &= ~V4L2_MBUS_DATA_ACTIVE_HIGH;
1045 else
1046 common_flags &= ~V4L2_MBUS_DATA_ACTIVE_LOW;
1047 }
1048
1049 if ((common_flags & V4L2_MBUS_PCLK_SAMPLE_RISING) &&
1050 (common_flags & V4L2_MBUS_PCLK_SAMPLE_FALLING)) {
1051 if (mx3_cam->platform_flags & MX3_CAMERA_PCP)
1052 common_flags &= ~V4L2_MBUS_PCLK_SAMPLE_RISING;
1053 else
1054 common_flags &= ~V4L2_MBUS_PCLK_SAMPLE_FALLING;
1055 }
1056
1057 cfg.flags = common_flags;
1058 ret = v4l2_subdev_call(sd, video, s_mbus_config, &cfg);
1059 if (ret < 0 && ret != -ENOIOCTLCMD) {
1060 dev_dbg(dev, "camera s_mbus_config(0x%lx) returned %d\n",
1061 common_flags, ret);
1062 return ret;
1063 }
1064
1065 /*
1066 * So far only gated clock mode is supported. Add a line
1067 * (3 << CSI_SENS_CONF_SENS_PRTCL_SHIFT) |
1068 * below and select the required mode when supporting other
1069 * synchronisation protocols.
1070 */
1071 sens_conf = csi_reg_read(mx3_cam, CSI_SENS_CONF) &
1072 ~((1 << CSI_SENS_CONF_VSYNC_POL_SHIFT) |
1073 (1 << CSI_SENS_CONF_HSYNC_POL_SHIFT) |
1074 (1 << CSI_SENS_CONF_DATA_POL_SHIFT) |
1075 (1 << CSI_SENS_CONF_PIX_CLK_POL_SHIFT) |
1076 (3 << CSI_SENS_CONF_DATA_FMT_SHIFT) |
1077 (3 << CSI_SENS_CONF_DATA_WIDTH_SHIFT));
1078
1079 /* TODO: Support RGB and YUV formats */
1080
1081 /* This has been set in mx3_camera_activate(), but we clear it above */
1082 sens_conf |= CSI_SENS_CONF_DATA_FMT_BAYER;
1083
1084 if (common_flags & V4L2_MBUS_PCLK_SAMPLE_FALLING)
1085 sens_conf |= 1 << CSI_SENS_CONF_PIX_CLK_POL_SHIFT;
1086 if (common_flags & V4L2_MBUS_HSYNC_ACTIVE_LOW)
1087 sens_conf |= 1 << CSI_SENS_CONF_HSYNC_POL_SHIFT;
1088 if (common_flags & V4L2_MBUS_VSYNC_ACTIVE_LOW)
1089 sens_conf |= 1 << CSI_SENS_CONF_VSYNC_POL_SHIFT;
1090 if (common_flags & V4L2_MBUS_DATA_ACTIVE_LOW)
1091 sens_conf |= 1 << CSI_SENS_CONF_DATA_POL_SHIFT;
1092
1093 /* Just do what we're asked to do */
1094 switch (xlate->host_fmt->bits_per_sample) {
1095 case 4:
1096 dw = 0 << CSI_SENS_CONF_DATA_WIDTH_SHIFT;
1097 break;
1098 case 8:
1099 dw = 1 << CSI_SENS_CONF_DATA_WIDTH_SHIFT;
1100 break;
1101 case 10:
1102 dw = 2 << CSI_SENS_CONF_DATA_WIDTH_SHIFT;
1103 break;
1104 default:
1105 /*
1106 * Actually it can only be 15 now, default is just to silence
1107 * compiler warnings
1108 */
1109 case 15:
1110 dw = 3 << CSI_SENS_CONF_DATA_WIDTH_SHIFT;
1111 }
1112
1113 csi_reg_write(mx3_cam, sens_conf | dw, CSI_SENS_CONF);
1114
1115 dev_dbg(dev, "Set SENS_CONF to %x\n", sens_conf | dw);
1116
1117 return 0;
1118}
1119
1120static struct soc_camera_host_ops mx3_soc_camera_host_ops = {
1121 .owner = THIS_MODULE,
1122 .add = mx3_camera_add_device,
1123 .remove = mx3_camera_remove_device,
1124 .clock_start = mx3_camera_clock_start,
1125 .clock_stop = mx3_camera_clock_stop,
1126 .set_crop = mx3_camera_set_crop,
1127 .set_fmt = mx3_camera_set_fmt,
1128 .try_fmt = mx3_camera_try_fmt,
1129 .get_formats = mx3_camera_get_formats,
1130 .init_videobuf2 = mx3_camera_init_videobuf,
1131 .reqbufs = mx3_camera_reqbufs,
1132 .poll = mx3_camera_poll,
1133 .querycap = mx3_camera_querycap,
1134 .set_bus_param = mx3_camera_set_bus_param,
1135};
1136
1137static int mx3_camera_probe(struct platform_device *pdev)
1138{
1139 struct mx3_camera_pdata *pdata = pdev->dev.platform_data;
1140 struct mx3_camera_dev *mx3_cam;
1141 struct resource *res;
1142 void __iomem *base;
1143 int err = 0;
1144 struct soc_camera_host *soc_host;
1145
1146 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1147 base = devm_ioremap_resource(&pdev->dev, res);
1148 if (IS_ERR(base))
1149 return PTR_ERR(base);
1150
1151 if (!pdata)
1152 return -EINVAL;
1153
1154 mx3_cam = devm_kzalloc(&pdev->dev, sizeof(*mx3_cam), GFP_KERNEL);
1155 if (!mx3_cam) {
1156 dev_err(&pdev->dev, "Could not allocate mx3 camera object\n");
1157 return -ENOMEM;
1158 }
1159
1160 mx3_cam->clk = devm_clk_get(&pdev->dev, NULL);
1161 if (IS_ERR(mx3_cam->clk))
1162 return PTR_ERR(mx3_cam->clk);
1163
1164 mx3_cam->pdata = pdata;
1165 mx3_cam->platform_flags = pdata->flags;
1166 if (!(mx3_cam->platform_flags & MX3_CAMERA_DATAWIDTH_MASK)) {
1167 /*
1168 * Platform hasn't set available data widths. This is bad.
1169 * Warn and use a default.
1170 */
1171 dev_warn(&pdev->dev, "WARNING! Platform hasn't set available "
1172 "data widths, using default 8 bit\n");
1173 mx3_cam->platform_flags |= MX3_CAMERA_DATAWIDTH_8;
1174 }
1175 if (mx3_cam->platform_flags & MX3_CAMERA_DATAWIDTH_4)
1176 mx3_cam->width_flags = 1 << 3;
1177 if (mx3_cam->platform_flags & MX3_CAMERA_DATAWIDTH_8)
1178 mx3_cam->width_flags |= 1 << 7;
1179 if (mx3_cam->platform_flags & MX3_CAMERA_DATAWIDTH_10)
1180 mx3_cam->width_flags |= 1 << 9;
1181 if (mx3_cam->platform_flags & MX3_CAMERA_DATAWIDTH_15)
1182 mx3_cam->width_flags |= 1 << 14;
1183
1184 mx3_cam->mclk = pdata->mclk_10khz * 10000;
1185 if (!mx3_cam->mclk) {
1186 dev_warn(&pdev->dev,
1187 "mclk_10khz == 0! Please, fix your platform data. "
1188 "Using default 20MHz\n");
1189 mx3_cam->mclk = 20000000;
1190 }
1191
1192 /* list of video-buffers */
1193 INIT_LIST_HEAD(&mx3_cam->capture);
1194 spin_lock_init(&mx3_cam->lock);
1195
1196 mx3_cam->base = base;
1197
1198 soc_host = &mx3_cam->soc_host;
1199 soc_host->drv_name = MX3_CAM_DRV_NAME;
1200 soc_host->ops = &mx3_soc_camera_host_ops;
1201 soc_host->priv = mx3_cam;
1202 soc_host->v4l2_dev.dev = &pdev->dev;
1203 soc_host->nr = pdev->id;
1204
1205 mx3_cam->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
1206 if (IS_ERR(mx3_cam->alloc_ctx))
1207 return PTR_ERR(mx3_cam->alloc_ctx);
1208
1209 if (pdata->asd_sizes) {
1210 soc_host->asd = pdata->asd;
1211 soc_host->asd_sizes = pdata->asd_sizes;
1212 }
1213
1214 err = soc_camera_host_register(soc_host);
1215 if (err)
1216 goto ecamhostreg;
1217
1218 /* IDMAC interface */
1219 dmaengine_get();
1220
1221 return 0;
1222
1223ecamhostreg:
1224 vb2_dma_contig_cleanup_ctx(mx3_cam->alloc_ctx);
1225 return err;
1226}
1227
1228static int mx3_camera_remove(struct platform_device *pdev)
1229{
1230 struct soc_camera_host *soc_host = to_soc_camera_host(&pdev->dev);
1231 struct mx3_camera_dev *mx3_cam = container_of(soc_host,
1232 struct mx3_camera_dev, soc_host);
1233
1234 soc_camera_host_unregister(soc_host);
1235
1236 /*
1237 * The channel has either not been allocated,
1238 * or should have been released
1239 */
1240 if (WARN_ON(mx3_cam->idmac_channel[0]))
1241 dma_release_channel(&mx3_cam->idmac_channel[0]->dma_chan);
1242
1243 vb2_dma_contig_cleanup_ctx(mx3_cam->alloc_ctx);
1244
1245 dmaengine_put();
1246
1247 return 0;
1248}
1249
1250static struct platform_driver mx3_camera_driver = {
1251 .driver = {
1252 .name = MX3_CAM_DRV_NAME,
1253 },
1254 .probe = mx3_camera_probe,
1255 .remove = mx3_camera_remove,
1256};
1257
1258module_platform_driver(mx3_camera_driver);
1259
1260MODULE_DESCRIPTION("i.MX3x SoC Camera Host driver");
1261MODULE_AUTHOR("Guennadi Liakhovetski <lg@denx.de>");
1262MODULE_LICENSE("GPL v2");
1263MODULE_VERSION("0.2.3");
1264MODULE_ALIAS("platform:" MX3_CAM_DRV_NAME);
diff --git a/drivers/staging/media/omap1/Kconfig b/drivers/staging/media/omap1/Kconfig
deleted file mode 100644
index 6cfab3a04ae1..000000000000
--- a/drivers/staging/media/omap1/Kconfig
+++ /dev/null
@@ -1,13 +0,0 @@
1config VIDEO_OMAP1
2 tristate "OMAP1 Camera Interface driver"
3 depends on VIDEO_DEV && SOC_CAMERA
4 depends on ARCH_OMAP1
5 depends on HAS_DMA
6 select VIDEOBUF_DMA_CONTIG
7 select VIDEOBUF_DMA_SG
8 ---help---
9 This is a v4l2 driver for the TI OMAP1 camera interface
10
11 This driver is deprecated and will be removed soon unless someone
12 will start the work to convert this driver to the vb2 framework
13 and remove the soc-camera dependency.
diff --git a/drivers/staging/media/omap1/Makefile b/drivers/staging/media/omap1/Makefile
deleted file mode 100644
index 2885622600f2..000000000000
--- a/drivers/staging/media/omap1/Makefile
+++ /dev/null
@@ -1,3 +0,0 @@
1# Makefile for OMAP1 driver
2
3obj-$(CONFIG_VIDEO_OMAP1) += omap1_camera.o
diff --git a/drivers/staging/media/omap1/TODO b/drivers/staging/media/omap1/TODO
deleted file mode 100644
index 1025f9f60ff0..000000000000
--- a/drivers/staging/media/omap1/TODO
+++ /dev/null
@@ -1,8 +0,0 @@
1This driver is deprecated and will be removed soon unless someone will start
2the work to convert this driver to the vb2 framework and remove the
3soc-camera dependency.
4
5Note that trivial patches will not be accepted anymore, only a full conversion.
6
7If you want to convert this driver, please contact the linux-media mailinglist
8(see http://linuxtv.org/lists.php).
diff --git a/drivers/staging/media/omap1/omap1_camera.c b/drivers/staging/media/omap1/omap1_camera.c
deleted file mode 100644
index 54b8dd2d2bba..000000000000
--- a/drivers/staging/media/omap1/omap1_camera.c
+++ /dev/null
@@ -1,1702 +0,0 @@
1/*
2 * V4L2 SoC Camera driver for OMAP1 Camera Interface
3 *
4 * Copyright (C) 2010, Janusz Krzysztofik <jkrzyszt@tis.icnet.pl>
5 *
6 * Based on V4L2 Driver for i.MXL/i.MXL camera (CSI) host
7 * Copyright (C) 2008, Paulius Zaleckas <paulius.zaleckas@teltonika.lt>
8 * Copyright (C) 2009, Darius Augulis <augulis.darius@gmail.com>
9 *
10 * Based on PXA SoC camera driver
11 * Copyright (C) 2006, Sascha Hauer, Pengutronix
12 * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.de>
13 *
14 * Hardware specific bits initialy based on former work by Matt Callow
15 * drivers/media/platform/omap/omap1510cam.c
16 * Copyright (C) 2006 Matt Callow
17 *
18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License version 2 as
20 * published by the Free Software Foundation.
21 */
22
23
24#include <linux/clk.h>
25#include <linux/dma-mapping.h>
26#include <linux/interrupt.h>
27#include <linux/module.h>
28#include <linux/platform_device.h>
29#include <linux/slab.h>
30
31#include <linux/platform_data/media/omap1_camera.h>
32#include <media/soc_camera.h>
33#include <media/drv-intf/soc_mediabus.h>
34#include <media/videobuf-dma-contig.h>
35#include <media/videobuf-dma-sg.h>
36
37#include <linux/omap-dma.h>
38
39
40#define DRIVER_NAME "omap1-camera"
41#define DRIVER_VERSION "0.0.2"
42
43#define OMAP_DMA_CAMERA_IF_RX 20
44
45/*
46 * ---------------------------------------------------------------------------
47 * OMAP1 Camera Interface registers
48 * ---------------------------------------------------------------------------
49 */
50
51#define REG_CTRLCLOCK 0x00
52#define REG_IT_STATUS 0x04
53#define REG_MODE 0x08
54#define REG_STATUS 0x0C
55#define REG_CAMDATA 0x10
56#define REG_GPIO 0x14
57#define REG_PEAK_COUNTER 0x18
58
59/* CTRLCLOCK bit shifts */
60#define LCLK_EN BIT(7)
61#define DPLL_EN BIT(6)
62#define MCLK_EN BIT(5)
63#define CAMEXCLK_EN BIT(4)
64#define POLCLK BIT(3)
65#define FOSCMOD_SHIFT 0
66#define FOSCMOD_MASK (0x7 << FOSCMOD_SHIFT)
67#define FOSCMOD_12MHz 0x0
68#define FOSCMOD_6MHz 0x2
69#define FOSCMOD_9_6MHz 0x4
70#define FOSCMOD_24MHz 0x5
71#define FOSCMOD_8MHz 0x6
72
73/* IT_STATUS bit shifts */
74#define DATA_TRANSFER BIT(5)
75#define FIFO_FULL BIT(4)
76#define H_DOWN BIT(3)
77#define H_UP BIT(2)
78#define V_DOWN BIT(1)
79#define V_UP BIT(0)
80
81/* MODE bit shifts */
82#define RAZ_FIFO BIT(18)
83#define EN_FIFO_FULL BIT(17)
84#define EN_NIRQ BIT(16)
85#define THRESHOLD_SHIFT 9
86#define THRESHOLD_MASK (0x7f << THRESHOLD_SHIFT)
87#define DMA BIT(8)
88#define EN_H_DOWN BIT(7)
89#define EN_H_UP BIT(6)
90#define EN_V_DOWN BIT(5)
91#define EN_V_UP BIT(4)
92#define ORDERCAMD BIT(3)
93
94#define IRQ_MASK (EN_V_UP | EN_V_DOWN | EN_H_UP | EN_H_DOWN | \
95 EN_NIRQ | EN_FIFO_FULL)
96
97/* STATUS bit shifts */
98#define HSTATUS BIT(1)
99#define VSTATUS BIT(0)
100
101/* GPIO bit shifts */
102#define CAM_RST BIT(0)
103
104/* end of OMAP1 Camera Interface registers */
105
106
107#define SOCAM_BUS_FLAGS (V4L2_MBUS_MASTER | \
108 V4L2_MBUS_HSYNC_ACTIVE_HIGH | V4L2_MBUS_VSYNC_ACTIVE_HIGH | \
109 V4L2_MBUS_PCLK_SAMPLE_RISING | V4L2_MBUS_PCLK_SAMPLE_FALLING | \
110 V4L2_MBUS_DATA_ACTIVE_HIGH)
111
112
113#define FIFO_SIZE ((THRESHOLD_MASK >> THRESHOLD_SHIFT) + 1)
114#define FIFO_SHIFT __fls(FIFO_SIZE)
115
116#define DMA_BURST_SHIFT (1 + OMAP_DMA_DATA_BURST_4)
117#define DMA_BURST_SIZE (1 << DMA_BURST_SHIFT)
118
119#define DMA_ELEMENT_SHIFT OMAP_DMA_DATA_TYPE_S32
120#define DMA_ELEMENT_SIZE (1 << DMA_ELEMENT_SHIFT)
121
122#define DMA_FRAME_SHIFT_CONTIG (FIFO_SHIFT - 1)
123#define DMA_FRAME_SHIFT_SG DMA_BURST_SHIFT
124
125#define DMA_FRAME_SHIFT(x) ((x) == OMAP1_CAM_DMA_CONTIG ? \
126 DMA_FRAME_SHIFT_CONTIG : \
127 DMA_FRAME_SHIFT_SG)
128#define DMA_FRAME_SIZE(x) (1 << DMA_FRAME_SHIFT(x))
129#define DMA_SYNC OMAP_DMA_SYNC_FRAME
130#define THRESHOLD_LEVEL DMA_FRAME_SIZE
131
132
133#define MAX_VIDEO_MEM 4 /* arbitrary video memory limit in MB */
134
135
136/*
137 * Structures
138 */
139
140/* buffer for one video frame */
141struct omap1_cam_buf {
142 struct videobuf_buffer vb;
143 u32 code;
144 int inwork;
145 struct scatterlist *sgbuf;
146 int sgcount;
147 int bytes_left;
148 enum videobuf_state result;
149};
150
151struct omap1_cam_dev {
152 struct soc_camera_host soc_host;
153 struct clk *clk;
154
155 unsigned int irq;
156 void __iomem *base;
157
158 int dma_ch;
159
160 struct omap1_cam_platform_data *pdata;
161 struct resource *res;
162 unsigned long pflags;
163 unsigned long camexclk;
164
165 struct list_head capture;
166
167 /* lock used to protect videobuf */
168 spinlock_t lock;
169
170 /* Pointers to DMA buffers */
171 struct omap1_cam_buf *active;
172 struct omap1_cam_buf *ready;
173
174 enum omap1_cam_vb_mode vb_mode;
175 int (*mmap_mapper)(struct videobuf_queue *q,
176 struct videobuf_buffer *buf,
177 struct vm_area_struct *vma);
178
179 u32 reg_cache[0];
180};
181
182
183static void cam_write(struct omap1_cam_dev *pcdev, u16 reg, u32 val)
184{
185 pcdev->reg_cache[reg / sizeof(u32)] = val;
186 __raw_writel(val, pcdev->base + reg);
187}
188
189static u32 cam_read(struct omap1_cam_dev *pcdev, u16 reg, bool from_cache)
190{
191 return !from_cache ? __raw_readl(pcdev->base + reg) :
192 pcdev->reg_cache[reg / sizeof(u32)];
193}
194
195#define CAM_READ(pcdev, reg) \
196 cam_read(pcdev, REG_##reg, false)
197#define CAM_WRITE(pcdev, reg, val) \
198 cam_write(pcdev, REG_##reg, val)
199#define CAM_READ_CACHE(pcdev, reg) \
200 cam_read(pcdev, REG_##reg, true)
201
202/*
203 * Videobuf operations
204 */
205static int omap1_videobuf_setup(struct videobuf_queue *vq, unsigned int *count,
206 unsigned int *size)
207{
208 struct soc_camera_device *icd = vq->priv_data;
209 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
210 struct omap1_cam_dev *pcdev = ici->priv;
211
212 *size = icd->sizeimage;
213
214 if (!*count || *count < OMAP1_CAMERA_MIN_BUF_COUNT(pcdev->vb_mode))
215 *count = OMAP1_CAMERA_MIN_BUF_COUNT(pcdev->vb_mode);
216
217 if (*size * *count > MAX_VIDEO_MEM * 1024 * 1024)
218 *count = (MAX_VIDEO_MEM * 1024 * 1024) / *size;
219
220 dev_dbg(icd->parent,
221 "%s: count=%d, size=%d\n", __func__, *count, *size);
222
223 return 0;
224}
225
226static void free_buffer(struct videobuf_queue *vq, struct omap1_cam_buf *buf,
227 enum omap1_cam_vb_mode vb_mode)
228{
229 struct videobuf_buffer *vb = &buf->vb;
230
231 BUG_ON(in_interrupt());
232
233 videobuf_waiton(vq, vb, 0, 0);
234
235 if (vb_mode == OMAP1_CAM_DMA_CONTIG) {
236 videobuf_dma_contig_free(vq, vb);
237 } else {
238 struct soc_camera_device *icd = vq->priv_data;
239 struct device *dev = icd->parent;
240 struct videobuf_dmabuf *dma = videobuf_to_dma(vb);
241
242 videobuf_dma_unmap(dev, dma);
243 videobuf_dma_free(dma);
244 }
245
246 vb->state = VIDEOBUF_NEEDS_INIT;
247}
248
249static int omap1_videobuf_prepare(struct videobuf_queue *vq,
250 struct videobuf_buffer *vb, enum v4l2_field field)
251{
252 struct soc_camera_device *icd = vq->priv_data;
253 struct omap1_cam_buf *buf = container_of(vb, struct omap1_cam_buf, vb);
254 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
255 struct omap1_cam_dev *pcdev = ici->priv;
256 int ret;
257
258 WARN_ON(!list_empty(&vb->queue));
259
260 BUG_ON(NULL == icd->current_fmt);
261
262 buf->inwork = 1;
263
264 if (buf->code != icd->current_fmt->code || vb->field != field ||
265 vb->width != icd->user_width ||
266 vb->height != icd->user_height) {
267 buf->code = icd->current_fmt->code;
268 vb->width = icd->user_width;
269 vb->height = icd->user_height;
270 vb->field = field;
271 vb->state = VIDEOBUF_NEEDS_INIT;
272 }
273
274 vb->size = icd->sizeimage;
275
276 if (vb->baddr && vb->bsize < vb->size) {
277 ret = -EINVAL;
278 goto out;
279 }
280
281 if (vb->state == VIDEOBUF_NEEDS_INIT) {
282 ret = videobuf_iolock(vq, vb, NULL);
283 if (ret)
284 goto fail;
285
286 vb->state = VIDEOBUF_PREPARED;
287 }
288 buf->inwork = 0;
289
290 return 0;
291fail:
292 free_buffer(vq, buf, pcdev->vb_mode);
293out:
294 buf->inwork = 0;
295 return ret;
296}
297
298static void set_dma_dest_params(int dma_ch, struct omap1_cam_buf *buf,
299 enum omap1_cam_vb_mode vb_mode)
300{
301 dma_addr_t dma_addr;
302 unsigned int block_size;
303
304 if (vb_mode == OMAP1_CAM_DMA_CONTIG) {
305 dma_addr = videobuf_to_dma_contig(&buf->vb);
306 block_size = buf->vb.size;
307 } else {
308 if (WARN_ON(!buf->sgbuf)) {
309 buf->result = VIDEOBUF_ERROR;
310 return;
311 }
312 dma_addr = sg_dma_address(buf->sgbuf);
313 if (WARN_ON(!dma_addr)) {
314 buf->sgbuf = NULL;
315 buf->result = VIDEOBUF_ERROR;
316 return;
317 }
318 block_size = sg_dma_len(buf->sgbuf);
319 if (WARN_ON(!block_size)) {
320 buf->sgbuf = NULL;
321 buf->result = VIDEOBUF_ERROR;
322 return;
323 }
324 if (unlikely(buf->bytes_left < block_size))
325 block_size = buf->bytes_left;
326 if (WARN_ON(dma_addr & (DMA_FRAME_SIZE(vb_mode) *
327 DMA_ELEMENT_SIZE - 1))) {
328 dma_addr = ALIGN(dma_addr, DMA_FRAME_SIZE(vb_mode) *
329 DMA_ELEMENT_SIZE);
330 block_size &= ~(DMA_FRAME_SIZE(vb_mode) *
331 DMA_ELEMENT_SIZE - 1);
332 }
333 buf->bytes_left -= block_size;
334 buf->sgcount++;
335 }
336
337 omap_set_dma_dest_params(dma_ch,
338 OMAP_DMA_PORT_EMIFF, OMAP_DMA_AMODE_POST_INC, dma_addr, 0, 0);
339 omap_set_dma_transfer_params(dma_ch,
340 OMAP_DMA_DATA_TYPE_S32, DMA_FRAME_SIZE(vb_mode),
341 block_size >> (DMA_FRAME_SHIFT(vb_mode) + DMA_ELEMENT_SHIFT),
342 DMA_SYNC, 0, 0);
343}
344
345static struct omap1_cam_buf *prepare_next_vb(struct omap1_cam_dev *pcdev)
346{
347 struct omap1_cam_buf *buf;
348
349 /*
350 * If there is already a buffer pointed out by the pcdev->ready,
351 * (re)use it, otherwise try to fetch and configure a new one.
352 */
353 buf = pcdev->ready;
354 if (!buf) {
355 if (list_empty(&pcdev->capture))
356 return buf;
357 buf = list_entry(pcdev->capture.next,
358 struct omap1_cam_buf, vb.queue);
359 buf->vb.state = VIDEOBUF_ACTIVE;
360 pcdev->ready = buf;
361 list_del_init(&buf->vb.queue);
362 }
363
364 if (pcdev->vb_mode == OMAP1_CAM_DMA_CONTIG) {
365 /*
366 * In CONTIG mode, we can safely enter next buffer parameters
367 * into the DMA programming register set after the DMA
368 * has already been activated on the previous buffer
369 */
370 set_dma_dest_params(pcdev->dma_ch, buf, pcdev->vb_mode);
371 } else {
372 /*
373 * In SG mode, the above is not safe since there are probably
374 * a bunch of sgbufs from previous sglist still pending.
375 * Instead, mark the sglist fresh for the upcoming
376 * try_next_sgbuf().
377 */
378 buf->sgbuf = NULL;
379 }
380
381 return buf;
382}
383
384static struct scatterlist *try_next_sgbuf(int dma_ch, struct omap1_cam_buf *buf)
385{
386 struct scatterlist *sgbuf;
387
388 if (likely(buf->sgbuf)) {
389 /* current sglist is active */
390 if (unlikely(!buf->bytes_left)) {
391 /* indicate sglist complete */
392 sgbuf = NULL;
393 } else {
394 /* process next sgbuf */
395 sgbuf = sg_next(buf->sgbuf);
396 if (WARN_ON(!sgbuf)) {
397 buf->result = VIDEOBUF_ERROR;
398 } else if (WARN_ON(!sg_dma_len(sgbuf))) {
399 sgbuf = NULL;
400 buf->result = VIDEOBUF_ERROR;
401 }
402 }
403 buf->sgbuf = sgbuf;
404 } else {
405 /* sglist is fresh, initialize it before using */
406 struct videobuf_dmabuf *dma = videobuf_to_dma(&buf->vb);
407
408 sgbuf = dma->sglist;
409 if (!(WARN_ON(!sgbuf))) {
410 buf->sgbuf = sgbuf;
411 buf->sgcount = 0;
412 buf->bytes_left = buf->vb.size;
413 buf->result = VIDEOBUF_DONE;
414 }
415 }
416 if (sgbuf)
417 /*
418 * Put our next sgbuf parameters (address, size)
419 * into the DMA programming register set.
420 */
421 set_dma_dest_params(dma_ch, buf, OMAP1_CAM_DMA_SG);
422
423 return sgbuf;
424}
425
426static void start_capture(struct omap1_cam_dev *pcdev)
427{
428 struct omap1_cam_buf *buf = pcdev->active;
429 u32 ctrlclock = CAM_READ_CACHE(pcdev, CTRLCLOCK);
430 u32 mode = CAM_READ_CACHE(pcdev, MODE) & ~EN_V_DOWN;
431
432 if (WARN_ON(!buf))
433 return;
434
435 /*
436 * Enable start of frame interrupt, which we will use for activating
437 * our end of frame watchdog when capture actually starts.
438 */
439 mode |= EN_V_UP;
440
441 if (unlikely(ctrlclock & LCLK_EN))
442 /* stop pixel clock before FIFO reset */
443 CAM_WRITE(pcdev, CTRLCLOCK, ctrlclock & ~LCLK_EN);
444 /* reset FIFO */
445 CAM_WRITE(pcdev, MODE, mode | RAZ_FIFO);
446
447 omap_start_dma(pcdev->dma_ch);
448
449 if (pcdev->vb_mode == OMAP1_CAM_DMA_SG) {
450 /*
451 * In SG mode, it's a good moment for fetching next sgbuf
452 * from the current sglist and, if available, already putting
453 * its parameters into the DMA programming register set.
454 */
455 try_next_sgbuf(pcdev->dma_ch, buf);
456 }
457
458 /* (re)enable pixel clock */
459 CAM_WRITE(pcdev, CTRLCLOCK, ctrlclock | LCLK_EN);
460 /* release FIFO reset */
461 CAM_WRITE(pcdev, MODE, mode);
462}
463
464static void suspend_capture(struct omap1_cam_dev *pcdev)
465{
466 u32 ctrlclock = CAM_READ_CACHE(pcdev, CTRLCLOCK);
467
468 CAM_WRITE(pcdev, CTRLCLOCK, ctrlclock & ~LCLK_EN);
469 omap_stop_dma(pcdev->dma_ch);
470}
471
472static void disable_capture(struct omap1_cam_dev *pcdev)
473{
474 u32 mode = CAM_READ_CACHE(pcdev, MODE);
475
476 CAM_WRITE(pcdev, MODE, mode & ~(IRQ_MASK | DMA));
477}
478
479static void omap1_videobuf_queue(struct videobuf_queue *vq,
480 struct videobuf_buffer *vb)
481{
482 struct soc_camera_device *icd = vq->priv_data;
483 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
484 struct omap1_cam_dev *pcdev = ici->priv;
485 struct omap1_cam_buf *buf;
486 u32 mode;
487
488 list_add_tail(&vb->queue, &pcdev->capture);
489 vb->state = VIDEOBUF_QUEUED;
490
491 if (pcdev->active) {
492 /*
493 * Capture in progress, so don't touch pcdev->ready even if
494 * empty. Since the transfer of the DMA programming register set
495 * content to the DMA working register set is done automatically
496 * by the DMA hardware, this can pretty well happen while we
497 * are keeping the lock here. Leave fetching it from the queue
498 * to be done when a next DMA interrupt occures instead.
499 */
500 return;
501 }
502
503 WARN_ON(pcdev->ready);
504
505 buf = prepare_next_vb(pcdev);
506 if (WARN_ON(!buf))
507 return;
508
509 pcdev->active = buf;
510 pcdev->ready = NULL;
511
512 dev_dbg(icd->parent,
513 "%s: capture not active, setup FIFO, start DMA\n", __func__);
514 mode = CAM_READ_CACHE(pcdev, MODE) & ~THRESHOLD_MASK;
515 mode |= THRESHOLD_LEVEL(pcdev->vb_mode) << THRESHOLD_SHIFT;
516 CAM_WRITE(pcdev, MODE, mode | EN_FIFO_FULL | DMA);
517
518 if (pcdev->vb_mode == OMAP1_CAM_DMA_SG) {
519 /*
520 * In SG mode, the above prepare_next_vb() didn't actually
521 * put anything into the DMA programming register set,
522 * so we have to do it now, before activating DMA.
523 */
524 try_next_sgbuf(pcdev->dma_ch, buf);
525 }
526
527 start_capture(pcdev);
528}
529
530static void omap1_videobuf_release(struct videobuf_queue *vq,
531 struct videobuf_buffer *vb)
532{
533 struct omap1_cam_buf *buf =
534 container_of(vb, struct omap1_cam_buf, vb);
535 struct soc_camera_device *icd = vq->priv_data;
536 struct device *dev = icd->parent;
537 struct soc_camera_host *ici = to_soc_camera_host(dev);
538 struct omap1_cam_dev *pcdev = ici->priv;
539
540 switch (vb->state) {
541 case VIDEOBUF_DONE:
542 dev_dbg(dev, "%s (done)\n", __func__);
543 break;
544 case VIDEOBUF_ACTIVE:
545 dev_dbg(dev, "%s (active)\n", __func__);
546 break;
547 case VIDEOBUF_QUEUED:
548 dev_dbg(dev, "%s (queued)\n", __func__);
549 break;
550 case VIDEOBUF_PREPARED:
551 dev_dbg(dev, "%s (prepared)\n", __func__);
552 break;
553 default:
554 dev_dbg(dev, "%s (unknown %d)\n", __func__, vb->state);
555 break;
556 }
557
558 free_buffer(vq, buf, pcdev->vb_mode);
559}
560
561static void videobuf_done(struct omap1_cam_dev *pcdev,
562 enum videobuf_state result)
563{
564 struct omap1_cam_buf *buf = pcdev->active;
565 struct videobuf_buffer *vb;
566 struct device *dev = pcdev->soc_host.icd->parent;
567
568 if (WARN_ON(!buf)) {
569 suspend_capture(pcdev);
570 disable_capture(pcdev);
571 return;
572 }
573
574 if (result == VIDEOBUF_ERROR)
575 suspend_capture(pcdev);
576
577 vb = &buf->vb;
578 if (waitqueue_active(&vb->done)) {
579 if (!pcdev->ready && result != VIDEOBUF_ERROR) {
580 /*
581 * No next buffer has been entered into the DMA
582 * programming register set on time (could be done only
583 * while the previous DMA interurpt was processed, not
584 * later), so the last DMA block, be it a whole buffer
585 * if in CONTIG or its last sgbuf if in SG mode, is
586 * about to be reused by the just autoreinitialized DMA
587 * engine, and overwritten with next frame data. Best we
588 * can do is stopping the capture as soon as possible,
589 * hopefully before the next frame start.
590 */
591 suspend_capture(pcdev);
592 }
593 vb->state = result;
594 v4l2_get_timestamp(&vb->ts);
595 if (result != VIDEOBUF_ERROR)
596 vb->field_count++;
597 wake_up(&vb->done);
598
599 /* shift in next buffer */
600 buf = pcdev->ready;
601 pcdev->active = buf;
602 pcdev->ready = NULL;
603
604 if (!buf) {
605 /*
606 * No next buffer was ready on time (see above), so
607 * indicate error condition to force capture restart or
608 * stop, depending on next buffer already queued or not.
609 */
610 result = VIDEOBUF_ERROR;
611 prepare_next_vb(pcdev);
612
613 buf = pcdev->ready;
614 pcdev->active = buf;
615 pcdev->ready = NULL;
616 }
617 } else if (pcdev->ready) {
618 /*
619 * In both CONTIG and SG mode, the DMA engine has possibly
620 * been already autoreinitialized with the preprogrammed
621 * pcdev->ready buffer. We can either accept this fact
622 * and just swap the buffers, or provoke an error condition
623 * and restart capture. The former seems less intrusive.
624 */
625 dev_dbg(dev, "%s: nobody waiting on videobuf, swap with next\n",
626 __func__);
627 pcdev->active = pcdev->ready;
628
629 if (pcdev->vb_mode == OMAP1_CAM_DMA_SG) {
630 /*
631 * In SG mode, we have to make sure that the buffer we
632 * are putting back into the pcdev->ready is marked
633 * fresh.
634 */
635 buf->sgbuf = NULL;
636 }
637 pcdev->ready = buf;
638
639 buf = pcdev->active;
640 } else {
641 /*
642 * No next buffer has been entered into
643 * the DMA programming register set on time.
644 */
645 if (pcdev->vb_mode == OMAP1_CAM_DMA_CONTIG) {
646 /*
647 * In CONTIG mode, the DMA engine has already been
648 * reinitialized with the current buffer. Best we can do
649 * is not touching it.
650 */
651 dev_dbg(dev,
652 "%s: nobody waiting on videobuf, reuse it\n",
653 __func__);
654 } else {
655 /*
656 * In SG mode, the DMA engine has just been
657 * autoreinitialized with the last sgbuf from the
658 * current list. Restart capture in order to transfer
659 * next frame start into the first sgbuf, not the last
660 * one.
661 */
662 if (result != VIDEOBUF_ERROR) {
663 suspend_capture(pcdev);
664 result = VIDEOBUF_ERROR;
665 }
666 }
667 }
668
669 if (!buf) {
670 dev_dbg(dev, "%s: no more videobufs, stop capture\n", __func__);
671 disable_capture(pcdev);
672 return;
673 }
674
675 if (pcdev->vb_mode == OMAP1_CAM_DMA_CONTIG) {
676 /*
677 * In CONTIG mode, the current buffer parameters had already
678 * been entered into the DMA programming register set while the
679 * buffer was fetched with prepare_next_vb(), they may have also
680 * been transferred into the runtime set and already active if
681 * the DMA still running.
682 */
683 } else {
684 /* In SG mode, extra steps are required */
685 if (result == VIDEOBUF_ERROR)
686 /* make sure we (re)use sglist from start on error */
687 buf->sgbuf = NULL;
688
689 /*
690 * In any case, enter the next sgbuf parameters into the DMA
691 * programming register set. They will be used either during
692 * nearest DMA autoreinitialization or, in case of an error,
693 * on DMA startup below.
694 */
695 try_next_sgbuf(pcdev->dma_ch, buf);
696 }
697
698 if (result == VIDEOBUF_ERROR) {
699 dev_dbg(dev, "%s: videobuf error; reset FIFO, restart DMA\n",
700 __func__);
701 start_capture(pcdev);
702 /*
703 * In SG mode, the above also resulted in the next sgbuf
704 * parameters being entered into the DMA programming register
705 * set, making them ready for next DMA autoreinitialization.
706 */
707 }
708
709 /*
710 * Finally, try fetching next buffer.
711 * In CONTIG mode, it will also enter it into the DMA programming
712 * register set, making it ready for next DMA autoreinitialization.
713 */
714 prepare_next_vb(pcdev);
715}
716
717static void dma_isr(int channel, unsigned short status, void *data)
718{
719 struct omap1_cam_dev *pcdev = data;
720 struct omap1_cam_buf *buf = pcdev->active;
721 unsigned long flags;
722
723 spin_lock_irqsave(&pcdev->lock, flags);
724
725 if (WARN_ON(!buf)) {
726 suspend_capture(pcdev);
727 disable_capture(pcdev);
728 goto out;
729 }
730
731 if (pcdev->vb_mode == OMAP1_CAM_DMA_CONTIG) {
732 /*
733 * In CONTIG mode, assume we have just managed to collect the
734 * whole frame, hopefully before our end of frame watchdog is
735 * triggered. Then, all we have to do is disabling the watchdog
736 * for this frame, and calling videobuf_done() with success
737 * indicated.
738 */
739 CAM_WRITE(pcdev, MODE,
740 CAM_READ_CACHE(pcdev, MODE) & ~EN_V_DOWN);
741 videobuf_done(pcdev, VIDEOBUF_DONE);
742 } else {
743 /*
744 * In SG mode, we have to process every sgbuf from the current
745 * sglist, one after another.
746 */
747 if (buf->sgbuf) {
748 /*
749 * Current sglist not completed yet, try fetching next
750 * sgbuf, hopefully putting it into the DMA programming
751 * register set, making it ready for next DMA
752 * autoreinitialization.
753 */
754 try_next_sgbuf(pcdev->dma_ch, buf);
755 if (buf->sgbuf)
756 goto out;
757
758 /*
759 * No more sgbufs left in the current sglist. This
760 * doesn't mean that the whole videobuffer is already
761 * complete, but only that the last sgbuf from the
762 * current sglist is about to be filled. It will be
763 * ready on next DMA interrupt, signalled with the
764 * buf->sgbuf set back to NULL.
765 */
766 if (buf->result != VIDEOBUF_ERROR) {
767 /*
768 * Video frame collected without errors so far,
769 * we can prepare for collecting a next one
770 * as soon as DMA gets autoreinitialized
771 * after the current (last) sgbuf is completed.
772 */
773 buf = prepare_next_vb(pcdev);
774 if (!buf)
775 goto out;
776
777 try_next_sgbuf(pcdev->dma_ch, buf);
778 goto out;
779 }
780 }
781 /* end of videobuf */
782 videobuf_done(pcdev, buf->result);
783 }
784
785out:
786 spin_unlock_irqrestore(&pcdev->lock, flags);
787}
788
789static irqreturn_t cam_isr(int irq, void *data)
790{
791 struct omap1_cam_dev *pcdev = data;
792 struct device *dev = pcdev->soc_host.icd->parent;
793 struct omap1_cam_buf *buf = pcdev->active;
794 u32 it_status;
795 unsigned long flags;
796
797 it_status = CAM_READ(pcdev, IT_STATUS);
798 if (!it_status)
799 return IRQ_NONE;
800
801 spin_lock_irqsave(&pcdev->lock, flags);
802
803 if (WARN_ON(!buf)) {
804 dev_warn(dev, "%s: unhandled camera interrupt, status == %#x\n",
805 __func__, it_status);
806 suspend_capture(pcdev);
807 disable_capture(pcdev);
808 goto out;
809 }
810
811 if (unlikely(it_status & FIFO_FULL)) {
812 dev_warn(dev, "%s: FIFO overflow\n", __func__);
813
814 } else if (it_status & V_DOWN) {
815 /* end of video frame watchdog */
816 if (pcdev->vb_mode == OMAP1_CAM_DMA_CONTIG) {
817 /*
818 * In CONTIG mode, the watchdog is disabled with
819 * successful DMA end of block interrupt, and reenabled
820 * on next frame start. If we get here, there is nothing
821 * to check, we must be out of sync.
822 */
823 } else {
824 if (buf->sgcount == 2) {
825 /*
826 * If exactly 2 sgbufs from the next sglist have
827 * been programmed into the DMA engine (the
828 * first one already transferred into the DMA
829 * runtime register set, the second one still
830 * in the programming set), then we are in sync.
831 */
832 goto out;
833 }
834 }
835 dev_notice(dev, "%s: unexpected end of video frame\n",
836 __func__);
837
838 } else if (it_status & V_UP) {
839 u32 mode;
840
841 if (pcdev->vb_mode == OMAP1_CAM_DMA_CONTIG) {
842 /*
843 * In CONTIG mode, we need this interrupt every frame
844 * in oredr to reenable our end of frame watchdog.
845 */
846 mode = CAM_READ_CACHE(pcdev, MODE);
847 } else {
848 /*
849 * In SG mode, the below enabled end of frame watchdog
850 * is kept on permanently, so we can turn this one shot
851 * setup off.
852 */
853 mode = CAM_READ_CACHE(pcdev, MODE) & ~EN_V_UP;
854 }
855
856 if (!(mode & EN_V_DOWN)) {
857 /* (re)enable end of frame watchdog interrupt */
858 mode |= EN_V_DOWN;
859 }
860 CAM_WRITE(pcdev, MODE, mode);
861 goto out;
862
863 } else {
864 dev_warn(dev, "%s: unhandled camera interrupt, status == %#x\n",
865 __func__, it_status);
866 goto out;
867 }
868
869 videobuf_done(pcdev, VIDEOBUF_ERROR);
870out:
871 spin_unlock_irqrestore(&pcdev->lock, flags);
872 return IRQ_HANDLED;
873}
874
875static struct videobuf_queue_ops omap1_videobuf_ops = {
876 .buf_setup = omap1_videobuf_setup,
877 .buf_prepare = omap1_videobuf_prepare,
878 .buf_queue = omap1_videobuf_queue,
879 .buf_release = omap1_videobuf_release,
880};
881
882
883/*
884 * SOC Camera host operations
885 */
886
887static void sensor_reset(struct omap1_cam_dev *pcdev, bool reset)
888{
889 /* apply/release camera sensor reset if requested by platform data */
890 if (pcdev->pflags & OMAP1_CAMERA_RST_HIGH)
891 CAM_WRITE(pcdev, GPIO, reset);
892 else if (pcdev->pflags & OMAP1_CAMERA_RST_LOW)
893 CAM_WRITE(pcdev, GPIO, !reset);
894}
895
896static int omap1_cam_add_device(struct soc_camera_device *icd)
897{
898 dev_dbg(icd->parent, "OMAP1 Camera driver attached to camera %d\n",
899 icd->devnum);
900
901 return 0;
902}
903
904static void omap1_cam_remove_device(struct soc_camera_device *icd)
905{
906 dev_dbg(icd->parent,
907 "OMAP1 Camera driver detached from camera %d\n", icd->devnum);
908}
909
910/*
911 * The following two functions absolutely depend on the fact, that
912 * there can be only one camera on OMAP1 camera sensor interface
913 */
914static int omap1_cam_clock_start(struct soc_camera_host *ici)
915{
916 struct omap1_cam_dev *pcdev = ici->priv;
917 u32 ctrlclock;
918
919 clk_enable(pcdev->clk);
920
921 /* setup sensor clock */
922 ctrlclock = CAM_READ(pcdev, CTRLCLOCK);
923 ctrlclock &= ~(CAMEXCLK_EN | MCLK_EN | DPLL_EN);
924 CAM_WRITE(pcdev, CTRLCLOCK, ctrlclock);
925
926 ctrlclock &= ~FOSCMOD_MASK;
927 switch (pcdev->camexclk) {
928 case 6000000:
929 ctrlclock |= CAMEXCLK_EN | FOSCMOD_6MHz;
930 break;
931 case 8000000:
932 ctrlclock |= CAMEXCLK_EN | FOSCMOD_8MHz | DPLL_EN;
933 break;
934 case 9600000:
935 ctrlclock |= CAMEXCLK_EN | FOSCMOD_9_6MHz | DPLL_EN;
936 break;
937 case 12000000:
938 ctrlclock |= CAMEXCLK_EN | FOSCMOD_12MHz;
939 break;
940 case 24000000:
941 ctrlclock |= CAMEXCLK_EN | FOSCMOD_24MHz | DPLL_EN;
942 default:
943 break;
944 }
945 CAM_WRITE(pcdev, CTRLCLOCK, ctrlclock & ~DPLL_EN);
946
947 /* enable internal clock */
948 ctrlclock |= MCLK_EN;
949 CAM_WRITE(pcdev, CTRLCLOCK, ctrlclock);
950
951 sensor_reset(pcdev, false);
952
953 return 0;
954}
955
956static void omap1_cam_clock_stop(struct soc_camera_host *ici)
957{
958 struct omap1_cam_dev *pcdev = ici->priv;
959 u32 ctrlclock;
960
961 suspend_capture(pcdev);
962 disable_capture(pcdev);
963
964 sensor_reset(pcdev, true);
965
966 /* disable and release system clocks */
967 ctrlclock = CAM_READ_CACHE(pcdev, CTRLCLOCK);
968 ctrlclock &= ~(MCLK_EN | DPLL_EN | CAMEXCLK_EN);
969 CAM_WRITE(pcdev, CTRLCLOCK, ctrlclock);
970
971 ctrlclock = (ctrlclock & ~FOSCMOD_MASK) | FOSCMOD_12MHz;
972 CAM_WRITE(pcdev, CTRLCLOCK, ctrlclock);
973 CAM_WRITE(pcdev, CTRLCLOCK, ctrlclock | MCLK_EN);
974
975 CAM_WRITE(pcdev, CTRLCLOCK, ctrlclock & ~MCLK_EN);
976
977 clk_disable(pcdev->clk);
978}
979
980/* Duplicate standard formats based on host capability of byte swapping */
981static const struct soc_mbus_lookup omap1_cam_formats[] = {
982{
983 .code = MEDIA_BUS_FMT_UYVY8_2X8,
984 .fmt = {
985 .fourcc = V4L2_PIX_FMT_YUYV,
986 .name = "YUYV",
987 .bits_per_sample = 8,
988 .packing = SOC_MBUS_PACKING_2X8_PADHI,
989 .order = SOC_MBUS_ORDER_BE,
990 .layout = SOC_MBUS_LAYOUT_PACKED,
991 },
992}, {
993 .code = MEDIA_BUS_FMT_VYUY8_2X8,
994 .fmt = {
995 .fourcc = V4L2_PIX_FMT_YVYU,
996 .name = "YVYU",
997 .bits_per_sample = 8,
998 .packing = SOC_MBUS_PACKING_2X8_PADHI,
999 .order = SOC_MBUS_ORDER_BE,
1000 .layout = SOC_MBUS_LAYOUT_PACKED,
1001 },
1002}, {
1003 .code = MEDIA_BUS_FMT_YUYV8_2X8,
1004 .fmt = {
1005 .fourcc = V4L2_PIX_FMT_UYVY,
1006 .name = "UYVY",
1007 .bits_per_sample = 8,
1008 .packing = SOC_MBUS_PACKING_2X8_PADHI,
1009 .order = SOC_MBUS_ORDER_BE,
1010 .layout = SOC_MBUS_LAYOUT_PACKED,
1011 },
1012}, {
1013 .code = MEDIA_BUS_FMT_YVYU8_2X8,
1014 .fmt = {
1015 .fourcc = V4L2_PIX_FMT_VYUY,
1016 .name = "VYUY",
1017 .bits_per_sample = 8,
1018 .packing = SOC_MBUS_PACKING_2X8_PADHI,
1019 .order = SOC_MBUS_ORDER_BE,
1020 .layout = SOC_MBUS_LAYOUT_PACKED,
1021 },
1022}, {
1023 .code = MEDIA_BUS_FMT_RGB555_2X8_PADHI_BE,
1024 .fmt = {
1025 .fourcc = V4L2_PIX_FMT_RGB555,
1026 .name = "RGB555",
1027 .bits_per_sample = 8,
1028 .packing = SOC_MBUS_PACKING_2X8_PADHI,
1029 .order = SOC_MBUS_ORDER_BE,
1030 .layout = SOC_MBUS_LAYOUT_PACKED,
1031 },
1032}, {
1033 .code = MEDIA_BUS_FMT_RGB555_2X8_PADHI_LE,
1034 .fmt = {
1035 .fourcc = V4L2_PIX_FMT_RGB555X,
1036 .name = "RGB555X",
1037 .bits_per_sample = 8,
1038 .packing = SOC_MBUS_PACKING_2X8_PADHI,
1039 .order = SOC_MBUS_ORDER_BE,
1040 .layout = SOC_MBUS_LAYOUT_PACKED,
1041 },
1042}, {
1043 .code = MEDIA_BUS_FMT_RGB565_2X8_BE,
1044 .fmt = {
1045 .fourcc = V4L2_PIX_FMT_RGB565,
1046 .name = "RGB565",
1047 .bits_per_sample = 8,
1048 .packing = SOC_MBUS_PACKING_2X8_PADHI,
1049 .order = SOC_MBUS_ORDER_BE,
1050 .layout = SOC_MBUS_LAYOUT_PACKED,
1051 },
1052}, {
1053 .code = MEDIA_BUS_FMT_RGB565_2X8_LE,
1054 .fmt = {
1055 .fourcc = V4L2_PIX_FMT_RGB565X,
1056 .name = "RGB565X",
1057 .bits_per_sample = 8,
1058 .packing = SOC_MBUS_PACKING_2X8_PADHI,
1059 .order = SOC_MBUS_ORDER_BE,
1060 .layout = SOC_MBUS_LAYOUT_PACKED,
1061 },
1062},
1063};
1064
1065static int omap1_cam_get_formats(struct soc_camera_device *icd,
1066 unsigned int idx, struct soc_camera_format_xlate *xlate)
1067{
1068 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1069 struct device *dev = icd->parent;
1070 int formats = 0, ret;
1071 struct v4l2_subdev_mbus_code_enum code = {
1072 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
1073 .index = idx,
1074 };
1075 const struct soc_mbus_pixelfmt *fmt;
1076
1077 ret = v4l2_subdev_call(sd, pad, enum_mbus_code, NULL, &code);
1078 if (ret < 0)
1079 /* No more formats */
1080 return 0;
1081
1082 fmt = soc_mbus_get_fmtdesc(code.code);
1083 if (!fmt) {
1084 dev_warn(dev, "%s: unsupported format code #%d: %d\n", __func__,
1085 idx, code.code);
1086 return 0;
1087 }
1088
1089 /* Check support for the requested bits-per-sample */
1090 if (fmt->bits_per_sample != 8)
1091 return 0;
1092
1093 switch (code.code) {
1094 case MEDIA_BUS_FMT_YUYV8_2X8:
1095 case MEDIA_BUS_FMT_YVYU8_2X8:
1096 case MEDIA_BUS_FMT_UYVY8_2X8:
1097 case MEDIA_BUS_FMT_VYUY8_2X8:
1098 case MEDIA_BUS_FMT_RGB555_2X8_PADHI_BE:
1099 case MEDIA_BUS_FMT_RGB555_2X8_PADHI_LE:
1100 case MEDIA_BUS_FMT_RGB565_2X8_BE:
1101 case MEDIA_BUS_FMT_RGB565_2X8_LE:
1102 formats++;
1103 if (xlate) {
1104 xlate->host_fmt = soc_mbus_find_fmtdesc(code.code,
1105 omap1_cam_formats,
1106 ARRAY_SIZE(omap1_cam_formats));
1107 xlate->code = code.code;
1108 xlate++;
1109 dev_dbg(dev,
1110 "%s: providing format %s as byte swapped code #%d\n",
1111 __func__, xlate->host_fmt->name, code.code);
1112 }
1113 default:
1114 if (xlate)
1115 dev_dbg(dev,
1116 "%s: providing format %s in pass-through mode\n",
1117 __func__, fmt->name);
1118 }
1119 formats++;
1120 if (xlate) {
1121 xlate->host_fmt = fmt;
1122 xlate->code = code.code;
1123 xlate++;
1124 }
1125
1126 return formats;
1127}
1128
1129static bool is_dma_aligned(s32 bytes_per_line, unsigned int height,
1130 enum omap1_cam_vb_mode vb_mode)
1131{
1132 int size = bytes_per_line * height;
1133
1134 return IS_ALIGNED(bytes_per_line, DMA_ELEMENT_SIZE) &&
1135 IS_ALIGNED(size, DMA_FRAME_SIZE(vb_mode) * DMA_ELEMENT_SIZE);
1136}
1137
1138static int dma_align(int *width, int *height,
1139 const struct soc_mbus_pixelfmt *fmt,
1140 enum omap1_cam_vb_mode vb_mode, bool enlarge)
1141{
1142 s32 bytes_per_line = soc_mbus_bytes_per_line(*width, fmt);
1143
1144 if (bytes_per_line < 0)
1145 return bytes_per_line;
1146
1147 if (!is_dma_aligned(bytes_per_line, *height, vb_mode)) {
1148 unsigned int pxalign = __fls(bytes_per_line / *width);
1149 unsigned int salign = DMA_FRAME_SHIFT(vb_mode) +
1150 DMA_ELEMENT_SHIFT - pxalign;
1151 unsigned int incr = enlarge << salign;
1152
1153 v4l_bound_align_image(width, 1, *width + incr, 0,
1154 height, 1, *height + incr, 0, salign);
1155 return 0;
1156 }
1157 return 1;
1158}
1159
1160#define subdev_call_with_sense(pcdev, dev, icd, sd, op, function, args...) \
1161({ \
1162 struct soc_camera_sense sense = { \
1163 .master_clock = pcdev->camexclk, \
1164 .pixel_clock_max = 0, \
1165 }; \
1166 int __ret; \
1167 \
1168 if (pcdev->pdata) \
1169 sense.pixel_clock_max = pcdev->pdata->lclk_khz_max * 1000; \
1170 icd->sense = &sense; \
1171 __ret = v4l2_subdev_call(sd, op, function, ##args); \
1172 icd->sense = NULL; \
1173 \
1174 if (sense.flags & SOCAM_SENSE_PCLK_CHANGED) { \
1175 if (sense.pixel_clock > sense.pixel_clock_max) { \
1176 dev_err(dev, \
1177 "%s: pixel clock %lu set by the camera too high!\n", \
1178 __func__, sense.pixel_clock); \
1179 __ret = -EINVAL; \
1180 } \
1181 } \
1182 __ret; \
1183})
1184
1185static int set_format(struct omap1_cam_dev *pcdev, struct device *dev,
1186 struct soc_camera_device *icd, struct v4l2_subdev *sd,
1187 struct v4l2_subdev_format *format,
1188 const struct soc_camera_format_xlate *xlate)
1189{
1190 s32 bytes_per_line;
1191 struct v4l2_mbus_framefmt *mf = &format->format;
1192 int ret = subdev_call_with_sense(pcdev, dev, icd, sd, pad, set_fmt, NULL, format);
1193
1194 if (ret < 0) {
1195 dev_err(dev, "%s: set_fmt failed\n", __func__);
1196 return ret;
1197 }
1198
1199 if (mf->code != xlate->code) {
1200 dev_err(dev, "%s: unexpected pixel code change\n", __func__);
1201 return -EINVAL;
1202 }
1203
1204 bytes_per_line = soc_mbus_bytes_per_line(mf->width, xlate->host_fmt);
1205 if (bytes_per_line < 0) {
1206 dev_err(dev, "%s: soc_mbus_bytes_per_line() failed\n",
1207 __func__);
1208 return bytes_per_line;
1209 }
1210
1211 if (!is_dma_aligned(bytes_per_line, mf->height, pcdev->vb_mode)) {
1212 dev_err(dev, "%s: resulting geometry %ux%u not DMA aligned\n",
1213 __func__, mf->width, mf->height);
1214 return -EINVAL;
1215 }
1216 return 0;
1217}
1218
1219static int omap1_cam_set_crop(struct soc_camera_device *icd,
1220 const struct v4l2_crop *crop)
1221{
1222 const struct v4l2_rect *rect = &crop->c;
1223 const struct soc_camera_format_xlate *xlate = icd->current_fmt;
1224 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1225 struct device *dev = icd->parent;
1226 struct soc_camera_host *ici = to_soc_camera_host(dev);
1227 struct omap1_cam_dev *pcdev = ici->priv;
1228 struct v4l2_subdev_format fmt = {
1229 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
1230 };
1231 struct v4l2_mbus_framefmt *mf = &fmt.format;
1232 int ret;
1233
1234 ret = subdev_call_with_sense(pcdev, dev, icd, sd, video, s_crop, crop);
1235 if (ret < 0) {
1236 dev_warn(dev, "%s: failed to crop to %ux%u@%u:%u\n", __func__,
1237 rect->width, rect->height, rect->left, rect->top);
1238 return ret;
1239 }
1240
1241 ret = v4l2_subdev_call(sd, pad, get_fmt, NULL, &fmt);
1242 if (ret < 0) {
1243 dev_warn(dev, "%s: failed to fetch current format\n", __func__);
1244 return ret;
1245 }
1246
1247 ret = dma_align(&mf->width, &mf->height, xlate->host_fmt, pcdev->vb_mode,
1248 false);
1249 if (ret < 0) {
1250 dev_err(dev, "%s: failed to align %ux%u %s with DMA\n",
1251 __func__, mf->width, mf->height,
1252 xlate->host_fmt->name);
1253 return ret;
1254 }
1255
1256 if (!ret) {
1257 /* sensor returned geometry not DMA aligned, trying to fix */
1258 ret = set_format(pcdev, dev, icd, sd, &fmt, xlate);
1259 if (ret < 0) {
1260 dev_err(dev, "%s: failed to set format\n", __func__);
1261 return ret;
1262 }
1263 }
1264
1265 icd->user_width = mf->width;
1266 icd->user_height = mf->height;
1267
1268 return 0;
1269}
1270
1271static int omap1_cam_set_fmt(struct soc_camera_device *icd,
1272 struct v4l2_format *f)
1273{
1274 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1275 const struct soc_camera_format_xlate *xlate;
1276 struct device *dev = icd->parent;
1277 struct soc_camera_host *ici = to_soc_camera_host(dev);
1278 struct omap1_cam_dev *pcdev = ici->priv;
1279 struct v4l2_pix_format *pix = &f->fmt.pix;
1280 struct v4l2_subdev_format format = {
1281 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
1282 };
1283 struct v4l2_mbus_framefmt *mf = &format.format;
1284 int ret;
1285
1286 xlate = soc_camera_xlate_by_fourcc(icd, pix->pixelformat);
1287 if (!xlate) {
1288 dev_warn(dev, "%s: format %#x not found\n", __func__,
1289 pix->pixelformat);
1290 return -EINVAL;
1291 }
1292
1293 mf->width = pix->width;
1294 mf->height = pix->height;
1295 mf->field = pix->field;
1296 mf->colorspace = pix->colorspace;
1297 mf->code = xlate->code;
1298
1299 ret = dma_align(&mf->width, &mf->height, xlate->host_fmt, pcdev->vb_mode,
1300 true);
1301 if (ret < 0) {
1302 dev_err(dev, "%s: failed to align %ux%u %s with DMA\n",
1303 __func__, pix->width, pix->height,
1304 xlate->host_fmt->name);
1305 return ret;
1306 }
1307
1308 ret = set_format(pcdev, dev, icd, sd, &format, xlate);
1309 if (ret < 0) {
1310 dev_err(dev, "%s: failed to set format\n", __func__);
1311 return ret;
1312 }
1313
1314 pix->width = mf->width;
1315 pix->height = mf->height;
1316 pix->field = mf->field;
1317 pix->colorspace = mf->colorspace;
1318 icd->current_fmt = xlate;
1319
1320 return 0;
1321}
1322
1323static int omap1_cam_try_fmt(struct soc_camera_device *icd,
1324 struct v4l2_format *f)
1325{
1326 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1327 const struct soc_camera_format_xlate *xlate;
1328 struct v4l2_pix_format *pix = &f->fmt.pix;
1329 struct v4l2_subdev_pad_config pad_cfg;
1330 struct v4l2_subdev_format format = {
1331 .which = V4L2_SUBDEV_FORMAT_TRY,
1332 };
1333 struct v4l2_mbus_framefmt *mf = &format.format;
1334 int ret;
1335 /* TODO: limit to mx1 hardware capabilities */
1336
1337 xlate = soc_camera_xlate_by_fourcc(icd, pix->pixelformat);
1338 if (!xlate) {
1339 dev_warn(icd->parent, "Format %#x not found\n",
1340 pix->pixelformat);
1341 return -EINVAL;
1342 }
1343
1344 mf->width = pix->width;
1345 mf->height = pix->height;
1346 mf->field = pix->field;
1347 mf->colorspace = pix->colorspace;
1348 mf->code = xlate->code;
1349
1350 /* limit to sensor capabilities */
1351 ret = v4l2_subdev_call(sd, pad, set_fmt, &pad_cfg, &format);
1352 if (ret < 0)
1353 return ret;
1354
1355 pix->width = mf->width;
1356 pix->height = mf->height;
1357 pix->field = mf->field;
1358 pix->colorspace = mf->colorspace;
1359
1360 return 0;
1361}
1362
1363static bool sg_mode;
1364
1365/*
1366 * Local mmap_mapper wrapper,
1367 * used for detecting videobuf-dma-contig buffer allocation failures
1368 * and switching to videobuf-dma-sg automatically for future attempts.
1369 */
1370static int omap1_cam_mmap_mapper(struct videobuf_queue *q,
1371 struct videobuf_buffer *buf,
1372 struct vm_area_struct *vma)
1373{
1374 struct soc_camera_device *icd = q->priv_data;
1375 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
1376 struct omap1_cam_dev *pcdev = ici->priv;
1377 int ret;
1378
1379 ret = pcdev->mmap_mapper(q, buf, vma);
1380
1381 if (ret == -ENOMEM)
1382 sg_mode = true;
1383
1384 return ret;
1385}
1386
1387static void omap1_cam_init_videobuf(struct videobuf_queue *q,
1388 struct soc_camera_device *icd)
1389{
1390 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
1391 struct omap1_cam_dev *pcdev = ici->priv;
1392
1393 if (!sg_mode)
1394 videobuf_queue_dma_contig_init(q, &omap1_videobuf_ops,
1395 icd->parent, &pcdev->lock,
1396 V4L2_BUF_TYPE_VIDEO_CAPTURE, V4L2_FIELD_NONE,
1397 sizeof(struct omap1_cam_buf), icd, &ici->host_lock);
1398 else
1399 videobuf_queue_sg_init(q, &omap1_videobuf_ops,
1400 icd->parent, &pcdev->lock,
1401 V4L2_BUF_TYPE_VIDEO_CAPTURE, V4L2_FIELD_NONE,
1402 sizeof(struct omap1_cam_buf), icd, &ici->host_lock);
1403
1404 /* use videobuf mode (auto)selected with the module parameter */
1405 pcdev->vb_mode = sg_mode ? OMAP1_CAM_DMA_SG : OMAP1_CAM_DMA_CONTIG;
1406
1407 /*
1408 * Ensure we substitute the videobuf-dma-contig version of the
1409 * mmap_mapper() callback with our own wrapper, used for switching
1410 * automatically to videobuf-dma-sg on buffer allocation failure.
1411 */
1412 if (!sg_mode && q->int_ops->mmap_mapper != omap1_cam_mmap_mapper) {
1413 pcdev->mmap_mapper = q->int_ops->mmap_mapper;
1414 q->int_ops->mmap_mapper = omap1_cam_mmap_mapper;
1415 }
1416}
1417
1418static int omap1_cam_reqbufs(struct soc_camera_device *icd,
1419 struct v4l2_requestbuffers *p)
1420{
1421 int i;
1422
1423 /*
1424 * This is for locking debugging only. I removed spinlocks and now I
1425 * check whether .prepare is ever called on a linked buffer, or whether
1426 * a dma IRQ can occur for an in-work or unlinked buffer. Until now
1427 * it hadn't triggered
1428 */
1429 for (i = 0; i < p->count; i++) {
1430 struct omap1_cam_buf *buf = container_of(icd->vb_vidq.bufs[i],
1431 struct omap1_cam_buf, vb);
1432 buf->inwork = 0;
1433 INIT_LIST_HEAD(&buf->vb.queue);
1434 }
1435
1436 return 0;
1437}
1438
1439static int omap1_cam_querycap(struct soc_camera_host *ici,
1440 struct v4l2_capability *cap)
1441{
1442 /* cap->name is set by the friendly caller:-> */
1443 strlcpy(cap->card, "OMAP1 Camera", sizeof(cap->card));
1444 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
1445 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
1446
1447 return 0;
1448}
1449
1450static int omap1_cam_set_bus_param(struct soc_camera_device *icd)
1451{
1452 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1453 struct device *dev = icd->parent;
1454 struct soc_camera_host *ici = to_soc_camera_host(dev);
1455 struct omap1_cam_dev *pcdev = ici->priv;
1456 u32 pixfmt = icd->current_fmt->host_fmt->fourcc;
1457 const struct soc_camera_format_xlate *xlate;
1458 const struct soc_mbus_pixelfmt *fmt;
1459 struct v4l2_mbus_config cfg = {.type = V4L2_MBUS_PARALLEL,};
1460 unsigned long common_flags;
1461 u32 ctrlclock, mode;
1462 int ret;
1463
1464 ret = v4l2_subdev_call(sd, video, g_mbus_config, &cfg);
1465 if (!ret) {
1466 common_flags = soc_mbus_config_compatible(&cfg, SOCAM_BUS_FLAGS);
1467 if (!common_flags) {
1468 dev_warn(dev,
1469 "Flags incompatible: camera 0x%x, host 0x%x\n",
1470 cfg.flags, SOCAM_BUS_FLAGS);
1471 return -EINVAL;
1472 }
1473 } else if (ret != -ENOIOCTLCMD) {
1474 return ret;
1475 } else {
1476 common_flags = SOCAM_BUS_FLAGS;
1477 }
1478
1479 /* Make choices, possibly based on platform configuration */
1480 if ((common_flags & V4L2_MBUS_PCLK_SAMPLE_RISING) &&
1481 (common_flags & V4L2_MBUS_PCLK_SAMPLE_FALLING)) {
1482 if (!pcdev->pdata ||
1483 pcdev->pdata->flags & OMAP1_CAMERA_LCLK_RISING)
1484 common_flags &= ~V4L2_MBUS_PCLK_SAMPLE_FALLING;
1485 else
1486 common_flags &= ~V4L2_MBUS_PCLK_SAMPLE_RISING;
1487 }
1488
1489 cfg.flags = common_flags;
1490 ret = v4l2_subdev_call(sd, video, s_mbus_config, &cfg);
1491 if (ret < 0 && ret != -ENOIOCTLCMD) {
1492 dev_dbg(dev, "camera s_mbus_config(0x%lx) returned %d\n",
1493 common_flags, ret);
1494 return ret;
1495 }
1496
1497 ctrlclock = CAM_READ_CACHE(pcdev, CTRLCLOCK);
1498 if (ctrlclock & LCLK_EN)
1499 CAM_WRITE(pcdev, CTRLCLOCK, ctrlclock & ~LCLK_EN);
1500
1501 if (common_flags & V4L2_MBUS_PCLK_SAMPLE_RISING) {
1502 dev_dbg(dev, "CTRLCLOCK_REG |= POLCLK\n");
1503 ctrlclock |= POLCLK;
1504 } else {
1505 dev_dbg(dev, "CTRLCLOCK_REG &= ~POLCLK\n");
1506 ctrlclock &= ~POLCLK;
1507 }
1508 CAM_WRITE(pcdev, CTRLCLOCK, ctrlclock & ~LCLK_EN);
1509
1510 if (ctrlclock & LCLK_EN)
1511 CAM_WRITE(pcdev, CTRLCLOCK, ctrlclock);
1512
1513 /* select bus endianness */
1514 xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
1515 fmt = xlate->host_fmt;
1516
1517 mode = CAM_READ(pcdev, MODE) & ~(RAZ_FIFO | IRQ_MASK | DMA);
1518 if (fmt->order == SOC_MBUS_ORDER_LE) {
1519 dev_dbg(dev, "MODE_REG &= ~ORDERCAMD\n");
1520 CAM_WRITE(pcdev, MODE, mode & ~ORDERCAMD);
1521 } else {
1522 dev_dbg(dev, "MODE_REG |= ORDERCAMD\n");
1523 CAM_WRITE(pcdev, MODE, mode | ORDERCAMD);
1524 }
1525
1526 return 0;
1527}
1528
1529static unsigned int omap1_cam_poll(struct file *file, poll_table *pt)
1530{
1531 struct soc_camera_device *icd = file->private_data;
1532 struct omap1_cam_buf *buf;
1533
1534 buf = list_entry(icd->vb_vidq.stream.next, struct omap1_cam_buf,
1535 vb.stream);
1536
1537 poll_wait(file, &buf->vb.done, pt);
1538
1539 if (buf->vb.state == VIDEOBUF_DONE ||
1540 buf->vb.state == VIDEOBUF_ERROR)
1541 return POLLIN | POLLRDNORM;
1542
1543 return 0;
1544}
1545
1546static struct soc_camera_host_ops omap1_host_ops = {
1547 .owner = THIS_MODULE,
1548 .add = omap1_cam_add_device,
1549 .remove = omap1_cam_remove_device,
1550 .clock_start = omap1_cam_clock_start,
1551 .clock_stop = omap1_cam_clock_stop,
1552 .get_formats = omap1_cam_get_formats,
1553 .set_crop = omap1_cam_set_crop,
1554 .set_fmt = omap1_cam_set_fmt,
1555 .try_fmt = omap1_cam_try_fmt,
1556 .init_videobuf = omap1_cam_init_videobuf,
1557 .reqbufs = omap1_cam_reqbufs,
1558 .querycap = omap1_cam_querycap,
1559 .set_bus_param = omap1_cam_set_bus_param,
1560 .poll = omap1_cam_poll,
1561};
1562
1563static int omap1_cam_probe(struct platform_device *pdev)
1564{
1565 struct omap1_cam_dev *pcdev;
1566 struct resource *res;
1567 struct clk *clk;
1568 void __iomem *base;
1569 unsigned int irq;
1570 int err = 0;
1571
1572 irq = platform_get_irq(pdev, 0);
1573 if ((int)irq <= 0) {
1574 err = -ENODEV;
1575 goto exit;
1576 }
1577
1578 clk = devm_clk_get(&pdev->dev, "armper_ck");
1579 if (IS_ERR(clk))
1580 return PTR_ERR(clk);
1581
1582 pcdev = devm_kzalloc(&pdev->dev, sizeof(*pcdev) + resource_size(res),
1583 GFP_KERNEL);
1584 if (!pcdev)
1585 return -ENOMEM;
1586
1587 pcdev->clk = clk;
1588
1589 pcdev->pdata = pdev->dev.platform_data;
1590 if (pcdev->pdata) {
1591 pcdev->pflags = pcdev->pdata->flags;
1592 pcdev->camexclk = pcdev->pdata->camexclk_khz * 1000;
1593 }
1594
1595 switch (pcdev->camexclk) {
1596 case 6000000:
1597 case 8000000:
1598 case 9600000:
1599 case 12000000:
1600 case 24000000:
1601 break;
1602 default:
1603 /* pcdev->camexclk != 0 => pcdev->pdata != NULL */
1604 dev_warn(&pdev->dev,
1605 "Incorrect sensor clock frequency %ld kHz, "
1606 "should be one of 0, 6, 8, 9.6, 12 or 24 MHz, "
1607 "please correct your platform data\n",
1608 pcdev->pdata->camexclk_khz);
1609 pcdev->camexclk = 0;
1610 case 0:
1611 dev_info(&pdev->dev, "Not providing sensor clock\n");
1612 }
1613
1614 INIT_LIST_HEAD(&pcdev->capture);
1615 spin_lock_init(&pcdev->lock);
1616
1617 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1618 base = devm_ioremap_resource(&pdev->dev, res);
1619 if (IS_ERR(base))
1620 return PTR_ERR(base);
1621
1622 pcdev->irq = irq;
1623 pcdev->base = base;
1624
1625 sensor_reset(pcdev, true);
1626
1627 err = omap_request_dma(OMAP_DMA_CAMERA_IF_RX, DRIVER_NAME,
1628 dma_isr, (void *)pcdev, &pcdev->dma_ch);
1629 if (err < 0) {
1630 dev_err(&pdev->dev, "Can't request DMA for OMAP1 Camera\n");
1631 return -EBUSY;
1632 }
1633 dev_dbg(&pdev->dev, "got DMA channel %d\n", pcdev->dma_ch);
1634
1635 /* preconfigure DMA */
1636 omap_set_dma_src_params(pcdev->dma_ch, OMAP_DMA_PORT_TIPB,
1637 OMAP_DMA_AMODE_CONSTANT, res->start + REG_CAMDATA,
1638 0, 0);
1639 omap_set_dma_dest_burst_mode(pcdev->dma_ch, OMAP_DMA_DATA_BURST_4);
1640 /* setup DMA autoinitialization */
1641 omap_dma_link_lch(pcdev->dma_ch, pcdev->dma_ch);
1642
1643 err = devm_request_irq(&pdev->dev, pcdev->irq, cam_isr, 0, DRIVER_NAME,
1644 pcdev);
1645 if (err) {
1646 dev_err(&pdev->dev, "Camera interrupt register failed\n");
1647 goto exit_free_dma;
1648 }
1649
1650 pcdev->soc_host.drv_name = DRIVER_NAME;
1651 pcdev->soc_host.ops = &omap1_host_ops;
1652 pcdev->soc_host.priv = pcdev;
1653 pcdev->soc_host.v4l2_dev.dev = &pdev->dev;
1654 pcdev->soc_host.nr = pdev->id;
1655
1656 err = soc_camera_host_register(&pcdev->soc_host);
1657 if (err)
1658 return err;
1659
1660 dev_info(&pdev->dev, "OMAP1 Camera Interface driver loaded\n");
1661
1662 return 0;
1663
1664exit_free_dma:
1665 omap_free_dma(pcdev->dma_ch);
1666exit:
1667 return err;
1668}
1669
1670static int omap1_cam_remove(struct platform_device *pdev)
1671{
1672 struct soc_camera_host *soc_host = to_soc_camera_host(&pdev->dev);
1673 struct omap1_cam_dev *pcdev = container_of(soc_host,
1674 struct omap1_cam_dev, soc_host);
1675
1676 omap_free_dma(pcdev->dma_ch);
1677
1678 soc_camera_host_unregister(soc_host);
1679
1680 dev_info(&pdev->dev, "OMAP1 Camera Interface driver unloaded\n");
1681
1682 return 0;
1683}
1684
1685static struct platform_driver omap1_cam_driver = {
1686 .driver = {
1687 .name = DRIVER_NAME,
1688 },
1689 .probe = omap1_cam_probe,
1690 .remove = omap1_cam_remove,
1691};
1692
1693module_platform_driver(omap1_cam_driver);
1694
1695module_param(sg_mode, bool, 0644);
1696MODULE_PARM_DESC(sg_mode, "videobuf mode, 0: dma-contig (default), 1: dma-sg");
1697
1698MODULE_DESCRIPTION("OMAP1 Camera Interface driver");
1699MODULE_AUTHOR("Janusz Krzysztofik <jkrzyszt@tis.icnet.pl>");
1700MODULE_LICENSE("GPL v2");
1701MODULE_VERSION(DRIVER_VERSION);
1702MODULE_ALIAS("platform:" DRIVER_NAME);
diff --git a/drivers/staging/media/omap4iss/iss_video.c b/drivers/staging/media/omap4iss/iss_video.c
index cf8da23558bb..90b7ff56722d 100644
--- a/drivers/staging/media/omap4iss/iss_video.c
+++ b/drivers/staging/media/omap4iss/iss_video.c
@@ -298,7 +298,7 @@ iss_video_check_format(struct iss_video *video, struct iss_video_fh *vfh)
298 298
299static int iss_video_queue_setup(struct vb2_queue *vq, 299static int iss_video_queue_setup(struct vb2_queue *vq,
300 unsigned int *count, unsigned int *num_planes, 300 unsigned int *count, unsigned int *num_planes,
301 unsigned int sizes[], void *alloc_ctxs[]) 301 unsigned int sizes[], struct device *alloc_devs[])
302{ 302{
303 struct iss_video_fh *vfh = vb2_get_drv_priv(vq); 303 struct iss_video_fh *vfh = vb2_get_drv_priv(vq);
304 struct iss_video *video = vfh->video; 304 struct iss_video *video = vfh->video;
@@ -310,8 +310,6 @@ static int iss_video_queue_setup(struct vb2_queue *vq,
310 if (sizes[0] == 0) 310 if (sizes[0] == 0)
311 return -EINVAL; 311 return -EINVAL;
312 312
313 alloc_ctxs[0] = video->alloc_ctx;
314
315 *count = min(*count, video->capture_mem / PAGE_ALIGN(sizes[0])); 313 *count = min(*count, video->capture_mem / PAGE_ALIGN(sizes[0]));
316 314
317 return 0; 315 return 0;
@@ -1017,13 +1015,6 @@ static int iss_video_open(struct file *file)
1017 goto done; 1015 goto done;
1018 } 1016 }
1019 1017
1020 video->alloc_ctx = vb2_dma_contig_init_ctx(video->iss->dev);
1021 if (IS_ERR(video->alloc_ctx)) {
1022 ret = PTR_ERR(video->alloc_ctx);
1023 omap4iss_put(video->iss);
1024 goto done;
1025 }
1026
1027 q = &handle->queue; 1018 q = &handle->queue;
1028 1019
1029 q->type = video->type; 1020 q->type = video->type;
@@ -1033,6 +1024,7 @@ static int iss_video_open(struct file *file)
1033 q->mem_ops = &vb2_dma_contig_memops; 1024 q->mem_ops = &vb2_dma_contig_memops;
1034 q->buf_struct_size = sizeof(struct iss_buffer); 1025 q->buf_struct_size = sizeof(struct iss_buffer);
1035 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC; 1026 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1027 q->dev = video->iss->dev;
1036 1028
1037 ret = vb2_queue_init(q); 1029 ret = vb2_queue_init(q);
1038 if (ret) { 1030 if (ret) {
diff --git a/drivers/staging/media/omap4iss/iss_video.h b/drivers/staging/media/omap4iss/iss_video.h
index c8bd2958a3f8..d7e05d04512c 100644
--- a/drivers/staging/media/omap4iss/iss_video.h
+++ b/drivers/staging/media/omap4iss/iss_video.h
@@ -170,7 +170,6 @@ struct iss_video {
170 spinlock_t qlock; /* protects dmaqueue and error */ 170 spinlock_t qlock; /* protects dmaqueue and error */
171 struct list_head dmaqueue; 171 struct list_head dmaqueue;
172 enum iss_video_dmaqueue_flags dmaqueue_flags; 172 enum iss_video_dmaqueue_flags dmaqueue_flags;
173 struct vb2_alloc_ctx *alloc_ctx;
174 173
175 const struct iss_video_operations *ops; 174 const struct iss_video_operations *ops;
176}; 175};
diff --git a/drivers/staging/media/pulse8-cec/Kconfig b/drivers/staging/media/pulse8-cec/Kconfig
new file mode 100644
index 000000000000..c6aa2d1c9df0
--- /dev/null
+++ b/drivers/staging/media/pulse8-cec/Kconfig
@@ -0,0 +1,10 @@
1config USB_PULSE8_CEC
2 tristate "Pulse Eight HDMI CEC"
3 depends on USB_ACM && MEDIA_CEC
4 select SERIO
5 select SERIO_SERPORT
6 ---help---
7 This is a cec driver for the Pulse Eight HDMI CEC device.
8
9 To compile this driver as a module, choose M here: the
10 module will be called pulse8-cec.
diff --git a/drivers/staging/media/pulse8-cec/Makefile b/drivers/staging/media/pulse8-cec/Makefile
new file mode 100644
index 000000000000..9800690bc25a
--- /dev/null
+++ b/drivers/staging/media/pulse8-cec/Makefile
@@ -0,0 +1 @@
obj-$(CONFIG_USB_PULSE8_CEC) += pulse8-cec.o
diff --git a/drivers/staging/media/pulse8-cec/TODO b/drivers/staging/media/pulse8-cec/TODO
new file mode 100644
index 000000000000..fa6660245e5f
--- /dev/null
+++ b/drivers/staging/media/pulse8-cec/TODO
@@ -0,0 +1,52 @@
1This driver needs to mature a bit more and another round of
2code cleanups.
3
4Otherwise it looks to be in good shape. And of course the fact
5that the CEC framework is in staging at the moment also prevents
6this driver from being mainlined.
7
8Some notes:
9
101) Regarding the "autonomous" mode of the Pulse-Eight: currently this
11is disabled, but the idea is that this allows basic functionality
12when the PC is off, and it can wake-up the PC through USB.
13
14To prevent the device to go into autonomous mode the driver would
15have to send MSGCODE_SET_CONTROLLED 1 and then send a ping every
1630 seconds (in practice once every 15 seconds would be good). When
17powering off or going to standby send MSGCODE_SET_CONTROLLED 0 to
18turn the autonomous mode back on.
19
20This needs to be implemented in the driver. Autonomous mode was
21added in firmware v2.
22
232) Writing to the EEPROM can only be done once every 10 seconds.
24
253) To use this driver you also need to patch the inputattach utility,
26this patch will be submitted once this driver is moved out of staging.
27
28diff -urN linuxconsoletools-1.4.9/utils/inputattach.c linuxconsoletools-1.4.9.new/utils/inputattach.c
29--- linuxconsoletools-1.4.9/utils/inputattach.c 2016-01-09 16:27:02.000000000 +0100
30+++ linuxconsoletools-1.4.9.new/utils/inputattach.c 2016-03-20 11:35:31.707788967 +0100
31@@ -861,6 +861,9 @@
32 { "--wacom_iv", "-wacom_iv", "Wacom protocol IV tablet",
33 B9600, CS8 | CRTSCTS,
34 SERIO_WACOM_IV, 0x00, 0x00, 0, wacom_iv_init },
35+{ "--pulse8-cec", "-pulse8-cec", "Pulse Eight HDMI CEC dongle",
36+ B9600, CS8,
37+ SERIO_PULSE8_CEC, 0x00, 0x00, 0, NULL },
38 { NULL, NULL, NULL, 0, 0, 0, 0, 0, 0, NULL }
39 };
40
41diff -urN linuxconsoletools-1.4.9/utils/serio-ids.h linuxconsoletools-1.4.9.new/utils/serio-ids.h
42--- linuxconsoletools-1.4.9/utils/serio-ids.h 2015-04-26 18:29:42.000000000 +0200
43+++ linuxconsoletools-1.4.9.new/utils/serio-ids.h 2016-03-20 11:41:00.153558539 +0100
44@@ -131,5 +131,8 @@
45 #ifndef SERIO_EASYPEN
46 # define SERIO_EASYPEN 0x3f
47 #endif
48+#ifndef SERIO_PULSE8_CEC
49+# define SERIO_PULSE8_CEC 0x40
50+#endif
51
52 #endif
diff --git a/drivers/staging/media/pulse8-cec/pulse8-cec.c b/drivers/staging/media/pulse8-cec/pulse8-cec.c
new file mode 100644
index 000000000000..94f8590492dc
--- /dev/null
+++ b/drivers/staging/media/pulse8-cec/pulse8-cec.c
@@ -0,0 +1,505 @@
1/*
2 * Pulse Eight HDMI CEC driver
3 *
4 * Copyright 2016 Hans Verkuil <hverkuil@xs4all.nl
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version of 2 of the License, or (at your
9 * option) any later version. See the file COPYING in the main directory of
10 * this archive for more details.
11 */
12
13#include <linux/completion.h>
14#include <linux/init.h>
15#include <linux/interrupt.h>
16#include <linux/kernel.h>
17#include <linux/module.h>
18#include <linux/workqueue.h>
19#include <linux/serio.h>
20#include <linux/slab.h>
21#include <linux/time.h>
22#include <linux/delay.h>
23
24#include <media/cec.h>
25
26MODULE_AUTHOR("Hans Verkuil <hverkuil@xs4all.nl>");
27MODULE_DESCRIPTION("Pulse Eight HDMI CEC driver");
28MODULE_LICENSE("GPL");
29
30static int debug;
31module_param(debug, int, 0644);
32MODULE_PARM_DESC(debug, "debug level (0-1)");
33
34enum pulse8_msgcodes {
35 MSGCODE_NOTHING = 0,
36 MSGCODE_PING,
37 MSGCODE_TIMEOUT_ERROR,
38 MSGCODE_HIGH_ERROR,
39 MSGCODE_LOW_ERROR,
40 MSGCODE_FRAME_START,
41 MSGCODE_FRAME_DATA,
42 MSGCODE_RECEIVE_FAILED,
43 MSGCODE_COMMAND_ACCEPTED, /* 0x08 */
44 MSGCODE_COMMAND_REJECTED,
45 MSGCODE_SET_ACK_MASK,
46 MSGCODE_TRANSMIT,
47 MSGCODE_TRANSMIT_EOM,
48 MSGCODE_TRANSMIT_IDLETIME,
49 MSGCODE_TRANSMIT_ACK_POLARITY,
50 MSGCODE_TRANSMIT_LINE_TIMEOUT,
51 MSGCODE_TRANSMIT_SUCCEEDED, /* 0x10 */
52 MSGCODE_TRANSMIT_FAILED_LINE,
53 MSGCODE_TRANSMIT_FAILED_ACK,
54 MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA,
55 MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE,
56 MSGCODE_FIRMWARE_VERSION,
57 MSGCODE_START_BOOTLOADER,
58 MSGCODE_GET_BUILDDATE,
59 MSGCODE_SET_CONTROLLED, /* 0x18 */
60 MSGCODE_GET_AUTO_ENABLED,
61 MSGCODE_SET_AUTO_ENABLED,
62 MSGCODE_GET_DEFAULT_LOGICAL_ADDRESS,
63 MSGCODE_SET_DEFAULT_LOGICAL_ADDRESS,
64 MSGCODE_GET_LOGICAL_ADDRESS_MASK,
65 MSGCODE_SET_LOGICAL_ADDRESS_MASK,
66 MSGCODE_GET_PHYSICAL_ADDRESS,
67 MSGCODE_SET_PHYSICAL_ADDRESS, /* 0x20 */
68 MSGCODE_GET_DEVICE_TYPE,
69 MSGCODE_SET_DEVICE_TYPE,
70 MSGCODE_GET_HDMI_VERSION,
71 MSGCODE_SET_HDMI_VERSION,
72 MSGCODE_GET_OSD_NAME,
73 MSGCODE_SET_OSD_NAME,
74 MSGCODE_WRITE_EEPROM,
75 MSGCODE_GET_ADAPTER_TYPE, /* 0x28 */
76 MSGCODE_SET_ACTIVE_SOURCE,
77
78 MSGCODE_FRAME_EOM = 0x80,
79 MSGCODE_FRAME_ACK = 0x40,
80};
81
82#define MSGSTART 0xff
83#define MSGEND 0xfe
84#define MSGESC 0xfd
85#define MSGOFFSET 3
86
87#define DATA_SIZE 256
88
89struct pulse8 {
90 struct device *dev;
91 struct serio *serio;
92 struct cec_adapter *adap;
93 struct completion cmd_done;
94 struct work_struct work;
95 struct cec_msg rx_msg;
96 u8 data[DATA_SIZE];
97 unsigned int len;
98 u8 buf[DATA_SIZE];
99 unsigned int idx;
100 bool escape;
101 bool started;
102};
103
104static void pulse8_irq_work_handler(struct work_struct *work)
105{
106 struct pulse8 *pulse8 =
107 container_of(work, struct pulse8, work);
108
109 switch (pulse8->data[0] & 0x3f) {
110 case MSGCODE_FRAME_DATA:
111 cec_received_msg(pulse8->adap, &pulse8->rx_msg);
112 break;
113 case MSGCODE_TRANSMIT_SUCCEEDED:
114 cec_transmit_done(pulse8->adap, CEC_TX_STATUS_OK,
115 0, 0, 0, 0);
116 break;
117 case MSGCODE_TRANSMIT_FAILED_LINE:
118 cec_transmit_done(pulse8->adap, CEC_TX_STATUS_ARB_LOST,
119 1, 0, 0, 0);
120 break;
121 case MSGCODE_TRANSMIT_FAILED_ACK:
122 cec_transmit_done(pulse8->adap, CEC_TX_STATUS_NACK,
123 0, 1, 0, 0);
124 break;
125 case MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA:
126 case MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE:
127 cec_transmit_done(pulse8->adap, CEC_TX_STATUS_ERROR,
128 0, 0, 0, 1);
129 break;
130 }
131}
132
133static irqreturn_t pulse8_interrupt(struct serio *serio, unsigned char data,
134 unsigned int flags)
135{
136 struct pulse8 *pulse8 = serio_get_drvdata(serio);
137
138 if (!pulse8->started && data != MSGSTART)
139 return IRQ_HANDLED;
140 if (data == MSGESC) {
141 pulse8->escape = true;
142 return IRQ_HANDLED;
143 }
144 if (pulse8->escape) {
145 data += MSGOFFSET;
146 pulse8->escape = false;
147 } else if (data == MSGEND) {
148 struct cec_msg *msg = &pulse8->rx_msg;
149
150 if (debug)
151 dev_info(pulse8->dev, "received: %*ph\n",
152 pulse8->idx, pulse8->buf);
153 pulse8->data[0] = pulse8->buf[0];
154 switch (pulse8->buf[0] & 0x3f) {
155 case MSGCODE_FRAME_START:
156 msg->len = 1;
157 msg->msg[0] = pulse8->buf[1];
158 break;
159 case MSGCODE_FRAME_DATA:
160 if (msg->len == CEC_MAX_MSG_SIZE)
161 break;
162 msg->msg[msg->len++] = pulse8->buf[1];
163 if (pulse8->buf[0] & MSGCODE_FRAME_EOM)
164 schedule_work(&pulse8->work);
165 break;
166 case MSGCODE_TRANSMIT_SUCCEEDED:
167 case MSGCODE_TRANSMIT_FAILED_LINE:
168 case MSGCODE_TRANSMIT_FAILED_ACK:
169 case MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA:
170 case MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE:
171 schedule_work(&pulse8->work);
172 break;
173 case MSGCODE_TIMEOUT_ERROR:
174 break;
175 case MSGCODE_COMMAND_ACCEPTED:
176 case MSGCODE_COMMAND_REJECTED:
177 default:
178 if (pulse8->idx == 0)
179 break;
180 memcpy(pulse8->data, pulse8->buf, pulse8->idx);
181 pulse8->len = pulse8->idx;
182 complete(&pulse8->cmd_done);
183 break;
184 }
185 pulse8->idx = 0;
186 pulse8->started = false;
187 return IRQ_HANDLED;
188 } else if (data == MSGSTART) {
189 pulse8->idx = 0;
190 pulse8->started = true;
191 return IRQ_HANDLED;
192 }
193
194 if (pulse8->idx >= DATA_SIZE) {
195 dev_dbg(pulse8->dev,
196 "throwing away %d bytes of garbage\n", pulse8->idx);
197 pulse8->idx = 0;
198 }
199 pulse8->buf[pulse8->idx++] = data;
200 return IRQ_HANDLED;
201}
202
203static void pulse8_disconnect(struct serio *serio)
204{
205 struct pulse8 *pulse8 = serio_get_drvdata(serio);
206
207 cec_unregister_adapter(pulse8->adap);
208 dev_info(&serio->dev, "disconnected\n");
209 serio_close(serio);
210 serio_set_drvdata(serio, NULL);
211 kfree(pulse8);
212}
213
214static int pulse8_send(struct serio *serio, const u8 *command, u8 cmd_len)
215{
216 int err = 0;
217
218 err = serio_write(serio, MSGSTART);
219 if (err)
220 return err;
221 for (; !err && cmd_len; command++, cmd_len--) {
222 if (*command >= MSGESC) {
223 err = serio_write(serio, MSGESC);
224 if (!err)
225 err = serio_write(serio, *command - MSGOFFSET);
226 } else {
227 err = serio_write(serio, *command);
228 }
229 }
230 if (!err)
231 err = serio_write(serio, 0xfe);
232
233 return err;
234}
235
236static int pulse8_send_and_wait(struct pulse8 *pulse8,
237 const u8 *cmd, u8 cmd_len, u8 response, u8 size)
238{
239 int err;
240
241 /*dev_info(pulse8->dev, "transmit: %*ph\n", cmd_len, cmd);*/
242 init_completion(&pulse8->cmd_done);
243
244 err = pulse8_send(pulse8->serio, cmd, cmd_len);
245 if (err)
246 return err;
247
248 if (!wait_for_completion_timeout(&pulse8->cmd_done, HZ))
249 return -ETIMEDOUT;
250 if ((pulse8->data[0] & 0x3f) == MSGCODE_COMMAND_REJECTED &&
251 cmd[0] != MSGCODE_SET_CONTROLLED &&
252 cmd[0] != MSGCODE_SET_AUTO_ENABLED &&
253 cmd[0] != MSGCODE_GET_BUILDDATE) {
254 u8 cmd_sc[2];
255
256 cmd_sc[0] = MSGCODE_SET_CONTROLLED;
257 cmd_sc[1] = 1;
258 err = pulse8_send_and_wait(pulse8, cmd_sc, 2,
259 MSGCODE_COMMAND_ACCEPTED, 1);
260 if (err)
261 return err;
262 init_completion(&pulse8->cmd_done);
263
264 err = pulse8_send(pulse8->serio, cmd, cmd_len);
265 if (err)
266 return err;
267
268 if (!wait_for_completion_timeout(&pulse8->cmd_done, HZ))
269 return -ETIMEDOUT;
270 }
271 if (response &&
272 ((pulse8->data[0] & 0x3f) != response || pulse8->len < size + 1)) {
273 dev_info(pulse8->dev, "transmit: failed %02x\n",
274 pulse8->data[0] & 0x3f);
275 return -EIO;
276 }
277 return 0;
278}
279
280static int pulse8_setup(struct pulse8 *pulse8, struct serio *serio)
281{
282 u8 *data = pulse8->data + 1;
283 unsigned int count = 0;
284 unsigned int vers = 0;
285 u8 cmd[2];
286 int err;
287
288 cmd[0] = MSGCODE_PING;
289 err = pulse8_send_and_wait(pulse8, cmd, 1,
290 MSGCODE_COMMAND_ACCEPTED, 0);
291 cmd[0] = MSGCODE_FIRMWARE_VERSION;
292 if (!err)
293 err = pulse8_send_and_wait(pulse8, cmd, 1, cmd[0], 2);
294 if (err)
295 return err;
296
297 vers = (data[0] << 8) | data[1];
298
299 dev_info(pulse8->dev, "Firmware version %04x\n", vers);
300 if (vers < 2)
301 return 0;
302
303 cmd[0] = MSGCODE_GET_BUILDDATE;
304 if (!err)
305 err = pulse8_send_and_wait(pulse8, cmd, 1, cmd[0], 4);
306 if (!err) {
307 time_t date = (data[0] << 24) | (data[1] << 16) |
308 (data[2] << 8) | data[3];
309 struct tm tm;
310
311 time_to_tm(date, 0, &tm);
312
313 dev_info(pulse8->dev, "Firmware build date %04ld.%02d.%02d %02d:%02d:%02d\n",
314 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
315 tm.tm_hour, tm.tm_min, tm.tm_sec);
316 }
317
318 do {
319 if (count)
320 msleep(500);
321 cmd[0] = MSGCODE_SET_AUTO_ENABLED;
322 cmd[1] = 0;
323 err = pulse8_send_and_wait(pulse8, cmd, 2,
324 MSGCODE_COMMAND_ACCEPTED, 1);
325 if (err && count == 0) {
326 dev_info(pulse8->dev, "No Auto Enabled supported\n");
327 return 0;
328 }
329
330 cmd[0] = MSGCODE_GET_AUTO_ENABLED;
331 if (!err)
332 err = pulse8_send_and_wait(pulse8, cmd, 1, cmd[0], 1);
333 if (!err && !data[0]) {
334 cmd[0] = MSGCODE_WRITE_EEPROM;
335 err = pulse8_send_and_wait(pulse8, cmd, 1,
336 MSGCODE_COMMAND_ACCEPTED, 1);
337 cmd[0] = MSGCODE_GET_AUTO_ENABLED;
338 if (!err)
339 err = pulse8_send_and_wait(pulse8, cmd, 1,
340 cmd[0], 1);
341 }
342 } while (!err && data[0] && count++ < 5);
343
344 if (!err && data[0])
345 err = -EIO;
346
347 return err;
348}
349
350static int pulse8_cec_adap_enable(struct cec_adapter *adap, bool enable)
351{
352 struct pulse8 *pulse8 = adap->priv;
353 u8 cmd[16];
354 int err;
355
356 cmd[0] = MSGCODE_SET_CONTROLLED;
357 cmd[1] = enable;
358 err = pulse8_send_and_wait(pulse8, cmd, 2,
359 MSGCODE_COMMAND_ACCEPTED, 1);
360 return enable ? err : 0;
361}
362
363static int pulse8_cec_adap_log_addr(struct cec_adapter *adap, u8 log_addr)
364{
365 struct pulse8 *pulse8 = adap->priv;
366 u16 mask = 0;
367 u8 cmd[3];
368 int err;
369
370 if (log_addr != CEC_LOG_ADDR_INVALID)
371 mask = 1 << log_addr;
372 cmd[0] = MSGCODE_SET_ACK_MASK;
373 cmd[1] = mask >> 8;
374 cmd[2] = mask & 0xff;
375 err = pulse8_send_and_wait(pulse8, cmd, 3,
376 MSGCODE_COMMAND_ACCEPTED, 0);
377 if (mask == 0)
378 return 0;
379 return err;
380}
381
382static int pulse8_cec_adap_transmit(struct cec_adapter *adap, u8 attempts,
383 u32 signal_free_time, struct cec_msg *msg)
384{
385 struct pulse8 *pulse8 = adap->priv;
386 u8 cmd[2];
387 unsigned int i;
388 int err;
389
390 cmd[0] = MSGCODE_TRANSMIT_IDLETIME;
391 cmd[1] = 3;
392 err = pulse8_send_and_wait(pulse8, cmd, 2,
393 MSGCODE_COMMAND_ACCEPTED, 1);
394 cmd[0] = MSGCODE_TRANSMIT_ACK_POLARITY;
395 cmd[1] = cec_msg_is_broadcast(msg);
396 if (!err)
397 err = pulse8_send_and_wait(pulse8, cmd, 2,
398 MSGCODE_COMMAND_ACCEPTED, 1);
399 cmd[0] = msg->len == 1 ? MSGCODE_TRANSMIT_EOM : MSGCODE_TRANSMIT;
400 cmd[1] = msg->msg[0];
401 if (!err)
402 err = pulse8_send_and_wait(pulse8, cmd, 2,
403 MSGCODE_COMMAND_ACCEPTED, 1);
404 if (!err && msg->len > 1) {
405 cmd[0] = msg->len == 2 ? MSGCODE_TRANSMIT_EOM :
406 MSGCODE_TRANSMIT;
407 cmd[1] = msg->msg[1];
408 err = pulse8_send_and_wait(pulse8, cmd, 2,
409 MSGCODE_COMMAND_ACCEPTED, 1);
410 for (i = 0; !err && i + 2 < msg->len; i++) {
411 cmd[0] = (i + 2 == msg->len - 1) ?
412 MSGCODE_TRANSMIT_EOM : MSGCODE_TRANSMIT;
413 cmd[1] = msg->msg[i + 2];
414 err = pulse8_send_and_wait(pulse8, cmd, 2,
415 MSGCODE_COMMAND_ACCEPTED, 1);
416 }
417 }
418
419 return err;
420}
421
422static int pulse8_received(struct cec_adapter *adap, struct cec_msg *msg)
423{
424 return -ENOMSG;
425}
426
427static const struct cec_adap_ops pulse8_cec_adap_ops = {
428 .adap_enable = pulse8_cec_adap_enable,
429 .adap_log_addr = pulse8_cec_adap_log_addr,
430 .adap_transmit = pulse8_cec_adap_transmit,
431 .received = pulse8_received,
432};
433
434static int pulse8_connect(struct serio *serio, struct serio_driver *drv)
435{
436 u32 caps = CEC_CAP_TRANSMIT | CEC_CAP_LOG_ADDRS | CEC_CAP_PHYS_ADDR |
437 CEC_CAP_PASSTHROUGH | CEC_CAP_RC | CEC_CAP_MONITOR_ALL;
438 struct pulse8 *pulse8;
439 int err = -ENOMEM;
440
441 pulse8 = kzalloc(sizeof(*pulse8), GFP_KERNEL);
442
443 if (!pulse8)
444 return -ENOMEM;
445
446 pulse8->serio = serio;
447 pulse8->adap = cec_allocate_adapter(&pulse8_cec_adap_ops, pulse8,
448 "HDMI CEC", caps, 1, &serio->dev);
449 err = PTR_ERR_OR_ZERO(pulse8->adap);
450 if (err < 0)
451 goto free_device;
452
453 pulse8->dev = &serio->dev;
454 serio_set_drvdata(serio, pulse8);
455 INIT_WORK(&pulse8->work, pulse8_irq_work_handler);
456
457 err = serio_open(serio, drv);
458 if (err)
459 goto delete_adap;
460
461 err = pulse8_setup(pulse8, serio);
462 if (err)
463 goto close_serio;
464
465 err = cec_register_adapter(pulse8->adap);
466 if (err < 0)
467 goto close_serio;
468
469 pulse8->dev = &pulse8->adap->devnode.dev;
470 return 0;
471
472close_serio:
473 serio_close(serio);
474delete_adap:
475 cec_delete_adapter(pulse8->adap);
476 serio_set_drvdata(serio, NULL);
477free_device:
478 kfree(pulse8);
479 return err;
480}
481
482static struct serio_device_id pulse8_serio_ids[] = {
483 {
484 .type = SERIO_RS232,
485 .proto = SERIO_PULSE8_CEC,
486 .id = SERIO_ANY,
487 .extra = SERIO_ANY,
488 },
489 { 0 }
490};
491
492MODULE_DEVICE_TABLE(serio, pulse8_serio_ids);
493
494static struct serio_driver pulse8_drv = {
495 .driver = {
496 .name = "pulse8-cec",
497 },
498 .description = "Pulse Eight HDMI CEC driver",
499 .id_table = pulse8_serio_ids,
500 .interrupt = pulse8_interrupt,
501 .connect = pulse8_connect,
502 .disconnect = pulse8_disconnect,
503};
504
505module_serio_driver(pulse8_drv);
diff --git a/drivers/staging/media/s5p-cec/Kconfig b/drivers/staging/media/s5p-cec/Kconfig
new file mode 100644
index 000000000000..0315fd7ad0f1
--- /dev/null
+++ b/drivers/staging/media/s5p-cec/Kconfig
@@ -0,0 +1,9 @@
1config VIDEO_SAMSUNG_S5P_CEC
2 tristate "Samsung S5P CEC driver"
3 depends on VIDEO_DEV && MEDIA_CEC && (PLAT_S5P || ARCH_EXYNOS || COMPILE_TEST)
4 ---help---
5 This is a driver for Samsung S5P HDMI CEC interface. It uses the
6 generic CEC framework interface.
7 CEC bus is present in the HDMI connector and enables communication
8 between compatible devices.
9
diff --git a/drivers/staging/media/s5p-cec/Makefile b/drivers/staging/media/s5p-cec/Makefile
new file mode 100644
index 000000000000..0e2cf457825a
--- /dev/null
+++ b/drivers/staging/media/s5p-cec/Makefile
@@ -0,0 +1,2 @@
1obj-$(CONFIG_VIDEO_SAMSUNG_S5P_CEC) += s5p-cec.o
2s5p-cec-y += s5p_cec.o exynos_hdmi_cecctrl.o
diff --git a/drivers/staging/media/s5p-cec/TODO b/drivers/staging/media/s5p-cec/TODO
new file mode 100644
index 000000000000..f51d5268ac40
--- /dev/null
+++ b/drivers/staging/media/s5p-cec/TODO
@@ -0,0 +1,7 @@
1This driver depends on the CEC framework, which is currently in
2staging, so therefor this driver is in staging as well.
3
4In addition, this driver requires that userspace sets the physical
5address. However, this should be passed on from the corresponding
6samsung HDMI driver. It is very annoying if userspace has to do this,
7and other than USB CEC adapters this must be handled automatically.
diff --git a/drivers/staging/media/s5p-cec/exynos_hdmi_cec.h b/drivers/staging/media/s5p-cec/exynos_hdmi_cec.h
new file mode 100644
index 000000000000..3e4fc7b05e83
--- /dev/null
+++ b/drivers/staging/media/s5p-cec/exynos_hdmi_cec.h
@@ -0,0 +1,38 @@
1/* drivers/media/platform/s5p-cec/exynos_hdmi_cec.h
2 *
3 * Copyright (c) 2010, 2014 Samsung Electronics
4 * http://www.samsung.com/
5 *
6 * Header file for interface of Samsung Exynos hdmi cec hardware
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 */
12
13#ifndef _EXYNOS_HDMI_CEC_H_
14#define _EXYNOS_HDMI_CEC_H_ __FILE__
15
16#include <linux/regmap.h>
17#include <linux/miscdevice.h>
18#include "s5p_cec.h"
19
20void s5p_cec_set_divider(struct s5p_cec_dev *cec);
21void s5p_cec_enable_rx(struct s5p_cec_dev *cec);
22void s5p_cec_mask_rx_interrupts(struct s5p_cec_dev *cec);
23void s5p_cec_unmask_rx_interrupts(struct s5p_cec_dev *cec);
24void s5p_cec_mask_tx_interrupts(struct s5p_cec_dev *cec);
25void s5p_cec_unmask_tx_interrupts(struct s5p_cec_dev *cec);
26void s5p_cec_reset(struct s5p_cec_dev *cec);
27void s5p_cec_tx_reset(struct s5p_cec_dev *cec);
28void s5p_cec_rx_reset(struct s5p_cec_dev *cec);
29void s5p_cec_threshold(struct s5p_cec_dev *cec);
30void s5p_cec_copy_packet(struct s5p_cec_dev *cec, char *data,
31 size_t count, u8 retries);
32void s5p_cec_set_addr(struct s5p_cec_dev *cec, u32 addr);
33u32 s5p_cec_get_status(struct s5p_cec_dev *cec);
34void s5p_clr_pending_tx(struct s5p_cec_dev *cec);
35void s5p_clr_pending_rx(struct s5p_cec_dev *cec);
36void s5p_cec_get_rx_buf(struct s5p_cec_dev *cec, u32 size, u8 *buffer);
37
38#endif /* _EXYNOS_HDMI_CEC_H_ */
diff --git a/drivers/staging/media/s5p-cec/exynos_hdmi_cecctrl.c b/drivers/staging/media/s5p-cec/exynos_hdmi_cecctrl.c
new file mode 100644
index 000000000000..ce95e0fcd882
--- /dev/null
+++ b/drivers/staging/media/s5p-cec/exynos_hdmi_cecctrl.c
@@ -0,0 +1,209 @@
1/* drivers/media/platform/s5p-cec/exynos_hdmi_cecctrl.c
2 *
3 * Copyright (c) 2009, 2014 Samsung Electronics
4 * http://www.samsung.com/
5 *
6 * cec ftn file for Samsung TVOUT driver
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 */
12
13#include <linux/io.h>
14#include <linux/device.h>
15
16#include "exynos_hdmi_cec.h"
17#include "regs-cec.h"
18
19#define S5P_HDMI_FIN 24000000
20#define CEC_DIV_RATIO 320000
21
22#define CEC_MESSAGE_BROADCAST_MASK 0x0F
23#define CEC_MESSAGE_BROADCAST 0x0F
24#define CEC_FILTER_THRESHOLD 0x15
25
26void s5p_cec_set_divider(struct s5p_cec_dev *cec)
27{
28 u32 div_ratio, div_val;
29 unsigned int reg;
30
31 div_ratio = S5P_HDMI_FIN / CEC_DIV_RATIO - 1;
32
33 if (regmap_read(cec->pmu, EXYNOS_HDMI_PHY_CONTROL, &reg)) {
34 dev_err(cec->dev, "failed to read phy control\n");
35 return;
36 }
37
38 reg = (reg & ~(0x3FF << 16)) | (div_ratio << 16);
39
40 if (regmap_write(cec->pmu, EXYNOS_HDMI_PHY_CONTROL, reg)) {
41 dev_err(cec->dev, "failed to write phy control\n");
42 return;
43 }
44
45 div_val = CEC_DIV_RATIO * 0.00005 - 1;
46
47 writeb(0x0, cec->reg + S5P_CEC_DIVISOR_3);
48 writeb(0x0, cec->reg + S5P_CEC_DIVISOR_2);
49 writeb(0x0, cec->reg + S5P_CEC_DIVISOR_1);
50 writeb(div_val, cec->reg + S5P_CEC_DIVISOR_0);
51}
52
53void s5p_cec_enable_rx(struct s5p_cec_dev *cec)
54{
55 u8 reg;
56
57 reg = readb(cec->reg + S5P_CEC_RX_CTRL);
58 reg |= S5P_CEC_RX_CTRL_ENABLE;
59 writeb(reg, cec->reg + S5P_CEC_RX_CTRL);
60}
61
62void s5p_cec_mask_rx_interrupts(struct s5p_cec_dev *cec)
63{
64 u8 reg;
65
66 reg = readb(cec->reg + S5P_CEC_IRQ_MASK);
67 reg |= S5P_CEC_IRQ_RX_DONE;
68 reg |= S5P_CEC_IRQ_RX_ERROR;
69 writeb(reg, cec->reg + S5P_CEC_IRQ_MASK);
70}
71
72void s5p_cec_unmask_rx_interrupts(struct s5p_cec_dev *cec)
73{
74 u8 reg;
75
76 reg = readb(cec->reg + S5P_CEC_IRQ_MASK);
77 reg &= ~S5P_CEC_IRQ_RX_DONE;
78 reg &= ~S5P_CEC_IRQ_RX_ERROR;
79 writeb(reg, cec->reg + S5P_CEC_IRQ_MASK);
80}
81
82void s5p_cec_mask_tx_interrupts(struct s5p_cec_dev *cec)
83{
84 u8 reg;
85
86 reg = readb(cec->reg + S5P_CEC_IRQ_MASK);
87 reg |= S5P_CEC_IRQ_TX_DONE;
88 reg |= S5P_CEC_IRQ_TX_ERROR;
89 writeb(reg, cec->reg + S5P_CEC_IRQ_MASK);
90
91}
92
93void s5p_cec_unmask_tx_interrupts(struct s5p_cec_dev *cec)
94{
95 u8 reg;
96
97 reg = readb(cec->reg + S5P_CEC_IRQ_MASK);
98 reg &= ~S5P_CEC_IRQ_TX_DONE;
99 reg &= ~S5P_CEC_IRQ_TX_ERROR;
100 writeb(reg, cec->reg + S5P_CEC_IRQ_MASK);
101}
102
103void s5p_cec_reset(struct s5p_cec_dev *cec)
104{
105 u8 reg;
106
107 writeb(S5P_CEC_RX_CTRL_RESET, cec->reg + S5P_CEC_RX_CTRL);
108 writeb(S5P_CEC_TX_CTRL_RESET, cec->reg + S5P_CEC_TX_CTRL);
109
110 reg = readb(cec->reg + 0xc4);
111 reg &= ~0x1;
112 writeb(reg, cec->reg + 0xc4);
113}
114
115void s5p_cec_tx_reset(struct s5p_cec_dev *cec)
116{
117 writeb(S5P_CEC_TX_CTRL_RESET, cec->reg + S5P_CEC_TX_CTRL);
118}
119
120void s5p_cec_rx_reset(struct s5p_cec_dev *cec)
121{
122 u8 reg;
123
124 writeb(S5P_CEC_RX_CTRL_RESET, cec->reg + S5P_CEC_RX_CTRL);
125
126 reg = readb(cec->reg + 0xc4);
127 reg &= ~0x1;
128 writeb(reg, cec->reg + 0xc4);
129}
130
131void s5p_cec_threshold(struct s5p_cec_dev *cec)
132{
133 writeb(CEC_FILTER_THRESHOLD, cec->reg + S5P_CEC_RX_FILTER_TH);
134 writeb(0, cec->reg + S5P_CEC_RX_FILTER_CTRL);
135}
136
137void s5p_cec_copy_packet(struct s5p_cec_dev *cec, char *data,
138 size_t count, u8 retries)
139{
140 int i = 0;
141 u8 reg;
142
143 while (i < count) {
144 writeb(data[i], cec->reg + (S5P_CEC_TX_BUFF0 + (i * 4)));
145 i++;
146 }
147
148 writeb(count, cec->reg + S5P_CEC_TX_BYTES);
149 reg = readb(cec->reg + S5P_CEC_TX_CTRL);
150 reg |= S5P_CEC_TX_CTRL_START;
151 reg &= ~0x70;
152 reg |= retries << 4;
153
154 if ((data[0] & CEC_MESSAGE_BROADCAST_MASK) == CEC_MESSAGE_BROADCAST) {
155 dev_dbg(cec->dev, "Broadcast");
156 reg |= S5P_CEC_TX_CTRL_BCAST;
157 } else {
158 dev_dbg(cec->dev, "No Broadcast");
159 reg &= ~S5P_CEC_TX_CTRL_BCAST;
160 }
161
162 writeb(reg, cec->reg + S5P_CEC_TX_CTRL);
163 dev_dbg(cec->dev, "cec-tx: cec count (%zu): %*ph", count,
164 (int)count, data);
165}
166
167void s5p_cec_set_addr(struct s5p_cec_dev *cec, u32 addr)
168{
169 writeb(addr & 0x0F, cec->reg + S5P_CEC_LOGIC_ADDR);
170}
171
172u32 s5p_cec_get_status(struct s5p_cec_dev *cec)
173{
174 u32 status = 0;
175
176 status = readb(cec->reg + S5P_CEC_STATUS_0);
177 status |= readb(cec->reg + S5P_CEC_STATUS_1) << 8;
178 status |= readb(cec->reg + S5P_CEC_STATUS_2) << 16;
179 status |= readb(cec->reg + S5P_CEC_STATUS_3) << 24;
180
181 dev_dbg(cec->dev, "status = 0x%x!\n", status);
182
183 return status;
184}
185
186void s5p_clr_pending_tx(struct s5p_cec_dev *cec)
187{
188 writeb(S5P_CEC_IRQ_TX_DONE | S5P_CEC_IRQ_TX_ERROR,
189 cec->reg + S5P_CEC_IRQ_CLEAR);
190}
191
192void s5p_clr_pending_rx(struct s5p_cec_dev *cec)
193{
194 writeb(S5P_CEC_IRQ_RX_DONE | S5P_CEC_IRQ_RX_ERROR,
195 cec->reg + S5P_CEC_IRQ_CLEAR);
196}
197
198void s5p_cec_get_rx_buf(struct s5p_cec_dev *cec, u32 size, u8 *buffer)
199{
200 u32 i = 0;
201 char debug[40];
202
203 while (i < size) {
204 buffer[i] = readb(cec->reg + S5P_CEC_RX_BUFF0 + (i * 4));
205 sprintf(debug + i * 2, "%02x ", buffer[i]);
206 i++;
207 }
208 dev_dbg(cec->dev, "cec-rx: cec size(%d): %s", size, debug);
209}
diff --git a/drivers/staging/media/s5p-cec/regs-cec.h b/drivers/staging/media/s5p-cec/regs-cec.h
new file mode 100644
index 000000000000..b2e7e129920e
--- /dev/null
+++ b/drivers/staging/media/s5p-cec/regs-cec.h
@@ -0,0 +1,96 @@
1/* drivers/media/platform/s5p-cec/regs-cec.h
2 *
3 * Copyright (c) 2010 Samsung Electronics
4 * http://www.samsung.com/
5 *
6 * register header file for Samsung TVOUT driver
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 */
12
13#ifndef __EXYNOS_REGS__H
14#define __EXYNOS_REGS__H
15
16/*
17 * Register part
18 */
19#define S5P_CEC_STATUS_0 (0x0000)
20#define S5P_CEC_STATUS_1 (0x0004)
21#define S5P_CEC_STATUS_2 (0x0008)
22#define S5P_CEC_STATUS_3 (0x000C)
23#define S5P_CEC_IRQ_MASK (0x0010)
24#define S5P_CEC_IRQ_CLEAR (0x0014)
25#define S5P_CEC_LOGIC_ADDR (0x0020)
26#define S5P_CEC_DIVISOR_0 (0x0030)
27#define S5P_CEC_DIVISOR_1 (0x0034)
28#define S5P_CEC_DIVISOR_2 (0x0038)
29#define S5P_CEC_DIVISOR_3 (0x003C)
30
31#define S5P_CEC_TX_CTRL (0x0040)
32#define S5P_CEC_TX_BYTES (0x0044)
33#define S5P_CEC_TX_STAT0 (0x0060)
34#define S5P_CEC_TX_STAT1 (0x0064)
35#define S5P_CEC_TX_BUFF0 (0x0080)
36#define S5P_CEC_TX_BUFF1 (0x0084)
37#define S5P_CEC_TX_BUFF2 (0x0088)
38#define S5P_CEC_TX_BUFF3 (0x008C)
39#define S5P_CEC_TX_BUFF4 (0x0090)
40#define S5P_CEC_TX_BUFF5 (0x0094)
41#define S5P_CEC_TX_BUFF6 (0x0098)
42#define S5P_CEC_TX_BUFF7 (0x009C)
43#define S5P_CEC_TX_BUFF8 (0x00A0)
44#define S5P_CEC_TX_BUFF9 (0x00A4)
45#define S5P_CEC_TX_BUFF10 (0x00A8)
46#define S5P_CEC_TX_BUFF11 (0x00AC)
47#define S5P_CEC_TX_BUFF12 (0x00B0)
48#define S5P_CEC_TX_BUFF13 (0x00B4)
49#define S5P_CEC_TX_BUFF14 (0x00B8)
50#define S5P_CEC_TX_BUFF15 (0x00BC)
51
52#define S5P_CEC_RX_CTRL (0x00C0)
53#define S5P_CEC_RX_STAT0 (0x00E0)
54#define S5P_CEC_RX_STAT1 (0x00E4)
55#define S5P_CEC_RX_BUFF0 (0x0100)
56#define S5P_CEC_RX_BUFF1 (0x0104)
57#define S5P_CEC_RX_BUFF2 (0x0108)
58#define S5P_CEC_RX_BUFF3 (0x010C)
59#define S5P_CEC_RX_BUFF4 (0x0110)
60#define S5P_CEC_RX_BUFF5 (0x0114)
61#define S5P_CEC_RX_BUFF6 (0x0118)
62#define S5P_CEC_RX_BUFF7 (0x011C)
63#define S5P_CEC_RX_BUFF8 (0x0120)
64#define S5P_CEC_RX_BUFF9 (0x0124)
65#define S5P_CEC_RX_BUFF10 (0x0128)
66#define S5P_CEC_RX_BUFF11 (0x012C)
67#define S5P_CEC_RX_BUFF12 (0x0130)
68#define S5P_CEC_RX_BUFF13 (0x0134)
69#define S5P_CEC_RX_BUFF14 (0x0138)
70#define S5P_CEC_RX_BUFF15 (0x013C)
71
72#define S5P_CEC_RX_FILTER_CTRL (0x0180)
73#define S5P_CEC_RX_FILTER_TH (0x0184)
74
75/*
76 * Bit definition part
77 */
78#define S5P_CEC_IRQ_TX_DONE (1<<0)
79#define S5P_CEC_IRQ_TX_ERROR (1<<1)
80#define S5P_CEC_IRQ_RX_DONE (1<<4)
81#define S5P_CEC_IRQ_RX_ERROR (1<<5)
82
83#define S5P_CEC_TX_CTRL_START (1<<0)
84#define S5P_CEC_TX_CTRL_BCAST (1<<1)
85#define S5P_CEC_TX_CTRL_RETRY (0x04<<4)
86#define S5P_CEC_TX_CTRL_RESET (1<<7)
87
88#define S5P_CEC_RX_CTRL_ENABLE (1<<0)
89#define S5P_CEC_RX_CTRL_RESET (1<<7)
90
91#define S5P_CEC_LOGIC_ADDR_MASK (0xF)
92
93/* PMU Registers for PHY */
94#define EXYNOS_HDMI_PHY_CONTROL 0x700
95
96#endif /* __EXYNOS_REGS__H */
diff --git a/drivers/staging/media/s5p-cec/s5p_cec.c b/drivers/staging/media/s5p-cec/s5p_cec.c
new file mode 100644
index 000000000000..78333273c4e5
--- /dev/null
+++ b/drivers/staging/media/s5p-cec/s5p_cec.c
@@ -0,0 +1,294 @@
1/* drivers/media/platform/s5p-cec/s5p_cec.c
2 *
3 * Samsung S5P CEC driver
4 *
5 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This driver is based on the "cec interface driver for exynos soc" by
13 * SangPil Moon.
14 */
15
16#include <linux/clk.h>
17#include <linux/interrupt.h>
18#include <linux/kernel.h>
19#include <linux/mfd/syscon.h>
20#include <linux/module.h>
21#include <linux/of.h>
22#include <linux/platform_device.h>
23#include <linux/pm_runtime.h>
24#include <linux/timer.h>
25#include <linux/version.h>
26#include <linux/workqueue.h>
27#include <media/cec.h>
28
29#include "exynos_hdmi_cec.h"
30#include "regs-cec.h"
31#include "s5p_cec.h"
32
33#define CEC_NAME "s5p-cec"
34
35static int debug;
36module_param(debug, int, 0644);
37MODULE_PARM_DESC(debug, "debug level (0-2)");
38
39static int s5p_cec_adap_enable(struct cec_adapter *adap, bool enable)
40{
41 struct s5p_cec_dev *cec = adap->priv;
42
43 if (enable) {
44 pm_runtime_get_sync(cec->dev);
45
46 s5p_cec_reset(cec);
47
48 s5p_cec_set_divider(cec);
49 s5p_cec_threshold(cec);
50
51 s5p_cec_unmask_tx_interrupts(cec);
52 s5p_cec_unmask_rx_interrupts(cec);
53 s5p_cec_enable_rx(cec);
54 } else {
55 s5p_cec_mask_tx_interrupts(cec);
56 s5p_cec_mask_rx_interrupts(cec);
57 pm_runtime_disable(cec->dev);
58 }
59
60 return 0;
61}
62
63static int s5p_cec_adap_log_addr(struct cec_adapter *adap, u8 addr)
64{
65 struct s5p_cec_dev *cec = adap->priv;
66
67 s5p_cec_set_addr(cec, addr);
68 return 0;
69}
70
71static int s5p_cec_adap_transmit(struct cec_adapter *adap, u8 attempts,
72 u32 signal_free_time, struct cec_msg *msg)
73{
74 struct s5p_cec_dev *cec = adap->priv;
75
76 /*
77 * Unclear if 0 retries are allowed by the hardware, so have 1 as
78 * the minimum.
79 */
80 s5p_cec_copy_packet(cec, msg->msg, msg->len, max(1, attempts - 1));
81 return 0;
82}
83
84static irqreturn_t s5p_cec_irq_handler(int irq, void *priv)
85{
86 struct s5p_cec_dev *cec = priv;
87 u32 status = 0;
88
89 status = s5p_cec_get_status(cec);
90
91 dev_dbg(cec->dev, "irq received\n");
92
93 if (status & CEC_STATUS_TX_DONE) {
94 if (status & CEC_STATUS_TX_ERROR) {
95 dev_dbg(cec->dev, "CEC_STATUS_TX_ERROR set\n");
96 cec->tx = STATE_ERROR;
97 } else {
98 dev_dbg(cec->dev, "CEC_STATUS_TX_DONE\n");
99 cec->tx = STATE_DONE;
100 }
101 s5p_clr_pending_tx(cec);
102 }
103
104 if (status & CEC_STATUS_RX_DONE) {
105 if (status & CEC_STATUS_RX_ERROR) {
106 dev_dbg(cec->dev, "CEC_STATUS_RX_ERROR set\n");
107 s5p_cec_rx_reset(cec);
108 s5p_cec_enable_rx(cec);
109 } else {
110 dev_dbg(cec->dev, "CEC_STATUS_RX_DONE set\n");
111 if (cec->rx != STATE_IDLE)
112 dev_dbg(cec->dev, "Buffer overrun (worker did not process previous message)\n");
113 cec->rx = STATE_BUSY;
114 cec->msg.len = status >> 24;
115 cec->msg.rx_status = CEC_RX_STATUS_OK;
116 s5p_cec_get_rx_buf(cec, cec->msg.len,
117 cec->msg.msg);
118 cec->rx = STATE_DONE;
119 s5p_cec_enable_rx(cec);
120 }
121 /* Clear interrupt pending bit */
122 s5p_clr_pending_rx(cec);
123 }
124 return IRQ_WAKE_THREAD;
125}
126
127static irqreturn_t s5p_cec_irq_handler_thread(int irq, void *priv)
128{
129 struct s5p_cec_dev *cec = priv;
130
131 dev_dbg(cec->dev, "irq processing thread\n");
132 switch (cec->tx) {
133 case STATE_DONE:
134 cec_transmit_done(cec->adap, CEC_TX_STATUS_OK, 0, 0, 0, 0);
135 cec->tx = STATE_IDLE;
136 break;
137 case STATE_ERROR:
138 cec_transmit_done(cec->adap,
139 CEC_TX_STATUS_MAX_RETRIES | CEC_TX_STATUS_ERROR,
140 0, 0, 0, 1);
141 cec->tx = STATE_IDLE;
142 break;
143 case STATE_BUSY:
144 dev_err(cec->dev, "state set to busy, this should not occur here\n");
145 break;
146 default:
147 break;
148 }
149
150 switch (cec->rx) {
151 case STATE_DONE:
152 cec_received_msg(cec->adap, &cec->msg);
153 cec->rx = STATE_IDLE;
154 break;
155 default:
156 break;
157 }
158
159 return IRQ_HANDLED;
160}
161
162static const struct cec_adap_ops s5p_cec_adap_ops = {
163 .adap_enable = s5p_cec_adap_enable,
164 .adap_log_addr = s5p_cec_adap_log_addr,
165 .adap_transmit = s5p_cec_adap_transmit,
166};
167
168static int s5p_cec_probe(struct platform_device *pdev)
169{
170 struct device *dev = &pdev->dev;
171 struct resource *res;
172 struct s5p_cec_dev *cec;
173 int ret;
174
175 cec = devm_kzalloc(&pdev->dev, sizeof(*cec), GFP_KERNEL);
176 if (!dev)
177 return -ENOMEM;
178
179 cec->dev = dev;
180
181 cec->irq = platform_get_irq(pdev, 0);
182 if (cec->irq < 0)
183 return cec->irq;
184
185 ret = devm_request_threaded_irq(dev, cec->irq, s5p_cec_irq_handler,
186 s5p_cec_irq_handler_thread, 0, pdev->name, cec);
187 if (ret)
188 return ret;
189
190 cec->clk = devm_clk_get(dev, "hdmicec");
191 if (IS_ERR(cec->clk))
192 return PTR_ERR(cec->clk);
193
194 cec->pmu = syscon_regmap_lookup_by_phandle(dev->of_node,
195 "samsung,syscon-phandle");
196 if (IS_ERR(cec->pmu))
197 return -EPROBE_DEFER;
198
199 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
200 cec->reg = devm_ioremap_resource(dev, res);
201 if (IS_ERR(cec->reg))
202 return PTR_ERR(cec->reg);
203
204 cec->adap = cec_allocate_adapter(&s5p_cec_adap_ops, cec,
205 CEC_NAME,
206 CEC_CAP_PHYS_ADDR | CEC_CAP_LOG_ADDRS | CEC_CAP_TRANSMIT |
207 CEC_CAP_PASSTHROUGH | CEC_CAP_RC,
208 1, &pdev->dev);
209 ret = PTR_ERR_OR_ZERO(cec->adap);
210 if (ret)
211 return ret;
212 ret = cec_register_adapter(cec->adap);
213 if (ret) {
214 cec_delete_adapter(cec->adap);
215 return ret;
216 }
217
218 platform_set_drvdata(pdev, cec);
219 pm_runtime_enable(dev);
220
221 dev_dbg(dev, "successfuly probed\n");
222 return 0;
223}
224
225static int s5p_cec_remove(struct platform_device *pdev)
226{
227 struct s5p_cec_dev *cec = platform_get_drvdata(pdev);
228
229 cec_unregister_adapter(cec->adap);
230 pm_runtime_disable(&pdev->dev);
231 return 0;
232}
233
234static int s5p_cec_runtime_suspend(struct device *dev)
235{
236 struct s5p_cec_dev *cec = dev_get_drvdata(dev);
237
238 clk_disable_unprepare(cec->clk);
239 return 0;
240}
241
242static int s5p_cec_runtime_resume(struct device *dev)
243{
244 struct s5p_cec_dev *cec = dev_get_drvdata(dev);
245 int ret;
246
247 ret = clk_prepare_enable(cec->clk);
248 if (ret < 0)
249 return ret;
250 return 0;
251}
252
253static int __maybe_unused s5p_cec_suspend(struct device *dev)
254{
255 if (pm_runtime_suspended(dev))
256 return 0;
257 return s5p_cec_runtime_suspend(dev);
258}
259
260static int __maybe_unused s5p_cec_resume(struct device *dev)
261{
262 if (pm_runtime_suspended(dev))
263 return 0;
264 return s5p_cec_runtime_resume(dev);
265}
266
267static const struct dev_pm_ops s5p_cec_pm_ops = {
268 SET_SYSTEM_SLEEP_PM_OPS(s5p_cec_suspend, s5p_cec_resume)
269 SET_RUNTIME_PM_OPS(s5p_cec_runtime_suspend, s5p_cec_runtime_resume,
270 NULL)
271};
272
273static const struct of_device_id s5p_cec_match[] = {
274 {
275 .compatible = "samsung,s5p-cec",
276 },
277 {},
278};
279
280static struct platform_driver s5p_cec_pdrv = {
281 .probe = s5p_cec_probe,
282 .remove = s5p_cec_remove,
283 .driver = {
284 .name = CEC_NAME,
285 .of_match_table = s5p_cec_match,
286 .pm = &s5p_cec_pm_ops,
287 },
288};
289
290module_platform_driver(s5p_cec_pdrv);
291
292MODULE_AUTHOR("Kamil Debski <kamil@wypas.org>");
293MODULE_LICENSE("GPL");
294MODULE_DESCRIPTION("Samsung S5P CEC driver");
diff --git a/drivers/staging/media/s5p-cec/s5p_cec.h b/drivers/staging/media/s5p-cec/s5p_cec.h
new file mode 100644
index 000000000000..03732c13d19f
--- /dev/null
+++ b/drivers/staging/media/s5p-cec/s5p_cec.h
@@ -0,0 +1,76 @@
1/* drivers/media/platform/s5p-cec/s5p_cec.h
2 *
3 * Samsung S5P HDMI CEC driver
4 *
5 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 */
12
13#ifndef _S5P_CEC_H_
14#define _S5P_CEC_H_ __FILE__
15
16#include <linux/clk.h>
17#include <linux/interrupt.h>
18#include <linux/kernel.h>
19#include <linux/mfd/syscon.h>
20#include <linux/module.h>
21#include <linux/of.h>
22#include <linux/platform_device.h>
23#include <linux/pm_runtime.h>
24#include <linux/timer.h>
25#include <linux/version.h>
26#include <linux/workqueue.h>
27#include <media/cec.h>
28
29#include "exynos_hdmi_cec.h"
30#include "regs-cec.h"
31#include "s5p_cec.h"
32
33#define CEC_NAME "s5p-cec"
34
35#define CEC_STATUS_TX_RUNNING (1 << 0)
36#define CEC_STATUS_TX_TRANSFERRING (1 << 1)
37#define CEC_STATUS_TX_DONE (1 << 2)
38#define CEC_STATUS_TX_ERROR (1 << 3)
39#define CEC_STATUS_TX_BYTES (0xFF << 8)
40#define CEC_STATUS_RX_RUNNING (1 << 16)
41#define CEC_STATUS_RX_RECEIVING (1 << 17)
42#define CEC_STATUS_RX_DONE (1 << 18)
43#define CEC_STATUS_RX_ERROR (1 << 19)
44#define CEC_STATUS_RX_BCAST (1 << 20)
45#define CEC_STATUS_RX_BYTES (0xFF << 24)
46
47#define CEC_WORKER_TX_DONE (1 << 0)
48#define CEC_WORKER_RX_MSG (1 << 1)
49
50/* CEC Rx buffer size */
51#define CEC_RX_BUFF_SIZE 16
52/* CEC Tx buffer size */
53#define CEC_TX_BUFF_SIZE 16
54
55enum cec_state {
56 STATE_IDLE,
57 STATE_BUSY,
58 STATE_DONE,
59 STATE_ERROR
60};
61
62struct s5p_cec_dev {
63 struct cec_adapter *adap;
64 struct clk *clk;
65 struct device *dev;
66 struct mutex lock;
67 struct regmap *pmu;
68 int irq;
69 void __iomem *reg;
70
71 enum cec_state rx;
72 enum cec_state tx;
73 struct cec_msg msg;
74};
75
76#endif /* _S5P_CEC_H_ */
diff --git a/drivers/staging/media/timb/Kconfig b/drivers/staging/media/timb/Kconfig
deleted file mode 100644
index e413fecc1e67..000000000000
--- a/drivers/staging/media/timb/Kconfig
+++ /dev/null
@@ -1,11 +0,0 @@
1config VIDEO_TIMBERDALE
2 tristate "Support for timberdale Video In/LogiWIN"
3 depends on VIDEO_V4L2 && I2C && VIDEO_V4L2_SUBDEV_API && HAS_DMA
4 depends on (MFD_TIMBERDALE && TIMB_DMA) || COMPILE_TEST
5 select VIDEO_ADV7180
6 select VIDEOBUF_DMA_CONTIG
7 ---help---
8 Add support for the Video In peripherial of the timberdale FPGA.
9
10 This driver is deprecated and will be removed soon unless someone
11 will start the work to convert this driver to the vb2 framework.
diff --git a/drivers/staging/media/timb/Makefile b/drivers/staging/media/timb/Makefile
deleted file mode 100644
index 4c989c23a0e0..000000000000
--- a/drivers/staging/media/timb/Makefile
+++ /dev/null
@@ -1 +0,0 @@
1obj-$(CONFIG_VIDEO_TIMBERDALE) += timblogiw.o
diff --git a/drivers/staging/media/timb/timblogiw.c b/drivers/staging/media/timb/timblogiw.c
deleted file mode 100644
index 113c9f3c0b3e..000000000000
--- a/drivers/staging/media/timb/timblogiw.c
+++ /dev/null
@@ -1,870 +0,0 @@
1/*
2 * timblogiw.c timberdale FPGA LogiWin Video In driver
3 * Copyright (c) 2009-2010 Intel Corporation
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 */
18
19/* Supports:
20 * Timberdale FPGA LogiWin Video In
21 */
22
23#include <linux/platform_device.h>
24#include <linux/slab.h>
25#include <linux/dmaengine.h>
26#include <linux/scatterlist.h>
27#include <linux/interrupt.h>
28#include <linux/list.h>
29#include <linux/i2c.h>
30#include <linux/module.h>
31#include <media/v4l2-ioctl.h>
32#include <media/v4l2-device.h>
33#include <media/videobuf-dma-contig.h>
34#include <linux/platform_data/media/timb_video.h>
35
36#define DRIVER_NAME "timb-video"
37
38#define TIMBLOGIWIN_NAME "Timberdale Video-In"
39#define TIMBLOGIW_VERSION_CODE 0x04
40
41#define TIMBLOGIW_LINES_PER_DESC 44
42#define TIMBLOGIW_MAX_VIDEO_MEM 16
43
44#define TIMBLOGIW_HAS_DECODER(lw) (lw->pdata.encoder.module_name)
45
46
47struct timblogiw {
48 struct video_device video_dev;
49 struct v4l2_device v4l2_dev; /* mutual exclusion */
50 struct mutex lock;
51 struct device *dev;
52 struct timb_video_platform_data pdata;
53 struct v4l2_subdev *sd_enc; /* encoder */
54 bool opened;
55};
56
57struct timblogiw_tvnorm {
58 v4l2_std_id std;
59 u16 width;
60 u16 height;
61 u8 fps;
62};
63
64struct timblogiw_fh {
65 struct videobuf_queue vb_vidq;
66 struct timblogiw_tvnorm const *cur_norm;
67 struct list_head capture;
68 struct dma_chan *chan;
69 spinlock_t queue_lock; /* mutual exclusion */
70 unsigned int frame_count;
71};
72
73struct timblogiw_buffer {
74 /* common v4l buffer stuff -- must be first */
75 struct videobuf_buffer vb;
76 struct scatterlist sg[16];
77 dma_cookie_t cookie;
78 struct timblogiw_fh *fh;
79};
80
81static const struct timblogiw_tvnorm timblogiw_tvnorms[] = {
82 {
83 .std = V4L2_STD_PAL,
84 .width = 720,
85 .height = 576,
86 .fps = 25
87 },
88 {
89 .std = V4L2_STD_NTSC,
90 .width = 720,
91 .height = 480,
92 .fps = 30
93 }
94};
95
96static int timblogiw_bytes_per_line(const struct timblogiw_tvnorm *norm)
97{
98 return norm->width * 2;
99}
100
101
102static int timblogiw_frame_size(const struct timblogiw_tvnorm *norm)
103{
104 return norm->height * timblogiw_bytes_per_line(norm);
105}
106
107static const struct timblogiw_tvnorm *timblogiw_get_norm(const v4l2_std_id std)
108{
109 int i;
110 for (i = 0; i < ARRAY_SIZE(timblogiw_tvnorms); i++)
111 if (timblogiw_tvnorms[i].std & std)
112 return timblogiw_tvnorms + i;
113
114 /* default to first element */
115 return timblogiw_tvnorms;
116}
117
118static void timblogiw_dma_cb(void *data)
119{
120 struct timblogiw_buffer *buf = data;
121 struct timblogiw_fh *fh = buf->fh;
122 struct videobuf_buffer *vb = &buf->vb;
123
124 spin_lock(&fh->queue_lock);
125
126 /* mark the transfer done */
127 buf->cookie = -1;
128
129 fh->frame_count++;
130
131 if (vb->state != VIDEOBUF_ERROR) {
132 list_del(&vb->queue);
133 v4l2_get_timestamp(&vb->ts);
134 vb->field_count = fh->frame_count * 2;
135 vb->state = VIDEOBUF_DONE;
136
137 wake_up(&vb->done);
138 }
139
140 if (!list_empty(&fh->capture)) {
141 vb = list_entry(fh->capture.next, struct videobuf_buffer,
142 queue);
143 vb->state = VIDEOBUF_ACTIVE;
144 }
145
146 spin_unlock(&fh->queue_lock);
147}
148
149static bool timblogiw_dma_filter_fn(struct dma_chan *chan, void *filter_param)
150{
151 return chan->chan_id == (uintptr_t)filter_param;
152}
153
154/* IOCTL functions */
155
156static int timblogiw_g_fmt(struct file *file, void *priv,
157 struct v4l2_format *format)
158{
159 struct video_device *vdev = video_devdata(file);
160 struct timblogiw *lw = video_get_drvdata(vdev);
161 struct timblogiw_fh *fh = priv;
162
163 dev_dbg(&vdev->dev, "%s entry\n", __func__);
164
165 if (format->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
166 return -EINVAL;
167
168 mutex_lock(&lw->lock);
169
170 format->fmt.pix.width = fh->cur_norm->width;
171 format->fmt.pix.height = fh->cur_norm->height;
172 format->fmt.pix.pixelformat = V4L2_PIX_FMT_UYVY;
173 format->fmt.pix.bytesperline = timblogiw_bytes_per_line(fh->cur_norm);
174 format->fmt.pix.sizeimage = timblogiw_frame_size(fh->cur_norm);
175 format->fmt.pix.field = V4L2_FIELD_NONE;
176
177 mutex_unlock(&lw->lock);
178
179 return 0;
180}
181
182static int timblogiw_try_fmt(struct file *file, void *priv,
183 struct v4l2_format *format)
184{
185 struct video_device *vdev = video_devdata(file);
186 struct v4l2_pix_format *pix = &format->fmt.pix;
187
188 dev_dbg(&vdev->dev,
189 "%s - width=%d, height=%d, pixelformat=%d, field=%d\n"
190 "bytes per line %d, size image: %d, colorspace: %d\n",
191 __func__,
192 pix->width, pix->height, pix->pixelformat, pix->field,
193 pix->bytesperline, pix->sizeimage, pix->colorspace);
194
195 if (format->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
196 return -EINVAL;
197
198 if (pix->field != V4L2_FIELD_NONE)
199 return -EINVAL;
200
201 if (pix->pixelformat != V4L2_PIX_FMT_UYVY)
202 return -EINVAL;
203
204 return 0;
205}
206
207static int timblogiw_s_fmt(struct file *file, void *priv,
208 struct v4l2_format *format)
209{
210 struct video_device *vdev = video_devdata(file);
211 struct timblogiw *lw = video_get_drvdata(vdev);
212 struct timblogiw_fh *fh = priv;
213 struct v4l2_pix_format *pix = &format->fmt.pix;
214 int err;
215
216 mutex_lock(&lw->lock);
217
218 err = timblogiw_try_fmt(file, priv, format);
219 if (err)
220 goto out;
221
222 if (videobuf_queue_is_busy(&fh->vb_vidq)) {
223 dev_err(&vdev->dev, "%s queue busy\n", __func__);
224 err = -EBUSY;
225 goto out;
226 }
227
228 pix->width = fh->cur_norm->width;
229 pix->height = fh->cur_norm->height;
230
231out:
232 mutex_unlock(&lw->lock);
233 return err;
234}
235
236static int timblogiw_querycap(struct file *file, void *priv,
237 struct v4l2_capability *cap)
238{
239 struct video_device *vdev = video_devdata(file);
240
241 dev_dbg(&vdev->dev, "%s: Entry\n", __func__);
242 strncpy(cap->card, TIMBLOGIWIN_NAME, sizeof(cap->card)-1);
243 strncpy(cap->driver, DRIVER_NAME, sizeof(cap->driver) - 1);
244 snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s", vdev->name);
245 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING |
246 V4L2_CAP_READWRITE;
247 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
248
249 return 0;
250}
251
252static int timblogiw_enum_fmt(struct file *file, void *priv,
253 struct v4l2_fmtdesc *fmt)
254{
255 struct video_device *vdev = video_devdata(file);
256
257 dev_dbg(&vdev->dev, "%s, index: %d\n", __func__, fmt->index);
258
259 if (fmt->index != 0)
260 return -EINVAL;
261 memset(fmt, 0, sizeof(*fmt));
262 fmt->index = 0;
263 fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
264 strncpy(fmt->description, "4:2:2, packed, YUYV",
265 sizeof(fmt->description)-1);
266 fmt->pixelformat = V4L2_PIX_FMT_UYVY;
267
268 return 0;
269}
270
271static int timblogiw_g_parm(struct file *file, void *priv,
272 struct v4l2_streamparm *sp)
273{
274 struct timblogiw_fh *fh = priv;
275 struct v4l2_captureparm *cp = &sp->parm.capture;
276
277 cp->capability = V4L2_CAP_TIMEPERFRAME;
278 cp->timeperframe.numerator = 1;
279 cp->timeperframe.denominator = fh->cur_norm->fps;
280
281 return 0;
282}
283
284static int timblogiw_reqbufs(struct file *file, void *priv,
285 struct v4l2_requestbuffers *rb)
286{
287 struct video_device *vdev = video_devdata(file);
288 struct timblogiw_fh *fh = priv;
289
290 dev_dbg(&vdev->dev, "%s: entry\n", __func__);
291
292 return videobuf_reqbufs(&fh->vb_vidq, rb);
293}
294
295static int timblogiw_querybuf(struct file *file, void *priv,
296 struct v4l2_buffer *b)
297{
298 struct video_device *vdev = video_devdata(file);
299 struct timblogiw_fh *fh = priv;
300
301 dev_dbg(&vdev->dev, "%s: entry\n", __func__);
302
303 return videobuf_querybuf(&fh->vb_vidq, b);
304}
305
306static int timblogiw_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
307{
308 struct video_device *vdev = video_devdata(file);
309 struct timblogiw_fh *fh = priv;
310
311 dev_dbg(&vdev->dev, "%s: entry\n", __func__);
312
313 return videobuf_qbuf(&fh->vb_vidq, b);
314}
315
316static int timblogiw_dqbuf(struct file *file, void *priv,
317 struct v4l2_buffer *b)
318{
319 struct video_device *vdev = video_devdata(file);
320 struct timblogiw_fh *fh = priv;
321
322 dev_dbg(&vdev->dev, "%s: entry\n", __func__);
323
324 return videobuf_dqbuf(&fh->vb_vidq, b, file->f_flags & O_NONBLOCK);
325}
326
327static int timblogiw_g_std(struct file *file, void *priv, v4l2_std_id *std)
328{
329 struct video_device *vdev = video_devdata(file);
330 struct timblogiw_fh *fh = priv;
331
332 dev_dbg(&vdev->dev, "%s: entry\n", __func__);
333
334 *std = fh->cur_norm->std;
335 return 0;
336}
337
338static int timblogiw_s_std(struct file *file, void *priv, v4l2_std_id std)
339{
340 struct video_device *vdev = video_devdata(file);
341 struct timblogiw *lw = video_get_drvdata(vdev);
342 struct timblogiw_fh *fh = priv;
343 int err = 0;
344
345 dev_dbg(&vdev->dev, "%s: entry\n", __func__);
346
347 mutex_lock(&lw->lock);
348
349 if (TIMBLOGIW_HAS_DECODER(lw))
350 err = v4l2_subdev_call(lw->sd_enc, video, s_std, std);
351
352 if (!err)
353 fh->cur_norm = timblogiw_get_norm(std);
354
355 mutex_unlock(&lw->lock);
356
357 return err;
358}
359
360static int timblogiw_enuminput(struct file *file, void *priv,
361 struct v4l2_input *inp)
362{
363 struct video_device *vdev = video_devdata(file);
364 int i;
365
366 dev_dbg(&vdev->dev, "%s: Entry\n", __func__);
367
368 if (inp->index != 0)
369 return -EINVAL;
370
371 inp->index = 0;
372
373 strncpy(inp->name, "Timb input 1", sizeof(inp->name) - 1);
374 inp->type = V4L2_INPUT_TYPE_CAMERA;
375
376 inp->std = 0;
377 for (i = 0; i < ARRAY_SIZE(timblogiw_tvnorms); i++)
378 inp->std |= timblogiw_tvnorms[i].std;
379
380 return 0;
381}
382
383static int timblogiw_g_input(struct file *file, void *priv,
384 unsigned int *input)
385{
386 struct video_device *vdev = video_devdata(file);
387
388 dev_dbg(&vdev->dev, "%s: Entry\n", __func__);
389
390 *input = 0;
391
392 return 0;
393}
394
395static int timblogiw_s_input(struct file *file, void *priv, unsigned int input)
396{
397 struct video_device *vdev = video_devdata(file);
398
399 dev_dbg(&vdev->dev, "%s: Entry\n", __func__);
400
401 if (input != 0)
402 return -EINVAL;
403 return 0;
404}
405
406static int timblogiw_streamon(struct file *file, void *priv, enum v4l2_buf_type type)
407{
408 struct video_device *vdev = video_devdata(file);
409 struct timblogiw_fh *fh = priv;
410
411 dev_dbg(&vdev->dev, "%s: entry\n", __func__);
412
413 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
414 dev_dbg(&vdev->dev, "%s - No capture device\n", __func__);
415 return -EINVAL;
416 }
417
418 fh->frame_count = 0;
419 return videobuf_streamon(&fh->vb_vidq);
420}
421
422static int timblogiw_streamoff(struct file *file, void *priv,
423 enum v4l2_buf_type type)
424{
425 struct video_device *vdev = video_devdata(file);
426 struct timblogiw_fh *fh = priv;
427
428 dev_dbg(&vdev->dev, "%s entry\n", __func__);
429
430 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
431 return -EINVAL;
432
433 return videobuf_streamoff(&fh->vb_vidq);
434}
435
436static int timblogiw_querystd(struct file *file, void *priv, v4l2_std_id *std)
437{
438 struct video_device *vdev = video_devdata(file);
439 struct timblogiw *lw = video_get_drvdata(vdev);
440 struct timblogiw_fh *fh = priv;
441
442 dev_dbg(&vdev->dev, "%s entry\n", __func__);
443
444 if (TIMBLOGIW_HAS_DECODER(lw))
445 return v4l2_subdev_call(lw->sd_enc, video, querystd, std);
446 else {
447 *std = fh->cur_norm->std;
448 return 0;
449 }
450}
451
452static int timblogiw_enum_framesizes(struct file *file, void *priv,
453 struct v4l2_frmsizeenum *fsize)
454{
455 struct video_device *vdev = video_devdata(file);
456 struct timblogiw_fh *fh = priv;
457
458 dev_dbg(&vdev->dev, "%s - index: %d, format: %d\n", __func__,
459 fsize->index, fsize->pixel_format);
460
461 if ((fsize->index != 0) ||
462 (fsize->pixel_format != V4L2_PIX_FMT_UYVY))
463 return -EINVAL;
464
465 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
466 fsize->discrete.width = fh->cur_norm->width;
467 fsize->discrete.height = fh->cur_norm->height;
468
469 return 0;
470}
471
472/* Video buffer functions */
473
474static int buffer_setup(struct videobuf_queue *vq, unsigned int *count,
475 unsigned int *size)
476{
477 struct timblogiw_fh *fh = vq->priv_data;
478
479 *size = timblogiw_frame_size(fh->cur_norm);
480
481 if (!*count)
482 *count = 32;
483
484 while (*size * *count > TIMBLOGIW_MAX_VIDEO_MEM * 1024 * 1024)
485 (*count)--;
486
487 return 0;
488}
489
490static int buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
491 enum v4l2_field field)
492{
493 struct timblogiw_fh *fh = vq->priv_data;
494 struct timblogiw_buffer *buf = container_of(vb, struct timblogiw_buffer,
495 vb);
496 unsigned int data_size = timblogiw_frame_size(fh->cur_norm);
497 int err = 0;
498
499 if (vb->baddr && vb->bsize < data_size)
500 /* User provided buffer, but it is too small */
501 return -ENOMEM;
502
503 vb->size = data_size;
504 vb->width = fh->cur_norm->width;
505 vb->height = fh->cur_norm->height;
506 vb->field = field;
507
508 if (vb->state == VIDEOBUF_NEEDS_INIT) {
509 int i;
510 unsigned int size;
511 unsigned int bytes_per_desc = TIMBLOGIW_LINES_PER_DESC *
512 timblogiw_bytes_per_line(fh->cur_norm);
513 dma_addr_t addr;
514
515 sg_init_table(buf->sg, ARRAY_SIZE(buf->sg));
516
517 err = videobuf_iolock(vq, vb, NULL);
518 if (err)
519 goto err;
520
521 addr = videobuf_to_dma_contig(vb);
522 for (i = 0, size = 0; size < data_size; i++) {
523 sg_dma_address(buf->sg + i) = addr + size;
524 size += bytes_per_desc;
525 sg_dma_len(buf->sg + i) = (size > data_size) ?
526 (bytes_per_desc - (size - data_size)) :
527 bytes_per_desc;
528 }
529
530 vb->state = VIDEOBUF_PREPARED;
531 buf->cookie = -1;
532 buf->fh = fh;
533 }
534
535 return 0;
536
537err:
538 videobuf_dma_contig_free(vq, vb);
539 vb->state = VIDEOBUF_NEEDS_INIT;
540 return err;
541}
542
543static void buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
544{
545 struct timblogiw_fh *fh = vq->priv_data;
546 struct timblogiw_buffer *buf = container_of(vb, struct timblogiw_buffer,
547 vb);
548 struct dma_async_tx_descriptor *desc;
549 int sg_elems;
550 int bytes_per_desc = TIMBLOGIW_LINES_PER_DESC *
551 timblogiw_bytes_per_line(fh->cur_norm);
552
553 sg_elems = timblogiw_frame_size(fh->cur_norm) / bytes_per_desc;
554 sg_elems +=
555 (timblogiw_frame_size(fh->cur_norm) % bytes_per_desc) ? 1 : 0;
556
557 if (list_empty(&fh->capture))
558 vb->state = VIDEOBUF_ACTIVE;
559 else
560 vb->state = VIDEOBUF_QUEUED;
561
562 list_add_tail(&vb->queue, &fh->capture);
563
564 spin_unlock_irq(&fh->queue_lock);
565
566 desc = dmaengine_prep_slave_sg(fh->chan,
567 buf->sg, sg_elems, DMA_DEV_TO_MEM,
568 DMA_PREP_INTERRUPT);
569 if (!desc) {
570 spin_lock_irq(&fh->queue_lock);
571 list_del_init(&vb->queue);
572 vb->state = VIDEOBUF_PREPARED;
573 return;
574 }
575
576 desc->callback_param = buf;
577 desc->callback = timblogiw_dma_cb;
578
579 buf->cookie = desc->tx_submit(desc);
580
581 spin_lock_irq(&fh->queue_lock);
582}
583
584static void buffer_release(struct videobuf_queue *vq,
585 struct videobuf_buffer *vb)
586{
587 struct timblogiw_fh *fh = vq->priv_data;
588 struct timblogiw_buffer *buf = container_of(vb, struct timblogiw_buffer,
589 vb);
590
591 videobuf_waiton(vq, vb, 0, 0);
592 if (buf->cookie >= 0)
593 dma_sync_wait(fh->chan, buf->cookie);
594
595 videobuf_dma_contig_free(vq, vb);
596 vb->state = VIDEOBUF_NEEDS_INIT;
597}
598
599static struct videobuf_queue_ops timblogiw_video_qops = {
600 .buf_setup = buffer_setup,
601 .buf_prepare = buffer_prepare,
602 .buf_queue = buffer_queue,
603 .buf_release = buffer_release,
604};
605
606/* Device Operations functions */
607
608static int timblogiw_open(struct file *file)
609{
610 struct video_device *vdev = video_devdata(file);
611 struct timblogiw *lw = video_get_drvdata(vdev);
612 struct timblogiw_fh *fh;
613 v4l2_std_id std;
614 dma_cap_mask_t mask;
615 int err = 0;
616
617 dev_dbg(&vdev->dev, "%s: entry\n", __func__);
618
619 mutex_lock(&lw->lock);
620 if (lw->opened) {
621 err = -EBUSY;
622 goto out;
623 }
624
625 if (TIMBLOGIW_HAS_DECODER(lw) && !lw->sd_enc) {
626 struct i2c_adapter *adapt;
627
628 /* find the video decoder */
629 adapt = i2c_get_adapter(lw->pdata.i2c_adapter);
630 if (!adapt) {
631 dev_err(&vdev->dev, "No I2C bus #%d\n",
632 lw->pdata.i2c_adapter);
633 err = -ENODEV;
634 goto out;
635 }
636
637 /* now find the encoder */
638 lw->sd_enc = v4l2_i2c_new_subdev_board(&lw->v4l2_dev, adapt,
639 lw->pdata.encoder.info, NULL);
640
641 i2c_put_adapter(adapt);
642
643 if (!lw->sd_enc) {
644 dev_err(&vdev->dev, "Failed to get encoder: %s\n",
645 lw->pdata.encoder.module_name);
646 err = -ENODEV;
647 goto out;
648 }
649 }
650
651 fh = kzalloc(sizeof(*fh), GFP_KERNEL);
652 if (!fh) {
653 err = -ENOMEM;
654 goto out;
655 }
656
657 fh->cur_norm = timblogiw_tvnorms;
658 timblogiw_querystd(file, fh, &std);
659 fh->cur_norm = timblogiw_get_norm(std);
660
661 INIT_LIST_HEAD(&fh->capture);
662 spin_lock_init(&fh->queue_lock);
663
664 dma_cap_zero(mask);
665 dma_cap_set(DMA_SLAVE, mask);
666 dma_cap_set(DMA_PRIVATE, mask);
667
668 /* find the DMA channel */
669 fh->chan = dma_request_channel(mask, timblogiw_dma_filter_fn,
670 (void *)(uintptr_t)lw->pdata.dma_channel);
671 if (!fh->chan) {
672 dev_err(&vdev->dev, "Failed to get DMA channel\n");
673 kfree(fh);
674 err = -ENODEV;
675 goto out;
676 }
677
678 file->private_data = fh;
679 videobuf_queue_dma_contig_init(&fh->vb_vidq,
680 &timblogiw_video_qops, lw->dev, &fh->queue_lock,
681 V4L2_BUF_TYPE_VIDEO_CAPTURE, V4L2_FIELD_NONE,
682 sizeof(struct timblogiw_buffer), fh, NULL);
683
684 lw->opened = true;
685out:
686 mutex_unlock(&lw->lock);
687
688 return err;
689}
690
691static int timblogiw_close(struct file *file)
692{
693 struct video_device *vdev = video_devdata(file);
694 struct timblogiw *lw = video_get_drvdata(vdev);
695 struct timblogiw_fh *fh = file->private_data;
696
697 dev_dbg(&vdev->dev, "%s: Entry\n", __func__);
698
699 videobuf_stop(&fh->vb_vidq);
700 videobuf_mmap_free(&fh->vb_vidq);
701
702 dma_release_channel(fh->chan);
703
704 kfree(fh);
705
706 mutex_lock(&lw->lock);
707 lw->opened = false;
708 mutex_unlock(&lw->lock);
709 return 0;
710}
711
712static ssize_t timblogiw_read(struct file *file, char __user *data,
713 size_t count, loff_t *ppos)
714{
715 struct video_device *vdev = video_devdata(file);
716 struct timblogiw_fh *fh = file->private_data;
717
718 dev_dbg(&vdev->dev, "%s: entry\n", __func__);
719
720 return videobuf_read_stream(&fh->vb_vidq, data, count, ppos, 0,
721 file->f_flags & O_NONBLOCK);
722}
723
724static unsigned int timblogiw_poll(struct file *file,
725 struct poll_table_struct *wait)
726{
727 struct video_device *vdev = video_devdata(file);
728 struct timblogiw_fh *fh = file->private_data;
729
730 dev_dbg(&vdev->dev, "%s: entry\n", __func__);
731
732 return videobuf_poll_stream(file, &fh->vb_vidq, wait);
733}
734
735static int timblogiw_mmap(struct file *file, struct vm_area_struct *vma)
736{
737 struct video_device *vdev = video_devdata(file);
738 struct timblogiw_fh *fh = file->private_data;
739
740 dev_dbg(&vdev->dev, "%s: entry\n", __func__);
741
742 return videobuf_mmap_mapper(&fh->vb_vidq, vma);
743}
744
745/* Platform device functions */
746
747static struct v4l2_ioctl_ops timblogiw_ioctl_ops = {
748 .vidioc_querycap = timblogiw_querycap,
749 .vidioc_enum_fmt_vid_cap = timblogiw_enum_fmt,
750 .vidioc_g_fmt_vid_cap = timblogiw_g_fmt,
751 .vidioc_try_fmt_vid_cap = timblogiw_try_fmt,
752 .vidioc_s_fmt_vid_cap = timblogiw_s_fmt,
753 .vidioc_g_parm = timblogiw_g_parm,
754 .vidioc_reqbufs = timblogiw_reqbufs,
755 .vidioc_querybuf = timblogiw_querybuf,
756 .vidioc_qbuf = timblogiw_qbuf,
757 .vidioc_dqbuf = timblogiw_dqbuf,
758 .vidioc_g_std = timblogiw_g_std,
759 .vidioc_s_std = timblogiw_s_std,
760 .vidioc_enum_input = timblogiw_enuminput,
761 .vidioc_g_input = timblogiw_g_input,
762 .vidioc_s_input = timblogiw_s_input,
763 .vidioc_streamon = timblogiw_streamon,
764 .vidioc_streamoff = timblogiw_streamoff,
765 .vidioc_querystd = timblogiw_querystd,
766 .vidioc_enum_framesizes = timblogiw_enum_framesizes,
767};
768
769static struct v4l2_file_operations timblogiw_fops = {
770 .owner = THIS_MODULE,
771 .open = timblogiw_open,
772 .release = timblogiw_close,
773 .unlocked_ioctl = video_ioctl2, /* V4L2 ioctl handler */
774 .mmap = timblogiw_mmap,
775 .read = timblogiw_read,
776 .poll = timblogiw_poll,
777};
778
779static struct video_device timblogiw_template = {
780 .name = TIMBLOGIWIN_NAME,
781 .fops = &timblogiw_fops,
782 .ioctl_ops = &timblogiw_ioctl_ops,
783 .release = video_device_release_empty,
784 .minor = -1,
785 .tvnorms = V4L2_STD_PAL | V4L2_STD_NTSC
786};
787
788static int timblogiw_probe(struct platform_device *pdev)
789{
790 int err;
791 struct timblogiw *lw = NULL;
792 struct timb_video_platform_data *pdata = pdev->dev.platform_data;
793
794 if (!pdata) {
795 dev_err(&pdev->dev, "No platform data\n");
796 err = -EINVAL;
797 goto err;
798 }
799
800 if (!pdata->encoder.module_name)
801 dev_info(&pdev->dev, "Running without decoder\n");
802
803 lw = devm_kzalloc(&pdev->dev, sizeof(*lw), GFP_KERNEL);
804 if (!lw) {
805 err = -ENOMEM;
806 goto err;
807 }
808
809 if (pdev->dev.parent)
810 lw->dev = pdev->dev.parent;
811 else
812 lw->dev = &pdev->dev;
813
814 memcpy(&lw->pdata, pdata, sizeof(lw->pdata));
815
816 mutex_init(&lw->lock);
817
818 lw->video_dev = timblogiw_template;
819
820 strlcpy(lw->v4l2_dev.name, DRIVER_NAME, sizeof(lw->v4l2_dev.name));
821 err = v4l2_device_register(NULL, &lw->v4l2_dev);
822 if (err)
823 goto err;
824
825 lw->video_dev.v4l2_dev = &lw->v4l2_dev;
826
827 platform_set_drvdata(pdev, lw);
828 video_set_drvdata(&lw->video_dev, lw);
829
830 err = video_register_device(&lw->video_dev, VFL_TYPE_GRABBER, 0);
831 if (err) {
832 dev_err(&pdev->dev, "Error reg video: %d\n", err);
833 goto err_request;
834 }
835
836 return 0;
837
838err_request:
839 v4l2_device_unregister(&lw->v4l2_dev);
840err:
841 dev_err(&pdev->dev, "Failed to register: %d\n", err);
842
843 return err;
844}
845
846static int timblogiw_remove(struct platform_device *pdev)
847{
848 struct timblogiw *lw = platform_get_drvdata(pdev);
849
850 video_unregister_device(&lw->video_dev);
851
852 v4l2_device_unregister(&lw->v4l2_dev);
853
854 return 0;
855}
856
857static struct platform_driver timblogiw_platform_driver = {
858 .driver = {
859 .name = DRIVER_NAME,
860 },
861 .probe = timblogiw_probe,
862 .remove = timblogiw_remove,
863};
864
865module_platform_driver(timblogiw_platform_driver);
866
867MODULE_DESCRIPTION(TIMBLOGIWIN_NAME);
868MODULE_AUTHOR("Pelagicore AB <info@pelagicore.com>");
869MODULE_LICENSE("GPL v2");
870MODULE_ALIAS("platform:"DRIVER_NAME);
diff --git a/drivers/staging/media/tw686x-kh/tw686x-kh-video.c b/drivers/staging/media/tw686x-kh/tw686x-kh-video.c
index 6ecb504a79f9..9bf32aec2fc6 100644
--- a/drivers/staging/media/tw686x-kh/tw686x-kh-video.c
+++ b/drivers/staging/media/tw686x-kh/tw686x-kh-video.c
@@ -130,12 +130,11 @@ static void tw686x_get_format(struct tw686x_video_channel *vc,
130 130
131static int tw686x_queue_setup(struct vb2_queue *vq, unsigned int *nbuffers, 131static int tw686x_queue_setup(struct vb2_queue *vq, unsigned int *nbuffers,
132 unsigned int *nplanes, unsigned int sizes[], 132 unsigned int *nplanes, unsigned int sizes[],
133 void *alloc_ctxs[]) 133 struct device *alloc_devs[])
134{ 134{
135 struct tw686x_video_channel *vc = vb2_get_drv_priv(vq); 135 struct tw686x_video_channel *vc = vb2_get_drv_priv(vq);
136 unsigned int size = vc->width * vc->height * vc->format->depth / 8; 136 unsigned int size = vc->width * vc->height * vc->format->depth / 8;
137 137
138 alloc_ctxs[0] = vc->alloc_ctx;
139 if (*nbuffers < 2) 138 if (*nbuffers < 2)
140 *nbuffers = 2; 139 *nbuffers = 2;
141 140
@@ -645,7 +644,6 @@ void tw686x_kh_video_free(struct tw686x_dev *dev)
645 v4l2_ctrl_handler_free(&vc->ctrl_handler); 644 v4l2_ctrl_handler_free(&vc->ctrl_handler);
646 if (vc->device) 645 if (vc->device)
647 video_unregister_device(vc->device); 646 video_unregister_device(vc->device);
648 vb2_dma_sg_cleanup_ctx(vc->alloc_ctx);
649 for (n = 0; n < 2; n++) { 647 for (n = 0; n < 2; n++) {
650 struct dma_desc *descs = &vc->sg_tables[n]; 648 struct dma_desc *descs = &vc->sg_tables[n];
651 649
@@ -750,13 +748,6 @@ int tw686x_kh_video_init(struct tw686x_dev *dev)
750 goto error; 748 goto error;
751 } 749 }
752 750
753 vc->alloc_ctx = vb2_dma_sg_init_ctx(&dev->pci_dev->dev);
754 if (IS_ERR(vc->alloc_ctx)) {
755 pr_warn("Unable to initialize DMA scatter-gather context\n");
756 err = PTR_ERR(vc->alloc_ctx);
757 goto error;
758 }
759
760 vc->vidq.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; 751 vc->vidq.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
761 vc->vidq.io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF; 752 vc->vidq.io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
762 vc->vidq.drv_priv = vc; 753 vc->vidq.drv_priv = vc;
@@ -766,6 +757,7 @@ int tw686x_kh_video_init(struct tw686x_dev *dev)
766 vc->vidq.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC; 757 vc->vidq.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
767 vc->vidq.min_buffers_needed = 2; 758 vc->vidq.min_buffers_needed = 2;
768 vc->vidq.lock = &vc->vb_mutex; 759 vc->vidq.lock = &vc->vb_mutex;
760 vc->vidq.dev = &dev->pci_dev->dev;
769 vc->vidq.gfp_flags = GFP_DMA32; 761 vc->vidq.gfp_flags = GFP_DMA32;
770 762
771 err = vb2_queue_init(&vc->vidq); 763 err = vb2_queue_init(&vc->vidq);
diff --git a/drivers/staging/media/tw686x-kh/tw686x-kh.h b/drivers/staging/media/tw686x-kh/tw686x-kh.h
index dc257967dbc7..6284a90d6fe3 100644
--- a/drivers/staging/media/tw686x-kh/tw686x-kh.h
+++ b/drivers/staging/media/tw686x-kh/tw686x-kh.h
@@ -56,7 +56,6 @@ struct tw686x_video_channel {
56 struct video_device *device; 56 struct video_device *device;
57 struct dma_desc sg_tables[2]; 57 struct dma_desc sg_tables[2];
58 struct tw686x_vb2_buf *curr_bufs[2]; 58 struct tw686x_vb2_buf *curr_bufs[2];
59 void *alloc_ctx;
60 struct vdma_desc *sg_descs[2]; 59 struct vdma_desc *sg_descs[2];
61 60
62 struct v4l2_ctrl_handler ctrl_handler; 61 struct v4l2_ctrl_handler ctrl_handler;
diff --git a/drivers/usb/gadget/function/uvc_queue.c b/drivers/usb/gadget/function/uvc_queue.c
index 912694f3d54e..6377e9fee6e5 100644
--- a/drivers/usb/gadget/function/uvc_queue.c
+++ b/drivers/usb/gadget/function/uvc_queue.c
@@ -43,7 +43,7 @@
43 43
44static int uvc_queue_setup(struct vb2_queue *vq, 44static int uvc_queue_setup(struct vb2_queue *vq,
45 unsigned int *nbuffers, unsigned int *nplanes, 45 unsigned int *nbuffers, unsigned int *nplanes,
46 unsigned int sizes[], void *alloc_ctxs[]) 46 unsigned int sizes[], struct device *alloc_devs[])
47{ 47{
48 struct uvc_video_queue *queue = vb2_get_drv_priv(vq); 48 struct uvc_video_queue *queue = vb2_get_drv_priv(vq);
49 struct uvc_video *video = container_of(queue, struct uvc_video, queue); 49 struct uvc_video *video = container_of(queue, struct uvc_video, queue);
diff --git a/fs/compat_ioctl.c b/fs/compat_ioctl.c
index bd01b92aad98..c1e9f29c924c 100644
--- a/fs/compat_ioctl.c
+++ b/fs/compat_ioctl.c
@@ -57,6 +57,7 @@
57#include <linux/i2c-dev.h> 57#include <linux/i2c-dev.h>
58#include <linux/atalk.h> 58#include <linux/atalk.h>
59#include <linux/gfp.h> 59#include <linux/gfp.h>
60#include <linux/cec.h>
60 61
61#include "internal.h" 62#include "internal.h"
62 63
@@ -1377,6 +1378,17 @@ COMPATIBLE_IOCTL(VIDEO_GET_NAVI)
1377COMPATIBLE_IOCTL(VIDEO_SET_ATTRIBUTES) 1378COMPATIBLE_IOCTL(VIDEO_SET_ATTRIBUTES)
1378COMPATIBLE_IOCTL(VIDEO_GET_SIZE) 1379COMPATIBLE_IOCTL(VIDEO_GET_SIZE)
1379COMPATIBLE_IOCTL(VIDEO_GET_FRAME_RATE) 1380COMPATIBLE_IOCTL(VIDEO_GET_FRAME_RATE)
1381/* cec */
1382COMPATIBLE_IOCTL(CEC_ADAP_G_CAPS)
1383COMPATIBLE_IOCTL(CEC_ADAP_G_LOG_ADDRS)
1384COMPATIBLE_IOCTL(CEC_ADAP_S_LOG_ADDRS)
1385COMPATIBLE_IOCTL(CEC_ADAP_G_PHYS_ADDR)
1386COMPATIBLE_IOCTL(CEC_ADAP_S_PHYS_ADDR)
1387COMPATIBLE_IOCTL(CEC_G_MODE)
1388COMPATIBLE_IOCTL(CEC_S_MODE)
1389COMPATIBLE_IOCTL(CEC_TRANSMIT)
1390COMPATIBLE_IOCTL(CEC_RECEIVE)
1391COMPATIBLE_IOCTL(CEC_DQEVENT)
1380 1392
1381/* joystick */ 1393/* joystick */
1382COMPATIBLE_IOCTL(JSIOCGVERSION) 1394COMPATIBLE_IOCTL(JSIOCGVERSION)
diff --git a/include/linux/cec-funcs.h b/include/linux/cec-funcs.h
new file mode 100644
index 000000000000..82c3d3b7269d
--- /dev/null
+++ b/include/linux/cec-funcs.h
@@ -0,0 +1,1899 @@
1/*
2 * cec - HDMI Consumer Electronics Control message functions
3 *
4 * Copyright 2016 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
5 *
6 * This program is free software; you may redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * Alternatively you can redistribute this file under the terms of the
11 * BSD license as stated below:
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in
20 * the documentation and/or other materials provided with the
21 * distribution.
22 * 3. The names of its contributors may not be used to endorse or promote
23 * products derived from this software without specific prior written
24 * permission.
25 *
26 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
30 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
31 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
32 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
33 * SOFTWARE.
34 */
35
36/*
37 * Note: this framework is still in staging and it is likely the API
38 * will change before it goes out of staging.
39 *
40 * Once it is moved out of staging this header will move to uapi.
41 */
42#ifndef _CEC_UAPI_FUNCS_H
43#define _CEC_UAPI_FUNCS_H
44
45#include <linux/cec.h>
46
47/* One Touch Play Feature */
48static inline void cec_msg_active_source(struct cec_msg *msg, __u16 phys_addr)
49{
50 msg->len = 4;
51 msg->msg[0] |= 0xf; /* broadcast */
52 msg->msg[1] = CEC_MSG_ACTIVE_SOURCE;
53 msg->msg[2] = phys_addr >> 8;
54 msg->msg[3] = phys_addr & 0xff;
55}
56
57static inline void cec_ops_active_source(const struct cec_msg *msg,
58 __u16 *phys_addr)
59{
60 *phys_addr = (msg->msg[2] << 8) | msg->msg[3];
61}
62
63static inline void cec_msg_image_view_on(struct cec_msg *msg)
64{
65 msg->len = 2;
66 msg->msg[1] = CEC_MSG_IMAGE_VIEW_ON;
67}
68
69static inline void cec_msg_text_view_on(struct cec_msg *msg)
70{
71 msg->len = 2;
72 msg->msg[1] = CEC_MSG_TEXT_VIEW_ON;
73}
74
75
76/* Routing Control Feature */
77static inline void cec_msg_inactive_source(struct cec_msg *msg,
78 __u16 phys_addr)
79{
80 msg->len = 4;
81 msg->msg[1] = CEC_MSG_INACTIVE_SOURCE;
82 msg->msg[2] = phys_addr >> 8;
83 msg->msg[3] = phys_addr & 0xff;
84}
85
86static inline void cec_ops_inactive_source(const struct cec_msg *msg,
87 __u16 *phys_addr)
88{
89 *phys_addr = (msg->msg[2] << 8) | msg->msg[3];
90}
91
92static inline void cec_msg_request_active_source(struct cec_msg *msg,
93 bool reply)
94{
95 msg->len = 2;
96 msg->msg[0] |= 0xf; /* broadcast */
97 msg->msg[1] = CEC_MSG_REQUEST_ACTIVE_SOURCE;
98 msg->reply = reply ? CEC_MSG_ACTIVE_SOURCE : 0;
99}
100
101static inline void cec_msg_routing_information(struct cec_msg *msg,
102 __u16 phys_addr)
103{
104 msg->len = 4;
105 msg->msg[0] |= 0xf; /* broadcast */
106 msg->msg[1] = CEC_MSG_ROUTING_INFORMATION;
107 msg->msg[2] = phys_addr >> 8;
108 msg->msg[3] = phys_addr & 0xff;
109}
110
111static inline void cec_ops_routing_information(const struct cec_msg *msg,
112 __u16 *phys_addr)
113{
114 *phys_addr = (msg->msg[2] << 8) | msg->msg[3];
115}
116
117static inline void cec_msg_routing_change(struct cec_msg *msg,
118 bool reply,
119 __u16 orig_phys_addr,
120 __u16 new_phys_addr)
121{
122 msg->len = 6;
123 msg->msg[0] |= 0xf; /* broadcast */
124 msg->msg[1] = CEC_MSG_ROUTING_CHANGE;
125 msg->msg[2] = orig_phys_addr >> 8;
126 msg->msg[3] = orig_phys_addr & 0xff;
127 msg->msg[4] = new_phys_addr >> 8;
128 msg->msg[5] = new_phys_addr & 0xff;
129 msg->reply = reply ? CEC_MSG_ROUTING_INFORMATION : 0;
130}
131
132static inline void cec_ops_routing_change(const struct cec_msg *msg,
133 __u16 *orig_phys_addr,
134 __u16 *new_phys_addr)
135{
136 *orig_phys_addr = (msg->msg[2] << 8) | msg->msg[3];
137 *new_phys_addr = (msg->msg[4] << 8) | msg->msg[5];
138}
139
140static inline void cec_msg_set_stream_path(struct cec_msg *msg, __u16 phys_addr)
141{
142 msg->len = 4;
143 msg->msg[0] |= 0xf; /* broadcast */
144 msg->msg[1] = CEC_MSG_SET_STREAM_PATH;
145 msg->msg[2] = phys_addr >> 8;
146 msg->msg[3] = phys_addr & 0xff;
147}
148
149static inline void cec_ops_set_stream_path(const struct cec_msg *msg,
150 __u16 *phys_addr)
151{
152 *phys_addr = (msg->msg[2] << 8) | msg->msg[3];
153}
154
155
156/* Standby Feature */
157static inline void cec_msg_standby(struct cec_msg *msg)
158{
159 msg->len = 2;
160 msg->msg[1] = CEC_MSG_STANDBY;
161}
162
163
164/* One Touch Record Feature */
165static inline void cec_msg_record_off(struct cec_msg *msg)
166{
167 msg->len = 2;
168 msg->msg[1] = CEC_MSG_RECORD_OFF;
169}
170
171struct cec_op_arib_data {
172 __u16 transport_id;
173 __u16 service_id;
174 __u16 orig_network_id;
175};
176
177struct cec_op_atsc_data {
178 __u16 transport_id;
179 __u16 program_number;
180};
181
182struct cec_op_dvb_data {
183 __u16 transport_id;
184 __u16 service_id;
185 __u16 orig_network_id;
186};
187
188struct cec_op_channel_data {
189 __u8 channel_number_fmt;
190 __u16 major;
191 __u16 minor;
192};
193
194struct cec_op_digital_service_id {
195 __u8 service_id_method;
196 __u8 dig_bcast_system;
197 union {
198 struct cec_op_arib_data arib;
199 struct cec_op_atsc_data atsc;
200 struct cec_op_dvb_data dvb;
201 struct cec_op_channel_data channel;
202 };
203};
204
205struct cec_op_record_src {
206 __u8 type;
207 union {
208 struct cec_op_digital_service_id digital;
209 struct {
210 __u8 ana_bcast_type;
211 __u16 ana_freq;
212 __u8 bcast_system;
213 } analog;
214 struct {
215 __u8 plug;
216 } ext_plug;
217 struct {
218 __u16 phys_addr;
219 } ext_phys_addr;
220 };
221};
222
223static inline void cec_set_digital_service_id(__u8 *msg,
224 const struct cec_op_digital_service_id *digital)
225{
226 *msg++ = (digital->service_id_method << 7) | digital->dig_bcast_system;
227 if (digital->service_id_method == CEC_OP_SERVICE_ID_METHOD_BY_CHANNEL) {
228 *msg++ = (digital->channel.channel_number_fmt << 2) |
229 (digital->channel.major >> 8);
230 *msg++ = digital->channel.major && 0xff;
231 *msg++ = digital->channel.minor >> 8;
232 *msg++ = digital->channel.minor & 0xff;
233 *msg++ = 0;
234 *msg++ = 0;
235 return;
236 }
237 switch (digital->dig_bcast_system) {
238 case CEC_OP_DIG_SERVICE_BCAST_SYSTEM_ATSC_GEN:
239 case CEC_OP_DIG_SERVICE_BCAST_SYSTEM_ATSC_CABLE:
240 case CEC_OP_DIG_SERVICE_BCAST_SYSTEM_ATSC_SAT:
241 case CEC_OP_DIG_SERVICE_BCAST_SYSTEM_ATSC_T:
242 *msg++ = digital->atsc.transport_id >> 8;
243 *msg++ = digital->atsc.transport_id & 0xff;
244 *msg++ = digital->atsc.program_number >> 8;
245 *msg++ = digital->atsc.program_number & 0xff;
246 *msg++ = 0;
247 *msg++ = 0;
248 break;
249 default:
250 *msg++ = digital->dvb.transport_id >> 8;
251 *msg++ = digital->dvb.transport_id & 0xff;
252 *msg++ = digital->dvb.service_id >> 8;
253 *msg++ = digital->dvb.service_id & 0xff;
254 *msg++ = digital->dvb.orig_network_id >> 8;
255 *msg++ = digital->dvb.orig_network_id & 0xff;
256 break;
257 }
258}
259
260static inline void cec_get_digital_service_id(const __u8 *msg,
261 struct cec_op_digital_service_id *digital)
262{
263 digital->service_id_method = msg[0] >> 7;
264 digital->dig_bcast_system = msg[0] & 0x7f;
265 if (digital->service_id_method == CEC_OP_SERVICE_ID_METHOD_BY_CHANNEL) {
266 digital->channel.channel_number_fmt = msg[1] >> 2;
267 digital->channel.major = ((msg[1] & 3) << 6) | msg[2];
268 digital->channel.minor = (msg[3] << 8) | msg[4];
269 return;
270 }
271 digital->dvb.transport_id = (msg[1] << 8) | msg[2];
272 digital->dvb.service_id = (msg[3] << 8) | msg[4];
273 digital->dvb.orig_network_id = (msg[5] << 8) | msg[6];
274}
275
276static inline void cec_msg_record_on_own(struct cec_msg *msg)
277{
278 msg->len = 3;
279 msg->msg[1] = CEC_MSG_RECORD_ON;
280 msg->msg[2] = CEC_OP_RECORD_SRC_OWN;
281}
282
283static inline void cec_msg_record_on_digital(struct cec_msg *msg,
284 const struct cec_op_digital_service_id *digital)
285{
286 msg->len = 10;
287 msg->msg[1] = CEC_MSG_RECORD_ON;
288 msg->msg[2] = CEC_OP_RECORD_SRC_DIGITAL;
289 cec_set_digital_service_id(msg->msg + 3, digital);
290}
291
292static inline void cec_msg_record_on_analog(struct cec_msg *msg,
293 __u8 ana_bcast_type,
294 __u16 ana_freq,
295 __u8 bcast_system)
296{
297 msg->len = 7;
298 msg->msg[1] = CEC_MSG_RECORD_ON;
299 msg->msg[2] = CEC_OP_RECORD_SRC_ANALOG;
300 msg->msg[3] = ana_bcast_type;
301 msg->msg[4] = ana_freq >> 8;
302 msg->msg[5] = ana_freq & 0xff;
303 msg->msg[6] = bcast_system;
304}
305
306static inline void cec_msg_record_on_plug(struct cec_msg *msg,
307 __u8 plug)
308{
309 msg->len = 4;
310 msg->msg[1] = CEC_MSG_RECORD_ON;
311 msg->msg[2] = CEC_OP_RECORD_SRC_EXT_PLUG;
312 msg->msg[3] = plug;
313}
314
315static inline void cec_msg_record_on_phys_addr(struct cec_msg *msg,
316 __u16 phys_addr)
317{
318 msg->len = 5;
319 msg->msg[1] = CEC_MSG_RECORD_ON;
320 msg->msg[2] = CEC_OP_RECORD_SRC_EXT_PHYS_ADDR;
321 msg->msg[3] = phys_addr >> 8;
322 msg->msg[4] = phys_addr & 0xff;
323}
324
325static inline void cec_msg_record_on(struct cec_msg *msg,
326 const struct cec_op_record_src *rec_src)
327{
328 switch (rec_src->type) {
329 case CEC_OP_RECORD_SRC_OWN:
330 cec_msg_record_on_own(msg);
331 break;
332 case CEC_OP_RECORD_SRC_DIGITAL:
333 cec_msg_record_on_digital(msg, &rec_src->digital);
334 break;
335 case CEC_OP_RECORD_SRC_ANALOG:
336 cec_msg_record_on_analog(msg,
337 rec_src->analog.ana_bcast_type,
338 rec_src->analog.ana_freq,
339 rec_src->analog.bcast_system);
340 break;
341 case CEC_OP_RECORD_SRC_EXT_PLUG:
342 cec_msg_record_on_plug(msg, rec_src->ext_plug.plug);
343 break;
344 case CEC_OP_RECORD_SRC_EXT_PHYS_ADDR:
345 cec_msg_record_on_phys_addr(msg,
346 rec_src->ext_phys_addr.phys_addr);
347 break;
348 }
349}
350
351static inline void cec_ops_record_on(const struct cec_msg *msg,
352 struct cec_op_record_src *rec_src)
353{
354 rec_src->type = msg->msg[2];
355 switch (rec_src->type) {
356 case CEC_OP_RECORD_SRC_OWN:
357 break;
358 case CEC_OP_RECORD_SRC_DIGITAL:
359 cec_get_digital_service_id(msg->msg + 3, &rec_src->digital);
360 break;
361 case CEC_OP_RECORD_SRC_ANALOG:
362 rec_src->analog.ana_bcast_type = msg->msg[3];
363 rec_src->analog.ana_freq =
364 (msg->msg[4] << 8) | msg->msg[5];
365 rec_src->analog.bcast_system = msg->msg[6];
366 break;
367 case CEC_OP_RECORD_SRC_EXT_PLUG:
368 rec_src->ext_plug.plug = msg->msg[3];
369 break;
370 case CEC_OP_RECORD_SRC_EXT_PHYS_ADDR:
371 rec_src->ext_phys_addr.phys_addr =
372 (msg->msg[3] << 8) | msg->msg[4];
373 break;
374 }
375}
376
377static inline void cec_msg_record_status(struct cec_msg *msg, __u8 rec_status)
378{
379 msg->len = 3;
380 msg->msg[1] = CEC_MSG_RECORD_STATUS;
381 msg->msg[2] = rec_status;
382}
383
384static inline void cec_ops_record_status(const struct cec_msg *msg,
385 __u8 *rec_status)
386{
387 *rec_status = msg->msg[2];
388}
389
390static inline void cec_msg_record_tv_screen(struct cec_msg *msg,
391 bool reply)
392{
393 msg->len = 2;
394 msg->msg[1] = CEC_MSG_RECORD_TV_SCREEN;
395 msg->reply = reply ? CEC_MSG_RECORD_ON : 0;
396}
397
398
399/* Timer Programming Feature */
400static inline void cec_msg_timer_status(struct cec_msg *msg,
401 __u8 timer_overlap_warning,
402 __u8 media_info,
403 __u8 prog_info,
404 __u8 prog_error,
405 __u8 duration_hr,
406 __u8 duration_min)
407{
408 msg->len = 3;
409 msg->msg[1] = CEC_MSG_TIMER_STATUS;
410 msg->msg[2] = (timer_overlap_warning << 7) |
411 (media_info << 5) |
412 (prog_info ? 0x10 : 0) |
413 (prog_info ? prog_info : prog_error);
414 if (prog_info == CEC_OP_PROG_INFO_NOT_ENOUGH_SPACE ||
415 prog_info == CEC_OP_PROG_INFO_MIGHT_NOT_BE_ENOUGH_SPACE ||
416 prog_error == CEC_OP_PROG_ERROR_DUPLICATE) {
417 msg->len += 2;
418 msg->msg[3] = ((duration_hr / 10) << 4) | (duration_hr % 10);
419 msg->msg[4] = ((duration_min / 10) << 4) | (duration_min % 10);
420 }
421}
422
423static inline void cec_ops_timer_status(const struct cec_msg *msg,
424 __u8 *timer_overlap_warning,
425 __u8 *media_info,
426 __u8 *prog_info,
427 __u8 *prog_error,
428 __u8 *duration_hr,
429 __u8 *duration_min)
430{
431 *timer_overlap_warning = msg->msg[2] >> 7;
432 *media_info = (msg->msg[2] >> 5) & 3;
433 if (msg->msg[2] & 0x10) {
434 *prog_info = msg->msg[2] & 0xf;
435 *prog_error = 0;
436 } else {
437 *prog_info = 0;
438 *prog_error = msg->msg[2] & 0xf;
439 }
440 if (*prog_info == CEC_OP_PROG_INFO_NOT_ENOUGH_SPACE ||
441 *prog_info == CEC_OP_PROG_INFO_MIGHT_NOT_BE_ENOUGH_SPACE ||
442 *prog_error == CEC_OP_PROG_ERROR_DUPLICATE) {
443 *duration_hr = (msg->msg[3] >> 4) * 10 + (msg->msg[3] & 0xf);
444 *duration_min = (msg->msg[4] >> 4) * 10 + (msg->msg[4] & 0xf);
445 } else {
446 *duration_hr = *duration_min = 0;
447 }
448}
449
450static inline void cec_msg_timer_cleared_status(struct cec_msg *msg,
451 __u8 timer_cleared_status)
452{
453 msg->len = 3;
454 msg->msg[1] = CEC_MSG_TIMER_CLEARED_STATUS;
455 msg->msg[2] = timer_cleared_status;
456}
457
458static inline void cec_ops_timer_cleared_status(const struct cec_msg *msg,
459 __u8 *timer_cleared_status)
460{
461 *timer_cleared_status = msg->msg[2];
462}
463
464static inline void cec_msg_clear_analogue_timer(struct cec_msg *msg,
465 bool reply,
466 __u8 day,
467 __u8 month,
468 __u8 start_hr,
469 __u8 start_min,
470 __u8 duration_hr,
471 __u8 duration_min,
472 __u8 recording_seq,
473 __u8 ana_bcast_type,
474 __u16 ana_freq,
475 __u8 bcast_system)
476{
477 msg->len = 13;
478 msg->msg[1] = CEC_MSG_CLEAR_ANALOGUE_TIMER;
479 msg->msg[2] = day;
480 msg->msg[3] = month;
481 /* Hours and minutes are in BCD format */
482 msg->msg[4] = ((start_hr / 10) << 4) | (start_hr % 10);
483 msg->msg[5] = ((start_min / 10) << 4) | (start_min % 10);
484 msg->msg[6] = ((duration_hr / 10) << 4) | (duration_hr % 10);
485 msg->msg[7] = ((duration_min / 10) << 4) | (duration_min % 10);
486 msg->msg[8] = recording_seq;
487 msg->msg[9] = ana_bcast_type;
488 msg->msg[10] = ana_freq >> 8;
489 msg->msg[11] = ana_freq & 0xff;
490 msg->msg[12] = bcast_system;
491 msg->reply = reply ? CEC_MSG_TIMER_CLEARED_STATUS : 0;
492}
493
494static inline void cec_ops_clear_analogue_timer(const struct cec_msg *msg,
495 __u8 *day,
496 __u8 *month,
497 __u8 *start_hr,
498 __u8 *start_min,
499 __u8 *duration_hr,
500 __u8 *duration_min,
501 __u8 *recording_seq,
502 __u8 *ana_bcast_type,
503 __u16 *ana_freq,
504 __u8 *bcast_system)
505{
506 *day = msg->msg[2];
507 *month = msg->msg[3];
508 /* Hours and minutes are in BCD format */
509 *start_hr = (msg->msg[4] >> 4) * 10 + (msg->msg[4] & 0xf);
510 *start_min = (msg->msg[5] >> 4) * 10 + (msg->msg[5] & 0xf);
511 *duration_hr = (msg->msg[6] >> 4) * 10 + (msg->msg[6] & 0xf);
512 *duration_min = (msg->msg[7] >> 4) * 10 + (msg->msg[7] & 0xf);
513 *recording_seq = msg->msg[8];
514 *ana_bcast_type = msg->msg[9];
515 *ana_freq = (msg->msg[10] << 8) | msg->msg[11];
516 *bcast_system = msg->msg[12];
517}
518
519static inline void cec_msg_clear_digital_timer(struct cec_msg *msg,
520 bool reply,
521 __u8 day,
522 __u8 month,
523 __u8 start_hr,
524 __u8 start_min,
525 __u8 duration_hr,
526 __u8 duration_min,
527 __u8 recording_seq,
528 const struct cec_op_digital_service_id *digital)
529{
530 msg->len = 16;
531 msg->reply = reply ? CEC_MSG_TIMER_CLEARED_STATUS : 0;
532 msg->msg[1] = CEC_MSG_CLEAR_DIGITAL_TIMER;
533 msg->msg[2] = day;
534 msg->msg[3] = month;
535 /* Hours and minutes are in BCD format */
536 msg->msg[4] = ((start_hr / 10) << 4) | (start_hr % 10);
537 msg->msg[5] = ((start_min / 10) << 4) | (start_min % 10);
538 msg->msg[6] = ((duration_hr / 10) << 4) | (duration_hr % 10);
539 msg->msg[7] = ((duration_min / 10) << 4) | (duration_min % 10);
540 msg->msg[8] = recording_seq;
541 cec_set_digital_service_id(msg->msg + 9, digital);
542}
543
544static inline void cec_ops_clear_digital_timer(const struct cec_msg *msg,
545 __u8 *day,
546 __u8 *month,
547 __u8 *start_hr,
548 __u8 *start_min,
549 __u8 *duration_hr,
550 __u8 *duration_min,
551 __u8 *recording_seq,
552 struct cec_op_digital_service_id *digital)
553{
554 *day = msg->msg[2];
555 *month = msg->msg[3];
556 /* Hours and minutes are in BCD format */
557 *start_hr = (msg->msg[4] >> 4) * 10 + (msg->msg[4] & 0xf);
558 *start_min = (msg->msg[5] >> 4) * 10 + (msg->msg[5] & 0xf);
559 *duration_hr = (msg->msg[6] >> 4) * 10 + (msg->msg[6] & 0xf);
560 *duration_min = (msg->msg[7] >> 4) * 10 + (msg->msg[7] & 0xf);
561 *recording_seq = msg->msg[8];
562 cec_get_digital_service_id(msg->msg + 9, digital);
563}
564
565static inline void cec_msg_clear_ext_timer(struct cec_msg *msg,
566 bool reply,
567 __u8 day,
568 __u8 month,
569 __u8 start_hr,
570 __u8 start_min,
571 __u8 duration_hr,
572 __u8 duration_min,
573 __u8 recording_seq,
574 __u8 ext_src_spec,
575 __u8 plug,
576 __u16 phys_addr)
577{
578 msg->len = 13;
579 msg->msg[1] = CEC_MSG_CLEAR_EXT_TIMER;
580 msg->msg[2] = day;
581 msg->msg[3] = month;
582 /* Hours and minutes are in BCD format */
583 msg->msg[4] = ((start_hr / 10) << 4) | (start_hr % 10);
584 msg->msg[5] = ((start_min / 10) << 4) | (start_min % 10);
585 msg->msg[6] = ((duration_hr / 10) << 4) | (duration_hr % 10);
586 msg->msg[7] = ((duration_min / 10) << 4) | (duration_min % 10);
587 msg->msg[8] = recording_seq;
588 msg->msg[9] = ext_src_spec;
589 msg->msg[10] = plug;
590 msg->msg[11] = phys_addr >> 8;
591 msg->msg[12] = phys_addr & 0xff;
592 msg->reply = reply ? CEC_MSG_TIMER_CLEARED_STATUS : 0;
593}
594
595static inline void cec_ops_clear_ext_timer(const struct cec_msg *msg,
596 __u8 *day,
597 __u8 *month,
598 __u8 *start_hr,
599 __u8 *start_min,
600 __u8 *duration_hr,
601 __u8 *duration_min,
602 __u8 *recording_seq,
603 __u8 *ext_src_spec,
604 __u8 *plug,
605 __u16 *phys_addr)
606{
607 *day = msg->msg[2];
608 *month = msg->msg[3];
609 /* Hours and minutes are in BCD format */
610 *start_hr = (msg->msg[4] >> 4) * 10 + (msg->msg[4] & 0xf);
611 *start_min = (msg->msg[5] >> 4) * 10 + (msg->msg[5] & 0xf);
612 *duration_hr = (msg->msg[6] >> 4) * 10 + (msg->msg[6] & 0xf);
613 *duration_min = (msg->msg[7] >> 4) * 10 + (msg->msg[7] & 0xf);
614 *recording_seq = msg->msg[8];
615 *ext_src_spec = msg->msg[9];
616 *plug = msg->msg[10];
617 *phys_addr = (msg->msg[11] << 8) | msg->msg[12];
618}
619
620static inline void cec_msg_set_analogue_timer(struct cec_msg *msg,
621 bool reply,
622 __u8 day,
623 __u8 month,
624 __u8 start_hr,
625 __u8 start_min,
626 __u8 duration_hr,
627 __u8 duration_min,
628 __u8 recording_seq,
629 __u8 ana_bcast_type,
630 __u16 ana_freq,
631 __u8 bcast_system)
632{
633 msg->len = 13;
634 msg->msg[1] = CEC_MSG_SET_ANALOGUE_TIMER;
635 msg->msg[2] = day;
636 msg->msg[3] = month;
637 /* Hours and minutes are in BCD format */
638 msg->msg[4] = ((start_hr / 10) << 4) | (start_hr % 10);
639 msg->msg[5] = ((start_min / 10) << 4) | (start_min % 10);
640 msg->msg[6] = ((duration_hr / 10) << 4) | (duration_hr % 10);
641 msg->msg[7] = ((duration_min / 10) << 4) | (duration_min % 10);
642 msg->msg[8] = recording_seq;
643 msg->msg[9] = ana_bcast_type;
644 msg->msg[10] = ana_freq >> 8;
645 msg->msg[11] = ana_freq & 0xff;
646 msg->msg[12] = bcast_system;
647 msg->reply = reply ? CEC_MSG_TIMER_STATUS : 0;
648}
649
650static inline void cec_ops_set_analogue_timer(const struct cec_msg *msg,
651 __u8 *day,
652 __u8 *month,
653 __u8 *start_hr,
654 __u8 *start_min,
655 __u8 *duration_hr,
656 __u8 *duration_min,
657 __u8 *recording_seq,
658 __u8 *ana_bcast_type,
659 __u16 *ana_freq,
660 __u8 *bcast_system)
661{
662 *day = msg->msg[2];
663 *month = msg->msg[3];
664 /* Hours and minutes are in BCD format */
665 *start_hr = (msg->msg[4] >> 4) * 10 + (msg->msg[4] & 0xf);
666 *start_min = (msg->msg[5] >> 4) * 10 + (msg->msg[5] & 0xf);
667 *duration_hr = (msg->msg[6] >> 4) * 10 + (msg->msg[6] & 0xf);
668 *duration_min = (msg->msg[7] >> 4) * 10 + (msg->msg[7] & 0xf);
669 *recording_seq = msg->msg[8];
670 *ana_bcast_type = msg->msg[9];
671 *ana_freq = (msg->msg[10] << 8) | msg->msg[11];
672 *bcast_system = msg->msg[12];
673}
674
675static inline void cec_msg_set_digital_timer(struct cec_msg *msg,
676 bool reply,
677 __u8 day,
678 __u8 month,
679 __u8 start_hr,
680 __u8 start_min,
681 __u8 duration_hr,
682 __u8 duration_min,
683 __u8 recording_seq,
684 const struct cec_op_digital_service_id *digital)
685{
686 msg->len = 16;
687 msg->reply = reply ? CEC_MSG_TIMER_STATUS : 0;
688 msg->msg[1] = CEC_MSG_SET_DIGITAL_TIMER;
689 msg->msg[2] = day;
690 msg->msg[3] = month;
691 /* Hours and minutes are in BCD format */
692 msg->msg[4] = ((start_hr / 10) << 4) | (start_hr % 10);
693 msg->msg[5] = ((start_min / 10) << 4) | (start_min % 10);
694 msg->msg[6] = ((duration_hr / 10) << 4) | (duration_hr % 10);
695 msg->msg[7] = ((duration_min / 10) << 4) | (duration_min % 10);
696 msg->msg[8] = recording_seq;
697 cec_set_digital_service_id(msg->msg + 9, digital);
698}
699
700static inline void cec_ops_set_digital_timer(const struct cec_msg *msg,
701 __u8 *day,
702 __u8 *month,
703 __u8 *start_hr,
704 __u8 *start_min,
705 __u8 *duration_hr,
706 __u8 *duration_min,
707 __u8 *recording_seq,
708 struct cec_op_digital_service_id *digital)
709{
710 *day = msg->msg[2];
711 *month = msg->msg[3];
712 /* Hours and minutes are in BCD format */
713 *start_hr = (msg->msg[4] >> 4) * 10 + (msg->msg[4] & 0xf);
714 *start_min = (msg->msg[5] >> 4) * 10 + (msg->msg[5] & 0xf);
715 *duration_hr = (msg->msg[6] >> 4) * 10 + (msg->msg[6] & 0xf);
716 *duration_min = (msg->msg[7] >> 4) * 10 + (msg->msg[7] & 0xf);
717 *recording_seq = msg->msg[8];
718 cec_get_digital_service_id(msg->msg + 9, digital);
719}
720
721static inline void cec_msg_set_ext_timer(struct cec_msg *msg,
722 bool reply,
723 __u8 day,
724 __u8 month,
725 __u8 start_hr,
726 __u8 start_min,
727 __u8 duration_hr,
728 __u8 duration_min,
729 __u8 recording_seq,
730 __u8 ext_src_spec,
731 __u8 plug,
732 __u16 phys_addr)
733{
734 msg->len = 13;
735 msg->msg[1] = CEC_MSG_SET_EXT_TIMER;
736 msg->msg[2] = day;
737 msg->msg[3] = month;
738 /* Hours and minutes are in BCD format */
739 msg->msg[4] = ((start_hr / 10) << 4) | (start_hr % 10);
740 msg->msg[5] = ((start_min / 10) << 4) | (start_min % 10);
741 msg->msg[6] = ((duration_hr / 10) << 4) | (duration_hr % 10);
742 msg->msg[7] = ((duration_min / 10) << 4) | (duration_min % 10);
743 msg->msg[8] = recording_seq;
744 msg->msg[9] = ext_src_spec;
745 msg->msg[10] = plug;
746 msg->msg[11] = phys_addr >> 8;
747 msg->msg[12] = phys_addr & 0xff;
748 msg->reply = reply ? CEC_MSG_TIMER_STATUS : 0;
749}
750
751static inline void cec_ops_set_ext_timer(const struct cec_msg *msg,
752 __u8 *day,
753 __u8 *month,
754 __u8 *start_hr,
755 __u8 *start_min,
756 __u8 *duration_hr,
757 __u8 *duration_min,
758 __u8 *recording_seq,
759 __u8 *ext_src_spec,
760 __u8 *plug,
761 __u16 *phys_addr)
762{
763 *day = msg->msg[2];
764 *month = msg->msg[3];
765 /* Hours and minutes are in BCD format */
766 *start_hr = (msg->msg[4] >> 4) * 10 + (msg->msg[4] & 0xf);
767 *start_min = (msg->msg[5] >> 4) * 10 + (msg->msg[5] & 0xf);
768 *duration_hr = (msg->msg[6] >> 4) * 10 + (msg->msg[6] & 0xf);
769 *duration_min = (msg->msg[7] >> 4) * 10 + (msg->msg[7] & 0xf);
770 *recording_seq = msg->msg[8];
771 *ext_src_spec = msg->msg[9];
772 *plug = msg->msg[10];
773 *phys_addr = (msg->msg[11] << 8) | msg->msg[12];
774}
775
776static inline void cec_msg_set_timer_program_title(struct cec_msg *msg,
777 const char *prog_title)
778{
779 unsigned int len = strlen(prog_title);
780
781 if (len > 14)
782 len = 14;
783 msg->len = 2 + len;
784 msg->msg[1] = CEC_MSG_SET_TIMER_PROGRAM_TITLE;
785 memcpy(msg->msg + 2, prog_title, len);
786}
787
788static inline void cec_ops_set_timer_program_title(const struct cec_msg *msg,
789 char *prog_title)
790{
791 unsigned int len = msg->len > 2 ? msg->len - 2 : 0;
792
793 if (len > 14)
794 len = 14;
795 memcpy(prog_title, msg->msg + 2, len);
796 prog_title[len] = '\0';
797}
798
799/* System Information Feature */
800static inline void cec_msg_cec_version(struct cec_msg *msg, __u8 cec_version)
801{
802 msg->len = 3;
803 msg->msg[1] = CEC_MSG_CEC_VERSION;
804 msg->msg[2] = cec_version;
805}
806
807static inline void cec_ops_cec_version(const struct cec_msg *msg,
808 __u8 *cec_version)
809{
810 *cec_version = msg->msg[2];
811}
812
813static inline void cec_msg_get_cec_version(struct cec_msg *msg,
814 bool reply)
815{
816 msg->len = 2;
817 msg->msg[1] = CEC_MSG_GET_CEC_VERSION;
818 msg->reply = reply ? CEC_MSG_CEC_VERSION : 0;
819}
820
821static inline void cec_msg_report_physical_addr(struct cec_msg *msg,
822 __u16 phys_addr, __u8 prim_devtype)
823{
824 msg->len = 5;
825 msg->msg[0] |= 0xf; /* broadcast */
826 msg->msg[1] = CEC_MSG_REPORT_PHYSICAL_ADDR;
827 msg->msg[2] = phys_addr >> 8;
828 msg->msg[3] = phys_addr & 0xff;
829 msg->msg[4] = prim_devtype;
830}
831
832static inline void cec_ops_report_physical_addr(const struct cec_msg *msg,
833 __u16 *phys_addr, __u8 *prim_devtype)
834{
835 *phys_addr = (msg->msg[2] << 8) | msg->msg[3];
836 *prim_devtype = msg->msg[4];
837}
838
839static inline void cec_msg_give_physical_addr(struct cec_msg *msg,
840 bool reply)
841{
842 msg->len = 2;
843 msg->msg[1] = CEC_MSG_GIVE_PHYSICAL_ADDR;
844 msg->reply = reply ? CEC_MSG_REPORT_PHYSICAL_ADDR : 0;
845}
846
847static inline void cec_msg_set_menu_language(struct cec_msg *msg,
848 const char *language)
849{
850 msg->len = 5;
851 msg->msg[0] |= 0xf; /* broadcast */
852 msg->msg[1] = CEC_MSG_SET_MENU_LANGUAGE;
853 memcpy(msg->msg + 2, language, 3);
854}
855
856static inline void cec_ops_set_menu_language(const struct cec_msg *msg,
857 char *language)
858{
859 memcpy(language, msg->msg + 2, 3);
860 language[3] = '\0';
861}
862
863static inline void cec_msg_get_menu_language(struct cec_msg *msg,
864 bool reply)
865{
866 msg->len = 2;
867 msg->msg[1] = CEC_MSG_GET_MENU_LANGUAGE;
868 msg->reply = reply ? CEC_MSG_SET_MENU_LANGUAGE : 0;
869}
870
871/*
872 * Assumes a single RC Profile byte and a single Device Features byte,
873 * i.e. no extended features are supported by this helper function.
874 *
875 * As of CEC 2.0 no extended features are defined, should those be added
876 * in the future, then this function needs to be adapted or a new function
877 * should be added.
878 */
879static inline void cec_msg_report_features(struct cec_msg *msg,
880 __u8 cec_version, __u8 all_device_types,
881 __u8 rc_profile, __u8 dev_features)
882{
883 msg->len = 6;
884 msg->msg[0] |= 0xf; /* broadcast */
885 msg->msg[1] = CEC_MSG_REPORT_FEATURES;
886 msg->msg[2] = cec_version;
887 msg->msg[3] = all_device_types;
888 msg->msg[4] = rc_profile;
889 msg->msg[5] = dev_features;
890}
891
892static inline void cec_ops_report_features(const struct cec_msg *msg,
893 __u8 *cec_version, __u8 *all_device_types,
894 const __u8 **rc_profile, const __u8 **dev_features)
895{
896 const __u8 *p = &msg->msg[4];
897
898 *cec_version = msg->msg[2];
899 *all_device_types = msg->msg[3];
900 *rc_profile = p;
901 while (p < &msg->msg[14] && (*p & CEC_OP_FEAT_EXT))
902 p++;
903 if (!(*p & CEC_OP_FEAT_EXT)) {
904 *dev_features = p + 1;
905 while (p < &msg->msg[15] && (*p & CEC_OP_FEAT_EXT))
906 p++;
907 }
908 if (*p & CEC_OP_FEAT_EXT)
909 *rc_profile = *dev_features = NULL;
910}
911
912static inline void cec_msg_give_features(struct cec_msg *msg,
913 bool reply)
914{
915 msg->len = 2;
916 msg->msg[1] = CEC_MSG_GIVE_FEATURES;
917 msg->reply = reply ? CEC_MSG_REPORT_FEATURES : 0;
918}
919
920/* Deck Control Feature */
921static inline void cec_msg_deck_control(struct cec_msg *msg,
922 __u8 deck_control_mode)
923{
924 msg->len = 3;
925 msg->msg[1] = CEC_MSG_DECK_CONTROL;
926 msg->msg[2] = deck_control_mode;
927}
928
929static inline void cec_ops_deck_control(const struct cec_msg *msg,
930 __u8 *deck_control_mode)
931{
932 *deck_control_mode = msg->msg[2];
933}
934
935static inline void cec_msg_deck_status(struct cec_msg *msg,
936 __u8 deck_info)
937{
938 msg->len = 3;
939 msg->msg[1] = CEC_MSG_DECK_STATUS;
940 msg->msg[2] = deck_info;
941}
942
943static inline void cec_ops_deck_status(const struct cec_msg *msg,
944 __u8 *deck_info)
945{
946 *deck_info = msg->msg[2];
947}
948
949static inline void cec_msg_give_deck_status(struct cec_msg *msg,
950 bool reply,
951 __u8 status_req)
952{
953 msg->len = 3;
954 msg->msg[1] = CEC_MSG_GIVE_DECK_STATUS;
955 msg->msg[2] = status_req;
956 msg->reply = reply ? CEC_MSG_DECK_STATUS : 0;
957}
958
959static inline void cec_ops_give_deck_status(const struct cec_msg *msg,
960 __u8 *status_req)
961{
962 *status_req = msg->msg[2];
963}
964
965static inline void cec_msg_play(struct cec_msg *msg,
966 __u8 play_mode)
967{
968 msg->len = 3;
969 msg->msg[1] = CEC_MSG_PLAY;
970 msg->msg[2] = play_mode;
971}
972
973static inline void cec_ops_play(const struct cec_msg *msg,
974 __u8 *play_mode)
975{
976 *play_mode = msg->msg[2];
977}
978
979
980/* Tuner Control Feature */
981struct cec_op_tuner_device_info {
982 __u8 rec_flag;
983 __u8 tuner_display_info;
984 bool is_analog;
985 union {
986 struct cec_op_digital_service_id digital;
987 struct {
988 __u8 ana_bcast_type;
989 __u16 ana_freq;
990 __u8 bcast_system;
991 } analog;
992 };
993};
994
995static inline void cec_msg_tuner_device_status_analog(struct cec_msg *msg,
996 __u8 rec_flag,
997 __u8 tuner_display_info,
998 __u8 ana_bcast_type,
999 __u16 ana_freq,
1000 __u8 bcast_system)
1001{
1002 msg->len = 7;
1003 msg->msg[1] = CEC_MSG_TUNER_DEVICE_STATUS;
1004 msg->msg[2] = (rec_flag << 7) | tuner_display_info;
1005 msg->msg[3] = ana_bcast_type;
1006 msg->msg[4] = ana_freq >> 8;
1007 msg->msg[5] = ana_freq & 0xff;
1008 msg->msg[6] = bcast_system;
1009}
1010
1011static inline void cec_msg_tuner_device_status_digital(struct cec_msg *msg,
1012 __u8 rec_flag, __u8 tuner_display_info,
1013 const struct cec_op_digital_service_id *digital)
1014{
1015 msg->len = 10;
1016 msg->msg[1] = CEC_MSG_TUNER_DEVICE_STATUS;
1017 msg->msg[2] = (rec_flag << 7) | tuner_display_info;
1018 cec_set_digital_service_id(msg->msg + 3, digital);
1019}
1020
1021static inline void cec_msg_tuner_device_status(struct cec_msg *msg,
1022 const struct cec_op_tuner_device_info *tuner_dev_info)
1023{
1024 if (tuner_dev_info->is_analog)
1025 cec_msg_tuner_device_status_analog(msg,
1026 tuner_dev_info->rec_flag,
1027 tuner_dev_info->tuner_display_info,
1028 tuner_dev_info->analog.ana_bcast_type,
1029 tuner_dev_info->analog.ana_freq,
1030 tuner_dev_info->analog.bcast_system);
1031 else
1032 cec_msg_tuner_device_status_digital(msg,
1033 tuner_dev_info->rec_flag,
1034 tuner_dev_info->tuner_display_info,
1035 &tuner_dev_info->digital);
1036}
1037
1038static inline void cec_ops_tuner_device_status(const struct cec_msg *msg,
1039 struct cec_op_tuner_device_info *tuner_dev_info)
1040{
1041 tuner_dev_info->is_analog = msg->len < 10;
1042 tuner_dev_info->rec_flag = msg->msg[2] >> 7;
1043 tuner_dev_info->tuner_display_info = msg->msg[2] & 0x7f;
1044 if (tuner_dev_info->is_analog) {
1045 tuner_dev_info->analog.ana_bcast_type = msg->msg[3];
1046 tuner_dev_info->analog.ana_freq = (msg->msg[4] << 8) | msg->msg[5];
1047 tuner_dev_info->analog.bcast_system = msg->msg[6];
1048 return;
1049 }
1050 cec_get_digital_service_id(msg->msg + 3, &tuner_dev_info->digital);
1051}
1052
1053static inline void cec_msg_give_tuner_device_status(struct cec_msg *msg,
1054 bool reply,
1055 __u8 status_req)
1056{
1057 msg->len = 3;
1058 msg->msg[1] = CEC_MSG_GIVE_TUNER_DEVICE_STATUS;
1059 msg->msg[2] = status_req;
1060 msg->reply = reply ? CEC_MSG_TUNER_DEVICE_STATUS : 0;
1061}
1062
1063static inline void cec_ops_give_tuner_device_status(const struct cec_msg *msg,
1064 __u8 *status_req)
1065{
1066 *status_req = msg->msg[2];
1067}
1068
1069static inline void cec_msg_select_analogue_service(struct cec_msg *msg,
1070 __u8 ana_bcast_type,
1071 __u16 ana_freq,
1072 __u8 bcast_system)
1073{
1074 msg->len = 6;
1075 msg->msg[1] = CEC_MSG_SELECT_ANALOGUE_SERVICE;
1076 msg->msg[2] = ana_bcast_type;
1077 msg->msg[3] = ana_freq >> 8;
1078 msg->msg[4] = ana_freq & 0xff;
1079 msg->msg[5] = bcast_system;
1080}
1081
1082static inline void cec_ops_select_analogue_service(const struct cec_msg *msg,
1083 __u8 *ana_bcast_type,
1084 __u16 *ana_freq,
1085 __u8 *bcast_system)
1086{
1087 *ana_bcast_type = msg->msg[2];
1088 *ana_freq = (msg->msg[3] << 8) | msg->msg[4];
1089 *bcast_system = msg->msg[5];
1090}
1091
1092static inline void cec_msg_select_digital_service(struct cec_msg *msg,
1093 const struct cec_op_digital_service_id *digital)
1094{
1095 msg->len = 9;
1096 msg->msg[1] = CEC_MSG_SELECT_DIGITAL_SERVICE;
1097 cec_set_digital_service_id(msg->msg + 2, digital);
1098}
1099
1100static inline void cec_ops_select_digital_service(const struct cec_msg *msg,
1101 struct cec_op_digital_service_id *digital)
1102{
1103 cec_get_digital_service_id(msg->msg + 2, digital);
1104}
1105
1106static inline void cec_msg_tuner_step_decrement(struct cec_msg *msg)
1107{
1108 msg->len = 2;
1109 msg->msg[1] = CEC_MSG_TUNER_STEP_DECREMENT;
1110}
1111
1112static inline void cec_msg_tuner_step_increment(struct cec_msg *msg)
1113{
1114 msg->len = 2;
1115 msg->msg[1] = CEC_MSG_TUNER_STEP_INCREMENT;
1116}
1117
1118
1119/* Vendor Specific Commands Feature */
1120static inline void cec_msg_device_vendor_id(struct cec_msg *msg, __u32 vendor_id)
1121{
1122 msg->len = 5;
1123 msg->msg[0] |= 0xf; /* broadcast */
1124 msg->msg[1] = CEC_MSG_DEVICE_VENDOR_ID;
1125 msg->msg[2] = vendor_id >> 16;
1126 msg->msg[3] = (vendor_id >> 8) & 0xff;
1127 msg->msg[4] = vendor_id & 0xff;
1128}
1129
1130static inline void cec_ops_device_vendor_id(const struct cec_msg *msg,
1131 __u32 *vendor_id)
1132{
1133 *vendor_id = (msg->msg[2] << 16) | (msg->msg[3] << 8) | msg->msg[4];
1134}
1135
1136static inline void cec_msg_give_device_vendor_id(struct cec_msg *msg,
1137 bool reply)
1138{
1139 msg->len = 2;
1140 msg->msg[1] = CEC_MSG_GIVE_DEVICE_VENDOR_ID;
1141 msg->reply = reply ? CEC_MSG_DEVICE_VENDOR_ID : 0;
1142}
1143
1144static inline void cec_msg_vendor_remote_button_up(struct cec_msg *msg)
1145{
1146 msg->len = 2;
1147 msg->msg[1] = CEC_MSG_VENDOR_REMOTE_BUTTON_UP;
1148}
1149
1150
1151/* OSD Display Feature */
1152static inline void cec_msg_set_osd_string(struct cec_msg *msg,
1153 __u8 disp_ctl,
1154 const char *osd)
1155{
1156 unsigned int len = strlen(osd);
1157
1158 if (len > 13)
1159 len = 13;
1160 msg->len = 3 + len;
1161 msg->msg[1] = CEC_MSG_SET_OSD_STRING;
1162 msg->msg[2] = disp_ctl;
1163 memcpy(msg->msg + 3, osd, len);
1164}
1165
1166static inline void cec_ops_set_osd_string(const struct cec_msg *msg,
1167 __u8 *disp_ctl,
1168 char *osd)
1169{
1170 unsigned int len = msg->len > 3 ? msg->len - 3 : 0;
1171
1172 *disp_ctl = msg->msg[2];
1173 if (len > 13)
1174 len = 13;
1175 memcpy(osd, msg->msg + 3, len);
1176 osd[len] = '\0';
1177}
1178
1179
1180/* Device OSD Transfer Feature */
1181static inline void cec_msg_set_osd_name(struct cec_msg *msg, const char *name)
1182{
1183 unsigned int len = strlen(name);
1184
1185 if (len > 14)
1186 len = 14;
1187 msg->len = 2 + len;
1188 msg->msg[1] = CEC_MSG_SET_OSD_NAME;
1189 memcpy(msg->msg + 2, name, len);
1190}
1191
1192static inline void cec_ops_set_osd_name(const struct cec_msg *msg,
1193 char *name)
1194{
1195 unsigned int len = msg->len > 2 ? msg->len - 2 : 0;
1196
1197 if (len > 14)
1198 len = 14;
1199 memcpy(name, msg->msg + 2, len);
1200 name[len] = '\0';
1201}
1202
1203static inline void cec_msg_give_osd_name(struct cec_msg *msg,
1204 bool reply)
1205{
1206 msg->len = 2;
1207 msg->msg[1] = CEC_MSG_GIVE_OSD_NAME;
1208 msg->reply = reply ? CEC_MSG_SET_OSD_NAME : 0;
1209}
1210
1211
1212/* Device Menu Control Feature */
1213static inline void cec_msg_menu_status(struct cec_msg *msg,
1214 __u8 menu_state)
1215{
1216 msg->len = 3;
1217 msg->msg[1] = CEC_MSG_MENU_STATUS;
1218 msg->msg[2] = menu_state;
1219}
1220
1221static inline void cec_ops_menu_status(const struct cec_msg *msg,
1222 __u8 *menu_state)
1223{
1224 *menu_state = msg->msg[2];
1225}
1226
1227static inline void cec_msg_menu_request(struct cec_msg *msg,
1228 bool reply,
1229 __u8 menu_req)
1230{
1231 msg->len = 3;
1232 msg->msg[1] = CEC_MSG_MENU_REQUEST;
1233 msg->msg[2] = menu_req;
1234 msg->reply = reply ? CEC_MSG_MENU_STATUS : 0;
1235}
1236
1237static inline void cec_ops_menu_request(const struct cec_msg *msg,
1238 __u8 *menu_req)
1239{
1240 *menu_req = msg->msg[2];
1241}
1242
1243struct cec_op_ui_command {
1244 __u8 ui_cmd;
1245 bool has_opt_arg;
1246 union {
1247 struct cec_op_channel_data channel_identifier;
1248 __u8 ui_broadcast_type;
1249 __u8 ui_sound_presentation_control;
1250 __u8 play_mode;
1251 __u8 ui_function_media;
1252 __u8 ui_function_select_av_input;
1253 __u8 ui_function_select_audio_input;
1254 };
1255};
1256
1257static inline void cec_msg_user_control_pressed(struct cec_msg *msg,
1258 const struct cec_op_ui_command *ui_cmd)
1259{
1260 msg->len = 3;
1261 msg->msg[1] = CEC_MSG_USER_CONTROL_PRESSED;
1262 msg->msg[2] = ui_cmd->ui_cmd;
1263 if (!ui_cmd->has_opt_arg)
1264 return;
1265 switch (ui_cmd->ui_cmd) {
1266 case 0x56:
1267 case 0x57:
1268 case 0x60:
1269 case 0x68:
1270 case 0x69:
1271 case 0x6a:
1272 /* The optional operand is one byte for all these ui commands */
1273 msg->len++;
1274 msg->msg[3] = ui_cmd->play_mode;
1275 break;
1276 case 0x67:
1277 msg->len += 4;
1278 msg->msg[3] = (ui_cmd->channel_identifier.channel_number_fmt << 2) |
1279 (ui_cmd->channel_identifier.major >> 8);
1280 msg->msg[4] = ui_cmd->channel_identifier.major && 0xff;
1281 msg->msg[5] = ui_cmd->channel_identifier.minor >> 8;
1282 msg->msg[6] = ui_cmd->channel_identifier.minor & 0xff;
1283 break;
1284 }
1285}
1286
1287static inline void cec_ops_user_control_pressed(const struct cec_msg *msg,
1288 struct cec_op_ui_command *ui_cmd)
1289{
1290 ui_cmd->ui_cmd = msg->msg[2];
1291 ui_cmd->has_opt_arg = false;
1292 if (msg->len == 3)
1293 return;
1294 switch (ui_cmd->ui_cmd) {
1295 case 0x56:
1296 case 0x57:
1297 case 0x60:
1298 case 0x68:
1299 case 0x69:
1300 case 0x6a:
1301 /* The optional operand is one byte for all these ui commands */
1302 ui_cmd->play_mode = msg->msg[3];
1303 ui_cmd->has_opt_arg = true;
1304 break;
1305 case 0x67:
1306 if (msg->len < 7)
1307 break;
1308 ui_cmd->has_opt_arg = true;
1309 ui_cmd->channel_identifier.channel_number_fmt = msg->msg[3] >> 2;
1310 ui_cmd->channel_identifier.major = ((msg->msg[3] & 3) << 6) | msg->msg[4];
1311 ui_cmd->channel_identifier.minor = (msg->msg[5] << 8) | msg->msg[6];
1312 break;
1313 }
1314}
1315
1316static inline void cec_msg_user_control_released(struct cec_msg *msg)
1317{
1318 msg->len = 2;
1319 msg->msg[1] = CEC_MSG_USER_CONTROL_RELEASED;
1320}
1321
1322/* Remote Control Passthrough Feature */
1323
1324/* Power Status Feature */
1325static inline void cec_msg_report_power_status(struct cec_msg *msg,
1326 __u8 pwr_state)
1327{
1328 msg->len = 3;
1329 msg->msg[1] = CEC_MSG_REPORT_POWER_STATUS;
1330 msg->msg[2] = pwr_state;
1331}
1332
1333static inline void cec_ops_report_power_status(const struct cec_msg *msg,
1334 __u8 *pwr_state)
1335{
1336 *pwr_state = msg->msg[2];
1337}
1338
1339static inline void cec_msg_give_device_power_status(struct cec_msg *msg,
1340 bool reply)
1341{
1342 msg->len = 2;
1343 msg->msg[1] = CEC_MSG_GIVE_DEVICE_POWER_STATUS;
1344 msg->reply = reply ? CEC_MSG_REPORT_POWER_STATUS : 0;
1345}
1346
1347/* General Protocol Messages */
1348static inline void cec_msg_feature_abort(struct cec_msg *msg,
1349 __u8 abort_msg, __u8 reason)
1350{
1351 msg->len = 4;
1352 msg->msg[1] = CEC_MSG_FEATURE_ABORT;
1353 msg->msg[2] = abort_msg;
1354 msg->msg[3] = reason;
1355}
1356
1357static inline void cec_ops_feature_abort(const struct cec_msg *msg,
1358 __u8 *abort_msg, __u8 *reason)
1359{
1360 *abort_msg = msg->msg[2];
1361 *reason = msg->msg[3];
1362}
1363
1364/* This changes the current message into a feature abort message */
1365static inline void cec_msg_reply_feature_abort(struct cec_msg *msg, __u8 reason)
1366{
1367 cec_msg_set_reply_to(msg, msg);
1368 msg->len = 4;
1369 msg->msg[2] = msg->msg[1];
1370 msg->msg[3] = reason;
1371 msg->msg[1] = CEC_MSG_FEATURE_ABORT;
1372}
1373
1374static inline void cec_msg_abort(struct cec_msg *msg)
1375{
1376 msg->len = 2;
1377 msg->msg[1] = CEC_MSG_ABORT;
1378}
1379
1380
1381/* System Audio Control Feature */
1382static inline void cec_msg_report_audio_status(struct cec_msg *msg,
1383 __u8 aud_mute_status,
1384 __u8 aud_vol_status)
1385{
1386 msg->len = 3;
1387 msg->msg[1] = CEC_MSG_REPORT_AUDIO_STATUS;
1388 msg->msg[2] = (aud_mute_status << 7) | (aud_vol_status & 0x7f);
1389}
1390
1391static inline void cec_ops_report_audio_status(const struct cec_msg *msg,
1392 __u8 *aud_mute_status,
1393 __u8 *aud_vol_status)
1394{
1395 *aud_mute_status = msg->msg[2] >> 7;
1396 *aud_vol_status = msg->msg[2] & 0x7f;
1397}
1398
1399static inline void cec_msg_give_audio_status(struct cec_msg *msg,
1400 bool reply)
1401{
1402 msg->len = 2;
1403 msg->msg[1] = CEC_MSG_GIVE_AUDIO_STATUS;
1404 msg->reply = reply ? CEC_MSG_REPORT_AUDIO_STATUS : 0;
1405}
1406
1407static inline void cec_msg_set_system_audio_mode(struct cec_msg *msg,
1408 __u8 sys_aud_status)
1409{
1410 msg->len = 3;
1411 msg->msg[1] = CEC_MSG_SET_SYSTEM_AUDIO_MODE;
1412 msg->msg[2] = sys_aud_status;
1413}
1414
1415static inline void cec_ops_set_system_audio_mode(const struct cec_msg *msg,
1416 __u8 *sys_aud_status)
1417{
1418 *sys_aud_status = msg->msg[2];
1419}
1420
1421static inline void cec_msg_system_audio_mode_request(struct cec_msg *msg,
1422 bool reply,
1423 __u16 phys_addr)
1424{
1425 msg->len = phys_addr == 0xffff ? 2 : 4;
1426 msg->msg[1] = CEC_MSG_SYSTEM_AUDIO_MODE_REQUEST;
1427 msg->msg[2] = phys_addr >> 8;
1428 msg->msg[3] = phys_addr & 0xff;
1429 msg->reply = reply ? CEC_MSG_SET_SYSTEM_AUDIO_MODE : 0;
1430
1431}
1432
1433static inline void cec_ops_system_audio_mode_request(const struct cec_msg *msg,
1434 __u16 *phys_addr)
1435{
1436 if (msg->len < 4)
1437 *phys_addr = 0xffff;
1438 else
1439 *phys_addr = (msg->msg[2] << 8) | msg->msg[3];
1440}
1441
1442static inline void cec_msg_system_audio_mode_status(struct cec_msg *msg,
1443 __u8 sys_aud_status)
1444{
1445 msg->len = 3;
1446 msg->msg[1] = CEC_MSG_SYSTEM_AUDIO_MODE_STATUS;
1447 msg->msg[2] = sys_aud_status;
1448}
1449
1450static inline void cec_ops_system_audio_mode_status(const struct cec_msg *msg,
1451 __u8 *sys_aud_status)
1452{
1453 *sys_aud_status = msg->msg[2];
1454}
1455
1456static inline void cec_msg_give_system_audio_mode_status(struct cec_msg *msg,
1457 bool reply)
1458{
1459 msg->len = 2;
1460 msg->msg[1] = CEC_MSG_GIVE_SYSTEM_AUDIO_MODE_STATUS;
1461 msg->reply = reply ? CEC_MSG_SYSTEM_AUDIO_MODE_STATUS : 0;
1462}
1463
1464static inline void cec_msg_report_short_audio_descriptor(struct cec_msg *msg,
1465 __u8 num_descriptors,
1466 const __u32 *descriptors)
1467{
1468 unsigned int i;
1469
1470 if (num_descriptors > 4)
1471 num_descriptors = 4;
1472 msg->len = 2 + num_descriptors * 3;
1473 msg->msg[1] = CEC_MSG_REPORT_SHORT_AUDIO_DESCRIPTOR;
1474 for (i = 0; i < num_descriptors; i++) {
1475 msg->msg[2 + i * 3] = (descriptors[i] >> 16) & 0xff;
1476 msg->msg[3 + i * 3] = (descriptors[i] >> 8) & 0xff;
1477 msg->msg[4 + i * 3] = descriptors[i] & 0xff;
1478 }
1479}
1480
1481static inline void cec_ops_report_short_audio_descriptor(const struct cec_msg *msg,
1482 __u8 *num_descriptors,
1483 __u32 *descriptors)
1484{
1485 unsigned int i;
1486
1487 *num_descriptors = (msg->len - 2) / 3;
1488 if (*num_descriptors > 4)
1489 *num_descriptors = 4;
1490 for (i = 0; i < *num_descriptors; i++)
1491 descriptors[i] = (msg->msg[2 + i * 3] << 16) |
1492 (msg->msg[3 + i * 3] << 8) |
1493 msg->msg[4 + i * 3];
1494}
1495
1496static inline void cec_msg_request_short_audio_descriptor(struct cec_msg *msg,
1497 bool reply,
1498 __u8 num_descriptors,
1499 const __u8 *audio_format_id,
1500 const __u8 *audio_format_code)
1501{
1502 unsigned int i;
1503
1504 if (num_descriptors > 4)
1505 num_descriptors = 4;
1506 msg->len = 2 + num_descriptors;
1507 msg->msg[1] = CEC_MSG_REQUEST_SHORT_AUDIO_DESCRIPTOR;
1508 msg->reply = reply ? CEC_MSG_REPORT_SHORT_AUDIO_DESCRIPTOR : 0;
1509 for (i = 0; i < num_descriptors; i++)
1510 msg->msg[2 + i] = (audio_format_id[i] << 6) |
1511 (audio_format_code[i] & 0x3f);
1512}
1513
1514static inline void cec_ops_request_short_audio_descriptor(const struct cec_msg *msg,
1515 __u8 *num_descriptors,
1516 __u8 *audio_format_id,
1517 __u8 *audio_format_code)
1518{
1519 unsigned int i;
1520
1521 *num_descriptors = msg->len - 2;
1522 if (*num_descriptors > 4)
1523 *num_descriptors = 4;
1524 for (i = 0; i < *num_descriptors; i++) {
1525 audio_format_id[i] = msg->msg[2 + i] >> 6;
1526 audio_format_code[i] = msg->msg[2 + i] & 0x3f;
1527 }
1528}
1529
1530
1531/* Audio Rate Control Feature */
1532static inline void cec_msg_set_audio_rate(struct cec_msg *msg,
1533 __u8 audio_rate)
1534{
1535 msg->len = 3;
1536 msg->msg[1] = CEC_MSG_SET_AUDIO_RATE;
1537 msg->msg[2] = audio_rate;
1538}
1539
1540static inline void cec_ops_set_audio_rate(const struct cec_msg *msg,
1541 __u8 *audio_rate)
1542{
1543 *audio_rate = msg->msg[2];
1544}
1545
1546
1547/* Audio Return Channel Control Feature */
1548static inline void cec_msg_report_arc_initiated(struct cec_msg *msg)
1549{
1550 msg->len = 2;
1551 msg->msg[1] = CEC_MSG_REPORT_ARC_INITIATED;
1552}
1553
1554static inline void cec_msg_initiate_arc(struct cec_msg *msg,
1555 bool reply)
1556{
1557 msg->len = 2;
1558 msg->msg[1] = CEC_MSG_INITIATE_ARC;
1559 msg->reply = reply ? CEC_MSG_REPORT_ARC_INITIATED : 0;
1560}
1561
1562static inline void cec_msg_request_arc_initiation(struct cec_msg *msg,
1563 bool reply)
1564{
1565 msg->len = 2;
1566 msg->msg[1] = CEC_MSG_REQUEST_ARC_INITIATION;
1567 msg->reply = reply ? CEC_MSG_INITIATE_ARC : 0;
1568}
1569
1570static inline void cec_msg_report_arc_terminated(struct cec_msg *msg)
1571{
1572 msg->len = 2;
1573 msg->msg[1] = CEC_MSG_REPORT_ARC_TERMINATED;
1574}
1575
1576static inline void cec_msg_terminate_arc(struct cec_msg *msg,
1577 bool reply)
1578{
1579 msg->len = 2;
1580 msg->msg[1] = CEC_MSG_TERMINATE_ARC;
1581 msg->reply = reply ? CEC_MSG_REPORT_ARC_TERMINATED : 0;
1582}
1583
1584static inline void cec_msg_request_arc_termination(struct cec_msg *msg,
1585 bool reply)
1586{
1587 msg->len = 2;
1588 msg->msg[1] = CEC_MSG_REQUEST_ARC_TERMINATION;
1589 msg->reply = reply ? CEC_MSG_TERMINATE_ARC : 0;
1590}
1591
1592
1593/* Dynamic Audio Lipsync Feature */
1594/* Only for CEC 2.0 and up */
1595static inline void cec_msg_report_current_latency(struct cec_msg *msg,
1596 __u16 phys_addr,
1597 __u8 video_latency,
1598 __u8 low_latency_mode,
1599 __u8 audio_out_compensated,
1600 __u8 audio_out_delay)
1601{
1602 msg->len = 7;
1603 msg->msg[0] |= 0xf; /* broadcast */
1604 msg->msg[1] = CEC_MSG_REPORT_CURRENT_LATENCY;
1605 msg->msg[2] = phys_addr >> 8;
1606 msg->msg[3] = phys_addr & 0xff;
1607 msg->msg[4] = video_latency;
1608 msg->msg[5] = (low_latency_mode << 2) | audio_out_compensated;
1609 msg->msg[6] = audio_out_delay;
1610}
1611
1612static inline void cec_ops_report_current_latency(const struct cec_msg *msg,
1613 __u16 *phys_addr,
1614 __u8 *video_latency,
1615 __u8 *low_latency_mode,
1616 __u8 *audio_out_compensated,
1617 __u8 *audio_out_delay)
1618{
1619 *phys_addr = (msg->msg[2] << 8) | msg->msg[3];
1620 *video_latency = msg->msg[4];
1621 *low_latency_mode = (msg->msg[5] >> 2) & 1;
1622 *audio_out_compensated = msg->msg[5] & 3;
1623 *audio_out_delay = msg->msg[6];
1624}
1625
1626static inline void cec_msg_request_current_latency(struct cec_msg *msg,
1627 bool reply,
1628 __u16 phys_addr)
1629{
1630 msg->len = 4;
1631 msg->msg[0] |= 0xf; /* broadcast */
1632 msg->msg[1] = CEC_MSG_REQUEST_CURRENT_LATENCY;
1633 msg->msg[2] = phys_addr >> 8;
1634 msg->msg[3] = phys_addr & 0xff;
1635 msg->reply = reply ? CEC_MSG_REPORT_CURRENT_LATENCY : 0;
1636}
1637
1638static inline void cec_ops_request_current_latency(const struct cec_msg *msg,
1639 __u16 *phys_addr)
1640{
1641 *phys_addr = (msg->msg[2] << 8) | msg->msg[3];
1642}
1643
1644
1645/* Capability Discovery and Control Feature */
1646static inline void cec_msg_cdc_hec_inquire_state(struct cec_msg *msg,
1647 __u16 phys_addr1,
1648 __u16 phys_addr2)
1649{
1650 msg->len = 9;
1651 msg->msg[0] |= 0xf; /* broadcast */
1652 msg->msg[1] = CEC_MSG_CDC_MESSAGE;
1653 /* msg[2] and msg[3] (phys_addr) are filled in by the CEC framework */
1654 msg->msg[4] = CEC_MSG_CDC_HEC_INQUIRE_STATE;
1655 msg->msg[5] = phys_addr1 >> 8;
1656 msg->msg[6] = phys_addr1 & 0xff;
1657 msg->msg[7] = phys_addr2 >> 8;
1658 msg->msg[8] = phys_addr2 & 0xff;
1659}
1660
1661static inline void cec_ops_cdc_hec_inquire_state(const struct cec_msg *msg,
1662 __u16 *phys_addr,
1663 __u16 *phys_addr1,
1664 __u16 *phys_addr2)
1665{
1666 *phys_addr = (msg->msg[2] << 8) | msg->msg[3];
1667 *phys_addr1 = (msg->msg[5] << 8) | msg->msg[6];
1668 *phys_addr2 = (msg->msg[7] << 8) | msg->msg[8];
1669}
1670
1671static inline void cec_msg_cdc_hec_report_state(struct cec_msg *msg,
1672 __u16 target_phys_addr,
1673 __u8 hec_func_state,
1674 __u8 host_func_state,
1675 __u8 enc_func_state,
1676 __u8 cdc_errcode,
1677 __u8 has_field,
1678 __u16 hec_field)
1679{
1680 msg->len = has_field ? 10 : 8;
1681 msg->msg[0] |= 0xf; /* broadcast */
1682 msg->msg[1] = CEC_MSG_CDC_MESSAGE;
1683 /* msg[2] and msg[3] (phys_addr) are filled in by the CEC framework */
1684 msg->msg[4] = CEC_MSG_CDC_HEC_REPORT_STATE;
1685 msg->msg[5] = target_phys_addr >> 8;
1686 msg->msg[6] = target_phys_addr & 0xff;
1687 msg->msg[7] = (hec_func_state << 6) |
1688 (host_func_state << 4) |
1689 (enc_func_state << 2) |
1690 cdc_errcode;
1691 if (has_field) {
1692 msg->msg[8] = hec_field >> 8;
1693 msg->msg[9] = hec_field & 0xff;
1694 }
1695}
1696
1697static inline void cec_ops_cdc_hec_report_state(const struct cec_msg *msg,
1698 __u16 *phys_addr,
1699 __u16 *target_phys_addr,
1700 __u8 *hec_func_state,
1701 __u8 *host_func_state,
1702 __u8 *enc_func_state,
1703 __u8 *cdc_errcode,
1704 __u8 *has_field,
1705 __u16 *hec_field)
1706{
1707 *phys_addr = (msg->msg[2] << 8) | msg->msg[3];
1708 *target_phys_addr = (msg->msg[5] << 8) | msg->msg[6];
1709 *hec_func_state = msg->msg[7] >> 6;
1710 *host_func_state = (msg->msg[7] >> 4) & 3;
1711 *enc_func_state = (msg->msg[7] >> 4) & 3;
1712 *cdc_errcode = msg->msg[7] & 3;
1713 *has_field = msg->len >= 10;
1714 *hec_field = *has_field ? ((msg->msg[8] << 8) | msg->msg[9]) : 0;
1715}
1716
1717static inline void cec_msg_cdc_hec_set_state(struct cec_msg *msg,
1718 __u16 phys_addr1,
1719 __u16 phys_addr2,
1720 __u8 hec_set_state,
1721 __u16 phys_addr3,
1722 __u16 phys_addr4,
1723 __u16 phys_addr5)
1724{
1725 msg->len = 10;
1726 msg->msg[0] |= 0xf; /* broadcast */
1727 msg->msg[1] = CEC_MSG_CDC_MESSAGE;
1728 /* msg[2] and msg[3] (phys_addr) are filled in by the CEC framework */
1729 msg->msg[4] = CEC_MSG_CDC_HEC_INQUIRE_STATE;
1730 msg->msg[5] = phys_addr1 >> 8;
1731 msg->msg[6] = phys_addr1 & 0xff;
1732 msg->msg[7] = phys_addr2 >> 8;
1733 msg->msg[8] = phys_addr2 & 0xff;
1734 msg->msg[9] = hec_set_state;
1735 if (phys_addr3 != CEC_PHYS_ADDR_INVALID) {
1736 msg->msg[msg->len++] = phys_addr3 >> 8;
1737 msg->msg[msg->len++] = phys_addr3 & 0xff;
1738 if (phys_addr4 != CEC_PHYS_ADDR_INVALID) {
1739 msg->msg[msg->len++] = phys_addr4 >> 8;
1740 msg->msg[msg->len++] = phys_addr4 & 0xff;
1741 if (phys_addr5 != CEC_PHYS_ADDR_INVALID) {
1742 msg->msg[msg->len++] = phys_addr5 >> 8;
1743 msg->msg[msg->len++] = phys_addr5 & 0xff;
1744 }
1745 }
1746 }
1747}
1748
1749static inline void cec_ops_cdc_hec_set_state(const struct cec_msg *msg,
1750 __u16 *phys_addr,
1751 __u16 *phys_addr1,
1752 __u16 *phys_addr2,
1753 __u8 *hec_set_state,
1754 __u16 *phys_addr3,
1755 __u16 *phys_addr4,
1756 __u16 *phys_addr5)
1757{
1758 *phys_addr = (msg->msg[2] << 8) | msg->msg[3];
1759 *phys_addr1 = (msg->msg[5] << 8) | msg->msg[6];
1760 *phys_addr2 = (msg->msg[7] << 8) | msg->msg[8];
1761 *hec_set_state = msg->msg[9];
1762 *phys_addr3 = *phys_addr4 = *phys_addr5 = CEC_PHYS_ADDR_INVALID;
1763 if (msg->len >= 12)
1764 *phys_addr3 = (msg->msg[10] << 8) | msg->msg[11];
1765 if (msg->len >= 14)
1766 *phys_addr4 = (msg->msg[12] << 8) | msg->msg[13];
1767 if (msg->len >= 16)
1768 *phys_addr5 = (msg->msg[14] << 8) | msg->msg[15];
1769}
1770
1771static inline void cec_msg_cdc_hec_set_state_adjacent(struct cec_msg *msg,
1772 __u16 phys_addr1,
1773 __u8 hec_set_state)
1774{
1775 msg->len = 8;
1776 msg->msg[0] |= 0xf; /* broadcast */
1777 msg->msg[1] = CEC_MSG_CDC_MESSAGE;
1778 /* msg[2] and msg[3] (phys_addr) are filled in by the CEC framework */
1779 msg->msg[4] = CEC_MSG_CDC_HEC_SET_STATE_ADJACENT;
1780 msg->msg[5] = phys_addr1 >> 8;
1781 msg->msg[6] = phys_addr1 & 0xff;
1782 msg->msg[7] = hec_set_state;
1783}
1784
1785static inline void cec_ops_cdc_hec_set_state_adjacent(const struct cec_msg *msg,
1786 __u16 *phys_addr,
1787 __u16 *phys_addr1,
1788 __u8 *hec_set_state)
1789{
1790 *phys_addr = (msg->msg[2] << 8) | msg->msg[3];
1791 *phys_addr1 = (msg->msg[5] << 8) | msg->msg[6];
1792 *hec_set_state = msg->msg[7];
1793}
1794
1795static inline void cec_msg_cdc_hec_request_deactivation(struct cec_msg *msg,
1796 __u16 phys_addr1,
1797 __u16 phys_addr2,
1798 __u16 phys_addr3)
1799{
1800 msg->len = 11;
1801 msg->msg[0] |= 0xf; /* broadcast */
1802 msg->msg[1] = CEC_MSG_CDC_MESSAGE;
1803 /* msg[2] and msg[3] (phys_addr) are filled in by the CEC framework */
1804 msg->msg[4] = CEC_MSG_CDC_HEC_REQUEST_DEACTIVATION;
1805 msg->msg[5] = phys_addr1 >> 8;
1806 msg->msg[6] = phys_addr1 & 0xff;
1807 msg->msg[7] = phys_addr2 >> 8;
1808 msg->msg[8] = phys_addr2 & 0xff;
1809 msg->msg[9] = phys_addr3 >> 8;
1810 msg->msg[10] = phys_addr3 & 0xff;
1811}
1812
1813static inline void cec_ops_cdc_hec_request_deactivation(const struct cec_msg *msg,
1814 __u16 *phys_addr,
1815 __u16 *phys_addr1,
1816 __u16 *phys_addr2,
1817 __u16 *phys_addr3)
1818{
1819 *phys_addr = (msg->msg[2] << 8) | msg->msg[3];
1820 *phys_addr1 = (msg->msg[5] << 8) | msg->msg[6];
1821 *phys_addr2 = (msg->msg[7] << 8) | msg->msg[8];
1822 *phys_addr3 = (msg->msg[9] << 8) | msg->msg[10];
1823}
1824
1825static inline void cec_msg_cdc_hec_notify_alive(struct cec_msg *msg)
1826{
1827 msg->len = 5;
1828 msg->msg[0] |= 0xf; /* broadcast */
1829 msg->msg[1] = CEC_MSG_CDC_MESSAGE;
1830 /* msg[2] and msg[3] (phys_addr) are filled in by the CEC framework */
1831 msg->msg[4] = CEC_MSG_CDC_HEC_NOTIFY_ALIVE;
1832}
1833
1834static inline void cec_ops_cdc_hec_notify_alive(const struct cec_msg *msg,
1835 __u16 *phys_addr)
1836{
1837 *phys_addr = (msg->msg[2] << 8) | msg->msg[3];
1838}
1839
1840static inline void cec_msg_cdc_hec_discover(struct cec_msg *msg)
1841{
1842 msg->len = 5;
1843 msg->msg[0] |= 0xf; /* broadcast */
1844 msg->msg[1] = CEC_MSG_CDC_MESSAGE;
1845 /* msg[2] and msg[3] (phys_addr) are filled in by the CEC framework */
1846 msg->msg[4] = CEC_MSG_CDC_HEC_DISCOVER;
1847}
1848
1849static inline void cec_ops_cdc_hec_discover(const struct cec_msg *msg,
1850 __u16 *phys_addr)
1851{
1852 *phys_addr = (msg->msg[2] << 8) | msg->msg[3];
1853}
1854
1855static inline void cec_msg_cdc_hpd_set_state(struct cec_msg *msg,
1856 __u8 input_port,
1857 __u8 hpd_state)
1858{
1859 msg->len = 6;
1860 msg->msg[0] |= 0xf; /* broadcast */
1861 msg->msg[1] = CEC_MSG_CDC_MESSAGE;
1862 /* msg[2] and msg[3] (phys_addr) are filled in by the CEC framework */
1863 msg->msg[4] = CEC_MSG_CDC_HPD_SET_STATE;
1864 msg->msg[5] = (input_port << 4) | hpd_state;
1865}
1866
1867static inline void cec_ops_cdc_hpd_set_state(const struct cec_msg *msg,
1868 __u16 *phys_addr,
1869 __u8 *input_port,
1870 __u8 *hpd_state)
1871{
1872 *phys_addr = (msg->msg[2] << 8) | msg->msg[3];
1873 *input_port = msg->msg[5] >> 4;
1874 *hpd_state = msg->msg[5] & 0xf;
1875}
1876
1877static inline void cec_msg_cdc_hpd_report_state(struct cec_msg *msg,
1878 __u8 hpd_state,
1879 __u8 hpd_error)
1880{
1881 msg->len = 6;
1882 msg->msg[0] |= 0xf; /* broadcast */
1883 msg->msg[1] = CEC_MSG_CDC_MESSAGE;
1884 /* msg[2] and msg[3] (phys_addr) are filled in by the CEC framework */
1885 msg->msg[4] = CEC_MSG_CDC_HPD_REPORT_STATE;
1886 msg->msg[5] = (hpd_state << 4) | hpd_error;
1887}
1888
1889static inline void cec_ops_cdc_hpd_report_state(const struct cec_msg *msg,
1890 __u16 *phys_addr,
1891 __u8 *hpd_state,
1892 __u8 *hpd_error)
1893{
1894 *phys_addr = (msg->msg[2] << 8) | msg->msg[3];
1895 *hpd_state = msg->msg[5] >> 4;
1896 *hpd_error = msg->msg[5] & 0xf;
1897}
1898
1899#endif
diff --git a/include/linux/cec.h b/include/linux/cec.h
new file mode 100644
index 000000000000..b3e22893a002
--- /dev/null
+++ b/include/linux/cec.h
@@ -0,0 +1,1011 @@
1/*
2 * cec - HDMI Consumer Electronics Control public header
3 *
4 * Copyright 2016 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
5 *
6 * This program is free software; you may redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * Alternatively you can redistribute this file under the terms of the
11 * BSD license as stated below:
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in
20 * the documentation and/or other materials provided with the
21 * distribution.
22 * 3. The names of its contributors may not be used to endorse or promote
23 * products derived from this software without specific prior written
24 * permission.
25 *
26 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
30 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
31 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
32 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
33 * SOFTWARE.
34 */
35
36/*
37 * Note: this framework is still in staging and it is likely the API
38 * will change before it goes out of staging.
39 *
40 * Once it is moved out of staging this header will move to uapi.
41 */
42#ifndef _CEC_UAPI_H
43#define _CEC_UAPI_H
44
45#include <linux/types.h>
46
47#define CEC_MAX_MSG_SIZE 16
48
49/**
50 * struct cec_msg - CEC message structure.
51 * @tx_ts: Timestamp in nanoseconds using CLOCK_MONOTONIC. Set by the
52 * driver when the message transmission has finished.
53 * @rx_ts: Timestamp in nanoseconds using CLOCK_MONOTONIC. Set by the
54 * driver when the message was received.
55 * @len: Length in bytes of the message.
56 * @timeout: The timeout (in ms) that is used to timeout CEC_RECEIVE.
57 * Set to 0 if you want to wait forever. This timeout can also be
58 * used with CEC_TRANSMIT as the timeout for waiting for a reply.
59 * If 0, then it will use a 1 second timeout instead of waiting
60 * forever as is done with CEC_RECEIVE.
61 * @sequence: The framework assigns a sequence number to messages that are
62 * sent. This can be used to track replies to previously sent
63 * messages.
64 * @flags: Set to 0.
65 * @msg: The message payload.
66 * @reply: This field is ignored with CEC_RECEIVE and is only used by
67 * CEC_TRANSMIT. If non-zero, then wait for a reply with this
68 * opcode. Set to CEC_MSG_FEATURE_ABORT if you want to wait for
69 * a possible ABORT reply. If there was an error when sending the
70 * msg or FeatureAbort was returned, then reply is set to 0.
71 * If reply is non-zero upon return, then len/msg are set to
72 * the received message.
73 * If reply is zero upon return and status has the
74 * CEC_TX_STATUS_FEATURE_ABORT bit set, then len/msg are set to
75 * the received feature abort message.
76 * If reply is zero upon return and status has the
77 * CEC_TX_STATUS_MAX_RETRIES bit set, then no reply was seen at
78 * all. If reply is non-zero for CEC_TRANSMIT and the message is a
79 * broadcast, then -EINVAL is returned.
80 * if reply is non-zero, then timeout is set to 1000 (the required
81 * maximum response time).
82 * @rx_status: The message receive status bits. Set by the driver.
83 * @tx_status: The message transmit status bits. Set by the driver.
84 * @tx_arb_lost_cnt: The number of 'Arbitration Lost' events. Set by the driver.
85 * @tx_nack_cnt: The number of 'Not Acknowledged' events. Set by the driver.
86 * @tx_low_drive_cnt: The number of 'Low Drive Detected' events. Set by the
87 * driver.
88 * @tx_error_cnt: The number of 'Error' events. Set by the driver.
89 */
90struct cec_msg {
91 __u64 tx_ts;
92 __u64 rx_ts;
93 __u32 len;
94 __u32 timeout;
95 __u32 sequence;
96 __u32 flags;
97 __u8 msg[CEC_MAX_MSG_SIZE];
98 __u8 reply;
99 __u8 rx_status;
100 __u8 tx_status;
101 __u8 tx_arb_lost_cnt;
102 __u8 tx_nack_cnt;
103 __u8 tx_low_drive_cnt;
104 __u8 tx_error_cnt;
105};
106
107/**
108 * cec_msg_initiator - return the initiator's logical address.
109 * @msg: the message structure
110 */
111static inline __u8 cec_msg_initiator(const struct cec_msg *msg)
112{
113 return msg->msg[0] >> 4;
114}
115
116/**
117 * cec_msg_destination - return the destination's logical address.
118 * @msg: the message structure
119 */
120static inline __u8 cec_msg_destination(const struct cec_msg *msg)
121{
122 return msg->msg[0] & 0xf;
123}
124
125/**
126 * cec_msg_opcode - return the opcode of the message, -1 for poll
127 * @msg: the message structure
128 */
129static inline int cec_msg_opcode(const struct cec_msg *msg)
130{
131 return msg->len > 1 ? msg->msg[1] : -1;
132}
133
134/**
135 * cec_msg_is_broadcast - return true if this is a broadcast message.
136 * @msg: the message structure
137 */
138static inline bool cec_msg_is_broadcast(const struct cec_msg *msg)
139{
140 return (msg->msg[0] & 0xf) == 0xf;
141}
142
143/**
144 * cec_msg_init - initialize the message structure.
145 * @msg: the message structure
146 * @initiator: the logical address of the initiator
147 * @destination:the logical address of the destination (0xf for broadcast)
148 *
149 * The whole structure is zeroed, the len field is set to 1 (i.e. a poll
150 * message) and the initiator and destination are filled in.
151 */
152static inline void cec_msg_init(struct cec_msg *msg,
153 __u8 initiator, __u8 destination)
154{
155 memset(msg, 0, sizeof(*msg));
156 msg->msg[0] = (initiator << 4) | destination;
157 msg->len = 1;
158}
159
160/**
161 * cec_msg_set_reply_to - fill in destination/initiator in a reply message.
162 * @msg: the message structure for the reply
163 * @orig: the original message structure
164 *
165 * Set the msg destination to the orig initiator and the msg initiator to the
166 * orig destination. Note that msg and orig may be the same pointer, in which
167 * case the change is done in place.
168 */
169static inline void cec_msg_set_reply_to(struct cec_msg *msg,
170 struct cec_msg *orig)
171{
172 /* The destination becomes the initiator and vice versa */
173 msg->msg[0] = (cec_msg_destination(orig) << 4) |
174 cec_msg_initiator(orig);
175 msg->reply = msg->timeout = 0;
176}
177
178/* cec status field */
179#define CEC_TX_STATUS_OK (1 << 0)
180#define CEC_TX_STATUS_ARB_LOST (1 << 1)
181#define CEC_TX_STATUS_NACK (1 << 2)
182#define CEC_TX_STATUS_LOW_DRIVE (1 << 3)
183#define CEC_TX_STATUS_ERROR (1 << 4)
184#define CEC_TX_STATUS_MAX_RETRIES (1 << 5)
185
186#define CEC_RX_STATUS_OK (1 << 0)
187#define CEC_RX_STATUS_TIMEOUT (1 << 1)
188#define CEC_RX_STATUS_FEATURE_ABORT (1 << 2)
189
190static inline bool cec_msg_status_is_ok(const struct cec_msg *msg)
191{
192 if (msg->tx_status && !(msg->tx_status & CEC_TX_STATUS_OK))
193 return false;
194 if (msg->rx_status && !(msg->rx_status & CEC_RX_STATUS_OK))
195 return false;
196 if (!msg->tx_status && !msg->rx_status)
197 return false;
198 return !(msg->rx_status & CEC_RX_STATUS_FEATURE_ABORT);
199}
200
201#define CEC_LOG_ADDR_INVALID 0xff
202#define CEC_PHYS_ADDR_INVALID 0xffff
203
204/*
205 * The maximum number of logical addresses one device can be assigned to.
206 * The CEC 2.0 spec allows for only 2 logical addresses at the moment. The
207 * Analog Devices CEC hardware supports 3. So let's go wild and go for 4.
208 */
209#define CEC_MAX_LOG_ADDRS 4
210
211/* The logical addresses defined by CEC 2.0 */
212#define CEC_LOG_ADDR_TV 0
213#define CEC_LOG_ADDR_RECORD_1 1
214#define CEC_LOG_ADDR_RECORD_2 2
215#define CEC_LOG_ADDR_TUNER_1 3
216#define CEC_LOG_ADDR_PLAYBACK_1 4
217#define CEC_LOG_ADDR_AUDIOSYSTEM 5
218#define CEC_LOG_ADDR_TUNER_2 6
219#define CEC_LOG_ADDR_TUNER_3 7
220#define CEC_LOG_ADDR_PLAYBACK_2 8
221#define CEC_LOG_ADDR_RECORD_3 9
222#define CEC_LOG_ADDR_TUNER_4 10
223#define CEC_LOG_ADDR_PLAYBACK_3 11
224#define CEC_LOG_ADDR_BACKUP_1 12
225#define CEC_LOG_ADDR_BACKUP_2 13
226#define CEC_LOG_ADDR_SPECIFIC 14
227#define CEC_LOG_ADDR_UNREGISTERED 15 /* as initiator address */
228#define CEC_LOG_ADDR_BROADCAST 15 /* ad destination address */
229
230/* The logical address types that the CEC device wants to claim */
231#define CEC_LOG_ADDR_TYPE_TV 0
232#define CEC_LOG_ADDR_TYPE_RECORD 1
233#define CEC_LOG_ADDR_TYPE_TUNER 2
234#define CEC_LOG_ADDR_TYPE_PLAYBACK 3
235#define CEC_LOG_ADDR_TYPE_AUDIOSYSTEM 4
236#define CEC_LOG_ADDR_TYPE_SPECIFIC 5
237#define CEC_LOG_ADDR_TYPE_UNREGISTERED 6
238/*
239 * Switches should use UNREGISTERED.
240 * Processors should use SPECIFIC.
241 */
242
243#define CEC_LOG_ADDR_MASK_TV (1 << CEC_LOG_ADDR_TV)
244#define CEC_LOG_ADDR_MASK_RECORD ((1 << CEC_LOG_ADDR_RECORD_1) | \
245 (1 << CEC_LOG_ADDR_RECORD_2) | \
246 (1 << CEC_LOG_ADDR_RECORD_3))
247#define CEC_LOG_ADDR_MASK_TUNER ((1 << CEC_LOG_ADDR_TUNER_1) | \
248 (1 << CEC_LOG_ADDR_TUNER_2) | \
249 (1 << CEC_LOG_ADDR_TUNER_3) | \
250 (1 << CEC_LOG_ADDR_TUNER_4))
251#define CEC_LOG_ADDR_MASK_PLAYBACK ((1 << CEC_LOG_ADDR_PLAYBACK_1) | \
252 (1 << CEC_LOG_ADDR_PLAYBACK_2) | \
253 (1 << CEC_LOG_ADDR_PLAYBACK_3))
254#define CEC_LOG_ADDR_MASK_AUDIOSYSTEM (1 << CEC_LOG_ADDR_AUDIOSYSTEM)
255#define CEC_LOG_ADDR_MASK_BACKUP ((1 << CEC_LOG_ADDR_BACKUP_1) | \
256 (1 << CEC_LOG_ADDR_BACKUP_2))
257#define CEC_LOG_ADDR_MASK_SPECIFIC (1 << CEC_LOG_ADDR_SPECIFIC)
258#define CEC_LOG_ADDR_MASK_UNREGISTERED (1 << CEC_LOG_ADDR_UNREGISTERED)
259
260static inline bool cec_has_tv(__u16 log_addr_mask)
261{
262 return log_addr_mask & CEC_LOG_ADDR_MASK_TV;
263}
264
265static inline bool cec_has_record(__u16 log_addr_mask)
266{
267 return log_addr_mask & CEC_LOG_ADDR_MASK_RECORD;
268}
269
270static inline bool cec_has_tuner(__u16 log_addr_mask)
271{
272 return log_addr_mask & CEC_LOG_ADDR_MASK_TUNER;
273}
274
275static inline bool cec_has_playback(__u16 log_addr_mask)
276{
277 return log_addr_mask & CEC_LOG_ADDR_MASK_PLAYBACK;
278}
279
280static inline bool cec_has_audiosystem(__u16 log_addr_mask)
281{
282 return log_addr_mask & CEC_LOG_ADDR_MASK_AUDIOSYSTEM;
283}
284
285static inline bool cec_has_backup(__u16 log_addr_mask)
286{
287 return log_addr_mask & CEC_LOG_ADDR_MASK_BACKUP;
288}
289
290static inline bool cec_has_specific(__u16 log_addr_mask)
291{
292 return log_addr_mask & CEC_LOG_ADDR_MASK_SPECIFIC;
293}
294
295static inline bool cec_is_unregistered(__u16 log_addr_mask)
296{
297 return log_addr_mask & CEC_LOG_ADDR_MASK_UNREGISTERED;
298}
299
300static inline bool cec_is_unconfigured(__u16 log_addr_mask)
301{
302 return log_addr_mask == 0;
303}
304
305/*
306 * Use this if there is no vendor ID (CEC_G_VENDOR_ID) or if the vendor ID
307 * should be disabled (CEC_S_VENDOR_ID)
308 */
309#define CEC_VENDOR_ID_NONE 0xffffffff
310
311/* The message handling modes */
312/* Modes for initiator */
313#define CEC_MODE_NO_INITIATOR (0x0 << 0)
314#define CEC_MODE_INITIATOR (0x1 << 0)
315#define CEC_MODE_EXCL_INITIATOR (0x2 << 0)
316#define CEC_MODE_INITIATOR_MSK 0x0f
317
318/* Modes for follower */
319#define CEC_MODE_NO_FOLLOWER (0x0 << 4)
320#define CEC_MODE_FOLLOWER (0x1 << 4)
321#define CEC_MODE_EXCL_FOLLOWER (0x2 << 4)
322#define CEC_MODE_EXCL_FOLLOWER_PASSTHRU (0x3 << 4)
323#define CEC_MODE_MONITOR (0xe << 4)
324#define CEC_MODE_MONITOR_ALL (0xf << 4)
325#define CEC_MODE_FOLLOWER_MSK 0xf0
326
327/* Userspace has to configure the physical address */
328#define CEC_CAP_PHYS_ADDR (1 << 0)
329/* Userspace has to configure the logical addresses */
330#define CEC_CAP_LOG_ADDRS (1 << 1)
331/* Userspace can transmit messages (and thus become follower as well) */
332#define CEC_CAP_TRANSMIT (1 << 2)
333/*
334 * Passthrough all messages instead of processing them.
335 */
336#define CEC_CAP_PASSTHROUGH (1 << 3)
337/* Supports remote control */
338#define CEC_CAP_RC (1 << 4)
339/* Hardware can monitor all messages, not just directed and broadcast. */
340#define CEC_CAP_MONITOR_ALL (1 << 5)
341
342/**
343 * struct cec_caps - CEC capabilities structure.
344 * @driver: name of the CEC device driver.
345 * @name: name of the CEC device. @driver + @name must be unique.
346 * @available_log_addrs: number of available logical addresses.
347 * @capabilities: capabilities of the CEC adapter.
348 * @version: version of the CEC adapter framework.
349 */
350struct cec_caps {
351 char driver[32];
352 char name[32];
353 __u32 available_log_addrs;
354 __u32 capabilities;
355 __u32 version;
356};
357
358/**
359 * struct cec_log_addrs - CEC logical addresses structure.
360 * @log_addr: the claimed logical addresses. Set by the driver.
361 * @log_addr_mask: current logical address mask. Set by the driver.
362 * @cec_version: the CEC version that the adapter should implement. Set by the
363 * caller.
364 * @num_log_addrs: how many logical addresses should be claimed. Set by the
365 * caller.
366 * @vendor_id: the vendor ID of the device. Set by the caller.
367 * @flags: set to 0.
368 * @osd_name: the OSD name of the device. Set by the caller.
369 * @primary_device_type: the primary device type for each logical address.
370 * Set by the caller.
371 * @log_addr_type: the logical address types. Set by the caller.
372 * @all_device_types: CEC 2.0: all device types represented by the logical
373 * address. Set by the caller.
374 * @features: CEC 2.0: The logical address features. Set by the caller.
375 */
376struct cec_log_addrs {
377 __u8 log_addr[CEC_MAX_LOG_ADDRS];
378 __u16 log_addr_mask;
379 __u8 cec_version;
380 __u8 num_log_addrs;
381 __u32 vendor_id;
382 __u32 flags;
383 char osd_name[15];
384 __u8 primary_device_type[CEC_MAX_LOG_ADDRS];
385 __u8 log_addr_type[CEC_MAX_LOG_ADDRS];
386
387 /* CEC 2.0 */
388 __u8 all_device_types[CEC_MAX_LOG_ADDRS];
389 __u8 features[CEC_MAX_LOG_ADDRS][12];
390};
391
392/* Events */
393
394/* Event that occurs when the adapter state changes */
395#define CEC_EVENT_STATE_CHANGE 1
396/*
397 * This event is sent when messages are lost because the application
398 * didn't empty the message queue in time
399 */
400#define CEC_EVENT_LOST_MSGS 2
401
402#define CEC_EVENT_FL_INITIAL_STATE (1 << 0)
403
404/**
405 * struct cec_event_state_change - used when the CEC adapter changes state.
406 * @phys_addr: the current physical address
407 * @log_addr_mask: the current logical address mask
408 */
409struct cec_event_state_change {
410 __u16 phys_addr;
411 __u16 log_addr_mask;
412};
413
414/**
415 * struct cec_event_lost_msgs - tells you how many messages were lost due.
416 * @lost_msgs: how many messages were lost.
417 */
418struct cec_event_lost_msgs {
419 __u32 lost_msgs;
420};
421
422/**
423 * struct cec_event - CEC event structure
424 * @ts: the timestamp of when the event was sent.
425 * @event: the event.
426 * array.
427 * @state_change: the event payload for CEC_EVENT_STATE_CHANGE.
428 * @lost_msgs: the event payload for CEC_EVENT_LOST_MSGS.
429 * @raw: array to pad the union.
430 */
431struct cec_event {
432 __u64 ts;
433 __u32 event;
434 __u32 flags;
435 union {
436 struct cec_event_state_change state_change;
437 struct cec_event_lost_msgs lost_msgs;
438 __u32 raw[16];
439 };
440};
441
442/* ioctls */
443
444/* Adapter capabilities */
445#define CEC_ADAP_G_CAPS _IOWR('a', 0, struct cec_caps)
446
447/*
448 * phys_addr is either 0 (if this is the CEC root device)
449 * or a valid physical address obtained from the sink's EDID
450 * as read by this CEC device (if this is a source device)
451 * or a physical address obtained and modified from a sink
452 * EDID and used for a sink CEC device.
453 * If nothing is connected, then phys_addr is 0xffff.
454 * See HDMI 1.4b, section 8.7 (Physical Address).
455 *
456 * The CEC_ADAP_S_PHYS_ADDR ioctl may not be available if that is handled
457 * internally.
458 */
459#define CEC_ADAP_G_PHYS_ADDR _IOR('a', 1, __u16)
460#define CEC_ADAP_S_PHYS_ADDR _IOW('a', 2, __u16)
461
462/*
463 * Configure the CEC adapter. It sets the device type and which
464 * logical types it will try to claim. It will return which
465 * logical addresses it could actually claim.
466 * An error is returned if the adapter is disabled or if there
467 * is no physical address assigned.
468 */
469
470#define CEC_ADAP_G_LOG_ADDRS _IOR('a', 3, struct cec_log_addrs)
471#define CEC_ADAP_S_LOG_ADDRS _IOWR('a', 4, struct cec_log_addrs)
472
473/* Transmit/receive a CEC command */
474#define CEC_TRANSMIT _IOWR('a', 5, struct cec_msg)
475#define CEC_RECEIVE _IOWR('a', 6, struct cec_msg)
476
477/* Dequeue CEC events */
478#define CEC_DQEVENT _IOWR('a', 7, struct cec_event)
479
480/*
481 * Get and set the message handling mode for this filehandle.
482 */
483#define CEC_G_MODE _IOR('a', 8, __u32)
484#define CEC_S_MODE _IOW('a', 9, __u32)
485
486/*
487 * The remainder of this header defines all CEC messages and operands.
488 * The format matters since it the cec-ctl utility parses it to generate
489 * code for implementing all these messages.
490 *
491 * Comments ending with 'Feature' group messages for each feature.
492 * If messages are part of multiple features, then the "Has also"
493 * comment is used to list the previously defined messages that are
494 * supported by the feature.
495 *
496 * Before operands are defined a comment is added that gives the
497 * name of the operand and in brackets the variable name of the
498 * corresponding argument in the cec-funcs.h function.
499 */
500
501/* Messages */
502
503/* One Touch Play Feature */
504#define CEC_MSG_ACTIVE_SOURCE 0x82
505#define CEC_MSG_IMAGE_VIEW_ON 0x04
506#define CEC_MSG_TEXT_VIEW_ON 0x0d
507
508
509/* Routing Control Feature */
510
511/*
512 * Has also:
513 * CEC_MSG_ACTIVE_SOURCE
514 */
515
516#define CEC_MSG_INACTIVE_SOURCE 0x9d
517#define CEC_MSG_REQUEST_ACTIVE_SOURCE 0x85
518#define CEC_MSG_ROUTING_CHANGE 0x80
519#define CEC_MSG_ROUTING_INFORMATION 0x81
520#define CEC_MSG_SET_STREAM_PATH 0x86
521
522
523/* Standby Feature */
524#define CEC_MSG_STANDBY 0x36
525
526
527/* One Touch Record Feature */
528#define CEC_MSG_RECORD_OFF 0x0b
529#define CEC_MSG_RECORD_ON 0x09
530/* Record Source Type Operand (rec_src_type) */
531#define CEC_OP_RECORD_SRC_OWN 1
532#define CEC_OP_RECORD_SRC_DIGITAL 2
533#define CEC_OP_RECORD_SRC_ANALOG 3
534#define CEC_OP_RECORD_SRC_EXT_PLUG 4
535#define CEC_OP_RECORD_SRC_EXT_PHYS_ADDR 5
536/* Service Identification Method Operand (service_id_method) */
537#define CEC_OP_SERVICE_ID_METHOD_BY_DIG_ID 0
538#define CEC_OP_SERVICE_ID_METHOD_BY_CHANNEL 1
539/* Digital Service Broadcast System Operand (dig_bcast_system) */
540#define CEC_OP_DIG_SERVICE_BCAST_SYSTEM_ARIB_GEN 0x00
541#define CEC_OP_DIG_SERVICE_BCAST_SYSTEM_ATSC_GEN 0x01
542#define CEC_OP_DIG_SERVICE_BCAST_SYSTEM_DVB_GEN 0x02
543#define CEC_OP_DIG_SERVICE_BCAST_SYSTEM_ARIB_BS 0x08
544#define CEC_OP_DIG_SERVICE_BCAST_SYSTEM_ARIB_CS 0x09
545#define CEC_OP_DIG_SERVICE_BCAST_SYSTEM_ARIB_T 0x0a
546#define CEC_OP_DIG_SERVICE_BCAST_SYSTEM_ATSC_CABLE 0x10
547#define CEC_OP_DIG_SERVICE_BCAST_SYSTEM_ATSC_SAT 0x11
548#define CEC_OP_DIG_SERVICE_BCAST_SYSTEM_ATSC_T 0x12
549#define CEC_OP_DIG_SERVICE_BCAST_SYSTEM_DVB_C 0x18
550#define CEC_OP_DIG_SERVICE_BCAST_SYSTEM_DVB_S 0x19
551#define CEC_OP_DIG_SERVICE_BCAST_SYSTEM_DVB_S2 0x1a
552#define CEC_OP_DIG_SERVICE_BCAST_SYSTEM_DVB_T 0x1b
553/* Analogue Broadcast Type Operand (ana_bcast_type) */
554#define CEC_OP_ANA_BCAST_TYPE_CABLE 0
555#define CEC_OP_ANA_BCAST_TYPE_SATELLITE 1
556#define CEC_OP_ANA_BCAST_TYPE_TERRESTRIAL 2
557/* Broadcast System Operand (bcast_system) */
558#define CEC_OP_BCAST_SYSTEM_PAL_BG 0x00
559#define CEC_OP_BCAST_SYSTEM_SECAM_LQ 0x01 /* SECAM L' */
560#define CEC_OP_BCAST_SYSTEM_PAL_M 0x02
561#define CEC_OP_BCAST_SYSTEM_NTSC_M 0x03
562#define CEC_OP_BCAST_SYSTEM_PAL_I 0x04
563#define CEC_OP_BCAST_SYSTEM_SECAM_DK 0x05
564#define CEC_OP_BCAST_SYSTEM_SECAM_BG 0x06
565#define CEC_OP_BCAST_SYSTEM_SECAM_L 0x07
566#define CEC_OP_BCAST_SYSTEM_PAL_DK 0x08
567#define CEC_OP_BCAST_SYSTEM_OTHER 0x1f
568/* Channel Number Format Operand (channel_number_fmt) */
569#define CEC_OP_CHANNEL_NUMBER_FMT_1_PART 0x01
570#define CEC_OP_CHANNEL_NUMBER_FMT_2_PART 0x02
571
572#define CEC_MSG_RECORD_STATUS 0x0a
573/* Record Status Operand (rec_status) */
574#define CEC_OP_RECORD_STATUS_CUR_SRC 0x01
575#define CEC_OP_RECORD_STATUS_DIG_SERVICE 0x02
576#define CEC_OP_RECORD_STATUS_ANA_SERVICE 0x03
577#define CEC_OP_RECORD_STATUS_EXT_INPUT 0x04
578#define CEC_OP_RECORD_STATUS_NO_DIG_SERVICE 0x05
579#define CEC_OP_RECORD_STATUS_NO_ANA_SERVICE 0x06
580#define CEC_OP_RECORD_STATUS_NO_SERVICE 0x07
581#define CEC_OP_RECORD_STATUS_INVALID_EXT_PLUG 0x09
582#define CEC_OP_RECORD_STATUS_INVALID_EXT_PHYS_ADDR 0x0a
583#define CEC_OP_RECORD_STATUS_UNSUP_CA 0x0b
584#define CEC_OP_RECORD_STATUS_NO_CA_ENTITLEMENTS 0x0c
585#define CEC_OP_RECORD_STATUS_CANT_COPY_SRC 0x0d
586#define CEC_OP_RECORD_STATUS_NO_MORE_COPIES 0x0e
587#define CEC_OP_RECORD_STATUS_NO_MEDIA 0x10
588#define CEC_OP_RECORD_STATUS_PLAYING 0x11
589#define CEC_OP_RECORD_STATUS_ALREADY_RECORDING 0x12
590#define CEC_OP_RECORD_STATUS_MEDIA_PROT 0x13
591#define CEC_OP_RECORD_STATUS_NO_SIGNAL 0x14
592#define CEC_OP_RECORD_STATUS_MEDIA_PROBLEM 0x15
593#define CEC_OP_RECORD_STATUS_NO_SPACE 0x16
594#define CEC_OP_RECORD_STATUS_PARENTAL_LOCK 0x17
595#define CEC_OP_RECORD_STATUS_TERMINATED_OK 0x1a
596#define CEC_OP_RECORD_STATUS_ALREADY_TERM 0x1b
597#define CEC_OP_RECORD_STATUS_OTHER 0x1f
598
599#define CEC_MSG_RECORD_TV_SCREEN 0x0f
600
601
602/* Timer Programming Feature */
603#define CEC_MSG_CLEAR_ANALOGUE_TIMER 0x33
604/* Recording Sequence Operand (recording_seq) */
605#define CEC_OP_REC_SEQ_SUNDAY 0x01
606#define CEC_OP_REC_SEQ_MONDAY 0x02
607#define CEC_OP_REC_SEQ_TUESDAY 0x04
608#define CEC_OP_REC_SEQ_WEDNESDAY 0x08
609#define CEC_OP_REC_SEQ_THURSDAY 0x10
610#define CEC_OP_REC_SEQ_FRIDAY 0x20
611#define CEC_OP_REC_SEQ_SATERDAY 0x40
612#define CEC_OP_REC_SEQ_ONCE_ONLY 0x00
613
614#define CEC_MSG_CLEAR_DIGITAL_TIMER 0x99
615
616#define CEC_MSG_CLEAR_EXT_TIMER 0xa1
617/* External Source Specifier Operand (ext_src_spec) */
618#define CEC_OP_EXT_SRC_PLUG 0x04
619#define CEC_OP_EXT_SRC_PHYS_ADDR 0x05
620
621#define CEC_MSG_SET_ANALOGUE_TIMER 0x34
622#define CEC_MSG_SET_DIGITAL_TIMER 0x97
623#define CEC_MSG_SET_EXT_TIMER 0xa2
624
625#define CEC_MSG_SET_TIMER_PROGRAM_TITLE 0x67
626#define CEC_MSG_TIMER_CLEARED_STATUS 0x43
627/* Timer Cleared Status Data Operand (timer_cleared_status) */
628#define CEC_OP_TIMER_CLR_STAT_RECORDING 0x00
629#define CEC_OP_TIMER_CLR_STAT_NO_MATCHING 0x01
630#define CEC_OP_TIMER_CLR_STAT_NO_INFO 0x02
631#define CEC_OP_TIMER_CLR_STAT_CLEARED 0x80
632
633#define CEC_MSG_TIMER_STATUS 0x35
634/* Timer Overlap Warning Operand (timer_overlap_warning) */
635#define CEC_OP_TIMER_OVERLAP_WARNING_NO_OVERLAP 0
636#define CEC_OP_TIMER_OVERLAP_WARNING_OVERLAP 1
637/* Media Info Operand (media_info) */
638#define CEC_OP_MEDIA_INFO_UNPROT_MEDIA 0
639#define CEC_OP_MEDIA_INFO_PROT_MEDIA 1
640#define CEC_OP_MEDIA_INFO_NO_MEDIA 2
641/* Programmed Indicator Operand (prog_indicator) */
642#define CEC_OP_PROG_IND_NOT_PROGRAMMED 0
643#define CEC_OP_PROG_IND_PROGRAMMED 1
644/* Programmed Info Operand (prog_info) */
645#define CEC_OP_PROG_INFO_ENOUGH_SPACE 0x08
646#define CEC_OP_PROG_INFO_NOT_ENOUGH_SPACE 0x09
647#define CEC_OP_PROG_INFO_MIGHT_NOT_BE_ENOUGH_SPACE 0x0b
648#define CEC_OP_PROG_INFO_NONE_AVAILABLE 0x0a
649/* Not Programmed Error Info Operand (prog_error) */
650#define CEC_OP_PROG_ERROR_NO_FREE_TIMER 0x01
651#define CEC_OP_PROG_ERROR_DATE_OUT_OF_RANGE 0x02
652#define CEC_OP_PROG_ERROR_REC_SEQ_ERROR 0x03
653#define CEC_OP_PROG_ERROR_INV_EXT_PLUG 0x04
654#define CEC_OP_PROG_ERROR_INV_EXT_PHYS_ADDR 0x05
655#define CEC_OP_PROG_ERROR_CA_UNSUPP 0x06
656#define CEC_OP_PROG_ERROR_INSUF_CA_ENTITLEMENTS 0x07
657#define CEC_OP_PROG_ERROR_RESOLUTION_UNSUPP 0x08
658#define CEC_OP_PROG_ERROR_PARENTAL_LOCK 0x09
659#define CEC_OP_PROG_ERROR_CLOCK_FAILURE 0x0a
660#define CEC_OP_PROG_ERROR_DUPLICATE 0x0e
661
662
663/* System Information Feature */
664#define CEC_MSG_CEC_VERSION 0x9e
665/* CEC Version Operand (cec_version) */
666#define CEC_OP_CEC_VERSION_1_3A 4
667#define CEC_OP_CEC_VERSION_1_4 5
668#define CEC_OP_CEC_VERSION_2_0 6
669
670#define CEC_MSG_GET_CEC_VERSION 0x9f
671#define CEC_MSG_GIVE_PHYSICAL_ADDR 0x83
672#define CEC_MSG_GET_MENU_LANGUAGE 0x91
673#define CEC_MSG_REPORT_PHYSICAL_ADDR 0x84
674/* Primary Device Type Operand (prim_devtype) */
675#define CEC_OP_PRIM_DEVTYPE_TV 0
676#define CEC_OP_PRIM_DEVTYPE_RECORD 1
677#define CEC_OP_PRIM_DEVTYPE_TUNER 3
678#define CEC_OP_PRIM_DEVTYPE_PLAYBACK 4
679#define CEC_OP_PRIM_DEVTYPE_AUDIOSYSTEM 5
680#define CEC_OP_PRIM_DEVTYPE_SWITCH 6
681#define CEC_OP_PRIM_DEVTYPE_PROCESSOR 7
682
683#define CEC_MSG_SET_MENU_LANGUAGE 0x32
684#define CEC_MSG_REPORT_FEATURES 0xa6 /* HDMI 2.0 */
685/* All Device Types Operand (all_device_types) */
686#define CEC_OP_ALL_DEVTYPE_TV 0x80
687#define CEC_OP_ALL_DEVTYPE_RECORD 0x40
688#define CEC_OP_ALL_DEVTYPE_TUNER 0x20
689#define CEC_OP_ALL_DEVTYPE_PLAYBACK 0x10
690#define CEC_OP_ALL_DEVTYPE_AUDIOSYSTEM 0x08
691#define CEC_OP_ALL_DEVTYPE_SWITCH 0x04
692/*
693 * And if you wondering what happened to PROCESSOR devices: those should
694 * be mapped to a SWITCH.
695 */
696
697/* Valid for RC Profile and Device Feature operands */
698#define CEC_OP_FEAT_EXT 0x80 /* Extension bit */
699/* RC Profile Operand (rc_profile) */
700#define CEC_OP_FEAT_RC_TV_PROFILE_NONE 0x00
701#define CEC_OP_FEAT_RC_TV_PROFILE_1 0x02
702#define CEC_OP_FEAT_RC_TV_PROFILE_2 0x06
703#define CEC_OP_FEAT_RC_TV_PROFILE_3 0x0a
704#define CEC_OP_FEAT_RC_TV_PROFILE_4 0x0e
705#define CEC_OP_FEAT_RC_SRC_HAS_DEV_ROOT_MENU 0x50
706#define CEC_OP_FEAT_RC_SRC_HAS_DEV_SETUP_MENU 0x48
707#define CEC_OP_FEAT_RC_SRC_HAS_CONTENTS_MENU 0x44
708#define CEC_OP_FEAT_RC_SRC_HAS_MEDIA_TOP_MENU 0x42
709#define CEC_OP_FEAT_RC_SRC_HAS_MEDIA_CONTEXT_MENU 0x41
710/* Device Feature Operand (dev_features) */
711#define CEC_OP_FEAT_DEV_HAS_RECORD_TV_SCREEN 0x40
712#define CEC_OP_FEAT_DEV_HAS_SET_OSD_STRING 0x20
713#define CEC_OP_FEAT_DEV_HAS_DECK_CONTROL 0x10
714#define CEC_OP_FEAT_DEV_HAS_SET_AUDIO_RATE 0x08
715#define CEC_OP_FEAT_DEV_SINK_HAS_ARC_TX 0x04
716#define CEC_OP_FEAT_DEV_SOURCE_HAS_ARC_RX 0x02
717
718#define CEC_MSG_GIVE_FEATURES 0xa5 /* HDMI 2.0 */
719
720
721/* Deck Control Feature */
722#define CEC_MSG_DECK_CONTROL 0x42
723/* Deck Control Mode Operand (deck_control_mode) */
724#define CEC_OP_DECK_CTL_MODE_SKIP_FWD 1
725#define CEC_OP_DECK_CTL_MODE_SKIP_REV 2
726#define CEC_OP_DECK_CTL_MODE_STOP 3
727#define CEC_OP_DECK_CTL_MODE_EJECT 4
728
729#define CEC_MSG_DECK_STATUS 0x1b
730/* Deck Info Operand (deck_info) */
731#define CEC_OP_DECK_INFO_PLAY 0x11
732#define CEC_OP_DECK_INFO_RECORD 0x12
733#define CEC_OP_DECK_INFO_PLAY_REV 0x13
734#define CEC_OP_DECK_INFO_STILL 0x14
735#define CEC_OP_DECK_INFO_SLOW 0x15
736#define CEC_OP_DECK_INFO_SLOW_REV 0x16
737#define CEC_OP_DECK_INFO_FAST_FWD 0x17
738#define CEC_OP_DECK_INFO_FAST_REV 0x18
739#define CEC_OP_DECK_INFO_NO_MEDIA 0x19
740#define CEC_OP_DECK_INFO_STOP 0x1a
741#define CEC_OP_DECK_INFO_SKIP_FWD 0x1b
742#define CEC_OP_DECK_INFO_SKIP_REV 0x1c
743#define CEC_OP_DECK_INFO_INDEX_SEARCH_FWD 0x1d
744#define CEC_OP_DECK_INFO_INDEX_SEARCH_REV 0x1e
745#define CEC_OP_DECK_INFO_OTHER 0x1f
746
747#define CEC_MSG_GIVE_DECK_STATUS 0x1a
748/* Status Request Operand (status_req) */
749#define CEC_OP_STATUS_REQ_ON 1
750#define CEC_OP_STATUS_REQ_OFF 2
751#define CEC_OP_STATUS_REQ_ONCE 3
752
753#define CEC_MSG_PLAY 0x41
754/* Play Mode Operand (play_mode) */
755#define CEC_OP_PLAY_MODE_PLAY_FWD 0x24
756#define CEC_OP_PLAY_MODE_PLAY_REV 0x20
757#define CEC_OP_PLAY_MODE_PLAY_STILL 0x25
758#define CEC_OP_PLAY_MODE_PLAY_FAST_FWD_MIN 0x05
759#define CEC_OP_PLAY_MODE_PLAY_FAST_FWD_MED 0x06
760#define CEC_OP_PLAY_MODE_PLAY_FAST_FWD_MAX 0x07
761#define CEC_OP_PLAY_MODE_PLAY_FAST_REV_MIN 0x09
762#define CEC_OP_PLAY_MODE_PLAY_FAST_REV_MED 0x0a
763#define CEC_OP_PLAY_MODE_PLAY_FAST_REV_MAX 0x0b
764#define CEC_OP_PLAY_MODE_PLAY_SLOW_FWD_MIN 0x15
765#define CEC_OP_PLAY_MODE_PLAY_SLOW_FWD_MED 0x16
766#define CEC_OP_PLAY_MODE_PLAY_SLOW_FWD_MAX 0x17
767#define CEC_OP_PLAY_MODE_PLAY_SLOW_REV_MIN 0x19
768#define CEC_OP_PLAY_MODE_PLAY_SLOW_REV_MED 0x1a
769#define CEC_OP_PLAY_MODE_PLAY_SLOW_REV_MAX 0x1b
770
771
772/* Tuner Control Feature */
773#define CEC_MSG_GIVE_TUNER_DEVICE_STATUS 0x08
774#define CEC_MSG_SELECT_ANALOGUE_SERVICE 0x92
775#define CEC_MSG_SELECT_DIGITAL_SERVICE 0x93
776#define CEC_MSG_TUNER_DEVICE_STATUS 0x07
777/* Recording Flag Operand (rec_flag) */
778#define CEC_OP_REC_FLAG_USED 0
779#define CEC_OP_REC_FLAG_NOT_USED 1
780/* Tuner Display Info Operand (tuner_display_info) */
781#define CEC_OP_TUNER_DISPLAY_INFO_DIGITAL 0
782#define CEC_OP_TUNER_DISPLAY_INFO_NONE 1
783#define CEC_OP_TUNER_DISPLAY_INFO_ANALOGUE 2
784
785#define CEC_MSG_TUNER_STEP_DECREMENT 0x06
786#define CEC_MSG_TUNER_STEP_INCREMENT 0x05
787
788
789/* Vendor Specific Commands Feature */
790
791/*
792 * Has also:
793 * CEC_MSG_CEC_VERSION
794 * CEC_MSG_GET_CEC_VERSION
795 */
796#define CEC_MSG_DEVICE_VENDOR_ID 0x87
797#define CEC_MSG_GIVE_DEVICE_VENDOR_ID 0x8c
798#define CEC_MSG_VENDOR_COMMAND 0x89
799#define CEC_MSG_VENDOR_COMMAND_WITH_ID 0xa0
800#define CEC_MSG_VENDOR_REMOTE_BUTTON_DOWN 0x8a
801#define CEC_MSG_VENDOR_REMOTE_BUTTON_UP 0x8b
802
803
804/* OSD Display Feature */
805#define CEC_MSG_SET_OSD_STRING 0x64
806/* Display Control Operand (disp_ctl) */
807#define CEC_OP_DISP_CTL_DEFAULT 0x00
808#define CEC_OP_DISP_CTL_UNTIL_CLEARED 0x40
809#define CEC_OP_DISP_CTL_CLEAR 0x80
810
811
812/* Device OSD Transfer Feature */
813#define CEC_MSG_GIVE_OSD_NAME 0x46
814#define CEC_MSG_SET_OSD_NAME 0x47
815
816
817/* Device Menu Control Feature */
818#define CEC_MSG_MENU_REQUEST 0x8d
819/* Menu Request Type Operand (menu_req) */
820#define CEC_OP_MENU_REQUEST_ACTIVATE 0x00
821#define CEC_OP_MENU_REQUEST_DEACTIVATE 0x01
822#define CEC_OP_MENU_REQUEST_QUERY 0x02
823
824#define CEC_MSG_MENU_STATUS 0x8e
825/* Menu State Operand (menu_state) */
826#define CEC_OP_MENU_STATE_ACTIVATED 0x00
827#define CEC_OP_MENU_STATE_DEACTIVATED 0x01
828
829#define CEC_MSG_USER_CONTROL_PRESSED 0x44
830/* UI Broadcast Type Operand (ui_bcast_type) */
831#define CEC_OP_UI_BCAST_TYPE_TOGGLE_ALL 0x00
832#define CEC_OP_UI_BCAST_TYPE_TOGGLE_DIG_ANA 0x01
833#define CEC_OP_UI_BCAST_TYPE_ANALOGUE 0x10
834#define CEC_OP_UI_BCAST_TYPE_ANALOGUE_T 0x20
835#define CEC_OP_UI_BCAST_TYPE_ANALOGUE_CABLE 0x30
836#define CEC_OP_UI_BCAST_TYPE_ANALOGUE_SAT 0x40
837#define CEC_OP_UI_BCAST_TYPE_DIGITAL 0x50
838#define CEC_OP_UI_BCAST_TYPE_DIGITAL_T 0x60
839#define CEC_OP_UI_BCAST_TYPE_DIGITAL_CABLE 0x70
840#define CEC_OP_UI_BCAST_TYPE_DIGITAL_SAT 0x80
841#define CEC_OP_UI_BCAST_TYPE_DIGITAL_COM_SAT 0x90
842#define CEC_OP_UI_BCAST_TYPE_DIGITAL_COM_SAT2 0x91
843#define CEC_OP_UI_BCAST_TYPE_IP 0xa0
844/* UI Sound Presentation Control Operand (ui_snd_pres_ctl) */
845#define CEC_OP_UI_SND_PRES_CTL_DUAL_MONO 0x10
846#define CEC_OP_UI_SND_PRES_CTL_KARAOKE 0x20
847#define CEC_OP_UI_SND_PRES_CTL_DOWNMIX 0x80
848#define CEC_OP_UI_SND_PRES_CTL_REVERB 0x90
849#define CEC_OP_UI_SND_PRES_CTL_EQUALIZER 0xa0
850#define CEC_OP_UI_SND_PRES_CTL_BASS_UP 0xb1
851#define CEC_OP_UI_SND_PRES_CTL_BASS_NEUTRAL 0xb2
852#define CEC_OP_UI_SND_PRES_CTL_BASS_DOWN 0xb3
853#define CEC_OP_UI_SND_PRES_CTL_TREBLE_UP 0xc1
854#define CEC_OP_UI_SND_PRES_CTL_TREBLE_NEUTRAL 0xc2
855#define CEC_OP_UI_SND_PRES_CTL_TREBLE_DOWN 0xc3
856
857#define CEC_MSG_USER_CONTROL_RELEASED 0x45
858
859
860/* Remote Control Passthrough Feature */
861
862/*
863 * Has also:
864 * CEC_MSG_USER_CONTROL_PRESSED
865 * CEC_MSG_USER_CONTROL_RELEASED
866 */
867
868
869/* Power Status Feature */
870#define CEC_MSG_GIVE_DEVICE_POWER_STATUS 0x8f
871#define CEC_MSG_REPORT_POWER_STATUS 0x90
872/* Power Status Operand (pwr_state) */
873#define CEC_OP_POWER_STATUS_ON 0
874#define CEC_OP_POWER_STATUS_STANDBY 1
875#define CEC_OP_POWER_STATUS_TO_ON 2
876#define CEC_OP_POWER_STATUS_TO_STANDBY 3
877
878
879/* General Protocol Messages */
880#define CEC_MSG_FEATURE_ABORT 0x00
881/* Abort Reason Operand (reason) */
882#define CEC_OP_ABORT_UNRECOGNIZED_OP 0
883#define CEC_OP_ABORT_INCORRECT_MODE 1
884#define CEC_OP_ABORT_NO_SOURCE 2
885#define CEC_OP_ABORT_INVALID_OP 3
886#define CEC_OP_ABORT_REFUSED 4
887#define CEC_OP_ABORT_UNDETERMINED 5
888
889#define CEC_MSG_ABORT 0xff
890
891
892/* System Audio Control Feature */
893
894/*
895 * Has also:
896 * CEC_MSG_USER_CONTROL_PRESSED
897 * CEC_MSG_USER_CONTROL_RELEASED
898 */
899#define CEC_MSG_GIVE_AUDIO_STATUS 0x71
900#define CEC_MSG_GIVE_SYSTEM_AUDIO_MODE_STATUS 0x7d
901#define CEC_MSG_REPORT_AUDIO_STATUS 0x7a
902/* Audio Mute Status Operand (aud_mute_status) */
903#define CEC_OP_AUD_MUTE_STATUS_OFF 0
904#define CEC_OP_AUD_MUTE_STATUS_ON 1
905
906#define CEC_MSG_REPORT_SHORT_AUDIO_DESCRIPTOR 0xa3
907#define CEC_MSG_REQUEST_SHORT_AUDIO_DESCRIPTOR 0xa4
908#define CEC_MSG_SET_SYSTEM_AUDIO_MODE 0x72
909/* System Audio Status Operand (sys_aud_status) */
910#define CEC_OP_SYS_AUD_STATUS_OFF 0
911#define CEC_OP_SYS_AUD_STATUS_ON 1
912
913#define CEC_MSG_SYSTEM_AUDIO_MODE_REQUEST 0x70
914#define CEC_MSG_SYSTEM_AUDIO_MODE_STATUS 0x7e
915/* Audio Format ID Operand (audio_format_id) */
916#define CEC_OP_AUD_FMT_ID_CEA861 0
917#define CEC_OP_AUD_FMT_ID_CEA861_CXT 1
918
919
920/* Audio Rate Control Feature */
921#define CEC_MSG_SET_AUDIO_RATE 0x9a
922/* Audio Rate Operand (audio_rate) */
923#define CEC_OP_AUD_RATE_OFF 0
924#define CEC_OP_AUD_RATE_WIDE_STD 1
925#define CEC_OP_AUD_RATE_WIDE_FAST 2
926#define CEC_OP_AUD_RATE_WIDE_SLOW 3
927#define CEC_OP_AUD_RATE_NARROW_STD 4
928#define CEC_OP_AUD_RATE_NARROW_FAST 5
929#define CEC_OP_AUD_RATE_NARROW_SLOW 6
930
931
932/* Audio Return Channel Control Feature */
933#define CEC_MSG_INITIATE_ARC 0xc0
934#define CEC_MSG_REPORT_ARC_INITIATED 0xc1
935#define CEC_MSG_REPORT_ARC_TERMINATED 0xc2
936#define CEC_MSG_REQUEST_ARC_INITIATION 0xc3
937#define CEC_MSG_REQUEST_ARC_TERMINATION 0xc4
938#define CEC_MSG_TERMINATE_ARC 0xc5
939
940
941/* Dynamic Audio Lipsync Feature */
942/* Only for CEC 2.0 and up */
943#define CEC_MSG_REQUEST_CURRENT_LATENCY 0xa7
944#define CEC_MSG_REPORT_CURRENT_LATENCY 0xa8
945/* Low Latency Mode Operand (low_latency_mode) */
946#define CEC_OP_LOW_LATENCY_MODE_OFF 0
947#define CEC_OP_LOW_LATENCY_MODE_ON 1
948/* Audio Output Compensated Operand (audio_out_compensated) */
949#define CEC_OP_AUD_OUT_COMPENSATED_NA 0
950#define CEC_OP_AUD_OUT_COMPENSATED_DELAY 1
951#define CEC_OP_AUD_OUT_COMPENSATED_NO_DELAY 2
952#define CEC_OP_AUD_OUT_COMPENSATED_PARTIAL_DELAY 3
953
954
955/* Capability Discovery and Control Feature */
956#define CEC_MSG_CDC_MESSAGE 0xf8
957/* Ethernet-over-HDMI: nobody ever does this... */
958#define CEC_MSG_CDC_HEC_INQUIRE_STATE 0x00
959#define CEC_MSG_CDC_HEC_REPORT_STATE 0x01
960/* HEC Functionality State Operand (hec_func_state) */
961#define CEC_OP_HEC_FUNC_STATE_NOT_SUPPORTED 0
962#define CEC_OP_HEC_FUNC_STATE_INACTIVE 1
963#define CEC_OP_HEC_FUNC_STATE_ACTIVE 2
964#define CEC_OP_HEC_FUNC_STATE_ACTIVATION_FIELD 3
965/* Host Functionality State Operand (host_func_state) */
966#define CEC_OP_HOST_FUNC_STATE_NOT_SUPPORTED 0
967#define CEC_OP_HOST_FUNC_STATE_INACTIVE 1
968#define CEC_OP_HOST_FUNC_STATE_ACTIVE 2
969/* ENC Functionality State Operand (enc_func_state) */
970#define CEC_OP_ENC_FUNC_STATE_EXT_CON_NOT_SUPPORTED 0
971#define CEC_OP_ENC_FUNC_STATE_EXT_CON_INACTIVE 1
972#define CEC_OP_ENC_FUNC_STATE_EXT_CON_ACTIVE 2
973/* CDC Error Code Operand (cdc_errcode) */
974#define CEC_OP_CDC_ERROR_CODE_NONE 0
975#define CEC_OP_CDC_ERROR_CODE_CAP_UNSUPPORTED 1
976#define CEC_OP_CDC_ERROR_CODE_WRONG_STATE 2
977#define CEC_OP_CDC_ERROR_CODE_OTHER 3
978/* HEC Support Operand (hec_support) */
979#define CEC_OP_HEC_SUPPORT_NO 0
980#define CEC_OP_HEC_SUPPORT_YES 1
981/* HEC Activation Operand (hec_activation) */
982#define CEC_OP_HEC_ACTIVATION_ON 0
983#define CEC_OP_HEC_ACTIVATION_OFF 1
984
985#define CEC_MSG_CDC_HEC_SET_STATE_ADJACENT 0x02
986#define CEC_MSG_CDC_HEC_SET_STATE 0x03
987/* HEC Set State Operand (hec_set_state) */
988#define CEC_OP_HEC_SET_STATE_DEACTIVATE 0
989#define CEC_OP_HEC_SET_STATE_ACTIVATE 1
990
991#define CEC_MSG_CDC_HEC_REQUEST_DEACTIVATION 0x04
992#define CEC_MSG_CDC_HEC_NOTIFY_ALIVE 0x05
993#define CEC_MSG_CDC_HEC_DISCOVER 0x06
994/* Hotplug Detect messages */
995#define CEC_MSG_CDC_HPD_SET_STATE 0x10
996/* HPD State Operand (hpd_state) */
997#define CEC_OP_HPD_STATE_CP_EDID_DISABLE 0
998#define CEC_OP_HPD_STATE_CP_EDID_ENABLE 1
999#define CEC_OP_HPD_STATE_CP_EDID_DISABLE_ENABLE 2
1000#define CEC_OP_HPD_STATE_EDID_DISABLE 3
1001#define CEC_OP_HPD_STATE_EDID_ENABLE 4
1002#define CEC_OP_HPD_STATE_EDID_DISABLE_ENABLE 5
1003#define CEC_MSG_CDC_HPD_REPORT_STATE 0x11
1004/* HPD Error Code Operand (hpd_error) */
1005#define CEC_OP_HPD_ERROR_NONE 0
1006#define CEC_OP_HPD_ERROR_INITIATOR_NOT_CAPABLE 1
1007#define CEC_OP_HPD_ERROR_INITIATOR_WRONG_STATE 2
1008#define CEC_OP_HPD_ERROR_OTHER 3
1009#define CEC_OP_HPD_ERROR_NONE_NO_VIDEO 4
1010
1011#endif
diff --git a/include/linux/of_reserved_mem.h b/include/linux/of_reserved_mem.h
index c201060e0c6d..f8e1992d6423 100644
--- a/include/linux/of_reserved_mem.h
+++ b/include/linux/of_reserved_mem.h
@@ -1,7 +1,8 @@
1#ifndef __OF_RESERVED_MEM_H 1#ifndef __OF_RESERVED_MEM_H
2#define __OF_RESERVED_MEM_H 2#define __OF_RESERVED_MEM_H
3 3
4struct device; 4#include <linux/device.h>
5
5struct of_phandle_args; 6struct of_phandle_args;
6struct reserved_mem_ops; 7struct reserved_mem_ops;
7 8
@@ -28,7 +29,9 @@ typedef int (*reservedmem_of_init_fn)(struct reserved_mem *rmem);
28 _OF_DECLARE(reservedmem, name, compat, init, reservedmem_of_init_fn) 29 _OF_DECLARE(reservedmem, name, compat, init, reservedmem_of_init_fn)
29 30
30#ifdef CONFIG_OF_RESERVED_MEM 31#ifdef CONFIG_OF_RESERVED_MEM
31int of_reserved_mem_device_init(struct device *dev); 32
33int of_reserved_mem_device_init_by_idx(struct device *dev,
34 struct device_node *np, int idx);
32void of_reserved_mem_device_release(struct device *dev); 35void of_reserved_mem_device_release(struct device *dev);
33 36
34int early_init_dt_alloc_reserved_memory_arch(phys_addr_t size, 37int early_init_dt_alloc_reserved_memory_arch(phys_addr_t size,
@@ -42,7 +45,8 @@ void fdt_init_reserved_mem(void);
42void fdt_reserved_mem_save_node(unsigned long node, const char *uname, 45void fdt_reserved_mem_save_node(unsigned long node, const char *uname,
43 phys_addr_t base, phys_addr_t size); 46 phys_addr_t base, phys_addr_t size);
44#else 47#else
45static inline int of_reserved_mem_device_init(struct device *dev) 48static inline int of_reserved_mem_device_init_by_idx(struct device *dev,
49 struct device_node *np, int idx)
46{ 50{
47 return -ENOSYS; 51 return -ENOSYS;
48} 52}
@@ -53,4 +57,19 @@ static inline void fdt_reserved_mem_save_node(unsigned long node,
53 const char *uname, phys_addr_t base, phys_addr_t size) { } 57 const char *uname, phys_addr_t base, phys_addr_t size) { }
54#endif 58#endif
55 59
60/**
61 * of_reserved_mem_device_init() - assign reserved memory region to given device
62 * @dev: Pointer to the device to configure
63 *
64 * This function assigns respective DMA-mapping operations based on the first
65 * reserved memory region specified by 'memory-region' property in device tree
66 * node of the given device.
67 *
68 * Returns error code or zero on success.
69 */
70static inline int of_reserved_mem_device_init(struct device *dev)
71{
72 return of_reserved_mem_device_init_by_idx(dev, dev->of_node, 0);
73}
74
56#endif /* __OF_RESERVED_MEM_H */ 75#endif /* __OF_RESERVED_MEM_H */
diff --git a/include/media/cec-edid.h b/include/media/cec-edid.h
new file mode 100644
index 000000000000..bdf731ecba1a
--- /dev/null
+++ b/include/media/cec-edid.h
@@ -0,0 +1,104 @@
1/*
2 * cec-edid - HDMI Consumer Electronics Control & EDID helpers
3 *
4 * Copyright 2016 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
5 *
6 * This program is free software; you may redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
11 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
12 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
13 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
14 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
15 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
16 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17 * SOFTWARE.
18 */
19
20#ifndef _MEDIA_CEC_EDID_H
21#define _MEDIA_CEC_EDID_H
22
23#include <linux/types.h>
24
25#define CEC_PHYS_ADDR_INVALID 0xffff
26#define cec_phys_addr_exp(pa) \
27 ((pa) >> 12), ((pa) >> 8) & 0xf, ((pa) >> 4) & 0xf, (pa) & 0xf
28
29/**
30 * cec_get_edid_phys_addr() - find and return the physical address
31 *
32 * @edid: pointer to the EDID data
33 * @size: size in bytes of the EDID data
34 * @offset: If not %NULL then the location of the physical address
35 * bytes in the EDID will be returned here. This is set to 0
36 * if there is no physical address found.
37 *
38 * Return: the physical address or CEC_PHYS_ADDR_INVALID if there is none.
39 */
40u16 cec_get_edid_phys_addr(const u8 *edid, unsigned int size,
41 unsigned int *offset);
42
43/**
44 * cec_set_edid_phys_addr() - find and set the physical address
45 *
46 * @edid: pointer to the EDID data
47 * @size: size in bytes of the EDID data
48 * @phys_addr: the new physical address
49 *
50 * This function finds the location of the physical address in the EDID
51 * and fills in the given physical address and updates the checksum
52 * at the end of the EDID block. It does nothing if the EDID doesn't
53 * contain a physical address.
54 */
55void cec_set_edid_phys_addr(u8 *edid, unsigned int size, u16 phys_addr);
56
57/**
58 * cec_phys_addr_for_input() - calculate the PA for an input
59 *
60 * @phys_addr: the physical address of the parent
61 * @input: the number of the input port, must be between 1 and 15
62 *
63 * This function calculates a new physical address based on the input
64 * port number. For example:
65 *
66 * PA = 0.0.0.0 and input = 2 becomes 2.0.0.0
67 *
68 * PA = 3.0.0.0 and input = 1 becomes 3.1.0.0
69 *
70 * PA = 3.2.1.0 and input = 5 becomes 3.2.1.5
71 *
72 * PA = 3.2.1.3 and input = 5 becomes f.f.f.f since it maxed out the depth.
73 *
74 * Return: the new physical address or CEC_PHYS_ADDR_INVALID.
75 */
76u16 cec_phys_addr_for_input(u16 phys_addr, u8 input);
77
78/**
79 * cec_phys_addr_validate() - validate a physical address from an EDID
80 *
81 * @phys_addr: the physical address to validate
82 * @parent: if not %NULL, then this is filled with the parents PA.
83 * @port: if not %NULL, then this is filled with the input port.
84 *
85 * This validates a physical address as read from an EDID. If the
86 * PA is invalid (such as 1.0.1.0 since '0' is only allowed at the end),
87 * then it will return -EINVAL.
88 *
89 * The parent PA is passed into %parent and the input port is passed into
90 * %port. For example:
91 *
92 * PA = 0.0.0.0: has parent 0.0.0.0 and input port 0.
93 *
94 * PA = 1.0.0.0: has parent 0.0.0.0 and input port 1.
95 *
96 * PA = 3.2.0.0: has parent 3.0.0.0 and input port 2.
97 *
98 * PA = f.f.f.f: has parent f.f.f.f and input port 0.
99 *
100 * Return: 0 if the PA is valid, -EINVAL if not.
101 */
102int cec_phys_addr_validate(u16 phys_addr, u16 *parent, u16 *port);
103
104#endif /* _MEDIA_CEC_EDID_H */
diff --git a/include/media/cec.h b/include/media/cec.h
new file mode 100644
index 000000000000..dc7854b855f3
--- /dev/null
+++ b/include/media/cec.h
@@ -0,0 +1,241 @@
1/*
2 * cec - HDMI Consumer Electronics Control support header
3 *
4 * Copyright 2016 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
5 *
6 * This program is free software; you may redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
11 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
12 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
13 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
14 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
15 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
16 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17 * SOFTWARE.
18 */
19
20#ifndef _MEDIA_CEC_H
21#define _MEDIA_CEC_H
22
23#include <linux/poll.h>
24#include <linux/fs.h>
25#include <linux/debugfs.h>
26#include <linux/device.h>
27#include <linux/cdev.h>
28#include <linux/kthread.h>
29#include <linux/timer.h>
30#include <linux/cec-funcs.h>
31#include <media/rc-core.h>
32#include <media/cec-edid.h>
33
34/**
35 * struct cec_devnode - cec device node
36 * @dev: cec device
37 * @cdev: cec character device
38 * @parent: parent device
39 * @minor: device node minor number
40 * @registered: the device was correctly registered
41 * @unregistered: the device was unregistered
42 * @fhs_lock: lock to control access to the filehandle list
43 * @fhs: the list of open filehandles (cec_fh)
44 *
45 * This structure represents a cec-related device node.
46 *
47 * The @parent is a physical device. It must be set by core or device drivers
48 * before registering the node.
49 */
50struct cec_devnode {
51 /* sysfs */
52 struct device dev;
53 struct cdev cdev;
54 struct device *parent;
55
56 /* device info */
57 int minor;
58 bool registered;
59 bool unregistered;
60 struct mutex fhs_lock;
61 struct list_head fhs;
62};
63
64struct cec_adapter;
65struct cec_data;
66
67struct cec_data {
68 struct list_head list;
69 struct list_head xfer_list;
70 struct cec_adapter *adap;
71 struct cec_msg msg;
72 struct cec_fh *fh;
73 struct delayed_work work;
74 struct completion c;
75 u8 attempts;
76 bool new_initiator;
77 bool blocking;
78 bool completed;
79};
80
81struct cec_msg_entry {
82 struct list_head list;
83 struct cec_msg msg;
84};
85
86#define CEC_NUM_EVENTS CEC_EVENT_LOST_MSGS
87
88struct cec_fh {
89 struct list_head list;
90 struct list_head xfer_list;
91 struct cec_adapter *adap;
92 u8 mode_initiator;
93 u8 mode_follower;
94
95 /* Events */
96 wait_queue_head_t wait;
97 unsigned int pending_events;
98 struct cec_event events[CEC_NUM_EVENTS];
99 struct mutex lock;
100 struct list_head msgs; /* queued messages */
101 unsigned int queued_msgs;
102};
103
104#define CEC_SIGNAL_FREE_TIME_RETRY 3
105#define CEC_SIGNAL_FREE_TIME_NEW_INITIATOR 5
106#define CEC_SIGNAL_FREE_TIME_NEXT_XFER 7
107
108/* The nominal data bit period is 2.4 ms */
109#define CEC_FREE_TIME_TO_USEC(ft) ((ft) * 2400)
110
111struct cec_adap_ops {
112 /* Low-level callbacks */
113 int (*adap_enable)(struct cec_adapter *adap, bool enable);
114 int (*adap_monitor_all_enable)(struct cec_adapter *adap, bool enable);
115 int (*adap_log_addr)(struct cec_adapter *adap, u8 logical_addr);
116 int (*adap_transmit)(struct cec_adapter *adap, u8 attempts,
117 u32 signal_free_time, struct cec_msg *msg);
118 void (*adap_status)(struct cec_adapter *adap, struct seq_file *file);
119
120 /* High-level CEC message callback */
121 int (*received)(struct cec_adapter *adap, struct cec_msg *msg);
122};
123
124/*
125 * The minimum message length you can receive (excepting poll messages) is 2.
126 * With a transfer rate of at most 36 bytes per second this makes 18 messages
127 * per second worst case.
128 *
129 * We queue at most 3 seconds worth of received messages. The CEC specification
130 * requires that messages are replied to within a second, so 3 seconds should
131 * give more than enough margin. Since most messages are actually more than 2
132 * bytes, this is in practice a lot more than 3 seconds.
133 */
134#define CEC_MAX_MSG_RX_QUEUE_SZ (18 * 3)
135
136/*
137 * The transmit queue is limited to 1 second worth of messages (worst case).
138 * Messages can be transmitted by userspace and kernel space. But for both it
139 * makes no sense to have a lot of messages queued up. One second seems
140 * reasonable.
141 */
142#define CEC_MAX_MSG_TX_QUEUE_SZ (18 * 1)
143
144struct cec_adapter {
145 struct module *owner;
146 char name[32];
147 struct cec_devnode devnode;
148 struct mutex lock;
149 struct rc_dev *rc;
150
151 struct list_head transmit_queue;
152 unsigned int transmit_queue_sz;
153 struct list_head wait_queue;
154 struct cec_data *transmitting;
155
156 struct task_struct *kthread_config;
157 struct completion config_completion;
158
159 struct task_struct *kthread;
160 wait_queue_head_t kthread_waitq;
161 wait_queue_head_t waitq;
162
163 const struct cec_adap_ops *ops;
164 void *priv;
165 u32 capabilities;
166 u8 available_log_addrs;
167
168 u16 phys_addr;
169 bool is_configuring;
170 bool is_configured;
171 u32 monitor_all_cnt;
172 u32 follower_cnt;
173 struct cec_fh *cec_follower;
174 struct cec_fh *cec_initiator;
175 bool passthrough;
176 struct cec_log_addrs log_addrs;
177
178 struct dentry *cec_dir;
179 struct dentry *status_file;
180
181 u16 phys_addrs[15];
182 u32 sequence;
183
184 char input_name[32];
185 char input_phys[32];
186 char input_drv[32];
187};
188
189static inline bool cec_has_log_addr(const struct cec_adapter *adap, u8 log_addr)
190{
191 return adap->log_addrs.log_addr_mask & (1 << log_addr);
192}
193
194static inline bool cec_is_sink(const struct cec_adapter *adap)
195{
196 return adap->phys_addr == 0;
197}
198
199#if IS_ENABLED(CONFIG_MEDIA_CEC)
200struct cec_adapter *cec_allocate_adapter(const struct cec_adap_ops *ops,
201 void *priv, const char *name, u32 caps, u8 available_las,
202 struct device *parent);
203int cec_register_adapter(struct cec_adapter *adap);
204void cec_unregister_adapter(struct cec_adapter *adap);
205void cec_delete_adapter(struct cec_adapter *adap);
206
207int cec_s_log_addrs(struct cec_adapter *adap, struct cec_log_addrs *log_addrs,
208 bool block);
209void cec_s_phys_addr(struct cec_adapter *adap, u16 phys_addr,
210 bool block);
211int cec_transmit_msg(struct cec_adapter *adap, struct cec_msg *msg,
212 bool block);
213
214/* Called by the adapter */
215void cec_transmit_done(struct cec_adapter *adap, u8 status, u8 arb_lost_cnt,
216 u8 nack_cnt, u8 low_drive_cnt, u8 error_cnt);
217void cec_received_msg(struct cec_adapter *adap, struct cec_msg *msg);
218
219#else
220
221static inline int cec_register_adapter(struct cec_adapter *adap)
222{
223 return 0;
224}
225
226static inline void cec_unregister_adapter(struct cec_adapter *adap)
227{
228}
229
230static inline void cec_delete_adapter(struct cec_adapter *adap)
231{
232}
233
234static inline void cec_s_phys_addr(struct cec_adapter *adap, u16 phys_addr,
235 bool block)
236{
237}
238
239#endif
240
241#endif /* _MEDIA_CEC_H */
diff --git a/include/media/davinci/vpbe_display.h b/include/media/davinci/vpbe_display.h
index e14a9370b67e..12783fd823f8 100644
--- a/include/media/davinci/vpbe_display.h
+++ b/include/media/davinci/vpbe_display.h
@@ -81,8 +81,6 @@ struct vpbe_layer {
81 * Buffer queue used in video-buf 81 * Buffer queue used in video-buf
82 */ 82 */
83 struct vb2_queue buffer_queue; 83 struct vb2_queue buffer_queue;
84 /* allocator-specific contexts for each plane */
85 struct vb2_alloc_ctx *alloc_ctx;
86 /* Queue of filled frames */ 84 /* Queue of filled frames */
87 struct list_head dma_queue; 85 struct list_head dma_queue;
88 /* Used in video-buf */ 86 /* Used in video-buf */
diff --git a/include/media/i2c/adv7511.h b/include/media/i2c/adv7511.h
index d83b91d80764..61c3d711cc69 100644
--- a/include/media/i2c/adv7511.h
+++ b/include/media/i2c/adv7511.h
@@ -32,11 +32,7 @@ struct adv7511_monitor_detect {
32struct adv7511_edid_detect { 32struct adv7511_edid_detect {
33 int present; 33 int present;
34 int segment; 34 int segment;
35}; 35 uint16_t phys_addr;
36
37struct adv7511_cec_arg {
38 void *arg;
39 u32 f_flags;
40}; 36};
41 37
42struct adv7511_platform_data { 38struct adv7511_platform_data {
diff --git a/include/media/i2c/adv7604.h b/include/media/i2c/adv7604.h
index a913859bfd30..2e6857dee0cc 100644
--- a/include/media/i2c/adv7604.h
+++ b/include/media/i2c/adv7604.h
@@ -121,8 +121,6 @@ struct adv76xx_platform_data {
121 121
122 /* IO register 0x02 */ 122 /* IO register 0x02 */
123 unsigned alt_gamma:1; 123 unsigned alt_gamma:1;
124 unsigned op_656_range:1;
125 unsigned alt_data_sat:1;
126 124
127 /* IO register 0x05 */ 125 /* IO register 0x05 */
128 unsigned blank_data:1; 126 unsigned blank_data:1;
diff --git a/include/media/i2c/adv7842.h b/include/media/i2c/adv7842.h
index bc249709bf35..7f53ada9bdf1 100644
--- a/include/media/i2c/adv7842.h
+++ b/include/media/i2c/adv7842.h
@@ -165,8 +165,6 @@ struct adv7842_platform_data {
165 165
166 /* IO register 0x02 */ 166 /* IO register 0x02 */
167 unsigned alt_gamma:1; 167 unsigned alt_gamma:1;
168 unsigned op_656_range:1;
169 unsigned alt_data_sat:1;
170 168
171 /* IO register 0x05 */ 169 /* IO register 0x05 */
172 unsigned blank_data:1; 170 unsigned blank_data:1;
diff --git a/include/media/media-device.h b/include/media/media-device.h
index a9b33c47310d..f743ae2210ee 100644
--- a/include/media/media-device.h
+++ b/include/media/media-device.h
@@ -347,7 +347,7 @@ struct media_entity_notify {
347struct media_device { 347struct media_device {
348 /* dev->driver_data points to this struct. */ 348 /* dev->driver_data points to this struct. */
349 struct device *dev; 349 struct device *dev;
350 struct media_devnode devnode; 350 struct media_devnode *devnode;
351 351
352 char model[32]; 352 char model[32];
353 char driver_name[32]; 353 char driver_name[32];
@@ -393,9 +393,6 @@ struct usb_device;
393#define MEDIA_DEV_NOTIFY_PRE_LINK_CH 0 393#define MEDIA_DEV_NOTIFY_PRE_LINK_CH 0
394#define MEDIA_DEV_NOTIFY_POST_LINK_CH 1 394#define MEDIA_DEV_NOTIFY_POST_LINK_CH 1
395 395
396/* media_devnode to media_device */
397#define to_media_device(node) container_of(node, struct media_device, devnode)
398
399/** 396/**
400 * media_entity_enum_init - Initialise an entity enumeration 397 * media_entity_enum_init - Initialise an entity enumeration
401 * 398 *
diff --git a/include/media/media-devnode.h b/include/media/media-devnode.h
index fe42f08e72bd..37d494805944 100644
--- a/include/media/media-devnode.h
+++ b/include/media/media-devnode.h
@@ -33,6 +33,8 @@
33#include <linux/device.h> 33#include <linux/device.h>
34#include <linux/cdev.h> 34#include <linux/cdev.h>
35 35
36struct media_device;
37
36/* 38/*
37 * Flag to mark the media_devnode struct as registered. Drivers must not touch 39 * Flag to mark the media_devnode struct as registered. Drivers must not touch
38 * this flag directly, it will be set and cleared by media_devnode_register and 40 * this flag directly, it will be set and cleared by media_devnode_register and
@@ -67,8 +69,9 @@ struct media_file_operations {
67 69
68/** 70/**
69 * struct media_devnode - Media device node 71 * struct media_devnode - Media device node
72 * @media_dev: pointer to struct &media_device
70 * @fops: pointer to struct &media_file_operations with media device ops 73 * @fops: pointer to struct &media_file_operations with media device ops
71 * @dev: struct device pointer for the media controller device 74 * @dev: pointer to struct &device containing the media controller device
72 * @cdev: struct cdev pointer character device 75 * @cdev: struct cdev pointer character device
73 * @parent: parent device 76 * @parent: parent device
74 * @minor: device node minor number 77 * @minor: device node minor number
@@ -81,6 +84,8 @@ struct media_file_operations {
81 * before registering the node. 84 * before registering the node.
82 */ 85 */
83struct media_devnode { 86struct media_devnode {
87 struct media_device *media_dev;
88
84 /* device ops */ 89 /* device ops */
85 const struct media_file_operations *fops; 90 const struct media_file_operations *fops;
86 91
@@ -94,7 +99,7 @@ struct media_devnode {
94 unsigned long flags; /* Use bitops to access flags */ 99 unsigned long flags; /* Use bitops to access flags */
95 100
96 /* callbacks */ 101 /* callbacks */
97 void (*release)(struct media_devnode *mdev); 102 void (*release)(struct media_devnode *devnode);
98}; 103};
99 104
100/* dev to media_devnode */ 105/* dev to media_devnode */
@@ -103,7 +108,8 @@ struct media_devnode {
103/** 108/**
104 * media_devnode_register - register a media device node 109 * media_devnode_register - register a media device node
105 * 110 *
106 * @mdev: media device node structure we want to register 111 * @mdev: struct media_device we want to register a device node
112 * @devnode: media device node structure we want to register
107 * @owner: should be filled with %THIS_MODULE 113 * @owner: should be filled with %THIS_MODULE
108 * 114 *
109 * The registration code assigns minor numbers and registers the new device node 115 * The registration code assigns minor numbers and registers the new device node
@@ -116,20 +122,33 @@ struct media_devnode {
116 * the media_devnode structure is *not* called, so the caller is responsible for 122 * the media_devnode structure is *not* called, so the caller is responsible for
117 * freeing any data. 123 * freeing any data.
118 */ 124 */
119int __must_check media_devnode_register(struct media_devnode *mdev, 125int __must_check media_devnode_register(struct media_device *mdev,
126 struct media_devnode *devnode,
120 struct module *owner); 127 struct module *owner);
121 128
122/** 129/**
130 * media_devnode_unregister_prepare - clear the media device node register bit
131 * @devnode: the device node to prepare for unregister
132 *
133 * This clears the passed device register bit. Future open calls will be met
134 * with errors. Should be called before media_devnode_unregister() to avoid
135 * races with unregister and device file open calls.
136 *
137 * This function can safely be called if the device node has never been
138 * registered or has already been unregistered.
139 */
140void media_devnode_unregister_prepare(struct media_devnode *devnode);
141
142/**
123 * media_devnode_unregister - unregister a media device node 143 * media_devnode_unregister - unregister a media device node
124 * @mdev: the device node to unregister 144 * @devnode: the device node to unregister
125 * 145 *
126 * This unregisters the passed device. Future open calls will be met with 146 * This unregisters the passed device. Future open calls will be met with
127 * errors. 147 * errors.
128 * 148 *
129 * This function can safely be called if the device node has never been 149 * Should be called after media_devnode_unregister_prepare()
130 * registered or has already been unregistered.
131 */ 150 */
132void media_devnode_unregister(struct media_devnode *mdev); 151void media_devnode_unregister(struct media_devnode *devnode);
133 152
134/** 153/**
135 * media_devnode_data - returns a pointer to the &media_devnode 154 * media_devnode_data - returns a pointer to the &media_devnode
@@ -145,11 +164,16 @@ static inline struct media_devnode *media_devnode_data(struct file *filp)
145 * media_devnode_is_registered - returns true if &media_devnode is registered; 164 * media_devnode_is_registered - returns true if &media_devnode is registered;
146 * false otherwise. 165 * false otherwise.
147 * 166 *
148 * @mdev: pointer to struct &media_devnode. 167 * @devnode: pointer to struct &media_devnode.
168 *
169 * Note: If mdev is NULL, it also returns false.
149 */ 170 */
150static inline int media_devnode_is_registered(struct media_devnode *mdev) 171static inline int media_devnode_is_registered(struct media_devnode *devnode)
151{ 172{
152 return test_bit(MEDIA_FLAG_REGISTERED, &mdev->flags); 173 if (!devnode)
174 return false;
175
176 return test_bit(MEDIA_FLAG_REGISTERED, &devnode->flags);
153} 177}
154 178
155#endif /* _MEDIA_DEVNODE_H */ 179#endif /* _MEDIA_DEVNODE_H */
diff --git a/include/media/rc-core.h b/include/media/rc-core.h
index b6586a91129c..324232cfc08d 100644
--- a/include/media/rc-core.h
+++ b/include/media/rc-core.h
@@ -119,6 +119,7 @@ enum rc_filter_type {
119 * @s_carrier_report: enable carrier reports 119 * @s_carrier_report: enable carrier reports
120 * @s_filter: set the scancode filter 120 * @s_filter: set the scancode filter
121 * @s_wakeup_filter: set the wakeup scancode filter 121 * @s_wakeup_filter: set the wakeup scancode filter
122 * @s_timeout: set hardware timeout in ns
122 */ 123 */
123struct rc_dev { 124struct rc_dev {
124 struct device dev; 125 struct device dev;
@@ -174,6 +175,8 @@ struct rc_dev {
174 struct rc_scancode_filter *filter); 175 struct rc_scancode_filter *filter);
175 int (*s_wakeup_filter)(struct rc_dev *dev, 176 int (*s_wakeup_filter)(struct rc_dev *dev,
176 struct rc_scancode_filter *filter); 177 struct rc_scancode_filter *filter);
178 int (*s_timeout)(struct rc_dev *dev,
179 unsigned int timeout);
177}; 180};
178 181
179#define to_rc_dev(d) container_of(d, struct rc_dev, dev) 182#define to_rc_dev(d) container_of(d, struct rc_dev, dev)
diff --git a/include/media/rc-map.h b/include/media/rc-map.h
index 7844e9879497..a459129dd554 100644
--- a/include/media/rc-map.h
+++ b/include/media/rc-map.h
@@ -31,6 +31,7 @@ enum rc_type {
31 RC_TYPE_RC6_MCE = 16, /* MCE (Philips RC6-6A-32 subtype) protocol */ 31 RC_TYPE_RC6_MCE = 16, /* MCE (Philips RC6-6A-32 subtype) protocol */
32 RC_TYPE_SHARP = 17, /* Sharp protocol */ 32 RC_TYPE_SHARP = 17, /* Sharp protocol */
33 RC_TYPE_XMP = 18, /* XMP protocol */ 33 RC_TYPE_XMP = 18, /* XMP protocol */
34 RC_TYPE_CEC = 19, /* CEC protocol */
34}; 35};
35 36
36#define RC_BIT_NONE 0ULL 37#define RC_BIT_NONE 0ULL
@@ -53,6 +54,7 @@ enum rc_type {
53#define RC_BIT_RC6_MCE (1ULL << RC_TYPE_RC6_MCE) 54#define RC_BIT_RC6_MCE (1ULL << RC_TYPE_RC6_MCE)
54#define RC_BIT_SHARP (1ULL << RC_TYPE_SHARP) 55#define RC_BIT_SHARP (1ULL << RC_TYPE_SHARP)
55#define RC_BIT_XMP (1ULL << RC_TYPE_XMP) 56#define RC_BIT_XMP (1ULL << RC_TYPE_XMP)
57#define RC_BIT_CEC (1ULL << RC_TYPE_CEC)
56 58
57#define RC_BIT_ALL (RC_BIT_UNKNOWN | RC_BIT_OTHER | \ 59#define RC_BIT_ALL (RC_BIT_UNKNOWN | RC_BIT_OTHER | \
58 RC_BIT_RC5 | RC_BIT_RC5X | RC_BIT_RC5_SZ | \ 60 RC_BIT_RC5 | RC_BIT_RC5X | RC_BIT_RC5_SZ | \
@@ -61,7 +63,7 @@ enum rc_type {
61 RC_BIT_NEC | RC_BIT_SANYO | RC_BIT_MCE_KBD | \ 63 RC_BIT_NEC | RC_BIT_SANYO | RC_BIT_MCE_KBD | \
62 RC_BIT_RC6_0 | RC_BIT_RC6_6A_20 | RC_BIT_RC6_6A_24 | \ 64 RC_BIT_RC6_0 | RC_BIT_RC6_6A_20 | RC_BIT_RC6_6A_24 | \
63 RC_BIT_RC6_6A_32 | RC_BIT_RC6_MCE | RC_BIT_SHARP | \ 65 RC_BIT_RC6_6A_32 | RC_BIT_RC6_MCE | RC_BIT_SHARP | \
64 RC_BIT_XMP) 66 RC_BIT_XMP | RC_BIT_CEC)
65 67
66 68
67#define RC_SCANCODE_UNKNOWN(x) (x) 69#define RC_SCANCODE_UNKNOWN(x) (x)
@@ -123,6 +125,7 @@ void rc_map_init(void);
123#define RC_MAP_BEHOLD_COLUMBUS "rc-behold-columbus" 125#define RC_MAP_BEHOLD_COLUMBUS "rc-behold-columbus"
124#define RC_MAP_BEHOLD "rc-behold" 126#define RC_MAP_BEHOLD "rc-behold"
125#define RC_MAP_BUDGET_CI_OLD "rc-budget-ci-old" 127#define RC_MAP_BUDGET_CI_OLD "rc-budget-ci-old"
128#define RC_MAP_CEC "rc-cec"
126#define RC_MAP_CINERGY_1400 "rc-cinergy-1400" 129#define RC_MAP_CINERGY_1400 "rc-cinergy-1400"
127#define RC_MAP_CINERGY "rc-cinergy" 130#define RC_MAP_CINERGY "rc-cinergy"
128#define RC_MAP_DELOCK_61959 "rc-delock-61959" 131#define RC_MAP_DELOCK_61959 "rc-delock-61959"
@@ -133,6 +136,7 @@ void rc_map_init(void);
133#define RC_MAP_DM1105_NEC "rc-dm1105-nec" 136#define RC_MAP_DM1105_NEC "rc-dm1105-nec"
134#define RC_MAP_DNTV_LIVE_DVBT_PRO "rc-dntv-live-dvbt-pro" 137#define RC_MAP_DNTV_LIVE_DVBT_PRO "rc-dntv-live-dvbt-pro"
135#define RC_MAP_DNTV_LIVE_DVB_T "rc-dntv-live-dvb-t" 138#define RC_MAP_DNTV_LIVE_DVB_T "rc-dntv-live-dvb-t"
139#define RC_MAP_DTT200U "rc-dtt200u"
136#define RC_MAP_DVBSKY "rc-dvbsky" 140#define RC_MAP_DVBSKY "rc-dvbsky"
137#define RC_MAP_EMPTY "rc-empty" 141#define RC_MAP_EMPTY "rc-empty"
138#define RC_MAP_EM_TERRATEC "rc-em-terratec" 142#define RC_MAP_EM_TERRATEC "rc-em-terratec"
diff --git a/include/media/rcar-fcp.h b/include/media/rcar-fcp.h
new file mode 100644
index 000000000000..4c7fc77eaf29
--- /dev/null
+++ b/include/media/rcar-fcp.h
@@ -0,0 +1,37 @@
1/*
2 * rcar-fcp.h -- R-Car Frame Compression Processor Driver
3 *
4 * Copyright (C) 2016 Renesas Electronics Corporation
5 *
6 * Contact: Laurent Pinchart (laurent.pinchart@ideasonboard.com)
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 */
13#ifndef __MEDIA_RCAR_FCP_H__
14#define __MEDIA_RCAR_FCP_H__
15
16struct device_node;
17struct rcar_fcp_device;
18
19#if IS_ENABLED(CONFIG_VIDEO_RENESAS_FCP)
20struct rcar_fcp_device *rcar_fcp_get(const struct device_node *np);
21void rcar_fcp_put(struct rcar_fcp_device *fcp);
22int rcar_fcp_enable(struct rcar_fcp_device *fcp);
23void rcar_fcp_disable(struct rcar_fcp_device *fcp);
24#else
25static inline struct rcar_fcp_device *rcar_fcp_get(const struct device_node *np)
26{
27 return ERR_PTR(-ENOENT);
28}
29static inline void rcar_fcp_put(struct rcar_fcp_device *fcp) { }
30static inline int rcar_fcp_enable(struct rcar_fcp_device *fcp)
31{
32 return -ENOSYS;
33}
34static inline void rcar_fcp_disable(struct rcar_fcp_device *fcp) { }
35#endif
36
37#endif /* __MEDIA_RCAR_FCP_H__ */
diff --git a/include/media/v4l2-ctrls.h b/include/media/v4l2-ctrls.h
index 0bc9b35b8f3e..8b59336b2217 100644
--- a/include/media/v4l2-ctrls.h
+++ b/include/media/v4l2-ctrls.h
@@ -759,9 +759,9 @@ s32 v4l2_ctrl_g_ctrl(struct v4l2_ctrl *ctrl);
759 * @ctrl: The control. 759 * @ctrl: The control.
760 * @val: The new value. 760 * @val: The new value.
761 * 761 *
762 * This set the control's new value safely by going through the control 762 * This sets the control's new value safely by going through the control
763 * framework. This function will lock the control's handler, so it cannot be 763 * framework. This function assumes the control's handler is already locked,
764 * used from within the &v4l2_ctrl_ops functions. 764 * allowing it to be used from within the &v4l2_ctrl_ops functions.
765 * 765 *
766 * This function is for integer type controls only. 766 * This function is for integer type controls only.
767 */ 767 */
@@ -771,7 +771,7 @@ int __v4l2_ctrl_s_ctrl(struct v4l2_ctrl *ctrl, s32 val);
771 * @ctrl: The control. 771 * @ctrl: The control.
772 * @val: The new value. 772 * @val: The new value.
773 * 773 *
774 * This set the control's new value safely by going through the control 774 * This sets the control's new value safely by going through the control
775 * framework. This function will lock the control's handler, so it cannot be 775 * framework. This function will lock the control's handler, so it cannot be
776 * used from within the &v4l2_ctrl_ops functions. 776 * used from within the &v4l2_ctrl_ops functions.
777 * 777 *
@@ -807,9 +807,9 @@ s64 v4l2_ctrl_g_ctrl_int64(struct v4l2_ctrl *ctrl);
807 * @ctrl: The control. 807 * @ctrl: The control.
808 * @val: The new value. 808 * @val: The new value.
809 * 809 *
810 * This set the control's new value safely by going through the control 810 * This sets the control's new value safely by going through the control
811 * framework. This function will lock the control's handler, so it cannot be 811 * framework. This function assumes the control's handler is already locked,
812 * used from within the &v4l2_ctrl_ops functions. 812 * allowing it to be used from within the &v4l2_ctrl_ops functions.
813 * 813 *
814 * This function is for 64-bit integer type controls only. 814 * This function is for 64-bit integer type controls only.
815 */ 815 */
@@ -821,7 +821,7 @@ int __v4l2_ctrl_s_ctrl_int64(struct v4l2_ctrl *ctrl, s64 val);
821 * @ctrl: The control. 821 * @ctrl: The control.
822 * @val: The new value. 822 * @val: The new value.
823 * 823 *
824 * This set the control's new value safely by going through the control 824 * This sets the control's new value safely by going through the control
825 * framework. This function will lock the control's handler, so it cannot be 825 * framework. This function will lock the control's handler, so it cannot be
826 * used from within the &v4l2_ctrl_ops functions. 826 * used from within the &v4l2_ctrl_ops functions.
827 * 827 *
@@ -843,9 +843,9 @@ static inline int v4l2_ctrl_s_ctrl_int64(struct v4l2_ctrl *ctrl, s64 val)
843 * @ctrl: The control. 843 * @ctrl: The control.
844 * @s: The new string. 844 * @s: The new string.
845 * 845 *
846 * This set the control's new string safely by going through the control 846 * This sets the control's new string safely by going through the control
847 * framework. This function will lock the control's handler, so it cannot be 847 * framework. This function assumes the control's handler is already locked,
848 * used from within the &v4l2_ctrl_ops functions. 848 * allowing it to be used from within the &v4l2_ctrl_ops functions.
849 * 849 *
850 * This function is for string type controls only. 850 * This function is for string type controls only.
851 */ 851 */
@@ -857,7 +857,7 @@ int __v4l2_ctrl_s_ctrl_string(struct v4l2_ctrl *ctrl, const char *s);
857 * @ctrl: The control. 857 * @ctrl: The control.
858 * @s: The new string. 858 * @s: The new string.
859 * 859 *
860 * This set the control's new string safely by going through the control 860 * This sets the control's new string safely by going through the control
861 * framework. This function will lock the control's handler, so it cannot be 861 * framework. This function will lock the control's handler, so it cannot be
862 * used from within the &v4l2_ctrl_ops functions. 862 * used from within the &v4l2_ctrl_ops functions.
863 * 863 *
@@ -903,16 +903,6 @@ int v4l2_try_ext_ctrls(struct v4l2_ctrl_handler *hdl, struct v4l2_ext_controls *
903int v4l2_s_ext_ctrls(struct v4l2_fh *fh, struct v4l2_ctrl_handler *hdl, 903int v4l2_s_ext_ctrls(struct v4l2_fh *fh, struct v4l2_ctrl_handler *hdl,
904 struct v4l2_ext_controls *c); 904 struct v4l2_ext_controls *c);
905 905
906/* Helpers for subdevices. If the associated ctrl_handler == NULL then they
907 will all return -EINVAL. */
908int v4l2_subdev_queryctrl(struct v4l2_subdev *sd, struct v4l2_queryctrl *qc);
909int v4l2_subdev_querymenu(struct v4l2_subdev *sd, struct v4l2_querymenu *qm);
910int v4l2_subdev_g_ext_ctrls(struct v4l2_subdev *sd, struct v4l2_ext_controls *cs);
911int v4l2_subdev_try_ext_ctrls(struct v4l2_subdev *sd, struct v4l2_ext_controls *cs);
912int v4l2_subdev_s_ext_ctrls(struct v4l2_subdev *sd, struct v4l2_ext_controls *cs);
913int v4l2_subdev_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl);
914int v4l2_subdev_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl);
915
916/* Can be used as a subscribe_event function that just subscribes control 906/* Can be used as a subscribe_event function that just subscribes control
917 events. */ 907 events. */
918int v4l2_ctrl_subdev_subscribe_event(struct v4l2_subdev *sd, struct v4l2_fh *fh, 908int v4l2_ctrl_subdev_subscribe_event(struct v4l2_subdev *sd, struct v4l2_fh *fh,
diff --git a/include/media/v4l2-subdev.h b/include/media/v4l2-subdev.h
index 32fc7a4beb5e..c672efc4f87b 100644
--- a/include/media/v4l2-subdev.h
+++ b/include/media/v4l2-subdev.h
@@ -143,20 +143,6 @@ struct v4l2_subdev_io_pin_config {
143 * @s_gpio: set GPIO pins. Very simple right now, might need to be extended with 143 * @s_gpio: set GPIO pins. Very simple right now, might need to be extended with
144 * a direction argument if needed. 144 * a direction argument if needed.
145 * 145 *
146 * @queryctrl: callback for VIDIOC_QUERYCTL ioctl handler code.
147 *
148 * @g_ctrl: callback for VIDIOC_G_CTRL ioctl handler code.
149 *
150 * @s_ctrl: callback for VIDIOC_S_CTRL ioctl handler code.
151 *
152 * @g_ext_ctrls: callback for VIDIOC_G_EXT_CTRLS ioctl handler code.
153 *
154 * @s_ext_ctrls: callback for VIDIOC_S_EXT_CTRLS ioctl handler code.
155 *
156 * @try_ext_ctrls: callback for VIDIOC_TRY_EXT_CTRLS ioctl handler code.
157 *
158 * @querymenu: callback for VIDIOC_QUERYMENU ioctl handler code.
159 *
160 * @ioctl: called at the end of ioctl() syscall handler at the V4L2 core. 146 * @ioctl: called at the end of ioctl() syscall handler at the V4L2 core.
161 * used to provide support for private ioctls used on the driver. 147 * used to provide support for private ioctls used on the driver.
162 * 148 *
@@ -190,13 +176,6 @@ struct v4l2_subdev_core_ops {
190 int (*load_fw)(struct v4l2_subdev *sd); 176 int (*load_fw)(struct v4l2_subdev *sd);
191 int (*reset)(struct v4l2_subdev *sd, u32 val); 177 int (*reset)(struct v4l2_subdev *sd, u32 val);
192 int (*s_gpio)(struct v4l2_subdev *sd, u32 val); 178 int (*s_gpio)(struct v4l2_subdev *sd, u32 val);
193 int (*queryctrl)(struct v4l2_subdev *sd, struct v4l2_queryctrl *qc);
194 int (*g_ctrl)(struct v4l2_subdev *sd, struct v4l2_control *ctrl);
195 int (*s_ctrl)(struct v4l2_subdev *sd, struct v4l2_control *ctrl);
196 int (*g_ext_ctrls)(struct v4l2_subdev *sd, struct v4l2_ext_controls *ctrls);
197 int (*s_ext_ctrls)(struct v4l2_subdev *sd, struct v4l2_ext_controls *ctrls);
198 int (*try_ext_ctrls)(struct v4l2_subdev *sd, struct v4l2_ext_controls *ctrls);
199 int (*querymenu)(struct v4l2_subdev *sd, struct v4l2_querymenu *qm);
200 long (*ioctl)(struct v4l2_subdev *sd, unsigned int cmd, void *arg); 179 long (*ioctl)(struct v4l2_subdev *sd, unsigned int cmd, void *arg);
201#ifdef CONFIG_COMPAT 180#ifdef CONFIG_COMPAT
202 long (*compat_ioctl32)(struct v4l2_subdev *sd, unsigned int cmd, 181 long (*compat_ioctl32)(struct v4l2_subdev *sd, unsigned int cmd,
diff --git a/include/media/videobuf2-core.h b/include/media/videobuf2-core.h
index 88e3ab496e8f..bea81c9e3758 100644
--- a/include/media/videobuf2-core.h
+++ b/include/media/videobuf2-core.h
@@ -27,7 +27,6 @@ enum vb2_memory {
27 VB2_MEMORY_DMABUF = 4, 27 VB2_MEMORY_DMABUF = 4,
28}; 28};
29 29
30struct vb2_alloc_ctx;
31struct vb2_fileio_data; 30struct vb2_fileio_data;
32struct vb2_threadio_data; 31struct vb2_threadio_data;
33 32
@@ -57,7 +56,7 @@ struct vb2_threadio_data;
57 * @put_userptr: inform the allocator that a USERPTR buffer will no longer 56 * @put_userptr: inform the allocator that a USERPTR buffer will no longer
58 * be used. 57 * be used.
59 * @attach_dmabuf: attach a shared struct dma_buf for a hardware operation; 58 * @attach_dmabuf: attach a shared struct dma_buf for a hardware operation;
60 * used for DMABUF memory types; alloc_ctx is the alloc context 59 * used for DMABUF memory types; dev is the alloc device
61 * dbuf is the shared dma_buf; returns NULL on failure; 60 * dbuf is the shared dma_buf; returns NULL on failure;
62 * allocator private per-buffer structure on success; 61 * allocator private per-buffer structure on success;
63 * this needs to be used for further accesses to the buffer. 62 * this needs to be used for further accesses to the buffer.
@@ -93,13 +92,13 @@ struct vb2_threadio_data;
93 * unmap_dmabuf. 92 * unmap_dmabuf.
94 */ 93 */
95struct vb2_mem_ops { 94struct vb2_mem_ops {
96 void *(*alloc)(void *alloc_ctx, unsigned long size, 95 void *(*alloc)(struct device *dev, const struct dma_attrs *attrs,
97 enum dma_data_direction dma_dir, 96 unsigned long size, enum dma_data_direction dma_dir,
98 gfp_t gfp_flags); 97 gfp_t gfp_flags);
99 void (*put)(void *buf_priv); 98 void (*put)(void *buf_priv);
100 struct dma_buf *(*get_dmabuf)(void *buf_priv, unsigned long flags); 99 struct dma_buf *(*get_dmabuf)(void *buf_priv, unsigned long flags);
101 100
102 void *(*get_userptr)(void *alloc_ctx, unsigned long vaddr, 101 void *(*get_userptr)(struct device *dev, unsigned long vaddr,
103 unsigned long size, 102 unsigned long size,
104 enum dma_data_direction dma_dir); 103 enum dma_data_direction dma_dir);
105 void (*put_userptr)(void *buf_priv); 104 void (*put_userptr)(void *buf_priv);
@@ -107,7 +106,7 @@ struct vb2_mem_ops {
107 void (*prepare)(void *buf_priv); 106 void (*prepare)(void *buf_priv);
108 void (*finish)(void *buf_priv); 107 void (*finish)(void *buf_priv);
109 108
110 void *(*attach_dmabuf)(void *alloc_ctx, struct dma_buf *dbuf, 109 void *(*attach_dmabuf)(struct device *dev, struct dma_buf *dbuf,
111 unsigned long size, 110 unsigned long size,
112 enum dma_data_direction dma_dir); 111 enum dma_data_direction dma_dir);
113 void (*detach_dmabuf)(void *buf_priv); 112 void (*detach_dmabuf)(void *buf_priv);
@@ -282,7 +281,7 @@ struct vb2_buffer {
282 * in *num_buffers, the required number of planes per 281 * in *num_buffers, the required number of planes per
283 * buffer in *num_planes, the size of each plane should be 282 * buffer in *num_planes, the size of each plane should be
284 * set in the sizes[] array and optional per-plane 283 * set in the sizes[] array and optional per-plane
285 * allocator specific context in the alloc_ctxs[] array. 284 * allocator specific device in the alloc_devs[] array.
286 * When called from VIDIOC_REQBUFS, *num_planes == 0, the 285 * When called from VIDIOC_REQBUFS, *num_planes == 0, the
287 * driver has to use the currently configured format to 286 * driver has to use the currently configured format to
288 * determine the plane sizes and *num_buffers is the total 287 * determine the plane sizes and *num_buffers is the total
@@ -356,7 +355,7 @@ struct vb2_buffer {
356struct vb2_ops { 355struct vb2_ops {
357 int (*queue_setup)(struct vb2_queue *q, 356 int (*queue_setup)(struct vb2_queue *q,
358 unsigned int *num_buffers, unsigned int *num_planes, 357 unsigned int *num_buffers, unsigned int *num_planes,
359 unsigned int sizes[], void *alloc_ctxs[]); 358 unsigned int sizes[], struct device *alloc_devs[]);
360 359
361 void (*wait_prepare)(struct vb2_queue *q); 360 void (*wait_prepare)(struct vb2_queue *q);
362 void (*wait_finish)(struct vb2_queue *q); 361 void (*wait_finish)(struct vb2_queue *q);
@@ -401,6 +400,9 @@ struct vb2_buf_ops {
401 * caller. For example, for V4L2, it should match 400 * caller. For example, for V4L2, it should match
402 * the V4L2_BUF_TYPE_* in include/uapi/linux/videodev2.h 401 * the V4L2_BUF_TYPE_* in include/uapi/linux/videodev2.h
403 * @io_modes: supported io methods (see vb2_io_modes enum) 402 * @io_modes: supported io methods (see vb2_io_modes enum)
403 * @dev: device to use for the default allocation context if the driver
404 * doesn't fill in the @alloc_devs array.
405 * @dma_attrs: DMA attributes to use for the DMA. May be NULL.
404 * @fileio_read_once: report EOF after reading the first buffer 406 * @fileio_read_once: report EOF after reading the first buffer
405 * @fileio_write_immediately: queue buffer after each write() call 407 * @fileio_write_immediately: queue buffer after each write() call
406 * @allow_zero_bytesused: allow bytesused == 0 to be passed to the driver 408 * @allow_zero_bytesused: allow bytesused == 0 to be passed to the driver
@@ -447,7 +449,7 @@ struct vb2_buf_ops {
447 * @done_list: list of buffers ready to be dequeued to userspace 449 * @done_list: list of buffers ready to be dequeued to userspace
448 * @done_lock: lock to protect done_list list 450 * @done_lock: lock to protect done_list list
449 * @done_wq: waitqueue for processes waiting for buffers ready to be dequeued 451 * @done_wq: waitqueue for processes waiting for buffers ready to be dequeued
450 * @alloc_ctx: memory type/allocator-specific contexts for each plane 452 * @alloc_devs: memory type/allocator-specific per-plane device
451 * @streaming: current streaming state 453 * @streaming: current streaming state
452 * @start_streaming_called: start_streaming() was called successfully and we 454 * @start_streaming_called: start_streaming() was called successfully and we
453 * started streaming. 455 * started streaming.
@@ -467,6 +469,8 @@ struct vb2_buf_ops {
467struct vb2_queue { 469struct vb2_queue {
468 unsigned int type; 470 unsigned int type;
469 unsigned int io_modes; 471 unsigned int io_modes;
472 struct device *dev;
473 const struct dma_attrs *dma_attrs;
470 unsigned fileio_read_once:1; 474 unsigned fileio_read_once:1;
471 unsigned fileio_write_immediately:1; 475 unsigned fileio_write_immediately:1;
472 unsigned allow_zero_bytesused:1; 476 unsigned allow_zero_bytesused:1;
@@ -499,7 +503,7 @@ struct vb2_queue {
499 spinlock_t done_lock; 503 spinlock_t done_lock;
500 wait_queue_head_t done_wq; 504 wait_queue_head_t done_wq;
501 505
502 void *alloc_ctx[VB2_MAX_PLANES]; 506 struct device *alloc_devs[VB2_MAX_PLANES];
503 507
504 unsigned int streaming:1; 508 unsigned int streaming:1;
505 unsigned int start_streaming_called:1; 509 unsigned int start_streaming_called:1;
diff --git a/include/media/videobuf2-dma-contig.h b/include/media/videobuf2-dma-contig.h
index 2087c9a68be3..df2aabee3401 100644
--- a/include/media/videobuf2-dma-contig.h
+++ b/include/media/videobuf2-dma-contig.h
@@ -26,15 +26,8 @@ vb2_dma_contig_plane_dma_addr(struct vb2_buffer *vb, unsigned int plane_no)
26 return *addr; 26 return *addr;
27} 27}
28 28
29void *vb2_dma_contig_init_ctx_attrs(struct device *dev, 29int vb2_dma_contig_set_max_seg_size(struct device *dev, unsigned int size);
30 struct dma_attrs *attrs); 30void vb2_dma_contig_clear_max_seg_size(struct device *dev);
31
32static inline void *vb2_dma_contig_init_ctx(struct device *dev)
33{
34 return vb2_dma_contig_init_ctx_attrs(dev, NULL);
35}
36
37void vb2_dma_contig_cleanup_ctx(void *alloc_ctx);
38 31
39extern const struct vb2_mem_ops vb2_dma_contig_memops; 32extern const struct vb2_mem_ops vb2_dma_contig_memops;
40 33
diff --git a/include/media/videobuf2-dma-sg.h b/include/media/videobuf2-dma-sg.h
index 8d1083f83c3d..52afa0e2bb17 100644
--- a/include/media/videobuf2-dma-sg.h
+++ b/include/media/videobuf2-dma-sg.h
@@ -21,9 +21,6 @@ static inline struct sg_table *vb2_dma_sg_plane_desc(
21 return (struct sg_table *)vb2_plane_cookie(vb, plane_no); 21 return (struct sg_table *)vb2_plane_cookie(vb, plane_no);
22} 22}
23 23
24void *vb2_dma_sg_init_ctx(struct device *dev);
25void vb2_dma_sg_cleanup_ctx(void *alloc_ctx);
26
27extern const struct vb2_mem_ops vb2_dma_sg_memops; 24extern const struct vb2_mem_ops vb2_dma_sg_memops;
28 25
29#endif 26#endif
diff --git a/include/media/vsp1.h b/include/media/vsp1.h
index 3e654a0455bd..9322d9775fb7 100644
--- a/include/media/vsp1.h
+++ b/include/media/vsp1.h
@@ -14,31 +14,28 @@
14#define __MEDIA_VSP1_H__ 14#define __MEDIA_VSP1_H__
15 15
16#include <linux/types.h> 16#include <linux/types.h>
17#include <linux/videodev2.h>
17 18
18struct device; 19struct device;
19struct v4l2_rect;
20 20
21int vsp1_du_init(struct device *dev); 21int vsp1_du_init(struct device *dev);
22 22
23int vsp1_du_setup_lif(struct device *dev, unsigned int width, 23int vsp1_du_setup_lif(struct device *dev, unsigned int width,
24 unsigned int height); 24 unsigned int height);
25 25
26struct vsp1_du_atomic_config {
27 u32 pixelformat;
28 unsigned int pitch;
29 dma_addr_t mem[2];
30 struct v4l2_rect src;
31 struct v4l2_rect dst;
32 unsigned int alpha;
33 unsigned int zpos;
34};
35
26void vsp1_du_atomic_begin(struct device *dev); 36void vsp1_du_atomic_begin(struct device *dev);
27int vsp1_du_atomic_update_ext(struct device *dev, unsigned int rpf, 37int vsp1_du_atomic_update(struct device *dev, unsigned int rpf,
28 u32 pixelformat, unsigned int pitch, 38 const struct vsp1_du_atomic_config *cfg);
29 dma_addr_t mem[2], const struct v4l2_rect *src,
30 const struct v4l2_rect *dst, unsigned int alpha,
31 unsigned int zpos);
32void vsp1_du_atomic_flush(struct device *dev); 39void vsp1_du_atomic_flush(struct device *dev);
33 40
34static inline int vsp1_du_atomic_update(struct device *dev,
35 unsigned int rpf_index, u32 pixelformat,
36 unsigned int pitch, dma_addr_t mem[2],
37 const struct v4l2_rect *src,
38 const struct v4l2_rect *dst)
39{
40 return vsp1_du_atomic_update_ext(dev, rpf_index, pixelformat, pitch,
41 mem, src, dst, 255, 0);
42}
43
44#endif /* __MEDIA_VSP1_H__ */ 41#endif /* __MEDIA_VSP1_H__ */
diff --git a/include/uapi/linux/media.h b/include/uapi/linux/media.h
index df59edee25d1..7acf0f634f70 100644
--- a/include/uapi/linux/media.h
+++ b/include/uapi/linux/media.h
@@ -95,6 +95,16 @@ struct media_device_info {
95#define MEDIA_ENT_F_AUDIO_MIXER (MEDIA_ENT_F_BASE + 0x03003) 95#define MEDIA_ENT_F_AUDIO_MIXER (MEDIA_ENT_F_BASE + 0x03003)
96 96
97/* 97/*
98 * Processing entities
99 */
100#define MEDIA_ENT_F_PROC_VIDEO_COMPOSER (MEDIA_ENT_F_BASE + 0x4001)
101#define MEDIA_ENT_F_PROC_VIDEO_PIXEL_FORMATTER (MEDIA_ENT_F_BASE + 0x4002)
102#define MEDIA_ENT_F_PROC_VIDEO_PIXEL_ENC_CONV (MEDIA_ENT_F_BASE + 0x4003)
103#define MEDIA_ENT_F_PROC_VIDEO_LUT (MEDIA_ENT_F_BASE + 0x4004)
104#define MEDIA_ENT_F_PROC_VIDEO_SCALER (MEDIA_ENT_F_BASE + 0x4005)
105#define MEDIA_ENT_F_PROC_VIDEO_STATISTICS (MEDIA_ENT_F_BASE + 0x4006)
106
107/*
98 * Connectors 108 * Connectors
99 */ 109 */
100/* It is a responsibility of the entity drivers to add connectors and links */ 110/* It is a responsibility of the entity drivers to add connectors and links */
diff --git a/include/uapi/linux/serio.h b/include/uapi/linux/serio.h
index c2ea1698257f..f2447a83ac8d 100644
--- a/include/uapi/linux/serio.h
+++ b/include/uapi/linux/serio.h
@@ -78,5 +78,6 @@
78#define SERIO_TSC40 0x3d 78#define SERIO_TSC40 0x3d
79#define SERIO_WACOM_IV 0x3e 79#define SERIO_WACOM_IV 0x3e
80#define SERIO_EGALAX 0x3f 80#define SERIO_EGALAX 0x3f
81#define SERIO_PULSE8_CEC 0x40
81 82
82#endif /* _UAPI_SERIO_H */ 83#endif /* _UAPI_SERIO_H */
diff --git a/include/uapi/linux/videodev2.h b/include/uapi/linux/videodev2.h
index 8f951917be74..724f43e69d03 100644
--- a/include/uapi/linux/videodev2.h
+++ b/include/uapi/linux/videodev2.h
@@ -504,22 +504,16 @@ struct v4l2_pix_format {
504#define V4L2_PIX_FMT_UV8 v4l2_fourcc('U', 'V', '8', ' ') /* 8 UV 4:4 */ 504#define V4L2_PIX_FMT_UV8 v4l2_fourcc('U', 'V', '8', ' ') /* 8 UV 4:4 */
505 505
506/* Luminance+Chrominance formats */ 506/* Luminance+Chrominance formats */
507#define V4L2_PIX_FMT_YVU410 v4l2_fourcc('Y', 'V', 'U', '9') /* 9 YVU 4:1:0 */
508#define V4L2_PIX_FMT_YVU420 v4l2_fourcc('Y', 'V', '1', '2') /* 12 YVU 4:2:0 */
509#define V4L2_PIX_FMT_YUYV v4l2_fourcc('Y', 'U', 'Y', 'V') /* 16 YUV 4:2:2 */ 507#define V4L2_PIX_FMT_YUYV v4l2_fourcc('Y', 'U', 'Y', 'V') /* 16 YUV 4:2:2 */
510#define V4L2_PIX_FMT_YYUV v4l2_fourcc('Y', 'Y', 'U', 'V') /* 16 YUV 4:2:2 */ 508#define V4L2_PIX_FMT_YYUV v4l2_fourcc('Y', 'Y', 'U', 'V') /* 16 YUV 4:2:2 */
511#define V4L2_PIX_FMT_YVYU v4l2_fourcc('Y', 'V', 'Y', 'U') /* 16 YVU 4:2:2 */ 509#define V4L2_PIX_FMT_YVYU v4l2_fourcc('Y', 'V', 'Y', 'U') /* 16 YVU 4:2:2 */
512#define V4L2_PIX_FMT_UYVY v4l2_fourcc('U', 'Y', 'V', 'Y') /* 16 YUV 4:2:2 */ 510#define V4L2_PIX_FMT_UYVY v4l2_fourcc('U', 'Y', 'V', 'Y') /* 16 YUV 4:2:2 */
513#define V4L2_PIX_FMT_VYUY v4l2_fourcc('V', 'Y', 'U', 'Y') /* 16 YUV 4:2:2 */ 511#define V4L2_PIX_FMT_VYUY v4l2_fourcc('V', 'Y', 'U', 'Y') /* 16 YUV 4:2:2 */
514#define V4L2_PIX_FMT_YUV422P v4l2_fourcc('4', '2', '2', 'P') /* 16 YVU422 planar */
515#define V4L2_PIX_FMT_YUV411P v4l2_fourcc('4', '1', '1', 'P') /* 16 YVU411 planar */
516#define V4L2_PIX_FMT_Y41P v4l2_fourcc('Y', '4', '1', 'P') /* 12 YUV 4:1:1 */ 512#define V4L2_PIX_FMT_Y41P v4l2_fourcc('Y', '4', '1', 'P') /* 12 YUV 4:1:1 */
517#define V4L2_PIX_FMT_YUV444 v4l2_fourcc('Y', '4', '4', '4') /* 16 xxxxyyyy uuuuvvvv */ 513#define V4L2_PIX_FMT_YUV444 v4l2_fourcc('Y', '4', '4', '4') /* 16 xxxxyyyy uuuuvvvv */
518#define V4L2_PIX_FMT_YUV555 v4l2_fourcc('Y', 'U', 'V', 'O') /* 16 YUV-5-5-5 */ 514#define V4L2_PIX_FMT_YUV555 v4l2_fourcc('Y', 'U', 'V', 'O') /* 16 YUV-5-5-5 */
519#define V4L2_PIX_FMT_YUV565 v4l2_fourcc('Y', 'U', 'V', 'P') /* 16 YUV-5-6-5 */ 515#define V4L2_PIX_FMT_YUV565 v4l2_fourcc('Y', 'U', 'V', 'P') /* 16 YUV-5-6-5 */
520#define V4L2_PIX_FMT_YUV32 v4l2_fourcc('Y', 'U', 'V', '4') /* 32 YUV-8-8-8-8 */ 516#define V4L2_PIX_FMT_YUV32 v4l2_fourcc('Y', 'U', 'V', '4') /* 32 YUV-8-8-8-8 */
521#define V4L2_PIX_FMT_YUV410 v4l2_fourcc('Y', 'U', 'V', '9') /* 9 YUV 4:1:0 */
522#define V4L2_PIX_FMT_YUV420 v4l2_fourcc('Y', 'U', '1', '2') /* 12 YUV 4:2:0 */
523#define V4L2_PIX_FMT_HI240 v4l2_fourcc('H', 'I', '2', '4') /* 8 8-bit color */ 517#define V4L2_PIX_FMT_HI240 v4l2_fourcc('H', 'I', '2', '4') /* 8 8-bit color */
524#define V4L2_PIX_FMT_HM12 v4l2_fourcc('H', 'M', '1', '2') /* 8 YUV 4:2:0 16x16 macroblocks */ 518#define V4L2_PIX_FMT_HM12 v4l2_fourcc('H', 'M', '1', '2') /* 8 YUV 4:2:0 16x16 macroblocks */
525#define V4L2_PIX_FMT_M420 v4l2_fourcc('M', '4', '2', '0') /* 12 YUV 4:2:0 2 lines y, 1 line uv interleaved */ 519#define V4L2_PIX_FMT_M420 v4l2_fourcc('M', '4', '2', '0') /* 12 YUV 4:2:0 2 lines y, 1 line uv interleaved */
@@ -540,6 +534,14 @@ struct v4l2_pix_format {
540#define V4L2_PIX_FMT_NV12MT v4l2_fourcc('T', 'M', '1', '2') /* 12 Y/CbCr 4:2:0 64x32 macroblocks */ 534#define V4L2_PIX_FMT_NV12MT v4l2_fourcc('T', 'M', '1', '2') /* 12 Y/CbCr 4:2:0 64x32 macroblocks */
541#define V4L2_PIX_FMT_NV12MT_16X16 v4l2_fourcc('V', 'M', '1', '2') /* 12 Y/CbCr 4:2:0 16x16 macroblocks */ 535#define V4L2_PIX_FMT_NV12MT_16X16 v4l2_fourcc('V', 'M', '1', '2') /* 12 Y/CbCr 4:2:0 16x16 macroblocks */
542 536
537/* three planes - Y Cb, Cr */
538#define V4L2_PIX_FMT_YUV410 v4l2_fourcc('Y', 'U', 'V', '9') /* 9 YUV 4:1:0 */
539#define V4L2_PIX_FMT_YVU410 v4l2_fourcc('Y', 'V', 'U', '9') /* 9 YVU 4:1:0 */
540#define V4L2_PIX_FMT_YUV411P v4l2_fourcc('4', '1', '1', 'P') /* 12 YVU411 planar */
541#define V4L2_PIX_FMT_YUV420 v4l2_fourcc('Y', 'U', '1', '2') /* 12 YUV 4:2:0 */
542#define V4L2_PIX_FMT_YVU420 v4l2_fourcc('Y', 'V', '1', '2') /* 12 YVU 4:2:0 */
543#define V4L2_PIX_FMT_YUV422P v4l2_fourcc('4', '2', '2', 'P') /* 16 YVU422 planar */
544
543/* three non contiguous planes - Y, Cb, Cr */ 545/* three non contiguous planes - Y, Cb, Cr */
544#define V4L2_PIX_FMT_YUV420M v4l2_fourcc('Y', 'M', '1', '2') /* 12 YUV420 planar */ 546#define V4L2_PIX_FMT_YUV420M v4l2_fourcc('Y', 'M', '1', '2') /* 12 YUV420 planar */
545#define V4L2_PIX_FMT_YVU420M v4l2_fourcc('Y', 'M', '2', '1') /* 12 YVU420 planar */ 547#define V4L2_PIX_FMT_YVU420M v4l2_fourcc('Y', 'M', '2', '1') /* 12 YVU420 planar */
diff --git a/include/uapi/linux/vsp1.h b/include/uapi/linux/vsp1.h
deleted file mode 100644
index 9a823696d816..000000000000
--- a/include/uapi/linux/vsp1.h
+++ /dev/null
@@ -1,34 +0,0 @@
1/*
2 * vsp1.h
3 *
4 * Renesas R-Car VSP1 - User-space API
5 *
6 * Copyright (C) 2013 Renesas Corporation
7 *
8 * Contacts: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
13 */
14
15#ifndef __VSP1_USER_H__
16#define __VSP1_USER_H__
17
18#include <linux/types.h>
19#include <linux/videodev2.h>
20
21/*
22 * Private IOCTLs
23 *
24 * VIDIOC_VSP1_LUT_CONFIG - Configure the lookup table
25 */
26
27#define VIDIOC_VSP1_LUT_CONFIG \
28 _IOWR('V', BASE_VIDIOC_PRIVATE + 1, struct vsp1_lut_config)
29
30struct vsp1_lut_config {
31 __u32 lut[256];
32};
33
34#endif /* __VSP1_USER_H__ */
diff --git a/samples/v4l/v4l2-pci-skeleton.c b/samples/v4l/v4l2-pci-skeleton.c
index a55cf94ac907..93b76c3220fd 100644
--- a/samples/v4l/v4l2-pci-skeleton.c
+++ b/samples/v4l/v4l2-pci-skeleton.c
@@ -56,7 +56,6 @@ MODULE_LICENSE("GPL v2");
56 * @format: current pix format 56 * @format: current pix format
57 * @input: current video input (0 = SDTV, 1 = HDTV) 57 * @input: current video input (0 = SDTV, 1 = HDTV)
58 * @queue: vb2 video capture queue 58 * @queue: vb2 video capture queue
59 * @alloc_ctx: vb2 contiguous DMA context
60 * @qlock: spinlock controlling access to buf_list and sequence 59 * @qlock: spinlock controlling access to buf_list and sequence
61 * @buf_list: list of buffers queued for DMA 60 * @buf_list: list of buffers queued for DMA
62 * @sequence: frame sequence counter 61 * @sequence: frame sequence counter
@@ -73,7 +72,6 @@ struct skeleton {
73 unsigned input; 72 unsigned input;
74 73
75 struct vb2_queue queue; 74 struct vb2_queue queue;
76 struct vb2_alloc_ctx *alloc_ctx;
77 75
78 spinlock_t qlock; 76 spinlock_t qlock;
79 struct list_head buf_list; 77 struct list_head buf_list;
@@ -165,7 +163,7 @@ static irqreturn_t skeleton_irq(int irq, void *dev_id)
165 */ 163 */
166static int queue_setup(struct vb2_queue *vq, 164static int queue_setup(struct vb2_queue *vq,
167 unsigned int *nbuffers, unsigned int *nplanes, 165 unsigned int *nbuffers, unsigned int *nplanes,
168 unsigned int sizes[], void *alloc_ctxs[]) 166 unsigned int sizes[], struct device *alloc_devs[])
169{ 167{
170 struct skeleton *skel = vb2_get_drv_priv(vq); 168 struct skeleton *skel = vb2_get_drv_priv(vq);
171 169
@@ -182,7 +180,6 @@ static int queue_setup(struct vb2_queue *vq,
182 180
183 if (vq->num_buffers + *nbuffers < 3) 181 if (vq->num_buffers + *nbuffers < 3)
184 *nbuffers = 3 - vq->num_buffers; 182 *nbuffers = 3 - vq->num_buffers;
185 alloc_ctxs[0] = skel->alloc_ctx;
186 183
187 if (*nplanes) 184 if (*nplanes)
188 return sizes[0] < skel->format.sizeimage ? -EINVAL : 0; 185 return sizes[0] < skel->format.sizeimage ? -EINVAL : 0;
@@ -820,6 +817,7 @@ static int skeleton_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
820 q = &skel->queue; 817 q = &skel->queue;
821 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; 818 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
822 q->io_modes = VB2_MMAP | VB2_DMABUF | VB2_READ; 819 q->io_modes = VB2_MMAP | VB2_DMABUF | VB2_READ;
820 q->dev = &pdev->dev;
823 q->drv_priv = skel; 821 q->drv_priv = skel;
824 q->buf_struct_size = sizeof(struct skel_buffer); 822 q->buf_struct_size = sizeof(struct skel_buffer);
825 q->ops = &skel_qops; 823 q->ops = &skel_qops;
@@ -850,12 +848,6 @@ static int skeleton_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
850 if (ret) 848 if (ret)
851 goto free_hdl; 849 goto free_hdl;
852 850
853 skel->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
854 if (IS_ERR(skel->alloc_ctx)) {
855 dev_err(&pdev->dev, "Can't allocate buffer context");
856 ret = PTR_ERR(skel->alloc_ctx);
857 goto free_hdl;
858 }
859 INIT_LIST_HEAD(&skel->buf_list); 851 INIT_LIST_HEAD(&skel->buf_list);
860 spin_lock_init(&skel->qlock); 852 spin_lock_init(&skel->qlock);
861 853
@@ -885,13 +877,11 @@ static int skeleton_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
885 877
886 ret = video_register_device(vdev, VFL_TYPE_GRABBER, -1); 878 ret = video_register_device(vdev, VFL_TYPE_GRABBER, -1);
887 if (ret) 879 if (ret)
888 goto free_ctx; 880 goto free_hdl;
889 881
890 dev_info(&pdev->dev, "V4L2 PCI Skeleton Driver loaded\n"); 882 dev_info(&pdev->dev, "V4L2 PCI Skeleton Driver loaded\n");
891 return 0; 883 return 0;
892 884
893free_ctx:
894 vb2_dma_contig_cleanup_ctx(skel->alloc_ctx);
895free_hdl: 885free_hdl:
896 v4l2_ctrl_handler_free(&skel->ctrl_handler); 886 v4l2_ctrl_handler_free(&skel->ctrl_handler);
897 v4l2_device_unregister(&skel->v4l2_dev); 887 v4l2_device_unregister(&skel->v4l2_dev);
@@ -907,7 +897,6 @@ static void skeleton_remove(struct pci_dev *pdev)
907 897
908 video_unregister_device(&skel->vdev); 898 video_unregister_device(&skel->vdev);
909 v4l2_ctrl_handler_free(&skel->ctrl_handler); 899 v4l2_ctrl_handler_free(&skel->ctrl_handler);
910 vb2_dma_contig_cleanup_ctx(skel->alloc_ctx);
911 v4l2_device_unregister(&skel->v4l2_dev); 900 v4l2_device_unregister(&skel->v4l2_dev);
912 pci_disable_device(skel->pdev); 901 pci_disable_device(skel->pdev);
913} 902}