1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * SMI PCIe driver for DVBSky cards.
5 * Copyright (C) 2014 Max nibble <nibble.max@gmail.com>
10 #define SMI_SAMPLE_PERIOD 83
11 #define SMI_SAMPLE_IDLEMIN (10000 / SMI_SAMPLE_PERIOD)
13 static void smi_ir_enableInterrupt(struct smi_rc
*ir
)
15 struct smi_dev
*dev
= ir
->dev
;
17 smi_write(MSI_INT_ENA_SET
, IR_X_INT
);
20 static void smi_ir_disableInterrupt(struct smi_rc
*ir
)
22 struct smi_dev
*dev
= ir
->dev
;
24 smi_write(MSI_INT_ENA_CLR
, IR_X_INT
);
27 static void smi_ir_clearInterrupt(struct smi_rc
*ir
)
29 struct smi_dev
*dev
= ir
->dev
;
31 smi_write(MSI_INT_STATUS_CLR
, IR_X_INT
);
34 static void smi_ir_stop(struct smi_rc
*ir
)
36 struct smi_dev
*dev
= ir
->dev
;
38 smi_ir_disableInterrupt(ir
);
39 smi_clear(IR_Init_Reg
, rbIRen
);
42 static void smi_raw_process(struct rc_dev
*rc_dev
, const u8
*buffer
,
45 struct ir_raw_event rawir
= {};
48 for (cnt
= 0; cnt
< length
; cnt
++) {
49 if (buffer
[cnt
] & 0x7f) {
50 rawir
.pulse
= (buffer
[cnt
] & 0x80) == 0;
51 rawir
.duration
= ((buffer
[cnt
] & 0x7f) +
52 (rawir
.pulse
? 0 : -1)) *
53 rc_dev
->rx_resolution
;
54 ir_raw_event_store_with_filter(rc_dev
, &rawir
);
59 static void smi_ir_decode(struct smi_rc
*ir
)
61 struct smi_dev
*dev
= ir
->dev
;
62 struct rc_dev
*rc_dev
= ir
->rc_dev
;
64 u8 index
, ir_count
, read_loop
;
66 control
= smi_read(IR_Init_Reg
);
68 dev_dbg(&rc_dev
->dev
, "ircontrol: 0x%08x\n", control
);
70 if (control
& rbIRVld
) {
71 ir_count
= (u8
)smi_read(IR_Data_Cnt
);
73 dev_dbg(&rc_dev
->dev
, "ircount %d\n", ir_count
);
75 read_loop
= ir_count
/ 4;
78 for (index
= 0; index
< read_loop
; index
++) {
79 data
= smi_read(IR_DATA_BUFFER_BASE
+ (index
* 4));
80 dev_dbg(&rc_dev
->dev
, "IRData 0x%08x\n", data
);
82 ir
->irData
[index
* 4 + 0] = (u8
)(data
);
83 ir
->irData
[index
* 4 + 1] = (u8
)(data
>> 8);
84 ir
->irData
[index
* 4 + 2] = (u8
)(data
>> 16);
85 ir
->irData
[index
* 4 + 3] = (u8
)(data
>> 24);
87 smi_raw_process(rc_dev
, ir
->irData
, ir_count
);
90 if (control
& rbIRhighidle
) {
91 struct ir_raw_event rawir
= {};
93 dev_dbg(&rc_dev
->dev
, "high idle\n");
96 rawir
.duration
= SMI_SAMPLE_PERIOD
* SMI_SAMPLE_IDLEMIN
;
97 ir_raw_event_store_with_filter(rc_dev
, &rawir
);
100 smi_set(IR_Init_Reg
, rbIRVld
);
101 ir_raw_event_handle(rc_dev
);
104 /* ir functions call by main driver.*/
105 int smi_ir_irq(struct smi_rc
*ir
, u32 int_status
)
109 if (int_status
& IR_X_INT
) {
110 smi_ir_disableInterrupt(ir
);
111 smi_ir_clearInterrupt(ir
);
113 smi_ir_enableInterrupt(ir
);
119 void smi_ir_start(struct smi_rc
*ir
)
121 struct smi_dev
*dev
= ir
->dev
;
123 smi_write(IR_Idle_Cnt_Low
,
124 (((SMI_SAMPLE_PERIOD
- 1) & 0xFFFF) << 16) |
125 (SMI_SAMPLE_IDLEMIN
& 0xFFFF));
127 smi_set(IR_Init_Reg
, rbIRen
| rbIRhighidle
);
129 smi_ir_enableInterrupt(ir
);
132 int smi_ir_init(struct smi_dev
*dev
)
135 struct rc_dev
*rc_dev
;
136 struct smi_rc
*ir
= &dev
->ir
;
138 rc_dev
= rc_allocate_device(RC_DRIVER_IR_RAW
);
142 /* init input device */
143 snprintf(ir
->device_name
, sizeof(ir
->device_name
), "IR (%s)",
145 snprintf(ir
->input_phys
, sizeof(ir
->input_phys
), "pci-%s/ir0",
146 pci_name(dev
->pci_dev
));
148 rc_dev
->allowed_protocols
= RC_PROTO_BIT_ALL_IR_DECODER
;
149 rc_dev
->driver_name
= "SMI_PCIe";
150 rc_dev
->input_phys
= ir
->input_phys
;
151 rc_dev
->device_name
= ir
->device_name
;
152 rc_dev
->input_id
.bustype
= BUS_PCI
;
153 rc_dev
->input_id
.version
= 1;
154 rc_dev
->input_id
.vendor
= dev
->pci_dev
->subsystem_vendor
;
155 rc_dev
->input_id
.product
= dev
->pci_dev
->subsystem_device
;
156 rc_dev
->dev
.parent
= &dev
->pci_dev
->dev
;
158 rc_dev
->map_name
= dev
->info
->rc_map
;
159 rc_dev
->timeout
= SMI_SAMPLE_PERIOD
* SMI_SAMPLE_IDLEMIN
;
160 rc_dev
->rx_resolution
= SMI_SAMPLE_PERIOD
;
165 smi_ir_disableInterrupt(ir
);
167 ret
= rc_register_device(rc_dev
);
173 rc_free_device(rc_dev
);
177 void smi_ir_exit(struct smi_dev
*dev
)
179 struct smi_rc
*ir
= &dev
->ir
;
180 struct rc_dev
*rc_dev
= ir
->rc_dev
;
182 rc_unregister_device(rc_dev
);