Lines Matching +full:comp +full:- +full:int
1 // SPDX-License-Identifier: GPL-2.0
20 #include "counter-sysfs.h"
28 * struct counter_attribute - Counter sysfs attribute
31 * @comp: Counter component callbacks and data
39 struct counter_comp comp; member
48 * struct counter_attribute_group - container for attribute group
62 [COUNTER_FUNCTION_PULSE_DIRECTION] = "pulse-direction",
90 [COUNTER_COUNT_MODE_NON_RECYCLE] = "non-recycle",
91 [COUNTER_COUNT_MODE_MODULO_N] = "modulo-n",
93 [COUNTER_COUNT_MODE_HARDWARE_RETRIGGERABLE_ONESHOT] = "hardware retriggerable one-shot",
110 int err; in counter_comp_u8_show()
113 switch (a->scope) { in counter_comp_u8_show()
115 err = a->comp.device_u8_read(counter, &data); in counter_comp_u8_show()
118 err = a->comp.signal_u8_read(counter, a->parent, &data); in counter_comp_u8_show()
121 err = a->comp.count_u8_read(counter, a->parent, &data); in counter_comp_u8_show()
124 return -EINVAL; in counter_comp_u8_show()
129 if (a->comp.type == COUNTER_COMP_BOOL) in counter_comp_u8_show()
133 return sysfs_emit(buf, "%u\n", (unsigned int)data); in counter_comp_u8_show()
142 int err; in counter_comp_u8_store()
146 if (a->comp.type == COUNTER_COMP_BOOL) { in counter_comp_u8_store()
154 switch (a->scope) { in counter_comp_u8_store()
156 err = a->comp.device_u8_write(counter, data); in counter_comp_u8_store()
159 err = a->comp.signal_u8_write(counter, a->parent, data); in counter_comp_u8_store()
162 err = a->comp.count_u8_write(counter, a->parent, data); in counter_comp_u8_store()
165 return -EINVAL; in counter_comp_u8_store()
178 const struct counter_available *const avail = a->comp.priv; in counter_comp_u32_show()
179 int err; in counter_comp_u32_show()
182 switch (a->scope) { in counter_comp_u32_show()
184 err = a->comp.device_u32_read(counter, &data); in counter_comp_u32_show()
187 err = a->comp.signal_u32_read(counter, a->parent, &data); in counter_comp_u32_show()
190 if (a->comp.type == COUNTER_COMP_SYNAPSE_ACTION) in counter_comp_u32_show()
191 err = a->comp.action_read(counter, a->parent, in counter_comp_u32_show()
192 a->comp.priv, &data); in counter_comp_u32_show()
194 err = a->comp.count_u32_read(counter, a->parent, &data); in counter_comp_u32_show()
197 return -EINVAL; in counter_comp_u32_show()
202 switch (a->comp.type) { in counter_comp_u32_show()
210 return sysfs_emit(buf, "%s\n", avail->strs[data]); in counter_comp_u32_show()
218 return sysfs_emit(buf, "%u\n", (unsigned int)data); in counter_comp_u32_show()
222 static int counter_find_enum(u32 *const enum_item, const u32 *const enums, in counter_find_enum()
234 return -EINVAL; in counter_find_enum()
243 struct counter_count *const count = a->parent; in counter_comp_u32_store()
244 struct counter_synapse *const synapse = a->comp.priv; in counter_comp_u32_store()
245 const struct counter_available *const avail = a->comp.priv; in counter_comp_u32_store()
246 int err; in counter_comp_u32_store()
249 switch (a->comp.type) { in counter_comp_u32_store()
251 err = counter_find_enum(&data, count->functions_list, in counter_comp_u32_store()
252 count->num_functions, buf, in counter_comp_u32_store()
256 err = counter_find_enum(&data, synapse->actions_list, in counter_comp_u32_store()
257 synapse->num_actions, buf, in counter_comp_u32_store()
261 err = __sysfs_match_string(avail->strs, avail->num_items, buf); in counter_comp_u32_store()
265 err = counter_find_enum(&data, avail->enums, avail->num_items, in counter_comp_u32_store()
269 err = counter_find_enum(&data, avail->enums, avail->num_items, in counter_comp_u32_store()
279 switch (a->scope) { in counter_comp_u32_store()
281 err = a->comp.device_u32_write(counter, data); in counter_comp_u32_store()
284 err = a->comp.signal_u32_write(counter, a->parent, data); in counter_comp_u32_store()
287 if (a->comp.type == COUNTER_COMP_SYNAPSE_ACTION) in counter_comp_u32_store()
288 err = a->comp.action_write(counter, count, synapse, in counter_comp_u32_store()
291 err = a->comp.count_u32_write(counter, count, data); in counter_comp_u32_store()
294 return -EINVAL; in counter_comp_u32_store()
307 int err; in counter_comp_u64_show()
310 switch (a->scope) { in counter_comp_u64_show()
312 err = a->comp.device_u64_read(counter, &data); in counter_comp_u64_show()
315 err = a->comp.signal_u64_read(counter, a->parent, &data); in counter_comp_u64_show()
318 err = a->comp.count_u64_read(counter, a->parent, &data); in counter_comp_u64_show()
321 return -EINVAL; in counter_comp_u64_show()
335 int err; in counter_comp_u64_store()
342 switch (a->scope) { in counter_comp_u64_store()
344 err = a->comp.device_u64_write(counter, data); in counter_comp_u64_store()
347 err = a->comp.signal_u64_write(counter, a->parent, data); in counter_comp_u64_store()
350 err = a->comp.count_u64_write(counter, a->parent, data); in counter_comp_u64_store()
353 return -EINVAL; in counter_comp_u64_store()
367 const struct counter_array *const element = a->comp.priv; in counter_comp_array_u32_show()
368 int err; in counter_comp_array_u32_show()
371 if (a->scope != COUNTER_SCOPE_SIGNAL || in counter_comp_array_u32_show()
372 element->type != COUNTER_COMP_SIGNAL_POLARITY) in counter_comp_array_u32_show()
373 return -EINVAL; in counter_comp_array_u32_show()
375 err = a->comp.signal_array_u32_read(counter, a->parent, element->idx, in counter_comp_array_u32_show()
389 const struct counter_array *const element = a->comp.priv; in counter_comp_array_u32_store()
390 int err; in counter_comp_array_u32_store()
393 if (element->type != COUNTER_COMP_SIGNAL_POLARITY || in counter_comp_array_u32_store()
394 a->scope != COUNTER_SCOPE_SIGNAL) in counter_comp_array_u32_store()
395 return -EINVAL; in counter_comp_array_u32_store()
397 err = counter_find_enum(&data, element->avail->enums, in counter_comp_array_u32_store()
398 element->avail->num_items, buf, in counter_comp_array_u32_store()
403 err = a->comp.signal_array_u32_write(counter, a->parent, element->idx, in counter_comp_array_u32_store()
417 const struct counter_array *const element = a->comp.priv; in counter_comp_array_u64_show()
418 int err; in counter_comp_array_u64_show()
421 switch (a->scope) { in counter_comp_array_u64_show()
423 err = a->comp.device_array_u64_read(counter, element->idx, in counter_comp_array_u64_show()
427 err = a->comp.signal_array_u64_read(counter, a->parent, in counter_comp_array_u64_show()
428 element->idx, &data); in counter_comp_array_u64_show()
431 err = a->comp.count_array_u64_read(counter, a->parent, in counter_comp_array_u64_show()
432 element->idx, &data); in counter_comp_array_u64_show()
435 return -EINVAL; in counter_comp_array_u64_show()
449 const struct counter_array *const element = a->comp.priv; in counter_comp_array_u64_store()
450 int err; in counter_comp_array_u64_store()
457 switch (a->scope) { in counter_comp_array_u64_store()
459 err = a->comp.device_array_u64_write(counter, element->idx, in counter_comp_array_u64_store()
463 err = a->comp.signal_array_u64_write(counter, a->parent, in counter_comp_array_u64_store()
464 element->idx, data); in counter_comp_array_u64_store()
467 err = a->comp.count_array_u64_write(counter, a->parent, in counter_comp_array_u64_store()
468 element->idx, data); in counter_comp_array_u64_store()
471 return -EINVAL; in counter_comp_array_u64_store()
498 for (index = 0; index < avail->num_items; index++) in strs_available_show()
499 len += sysfs_emit_at(buf, len, "%s\n", avail->strs[index]); in strs_available_show()
509 const struct counter_count *const count = a->parent; in counter_comp_available_show()
510 const struct counter_synapse *const synapse = a->comp.priv; in counter_comp_available_show()
511 const struct counter_available *const avail = a->comp.priv; in counter_comp_available_show()
513 switch (a->comp.type) { in counter_comp_available_show()
515 return enums_available_show(count->functions_list, in counter_comp_available_show()
516 count->num_functions, in counter_comp_available_show()
519 return enums_available_show(synapse->actions_list, in counter_comp_available_show()
520 synapse->num_actions, in counter_comp_available_show()
525 return enums_available_show(avail->enums, avail->num_items, in counter_comp_available_show()
528 return -EINVAL; in counter_comp_available_show()
532 static int counter_avail_attr_create(struct device *const dev, in counter_avail_attr_create()
534 const struct counter_comp *const comp, void *const parent) in counter_avail_attr_create() argument
541 return -ENOMEM; in counter_avail_attr_create()
544 counter_attr->comp.type = comp->type; in counter_avail_attr_create()
545 counter_attr->comp.priv = comp->priv; in counter_avail_attr_create()
546 counter_attr->parent = parent; in counter_avail_attr_create()
549 dev_attr = &counter_attr->dev_attr; in counter_avail_attr_create()
550 sysfs_attr_init(&dev_attr->attr); in counter_avail_attr_create()
553 dev_attr->attr.name = devm_kasprintf(dev, GFP_KERNEL, "%s_available", in counter_avail_attr_create()
554 comp->name); in counter_avail_attr_create()
555 if (!dev_attr->attr.name) in counter_avail_attr_create()
556 return -ENOMEM; in counter_avail_attr_create()
557 dev_attr->attr.mode = 0444; in counter_avail_attr_create()
558 dev_attr->show = counter_comp_available_show; in counter_avail_attr_create()
561 list_add(&counter_attr->l, &group->attr_list); in counter_avail_attr_create()
562 group->num_attr++; in counter_avail_attr_create()
567 static int counter_attr_create(struct device *const dev, in counter_attr_create()
569 const struct counter_comp *const comp, in counter_attr_create() argument
573 const struct counter_array *const array = comp->priv; in counter_attr_create()
579 return -ENOMEM; in counter_attr_create()
582 counter_attr->comp = *comp; in counter_attr_create()
583 counter_attr->scope = scope; in counter_attr_create()
584 counter_attr->parent = parent; in counter_attr_create()
587 dev_attr = &counter_attr->dev_attr; in counter_attr_create()
588 sysfs_attr_init(&dev_attr->attr); in counter_attr_create()
589 dev_attr->attr.name = comp->name; in counter_attr_create()
590 switch (comp->type) { in counter_attr_create()
593 if (comp->device_u8_read) { in counter_attr_create()
594 dev_attr->attr.mode |= 0444; in counter_attr_create()
595 dev_attr->show = counter_comp_u8_show; in counter_attr_create()
597 if (comp->device_u8_write) { in counter_attr_create()
598 dev_attr->attr.mode |= 0200; in counter_attr_create()
599 dev_attr->store = counter_comp_u8_store; in counter_attr_create()
609 if (comp->device_u32_read) { in counter_attr_create()
610 dev_attr->attr.mode |= 0444; in counter_attr_create()
611 dev_attr->show = counter_comp_u32_show; in counter_attr_create()
613 if (comp->device_u32_write) { in counter_attr_create()
614 dev_attr->attr.mode |= 0200; in counter_attr_create()
615 dev_attr->store = counter_comp_u32_store; in counter_attr_create()
619 if (comp->device_u64_read) { in counter_attr_create()
620 dev_attr->attr.mode |= 0444; in counter_attr_create()
621 dev_attr->show = counter_comp_u64_show; in counter_attr_create()
623 if (comp->device_u64_write) { in counter_attr_create()
624 dev_attr->attr.mode |= 0200; in counter_attr_create()
625 dev_attr->store = counter_comp_u64_store; in counter_attr_create()
629 switch (array->type) { in counter_attr_create()
631 if (comp->signal_array_u32_read) { in counter_attr_create()
632 dev_attr->attr.mode |= 0444; in counter_attr_create()
633 dev_attr->show = counter_comp_array_u32_show; in counter_attr_create()
635 if (comp->signal_array_u32_write) { in counter_attr_create()
636 dev_attr->attr.mode |= 0200; in counter_attr_create()
637 dev_attr->store = counter_comp_array_u32_store; in counter_attr_create()
641 if (comp->device_array_u64_read) { in counter_attr_create()
642 dev_attr->attr.mode |= 0444; in counter_attr_create()
643 dev_attr->show = counter_comp_array_u64_show; in counter_attr_create()
645 if (comp->device_array_u64_write) { in counter_attr_create()
646 dev_attr->attr.mode |= 0200; in counter_attr_create()
647 dev_attr->store = counter_comp_array_u64_store; in counter_attr_create()
651 return -EINVAL; in counter_attr_create()
655 return -EINVAL; in counter_attr_create()
659 list_add(&counter_attr->l, &group->attr_list); in counter_attr_create()
660 group->num_attr++; in counter_attr_create()
663 switch (comp->type) { in counter_attr_create()
668 return counter_avail_attr_create(dev, group, comp, parent); in counter_attr_create()
677 return sysfs_emit(buf, "%s\n", to_counter_attribute(attr)->comp.name); in counter_comp_name_show()
680 static int counter_name_attr_create(struct device *const dev, in counter_name_attr_create()
688 return -ENOMEM; in counter_name_attr_create()
691 counter_attr->comp.name = name; in counter_name_attr_create()
694 sysfs_attr_init(&counter_attr->dev_attr.attr); in counter_name_attr_create()
695 counter_attr->dev_attr.attr.name = "name"; in counter_name_attr_create()
696 counter_attr->dev_attr.attr.mode = 0444; in counter_name_attr_create()
697 counter_attr->dev_attr.show = counter_comp_name_show; in counter_name_attr_create()
700 list_add(&counter_attr->l, &group->attr_list); in counter_name_attr_create()
701 group->num_attr++; in counter_name_attr_create()
709 const size_t id = (size_t)to_counter_attribute(attr)->comp.priv; in counter_comp_id_show()
714 static int counter_comp_id_attr_create(struct device *const dev, in counter_comp_id_attr_create()
723 return -ENOMEM; in counter_comp_id_attr_create()
728 return -ENOMEM; in counter_comp_id_attr_create()
731 counter_attr->comp.priv = (void *)id; in counter_comp_id_attr_create()
734 sysfs_attr_init(&counter_attr->dev_attr.attr); in counter_comp_id_attr_create()
735 counter_attr->dev_attr.attr.name = name; in counter_comp_id_attr_create()
736 counter_attr->dev_attr.attr.mode = 0444; in counter_comp_id_attr_create()
737 counter_attr->dev_attr.show = counter_comp_id_show; in counter_comp_id_attr_create()
740 list_add(&counter_attr->l, &group->attr_list); in counter_comp_id_attr_create()
741 group->num_attr++; in counter_comp_id_attr_create()
746 static int counter_ext_attrs_create(struct device *const dev, in counter_ext_attrs_create()
752 int err; in counter_ext_attrs_create()
760 return counter_comp_id_attr_create(dev, group, ext->name, id); in counter_ext_attrs_create()
763 static int counter_array_attrs_create(struct device *const dev, in counter_array_attrs_create()
765 const struct counter_comp *const comp, in counter_array_attrs_create() argument
769 const struct counter_array *const array = comp->priv; in counter_array_attrs_create()
770 struct counter_comp ext = *comp; in counter_array_attrs_create()
773 int err; in counter_array_attrs_create()
776 for (idx = 0; idx < array->length; idx++) { in counter_array_attrs_create()
778 ext.name = devm_kasprintf(dev, GFP_KERNEL, "%s%zu", comp->name, in counter_array_attrs_create()
781 return -ENOMEM; in counter_array_attrs_create()
786 return -ENOMEM; in counter_array_attrs_create()
787 element->type = array->type; in counter_array_attrs_create()
788 element->avail = array->avail; in counter_array_attrs_create()
789 element->idx = idx; in counter_array_attrs_create()
802 static int counter_sysfs_exts_add(struct device *const dev, in counter_sysfs_exts_add()
811 int err; in counter_sysfs_exts_add()
818 if (ext->type == COUNTER_COMP_ARRAY) { in counter_sysfs_exts_add()
821 array = ext->priv; in counter_sysfs_exts_add()
822 id += array->length; in counter_sysfs_exts_add()
840 static int counter_signal_attrs_create(struct counter_device *const counter, in counter_signal_attrs_create()
845 struct device *const dev = &counter->dev; in counter_signal_attrs_create()
846 int err; in counter_signal_attrs_create()
847 struct counter_comp comp; in counter_signal_attrs_create() local
850 comp = counter_signal_comp; in counter_signal_attrs_create()
851 comp.signal_u32_read = counter->ops->signal_read; in counter_signal_attrs_create()
852 err = counter_attr_create(dev, cattr_group, &comp, scope, signal); in counter_signal_attrs_create()
857 err = counter_name_attr_create(dev, cattr_group, signal->name); in counter_signal_attrs_create()
862 return counter_sysfs_exts_add(dev, cattr_group, signal->ext, in counter_signal_attrs_create()
863 signal->num_ext, scope, signal); in counter_signal_attrs_create()
866 static int counter_sysfs_signals_add(struct counter_device *const counter, in counter_sysfs_signals_add()
870 int err; in counter_sysfs_signals_add()
873 for (i = 0; i < counter->num_signals; i++) { in counter_sysfs_signals_add()
875 groups[i].name = devm_kasprintf(&counter->dev, GFP_KERNEL, in counter_sysfs_signals_add()
878 return -ENOMEM; in counter_sysfs_signals_add()
882 counter->signals + i); in counter_sysfs_signals_add()
890 static int counter_sysfs_synapses_add(struct counter_device *const counter, in counter_sysfs_synapses_add()
897 for (i = 0; i < count->num_synapses; i++) { in counter_sysfs_synapses_add()
898 struct device *const dev = &counter->dev; in counter_sysfs_synapses_add()
901 struct counter_comp comp; in counter_sysfs_synapses_add() local
902 int err; in counter_sysfs_synapses_add()
904 synapse = count->synapses + i; in counter_sysfs_synapses_add()
907 id = synapse->signal - counter->signals; in counter_sysfs_synapses_add()
908 comp.name = devm_kasprintf(dev, GFP_KERNEL, "signal%zu_action", in counter_sysfs_synapses_add()
910 if (!comp.name) in counter_sysfs_synapses_add()
911 return -ENOMEM; in counter_sysfs_synapses_add()
914 comp.type = COUNTER_COMP_SYNAPSE_ACTION; in counter_sysfs_synapses_add()
915 comp.action_read = counter->ops->action_read; in counter_sysfs_synapses_add()
916 comp.action_write = counter->ops->action_write; in counter_sysfs_synapses_add()
917 comp.priv = synapse; in counter_sysfs_synapses_add()
918 err = counter_attr_create(dev, group, &comp, in counter_sysfs_synapses_add()
924 err = counter_comp_id_attr_create(dev, group, comp.name, i); in counter_sysfs_synapses_add()
940 static int counter_count_attrs_create(struct counter_device *const counter, in counter_count_attrs_create()
945 struct device *const dev = &counter->dev; in counter_count_attrs_create()
946 int err; in counter_count_attrs_create()
947 struct counter_comp comp; in counter_count_attrs_create() local
950 comp = counter_count_comp; in counter_count_attrs_create()
951 comp.count_u64_read = counter->ops->count_read; in counter_count_attrs_create()
952 comp.count_u64_write = counter->ops->count_write; in counter_count_attrs_create()
953 err = counter_attr_create(dev, cattr_group, &comp, scope, count); in counter_count_attrs_create()
958 err = counter_name_attr_create(dev, cattr_group, count->name); in counter_count_attrs_create()
963 comp = counter_function_comp; in counter_count_attrs_create()
964 comp.count_u32_read = counter->ops->function_read; in counter_count_attrs_create()
965 comp.count_u32_write = counter->ops->function_write; in counter_count_attrs_create()
966 err = counter_attr_create(dev, cattr_group, &comp, scope, count); in counter_count_attrs_create()
971 return counter_sysfs_exts_add(dev, cattr_group, count->ext, in counter_count_attrs_create()
972 count->num_ext, scope, count); in counter_count_attrs_create()
975 static int counter_sysfs_counts_add(struct counter_device *const counter, in counter_sysfs_counts_add()
980 int err; in counter_sysfs_counts_add()
983 for (i = 0; i < counter->num_counts; i++) { in counter_sysfs_counts_add()
984 count = counter->counts + i; in counter_sysfs_counts_add()
987 groups[i].name = devm_kasprintf(&counter->dev, GFP_KERNEL, in counter_sysfs_counts_add()
990 return -ENOMEM; in counter_sysfs_counts_add()
1006 static int counter_num_signals_read(struct counter_device *counter, u8 *val) in counter_num_signals_read()
1008 *val = counter->num_signals; in counter_num_signals_read()
1012 static int counter_num_counts_read(struct counter_device *counter, u8 *val) in counter_num_counts_read()
1014 *val = counter->num_counts; in counter_num_counts_read()
1018 static int counter_events_queue_size_read(struct counter_device *counter, in counter_events_queue_size_read()
1021 *val = kfifo_size(&counter->events); in counter_events_queue_size_read()
1025 static int counter_events_queue_size_write(struct counter_device *counter, in counter_events_queue_size_write()
1029 int err; in counter_events_queue_size_write()
1038 mutex_lock(&counter->events_out_lock); in counter_events_queue_size_write()
1039 spin_lock_irqsave(&counter->events_in_lock, flags); in counter_events_queue_size_write()
1040 kfifo_free(&counter->events); in counter_events_queue_size_write()
1041 counter->events.kfifo = events.kfifo; in counter_events_queue_size_write()
1042 spin_unlock_irqrestore(&counter->events_in_lock, flags); in counter_events_queue_size_write()
1043 mutex_unlock(&counter->events_out_lock); in counter_events_queue_size_write()
1059 static int counter_sysfs_attr_add(struct counter_device *const counter, in counter_sysfs_attr_add()
1063 struct device *const dev = &counter->dev; in counter_sysfs_attr_add()
1064 int err; in counter_sysfs_attr_add()
1070 cattr_group += counter->num_signals; in counter_sysfs_attr_add()
1076 cattr_group += counter->num_counts; in counter_sysfs_attr_add()
1079 err = counter_name_attr_create(dev, cattr_group, counter->name); in counter_sysfs_attr_add()
1102 return counter_sysfs_exts_add(dev, cattr_group, counter->ext, in counter_sysfs_attr_add()
1103 counter->num_ext, scope, NULL); in counter_sysfs_attr_add()
1109 * counter_sysfs_add - Adds Counter sysfs attributes to the device structure
1113 * structure for later registration to the system. Resource-managed memory
1118 int counter_sysfs_add(struct counter_device *const counter) in counter_sysfs_add()
1120 struct device *const dev = &counter->dev; in counter_sysfs_add()
1121 const size_t num_groups = counter->num_signals + counter->num_counts + 1; in counter_sysfs_add()
1124 int err; in counter_sysfs_add()
1132 return -ENOMEM; in counter_sysfs_add()
1144 dev->groups = devm_kcalloc(dev, num_groups + 1, sizeof(*dev->groups), in counter_sysfs_add()
1146 if (!dev->groups) in counter_sysfs_add()
1147 return -ENOMEM; in counter_sysfs_add()
1152 return -ENOMEM; in counter_sysfs_add()
1164 return -ENOMEM; in counter_sysfs_add()
1169 groups[i].attrs[j++] = &p->dev_attr.attr; in counter_sysfs_add()
1172 dev->groups[i] = &groups[i]; in counter_sysfs_add()