xref: /linux/drivers/input/rmi4/rmi_f01.c (revision 4b660dbd9ee2059850fd30e0df420ca7a38a1856)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2011-2016 Synaptics Incorporated
4  * Copyright (c) 2011 Unixphere
5  */
6 
7 #include <linux/kernel.h>
8 #include <linux/rmi.h>
9 #include <linux/slab.h>
10 #include <linux/uaccess.h>
11 #include <linux/of.h>
12 #include <asm/unaligned.h>
13 #include "rmi_driver.h"
14 
15 #define RMI_PRODUCT_ID_LENGTH    10
16 #define RMI_PRODUCT_INFO_LENGTH   2
17 
18 #define RMI_DATE_CODE_LENGTH      3
19 
20 #define PRODUCT_ID_OFFSET 0x10
21 #define PRODUCT_INFO_OFFSET 0x1E
22 
23 
24 /* Force a firmware reset of the sensor */
25 #define RMI_F01_CMD_DEVICE_RESET	1
26 
27 /* Various F01_RMI_QueryX bits */
28 
29 #define RMI_F01_QRY1_CUSTOM_MAP		BIT(0)
30 #define RMI_F01_QRY1_NON_COMPLIANT	BIT(1)
31 #define RMI_F01_QRY1_HAS_LTS		BIT(2)
32 #define RMI_F01_QRY1_HAS_SENSOR_ID	BIT(3)
33 #define RMI_F01_QRY1_HAS_CHARGER_INP	BIT(4)
34 #define RMI_F01_QRY1_HAS_ADJ_DOZE	BIT(5)
35 #define RMI_F01_QRY1_HAS_ADJ_DOZE_HOFF	BIT(6)
36 #define RMI_F01_QRY1_HAS_QUERY42	BIT(7)
37 
38 #define RMI_F01_QRY5_YEAR_MASK		0x1f
39 #define RMI_F01_QRY6_MONTH_MASK		0x0f
40 #define RMI_F01_QRY7_DAY_MASK		0x1f
41 
42 #define RMI_F01_QRY2_PRODINFO_MASK	0x7f
43 
44 #define RMI_F01_BASIC_QUERY_LEN		21 /* From Query 00 through 20 */
45 
46 struct f01_basic_properties {
47 	u8 manufacturer_id;
48 	bool has_lts;
49 	bool has_adjustable_doze;
50 	bool has_adjustable_doze_holdoff;
51 	char dom[11]; /* YYYY/MM/DD + '\0' */
52 	u8 product_id[RMI_PRODUCT_ID_LENGTH + 1];
53 	u16 productinfo;
54 	u32 firmware_id;
55 	u32 package_id;
56 };
57 
58 /* F01 device status bits */
59 
60 /* Most recent device status event */
61 #define RMI_F01_STATUS_CODE(status)		((status) & 0x0f)
62 /* The device has lost its configuration for some reason. */
63 #define RMI_F01_STATUS_UNCONFIGURED(status)	(!!((status) & 0x80))
64 /* The device is in bootloader mode */
65 #define RMI_F01_STATUS_BOOTLOADER(status)	((status) & 0x40)
66 
67 /* Control register bits */
68 
69 /*
70  * Sleep mode controls power management on the device and affects all
71  * functions of the device.
72  */
73 #define RMI_F01_CTRL0_SLEEP_MODE_MASK	0x03
74 
75 #define RMI_SLEEP_MODE_NORMAL		0x00
76 #define RMI_SLEEP_MODE_SENSOR_SLEEP	0x01
77 #define RMI_SLEEP_MODE_RESERVED0	0x02
78 #define RMI_SLEEP_MODE_RESERVED1	0x03
79 
80 /*
81  * This bit disables whatever sleep mode may be selected by the sleep_mode
82  * field and forces the device to run at full power without sleeping.
83  */
84 #define RMI_F01_CTRL0_NOSLEEP_BIT	BIT(2)
85 
86 /*
87  * When this bit is set, the touch controller employs a noise-filtering
88  * algorithm designed for use with a connected battery charger.
89  */
90 #define RMI_F01_CTRL0_CHARGER_BIT	BIT(5)
91 
92 /*
93  * Sets the report rate for the device. The effect of this setting is
94  * highly product dependent. Check the spec sheet for your particular
95  * touch sensor.
96  */
97 #define RMI_F01_CTRL0_REPORTRATE_BIT	BIT(6)
98 
99 /*
100  * Written by the host as an indicator that the device has been
101  * successfully configured.
102  */
103 #define RMI_F01_CTRL0_CONFIGURED_BIT	BIT(7)
104 
105 /**
106  * struct f01_device_control - controls basic sensor functions
107  *
108  * @ctrl0: see the bit definitions above.
109  * @doze_interval: controls the interval between checks for finger presence
110  *	when the touch sensor is in doze mode, in units of 10ms.
111  * @wakeup_threshold: controls the capacitance threshold at which the touch
112  *	sensor will decide to wake up from that low power state.
113  * @doze_holdoff: controls how long the touch sensor waits after the last
114  *	finger lifts before entering the doze state, in units of 100ms.
115  */
116 struct f01_device_control {
117 	u8 ctrl0;
118 	u8 doze_interval;
119 	u8 wakeup_threshold;
120 	u8 doze_holdoff;
121 };
122 
123 struct f01_data {
124 	struct f01_basic_properties properties;
125 	struct f01_device_control device_control;
126 
127 	u16 doze_interval_addr;
128 	u16 wakeup_threshold_addr;
129 	u16 doze_holdoff_addr;
130 
131 	bool suspended;
132 	bool old_nosleep;
133 
134 	unsigned int num_of_irq_regs;
135 };
136 
137 static int rmi_f01_read_properties(struct rmi_device *rmi_dev,
138 				   u16 query_base_addr,
139 				   struct f01_basic_properties *props)
140 {
141 	u8 queries[RMI_F01_BASIC_QUERY_LEN];
142 	int ret;
143 	int query_offset = query_base_addr;
144 	bool has_ds4_queries = false;
145 	bool has_query42 = false;
146 	bool has_sensor_id = false;
147 	bool has_package_id_query = false;
148 	bool has_build_id_query = false;
149 	u16 prod_info_addr;
150 	u8 ds4_query_len;
151 
152 	ret = rmi_read_block(rmi_dev, query_offset,
153 			       queries, RMI_F01_BASIC_QUERY_LEN);
154 	if (ret) {
155 		dev_err(&rmi_dev->dev,
156 			"Failed to read device query registers: %d\n", ret);
157 		return ret;
158 	}
159 
160 	prod_info_addr = query_offset + 17;
161 	query_offset += RMI_F01_BASIC_QUERY_LEN;
162 
163 	/* Now parse what we got */
164 	props->manufacturer_id = queries[0];
165 
166 	props->has_lts = queries[1] & RMI_F01_QRY1_HAS_LTS;
167 	props->has_adjustable_doze =
168 			queries[1] & RMI_F01_QRY1_HAS_ADJ_DOZE;
169 	props->has_adjustable_doze_holdoff =
170 			queries[1] & RMI_F01_QRY1_HAS_ADJ_DOZE_HOFF;
171 	has_query42 = queries[1] & RMI_F01_QRY1_HAS_QUERY42;
172 	has_sensor_id = queries[1] & RMI_F01_QRY1_HAS_SENSOR_ID;
173 
174 	snprintf(props->dom, sizeof(props->dom), "20%02d/%02d/%02d",
175 		 queries[5] & RMI_F01_QRY5_YEAR_MASK,
176 		 queries[6] & RMI_F01_QRY6_MONTH_MASK,
177 		 queries[7] & RMI_F01_QRY7_DAY_MASK);
178 
179 	memcpy(props->product_id, &queries[11],
180 		RMI_PRODUCT_ID_LENGTH);
181 	props->product_id[RMI_PRODUCT_ID_LENGTH] = '\0';
182 
183 	props->productinfo =
184 			((queries[2] & RMI_F01_QRY2_PRODINFO_MASK) << 7) |
185 			(queries[3] & RMI_F01_QRY2_PRODINFO_MASK);
186 
187 	if (has_sensor_id)
188 		query_offset++;
189 
190 	if (has_query42) {
191 		ret = rmi_read(rmi_dev, query_offset, queries);
192 		if (ret) {
193 			dev_err(&rmi_dev->dev,
194 				"Failed to read query 42 register: %d\n", ret);
195 			return ret;
196 		}
197 
198 		has_ds4_queries = !!(queries[0] & BIT(0));
199 		query_offset++;
200 	}
201 
202 	if (has_ds4_queries) {
203 		ret = rmi_read(rmi_dev, query_offset, &ds4_query_len);
204 		if (ret) {
205 			dev_err(&rmi_dev->dev,
206 				"Failed to read DS4 queries length: %d\n", ret);
207 			return ret;
208 		}
209 		query_offset++;
210 
211 		if (ds4_query_len > 0) {
212 			ret = rmi_read(rmi_dev, query_offset, queries);
213 			if (ret) {
214 				dev_err(&rmi_dev->dev,
215 					"Failed to read DS4 queries: %d\n",
216 					ret);
217 				return ret;
218 			}
219 
220 			has_package_id_query = !!(queries[0] & BIT(0));
221 			has_build_id_query = !!(queries[0] & BIT(1));
222 		}
223 
224 		if (has_package_id_query) {
225 			ret = rmi_read_block(rmi_dev, prod_info_addr,
226 					     queries, sizeof(__le64));
227 			if (ret) {
228 				dev_err(&rmi_dev->dev,
229 					"Failed to read package info: %d\n",
230 					ret);
231 				return ret;
232 			}
233 
234 			props->package_id = get_unaligned_le64(queries);
235 			prod_info_addr++;
236 		}
237 
238 		if (has_build_id_query) {
239 			ret = rmi_read_block(rmi_dev, prod_info_addr, queries,
240 					    3);
241 			if (ret) {
242 				dev_err(&rmi_dev->dev,
243 					"Failed to read product info: %d\n",
244 					ret);
245 				return ret;
246 			}
247 
248 			props->firmware_id = queries[1] << 8 | queries[0];
249 			props->firmware_id += queries[2] * 65536;
250 		}
251 	}
252 
253 	return 0;
254 }
255 
256 const char *rmi_f01_get_product_ID(struct rmi_function *fn)
257 {
258 	struct f01_data *f01 = dev_get_drvdata(&fn->dev);
259 
260 	return f01->properties.product_id;
261 }
262 
263 static ssize_t rmi_driver_manufacturer_id_show(struct device *dev,
264 					       struct device_attribute *dattr,
265 					       char *buf)
266 {
267 	struct rmi_driver_data *data = dev_get_drvdata(dev);
268 	struct f01_data *f01 = dev_get_drvdata(&data->f01_container->dev);
269 
270 	return sysfs_emit(buf, "%d\n", f01->properties.manufacturer_id);
271 }
272 
273 static DEVICE_ATTR(manufacturer_id, 0444,
274 		   rmi_driver_manufacturer_id_show, NULL);
275 
276 static ssize_t rmi_driver_dom_show(struct device *dev,
277 				   struct device_attribute *dattr, char *buf)
278 {
279 	struct rmi_driver_data *data = dev_get_drvdata(dev);
280 	struct f01_data *f01 = dev_get_drvdata(&data->f01_container->dev);
281 
282 	return sysfs_emit(buf, "%s\n", f01->properties.dom);
283 }
284 
285 static DEVICE_ATTR(date_of_manufacture, 0444, rmi_driver_dom_show, NULL);
286 
287 static ssize_t rmi_driver_product_id_show(struct device *dev,
288 					  struct device_attribute *dattr,
289 					  char *buf)
290 {
291 	struct rmi_driver_data *data = dev_get_drvdata(dev);
292 	struct f01_data *f01 = dev_get_drvdata(&data->f01_container->dev);
293 
294 	return sysfs_emit(buf, "%s\n", f01->properties.product_id);
295 }
296 
297 static DEVICE_ATTR(product_id, 0444, rmi_driver_product_id_show, NULL);
298 
299 static ssize_t rmi_driver_firmware_id_show(struct device *dev,
300 					   struct device_attribute *dattr,
301 					   char *buf)
302 {
303 	struct rmi_driver_data *data = dev_get_drvdata(dev);
304 	struct f01_data *f01 = dev_get_drvdata(&data->f01_container->dev);
305 
306 	return sysfs_emit(buf, "%d\n", f01->properties.firmware_id);
307 }
308 
309 static DEVICE_ATTR(firmware_id, 0444, rmi_driver_firmware_id_show, NULL);
310 
311 static ssize_t rmi_driver_package_id_show(struct device *dev,
312 					  struct device_attribute *dattr,
313 					  char *buf)
314 {
315 	struct rmi_driver_data *data = dev_get_drvdata(dev);
316 	struct f01_data *f01 = dev_get_drvdata(&data->f01_container->dev);
317 
318 	u32 package_id = f01->properties.package_id;
319 
320 	return sysfs_emit(buf, "%04x.%04x\n",
321 			  package_id & 0xffff, (package_id >> 16) & 0xffff);
322 }
323 
324 static DEVICE_ATTR(package_id, 0444, rmi_driver_package_id_show, NULL);
325 
326 static struct attribute *rmi_f01_attrs[] = {
327 	&dev_attr_manufacturer_id.attr,
328 	&dev_attr_date_of_manufacture.attr,
329 	&dev_attr_product_id.attr,
330 	&dev_attr_firmware_id.attr,
331 	&dev_attr_package_id.attr,
332 	NULL
333 };
334 
335 static const struct attribute_group rmi_f01_attr_group = {
336 	.attrs = rmi_f01_attrs,
337 };
338 
339 #ifdef CONFIG_OF
340 static int rmi_f01_of_probe(struct device *dev,
341 				struct rmi_device_platform_data *pdata)
342 {
343 	int retval;
344 	u32 val;
345 
346 	retval = rmi_of_property_read_u32(dev,
347 			(u32 *)&pdata->power_management.nosleep,
348 			"syna,nosleep-mode", 1);
349 	if (retval)
350 		return retval;
351 
352 	retval = rmi_of_property_read_u32(dev, &val,
353 			"syna,wakeup-threshold", 1);
354 	if (retval)
355 		return retval;
356 
357 	pdata->power_management.wakeup_threshold = val;
358 
359 	retval = rmi_of_property_read_u32(dev, &val,
360 			"syna,doze-holdoff-ms", 1);
361 	if (retval)
362 		return retval;
363 
364 	pdata->power_management.doze_holdoff = val * 100;
365 
366 	retval = rmi_of_property_read_u32(dev, &val,
367 			"syna,doze-interval-ms", 1);
368 	if (retval)
369 		return retval;
370 
371 	pdata->power_management.doze_interval = val / 10;
372 
373 	return 0;
374 }
375 #else
376 static inline int rmi_f01_of_probe(struct device *dev,
377 					struct rmi_device_platform_data *pdata)
378 {
379 	return -ENODEV;
380 }
381 #endif
382 
383 static int rmi_f01_probe(struct rmi_function *fn)
384 {
385 	struct rmi_device *rmi_dev = fn->rmi_dev;
386 	struct rmi_driver_data *driver_data = dev_get_drvdata(&rmi_dev->dev);
387 	struct rmi_device_platform_data *pdata = rmi_get_platform_data(rmi_dev);
388 	struct f01_data *f01;
389 	int error;
390 	u16 ctrl_base_addr = fn->fd.control_base_addr;
391 	u8 device_status;
392 	u8 temp;
393 
394 	if (fn->dev.of_node) {
395 		error = rmi_f01_of_probe(&fn->dev, pdata);
396 		if (error)
397 			return error;
398 	}
399 
400 	f01 = devm_kzalloc(&fn->dev, sizeof(struct f01_data), GFP_KERNEL);
401 	if (!f01)
402 		return -ENOMEM;
403 
404 	f01->num_of_irq_regs = driver_data->num_of_irq_regs;
405 
406 	/*
407 	 * Set the configured bit and (optionally) other important stuff
408 	 * in the device control register.
409 	 */
410 
411 	error = rmi_read(rmi_dev, fn->fd.control_base_addr,
412 			 &f01->device_control.ctrl0);
413 	if (error) {
414 		dev_err(&fn->dev, "Failed to read F01 control: %d\n", error);
415 		return error;
416 	}
417 
418 	switch (pdata->power_management.nosleep) {
419 	case RMI_REG_STATE_DEFAULT:
420 		break;
421 	case RMI_REG_STATE_OFF:
422 		f01->device_control.ctrl0 &= ~RMI_F01_CTRL0_NOSLEEP_BIT;
423 		break;
424 	case RMI_REG_STATE_ON:
425 		f01->device_control.ctrl0 |= RMI_F01_CTRL0_NOSLEEP_BIT;
426 		break;
427 	}
428 
429 	/*
430 	 * Sleep mode might be set as a hangover from a system crash or
431 	 * reboot without power cycle.  If so, clear it so the sensor
432 	 * is certain to function.
433 	 */
434 	if ((f01->device_control.ctrl0 & RMI_F01_CTRL0_SLEEP_MODE_MASK) !=
435 			RMI_SLEEP_MODE_NORMAL) {
436 		dev_warn(&fn->dev,
437 			 "WARNING: Non-zero sleep mode found. Clearing...\n");
438 		f01->device_control.ctrl0 &= ~RMI_F01_CTRL0_SLEEP_MODE_MASK;
439 	}
440 
441 	f01->device_control.ctrl0 |= RMI_F01_CTRL0_CONFIGURED_BIT;
442 
443 	error = rmi_write(rmi_dev, fn->fd.control_base_addr,
444 			  f01->device_control.ctrl0);
445 	if (error) {
446 		dev_err(&fn->dev, "Failed to write F01 control: %d\n", error);
447 		return error;
448 	}
449 
450 	/* Dummy read in order to clear irqs */
451 	error = rmi_read(rmi_dev, fn->fd.data_base_addr + 1, &temp);
452 	if (error < 0) {
453 		dev_err(&fn->dev, "Failed to read Interrupt Status.\n");
454 		return error;
455 	}
456 
457 	error = rmi_f01_read_properties(rmi_dev, fn->fd.query_base_addr,
458 					&f01->properties);
459 	if (error < 0) {
460 		dev_err(&fn->dev, "Failed to read F01 properties.\n");
461 		return error;
462 	}
463 
464 	dev_info(&fn->dev, "found RMI device, manufacturer: %s, product: %s, fw id: %d\n",
465 		 f01->properties.manufacturer_id == 1 ? "Synaptics" : "unknown",
466 		 f01->properties.product_id, f01->properties.firmware_id);
467 
468 	/* Advance to interrupt control registers, then skip over them. */
469 	ctrl_base_addr++;
470 	ctrl_base_addr += f01->num_of_irq_regs;
471 
472 	/* read control register */
473 	if (f01->properties.has_adjustable_doze) {
474 		f01->doze_interval_addr = ctrl_base_addr;
475 		ctrl_base_addr++;
476 
477 		if (pdata->power_management.doze_interval) {
478 			f01->device_control.doze_interval =
479 				pdata->power_management.doze_interval;
480 			error = rmi_write(rmi_dev, f01->doze_interval_addr,
481 					  f01->device_control.doze_interval);
482 			if (error) {
483 				dev_err(&fn->dev,
484 					"Failed to configure F01 doze interval register: %d\n",
485 					error);
486 				return error;
487 			}
488 		} else {
489 			error = rmi_read(rmi_dev, f01->doze_interval_addr,
490 					 &f01->device_control.doze_interval);
491 			if (error) {
492 				dev_err(&fn->dev,
493 					"Failed to read F01 doze interval register: %d\n",
494 					error);
495 				return error;
496 			}
497 		}
498 
499 		f01->wakeup_threshold_addr = ctrl_base_addr;
500 		ctrl_base_addr++;
501 
502 		if (pdata->power_management.wakeup_threshold) {
503 			f01->device_control.wakeup_threshold =
504 				pdata->power_management.wakeup_threshold;
505 			error = rmi_write(rmi_dev, f01->wakeup_threshold_addr,
506 					  f01->device_control.wakeup_threshold);
507 			if (error) {
508 				dev_err(&fn->dev,
509 					"Failed to configure F01 wakeup threshold register: %d\n",
510 					error);
511 				return error;
512 			}
513 		} else {
514 			error = rmi_read(rmi_dev, f01->wakeup_threshold_addr,
515 					 &f01->device_control.wakeup_threshold);
516 			if (error < 0) {
517 				dev_err(&fn->dev,
518 					"Failed to read F01 wakeup threshold register: %d\n",
519 					error);
520 				return error;
521 			}
522 		}
523 	}
524 
525 	if (f01->properties.has_lts)
526 		ctrl_base_addr++;
527 
528 	if (f01->properties.has_adjustable_doze_holdoff) {
529 		f01->doze_holdoff_addr = ctrl_base_addr;
530 		ctrl_base_addr++;
531 
532 		if (pdata->power_management.doze_holdoff) {
533 			f01->device_control.doze_holdoff =
534 				pdata->power_management.doze_holdoff;
535 			error = rmi_write(rmi_dev, f01->doze_holdoff_addr,
536 					  f01->device_control.doze_holdoff);
537 			if (error) {
538 				dev_err(&fn->dev,
539 					"Failed to configure F01 doze holdoff register: %d\n",
540 					error);
541 				return error;
542 			}
543 		} else {
544 			error = rmi_read(rmi_dev, f01->doze_holdoff_addr,
545 					 &f01->device_control.doze_holdoff);
546 			if (error) {
547 				dev_err(&fn->dev,
548 					"Failed to read F01 doze holdoff register: %d\n",
549 					error);
550 				return error;
551 			}
552 		}
553 	}
554 
555 	error = rmi_read(rmi_dev, fn->fd.data_base_addr, &device_status);
556 	if (error < 0) {
557 		dev_err(&fn->dev,
558 			"Failed to read device status: %d\n", error);
559 		return error;
560 	}
561 
562 	if (RMI_F01_STATUS_UNCONFIGURED(device_status)) {
563 		dev_err(&fn->dev,
564 			"Device was reset during configuration process, status: %#02x!\n",
565 			RMI_F01_STATUS_CODE(device_status));
566 		return -EINVAL;
567 	}
568 
569 	dev_set_drvdata(&fn->dev, f01);
570 
571 	error = sysfs_create_group(&fn->rmi_dev->dev.kobj, &rmi_f01_attr_group);
572 	if (error)
573 		dev_warn(&fn->dev, "Failed to create sysfs group: %d\n", error);
574 
575 	return 0;
576 }
577 
578 static void rmi_f01_remove(struct rmi_function *fn)
579 {
580 	/* Note that the bus device is used, not the F01 device */
581 	sysfs_remove_group(&fn->rmi_dev->dev.kobj, &rmi_f01_attr_group);
582 }
583 
584 static int rmi_f01_config(struct rmi_function *fn)
585 {
586 	struct f01_data *f01 = dev_get_drvdata(&fn->dev);
587 	int error;
588 
589 	error = rmi_write(fn->rmi_dev, fn->fd.control_base_addr,
590 			  f01->device_control.ctrl0);
591 	if (error) {
592 		dev_err(&fn->dev,
593 			"Failed to write device_control register: %d\n", error);
594 		return error;
595 	}
596 
597 	if (f01->properties.has_adjustable_doze) {
598 		error = rmi_write(fn->rmi_dev, f01->doze_interval_addr,
599 				  f01->device_control.doze_interval);
600 		if (error) {
601 			dev_err(&fn->dev,
602 				"Failed to write doze interval: %d\n", error);
603 			return error;
604 		}
605 
606 		error = rmi_write_block(fn->rmi_dev,
607 					 f01->wakeup_threshold_addr,
608 					 &f01->device_control.wakeup_threshold,
609 					 sizeof(u8));
610 		if (error) {
611 			dev_err(&fn->dev,
612 				"Failed to write wakeup threshold: %d\n",
613 				error);
614 			return error;
615 		}
616 	}
617 
618 	if (f01->properties.has_adjustable_doze_holdoff) {
619 		error = rmi_write(fn->rmi_dev, f01->doze_holdoff_addr,
620 				  f01->device_control.doze_holdoff);
621 		if (error) {
622 			dev_err(&fn->dev,
623 				"Failed to write doze holdoff: %d\n", error);
624 			return error;
625 		}
626 	}
627 
628 	return 0;
629 }
630 
631 static int rmi_f01_suspend(struct rmi_function *fn)
632 {
633 	struct f01_data *f01 = dev_get_drvdata(&fn->dev);
634 	int error;
635 
636 	f01->old_nosleep =
637 		f01->device_control.ctrl0 & RMI_F01_CTRL0_NOSLEEP_BIT;
638 	f01->device_control.ctrl0 &= ~RMI_F01_CTRL0_NOSLEEP_BIT;
639 
640 	f01->device_control.ctrl0 &= ~RMI_F01_CTRL0_SLEEP_MODE_MASK;
641 	if (device_may_wakeup(fn->rmi_dev->xport->dev))
642 		f01->device_control.ctrl0 |= RMI_SLEEP_MODE_RESERVED1;
643 	else
644 		f01->device_control.ctrl0 |= RMI_SLEEP_MODE_SENSOR_SLEEP;
645 
646 	error = rmi_write(fn->rmi_dev, fn->fd.control_base_addr,
647 			  f01->device_control.ctrl0);
648 	if (error) {
649 		dev_err(&fn->dev, "Failed to write sleep mode: %d.\n", error);
650 		if (f01->old_nosleep)
651 			f01->device_control.ctrl0 |= RMI_F01_CTRL0_NOSLEEP_BIT;
652 		f01->device_control.ctrl0 &= ~RMI_F01_CTRL0_SLEEP_MODE_MASK;
653 		f01->device_control.ctrl0 |= RMI_SLEEP_MODE_NORMAL;
654 		return error;
655 	}
656 
657 	return 0;
658 }
659 
660 static int rmi_f01_resume(struct rmi_function *fn)
661 {
662 	struct f01_data *f01 = dev_get_drvdata(&fn->dev);
663 	int error;
664 
665 	if (f01->old_nosleep)
666 		f01->device_control.ctrl0 |= RMI_F01_CTRL0_NOSLEEP_BIT;
667 
668 	f01->device_control.ctrl0 &= ~RMI_F01_CTRL0_SLEEP_MODE_MASK;
669 	f01->device_control.ctrl0 |= RMI_SLEEP_MODE_NORMAL;
670 
671 	error = rmi_write(fn->rmi_dev, fn->fd.control_base_addr,
672 			  f01->device_control.ctrl0);
673 	if (error) {
674 		dev_err(&fn->dev,
675 			"Failed to restore normal operation: %d.\n", error);
676 		return error;
677 	}
678 
679 	return 0;
680 }
681 
682 static irqreturn_t rmi_f01_attention(int irq, void *ctx)
683 {
684 	struct rmi_function *fn = ctx;
685 	struct rmi_device *rmi_dev = fn->rmi_dev;
686 	int error;
687 	u8 device_status;
688 
689 	error = rmi_read(rmi_dev, fn->fd.data_base_addr, &device_status);
690 	if (error) {
691 		dev_err(&fn->dev,
692 			"Failed to read device status: %d.\n", error);
693 		return IRQ_RETVAL(error);
694 	}
695 
696 	if (RMI_F01_STATUS_BOOTLOADER(device_status))
697 		dev_warn(&fn->dev,
698 			 "Device in bootloader mode, please update firmware\n");
699 
700 	if (RMI_F01_STATUS_UNCONFIGURED(device_status)) {
701 		dev_warn(&fn->dev, "Device reset detected.\n");
702 		error = rmi_dev->driver->reset_handler(rmi_dev);
703 		if (error) {
704 			dev_err(&fn->dev, "Device reset failed: %d\n", error);
705 			return IRQ_RETVAL(error);
706 		}
707 	}
708 
709 	return IRQ_HANDLED;
710 }
711 
712 struct rmi_function_handler rmi_f01_handler = {
713 	.driver = {
714 		.name	= "rmi4_f01",
715 		/*
716 		 * Do not allow user unbinding F01 as it is critical
717 		 * function.
718 		 */
719 		.suppress_bind_attrs = true,
720 	},
721 	.func		= 0x01,
722 	.probe		= rmi_f01_probe,
723 	.remove		= rmi_f01_remove,
724 	.config		= rmi_f01_config,
725 	.attention	= rmi_f01_attention,
726 	.suspend	= rmi_f01_suspend,
727 	.resume		= rmi_f01_resume,
728 };
729