fsi/gpio: Include command build in locked section
[linux-2.6-microblaze.git] / drivers / fsi / fsi-master-gpio.c
1 /*
2  * A FSI master controller, using a simple GPIO bit-banging interface
3  */
4
5 #include <linux/crc4.h>
6 #include <linux/delay.h>
7 #include <linux/device.h>
8 #include <linux/fsi.h>
9 #include <linux/gpio/consumer.h>
10 #include <linux/io.h>
11 #include <linux/module.h>
12 #include <linux/of.h>
13 #include <linux/platform_device.h>
14 #include <linux/slab.h>
15 #include <linux/spinlock.h>
16
17 #include "fsi-master.h"
18
19 #define FSI_GPIO_STD_DLY        1       /* Standard pin delay in nS */
20 #define FSI_ECHO_DELAY_CLOCKS   16      /* Number clocks for echo delay */
21 #define FSI_PRE_BREAK_CLOCKS    50      /* Number clocks to prep for break */
22 #define FSI_BREAK_CLOCKS        256     /* Number of clocks to issue break */
23 #define FSI_POST_BREAK_CLOCKS   16000   /* Number clocks to set up cfam */
24 #define FSI_INIT_CLOCKS         5000    /* Clock out any old data */
25 #define FSI_GPIO_STD_DELAY      10      /* Standard GPIO delay in nS */
26                                         /* todo: adjust down as low as */
27                                         /* possible or eliminate */
28 #define FSI_GPIO_CMD_DPOLL      0x2
29 #define FSI_GPIO_CMD_TERM       0x3f
30 #define FSI_GPIO_CMD_ABS_AR     0x4
31
32
33 #define FSI_GPIO_DPOLL_CLOCKS   50      /* < 21 will cause slave to hang */
34
35 /* Bus errors */
36 #define FSI_GPIO_ERR_BUSY       1       /* Slave stuck in busy state */
37 #define FSI_GPIO_RESP_ERRA      2       /* Any (misc) Error */
38 #define FSI_GPIO_RESP_ERRC      3       /* Slave reports master CRC error */
39 #define FSI_GPIO_MTOE           4       /* Master time out error */
40 #define FSI_GPIO_CRC_INVAL      5       /* Master reports slave CRC error */
41
42 /* Normal slave responses */
43 #define FSI_GPIO_RESP_BUSY      1
44 #define FSI_GPIO_RESP_ACK       0
45 #define FSI_GPIO_RESP_ACKD      4
46
47 #define FSI_GPIO_MAX_BUSY       200
48 #define FSI_GPIO_MTOE_COUNT     1000
49 #define FSI_GPIO_DRAIN_BITS     20
50 #define FSI_GPIO_CRC_SIZE       4
51 #define FSI_GPIO_MSG_ID_SIZE            2
52 #define FSI_GPIO_MSG_RESPID_SIZE        2
53 #define FSI_GPIO_PRIME_SLAVE_CLOCKS     20
54
55 struct fsi_master_gpio {
56         struct fsi_master       master;
57         struct device           *dev;
58         struct mutex            cmd_lock;       /* mutex for command ordering */
59         spinlock_t              bit_lock;       /* lock for clocking bits out */
60         struct gpio_desc        *gpio_clk;
61         struct gpio_desc        *gpio_data;
62         struct gpio_desc        *gpio_trans;    /* Voltage translator */
63         struct gpio_desc        *gpio_enable;   /* FSI enable */
64         struct gpio_desc        *gpio_mux;      /* Mux control */
65         bool                    external_mode;
66         bool                    no_delays;
67 };
68
69 #define CREATE_TRACE_POINTS
70 #include <trace/events/fsi_master_gpio.h>
71
72 #define to_fsi_master_gpio(m) container_of(m, struct fsi_master_gpio, master)
73
74 struct fsi_gpio_msg {
75         uint64_t        msg;
76         uint8_t         bits;
77 };
78
79 static void clock_toggle(struct fsi_master_gpio *master, int count)
80 {
81         int i;
82
83         for (i = 0; i < count; i++) {
84                 if (!master->no_delays)
85                         ndelay(FSI_GPIO_STD_DLY);
86                 gpiod_set_value(master->gpio_clk, 0);
87                 if (!master->no_delays)
88                         ndelay(FSI_GPIO_STD_DLY);
89                 gpiod_set_value(master->gpio_clk, 1);
90         }
91 }
92
93 static int sda_clock_in(struct fsi_master_gpio *master)
94 {
95         int in;
96
97         if (!master->no_delays)
98                 ndelay(FSI_GPIO_STD_DLY);
99         gpiod_set_value(master->gpio_clk, 0);
100
101         /* Dummy read to feed the synchronizers */
102         gpiod_get_value(master->gpio_data);
103
104         /* Actual data read */
105         in = gpiod_get_value(master->gpio_data);
106         if (!master->no_delays)
107                 ndelay(FSI_GPIO_STD_DLY);
108         gpiod_set_value(master->gpio_clk, 1);
109         return in ? 1 : 0;
110 }
111
112 static void sda_out(struct fsi_master_gpio *master, int value)
113 {
114         gpiod_set_value(master->gpio_data, value);
115 }
116
117 static void set_sda_input(struct fsi_master_gpio *master)
118 {
119         gpiod_direction_input(master->gpio_data);
120         gpiod_set_value(master->gpio_trans, 0);
121 }
122
123 static void set_sda_output(struct fsi_master_gpio *master, int value)
124 {
125         gpiod_set_value(master->gpio_trans, 1);
126         gpiod_direction_output(master->gpio_data, value);
127 }
128
129 static void clock_zeros(struct fsi_master_gpio *master, int count)
130 {
131         set_sda_output(master, 1);
132         clock_toggle(master, count);
133 }
134
135 static void serial_in(struct fsi_master_gpio *master, struct fsi_gpio_msg *msg,
136                         uint8_t num_bits)
137 {
138         uint8_t bit, in_bit;
139
140         set_sda_input(master);
141
142         for (bit = 0; bit < num_bits; bit++) {
143                 in_bit = sda_clock_in(master);
144                 msg->msg <<= 1;
145                 msg->msg |= ~in_bit & 0x1;      /* Data is active low */
146         }
147         msg->bits += num_bits;
148
149         trace_fsi_master_gpio_in(master, num_bits, msg->msg);
150 }
151
152 static void serial_out(struct fsi_master_gpio *master,
153                         const struct fsi_gpio_msg *cmd)
154 {
155         uint8_t bit;
156         uint64_t msg = ~cmd->msg;       /* Data is active low */
157         uint64_t sda_mask = 0x1ULL << (cmd->bits - 1);
158         uint64_t last_bit = ~0;
159         int next_bit;
160
161         trace_fsi_master_gpio_out(master, cmd->bits, cmd->msg);
162
163         if (!cmd->bits) {
164                 dev_warn(master->dev, "trying to output 0 bits\n");
165                 return;
166         }
167         set_sda_output(master, 0);
168
169         /* Send the start bit */
170         sda_out(master, 0);
171         clock_toggle(master, 1);
172
173         /* Send the message */
174         for (bit = 0; bit < cmd->bits; bit++) {
175                 next_bit = (msg & sda_mask) >> (cmd->bits - 1);
176                 if (last_bit ^ next_bit) {
177                         sda_out(master, next_bit);
178                         last_bit = next_bit;
179                 }
180                 clock_toggle(master, 1);
181                 msg <<= 1;
182         }
183 }
184
185 static void msg_push_bits(struct fsi_gpio_msg *msg, uint64_t data, int bits)
186 {
187         msg->msg <<= bits;
188         msg->msg |= data & ((1ull << bits) - 1);
189         msg->bits += bits;
190 }
191
192 static void msg_push_crc(struct fsi_gpio_msg *msg)
193 {
194         uint8_t crc;
195         int top;
196
197         top = msg->bits & 0x3;
198
199         /* start bit, and any non-aligned top bits */
200         crc = crc4(0, 1 << top | msg->msg >> (msg->bits - top), top + 1);
201
202         /* aligned bits */
203         crc = crc4(crc, msg->msg, msg->bits - top);
204
205         msg_push_bits(msg, crc, 4);
206 }
207
208 /*
209  * Encode an Absolute Address command
210  */
211 static void build_abs_ar_command(struct fsi_gpio_msg *cmd,
212                 uint8_t id, uint32_t addr, size_t size, const void *data)
213 {
214         bool write = !!data;
215         uint8_t ds;
216         int i;
217
218         cmd->bits = 0;
219         cmd->msg = 0;
220
221         msg_push_bits(cmd, id, 2);
222         msg_push_bits(cmd, FSI_GPIO_CMD_ABS_AR, 3);
223         msg_push_bits(cmd, write ? 0 : 1, 1);
224
225         /*
226          * The read/write size is encoded in the lower bits of the address
227          * (as it must be naturally-aligned), and the following ds bit.
228          *
229          *      size    addr:1  addr:0  ds
230          *      1       x       x       0
231          *      2       x       0       1
232          *      4       0       1       1
233          *
234          */
235         ds = size > 1 ? 1 : 0;
236         addr &= ~(size - 1);
237         if (size == 4)
238                 addr |= 1;
239
240         msg_push_bits(cmd, addr & ((1 << 21) - 1), 21);
241         msg_push_bits(cmd, ds, 1);
242         for (i = 0; write && i < size; i++)
243                 msg_push_bits(cmd, ((uint8_t *)data)[i], 8);
244
245         msg_push_crc(cmd);
246 }
247
248 static void build_dpoll_command(struct fsi_gpio_msg *cmd, uint8_t slave_id)
249 {
250         cmd->bits = 0;
251         cmd->msg = 0;
252
253         msg_push_bits(cmd, slave_id, 2);
254         msg_push_bits(cmd, FSI_GPIO_CMD_DPOLL, 3);
255         msg_push_crc(cmd);
256 }
257
258 static void echo_delay(struct fsi_master_gpio *master)
259 {
260         set_sda_output(master, 1);
261         clock_toggle(master, FSI_ECHO_DELAY_CLOCKS);
262 }
263
264 static void build_term_command(struct fsi_gpio_msg *cmd, uint8_t slave_id)
265 {
266         cmd->bits = 0;
267         cmd->msg = 0;
268
269         msg_push_bits(cmd, slave_id, 2);
270         msg_push_bits(cmd, FSI_GPIO_CMD_TERM, 6);
271         msg_push_crc(cmd);
272 }
273
274 /*
275  * Store information on master errors so handler can detect and clean
276  * up the bus
277  */
278 static void fsi_master_gpio_error(struct fsi_master_gpio *master, int error)
279 {
280
281 }
282
283 static int read_one_response(struct fsi_master_gpio *master,
284                 uint8_t data_size, struct fsi_gpio_msg *msgp, uint8_t *tagp)
285 {
286         struct fsi_gpio_msg msg;
287         unsigned long flags;
288         uint32_t crc;
289         uint8_t tag;
290         int i;
291
292         spin_lock_irqsave(&master->bit_lock, flags);
293
294         /* wait for the start bit */
295         for (i = 0; i < FSI_GPIO_MTOE_COUNT; i++) {
296                 msg.bits = 0;
297                 msg.msg = 0;
298                 serial_in(master, &msg, 1);
299                 if (msg.msg)
300                         break;
301         }
302         if (i == FSI_GPIO_MTOE_COUNT) {
303                 dev_dbg(master->dev,
304                         "Master time out waiting for response\n");
305                 fsi_master_gpio_error(master, FSI_GPIO_MTOE);
306                 spin_unlock_irqrestore(&master->bit_lock, flags);
307                 return -EIO;
308         }
309
310         msg.bits = 0;
311         msg.msg = 0;
312
313         /* Read slave ID & response tag */
314         serial_in(master, &msg, 4);
315
316         tag = msg.msg & 0x3;
317
318         /* If we have an ACK and we're expecting data, clock the data in too */
319         if (tag == FSI_GPIO_RESP_ACK && data_size)
320                 serial_in(master, &msg, data_size * 8);
321
322         /* read CRC */
323         serial_in(master, &msg, FSI_GPIO_CRC_SIZE);
324
325         spin_unlock_irqrestore(&master->bit_lock, flags);
326
327         /* we have a whole message now; check CRC */
328         crc = crc4(0, 1, 1);
329         crc = crc4(crc, msg.msg, msg.bits);
330         if (crc) {
331                 dev_dbg(master->dev, "ERR response CRC\n");
332                 fsi_master_gpio_error(master, FSI_GPIO_CRC_INVAL);
333                 return -EIO;
334         }
335
336         if (msgp)
337                 *msgp = msg;
338         if (tagp)
339                 *tagp = tag;
340
341         return 0;
342 }
343
344 static int issue_term(struct fsi_master_gpio *master, uint8_t slave)
345 {
346         struct fsi_gpio_msg cmd;
347         unsigned long flags;
348         uint8_t tag;
349         int rc;
350
351         build_term_command(&cmd, slave);
352
353         spin_lock_irqsave(&master->bit_lock, flags);
354         serial_out(master, &cmd);
355         echo_delay(master);
356         spin_unlock_irqrestore(&master->bit_lock, flags);
357
358         rc = read_one_response(master, 0, NULL, &tag);
359         if (rc < 0) {
360                 dev_err(master->dev,
361                                 "TERM failed; lost communication with slave\n");
362                 return -EIO;
363         } else if (tag != FSI_GPIO_RESP_ACK) {
364                 dev_err(master->dev, "TERM failed; response %d\n", tag);
365                 return -EIO;
366         }
367
368         return 0;
369 }
370
371 static int poll_for_response(struct fsi_master_gpio *master,
372                 uint8_t slave, uint8_t size, void *data)
373 {
374         struct fsi_gpio_msg response, cmd;
375         int busy_count = 0, rc, i;
376         unsigned long flags;
377         uint8_t tag;
378         uint8_t *data_byte = data;
379
380 retry:
381         rc = read_one_response(master, size, &response, &tag);
382         if (rc)
383                 return rc;
384
385         switch (tag) {
386         case FSI_GPIO_RESP_ACK:
387                 if (size && data) {
388                         uint64_t val = response.msg;
389                         /* clear crc & mask */
390                         val >>= 4;
391                         val &= (1ull << (size * 8)) - 1;
392
393                         for (i = 0; i < size; i++) {
394                                 data_byte[size-i-1] = val;
395                                 val >>= 8;
396                         }
397                 }
398                 break;
399         case FSI_GPIO_RESP_BUSY:
400                 /*
401                  * Its necessary to clock slave before issuing
402                  * d-poll, not indicated in the hardware protocol
403                  * spec. < 20 clocks causes slave to hang, 21 ok.
404                  */
405                 if (busy_count++ < FSI_GPIO_MAX_BUSY) {
406                         build_dpoll_command(&cmd, slave);
407                         spin_lock_irqsave(&master->bit_lock, flags);
408                         clock_zeros(master, FSI_GPIO_DPOLL_CLOCKS);
409                         serial_out(master, &cmd);
410                         echo_delay(master);
411                         spin_unlock_irqrestore(&master->bit_lock, flags);
412                         goto retry;
413                 }
414                 dev_warn(master->dev,
415                         "ERR slave is stuck in busy state, issuing TERM\n");
416                 spin_lock_irqsave(&master->bit_lock, flags);
417                 clock_zeros(master, FSI_GPIO_DPOLL_CLOCKS);
418                 spin_unlock_irqrestore(&master->bit_lock, flags);
419                 issue_term(master, slave);
420                 rc = -EIO;
421                 break;
422
423         case FSI_GPIO_RESP_ERRA:
424         case FSI_GPIO_RESP_ERRC:
425                 dev_dbg(master->dev, "ERR%c received: 0x%x\n",
426                         tag == FSI_GPIO_RESP_ERRA ? 'A' : 'C',
427                         (int)response.msg);
428                 fsi_master_gpio_error(master, response.msg);
429                 rc = -EIO;
430                 break;
431         }
432
433         if (busy_count > 0)
434                 trace_fsi_master_gpio_poll_response_busy(master, busy_count);
435
436         /* Clock the slave enough to be ready for next operation */
437         spin_lock_irqsave(&master->bit_lock, flags);
438         clock_zeros(master, FSI_GPIO_PRIME_SLAVE_CLOCKS);
439         spin_unlock_irqrestore(&master->bit_lock, flags);
440         return rc;
441 }
442
443 static int send_request(struct fsi_master_gpio *master,
444                 struct fsi_gpio_msg *cmd)
445 {
446         unsigned long flags;
447
448         spin_lock_irqsave(&master->bit_lock, flags);
449         if (master->external_mode) {
450                 spin_unlock_irqrestore(&master->bit_lock, flags);
451                 return -EBUSY;
452         }
453
454         serial_out(master, cmd);
455         echo_delay(master);
456         spin_unlock_irqrestore(&master->bit_lock, flags);
457
458         return 0;
459 }
460
461 static int fsi_master_gpio_xfer(struct fsi_master_gpio *master, uint8_t slave,
462                 struct fsi_gpio_msg *cmd, size_t resp_len, void *resp)
463 {
464         int rc;
465
466         rc = send_request(master, cmd);
467         if (!rc)
468                 rc = poll_for_response(master, slave, resp_len, resp);
469
470         return rc;
471 }
472
473 static int fsi_master_gpio_read(struct fsi_master *_master, int link,
474                 uint8_t id, uint32_t addr, void *val, size_t size)
475 {
476         struct fsi_master_gpio *master = to_fsi_master_gpio(_master);
477         struct fsi_gpio_msg cmd;
478         int rc;
479
480         if (link != 0)
481                 return -ENODEV;
482
483         mutex_lock(&master->cmd_lock);
484         build_abs_ar_command(&cmd, id, addr, size, NULL);
485         rc = fsi_master_gpio_xfer(master, id, &cmd, size, val);
486         mutex_unlock(&master->cmd_lock);
487
488         return rc;
489 }
490
491 static int fsi_master_gpio_write(struct fsi_master *_master, int link,
492                 uint8_t id, uint32_t addr, const void *val, size_t size)
493 {
494         struct fsi_master_gpio *master = to_fsi_master_gpio(_master);
495         struct fsi_gpio_msg cmd;
496         int rc;
497
498         if (link != 0)
499                 return -ENODEV;
500
501         mutex_lock(&master->cmd_lock);
502         build_abs_ar_command(&cmd, id, addr, size, val);
503         rc = fsi_master_gpio_xfer(master, id, &cmd, 0, NULL);
504         mutex_unlock(&master->cmd_lock);
505
506         return rc;
507 }
508
509 static int fsi_master_gpio_term(struct fsi_master *_master,
510                 int link, uint8_t id)
511 {
512         struct fsi_master_gpio *master = to_fsi_master_gpio(_master);
513         struct fsi_gpio_msg cmd;
514         int rc;
515
516         if (link != 0)
517                 return -ENODEV;
518
519         mutex_lock(&master->cmd_lock);
520         build_term_command(&cmd, id);
521         rc = fsi_master_gpio_xfer(master, id, &cmd, 0, NULL);
522         mutex_unlock(&master->cmd_lock);
523
524         return rc;
525 }
526
527 static int fsi_master_gpio_break(struct fsi_master *_master, int link)
528 {
529         struct fsi_master_gpio *master = to_fsi_master_gpio(_master);
530         unsigned long flags;
531
532         if (link != 0)
533                 return -ENODEV;
534
535         trace_fsi_master_gpio_break(master);
536
537         mutex_lock(&master->cmd_lock);
538         if (master->external_mode) {
539                 mutex_unlock(&master->cmd_lock);
540                 return -EBUSY;
541         }
542
543         spin_lock_irqsave(&master->bit_lock, flags);
544
545         set_sda_output(master, 1);
546         sda_out(master, 1);
547         clock_toggle(master, FSI_PRE_BREAK_CLOCKS);
548         sda_out(master, 0);
549         clock_toggle(master, FSI_BREAK_CLOCKS);
550         echo_delay(master);
551         sda_out(master, 1);
552         clock_toggle(master, FSI_POST_BREAK_CLOCKS);
553
554         spin_unlock_irqrestore(&master->bit_lock, flags);
555         mutex_unlock(&master->cmd_lock);
556
557         /* Wait for logic reset to take effect */
558         udelay(200);
559
560         return 0;
561 }
562
563 static void fsi_master_gpio_init(struct fsi_master_gpio *master)
564 {
565         unsigned long flags;
566
567         gpiod_direction_output(master->gpio_mux, 1);
568         gpiod_direction_output(master->gpio_trans, 1);
569         gpiod_direction_output(master->gpio_enable, 1);
570         gpiod_direction_output(master->gpio_clk, 1);
571         gpiod_direction_output(master->gpio_data, 1);
572
573         /* todo: evaluate if clocks can be reduced */
574         spin_lock_irqsave(&master->bit_lock, flags);
575         clock_zeros(master, FSI_INIT_CLOCKS);
576         spin_unlock_irqrestore(&master->bit_lock, flags);
577 }
578
579 static void fsi_master_gpio_init_external(struct fsi_master_gpio *master)
580 {
581         gpiod_direction_output(master->gpio_mux, 0);
582         gpiod_direction_output(master->gpio_trans, 0);
583         gpiod_direction_output(master->gpio_enable, 1);
584         gpiod_direction_input(master->gpio_clk);
585         gpiod_direction_input(master->gpio_data);
586 }
587
588 static int fsi_master_gpio_link_enable(struct fsi_master *_master, int link)
589 {
590         struct fsi_master_gpio *master = to_fsi_master_gpio(_master);
591         int rc = -EBUSY;
592
593         if (link != 0)
594                 return -ENODEV;
595
596         mutex_lock(&master->cmd_lock);
597         if (!master->external_mode) {
598                 gpiod_set_value(master->gpio_enable, 1);
599                 rc = 0;
600         }
601         mutex_unlock(&master->cmd_lock);
602
603         return rc;
604 }
605
606 static ssize_t external_mode_show(struct device *dev,
607                 struct device_attribute *attr, char *buf)
608 {
609         struct fsi_master_gpio *master = dev_get_drvdata(dev);
610
611         return snprintf(buf, PAGE_SIZE - 1, "%u\n",
612                         master->external_mode ? 1 : 0);
613 }
614
615 static ssize_t external_mode_store(struct device *dev,
616                 struct device_attribute *attr, const char *buf, size_t count)
617 {
618         struct fsi_master_gpio *master = dev_get_drvdata(dev);
619         unsigned long val;
620         bool external_mode;
621         int err;
622
623         err = kstrtoul(buf, 0, &val);
624         if (err)
625                 return err;
626
627         external_mode = !!val;
628
629         mutex_lock(&master->cmd_lock);
630
631         if (external_mode == master->external_mode) {
632                 mutex_unlock(&master->cmd_lock);
633                 return count;
634         }
635
636         master->external_mode = external_mode;
637         if (master->external_mode)
638                 fsi_master_gpio_init_external(master);
639         else
640                 fsi_master_gpio_init(master);
641
642         mutex_unlock(&master->cmd_lock);
643
644         fsi_master_rescan(&master->master);
645
646         return count;
647 }
648
649 static DEVICE_ATTR(external_mode, 0664,
650                 external_mode_show, external_mode_store);
651
652 static int fsi_master_gpio_probe(struct platform_device *pdev)
653 {
654         struct fsi_master_gpio *master;
655         struct gpio_desc *gpio;
656         int rc;
657
658         master = devm_kzalloc(&pdev->dev, sizeof(*master), GFP_KERNEL);
659         if (!master)
660                 return -ENOMEM;
661
662         master->dev = &pdev->dev;
663         master->master.dev.parent = master->dev;
664         master->master.dev.of_node = of_node_get(dev_of_node(master->dev));
665
666         gpio = devm_gpiod_get(&pdev->dev, "clock", 0);
667         if (IS_ERR(gpio)) {
668                 dev_err(&pdev->dev, "failed to get clock gpio\n");
669                 return PTR_ERR(gpio);
670         }
671         master->gpio_clk = gpio;
672
673         gpio = devm_gpiod_get(&pdev->dev, "data", 0);
674         if (IS_ERR(gpio)) {
675                 dev_err(&pdev->dev, "failed to get data gpio\n");
676                 return PTR_ERR(gpio);
677         }
678         master->gpio_data = gpio;
679
680         /* Optional GPIOs */
681         gpio = devm_gpiod_get_optional(&pdev->dev, "trans", 0);
682         if (IS_ERR(gpio)) {
683                 dev_err(&pdev->dev, "failed to get trans gpio\n");
684                 return PTR_ERR(gpio);
685         }
686         master->gpio_trans = gpio;
687
688         gpio = devm_gpiod_get_optional(&pdev->dev, "enable", 0);
689         if (IS_ERR(gpio)) {
690                 dev_err(&pdev->dev, "failed to get enable gpio\n");
691                 return PTR_ERR(gpio);
692         }
693         master->gpio_enable = gpio;
694
695         gpio = devm_gpiod_get_optional(&pdev->dev, "mux", 0);
696         if (IS_ERR(gpio)) {
697                 dev_err(&pdev->dev, "failed to get mux gpio\n");
698                 return PTR_ERR(gpio);
699         }
700         master->gpio_mux = gpio;
701
702         /*
703          * Check if GPIO block is slow enought that no extra delays
704          * are necessary. This improves performance on ast2500 by
705          * an order of magnitude.
706          */
707         master->no_delays = device_property_present(&pdev->dev, "no-gpio-delays");
708
709         master->master.n_links = 1;
710         master->master.flags = FSI_MASTER_FLAG_SWCLOCK;
711         master->master.read = fsi_master_gpio_read;
712         master->master.write = fsi_master_gpio_write;
713         master->master.term = fsi_master_gpio_term;
714         master->master.send_break = fsi_master_gpio_break;
715         master->master.link_enable = fsi_master_gpio_link_enable;
716         platform_set_drvdata(pdev, master);
717         spin_lock_init(&master->bit_lock);
718         mutex_init(&master->cmd_lock);
719
720         fsi_master_gpio_init(master);
721
722         rc = device_create_file(&pdev->dev, &dev_attr_external_mode);
723         if (rc)
724                 return rc;
725
726         return fsi_master_register(&master->master);
727 }
728
729
730 static int fsi_master_gpio_remove(struct platform_device *pdev)
731 {
732         struct fsi_master_gpio *master = platform_get_drvdata(pdev);
733
734         devm_gpiod_put(&pdev->dev, master->gpio_clk);
735         devm_gpiod_put(&pdev->dev, master->gpio_data);
736         if (master->gpio_trans)
737                 devm_gpiod_put(&pdev->dev, master->gpio_trans);
738         if (master->gpio_enable)
739                 devm_gpiod_put(&pdev->dev, master->gpio_enable);
740         if (master->gpio_mux)
741                 devm_gpiod_put(&pdev->dev, master->gpio_mux);
742         fsi_master_unregister(&master->master);
743
744         of_node_put(master->master.dev.of_node);
745
746         return 0;
747 }
748
749 static const struct of_device_id fsi_master_gpio_match[] = {
750         { .compatible = "fsi-master-gpio" },
751         { },
752 };
753
754 static struct platform_driver fsi_master_gpio_driver = {
755         .driver = {
756                 .name           = "fsi-master-gpio",
757                 .of_match_table = fsi_master_gpio_match,
758         },
759         .probe  = fsi_master_gpio_probe,
760         .remove = fsi_master_gpio_remove,
761 };
762
763 module_platform_driver(fsi_master_gpio_driver);
764 MODULE_LICENSE("GPL");