Hello Maxim,<br><br><div class="gmail_quote">On Tue, Jul 31, 2012 at 3:59 PM, Maxime Ripard <span dir="ltr"><<a href="mailto:maxime.ripard@free-electrons.com" target="_blank">maxime.ripard@free-electrons.com</a>></span> wrote:<br>

<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">This patch adds support for the Solomon SSD1307 OLED<br>
controller found on the Crystalfontz CFA10036 board.<br>
<br>
This controller can drive a display with a resolution up<br>
to 128x39 and can operate over I2C or SPI.<br>
<br>
The current driver has only been tested on the CFA-10036,<br>
that is using this controller over I2C to driver a 96x16<br>
OLED screen.<br>
<br>
Signed-off-by: Maxime Ripard <<a href="mailto:maxime.ripard@free-electrons.com">maxime.ripard@free-electrons.com</a>><br>
Cc: Brian Lilly <<a href="mailto:brian@crystalfontz.com">brian@crystalfontz.com</a>><br>
---<br>
 .../devicetree/bindings/video/ssd1307fb.txt        |   24 ++<br>
 drivers/video/Kconfig                              |   14 +<br>
 drivers/video/Makefile                             |    1 +<br>
 drivers/video/ssd1307fb.c                          |  418 ++++++++++++++++++++<br>
 4 files changed, 457 insertions(+)<br>
 create mode 100644 Documentation/devicetree/bindings/video/ssd1307fb.txt<br>
 create mode 100644 drivers/video/ssd1307fb.c<br>
<br>
diff --git a/Documentation/devicetree/bindings/video/ssd1307fb.txt b/Documentation/devicetree/bindings/video/ssd1307fb.txt<br>
new file mode 100644<br>
index 0000000..791e14f<br>
--- /dev/null<br>
+++ b/Documentation/devicetree/bindings/video/ssd1307fb.txt<br>
@@ -0,0 +1,24 @@<br>
+* Solomon SSD1307 Framebuffer Driver<br>
+<br>
+Required properties:<br>
+  - compatible: Should be "solomon,ssd1307fb-<bus>". The only supported bus for<br>
+    now is i2c.<br>
+  - reg: Should contain address of the controller on the I2C bus. Most likely<br>
+         0x3c or 0x3d<br>
+  - pwm: Should contain the pwm to use according to the OF device tree PWM<br>
+         specification [0]<br>
+  - oled-reset-gpio: Should contain the GPIO used to reset the OLED display<br>
+<br>
+Optional properties:<br>
+  - oled-reset-active-low: Is the reset gpio is active on physical low?<br>
+<br>
+[0]: Documentation/devicetree/bindings/pwm/pwm.txt<br>
+<br>
+Examples:<br>
+ssd1307: oled@3c {<br>
+        compatible = "solomon,ssd1307fb-i2c";<br>
+        reg = <0x3c>;<br>
+        pwms = <&pwm 4 3000>;<br>
+        oled-reset-gpio = <&gpio2 7 1>;<br>
+        oled-reset-active-low;<br>
+};<br>
diff --git a/drivers/video/Kconfig b/drivers/video/Kconfig<br>
index 0217f74..21ae6dd 100644<br>
--- a/drivers/video/Kconfig<br>
+++ b/drivers/video/Kconfig<br>
@@ -2469,4 +2469,18 @@ config FB_SH_MOBILE_MERAM<br>
          Up to 4 memory channels can be configured, allowing 4 RGB or<br>
          2 YCbCr framebuffers to be configured.<br>
<br>
+<br>
+config FB_SSD1307<br>
+       tristate "Solomon SSD1307 framebuffer support"<br>
+       depends on FB && I2C<br>
+       select FB_SYS_FOPS<br>
+       select FB_SYS_FILLRECT<br>
+       select FB_SYS_COPYAREA<br>
+       select FB_SYS_IMAGEBLIT<br>
+       select FB_DEFERRED_IO<br>
+       select PWM<br>
+       help<br>
+         This driver implements support for the Solomon SSD1307<br>
+         OLED controller over I2C.<br>
+<br>
 endmenu<br>
diff --git a/drivers/video/Makefile b/drivers/video/Makefile<br>
index ee8dafb..6bbb72c 100644<br>
--- a/drivers/video/Makefile<br>
+++ b/drivers/video/Makefile<br>
@@ -164,6 +164,7 @@ obj-$(CONFIG_FB_BFIN_7393)        += bfin_adv7393fb.o<br>
 obj-$(CONFIG_FB_MX3)             += mx3fb.o<br>
 obj-$(CONFIG_FB_DA8XX)           += da8xx-fb.o<br>
 obj-$(CONFIG_FB_MXS)             += mxsfb.o<br>
+obj-$(CONFIG_FB_SSD1307)         += ssd1307fb.o<br>
<br>
 # the test framebuffer is last<br>
 obj-$(CONFIG_FB_VIRTUAL)          += vfb.o<br>
diff --git a/drivers/video/ssd1307fb.c b/drivers/video/ssd1307fb.c<br>
new file mode 100644<br>
index 0000000..c705ab4<br>
--- /dev/null<br>
+++ b/drivers/video/ssd1307fb.c<br>
@@ -0,0 +1,418 @@<br>
+/*<br>
+ * Driver for the Solomon SSD1307 OLED controler<br>
+ *<br>
+ * Copyright 2012 Free Electrons<br>
+ *<br>
+ * Licensed under the GPLv2 or later.<br>
+ */<br>
+<br>
+#include <linux/module.h><br>
+#include <linux/kernel.h><br>
+#include <linux/i2c.h><br>
+#include <linux/fb.h><br>
+#include <linux/uaccess.h><br>
+#include <linux/of_device.h><br>
+#include <linux/of_gpio.h><br>
+#include <linux/pwm.h><br>
+#include <linux/delay.h><br>
+<br>
+#define SSD1307FB_WIDTH                        96<br>
+#define SSD1307FB_HEIGHT               16<br>
+<br>
+#define SSD1307FB_DATA                 0x40<br>
+#define SSD1307FB_COMMAND              0x80<br>
+<br>
+#define SSD1307FB_CONTRAST             0x81<br>
+#define SSD1307FB_SEG_REMAP_ON         0xa1<br>
+#define SSD1307FB_DISPLAY_OFF          0xae<br>
+#define SSD1307FB_DISPLAY_ON           0xaf<br>
+#define SSD1307FB_START_PAGE_ADDRESS   0xb0<br>
+<br>
+struct ssd1307fb_par {<br>
+       struct i2c_client *client;<br>
+       struct fb_info *info;<br>
+       struct pwm_device *pwm;<br>
+       u32 pwm_period;<br>
+       int reset;<br>
+};<br>
+<br>
+static struct fb_fix_screeninfo ssd1307fb_fix __devinitdata = {<br>
+       .id             = "Solomon SSD1307",<br>
+       .type           = FB_TYPE_PACKED_PIXELS,<br>
+       .visual         = FB_VISUAL_MONO10,<br>
+       .xpanstep       = 0,<br>
+       .ypanstep       = 0,<br>
+       .ywrapstep      = 0,<br>
+       .line_length    = SSD1307FB_WIDTH / 8,<br>
+       .accel          = FB_ACCEL_NONE,<br>
+};<br>
+<br>
+static struct fb_var_screeninfo ssd1307fb_var __devinitdata = {<br>
+       .xres           = SSD1307FB_WIDTH,<br>
+       .yres           = SSD1307FB_HEIGHT,<br>
+       .xres_virtual   = SSD1307FB_WIDTH,<br>
+       .yres_virtual   = SSD1307FB_HEIGHT,<br>
+       .bits_per_pixel = 1,<br>
+};<br>
+<br>
+static int ssd1307fb_write_array(struct i2c_client *client, u8 type, u8* cmd, u32 len)<br>
+{<br>
+       u8 *buf;<br>
+       int ret = 0;<br>
+<br>
+       buf = kzalloc(len + 1, GFP_KERNEL);<br>
+       if (!buf) {<br>
+               dev_err(&client->dev, "Couldn't allocate sending buffer.\n");<br>
+               ret = -ENOMEM;<br>
+               goto out;<br></blockquote><div><br></div><div><div>Why cant we just return from here with -ENOMEM directly rather than jumping to a label, which again just returns?</div><div><br></div></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">


+       }<br>
+<br>
+       buf[0] = type;<br>
+       memcpy(buf + 1, cmd, len);<br>
+<br>
+       ret = i2c_master_send(client, buf, len + 1);<br>
+       if (ret != len + 1) {<br>
+               dev_err(&client->dev, "Couldn't send I2C command.\n");<br>
+               goto error;<br>
+       }<br>
+<br>
+error:<br>
+       kfree(buf);<br>
+out:<br>
+       return ret;<br>
+}<br>
+<br>
+static inline int ssd1307fb_write_cmd_array(struct i2c_client *client, u8* cmd, u32 len)<br>
+{<br>
+       return ssd1307fb_write_array(client, SSD1307FB_COMMAND, cmd, len);<br>
+}<br>
+<br>
+static inline int ssd1307fb_write_cmd(struct i2c_client *client, u8 cmd)<br>
+{<br>
+       return ssd1307fb_write_cmd_array(client, &cmd, 1);<br>
+}<br>
+<br>
+static inline int ssd1307fb_write_data_array(struct i2c_client *client, u8* cmd, u32 len)<br>
+{<br>
+       return ssd1307fb_write_array(client, SSD1307FB_DATA, cmd, len);<br>
+}<br>
+<br>
+static inline int ssd1307fb_write_data(struct i2c_client *client, u8 data)<br>
+{<br>
+       return ssd1307fb_write_data_array(client, &data, 1);<br>
+}<br>
+<br>
+static int ssd1307fb_set(struct i2c_client *client, u8 value)<br>
+{<br>
+       int i, j, ret;<br>
+<br>
+       for (i = 1; i <= (SSD1307FB_HEIGHT / 8); i++) {<br>
+               ret = ssd1307fb_write_cmd(client, SSD1307FB_START_PAGE_ADDRESS + i);<br>
+               if (ret < 0)<br>
+                       goto i2c_error;<br>
+<br>
+               ret = ssd1307fb_write_cmd(client, 0x00);<br>
+               if (ret < 0)<br>
+                       goto i2c_error;<br>
+<br>
+               ret = ssd1307fb_write_cmd(client, 0x10);<br>
+               if (ret < 0)<br>
+                       goto i2c_error;<br>
+<br>
+               for (j = 0; j < SSD1307FB_WIDTH; j++)<br>
+                       ssd1307fb_write_data(client, value);<br>
+       }<br>
+<br>
+       return 0;<br>
+<br>
+i2c_error:<br>
+       dev_err(&client->dev, "Couldn't send i2c command: %d\n", ret);<br>
+       return ret;<br>
+}<br>
+<br>
+static void ssd1307fb_update_display(struct ssd1307fb_par *par)<br>
+{<br>
+       u8 *vmem = par->info->screen_base;<br>
+       int i, j, k;<br>
+<br>
+       /*<br>
+        * The screen is divided in pages, each having a height of 8<br>
+        * pixels, and the width of the screen. When sending a byte of<br>
+        * data to the controller, it gives the 8 bits for the current<br>
+        * column. I.e, the first byte are the 8 bits of the first<br>
+        * column, then the 8 bits for the second column, etc.<br>
+        *<br>
+        *<br>
+        * Representation of the screen, assuming it is 5 bits<br>
+        * wide. Each letter-number combination is a bit that controls<br>
+        * one pixel.<br>
+        *<br>
+        * A0 A1 A2 A3 A4<br>
+        * B0 B1 B2 B3 B4<br>
+        * C0 C1 C2 C3 C4<br>
+        * D0 D1 D2 D3 D4<br>
+        * E0 E1 E2 E3 E4<br>
+        * F0 F1 F2 F3 F4<br>
+        * G0 G1 G2 G3 G4<br>
+        * H0 H1 H2 H3 H4<br>
+        *<br>
+        * If you want to update this screen, you need to send 5 bytes:<br>
+        *  (1) A0 B0 C0 D0 E0 F0 G0 H0<br>
+        *  (2) A1 B1 C1 D1 E1 F1 G1 H1<br>
+        *  (3) A2 B2 C2 D2 E2 F2 G2 H2<br>
+        *  (4) A3 B3 C3 D3 E3 F3 G3 H3<br>
+        *  (5) A4 B4 C4 D4 E4 F4 G4 H4<br>
+        */<br>
+<br>
+       for (i = 0; i < (SSD1307FB_HEIGHT / 8); i++) {<br>
+               ssd1307fb_write_cmd(par->client, SSD1307FB_START_PAGE_ADDRESS + (i + 1));<br>
+               ssd1307fb_write_cmd(par->client, 0x00);<br>
+               ssd1307fb_write_cmd(par->client, 0x10);<br>
+<br>
+               for (j = 0; j < SSD1307FB_WIDTH; j++) {<br>
+                       u8 buf = 0;<br>
+                       for (k = 0; k < 8; k++) {<br>
+                               u32 page_length = SSD1307FB_WIDTH * i;<br>
+                               u32 index = page_length + (SSD1307FB_WIDTH * k + j) / 8;<br>
+                               u8 byte = *(vmem + index);<br>
+                               u8 bit = byte & (1 << (7 - (j % 8)));<br>
+                               bit = bit >> (7 - (j % 8));<br>
+                               buf |= bit << k;<br>
+                       }<br>
+                       ssd1307fb_write_data(par->client, buf);<br>
+               }<br>
+       }<br>
+<br>
+       return;<br></blockquote><div><br></div><div>'return' not required?</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
+}<br>
+<br>
+<br>
+static ssize_t ssd1307fb_write(struct fb_info *info, const char __user *buf,<br>
+               size_t count, loff_t *ppos)<br>
+{<br>
+       struct ssd1307fb_par *par = info->par;<br>
+       unsigned long p = *ppos;<br>
+       void *dst;<br>
+       int err = 0;<br>
+<br>
+       dst = (void __force *) (info->screen_base + p);<br>
+<br>
+       if (copy_from_user(dst, buf, count))<br>
+               err = -EFAULT;<br>
+<br>
+       if  (!err)<br>
+               *ppos += count;<br>
+<br>
+       ssd1307fb_update_display(par);<br>
+<br>
+       return (err) ? err : count;<br>
+}<br>
+<br>
+static void ssd1307fb_fillrect(struct fb_info *info, const struct fb_fillrect *rect)<br>
+{<br>
+       struct ssd1307fb_par *par = info->par;<br>
+       sys_fillrect(info, rect);<br>
+       ssd1307fb_update_display(par);<br>
+}<br>
+<br>
+static void ssd1307fb_copyarea(struct fb_info *info, const struct fb_copyarea *area)<br>
+{<br>
+       struct ssd1307fb_par *par = info->par;<br>
+       sys_copyarea(info, area);<br>
+       ssd1307fb_update_display(par);<br>
+}<br>
+<br>
+static void ssd1307fb_imageblit(struct fb_info *info, const struct fb_image *image)<br>
+{<br>
+       struct ssd1307fb_par *par = info->par;<br>
+       sys_imageblit(info, image);<br>
+       ssd1307fb_update_display(par);<br>
+}<br>
+<br>
+static struct fb_ops ssd1307fb_ops = {<br>
+       .owner          = THIS_MODULE,<br>
+       .fb_read        = fb_sys_read,<br>
+       .fb_write       = ssd1307fb_write,<br>
+       .fb_fillrect    = ssd1307fb_fillrect,<br>
+       .fb_copyarea    = ssd1307fb_copyarea,<br>
+       .fb_imageblit   = ssd1307fb_imageblit,<br>
+};<br>
+<br>
+static void ssd1307fb_deferred_io(struct fb_info *info,<br>
+                               struct list_head *pagelist)<br>
+{<br>
+       ssd1307fb_update_display(info->par);<br>
+}<br>
+<br>
+static struct fb_deferred_io ssd1307fb_defio = {<br>
+       .delay          = HZ,<br>
+       .deferred_io    = ssd1307fb_deferred_io,<br>
+};<br>
+<br>
+static int __devinit ssd1307fb_probe(struct i2c_client *client, const struct i2c_device_id *id)<br>
+{<br>
+       struct fb_info *info;<br>
+       u32 vmem_size = SSD1307FB_WIDTH * SSD1307FB_HEIGHT / 8;<br>
+       struct ssd1307fb_par *par;<br>
+       u8 *vmem;<br>
+       int ret;<br>
+<br>
+       if (!client->dev.of_node) {<br>
+               dev_err(&client->dev, "No device tree data found!\n");<br>
+               ret = -EINVAL;<br>
+               goto generic_error;<br></blockquote><div><br></div><div>Again, can just return from here with the error value...</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">


+       }<br>
+<br>
+       info = framebuffer_alloc(sizeof(struct ssd1307fb_par), &client->dev);<br>
+       if (!info) {<br>
+               ret = -ENOMEM;<br>
+               goto generic_error;<br></blockquote><div><br></div><div>ditto</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
+       }<br>
+<br>
+       vmem = devm_kzalloc(&client->dev, vmem_size, GFP_KERNEL);<br>
+       if (!vmem) {<br>
+               dev_err(&client->dev, "Couldn't allocate graphical memory.\n");<br>
+               ret = -ENOMEM;<br>
+               goto generic_error;<br></blockquote><div><br></div><div>ditto
</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
+       }<br>
+<br>
+       info->fbops = &ssd1307fb_ops;<br>
+       info->fix = ssd1307fb_fix;<br>
+       info->fbdefio = &ssd1307fb_defio;<br>
+<br>
+       info->var = ssd1307fb_var;<br>
+       info->var.red.length = 1;<br>
+       info->var.red.offset = 0;<br>
+       info->var.green.length = 1;<br>
+       info->var.green.offset = 0;<br>
+       info->var.blue.length = 1;<br>
+       info->var.blue.offset = 0;<br>
+<br>
+       info->screen_base = (u8 __force __iomem *)vmem;<br>
+       info->fix.smem_start = (unsigned long)vmem;<br>
+       info->fix.smem_len = vmem_size;<br>
+<br>
+       fb_deferred_io_init(info);<br>
+<br>
+       par = info->par;<br>
+       par->info = info;<br>
+       par->client = client;<br>
+<br>
+       par->reset = of_get_named_gpio(client->dev.of_node,<br>
+                                        "oled-reset-gpio", 0);<br>
+       if (gpio_is_valid(par->reset)) {<br>
+               int flags = GPIOF_OUT_INIT_HIGH;<br>
+               if (of_get_property(client->dev.of_node,<br>
+                                   "oled-reset-active-low", NULL))<br>
+                       flags = GPIOF_OUT_INIT_LOW;<br>
+               ret = devm_gpio_request_one(&client->dev, par->reset,<br>
+                                           flags, "oled-reset");<br>
+               if (ret) {<br>
+                       dev_err(&client->dev,<br>
+                               "failed to request gpio %d: %d\n",<br>
+                               par->reset, ret);<br>
+                       goto reset_oled_error;<br>
+               }<br>
+       }<br>
+<br>
+       par->pwm = pwm_get(&client->dev, NULL);<br>
+       if (IS_ERR(par->pwm)) {<br>
+               dev_err(&client->dev, "Could not get PWM from device tree!\n");<br>
+               ret = PTR_ERR(par->pwm);<br>
+               goto pwm_error;<br>
+       }<br>
+<br>
+       par->pwm_period = pwm_get_period(par->pwm);<br>
+<br>
+       dev_dbg(&client->dev, "Using PWM%d with a %dns period.\n", par->pwm->pwm, par->pwm_period);<br>
+<br>
+       ret = register_framebuffer(info);<br>
+       if (ret) {<br>
+               dev_err(&client->dev, "Couldn't register the framebuffer\n");<br>
+               goto fbreg_error;<br>
+       }<br>
+<br>
+       i2c_set_clientdata(client, info);<br>
+<br>
+       /* Reset the screen */<br>
+       gpio_set_value(par->reset, 1);<br>
+       udelay(4);<br>
+       gpio_set_value(par->reset, 0);<br>
+       udelay(4);<br>
+<br>
+       /* Enable the PWM */<br>
+       pwm_config(par->pwm, par->pwm_period / 2, par->pwm_period);<br>
+       pwm_enable(par->pwm);<br>
+<br>
+       /* Map column 127 of the OLED to segment 0 */<br>
+       ret = ssd1307fb_write_cmd(client, SSD1307FB_SEG_REMAP_ON);<br>
+       if (ret < 0) {<br>
+               dev_err(&client->dev, "Couldn't remap the screen.\n");<br>
+               goto remap_error;<br>
+       }<br>
+<br>
+       /* Turn on the display */<br>
+       ret = ssd1307fb_write_cmd(client, SSD1307FB_DISPLAY_ON);<br>
+       if (ret < 0) {<br>
+               dev_err(&client->dev, "Couldn't turn the display on.\n");<br>
+               goto remap_error;<br>
+       }<br>
+<br>
+       dev_info(&client->dev, "fb%d: %s framebuffer device registered, using %d bytes of video memory\n", info->node, info-><a href="http://fix.id" target="_blank">fix.id</a>, vmem_size);<br>
+<br>
+       return 0;<br>
+<br>
+remap_error:<br>
+       unregister_framebuffer(info);<br>
+       pwm_disable(par->pwm);<br>
+fbreg_error:<br>
+       pwm_put(par->pwm);<br>
+pwm_error:<br>
+reset_oled_error:<br>
+       fb_deferred_io_cleanup(info);<br>
+       framebuffer_release(info);<br>
+generic_error:<br>
+       return ret;<br>
+}<br>
+<br>
+static int __devexit ssd1307fb_remove(struct i2c_client *client)<br>
+{<br>
+       struct fb_info *info = i2c_get_clientdata(client);<br>
+       struct ssd1307fb_par *par = info->par;<br>
+       unregister_framebuffer(info);<br>
+       pwm_disable(par->pwm);<br>
+       pwm_put(par->pwm);<br>
+       fb_deferred_io_cleanup(info);<br>
+       framebuffer_release(info);<br>
+<br>
+       return 0;<br>
+}<br>
+<br>
+static const struct i2c_device_id ssd1307fb_i2c_id[] = {<br>
+       { "ssd1307fb", 0 },<br>
+       { }<br>
+};<br>
+MODULE_DEVICE_TABLE(i2c, ssd1307fb_i2c_id);<br>
+<br>
+static const struct of_device_id ssd1307fb_of_match[] = {<br>
+       { .compatible = "solomon,ssd1307fb-i2c" },<br>
+       {},<br>
+};<br>
+MODULE_DEVICE_TABLE(of, ssd1307fb_of_match);<br>
+<br>
+static struct i2c_driver ssd1307fb_driver = {<br>
+       .probe = ssd1307fb_probe,<br>
+       .remove = __devexit_p(ssd1307fb_remove),<br>
+       .id_table = ssd1307fb_i2c_id,<br>
+       .driver = {<br>
+               .name = "ssd1307fb",<br>
+               .of_match_table = of_match_ptr(ssd1307fb_of_match),<br>
+               .owner = THIS_MODULE,<br>
+       },<br>
+};<br>
+<br>
+module_i2c_driver(ssd1307fb_driver);<br>
+<br>
+MODULE_DESCRIPTION("FB driver for the Solomon SSD1307 OLED controler");<br>
+MODULE_AUTHOR("Maxime Ripard <<a href="mailto:maxime.ripard@free-electrons.com">maxime.ripard@free-electrons.com</a>>");<br>
+MODULE_LICENSE("GPL");<br>
<span class="HOEnZb"><font color="#888888">--<br>
1.7.9.5<br>
<br>
<br>
_______________________________________________<br>
linux-arm-kernel mailing list<br>
<a href="mailto:linux-arm-kernel@lists.infradead.org">linux-arm-kernel@lists.infradead.org</a><br>
<a href="http://lists.infradead.org/mailman/listinfo/linux-arm-kernel" target="_blank">http://lists.infradead.org/mailman/listinfo/linux-arm-kernel</a></font></span></blockquote><div><br></div><div>Regards,</div><div>-Amar </div>

</div><br>