Merge tag 'armsoc-dt' of git://git.kernel.org/pub/scm/linux/kernel/git/soc/soc
[linux-2.6-microblaze.git] / drivers / video / backlight / tosa_bl.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *  LCD / Backlight control code for Sharp SL-6000x (tosa)
4  *
5  *  Copyright (c) 2005          Dirk Opfer
6  *  Copyright (c) 2007,2008     Dmitry Baryshkov
7  */
8
9 #include <linux/kernel.h>
10 #include <linux/module.h>
11 #include <linux/device.h>
12 #include <linux/spi/spi.h>
13 #include <linux/i2c.h>
14 #include <linux/gpio.h>
15 #include <linux/fb.h>
16 #include <linux/backlight.h>
17 #include <linux/slab.h>
18
19 #include <asm/mach/sharpsl_param.h>
20
21 #include "tosa_bl.h"
22
23 #define COMADJ_DEFAULT  97
24
25 #define DAC_CH1         0
26 #define DAC_CH2         1
27
28 struct tosa_bl_data {
29         struct i2c_client *i2c;
30         struct backlight_device *bl;
31         struct gpio_desc *gpio;
32
33         int comadj;
34 };
35
36 static void tosa_bl_set_backlight(struct tosa_bl_data *data, int brightness)
37 {
38         struct spi_device *spi = dev_get_platdata(&data->i2c->dev);
39
40         i2c_smbus_write_byte_data(data->i2c, DAC_CH1, data->comadj);
41
42         /* SetBacklightDuty */
43         i2c_smbus_write_byte_data(data->i2c, DAC_CH2, (u8)(brightness & 0xff));
44
45         /* SetBacklightVR */
46         gpiod_set_value(data->gpio, brightness & 0x100);
47
48         tosa_bl_enable(spi, brightness);
49 }
50
51 static int tosa_bl_update_status(struct backlight_device *dev)
52 {
53         struct backlight_properties *props = &dev->props;
54         struct tosa_bl_data *data = bl_get_data(dev);
55         int power = max(props->power, props->fb_blank);
56         int brightness = props->brightness;
57
58         if (power)
59                 brightness = 0;
60
61         tosa_bl_set_backlight(data, brightness);
62
63         return 0;
64 }
65
66 static int tosa_bl_get_brightness(struct backlight_device *dev)
67 {
68         struct backlight_properties *props = &dev->props;
69
70         return props->brightness;
71 }
72
73 static const struct backlight_ops bl_ops = {
74         .get_brightness         = tosa_bl_get_brightness,
75         .update_status          = tosa_bl_update_status,
76 };
77
78 static int tosa_bl_probe(struct i2c_client *client,
79                 const struct i2c_device_id *id)
80 {
81         struct backlight_properties props;
82         struct tosa_bl_data *data;
83         int ret = 0;
84
85         data = devm_kzalloc(&client->dev, sizeof(struct tosa_bl_data),
86                                 GFP_KERNEL);
87         if (!data)
88                 return -ENOMEM;
89
90         data->comadj = sharpsl_param.comadj == -1 ? COMADJ_DEFAULT : sharpsl_param.comadj;
91         data->gpio = devm_gpiod_get(&client->dev, "backlight", GPIOD_OUT_LOW);
92         ret = PTR_ERR_OR_ZERO(data->gpio);
93         if (ret) {
94                 dev_dbg(&data->bl->dev, "Unable to request gpio!\n");
95                 return ret;
96         }
97
98         i2c_set_clientdata(client, data);
99         data->i2c = client;
100
101         memset(&props, 0, sizeof(struct backlight_properties));
102         props.type = BACKLIGHT_RAW;
103         props.max_brightness = 512 - 1;
104         data->bl = devm_backlight_device_register(&client->dev, "tosa-bl",
105                                                 &client->dev, data, &bl_ops,
106                                                 &props);
107         if (IS_ERR(data->bl)) {
108                 ret = PTR_ERR(data->bl);
109                 goto err_reg;
110         }
111
112         data->bl->props.brightness = 69;
113         data->bl->props.power = FB_BLANK_UNBLANK;
114
115         backlight_update_status(data->bl);
116
117         return 0;
118
119 err_reg:
120         data->bl = NULL;
121         return ret;
122 }
123
124 static int tosa_bl_remove(struct i2c_client *client)
125 {
126         struct tosa_bl_data *data = i2c_get_clientdata(client);
127
128         data->bl = NULL;
129         return 0;
130 }
131
132 #ifdef CONFIG_PM_SLEEP
133 static int tosa_bl_suspend(struct device *dev)
134 {
135         struct tosa_bl_data *data = dev_get_drvdata(dev);
136
137         tosa_bl_set_backlight(data, 0);
138
139         return 0;
140 }
141
142 static int tosa_bl_resume(struct device *dev)
143 {
144         struct tosa_bl_data *data = dev_get_drvdata(dev);
145
146         backlight_update_status(data->bl);
147         return 0;
148 }
149 #endif
150
151 static SIMPLE_DEV_PM_OPS(tosa_bl_pm_ops, tosa_bl_suspend, tosa_bl_resume);
152
153 static const struct i2c_device_id tosa_bl_id[] = {
154         { "tosa-bl", 0 },
155         { },
156 };
157 MODULE_DEVICE_TABLE(i2c, tosa_bl_id);
158
159 static struct i2c_driver tosa_bl_driver = {
160         .driver = {
161                 .name           = "tosa-bl",
162                 .pm             = &tosa_bl_pm_ops,
163         },
164         .probe          = tosa_bl_probe,
165         .remove         = tosa_bl_remove,
166         .id_table       = tosa_bl_id,
167 };
168
169 module_i2c_driver(tosa_bl_driver);
170
171 MODULE_AUTHOR("Dmitry Baryshkov");
172 MODULE_LICENSE("GPL v2");
173 MODULE_DESCRIPTION("LCD/Backlight control for Sharp SL-6000 PDA");
174