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/backlight.h> 164b0d711bSJin Park #include <linux/mfd/aat2870.h> 174b0d711bSJin Park 184b0d711bSJin Park struct aat2870_bl_driver_data { 194b0d711bSJin Park struct platform_device *pdev; 204b0d711bSJin Park struct backlight_device *bd; 214b0d711bSJin Park 224b0d711bSJin Park int channels; 234b0d711bSJin Park int max_current; 244b0d711bSJin Park int brightness; /* current brightness */ 254b0d711bSJin Park }; 264b0d711bSJin Park 274b0d711bSJin Park static inline int aat2870_brightness(struct aat2870_bl_driver_data *aat2870_bl, 284b0d711bSJin Park int brightness) 294b0d711bSJin Park { 304b0d711bSJin Park struct backlight_device *bd = aat2870_bl->bd; 314b0d711bSJin Park int val; 324b0d711bSJin Park 335d6f921bSAxel Lin val = brightness * (aat2870_bl->max_current - 1); 344b0d711bSJin Park val /= bd->props.max_brightness; 354b0d711bSJin Park 364b0d711bSJin Park return val; 374b0d711bSJin Park } 384b0d711bSJin Park 394b0d711bSJin Park static inline int aat2870_bl_enable(struct aat2870_bl_driver_data *aat2870_bl) 404b0d711bSJin Park { 414b0d711bSJin Park struct aat2870_data *aat2870 424b0d711bSJin Park = dev_get_drvdata(aat2870_bl->pdev->dev.parent); 434b0d711bSJin Park 444b0d711bSJin Park return aat2870->write(aat2870, AAT2870_BL_CH_EN, 454b0d711bSJin Park (u8)aat2870_bl->channels); 464b0d711bSJin Park } 474b0d711bSJin Park 484b0d711bSJin Park static inline int aat2870_bl_disable(struct aat2870_bl_driver_data *aat2870_bl) 494b0d711bSJin Park { 504b0d711bSJin Park struct aat2870_data *aat2870 514b0d711bSJin Park = dev_get_drvdata(aat2870_bl->pdev->dev.parent); 524b0d711bSJin Park 534b0d711bSJin Park return aat2870->write(aat2870, AAT2870_BL_CH_EN, 0x0); 544b0d711bSJin Park } 554b0d711bSJin Park 564b0d711bSJin Park static int aat2870_bl_update_status(struct backlight_device *bd) 574b0d711bSJin Park { 5876cb2598SJingoo Han struct aat2870_bl_driver_data *aat2870_bl = bl_get_data(bd); 594b0d711bSJin Park struct aat2870_data *aat2870 = 604b0d711bSJin Park dev_get_drvdata(aat2870_bl->pdev->dev.parent); 6132fb2588SStephen Kitt int brightness = backlight_get_brightness(bd); 624b0d711bSJin Park int ret; 634b0d711bSJin Park 644b0d711bSJin Park if ((brightness < 0) || (bd->props.max_brightness < brightness)) { 654b0d711bSJin Park dev_err(&bd->dev, "invalid brightness, %d\n", brightness); 664b0d711bSJin Park return -EINVAL; 674b0d711bSJin Park } 684b0d711bSJin Park 694b0d711bSJin Park dev_dbg(&bd->dev, "brightness=%d, power=%d, state=%d\n", 704b0d711bSJin Park bd->props.brightness, bd->props.power, bd->props.state); 714b0d711bSJin Park 724b0d711bSJin Park ret = aat2870->write(aat2870, AAT2870_BLM, 734b0d711bSJin Park (u8)aat2870_brightness(aat2870_bl, brightness)); 744b0d711bSJin Park if (ret < 0) 754b0d711bSJin Park return ret; 764b0d711bSJin Park 774b0d711bSJin Park if (brightness == 0) { 784b0d711bSJin Park ret = aat2870_bl_disable(aat2870_bl); 794b0d711bSJin Park if (ret < 0) 804b0d711bSJin Park return ret; 814b0d711bSJin Park } else if (aat2870_bl->brightness == 0) { 824b0d711bSJin Park ret = aat2870_bl_enable(aat2870_bl); 834b0d711bSJin Park if (ret < 0) 844b0d711bSJin Park return ret; 854b0d711bSJin Park } 864b0d711bSJin Park 874b0d711bSJin Park aat2870_bl->brightness = brightness; 884b0d711bSJin Park 894b0d711bSJin Park return 0; 904b0d711bSJin Park } 914b0d711bSJin Park 924b0d711bSJin Park static const struct backlight_ops aat2870_bl_ops = { 934b0d711bSJin Park .options = BL_CORE_SUSPENDRESUME, 944b0d711bSJin Park .update_status = aat2870_bl_update_status, 954b0d711bSJin Park }; 964b0d711bSJin Park 974b0d711bSJin Park static int aat2870_bl_probe(struct platform_device *pdev) 984b0d711bSJin Park { 99c512794cSJingoo Han struct aat2870_bl_platform_data *pdata = dev_get_platdata(&pdev->dev); 1004b0d711bSJin Park struct aat2870_bl_driver_data *aat2870_bl; 1014b0d711bSJin Park struct backlight_device *bd; 1024b0d711bSJin Park struct backlight_properties props; 1034b0d711bSJin Park int ret = 0; 1044b0d711bSJin Park 1054b0d711bSJin Park if (!pdata) { 1064b0d711bSJin Park dev_err(&pdev->dev, "No platform data\n"); 1074b0d711bSJin Park ret = -ENXIO; 1084b0d711bSJin Park goto out; 1094b0d711bSJin Park } 1104b0d711bSJin Park 1114b0d711bSJin Park if (pdev->id != AAT2870_ID_BL) { 1124b0d711bSJin Park dev_err(&pdev->dev, "Invalid device ID, %d\n", pdev->id); 1134b0d711bSJin Park ret = -EINVAL; 1144b0d711bSJin Park goto out; 1154b0d711bSJin Park } 1164b0d711bSJin Park 117ce969228SJulia Lawall aat2870_bl = devm_kzalloc(&pdev->dev, 118ce969228SJulia Lawall sizeof(struct aat2870_bl_driver_data), 119ce969228SJulia Lawall GFP_KERNEL); 1204b0d711bSJin Park if (!aat2870_bl) { 1214b0d711bSJin Park ret = -ENOMEM; 1224b0d711bSJin Park goto out; 1234b0d711bSJin Park } 1244b0d711bSJin Park 1254b0d711bSJin Park memset(&props, 0, sizeof(struct backlight_properties)); 1264b0d711bSJin Park 1274b0d711bSJin Park props.type = BACKLIGHT_RAW; 128c7a2bf3bSJingoo Han bd = devm_backlight_device_register(&pdev->dev, "aat2870-backlight", 129c7a2bf3bSJingoo Han &pdev->dev, aat2870_bl, &aat2870_bl_ops, 130c7a2bf3bSJingoo Han &props); 1314c4dd903SAxel Lin if (IS_ERR(bd)) { 1324b0d711bSJin Park dev_err(&pdev->dev, 1334b0d711bSJin Park "Failed allocate memory for backlight device\n"); 1344c4dd903SAxel Lin ret = PTR_ERR(bd); 135ce969228SJulia Lawall goto out; 1364b0d711bSJin Park } 1374b0d711bSJin Park 1384b0d711bSJin Park aat2870_bl->pdev = pdev; 1394b0d711bSJin Park platform_set_drvdata(pdev, aat2870_bl); 1404b0d711bSJin Park 1414b0d711bSJin Park aat2870_bl->bd = bd; 1424b0d711bSJin Park 1434b0d711bSJin Park if (pdata->channels > 0) 1444b0d711bSJin Park aat2870_bl->channels = pdata->channels; 1454b0d711bSJin Park else 1464b0d711bSJin Park aat2870_bl->channels = AAT2870_BL_CH_ALL; 1474b0d711bSJin Park 1485d6f921bSAxel Lin if (pdata->max_current > 0) 1494b0d711bSJin Park aat2870_bl->max_current = pdata->max_current; 1504b0d711bSJin Park else 1514b0d711bSJin Park aat2870_bl->max_current = AAT2870_CURRENT_27_9; 1524b0d711bSJin Park 1534b0d711bSJin Park if (pdata->max_brightness > 0) 1544b0d711bSJin Park bd->props.max_brightness = pdata->max_brightness; 1554b0d711bSJin Park else 1564b0d711bSJin Park bd->props.max_brightness = 255; 1574b0d711bSJin Park 1584b0d711bSJin Park aat2870_bl->brightness = 0; 159*26dcf623SThomas Zimmermann bd->props.power = BACKLIGHT_POWER_ON; 1604b0d711bSJin Park bd->props.brightness = bd->props.max_brightness; 1614b0d711bSJin Park 1624b0d711bSJin Park ret = aat2870_bl_update_status(bd); 1634b0d711bSJin Park if (ret < 0) { 1644b0d711bSJin Park dev_err(&pdev->dev, "Failed to initialize\n"); 165c7a2bf3bSJingoo Han return ret; 1664b0d711bSJin Park } 1674b0d711bSJin Park 1684b0d711bSJin Park return 0; 1694b0d711bSJin Park 1704b0d711bSJin Park out: 1714b0d711bSJin Park return ret; 1724b0d711bSJin Park } 1734b0d711bSJin Park 1748b339cafSUwe Kleine-König static void aat2870_bl_remove(struct platform_device *pdev) 1754b0d711bSJin Park { 1764b0d711bSJin Park struct aat2870_bl_driver_data *aat2870_bl = platform_get_drvdata(pdev); 1774b0d711bSJin Park struct backlight_device *bd = aat2870_bl->bd; 1784b0d711bSJin Park 179*26dcf623SThomas Zimmermann bd->props.power = BACKLIGHT_POWER_OFF; 1804b0d711bSJin Park bd->props.brightness = 0; 1814b0d711bSJin Park backlight_update_status(bd); 1824b0d711bSJin Park } 1834b0d711bSJin Park 1844b0d711bSJin Park static struct platform_driver aat2870_bl_driver = { 1854b0d711bSJin Park .driver = { 1864b0d711bSJin Park .name = "aat2870-backlight", 1874b0d711bSJin Park }, 1884b0d711bSJin Park .probe = aat2870_bl_probe, 1898b339cafSUwe Kleine-König .remove_new = aat2870_bl_remove, 1904b0d711bSJin Park }; 1914b0d711bSJin Park 1924b0d711bSJin Park static int __init aat2870_bl_init(void) 1934b0d711bSJin Park { 1944b0d711bSJin Park return platform_driver_register(&aat2870_bl_driver); 1954b0d711bSJin Park } 1964b0d711bSJin Park subsys_initcall(aat2870_bl_init); 1974b0d711bSJin Park 1984b0d711bSJin Park static void __exit aat2870_bl_exit(void) 1994b0d711bSJin Park { 2004b0d711bSJin Park platform_driver_unregister(&aat2870_bl_driver); 2014b0d711bSJin Park } 2024b0d711bSJin Park module_exit(aat2870_bl_exit); 2034b0d711bSJin Park 2044b0d711bSJin Park MODULE_DESCRIPTION("AnalogicTech AAT2870 Backlight"); 2054b0d711bSJin Park MODULE_LICENSE("GPL"); 2064b0d711bSJin Park MODULE_AUTHOR("Jin Park <jinyoungp@nvidia.com>"); 207