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 if (cl_data->num_hid_devices == 0) 114 return -ENODEV; 115 cl_data->is_any_sensor_enabled = false; 116 117 INIT_DELAYED_WORK(&cl_data->work, amd_sfh_work); 118 INIT_DELAYED_WORK(&cl_data->work_buffer, amd_sfh_work_buffer); 119 INIT_LIST_HEAD(&req_list->list); 120 cl_data->in_data = in_data; 121 122 for (i = 0; i < cl_data->num_hid_devices; i++) { 123 cl_data->sensor_sts[i] = SENSOR_DISABLED; 124 cl_data->sensor_requested_cnt[i] = 0; 125 cl_data->cur_hid_dev = i; 126 cl_idx = cl_data->sensor_idx[i]; 127 128 cl_data->report_descr_sz[i] = mp2_ops->get_desc_sz(cl_idx, descr_size); 129 if (!cl_data->report_descr_sz[i]) { 130 rc = -EINVAL; 131 goto cleanup; 132 } 133 feature_report_size = mp2_ops->get_desc_sz(cl_idx, feature_size); 134 if (!feature_report_size) { 135 rc = -EINVAL; 136 goto cleanup; 137 } 138 input_report_size = mp2_ops->get_desc_sz(cl_idx, input_size); 139 if (!input_report_size) { 140 rc = -EINVAL; 141 goto cleanup; 142 } 143 cl_data->feature_report[i] = devm_kzalloc(dev, feature_report_size, GFP_KERNEL); 144 if (!cl_data->feature_report[i]) { 145 rc = -ENOMEM; 146 goto cleanup; 147 } 148 in_data->input_report[i] = devm_kzalloc(dev, input_report_size, GFP_KERNEL); 149 if (!in_data->input_report[i]) { 150 rc = -ENOMEM; 151 goto cleanup; 152 } 153 154 info.sensor_idx = cl_idx; 155 156 cl_data->report_descr[i] = 157 devm_kzalloc(dev, cl_data->report_descr_sz[i], GFP_KERNEL); 158 if (!cl_data->report_descr[i]) { 159 rc = -ENOMEM; 160 goto cleanup; 161 } 162 rc = mp2_ops->get_rep_desc(cl_idx, cl_data->report_descr[i]); 163 if (rc) 164 goto cleanup; 165 166 writel(0, privdata->mmio + AMD_P2C_MSG(0)); 167 mp2_ops->start(privdata, info); 168 status = amd_sfh_wait_for_response 169 (privdata, cl_data->sensor_idx[i], ENABLE_SENSOR); 170 171 status = (status == 0) ? SENSOR_ENABLED : SENSOR_DISABLED; 172 173 if (status == SENSOR_ENABLED) { 174 cl_data->is_any_sensor_enabled = true; 175 cl_data->sensor_sts[i] = SENSOR_ENABLED; 176 rc = amdtp_hid_probe(i, cl_data); 177 if (rc) { 178 mp2_ops->stop(privdata, cl_data->sensor_idx[i]); 179 status = amd_sfh_wait_for_response 180 (privdata, cl_data->sensor_idx[i], DISABLE_SENSOR); 181 if (status == 0) 182 status = SENSOR_DISABLED; 183 if (status != SENSOR_ENABLED) 184 cl_data->sensor_sts[i] = SENSOR_DISABLED; 185 dev_dbg(dev, "sid 0x%x (%s) status 0x%x\n", 186 cl_data->sensor_idx[i], 187 get_sensor_name(cl_data->sensor_idx[i]), 188 cl_data->sensor_sts[i]); 189 goto cleanup; 190 } 191 } else { 192 cl_data->sensor_sts[i] = SENSOR_DISABLED; 193 } 194 dev_dbg(dev, "sid 0x%x (%s) status 0x%x\n", 195 cl_data->sensor_idx[i], get_sensor_name(cl_data->sensor_idx[i]), 196 cl_data->sensor_sts[i]); 197 } 198 199 if (!cl_data->is_any_sensor_enabled) { 200 dev_warn(dev, "Failed to discover, sensors not enabled is %d\n", 201 cl_data->is_any_sensor_enabled); 202 rc = -EOPNOTSUPP; 203 goto cleanup; 204 } 205 206 schedule_delayed_work(&cl_data->work_buffer, msecs_to_jiffies(AMD_SFH_IDLE_LOOP)); 207 return 0; 208 209 cleanup: 210 amd_sfh_hid_client_deinit(privdata); 211 for (i = 0; i < cl_data->num_hid_devices; i++) { 212 devm_kfree(dev, cl_data->feature_report[i]); 213 devm_kfree(dev, in_data->input_report[i]); 214 devm_kfree(dev, cl_data->report_descr[i]); 215 } 216 return rc; 217 } 218 219 static void amd_sfh_resume(struct amd_mp2_dev *mp2) 220 { 221 struct amdtp_cl_data *cl_data = mp2->cl_data; 222 struct amd_mp2_sensor_info info; 223 int i, status; 224 225 for (i = 0; i < cl_data->num_hid_devices; i++) { 226 if (cl_data->sensor_sts[i] == SENSOR_DISABLED) { 227 info.sensor_idx = cl_data->sensor_idx[i]; 228 mp2->mp2_ops->start(mp2, info); 229 status = amd_sfh_wait_for_response 230 (mp2, cl_data->sensor_idx[i], ENABLE_SENSOR); 231 if (status == 0) 232 status = SENSOR_ENABLED; 233 if (status == SENSOR_ENABLED) 234 cl_data->sensor_sts[i] = SENSOR_ENABLED; 235 dev_dbg(&mp2->pdev->dev, "resume sid 0x%x (%s) status 0x%x\n", 236 cl_data->sensor_idx[i], get_sensor_name(cl_data->sensor_idx[i]), 237 cl_data->sensor_sts[i]); 238 } 239 } 240 241 schedule_delayed_work(&cl_data->work_buffer, msecs_to_jiffies(AMD_SFH_IDLE_LOOP)); 242 amd_sfh_clear_intr(mp2); 243 } 244 245 static void amd_sfh_suspend(struct amd_mp2_dev *mp2) 246 { 247 struct amdtp_cl_data *cl_data = mp2->cl_data; 248 int i, status; 249 250 for (i = 0; i < cl_data->num_hid_devices; i++) { 251 if (cl_data->sensor_idx[i] != HPD_IDX && 252 cl_data->sensor_sts[i] == SENSOR_ENABLED) { 253 mp2->mp2_ops->stop(mp2, cl_data->sensor_idx[i]); 254 status = amd_sfh_wait_for_response 255 (mp2, cl_data->sensor_idx[i], DISABLE_SENSOR); 256 if (status == 0) 257 status = SENSOR_DISABLED; 258 if (status != SENSOR_ENABLED) 259 cl_data->sensor_sts[i] = SENSOR_DISABLED; 260 dev_dbg(&mp2->pdev->dev, "suspend sid 0x%x (%s) status 0x%x\n", 261 cl_data->sensor_idx[i], get_sensor_name(cl_data->sensor_idx[i]), 262 cl_data->sensor_sts[i]); 263 } 264 } 265 266 cancel_delayed_work_sync(&cl_data->work_buffer); 267 amd_sfh_clear_intr(mp2); 268 } 269 270 static void amd_mp2_pci_remove(void *privdata) 271 { 272 struct amd_mp2_dev *mp2 = privdata; 273 274 amd_sfh_hid_client_deinit(privdata); 275 mp2->mp2_ops->stop_all(mp2); 276 pci_intx(mp2->pdev, false); 277 amd_sfh_clear_intr(mp2); 278 } 279 280 static void amd_sfh_set_ops(struct amd_mp2_dev *mp2) 281 { 282 struct amd_mp2_ops *mp2_ops; 283 284 sfh_interface_init(mp2); 285 mp2_ops = mp2->mp2_ops; 286 mp2_ops->clear_intr = amd_sfh_clear_intr_v2, 287 mp2_ops->init_intr = amd_sfh_irq_init_v2, 288 mp2_ops->suspend = amd_sfh_suspend; 289 mp2_ops->resume = amd_sfh_resume; 290 mp2_ops->remove = amd_mp2_pci_remove; 291 } 292 293 int amd_sfh1_1_init(struct amd_mp2_dev *mp2) 294 { 295 u32 phy_base = readl(mp2->mmio + AMD_C2P_MSG(22)); 296 struct device *dev = &mp2->pdev->dev; 297 struct sfh_base_info binfo; 298 int rc; 299 300 phy_base <<= 21; 301 if (!devm_request_mem_region(dev, phy_base, 128 * 1024, "amd_sfh")) { 302 dev_dbg(dev, "can't reserve mmio registers\n"); 303 return -ENOMEM; 304 } 305 306 mp2->vsbase = devm_ioremap(dev, phy_base, 128 * 1024); 307 if (!mp2->vsbase) { 308 dev_dbg(dev, "failed to remap vsbase\n"); 309 return -ENOMEM; 310 } 311 312 /* Before accessing give time for SFH firmware for processing configuration */ 313 msleep(5000); 314 315 memcpy_fromio(&binfo, mp2->vsbase, sizeof(struct sfh_base_info)); 316 if (binfo.sbase.fw_info.fw_ver == 0 || binfo.sbase.s_list.sl.sensors == 0) { 317 dev_dbg(dev, "failed to get sensors\n"); 318 return -EOPNOTSUPP; 319 } 320 dev_dbg(dev, "firmware version 0x%x\n", binfo.sbase.fw_info.fw_ver); 321 322 amd_sfh_set_ops(mp2); 323 324 rc = amd_sfh_irq_init(mp2); 325 if (rc) { 326 dev_err(dev, "amd_sfh_irq_init failed\n"); 327 return rc; 328 } 329 330 rc = amd_sfh1_1_hid_client_init(mp2); 331 if (rc) { 332 dev_err(dev, "amd_sfh1_1_hid_client_init failed\n"); 333 return rc; 334 } 335 336 return rc; 337 } 338