ssd1307fb.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. /*
  2. * Driver for the Solomon SSD1307 OLED controler
  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_WIDTH 96
  18. #define SSD1307FB_HEIGHT 16
  19. #define SSD1307FB_DATA 0x40
  20. #define SSD1307FB_COMMAND 0x80
  21. #define SSD1307FB_CONTRAST 0x81
  22. #define SSD1307FB_SEG_REMAP_ON 0xa1
  23. #define SSD1307FB_DISPLAY_OFF 0xae
  24. #define SSD1307FB_DISPLAY_ON 0xaf
  25. #define SSD1307FB_START_PAGE_ADDRESS 0xb0
  26. struct ssd1307fb_par {
  27. struct i2c_client *client;
  28. struct fb_info *info;
  29. struct pwm_device *pwm;
  30. u32 pwm_period;
  31. int reset;
  32. };
  33. static struct fb_fix_screeninfo ssd1307fb_fix = {
  34. .id = "Solomon SSD1307",
  35. .type = FB_TYPE_PACKED_PIXELS,
  36. .visual = FB_VISUAL_MONO10,
  37. .xpanstep = 0,
  38. .ypanstep = 0,
  39. .ywrapstep = 0,
  40. .line_length = SSD1307FB_WIDTH / 8,
  41. .accel = FB_ACCEL_NONE,
  42. };
  43. static struct fb_var_screeninfo ssd1307fb_var = {
  44. .xres = SSD1307FB_WIDTH,
  45. .yres = SSD1307FB_HEIGHT,
  46. .xres_virtual = SSD1307FB_WIDTH,
  47. .yres_virtual = SSD1307FB_HEIGHT,
  48. .bits_per_pixel = 1,
  49. };
  50. static int ssd1307fb_write_array(struct i2c_client *client, u8 type, u8 *cmd, u32 len)
  51. {
  52. u8 *buf;
  53. int ret = 0;
  54. buf = kzalloc(len + 1, GFP_KERNEL);
  55. if (!buf) {
  56. dev_err(&client->dev, "Couldn't allocate sending buffer.\n");
  57. return -ENOMEM;
  58. }
  59. buf[0] = type;
  60. memcpy(buf + 1, cmd, len);
  61. ret = i2c_master_send(client, buf, len + 1);
  62. if (ret != len + 1) {
  63. dev_err(&client->dev, "Couldn't send I2C command.\n");
  64. goto error;
  65. }
  66. error:
  67. kfree(buf);
  68. return ret;
  69. }
  70. static inline int ssd1307fb_write_cmd_array(struct i2c_client *client, u8 *cmd, u32 len)
  71. {
  72. return ssd1307fb_write_array(client, SSD1307FB_COMMAND, cmd, len);
  73. }
  74. static inline int ssd1307fb_write_cmd(struct i2c_client *client, u8 cmd)
  75. {
  76. return ssd1307fb_write_cmd_array(client, &cmd, 1);
  77. }
  78. static inline int ssd1307fb_write_data_array(struct i2c_client *client, u8 *cmd, u32 len)
  79. {
  80. return ssd1307fb_write_array(client, SSD1307FB_DATA, cmd, len);
  81. }
  82. static inline int ssd1307fb_write_data(struct i2c_client *client, u8 data)
  83. {
  84. return ssd1307fb_write_data_array(client, &data, 1);
  85. }
  86. static void ssd1307fb_update_display(struct ssd1307fb_par *par)
  87. {
  88. u8 *vmem = par->info->screen_base;
  89. int i, j, k;
  90. /*
  91. * The screen is divided in pages, each having a height of 8
  92. * pixels, and the width of the screen. When sending a byte of
  93. * data to the controller, it gives the 8 bits for the current
  94. * column. I.e, the first byte are the 8 bits of the first
  95. * column, then the 8 bits for the second column, etc.
  96. *
  97. *
  98. * Representation of the screen, assuming it is 5 bits
  99. * wide. Each letter-number combination is a bit that controls
  100. * one pixel.
  101. *
  102. * A0 A1 A2 A3 A4
  103. * B0 B1 B2 B3 B4
  104. * C0 C1 C2 C3 C4
  105. * D0 D1 D2 D3 D4
  106. * E0 E1 E2 E3 E4
  107. * F0 F1 F2 F3 F4
  108. * G0 G1 G2 G3 G4
  109. * H0 H1 H2 H3 H4
  110. *
  111. * If you want to update this screen, you need to send 5 bytes:
  112. * (1) A0 B0 C0 D0 E0 F0 G0 H0
  113. * (2) A1 B1 C1 D1 E1 F1 G1 H1
  114. * (3) A2 B2 C2 D2 E2 F2 G2 H2
  115. * (4) A3 B3 C3 D3 E3 F3 G3 H3
  116. * (5) A4 B4 C4 D4 E4 F4 G4 H4
  117. */
  118. for (i = 0; i < (SSD1307FB_HEIGHT / 8); i++) {
  119. ssd1307fb_write_cmd(par->client, SSD1307FB_START_PAGE_ADDRESS + (i + 1));
  120. ssd1307fb_write_cmd(par->client, 0x00);
  121. ssd1307fb_write_cmd(par->client, 0x10);
  122. for (j = 0; j < SSD1307FB_WIDTH; j++) {
  123. u8 buf = 0;
  124. for (k = 0; k < 8; k++) {
  125. u32 page_length = SSD1307FB_WIDTH * i;
  126. u32 index = page_length + (SSD1307FB_WIDTH * k + j) / 8;
  127. u8 byte = *(vmem + index);
  128. u8 bit = byte & (1 << (j % 8));
  129. bit = bit >> (j % 8);
  130. buf |= bit << k;
  131. }
  132. ssd1307fb_write_data(par->client, buf);
  133. }
  134. }
  135. }
  136. static ssize_t ssd1307fb_write(struct fb_info *info, const char __user *buf,
  137. size_t count, loff_t *ppos)
  138. {
  139. struct ssd1307fb_par *par = info->par;
  140. unsigned long total_size;
  141. unsigned long p = *ppos;
  142. u8 __iomem *dst;
  143. total_size = info->fix.smem_len;
  144. if (p > total_size)
  145. return -EINVAL;
  146. if (count + p > total_size)
  147. count = total_size - p;
  148. if (!count)
  149. return -EINVAL;
  150. dst = (void __force *) (info->screen_base + p);
  151. if (copy_from_user(dst, buf, count))
  152. return -EFAULT;
  153. ssd1307fb_update_display(par);
  154. *ppos += count;
  155. return count;
  156. }
  157. static void ssd1307fb_fillrect(struct fb_info *info, const struct fb_fillrect *rect)
  158. {
  159. struct ssd1307fb_par *par = info->par;
  160. sys_fillrect(info, rect);
  161. ssd1307fb_update_display(par);
  162. }
  163. static void ssd1307fb_copyarea(struct fb_info *info, const struct fb_copyarea *area)
  164. {
  165. struct ssd1307fb_par *par = info->par;
  166. sys_copyarea(info, area);
  167. ssd1307fb_update_display(par);
  168. }
  169. static void ssd1307fb_imageblit(struct fb_info *info, const struct fb_image *image)
  170. {
  171. struct ssd1307fb_par *par = info->par;
  172. sys_imageblit(info, image);
  173. ssd1307fb_update_display(par);
  174. }
  175. static struct fb_ops ssd1307fb_ops = {
  176. .owner = THIS_MODULE,
  177. .fb_read = fb_sys_read,
  178. .fb_write = ssd1307fb_write,
  179. .fb_fillrect = ssd1307fb_fillrect,
  180. .fb_copyarea = ssd1307fb_copyarea,
  181. .fb_imageblit = ssd1307fb_imageblit,
  182. };
  183. static void ssd1307fb_deferred_io(struct fb_info *info,
  184. struct list_head *pagelist)
  185. {
  186. ssd1307fb_update_display(info->par);
  187. }
  188. static struct fb_deferred_io ssd1307fb_defio = {
  189. .delay = HZ,
  190. .deferred_io = ssd1307fb_deferred_io,
  191. };
  192. static int ssd1307fb_probe(struct i2c_client *client,
  193. const struct i2c_device_id *id)
  194. {
  195. struct fb_info *info;
  196. u32 vmem_size = SSD1307FB_WIDTH * SSD1307FB_HEIGHT / 8;
  197. struct ssd1307fb_par *par;
  198. u8 *vmem;
  199. int ret;
  200. if (!client->dev.of_node) {
  201. dev_err(&client->dev, "No device tree data found!\n");
  202. return -EINVAL;
  203. }
  204. info = framebuffer_alloc(sizeof(struct ssd1307fb_par), &client->dev);
  205. if (!info) {
  206. dev_err(&client->dev, "Couldn't allocate framebuffer.\n");
  207. return -ENOMEM;
  208. }
  209. vmem = devm_kzalloc(&client->dev, vmem_size, GFP_KERNEL);
  210. if (!vmem) {
  211. dev_err(&client->dev, "Couldn't allocate graphical memory.\n");
  212. ret = -ENOMEM;
  213. goto fb_alloc_error;
  214. }
  215. info->fbops = &ssd1307fb_ops;
  216. info->fix = ssd1307fb_fix;
  217. info->fbdefio = &ssd1307fb_defio;
  218. info->var = ssd1307fb_var;
  219. info->var.red.length = 1;
  220. info->var.red.offset = 0;
  221. info->var.green.length = 1;
  222. info->var.green.offset = 0;
  223. info->var.blue.length = 1;
  224. info->var.blue.offset = 0;
  225. info->screen_base = (u8 __force __iomem *)vmem;
  226. info->fix.smem_start = (unsigned long)vmem;
  227. info->fix.smem_len = vmem_size;
  228. fb_deferred_io_init(info);
  229. par = info->par;
  230. par->info = info;
  231. par->client = client;
  232. par->reset = of_get_named_gpio(client->dev.of_node,
  233. "reset-gpios", 0);
  234. if (!gpio_is_valid(par->reset)) {
  235. ret = -EINVAL;
  236. goto reset_oled_error;
  237. }
  238. ret = devm_gpio_request_one(&client->dev, par->reset,
  239. GPIOF_OUT_INIT_HIGH,
  240. "oled-reset");
  241. if (ret) {
  242. dev_err(&client->dev,
  243. "failed to request gpio %d: %d\n",
  244. par->reset, ret);
  245. goto reset_oled_error;
  246. }
  247. par->pwm = pwm_get(&client->dev, NULL);
  248. if (IS_ERR(par->pwm)) {
  249. dev_err(&client->dev, "Could not get PWM from device tree!\n");
  250. ret = PTR_ERR(par->pwm);
  251. goto pwm_error;
  252. }
  253. par->pwm_period = pwm_get_period(par->pwm);
  254. dev_dbg(&client->dev, "Using PWM%d with a %dns period.\n", par->pwm->pwm, par->pwm_period);
  255. ret = register_framebuffer(info);
  256. if (ret) {
  257. dev_err(&client->dev, "Couldn't register the framebuffer\n");
  258. goto fbreg_error;
  259. }
  260. i2c_set_clientdata(client, info);
  261. /* Reset the screen */
  262. gpio_set_value(par->reset, 0);
  263. udelay(4);
  264. gpio_set_value(par->reset, 1);
  265. udelay(4);
  266. /* Enable the PWM */
  267. pwm_config(par->pwm, par->pwm_period / 2, par->pwm_period);
  268. pwm_enable(par->pwm);
  269. /* Map column 127 of the OLED to segment 0 */
  270. ret = ssd1307fb_write_cmd(client, SSD1307FB_SEG_REMAP_ON);
  271. if (ret < 0) {
  272. dev_err(&client->dev, "Couldn't remap the screen.\n");
  273. goto remap_error;
  274. }
  275. /* Turn on the display */
  276. ret = ssd1307fb_write_cmd(client, SSD1307FB_DISPLAY_ON);
  277. if (ret < 0) {
  278. dev_err(&client->dev, "Couldn't turn the display on.\n");
  279. goto remap_error;
  280. }
  281. dev_info(&client->dev, "fb%d: %s framebuffer device registered, using %d bytes of video memory\n", info->node, info->fix.id, vmem_size);
  282. return 0;
  283. remap_error:
  284. unregister_framebuffer(info);
  285. pwm_disable(par->pwm);
  286. fbreg_error:
  287. pwm_put(par->pwm);
  288. pwm_error:
  289. reset_oled_error:
  290. fb_deferred_io_cleanup(info);
  291. fb_alloc_error:
  292. framebuffer_release(info);
  293. return ret;
  294. }
  295. static int ssd1307fb_remove(struct i2c_client *client)
  296. {
  297. struct fb_info *info = i2c_get_clientdata(client);
  298. struct ssd1307fb_par *par = info->par;
  299. unregister_framebuffer(info);
  300. pwm_disable(par->pwm);
  301. pwm_put(par->pwm);
  302. fb_deferred_io_cleanup(info);
  303. framebuffer_release(info);
  304. return 0;
  305. }
  306. static const struct i2c_device_id ssd1307fb_i2c_id[] = {
  307. { "ssd1307fb", 0 },
  308. { }
  309. };
  310. MODULE_DEVICE_TABLE(i2c, ssd1307fb_i2c_id);
  311. static const struct of_device_id ssd1307fb_of_match[] = {
  312. { .compatible = "solomon,ssd1307fb-i2c" },
  313. {},
  314. };
  315. MODULE_DEVICE_TABLE(of, ssd1307fb_of_match);
  316. static struct i2c_driver ssd1307fb_driver = {
  317. .probe = ssd1307fb_probe,
  318. .remove = ssd1307fb_remove,
  319. .id_table = ssd1307fb_i2c_id,
  320. .driver = {
  321. .name = "ssd1307fb",
  322. .of_match_table = of_match_ptr(ssd1307fb_of_match),
  323. .owner = THIS_MODULE,
  324. },
  325. };
  326. module_i2c_driver(ssd1307fb_driver);
  327. MODULE_DESCRIPTION("FB driver for the Solomon SSD1307 OLED controler");
  328. MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");
  329. MODULE_LICENSE("GPL");