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) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * Backlight driver for Analog Devices ADP5520/ADP5501 MFD PMICs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright 2009 Analog Devices Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/fb.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/backlight.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/mfd/adp5520.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) struct adp5520_bl {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) 	struct device *master;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) 	struct adp5520_backlight_platform_data *pdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 	struct mutex lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 	unsigned long cached_daylight_max;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 	int id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 	int current_brightness;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) static int adp5520_bl_set(struct backlight_device *bl, int brightness)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 	struct adp5520_bl *data = bl_get_data(bl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 	struct device *master = data->master;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 	if (data->pdata->en_ambl_sens) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 		if ((brightness > 0) && (brightness < ADP5020_MAX_BRIGHTNESS)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 			/* Disable Ambient Light auto adjust */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 			ret |= adp5520_clr_bits(master, ADP5520_BL_CONTROL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 					ADP5520_BL_AUTO_ADJ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 			ret |= adp5520_write(master, ADP5520_DAYLIGHT_MAX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 					brightness);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 			 * MAX_BRIGHTNESS -> Enable Ambient Light auto adjust
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 			 * restore daylight l3 sysfs brightness
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 			ret |= adp5520_write(master, ADP5520_DAYLIGHT_MAX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 					 data->cached_daylight_max);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 			ret |= adp5520_set_bits(master, ADP5520_BL_CONTROL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 					 ADP5520_BL_AUTO_ADJ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 		ret |= adp5520_write(master, ADP5520_DAYLIGHT_MAX, brightness);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	if (data->current_brightness && brightness == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 		ret |= adp5520_set_bits(master,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 				ADP5520_MODE_STATUS, ADP5520_DIM_EN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	else if (data->current_brightness == 0 && brightness)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 		ret |= adp5520_clr_bits(master,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 				ADP5520_MODE_STATUS, ADP5520_DIM_EN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 		data->current_brightness = brightness;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) static int adp5520_bl_update_status(struct backlight_device *bl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	return adp5520_bl_set(bl, backlight_get_brightness(bl));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) static int adp5520_bl_get_brightness(struct backlight_device *bl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	struct adp5520_bl *data = bl_get_data(bl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	uint8_t reg_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	error = adp5520_read(data->master, ADP5520_BL_VALUE, &reg_val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	return error ? data->current_brightness : reg_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) static const struct backlight_ops adp5520_bl_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	.update_status	= adp5520_bl_update_status,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	.get_brightness	= adp5520_bl_get_brightness,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) static int adp5520_bl_setup(struct backlight_device *bl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	struct adp5520_bl *data = bl_get_data(bl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	struct device *master = data->master;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	struct adp5520_backlight_platform_data *pdata = data->pdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	ret |= adp5520_write(master, ADP5520_DAYLIGHT_MAX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 				pdata->l1_daylight_max);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	ret |= adp5520_write(master, ADP5520_DAYLIGHT_DIM,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 				pdata->l1_daylight_dim);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	if (pdata->en_ambl_sens) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 		data->cached_daylight_max = pdata->l1_daylight_max;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 		ret |= adp5520_write(master, ADP5520_OFFICE_MAX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 				pdata->l2_office_max);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 		ret |= adp5520_write(master, ADP5520_OFFICE_DIM,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 				pdata->l2_office_dim);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 		ret |= adp5520_write(master, ADP5520_DARK_MAX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 				pdata->l3_dark_max);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 		ret |= adp5520_write(master, ADP5520_DARK_DIM,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 				pdata->l3_dark_dim);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 		ret |= adp5520_write(master, ADP5520_L2_TRIP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 				pdata->l2_trip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 		ret |= adp5520_write(master, ADP5520_L2_HYS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 				pdata->l2_hyst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 		ret |= adp5520_write(master, ADP5520_L3_TRIP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 				 pdata->l3_trip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 		ret |= adp5520_write(master, ADP5520_L3_HYS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 				pdata->l3_hyst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 		ret |= adp5520_write(master, ADP5520_ALS_CMPR_CFG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 				ALS_CMPR_CFG_VAL(pdata->abml_filt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 				ADP5520_L3_EN));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	ret |= adp5520_write(master, ADP5520_BL_CONTROL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 			BL_CTRL_VAL(pdata->fade_led_law,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 					pdata->en_ambl_sens));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	ret |= adp5520_write(master, ADP5520_BL_FADE, FADE_VAL(pdata->fade_in,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 			pdata->fade_out));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	ret |= adp5520_set_bits(master, ADP5520_MODE_STATUS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 			ADP5520_BL_EN | ADP5520_DIM_EN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) static ssize_t adp5520_show(struct device *dev, char *buf, int reg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	struct adp5520_bl *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	uint8_t reg_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	mutex_lock(&data->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	ret = adp5520_read(data->master, reg, &reg_val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	mutex_unlock(&data->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	return sprintf(buf, "%u\n", reg_val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) static ssize_t adp5520_store(struct device *dev, const char *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 			 size_t count, int reg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	struct adp5520_bl *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	unsigned long val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	ret = kstrtoul(buf, 10, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	mutex_lock(&data->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	adp5520_write(data->master, reg, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	mutex_unlock(&data->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) static ssize_t adp5520_bl_dark_max_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 			struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	return adp5520_show(dev, buf, ADP5520_DARK_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) static ssize_t adp5520_bl_dark_max_store(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 			struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 			const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	return adp5520_store(dev, buf, count, ADP5520_DARK_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) static DEVICE_ATTR(dark_max, 0664, adp5520_bl_dark_max_show,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 			adp5520_bl_dark_max_store);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) static ssize_t adp5520_bl_office_max_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 			struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	return adp5520_show(dev, buf, ADP5520_OFFICE_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) static ssize_t adp5520_bl_office_max_store(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 			struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 			const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 	return adp5520_store(dev, buf, count, ADP5520_OFFICE_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) static DEVICE_ATTR(office_max, 0664, adp5520_bl_office_max_show,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 			adp5520_bl_office_max_store);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) static ssize_t adp5520_bl_daylight_max_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 			struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 	return adp5520_show(dev, buf, ADP5520_DAYLIGHT_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) static ssize_t adp5520_bl_daylight_max_store(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 			struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 			const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 	struct adp5520_bl *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 	ret = kstrtoul(buf, 10, &data->cached_daylight_max);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 	return adp5520_store(dev, buf, count, ADP5520_DAYLIGHT_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) static DEVICE_ATTR(daylight_max, 0664, adp5520_bl_daylight_max_show,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 			adp5520_bl_daylight_max_store);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) static ssize_t adp5520_bl_dark_dim_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 			struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 	return adp5520_show(dev, buf, ADP5520_DARK_DIM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) static ssize_t adp5520_bl_dark_dim_store(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 			struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 			const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 	return adp5520_store(dev, buf, count, ADP5520_DARK_DIM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) static DEVICE_ATTR(dark_dim, 0664, adp5520_bl_dark_dim_show,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 			adp5520_bl_dark_dim_store);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) static ssize_t adp5520_bl_office_dim_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 			struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 	return adp5520_show(dev, buf, ADP5520_OFFICE_DIM);
^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) static ssize_t adp5520_bl_office_dim_store(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 			struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 			const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 	return adp5520_store(dev, buf, count, ADP5520_OFFICE_DIM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) static DEVICE_ATTR(office_dim, 0664, adp5520_bl_office_dim_show,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 			adp5520_bl_office_dim_store);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) static ssize_t adp5520_bl_daylight_dim_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 			struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 	return adp5520_show(dev, buf, ADP5520_DAYLIGHT_DIM);
^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) static ssize_t adp5520_bl_daylight_dim_store(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 			struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 			const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	return adp5520_store(dev, buf, count, ADP5520_DAYLIGHT_DIM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) static DEVICE_ATTR(daylight_dim, 0664, adp5520_bl_daylight_dim_show,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 			adp5520_bl_daylight_dim_store);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) static struct attribute *adp5520_bl_attributes[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 	&dev_attr_dark_max.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 	&dev_attr_dark_dim.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 	&dev_attr_office_max.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	&dev_attr_office_dim.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	&dev_attr_daylight_max.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 	&dev_attr_daylight_dim.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 	NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) static const struct attribute_group adp5520_bl_attr_group = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 	.attrs = adp5520_bl_attributes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) static int adp5520_bl_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 	struct backlight_properties props;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	struct backlight_device *bl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 	struct adp5520_bl *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 	data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	if (data == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 	data->master = pdev->dev.parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 	data->pdata = dev_get_platdata(&pdev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 	if (data->pdata  == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 		dev_err(&pdev->dev, "missing platform data\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 		return -ENODEV;
^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) 	data->id = pdev->id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 	data->current_brightness = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 	mutex_init(&data->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 	memset(&props, 0, sizeof(struct backlight_properties));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 	props.type = BACKLIGHT_RAW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 	props.max_brightness = ADP5020_MAX_BRIGHTNESS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 	bl = devm_backlight_device_register(&pdev->dev, pdev->name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 					data->master, data, &adp5520_bl_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 					&props);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 	if (IS_ERR(bl)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 		dev_err(&pdev->dev, "failed to register backlight\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 		return PTR_ERR(bl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 	bl->props.brightness = ADP5020_MAX_BRIGHTNESS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 	if (data->pdata->en_ambl_sens)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 		ret = sysfs_create_group(&bl->dev.kobj,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 			&adp5520_bl_attr_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 		dev_err(&pdev->dev, "failed to register sysfs\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 	platform_set_drvdata(pdev, bl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 	ret = adp5520_bl_setup(bl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 		dev_err(&pdev->dev, "failed to setup\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 		if (data->pdata->en_ambl_sens)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 			sysfs_remove_group(&bl->dev.kobj,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 					&adp5520_bl_attr_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 		return ret;
^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) 	backlight_update_status(bl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) static int adp5520_bl_remove(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 	struct backlight_device *bl = platform_get_drvdata(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 	struct adp5520_bl *data = bl_get_data(bl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 	adp5520_clr_bits(data->master, ADP5520_MODE_STATUS, ADP5520_BL_EN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 	if (data->pdata->en_ambl_sens)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 		sysfs_remove_group(&bl->dev.kobj,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 				&adp5520_bl_attr_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) #ifdef CONFIG_PM_SLEEP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) static int adp5520_bl_suspend(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 	struct backlight_device *bl = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 	return adp5520_bl_set(bl, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) static int adp5520_bl_resume(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 	struct backlight_device *bl = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 	backlight_update_status(bl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) static SIMPLE_DEV_PM_OPS(adp5520_bl_pm_ops, adp5520_bl_suspend,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 			adp5520_bl_resume);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) static struct platform_driver adp5520_bl_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 	.driver		= {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 		.name	= "adp5520-backlight",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 		.pm	= &adp5520_bl_pm_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 	.probe		= adp5520_bl_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 	.remove		= adp5520_bl_remove,
^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) module_platform_driver(adp5520_bl_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) MODULE_AUTHOR("Michael Hennerich <michael.hennerich@analog.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) MODULE_DESCRIPTION("ADP5520(01) Backlight Driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) MODULE_ALIAS("platform:adp5520-backlight");