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 <asm/unaligned.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
);
108 ret
= rmi_read(f34
->fn
->rmi_dev
, f34
->v5
.ctrl_address
, &f34
->v5
.status
);
109 rmi_dbg(RMI_DEBUG_FN
, &fn
->dev
, "%s: status: %#02x, ret: %d\n",
110 __func__
, f34
->v5
.status
, ret
);
112 if (!ret
&& !(f34
->v5
.status
& 0x7f))
113 complete(&f34
->v5
.cmd_done
);
118 static int rmi_f34_write_blocks(struct f34_data
*f34
, const void *data
,
119 int block_count
, u8 command
)
121 struct rmi_function
*fn
= f34
->fn
;
122 struct rmi_device
*rmi_dev
= fn
->rmi_dev
;
123 u16 address
= fn
->fd
.data_base_addr
+ F34_BLOCK_DATA_OFFSET
;
124 u8 start_address
[] = { 0, 0 };
128 ret
= rmi_write_block(rmi_dev
, fn
->fd
.data_base_addr
,
129 start_address
, sizeof(start_address
));
131 dev_err(&fn
->dev
, "Failed to write initial zeros: %d\n", ret
);
135 for (i
= 0; i
< block_count
; i
++) {
136 ret
= rmi_write_block(rmi_dev
, address
,
137 data
, f34
->v5
.block_size
);
140 "failed to write block #%d: %d\n", i
, ret
);
144 ret
= rmi_f34_command(f34
, command
, F34_IDLE_WAIT_MS
, false);
147 "Failed to write command for block #%d: %d\n",
152 rmi_dbg(RMI_DEBUG_FN
, &fn
->dev
, "wrote block %d of %d\n",
155 data
+= f34
->v5
.block_size
;
161 static int rmi_f34_write_firmware(struct f34_data
*f34
, const void *data
)
163 return rmi_f34_write_blocks(f34
, data
, f34
->v5
.fw_blocks
,
167 static int rmi_f34_write_config(struct f34_data
*f34
, const void *data
)
169 return rmi_f34_write_blocks(f34
, data
, f34
->v5
.config_blocks
,
170 F34_WRITE_CONFIG_BLOCK
);
173 int rmi_f34_enable_flash(struct f34_data
*f34
)
175 return rmi_f34_command(f34
, F34_ENABLE_FLASH_PROG
,
176 F34_ENABLE_WAIT_MS
, true);
179 static int rmi_f34_flash_firmware(struct f34_data
*f34
,
180 const struct rmi_f34_firmware
*syn_fw
)
182 struct rmi_function
*fn
= f34
->fn
;
185 if (syn_fw
->image_size
) {
186 dev_info(&fn
->dev
, "Erasing firmware...\n");
187 ret
= rmi_f34_command(f34
, F34_ERASE_ALL
,
188 F34_ERASE_WAIT_MS
, true);
192 dev_info(&fn
->dev
, "Writing firmware (%d bytes)...\n",
194 ret
= rmi_f34_write_firmware(f34
, syn_fw
->data
);
199 if (syn_fw
->config_size
) {
201 * We only need to erase config if we haven't updated
204 if (!syn_fw
->image_size
) {
205 dev_info(&fn
->dev
, "Erasing config...\n");
206 ret
= rmi_f34_command(f34
, F34_ERASE_CONFIG
,
207 F34_ERASE_WAIT_MS
, true);
212 dev_info(&fn
->dev
, "Writing config (%d bytes)...\n",
213 syn_fw
->config_size
);
214 ret
= rmi_f34_write_config(f34
,
215 &syn_fw
->data
[syn_fw
->image_size
]);
223 int rmi_f34_update_firmware(struct f34_data
*f34
, const struct firmware
*fw
)
225 const struct rmi_f34_firmware
*syn_fw
;
228 syn_fw
= (const struct rmi_f34_firmware
*)fw
->data
;
229 BUILD_BUG_ON(offsetof(struct rmi_f34_firmware
, data
) !=
230 F34_FW_IMAGE_OFFSET
);
232 rmi_dbg(RMI_DEBUG_FN
, &f34
->fn
->dev
,
233 "FW size:%d, checksum:%08x, image_size:%d, config_size:%d\n",
235 le32_to_cpu(syn_fw
->checksum
),
236 le32_to_cpu(syn_fw
->image_size
),
237 le32_to_cpu(syn_fw
->config_size
));
239 rmi_dbg(RMI_DEBUG_FN
, &f34
->fn
->dev
,
240 "FW bootloader_id:%02x, product_id:%.*s, info: %02x%02x\n",
241 syn_fw
->bootloader_version
,
242 (int)sizeof(syn_fw
->product_id
), syn_fw
->product_id
,
243 syn_fw
->product_info
[0], syn_fw
->product_info
[1]);
245 if (syn_fw
->image_size
&&
246 syn_fw
->image_size
!= f34
->v5
.fw_blocks
* f34
->v5
.block_size
) {
247 dev_err(&f34
->fn
->dev
,
248 "Bad firmware image: fw size %d, expected %d\n",
250 f34
->v5
.fw_blocks
* f34
->v5
.block_size
);
255 if (syn_fw
->config_size
&&
256 syn_fw
->config_size
!= f34
->v5
.config_blocks
* f34
->v5
.block_size
) {
257 dev_err(&f34
->fn
->dev
,
258 "Bad firmware image: config size %d, expected %d\n",
260 f34
->v5
.config_blocks
* f34
->v5
.block_size
);
265 if (syn_fw
->image_size
&& !syn_fw
->config_size
) {
266 dev_err(&f34
->fn
->dev
, "Bad firmware image: no config data\n");
271 dev_info(&f34
->fn
->dev
, "Firmware image OK\n");
272 mutex_lock(&f34
->v5
.flash_mutex
);
274 ret
= rmi_f34_flash_firmware(f34
, syn_fw
);
276 mutex_unlock(&f34
->v5
.flash_mutex
);
282 static int rmi_firmware_update(struct rmi_driver_data
*data
,
283 const struct firmware
*fw
)
285 struct rmi_device
*rmi_dev
= data
->rmi_dev
;
286 struct device
*dev
= &rmi_dev
->dev
;
287 struct f34_data
*f34
;
290 if (!data
->f34_container
) {
291 dev_warn(dev
, "%s: No F34 present!\n", __func__
);
295 /* Only version 0 currently supported */
296 if (data
->f34_container
->fd
.function_version
!= 0) {
297 dev_warn(dev
, "F34 V%d not supported!\n",
298 data
->f34_container
->fd
.function_version
);
302 f34
= dev_get_drvdata(&data
->f34_container
->dev
);
304 /* Enter flash mode */
305 ret
= rmi_f34_enable_flash(f34
);
309 rmi_disable_irq(rmi_dev
, false);
311 /* Tear down functions and re-probe */
312 rmi_free_function_list(rmi_dev
);
314 ret
= rmi_probe_interrupts(data
);
318 ret
= rmi_init_functions(data
);
322 if (!data
->f01_bootloader_mode
|| !data
->f34_container
) {
323 dev_warn(dev
, "%s: No F34 present or not in bootloader!\n",
328 rmi_enable_irq(rmi_dev
, false);
330 f34
= dev_get_drvdata(&data
->f34_container
->dev
);
332 /* Perform firmware update */
333 ret
= rmi_f34_update_firmware(f34
, fw
);
335 dev_info(&f34
->fn
->dev
, "Firmware update complete, status:%d\n", ret
);
337 rmi_disable_irq(rmi_dev
, false);
340 rmi_dbg(RMI_DEBUG_FN
, dev
, "Re-probing device\n");
341 rmi_free_function_list(rmi_dev
);
343 ret
= rmi_scan_pdt(rmi_dev
, NULL
, rmi_initial_reset
);
345 dev_warn(dev
, "RMI reset failed!\n");
347 ret
= rmi_probe_interrupts(data
);
351 ret
= rmi_init_functions(data
);
355 rmi_enable_irq(rmi_dev
, false);
357 if (data
->f01_container
->dev
.driver
)
358 /* Driver already bound, so enable ATTN now. */
359 return rmi_enable_sensor(rmi_dev
);
361 rmi_dbg(RMI_DEBUG_FN
, dev
, "%s complete\n", __func__
);
366 static ssize_t
rmi_driver_update_fw_store(struct device
*dev
,
367 struct device_attribute
*dattr
,
368 const char *buf
, size_t count
)
370 struct rmi_driver_data
*data
= dev_get_drvdata(dev
);
371 char fw_name
[NAME_MAX
];
372 const struct firmware
*fw
;
373 size_t copy_count
= count
;
376 if (count
== 0 || count
>= NAME_MAX
)
379 if (buf
[count
- 1] == '\0' || buf
[count
- 1] == '\n')
382 strncpy(fw_name
, buf
, copy_count
);
383 fw_name
[copy_count
] = '\0';
385 ret
= request_firmware(&fw
, fw_name
, dev
);
389 dev_info(dev
, "Flashing %s\n", fw_name
);
391 ret
= rmi_firmware_update(data
, fw
);
393 release_firmware(fw
);
398 static DEVICE_ATTR(update_fw
, 0200, NULL
, rmi_driver_update_fw_store
);
400 static struct attribute
*rmi_firmware_attrs
[] = {
401 &dev_attr_update_fw
.attr
,
405 static struct attribute_group rmi_firmware_attr_group
= {
406 .attrs
= rmi_firmware_attrs
,
409 static int rmi_f34_probe(struct rmi_function
*fn
)
411 struct f34_data
*f34
;
412 unsigned char f34_queries
[9];
416 f34
= devm_kzalloc(&fn
->dev
, sizeof(struct f34_data
), GFP_KERNEL
);
421 dev_set_drvdata(&fn
->dev
, f34
);
423 ret
= rmi_read_block(fn
->rmi_dev
, fn
->fd
.query_base_addr
,
424 f34_queries
, sizeof(f34_queries
));
426 dev_err(&fn
->dev
, "%s: Failed to query properties\n",
431 snprintf(f34
->bootloader_id
, sizeof(f34
->bootloader_id
),
432 "%c%c", f34_queries
[0], f34_queries
[1]);
434 mutex_init(&f34
->v5
.flash_mutex
);
435 init_completion(&f34
->v5
.cmd_done
);
437 f34
->v5
.block_size
= get_unaligned_le16(&f34_queries
[3]);
438 f34
->v5
.fw_blocks
= get_unaligned_le16(&f34_queries
[5]);
439 f34
->v5
.config_blocks
= get_unaligned_le16(&f34_queries
[7]);
440 f34
->v5
.ctrl_address
= fn
->fd
.data_base_addr
+ F34_BLOCK_DATA_OFFSET
+
442 has_config_id
= f34_queries
[2] & (1 << 2);
444 rmi_dbg(RMI_DEBUG_FN
, &fn
->dev
, "Bootloader ID: %s\n",
446 rmi_dbg(RMI_DEBUG_FN
, &fn
->dev
, "Block size: %d\n",
448 rmi_dbg(RMI_DEBUG_FN
, &fn
->dev
, "FW blocks: %d\n",
450 rmi_dbg(RMI_DEBUG_FN
, &fn
->dev
, "CFG blocks: %d\n",
451 f34
->v5
.config_blocks
);
454 ret
= rmi_read_block(fn
->rmi_dev
, fn
->fd
.control_base_addr
,
455 f34_queries
, sizeof(f34_queries
));
457 dev_err(&fn
->dev
, "Failed to read F34 config ID\n");
461 snprintf(f34
->configuration_id
, sizeof(f34
->configuration_id
),
463 f34_queries
[0], f34_queries
[1],
464 f34_queries
[2], f34_queries
[3]);
466 rmi_dbg(RMI_DEBUG_FN
, &fn
->dev
, "Configuration ID: %s\n",
467 f34
->configuration_id
);
473 int rmi_f34_create_sysfs(struct rmi_device
*rmi_dev
)
475 return sysfs_create_group(&rmi_dev
->dev
.kobj
, &rmi_firmware_attr_group
);
478 void rmi_f34_remove_sysfs(struct rmi_device
*rmi_dev
)
480 sysfs_remove_group(&rmi_dev
->dev
.kobj
, &rmi_firmware_attr_group
);
483 struct rmi_function_handler rmi_f34_handler
= {
488 .probe
= rmi_f34_probe
,
489 .attention
= rmi_f34_attention
,