ptp_clockmatrix.c 48.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
// SPDX-License-Identifier: GPL-2.0+
/*
 * PTP hardware clock driver for the IDT ClockMatrix(TM) family of timing and
 * synchronization devices.
 *
 * Copyright (C) 2019 Integrated Device Technology, Inc., a Renesas Company.
 */
#include <linux/firmware.h>
#include <linux/i2c.h>
#include <linux/module.h>
#include <linux/ptp_clock_kernel.h>
#include <linux/delay.h>
13
#include <linux/jiffies.h>
14 15
#include <linux/kernel.h>
#include <linux/timekeeping.h>
16
#include <linux/string.h>
17 18 19 20 21 22 23 24 25 26

#include "ptp_private.h"
#include "ptp_clockmatrix.h"

MODULE_DESCRIPTION("Driver for IDT ClockMatrix(TM) family");
MODULE_AUTHOR("Richard Cochran <richardcochran@gmail.com>");
MODULE_AUTHOR("IDT support-1588 <IDT-support-1588@lm.renesas.com>");
MODULE_VERSION("1.0");
MODULE_LICENSE("GPL");

27 28 29 30 31 32 33
/*
 * The name of the firmware file to be loaded
 * over-rides any automatic selection
 */
static char *firmware;
module_param(firmware, charp, 0);

34 35
#define SETTIME_CORRECTION (0)

36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
static int contains_full_configuration(const struct firmware *fw)
{
	s32 full_count = FULL_FW_CFG_BYTES - FULL_FW_CFG_SKIPPED_BYTES;
	struct idtcm_fwrc *rec = (struct idtcm_fwrc *)fw->data;
	s32 count = 0;
	u16 regaddr;
	u8 loaddr;
	s32 len;

	/* If the firmware contains 'full configuration' SM_RESET can be used
	 * to ensure proper configuration.
	 *
	 * Full configuration is defined as the number of programmable
	 * bytes within the configuration range minus page offset addr range.
	 */
	for (len = fw->size; len > 0; len -= sizeof(*rec)) {
		regaddr = rec->hiaddr << 8;
		regaddr |= rec->loaddr;

		loaddr = rec->loaddr;

		rec++;

		/* Top (status registers) and bottom are read-only */
		if (regaddr < GPIO_USER_CONTROL || regaddr >= SCRATCH)
			continue;

		/* Page size 128, last 4 bytes of page skipped */
		if ((loaddr > 0x7b && loaddr <= 0x7f) || loaddr > 0xfb)
			continue;

		count++;
	}

	return (count >= full_count);
}

73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
static int char_array_to_timespec(u8 *buf,
				  u8 count,
				  struct timespec64 *ts)
{
	u8 i;
	u64 nsec;
	time64_t sec;

	if (count < TOD_BYTE_COUNT)
		return 1;

	/* Sub-nanoseconds are in buf[0]. */
	nsec = buf[4];
	for (i = 0; i < 3; i++) {
		nsec <<= 8;
		nsec |= buf[3 - i];
	}

	sec = buf[10];
	for (i = 0; i < 5; i++) {
		sec <<= 8;
		sec |= buf[9 - i];
	}

	ts->tv_sec = sec;
	ts->tv_nsec = nsec;

	return 0;
}

static int timespec_to_char_array(struct timespec64 const *ts,
				  u8 *buf,
				  u8 count)
{
	u8 i;
	s32 nsec;
	time64_t sec;

	if (count < TOD_BYTE_COUNT)
		return 1;

	nsec = ts->tv_nsec;
	sec = ts->tv_sec;

	/* Sub-nanoseconds are in buf[0]. */
	buf[0] = 0;
	for (i = 1; i < 5; i++) {
		buf[i] = nsec & 0xff;
		nsec >>= 8;
	}

	for (i = 5; i < TOD_BYTE_COUNT; i++) {

		buf[i] = sec & 0xff;
		sec >>= 8;
	}

	return 0;
}

133
static int idtcm_strverscmp(const char *version1, const char *version2)
134
{
135 136
	u8 ver1[3], ver2[3];
	int i;
137

138 139 140 141 142 143
	if (sscanf(version1, "%hhu.%hhu.%hhu",
		   &ver1[0], &ver1[1], &ver1[2]) != 3)
		return -1;
	if (sscanf(version2, "%hhu.%hhu.%hhu",
		   &ver2[0], &ver2[1], &ver2[2]) != 3)
		return -1;
144

145 146 147 148 149
	for (i = 0; i < 3; i++) {
		if (ver1[i] > ver2[i])
			return 1;
		if (ver1[i] < ver2[i])
			return -1;
150
	}
151 152

	return 0;
153 154
}

155 156 157 158
static int idtcm_xfer_read(struct idtcm *idtcm,
			   u8 regaddr,
			   u8 *buf,
			   u16 count)
159 160 161 162 163 164 165 166 167 168 169
{
	struct i2c_client *client = idtcm->client;
	struct i2c_msg msg[2];
	int cnt;

	msg[0].addr = client->addr;
	msg[0].flags = 0;
	msg[0].len = 1;
	msg[0].buf = &regaddr;

	msg[1].addr = client->addr;
170
	msg[1].flags = I2C_M_RD;
171 172 173 174 175 176
	msg[1].len = count;
	msg[1].buf = buf;

	cnt = i2c_transfer(client->adapter, msg, 2);

	if (cnt < 0) {
177
		dev_err(&client->dev,
178 179
			"i2c_transfer failed at %d in %s, at addr: %04x!",
			__LINE__, __func__, regaddr);
180 181 182
		return cnt;
	} else if (cnt != 2) {
		dev_err(&client->dev,
183
			"i2c_transfer sent only %d of %d messages", cnt, 2);
184 185 186 187 188 189
		return -EIO;
	}

	return 0;
}

190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
static int idtcm_xfer_write(struct idtcm *idtcm,
			    u8 regaddr,
			    u8 *buf,
			    u16 count)
{
	struct i2c_client *client = idtcm->client;
	/* we add 1 byte for device register */
	u8 msg[IDTCM_MAX_WRITE_COUNT + 1];
	int cnt;

	if (count > IDTCM_MAX_WRITE_COUNT)
		return -EINVAL;

	msg[0] = regaddr;
	memcpy(&msg[1], buf, count);

	cnt = i2c_master_send(client, msg, count + 1);

	if (cnt < 0) {
		dev_err(&client->dev,
210 211
			"i2c_master_send failed at %d in %s, at addr: %04x!",
			__LINE__, __func__, regaddr);
212 213 214 215 216 217
		return cnt;
	}

	return 0;
}

218 219 220 221 222 223 224 225 226 227 228 229 230
static int idtcm_page_offset(struct idtcm *idtcm, u8 val)
{
	u8 buf[4];
	int err;

	if (idtcm->page_offset == val)
		return 0;

	buf[0] = 0x0;
	buf[1] = val;
	buf[2] = 0x10;
	buf[3] = 0x20;

231
	err = idtcm_xfer_write(idtcm, PAGE_ADDR, buf, sizeof(buf));
232

233 234
	if (err) {
		idtcm->page_offset = 0xff;
235
		dev_err(&idtcm->client->dev, "failed to set page offset");
236
	} else {
237
		idtcm->page_offset = val;
238
	}
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258

	return err;
}

static int _idtcm_rdwr(struct idtcm *idtcm,
		       u16 regaddr,
		       u8 *buf,
		       u16 count,
		       bool write)
{
	u8 hi;
	u8 lo;
	int err;

	hi = (regaddr >> 8) & 0xff;
	lo = regaddr & 0xff;

	err = idtcm_page_offset(idtcm, hi);

	if (err)
259
		return err;
260

261 262 263 264
	if (write)
		return idtcm_xfer_write(idtcm, lo, buf, count);

	return idtcm_xfer_read(idtcm, lo, buf, count);
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
}

static int idtcm_read(struct idtcm *idtcm,
		      u16 module,
		      u16 regaddr,
		      u8 *buf,
		      u16 count)
{
	return _idtcm_rdwr(idtcm, module + regaddr, buf, count, false);
}

static int idtcm_write(struct idtcm *idtcm,
		       u16 module,
		       u16 regaddr,
		       u8 *buf,
		       u16 count)
{
	return _idtcm_rdwr(idtcm, module + regaddr, buf, count, true);
}

285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
static int clear_boot_status(struct idtcm *idtcm)
{
	int err;
	u8 buf[4] = {0};

	err = idtcm_write(idtcm, GENERAL_STATUS, BOOT_STATUS, buf, sizeof(buf));

	return err;
}

static int read_boot_status(struct idtcm *idtcm, u32 *status)
{
	int err;
	u8 buf[4] = {0};

	err = idtcm_read(idtcm, GENERAL_STATUS, BOOT_STATUS, buf, sizeof(buf));

	*status = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];

	return err;
}

static int wait_for_boot_status_ready(struct idtcm *idtcm)
{
	u32 status = 0;
	u8 i = 30;	/* 30 * 100ms = 3s */
	int err;

	do {
		err = read_boot_status(idtcm, &status);

		if (err)
			return err;

		if (status == 0xA0)
			return 0;

		msleep(100);
		i--;

	} while (i);

327
	dev_warn(&idtcm->client->dev, "%s timed out", __func__);
328 329 330 331

	return -EBUSY;
}

332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
static int read_sys_apll_status(struct idtcm *idtcm, u8 *status)
{
	return idtcm_read(idtcm, STATUS, DPLL_SYS_APLL_STATUS, status,
			  sizeof(u8));
}

static int read_sys_dpll_status(struct idtcm *idtcm, u8 *status)
{
	return idtcm_read(idtcm, STATUS, DPLL_SYS_STATUS, status, sizeof(u8));
}

static int wait_for_sys_apll_dpll_lock(struct idtcm *idtcm)
{
	unsigned long timeout = jiffies + msecs_to_jiffies(LOCK_TIMEOUT_MS);
	u8 apll = 0;
	u8 dpll = 0;
	int err;

	do {
		err = read_sys_apll_status(idtcm, &apll);
		if (err)
			return err;

		err = read_sys_dpll_status(idtcm, &dpll);
		if (err)
			return err;

		apll &= SYS_APLL_LOSS_LOCK_LIVE_MASK;
		dpll &= DPLL_SYS_STATE_MASK;

		if (apll == SYS_APLL_LOSS_LOCK_LIVE_LOCKED &&
		    dpll == DPLL_STATE_LOCKED) {
			return 0;
		} else if (dpll == DPLL_STATE_FREERUN ||
			   dpll == DPLL_STATE_HOLDOVER ||
			   dpll == DPLL_STATE_OPEN_LOOP) {
			dev_warn(&idtcm->client->dev,
				"No wait state: DPLL_SYS_STATE %d", dpll);
			return -EPERM;
		}

		msleep(LOCK_POLL_INTERVAL_MS);
	} while (time_is_after_jiffies(timeout));

	dev_warn(&idtcm->client->dev,
		 "%d ms lock timeout: SYS APLL Loss Lock %d  SYS DPLL state %d",
		 LOCK_TIMEOUT_MS, apll, dpll);

	return -ETIME;
}

static void wait_for_chip_ready(struct idtcm *idtcm)
{
	if (wait_for_boot_status_ready(idtcm))
		dev_warn(&idtcm->client->dev, "BOOT_STATUS != 0xA0");

	if (wait_for_sys_apll_dpll_lock(idtcm))
		dev_warn(&idtcm->client->dev,
			 "Continuing while SYS APLL/DPLL is not locked");
}

393 394 395 396 397
static int _idtcm_gettime(struct idtcm_channel *channel,
			  struct timespec64 *ts)
{
	struct idtcm *idtcm = channel->idtcm;
	u8 buf[TOD_BYTE_COUNT];
398
	u8 timeout = 10;
399 400 401 402 403 404 405 406 407 408
	u8 trigger;
	int err;

	err = idtcm_read(idtcm, channel->tod_read_primary,
			 TOD_READ_PRIMARY_CMD, &trigger, sizeof(trigger));
	if (err)
		return err;

	trigger &= ~(TOD_READ_TRIGGER_MASK << TOD_READ_TRIGGER_SHIFT);
	trigger |= (1 << TOD_READ_TRIGGER_SHIFT);
409
	trigger &= ~TOD_READ_TRIGGER_MODE; /* single shot */
410 411 412 413 414 415

	err = idtcm_write(idtcm, channel->tod_read_primary,
			  TOD_READ_PRIMARY_CMD, &trigger, sizeof(trigger));
	if (err)
		return err;

416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431
	/* wait trigger to be 0 */
	while (trigger & TOD_READ_TRIGGER_MASK) {

		if (idtcm->calculate_overhead_flag)
			idtcm->start_time = ktime_get_raw();

		err = idtcm_read(idtcm, channel->tod_read_primary,
				 TOD_READ_PRIMARY_CMD, &trigger,
				 sizeof(trigger));

		if (err)
			return err;

		if (--timeout == 0)
			return -EIO;
	}
432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453

	err = idtcm_read(idtcm, channel->tod_read_primary,
			 TOD_READ_PRIMARY, buf, sizeof(buf));

	if (err)
		return err;

	err = char_array_to_timespec(buf, sizeof(buf), ts);

	return err;
}

static int _sync_pll_output(struct idtcm *idtcm,
			    u8 pll,
			    u8 sync_src,
			    u8 qn,
			    u8 qn_plus_1)
{
	int err;
	u8 val;
	u16 sync_ctrl0;
	u16 sync_ctrl1;
454
	u8 temp;
455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519

	if ((qn == 0) && (qn_plus_1 == 0))
		return 0;

	switch (pll) {
	case 0:
		sync_ctrl0 = HW_Q0_Q1_CH_SYNC_CTRL_0;
		sync_ctrl1 = HW_Q0_Q1_CH_SYNC_CTRL_1;
		break;
	case 1:
		sync_ctrl0 = HW_Q2_Q3_CH_SYNC_CTRL_0;
		sync_ctrl1 = HW_Q2_Q3_CH_SYNC_CTRL_1;
		break;
	case 2:
		sync_ctrl0 = HW_Q4_Q5_CH_SYNC_CTRL_0;
		sync_ctrl1 = HW_Q4_Q5_CH_SYNC_CTRL_1;
		break;
	case 3:
		sync_ctrl0 = HW_Q6_Q7_CH_SYNC_CTRL_0;
		sync_ctrl1 = HW_Q6_Q7_CH_SYNC_CTRL_1;
		break;
	case 4:
		sync_ctrl0 = HW_Q8_CH_SYNC_CTRL_0;
		sync_ctrl1 = HW_Q8_CH_SYNC_CTRL_1;
		break;
	case 5:
		sync_ctrl0 = HW_Q9_CH_SYNC_CTRL_0;
		sync_ctrl1 = HW_Q9_CH_SYNC_CTRL_1;
		break;
	case 6:
		sync_ctrl0 = HW_Q10_CH_SYNC_CTRL_0;
		sync_ctrl1 = HW_Q10_CH_SYNC_CTRL_1;
		break;
	case 7:
		sync_ctrl0 = HW_Q11_CH_SYNC_CTRL_0;
		sync_ctrl1 = HW_Q11_CH_SYNC_CTRL_1;
		break;
	default:
		return -EINVAL;
	}

	val = SYNCTRL1_MASTER_SYNC_RST;

	/* Place master sync in reset */
	err = idtcm_write(idtcm, 0, sync_ctrl1, &val, sizeof(val));
	if (err)
		return err;

	err = idtcm_write(idtcm, 0, sync_ctrl0, &sync_src, sizeof(sync_src));
	if (err)
		return err;

	/* Set sync trigger mask */
	val |= SYNCTRL1_FBDIV_FRAME_SYNC_TRIG | SYNCTRL1_FBDIV_SYNC_TRIG;

	if (qn)
		val |= SYNCTRL1_Q0_DIV_SYNC_TRIG;

	if (qn_plus_1)
		val |= SYNCTRL1_Q1_DIV_SYNC_TRIG;

	err = idtcm_write(idtcm, 0, sync_ctrl1, &val, sizeof(val));
	if (err)
		return err;

520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563
	/* PLL5 can have OUT8 as second additional output. */
	if ((pll == 5) && (qn_plus_1 != 0)) {
		err = idtcm_read(idtcm, 0, HW_Q8_CTRL_SPARE,
				 &temp, sizeof(temp));
		if (err)
			return err;

		temp &= ~(Q9_TO_Q8_SYNC_TRIG);

		err = idtcm_write(idtcm, 0, HW_Q8_CTRL_SPARE,
				  &temp, sizeof(temp));
		if (err)
			return err;

		temp |= Q9_TO_Q8_SYNC_TRIG;

		err = idtcm_write(idtcm, 0, HW_Q8_CTRL_SPARE,
				  &temp, sizeof(temp));
		if (err)
			return err;
	}

	/* PLL6 can have OUT11 as second additional output. */
	if ((pll == 6) && (qn_plus_1 != 0)) {
		err = idtcm_read(idtcm, 0, HW_Q11_CTRL_SPARE,
				 &temp, sizeof(temp));
		if (err)
			return err;

		temp &= ~(Q10_TO_Q11_SYNC_TRIG);

		err = idtcm_write(idtcm, 0, HW_Q11_CTRL_SPARE,
				  &temp, sizeof(temp));
		if (err)
			return err;

		temp |= Q10_TO_Q11_SYNC_TRIG;

		err = idtcm_write(idtcm, 0, HW_Q11_CTRL_SPARE,
				  &temp, sizeof(temp));
		if (err)
			return err;
	}

564 565 566 567 568 569 570
	/* Place master sync out of reset */
	val &= ~(SYNCTRL1_MASTER_SYNC_RST);
	err = idtcm_write(idtcm, 0, sync_ctrl1, &val, sizeof(val));

	return err;
}

571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594
static int sync_source_dpll_tod_pps(u16 tod_addr, u8 *sync_src)
{
	int err = 0;

	switch (tod_addr) {
	case TOD_0:
		*sync_src = SYNC_SOURCE_DPLL0_TOD_PPS;
		break;
	case TOD_1:
		*sync_src = SYNC_SOURCE_DPLL1_TOD_PPS;
		break;
	case TOD_2:
		*sync_src = SYNC_SOURCE_DPLL2_TOD_PPS;
		break;
	case TOD_3:
		*sync_src = SYNC_SOURCE_DPLL3_TOD_PPS;
		break;
	default:
		err = -EINVAL;
	}

	return err;
}

595 596 597 598 599 600 601 602 603
static int idtcm_sync_pps_output(struct idtcm_channel *channel)
{
	struct idtcm *idtcm = channel->idtcm;

	u8 pll;
	u8 sync_src;
	u8 qn;
	u8 qn_plus_1;
	int err = 0;
604 605 606
	u8 out8_mux = 0;
	u8 out11_mux = 0;
	u8 temp;
607 608 609

	u16 output_mask = channel->output_mask;

610 611 612
	err = sync_source_dpll_tod_pps(channel->tod_n, &sync_src);
	if (err)
		return err;
613

614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630
	err = idtcm_read(idtcm, 0, HW_Q8_CTRL_SPARE,
			 &temp, sizeof(temp));
	if (err)
		return err;

	if ((temp & Q9_TO_Q8_FANOUT_AND_CLOCK_SYNC_ENABLE_MASK) ==
	    Q9_TO_Q8_FANOUT_AND_CLOCK_SYNC_ENABLE_MASK)
		out8_mux = 1;

	err = idtcm_read(idtcm, 0, HW_Q11_CTRL_SPARE,
			 &temp, sizeof(temp));
	if (err)
		return err;

	if ((temp & Q10_TO_Q11_FANOUT_AND_CLOCK_SYNC_ENABLE_MASK) ==
	    Q10_TO_Q11_FANOUT_AND_CLOCK_SYNC_ENABLE_MASK)
		out11_mux = 1;
631

632 633 634
	for (pll = 0; pll < 8; pll++) {
		qn = 0;
		qn_plus_1 = 0;
635 636 637

		if (pll < 4) {
			/* First 4 pll has 2 outputs */
638 639
			qn = output_mask & 0x1;
			output_mask = output_mask >> 1;
640 641
			qn_plus_1 = output_mask & 0x1;
			output_mask = output_mask >> 1;
642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665
		} else if (pll == 4) {
			if (out8_mux == 0) {
				qn = output_mask & 0x1;
				output_mask = output_mask >> 1;
			}
		} else if (pll == 5) {
			if (out8_mux) {
				qn_plus_1 = output_mask & 0x1;
				output_mask = output_mask >> 1;
			}
			qn = output_mask & 0x1;
			output_mask = output_mask >> 1;
		} else if (pll == 6) {
			qn = output_mask & 0x1;
			output_mask = output_mask >> 1;
			if (out11_mux) {
				qn_plus_1 = output_mask & 0x1;
				output_mask = output_mask >> 1;
			}
		} else if (pll == 7) {
			if (out11_mux == 0) {
				qn = output_mask & 0x1;
				output_mask = output_mask >> 1;
			}
666 667 668 669 670 671 672 673 674 675 676 677 678
		}

		if ((qn != 0) || (qn_plus_1 != 0))
			err = _sync_pll_output(idtcm, pll, sync_src, qn,
					       qn_plus_1);

		if (err)
			return err;
	}

	return err;
}

679
static int _idtcm_set_dpll_hw_tod(struct idtcm_channel *channel,
680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729
			       struct timespec64 const *ts,
			       enum hw_tod_write_trig_sel wr_trig)
{
	struct idtcm *idtcm = channel->idtcm;

	u8 buf[TOD_BYTE_COUNT];
	u8 cmd;
	int err;
	struct timespec64 local_ts = *ts;
	s64 total_overhead_ns;

	/* Configure HW TOD write trigger. */
	err = idtcm_read(idtcm, channel->hw_dpll_n, HW_DPLL_TOD_CTRL_1,
			 &cmd, sizeof(cmd));

	if (err)
		return err;

	cmd &= ~(0x0f);
	cmd |= wr_trig | 0x08;

	err = idtcm_write(idtcm, channel->hw_dpll_n, HW_DPLL_TOD_CTRL_1,
			  &cmd, sizeof(cmd));

	if (err)
		return err;

	if (wr_trig  != HW_TOD_WR_TRIG_SEL_MSB) {

		err = timespec_to_char_array(&local_ts, buf, sizeof(buf));

		if (err)
			return err;

		err = idtcm_write(idtcm, channel->hw_dpll_n,
				  HW_DPLL_TOD_OVR__0, buf, sizeof(buf));

		if (err)
			return err;
	}

	/* ARM HW TOD write trigger. */
	cmd &= ~(0x08);

	err = idtcm_write(idtcm, channel->hw_dpll_n, HW_DPLL_TOD_CTRL_1,
			  &cmd, sizeof(cmd));

	if (wr_trig == HW_TOD_WR_TRIG_SEL_MSB) {

		if (idtcm->calculate_overhead_flag) {
730
			/* Assumption: I2C @ 400KHz */
731 732 733
			ktime_t diff = ktime_sub(ktime_get_raw(),
						 idtcm->start_time);
			total_overhead_ns =  ktime_to_ns(diff)
734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753
					     + idtcm->tod_write_overhead_ns
					     + SETTIME_CORRECTION;

			timespec64_add_ns(&local_ts, total_overhead_ns);

			idtcm->calculate_overhead_flag = 0;
		}

		err = timespec_to_char_array(&local_ts, buf, sizeof(buf));

		if (err)
			return err;

		err = idtcm_write(idtcm, channel->hw_dpll_n,
				  HW_DPLL_TOD_OVR__0, buf, sizeof(buf));
	}

	return err;
}

754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802
static int _idtcm_set_dpll_scsr_tod(struct idtcm_channel *channel,
				    struct timespec64 const *ts,
				    enum scsr_tod_write_trig_sel wr_trig,
				    enum scsr_tod_write_type_sel wr_type)
{
	struct idtcm *idtcm = channel->idtcm;
	unsigned char buf[TOD_BYTE_COUNT], cmd;
	struct timespec64 local_ts = *ts;
	int err, count = 0;

	timespec64_add_ns(&local_ts, SETTIME_CORRECTION);

	err = timespec_to_char_array(&local_ts, buf, sizeof(buf));

	if (err)
		return err;

	err = idtcm_write(idtcm, channel->tod_write, TOD_WRITE,
			  buf, sizeof(buf));
	if (err)
		return err;

	/* Trigger the write operation. */
	err = idtcm_read(idtcm, channel->tod_write, TOD_WRITE_CMD,
			 &cmd, sizeof(cmd));
	if (err)
		return err;

	cmd &= ~(TOD_WRITE_SELECTION_MASK << TOD_WRITE_SELECTION_SHIFT);
	cmd &= ~(TOD_WRITE_TYPE_MASK << TOD_WRITE_TYPE_SHIFT);
	cmd |= (wr_trig << TOD_WRITE_SELECTION_SHIFT);
	cmd |= (wr_type << TOD_WRITE_TYPE_SHIFT);

	err = idtcm_write(idtcm, channel->tod_write, TOD_WRITE_CMD,
			   &cmd, sizeof(cmd));
	if (err)
		return err;

	/* Wait for the operation to complete. */
	while (1) {
		/* pps trigger takes up to 1 sec to complete */
		if (wr_trig == SCSR_TOD_WR_TRIG_SEL_TODPPS)
			msleep(50);

		err = idtcm_read(idtcm, channel->tod_write, TOD_WRITE_CMD,
				 &cmd, sizeof(cmd));
		if (err)
			return err;

803
		if ((cmd & TOD_WRITE_SELECTION_MASK) == 0)
804 805 806 807
			break;

		if (++count > 20) {
			dev_err(&idtcm->client->dev,
808
				"Timed out waiting for the write counter");
809 810 811 812 813 814 815
			return -EIO;
		}
	}

	return 0;
}

816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863
static int get_output_base_addr(u8 outn)
{
	int base;

	switch (outn) {
	case 0:
		base = OUTPUT_0;
		break;
	case 1:
		base = OUTPUT_1;
		break;
	case 2:
		base = OUTPUT_2;
		break;
	case 3:
		base = OUTPUT_3;
		break;
	case 4:
		base = OUTPUT_4;
		break;
	case 5:
		base = OUTPUT_5;
		break;
	case 6:
		base = OUTPUT_6;
		break;
	case 7:
		base = OUTPUT_7;
		break;
	case 8:
		base = OUTPUT_8;
		break;
	case 9:
		base = OUTPUT_9;
		break;
	case 10:
		base = OUTPUT_10;
		break;
	case 11:
		base = OUTPUT_11;
		break;
	default:
		base = -EINVAL;
	}

	return base;
}

864 865
static int _idtcm_settime_deprecated(struct idtcm_channel *channel,
				     struct timespec64 const *ts)
866 867 868 869
{
	struct idtcm *idtcm = channel->idtcm;
	int err;

870
	err = _idtcm_set_dpll_hw_tod(channel, ts, HW_TOD_WR_TRIG_SEL_MSB);
871

872 873
	if (err) {
		dev_err(&idtcm->client->dev,
874
			"%s: Set HW ToD failed", __func__);
875
		return err;
876
	}
877

878 879
	return idtcm_sync_pps_output(channel);
}
880

881 882 883
static int _idtcm_settime(struct idtcm_channel *channel,
			  struct timespec64 const *ts,
			  enum scsr_tod_write_type_sel wr_type)
884 885 886 887
{
	return _idtcm_set_dpll_scsr_tod(channel, ts,
					SCSR_TOD_WR_TRIG_SEL_IMMEDIATE,
					wr_type);
888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977
}

static int idtcm_set_phase_pull_in_offset(struct idtcm_channel *channel,
					  s32 offset_ns)
{
	int err;
	int i;
	struct idtcm *idtcm = channel->idtcm;

	u8 buf[4];

	for (i = 0; i < 4; i++) {
		buf[i] = 0xff & (offset_ns);
		offset_ns >>= 8;
	}

	err = idtcm_write(idtcm, channel->dpll_phase_pull_in, PULL_IN_OFFSET,
			  buf, sizeof(buf));

	return err;
}

static int idtcm_set_phase_pull_in_slope_limit(struct idtcm_channel *channel,
					       u32 max_ffo_ppb)
{
	int err;
	u8 i;
	struct idtcm *idtcm = channel->idtcm;

	u8 buf[3];

	if (max_ffo_ppb & 0xff000000)
		max_ffo_ppb = 0;

	for (i = 0; i < 3; i++) {
		buf[i] = 0xff & (max_ffo_ppb);
		max_ffo_ppb >>= 8;
	}

	err = idtcm_write(idtcm, channel->dpll_phase_pull_in,
			  PULL_IN_SLOPE_LIMIT, buf, sizeof(buf));

	return err;
}

static int idtcm_start_phase_pull_in(struct idtcm_channel *channel)
{
	int err;
	struct idtcm *idtcm = channel->idtcm;

	u8 buf;

	err = idtcm_read(idtcm, channel->dpll_phase_pull_in, PULL_IN_CTRL,
			 &buf, sizeof(buf));

	if (err)
		return err;

	if (buf == 0) {
		buf = 0x01;
		err = idtcm_write(idtcm, channel->dpll_phase_pull_in,
				  PULL_IN_CTRL, &buf, sizeof(buf));
	} else {
		err = -EBUSY;
	}

	return err;
}

static int idtcm_do_phase_pull_in(struct idtcm_channel *channel,
				  s32 offset_ns,
				  u32 max_ffo_ppb)
{
	int err;

	err = idtcm_set_phase_pull_in_offset(channel, -offset_ns);

	if (err)
		return err;

	err = idtcm_set_phase_pull_in_slope_limit(channel, max_ffo_ppb);

	if (err)
		return err;

	err = idtcm_start_phase_pull_in(channel);

	return err;
}

978 979 980 981 982 983 984 985 986 987
static int set_tod_write_overhead(struct idtcm_channel *channel)
{
	struct idtcm *idtcm = channel->idtcm;
	s64 current_ns = 0;
	s64 lowest_ns = 0;
	int err;
	u8 i;

	ktime_t start;
	ktime_t stop;
988
	ktime_t diff;
989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007

	char buf[TOD_BYTE_COUNT] = {0};

	/* Set page offset */
	idtcm_write(idtcm, channel->hw_dpll_n, HW_DPLL_TOD_OVR__0,
		    buf, sizeof(buf));

	for (i = 0; i < TOD_WRITE_OVERHEAD_COUNT_MAX; i++) {

		start = ktime_get_raw();

		err = idtcm_write(idtcm, channel->hw_dpll_n,
				  HW_DPLL_TOD_OVR__0, buf, sizeof(buf));

		if (err)
			return err;

		stop = ktime_get_raw();

1008 1009 1010
		diff = ktime_sub(stop, start);

		current_ns = ktime_to_ns(diff);
1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024

		if (i == 0) {
			lowest_ns = current_ns;
		} else {
			if (current_ns < lowest_ns)
				lowest_ns = current_ns;
		}
	}

	idtcm->tod_write_overhead_ns = lowest_ns;

	return err;
}

1025
static int _idtcm_adjtime_deprecated(struct idtcm_channel *channel, s64 delta)
1026 1027 1028 1029 1030 1031
{
	int err;
	struct idtcm *idtcm = channel->idtcm;
	struct timespec64 ts;
	s64 now;

1032
	if (abs(delta) < PHASE_PULL_IN_THRESHOLD_NS_DEPRECATED) {
1033 1034 1035 1036
		err = idtcm_do_phase_pull_in(channel, delta, 0);
	} else {
		idtcm->calculate_overhead_flag = 1;

1037 1038 1039 1040 1041
		err = set_tod_write_overhead(channel);

		if (err)
			return err;

1042 1043 1044 1045 1046 1047 1048 1049 1050 1051
		err = _idtcm_gettime(channel, &ts);

		if (err)
			return err;

		now = timespec64_to_ns(&ts);
		now += delta;

		ts = ns_to_timespec64(now);

1052
		err = _idtcm_settime_deprecated(channel, &ts);
1053 1054 1055 1056 1057 1058 1059 1060
	}

	return err;
}

static int idtcm_state_machine_reset(struct idtcm *idtcm)
{
	u8 byte = SM_RESET_CMD;
1061 1062 1063 1064 1065
	u32 status = 0;
	int err;
	u8 i;

	clear_boot_status(idtcm);
1066 1067 1068

	err = idtcm_write(idtcm, RESET_CTRL, SM_RESET, &byte, sizeof(byte));

1069 1070 1071 1072 1073 1074 1075
	if (!err) {
		for (i = 0; i < 30; i++) {
			msleep_interruptible(100);
			read_boot_status(idtcm, &status);

			if (status == 0xA0) {
				dev_dbg(&idtcm->client->dev,
1076
					"SM_RESET completed in %d ms", i * 100);
1077 1078 1079 1080 1081
				break;
			}
		}

		if (!status)
1082 1083
			dev_err(&idtcm->client->dev,
				"Timed out waiting for CM_RESET to complete");
1084
	}
1085 1086 1087 1088 1089 1090

	return err;
}

static int idtcm_read_hw_rev_id(struct idtcm *idtcm, u8 *hw_rev_id)
{
1091
	return idtcm_read(idtcm, HW_REVISION, REV_ID, hw_rev_id, sizeof(u8));
1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131
}

static int idtcm_read_product_id(struct idtcm *idtcm, u16 *product_id)
{
	int err;
	u8 buf[2] = {0};

	err = idtcm_read(idtcm, GENERAL_STATUS, PRODUCT_ID, buf, sizeof(buf));

	*product_id = (buf[1] << 8) | buf[0];

	return err;
}

static int idtcm_read_major_release(struct idtcm *idtcm, u8 *major)
{
	int err;
	u8 buf = 0;

	err = idtcm_read(idtcm, GENERAL_STATUS, MAJ_REL, &buf, sizeof(buf));

	*major = buf >> 1;

	return err;
}

static int idtcm_read_minor_release(struct idtcm *idtcm, u8 *minor)
{
	return idtcm_read(idtcm, GENERAL_STATUS, MIN_REL, minor, sizeof(u8));
}

static int idtcm_read_hotfix_release(struct idtcm *idtcm, u8 *hotfix)
{
	return idtcm_read(idtcm,
			  GENERAL_STATUS,
			  HOTFIX_REL,
			  hotfix,
			  sizeof(u8));
}

1132 1133
static int idtcm_read_otp_scsr_config_select(struct idtcm *idtcm,
					     u8 *config_select)
1134
{
1135 1136
	return idtcm_read(idtcm, GENERAL_STATUS, OTP_SCSR_CONFIG_SELECT,
			  config_select, sizeof(u8));
1137 1138 1139 1140 1141 1142 1143
}

static int set_pll_output_mask(struct idtcm *idtcm, u16 addr, u8 val)
{
	int err = 0;

	switch (addr) {
1144
	case TOD0_OUT_ALIGN_MASK_ADDR:
1145 1146
		SET_U16_LSB(idtcm->channel[0].output_mask, val);
		break;
1147
	case TOD0_OUT_ALIGN_MASK_ADDR + 1:
1148 1149
		SET_U16_MSB(idtcm->channel[0].output_mask, val);
		break;
1150
	case TOD1_OUT_ALIGN_MASK_ADDR:
1151 1152
		SET_U16_LSB(idtcm->channel[1].output_mask, val);
		break;
1153
	case TOD1_OUT_ALIGN_MASK_ADDR + 1:
1154 1155
		SET_U16_MSB(idtcm->channel[1].output_mask, val);
		break;
1156
	case TOD2_OUT_ALIGN_MASK_ADDR:
1157 1158
		SET_U16_LSB(idtcm->channel[2].output_mask, val);
		break;
1159
	case TOD2_OUT_ALIGN_MASK_ADDR + 1:
1160 1161
		SET_U16_MSB(idtcm->channel[2].output_mask, val);
		break;
1162
	case TOD3_OUT_ALIGN_MASK_ADDR:
1163 1164
		SET_U16_LSB(idtcm->channel[3].output_mask, val);
		break;
1165
	case TOD3_OUT_ALIGN_MASK_ADDR + 1:
1166 1167 1168
		SET_U16_MSB(idtcm->channel[3].output_mask, val);
		break;
	default:
1169
		err = -EFAULT; /* Bad address */;
1170 1171 1172 1173 1174 1175
		break;
	}

	return err;
}

1176 1177 1178
static int set_tod_ptp_pll(struct idtcm *idtcm, u8 index, u8 pll)
{
	if (index >= MAX_TOD) {
1179
		dev_err(&idtcm->client->dev, "ToD%d not supported", index);
1180 1181 1182 1183
		return -EINVAL;
	}

	if (pll >= MAX_PLL) {
1184
		dev_err(&idtcm->client->dev, "Pll%d not supported", pll);
1185 1186 1187 1188 1189 1190 1191 1192
		return -EINVAL;
	}

	idtcm->channel[index].pll = pll;

	return 0;
}

1193 1194 1195 1196 1197 1198
static int check_and_set_masks(struct idtcm *idtcm,
			       u16 regaddr,
			       u8 val)
{
	int err = 0;

1199 1200 1201
	switch (regaddr) {
	case TOD_MASK_ADDR:
		if ((val & 0xf0) || !(val & 0x0f)) {
1202
			dev_err(&idtcm->client->dev, "Invalid TOD mask 0x%02x", val);
1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222
			err = -EINVAL;
		} else {
			idtcm->tod_mask = val;
		}
		break;
	case TOD0_PTP_PLL_ADDR:
		err = set_tod_ptp_pll(idtcm, 0, val);
		break;
	case TOD1_PTP_PLL_ADDR:
		err = set_tod_ptp_pll(idtcm, 1, val);
		break;
	case TOD2_PTP_PLL_ADDR:
		err = set_tod_ptp_pll(idtcm, 2, val);
		break;
	case TOD3_PTP_PLL_ADDR:
		err = set_tod_ptp_pll(idtcm, 3, val);
		break;
	default:
		err = set_pll_output_mask(idtcm, regaddr, val);
		break;
1223 1224 1225 1226 1227
	}

	return err;
}

1228
static void display_pll_and_masks(struct idtcm *idtcm)
1229 1230 1231 1232
{
	u8 i;
	u8 mask;

1233
	dev_dbg(&idtcm->client->dev, "tod_mask = 0x%02x", idtcm->tod_mask);
1234

1235
	for (i = 0; i < MAX_TOD; i++) {
1236 1237
		mask = 1 << i;

1238
		if (mask & idtcm->tod_mask)
1239
			dev_dbg(&idtcm->client->dev,
1240
				"TOD%d pll = %d    output_mask = 0x%04x",
1241 1242
				i, idtcm->channel[i].pll,
				idtcm->channel[i].output_mask);
1243 1244 1245 1246 1247 1248
	}
}

static int idtcm_load_firmware(struct idtcm *idtcm,
			       struct device *dev)
{
1249
	char fname[128] = FW_FILENAME;
1250 1251 1252 1253 1254 1255 1256 1257
	const struct firmware *fw;
	struct idtcm_fwrc *rec;
	u32 regaddr;
	int err;
	s32 len;
	u8 val;
	u8 loaddr;

1258 1259
	if (firmware) /* module parameter */
		snprintf(fname, sizeof(fname), "%s", firmware);
1260

1261
	dev_dbg(&idtcm->client->dev, "requesting firmware '%s'", fname);
1262

1263 1264 1265
	err = request_firmware(&fw, fname, dev);
	if (err) {
		dev_err(&idtcm->client->dev,
1266
			"Failed at line %d in %s!", __LINE__, __func__);
1267
		return err;
1268
	}
1269

1270
	dev_dbg(&idtcm->client->dev, "firmware size %zu bytes", fw->size);
1271 1272 1273

	rec = (struct idtcm_fwrc *) fw->data;

1274
	if (contains_full_configuration(fw))
1275 1276 1277 1278 1279 1280
		idtcm_state_machine_reset(idtcm);

	for (len = fw->size; len > 0; len -= sizeof(*rec)) {

		if (rec->reserved) {
			dev_err(&idtcm->client->dev,
1281
				"bad firmware, reserved field non-zero");
1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294
			err = -EINVAL;
		} else {
			regaddr = rec->hiaddr << 8;
			regaddr |= rec->loaddr;

			val = rec->value;
			loaddr = rec->loaddr;

			rec++;

			err = check_and_set_masks(idtcm, regaddr, val);
		}

1295 1296 1297
		if (err != -EINVAL) {
			err = 0;

1298 1299 1300 1301 1302 1303 1304
			/* Top (status registers) and bottom are read-only */
			if ((regaddr < GPIO_USER_CONTROL)
			    || (regaddr >= SCRATCH))
				continue;

			/* Page size 128, last 4 bytes of page skipped */
			if (((loaddr > 0x7b) && (loaddr <= 0x7f))
1305
			     || loaddr > 0xfb)
1306 1307 1308 1309 1310 1311 1312 1313 1314
				continue;

			err = idtcm_write(idtcm, regaddr, 0, &val, sizeof(val));
		}

		if (err)
			goto out;
	}

1315
	display_pll_and_masks(idtcm);
1316 1317 1318 1319 1320 1321

out:
	release_firmware(fw);
	return err;
}

1322 1323
static int idtcm_output_enable(struct idtcm_channel *channel,
			       bool enable, unsigned int outn)
1324 1325
{
	struct idtcm *idtcm = channel->idtcm;
1326
	int base;
1327
	int err;
1328
	u8 val;
1329

1330 1331 1332 1333 1334 1335 1336 1337 1338
	base = get_output_base_addr(outn);

	if (!(base > 0)) {
		dev_err(&idtcm->client->dev,
			"%s - Unsupported out%d", __func__, outn);
		return base;
	}

	err = idtcm_read(idtcm, (u16)base, OUT_CTRL_1, &val, sizeof(val));
1339 1340 1341 1342 1343 1344 1345 1346 1347

	if (err)
		return err;

	if (enable)
		val |= SQUELCH_DISABLE;
	else
		val &= ~SQUELCH_DISABLE;

1348
	return idtcm_write(idtcm, (u16)base, OUT_CTRL_1, &val, sizeof(val));
1349
}
1350

1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373
static int idtcm_output_mask_enable(struct idtcm_channel *channel,
				    bool enable)
{
	u16 mask;
	int err;
	u8 outn;

	mask = channel->output_mask;
	outn = 0;

	while (mask) {

		if (mask & 0x1) {

			err = idtcm_output_enable(channel, enable, outn);

			if (err)
				return err;
		}

		mask >>= 0x1;
		outn++;
	}
1374 1375 1376 1377

	return 0;
}

1378 1379 1380 1381
static int idtcm_perout_enable(struct idtcm_channel *channel,
			       bool enable,
			       struct ptp_perout_request *perout)
{
1382
	struct idtcm *idtcm = channel->idtcm;
1383
	unsigned int flags = perout->flags;
1384 1385
	struct timespec64 ts = {0, 0};
	int err;
1386 1387

	if (flags == PEROUT_ENABLE_OUTPUT_MASK)
1388 1389 1390 1391 1392 1393 1394 1395
		err = idtcm_output_mask_enable(channel, enable);
	else
		err = idtcm_output_enable(channel, enable, perout->index);

	if (err) {
		dev_err(&idtcm->client->dev, "Unable to set output enable");
		return err;
	}
1396

1397 1398
	/* Align output to internal 1 PPS */
	return _idtcm_settime(channel, &ts, SCSR_TOD_WR_TYPE_SEL_DELTA_PLUS);
1399 1400
}

1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417
static int idtcm_get_pll_mode(struct idtcm_channel *channel,
			      enum pll_mode *pll_mode)
{
	struct idtcm *idtcm = channel->idtcm;
	int err;
	u8 dpll_mode;

	err = idtcm_read(idtcm, channel->dpll_n, DPLL_MODE,
			 &dpll_mode, sizeof(dpll_mode));
	if (err)
		return err;

	*pll_mode = (dpll_mode >> PLL_MODE_SHIFT) & PLL_MODE_MASK;

	return 0;
}

1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445
static int idtcm_set_pll_mode(struct idtcm_channel *channel,
			      enum pll_mode pll_mode)
{
	struct idtcm *idtcm = channel->idtcm;
	int err;
	u8 dpll_mode;

	err = idtcm_read(idtcm, channel->dpll_n, DPLL_MODE,
			 &dpll_mode, sizeof(dpll_mode));
	if (err)
		return err;

	dpll_mode &= ~(PLL_MODE_MASK << PLL_MODE_SHIFT);

	dpll_mode |= (pll_mode << PLL_MODE_SHIFT);

	channel->pll_mode = pll_mode;

	err = idtcm_write(idtcm, channel->dpll_n, DPLL_MODE,
			  &dpll_mode, sizeof(dpll_mode));
	if (err)
		return err;

	return 0;
}

/* PTP Hardware Clock interface */

1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482
/**
 * @brief Maximum absolute value for write phase offset in picoseconds
 *
 * Destination signed register is 32-bit register in resolution of 50ps
 *
 * 0x7fffffff * 50 =  2147483647 * 50 = 107374182350
 */
static int _idtcm_adjphase(struct idtcm_channel *channel, s32 delta_ns)
{
	struct idtcm *idtcm = channel->idtcm;

	int err;
	u8 i;
	u8 buf[4] = {0};
	s32 phase_50ps;
	s64 offset_ps;

	if (channel->pll_mode != PLL_MODE_WRITE_PHASE) {

		err = idtcm_set_pll_mode(channel, PLL_MODE_WRITE_PHASE);

		if (err)
			return err;
	}

	offset_ps = (s64)delta_ns * 1000;

	/*
	 * Check for 32-bit signed max * 50:
	 *
	 * 0x7fffffff * 50 =  2147483647 * 50 = 107374182350
	 */
	if (offset_ps > MAX_ABS_WRITE_PHASE_PICOSECONDS)
		offset_ps = MAX_ABS_WRITE_PHASE_PICOSECONDS;
	else if (offset_ps < -MAX_ABS_WRITE_PHASE_PICOSECONDS)
		offset_ps = -MAX_ABS_WRITE_PHASE_PICOSECONDS;

1483
	phase_50ps = div_s64(offset_ps, 50);
1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495

	for (i = 0; i < 4; i++) {
		buf[i] = phase_50ps & 0xff;
		phase_50ps >>= 8;
	}

	err = idtcm_write(idtcm, channel->dpll_phase, DPLL_WR_PHASE,
			  buf, sizeof(buf));

	return err;
}

1496
static int _idtcm_adjfine(struct idtcm_channel *channel, long scaled_ppm)
1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524
{
	struct idtcm *idtcm = channel->idtcm;
	u8 i;
	int err;
	u8 buf[6] = {0};
	s64 fcw;

	if (channel->pll_mode  != PLL_MODE_WRITE_FREQUENCY) {
		err = idtcm_set_pll_mode(channel, PLL_MODE_WRITE_FREQUENCY);
		if (err)
			return err;
	}

	/*
	 * Frequency Control Word unit is: 1.11 * 10^-10 ppm
	 *
	 * adjfreq:
	 *       ppb * 10^9
	 * FCW = ----------
	 *          111
	 *
	 * adjfine:
	 *       ppm_16 * 5^12
	 * FCW = -------------
	 *         111 * 2^4
	 */

	/* 2 ^ -53 = 1.1102230246251565404236316680908e-16 */
1525
	fcw = scaled_ppm * 244140625ULL;
1526

1527
	fcw = div_s64(fcw, 1776);
1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550

	for (i = 0; i < 6; i++) {
		buf[i] = fcw & 0xff;
		fcw >>= 8;
	}

	err = idtcm_write(idtcm, channel->dpll_freq, DPLL_WR_FREQ,
			  buf, sizeof(buf));

	return err;
}

static int idtcm_gettime(struct ptp_clock_info *ptp, struct timespec64 *ts)
{
	struct idtcm_channel *channel =
		container_of(ptp, struct idtcm_channel, caps);
	struct idtcm *idtcm = channel->idtcm;
	int err;

	mutex_lock(&idtcm->reg_lock);

	err = _idtcm_gettime(channel, ts);

1551
	if (err)
1552 1553
		dev_err(&idtcm->client->dev, "Failed at line %d in %s!",
			__LINE__, __func__);
1554

1555 1556 1557 1558 1559
	mutex_unlock(&idtcm->reg_lock);

	return err;
}

1560 1561
static int idtcm_settime_deprecated(struct ptp_clock_info *ptp,
				    const struct timespec64 *ts)
1562 1563 1564 1565 1566 1567 1568 1569
{
	struct idtcm_channel *channel =
		container_of(ptp, struct idtcm_channel, caps);
	struct idtcm *idtcm = channel->idtcm;
	int err;

	mutex_lock(&idtcm->reg_lock);

1570
	err = _idtcm_settime_deprecated(channel, ts);
1571

1572 1573
	if (err)
		dev_err(&idtcm->client->dev,
1574
			"Failed at line %d in %s!", __LINE__, __func__);
1575

1576 1577 1578 1579 1580
	mutex_unlock(&idtcm->reg_lock);

	return err;
}

1581
static int idtcm_settime(struct ptp_clock_info *ptp,
1582
			 const struct timespec64 *ts)
1583 1584 1585 1586 1587 1588 1589 1590
{
	struct idtcm_channel *channel =
		container_of(ptp, struct idtcm_channel, caps);
	struct idtcm *idtcm = channel->idtcm;
	int err;

	mutex_lock(&idtcm->reg_lock);

1591
	err = _idtcm_settime(channel, ts, SCSR_TOD_WR_TYPE_SEL_ABSOLUTE);
1592 1593 1594

	if (err)
		dev_err(&idtcm->client->dev,
1595
			"Failed at line %d in %s!", __LINE__, __func__);
1596 1597 1598 1599 1600 1601

	mutex_unlock(&idtcm->reg_lock);

	return err;
}

1602
static int idtcm_adjtime_deprecated(struct ptp_clock_info *ptp, s64 delta)
1603 1604 1605 1606 1607 1608 1609 1610
{
	struct idtcm_channel *channel =
		container_of(ptp, struct idtcm_channel, caps);
	struct idtcm *idtcm = channel->idtcm;
	int err;

	mutex_lock(&idtcm->reg_lock);

1611
	err = _idtcm_adjtime_deprecated(channel, delta);
1612 1613 1614

	if (err)
		dev_err(&idtcm->client->dev,
1615
			"Failed at line %d in %s!", __LINE__, __func__);
1616 1617 1618 1619 1620 1621

	mutex_unlock(&idtcm->reg_lock);

	return err;
}

1622
static int idtcm_adjtime(struct ptp_clock_info *ptp, s64 delta)
1623 1624 1625 1626 1627 1628 1629 1630
{
	struct idtcm_channel *channel =
		container_of(ptp, struct idtcm_channel, caps);
	struct idtcm *idtcm = channel->idtcm;
	struct timespec64 ts;
	enum scsr_tod_write_type_sel type;
	int err;

1631
	if (abs(delta) < PHASE_PULL_IN_THRESHOLD_NS) {
1632 1633 1634
		err = idtcm_do_phase_pull_in(channel, delta, 0);
		if (err)
			dev_err(&idtcm->client->dev,
1635
				"Failed at line %d in %s!", __LINE__, __func__);
1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648
		return err;
	}

	if (delta >= 0) {
		ts = ns_to_timespec64(delta);
		type = SCSR_TOD_WR_TYPE_SEL_DELTA_PLUS;
	} else {
		ts = ns_to_timespec64(-delta);
		type = SCSR_TOD_WR_TYPE_SEL_DELTA_MINUS;
	}

	mutex_lock(&idtcm->reg_lock);

1649
	err = _idtcm_settime(channel, &ts, type);
1650 1651 1652

	if (err)
		dev_err(&idtcm->client->dev,
1653
			"Failed at line %d in %s!", __LINE__, __func__);
1654 1655 1656 1657 1658 1659 1660

	mutex_unlock(&idtcm->reg_lock);

	return err;
}

static int idtcm_adjphase(struct ptp_clock_info *ptp, s32 delta)
1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672
{
	struct idtcm_channel *channel =
		container_of(ptp, struct idtcm_channel, caps);

	struct idtcm *idtcm = channel->idtcm;

	int err;

	mutex_lock(&idtcm->reg_lock);

	err = _idtcm_adjphase(channel, delta);

1673 1674
	if (err)
		dev_err(&idtcm->client->dev,
1675
			"Failed at line %d in %s!", __LINE__, __func__);
1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696

	mutex_unlock(&idtcm->reg_lock);

	return err;
}

static int idtcm_adjfine(struct ptp_clock_info *ptp,  long scaled_ppm)
{
	struct idtcm_channel *channel =
		container_of(ptp, struct idtcm_channel, caps);

	struct idtcm *idtcm = channel->idtcm;

	int err;

	mutex_lock(&idtcm->reg_lock);

	err = _idtcm_adjfine(channel, scaled_ppm);

	if (err)
		dev_err(&idtcm->client->dev,
1697
			"Failed at line %d in %s!", __LINE__, __func__);
1698

1699 1700 1701 1702 1703
	mutex_unlock(&idtcm->reg_lock);

	return err;
}

1704 1705 1706
static int idtcm_enable(struct ptp_clock_info *ptp,
			struct ptp_clock_request *rq, int on)
{
1707 1708
	int err;

1709 1710 1711 1712 1713
	struct idtcm_channel *channel =
		container_of(ptp, struct idtcm_channel, caps);

	switch (rq->type) {
	case PTP_CLK_REQ_PEROUT:
1714 1715 1716 1717
		if (!on) {
			err = idtcm_perout_enable(channel, false, &rq->perout);
			if (err)
				dev_err(&channel->idtcm->client->dev,
1718 1719
					"Failed at line %d in %s!",
					__LINE__, __func__);
1720 1721
			return err;
		}
1722 1723 1724 1725 1726 1727

		/* Only accept a 1-PPS aligned to the second. */
		if (rq->perout.start.nsec || rq->perout.period.sec != 1 ||
		    rq->perout.period.nsec)
			return -ERANGE;

1728 1729 1730
		err = idtcm_perout_enable(channel, true, &rq->perout);
		if (err)
			dev_err(&channel->idtcm->client->dev,
1731
				"Failed at line %d in %s!", __LINE__, __func__);
1732
		return err;
1733 1734 1735 1736 1737 1738 1739
	default:
		break;
	}

	return -EOPNOTSUPP;
}

1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854
static int _enable_pll_tod_sync(struct idtcm *idtcm,
				u8 pll,
				u8 sync_src,
				u8 qn,
				u8 qn_plus_1)
{
	int err;
	u8 val;
	u16 dpll;
	u16 out0 = 0, out1 = 0;

	if ((qn == 0) && (qn_plus_1 == 0))
		return 0;

	switch (pll) {
	case 0:
		dpll = DPLL_0;
		if (qn)
			out0 = OUTPUT_0;
		if (qn_plus_1)
			out1 = OUTPUT_1;
		break;
	case 1:
		dpll = DPLL_1;
		if (qn)
			out0 = OUTPUT_2;
		if (qn_plus_1)
			out1 = OUTPUT_3;
		break;
	case 2:
		dpll = DPLL_2;
		if (qn)
			out0 = OUTPUT_4;
		if (qn_plus_1)
			out1 = OUTPUT_5;
		break;
	case 3:
		dpll = DPLL_3;
		if (qn)
			out0 = OUTPUT_6;
		if (qn_plus_1)
			out1 = OUTPUT_7;
		break;
	case 4:
		dpll = DPLL_4;
		if (qn)
			out0 = OUTPUT_8;
		break;
	case 5:
		dpll = DPLL_5;
		if (qn)
			out0 = OUTPUT_9;
		if (qn_plus_1)
			out1 = OUTPUT_8;
		break;
	case 6:
		dpll = DPLL_6;
		if (qn)
			out0 = OUTPUT_10;
		if (qn_plus_1)
			out1 = OUTPUT_11;
		break;
	case 7:
		dpll = DPLL_7;
		if (qn)
			out0 = OUTPUT_11;
		break;
	default:
		return -EINVAL;
	}

	/*
	 * Enable OUTPUT OUT_SYNC.
	 */
	if (out0) {
		err = idtcm_read(idtcm, out0, OUT_CTRL_1, &val, sizeof(val));

		if (err)
			return err;

		val &= ~OUT_SYNC_DISABLE;

		err = idtcm_write(idtcm, out0, OUT_CTRL_1, &val, sizeof(val));

		if (err)
			return err;
	}

	if (out1) {
		err = idtcm_read(idtcm, out1, OUT_CTRL_1, &val, sizeof(val));

		if (err)
			return err;

		val &= ~OUT_SYNC_DISABLE;

		err = idtcm_write(idtcm, out1, OUT_CTRL_1, &val, sizeof(val));

		if (err)
			return err;
	}

	/* enable dpll sync tod pps, must be set before dpll_mode */
	err = idtcm_read(idtcm, dpll, DPLL_TOD_SYNC_CFG, &val, sizeof(val));
	if (err)
		return err;

	val &= ~(TOD_SYNC_SOURCE_MASK << TOD_SYNC_SOURCE_SHIFT);
	val |= (sync_src << TOD_SYNC_SOURCE_SHIFT);
	val |= TOD_SYNC_EN;

	return idtcm_write(idtcm, dpll, DPLL_TOD_SYNC_CFG, &val, sizeof(val));
}

static int idtcm_enable_tod_sync(struct idtcm_channel *channel)
1855 1856
{
	struct idtcm *idtcm = channel->idtcm;
1857 1858 1859 1860 1861

	u8 pll;
	u8 sync_src;
	u8 qn;
	u8 qn_plus_1;
1862
	u8 cfg;
1863 1864 1865 1866 1867
	int err = 0;
	u16 output_mask = channel->output_mask;
	u8 out8_mux = 0;
	u8 out11_mux = 0;
	u8 temp;
1868

1869 1870 1871 1872
	/*
	 * set tod_out_sync_enable to 0.
	 */
	err = idtcm_read(idtcm, channel->tod_n, TOD_CFG, &cfg, sizeof(cfg));
1873 1874 1875
	if (err)
		return err;

1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970
	cfg &= ~TOD_OUT_SYNC_ENABLE;

	err = idtcm_write(idtcm, channel->tod_n, TOD_CFG, &cfg, sizeof(cfg));
	if (err)
		return err;

	switch (channel->tod_n) {
	case TOD_0:
		sync_src = 0;
		break;
	case TOD_1:
		sync_src = 1;
		break;
	case TOD_2:
		sync_src = 2;
		break;
	case TOD_3:
		sync_src = 3;
		break;
	default:
		return -EINVAL;
	}

	err = idtcm_read(idtcm, 0, HW_Q8_CTRL_SPARE,
			 &temp, sizeof(temp));
	if (err)
		return err;

	if ((temp & Q9_TO_Q8_FANOUT_AND_CLOCK_SYNC_ENABLE_MASK) ==
	    Q9_TO_Q8_FANOUT_AND_CLOCK_SYNC_ENABLE_MASK)
		out8_mux = 1;

	err = idtcm_read(idtcm, 0, HW_Q11_CTRL_SPARE,
			 &temp, sizeof(temp));
	if (err)
		return err;

	if ((temp & Q10_TO_Q11_FANOUT_AND_CLOCK_SYNC_ENABLE_MASK) ==
	    Q10_TO_Q11_FANOUT_AND_CLOCK_SYNC_ENABLE_MASK)
		out11_mux = 1;

	for (pll = 0; pll < 8; pll++) {
		qn = 0;
		qn_plus_1 = 0;

		if (pll < 4) {
			/* First 4 pll has 2 outputs */
			qn = output_mask & 0x1;
			output_mask = output_mask >> 1;
			qn_plus_1 = output_mask & 0x1;
			output_mask = output_mask >> 1;
		} else if (pll == 4) {
			if (out8_mux == 0) {
				qn = output_mask & 0x1;
				output_mask = output_mask >> 1;
			}
		} else if (pll == 5) {
			if (out8_mux) {
				qn_plus_1 = output_mask & 0x1;
				output_mask = output_mask >> 1;
			}
			qn = output_mask & 0x1;
			output_mask = output_mask >> 1;
		} else if (pll == 6) {
			qn = output_mask & 0x1;
			output_mask = output_mask >> 1;
			if (out11_mux) {
				qn_plus_1 = output_mask & 0x1;
				output_mask = output_mask >> 1;
			}
		} else if (pll == 7) {
			if (out11_mux == 0) {
				qn = output_mask & 0x1;
				output_mask = output_mask >> 1;
			}
		}

		if ((qn != 0) || (qn_plus_1 != 0))
			err = _enable_pll_tod_sync(idtcm, pll, sync_src, qn,
					       qn_plus_1);

		if (err)
			return err;
	}

	return err;
}

static int idtcm_enable_tod(struct idtcm_channel *channel)
{
	struct idtcm *idtcm = channel->idtcm;
	struct timespec64 ts = {0, 0};
	u8 cfg;
	int err;

1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983
	/*
	 * Start the TOD clock ticking.
	 */
	err = idtcm_read(idtcm, channel->tod_n, TOD_CFG, &cfg, sizeof(cfg));
	if (err)
		return err;

	cfg |= TOD_ENABLE;

	err = idtcm_write(idtcm, channel->tod_n, TOD_CFG, &cfg, sizeof(cfg));
	if (err)
		return err;

1984 1985 1986 1987 1988
	if (idtcm->deprecated)
		return _idtcm_settime_deprecated(channel, &ts);
	else
		return _idtcm_settime(channel, &ts,
				      SCSR_TOD_WR_TYPE_SEL_ABSOLUTE);
1989 1990
}

1991
static void idtcm_set_version_info(struct idtcm *idtcm)
1992 1993 1994 1995 1996 1997
{
	u8 major;
	u8 minor;
	u8 hotfix;
	u16 product_id;
	u8 hw_rev_id;
1998
	u8 config_select;
1999 2000 2001 2002 2003 2004 2005

	idtcm_read_major_release(idtcm, &major);
	idtcm_read_minor_release(idtcm, &minor);
	idtcm_read_hotfix_release(idtcm, &hotfix);

	idtcm_read_product_id(idtcm, &product_id);
	idtcm_read_hw_rev_id(idtcm, &hw_rev_id);
2006 2007 2008

	idtcm_read_otp_scsr_config_select(idtcm, &config_select);

2009 2010 2011
	snprintf(idtcm->version, sizeof(idtcm->version), "%u.%u.%u",
		 major, minor, hotfix);

2012 2013 2014 2015 2016
	if (idtcm_strverscmp(idtcm->version, "4.8.7") >= 0)
		idtcm->deprecated = 0;
	else
		idtcm->deprecated = 1;

2017 2018 2019
	dev_info(&idtcm->client->dev,
		 "%d.%d.%d, Id: 0x%04x  HW Rev: %d  OTP Config Select: %d",
		 major, minor, hotfix,
2020
		 product_id, hw_rev_id, config_select);
2021 2022
}

2023
static const struct ptp_clock_info idtcm_caps = {
2024 2025 2026 2027 2028
	.owner		= THIS_MODULE,
	.max_adj	= 244000,
	.n_per_out	= 12,
	.adjphase	= &idtcm_adjphase,
	.adjfine	= &idtcm_adjfine,
2029
	.adjtime	= &idtcm_adjtime,
2030
	.gettime64	= &idtcm_gettime,
2031
	.settime64	= &idtcm_settime,
2032 2033 2034
	.enable		= &idtcm_enable,
};

2035
static const struct ptp_clock_info idtcm_caps_deprecated = {
2036 2037
	.owner		= THIS_MODULE,
	.max_adj	= 244000,
2038
	.n_per_out	= 12,
2039
	.adjphase	= &idtcm_adjphase,
2040
	.adjfine	= &idtcm_adjfine,
2041
	.adjtime	= &idtcm_adjtime_deprecated,
2042
	.gettime64	= &idtcm_gettime,
2043
	.settime64	= &idtcm_settime_deprecated,
2044 2045 2046
	.enable		= &idtcm_enable,
};

2047
static int configure_channel_pll(struct idtcm_channel *channel)
2048
{
2049
	int err = 0;
2050

2051
	switch (channel->pll) {
2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083
	case 0:
		channel->dpll_freq = DPLL_FREQ_0;
		channel->dpll_n = DPLL_0;
		channel->hw_dpll_n = HW_DPLL_0;
		channel->dpll_phase = DPLL_PHASE_0;
		channel->dpll_ctrl_n = DPLL_CTRL_0;
		channel->dpll_phase_pull_in = DPLL_PHASE_PULL_IN_0;
		break;
	case 1:
		channel->dpll_freq = DPLL_FREQ_1;
		channel->dpll_n = DPLL_1;
		channel->hw_dpll_n = HW_DPLL_1;
		channel->dpll_phase = DPLL_PHASE_1;
		channel->dpll_ctrl_n = DPLL_CTRL_1;
		channel->dpll_phase_pull_in = DPLL_PHASE_PULL_IN_1;
		break;
	case 2:
		channel->dpll_freq = DPLL_FREQ_2;
		channel->dpll_n = DPLL_2;
		channel->hw_dpll_n = HW_DPLL_2;
		channel->dpll_phase = DPLL_PHASE_2;
		channel->dpll_ctrl_n = DPLL_CTRL_2;
		channel->dpll_phase_pull_in = DPLL_PHASE_PULL_IN_2;
		break;
	case 3:
		channel->dpll_freq = DPLL_FREQ_3;
		channel->dpll_n = DPLL_3;
		channel->hw_dpll_n = HW_DPLL_3;
		channel->dpll_phase = DPLL_PHASE_3;
		channel->dpll_ctrl_n = DPLL_CTRL_3;
		channel->dpll_phase_pull_in = DPLL_PHASE_PULL_IN_3;
		break;
2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159
	case 4:
		channel->dpll_freq = DPLL_FREQ_4;
		channel->dpll_n = DPLL_4;
		channel->hw_dpll_n = HW_DPLL_4;
		channel->dpll_phase = DPLL_PHASE_4;
		channel->dpll_ctrl_n = DPLL_CTRL_4;
		channel->dpll_phase_pull_in = DPLL_PHASE_PULL_IN_4;
		break;
	case 5:
		channel->dpll_freq = DPLL_FREQ_5;
		channel->dpll_n = DPLL_5;
		channel->hw_dpll_n = HW_DPLL_5;
		channel->dpll_phase = DPLL_PHASE_5;
		channel->dpll_ctrl_n = DPLL_CTRL_5;
		channel->dpll_phase_pull_in = DPLL_PHASE_PULL_IN_5;
		break;
	case 6:
		channel->dpll_freq = DPLL_FREQ_6;
		channel->dpll_n = DPLL_6;
		channel->hw_dpll_n = HW_DPLL_6;
		channel->dpll_phase = DPLL_PHASE_6;
		channel->dpll_ctrl_n = DPLL_CTRL_6;
		channel->dpll_phase_pull_in = DPLL_PHASE_PULL_IN_6;
		break;
	case 7:
		channel->dpll_freq = DPLL_FREQ_7;
		channel->dpll_n = DPLL_7;
		channel->hw_dpll_n = HW_DPLL_7;
		channel->dpll_phase = DPLL_PHASE_7;
		channel->dpll_ctrl_n = DPLL_CTRL_7;
		channel->dpll_phase_pull_in = DPLL_PHASE_PULL_IN_7;
		break;
	default:
		err = -EINVAL;
	}

	return err;
}

static int idtcm_enable_channel(struct idtcm *idtcm, u32 index)
{
	struct idtcm_channel *channel;
	int err;

	if (!(index < MAX_TOD))
		return -EINVAL;

	channel = &idtcm->channel[index];

	/* Set pll addresses */
	err = configure_channel_pll(channel);
	if (err)
		return err;

	/* Set tod addresses */
	switch (index) {
	case 0:
		channel->tod_read_primary = TOD_READ_PRIMARY_0;
		channel->tod_write = TOD_WRITE_0;
		channel->tod_n = TOD_0;
		break;
	case 1:
		channel->tod_read_primary = TOD_READ_PRIMARY_1;
		channel->tod_write = TOD_WRITE_1;
		channel->tod_n = TOD_1;
		break;
	case 2:
		channel->tod_read_primary = TOD_READ_PRIMARY_2;
		channel->tod_write = TOD_WRITE_2;
		channel->tod_n = TOD_2;
		break;
	case 3:
		channel->tod_read_primary = TOD_READ_PRIMARY_3;
		channel->tod_write = TOD_WRITE_3;
		channel->tod_n = TOD_3;
		break;
2160 2161 2162 2163 2164 2165
	default:
		return -EINVAL;
	}

	channel->idtcm = idtcm;

2166 2167
	if (idtcm->deprecated)
		channel->caps = idtcm_caps_deprecated;
2168 2169 2170
	else
		channel->caps = idtcm_caps;

2171
	snprintf(channel->caps.name, sizeof(channel->caps.name),
2172 2173
		 "IDT CM TOD%u", index);

2174
	if (!idtcm->deprecated) {
2175 2176 2177
		err = idtcm_enable_tod_sync(channel);
		if (err) {
			dev_err(&idtcm->client->dev,
2178
				"Failed at line %d in %s!", __LINE__, __func__);
2179 2180 2181
			return err;
		}
	}
2182

2183 2184
	/* Sync pll mode with hardware */
	err = idtcm_get_pll_mode(channel, &channel->pll_mode);
2185 2186
	if (err) {
		dev_err(&idtcm->client->dev,
2187
			"Error: %s - Unable to read pll mode", __func__);
2188
		return err;
2189
	}
2190 2191

	err = idtcm_enable_tod(channel);
2192 2193
	if (err) {
		dev_err(&idtcm->client->dev,
2194
			"Failed at line %d in %s!", __LINE__, __func__);
2195
		return err;
2196
	}
2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208

	channel->ptp_clock = ptp_clock_register(&channel->caps, NULL);

	if (IS_ERR(channel->ptp_clock)) {
		err = PTR_ERR(channel->ptp_clock);
		channel->ptp_clock = NULL;
		return err;
	}

	if (!channel->ptp_clock)
		return -ENOTSUPP;

2209
	dev_info(&idtcm->client->dev, "PLL%d registered as ptp%d",
2210 2211 2212 2213 2214 2215 2216 2217 2218 2219
		 index, channel->ptp_clock->index);

	return 0;
}

static void ptp_clock_unregister_all(struct idtcm *idtcm)
{
	u8 i;
	struct idtcm_channel *channel;

2220
	for (i = 0; i < MAX_TOD; i++) {
2221 2222 2223 2224 2225 2226 2227 2228 2229 2230

		channel = &idtcm->channel[i];

		if (channel->ptp_clock)
			ptp_clock_unregister(channel->ptp_clock);
	}
}

static void set_default_masks(struct idtcm *idtcm)
{
2231 2232 2233 2234 2235 2236
	idtcm->tod_mask = DEFAULT_TOD_MASK;

	idtcm->channel[0].pll = DEFAULT_TOD0_PTP_PLL;
	idtcm->channel[1].pll = DEFAULT_TOD1_PTP_PLL;
	idtcm->channel[2].pll = DEFAULT_TOD2_PTP_PLL;
	idtcm->channel[3].pll = DEFAULT_TOD3_PTP_PLL;
2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267

	idtcm->channel[0].output_mask = DEFAULT_OUTPUT_MASK_PLL0;
	idtcm->channel[1].output_mask = DEFAULT_OUTPUT_MASK_PLL1;
	idtcm->channel[2].output_mask = DEFAULT_OUTPUT_MASK_PLL2;
	idtcm->channel[3].output_mask = DEFAULT_OUTPUT_MASK_PLL3;
}

static int idtcm_probe(struct i2c_client *client,
		       const struct i2c_device_id *id)
{
	struct idtcm *idtcm;
	int err;
	u8 i;

	/* Unused for now */
	(void)id;

	idtcm = devm_kzalloc(&client->dev, sizeof(struct idtcm), GFP_KERNEL);

	if (!idtcm)
		return -ENOMEM;

	idtcm->client = client;
	idtcm->page_offset = 0xff;
	idtcm->calculate_overhead_flag = 0;

	set_default_masks(idtcm);

	mutex_init(&idtcm->reg_lock);
	mutex_lock(&idtcm->reg_lock);

2268
	idtcm_set_version_info(idtcm);
2269 2270 2271 2272 2273

	err = idtcm_load_firmware(idtcm, &client->dev);

	if (err)
		dev_warn(&idtcm->client->dev,
2274
			 "loading firmware failed with %d", err);
2275

2276
	wait_for_chip_ready(idtcm);
2277

2278 2279 2280
	if (idtcm->tod_mask) {
		for (i = 0; i < MAX_TOD; i++) {
			if (idtcm->tod_mask & (1 << i)) {
2281
				err = idtcm_enable_channel(idtcm, i);
2282 2283
				if (err) {
					dev_err(&idtcm->client->dev,
2284
						"idtcm_enable_channel %d failed!", i);
2285
					break;
2286
				}
2287 2288 2289 2290
			}
		}
	} else {
		dev_err(&idtcm->client->dev,
2291
			"no PLLs flagged as PHCs, nothing to do");
2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400
		err = -ENODEV;
	}

	mutex_unlock(&idtcm->reg_lock);

	if (err) {
		ptp_clock_unregister_all(idtcm);
		return err;
	}

	i2c_set_clientdata(client, idtcm);

	return 0;
}

static int idtcm_remove(struct i2c_client *client)
{
	struct idtcm *idtcm = i2c_get_clientdata(client);

	ptp_clock_unregister_all(idtcm);

	mutex_destroy(&idtcm->reg_lock);

	return 0;
}

#ifdef CONFIG_OF
static const struct of_device_id idtcm_dt_id[] = {
	{ .compatible = "idt,8a34000" },
	{ .compatible = "idt,8a34001" },
	{ .compatible = "idt,8a34002" },
	{ .compatible = "idt,8a34003" },
	{ .compatible = "idt,8a34004" },
	{ .compatible = "idt,8a34005" },
	{ .compatible = "idt,8a34006" },
	{ .compatible = "idt,8a34007" },
	{ .compatible = "idt,8a34008" },
	{ .compatible = "idt,8a34009" },
	{ .compatible = "idt,8a34010" },
	{ .compatible = "idt,8a34011" },
	{ .compatible = "idt,8a34012" },
	{ .compatible = "idt,8a34013" },
	{ .compatible = "idt,8a34014" },
	{ .compatible = "idt,8a34015" },
	{ .compatible = "idt,8a34016" },
	{ .compatible = "idt,8a34017" },
	{ .compatible = "idt,8a34018" },
	{ .compatible = "idt,8a34019" },
	{ .compatible = "idt,8a34040" },
	{ .compatible = "idt,8a34041" },
	{ .compatible = "idt,8a34042" },
	{ .compatible = "idt,8a34043" },
	{ .compatible = "idt,8a34044" },
	{ .compatible = "idt,8a34045" },
	{ .compatible = "idt,8a34046" },
	{ .compatible = "idt,8a34047" },
	{ .compatible = "idt,8a34048" },
	{ .compatible = "idt,8a34049" },
	{},
};
MODULE_DEVICE_TABLE(of, idtcm_dt_id);
#endif

static const struct i2c_device_id idtcm_i2c_id[] = {
	{ "8a34000" },
	{ "8a34001" },
	{ "8a34002" },
	{ "8a34003" },
	{ "8a34004" },
	{ "8a34005" },
	{ "8a34006" },
	{ "8a34007" },
	{ "8a34008" },
	{ "8a34009" },
	{ "8a34010" },
	{ "8a34011" },
	{ "8a34012" },
	{ "8a34013" },
	{ "8a34014" },
	{ "8a34015" },
	{ "8a34016" },
	{ "8a34017" },
	{ "8a34018" },
	{ "8a34019" },
	{ "8a34040" },
	{ "8a34041" },
	{ "8a34042" },
	{ "8a34043" },
	{ "8a34044" },
	{ "8a34045" },
	{ "8a34046" },
	{ "8a34047" },
	{ "8a34048" },
	{ "8a34049" },
	{},
};
MODULE_DEVICE_TABLE(i2c, idtcm_i2c_id);

static struct i2c_driver idtcm_driver = {
	.driver = {
		.of_match_table	= of_match_ptr(idtcm_dt_id),
		.name		= "idtcm",
	},
	.probe		= idtcm_probe,
	.remove		= idtcm_remove,
	.id_table	= idtcm_i2c_id,
};

module_i2c_driver(idtcm_driver);