1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * AMD MP2 1.1 communication driver 4 * 5 * Copyright (c) 2022, Advanced Micro Devices, Inc. 6 * All Rights Reserved. 7 * 8 * Author: Basavaraj Natikar <Basavaraj.Natikar@amd.com> 9 */ 10 11 #include <linux/delay.h> 12 #include <linux/hid.h> 13 14 #include "amd_sfh_init.h" 15 #include "amd_sfh_interface.h" 16 #include "../hid_descriptor/amd_sfh_hid_desc.h" 17 18 static int amd_sfh_get_sensor_num(struct amd_mp2_dev *mp2, u8 *sensor_id) 19 { 20 struct sfh_sensor_list *slist; 21 struct sfh_base_info binfo; 22 int num_of_sensors = 0; 23 int i; 24 25 memcpy_fromio(&binfo, mp2->vsbase, sizeof(struct sfh_base_info)); 26 slist = &binfo.sbase.s_list; 27 28 for (i = 0; i < MAX_IDX; i++) { 29 switch (i) { 30 case ACCEL_IDX: 31 case GYRO_IDX: 32 case MAG_IDX: 33 case ALS_IDX: 34 case HPD_IDX: 35 if (BIT(i) & slist->sl.sensors) 36 sensor_id[num_of_sensors++] = i; 37 break; 38 } 39 } 40 41 return num_of_sensors; 42 } 43 44 static u32 amd_sfh_wait_for_response(struct amd_mp2_dev *mp2, u8 sid, u32 cmd_id) 45 { 46 if (mp2->mp2_ops->response) 47 return mp2->mp2_ops->response(mp2, sid, cmd_id); 48 49 return 0; 50 } 51 52 static const char *get_sensor_name(int idx) 53 { 54 switch (idx) { 55 case ACCEL_IDX: 56 return "accelerometer"; 57 case GYRO_IDX: 58 return "gyroscope"; 59 case MAG_IDX: 60 return "magnetometer"; 61 case ALS_IDX: 62 return "ALS"; 63 case HPD_IDX: 64 return "HPD"; 65 default: 66 return "unknown sensor type"; 67 } 68 } 69 70 static int amd_sfh_hid_client_deinit(struct amd_mp2_dev *privdata) 71 { 72 struct amdtp_cl_data *cl_data = privdata->cl_data; 73 int i, status; 74 75 for (i = 0; i < cl_data->num_hid_devices; i++) { 76 if (cl_data->sensor_sts[i] == SENSOR_ENABLED) { 77 privdata->mp2_ops->stop(privdata, cl_data->sensor_idx[i]); 78 status = amd_sfh_wait_for_response 79 (privdata, cl_data->sensor_idx[i], DISABLE_SENSOR); 80 if (status == 0) 81 cl_data->sensor_sts[i] = SENSOR_DISABLED; 82 dev_dbg(&privdata->pdev->dev, "stopping sid 0x%x (%s) status 0x%x\n", 83 cl_data->sensor_idx[i], get_sensor_name(cl_data->sensor_idx[i]), 84 cl_data->sensor_sts[i]); 85 } 86 } 87 88 cancel_delayed_work_sync(&cl_data->work); 89 cancel_delayed_work_sync(&cl_data->work_buffer); 90 amdtp_hid_remove(cl_data); 91 92 return 0; 93 } 94 95 static int amd_sfh1_1_hid_client_init(struct amd_mp2_dev *privdata) 96 { 97 struct amd_input_data *in_data = &privdata->in_data; 98 struct amdtp_cl_data *cl_data = privdata->cl_data; 99 struct amd_mp2_ops *mp2_ops = privdata->mp2_ops; 100 struct amd_mp2_sensor_info info; 101 struct request_list *req_list; 102 u32 feature_report_size; 103 u32 input_report_size; 104 struct device *dev; 105 int rc, i, status; 106 u8 cl_idx; 107 108 req_list = &cl_data->req_list; 109 dev = &privdata->pdev->dev; 110 amd_sfh1_1_set_desc_ops(mp2_ops); 111 112 cl_data->num_hid_devices = amd_sfh_get_sensor_num(privdata, &cl_data->sensor_idx[0]); 113 114 INIT_DELAYED_WORK(&cl_data->work, amd_sfh_work); 115 INIT_DELAYED_WORK(&cl_data->work_buffer, amd_sfh_work_buffer); 116 INIT_LIST_HEAD(&req_list->list); 117 cl_data->in_data = in_data; 118 119 for (i = 0; i < cl_data->num_hid_devices; i++) { 120 cl_data->sensor_sts[i] = SENSOR_DISABLED; 121 cl_data->sensor_requested_cnt[i] = 0; 122 cl_data->cur_hid_dev = i; 123 cl_idx = cl_data->sensor_idx[i]; 124 125 cl_data->report_descr_sz[i] = mp2_ops->get_desc_sz(cl_idx, descr_size); 126 if (!cl_data->report_descr_sz[i]) { 127 rc = -EINVAL; 128 goto cleanup; 129 } 130 feature_report_size = mp2_ops->get_desc_sz(cl_idx, feature_size); 131 if (!feature_report_size) { 132 rc = -EINVAL; 133 goto cleanup; 134 } 135 input_report_size = mp2_ops->get_desc_sz(cl_idx, input_size); 136 if (!input_report_size) { 137 rc = -EINVAL; 138 goto cleanup; 139 } 140 cl_data->feature_report[i] = devm_kzalloc(dev, feature_report_size, GFP_KERNEL); 141 if (!cl_data->feature_report[i]) { 142 rc = -ENOMEM; 143 goto cleanup; 144 } 145 in_data->input_report[i] = devm_kzalloc(dev, input_report_size, GFP_KERNEL); 146 if (!in_data->input_report[i]) { 147 rc = -ENOMEM; 148 goto cleanup; 149 } 150 151 info.sensor_idx = cl_idx; 152 153 cl_data->report_descr[i] = 154 devm_kzalloc(dev, cl_data->report_descr_sz[i], GFP_KERNEL); 155 if (!cl_data->report_descr[i]) { 156 rc = -ENOMEM; 157 goto cleanup; 158 } 159 rc = mp2_ops->get_rep_desc(cl_idx, cl_data->report_descr[i]); 160 if (rc) 161 return rc; 162 163 writel(0, privdata->mmio + AMD_P2C_MSG(0)); 164 mp2_ops->start(privdata, info); 165 status = amd_sfh_wait_for_response 166 (privdata, cl_data->sensor_idx[i], ENABLE_SENSOR); 167 168 status = (status == 0) ? SENSOR_ENABLED : SENSOR_DISABLED; 169 170 if (status == SENSOR_ENABLED) { 171 cl_data->sensor_sts[i] = SENSOR_ENABLED; 172 rc = amdtp_hid_probe(i, cl_data); 173 if (rc) { 174 mp2_ops->stop(privdata, cl_data->sensor_idx[i]); 175 status = amd_sfh_wait_for_response 176 (privdata, cl_data->sensor_idx[i], DISABLE_SENSOR); 177 if (status == 0) 178 status = SENSOR_DISABLED; 179 if (status != SENSOR_ENABLED) 180 cl_data->sensor_sts[i] = SENSOR_DISABLED; 181 dev_dbg(dev, "sid 0x%x (%s) status 0x%x\n", 182 cl_data->sensor_idx[i], 183 get_sensor_name(cl_data->sensor_idx[i]), 184 cl_data->sensor_sts[i]); 185 goto cleanup; 186 } 187 } 188 dev_dbg(dev, "sid 0x%x (%s) status 0x%x\n", 189 cl_data->sensor_idx[i], get_sensor_name(cl_data->sensor_idx[i]), 190 cl_data->sensor_sts[i]); 191 } 192 193 schedule_delayed_work(&cl_data->work_buffer, msecs_to_jiffies(AMD_SFH_IDLE_LOOP)); 194 return 0; 195 196 cleanup: 197 amd_sfh_hid_client_deinit(privdata); 198 for (i = 0; i < cl_data->num_hid_devices; i++) { 199 devm_kfree(dev, cl_data->feature_report[i]); 200 devm_kfree(dev, in_data->input_report[i]); 201 devm_kfree(dev, cl_data->report_descr[i]); 202 } 203 return rc; 204 } 205 206 static void amd_sfh_resume(struct amd_mp2_dev *mp2) 207 { 208 struct amdtp_cl_data *cl_data = mp2->cl_data; 209 struct amd_mp2_sensor_info info; 210 int i, status; 211 212 for (i = 0; i < cl_data->num_hid_devices; i++) { 213 if (cl_data->sensor_sts[i] == SENSOR_DISABLED) { 214 info.sensor_idx = cl_data->sensor_idx[i]; 215 mp2->mp2_ops->start(mp2, info); 216 status = amd_sfh_wait_for_response 217 (mp2, cl_data->sensor_idx[i], ENABLE_SENSOR); 218 if (status == 0) 219 status = SENSOR_ENABLED; 220 if (status == SENSOR_ENABLED) 221 cl_data->sensor_sts[i] = SENSOR_ENABLED; 222 dev_dbg(&mp2->pdev->dev, "resume sid 0x%x (%s) status 0x%x\n", 223 cl_data->sensor_idx[i], get_sensor_name(cl_data->sensor_idx[i]), 224 cl_data->sensor_sts[i]); 225 } 226 } 227 228 schedule_delayed_work(&cl_data->work_buffer, msecs_to_jiffies(AMD_SFH_IDLE_LOOP)); 229 amd_sfh_clear_intr(mp2); 230 } 231 232 static void amd_sfh_suspend(struct amd_mp2_dev *mp2) 233 { 234 struct amdtp_cl_data *cl_data = mp2->cl_data; 235 int i, status; 236 237 for (i = 0; i < cl_data->num_hid_devices; i++) { 238 if (cl_data->sensor_idx[i] != HPD_IDX && 239 cl_data->sensor_sts[i] == SENSOR_ENABLED) { 240 mp2->mp2_ops->stop(mp2, cl_data->sensor_idx[i]); 241 status = amd_sfh_wait_for_response 242 (mp2, cl_data->sensor_idx[i], DISABLE_SENSOR); 243 if (status == 0) 244 status = SENSOR_DISABLED; 245 if (status != SENSOR_ENABLED) 246 cl_data->sensor_sts[i] = SENSOR_DISABLED; 247 dev_dbg(&mp2->pdev->dev, "suspend sid 0x%x (%s) status 0x%x\n", 248 cl_data->sensor_idx[i], get_sensor_name(cl_data->sensor_idx[i]), 249 cl_data->sensor_sts[i]); 250 } 251 } 252 253 cancel_delayed_work_sync(&cl_data->work_buffer); 254 amd_sfh_clear_intr(mp2); 255 } 256 257 static void amd_mp2_pci_remove(void *privdata) 258 { 259 struct amd_mp2_dev *mp2 = privdata; 260 261 amd_sfh_hid_client_deinit(privdata); 262 mp2->mp2_ops->stop_all(mp2); 263 pci_intx(mp2->pdev, false); 264 amd_sfh_clear_intr(mp2); 265 } 266 267 static void amd_sfh_set_ops(struct amd_mp2_dev *mp2) 268 { 269 struct amd_mp2_ops *mp2_ops; 270 271 sfh_interface_init(mp2); 272 mp2_ops = mp2->mp2_ops; 273 mp2_ops->clear_intr = amd_sfh_clear_intr_v2, 274 mp2_ops->init_intr = amd_sfh_irq_init_v2, 275 mp2_ops->suspend = amd_sfh_suspend; 276 mp2_ops->resume = amd_sfh_resume; 277 mp2_ops->remove = amd_mp2_pci_remove; 278 } 279 280 int amd_sfh1_1_init(struct amd_mp2_dev *mp2) 281 { 282 u32 phy_base = readl(mp2->mmio + AMD_C2P_MSG(22)); 283 struct device *dev = &mp2->pdev->dev; 284 struct sfh_base_info binfo; 285 int rc; 286 287 phy_base <<= 21; 288 if (!devm_request_mem_region(dev, phy_base, 128 * 1024, "amd_sfh")) { 289 dev_err(dev, "can't reserve mmio registers\n"); 290 return -ENOMEM; 291 } 292 293 mp2->vsbase = devm_ioremap(dev, phy_base, 128 * 1024); 294 if (!mp2->vsbase) { 295 dev_err(dev, "failed to remap vsbase\n"); 296 return -ENOMEM; 297 } 298 299 /* Before accessing give time for SFH firmware for processing configuration */ 300 msleep(5000); 301 302 memcpy_fromio(&binfo, mp2->vsbase, sizeof(struct sfh_base_info)); 303 if (binfo.sbase.fw_info.fw_ver == 0 || binfo.sbase.s_list.sl.sensors == 0) { 304 dev_err(dev, "failed to get sensors\n"); 305 return -EOPNOTSUPP; 306 } 307 dev_dbg(dev, "firmware version 0x%x\n", binfo.sbase.fw_info.fw_ver); 308 309 amd_sfh_set_ops(mp2); 310 311 rc = amd_sfh_irq_init(mp2); 312 if (rc) { 313 dev_err(dev, "amd_sfh_irq_init failed\n"); 314 return rc; 315 } 316 317 rc = amd_sfh1_1_hid_client_init(mp2); 318 if (rc) { 319 dev_err(dev, "amd_sfh1_1_hid_client_init failed\n"); 320 return rc; 321 } 322 323 return rc; 324 } 325