1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * PPS generators sysfs support
4  *
5  * Copyright (C) 2024 Rodolfo Giometti <[email protected]>
6  */
7 
8 #include <linux/device.h>
9 #include <linux/module.h>
10 #include <linux/string.h>
11 #include <linux/pps_gen_kernel.h>
12 
13 /*
14  * Attribute functions
15  */
16 
system_show(struct device * dev,struct device_attribute * attr,char * buf)17 static ssize_t system_show(struct device *dev, struct device_attribute *attr,
18 				char *buf)
19 {
20 	struct pps_gen_device *pps_gen = dev_get_drvdata(dev);
21 
22 	return sysfs_emit(buf, "%d\n", pps_gen->info.use_system_clock);
23 }
24 static DEVICE_ATTR_RO(system);
25 
time_show(struct device * dev,struct device_attribute * attr,char * buf)26 static ssize_t time_show(struct device *dev, struct device_attribute *attr,
27 				char *buf)
28 {
29 	struct pps_gen_device *pps_gen = dev_get_drvdata(dev);
30 	struct timespec64 time;
31 	int ret;
32 
33 	ret = pps_gen->info.get_time(pps_gen, &time);
34 	if (ret)
35 		return ret;
36 
37 	return sysfs_emit(buf, "%llu %09lu\n", time.tv_sec, time.tv_nsec);
38 }
39 static DEVICE_ATTR_RO(time);
40 
enable_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)41 static ssize_t enable_store(struct device *dev, struct device_attribute *attr,
42 				const char *buf, size_t count)
43 {
44 	struct pps_gen_device *pps_gen = dev_get_drvdata(dev);
45 	bool status;
46 	int ret;
47 
48 	ret = kstrtobool(buf, &status);
49 	if (ret)
50 		return ret;
51 
52 	ret = pps_gen->info.enable(pps_gen, status);
53 	if (ret)
54 		return ret;
55 	pps_gen->enabled = status;
56 
57 	return count;
58 }
59 static DEVICE_ATTR_WO(enable);
60 
61 static struct attribute *pps_gen_attrs[] = {
62 	&dev_attr_enable.attr,
63 	&dev_attr_time.attr,
64 	&dev_attr_system.attr,
65 	NULL,
66 };
67 
68 static const struct attribute_group pps_gen_group = {
69 	.attrs = pps_gen_attrs,
70 };
71 
72 const struct attribute_group *pps_gen_groups[] = {
73 	&pps_gen_group,
74 	NULL,
75 };
76