ssd1307fb.c 17 KB

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