This source file includes following definitions.
- to_cam
- cafe_smbus_write_done
- cafe_smbus_write_data
- cafe_smbus_read_done
- cafe_smbus_read_data
- cafe_smbus_xfer
- cafe_smbus_enable_irq
- cafe_smbus_func
- cafe_smbus_setup
- cafe_smbus_shutdown
- cafe_ctlr_init
- cafe_ctlr_power_up
- cafe_ctlr_power_down
- cafe_irq
- cafe_pci_probe
- cafe_shutdown
- cafe_pci_remove
- cafe_pci_suspend
- cafe_pci_resume
- cafe_init
- cafe_exit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 #include <linux/kernel.h>
20 #include <linux/module.h>
21 #include <linux/init.h>
22 #include <linux/pci.h>
23 #include <linux/i2c.h>
24 #include <linux/interrupt.h>
25 #include <linux/spinlock.h>
26 #include <linux/slab.h>
27 #include <linux/videodev2.h>
28 #include <media/v4l2-device.h>
29 #include <media/i2c/ov7670.h>
30 #include <linux/device.h>
31 #include <linux/wait.h>
32 #include <linux/delay.h>
33 #include <linux/io.h>
34 #include <linux/clkdev.h>
35
36 #include "mcam-core.h"
37
38 #define CAFE_VERSION 0x000002
39
40
41
42
43
44 MODULE_AUTHOR("Jonathan Corbet <corbet@lwn.net>");
45 MODULE_DESCRIPTION("Marvell 88ALP01 CMOS Camera Controller driver");
46 MODULE_LICENSE("GPL");
47 MODULE_SUPPORTED_DEVICE("Video");
48
49
50
51
52 struct cafe_camera {
53 int registered;
54 struct mcam_camera mcam;
55 struct pci_dev *pdev;
56 struct i2c_adapter *i2c_adapter;
57 wait_queue_head_t smbus_wait;
58 };
59
60
61
62
63
64
65
66
67
68
69
70 #define REG_GPR 0xb4
71 #define GPR_C1EN 0x00000020
72 #define GPR_C0EN 0x00000010
73 #define GPR_C1 0x00000002
74
75
76
77
78 #define GPR_C0 0x00000001
79
80
81
82
83
84 #define REG_TWSIC0 0xb8
85 #define TWSIC0_EN 0x00000001
86 #define TWSIC0_MODE 0x00000002
87 #define TWSIC0_SID 0x000003fc
88
89
90
91
92
93
94
95 #define TWSIC0_SID_SHIFT 3
96 #define TWSIC0_CLKDIV 0x0007fc00
97 #define TWSIC0_MASKACK 0x00400000
98 #define TWSIC0_OVMAGIC 0x00800000
99
100 #define REG_TWSIC1 0xbc
101 #define TWSIC1_DATA 0x0000ffff
102 #define TWSIC1_ADDR 0x00ff0000
103 #define TWSIC1_ADDR_SHIFT 16
104 #define TWSIC1_READ 0x01000000
105 #define TWSIC1_WSTAT 0x02000000
106 #define TWSIC1_RVALID 0x04000000
107 #define TWSIC1_ERROR 0x08000000
108
109
110
111
112 #define REG_GL_CSR 0x3004
113 #define GCSR_SRS 0x00000001
114 #define GCSR_SRC 0x00000002
115 #define GCSR_MRS 0x00000004
116 #define GCSR_MRC 0x00000008
117 #define GCSR_CCIC_EN 0x00004000
118 #define REG_GL_IMASK 0x300c
119 #define GIMSK_CCIC_EN 0x00000004
120
121 #define REG_GL_FCR 0x3038
122 #define GFCR_GPIO_ON 0x08
123 #define REG_GL_GPIOR 0x315c
124 #define GGPIO_OUT 0x80000
125 #define GGPIO_VAL 0x00008
126
127 #define REG_LEN (REG_GL_IMASK + 4)
128
129
130
131
132
133 #define cam_err(cam, fmt, arg...) \
134 dev_err(&(cam)->pdev->dev, fmt, ##arg);
135 #define cam_warn(cam, fmt, arg...) \
136 dev_warn(&(cam)->pdev->dev, fmt, ##arg);
137
138
139
140
141
142
143
144 #define CAFE_SMBUS_TIMEOUT (HZ)
145
146 static inline struct cafe_camera *to_cam(struct v4l2_device *dev)
147 {
148 struct mcam_camera *m = container_of(dev, struct mcam_camera, v4l2_dev);
149 return container_of(m, struct cafe_camera, mcam);
150 }
151
152
153 static int cafe_smbus_write_done(struct mcam_camera *mcam)
154 {
155 unsigned long flags;
156 int c1;
157
158
159
160
161
162
163 udelay(20);
164 spin_lock_irqsave(&mcam->dev_lock, flags);
165 c1 = mcam_reg_read(mcam, REG_TWSIC1);
166 spin_unlock_irqrestore(&mcam->dev_lock, flags);
167 return (c1 & (TWSIC1_WSTAT|TWSIC1_ERROR)) != TWSIC1_WSTAT;
168 }
169
170 static int cafe_smbus_write_data(struct cafe_camera *cam,
171 u16 addr, u8 command, u8 value)
172 {
173 unsigned int rval;
174 unsigned long flags;
175 struct mcam_camera *mcam = &cam->mcam;
176
177 spin_lock_irqsave(&mcam->dev_lock, flags);
178 rval = TWSIC0_EN | ((addr << TWSIC0_SID_SHIFT) & TWSIC0_SID);
179 rval |= TWSIC0_OVMAGIC;
180
181
182
183 rval |= TWSIC0_CLKDIV;
184 mcam_reg_write(mcam, REG_TWSIC0, rval);
185 (void) mcam_reg_read(mcam, REG_TWSIC1);
186 rval = value | ((command << TWSIC1_ADDR_SHIFT) & TWSIC1_ADDR);
187 mcam_reg_write(mcam, REG_TWSIC1, rval);
188 spin_unlock_irqrestore(&mcam->dev_lock, flags);
189
190
191
192
193
194
195
196
197 mdelay(2);
198
199
200
201
202
203
204
205
206
207
208 wait_event_timeout(cam->smbus_wait, cafe_smbus_write_done(mcam),
209 CAFE_SMBUS_TIMEOUT);
210
211 spin_lock_irqsave(&mcam->dev_lock, flags);
212 rval = mcam_reg_read(mcam, REG_TWSIC1);
213 spin_unlock_irqrestore(&mcam->dev_lock, flags);
214
215 if (rval & TWSIC1_WSTAT) {
216 cam_err(cam, "SMBUS write (%02x/%02x/%02x) timed out\n", addr,
217 command, value);
218 return -EIO;
219 }
220 if (rval & TWSIC1_ERROR) {
221 cam_err(cam, "SMBUS write (%02x/%02x/%02x) error\n", addr,
222 command, value);
223 return -EIO;
224 }
225 return 0;
226 }
227
228
229
230 static int cafe_smbus_read_done(struct mcam_camera *mcam)
231 {
232 unsigned long flags;
233 int c1;
234
235
236
237
238
239
240 udelay(20);
241 spin_lock_irqsave(&mcam->dev_lock, flags);
242 c1 = mcam_reg_read(mcam, REG_TWSIC1);
243 spin_unlock_irqrestore(&mcam->dev_lock, flags);
244 return c1 & (TWSIC1_RVALID|TWSIC1_ERROR);
245 }
246
247
248
249 static int cafe_smbus_read_data(struct cafe_camera *cam,
250 u16 addr, u8 command, u8 *value)
251 {
252 unsigned int rval;
253 unsigned long flags;
254 struct mcam_camera *mcam = &cam->mcam;
255
256 spin_lock_irqsave(&mcam->dev_lock, flags);
257 rval = TWSIC0_EN | ((addr << TWSIC0_SID_SHIFT) & TWSIC0_SID);
258 rval |= TWSIC0_OVMAGIC;
259
260
261
262 rval |= TWSIC0_CLKDIV;
263 mcam_reg_write(mcam, REG_TWSIC0, rval);
264 (void) mcam_reg_read(mcam, REG_TWSIC1);
265 rval = TWSIC1_READ | ((command << TWSIC1_ADDR_SHIFT) & TWSIC1_ADDR);
266 mcam_reg_write(mcam, REG_TWSIC1, rval);
267 spin_unlock_irqrestore(&mcam->dev_lock, flags);
268
269 wait_event_timeout(cam->smbus_wait,
270 cafe_smbus_read_done(mcam), CAFE_SMBUS_TIMEOUT);
271 spin_lock_irqsave(&mcam->dev_lock, flags);
272 rval = mcam_reg_read(mcam, REG_TWSIC1);
273 spin_unlock_irqrestore(&mcam->dev_lock, flags);
274
275 if (rval & TWSIC1_ERROR) {
276 cam_err(cam, "SMBUS read (%02x/%02x) error\n", addr, command);
277 return -EIO;
278 }
279 if (!(rval & TWSIC1_RVALID)) {
280 cam_err(cam, "SMBUS read (%02x/%02x) timed out\n", addr,
281 command);
282 return -EIO;
283 }
284 *value = rval & 0xff;
285 return 0;
286 }
287
288
289
290
291
292 static int cafe_smbus_xfer(struct i2c_adapter *adapter, u16 addr,
293 unsigned short flags, char rw, u8 command,
294 int size, union i2c_smbus_data *data)
295 {
296 struct cafe_camera *cam = i2c_get_adapdata(adapter);
297 int ret = -EINVAL;
298
299
300
301
302
303 if (size != I2C_SMBUS_BYTE_DATA) {
304 cam_err(cam, "funky xfer size %d\n", size);
305 return -EINVAL;
306 }
307
308 if (rw == I2C_SMBUS_WRITE)
309 ret = cafe_smbus_write_data(cam, addr, command, data->byte);
310 else if (rw == I2C_SMBUS_READ)
311 ret = cafe_smbus_read_data(cam, addr, command, &data->byte);
312 return ret;
313 }
314
315
316 static void cafe_smbus_enable_irq(struct cafe_camera *cam)
317 {
318 unsigned long flags;
319
320 spin_lock_irqsave(&cam->mcam.dev_lock, flags);
321 mcam_reg_set_bit(&cam->mcam, REG_IRQMASK, TWSIIRQS);
322 spin_unlock_irqrestore(&cam->mcam.dev_lock, flags);
323 }
324
325 static u32 cafe_smbus_func(struct i2c_adapter *adapter)
326 {
327 return I2C_FUNC_SMBUS_READ_BYTE_DATA |
328 I2C_FUNC_SMBUS_WRITE_BYTE_DATA;
329 }
330
331 static const struct i2c_algorithm cafe_smbus_algo = {
332 .smbus_xfer = cafe_smbus_xfer,
333 .functionality = cafe_smbus_func
334 };
335
336 static int cafe_smbus_setup(struct cafe_camera *cam)
337 {
338 struct i2c_adapter *adap;
339 int ret;
340
341 adap = kzalloc(sizeof(*adap), GFP_KERNEL);
342 if (adap == NULL)
343 return -ENOMEM;
344 adap->owner = THIS_MODULE;
345 adap->algo = &cafe_smbus_algo;
346 strscpy(adap->name, "cafe_ccic", sizeof(adap->name));
347 adap->dev.parent = &cam->pdev->dev;
348 i2c_set_adapdata(adap, cam);
349 ret = i2c_add_adapter(adap);
350 if (ret) {
351 printk(KERN_ERR "Unable to register cafe i2c adapter\n");
352 kfree(adap);
353 return ret;
354 }
355
356 cam->i2c_adapter = adap;
357 cafe_smbus_enable_irq(cam);
358 return 0;
359 }
360
361 static void cafe_smbus_shutdown(struct cafe_camera *cam)
362 {
363 i2c_del_adapter(cam->i2c_adapter);
364 kfree(cam->i2c_adapter);
365 }
366
367
368
369
370
371
372 static void cafe_ctlr_init(struct mcam_camera *mcam)
373 {
374 unsigned long flags;
375
376 spin_lock_irqsave(&mcam->dev_lock, flags);
377
378
379
380 mcam_reg_write(mcam, 0x3038, 0x8);
381 mcam_reg_write(mcam, 0x315c, 0x80008);
382
383
384
385
386
387
388 mcam_reg_write(mcam, REG_GL_CSR, GCSR_SRS|GCSR_MRS);
389 mcam_reg_write(mcam, REG_GL_CSR, GCSR_SRC|GCSR_MRC);
390 mcam_reg_write(mcam, REG_GL_CSR, GCSR_SRC|GCSR_MRS);
391
392
393
394 spin_unlock_irqrestore(&mcam->dev_lock, flags);
395 msleep(5);
396 spin_lock_irqsave(&mcam->dev_lock, flags);
397
398 mcam_reg_write(mcam, REG_GL_CSR, GCSR_CCIC_EN|GCSR_SRC|GCSR_MRC);
399 mcam_reg_set_bit(mcam, REG_GL_IMASK, GIMSK_CCIC_EN);
400
401
402
403 mcam_reg_write(mcam, REG_IRQMASK, 0);
404 spin_unlock_irqrestore(&mcam->dev_lock, flags);
405 }
406
407
408 static int cafe_ctlr_power_up(struct mcam_camera *mcam)
409 {
410
411
412
413
414 mcam_reg_write(mcam, REG_GL_FCR, GFCR_GPIO_ON);
415 mcam_reg_write(mcam, REG_GL_GPIOR, GGPIO_OUT|GGPIO_VAL);
416
417
418
419
420
421 mcam_reg_write(mcam, REG_GPR, GPR_C1EN|GPR_C0EN);
422 mcam_reg_write(mcam, REG_GPR, GPR_C1EN|GPR_C0EN|GPR_C0);
423
424 return 0;
425 }
426
427 static void cafe_ctlr_power_down(struct mcam_camera *mcam)
428 {
429 mcam_reg_write(mcam, REG_GPR, GPR_C1EN|GPR_C0EN|GPR_C1);
430 mcam_reg_write(mcam, REG_GL_FCR, GFCR_GPIO_ON);
431 mcam_reg_write(mcam, REG_GL_GPIOR, GGPIO_OUT);
432 }
433
434
435
436
437
438
439 static irqreturn_t cafe_irq(int irq, void *data)
440 {
441 struct cafe_camera *cam = data;
442 struct mcam_camera *mcam = &cam->mcam;
443 unsigned int irqs, handled;
444
445 spin_lock(&mcam->dev_lock);
446 irqs = mcam_reg_read(mcam, REG_IRQSTAT);
447 handled = cam->registered && mccic_irq(mcam, irqs);
448 if (irqs & TWSIIRQS) {
449 mcam_reg_write(mcam, REG_IRQSTAT, TWSIIRQS);
450 wake_up(&cam->smbus_wait);
451 handled = 1;
452 }
453 spin_unlock(&mcam->dev_lock);
454 return IRQ_RETVAL(handled);
455 }
456
457
458
459 static struct ov7670_config sensor_cfg = {
460
461
462
463
464 .min_width = 320,
465 .min_height = 240,
466
467
468
469
470
471 .clock_speed = 45,
472 .use_smbus = 1,
473 };
474
475 static struct i2c_board_info ov7670_info = {
476 .type = "ov7670",
477 .addr = 0x42 >> 1,
478 .platform_data = &sensor_cfg,
479 };
480
481
482
483
484
485
486 static int cafe_pci_probe(struct pci_dev *pdev,
487 const struct pci_device_id *id)
488 {
489 int ret;
490 struct cafe_camera *cam;
491 struct mcam_camera *mcam;
492
493
494
495
496 ret = -ENOMEM;
497 cam = kzalloc(sizeof(struct cafe_camera), GFP_KERNEL);
498 if (cam == NULL)
499 goto out;
500 cam->pdev = pdev;
501 mcam = &cam->mcam;
502 mcam->chip_id = MCAM_CAFE;
503 spin_lock_init(&mcam->dev_lock);
504 init_waitqueue_head(&cam->smbus_wait);
505 mcam->plat_power_up = cafe_ctlr_power_up;
506 mcam->plat_power_down = cafe_ctlr_power_down;
507 mcam->dev = &pdev->dev;
508 snprintf(mcam->bus_info, sizeof(mcam->bus_info), "PCI:%s", pci_name(pdev));
509
510
511
512
513
514 mcam->buffer_mode = B_vmalloc;
515
516
517
518 ret = pci_enable_device(pdev);
519 if (ret)
520 goto out_free;
521 pci_set_master(pdev);
522
523 ret = -EIO;
524 mcam->regs = pci_iomap(pdev, 0, 0);
525 if (!mcam->regs) {
526 printk(KERN_ERR "Unable to ioremap cafe-ccic regs\n");
527 goto out_disable;
528 }
529 mcam->regs_size = pci_resource_len(pdev, 0);
530 ret = request_irq(pdev->irq, cafe_irq, IRQF_SHARED, "cafe-ccic", cam);
531 if (ret)
532 goto out_iounmap;
533
534
535
536
537 cafe_ctlr_init(mcam);
538
539
540
541
542
543
544 ret = cafe_smbus_setup(cam);
545 if (ret)
546 goto out_pdown;
547
548 mcam->asd.match_type = V4L2_ASYNC_MATCH_I2C;
549 mcam->asd.match.i2c.adapter_id = i2c_adapter_id(cam->i2c_adapter);
550 mcam->asd.match.i2c.address = ov7670_info.addr;
551
552 ret = mccic_register(mcam);
553 if (ret)
554 goto out_smbus_shutdown;
555
556 clkdev_create(mcam->mclk, "xclk", "%d-%04x",
557 i2c_adapter_id(cam->i2c_adapter), ov7670_info.addr);
558
559 if (i2c_new_device(cam->i2c_adapter, &ov7670_info)) {
560 cam->registered = 1;
561 return 0;
562 }
563
564 mccic_shutdown(mcam);
565 out_smbus_shutdown:
566 cafe_smbus_shutdown(cam);
567 out_pdown:
568 cafe_ctlr_power_down(mcam);
569 free_irq(pdev->irq, cam);
570 out_iounmap:
571 pci_iounmap(pdev, mcam->regs);
572 out_disable:
573 pci_disable_device(pdev);
574 out_free:
575 kfree(cam);
576 out:
577 return ret;
578 }
579
580
581
582
583
584 static void cafe_shutdown(struct cafe_camera *cam)
585 {
586 mccic_shutdown(&cam->mcam);
587 cafe_smbus_shutdown(cam);
588 free_irq(cam->pdev->irq, cam);
589 pci_iounmap(cam->pdev, cam->mcam.regs);
590 }
591
592
593 static void cafe_pci_remove(struct pci_dev *pdev)
594 {
595 struct v4l2_device *v4l2_dev = dev_get_drvdata(&pdev->dev);
596 struct cafe_camera *cam = to_cam(v4l2_dev);
597
598 if (cam == NULL) {
599 printk(KERN_WARNING "pci_remove on unknown pdev %p\n", pdev);
600 return;
601 }
602 cafe_shutdown(cam);
603 kfree(cam);
604 }
605
606
607 #ifdef CONFIG_PM
608
609
610
611 static int cafe_pci_suspend(struct pci_dev *pdev, pm_message_t state)
612 {
613 struct v4l2_device *v4l2_dev = dev_get_drvdata(&pdev->dev);
614 struct cafe_camera *cam = to_cam(v4l2_dev);
615 int ret;
616
617 ret = pci_save_state(pdev);
618 if (ret)
619 return ret;
620 mccic_suspend(&cam->mcam);
621 pci_disable_device(pdev);
622 return 0;
623 }
624
625
626 static int cafe_pci_resume(struct pci_dev *pdev)
627 {
628 struct v4l2_device *v4l2_dev = dev_get_drvdata(&pdev->dev);
629 struct cafe_camera *cam = to_cam(v4l2_dev);
630 int ret = 0;
631
632 pci_restore_state(pdev);
633 ret = pci_enable_device(pdev);
634
635 if (ret) {
636 cam_warn(cam, "Unable to re-enable device on resume!\n");
637 return ret;
638 }
639 cafe_ctlr_init(&cam->mcam);
640 return mccic_resume(&cam->mcam);
641 }
642
643 #endif
644
645 static const struct pci_device_id cafe_ids[] = {
646 { PCI_DEVICE(PCI_VENDOR_ID_MARVELL,
647 PCI_DEVICE_ID_MARVELL_88ALP01_CCIC) },
648 { 0, }
649 };
650
651 MODULE_DEVICE_TABLE(pci, cafe_ids);
652
653 static struct pci_driver cafe_pci_driver = {
654 .name = "cafe1000-ccic",
655 .id_table = cafe_ids,
656 .probe = cafe_pci_probe,
657 .remove = cafe_pci_remove,
658 #ifdef CONFIG_PM
659 .suspend = cafe_pci_suspend,
660 .resume = cafe_pci_resume,
661 #endif
662 };
663
664
665
666
667 static int __init cafe_init(void)
668 {
669 int ret;
670
671 printk(KERN_NOTICE "Marvell M88ALP01 'CAFE' Camera Controller version %d\n",
672 CAFE_VERSION);
673 ret = pci_register_driver(&cafe_pci_driver);
674 if (ret) {
675 printk(KERN_ERR "Unable to register cafe_ccic driver\n");
676 goto out;
677 }
678 ret = 0;
679
680 out:
681 return ret;
682 }
683
684
685 static void __exit cafe_exit(void)
686 {
687 pci_unregister_driver(&cafe_pci_driver);
688 }
689
690 module_init(cafe_init);
691 module_exit(cafe_exit);