am200epd.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. /*
  2. * am200epd.c -- Platform device for AM200 EPD kit
  3. *
  4. * Copyright (C) 2008, Jaya Kumar
  5. *
  6. * This file is subject to the terms and conditions of the GNU General Public
  7. * License. See the file COPYING in the main directory of this archive for
  8. * more details.
  9. *
  10. * Layout is based on skeletonfb.c by James Simmons and Geert Uytterhoeven.
  11. *
  12. * This work was made possible by help and equipment support from E-Ink
  13. * Corporation. http://support.eink.com/community
  14. *
  15. * This driver is written to be used with the Metronome display controller.
  16. * on the AM200 EPD prototype kit/development kit with an E-Ink 800x600
  17. * Vizplex EPD on a Gumstix board using the Lyre interface board.
  18. *
  19. */
  20. #include <linux/module.h>
  21. #include <linux/kernel.h>
  22. #include <linux/errno.h>
  23. #include <linux/string.h>
  24. #include <linux/delay.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/fb.h>
  27. #include <linux/init.h>
  28. #include <linux/platform_device.h>
  29. #include <linux/irq.h>
  30. #include <linux/gpio.h>
  31. #include <mach/pxafb.h>
  32. #include <video/metronomefb.h>
  33. static unsigned int panel_type = 6;
  34. static struct platform_device *am200_device;
  35. static struct metronome_board am200_board;
  36. static struct pxafb_mode_info am200_fb_mode_9inch7 = {
  37. .pixclock = 40000,
  38. .xres = 1200,
  39. .yres = 842,
  40. .bpp = 16,
  41. .hsync_len = 2,
  42. .left_margin = 2,
  43. .right_margin = 2,
  44. .vsync_len = 1,
  45. .upper_margin = 2,
  46. .lower_margin = 25,
  47. .sync = FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
  48. };
  49. static struct pxafb_mode_info am200_fb_mode_8inch = {
  50. .pixclock = 40000,
  51. .xres = 1088,
  52. .yres = 791,
  53. .bpp = 16,
  54. .hsync_len = 28,
  55. .left_margin = 8,
  56. .right_margin = 30,
  57. .vsync_len = 8,
  58. .upper_margin = 10,
  59. .lower_margin = 8,
  60. .sync = FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
  61. };
  62. static struct pxafb_mode_info am200_fb_mode_6inch = {
  63. .pixclock = 40189,
  64. .xres = 832,
  65. .yres = 622,
  66. .bpp = 16,
  67. .hsync_len = 28,
  68. .left_margin = 34,
  69. .right_margin = 34,
  70. .vsync_len = 25,
  71. .upper_margin = 0,
  72. .lower_margin = 2,
  73. .sync = FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
  74. };
  75. static struct pxafb_mach_info am200_fb_info = {
  76. .modes = &am200_fb_mode_6inch,
  77. .num_modes = 1,
  78. .lcd_conn = LCD_TYPE_COLOR_TFT | LCD_PCLK_EDGE_FALL |
  79. LCD_AC_BIAS_FREQ(24),
  80. };
  81. /* register offsets for gpio control */
  82. #define LED_GPIO_PIN 51
  83. #define STDBY_GPIO_PIN 48
  84. #define RST_GPIO_PIN 49
  85. #define RDY_GPIO_PIN 32
  86. #define ERR_GPIO_PIN 17
  87. #define PCBPWR_GPIO_PIN 16
  88. static int gpios[] = { LED_GPIO_PIN , STDBY_GPIO_PIN , RST_GPIO_PIN,
  89. RDY_GPIO_PIN, ERR_GPIO_PIN, PCBPWR_GPIO_PIN };
  90. static char *gpio_names[] = { "LED" , "STDBY" , "RST", "RDY", "ERR", "PCBPWR" };
  91. static int am200_init_gpio_regs(struct metronomefb_par *par)
  92. {
  93. int i;
  94. int err;
  95. for (i = 0; i < ARRAY_SIZE(gpios); i++) {
  96. err = gpio_request(gpios[i], gpio_names[i]);
  97. if (err) {
  98. dev_err(&am200_device->dev, "failed requesting "
  99. "gpio %s, err=%d\n", gpio_names[i], err);
  100. goto err_req_gpio;
  101. }
  102. }
  103. gpio_direction_output(LED_GPIO_PIN, 0);
  104. gpio_direction_output(STDBY_GPIO_PIN, 0);
  105. gpio_direction_output(RST_GPIO_PIN, 0);
  106. gpio_direction_input(RDY_GPIO_PIN);
  107. gpio_direction_input(ERR_GPIO_PIN);
  108. gpio_direction_output(PCBPWR_GPIO_PIN, 0);
  109. return 0;
  110. err_req_gpio:
  111. while (i > 0)
  112. gpio_free(gpios[i--]);
  113. return err;
  114. }
  115. static void am200_cleanup(struct metronomefb_par *par)
  116. {
  117. int i;
  118. free_irq(IRQ_GPIO(RDY_GPIO_PIN), par);
  119. for (i = 0; i < ARRAY_SIZE(gpios); i++)
  120. gpio_free(gpios[i]);
  121. }
  122. static int am200_share_video_mem(struct fb_info *info)
  123. {
  124. /* rough check if this is our desired fb and not something else */
  125. if ((info->var.xres != am200_fb_info.modes->xres)
  126. || (info->var.yres != am200_fb_info.modes->yres))
  127. return 0;
  128. /* we've now been notified that we have our new fb */
  129. am200_board.metromem = info->screen_base;
  130. am200_board.host_fbinfo = info;
  131. /* try to refcount host drv since we are the consumer after this */
  132. if (!try_module_get(info->fbops->owner))
  133. return -ENODEV;
  134. return 0;
  135. }
  136. static int am200_unshare_video_mem(struct fb_info *info)
  137. {
  138. dev_dbg(&am200_device->dev, "ENTER %s\n", __func__);
  139. if (info != am200_board.host_fbinfo)
  140. return 0;
  141. module_put(am200_board.host_fbinfo->fbops->owner);
  142. return 0;
  143. }
  144. static int am200_fb_notifier_callback(struct notifier_block *self,
  145. unsigned long event, void *data)
  146. {
  147. struct fb_event *evdata = data;
  148. struct fb_info *info = evdata->info;
  149. dev_dbg(&am200_device->dev, "ENTER %s\n", __func__);
  150. if (event == FB_EVENT_FB_REGISTERED)
  151. return am200_share_video_mem(info);
  152. else if (event == FB_EVENT_FB_UNREGISTERED)
  153. return am200_unshare_video_mem(info);
  154. return 0;
  155. }
  156. static struct notifier_block am200_fb_notif = {
  157. .notifier_call = am200_fb_notifier_callback,
  158. };
  159. /* this gets called as part of our init. these steps must be done now so
  160. * that we can use set_pxa_fb_info */
  161. static void __init am200_presetup_fb(void)
  162. {
  163. int fw;
  164. int fh;
  165. int padding_size;
  166. int totalsize;
  167. switch (panel_type) {
  168. case 6:
  169. am200_fb_info.modes = &am200_fb_mode_6inch;
  170. break;
  171. case 8:
  172. am200_fb_info.modes = &am200_fb_mode_8inch;
  173. break;
  174. case 97:
  175. am200_fb_info.modes = &am200_fb_mode_9inch7;
  176. break;
  177. default:
  178. dev_err(&am200_device->dev, "invalid panel_type selection,"
  179. " setting to 6\n");
  180. am200_fb_info.modes = &am200_fb_mode_6inch;
  181. break;
  182. }
  183. /* the frame buffer is divided as follows:
  184. command | CRC | padding
  185. 16kb waveform data | CRC | padding
  186. image data | CRC
  187. */
  188. fw = am200_fb_info.modes->xres;
  189. fh = am200_fb_info.modes->yres;
  190. /* waveform must be 16k + 2 for checksum */
  191. am200_board.wfm_size = roundup(16*1024 + 2, fw);
  192. padding_size = PAGE_SIZE + (4 * fw);
  193. /* total is 1 cmd , 1 wfm, padding and image */
  194. totalsize = fw + am200_board.wfm_size + padding_size + (fw*fh);
  195. /* save this off because we're manipulating fw after this and
  196. * we'll need it when we're ready to setup the framebuffer */
  197. am200_board.fw = fw;
  198. am200_board.fh = fh;
  199. /* the reason we do this adjustment is because we want to acquire
  200. * more framebuffer memory without imposing custom awareness on the
  201. * underlying pxafb driver */
  202. am200_fb_info.modes->yres = DIV_ROUND_UP(totalsize, fw);
  203. /* we divide since we told the LCD controller we're 16bpp */
  204. am200_fb_info.modes->xres /= 2;
  205. set_pxa_fb_info(&am200_fb_info);
  206. }
  207. /* this gets called by metronomefb as part of its init, in our case, we
  208. * have already completed initial framebuffer init in presetup_fb so we
  209. * can just setup the fb access pointers */
  210. static int am200_setup_fb(struct metronomefb_par *par)
  211. {
  212. int fw;
  213. int fh;
  214. fw = am200_board.fw;
  215. fh = am200_board.fh;
  216. /* metromem was set up by the notifier in share_video_mem so now
  217. * we can use its value to calculate the other entries */
  218. par->metromem_cmd = (struct metromem_cmd *) am200_board.metromem;
  219. par->metromem_wfm = am200_board.metromem + fw;
  220. par->metromem_img = par->metromem_wfm + am200_board.wfm_size;
  221. par->metromem_img_csum = (u16 *) (par->metromem_img + (fw * fh));
  222. par->metromem_dma = am200_board.host_fbinfo->fix.smem_start;
  223. return 0;
  224. }
  225. static int am200_get_panel_type(void)
  226. {
  227. return panel_type;
  228. }
  229. static irqreturn_t am200_handle_irq(int irq, void *dev_id)
  230. {
  231. struct metronomefb_par *par = dev_id;
  232. wake_up_interruptible(&par->waitq);
  233. return IRQ_HANDLED;
  234. }
  235. static int am200_setup_irq(struct fb_info *info)
  236. {
  237. int ret;
  238. ret = request_irq(IRQ_GPIO(RDY_GPIO_PIN), am200_handle_irq,
  239. IRQF_DISABLED|IRQF_TRIGGER_FALLING,
  240. "AM200", info->par);
  241. if (ret)
  242. dev_err(&am200_device->dev, "request_irq failed: %d\n", ret);
  243. return ret;
  244. }
  245. static void am200_set_rst(struct metronomefb_par *par, int state)
  246. {
  247. gpio_set_value(RST_GPIO_PIN, state);
  248. }
  249. static void am200_set_stdby(struct metronomefb_par *par, int state)
  250. {
  251. gpio_set_value(STDBY_GPIO_PIN, state);
  252. }
  253. static int am200_wait_event(struct metronomefb_par *par)
  254. {
  255. return wait_event_timeout(par->waitq, gpio_get_value(RDY_GPIO_PIN), HZ);
  256. }
  257. static int am200_wait_event_intr(struct metronomefb_par *par)
  258. {
  259. return wait_event_interruptible_timeout(par->waitq,
  260. gpio_get_value(RDY_GPIO_PIN), HZ);
  261. }
  262. static struct metronome_board am200_board = {
  263. .owner = THIS_MODULE,
  264. .setup_irq = am200_setup_irq,
  265. .setup_io = am200_init_gpio_regs,
  266. .setup_fb = am200_setup_fb,
  267. .set_rst = am200_set_rst,
  268. .set_stdby = am200_set_stdby,
  269. .met_wait_event = am200_wait_event,
  270. .met_wait_event_intr = am200_wait_event_intr,
  271. .get_panel_type = am200_get_panel_type,
  272. .cleanup = am200_cleanup,
  273. };
  274. static int __init am200_init(void)
  275. {
  276. int ret;
  277. /* before anything else, we request notification for any fb
  278. * creation events */
  279. fb_register_client(&am200_fb_notif);
  280. /* request our platform independent driver */
  281. request_module("metronomefb");
  282. am200_device = platform_device_alloc("metronomefb", -1);
  283. if (!am200_device)
  284. return -ENOMEM;
  285. /* the am200_board that will be seen by metronomefb is a copy */
  286. platform_device_add_data(am200_device, &am200_board,
  287. sizeof(am200_board));
  288. /* this _add binds metronomefb to am200. metronomefb refcounts am200 */
  289. ret = platform_device_add(am200_device);
  290. if (ret) {
  291. platform_device_put(am200_device);
  292. fb_unregister_client(&am200_fb_notif);
  293. return ret;
  294. }
  295. am200_presetup_fb();
  296. return 0;
  297. }
  298. module_param(panel_type, uint, 0);
  299. MODULE_PARM_DESC(panel_type, "Select the panel type: 6, 8, 97");
  300. module_init(am200_init);
  301. MODULE_DESCRIPTION("board driver for am200 metronome epd kit");
  302. MODULE_AUTHOR("Jaya Kumar");
  303. MODULE_LICENSE("GPL");