/
panel-cmel-oled43.c
325 lines (262 loc) · 7.7 KB
/
panel-cmel-oled43.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/*
* OLED panel driver for CMEL OLED43
*
* Author: Chris Ladden <chris.ladden@liquidware.com>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 as published by
* the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <linux/module.h>
#include <linux/delay.h>
#include <linux/gpio.h>
#include <linux/leds.h>
#include <linux/fb.h>
#include <plat/display.h>
#define CS_PIN 139
#define MOSI_PIN 144
#define CLK_PIN 138
#define RESET_PIN 137
#define PANEL_PWR_PIN 143
#define digitalWrite(PIN,VALUE) (gpio_direction_output(PIN,VALUE))
#define CS_LOW digitalWrite(CS_PIN, 0)
#define CS_HIGH digitalWrite(CS_PIN, 1)
#define MOSI_LOW digitalWrite(MOSI_PIN, 0)
#define MOSI_HIGH digitalWrite(MOSI_PIN, 1)
#define CLK_LOW digitalWrite(CLK_PIN, 0)
#define CLK_HIGH digitalWrite(CLK_PIN, 1)
#define RESET_LOW digitalWrite(RESET_PIN, 0)
#define RESET_HIGH digitalWrite(RESET_PIN, 1)
#define PANEL_PWR_LOW digitalWrite(PANEL_PWR_PIN, 0)
#define PANEL_PWR_HIGH digitalWrite(PANEL_PWR_PIN, 1)
/*
* Video Timings
*
* Frame Rate = 1/((vbp + vfp + 272)*(hbp + hfp + 480)/pixel_clock)
* Where hfp must be > 30,
* hbp must be = 102,
* vfp must be > 10,
* vbp must be = 20,
*/
static struct omap_video_timings oled43_timings = {
.x_res = 480,
.y_res = 272,
.pixel_clock = 12342,
.hsw = 30,
.hfp = 31,
.hbp = 102,
.vsw = 3,
.vfp = 11,
.vbp = 20,
};
/* Initialize the software SPI interface */
static void oled43_spi_init(void) {
CS_HIGH;
MOSI_HIGH;
CLK_HIGH;
}
/* Write an 8-bit byte onto the SPI bus */
static void oled43_spi_write8(uint8_t data) {
uint8_t ii;
uint8_t bit;
for (ii=0; ii < 8; ii++) {
bit = (data >> (7-ii)) & 1;
if (bit) {
MOSI_HIGH;
} else {
MOSI_LOW;
}
CLK_LOW; //clock the data in
CLK_HIGH;
}
}
/* Write a value to the OLED panel */
static void oled43_writeReg(uint8_t index, uint8_t val) {
CS_LOW;
oled43_spi_write8((index << 1));
oled43_spi_write8(val);
CS_HIGH;
}
static int oled43_hardware_init(void) {
uint8_t brightness;
double percent;
/* Panel init sequence from the panel datasheet */
PANEL_PWR_LOW; // just to be sure, hold the oled power supply off
RESET_LOW; // panel in reset
oled43_spi_init(); // init spi interface
RESET_HIGH; // panel out of reset
oled43_writeReg(0x04, 0x23); //DISPLAY_MODE2
oled43_writeReg(0x05, 0x82); //DISPLAY_MODE3
oled43_writeReg(0x07, 0x0F); //DRIVER_CAPABILITY
oled43_writeReg(0x34, 0x18);
oled43_writeReg(0x35, 0x28);
oled43_writeReg(0x36, 0x16);
oled43_writeReg(0x37, 0x01);
oled43_writeReg(0x03, 35); //VGAM1_LEVEL
/* Set the brightness
* 0x20 - 200 nits
* 0x1E - 175 nits
* 0x1C - 150 nits
* 0x17 - 100 nits
* 0x14 - 70 nits
* 0x11 - 50 nits */
brightness = 0x14;
percent = ((double)brightness)/
(0x20) * 100.0;
printk(KERN_INFO "cmel_oled43_panel: Setting brightness to %d percent\n", (int)percent);
oled43_writeReg(0x3A, brightness);
/* Display ON */
oled43_writeReg(0x06, 0x03); //POWER_CTRL1
PANEL_PWR_HIGH;
return 0;
}
static int oled43_panel_power_on(struct omap_dss_device *dssdev)
{
int r;
oled43_hardware_init();
r = omapdss_dpi_display_enable(dssdev);
if (r)
goto err0;
if (dssdev->platform_enable) {
r = dssdev->platform_enable(dssdev);
if (r)
goto err1;
}
return 0;
err1:
omapdss_dpi_display_disable(dssdev);
err0:
return r;
}
static void oled43_panel_power_off(struct omap_dss_device *dssdev)
{
/* Turn off the power supply */
PANEL_PWR_LOW;
}
static void oled43_panel_led_set(struct led_classdev *cdev, enum led_brightness value)
{
int level;
level = value / 8; //scaled for the controller
if (value <= 20) {
/* brightness threshold reached, turning off */
printk(KERN_INFO "cmel_oled43_panel: brightness set at or below threshold, turning OFF\n");
level = 0;
}
oled43_writeReg(0x3A, level);
}
static struct led_classdev cmel_oled43_panel_led_ops = {
.name = "lcd-backlight",
.brightness_set = oled43_panel_led_set,
.brightness = 159,
.max_brightness = 255,
};
static int oled43_panel_probe(struct omap_dss_device *dssdev)
{
struct led_classdev *cdev;
dssdev->panel.config = OMAP_DSS_LCD_TFT;
dssdev->panel.timings = oled43_timings;
cdev = led_classdev_register((struct device *)&dssdev->dev,
&cmel_oled43_panel_led_ops);
return 0;
}
static void oled43_panel_remove(struct omap_dss_device *dssdev)
{
oled43_panel_power_off(dssdev);
}
static int oled43_panel_enable(struct omap_dss_device *dssdev)
{
int r = 0;
pr_info("cmel_oled43_panel: panel_enable begin\n");
r = oled43_panel_power_on(dssdev);
if (r)
return r;
dssdev->state = OMAP_DSS_DISPLAY_ACTIVE;
pr_info("cmel_oled43_panel: panel_enable end\n");
return 0;
}
static void oled43_panel_disable(struct omap_dss_device *dssdev)
{
oled43_panel_power_off(dssdev);
dssdev->state = OMAP_DSS_DISPLAY_DISABLED;
}
static int oled43_panel_suspend(struct omap_dss_device *dssdev)
{
oled43_panel_power_off(dssdev);
dssdev->state = OMAP_DSS_DISPLAY_SUSPENDED;
pr_info("cmel_oled43_panel: panel_suspend\n");
return 0;
}
static int oled43_panel_resume(struct omap_dss_device *dssdev)
{
int r = 0;
pr_info("cmel_oled43_panel: panel_resume\n");
r = oled43_panel_power_on(dssdev);
if (r)
return r;
dssdev->state = OMAP_DSS_DISPLAY_ACTIVE;
return 0;
}
static void oled43_panel_set_timings(struct omap_dss_device *dssdev,
struct omap_video_timings *timings)
{
dpi_set_timings(dssdev, timings);
}
static void oled43_panel_get_timings(struct omap_dss_device *dssdev,
struct omap_video_timings *timings)
{
*timings = dssdev->panel.timings;
}
static int oled43_panel_check_timings(struct omap_dss_device *dssdev,
struct omap_video_timings *timings)
{
return dpi_check_timings(dssdev, timings);
}
static struct omap_dss_driver oled43_driver = {
.probe = oled43_panel_probe,
.remove = oled43_panel_remove,
.enable = oled43_panel_enable,
.disable = oled43_panel_disable,
.suspend = oled43_panel_suspend,
.resume = oled43_panel_resume,
.set_timings = oled43_panel_set_timings,
.get_timings = oled43_panel_get_timings,
.check_timings = oled43_panel_check_timings,
.driver = {
.name = "cmel_oled43_panel",
.owner = THIS_MODULE,
},
};
static int __init oled43_panel_drv_init(void)
{
int ret = 0;
printk(KERN_INFO "cmel_oled43_panel: init panel\n");
/* Request GPIO pins used for the panel */
printk(KERN_INFO "cmel_oled43_panel: requesting GPIOs\n");
gpio_request(CS_PIN, "OLED43_CS_PIN");
gpio_request(MOSI_PIN, "OLED43_MOSI_PIN");
gpio_request(CLK_PIN, "OLED43_CLK_PIN");
gpio_request(RESET_PIN, "OLED43_RESET_PIN");
gpio_request(PANEL_PWR_PIN, "OLED43_PANEL_PWR_PIN");
/* Hold the power supply off until enabled */
PANEL_PWR_LOW;
ret = omap_dss_register_driver(&oled43_driver);
if (ret != 0)
pr_err("cmel_oled43: Unable to register panel driver: %d\n", ret);
printk(KERN_INFO "cmel_oled43_panel: done\n");
return ret;
}
static void __exit oled43_panel_drv_exit(void)
{
omap_dss_unregister_driver(&oled43_driver);
}
module_init(oled43_panel_drv_init);
module_exit(oled43_panel_drv_exit);
MODULE_LICENSE("GPL");