Merge tag 'trace-v5.11-rc2' of git://git.kernel.org/pub/scm/linux/kernel/git/rostedt...
[linux/fpc-iii.git] / drivers / video / backlight / tosa_bl.c
blob6df6fcd132e36794094c117fe496e9da32b0ae7b
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * LCD / Backlight control code for Sharp SL-6000x (tosa)
5 * Copyright (c) 2005 Dirk Opfer
6 * Copyright (c) 2007,2008 Dmitry Baryshkov
7 */
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/consumer.h>
15 #include <linux/fb.h>
16 #include <linux/backlight.h>
17 #include <linux/slab.h>
19 #include <asm/mach/sharpsl_param.h>
21 #include "tosa_bl.h"
23 #define COMADJ_DEFAULT 97
25 #define DAC_CH1 0
26 #define DAC_CH2 1
28 struct tosa_bl_data {
29 struct i2c_client *i2c;
30 struct backlight_device *bl;
31 struct gpio_desc *gpio;
33 int comadj;
36 static void tosa_bl_set_backlight(struct tosa_bl_data *data, int brightness)
38 struct spi_device *spi = dev_get_platdata(&data->i2c->dev);
40 i2c_smbus_write_byte_data(data->i2c, DAC_CH1, data->comadj);
42 /* SetBacklightDuty */
43 i2c_smbus_write_byte_data(data->i2c, DAC_CH2, (u8)(brightness & 0xff));
45 /* SetBacklightVR */
46 gpiod_set_value(data->gpio, brightness & 0x100);
48 tosa_bl_enable(spi, brightness);
51 static int tosa_bl_update_status(struct backlight_device *dev)
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;
58 if (power)
59 brightness = 0;
61 tosa_bl_set_backlight(data, brightness);
63 return 0;
66 static int tosa_bl_get_brightness(struct backlight_device *dev)
68 struct backlight_properties *props = &dev->props;
70 return props->brightness;
73 static const struct backlight_ops bl_ops = {
74 .get_brightness = tosa_bl_get_brightness,
75 .update_status = tosa_bl_update_status,
78 static int tosa_bl_probe(struct i2c_client *client,
79 const struct i2c_device_id *id)
81 struct backlight_properties props;
82 struct tosa_bl_data *data;
83 int ret = 0;
85 data = devm_kzalloc(&client->dev, sizeof(struct tosa_bl_data),
86 GFP_KERNEL);
87 if (!data)
88 return -ENOMEM;
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;
98 i2c_set_clientdata(client, data);
99 data->i2c = client;
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;
112 data->bl->props.brightness = 69;
113 data->bl->props.power = FB_BLANK_UNBLANK;
115 backlight_update_status(data->bl);
117 return 0;
119 err_reg:
120 data->bl = NULL;
121 return ret;
124 static int tosa_bl_remove(struct i2c_client *client)
126 struct tosa_bl_data *data = i2c_get_clientdata(client);
128 data->bl = NULL;
129 return 0;
132 #ifdef CONFIG_PM_SLEEP
133 static int tosa_bl_suspend(struct device *dev)
135 struct tosa_bl_data *data = dev_get_drvdata(dev);
137 tosa_bl_set_backlight(data, 0);
139 return 0;
142 static int tosa_bl_resume(struct device *dev)
144 struct tosa_bl_data *data = dev_get_drvdata(dev);
146 backlight_update_status(data->bl);
147 return 0;
149 #endif
151 static SIMPLE_DEV_PM_OPS(tosa_bl_pm_ops, tosa_bl_suspend, tosa_bl_resume);
153 static const struct i2c_device_id tosa_bl_id[] = {
154 { "tosa-bl", 0 },
155 { },
157 MODULE_DEVICE_TABLE(i2c, tosa_bl_id);
159 static struct i2c_driver tosa_bl_driver = {
160 .driver = {
161 .name = "tosa-bl",
162 .pm = &tosa_bl_pm_ops,
164 .probe = tosa_bl_probe,
165 .remove = tosa_bl_remove,
166 .id_table = tosa_bl_id,
169 module_i2c_driver(tosa_bl_driver);
171 MODULE_AUTHOR("Dmitry Baryshkov");
172 MODULE_LICENSE("GPL v2");
173 MODULE_DESCRIPTION("LCD/Backlight control for Sharp SL-6000 PDA");