1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * AMD MP2 PCIe communication driver 4 * Copyright 2020 Advanced Micro Devices, Inc. 5 * 6 * Authors: Shyam Sundar S K <Shyam-sundar.S-k@amd.com> 7 * Sandeep Singh <Sandeep.singh@amd.com> 8 */ 9 10 #include <linux/bitops.h> 11 #include <linux/delay.h> 12 #include <linux/dma-mapping.h> 13 #include <linux/dmi.h> 14 #include <linux/interrupt.h> 15 #include <linux/io-64-nonatomic-lo-hi.h> 16 #include <linux/iopoll.h> 17 #include <linux/module.h> 18 #include <linux/slab.h> 19 20 #include "amd_sfh_pcie.h" 21 22 #define DRIVER_NAME "pcie_mp2_amd" 23 #define DRIVER_DESC "AMD(R) PCIe MP2 Communication Driver" 24 25 #define ACEL_EN BIT(0) 26 #define GYRO_EN BIT(1) 27 #define MAGNO_EN BIT(2) 28 #define HPD_EN BIT(16) 29 #define ALS_EN BIT(19) 30 31 static int sensor_mask_override = -1; 32 module_param_named(sensor_mask, sensor_mask_override, int, 0444); 33 MODULE_PARM_DESC(sensor_mask, "override the detected sensors mask"); 34 35 static int amd_sfh_wait_response_v2(struct amd_mp2_dev *mp2, u8 sid, u32 sensor_sts) 36 { 37 union cmd_response cmd_resp; 38 39 /* Get response with status within a max of 800 ms timeout */ 40 if (!readl_poll_timeout(mp2->mmio + AMD_P2C_MSG(0), cmd_resp.resp, 41 (cmd_resp.response_v2.response == sensor_sts && 42 cmd_resp.response_v2.status == 0 && (sid == 0xff || 43 cmd_resp.response_v2.sensor_id == sid)), 500, 800000)) 44 return cmd_resp.response_v2.response; 45 46 return SENSOR_DISABLED; 47 } 48 49 static void amd_start_sensor_v2(struct amd_mp2_dev *privdata, struct amd_mp2_sensor_info info) 50 { 51 union sfh_cmd_base cmd_base; 52 53 cmd_base.ul = 0; 54 cmd_base.cmd_v2.cmd_id = ENABLE_SENSOR; 55 cmd_base.cmd_v2.period = info.period; 56 cmd_base.cmd_v2.sensor_id = info.sensor_idx; 57 cmd_base.cmd_v2.length = 16; 58 59 if (info.sensor_idx == als_idx) 60 cmd_base.cmd_v2.mem_type = USE_C2P_REG; 61 62 writeq(info.dma_address, privdata->mmio + AMD_C2P_MSG1); 63 writel(cmd_base.ul, privdata->mmio + AMD_C2P_MSG0); 64 } 65 66 static void amd_stop_sensor_v2(struct amd_mp2_dev *privdata, u16 sensor_idx) 67 { 68 union sfh_cmd_base cmd_base; 69 70 cmd_base.ul = 0; 71 cmd_base.cmd_v2.cmd_id = DISABLE_SENSOR; 72 cmd_base.cmd_v2.period = 0; 73 cmd_base.cmd_v2.sensor_id = sensor_idx; 74 cmd_base.cmd_v2.length = 16; 75 76 writeq(0x0, privdata->mmio + AMD_C2P_MSG1); 77 writel(cmd_base.ul, privdata->mmio + AMD_C2P_MSG0); 78 } 79 80 static void amd_stop_all_sensor_v2(struct amd_mp2_dev *privdata) 81 { 82 union sfh_cmd_base cmd_base; 83 84 cmd_base.cmd_v2.cmd_id = STOP_ALL_SENSORS; 85 cmd_base.cmd_v2.period = 0; 86 cmd_base.cmd_v2.sensor_id = 0; 87 88 writel(cmd_base.ul, privdata->mmio + AMD_C2P_MSG0); 89 } 90 91 void amd_start_sensor(struct amd_mp2_dev *privdata, struct amd_mp2_sensor_info info) 92 { 93 union sfh_cmd_param cmd_param; 94 union sfh_cmd_base cmd_base; 95 96 /* fill up command register */ 97 memset(&cmd_base, 0, sizeof(cmd_base)); 98 cmd_base.s.cmd_id = ENABLE_SENSOR; 99 cmd_base.s.period = info.period; 100 cmd_base.s.sensor_id = info.sensor_idx; 101 102 /* fill up command param register */ 103 memset(&cmd_param, 0, sizeof(cmd_param)); 104 cmd_param.s.buf_layout = 1; 105 cmd_param.s.buf_length = 16; 106 107 writeq(info.dma_address, privdata->mmio + AMD_C2P_MSG2); 108 writel(cmd_param.ul, privdata->mmio + AMD_C2P_MSG1); 109 writel(cmd_base.ul, privdata->mmio + AMD_C2P_MSG0); 110 } 111 112 void amd_stop_sensor(struct amd_mp2_dev *privdata, u16 sensor_idx) 113 { 114 union sfh_cmd_base cmd_base; 115 116 /* fill up command register */ 117 memset(&cmd_base, 0, sizeof(cmd_base)); 118 cmd_base.s.cmd_id = DISABLE_SENSOR; 119 cmd_base.s.period = 0; 120 cmd_base.s.sensor_id = sensor_idx; 121 122 writeq(0x0, privdata->mmio + AMD_C2P_MSG2); 123 writel(cmd_base.ul, privdata->mmio + AMD_C2P_MSG0); 124 } 125 126 void amd_stop_all_sensors(struct amd_mp2_dev *privdata) 127 { 128 union sfh_cmd_base cmd_base; 129 130 /* fill up command register */ 131 memset(&cmd_base, 0, sizeof(cmd_base)); 132 cmd_base.s.cmd_id = STOP_ALL_SENSORS; 133 cmd_base.s.period = 0; 134 cmd_base.s.sensor_id = 0; 135 136 writel(cmd_base.ul, privdata->mmio + AMD_C2P_MSG0); 137 } 138 139 static const struct dmi_system_id dmi_sensor_mask_overrides[] = { 140 { 141 .matches = { 142 DMI_MATCH(DMI_PRODUCT_NAME, "HP ENVY x360 Convertible 13-ag0xxx"), 143 }, 144 .driver_data = (void *)(ACEL_EN | MAGNO_EN), 145 }, 146 { 147 .matches = { 148 DMI_MATCH(DMI_PRODUCT_NAME, "HP ENVY x360 Convertible 15-cp0xxx"), 149 }, 150 .driver_data = (void *)(ACEL_EN | MAGNO_EN), 151 }, 152 { } 153 }; 154 155 int amd_mp2_get_sensor_num(struct amd_mp2_dev *privdata, u8 *sensor_id) 156 { 157 int activestatus, num_of_sensors = 0; 158 const struct dmi_system_id *dmi_id; 159 160 if (sensor_mask_override == -1) { 161 dmi_id = dmi_first_match(dmi_sensor_mask_overrides); 162 if (dmi_id) 163 sensor_mask_override = (long)dmi_id->driver_data; 164 } 165 166 if (sensor_mask_override >= 0) { 167 activestatus = sensor_mask_override; 168 } else { 169 activestatus = privdata->mp2_acs >> 4; 170 } 171 172 if (ACEL_EN & activestatus) 173 sensor_id[num_of_sensors++] = accel_idx; 174 175 if (GYRO_EN & activestatus) 176 sensor_id[num_of_sensors++] = gyro_idx; 177 178 if (MAGNO_EN & activestatus) 179 sensor_id[num_of_sensors++] = mag_idx; 180 181 if (ALS_EN & activestatus) 182 sensor_id[num_of_sensors++] = als_idx; 183 184 if (HPD_EN & activestatus) 185 sensor_id[num_of_sensors++] = HPD_IDX; 186 187 return num_of_sensors; 188 } 189 190 static void amd_mp2_pci_remove(void *privdata) 191 { 192 struct amd_mp2_dev *mp2 = privdata; 193 amd_sfh_hid_client_deinit(privdata); 194 mp2->mp2_ops->stop_all(mp2); 195 } 196 197 static const struct amd_mp2_ops amd_sfh_ops_v2 = { 198 .start = amd_start_sensor_v2, 199 .stop = amd_stop_sensor_v2, 200 .stop_all = amd_stop_all_sensor_v2, 201 .response = amd_sfh_wait_response_v2, 202 }; 203 204 static const struct amd_mp2_ops amd_sfh_ops = { 205 .start = amd_start_sensor, 206 .stop = amd_stop_sensor, 207 .stop_all = amd_stop_all_sensors, 208 }; 209 210 static void mp2_select_ops(struct amd_mp2_dev *privdata) 211 { 212 u8 acs; 213 214 privdata->mp2_acs = readl(privdata->mmio + AMD_P2C_MSG3); 215 acs = privdata->mp2_acs & GENMASK(3, 0); 216 217 switch (acs) { 218 case V2_STATUS: 219 privdata->mp2_ops = &amd_sfh_ops_v2; 220 break; 221 default: 222 privdata->mp2_ops = &amd_sfh_ops; 223 break; 224 } 225 } 226 227 static int amd_mp2_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id) 228 { 229 struct amd_mp2_dev *privdata; 230 int rc; 231 232 privdata = devm_kzalloc(&pdev->dev, sizeof(*privdata), GFP_KERNEL); 233 if (!privdata) 234 return -ENOMEM; 235 236 privdata->pdev = pdev; 237 pci_set_drvdata(pdev, privdata); 238 rc = pcim_enable_device(pdev); 239 if (rc) 240 return rc; 241 242 rc = pcim_iomap_regions(pdev, BIT(2), DRIVER_NAME); 243 if (rc) 244 return rc; 245 246 privdata->mmio = pcim_iomap_table(pdev)[2]; 247 pci_set_master(pdev); 248 rc = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)); 249 if (rc) { 250 rc = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)); 251 if (rc) { 252 dev_err(&pdev->dev, "failed to set DMA mask\n"); 253 return rc; 254 } 255 } 256 257 privdata->cl_data = devm_kzalloc(&pdev->dev, sizeof(struct amdtp_cl_data), GFP_KERNEL); 258 if (!privdata->cl_data) 259 return -ENOMEM; 260 261 rc = devm_add_action_or_reset(&pdev->dev, amd_mp2_pci_remove, privdata); 262 if (rc) 263 return rc; 264 265 mp2_select_ops(privdata); 266 267 return amd_sfh_hid_client_init(privdata); 268 } 269 270 static int __maybe_unused amd_mp2_pci_resume(struct device *dev) 271 { 272 struct pci_dev *pdev = to_pci_dev(dev); 273 struct amd_mp2_dev *mp2 = pci_get_drvdata(pdev); 274 struct amdtp_cl_data *cl_data = mp2->cl_data; 275 struct amd_mp2_sensor_info info; 276 int i, status; 277 278 for (i = 0; i < cl_data->num_hid_devices; i++) { 279 if (cl_data->sensor_sts[i] == SENSOR_DISABLED) { 280 info.period = AMD_SFH_IDLE_LOOP; 281 info.sensor_idx = cl_data->sensor_idx[i]; 282 info.dma_address = cl_data->sensor_dma_addr[i]; 283 mp2->mp2_ops->start(mp2, info); 284 status = amd_sfh_wait_for_response 285 (mp2, cl_data->sensor_idx[i], SENSOR_ENABLED); 286 if (status == SENSOR_ENABLED) 287 cl_data->sensor_sts[i] = SENSOR_ENABLED; 288 dev_dbg(dev, "resume sid 0x%x status 0x%x\n", 289 cl_data->sensor_idx[i], cl_data->sensor_sts[i]); 290 } 291 } 292 293 return 0; 294 } 295 296 static int __maybe_unused amd_mp2_pci_suspend(struct device *dev) 297 { 298 struct pci_dev *pdev = to_pci_dev(dev); 299 struct amd_mp2_dev *mp2 = pci_get_drvdata(pdev); 300 struct amdtp_cl_data *cl_data = mp2->cl_data; 301 int i, status; 302 303 for (i = 0; i < cl_data->num_hid_devices; i++) { 304 if (cl_data->sensor_idx[i] != HPD_IDX && 305 cl_data->sensor_sts[i] == SENSOR_ENABLED) { 306 mp2->mp2_ops->stop(mp2, cl_data->sensor_idx[i]); 307 status = amd_sfh_wait_for_response 308 (mp2, cl_data->sensor_idx[i], SENSOR_DISABLED); 309 if (status != SENSOR_ENABLED) 310 cl_data->sensor_sts[i] = SENSOR_DISABLED; 311 dev_dbg(dev, "suspend sid 0x%x status 0x%x\n", 312 cl_data->sensor_idx[i], cl_data->sensor_sts[i]); 313 } 314 } 315 316 return 0; 317 } 318 319 static SIMPLE_DEV_PM_OPS(amd_mp2_pm_ops, amd_mp2_pci_suspend, 320 amd_mp2_pci_resume); 321 322 static const struct pci_device_id amd_mp2_pci_tbl[] = { 323 { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_MP2) }, 324 { } 325 }; 326 MODULE_DEVICE_TABLE(pci, amd_mp2_pci_tbl); 327 328 static struct pci_driver amd_mp2_pci_driver = { 329 .name = DRIVER_NAME, 330 .id_table = amd_mp2_pci_tbl, 331 .probe = amd_mp2_pci_probe, 332 .driver.pm = &amd_mp2_pm_ops, 333 }; 334 module_pci_driver(amd_mp2_pci_driver); 335 336 MODULE_DESCRIPTION(DRIVER_DESC); 337 MODULE_LICENSE("Dual BSD/GPL"); 338 MODULE_AUTHOR("Shyam Sundar S K <Shyam-sundar.S-k@amd.com>"); 339 MODULE_AUTHOR("Sandeep Singh <Sandeep.singh@amd.com>"); 340