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.
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"
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
];
26 ret
= rmi_read_block(rmi_dev
, fn
->fd
.query_base_addr
,
27 bootloader_id
, sizeof(bootloader_id
));
29 dev_err(&fn
->dev
, "%s: Reading bootloader ID failed: %d\n",
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
));
41 dev_err(&fn
->dev
, "Failed to write bootloader ID: %d\n", ret
);
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
;
56 ret
= rmi_f34_write_bootloader_id(f34
);
61 init_completion(&f34
->v5
.cmd_done
);
63 ret
= rmi_read(rmi_dev
, f34
->v5
.ctrl_address
, &f34
->v5
.status
);
65 dev_err(&f34
->fn
->dev
,
66 "%s: Failed to read cmd register: %d (command %#02x)\n",
67 __func__
, ret
, command
);
71 f34
->v5
.status
|= command
& 0x0f;
73 ret
= rmi_write(rmi_dev
, f34
->v5
.ctrl_address
, f34
->v5
.status
);
75 dev_err(&f34
->fn
->dev
,
76 "Failed to write F34 command %#02x: %d\n",
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
);
86 dev_err(&f34
->fn
->dev
,
87 "%s: cmd %#02x timed out: %d\n",
88 __func__
, command
, 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
);
103 static int rmi_f34_attention(struct rmi_function
*fn
, unsigned long *irq_bits
)
105 struct f34_data
*f34
= dev_get_drvdata(&fn
->dev
);
109 if (f34
->bl_version
== 5) {
110 ret
= rmi_read(f34
->fn
->rmi_dev
, f34
->v5
.ctrl_address
,
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
);
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
);
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 };
142 ret
= rmi_write_block(rmi_dev
, fn
->fd
.data_base_addr
,
143 start_address
, sizeof(start_address
));
145 dev_err(&fn
->dev
, "Failed to write initial zeros: %d\n", ret
);
149 for (i
= 0; i
< block_count
; i
++) {
150 ret
= rmi_write_block(rmi_dev
, address
,
151 data
, f34
->v5
.block_size
);
154 "failed to write block #%d: %d\n", i
, ret
);
158 ret
= rmi_f34_command(f34
, command
, F34_IDLE_WAIT_MS
, false);
161 "Failed to write command for block #%d: %d\n",
166 rmi_dbg(RMI_DEBUG_FN
, &fn
->dev
, "wrote block %d of %d\n",
169 data
+= f34
->v5
.block_size
;
170 f34
->update_progress
+= f34
->v5
.block_size
;
171 f34
->update_status
= (f34
->update_progress
* 100) /
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
,
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
);
204 f34
->update_progress
= 0;
205 f34
->update_size
= image_size
+ config_size
;
208 dev_info(&fn
->dev
, "Erasing firmware...\n");
209 ret
= rmi_f34_command(f34
, F34_ERASE_ALL
,
210 F34_ERASE_WAIT_MS
, true);
214 dev_info(&fn
->dev
, "Writing firmware (%d bytes)...\n",
216 ret
= rmi_f34_write_firmware(f34
, syn_fw
->data
);
223 * We only need to erase config if we haven't updated
227 dev_info(&fn
->dev
, "Erasing config...\n");
228 ret
= rmi_f34_command(f34
, F34_ERASE_CONFIG
,
229 F34_ERASE_WAIT_MS
, true);
234 dev_info(&fn
->dev
, "Writing config (%d bytes)...\n",
236 ret
= rmi_f34_write_config(f34
, &syn_fw
->data
[image_size
]);
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
);
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",
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
);
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",
281 f34
->v5
.config_blocks
* f34
->v5
.block_size
);
286 if (image_size
&& !config_size
) {
287 dev_err(&f34
->fn
->dev
, "Bad firmware image: no config data\n");
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
);
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
,
318 struct rmi_driver_data
*data
= dev_get_drvdata(dev
);
319 struct rmi_function
*fn
= data
->f34_container
;
320 struct f34_data
*f34
;
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]);
330 return scnprintf(buf
, PAGE_SIZE
, "V%d.%d\n",
331 f34
->bootloader_id
[1],
332 f34
->bootloader_id
[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
,
344 struct rmi_driver_data
*data
= dev_get_drvdata(dev
);
345 struct rmi_function
*fn
= data
->f34_container
;
346 struct f34_data
*f34
;
349 f34
= dev_get_drvdata(&fn
->dev
);
351 return scnprintf(buf
, PAGE_SIZE
, "%s\n", f34
->configuration_id
);
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
;
368 if (!data
->f34_container
) {
369 dev_warn(dev
, "%s: No F34 present!\n", __func__
);
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__
);
380 } else if (f34
->bl_version
!= 5) {
381 dev_warn(dev
, "F34 V%d not supported!\n",
382 data
->f34_container
->fd
.function_version
);
386 /* Enter flash mode */
387 if (f34
->bl_version
== 7)
388 ret
= rmi_f34v7_start_reflash(f34
, fw
);
390 ret
= rmi_f34_enable_flash(f34
);
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
);
403 ret
= rmi_init_functions(data
);
407 if (!data
->bootloader_mode
|| !data
->f34_container
) {
408 dev_warn(dev
, "%s: No F34 present or not in bootloader!\n",
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
);
421 ret
= rmi_f34_update_firmware(f34
, fw
);
424 f34
->update_status
= ret
;
425 dev_err(&f34
->fn
->dev
,
426 "Firmware update failed, status: %d\n", ret
);
428 dev_info(&f34
->fn
->dev
, "Firmware update complete\n");
431 rmi_disable_irq(rmi_dev
, false);
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
);
439 dev_warn(dev
, "RMI reset failed!\n");
441 ret
= rmi_probe_interrupts(data
);
445 ret
= rmi_init_functions(data
);
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__
);
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
;
470 if (count
== 0 || count
>= NAME_MAX
)
473 if (buf
[count
- 1] == '\0' || buf
[count
- 1] == '\n')
476 strncpy(fw_name
, buf
, copy_count
);
477 fw_name
[copy_count
] = '\0';
479 ret
= request_firmware(&fw
, fw_name
, dev
);
483 dev_info(dev
, "Flashing %s\n", fw_name
);
485 ret
= rmi_firmware_update(data
, fw
);
487 release_firmware(fw
);
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
,
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
,
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];
527 u8 version
= fn
->fd
.function_version
;
530 f34
= devm_kzalloc(&fn
->dev
, sizeof(struct f34_data
), GFP_KERNEL
);
535 dev_set_drvdata(&fn
->dev
, f34
);
537 /* v5 code only supported version 0, try V7 probe */
539 return rmi_f34v7_probe(f34
);
543 ret
= rmi_read_block(fn
->rmi_dev
, fn
->fd
.query_base_addr
,
544 f34_queries
, sizeof(f34_queries
));
546 dev_err(&fn
->dev
, "%s: Failed to query properties\n",
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
+
562 has_config_id
= f34_queries
[2] & (1 << 2);
564 rmi_dbg(RMI_DEBUG_FN
, &fn
->dev
, "Bootloader ID: %s\n",
566 rmi_dbg(RMI_DEBUG_FN
, &fn
->dev
, "Block size: %d\n",
568 rmi_dbg(RMI_DEBUG_FN
, &fn
->dev
, "FW blocks: %d\n",
570 rmi_dbg(RMI_DEBUG_FN
, &fn
->dev
, "CFG blocks: %d\n",
571 f34
->v5
.config_blocks
);
574 ret
= rmi_read_block(fn
->rmi_dev
, fn
->fd
.control_base_addr
,
575 f34_queries
, sizeof(f34_queries
));
577 dev_err(&fn
->dev
, "Failed to read F34 config ID\n");
581 snprintf(f34
->configuration_id
, sizeof(f34
->configuration_id
),
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
);
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
= {
608 .probe
= rmi_f34_probe
,
609 .attention
= rmi_f34_attention
,