1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * AMD MP2 1.1 descriptor interfaces
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/hid-sensor-ids.h>
12 
13 #include "amd_sfh_interface.h"
14 #include "../hid_descriptor/amd_sfh_hid_desc.h"
15 #include "../hid_descriptor/amd_sfh_hid_report_desc.h"
16 
17 #define SENSOR_PROP_REPORTING_STATE_ALL_EVENTS_ENUM			0x41
18 #define SENSOR_PROP_POWER_STATE_D0_FULL_POWER_ENUM			0x51
19 #define HID_DEFAULT_REPORT_INTERVAL					0x50
20 #define HID_DEFAULT_MIN_VALUE						0X7F
21 #define HID_DEFAULT_MAX_VALUE						0x80
22 #define HID_DEFAULT_SENSITIVITY						0x7F
23 #define HID_USAGE_SENSOR_PROPERTY_CONNECTION_TYPE_PC_INTEGRATED_ENUM	0x01
24 /* state enums */
25 #define HID_USAGE_SENSOR_STATE_READY_ENUM				0x02
26 #define HID_USAGE_SENSOR_STATE_INITIALIZING_ENUM			0x05
27 #define HID_USAGE_SENSOR_EVENT_DATA_UPDATED_ENUM			0x04
28 
29 static int get_report_desc(int sensor_idx, u8 *rep_desc)
30 {
31 	switch (sensor_idx) {
32 	case ACCEL_IDX: /* accelerometer */
33 		memset(rep_desc, 0, sizeof(accel3_report_descriptor));
34 		memcpy(rep_desc, accel3_report_descriptor,
35 		       sizeof(accel3_report_descriptor));
36 		break;
37 	case GYRO_IDX: /* gyroscope */
38 		memset(rep_desc, 0, sizeof(gyro3_report_descriptor));
39 		memcpy(rep_desc, gyro3_report_descriptor,
40 		       sizeof(gyro3_report_descriptor));
41 		break;
42 	case MAG_IDX: /* magnetometer */
43 		memset(rep_desc, 0, sizeof(comp3_report_descriptor));
44 		memcpy(rep_desc, comp3_report_descriptor,
45 		       sizeof(comp3_report_descriptor));
46 		break;
47 	case ALS_IDX: /* ambient light sensor */
48 		memset(rep_desc, 0, sizeof(als_report_descriptor));
49 		memcpy(rep_desc, als_report_descriptor,
50 		       sizeof(als_report_descriptor));
51 		break;
52 	case HPD_IDX: /* HPD sensor */
53 		memset(rep_desc, 0, sizeof(hpd_report_descriptor));
54 		memcpy(rep_desc, hpd_report_descriptor,
55 		       sizeof(hpd_report_descriptor));
56 		break;
57 	}
58 	return 0;
59 }
60 
61 static void get_common_features(struct common_feature_property *common, int report_id)
62 {
63 	common->report_id = report_id;
64 	common->connection_type = HID_USAGE_SENSOR_PROPERTY_CONNECTION_TYPE_PC_INTEGRATED_ENUM;
65 	common->report_state = SENSOR_PROP_REPORTING_STATE_ALL_EVENTS_ENUM;
66 	common->power_state = SENSOR_PROP_POWER_STATE_D0_FULL_POWER_ENUM;
67 	common->sensor_state = HID_USAGE_SENSOR_STATE_INITIALIZING_ENUM;
68 	common->report_interval =  HID_DEFAULT_REPORT_INTERVAL;
69 }
70 
71 static u8 get_feature_rep(int sensor_idx, int report_id, u8 *feature_report)
72 {
73 	struct magno_feature_report magno_feature;
74 	struct accel3_feature_report acc_feature;
75 	struct gyro_feature_report gyro_feature;
76 	struct hpd_feature_report hpd_feature;
77 	struct als_feature_report als_feature;
78 	u8 report_size = 0;
79 
80 	if (!feature_report)
81 		return report_size;
82 
83 	switch (sensor_idx) {
84 	case ACCEL_IDX: /* accelerometer */
85 		get_common_features(&acc_feature.common_property, report_id);
86 		acc_feature.accel_change_sesnitivity = HID_DEFAULT_SENSITIVITY;
87 		acc_feature.accel_sensitivity_min = HID_DEFAULT_MIN_VALUE;
88 		acc_feature.accel_sensitivity_max = HID_DEFAULT_MAX_VALUE;
89 		memcpy(feature_report, &acc_feature, sizeof(acc_feature));
90 		report_size = sizeof(acc_feature);
91 		break;
92 	case GYRO_IDX: /* gyroscope */
93 		get_common_features(&gyro_feature.common_property, report_id);
94 		gyro_feature.gyro_change_sesnitivity = HID_DEFAULT_SENSITIVITY;
95 		gyro_feature.gyro_sensitivity_min = HID_DEFAULT_MIN_VALUE;
96 		gyro_feature.gyro_sensitivity_max = HID_DEFAULT_MAX_VALUE;
97 		memcpy(feature_report, &gyro_feature, sizeof(gyro_feature));
98 		report_size = sizeof(gyro_feature);
99 		break;
100 	case MAG_IDX: /* magnetometer */
101 		get_common_features(&magno_feature.common_property, report_id);
102 		magno_feature.magno_headingchange_sensitivity = HID_DEFAULT_SENSITIVITY;
103 		magno_feature.heading_min = HID_DEFAULT_MIN_VALUE;
104 		magno_feature.heading_max = HID_DEFAULT_MAX_VALUE;
105 		magno_feature.flux_change_sensitivity = HID_DEFAULT_MIN_VALUE;
106 		magno_feature.flux_min = HID_DEFAULT_MIN_VALUE;
107 		magno_feature.flux_max = HID_DEFAULT_MAX_VALUE;
108 		memcpy(feature_report, &magno_feature, sizeof(magno_feature));
109 		report_size = sizeof(magno_feature);
110 		break;
111 	case ALS_IDX:  /* ambient light sensor */
112 		get_common_features(&als_feature.common_property, report_id);
113 		als_feature.als_change_sesnitivity = HID_DEFAULT_SENSITIVITY;
114 		als_feature.als_sensitivity_min = HID_DEFAULT_MIN_VALUE;
115 		als_feature.als_sensitivity_max = HID_DEFAULT_MAX_VALUE;
116 		memcpy(feature_report, &als_feature, sizeof(als_feature));
117 		report_size = sizeof(als_feature);
118 		break;
119 	case HPD_IDX:  /* human presence detection sensor */
120 		get_common_features(&hpd_feature.common_property, report_id);
121 		memcpy(feature_report, &hpd_feature, sizeof(hpd_feature));
122 		report_size = sizeof(hpd_feature);
123 		break;
124 	}
125 	return report_size;
126 }
127 
128 static void get_common_inputs(struct common_input_property *common, int report_id)
129 {
130 	common->report_id = report_id;
131 	common->sensor_state = HID_USAGE_SENSOR_STATE_READY_ENUM;
132 	common->event_type = HID_USAGE_SENSOR_EVENT_DATA_UPDATED_ENUM;
133 }
134 
135 static int float_to_int(u32 flt32_val)
136 {
137 	int fraction, shift, mantissa, sign, exp, zeropre;
138 
139 	mantissa = flt32_val & GENMASK(22, 0);
140 	sign = (flt32_val & BIT(31)) ? -1 : 1;
141 	exp = (flt32_val & ~BIT(31)) >> 23;
142 
143 	if (!exp && !mantissa)
144 		return 0;
145 
146 	/*
147 	 * Calculate the exponent and fraction part of floating
148 	 * point representation.
149 	 */
150 	exp -= 127;
151 	if (exp < 0) {
152 		exp = -exp;
153 		if (exp >= BITS_PER_TYPE(u32))
154 			return 0;
155 		zeropre = (((BIT(23) + mantissa) * 100) >> 23) >> exp;
156 		return zeropre >= 50 ? sign : 0;
157 	}
158 
159 	shift = 23 - exp;
160 	if (abs(shift) >= BITS_PER_TYPE(u32))
161 		return 0;
162 
163 	if (shift < 0) {
164 		shift = -shift;
165 		flt32_val = BIT(exp) + (mantissa << shift);
166 		shift = 0;
167 	} else {
168 		flt32_val = BIT(exp) + (mantissa >> shift);
169 	}
170 
171 	fraction = (shift == 0) ? 0 : mantissa & GENMASK(shift - 1, 0);
172 
173 	return (((fraction * 100) >> shift) >= 50) ? sign * (flt32_val + 1) : sign * flt32_val;
174 }
175 
176 static u8 get_input_rep(u8 current_index, int sensor_idx, int report_id,
177 			struct amd_input_data *in_data)
178 {
179 	struct amd_mp2_dev *mp2 = container_of(in_data, struct amd_mp2_dev, in_data);
180 	u8 *input_report = in_data->input_report[current_index];
181 	struct magno_input_report magno_input;
182 	struct accel3_input_report acc_input;
183 	struct gyro_input_report gyro_input;
184 	struct als_input_report als_input;
185 	struct hpd_input_report hpd_input;
186 	struct sfh_accel_data accel_data;
187 	struct sfh_gyro_data gyro_data;
188 	struct sfh_mag_data mag_data;
189 	struct sfh_als_data als_data;
190 	struct hpd_status hpdstatus;
191 	void __iomem *sensoraddr;
192 	u8 report_size = 0;
193 
194 	if (!input_report)
195 		return report_size;
196 
197 	switch (sensor_idx) {
198 	case ACCEL_IDX: /* accelerometer */
199 		sensoraddr = mp2->vsbase + (ACCEL_IDX * SENSOR_DATA_MEM_SIZE_DEFAULT) +
200 			     OFFSET_SENSOR_DATA_DEFAULT;
201 		memcpy_fromio(&accel_data, sensoraddr, sizeof(struct sfh_accel_data));
202 		get_common_inputs(&acc_input.common_property, report_id);
203 		acc_input.in_accel_x_value = float_to_int(accel_data.acceldata.x) / 100;
204 		acc_input.in_accel_y_value = float_to_int(accel_data.acceldata.y) / 100;
205 		acc_input.in_accel_z_value = float_to_int(accel_data.acceldata.z) / 100;
206 		memcpy(input_report, &acc_input, sizeof(acc_input));
207 		report_size = sizeof(acc_input);
208 		break;
209 	case GYRO_IDX: /* gyroscope */
210 		sensoraddr = mp2->vsbase + (GYRO_IDX * SENSOR_DATA_MEM_SIZE_DEFAULT) +
211 			     OFFSET_SENSOR_DATA_DEFAULT;
212 		memcpy_fromio(&gyro_data, sensoraddr, sizeof(struct sfh_gyro_data));
213 		get_common_inputs(&gyro_input.common_property, report_id);
214 		gyro_input.in_angel_x_value = float_to_int(gyro_data.gyrodata.x) / 1000;
215 		gyro_input.in_angel_y_value = float_to_int(gyro_data.gyrodata.y) / 1000;
216 		gyro_input.in_angel_z_value = float_to_int(gyro_data.gyrodata.z) / 1000;
217 		memcpy(input_report, &gyro_input, sizeof(gyro_input));
218 		report_size = sizeof(gyro_input);
219 		break;
220 	case MAG_IDX: /* magnetometer */
221 		sensoraddr = mp2->vsbase + (MAG_IDX * SENSOR_DATA_MEM_SIZE_DEFAULT) +
222 			     OFFSET_SENSOR_DATA_DEFAULT;
223 		memcpy_fromio(&mag_data, sensoraddr, sizeof(struct sfh_mag_data));
224 		get_common_inputs(&magno_input.common_property, report_id);
225 		magno_input.in_magno_x = float_to_int(mag_data.magdata.x) / 100;
226 		magno_input.in_magno_y = float_to_int(mag_data.magdata.y) / 100;
227 		magno_input.in_magno_z = float_to_int(mag_data.magdata.z) / 100;
228 		magno_input.in_magno_accuracy = mag_data.accuracy / 100;
229 		memcpy(input_report, &magno_input, sizeof(magno_input));
230 		report_size = sizeof(magno_input);
231 		break;
232 	case ALS_IDX:
233 		sensoraddr = mp2->vsbase + (ALS_IDX * SENSOR_DATA_MEM_SIZE_DEFAULT) +
234 			     OFFSET_SENSOR_DATA_DEFAULT;
235 		memcpy_fromio(&als_data, sensoraddr, sizeof(struct sfh_als_data));
236 		get_common_inputs(&als_input.common_property, report_id);
237 		als_input.illuminance_value = float_to_int(als_data.lux);
238 		report_size = sizeof(als_input);
239 		memcpy(input_report, &als_input, sizeof(als_input));
240 		break;
241 	case HPD_IDX:
242 		get_common_inputs(&hpd_input.common_property, report_id);
243 		hpdstatus.val = readl(mp2->mmio + AMD_C2P_MSG(4));
244 		hpd_input.human_presence = hpdstatus.shpd.presence;
245 		report_size = sizeof(hpd_input);
246 		memcpy(input_report, &hpd_input, sizeof(hpd_input));
247 		break;
248 	}
249 	return report_size;
250 }
251 
252 static u32 get_desc_size(int sensor_idx, int descriptor_name)
253 {
254 	switch (sensor_idx) {
255 	case ACCEL_IDX:
256 		switch (descriptor_name) {
257 		case descr_size:
258 			return sizeof(accel3_report_descriptor);
259 		case input_size:
260 			return sizeof(struct accel3_input_report);
261 		case feature_size:
262 			return sizeof(struct accel3_feature_report);
263 		}
264 		break;
265 	case GYRO_IDX:
266 		switch (descriptor_name) {
267 		case descr_size:
268 			return sizeof(gyro3_report_descriptor);
269 		case input_size:
270 			return sizeof(struct gyro_input_report);
271 		case feature_size:
272 			return sizeof(struct gyro_feature_report);
273 		}
274 		break;
275 	case MAG_IDX:
276 		switch (descriptor_name) {
277 		case descr_size:
278 			return sizeof(comp3_report_descriptor);
279 		case input_size:
280 			return sizeof(struct magno_input_report);
281 		case feature_size:
282 			return sizeof(struct magno_feature_report);
283 		}
284 		break;
285 	case ALS_IDX:
286 		switch (descriptor_name) {
287 		case descr_size:
288 			return sizeof(als_report_descriptor);
289 		case input_size:
290 			return sizeof(struct als_input_report);
291 		case feature_size:
292 			return sizeof(struct als_feature_report);
293 		}
294 		break;
295 	case HPD_IDX:
296 		switch (descriptor_name) {
297 		case descr_size:
298 			return sizeof(hpd_report_descriptor);
299 		case input_size:
300 			return sizeof(struct hpd_input_report);
301 		case feature_size:
302 			return sizeof(struct hpd_feature_report);
303 		}
304 		break;
305 	}
306 
307 	return 0;
308 }
309 
310 void amd_sfh1_1_set_desc_ops(struct amd_mp2_ops *mp2_ops)
311 {
312 	mp2_ops->get_rep_desc = get_report_desc;
313 	mp2_ops->get_feat_rep = get_feature_rep;
314 	mp2_ops->get_desc_sz = get_desc_size;
315 	mp2_ops->get_in_rep = get_input_rep;
316 }
317