Linux 5.1.15
[linux/fpc-iii.git] / drivers / input / rmi4 / rmi_f34.c
blob87a7d4ba382d7210b294f8168adb5083c15b80ee
1 /*
2 * Copyright (c) 2007-2016, Synaptics Incorporated
3 * Copyright (C) 2016 Zodiac Inflight Innovations
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 */
10 #include <linux/kernel.h>
11 #include <linux/rmi.h>
12 #include <linux/firmware.h>
13 #include <asm/unaligned.h>
14 #include <linux/bitops.h>
16 #include "rmi_driver.h"
17 #include "rmi_f34.h"
19 static int rmi_f34_write_bootloader_id(struct f34_data *f34)
21 struct rmi_function *fn = f34->fn;
22 struct rmi_device *rmi_dev = fn->rmi_dev;
23 u8 bootloader_id[F34_BOOTLOADER_ID_LEN];
24 int ret;
26 ret = rmi_read_block(rmi_dev, fn->fd.query_base_addr,
27 bootloader_id, sizeof(bootloader_id));
28 if (ret) {
29 dev_err(&fn->dev, "%s: Reading bootloader ID failed: %d\n",
30 __func__, ret);
31 return ret;
34 rmi_dbg(RMI_DEBUG_FN, &fn->dev, "%s: writing bootloader id '%c%c'\n",
35 __func__, bootloader_id[0], bootloader_id[1]);
37 ret = rmi_write_block(rmi_dev,
38 fn->fd.data_base_addr + F34_BLOCK_DATA_OFFSET,
39 bootloader_id, sizeof(bootloader_id));
40 if (ret) {
41 dev_err(&fn->dev, "Failed to write bootloader ID: %d\n", ret);
42 return ret;
45 return 0;
48 static int rmi_f34_command(struct f34_data *f34, u8 command,
49 unsigned int timeout, bool write_bl_id)
51 struct rmi_function *fn = f34->fn;
52 struct rmi_device *rmi_dev = fn->rmi_dev;
53 int ret;
55 if (write_bl_id) {
56 ret = rmi_f34_write_bootloader_id(f34);
57 if (ret)
58 return ret;
61 init_completion(&f34->v5.cmd_done);
63 ret = rmi_read(rmi_dev, f34->v5.ctrl_address, &f34->v5.status);
64 if (ret) {
65 dev_err(&f34->fn->dev,
66 "%s: Failed to read cmd register: %d (command %#02x)\n",
67 __func__, ret, command);
68 return ret;
71 f34->v5.status |= command & 0x0f;
73 ret = rmi_write(rmi_dev, f34->v5.ctrl_address, f34->v5.status);
74 if (ret < 0) {
75 dev_err(&f34->fn->dev,
76 "Failed to write F34 command %#02x: %d\n",
77 command, ret);
78 return ret;
81 if (!wait_for_completion_timeout(&f34->v5.cmd_done,
82 msecs_to_jiffies(timeout))) {
84 ret = rmi_read(rmi_dev, f34->v5.ctrl_address, &f34->v5.status);
85 if (ret) {
86 dev_err(&f34->fn->dev,
87 "%s: cmd %#02x timed out: %d\n",
88 __func__, command, ret);
89 return ret;
92 if (f34->v5.status & 0x7f) {
93 dev_err(&f34->fn->dev,
94 "%s: cmd %#02x timed out, status: %#02x\n",
95 __func__, command, f34->v5.status);
96 return -ETIMEDOUT;
100 return 0;
103 static irqreturn_t rmi_f34_attention(int irq, void *ctx)
105 struct rmi_function *fn = ctx;
106 struct f34_data *f34 = dev_get_drvdata(&fn->dev);
107 int ret;
108 u8 status;
110 if (f34->bl_version == 5) {
111 ret = rmi_read(f34->fn->rmi_dev, f34->v5.ctrl_address,
112 &status);
113 rmi_dbg(RMI_DEBUG_FN, &fn->dev, "%s: status: %#02x, ret: %d\n",
114 __func__, status, ret);
116 if (!ret && !(status & 0x7f))
117 complete(&f34->v5.cmd_done);
118 } else {
119 ret = rmi_read_block(f34->fn->rmi_dev,
120 f34->fn->fd.data_base_addr +
121 f34->v7.off.flash_status,
122 &status, sizeof(status));
123 rmi_dbg(RMI_DEBUG_FN, &fn->dev, "%s: status: %#02x, ret: %d\n",
124 __func__, status, ret);
126 if (!ret && !(status & 0x1f))
127 complete(&f34->v7.cmd_done);
130 return IRQ_HANDLED;
133 static int rmi_f34_write_blocks(struct f34_data *f34, const void *data,
134 int block_count, u8 command)
136 struct rmi_function *fn = f34->fn;
137 struct rmi_device *rmi_dev = fn->rmi_dev;
138 u16 address = fn->fd.data_base_addr + F34_BLOCK_DATA_OFFSET;
139 u8 start_address[] = { 0, 0 };
140 int i;
141 int ret;
143 ret = rmi_write_block(rmi_dev, fn->fd.data_base_addr,
144 start_address, sizeof(start_address));
145 if (ret) {
146 dev_err(&fn->dev, "Failed to write initial zeros: %d\n", ret);
147 return ret;
150 for (i = 0; i < block_count; i++) {
151 ret = rmi_write_block(rmi_dev, address,
152 data, f34->v5.block_size);
153 if (ret) {
154 dev_err(&fn->dev,
155 "failed to write block #%d: %d\n", i, ret);
156 return ret;
159 ret = rmi_f34_command(f34, command, F34_IDLE_WAIT_MS, false);
160 if (ret) {
161 dev_err(&fn->dev,
162 "Failed to write command for block #%d: %d\n",
163 i, ret);
164 return ret;
167 rmi_dbg(RMI_DEBUG_FN, &fn->dev, "wrote block %d of %d\n",
168 i + 1, block_count);
170 data += f34->v5.block_size;
171 f34->update_progress += f34->v5.block_size;
172 f34->update_status = (f34->update_progress * 100) /
173 f34->update_size;
176 return 0;
179 static int rmi_f34_write_firmware(struct f34_data *f34, const void *data)
181 return rmi_f34_write_blocks(f34, data, f34->v5.fw_blocks,
182 F34_WRITE_FW_BLOCK);
185 static int rmi_f34_write_config(struct f34_data *f34, const void *data)
187 return rmi_f34_write_blocks(f34, data, f34->v5.config_blocks,
188 F34_WRITE_CONFIG_BLOCK);
191 static int rmi_f34_enable_flash(struct f34_data *f34)
193 return rmi_f34_command(f34, F34_ENABLE_FLASH_PROG,
194 F34_ENABLE_WAIT_MS, true);
197 static int rmi_f34_flash_firmware(struct f34_data *f34,
198 const struct rmi_f34_firmware *syn_fw)
200 struct rmi_function *fn = f34->fn;
201 u32 image_size = le32_to_cpu(syn_fw->image_size);
202 u32 config_size = le32_to_cpu(syn_fw->config_size);
203 int ret;
205 f34->update_progress = 0;
206 f34->update_size = image_size + config_size;
208 if (image_size) {
209 dev_info(&fn->dev, "Erasing firmware...\n");
210 ret = rmi_f34_command(f34, F34_ERASE_ALL,
211 F34_ERASE_WAIT_MS, true);
212 if (ret)
213 return ret;
215 dev_info(&fn->dev, "Writing firmware (%d bytes)...\n",
216 image_size);
217 ret = rmi_f34_write_firmware(f34, syn_fw->data);
218 if (ret)
219 return ret;
222 if (config_size) {
224 * We only need to erase config if we haven't updated
225 * firmware.
227 if (!image_size) {
228 dev_info(&fn->dev, "Erasing config...\n");
229 ret = rmi_f34_command(f34, F34_ERASE_CONFIG,
230 F34_ERASE_WAIT_MS, true);
231 if (ret)
232 return ret;
235 dev_info(&fn->dev, "Writing config (%d bytes)...\n",
236 config_size);
237 ret = rmi_f34_write_config(f34, &syn_fw->data[image_size]);
238 if (ret)
239 return ret;
242 return 0;
245 static int rmi_f34_update_firmware(struct f34_data *f34,
246 const struct firmware *fw)
248 const struct rmi_f34_firmware *syn_fw =
249 (const struct rmi_f34_firmware *)fw->data;
250 u32 image_size = le32_to_cpu(syn_fw->image_size);
251 u32 config_size = le32_to_cpu(syn_fw->config_size);
252 int ret;
254 BUILD_BUG_ON(offsetof(struct rmi_f34_firmware, data) !=
255 F34_FW_IMAGE_OFFSET);
257 rmi_dbg(RMI_DEBUG_FN, &f34->fn->dev,
258 "FW size:%zd, checksum:%08x, image_size:%d, config_size:%d\n",
259 fw->size,
260 le32_to_cpu(syn_fw->checksum),
261 image_size, config_size);
263 rmi_dbg(RMI_DEBUG_FN, &f34->fn->dev,
264 "FW bootloader_id:%02x, product_id:%.*s, info: %02x%02x\n",
265 syn_fw->bootloader_version,
266 (int)sizeof(syn_fw->product_id), syn_fw->product_id,
267 syn_fw->product_info[0], syn_fw->product_info[1]);
269 if (image_size && image_size != f34->v5.fw_blocks * f34->v5.block_size) {
270 dev_err(&f34->fn->dev,
271 "Bad firmware image: fw size %d, expected %d\n",
272 image_size, f34->v5.fw_blocks * f34->v5.block_size);
273 ret = -EILSEQ;
274 goto out;
277 if (config_size &&
278 config_size != f34->v5.config_blocks * f34->v5.block_size) {
279 dev_err(&f34->fn->dev,
280 "Bad firmware image: config size %d, expected %d\n",
281 config_size,
282 f34->v5.config_blocks * f34->v5.block_size);
283 ret = -EILSEQ;
284 goto out;
287 if (image_size && !config_size) {
288 dev_err(&f34->fn->dev, "Bad firmware image: no config data\n");
289 ret = -EILSEQ;
290 goto out;
293 dev_info(&f34->fn->dev, "Firmware image OK\n");
294 mutex_lock(&f34->v5.flash_mutex);
296 ret = rmi_f34_flash_firmware(f34, syn_fw);
298 mutex_unlock(&f34->v5.flash_mutex);
300 out:
301 return ret;
304 static int rmi_f34_status(struct rmi_function *fn)
306 struct f34_data *f34 = dev_get_drvdata(&fn->dev);
309 * The status is the percentage complete, or once complete,
310 * zero for success or a negative return code.
312 return f34->update_status;
315 static ssize_t rmi_driver_bootloader_id_show(struct device *dev,
316 struct device_attribute *dattr,
317 char *buf)
319 struct rmi_driver_data *data = dev_get_drvdata(dev);
320 struct rmi_function *fn = data->f34_container;
321 struct f34_data *f34;
323 if (fn) {
324 f34 = dev_get_drvdata(&fn->dev);
326 if (f34->bl_version == 5)
327 return scnprintf(buf, PAGE_SIZE, "%c%c\n",
328 f34->bootloader_id[0],
329 f34->bootloader_id[1]);
330 else
331 return scnprintf(buf, PAGE_SIZE, "V%d.%d\n",
332 f34->bootloader_id[1],
333 f34->bootloader_id[0]);
336 return 0;
339 static DEVICE_ATTR(bootloader_id, 0444, rmi_driver_bootloader_id_show, NULL);
341 static ssize_t rmi_driver_configuration_id_show(struct device *dev,
342 struct device_attribute *dattr,
343 char *buf)
345 struct rmi_driver_data *data = dev_get_drvdata(dev);
346 struct rmi_function *fn = data->f34_container;
347 struct f34_data *f34;
349 if (fn) {
350 f34 = dev_get_drvdata(&fn->dev);
352 return scnprintf(buf, PAGE_SIZE, "%s\n", f34->configuration_id);
355 return 0;
358 static DEVICE_ATTR(configuration_id, 0444,
359 rmi_driver_configuration_id_show, NULL);
361 static int rmi_firmware_update(struct rmi_driver_data *data,
362 const struct firmware *fw)
364 struct rmi_device *rmi_dev = data->rmi_dev;
365 struct device *dev = &rmi_dev->dev;
366 struct f34_data *f34;
367 int ret;
369 if (!data->f34_container) {
370 dev_warn(dev, "%s: No F34 present!\n", __func__);
371 return -EINVAL;
374 f34 = dev_get_drvdata(&data->f34_container->dev);
376 if (f34->bl_version == 7) {
377 if (data->pdt_props & HAS_BSR) {
378 dev_err(dev, "%s: LTS not supported\n", __func__);
379 return -ENODEV;
381 } else if (f34->bl_version != 5) {
382 dev_warn(dev, "F34 V%d not supported!\n",
383 data->f34_container->fd.function_version);
384 return -ENODEV;
387 /* Enter flash mode */
388 if (f34->bl_version == 7)
389 ret = rmi_f34v7_start_reflash(f34, fw);
390 else
391 ret = rmi_f34_enable_flash(f34);
392 if (ret)
393 return ret;
395 rmi_disable_irq(rmi_dev, false);
397 /* Tear down functions and re-probe */
398 rmi_free_function_list(rmi_dev);
400 ret = rmi_probe_interrupts(data);
401 if (ret)
402 return ret;
404 ret = rmi_init_functions(data);
405 if (ret)
406 return ret;
408 if (!data->bootloader_mode || !data->f34_container) {
409 dev_warn(dev, "%s: No F34 present or not in bootloader!\n",
410 __func__);
411 return -EINVAL;
414 rmi_enable_irq(rmi_dev, false);
416 f34 = dev_get_drvdata(&data->f34_container->dev);
418 /* Perform firmware update */
419 if (f34->bl_version == 7)
420 ret = rmi_f34v7_do_reflash(f34, fw);
421 else
422 ret = rmi_f34_update_firmware(f34, fw);
424 if (ret) {
425 f34->update_status = ret;
426 dev_err(&f34->fn->dev,
427 "Firmware update failed, status: %d\n", ret);
428 } else {
429 dev_info(&f34->fn->dev, "Firmware update complete\n");
432 rmi_disable_irq(rmi_dev, false);
434 /* Re-probe */
435 rmi_dbg(RMI_DEBUG_FN, dev, "Re-probing device\n");
436 rmi_free_function_list(rmi_dev);
438 ret = rmi_scan_pdt(rmi_dev, NULL, rmi_initial_reset);
439 if (ret < 0)
440 dev_warn(dev, "RMI reset failed!\n");
442 ret = rmi_probe_interrupts(data);
443 if (ret)
444 return ret;
446 ret = rmi_init_functions(data);
447 if (ret)
448 return ret;
450 rmi_enable_irq(rmi_dev, false);
452 if (data->f01_container->dev.driver)
453 /* Driver already bound, so enable ATTN now. */
454 return rmi_enable_sensor(rmi_dev);
456 rmi_dbg(RMI_DEBUG_FN, dev, "%s complete\n", __func__);
458 return ret;
461 static ssize_t rmi_driver_update_fw_store(struct device *dev,
462 struct device_attribute *dattr,
463 const char *buf, size_t count)
465 struct rmi_driver_data *data = dev_get_drvdata(dev);
466 char fw_name[NAME_MAX];
467 const struct firmware *fw;
468 size_t copy_count = count;
469 int ret;
471 if (count == 0 || count >= NAME_MAX)
472 return -EINVAL;
474 if (buf[count - 1] == '\0' || buf[count - 1] == '\n')
475 copy_count -= 1;
477 strncpy(fw_name, buf, copy_count);
478 fw_name[copy_count] = '\0';
480 ret = request_firmware(&fw, fw_name, dev);
481 if (ret)
482 return ret;
484 dev_info(dev, "Flashing %s\n", fw_name);
486 ret = rmi_firmware_update(data, fw);
488 release_firmware(fw);
490 return ret ?: count;
493 static DEVICE_ATTR(update_fw, 0200, NULL, rmi_driver_update_fw_store);
495 static ssize_t rmi_driver_update_fw_status_show(struct device *dev,
496 struct device_attribute *dattr,
497 char *buf)
499 struct rmi_driver_data *data = dev_get_drvdata(dev);
500 int update_status = 0;
502 if (data->f34_container)
503 update_status = rmi_f34_status(data->f34_container);
505 return scnprintf(buf, PAGE_SIZE, "%d\n", update_status);
508 static DEVICE_ATTR(update_fw_status, 0444,
509 rmi_driver_update_fw_status_show, NULL);
511 static struct attribute *rmi_firmware_attrs[] = {
512 &dev_attr_bootloader_id.attr,
513 &dev_attr_configuration_id.attr,
514 &dev_attr_update_fw.attr,
515 &dev_attr_update_fw_status.attr,
516 NULL
519 static const struct attribute_group rmi_firmware_attr_group = {
520 .attrs = rmi_firmware_attrs,
523 static int rmi_f34_probe(struct rmi_function *fn)
525 struct f34_data *f34;
526 unsigned char f34_queries[9];
527 bool has_config_id;
528 u8 version = fn->fd.function_version;
529 int ret;
531 f34 = devm_kzalloc(&fn->dev, sizeof(struct f34_data), GFP_KERNEL);
532 if (!f34)
533 return -ENOMEM;
535 f34->fn = fn;
536 dev_set_drvdata(&fn->dev, f34);
538 /* v5 code only supported version 0, try V7 probe */
539 if (version > 0)
540 return rmi_f34v7_probe(f34);
542 f34->bl_version = 5;
544 ret = rmi_read_block(fn->rmi_dev, fn->fd.query_base_addr,
545 f34_queries, sizeof(f34_queries));
546 if (ret) {
547 dev_err(&fn->dev, "%s: Failed to query properties\n",
548 __func__);
549 return ret;
552 snprintf(f34->bootloader_id, sizeof(f34->bootloader_id),
553 "%c%c", f34_queries[0], f34_queries[1]);
555 mutex_init(&f34->v5.flash_mutex);
556 init_completion(&f34->v5.cmd_done);
558 f34->v5.block_size = get_unaligned_le16(&f34_queries[3]);
559 f34->v5.fw_blocks = get_unaligned_le16(&f34_queries[5]);
560 f34->v5.config_blocks = get_unaligned_le16(&f34_queries[7]);
561 f34->v5.ctrl_address = fn->fd.data_base_addr + F34_BLOCK_DATA_OFFSET +
562 f34->v5.block_size;
563 has_config_id = f34_queries[2] & (1 << 2);
565 rmi_dbg(RMI_DEBUG_FN, &fn->dev, "Bootloader ID: %s\n",
566 f34->bootloader_id);
567 rmi_dbg(RMI_DEBUG_FN, &fn->dev, "Block size: %d\n",
568 f34->v5.block_size);
569 rmi_dbg(RMI_DEBUG_FN, &fn->dev, "FW blocks: %d\n",
570 f34->v5.fw_blocks);
571 rmi_dbg(RMI_DEBUG_FN, &fn->dev, "CFG blocks: %d\n",
572 f34->v5.config_blocks);
574 if (has_config_id) {
575 ret = rmi_read_block(fn->rmi_dev, fn->fd.control_base_addr,
576 f34_queries, sizeof(f34_queries));
577 if (ret) {
578 dev_err(&fn->dev, "Failed to read F34 config ID\n");
579 return ret;
582 snprintf(f34->configuration_id, sizeof(f34->configuration_id),
583 "%02x%02x%02x%02x",
584 f34_queries[0], f34_queries[1],
585 f34_queries[2], f34_queries[3]);
587 rmi_dbg(RMI_DEBUG_FN, &fn->dev, "Configuration ID: %s\n",
588 f34->configuration_id);
591 return 0;
594 int rmi_f34_create_sysfs(struct rmi_device *rmi_dev)
596 return sysfs_create_group(&rmi_dev->dev.kobj, &rmi_firmware_attr_group);
599 void rmi_f34_remove_sysfs(struct rmi_device *rmi_dev)
601 sysfs_remove_group(&rmi_dev->dev.kobj, &rmi_firmware_attr_group);
604 struct rmi_function_handler rmi_f34_handler = {
605 .driver = {
606 .name = "rmi4_f34",
608 .func = 0x34,
609 .probe = rmi_f34_probe,
610 .attention = rmi_f34_attention,