xref: /linux/drivers/input/rmi4/rmi_f34.c (revision 3e51108c72e8adbcf3180ed40527a2a9d2d0123b)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2007-2016, Synaptics Incorporated
4  * Copyright (C) 2016 Zodiac Inflight Innovations
5  */
6 
7 #include <linux/kernel.h>
8 #include <linux/rmi.h>
9 #include <linux/firmware.h>
10 #include <linux/unaligned.h>
11 #include <linux/bitops.h>
12 
13 #include "rmi_driver.h"
14 #include "rmi_f34.h"
15 
rmi_f34_write_bootloader_id(struct f34_data * f34)16 static int rmi_f34_write_bootloader_id(struct f34_data *f34)
17 {
18 	struct rmi_function *fn = f34->fn;
19 	struct rmi_device *rmi_dev = fn->rmi_dev;
20 	u8 bootloader_id[F34_BOOTLOADER_ID_LEN];
21 	int ret;
22 
23 	ret = rmi_read_block(rmi_dev, fn->fd.query_base_addr,
24 			     bootloader_id, sizeof(bootloader_id));
25 	if (ret) {
26 		dev_err(&fn->dev, "%s: Reading bootloader ID failed: %d\n",
27 				__func__, ret);
28 		return ret;
29 	}
30 
31 	rmi_dbg(RMI_DEBUG_FN, &fn->dev, "%s: writing bootloader id '%c%c'\n",
32 			__func__, bootloader_id[0], bootloader_id[1]);
33 
34 	ret = rmi_write_block(rmi_dev,
35 			      fn->fd.data_base_addr + F34_BLOCK_DATA_OFFSET,
36 			      bootloader_id, sizeof(bootloader_id));
37 	if (ret) {
38 		dev_err(&fn->dev, "Failed to write bootloader ID: %d\n", ret);
39 		return ret;
40 	}
41 
42 	return 0;
43 }
44 
rmi_f34_command(struct f34_data * f34,u8 command,unsigned int timeout,bool write_bl_id)45 static int rmi_f34_command(struct f34_data *f34, u8 command,
46 			   unsigned int timeout, bool write_bl_id)
47 {
48 	struct rmi_function *fn = f34->fn;
49 	struct rmi_device *rmi_dev = fn->rmi_dev;
50 	int ret;
51 
52 	if (write_bl_id) {
53 		ret = rmi_f34_write_bootloader_id(f34);
54 		if (ret)
55 			return ret;
56 	}
57 
58 	init_completion(&f34->v5.cmd_done);
59 
60 	ret = rmi_read(rmi_dev, f34->v5.ctrl_address, &f34->v5.status);
61 	if (ret) {
62 		dev_err(&f34->fn->dev,
63 			"%s: Failed to read cmd register: %d (command %#02x)\n",
64 			__func__, ret, command);
65 		return ret;
66 	}
67 
68 	f34->v5.status |= command & 0x0f;
69 
70 	ret = rmi_write(rmi_dev, f34->v5.ctrl_address, f34->v5.status);
71 	if (ret < 0) {
72 		dev_err(&f34->fn->dev,
73 			"Failed to write F34 command %#02x: %d\n",
74 			command, ret);
75 		return ret;
76 	}
77 
78 	if (!wait_for_completion_timeout(&f34->v5.cmd_done,
79 				msecs_to_jiffies(timeout))) {
80 
81 		ret = rmi_read(rmi_dev, f34->v5.ctrl_address, &f34->v5.status);
82 		if (ret) {
83 			dev_err(&f34->fn->dev,
84 				"%s: cmd %#02x timed out: %d\n",
85 				__func__, command, ret);
86 			return ret;
87 		}
88 
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);
93 			return -ETIMEDOUT;
94 		}
95 	}
96 
97 	return 0;
98 }
99 
rmi_f34_attention(int irq,void * ctx)100 static irqreturn_t rmi_f34_attention(int irq, void *ctx)
101 {
102 	struct rmi_function *fn = ctx;
103 	struct f34_data *f34 = dev_get_drvdata(&fn->dev);
104 	int ret;
105 	u8 status;
106 
107 	if (f34->bl_version == 5) {
108 		ret = rmi_read(f34->fn->rmi_dev, f34->v5.ctrl_address,
109 			       &status);
110 		rmi_dbg(RMI_DEBUG_FN, &fn->dev, "%s: status: %#02x, ret: %d\n",
111 			__func__, status, ret);
112 
113 		if (!ret && !(status & 0x7f))
114 			complete(&f34->v5.cmd_done);
115 	} else {
116 		ret = rmi_read_block(f34->fn->rmi_dev,
117 					f34->fn->fd.data_base_addr +
118 						V7_COMMAND_OFFSET,
119 					&status, sizeof(status));
120 		rmi_dbg(RMI_DEBUG_FN, &f34->fn->dev, "%s: cmd: %#02x, ret: %d\n",
121 			__func__, status, ret);
122 
123 		if (!ret && status == CMD_V7_IDLE)
124 			complete(&f34->v7.cmd_done);
125 	}
126 
127 	return IRQ_HANDLED;
128 }
129 
rmi_f34_write_blocks(struct f34_data * f34,const void * data,int block_count,u8 command)130 static int rmi_f34_write_blocks(struct f34_data *f34, const void *data,
131 				int block_count, u8 command)
132 {
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 };
137 	int i;
138 	int ret;
139 
140 	ret = rmi_write_block(rmi_dev, fn->fd.data_base_addr,
141 			      start_address, sizeof(start_address));
142 	if (ret) {
143 		dev_err(&fn->dev, "Failed to write initial zeros: %d\n", ret);
144 		return ret;
145 	}
146 
147 	for (i = 0; i < block_count; i++) {
148 		ret = rmi_write_block(rmi_dev, address,
149 				      data, f34->v5.block_size);
150 		if (ret) {
151 			dev_err(&fn->dev,
152 				"failed to write block #%d: %d\n", i, ret);
153 			return ret;
154 		}
155 
156 		ret = rmi_f34_command(f34, command, F34_IDLE_WAIT_MS, false);
157 		if (ret) {
158 			dev_err(&fn->dev,
159 				"Failed to write command for block #%d: %d\n",
160 				i, ret);
161 			return ret;
162 		}
163 
164 		rmi_dbg(RMI_DEBUG_FN, &fn->dev, "wrote block %d of %d\n",
165 			i + 1, block_count);
166 
167 		data += f34->v5.block_size;
168 		f34->update_progress += f34->v5.block_size;
169 		f34->update_status = (f34->update_progress * 100) /
170 			f34->update_size;
171 	}
172 
173 	return 0;
174 }
175 
rmi_f34_write_firmware(struct f34_data * f34,const void * data)176 static int rmi_f34_write_firmware(struct f34_data *f34, const void *data)
177 {
178 	return rmi_f34_write_blocks(f34, data, f34->v5.fw_blocks,
179 				    F34_WRITE_FW_BLOCK);
180 }
181 
rmi_f34_write_config(struct f34_data * f34,const void * data)182 static int rmi_f34_write_config(struct f34_data *f34, const void *data)
183 {
184 	return rmi_f34_write_blocks(f34, data, f34->v5.config_blocks,
185 				    F34_WRITE_CONFIG_BLOCK);
186 }
187 
rmi_f34_enable_flash(struct f34_data * f34)188 static int rmi_f34_enable_flash(struct f34_data *f34)
189 {
190 	return rmi_f34_command(f34, F34_ENABLE_FLASH_PROG,
191 			       F34_ENABLE_WAIT_MS, true);
192 }
193 
rmi_f34_flash_firmware(struct f34_data * f34,const struct rmi_f34_firmware * syn_fw)194 static int rmi_f34_flash_firmware(struct f34_data *f34,
195 				  const struct rmi_f34_firmware *syn_fw)
196 {
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);
200 	int ret;
201 
202 	f34->update_progress = 0;
203 	f34->update_size = image_size + config_size;
204 
205 	if (image_size) {
206 		dev_info(&fn->dev, "Erasing firmware...\n");
207 		ret = rmi_f34_command(f34, F34_ERASE_ALL,
208 				      F34_ERASE_WAIT_MS, true);
209 		if (ret)
210 			return ret;
211 
212 		dev_info(&fn->dev, "Writing firmware (%d bytes)...\n",
213 			 image_size);
214 		ret = rmi_f34_write_firmware(f34, syn_fw->data);
215 		if (ret)
216 			return ret;
217 	}
218 
219 	if (config_size) {
220 		/*
221 		 * We only need to erase config if we haven't updated
222 		 * firmware.
223 		 */
224 		if (!image_size) {
225 			dev_info(&fn->dev, "Erasing config...\n");
226 			ret = rmi_f34_command(f34, F34_ERASE_CONFIG,
227 					      F34_ERASE_WAIT_MS, true);
228 			if (ret)
229 				return ret;
230 		}
231 
232 		dev_info(&fn->dev, "Writing config (%d bytes)...\n",
233 			 config_size);
234 		ret = rmi_f34_write_config(f34, &syn_fw->data[image_size]);
235 		if (ret)
236 			return ret;
237 	}
238 
239 	return 0;
240 }
241 
rmi_f34_update_firmware(struct f34_data * f34,const struct firmware * fw)242 static int rmi_f34_update_firmware(struct f34_data *f34,
243 				   const struct firmware *fw)
244 {
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);
249 
250 	BUILD_BUG_ON(offsetof(struct rmi_f34_firmware, data) !=
251 			F34_FW_IMAGE_OFFSET);
252 
253 	rmi_dbg(RMI_DEBUG_FN, &f34->fn->dev,
254 		"FW size:%zd, checksum:%08x, image_size:%d, config_size:%d\n",
255 		fw->size,
256 		le32_to_cpu(syn_fw->checksum),
257 		image_size, config_size);
258 
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]);
264 
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);
269 		return -EILSEQ;
270 	}
271 
272 	if (config_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",
276 			config_size,
277 			f34->v5.config_blocks * f34->v5.block_size);
278 		return -EILSEQ;
279 	}
280 
281 	if (image_size && !config_size) {
282 		dev_err(&f34->fn->dev, "Bad firmware image: no config data\n");
283 		return -EILSEQ;
284 	}
285 
286 	dev_info(&f34->fn->dev, "Firmware image OK\n");
287 
288 	guard(mutex)(&f34->v5.flash_mutex);
289 	return rmi_f34_flash_firmware(f34, syn_fw);
290 }
291 
rmi_f34_status(struct rmi_function * fn)292 static int rmi_f34_status(struct rmi_function *fn)
293 {
294 	struct f34_data *f34 = dev_get_drvdata(&fn->dev);
295 
296 	/*
297 	 * The status is the percentage complete, or once complete,
298 	 * zero for success or a negative return code.
299 	 */
300 	return f34->update_status;
301 }
302 
rmi_driver_bootloader_id_show(struct device * dev,struct device_attribute * dattr,char * buf)303 static ssize_t rmi_driver_bootloader_id_show(struct device *dev,
304 					     struct device_attribute *dattr,
305 					     char *buf)
306 {
307 	struct rmi_driver_data *data = dev_get_drvdata(dev);
308 	struct rmi_function *fn = data->f34_container;
309 	struct f34_data *f34;
310 
311 	if (fn) {
312 		f34 = dev_get_drvdata(&fn->dev);
313 
314 		if (f34->bl_version == 5)
315 			return sysfs_emit(buf, "%c%c\n",
316 					  f34->bootloader_id[0],
317 					  f34->bootloader_id[1]);
318 		else
319 			return sysfs_emit(buf, "V%d.%d\n",
320 					  f34->bootloader_id[1],
321 					  f34->bootloader_id[0]);
322 	}
323 
324 	return 0;
325 }
326 
327 static DEVICE_ATTR(bootloader_id, 0444, rmi_driver_bootloader_id_show, NULL);
328 
rmi_driver_configuration_id_show(struct device * dev,struct device_attribute * dattr,char * buf)329 static ssize_t rmi_driver_configuration_id_show(struct device *dev,
330 						struct device_attribute *dattr,
331 						char *buf)
332 {
333 	struct rmi_driver_data *data = dev_get_drvdata(dev);
334 	struct rmi_function *fn = data->f34_container;
335 	struct f34_data *f34;
336 
337 	if (fn) {
338 		f34 = dev_get_drvdata(&fn->dev);
339 
340 		return sysfs_emit(buf, "%s\n", f34->configuration_id);
341 	}
342 
343 	return 0;
344 }
345 
346 static DEVICE_ATTR(configuration_id, 0444,
347 		   rmi_driver_configuration_id_show, NULL);
348 
rmi_firmware_update(struct rmi_driver_data * data,const struct firmware * fw)349 static int rmi_firmware_update(struct rmi_driver_data *data,
350 			       const struct firmware *fw)
351 {
352 	struct rmi_device *rmi_dev = data->rmi_dev;
353 	struct device *dev = &rmi_dev->dev;
354 	struct f34_data *f34;
355 	int ret;
356 
357 	if (!data->f34_container) {
358 		dev_warn(dev, "%s: No F34 present!\n", __func__);
359 		return -EINVAL;
360 	}
361 
362 	f34 = dev_get_drvdata(&data->f34_container->dev);
363 
364 	if (f34->bl_version >= 7) {
365 		if (data->pdt_props & HAS_BSR) {
366 			dev_err(dev, "%s: LTS not supported\n", __func__);
367 			return -ENODEV;
368 		}
369 	} else if (f34->bl_version != 5) {
370 		dev_warn(dev, "F34 V%d not supported!\n",
371 			 data->f34_container->fd.function_version);
372 		return -ENODEV;
373 	}
374 
375 	/* Enter flash mode */
376 	if (f34->bl_version >= 7)
377 		ret = rmi_f34v7_start_reflash(f34, fw);
378 	else
379 		ret = rmi_f34_enable_flash(f34);
380 	if (ret)
381 		return ret;
382 
383 	rmi_disable_irq(rmi_dev, false);
384 
385 	/* Tear down functions and re-probe */
386 	rmi_free_function_list(rmi_dev);
387 
388 	ret = rmi_probe_interrupts(data);
389 	if (ret)
390 		return ret;
391 
392 	ret = rmi_init_functions(data);
393 	if (ret)
394 		return ret;
395 
396 	if (!data->bootloader_mode || !data->f34_container) {
397 		dev_warn(dev, "%s: No F34 present or not in bootloader!\n",
398 				__func__);
399 		return -EINVAL;
400 	}
401 
402 	rmi_enable_irq(rmi_dev, false);
403 
404 	f34 = dev_get_drvdata(&data->f34_container->dev);
405 
406 	/* Perform firmware update */
407 	if (f34->bl_version >= 7)
408 		ret = rmi_f34v7_do_reflash(f34, fw);
409 	else
410 		ret = rmi_f34_update_firmware(f34, fw);
411 
412 	if (ret) {
413 		f34->update_status = ret;
414 		dev_err(&f34->fn->dev,
415 			"Firmware update failed, status: %d\n", ret);
416 	} else {
417 		dev_info(&f34->fn->dev, "Firmware update complete\n");
418 	}
419 
420 	rmi_disable_irq(rmi_dev, false);
421 
422 	/* Re-probe */
423 	rmi_dbg(RMI_DEBUG_FN, dev, "Re-probing device\n");
424 	rmi_free_function_list(rmi_dev);
425 
426 	ret = rmi_scan_pdt(rmi_dev, NULL, rmi_initial_reset);
427 	if (ret < 0)
428 		dev_warn(dev, "RMI reset failed!\n");
429 
430 	ret = rmi_probe_interrupts(data);
431 	if (ret)
432 		return ret;
433 
434 	ret = rmi_init_functions(data);
435 	if (ret)
436 		return ret;
437 
438 	rmi_enable_irq(rmi_dev, false);
439 
440 	if (data->f01_container->dev.driver)
441 		/* Driver already bound, so enable ATTN now. */
442 		return rmi_enable_sensor(rmi_dev);
443 
444 	rmi_dbg(RMI_DEBUG_FN, dev, "%s complete\n", __func__);
445 
446 	return ret;
447 }
448 
rmi_driver_update_fw_store(struct device * dev,struct device_attribute * dattr,const char * buf,size_t count)449 static ssize_t rmi_driver_update_fw_store(struct device *dev,
450 					  struct device_attribute *dattr,
451 					  const char *buf, size_t count)
452 {
453 	struct rmi_driver_data *data = dev_get_drvdata(dev);
454 	char fw_name[NAME_MAX];
455 	size_t copy_count = count;
456 	int error;
457 
458 	if (count == 0 || count >= NAME_MAX)
459 		return -EINVAL;
460 
461 	if (buf[count - 1] == '\0' || buf[count - 1] == '\n')
462 		copy_count -= 1;
463 
464 	memcpy(fw_name, buf, copy_count);
465 	fw_name[copy_count] = '\0';
466 
467 	const struct firmware *fw __free(firmware) = NULL;
468 	error = request_firmware(&fw, fw_name, dev);
469 	if (error)
470 		return error;
471 
472 	dev_info(dev, "Flashing %s\n", fw_name);
473 
474 	error = rmi_firmware_update(data, fw);
475 	if (error)
476 		return error;
477 
478 	return count;
479 }
480 
481 static DEVICE_ATTR(update_fw, 0200, NULL, rmi_driver_update_fw_store);
482 
rmi_driver_update_fw_status_show(struct device * dev,struct device_attribute * dattr,char * buf)483 static ssize_t rmi_driver_update_fw_status_show(struct device *dev,
484 						struct device_attribute *dattr,
485 						char *buf)
486 {
487 	struct rmi_driver_data *data = dev_get_drvdata(dev);
488 	int update_status = 0;
489 
490 	if (data->f34_container)
491 		update_status = rmi_f34_status(data->f34_container);
492 
493 	return sysfs_emit(buf, "%d\n", update_status);
494 }
495 
496 static DEVICE_ATTR(update_fw_status, 0444,
497 		   rmi_driver_update_fw_status_show, NULL);
498 
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,
504 	NULL
505 };
506 
507 static const struct attribute_group rmi_firmware_attr_group = {
508 	.attrs = rmi_firmware_attrs,
509 };
510 
rmi_f34_probe(struct rmi_function * fn)511 static int rmi_f34_probe(struct rmi_function *fn)
512 {
513 	struct f34_data *f34;
514 	unsigned char f34_queries[9];
515 	bool has_config_id;
516 	u8 version = fn->fd.function_version;
517 	int ret;
518 
519 	f34 = devm_kzalloc(&fn->dev, sizeof(struct f34_data), GFP_KERNEL);
520 	if (!f34)
521 		return -ENOMEM;
522 
523 	f34->fn = fn;
524 	dev_set_drvdata(&fn->dev, f34);
525 
526 	/* v5 code only supported version 0, try V7 probe */
527 	if (version > 0)
528 		return rmi_f34v7_probe(f34);
529 
530 	f34->bl_version = 5;
531 
532 	ret = rmi_read_block(fn->rmi_dev, fn->fd.query_base_addr,
533 			     f34_queries, sizeof(f34_queries));
534 	if (ret) {
535 		dev_err(&fn->dev, "%s: Failed to query properties\n",
536 			__func__);
537 		return ret;
538 	}
539 
540 	snprintf(f34->bootloader_id, sizeof(f34->bootloader_id),
541 		 "%c%c", f34_queries[0], f34_queries[1]);
542 
543 	mutex_init(&f34->v5.flash_mutex);
544 	init_completion(&f34->v5.cmd_done);
545 
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 +
550 		f34->v5.block_size;
551 	has_config_id = f34_queries[2] & (1 << 2);
552 
553 	rmi_dbg(RMI_DEBUG_FN, &fn->dev, "Bootloader ID: %s\n",
554 		f34->bootloader_id);
555 	rmi_dbg(RMI_DEBUG_FN, &fn->dev, "Block size: %d\n",
556 		f34->v5.block_size);
557 	rmi_dbg(RMI_DEBUG_FN, &fn->dev, "FW blocks: %d\n",
558 		f34->v5.fw_blocks);
559 	rmi_dbg(RMI_DEBUG_FN, &fn->dev, "CFG blocks: %d\n",
560 		f34->v5.config_blocks);
561 
562 	if (has_config_id) {
563 		ret = rmi_read_block(fn->rmi_dev, fn->fd.control_base_addr,
564 				     f34_queries, sizeof(f34_queries));
565 		if (ret) {
566 			dev_err(&fn->dev, "Failed to read F34 config ID\n");
567 			return ret;
568 		}
569 
570 		snprintf(f34->configuration_id, sizeof(f34->configuration_id),
571 			 "%02x%02x%02x%02x",
572 			 f34_queries[0], f34_queries[1],
573 			 f34_queries[2], f34_queries[3]);
574 
575 		rmi_dbg(RMI_DEBUG_FN, &fn->dev, "Configuration ID: %s\n",
576 			 f34->configuration_id);
577 	}
578 
579 	return 0;
580 }
581 
rmi_f34_create_sysfs(struct rmi_device * rmi_dev)582 int rmi_f34_create_sysfs(struct rmi_device *rmi_dev)
583 {
584 	return sysfs_create_group(&rmi_dev->dev.kobj, &rmi_firmware_attr_group);
585 }
586 
rmi_f34_remove_sysfs(struct rmi_device * rmi_dev)587 void rmi_f34_remove_sysfs(struct rmi_device *rmi_dev)
588 {
589 	sysfs_remove_group(&rmi_dev->dev.kobj, &rmi_firmware_attr_group);
590 }
591 
592 struct rmi_function_handler rmi_f34_handler = {
593 	.driver = {
594 		.name = "rmi4_f34",
595 	},
596 	.func = 0x34,
597 	.probe = rmi_f34_probe,
598 	.attention = rmi_f34_attention,
599 };
600