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