rt2800: initialize BBP_R104 on proper subroutines
[linux/fpc-iii.git] / drivers / media / pci / pt1 / pt1.c
blobe9211086df490c1b7ec9db82f7ab74d82f4fd427
1 /*
2 * driver for Earthsoft PT1/PT2
4 * Copyright (C) 2009 HIRANO Takahito <hiranotaka@zng.info>
6 * based on pt1dvr - http://pt1dvr.sourceforge.jp/
7 * by Tomoaki Ishikawa <tomy@users.sourceforge.jp>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include <linux/kernel.h>
25 #include <linux/module.h>
26 #include <linux/slab.h>
27 #include <linux/vmalloc.h>
28 #include <linux/pci.h>
29 #include <linux/kthread.h>
30 #include <linux/freezer.h>
31 #include <linux/ratelimit.h>
33 #include "dvbdev.h"
34 #include "dvb_demux.h"
35 #include "dmxdev.h"
36 #include "dvb_net.h"
37 #include "dvb_frontend.h"
39 #include "va1j5jf8007t.h"
40 #include "va1j5jf8007s.h"
42 #define DRIVER_NAME "earth-pt1"
44 #define PT1_PAGE_SHIFT 12
45 #define PT1_PAGE_SIZE (1 << PT1_PAGE_SHIFT)
46 #define PT1_NR_UPACKETS 1024
47 #define PT1_NR_BUFS 511
49 struct pt1_buffer_page {
50 __le32 upackets[PT1_NR_UPACKETS];
53 struct pt1_table_page {
54 __le32 next_pfn;
55 __le32 buf_pfns[PT1_NR_BUFS];
58 struct pt1_buffer {
59 struct pt1_buffer_page *page;
60 dma_addr_t addr;
63 struct pt1_table {
64 struct pt1_table_page *page;
65 dma_addr_t addr;
66 struct pt1_buffer bufs[PT1_NR_BUFS];
69 #define PT1_NR_ADAPS 4
71 struct pt1_adapter;
73 struct pt1 {
74 struct pci_dev *pdev;
75 void __iomem *regs;
76 struct i2c_adapter i2c_adap;
77 int i2c_running;
78 struct pt1_adapter *adaps[PT1_NR_ADAPS];
79 struct pt1_table *tables;
80 struct task_struct *kthread;
81 int table_index;
82 int buf_index;
84 struct mutex lock;
85 int power;
86 int reset;
89 struct pt1_adapter {
90 struct pt1 *pt1;
91 int index;
93 u8 *buf;
94 int upacket_count;
95 int packet_count;
96 int st_count;
98 struct dvb_adapter adap;
99 struct dvb_demux demux;
100 int users;
101 struct dmxdev dmxdev;
102 struct dvb_frontend *fe;
103 int (*orig_set_voltage)(struct dvb_frontend *fe,
104 fe_sec_voltage_t voltage);
105 int (*orig_sleep)(struct dvb_frontend *fe);
106 int (*orig_init)(struct dvb_frontend *fe);
108 fe_sec_voltage_t voltage;
109 int sleep;
112 #define pt1_printk(level, pt1, format, arg...) \
113 dev_printk(level, &(pt1)->pdev->dev, format, ##arg)
115 static void pt1_write_reg(struct pt1 *pt1, int reg, u32 data)
117 writel(data, pt1->regs + reg * 4);
120 static u32 pt1_read_reg(struct pt1 *pt1, int reg)
122 return readl(pt1->regs + reg * 4);
125 static int pt1_nr_tables = 8;
126 module_param_named(nr_tables, pt1_nr_tables, int, 0);
128 static void pt1_increment_table_count(struct pt1 *pt1)
130 pt1_write_reg(pt1, 0, 0x00000020);
133 static void pt1_init_table_count(struct pt1 *pt1)
135 pt1_write_reg(pt1, 0, 0x00000010);
138 static void pt1_register_tables(struct pt1 *pt1, u32 first_pfn)
140 pt1_write_reg(pt1, 5, first_pfn);
141 pt1_write_reg(pt1, 0, 0x0c000040);
144 static void pt1_unregister_tables(struct pt1 *pt1)
146 pt1_write_reg(pt1, 0, 0x08080000);
149 static int pt1_sync(struct pt1 *pt1)
151 int i;
152 for (i = 0; i < 57; i++) {
153 if (pt1_read_reg(pt1, 0) & 0x20000000)
154 return 0;
155 pt1_write_reg(pt1, 0, 0x00000008);
157 pt1_printk(KERN_ERR, pt1, "could not sync\n");
158 return -EIO;
161 static u64 pt1_identify(struct pt1 *pt1)
163 int i;
164 u64 id;
165 id = 0;
166 for (i = 0; i < 57; i++) {
167 id |= (u64)(pt1_read_reg(pt1, 0) >> 30 & 1) << i;
168 pt1_write_reg(pt1, 0, 0x00000008);
170 return id;
173 static int pt1_unlock(struct pt1 *pt1)
175 int i;
176 pt1_write_reg(pt1, 0, 0x00000008);
177 for (i = 0; i < 3; i++) {
178 if (pt1_read_reg(pt1, 0) & 0x80000000)
179 return 0;
180 schedule_timeout_uninterruptible((HZ + 999) / 1000);
182 pt1_printk(KERN_ERR, pt1, "could not unlock\n");
183 return -EIO;
186 static int pt1_reset_pci(struct pt1 *pt1)
188 int i;
189 pt1_write_reg(pt1, 0, 0x01010000);
190 pt1_write_reg(pt1, 0, 0x01000000);
191 for (i = 0; i < 10; i++) {
192 if (pt1_read_reg(pt1, 0) & 0x00000001)
193 return 0;
194 schedule_timeout_uninterruptible((HZ + 999) / 1000);
196 pt1_printk(KERN_ERR, pt1, "could not reset PCI\n");
197 return -EIO;
200 static int pt1_reset_ram(struct pt1 *pt1)
202 int i;
203 pt1_write_reg(pt1, 0, 0x02020000);
204 pt1_write_reg(pt1, 0, 0x02000000);
205 for (i = 0; i < 10; i++) {
206 if (pt1_read_reg(pt1, 0) & 0x00000002)
207 return 0;
208 schedule_timeout_uninterruptible((HZ + 999) / 1000);
210 pt1_printk(KERN_ERR, pt1, "could not reset RAM\n");
211 return -EIO;
214 static int pt1_do_enable_ram(struct pt1 *pt1)
216 int i, j;
217 u32 status;
218 status = pt1_read_reg(pt1, 0) & 0x00000004;
219 pt1_write_reg(pt1, 0, 0x00000002);
220 for (i = 0; i < 10; i++) {
221 for (j = 0; j < 1024; j++) {
222 if ((pt1_read_reg(pt1, 0) & 0x00000004) != status)
223 return 0;
225 schedule_timeout_uninterruptible((HZ + 999) / 1000);
227 pt1_printk(KERN_ERR, pt1, "could not enable RAM\n");
228 return -EIO;
231 static int pt1_enable_ram(struct pt1 *pt1)
233 int i, ret;
234 int phase;
235 schedule_timeout_uninterruptible((HZ + 999) / 1000);
236 phase = pt1->pdev->device == 0x211a ? 128 : 166;
237 for (i = 0; i < phase; i++) {
238 ret = pt1_do_enable_ram(pt1);
239 if (ret < 0)
240 return ret;
242 return 0;
245 static void pt1_disable_ram(struct pt1 *pt1)
247 pt1_write_reg(pt1, 0, 0x0b0b0000);
250 static void pt1_set_stream(struct pt1 *pt1, int index, int enabled)
252 pt1_write_reg(pt1, 2, 1 << (index + 8) | enabled << index);
255 static void pt1_init_streams(struct pt1 *pt1)
257 int i;
258 for (i = 0; i < PT1_NR_ADAPS; i++)
259 pt1_set_stream(pt1, i, 0);
262 static int pt1_filter(struct pt1 *pt1, struct pt1_buffer_page *page)
264 u32 upacket;
265 int i;
266 int index;
267 struct pt1_adapter *adap;
268 int offset;
269 u8 *buf;
270 int sc;
272 if (!page->upackets[PT1_NR_UPACKETS - 1])
273 return 0;
275 for (i = 0; i < PT1_NR_UPACKETS; i++) {
276 upacket = le32_to_cpu(page->upackets[i]);
277 index = (upacket >> 29) - 1;
278 if (index < 0 || index >= PT1_NR_ADAPS)
279 continue;
281 adap = pt1->adaps[index];
282 if (upacket >> 25 & 1)
283 adap->upacket_count = 0;
284 else if (!adap->upacket_count)
285 continue;
287 if (upacket >> 24 & 1)
288 printk_ratelimited(KERN_INFO "earth-pt1: device "
289 "buffer overflowing. table[%d] buf[%d]\n",
290 pt1->table_index, pt1->buf_index);
291 sc = upacket >> 26 & 0x7;
292 if (adap->st_count != -1 && sc != ((adap->st_count + 1) & 0x7))
293 printk_ratelimited(KERN_INFO "earth-pt1: data loss"
294 " in streamID(adapter)[%d]\n", index);
295 adap->st_count = sc;
297 buf = adap->buf;
298 offset = adap->packet_count * 188 + adap->upacket_count * 3;
299 buf[offset] = upacket >> 16;
300 buf[offset + 1] = upacket >> 8;
301 if (adap->upacket_count != 62)
302 buf[offset + 2] = upacket;
304 if (++adap->upacket_count >= 63) {
305 adap->upacket_count = 0;
306 if (++adap->packet_count >= 21) {
307 dvb_dmx_swfilter_packets(&adap->demux, buf, 21);
308 adap->packet_count = 0;
313 page->upackets[PT1_NR_UPACKETS - 1] = 0;
314 return 1;
317 static int pt1_thread(void *data)
319 struct pt1 *pt1;
320 struct pt1_buffer_page *page;
322 pt1 = data;
323 set_freezable();
325 while (!kthread_should_stop()) {
326 try_to_freeze();
328 page = pt1->tables[pt1->table_index].bufs[pt1->buf_index].page;
329 if (!pt1_filter(pt1, page)) {
330 schedule_timeout_interruptible((HZ + 999) / 1000);
331 continue;
334 if (++pt1->buf_index >= PT1_NR_BUFS) {
335 pt1_increment_table_count(pt1);
336 pt1->buf_index = 0;
337 if (++pt1->table_index >= pt1_nr_tables)
338 pt1->table_index = 0;
342 return 0;
345 static void pt1_free_page(struct pt1 *pt1, void *page, dma_addr_t addr)
347 dma_free_coherent(&pt1->pdev->dev, PT1_PAGE_SIZE, page, addr);
350 static void *pt1_alloc_page(struct pt1 *pt1, dma_addr_t *addrp, u32 *pfnp)
352 void *page;
353 dma_addr_t addr;
355 page = dma_alloc_coherent(&pt1->pdev->dev, PT1_PAGE_SIZE, &addr,
356 GFP_KERNEL);
357 if (page == NULL)
358 return NULL;
360 BUG_ON(addr & (PT1_PAGE_SIZE - 1));
361 BUG_ON(addr >> PT1_PAGE_SHIFT >> 31 >> 1);
363 *addrp = addr;
364 *pfnp = addr >> PT1_PAGE_SHIFT;
365 return page;
368 static void pt1_cleanup_buffer(struct pt1 *pt1, struct pt1_buffer *buf)
370 pt1_free_page(pt1, buf->page, buf->addr);
373 static int
374 pt1_init_buffer(struct pt1 *pt1, struct pt1_buffer *buf, u32 *pfnp)
376 struct pt1_buffer_page *page;
377 dma_addr_t addr;
379 page = pt1_alloc_page(pt1, &addr, pfnp);
380 if (page == NULL)
381 return -ENOMEM;
383 page->upackets[PT1_NR_UPACKETS - 1] = 0;
385 buf->page = page;
386 buf->addr = addr;
387 return 0;
390 static void pt1_cleanup_table(struct pt1 *pt1, struct pt1_table *table)
392 int i;
394 for (i = 0; i < PT1_NR_BUFS; i++)
395 pt1_cleanup_buffer(pt1, &table->bufs[i]);
397 pt1_free_page(pt1, table->page, table->addr);
400 static int
401 pt1_init_table(struct pt1 *pt1, struct pt1_table *table, u32 *pfnp)
403 struct pt1_table_page *page;
404 dma_addr_t addr;
405 int i, ret;
406 u32 buf_pfn;
408 page = pt1_alloc_page(pt1, &addr, pfnp);
409 if (page == NULL)
410 return -ENOMEM;
412 for (i = 0; i < PT1_NR_BUFS; i++) {
413 ret = pt1_init_buffer(pt1, &table->bufs[i], &buf_pfn);
414 if (ret < 0)
415 goto err;
417 page->buf_pfns[i] = cpu_to_le32(buf_pfn);
420 pt1_increment_table_count(pt1);
421 table->page = page;
422 table->addr = addr;
423 return 0;
425 err:
426 while (i--)
427 pt1_cleanup_buffer(pt1, &table->bufs[i]);
429 pt1_free_page(pt1, page, addr);
430 return ret;
433 static void pt1_cleanup_tables(struct pt1 *pt1)
435 struct pt1_table *tables;
436 int i;
438 tables = pt1->tables;
439 pt1_unregister_tables(pt1);
441 for (i = 0; i < pt1_nr_tables; i++)
442 pt1_cleanup_table(pt1, &tables[i]);
444 vfree(tables);
447 static int pt1_init_tables(struct pt1 *pt1)
449 struct pt1_table *tables;
450 int i, ret;
451 u32 first_pfn, pfn;
453 tables = vmalloc(sizeof(struct pt1_table) * pt1_nr_tables);
454 if (tables == NULL)
455 return -ENOMEM;
457 pt1_init_table_count(pt1);
459 i = 0;
460 if (pt1_nr_tables) {
461 ret = pt1_init_table(pt1, &tables[0], &first_pfn);
462 if (ret)
463 goto err;
464 i++;
467 while (i < pt1_nr_tables) {
468 ret = pt1_init_table(pt1, &tables[i], &pfn);
469 if (ret)
470 goto err;
471 tables[i - 1].page->next_pfn = cpu_to_le32(pfn);
472 i++;
475 tables[pt1_nr_tables - 1].page->next_pfn = cpu_to_le32(first_pfn);
477 pt1_register_tables(pt1, first_pfn);
478 pt1->tables = tables;
479 return 0;
481 err:
482 while (i--)
483 pt1_cleanup_table(pt1, &tables[i]);
485 vfree(tables);
486 return ret;
489 static int pt1_start_polling(struct pt1 *pt1)
491 int ret = 0;
493 mutex_lock(&pt1->lock);
494 if (!pt1->kthread) {
495 pt1->kthread = kthread_run(pt1_thread, pt1, "earth-pt1");
496 if (IS_ERR(pt1->kthread)) {
497 ret = PTR_ERR(pt1->kthread);
498 pt1->kthread = NULL;
501 mutex_unlock(&pt1->lock);
502 return ret;
505 static int pt1_start_feed(struct dvb_demux_feed *feed)
507 struct pt1_adapter *adap;
508 adap = container_of(feed->demux, struct pt1_adapter, demux);
509 if (!adap->users++) {
510 int ret;
512 ret = pt1_start_polling(adap->pt1);
513 if (ret)
514 return ret;
515 pt1_set_stream(adap->pt1, adap->index, 1);
517 return 0;
520 static void pt1_stop_polling(struct pt1 *pt1)
522 int i, count;
524 mutex_lock(&pt1->lock);
525 for (i = 0, count = 0; i < PT1_NR_ADAPS; i++)
526 count += pt1->adaps[i]->users;
528 if (count == 0 && pt1->kthread) {
529 kthread_stop(pt1->kthread);
530 pt1->kthread = NULL;
532 mutex_unlock(&pt1->lock);
535 static int pt1_stop_feed(struct dvb_demux_feed *feed)
537 struct pt1_adapter *adap;
538 adap = container_of(feed->demux, struct pt1_adapter, demux);
539 if (!--adap->users) {
540 pt1_set_stream(adap->pt1, adap->index, 0);
541 pt1_stop_polling(adap->pt1);
543 return 0;
546 static void
547 pt1_update_power(struct pt1 *pt1)
549 int bits;
550 int i;
551 struct pt1_adapter *adap;
552 static const int sleep_bits[] = {
553 1 << 4,
554 1 << 6 | 1 << 7,
555 1 << 5,
556 1 << 6 | 1 << 8,
559 bits = pt1->power | !pt1->reset << 3;
560 mutex_lock(&pt1->lock);
561 for (i = 0; i < PT1_NR_ADAPS; i++) {
562 adap = pt1->adaps[i];
563 switch (adap->voltage) {
564 case SEC_VOLTAGE_13: /* actually 11V */
565 bits |= 1 << 1;
566 break;
567 case SEC_VOLTAGE_18: /* actually 15V */
568 bits |= 1 << 1 | 1 << 2;
569 break;
570 default:
571 break;
574 /* XXX: The bits should be changed depending on adap->sleep. */
575 bits |= sleep_bits[i];
577 pt1_write_reg(pt1, 1, bits);
578 mutex_unlock(&pt1->lock);
581 static int pt1_set_voltage(struct dvb_frontend *fe, fe_sec_voltage_t voltage)
583 struct pt1_adapter *adap;
585 adap = container_of(fe->dvb, struct pt1_adapter, adap);
586 adap->voltage = voltage;
587 pt1_update_power(adap->pt1);
589 if (adap->orig_set_voltage)
590 return adap->orig_set_voltage(fe, voltage);
591 else
592 return 0;
595 static int pt1_sleep(struct dvb_frontend *fe)
597 struct pt1_adapter *adap;
599 adap = container_of(fe->dvb, struct pt1_adapter, adap);
600 adap->sleep = 1;
601 pt1_update_power(adap->pt1);
603 if (adap->orig_sleep)
604 return adap->orig_sleep(fe);
605 else
606 return 0;
609 static int pt1_wakeup(struct dvb_frontend *fe)
611 struct pt1_adapter *adap;
613 adap = container_of(fe->dvb, struct pt1_adapter, adap);
614 adap->sleep = 0;
615 pt1_update_power(adap->pt1);
616 schedule_timeout_uninterruptible((HZ + 999) / 1000);
618 if (adap->orig_init)
619 return adap->orig_init(fe);
620 else
621 return 0;
624 static void pt1_free_adapter(struct pt1_adapter *adap)
626 adap->demux.dmx.close(&adap->demux.dmx);
627 dvb_dmxdev_release(&adap->dmxdev);
628 dvb_dmx_release(&adap->demux);
629 dvb_unregister_adapter(&adap->adap);
630 free_page((unsigned long)adap->buf);
631 kfree(adap);
634 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
636 static struct pt1_adapter *
637 pt1_alloc_adapter(struct pt1 *pt1)
639 struct pt1_adapter *adap;
640 void *buf;
641 struct dvb_adapter *dvb_adap;
642 struct dvb_demux *demux;
643 struct dmxdev *dmxdev;
644 int ret;
646 adap = kzalloc(sizeof(struct pt1_adapter), GFP_KERNEL);
647 if (!adap) {
648 ret = -ENOMEM;
649 goto err;
652 adap->pt1 = pt1;
654 adap->voltage = SEC_VOLTAGE_OFF;
655 adap->sleep = 1;
657 buf = (u8 *)__get_free_page(GFP_KERNEL);
658 if (!buf) {
659 ret = -ENOMEM;
660 goto err_kfree;
663 adap->buf = buf;
664 adap->upacket_count = 0;
665 adap->packet_count = 0;
666 adap->st_count = -1;
668 dvb_adap = &adap->adap;
669 dvb_adap->priv = adap;
670 ret = dvb_register_adapter(dvb_adap, DRIVER_NAME, THIS_MODULE,
671 &pt1->pdev->dev, adapter_nr);
672 if (ret < 0)
673 goto err_free_page;
675 demux = &adap->demux;
676 demux->dmx.capabilities = DMX_TS_FILTERING | DMX_SECTION_FILTERING;
677 demux->priv = adap;
678 demux->feednum = 256;
679 demux->filternum = 256;
680 demux->start_feed = pt1_start_feed;
681 demux->stop_feed = pt1_stop_feed;
682 demux->write_to_decoder = NULL;
683 ret = dvb_dmx_init(demux);
684 if (ret < 0)
685 goto err_unregister_adapter;
687 dmxdev = &adap->dmxdev;
688 dmxdev->filternum = 256;
689 dmxdev->demux = &demux->dmx;
690 dmxdev->capabilities = 0;
691 ret = dvb_dmxdev_init(dmxdev, dvb_adap);
692 if (ret < 0)
693 goto err_dmx_release;
695 return adap;
697 err_dmx_release:
698 dvb_dmx_release(demux);
699 err_unregister_adapter:
700 dvb_unregister_adapter(dvb_adap);
701 err_free_page:
702 free_page((unsigned long)buf);
703 err_kfree:
704 kfree(adap);
705 err:
706 return ERR_PTR(ret);
709 static void pt1_cleanup_adapters(struct pt1 *pt1)
711 int i;
712 for (i = 0; i < PT1_NR_ADAPS; i++)
713 pt1_free_adapter(pt1->adaps[i]);
716 static int pt1_init_adapters(struct pt1 *pt1)
718 int i;
719 struct pt1_adapter *adap;
720 int ret;
722 for (i = 0; i < PT1_NR_ADAPS; i++) {
723 adap = pt1_alloc_adapter(pt1);
724 if (IS_ERR(adap)) {
725 ret = PTR_ERR(adap);
726 goto err;
729 adap->index = i;
730 pt1->adaps[i] = adap;
732 return 0;
734 err:
735 while (i--)
736 pt1_free_adapter(pt1->adaps[i]);
738 return ret;
741 static void pt1_cleanup_frontend(struct pt1_adapter *adap)
743 dvb_unregister_frontend(adap->fe);
746 static int pt1_init_frontend(struct pt1_adapter *adap, struct dvb_frontend *fe)
748 int ret;
750 adap->orig_set_voltage = fe->ops.set_voltage;
751 adap->orig_sleep = fe->ops.sleep;
752 adap->orig_init = fe->ops.init;
753 fe->ops.set_voltage = pt1_set_voltage;
754 fe->ops.sleep = pt1_sleep;
755 fe->ops.init = pt1_wakeup;
757 ret = dvb_register_frontend(&adap->adap, fe);
758 if (ret < 0)
759 return ret;
761 adap->fe = fe;
762 return 0;
765 static void pt1_cleanup_frontends(struct pt1 *pt1)
767 int i;
768 for (i = 0; i < PT1_NR_ADAPS; i++)
769 pt1_cleanup_frontend(pt1->adaps[i]);
772 struct pt1_config {
773 struct va1j5jf8007s_config va1j5jf8007s_config;
774 struct va1j5jf8007t_config va1j5jf8007t_config;
777 static const struct pt1_config pt1_configs[2] = {
780 .demod_address = 0x1b,
781 .frequency = VA1J5JF8007S_20MHZ,
784 .demod_address = 0x1a,
785 .frequency = VA1J5JF8007T_20MHZ,
787 }, {
789 .demod_address = 0x19,
790 .frequency = VA1J5JF8007S_20MHZ,
793 .demod_address = 0x18,
794 .frequency = VA1J5JF8007T_20MHZ,
799 static const struct pt1_config pt2_configs[2] = {
802 .demod_address = 0x1b,
803 .frequency = VA1J5JF8007S_25MHZ,
806 .demod_address = 0x1a,
807 .frequency = VA1J5JF8007T_25MHZ,
809 }, {
811 .demod_address = 0x19,
812 .frequency = VA1J5JF8007S_25MHZ,
815 .demod_address = 0x18,
816 .frequency = VA1J5JF8007T_25MHZ,
821 static int pt1_init_frontends(struct pt1 *pt1)
823 int i, j;
824 struct i2c_adapter *i2c_adap;
825 const struct pt1_config *configs, *config;
826 struct dvb_frontend *fe[4];
827 int ret;
829 i = 0;
830 j = 0;
832 i2c_adap = &pt1->i2c_adap;
833 configs = pt1->pdev->device == 0x211a ? pt1_configs : pt2_configs;
834 do {
835 config = &configs[i / 2];
837 fe[i] = va1j5jf8007s_attach(&config->va1j5jf8007s_config,
838 i2c_adap);
839 if (!fe[i]) {
840 ret = -ENODEV; /* This does not sound nice... */
841 goto err;
843 i++;
845 fe[i] = va1j5jf8007t_attach(&config->va1j5jf8007t_config,
846 i2c_adap);
847 if (!fe[i]) {
848 ret = -ENODEV;
849 goto err;
851 i++;
853 ret = va1j5jf8007s_prepare(fe[i - 2]);
854 if (ret < 0)
855 goto err;
857 ret = va1j5jf8007t_prepare(fe[i - 1]);
858 if (ret < 0)
859 goto err;
861 } while (i < 4);
863 do {
864 ret = pt1_init_frontend(pt1->adaps[j], fe[j]);
865 if (ret < 0)
866 goto err;
867 } while (++j < 4);
869 return 0;
871 err:
872 while (i-- > j)
873 fe[i]->ops.release(fe[i]);
875 while (j--)
876 dvb_unregister_frontend(fe[j]);
878 return ret;
881 static void pt1_i2c_emit(struct pt1 *pt1, int addr, int busy, int read_enable,
882 int clock, int data, int next_addr)
884 pt1_write_reg(pt1, 4, addr << 18 | busy << 13 | read_enable << 12 |
885 !clock << 11 | !data << 10 | next_addr);
888 static void pt1_i2c_write_bit(struct pt1 *pt1, int addr, int *addrp, int data)
890 pt1_i2c_emit(pt1, addr, 1, 0, 0, data, addr + 1);
891 pt1_i2c_emit(pt1, addr + 1, 1, 0, 1, data, addr + 2);
892 pt1_i2c_emit(pt1, addr + 2, 1, 0, 0, data, addr + 3);
893 *addrp = addr + 3;
896 static void pt1_i2c_read_bit(struct pt1 *pt1, int addr, int *addrp)
898 pt1_i2c_emit(pt1, addr, 1, 0, 0, 1, addr + 1);
899 pt1_i2c_emit(pt1, addr + 1, 1, 0, 1, 1, addr + 2);
900 pt1_i2c_emit(pt1, addr + 2, 1, 1, 1, 1, addr + 3);
901 pt1_i2c_emit(pt1, addr + 3, 1, 0, 0, 1, addr + 4);
902 *addrp = addr + 4;
905 static void pt1_i2c_write_byte(struct pt1 *pt1, int addr, int *addrp, int data)
907 int i;
908 for (i = 0; i < 8; i++)
909 pt1_i2c_write_bit(pt1, addr, &addr, data >> (7 - i) & 1);
910 pt1_i2c_write_bit(pt1, addr, &addr, 1);
911 *addrp = addr;
914 static void pt1_i2c_read_byte(struct pt1 *pt1, int addr, int *addrp, int last)
916 int i;
917 for (i = 0; i < 8; i++)
918 pt1_i2c_read_bit(pt1, addr, &addr);
919 pt1_i2c_write_bit(pt1, addr, &addr, last);
920 *addrp = addr;
923 static void pt1_i2c_prepare(struct pt1 *pt1, int addr, int *addrp)
925 pt1_i2c_emit(pt1, addr, 1, 0, 1, 1, addr + 1);
926 pt1_i2c_emit(pt1, addr + 1, 1, 0, 1, 0, addr + 2);
927 pt1_i2c_emit(pt1, addr + 2, 1, 0, 0, 0, addr + 3);
928 *addrp = addr + 3;
931 static void
932 pt1_i2c_write_msg(struct pt1 *pt1, int addr, int *addrp, struct i2c_msg *msg)
934 int i;
935 pt1_i2c_prepare(pt1, addr, &addr);
936 pt1_i2c_write_byte(pt1, addr, &addr, msg->addr << 1);
937 for (i = 0; i < msg->len; i++)
938 pt1_i2c_write_byte(pt1, addr, &addr, msg->buf[i]);
939 *addrp = addr;
942 static void
943 pt1_i2c_read_msg(struct pt1 *pt1, int addr, int *addrp, struct i2c_msg *msg)
945 int i;
946 pt1_i2c_prepare(pt1, addr, &addr);
947 pt1_i2c_write_byte(pt1, addr, &addr, msg->addr << 1 | 1);
948 for (i = 0; i < msg->len; i++)
949 pt1_i2c_read_byte(pt1, addr, &addr, i == msg->len - 1);
950 *addrp = addr;
953 static int pt1_i2c_end(struct pt1 *pt1, int addr)
955 pt1_i2c_emit(pt1, addr, 1, 0, 0, 0, addr + 1);
956 pt1_i2c_emit(pt1, addr + 1, 1, 0, 1, 0, addr + 2);
957 pt1_i2c_emit(pt1, addr + 2, 1, 0, 1, 1, 0);
959 pt1_write_reg(pt1, 0, 0x00000004);
960 do {
961 if (signal_pending(current))
962 return -EINTR;
963 schedule_timeout_interruptible((HZ + 999) / 1000);
964 } while (pt1_read_reg(pt1, 0) & 0x00000080);
965 return 0;
968 static void pt1_i2c_begin(struct pt1 *pt1, int *addrp)
970 int addr;
971 addr = 0;
973 pt1_i2c_emit(pt1, addr, 0, 0, 1, 1, addr /* itself */);
974 addr = addr + 1;
976 if (!pt1->i2c_running) {
977 pt1_i2c_emit(pt1, addr, 1, 0, 1, 1, addr + 1);
978 pt1_i2c_emit(pt1, addr + 1, 1, 0, 1, 0, addr + 2);
979 addr = addr + 2;
980 pt1->i2c_running = 1;
982 *addrp = addr;
985 static int pt1_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
987 struct pt1 *pt1;
988 int i;
989 struct i2c_msg *msg, *next_msg;
990 int addr, ret;
991 u16 len;
992 u32 word;
994 pt1 = i2c_get_adapdata(adap);
996 for (i = 0; i < num; i++) {
997 msg = &msgs[i];
998 if (msg->flags & I2C_M_RD)
999 return -ENOTSUPP;
1001 if (i + 1 < num)
1002 next_msg = &msgs[i + 1];
1003 else
1004 next_msg = NULL;
1006 if (next_msg && next_msg->flags & I2C_M_RD) {
1007 i++;
1009 len = next_msg->len;
1010 if (len > 4)
1011 return -ENOTSUPP;
1013 pt1_i2c_begin(pt1, &addr);
1014 pt1_i2c_write_msg(pt1, addr, &addr, msg);
1015 pt1_i2c_read_msg(pt1, addr, &addr, next_msg);
1016 ret = pt1_i2c_end(pt1, addr);
1017 if (ret < 0)
1018 return ret;
1020 word = pt1_read_reg(pt1, 2);
1021 while (len--) {
1022 next_msg->buf[len] = word;
1023 word >>= 8;
1025 } else {
1026 pt1_i2c_begin(pt1, &addr);
1027 pt1_i2c_write_msg(pt1, addr, &addr, msg);
1028 ret = pt1_i2c_end(pt1, addr);
1029 if (ret < 0)
1030 return ret;
1034 return num;
1037 static u32 pt1_i2c_func(struct i2c_adapter *adap)
1039 return I2C_FUNC_I2C;
1042 static const struct i2c_algorithm pt1_i2c_algo = {
1043 .master_xfer = pt1_i2c_xfer,
1044 .functionality = pt1_i2c_func,
1047 static void pt1_i2c_wait(struct pt1 *pt1)
1049 int i;
1050 for (i = 0; i < 128; i++)
1051 pt1_i2c_emit(pt1, 0, 0, 0, 1, 1, 0);
1054 static void pt1_i2c_init(struct pt1 *pt1)
1056 int i;
1057 for (i = 0; i < 1024; i++)
1058 pt1_i2c_emit(pt1, i, 0, 0, 1, 1, 0);
1061 static void pt1_remove(struct pci_dev *pdev)
1063 struct pt1 *pt1;
1064 void __iomem *regs;
1066 pt1 = pci_get_drvdata(pdev);
1067 regs = pt1->regs;
1069 if (pt1->kthread)
1070 kthread_stop(pt1->kthread);
1071 pt1_cleanup_tables(pt1);
1072 pt1_cleanup_frontends(pt1);
1073 pt1_disable_ram(pt1);
1074 pt1->power = 0;
1075 pt1->reset = 1;
1076 pt1_update_power(pt1);
1077 pt1_cleanup_adapters(pt1);
1078 i2c_del_adapter(&pt1->i2c_adap);
1079 pci_set_drvdata(pdev, NULL);
1080 kfree(pt1);
1081 pci_iounmap(pdev, regs);
1082 pci_release_regions(pdev);
1083 pci_disable_device(pdev);
1086 static int pt1_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
1088 int ret;
1089 void __iomem *regs;
1090 struct pt1 *pt1;
1091 struct i2c_adapter *i2c_adap;
1093 ret = pci_enable_device(pdev);
1094 if (ret < 0)
1095 goto err;
1097 ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
1098 if (ret < 0)
1099 goto err_pci_disable_device;
1101 pci_set_master(pdev);
1103 ret = pci_request_regions(pdev, DRIVER_NAME);
1104 if (ret < 0)
1105 goto err_pci_disable_device;
1107 regs = pci_iomap(pdev, 0, 0);
1108 if (!regs) {
1109 ret = -EIO;
1110 goto err_pci_release_regions;
1113 pt1 = kzalloc(sizeof(struct pt1), GFP_KERNEL);
1114 if (!pt1) {
1115 ret = -ENOMEM;
1116 goto err_pci_iounmap;
1119 mutex_init(&pt1->lock);
1120 pt1->pdev = pdev;
1121 pt1->regs = regs;
1122 pci_set_drvdata(pdev, pt1);
1124 ret = pt1_init_adapters(pt1);
1125 if (ret < 0)
1126 goto err_kfree;
1128 mutex_init(&pt1->lock);
1130 pt1->power = 0;
1131 pt1->reset = 1;
1132 pt1_update_power(pt1);
1134 i2c_adap = &pt1->i2c_adap;
1135 i2c_adap->algo = &pt1_i2c_algo;
1136 i2c_adap->algo_data = NULL;
1137 i2c_adap->dev.parent = &pdev->dev;
1138 strcpy(i2c_adap->name, DRIVER_NAME);
1139 i2c_set_adapdata(i2c_adap, pt1);
1140 ret = i2c_add_adapter(i2c_adap);
1141 if (ret < 0)
1142 goto err_pt1_cleanup_adapters;
1144 pt1_i2c_init(pt1);
1145 pt1_i2c_wait(pt1);
1147 ret = pt1_sync(pt1);
1148 if (ret < 0)
1149 goto err_i2c_del_adapter;
1151 pt1_identify(pt1);
1153 ret = pt1_unlock(pt1);
1154 if (ret < 0)
1155 goto err_i2c_del_adapter;
1157 ret = pt1_reset_pci(pt1);
1158 if (ret < 0)
1159 goto err_i2c_del_adapter;
1161 ret = pt1_reset_ram(pt1);
1162 if (ret < 0)
1163 goto err_i2c_del_adapter;
1165 ret = pt1_enable_ram(pt1);
1166 if (ret < 0)
1167 goto err_i2c_del_adapter;
1169 pt1_init_streams(pt1);
1171 pt1->power = 1;
1172 pt1_update_power(pt1);
1173 schedule_timeout_uninterruptible((HZ + 49) / 50);
1175 pt1->reset = 0;
1176 pt1_update_power(pt1);
1177 schedule_timeout_uninterruptible((HZ + 999) / 1000);
1179 ret = pt1_init_frontends(pt1);
1180 if (ret < 0)
1181 goto err_pt1_disable_ram;
1183 ret = pt1_init_tables(pt1);
1184 if (ret < 0)
1185 goto err_pt1_cleanup_frontends;
1187 return 0;
1189 err_pt1_cleanup_frontends:
1190 pt1_cleanup_frontends(pt1);
1191 err_pt1_disable_ram:
1192 pt1_disable_ram(pt1);
1193 pt1->power = 0;
1194 pt1->reset = 1;
1195 pt1_update_power(pt1);
1196 err_i2c_del_adapter:
1197 i2c_del_adapter(i2c_adap);
1198 err_pt1_cleanup_adapters:
1199 pt1_cleanup_adapters(pt1);
1200 err_kfree:
1201 pci_set_drvdata(pdev, NULL);
1202 kfree(pt1);
1203 err_pci_iounmap:
1204 pci_iounmap(pdev, regs);
1205 err_pci_release_regions:
1206 pci_release_regions(pdev);
1207 err_pci_disable_device:
1208 pci_disable_device(pdev);
1209 err:
1210 return ret;
1214 static struct pci_device_id pt1_id_table[] = {
1215 { PCI_DEVICE(0x10ee, 0x211a) },
1216 { PCI_DEVICE(0x10ee, 0x222a) },
1217 { },
1219 MODULE_DEVICE_TABLE(pci, pt1_id_table);
1221 static struct pci_driver pt1_driver = {
1222 .name = DRIVER_NAME,
1223 .probe = pt1_probe,
1224 .remove = pt1_remove,
1225 .id_table = pt1_id_table,
1229 static int __init pt1_init(void)
1231 return pci_register_driver(&pt1_driver);
1235 static void __exit pt1_cleanup(void)
1237 pci_unregister_driver(&pt1_driver);
1240 module_init(pt1_init);
1241 module_exit(pt1_cleanup);
1243 MODULE_AUTHOR("Takahito HIRANO <hiranotaka@zng.info>");
1244 MODULE_DESCRIPTION("Earthsoft PT1/PT2 Driver");
1245 MODULE_LICENSE("GPL");