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