1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2010-2013 Bluecherry, LLC <https://www.bluecherrydvr.com>
6 * Ben Collins <bcollins@ubuntu.com>
9 * John Brooks <john.brooks@bluecherry.net>
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/pci.h>
15 #include <linux/interrupt.h>
16 #include <linux/videodev2.h>
17 #include <linux/delay.h>
18 #include <linux/sysfs.h>
19 #include <linux/ktime.h>
20 #include <linux/slab.h>
23 #include "solo6x10-tw28.h"
25 MODULE_DESCRIPTION("Softlogic 6x10 MPEG4/H.264/G.723 CODEC V4L2/ALSA Driver");
26 MODULE_AUTHOR("Bluecherry <maintainers@bluecherrydvr.com>");
27 MODULE_VERSION(SOLO6X10_VERSION
);
28 MODULE_LICENSE("GPL");
30 static unsigned video_nr
= -1;
31 module_param(video_nr
, uint
, 0644);
32 MODULE_PARM_DESC(video_nr
, "videoX start number, -1 is autodetect (default)");
34 static int full_eeprom
; /* default is only top 64B */
35 module_param(full_eeprom
, uint
, 0644);
36 MODULE_PARM_DESC(full_eeprom
, "Allow access to full 128B EEPROM (dangerous)");
39 static void solo_set_time(struct solo_dev
*solo_dev
)
45 /* no overflow because we use monotonic timestamps */
46 solo_reg_write(solo_dev
, SOLO_TIMER_SEC
, (u32
)ts
.tv_sec
);
47 solo_reg_write(solo_dev
, SOLO_TIMER_USEC
, (u32
)ts
.tv_nsec
/ NSEC_PER_USEC
);
50 static void solo_timer_sync(struct solo_dev
*solo_dev
)
56 if (solo_dev
->type
!= SOLO_DEV_6110
)
59 if (++solo_dev
->time_sync
< 60)
62 solo_dev
->time_sync
= 0;
64 sec
= solo_reg_read(solo_dev
, SOLO_TIMER_SEC
);
65 usec
= solo_reg_read(solo_dev
, SOLO_TIMER_USEC
);
69 diff
= (s32
)ts
.tv_sec
- (s32
)sec
;
70 diff
= (diff
* 1000000)
71 + ((s32
)(ts
.tv_nsec
/ NSEC_PER_USEC
) - (s32
)usec
);
73 if (diff
> 1000 || diff
< -1000) {
74 solo_set_time(solo_dev
);
76 long usec_lsb
= solo_dev
->usec_lsb
;
81 else if (usec_lsb
> 255)
84 solo_dev
->usec_lsb
= usec_lsb
;
85 solo_reg_write(solo_dev
, SOLO_TIMER_USEC_LSB
,
90 static irqreturn_t
solo_isr(int irq
, void *data
)
92 struct solo_dev
*solo_dev
= data
;
96 status
= solo_reg_read(solo_dev
, SOLO_IRQ_STAT
);
100 /* Acknowledge all interrupts immediately */
101 solo_reg_write(solo_dev
, SOLO_IRQ_STAT
, status
);
103 if (status
& SOLO_IRQ_PCI_ERR
)
104 solo_p2m_error_isr(solo_dev
);
106 for (i
= 0; i
< SOLO_NR_P2M
; i
++)
107 if (status
& SOLO_IRQ_P2M(i
))
108 solo_p2m_isr(solo_dev
, i
);
110 if (status
& SOLO_IRQ_IIC
)
111 solo_i2c_isr(solo_dev
);
113 if (status
& SOLO_IRQ_VIDEO_IN
) {
114 solo_video_in_isr(solo_dev
);
115 solo_timer_sync(solo_dev
);
118 if (status
& SOLO_IRQ_ENCODER
)
119 solo_enc_v4l2_isr(solo_dev
);
121 if (status
& SOLO_IRQ_G723
)
122 solo_g723_isr(solo_dev
);
127 static void free_solo_dev(struct solo_dev
*solo_dev
)
129 struct pci_dev
*pdev
= solo_dev
->pdev
;
131 if (solo_dev
->dev
.parent
)
132 device_unregister(&solo_dev
->dev
);
134 if (solo_dev
->reg_base
) {
135 /* Bring down the sub-devices first */
136 solo_g723_exit(solo_dev
);
137 solo_enc_v4l2_exit(solo_dev
);
138 solo_enc_exit(solo_dev
);
139 solo_v4l2_exit(solo_dev
);
140 solo_disp_exit(solo_dev
);
141 solo_gpio_exit(solo_dev
);
142 solo_p2m_exit(solo_dev
);
143 solo_i2c_exit(solo_dev
);
145 /* Now cleanup the PCI device */
146 solo_irq_off(solo_dev
, ~0);
147 free_irq(pdev
->irq
, solo_dev
);
148 pci_iounmap(pdev
, solo_dev
->reg_base
);
151 pci_release_regions(pdev
);
152 pci_disable_device(pdev
);
153 v4l2_device_unregister(&solo_dev
->v4l2_dev
);
154 pci_set_drvdata(pdev
, NULL
);
159 static ssize_t
eeprom_store(struct device
*dev
, struct device_attribute
*attr
,
160 const char *buf
, size_t count
)
162 struct solo_dev
*solo_dev
=
163 container_of(dev
, struct solo_dev
, dev
);
168 dev_warn(dev
, "EEPROM Write not aligned (truncating)\n");
170 if (!full_eeprom
&& count
> 64) {
171 dev_warn(dev
, "EEPROM Write truncated to 64 bytes\n");
173 } else if (full_eeprom
&& count
> 128) {
174 dev_warn(dev
, "EEPROM Write truncated to 128 bytes\n");
178 solo_eeprom_ewen(solo_dev
, 1);
180 for (i
= full_eeprom
? 0 : 32; i
< min((int)(full_eeprom
? 64 : 32),
181 (int)(count
/ 2)); i
++)
182 solo_eeprom_write(solo_dev
, i
, cpu_to_be16(p
[i
]));
184 solo_eeprom_ewen(solo_dev
, 0);
189 static ssize_t
eeprom_show(struct device
*dev
, struct device_attribute
*attr
,
192 struct solo_dev
*solo_dev
=
193 container_of(dev
, struct solo_dev
, dev
);
195 int count
= (full_eeprom
? 128 : 64);
198 for (i
= (full_eeprom
? 0 : 32); i
< (count
/ 2); i
++)
199 p
[i
] = be16_to_cpu(solo_eeprom_read(solo_dev
, i
));
204 static ssize_t
p2m_timeouts_show(struct device
*dev
,
205 struct device_attribute
*attr
,
208 struct solo_dev
*solo_dev
=
209 container_of(dev
, struct solo_dev
, dev
);
211 return sprintf(buf
, "%d\n", solo_dev
->p2m_timeouts
);
214 static ssize_t
sdram_size_show(struct device
*dev
,
215 struct device_attribute
*attr
,
218 struct solo_dev
*solo_dev
=
219 container_of(dev
, struct solo_dev
, dev
);
221 return sprintf(buf
, "%dMegs\n", solo_dev
->sdram_size
>> 20);
224 static ssize_t
tw28xx_show(struct device
*dev
,
225 struct device_attribute
*attr
,
228 struct solo_dev
*solo_dev
=
229 container_of(dev
, struct solo_dev
, dev
);
231 return sprintf(buf
, "tw2815[%d] tw2864[%d] tw2865[%d]\n",
232 hweight32(solo_dev
->tw2815
),
233 hweight32(solo_dev
->tw2864
),
234 hweight32(solo_dev
->tw2865
));
237 static ssize_t
input_map_show(struct device
*dev
,
238 struct device_attribute
*attr
,
241 struct solo_dev
*solo_dev
=
242 container_of(dev
, struct solo_dev
, dev
);
246 val
= solo_reg_read(solo_dev
, SOLO_VI_CH_SWITCH_0
);
247 out
+= sprintf(out
, "Channel 0 => Input %d\n", val
& 0x1f);
248 out
+= sprintf(out
, "Channel 1 => Input %d\n", (val
>> 5) & 0x1f);
249 out
+= sprintf(out
, "Channel 2 => Input %d\n", (val
>> 10) & 0x1f);
250 out
+= sprintf(out
, "Channel 3 => Input %d\n", (val
>> 15) & 0x1f);
251 out
+= sprintf(out
, "Channel 4 => Input %d\n", (val
>> 20) & 0x1f);
252 out
+= sprintf(out
, "Channel 5 => Input %d\n", (val
>> 25) & 0x1f);
254 val
= solo_reg_read(solo_dev
, SOLO_VI_CH_SWITCH_1
);
255 out
+= sprintf(out
, "Channel 6 => Input %d\n", val
& 0x1f);
256 out
+= sprintf(out
, "Channel 7 => Input %d\n", (val
>> 5) & 0x1f);
257 out
+= sprintf(out
, "Channel 8 => Input %d\n", (val
>> 10) & 0x1f);
258 out
+= sprintf(out
, "Channel 9 => Input %d\n", (val
>> 15) & 0x1f);
259 out
+= sprintf(out
, "Channel 10 => Input %d\n", (val
>> 20) & 0x1f);
260 out
+= sprintf(out
, "Channel 11 => Input %d\n", (val
>> 25) & 0x1f);
262 val
= solo_reg_read(solo_dev
, SOLO_VI_CH_SWITCH_2
);
263 out
+= sprintf(out
, "Channel 12 => Input %d\n", val
& 0x1f);
264 out
+= sprintf(out
, "Channel 13 => Input %d\n", (val
>> 5) & 0x1f);
265 out
+= sprintf(out
, "Channel 14 => Input %d\n", (val
>> 10) & 0x1f);
266 out
+= sprintf(out
, "Channel 15 => Input %d\n", (val
>> 15) & 0x1f);
267 out
+= sprintf(out
, "Spot Output => Input %d\n", (val
>> 20) & 0x1f);
272 static ssize_t
p2m_timeout_store(struct device
*dev
,
273 struct device_attribute
*attr
,
274 const char *buf
, size_t count
)
276 struct solo_dev
*solo_dev
=
277 container_of(dev
, struct solo_dev
, dev
);
279 int ret
= kstrtoul(buf
, 10, &ms
);
281 if (ret
< 0 || ms
> 200)
283 solo_dev
->p2m_jiffies
= msecs_to_jiffies(ms
);
288 static ssize_t
p2m_timeout_show(struct device
*dev
,
289 struct device_attribute
*attr
,
292 struct solo_dev
*solo_dev
=
293 container_of(dev
, struct solo_dev
, dev
);
295 return sprintf(buf
, "%ums\n", jiffies_to_msecs(solo_dev
->p2m_jiffies
));
298 static ssize_t
intervals_show(struct device
*dev
,
299 struct device_attribute
*attr
,
302 struct solo_dev
*solo_dev
=
303 container_of(dev
, struct solo_dev
, dev
);
305 int fps
= solo_dev
->fps
;
308 for (i
= 0; i
< solo_dev
->nr_chans
; i
++) {
309 out
+= sprintf(out
, "Channel %d: %d/%d (0x%08x)\n",
310 i
, solo_dev
->v4l2_enc
[i
]->interval
, fps
,
311 solo_reg_read(solo_dev
, SOLO_CAP_CH_INTV(i
)));
317 static ssize_t
sdram_offsets_show(struct device
*dev
,
318 struct device_attribute
*attr
,
321 struct solo_dev
*solo_dev
=
322 container_of(dev
, struct solo_dev
, dev
);
325 out
+= sprintf(out
, "DISP: 0x%08x @ 0x%08x\n",
329 out
+= sprintf(out
, "EOSD: 0x%08x @ 0x%08x (0x%08x * %d)\n",
331 SOLO_EOSD_EXT_AREA(solo_dev
),
332 SOLO_EOSD_EXT_SIZE(solo_dev
),
333 SOLO_EOSD_EXT_AREA(solo_dev
) /
334 SOLO_EOSD_EXT_SIZE(solo_dev
));
336 out
+= sprintf(out
, "MOTI: 0x%08x @ 0x%08x\n",
337 SOLO_MOTION_EXT_ADDR(solo_dev
),
338 SOLO_MOTION_EXT_SIZE
);
340 out
+= sprintf(out
, "G723: 0x%08x @ 0x%08x\n",
341 SOLO_G723_EXT_ADDR(solo_dev
),
344 out
+= sprintf(out
, "CAPT: 0x%08x @ 0x%08x (0x%08x * %d)\n",
345 SOLO_CAP_EXT_ADDR(solo_dev
),
346 SOLO_CAP_EXT_SIZE(solo_dev
),
348 SOLO_CAP_EXT_SIZE(solo_dev
) / SOLO_CAP_PAGE_SIZE
);
350 out
+= sprintf(out
, "EREF: 0x%08x @ 0x%08x (0x%08x * %d)\n",
351 SOLO_EREF_EXT_ADDR(solo_dev
),
352 SOLO_EREF_EXT_AREA(solo_dev
),
354 SOLO_EREF_EXT_AREA(solo_dev
) / SOLO_EREF_EXT_SIZE
);
356 out
+= sprintf(out
, "MPEG: 0x%08x @ 0x%08x\n",
357 SOLO_MP4E_EXT_ADDR(solo_dev
),
358 SOLO_MP4E_EXT_SIZE(solo_dev
));
360 out
+= sprintf(out
, "JPEG: 0x%08x @ 0x%08x\n",
361 SOLO_JPEG_EXT_ADDR(solo_dev
),
362 SOLO_JPEG_EXT_SIZE(solo_dev
));
367 static ssize_t
sdram_show(struct file
*file
, struct kobject
*kobj
,
368 struct bin_attribute
*a
, char *buf
,
369 loff_t off
, size_t count
)
371 struct device
*dev
= container_of(kobj
, struct device
, kobj
);
372 struct solo_dev
*solo_dev
=
373 container_of(dev
, struct solo_dev
, dev
);
374 const int size
= solo_dev
->sdram_size
;
379 if (off
+ count
> size
)
382 if (solo_p2m_dma(solo_dev
, 0, buf
, off
, count
, 0, 0))
388 static const struct device_attribute solo_dev_attrs
[] = {
389 __ATTR(eeprom
, 0640, eeprom_show
, eeprom_store
),
390 __ATTR(p2m_timeout
, 0644, p2m_timeout_show
, p2m_timeout_store
),
391 __ATTR_RO(p2m_timeouts
),
392 __ATTR_RO(sdram_size
),
394 __ATTR_RO(input_map
),
395 __ATTR_RO(intervals
),
396 __ATTR_RO(sdram_offsets
),
399 static void solo_device_release(struct device
*dev
)
404 static int solo_sysfs_init(struct solo_dev
*solo_dev
)
406 struct bin_attribute
*sdram_attr
= &solo_dev
->sdram_attr
;
407 struct device
*dev
= &solo_dev
->dev
;
411 if (solo_dev
->type
== SOLO_DEV_6110
)
416 dev
->release
= solo_device_release
;
417 dev
->parent
= &solo_dev
->pdev
->dev
;
418 set_dev_node(dev
, dev_to_node(&solo_dev
->pdev
->dev
));
419 dev_set_name(dev
, "%s-%d-%d", driver
, solo_dev
->vfd
->num
,
422 if (device_register(dev
)) {
427 for (i
= 0; i
< ARRAY_SIZE(solo_dev_attrs
); i
++) {
428 if (device_create_file(dev
, &solo_dev_attrs
[i
])) {
429 device_unregister(dev
);
434 sysfs_attr_init(&sdram_attr
->attr
);
435 sdram_attr
->attr
.name
= "sdram";
436 sdram_attr
->attr
.mode
= 0440;
437 sdram_attr
->read
= sdram_show
;
438 sdram_attr
->size
= solo_dev
->sdram_size
;
440 if (device_create_bin_file(dev
, sdram_attr
)) {
441 device_unregister(dev
);
448 static int solo_pci_probe(struct pci_dev
*pdev
, const struct pci_device_id
*id
)
450 struct solo_dev
*solo_dev
;
454 solo_dev
= kzalloc(sizeof(*solo_dev
), GFP_KERNEL
);
455 if (solo_dev
== NULL
)
458 if (id
->driver_data
== SOLO_DEV_6010
)
459 dev_info(&pdev
->dev
, "Probing Softlogic 6010\n");
461 dev_info(&pdev
->dev
, "Probing Softlogic 6110\n");
463 solo_dev
->type
= id
->driver_data
;
464 solo_dev
->pdev
= pdev
;
465 ret
= v4l2_device_register(&pdev
->dev
, &solo_dev
->v4l2_dev
);
469 /* Only for during init */
470 solo_dev
->p2m_jiffies
= msecs_to_jiffies(100);
472 ret
= pci_enable_device(pdev
);
476 pci_set_master(pdev
);
478 /* RETRY/TRDY Timeout disabled */
479 pci_write_config_byte(pdev
, 0x40, 0x00);
480 pci_write_config_byte(pdev
, 0x41, 0x00);
482 ret
= pci_request_regions(pdev
, SOLO6X10_NAME
);
486 solo_dev
->reg_base
= pci_ioremap_bar(pdev
, 0);
487 if (solo_dev
->reg_base
== NULL
) {
492 chip_id
= solo_reg_read(solo_dev
, SOLO_CHIP_OPTION
) &
496 solo_dev
->nr_chans
= 16;
497 solo_dev
->nr_ext
= 5;
500 solo_dev
->nr_chans
= 8;
501 solo_dev
->nr_ext
= 2;
504 dev_warn(&pdev
->dev
, "Invalid chip_id 0x%02x, assuming 4 ch\n",
508 solo_dev
->nr_chans
= 4;
509 solo_dev
->nr_ext
= 1;
512 /* Disable all interrupts to start */
513 solo_irq_off(solo_dev
, ~0);
515 /* Initial global settings */
516 if (solo_dev
->type
== SOLO_DEV_6010
) {
517 solo_dev
->clock_mhz
= 108;
518 solo_dev
->sys_config
= SOLO_SYS_CFG_SDRAM64BIT
519 | SOLO_SYS_CFG_INPUTDIV(25)
520 | SOLO_SYS_CFG_FEEDBACKDIV(solo_dev
->clock_mhz
* 2 - 2)
521 | SOLO_SYS_CFG_OUTDIV(3);
522 solo_reg_write(solo_dev
, SOLO_SYS_CFG
, solo_dev
->sys_config
);
526 solo_dev
->clock_mhz
= 135;
528 if (solo_dev
->clock_mhz
< 125) {
530 divf
= (solo_dev
->clock_mhz
* 4) / 3 - 1;
533 divf
= (solo_dev
->clock_mhz
* 2) / 3 - 1;
536 solo_reg_write(solo_dev
, SOLO_PLL_CONFIG
,
537 (1 << 20) | /* PLL_RANGE */
538 (8 << 15) | /* PLL_DIVR */
541 (1 << 1) /* PLL_FSEN */);
543 solo_dev
->sys_config
= SOLO_SYS_CFG_SDRAM64BIT
;
546 solo_reg_write(solo_dev
, SOLO_SYS_CFG
, solo_dev
->sys_config
);
547 solo_reg_write(solo_dev
, SOLO_TIMER_CLOCK_NUM
,
548 solo_dev
->clock_mhz
- 1);
550 /* PLL locking time of 1ms */
553 ret
= request_irq(pdev
->irq
, solo_isr
, IRQF_SHARED
, SOLO6X10_NAME
,
558 /* Handle this from the start */
559 solo_irq_on(solo_dev
, SOLO_IRQ_PCI_ERR
);
561 ret
= solo_i2c_init(solo_dev
);
565 /* Setup the DMA engine */
566 solo_reg_write(solo_dev
, SOLO_DMA_CTRL
,
567 SOLO_DMA_CTRL_REFRESH_CYCLE(1) |
568 SOLO_DMA_CTRL_SDRAM_SIZE(2) |
569 SOLO_DMA_CTRL_SDRAM_CLK_INVERT
|
570 SOLO_DMA_CTRL_READ_CLK_SELECT
|
571 SOLO_DMA_CTRL_LATENCY(1));
573 /* Undocumented crap */
574 solo_reg_write(solo_dev
, SOLO_DMA_CTRL1
,
575 solo_dev
->type
== SOLO_DEV_6010
? 0x100 : 0x300);
577 if (solo_dev
->type
!= SOLO_DEV_6010
) {
578 solo_dev
->usec_lsb
= 0x3f;
579 solo_set_time(solo_dev
);
582 /* Disable watchdog */
583 solo_reg_write(solo_dev
, SOLO_WATCHDOG
, 0);
585 /* Initialize sub components */
587 ret
= solo_p2m_init(solo_dev
);
591 ret
= solo_disp_init(solo_dev
);
595 ret
= solo_gpio_init(solo_dev
);
599 ret
= solo_tw28_init(solo_dev
);
603 ret
= solo_v4l2_init(solo_dev
, video_nr
);
607 ret
= solo_enc_init(solo_dev
);
611 ret
= solo_enc_v4l2_init(solo_dev
, video_nr
);
615 ret
= solo_g723_init(solo_dev
);
619 ret
= solo_sysfs_init(solo_dev
);
623 /* Now that init is over, set this lower */
624 solo_dev
->p2m_jiffies
= msecs_to_jiffies(20);
629 free_solo_dev(solo_dev
);
633 static void solo_pci_remove(struct pci_dev
*pdev
)
635 struct v4l2_device
*v4l2_dev
= pci_get_drvdata(pdev
);
636 struct solo_dev
*solo_dev
= container_of(v4l2_dev
, struct solo_dev
, v4l2_dev
);
638 free_solo_dev(solo_dev
);
641 static const struct pci_device_id solo_id_table
[] = {
642 /* 6010 based cards */
643 { PCI_DEVICE(PCI_VENDOR_ID_SOFTLOGIC
, PCI_DEVICE_ID_SOLO6010
),
644 .driver_data
= SOLO_DEV_6010
},
645 { PCI_DEVICE(PCI_VENDOR_ID_BLUECHERRY
, PCI_DEVICE_ID_NEUSOLO_4
),
646 .driver_data
= SOLO_DEV_6010
},
647 { PCI_DEVICE(PCI_VENDOR_ID_BLUECHERRY
, PCI_DEVICE_ID_NEUSOLO_9
),
648 .driver_data
= SOLO_DEV_6010
},
649 { PCI_DEVICE(PCI_VENDOR_ID_BLUECHERRY
, PCI_DEVICE_ID_NEUSOLO_16
),
650 .driver_data
= SOLO_DEV_6010
},
651 { PCI_DEVICE(PCI_VENDOR_ID_BLUECHERRY
, PCI_DEVICE_ID_BC_SOLO_4
),
652 .driver_data
= SOLO_DEV_6010
},
653 { PCI_DEVICE(PCI_VENDOR_ID_BLUECHERRY
, PCI_DEVICE_ID_BC_SOLO_9
),
654 .driver_data
= SOLO_DEV_6010
},
655 { PCI_DEVICE(PCI_VENDOR_ID_BLUECHERRY
, PCI_DEVICE_ID_BC_SOLO_16
),
656 .driver_data
= SOLO_DEV_6010
},
657 /* 6110 based cards */
658 { PCI_DEVICE(PCI_VENDOR_ID_SOFTLOGIC
, PCI_DEVICE_ID_SOLO6110
),
659 .driver_data
= SOLO_DEV_6110
},
660 { PCI_DEVICE(PCI_VENDOR_ID_BLUECHERRY
, PCI_DEVICE_ID_BC_6110_4
),
661 .driver_data
= SOLO_DEV_6110
},
662 { PCI_DEVICE(PCI_VENDOR_ID_BLUECHERRY
, PCI_DEVICE_ID_BC_6110_8
),
663 .driver_data
= SOLO_DEV_6110
},
664 { PCI_DEVICE(PCI_VENDOR_ID_BLUECHERRY
, PCI_DEVICE_ID_BC_6110_16
),
665 .driver_data
= SOLO_DEV_6110
},
669 MODULE_DEVICE_TABLE(pci
, solo_id_table
);
671 static struct pci_driver solo_pci_driver
= {
672 .name
= SOLO6X10_NAME
,
673 .id_table
= solo_id_table
,
674 .probe
= solo_pci_probe
,
675 .remove
= solo_pci_remove
,
678 module_pci_driver(solo_pci_driver
);