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