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 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
);
110 if (f34
->bl_version
== 5) {
111 ret
= rmi_read(f34
->fn
->rmi_dev
, f34
->v5
.ctrl_address
,
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
);
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
);
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 };
143 ret
= rmi_write_block(rmi_dev
, fn
->fd
.data_base_addr
,
144 start_address
, sizeof(start_address
));
146 dev_err(&fn
->dev
, "Failed to write initial zeros: %d\n", ret
);
150 for (i
= 0; i
< block_count
; i
++) {
151 ret
= rmi_write_block(rmi_dev
, address
,
152 data
, f34
->v5
.block_size
);
155 "failed to write block #%d: %d\n", i
, ret
);
159 ret
= rmi_f34_command(f34
, command
, F34_IDLE_WAIT_MS
, false);
162 "Failed to write command for block #%d: %d\n",
167 rmi_dbg(RMI_DEBUG_FN
, &fn
->dev
, "wrote block %d of %d\n",
170 data
+= f34
->v5
.block_size
;
171 f34
->update_progress
+= f34
->v5
.block_size
;
172 f34
->update_status
= (f34
->update_progress
* 100) /
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
,
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
);
205 f34
->update_progress
= 0;
206 f34
->update_size
= image_size
+ config_size
;
209 dev_info(&fn
->dev
, "Erasing firmware...\n");
210 ret
= rmi_f34_command(f34
, F34_ERASE_ALL
,
211 F34_ERASE_WAIT_MS
, true);
215 dev_info(&fn
->dev
, "Writing firmware (%d bytes)...\n",
217 ret
= rmi_f34_write_firmware(f34
, syn_fw
->data
);
224 * We only need to erase config if we haven't updated
228 dev_info(&fn
->dev
, "Erasing config...\n");
229 ret
= rmi_f34_command(f34
, F34_ERASE_CONFIG
,
230 F34_ERASE_WAIT_MS
, true);
235 dev_info(&fn
->dev
, "Writing config (%d bytes)...\n",
237 ret
= rmi_f34_write_config(f34
, &syn_fw
->data
[image_size
]);
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
);
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",
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
);
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",
282 f34
->v5
.config_blocks
* f34
->v5
.block_size
);
287 if (image_size
&& !config_size
) {
288 dev_err(&f34
->fn
->dev
, "Bad firmware image: no config data\n");
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
);
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
,
319 struct rmi_driver_data
*data
= dev_get_drvdata(dev
);
320 struct rmi_function
*fn
= data
->f34_container
;
321 struct f34_data
*f34
;
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]);
331 return scnprintf(buf
, PAGE_SIZE
, "V%d.%d\n",
332 f34
->bootloader_id
[1],
333 f34
->bootloader_id
[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
,
345 struct rmi_driver_data
*data
= dev_get_drvdata(dev
);
346 struct rmi_function
*fn
= data
->f34_container
;
347 struct f34_data
*f34
;
350 f34
= dev_get_drvdata(&fn
->dev
);
352 return scnprintf(buf
, PAGE_SIZE
, "%s\n", f34
->configuration_id
);
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
;
369 if (!data
->f34_container
) {
370 dev_warn(dev
, "%s: No F34 present!\n", __func__
);
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__
);
381 } else if (f34
->bl_version
!= 5) {
382 dev_warn(dev
, "F34 V%d not supported!\n",
383 data
->f34_container
->fd
.function_version
);
387 /* Enter flash mode */
388 if (f34
->bl_version
== 7)
389 ret
= rmi_f34v7_start_reflash(f34
, fw
);
391 ret
= rmi_f34_enable_flash(f34
);
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
);
404 ret
= rmi_init_functions(data
);
408 if (!data
->bootloader_mode
|| !data
->f34_container
) {
409 dev_warn(dev
, "%s: No F34 present or not in bootloader!\n",
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
);
422 ret
= rmi_f34_update_firmware(f34
, fw
);
425 f34
->update_status
= ret
;
426 dev_err(&f34
->fn
->dev
,
427 "Firmware update failed, status: %d\n", ret
);
429 dev_info(&f34
->fn
->dev
, "Firmware update complete\n");
432 rmi_disable_irq(rmi_dev
, false);
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
);
440 dev_warn(dev
, "RMI reset failed!\n");
442 ret
= rmi_probe_interrupts(data
);
446 ret
= rmi_init_functions(data
);
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__
);
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
;
471 if (count
== 0 || count
>= NAME_MAX
)
474 if (buf
[count
- 1] == '\0' || buf
[count
- 1] == '\n')
477 strncpy(fw_name
, buf
, copy_count
);
478 fw_name
[copy_count
] = '\0';
480 ret
= request_firmware(&fw
, fw_name
, dev
);
484 dev_info(dev
, "Flashing %s\n", fw_name
);
486 ret
= rmi_firmware_update(data
, fw
);
488 release_firmware(fw
);
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
,
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
,
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];
528 u8 version
= fn
->fd
.function_version
;
531 f34
= devm_kzalloc(&fn
->dev
, sizeof(struct f34_data
), GFP_KERNEL
);
536 dev_set_drvdata(&fn
->dev
, f34
);
538 /* v5 code only supported version 0, try V7 probe */
540 return rmi_f34v7_probe(f34
);
544 ret
= rmi_read_block(fn
->rmi_dev
, fn
->fd
.query_base_addr
,
545 f34_queries
, sizeof(f34_queries
));
547 dev_err(&fn
->dev
, "%s: Failed to query properties\n",
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
+
563 has_config_id
= f34_queries
[2] & (1 << 2);
565 rmi_dbg(RMI_DEBUG_FN
, &fn
->dev
, "Bootloader ID: %s\n",
567 rmi_dbg(RMI_DEBUG_FN
, &fn
->dev
, "Block size: %d\n",
569 rmi_dbg(RMI_DEBUG_FN
, &fn
->dev
, "FW blocks: %d\n",
571 rmi_dbg(RMI_DEBUG_FN
, &fn
->dev
, "CFG blocks: %d\n",
572 f34
->v5
.config_blocks
);
575 ret
= rmi_read_block(fn
->rmi_dev
, fn
->fd
.control_base_addr
,
576 f34_queries
, sizeof(f34_queries
));
578 dev_err(&fn
->dev
, "Failed to read F34 config ID\n");
582 snprintf(f34
->configuration_id
, sizeof(f34
->configuration_id
),
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
);
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
= {
609 .probe
= rmi_f34_probe
,
610 .attention
= rmi_f34_attention
,