1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * shmob_drm_backlight.c  --  SH Mobile DRM Backlight
4  *
5  * Copyright (C) 2012 Renesas Electronics Corporation
6  *
7  * Laurent Pinchart (laurent.pinchart@ideasonboard.com)
8  */
9 
10 #include <linux/backlight.h>
11 
12 #include "shmob_drm_backlight.h"
13 #include "shmob_drm_crtc.h"
14 #include "shmob_drm_drv.h"
15 
16 static int shmob_drm_backlight_update(struct backlight_device *bdev)
17 {
18 	struct shmob_drm_connector *scon = bl_get_data(bdev);
19 	struct shmob_drm_device *sdev = scon->connector.dev->dev_private;
20 	const struct shmob_drm_backlight_data *bdata = &sdev->pdata->backlight;
21 	int brightness = backlight_get_brightness(bdev);
22 
23 	return bdata->set_brightness(brightness);
24 }
25 
26 static int shmob_drm_backlight_get_brightness(struct backlight_device *bdev)
27 {
28 	struct shmob_drm_connector *scon = bl_get_data(bdev);
29 	struct shmob_drm_device *sdev = scon->connector.dev->dev_private;
30 	const struct shmob_drm_backlight_data *bdata = &sdev->pdata->backlight;
31 
32 	return bdata->get_brightness();
33 }
34 
35 static const struct backlight_ops shmob_drm_backlight_ops = {
36 	.options	= BL_CORE_SUSPENDRESUME,
37 	.update_status	= shmob_drm_backlight_update,
38 	.get_brightness	= shmob_drm_backlight_get_brightness,
39 };
40 
41 void shmob_drm_backlight_dpms(struct shmob_drm_connector *scon, int mode)
42 {
43 	if (scon->backlight == NULL)
44 		return;
45 
46 	scon->backlight->props.power = mode == DRM_MODE_DPMS_ON
47 				     ? FB_BLANK_UNBLANK : FB_BLANK_POWERDOWN;
48 	backlight_update_status(scon->backlight);
49 }
50 
51 int shmob_drm_backlight_init(struct shmob_drm_connector *scon)
52 {
53 	struct shmob_drm_device *sdev = scon->connector.dev->dev_private;
54 	const struct shmob_drm_backlight_data *bdata = &sdev->pdata->backlight;
55 	struct drm_connector *connector = &scon->connector;
56 	struct drm_device *dev = connector->dev;
57 	struct backlight_device *backlight;
58 
59 	if (!bdata->max_brightness)
60 		return 0;
61 
62 	backlight = backlight_device_register(bdata->name, dev->dev, scon,
63 					      &shmob_drm_backlight_ops, NULL);
64 	if (IS_ERR(backlight)) {
65 		dev_err(dev->dev, "unable to register backlight device: %ld\n",
66 			PTR_ERR(backlight));
67 		return PTR_ERR(backlight);
68 	}
69 
70 	backlight->props.max_brightness = bdata->max_brightness;
71 	backlight->props.brightness = bdata->max_brightness;
72 	backlight->props.power = FB_BLANK_POWERDOWN;
73 	backlight_update_status(backlight);
74 
75 	scon->backlight = backlight;
76 	return 0;
77 }
78 
79 void shmob_drm_backlight_exit(struct shmob_drm_connector *scon)
80 {
81 	backlight_device_unregister(scon->backlight);
82 }
83