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