18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Backlight driver for the Kinetic KTD253 48c2ecf20Sopenharmony_ci * Based on code and know-how from the Samsung GT-S7710 58c2ecf20Sopenharmony_ci * Gareth Phillips <gareth.phillips@samsung.com> 68c2ecf20Sopenharmony_ci */ 78c2ecf20Sopenharmony_ci#include <linux/backlight.h> 88c2ecf20Sopenharmony_ci#include <linux/delay.h> 98c2ecf20Sopenharmony_ci#include <linux/err.h> 108c2ecf20Sopenharmony_ci#include <linux/fb.h> 118c2ecf20Sopenharmony_ci#include <linux/gpio/consumer.h> 128c2ecf20Sopenharmony_ci#include <linux/init.h> 138c2ecf20Sopenharmony_ci#include <linux/kernel.h> 148c2ecf20Sopenharmony_ci#include <linux/limits.h> 158c2ecf20Sopenharmony_ci#include <linux/module.h> 168c2ecf20Sopenharmony_ci#include <linux/of.h> 178c2ecf20Sopenharmony_ci#include <linux/platform_device.h> 188c2ecf20Sopenharmony_ci#include <linux/property.h> 198c2ecf20Sopenharmony_ci#include <linux/slab.h> 208c2ecf20Sopenharmony_ci 218c2ecf20Sopenharmony_ci/* Current ratio is n/32 from 1/32 to 32/32 */ 228c2ecf20Sopenharmony_ci#define KTD253_MIN_RATIO 1 238c2ecf20Sopenharmony_ci#define KTD253_MAX_RATIO 32 248c2ecf20Sopenharmony_ci#define KTD253_DEFAULT_RATIO 13 258c2ecf20Sopenharmony_ci 268c2ecf20Sopenharmony_ci#define KTD253_T_LOW_NS (200 + 10) /* Additional 10ns as safety factor */ 278c2ecf20Sopenharmony_ci#define KTD253_T_HIGH_NS (200 + 10) /* Additional 10ns as safety factor */ 288c2ecf20Sopenharmony_ci#define KTD253_T_OFF_CRIT_NS 100000 /* 100 us, now it doesn't look good */ 298c2ecf20Sopenharmony_ci#define KTD253_T_OFF_MS 3 308c2ecf20Sopenharmony_ci 318c2ecf20Sopenharmony_cistruct ktd253_backlight { 328c2ecf20Sopenharmony_ci struct device *dev; 338c2ecf20Sopenharmony_ci struct backlight_device *bl; 348c2ecf20Sopenharmony_ci struct gpio_desc *gpiod; 358c2ecf20Sopenharmony_ci u16 ratio; 368c2ecf20Sopenharmony_ci}; 378c2ecf20Sopenharmony_ci 388c2ecf20Sopenharmony_cistatic void ktd253_backlight_set_max_ratio(struct ktd253_backlight *ktd253) 398c2ecf20Sopenharmony_ci{ 408c2ecf20Sopenharmony_ci gpiod_set_value_cansleep(ktd253->gpiod, 1); 418c2ecf20Sopenharmony_ci ndelay(KTD253_T_HIGH_NS); 428c2ecf20Sopenharmony_ci /* We always fall back to this when we power on */ 438c2ecf20Sopenharmony_ci} 448c2ecf20Sopenharmony_ci 458c2ecf20Sopenharmony_cistatic int ktd253_backlight_stepdown(struct ktd253_backlight *ktd253) 468c2ecf20Sopenharmony_ci{ 478c2ecf20Sopenharmony_ci /* 488c2ecf20Sopenharmony_ci * These GPIO operations absolutely can NOT sleep so no _cansleep 498c2ecf20Sopenharmony_ci * suffixes, and no using GPIO expanders on slow buses for this! 508c2ecf20Sopenharmony_ci * 518c2ecf20Sopenharmony_ci * The maximum number of cycles of the loop is 32 so the time taken 528c2ecf20Sopenharmony_ci * should nominally be: 538c2ecf20Sopenharmony_ci * (T_LOW_NS + T_HIGH_NS + loop_time) * 32 548c2ecf20Sopenharmony_ci * 558c2ecf20Sopenharmony_ci * Architectures do not always support ndelay() and we will get a few us 568c2ecf20Sopenharmony_ci * instead. If we get to a critical time limit an interrupt has likely 578c2ecf20Sopenharmony_ci * occured in the low part of the loop and we need to restart from the 588c2ecf20Sopenharmony_ci * top so we have the backlight in a known state. 598c2ecf20Sopenharmony_ci */ 608c2ecf20Sopenharmony_ci u64 ns; 618c2ecf20Sopenharmony_ci 628c2ecf20Sopenharmony_ci ns = ktime_get_ns(); 638c2ecf20Sopenharmony_ci gpiod_set_value(ktd253->gpiod, 0); 648c2ecf20Sopenharmony_ci ndelay(KTD253_T_LOW_NS); 658c2ecf20Sopenharmony_ci gpiod_set_value(ktd253->gpiod, 1); 668c2ecf20Sopenharmony_ci ns = ktime_get_ns() - ns; 678c2ecf20Sopenharmony_ci if (ns >= KTD253_T_OFF_CRIT_NS) { 688c2ecf20Sopenharmony_ci dev_err(ktd253->dev, "PCM on backlight took too long (%llu ns)\n", ns); 698c2ecf20Sopenharmony_ci return -EAGAIN; 708c2ecf20Sopenharmony_ci } 718c2ecf20Sopenharmony_ci ndelay(KTD253_T_HIGH_NS); 728c2ecf20Sopenharmony_ci return 0; 738c2ecf20Sopenharmony_ci} 748c2ecf20Sopenharmony_ci 758c2ecf20Sopenharmony_cistatic int ktd253_backlight_update_status(struct backlight_device *bl) 768c2ecf20Sopenharmony_ci{ 778c2ecf20Sopenharmony_ci struct ktd253_backlight *ktd253 = bl_get_data(bl); 788c2ecf20Sopenharmony_ci int brightness = backlight_get_brightness(bl); 798c2ecf20Sopenharmony_ci u16 target_ratio; 808c2ecf20Sopenharmony_ci u16 current_ratio = ktd253->ratio; 818c2ecf20Sopenharmony_ci int ret; 828c2ecf20Sopenharmony_ci 838c2ecf20Sopenharmony_ci dev_dbg(ktd253->dev, "new brightness/ratio: %d/32\n", brightness); 848c2ecf20Sopenharmony_ci 858c2ecf20Sopenharmony_ci target_ratio = brightness; 868c2ecf20Sopenharmony_ci 878c2ecf20Sopenharmony_ci if (target_ratio == current_ratio) 888c2ecf20Sopenharmony_ci /* This is already right */ 898c2ecf20Sopenharmony_ci return 0; 908c2ecf20Sopenharmony_ci 918c2ecf20Sopenharmony_ci if (target_ratio == 0) { 928c2ecf20Sopenharmony_ci gpiod_set_value_cansleep(ktd253->gpiod, 0); 938c2ecf20Sopenharmony_ci /* 948c2ecf20Sopenharmony_ci * We need to keep the GPIO low for at least this long 958c2ecf20Sopenharmony_ci * to actually switch the KTD253 off. 968c2ecf20Sopenharmony_ci */ 978c2ecf20Sopenharmony_ci msleep(KTD253_T_OFF_MS); 988c2ecf20Sopenharmony_ci ktd253->ratio = 0; 998c2ecf20Sopenharmony_ci return 0; 1008c2ecf20Sopenharmony_ci } 1018c2ecf20Sopenharmony_ci 1028c2ecf20Sopenharmony_ci if (current_ratio == 0) { 1038c2ecf20Sopenharmony_ci ktd253_backlight_set_max_ratio(ktd253); 1048c2ecf20Sopenharmony_ci current_ratio = KTD253_MAX_RATIO; 1058c2ecf20Sopenharmony_ci } 1068c2ecf20Sopenharmony_ci 1078c2ecf20Sopenharmony_ci while (current_ratio != target_ratio) { 1088c2ecf20Sopenharmony_ci /* 1098c2ecf20Sopenharmony_ci * These GPIO operations absolutely can NOT sleep so no 1108c2ecf20Sopenharmony_ci * _cansleep suffixes, and no using GPIO expanders on 1118c2ecf20Sopenharmony_ci * slow buses for this! 1128c2ecf20Sopenharmony_ci */ 1138c2ecf20Sopenharmony_ci ret = ktd253_backlight_stepdown(ktd253); 1148c2ecf20Sopenharmony_ci if (ret == -EAGAIN) { 1158c2ecf20Sopenharmony_ci /* 1168c2ecf20Sopenharmony_ci * Something disturbed the backlight setting code when 1178c2ecf20Sopenharmony_ci * running so we need to bring the PWM back to a known 1188c2ecf20Sopenharmony_ci * state. This shouldn't happen too much. 1198c2ecf20Sopenharmony_ci */ 1208c2ecf20Sopenharmony_ci gpiod_set_value_cansleep(ktd253->gpiod, 0); 1218c2ecf20Sopenharmony_ci msleep(KTD253_T_OFF_MS); 1228c2ecf20Sopenharmony_ci ktd253_backlight_set_max_ratio(ktd253); 1238c2ecf20Sopenharmony_ci current_ratio = KTD253_MAX_RATIO; 1248c2ecf20Sopenharmony_ci } else if (current_ratio == KTD253_MIN_RATIO) { 1258c2ecf20Sopenharmony_ci /* After 1/32 we loop back to 32/32 */ 1268c2ecf20Sopenharmony_ci current_ratio = KTD253_MAX_RATIO; 1278c2ecf20Sopenharmony_ci } else { 1288c2ecf20Sopenharmony_ci current_ratio--; 1298c2ecf20Sopenharmony_ci } 1308c2ecf20Sopenharmony_ci } 1318c2ecf20Sopenharmony_ci ktd253->ratio = current_ratio; 1328c2ecf20Sopenharmony_ci 1338c2ecf20Sopenharmony_ci dev_dbg(ktd253->dev, "new ratio set to %d/32\n", target_ratio); 1348c2ecf20Sopenharmony_ci 1358c2ecf20Sopenharmony_ci return 0; 1368c2ecf20Sopenharmony_ci} 1378c2ecf20Sopenharmony_ci 1388c2ecf20Sopenharmony_cistatic const struct backlight_ops ktd253_backlight_ops = { 1398c2ecf20Sopenharmony_ci .options = BL_CORE_SUSPENDRESUME, 1408c2ecf20Sopenharmony_ci .update_status = ktd253_backlight_update_status, 1418c2ecf20Sopenharmony_ci}; 1428c2ecf20Sopenharmony_ci 1438c2ecf20Sopenharmony_cistatic int ktd253_backlight_probe(struct platform_device *pdev) 1448c2ecf20Sopenharmony_ci{ 1458c2ecf20Sopenharmony_ci struct device *dev = &pdev->dev; 1468c2ecf20Sopenharmony_ci struct backlight_device *bl; 1478c2ecf20Sopenharmony_ci struct ktd253_backlight *ktd253; 1488c2ecf20Sopenharmony_ci u32 max_brightness; 1498c2ecf20Sopenharmony_ci u32 brightness; 1508c2ecf20Sopenharmony_ci int ret; 1518c2ecf20Sopenharmony_ci 1528c2ecf20Sopenharmony_ci ktd253 = devm_kzalloc(dev, sizeof(*ktd253), GFP_KERNEL); 1538c2ecf20Sopenharmony_ci if (!ktd253) 1548c2ecf20Sopenharmony_ci return -ENOMEM; 1558c2ecf20Sopenharmony_ci ktd253->dev = dev; 1568c2ecf20Sopenharmony_ci 1578c2ecf20Sopenharmony_ci ret = device_property_read_u32(dev, "max-brightness", &max_brightness); 1588c2ecf20Sopenharmony_ci if (ret) 1598c2ecf20Sopenharmony_ci max_brightness = KTD253_MAX_RATIO; 1608c2ecf20Sopenharmony_ci if (max_brightness > KTD253_MAX_RATIO) { 1618c2ecf20Sopenharmony_ci /* Clamp brightness to hardware max */ 1628c2ecf20Sopenharmony_ci dev_err(dev, "illegal max brightness specified\n"); 1638c2ecf20Sopenharmony_ci max_brightness = KTD253_MAX_RATIO; 1648c2ecf20Sopenharmony_ci } 1658c2ecf20Sopenharmony_ci 1668c2ecf20Sopenharmony_ci ret = device_property_read_u32(dev, "default-brightness", &brightness); 1678c2ecf20Sopenharmony_ci if (ret) 1688c2ecf20Sopenharmony_ci brightness = KTD253_DEFAULT_RATIO; 1698c2ecf20Sopenharmony_ci if (brightness > max_brightness) { 1708c2ecf20Sopenharmony_ci /* Clamp default brightness to max brightness */ 1718c2ecf20Sopenharmony_ci dev_err(dev, "default brightness exceeds max brightness\n"); 1728c2ecf20Sopenharmony_ci brightness = max_brightness; 1738c2ecf20Sopenharmony_ci } 1748c2ecf20Sopenharmony_ci 1758c2ecf20Sopenharmony_ci if (brightness) 1768c2ecf20Sopenharmony_ci /* This will be the default ratio when the KTD253 is enabled */ 1778c2ecf20Sopenharmony_ci ktd253->ratio = KTD253_MAX_RATIO; 1788c2ecf20Sopenharmony_ci else 1798c2ecf20Sopenharmony_ci ktd253->ratio = 0; 1808c2ecf20Sopenharmony_ci 1818c2ecf20Sopenharmony_ci ktd253->gpiod = devm_gpiod_get(dev, "enable", 1828c2ecf20Sopenharmony_ci brightness ? GPIOD_OUT_HIGH : 1838c2ecf20Sopenharmony_ci GPIOD_OUT_LOW); 1848c2ecf20Sopenharmony_ci if (IS_ERR(ktd253->gpiod)) { 1858c2ecf20Sopenharmony_ci ret = PTR_ERR(ktd253->gpiod); 1868c2ecf20Sopenharmony_ci if (ret != -EPROBE_DEFER) 1878c2ecf20Sopenharmony_ci dev_err(dev, "gpio line missing or invalid.\n"); 1888c2ecf20Sopenharmony_ci return ret; 1898c2ecf20Sopenharmony_ci } 1908c2ecf20Sopenharmony_ci gpiod_set_consumer_name(ktd253->gpiod, dev_name(dev)); 1918c2ecf20Sopenharmony_ci 1928c2ecf20Sopenharmony_ci bl = devm_backlight_device_register(dev, dev_name(dev), dev, ktd253, 1938c2ecf20Sopenharmony_ci &ktd253_backlight_ops, NULL); 1948c2ecf20Sopenharmony_ci if (IS_ERR(bl)) { 1958c2ecf20Sopenharmony_ci dev_err(dev, "failed to register backlight\n"); 1968c2ecf20Sopenharmony_ci return PTR_ERR(bl); 1978c2ecf20Sopenharmony_ci } 1988c2ecf20Sopenharmony_ci bl->props.max_brightness = max_brightness; 1998c2ecf20Sopenharmony_ci /* When we just enable the GPIO line we set max brightness */ 2008c2ecf20Sopenharmony_ci if (brightness) { 2018c2ecf20Sopenharmony_ci bl->props.brightness = brightness; 2028c2ecf20Sopenharmony_ci bl->props.power = FB_BLANK_UNBLANK; 2038c2ecf20Sopenharmony_ci } else { 2048c2ecf20Sopenharmony_ci bl->props.brightness = 0; 2058c2ecf20Sopenharmony_ci bl->props.power = FB_BLANK_POWERDOWN; 2068c2ecf20Sopenharmony_ci } 2078c2ecf20Sopenharmony_ci 2088c2ecf20Sopenharmony_ci ktd253->bl = bl; 2098c2ecf20Sopenharmony_ci platform_set_drvdata(pdev, bl); 2108c2ecf20Sopenharmony_ci backlight_update_status(bl); 2118c2ecf20Sopenharmony_ci 2128c2ecf20Sopenharmony_ci return 0; 2138c2ecf20Sopenharmony_ci} 2148c2ecf20Sopenharmony_ci 2158c2ecf20Sopenharmony_cistatic const struct of_device_id ktd253_backlight_of_match[] = { 2168c2ecf20Sopenharmony_ci { .compatible = "kinetic,ktd253" }, 2178c2ecf20Sopenharmony_ci { /* sentinel */ } 2188c2ecf20Sopenharmony_ci}; 2198c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, ktd253_backlight_of_match); 2208c2ecf20Sopenharmony_ci 2218c2ecf20Sopenharmony_cistatic struct platform_driver ktd253_backlight_driver = { 2228c2ecf20Sopenharmony_ci .driver = { 2238c2ecf20Sopenharmony_ci .name = "ktd253-backlight", 2248c2ecf20Sopenharmony_ci .of_match_table = ktd253_backlight_of_match, 2258c2ecf20Sopenharmony_ci }, 2268c2ecf20Sopenharmony_ci .probe = ktd253_backlight_probe, 2278c2ecf20Sopenharmony_ci}; 2288c2ecf20Sopenharmony_cimodule_platform_driver(ktd253_backlight_driver); 2298c2ecf20Sopenharmony_ci 2308c2ecf20Sopenharmony_ciMODULE_AUTHOR("Linus Walleij <linus.walleij@linaro.org>"); 2318c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Kinetic KTD253 Backlight Driver"); 2328c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL"); 2338c2ecf20Sopenharmony_ciMODULE_ALIAS("platform:ktd253-backlight"); 234