ssd1307fb.c 14 KB

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