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) // Copyright (c) 2018 Crane Merchandising Systems. All rights reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3) // Copyright (C) 2018 Oleh Kravchenko <oleg@kaa.org.ua>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6) #include <linux/leds.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <linux/of_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/spi/spi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/workqueue.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13)  *  CR0014114 SPI protocol descrtiption:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14)  *  +----+-----------------------------------+----+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15)  *  | CMD|             BRIGHTNESS            |CRC |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16)  *  +----+-----------------------------------+----+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17)  *  |    | LED0| LED1| LED2| LED3| LED4| LED5|    |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18)  *  |    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+    |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19)  *  |    |R|G|B|R|G|B|R|G|B|R|G|B|R|G|B|R|G|B|    |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20)  *  | 1  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 1  |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21)  *  |    |1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|    |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22)  *  |    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+    |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23)  *  |    |               18                  |    |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24)  *  +----+-----------------------------------+----+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25)  *  |                    20                       |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26)  *  +---------------------------------------------+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28)  *  PS: Boards can be connected to the chain:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29)  *      SPI -> board0 -> board1 -> board2 ..
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) /* CR0014114 SPI commands */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) #define CR_SET_BRIGHTNESS	0x80
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) #define CR_INIT_REENUMERATE	0x81
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) #define CR_NEXT_REENUMERATE	0x82
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) /* CR0014114 default settings */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) #define CR_MAX_BRIGHTNESS	GENMASK(6, 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) #define CR_FW_DELAY_MSEC	10
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) #define CR_RECOUNT_DELAY	(HZ * 3600)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) #define CR_DEV_NAME		"cr0014114"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) struct cr0014114_led {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	struct cr0014114	*priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	struct led_classdev	ldev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	u8			brightness;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) struct cr0014114 {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	bool			do_recount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	size_t			count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	struct delayed_work	work;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	struct device		*dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	struct mutex		lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	struct spi_device	*spi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	u8			*buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	unsigned long		delay;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	struct cr0014114_led	leds[];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) static void cr0014114_calc_crc(u8 *buf, const size_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	size_t	i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	u8	crc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	for (i = 1, crc = 1; i < len - 1; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 		crc += buf[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	crc |= BIT(7);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	/* special case when CRC matches the SPI commands */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	if (crc == CR_SET_BRIGHTNESS ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	    crc == CR_INIT_REENUMERATE ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	    crc == CR_NEXT_REENUMERATE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 		crc = 0xfe;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	buf[len - 1] = crc;
^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) static int cr0014114_recount(struct cr0014114 *priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	int	ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	size_t	i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	u8	cmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	dev_dbg(priv->dev, "LEDs recount is started\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	cmd = CR_INIT_REENUMERATE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	ret = spi_write(priv->spi, &cmd, sizeof(cmd));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	cmd = CR_NEXT_REENUMERATE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	for (i = 0; i < priv->count; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 		msleep(CR_FW_DELAY_MSEC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 		ret = spi_write(priv->spi, &cmd, sizeof(cmd));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 			goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	dev_dbg(priv->dev, "LEDs recount is finished\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 		dev_err(priv->dev, "with error %d", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) static int cr0014114_sync(struct cr0014114 *priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	int		ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	size_t		i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	unsigned long	udelay, now = jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	/* to avoid SPI mistiming with firmware we should wait some time */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	if (time_after(priv->delay, now)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 		udelay = jiffies_to_usecs(priv->delay - now);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 		usleep_range(udelay, udelay + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	if (unlikely(priv->do_recount)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 		ret = cr0014114_recount(priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 			goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 		priv->do_recount = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 		msleep(CR_FW_DELAY_MSEC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	priv->buf[0] = CR_SET_BRIGHTNESS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	for (i = 0; i < priv->count; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 		priv->buf[i + 1] = priv->leds[i].brightness;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	cr0014114_calc_crc(priv->buf, priv->count + 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	ret = spi_write(priv->spi, priv->buf, priv->count + 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	priv->delay = jiffies + msecs_to_jiffies(CR_FW_DELAY_MSEC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	return ret;
^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) static void cr0014114_recount_work(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	int			ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	struct cr0014114	*priv = container_of(work,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 						     struct cr0014114,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 						     work.work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	mutex_lock(&priv->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	priv->do_recount = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	ret = cr0014114_sync(priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	mutex_unlock(&priv->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 		dev_warn(priv->dev, "sync of LEDs failed %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	schedule_delayed_work(&priv->work, CR_RECOUNT_DELAY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) static int cr0014114_set_sync(struct led_classdev *ldev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 			      enum led_brightness brightness)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	int			ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	struct cr0014114_led    *led = container_of(ldev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 						    struct cr0014114_led,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 						    ldev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	dev_dbg(led->priv->dev, "Set brightness to %d\n", brightness);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	mutex_lock(&led->priv->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	led->brightness = (u8)brightness;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	ret = cr0014114_sync(led->priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	mutex_unlock(&led->priv->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) static int cr0014114_probe_dt(struct cr0014114 *priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 	size_t			i = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	struct cr0014114_led	*led;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	struct fwnode_handle	*child;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 	struct led_init_data	init_data = {};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	int			ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	device_for_each_child_node(priv->dev, child) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 		led = &priv->leds[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 		led->priv			  = priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 		led->ldev.max_brightness	  = CR_MAX_BRIGHTNESS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 		led->ldev.brightness_set_blocking = cr0014114_set_sync;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 		init_data.fwnode = child;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 		init_data.devicename = CR_DEV_NAME;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 		init_data.default_label = ":";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 		ret = devm_led_classdev_register_ext(priv->dev, &led->ldev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 						     &init_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 		if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 			dev_err(priv->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 				"failed to register LED device, err %d", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 			fwnode_handle_put(child);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 		i++;
^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) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) static int cr0014114_probe(struct spi_device *spi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 	struct cr0014114	*priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 	size_t			count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 	int			ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	count = device_get_child_node_count(&spi->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 	if (!count) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 		dev_err(&spi->dev, "LEDs are not defined in device tree!");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 	priv = devm_kzalloc(&spi->dev, struct_size(priv, leds, count),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 			    GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 	if (!priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 	priv->buf = devm_kzalloc(&spi->dev, count + 2, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	if (!priv->buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	mutex_init(&priv->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 	INIT_DELAYED_WORK(&priv->work, cr0014114_recount_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 	priv->count	= count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 	priv->dev	= &spi->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 	priv->spi	= spi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	priv->delay	= jiffies -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 			  msecs_to_jiffies(CR_FW_DELAY_MSEC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 	priv->do_recount = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	ret = cr0014114_sync(priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 		dev_err(priv->dev, "first recount failed %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 	priv->do_recount = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 	ret = cr0014114_sync(priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 		dev_err(priv->dev, "second recount failed %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 	ret = cr0014114_probe_dt(priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	/* setup recount work to workaround buggy firmware */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 	schedule_delayed_work(&priv->work, CR_RECOUNT_DELAY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	spi_set_drvdata(spi, priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) static int cr0014114_remove(struct spi_device *spi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	struct cr0014114 *priv = spi_get_drvdata(spi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 	cancel_delayed_work_sync(&priv->work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 	mutex_destroy(&priv->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) static const struct of_device_id cr0014114_dt_ids[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 	{ .compatible = "crane,cr0014114", },
^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) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) MODULE_DEVICE_TABLE(of, cr0014114_dt_ids);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) static struct spi_driver cr0014114_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 	.probe		= cr0014114_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	.remove		= cr0014114_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 	.driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 		.name		= KBUILD_MODNAME,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 		.of_match_table	= cr0014114_dt_ids,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) module_spi_driver(cr0014114_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) MODULE_AUTHOR("Oleh Kravchenko <oleg@kaa.org.ua>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) MODULE_DESCRIPTION("cr0014114 LED driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) MODULE_LICENSE("GPL v2");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) MODULE_ALIAS("spi:cr0014114");