repaper.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100
  1. /*
  2. * DRM driver for Pervasive Displays RePaper branded e-ink panels
  3. *
  4. * Copyright 2013-2017 Pervasive Displays, Inc.
  5. * Copyright 2017 Noralf Trønnes
  6. *
  7. * The driver supports:
  8. * Material Film: Aurora Mb (V231)
  9. * Driver IC: G2 (eTC)
  10. *
  11. * The controller code was taken from the userspace driver:
  12. * https://github.com/repaper/gratis
  13. *
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License as published by
  16. * the Free Software Foundation; either version 2 of the License, or
  17. * (at your option) any later version.
  18. */
  19. #include <linux/delay.h>
  20. #include <linux/dma-buf.h>
  21. #include <linux/gpio/consumer.h>
  22. #include <linux/module.h>
  23. #include <linux/of_device.h>
  24. #include <linux/sched/clock.h>
  25. #include <linux/spi/spi.h>
  26. #include <linux/thermal.h>
  27. #include <drm/drm_gem_framebuffer_helper.h>
  28. #include <drm/tinydrm/tinydrm.h>
  29. #include <drm/tinydrm/tinydrm-helpers.h>
  30. #define REPAPER_RID_G2_COG_ID 0x12
  31. enum repaper_model {
  32. E1144CS021 = 1,
  33. E1190CS021,
  34. E2200CS021,
  35. E2271CS021,
  36. };
  37. enum repaper_stage { /* Image pixel -> Display pixel */
  38. REPAPER_COMPENSATE, /* B -> W, W -> B (Current Image) */
  39. REPAPER_WHITE, /* B -> N, W -> W (Current Image) */
  40. REPAPER_INVERSE, /* B -> N, W -> B (New Image) */
  41. REPAPER_NORMAL /* B -> B, W -> W (New Image) */
  42. };
  43. enum repaper_epd_border_byte {
  44. REPAPER_BORDER_BYTE_NONE,
  45. REPAPER_BORDER_BYTE_ZERO,
  46. REPAPER_BORDER_BYTE_SET,
  47. };
  48. struct repaper_epd {
  49. struct tinydrm_device tinydrm;
  50. struct spi_device *spi;
  51. struct gpio_desc *panel_on;
  52. struct gpio_desc *border;
  53. struct gpio_desc *discharge;
  54. struct gpio_desc *reset;
  55. struct gpio_desc *busy;
  56. struct thermal_zone_device *thermal;
  57. unsigned int height;
  58. unsigned int width;
  59. unsigned int bytes_per_scan;
  60. const u8 *channel_select;
  61. unsigned int stage_time;
  62. unsigned int factored_stage_time;
  63. bool middle_scan;
  64. bool pre_border_byte;
  65. enum repaper_epd_border_byte border_byte;
  66. u8 *line_buffer;
  67. void *current_frame;
  68. bool enabled;
  69. bool cleared;
  70. bool partial;
  71. };
  72. static inline struct repaper_epd *
  73. epd_from_tinydrm(struct tinydrm_device *tdev)
  74. {
  75. return container_of(tdev, struct repaper_epd, tinydrm);
  76. }
  77. static int repaper_spi_transfer(struct spi_device *spi, u8 header,
  78. const void *tx, void *rx, size_t len)
  79. {
  80. void *txbuf = NULL, *rxbuf = NULL;
  81. struct spi_transfer tr[2] = {};
  82. u8 *headerbuf;
  83. int ret;
  84. headerbuf = kmalloc(1, GFP_KERNEL);
  85. if (!headerbuf)
  86. return -ENOMEM;
  87. headerbuf[0] = header;
  88. tr[0].tx_buf = headerbuf;
  89. tr[0].len = 1;
  90. /* Stack allocated tx? */
  91. if (tx && len <= 32) {
  92. txbuf = kmalloc(len, GFP_KERNEL);
  93. if (!txbuf) {
  94. ret = -ENOMEM;
  95. goto out_free;
  96. }
  97. memcpy(txbuf, tx, len);
  98. }
  99. if (rx) {
  100. rxbuf = kmalloc(len, GFP_KERNEL);
  101. if (!rxbuf) {
  102. ret = -ENOMEM;
  103. goto out_free;
  104. }
  105. }
  106. tr[1].tx_buf = txbuf ? txbuf : tx;
  107. tr[1].rx_buf = rxbuf;
  108. tr[1].len = len;
  109. ndelay(80);
  110. ret = spi_sync_transfer(spi, tr, 2);
  111. if (rx && !ret)
  112. memcpy(rx, rxbuf, len);
  113. out_free:
  114. kfree(headerbuf);
  115. kfree(txbuf);
  116. kfree(rxbuf);
  117. return ret;
  118. }
  119. static int repaper_write_buf(struct spi_device *spi, u8 reg,
  120. const u8 *buf, size_t len)
  121. {
  122. int ret;
  123. ret = repaper_spi_transfer(spi, 0x70, &reg, NULL, 1);
  124. if (ret)
  125. return ret;
  126. return repaper_spi_transfer(spi, 0x72, buf, NULL, len);
  127. }
  128. static int repaper_write_val(struct spi_device *spi, u8 reg, u8 val)
  129. {
  130. return repaper_write_buf(spi, reg, &val, 1);
  131. }
  132. static int repaper_read_val(struct spi_device *spi, u8 reg)
  133. {
  134. int ret;
  135. u8 val;
  136. ret = repaper_spi_transfer(spi, 0x70, &reg, NULL, 1);
  137. if (ret)
  138. return ret;
  139. ret = repaper_spi_transfer(spi, 0x73, NULL, &val, 1);
  140. return ret ? ret : val;
  141. }
  142. static int repaper_read_id(struct spi_device *spi)
  143. {
  144. int ret;
  145. u8 id;
  146. ret = repaper_spi_transfer(spi, 0x71, NULL, &id, 1);
  147. return ret ? ret : id;
  148. }
  149. static void repaper_spi_mosi_low(struct spi_device *spi)
  150. {
  151. const u8 buf[1] = { 0 };
  152. spi_write(spi, buf, 1);
  153. }
  154. /* pixels on display are numbered from 1 so even is actually bits 1,3,5,... */
  155. static void repaper_even_pixels(struct repaper_epd *epd, u8 **pp,
  156. const u8 *data, u8 fixed_value, const u8 *mask,
  157. enum repaper_stage stage)
  158. {
  159. unsigned int b;
  160. for (b = 0; b < (epd->width / 8); b++) {
  161. if (data) {
  162. u8 pixels = data[b] & 0xaa;
  163. u8 pixel_mask = 0xff;
  164. u8 p1, p2, p3, p4;
  165. if (mask) {
  166. pixel_mask = (mask[b] ^ pixels) & 0xaa;
  167. pixel_mask |= pixel_mask >> 1;
  168. }
  169. switch (stage) {
  170. case REPAPER_COMPENSATE: /* B -> W, W -> B (Current) */
  171. pixels = 0xaa | ((pixels ^ 0xaa) >> 1);
  172. break;
  173. case REPAPER_WHITE: /* B -> N, W -> W (Current) */
  174. pixels = 0x55 + ((pixels ^ 0xaa) >> 1);
  175. break;
  176. case REPAPER_INVERSE: /* B -> N, W -> B (New) */
  177. pixels = 0x55 | (pixels ^ 0xaa);
  178. break;
  179. case REPAPER_NORMAL: /* B -> B, W -> W (New) */
  180. pixels = 0xaa | (pixels >> 1);
  181. break;
  182. }
  183. pixels = (pixels & pixel_mask) | (~pixel_mask & 0x55);
  184. p1 = (pixels >> 6) & 0x03;
  185. p2 = (pixels >> 4) & 0x03;
  186. p3 = (pixels >> 2) & 0x03;
  187. p4 = (pixels >> 0) & 0x03;
  188. pixels = (p1 << 0) | (p2 << 2) | (p3 << 4) | (p4 << 6);
  189. *(*pp)++ = pixels;
  190. } else {
  191. *(*pp)++ = fixed_value;
  192. }
  193. }
  194. }
  195. /* pixels on display are numbered from 1 so odd is actually bits 0,2,4,... */
  196. static void repaper_odd_pixels(struct repaper_epd *epd, u8 **pp,
  197. const u8 *data, u8 fixed_value, const u8 *mask,
  198. enum repaper_stage stage)
  199. {
  200. unsigned int b;
  201. for (b = epd->width / 8; b > 0; b--) {
  202. if (data) {
  203. u8 pixels = data[b - 1] & 0x55;
  204. u8 pixel_mask = 0xff;
  205. if (mask) {
  206. pixel_mask = (mask[b - 1] ^ pixels) & 0x55;
  207. pixel_mask |= pixel_mask << 1;
  208. }
  209. switch (stage) {
  210. case REPAPER_COMPENSATE: /* B -> W, W -> B (Current) */
  211. pixels = 0xaa | (pixels ^ 0x55);
  212. break;
  213. case REPAPER_WHITE: /* B -> N, W -> W (Current) */
  214. pixels = 0x55 + (pixels ^ 0x55);
  215. break;
  216. case REPAPER_INVERSE: /* B -> N, W -> B (New) */
  217. pixels = 0x55 | ((pixels ^ 0x55) << 1);
  218. break;
  219. case REPAPER_NORMAL: /* B -> B, W -> W (New) */
  220. pixels = 0xaa | pixels;
  221. break;
  222. }
  223. pixels = (pixels & pixel_mask) | (~pixel_mask & 0x55);
  224. *(*pp)++ = pixels;
  225. } else {
  226. *(*pp)++ = fixed_value;
  227. }
  228. }
  229. }
  230. /* interleave bits: (byte)76543210 -> (16 bit).7.6.5.4.3.2.1 */
  231. static inline u16 repaper_interleave_bits(u16 value)
  232. {
  233. value = (value | (value << 4)) & 0x0f0f;
  234. value = (value | (value << 2)) & 0x3333;
  235. value = (value | (value << 1)) & 0x5555;
  236. return value;
  237. }
  238. /* pixels on display are numbered from 1 */
  239. static void repaper_all_pixels(struct repaper_epd *epd, u8 **pp,
  240. const u8 *data, u8 fixed_value, const u8 *mask,
  241. enum repaper_stage stage)
  242. {
  243. unsigned int b;
  244. for (b = epd->width / 8; b > 0; b--) {
  245. if (data) {
  246. u16 pixels = repaper_interleave_bits(data[b - 1]);
  247. u16 pixel_mask = 0xffff;
  248. if (mask) {
  249. pixel_mask = repaper_interleave_bits(mask[b - 1]);
  250. pixel_mask = (pixel_mask ^ pixels) & 0x5555;
  251. pixel_mask |= pixel_mask << 1;
  252. }
  253. switch (stage) {
  254. case REPAPER_COMPENSATE: /* B -> W, W -> B (Current) */
  255. pixels = 0xaaaa | (pixels ^ 0x5555);
  256. break;
  257. case REPAPER_WHITE: /* B -> N, W -> W (Current) */
  258. pixels = 0x5555 + (pixels ^ 0x5555);
  259. break;
  260. case REPAPER_INVERSE: /* B -> N, W -> B (New) */
  261. pixels = 0x5555 | ((pixels ^ 0x5555) << 1);
  262. break;
  263. case REPAPER_NORMAL: /* B -> B, W -> W (New) */
  264. pixels = 0xaaaa | pixels;
  265. break;
  266. }
  267. pixels = (pixels & pixel_mask) | (~pixel_mask & 0x5555);
  268. *(*pp)++ = pixels >> 8;
  269. *(*pp)++ = pixels;
  270. } else {
  271. *(*pp)++ = fixed_value;
  272. *(*pp)++ = fixed_value;
  273. }
  274. }
  275. }
  276. /* output one line of scan and data bytes to the display */
  277. static void repaper_one_line(struct repaper_epd *epd, unsigned int line,
  278. const u8 *data, u8 fixed_value, const u8 *mask,
  279. enum repaper_stage stage)
  280. {
  281. u8 *p = epd->line_buffer;
  282. unsigned int b;
  283. repaper_spi_mosi_low(epd->spi);
  284. if (epd->pre_border_byte)
  285. *p++ = 0x00;
  286. if (epd->middle_scan) {
  287. /* data bytes */
  288. repaper_odd_pixels(epd, &p, data, fixed_value, mask, stage);
  289. /* scan line */
  290. for (b = epd->bytes_per_scan; b > 0; b--) {
  291. if (line / 4 == b - 1)
  292. *p++ = 0x03 << (2 * (line & 0x03));
  293. else
  294. *p++ = 0x00;
  295. }
  296. /* data bytes */
  297. repaper_even_pixels(epd, &p, data, fixed_value, mask, stage);
  298. } else {
  299. /*
  300. * even scan line, but as lines on display are numbered from 1,
  301. * line: 1,3,5,...
  302. */
  303. for (b = 0; b < epd->bytes_per_scan; b++) {
  304. if (0 != (line & 0x01) && line / 8 == b)
  305. *p++ = 0xc0 >> (line & 0x06);
  306. else
  307. *p++ = 0x00;
  308. }
  309. /* data bytes */
  310. repaper_all_pixels(epd, &p, data, fixed_value, mask, stage);
  311. /*
  312. * odd scan line, but as lines on display are numbered from 1,
  313. * line: 0,2,4,6,...
  314. */
  315. for (b = epd->bytes_per_scan; b > 0; b--) {
  316. if (0 == (line & 0x01) && line / 8 == b - 1)
  317. *p++ = 0x03 << (line & 0x06);
  318. else
  319. *p++ = 0x00;
  320. }
  321. }
  322. switch (epd->border_byte) {
  323. case REPAPER_BORDER_BYTE_NONE:
  324. break;
  325. case REPAPER_BORDER_BYTE_ZERO:
  326. *p++ = 0x00;
  327. break;
  328. case REPAPER_BORDER_BYTE_SET:
  329. switch (stage) {
  330. case REPAPER_COMPENSATE:
  331. case REPAPER_WHITE:
  332. case REPAPER_INVERSE:
  333. *p++ = 0x00;
  334. break;
  335. case REPAPER_NORMAL:
  336. *p++ = 0xaa;
  337. break;
  338. }
  339. break;
  340. }
  341. repaper_write_buf(epd->spi, 0x0a, epd->line_buffer,
  342. p - epd->line_buffer);
  343. /* Output data to panel */
  344. repaper_write_val(epd->spi, 0x02, 0x07);
  345. repaper_spi_mosi_low(epd->spi);
  346. }
  347. static void repaper_frame_fixed(struct repaper_epd *epd, u8 fixed_value,
  348. enum repaper_stage stage)
  349. {
  350. unsigned int line;
  351. for (line = 0; line < epd->height; line++)
  352. repaper_one_line(epd, line, NULL, fixed_value, NULL, stage);
  353. }
  354. static void repaper_frame_data(struct repaper_epd *epd, const u8 *image,
  355. const u8 *mask, enum repaper_stage stage)
  356. {
  357. unsigned int line;
  358. if (!mask) {
  359. for (line = 0; line < epd->height; line++) {
  360. repaper_one_line(epd, line,
  361. &image[line * (epd->width / 8)],
  362. 0, NULL, stage);
  363. }
  364. } else {
  365. for (line = 0; line < epd->height; line++) {
  366. size_t n = line * epd->width / 8;
  367. repaper_one_line(epd, line, &image[n], 0, &mask[n],
  368. stage);
  369. }
  370. }
  371. }
  372. static void repaper_frame_fixed_repeat(struct repaper_epd *epd, u8 fixed_value,
  373. enum repaper_stage stage)
  374. {
  375. u64 start = local_clock();
  376. u64 end = start + (epd->factored_stage_time * 1000 * 1000);
  377. do {
  378. repaper_frame_fixed(epd, fixed_value, stage);
  379. } while (local_clock() < end);
  380. }
  381. static void repaper_frame_data_repeat(struct repaper_epd *epd, const u8 *image,
  382. const u8 *mask, enum repaper_stage stage)
  383. {
  384. u64 start = local_clock();
  385. u64 end = start + (epd->factored_stage_time * 1000 * 1000);
  386. do {
  387. repaper_frame_data(epd, image, mask, stage);
  388. } while (local_clock() < end);
  389. }
  390. static void repaper_get_temperature(struct repaper_epd *epd)
  391. {
  392. int ret, temperature = 0;
  393. unsigned int factor10x;
  394. if (!epd->thermal)
  395. return;
  396. ret = thermal_zone_get_temp(epd->thermal, &temperature);
  397. if (ret) {
  398. DRM_DEV_ERROR(&epd->spi->dev, "Failed to get temperature (%d)\n", ret);
  399. return;
  400. }
  401. temperature /= 1000;
  402. if (temperature <= -10)
  403. factor10x = 170;
  404. else if (temperature <= -5)
  405. factor10x = 120;
  406. else if (temperature <= 5)
  407. factor10x = 80;
  408. else if (temperature <= 10)
  409. factor10x = 40;
  410. else if (temperature <= 15)
  411. factor10x = 30;
  412. else if (temperature <= 20)
  413. factor10x = 20;
  414. else if (temperature <= 40)
  415. factor10x = 10;
  416. else
  417. factor10x = 7;
  418. epd->factored_stage_time = epd->stage_time * factor10x / 10;
  419. }
  420. static void repaper_gray8_to_mono_reversed(u8 *buf, u32 width, u32 height)
  421. {
  422. u8 *gray8 = buf, *mono = buf;
  423. int y, xb, i;
  424. for (y = 0; y < height; y++)
  425. for (xb = 0; xb < width / 8; xb++) {
  426. u8 byte = 0x00;
  427. for (i = 0; i < 8; i++) {
  428. int x = xb * 8 + i;
  429. byte >>= 1;
  430. if (gray8[y * width + x] >> 7)
  431. byte |= BIT(7);
  432. }
  433. *mono++ = byte;
  434. }
  435. }
  436. static int repaper_fb_dirty(struct drm_framebuffer *fb,
  437. struct drm_file *file_priv,
  438. unsigned int flags, unsigned int color,
  439. struct drm_clip_rect *clips,
  440. unsigned int num_clips)
  441. {
  442. struct drm_gem_cma_object *cma_obj = drm_fb_cma_get_gem_obj(fb, 0);
  443. struct dma_buf_attachment *import_attach = cma_obj->base.import_attach;
  444. struct tinydrm_device *tdev = fb->dev->dev_private;
  445. struct repaper_epd *epd = epd_from_tinydrm(tdev);
  446. struct drm_clip_rect clip;
  447. u8 *buf = NULL;
  448. int ret = 0;
  449. /* repaper can't do partial updates */
  450. clip.x1 = 0;
  451. clip.x2 = fb->width;
  452. clip.y1 = 0;
  453. clip.y2 = fb->height;
  454. if (!epd->enabled)
  455. return 0;
  456. repaper_get_temperature(epd);
  457. DRM_DEBUG("Flushing [FB:%d] st=%ums\n", fb->base.id,
  458. epd->factored_stage_time);
  459. buf = kmalloc_array(fb->width, fb->height, GFP_KERNEL);
  460. if (!buf)
  461. return -ENOMEM;
  462. if (import_attach) {
  463. ret = dma_buf_begin_cpu_access(import_attach->dmabuf,
  464. DMA_FROM_DEVICE);
  465. if (ret)
  466. goto out_free;
  467. }
  468. tinydrm_xrgb8888_to_gray8(buf, cma_obj->vaddr, fb, &clip);
  469. if (import_attach) {
  470. ret = dma_buf_end_cpu_access(import_attach->dmabuf,
  471. DMA_FROM_DEVICE);
  472. if (ret)
  473. goto out_free;
  474. }
  475. repaper_gray8_to_mono_reversed(buf, fb->width, fb->height);
  476. if (epd->partial) {
  477. repaper_frame_data_repeat(epd, buf, epd->current_frame,
  478. REPAPER_NORMAL);
  479. } else if (epd->cleared) {
  480. repaper_frame_data_repeat(epd, epd->current_frame, NULL,
  481. REPAPER_COMPENSATE);
  482. repaper_frame_data_repeat(epd, epd->current_frame, NULL,
  483. REPAPER_WHITE);
  484. repaper_frame_data_repeat(epd, buf, NULL, REPAPER_INVERSE);
  485. repaper_frame_data_repeat(epd, buf, NULL, REPAPER_NORMAL);
  486. epd->partial = true;
  487. } else {
  488. /* Clear display (anything -> white) */
  489. repaper_frame_fixed_repeat(epd, 0xff, REPAPER_COMPENSATE);
  490. repaper_frame_fixed_repeat(epd, 0xff, REPAPER_WHITE);
  491. repaper_frame_fixed_repeat(epd, 0xaa, REPAPER_INVERSE);
  492. repaper_frame_fixed_repeat(epd, 0xaa, REPAPER_NORMAL);
  493. /* Assuming a clear (white) screen output an image */
  494. repaper_frame_fixed_repeat(epd, 0xaa, REPAPER_COMPENSATE);
  495. repaper_frame_fixed_repeat(epd, 0xaa, REPAPER_WHITE);
  496. repaper_frame_data_repeat(epd, buf, NULL, REPAPER_INVERSE);
  497. repaper_frame_data_repeat(epd, buf, NULL, REPAPER_NORMAL);
  498. epd->cleared = true;
  499. epd->partial = true;
  500. }
  501. memcpy(epd->current_frame, buf, fb->width * fb->height / 8);
  502. /*
  503. * An extra frame write is needed if pixels are set in the bottom line,
  504. * or else grey lines rises up from the pixels
  505. */
  506. if (epd->pre_border_byte) {
  507. unsigned int x;
  508. for (x = 0; x < (fb->width / 8); x++)
  509. if (buf[x + (fb->width * (fb->height - 1) / 8)]) {
  510. repaper_frame_data_repeat(epd, buf,
  511. epd->current_frame,
  512. REPAPER_NORMAL);
  513. break;
  514. }
  515. }
  516. out_free:
  517. kfree(buf);
  518. return ret;
  519. }
  520. static const struct drm_framebuffer_funcs repaper_fb_funcs = {
  521. .destroy = drm_gem_fb_destroy,
  522. .create_handle = drm_gem_fb_create_handle,
  523. .dirty = tinydrm_fb_dirty,
  524. };
  525. static void power_off(struct repaper_epd *epd)
  526. {
  527. /* Turn off power and all signals */
  528. gpiod_set_value_cansleep(epd->reset, 0);
  529. gpiod_set_value_cansleep(epd->panel_on, 0);
  530. if (epd->border)
  531. gpiod_set_value_cansleep(epd->border, 0);
  532. /* Ensure SPI MOSI and CLOCK are Low before CS Low */
  533. repaper_spi_mosi_low(epd->spi);
  534. /* Discharge pulse */
  535. gpiod_set_value_cansleep(epd->discharge, 1);
  536. msleep(150);
  537. gpiod_set_value_cansleep(epd->discharge, 0);
  538. }
  539. static void repaper_pipe_enable(struct drm_simple_display_pipe *pipe,
  540. struct drm_crtc_state *crtc_state,
  541. struct drm_plane_state *plane_state)
  542. {
  543. struct tinydrm_device *tdev = pipe_to_tinydrm(pipe);
  544. struct repaper_epd *epd = epd_from_tinydrm(tdev);
  545. struct spi_device *spi = epd->spi;
  546. struct device *dev = &spi->dev;
  547. bool dc_ok = false;
  548. int i, ret;
  549. DRM_DEBUG_DRIVER("\n");
  550. /* Power up sequence */
  551. gpiod_set_value_cansleep(epd->reset, 0);
  552. gpiod_set_value_cansleep(epd->panel_on, 0);
  553. gpiod_set_value_cansleep(epd->discharge, 0);
  554. if (epd->border)
  555. gpiod_set_value_cansleep(epd->border, 0);
  556. repaper_spi_mosi_low(spi);
  557. usleep_range(5000, 10000);
  558. gpiod_set_value_cansleep(epd->panel_on, 1);
  559. /*
  560. * This delay comes from the repaper.org userspace driver, it's not
  561. * mentioned in the datasheet.
  562. */
  563. usleep_range(10000, 15000);
  564. gpiod_set_value_cansleep(epd->reset, 1);
  565. if (epd->border)
  566. gpiod_set_value_cansleep(epd->border, 1);
  567. usleep_range(5000, 10000);
  568. gpiod_set_value_cansleep(epd->reset, 0);
  569. usleep_range(5000, 10000);
  570. gpiod_set_value_cansleep(epd->reset, 1);
  571. usleep_range(5000, 10000);
  572. /* Wait for COG to become ready */
  573. for (i = 100; i > 0; i--) {
  574. if (!gpiod_get_value_cansleep(epd->busy))
  575. break;
  576. usleep_range(10, 100);
  577. }
  578. if (!i) {
  579. DRM_DEV_ERROR(dev, "timeout waiting for panel to become ready.\n");
  580. power_off(epd);
  581. return;
  582. }
  583. repaper_read_id(spi);
  584. ret = repaper_read_id(spi);
  585. if (ret != REPAPER_RID_G2_COG_ID) {
  586. if (ret < 0)
  587. dev_err(dev, "failed to read chip (%d)\n", ret);
  588. else
  589. dev_err(dev, "wrong COG ID 0x%02x\n", ret);
  590. power_off(epd);
  591. return;
  592. }
  593. /* Disable OE */
  594. repaper_write_val(spi, 0x02, 0x40);
  595. ret = repaper_read_val(spi, 0x0f);
  596. if (ret < 0 || !(ret & 0x80)) {
  597. if (ret < 0)
  598. DRM_DEV_ERROR(dev, "failed to read chip (%d)\n", ret);
  599. else
  600. DRM_DEV_ERROR(dev, "panel is reported broken\n");
  601. power_off(epd);
  602. return;
  603. }
  604. /* Power saving mode */
  605. repaper_write_val(spi, 0x0b, 0x02);
  606. /* Channel select */
  607. repaper_write_buf(spi, 0x01, epd->channel_select, 8);
  608. /* High power mode osc */
  609. repaper_write_val(spi, 0x07, 0xd1);
  610. /* Power setting */
  611. repaper_write_val(spi, 0x08, 0x02);
  612. /* Vcom level */
  613. repaper_write_val(spi, 0x09, 0xc2);
  614. /* Power setting */
  615. repaper_write_val(spi, 0x04, 0x03);
  616. /* Driver latch on */
  617. repaper_write_val(spi, 0x03, 0x01);
  618. /* Driver latch off */
  619. repaper_write_val(spi, 0x03, 0x00);
  620. usleep_range(5000, 10000);
  621. /* Start chargepump */
  622. for (i = 0; i < 4; ++i) {
  623. /* Charge pump positive voltage on - VGH/VDL on */
  624. repaper_write_val(spi, 0x05, 0x01);
  625. msleep(240);
  626. /* Charge pump negative voltage on - VGL/VDL on */
  627. repaper_write_val(spi, 0x05, 0x03);
  628. msleep(40);
  629. /* Charge pump Vcom on - Vcom driver on */
  630. repaper_write_val(spi, 0x05, 0x0f);
  631. msleep(40);
  632. /* check DC/DC */
  633. ret = repaper_read_val(spi, 0x0f);
  634. if (ret < 0) {
  635. DRM_DEV_ERROR(dev, "failed to read chip (%d)\n", ret);
  636. power_off(epd);
  637. return;
  638. }
  639. if (ret & 0x40) {
  640. dc_ok = true;
  641. break;
  642. }
  643. }
  644. if (!dc_ok) {
  645. DRM_DEV_ERROR(dev, "dc/dc failed\n");
  646. power_off(epd);
  647. return;
  648. }
  649. /*
  650. * Output enable to disable
  651. * The userspace driver sets this to 0x04, but the datasheet says 0x06
  652. */
  653. repaper_write_val(spi, 0x02, 0x04);
  654. epd->enabled = true;
  655. epd->partial = false;
  656. }
  657. static void repaper_pipe_disable(struct drm_simple_display_pipe *pipe)
  658. {
  659. struct tinydrm_device *tdev = pipe_to_tinydrm(pipe);
  660. struct repaper_epd *epd = epd_from_tinydrm(tdev);
  661. struct spi_device *spi = epd->spi;
  662. unsigned int line;
  663. DRM_DEBUG_DRIVER("\n");
  664. mutex_lock(&tdev->dirty_lock);
  665. epd->enabled = false;
  666. mutex_unlock(&tdev->dirty_lock);
  667. /* Nothing frame */
  668. for (line = 0; line < epd->height; line++)
  669. repaper_one_line(epd, 0x7fffu, NULL, 0x00, NULL,
  670. REPAPER_COMPENSATE);
  671. /* 2.7" */
  672. if (epd->border) {
  673. /* Dummy line */
  674. repaper_one_line(epd, 0x7fffu, NULL, 0x00, NULL,
  675. REPAPER_COMPENSATE);
  676. msleep(25);
  677. gpiod_set_value_cansleep(epd->border, 0);
  678. msleep(200);
  679. gpiod_set_value_cansleep(epd->border, 1);
  680. } else {
  681. /* Border dummy line */
  682. repaper_one_line(epd, 0x7fffu, NULL, 0x00, NULL,
  683. REPAPER_NORMAL);
  684. msleep(200);
  685. }
  686. /* not described in datasheet */
  687. repaper_write_val(spi, 0x0b, 0x00);
  688. /* Latch reset turn on */
  689. repaper_write_val(spi, 0x03, 0x01);
  690. /* Power off charge pump Vcom */
  691. repaper_write_val(spi, 0x05, 0x03);
  692. /* Power off charge pump neg voltage */
  693. repaper_write_val(spi, 0x05, 0x01);
  694. msleep(120);
  695. /* Discharge internal */
  696. repaper_write_val(spi, 0x04, 0x80);
  697. /* turn off all charge pumps */
  698. repaper_write_val(spi, 0x05, 0x00);
  699. /* Turn off osc */
  700. repaper_write_val(spi, 0x07, 0x01);
  701. msleep(50);
  702. power_off(epd);
  703. }
  704. static const struct drm_simple_display_pipe_funcs repaper_pipe_funcs = {
  705. .enable = repaper_pipe_enable,
  706. .disable = repaper_pipe_disable,
  707. .update = tinydrm_display_pipe_update,
  708. .prepare_fb = drm_gem_fb_simple_display_pipe_prepare_fb,
  709. };
  710. static const uint32_t repaper_formats[] = {
  711. DRM_FORMAT_XRGB8888,
  712. };
  713. static const struct drm_display_mode repaper_e1144cs021_mode = {
  714. TINYDRM_MODE(128, 96, 29, 22),
  715. };
  716. static const u8 repaper_e1144cs021_cs[] = { 0x00, 0x00, 0x00, 0x00,
  717. 0x00, 0x0f, 0xff, 0x00 };
  718. static const struct drm_display_mode repaper_e1190cs021_mode = {
  719. TINYDRM_MODE(144, 128, 36, 32),
  720. };
  721. static const u8 repaper_e1190cs021_cs[] = { 0x00, 0x00, 0x00, 0x03,
  722. 0xfc, 0x00, 0x00, 0xff };
  723. static const struct drm_display_mode repaper_e2200cs021_mode = {
  724. TINYDRM_MODE(200, 96, 46, 22),
  725. };
  726. static const u8 repaper_e2200cs021_cs[] = { 0x00, 0x00, 0x00, 0x00,
  727. 0x01, 0xff, 0xe0, 0x00 };
  728. static const struct drm_display_mode repaper_e2271cs021_mode = {
  729. TINYDRM_MODE(264, 176, 57, 38),
  730. };
  731. static const u8 repaper_e2271cs021_cs[] = { 0x00, 0x00, 0x00, 0x7f,
  732. 0xff, 0xfe, 0x00, 0x00 };
  733. DEFINE_DRM_GEM_CMA_FOPS(repaper_fops);
  734. static struct drm_driver repaper_driver = {
  735. .driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_PRIME |
  736. DRIVER_ATOMIC,
  737. .fops = &repaper_fops,
  738. TINYDRM_GEM_DRIVER_OPS,
  739. .name = "repaper",
  740. .desc = "Pervasive Displays RePaper e-ink panels",
  741. .date = "20170405",
  742. .major = 1,
  743. .minor = 0,
  744. };
  745. static const struct of_device_id repaper_of_match[] = {
  746. { .compatible = "pervasive,e1144cs021", .data = (void *)E1144CS021 },
  747. { .compatible = "pervasive,e1190cs021", .data = (void *)E1190CS021 },
  748. { .compatible = "pervasive,e2200cs021", .data = (void *)E2200CS021 },
  749. { .compatible = "pervasive,e2271cs021", .data = (void *)E2271CS021 },
  750. {},
  751. };
  752. MODULE_DEVICE_TABLE(of, repaper_of_match);
  753. static const struct spi_device_id repaper_id[] = {
  754. { "e1144cs021", E1144CS021 },
  755. { "e1190cs021", E1190CS021 },
  756. { "e2200cs021", E2200CS021 },
  757. { "e2271cs021", E2271CS021 },
  758. { },
  759. };
  760. MODULE_DEVICE_TABLE(spi, repaper_id);
  761. static int repaper_probe(struct spi_device *spi)
  762. {
  763. const struct drm_display_mode *mode;
  764. const struct spi_device_id *spi_id;
  765. const struct of_device_id *match;
  766. struct device *dev = &spi->dev;
  767. struct tinydrm_device *tdev;
  768. enum repaper_model model;
  769. const char *thermal_zone;
  770. struct repaper_epd *epd;
  771. size_t line_buffer_size;
  772. int ret;
  773. match = of_match_device(repaper_of_match, dev);
  774. if (match) {
  775. model = (enum repaper_model)match->data;
  776. } else {
  777. spi_id = spi_get_device_id(spi);
  778. model = spi_id->driver_data;
  779. }
  780. /* The SPI device is used to allocate dma memory */
  781. if (!dev->coherent_dma_mask) {
  782. ret = dma_coerce_mask_and_coherent(dev, DMA_BIT_MASK(32));
  783. if (ret) {
  784. dev_warn(dev, "Failed to set dma mask %d\n", ret);
  785. return ret;
  786. }
  787. }
  788. epd = devm_kzalloc(dev, sizeof(*epd), GFP_KERNEL);
  789. if (!epd)
  790. return -ENOMEM;
  791. epd->spi = spi;
  792. epd->panel_on = devm_gpiod_get(dev, "panel-on", GPIOD_OUT_LOW);
  793. if (IS_ERR(epd->panel_on)) {
  794. ret = PTR_ERR(epd->panel_on);
  795. if (ret != -EPROBE_DEFER)
  796. DRM_DEV_ERROR(dev, "Failed to get gpio 'panel-on'\n");
  797. return ret;
  798. }
  799. epd->discharge = devm_gpiod_get(dev, "discharge", GPIOD_OUT_LOW);
  800. if (IS_ERR(epd->discharge)) {
  801. ret = PTR_ERR(epd->discharge);
  802. if (ret != -EPROBE_DEFER)
  803. DRM_DEV_ERROR(dev, "Failed to get gpio 'discharge'\n");
  804. return ret;
  805. }
  806. epd->reset = devm_gpiod_get(dev, "reset", GPIOD_OUT_LOW);
  807. if (IS_ERR(epd->reset)) {
  808. ret = PTR_ERR(epd->reset);
  809. if (ret != -EPROBE_DEFER)
  810. DRM_DEV_ERROR(dev, "Failed to get gpio 'reset'\n");
  811. return ret;
  812. }
  813. epd->busy = devm_gpiod_get(dev, "busy", GPIOD_IN);
  814. if (IS_ERR(epd->busy)) {
  815. ret = PTR_ERR(epd->busy);
  816. if (ret != -EPROBE_DEFER)
  817. DRM_DEV_ERROR(dev, "Failed to get gpio 'busy'\n");
  818. return ret;
  819. }
  820. if (!device_property_read_string(dev, "pervasive,thermal-zone",
  821. &thermal_zone)) {
  822. epd->thermal = thermal_zone_get_zone_by_name(thermal_zone);
  823. if (IS_ERR(epd->thermal)) {
  824. DRM_DEV_ERROR(dev, "Failed to get thermal zone: %s\n", thermal_zone);
  825. return PTR_ERR(epd->thermal);
  826. }
  827. }
  828. switch (model) {
  829. case E1144CS021:
  830. mode = &repaper_e1144cs021_mode;
  831. epd->channel_select = repaper_e1144cs021_cs;
  832. epd->stage_time = 480;
  833. epd->bytes_per_scan = 96 / 4;
  834. epd->middle_scan = true; /* data-scan-data */
  835. epd->pre_border_byte = false;
  836. epd->border_byte = REPAPER_BORDER_BYTE_ZERO;
  837. break;
  838. case E1190CS021:
  839. mode = &repaper_e1190cs021_mode;
  840. epd->channel_select = repaper_e1190cs021_cs;
  841. epd->stage_time = 480;
  842. epd->bytes_per_scan = 128 / 4 / 2;
  843. epd->middle_scan = false; /* scan-data-scan */
  844. epd->pre_border_byte = false;
  845. epd->border_byte = REPAPER_BORDER_BYTE_SET;
  846. break;
  847. case E2200CS021:
  848. mode = &repaper_e2200cs021_mode;
  849. epd->channel_select = repaper_e2200cs021_cs;
  850. epd->stage_time = 480;
  851. epd->bytes_per_scan = 96 / 4;
  852. epd->middle_scan = true; /* data-scan-data */
  853. epd->pre_border_byte = true;
  854. epd->border_byte = REPAPER_BORDER_BYTE_NONE;
  855. break;
  856. case E2271CS021:
  857. epd->border = devm_gpiod_get(dev, "border", GPIOD_OUT_LOW);
  858. if (IS_ERR(epd->border)) {
  859. ret = PTR_ERR(epd->border);
  860. if (ret != -EPROBE_DEFER)
  861. DRM_DEV_ERROR(dev, "Failed to get gpio 'border'\n");
  862. return ret;
  863. }
  864. mode = &repaper_e2271cs021_mode;
  865. epd->channel_select = repaper_e2271cs021_cs;
  866. epd->stage_time = 630;
  867. epd->bytes_per_scan = 176 / 4;
  868. epd->middle_scan = true; /* data-scan-data */
  869. epd->pre_border_byte = true;
  870. epd->border_byte = REPAPER_BORDER_BYTE_NONE;
  871. break;
  872. default:
  873. return -ENODEV;
  874. }
  875. epd->width = mode->hdisplay;
  876. epd->height = mode->vdisplay;
  877. epd->factored_stage_time = epd->stage_time;
  878. line_buffer_size = 2 * epd->width / 8 + epd->bytes_per_scan + 2;
  879. epd->line_buffer = devm_kzalloc(dev, line_buffer_size, GFP_KERNEL);
  880. if (!epd->line_buffer)
  881. return -ENOMEM;
  882. epd->current_frame = devm_kzalloc(dev, epd->width * epd->height / 8,
  883. GFP_KERNEL);
  884. if (!epd->current_frame)
  885. return -ENOMEM;
  886. tdev = &epd->tinydrm;
  887. ret = devm_tinydrm_init(dev, tdev, &repaper_fb_funcs, &repaper_driver);
  888. if (ret)
  889. return ret;
  890. tdev->fb_dirty = repaper_fb_dirty;
  891. ret = tinydrm_display_pipe_init(tdev, &repaper_pipe_funcs,
  892. DRM_MODE_CONNECTOR_VIRTUAL,
  893. repaper_formats,
  894. ARRAY_SIZE(repaper_formats), mode, 0);
  895. if (ret)
  896. return ret;
  897. drm_mode_config_reset(tdev->drm);
  898. spi_set_drvdata(spi, tdev);
  899. DRM_DEBUG_DRIVER("SPI speed: %uMHz\n", spi->max_speed_hz / 1000000);
  900. return devm_tinydrm_register(tdev);
  901. }
  902. static void repaper_shutdown(struct spi_device *spi)
  903. {
  904. struct tinydrm_device *tdev = spi_get_drvdata(spi);
  905. tinydrm_shutdown(tdev);
  906. }
  907. static struct spi_driver repaper_spi_driver = {
  908. .driver = {
  909. .name = "repaper",
  910. .owner = THIS_MODULE,
  911. .of_match_table = repaper_of_match,
  912. },
  913. .id_table = repaper_id,
  914. .probe = repaper_probe,
  915. .shutdown = repaper_shutdown,
  916. };
  917. module_spi_driver(repaper_spi_driver);
  918. MODULE_DESCRIPTION("Pervasive Displays RePaper DRM driver");
  919. MODULE_AUTHOR("Noralf Trønnes");
  920. MODULE_LICENSE("GPL");