1 // SPDX-License-Identifier: GPL-2.0-only
3 * LCD / Backlight control code for Sharp SL-6000x (tosa)
5 * Copyright (c) 2005 Dirk Opfer
6 * Copyright (c) 2007,2008 Dmitry Baryshkov
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>
16 #include <linux/backlight.h>
17 #include <linux/slab.h>
19 #include <asm/mach/sharpsl_param.h>
23 #define COMADJ_DEFAULT 97
29 struct i2c_client
*i2c
;
30 struct backlight_device
*bl
;
31 struct gpio_desc
*gpio
;
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));
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
;
61 tosa_bl_set_backlight(data
, brightness
);
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
;
85 data
= devm_kzalloc(&client
->dev
, sizeof(struct tosa_bl_data
),
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
);
94 dev_dbg(&data
->bl
->dev
, "Unable to request gpio!\n");
98 i2c_set_clientdata(client
, data
);
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
,
107 if (IS_ERR(data
->bl
)) {
108 ret
= PTR_ERR(data
->bl
);
112 data
->bl
->props
.brightness
= 69;
113 data
->bl
->props
.power
= FB_BLANK_UNBLANK
;
115 backlight_update_status(data
->bl
);
124 static int tosa_bl_remove(struct i2c_client
*client
)
126 struct tosa_bl_data
*data
= i2c_get_clientdata(client
);
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);
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
);
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
[] = {
157 MODULE_DEVICE_TABLE(i2c
, tosa_bl_id
);
159 static struct i2c_driver tosa_bl_driver
= {
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");