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) * Simple driver for Texas Instruments LM3639 Backlight + Flash LED driver chip
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4) * Copyright (C) 2012 Texas Instruments
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <linux/i2c.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/leds.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/backlight.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/interrupt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/regmap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/platform_data/lm3639_bl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #define REG_DEV_ID	0x00
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #define REG_CHECKSUM	0x01
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #define REG_BL_CONF_1	0x02
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #define REG_BL_CONF_2	0x03
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #define REG_BL_CONF_3	0x04
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #define REG_BL_CONF_4	0x05
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) #define REG_FL_CONF_1	0x06
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) #define REG_FL_CONF_2	0x07
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) #define REG_FL_CONF_3	0x08
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) #define REG_IO_CTRL	0x09
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) #define REG_ENABLE	0x0A
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) #define REG_FLAG	0x0B
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) #define REG_MAX		REG_FLAG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) struct lm3639_chip_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	struct device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 	struct lm3639_platform_data *pdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	struct backlight_device *bled;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 	struct led_classdev cdev_flash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	struct led_classdev cdev_torch;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	struct regmap *regmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	unsigned int bled_mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	unsigned int bled_map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	unsigned int last_flag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) /* initialize chip */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) static int lm3639_chip_init(struct lm3639_chip_data *pchip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	unsigned int reg_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	struct lm3639_platform_data *pdata = pchip->pdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	/* input pins config. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	ret =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	    regmap_update_bits(pchip->regmap, REG_BL_CONF_1, 0x08,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 			       pdata->pin_pwm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	reg_val = (pdata->pin_pwm & 0x40) | pdata->pin_strobe | pdata->pin_tx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	ret = regmap_update_bits(pchip->regmap, REG_IO_CTRL, 0x7C, reg_val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	/* init brightness */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	ret = regmap_write(pchip->regmap, REG_BL_CONF_4, pdata->init_brt_led);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	ret = regmap_write(pchip->regmap, REG_BL_CONF_3, pdata->init_brt_led);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	/* output pins config. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	if (!pdata->init_brt_led) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 		reg_val = pdata->fled_pins;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 		reg_val |= pdata->bled_pins;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 		reg_val = pdata->fled_pins;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 		reg_val |= pdata->bled_pins | 0x01;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	ret = regmap_update_bits(pchip->regmap, REG_ENABLE, 0x79, reg_val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	dev_err(pchip->dev, "i2c failed to access register\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) /* update and get brightness */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) static int lm3639_bled_update_status(struct backlight_device *bl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	unsigned int reg_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	struct lm3639_chip_data *pchip = bl_get_data(bl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	struct lm3639_platform_data *pdata = pchip->pdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	ret = regmap_read(pchip->regmap, REG_FLAG, &reg_val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	if (reg_val != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 		dev_info(pchip->dev, "last flag is 0x%x\n", reg_val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	/* pwm control */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	if (pdata->pin_pwm) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 		if (pdata->pwm_set_intensity)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 			pdata->pwm_set_intensity(bl->props.brightness,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 						 pdata->max_brt_led);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 			dev_err(pchip->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 				"No pwm control func. in plat-data\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 		return bl->props.brightness;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	/* i2c control and set brigtness */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	ret = regmap_write(pchip->regmap, REG_BL_CONF_4, bl->props.brightness);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	ret = regmap_write(pchip->regmap, REG_BL_CONF_3, bl->props.brightness);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	if (!bl->props.brightness)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 		ret = regmap_update_bits(pchip->regmap, REG_ENABLE, 0x01, 0x00);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 		ret = regmap_update_bits(pchip->regmap, REG_ENABLE, 0x01, 0x01);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	return bl->props.brightness;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	dev_err(pchip->dev, "i2c failed to access registers\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	return bl->props.brightness;
^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) static int lm3639_bled_get_brightness(struct backlight_device *bl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	unsigned int reg_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	struct lm3639_chip_data *pchip = bl_get_data(bl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	struct lm3639_platform_data *pdata = pchip->pdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	if (pdata->pin_pwm) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 		if (pdata->pwm_get_intensity)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 			bl->props.brightness = pdata->pwm_get_intensity();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 			dev_err(pchip->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 				"No pwm control func. in plat-data\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 		return bl->props.brightness;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	ret = regmap_read(pchip->regmap, REG_BL_CONF_1, &reg_val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	if (reg_val & 0x10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 		ret = regmap_read(pchip->regmap, REG_BL_CONF_4, &reg_val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 		ret = regmap_read(pchip->regmap, REG_BL_CONF_3, &reg_val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	bl->props.brightness = reg_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	return bl->props.brightness;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	dev_err(pchip->dev, "i2c failed to access register\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	return bl->props.brightness;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) static const struct backlight_ops lm3639_bled_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	.options = BL_CORE_SUSPENDRESUME,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	.update_status = lm3639_bled_update_status,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	.get_brightness = lm3639_bled_get_brightness,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) /* backlight mapping mode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) static ssize_t lm3639_bled_mode_store(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 				      struct device_attribute *devAttr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 				      const char *buf, size_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	ssize_t ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 	struct lm3639_chip_data *pchip = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	unsigned int state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	ret = kstrtouint(buf, 10, &state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 		goto out_input;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	if (!state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 		ret =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 		    regmap_update_bits(pchip->regmap, REG_BL_CONF_1, 0x10,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 				       0x00);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 		ret =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 		    regmap_update_bits(pchip->regmap, REG_BL_CONF_1, 0x10,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 				       0x10);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 	return size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	dev_err(pchip->dev, "%s:i2c access fail to register\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) out_input:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 	dev_err(pchip->dev, "%s:input conversion fail\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 
^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) static DEVICE_ATTR(bled_mode, S_IWUSR, NULL, lm3639_bled_mode_store);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) /* torch */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) static void lm3639_torch_brightness_set(struct led_classdev *cdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 					enum led_brightness brightness)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	unsigned int reg_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 	struct lm3639_chip_data *pchip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 	pchip = container_of(cdev, struct lm3639_chip_data, cdev_torch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 	ret = regmap_read(pchip->regmap, REG_FLAG, &reg_val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 	if (reg_val != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 		dev_info(pchip->dev, "last flag is 0x%x\n", reg_val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 	/* brightness 0 means off state */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	if (!brightness) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 		ret = regmap_update_bits(pchip->regmap, REG_ENABLE, 0x06, 0x00);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 	ret = regmap_update_bits(pchip->regmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 				 REG_FL_CONF_1, 0x70, (brightness - 1) << 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 	ret = regmap_update_bits(pchip->regmap, REG_ENABLE, 0x06, 0x02);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 	return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 	dev_err(pchip->dev, "i2c failed to access register\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) /* flash */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) static void lm3639_flash_brightness_set(struct led_classdev *cdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 					enum led_brightness brightness)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 	unsigned int reg_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	struct lm3639_chip_data *pchip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 	pchip = container_of(cdev, struct lm3639_chip_data, cdev_flash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 	ret = regmap_read(pchip->regmap, REG_FLAG, &reg_val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 	if (reg_val != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 		dev_info(pchip->dev, "last flag is 0x%x\n", reg_val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	/* torch off before flash control */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 	ret = regmap_update_bits(pchip->regmap, REG_ENABLE, 0x06, 0x00);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 	/* brightness 0 means off state */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 	if (!brightness)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 	ret = regmap_update_bits(pchip->regmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 				 REG_FL_CONF_1, 0x0F, brightness - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 	ret = regmap_update_bits(pchip->regmap, REG_ENABLE, 0x06, 0x06);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 	dev_err(pchip->dev, "i2c failed to access register\n");
^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 const struct regmap_config lm3639_regmap = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 	.reg_bits = 8,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 	.val_bits = 8,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 	.max_register = REG_MAX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) static int lm3639_probe(struct i2c_client *client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 				  const struct i2c_device_id *id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 	struct lm3639_chip_data *pchip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 	struct lm3639_platform_data *pdata = dev_get_platdata(&client->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 	struct backlight_properties props;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 		dev_err(&client->dev, "i2c functionality check fail.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 		return -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 	if (pdata == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 		dev_err(&client->dev, "Needs Platform Data.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 		return -ENODATA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 	pchip = devm_kzalloc(&client->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 			     sizeof(struct lm3639_chip_data), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 	if (!pchip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 	pchip->pdata = pdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 	pchip->dev = &client->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 	pchip->regmap = devm_regmap_init_i2c(client, &lm3639_regmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 	if (IS_ERR(pchip->regmap)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 		ret = PTR_ERR(pchip->regmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 		dev_err(&client->dev, "fail : allocate register map: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 			ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 	i2c_set_clientdata(client, pchip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 	/* chip initialize */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 	ret = lm3639_chip_init(pchip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 		dev_err(&client->dev, "fail : chip init\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 		goto err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 	/* backlight */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 	props.type = BACKLIGHT_RAW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 	props.brightness = pdata->init_brt_led;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 	props.max_brightness = pdata->max_brt_led;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 	pchip->bled =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 	    devm_backlight_device_register(pchip->dev, "lm3639_bled",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 					   pchip->dev, pchip, &lm3639_bled_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 					   &props);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 	if (IS_ERR(pchip->bled)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 		dev_err(&client->dev, "fail : backlight register\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 		ret = PTR_ERR(pchip->bled);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 		goto err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 	ret = device_create_file(&(pchip->bled->dev), &dev_attr_bled_mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 		dev_err(&client->dev, "failed : add sysfs entries\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 		goto err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 	/* flash */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 	pchip->cdev_flash.name = "lm3639_flash";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 	pchip->cdev_flash.max_brightness = 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 	pchip->cdev_flash.brightness_set = lm3639_flash_brightness_set;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 	ret = led_classdev_register((struct device *)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 				    &client->dev, &pchip->cdev_flash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 		dev_err(&client->dev, "fail : flash register\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 		goto err_flash;
^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) 	/* torch */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 	pchip->cdev_torch.name = "lm3639_torch";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 	pchip->cdev_torch.max_brightness = 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 	pchip->cdev_torch.brightness_set = lm3639_torch_brightness_set;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 	ret = led_classdev_register((struct device *)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 				    &client->dev, &pchip->cdev_torch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 		dev_err(&client->dev, "fail : torch register\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 		goto err_torch;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) err_torch:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 	led_classdev_unregister(&pchip->cdev_flash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) err_flash:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 	device_remove_file(&(pchip->bled->dev), &dev_attr_bled_mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) err_out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) static int lm3639_remove(struct i2c_client *client)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 	struct lm3639_chip_data *pchip = i2c_get_clientdata(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 	regmap_write(pchip->regmap, REG_ENABLE, 0x00);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 	led_classdev_unregister(&pchip->cdev_torch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 	led_classdev_unregister(&pchip->cdev_flash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 	if (pchip->bled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 		device_remove_file(&(pchip->bled->dev), &dev_attr_bled_mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) static const struct i2c_device_id lm3639_id[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 	{LM3639_NAME, 0},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 	{}
^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) MODULE_DEVICE_TABLE(i2c, lm3639_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) static struct i2c_driver lm3639_i2c_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 	.driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 		   .name = LM3639_NAME,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 		   },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 	.probe = lm3639_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 	.remove = lm3639_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 	.id_table = lm3639_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) module_i2c_driver(lm3639_i2c_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) MODULE_DESCRIPTION("Texas Instruments Backlight+Flash LED driver for LM3639");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) MODULE_AUTHOR("Daniel Jeong <gshark.jeong@gmail.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) MODULE_AUTHOR("Ldd Mlp <ldd-mlp@list.ti.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) MODULE_LICENSE("GPL v2");