ssd1307fb.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  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. ssd1307fb_write_cmd(par->client,
  142. SSD1307FB_START_PAGE_ADDRESS + i + par->page_offset);
  143. ssd1307fb_write_cmd(par->client, 0x00);
  144. ssd1307fb_write_cmd(par->client, 0x10);
  145. for (j = 0; j < par->width; j++) {
  146. u8 buf = 0;
  147. for (k = 0; k < 8; k++) {
  148. u32 page_length = par->width * i;
  149. u32 index = page_length + (par->width * k + j) / 8;
  150. u8 byte = *(vmem + index);
  151. u8 bit = byte & (1 << (j % 8));
  152. bit = bit >> (j % 8);
  153. buf |= bit << k;
  154. }
  155. ssd1307fb_write_data(par->client, buf);
  156. }
  157. }
  158. }
  159. static ssize_t ssd1307fb_write(struct fb_info *info, const char __user *buf,
  160. size_t count, loff_t *ppos)
  161. {
  162. struct ssd1307fb_par *par = info->par;
  163. unsigned long total_size;
  164. unsigned long p = *ppos;
  165. u8 __iomem *dst;
  166. total_size = info->fix.smem_len;
  167. if (p > total_size)
  168. return -EINVAL;
  169. if (count + p > total_size)
  170. count = total_size - p;
  171. if (!count)
  172. return -EINVAL;
  173. dst = (void __force *) (info->screen_base + p);
  174. if (copy_from_user(dst, buf, count))
  175. return -EFAULT;
  176. ssd1307fb_update_display(par);
  177. *ppos += count;
  178. return count;
  179. }
  180. static void ssd1307fb_fillrect(struct fb_info *info, const struct fb_fillrect *rect)
  181. {
  182. struct ssd1307fb_par *par = info->par;
  183. sys_fillrect(info, rect);
  184. ssd1307fb_update_display(par);
  185. }
  186. static void ssd1307fb_copyarea(struct fb_info *info, const struct fb_copyarea *area)
  187. {
  188. struct ssd1307fb_par *par = info->par;
  189. sys_copyarea(info, area);
  190. ssd1307fb_update_display(par);
  191. }
  192. static void ssd1307fb_imageblit(struct fb_info *info, const struct fb_image *image)
  193. {
  194. struct ssd1307fb_par *par = info->par;
  195. sys_imageblit(info, image);
  196. ssd1307fb_update_display(par);
  197. }
  198. static struct fb_ops ssd1307fb_ops = {
  199. .owner = THIS_MODULE,
  200. .fb_read = fb_sys_read,
  201. .fb_write = ssd1307fb_write,
  202. .fb_fillrect = ssd1307fb_fillrect,
  203. .fb_copyarea = ssd1307fb_copyarea,
  204. .fb_imageblit = ssd1307fb_imageblit,
  205. };
  206. static void ssd1307fb_deferred_io(struct fb_info *info,
  207. struct list_head *pagelist)
  208. {
  209. ssd1307fb_update_display(info->par);
  210. }
  211. static struct fb_deferred_io ssd1307fb_defio = {
  212. .delay = HZ,
  213. .deferred_io = ssd1307fb_deferred_io,
  214. };
  215. static int ssd1307fb_ssd1307_init(struct ssd1307fb_par *par)
  216. {
  217. int ret;
  218. par->pwm = pwm_get(&par->client->dev, NULL);
  219. if (IS_ERR(par->pwm)) {
  220. dev_err(&par->client->dev, "Could not get PWM from device tree!\n");
  221. return PTR_ERR(par->pwm);
  222. }
  223. par->pwm_period = pwm_get_period(par->pwm);
  224. /* Enable the PWM */
  225. pwm_config(par->pwm, par->pwm_period / 2, par->pwm_period);
  226. pwm_enable(par->pwm);
  227. dev_dbg(&par->client->dev, "Using PWM%d with a %dns period.\n",
  228. par->pwm->pwm, par->pwm_period);
  229. /* Map column 127 of the OLED to segment 0 */
  230. ret = ssd1307fb_write_cmd(par->client, SSD1307FB_SEG_REMAP_ON);
  231. if (ret < 0)
  232. return ret;
  233. /* Turn on the display */
  234. ret = ssd1307fb_write_cmd(par->client, SSD1307FB_DISPLAY_ON);
  235. if (ret < 0)
  236. return ret;
  237. return 0;
  238. }
  239. static int ssd1307fb_ssd1307_remove(struct ssd1307fb_par *par)
  240. {
  241. pwm_disable(par->pwm);
  242. pwm_put(par->pwm);
  243. return 0;
  244. }
  245. static struct ssd1307fb_ops ssd1307fb_ssd1307_ops = {
  246. .init = ssd1307fb_ssd1307_init,
  247. .remove = ssd1307fb_ssd1307_remove,
  248. };
  249. static int ssd1307fb_ssd1306_init(struct ssd1307fb_par *par)
  250. {
  251. int ret;
  252. /* Set initial contrast */
  253. ret = ssd1307fb_write_cmd(par->client, SSD1307FB_CONTRAST);
  254. ret = ret & ssd1307fb_write_cmd(par->client, 0x7f);
  255. if (ret < 0)
  256. return ret;
  257. /* Set COM direction */
  258. ret = ssd1307fb_write_cmd(par->client, 0xc8);
  259. if (ret < 0)
  260. return ret;
  261. /* Set segment re-map */
  262. ret = ssd1307fb_write_cmd(par->client, SSD1307FB_SEG_REMAP_ON);
  263. if (ret < 0)
  264. return ret;
  265. /* Set multiplex ratio value */
  266. ret = ssd1307fb_write_cmd(par->client, SSD1307FB_SET_MULTIPLEX_RATIO);
  267. ret = ret & ssd1307fb_write_cmd(par->client, par->height - 1);
  268. if (ret < 0)
  269. return ret;
  270. /* set display offset value */
  271. ret = ssd1307fb_write_cmd(par->client, SSD1307FB_SET_DISPLAY_OFFSET);
  272. ret = ssd1307fb_write_cmd(par->client, 0x20);
  273. if (ret < 0)
  274. return ret;
  275. /* Set clock frequency */
  276. ret = ssd1307fb_write_cmd(par->client, SSD1307FB_SET_CLOCK_FREQ);
  277. ret = ret & ssd1307fb_write_cmd(par->client, 0xf0);
  278. if (ret < 0)
  279. return ret;
  280. /* Set precharge period in number of ticks from the internal clock */
  281. ret = ssd1307fb_write_cmd(par->client, SSD1307FB_SET_PRECHARGE_PERIOD);
  282. ret = ret & ssd1307fb_write_cmd(par->client, 0x22);
  283. if (ret < 0)
  284. return ret;
  285. /* Set COM pins configuration */
  286. ret = ssd1307fb_write_cmd(par->client, SSD1307FB_SET_COM_PINS_CONFIG);
  287. ret = ret & ssd1307fb_write_cmd(par->client, 0x22);
  288. if (ret < 0)
  289. return ret;
  290. /* Set VCOMH */
  291. ret = ssd1307fb_write_cmd(par->client, SSD1307FB_SET_VCOMH);
  292. ret = ret & ssd1307fb_write_cmd(par->client, 0x49);
  293. if (ret < 0)
  294. return ret;
  295. /* Turn on the DC-DC Charge Pump */
  296. ret = ssd1307fb_write_cmd(par->client, SSD1307FB_CHARGE_PUMP);
  297. ret = ret & ssd1307fb_write_cmd(par->client, 0x14);
  298. if (ret < 0)
  299. return ret;
  300. /* Turn on the display */
  301. ret = ssd1307fb_write_cmd(par->client, SSD1307FB_DISPLAY_ON);
  302. if (ret < 0)
  303. return ret;
  304. return 0;
  305. }
  306. static struct ssd1307fb_ops ssd1307fb_ssd1306_ops = {
  307. .init = ssd1307fb_ssd1306_init,
  308. };
  309. static const struct of_device_id ssd1307fb_of_match[] = {
  310. {
  311. .compatible = "solomon,ssd1306fb-i2c",
  312. .data = (void *)&ssd1307fb_ssd1306_ops,
  313. },
  314. {
  315. .compatible = "solomon,ssd1307fb-i2c",
  316. .data = (void *)&ssd1307fb_ssd1307_ops,
  317. },
  318. {},
  319. };
  320. MODULE_DEVICE_TABLE(of, ssd1307fb_of_match);
  321. static int ssd1307fb_probe(struct i2c_client *client,
  322. const struct i2c_device_id *id)
  323. {
  324. struct fb_info *info;
  325. struct device_node *node = client->dev.of_node;
  326. u32 vmem_size;
  327. struct ssd1307fb_par *par;
  328. u8 *vmem;
  329. int ret;
  330. if (!node) {
  331. dev_err(&client->dev, "No device tree data found!\n");
  332. return -EINVAL;
  333. }
  334. info = framebuffer_alloc(sizeof(struct ssd1307fb_par), &client->dev);
  335. if (!info) {
  336. dev_err(&client->dev, "Couldn't allocate framebuffer.\n");
  337. return -ENOMEM;
  338. }
  339. par = info->par;
  340. par->info = info;
  341. par->client = client;
  342. par->ops = (struct ssd1307fb_ops *)of_match_device(ssd1307fb_of_match,
  343. &client->dev)->data;
  344. par->reset = of_get_named_gpio(client->dev.of_node,
  345. "reset-gpios", 0);
  346. if (!gpio_is_valid(par->reset)) {
  347. ret = -EINVAL;
  348. goto fb_alloc_error;
  349. }
  350. if (of_property_read_u32(node, "solomon,width", &par->width))
  351. par->width = 96;
  352. if (of_property_read_u32(node, "solomon,height", &par->height))
  353. par->width = 16;
  354. if (of_property_read_u32(node, "solomon,page-offset", &par->page_offset))
  355. par->page_offset = 1;
  356. vmem_size = par->width * par->height / 8;
  357. vmem = devm_kzalloc(&client->dev, vmem_size, GFP_KERNEL);
  358. if (!vmem) {
  359. dev_err(&client->dev, "Couldn't allocate graphical memory.\n");
  360. ret = -ENOMEM;
  361. goto fb_alloc_error;
  362. }
  363. info->fbops = &ssd1307fb_ops;
  364. info->fix = ssd1307fb_fix;
  365. info->fix.line_length = par->width / 8;
  366. info->fbdefio = &ssd1307fb_defio;
  367. info->var = ssd1307fb_var;
  368. info->var.xres = par->width;
  369. info->var.xres_virtual = par->width;
  370. info->var.yres = par->height;
  371. info->var.yres_virtual = par->height;
  372. info->var.red.length = 1;
  373. info->var.red.offset = 0;
  374. info->var.green.length = 1;
  375. info->var.green.offset = 0;
  376. info->var.blue.length = 1;
  377. info->var.blue.offset = 0;
  378. info->screen_base = (u8 __force __iomem *)vmem;
  379. info->fix.smem_start = (unsigned long)vmem;
  380. info->fix.smem_len = vmem_size;
  381. fb_deferred_io_init(info);
  382. ret = devm_gpio_request_one(&client->dev, par->reset,
  383. GPIOF_OUT_INIT_HIGH,
  384. "oled-reset");
  385. if (ret) {
  386. dev_err(&client->dev,
  387. "failed to request gpio %d: %d\n",
  388. par->reset, ret);
  389. goto reset_oled_error;
  390. }
  391. i2c_set_clientdata(client, info);
  392. /* Reset the screen */
  393. gpio_set_value(par->reset, 0);
  394. udelay(4);
  395. gpio_set_value(par->reset, 1);
  396. udelay(4);
  397. if (par->ops->init) {
  398. ret = par->ops->init(par);
  399. if (ret)
  400. goto reset_oled_error;
  401. }
  402. ret = register_framebuffer(info);
  403. if (ret) {
  404. dev_err(&client->dev, "Couldn't register the framebuffer\n");
  405. goto panel_init_error;
  406. }
  407. dev_info(&client->dev, "fb%d: %s framebuffer device registered, using %d bytes of video memory\n", info->node, info->fix.id, vmem_size);
  408. return 0;
  409. panel_init_error:
  410. if (par->ops->remove)
  411. par->ops->remove(par);
  412. reset_oled_error:
  413. fb_deferred_io_cleanup(info);
  414. fb_alloc_error:
  415. framebuffer_release(info);
  416. return ret;
  417. }
  418. static int ssd1307fb_remove(struct i2c_client *client)
  419. {
  420. struct fb_info *info = i2c_get_clientdata(client);
  421. struct ssd1307fb_par *par = info->par;
  422. unregister_framebuffer(info);
  423. if (par->ops->remove)
  424. par->ops->remove(par);
  425. fb_deferred_io_cleanup(info);
  426. framebuffer_release(info);
  427. return 0;
  428. }
  429. static const struct i2c_device_id ssd1307fb_i2c_id[] = {
  430. { "ssd1306fb", 0 },
  431. { "ssd1307fb", 0 },
  432. { }
  433. };
  434. MODULE_DEVICE_TABLE(i2c, ssd1307fb_i2c_id);
  435. static struct i2c_driver ssd1307fb_driver = {
  436. .probe = ssd1307fb_probe,
  437. .remove = ssd1307fb_remove,
  438. .id_table = ssd1307fb_i2c_id,
  439. .driver = {
  440. .name = "ssd1307fb",
  441. .of_match_table = of_match_ptr(ssd1307fb_of_match),
  442. .owner = THIS_MODULE,
  443. },
  444. };
  445. module_i2c_driver(ssd1307fb_driver);
  446. MODULE_DESCRIPTION("FB driver for the Solomon SSD1307 OLED controller");
  447. MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");
  448. MODULE_LICENSE("GPL");