auo_k190x.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044
  1. /*
  2. * Common code for AUO-K190X framebuffer drivers
  3. *
  4. * Copyright (C) 2012 Heiko Stuebner <heiko@sntech.de>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <linux/module.h>
  11. #include <linux/kernel.h>
  12. #include <linux/gpio.h>
  13. #include <linux/pm_runtime.h>
  14. #include <linux/fb.h>
  15. #include <linux/delay.h>
  16. #include <linux/uaccess.h>
  17. #include <linux/vmalloc.h>
  18. #include <linux/regulator/consumer.h>
  19. #include <video/auo_k190xfb.h>
  20. #include "auo_k190x.h"
  21. struct panel_info {
  22. int w;
  23. int h;
  24. };
  25. /* table of panel specific parameters to be indexed into by the board drivers */
  26. static struct panel_info panel_table[] = {
  27. /* standard 6" */
  28. [AUOK190X_RESOLUTION_800_600] = {
  29. .w = 800,
  30. .h = 600,
  31. },
  32. /* standard 9" */
  33. [AUOK190X_RESOLUTION_1024_768] = {
  34. .w = 1024,
  35. .h = 768,
  36. },
  37. };
  38. /*
  39. * private I80 interface to the board driver
  40. */
  41. static void auok190x_issue_data(struct auok190xfb_par *par, u16 data)
  42. {
  43. par->board->set_ctl(par, AUOK190X_I80_WR, 0);
  44. par->board->set_hdb(par, data);
  45. par->board->set_ctl(par, AUOK190X_I80_WR, 1);
  46. }
  47. static void auok190x_issue_cmd(struct auok190xfb_par *par, u16 data)
  48. {
  49. par->board->set_ctl(par, AUOK190X_I80_DC, 0);
  50. auok190x_issue_data(par, data);
  51. par->board->set_ctl(par, AUOK190X_I80_DC, 1);
  52. }
  53. static int auok190x_issue_pixels(struct auok190xfb_par *par, int size,
  54. u16 *data)
  55. {
  56. struct device *dev = par->info->device;
  57. int i;
  58. u16 tmp;
  59. if (size & 3) {
  60. dev_err(dev, "issue_pixels: size %d must be a multiple of 4\n",
  61. size);
  62. return -EINVAL;
  63. }
  64. for (i = 0; i < (size >> 1); i++) {
  65. par->board->set_ctl(par, AUOK190X_I80_WR, 0);
  66. /* simple reduction of 8bit staticgray to 4bit gray
  67. * combines 4 * 4bit pixel values into a 16bit value
  68. */
  69. tmp = (data[2*i] & 0xF0) >> 4;
  70. tmp |= (data[2*i] & 0xF000) >> 8;
  71. tmp |= (data[2*i+1] & 0xF0) << 4;
  72. tmp |= (data[2*i+1] & 0xF000);
  73. par->board->set_hdb(par, tmp);
  74. par->board->set_ctl(par, AUOK190X_I80_WR, 1);
  75. }
  76. return 0;
  77. }
  78. static u16 auok190x_read_data(struct auok190xfb_par *par)
  79. {
  80. u16 data;
  81. par->board->set_ctl(par, AUOK190X_I80_OE, 0);
  82. data = par->board->get_hdb(par);
  83. par->board->set_ctl(par, AUOK190X_I80_OE, 1);
  84. return data;
  85. }
  86. /*
  87. * Command interface for the controller drivers
  88. */
  89. void auok190x_send_command_nowait(struct auok190xfb_par *par, u16 data)
  90. {
  91. par->board->set_ctl(par, AUOK190X_I80_CS, 0);
  92. auok190x_issue_cmd(par, data);
  93. par->board->set_ctl(par, AUOK190X_I80_CS, 1);
  94. }
  95. EXPORT_SYMBOL_GPL(auok190x_send_command_nowait);
  96. void auok190x_send_cmdargs_nowait(struct auok190xfb_par *par, u16 cmd,
  97. int argc, u16 *argv)
  98. {
  99. int i;
  100. par->board->set_ctl(par, AUOK190X_I80_CS, 0);
  101. auok190x_issue_cmd(par, cmd);
  102. for (i = 0; i < argc; i++)
  103. auok190x_issue_data(par, argv[i]);
  104. par->board->set_ctl(par, AUOK190X_I80_CS, 1);
  105. }
  106. EXPORT_SYMBOL_GPL(auok190x_send_cmdargs_nowait);
  107. int auok190x_send_command(struct auok190xfb_par *par, u16 data)
  108. {
  109. int ret;
  110. ret = par->board->wait_for_rdy(par);
  111. if (ret)
  112. return ret;
  113. auok190x_send_command_nowait(par, data);
  114. return 0;
  115. }
  116. EXPORT_SYMBOL_GPL(auok190x_send_command);
  117. int auok190x_send_cmdargs(struct auok190xfb_par *par, u16 cmd,
  118. int argc, u16 *argv)
  119. {
  120. int ret;
  121. ret = par->board->wait_for_rdy(par);
  122. if (ret)
  123. return ret;
  124. auok190x_send_cmdargs_nowait(par, cmd, argc, argv);
  125. return 0;
  126. }
  127. EXPORT_SYMBOL_GPL(auok190x_send_cmdargs);
  128. int auok190x_read_cmdargs(struct auok190xfb_par *par, u16 cmd,
  129. int argc, u16 *argv)
  130. {
  131. int i, ret;
  132. ret = par->board->wait_for_rdy(par);
  133. if (ret)
  134. return ret;
  135. par->board->set_ctl(par, AUOK190X_I80_CS, 0);
  136. auok190x_issue_cmd(par, cmd);
  137. for (i = 0; i < argc; i++)
  138. argv[i] = auok190x_read_data(par);
  139. par->board->set_ctl(par, AUOK190X_I80_CS, 1);
  140. return 0;
  141. }
  142. EXPORT_SYMBOL_GPL(auok190x_read_cmdargs);
  143. void auok190x_send_cmdargs_pixels_nowait(struct auok190xfb_par *par, u16 cmd,
  144. int argc, u16 *argv, int size, u16 *data)
  145. {
  146. int i;
  147. par->board->set_ctl(par, AUOK190X_I80_CS, 0);
  148. auok190x_issue_cmd(par, cmd);
  149. for (i = 0; i < argc; i++)
  150. auok190x_issue_data(par, argv[i]);
  151. auok190x_issue_pixels(par, size, data);
  152. par->board->set_ctl(par, AUOK190X_I80_CS, 1);
  153. }
  154. EXPORT_SYMBOL_GPL(auok190x_send_cmdargs_pixels_nowait);
  155. int auok190x_send_cmdargs_pixels(struct auok190xfb_par *par, u16 cmd,
  156. int argc, u16 *argv, int size, u16 *data)
  157. {
  158. int ret;
  159. ret = par->board->wait_for_rdy(par);
  160. if (ret)
  161. return ret;
  162. auok190x_send_cmdargs_pixels_nowait(par, cmd, argc, argv, size, data);
  163. return 0;
  164. }
  165. EXPORT_SYMBOL_GPL(auok190x_send_cmdargs_pixels);
  166. /*
  167. * fbdefio callbacks - common on both controllers.
  168. */
  169. static void auok190xfb_dpy_first_io(struct fb_info *info)
  170. {
  171. /* tell runtime-pm that we wish to use the device in a short time */
  172. pm_runtime_get(info->device);
  173. }
  174. /* this is called back from the deferred io workqueue */
  175. static void auok190xfb_dpy_deferred_io(struct fb_info *info,
  176. struct list_head *pagelist)
  177. {
  178. struct fb_deferred_io *fbdefio = info->fbdefio;
  179. struct auok190xfb_par *par = info->par;
  180. u16 yres = info->var.yres;
  181. u16 xres = info->var.xres;
  182. u16 y1 = 0, h = 0;
  183. int prev_index = -1;
  184. struct page *cur;
  185. int h_inc;
  186. int threshold;
  187. if (!list_empty(pagelist))
  188. /* the device resume should've been requested through first_io,
  189. * if the resume did not finish until now, wait for it.
  190. */
  191. pm_runtime_barrier(info->device);
  192. else
  193. /* We reached this via the fsync or some other way.
  194. * In either case the first_io function did not run,
  195. * so we runtime_resume the device here synchronously.
  196. */
  197. pm_runtime_get_sync(info->device);
  198. /* Do a full screen update every n updates to prevent
  199. * excessive darkening of the Sipix display.
  200. * If we do this, there is no need to walk the pages.
  201. */
  202. if (par->need_refresh(par)) {
  203. par->update_all(par);
  204. goto out;
  205. }
  206. /* height increment is fixed per page */
  207. h_inc = DIV_ROUND_UP(PAGE_SIZE , xres);
  208. /* calculate number of pages from pixel height */
  209. threshold = par->consecutive_threshold / h_inc;
  210. if (threshold < 1)
  211. threshold = 1;
  212. /* walk the written page list and swizzle the data */
  213. list_for_each_entry(cur, &fbdefio->pagelist, lru) {
  214. if (prev_index < 0) {
  215. /* just starting so assign first page */
  216. y1 = (cur->index << PAGE_SHIFT) / xres;
  217. h = h_inc;
  218. } else if ((cur->index - prev_index) <= threshold) {
  219. /* page is within our threshold for single updates */
  220. h += h_inc * (cur->index - prev_index);
  221. } else {
  222. /* page not consecutive, issue previous update first */
  223. par->update_partial(par, y1, y1 + h);
  224. /* start over with our non consecutive page */
  225. y1 = (cur->index << PAGE_SHIFT) / xres;
  226. h = h_inc;
  227. }
  228. prev_index = cur->index;
  229. }
  230. /* if we still have any pages to update we do so now */
  231. if (h >= yres)
  232. /* its a full screen update, just do it */
  233. par->update_all(par);
  234. else
  235. par->update_partial(par, y1, min((u16) (y1 + h), yres));
  236. out:
  237. pm_runtime_mark_last_busy(info->device);
  238. pm_runtime_put_autosuspend(info->device);
  239. }
  240. /*
  241. * framebuffer operations
  242. */
  243. /*
  244. * this is the slow path from userspace. they can seek and write to
  245. * the fb. it's inefficient to do anything less than a full screen draw
  246. */
  247. static ssize_t auok190xfb_write(struct fb_info *info, const char __user *buf,
  248. size_t count, loff_t *ppos)
  249. {
  250. struct auok190xfb_par *par = info->par;
  251. unsigned long p = *ppos;
  252. void *dst;
  253. int err = 0;
  254. unsigned long total_size;
  255. if (info->state != FBINFO_STATE_RUNNING)
  256. return -EPERM;
  257. total_size = info->fix.smem_len;
  258. if (p > total_size)
  259. return -EFBIG;
  260. if (count > total_size) {
  261. err = -EFBIG;
  262. count = total_size;
  263. }
  264. if (count + p > total_size) {
  265. if (!err)
  266. err = -ENOSPC;
  267. count = total_size - p;
  268. }
  269. dst = (void *)(info->screen_base + p);
  270. if (copy_from_user(dst, buf, count))
  271. err = -EFAULT;
  272. if (!err)
  273. *ppos += count;
  274. par->update_all(par);
  275. return (err) ? err : count;
  276. }
  277. static void auok190xfb_fillrect(struct fb_info *info,
  278. const struct fb_fillrect *rect)
  279. {
  280. struct auok190xfb_par *par = info->par;
  281. sys_fillrect(info, rect);
  282. par->update_all(par);
  283. }
  284. static void auok190xfb_copyarea(struct fb_info *info,
  285. const struct fb_copyarea *area)
  286. {
  287. struct auok190xfb_par *par = info->par;
  288. sys_copyarea(info, area);
  289. par->update_all(par);
  290. }
  291. static void auok190xfb_imageblit(struct fb_info *info,
  292. const struct fb_image *image)
  293. {
  294. struct auok190xfb_par *par = info->par;
  295. sys_imageblit(info, image);
  296. par->update_all(par);
  297. }
  298. static int auok190xfb_check_var(struct fb_var_screeninfo *var,
  299. struct fb_info *info)
  300. {
  301. if (info->var.xres != var->xres || info->var.yres != var->yres ||
  302. info->var.xres_virtual != var->xres_virtual ||
  303. info->var.yres_virtual != var->yres_virtual) {
  304. pr_info("%s: Resolution not supported: X%u x Y%u\n",
  305. __func__, var->xres, var->yres);
  306. return -EINVAL;
  307. }
  308. /*
  309. * Memory limit
  310. */
  311. if ((info->fix.line_length * var->yres_virtual) > info->fix.smem_len) {
  312. pr_info("%s: Memory Limit requested yres_virtual = %u\n",
  313. __func__, var->yres_virtual);
  314. return -ENOMEM;
  315. }
  316. return 0;
  317. }
  318. static struct fb_ops auok190xfb_ops = {
  319. .owner = THIS_MODULE,
  320. .fb_read = fb_sys_read,
  321. .fb_write = auok190xfb_write,
  322. .fb_fillrect = auok190xfb_fillrect,
  323. .fb_copyarea = auok190xfb_copyarea,
  324. .fb_imageblit = auok190xfb_imageblit,
  325. .fb_check_var = auok190xfb_check_var,
  326. };
  327. /*
  328. * Controller-functions common to both K1900 and K1901
  329. */
  330. static int auok190x_read_temperature(struct auok190xfb_par *par)
  331. {
  332. struct device *dev = par->info->device;
  333. u16 data[4];
  334. int temp;
  335. pm_runtime_get_sync(dev);
  336. mutex_lock(&(par->io_lock));
  337. auok190x_read_cmdargs(par, AUOK190X_CMD_READ_VERSION, 4, data);
  338. mutex_unlock(&(par->io_lock));
  339. pm_runtime_mark_last_busy(dev);
  340. pm_runtime_put_autosuspend(dev);
  341. /* sanitize and split of half-degrees for now */
  342. temp = ((data[0] & AUOK190X_VERSION_TEMP_MASK) >> 1);
  343. /* handle positive and negative temperatures */
  344. if (temp >= 201)
  345. return (255 - temp + 1) * (-1);
  346. else
  347. return temp;
  348. }
  349. static void auok190x_identify(struct auok190xfb_par *par)
  350. {
  351. struct device *dev = par->info->device;
  352. u16 data[4];
  353. pm_runtime_get_sync(dev);
  354. mutex_lock(&(par->io_lock));
  355. auok190x_read_cmdargs(par, AUOK190X_CMD_READ_VERSION, 4, data);
  356. mutex_unlock(&(par->io_lock));
  357. par->epd_type = data[1] & AUOK190X_VERSION_TEMP_MASK;
  358. par->panel_size_int = AUOK190X_VERSION_SIZE_INT(data[2]);
  359. par->panel_size_float = AUOK190X_VERSION_SIZE_FLOAT(data[2]);
  360. par->panel_model = AUOK190X_VERSION_MODEL(data[2]);
  361. par->tcon_version = AUOK190X_VERSION_TCON(data[3]);
  362. par->lut_version = AUOK190X_VERSION_LUT(data[3]);
  363. dev_dbg(dev, "panel %d.%din, model 0x%x, EPD 0x%x TCON-rev 0x%x, LUT-rev 0x%x",
  364. par->panel_size_int, par->panel_size_float, par->panel_model,
  365. par->epd_type, par->tcon_version, par->lut_version);
  366. pm_runtime_mark_last_busy(dev);
  367. pm_runtime_put_autosuspend(dev);
  368. }
  369. /*
  370. * Sysfs functions
  371. */
  372. static ssize_t update_mode_show(struct device *dev,
  373. struct device_attribute *attr, char *buf)
  374. {
  375. struct fb_info *info = dev_get_drvdata(dev);
  376. struct auok190xfb_par *par = info->par;
  377. return sprintf(buf, "%d\n", par->update_mode);
  378. }
  379. static ssize_t update_mode_store(struct device *dev,
  380. struct device_attribute *attr,
  381. const char *buf, size_t count)
  382. {
  383. struct fb_info *info = dev_get_drvdata(dev);
  384. struct auok190xfb_par *par = info->par;
  385. int mode, ret;
  386. ret = kstrtoint(buf, 10, &mode);
  387. if (ret)
  388. return ret;
  389. par->update_mode = mode;
  390. /* if we enter a better mode, do a full update */
  391. if (par->last_mode > 1 && mode < par->last_mode)
  392. par->update_all(par);
  393. return count;
  394. }
  395. static ssize_t flash_show(struct device *dev, struct device_attribute *attr,
  396. char *buf)
  397. {
  398. struct fb_info *info = dev_get_drvdata(dev);
  399. struct auok190xfb_par *par = info->par;
  400. return sprintf(buf, "%d\n", par->flash);
  401. }
  402. static ssize_t flash_store(struct device *dev, struct device_attribute *attr,
  403. const char *buf, size_t count)
  404. {
  405. struct fb_info *info = dev_get_drvdata(dev);
  406. struct auok190xfb_par *par = info->par;
  407. int flash, ret;
  408. ret = kstrtoint(buf, 10, &flash);
  409. if (ret)
  410. return ret;
  411. if (flash > 0)
  412. par->flash = 1;
  413. else
  414. par->flash = 0;
  415. return count;
  416. }
  417. static ssize_t temp_show(struct device *dev, struct device_attribute *attr,
  418. char *buf)
  419. {
  420. struct fb_info *info = dev_get_drvdata(dev);
  421. struct auok190xfb_par *par = info->par;
  422. int temp;
  423. temp = auok190x_read_temperature(par);
  424. return sprintf(buf, "%d\n", temp);
  425. }
  426. static DEVICE_ATTR(update_mode, 0644, update_mode_show, update_mode_store);
  427. static DEVICE_ATTR(flash, 0644, flash_show, flash_store);
  428. static DEVICE_ATTR(temp, 0644, temp_show, NULL);
  429. static struct attribute *auok190x_attributes[] = {
  430. &dev_attr_update_mode.attr,
  431. &dev_attr_flash.attr,
  432. &dev_attr_temp.attr,
  433. NULL
  434. };
  435. static const struct attribute_group auok190x_attr_group = {
  436. .attrs = auok190x_attributes,
  437. };
  438. static int auok190x_power(struct auok190xfb_par *par, bool on)
  439. {
  440. struct auok190x_board *board = par->board;
  441. int ret;
  442. if (on) {
  443. /* We should maintain POWER up for at least 80ms before set
  444. * RST_N and SLP_N to high (TCON spec 20100803_v35 p59)
  445. */
  446. ret = regulator_enable(par->regulator);
  447. if (ret)
  448. return ret;
  449. msleep(200);
  450. gpio_set_value(board->gpio_nrst, 1);
  451. gpio_set_value(board->gpio_nsleep, 1);
  452. msleep(200);
  453. } else {
  454. regulator_disable(par->regulator);
  455. gpio_set_value(board->gpio_nrst, 0);
  456. gpio_set_value(board->gpio_nsleep, 0);
  457. }
  458. return 0;
  459. }
  460. /*
  461. * Recovery - powercycle the controller
  462. */
  463. static void auok190x_recover(struct auok190xfb_par *par)
  464. {
  465. auok190x_power(par, 0);
  466. msleep(100);
  467. auok190x_power(par, 1);
  468. par->init(par);
  469. /* wait for init to complete */
  470. par->board->wait_for_rdy(par);
  471. }
  472. /*
  473. * Power-management
  474. */
  475. #ifdef CONFIG_PM
  476. static int auok190x_runtime_suspend(struct device *dev)
  477. {
  478. struct platform_device *pdev = to_platform_device(dev);
  479. struct fb_info *info = platform_get_drvdata(pdev);
  480. struct auok190xfb_par *par = info->par;
  481. struct auok190x_board *board = par->board;
  482. u16 standby_param;
  483. /* take and keep the lock until we are resumed, as the controller
  484. * will never reach the non-busy state when in standby mode
  485. */
  486. mutex_lock(&(par->io_lock));
  487. if (par->standby) {
  488. dev_warn(dev, "already in standby, runtime-pm pairing mismatch\n");
  489. mutex_unlock(&(par->io_lock));
  490. return 0;
  491. }
  492. /* according to runtime_pm.txt runtime_suspend only means, that the
  493. * device will not process data and will not communicate with the CPU
  494. * As we hold the lock, this stays true even without standby
  495. */
  496. if (board->quirks & AUOK190X_QUIRK_STANDBYBROKEN) {
  497. dev_dbg(dev, "runtime suspend without standby\n");
  498. goto finish;
  499. } else if (board->quirks & AUOK190X_QUIRK_STANDBYPARAM) {
  500. /* for some TCON versions STANDBY expects a parameter (0) but
  501. * it seems the real tcon version has to be determined yet.
  502. */
  503. dev_dbg(dev, "runtime suspend with additional empty param\n");
  504. standby_param = 0;
  505. auok190x_send_cmdargs(par, AUOK190X_CMD_STANDBY, 1,
  506. &standby_param);
  507. } else {
  508. dev_dbg(dev, "runtime suspend without param\n");
  509. auok190x_send_command(par, AUOK190X_CMD_STANDBY);
  510. }
  511. msleep(64);
  512. finish:
  513. par->standby = 1;
  514. return 0;
  515. }
  516. static int auok190x_runtime_resume(struct device *dev)
  517. {
  518. struct platform_device *pdev = to_platform_device(dev);
  519. struct fb_info *info = platform_get_drvdata(pdev);
  520. struct auok190xfb_par *par = info->par;
  521. struct auok190x_board *board = par->board;
  522. if (!par->standby) {
  523. dev_warn(dev, "not in standby, runtime-pm pairing mismatch\n");
  524. return 0;
  525. }
  526. if (board->quirks & AUOK190X_QUIRK_STANDBYBROKEN) {
  527. dev_dbg(dev, "runtime resume without standby\n");
  528. } else {
  529. /* when in standby, controller is always busy
  530. * and only accepts the wakeup command
  531. */
  532. dev_dbg(dev, "runtime resume from standby\n");
  533. auok190x_send_command_nowait(par, AUOK190X_CMD_WAKEUP);
  534. msleep(160);
  535. /* wait for the controller to be ready and release the lock */
  536. board->wait_for_rdy(par);
  537. }
  538. par->standby = 0;
  539. mutex_unlock(&(par->io_lock));
  540. return 0;
  541. }
  542. static int auok190x_suspend(struct device *dev)
  543. {
  544. struct platform_device *pdev = to_platform_device(dev);
  545. struct fb_info *info = platform_get_drvdata(pdev);
  546. struct auok190xfb_par *par = info->par;
  547. struct auok190x_board *board = par->board;
  548. int ret;
  549. dev_dbg(dev, "suspend\n");
  550. if (board->quirks & AUOK190X_QUIRK_STANDBYBROKEN) {
  551. /* suspend via powering off the ic */
  552. dev_dbg(dev, "suspend with broken standby\n");
  553. auok190x_power(par, 0);
  554. } else {
  555. dev_dbg(dev, "suspend using sleep\n");
  556. /* the sleep state can only be entered from the standby state.
  557. * pm_runtime_get_noresume gets called before the suspend call.
  558. * So the devices usage count is >0 but it is not necessarily
  559. * active.
  560. */
  561. if (!pm_runtime_status_suspended(dev)) {
  562. ret = auok190x_runtime_suspend(dev);
  563. if (ret < 0) {
  564. dev_err(dev, "auok190x_runtime_suspend failed with %d\n",
  565. ret);
  566. return ret;
  567. }
  568. par->manual_standby = 1;
  569. }
  570. gpio_direction_output(board->gpio_nsleep, 0);
  571. }
  572. msleep(100);
  573. return 0;
  574. }
  575. static int auok190x_resume(struct device *dev)
  576. {
  577. struct platform_device *pdev = to_platform_device(dev);
  578. struct fb_info *info = platform_get_drvdata(pdev);
  579. struct auok190xfb_par *par = info->par;
  580. struct auok190x_board *board = par->board;
  581. dev_dbg(dev, "resume\n");
  582. if (board->quirks & AUOK190X_QUIRK_STANDBYBROKEN) {
  583. dev_dbg(dev, "resume with broken standby\n");
  584. auok190x_power(par, 1);
  585. par->init(par);
  586. } else {
  587. dev_dbg(dev, "resume from sleep\n");
  588. /* device should be in runtime suspend when we were suspended
  589. * and pm_runtime_put_sync gets called after this function.
  590. * So there is no need to touch the standby mode here at all.
  591. */
  592. gpio_direction_output(board->gpio_nsleep, 1);
  593. msleep(100);
  594. /* an additional init call seems to be necessary after sleep */
  595. auok190x_runtime_resume(dev);
  596. par->init(par);
  597. /* if we were runtime-suspended before, suspend again*/
  598. if (!par->manual_standby)
  599. auok190x_runtime_suspend(dev);
  600. else
  601. par->manual_standby = 0;
  602. }
  603. return 0;
  604. }
  605. #endif
  606. const struct dev_pm_ops auok190x_pm = {
  607. SET_RUNTIME_PM_OPS(auok190x_runtime_suspend, auok190x_runtime_resume,
  608. NULL)
  609. SET_SYSTEM_SLEEP_PM_OPS(auok190x_suspend, auok190x_resume)
  610. };
  611. EXPORT_SYMBOL_GPL(auok190x_pm);
  612. /*
  613. * Common probe and remove code
  614. */
  615. int __devinit auok190x_common_probe(struct platform_device *pdev,
  616. struct auok190x_init_data *init)
  617. {
  618. struct auok190x_board *board = init->board;
  619. struct auok190xfb_par *par;
  620. struct fb_info *info;
  621. struct panel_info *panel;
  622. int videomemorysize, ret;
  623. unsigned char *videomemory;
  624. /* check board contents */
  625. if (!board->init || !board->cleanup || !board->wait_for_rdy
  626. || !board->set_ctl || !board->set_hdb || !board->get_hdb
  627. || !board->setup_irq)
  628. return -EINVAL;
  629. info = framebuffer_alloc(sizeof(struct auok190xfb_par), &pdev->dev);
  630. if (!info)
  631. return -ENOMEM;
  632. par = info->par;
  633. par->info = info;
  634. par->board = board;
  635. par->recover = auok190x_recover;
  636. par->update_partial = init->update_partial;
  637. par->update_all = init->update_all;
  638. par->need_refresh = init->need_refresh;
  639. par->init = init->init;
  640. /* init update modes */
  641. par->update_cnt = 0;
  642. par->update_mode = -1;
  643. par->last_mode = -1;
  644. par->flash = 0;
  645. par->regulator = regulator_get(info->device, "vdd");
  646. if (IS_ERR(par->regulator)) {
  647. ret = PTR_ERR(par->regulator);
  648. dev_err(info->device, "Failed to get regulator: %d\n", ret);
  649. goto err_reg;
  650. }
  651. ret = board->init(par);
  652. if (ret) {
  653. dev_err(info->device, "board init failed, %d\n", ret);
  654. goto err_board;
  655. }
  656. ret = gpio_request(board->gpio_nsleep, "AUOK190x sleep");
  657. if (ret) {
  658. dev_err(info->device, "could not request sleep gpio, %d\n",
  659. ret);
  660. goto err_gpio1;
  661. }
  662. ret = gpio_direction_output(board->gpio_nsleep, 0);
  663. if (ret) {
  664. dev_err(info->device, "could not set sleep gpio, %d\n", ret);
  665. goto err_gpio2;
  666. }
  667. ret = gpio_request(board->gpio_nrst, "AUOK190x reset");
  668. if (ret) {
  669. dev_err(info->device, "could not request reset gpio, %d\n",
  670. ret);
  671. goto err_gpio2;
  672. }
  673. ret = gpio_direction_output(board->gpio_nrst, 0);
  674. if (ret) {
  675. dev_err(info->device, "could not set reset gpio, %d\n", ret);
  676. goto err_gpio3;
  677. }
  678. ret = auok190x_power(par, 1);
  679. if (ret) {
  680. dev_err(info->device, "could not power on the device, %d\n",
  681. ret);
  682. goto err_gpio3;
  683. }
  684. mutex_init(&par->io_lock);
  685. init_waitqueue_head(&par->waitq);
  686. ret = par->board->setup_irq(par->info);
  687. if (ret) {
  688. dev_err(info->device, "could not setup ready-irq, %d\n", ret);
  689. goto err_irq;
  690. }
  691. /* wait for init to complete */
  692. par->board->wait_for_rdy(par);
  693. /*
  694. * From here on the controller can talk to us
  695. */
  696. /* initialise fix, var, resolution and rotation */
  697. strlcpy(info->fix.id, init->id, 16);
  698. info->fix.type = FB_TYPE_PACKED_PIXELS;
  699. info->fix.visual = FB_VISUAL_STATIC_PSEUDOCOLOR;
  700. info->fix.xpanstep = 0;
  701. info->fix.ypanstep = 0;
  702. info->fix.ywrapstep = 0;
  703. info->fix.accel = FB_ACCEL_NONE;
  704. info->var.bits_per_pixel = 8;
  705. info->var.grayscale = 1;
  706. info->var.red.length = 8;
  707. info->var.green.length = 8;
  708. info->var.blue.length = 8;
  709. panel = &panel_table[board->resolution];
  710. /* if 90 degree rotation, switch width and height */
  711. if (board->rotation & 1) {
  712. info->var.xres = panel->h;
  713. info->var.yres = panel->w;
  714. info->var.xres_virtual = panel->h;
  715. info->var.yres_virtual = panel->w;
  716. info->fix.line_length = panel->h;
  717. } else {
  718. info->var.xres = panel->w;
  719. info->var.yres = panel->h;
  720. info->var.xres_virtual = panel->w;
  721. info->var.yres_virtual = panel->h;
  722. info->fix.line_length = panel->w;
  723. }
  724. par->resolution = board->resolution;
  725. par->rotation = board->rotation;
  726. /* videomemory handling */
  727. videomemorysize = roundup((panel->w * panel->h), PAGE_SIZE);
  728. videomemory = vmalloc(videomemorysize);
  729. if (!videomemory) {
  730. ret = -ENOMEM;
  731. goto err_irq;
  732. }
  733. memset(videomemory, 0, videomemorysize);
  734. info->screen_base = (char *)videomemory;
  735. info->fix.smem_len = videomemorysize;
  736. info->flags = FBINFO_FLAG_DEFAULT | FBINFO_VIRTFB;
  737. info->fbops = &auok190xfb_ops;
  738. /* deferred io init */
  739. info->fbdefio = devm_kzalloc(info->device,
  740. sizeof(struct fb_deferred_io),
  741. GFP_KERNEL);
  742. if (!info->fbdefio) {
  743. dev_err(info->device, "Failed to allocate memory\n");
  744. ret = -ENOMEM;
  745. goto err_defio;
  746. }
  747. dev_dbg(info->device, "targetting %d frames per second\n", board->fps);
  748. info->fbdefio->delay = HZ / board->fps;
  749. info->fbdefio->first_io = auok190xfb_dpy_first_io,
  750. info->fbdefio->deferred_io = auok190xfb_dpy_deferred_io,
  751. fb_deferred_io_init(info);
  752. /* color map */
  753. ret = fb_alloc_cmap(&info->cmap, 256, 0);
  754. if (ret < 0) {
  755. dev_err(info->device, "Failed to allocate colormap\n");
  756. goto err_cmap;
  757. }
  758. /* controller init */
  759. par->consecutive_threshold = 100;
  760. par->init(par);
  761. auok190x_identify(par);
  762. platform_set_drvdata(pdev, info);
  763. ret = register_framebuffer(info);
  764. if (ret < 0)
  765. goto err_regfb;
  766. ret = sysfs_create_group(&info->device->kobj, &auok190x_attr_group);
  767. if (ret)
  768. goto err_sysfs;
  769. dev_info(info->device, "fb%d: %dx%d using %dK of video memory\n",
  770. info->node, info->var.xres, info->var.yres,
  771. videomemorysize >> 10);
  772. /* increase autosuspend_delay when we use alternative methods
  773. * for runtime_pm
  774. */
  775. par->autosuspend_delay = (board->quirks & AUOK190X_QUIRK_STANDBYBROKEN)
  776. ? 1000 : 200;
  777. pm_runtime_set_active(info->device);
  778. pm_runtime_enable(info->device);
  779. pm_runtime_set_autosuspend_delay(info->device, par->autosuspend_delay);
  780. pm_runtime_use_autosuspend(info->device);
  781. return 0;
  782. err_sysfs:
  783. unregister_framebuffer(info);
  784. err_regfb:
  785. fb_dealloc_cmap(&info->cmap);
  786. err_cmap:
  787. fb_deferred_io_cleanup(info);
  788. err_defio:
  789. vfree((void *)info->screen_base);
  790. err_irq:
  791. auok190x_power(par, 0);
  792. err_gpio3:
  793. gpio_free(board->gpio_nrst);
  794. err_gpio2:
  795. gpio_free(board->gpio_nsleep);
  796. err_gpio1:
  797. board->cleanup(par);
  798. err_board:
  799. regulator_put(par->regulator);
  800. err_reg:
  801. framebuffer_release(info);
  802. return ret;
  803. }
  804. EXPORT_SYMBOL_GPL(auok190x_common_probe);
  805. int __devexit auok190x_common_remove(struct platform_device *pdev)
  806. {
  807. struct fb_info *info = platform_get_drvdata(pdev);
  808. struct auok190xfb_par *par = info->par;
  809. struct auok190x_board *board = par->board;
  810. pm_runtime_disable(info->device);
  811. sysfs_remove_group(&info->device->kobj, &auok190x_attr_group);
  812. unregister_framebuffer(info);
  813. fb_dealloc_cmap(&info->cmap);
  814. fb_deferred_io_cleanup(info);
  815. vfree((void *)info->screen_base);
  816. auok190x_power(par, 0);
  817. gpio_free(board->gpio_nrst);
  818. gpio_free(board->gpio_nsleep);
  819. board->cleanup(par);
  820. regulator_put(par->regulator);
  821. framebuffer_release(info);
  822. return 0;
  823. }
  824. EXPORT_SYMBOL_GPL(auok190x_common_remove);
  825. MODULE_DESCRIPTION("Common code for AUO-K190X controllers");
  826. MODULE_AUTHOR("Heiko Stuebner <heiko@sntech.de>");
  827. MODULE_LICENSE("GPL");