xref: /linux/drivers/video/backlight/rave-sp-backlight.c (revision b746a1a2860f4a918f32d10dc569115d282aaf2f)
16552d314SAndrey Smirnov // SPDX-License-Identifier: GPL-2.0+
26552d314SAndrey Smirnov 
36552d314SAndrey Smirnov /*
46552d314SAndrey Smirnov  * LCD Backlight driver for RAVE SP
56552d314SAndrey Smirnov  *
66552d314SAndrey Smirnov  * Copyright (C) 2018 Zodiac Inflight Innovations
76552d314SAndrey Smirnov  *
86552d314SAndrey Smirnov  */
96552d314SAndrey Smirnov 
106552d314SAndrey Smirnov #include <linux/backlight.h>
116552d314SAndrey Smirnov #include <linux/kernel.h>
126552d314SAndrey Smirnov #include <linux/module.h>
136552d314SAndrey Smirnov #include <linux/mfd/rave-sp.h>
146552d314SAndrey Smirnov #include <linux/platform_device.h>
156552d314SAndrey Smirnov 
166552d314SAndrey Smirnov #define	RAVE_SP_BACKLIGHT_LCD_EN	BIT(7)
176552d314SAndrey Smirnov 
186552d314SAndrey Smirnov static int rave_sp_backlight_update_status(struct backlight_device *bd)
196552d314SAndrey Smirnov {
206552d314SAndrey Smirnov 	const struct backlight_properties *p = &bd->props;
216552d314SAndrey Smirnov 	const u8 intensity =
226552d314SAndrey Smirnov 		(p->power == FB_BLANK_UNBLANK) ? p->brightness : 0;
236552d314SAndrey Smirnov 	struct rave_sp *sp = dev_get_drvdata(&bd->dev);
246552d314SAndrey Smirnov 	u8 cmd[] = {
256552d314SAndrey Smirnov 		[0] = RAVE_SP_CMD_SET_BACKLIGHT,
266552d314SAndrey Smirnov 		[1] = 0,
276552d314SAndrey Smirnov 		[2] = intensity ? RAVE_SP_BACKLIGHT_LCD_EN | intensity : 0,
286552d314SAndrey Smirnov 		[3] = 0,
296552d314SAndrey Smirnov 		[4] = 0,
306552d314SAndrey Smirnov 	};
316552d314SAndrey Smirnov 
326552d314SAndrey Smirnov 	return rave_sp_exec(sp, cmd, sizeof(cmd), NULL, 0);
336552d314SAndrey Smirnov }
346552d314SAndrey Smirnov 
356552d314SAndrey Smirnov static const struct backlight_ops rave_sp_backlight_ops = {
366552d314SAndrey Smirnov 	.options	= BL_CORE_SUSPENDRESUME,
376552d314SAndrey Smirnov 	.update_status	= rave_sp_backlight_update_status,
386552d314SAndrey Smirnov };
396552d314SAndrey Smirnov 
406552d314SAndrey Smirnov static struct backlight_properties rave_sp_backlight_props = {
416552d314SAndrey Smirnov 	.type		= BACKLIGHT_PLATFORM,
426552d314SAndrey Smirnov 	.max_brightness = 100,
436552d314SAndrey Smirnov 	.brightness	= 50,
446552d314SAndrey Smirnov };
456552d314SAndrey Smirnov 
466552d314SAndrey Smirnov static int rave_sp_backlight_probe(struct platform_device *pdev)
476552d314SAndrey Smirnov {
486552d314SAndrey Smirnov 	struct device *dev = &pdev->dev;
496552d314SAndrey Smirnov 	struct backlight_device *bd;
506552d314SAndrey Smirnov 
51*865cac14SLucas Stach 	bd = devm_backlight_device_register(dev, pdev->name, dev,
526552d314SAndrey Smirnov 					    dev_get_drvdata(dev->parent),
536552d314SAndrey Smirnov 					    &rave_sp_backlight_ops,
546552d314SAndrey Smirnov 					    &rave_sp_backlight_props);
556552d314SAndrey Smirnov 	if (IS_ERR(bd))
566552d314SAndrey Smirnov 		return PTR_ERR(bd);
576552d314SAndrey Smirnov 
58*865cac14SLucas Stach 	/*
59*865cac14SLucas Stach 	 * If there is a phandle pointing to the device node we can
60*865cac14SLucas Stach 	 * assume that another device will manage the status changes.
61*865cac14SLucas Stach 	 * If not we make sure the backlight is in a consistent state.
62*865cac14SLucas Stach 	 */
63*865cac14SLucas Stach 	if (!dev->of_node->phandle)
646552d314SAndrey Smirnov 		backlight_update_status(bd);
656552d314SAndrey Smirnov 
666552d314SAndrey Smirnov 	return 0;
676552d314SAndrey Smirnov }
686552d314SAndrey Smirnov 
696552d314SAndrey Smirnov static const struct of_device_id rave_sp_backlight_of_match[] = {
706552d314SAndrey Smirnov 	{ .compatible = "zii,rave-sp-backlight" },
716552d314SAndrey Smirnov 	{}
726552d314SAndrey Smirnov };
736552d314SAndrey Smirnov 
746552d314SAndrey Smirnov static struct platform_driver rave_sp_backlight_driver = {
756552d314SAndrey Smirnov 	.probe = rave_sp_backlight_probe,
766552d314SAndrey Smirnov 	.driver	= {
776552d314SAndrey Smirnov 		.name = KBUILD_MODNAME,
786552d314SAndrey Smirnov 		.of_match_table = rave_sp_backlight_of_match,
796552d314SAndrey Smirnov 	},
806552d314SAndrey Smirnov };
816552d314SAndrey Smirnov module_platform_driver(rave_sp_backlight_driver);
826552d314SAndrey Smirnov 
836552d314SAndrey Smirnov MODULE_DEVICE_TABLE(of, rave_sp_backlight_of_match);
846552d314SAndrey Smirnov MODULE_LICENSE("GPL");
856552d314SAndrey Smirnov MODULE_AUTHOR("Andrey Vostrikov <andrey.vostrikov@cogentembedded.com>");
866552d314SAndrey Smirnov MODULE_AUTHOR("Nikita Yushchenko <nikita.yoush@cogentembedded.com>");
876552d314SAndrey Smirnov MODULE_AUTHOR("Andrey Smirnov <andrew.smirnov@gmail.com>");
886552d314SAndrey Smirnov MODULE_DESCRIPTION("RAVE SP Backlight driver");
89