Sunday, 27 October 2013

Linux driver for ST7565

Today I will describe my LCD module driver for ST7565. This driver based on fbtft library solution created by Noralf Tronnes.
As I mentioned in a previous post the frame buffer is the intermediate layer between higher kernel layers and video drivers. The frame buffer provider general abstraction to handle all forms of video device.
Our device can communicate with system via different types of bus ( eg. SPI, I2C or parallel ) therefore we have to link specific bus driver with frame buffer interface.  Below we can see "par" structure where "xxx platform device" or "xxx spi device" can be plug in. 

"xxx platform device" is a device defined in platform specific files, used to parallel bus connection.
"xxx spi device" is a spi device driver. 

The Linux device model is based on bus and device concepts.  More information you can find o

The frame buffer is a platform specific segment of the system.  The most popular implementation for Raspberry Pi is Fbtft created by Noralf Tronnes. More information you can find here

I decided to use fbtft because all frame buffer interface methods are implemented and my job was only to create a new device file with a few methods to implement a specialized interface for st7565 driver.

The most important methods are:
- static int init_display(struct fbtft_par *par) - executed during initialization in fbtft_register_framebuffer implemented in fbtft library.  

- static int write_vmem(struct fbtft_par *par, size_t offset, size_t len) - executed
during screen refreshing process. This function is taking a picture image from video memory buffer and convert it to appropriate to display device format. In my case video memory buffer have a format RGB 565 (16 bpp) with screen resolution 132 x 64. Because my LCD has only two colors I convert 16 bpp video memory format to 2bpp device format black and white. Conversion RGB 565 to 2bpp pattern is simple: zero values cast to black pixels and all the other to white pixels.

Last thing to do is prepare fbtft_display structure, in this structure we define parameters which are passed to fb_var_screeninfo and fb_fix_screeninfo structures. Second thing is our device specific operation which are assigned to fbtftops structure:
static struct fbtft_display display = {
        .regwidth = 8,
        .width = WIDTH,
        .height = HEIGHT,
        .txbuflen = TXBUFLEN,
        .gamma_num = 1,
        .gamma_len = 1,
        .gamma = DEFAULT_GAMMA,
        .fbtftops = {
                .init_display = init_display,
                .set_addr_win = set_addr_win,
                .set_var = set_var,
                .write_vmem = write_vmem,
                .set_gamma = set_gamma,
        .backlight = 1,

You can find all code on this repository :

Ok - and this is result of my work :