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 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 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 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 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 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 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 control, data; 64 u8 index, ir_count, read_loop; 65 66 control = smi_read(IR_Init_Reg); 67 68 dev_dbg(&rc_dev->dev, "ircontrol: 0x%08x\n", control); 69 70 if (control & rbIRVld) { 71 ir_count = (u8)smi_read(IR_Data_Cnt); 72 73 dev_dbg(&rc_dev->dev, "ircount %d\n", ir_count); 74 75 read_loop = ir_count / 4; 76 if (ir_count % 4) 77 read_loop += 1; 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); 81 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); 86 } 87 smi_raw_process(rc_dev, ir->irData, ir_count); 88 } 89 90 if (control & rbIRhighidle) { 91 struct ir_raw_event rawir = {}; 92 93 dev_dbg(&rc_dev->dev, "high idle\n"); 94 95 rawir.pulse = 0; 96 rawir.duration = SMI_SAMPLE_PERIOD * SMI_SAMPLE_IDLEMIN; 97 ir_raw_event_store_with_filter(rc_dev, &rawir); 98 } 99 100 smi_set(IR_Init_Reg, rbIRVld); 101 ir_raw_event_handle(rc_dev); 102 } 103 104 /* ir functions call by main driver.*/ 105 int smi_ir_irq(struct smi_rc *ir, u32 int_status) 106 { 107 int handled = 0; 108 109 if (int_status & IR_X_INT) { 110 smi_ir_disableInterrupt(ir); 111 smi_ir_clearInterrupt(ir); 112 smi_ir_decode(ir); 113 smi_ir_enableInterrupt(ir); 114 handled = 1; 115 } 116 return handled; 117 } 118 119 void smi_ir_start(struct smi_rc *ir) 120 { 121 struct smi_dev *dev = ir->dev; 122 123 smi_write(IR_Idle_Cnt_Low, 124 (((SMI_SAMPLE_PERIOD - 1) & 0xFFFF) << 16) | 125 (SMI_SAMPLE_IDLEMIN & 0xFFFF)); 126 msleep(20); 127 smi_set(IR_Init_Reg, rbIRen | rbIRhighidle); 128 129 smi_ir_enableInterrupt(ir); 130 } 131 132 int smi_ir_init(struct smi_dev *dev) 133 { 134 int ret; 135 struct rc_dev *rc_dev; 136 struct smi_rc *ir = &dev->ir; 137 138 rc_dev = rc_allocate_device(RC_DRIVER_IR_RAW); 139 if (!rc_dev) 140 return -ENOMEM; 141 142 /* init input device */ 143 snprintf(ir->device_name, sizeof(ir->device_name), "IR (%s)", 144 dev->info->name); 145 snprintf(ir->input_phys, sizeof(ir->input_phys), "pci-%s/ir0", 146 pci_name(dev->pci_dev)); 147 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; 157 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; 161 162 ir->rc_dev = rc_dev; 163 ir->dev = dev; 164 165 smi_ir_disableInterrupt(ir); 166 167 ret = rc_register_device(rc_dev); 168 if (ret) 169 goto ir_err; 170 171 return 0; 172 ir_err: 173 rc_free_device(rc_dev); 174 return ret; 175 } 176 177 void smi_ir_exit(struct smi_dev *dev) 178 { 179 struct smi_rc *ir = &dev->ir; 180 struct rc_dev *rc_dev = ir->rc_dev; 181 182 rc_unregister_device(rc_dev); 183 smi_ir_stop(ir); 184 ir->rc_dev = NULL; 185 } 186