1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * SMI PCIe driver for DVBSky cards.
4 *
5 * Copyright (C) 2014 Max nibble <nibble.max@gmail.com>
6 */
7
8 #include "smipcie.h"
9
10 #define SMI_SAMPLE_PERIOD 83
11 #define SMI_SAMPLE_IDLEMIN (10000 / SMI_SAMPLE_PERIOD)
12
smi_ir_enableInterrupt(struct smi_rc * ir)13 static void smi_ir_enableInterrupt(struct smi_rc *ir)
14 {
15 struct smi_dev *dev = ir->dev;
16
17 smi_write(MSI_INT_ENA_SET, IR_X_INT);
18 }
19
smi_ir_disableInterrupt(struct smi_rc * ir)20 static void smi_ir_disableInterrupt(struct smi_rc *ir)
21 {
22 struct smi_dev *dev = ir->dev;
23
24 smi_write(MSI_INT_ENA_CLR, IR_X_INT);
25 }
26
smi_ir_clearInterrupt(struct smi_rc * ir)27 static void smi_ir_clearInterrupt(struct smi_rc *ir)
28 {
29 struct smi_dev *dev = ir->dev;
30
31 smi_write(MSI_INT_STATUS_CLR, IR_X_INT);
32 }
33
smi_ir_stop(struct smi_rc * ir)34 static void smi_ir_stop(struct smi_rc *ir)
35 {
36 struct smi_dev *dev = ir->dev;
37
38 smi_ir_disableInterrupt(ir);
39 smi_clear(IR_Init_Reg, rbIRen);
40 }
41
smi_raw_process(struct rc_dev * rc_dev,const u8 * buffer,const u8 length)42 static void smi_raw_process(struct rc_dev *rc_dev, const u8 *buffer,
43 const u8 length)
44 {
45 struct ir_raw_event rawir = {};
46 int cnt;
47
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);
55 }
56 }
57 }
58
smi_ir_decode(struct smi_rc * ir)59 static void smi_ir_decode(struct smi_rc *ir)
60 {
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;
65
66 dwIRControl = smi_read(IR_Init_Reg);
67
68 if (dwIRControl & rbIRVld) {
69 ucIRCount = (u8) smi_read(IR_Data_Cnt);
70
71 readLoop = ucIRCount/4;
72 if (ucIRCount % 4)
73 readLoop += 1;
74 for (index = 0; index < readLoop; index++) {
75 dwIRData = smi_read(IR_DATA_BUFFER_BASE + (index * 4));
76
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);
81 }
82 smi_raw_process(rc_dev, ir->irData, ucIRCount);
83 smi_set(IR_Init_Reg, rbIRVld);
84 }
85
86 if (dwIRControl & rbIRhighidle) {
87 struct ir_raw_event rawir = {};
88
89 rawir.pulse = 0;
90 rawir.duration = SMI_SAMPLE_PERIOD * SMI_SAMPLE_IDLEMIN;
91 ir_raw_event_store_with_filter(rc_dev, &rawir);
92 smi_set(IR_Init_Reg, rbIRhighidle);
93 }
94
95 ir_raw_event_handle(rc_dev);
96 }
97
98 /* ir functions call by main driver.*/
smi_ir_irq(struct smi_rc * ir,u32 int_status)99 int smi_ir_irq(struct smi_rc *ir, u32 int_status)
100 {
101 int handled = 0;
102
103 if (int_status & IR_X_INT) {
104 smi_ir_disableInterrupt(ir);
105 smi_ir_clearInterrupt(ir);
106 smi_ir_decode(ir);
107 smi_ir_enableInterrupt(ir);
108 handled = 1;
109 }
110 return handled;
111 }
112
smi_ir_start(struct smi_rc * ir)113 void smi_ir_start(struct smi_rc *ir)
114 {
115 struct smi_dev *dev = ir->dev;
116
117 smi_write(IR_Idle_Cnt_Low,
118 (((SMI_SAMPLE_PERIOD - 1) & 0xFFFF) << 16) |
119 (SMI_SAMPLE_IDLEMIN & 0xFFFF));
120 msleep(20);
121 smi_set(IR_Init_Reg, rbIRen | rbIRhighidle);
122
123 smi_ir_enableInterrupt(ir);
124 }
125
smi_ir_init(struct smi_dev * dev)126 int smi_ir_init(struct smi_dev *dev)
127 {
128 int ret;
129 struct rc_dev *rc_dev;
130 struct smi_rc *ir = &dev->ir;
131
132 rc_dev = rc_allocate_device(RC_DRIVER_IR_RAW);
133 if (!rc_dev)
134 return -ENOMEM;
135
136 /* init input device */
137 snprintf(ir->device_name, sizeof(ir->device_name), "IR (%s)",
138 dev->info->name);
139 snprintf(ir->input_phys, sizeof(ir->input_phys), "pci-%s/ir0",
140 pci_name(dev->pci_dev));
141
142 rc_dev->allowed_protocols = RC_PROTO_BIT_ALL_IR_DECODER;
143 rc_dev->driver_name = "SMI_PCIe";
144 rc_dev->input_phys = ir->input_phys;
145 rc_dev->device_name = ir->device_name;
146 rc_dev->input_id.bustype = BUS_PCI;
147 rc_dev->input_id.version = 1;
148 rc_dev->input_id.vendor = dev->pci_dev->subsystem_vendor;
149 rc_dev->input_id.product = dev->pci_dev->subsystem_device;
150 rc_dev->dev.parent = &dev->pci_dev->dev;
151
152 rc_dev->map_name = dev->info->rc_map;
153 rc_dev->timeout = MS_TO_US(100);
154 rc_dev->rx_resolution = SMI_SAMPLE_PERIOD;
155
156 ir->rc_dev = rc_dev;
157 ir->dev = dev;
158
159 smi_ir_disableInterrupt(ir);
160
161 ret = rc_register_device(rc_dev);
162 if (ret)
163 goto ir_err;
164
165 return 0;
166 ir_err:
167 rc_free_device(rc_dev);
168 return ret;
169 }
170
smi_ir_exit(struct smi_dev * dev)171 void smi_ir_exit(struct smi_dev *dev)
172 {
173 struct smi_rc *ir = &dev->ir;
174 struct rc_dev *rc_dev = ir->rc_dev;
175
176 smi_ir_stop(ir);
177 rc_unregister_device(rc_dev);
178 ir->rc_dev = NULL;
179 }
180