ssd1307fb.c 14 KB

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