xtensa: support DMA buffers in high memory
[cris-mirror.git] / drivers / input / rmi4 / rmi_f34.c
blobf1f5ac539d5d56b2d554e2aa7bdb50fd0af0d5e3
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 int rmi_f34_attention(struct rmi_function *fn, unsigned long *irq_bits)
105 struct f34_data *f34 = dev_get_drvdata(&fn->dev);
106 int ret;
107 u8 status;
109 if (f34->bl_version == 5) {
110 ret = rmi_read(f34->fn->rmi_dev, f34->v5.ctrl_address,
111 &status);
112 rmi_dbg(RMI_DEBUG_FN, &fn->dev, "%s: status: %#02x, ret: %d\n",
113 __func__, status, ret);
115 if (!ret && !(status & 0x7f))
116 complete(&f34->v5.cmd_done);
117 } else {
118 ret = rmi_read_block(f34->fn->rmi_dev,
119 f34->fn->fd.data_base_addr +
120 f34->v7.off.flash_status,
121 &status, sizeof(status));
122 rmi_dbg(RMI_DEBUG_FN, &fn->dev, "%s: status: %#02x, ret: %d\n",
123 __func__, status, ret);
125 if (!ret && !(status & 0x1f))
126 complete(&f34->v7.cmd_done);
129 return 0;
132 static int rmi_f34_write_blocks(struct f34_data *f34, const void *data,
133 int block_count, u8 command)
135 struct rmi_function *fn = f34->fn;
136 struct rmi_device *rmi_dev = fn->rmi_dev;
137 u16 address = fn->fd.data_base_addr + F34_BLOCK_DATA_OFFSET;
138 u8 start_address[] = { 0, 0 };
139 int i;
140 int ret;
142 ret = rmi_write_block(rmi_dev, fn->fd.data_base_addr,
143 start_address, sizeof(start_address));
144 if (ret) {
145 dev_err(&fn->dev, "Failed to write initial zeros: %d\n", ret);
146 return ret;
149 for (i = 0; i < block_count; i++) {
150 ret = rmi_write_block(rmi_dev, address,
151 data, f34->v5.block_size);
152 if (ret) {
153 dev_err(&fn->dev,
154 "failed to write block #%d: %d\n", i, ret);
155 return ret;
158 ret = rmi_f34_command(f34, command, F34_IDLE_WAIT_MS, false);
159 if (ret) {
160 dev_err(&fn->dev,
161 "Failed to write command for block #%d: %d\n",
162 i, ret);
163 return ret;
166 rmi_dbg(RMI_DEBUG_FN, &fn->dev, "wrote block %d of %d\n",
167 i + 1, block_count);
169 data += f34->v5.block_size;
170 f34->update_progress += f34->v5.block_size;
171 f34->update_status = (f34->update_progress * 100) /
172 f34->update_size;
175 return 0;
178 static int rmi_f34_write_firmware(struct f34_data *f34, const void *data)
180 return rmi_f34_write_blocks(f34, data, f34->v5.fw_blocks,
181 F34_WRITE_FW_BLOCK);
184 static int rmi_f34_write_config(struct f34_data *f34, const void *data)
186 return rmi_f34_write_blocks(f34, data, f34->v5.config_blocks,
187 F34_WRITE_CONFIG_BLOCK);
190 static int rmi_f34_enable_flash(struct f34_data *f34)
192 return rmi_f34_command(f34, F34_ENABLE_FLASH_PROG,
193 F34_ENABLE_WAIT_MS, true);
196 static int rmi_f34_flash_firmware(struct f34_data *f34,
197 const struct rmi_f34_firmware *syn_fw)
199 struct rmi_function *fn = f34->fn;
200 u32 image_size = le32_to_cpu(syn_fw->image_size);
201 u32 config_size = le32_to_cpu(syn_fw->config_size);
202 int ret;
204 f34->update_progress = 0;
205 f34->update_size = image_size + config_size;
207 if (image_size) {
208 dev_info(&fn->dev, "Erasing firmware...\n");
209 ret = rmi_f34_command(f34, F34_ERASE_ALL,
210 F34_ERASE_WAIT_MS, true);
211 if (ret)
212 return ret;
214 dev_info(&fn->dev, "Writing firmware (%d bytes)...\n",
215 image_size);
216 ret = rmi_f34_write_firmware(f34, syn_fw->data);
217 if (ret)
218 return ret;
221 if (config_size) {
223 * We only need to erase config if we haven't updated
224 * firmware.
226 if (!image_size) {
227 dev_info(&fn->dev, "Erasing config...\n");
228 ret = rmi_f34_command(f34, F34_ERASE_CONFIG,
229 F34_ERASE_WAIT_MS, true);
230 if (ret)
231 return ret;
234 dev_info(&fn->dev, "Writing config (%d bytes)...\n",
235 config_size);
236 ret = rmi_f34_write_config(f34, &syn_fw->data[image_size]);
237 if (ret)
238 return ret;
241 return 0;
244 static int rmi_f34_update_firmware(struct f34_data *f34,
245 const struct firmware *fw)
247 const struct rmi_f34_firmware *syn_fw =
248 (const struct rmi_f34_firmware *)fw->data;
249 u32 image_size = le32_to_cpu(syn_fw->image_size);
250 u32 config_size = le32_to_cpu(syn_fw->config_size);
251 int ret;
253 BUILD_BUG_ON(offsetof(struct rmi_f34_firmware, data) !=
254 F34_FW_IMAGE_OFFSET);
256 rmi_dbg(RMI_DEBUG_FN, &f34->fn->dev,
257 "FW size:%zd, checksum:%08x, image_size:%d, config_size:%d\n",
258 fw->size,
259 le32_to_cpu(syn_fw->checksum),
260 image_size, config_size);
262 rmi_dbg(RMI_DEBUG_FN, &f34->fn->dev,
263 "FW bootloader_id:%02x, product_id:%.*s, info: %02x%02x\n",
264 syn_fw->bootloader_version,
265 (int)sizeof(syn_fw->product_id), syn_fw->product_id,
266 syn_fw->product_info[0], syn_fw->product_info[1]);
268 if (image_size && image_size != f34->v5.fw_blocks * f34->v5.block_size) {
269 dev_err(&f34->fn->dev,
270 "Bad firmware image: fw size %d, expected %d\n",
271 image_size, f34->v5.fw_blocks * f34->v5.block_size);
272 ret = -EILSEQ;
273 goto out;
276 if (config_size &&
277 config_size != f34->v5.config_blocks * f34->v5.block_size) {
278 dev_err(&f34->fn->dev,
279 "Bad firmware image: config size %d, expected %d\n",
280 config_size,
281 f34->v5.config_blocks * f34->v5.block_size);
282 ret = -EILSEQ;
283 goto out;
286 if (image_size && !config_size) {
287 dev_err(&f34->fn->dev, "Bad firmware image: no config data\n");
288 ret = -EILSEQ;
289 goto out;
292 dev_info(&f34->fn->dev, "Firmware image OK\n");
293 mutex_lock(&f34->v5.flash_mutex);
295 ret = rmi_f34_flash_firmware(f34, syn_fw);
297 mutex_unlock(&f34->v5.flash_mutex);
299 out:
300 return ret;
303 static int rmi_f34_status(struct rmi_function *fn)
305 struct f34_data *f34 = dev_get_drvdata(&fn->dev);
308 * The status is the percentage complete, or once complete,
309 * zero for success or a negative return code.
311 return f34->update_status;
314 static ssize_t rmi_driver_bootloader_id_show(struct device *dev,
315 struct device_attribute *dattr,
316 char *buf)
318 struct rmi_driver_data *data = dev_get_drvdata(dev);
319 struct rmi_function *fn = data->f34_container;
320 struct f34_data *f34;
322 if (fn) {
323 f34 = dev_get_drvdata(&fn->dev);
325 if (f34->bl_version == 5)
326 return scnprintf(buf, PAGE_SIZE, "%c%c\n",
327 f34->bootloader_id[0],
328 f34->bootloader_id[1]);
329 else
330 return scnprintf(buf, PAGE_SIZE, "V%d.%d\n",
331 f34->bootloader_id[1],
332 f34->bootloader_id[0]);
335 return 0;
338 static DEVICE_ATTR(bootloader_id, 0444, rmi_driver_bootloader_id_show, NULL);
340 static ssize_t rmi_driver_configuration_id_show(struct device *dev,
341 struct device_attribute *dattr,
342 char *buf)
344 struct rmi_driver_data *data = dev_get_drvdata(dev);
345 struct rmi_function *fn = data->f34_container;
346 struct f34_data *f34;
348 if (fn) {
349 f34 = dev_get_drvdata(&fn->dev);
351 return scnprintf(buf, PAGE_SIZE, "%s\n", f34->configuration_id);
354 return 0;
357 static DEVICE_ATTR(configuration_id, 0444,
358 rmi_driver_configuration_id_show, NULL);
360 static int rmi_firmware_update(struct rmi_driver_data *data,
361 const struct firmware *fw)
363 struct rmi_device *rmi_dev = data->rmi_dev;
364 struct device *dev = &rmi_dev->dev;
365 struct f34_data *f34;
366 int ret;
368 if (!data->f34_container) {
369 dev_warn(dev, "%s: No F34 present!\n", __func__);
370 return -EINVAL;
373 f34 = dev_get_drvdata(&data->f34_container->dev);
375 if (f34->bl_version == 7) {
376 if (data->pdt_props & HAS_BSR) {
377 dev_err(dev, "%s: LTS not supported\n", __func__);
378 return -ENODEV;
380 } else if (f34->bl_version != 5) {
381 dev_warn(dev, "F34 V%d not supported!\n",
382 data->f34_container->fd.function_version);
383 return -ENODEV;
386 /* Enter flash mode */
387 if (f34->bl_version == 7)
388 ret = rmi_f34v7_start_reflash(f34, fw);
389 else
390 ret = rmi_f34_enable_flash(f34);
391 if (ret)
392 return ret;
394 rmi_disable_irq(rmi_dev, false);
396 /* Tear down functions and re-probe */
397 rmi_free_function_list(rmi_dev);
399 ret = rmi_probe_interrupts(data);
400 if (ret)
401 return ret;
403 ret = rmi_init_functions(data);
404 if (ret)
405 return ret;
407 if (!data->bootloader_mode || !data->f34_container) {
408 dev_warn(dev, "%s: No F34 present or not in bootloader!\n",
409 __func__);
410 return -EINVAL;
413 rmi_enable_irq(rmi_dev, false);
415 f34 = dev_get_drvdata(&data->f34_container->dev);
417 /* Perform firmware update */
418 if (f34->bl_version == 7)
419 ret = rmi_f34v7_do_reflash(f34, fw);
420 else
421 ret = rmi_f34_update_firmware(f34, fw);
423 if (ret) {
424 f34->update_status = ret;
425 dev_err(&f34->fn->dev,
426 "Firmware update failed, status: %d\n", ret);
427 } else {
428 dev_info(&f34->fn->dev, "Firmware update complete\n");
431 rmi_disable_irq(rmi_dev, false);
433 /* Re-probe */
434 rmi_dbg(RMI_DEBUG_FN, dev, "Re-probing device\n");
435 rmi_free_function_list(rmi_dev);
437 ret = rmi_scan_pdt(rmi_dev, NULL, rmi_initial_reset);
438 if (ret < 0)
439 dev_warn(dev, "RMI reset failed!\n");
441 ret = rmi_probe_interrupts(data);
442 if (ret)
443 return ret;
445 ret = rmi_init_functions(data);
446 if (ret)
447 return ret;
449 rmi_enable_irq(rmi_dev, false);
451 if (data->f01_container->dev.driver)
452 /* Driver already bound, so enable ATTN now. */
453 return rmi_enable_sensor(rmi_dev);
455 rmi_dbg(RMI_DEBUG_FN, dev, "%s complete\n", __func__);
457 return ret;
460 static ssize_t rmi_driver_update_fw_store(struct device *dev,
461 struct device_attribute *dattr,
462 const char *buf, size_t count)
464 struct rmi_driver_data *data = dev_get_drvdata(dev);
465 char fw_name[NAME_MAX];
466 const struct firmware *fw;
467 size_t copy_count = count;
468 int ret;
470 if (count == 0 || count >= NAME_MAX)
471 return -EINVAL;
473 if (buf[count - 1] == '\0' || buf[count - 1] == '\n')
474 copy_count -= 1;
476 strncpy(fw_name, buf, copy_count);
477 fw_name[copy_count] = '\0';
479 ret = request_firmware(&fw, fw_name, dev);
480 if (ret)
481 return ret;
483 dev_info(dev, "Flashing %s\n", fw_name);
485 ret = rmi_firmware_update(data, fw);
487 release_firmware(fw);
489 return ret ?: count;
492 static DEVICE_ATTR(update_fw, 0200, NULL, rmi_driver_update_fw_store);
494 static ssize_t rmi_driver_update_fw_status_show(struct device *dev,
495 struct device_attribute *dattr,
496 char *buf)
498 struct rmi_driver_data *data = dev_get_drvdata(dev);
499 int update_status = 0;
501 if (data->f34_container)
502 update_status = rmi_f34_status(data->f34_container);
504 return scnprintf(buf, PAGE_SIZE, "%d\n", update_status);
507 static DEVICE_ATTR(update_fw_status, 0444,
508 rmi_driver_update_fw_status_show, NULL);
510 static struct attribute *rmi_firmware_attrs[] = {
511 &dev_attr_bootloader_id.attr,
512 &dev_attr_configuration_id.attr,
513 &dev_attr_update_fw.attr,
514 &dev_attr_update_fw_status.attr,
515 NULL
518 static const struct attribute_group rmi_firmware_attr_group = {
519 .attrs = rmi_firmware_attrs,
522 static int rmi_f34_probe(struct rmi_function *fn)
524 struct f34_data *f34;
525 unsigned char f34_queries[9];
526 bool has_config_id;
527 u8 version = fn->fd.function_version;
528 int ret;
530 f34 = devm_kzalloc(&fn->dev, sizeof(struct f34_data), GFP_KERNEL);
531 if (!f34)
532 return -ENOMEM;
534 f34->fn = fn;
535 dev_set_drvdata(&fn->dev, f34);
537 /* v5 code only supported version 0, try V7 probe */
538 if (version > 0)
539 return rmi_f34v7_probe(f34);
541 f34->bl_version = 5;
543 ret = rmi_read_block(fn->rmi_dev, fn->fd.query_base_addr,
544 f34_queries, sizeof(f34_queries));
545 if (ret) {
546 dev_err(&fn->dev, "%s: Failed to query properties\n",
547 __func__);
548 return ret;
551 snprintf(f34->bootloader_id, sizeof(f34->bootloader_id),
552 "%c%c", f34_queries[0], f34_queries[1]);
554 mutex_init(&f34->v5.flash_mutex);
555 init_completion(&f34->v5.cmd_done);
557 f34->v5.block_size = get_unaligned_le16(&f34_queries[3]);
558 f34->v5.fw_blocks = get_unaligned_le16(&f34_queries[5]);
559 f34->v5.config_blocks = get_unaligned_le16(&f34_queries[7]);
560 f34->v5.ctrl_address = fn->fd.data_base_addr + F34_BLOCK_DATA_OFFSET +
561 f34->v5.block_size;
562 has_config_id = f34_queries[2] & (1 << 2);
564 rmi_dbg(RMI_DEBUG_FN, &fn->dev, "Bootloader ID: %s\n",
565 f34->bootloader_id);
566 rmi_dbg(RMI_DEBUG_FN, &fn->dev, "Block size: %d\n",
567 f34->v5.block_size);
568 rmi_dbg(RMI_DEBUG_FN, &fn->dev, "FW blocks: %d\n",
569 f34->v5.fw_blocks);
570 rmi_dbg(RMI_DEBUG_FN, &fn->dev, "CFG blocks: %d\n",
571 f34->v5.config_blocks);
573 if (has_config_id) {
574 ret = rmi_read_block(fn->rmi_dev, fn->fd.control_base_addr,
575 f34_queries, sizeof(f34_queries));
576 if (ret) {
577 dev_err(&fn->dev, "Failed to read F34 config ID\n");
578 return ret;
581 snprintf(f34->configuration_id, sizeof(f34->configuration_id),
582 "%02x%02x%02x%02x",
583 f34_queries[0], f34_queries[1],
584 f34_queries[2], f34_queries[3]);
586 rmi_dbg(RMI_DEBUG_FN, &fn->dev, "Configuration ID: %s\n",
587 f34->configuration_id);
590 return 0;
593 int rmi_f34_create_sysfs(struct rmi_device *rmi_dev)
595 return sysfs_create_group(&rmi_dev->dev.kobj, &rmi_firmware_attr_group);
598 void rmi_f34_remove_sysfs(struct rmi_device *rmi_dev)
600 sysfs_remove_group(&rmi_dev->dev.kobj, &rmi_firmware_attr_group);
603 struct rmi_function_handler rmi_f34_handler = {
604 .driver = {
605 .name = "rmi4_f34",
607 .func = 0x34,
608 .probe = rmi_f34_probe,
609 .attention = rmi_f34_attention,