1
2
3
4
5
6
7
8
9
10#include <linux/kernel.h>
11#include <linux/kconfig.h>
12#include <linux/rmi.h>
13#include <linux/slab.h>
14#include <linux/uaccess.h>
15#include <linux/of.h>
16#include <asm/unaligned.h>
17#include "rmi_driver.h"
18
19#define RMI_PRODUCT_ID_LENGTH 10
20#define RMI_PRODUCT_INFO_LENGTH 2
21
22#define RMI_DATE_CODE_LENGTH 3
23
24#define PRODUCT_ID_OFFSET 0x10
25#define PRODUCT_INFO_OFFSET 0x1E
26
27
28
29#define RMI_F01_CMD_DEVICE_RESET 1
30
31
32
33#define RMI_F01_QRY1_CUSTOM_MAP BIT(0)
34#define RMI_F01_QRY1_NON_COMPLIANT BIT(1)
35#define RMI_F01_QRY1_HAS_LTS BIT(2)
36#define RMI_F01_QRY1_HAS_SENSOR_ID BIT(3)
37#define RMI_F01_QRY1_HAS_CHARGER_INP BIT(4)
38#define RMI_F01_QRY1_HAS_ADJ_DOZE BIT(5)
39#define RMI_F01_QRY1_HAS_ADJ_DOZE_HOFF BIT(6)
40#define RMI_F01_QRY1_HAS_QUERY42 BIT(7)
41
42#define RMI_F01_QRY5_YEAR_MASK 0x1f
43#define RMI_F01_QRY6_MONTH_MASK 0x0f
44#define RMI_F01_QRY7_DAY_MASK 0x1f
45
46#define RMI_F01_QRY2_PRODINFO_MASK 0x7f
47
48#define RMI_F01_BASIC_QUERY_LEN 21
49
50struct f01_basic_properties {
51 u8 manufacturer_id;
52 bool has_lts;
53 bool has_adjustable_doze;
54 bool has_adjustable_doze_holdoff;
55 char dom[11];
56 u8 product_id[RMI_PRODUCT_ID_LENGTH + 1];
57 u16 productinfo;
58 u32 firmware_id;
59 u32 package_id;
60};
61
62
63
64
65#define RMI_F01_STATUS_CODE(status) ((status) & 0x0f)
66
67#define RMI_F01_STATUS_UNCONFIGURED(status) (!!((status) & 0x80))
68
69
70
71
72
73
74
75#define RMI_F01_CTRL0_SLEEP_MODE_MASK 0x03
76
77#define RMI_SLEEP_MODE_NORMAL 0x00
78#define RMI_SLEEP_MODE_SENSOR_SLEEP 0x01
79#define RMI_SLEEP_MODE_RESERVED0 0x02
80#define RMI_SLEEP_MODE_RESERVED1 0x03
81
82
83
84
85
86#define RMI_F01_CTRL0_NOSLEEP_BIT BIT(2)
87
88
89
90
91
92#define RMI_F01_CTRL0_CHARGER_BIT BIT(5)
93
94
95
96
97
98
99#define RMI_F01_CTRL0_REPORTRATE_BIT BIT(6)
100
101
102
103
104
105#define RMI_F01_CTRL0_CONFIGURED_BIT BIT(7)
106
107
108
109
110
111
112
113
114
115
116struct f01_device_control {
117 u8 ctrl0;
118 u8 doze_interval;
119 u8 wakeup_threshold;
120 u8 doze_holdoff;
121};
122
123struct 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
137static 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
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
256const 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
263static 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 scnprintf(buf, PAGE_SIZE, "%d\n",
271 f01->properties.manufacturer_id);
272}
273
274static DEVICE_ATTR(manufacturer_id, 0444,
275 rmi_driver_manufacturer_id_show, NULL);
276
277static ssize_t rmi_driver_dom_show(struct device *dev,
278 struct device_attribute *dattr, char *buf)
279{
280 struct rmi_driver_data *data = dev_get_drvdata(dev);
281 struct f01_data *f01 = dev_get_drvdata(&data->f01_container->dev);
282
283 return scnprintf(buf, PAGE_SIZE, "%s\n", f01->properties.dom);
284}
285
286static DEVICE_ATTR(date_of_manufacture, 0444, rmi_driver_dom_show, NULL);
287
288static ssize_t rmi_driver_product_id_show(struct device *dev,
289 struct device_attribute *dattr,
290 char *buf)
291{
292 struct rmi_driver_data *data = dev_get_drvdata(dev);
293 struct f01_data *f01 = dev_get_drvdata(&data->f01_container->dev);
294
295 return scnprintf(buf, PAGE_SIZE, "%s\n", f01->properties.product_id);
296}
297
298static DEVICE_ATTR(product_id, 0444, rmi_driver_product_id_show, NULL);
299
300static ssize_t rmi_driver_firmware_id_show(struct device *dev,
301 struct device_attribute *dattr,
302 char *buf)
303{
304 struct rmi_driver_data *data = dev_get_drvdata(dev);
305 struct f01_data *f01 = dev_get_drvdata(&data->f01_container->dev);
306
307 return scnprintf(buf, PAGE_SIZE, "%d\n", f01->properties.firmware_id);
308}
309
310static DEVICE_ATTR(firmware_id, 0444, rmi_driver_firmware_id_show, NULL);
311
312static ssize_t rmi_driver_package_id_show(struct device *dev,
313 struct device_attribute *dattr,
314 char *buf)
315{
316 struct rmi_driver_data *data = dev_get_drvdata(dev);
317 struct f01_data *f01 = dev_get_drvdata(&data->f01_container->dev);
318
319 u32 package_id = f01->properties.package_id;
320
321 return scnprintf(buf, PAGE_SIZE, "%04x.%04x\n",
322 package_id & 0xffff, (package_id >> 16) & 0xffff);
323}
324
325static DEVICE_ATTR(package_id, 0444, rmi_driver_package_id_show, NULL);
326
327static struct attribute *rmi_f01_attrs[] = {
328 &dev_attr_manufacturer_id.attr,
329 &dev_attr_date_of_manufacture.attr,
330 &dev_attr_product_id.attr,
331 &dev_attr_firmware_id.attr,
332 &dev_attr_package_id.attr,
333 NULL
334};
335
336static const struct attribute_group rmi_f01_attr_group = {
337 .attrs = rmi_f01_attrs,
338};
339
340static int rmi_f01_probe(struct rmi_function *fn)
341{
342 struct rmi_device *rmi_dev = fn->rmi_dev;
343 struct rmi_driver_data *driver_data = dev_get_drvdata(&rmi_dev->dev);
344 struct rmi_device_platform_data *pdata = rmi_get_platform_data(rmi_dev);
345 struct f01_data *f01;
346 int error;
347 u16 ctrl_base_addr = fn->fd.control_base_addr;
348 u8 device_status;
349 u8 temp;
350
351 f01 = devm_kzalloc(&fn->dev, sizeof(struct f01_data), GFP_KERNEL);
352 if (!f01)
353 return -ENOMEM;
354
355 f01->num_of_irq_regs = driver_data->num_of_irq_regs;
356
357
358
359
360
361
362 error = rmi_read(rmi_dev, fn->fd.control_base_addr,
363 &f01->device_control.ctrl0);
364 if (error) {
365 dev_err(&fn->dev, "Failed to read F01 control: %d\n", error);
366 return error;
367 }
368
369 switch (pdata->power_management.nosleep) {
370 case RMI_REG_STATE_DEFAULT:
371 break;
372 case RMI_REG_STATE_OFF:
373 f01->device_control.ctrl0 &= ~RMI_F01_CTRL0_NOSLEEP_BIT;
374 break;
375 case RMI_REG_STATE_ON:
376 f01->device_control.ctrl0 |= RMI_F01_CTRL0_NOSLEEP_BIT;
377 break;
378 }
379
380
381
382
383
384
385 if ((f01->device_control.ctrl0 & RMI_F01_CTRL0_SLEEP_MODE_MASK) !=
386 RMI_SLEEP_MODE_NORMAL) {
387 dev_warn(&fn->dev,
388 "WARNING: Non-zero sleep mode found. Clearing...\n");
389 f01->device_control.ctrl0 &= ~RMI_F01_CTRL0_SLEEP_MODE_MASK;
390 }
391
392 f01->device_control.ctrl0 |= RMI_F01_CTRL0_CONFIGURED_BIT;
393
394 error = rmi_write(rmi_dev, fn->fd.control_base_addr,
395 f01->device_control.ctrl0);
396 if (error) {
397 dev_err(&fn->dev, "Failed to write F01 control: %d\n", error);
398 return error;
399 }
400
401
402 error = rmi_read(rmi_dev, fn->fd.data_base_addr + 1, &temp);
403 if (error < 0) {
404 dev_err(&fn->dev, "Failed to read Interrupt Status.\n");
405 return error;
406 }
407
408 error = rmi_f01_read_properties(rmi_dev, fn->fd.query_base_addr,
409 &f01->properties);
410 if (error < 0) {
411 dev_err(&fn->dev, "Failed to read F01 properties.\n");
412 return error;
413 }
414
415 dev_info(&fn->dev, "found RMI device, manufacturer: %s, product: %s, fw id: %d\n",
416 f01->properties.manufacturer_id == 1 ? "Synaptics" : "unknown",
417 f01->properties.product_id, f01->properties.firmware_id);
418
419
420 ctrl_base_addr++;
421 ctrl_base_addr += f01->num_of_irq_regs;
422
423
424 if (f01->properties.has_adjustable_doze) {
425 f01->doze_interval_addr = ctrl_base_addr;
426 ctrl_base_addr++;
427
428 if (pdata->power_management.doze_interval) {
429 f01->device_control.doze_interval =
430 pdata->power_management.doze_interval;
431 error = rmi_write(rmi_dev, f01->doze_interval_addr,
432 f01->device_control.doze_interval);
433 if (error) {
434 dev_err(&fn->dev,
435 "Failed to configure F01 doze interval register: %d\n",
436 error);
437 return error;
438 }
439 } else {
440 error = rmi_read(rmi_dev, f01->doze_interval_addr,
441 &f01->device_control.doze_interval);
442 if (error) {
443 dev_err(&fn->dev,
444 "Failed to read F01 doze interval register: %d\n",
445 error);
446 return error;
447 }
448 }
449
450 f01->wakeup_threshold_addr = ctrl_base_addr;
451 ctrl_base_addr++;
452
453 if (pdata->power_management.wakeup_threshold) {
454 f01->device_control.wakeup_threshold =
455 pdata->power_management.wakeup_threshold;
456 error = rmi_write(rmi_dev, f01->wakeup_threshold_addr,
457 f01->device_control.wakeup_threshold);
458 if (error) {
459 dev_err(&fn->dev,
460 "Failed to configure F01 wakeup threshold register: %d\n",
461 error);
462 return error;
463 }
464 } else {
465 error = rmi_read(rmi_dev, f01->wakeup_threshold_addr,
466 &f01->device_control.wakeup_threshold);
467 if (error < 0) {
468 dev_err(&fn->dev,
469 "Failed to read F01 wakeup threshold register: %d\n",
470 error);
471 return error;
472 }
473 }
474 }
475
476 if (f01->properties.has_lts)
477 ctrl_base_addr++;
478
479 if (f01->properties.has_adjustable_doze_holdoff) {
480 f01->doze_holdoff_addr = ctrl_base_addr;
481 ctrl_base_addr++;
482
483 if (pdata->power_management.doze_holdoff) {
484 f01->device_control.doze_holdoff =
485 pdata->power_management.doze_holdoff;
486 error = rmi_write(rmi_dev, f01->doze_holdoff_addr,
487 f01->device_control.doze_holdoff);
488 if (error) {
489 dev_err(&fn->dev,
490 "Failed to configure F01 doze holdoff register: %d\n",
491 error);
492 return error;
493 }
494 } else {
495 error = rmi_read(rmi_dev, f01->doze_holdoff_addr,
496 &f01->device_control.doze_holdoff);
497 if (error) {
498 dev_err(&fn->dev,
499 "Failed to read F01 doze holdoff register: %d\n",
500 error);
501 return error;
502 }
503 }
504 }
505
506 error = rmi_read(rmi_dev, fn->fd.data_base_addr, &device_status);
507 if (error < 0) {
508 dev_err(&fn->dev,
509 "Failed to read device status: %d\n", error);
510 return error;
511 }
512
513 if (RMI_F01_STATUS_UNCONFIGURED(device_status)) {
514 dev_err(&fn->dev,
515 "Device was reset during configuration process, status: %#02x!\n",
516 RMI_F01_STATUS_CODE(device_status));
517 return -EINVAL;
518 }
519
520 dev_set_drvdata(&fn->dev, f01);
521
522 error = sysfs_create_group(&fn->rmi_dev->dev.kobj, &rmi_f01_attr_group);
523 if (error)
524 dev_warn(&fn->dev, "Failed to create sysfs group: %d\n", error);
525
526 return 0;
527}
528
529static void rmi_f01_remove(struct rmi_function *fn)
530{
531 sysfs_remove_group(&fn->rmi_dev->dev.kobj, &rmi_f01_attr_group);
532}
533
534static int rmi_f01_config(struct rmi_function *fn)
535{
536 struct f01_data *f01 = dev_get_drvdata(&fn->dev);
537 int error;
538
539 error = rmi_write(fn->rmi_dev, fn->fd.control_base_addr,
540 f01->device_control.ctrl0);
541 if (error) {
542 dev_err(&fn->dev,
543 "Failed to write device_control register: %d\n", error);
544 return error;
545 }
546
547 if (f01->properties.has_adjustable_doze) {
548 error = rmi_write(fn->rmi_dev, f01->doze_interval_addr,
549 f01->device_control.doze_interval);
550 if (error) {
551 dev_err(&fn->dev,
552 "Failed to write doze interval: %d\n", error);
553 return error;
554 }
555
556 error = rmi_write_block(fn->rmi_dev,
557 f01->wakeup_threshold_addr,
558 &f01->device_control.wakeup_threshold,
559 sizeof(u8));
560 if (error) {
561 dev_err(&fn->dev,
562 "Failed to write wakeup threshold: %d\n",
563 error);
564 return error;
565 }
566 }
567
568 if (f01->properties.has_adjustable_doze_holdoff) {
569 error = rmi_write(fn->rmi_dev, f01->doze_holdoff_addr,
570 f01->device_control.doze_holdoff);
571 if (error) {
572 dev_err(&fn->dev,
573 "Failed to write doze holdoff: %d\n", error);
574 return error;
575 }
576 }
577
578 return 0;
579}
580
581static int rmi_f01_suspend(struct rmi_function *fn)
582{
583 struct f01_data *f01 = dev_get_drvdata(&fn->dev);
584 int error;
585
586 f01->old_nosleep =
587 f01->device_control.ctrl0 & RMI_F01_CTRL0_NOSLEEP_BIT;
588 f01->device_control.ctrl0 &= ~RMI_F01_CTRL0_NOSLEEP_BIT;
589
590 f01->device_control.ctrl0 &= ~RMI_F01_CTRL0_SLEEP_MODE_MASK;
591 if (device_may_wakeup(fn->rmi_dev->xport->dev))
592 f01->device_control.ctrl0 |= RMI_SLEEP_MODE_RESERVED1;
593 else
594 f01->device_control.ctrl0 |= RMI_SLEEP_MODE_SENSOR_SLEEP;
595
596 error = rmi_write(fn->rmi_dev, fn->fd.control_base_addr,
597 f01->device_control.ctrl0);
598 if (error) {
599 dev_err(&fn->dev, "Failed to write sleep mode: %d.\n", error);
600 if (f01->old_nosleep)
601 f01->device_control.ctrl0 |= RMI_F01_CTRL0_NOSLEEP_BIT;
602 f01->device_control.ctrl0 &= ~RMI_F01_CTRL0_SLEEP_MODE_MASK;
603 f01->device_control.ctrl0 |= RMI_SLEEP_MODE_NORMAL;
604 return error;
605 }
606
607 return 0;
608}
609
610static int rmi_f01_resume(struct rmi_function *fn)
611{
612 struct f01_data *f01 = dev_get_drvdata(&fn->dev);
613 int error;
614
615 if (f01->old_nosleep)
616 f01->device_control.ctrl0 |= RMI_F01_CTRL0_NOSLEEP_BIT;
617
618 f01->device_control.ctrl0 &= ~RMI_F01_CTRL0_SLEEP_MODE_MASK;
619 f01->device_control.ctrl0 |= RMI_SLEEP_MODE_NORMAL;
620
621 error = rmi_write(fn->rmi_dev, fn->fd.control_base_addr,
622 f01->device_control.ctrl0);
623 if (error) {
624 dev_err(&fn->dev,
625 "Failed to restore normal operation: %d.\n", error);
626 return error;
627 }
628
629 return 0;
630}
631
632static int rmi_f01_attention(struct rmi_function *fn,
633 unsigned long *irq_bits)
634{
635 struct rmi_device *rmi_dev = fn->rmi_dev;
636 int error;
637 u8 device_status;
638
639 error = rmi_read(rmi_dev, fn->fd.data_base_addr, &device_status);
640 if (error) {
641 dev_err(&fn->dev,
642 "Failed to read device status: %d.\n", error);
643 return error;
644 }
645
646 if (RMI_F01_STATUS_UNCONFIGURED(device_status)) {
647 dev_warn(&fn->dev, "Device reset detected.\n");
648 error = rmi_dev->driver->reset_handler(rmi_dev);
649 if (error) {
650 dev_err(&fn->dev, "Device reset failed: %d\n", error);
651 return error;
652 }
653 }
654
655 return 0;
656}
657
658struct rmi_function_handler rmi_f01_handler = {
659 .driver = {
660 .name = "rmi4_f01",
661
662
663
664
665 .suppress_bind_attrs = true,
666 },
667 .func = 0x01,
668 .probe = rmi_f01_probe,
669 .remove = rmi_f01_remove,
670 .config = rmi_f01_config,
671 .attention = rmi_f01_attention,
672 .suspend = rmi_f01_suspend,
673 .resume = rmi_f01_resume,
674};
675