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-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * Copyright 2011 bct electronic GmbH
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  * Copyright 2013 Qtechnology/AS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * Author: Peter Meerwald <p.meerwald@bct-electronic.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * Author: Ricardo Ribalda <ribalda@kernel.org>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  * Based on leds-pca955x.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11)  * LED driver for the PCA9633 I2C LED driver (7-bit slave address 0x62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12)  * LED driver for the PCA9634/5 I2C LED driver (7-bit slave address set by hw.)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14)  * Note that hardware blinking violates the leds infrastructure driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15)  * interface since the hardware only supports blinking all LEDs with the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16)  * same delay_on/delay_off rates.  That is, only the LEDs that are set to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17)  * blink will actually blink but all LEDs that are set to blink will blink
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18)  * in identical fashion.  The delay_on/delay_off values of the last LED
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19)  * that is set to blink will be used for all of the blinking LEDs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20)  * Hardware blinking is disabled by default but can be enabled by setting
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21)  * the 'blink_type' member in the platform_data struct to 'PCA963X_HW_BLINK'
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22)  * or by adding the 'nxp,hw-blink' property to the DTS.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) #include <linux/ctype.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) #include <linux/leds.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) #include <linux/i2c.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) #include <linux/property.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) #include <linux/of.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) /* LED select registers determine the source that drives LED outputs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) #define PCA963X_LED_OFF		0x0	/* LED driver off */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) #define PCA963X_LED_ON		0x1	/* LED driver on */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) #define PCA963X_LED_PWM		0x2	/* Controlled through PWM */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) #define PCA963X_LED_GRP_PWM	0x3	/* Controlled through PWM/GRPPWM */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) #define PCA963X_MODE2_OUTDRV	0x04	/* Open-drain or totem pole */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) #define PCA963X_MODE2_INVRT	0x10	/* Normal or inverted direction */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) #define PCA963X_MODE2_DMBLNK	0x20	/* Enable blinking */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) #define PCA963X_MODE1		0x00
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) #define PCA963X_MODE2		0x01
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) #define PCA963X_PWM_BASE	0x02
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) enum pca963x_type {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	pca9633,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	pca9634,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	pca9635,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) struct pca963x_chipdef {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	u8			grppwm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	u8			grpfreq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	u8			ledout_base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	int			n_leds;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	unsigned int		scaling;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) static struct pca963x_chipdef pca963x_chipdefs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	[pca9633] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 		.grppwm		= 0x6,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 		.grpfreq	= 0x7,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 		.ledout_base	= 0x8,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 		.n_leds		= 4,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	[pca9634] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 		.grppwm		= 0xa,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 		.grpfreq	= 0xb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 		.ledout_base	= 0xc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 		.n_leds		= 8,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	[pca9635] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 		.grppwm		= 0x12,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 		.grpfreq	= 0x13,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 		.ledout_base	= 0x14,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 		.n_leds		= 16,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	},
^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) /* Total blink period in milliseconds */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) #define PCA963X_BLINK_PERIOD_MIN	42
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) #define PCA963X_BLINK_PERIOD_MAX	10667
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) static const struct i2c_device_id pca963x_id[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	{ "pca9632", pca9633 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	{ "pca9633", pca9633 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	{ "pca9634", pca9634 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	{ "pca9635", pca9635 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	{ }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) MODULE_DEVICE_TABLE(i2c, pca963x_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) struct pca963x;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) struct pca963x_led {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	struct pca963x *chip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	struct led_classdev led_cdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	int led_num; /* 0 .. 15 potentially */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	u8 gdc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	u8 gfrq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) struct pca963x {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	struct pca963x_chipdef *chipdef;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	struct mutex mutex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	struct i2c_client *client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	unsigned long leds_on;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	struct pca963x_led leds[];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) static int pca963x_brightness(struct pca963x_led *led,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 			      enum led_brightness brightness)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	struct i2c_client *client = led->chip->client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	struct pca963x_chipdef *chipdef = led->chip->chipdef;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	u8 ledout_addr, ledout, mask, val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	int shift;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	ledout_addr = chipdef->ledout_base + (led->led_num / 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	shift = 2 * (led->led_num % 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	mask = 0x3 << shift;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	ledout = i2c_smbus_read_byte_data(client, ledout_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	switch (brightness) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	case LED_FULL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 		val = (ledout & ~mask) | (PCA963X_LED_ON << shift);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 		ret = i2c_smbus_write_byte_data(client, ledout_addr, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	case LED_OFF:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 		val = ledout & ~mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 		ret = i2c_smbus_write_byte_data(client, ledout_addr, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 		ret = i2c_smbus_write_byte_data(client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 						PCA963X_PWM_BASE +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 						led->led_num,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 						brightness);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 		val = (ledout & ~mask) | (PCA963X_LED_PWM << shift);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 		ret = i2c_smbus_write_byte_data(client, ledout_addr, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	return ret;
^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) static void pca963x_blink(struct pca963x_led *led)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	struct i2c_client *client = led->chip->client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	struct pca963x_chipdef *chipdef = led->chip->chipdef;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	u8 ledout_addr, ledout, mask, val, mode2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	int shift;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	ledout_addr = chipdef->ledout_base + (led->led_num / 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	shift = 2 * (led->led_num % 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	mask = 0x3 << shift;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	mode2 = i2c_smbus_read_byte_data(client, PCA963X_MODE2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	i2c_smbus_write_byte_data(client, chipdef->grppwm, led->gdc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	i2c_smbus_write_byte_data(client, chipdef->grpfreq, led->gfrq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	if (!(mode2 & PCA963X_MODE2_DMBLNK))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 		i2c_smbus_write_byte_data(client, PCA963X_MODE2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 					  mode2 | PCA963X_MODE2_DMBLNK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	mutex_lock(&led->chip->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	ledout = i2c_smbus_read_byte_data(client, ledout_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	if ((ledout & mask) != (PCA963X_LED_GRP_PWM << shift)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 		val = (ledout & ~mask) | (PCA963X_LED_GRP_PWM << shift);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 		i2c_smbus_write_byte_data(client, ledout_addr, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	mutex_unlock(&led->chip->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) static int pca963x_power_state(struct pca963x_led *led)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	struct i2c_client *client = led->chip->client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	unsigned long *leds_on = &led->chip->leds_on;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	unsigned long cached_leds = *leds_on;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	if (led->led_cdev.brightness)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 		set_bit(led->led_num, leds_on);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 		clear_bit(led->led_num, leds_on);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	if (!(*leds_on) != !cached_leds)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 		return i2c_smbus_write_byte_data(client, PCA963X_MODE1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 						 *leds_on ? 0 : BIT(4));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) static int pca963x_led_set(struct led_classdev *led_cdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 			   enum led_brightness value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	struct pca963x_led *led;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	led = container_of(led_cdev, struct pca963x_led, led_cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 	mutex_lock(&led->chip->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 	ret = pca963x_brightness(led, value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 		goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 	ret = pca963x_power_state(led);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	mutex_unlock(&led->chip->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) static unsigned int pca963x_period_scale(struct pca963x_led *led,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 					 unsigned int val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 	unsigned int scaling = led->chip->chipdef->scaling;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 	return scaling ? DIV_ROUND_CLOSEST(val * scaling, 1000) : val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) static int pca963x_blink_set(struct led_classdev *led_cdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 			     unsigned long *delay_on, unsigned long *delay_off)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	unsigned long time_on, time_off, period;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 	struct pca963x_led *led;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 	u8 gdc, gfrq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 	led = container_of(led_cdev, struct pca963x_led, led_cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 	time_on = *delay_on;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 	time_off = *delay_off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	/* If both zero, pick reasonable defaults of 500ms each */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 	if (!time_on && !time_off) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 		time_on = 500;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 		time_off = 500;
^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) 	period = pca963x_period_scale(led, time_on + time_off);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 	/* If period not supported by hardware, default to someting sane. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 	if ((period < PCA963X_BLINK_PERIOD_MIN) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 	    (period > PCA963X_BLINK_PERIOD_MAX)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 		time_on = 500;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 		time_off = 500;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 		period = pca963x_period_scale(led, 1000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	 * From manual: duty cycle = (GDC / 256) ->
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 	 *	(time_on / period) = (GDC / 256) ->
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 	 *		GDC = ((time_on * 256) / period)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 	gdc = (pca963x_period_scale(led, time_on) * 256) / period;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 	 * From manual: period = ((GFRQ + 1) / 24) in seconds.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 	 * So, period (in ms) = (((GFRQ + 1) / 24) * 1000) ->
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	 *		GFRQ = ((period * 24 / 1000) - 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 	gfrq = (period * 24 / 1000) - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 	led->gdc = gdc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 	led->gfrq = gfrq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 	pca963x_blink(led);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 	*delay_on = time_on;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 	*delay_off = time_off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) static int pca963x_register_leds(struct i2c_client *client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 				 struct pca963x *chip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	struct pca963x_chipdef *chipdef = chip->chipdef;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 	struct pca963x_led *led = chip->leds;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 	struct device *dev = &client->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 	struct fwnode_handle *child;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 	bool hw_blink;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 	s32 mode2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 	u32 reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 	if (device_property_read_u32(dev, "nxp,period-scale",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 				     &chipdef->scaling))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 		chipdef->scaling = 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 	hw_blink = device_property_read_bool(dev, "nxp,hw-blink");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 	mode2 = i2c_smbus_read_byte_data(client, PCA963X_MODE2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 	if (mode2 < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 		return mode2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 	/* default to open-drain unless totem pole (push-pull) is specified */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 	if (device_property_read_bool(dev, "nxp,totem-pole"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 		mode2 |= PCA963X_MODE2_OUTDRV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 		mode2 &= ~PCA963X_MODE2_OUTDRV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 	/* default to non-inverted output, unless inverted is specified */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 	if (device_property_read_bool(dev, "nxp,inverted-out"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 		mode2 |= PCA963X_MODE2_INVRT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 		mode2 &= ~PCA963X_MODE2_INVRT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 	ret = i2c_smbus_write_byte_data(client, PCA963X_MODE2, mode2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 	device_for_each_child_node(dev, child) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 		struct led_init_data init_data = {};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 		char default_label[32];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 		ret = fwnode_property_read_u32(child, "reg", &reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 		if (ret || reg >= chipdef->n_leds) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 			dev_err(dev, "Invalid 'reg' property for node %pfw\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 				child);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 			ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 			goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 		led->led_num = reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 		led->chip = chip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 		led->led_cdev.brightness_set_blocking = pca963x_led_set;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 		if (hw_blink)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 			led->led_cdev.blink_set = pca963x_blink_set;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 		init_data.fwnode = child;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 		/* for backwards compatibility */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 		init_data.devicename = "pca963x";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 		snprintf(default_label, sizeof(default_label), "%d:%.2x:%u",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 			 client->adapter->nr, client->addr, reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 		init_data.default_label = default_label;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 		ret = devm_led_classdev_register_ext(dev, &led->led_cdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 						     &init_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 		if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 			dev_err(dev, "Failed to register LED for node %pfw\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 				child);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 			goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 		++led;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 	fwnode_handle_put(child);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) static const struct of_device_id of_pca963x_match[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 	{ .compatible = "nxp,pca9632", },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 	{ .compatible = "nxp,pca9633", },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 	{ .compatible = "nxp,pca9634", },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 	{ .compatible = "nxp,pca9635", },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 	{},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) MODULE_DEVICE_TABLE(of, of_pca963x_match);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) static int pca963x_probe(struct i2c_client *client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 			 const struct i2c_device_id *id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 	struct device *dev = &client->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 	struct pca963x_chipdef *chipdef;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 	struct pca963x *chip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 	int i, count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 	chipdef = &pca963x_chipdefs[id->driver_data];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 	count = device_get_child_node_count(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 	if (!count || count > chipdef->n_leds) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 		dev_err(dev, "Node %pfw must define between 1 and %d LEDs\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 			dev_fwnode(dev), chipdef->n_leds);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 	chip = devm_kzalloc(dev, struct_size(chip, leds, count), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 	if (!chip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 	i2c_set_clientdata(client, chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 	mutex_init(&chip->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 	chip->chipdef = chipdef;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 	chip->client = client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 	/* Turn off LEDs by default*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 	for (i = 0; i < chipdef->n_leds / 4; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 		i2c_smbus_write_byte_data(client, chipdef->ledout_base + i, 0x00);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 	/* Disable LED all-call address, and power down initially */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 	i2c_smbus_write_byte_data(client, PCA963X_MODE1, BIT(4));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 	return pca963x_register_leds(client, chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) static struct i2c_driver pca963x_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 	.driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 		.name	= "leds-pca963x",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 		.of_match_table = of_pca963x_match,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 	.probe	= pca963x_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 	.id_table = pca963x_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) module_i2c_driver(pca963x_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) MODULE_AUTHOR("Peter Meerwald <p.meerwald@bct-electronic.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) MODULE_DESCRIPTION("PCA963X LED driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) MODULE_LICENSE("GPL v2");