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 <mach/tosa.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 
32 	int comadj;
33 };
34 
tosa_bl_set_backlight(struct tosa_bl_data * data,int brightness)35 static void tosa_bl_set_backlight(struct tosa_bl_data *data, int brightness)
36 {
37 	struct spi_device *spi = dev_get_platdata(&data->i2c->dev);
38 
39 	i2c_smbus_write_byte_data(data->i2c, DAC_CH1, data->comadj);
40 
41 	/* SetBacklightDuty */
42 	i2c_smbus_write_byte_data(data->i2c, DAC_CH2, (u8)(brightness & 0xff));
43 
44 	/* SetBacklightVR */
45 	gpio_set_value(TOSA_GPIO_BL_C20MA, brightness & 0x100);
46 
47 	tosa_bl_enable(spi, brightness);
48 }
49 
tosa_bl_update_status(struct backlight_device * dev)50 static int tosa_bl_update_status(struct backlight_device *dev)
51 {
52 	struct backlight_properties *props = &dev->props;
53 	struct tosa_bl_data *data = bl_get_data(dev);
54 	int power = max(props->power, props->fb_blank);
55 	int brightness = props->brightness;
56 
57 	if (power)
58 		brightness = 0;
59 
60 	tosa_bl_set_backlight(data, brightness);
61 
62 	return 0;
63 }
64 
tosa_bl_get_brightness(struct backlight_device * dev)65 static int tosa_bl_get_brightness(struct backlight_device *dev)
66 {
67 	struct backlight_properties *props = &dev->props;
68 
69 	return props->brightness;
70 }
71 
72 static const struct backlight_ops bl_ops = {
73 	.get_brightness		= tosa_bl_get_brightness,
74 	.update_status		= tosa_bl_update_status,
75 };
76 
tosa_bl_probe(struct i2c_client * client,const struct i2c_device_id * id)77 static int tosa_bl_probe(struct i2c_client *client,
78 		const struct i2c_device_id *id)
79 {
80 	struct backlight_properties props;
81 	struct tosa_bl_data *data;
82 	int ret = 0;
83 
84 	data = devm_kzalloc(&client->dev, sizeof(struct tosa_bl_data),
85 				GFP_KERNEL);
86 	if (!data)
87 		return -ENOMEM;
88 
89 	data->comadj = sharpsl_param.comadj == -1 ? COMADJ_DEFAULT : sharpsl_param.comadj;
90 
91 	ret = devm_gpio_request_one(&client->dev, TOSA_GPIO_BL_C20MA,
92 				GPIOF_OUT_INIT_LOW, "backlight");
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 
tosa_bl_remove(struct i2c_client * client)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
tosa_bl_suspend(struct device * dev)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 
tosa_bl_resume(struct device * dev)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 
175