broadsheetfb.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237
  1. /*
  2. * broadsheetfb.c -- FB driver for E-Ink Broadsheet controller
  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 driver is written to be used with the Broadsheet display controller.
  13. *
  14. * It is intended to be architecture independent. A board specific driver
  15. * must be used to perform all the physical IO interactions.
  16. *
  17. */
  18. #include <linux/module.h>
  19. #include <linux/kernel.h>
  20. #include <linux/errno.h>
  21. #include <linux/string.h>
  22. #include <linux/mm.h>
  23. #include <linux/slab.h>
  24. #include <linux/vmalloc.h>
  25. #include <linux/delay.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/fb.h>
  28. #include <linux/init.h>
  29. #include <linux/platform_device.h>
  30. #include <linux/list.h>
  31. #include <linux/firmware.h>
  32. #include <linux/uaccess.h>
  33. #include <video/broadsheetfb.h>
  34. /* track panel specific parameters */
  35. struct panel_info {
  36. int w;
  37. int h;
  38. u16 sdcfg;
  39. u16 gdcfg;
  40. u16 lutfmt;
  41. u16 fsynclen;
  42. u16 fendfbegin;
  43. u16 lsynclen;
  44. u16 lendlbegin;
  45. u16 pixclk;
  46. };
  47. /* table of panel specific parameters to be indexed into by the board drivers */
  48. static struct panel_info panel_table[] = {
  49. { /* standard 6" on TFT backplane */
  50. .w = 800,
  51. .h = 600,
  52. .sdcfg = (100 | (1 << 8) | (1 << 9)),
  53. .gdcfg = 2,
  54. .lutfmt = (4 | (1 << 7)),
  55. .fsynclen = 4,
  56. .fendfbegin = (10 << 8) | 4,
  57. .lsynclen = 10,
  58. .lendlbegin = (100 << 8) | 4,
  59. .pixclk = 6,
  60. },
  61. { /* custom 3.7" flexible on PET or steel */
  62. .w = 320,
  63. .h = 240,
  64. .sdcfg = (67 | (0 << 8) | (0 << 9) | (0 << 10) | (0 << 12)),
  65. .gdcfg = 3,
  66. .lutfmt = (4 | (1 << 7)),
  67. .fsynclen = 0,
  68. .fendfbegin = (80 << 8) | 4,
  69. .lsynclen = 10,
  70. .lendlbegin = (80 << 8) | 20,
  71. .pixclk = 14,
  72. },
  73. { /* standard 9.7" on TFT backplane */
  74. .w = 1200,
  75. .h = 825,
  76. .sdcfg = (100 | (1 << 8) | (1 << 9) | (0 << 10) | (0 << 12)),
  77. .gdcfg = 2,
  78. .lutfmt = (4 | (1 << 7)),
  79. .fsynclen = 0,
  80. .fendfbegin = (4 << 8) | 4,
  81. .lsynclen = 4,
  82. .lendlbegin = (60 << 8) | 10,
  83. .pixclk = 3,
  84. },
  85. };
  86. #define DPY_W 800
  87. #define DPY_H 600
  88. static struct fb_fix_screeninfo broadsheetfb_fix __devinitdata = {
  89. .id = "broadsheetfb",
  90. .type = FB_TYPE_PACKED_PIXELS,
  91. .visual = FB_VISUAL_STATIC_PSEUDOCOLOR,
  92. .xpanstep = 0,
  93. .ypanstep = 0,
  94. .ywrapstep = 0,
  95. .line_length = DPY_W,
  96. .accel = FB_ACCEL_NONE,
  97. };
  98. static struct fb_var_screeninfo broadsheetfb_var __devinitdata = {
  99. .xres = DPY_W,
  100. .yres = DPY_H,
  101. .xres_virtual = DPY_W,
  102. .yres_virtual = DPY_H,
  103. .bits_per_pixel = 8,
  104. .grayscale = 1,
  105. .red = { 0, 4, 0 },
  106. .green = { 0, 4, 0 },
  107. .blue = { 0, 4, 0 },
  108. .transp = { 0, 0, 0 },
  109. };
  110. /* main broadsheetfb functions */
  111. static void broadsheet_gpio_issue_data(struct broadsheetfb_par *par, u16 data)
  112. {
  113. par->board->set_ctl(par, BS_WR, 0);
  114. par->board->set_hdb(par, data);
  115. par->board->set_ctl(par, BS_WR, 1);
  116. }
  117. static void broadsheet_gpio_issue_cmd(struct broadsheetfb_par *par, u16 data)
  118. {
  119. par->board->set_ctl(par, BS_DC, 0);
  120. broadsheet_gpio_issue_data(par, data);
  121. }
  122. static void broadsheet_gpio_send_command(struct broadsheetfb_par *par, u16 data)
  123. {
  124. par->board->wait_for_rdy(par);
  125. par->board->set_ctl(par, BS_CS, 0);
  126. broadsheet_gpio_issue_cmd(par, data);
  127. par->board->set_ctl(par, BS_DC, 1);
  128. par->board->set_ctl(par, BS_CS, 1);
  129. }
  130. static void broadsheet_gpio_send_cmdargs(struct broadsheetfb_par *par, u16 cmd,
  131. int argc, u16 *argv)
  132. {
  133. int i;
  134. par->board->wait_for_rdy(par);
  135. par->board->set_ctl(par, BS_CS, 0);
  136. broadsheet_gpio_issue_cmd(par, cmd);
  137. par->board->set_ctl(par, BS_DC, 1);
  138. for (i = 0; i < argc; i++)
  139. broadsheet_gpio_issue_data(par, argv[i]);
  140. par->board->set_ctl(par, BS_CS, 1);
  141. }
  142. static void broadsheet_mmio_send_cmdargs(struct broadsheetfb_par *par, u16 cmd,
  143. int argc, u16 *argv)
  144. {
  145. int i;
  146. par->board->mmio_write(par, BS_MMIO_CMD, cmd);
  147. for (i = 0; i < argc; i++)
  148. par->board->mmio_write(par, BS_MMIO_DATA, argv[i]);
  149. }
  150. static void broadsheet_send_command(struct broadsheetfb_par *par, u16 data)
  151. {
  152. if (par->board->mmio_write)
  153. par->board->mmio_write(par, BS_MMIO_CMD, data);
  154. else
  155. broadsheet_gpio_send_command(par, data);
  156. }
  157. static void broadsheet_send_cmdargs(struct broadsheetfb_par *par, u16 cmd,
  158. int argc, u16 *argv)
  159. {
  160. if (par->board->mmio_write)
  161. broadsheet_mmio_send_cmdargs(par, cmd, argc, argv);
  162. else
  163. broadsheet_gpio_send_cmdargs(par, cmd, argc, argv);
  164. }
  165. static void broadsheet_gpio_burst_write(struct broadsheetfb_par *par, int size,
  166. u16 *data)
  167. {
  168. int i;
  169. u16 tmp;
  170. par->board->set_ctl(par, BS_CS, 0);
  171. par->board->set_ctl(par, BS_DC, 1);
  172. for (i = 0; i < size; i++) {
  173. par->board->set_ctl(par, BS_WR, 0);
  174. tmp = (data[i] & 0x0F) << 4;
  175. tmp |= (data[i] & 0x0F00) << 4;
  176. par->board->set_hdb(par, tmp);
  177. par->board->set_ctl(par, BS_WR, 1);
  178. }
  179. par->board->set_ctl(par, BS_CS, 1);
  180. }
  181. static void broadsheet_mmio_burst_write(struct broadsheetfb_par *par, int size,
  182. u16 *data)
  183. {
  184. int i;
  185. u16 tmp;
  186. for (i = 0; i < size; i++) {
  187. tmp = (data[i] & 0x0F) << 4;
  188. tmp |= (data[i] & 0x0F00) << 4;
  189. par->board->mmio_write(par, BS_MMIO_DATA, tmp);
  190. }
  191. }
  192. static void broadsheet_burst_write(struct broadsheetfb_par *par, int size,
  193. u16 *data)
  194. {
  195. if (par->board->mmio_write)
  196. broadsheet_mmio_burst_write(par, size, data);
  197. else
  198. broadsheet_gpio_burst_write(par, size, data);
  199. }
  200. static u16 broadsheet_gpio_get_data(struct broadsheetfb_par *par)
  201. {
  202. u16 res;
  203. /* wait for ready to go hi. (lo is busy) */
  204. par->board->wait_for_rdy(par);
  205. /* cs lo, dc lo for cmd, we lo for each data, db as usual */
  206. par->board->set_ctl(par, BS_DC, 1);
  207. par->board->set_ctl(par, BS_CS, 0);
  208. par->board->set_ctl(par, BS_WR, 0);
  209. res = par->board->get_hdb(par);
  210. /* strobe wr */
  211. par->board->set_ctl(par, BS_WR, 1);
  212. par->board->set_ctl(par, BS_CS, 1);
  213. return res;
  214. }
  215. static u16 broadsheet_get_data(struct broadsheetfb_par *par)
  216. {
  217. if (par->board->mmio_read)
  218. return par->board->mmio_read(par);
  219. else
  220. return broadsheet_gpio_get_data(par);
  221. }
  222. static void broadsheet_gpio_write_reg(struct broadsheetfb_par *par, u16 reg,
  223. u16 data)
  224. {
  225. /* wait for ready to go hi. (lo is busy) */
  226. par->board->wait_for_rdy(par);
  227. /* cs lo, dc lo for cmd, we lo for each data, db as usual */
  228. par->board->set_ctl(par, BS_CS, 0);
  229. broadsheet_gpio_issue_cmd(par, BS_CMD_WR_REG);
  230. par->board->set_ctl(par, BS_DC, 1);
  231. broadsheet_gpio_issue_data(par, reg);
  232. broadsheet_gpio_issue_data(par, data);
  233. par->board->set_ctl(par, BS_CS, 1);
  234. }
  235. static void broadsheet_mmio_write_reg(struct broadsheetfb_par *par, u16 reg,
  236. u16 data)
  237. {
  238. par->board->mmio_write(par, BS_MMIO_CMD, BS_CMD_WR_REG);
  239. par->board->mmio_write(par, BS_MMIO_DATA, reg);
  240. par->board->mmio_write(par, BS_MMIO_DATA, data);
  241. }
  242. static void broadsheet_write_reg(struct broadsheetfb_par *par, u16 reg,
  243. u16 data)
  244. {
  245. if (par->board->mmio_write)
  246. broadsheet_mmio_write_reg(par, reg, data);
  247. else
  248. broadsheet_gpio_write_reg(par, reg, data);
  249. }
  250. static void broadsheet_write_reg32(struct broadsheetfb_par *par, u16 reg,
  251. u32 data)
  252. {
  253. broadsheet_write_reg(par, reg, cpu_to_le32(data) & 0xFFFF);
  254. broadsheet_write_reg(par, reg + 2, (cpu_to_le32(data) >> 16) & 0xFFFF);
  255. }
  256. static u16 broadsheet_read_reg(struct broadsheetfb_par *par, u16 reg)
  257. {
  258. broadsheet_send_cmdargs(par, BS_CMD_RD_REG, 1, &reg);
  259. par->board->wait_for_rdy(par);
  260. return broadsheet_get_data(par);
  261. }
  262. /* functions for waveform manipulation */
  263. static int is_broadsheet_pll_locked(struct broadsheetfb_par *par)
  264. {
  265. return broadsheet_read_reg(par, 0x000A) & 0x0001;
  266. }
  267. static int broadsheet_setup_plls(struct broadsheetfb_par *par)
  268. {
  269. int retry_count = 0;
  270. u16 tmp;
  271. /* disable arral saemipu mode */
  272. broadsheet_write_reg(par, 0x0006, 0x0000);
  273. broadsheet_write_reg(par, 0x0010, 0x0004);
  274. broadsheet_write_reg(par, 0x0012, 0x5949);
  275. broadsheet_write_reg(par, 0x0014, 0x0040);
  276. broadsheet_write_reg(par, 0x0016, 0x0000);
  277. do {
  278. if (retry_count++ > 100)
  279. return -ETIMEDOUT;
  280. mdelay(1);
  281. } while (!is_broadsheet_pll_locked(par));
  282. tmp = broadsheet_read_reg(par, 0x0006);
  283. tmp &= ~0x1;
  284. broadsheet_write_reg(par, 0x0006, tmp);
  285. return 0;
  286. }
  287. static int broadsheet_setup_spi(struct broadsheetfb_par *par)
  288. {
  289. broadsheet_write_reg(par, 0x0204, ((3 << 3) | 1));
  290. broadsheet_write_reg(par, 0x0208, 0x0001);
  291. return 0;
  292. }
  293. static int broadsheet_setup_spiflash(struct broadsheetfb_par *par,
  294. u16 *orig_sfmcd)
  295. {
  296. *orig_sfmcd = broadsheet_read_reg(par, 0x0204);
  297. broadsheet_write_reg(par, 0x0208, 0);
  298. broadsheet_write_reg(par, 0x0204, 0);
  299. broadsheet_write_reg(par, 0x0204, ((3 << 3) | 1));
  300. return 0;
  301. }
  302. static int broadsheet_spiflash_wait_for_bit(struct broadsheetfb_par *par,
  303. u16 reg, int bitnum, int val,
  304. int timeout)
  305. {
  306. u16 tmp;
  307. do {
  308. tmp = broadsheet_read_reg(par, reg);
  309. if (((tmp >> bitnum) & 1) == val)
  310. return 0;
  311. mdelay(1);
  312. } while (timeout--);
  313. return -ETIMEDOUT;
  314. }
  315. static int broadsheet_spiflash_write_byte(struct broadsheetfb_par *par, u8 data)
  316. {
  317. broadsheet_write_reg(par, 0x0202, (data | 0x100));
  318. return broadsheet_spiflash_wait_for_bit(par, 0x0206, 3, 0, 100);
  319. }
  320. static int broadsheet_spiflash_read_byte(struct broadsheetfb_par *par, u8 *data)
  321. {
  322. int err;
  323. u16 tmp;
  324. broadsheet_write_reg(par, 0x0202, 0);
  325. err = broadsheet_spiflash_wait_for_bit(par, 0x0206, 3, 0, 100);
  326. if (err)
  327. return err;
  328. tmp = broadsheet_read_reg(par, 0x200);
  329. *data = tmp & 0xFF;
  330. return 0;
  331. }
  332. static int broadsheet_spiflash_wait_for_status(struct broadsheetfb_par *par,
  333. int timeout)
  334. {
  335. u8 tmp;
  336. int err;
  337. do {
  338. broadsheet_write_reg(par, 0x0208, 1);
  339. err = broadsheet_spiflash_write_byte(par, 0x05);
  340. if (err)
  341. goto failout;
  342. err = broadsheet_spiflash_read_byte(par, &tmp);
  343. if (err)
  344. goto failout;
  345. broadsheet_write_reg(par, 0x0208, 0);
  346. if (!(tmp & 0x1))
  347. return 0;
  348. mdelay(5);
  349. } while (timeout--);
  350. dev_err(par->info->device, "Timed out waiting for spiflash status\n");
  351. return -ETIMEDOUT;
  352. failout:
  353. broadsheet_write_reg(par, 0x0208, 0);
  354. return err;
  355. }
  356. static int broadsheet_spiflash_op_on_address(struct broadsheetfb_par *par,
  357. u8 op, u32 addr)
  358. {
  359. int i;
  360. u8 tmp;
  361. int err;
  362. broadsheet_write_reg(par, 0x0208, 1);
  363. err = broadsheet_spiflash_write_byte(par, op);
  364. if (err)
  365. return err;
  366. for (i = 2; i >= 0; i--) {
  367. tmp = ((addr >> (i * 8)) & 0xFF);
  368. err = broadsheet_spiflash_write_byte(par, tmp);
  369. if (err)
  370. return err;
  371. }
  372. return err;
  373. }
  374. static int broadsheet_verify_spiflash(struct broadsheetfb_par *par,
  375. int *flash_type)
  376. {
  377. int err = 0;
  378. u8 sig;
  379. err = broadsheet_spiflash_op_on_address(par, 0xAB, 0x00000000);
  380. if (err)
  381. goto failout;
  382. err = broadsheet_spiflash_read_byte(par, &sig);
  383. if (err)
  384. goto failout;
  385. if ((sig != 0x10) && (sig != 0x11)) {
  386. dev_err(par->info->device, "Unexpected flash type\n");
  387. err = -EINVAL;
  388. goto failout;
  389. }
  390. *flash_type = sig;
  391. failout:
  392. broadsheet_write_reg(par, 0x0208, 0);
  393. return err;
  394. }
  395. static int broadsheet_setup_for_wfm_write(struct broadsheetfb_par *par,
  396. u16 *initial_sfmcd, int *flash_type)
  397. {
  398. int err;
  399. err = broadsheet_setup_plls(par);
  400. if (err)
  401. return err;
  402. broadsheet_write_reg(par, 0x0106, 0x0203);
  403. err = broadsheet_setup_spi(par);
  404. if (err)
  405. return err;
  406. err = broadsheet_setup_spiflash(par, initial_sfmcd);
  407. if (err)
  408. return err;
  409. return broadsheet_verify_spiflash(par, flash_type);
  410. }
  411. static int broadsheet_spiflash_write_control(struct broadsheetfb_par *par,
  412. int mode)
  413. {
  414. int err;
  415. broadsheet_write_reg(par, 0x0208, 1);
  416. if (mode)
  417. err = broadsheet_spiflash_write_byte(par, 0x06);
  418. else
  419. err = broadsheet_spiflash_write_byte(par, 0x04);
  420. broadsheet_write_reg(par, 0x0208, 0);
  421. return err;
  422. }
  423. static int broadsheet_spiflash_erase_sector(struct broadsheetfb_par *par,
  424. int addr)
  425. {
  426. int err;
  427. broadsheet_spiflash_write_control(par, 1);
  428. err = broadsheet_spiflash_op_on_address(par, 0xD8, addr);
  429. broadsheet_write_reg(par, 0x0208, 0);
  430. if (err)
  431. return err;
  432. err = broadsheet_spiflash_wait_for_status(par, 1000);
  433. return err;
  434. }
  435. static int broadsheet_spiflash_read_range(struct broadsheetfb_par *par,
  436. int addr, int size, char *data)
  437. {
  438. int err;
  439. int i;
  440. err = broadsheet_spiflash_op_on_address(par, 0x03, addr);
  441. if (err)
  442. goto failout;
  443. for (i = 0; i < size; i++) {
  444. err = broadsheet_spiflash_read_byte(par, &data[i]);
  445. if (err)
  446. goto failout;
  447. }
  448. failout:
  449. broadsheet_write_reg(par, 0x0208, 0);
  450. return err;
  451. }
  452. #define BS_SPIFLASH_PAGE_SIZE 256
  453. static int broadsheet_spiflash_write_page(struct broadsheetfb_par *par,
  454. int addr, const char *data)
  455. {
  456. int err;
  457. int i;
  458. broadsheet_spiflash_write_control(par, 1);
  459. err = broadsheet_spiflash_op_on_address(par, 0x02, addr);
  460. if (err)
  461. goto failout;
  462. for (i = 0; i < BS_SPIFLASH_PAGE_SIZE; i++) {
  463. err = broadsheet_spiflash_write_byte(par, data[i]);
  464. if (err)
  465. goto failout;
  466. }
  467. broadsheet_write_reg(par, 0x0208, 0);
  468. err = broadsheet_spiflash_wait_for_status(par, 100);
  469. failout:
  470. return err;
  471. }
  472. static int broadsheet_spiflash_write_sector(struct broadsheetfb_par *par,
  473. int addr, const char *data, int sector_size)
  474. {
  475. int i;
  476. int err;
  477. for (i = 0; i < sector_size; i += BS_SPIFLASH_PAGE_SIZE) {
  478. err = broadsheet_spiflash_write_page(par, addr + i, &data[i]);
  479. if (err)
  480. return err;
  481. }
  482. return 0;
  483. }
  484. /*
  485. * The caller must guarantee that the data to be rewritten is entirely
  486. * contained within this sector. That is, data_start_addr + data_len
  487. * must be less than sector_start_addr + sector_size.
  488. */
  489. static int broadsheet_spiflash_rewrite_sector(struct broadsheetfb_par *par,
  490. int sector_size, int data_start_addr,
  491. int data_len, const char *data)
  492. {
  493. int err;
  494. char *sector_buffer;
  495. int tail_start_addr;
  496. int start_sector_addr;
  497. sector_buffer = kzalloc(sizeof(char)*sector_size, GFP_KERNEL);
  498. if (!sector_buffer)
  499. return -ENOMEM;
  500. /* the start address of the sector is the 0th byte of that sector */
  501. start_sector_addr = (data_start_addr / sector_size) * sector_size;
  502. /*
  503. * check if there is head data that we need to readback into our sector
  504. * buffer first
  505. */
  506. if (data_start_addr != start_sector_addr) {
  507. /*
  508. * we need to read every byte up till the start address of our
  509. * data and we put it into our sector buffer.
  510. */
  511. err = broadsheet_spiflash_read_range(par, start_sector_addr,
  512. data_start_addr, sector_buffer);
  513. if (err)
  514. return err;
  515. }
  516. /* now we copy our data into the right place in the sector buffer */
  517. memcpy(sector_buffer + data_start_addr, data, data_len);
  518. /*
  519. * now we check if there is a tail section of the sector that we need to
  520. * readback.
  521. */
  522. tail_start_addr = (data_start_addr + data_len) % sector_size;
  523. if (tail_start_addr) {
  524. int tail_len;
  525. tail_len = sector_size - tail_start_addr;
  526. /* now we read this tail into our sector buffer */
  527. err = broadsheet_spiflash_read_range(par, tail_start_addr,
  528. tail_len, sector_buffer + tail_start_addr);
  529. if (err)
  530. return err;
  531. }
  532. /* if we got here we have the full sector that we want to rewrite. */
  533. /* first erase the sector */
  534. err = broadsheet_spiflash_erase_sector(par, start_sector_addr);
  535. if (err)
  536. return err;
  537. /* now write it */
  538. err = broadsheet_spiflash_write_sector(par, start_sector_addr,
  539. sector_buffer, sector_size);
  540. return err;
  541. }
  542. static int broadsheet_write_spiflash(struct broadsheetfb_par *par, u32 wfm_addr,
  543. const u8 *wfm, int bytecount, int flash_type)
  544. {
  545. int sector_size;
  546. int err;
  547. int cur_addr;
  548. int writecount;
  549. int maxlen;
  550. int offset = 0;
  551. switch (flash_type) {
  552. case 0x10:
  553. sector_size = 32*1024;
  554. break;
  555. case 0x11:
  556. default:
  557. sector_size = 64*1024;
  558. break;
  559. }
  560. while (bytecount) {
  561. cur_addr = wfm_addr + offset;
  562. maxlen = roundup(cur_addr, sector_size) - cur_addr;
  563. writecount = min(bytecount, maxlen);
  564. err = broadsheet_spiflash_rewrite_sector(par, sector_size,
  565. cur_addr, writecount, wfm + offset);
  566. if (err)
  567. return err;
  568. offset += writecount;
  569. bytecount -= writecount;
  570. }
  571. return 0;
  572. }
  573. static int broadsheet_store_waveform_to_spiflash(struct broadsheetfb_par *par,
  574. const u8 *wfm, size_t wfm_size)
  575. {
  576. int err = 0;
  577. u16 initial_sfmcd = 0;
  578. int flash_type = 0;
  579. err = broadsheet_setup_for_wfm_write(par, &initial_sfmcd, &flash_type);
  580. if (err)
  581. goto failout;
  582. err = broadsheet_write_spiflash(par, 0x886, wfm, wfm_size, flash_type);
  583. failout:
  584. broadsheet_write_reg(par, 0x0204, initial_sfmcd);
  585. return err;
  586. }
  587. static ssize_t broadsheet_loadstore_waveform(struct device *dev,
  588. struct device_attribute *attr,
  589. const char *buf, size_t len)
  590. {
  591. int err;
  592. struct fb_info *info = dev_get_drvdata(dev);
  593. struct broadsheetfb_par *par = info->par;
  594. const struct firmware *fw_entry;
  595. if (len < 1)
  596. return -EINVAL;
  597. err = request_firmware(&fw_entry, "broadsheet.wbf", dev);
  598. if (err < 0) {
  599. dev_err(dev, "Failed to get broadsheet waveform\n");
  600. goto err_failed;
  601. }
  602. /* try to enforce reasonable min max on waveform */
  603. if ((fw_entry->size < 8*1024) || (fw_entry->size > 64*1024)) {
  604. dev_err(dev, "Invalid waveform\n");
  605. err = -EINVAL;
  606. goto err_failed;
  607. }
  608. mutex_lock(&(par->io_lock));
  609. err = broadsheet_store_waveform_to_spiflash(par, fw_entry->data,
  610. fw_entry->size);
  611. mutex_unlock(&(par->io_lock));
  612. if (err < 0) {
  613. dev_err(dev, "Failed to store broadsheet waveform\n");
  614. goto err_failed;
  615. }
  616. dev_info(dev, "Stored broadsheet waveform, size %zd\n", fw_entry->size);
  617. return len;
  618. err_failed:
  619. return err;
  620. }
  621. static DEVICE_ATTR(loadstore_waveform, S_IWUSR, NULL,
  622. broadsheet_loadstore_waveform);
  623. /* upper level functions that manipulate the display and other stuff */
  624. static void __devinit broadsheet_init_display(struct broadsheetfb_par *par)
  625. {
  626. u16 args[5];
  627. int xres = par->info->var.xres;
  628. int yres = par->info->var.yres;
  629. args[0] = panel_table[par->panel_index].w;
  630. args[1] = panel_table[par->panel_index].h;
  631. args[2] = panel_table[par->panel_index].sdcfg;
  632. args[3] = panel_table[par->panel_index].gdcfg;
  633. args[4] = panel_table[par->panel_index].lutfmt;
  634. broadsheet_send_cmdargs(par, BS_CMD_INIT_DSPE_CFG, 5, args);
  635. /* did the controller really set it? */
  636. broadsheet_send_cmdargs(par, BS_CMD_INIT_DSPE_CFG, 5, args);
  637. args[0] = panel_table[par->panel_index].fsynclen;
  638. args[1] = panel_table[par->panel_index].fendfbegin;
  639. args[2] = panel_table[par->panel_index].lsynclen;
  640. args[3] = panel_table[par->panel_index].lendlbegin;
  641. args[4] = panel_table[par->panel_index].pixclk;
  642. broadsheet_send_cmdargs(par, BS_CMD_INIT_DSPE_TMG, 5, args);
  643. broadsheet_write_reg32(par, 0x310, xres*yres*2);
  644. /* setup waveform */
  645. args[0] = 0x886;
  646. args[1] = 0;
  647. broadsheet_send_cmdargs(par, BS_CMD_RD_WFM_INFO, 2, args);
  648. broadsheet_send_command(par, BS_CMD_UPD_GDRV_CLR);
  649. broadsheet_send_command(par, BS_CMD_WAIT_DSPE_TRG);
  650. broadsheet_write_reg(par, 0x330, 0x84);
  651. broadsheet_send_command(par, BS_CMD_WAIT_DSPE_TRG);
  652. args[0] = (0x3 << 4);
  653. broadsheet_send_cmdargs(par, BS_CMD_LD_IMG, 1, args);
  654. args[0] = 0x154;
  655. broadsheet_send_cmdargs(par, BS_CMD_WR_REG, 1, args);
  656. broadsheet_burst_write(par, (panel_table[par->panel_index].w *
  657. panel_table[par->panel_index].h)/2,
  658. (u16 *) par->info->screen_base);
  659. broadsheet_send_command(par, BS_CMD_LD_IMG_END);
  660. args[0] = 0x4300;
  661. broadsheet_send_cmdargs(par, BS_CMD_UPD_FULL, 1, args);
  662. broadsheet_send_command(par, BS_CMD_WAIT_DSPE_TRG);
  663. broadsheet_send_command(par, BS_CMD_WAIT_DSPE_FREND);
  664. par->board->wait_for_rdy(par);
  665. }
  666. static void __devinit broadsheet_identify(struct broadsheetfb_par *par)
  667. {
  668. u16 rev, prc;
  669. struct device *dev = par->info->device;
  670. rev = broadsheet_read_reg(par, BS_REG_REV);
  671. prc = broadsheet_read_reg(par, BS_REG_PRC);
  672. dev_info(dev, "Broadsheet Rev 0x%x, Product Code 0x%x\n", rev, prc);
  673. if (prc != 0x0047)
  674. dev_warn(dev, "Unrecognized Broadsheet Product Code\n");
  675. if (rev != 0x0100)
  676. dev_warn(dev, "Unrecognized Broadsheet Revision\n");
  677. }
  678. static void __devinit broadsheet_init(struct broadsheetfb_par *par)
  679. {
  680. broadsheet_send_command(par, BS_CMD_INIT_SYS_RUN);
  681. /* the controller needs a second */
  682. msleep(1000);
  683. broadsheet_init_display(par);
  684. }
  685. static void broadsheetfb_dpy_update_pages(struct broadsheetfb_par *par,
  686. u16 y1, u16 y2)
  687. {
  688. u16 args[5];
  689. unsigned char *buf = (unsigned char *)par->info->screen_base;
  690. mutex_lock(&(par->io_lock));
  691. /* y1 must be a multiple of 4 so drop the lower bits */
  692. y1 &= 0xFFFC;
  693. /* y2 must be a multiple of 4 , but - 1 so up the lower bits */
  694. y2 |= 0x0003;
  695. args[0] = 0x3 << 4;
  696. args[1] = 0;
  697. args[2] = y1;
  698. args[3] = cpu_to_le16(par->info->var.xres);
  699. args[4] = y2;
  700. broadsheet_send_cmdargs(par, BS_CMD_LD_IMG_AREA, 5, args);
  701. args[0] = 0x154;
  702. broadsheet_send_cmdargs(par, BS_CMD_WR_REG, 1, args);
  703. buf += y1 * par->info->var.xres;
  704. broadsheet_burst_write(par, ((1 + y2 - y1) * par->info->var.xres)/2,
  705. (u16 *) buf);
  706. broadsheet_send_command(par, BS_CMD_LD_IMG_END);
  707. args[0] = 0x4300;
  708. broadsheet_send_cmdargs(par, BS_CMD_UPD_FULL, 1, args);
  709. broadsheet_send_command(par, BS_CMD_WAIT_DSPE_TRG);
  710. broadsheet_send_command(par, BS_CMD_WAIT_DSPE_FREND);
  711. par->board->wait_for_rdy(par);
  712. mutex_unlock(&(par->io_lock));
  713. }
  714. static void broadsheetfb_dpy_update(struct broadsheetfb_par *par)
  715. {
  716. u16 args[5];
  717. mutex_lock(&(par->io_lock));
  718. args[0] = 0x3 << 4;
  719. broadsheet_send_cmdargs(par, BS_CMD_LD_IMG, 1, args);
  720. args[0] = 0x154;
  721. broadsheet_send_cmdargs(par, BS_CMD_WR_REG, 1, args);
  722. broadsheet_burst_write(par, (panel_table[par->panel_index].w *
  723. panel_table[par->panel_index].h)/2,
  724. (u16 *) par->info->screen_base);
  725. broadsheet_send_command(par, BS_CMD_LD_IMG_END);
  726. args[0] = 0x4300;
  727. broadsheet_send_cmdargs(par, BS_CMD_UPD_FULL, 1, args);
  728. broadsheet_send_command(par, BS_CMD_WAIT_DSPE_TRG);
  729. broadsheet_send_command(par, BS_CMD_WAIT_DSPE_FREND);
  730. par->board->wait_for_rdy(par);
  731. mutex_unlock(&(par->io_lock));
  732. }
  733. /* this is called back from the deferred io workqueue */
  734. static void broadsheetfb_dpy_deferred_io(struct fb_info *info,
  735. struct list_head *pagelist)
  736. {
  737. u16 y1 = 0, h = 0;
  738. int prev_index = -1;
  739. struct page *cur;
  740. struct fb_deferred_io *fbdefio = info->fbdefio;
  741. int h_inc;
  742. u16 yres = info->var.yres;
  743. u16 xres = info->var.xres;
  744. /* height increment is fixed per page */
  745. h_inc = DIV_ROUND_UP(PAGE_SIZE , xres);
  746. /* walk the written page list and swizzle the data */
  747. list_for_each_entry(cur, &fbdefio->pagelist, lru) {
  748. if (prev_index < 0) {
  749. /* just starting so assign first page */
  750. y1 = (cur->index << PAGE_SHIFT) / xres;
  751. h = h_inc;
  752. } else if ((prev_index + 1) == cur->index) {
  753. /* this page is consecutive so increase our height */
  754. h += h_inc;
  755. } else {
  756. /* page not consecutive, issue previous update first */
  757. broadsheetfb_dpy_update_pages(info->par, y1, y1 + h);
  758. /* start over with our non consecutive page */
  759. y1 = (cur->index << PAGE_SHIFT) / xres;
  760. h = h_inc;
  761. }
  762. prev_index = cur->index;
  763. }
  764. /* if we still have any pages to update we do so now */
  765. if (h >= yres) {
  766. /* its a full screen update, just do it */
  767. broadsheetfb_dpy_update(info->par);
  768. } else {
  769. broadsheetfb_dpy_update_pages(info->par, y1,
  770. min((u16) (y1 + h), yres));
  771. }
  772. }
  773. static void broadsheetfb_fillrect(struct fb_info *info,
  774. const struct fb_fillrect *rect)
  775. {
  776. struct broadsheetfb_par *par = info->par;
  777. sys_fillrect(info, rect);
  778. broadsheetfb_dpy_update(par);
  779. }
  780. static void broadsheetfb_copyarea(struct fb_info *info,
  781. const struct fb_copyarea *area)
  782. {
  783. struct broadsheetfb_par *par = info->par;
  784. sys_copyarea(info, area);
  785. broadsheetfb_dpy_update(par);
  786. }
  787. static void broadsheetfb_imageblit(struct fb_info *info,
  788. const struct fb_image *image)
  789. {
  790. struct broadsheetfb_par *par = info->par;
  791. sys_imageblit(info, image);
  792. broadsheetfb_dpy_update(par);
  793. }
  794. /*
  795. * this is the slow path from userspace. they can seek and write to
  796. * the fb. it's inefficient to do anything less than a full screen draw
  797. */
  798. static ssize_t broadsheetfb_write(struct fb_info *info, const char __user *buf,
  799. size_t count, loff_t *ppos)
  800. {
  801. struct broadsheetfb_par *par = info->par;
  802. unsigned long p = *ppos;
  803. void *dst;
  804. int err = 0;
  805. unsigned long total_size;
  806. if (info->state != FBINFO_STATE_RUNNING)
  807. return -EPERM;
  808. total_size = info->fix.smem_len;
  809. if (p > total_size)
  810. return -EFBIG;
  811. if (count > total_size) {
  812. err = -EFBIG;
  813. count = total_size;
  814. }
  815. if (count + p > total_size) {
  816. if (!err)
  817. err = -ENOSPC;
  818. count = total_size - p;
  819. }
  820. dst = (void *)(info->screen_base + p);
  821. if (copy_from_user(dst, buf, count))
  822. err = -EFAULT;
  823. if (!err)
  824. *ppos += count;
  825. broadsheetfb_dpy_update(par);
  826. return (err) ? err : count;
  827. }
  828. static struct fb_ops broadsheetfb_ops = {
  829. .owner = THIS_MODULE,
  830. .fb_read = fb_sys_read,
  831. .fb_write = broadsheetfb_write,
  832. .fb_fillrect = broadsheetfb_fillrect,
  833. .fb_copyarea = broadsheetfb_copyarea,
  834. .fb_imageblit = broadsheetfb_imageblit,
  835. };
  836. static struct fb_deferred_io broadsheetfb_defio = {
  837. .delay = HZ/4,
  838. .deferred_io = broadsheetfb_dpy_deferred_io,
  839. };
  840. static int __devinit broadsheetfb_probe(struct platform_device *dev)
  841. {
  842. struct fb_info *info;
  843. struct broadsheet_board *board;
  844. int retval = -ENOMEM;
  845. int videomemorysize;
  846. unsigned char *videomemory;
  847. struct broadsheetfb_par *par;
  848. int i;
  849. int dpyw, dpyh;
  850. int panel_index;
  851. /* pick up board specific routines */
  852. board = dev->dev.platform_data;
  853. if (!board)
  854. return -EINVAL;
  855. /* try to count device specific driver, if can't, platform recalls */
  856. if (!try_module_get(board->owner))
  857. return -ENODEV;
  858. info = framebuffer_alloc(sizeof(struct broadsheetfb_par), &dev->dev);
  859. if (!info)
  860. goto err;
  861. switch (board->get_panel_type()) {
  862. case 37:
  863. panel_index = 1;
  864. break;
  865. case 97:
  866. panel_index = 2;
  867. break;
  868. case 6:
  869. default:
  870. panel_index = 0;
  871. break;
  872. }
  873. dpyw = panel_table[panel_index].w;
  874. dpyh = panel_table[panel_index].h;
  875. videomemorysize = roundup((dpyw*dpyh), PAGE_SIZE);
  876. videomemory = vzalloc(videomemorysize);
  877. if (!videomemory)
  878. goto err_fb_rel;
  879. info->screen_base = (char *)videomemory;
  880. info->fbops = &broadsheetfb_ops;
  881. broadsheetfb_var.xres = dpyw;
  882. broadsheetfb_var.yres = dpyh;
  883. broadsheetfb_var.xres_virtual = dpyw;
  884. broadsheetfb_var.yres_virtual = dpyh;
  885. info->var = broadsheetfb_var;
  886. broadsheetfb_fix.line_length = dpyw;
  887. info->fix = broadsheetfb_fix;
  888. info->fix.smem_len = videomemorysize;
  889. par = info->par;
  890. par->panel_index = panel_index;
  891. par->info = info;
  892. par->board = board;
  893. par->write_reg = broadsheet_write_reg;
  894. par->read_reg = broadsheet_read_reg;
  895. init_waitqueue_head(&par->waitq);
  896. mutex_init(&par->io_lock);
  897. info->flags = FBINFO_FLAG_DEFAULT | FBINFO_VIRTFB;
  898. info->fbdefio = &broadsheetfb_defio;
  899. fb_deferred_io_init(info);
  900. retval = fb_alloc_cmap(&info->cmap, 16, 0);
  901. if (retval < 0) {
  902. dev_err(&dev->dev, "Failed to allocate colormap\n");
  903. goto err_vfree;
  904. }
  905. /* set cmap */
  906. for (i = 0; i < 16; i++)
  907. info->cmap.red[i] = (((2*i)+1)*(0xFFFF))/32;
  908. memcpy(info->cmap.green, info->cmap.red, sizeof(u16)*16);
  909. memcpy(info->cmap.blue, info->cmap.red, sizeof(u16)*16);
  910. retval = par->board->setup_irq(info);
  911. if (retval < 0)
  912. goto err_cmap;
  913. /* this inits the dpy */
  914. retval = board->init(par);
  915. if (retval < 0)
  916. goto err_free_irq;
  917. broadsheet_identify(par);
  918. broadsheet_init(par);
  919. retval = register_framebuffer(info);
  920. if (retval < 0)
  921. goto err_free_irq;
  922. platform_set_drvdata(dev, info);
  923. retval = device_create_file(&dev->dev, &dev_attr_loadstore_waveform);
  924. if (retval < 0)
  925. goto err_unreg_fb;
  926. printk(KERN_INFO
  927. "fb%d: Broadsheet frame buffer, using %dK of video memory\n",
  928. info->node, videomemorysize >> 10);
  929. return 0;
  930. err_unreg_fb:
  931. unregister_framebuffer(info);
  932. err_free_irq:
  933. board->cleanup(par);
  934. err_cmap:
  935. fb_dealloc_cmap(&info->cmap);
  936. err_vfree:
  937. vfree(videomemory);
  938. err_fb_rel:
  939. framebuffer_release(info);
  940. err:
  941. module_put(board->owner);
  942. return retval;
  943. }
  944. static int __devexit broadsheetfb_remove(struct platform_device *dev)
  945. {
  946. struct fb_info *info = platform_get_drvdata(dev);
  947. if (info) {
  948. struct broadsheetfb_par *par = info->par;
  949. device_remove_file(info->dev, &dev_attr_loadstore_waveform);
  950. unregister_framebuffer(info);
  951. fb_deferred_io_cleanup(info);
  952. par->board->cleanup(par);
  953. fb_dealloc_cmap(&info->cmap);
  954. vfree((void *)info->screen_base);
  955. module_put(par->board->owner);
  956. framebuffer_release(info);
  957. }
  958. return 0;
  959. }
  960. static struct platform_driver broadsheetfb_driver = {
  961. .probe = broadsheetfb_probe,
  962. .remove = broadsheetfb_remove,
  963. .driver = {
  964. .owner = THIS_MODULE,
  965. .name = "broadsheetfb",
  966. },
  967. };
  968. static int __init broadsheetfb_init(void)
  969. {
  970. return platform_driver_register(&broadsheetfb_driver);
  971. }
  972. static void __exit broadsheetfb_exit(void)
  973. {
  974. platform_driver_unregister(&broadsheetfb_driver);
  975. }
  976. module_init(broadsheetfb_init);
  977. module_exit(broadsheetfb_exit);
  978. MODULE_DESCRIPTION("fbdev driver for Broadsheet controller");
  979. MODULE_AUTHOR("Jaya Kumar");
  980. MODULE_LICENSE("GPL");