^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * PTP 1588 clock support - sysfs interface.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2010 OMICRON electronics GmbH
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/capability.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include "ptp_private.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) static ssize_t clock_name_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) struct device_attribute *attr, char *page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) struct ptp_clock *ptp = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) return snprintf(page, PAGE_SIZE-1, "%s\n", ptp->info->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) static DEVICE_ATTR_RO(clock_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #define PTP_SHOW_INT(name, var) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) static ssize_t var##_show(struct device *dev, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) struct device_attribute *attr, char *page) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) struct ptp_clock *ptp = dev_get_drvdata(dev); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) return snprintf(page, PAGE_SIZE-1, "%d\n", ptp->info->var); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) } \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) static DEVICE_ATTR(name, 0444, var##_show, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) PTP_SHOW_INT(max_adjustment, max_adj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) PTP_SHOW_INT(n_alarms, n_alarm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) PTP_SHOW_INT(n_external_timestamps, n_ext_ts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) PTP_SHOW_INT(n_periodic_outputs, n_per_out);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) PTP_SHOW_INT(n_programmable_pins, n_pins);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) PTP_SHOW_INT(pps_available, pps);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) static ssize_t extts_enable_store(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) struct ptp_clock *ptp = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) struct ptp_clock_info *ops = ptp->info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) struct ptp_clock_request req = { .type = PTP_CLK_REQ_EXTTS };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) int cnt, enable;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) int err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) cnt = sscanf(buf, "%u %d", &req.extts.index, &enable);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) if (cnt != 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) if (req.extts.index >= ops->n_ext_ts)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) err = ops->enable(ops, &req, enable ? 1 : 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) static DEVICE_ATTR(extts_enable, 0220, NULL, extts_enable_store);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) static ssize_t extts_fifo_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) struct device_attribute *attr, char *page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) struct ptp_clock *ptp = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) struct timestamp_event_queue *queue = &ptp->tsevq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) struct ptp_extts_event event;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) size_t qcnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) int cnt = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) memset(&event, 0, sizeof(event));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) if (mutex_lock_interruptible(&ptp->tsevq_mux))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) return -ERESTARTSYS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) spin_lock_irqsave(&queue->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) qcnt = queue_cnt(queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) if (qcnt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) event = queue->buf[queue->head];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) queue->head = (queue->head + 1) % PTP_MAX_TIMESTAMPS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) spin_unlock_irqrestore(&queue->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) if (!qcnt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) cnt = snprintf(page, PAGE_SIZE, "%u %lld %u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) event.index, event.t.sec, event.t.nsec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) mutex_unlock(&ptp->tsevq_mux);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) return cnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) static DEVICE_ATTR(fifo, 0444, extts_fifo_show, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) static ssize_t period_store(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) struct ptp_clock *ptp = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) struct ptp_clock_info *ops = ptp->info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) struct ptp_clock_request req = { .type = PTP_CLK_REQ_PEROUT };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) int cnt, enable, err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) cnt = sscanf(buf, "%u %lld %u %lld %u", &req.perout.index,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) &req.perout.start.sec, &req.perout.start.nsec,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) &req.perout.period.sec, &req.perout.period.nsec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) if (cnt != 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) if (req.perout.index >= ops->n_per_out)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) enable = req.perout.period.sec || req.perout.period.nsec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) err = ops->enable(ops, &req, enable);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) static DEVICE_ATTR(period, 0220, NULL, period_store);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) static ssize_t pps_enable_store(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) struct ptp_clock *ptp = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) struct ptp_clock_info *ops = ptp->info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) struct ptp_clock_request req = { .type = PTP_CLK_REQ_PPS };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) int cnt, enable;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) int err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) if (!capable(CAP_SYS_TIME))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) cnt = sscanf(buf, "%d", &enable);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) if (cnt != 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) err = ops->enable(ops, &req, enable ? 1 : 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) static DEVICE_ATTR(pps_enable, 0220, NULL, pps_enable_store);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) static struct attribute *ptp_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) &dev_attr_clock_name.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) &dev_attr_max_adjustment.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) &dev_attr_n_alarms.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) &dev_attr_n_external_timestamps.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) &dev_attr_n_periodic_outputs.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) &dev_attr_n_programmable_pins.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) &dev_attr_pps_available.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) &dev_attr_extts_enable.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) &dev_attr_fifo.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) &dev_attr_period.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) &dev_attr_pps_enable.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) static umode_t ptp_is_attribute_visible(struct kobject *kobj,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) struct attribute *attr, int n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) struct device *dev = kobj_to_dev(kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) struct ptp_clock *ptp = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) struct ptp_clock_info *info = ptp->info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) umode_t mode = attr->mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) if (attr == &dev_attr_extts_enable.attr ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) attr == &dev_attr_fifo.attr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) if (!info->n_ext_ts)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) mode = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) } else if (attr == &dev_attr_period.attr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) if (!info->n_per_out)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) mode = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) } else if (attr == &dev_attr_pps_enable.attr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) if (!info->pps)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) mode = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) return mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) static const struct attribute_group ptp_group = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) .is_visible = ptp_is_attribute_visible,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) .attrs = ptp_attrs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) const struct attribute_group *ptp_groups[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) &ptp_group,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) static int ptp_pin_name2index(struct ptp_clock *ptp, const char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) for (i = 0; i < ptp->info->n_pins; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) if (!strcmp(ptp->info->pin_config[i].name, name))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) return i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) static ssize_t ptp_pin_show(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) char *page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) struct ptp_clock *ptp = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) unsigned int func, chan;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) int index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) index = ptp_pin_name2index(ptp, attr->attr.name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) if (index < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) if (mutex_lock_interruptible(&ptp->pincfg_mux))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) return -ERESTARTSYS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) func = ptp->info->pin_config[index].func;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) chan = ptp->info->pin_config[index].chan;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) mutex_unlock(&ptp->pincfg_mux);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) return snprintf(page, PAGE_SIZE, "%u %u\n", func, chan);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) static ssize_t ptp_pin_store(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) struct ptp_clock *ptp = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) unsigned int func, chan;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) int cnt, err, index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) cnt = sscanf(buf, "%u %u", &func, &chan);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) if (cnt != 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) index = ptp_pin_name2index(ptp, attr->attr.name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) if (index < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) if (mutex_lock_interruptible(&ptp->pincfg_mux))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) return -ERESTARTSYS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) err = ptp_set_pinfunc(ptp, index, func, chan);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) mutex_unlock(&ptp->pincfg_mux);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) int ptp_populate_pin_groups(struct ptp_clock *ptp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) struct ptp_clock_info *info = ptp->info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) int err = -ENOMEM, i, n_pins = info->n_pins;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) if (!n_pins)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) ptp->pin_dev_attr = kcalloc(n_pins, sizeof(*ptp->pin_dev_attr),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) if (!ptp->pin_dev_attr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) goto no_dev_attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) ptp->pin_attr = kcalloc(1 + n_pins, sizeof(*ptp->pin_attr), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) if (!ptp->pin_attr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) goto no_pin_attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) for (i = 0; i < n_pins; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) struct device_attribute *da = &ptp->pin_dev_attr[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) sysfs_attr_init(&da->attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) da->attr.name = info->pin_config[i].name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) da->attr.mode = 0644;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) da->show = ptp_pin_show;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) da->store = ptp_pin_store;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) ptp->pin_attr[i] = &da->attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) ptp->pin_attr_group.name = "pins";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) ptp->pin_attr_group.attrs = ptp->pin_attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) ptp->pin_attr_groups[0] = &ptp->pin_attr_group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) no_pin_attr:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) kfree(ptp->pin_dev_attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) no_dev_attr:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) void ptp_cleanup_pin_groups(struct ptp_clock *ptp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) kfree(ptp->pin_attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) kfree(ptp->pin_dev_attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) }