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