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