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 		cl_data->sensor_sts[i] = (status == 0) ? SENSOR_ENABLED : SENSOR_DISABLED;
172 	}
173 
174 	for (i = 0; i < cl_data->num_hid_devices; i++) {
175 		cl_data->cur_hid_dev = i;
176 		if (cl_data->sensor_sts[i] == SENSOR_ENABLED) {
177 			cl_data->is_any_sensor_enabled = true;
178 			rc = amdtp_hid_probe(i, cl_data);
179 			if (rc)
180 				goto cleanup;
181 		}
182 		dev_dbg(dev, "sid 0x%x (%s) status 0x%x\n",
183 			cl_data->sensor_idx[i], get_sensor_name(cl_data->sensor_idx[i]),
184 			cl_data->sensor_sts[i]);
185 	}
186 
187 	if (!cl_data->is_any_sensor_enabled) {
188 		dev_warn(dev, "Failed to discover, sensors not enabled is %d\n",
189 			 cl_data->is_any_sensor_enabled);
190 		rc = -EOPNOTSUPP;
191 		goto cleanup;
192 	}
193 
194 	schedule_delayed_work(&cl_data->work_buffer, msecs_to_jiffies(AMD_SFH_IDLE_LOOP));
195 	return 0;
196 
197 cleanup:
198 	amd_sfh_hid_client_deinit(privdata);
199 	for (i = 0; i < cl_data->num_hid_devices; i++) {
200 		devm_kfree(dev, cl_data->feature_report[i]);
201 		devm_kfree(dev, in_data->input_report[i]);
202 		devm_kfree(dev, cl_data->report_descr[i]);
203 	}
204 	return rc;
205 }
206 
207 static void amd_sfh_resume(struct amd_mp2_dev *mp2)
208 {
209 	struct amdtp_cl_data *cl_data = mp2->cl_data;
210 	struct amd_mp2_sensor_info info;
211 	int i, status;
212 
213 	for (i = 0; i < cl_data->num_hid_devices; i++) {
214 		if (cl_data->sensor_sts[i] == SENSOR_DISABLED) {
215 			info.sensor_idx = cl_data->sensor_idx[i];
216 			mp2->mp2_ops->start(mp2, info);
217 			status = amd_sfh_wait_for_response
218 					(mp2, cl_data->sensor_idx[i], ENABLE_SENSOR);
219 			if (status == 0)
220 				status = SENSOR_ENABLED;
221 			if (status == SENSOR_ENABLED)
222 				cl_data->sensor_sts[i] = SENSOR_ENABLED;
223 			dev_dbg(&mp2->pdev->dev, "resume sid 0x%x (%s) status 0x%x\n",
224 				cl_data->sensor_idx[i], get_sensor_name(cl_data->sensor_idx[i]),
225 				cl_data->sensor_sts[i]);
226 		}
227 	}
228 
229 	schedule_delayed_work(&cl_data->work_buffer, msecs_to_jiffies(AMD_SFH_IDLE_LOOP));
230 	amd_sfh_clear_intr(mp2);
231 }
232 
233 static void amd_sfh_suspend(struct amd_mp2_dev *mp2)
234 {
235 	struct amdtp_cl_data *cl_data = mp2->cl_data;
236 	int i, status;
237 
238 	for (i = 0; i < cl_data->num_hid_devices; i++) {
239 		if (cl_data->sensor_idx[i] != HPD_IDX &&
240 		    cl_data->sensor_sts[i] == SENSOR_ENABLED) {
241 			mp2->mp2_ops->stop(mp2, cl_data->sensor_idx[i]);
242 			status = amd_sfh_wait_for_response
243 					(mp2, cl_data->sensor_idx[i], DISABLE_SENSOR);
244 			if (status == 0)
245 				status = SENSOR_DISABLED;
246 			if (status != SENSOR_ENABLED)
247 				cl_data->sensor_sts[i] = SENSOR_DISABLED;
248 			dev_dbg(&mp2->pdev->dev, "suspend sid 0x%x (%s) status 0x%x\n",
249 				cl_data->sensor_idx[i], get_sensor_name(cl_data->sensor_idx[i]),
250 				cl_data->sensor_sts[i]);
251 		}
252 	}
253 
254 	cancel_delayed_work_sync(&cl_data->work_buffer);
255 	amd_sfh_clear_intr(mp2);
256 }
257 
258 static void amd_mp2_pci_remove(void *privdata)
259 {
260 	struct amd_mp2_dev *mp2 = privdata;
261 
262 	amd_sfh_hid_client_deinit(privdata);
263 	mp2->mp2_ops->stop_all(mp2);
264 	pci_intx(mp2->pdev, false);
265 	amd_sfh_clear_intr(mp2);
266 }
267 
268 static void amd_sfh_set_ops(struct amd_mp2_dev *mp2)
269 {
270 	struct amd_mp2_ops *mp2_ops;
271 
272 	sfh_interface_init(mp2);
273 	mp2_ops = mp2->mp2_ops;
274 	mp2_ops->clear_intr = amd_sfh_clear_intr_v2,
275 	mp2_ops->init_intr = amd_sfh_irq_init_v2,
276 	mp2_ops->suspend = amd_sfh_suspend;
277 	mp2_ops->resume = amd_sfh_resume;
278 	mp2_ops->remove = amd_mp2_pci_remove;
279 }
280 
281 int amd_sfh1_1_init(struct amd_mp2_dev *mp2)
282 {
283 	u32 phy_base = readl(mp2->mmio + AMD_C2P_MSG(22));
284 	struct device *dev = &mp2->pdev->dev;
285 	struct sfh_base_info binfo;
286 	int rc;
287 
288 	phy_base <<= 21;
289 	if (!devm_request_mem_region(dev, phy_base, 128 * 1024, "amd_sfh")) {
290 		dev_dbg(dev, "can't reserve mmio registers\n");
291 		return -ENOMEM;
292 	}
293 
294 	mp2->vsbase = devm_ioremap(dev, phy_base, 128 * 1024);
295 	if (!mp2->vsbase) {
296 		dev_dbg(dev, "failed to remap vsbase\n");
297 		return -ENOMEM;
298 	}
299 
300 	/* Before accessing give time for SFH firmware for processing configuration */
301 	msleep(5000);
302 
303 	memcpy_fromio(&binfo, mp2->vsbase, sizeof(struct sfh_base_info));
304 	if (binfo.sbase.fw_info.fw_ver == 0 || binfo.sbase.s_list.sl.sensors == 0) {
305 		dev_dbg(dev, "failed to get sensors\n");
306 		return -EOPNOTSUPP;
307 	}
308 	dev_dbg(dev, "firmware version 0x%x\n", binfo.sbase.fw_info.fw_ver);
309 
310 	amd_sfh_set_ops(mp2);
311 
312 	rc = amd_sfh_irq_init(mp2);
313 	if (rc) {
314 		dev_err(dev, "amd_sfh_irq_init failed\n");
315 		return rc;
316 	}
317 
318 	rc = amd_sfh1_1_hid_client_init(mp2);
319 	if (rc) {
320 		dev_err(dev, "amd_sfh1_1_hid_client_init failed\n");
321 		return rc;
322 	}
323 
324 	return rc;
325 }
326