2 * ACPI helpers for DMA request / controller
6 * Copyright (C) 2013, Intel Corporation
7 * Author: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
14 #include <linux/device.h>
15 #include <linux/module.h>
16 #include <linux/list.h>
17 #include <linux/mutex.h>
18 #include <linux/slab.h>
19 #include <linux/acpi.h>
20 #include <linux/acpi_dma.h>
22 static LIST_HEAD(acpi_dma_list);
23 static DEFINE_MUTEX(acpi_dma_lock);
26 * acpi_dma_controller_register - Register a DMA controller to ACPI DMA helpers
27 * @dev: struct device of DMA controller
28 * @acpi_dma_xlate: translation function which converts a dma specifier
29 * into a dma_chan structure
30 * @data pointer to controller specific data to be used by
31 * translation function
33 * Returns 0 on success or appropriate errno value on error.
35 * Allocated memory should be freed with appropriate acpi_dma_controller_free()
38 int acpi_dma_controller_register(struct device *dev,
39 struct dma_chan *(*acpi_dma_xlate)
40 (struct acpi_dma_spec *, struct acpi_dma *),
43 struct acpi_device *adev;
44 struct acpi_dma *adma;
46 if (!dev || !acpi_dma_xlate)
49 /* Check if the device was enumerated by ACPI */
50 if (!ACPI_HANDLE(dev))
53 if (acpi_bus_get_device(ACPI_HANDLE(dev), &adev))
56 adma = kzalloc(sizeof(*adma), GFP_KERNEL);
61 adma->acpi_dma_xlate = acpi_dma_xlate;
64 /* Now queue acpi_dma controller structure in list */
65 mutex_lock(&acpi_dma_lock);
66 list_add_tail(&adma->dma_controllers, &acpi_dma_list);
67 mutex_unlock(&acpi_dma_lock);
71 EXPORT_SYMBOL_GPL(acpi_dma_controller_register);
74 * acpi_dma_controller_free - Remove a DMA controller from ACPI DMA helpers list
75 * @dev: struct device of DMA controller
77 * Memory allocated by acpi_dma_controller_register() is freed here.
79 int acpi_dma_controller_free(struct device *dev)
81 struct acpi_dma *adma;
86 mutex_lock(&acpi_dma_lock);
88 list_for_each_entry(adma, &acpi_dma_list, dma_controllers)
89 if (adma->dev == dev) {
90 list_del(&adma->dma_controllers);
91 mutex_unlock(&acpi_dma_lock);
96 mutex_unlock(&acpi_dma_lock);
99 EXPORT_SYMBOL_GPL(acpi_dma_controller_free);
101 static void devm_acpi_dma_release(struct device *dev, void *res)
103 acpi_dma_controller_free(dev);
107 * devm_acpi_dma_controller_register - resource managed acpi_dma_controller_register()
108 * @dev: device that is registering this DMA controller
109 * @acpi_dma_xlate: translation function
110 * @data pointer to controller specific data
112 * Managed acpi_dma_controller_register(). DMA controller registered by this
113 * function are automatically freed on driver detach. See
114 * acpi_dma_controller_register() for more information.
116 int devm_acpi_dma_controller_register(struct device *dev,
117 struct dma_chan *(*acpi_dma_xlate)
118 (struct acpi_dma_spec *, struct acpi_dma *),
124 res = devres_alloc(devm_acpi_dma_release, 0, GFP_KERNEL);
128 ret = acpi_dma_controller_register(dev, acpi_dma_xlate, data);
133 devres_add(dev, res);
136 EXPORT_SYMBOL_GPL(devm_acpi_dma_controller_register);
139 * devm_acpi_dma_controller_free - resource managed acpi_dma_controller_free()
141 * Unregister a DMA controller registered with
142 * devm_acpi_dma_controller_register(). Normally this function will not need to
143 * be called and the resource management code will ensure that the resource is
146 void devm_acpi_dma_controller_free(struct device *dev)
148 WARN_ON(devres_destroy(dev, devm_acpi_dma_release, NULL, NULL));
150 EXPORT_SYMBOL_GPL(devm_acpi_dma_controller_free);
152 struct acpi_dma_parser_data {
153 struct acpi_dma_spec dma_spec;
159 * acpi_dma_parse_fixed_dma - Parse FixedDMA ACPI resources to a DMA specifier
160 * @res: struct acpi_resource to get FixedDMA resources from
161 * @data: pointer to a helper struct acpi_dma_parser_data
163 static int acpi_dma_parse_fixed_dma(struct acpi_resource *res, void *data)
165 struct acpi_dma_parser_data *pdata = data;
167 if (res->type == ACPI_RESOURCE_TYPE_FIXED_DMA) {
168 struct acpi_resource_fixed_dma *dma = &res->data.fixed_dma;
170 if (pdata->n++ == pdata->index) {
171 pdata->dma_spec.chan_id = dma->channels;
172 pdata->dma_spec.slave_id = dma->request_lines;
176 /* Tell the ACPI core to skip this resource */
181 * acpi_dma_request_slave_chan_by_index - Get the DMA slave channel
182 * @dev: struct device to get DMA request from
183 * @index: index of FixedDMA descriptor for @dev
185 * Returns pointer to appropriate dma channel on success or NULL on error.
187 struct dma_chan *acpi_dma_request_slave_chan_by_index(struct device *dev,
190 struct acpi_dma_parser_data pdata;
191 struct acpi_dma_spec *dma_spec = &pdata.dma_spec;
192 struct list_head resource_list;
193 struct acpi_device *adev;
194 struct acpi_dma *adma;
195 struct dma_chan *chan = NULL;
197 /* Check if the device was enumerated by ACPI */
198 if (!dev || !ACPI_HANDLE(dev))
201 if (acpi_bus_get_device(ACPI_HANDLE(dev), &adev))
204 memset(&pdata, 0, sizeof(pdata));
207 /* Initial values for the request line and channel */
208 dma_spec->chan_id = -1;
209 dma_spec->slave_id = -1;
211 INIT_LIST_HEAD(&resource_list);
212 acpi_dev_get_resources(adev, &resource_list,
213 acpi_dma_parse_fixed_dma, &pdata);
214 acpi_dev_free_resource_list(&resource_list);
216 if (dma_spec->slave_id < 0 || dma_spec->chan_id < 0)
219 mutex_lock(&acpi_dma_lock);
221 list_for_each_entry(adma, &acpi_dma_list, dma_controllers) {
222 dma_spec->dev = adma->dev;
223 chan = adma->acpi_dma_xlate(dma_spec, adma);
228 mutex_unlock(&acpi_dma_lock);
231 EXPORT_SYMBOL_GPL(acpi_dma_request_slave_chan_by_index);
234 * acpi_dma_request_slave_chan_by_name - Get the DMA slave channel
235 * @dev: struct device to get DMA request from
236 * @name: represents corresponding FixedDMA descriptor for @dev
238 * In order to support both Device Tree and ACPI in a single driver we
239 * translate the names "tx" and "rx" here based on the most common case where
240 * the first FixedDMA descriptor is TX and second is RX.
242 * Returns pointer to appropriate dma channel on success or NULL on error.
244 struct dma_chan *acpi_dma_request_slave_chan_by_name(struct device *dev,
249 if (!strcmp(name, "tx"))
251 else if (!strcmp(name, "rx"))
256 return acpi_dma_request_slave_chan_by_index(dev, index);
258 EXPORT_SYMBOL_GPL(acpi_dma_request_slave_chan_by_name);
261 * acpi_dma_simple_xlate - Simple ACPI DMA engine translation helper
262 * @dma_spec: pointer to ACPI DMA specifier
263 * @adma: pointer to ACPI DMA controller data
265 * A simple translation function for ACPI based devices. Passes &struct
266 * dma_spec to the DMA controller driver provided filter function. Returns
267 * pointer to the channel if found or %NULL otherwise.
269 struct dma_chan *acpi_dma_simple_xlate(struct acpi_dma_spec *dma_spec,
270 struct acpi_dma *adma)
272 struct acpi_dma_filter_info *info = adma->data;
274 if (!info || !info->filter_fn)
277 return dma_request_channel(info->dma_cap, info->filter_fn, dma_spec);
279 EXPORT_SYMBOL_GPL(acpi_dma_simple_xlate);