2 * ppi.c Analog Devices Parallel Peripheral Interface driver
4 * Copyright (c) 2011 Analog Devices Inc.
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.
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.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #include <linux/slab.h>
22 #include <asm/bfin_ppi.h>
23 #include <asm/blackfin.h>
24 #include <asm/cacheflush.h>
26 #include <asm/portmux.h>
28 #include <media/blackfin/ppi.h>
30 static int ppi_attach_irq(struct ppi_if *ppi, irq_handler_t handler);
31 static void ppi_detach_irq(struct ppi_if *ppi);
32 static int ppi_start(struct ppi_if *ppi);
33 static int ppi_stop(struct ppi_if *ppi);
34 static int ppi_set_params(struct ppi_if *ppi, struct ppi_params *params);
35 static void ppi_update_addr(struct ppi_if *ppi, unsigned long addr);
37 static const struct ppi_ops ppi_ops = {
38 .attach_irq = ppi_attach_irq,
39 .detach_irq = ppi_detach_irq,
42 .set_params = ppi_set_params,
43 .update_addr = ppi_update_addr,
46 static irqreturn_t ppi_irq_err(int irq, void *dev_id)
48 struct ppi_if *ppi = dev_id;
49 const struct ppi_info *info = ppi->info;
54 struct bfin_ppi_regs *reg = info->base;
55 unsigned short status;
57 /* register on bf561 is cleared when read
60 status = bfin_read16(®->status);
61 bfin_write16(®->status, 0xff00);
66 struct bfin_eppi_regs *reg = info->base;
67 bfin_write16(®->status, 0xffff);
77 static int ppi_attach_irq(struct ppi_if *ppi, irq_handler_t handler)
79 const struct ppi_info *info = ppi->info;
82 ret = request_dma(info->dma_ch, "PPI_DMA");
85 pr_err("Unable to allocate DMA channel for PPI\n");
88 set_dma_callback(info->dma_ch, handler, ppi);
91 ret = request_irq(info->irq_err, ppi_irq_err, 0, "PPI ERROR", ppi);
93 pr_err("Unable to allocate IRQ for PPI\n");
94 free_dma(info->dma_ch);
100 static void ppi_detach_irq(struct ppi_if *ppi)
102 const struct ppi_info *info = ppi->info;
105 free_irq(info->irq_err, ppi);
106 free_dma(info->dma_ch);
109 static int ppi_start(struct ppi_if *ppi)
111 const struct ppi_info *info = ppi->info;
114 enable_dma(info->dma_ch);
117 ppi->ppi_control |= PORT_EN;
118 switch (info->type) {
121 struct bfin_ppi_regs *reg = info->base;
122 bfin_write16(®->control, ppi->ppi_control);
127 struct bfin_eppi_regs *reg = info->base;
128 bfin_write32(®->control, ppi->ppi_control);
139 static int ppi_stop(struct ppi_if *ppi)
141 const struct ppi_info *info = ppi->info;
144 ppi->ppi_control &= ~PORT_EN;
145 switch (info->type) {
148 struct bfin_ppi_regs *reg = info->base;
149 bfin_write16(®->control, ppi->ppi_control);
154 struct bfin_eppi_regs *reg = info->base;
155 bfin_write32(®->control, ppi->ppi_control);
163 clear_dma_irqstat(info->dma_ch);
164 disable_dma(info->dma_ch);
170 static int ppi_set_params(struct ppi_if *ppi, struct ppi_params *params)
172 const struct ppi_info *info = ppi->info;
174 int dma_config, bytes_per_line, lines_per_frame;
176 bytes_per_line = params->width * params->bpp / 8;
177 lines_per_frame = params->height;
178 if (params->int_mask == 0xFFFFFFFF)
179 ppi->err_int = false;
183 dma_config = (DMA_FLOW_STOP | WNR | RESTART | DMA2D | DI_EN);
184 ppi->ppi_control = params->ppi_control & ~PORT_EN;
185 switch (info->type) {
188 struct bfin_ppi_regs *reg = info->base;
190 if (params->ppi_control & DMA32)
193 bfin_write16(®->control, ppi->ppi_control);
194 bfin_write16(®->count, bytes_per_line - 1);
195 bfin_write16(®->frame, lines_per_frame);
200 struct bfin_eppi_regs *reg = info->base;
202 if ((params->ppi_control & PACK_EN)
203 || (params->ppi_control & 0x38000) > DLEN_16)
206 bfin_write32(®->control, ppi->ppi_control);
207 bfin_write16(®->line, bytes_per_line + params->blank_clocks);
208 bfin_write16(®->frame, lines_per_frame);
209 bfin_write16(®->hdelay, 0);
210 bfin_write16(®->vdelay, 0);
211 bfin_write16(®->hcount, bytes_per_line);
212 bfin_write16(®->vcount, lines_per_frame);
220 dma_config |= WDSIZE_32;
221 set_dma_x_count(info->dma_ch, bytes_per_line >> 2);
222 set_dma_x_modify(info->dma_ch, 4);
223 set_dma_y_modify(info->dma_ch, 4);
225 dma_config |= WDSIZE_16;
226 set_dma_x_count(info->dma_ch, bytes_per_line >> 1);
227 set_dma_x_modify(info->dma_ch, 2);
228 set_dma_y_modify(info->dma_ch, 2);
230 set_dma_y_count(info->dma_ch, lines_per_frame);
231 set_dma_config(info->dma_ch, dma_config);
237 static void ppi_update_addr(struct ppi_if *ppi, unsigned long addr)
239 set_dma_start_addr(ppi->info->dma_ch, addr);
242 struct ppi_if *ppi_create_instance(const struct ppi_info *info)
246 if (!info || !info->pin_req)
249 if (peripheral_request_list(info->pin_req, KBUILD_MODNAME)) {
250 pr_err("request peripheral failed\n");
254 ppi = kzalloc(sizeof(*ppi), GFP_KERNEL);
256 peripheral_free_list(info->pin_req);
257 pr_err("unable to allocate memory for ppi handle\n");
263 pr_info("ppi probe success\n");
267 void ppi_delete_instance(struct ppi_if *ppi)
269 peripheral_free_list(ppi->info->pin_req);