186b525beSRodolfo Giometti // SPDX-License-Identifier: GPL-2.0-or-later
286b525beSRodolfo Giometti /*
386b525beSRodolfo Giometti * PPS generators sysfs support
486b525beSRodolfo Giometti *
586b525beSRodolfo Giometti * Copyright (C) 2024 Rodolfo Giometti <[email protected]>
686b525beSRodolfo Giometti */
786b525beSRodolfo Giometti
886b525beSRodolfo Giometti #include <linux/device.h>
986b525beSRodolfo Giometti #include <linux/module.h>
1086b525beSRodolfo Giometti #include <linux/string.h>
1186b525beSRodolfo Giometti #include <linux/pps_gen_kernel.h>
1286b525beSRodolfo Giometti
1386b525beSRodolfo Giometti /*
1486b525beSRodolfo Giometti * Attribute functions
1586b525beSRodolfo Giometti */
1686b525beSRodolfo Giometti
system_show(struct device * dev,struct device_attribute * attr,char * buf)1786b525beSRodolfo Giometti static ssize_t system_show(struct device *dev, struct device_attribute *attr,
1886b525beSRodolfo Giometti char *buf)
1986b525beSRodolfo Giometti {
2086b525beSRodolfo Giometti struct pps_gen_device *pps_gen = dev_get_drvdata(dev);
2186b525beSRodolfo Giometti
22*ac9c5170SSubramanian Mohan return sysfs_emit(buf, "%d\n", pps_gen->info->use_system_clock);
2386b525beSRodolfo Giometti }
2486b525beSRodolfo Giometti static DEVICE_ATTR_RO(system);
2586b525beSRodolfo Giometti
time_show(struct device * dev,struct device_attribute * attr,char * buf)2686b525beSRodolfo Giometti static ssize_t time_show(struct device *dev, struct device_attribute *attr,
2786b525beSRodolfo Giometti char *buf)
2886b525beSRodolfo Giometti {
2986b525beSRodolfo Giometti struct pps_gen_device *pps_gen = dev_get_drvdata(dev);
3086b525beSRodolfo Giometti struct timespec64 time;
3186b525beSRodolfo Giometti int ret;
3286b525beSRodolfo Giometti
33*ac9c5170SSubramanian Mohan ret = pps_gen->info->get_time(pps_gen, &time);
3486b525beSRodolfo Giometti if (ret)
3586b525beSRodolfo Giometti return ret;
3686b525beSRodolfo Giometti
3786b525beSRodolfo Giometti return sysfs_emit(buf, "%llu %09lu\n", time.tv_sec, time.tv_nsec);
3886b525beSRodolfo Giometti }
3986b525beSRodolfo Giometti static DEVICE_ATTR_RO(time);
4086b525beSRodolfo Giometti
enable_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)4186b525beSRodolfo Giometti static ssize_t enable_store(struct device *dev, struct device_attribute *attr,
4286b525beSRodolfo Giometti const char *buf, size_t count)
4386b525beSRodolfo Giometti {
4486b525beSRodolfo Giometti struct pps_gen_device *pps_gen = dev_get_drvdata(dev);
4586b525beSRodolfo Giometti bool status;
4686b525beSRodolfo Giometti int ret;
4786b525beSRodolfo Giometti
4886b525beSRodolfo Giometti ret = kstrtobool(buf, &status);
4986b525beSRodolfo Giometti if (ret)
5086b525beSRodolfo Giometti return ret;
5186b525beSRodolfo Giometti
52*ac9c5170SSubramanian Mohan ret = pps_gen->info->enable(pps_gen, status);
5386b525beSRodolfo Giometti if (ret)
5486b525beSRodolfo Giometti return ret;
5586b525beSRodolfo Giometti pps_gen->enabled = status;
5686b525beSRodolfo Giometti
5786b525beSRodolfo Giometti return count;
5886b525beSRodolfo Giometti }
5986b525beSRodolfo Giometti static DEVICE_ATTR_WO(enable);
6086b525beSRodolfo Giometti
6186b525beSRodolfo Giometti static struct attribute *pps_gen_attrs[] = {
6286b525beSRodolfo Giometti &dev_attr_enable.attr,
6386b525beSRodolfo Giometti &dev_attr_time.attr,
6486b525beSRodolfo Giometti &dev_attr_system.attr,
6586b525beSRodolfo Giometti NULL,
6686b525beSRodolfo Giometti };
6786b525beSRodolfo Giometti
6886b525beSRodolfo Giometti static const struct attribute_group pps_gen_group = {
6986b525beSRodolfo Giometti .attrs = pps_gen_attrs,
7086b525beSRodolfo Giometti };
7186b525beSRodolfo Giometti
7286b525beSRodolfo Giometti const struct attribute_group *pps_gen_groups[] = {
7386b525beSRodolfo Giometti &pps_gen_group,
7486b525beSRodolfo Giometti NULL,
7586b525beSRodolfo Giometti };
76