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);
149 pci_disable_device(pdev
);
150 v4l2_device_unregister(&solo_dev
->v4l2_dev
);
151 pci_set_drvdata(pdev
, NULL
);
156 static ssize_t
eeprom_store(struct device
*dev
, struct device_attribute
*attr
,
157 const char *buf
, size_t count
)
159 struct solo_dev
*solo_dev
=
160 container_of(dev
, struct solo_dev
, dev
);
165 dev_warn(dev
, "EEPROM Write not aligned (truncating)\n");
167 if (!full_eeprom
&& count
> 64) {
168 dev_warn(dev
, "EEPROM Write truncated to 64 bytes\n");
170 } else if (full_eeprom
&& count
> 128) {
171 dev_warn(dev
, "EEPROM Write truncated to 128 bytes\n");
175 solo_eeprom_ewen(solo_dev
, 1);
177 for (i
= full_eeprom
? 0 : 32; i
< min((int)(full_eeprom
? 64 : 32),
178 (int)(count
/ 2)); i
++)
179 solo_eeprom_write(solo_dev
, i
, cpu_to_be16(p
[i
]));
181 solo_eeprom_ewen(solo_dev
, 0);
186 static ssize_t
eeprom_show(struct device
*dev
, struct device_attribute
*attr
,
189 struct solo_dev
*solo_dev
=
190 container_of(dev
, struct solo_dev
, dev
);
192 int count
= (full_eeprom
? 128 : 64);
195 for (i
= (full_eeprom
? 0 : 32); i
< (count
/ 2); i
++)
196 p
[i
] = be16_to_cpu(solo_eeprom_read(solo_dev
, i
));
201 static ssize_t
p2m_timeouts_show(struct device
*dev
,
202 struct device_attribute
*attr
,
205 struct solo_dev
*solo_dev
=
206 container_of(dev
, struct solo_dev
, dev
);
208 return sprintf(buf
, "%d\n", solo_dev
->p2m_timeouts
);
211 static ssize_t
sdram_size_show(struct device
*dev
,
212 struct device_attribute
*attr
,
215 struct solo_dev
*solo_dev
=
216 container_of(dev
, struct solo_dev
, dev
);
218 return sprintf(buf
, "%dMegs\n", solo_dev
->sdram_size
>> 20);
221 static ssize_t
tw28xx_show(struct device
*dev
,
222 struct device_attribute
*attr
,
225 struct solo_dev
*solo_dev
=
226 container_of(dev
, struct solo_dev
, dev
);
228 return sprintf(buf
, "tw2815[%d] tw2864[%d] tw2865[%d]\n",
229 hweight32(solo_dev
->tw2815
),
230 hweight32(solo_dev
->tw2864
),
231 hweight32(solo_dev
->tw2865
));
234 static ssize_t
input_map_show(struct device
*dev
,
235 struct device_attribute
*attr
,
238 struct solo_dev
*solo_dev
=
239 container_of(dev
, struct solo_dev
, dev
);
243 val
= solo_reg_read(solo_dev
, SOLO_VI_CH_SWITCH_0
);
244 out
+= sprintf(out
, "Channel 0 => Input %d\n", val
& 0x1f);
245 out
+= sprintf(out
, "Channel 1 => Input %d\n", (val
>> 5) & 0x1f);
246 out
+= sprintf(out
, "Channel 2 => Input %d\n", (val
>> 10) & 0x1f);
247 out
+= sprintf(out
, "Channel 3 => Input %d\n", (val
>> 15) & 0x1f);
248 out
+= sprintf(out
, "Channel 4 => Input %d\n", (val
>> 20) & 0x1f);
249 out
+= sprintf(out
, "Channel 5 => Input %d\n", (val
>> 25) & 0x1f);
251 val
= solo_reg_read(solo_dev
, SOLO_VI_CH_SWITCH_1
);
252 out
+= sprintf(out
, "Channel 6 => Input %d\n", val
& 0x1f);
253 out
+= sprintf(out
, "Channel 7 => Input %d\n", (val
>> 5) & 0x1f);
254 out
+= sprintf(out
, "Channel 8 => Input %d\n", (val
>> 10) & 0x1f);
255 out
+= sprintf(out
, "Channel 9 => Input %d\n", (val
>> 15) & 0x1f);
256 out
+= sprintf(out
, "Channel 10 => Input %d\n", (val
>> 20) & 0x1f);
257 out
+= sprintf(out
, "Channel 11 => Input %d\n", (val
>> 25) & 0x1f);
259 val
= solo_reg_read(solo_dev
, SOLO_VI_CH_SWITCH_2
);
260 out
+= sprintf(out
, "Channel 12 => Input %d\n", val
& 0x1f);
261 out
+= sprintf(out
, "Channel 13 => Input %d\n", (val
>> 5) & 0x1f);
262 out
+= sprintf(out
, "Channel 14 => Input %d\n", (val
>> 10) & 0x1f);
263 out
+= sprintf(out
, "Channel 15 => Input %d\n", (val
>> 15) & 0x1f);
264 out
+= sprintf(out
, "Spot Output => Input %d\n", (val
>> 20) & 0x1f);
269 static ssize_t
p2m_timeout_store(struct device
*dev
,
270 struct device_attribute
*attr
,
271 const char *buf
, size_t count
)
273 struct solo_dev
*solo_dev
=
274 container_of(dev
, struct solo_dev
, dev
);
276 int ret
= kstrtoul(buf
, 10, &ms
);
278 if (ret
< 0 || ms
> 200)
280 solo_dev
->p2m_jiffies
= msecs_to_jiffies(ms
);
285 static ssize_t
p2m_timeout_show(struct device
*dev
,
286 struct device_attribute
*attr
,
289 struct solo_dev
*solo_dev
=
290 container_of(dev
, struct solo_dev
, dev
);
292 return sprintf(buf
, "%ums\n", jiffies_to_msecs(solo_dev
->p2m_jiffies
));
295 static ssize_t
intervals_show(struct device
*dev
,
296 struct device_attribute
*attr
,
299 struct solo_dev
*solo_dev
=
300 container_of(dev
, struct solo_dev
, dev
);
302 int fps
= solo_dev
->fps
;
305 for (i
= 0; i
< solo_dev
->nr_chans
; i
++) {
306 out
+= sprintf(out
, "Channel %d: %d/%d (0x%08x)\n",
307 i
, solo_dev
->v4l2_enc
[i
]->interval
, fps
,
308 solo_reg_read(solo_dev
, SOLO_CAP_CH_INTV(i
)));
314 static ssize_t
sdram_offsets_show(struct device
*dev
,
315 struct device_attribute
*attr
,
318 struct solo_dev
*solo_dev
=
319 container_of(dev
, struct solo_dev
, dev
);
322 out
+= sprintf(out
, "DISP: 0x%08x @ 0x%08x\n",
326 out
+= sprintf(out
, "EOSD: 0x%08x @ 0x%08x (0x%08x * %d)\n",
328 SOLO_EOSD_EXT_AREA(solo_dev
),
329 SOLO_EOSD_EXT_SIZE(solo_dev
),
330 SOLO_EOSD_EXT_AREA(solo_dev
) /
331 SOLO_EOSD_EXT_SIZE(solo_dev
));
333 out
+= sprintf(out
, "MOTI: 0x%08x @ 0x%08x\n",
334 SOLO_MOTION_EXT_ADDR(solo_dev
),
335 SOLO_MOTION_EXT_SIZE
);
337 out
+= sprintf(out
, "G723: 0x%08x @ 0x%08x\n",
338 SOLO_G723_EXT_ADDR(solo_dev
),
341 out
+= sprintf(out
, "CAPT: 0x%08x @ 0x%08x (0x%08x * %d)\n",
342 SOLO_CAP_EXT_ADDR(solo_dev
),
343 SOLO_CAP_EXT_SIZE(solo_dev
),
345 SOLO_CAP_EXT_SIZE(solo_dev
) / SOLO_CAP_PAGE_SIZE
);
347 out
+= sprintf(out
, "EREF: 0x%08x @ 0x%08x (0x%08x * %d)\n",
348 SOLO_EREF_EXT_ADDR(solo_dev
),
349 SOLO_EREF_EXT_AREA(solo_dev
),
351 SOLO_EREF_EXT_AREA(solo_dev
) / SOLO_EREF_EXT_SIZE
);
353 out
+= sprintf(out
, "MPEG: 0x%08x @ 0x%08x\n",
354 SOLO_MP4E_EXT_ADDR(solo_dev
),
355 SOLO_MP4E_EXT_SIZE(solo_dev
));
357 out
+= sprintf(out
, "JPEG: 0x%08x @ 0x%08x\n",
358 SOLO_JPEG_EXT_ADDR(solo_dev
),
359 SOLO_JPEG_EXT_SIZE(solo_dev
));
364 static ssize_t
sdram_show(struct file
*file
, struct kobject
*kobj
,
365 struct bin_attribute
*a
, char *buf
,
366 loff_t off
, size_t count
)
368 struct device
*dev
= kobj_to_dev(kobj
);
369 struct solo_dev
*solo_dev
=
370 container_of(dev
, struct solo_dev
, dev
);
371 const int size
= solo_dev
->sdram_size
;
376 if (off
+ count
> size
)
379 if (solo_p2m_dma(solo_dev
, 0, buf
, off
, count
, 0, 0))
385 static const struct device_attribute solo_dev_attrs
[] = {
386 __ATTR(eeprom
, 0640, eeprom_show
, eeprom_store
),
387 __ATTR(p2m_timeout
, 0644, p2m_timeout_show
, p2m_timeout_store
),
388 __ATTR_RO(p2m_timeouts
),
389 __ATTR_RO(sdram_size
),
391 __ATTR_RO(input_map
),
392 __ATTR_RO(intervals
),
393 __ATTR_RO(sdram_offsets
),
396 static void solo_device_release(struct device
*dev
)
401 static int solo_sysfs_init(struct solo_dev
*solo_dev
)
403 struct bin_attribute
*sdram_attr
= &solo_dev
->sdram_attr
;
404 struct device
*dev
= &solo_dev
->dev
;
408 if (solo_dev
->type
== SOLO_DEV_6110
)
413 dev
->release
= solo_device_release
;
414 dev
->parent
= &solo_dev
->pdev
->dev
;
415 set_dev_node(dev
, dev_to_node(&solo_dev
->pdev
->dev
));
416 dev_set_name(dev
, "%s-%d-%d", driver
, solo_dev
->vfd
->num
,
419 if (device_register(dev
)) {
425 for (i
= 0; i
< ARRAY_SIZE(solo_dev_attrs
); i
++) {
426 if (device_create_file(dev
, &solo_dev_attrs
[i
])) {
427 device_unregister(dev
);
432 sysfs_attr_init(&sdram_attr
->attr
);
433 sdram_attr
->attr
.name
= "sdram";
434 sdram_attr
->attr
.mode
= 0440;
435 sdram_attr
->read
= sdram_show
;
436 sdram_attr
->size
= solo_dev
->sdram_size
;
438 if (device_create_bin_file(dev
, sdram_attr
)) {
439 device_unregister(dev
);
446 static int solo_pci_probe(struct pci_dev
*pdev
, const struct pci_device_id
*id
)
448 struct solo_dev
*solo_dev
;
452 solo_dev
= kzalloc(sizeof(*solo_dev
), GFP_KERNEL
);
453 if (solo_dev
== NULL
)
456 if (id
->driver_data
== SOLO_DEV_6010
)
457 dev_info(&pdev
->dev
, "Probing Softlogic 6010\n");
459 dev_info(&pdev
->dev
, "Probing Softlogic 6110\n");
461 solo_dev
->type
= id
->driver_data
;
462 solo_dev
->pdev
= pdev
;
463 ret
= v4l2_device_register(&pdev
->dev
, &solo_dev
->v4l2_dev
);
467 /* Only for during init */
468 solo_dev
->p2m_jiffies
= msecs_to_jiffies(100);
470 ret
= pci_enable_device(pdev
);
474 pci_set_master(pdev
);
476 /* RETRY/TRDY Timeout disabled */
477 pci_write_config_byte(pdev
, 0x40, 0x00);
478 pci_write_config_byte(pdev
, 0x41, 0x00);
480 ret
= pcim_iomap_regions(pdev
, BIT(0), SOLO6X10_NAME
);
483 solo_dev
->reg_base
= pcim_iomap_table(pdev
)[0];
485 chip_id
= solo_reg_read(solo_dev
, SOLO_CHIP_OPTION
) &
489 solo_dev
->nr_chans
= 16;
490 solo_dev
->nr_ext
= 5;
493 solo_dev
->nr_chans
= 8;
494 solo_dev
->nr_ext
= 2;
497 dev_warn(&pdev
->dev
, "Invalid chip_id 0x%02x, assuming 4 ch\n",
501 solo_dev
->nr_chans
= 4;
502 solo_dev
->nr_ext
= 1;
505 /* Disable all interrupts to start */
506 solo_irq_off(solo_dev
, ~0);
508 /* Initial global settings */
509 if (solo_dev
->type
== SOLO_DEV_6010
) {
510 solo_dev
->clock_mhz
= 108;
511 solo_dev
->sys_config
= SOLO_SYS_CFG_SDRAM64BIT
512 | SOLO_SYS_CFG_INPUTDIV(25)
513 | SOLO_SYS_CFG_FEEDBACKDIV(solo_dev
->clock_mhz
* 2 - 2)
514 | SOLO_SYS_CFG_OUTDIV(3);
515 solo_reg_write(solo_dev
, SOLO_SYS_CFG
, solo_dev
->sys_config
);
519 solo_dev
->clock_mhz
= 135;
521 if (solo_dev
->clock_mhz
< 125) {
523 divf
= (solo_dev
->clock_mhz
* 4) / 3 - 1;
526 divf
= (solo_dev
->clock_mhz
* 2) / 3 - 1;
529 solo_reg_write(solo_dev
, SOLO_PLL_CONFIG
,
530 (1 << 20) | /* PLL_RANGE */
531 (8 << 15) | /* PLL_DIVR */
534 (1 << 1) /* PLL_FSEN */);
536 solo_dev
->sys_config
= SOLO_SYS_CFG_SDRAM64BIT
;
539 solo_reg_write(solo_dev
, SOLO_SYS_CFG
, solo_dev
->sys_config
);
540 solo_reg_write(solo_dev
, SOLO_TIMER_CLOCK_NUM
,
541 solo_dev
->clock_mhz
- 1);
543 /* PLL locking time of 1ms */
546 ret
= devm_request_irq(&pdev
->dev
, pdev
->irq
, solo_isr
, IRQF_SHARED
,
547 SOLO6X10_NAME
, solo_dev
);
551 /* Handle this from the start */
552 solo_irq_on(solo_dev
, SOLO_IRQ_PCI_ERR
);
554 ret
= solo_i2c_init(solo_dev
);
558 /* Setup the DMA engine */
559 solo_reg_write(solo_dev
, SOLO_DMA_CTRL
,
560 SOLO_DMA_CTRL_REFRESH_CYCLE(1) |
561 SOLO_DMA_CTRL_SDRAM_SIZE(2) |
562 SOLO_DMA_CTRL_SDRAM_CLK_INVERT
|
563 SOLO_DMA_CTRL_READ_CLK_SELECT
|
564 SOLO_DMA_CTRL_LATENCY(1));
566 /* Undocumented crap */
567 solo_reg_write(solo_dev
, SOLO_DMA_CTRL1
,
568 solo_dev
->type
== SOLO_DEV_6010
? 0x100 : 0x300);
570 if (solo_dev
->type
!= SOLO_DEV_6010
) {
571 solo_dev
->usec_lsb
= 0x3f;
572 solo_set_time(solo_dev
);
575 /* Disable watchdog */
576 solo_reg_write(solo_dev
, SOLO_WATCHDOG
, 0);
578 /* Initialize sub components */
580 ret
= solo_p2m_init(solo_dev
);
584 ret
= solo_disp_init(solo_dev
);
588 ret
= solo_gpio_init(solo_dev
);
592 ret
= solo_tw28_init(solo_dev
);
596 ret
= solo_v4l2_init(solo_dev
, video_nr
);
600 ret
= solo_enc_init(solo_dev
);
604 ret
= solo_enc_v4l2_init(solo_dev
, video_nr
);
608 ret
= solo_g723_init(solo_dev
);
612 ret
= solo_sysfs_init(solo_dev
);
616 /* Now that init is over, set this lower */
617 solo_dev
->p2m_jiffies
= msecs_to_jiffies(20);
622 free_solo_dev(solo_dev
);
626 static void solo_pci_remove(struct pci_dev
*pdev
)
628 struct v4l2_device
*v4l2_dev
= pci_get_drvdata(pdev
);
629 struct solo_dev
*solo_dev
= container_of(v4l2_dev
, struct solo_dev
, v4l2_dev
);
631 free_solo_dev(solo_dev
);
634 static const struct pci_device_id solo_id_table
[] = {
635 /* 6010 based cards */
636 { PCI_DEVICE(PCI_VENDOR_ID_SOFTLOGIC
, PCI_DEVICE_ID_SOLO6010
),
637 .driver_data
= SOLO_DEV_6010
},
638 { PCI_DEVICE(PCI_VENDOR_ID_BLUECHERRY
, PCI_DEVICE_ID_NEUSOLO_4
),
639 .driver_data
= SOLO_DEV_6010
},
640 { PCI_DEVICE(PCI_VENDOR_ID_BLUECHERRY
, PCI_DEVICE_ID_NEUSOLO_9
),
641 .driver_data
= SOLO_DEV_6010
},
642 { PCI_DEVICE(PCI_VENDOR_ID_BLUECHERRY
, PCI_DEVICE_ID_NEUSOLO_16
),
643 .driver_data
= SOLO_DEV_6010
},
644 { PCI_DEVICE(PCI_VENDOR_ID_BLUECHERRY
, PCI_DEVICE_ID_BC_SOLO_4
),
645 .driver_data
= SOLO_DEV_6010
},
646 { PCI_DEVICE(PCI_VENDOR_ID_BLUECHERRY
, PCI_DEVICE_ID_BC_SOLO_9
),
647 .driver_data
= SOLO_DEV_6010
},
648 { PCI_DEVICE(PCI_VENDOR_ID_BLUECHERRY
, PCI_DEVICE_ID_BC_SOLO_16
),
649 .driver_data
= SOLO_DEV_6010
},
650 /* 6110 based cards */
651 { PCI_DEVICE(PCI_VENDOR_ID_SOFTLOGIC
, PCI_DEVICE_ID_SOLO6110
),
652 .driver_data
= SOLO_DEV_6110
},
653 { PCI_DEVICE(PCI_VENDOR_ID_BLUECHERRY
, PCI_DEVICE_ID_BC_6110_4
),
654 .driver_data
= SOLO_DEV_6110
},
655 { PCI_DEVICE(PCI_VENDOR_ID_BLUECHERRY
, PCI_DEVICE_ID_BC_6110_8
),
656 .driver_data
= SOLO_DEV_6110
},
657 { PCI_DEVICE(PCI_VENDOR_ID_BLUECHERRY
, PCI_DEVICE_ID_BC_6110_16
),
658 .driver_data
= SOLO_DEV_6110
},
662 MODULE_DEVICE_TABLE(pci
, solo_id_table
);
664 static struct pci_driver solo_pci_driver
= {
665 .name
= SOLO6X10_NAME
,
666 .id_table
= solo_id_table
,
667 .probe
= solo_pci_probe
,
668 .remove
= solo_pci_remove
,
671 module_pci_driver(solo_pci_driver
);