18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * Telecom Clock driver for Intel NetStructure(tm) MPCBL0010
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci * Copyright (C) 2005 Kontron Canada
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci * All rights reserved.
78c2ecf20Sopenharmony_ci *
88c2ecf20Sopenharmony_ci * This program is free software; you can redistribute it and/or modify
98c2ecf20Sopenharmony_ci * it under the terms of the GNU General Public License as published by
108c2ecf20Sopenharmony_ci * the Free Software Foundation; either version 2 of the License, or (at
118c2ecf20Sopenharmony_ci * your option) any later version.
128c2ecf20Sopenharmony_ci *
138c2ecf20Sopenharmony_ci * This program is distributed in the hope that it will be useful, but
148c2ecf20Sopenharmony_ci * WITHOUT ANY WARRANTY; without even the implied warranty of
158c2ecf20Sopenharmony_ci * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
168c2ecf20Sopenharmony_ci * NON INFRINGEMENT.  See the GNU General Public License for more
178c2ecf20Sopenharmony_ci * details.
188c2ecf20Sopenharmony_ci *
198c2ecf20Sopenharmony_ci * You should have received a copy of the GNU General Public License
208c2ecf20Sopenharmony_ci * along with this program; if not, write to the Free Software
218c2ecf20Sopenharmony_ci * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
228c2ecf20Sopenharmony_ci *
238c2ecf20Sopenharmony_ci * Send feedback to <sebastien.bouchard@ca.kontron.com> and the current
248c2ecf20Sopenharmony_ci * Maintainer  <mark.gross@intel.com>
258c2ecf20Sopenharmony_ci *
268c2ecf20Sopenharmony_ci * Description : This is the TELECOM CLOCK module driver for the ATCA
278c2ecf20Sopenharmony_ci * MPCBL0010 ATCA computer.
288c2ecf20Sopenharmony_ci */
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci#include <linux/module.h>
318c2ecf20Sopenharmony_ci#include <linux/init.h>
328c2ecf20Sopenharmony_ci#include <linux/kernel.h>	/* printk() */
338c2ecf20Sopenharmony_ci#include <linux/fs.h>		/* everything... */
348c2ecf20Sopenharmony_ci#include <linux/errno.h>	/* error codes */
358c2ecf20Sopenharmony_ci#include <linux/sched.h>
368c2ecf20Sopenharmony_ci#include <linux/slab.h>
378c2ecf20Sopenharmony_ci#include <linux/ioport.h>
388c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
398c2ecf20Sopenharmony_ci#include <linux/spinlock.h>
408c2ecf20Sopenharmony_ci#include <linux/mutex.h>
418c2ecf20Sopenharmony_ci#include <linux/timer.h>
428c2ecf20Sopenharmony_ci#include <linux/sysfs.h>
438c2ecf20Sopenharmony_ci#include <linux/device.h>
448c2ecf20Sopenharmony_ci#include <linux/miscdevice.h>
458c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
468c2ecf20Sopenharmony_ci#include <asm/io.h>		/* inb/outb */
478c2ecf20Sopenharmony_ci#include <linux/uaccess.h>
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ciMODULE_AUTHOR("Sebastien Bouchard <sebastien.bouchard@ca.kontron.com>");
508c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_ci/*Hardware Reset of the PLL */
538c2ecf20Sopenharmony_ci#define RESET_ON	0x00
548c2ecf20Sopenharmony_ci#define RESET_OFF	0x01
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_ci/* MODE SELECT */
578c2ecf20Sopenharmony_ci#define NORMAL_MODE 	0x00
588c2ecf20Sopenharmony_ci#define HOLDOVER_MODE	0x10
598c2ecf20Sopenharmony_ci#define FREERUN_MODE	0x20
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ci/* FILTER SELECT */
628c2ecf20Sopenharmony_ci#define FILTER_6HZ	0x04
638c2ecf20Sopenharmony_ci#define FILTER_12HZ	0x00
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ci/* SELECT REFERENCE FREQUENCY */
668c2ecf20Sopenharmony_ci#define REF_CLK1_8kHz		0x00
678c2ecf20Sopenharmony_ci#define REF_CLK2_19_44MHz	0x02
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ci/* Select primary or secondary redundant clock */
708c2ecf20Sopenharmony_ci#define PRIMARY_CLOCK	0x00
718c2ecf20Sopenharmony_ci#define SECONDARY_CLOCK	0x01
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ci/* CLOCK TRANSMISSION DEFINE */
748c2ecf20Sopenharmony_ci#define CLK_8kHz	0xff
758c2ecf20Sopenharmony_ci#define CLK_16_384MHz	0xfb
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ci#define CLK_1_544MHz	0x00
788c2ecf20Sopenharmony_ci#define CLK_2_048MHz	0x01
798c2ecf20Sopenharmony_ci#define CLK_4_096MHz	0x02
808c2ecf20Sopenharmony_ci#define CLK_6_312MHz	0x03
818c2ecf20Sopenharmony_ci#define CLK_8_192MHz	0x04
828c2ecf20Sopenharmony_ci#define CLK_19_440MHz	0x06
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci#define CLK_8_592MHz	0x08
858c2ecf20Sopenharmony_ci#define CLK_11_184MHz	0x09
868c2ecf20Sopenharmony_ci#define CLK_34_368MHz	0x0b
878c2ecf20Sopenharmony_ci#define CLK_44_736MHz	0x0a
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_ci/* RECEIVED REFERENCE */
908c2ecf20Sopenharmony_ci#define AMC_B1 0
918c2ecf20Sopenharmony_ci#define AMC_B2 1
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ci/* HARDWARE SWITCHING DEFINE */
948c2ecf20Sopenharmony_ci#define HW_ENABLE	0x80
958c2ecf20Sopenharmony_ci#define HW_DISABLE	0x00
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_ci/* HARDWARE SWITCHING MODE DEFINE */
988c2ecf20Sopenharmony_ci#define PLL_HOLDOVER	0x40
998c2ecf20Sopenharmony_ci#define LOST_CLOCK	0x00
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci/* ALARMS DEFINE */
1028c2ecf20Sopenharmony_ci#define UNLOCK_MASK	0x10
1038c2ecf20Sopenharmony_ci#define HOLDOVER_MASK	0x20
1048c2ecf20Sopenharmony_ci#define SEC_LOST_MASK	0x40
1058c2ecf20Sopenharmony_ci#define PRI_LOST_MASK	0x80
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_ci/* INTERRUPT CAUSE DEFINE */
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ci#define PRI_LOS_01_MASK		0x01
1108c2ecf20Sopenharmony_ci#define PRI_LOS_10_MASK		0x02
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_ci#define SEC_LOS_01_MASK		0x04
1138c2ecf20Sopenharmony_ci#define SEC_LOS_10_MASK		0x08
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_ci#define HOLDOVER_01_MASK	0x10
1168c2ecf20Sopenharmony_ci#define HOLDOVER_10_MASK	0x20
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_ci#define UNLOCK_01_MASK		0x40
1198c2ecf20Sopenharmony_ci#define UNLOCK_10_MASK		0x80
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_cistruct tlclk_alarms {
1228c2ecf20Sopenharmony_ci	__u32 lost_clocks;
1238c2ecf20Sopenharmony_ci	__u32 lost_primary_clock;
1248c2ecf20Sopenharmony_ci	__u32 lost_secondary_clock;
1258c2ecf20Sopenharmony_ci	__u32 primary_clock_back;
1268c2ecf20Sopenharmony_ci	__u32 secondary_clock_back;
1278c2ecf20Sopenharmony_ci	__u32 switchover_primary;
1288c2ecf20Sopenharmony_ci	__u32 switchover_secondary;
1298c2ecf20Sopenharmony_ci	__u32 pll_holdover;
1308c2ecf20Sopenharmony_ci	__u32 pll_end_holdover;
1318c2ecf20Sopenharmony_ci	__u32 pll_lost_sync;
1328c2ecf20Sopenharmony_ci	__u32 pll_sync;
1338c2ecf20Sopenharmony_ci};
1348c2ecf20Sopenharmony_ci/* Telecom clock I/O register definition */
1358c2ecf20Sopenharmony_ci#define TLCLK_BASE 0xa08
1368c2ecf20Sopenharmony_ci#define TLCLK_REG0 TLCLK_BASE
1378c2ecf20Sopenharmony_ci#define TLCLK_REG1 (TLCLK_BASE+1)
1388c2ecf20Sopenharmony_ci#define TLCLK_REG2 (TLCLK_BASE+2)
1398c2ecf20Sopenharmony_ci#define TLCLK_REG3 (TLCLK_BASE+3)
1408c2ecf20Sopenharmony_ci#define TLCLK_REG4 (TLCLK_BASE+4)
1418c2ecf20Sopenharmony_ci#define TLCLK_REG5 (TLCLK_BASE+5)
1428c2ecf20Sopenharmony_ci#define TLCLK_REG6 (TLCLK_BASE+6)
1438c2ecf20Sopenharmony_ci#define TLCLK_REG7 (TLCLK_BASE+7)
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_ci#define SET_PORT_BITS(port, mask, val) outb(((inb(port) & mask) | val), port)
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_ci/* 0 = Dynamic allocation of the major device number */
1488c2ecf20Sopenharmony_ci#define TLCLK_MAJOR 0
1498c2ecf20Sopenharmony_ci
1508c2ecf20Sopenharmony_ci/* sysfs interface definition:
1518c2ecf20Sopenharmony_ciUpon loading the driver will create a sysfs directory under
1528c2ecf20Sopenharmony_ci/sys/devices/platform/telco_clock.
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_ciThis directory exports the following interfaces.  There operation is
1558c2ecf20Sopenharmony_cidocumented in the MCPBL0010 TPS under the Telecom Clock API section, 11.4.
1568c2ecf20Sopenharmony_cialarms				:
1578c2ecf20Sopenharmony_cicurrent_ref			:
1588c2ecf20Sopenharmony_cireceived_ref_clk3a		:
1598c2ecf20Sopenharmony_cireceived_ref_clk3b		:
1608c2ecf20Sopenharmony_cienable_clk3a_output		:
1618c2ecf20Sopenharmony_cienable_clk3b_output		:
1628c2ecf20Sopenharmony_cienable_clka0_output		:
1638c2ecf20Sopenharmony_cienable_clka1_output		:
1648c2ecf20Sopenharmony_cienable_clkb0_output		:
1658c2ecf20Sopenharmony_cienable_clkb1_output		:
1668c2ecf20Sopenharmony_cifilter_select			:
1678c2ecf20Sopenharmony_cihardware_switching		:
1688c2ecf20Sopenharmony_cihardware_switching_mode		:
1698c2ecf20Sopenharmony_citelclock_version		:
1708c2ecf20Sopenharmony_cimode_select			:
1718c2ecf20Sopenharmony_cirefalign			:
1728c2ecf20Sopenharmony_cireset				:
1738c2ecf20Sopenharmony_ciselect_amcb1_transmit_clock	:
1748c2ecf20Sopenharmony_ciselect_amcb2_transmit_clock	:
1758c2ecf20Sopenharmony_ciselect_redundant_clock		:
1768c2ecf20Sopenharmony_ciselect_ref_frequency		:
1778c2ecf20Sopenharmony_ci
1788c2ecf20Sopenharmony_ciAll sysfs interfaces are integers in hex format, i.e echo 99 > refalign
1798c2ecf20Sopenharmony_cihas the same effect as echo 0x99 > refalign.
1808c2ecf20Sopenharmony_ci*/
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_cistatic unsigned int telclk_interrupt;
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_cistatic int int_events;		/* Event that generate a interrupt */
1858c2ecf20Sopenharmony_cistatic int got_event;		/* if events processing have been done */
1868c2ecf20Sopenharmony_ci
1878c2ecf20Sopenharmony_cistatic void switchover_timeout(struct timer_list *t);
1888c2ecf20Sopenharmony_cistatic struct timer_list switchover_timer;
1898c2ecf20Sopenharmony_cistatic unsigned long tlclk_timer_data;
1908c2ecf20Sopenharmony_ci
1918c2ecf20Sopenharmony_cistatic struct tlclk_alarms *alarm_events;
1928c2ecf20Sopenharmony_ci
1938c2ecf20Sopenharmony_cistatic DEFINE_SPINLOCK(event_lock);
1948c2ecf20Sopenharmony_ci
1958c2ecf20Sopenharmony_cistatic int tlclk_major = TLCLK_MAJOR;
1968c2ecf20Sopenharmony_ci
1978c2ecf20Sopenharmony_cistatic irqreturn_t tlclk_interrupt(int irq, void *dev_id);
1988c2ecf20Sopenharmony_ci
1998c2ecf20Sopenharmony_cistatic DECLARE_WAIT_QUEUE_HEAD(wq);
2008c2ecf20Sopenharmony_ci
2018c2ecf20Sopenharmony_cistatic unsigned long useflags;
2028c2ecf20Sopenharmony_cistatic DEFINE_MUTEX(tlclk_mutex);
2038c2ecf20Sopenharmony_ci
2048c2ecf20Sopenharmony_cistatic int tlclk_open(struct inode *inode, struct file *filp)
2058c2ecf20Sopenharmony_ci{
2068c2ecf20Sopenharmony_ci	int result;
2078c2ecf20Sopenharmony_ci
2088c2ecf20Sopenharmony_ci	mutex_lock(&tlclk_mutex);
2098c2ecf20Sopenharmony_ci	if (test_and_set_bit(0, &useflags)) {
2108c2ecf20Sopenharmony_ci		result = -EBUSY;
2118c2ecf20Sopenharmony_ci		/* this legacy device is always one per system and it doesn't
2128c2ecf20Sopenharmony_ci		 * know how to handle multiple concurrent clients.
2138c2ecf20Sopenharmony_ci		 */
2148c2ecf20Sopenharmony_ci		goto out;
2158c2ecf20Sopenharmony_ci	}
2168c2ecf20Sopenharmony_ci
2178c2ecf20Sopenharmony_ci	/* Make sure there is no interrupt pending while
2188c2ecf20Sopenharmony_ci	 * initialising interrupt handler */
2198c2ecf20Sopenharmony_ci	inb(TLCLK_REG6);
2208c2ecf20Sopenharmony_ci
2218c2ecf20Sopenharmony_ci	/* This device is wired through the FPGA IO space of the ATCA blade
2228c2ecf20Sopenharmony_ci	 * we can't share this IRQ */
2238c2ecf20Sopenharmony_ci	result = request_irq(telclk_interrupt, &tlclk_interrupt,
2248c2ecf20Sopenharmony_ci			     0, "telco_clock", tlclk_interrupt);
2258c2ecf20Sopenharmony_ci	if (result == -EBUSY)
2268c2ecf20Sopenharmony_ci		printk(KERN_ERR "tlclk: Interrupt can't be reserved.\n");
2278c2ecf20Sopenharmony_ci	else
2288c2ecf20Sopenharmony_ci		inb(TLCLK_REG6);	/* Clear interrupt events */
2298c2ecf20Sopenharmony_ci
2308c2ecf20Sopenharmony_ciout:
2318c2ecf20Sopenharmony_ci	mutex_unlock(&tlclk_mutex);
2328c2ecf20Sopenharmony_ci	return result;
2338c2ecf20Sopenharmony_ci}
2348c2ecf20Sopenharmony_ci
2358c2ecf20Sopenharmony_cistatic int tlclk_release(struct inode *inode, struct file *filp)
2368c2ecf20Sopenharmony_ci{
2378c2ecf20Sopenharmony_ci	free_irq(telclk_interrupt, tlclk_interrupt);
2388c2ecf20Sopenharmony_ci	clear_bit(0, &useflags);
2398c2ecf20Sopenharmony_ci
2408c2ecf20Sopenharmony_ci	return 0;
2418c2ecf20Sopenharmony_ci}
2428c2ecf20Sopenharmony_ci
2438c2ecf20Sopenharmony_cistatic ssize_t tlclk_read(struct file *filp, char __user *buf, size_t count,
2448c2ecf20Sopenharmony_ci		loff_t *f_pos)
2458c2ecf20Sopenharmony_ci{
2468c2ecf20Sopenharmony_ci	if (count < sizeof(struct tlclk_alarms))
2478c2ecf20Sopenharmony_ci		return -EIO;
2488c2ecf20Sopenharmony_ci	if (mutex_lock_interruptible(&tlclk_mutex))
2498c2ecf20Sopenharmony_ci		return -EINTR;
2508c2ecf20Sopenharmony_ci
2518c2ecf20Sopenharmony_ci
2528c2ecf20Sopenharmony_ci	wait_event_interruptible(wq, got_event);
2538c2ecf20Sopenharmony_ci	if (copy_to_user(buf, alarm_events, sizeof(struct tlclk_alarms))) {
2548c2ecf20Sopenharmony_ci		mutex_unlock(&tlclk_mutex);
2558c2ecf20Sopenharmony_ci		return -EFAULT;
2568c2ecf20Sopenharmony_ci	}
2578c2ecf20Sopenharmony_ci
2588c2ecf20Sopenharmony_ci	memset(alarm_events, 0, sizeof(struct tlclk_alarms));
2598c2ecf20Sopenharmony_ci	got_event = 0;
2608c2ecf20Sopenharmony_ci
2618c2ecf20Sopenharmony_ci	mutex_unlock(&tlclk_mutex);
2628c2ecf20Sopenharmony_ci	return  sizeof(struct tlclk_alarms);
2638c2ecf20Sopenharmony_ci}
2648c2ecf20Sopenharmony_ci
2658c2ecf20Sopenharmony_cistatic const struct file_operations tlclk_fops = {
2668c2ecf20Sopenharmony_ci	.read = tlclk_read,
2678c2ecf20Sopenharmony_ci	.open = tlclk_open,
2688c2ecf20Sopenharmony_ci	.release = tlclk_release,
2698c2ecf20Sopenharmony_ci	.llseek = noop_llseek,
2708c2ecf20Sopenharmony_ci
2718c2ecf20Sopenharmony_ci};
2728c2ecf20Sopenharmony_ci
2738c2ecf20Sopenharmony_cistatic struct miscdevice tlclk_miscdev = {
2748c2ecf20Sopenharmony_ci	.minor = MISC_DYNAMIC_MINOR,
2758c2ecf20Sopenharmony_ci	.name = "telco_clock",
2768c2ecf20Sopenharmony_ci	.fops = &tlclk_fops,
2778c2ecf20Sopenharmony_ci};
2788c2ecf20Sopenharmony_ci
2798c2ecf20Sopenharmony_cistatic ssize_t show_current_ref(struct device *d,
2808c2ecf20Sopenharmony_ci		struct device_attribute *attr, char *buf)
2818c2ecf20Sopenharmony_ci{
2828c2ecf20Sopenharmony_ci	unsigned long ret_val;
2838c2ecf20Sopenharmony_ci	unsigned long flags;
2848c2ecf20Sopenharmony_ci
2858c2ecf20Sopenharmony_ci	spin_lock_irqsave(&event_lock, flags);
2868c2ecf20Sopenharmony_ci	ret_val = ((inb(TLCLK_REG1) & 0x08) >> 3);
2878c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&event_lock, flags);
2888c2ecf20Sopenharmony_ci
2898c2ecf20Sopenharmony_ci	return sprintf(buf, "0x%lX\n", ret_val);
2908c2ecf20Sopenharmony_ci}
2918c2ecf20Sopenharmony_ci
2928c2ecf20Sopenharmony_cistatic DEVICE_ATTR(current_ref, S_IRUGO, show_current_ref, NULL);
2938c2ecf20Sopenharmony_ci
2948c2ecf20Sopenharmony_ci
2958c2ecf20Sopenharmony_cistatic ssize_t show_telclock_version(struct device *d,
2968c2ecf20Sopenharmony_ci		struct device_attribute *attr, char *buf)
2978c2ecf20Sopenharmony_ci{
2988c2ecf20Sopenharmony_ci	unsigned long ret_val;
2998c2ecf20Sopenharmony_ci	unsigned long flags;
3008c2ecf20Sopenharmony_ci
3018c2ecf20Sopenharmony_ci	spin_lock_irqsave(&event_lock, flags);
3028c2ecf20Sopenharmony_ci	ret_val = inb(TLCLK_REG5);
3038c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&event_lock, flags);
3048c2ecf20Sopenharmony_ci
3058c2ecf20Sopenharmony_ci	return sprintf(buf, "0x%lX\n", ret_val);
3068c2ecf20Sopenharmony_ci}
3078c2ecf20Sopenharmony_ci
3088c2ecf20Sopenharmony_cistatic DEVICE_ATTR(telclock_version, S_IRUGO,
3098c2ecf20Sopenharmony_ci		show_telclock_version, NULL);
3108c2ecf20Sopenharmony_ci
3118c2ecf20Sopenharmony_cistatic ssize_t show_alarms(struct device *d,
3128c2ecf20Sopenharmony_ci		struct device_attribute *attr,  char *buf)
3138c2ecf20Sopenharmony_ci{
3148c2ecf20Sopenharmony_ci	unsigned long ret_val;
3158c2ecf20Sopenharmony_ci	unsigned long flags;
3168c2ecf20Sopenharmony_ci
3178c2ecf20Sopenharmony_ci	spin_lock_irqsave(&event_lock, flags);
3188c2ecf20Sopenharmony_ci	ret_val = (inb(TLCLK_REG2) & 0xf0);
3198c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&event_lock, flags);
3208c2ecf20Sopenharmony_ci
3218c2ecf20Sopenharmony_ci	return sprintf(buf, "0x%lX\n", ret_val);
3228c2ecf20Sopenharmony_ci}
3238c2ecf20Sopenharmony_ci
3248c2ecf20Sopenharmony_cistatic DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
3258c2ecf20Sopenharmony_ci
3268c2ecf20Sopenharmony_cistatic ssize_t store_received_ref_clk3a(struct device *d,
3278c2ecf20Sopenharmony_ci		 struct device_attribute *attr, const char *buf, size_t count)
3288c2ecf20Sopenharmony_ci{
3298c2ecf20Sopenharmony_ci	unsigned long tmp;
3308c2ecf20Sopenharmony_ci	unsigned char val;
3318c2ecf20Sopenharmony_ci	unsigned long flags;
3328c2ecf20Sopenharmony_ci
3338c2ecf20Sopenharmony_ci	sscanf(buf, "%lX", &tmp);
3348c2ecf20Sopenharmony_ci	dev_dbg(d, ": tmp = 0x%lX\n", tmp);
3358c2ecf20Sopenharmony_ci
3368c2ecf20Sopenharmony_ci	val = (unsigned char)tmp;
3378c2ecf20Sopenharmony_ci	spin_lock_irqsave(&event_lock, flags);
3388c2ecf20Sopenharmony_ci	SET_PORT_BITS(TLCLK_REG1, 0xef, val);
3398c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&event_lock, flags);
3408c2ecf20Sopenharmony_ci
3418c2ecf20Sopenharmony_ci	return strnlen(buf, count);
3428c2ecf20Sopenharmony_ci}
3438c2ecf20Sopenharmony_ci
3448c2ecf20Sopenharmony_cistatic DEVICE_ATTR(received_ref_clk3a, (S_IWUSR|S_IWGRP), NULL,
3458c2ecf20Sopenharmony_ci		store_received_ref_clk3a);
3468c2ecf20Sopenharmony_ci
3478c2ecf20Sopenharmony_ci
3488c2ecf20Sopenharmony_cistatic ssize_t store_received_ref_clk3b(struct device *d,
3498c2ecf20Sopenharmony_ci		 struct device_attribute *attr, const char *buf, size_t count)
3508c2ecf20Sopenharmony_ci{
3518c2ecf20Sopenharmony_ci	unsigned long tmp;
3528c2ecf20Sopenharmony_ci	unsigned char val;
3538c2ecf20Sopenharmony_ci	unsigned long flags;
3548c2ecf20Sopenharmony_ci
3558c2ecf20Sopenharmony_ci	sscanf(buf, "%lX", &tmp);
3568c2ecf20Sopenharmony_ci	dev_dbg(d, ": tmp = 0x%lX\n", tmp);
3578c2ecf20Sopenharmony_ci
3588c2ecf20Sopenharmony_ci	val = (unsigned char)tmp;
3598c2ecf20Sopenharmony_ci	spin_lock_irqsave(&event_lock, flags);
3608c2ecf20Sopenharmony_ci	SET_PORT_BITS(TLCLK_REG1, 0xdf, val << 1);
3618c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&event_lock, flags);
3628c2ecf20Sopenharmony_ci
3638c2ecf20Sopenharmony_ci	return strnlen(buf, count);
3648c2ecf20Sopenharmony_ci}
3658c2ecf20Sopenharmony_ci
3668c2ecf20Sopenharmony_cistatic DEVICE_ATTR(received_ref_clk3b, (S_IWUSR|S_IWGRP), NULL,
3678c2ecf20Sopenharmony_ci		store_received_ref_clk3b);
3688c2ecf20Sopenharmony_ci
3698c2ecf20Sopenharmony_ci
3708c2ecf20Sopenharmony_cistatic ssize_t store_enable_clk3b_output(struct device *d,
3718c2ecf20Sopenharmony_ci		 struct device_attribute *attr, const char *buf, size_t count)
3728c2ecf20Sopenharmony_ci{
3738c2ecf20Sopenharmony_ci	unsigned long tmp;
3748c2ecf20Sopenharmony_ci	unsigned char val;
3758c2ecf20Sopenharmony_ci	unsigned long flags;
3768c2ecf20Sopenharmony_ci
3778c2ecf20Sopenharmony_ci	sscanf(buf, "%lX", &tmp);
3788c2ecf20Sopenharmony_ci	dev_dbg(d, ": tmp = 0x%lX\n", tmp);
3798c2ecf20Sopenharmony_ci
3808c2ecf20Sopenharmony_ci	val = (unsigned char)tmp;
3818c2ecf20Sopenharmony_ci	spin_lock_irqsave(&event_lock, flags);
3828c2ecf20Sopenharmony_ci	SET_PORT_BITS(TLCLK_REG3, 0x7f, val << 7);
3838c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&event_lock, flags);
3848c2ecf20Sopenharmony_ci
3858c2ecf20Sopenharmony_ci	return strnlen(buf, count);
3868c2ecf20Sopenharmony_ci}
3878c2ecf20Sopenharmony_ci
3888c2ecf20Sopenharmony_cistatic DEVICE_ATTR(enable_clk3b_output, (S_IWUSR|S_IWGRP), NULL,
3898c2ecf20Sopenharmony_ci		store_enable_clk3b_output);
3908c2ecf20Sopenharmony_ci
3918c2ecf20Sopenharmony_cistatic ssize_t store_enable_clk3a_output(struct device *d,
3928c2ecf20Sopenharmony_ci		 struct device_attribute *attr, const char *buf, size_t count)
3938c2ecf20Sopenharmony_ci{
3948c2ecf20Sopenharmony_ci	unsigned long flags;
3958c2ecf20Sopenharmony_ci	unsigned long tmp;
3968c2ecf20Sopenharmony_ci	unsigned char val;
3978c2ecf20Sopenharmony_ci
3988c2ecf20Sopenharmony_ci	sscanf(buf, "%lX", &tmp);
3998c2ecf20Sopenharmony_ci	dev_dbg(d, "tmp = 0x%lX\n", tmp);
4008c2ecf20Sopenharmony_ci
4018c2ecf20Sopenharmony_ci	val = (unsigned char)tmp;
4028c2ecf20Sopenharmony_ci	spin_lock_irqsave(&event_lock, flags);
4038c2ecf20Sopenharmony_ci	SET_PORT_BITS(TLCLK_REG3, 0xbf, val << 6);
4048c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&event_lock, flags);
4058c2ecf20Sopenharmony_ci
4068c2ecf20Sopenharmony_ci	return strnlen(buf, count);
4078c2ecf20Sopenharmony_ci}
4088c2ecf20Sopenharmony_ci
4098c2ecf20Sopenharmony_cistatic DEVICE_ATTR(enable_clk3a_output, (S_IWUSR|S_IWGRP), NULL,
4108c2ecf20Sopenharmony_ci		store_enable_clk3a_output);
4118c2ecf20Sopenharmony_ci
4128c2ecf20Sopenharmony_cistatic ssize_t store_enable_clkb1_output(struct device *d,
4138c2ecf20Sopenharmony_ci		 struct device_attribute *attr, const char *buf, size_t count)
4148c2ecf20Sopenharmony_ci{
4158c2ecf20Sopenharmony_ci	unsigned long flags;
4168c2ecf20Sopenharmony_ci	unsigned long tmp;
4178c2ecf20Sopenharmony_ci	unsigned char val;
4188c2ecf20Sopenharmony_ci
4198c2ecf20Sopenharmony_ci	sscanf(buf, "%lX", &tmp);
4208c2ecf20Sopenharmony_ci	dev_dbg(d, "tmp = 0x%lX\n", tmp);
4218c2ecf20Sopenharmony_ci
4228c2ecf20Sopenharmony_ci	val = (unsigned char)tmp;
4238c2ecf20Sopenharmony_ci	spin_lock_irqsave(&event_lock, flags);
4248c2ecf20Sopenharmony_ci	SET_PORT_BITS(TLCLK_REG2, 0xf7, val << 3);
4258c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&event_lock, flags);
4268c2ecf20Sopenharmony_ci
4278c2ecf20Sopenharmony_ci	return strnlen(buf, count);
4288c2ecf20Sopenharmony_ci}
4298c2ecf20Sopenharmony_ci
4308c2ecf20Sopenharmony_cistatic DEVICE_ATTR(enable_clkb1_output, (S_IWUSR|S_IWGRP), NULL,
4318c2ecf20Sopenharmony_ci		store_enable_clkb1_output);
4328c2ecf20Sopenharmony_ci
4338c2ecf20Sopenharmony_ci
4348c2ecf20Sopenharmony_cistatic ssize_t store_enable_clka1_output(struct device *d,
4358c2ecf20Sopenharmony_ci		 struct device_attribute *attr, const char *buf, size_t count)
4368c2ecf20Sopenharmony_ci{
4378c2ecf20Sopenharmony_ci	unsigned long flags;
4388c2ecf20Sopenharmony_ci	unsigned long tmp;
4398c2ecf20Sopenharmony_ci	unsigned char val;
4408c2ecf20Sopenharmony_ci
4418c2ecf20Sopenharmony_ci	sscanf(buf, "%lX", &tmp);
4428c2ecf20Sopenharmony_ci	dev_dbg(d, "tmp = 0x%lX\n", tmp);
4438c2ecf20Sopenharmony_ci
4448c2ecf20Sopenharmony_ci	val = (unsigned char)tmp;
4458c2ecf20Sopenharmony_ci	spin_lock_irqsave(&event_lock, flags);
4468c2ecf20Sopenharmony_ci	SET_PORT_BITS(TLCLK_REG2, 0xfb, val << 2);
4478c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&event_lock, flags);
4488c2ecf20Sopenharmony_ci
4498c2ecf20Sopenharmony_ci	return strnlen(buf, count);
4508c2ecf20Sopenharmony_ci}
4518c2ecf20Sopenharmony_ci
4528c2ecf20Sopenharmony_cistatic DEVICE_ATTR(enable_clka1_output, (S_IWUSR|S_IWGRP), NULL,
4538c2ecf20Sopenharmony_ci		store_enable_clka1_output);
4548c2ecf20Sopenharmony_ci
4558c2ecf20Sopenharmony_cistatic ssize_t store_enable_clkb0_output(struct device *d,
4568c2ecf20Sopenharmony_ci		 struct device_attribute *attr, const char *buf, size_t count)
4578c2ecf20Sopenharmony_ci{
4588c2ecf20Sopenharmony_ci	unsigned long flags;
4598c2ecf20Sopenharmony_ci	unsigned long tmp;
4608c2ecf20Sopenharmony_ci	unsigned char val;
4618c2ecf20Sopenharmony_ci
4628c2ecf20Sopenharmony_ci	sscanf(buf, "%lX", &tmp);
4638c2ecf20Sopenharmony_ci	dev_dbg(d, "tmp = 0x%lX\n", tmp);
4648c2ecf20Sopenharmony_ci
4658c2ecf20Sopenharmony_ci	val = (unsigned char)tmp;
4668c2ecf20Sopenharmony_ci	spin_lock_irqsave(&event_lock, flags);
4678c2ecf20Sopenharmony_ci	SET_PORT_BITS(TLCLK_REG2, 0xfd, val << 1);
4688c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&event_lock, flags);
4698c2ecf20Sopenharmony_ci
4708c2ecf20Sopenharmony_ci	return strnlen(buf, count);
4718c2ecf20Sopenharmony_ci}
4728c2ecf20Sopenharmony_ci
4738c2ecf20Sopenharmony_cistatic DEVICE_ATTR(enable_clkb0_output, (S_IWUSR|S_IWGRP), NULL,
4748c2ecf20Sopenharmony_ci		store_enable_clkb0_output);
4758c2ecf20Sopenharmony_ci
4768c2ecf20Sopenharmony_cistatic ssize_t store_enable_clka0_output(struct device *d,
4778c2ecf20Sopenharmony_ci		 struct device_attribute *attr, const char *buf, size_t count)
4788c2ecf20Sopenharmony_ci{
4798c2ecf20Sopenharmony_ci	unsigned long flags;
4808c2ecf20Sopenharmony_ci	unsigned long tmp;
4818c2ecf20Sopenharmony_ci	unsigned char val;
4828c2ecf20Sopenharmony_ci
4838c2ecf20Sopenharmony_ci	sscanf(buf, "%lX", &tmp);
4848c2ecf20Sopenharmony_ci	dev_dbg(d, "tmp = 0x%lX\n", tmp);
4858c2ecf20Sopenharmony_ci
4868c2ecf20Sopenharmony_ci	val = (unsigned char)tmp;
4878c2ecf20Sopenharmony_ci	spin_lock_irqsave(&event_lock, flags);
4888c2ecf20Sopenharmony_ci	SET_PORT_BITS(TLCLK_REG2, 0xfe, val);
4898c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&event_lock, flags);
4908c2ecf20Sopenharmony_ci
4918c2ecf20Sopenharmony_ci	return strnlen(buf, count);
4928c2ecf20Sopenharmony_ci}
4938c2ecf20Sopenharmony_ci
4948c2ecf20Sopenharmony_cistatic DEVICE_ATTR(enable_clka0_output, (S_IWUSR|S_IWGRP), NULL,
4958c2ecf20Sopenharmony_ci		store_enable_clka0_output);
4968c2ecf20Sopenharmony_ci
4978c2ecf20Sopenharmony_cistatic ssize_t store_select_amcb2_transmit_clock(struct device *d,
4988c2ecf20Sopenharmony_ci		struct device_attribute *attr, const char *buf, size_t count)
4998c2ecf20Sopenharmony_ci{
5008c2ecf20Sopenharmony_ci	unsigned long flags;
5018c2ecf20Sopenharmony_ci	unsigned long tmp;
5028c2ecf20Sopenharmony_ci	unsigned char val;
5038c2ecf20Sopenharmony_ci
5048c2ecf20Sopenharmony_ci	sscanf(buf, "%lX", &tmp);
5058c2ecf20Sopenharmony_ci	dev_dbg(d, "tmp = 0x%lX\n", tmp);
5068c2ecf20Sopenharmony_ci
5078c2ecf20Sopenharmony_ci	val = (unsigned char)tmp;
5088c2ecf20Sopenharmony_ci	spin_lock_irqsave(&event_lock, flags);
5098c2ecf20Sopenharmony_ci	if ((val == CLK_8kHz) || (val == CLK_16_384MHz)) {
5108c2ecf20Sopenharmony_ci		SET_PORT_BITS(TLCLK_REG3, 0xc7, 0x28);
5118c2ecf20Sopenharmony_ci		SET_PORT_BITS(TLCLK_REG1, 0xfb, ~val);
5128c2ecf20Sopenharmony_ci	} else if (val >= CLK_8_592MHz) {
5138c2ecf20Sopenharmony_ci		SET_PORT_BITS(TLCLK_REG3, 0xc7, 0x38);
5148c2ecf20Sopenharmony_ci		switch (val) {
5158c2ecf20Sopenharmony_ci		case CLK_8_592MHz:
5168c2ecf20Sopenharmony_ci			SET_PORT_BITS(TLCLK_REG0, 0xfc, 2);
5178c2ecf20Sopenharmony_ci			break;
5188c2ecf20Sopenharmony_ci		case CLK_11_184MHz:
5198c2ecf20Sopenharmony_ci			SET_PORT_BITS(TLCLK_REG0, 0xfc, 0);
5208c2ecf20Sopenharmony_ci			break;
5218c2ecf20Sopenharmony_ci		case CLK_34_368MHz:
5228c2ecf20Sopenharmony_ci			SET_PORT_BITS(TLCLK_REG0, 0xfc, 3);
5238c2ecf20Sopenharmony_ci			break;
5248c2ecf20Sopenharmony_ci		case CLK_44_736MHz:
5258c2ecf20Sopenharmony_ci			SET_PORT_BITS(TLCLK_REG0, 0xfc, 1);
5268c2ecf20Sopenharmony_ci			break;
5278c2ecf20Sopenharmony_ci		}
5288c2ecf20Sopenharmony_ci	} else {
5298c2ecf20Sopenharmony_ci		SET_PORT_BITS(TLCLK_REG3, 0xc7, val << 3);
5308c2ecf20Sopenharmony_ci	}
5318c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&event_lock, flags);
5328c2ecf20Sopenharmony_ci
5338c2ecf20Sopenharmony_ci	return strnlen(buf, count);
5348c2ecf20Sopenharmony_ci}
5358c2ecf20Sopenharmony_ci
5368c2ecf20Sopenharmony_cistatic DEVICE_ATTR(select_amcb2_transmit_clock, (S_IWUSR|S_IWGRP), NULL,
5378c2ecf20Sopenharmony_ci	store_select_amcb2_transmit_clock);
5388c2ecf20Sopenharmony_ci
5398c2ecf20Sopenharmony_cistatic ssize_t store_select_amcb1_transmit_clock(struct device *d,
5408c2ecf20Sopenharmony_ci		 struct device_attribute *attr, const char *buf, size_t count)
5418c2ecf20Sopenharmony_ci{
5428c2ecf20Sopenharmony_ci	unsigned long tmp;
5438c2ecf20Sopenharmony_ci	unsigned char val;
5448c2ecf20Sopenharmony_ci	unsigned long flags;
5458c2ecf20Sopenharmony_ci
5468c2ecf20Sopenharmony_ci	sscanf(buf, "%lX", &tmp);
5478c2ecf20Sopenharmony_ci	dev_dbg(d, "tmp = 0x%lX\n", tmp);
5488c2ecf20Sopenharmony_ci
5498c2ecf20Sopenharmony_ci	val = (unsigned char)tmp;
5508c2ecf20Sopenharmony_ci	spin_lock_irqsave(&event_lock, flags);
5518c2ecf20Sopenharmony_ci	if ((val == CLK_8kHz) || (val == CLK_16_384MHz)) {
5528c2ecf20Sopenharmony_ci		SET_PORT_BITS(TLCLK_REG3, 0xf8, 0x5);
5538c2ecf20Sopenharmony_ci		SET_PORT_BITS(TLCLK_REG1, 0xfb, ~val);
5548c2ecf20Sopenharmony_ci	} else if (val >= CLK_8_592MHz) {
5558c2ecf20Sopenharmony_ci		SET_PORT_BITS(TLCLK_REG3, 0xf8, 0x7);
5568c2ecf20Sopenharmony_ci		switch (val) {
5578c2ecf20Sopenharmony_ci		case CLK_8_592MHz:
5588c2ecf20Sopenharmony_ci			SET_PORT_BITS(TLCLK_REG0, 0xfc, 2);
5598c2ecf20Sopenharmony_ci			break;
5608c2ecf20Sopenharmony_ci		case CLK_11_184MHz:
5618c2ecf20Sopenharmony_ci			SET_PORT_BITS(TLCLK_REG0, 0xfc, 0);
5628c2ecf20Sopenharmony_ci			break;
5638c2ecf20Sopenharmony_ci		case CLK_34_368MHz:
5648c2ecf20Sopenharmony_ci			SET_PORT_BITS(TLCLK_REG0, 0xfc, 3);
5658c2ecf20Sopenharmony_ci			break;
5668c2ecf20Sopenharmony_ci		case CLK_44_736MHz:
5678c2ecf20Sopenharmony_ci			SET_PORT_BITS(TLCLK_REG0, 0xfc, 1);
5688c2ecf20Sopenharmony_ci			break;
5698c2ecf20Sopenharmony_ci		}
5708c2ecf20Sopenharmony_ci	} else {
5718c2ecf20Sopenharmony_ci		SET_PORT_BITS(TLCLK_REG3, 0xf8, val);
5728c2ecf20Sopenharmony_ci	}
5738c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&event_lock, flags);
5748c2ecf20Sopenharmony_ci
5758c2ecf20Sopenharmony_ci	return strnlen(buf, count);
5768c2ecf20Sopenharmony_ci}
5778c2ecf20Sopenharmony_ci
5788c2ecf20Sopenharmony_cistatic DEVICE_ATTR(select_amcb1_transmit_clock, (S_IWUSR|S_IWGRP), NULL,
5798c2ecf20Sopenharmony_ci		store_select_amcb1_transmit_clock);
5808c2ecf20Sopenharmony_ci
5818c2ecf20Sopenharmony_cistatic ssize_t store_select_redundant_clock(struct device *d,
5828c2ecf20Sopenharmony_ci		 struct device_attribute *attr, const char *buf, size_t count)
5838c2ecf20Sopenharmony_ci{
5848c2ecf20Sopenharmony_ci	unsigned long tmp;
5858c2ecf20Sopenharmony_ci	unsigned char val;
5868c2ecf20Sopenharmony_ci	unsigned long flags;
5878c2ecf20Sopenharmony_ci
5888c2ecf20Sopenharmony_ci	sscanf(buf, "%lX", &tmp);
5898c2ecf20Sopenharmony_ci	dev_dbg(d, "tmp = 0x%lX\n", tmp);
5908c2ecf20Sopenharmony_ci
5918c2ecf20Sopenharmony_ci	val = (unsigned char)tmp;
5928c2ecf20Sopenharmony_ci	spin_lock_irqsave(&event_lock, flags);
5938c2ecf20Sopenharmony_ci	SET_PORT_BITS(TLCLK_REG1, 0xfe, val);
5948c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&event_lock, flags);
5958c2ecf20Sopenharmony_ci
5968c2ecf20Sopenharmony_ci	return strnlen(buf, count);
5978c2ecf20Sopenharmony_ci}
5988c2ecf20Sopenharmony_ci
5998c2ecf20Sopenharmony_cistatic DEVICE_ATTR(select_redundant_clock, (S_IWUSR|S_IWGRP), NULL,
6008c2ecf20Sopenharmony_ci		store_select_redundant_clock);
6018c2ecf20Sopenharmony_ci
6028c2ecf20Sopenharmony_cistatic ssize_t store_select_ref_frequency(struct device *d,
6038c2ecf20Sopenharmony_ci		 struct device_attribute *attr, const char *buf, size_t count)
6048c2ecf20Sopenharmony_ci{
6058c2ecf20Sopenharmony_ci	unsigned long tmp;
6068c2ecf20Sopenharmony_ci	unsigned char val;
6078c2ecf20Sopenharmony_ci	unsigned long flags;
6088c2ecf20Sopenharmony_ci
6098c2ecf20Sopenharmony_ci	sscanf(buf, "%lX", &tmp);
6108c2ecf20Sopenharmony_ci	dev_dbg(d, "tmp = 0x%lX\n", tmp);
6118c2ecf20Sopenharmony_ci
6128c2ecf20Sopenharmony_ci	val = (unsigned char)tmp;
6138c2ecf20Sopenharmony_ci	spin_lock_irqsave(&event_lock, flags);
6148c2ecf20Sopenharmony_ci	SET_PORT_BITS(TLCLK_REG1, 0xfd, val);
6158c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&event_lock, flags);
6168c2ecf20Sopenharmony_ci
6178c2ecf20Sopenharmony_ci	return strnlen(buf, count);
6188c2ecf20Sopenharmony_ci}
6198c2ecf20Sopenharmony_ci
6208c2ecf20Sopenharmony_cistatic DEVICE_ATTR(select_ref_frequency, (S_IWUSR|S_IWGRP), NULL,
6218c2ecf20Sopenharmony_ci		store_select_ref_frequency);
6228c2ecf20Sopenharmony_ci
6238c2ecf20Sopenharmony_cistatic ssize_t store_filter_select(struct device *d,
6248c2ecf20Sopenharmony_ci		 struct device_attribute *attr, const char *buf, size_t count)
6258c2ecf20Sopenharmony_ci{
6268c2ecf20Sopenharmony_ci	unsigned long tmp;
6278c2ecf20Sopenharmony_ci	unsigned char val;
6288c2ecf20Sopenharmony_ci	unsigned long flags;
6298c2ecf20Sopenharmony_ci
6308c2ecf20Sopenharmony_ci	sscanf(buf, "%lX", &tmp);
6318c2ecf20Sopenharmony_ci	dev_dbg(d, "tmp = 0x%lX\n", tmp);
6328c2ecf20Sopenharmony_ci
6338c2ecf20Sopenharmony_ci	val = (unsigned char)tmp;
6348c2ecf20Sopenharmony_ci	spin_lock_irqsave(&event_lock, flags);
6358c2ecf20Sopenharmony_ci	SET_PORT_BITS(TLCLK_REG0, 0xfb, val);
6368c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&event_lock, flags);
6378c2ecf20Sopenharmony_ci
6388c2ecf20Sopenharmony_ci	return strnlen(buf, count);
6398c2ecf20Sopenharmony_ci}
6408c2ecf20Sopenharmony_ci
6418c2ecf20Sopenharmony_cistatic DEVICE_ATTR(filter_select, (S_IWUSR|S_IWGRP), NULL, store_filter_select);
6428c2ecf20Sopenharmony_ci
6438c2ecf20Sopenharmony_cistatic ssize_t store_hardware_switching_mode(struct device *d,
6448c2ecf20Sopenharmony_ci		 struct device_attribute *attr, const char *buf, size_t count)
6458c2ecf20Sopenharmony_ci{
6468c2ecf20Sopenharmony_ci	unsigned long tmp;
6478c2ecf20Sopenharmony_ci	unsigned char val;
6488c2ecf20Sopenharmony_ci	unsigned long flags;
6498c2ecf20Sopenharmony_ci
6508c2ecf20Sopenharmony_ci	sscanf(buf, "%lX", &tmp);
6518c2ecf20Sopenharmony_ci	dev_dbg(d, "tmp = 0x%lX\n", tmp);
6528c2ecf20Sopenharmony_ci
6538c2ecf20Sopenharmony_ci	val = (unsigned char)tmp;
6548c2ecf20Sopenharmony_ci	spin_lock_irqsave(&event_lock, flags);
6558c2ecf20Sopenharmony_ci	SET_PORT_BITS(TLCLK_REG0, 0xbf, val);
6568c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&event_lock, flags);
6578c2ecf20Sopenharmony_ci
6588c2ecf20Sopenharmony_ci	return strnlen(buf, count);
6598c2ecf20Sopenharmony_ci}
6608c2ecf20Sopenharmony_ci
6618c2ecf20Sopenharmony_cistatic DEVICE_ATTR(hardware_switching_mode, (S_IWUSR|S_IWGRP), NULL,
6628c2ecf20Sopenharmony_ci		store_hardware_switching_mode);
6638c2ecf20Sopenharmony_ci
6648c2ecf20Sopenharmony_cistatic ssize_t store_hardware_switching(struct device *d,
6658c2ecf20Sopenharmony_ci		 struct device_attribute *attr, const char *buf, size_t count)
6668c2ecf20Sopenharmony_ci{
6678c2ecf20Sopenharmony_ci	unsigned long tmp;
6688c2ecf20Sopenharmony_ci	unsigned char val;
6698c2ecf20Sopenharmony_ci	unsigned long flags;
6708c2ecf20Sopenharmony_ci
6718c2ecf20Sopenharmony_ci	sscanf(buf, "%lX", &tmp);
6728c2ecf20Sopenharmony_ci	dev_dbg(d, "tmp = 0x%lX\n", tmp);
6738c2ecf20Sopenharmony_ci
6748c2ecf20Sopenharmony_ci	val = (unsigned char)tmp;
6758c2ecf20Sopenharmony_ci	spin_lock_irqsave(&event_lock, flags);
6768c2ecf20Sopenharmony_ci	SET_PORT_BITS(TLCLK_REG0, 0x7f, val);
6778c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&event_lock, flags);
6788c2ecf20Sopenharmony_ci
6798c2ecf20Sopenharmony_ci	return strnlen(buf, count);
6808c2ecf20Sopenharmony_ci}
6818c2ecf20Sopenharmony_ci
6828c2ecf20Sopenharmony_cistatic DEVICE_ATTR(hardware_switching, (S_IWUSR|S_IWGRP), NULL,
6838c2ecf20Sopenharmony_ci		store_hardware_switching);
6848c2ecf20Sopenharmony_ci
6858c2ecf20Sopenharmony_cistatic ssize_t store_refalign (struct device *d,
6868c2ecf20Sopenharmony_ci		 struct device_attribute *attr, const char *buf, size_t count)
6878c2ecf20Sopenharmony_ci{
6888c2ecf20Sopenharmony_ci	unsigned long tmp;
6898c2ecf20Sopenharmony_ci	unsigned long flags;
6908c2ecf20Sopenharmony_ci
6918c2ecf20Sopenharmony_ci	sscanf(buf, "%lX", &tmp);
6928c2ecf20Sopenharmony_ci	dev_dbg(d, "tmp = 0x%lX\n", tmp);
6938c2ecf20Sopenharmony_ci	spin_lock_irqsave(&event_lock, flags);
6948c2ecf20Sopenharmony_ci	SET_PORT_BITS(TLCLK_REG0, 0xf7, 0);
6958c2ecf20Sopenharmony_ci	SET_PORT_BITS(TLCLK_REG0, 0xf7, 0x08);
6968c2ecf20Sopenharmony_ci	SET_PORT_BITS(TLCLK_REG0, 0xf7, 0);
6978c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&event_lock, flags);
6988c2ecf20Sopenharmony_ci
6998c2ecf20Sopenharmony_ci	return strnlen(buf, count);
7008c2ecf20Sopenharmony_ci}
7018c2ecf20Sopenharmony_ci
7028c2ecf20Sopenharmony_cistatic DEVICE_ATTR(refalign, (S_IWUSR|S_IWGRP), NULL, store_refalign);
7038c2ecf20Sopenharmony_ci
7048c2ecf20Sopenharmony_cistatic ssize_t store_mode_select (struct device *d,
7058c2ecf20Sopenharmony_ci		 struct device_attribute *attr, const char *buf, size_t count)
7068c2ecf20Sopenharmony_ci{
7078c2ecf20Sopenharmony_ci	unsigned long tmp;
7088c2ecf20Sopenharmony_ci	unsigned char val;
7098c2ecf20Sopenharmony_ci	unsigned long flags;
7108c2ecf20Sopenharmony_ci
7118c2ecf20Sopenharmony_ci	sscanf(buf, "%lX", &tmp);
7128c2ecf20Sopenharmony_ci	dev_dbg(d, "tmp = 0x%lX\n", tmp);
7138c2ecf20Sopenharmony_ci
7148c2ecf20Sopenharmony_ci	val = (unsigned char)tmp;
7158c2ecf20Sopenharmony_ci	spin_lock_irqsave(&event_lock, flags);
7168c2ecf20Sopenharmony_ci	SET_PORT_BITS(TLCLK_REG0, 0xcf, val);
7178c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&event_lock, flags);
7188c2ecf20Sopenharmony_ci
7198c2ecf20Sopenharmony_ci	return strnlen(buf, count);
7208c2ecf20Sopenharmony_ci}
7218c2ecf20Sopenharmony_ci
7228c2ecf20Sopenharmony_cistatic DEVICE_ATTR(mode_select, (S_IWUSR|S_IWGRP), NULL, store_mode_select);
7238c2ecf20Sopenharmony_ci
7248c2ecf20Sopenharmony_cistatic ssize_t store_reset (struct device *d,
7258c2ecf20Sopenharmony_ci		 struct device_attribute *attr, const char *buf, size_t count)
7268c2ecf20Sopenharmony_ci{
7278c2ecf20Sopenharmony_ci	unsigned long tmp;
7288c2ecf20Sopenharmony_ci	unsigned char val;
7298c2ecf20Sopenharmony_ci	unsigned long flags;
7308c2ecf20Sopenharmony_ci
7318c2ecf20Sopenharmony_ci	sscanf(buf, "%lX", &tmp);
7328c2ecf20Sopenharmony_ci	dev_dbg(d, "tmp = 0x%lX\n", tmp);
7338c2ecf20Sopenharmony_ci
7348c2ecf20Sopenharmony_ci	val = (unsigned char)tmp;
7358c2ecf20Sopenharmony_ci	spin_lock_irqsave(&event_lock, flags);
7368c2ecf20Sopenharmony_ci	SET_PORT_BITS(TLCLK_REG4, 0xfd, val);
7378c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&event_lock, flags);
7388c2ecf20Sopenharmony_ci
7398c2ecf20Sopenharmony_ci	return strnlen(buf, count);
7408c2ecf20Sopenharmony_ci}
7418c2ecf20Sopenharmony_ci
7428c2ecf20Sopenharmony_cistatic DEVICE_ATTR(reset, (S_IWUSR|S_IWGRP), NULL, store_reset);
7438c2ecf20Sopenharmony_ci
7448c2ecf20Sopenharmony_cistatic struct attribute *tlclk_sysfs_entries[] = {
7458c2ecf20Sopenharmony_ci	&dev_attr_current_ref.attr,
7468c2ecf20Sopenharmony_ci	&dev_attr_telclock_version.attr,
7478c2ecf20Sopenharmony_ci	&dev_attr_alarms.attr,
7488c2ecf20Sopenharmony_ci	&dev_attr_received_ref_clk3a.attr,
7498c2ecf20Sopenharmony_ci	&dev_attr_received_ref_clk3b.attr,
7508c2ecf20Sopenharmony_ci	&dev_attr_enable_clk3a_output.attr,
7518c2ecf20Sopenharmony_ci	&dev_attr_enable_clk3b_output.attr,
7528c2ecf20Sopenharmony_ci	&dev_attr_enable_clkb1_output.attr,
7538c2ecf20Sopenharmony_ci	&dev_attr_enable_clka1_output.attr,
7548c2ecf20Sopenharmony_ci	&dev_attr_enable_clkb0_output.attr,
7558c2ecf20Sopenharmony_ci	&dev_attr_enable_clka0_output.attr,
7568c2ecf20Sopenharmony_ci	&dev_attr_select_amcb1_transmit_clock.attr,
7578c2ecf20Sopenharmony_ci	&dev_attr_select_amcb2_transmit_clock.attr,
7588c2ecf20Sopenharmony_ci	&dev_attr_select_redundant_clock.attr,
7598c2ecf20Sopenharmony_ci	&dev_attr_select_ref_frequency.attr,
7608c2ecf20Sopenharmony_ci	&dev_attr_filter_select.attr,
7618c2ecf20Sopenharmony_ci	&dev_attr_hardware_switching_mode.attr,
7628c2ecf20Sopenharmony_ci	&dev_attr_hardware_switching.attr,
7638c2ecf20Sopenharmony_ci	&dev_attr_refalign.attr,
7648c2ecf20Sopenharmony_ci	&dev_attr_mode_select.attr,
7658c2ecf20Sopenharmony_ci	&dev_attr_reset.attr,
7668c2ecf20Sopenharmony_ci	NULL
7678c2ecf20Sopenharmony_ci};
7688c2ecf20Sopenharmony_ci
7698c2ecf20Sopenharmony_cistatic const struct attribute_group tlclk_attribute_group = {
7708c2ecf20Sopenharmony_ci	.name = NULL,		/* put in device directory */
7718c2ecf20Sopenharmony_ci	.attrs = tlclk_sysfs_entries,
7728c2ecf20Sopenharmony_ci};
7738c2ecf20Sopenharmony_ci
7748c2ecf20Sopenharmony_cistatic struct platform_device *tlclk_device;
7758c2ecf20Sopenharmony_ci
7768c2ecf20Sopenharmony_cistatic int __init tlclk_init(void)
7778c2ecf20Sopenharmony_ci{
7788c2ecf20Sopenharmony_ci	int ret;
7798c2ecf20Sopenharmony_ci
7808c2ecf20Sopenharmony_ci	telclk_interrupt = (inb(TLCLK_REG7) & 0x0f);
7818c2ecf20Sopenharmony_ci
7828c2ecf20Sopenharmony_ci	alarm_events = kzalloc( sizeof(struct tlclk_alarms), GFP_KERNEL);
7838c2ecf20Sopenharmony_ci	if (!alarm_events) {
7848c2ecf20Sopenharmony_ci		ret = -ENOMEM;
7858c2ecf20Sopenharmony_ci		goto out1;
7868c2ecf20Sopenharmony_ci	}
7878c2ecf20Sopenharmony_ci
7888c2ecf20Sopenharmony_ci	ret = register_chrdev(tlclk_major, "telco_clock", &tlclk_fops);
7898c2ecf20Sopenharmony_ci	if (ret < 0) {
7908c2ecf20Sopenharmony_ci		printk(KERN_ERR "tlclk: can't get major %d.\n", tlclk_major);
7918c2ecf20Sopenharmony_ci		kfree(alarm_events);
7928c2ecf20Sopenharmony_ci		return ret;
7938c2ecf20Sopenharmony_ci	}
7948c2ecf20Sopenharmony_ci	tlclk_major = ret;
7958c2ecf20Sopenharmony_ci
7968c2ecf20Sopenharmony_ci	/* Read telecom clock IRQ number (Set by BIOS) */
7978c2ecf20Sopenharmony_ci	if (!request_region(TLCLK_BASE, 8, "telco_clock")) {
7988c2ecf20Sopenharmony_ci		printk(KERN_ERR "tlclk: request_region 0x%X failed.\n",
7998c2ecf20Sopenharmony_ci			TLCLK_BASE);
8008c2ecf20Sopenharmony_ci		ret = -EBUSY;
8018c2ecf20Sopenharmony_ci		goto out2;
8028c2ecf20Sopenharmony_ci	}
8038c2ecf20Sopenharmony_ci
8048c2ecf20Sopenharmony_ci	if (0x0F == telclk_interrupt ) { /* not MCPBL0010 ? */
8058c2ecf20Sopenharmony_ci		printk(KERN_ERR "telclk_interrupt = 0x%x non-mcpbl0010 hw.\n",
8068c2ecf20Sopenharmony_ci			telclk_interrupt);
8078c2ecf20Sopenharmony_ci		ret = -ENXIO;
8088c2ecf20Sopenharmony_ci		goto out3;
8098c2ecf20Sopenharmony_ci	}
8108c2ecf20Sopenharmony_ci
8118c2ecf20Sopenharmony_ci	timer_setup(&switchover_timer, switchover_timeout, 0);
8128c2ecf20Sopenharmony_ci
8138c2ecf20Sopenharmony_ci	ret = misc_register(&tlclk_miscdev);
8148c2ecf20Sopenharmony_ci	if (ret < 0) {
8158c2ecf20Sopenharmony_ci		printk(KERN_ERR "tlclk: misc_register returns %d.\n", ret);
8168c2ecf20Sopenharmony_ci		goto out3;
8178c2ecf20Sopenharmony_ci	}
8188c2ecf20Sopenharmony_ci
8198c2ecf20Sopenharmony_ci	tlclk_device = platform_device_register_simple("telco_clock",
8208c2ecf20Sopenharmony_ci				-1, NULL, 0);
8218c2ecf20Sopenharmony_ci	if (IS_ERR(tlclk_device)) {
8228c2ecf20Sopenharmony_ci		printk(KERN_ERR "tlclk: platform_device_register failed.\n");
8238c2ecf20Sopenharmony_ci		ret = PTR_ERR(tlclk_device);
8248c2ecf20Sopenharmony_ci		goto out4;
8258c2ecf20Sopenharmony_ci	}
8268c2ecf20Sopenharmony_ci
8278c2ecf20Sopenharmony_ci	ret = sysfs_create_group(&tlclk_device->dev.kobj,
8288c2ecf20Sopenharmony_ci			&tlclk_attribute_group);
8298c2ecf20Sopenharmony_ci	if (ret) {
8308c2ecf20Sopenharmony_ci		printk(KERN_ERR "tlclk: failed to create sysfs device attributes.\n");
8318c2ecf20Sopenharmony_ci		goto out5;
8328c2ecf20Sopenharmony_ci	}
8338c2ecf20Sopenharmony_ci
8348c2ecf20Sopenharmony_ci	return 0;
8358c2ecf20Sopenharmony_ciout5:
8368c2ecf20Sopenharmony_ci	platform_device_unregister(tlclk_device);
8378c2ecf20Sopenharmony_ciout4:
8388c2ecf20Sopenharmony_ci	misc_deregister(&tlclk_miscdev);
8398c2ecf20Sopenharmony_ciout3:
8408c2ecf20Sopenharmony_ci	release_region(TLCLK_BASE, 8);
8418c2ecf20Sopenharmony_ciout2:
8428c2ecf20Sopenharmony_ci	kfree(alarm_events);
8438c2ecf20Sopenharmony_ci	unregister_chrdev(tlclk_major, "telco_clock");
8448c2ecf20Sopenharmony_ciout1:
8458c2ecf20Sopenharmony_ci	return ret;
8468c2ecf20Sopenharmony_ci}
8478c2ecf20Sopenharmony_ci
8488c2ecf20Sopenharmony_cistatic void __exit tlclk_cleanup(void)
8498c2ecf20Sopenharmony_ci{
8508c2ecf20Sopenharmony_ci	sysfs_remove_group(&tlclk_device->dev.kobj, &tlclk_attribute_group);
8518c2ecf20Sopenharmony_ci	platform_device_unregister(tlclk_device);
8528c2ecf20Sopenharmony_ci	misc_deregister(&tlclk_miscdev);
8538c2ecf20Sopenharmony_ci	unregister_chrdev(tlclk_major, "telco_clock");
8548c2ecf20Sopenharmony_ci
8558c2ecf20Sopenharmony_ci	release_region(TLCLK_BASE, 8);
8568c2ecf20Sopenharmony_ci	del_timer_sync(&switchover_timer);
8578c2ecf20Sopenharmony_ci	kfree(alarm_events);
8588c2ecf20Sopenharmony_ci
8598c2ecf20Sopenharmony_ci}
8608c2ecf20Sopenharmony_ci
8618c2ecf20Sopenharmony_cistatic void switchover_timeout(struct timer_list *unused)
8628c2ecf20Sopenharmony_ci{
8638c2ecf20Sopenharmony_ci	unsigned long flags = tlclk_timer_data;
8648c2ecf20Sopenharmony_ci
8658c2ecf20Sopenharmony_ci	if ((flags & 1)) {
8668c2ecf20Sopenharmony_ci		if ((inb(TLCLK_REG1) & 0x08) != (flags & 0x08))
8678c2ecf20Sopenharmony_ci			alarm_events->switchover_primary++;
8688c2ecf20Sopenharmony_ci	} else {
8698c2ecf20Sopenharmony_ci		if ((inb(TLCLK_REG1) & 0x08) != (flags & 0x08))
8708c2ecf20Sopenharmony_ci			alarm_events->switchover_secondary++;
8718c2ecf20Sopenharmony_ci	}
8728c2ecf20Sopenharmony_ci
8738c2ecf20Sopenharmony_ci	/* Alarm processing is done, wake up read task */
8748c2ecf20Sopenharmony_ci	del_timer(&switchover_timer);
8758c2ecf20Sopenharmony_ci	got_event = 1;
8768c2ecf20Sopenharmony_ci	wake_up(&wq);
8778c2ecf20Sopenharmony_ci}
8788c2ecf20Sopenharmony_ci
8798c2ecf20Sopenharmony_cistatic irqreturn_t tlclk_interrupt(int irq, void *dev_id)
8808c2ecf20Sopenharmony_ci{
8818c2ecf20Sopenharmony_ci	unsigned long flags;
8828c2ecf20Sopenharmony_ci
8838c2ecf20Sopenharmony_ci	spin_lock_irqsave(&event_lock, flags);
8848c2ecf20Sopenharmony_ci	/* Read and clear interrupt events */
8858c2ecf20Sopenharmony_ci	int_events = inb(TLCLK_REG6);
8868c2ecf20Sopenharmony_ci
8878c2ecf20Sopenharmony_ci	/* Primary_Los changed from 0 to 1 ? */
8888c2ecf20Sopenharmony_ci	if (int_events & PRI_LOS_01_MASK) {
8898c2ecf20Sopenharmony_ci		if (inb(TLCLK_REG2) & SEC_LOST_MASK)
8908c2ecf20Sopenharmony_ci			alarm_events->lost_clocks++;
8918c2ecf20Sopenharmony_ci		else
8928c2ecf20Sopenharmony_ci			alarm_events->lost_primary_clock++;
8938c2ecf20Sopenharmony_ci	}
8948c2ecf20Sopenharmony_ci
8958c2ecf20Sopenharmony_ci	/* Primary_Los changed from 1 to 0 ? */
8968c2ecf20Sopenharmony_ci	if (int_events & PRI_LOS_10_MASK) {
8978c2ecf20Sopenharmony_ci		alarm_events->primary_clock_back++;
8988c2ecf20Sopenharmony_ci		SET_PORT_BITS(TLCLK_REG1, 0xFE, 1);
8998c2ecf20Sopenharmony_ci	}
9008c2ecf20Sopenharmony_ci	/* Secondary_Los changed from 0 to 1 ? */
9018c2ecf20Sopenharmony_ci	if (int_events & SEC_LOS_01_MASK) {
9028c2ecf20Sopenharmony_ci		if (inb(TLCLK_REG2) & PRI_LOST_MASK)
9038c2ecf20Sopenharmony_ci			alarm_events->lost_clocks++;
9048c2ecf20Sopenharmony_ci		else
9058c2ecf20Sopenharmony_ci			alarm_events->lost_secondary_clock++;
9068c2ecf20Sopenharmony_ci	}
9078c2ecf20Sopenharmony_ci	/* Secondary_Los changed from 1 to 0 ? */
9088c2ecf20Sopenharmony_ci	if (int_events & SEC_LOS_10_MASK) {
9098c2ecf20Sopenharmony_ci		alarm_events->secondary_clock_back++;
9108c2ecf20Sopenharmony_ci		SET_PORT_BITS(TLCLK_REG1, 0xFE, 0);
9118c2ecf20Sopenharmony_ci	}
9128c2ecf20Sopenharmony_ci	if (int_events & HOLDOVER_10_MASK)
9138c2ecf20Sopenharmony_ci		alarm_events->pll_end_holdover++;
9148c2ecf20Sopenharmony_ci
9158c2ecf20Sopenharmony_ci	if (int_events & UNLOCK_01_MASK)
9168c2ecf20Sopenharmony_ci		alarm_events->pll_lost_sync++;
9178c2ecf20Sopenharmony_ci
9188c2ecf20Sopenharmony_ci	if (int_events & UNLOCK_10_MASK)
9198c2ecf20Sopenharmony_ci		alarm_events->pll_sync++;
9208c2ecf20Sopenharmony_ci
9218c2ecf20Sopenharmony_ci	/* Holdover changed from 0 to 1 ? */
9228c2ecf20Sopenharmony_ci	if (int_events & HOLDOVER_01_MASK) {
9238c2ecf20Sopenharmony_ci		alarm_events->pll_holdover++;
9248c2ecf20Sopenharmony_ci
9258c2ecf20Sopenharmony_ci		/* TIMEOUT in ~10ms */
9268c2ecf20Sopenharmony_ci		switchover_timer.expires = jiffies + msecs_to_jiffies(10);
9278c2ecf20Sopenharmony_ci		tlclk_timer_data = inb(TLCLK_REG1);
9288c2ecf20Sopenharmony_ci		mod_timer(&switchover_timer, switchover_timer.expires);
9298c2ecf20Sopenharmony_ci	} else {
9308c2ecf20Sopenharmony_ci		got_event = 1;
9318c2ecf20Sopenharmony_ci		wake_up(&wq);
9328c2ecf20Sopenharmony_ci	}
9338c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&event_lock, flags);
9348c2ecf20Sopenharmony_ci
9358c2ecf20Sopenharmony_ci	return IRQ_HANDLED;
9368c2ecf20Sopenharmony_ci}
9378c2ecf20Sopenharmony_ci
9388c2ecf20Sopenharmony_cimodule_init(tlclk_init);
9398c2ecf20Sopenharmony_cimodule_exit(tlclk_cleanup);
940