123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325
#include <linux/clk.h>
#include <linux/completion.h>
#include <linux/delay.h>
#include <linux/dmaengine.h>
#include <linux/genalloc.h>
#include <linux/init.h>
#include <linux/interrupt.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/of_graph.h>
#include <linux/pinctrl/consumer.h>
#include <linux/platform_device.h>
#include <linux/pm_runtime.h>
#include <linux/reset.h>
#include <linux/videodev2.h>
#include <media/v4l2-ctrls.h>
#include <media/v4l2-dev.h>
#include <media/v4l2-device.h>
#include <media/v4l2-event.h>
#include <media/v4l2-fwnode.h>
#include <media/v4l2-image-sizes.h>
#include <media/v4l2-ioctl.h>
#include <media/v4l2-rect.h>
#include <media/videobuf2-dma-contig.h>
#define DRV_NAME "stm32-dcmi"
#define DCMI_CR 0x00 /* Control Register */
#define DCMI_SR 0x04 /* Status Register */
#define DCMI_RIS 0x08 /* Raw Interrupt Status register */
#define DCMI_IER 0x0C /* Interrupt Enable Register */
#define DCMI_MIS 0x10 /* Masked Interrupt Status register */
#define DCMI_ICR 0x14 /* Interrupt Clear Register */
#define DCMI_ESCR 0x18 /* Embedded Synchronization Code Register */
#define DCMI_ESUR 0x1C /* Embedded Synchronization Unmask Register */
#define DCMI_CWSTRT 0x20 /* Crop Window STaRT */
#define DCMI_CWSIZE 0x24 /* Crop Window SIZE */
#define DCMI_DR 0x28 /* Data Register */
#define DCMI_IDR 0x2C /* IDentifier Register */
#define CR_CAPTURE BIT(0)
#define CR_CM BIT(1)
#define CR_CROP BIT(2)
#define CR_JPEG BIT(3)
#define CR_ESS BIT(4)
#define CR_PCKPOL BIT(5)
#define CR_HSPOL BIT(6)
#define CR_VSPOL BIT(7)
#define CR_FCRC_0 BIT(8)
#define CR_FCRC_1 BIT(9)
#define CR_EDM_0 BIT(10)
#define CR_EDM_1 BIT(11)
#define CR_ENABLE BIT(14)
#define SR_HSYNC BIT(0)
#define SR_VSYNC BIT(1)
#define SR_FNE BIT(2)
#define IT_FRAME BIT(0)
#define IT_OVR BIT(1)
#define IT_ERR BIT(2)
#define IT_VSYNC BIT(3)
#define IT_LINE BIT(4)
enum state {
STOPPED = 0,
WAIT_FOR_BUFFER,
RUNNING,
};
#define MIN_WIDTH 16U
#define MAX_WIDTH 2592U
#define MIN_HEIGHT 16U
#define MAX_HEIGHT 2592U
#define TIMEOUT_MS 1000
#define OVERRUN_ERROR_THRESHOLD 3
struct dcmi_format {
u32 fourcc;
u32 mbus_code;
u8 bpp;
};
struct dcmi_framesize {
u32 width;
u32 height;
};
struct dcmi_buf {
struct vb2_v4l2_buffer vb;
bool prepared;
struct sg_table sgt;
struct sg_table sgt_mdma;
struct dma_async_tx_descriptor *dma_desc;
struct dma_async_tx_descriptor *mdma_desc;
size_t size;
struct list_head list;
};
struct stm32_dcmi {
spinlock_t irqlock;
struct device *dev;
void __iomem *regs;
struct resource *res;
struct reset_control *rstc;
int sequence;
struct list_head buffers;
struct dcmi_buf *active;
int irq;
struct v4l2_device v4l2_dev;
struct video_device *vdev;
struct v4l2_async_notifier notifier;
struct v4l2_subdev *source;
struct v4l2_subdev *s_subdev;
struct v4l2_format fmt;
struct v4l2_rect crop;
bool do_crop;
const struct dcmi_format **sd_formats;
unsigned int num_of_sd_formats;
const struct dcmi_format *sd_format;
struct dcmi_framesize *sd_framesizes;
unsigned int num_of_sd_framesizes;
struct dcmi_framesize sd_framesize;
struct v4l2_rect sd_bounds;
struct mutex lock;
struct vb2_queue queue;
struct v4l2_mbus_config_parallel bus;
enum v4l2_mbus_type bus_type;
struct completion complete;
struct clk *mclk;
enum state state;
struct dma_chan *dma_chan;
dma_cookie_t dma_cookie;
u32 dma_max_burst;
struct gen_pool *sram_pool;
struct dma_chan *mdma_chan;
void *sram_buf;
u32 sram_buf_size;
dma_addr_t sram_dma_buf;
dma_cookie_t mdma_cookie;
u32 misr;
int errors_count;
int overrun_count;
int buffers_count;
struct media_device mdev;
struct media_pad vid_cap_pad;
struct media_pipeline pipeline;
};
static inline struct stm32_dcmi *notifier_to_dcmi(struct v4l2_async_notifier *n)
{
return container_of(n, struct stm32_dcmi, notifier);
}
static inline u32 reg_read(void __iomem *base, u32 reg)
{
return readl_relaxed(base + reg);
}
static inline void reg_write(void __iomem *base, u32 reg, u32 val)
{
writel_relaxed(val, base + reg);
}
static inline void reg_set(void __iomem *base, u32 reg, u32 mask)
{
reg_write(base, reg, reg_read(base, reg) | mask);
}
static inline void reg_clear(void __iomem *base, u32 reg, u32 mask)
{
reg_write(base, reg, reg_read(base, reg) & ~mask);
}
static int dcmi_start_capture(struct stm32_dcmi *dcmi, struct dcmi_buf *buf);
static void dcmi_buffer_done(struct stm32_dcmi *dcmi,
struct dcmi_buf *buf,
size_t bytesused,
int err)
{
struct vb2_v4l2_buffer *vbuf;
if (!buf)
return;
list_del_init(&buf->list);
vbuf = &buf->vb;
vbuf->sequence = dcmi->sequence++;
vbuf->field = V4L2_FIELD_NONE;
vbuf->vb2_buf.timestamp = ktime_get_ns();
vb2_set_plane_payload(&vbuf->vb2_buf, 0, bytesused);
vb2_buffer_done(&vbuf->vb2_buf,
err ? VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE);
dev_dbg(dcmi->dev, "buffer[%d] done seq=%d, bytesused=%zu\n",
vbuf->vb2_buf.index, vbuf->sequence, bytesused);
dcmi->buffers_count++;
dcmi->active = NULL;
}
static int dcmi_restart_capture(struct stm32_dcmi *dcmi)
{
struct dcmi_buf *buf;
if (dcmi->state != RUNNING)
return 0;
if (list_empty(&dcmi->buffers)) {
dev_dbg(dcmi->dev, "Capture restart is deferred to next buffer queueing\n");
dcmi->state = WAIT_FOR_BUFFER;
return 0;
}
buf = list_entry(dcmi->buffers.next, struct dcmi_buf, list);
dcmi->active = buf;
return dcmi_start_capture(dcmi, buf);
}
static int dcmi_start_dma(struct stm32_dcmi *dcmi,
struct dcmi_buf *buf)
{
if (dcmi->mdma_chan) {
dcmi->mdma_cookie = dmaengine_submit(buf->mdma_desc);
if (dma_submit_error(dcmi->mdma_cookie)) {
dev_err(dcmi->dev, "%s: MDMA submission failed\n", __func__);
return -ENXIO;
}
}
dcmi->dma_cookie = dmaengine_submit(buf->dma_desc);
if (dma_submit_error(dcmi->dma_cookie)) {
dev_err(dcmi->dev, "%s: DMA submission failed\n", __func__);
return -ENXIO;
}
if (dcmi->mdma_chan)
dma_async_issue_pending(dcmi->mdma_chan);
dma_async_issue_pending(dcmi->dma_chan);
return 0;
}
static int dcmi_start_capture(struct stm32_dcmi *dcmi, struct dcmi_buf *buf)
{
int ret;
if (!buf)
return -EINVAL;
ret = dcmi_start_dma(dcmi, buf);
if (ret) {
dcmi->errors_count++;
return ret;
}
reg_set(dcmi->regs, DCMI_CR, CR_CAPTURE);
return 0;
}
static void dcmi_set_crop(struct stm32_dcmi *dcmi)
{
u32 size, start;
size = ((dcmi->crop.height - 1) << 16) |
((dcmi->crop.width << 1) - 1);
reg_write(dcmi->regs, DCMI_CWSIZE, size);
start = ((dcmi->crop.top) << 16) |
((dcmi->crop.left << 1));
reg_write(dcmi->regs, DCMI_CWSTRT, start);
dev_dbg(dcmi->dev, "Cropping to (%d,%d)/%ux%u\n",
dcmi->crop.left, dcmi->crop.top,
dcmi->crop.width, dcmi->crop.height);
reg_set(dcmi->regs, DCMI_CR, CR_CROP);
}
static void dcmi_process_frame(struct stm32_dcmi *dcmi)
{
struct dma_tx_state state, state_dma;
size_t bytes_used;
enum dma_status status;
struct dcmi_buf *buf = dcmi->active;
if (!buf)
return;
if (dcmi->mdma_chan) {
spin_unlock_irq(&dcmi->irqlock);
dmaengine_pause(dcmi->dma_chan);
spin_lock_irq(&dcmi->irqlock);
do {
status = dmaengine_tx_status(dcmi->mdma_chan, dcmi->mdma_cookie, &state);
cpu_relax();
} while (status != DMA_ERROR && status != DMA_COMPLETE &&
state.residue && state.in_flight_bytes);
} else {
status = dmaengine_tx_status(dcmi->dma_chan, dcmi->dma_cookie, &state);
}
if (status != DMA_ERROR && state.residue < buf->size) {
bytes_used = buf->size - state.residue;
if (state.residue && dcmi->mdma_chan) {
dmaengine_tx_status(dcmi->dma_chan, dcmi->dma_cookie, &state_dma);
if (state_dma.residue == dcmi->sram_buf_size)
state_dma.residue = 0;
bytes_used -= state_dma.residue;
}
dcmi_buffer_done(dcmi, buf, bytes_used, 0);
} else {
dcmi->errors_count++;
dev_err(dcmi->dev, "%s: DMA error. status: 0x%x, residue: %d\n",
__func__, status, state.residue);
dcmi_buffer_done(dcmi, buf, 0, -EIO);
}
dmaengine_terminate_async(dcmi->dma_chan);
if (dcmi->mdma_chan)
dmaengine_terminate_async(dcmi->mdma_chan);
}
static irqreturn_t dcmi_irq_thread(int irq, void *arg)
{
struct stm32_dcmi *dcmi = arg;
spin_lock_irq(&dcmi->irqlock);
if (dcmi->misr & IT_OVR) {
reg_clear(dcmi->regs, DCMI_CR, CR_CAPTURE);
dcmi->overrun_count++;
if (dcmi->overrun_count > OVERRUN_ERROR_THRESHOLD)
dcmi->errors_count++;
dmaengine_terminate_async(dcmi->dma_chan);
if (dcmi->mdma_chan)
dmaengine_terminate_async(dcmi->mdma_chan);
if (dcmi_restart_capture(dcmi))
dev_err(dcmi->dev, "%s: Cannot restart capture\n", __func__);
spin_unlock_irq(&dcmi->irqlock);
return IRQ_HANDLED;
}
if (dcmi->misr & IT_ERR)
dcmi->errors_count++;
if (dcmi->misr & IT_FRAME) {
dcmi_process_frame(dcmi);
if (dcmi_restart_capture(dcmi))
dev_err(dcmi->dev, "%s: Cannot restart capture\n", __func__);
}
spin_unlock_irq(&dcmi->irqlock);
return IRQ_HANDLED;
}
static irqreturn_t dcmi_irq_callback(int irq, void *arg)
{
struct stm32_dcmi *dcmi = arg;
unsigned long flags;
spin_lock_irqsave(&dcmi->irqlock, flags);
dcmi->misr = reg_read(dcmi->regs, DCMI_MIS);
reg_set(dcmi->regs, DCMI_ICR, IT_FRAME | IT_OVR | IT_ERR);
spin_unlock_irqrestore(&dcmi->irqlock, flags);
return IRQ_WAKE_THREAD;
}
static int dcmi_queue_setup(struct vb2_queue *vq,
unsigned int *nbuffers,
unsigned int *nplanes,
unsigned int sizes[],
struct device *alloc_devs[])
{
struct stm32_dcmi *dcmi = vb2_get_drv_priv(vq);
unsigned int size;
size = dcmi->fmt.fmt.pix.sizeimage;
if (*nplanes)
return sizes[0] < size ? -EINVAL : 0;
*nplanes = 1;
sizes[0] = size;
dev_dbg(dcmi->dev, "Setup queue, count=%d, size=%d\n",
*nbuffers, size);
return 0;
}
static int dcmi_buf_init(struct vb2_buffer *vb)
{
struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
struct dcmi_buf *buf = container_of(vbuf, struct dcmi_buf, vb);
INIT_LIST_HEAD(&buf->list);
return 0;
}
static int dcmi_buf_prepare(struct vb2_buffer *vb)
{
struct stm32_dcmi *dcmi = vb2_get_drv_priv(vb->vb2_queue);
struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
struct dcmi_buf *buf = container_of(vbuf, struct dcmi_buf, vb);
unsigned long size;
unsigned int num_sgs = 1;
dma_addr_t dma_buf;
struct scatterlist *sg;
int i, ret;
size = dcmi->fmt.fmt.pix.sizeimage;
if (vb2_plane_size(vb, 0) < size) {
dev_err(dcmi->dev, "%s data will not fit into plane (%lu < %lu)\n",
__func__, vb2_plane_size(vb, 0), size);
return -EINVAL;
}
vb2_set_plane_payload(vb, 0, size);
if (!buf->prepared) {
u32 max_size = dcmi->dma_max_burst;
unsigned int dma_nents;
buf->size = vb2_plane_size(&buf->vb.vb2_buf, 0);
if (dcmi->mdma_chan)
max_size = dcmi->sram_buf_size / 2;
if (buf->size > max_size)
num_sgs = DIV_ROUND_UP(buf->size, max_size);
dma_nents = num_sgs;
if (dcmi->mdma_chan)
dma_nents = min_t(unsigned int, num_sgs, 2);
ret = sg_alloc_table(&buf->sgt, dma_nents, GFP_ATOMIC);
if (ret) {
dev_err(dcmi->dev, "sg table alloc failed for DMA\n");
return ret;
}
if (dcmi->mdma_chan) {
ret = sg_alloc_table(&buf->sgt_mdma, num_sgs, GFP_ATOMIC);
if (ret) {
dev_err(dcmi->dev, "sg table alloc failed for MDMA\n");
return ret;
}
}
dma_buf = vb2_dma_contig_plane_dma_addr(&buf->vb.vb2_buf, 0);
dev_dbg(dcmi->dev, "buffer[%d] phy=%pad size=%zu\n",
vb->index, &dma_buf, buf->size);
for_each_sg(buf->sgt.sgl, sg, dma_nents, i) {
size_t bytes = min_t(size_t, size, max_size);
if (!dcmi->mdma_chan) {
sg_dma_address(sg) = dma_buf;
dma_buf += bytes;
} else {
sg_dma_address(sg) = dcmi->sram_dma_buf;
if (i & 1)
sg->dma_address += (dcmi->sram_buf_size / 2);
}
if (!dcmi->mdma_chan)
sg_dma_len(sg) = bytes;
else
sg_dma_len(sg) = dcmi->sram_buf_size / 2;
size -= bytes;
}
buf->dma_desc = dmaengine_prep_slave_sg(dcmi->dma_chan,
buf->sgt.sgl,
buf->sgt.nents,
DMA_DEV_TO_MEM,
DMA_PREP_INTERRUPT);
if (!buf->dma_desc) {
dev_err(dcmi->dev, "%s: DMA dmaengine_prep_slave_sg failed\n", __func__);
if (dcmi->mdma_chan)
sg_free_table(&buf->sgt_mdma);
sg_free_table(&buf->sgt);
return -EIO;
}
ret = dmaengine_desc_set_reuse(buf->dma_desc);
if (ret) {
dev_err(dcmi->dev, "%s: DMA dmaengine_desc_set_reuse failed\n", __func__);
dmaengine_desc_free(buf->dma_desc);
if (dcmi->mdma_chan)
sg_free_table(&buf->sgt_mdma);
sg_free_table(&buf->sgt);
return -EIO;
}
if (dcmi->mdma_chan) {
size = dcmi->fmt.fmt.pix.sizeimage;
for_each_sg(buf->sgt_mdma.sgl, sg, num_sgs, i) {
size_t bytes = min_t(size_t, size, max_size);
sg_dma_address(sg) = dma_buf;
sg_dma_len(sg) = bytes;
dma_buf += bytes;
size -= bytes;
}
buf->mdma_desc = dmaengine_prep_slave_sg(dcmi->mdma_chan, buf->sgt_mdma.sgl,
buf->sgt_mdma.nents,
DMA_DEV_TO_MEM,
DMA_PREP_INTERRUPT);
if (!buf->mdma_desc) {
dev_err(dcmi->dev, "%s: failed dmaengine_prep_slave_sg for MDMA\n",
__func__);
dmaengine_desc_free(buf->dma_desc);
sg_free_table(&buf->sgt_mdma);
sg_free_table(&buf->sgt);
return -EIO;
}
ret = dmaengine_desc_set_reuse(buf->mdma_desc);
if (ret) {
dev_err(dcmi->dev, "%s: failed to set reuse for mdma desc\n",
__func__);
dmaengine_desc_free(buf->mdma_desc);
dmaengine_desc_free(buf->dma_desc);
sg_free_table(&buf->sgt_mdma);
sg_free_table(&buf->sgt);
return -EIO;
}
}
buf->prepared = true;
vb2_set_plane_payload(&buf->vb.vb2_buf, 0, buf->size);
}
return 0;
}
static void dcmi_buf_cleanup(struct vb2_buffer *vb)
{
struct stm32_dcmi *dcmi = vb2_get_drv_priv(vb->vb2_queue);
struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
struct dcmi_buf *buf = container_of(vbuf, struct dcmi_buf, vb);
int ret;
if (!buf->prepared)
return;
if (dcmi->mdma_chan) {
ret = dmaengine_desc_free(buf->mdma_desc);
if (ret)
dev_err(dcmi->dev, "%s: Failed to free the mdma descriptor (0x%x)\n",
__func__, ret);
sg_free_table(&buf->sgt_mdma);
}
ret = dmaengine_desc_free(buf->dma_desc);
if (ret)
dev_err(dcmi->dev, "%s: Failed to free the mdma descriptor (0x%x)\n",
__func__, ret);
sg_free_table(&buf->sgt);
}
static void dcmi_buf_queue(struct vb2_buffer *vb)
{
struct stm32_dcmi *dcmi = vb2_get_drv_priv(vb->vb2_queue);
struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
struct dcmi_buf *buf = container_of(vbuf, struct dcmi_buf, vb);
spin_lock_irq(&dcmi->irqlock);
list_add_tail(&buf->list, &dcmi->buffers);
if (dcmi->state == WAIT_FOR_BUFFER) {
dcmi->state = RUNNING;
dcmi->active = buf;
dev_dbg(dcmi->dev, "Starting capture on buffer[%d] queued\n",
buf->vb.vb2_buf.index);
if (dcmi_start_capture(dcmi, buf))
dev_err(dcmi->dev, "%s: Cannot restart capture on overflow or error\n",
__func__);
}
spin_unlock_irq(&dcmi->irqlock);
}
static struct media_entity *dcmi_find_source(struct stm32_dcmi *dcmi)
{
struct media_entity *entity = &dcmi->vdev->entity;
struct media_pad *pad;
while (1) {
pad = &entity->pads[0];
if (!(pad->flags & MEDIA_PAD_FL_SINK))
break;
pad = media_pad_remote_pad_first(pad);
if (!pad || !is_media_entity_v4l2_subdev(pad->entity))
break;
entity = pad->entity;
}
return entity;
}
static int dcmi_pipeline_s_fmt(struct stm32_dcmi *dcmi,
struct v4l2_subdev_format *format)
{
struct media_entity *entity = &dcmi->source->entity;
struct v4l2_subdev *subdev;
struct media_pad *sink_pad = NULL;
struct media_pad *src_pad = NULL;
struct media_pad *pad = NULL;
struct v4l2_subdev_format fmt = *format;
bool found = false;
int ret;
while (1) {
unsigned int i;
for (i = 0; i < entity->num_pads; i++) {
pad = &entity->pads[i];
if (pad->flags & MEDIA_PAD_FL_SOURCE) {
src_pad = pad;
found = true;
break;
}
}
if (!found)
break;
subdev = media_entity_to_v4l2_subdev(entity);
if (sink_pad)
pad = sink_pad;
dev_dbg(dcmi->dev, "\"%s\":%d pad format set to 0x%x %ux%u\n",
subdev->name, pad->index, format->format.code,
format->format.width, format->format.height);
fmt.pad = pad->index;
ret = v4l2_subdev_call(subdev, pad, set_fmt, NULL, NULL, &fmt);
if (ret < 0) {
dev_err(dcmi->dev, "%s: Failed to set format 0x%x %ux%u on \"%s\":%d pad (%d)\n",
__func__, format->format.code,
format->format.width, format->format.height,
subdev->name, pad->index, ret);
return ret;
}
if (fmt.format.code != format->format.code ||
fmt.format.width != format->format.width ||
fmt.format.height != format->format.height) {
dev_dbg(dcmi->dev, "\"%s\":%d pad format has been changed to 0x%x %ux%u\n",
subdev->name, pad->index, fmt.format.code,
fmt.format.width, fmt.format.height);
}
sink_pad = media_pad_remote_pad_first(src_pad);
if (!sink_pad || !is_media_entity_v4l2_subdev(sink_pad->entity))
break;
entity = sink_pad->entity;
}
*format = fmt;
return 0;
}
static int dcmi_start_streaming(struct vb2_queue *vq, unsigned int count)
{
struct stm32_dcmi *dcmi = vb2_get_drv_priv(vq);
struct dcmi_buf *buf, *node;
u32 val = 0;
int ret;
ret = pm_runtime_resume_and_get(dcmi->dev);
if (ret < 0) {
dev_err(dcmi->dev, "%s: Failed to start streaming, cannot get sync (%d)\n",
__func__, ret);
goto err_unlocked;
}
ret = video_device_pipeline_start(dcmi->vdev, &dcmi->pipeline);
if (ret < 0) {
dev_err(dcmi->dev, "%s: Failed to start streaming, media pipeline start error (%d)\n",
__func__, ret);
goto err_pm_put;
}
ret = v4l2_subdev_call(dcmi->s_subdev, video, s_stream, 1);
if (ret < 0) {
dev_err(dcmi->dev, "%s: Failed to start source subdev, error (%d)\n",
__func__, ret);
goto err_media_pipeline_stop;
}
spin_lock_irq(&dcmi->irqlock);
switch (dcmi->bus.bus_width) {
case 14:
val |= CR_EDM_0 | CR_EDM_1;
break;
case 12:
val |= CR_EDM_1;
break;
case 10:
val |= CR_EDM_0;
break;
default:
break;
}
if (dcmi->bus.flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH)
val |= CR_VSPOL;
if (dcmi->bus.flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH)
val |= CR_HSPOL;
if (dcmi->bus.flags & V4L2_MBUS_PCLK_SAMPLE_RISING)
val |= CR_PCKPOL;
if (dcmi->bus_type == V4L2_MBUS_BT656) {
val |= CR_ESS;
reg_write(dcmi->regs, DCMI_ESUR, 0xffffffff);
reg_write(dcmi->regs, DCMI_ESCR, 0xff9d80ff);
}
reg_write(dcmi->regs, DCMI_CR, val);
if (dcmi->do_crop)
dcmi_set_crop(dcmi);
reg_set(dcmi->regs, DCMI_CR, CR_ENABLE);
dcmi->sequence = 0;
dcmi->errors_count = 0;
dcmi->overrun_count = 0;
dcmi->buffers_count = 0;
if (list_empty(&dcmi->buffers)) {
dev_dbg(dcmi->dev, "Start streaming is deferred to next buffer queueing\n");
dcmi->state = WAIT_FOR_BUFFER;
spin_unlock_irq(&dcmi->irqlock);
return 0;
}
buf = list_entry(dcmi->buffers.next, struct dcmi_buf, list);
dcmi->active = buf;
dcmi->state = RUNNING;
dev_dbg(dcmi->dev, "Start streaming, starting capture\n");
ret = dcmi_start_capture(dcmi, buf);
if (ret) {
dev_err(dcmi->dev, "%s: Start streaming failed, cannot start capture\n",
__func__);
spin_unlock_irq(&dcmi->irqlock);
goto err_pipeline_stop;
}
reg_set(dcmi->regs, DCMI_IER, IT_FRAME | IT_OVR | IT_ERR);
spin_unlock_irq(&dcmi->irqlock);
return 0;
err_pipeline_stop:
v4l2_subdev_call(dcmi->s_subdev, video, s_stream, 0);
err_media_pipeline_stop:
video_device_pipeline_stop(dcmi->vdev);
err_pm_put:
pm_runtime_put(dcmi->dev);
err_unlocked:
spin_lock_irq(&dcmi->irqlock);
list_for_each_entry_safe(buf, node, &dcmi->buffers, list) {
list_del_init(&buf->list);
vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_QUEUED);
}
dcmi->active = NULL;
spin_unlock_irq(&dcmi->irqlock);
return ret;
}
static void dcmi_stop_streaming(struct vb2_queue *vq)
{
struct stm32_dcmi *dcmi = vb2_get_drv_priv(vq);
struct dcmi_buf *buf, *node;
int ret;
ret = v4l2_subdev_call(dcmi->s_subdev, video, s_stream, 0);
if (ret < 0)
dev_err(dcmi->dev, "%s: Failed to stop source subdev, error (%d)\n",
__func__, ret);
video_device_pipeline_stop(dcmi->vdev);
spin_lock_irq(&dcmi->irqlock);
reg_clear(dcmi->regs, DCMI_IER, IT_FRAME | IT_OVR | IT_ERR);
reg_clear(dcmi->regs, DCMI_CR, CR_ENABLE);
list_for_each_entry_safe(buf, node, &dcmi->buffers, list) {
list_del_init(&buf->list);
vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
}
dcmi->active = NULL;
dcmi->state = STOPPED;
spin_unlock_irq(&dcmi->irqlock);
dmaengine_terminate_sync(dcmi->dma_chan);
if (dcmi->mdma_chan)
dmaengine_terminate_sync(dcmi->mdma_chan);
pm_runtime_put(dcmi->dev);
if (dcmi->errors_count)
dev_warn(dcmi->dev, "Some errors found while streaming: errors=%d (overrun=%d), buffers=%d\n",
dcmi->errors_count, dcmi->overrun_count,
dcmi->buffers_count);
dev_dbg(dcmi->dev, "Stop streaming, errors=%d (overrun=%d), buffers=%d\n",
dcmi->errors_count, dcmi->overrun_count,
dcmi->buffers_count);
}
static const struct vb2_ops dcmi_video_qops = {
.queue_setup = dcmi_queue_setup,
.buf_init = dcmi_buf_init,
.buf_prepare = dcmi_buf_prepare,
.buf_cleanup = dcmi_buf_cleanup,
.buf_queue = dcmi_buf_queue,
.start_streaming = dcmi_start_streaming,
.stop_streaming = dcmi_stop_streaming,
};
static int dcmi_g_fmt_vid_cap(struct file *file, void *priv,
struct v4l2_format *fmt)
{
struct stm32_dcmi *dcmi = video_drvdata(file);
*fmt = dcmi->fmt;
return 0;
}
static const struct dcmi_format *find_format_by_fourcc(struct stm32_dcmi *dcmi,
unsigned int fourcc)
{
unsigned int num_formats = dcmi->num_of_sd_formats;
const struct dcmi_format *fmt;
unsigned int i;
for (i = 0; i < num_formats; i++) {
fmt = dcmi->sd_formats[i];
if (fmt->fourcc == fourcc)
return fmt;
}
return NULL;
}
static void __find_outer_frame_size(struct stm32_dcmi *dcmi,
struct v4l2_pix_format *pix,
struct dcmi_framesize *framesize)
{
struct dcmi_framesize *match = NULL;
unsigned int i;
unsigned int min_err = UINT_MAX;
for (i = 0; i < dcmi->num_of_sd_framesizes; i++) {
struct dcmi_framesize *fsize = &dcmi->sd_framesizes[i];
int w_err = (fsize->width - pix->width);
int h_err = (fsize->height - pix->height);
int err = w_err + h_err;
if (w_err >= 0 && h_err >= 0 && err < min_err) {
min_err = err;
match = fsize;
}
}
if (!match)
match = &dcmi->sd_framesizes[0];
*framesize = *match;
}
static int dcmi_source_call_try_state_set_fmt(struct v4l2_subdev *source,
struct v4l2_subdev_format *fmt)
{
static struct lock_class_key lock_key;
const char *lock_name = KBUILD_BASENAME ":" __stringify(__LINE__)
":state->lock";
struct v4l2_subdev_state *source_state;
int ret;
source_state = __v4l2_subdev_state_alloc(source, lock_name, &lock_key);
if (IS_ERR(source_state))
return PTR_ERR(source_state);
v4l2_subdev_lock_state(source_state);
ret = v4l2_subdev_call(source, pad, set_fmt, NULL, source_state, fmt);
v4l2_subdev_unlock_state(source_state);
__v4l2_subdev_state_free(source_state);
return ret;
}
static int dcmi_try_fmt(struct stm32_dcmi *dcmi, struct v4l2_format *f,
const struct dcmi_format **sd_format,
struct dcmi_framesize *sd_framesize)
{
const struct dcmi_format *sd_fmt;
struct dcmi_framesize sd_fsize;
struct v4l2_pix_format *pix = &f->fmt.pix;
struct v4l2_subdev_format format = {
.which = V4L2_SUBDEV_FORMAT_TRY,
};
bool do_crop;
int ret;
sd_fmt = find_format_by_fourcc(dcmi, pix->pixelformat);
if (!sd_fmt) {
if (!dcmi->num_of_sd_formats)
return -ENODATA;
sd_fmt = dcmi->sd_formats[dcmi->num_of_sd_formats - 1];
pix->pixelformat = sd_fmt->fourcc;
}
pix->width = clamp(pix->width, MIN_WIDTH, MAX_WIDTH);
pix->height = clamp(pix->height, MIN_HEIGHT, MAX_HEIGHT);
do_crop = dcmi->do_crop && (pix->pixelformat != V4L2_PIX_FMT_JPEG);
if (do_crop && dcmi->num_of_sd_framesizes) {
struct dcmi_framesize outer_sd_fsize;
__find_outer_frame_size(dcmi, pix, &outer_sd_fsize);
pix->width = outer_sd_fsize.width;
pix->height = outer_sd_fsize.height;
}
v4l2_fill_mbus_format(&format.format, pix, sd_fmt->mbus_code);
ret = dcmi_source_call_try_state_set_fmt(dcmi->source, &format);
if (ret)
return ret;
v4l2_fill_pix_format(pix, &format.format);
sd_fsize.width = pix->width;
sd_fsize.height = pix->height;
if (do_crop) {
struct v4l2_rect c = dcmi->crop;
struct v4l2_rect max_rect;
max_rect.top = 0;
max_rect.left = 0;
max_rect.width = pix->width;
max_rect.height = pix->height;
v4l2_rect_map_inside(&c, &max_rect);
c.top = clamp_t(s32, c.top, 0, pix->height - c.height);
c.left = clamp_t(s32, c.left, 0, pix->width - c.width);
dcmi->crop = c;
pix->width = dcmi->crop.width;
pix->height = dcmi->crop.height;
}
pix->field = V4L2_FIELD_NONE;
pix->bytesperline = pix->width * sd_fmt->bpp;
pix->sizeimage = pix->bytesperline * pix->height;
if (sd_format)
*sd_format = sd_fmt;
if (sd_framesize)
*sd_framesize = sd_fsize;
return 0;
}
static int dcmi_set_fmt(struct stm32_dcmi *dcmi, struct v4l2_format *f)
{
struct v4l2_subdev_format format = {
.which = V4L2_SUBDEV_FORMAT_ACTIVE,
};
const struct dcmi_format *sd_format;
struct dcmi_framesize sd_framesize;
struct v4l2_mbus_framefmt *mf = &format.format;
struct v4l2_pix_format *pix = &f->fmt.pix;
int ret;
ret = dcmi_try_fmt(dcmi, f, &sd_format, &sd_framesize);
if (ret)
return ret;
if (pix->pixelformat == V4L2_PIX_FMT_JPEG &&
dcmi->bus_type != V4L2_MBUS_BT656)
dcmi->do_crop = false;
v4l2_fill_mbus_format(mf, pix,
sd_format->mbus_code);
mf->width = sd_framesize.width;
mf->height = sd_framesize.height;
ret = dcmi_pipeline_s_fmt(dcmi, &format);
if (ret < 0)
return ret;
dev_dbg(dcmi->dev, "Sensor format set to 0x%x %ux%u\n",
mf->code, mf->width, mf->height);
dev_dbg(dcmi->dev, "Buffer format set to %4.4s %ux%u\n",
(char *)&pix->pixelformat,
pix->width, pix->height);
dcmi->fmt = *f;
dcmi->sd_format = sd_format;
dcmi->sd_framesize = sd_framesize;
return 0;
}
static int dcmi_s_fmt_vid_cap(struct file *file, void *priv,
struct v4l2_format *f)
{
struct stm32_dcmi *dcmi = video_drvdata(file);
if (vb2_is_streaming(&dcmi->queue))
return -EBUSY;
return dcmi_set_fmt(dcmi, f);
}
static int dcmi_try_fmt_vid_cap(struct file *file, void *priv,
struct v4l2_format *f)
{
struct stm32_dcmi *dcmi = video_drvdata(file);
return dcmi_try_fmt(dcmi, f, NULL, NULL);
}
static int dcmi_enum_fmt_vid_cap(struct file *file, void *priv,
struct v4l2_fmtdesc *f)
{
struct stm32_dcmi *dcmi = video_drvdata(file);
if (f->index >= dcmi->num_of_sd_formats)
return -EINVAL;
f->pixelformat = dcmi->sd_formats[f->index]->fourcc;
return 0;
}
static int dcmi_get_sensor_format(struct stm32_dcmi *dcmi,
struct v4l2_pix_format *pix)
{
struct v4l2_subdev_format fmt = {
.which = V4L2_SUBDEV_FORMAT_ACTIVE,
};
int ret;
ret = v4l2_subdev_call(dcmi->source, pad, get_fmt, NULL, &fmt);
if (ret)
return ret;
v4l2_fill_pix_format(pix, &fmt.format);
return 0;
}
static int dcmi_set_sensor_format(struct stm32_dcmi *dcmi,
struct v4l2_pix_format *pix)
{
const struct dcmi_format *sd_fmt;
struct v4l2_subdev_format format = {
.which = V4L2_SUBDEV_FORMAT_TRY,
};
int ret;
sd_fmt = find_format_by_fourcc(dcmi, pix->pixelformat);
if (!sd_fmt) {
if (!dcmi->num_of_sd_formats)
return -ENODATA;
sd_fmt = dcmi->sd_formats[dcmi->num_of_sd_formats - 1];
pix->pixelformat = sd_fmt->fourcc;
}
v4l2_fill_mbus_format(&format.format, pix, sd_fmt->mbus_code);
ret = dcmi_source_call_try_state_set_fmt(dcmi->source, &format);
if (ret < 0)
return ret;
return 0;
}
static int dcmi_get_sensor_bounds(struct stm32_dcmi *dcmi,
struct v4l2_rect *r)
{
struct v4l2_subdev_selection bounds = {
.which = V4L2_SUBDEV_FORMAT_ACTIVE,
.target = V4L2_SEL_TGT_CROP_BOUNDS,
};
unsigned int max_width, max_height, max_pixsize;
struct v4l2_pix_format pix;
unsigned int i;
int ret;
ret = v4l2_subdev_call(dcmi->source, pad, get_selection, NULL,
NULL, &bounds);
if (!ret)
*r = bounds.r;
if (ret != -ENOIOCTLCMD)
return ret;
max_width = 0;
max_height = 0;
max_pixsize = 0;
for (i = 0; i < dcmi->num_of_sd_framesizes; i++) {
struct dcmi_framesize *fsize = &dcmi->sd_framesizes[i];
unsigned int pixsize = fsize->width * fsize->height;
if (pixsize > max_pixsize) {
max_pixsize = pixsize;
max_width = fsize->width;
max_height = fsize->height;
}
}
if (max_pixsize > 0) {
r->top = 0;
r->left = 0;
r->width = max_width;
r->height = max_height;
return 0;
}
ret = dcmi_get_sensor_format(dcmi, &pix);
if (ret)
return ret;
r->top = 0;
r->left = 0;
r->width = pix.width;
r->height = pix.height;
return 0;
}
static int dcmi_g_selection(struct file *file, void *fh,
struct v4l2_selection *s)
{
struct stm32_dcmi *dcmi = video_drvdata(file);
if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
return -EINVAL;
switch (s->target) {
case V4L2_SEL_TGT_CROP_DEFAULT:
case V4L2_SEL_TGT_CROP_BOUNDS:
s->r = dcmi->sd_bounds;
return 0;
case V4L2_SEL_TGT_CROP:
if (dcmi->do_crop) {
s->r = dcmi->crop;
} else {
s->r.top = 0;
s->r.left = 0;
s->r.width = dcmi->fmt.fmt.pix.width;
s->r.height = dcmi->fmt.fmt.pix.height;
}
break;
default:
return -EINVAL;
}
return 0;
}
static int dcmi_s_selection(struct file *file, void *priv,
struct v4l2_selection *s)
{
struct stm32_dcmi *dcmi = video_drvdata(file);
struct v4l2_rect r = s->r;
struct v4l2_rect max_rect;
struct v4l2_pix_format pix;
if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
s->target != V4L2_SEL_TGT_CROP)
return -EINVAL;
pix.pixelformat = dcmi->fmt.fmt.pix.pixelformat;
pix.width = dcmi->sd_bounds.width;
pix.height = dcmi->sd_bounds.height;
dcmi_set_sensor_format(dcmi, &pix);
max_rect.top = 0;
max_rect.left = 0;
max_rect.width = pix.width;
max_rect.height = pix.height;
v4l2_rect_map_inside(&r, &max_rect);
r.top = clamp_t(s32, r.top, 0, pix.height - r.height);
r.left = clamp_t(s32, r.left, 0, pix.width - r.width);
if (!(r.top == dcmi->sd_bounds.top &&
r.left == dcmi->sd_bounds.left &&
r.width == dcmi->sd_bounds.width &&
r.height == dcmi->sd_bounds.height)) {
dcmi->do_crop = true;
dcmi->crop = r;
dev_dbg(dcmi->dev, "s_selection: crop (%d,%d)/%ux%u from %ux%u\n",
r.left, r.top, r.width, r.height,
pix.width, pix.height);
} else {
dcmi->do_crop = false;
dev_dbg(dcmi->dev, "s_selection: crop is disabled\n");
}
s->r = r;
return 0;
}
static int dcmi_querycap(struct file *file, void *priv,
struct v4l2_capability *cap)
{
strscpy(cap->driver, DRV_NAME, sizeof(cap->driver));
strscpy(cap->card, "STM32 Camera Memory Interface",
sizeof(cap->card));
strscpy(cap->bus_info, "platform:dcmi", sizeof(cap->bus_info));
return 0;
}
static int dcmi_enum_input(struct file *file, void *priv,
struct v4l2_input *i)
{
if (i->index != 0)
return -EINVAL;
i->type = V4L2_INPUT_TYPE_CAMERA;
strscpy(i->name, "Camera", sizeof(i->name));
return 0;
}
static int dcmi_g_input(struct file *file, void *priv, unsigned int *i)
{
*i = 0;
return 0;
}
static int dcmi_s_input(struct file *file, void *priv, unsigned int i)
{
if (i > 0)
return -EINVAL;
return 0;
}
static int dcmi_enum_framesizes(struct file *file, void *fh,
struct v4l2_frmsizeenum *fsize)
{
struct stm32_dcmi *dcmi = video_drvdata(file);
const struct dcmi_format *sd_fmt;
struct v4l2_subdev_frame_size_enum fse = {
.index = fsize->index,
.which = V4L2_SUBDEV_FORMAT_ACTIVE,
};
int ret;
sd_fmt = find_format_by_fourcc(dcmi, fsize->pixel_format);
if (!sd_fmt)
return -EINVAL;
fse.code = sd_fmt->mbus_code;
ret = v4l2_subdev_call(dcmi->source, pad, enum_frame_size,
NULL, &fse);
if (ret)
return ret;
fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
fsize->discrete.width = fse.max_width;
fsize->discrete.height = fse.max_height;
return 0;
}
static int dcmi_g_parm(struct file *file, void *priv,
struct v4l2_streamparm *p)
{
struct stm32_dcmi *dcmi = video_drvdata(file);
return v4l2_g_parm_cap(video_devdata(file), dcmi->source, p);
}
static int dcmi_s_parm(struct file *file, void *priv,
struct v4l2_streamparm *p)
{
struct stm32_dcmi *dcmi = video_drvdata(file);
return v4l2_s_parm_cap(video_devdata(file), dcmi->source, p);
}
static int dcmi_enum_frameintervals(struct file *file, void *fh,
struct v4l2_frmivalenum *fival)
{
struct stm32_dcmi *dcmi = video_drvdata(file);
const struct dcmi_format *sd_fmt;
struct v4l2_subdev_frame_interval_enum fie = {
.index = fival->index,
.width = fival->width,
.height = fival->height,
.which = V4L2_SUBDEV_FORMAT_ACTIVE,
};
int ret;
sd_fmt = find_format_by_fourcc(dcmi, fival->pixel_format);
if (!sd_fmt)
return -EINVAL;
fie.code = sd_fmt->mbus_code;
ret = v4l2_subdev_call(dcmi->source, pad,
enum_frame_interval, NULL, &fie);
if (ret)
return ret;
fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
fival->discrete = fie.interval;
return 0;
}
static const struct of_device_id stm32_dcmi_of_match[] = {
{ .compatible = "st,stm32-dcmi"},
{ },
};
MODULE_DEVICE_TABLE(of, stm32_dcmi_of_match);
static int dcmi_open(struct file *file)
{
struct stm32_dcmi *dcmi = video_drvdata(file);
struct v4l2_subdev *sd = dcmi->source;
int ret;
if (mutex_lock_interruptible(&dcmi->lock))
return -ERESTARTSYS;
ret = v4l2_fh_open(file);
if (ret < 0)
goto unlock;
if (!v4l2_fh_is_singular_file(file))
goto fh_rel;
ret = v4l2_subdev_call(sd, core, s_power, 1);
if (ret < 0 && ret != -ENOIOCTLCMD)
goto fh_rel;
ret = dcmi_set_fmt(dcmi, &dcmi->fmt);
if (ret)
v4l2_subdev_call(sd, core, s_power, 0);
fh_rel:
if (ret)
v4l2_fh_release(file);
unlock:
mutex_unlock(&dcmi->lock);
return ret;
}
static int dcmi_release(struct file *file)
{
struct stm32_dcmi *dcmi = video_drvdata(file);
struct v4l2_subdev *sd = dcmi->source;
bool fh_singular;
int ret;
mutex_lock(&dcmi->lock);
fh_singular = v4l2_fh_is_singular_file(file);
ret = _vb2_fop_release(file, NULL);
if (fh_singular)
v4l2_subdev_call(sd, core, s_power, 0);
mutex_unlock(&dcmi->lock);
return ret;
}
static const struct v4l2_ioctl_ops dcmi_ioctl_ops = {
.vidioc_querycap = dcmi_querycap,
.vidioc_try_fmt_vid_cap = dcmi_try_fmt_vid_cap,
.vidioc_g_fmt_vid_cap = dcmi_g_fmt_vid_cap,
.vidioc_s_fmt_vid_cap = dcmi_s_fmt_vid_cap,
.vidioc_enum_fmt_vid_cap = dcmi_enum_fmt_vid_cap,
.vidioc_g_selection = dcmi_g_selection,
.vidioc_s_selection = dcmi_s_selection,
.vidioc_enum_input = dcmi_enum_input,
.vidioc_g_input = dcmi_g_input,
.vidioc_s_input = dcmi_s_input,
.vidioc_g_parm = dcmi_g_parm,
.vidioc_s_parm = dcmi_s_parm,
.vidioc_enum_framesizes = dcmi_enum_framesizes,
.vidioc_enum_frameintervals = dcmi_enum_frameintervals,
.vidioc_reqbufs = vb2_ioctl_reqbufs,
.vidioc_create_bufs = vb2_ioctl_create_bufs,
.vidioc_querybuf = vb2_ioctl_querybuf,
.vidioc_qbuf = vb2_ioctl_qbuf,
.vidioc_dqbuf = vb2_ioctl_dqbuf,
.vidioc_expbuf = vb2_ioctl_expbuf,
.vidioc_prepare_buf = vb2_ioctl_prepare_buf,
.vidioc_streamon = vb2_ioctl_streamon,
.vidioc_streamoff = vb2_ioctl_streamoff,
.vidioc_log_status = v4l2_ctrl_log_status,
.vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
.vidioc_unsubscribe_event = v4l2_event_unsubscribe,
};
static const struct v4l2_file_operations dcmi_fops = {
.owner = THIS_MODULE,
.unlocked_ioctl = video_ioctl2,
.open = dcmi_open,
.release = dcmi_release,
.poll = vb2_fop_poll,
.mmap = vb2_fop_mmap,
#ifndef CONFIG_MMU
.get_unmapped_area = vb2_fop_get_unmapped_area,
#endif
.read = vb2_fop_read,
};
static int dcmi_set_default_fmt(struct stm32_dcmi *dcmi)
{
struct v4l2_format f = {
.type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
.fmt.pix = {
.width = CIF_WIDTH,
.height = CIF_HEIGHT,
.field = V4L2_FIELD_NONE,
.pixelformat = dcmi->sd_formats[0]->fourcc,
},
};
int ret;
ret = dcmi_try_fmt(dcmi, &f, NULL, NULL);
if (ret)
return ret;
dcmi->sd_format = dcmi->sd_formats[0];
dcmi->fmt = f;
return 0;
}
static const struct dcmi_format dcmi_formats[] = {
{
.fourcc = V4L2_PIX_FMT_RGB565,
.mbus_code = MEDIA_BUS_FMT_RGB565_2X8_LE,
.bpp = 2,
}, {
.fourcc = V4L2_PIX_FMT_RGB565,
.mbus_code = MEDIA_BUS_FMT_RGB565_1X16,
.bpp = 2,
}, {
.fourcc = V4L2_PIX_FMT_YUYV,
.mbus_code = MEDIA_BUS_FMT_YUYV8_2X8,
.bpp = 2,
}, {
.fourcc = V4L2_PIX_FMT_YUYV,
.mbus_code = MEDIA_BUS_FMT_YUYV8_1X16,
.bpp = 2,
}, {
.fourcc = V4L2_PIX_FMT_UYVY,
.mbus_code = MEDIA_BUS_FMT_UYVY8_2X8,
.bpp = 2,
}, {
.fourcc = V4L2_PIX_FMT_UYVY,
.mbus_code = MEDIA_BUS_FMT_UYVY8_1X16,
.bpp = 2,
}, {
.fourcc = V4L2_PIX_FMT_JPEG,
.mbus_code = MEDIA_BUS_FMT_JPEG_1X8,
.bpp = 1,
}, {
.fourcc = V4L2_PIX_FMT_SBGGR8,
.mbus_code = MEDIA_BUS_FMT_SBGGR8_1X8,
.bpp = 1,
}, {
.fourcc = V4L2_PIX_FMT_SGBRG8,
.mbus_code = MEDIA_BUS_FMT_SGBRG8_1X8,
.bpp = 1,
}, {
.fourcc = V4L2_PIX_FMT_SGRBG8,
.mbus_code = MEDIA_BUS_FMT_SGRBG8_1X8,
.bpp = 1,
}, {
.fourcc = V4L2_PIX_FMT_SRGGB8,
.mbus_code = MEDIA_BUS_FMT_SRGGB8_1X8,
.bpp = 1,
}, {
.fourcc = V4L2_PIX_FMT_SBGGR10,
.mbus_code = MEDIA_BUS_FMT_SBGGR10_1X10,
.bpp = 2,
}, {
.fourcc = V4L2_PIX_FMT_SGBRG10,
.mbus_code = MEDIA_BUS_FMT_SGBRG10_1X10,
.bpp = 2,
}, {
.fourcc = V4L2_PIX_FMT_SGRBG10,
.mbus_code = MEDIA_BUS_FMT_SGRBG10_1X10,
.bpp = 2,
}, {
.fourcc = V4L2_PIX_FMT_SRGGB10,
.mbus_code = MEDIA_BUS_FMT_SRGGB10_1X10,
.bpp = 2,
}, {
.fourcc = V4L2_PIX_FMT_SBGGR12,
.mbus_code = MEDIA_BUS_FMT_SBGGR12_1X12,
.bpp = 2,
}, {
.fourcc = V4L2_PIX_FMT_SGBRG12,
.mbus_code = MEDIA_BUS_FMT_SGBRG12_1X12,
.bpp = 2,
}, {
.fourcc = V4L2_PIX_FMT_SGRBG12,
.mbus_code = MEDIA_BUS_FMT_SGRBG12_1X12,
.bpp = 2,
}, {
.fourcc = V4L2_PIX_FMT_SRGGB12,
.mbus_code = MEDIA_BUS_FMT_SRGGB12_1X12,
.bpp = 2,
}, {
.fourcc = V4L2_PIX_FMT_SBGGR14,
.mbus_code = MEDIA_BUS_FMT_SBGGR14_1X14,
.bpp = 2,
}, {
.fourcc = V4L2_PIX_FMT_SGBRG14,
.mbus_code = MEDIA_BUS_FMT_SGBRG14_1X14,
.bpp = 2,
}, {
.fourcc = V4L2_PIX_FMT_SGRBG14,
.mbus_code = MEDIA_BUS_FMT_SGRBG14_1X14,
.bpp = 2,
}, {
.fourcc = V4L2_PIX_FMT_SRGGB14,
.mbus_code = MEDIA_BUS_FMT_SRGGB14_1X14,
.bpp = 2,
},
};
static int dcmi_formats_init(struct stm32_dcmi *dcmi)
{
const struct dcmi_format *sd_fmts[ARRAY_SIZE(dcmi_formats)];
unsigned int num_fmts = 0, i, j;
struct v4l2_subdev *subdev = dcmi->source;
struct v4l2_subdev_mbus_code_enum mbus_code = {
.which = V4L2_SUBDEV_FORMAT_ACTIVE,
};
while (!v4l2_subdev_call(subdev, pad, enum_mbus_code,
NULL, &mbus_code)) {
for (i = 0; i < ARRAY_SIZE(dcmi_formats); i++) {
if (dcmi_formats[i].mbus_code != mbus_code.code)
continue;
if (dcmi_formats[i].fourcc == V4L2_PIX_FMT_JPEG &&
dcmi->bus_type == V4L2_MBUS_BT656)
continue;
for (j = 0; j < num_fmts; j++)
if (sd_fmts[j]->fourcc ==
dcmi_formats[i].fourcc) {
dev_dbg(dcmi->dev, "Skipping fourcc/code: %4.4s/0x%x\n",
(char *)&sd_fmts[j]->fourcc,
mbus_code.code);
break;
}
if (j == num_fmts) {
sd_fmts[num_fmts++] = dcmi_formats + i;
dev_dbg(dcmi->dev, "Supported fourcc/code: %4.4s/0x%x\n",
(char *)&sd_fmts[num_fmts - 1]->fourcc,
sd_fmts[num_fmts - 1]->mbus_code);
}
}
mbus_code.index++;
}
if (!num_fmts)
return -ENXIO;
dcmi->num_of_sd_formats = num_fmts;
dcmi->sd_formats = devm_kmemdup_array(dcmi->dev, sd_fmts, num_fmts,
sizeof(*sd_fmts), GFP_KERNEL);
if (!dcmi->sd_formats) {
dev_err(dcmi->dev, "Could not allocate memory\n");
return -ENOMEM;
}
dcmi->sd_format = dcmi->sd_formats[0];
return 0;
}
static int dcmi_framesizes_init(struct stm32_dcmi *dcmi)
{
unsigned int num_fsize = 0;
struct v4l2_subdev *subdev = dcmi->source;
struct v4l2_subdev_frame_size_enum fse = {
.which = V4L2_SUBDEV_FORMAT_ACTIVE,
.code = dcmi->sd_format->mbus_code,
};
unsigned int i;
int ret;
while (!v4l2_subdev_call(subdev, pad, enum_frame_size,
NULL, &fse))
fse.index++;
num_fsize = fse.index;
if (!num_fsize)
return 0;
dcmi->num_of_sd_framesizes = num_fsize;
dcmi->sd_framesizes = devm_kzalloc_objs(dcmi->dev,
struct dcmi_framesize,
num_fsize);
if (!dcmi->sd_framesizes) {
dev_err(dcmi->dev, "Could not allocate memory\n");
return -ENOMEM;
}
dev_dbg(dcmi->dev, "Sensor supports %u frame sizes:\n", num_fsize);
for (i = 0; i < dcmi->num_of_sd_framesizes; i++) {
fse.index = i;
ret = v4l2_subdev_call(subdev, pad, enum_frame_size,
NULL, &fse);
if (ret)
return ret;
dcmi->sd_framesizes[fse.index].width = fse.max_width;
dcmi->sd_framesizes[fse.index].height = fse.max_height;
dev_dbg(dcmi->dev, "%ux%u\n", fse.max_width, fse.max_height);
}
return 0;
}
static int dcmi_graph_notify_complete(struct v4l2_async_notifier *notifier)
{
struct stm32_dcmi *dcmi = notifier_to_dcmi(notifier);
int ret;
dcmi->source = media_entity_to_v4l2_subdev(dcmi_find_source(dcmi));
if (!dcmi->source) {
dev_err(dcmi->dev, "Source subdevice not found\n");
return -ENODEV;
}
dcmi->vdev->ctrl_handler = dcmi->source->ctrl_handler;
ret = dcmi_formats_init(dcmi);
if (ret) {
dev_err(dcmi->dev, "No supported mediabus format found\n");
return ret;
}
ret = dcmi_framesizes_init(dcmi);
if (ret) {
dev_err(dcmi->dev, "Could not initialize framesizes\n");
return ret;
}
ret = dcmi_get_sensor_bounds(dcmi, &dcmi->sd_bounds);
if (ret) {
dev_err(dcmi->dev, "Could not get sensor bounds\n");
return ret;
}
ret = dcmi_set_default_fmt(dcmi);
if (ret) {
dev_err(dcmi->dev, "Could not set default format\n");
return ret;
}
ret = devm_request_threaded_irq(dcmi->dev, dcmi->irq, dcmi_irq_callback,
dcmi_irq_thread, IRQF_ONESHOT,
dev_name(dcmi->dev), dcmi);
if (ret) {
dev_err(dcmi->dev, "Unable to request irq %d\n", dcmi->irq);
return ret;
}
return 0;
}
static void dcmi_graph_notify_unbind(struct v4l2_async_notifier *notifier,
struct v4l2_subdev *sd,
struct v4l2_async_connection *asd)
{
struct stm32_dcmi *dcmi = notifier_to_dcmi(notifier);
dev_dbg(dcmi->dev, "Removing %s\n", video_device_node_name(dcmi->vdev));
video_unregister_device(dcmi->vdev);
}
static int dcmi_graph_notify_bound(struct v4l2_async_notifier *notifier,
struct v4l2_subdev *subdev,
struct v4l2_async_connection *asd)
{
struct stm32_dcmi *dcmi = notifier_to_dcmi(notifier);
int src_pad;
int ret;
dev_dbg(dcmi->dev, "Subdev \"%s\" bound\n", subdev->name);
src_pad = media_entity_get_fwnode_pad(&subdev->entity,
subdev->fwnode,
MEDIA_PAD_FL_SOURCE);
ret = media_create_pad_link(&subdev->entity, src_pad,
&dcmi->vdev->entity, 0,
MEDIA_LNK_FL_IMMUTABLE |
MEDIA_LNK_FL_ENABLED);
if (ret)
dev_err(dcmi->dev, "Failed to create media pad link with subdev \"%s\"\n",
subdev->name);
else
dev_dbg(dcmi->dev, "DCMI is now linked to \"%s\"\n",
subdev->name);
dcmi->s_subdev = subdev;
return ret;
}
static const struct v4l2_async_notifier_operations dcmi_graph_notify_ops = {
.bound = dcmi_graph_notify_bound,
.unbind = dcmi_graph_notify_unbind,
.complete = dcmi_graph_notify_complete,
};
static int dcmi_graph_init(struct stm32_dcmi *dcmi)
{
struct v4l2_async_connection *asd;
struct device_node *ep;
int ret;
ep = of_graph_get_endpoint_by_regs(dcmi->dev->of_node, 0, -1);
if (!ep) {
dev_err(dcmi->dev, "Failed to get next endpoint\n");
return -EINVAL;
}
v4l2_async_nf_init(&dcmi->notifier, &dcmi->v4l2_dev);
asd = v4l2_async_nf_add_fwnode_remote(&dcmi->notifier,
of_fwnode_handle(ep),
struct v4l2_async_connection);
of_node_put(ep);
if (IS_ERR(asd)) {
dev_err(dcmi->dev, "Failed to add subdev notifier\n");
return PTR_ERR(asd);
}
dcmi->notifier.ops = &dcmi_graph_notify_ops;
ret = v4l2_async_nf_register(&dcmi->notifier);
if (ret < 0) {
dev_err(dcmi->dev, "Failed to register notifier\n");
v4l2_async_nf_cleanup(&dcmi->notifier);
return ret;
}
return 0;
}
static int dcmi_probe(struct platform_device *pdev)
{
struct device_node *np = pdev->dev.of_node;
struct v4l2_fwnode_endpoint ep = { .bus_type = 0 };
struct stm32_dcmi *dcmi;
struct vb2_queue *q;
struct dma_chan *chan, *mdma_chan;
struct dma_slave_caps caps;
struct dma_slave_config dma_config, mdma_config;
struct clk *mclk;
int ret = 0;
dcmi = devm_kzalloc_obj(&pdev->dev, struct stm32_dcmi);
if (!dcmi)
return -ENOMEM;
dcmi->rstc = devm_reset_control_get_exclusive(&pdev->dev, NULL);
if (IS_ERR(dcmi->rstc))
return dev_err_probe(&pdev->dev, PTR_ERR(dcmi->rstc),
"Could not get reset control\n");
np = of_graph_get_endpoint_by_regs(np, 0, -1);
if (!np) {
dev_err(&pdev->dev, "Could not find the endpoint\n");
return -ENODEV;
}
ret = v4l2_fwnode_endpoint_parse(of_fwnode_handle(np), &ep);
of_node_put(np);
if (ret) {
dev_err(&pdev->dev, "Could not parse the endpoint\n");
return ret;
}
if (ep.bus_type == V4L2_MBUS_CSI2_DPHY) {
dev_err(&pdev->dev, "CSI bus not supported\n");
return -ENODEV;
}
if (ep.bus_type == V4L2_MBUS_BT656 &&
ep.bus.parallel.bus_width != 8) {
dev_err(&pdev->dev, "BT656 bus conflicts with %u bits bus width (8 bits required)\n",
ep.bus.parallel.bus_width);
return -ENODEV;
}
dcmi->bus.flags = ep.bus.parallel.flags;
dcmi->bus.bus_width = ep.bus.parallel.bus_width;
dcmi->bus.data_shift = ep.bus.parallel.data_shift;
dcmi->bus_type = ep.bus_type;
dcmi->irq = platform_get_irq(pdev, 0);
if (dcmi->irq < 0)
return dcmi->irq;
dcmi->regs = devm_platform_get_and_ioremap_resource(pdev, 0, &dcmi->res);
if (IS_ERR(dcmi->regs))
return PTR_ERR(dcmi->regs);
mclk = devm_clk_get(&pdev->dev, "mclk");
if (IS_ERR(mclk))
return dev_err_probe(&pdev->dev, PTR_ERR(mclk),
"Unable to get mclk\n");
chan = dma_request_chan(&pdev->dev, "tx");
if (IS_ERR(chan))
return dev_err_probe(&pdev->dev, PTR_ERR(chan),
"Failed to request DMA channel\n");
mdma_chan = dma_request_chan(&pdev->dev, "mdma_tx");
if (IS_ERR(mdma_chan)) {
ret = PTR_ERR(mdma_chan);
if (ret != -ENODEV) {
dev_err_probe(&pdev->dev, ret, "Failed to request MDMA channel\n");
goto err_release_chan;
}
mdma_chan = NULL;
}
memset(&dma_config, 0, sizeof(dma_config));
dma_config.src_addr = (dma_addr_t)dcmi->res->start + DCMI_DR;
dma_config.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
if (mdma_chan)
dma_config.peripheral_size = 1;
ret = dmaengine_slave_config(chan, &dma_config);
if (ret < 0) {
dev_err(dcmi->dev, "%s: DMA channel config failed (%d)\n",
__func__, ret);
goto err_dma_slave_config;
}
if (mdma_chan) {
dcmi->sram_pool = of_gen_pool_get(pdev->dev.of_node, "sram", 0);
if (!dcmi->sram_pool) {
dev_info(&pdev->dev, "No SRAM pool, can't use MDMA chaining\n");
ret = -ENOMEM;
goto err_dma_slave_config;
}
dev_info(&pdev->dev, "SRAM pool: %zu KiB for DMA/MDMA chaining\n",
gen_pool_size(dcmi->sram_pool) / 1024);
dcmi->sram_buf_size = gen_pool_size(dcmi->sram_pool);
dcmi->sram_buf = gen_pool_dma_zalloc(dcmi->sram_pool, dcmi->sram_buf_size,
&dcmi->sram_dma_buf);
if (!dcmi->sram_buf) {
dev_err(dcmi->dev, "Failed to allocate from SRAM\n");
ret = -ENOMEM;
goto err_dma_slave_config;
}
memset(&mdma_config, 0, sizeof(mdma_config));
mdma_config.direction = DMA_DEV_TO_MEM;
mdma_config.src_addr = dcmi->sram_dma_buf;
mdma_config.peripheral_size = dma_config.peripheral_size;
mdma_config.peripheral_config = dma_config.peripheral_config;
ret = dmaengine_slave_config(mdma_chan, &mdma_config);
if (ret < 0) {
dev_err(dcmi->dev, "%s: MDMA channel config failed (%d)\n",
__func__, ret);
goto err_mdma_slave_config;
}
}
dcmi->dma_max_burst = UINT_MAX;
if (!mdma_chan) {
ret = dma_get_slave_caps(chan, &caps);
if (!ret && caps.max_sg_burst)
dcmi->dma_max_burst = caps.max_sg_burst * DMA_SLAVE_BUSWIDTH_4_BYTES;
}
spin_lock_init(&dcmi->irqlock);
mutex_init(&dcmi->lock);
init_completion(&dcmi->complete);
INIT_LIST_HEAD(&dcmi->buffers);
dcmi->dev = &pdev->dev;
dcmi->mclk = mclk;
dcmi->state = STOPPED;
dcmi->dma_chan = chan;
dcmi->mdma_chan = mdma_chan;
q = &dcmi->queue;
dcmi->v4l2_dev.mdev = &dcmi->mdev;
strscpy(dcmi->mdev.model, DRV_NAME, sizeof(dcmi->mdev.model));
dcmi->mdev.dev = &pdev->dev;
media_device_init(&dcmi->mdev);
ret = v4l2_device_register(&pdev->dev, &dcmi->v4l2_dev);
if (ret)
goto err_media_device_cleanup;
dcmi->vdev = video_device_alloc();
if (!dcmi->vdev) {
ret = -ENOMEM;
goto err_device_unregister;
}
dcmi->vdev->fops = &dcmi_fops;
dcmi->vdev->v4l2_dev = &dcmi->v4l2_dev;
dcmi->vdev->queue = &dcmi->queue;
strscpy(dcmi->vdev->name, KBUILD_MODNAME, sizeof(dcmi->vdev->name));
dcmi->vdev->release = video_device_release;
dcmi->vdev->ioctl_ops = &dcmi_ioctl_ops;
dcmi->vdev->lock = &dcmi->lock;
dcmi->vdev->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING |
V4L2_CAP_READWRITE;
video_set_drvdata(dcmi->vdev, dcmi);
dcmi->vid_cap_pad.flags = MEDIA_PAD_FL_SINK;
ret = media_entity_pads_init(&dcmi->vdev->entity,
1, &dcmi->vid_cap_pad);
if (ret) {
dev_err(dcmi->dev, "Failed to init media entity pad\n");
goto err_device_release;
}
dcmi->vdev->entity.flags |= MEDIA_ENT_FL_DEFAULT;
ret = video_register_device(dcmi->vdev, VFL_TYPE_VIDEO, -1);
if (ret) {
dev_err(dcmi->dev, "Failed to register video device\n");
goto err_media_entity_cleanup;
}
dev_dbg(dcmi->dev, "Device registered as %s\n",
video_device_node_name(dcmi->vdev));
q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
q->io_modes = VB2_MMAP | VB2_READ | VB2_DMABUF;
q->lock = &dcmi->lock;
q->drv_priv = dcmi;
q->buf_struct_size = sizeof(struct dcmi_buf);
q->ops = &dcmi_video_qops;
q->mem_ops = &vb2_dma_contig_memops;
q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
q->min_queued_buffers = 2;
q->allow_cache_hints = 1;
q->dev = &pdev->dev;
ret = vb2_queue_init(q);
if (ret < 0) {
dev_err(&pdev->dev, "Failed to initialize vb2 queue\n");
goto err_media_entity_cleanup;
}
ret = dcmi_graph_init(dcmi);
if (ret < 0)
goto err_media_entity_cleanup;
ret = reset_control_assert(dcmi->rstc);
if (ret) {
dev_err(&pdev->dev, "Failed to assert the reset line\n");
goto err_cleanup;
}
usleep_range(3000, 5000);
ret = reset_control_deassert(dcmi->rstc);
if (ret) {
dev_err(&pdev->dev, "Failed to deassert the reset line\n");
goto err_cleanup;
}
dev_info(&pdev->dev, "Probe done\n");
platform_set_drvdata(pdev, dcmi);
pm_runtime_enable(&pdev->dev);
return 0;
err_cleanup:
v4l2_async_nf_unregister(&dcmi->notifier);
v4l2_async_nf_cleanup(&dcmi->notifier);
err_media_entity_cleanup:
media_entity_cleanup(&dcmi->vdev->entity);
err_device_release:
video_device_release(dcmi->vdev);
err_device_unregister:
v4l2_device_unregister(&dcmi->v4l2_dev);
err_media_device_cleanup:
media_device_cleanup(&dcmi->mdev);
err_mdma_slave_config:
if (mdma_chan)
gen_pool_free(dcmi->sram_pool, (unsigned long)dcmi->sram_buf, dcmi->sram_buf_size);
err_dma_slave_config:
if (mdma_chan)
dma_release_channel(mdma_chan);
err_release_chan:
dma_release_channel(chan);
return ret;
}
static void dcmi_remove(struct platform_device *pdev)
{
struct stm32_dcmi *dcmi = platform_get_drvdata(pdev);
pm_runtime_disable(&pdev->dev);
v4l2_async_nf_unregister(&dcmi->notifier);
v4l2_async_nf_cleanup(&dcmi->notifier);
media_entity_cleanup(&dcmi->vdev->entity);
v4l2_device_unregister(&dcmi->v4l2_dev);
media_device_cleanup(&dcmi->mdev);
dma_release_channel(dcmi->dma_chan);
if (dcmi->mdma_chan) {
gen_pool_free(dcmi->sram_pool, (unsigned long)dcmi->sram_buf, dcmi->sram_buf_size);
dma_release_channel(dcmi->mdma_chan);
}
}
static int dcmi_runtime_suspend(struct device *dev)
{
struct stm32_dcmi *dcmi = dev_get_drvdata(dev);
clk_disable_unprepare(dcmi->mclk);
return 0;
}
static int dcmi_runtime_resume(struct device *dev)
{
struct stm32_dcmi *dcmi = dev_get_drvdata(dev);
int ret;
ret = clk_prepare_enable(dcmi->mclk);
if (ret)
dev_err(dev, "%s: Failed to prepare_enable clock\n", __func__);
return ret;
}
static int dcmi_suspend(struct device *dev)
{
pm_runtime_force_suspend(dev);
pinctrl_pm_select_sleep_state(dev);
return 0;
}
static int dcmi_resume(struct device *dev)
{
pinctrl_pm_select_default_state(dev);
return pm_runtime_force_resume(dev);
}
static const struct dev_pm_ops dcmi_pm_ops = {
SYSTEM_SLEEP_PM_OPS(dcmi_suspend, dcmi_resume)
RUNTIME_PM_OPS(dcmi_runtime_suspend, dcmi_runtime_resume, NULL)
};
static struct platform_driver stm32_dcmi_driver = {
.probe = dcmi_probe,
.remove = dcmi_remove,
.driver = {
.name = DRV_NAME,
.of_match_table = of_match_ptr(stm32_dcmi_of_match),
.pm = pm_ptr(&dcmi_pm_ops),
},
};
module_platform_driver(stm32_dcmi_driver);
MODULE_AUTHOR("Yannick Fertre <yannick.fertre@st.com>");
MODULE_AUTHOR("Hugues Fruchet <hugues.fruchet@st.com>");
MODULE_DESCRIPTION("STMicroelectronics STM32 Digital Camera Memory Interface driver");
MODULE_LICENSE("GPL");