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