treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / fsi / fsi-master-gpio.c
blob4dcce17f243fe10ca14ad520069a984059711f34
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * A FSI master controller, using a simple GPIO bit-banging interface
4 */
6 #include <linux/crc4.h>
7 #include <linux/delay.h>
8 #include <linux/device.h>
9 #include <linux/fsi.h>
10 #include <linux/gpio/consumer.h>
11 #include <linux/io.h>
12 #include <linux/irqflags.h>
13 #include <linux/module.h>
14 #include <linux/of.h>
15 #include <linux/platform_device.h>
16 #include <linux/slab.h>
18 #include "fsi-master.h"
20 #define FSI_GPIO_STD_DLY 1 /* Standard pin delay in nS */
21 #define LAST_ADDR_INVALID 0x1
23 struct fsi_master_gpio {
24 struct fsi_master master;
25 struct device *dev;
26 struct mutex cmd_lock; /* mutex for command ordering */
27 struct gpio_desc *gpio_clk;
28 struct gpio_desc *gpio_data;
29 struct gpio_desc *gpio_trans; /* Voltage translator */
30 struct gpio_desc *gpio_enable; /* FSI enable */
31 struct gpio_desc *gpio_mux; /* Mux control */
32 bool external_mode;
33 bool no_delays;
34 uint32_t last_addr;
35 uint8_t t_send_delay;
36 uint8_t t_echo_delay;
39 #define CREATE_TRACE_POINTS
40 #include <trace/events/fsi_master_gpio.h>
42 #define to_fsi_master_gpio(m) container_of(m, struct fsi_master_gpio, master)
44 struct fsi_gpio_msg {
45 uint64_t msg;
46 uint8_t bits;
49 static void clock_toggle(struct fsi_master_gpio *master, int count)
51 int i;
53 for (i = 0; i < count; i++) {
54 if (!master->no_delays)
55 ndelay(FSI_GPIO_STD_DLY);
56 gpiod_set_value(master->gpio_clk, 0);
57 if (!master->no_delays)
58 ndelay(FSI_GPIO_STD_DLY);
59 gpiod_set_value(master->gpio_clk, 1);
63 static int sda_clock_in(struct fsi_master_gpio *master)
65 int in;
67 if (!master->no_delays)
68 ndelay(FSI_GPIO_STD_DLY);
69 gpiod_set_value(master->gpio_clk, 0);
71 /* Dummy read to feed the synchronizers */
72 gpiod_get_value(master->gpio_data);
74 /* Actual data read */
75 in = gpiod_get_value(master->gpio_data);
76 if (!master->no_delays)
77 ndelay(FSI_GPIO_STD_DLY);
78 gpiod_set_value(master->gpio_clk, 1);
79 return in ? 1 : 0;
82 static void sda_out(struct fsi_master_gpio *master, int value)
84 gpiod_set_value(master->gpio_data, value);
87 static void set_sda_input(struct fsi_master_gpio *master)
89 gpiod_direction_input(master->gpio_data);
90 gpiod_set_value(master->gpio_trans, 0);
93 static void set_sda_output(struct fsi_master_gpio *master, int value)
95 gpiod_set_value(master->gpio_trans, 1);
96 gpiod_direction_output(master->gpio_data, value);
99 static void clock_zeros(struct fsi_master_gpio *master, int count)
101 trace_fsi_master_gpio_clock_zeros(master, count);
102 set_sda_output(master, 1);
103 clock_toggle(master, count);
106 static void echo_delay(struct fsi_master_gpio *master)
108 clock_zeros(master, master->t_echo_delay);
112 static void serial_in(struct fsi_master_gpio *master, struct fsi_gpio_msg *msg,
113 uint8_t num_bits)
115 uint8_t bit, in_bit;
117 set_sda_input(master);
119 for (bit = 0; bit < num_bits; bit++) {
120 in_bit = sda_clock_in(master);
121 msg->msg <<= 1;
122 msg->msg |= ~in_bit & 0x1; /* Data is active low */
124 msg->bits += num_bits;
126 trace_fsi_master_gpio_in(master, num_bits, msg->msg);
129 static void serial_out(struct fsi_master_gpio *master,
130 const struct fsi_gpio_msg *cmd)
132 uint8_t bit;
133 uint64_t msg = ~cmd->msg; /* Data is active low */
134 uint64_t sda_mask = 0x1ULL << (cmd->bits - 1);
135 uint64_t last_bit = ~0;
136 int next_bit;
138 trace_fsi_master_gpio_out(master, cmd->bits, cmd->msg);
140 if (!cmd->bits) {
141 dev_warn(master->dev, "trying to output 0 bits\n");
142 return;
144 set_sda_output(master, 0);
146 /* Send the start bit */
147 sda_out(master, 0);
148 clock_toggle(master, 1);
150 /* Send the message */
151 for (bit = 0; bit < cmd->bits; bit++) {
152 next_bit = (msg & sda_mask) >> (cmd->bits - 1);
153 if (last_bit ^ next_bit) {
154 sda_out(master, next_bit);
155 last_bit = next_bit;
157 clock_toggle(master, 1);
158 msg <<= 1;
162 static void msg_push_bits(struct fsi_gpio_msg *msg, uint64_t data, int bits)
164 msg->msg <<= bits;
165 msg->msg |= data & ((1ull << bits) - 1);
166 msg->bits += bits;
169 static void msg_push_crc(struct fsi_gpio_msg *msg)
171 uint8_t crc;
172 int top;
174 top = msg->bits & 0x3;
176 /* start bit, and any non-aligned top bits */
177 crc = crc4(0, 1 << top | msg->msg >> (msg->bits - top), top + 1);
179 /* aligned bits */
180 crc = crc4(crc, msg->msg, msg->bits - top);
182 msg_push_bits(msg, crc, 4);
185 static bool check_same_address(struct fsi_master_gpio *master, int id,
186 uint32_t addr)
188 /* this will also handle LAST_ADDR_INVALID */
189 return master->last_addr == (((id & 0x3) << 21) | (addr & ~0x3));
192 static bool check_relative_address(struct fsi_master_gpio *master, int id,
193 uint32_t addr, uint32_t *rel_addrp)
195 uint32_t last_addr = master->last_addr;
196 int32_t rel_addr;
198 if (last_addr == LAST_ADDR_INVALID)
199 return false;
201 /* We may be in 23-bit addressing mode, which uses the id as the
202 * top two address bits. So, if we're referencing a different ID,
203 * use absolute addresses.
205 if (((last_addr >> 21) & 0x3) != id)
206 return false;
208 /* remove the top two bits from any 23-bit addressing */
209 last_addr &= (1 << 21) - 1;
211 /* We know that the addresses are limited to 21 bits, so this won't
212 * overflow the signed rel_addr */
213 rel_addr = addr - last_addr;
214 if (rel_addr > 255 || rel_addr < -256)
215 return false;
217 *rel_addrp = (uint32_t)rel_addr;
219 return true;
222 static void last_address_update(struct fsi_master_gpio *master,
223 int id, bool valid, uint32_t addr)
225 if (!valid)
226 master->last_addr = LAST_ADDR_INVALID;
227 else
228 master->last_addr = ((id & 0x3) << 21) | (addr & ~0x3);
232 * Encode an Absolute/Relative/Same Address command
234 static void build_ar_command(struct fsi_master_gpio *master,
235 struct fsi_gpio_msg *cmd, uint8_t id,
236 uint32_t addr, size_t size, const void *data)
238 int i, addr_bits, opcode_bits;
239 bool write = !!data;
240 uint8_t ds, opcode;
241 uint32_t rel_addr;
243 cmd->bits = 0;
244 cmd->msg = 0;
246 /* we have 21 bits of address max */
247 addr &= ((1 << 21) - 1);
249 /* cmd opcodes are variable length - SAME_AR is only two bits */
250 opcode_bits = 3;
252 if (check_same_address(master, id, addr)) {
253 /* we still address the byte offset within the word */
254 addr_bits = 2;
255 opcode_bits = 2;
256 opcode = FSI_CMD_SAME_AR;
257 trace_fsi_master_gpio_cmd_same_addr(master);
259 } else if (check_relative_address(master, id, addr, &rel_addr)) {
260 /* 8 bits plus sign */
261 addr_bits = 9;
262 addr = rel_addr;
263 opcode = FSI_CMD_REL_AR;
264 trace_fsi_master_gpio_cmd_rel_addr(master, rel_addr);
266 } else {
267 addr_bits = 21;
268 opcode = FSI_CMD_ABS_AR;
269 trace_fsi_master_gpio_cmd_abs_addr(master, addr);
273 * The read/write size is encoded in the lower bits of the address
274 * (as it must be naturally-aligned), and the following ds bit.
276 * size addr:1 addr:0 ds
277 * 1 x x 0
278 * 2 x 0 1
279 * 4 0 1 1
282 ds = size > 1 ? 1 : 0;
283 addr &= ~(size - 1);
284 if (size == 4)
285 addr |= 1;
287 msg_push_bits(cmd, id, 2);
288 msg_push_bits(cmd, opcode, opcode_bits);
289 msg_push_bits(cmd, write ? 0 : 1, 1);
290 msg_push_bits(cmd, addr, addr_bits);
291 msg_push_bits(cmd, ds, 1);
292 for (i = 0; write && i < size; i++)
293 msg_push_bits(cmd, ((uint8_t *)data)[i], 8);
295 msg_push_crc(cmd);
298 static void build_dpoll_command(struct fsi_gpio_msg *cmd, uint8_t slave_id)
300 cmd->bits = 0;
301 cmd->msg = 0;
303 msg_push_bits(cmd, slave_id, 2);
304 msg_push_bits(cmd, FSI_CMD_DPOLL, 3);
305 msg_push_crc(cmd);
308 static void build_epoll_command(struct fsi_gpio_msg *cmd, uint8_t slave_id)
310 cmd->bits = 0;
311 cmd->msg = 0;
313 msg_push_bits(cmd, slave_id, 2);
314 msg_push_bits(cmd, FSI_CMD_EPOLL, 3);
315 msg_push_crc(cmd);
318 static void build_term_command(struct fsi_gpio_msg *cmd, uint8_t slave_id)
320 cmd->bits = 0;
321 cmd->msg = 0;
323 msg_push_bits(cmd, slave_id, 2);
324 msg_push_bits(cmd, FSI_CMD_TERM, 6);
325 msg_push_crc(cmd);
329 * Note: callers rely specifically on this returning -EAGAIN for
330 * a CRC error detected in the response. Use other error code
331 * for other situations. It will be converted to something else
332 * higher up the stack before it reaches userspace.
334 static int read_one_response(struct fsi_master_gpio *master,
335 uint8_t data_size, struct fsi_gpio_msg *msgp, uint8_t *tagp)
337 struct fsi_gpio_msg msg;
338 unsigned long flags;
339 uint32_t crc;
340 uint8_t tag;
341 int i;
343 local_irq_save(flags);
345 /* wait for the start bit */
346 for (i = 0; i < FSI_MASTER_MTOE_COUNT; i++) {
347 msg.bits = 0;
348 msg.msg = 0;
349 serial_in(master, &msg, 1);
350 if (msg.msg)
351 break;
353 if (i == FSI_MASTER_MTOE_COUNT) {
354 dev_dbg(master->dev,
355 "Master time out waiting for response\n");
356 local_irq_restore(flags);
357 return -ETIMEDOUT;
360 msg.bits = 0;
361 msg.msg = 0;
363 /* Read slave ID & response tag */
364 serial_in(master, &msg, 4);
366 tag = msg.msg & 0x3;
368 /* If we have an ACK and we're expecting data, clock the data in too */
369 if (tag == FSI_RESP_ACK && data_size)
370 serial_in(master, &msg, data_size * 8);
372 /* read CRC */
373 serial_in(master, &msg, FSI_CRC_SIZE);
375 local_irq_restore(flags);
377 /* we have a whole message now; check CRC */
378 crc = crc4(0, 1, 1);
379 crc = crc4(crc, msg.msg, msg.bits);
380 if (crc) {
381 /* Check if it's all 1's, that probably means the host is off */
382 if (((~msg.msg) & ((1ull << msg.bits) - 1)) == 0)
383 return -ENODEV;
384 dev_dbg(master->dev, "ERR response CRC msg: 0x%016llx (%d bits)\n",
385 msg.msg, msg.bits);
386 return -EAGAIN;
389 if (msgp)
390 *msgp = msg;
391 if (tagp)
392 *tagp = tag;
394 return 0;
397 static int issue_term(struct fsi_master_gpio *master, uint8_t slave)
399 struct fsi_gpio_msg cmd;
400 unsigned long flags;
401 uint8_t tag;
402 int rc;
404 build_term_command(&cmd, slave);
406 local_irq_save(flags);
407 serial_out(master, &cmd);
408 echo_delay(master);
409 local_irq_restore(flags);
411 rc = read_one_response(master, 0, NULL, &tag);
412 if (rc < 0) {
413 dev_err(master->dev,
414 "TERM failed; lost communication with slave\n");
415 return -EIO;
416 } else if (tag != FSI_RESP_ACK) {
417 dev_err(master->dev, "TERM failed; response %d\n", tag);
418 return -EIO;
421 return 0;
424 static int poll_for_response(struct fsi_master_gpio *master,
425 uint8_t slave, uint8_t size, void *data)
427 struct fsi_gpio_msg response, cmd;
428 int busy_count = 0, rc, i;
429 unsigned long flags;
430 uint8_t tag;
431 uint8_t *data_byte = data;
432 int crc_err_retries = 0;
433 retry:
434 rc = read_one_response(master, size, &response, &tag);
436 /* Handle retries on CRC errors */
437 if (rc == -EAGAIN) {
438 /* Too many retries ? */
439 if (crc_err_retries++ > FSI_CRC_ERR_RETRIES) {
441 * Pass it up as a -EIO otherwise upper level will retry
442 * the whole command which isn't what we want here.
444 rc = -EIO;
445 goto fail;
447 dev_dbg(master->dev,
448 "CRC error retry %d\n", crc_err_retries);
449 trace_fsi_master_gpio_crc_rsp_error(master);
450 build_epoll_command(&cmd, slave);
451 local_irq_save(flags);
452 clock_zeros(master, FSI_MASTER_EPOLL_CLOCKS);
453 serial_out(master, &cmd);
454 echo_delay(master);
455 local_irq_restore(flags);
456 goto retry;
457 } else if (rc)
458 goto fail;
460 switch (tag) {
461 case FSI_RESP_ACK:
462 if (size && data) {
463 uint64_t val = response.msg;
464 /* clear crc & mask */
465 val >>= 4;
466 val &= (1ull << (size * 8)) - 1;
468 for (i = 0; i < size; i++) {
469 data_byte[size-i-1] = val;
470 val >>= 8;
473 break;
474 case FSI_RESP_BUSY:
476 * Its necessary to clock slave before issuing
477 * d-poll, not indicated in the hardware protocol
478 * spec. < 20 clocks causes slave to hang, 21 ok.
480 if (busy_count++ < FSI_MASTER_MAX_BUSY) {
481 build_dpoll_command(&cmd, slave);
482 local_irq_save(flags);
483 clock_zeros(master, FSI_MASTER_DPOLL_CLOCKS);
484 serial_out(master, &cmd);
485 echo_delay(master);
486 local_irq_restore(flags);
487 goto retry;
489 dev_warn(master->dev,
490 "ERR slave is stuck in busy state, issuing TERM\n");
491 local_irq_save(flags);
492 clock_zeros(master, FSI_MASTER_DPOLL_CLOCKS);
493 local_irq_restore(flags);
494 issue_term(master, slave);
495 rc = -EIO;
496 break;
498 case FSI_RESP_ERRA:
499 dev_dbg(master->dev, "ERRA received: 0x%x\n", (int)response.msg);
500 rc = -EIO;
501 break;
502 case FSI_RESP_ERRC:
503 dev_dbg(master->dev, "ERRC received: 0x%x\n", (int)response.msg);
504 trace_fsi_master_gpio_crc_cmd_error(master);
505 rc = -EAGAIN;
506 break;
509 if (busy_count > 0)
510 trace_fsi_master_gpio_poll_response_busy(master, busy_count);
511 fail:
513 * tSendDelay clocks, avoids signal reflections when switching
514 * from receive of response back to send of data.
516 local_irq_save(flags);
517 clock_zeros(master, master->t_send_delay);
518 local_irq_restore(flags);
520 return rc;
523 static int send_request(struct fsi_master_gpio *master,
524 struct fsi_gpio_msg *cmd)
526 unsigned long flags;
528 if (master->external_mode)
529 return -EBUSY;
531 local_irq_save(flags);
532 serial_out(master, cmd);
533 echo_delay(master);
534 local_irq_restore(flags);
536 return 0;
539 static int fsi_master_gpio_xfer(struct fsi_master_gpio *master, uint8_t slave,
540 struct fsi_gpio_msg *cmd, size_t resp_len, void *resp)
542 int rc = -EAGAIN, retries = 0;
544 while ((retries++) < FSI_CRC_ERR_RETRIES) {
545 rc = send_request(master, cmd);
546 if (rc)
547 break;
548 rc = poll_for_response(master, slave, resp_len, resp);
549 if (rc != -EAGAIN)
550 break;
551 rc = -EIO;
552 dev_warn(master->dev, "ECRC retry %d\n", retries);
554 /* Pace it a bit before retry */
555 msleep(1);
558 return rc;
561 static int fsi_master_gpio_read(struct fsi_master *_master, int link,
562 uint8_t id, uint32_t addr, void *val, size_t size)
564 struct fsi_master_gpio *master = to_fsi_master_gpio(_master);
565 struct fsi_gpio_msg cmd;
566 int rc;
568 if (link != 0)
569 return -ENODEV;
571 mutex_lock(&master->cmd_lock);
572 build_ar_command(master, &cmd, id, addr, size, NULL);
573 rc = fsi_master_gpio_xfer(master, id, &cmd, size, val);
574 last_address_update(master, id, rc == 0, addr);
575 mutex_unlock(&master->cmd_lock);
577 return rc;
580 static int fsi_master_gpio_write(struct fsi_master *_master, int link,
581 uint8_t id, uint32_t addr, const void *val, size_t size)
583 struct fsi_master_gpio *master = to_fsi_master_gpio(_master);
584 struct fsi_gpio_msg cmd;
585 int rc;
587 if (link != 0)
588 return -ENODEV;
590 mutex_lock(&master->cmd_lock);
591 build_ar_command(master, &cmd, id, addr, size, val);
592 rc = fsi_master_gpio_xfer(master, id, &cmd, 0, NULL);
593 last_address_update(master, id, rc == 0, addr);
594 mutex_unlock(&master->cmd_lock);
596 return rc;
599 static int fsi_master_gpio_term(struct fsi_master *_master,
600 int link, uint8_t id)
602 struct fsi_master_gpio *master = to_fsi_master_gpio(_master);
603 struct fsi_gpio_msg cmd;
604 int rc;
606 if (link != 0)
607 return -ENODEV;
609 mutex_lock(&master->cmd_lock);
610 build_term_command(&cmd, id);
611 rc = fsi_master_gpio_xfer(master, id, &cmd, 0, NULL);
612 last_address_update(master, id, false, 0);
613 mutex_unlock(&master->cmd_lock);
615 return rc;
618 static int fsi_master_gpio_break(struct fsi_master *_master, int link)
620 struct fsi_master_gpio *master = to_fsi_master_gpio(_master);
621 unsigned long flags;
623 if (link != 0)
624 return -ENODEV;
626 trace_fsi_master_gpio_break(master);
628 mutex_lock(&master->cmd_lock);
629 if (master->external_mode) {
630 mutex_unlock(&master->cmd_lock);
631 return -EBUSY;
634 local_irq_save(flags);
636 set_sda_output(master, 1);
637 sda_out(master, 1);
638 clock_toggle(master, FSI_PRE_BREAK_CLOCKS);
639 sda_out(master, 0);
640 clock_toggle(master, FSI_BREAK_CLOCKS);
641 echo_delay(master);
642 sda_out(master, 1);
643 clock_toggle(master, FSI_POST_BREAK_CLOCKS);
645 local_irq_restore(flags);
647 last_address_update(master, 0, false, 0);
648 mutex_unlock(&master->cmd_lock);
650 /* Wait for logic reset to take effect */
651 udelay(200);
653 return 0;
656 static void fsi_master_gpio_init(struct fsi_master_gpio *master)
658 unsigned long flags;
660 gpiod_direction_output(master->gpio_mux, 1);
661 gpiod_direction_output(master->gpio_trans, 1);
662 gpiod_direction_output(master->gpio_enable, 1);
663 gpiod_direction_output(master->gpio_clk, 1);
664 gpiod_direction_output(master->gpio_data, 1);
666 /* todo: evaluate if clocks can be reduced */
667 local_irq_save(flags);
668 clock_zeros(master, FSI_INIT_CLOCKS);
669 local_irq_restore(flags);
672 static void fsi_master_gpio_init_external(struct fsi_master_gpio *master)
674 gpiod_direction_output(master->gpio_mux, 0);
675 gpiod_direction_output(master->gpio_trans, 0);
676 gpiod_direction_output(master->gpio_enable, 1);
677 gpiod_direction_input(master->gpio_clk);
678 gpiod_direction_input(master->gpio_data);
681 static int fsi_master_gpio_link_enable(struct fsi_master *_master, int link)
683 struct fsi_master_gpio *master = to_fsi_master_gpio(_master);
684 int rc = -EBUSY;
686 if (link != 0)
687 return -ENODEV;
689 mutex_lock(&master->cmd_lock);
690 if (!master->external_mode) {
691 gpiod_set_value(master->gpio_enable, 1);
692 rc = 0;
694 mutex_unlock(&master->cmd_lock);
696 return rc;
699 static int fsi_master_gpio_link_config(struct fsi_master *_master, int link,
700 u8 t_send_delay, u8 t_echo_delay)
702 struct fsi_master_gpio *master = to_fsi_master_gpio(_master);
704 if (link != 0)
705 return -ENODEV;
707 mutex_lock(&master->cmd_lock);
708 master->t_send_delay = t_send_delay;
709 master->t_echo_delay = t_echo_delay;
710 mutex_unlock(&master->cmd_lock);
712 return 0;
715 static ssize_t external_mode_show(struct device *dev,
716 struct device_attribute *attr, char *buf)
718 struct fsi_master_gpio *master = dev_get_drvdata(dev);
720 return snprintf(buf, PAGE_SIZE - 1, "%u\n",
721 master->external_mode ? 1 : 0);
724 static ssize_t external_mode_store(struct device *dev,
725 struct device_attribute *attr, const char *buf, size_t count)
727 struct fsi_master_gpio *master = dev_get_drvdata(dev);
728 unsigned long val;
729 bool external_mode;
730 int err;
732 err = kstrtoul(buf, 0, &val);
733 if (err)
734 return err;
736 external_mode = !!val;
738 mutex_lock(&master->cmd_lock);
740 if (external_mode == master->external_mode) {
741 mutex_unlock(&master->cmd_lock);
742 return count;
745 master->external_mode = external_mode;
746 if (master->external_mode)
747 fsi_master_gpio_init_external(master);
748 else
749 fsi_master_gpio_init(master);
751 mutex_unlock(&master->cmd_lock);
753 fsi_master_rescan(&master->master);
755 return count;
758 static DEVICE_ATTR(external_mode, 0664,
759 external_mode_show, external_mode_store);
761 static void fsi_master_gpio_release(struct device *dev)
763 struct fsi_master_gpio *master = to_fsi_master_gpio(dev_to_fsi_master(dev));
765 of_node_put(dev_of_node(master->dev));
767 kfree(master);
770 static int fsi_master_gpio_probe(struct platform_device *pdev)
772 struct fsi_master_gpio *master;
773 struct gpio_desc *gpio;
774 int rc;
776 master = kzalloc(sizeof(*master), GFP_KERNEL);
777 if (!master)
778 return -ENOMEM;
780 master->dev = &pdev->dev;
781 master->master.dev.parent = master->dev;
782 master->master.dev.of_node = of_node_get(dev_of_node(master->dev));
783 master->master.dev.release = fsi_master_gpio_release;
784 master->last_addr = LAST_ADDR_INVALID;
786 gpio = devm_gpiod_get(&pdev->dev, "clock", 0);
787 if (IS_ERR(gpio)) {
788 dev_err(&pdev->dev, "failed to get clock gpio\n");
789 rc = PTR_ERR(gpio);
790 goto err_free;
792 master->gpio_clk = gpio;
794 gpio = devm_gpiod_get(&pdev->dev, "data", 0);
795 if (IS_ERR(gpio)) {
796 dev_err(&pdev->dev, "failed to get data gpio\n");
797 rc = PTR_ERR(gpio);
798 goto err_free;
800 master->gpio_data = gpio;
802 /* Optional GPIOs */
803 gpio = devm_gpiod_get_optional(&pdev->dev, "trans", 0);
804 if (IS_ERR(gpio)) {
805 dev_err(&pdev->dev, "failed to get trans gpio\n");
806 rc = PTR_ERR(gpio);
807 goto err_free;
809 master->gpio_trans = gpio;
811 gpio = devm_gpiod_get_optional(&pdev->dev, "enable", 0);
812 if (IS_ERR(gpio)) {
813 dev_err(&pdev->dev, "failed to get enable gpio\n");
814 rc = PTR_ERR(gpio);
815 goto err_free;
817 master->gpio_enable = gpio;
819 gpio = devm_gpiod_get_optional(&pdev->dev, "mux", 0);
820 if (IS_ERR(gpio)) {
821 dev_err(&pdev->dev, "failed to get mux gpio\n");
822 rc = PTR_ERR(gpio);
823 goto err_free;
825 master->gpio_mux = gpio;
828 * Check if GPIO block is slow enought that no extra delays
829 * are necessary. This improves performance on ast2500 by
830 * an order of magnitude.
832 master->no_delays = device_property_present(&pdev->dev, "no-gpio-delays");
834 /* Default FSI command delays */
835 master->t_send_delay = FSI_SEND_DELAY_CLOCKS;
836 master->t_echo_delay = FSI_ECHO_DELAY_CLOCKS;
838 master->master.n_links = 1;
839 master->master.flags = FSI_MASTER_FLAG_SWCLOCK;
840 master->master.read = fsi_master_gpio_read;
841 master->master.write = fsi_master_gpio_write;
842 master->master.term = fsi_master_gpio_term;
843 master->master.send_break = fsi_master_gpio_break;
844 master->master.link_enable = fsi_master_gpio_link_enable;
845 master->master.link_config = fsi_master_gpio_link_config;
846 platform_set_drvdata(pdev, master);
847 mutex_init(&master->cmd_lock);
849 fsi_master_gpio_init(master);
851 rc = device_create_file(&pdev->dev, &dev_attr_external_mode);
852 if (rc)
853 goto err_free;
855 rc = fsi_master_register(&master->master);
856 if (rc) {
857 device_remove_file(&pdev->dev, &dev_attr_external_mode);
858 put_device(&master->master.dev);
859 return rc;
861 return 0;
862 err_free:
863 kfree(master);
864 return rc;
869 static int fsi_master_gpio_remove(struct platform_device *pdev)
871 struct fsi_master_gpio *master = platform_get_drvdata(pdev);
873 device_remove_file(&pdev->dev, &dev_attr_external_mode);
875 fsi_master_unregister(&master->master);
877 return 0;
880 static const struct of_device_id fsi_master_gpio_match[] = {
881 { .compatible = "fsi-master-gpio" },
882 { },
885 static struct platform_driver fsi_master_gpio_driver = {
886 .driver = {
887 .name = "fsi-master-gpio",
888 .of_match_table = fsi_master_gpio_match,
890 .probe = fsi_master_gpio_probe,
891 .remove = fsi_master_gpio_remove,
894 module_platform_driver(fsi_master_gpio_driver);
895 MODULE_LICENSE("GPL");