auo_k190x.c 24 KB

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