Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * Defines interfaces for interacting wtih the Raspberry Pi firmware's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  * property channel.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * Copyright © 2015 Broadcom
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/dma-mapping.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/kref.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/mailbox_client.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/of_platform.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <soc/bcm2835/raspberrypi-firmware.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #define MBOX_MSG(chan, data28)		(((data28) & ~0xf) | ((chan) & 0xf))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #define MBOX_CHAN(msg)			((msg) & 0xf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #define MBOX_DATA28(msg)		((msg) & ~0xf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #define MBOX_CHAN_PROPERTY		8
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) static struct platform_device *rpi_hwmon;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) static struct platform_device *rpi_clk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) struct rpi_firmware {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 	struct mbox_client cl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 	struct mbox_chan *chan; /* The property channel. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 	struct completion c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 	u32 enabled;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 	struct kref consumers;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) static DEFINE_MUTEX(transaction_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) static void response_callback(struct mbox_client *cl, void *msg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	struct rpi_firmware *fw = container_of(cl, struct rpi_firmware, cl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	complete(&fw->c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44)  * Sends a request to the firmware through the BCM2835 mailbox driver,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45)  * and synchronously waits for the reply.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) rpi_firmware_transaction(struct rpi_firmware *fw, u32 chan, u32 data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	u32 message = MBOX_MSG(chan, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	WARN_ON(data & 0xf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	mutex_lock(&transaction_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	reinit_completion(&fw->c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	ret = mbox_send_message(fw->chan, &message);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	if (ret >= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 		if (wait_for_completion_timeout(&fw->c, HZ)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 			ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 			ret = -ETIMEDOUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 			WARN_ONCE(1, "Firmware transaction timeout");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 		dev_err(fw->cl.dev, "mbox_send_message returned %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	mutex_unlock(&transaction_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74)  * rpi_firmware_property_list - Submit firmware property list
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75)  * @fw:		Pointer to firmware structure from rpi_firmware_get().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76)  * @data:	Buffer holding tags.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77)  * @tag_size:	Size of tags buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79)  * Submits a set of concatenated tags to the VPU firmware through the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80)  * mailbox property interface.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82)  * The buffer header and the ending tag are added by this function and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83)  * don't need to be supplied, just the actual tags for your operation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84)  * See struct rpi_firmware_property_tag_header for the per-tag
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85)  * structure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) int rpi_firmware_property_list(struct rpi_firmware *fw,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 			       void *data, size_t tag_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	size_t size = tag_size + 12;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	u32 *buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	dma_addr_t bus_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	/* Packets are processed a dword at a time. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	if (size & 3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	buf = dma_alloc_coherent(fw->cl.dev, PAGE_ALIGN(size), &bus_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 				 GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	if (!buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	/* The firmware will error out without parsing in this case. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	WARN_ON(size >= 1024 * 1024);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	buf[0] = size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	buf[1] = RPI_FIRMWARE_STATUS_REQUEST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	memcpy(&buf[2], data, tag_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	buf[size / 4 - 1] = RPI_FIRMWARE_PROPERTY_END;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	wmb();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	ret = rpi_firmware_transaction(fw, MBOX_CHAN_PROPERTY, bus_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	rmb();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	memcpy(data, &buf[2], tag_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	if (ret == 0 && buf[1] != RPI_FIRMWARE_STATUS_SUCCESS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 		 * The tag name here might not be the one causing the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 		 * error, if there were multiple tags in the request.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 		 * But single-tag is the most common, so go with it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 		dev_err(fw->cl.dev, "Request 0x%08x returned status 0x%08x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 			buf[2], buf[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 		ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	dma_free_coherent(fw->cl.dev, PAGE_ALIGN(size), buf, bus_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) EXPORT_SYMBOL_GPL(rpi_firmware_property_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)  * rpi_firmware_property - Submit single firmware property
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136)  * @fw:		Pointer to firmware structure from rpi_firmware_get().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)  * @tag:	One of enum_mbox_property_tag.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)  * @tag_data:	Tag data buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139)  * @buf_size:	Buffer size.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141)  * Submits a single tag to the VPU firmware through the mailbox
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)  * property interface.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144)  * This is a convenience wrapper around
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)  * rpi_firmware_property_list() to avoid some of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146)  * boilerplate in property calls.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) int rpi_firmware_property(struct rpi_firmware *fw,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 			  u32 tag, void *tag_data, size_t buf_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	struct rpi_firmware_property_tag_header *header;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	/* Some mailboxes can use over 1k bytes. Rather than checking
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	 * size and using stack or kmalloc depending on requirements,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	 * just use kmalloc. Mailboxes don't get called enough to worry
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	 * too much about the time taken in the allocation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	void *data = kmalloc(sizeof(*header) + buf_size, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	if (!data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	header = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	header->tag = tag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	header->buf_size = buf_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	header->req_resp_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	memcpy(data + sizeof(*header), tag_data, buf_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	ret = rpi_firmware_property_list(fw, data, buf_size + sizeof(*header));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	memcpy(tag_data, data + sizeof(*header), buf_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	kfree(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) EXPORT_SYMBOL_GPL(rpi_firmware_property);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) rpi_firmware_print_firmware_revision(struct rpi_firmware *fw)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	time64_t date_and_time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	u32 packet;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 	int ret = rpi_firmware_property(fw,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 					RPI_FIRMWARE_GET_FIRMWARE_REVISION,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 					&packet, sizeof(packet));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	/* This is not compatible with y2038 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	date_and_time = packet;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 	dev_info(fw->cl.dev, "Attached to firmware from %ptT\n", &date_and_time);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) rpi_register_hwmon_driver(struct device *dev, struct rpi_firmware *fw)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	u32 packet;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	int ret = rpi_firmware_property(fw, RPI_FIRMWARE_GET_THROTTLED,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 					&packet, sizeof(packet));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	rpi_hwmon = platform_device_register_data(dev, "raspberrypi-hwmon",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 						  -1, NULL, 0);
^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 void rpi_register_clk_driver(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 	struct device_node *firmware;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 	 * Earlier DTs don't have a node for the firmware clocks but
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 	 * rely on us creating a platform device by hand. If we do
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 	 * have a node for the firmware clocks, just bail out here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	firmware = of_get_compatible_child(dev->of_node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 					   "raspberrypi,firmware-clocks");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 	if (firmware) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 		of_node_put(firmware);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 	rpi_clk = platform_device_register_data(dev, "raspberrypi-clk",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 						-1, NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) static void rpi_firmware_delete(struct kref *kref)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 	struct rpi_firmware *fw = container_of(kref, struct rpi_firmware,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 					       consumers);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 	mbox_free_channel(fw->chan);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 	kfree(fw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) void rpi_firmware_put(struct rpi_firmware *fw)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 	kref_put(&fw->consumers, rpi_firmware_delete);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) EXPORT_SYMBOL_GPL(rpi_firmware_put);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) static int rpi_firmware_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 	struct device *dev = &pdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 	struct rpi_firmware *fw;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 	 * Memory will be freed by rpi_firmware_delete() once all users have
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 	 * released their firmware handles. Don't use devm_kzalloc() here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 	fw = kzalloc(sizeof(*fw), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 	if (!fw)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 	fw->cl.dev = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 	fw->cl.rx_callback = response_callback;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	fw->cl.tx_block = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 	fw->chan = mbox_request_channel(&fw->cl, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	if (IS_ERR(fw->chan)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 		int ret = PTR_ERR(fw->chan);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 		if (ret != -EPROBE_DEFER)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 			dev_err(dev, "Failed to get mbox channel: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	init_completion(&fw->c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 	kref_init(&fw->consumers);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 	platform_set_drvdata(pdev, fw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 	rpi_firmware_print_firmware_revision(fw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 	rpi_register_hwmon_driver(dev, fw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 	rpi_register_clk_driver(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) static void rpi_firmware_shutdown(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 	struct rpi_firmware *fw = platform_get_drvdata(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 	if (!fw)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 	rpi_firmware_property(fw, RPI_FIRMWARE_NOTIFY_REBOOT, NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) static int rpi_firmware_remove(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 	struct rpi_firmware *fw = platform_get_drvdata(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 	platform_device_unregister(rpi_hwmon);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 	rpi_hwmon = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 	platform_device_unregister(rpi_clk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 	rpi_clk = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 	rpi_firmware_put(fw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308)  * rpi_firmware_get - Get pointer to rpi_firmware structure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309)  * @firmware_node:    Pointer to the firmware Device Tree node.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311)  * The reference to rpi_firmware has to be released with rpi_firmware_put().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313)  * Returns NULL is the firmware device is not ready.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) struct rpi_firmware *rpi_firmware_get(struct device_node *firmware_node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 	struct platform_device *pdev = of_find_device_by_node(firmware_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 	struct rpi_firmware *fw;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 	if (!pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 	fw = platform_get_drvdata(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 	if (!fw)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 		goto err_put_device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 	if (!kref_get_unless_zero(&fw->consumers))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 		goto err_put_device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 	put_device(&pdev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 	return fw;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) err_put_device:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 	put_device(&pdev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) EXPORT_SYMBOL_GPL(rpi_firmware_get);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) static const struct of_device_id rpi_firmware_of_match[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 	{ .compatible = "raspberrypi,bcm2835-firmware", },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 	{},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) MODULE_DEVICE_TABLE(of, rpi_firmware_of_match);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) static struct platform_driver rpi_firmware_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 	.driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 		.name = "raspberrypi-firmware",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 		.of_match_table = rpi_firmware_of_match,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 	.probe		= rpi_firmware_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 	.shutdown	= rpi_firmware_shutdown,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 	.remove		= rpi_firmware_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) module_platform_driver(rpi_firmware_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) MODULE_AUTHOR("Eric Anholt <eric@anholt.net>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) MODULE_DESCRIPTION("Raspberry Pi firmware driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) MODULE_LICENSE("GPL v2");