~ubuntu-branches/ubuntu/vivid/linux-fsl-imx51/vivid

« back to all changes in this revision

Viewing changes to drivers/mxc/hmp4e/mxc_hmp4e.c

  • Committer: Bazaar Package Importer
  • Author(s): Andy Whitcroft, Amit Kucheria, Andy Whitcroft, Bryan Wu, Upstream Kernel Changes
  • Date: 2010-01-11 16:26:27 UTC
  • Revision ID: james.westby@ubuntu.com-20100111162627-1q2fl9tcuwcywt1e
Tags: 2.6.31-602.4
[ Amit Kucheria ]

* Update to official 2.6.31 BSP release from Freescale

[ Andy Whitcroft ]

* drop a number of modules no longer built

[ Bryan Wu ]

* [Config] Update configs after applying .31 patchset from Freescale
* [Config] Sync with imx51_defconfig from Freescale BSP

[ Upstream Kernel Changes ]

* Update to official 2.6.31 BSP release from Freescale.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright 2005-2009 Freescale Semiconductor, Inc. All Rights Reserved.
 
3
 */
 
4
 
 
5
/*
 
6
 * The code contained herein is licensed under the GNU General Public
 
7
 * License. You may obtain a copy of the GNU General Public License
 
8
 * Version 2 or later at the following locations:
 
9
 *
 
10
 * http://www.opensource.org/licenses/gpl-license.html
 
11
 * http://www.gnu.org/copyleft/gpl.html
 
12
 */
 
13
 
 
14
/*
 
15
 * Encoder device driver (kernel module)
 
16
 *
 
17
 * Copyright (C) 2005  Hantro Products Oy.
 
18
 *
 
19
 * This program is free software; you can redistribute it and/or
 
20
 * modify it under the terms of the GNU General Public License
 
21
 * as published by the Free Software Foundation; either version 2
 
22
 * of the License, or (at your option) any later version.
 
23
 
 
24
 * This program is distributed in the hope that it will be useful,
 
25
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
26
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
27
 * GNU General Public License for more details.
 
28
 *
 
29
 * You should have received a copy of the GNU General Public License
 
30
 * along with this program; if not, write to the Free Software Foundation,
 
31
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 
32
 */
 
33
 
 
34
#include <linux/kernel.h>
 
35
#include <linux/module.h>
 
36
#include <linux/init.h>         /*  __init,__exit directives */
 
37
#include <linux/mm.h>           /* remap_page_range / remap_pfn_range */
 
38
#include <linux/fs.h>           /* for struct file_operations */
 
39
#include <linux/errno.h>        /* standard error codes */
 
40
#include <linux/platform_device.h>      /* for device registeration for PM */
 
41
#include <linux/delay.h>        /* for msleep_interruptible */
 
42
#include <linux/interrupt.h>
 
43
#include <linux/dma-mapping.h>  /* for dma_alloc_consistent */
 
44
#include <linux/clk.h>
 
45
#include <asm/uaccess.h>        /* for ioctl __get_user, __put_user */
 
46
#include <mach/hardware.h>
 
47
#include "mxc_hmp4e.h"          /* MPEG4 encoder specific */
 
48
 
 
49
/* here's all the must remember stuff */
 
50
typedef struct {
 
51
        ulong buffaddr;
 
52
        u32 buffsize;
 
53
        ulong iobaseaddr;
 
54
        u32 iosize;
 
55
        volatile u32 *hwregs;
 
56
        u32 irq;
 
57
        u16 hwid_offset;
 
58
        u16 intr_offset;
 
59
        u16 busy_offset;
 
60
        u16 type;               /* Encoder type, CIF = 0, VGA = 1 */
 
61
        u16 clk_gate;
 
62
        u16 busy_val;
 
63
        struct fasync_struct *async_queue;
 
64
#ifdef CONFIG_PM
 
65
        s32 suspend_state;
 
66
        wait_queue_head_t power_queue;
 
67
#endif
 
68
} hmp4e_t;
 
69
 
 
70
/* and this is our MAJOR; use 0 for dynamic allocation (recommended)*/
 
71
static s32 hmp4e_major;
 
72
 
 
73
static u32 hmp4e_phys;
 
74
static struct class *hmp4e_class;
 
75
static hmp4e_t hmp4e_data;
 
76
 
 
77
/*! MPEG4 enc clock handle. */
 
78
static struct clk *hmp4e_clk;
 
79
 
 
80
/*
 
81
 * avoid "enable_irq(x) unbalanced from ..."
 
82
 * error messages from the kernel, since {ena,dis}able_irq()
 
83
 * calls are stacked in kernel.
 
84
 */
 
85
static bool irq_enable = false;
 
86
 
 
87
ulong base_port = MPEG4_ENC_BASE_ADDR;
 
88
u32 irq = MXC_INT_MPEG4_ENCODER;
 
89
 
 
90
module_param(base_port, long, 000);
 
91
module_param(irq, int, 000);
 
92
 
 
93
/*!
 
94
 * These variables store the register values when HMP4E is in suspend mode.
 
95
 */
 
96
#ifdef CONFIG_PM
 
97
u32 io_regs[64];
 
98
#endif
 
99
 
 
100
static s32 hmp4e_map_buffer(struct file *filp, struct vm_area_struct *vma);
 
101
static s32 hmp4e_map_hwregs(struct file *filp, struct vm_area_struct *vma);
 
102
static void hmp4e_reset(hmp4e_t * dev);
 
103
irqreturn_t hmp4e_isr(s32 irq, void *dev_id);
 
104
 
 
105
/*!
 
106
 * This funtion is called to write h/w register.
 
107
 *
 
108
 * @param   val         value to be written into the register
 
109
 * @param   offset      register offset
 
110
 *
 
111
 */
 
112
static inline void hmp4e_write(u32 val, u32 offset)
 
113
{
 
114
        hmp4e_t *dev = &hmp4e_data;
 
115
        __raw_writel(val, (dev->hwregs + offset));
 
116
}
 
117
 
 
118
/*!
 
119
 * This funtion is called to read h/w register.
 
120
 *
 
121
 * @param   offset      register offset
 
122
 *
 
123
 * @return  This function returns the value read from the register.
 
124
 *
 
125
 */
 
126
static inline u32 hmp4e_read(u32 offset)
 
127
{
 
128
        hmp4e_t *dev = &hmp4e_data;
 
129
        u32 val;
 
130
 
 
131
        val = __raw_readl(dev->hwregs + offset);
 
132
 
 
133
        return val;
 
134
}
 
135
 
 
136
/*!
 
137
 * The device's mmap method. The VFS has kindly prepared the process's
 
138
 * vm_area_struct for us, so we examine this to see what was requested.
 
139
 *
 
140
 * @param   filp        pointer to struct file
 
141
 * @param   vma         pointer to struct vma_area_struct
 
142
 *
 
143
 * @return  This function returns 0 if successful or -ve value on error.
 
144
 *
 
145
 */
 
146
static s32 hmp4e_mmap(struct file *filp, struct vm_area_struct *vma)
 
147
{
 
148
        s32 result;
 
149
        ulong offset = vma->vm_pgoff << PAGE_SHIFT;
 
150
 
 
151
        pr_debug("hmp4e_mmap: size = %lu off = 0x%08lx\n",
 
152
                 (unsigned long)(vma->vm_end - vma->vm_start), offset);
 
153
 
 
154
        if (offset == 0) {
 
155
                result = hmp4e_map_buffer(filp, vma);
 
156
        } else if (offset == hmp4e_data.iobaseaddr) {
 
157
                result = hmp4e_map_hwregs(filp, vma);
 
158
        } else {
 
159
                pr_debug("hmp4e: mmap invalid value\n");
 
160
                result = -EINVAL;
 
161
        }
 
162
 
 
163
        return result;
 
164
}
 
165
 
 
166
/*!
 
167
 * This funtion is called to handle ioctls.
 
168
 *
 
169
 * @param   inode       pointer to struct inode
 
170
 * @param   filp        pointer to struct file
 
171
 * @param   cmd         ioctl command
 
172
 * @param   arg         user data
 
173
 *
 
174
 * @return  This function returns 0 if successful or -ve value on error.
 
175
 *
 
176
 */
 
177
static s32 hmp4e_ioctl(struct inode *inode, struct file *filp,
 
178
                       u32 cmd, ulong arg)
 
179
{
 
180
        s32 err = 0, retval = 0;
 
181
        ulong offset = 0;
 
182
        hmp4e_t *dev = &hmp4e_data;
 
183
        write_t bwrite;
 
184
 
 
185
#ifdef CONFIG_PM
 
186
        wait_event_interruptible(hmp4e_data.power_queue,
 
187
                                 hmp4e_data.suspend_state == 0);
 
188
#endif
 
189
 
 
190
        /*
 
191
         * extract the type and number bitfields, and don't decode
 
192
         * wrong cmds: return ENOTTY (inappropriate ioctl) before access_ok()
 
193
         */
 
194
        if (_IOC_TYPE(cmd) != HMP4E_IOC_MAGIC) {
 
195
                pr_debug("hmp4e: ioctl invalid magic\n");
 
196
                return -ENOTTY;
 
197
        }
 
198
 
 
199
        if (_IOC_NR(cmd) > HMP4E_IOC_MAXNR) {
 
200
                pr_debug("hmp4e: ioctl exceeds max ioctl\n");
 
201
                return -ENOTTY;
 
202
        }
 
203
 
 
204
        /*
 
205
         * the direction is a bitmask, and VERIFY_WRITE catches R/W
 
206
         * transfers. `Type' is user-oriented, while
 
207
         * access_ok is kernel-oriented, so the concept of "read" and
 
208
         * "write" is reversed
 
209
         */
 
210
        if (_IOC_DIR(cmd) & _IOC_READ) {
 
211
                err = !access_ok(VERIFY_WRITE, (void *)arg, _IOC_SIZE(cmd));
 
212
        } else if (_IOC_DIR(cmd) & _IOC_WRITE) {
 
213
                err = !access_ok(VERIFY_READ, (void *)arg, _IOC_SIZE(cmd));
 
214
        }
 
215
 
 
216
        if (err) {
 
217
                pr_debug("hmp4e: ioctl invalid direction\n");
 
218
                return -EFAULT;
 
219
        }
 
220
 
 
221
        switch (cmd) {
 
222
        case HMP4E_IOCHARDRESET:
 
223
                break;
 
224
 
 
225
        case HMP4E_IOCGBUFBUSADDRESS:
 
226
                retval = __put_user((ulong) hmp4e_phys, (u32 *) arg);
 
227
                break;
 
228
 
 
229
        case HMP4E_IOCGBUFSIZE:
 
230
                retval = __put_user(hmp4e_data.buffsize, (u32 *) arg);
 
231
                break;
 
232
 
 
233
        case HMP4E_IOCSREGWRITE:
 
234
                if (dev->type != 1) {   /* This ioctl only for VGA */
 
235
                        pr_debug("hmp4e: HMP4E_IOCSREGWRITE invalid\n");
 
236
                        retval = -EINVAL;
 
237
                        break;
 
238
                }
 
239
 
 
240
                retval = __copy_from_user(&bwrite, (u32 *) arg,
 
241
                                          sizeof(write_t));
 
242
 
 
243
                if (bwrite.offset <= hmp4e_data.iosize - 4) {
 
244
                        hmp4e_write(bwrite.data, (bwrite.offset / 4));
 
245
                } else {
 
246
                        pr_debug("hmp4e: HMP4E_IOCSREGWRITE failed\n");
 
247
                        retval = -EFAULT;
 
248
                }
 
249
                break;
 
250
 
 
251
        case HMP4E_IOCXREGREAD:
 
252
                if (dev->type != 1) {   /* This ioctl only for VGA */
 
253
                        pr_debug("hmp4e: HMP4E_IOCSREGWRITE invalid\n");
 
254
                        retval = -EINVAL;
 
255
                        break;
 
256
                }
 
257
 
 
258
                retval = __get_user(offset, (ulong *) arg);
 
259
                if (offset <= hmp4e_data.iosize - 4) {
 
260
                        __put_user(hmp4e_read((offset / 4)), (ulong *) arg);
 
261
                } else {
 
262
                        pr_debug("hmp4e: HMP4E_IOCXREGREAD failed\n");
 
263
                        retval = -EFAULT;
 
264
                }
 
265
                break;
 
266
 
 
267
        case HMP4E_IOCGHWOFFSET:
 
268
                __put_user(hmp4e_data.iobaseaddr, (ulong *) arg);
 
269
                break;
 
270
 
 
271
        case HMP4E_IOCGHWIOSIZE:
 
272
                __put_user(hmp4e_data.iosize, (u32 *) arg);
 
273
                break;
 
274
 
 
275
        case HMP4E_IOC_CLI:
 
276
                if (irq_enable == true) {
 
277
                        disable_irq(hmp4e_data.irq);
 
278
                        irq_enable = false;
 
279
                }
 
280
                break;
 
281
 
 
282
        case HMP4E_IOC_STI:
 
283
                if (irq_enable == false) {
 
284
                        enable_irq(hmp4e_data.irq);
 
285
                        irq_enable = true;
 
286
                }
 
287
                break;
 
288
 
 
289
        default:
 
290
                pr_debug("unknown case %x\n", cmd);
 
291
        }
 
292
 
 
293
        return retval;
 
294
}
 
295
 
 
296
/*!
 
297
 * This funtion is called when the device is opened.
 
298
 *
 
299
 * @param   inode       pointer to struct inode
 
300
 * @param   filp        pointer to struct file
 
301
 *
 
302
 * @return  This function returns 0 if successful or -ve value on error.
 
303
 *
 
304
 */
 
305
static s32 hmp4e_open(struct inode *inode, struct file *filp)
 
306
{
 
307
        hmp4e_t *dev = &hmp4e_data;
 
308
 
 
309
        filp->private_data = (void *)dev;
 
310
 
 
311
        if (request_irq(dev->irq, hmp4e_isr, 0, "mxc_hmp4e", dev) != 0) {
 
312
                pr_debug("hmp4e: request irq failed\n");
 
313
                return -EBUSY;
 
314
        }
 
315
 
 
316
        if (irq_enable == false) {
 
317
                irq_enable = true;
 
318
        }
 
319
        clk_enable(hmp4e_clk);
 
320
        return 0;
 
321
}
 
322
 
 
323
static s32 hmp4e_fasync(s32 fd, struct file *filp, s32 mode)
 
324
{
 
325
        hmp4e_t *dev = (hmp4e_t *) filp->private_data;
 
326
        return fasync_helper(fd, filp, mode, &dev->async_queue);
 
327
}
 
328
 
 
329
/*!
 
330
 * This funtion is called when the device is closed.
 
331
 *
 
332
 * @param   inode       pointer to struct inode
 
333
 * @param   filp        pointer to struct file
 
334
 *
 
335
 * @return  This function returns 0.
 
336
 *
 
337
 */
 
338
static s32 hmp4e_release(struct inode *inode, struct file *filp)
 
339
{
 
340
        hmp4e_t *dev = (hmp4e_t *) filp->private_data;
 
341
 
 
342
        /* this is necessary if user process exited asynchronously */
 
343
        if (irq_enable == true) {
 
344
                disable_irq(dev->irq);
 
345
                irq_enable = false;
 
346
        }
 
347
 
 
348
        /* reset hardware */
 
349
        hmp4e_reset(&hmp4e_data);
 
350
 
 
351
        /* free the encoder IRQ */
 
352
        free_irq(dev->irq, (void *)dev);
 
353
 
 
354
        /* remove this filp from the asynchronusly notified filp's */
 
355
        hmp4e_fasync(-1, filp, 0);
 
356
        clk_disable(hmp4e_clk);
 
357
        return 0;
 
358
}
 
359
 
 
360
/* VFS methods */
 
361
static struct file_operations hmp4e_fops = {
 
362
        .owner = THIS_MODULE,
 
363
        .open = hmp4e_open,
 
364
        .release = hmp4e_release,
 
365
        .ioctl = hmp4e_ioctl,
 
366
        .mmap = hmp4e_mmap,
 
367
        .fasync = hmp4e_fasync,
 
368
};
 
369
 
 
370
/*!
 
371
 * This funtion allocates physical contigous memory.
 
372
 *
 
373
 * @param   size        size of memory to be allocated
 
374
 *
 
375
 * @return  This function returns 0 if successful or -ve value on error.
 
376
 *
 
377
 */
 
378
static s32 hmp4e_alloc(u32 size)
 
379
{
 
380
        hmp4e_data.buffsize = PAGE_ALIGN(size);
 
381
        hmp4e_data.buffaddr =
 
382
            (ulong) dma_alloc_coherent(NULL, hmp4e_data.buffsize,
 
383
                                       (dma_addr_t *) & hmp4e_phys,
 
384
                                       GFP_DMA | GFP_KERNEL);
 
385
 
 
386
        if (hmp4e_data.buffaddr == 0) {
 
387
                printk(KERN_ERR "hmp4e: couldn't allocate data buffer\n");
 
388
                return -ENOMEM;
 
389
        }
 
390
 
 
391
        memset((s8 *) hmp4e_data.buffaddr, 0, hmp4e_data.buffsize);
 
392
        return 0;
 
393
}
 
394
 
 
395
/*!
 
396
 * This funtion frees the DMAed memory.
 
397
 */
 
398
static void hmp4e_free(void)
 
399
{
 
400
        if (hmp4e_data.buffaddr != 0) {
 
401
                dma_free_coherent(NULL, hmp4e_data.buffsize,
 
402
                                  (void *)hmp4e_data.buffaddr, hmp4e_phys);
 
403
                hmp4e_data.buffaddr = 0;
 
404
        }
 
405
}
 
406
 
 
407
/*!
 
408
 * This funtion maps the shared buffer in memory.
 
409
 *
 
410
 * @param   filp        pointer to struct file
 
411
 * @param   vma         pointer to struct vm_area_struct
 
412
 *
 
413
 * @return  This function returns 0 if successful or -ve value on error.
 
414
 *
 
415
 */
 
416
static s32 hmp4e_map_buffer(struct file *filp, struct vm_area_struct *vma)
 
417
{
 
418
        ulong phys;
 
419
        ulong start = (u32) vma->vm_start;
 
420
        ulong size = (u32) (vma->vm_end - vma->vm_start);
 
421
 
 
422
        /* if userspace tries to mmap beyond end of our buffer, fail */
 
423
        if (size > hmp4e_data.buffsize) {
 
424
                pr_debug("hmp4e: hmp4e_map_buffer, invalid size\n");
 
425
                return -EINVAL;
 
426
        }
 
427
 
 
428
        vma->vm_flags |= VM_RESERVED | VM_IO;
 
429
        vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
 
430
 
 
431
        phys = hmp4e_phys;
 
432
 
 
433
        if (remap_pfn_range(vma, start, phys >> PAGE_SHIFT, size,
 
434
                            vma->vm_page_prot)) {
 
435
                pr_debug("hmp4e: failed mmapping shared buffer\n");
 
436
                return -EAGAIN;
 
437
        }
 
438
 
 
439
        return 0;
 
440
}
 
441
 
 
442
/*!
 
443
 * This funtion maps the h/w register space in memory.
 
444
 *
 
445
 * @param   filp        pointer to struct file
 
446
 * @param   vma         pointer to struct vm_area_struct
 
447
 *
 
448
 * @return  This function returns 0 if successful or -ve value on error.
 
449
 *
 
450
 */
 
451
static s32 hmp4e_map_hwregs(struct file *filp, struct vm_area_struct *vma)
 
452
{
 
453
        ulong phys;
 
454
        ulong start = (unsigned long)vma->vm_start;
 
455
        ulong size = (unsigned long)(vma->vm_end - vma->vm_start);
 
456
 
 
457
        /* if userspace tries to mmap beyond end of our buffer, fail */
 
458
        if (size > PAGE_SIZE) {
 
459
                pr_debug("hmp4e: hmp4e_map_hwregs, invalid size\n");
 
460
                return -EINVAL;
 
461
        }
 
462
 
 
463
        vma->vm_flags |= VM_RESERVED | VM_IO;
 
464
        vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
 
465
 
 
466
        /* Remember this won't work for vmalloc()d memory ! */
 
467
        phys = hmp4e_data.iobaseaddr;
 
468
 
 
469
        if (remap_pfn_range(vma, start, phys >> PAGE_SHIFT, hmp4e_data.iosize,
 
470
                            vma->vm_page_prot)) {
 
471
                pr_debug("hmp4e: failed mmapping HW registers\n");
 
472
                return -EAGAIN;
 
473
        }
 
474
 
 
475
        return 0;
 
476
}
 
477
 
 
478
/*!
 
479
 * This function is the interrupt service routine.
 
480
 *
 
481
 * @param   irq         the irq number
 
482
 * @param   dev_id      driver data when ISR was regiatered
 
483
 *
 
484
 * @return  The return value is IRQ_HANDLED.
 
485
 *
 
486
 */
 
487
irqreturn_t hmp4e_isr(s32 irq, void *dev_id)
 
488
{
 
489
        hmp4e_t *dev = (hmp4e_t *) dev_id;
 
490
        u32 offset = dev->intr_offset;
 
491
 
 
492
        u32 irq_status = hmp4e_read(offset);
 
493
 
 
494
        /* clear enc IRQ */
 
495
        hmp4e_write(irq_status & (~0x01), offset);
 
496
 
 
497
        if (dev->async_queue)
 
498
                kill_fasync(&dev->async_queue, SIGIO, POLL_IN);
 
499
 
 
500
        return IRQ_HANDLED;
 
501
}
 
502
 
 
503
/*!
 
504
 * This function is called to reset the encoder.
 
505
 *
 
506
 * @param   dev  pointer to struct hmp4e_data
 
507
 *
 
508
 */
 
509
static void hmp4e_reset(hmp4e_t * dev)
 
510
{
 
511
        s32 i;
 
512
 
 
513
        /* enable HCLK for register reset */
 
514
        hmp4e_write(dev->clk_gate, 0);
 
515
 
 
516
        /* Reset registers, except ECR0 (0x00) and ID (read-only) */
 
517
        for (i = 1; i < (dev->iosize / 4); i += 1) {
 
518
                if (i == dev->hwid_offset)      /* ID is read only */
 
519
                        continue;
 
520
 
 
521
                /* Only for CIF, not used */
 
522
                if ((dev->type == 0) && (i == 14))
 
523
                        continue;
 
524
 
 
525
                hmp4e_write(0, i);
 
526
        }
 
527
 
 
528
        /* disable HCLK */
 
529
        hmp4e_write(0, 0);
 
530
        return;
 
531
}
 
532
 
 
533
/*!
 
534
 * This function is called during the driver binding process. This function
 
535
 * does the hardware initialization.
 
536
 *
 
537
 * @param   dev   the device structure used to store device specific
 
538
 *                information that is used by the suspend, resume and remove
 
539
 *                functions
 
540
 *
 
541
 * @return  The function returns 0 if successful.
 
542
 */
 
543
static s32 hmp4e_probe(struct platform_device *pdev)
 
544
{
 
545
        s32 result;
 
546
        u32 hwid;
 
547
        struct device *temp_class;
 
548
 
 
549
        hmp4e_data.iobaseaddr = base_port;
 
550
        hmp4e_data.irq = irq;
 
551
        hmp4e_data.buffaddr = 0;
 
552
 
 
553
        /* map hw i/o registers into kernel space */
 
554
        hmp4e_data.hwregs = (volatile void *)IO_ADDRESS(hmp4e_data.iobaseaddr);
 
555
 
 
556
        hmp4e_clk = clk_get(&pdev->dev, "mpeg4_clk");
 
557
        if (IS_ERR(hmp4e_clk)) {
 
558
                printk(KERN_INFO "hmp4e: Unable to get clock\n");
 
559
                return -EIO;
 
560
        }
 
561
 
 
562
        clk_enable(hmp4e_clk);
 
563
 
 
564
        /* check hw id for encoder signature */
 
565
        hwid = hmp4e_read(7);
 
566
        if ((hwid & 0xffff) == 0x1882) {        /* CIF first */
 
567
                hmp4e_data.type = 0;
 
568
                hmp4e_data.iosize = (16 * 4);
 
569
                hmp4e_data.hwid_offset = 7;
 
570
                hmp4e_data.intr_offset = 5;
 
571
                hmp4e_data.clk_gate = (1 << 1);
 
572
                hmp4e_data.buffsize = 512000;
 
573
                hmp4e_data.busy_offset = 0;
 
574
                hmp4e_data.busy_val = 1;
 
575
        } else {
 
576
                hwid = hmp4e_read((0x88 / 4));
 
577
                if ((hwid & 0xffff0000) == 0x52510000) {        /* VGA */
 
578
                        hmp4e_data.type = 1;
 
579
                        hmp4e_data.iosize = (35 * 4);
 
580
                        hmp4e_data.hwid_offset = (0x88 / 4);
 
581
                        hmp4e_data.intr_offset = (0x10 / 4);
 
582
                        hmp4e_data.clk_gate = (1 << 12);
 
583
                        hmp4e_data.buffsize = 1048576;
 
584
                        hmp4e_data.busy_offset = (0x10 / 4);
 
585
                        hmp4e_data.busy_val = (1 << 1);
 
586
                } else {
 
587
                        printk(KERN_INFO "hmp4e: HW ID not found\n");
 
588
                        goto error1;
 
589
                }
 
590
        }
 
591
 
 
592
        /* Reset hardware */
 
593
        hmp4e_reset(&hmp4e_data);
 
594
 
 
595
        /* allocate memory shared with ewl */
 
596
        result = hmp4e_alloc(hmp4e_data.buffsize);
 
597
        if (result < 0)
 
598
                goto error1;
 
599
 
 
600
        result = register_chrdev(hmp4e_major, "hmp4e", &hmp4e_fops);
 
601
        if (result <= 0) {
 
602
                pr_debug("hmp4e: unable to get major %d\n", hmp4e_major);
 
603
                goto error2;
 
604
        }
 
605
 
 
606
        hmp4e_major = result;
 
607
 
 
608
        hmp4e_class = class_create(THIS_MODULE, "hmp4e");
 
609
        if (IS_ERR(hmp4e_class)) {
 
610
                pr_debug("Error creating hmp4e class.\n");
 
611
                goto error3;
 
612
        }
 
613
 
 
614
        temp_class = device_create(hmp4e_class, NULL, MKDEV(hmp4e_major, 0), NULL,
 
615
                                   "hmp4e");
 
616
        if (IS_ERR(temp_class)) {
 
617
                pr_debug("Error creating hmp4e class device.\n");
 
618
                goto error4;
 
619
        }
 
620
 
 
621
        platform_set_drvdata(pdev, &hmp4e_data);
 
622
 
 
623
#ifdef CONFIG_PM
 
624
        hmp4e_data.async_queue = NULL;
 
625
        hmp4e_data.suspend_state = 0;
 
626
        init_waitqueue_head(&hmp4e_data.power_queue);
 
627
#endif
 
628
 
 
629
        printk(KERN_INFO "hmp4e: %s encoder initialized\n",
 
630
               hmp4e_data.type ? "VGA" : "CIF");
 
631
        clk_disable(hmp4e_clk);
 
632
        return 0;
 
633
 
 
634
      error4:
 
635
        class_destroy(hmp4e_class);
 
636
      error3:
 
637
        unregister_chrdev(hmp4e_major, "hmp4e");
 
638
      error2:
 
639
        hmp4e_free();
 
640
      error1:
 
641
        clk_disable(hmp4e_clk);
 
642
        clk_put(hmp4e_clk);
 
643
        printk(KERN_INFO "hmp4e: module not inserted\n");
 
644
        return -EIO;
 
645
}
 
646
 
 
647
/*!
 
648
 * Dissociates the driver.
 
649
 *
 
650
 * @param   dev   the device structure
 
651
 *
 
652
 * @return  The function always returns 0.
 
653
 */
 
654
static s32 hmp4e_remove(struct platform_device *pdev)
 
655
{
 
656
        device_destroy(hmp4e_class, MKDEV(hmp4e_major, 0));
 
657
        class_destroy(hmp4e_class);
 
658
        unregister_chrdev(hmp4e_major, "hmp4e");
 
659
        hmp4e_free();
 
660
        clk_disable(hmp4e_clk);
 
661
        clk_put(hmp4e_clk);
 
662
        platform_set_drvdata(pdev, NULL);
 
663
        return 0;
 
664
}
 
665
 
 
666
#ifdef CONFIG_PM
 
667
/*!
 
668
 * This is the suspend of power management for the Hantro MPEG4 module
 
669
 *
 
670
 * @param        dev            the device
 
671
 * @param        state          the state
 
672
 *
 
673
 * @return       This function always returns 0.
 
674
 */
 
675
static s32 hmp4e_suspend(struct platform_device *pdev, pm_message_t state)
 
676
{
 
677
        s32 i;
 
678
        hmp4e_t *pdata = &hmp4e_data;
 
679
 
 
680
        /*
 
681
         * how many times msleep_interruptible will be called before
 
682
         * giving up
 
683
         */
 
684
        s32 timeout = 10;
 
685
 
 
686
        pr_debug("hmp4e: Suspend\n");
 
687
        hmp4e_data.suspend_state = 1;
 
688
 
 
689
        /* check if encoder is currently running */
 
690
        while ((hmp4e_read(pdata->busy_offset) & (pdata->busy_val)) &&
 
691
               --timeout) {
 
692
                pr_debug("hmp4e: encoder is running, going to sleep\n");
 
693
                msleep_interruptible((unsigned int)30);
 
694
        }
 
695
 
 
696
        if (!timeout) {
 
697
                pr_debug("hmp4e: timeout suspending, resetting encoder\n");
 
698
                hmp4e_write(hmp4e_read(pdata->busy_offset) &
 
699
                            (~pdata->busy_val), pdata->busy_offset);
 
700
        }
 
701
 
 
702
        /* first read register 0 */
 
703
        io_regs[0] = hmp4e_read(0);
 
704
 
 
705
        /* then override HCLK to make sure other registers can be read */
 
706
        hmp4e_write(pdata->clk_gate, 0);
 
707
 
 
708
        /* read other registers */
 
709
        for (i = 1; i < (pdata->iosize / 4); i += 1) {
 
710
 
 
711
                /* Only for CIF, not used */
 
712
                if ((pdata->type == 0) && (i == 14))
 
713
                        continue;
 
714
 
 
715
                io_regs[i] = hmp4e_read(i);
 
716
        }
 
717
 
 
718
        /* restore value of register 0 */
 
719
        hmp4e_write(io_regs[0], 0);
 
720
 
 
721
        /* stop HCLK */
 
722
        hmp4e_write(0, 0);
 
723
        clk_disable(hmp4e_clk);
 
724
        return 0;
 
725
};
 
726
 
 
727
/*!
 
728
 * This is the resume of power management for the Hantro MPEG4 module
 
729
 * It suports RESTORE state.
 
730
 *
 
731
 * @param        pdev            the platform device
 
732
 *
 
733
 * @return       This function always returns 0
 
734
 */
 
735
static s32 hmp4e_resume(struct platform_device *pdev)
 
736
{
 
737
        s32 i;
 
738
        u32 status;
 
739
        hmp4e_t *pdata = &hmp4e_data;
 
740
 
 
741
        pr_debug("hmp4e: Resume\n");
 
742
        clk_enable(hmp4e_clk);
 
743
 
 
744
        /* override HCLK to make sure registers can be written */
 
745
        hmp4e_write(pdata->clk_gate, 0x00);
 
746
 
 
747
        for (i = 1; i < (pdata->iosize / 4); i += 1) {
 
748
                if (i == pdata->hwid_offset)    /* Read only */
 
749
                        continue;
 
750
 
 
751
                /* Only for CIF, not used */
 
752
                if ((pdata->type == 0) && (i == 14))
 
753
                        continue;
 
754
 
 
755
                hmp4e_write(io_regs[i], i);
 
756
        }
 
757
 
 
758
        /* write register 0 last */
 
759
        hmp4e_write(io_regs[0], 0x00);
 
760
 
 
761
        /* Clear the suspend flag */
 
762
        hmp4e_data.suspend_state = 0;
 
763
 
 
764
        /* Unblock the wait queue */
 
765
        wake_up_interruptible(&hmp4e_data.power_queue);
 
766
 
 
767
        /* Continue operations */
 
768
        status = hmp4e_read(pdata->intr_offset);
 
769
        if (status & 0x1) {
 
770
                hmp4e_write(status & (~0x01), pdata->intr_offset);
 
771
                if (hmp4e_data.async_queue)
 
772
                        kill_fasync(&hmp4e_data.async_queue, SIGIO, POLL_IN);
 
773
        }
 
774
 
 
775
        return 0;
 
776
};
 
777
 
 
778
#endif
 
779
 
 
780
static struct platform_driver hmp4e_driver = {
 
781
        .driver = {
 
782
                   .name = "mxc_hmp4e",
 
783
                   },
 
784
        .probe = hmp4e_probe,
 
785
        .remove = hmp4e_remove,
 
786
#ifdef CONFIG_PM
 
787
        .suspend = hmp4e_suspend,
 
788
        .resume = hmp4e_resume,
 
789
#endif
 
790
};
 
791
 
 
792
static s32 __init hmp4e_init(void)
 
793
{
 
794
        printk(KERN_INFO "hmp4e: init\n");
 
795
        platform_driver_register(&hmp4e_driver);
 
796
        return 0;
 
797
}
 
798
 
 
799
static void __exit hmp4e_cleanup(void)
 
800
{
 
801
        platform_driver_unregister(&hmp4e_driver);
 
802
        printk(KERN_INFO "hmp4e: module removed\n");
 
803
}
 
804
 
 
805
module_init(hmp4e_init);
 
806
module_exit(hmp4e_cleanup);
 
807
 
 
808
/* module description */
 
809
MODULE_AUTHOR("Hantro Products Oy");
 
810
MODULE_DESCRIPTION("Device driver for Hantro's hardware based MPEG4 encoder");
 
811
MODULE_SUPPORTED_DEVICE("5251/4251 MPEG4 Encoder");
 
812
MODULE_LICENSE("GPL");