24 #include <linux/kernel.h>
25 #include <linux/module.h>
26 #include <linux/slab.h>
28 #include <linux/pci.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
69 #define PT1_NR_ADAPS 4
112 #define pt1_printk(level, pt1, format, arg...) \
113 dev_printk(level, &(pt1)->pdev->dev, format, ##arg)
125 static int pt1_nr_tables = 8;
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)
152 for (i = 0; i < 57; i++) {
153 if (pt1_read_reg(pt1, 0) & 0x20000000)
155 pt1_write_reg(pt1, 0, 0x00000008);
161 static u64 pt1_identify(
struct pt1 *pt1)
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);
173 static int pt1_unlock(
struct pt1 *pt1)
176 pt1_write_reg(pt1, 0, 0x00000008);
177 for (i = 0; i < 3; i++) {
178 if (pt1_read_reg(pt1, 0) & 0x80000000)
186 static int pt1_reset_pci(
struct pt1 *pt1)
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)
200 static int pt1_reset_ram(
struct pt1 *pt1)
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)
214 static int pt1_do_enable_ram(
struct pt1 *pt1)
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)
231 static int pt1_enable_ram(
struct pt1 *pt1)
236 phase = pt1->
pdev->device == 0x211a ? 128 : 166;
237 for (i = 0; i <
phase; i++) {
238 ret = pt1_do_enable_ram(pt1);
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)
259 pt1_set_stream(pt1, i, 0);
277 index = (upacket >> 29) - 1;
278 if (index < 0 || index >= PT1_NR_ADAPS)
282 if (upacket >> 25 & 1)
287 if (upacket >> 24 & 1)
289 "buffer overflowing. table[%d] buf[%d]\n",
291 sc = upacket >> 26 & 0x7;
294 " in streamID(adapter)[%d]\n", index);
299 buf[
offset] = upacket >> 16;
300 buf[offset + 1] = upacket >> 8;
302 buf[offset + 2] = upacket;
313 page->
upackets[PT1_NR_UPACKETS - 1] = 0;
317 static int pt1_thread(
void *
data)
329 if (!pt1_filter(pt1, page)) {
335 pt1_increment_table_count(pt1);
345 static void pt1_free_page(
struct pt1 *pt1,
void *page,
dma_addr_t addr)
350 static void *pt1_alloc_page(
struct pt1 *pt1,
dma_addr_t *addrp,
u32 *pfnp)
368 static void pt1_cleanup_buffer(
struct pt1 *pt1,
struct pt1_buffer *buf)
370 pt1_free_page(pt1, buf->
page, buf->
addr);
374 pt1_init_buffer(
struct pt1 *pt1,
struct pt1_buffer *buf,
u32 *pfnp)
379 page = pt1_alloc_page(pt1, &addr, pfnp);
383 page->
upackets[PT1_NR_UPACKETS - 1] = 0;
390 static void pt1_cleanup_table(
struct pt1 *pt1,
struct pt1_table *
table)
395 pt1_cleanup_buffer(pt1, &table->
bufs[i]);
397 pt1_free_page(pt1, table->
page, table->
addr);
401 pt1_init_table(
struct pt1 *pt1,
struct pt1_table *table,
u32 *pfnp)
408 page = pt1_alloc_page(pt1, &addr, pfnp);
413 ret = pt1_init_buffer(pt1, &table->
bufs[i], &buf_pfn);
420 pt1_increment_table_count(pt1);
427 pt1_cleanup_buffer(pt1, &table->
bufs[i]);
429 pt1_free_page(pt1, page, addr);
433 static void pt1_cleanup_tables(
struct pt1 *pt1)
439 pt1_unregister_tables(pt1);
441 for (i = 0; i < pt1_nr_tables; i++)
442 pt1_cleanup_table(pt1, &tables[i]);
447 static int pt1_init_tables(
struct pt1 *pt1)
457 pt1_init_table_count(pt1);
461 ret = pt1_init_table(pt1, &tables[0], &first_pfn);
467 while (i < pt1_nr_tables) {
468 ret = pt1_init_table(pt1, &tables[i], &pfn);
477 pt1_register_tables(pt1, first_pfn);
483 pt1_cleanup_table(pt1, &tables[i]);
489 static int pt1_start_polling(
struct pt1 *pt1)
509 if (!adap->
users++) {
512 ret = pt1_start_polling(adap->
pt1);
515 pt1_set_stream(adap->
pt1, adap->
index, 1);
520 static void pt1_stop_polling(
struct pt1 *pt1)
526 count += pt1->
adaps[i]->users;
539 if (!--adap->
users) {
540 pt1_set_stream(adap->
pt1, adap->
index, 0);
541 pt1_stop_polling(adap->
pt1);
547 pt1_update_power(
struct pt1 *pt1)
552 static const int sleep_bits[] = {
568 bits |= 1 << 1 | 1 << 2;
575 bits |= sleep_bits[
i];
577 pt1_write_reg(pt1, 1, bits);
587 pt1_update_power(adap->
pt1);
601 pt1_update_power(adap->
pt1);
615 pt1_update_power(adap->
pt1);
624 static void pt1_free_adapter(
struct pt1_adapter *adap)
637 pt1_alloc_adapter(
struct pt1 *pt1)
668 dvb_adap = &adap->
adap;
669 dvb_adap->
priv = adap;
671 &pt1->
pdev->dev, adapter_nr);
675 demux = &adap->
demux;
685 goto err_unregister_adapter;
693 goto err_dmx_release;
699 err_unregister_adapter:
709 static void pt1_cleanup_adapters(
struct pt1 *pt1)
713 pt1_free_adapter(pt1->
adaps[i]);
716 static int pt1_init_adapters(
struct pt1 *pt1)
723 adap = pt1_alloc_adapter(pt1);
736 pt1_free_adapter(pt1->
adaps[i]);
741 static void pt1_cleanup_frontend(
struct pt1_adapter *adap)
753 fe->
ops.set_voltage = pt1_set_voltage;
754 fe->
ops.sleep = pt1_sleep;
755 fe->
ops.init = pt1_wakeup;
765 static void pt1_cleanup_frontends(
struct pt1 *pt1)
769 pt1_cleanup_frontend(pt1->
adaps[i]);
777 static const struct pt1_config pt1_configs[2] = {
780 .demod_address = 0x1b,
784 .demod_address = 0x1a,
789 .demod_address = 0x19,
793 .demod_address = 0x18,
799 static const struct pt1_config pt2_configs[2] = {
802 .demod_address = 0x1b,
806 .demod_address = 0x1a,
811 .demod_address = 0x19,
815 .demod_address = 0x18,
821 static int pt1_init_frontends(
struct pt1 *pt1)
833 configs = pt1->
pdev->device == 0x211a ? pt1_configs : pt2_configs;
835 config = &configs[i / 2];
864 ret = pt1_init_frontend(pt1->
adaps[j], fe[j]);
873 fe[
i]->
ops.release(fe[i]);
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);
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);
905 static void pt1_i2c_write_byte(
struct pt1 *pt1,
int addr,
int *addrp,
int data)
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);
914 static void pt1_i2c_read_byte(
struct pt1 *pt1,
int addr,
int *addrp,
int last)
917 for (i = 0; i < 8; i++)
918 pt1_i2c_read_bit(pt1, addr, &addr);
919 pt1_i2c_write_bit(pt1, addr, &addr, last);
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);
932 pt1_i2c_write_msg(
struct pt1 *pt1,
int addr,
int *addrp,
struct i2c_msg *
msg)
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]);
943 pt1_i2c_read_msg(
struct pt1 *pt1,
int addr,
int *addrp,
struct i2c_msg *msg)
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);
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);
964 }
while (pt1_read_reg(pt1, 0) & 0x00000080);
968 static void pt1_i2c_begin(
struct pt1 *pt1,
int *addrp)
973 pt1_i2c_emit(pt1, addr, 0, 0, 1, 1, addr );
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);
994 pt1 = i2c_get_adapdata(adap);
996 for (i = 0; i < num; i++) {
1002 next_msg = &msgs[i + 1];
1009 len = next_msg->
len;
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);
1020 word = pt1_read_reg(pt1, 2);
1026 pt1_i2c_begin(pt1, &addr);
1027 pt1_i2c_write_msg(pt1, addr, &addr, msg);
1028 ret = pt1_i2c_end(pt1, addr);
1043 .master_xfer = pt1_i2c_xfer,
1044 .functionality = pt1_i2c_func,
1047 static void pt1_i2c_wait(
struct pt1 *pt1)
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)
1057 for (i = 0; i < 1024; i++)
1058 pt1_i2c_emit(pt1, i, 0, 0, 1, 1, 0);
1066 pt1 = pci_get_drvdata(pdev);
1071 pt1_cleanup_tables(pt1);
1072 pt1_cleanup_frontends(pt1);
1073 pt1_disable_ram(pt1);
1076 pt1_update_power(pt1);
1077 pt1_cleanup_adapters(pt1);
1079 pci_set_drvdata(pdev,
NULL);
1100 goto err_pci_disable_device;
1106 goto err_pci_disable_device;
1108 regs = pci_iomap(pdev, 0, 0);
1111 goto err_pci_release_regions;
1114 pt1 = kzalloc(
sizeof(
struct pt1),
GFP_KERNEL);
1117 goto err_pci_iounmap;
1123 pci_set_drvdata(pdev, pt1);
1125 ret = pt1_init_adapters(pt1);
1133 pt1_update_power(pt1);
1136 i2c_adap->
algo = &pt1_i2c_algo;
1138 i2c_adap->
dev.parent = &pdev->
dev;
1140 i2c_set_adapdata(i2c_adap, pt1);
1143 goto err_pt1_cleanup_adapters;
1148 ret = pt1_sync(pt1);
1150 goto err_i2c_del_adapter;
1154 ret = pt1_unlock(pt1);
1156 goto err_i2c_del_adapter;
1158 ret = pt1_reset_pci(pt1);
1160 goto err_i2c_del_adapter;
1162 ret = pt1_reset_ram(pt1);
1164 goto err_i2c_del_adapter;
1166 ret = pt1_enable_ram(pt1);
1168 goto err_i2c_del_adapter;
1170 pt1_init_streams(pt1);
1173 pt1_update_power(pt1);
1177 pt1_update_power(pt1);
1180 ret = pt1_init_frontends(pt1);
1182 goto err_pt1_disable_ram;
1184 ret = pt1_init_tables(pt1);
1186 goto err_pt1_cleanup_frontends;
1190 err_pt1_cleanup_frontends:
1191 pt1_cleanup_frontends(pt1);
1192 err_pt1_disable_ram:
1193 pt1_disable_ram(pt1);
1196 pt1_update_power(pt1);
1197 err_i2c_del_adapter:
1199 err_pt1_cleanup_adapters:
1200 pt1_cleanup_adapters(pt1);
1202 pci_set_drvdata(pdev,
NULL);
1206 err_pci_release_regions:
1208 err_pci_disable_device:
1226 .id_table = pt1_id_table,
1230 static int __init pt1_init(
void)
1232 return pci_register_driver(&pt1_driver);
1236 static void __exit pt1_cleanup(
void)