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 	if (!cl_data->is_any_sensor_enabled) {
214 		amd_sfh_clear_intr(mp2);
215 		return;
216 	}
217 
218 	for (i = 0; i < cl_data->num_hid_devices; i++) {
219 		if (cl_data->sensor_sts[i] == SENSOR_DISABLED) {
220 			info.sensor_idx = cl_data->sensor_idx[i];
221 			mp2->mp2_ops->start(mp2, info);
222 			status = amd_sfh_wait_for_response
223 					(mp2, cl_data->sensor_idx[i], ENABLE_SENSOR);
224 			if (status == 0)
225 				status = SENSOR_ENABLED;
226 			if (status == SENSOR_ENABLED)
227 				cl_data->sensor_sts[i] = SENSOR_ENABLED;
228 			dev_dbg(&mp2->pdev->dev, "resume sid 0x%x (%s) status 0x%x\n",
229 				cl_data->sensor_idx[i], get_sensor_name(cl_data->sensor_idx[i]),
230 				cl_data->sensor_sts[i]);
231 		}
232 	}
233 
234 	schedule_delayed_work(&cl_data->work_buffer, msecs_to_jiffies(AMD_SFH_IDLE_LOOP));
235 	amd_sfh_clear_intr(mp2);
236 }
237 
238 static void amd_sfh_suspend(struct amd_mp2_dev *mp2)
239 {
240 	struct amdtp_cl_data *cl_data = mp2->cl_data;
241 	int i, status;
242 
243 	if (!cl_data->is_any_sensor_enabled) {
244 		amd_sfh_clear_intr(mp2);
245 		return;
246 	}
247 
248 	for (i = 0; i < cl_data->num_hid_devices; i++) {
249 		if (cl_data->sensor_idx[i] != HPD_IDX &&
250 		    cl_data->sensor_sts[i] == SENSOR_ENABLED) {
251 			mp2->mp2_ops->stop(mp2, cl_data->sensor_idx[i]);
252 			status = amd_sfh_wait_for_response
253 					(mp2, cl_data->sensor_idx[i], DISABLE_SENSOR);
254 			if (status == 0)
255 				status = SENSOR_DISABLED;
256 			if (status != SENSOR_ENABLED)
257 				cl_data->sensor_sts[i] = SENSOR_DISABLED;
258 			dev_dbg(&mp2->pdev->dev, "suspend sid 0x%x (%s) status 0x%x\n",
259 				cl_data->sensor_idx[i], get_sensor_name(cl_data->sensor_idx[i]),
260 				cl_data->sensor_sts[i]);
261 		}
262 	}
263 
264 	cancel_delayed_work_sync(&cl_data->work_buffer);
265 	amd_sfh_clear_intr(mp2);
266 }
267 
268 static void amd_mp2_pci_remove(void *privdata)
269 {
270 	struct amd_mp2_dev *mp2 = privdata;
271 
272 	amd_sfh_hid_client_deinit(privdata);
273 	mp2->mp2_ops->stop_all(mp2);
274 	pci_intx(mp2->pdev, false);
275 	amd_sfh_clear_intr(mp2);
276 }
277 
278 static void amd_sfh_set_ops(struct amd_mp2_dev *mp2)
279 {
280 	struct amd_mp2_ops *mp2_ops;
281 
282 	sfh_interface_init(mp2);
283 	mp2_ops = mp2->mp2_ops;
284 	mp2_ops->clear_intr = amd_sfh_clear_intr_v2,
285 	mp2_ops->init_intr = amd_sfh_irq_init_v2,
286 	mp2_ops->suspend = amd_sfh_suspend;
287 	mp2_ops->resume = amd_sfh_resume;
288 	mp2_ops->remove = amd_mp2_pci_remove;
289 }
290 
291 int amd_sfh1_1_init(struct amd_mp2_dev *mp2)
292 {
293 	u32 phy_base = readl(mp2->mmio + AMD_C2P_MSG(22));
294 	struct device *dev = &mp2->pdev->dev;
295 	struct sfh_base_info binfo;
296 	int rc;
297 
298 	phy_base <<= 21;
299 	if (!devm_request_mem_region(dev, phy_base, 128 * 1024, "amd_sfh")) {
300 		dev_dbg(dev, "can't reserve mmio registers\n");
301 		return -ENOMEM;
302 	}
303 
304 	mp2->vsbase = devm_ioremap(dev, phy_base, 128 * 1024);
305 	if (!mp2->vsbase) {
306 		dev_dbg(dev, "failed to remap vsbase\n");
307 		return -ENOMEM;
308 	}
309 
310 	/* Before accessing give time for SFH firmware for processing configuration */
311 	msleep(5000);
312 
313 	memcpy_fromio(&binfo, mp2->vsbase, sizeof(struct sfh_base_info));
314 	if (binfo.sbase.fw_info.fw_ver == 0 || binfo.sbase.s_list.sl.sensors == 0) {
315 		dev_dbg(dev, "failed to get sensors\n");
316 		return -EOPNOTSUPP;
317 	}
318 	dev_dbg(dev, "firmware version 0x%x\n", binfo.sbase.fw_info.fw_ver);
319 
320 	amd_sfh_set_ops(mp2);
321 
322 	rc = amd_sfh_irq_init(mp2);
323 	if (rc) {
324 		dev_err(dev, "amd_sfh_irq_init failed\n");
325 		return rc;
326 	}
327 
328 	rc = amd_sfh1_1_hid_client_init(mp2);
329 	if (rc) {
330 		dev_err(dev, "amd_sfh1_1_hid_client_init failed\n");
331 		return rc;
332 	}
333 
334 	return rc;
335 }
336