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;
63 u32 dwIRControl, dwIRData;
64 u8 index, ucIRCount, readLoop;
66 dwIRControl = smi_read(IR_Init_Reg);
68 if (dwIRControl & rbIRVld) {
69 ucIRCount = (u8) smi_read(IR_Data_Cnt);
71 readLoop = ucIRCount/4;
74 for (index = 0; index < readLoop; index++) {
75 dwIRData = smi_read(IR_DATA_BUFFER_BASE + (index * 4));
77 ir->irData[index*4 + 0] = (u8)(dwIRData);
78 ir->irData[index*4 + 1] = (u8)(dwIRData >> 8);
79 ir->irData[index*4 + 2] = (u8)(dwIRData >> 16);
80 ir->irData[index*4 + 3] = (u8)(dwIRData >> 24);
82 smi_raw_process(rc_dev, ir->irData, ucIRCount);
83 smi_set(IR_Init_Reg, rbIRVld);
86 if (dwIRControl & rbIRhighidle) {
87 struct ir_raw_event rawir = {};
90 rawir.duration = US_TO_NS(SMI_SAMPLE_PERIOD *
92 ir_raw_event_store_with_filter(rc_dev, &rawir);
93 smi_set(IR_Init_Reg, rbIRhighidle);
96 ir_raw_event_handle(rc_dev);
99 /* ir functions call by main driver.*/
100 int smi_ir_irq(struct smi_rc *ir, u32 int_status)
104 if (int_status & IR_X_INT) {
105 smi_ir_disableInterrupt(ir);
106 smi_ir_clearInterrupt(ir);
108 smi_ir_enableInterrupt(ir);
114 void smi_ir_start(struct smi_rc *ir)
116 struct smi_dev *dev = ir->dev;
118 smi_write(IR_Idle_Cnt_Low,
119 (((SMI_SAMPLE_PERIOD - 1) & 0xFFFF) << 16) |
120 (SMI_SAMPLE_IDLEMIN & 0xFFFF));
122 smi_set(IR_Init_Reg, rbIRen | rbIRhighidle);
124 smi_ir_enableInterrupt(ir);
127 int smi_ir_init(struct smi_dev *dev)
130 struct rc_dev *rc_dev;
131 struct smi_rc *ir = &dev->ir;
133 rc_dev = rc_allocate_device(RC_DRIVER_IR_RAW);
137 /* init input device */
138 snprintf(ir->device_name, sizeof(ir->device_name), "IR (%s)",
140 snprintf(ir->input_phys, sizeof(ir->input_phys), "pci-%s/ir0",
141 pci_name(dev->pci_dev));
143 rc_dev->allowed_protocols = RC_PROTO_BIT_ALL_IR_DECODER;
144 rc_dev->driver_name = "SMI_PCIe";
145 rc_dev->input_phys = ir->input_phys;
146 rc_dev->device_name = ir->device_name;
147 rc_dev->input_id.bustype = BUS_PCI;
148 rc_dev->input_id.version = 1;
149 rc_dev->input_id.vendor = dev->pci_dev->subsystem_vendor;
150 rc_dev->input_id.product = dev->pci_dev->subsystem_device;
151 rc_dev->dev.parent = &dev->pci_dev->dev;
153 rc_dev->map_name = dev->info->rc_map;
154 rc_dev->timeout = MS_TO_NS(100);
155 rc_dev->rx_resolution = US_TO_NS(SMI_SAMPLE_PERIOD);
160 smi_ir_disableInterrupt(ir);
162 ret = rc_register_device(rc_dev);
168 rc_free_device(rc_dev);
172 void smi_ir_exit(struct smi_dev *dev)
174 struct smi_rc *ir = &dev->ir;
175 struct rc_dev *rc_dev = ir->rc_dev;
178 rc_unregister_device(rc_dev);