ssd1307fb.c 13 KB

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