1502b431cSStefan Wahren // SPDX-License-Identifier: GPL-2.0
24e3d6065SEric Anholt /*
360aa8782SWang Qing * Defines interfaces for interacting with the Raspberry Pi firmware's
44e3d6065SEric Anholt * property channel.
54e3d6065SEric Anholt *
64e3d6065SEric Anholt * Copyright © 2015 Broadcom
74e3d6065SEric Anholt */
84e3d6065SEric Anholt
94e3d6065SEric Anholt #include <linux/dma-mapping.h>
101e7c5735SNicolas Saenz Julienne #include <linux/kref.h>
114e3d6065SEric Anholt #include <linux/mailbox_client.h>
12*25edcae6SLaurent Pinchart #include <linux/mailbox_controller.h>
134e3d6065SEric Anholt #include <linux/module.h>
145b45759cSRob Herring #include <linux/of.h>
154e3d6065SEric Anholt #include <linux/of_platform.h>
164e3d6065SEric Anholt #include <linux/platform_device.h>
1791c6ada6SJames Hughes #include <linux/slab.h>
184e3d6065SEric Anholt #include <soc/bcm2835/raspberrypi-firmware.h>
194e3d6065SEric Anholt
204e3d6065SEric Anholt #define MBOX_MSG(chan, data28) (((data28) & ~0xf) | ((chan) & 0xf))
214e3d6065SEric Anholt #define MBOX_CHAN(msg) ((msg) & 0xf)
224e3d6065SEric Anholt #define MBOX_DATA28(msg) ((msg) & ~0xf)
234e3d6065SEric Anholt #define MBOX_CHAN_PROPERTY 8
244e3d6065SEric Anholt
2570eea1bbSStefan Wahren static struct platform_device *rpi_hwmon;
2691f2cf4aSNicolas Saenz Julienne static struct platform_device *rpi_clk;
2770eea1bbSStefan Wahren
284e3d6065SEric Anholt struct rpi_firmware {
294e3d6065SEric Anholt struct mbox_client cl;
304e3d6065SEric Anholt struct mbox_chan *chan; /* The property channel. */
314e3d6065SEric Anholt struct completion c;
324e3d6065SEric Anholt u32 enabled;
331e7c5735SNicolas Saenz Julienne
341e7c5735SNicolas Saenz Julienne struct kref consumers;
354e3d6065SEric Anholt };
364e3d6065SEric Anholt
374e3d6065SEric Anholt static DEFINE_MUTEX(transaction_lock);
384e3d6065SEric Anholt
response_callback(struct mbox_client * cl,void * msg)394e3d6065SEric Anholt static void response_callback(struct mbox_client *cl, void *msg)
404e3d6065SEric Anholt {
414e3d6065SEric Anholt struct rpi_firmware *fw = container_of(cl, struct rpi_firmware, cl);
424e3d6065SEric Anholt complete(&fw->c);
434e3d6065SEric Anholt }
444e3d6065SEric Anholt
454e3d6065SEric Anholt /*
464e3d6065SEric Anholt * Sends a request to the firmware through the BCM2835 mailbox driver,
474e3d6065SEric Anholt * and synchronously waits for the reply.
484e3d6065SEric Anholt */
494e3d6065SEric Anholt static int
rpi_firmware_transaction(struct rpi_firmware * fw,u32 chan,u32 data)504e3d6065SEric Anholt rpi_firmware_transaction(struct rpi_firmware *fw, u32 chan, u32 data)
514e3d6065SEric Anholt {
524e3d6065SEric Anholt u32 message = MBOX_MSG(chan, data);
534e3d6065SEric Anholt int ret;
544e3d6065SEric Anholt
554e3d6065SEric Anholt WARN_ON(data & 0xf);
564e3d6065SEric Anholt
574e3d6065SEric Anholt mutex_lock(&transaction_lock);
584e3d6065SEric Anholt reinit_completion(&fw->c);
594e3d6065SEric Anholt ret = mbox_send_message(fw->chan, &message);
604e3d6065SEric Anholt if (ret >= 0) {
610829187bSStefan Wahren if (wait_for_completion_timeout(&fw->c, HZ)) {
624e3d6065SEric Anholt ret = 0;
634e3d6065SEric Anholt } else {
640829187bSStefan Wahren ret = -ETIMEDOUT;
650829187bSStefan Wahren WARN_ONCE(1, "Firmware transaction timeout");
660829187bSStefan Wahren }
670829187bSStefan Wahren } else {
684e3d6065SEric Anholt dev_err(fw->cl.dev, "mbox_send_message returned %d\n", ret);
694e3d6065SEric Anholt }
704e3d6065SEric Anholt mutex_unlock(&transaction_lock);
714e3d6065SEric Anholt
724e3d6065SEric Anholt return ret;
734e3d6065SEric Anholt }
744e3d6065SEric Anholt
754e3d6065SEric Anholt /**
764e3d6065SEric Anholt * rpi_firmware_property_list - Submit firmware property list
774e3d6065SEric Anholt * @fw: Pointer to firmware structure from rpi_firmware_get().
784e3d6065SEric Anholt * @data: Buffer holding tags.
794e3d6065SEric Anholt * @tag_size: Size of tags buffer.
804e3d6065SEric Anholt *
814e3d6065SEric Anholt * Submits a set of concatenated tags to the VPU firmware through the
824e3d6065SEric Anholt * mailbox property interface.
834e3d6065SEric Anholt *
844e3d6065SEric Anholt * The buffer header and the ending tag are added by this function and
854e3d6065SEric Anholt * don't need to be supplied, just the actual tags for your operation.
864e3d6065SEric Anholt * See struct rpi_firmware_property_tag_header for the per-tag
874e3d6065SEric Anholt * structure.
884e3d6065SEric Anholt */
rpi_firmware_property_list(struct rpi_firmware * fw,void * data,size_t tag_size)894e3d6065SEric Anholt int rpi_firmware_property_list(struct rpi_firmware *fw,
904e3d6065SEric Anholt void *data, size_t tag_size)
914e3d6065SEric Anholt {
924e3d6065SEric Anholt size_t size = tag_size + 12;
934e3d6065SEric Anholt u32 *buf;
944e3d6065SEric Anholt dma_addr_t bus_addr;
954e3d6065SEric Anholt int ret;
964e3d6065SEric Anholt
974e3d6065SEric Anholt /* Packets are processed a dword at a time. */
984e3d6065SEric Anholt if (size & 3)
994e3d6065SEric Anholt return -EINVAL;
1004e3d6065SEric Anholt
101*25edcae6SLaurent Pinchart buf = dma_alloc_coherent(fw->chan->mbox->dev, PAGE_ALIGN(size),
102*25edcae6SLaurent Pinchart &bus_addr, GFP_ATOMIC);
1034e3d6065SEric Anholt if (!buf)
1044e3d6065SEric Anholt return -ENOMEM;
1054e3d6065SEric Anholt
1064e3d6065SEric Anholt /* The firmware will error out without parsing in this case. */
1074e3d6065SEric Anholt WARN_ON(size >= 1024 * 1024);
1084e3d6065SEric Anholt
1094e3d6065SEric Anholt buf[0] = size;
1104e3d6065SEric Anholt buf[1] = RPI_FIRMWARE_STATUS_REQUEST;
1114e3d6065SEric Anholt memcpy(&buf[2], data, tag_size);
1124e3d6065SEric Anholt buf[size / 4 - 1] = RPI_FIRMWARE_PROPERTY_END;
1134e3d6065SEric Anholt wmb();
1144e3d6065SEric Anholt
1154e3d6065SEric Anholt ret = rpi_firmware_transaction(fw, MBOX_CHAN_PROPERTY, bus_addr);
1164e3d6065SEric Anholt
1174e3d6065SEric Anholt rmb();
1184e3d6065SEric Anholt memcpy(data, &buf[2], tag_size);
1194e3d6065SEric Anholt if (ret == 0 && buf[1] != RPI_FIRMWARE_STATUS_SUCCESS) {
1204e3d6065SEric Anholt /*
1214e3d6065SEric Anholt * The tag name here might not be the one causing the
1224e3d6065SEric Anholt * error, if there were multiple tags in the request.
1234e3d6065SEric Anholt * But single-tag is the most common, so go with it.
1244e3d6065SEric Anholt */
1254e3d6065SEric Anholt dev_err(fw->cl.dev, "Request 0x%08x returned status 0x%08x\n",
1264e3d6065SEric Anholt buf[2], buf[1]);
1274e3d6065SEric Anholt ret = -EINVAL;
1284e3d6065SEric Anholt }
1294e3d6065SEric Anholt
130*25edcae6SLaurent Pinchart dma_free_coherent(fw->chan->mbox->dev, PAGE_ALIGN(size), buf, bus_addr);
1314e3d6065SEric Anholt
1324e3d6065SEric Anholt return ret;
1334e3d6065SEric Anholt }
1344e3d6065SEric Anholt EXPORT_SYMBOL_GPL(rpi_firmware_property_list);
1354e3d6065SEric Anholt
1364e3d6065SEric Anholt /**
1374e3d6065SEric Anholt * rpi_firmware_property - Submit single firmware property
1384e3d6065SEric Anholt * @fw: Pointer to firmware structure from rpi_firmware_get().
1394e3d6065SEric Anholt * @tag: One of enum_mbox_property_tag.
1404e3d6065SEric Anholt * @tag_data: Tag data buffer.
1414e3d6065SEric Anholt * @buf_size: Buffer size.
1424e3d6065SEric Anholt *
1434e3d6065SEric Anholt * Submits a single tag to the VPU firmware through the mailbox
1444e3d6065SEric Anholt * property interface.
1454e3d6065SEric Anholt *
1464e3d6065SEric Anholt * This is a convenience wrapper around
1474e3d6065SEric Anholt * rpi_firmware_property_list() to avoid some of the
1484e3d6065SEric Anholt * boilerplate in property calls.
1494e3d6065SEric Anholt */
rpi_firmware_property(struct rpi_firmware * fw,u32 tag,void * tag_data,size_t buf_size)1504e3d6065SEric Anholt int rpi_firmware_property(struct rpi_firmware *fw,
1514e3d6065SEric Anholt u32 tag, void *tag_data, size_t buf_size)
1524e3d6065SEric Anholt {
15391c6ada6SJames Hughes struct rpi_firmware_property_tag_header *header;
1544e3d6065SEric Anholt int ret;
1554e3d6065SEric Anholt
15691c6ada6SJames Hughes /* Some mailboxes can use over 1k bytes. Rather than checking
15791c6ada6SJames Hughes * size and using stack or kmalloc depending on requirements,
15891c6ada6SJames Hughes * just use kmalloc. Mailboxes don't get called enough to worry
15991c6ada6SJames Hughes * too much about the time taken in the allocation.
16091c6ada6SJames Hughes */
16191c6ada6SJames Hughes void *data = kmalloc(sizeof(*header) + buf_size, GFP_KERNEL);
162a1547e0bSKees Cook
16391c6ada6SJames Hughes if (!data)
16491c6ada6SJames Hughes return -ENOMEM;
16591c6ada6SJames Hughes
16691c6ada6SJames Hughes header = data;
1674e3d6065SEric Anholt header->tag = tag;
1684e3d6065SEric Anholt header->buf_size = buf_size;
1694e3d6065SEric Anholt header->req_resp_size = 0;
17091c6ada6SJames Hughes memcpy(data + sizeof(*header), tag_data, buf_size);
1714e3d6065SEric Anholt
17291c6ada6SJames Hughes ret = rpi_firmware_property_list(fw, data, buf_size + sizeof(*header));
17391c6ada6SJames Hughes
17491c6ada6SJames Hughes memcpy(tag_data, data + sizeof(*header), buf_size);
17591c6ada6SJames Hughes
17691c6ada6SJames Hughes kfree(data);
1774e3d6065SEric Anholt
1784e3d6065SEric Anholt return ret;
1794e3d6065SEric Anholt }
1804e3d6065SEric Anholt EXPORT_SYMBOL_GPL(rpi_firmware_property);
1814e3d6065SEric Anholt
1824e3d6065SEric Anholt static void
rpi_firmware_print_firmware_revision(struct rpi_firmware * fw)1834e3d6065SEric Anholt rpi_firmware_print_firmware_revision(struct rpi_firmware *fw)
1844e3d6065SEric Anholt {
185da785a87SAndy Shevchenko time64_t date_and_time;
1864e3d6065SEric Anholt u32 packet;
1874e3d6065SEric Anholt int ret = rpi_firmware_property(fw,
1884e3d6065SEric Anholt RPI_FIRMWARE_GET_FIRMWARE_REVISION,
1894e3d6065SEric Anholt &packet, sizeof(packet));
1904e3d6065SEric Anholt
1914a60f58eSAndy Shevchenko if (ret)
1924a60f58eSAndy Shevchenko return;
1934e3d6065SEric Anholt
194da785a87SAndy Shevchenko /* This is not compatible with y2038 */
195da785a87SAndy Shevchenko date_and_time = packet;
196da785a87SAndy Shevchenko dev_info(fw->cl.dev, "Attached to firmware from %ptT\n", &date_and_time);
1974e3d6065SEric Anholt }
1984e3d6065SEric Anholt
19970eea1bbSStefan Wahren static void
rpi_register_hwmon_driver(struct device * dev,struct rpi_firmware * fw)20070eea1bbSStefan Wahren rpi_register_hwmon_driver(struct device *dev, struct rpi_firmware *fw)
20170eea1bbSStefan Wahren {
20270eea1bbSStefan Wahren u32 packet;
20370eea1bbSStefan Wahren int ret = rpi_firmware_property(fw, RPI_FIRMWARE_GET_THROTTLED,
20470eea1bbSStefan Wahren &packet, sizeof(packet));
20570eea1bbSStefan Wahren
20670eea1bbSStefan Wahren if (ret)
20770eea1bbSStefan Wahren return;
20870eea1bbSStefan Wahren
20970eea1bbSStefan Wahren rpi_hwmon = platform_device_register_data(dev, "raspberrypi-hwmon",
21070eea1bbSStefan Wahren -1, NULL, 0);
21170eea1bbSStefan Wahren }
21270eea1bbSStefan Wahren
rpi_register_clk_driver(struct device * dev)21391f2cf4aSNicolas Saenz Julienne static void rpi_register_clk_driver(struct device *dev)
21491f2cf4aSNicolas Saenz Julienne {
215511aba09SMaxime Ripard struct device_node *firmware;
216511aba09SMaxime Ripard
217511aba09SMaxime Ripard /*
218511aba09SMaxime Ripard * Earlier DTs don't have a node for the firmware clocks but
219511aba09SMaxime Ripard * rely on us creating a platform device by hand. If we do
220511aba09SMaxime Ripard * have a node for the firmware clocks, just bail out here.
221511aba09SMaxime Ripard */
222511aba09SMaxime Ripard firmware = of_get_compatible_child(dev->of_node,
223511aba09SMaxime Ripard "raspberrypi,firmware-clocks");
224511aba09SMaxime Ripard if (firmware) {
225511aba09SMaxime Ripard of_node_put(firmware);
226511aba09SMaxime Ripard return;
227511aba09SMaxime Ripard }
228511aba09SMaxime Ripard
22991f2cf4aSNicolas Saenz Julienne rpi_clk = platform_device_register_data(dev, "raspberrypi-clk",
23091f2cf4aSNicolas Saenz Julienne -1, NULL, 0);
23191f2cf4aSNicolas Saenz Julienne }
23291f2cf4aSNicolas Saenz Julienne
rpi_firmware_clk_get_max_rate(struct rpi_firmware * fw,unsigned int id)23340c31955SMaxime Ripard unsigned int rpi_firmware_clk_get_max_rate(struct rpi_firmware *fw, unsigned int id)
23440c31955SMaxime Ripard {
23540c31955SMaxime Ripard struct rpi_firmware_clk_rate_request msg =
23640c31955SMaxime Ripard RPI_FIRMWARE_CLK_RATE_REQUEST(id);
23740c31955SMaxime Ripard int ret;
23840c31955SMaxime Ripard
23940c31955SMaxime Ripard ret = rpi_firmware_property(fw, RPI_FIRMWARE_GET_MAX_CLOCK_RATE,
24040c31955SMaxime Ripard &msg, sizeof(msg));
24140c31955SMaxime Ripard if (ret)
24240c31955SMaxime Ripard /*
24340c31955SMaxime Ripard * If our firmware doesn't support that operation, or fails, we
24440c31955SMaxime Ripard * assume the maximum clock rate is absolute maximum we can
24540c31955SMaxime Ripard * store over our type.
24640c31955SMaxime Ripard */
24740c31955SMaxime Ripard return UINT_MAX;
24840c31955SMaxime Ripard
24940c31955SMaxime Ripard return le32_to_cpu(msg.rate);
25040c31955SMaxime Ripard }
25140c31955SMaxime Ripard EXPORT_SYMBOL_GPL(rpi_firmware_clk_get_max_rate);
25240c31955SMaxime Ripard
rpi_firmware_delete(struct kref * kref)2531e7c5735SNicolas Saenz Julienne static void rpi_firmware_delete(struct kref *kref)
2541e7c5735SNicolas Saenz Julienne {
2551e7c5735SNicolas Saenz Julienne struct rpi_firmware *fw = container_of(kref, struct rpi_firmware,
2561e7c5735SNicolas Saenz Julienne consumers);
2571e7c5735SNicolas Saenz Julienne
2581e7c5735SNicolas Saenz Julienne mbox_free_channel(fw->chan);
2591e7c5735SNicolas Saenz Julienne kfree(fw);
2601e7c5735SNicolas Saenz Julienne }
2611e7c5735SNicolas Saenz Julienne
rpi_firmware_put(struct rpi_firmware * fw)2621e7c5735SNicolas Saenz Julienne void rpi_firmware_put(struct rpi_firmware *fw)
2631e7c5735SNicolas Saenz Julienne {
2641e7c5735SNicolas Saenz Julienne kref_put(&fw->consumers, rpi_firmware_delete);
2651e7c5735SNicolas Saenz Julienne }
2661e7c5735SNicolas Saenz Julienne EXPORT_SYMBOL_GPL(rpi_firmware_put);
2671e7c5735SNicolas Saenz Julienne
devm_rpi_firmware_put(void * data)268f663204cSNicolas Saenz Julienne static void devm_rpi_firmware_put(void *data)
269f663204cSNicolas Saenz Julienne {
270f663204cSNicolas Saenz Julienne struct rpi_firmware *fw = data;
271f663204cSNicolas Saenz Julienne
272f663204cSNicolas Saenz Julienne rpi_firmware_put(fw);
273f663204cSNicolas Saenz Julienne }
274f663204cSNicolas Saenz Julienne
rpi_firmware_probe(struct platform_device * pdev)2754e3d6065SEric Anholt static int rpi_firmware_probe(struct platform_device *pdev)
2764e3d6065SEric Anholt {
2774e3d6065SEric Anholt struct device *dev = &pdev->dev;
2784e3d6065SEric Anholt struct rpi_firmware *fw;
2794e3d6065SEric Anholt
2801e7c5735SNicolas Saenz Julienne /*
2811e7c5735SNicolas Saenz Julienne * Memory will be freed by rpi_firmware_delete() once all users have
2821e7c5735SNicolas Saenz Julienne * released their firmware handles. Don't use devm_kzalloc() here.
2831e7c5735SNicolas Saenz Julienne */
2841e7c5735SNicolas Saenz Julienne fw = kzalloc(sizeof(*fw), GFP_KERNEL);
2854e3d6065SEric Anholt if (!fw)
2864e3d6065SEric Anholt return -ENOMEM;
2874e3d6065SEric Anholt
2884e3d6065SEric Anholt fw->cl.dev = dev;
2894e3d6065SEric Anholt fw->cl.rx_callback = response_callback;
2904e3d6065SEric Anholt fw->cl.tx_block = true;
2914e3d6065SEric Anholt
2924e3d6065SEric Anholt fw->chan = mbox_request_channel(&fw->cl, 0);
2934e3d6065SEric Anholt if (IS_ERR(fw->chan)) {
2944e3d6065SEric Anholt int ret = PTR_ERR(fw->chan);
2957b511616SYang Yingliang kfree(fw);
296ba54ff1fSLinus Torvalds return dev_err_probe(dev, ret, "Failed to get mbox channel\n");
2974e3d6065SEric Anholt }
2984e3d6065SEric Anholt
2994e3d6065SEric Anholt init_completion(&fw->c);
3001e7c5735SNicolas Saenz Julienne kref_init(&fw->consumers);
3014e3d6065SEric Anholt
3024e3d6065SEric Anholt platform_set_drvdata(pdev, fw);
3034e3d6065SEric Anholt
3044e3d6065SEric Anholt rpi_firmware_print_firmware_revision(fw);
30570eea1bbSStefan Wahren rpi_register_hwmon_driver(dev, fw);
30691f2cf4aSNicolas Saenz Julienne rpi_register_clk_driver(dev);
3074e3d6065SEric Anholt
3084e3d6065SEric Anholt return 0;
3094e3d6065SEric Anholt }
3104e3d6065SEric Anholt
rpi_firmware_shutdown(struct platform_device * pdev)311b80ec7c0SStefan Wahren static void rpi_firmware_shutdown(struct platform_device *pdev)
312b80ec7c0SStefan Wahren {
313b80ec7c0SStefan Wahren struct rpi_firmware *fw = platform_get_drvdata(pdev);
314b80ec7c0SStefan Wahren
315b80ec7c0SStefan Wahren if (!fw)
316b80ec7c0SStefan Wahren return;
317b80ec7c0SStefan Wahren
318b80ec7c0SStefan Wahren rpi_firmware_property(fw, RPI_FIRMWARE_NOTIFY_REBOOT, NULL, 0);
319b80ec7c0SStefan Wahren }
320b80ec7c0SStefan Wahren
rpi_firmware_remove(struct platform_device * pdev)3214e3d6065SEric Anholt static int rpi_firmware_remove(struct platform_device *pdev)
3224e3d6065SEric Anholt {
3234e3d6065SEric Anholt struct rpi_firmware *fw = platform_get_drvdata(pdev);
3244e3d6065SEric Anholt
32570eea1bbSStefan Wahren platform_device_unregister(rpi_hwmon);
32670eea1bbSStefan Wahren rpi_hwmon = NULL;
32791f2cf4aSNicolas Saenz Julienne platform_device_unregister(rpi_clk);
32891f2cf4aSNicolas Saenz Julienne rpi_clk = NULL;
3291e7c5735SNicolas Saenz Julienne
3301e7c5735SNicolas Saenz Julienne rpi_firmware_put(fw);
3314e3d6065SEric Anholt
3324e3d6065SEric Anholt return 0;
3334e3d6065SEric Anholt }
3344e3d6065SEric Anholt
335bc63897bSMaxime Ripard static const struct of_device_id rpi_firmware_of_match[] = {
336bc63897bSMaxime Ripard { .compatible = "raspberrypi,bcm2835-firmware", },
337bc63897bSMaxime Ripard {},
338bc63897bSMaxime Ripard };
339bc63897bSMaxime Ripard MODULE_DEVICE_TABLE(of, rpi_firmware_of_match);
340bc63897bSMaxime Ripard
rpi_firmware_find_node(void)341bc63897bSMaxime Ripard struct device_node *rpi_firmware_find_node(void)
342bc63897bSMaxime Ripard {
343bc63897bSMaxime Ripard return of_find_matching_node(NULL, rpi_firmware_of_match);
344bc63897bSMaxime Ripard }
345bc63897bSMaxime Ripard EXPORT_SYMBOL_GPL(rpi_firmware_find_node);
346bc63897bSMaxime Ripard
3474e3d6065SEric Anholt /**
3484e3d6065SEric Anholt * rpi_firmware_get - Get pointer to rpi_firmware structure.
3494e3d6065SEric Anholt * @firmware_node: Pointer to the firmware Device Tree node.
3504e3d6065SEric Anholt *
3511e7c5735SNicolas Saenz Julienne * The reference to rpi_firmware has to be released with rpi_firmware_put().
3521e7c5735SNicolas Saenz Julienne *
3534e3d6065SEric Anholt * Returns NULL is the firmware device is not ready.
3544e3d6065SEric Anholt */
rpi_firmware_get(struct device_node * firmware_node)3554e3d6065SEric Anholt struct rpi_firmware *rpi_firmware_get(struct device_node *firmware_node)
3564e3d6065SEric Anholt {
3574e3d6065SEric Anholt struct platform_device *pdev = of_find_device_by_node(firmware_node);
3581e7c5735SNicolas Saenz Julienne struct rpi_firmware *fw;
3594e3d6065SEric Anholt
3604e3d6065SEric Anholt if (!pdev)
3614e3d6065SEric Anholt return NULL;
3624e3d6065SEric Anholt
3631e7c5735SNicolas Saenz Julienne fw = platform_get_drvdata(pdev);
3641e7c5735SNicolas Saenz Julienne if (!fw)
36509cbd1dfSChristophe JAILLET goto err_put_device;
3661e7c5735SNicolas Saenz Julienne
3671e7c5735SNicolas Saenz Julienne if (!kref_get_unless_zero(&fw->consumers))
36809cbd1dfSChristophe JAILLET goto err_put_device;
36909cbd1dfSChristophe JAILLET
37009cbd1dfSChristophe JAILLET put_device(&pdev->dev);
3711e7c5735SNicolas Saenz Julienne
3721e7c5735SNicolas Saenz Julienne return fw;
37309cbd1dfSChristophe JAILLET
37409cbd1dfSChristophe JAILLET err_put_device:
37509cbd1dfSChristophe JAILLET put_device(&pdev->dev);
37609cbd1dfSChristophe JAILLET return NULL;
3774e3d6065SEric Anholt }
3784e3d6065SEric Anholt EXPORT_SYMBOL_GPL(rpi_firmware_get);
3794e3d6065SEric Anholt
380f663204cSNicolas Saenz Julienne /**
381f663204cSNicolas Saenz Julienne * devm_rpi_firmware_get - Get pointer to rpi_firmware structure.
382f663204cSNicolas Saenz Julienne * @firmware_node: Pointer to the firmware Device Tree node.
383f663204cSNicolas Saenz Julienne *
384f663204cSNicolas Saenz Julienne * Returns NULL is the firmware device is not ready.
385f663204cSNicolas Saenz Julienne */
devm_rpi_firmware_get(struct device * dev,struct device_node * firmware_node)386f663204cSNicolas Saenz Julienne struct rpi_firmware *devm_rpi_firmware_get(struct device *dev,
387f663204cSNicolas Saenz Julienne struct device_node *firmware_node)
388f663204cSNicolas Saenz Julienne {
389f663204cSNicolas Saenz Julienne struct rpi_firmware *fw;
390f663204cSNicolas Saenz Julienne
391f663204cSNicolas Saenz Julienne fw = rpi_firmware_get(firmware_node);
392f663204cSNicolas Saenz Julienne if (!fw)
393f663204cSNicolas Saenz Julienne return NULL;
394f663204cSNicolas Saenz Julienne
395f663204cSNicolas Saenz Julienne if (devm_add_action_or_reset(dev, devm_rpi_firmware_put, fw))
396f663204cSNicolas Saenz Julienne return NULL;
397f663204cSNicolas Saenz Julienne
398f663204cSNicolas Saenz Julienne return fw;
399f663204cSNicolas Saenz Julienne }
400f663204cSNicolas Saenz Julienne EXPORT_SYMBOL_GPL(devm_rpi_firmware_get);
401f663204cSNicolas Saenz Julienne
4024e3d6065SEric Anholt static struct platform_driver rpi_firmware_driver = {
4034e3d6065SEric Anholt .driver = {
4044e3d6065SEric Anholt .name = "raspberrypi-firmware",
4054e3d6065SEric Anholt .of_match_table = rpi_firmware_of_match,
4064e3d6065SEric Anholt },
4074e3d6065SEric Anholt .probe = rpi_firmware_probe,
408b80ec7c0SStefan Wahren .shutdown = rpi_firmware_shutdown,
4094e3d6065SEric Anholt .remove = rpi_firmware_remove,
4104e3d6065SEric Anholt };
4114e3d6065SEric Anholt module_platform_driver(rpi_firmware_driver);
4124e3d6065SEric Anholt
4134e3d6065SEric Anholt MODULE_AUTHOR("Eric Anholt <eric@anholt.net>");
4144e3d6065SEric Anholt MODULE_DESCRIPTION("Raspberry Pi firmware driver");
4154e3d6065SEric Anholt MODULE_LICENSE("GPL v2");
416