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 <asm/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
+
118 f34
->v7
.off
.flash_status
,
119 &status
, sizeof(status
));
120 rmi_dbg(RMI_DEBUG_FN
, &fn
->dev
, "%s: status: %#02x, ret: %d\n",
121 __func__
, status
, ret
);
123 if (!ret
&& !(status
& 0x1f))
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
);
251 BUILD_BUG_ON(offsetof(struct rmi_f34_firmware
, data
) !=
252 F34_FW_IMAGE_OFFSET
);
254 rmi_dbg(RMI_DEBUG_FN
, &f34
->fn
->dev
,
255 "FW size:%zd, checksum:%08x, image_size:%d, config_size:%d\n",
257 le32_to_cpu(syn_fw
->checksum
),
258 image_size
, config_size
);
260 rmi_dbg(RMI_DEBUG_FN
, &f34
->fn
->dev
,
261 "FW bootloader_id:%02x, product_id:%.*s, info: %02x%02x\n",
262 syn_fw
->bootloader_version
,
263 (int)sizeof(syn_fw
->product_id
), syn_fw
->product_id
,
264 syn_fw
->product_info
[0], syn_fw
->product_info
[1]);
266 if (image_size
&& image_size
!= f34
->v5
.fw_blocks
* f34
->v5
.block_size
) {
267 dev_err(&f34
->fn
->dev
,
268 "Bad firmware image: fw size %d, expected %d\n",
269 image_size
, f34
->v5
.fw_blocks
* f34
->v5
.block_size
);
275 config_size
!= f34
->v5
.config_blocks
* f34
->v5
.block_size
) {
276 dev_err(&f34
->fn
->dev
,
277 "Bad firmware image: config size %d, expected %d\n",
279 f34
->v5
.config_blocks
* f34
->v5
.block_size
);
284 if (image_size
&& !config_size
) {
285 dev_err(&f34
->fn
->dev
, "Bad firmware image: no config data\n");
290 dev_info(&f34
->fn
->dev
, "Firmware image OK\n");
291 mutex_lock(&f34
->v5
.flash_mutex
);
293 ret
= rmi_f34_flash_firmware(f34
, syn_fw
);
295 mutex_unlock(&f34
->v5
.flash_mutex
);
301 static int rmi_f34_status(struct rmi_function
*fn
)
303 struct f34_data
*f34
= dev_get_drvdata(&fn
->dev
);
306 * The status is the percentage complete, or once complete,
307 * zero for success or a negative return code.
309 return f34
->update_status
;
312 static ssize_t
rmi_driver_bootloader_id_show(struct device
*dev
,
313 struct device_attribute
*dattr
,
316 struct rmi_driver_data
*data
= dev_get_drvdata(dev
);
317 struct rmi_function
*fn
= data
->f34_container
;
318 struct f34_data
*f34
;
321 f34
= dev_get_drvdata(&fn
->dev
);
323 if (f34
->bl_version
== 5)
324 return scnprintf(buf
, PAGE_SIZE
, "%c%c\n",
325 f34
->bootloader_id
[0],
326 f34
->bootloader_id
[1]);
328 return scnprintf(buf
, PAGE_SIZE
, "V%d.%d\n",
329 f34
->bootloader_id
[1],
330 f34
->bootloader_id
[0]);
336 static DEVICE_ATTR(bootloader_id
, 0444, rmi_driver_bootloader_id_show
, NULL
);
338 static ssize_t
rmi_driver_configuration_id_show(struct device
*dev
,
339 struct device_attribute
*dattr
,
342 struct rmi_driver_data
*data
= dev_get_drvdata(dev
);
343 struct rmi_function
*fn
= data
->f34_container
;
344 struct f34_data
*f34
;
347 f34
= dev_get_drvdata(&fn
->dev
);
349 return scnprintf(buf
, PAGE_SIZE
, "%s\n", f34
->configuration_id
);
355 static DEVICE_ATTR(configuration_id
, 0444,
356 rmi_driver_configuration_id_show
, NULL
);
358 static int rmi_firmware_update(struct rmi_driver_data
*data
,
359 const struct firmware
*fw
)
361 struct rmi_device
*rmi_dev
= data
->rmi_dev
;
362 struct device
*dev
= &rmi_dev
->dev
;
363 struct f34_data
*f34
;
366 if (!data
->f34_container
) {
367 dev_warn(dev
, "%s: No F34 present!\n", __func__
);
371 f34
= dev_get_drvdata(&data
->f34_container
->dev
);
373 if (f34
->bl_version
== 7) {
374 if (data
->pdt_props
& HAS_BSR
) {
375 dev_err(dev
, "%s: LTS not supported\n", __func__
);
378 } else if (f34
->bl_version
!= 5) {
379 dev_warn(dev
, "F34 V%d not supported!\n",
380 data
->f34_container
->fd
.function_version
);
384 /* Enter flash mode */
385 if (f34
->bl_version
== 7)
386 ret
= rmi_f34v7_start_reflash(f34
, fw
);
388 ret
= rmi_f34_enable_flash(f34
);
392 rmi_disable_irq(rmi_dev
, false);
394 /* Tear down functions and re-probe */
395 rmi_free_function_list(rmi_dev
);
397 ret
= rmi_probe_interrupts(data
);
401 ret
= rmi_init_functions(data
);
405 if (!data
->bootloader_mode
|| !data
->f34_container
) {
406 dev_warn(dev
, "%s: No F34 present or not in bootloader!\n",
411 rmi_enable_irq(rmi_dev
, false);
413 f34
= dev_get_drvdata(&data
->f34_container
->dev
);
415 /* Perform firmware update */
416 if (f34
->bl_version
== 7)
417 ret
= rmi_f34v7_do_reflash(f34
, fw
);
419 ret
= rmi_f34_update_firmware(f34
, fw
);
422 f34
->update_status
= ret
;
423 dev_err(&f34
->fn
->dev
,
424 "Firmware update failed, status: %d\n", ret
);
426 dev_info(&f34
->fn
->dev
, "Firmware update complete\n");
429 rmi_disable_irq(rmi_dev
, false);
432 rmi_dbg(RMI_DEBUG_FN
, dev
, "Re-probing device\n");
433 rmi_free_function_list(rmi_dev
);
435 ret
= rmi_scan_pdt(rmi_dev
, NULL
, rmi_initial_reset
);
437 dev_warn(dev
, "RMI reset failed!\n");
439 ret
= rmi_probe_interrupts(data
);
443 ret
= rmi_init_functions(data
);
447 rmi_enable_irq(rmi_dev
, false);
449 if (data
->f01_container
->dev
.driver
)
450 /* Driver already bound, so enable ATTN now. */
451 return rmi_enable_sensor(rmi_dev
);
453 rmi_dbg(RMI_DEBUG_FN
, dev
, "%s complete\n", __func__
);
458 static ssize_t
rmi_driver_update_fw_store(struct device
*dev
,
459 struct device_attribute
*dattr
,
460 const char *buf
, size_t count
)
462 struct rmi_driver_data
*data
= dev_get_drvdata(dev
);
463 char fw_name
[NAME_MAX
];
464 const struct firmware
*fw
;
465 size_t copy_count
= count
;
468 if (count
== 0 || count
>= NAME_MAX
)
471 if (buf
[count
- 1] == '\0' || buf
[count
- 1] == '\n')
474 strncpy(fw_name
, buf
, copy_count
);
475 fw_name
[copy_count
] = '\0';
477 ret
= request_firmware(&fw
, fw_name
, dev
);
481 dev_info(dev
, "Flashing %s\n", fw_name
);
483 ret
= rmi_firmware_update(data
, fw
);
485 release_firmware(fw
);
490 static DEVICE_ATTR(update_fw
, 0200, NULL
, rmi_driver_update_fw_store
);
492 static ssize_t
rmi_driver_update_fw_status_show(struct device
*dev
,
493 struct device_attribute
*dattr
,
496 struct rmi_driver_data
*data
= dev_get_drvdata(dev
);
497 int update_status
= 0;
499 if (data
->f34_container
)
500 update_status
= rmi_f34_status(data
->f34_container
);
502 return scnprintf(buf
, PAGE_SIZE
, "%d\n", update_status
);
505 static DEVICE_ATTR(update_fw_status
, 0444,
506 rmi_driver_update_fw_status_show
, NULL
);
508 static struct attribute
*rmi_firmware_attrs
[] = {
509 &dev_attr_bootloader_id
.attr
,
510 &dev_attr_configuration_id
.attr
,
511 &dev_attr_update_fw
.attr
,
512 &dev_attr_update_fw_status
.attr
,
516 static const struct attribute_group rmi_firmware_attr_group
= {
517 .attrs
= rmi_firmware_attrs
,
520 static int rmi_f34_probe(struct rmi_function
*fn
)
522 struct f34_data
*f34
;
523 unsigned char f34_queries
[9];
525 u8 version
= fn
->fd
.function_version
;
528 f34
= devm_kzalloc(&fn
->dev
, sizeof(struct f34_data
), GFP_KERNEL
);
533 dev_set_drvdata(&fn
->dev
, f34
);
535 /* v5 code only supported version 0, try V7 probe */
537 return rmi_f34v7_probe(f34
);
541 ret
= rmi_read_block(fn
->rmi_dev
, fn
->fd
.query_base_addr
,
542 f34_queries
, sizeof(f34_queries
));
544 dev_err(&fn
->dev
, "%s: Failed to query properties\n",
549 snprintf(f34
->bootloader_id
, sizeof(f34
->bootloader_id
),
550 "%c%c", f34_queries
[0], f34_queries
[1]);
552 mutex_init(&f34
->v5
.flash_mutex
);
553 init_completion(&f34
->v5
.cmd_done
);
555 f34
->v5
.block_size
= get_unaligned_le16(&f34_queries
[3]);
556 f34
->v5
.fw_blocks
= get_unaligned_le16(&f34_queries
[5]);
557 f34
->v5
.config_blocks
= get_unaligned_le16(&f34_queries
[7]);
558 f34
->v5
.ctrl_address
= fn
->fd
.data_base_addr
+ F34_BLOCK_DATA_OFFSET
+
560 has_config_id
= f34_queries
[2] & (1 << 2);
562 rmi_dbg(RMI_DEBUG_FN
, &fn
->dev
, "Bootloader ID: %s\n",
564 rmi_dbg(RMI_DEBUG_FN
, &fn
->dev
, "Block size: %d\n",
566 rmi_dbg(RMI_DEBUG_FN
, &fn
->dev
, "FW blocks: %d\n",
568 rmi_dbg(RMI_DEBUG_FN
, &fn
->dev
, "CFG blocks: %d\n",
569 f34
->v5
.config_blocks
);
572 ret
= rmi_read_block(fn
->rmi_dev
, fn
->fd
.control_base_addr
,
573 f34_queries
, sizeof(f34_queries
));
575 dev_err(&fn
->dev
, "Failed to read F34 config ID\n");
579 snprintf(f34
->configuration_id
, sizeof(f34
->configuration_id
),
581 f34_queries
[0], f34_queries
[1],
582 f34_queries
[2], f34_queries
[3]);
584 rmi_dbg(RMI_DEBUG_FN
, &fn
->dev
, "Configuration ID: %s\n",
585 f34
->configuration_id
);
591 int rmi_f34_create_sysfs(struct rmi_device
*rmi_dev
)
593 return sysfs_create_group(&rmi_dev
->dev
.kobj
, &rmi_firmware_attr_group
);
596 void rmi_f34_remove_sysfs(struct rmi_device
*rmi_dev
)
598 sysfs_remove_group(&rmi_dev
->dev
.kobj
, &rmi_firmware_attr_group
);
601 struct rmi_function_handler rmi_f34_handler
= {
606 .probe
= rmi_f34_probe
,
607 .attention
= rmi_f34_attention
,