1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2007-2016, Synaptics Incorporated
4 * Copyright (C) 2016 Zodiac Inflight Innovations
7 #include <linux/kernel.h>
9 #include <linux/firmware.h>
10 #include <linux/unaligned.h>
11 #include <linux/bitops.h>
13 #include "rmi_driver.h"
16 static int rmi_f34_write_bootloader_id(struct f34_data
*f34
)
18 struct rmi_function
*fn
= f34
->fn
;
19 struct rmi_device
*rmi_dev
= fn
->rmi_dev
;
20 u8 bootloader_id
[F34_BOOTLOADER_ID_LEN
];
23 ret
= rmi_read_block(rmi_dev
, fn
->fd
.query_base_addr
,
24 bootloader_id
, sizeof(bootloader_id
));
26 dev_err(&fn
->dev
, "%s: Reading bootloader ID failed: %d\n",
31 rmi_dbg(RMI_DEBUG_FN
, &fn
->dev
, "%s: writing bootloader id '%c%c'\n",
32 __func__
, bootloader_id
[0], bootloader_id
[1]);
34 ret
= rmi_write_block(rmi_dev
,
35 fn
->fd
.data_base_addr
+ F34_BLOCK_DATA_OFFSET
,
36 bootloader_id
, sizeof(bootloader_id
));
38 dev_err(&fn
->dev
, "Failed to write bootloader ID: %d\n", ret
);
45 static int rmi_f34_command(struct f34_data
*f34
, u8 command
,
46 unsigned int timeout
, bool write_bl_id
)
48 struct rmi_function
*fn
= f34
->fn
;
49 struct rmi_device
*rmi_dev
= fn
->rmi_dev
;
53 ret
= rmi_f34_write_bootloader_id(f34
);
58 init_completion(&f34
->v5
.cmd_done
);
60 ret
= rmi_read(rmi_dev
, f34
->v5
.ctrl_address
, &f34
->v5
.status
);
62 dev_err(&f34
->fn
->dev
,
63 "%s: Failed to read cmd register: %d (command %#02x)\n",
64 __func__
, ret
, command
);
68 f34
->v5
.status
|= command
& 0x0f;
70 ret
= rmi_write(rmi_dev
, f34
->v5
.ctrl_address
, f34
->v5
.status
);
72 dev_err(&f34
->fn
->dev
,
73 "Failed to write F34 command %#02x: %d\n",
78 if (!wait_for_completion_timeout(&f34
->v5
.cmd_done
,
79 msecs_to_jiffies(timeout
))) {
81 ret
= rmi_read(rmi_dev
, f34
->v5
.ctrl_address
, &f34
->v5
.status
);
83 dev_err(&f34
->fn
->dev
,
84 "%s: cmd %#02x timed out: %d\n",
85 __func__
, command
, ret
);
89 if (f34
->v5
.status
& 0x7f) {
90 dev_err(&f34
->fn
->dev
,
91 "%s: cmd %#02x timed out, status: %#02x\n",
92 __func__
, command
, f34
->v5
.status
);
100 static irqreturn_t
rmi_f34_attention(int irq
, void *ctx
)
102 struct rmi_function
*fn
= ctx
;
103 struct f34_data
*f34
= dev_get_drvdata(&fn
->dev
);
107 if (f34
->bl_version
== 5) {
108 ret
= rmi_read(f34
->fn
->rmi_dev
, f34
->v5
.ctrl_address
,
110 rmi_dbg(RMI_DEBUG_FN
, &fn
->dev
, "%s: status: %#02x, ret: %d\n",
111 __func__
, status
, ret
);
113 if (!ret
&& !(status
& 0x7f))
114 complete(&f34
->v5
.cmd_done
);
116 ret
= rmi_read_block(f34
->fn
->rmi_dev
,
117 f34
->fn
->fd
.data_base_addr
+
119 &status
, sizeof(status
));
120 rmi_dbg(RMI_DEBUG_FN
, &f34
->fn
->dev
, "%s: cmd: %#02x, ret: %d\n",
121 __func__
, status
, ret
);
123 if (!ret
&& status
== CMD_V7_IDLE
)
124 complete(&f34
->v7
.cmd_done
);
130 static int rmi_f34_write_blocks(struct f34_data
*f34
, const void *data
,
131 int block_count
, u8 command
)
133 struct rmi_function
*fn
= f34
->fn
;
134 struct rmi_device
*rmi_dev
= fn
->rmi_dev
;
135 u16 address
= fn
->fd
.data_base_addr
+ F34_BLOCK_DATA_OFFSET
;
136 u8 start_address
[] = { 0, 0 };
140 ret
= rmi_write_block(rmi_dev
, fn
->fd
.data_base_addr
,
141 start_address
, sizeof(start_address
));
143 dev_err(&fn
->dev
, "Failed to write initial zeros: %d\n", ret
);
147 for (i
= 0; i
< block_count
; i
++) {
148 ret
= rmi_write_block(rmi_dev
, address
,
149 data
, f34
->v5
.block_size
);
152 "failed to write block #%d: %d\n", i
, ret
);
156 ret
= rmi_f34_command(f34
, command
, F34_IDLE_WAIT_MS
, false);
159 "Failed to write command for block #%d: %d\n",
164 rmi_dbg(RMI_DEBUG_FN
, &fn
->dev
, "wrote block %d of %d\n",
167 data
+= f34
->v5
.block_size
;
168 f34
->update_progress
+= f34
->v5
.block_size
;
169 f34
->update_status
= (f34
->update_progress
* 100) /
176 static int rmi_f34_write_firmware(struct f34_data
*f34
, const void *data
)
178 return rmi_f34_write_blocks(f34
, data
, f34
->v5
.fw_blocks
,
182 static int rmi_f34_write_config(struct f34_data
*f34
, const void *data
)
184 return rmi_f34_write_blocks(f34
, data
, f34
->v5
.config_blocks
,
185 F34_WRITE_CONFIG_BLOCK
);
188 static int rmi_f34_enable_flash(struct f34_data
*f34
)
190 return rmi_f34_command(f34
, F34_ENABLE_FLASH_PROG
,
191 F34_ENABLE_WAIT_MS
, true);
194 static int rmi_f34_flash_firmware(struct f34_data
*f34
,
195 const struct rmi_f34_firmware
*syn_fw
)
197 struct rmi_function
*fn
= f34
->fn
;
198 u32 image_size
= le32_to_cpu(syn_fw
->image_size
);
199 u32 config_size
= le32_to_cpu(syn_fw
->config_size
);
202 f34
->update_progress
= 0;
203 f34
->update_size
= image_size
+ config_size
;
206 dev_info(&fn
->dev
, "Erasing firmware...\n");
207 ret
= rmi_f34_command(f34
, F34_ERASE_ALL
,
208 F34_ERASE_WAIT_MS
, true);
212 dev_info(&fn
->dev
, "Writing firmware (%d bytes)...\n",
214 ret
= rmi_f34_write_firmware(f34
, syn_fw
->data
);
221 * We only need to erase config if we haven't updated
225 dev_info(&fn
->dev
, "Erasing config...\n");
226 ret
= rmi_f34_command(f34
, F34_ERASE_CONFIG
,
227 F34_ERASE_WAIT_MS
, true);
232 dev_info(&fn
->dev
, "Writing config (%d bytes)...\n",
234 ret
= rmi_f34_write_config(f34
, &syn_fw
->data
[image_size
]);
242 static int rmi_f34_update_firmware(struct f34_data
*f34
,
243 const struct firmware
*fw
)
245 const struct rmi_f34_firmware
*syn_fw
=
246 (const struct rmi_f34_firmware
*)fw
->data
;
247 u32 image_size
= le32_to_cpu(syn_fw
->image_size
);
248 u32 config_size
= le32_to_cpu(syn_fw
->config_size
);
250 BUILD_BUG_ON(offsetof(struct rmi_f34_firmware
, data
) !=
251 F34_FW_IMAGE_OFFSET
);
253 rmi_dbg(RMI_DEBUG_FN
, &f34
->fn
->dev
,
254 "FW size:%zd, checksum:%08x, image_size:%d, config_size:%d\n",
256 le32_to_cpu(syn_fw
->checksum
),
257 image_size
, config_size
);
259 rmi_dbg(RMI_DEBUG_FN
, &f34
->fn
->dev
,
260 "FW bootloader_id:%02x, product_id:%.*s, info: %02x%02x\n",
261 syn_fw
->bootloader_version
,
262 (int)sizeof(syn_fw
->product_id
), syn_fw
->product_id
,
263 syn_fw
->product_info
[0], syn_fw
->product_info
[1]);
265 if (image_size
&& image_size
!= f34
->v5
.fw_blocks
* f34
->v5
.block_size
) {
266 dev_err(&f34
->fn
->dev
,
267 "Bad firmware image: fw size %d, expected %d\n",
268 image_size
, f34
->v5
.fw_blocks
* f34
->v5
.block_size
);
273 config_size
!= f34
->v5
.config_blocks
* f34
->v5
.block_size
) {
274 dev_err(&f34
->fn
->dev
,
275 "Bad firmware image: config size %d, expected %d\n",
277 f34
->v5
.config_blocks
* f34
->v5
.block_size
);
281 if (image_size
&& !config_size
) {
282 dev_err(&f34
->fn
->dev
, "Bad firmware image: no config data\n");
286 dev_info(&f34
->fn
->dev
, "Firmware image OK\n");
288 guard(mutex
)(&f34
->v5
.flash_mutex
);
289 return rmi_f34_flash_firmware(f34
, syn_fw
);
292 static int rmi_f34_status(struct rmi_function
*fn
)
294 struct f34_data
*f34
= dev_get_drvdata(&fn
->dev
);
297 * The status is the percentage complete, or once complete,
298 * zero for success or a negative return code.
300 return f34
->update_status
;
303 static ssize_t
rmi_driver_bootloader_id_show(struct device
*dev
,
304 struct device_attribute
*dattr
,
307 struct rmi_driver_data
*data
= dev_get_drvdata(dev
);
308 struct rmi_function
*fn
= data
->f34_container
;
309 struct f34_data
*f34
;
312 f34
= dev_get_drvdata(&fn
->dev
);
314 if (f34
->bl_version
== 5)
315 return sysfs_emit(buf
, "%c%c\n",
316 f34
->bootloader_id
[0],
317 f34
->bootloader_id
[1]);
319 return sysfs_emit(buf
, "V%d.%d\n",
320 f34
->bootloader_id
[1],
321 f34
->bootloader_id
[0]);
327 static DEVICE_ATTR(bootloader_id
, 0444, rmi_driver_bootloader_id_show
, NULL
);
329 static ssize_t
rmi_driver_configuration_id_show(struct device
*dev
,
330 struct device_attribute
*dattr
,
333 struct rmi_driver_data
*data
= dev_get_drvdata(dev
);
334 struct rmi_function
*fn
= data
->f34_container
;
335 struct f34_data
*f34
;
338 f34
= dev_get_drvdata(&fn
->dev
);
340 return sysfs_emit(buf
, "%s\n", f34
->configuration_id
);
346 static DEVICE_ATTR(configuration_id
, 0444,
347 rmi_driver_configuration_id_show
, NULL
);
349 static int rmi_firmware_update(struct rmi_driver_data
*data
,
350 const struct firmware
*fw
)
352 struct rmi_device
*rmi_dev
= data
->rmi_dev
;
353 struct device
*dev
= &rmi_dev
->dev
;
354 struct f34_data
*f34
;
357 if (!data
->f34_container
) {
358 dev_warn(dev
, "%s: No F34 present!\n", __func__
);
362 f34
= dev_get_drvdata(&data
->f34_container
->dev
);
364 if (f34
->bl_version
>= 7) {
365 if (data
->pdt_props
& HAS_BSR
) {
366 dev_err(dev
, "%s: LTS not supported\n", __func__
);
369 } else if (f34
->bl_version
!= 5) {
370 dev_warn(dev
, "F34 V%d not supported!\n",
371 data
->f34_container
->fd
.function_version
);
375 /* Enter flash mode */
376 if (f34
->bl_version
>= 7)
377 ret
= rmi_f34v7_start_reflash(f34
, fw
);
379 ret
= rmi_f34_enable_flash(f34
);
383 rmi_disable_irq(rmi_dev
, false);
385 /* Tear down functions and re-probe */
386 rmi_free_function_list(rmi_dev
);
388 ret
= rmi_probe_interrupts(data
);
392 ret
= rmi_init_functions(data
);
396 if (!data
->bootloader_mode
|| !data
->f34_container
) {
397 dev_warn(dev
, "%s: No F34 present or not in bootloader!\n",
402 rmi_enable_irq(rmi_dev
, false);
404 f34
= dev_get_drvdata(&data
->f34_container
->dev
);
406 /* Perform firmware update */
407 if (f34
->bl_version
>= 7)
408 ret
= rmi_f34v7_do_reflash(f34
, fw
);
410 ret
= rmi_f34_update_firmware(f34
, fw
);
413 f34
->update_status
= ret
;
414 dev_err(&f34
->fn
->dev
,
415 "Firmware update failed, status: %d\n", ret
);
417 dev_info(&f34
->fn
->dev
, "Firmware update complete\n");
420 rmi_disable_irq(rmi_dev
, false);
423 rmi_dbg(RMI_DEBUG_FN
, dev
, "Re-probing device\n");
424 rmi_free_function_list(rmi_dev
);
426 ret
= rmi_scan_pdt(rmi_dev
, NULL
, rmi_initial_reset
);
428 dev_warn(dev
, "RMI reset failed!\n");
430 ret
= rmi_probe_interrupts(data
);
434 ret
= rmi_init_functions(data
);
438 rmi_enable_irq(rmi_dev
, false);
440 if (data
->f01_container
->dev
.driver
)
441 /* Driver already bound, so enable ATTN now. */
442 return rmi_enable_sensor(rmi_dev
);
444 rmi_dbg(RMI_DEBUG_FN
, dev
, "%s complete\n", __func__
);
449 static ssize_t
rmi_driver_update_fw_store(struct device
*dev
,
450 struct device_attribute
*dattr
,
451 const char *buf
, size_t count
)
453 struct rmi_driver_data
*data
= dev_get_drvdata(dev
);
454 char fw_name
[NAME_MAX
];
455 size_t copy_count
= count
;
458 if (count
== 0 || count
>= NAME_MAX
)
461 if (buf
[count
- 1] == '\0' || buf
[count
- 1] == '\n')
464 memcpy(fw_name
, buf
, copy_count
);
465 fw_name
[copy_count
] = '\0';
467 const struct firmware
*fw
__free(firmware
) = NULL
;
468 error
= request_firmware(&fw
, fw_name
, dev
);
472 dev_info(dev
, "Flashing %s\n", fw_name
);
474 error
= rmi_firmware_update(data
, fw
);
481 static DEVICE_ATTR(update_fw
, 0200, NULL
, rmi_driver_update_fw_store
);
483 static ssize_t
rmi_driver_update_fw_status_show(struct device
*dev
,
484 struct device_attribute
*dattr
,
487 struct rmi_driver_data
*data
= dev_get_drvdata(dev
);
488 int update_status
= 0;
490 if (data
->f34_container
)
491 update_status
= rmi_f34_status(data
->f34_container
);
493 return sysfs_emit(buf
, "%d\n", update_status
);
496 static DEVICE_ATTR(update_fw_status
, 0444,
497 rmi_driver_update_fw_status_show
, NULL
);
499 static struct attribute
*rmi_firmware_attrs
[] = {
500 &dev_attr_bootloader_id
.attr
,
501 &dev_attr_configuration_id
.attr
,
502 &dev_attr_update_fw
.attr
,
503 &dev_attr_update_fw_status
.attr
,
507 static const struct attribute_group rmi_firmware_attr_group
= {
508 .attrs
= rmi_firmware_attrs
,
511 static int rmi_f34_probe(struct rmi_function
*fn
)
513 struct f34_data
*f34
;
514 unsigned char f34_queries
[9];
516 u8 version
= fn
->fd
.function_version
;
519 f34
= devm_kzalloc(&fn
->dev
, sizeof(struct f34_data
), GFP_KERNEL
);
524 dev_set_drvdata(&fn
->dev
, f34
);
526 /* v5 code only supported version 0, try V7 probe */
528 return rmi_f34v7_probe(f34
);
532 ret
= rmi_read_block(fn
->rmi_dev
, fn
->fd
.query_base_addr
,
533 f34_queries
, sizeof(f34_queries
));
535 dev_err(&fn
->dev
, "%s: Failed to query properties\n",
540 snprintf(f34
->bootloader_id
, sizeof(f34
->bootloader_id
),
541 "%c%c", f34_queries
[0], f34_queries
[1]);
543 mutex_init(&f34
->v5
.flash_mutex
);
544 init_completion(&f34
->v5
.cmd_done
);
546 f34
->v5
.block_size
= get_unaligned_le16(&f34_queries
[3]);
547 f34
->v5
.fw_blocks
= get_unaligned_le16(&f34_queries
[5]);
548 f34
->v5
.config_blocks
= get_unaligned_le16(&f34_queries
[7]);
549 f34
->v5
.ctrl_address
= fn
->fd
.data_base_addr
+ F34_BLOCK_DATA_OFFSET
+
551 has_config_id
= f34_queries
[2] & (1 << 2);
553 rmi_dbg(RMI_DEBUG_FN
, &fn
->dev
, "Bootloader ID: %s\n",
555 rmi_dbg(RMI_DEBUG_FN
, &fn
->dev
, "Block size: %d\n",
557 rmi_dbg(RMI_DEBUG_FN
, &fn
->dev
, "FW blocks: %d\n",
559 rmi_dbg(RMI_DEBUG_FN
, &fn
->dev
, "CFG blocks: %d\n",
560 f34
->v5
.config_blocks
);
563 ret
= rmi_read_block(fn
->rmi_dev
, fn
->fd
.control_base_addr
,
564 f34_queries
, sizeof(f34_queries
));
566 dev_err(&fn
->dev
, "Failed to read F34 config ID\n");
570 snprintf(f34
->configuration_id
, sizeof(f34
->configuration_id
),
572 f34_queries
[0], f34_queries
[1],
573 f34_queries
[2], f34_queries
[3]);
575 rmi_dbg(RMI_DEBUG_FN
, &fn
->dev
, "Configuration ID: %s\n",
576 f34
->configuration_id
);
582 int rmi_f34_create_sysfs(struct rmi_device
*rmi_dev
)
584 return sysfs_create_group(&rmi_dev
->dev
.kobj
, &rmi_firmware_attr_group
);
587 void rmi_f34_remove_sysfs(struct rmi_device
*rmi_dev
)
589 sysfs_remove_group(&rmi_dev
->dev
.kobj
, &rmi_firmware_attr_group
);
592 struct rmi_function_handler rmi_f34_handler
= {
597 .probe
= rmi_f34_probe
,
598 .attention
= rmi_f34_attention
,