Maxime Ripard | a2ed00d | 2012-12-07 12:30:38 -0800 | [diff] [blame] | 1 | /* |
Masanari Iida | a895d57 | 2013-04-09 02:06:50 +0900 | [diff] [blame] | 2 | * Driver for the Solomon SSD1307 OLED controller |
Maxime Ripard | a2ed00d | 2012-12-07 12:30:38 -0800 | [diff] [blame] | 3 | * |
| 4 | * Copyright 2012 Free Electrons |
| 5 | * |
| 6 | * Licensed under the GPLv2 or later. |
| 7 | */ |
| 8 | |
| 9 | #include <linux/module.h> |
| 10 | #include <linux/kernel.h> |
| 11 | #include <linux/i2c.h> |
| 12 | #include <linux/fb.h> |
| 13 | #include <linux/uaccess.h> |
| 14 | #include <linux/of_device.h> |
| 15 | #include <linux/of_gpio.h> |
| 16 | #include <linux/pwm.h> |
| 17 | #include <linux/delay.h> |
| 18 | |
Maxime Ripard | a2ed00d | 2012-12-07 12:30:38 -0800 | [diff] [blame] | 19 | #define SSD1307FB_DATA 0x40 |
| 20 | #define SSD1307FB_COMMAND 0x80 |
| 21 | |
Maxime Ripard | 301bc06 | 2013-04-22 12:02:25 +0200 | [diff] [blame] | 22 | #define SSD1307FB_SET_ADDRESS_MODE 0x20 |
| 23 | #define SSD1307FB_SET_ADDRESS_MODE_HORIZONTAL (0x00) |
| 24 | #define SSD1307FB_SET_ADDRESS_MODE_VERTICAL (0x01) |
| 25 | #define SSD1307FB_SET_ADDRESS_MODE_PAGE (0x02) |
| 26 | #define SSD1307FB_SET_COL_RANGE 0x21 |
| 27 | #define SSD1307FB_SET_PAGE_RANGE 0x22 |
Maxime Ripard | a2ed00d | 2012-12-07 12:30:38 -0800 | [diff] [blame] | 28 | #define SSD1307FB_CONTRAST 0x81 |
Maxime Ripard | bbc7908 | 2013-04-22 11:55:54 +0200 | [diff] [blame] | 29 | #define SSD1307FB_CHARGE_PUMP 0x8d |
Maxime Ripard | a2ed00d | 2012-12-07 12:30:38 -0800 | [diff] [blame] | 30 | #define SSD1307FB_SEG_REMAP_ON 0xa1 |
| 31 | #define SSD1307FB_DISPLAY_OFF 0xae |
Maxime Ripard | bbc7908 | 2013-04-22 11:55:54 +0200 | [diff] [blame] | 32 | #define SSD1307FB_SET_MULTIPLEX_RATIO 0xa8 |
Maxime Ripard | a2ed00d | 2012-12-07 12:30:38 -0800 | [diff] [blame] | 33 | #define SSD1307FB_DISPLAY_ON 0xaf |
| 34 | #define SSD1307FB_START_PAGE_ADDRESS 0xb0 |
Maxime Ripard | bbc7908 | 2013-04-22 11:55:54 +0200 | [diff] [blame] | 35 | #define SSD1307FB_SET_DISPLAY_OFFSET 0xd3 |
| 36 | #define SSD1307FB_SET_CLOCK_FREQ 0xd5 |
| 37 | #define SSD1307FB_SET_PRECHARGE_PERIOD 0xd9 |
| 38 | #define SSD1307FB_SET_COM_PINS_CONFIG 0xda |
| 39 | #define SSD1307FB_SET_VCOMH 0xdb |
| 40 | |
| 41 | struct ssd1307fb_par; |
| 42 | |
Thomas Niederprüm | c89eacf | 2015-03-31 20:27:10 +0200 | [diff] [blame] | 43 | struct ssd1307fb_deviceinfo { |
| 44 | u32 default_vcomh; |
| 45 | u32 default_dclk_div; |
| 46 | u32 default_dclk_frq; |
| 47 | int need_pwm; |
| 48 | int need_chargepump; |
Maxime Ripard | bbc7908 | 2013-04-22 11:55:54 +0200 | [diff] [blame] | 49 | }; |
Maxime Ripard | a2ed00d | 2012-12-07 12:30:38 -0800 | [diff] [blame] | 50 | |
| 51 | struct ssd1307fb_par { |
Thomas Niederprüm | c89eacf | 2015-03-31 20:27:10 +0200 | [diff] [blame] | 52 | u32 com_invdir; |
| 53 | u32 com_lrremap; |
| 54 | u32 com_offset; |
| 55 | u32 com_seq; |
| 56 | u32 contrast; |
| 57 | u32 dclk_div; |
| 58 | u32 dclk_frq; |
| 59 | struct ssd1307fb_deviceinfo *device_info; |
Maxime Ripard | a2ed00d | 2012-12-07 12:30:38 -0800 | [diff] [blame] | 60 | struct i2c_client *client; |
Maxime Ripard | bbc7908 | 2013-04-22 11:55:54 +0200 | [diff] [blame] | 61 | u32 height; |
Maxime Ripard | a2ed00d | 2012-12-07 12:30:38 -0800 | [diff] [blame] | 62 | struct fb_info *info; |
Maxime Ripard | bbc7908 | 2013-04-22 11:55:54 +0200 | [diff] [blame] | 63 | u32 page_offset; |
Thomas Niederprüm | c89eacf | 2015-03-31 20:27:10 +0200 | [diff] [blame] | 64 | u32 prechargep1; |
| 65 | u32 prechargep2; |
Maxime Ripard | a2ed00d | 2012-12-07 12:30:38 -0800 | [diff] [blame] | 66 | struct pwm_device *pwm; |
| 67 | u32 pwm_period; |
| 68 | int reset; |
Thomas Niederprüm | c89eacf | 2015-03-31 20:27:10 +0200 | [diff] [blame] | 69 | u32 seg_remap; |
| 70 | u32 vcomh; |
Maxime Ripard | bbc7908 | 2013-04-22 11:55:54 +0200 | [diff] [blame] | 71 | u32 width; |
Maxime Ripard | a2ed00d | 2012-12-07 12:30:38 -0800 | [diff] [blame] | 72 | }; |
| 73 | |
Maxime Ripard | 9f7714d | 2013-04-22 12:02:23 +0200 | [diff] [blame] | 74 | struct ssd1307fb_array { |
| 75 | u8 type; |
| 76 | u8 data[0]; |
| 77 | }; |
| 78 | |
Greg Kroah-Hartman | 48c68c4 | 2012-12-21 13:07:39 -0800 | [diff] [blame] | 79 | static struct fb_fix_screeninfo ssd1307fb_fix = { |
Maxime Ripard | a2ed00d | 2012-12-07 12:30:38 -0800 | [diff] [blame] | 80 | .id = "Solomon SSD1307", |
| 81 | .type = FB_TYPE_PACKED_PIXELS, |
| 82 | .visual = FB_VISUAL_MONO10, |
| 83 | .xpanstep = 0, |
| 84 | .ypanstep = 0, |
| 85 | .ywrapstep = 0, |
Maxime Ripard | a2ed00d | 2012-12-07 12:30:38 -0800 | [diff] [blame] | 86 | .accel = FB_ACCEL_NONE, |
| 87 | }; |
| 88 | |
Greg Kroah-Hartman | 48c68c4 | 2012-12-21 13:07:39 -0800 | [diff] [blame] | 89 | static struct fb_var_screeninfo ssd1307fb_var = { |
Maxime Ripard | a2ed00d | 2012-12-07 12:30:38 -0800 | [diff] [blame] | 90 | .bits_per_pixel = 1, |
| 91 | }; |
| 92 | |
Maxime Ripard | 9f7714d | 2013-04-22 12:02:23 +0200 | [diff] [blame] | 93 | static struct ssd1307fb_array *ssd1307fb_alloc_array(u32 len, u8 type) |
Maxime Ripard | a2ed00d | 2012-12-07 12:30:38 -0800 | [diff] [blame] | 94 | { |
Maxime Ripard | 9f7714d | 2013-04-22 12:02:23 +0200 | [diff] [blame] | 95 | struct ssd1307fb_array *array; |
Maxime Ripard | a2ed00d | 2012-12-07 12:30:38 -0800 | [diff] [blame] | 96 | |
Maxime Ripard | 9f7714d | 2013-04-22 12:02:23 +0200 | [diff] [blame] | 97 | array = kzalloc(sizeof(struct ssd1307fb_array) + len, GFP_KERNEL); |
| 98 | if (!array) |
| 99 | return NULL; |
Maxime Ripard | a2ed00d | 2012-12-07 12:30:38 -0800 | [diff] [blame] | 100 | |
Maxime Ripard | 9f7714d | 2013-04-22 12:02:23 +0200 | [diff] [blame] | 101 | array->type = type; |
Maxime Ripard | a2ed00d | 2012-12-07 12:30:38 -0800 | [diff] [blame] | 102 | |
Maxime Ripard | 9f7714d | 2013-04-22 12:02:23 +0200 | [diff] [blame] | 103 | return array; |
Maxime Ripard | a2ed00d | 2012-12-07 12:30:38 -0800 | [diff] [blame] | 104 | } |
| 105 | |
Maxime Ripard | 9f7714d | 2013-04-22 12:02:23 +0200 | [diff] [blame] | 106 | static int ssd1307fb_write_array(struct i2c_client *client, |
| 107 | struct ssd1307fb_array *array, u32 len) |
Maxime Ripard | a2ed00d | 2012-12-07 12:30:38 -0800 | [diff] [blame] | 108 | { |
Maxime Ripard | 9f7714d | 2013-04-22 12:02:23 +0200 | [diff] [blame] | 109 | int ret; |
| 110 | |
| 111 | len += sizeof(struct ssd1307fb_array); |
| 112 | |
| 113 | ret = i2c_master_send(client, (u8 *)array, len); |
| 114 | if (ret != len) { |
| 115 | dev_err(&client->dev, "Couldn't send I2C command.\n"); |
| 116 | return ret; |
| 117 | } |
| 118 | |
| 119 | return 0; |
Maxime Ripard | a2ed00d | 2012-12-07 12:30:38 -0800 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | static inline int ssd1307fb_write_cmd(struct i2c_client *client, u8 cmd) |
| 123 | { |
Maxime Ripard | 9f7714d | 2013-04-22 12:02:23 +0200 | [diff] [blame] | 124 | struct ssd1307fb_array *array; |
| 125 | int ret; |
Maxime Ripard | a2ed00d | 2012-12-07 12:30:38 -0800 | [diff] [blame] | 126 | |
Maxime Ripard | 9f7714d | 2013-04-22 12:02:23 +0200 | [diff] [blame] | 127 | array = ssd1307fb_alloc_array(1, SSD1307FB_COMMAND); |
| 128 | if (!array) |
| 129 | return -ENOMEM; |
| 130 | |
| 131 | array->data[0] = cmd; |
| 132 | |
| 133 | ret = ssd1307fb_write_array(client, array, 1); |
| 134 | kfree(array); |
| 135 | |
| 136 | return ret; |
Maxime Ripard | a2ed00d | 2012-12-07 12:30:38 -0800 | [diff] [blame] | 137 | } |
| 138 | |
Maxime Ripard | a2ed00d | 2012-12-07 12:30:38 -0800 | [diff] [blame] | 139 | static void ssd1307fb_update_display(struct ssd1307fb_par *par) |
| 140 | { |
Maxime Ripard | 301bc06 | 2013-04-22 12:02:25 +0200 | [diff] [blame] | 141 | struct ssd1307fb_array *array; |
Maxime Ripard | a2ed00d | 2012-12-07 12:30:38 -0800 | [diff] [blame] | 142 | u8 *vmem = par->info->screen_base; |
| 143 | int i, j, k; |
| 144 | |
Maxime Ripard | 301bc06 | 2013-04-22 12:02:25 +0200 | [diff] [blame] | 145 | array = ssd1307fb_alloc_array(par->width * par->height / 8, |
| 146 | SSD1307FB_DATA); |
| 147 | if (!array) |
| 148 | return; |
| 149 | |
Maxime Ripard | a2ed00d | 2012-12-07 12:30:38 -0800 | [diff] [blame] | 150 | /* |
| 151 | * The screen is divided in pages, each having a height of 8 |
| 152 | * pixels, and the width of the screen. When sending a byte of |
| 153 | * data to the controller, it gives the 8 bits for the current |
| 154 | * column. I.e, the first byte are the 8 bits of the first |
| 155 | * column, then the 8 bits for the second column, etc. |
| 156 | * |
| 157 | * |
| 158 | * Representation of the screen, assuming it is 5 bits |
| 159 | * wide. Each letter-number combination is a bit that controls |
| 160 | * one pixel. |
| 161 | * |
| 162 | * A0 A1 A2 A3 A4 |
| 163 | * B0 B1 B2 B3 B4 |
| 164 | * C0 C1 C2 C3 C4 |
| 165 | * D0 D1 D2 D3 D4 |
| 166 | * E0 E1 E2 E3 E4 |
| 167 | * F0 F1 F2 F3 F4 |
| 168 | * G0 G1 G2 G3 G4 |
| 169 | * H0 H1 H2 H3 H4 |
| 170 | * |
| 171 | * If you want to update this screen, you need to send 5 bytes: |
| 172 | * (1) A0 B0 C0 D0 E0 F0 G0 H0 |
| 173 | * (2) A1 B1 C1 D1 E1 F1 G1 H1 |
| 174 | * (3) A2 B2 C2 D2 E2 F2 G2 H2 |
| 175 | * (4) A3 B3 C3 D3 E3 F3 G3 H3 |
| 176 | * (5) A4 B4 C4 D4 E4 F4 G4 H4 |
| 177 | */ |
| 178 | |
Maxime Ripard | bbc7908 | 2013-04-22 11:55:54 +0200 | [diff] [blame] | 179 | for (i = 0; i < (par->height / 8); i++) { |
Maxime Ripard | bbc7908 | 2013-04-22 11:55:54 +0200 | [diff] [blame] | 180 | for (j = 0; j < par->width; j++) { |
Maxime Ripard | 301bc06 | 2013-04-22 12:02:25 +0200 | [diff] [blame] | 181 | u32 array_idx = i * par->width + j; |
| 182 | array->data[array_idx] = 0; |
Maxime Ripard | a2ed00d | 2012-12-07 12:30:38 -0800 | [diff] [blame] | 183 | for (k = 0; k < 8; k++) { |
Maxime Ripard | bbc7908 | 2013-04-22 11:55:54 +0200 | [diff] [blame] | 184 | u32 page_length = par->width * i; |
| 185 | u32 index = page_length + (par->width * k + j) / 8; |
Maxime Ripard | a2ed00d | 2012-12-07 12:30:38 -0800 | [diff] [blame] | 186 | u8 byte = *(vmem + index); |
Maxime Ripard | 552f0cc | 2013-01-11 14:31:43 -0800 | [diff] [blame] | 187 | u8 bit = byte & (1 << (j % 8)); |
| 188 | bit = bit >> (j % 8); |
Maxime Ripard | 301bc06 | 2013-04-22 12:02:25 +0200 | [diff] [blame] | 189 | array->data[array_idx] |= bit << k; |
Maxime Ripard | a2ed00d | 2012-12-07 12:30:38 -0800 | [diff] [blame] | 190 | } |
Maxime Ripard | a2ed00d | 2012-12-07 12:30:38 -0800 | [diff] [blame] | 191 | } |
| 192 | } |
Maxime Ripard | 301bc06 | 2013-04-22 12:02:25 +0200 | [diff] [blame] | 193 | |
| 194 | ssd1307fb_write_array(par->client, array, par->width * par->height / 8); |
| 195 | kfree(array); |
Maxime Ripard | a2ed00d | 2012-12-07 12:30:38 -0800 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | |
| 199 | static ssize_t ssd1307fb_write(struct fb_info *info, const char __user *buf, |
| 200 | size_t count, loff_t *ppos) |
| 201 | { |
| 202 | struct ssd1307fb_par *par = info->par; |
| 203 | unsigned long total_size; |
| 204 | unsigned long p = *ppos; |
| 205 | u8 __iomem *dst; |
| 206 | |
| 207 | total_size = info->fix.smem_len; |
| 208 | |
| 209 | if (p > total_size) |
| 210 | return -EINVAL; |
| 211 | |
| 212 | if (count + p > total_size) |
| 213 | count = total_size - p; |
| 214 | |
| 215 | if (!count) |
| 216 | return -EINVAL; |
| 217 | |
| 218 | dst = (void __force *) (info->screen_base + p); |
| 219 | |
| 220 | if (copy_from_user(dst, buf, count)) |
| 221 | return -EFAULT; |
| 222 | |
| 223 | ssd1307fb_update_display(par); |
| 224 | |
| 225 | *ppos += count; |
| 226 | |
| 227 | return count; |
| 228 | } |
| 229 | |
| 230 | static void ssd1307fb_fillrect(struct fb_info *info, const struct fb_fillrect *rect) |
| 231 | { |
| 232 | struct ssd1307fb_par *par = info->par; |
| 233 | sys_fillrect(info, rect); |
| 234 | ssd1307fb_update_display(par); |
| 235 | } |
| 236 | |
| 237 | static void ssd1307fb_copyarea(struct fb_info *info, const struct fb_copyarea *area) |
| 238 | { |
| 239 | struct ssd1307fb_par *par = info->par; |
| 240 | sys_copyarea(info, area); |
| 241 | ssd1307fb_update_display(par); |
| 242 | } |
| 243 | |
| 244 | static void ssd1307fb_imageblit(struct fb_info *info, const struct fb_image *image) |
| 245 | { |
| 246 | struct ssd1307fb_par *par = info->par; |
| 247 | sys_imageblit(info, image); |
| 248 | ssd1307fb_update_display(par); |
| 249 | } |
| 250 | |
| 251 | static struct fb_ops ssd1307fb_ops = { |
| 252 | .owner = THIS_MODULE, |
| 253 | .fb_read = fb_sys_read, |
| 254 | .fb_write = ssd1307fb_write, |
| 255 | .fb_fillrect = ssd1307fb_fillrect, |
| 256 | .fb_copyarea = ssd1307fb_copyarea, |
| 257 | .fb_imageblit = ssd1307fb_imageblit, |
| 258 | }; |
| 259 | |
| 260 | static void ssd1307fb_deferred_io(struct fb_info *info, |
| 261 | struct list_head *pagelist) |
| 262 | { |
| 263 | ssd1307fb_update_display(info->par); |
| 264 | } |
| 265 | |
| 266 | static struct fb_deferred_io ssd1307fb_defio = { |
| 267 | .delay = HZ, |
| 268 | .deferred_io = ssd1307fb_deferred_io, |
| 269 | }; |
| 270 | |
Thomas Niederprüm | c89eacf | 2015-03-31 20:27:10 +0200 | [diff] [blame] | 271 | static int ssd1307fb_init(struct ssd1307fb_par *par) |
Maxime Ripard | bbc7908 | 2013-04-22 11:55:54 +0200 | [diff] [blame] | 272 | { |
| 273 | int ret; |
Thomas Niederprüm | c89eacf | 2015-03-31 20:27:10 +0200 | [diff] [blame] | 274 | u32 precharge, dclk, com_invdir, compins; |
Maxime Ripard | bbc7908 | 2013-04-22 11:55:54 +0200 | [diff] [blame] | 275 | |
Thomas Niederprüm | c89eacf | 2015-03-31 20:27:10 +0200 | [diff] [blame] | 276 | if (par->device_info->need_pwm) { |
| 277 | par->pwm = pwm_get(&par->client->dev, NULL); |
| 278 | if (IS_ERR(par->pwm)) { |
| 279 | dev_err(&par->client->dev, "Could not get PWM from device tree!\n"); |
| 280 | return PTR_ERR(par->pwm); |
| 281 | } |
Maxime Ripard | bbc7908 | 2013-04-22 11:55:54 +0200 | [diff] [blame] | 282 | |
Thomas Niederprüm | c89eacf | 2015-03-31 20:27:10 +0200 | [diff] [blame] | 283 | par->pwm_period = pwm_get_period(par->pwm); |
| 284 | /* Enable the PWM */ |
| 285 | pwm_config(par->pwm, par->pwm_period / 2, par->pwm_period); |
| 286 | pwm_enable(par->pwm); |
Maxime Ripard | bbc7908 | 2013-04-22 11:55:54 +0200 | [diff] [blame] | 287 | |
Thomas Niederprüm | c89eacf | 2015-03-31 20:27:10 +0200 | [diff] [blame] | 288 | dev_dbg(&par->client->dev, "Using PWM%d with a %dns period.\n", |
| 289 | par->pwm->pwm, par->pwm_period); |
| 290 | }; |
Maxime Ripard | bbc7908 | 2013-04-22 11:55:54 +0200 | [diff] [blame] | 291 | |
| 292 | /* Set initial contrast */ |
| 293 | ret = ssd1307fb_write_cmd(par->client, SSD1307FB_CONTRAST); |
Prabhakar Lad | 5b72ae9 | 2015-01-15 19:05:37 +0000 | [diff] [blame] | 294 | if (ret < 0) |
| 295 | return ret; |
| 296 | |
Thomas Niederprüm | c89eacf | 2015-03-31 20:27:10 +0200 | [diff] [blame] | 297 | ret = ssd1307fb_write_cmd(par->client, par->contrast); |
Maxime Ripard | bbc7908 | 2013-04-22 11:55:54 +0200 | [diff] [blame] | 298 | if (ret < 0) |
| 299 | return ret; |
| 300 | |
| 301 | /* Set segment re-map */ |
Thomas Niederprüm | c89eacf | 2015-03-31 20:27:10 +0200 | [diff] [blame] | 302 | if (par->seg_remap) { |
| 303 | ret = ssd1307fb_write_cmd(par->client, SSD1307FB_SEG_REMAP_ON); |
| 304 | if (ret < 0) |
| 305 | return ret; |
| 306 | }; |
| 307 | |
| 308 | /* Set COM direction */ |
| 309 | com_invdir = 0xc0 | (par->com_invdir & 0x1) << 3; |
| 310 | ret = ssd1307fb_write_cmd(par->client, com_invdir); |
Maxime Ripard | bbc7908 | 2013-04-22 11:55:54 +0200 | [diff] [blame] | 311 | if (ret < 0) |
| 312 | return ret; |
| 313 | |
| 314 | /* Set multiplex ratio value */ |
| 315 | ret = ssd1307fb_write_cmd(par->client, SSD1307FB_SET_MULTIPLEX_RATIO); |
Prabhakar Lad | 5b72ae9 | 2015-01-15 19:05:37 +0000 | [diff] [blame] | 316 | if (ret < 0) |
| 317 | return ret; |
| 318 | |
| 319 | ret = ssd1307fb_write_cmd(par->client, par->height - 1); |
Maxime Ripard | bbc7908 | 2013-04-22 11:55:54 +0200 | [diff] [blame] | 320 | if (ret < 0) |
| 321 | return ret; |
| 322 | |
| 323 | /* set display offset value */ |
| 324 | ret = ssd1307fb_write_cmd(par->client, SSD1307FB_SET_DISPLAY_OFFSET); |
Prabhakar Lad | 5b72ae9 | 2015-01-15 19:05:37 +0000 | [diff] [blame] | 325 | if (ret < 0) |
| 326 | return ret; |
| 327 | |
Thomas Niederprüm | c89eacf | 2015-03-31 20:27:10 +0200 | [diff] [blame] | 328 | ret = ssd1307fb_write_cmd(par->client, par->com_offset); |
Maxime Ripard | bbc7908 | 2013-04-22 11:55:54 +0200 | [diff] [blame] | 329 | if (ret < 0) |
| 330 | return ret; |
| 331 | |
| 332 | /* Set clock frequency */ |
| 333 | ret = ssd1307fb_write_cmd(par->client, SSD1307FB_SET_CLOCK_FREQ); |
Prabhakar Lad | 5b72ae9 | 2015-01-15 19:05:37 +0000 | [diff] [blame] | 334 | if (ret < 0) |
| 335 | return ret; |
| 336 | |
Thomas Niederprüm | c89eacf | 2015-03-31 20:27:10 +0200 | [diff] [blame] | 337 | dclk = ((par->dclk_div - 1) & 0xf) | (par->dclk_frq & 0xf) << 4; |
| 338 | ret = ssd1307fb_write_cmd(par->client, dclk); |
Maxime Ripard | bbc7908 | 2013-04-22 11:55:54 +0200 | [diff] [blame] | 339 | if (ret < 0) |
| 340 | return ret; |
| 341 | |
| 342 | /* Set precharge period in number of ticks from the internal clock */ |
| 343 | ret = ssd1307fb_write_cmd(par->client, SSD1307FB_SET_PRECHARGE_PERIOD); |
Prabhakar Lad | 5b72ae9 | 2015-01-15 19:05:37 +0000 | [diff] [blame] | 344 | if (ret < 0) |
| 345 | return ret; |
| 346 | |
Thomas Niederprüm | c89eacf | 2015-03-31 20:27:10 +0200 | [diff] [blame] | 347 | precharge = (par->prechargep1 & 0xf) | (par->prechargep2 & 0xf) << 4; |
| 348 | ret = ssd1307fb_write_cmd(par->client, precharge); |
Maxime Ripard | bbc7908 | 2013-04-22 11:55:54 +0200 | [diff] [blame] | 349 | if (ret < 0) |
| 350 | return ret; |
| 351 | |
| 352 | /* Set COM pins configuration */ |
| 353 | ret = ssd1307fb_write_cmd(par->client, SSD1307FB_SET_COM_PINS_CONFIG); |
Prabhakar Lad | 5b72ae9 | 2015-01-15 19:05:37 +0000 | [diff] [blame] | 354 | if (ret < 0) |
| 355 | return ret; |
| 356 | |
Thomas Niederprüm | c89eacf | 2015-03-31 20:27:10 +0200 | [diff] [blame] | 357 | compins = 0x02 | (!par->com_seq & 0x1) << 4 |
| 358 | | (par->com_lrremap & 0x1) << 5; |
| 359 | ret = ssd1307fb_write_cmd(par->client, compins); |
Maxime Ripard | bbc7908 | 2013-04-22 11:55:54 +0200 | [diff] [blame] | 360 | if (ret < 0) |
| 361 | return ret; |
| 362 | |
| 363 | /* Set VCOMH */ |
| 364 | ret = ssd1307fb_write_cmd(par->client, SSD1307FB_SET_VCOMH); |
Prabhakar Lad | 5b72ae9 | 2015-01-15 19:05:37 +0000 | [diff] [blame] | 365 | if (ret < 0) |
| 366 | return ret; |
| 367 | |
Thomas Niederprüm | c89eacf | 2015-03-31 20:27:10 +0200 | [diff] [blame] | 368 | ret = ssd1307fb_write_cmd(par->client, par->vcomh); |
Maxime Ripard | bbc7908 | 2013-04-22 11:55:54 +0200 | [diff] [blame] | 369 | if (ret < 0) |
| 370 | return ret; |
| 371 | |
| 372 | /* Turn on the DC-DC Charge Pump */ |
| 373 | ret = ssd1307fb_write_cmd(par->client, SSD1307FB_CHARGE_PUMP); |
Prabhakar Lad | 5b72ae9 | 2015-01-15 19:05:37 +0000 | [diff] [blame] | 374 | if (ret < 0) |
| 375 | return ret; |
| 376 | |
Thomas Niederprüm | c89eacf | 2015-03-31 20:27:10 +0200 | [diff] [blame] | 377 | ret = ssd1307fb_write_cmd(par->client, |
| 378 | (par->device_info->need_chargepump & 0x1 << 2) & 0x14); |
Maxime Ripard | bbc7908 | 2013-04-22 11:55:54 +0200 | [diff] [blame] | 379 | if (ret < 0) |
| 380 | return ret; |
| 381 | |
Maxime Ripard | 301bc06 | 2013-04-22 12:02:25 +0200 | [diff] [blame] | 382 | /* Switch to horizontal addressing mode */ |
| 383 | ret = ssd1307fb_write_cmd(par->client, SSD1307FB_SET_ADDRESS_MODE); |
Prabhakar Lad | 5b72ae9 | 2015-01-15 19:05:37 +0000 | [diff] [blame] | 384 | if (ret < 0) |
| 385 | return ret; |
| 386 | |
| 387 | ret = ssd1307fb_write_cmd(par->client, |
| 388 | SSD1307FB_SET_ADDRESS_MODE_HORIZONTAL); |
Maxime Ripard | 301bc06 | 2013-04-22 12:02:25 +0200 | [diff] [blame] | 389 | if (ret < 0) |
| 390 | return ret; |
| 391 | |
Thomas Niederprüm | c89eacf | 2015-03-31 20:27:10 +0200 | [diff] [blame] | 392 | /* Set column range */ |
Maxime Ripard | 301bc06 | 2013-04-22 12:02:25 +0200 | [diff] [blame] | 393 | ret = ssd1307fb_write_cmd(par->client, SSD1307FB_SET_COL_RANGE); |
Prabhakar Lad | 5b72ae9 | 2015-01-15 19:05:37 +0000 | [diff] [blame] | 394 | if (ret < 0) |
| 395 | return ret; |
| 396 | |
| 397 | ret = ssd1307fb_write_cmd(par->client, 0x0); |
| 398 | if (ret < 0) |
| 399 | return ret; |
| 400 | |
| 401 | ret = ssd1307fb_write_cmd(par->client, par->width - 1); |
Maxime Ripard | 301bc06 | 2013-04-22 12:02:25 +0200 | [diff] [blame] | 402 | if (ret < 0) |
| 403 | return ret; |
| 404 | |
Thomas Niederprüm | c89eacf | 2015-03-31 20:27:10 +0200 | [diff] [blame] | 405 | /* Set page range */ |
Maxime Ripard | 301bc06 | 2013-04-22 12:02:25 +0200 | [diff] [blame] | 406 | ret = ssd1307fb_write_cmd(par->client, SSD1307FB_SET_PAGE_RANGE); |
Prabhakar Lad | 5b72ae9 | 2015-01-15 19:05:37 +0000 | [diff] [blame] | 407 | if (ret < 0) |
| 408 | return ret; |
| 409 | |
| 410 | ret = ssd1307fb_write_cmd(par->client, 0x0); |
| 411 | if (ret < 0) |
| 412 | return ret; |
| 413 | |
| 414 | ret = ssd1307fb_write_cmd(par->client, |
| 415 | par->page_offset + (par->height / 8) - 1); |
Maxime Ripard | 301bc06 | 2013-04-22 12:02:25 +0200 | [diff] [blame] | 416 | if (ret < 0) |
| 417 | return ret; |
| 418 | |
Maxime Ripard | bbc7908 | 2013-04-22 11:55:54 +0200 | [diff] [blame] | 419 | /* Turn on the display */ |
| 420 | ret = ssd1307fb_write_cmd(par->client, SSD1307FB_DISPLAY_ON); |
| 421 | if (ret < 0) |
| 422 | return ret; |
| 423 | |
| 424 | return 0; |
| 425 | } |
| 426 | |
Thomas Niederprüm | 5f2d36b | 2015-03-31 20:27:12 +0200 | [diff] [blame^] | 427 | static struct ssd1307fb_deviceinfo ssd1307fb_ssd1305_deviceinfo = { |
| 428 | .default_vcomh = 0x34, |
| 429 | .default_dclk_div = 1, |
| 430 | .default_dclk_frq = 7, |
| 431 | }; |
| 432 | |
Thomas Niederprüm | c89eacf | 2015-03-31 20:27:10 +0200 | [diff] [blame] | 433 | static struct ssd1307fb_deviceinfo ssd1307fb_ssd1306_deviceinfo = { |
| 434 | .default_vcomh = 0x20, |
| 435 | .default_dclk_div = 1, |
| 436 | .default_dclk_frq = 8, |
| 437 | .need_chargepump = 1, |
| 438 | }; |
| 439 | |
| 440 | static struct ssd1307fb_deviceinfo ssd1307fb_ssd1307_deviceinfo = { |
| 441 | .default_vcomh = 0x20, |
| 442 | .default_dclk_div = 2, |
| 443 | .default_dclk_frq = 12, |
| 444 | .need_pwm = 1, |
Maxime Ripard | bbc7908 | 2013-04-22 11:55:54 +0200 | [diff] [blame] | 445 | }; |
| 446 | |
| 447 | static const struct of_device_id ssd1307fb_of_match[] = { |
| 448 | { |
Thomas Niederprüm | 5f2d36b | 2015-03-31 20:27:12 +0200 | [diff] [blame^] | 449 | .compatible = "solomon,ssd1305fb-i2c", |
| 450 | .data = (void *)&ssd1307fb_ssd1305_deviceinfo, |
| 451 | }, |
| 452 | { |
Maxime Ripard | bbc7908 | 2013-04-22 11:55:54 +0200 | [diff] [blame] | 453 | .compatible = "solomon,ssd1306fb-i2c", |
Thomas Niederprüm | c89eacf | 2015-03-31 20:27:10 +0200 | [diff] [blame] | 454 | .data = (void *)&ssd1307fb_ssd1306_deviceinfo, |
Maxime Ripard | bbc7908 | 2013-04-22 11:55:54 +0200 | [diff] [blame] | 455 | }, |
| 456 | { |
| 457 | .compatible = "solomon,ssd1307fb-i2c", |
Thomas Niederprüm | c89eacf | 2015-03-31 20:27:10 +0200 | [diff] [blame] | 458 | .data = (void *)&ssd1307fb_ssd1307_deviceinfo, |
Maxime Ripard | bbc7908 | 2013-04-22 11:55:54 +0200 | [diff] [blame] | 459 | }, |
| 460 | {}, |
| 461 | }; |
| 462 | MODULE_DEVICE_TABLE(of, ssd1307fb_of_match); |
| 463 | |
Greg Kroah-Hartman | 48c68c4 | 2012-12-21 13:07:39 -0800 | [diff] [blame] | 464 | static int ssd1307fb_probe(struct i2c_client *client, |
| 465 | const struct i2c_device_id *id) |
Maxime Ripard | a2ed00d | 2012-12-07 12:30:38 -0800 | [diff] [blame] | 466 | { |
| 467 | struct fb_info *info; |
Maxime Ripard | bbc7908 | 2013-04-22 11:55:54 +0200 | [diff] [blame] | 468 | struct device_node *node = client->dev.of_node; |
| 469 | u32 vmem_size; |
Maxime Ripard | a2ed00d | 2012-12-07 12:30:38 -0800 | [diff] [blame] | 470 | struct ssd1307fb_par *par; |
| 471 | u8 *vmem; |
| 472 | int ret; |
| 473 | |
Maxime Ripard | bbc7908 | 2013-04-22 11:55:54 +0200 | [diff] [blame] | 474 | if (!node) { |
Maxime Ripard | a2ed00d | 2012-12-07 12:30:38 -0800 | [diff] [blame] | 475 | dev_err(&client->dev, "No device tree data found!\n"); |
| 476 | return -EINVAL; |
| 477 | } |
| 478 | |
| 479 | info = framebuffer_alloc(sizeof(struct ssd1307fb_par), &client->dev); |
| 480 | if (!info) { |
| 481 | dev_err(&client->dev, "Couldn't allocate framebuffer.\n"); |
| 482 | return -ENOMEM; |
| 483 | } |
| 484 | |
Maxime Ripard | bbc7908 | 2013-04-22 11:55:54 +0200 | [diff] [blame] | 485 | par = info->par; |
| 486 | par->info = info; |
| 487 | par->client = client; |
| 488 | |
Thomas Niederprüm | c89eacf | 2015-03-31 20:27:10 +0200 | [diff] [blame] | 489 | par->device_info = (struct ssd1307fb_deviceinfo *)of_match_device( |
| 490 | ssd1307fb_of_match, &client->dev)->data; |
Maxime Ripard | bbc7908 | 2013-04-22 11:55:54 +0200 | [diff] [blame] | 491 | |
| 492 | par->reset = of_get_named_gpio(client->dev.of_node, |
| 493 | "reset-gpios", 0); |
| 494 | if (!gpio_is_valid(par->reset)) { |
| 495 | ret = -EINVAL; |
| 496 | goto fb_alloc_error; |
| 497 | } |
| 498 | |
| 499 | if (of_property_read_u32(node, "solomon,width", &par->width)) |
| 500 | par->width = 96; |
| 501 | |
| 502 | if (of_property_read_u32(node, "solomon,height", &par->height)) |
Lad, Prabhakar | 9561def | 2015-01-08 08:17:58 +0000 | [diff] [blame] | 503 | par->height = 16; |
Maxime Ripard | bbc7908 | 2013-04-22 11:55:54 +0200 | [diff] [blame] | 504 | |
| 505 | if (of_property_read_u32(node, "solomon,page-offset", &par->page_offset)) |
| 506 | par->page_offset = 1; |
| 507 | |
Thomas Niederprüm | c89eacf | 2015-03-31 20:27:10 +0200 | [diff] [blame] | 508 | if (of_property_read_u32(node, "solomon,com-offset", &par->com_offset)) |
| 509 | par->com_offset = 0; |
| 510 | |
| 511 | if (of_property_read_u32(node, "solomon,prechargep1", &par->prechargep1)) |
| 512 | par->prechargep1 = 2; |
| 513 | |
| 514 | if (of_property_read_u32(node, "solomon,prechargep2", &par->prechargep2)) |
| 515 | par->prechargep2 = 2; |
| 516 | |
| 517 | par->seg_remap = !of_property_read_bool(node, "solomon,segment-no-remap"); |
| 518 | par->com_seq = of_property_read_bool(node, "solomon,com-seq"); |
| 519 | par->com_lrremap = of_property_read_bool(node, "solomon,com-lrremap"); |
| 520 | par->com_invdir = of_property_read_bool(node, "solomon,com-invdir"); |
| 521 | |
| 522 | par->contrast = 127; |
| 523 | par->vcomh = par->device_info->default_vcomh; |
| 524 | |
| 525 | /* Setup display timing */ |
| 526 | par->dclk_div = par->device_info->default_dclk_div; |
| 527 | par->dclk_frq = par->device_info->default_dclk_frq; |
| 528 | |
Maxime Ripard | bbc7908 | 2013-04-22 11:55:54 +0200 | [diff] [blame] | 529 | vmem_size = par->width * par->height / 8; |
| 530 | |
Thomas Niederprüm | facd94b | 2015-03-31 20:27:08 +0200 | [diff] [blame] | 531 | vmem = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, |
| 532 | get_order(vmem_size)); |
Maxime Ripard | a2ed00d | 2012-12-07 12:30:38 -0800 | [diff] [blame] | 533 | if (!vmem) { |
| 534 | dev_err(&client->dev, "Couldn't allocate graphical memory.\n"); |
| 535 | ret = -ENOMEM; |
| 536 | goto fb_alloc_error; |
| 537 | } |
| 538 | |
| 539 | info->fbops = &ssd1307fb_ops; |
| 540 | info->fix = ssd1307fb_fix; |
Maxime Ripard | bbc7908 | 2013-04-22 11:55:54 +0200 | [diff] [blame] | 541 | info->fix.line_length = par->width / 8; |
Maxime Ripard | a2ed00d | 2012-12-07 12:30:38 -0800 | [diff] [blame] | 542 | info->fbdefio = &ssd1307fb_defio; |
| 543 | |
| 544 | info->var = ssd1307fb_var; |
Maxime Ripard | bbc7908 | 2013-04-22 11:55:54 +0200 | [diff] [blame] | 545 | info->var.xres = par->width; |
| 546 | info->var.xres_virtual = par->width; |
| 547 | info->var.yres = par->height; |
| 548 | info->var.yres_virtual = par->height; |
| 549 | |
Maxime Ripard | a2ed00d | 2012-12-07 12:30:38 -0800 | [diff] [blame] | 550 | info->var.red.length = 1; |
| 551 | info->var.red.offset = 0; |
| 552 | info->var.green.length = 1; |
| 553 | info->var.green.offset = 0; |
| 554 | info->var.blue.length = 1; |
| 555 | info->var.blue.offset = 0; |
| 556 | |
| 557 | info->screen_base = (u8 __force __iomem *)vmem; |
Thomas Niederprüm | 258c0ea | 2015-03-31 20:27:07 +0200 | [diff] [blame] | 558 | info->fix.smem_start = __pa(vmem); |
Maxime Ripard | a2ed00d | 2012-12-07 12:30:38 -0800 | [diff] [blame] | 559 | info->fix.smem_len = vmem_size; |
| 560 | |
| 561 | fb_deferred_io_init(info); |
| 562 | |
Maxime Ripard | a2ed00d | 2012-12-07 12:30:38 -0800 | [diff] [blame] | 563 | ret = devm_gpio_request_one(&client->dev, par->reset, |
| 564 | GPIOF_OUT_INIT_HIGH, |
| 565 | "oled-reset"); |
| 566 | if (ret) { |
| 567 | dev_err(&client->dev, |
| 568 | "failed to request gpio %d: %d\n", |
| 569 | par->reset, ret); |
| 570 | goto reset_oled_error; |
| 571 | } |
| 572 | |
Maxime Ripard | a2ed00d | 2012-12-07 12:30:38 -0800 | [diff] [blame] | 573 | i2c_set_clientdata(client, info); |
| 574 | |
| 575 | /* Reset the screen */ |
| 576 | gpio_set_value(par->reset, 0); |
| 577 | udelay(4); |
| 578 | gpio_set_value(par->reset, 1); |
| 579 | udelay(4); |
| 580 | |
Thomas Niederprüm | c89eacf | 2015-03-31 20:27:10 +0200 | [diff] [blame] | 581 | ret = ssd1307fb_init(par); |
| 582 | if (ret) |
| 583 | goto reset_oled_error; |
Maxime Ripard | a2ed00d | 2012-12-07 12:30:38 -0800 | [diff] [blame] | 584 | |
Maxime Ripard | bbc7908 | 2013-04-22 11:55:54 +0200 | [diff] [blame] | 585 | ret = register_framebuffer(info); |
| 586 | if (ret) { |
| 587 | dev_err(&client->dev, "Couldn't register the framebuffer\n"); |
| 588 | goto panel_init_error; |
Maxime Ripard | a2ed00d | 2012-12-07 12:30:38 -0800 | [diff] [blame] | 589 | } |
| 590 | |
| 591 | dev_info(&client->dev, "fb%d: %s framebuffer device registered, using %d bytes of video memory\n", info->node, info->fix.id, vmem_size); |
| 592 | |
| 593 | return 0; |
| 594 | |
Maxime Ripard | bbc7908 | 2013-04-22 11:55:54 +0200 | [diff] [blame] | 595 | panel_init_error: |
Thomas Niederprüm | c89eacf | 2015-03-31 20:27:10 +0200 | [diff] [blame] | 596 | if (par->device_info->need_pwm) { |
| 597 | pwm_disable(par->pwm); |
| 598 | pwm_put(par->pwm); |
| 599 | }; |
Maxime Ripard | a2ed00d | 2012-12-07 12:30:38 -0800 | [diff] [blame] | 600 | reset_oled_error: |
| 601 | fb_deferred_io_cleanup(info); |
| 602 | fb_alloc_error: |
| 603 | framebuffer_release(info); |
| 604 | return ret; |
| 605 | } |
| 606 | |
Greg Kroah-Hartman | 48c68c4 | 2012-12-21 13:07:39 -0800 | [diff] [blame] | 607 | static int ssd1307fb_remove(struct i2c_client *client) |
Maxime Ripard | a2ed00d | 2012-12-07 12:30:38 -0800 | [diff] [blame] | 608 | { |
| 609 | struct fb_info *info = i2c_get_clientdata(client); |
| 610 | struct ssd1307fb_par *par = info->par; |
| 611 | |
| 612 | unregister_framebuffer(info); |
Thomas Niederprüm | c89eacf | 2015-03-31 20:27:10 +0200 | [diff] [blame] | 613 | if (par->device_info->need_pwm) { |
| 614 | pwm_disable(par->pwm); |
| 615 | pwm_put(par->pwm); |
| 616 | }; |
Maxime Ripard | a2ed00d | 2012-12-07 12:30:38 -0800 | [diff] [blame] | 617 | fb_deferred_io_cleanup(info); |
Thomas Niederprüm | facd94b | 2015-03-31 20:27:08 +0200 | [diff] [blame] | 618 | __free_pages(__va(info->fix.smem_start), get_order(info->fix.smem_len)); |
Maxime Ripard | a2ed00d | 2012-12-07 12:30:38 -0800 | [diff] [blame] | 619 | framebuffer_release(info); |
| 620 | |
| 621 | return 0; |
| 622 | } |
| 623 | |
| 624 | static const struct i2c_device_id ssd1307fb_i2c_id[] = { |
Thomas Niederprüm | 5f2d36b | 2015-03-31 20:27:12 +0200 | [diff] [blame^] | 625 | { "ssd1305fb", 0 }, |
Maxime Ripard | bbc7908 | 2013-04-22 11:55:54 +0200 | [diff] [blame] | 626 | { "ssd1306fb", 0 }, |
Maxime Ripard | a2ed00d | 2012-12-07 12:30:38 -0800 | [diff] [blame] | 627 | { "ssd1307fb", 0 }, |
| 628 | { } |
| 629 | }; |
| 630 | MODULE_DEVICE_TABLE(i2c, ssd1307fb_i2c_id); |
| 631 | |
Maxime Ripard | a2ed00d | 2012-12-07 12:30:38 -0800 | [diff] [blame] | 632 | static struct i2c_driver ssd1307fb_driver = { |
| 633 | .probe = ssd1307fb_probe, |
Greg Kroah-Hartman | 48c68c4 | 2012-12-21 13:07:39 -0800 | [diff] [blame] | 634 | .remove = ssd1307fb_remove, |
Maxime Ripard | a2ed00d | 2012-12-07 12:30:38 -0800 | [diff] [blame] | 635 | .id_table = ssd1307fb_i2c_id, |
| 636 | .driver = { |
| 637 | .name = "ssd1307fb", |
Sachin Kamat | d71f8cd | 2013-09-30 14:15:31 +0530 | [diff] [blame] | 638 | .of_match_table = ssd1307fb_of_match, |
Maxime Ripard | a2ed00d | 2012-12-07 12:30:38 -0800 | [diff] [blame] | 639 | .owner = THIS_MODULE, |
| 640 | }, |
| 641 | }; |
| 642 | |
| 643 | module_i2c_driver(ssd1307fb_driver); |
| 644 | |
Masanari Iida | a895d57 | 2013-04-09 02:06:50 +0900 | [diff] [blame] | 645 | MODULE_DESCRIPTION("FB driver for the Solomon SSD1307 OLED controller"); |
Maxime Ripard | a2ed00d | 2012-12-07 12:30:38 -0800 | [diff] [blame] | 646 | MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>"); |
| 647 | MODULE_LICENSE("GPL"); |