2 * Copyright (C) 2010-2013 Bluecherry, LLC <http://www.bluecherrydvr.com>
5 * Ben Collins <bcollins@ubuntu.com>
8 * John Brooks <john.brooks@bluecherry.net>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 #include <linux/kernel.h>
26 #include <linux/module.h>
27 #include <linux/pci.h>
28 #include <linux/interrupt.h>
29 #include <linux/videodev2.h>
30 #include <linux/delay.h>
31 #include <linux/sysfs.h>
32 #include <linux/ktime.h>
33 #include <linux/slab.h>
38 MODULE_DESCRIPTION("Softlogic 6x10 MPEG4/H.264/G.723 CODEC V4L2/ALSA Driver");
39 MODULE_AUTHOR("Bluecherry <maintainers@bluecherrydvr.com>");
40 MODULE_VERSION(SOLO6X10_VERSION);
41 MODULE_LICENSE("GPL");
43 unsigned video_nr = -1;
44 module_param(video_nr, uint, 0644);
45 MODULE_PARM_DESC(video_nr, "videoX start number, -1 is autodetect (default)");
47 static int full_eeprom; /* default is only top 64B */
48 module_param(full_eeprom, uint, 0644);
49 MODULE_PARM_DESC(full_eeprom, "Allow access to full 128B EEPROM (dangerous)");
52 static void solo_set_time(struct solo_dev *solo_dev)
58 solo_reg_write(solo_dev, SOLO_TIMER_SEC, ts.tv_sec);
59 solo_reg_write(solo_dev, SOLO_TIMER_USEC, ts.tv_nsec / NSEC_PER_USEC);
62 static void solo_timer_sync(struct solo_dev *solo_dev)
68 if (solo_dev->type != SOLO_DEV_6110)
71 if (++solo_dev->time_sync < 60)
74 solo_dev->time_sync = 0;
76 sec = solo_reg_read(solo_dev, SOLO_TIMER_SEC);
77 usec = solo_reg_read(solo_dev, SOLO_TIMER_USEC);
81 diff = (long)ts.tv_sec - (long)sec;
82 diff = (diff * 1000000)
83 + ((long)(ts.tv_nsec / NSEC_PER_USEC) - (long)usec);
85 if (diff > 1000 || diff < -1000) {
86 solo_set_time(solo_dev);
88 long usec_lsb = solo_dev->usec_lsb;
93 else if (usec_lsb > 255)
96 solo_dev->usec_lsb = usec_lsb;
97 solo_reg_write(solo_dev, SOLO_TIMER_USEC_LSB,
102 static irqreturn_t solo_isr(int irq, void *data)
104 struct solo_dev *solo_dev = data;
108 status = solo_reg_read(solo_dev, SOLO_IRQ_STAT);
112 if (status & ~solo_dev->irq_mask) {
113 solo_reg_write(solo_dev, SOLO_IRQ_STAT,
114 status & ~solo_dev->irq_mask);
115 status &= solo_dev->irq_mask;
118 if (status & SOLO_IRQ_PCI_ERR)
119 solo_p2m_error_isr(solo_dev);
121 for (i = 0; i < SOLO_NR_P2M; i++)
122 if (status & SOLO_IRQ_P2M(i))
123 solo_p2m_isr(solo_dev, i);
125 if (status & SOLO_IRQ_IIC)
126 solo_i2c_isr(solo_dev);
128 if (status & SOLO_IRQ_VIDEO_IN) {
129 solo_video_in_isr(solo_dev);
130 solo_timer_sync(solo_dev);
133 if (status & SOLO_IRQ_ENCODER)
134 solo_enc_v4l2_isr(solo_dev);
136 if (status & SOLO_IRQ_G723)
137 solo_g723_isr(solo_dev);
139 /* Clear all interrupts handled */
140 solo_reg_write(solo_dev, SOLO_IRQ_STAT, status);
145 static void free_solo_dev(struct solo_dev *solo_dev)
147 struct pci_dev *pdev;
152 if (solo_dev->dev.parent)
153 device_unregister(&solo_dev->dev);
155 pdev = solo_dev->pdev;
157 /* If we never initialized the PCI device, then nothing else
158 * below here needs cleanup */
164 if (solo_dev->reg_base) {
165 /* Bring down the sub-devices first */
166 solo_g723_exit(solo_dev);
167 solo_enc_v4l2_exit(solo_dev);
168 solo_enc_exit(solo_dev);
169 solo_v4l2_exit(solo_dev);
170 solo_disp_exit(solo_dev);
171 solo_gpio_exit(solo_dev);
172 solo_p2m_exit(solo_dev);
173 solo_i2c_exit(solo_dev);
175 /* Now cleanup the PCI device */
176 solo_irq_off(solo_dev, ~0);
177 pci_iounmap(pdev, solo_dev->reg_base);
179 free_irq(pdev->irq, solo_dev);
182 pci_release_regions(pdev);
183 pci_disable_device(pdev);
184 pci_set_drvdata(pdev, NULL);
189 static ssize_t eeprom_store(struct device *dev, struct device_attribute *attr,
190 const char *buf, size_t count)
192 struct solo_dev *solo_dev =
193 container_of(dev, struct solo_dev, dev);
194 unsigned short *p = (unsigned short *)buf;
198 dev_warn(dev, "EEPROM Write not aligned (truncating)\n");
200 if (!full_eeprom && count > 64) {
201 dev_warn(dev, "EEPROM Write truncated to 64 bytes\n");
203 } else if (full_eeprom && count > 128) {
204 dev_warn(dev, "EEPROM Write truncated to 128 bytes\n");
208 solo_eeprom_ewen(solo_dev, 1);
210 for (i = full_eeprom ? 0 : 32; i < min((int)(full_eeprom ? 64 : 32),
211 (int)(count / 2)); i++)
212 solo_eeprom_write(solo_dev, i, cpu_to_be16(p[i]));
214 solo_eeprom_ewen(solo_dev, 0);
219 static ssize_t eeprom_show(struct device *dev, struct device_attribute *attr,
222 struct solo_dev *solo_dev =
223 container_of(dev, struct solo_dev, dev);
224 unsigned short *p = (unsigned short *)buf;
225 int count = (full_eeprom ? 128 : 64);
228 for (i = (full_eeprom ? 0 : 32); i < (count / 2); i++)
229 p[i] = be16_to_cpu(solo_eeprom_read(solo_dev, i));
234 static ssize_t video_type_store(struct device *dev,
235 struct device_attribute *attr,
236 const char *buf, size_t count)
241 static ssize_t video_type_show(struct device *dev,
242 struct device_attribute *attr,
245 struct solo_dev *solo_dev =
246 container_of(dev, struct solo_dev, dev);
248 return sprintf(buf, "%s", solo_dev->video_type ==
249 SOLO_VO_FMT_TYPE_NTSC ? "NTSC" : "PAL");
252 static ssize_t p2m_timeouts_show(struct device *dev,
253 struct device_attribute *attr,
256 struct solo_dev *solo_dev =
257 container_of(dev, struct solo_dev, dev);
259 return sprintf(buf, "%d\n", solo_dev->p2m_timeouts);
262 static ssize_t sdram_size_show(struct device *dev,
263 struct device_attribute *attr,
266 struct solo_dev *solo_dev =
267 container_of(dev, struct solo_dev, dev);
269 return sprintf(buf, "%dMegs\n", solo_dev->sdram_size >> 20);
272 static ssize_t tw28xx_show(struct device *dev,
273 struct device_attribute *attr,
276 struct solo_dev *solo_dev =
277 container_of(dev, struct solo_dev, dev);
279 return sprintf(buf, "tw2815[%d] tw2864[%d] tw2865[%d]\n",
280 hweight32(solo_dev->tw2815),
281 hweight32(solo_dev->tw2864),
282 hweight32(solo_dev->tw2865));
285 static ssize_t input_map_show(struct device *dev,
286 struct device_attribute *attr,
289 struct solo_dev *solo_dev =
290 container_of(dev, struct solo_dev, dev);
294 val = solo_reg_read(solo_dev, SOLO_VI_CH_SWITCH_0);
295 out += sprintf(out, "Channel 0 => Input %d\n", val & 0x1f);
296 out += sprintf(out, "Channel 1 => Input %d\n", (val >> 5) & 0x1f);
297 out += sprintf(out, "Channel 2 => Input %d\n", (val >> 10) & 0x1f);
298 out += sprintf(out, "Channel 3 => Input %d\n", (val >> 15) & 0x1f);
299 out += sprintf(out, "Channel 4 => Input %d\n", (val >> 20) & 0x1f);
300 out += sprintf(out, "Channel 5 => Input %d\n", (val >> 25) & 0x1f);
302 val = solo_reg_read(solo_dev, SOLO_VI_CH_SWITCH_1);
303 out += sprintf(out, "Channel 6 => Input %d\n", val & 0x1f);
304 out += sprintf(out, "Channel 7 => Input %d\n", (val >> 5) & 0x1f);
305 out += sprintf(out, "Channel 8 => Input %d\n", (val >> 10) & 0x1f);
306 out += sprintf(out, "Channel 9 => Input %d\n", (val >> 15) & 0x1f);
307 out += sprintf(out, "Channel 10 => Input %d\n", (val >> 20) & 0x1f);
308 out += sprintf(out, "Channel 11 => Input %d\n", (val >> 25) & 0x1f);
310 val = solo_reg_read(solo_dev, SOLO_VI_CH_SWITCH_2);
311 out += sprintf(out, "Channel 12 => Input %d\n", val & 0x1f);
312 out += sprintf(out, "Channel 13 => Input %d\n", (val >> 5) & 0x1f);
313 out += sprintf(out, "Channel 14 => Input %d\n", (val >> 10) & 0x1f);
314 out += sprintf(out, "Channel 15 => Input %d\n", (val >> 15) & 0x1f);
315 out += sprintf(out, "Spot Output => Input %d\n", (val >> 20) & 0x1f);
320 static ssize_t p2m_timeout_store(struct device *dev,
321 struct device_attribute *attr,
322 const char *buf, size_t count)
324 struct solo_dev *solo_dev =
325 container_of(dev, struct solo_dev, dev);
328 int ret = kstrtoul(buf, 10, &ms);
329 if (ret < 0 || ms > 200)
331 solo_dev->p2m_jiffies = msecs_to_jiffies(ms);
336 static ssize_t p2m_timeout_show(struct device *dev,
337 struct device_attribute *attr,
340 struct solo_dev *solo_dev =
341 container_of(dev, struct solo_dev, dev);
343 return sprintf(buf, "%ums\n", jiffies_to_msecs(solo_dev->p2m_jiffies));
346 static ssize_t intervals_show(struct device *dev,
347 struct device_attribute *attr,
350 struct solo_dev *solo_dev =
351 container_of(dev, struct solo_dev, dev);
353 int fps = solo_dev->fps;
356 for (i = 0; i < solo_dev->nr_chans; i++) {
357 out += sprintf(out, "Channel %d: %d/%d (0x%08x)\n",
358 i, solo_dev->v4l2_enc[i]->interval, fps,
359 solo_reg_read(solo_dev, SOLO_CAP_CH_INTV(i)));
365 static ssize_t sdram_offsets_show(struct device *dev,
366 struct device_attribute *attr,
369 struct solo_dev *solo_dev =
370 container_of(dev, struct solo_dev, dev);
373 out += sprintf(out, "DISP: 0x%08x @ 0x%08x\n",
377 out += sprintf(out, "EOSD: 0x%08x @ 0x%08x (0x%08x * %d)\n",
379 SOLO_EOSD_EXT_AREA(solo_dev),
380 SOLO_EOSD_EXT_SIZE(solo_dev),
381 SOLO_EOSD_EXT_AREA(solo_dev) /
382 SOLO_EOSD_EXT_SIZE(solo_dev));
384 out += sprintf(out, "MOTI: 0x%08x @ 0x%08x\n",
385 SOLO_MOTION_EXT_ADDR(solo_dev),
386 SOLO_MOTION_EXT_SIZE);
388 out += sprintf(out, "G723: 0x%08x @ 0x%08x\n",
389 SOLO_G723_EXT_ADDR(solo_dev),
392 out += sprintf(out, "CAPT: 0x%08x @ 0x%08x (0x%08x * %d)\n",
393 SOLO_CAP_EXT_ADDR(solo_dev),
394 SOLO_CAP_EXT_SIZE(solo_dev),
396 SOLO_CAP_EXT_SIZE(solo_dev) / SOLO_CAP_PAGE_SIZE);
398 out += sprintf(out, "EREF: 0x%08x @ 0x%08x (0x%08x * %d)\n",
399 SOLO_EREF_EXT_ADDR(solo_dev),
400 SOLO_EREF_EXT_AREA(solo_dev),
402 SOLO_EREF_EXT_AREA(solo_dev) / SOLO_EREF_EXT_SIZE);
404 out += sprintf(out, "MPEG: 0x%08x @ 0x%08x\n",
405 SOLO_MP4E_EXT_ADDR(solo_dev),
406 SOLO_MP4E_EXT_SIZE(solo_dev));
408 out += sprintf(out, "JPEG: 0x%08x @ 0x%08x\n",
409 SOLO_JPEG_EXT_ADDR(solo_dev),
410 SOLO_JPEG_EXT_SIZE(solo_dev));
415 static ssize_t sdram_show(struct file *file, struct kobject *kobj,
416 struct bin_attribute *a, char *buf,
417 loff_t off, size_t count)
419 struct device *dev = container_of(kobj, struct device, kobj);
420 struct solo_dev *solo_dev =
421 container_of(dev, struct solo_dev, dev);
422 const int size = solo_dev->sdram_size;
427 if (off + count > size)
430 if (solo_p2m_dma(solo_dev, 0, buf, off, count, 0, 0))
436 static const struct device_attribute solo_dev_attrs[] = {
437 __ATTR(eeprom, 0640, eeprom_show, eeprom_store),
438 __ATTR(video_type, 0644, video_type_show, video_type_store),
439 __ATTR(p2m_timeout, 0644, p2m_timeout_show, p2m_timeout_store),
440 __ATTR_RO(p2m_timeouts),
441 __ATTR_RO(sdram_size),
443 __ATTR_RO(input_map),
444 __ATTR_RO(intervals),
445 __ATTR_RO(sdram_offsets),
448 static void solo_device_release(struct device *dev)
453 static int solo_sysfs_init(struct solo_dev *solo_dev)
455 struct bin_attribute *sdram_attr = &solo_dev->sdram_attr;
456 struct device *dev = &solo_dev->dev;
460 if (solo_dev->type == SOLO_DEV_6110)
465 dev->release = solo_device_release;
466 dev->parent = &solo_dev->pdev->dev;
467 set_dev_node(dev, dev_to_node(&solo_dev->pdev->dev));
468 dev_set_name(dev, "%s-%d-%d", driver, solo_dev->vfd->num,
471 if (device_register(dev)) {
476 for (i = 0; i < ARRAY_SIZE(solo_dev_attrs); i++) {
477 if (device_create_file(dev, &solo_dev_attrs[i])) {
478 device_unregister(dev);
483 sdram_attr->attr.name = "sdram";
484 sdram_attr->attr.mode = 0440;
485 sdram_attr->read = sdram_show;
486 sdram_attr->size = solo_dev->sdram_size;
488 if (device_create_bin_file(dev, sdram_attr)) {
489 device_unregister(dev);
496 static int solo_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
498 struct solo_dev *solo_dev;
502 solo_dev = kzalloc(sizeof(*solo_dev), GFP_KERNEL);
503 if (solo_dev == NULL)
506 if (id->driver_data == SOLO_DEV_6010)
507 dev_info(&pdev->dev, "Probing Softlogic 6010\n");
509 dev_info(&pdev->dev, "Probing Softlogic 6110\n");
511 solo_dev->type = id->driver_data;
512 solo_dev->pdev = pdev;
513 spin_lock_init(&solo_dev->reg_io_lock);
514 pci_set_drvdata(pdev, solo_dev);
516 /* Only for during init */
517 solo_dev->p2m_jiffies = msecs_to_jiffies(100);
519 ret = pci_enable_device(pdev);
523 pci_set_master(pdev);
525 /* RETRY/TRDY Timeout disabled */
526 pci_write_config_byte(pdev, 0x40, 0x00);
527 pci_write_config_byte(pdev, 0x41, 0x00);
529 ret = pci_request_regions(pdev, SOLO6X10_NAME);
533 solo_dev->reg_base = pci_ioremap_bar(pdev, 0);
534 if (solo_dev->reg_base == NULL) {
539 chip_id = solo_reg_read(solo_dev, SOLO_CHIP_OPTION) &
543 solo_dev->nr_chans = 16;
544 solo_dev->nr_ext = 5;
547 solo_dev->nr_chans = 8;
548 solo_dev->nr_ext = 2;
551 dev_warn(&pdev->dev, "Invalid chip_id 0x%02x, assuming 4 ch\n",
554 solo_dev->nr_chans = 4;
555 solo_dev->nr_ext = 1;
558 /* Disable all interrupts to start */
559 solo_irq_off(solo_dev, ~0);
561 /* Initial global settings */
562 if (solo_dev->type == SOLO_DEV_6010) {
563 solo_dev->clock_mhz = 108;
564 solo_dev->sys_config = SOLO_SYS_CFG_SDRAM64BIT
565 | SOLO_SYS_CFG_INPUTDIV(25)
566 | SOLO_SYS_CFG_FEEDBACKDIV(solo_dev->clock_mhz * 2 - 2)
567 | SOLO_SYS_CFG_OUTDIV(3);
568 solo_reg_write(solo_dev, SOLO_SYS_CFG, solo_dev->sys_config);
572 solo_dev->clock_mhz = 135;
574 if (solo_dev->clock_mhz < 125) {
576 divf = (solo_dev->clock_mhz * 4) / 3 - 1;
579 divf = (solo_dev->clock_mhz * 2) / 3 - 1;
582 solo_reg_write(solo_dev, SOLO_PLL_CONFIG,
583 (1 << 20) | /* PLL_RANGE */
584 (8 << 15) | /* PLL_DIVR */
587 (1 << 1) /* PLL_FSEN */);
589 solo_dev->sys_config = SOLO_SYS_CFG_SDRAM64BIT;
592 solo_reg_write(solo_dev, SOLO_SYS_CFG, solo_dev->sys_config);
593 solo_reg_write(solo_dev, SOLO_TIMER_CLOCK_NUM,
594 solo_dev->clock_mhz - 1);
596 /* PLL locking time of 1ms */
599 ret = request_irq(pdev->irq, solo_isr, IRQF_SHARED, SOLO6X10_NAME,
604 /* Handle this from the start */
605 solo_irq_on(solo_dev, SOLO_IRQ_PCI_ERR);
607 ret = solo_i2c_init(solo_dev);
611 /* Setup the DMA engine */
612 solo_reg_write(solo_dev, SOLO_DMA_CTRL,
613 SOLO_DMA_CTRL_REFRESH_CYCLE(1) |
614 SOLO_DMA_CTRL_SDRAM_SIZE(2) |
615 SOLO_DMA_CTRL_SDRAM_CLK_INVERT |
616 SOLO_DMA_CTRL_READ_CLK_SELECT |
617 SOLO_DMA_CTRL_LATENCY(1));
619 /* Undocumented crap */
620 solo_reg_write(solo_dev, SOLO_DMA_CTRL1,
621 solo_dev->type == SOLO_DEV_6010 ? 0x100 : 0x300);
623 if (solo_dev->type != SOLO_DEV_6010) {
624 solo_dev->usec_lsb = 0x3f;
625 solo_set_time(solo_dev);
628 /* Disable watchdog */
629 solo_reg_write(solo_dev, SOLO_WATCHDOG, 0);
631 /* Initialize sub components */
633 ret = solo_p2m_init(solo_dev);
637 ret = solo_disp_init(solo_dev);
641 ret = solo_gpio_init(solo_dev);
645 ret = solo_tw28_init(solo_dev);
649 ret = solo_v4l2_init(solo_dev, video_nr);
653 ret = solo_enc_init(solo_dev);
657 ret = solo_enc_v4l2_init(solo_dev, video_nr);
661 ret = solo_g723_init(solo_dev);
665 ret = solo_sysfs_init(solo_dev);
669 /* Now that init is over, set this lower */
670 solo_dev->p2m_jiffies = msecs_to_jiffies(20);
675 free_solo_dev(solo_dev);
679 static void solo_pci_remove(struct pci_dev *pdev)
681 struct solo_dev *solo_dev = pci_get_drvdata(pdev);
683 free_solo_dev(solo_dev);
686 static DEFINE_PCI_DEVICE_TABLE(solo_id_table) = {
687 /* 6010 based cards */
688 { PCI_DEVICE(PCI_VENDOR_ID_SOFTLOGIC, PCI_DEVICE_ID_SOLO6010),
689 .driver_data = SOLO_DEV_6010 },
690 { PCI_DEVICE(PCI_VENDOR_ID_BLUECHERRY, PCI_DEVICE_ID_NEUSOLO_4),
691 .driver_data = SOLO_DEV_6010 },
692 { PCI_DEVICE(PCI_VENDOR_ID_BLUECHERRY, PCI_DEVICE_ID_NEUSOLO_9),
693 .driver_data = SOLO_DEV_6010 },
694 { PCI_DEVICE(PCI_VENDOR_ID_BLUECHERRY, PCI_DEVICE_ID_NEUSOLO_16),
695 .driver_data = SOLO_DEV_6010 },
696 { PCI_DEVICE(PCI_VENDOR_ID_BLUECHERRY, PCI_DEVICE_ID_BC_SOLO_4),
697 .driver_data = SOLO_DEV_6010 },
698 { PCI_DEVICE(PCI_VENDOR_ID_BLUECHERRY, PCI_DEVICE_ID_BC_SOLO_9),
699 .driver_data = SOLO_DEV_6010 },
700 { PCI_DEVICE(PCI_VENDOR_ID_BLUECHERRY, PCI_DEVICE_ID_BC_SOLO_16),
701 .driver_data = SOLO_DEV_6010 },
702 /* 6110 based cards */
703 { PCI_DEVICE(PCI_VENDOR_ID_SOFTLOGIC, PCI_DEVICE_ID_SOLO6110),
704 .driver_data = SOLO_DEV_6110 },
705 { PCI_DEVICE(PCI_VENDOR_ID_BLUECHERRY, PCI_DEVICE_ID_BC_6110_4),
706 .driver_data = SOLO_DEV_6110 },
707 { PCI_DEVICE(PCI_VENDOR_ID_BLUECHERRY, PCI_DEVICE_ID_BC_6110_8),
708 .driver_data = SOLO_DEV_6110 },
709 { PCI_DEVICE(PCI_VENDOR_ID_BLUECHERRY, PCI_DEVICE_ID_BC_6110_16),
710 .driver_data = SOLO_DEV_6110 },
714 MODULE_DEVICE_TABLE(pci, solo_id_table);
716 static struct pci_driver solo_pci_driver = {
717 .name = SOLO6X10_NAME,
718 .id_table = solo_id_table,
719 .probe = solo_pci_probe,
720 .remove = solo_pci_remove,
723 module_pci_driver(solo_pci_driver);