fsi-master-gpio.c 18.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
/*
 * A FSI master controller, using a simple GPIO bit-banging interface
 */

#include <linux/crc4.h>
#include <linux/delay.h>
#include <linux/device.h>
#include <linux/fsi.h>
#include <linux/gpio/consumer.h>
#include <linux/io.h>
#include <linux/module.h>
12
#include <linux/of.h>
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
#include <linux/platform_device.h>
#include <linux/slab.h>
#include <linux/spinlock.h>

#include "fsi-master.h"

#define	FSI_GPIO_STD_DLY	1	/* Standard pin delay in nS */
#define	FSI_ECHO_DELAY_CLOCKS	16	/* Number clocks for echo delay */
#define	FSI_PRE_BREAK_CLOCKS	50	/* Number clocks to prep for break */
#define	FSI_BREAK_CLOCKS	256	/* Number of clocks to issue break */
#define	FSI_POST_BREAK_CLOCKS	16000	/* Number clocks to set up cfam */
#define	FSI_INIT_CLOCKS		5000	/* Clock out any old data */
#define	FSI_GPIO_STD_DELAY	10	/* Standard GPIO delay in nS */
					/* todo: adjust down as low as */
					/* possible or eliminate */
#define	FSI_GPIO_CMD_DPOLL      0x2
#define	FSI_GPIO_CMD_TERM	0x3f
#define FSI_GPIO_CMD_ABS_AR	0x4

32 33

#define	FSI_GPIO_DPOLL_CLOCKS	50      /* < 21 will cause slave to hang */
34 35 36 37 38 39 40 41 42 43 44 45 46

/* Bus errors */
#define	FSI_GPIO_ERR_BUSY	1	/* Slave stuck in busy state */
#define	FSI_GPIO_RESP_ERRA	2	/* Any (misc) Error */
#define	FSI_GPIO_RESP_ERRC	3	/* Slave reports master CRC error */
#define	FSI_GPIO_MTOE		4	/* Master time out error */
#define	FSI_GPIO_CRC_INVAL	5	/* Master reports slave CRC error */

/* Normal slave responses */
#define	FSI_GPIO_RESP_BUSY	1
#define	FSI_GPIO_RESP_ACK	0
#define	FSI_GPIO_RESP_ACKD	4

47
#define	FSI_GPIO_MAX_BUSY	200
48 49 50 51 52
#define	FSI_GPIO_MTOE_COUNT	1000
#define	FSI_GPIO_DRAIN_BITS	20
#define	FSI_GPIO_CRC_SIZE	4
#define	FSI_GPIO_MSG_ID_SIZE		2
#define	FSI_GPIO_MSG_RESPID_SIZE	2
53
#define	FSI_GPIO_PRIME_SLAVE_CLOCKS	20
54 55 56 57

struct fsi_master_gpio {
	struct fsi_master	master;
	struct device		*dev;
58 59
	struct mutex		cmd_lock;	/* mutex for command ordering */
	spinlock_t		bit_lock;	/* lock for clocking bits out */
60 61 62 63 64
	struct gpio_desc	*gpio_clk;
	struct gpio_desc	*gpio_data;
	struct gpio_desc	*gpio_trans;	/* Voltage translator */
	struct gpio_desc	*gpio_enable;	/* FSI enable */
	struct gpio_desc	*gpio_mux;	/* Mux control */
65
	bool			external_mode;
66
	bool			no_delays;
67 68
};

69 70 71
#define CREATE_TRACE_POINTS
#include <trace/events/fsi_master_gpio.h>

72 73 74 75 76 77 78 79 80 81 82 83
#define to_fsi_master_gpio(m) container_of(m, struct fsi_master_gpio, master)

struct fsi_gpio_msg {
	uint64_t	msg;
	uint8_t		bits;
};

static void clock_toggle(struct fsi_master_gpio *master, int count)
{
	int i;

	for (i = 0; i < count; i++) {
84 85
		if (!master->no_delays)
			ndelay(FSI_GPIO_STD_DLY);
86
		gpiod_set_value(master->gpio_clk, 0);
87 88
		if (!master->no_delays)
			ndelay(FSI_GPIO_STD_DLY);
89 90 91 92
		gpiod_set_value(master->gpio_clk, 1);
	}
}

93
static int sda_clock_in(struct fsi_master_gpio *master)
94 95 96
{
	int in;

97 98
	if (!master->no_delays)
		ndelay(FSI_GPIO_STD_DLY);
99
	gpiod_set_value(master->gpio_clk, 0);
100 101 102 103 104

	/* Dummy read to feed the synchronizers */
	gpiod_get_value(master->gpio_data);

	/* Actual data read */
105
	in = gpiod_get_value(master->gpio_data);
106 107
	if (!master->no_delays)
		ndelay(FSI_GPIO_STD_DLY);
108
	gpiod_set_value(master->gpio_clk, 1);
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
	return in ? 1 : 0;
}

static void sda_out(struct fsi_master_gpio *master, int value)
{
	gpiod_set_value(master->gpio_data, value);
}

static void set_sda_input(struct fsi_master_gpio *master)
{
	gpiod_direction_input(master->gpio_data);
	gpiod_set_value(master->gpio_trans, 0);
}

static void set_sda_output(struct fsi_master_gpio *master, int value)
{
	gpiod_set_value(master->gpio_trans, 1);
	gpiod_direction_output(master->gpio_data, value);
}

static void clock_zeros(struct fsi_master_gpio *master, int count)
{
	set_sda_output(master, 1);
	clock_toggle(master, count);
}

static void serial_in(struct fsi_master_gpio *master, struct fsi_gpio_msg *msg,
			uint8_t num_bits)
{
	uint8_t bit, in_bit;

	set_sda_input(master);

	for (bit = 0; bit < num_bits; bit++) {
143
		in_bit = sda_clock_in(master);
144 145 146 147
		msg->msg <<= 1;
		msg->msg |= ~in_bit & 0x1;	/* Data is active low */
	}
	msg->bits += num_bits;
148 149

	trace_fsi_master_gpio_in(master, num_bits, msg->msg);
150 151 152 153 154 155 156 157 158 159 160
}

static void serial_out(struct fsi_master_gpio *master,
			const struct fsi_gpio_msg *cmd)
{
	uint8_t bit;
	uint64_t msg = ~cmd->msg;	/* Data is active low */
	uint64_t sda_mask = 0x1ULL << (cmd->bits - 1);
	uint64_t last_bit = ~0;
	int next_bit;

161 162
	trace_fsi_master_gpio_out(master, cmd->bits, cmd->msg);

163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
	if (!cmd->bits) {
		dev_warn(master->dev, "trying to output 0 bits\n");
		return;
	}
	set_sda_output(master, 0);

	/* Send the start bit */
	sda_out(master, 0);
	clock_toggle(master, 1);

	/* Send the message */
	for (bit = 0; bit < cmd->bits; bit++) {
		next_bit = (msg & sda_mask) >> (cmd->bits - 1);
		if (last_bit ^ next_bit) {
			sda_out(master, next_bit);
			last_bit = next_bit;
		}
		clock_toggle(master, 1);
		msg <<= 1;
	}
}

static void msg_push_bits(struct fsi_gpio_msg *msg, uint64_t data, int bits)
{
	msg->msg <<= bits;
	msg->msg |= data & ((1ull << bits) - 1);
	msg->bits += bits;
}

static void msg_push_crc(struct fsi_gpio_msg *msg)
{
	uint8_t crc;
	int top;

	top = msg->bits & 0x3;

	/* start bit, and any non-aligned top bits */
	crc = crc4(0, 1 << top | msg->msg >> (msg->bits - top), top + 1);

	/* aligned bits */
	crc = crc4(crc, msg->msg, msg->bits - top);

	msg_push_bits(msg, crc, 4);
}

/*
 * Encode an Absolute Address command
 */
static void build_abs_ar_command(struct fsi_gpio_msg *cmd,
		uint8_t id, uint32_t addr, size_t size, const void *data)
{
	bool write = !!data;
	uint8_t ds;
	int i;

	cmd->bits = 0;
	cmd->msg = 0;

	msg_push_bits(cmd, id, 2);
	msg_push_bits(cmd, FSI_GPIO_CMD_ABS_AR, 3);
	msg_push_bits(cmd, write ? 0 : 1, 1);

	/*
	 * The read/write size is encoded in the lower bits of the address
	 * (as it must be naturally-aligned), and the following ds bit.
	 *
	 *	size	addr:1	addr:0	ds
	 *	1	x	x	0
	 *	2	x	0	1
	 *	4	0	1	1
	 *
	 */
	ds = size > 1 ? 1 : 0;
	addr &= ~(size - 1);
	if (size == 4)
		addr |= 1;

	msg_push_bits(cmd, addr & ((1 << 21) - 1), 21);
	msg_push_bits(cmd, ds, 1);
	for (i = 0; write && i < size; i++)
		msg_push_bits(cmd, ((uint8_t *)data)[i], 8);

	msg_push_crc(cmd);
}

static void build_dpoll_command(struct fsi_gpio_msg *cmd, uint8_t slave_id)
{
	cmd->bits = 0;
	cmd->msg = 0;

	msg_push_bits(cmd, slave_id, 2);
	msg_push_bits(cmd, FSI_GPIO_CMD_DPOLL, 3);
	msg_push_crc(cmd);
}

static void echo_delay(struct fsi_master_gpio *master)
{
	set_sda_output(master, 1);
	clock_toggle(master, FSI_ECHO_DELAY_CLOCKS);
}

static void build_term_command(struct fsi_gpio_msg *cmd, uint8_t slave_id)
{
	cmd->bits = 0;
	cmd->msg = 0;

	msg_push_bits(cmd, slave_id, 2);
	msg_push_bits(cmd, FSI_GPIO_CMD_TERM, 6);
	msg_push_crc(cmd);
}

/*
 * Store information on master errors so handler can detect and clean
 * up the bus
 */
static void fsi_master_gpio_error(struct fsi_master_gpio *master, int error)
{

}

static int read_one_response(struct fsi_master_gpio *master,
		uint8_t data_size, struct fsi_gpio_msg *msgp, uint8_t *tagp)
{
	struct fsi_gpio_msg msg;
287
	unsigned long flags;
288
	uint32_t crc;
289
	uint8_t tag;
290 291
	int i;

292 293
	spin_lock_irqsave(&master->bit_lock, flags);

294 295 296 297 298 299 300 301 302 303 304 305
	/* wait for the start bit */
	for (i = 0; i < FSI_GPIO_MTOE_COUNT; i++) {
		msg.bits = 0;
		msg.msg = 0;
		serial_in(master, &msg, 1);
		if (msg.msg)
			break;
	}
	if (i == FSI_GPIO_MTOE_COUNT) {
		dev_dbg(master->dev,
			"Master time out waiting for response\n");
		fsi_master_gpio_error(master, FSI_GPIO_MTOE);
306
		spin_unlock_irqrestore(&master->bit_lock, flags);
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
		return -EIO;
	}

	msg.bits = 0;
	msg.msg = 0;

	/* Read slave ID & response tag */
	serial_in(master, &msg, 4);

	tag = msg.msg & 0x3;

	/* If we have an ACK and we're expecting data, clock the data in too */
	if (tag == FSI_GPIO_RESP_ACK && data_size)
		serial_in(master, &msg, data_size * 8);

	/* read CRC */
	serial_in(master, &msg, FSI_GPIO_CRC_SIZE);

325 326
	spin_unlock_irqrestore(&master->bit_lock, flags);

327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
	/* we have a whole message now; check CRC */
	crc = crc4(0, 1, 1);
	crc = crc4(crc, msg.msg, msg.bits);
	if (crc) {
		dev_dbg(master->dev, "ERR response CRC\n");
		fsi_master_gpio_error(master, FSI_GPIO_CRC_INVAL);
		return -EIO;
	}

	if (msgp)
		*msgp = msg;
	if (tagp)
		*tagp = tag;

	return 0;
}

static int issue_term(struct fsi_master_gpio *master, uint8_t slave)
{
	struct fsi_gpio_msg cmd;
347
	unsigned long flags;
348 349 350 351
	uint8_t tag;
	int rc;

	build_term_command(&cmd, slave);
352 353

	spin_lock_irqsave(&master->bit_lock, flags);
354 355
	serial_out(master, &cmd);
	echo_delay(master);
356
	spin_unlock_irqrestore(&master->bit_lock, flags);
357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375

	rc = read_one_response(master, 0, NULL, &tag);
	if (rc < 0) {
		dev_err(master->dev,
				"TERM failed; lost communication with slave\n");
		return -EIO;
	} else if (tag != FSI_GPIO_RESP_ACK) {
		dev_err(master->dev, "TERM failed; response %d\n", tag);
		return -EIO;
	}

	return 0;
}

static int poll_for_response(struct fsi_master_gpio *master,
		uint8_t slave, uint8_t size, void *data)
{
	struct fsi_gpio_msg response, cmd;
	int busy_count = 0, rc, i;
376
	unsigned long flags;
377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406
	uint8_t tag;
	uint8_t *data_byte = data;

retry:
	rc = read_one_response(master, size, &response, &tag);
	if (rc)
		return rc;

	switch (tag) {
	case FSI_GPIO_RESP_ACK:
		if (size && data) {
			uint64_t val = response.msg;
			/* clear crc & mask */
			val >>= 4;
			val &= (1ull << (size * 8)) - 1;

			for (i = 0; i < size; i++) {
				data_byte[size-i-1] = val;
				val >>= 8;
			}
		}
		break;
	case FSI_GPIO_RESP_BUSY:
		/*
		 * Its necessary to clock slave before issuing
		 * d-poll, not indicated in the hardware protocol
		 * spec. < 20 clocks causes slave to hang, 21 ok.
		 */
		if (busy_count++ < FSI_GPIO_MAX_BUSY) {
			build_dpoll_command(&cmd, slave);
407 408
			spin_lock_irqsave(&master->bit_lock, flags);
			clock_zeros(master, FSI_GPIO_DPOLL_CLOCKS);
409 410
			serial_out(master, &cmd);
			echo_delay(master);
411
			spin_unlock_irqrestore(&master->bit_lock, flags);
412 413 414 415
			goto retry;
		}
		dev_warn(master->dev,
			"ERR slave is stuck in busy state, issuing TERM\n");
416 417 418
		spin_lock_irqsave(&master->bit_lock, flags);
		clock_zeros(master, FSI_GPIO_DPOLL_CLOCKS);
		spin_unlock_irqrestore(&master->bit_lock, flags);
419 420 421 422 423 424 425 426 427 428 429 430 431 432
		issue_term(master, slave);
		rc = -EIO;
		break;

	case FSI_GPIO_RESP_ERRA:
	case FSI_GPIO_RESP_ERRC:
		dev_dbg(master->dev, "ERR%c received: 0x%x\n",
			tag == FSI_GPIO_RESP_ERRA ? 'A' : 'C',
			(int)response.msg);
		fsi_master_gpio_error(master, response.msg);
		rc = -EIO;
		break;
	}

A
Andrew Jeffery 已提交
433 434 435
	if (busy_count > 0)
		trace_fsi_master_gpio_poll_response_busy(master, busy_count);

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 441 442
	return rc;
}

443 444
static int send_request(struct fsi_master_gpio *master,
		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 452 453
		return -EBUSY;
	}

454 455
	serial_out(master, cmd);
	echo_delay(master);
456 457 458 459 460 461 462 463 464 465 466 467 468 469
	spin_unlock_irqrestore(&master->bit_lock, flags);

	return 0;
}

static int fsi_master_gpio_xfer(struct fsi_master_gpio *master, uint8_t slave,
		struct fsi_gpio_msg *cmd, size_t resp_len, void *resp)
{
	int rc;

	rc = send_request(master, cmd);
	if (!rc)
		rc = poll_for_response(master, slave, resp_len, resp);

470 471 472 473 474 475 476 477
	return rc;
}

static int fsi_master_gpio_read(struct fsi_master *_master, int link,
		uint8_t id, uint32_t addr, void *val, size_t size)
{
	struct fsi_master_gpio *master = to_fsi_master_gpio(_master);
	struct fsi_gpio_msg cmd;
478
	int rc;
479 480 481 482

	if (link != 0)
		return -ENODEV;

483
	mutex_lock(&master->cmd_lock);
484
	build_abs_ar_command(&cmd, id, addr, size, NULL);
485 486 487 488
	rc = fsi_master_gpio_xfer(master, id, &cmd, size, val);
	mutex_unlock(&master->cmd_lock);

	return rc;
489 490 491 492 493 494 495
}

static int fsi_master_gpio_write(struct fsi_master *_master, int link,
		uint8_t id, uint32_t addr, const void *val, size_t size)
{
	struct fsi_master_gpio *master = to_fsi_master_gpio(_master);
	struct fsi_gpio_msg cmd;
496
	int rc;
497 498 499 500

	if (link != 0)
		return -ENODEV;

501
	mutex_lock(&master->cmd_lock);
502
	build_abs_ar_command(&cmd, id, addr, size, val);
503 504 505 506
	rc = fsi_master_gpio_xfer(master, id, &cmd, 0, NULL);
	mutex_unlock(&master->cmd_lock);

	return rc;
507 508 509 510 511 512 513
}

static int fsi_master_gpio_term(struct fsi_master *_master,
		int link, uint8_t id)
{
	struct fsi_master_gpio *master = to_fsi_master_gpio(_master);
	struct fsi_gpio_msg cmd;
514
	int rc;
515 516 517 518

	if (link != 0)
		return -ENODEV;

519
	mutex_lock(&master->cmd_lock);
520
	build_term_command(&cmd, id);
521 522 523 524
	rc = fsi_master_gpio_xfer(master, id, &cmd, 0, NULL);
	mutex_unlock(&master->cmd_lock);

	return rc;
525 526 527 528 529
}

static int fsi_master_gpio_break(struct fsi_master *_master, int link)
{
	struct fsi_master_gpio *master = to_fsi_master_gpio(_master);
530
	unsigned long flags;
531 532 533 534

	if (link != 0)
		return -ENODEV;

535 536
	trace_fsi_master_gpio_break(master);

537
	mutex_lock(&master->cmd_lock);
538
	if (master->external_mode) {
539
		mutex_unlock(&master->cmd_lock);
540 541
		return -EBUSY;
	}
542 543 544

	spin_lock_irqsave(&master->bit_lock, flags);

545 546 547 548 549 550 551 552
	set_sda_output(master, 1);
	sda_out(master, 1);
	clock_toggle(master, FSI_PRE_BREAK_CLOCKS);
	sda_out(master, 0);
	clock_toggle(master, FSI_BREAK_CLOCKS);
	echo_delay(master);
	sda_out(master, 1);
	clock_toggle(master, FSI_POST_BREAK_CLOCKS);
553 554 555

	spin_unlock_irqrestore(&master->bit_lock, flags);
	mutex_unlock(&master->cmd_lock);
556 557 558 559 560 561 562 563 564

	/* Wait for logic reset to take effect */
	udelay(200);

	return 0;
}

static void fsi_master_gpio_init(struct fsi_master_gpio *master)
{
565 566
	unsigned long flags;

567 568 569 570 571 572 573
	gpiod_direction_output(master->gpio_mux, 1);
	gpiod_direction_output(master->gpio_trans, 1);
	gpiod_direction_output(master->gpio_enable, 1);
	gpiod_direction_output(master->gpio_clk, 1);
	gpiod_direction_output(master->gpio_data, 1);

	/* 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 580 581 582 583 584 585 586 587
static void fsi_master_gpio_init_external(struct fsi_master_gpio *master)
{
	gpiod_direction_output(master->gpio_mux, 0);
	gpiod_direction_output(master->gpio_trans, 0);
	gpiod_direction_output(master->gpio_enable, 1);
	gpiod_direction_input(master->gpio_clk);
	gpiod_direction_input(master->gpio_data);
}

588 589 590
static int fsi_master_gpio_link_enable(struct fsi_master *_master, int link)
{
	struct fsi_master_gpio *master = to_fsi_master_gpio(_master);
591
	int rc = -EBUSY;
592 593 594

	if (link != 0)
		return -ENODEV;
595

596
	mutex_lock(&master->cmd_lock);
597 598 599 600
	if (!master->external_mode) {
		gpiod_set_value(master->gpio_enable, 1);
		rc = 0;
	}
601
	mutex_unlock(&master->cmd_lock);
602

603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618
	return rc;
}

static ssize_t external_mode_show(struct device *dev,
		struct device_attribute *attr, char *buf)
{
	struct fsi_master_gpio *master = dev_get_drvdata(dev);

	return snprintf(buf, PAGE_SIZE - 1, "%u\n",
			master->external_mode ? 1 : 0);
}

static ssize_t external_mode_store(struct device *dev,
		struct device_attribute *attr, const char *buf, size_t count)
{
	struct fsi_master_gpio *master = dev_get_drvdata(dev);
619
	unsigned long val;
620 621 622 623 624 625 626 627 628
	bool external_mode;
	int err;

	err = kstrtoul(buf, 0, &val);
	if (err)
		return err;

	external_mode = !!val;

629
	mutex_lock(&master->cmd_lock);
630 631

	if (external_mode == master->external_mode) {
632
		mutex_unlock(&master->cmd_lock);
633 634 635 636 637 638 639 640
		return count;
	}

	master->external_mode = external_mode;
	if (master->external_mode)
		fsi_master_gpio_init_external(master);
	else
		fsi_master_gpio_init(master);
641 642

	mutex_unlock(&master->cmd_lock);
643 644 645 646

	fsi_master_rescan(&master->master);

	return count;
647 648
}

649 650 651
static DEVICE_ATTR(external_mode, 0664,
		external_mode_show, external_mode_store);

652 653 654 655
static int fsi_master_gpio_probe(struct platform_device *pdev)
{
	struct fsi_master_gpio *master;
	struct gpio_desc *gpio;
656
	int rc;
657 658 659 660 661 662 663

	master = devm_kzalloc(&pdev->dev, sizeof(*master), GFP_KERNEL);
	if (!master)
		return -ENOMEM;

	master->dev = &pdev->dev;
	master->master.dev.parent = master->dev;
664
	master->master.dev.of_node = of_node_get(dev_of_node(master->dev));
665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701

	gpio = devm_gpiod_get(&pdev->dev, "clock", 0);
	if (IS_ERR(gpio)) {
		dev_err(&pdev->dev, "failed to get clock gpio\n");
		return PTR_ERR(gpio);
	}
	master->gpio_clk = gpio;

	gpio = devm_gpiod_get(&pdev->dev, "data", 0);
	if (IS_ERR(gpio)) {
		dev_err(&pdev->dev, "failed to get data gpio\n");
		return PTR_ERR(gpio);
	}
	master->gpio_data = gpio;

	/* Optional GPIOs */
	gpio = devm_gpiod_get_optional(&pdev->dev, "trans", 0);
	if (IS_ERR(gpio)) {
		dev_err(&pdev->dev, "failed to get trans gpio\n");
		return PTR_ERR(gpio);
	}
	master->gpio_trans = gpio;

	gpio = devm_gpiod_get_optional(&pdev->dev, "enable", 0);
	if (IS_ERR(gpio)) {
		dev_err(&pdev->dev, "failed to get enable gpio\n");
		return PTR_ERR(gpio);
	}
	master->gpio_enable = gpio;

	gpio = devm_gpiod_get_optional(&pdev->dev, "mux", 0);
	if (IS_ERR(gpio)) {
		dev_err(&pdev->dev, "failed to get mux gpio\n");
		return PTR_ERR(gpio);
	}
	master->gpio_mux = gpio;

702 703 704 705 706 707 708
	/*
	 * Check if GPIO block is slow enought that no extra delays
	 * are necessary. This improves performance on ast2500 by
	 * an order of magnitude.
	 */
	master->no_delays = device_property_present(&pdev->dev, "no-gpio-delays");

709
	master->master.n_links = 1;
710
	master->master.flags = FSI_MASTER_FLAG_SWCLOCK;
711 712 713 714 715 716
	master->master.read = fsi_master_gpio_read;
	master->master.write = fsi_master_gpio_write;
	master->master.term = fsi_master_gpio_term;
	master->master.send_break = fsi_master_gpio_break;
	master->master.link_enable = fsi_master_gpio_link_enable;
	platform_set_drvdata(pdev, master);
717 718
	spin_lock_init(&master->bit_lock);
	mutex_init(&master->cmd_lock);
719 720 721

	fsi_master_gpio_init(master);

722 723 724 725
	rc = device_create_file(&pdev->dev, &dev_attr_external_mode);
	if (rc)
		return rc;

726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743
	return fsi_master_register(&master->master);
}


static int fsi_master_gpio_remove(struct platform_device *pdev)
{
	struct fsi_master_gpio *master = platform_get_drvdata(pdev);

	devm_gpiod_put(&pdev->dev, master->gpio_clk);
	devm_gpiod_put(&pdev->dev, master->gpio_data);
	if (master->gpio_trans)
		devm_gpiod_put(&pdev->dev, master->gpio_trans);
	if (master->gpio_enable)
		devm_gpiod_put(&pdev->dev, master->gpio_enable);
	if (master->gpio_mux)
		devm_gpiod_put(&pdev->dev, master->gpio_mux);
	fsi_master_unregister(&master->master);

744 745
	of_node_put(master->master.dev.of_node);

746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764
	return 0;
}

static const struct of_device_id fsi_master_gpio_match[] = {
	{ .compatible = "fsi-master-gpio" },
	{ },
};

static struct platform_driver fsi_master_gpio_driver = {
	.driver = {
		.name		= "fsi-master-gpio",
		.of_match_table	= fsi_master_gpio_match,
	},
	.probe	= fsi_master_gpio_probe,
	.remove = fsi_master_gpio_remove,
};

module_platform_driver(fsi_master_gpio_driver);
MODULE_LICENSE("GPL");