12b27bdccSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 24b0d711bSJin Park /* 34b0d711bSJin Park * linux/drivers/video/backlight/aat2870_bl.c 44b0d711bSJin Park * 54b0d711bSJin Park * Copyright (c) 2011, NVIDIA Corporation. 64b0d711bSJin Park * Author: Jin Park <jinyoungp@nvidia.com> 74b0d711bSJin Park */ 84b0d711bSJin Park 94b0d711bSJin Park #include <linux/module.h> 104b0d711bSJin Park #include <linux/kernel.h> 114b0d711bSJin Park #include <linux/init.h> 124b0d711bSJin Park #include <linux/platform_device.h> 134b0d711bSJin Park #include <linux/mutex.h> 144b0d711bSJin Park #include <linux/delay.h> 154b0d711bSJin Park #include <linux/fb.h> 164b0d711bSJin Park #include <linux/backlight.h> 174b0d711bSJin Park #include <linux/mfd/aat2870.h> 184b0d711bSJin Park 194b0d711bSJin Park struct aat2870_bl_driver_data { 204b0d711bSJin Park struct platform_device *pdev; 214b0d711bSJin Park struct backlight_device *bd; 224b0d711bSJin Park 234b0d711bSJin Park int channels; 244b0d711bSJin Park int max_current; 254b0d711bSJin Park int brightness; /* current brightness */ 264b0d711bSJin Park }; 274b0d711bSJin Park 284b0d711bSJin Park static inline int aat2870_brightness(struct aat2870_bl_driver_data *aat2870_bl, 294b0d711bSJin Park int brightness) 304b0d711bSJin Park { 314b0d711bSJin Park struct backlight_device *bd = aat2870_bl->bd; 324b0d711bSJin Park int val; 334b0d711bSJin Park 345d6f921bSAxel Lin val = brightness * (aat2870_bl->max_current - 1); 354b0d711bSJin Park val /= bd->props.max_brightness; 364b0d711bSJin Park 374b0d711bSJin Park return val; 384b0d711bSJin Park } 394b0d711bSJin Park 404b0d711bSJin Park static inline int aat2870_bl_enable(struct aat2870_bl_driver_data *aat2870_bl) 414b0d711bSJin Park { 424b0d711bSJin Park struct aat2870_data *aat2870 434b0d711bSJin Park = dev_get_drvdata(aat2870_bl->pdev->dev.parent); 444b0d711bSJin Park 454b0d711bSJin Park return aat2870->write(aat2870, AAT2870_BL_CH_EN, 464b0d711bSJin Park (u8)aat2870_bl->channels); 474b0d711bSJin Park } 484b0d711bSJin Park 494b0d711bSJin Park static inline int aat2870_bl_disable(struct aat2870_bl_driver_data *aat2870_bl) 504b0d711bSJin Park { 514b0d711bSJin Park struct aat2870_data *aat2870 524b0d711bSJin Park = dev_get_drvdata(aat2870_bl->pdev->dev.parent); 534b0d711bSJin Park 544b0d711bSJin Park return aat2870->write(aat2870, AAT2870_BL_CH_EN, 0x0); 554b0d711bSJin Park } 564b0d711bSJin Park 574b0d711bSJin Park static int aat2870_bl_update_status(struct backlight_device *bd) 584b0d711bSJin Park { 5976cb2598SJingoo Han struct aat2870_bl_driver_data *aat2870_bl = bl_get_data(bd); 604b0d711bSJin Park struct aat2870_data *aat2870 = 614b0d711bSJin Park dev_get_drvdata(aat2870_bl->pdev->dev.parent); 6232fb2588SStephen Kitt int brightness = backlight_get_brightness(bd); 634b0d711bSJin Park int ret; 644b0d711bSJin Park 654b0d711bSJin Park if ((brightness < 0) || (bd->props.max_brightness < brightness)) { 664b0d711bSJin Park dev_err(&bd->dev, "invalid brightness, %d\n", brightness); 674b0d711bSJin Park return -EINVAL; 684b0d711bSJin Park } 694b0d711bSJin Park 704b0d711bSJin Park dev_dbg(&bd->dev, "brightness=%d, power=%d, state=%d\n", 714b0d711bSJin Park bd->props.brightness, bd->props.power, bd->props.state); 724b0d711bSJin Park 734b0d711bSJin Park ret = aat2870->write(aat2870, AAT2870_BLM, 744b0d711bSJin Park (u8)aat2870_brightness(aat2870_bl, brightness)); 754b0d711bSJin Park if (ret < 0) 764b0d711bSJin Park return ret; 774b0d711bSJin Park 784b0d711bSJin Park if (brightness == 0) { 794b0d711bSJin Park ret = aat2870_bl_disable(aat2870_bl); 804b0d711bSJin Park if (ret < 0) 814b0d711bSJin Park return ret; 824b0d711bSJin Park } else if (aat2870_bl->brightness == 0) { 834b0d711bSJin Park ret = aat2870_bl_enable(aat2870_bl); 844b0d711bSJin Park if (ret < 0) 854b0d711bSJin Park return ret; 864b0d711bSJin Park } 874b0d711bSJin Park 884b0d711bSJin Park aat2870_bl->brightness = brightness; 894b0d711bSJin Park 904b0d711bSJin Park return 0; 914b0d711bSJin Park } 924b0d711bSJin Park 934b0d711bSJin Park static int aat2870_bl_check_fb(struct backlight_device *bd, struct fb_info *fi) 944b0d711bSJin Park { 954b0d711bSJin Park return 1; 964b0d711bSJin Park } 974b0d711bSJin Park 984b0d711bSJin Park static const struct backlight_ops aat2870_bl_ops = { 994b0d711bSJin Park .options = BL_CORE_SUSPENDRESUME, 1004b0d711bSJin Park .update_status = aat2870_bl_update_status, 1014b0d711bSJin Park .check_fb = aat2870_bl_check_fb, 1024b0d711bSJin Park }; 1034b0d711bSJin Park 1044b0d711bSJin Park static int aat2870_bl_probe(struct platform_device *pdev) 1054b0d711bSJin Park { 106c512794cSJingoo Han struct aat2870_bl_platform_data *pdata = dev_get_platdata(&pdev->dev); 1074b0d711bSJin Park struct aat2870_bl_driver_data *aat2870_bl; 1084b0d711bSJin Park struct backlight_device *bd; 1094b0d711bSJin Park struct backlight_properties props; 1104b0d711bSJin Park int ret = 0; 1114b0d711bSJin Park 1124b0d711bSJin Park if (!pdata) { 1134b0d711bSJin Park dev_err(&pdev->dev, "No platform data\n"); 1144b0d711bSJin Park ret = -ENXIO; 1154b0d711bSJin Park goto out; 1164b0d711bSJin Park } 1174b0d711bSJin Park 1184b0d711bSJin Park if (pdev->id != AAT2870_ID_BL) { 1194b0d711bSJin Park dev_err(&pdev->dev, "Invalid device ID, %d\n", pdev->id); 1204b0d711bSJin Park ret = -EINVAL; 1214b0d711bSJin Park goto out; 1224b0d711bSJin Park } 1234b0d711bSJin Park 124ce969228SJulia Lawall aat2870_bl = devm_kzalloc(&pdev->dev, 125ce969228SJulia Lawall sizeof(struct aat2870_bl_driver_data), 126ce969228SJulia Lawall GFP_KERNEL); 1274b0d711bSJin Park if (!aat2870_bl) { 1284b0d711bSJin Park ret = -ENOMEM; 1294b0d711bSJin Park goto out; 1304b0d711bSJin Park } 1314b0d711bSJin Park 1324b0d711bSJin Park memset(&props, 0, sizeof(struct backlight_properties)); 1334b0d711bSJin Park 1344b0d711bSJin Park props.type = BACKLIGHT_RAW; 135c7a2bf3bSJingoo Han bd = devm_backlight_device_register(&pdev->dev, "aat2870-backlight", 136c7a2bf3bSJingoo Han &pdev->dev, aat2870_bl, &aat2870_bl_ops, 137c7a2bf3bSJingoo Han &props); 1384c4dd903SAxel Lin if (IS_ERR(bd)) { 1394b0d711bSJin Park dev_err(&pdev->dev, 1404b0d711bSJin Park "Failed allocate memory for backlight device\n"); 1414c4dd903SAxel Lin ret = PTR_ERR(bd); 142ce969228SJulia Lawall goto out; 1434b0d711bSJin Park } 1444b0d711bSJin Park 1454b0d711bSJin Park aat2870_bl->pdev = pdev; 1464b0d711bSJin Park platform_set_drvdata(pdev, aat2870_bl); 1474b0d711bSJin Park 1484b0d711bSJin Park aat2870_bl->bd = bd; 1494b0d711bSJin Park 1504b0d711bSJin Park if (pdata->channels > 0) 1514b0d711bSJin Park aat2870_bl->channels = pdata->channels; 1524b0d711bSJin Park else 1534b0d711bSJin Park aat2870_bl->channels = AAT2870_BL_CH_ALL; 1544b0d711bSJin Park 1555d6f921bSAxel Lin if (pdata->max_current > 0) 1564b0d711bSJin Park aat2870_bl->max_current = pdata->max_current; 1574b0d711bSJin Park else 1584b0d711bSJin Park aat2870_bl->max_current = AAT2870_CURRENT_27_9; 1594b0d711bSJin Park 1604b0d711bSJin Park if (pdata->max_brightness > 0) 1614b0d711bSJin Park bd->props.max_brightness = pdata->max_brightness; 1624b0d711bSJin Park else 1634b0d711bSJin Park bd->props.max_brightness = 255; 1644b0d711bSJin Park 1654b0d711bSJin Park aat2870_bl->brightness = 0; 1664b0d711bSJin Park bd->props.power = FB_BLANK_UNBLANK; 1674b0d711bSJin Park bd->props.brightness = bd->props.max_brightness; 1684b0d711bSJin Park 1694b0d711bSJin Park ret = aat2870_bl_update_status(bd); 1704b0d711bSJin Park if (ret < 0) { 1714b0d711bSJin Park dev_err(&pdev->dev, "Failed to initialize\n"); 172c7a2bf3bSJingoo Han return ret; 1734b0d711bSJin Park } 1744b0d711bSJin Park 1754b0d711bSJin Park return 0; 1764b0d711bSJin Park 1774b0d711bSJin Park out: 1784b0d711bSJin Park return ret; 1794b0d711bSJin Park } 1804b0d711bSJin Park 181*8b339cafSUwe Kleine-König static void aat2870_bl_remove(struct platform_device *pdev) 1824b0d711bSJin Park { 1834b0d711bSJin Park struct aat2870_bl_driver_data *aat2870_bl = platform_get_drvdata(pdev); 1844b0d711bSJin Park struct backlight_device *bd = aat2870_bl->bd; 1854b0d711bSJin Park 1864b0d711bSJin Park bd->props.power = FB_BLANK_POWERDOWN; 1874b0d711bSJin Park bd->props.brightness = 0; 1884b0d711bSJin Park backlight_update_status(bd); 1894b0d711bSJin Park } 1904b0d711bSJin Park 1914b0d711bSJin Park static struct platform_driver aat2870_bl_driver = { 1924b0d711bSJin Park .driver = { 1934b0d711bSJin Park .name = "aat2870-backlight", 1944b0d711bSJin Park }, 1954b0d711bSJin Park .probe = aat2870_bl_probe, 196*8b339cafSUwe Kleine-König .remove_new = aat2870_bl_remove, 1974b0d711bSJin Park }; 1984b0d711bSJin Park 1994b0d711bSJin Park static int __init aat2870_bl_init(void) 2004b0d711bSJin Park { 2014b0d711bSJin Park return platform_driver_register(&aat2870_bl_driver); 2024b0d711bSJin Park } 2034b0d711bSJin Park subsys_initcall(aat2870_bl_init); 2044b0d711bSJin Park 2054b0d711bSJin Park static void __exit aat2870_bl_exit(void) 2064b0d711bSJin Park { 2074b0d711bSJin Park platform_driver_unregister(&aat2870_bl_driver); 2084b0d711bSJin Park } 2094b0d711bSJin Park module_exit(aat2870_bl_exit); 2104b0d711bSJin Park 2114b0d711bSJin Park MODULE_DESCRIPTION("AnalogicTech AAT2870 Backlight"); 2124b0d711bSJin Park MODULE_LICENSE("GPL"); 2134b0d711bSJin Park MODULE_AUTHOR("Jin Park <jinyoungp@nvidia.com>"); 214