xref: /openbmc/linux/drivers/greybus/bundle.c (revision 8465def4)
18465def4SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
28465def4SGreg Kroah-Hartman /*
38465def4SGreg Kroah-Hartman  * Greybus bundles
48465def4SGreg Kroah-Hartman  *
58465def4SGreg Kroah-Hartman  * Copyright 2014-2015 Google Inc.
68465def4SGreg Kroah-Hartman  * Copyright 2014-2015 Linaro Ltd.
78465def4SGreg Kroah-Hartman  */
88465def4SGreg Kroah-Hartman 
98465def4SGreg Kroah-Hartman #include <linux/greybus.h>
108465def4SGreg Kroah-Hartman #include "greybus_trace.h"
118465def4SGreg Kroah-Hartman 
bundle_class_show(struct device * dev,struct device_attribute * attr,char * buf)128465def4SGreg Kroah-Hartman static ssize_t bundle_class_show(struct device *dev,
138465def4SGreg Kroah-Hartman 				 struct device_attribute *attr, char *buf)
148465def4SGreg Kroah-Hartman {
158465def4SGreg Kroah-Hartman 	struct gb_bundle *bundle = to_gb_bundle(dev);
168465def4SGreg Kroah-Hartman 
178465def4SGreg Kroah-Hartman 	return sprintf(buf, "0x%02x\n", bundle->class);
188465def4SGreg Kroah-Hartman }
198465def4SGreg Kroah-Hartman static DEVICE_ATTR_RO(bundle_class);
208465def4SGreg Kroah-Hartman 
bundle_id_show(struct device * dev,struct device_attribute * attr,char * buf)218465def4SGreg Kroah-Hartman static ssize_t bundle_id_show(struct device *dev,
228465def4SGreg Kroah-Hartman 			      struct device_attribute *attr, char *buf)
238465def4SGreg Kroah-Hartman {
248465def4SGreg Kroah-Hartman 	struct gb_bundle *bundle = to_gb_bundle(dev);
258465def4SGreg Kroah-Hartman 
268465def4SGreg Kroah-Hartman 	return sprintf(buf, "%u\n", bundle->id);
278465def4SGreg Kroah-Hartman }
288465def4SGreg Kroah-Hartman static DEVICE_ATTR_RO(bundle_id);
298465def4SGreg Kroah-Hartman 
state_show(struct device * dev,struct device_attribute * attr,char * buf)308465def4SGreg Kroah-Hartman static ssize_t state_show(struct device *dev, struct device_attribute *attr,
318465def4SGreg Kroah-Hartman 			  char *buf)
328465def4SGreg Kroah-Hartman {
338465def4SGreg Kroah-Hartman 	struct gb_bundle *bundle = to_gb_bundle(dev);
348465def4SGreg Kroah-Hartman 
358465def4SGreg Kroah-Hartman 	if (!bundle->state)
368465def4SGreg Kroah-Hartman 		return sprintf(buf, "\n");
378465def4SGreg Kroah-Hartman 
388465def4SGreg Kroah-Hartman 	return sprintf(buf, "%s\n", bundle->state);
398465def4SGreg Kroah-Hartman }
408465def4SGreg Kroah-Hartman 
state_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t size)418465def4SGreg Kroah-Hartman static ssize_t state_store(struct device *dev, struct device_attribute *attr,
428465def4SGreg Kroah-Hartman 			   const char *buf, size_t size)
438465def4SGreg Kroah-Hartman {
448465def4SGreg Kroah-Hartman 	struct gb_bundle *bundle = to_gb_bundle(dev);
458465def4SGreg Kroah-Hartman 
468465def4SGreg Kroah-Hartman 	kfree(bundle->state);
478465def4SGreg Kroah-Hartman 	bundle->state = kstrdup(buf, GFP_KERNEL);
488465def4SGreg Kroah-Hartman 	if (!bundle->state)
498465def4SGreg Kroah-Hartman 		return -ENOMEM;
508465def4SGreg Kroah-Hartman 
518465def4SGreg Kroah-Hartman 	/* Tell userspace that the file contents changed */
528465def4SGreg Kroah-Hartman 	sysfs_notify(&bundle->dev.kobj, NULL, "state");
538465def4SGreg Kroah-Hartman 
548465def4SGreg Kroah-Hartman 	return size;
558465def4SGreg Kroah-Hartman }
568465def4SGreg Kroah-Hartman static DEVICE_ATTR_RW(state);
578465def4SGreg Kroah-Hartman 
588465def4SGreg Kroah-Hartman static struct attribute *bundle_attrs[] = {
598465def4SGreg Kroah-Hartman 	&dev_attr_bundle_class.attr,
608465def4SGreg Kroah-Hartman 	&dev_attr_bundle_id.attr,
618465def4SGreg Kroah-Hartman 	&dev_attr_state.attr,
628465def4SGreg Kroah-Hartman 	NULL,
638465def4SGreg Kroah-Hartman };
648465def4SGreg Kroah-Hartman 
658465def4SGreg Kroah-Hartman ATTRIBUTE_GROUPS(bundle);
668465def4SGreg Kroah-Hartman 
gb_bundle_find(struct gb_interface * intf,u8 bundle_id)678465def4SGreg Kroah-Hartman static struct gb_bundle *gb_bundle_find(struct gb_interface *intf,
688465def4SGreg Kroah-Hartman 					u8 bundle_id)
698465def4SGreg Kroah-Hartman {
708465def4SGreg Kroah-Hartman 	struct gb_bundle *bundle;
718465def4SGreg Kroah-Hartman 
728465def4SGreg Kroah-Hartman 	list_for_each_entry(bundle, &intf->bundles, links) {
738465def4SGreg Kroah-Hartman 		if (bundle->id == bundle_id)
748465def4SGreg Kroah-Hartman 			return bundle;
758465def4SGreg Kroah-Hartman 	}
768465def4SGreg Kroah-Hartman 
778465def4SGreg Kroah-Hartman 	return NULL;
788465def4SGreg Kroah-Hartman }
798465def4SGreg Kroah-Hartman 
gb_bundle_release(struct device * dev)808465def4SGreg Kroah-Hartman static void gb_bundle_release(struct device *dev)
818465def4SGreg Kroah-Hartman {
828465def4SGreg Kroah-Hartman 	struct gb_bundle *bundle = to_gb_bundle(dev);
838465def4SGreg Kroah-Hartman 
848465def4SGreg Kroah-Hartman 	trace_gb_bundle_release(bundle);
858465def4SGreg Kroah-Hartman 
868465def4SGreg Kroah-Hartman 	kfree(bundle->state);
878465def4SGreg Kroah-Hartman 	kfree(bundle->cport_desc);
888465def4SGreg Kroah-Hartman 	kfree(bundle);
898465def4SGreg Kroah-Hartman }
908465def4SGreg Kroah-Hartman 
918465def4SGreg Kroah-Hartman #ifdef CONFIG_PM
gb_bundle_disable_all_connections(struct gb_bundle * bundle)928465def4SGreg Kroah-Hartman static void gb_bundle_disable_all_connections(struct gb_bundle *bundle)
938465def4SGreg Kroah-Hartman {
948465def4SGreg Kroah-Hartman 	struct gb_connection *connection;
958465def4SGreg Kroah-Hartman 
968465def4SGreg Kroah-Hartman 	list_for_each_entry(connection, &bundle->connections, bundle_links)
978465def4SGreg Kroah-Hartman 		gb_connection_disable(connection);
988465def4SGreg Kroah-Hartman }
998465def4SGreg Kroah-Hartman 
gb_bundle_enable_all_connections(struct gb_bundle * bundle)1008465def4SGreg Kroah-Hartman static void gb_bundle_enable_all_connections(struct gb_bundle *bundle)
1018465def4SGreg Kroah-Hartman {
1028465def4SGreg Kroah-Hartman 	struct gb_connection *connection;
1038465def4SGreg Kroah-Hartman 
1048465def4SGreg Kroah-Hartman 	list_for_each_entry(connection, &bundle->connections, bundle_links)
1058465def4SGreg Kroah-Hartman 		gb_connection_enable(connection);
1068465def4SGreg Kroah-Hartman }
1078465def4SGreg Kroah-Hartman 
gb_bundle_suspend(struct device * dev)1088465def4SGreg Kroah-Hartman static int gb_bundle_suspend(struct device *dev)
1098465def4SGreg Kroah-Hartman {
1108465def4SGreg Kroah-Hartman 	struct gb_bundle *bundle = to_gb_bundle(dev);
1118465def4SGreg Kroah-Hartman 	const struct dev_pm_ops *pm = dev->driver->pm;
1128465def4SGreg Kroah-Hartman 	int ret;
1138465def4SGreg Kroah-Hartman 
1148465def4SGreg Kroah-Hartman 	if (pm && pm->runtime_suspend) {
1158465def4SGreg Kroah-Hartman 		ret = pm->runtime_suspend(&bundle->dev);
1168465def4SGreg Kroah-Hartman 		if (ret)
1178465def4SGreg Kroah-Hartman 			return ret;
1188465def4SGreg Kroah-Hartman 	} else {
1198465def4SGreg Kroah-Hartman 		gb_bundle_disable_all_connections(bundle);
1208465def4SGreg Kroah-Hartman 	}
1218465def4SGreg Kroah-Hartman 
1228465def4SGreg Kroah-Hartman 	ret = gb_control_bundle_suspend(bundle->intf->control, bundle->id);
1238465def4SGreg Kroah-Hartman 	if (ret) {
1248465def4SGreg Kroah-Hartman 		if (pm && pm->runtime_resume)
1258465def4SGreg Kroah-Hartman 			ret = pm->runtime_resume(dev);
1268465def4SGreg Kroah-Hartman 		else
1278465def4SGreg Kroah-Hartman 			gb_bundle_enable_all_connections(bundle);
1288465def4SGreg Kroah-Hartman 
1298465def4SGreg Kroah-Hartman 		return ret;
1308465def4SGreg Kroah-Hartman 	}
1318465def4SGreg Kroah-Hartman 
1328465def4SGreg Kroah-Hartman 	return 0;
1338465def4SGreg Kroah-Hartman }
1348465def4SGreg Kroah-Hartman 
gb_bundle_resume(struct device * dev)1358465def4SGreg Kroah-Hartman static int gb_bundle_resume(struct device *dev)
1368465def4SGreg Kroah-Hartman {
1378465def4SGreg Kroah-Hartman 	struct gb_bundle *bundle = to_gb_bundle(dev);
1388465def4SGreg Kroah-Hartman 	const struct dev_pm_ops *pm = dev->driver->pm;
1398465def4SGreg Kroah-Hartman 	int ret;
1408465def4SGreg Kroah-Hartman 
1418465def4SGreg Kroah-Hartman 	ret = gb_control_bundle_resume(bundle->intf->control, bundle->id);
1428465def4SGreg Kroah-Hartman 	if (ret)
1438465def4SGreg Kroah-Hartman 		return ret;
1448465def4SGreg Kroah-Hartman 
1458465def4SGreg Kroah-Hartman 	if (pm && pm->runtime_resume) {
1468465def4SGreg Kroah-Hartman 		ret = pm->runtime_resume(dev);
1478465def4SGreg Kroah-Hartman 		if (ret)
1488465def4SGreg Kroah-Hartman 			return ret;
1498465def4SGreg Kroah-Hartman 	} else {
1508465def4SGreg Kroah-Hartman 		gb_bundle_enable_all_connections(bundle);
1518465def4SGreg Kroah-Hartman 	}
1528465def4SGreg Kroah-Hartman 
1538465def4SGreg Kroah-Hartman 	return 0;
1548465def4SGreg Kroah-Hartman }
1558465def4SGreg Kroah-Hartman 
gb_bundle_idle(struct device * dev)1568465def4SGreg Kroah-Hartman static int gb_bundle_idle(struct device *dev)
1578465def4SGreg Kroah-Hartman {
1588465def4SGreg Kroah-Hartman 	pm_runtime_mark_last_busy(dev);
1598465def4SGreg Kroah-Hartman 	pm_request_autosuspend(dev);
1608465def4SGreg Kroah-Hartman 
1618465def4SGreg Kroah-Hartman 	return 0;
1628465def4SGreg Kroah-Hartman }
1638465def4SGreg Kroah-Hartman #endif
1648465def4SGreg Kroah-Hartman 
1658465def4SGreg Kroah-Hartman static const struct dev_pm_ops gb_bundle_pm_ops = {
1668465def4SGreg Kroah-Hartman 	SET_RUNTIME_PM_OPS(gb_bundle_suspend, gb_bundle_resume, gb_bundle_idle)
1678465def4SGreg Kroah-Hartman };
1688465def4SGreg Kroah-Hartman 
1698465def4SGreg Kroah-Hartman struct device_type greybus_bundle_type = {
1708465def4SGreg Kroah-Hartman 	.name =		"greybus_bundle",
1718465def4SGreg Kroah-Hartman 	.release =	gb_bundle_release,
1728465def4SGreg Kroah-Hartman 	.pm =		&gb_bundle_pm_ops,
1738465def4SGreg Kroah-Hartman };
1748465def4SGreg Kroah-Hartman 
1758465def4SGreg Kroah-Hartman /*
1768465def4SGreg Kroah-Hartman  * Create a gb_bundle structure to represent a discovered
1778465def4SGreg Kroah-Hartman  * bundle.  Returns a pointer to the new bundle or a null
1788465def4SGreg Kroah-Hartman  * pointer if a failure occurs due to memory exhaustion.
1798465def4SGreg Kroah-Hartman  */
gb_bundle_create(struct gb_interface * intf,u8 bundle_id,u8 class)1808465def4SGreg Kroah-Hartman struct gb_bundle *gb_bundle_create(struct gb_interface *intf, u8 bundle_id,
1818465def4SGreg Kroah-Hartman 				   u8 class)
1828465def4SGreg Kroah-Hartman {
1838465def4SGreg Kroah-Hartman 	struct gb_bundle *bundle;
1848465def4SGreg Kroah-Hartman 
1858465def4SGreg Kroah-Hartman 	if (bundle_id == BUNDLE_ID_NONE) {
1868465def4SGreg Kroah-Hartman 		dev_err(&intf->dev, "can't use bundle id %u\n", bundle_id);
1878465def4SGreg Kroah-Hartman 		return NULL;
1888465def4SGreg Kroah-Hartman 	}
1898465def4SGreg Kroah-Hartman 
1908465def4SGreg Kroah-Hartman 	/*
1918465def4SGreg Kroah-Hartman 	 * Reject any attempt to reuse a bundle id.  We initialize
1928465def4SGreg Kroah-Hartman 	 * these serially, so there's no need to worry about keeping
1938465def4SGreg Kroah-Hartman 	 * the interface bundle list locked here.
1948465def4SGreg Kroah-Hartman 	 */
1958465def4SGreg Kroah-Hartman 	if (gb_bundle_find(intf, bundle_id)) {
1968465def4SGreg Kroah-Hartman 		dev_err(&intf->dev, "duplicate bundle id %u\n", bundle_id);
1978465def4SGreg Kroah-Hartman 		return NULL;
1988465def4SGreg Kroah-Hartman 	}
1998465def4SGreg Kroah-Hartman 
2008465def4SGreg Kroah-Hartman 	bundle = kzalloc(sizeof(*bundle), GFP_KERNEL);
2018465def4SGreg Kroah-Hartman 	if (!bundle)
2028465def4SGreg Kroah-Hartman 		return NULL;
2038465def4SGreg Kroah-Hartman 
2048465def4SGreg Kroah-Hartman 	bundle->intf = intf;
2058465def4SGreg Kroah-Hartman 	bundle->id = bundle_id;
2068465def4SGreg Kroah-Hartman 	bundle->class = class;
2078465def4SGreg Kroah-Hartman 	INIT_LIST_HEAD(&bundle->connections);
2088465def4SGreg Kroah-Hartman 
2098465def4SGreg Kroah-Hartman 	bundle->dev.parent = &intf->dev;
2108465def4SGreg Kroah-Hartman 	bundle->dev.bus = &greybus_bus_type;
2118465def4SGreg Kroah-Hartman 	bundle->dev.type = &greybus_bundle_type;
2128465def4SGreg Kroah-Hartman 	bundle->dev.groups = bundle_groups;
2138465def4SGreg Kroah-Hartman 	bundle->dev.dma_mask = intf->dev.dma_mask;
2148465def4SGreg Kroah-Hartman 	device_initialize(&bundle->dev);
2158465def4SGreg Kroah-Hartman 	dev_set_name(&bundle->dev, "%s.%d", dev_name(&intf->dev), bundle_id);
2168465def4SGreg Kroah-Hartman 
2178465def4SGreg Kroah-Hartman 	list_add(&bundle->links, &intf->bundles);
2188465def4SGreg Kroah-Hartman 
2198465def4SGreg Kroah-Hartman 	trace_gb_bundle_create(bundle);
2208465def4SGreg Kroah-Hartman 
2218465def4SGreg Kroah-Hartman 	return bundle;
2228465def4SGreg Kroah-Hartman }
2238465def4SGreg Kroah-Hartman 
gb_bundle_add(struct gb_bundle * bundle)2248465def4SGreg Kroah-Hartman int gb_bundle_add(struct gb_bundle *bundle)
2258465def4SGreg Kroah-Hartman {
2268465def4SGreg Kroah-Hartman 	int ret;
2278465def4SGreg Kroah-Hartman 
2288465def4SGreg Kroah-Hartman 	ret = device_add(&bundle->dev);
2298465def4SGreg Kroah-Hartman 	if (ret) {
2308465def4SGreg Kroah-Hartman 		dev_err(&bundle->dev, "failed to register bundle: %d\n", ret);
2318465def4SGreg Kroah-Hartman 		return ret;
2328465def4SGreg Kroah-Hartman 	}
2338465def4SGreg Kroah-Hartman 
2348465def4SGreg Kroah-Hartman 	trace_gb_bundle_add(bundle);
2358465def4SGreg Kroah-Hartman 
2368465def4SGreg Kroah-Hartman 	return 0;
2378465def4SGreg Kroah-Hartman }
2388465def4SGreg Kroah-Hartman 
2398465def4SGreg Kroah-Hartman /*
2408465def4SGreg Kroah-Hartman  * Tear down a previously set up bundle.
2418465def4SGreg Kroah-Hartman  */
gb_bundle_destroy(struct gb_bundle * bundle)2428465def4SGreg Kroah-Hartman void gb_bundle_destroy(struct gb_bundle *bundle)
2438465def4SGreg Kroah-Hartman {
2448465def4SGreg Kroah-Hartman 	trace_gb_bundle_destroy(bundle);
2458465def4SGreg Kroah-Hartman 
2468465def4SGreg Kroah-Hartman 	if (device_is_registered(&bundle->dev))
2478465def4SGreg Kroah-Hartman 		device_del(&bundle->dev);
2488465def4SGreg Kroah-Hartman 
2498465def4SGreg Kroah-Hartman 	list_del(&bundle->links);
2508465def4SGreg Kroah-Hartman 
2518465def4SGreg Kroah-Hartman 	put_device(&bundle->dev);
2528465def4SGreg Kroah-Hartman }
253