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-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /* cx25840 firmware functions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6) #include <linux/i2c.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) #include <linux/firmware.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <media/v4l2-common.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <media/drv-intf/cx25840.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include "cx25840-core.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14)  * Mike Isely <isely@pobox.com> - The FWSEND parameter controls the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15)  * size of the firmware chunks sent down the I2C bus to the chip.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16)  * Previously this had been set to 1024 but unfortunately some I2C
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17)  * implementations can't transfer data in such big gulps.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18)  * Specifically, the pvrusb2 driver has a hard limit of around 60
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19)  * bytes, due to the encapsulation there of I2C traffic into USB
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20)  * messages.  So we have to significantly reduce this parameter.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #define FWSEND 48
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) #define FWDEV(x) &((x)->dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) static char *firmware = "";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) module_param(firmware, charp, 0444);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) MODULE_PARM_DESC(firmware, "Firmware image to load");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) static void start_fw_load(struct i2c_client *client)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 	/* DL_ADDR_LB=0 DL_ADDR_HB=0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	cx25840_write(client, 0x800, 0x00);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	cx25840_write(client, 0x801, 0x00);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 	// DL_MAP=3 DL_AUTO_INC=0 DL_ENABLE=1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	cx25840_write(client, 0x803, 0x0b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	/* AUTO_INC_DIS=1 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	cx25840_write(client, 0x000, 0x20);
^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) static void end_fw_load(struct i2c_client *client)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	/* AUTO_INC_DIS=0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	cx25840_write(client, 0x000, 0x00);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	/* DL_ENABLE=0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	cx25840_write(client, 0x803, 0x03);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) #define CX2388x_FIRMWARE "v4l-cx23885-avcore-01.fw"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) #define CX231xx_FIRMWARE "v4l-cx231xx-avcore-01.fw"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) #define CX25840_FIRMWARE "v4l-cx25840.fw"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) static const char *get_fw_name(struct i2c_client *client)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	struct cx25840_state *state = to_state(i2c_get_clientdata(client));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	if (firmware[0])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 		return firmware;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	if (is_cx2388x(state))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 		return CX2388x_FIRMWARE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	if (is_cx231xx(state))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 		return CX231xx_FIRMWARE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	return CX25840_FIRMWARE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) static int check_fw_load(struct i2c_client *client, int size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	/* DL_ADDR_HB DL_ADDR_LB */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	int s = cx25840_read(client, 0x801) << 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	s |= cx25840_read(client, 0x800);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	if (size != s) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 		v4l_err(client, "firmware %s load failed\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 				get_fw_name(client));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	v4l_info(client, "loaded %s firmware (%d bytes)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 			get_fw_name(client), size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) static int fw_write(struct i2c_client *client, const u8 *data, int size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	if (i2c_master_send(client, data, size) < size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 		v4l_err(client, "firmware load i2c failure\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 		return -ENOSYS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) int cx25840_loadfw(struct i2c_client *client)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	struct cx25840_state *state = to_state(i2c_get_clientdata(client));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	const struct firmware *fw = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	u8 buffer[FWSEND];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	const u8 *ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	const char *fwname = get_fw_name(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	int size, retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	int max_buf_size = FWSEND;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	u32 gpio_oe = 0, gpio_da = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	if (is_cx2388x(state)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 		/* Preserve the GPIO OE and output bits */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 		gpio_oe = cx25840_read(client, 0x160);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 		gpio_da = cx25840_read(client, 0x164);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	/* cx231xx cannot accept more than 16 bytes at a time */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	if (is_cx231xx(state) && max_buf_size > 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 		max_buf_size = 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	if (request_firmware(&fw, fwname, FWDEV(client)) != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 		v4l_err(client, "unable to open firmware %s\n", fwname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	start_fw_load(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	buffer[0] = 0x08;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	buffer[1] = 0x02;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	size = fw->size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	ptr = fw->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	while (size > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 		int len = min(max_buf_size - 2, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 		memcpy(buffer + 2, ptr, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 		retval = fw_write(client, buffer, len + 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 		if (retval < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 			release_firmware(fw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 			return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 		size -= len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 		ptr += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	end_fw_load(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	size = fw->size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	release_firmware(fw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	if (is_cx2388x(state)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 		/* Restore GPIO configuration after f/w load */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 		cx25840_write(client, 0x160, gpio_oe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 		cx25840_write(client, 0x164, gpio_da);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	return check_fw_load(client, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) MODULE_FIRMWARE(CX2388x_FIRMWARE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) MODULE_FIRMWARE(CX231xx_FIRMWARE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) MODULE_FIRMWARE(CX25840_FIRMWARE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)