rcar-csi2.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Driver for Renesas R-Car MIPI CSI-2 Receiver
  4. *
  5. * Copyright (C) 2018 Renesas Electronics Corp.
  6. */
  7. #include <linux/delay.h>
  8. #include <linux/interrupt.h>
  9. #include <linux/io.h>
  10. #include <linux/module.h>
  11. #include <linux/of.h>
  12. #include <linux/of_device.h>
  13. #include <linux/of_graph.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/pm_runtime.h>
  16. #include <linux/sys_soc.h>
  17. #include <media/v4l2-ctrls.h>
  18. #include <media/v4l2-device.h>
  19. #include <media/v4l2-fwnode.h>
  20. #include <media/v4l2-mc.h>
  21. #include <media/v4l2-subdev.h>
  22. struct rcar_csi2;
  23. /* Register offsets and bits */
  24. /* Control Timing Select */
  25. #define TREF_REG 0x00
  26. #define TREF_TREF BIT(0)
  27. /* Software Reset */
  28. #define SRST_REG 0x04
  29. #define SRST_SRST BIT(0)
  30. /* PHY Operation Control */
  31. #define PHYCNT_REG 0x08
  32. #define PHYCNT_SHUTDOWNZ BIT(17)
  33. #define PHYCNT_RSTZ BIT(16)
  34. #define PHYCNT_ENABLECLK BIT(4)
  35. #define PHYCNT_ENABLE_3 BIT(3)
  36. #define PHYCNT_ENABLE_2 BIT(2)
  37. #define PHYCNT_ENABLE_1 BIT(1)
  38. #define PHYCNT_ENABLE_0 BIT(0)
  39. /* Checksum Control */
  40. #define CHKSUM_REG 0x0c
  41. #define CHKSUM_ECC_EN BIT(1)
  42. #define CHKSUM_CRC_EN BIT(0)
  43. /*
  44. * Channel Data Type Select
  45. * VCDT[0-15]: Channel 1 VCDT[16-31]: Channel 2
  46. * VCDT2[0-15]: Channel 3 VCDT2[16-31]: Channel 4
  47. */
  48. #define VCDT_REG 0x10
  49. #define VCDT2_REG 0x14
  50. #define VCDT_VCDTN_EN BIT(15)
  51. #define VCDT_SEL_VC(n) (((n) & 0x3) << 8)
  52. #define VCDT_SEL_DTN_ON BIT(6)
  53. #define VCDT_SEL_DT(n) (((n) & 0x3f) << 0)
  54. /* Frame Data Type Select */
  55. #define FRDT_REG 0x18
  56. /* Field Detection Control */
  57. #define FLD_REG 0x1c
  58. #define FLD_FLD_NUM(n) (((n) & 0xff) << 16)
  59. #define FLD_FLD_EN4 BIT(3)
  60. #define FLD_FLD_EN3 BIT(2)
  61. #define FLD_FLD_EN2 BIT(1)
  62. #define FLD_FLD_EN BIT(0)
  63. /* Automatic Standby Control */
  64. #define ASTBY_REG 0x20
  65. /* Long Data Type Setting 0 */
  66. #define LNGDT0_REG 0x28
  67. /* Long Data Type Setting 1 */
  68. #define LNGDT1_REG 0x2c
  69. /* Interrupt Enable */
  70. #define INTEN_REG 0x30
  71. /* Interrupt Source Mask */
  72. #define INTCLOSE_REG 0x34
  73. /* Interrupt Status Monitor */
  74. #define INTSTATE_REG 0x38
  75. #define INTSTATE_INT_ULPS_START BIT(7)
  76. #define INTSTATE_INT_ULPS_END BIT(6)
  77. /* Interrupt Error Status Monitor */
  78. #define INTERRSTATE_REG 0x3c
  79. /* Short Packet Data */
  80. #define SHPDAT_REG 0x40
  81. /* Short Packet Count */
  82. #define SHPCNT_REG 0x44
  83. /* LINK Operation Control */
  84. #define LINKCNT_REG 0x48
  85. #define LINKCNT_MONITOR_EN BIT(31)
  86. #define LINKCNT_REG_MONI_PACT_EN BIT(25)
  87. #define LINKCNT_ICLK_NONSTOP BIT(24)
  88. /* Lane Swap */
  89. #define LSWAP_REG 0x4c
  90. #define LSWAP_L3SEL(n) (((n) & 0x3) << 6)
  91. #define LSWAP_L2SEL(n) (((n) & 0x3) << 4)
  92. #define LSWAP_L1SEL(n) (((n) & 0x3) << 2)
  93. #define LSWAP_L0SEL(n) (((n) & 0x3) << 0)
  94. /* PHY Test Interface Write Register */
  95. #define PHTW_REG 0x50
  96. #define PHTW_DWEN BIT(24)
  97. #define PHTW_TESTDIN_DATA(n) (((n & 0xff)) << 16)
  98. #define PHTW_CWEN BIT(8)
  99. #define PHTW_TESTDIN_CODE(n) ((n & 0xff))
  100. struct phtw_value {
  101. u16 data;
  102. u16 code;
  103. };
  104. struct rcsi2_mbps_reg {
  105. u16 mbps;
  106. u16 reg;
  107. };
  108. static const struct rcsi2_mbps_reg phtw_mbps_h3_v3h_m3n[] = {
  109. { .mbps = 80, .reg = 0x86 },
  110. { .mbps = 90, .reg = 0x86 },
  111. { .mbps = 100, .reg = 0x87 },
  112. { .mbps = 110, .reg = 0x87 },
  113. { .mbps = 120, .reg = 0x88 },
  114. { .mbps = 130, .reg = 0x88 },
  115. { .mbps = 140, .reg = 0x89 },
  116. { .mbps = 150, .reg = 0x89 },
  117. { .mbps = 160, .reg = 0x8a },
  118. { .mbps = 170, .reg = 0x8a },
  119. { .mbps = 180, .reg = 0x8b },
  120. { .mbps = 190, .reg = 0x8b },
  121. { .mbps = 205, .reg = 0x8c },
  122. { .mbps = 220, .reg = 0x8d },
  123. { .mbps = 235, .reg = 0x8e },
  124. { .mbps = 250, .reg = 0x8e },
  125. { /* sentinel */ },
  126. };
  127. static const struct rcsi2_mbps_reg phtw_mbps_v3m_e3[] = {
  128. { .mbps = 80, .reg = 0x00 },
  129. { .mbps = 90, .reg = 0x20 },
  130. { .mbps = 100, .reg = 0x40 },
  131. { .mbps = 110, .reg = 0x02 },
  132. { .mbps = 130, .reg = 0x22 },
  133. { .mbps = 140, .reg = 0x42 },
  134. { .mbps = 150, .reg = 0x04 },
  135. { .mbps = 170, .reg = 0x24 },
  136. { .mbps = 180, .reg = 0x44 },
  137. { .mbps = 200, .reg = 0x06 },
  138. { .mbps = 220, .reg = 0x26 },
  139. { .mbps = 240, .reg = 0x46 },
  140. { .mbps = 250, .reg = 0x08 },
  141. { .mbps = 270, .reg = 0x28 },
  142. { .mbps = 300, .reg = 0x0a },
  143. { .mbps = 330, .reg = 0x2a },
  144. { .mbps = 360, .reg = 0x4a },
  145. { .mbps = 400, .reg = 0x0c },
  146. { .mbps = 450, .reg = 0x2c },
  147. { .mbps = 500, .reg = 0x0e },
  148. { .mbps = 550, .reg = 0x2e },
  149. { .mbps = 600, .reg = 0x10 },
  150. { .mbps = 650, .reg = 0x30 },
  151. { .mbps = 700, .reg = 0x12 },
  152. { .mbps = 750, .reg = 0x32 },
  153. { .mbps = 800, .reg = 0x52 },
  154. { .mbps = 850, .reg = 0x72 },
  155. { .mbps = 900, .reg = 0x14 },
  156. { .mbps = 950, .reg = 0x34 },
  157. { .mbps = 1000, .reg = 0x54 },
  158. { .mbps = 1050, .reg = 0x74 },
  159. { .mbps = 1125, .reg = 0x16 },
  160. { /* sentinel */ },
  161. };
  162. /* PHY Test Interface Clear */
  163. #define PHTC_REG 0x58
  164. #define PHTC_TESTCLR BIT(0)
  165. /* PHY Frequency Control */
  166. #define PHYPLL_REG 0x68
  167. #define PHYPLL_HSFREQRANGE(n) ((n) << 16)
  168. static const struct rcsi2_mbps_reg hsfreqrange_h3_v3h_m3n[] = {
  169. { .mbps = 80, .reg = 0x00 },
  170. { .mbps = 90, .reg = 0x10 },
  171. { .mbps = 100, .reg = 0x20 },
  172. { .mbps = 110, .reg = 0x30 },
  173. { .mbps = 120, .reg = 0x01 },
  174. { .mbps = 130, .reg = 0x11 },
  175. { .mbps = 140, .reg = 0x21 },
  176. { .mbps = 150, .reg = 0x31 },
  177. { .mbps = 160, .reg = 0x02 },
  178. { .mbps = 170, .reg = 0x12 },
  179. { .mbps = 180, .reg = 0x22 },
  180. { .mbps = 190, .reg = 0x32 },
  181. { .mbps = 205, .reg = 0x03 },
  182. { .mbps = 220, .reg = 0x13 },
  183. { .mbps = 235, .reg = 0x23 },
  184. { .mbps = 250, .reg = 0x33 },
  185. { .mbps = 275, .reg = 0x04 },
  186. { .mbps = 300, .reg = 0x14 },
  187. { .mbps = 325, .reg = 0x25 },
  188. { .mbps = 350, .reg = 0x35 },
  189. { .mbps = 400, .reg = 0x05 },
  190. { .mbps = 450, .reg = 0x16 },
  191. { .mbps = 500, .reg = 0x26 },
  192. { .mbps = 550, .reg = 0x37 },
  193. { .mbps = 600, .reg = 0x07 },
  194. { .mbps = 650, .reg = 0x18 },
  195. { .mbps = 700, .reg = 0x28 },
  196. { .mbps = 750, .reg = 0x39 },
  197. { .mbps = 800, .reg = 0x09 },
  198. { .mbps = 850, .reg = 0x19 },
  199. { .mbps = 900, .reg = 0x29 },
  200. { .mbps = 950, .reg = 0x3a },
  201. { .mbps = 1000, .reg = 0x0a },
  202. { .mbps = 1050, .reg = 0x1a },
  203. { .mbps = 1100, .reg = 0x2a },
  204. { .mbps = 1150, .reg = 0x3b },
  205. { .mbps = 1200, .reg = 0x0b },
  206. { .mbps = 1250, .reg = 0x1b },
  207. { .mbps = 1300, .reg = 0x2b },
  208. { .mbps = 1350, .reg = 0x3c },
  209. { .mbps = 1400, .reg = 0x0c },
  210. { .mbps = 1450, .reg = 0x1c },
  211. { .mbps = 1500, .reg = 0x2c },
  212. { /* sentinel */ },
  213. };
  214. static const struct rcsi2_mbps_reg hsfreqrange_m3w_h3es1[] = {
  215. { .mbps = 80, .reg = 0x00 },
  216. { .mbps = 90, .reg = 0x10 },
  217. { .mbps = 100, .reg = 0x20 },
  218. { .mbps = 110, .reg = 0x30 },
  219. { .mbps = 120, .reg = 0x01 },
  220. { .mbps = 130, .reg = 0x11 },
  221. { .mbps = 140, .reg = 0x21 },
  222. { .mbps = 150, .reg = 0x31 },
  223. { .mbps = 160, .reg = 0x02 },
  224. { .mbps = 170, .reg = 0x12 },
  225. { .mbps = 180, .reg = 0x22 },
  226. { .mbps = 190, .reg = 0x32 },
  227. { .mbps = 205, .reg = 0x03 },
  228. { .mbps = 220, .reg = 0x13 },
  229. { .mbps = 235, .reg = 0x23 },
  230. { .mbps = 250, .reg = 0x33 },
  231. { .mbps = 275, .reg = 0x04 },
  232. { .mbps = 300, .reg = 0x14 },
  233. { .mbps = 325, .reg = 0x05 },
  234. { .mbps = 350, .reg = 0x15 },
  235. { .mbps = 400, .reg = 0x25 },
  236. { .mbps = 450, .reg = 0x06 },
  237. { .mbps = 500, .reg = 0x16 },
  238. { .mbps = 550, .reg = 0x07 },
  239. { .mbps = 600, .reg = 0x17 },
  240. { .mbps = 650, .reg = 0x08 },
  241. { .mbps = 700, .reg = 0x18 },
  242. { .mbps = 750, .reg = 0x09 },
  243. { .mbps = 800, .reg = 0x19 },
  244. { .mbps = 850, .reg = 0x29 },
  245. { .mbps = 900, .reg = 0x39 },
  246. { .mbps = 950, .reg = 0x0a },
  247. { .mbps = 1000, .reg = 0x1a },
  248. { .mbps = 1050, .reg = 0x2a },
  249. { .mbps = 1100, .reg = 0x3a },
  250. { .mbps = 1150, .reg = 0x0b },
  251. { .mbps = 1200, .reg = 0x1b },
  252. { .mbps = 1250, .reg = 0x2b },
  253. { .mbps = 1300, .reg = 0x3b },
  254. { .mbps = 1350, .reg = 0x0c },
  255. { .mbps = 1400, .reg = 0x1c },
  256. { .mbps = 1450, .reg = 0x2c },
  257. { .mbps = 1500, .reg = 0x3c },
  258. { /* sentinel */ },
  259. };
  260. /* PHY ESC Error Monitor */
  261. #define PHEERM_REG 0x74
  262. /* PHY Clock Lane Monitor */
  263. #define PHCLM_REG 0x78
  264. #define PHCLM_STOPSTATECKL BIT(0)
  265. /* PHY Data Lane Monitor */
  266. #define PHDLM_REG 0x7c
  267. /* CSI0CLK Frequency Configuration Preset Register */
  268. #define CSI0CLKFCPR_REG 0x260
  269. #define CSI0CLKFREQRANGE(n) ((n & 0x3f) << 16)
  270. struct rcar_csi2_format {
  271. u32 code;
  272. unsigned int datatype;
  273. unsigned int bpp;
  274. };
  275. static const struct rcar_csi2_format rcar_csi2_formats[] = {
  276. { .code = MEDIA_BUS_FMT_RGB888_1X24, .datatype = 0x24, .bpp = 24 },
  277. { .code = MEDIA_BUS_FMT_UYVY8_1X16, .datatype = 0x1e, .bpp = 16 },
  278. { .code = MEDIA_BUS_FMT_YUYV8_1X16, .datatype = 0x1e, .bpp = 16 },
  279. { .code = MEDIA_BUS_FMT_UYVY8_2X8, .datatype = 0x1e, .bpp = 16 },
  280. { .code = MEDIA_BUS_FMT_YUYV10_2X10, .datatype = 0x1e, .bpp = 20 },
  281. };
  282. static const struct rcar_csi2_format *rcsi2_code_to_fmt(unsigned int code)
  283. {
  284. unsigned int i;
  285. for (i = 0; i < ARRAY_SIZE(rcar_csi2_formats); i++)
  286. if (rcar_csi2_formats[i].code == code)
  287. return &rcar_csi2_formats[i];
  288. return NULL;
  289. }
  290. enum rcar_csi2_pads {
  291. RCAR_CSI2_SINK,
  292. RCAR_CSI2_SOURCE_VC0,
  293. RCAR_CSI2_SOURCE_VC1,
  294. RCAR_CSI2_SOURCE_VC2,
  295. RCAR_CSI2_SOURCE_VC3,
  296. NR_OF_RCAR_CSI2_PAD,
  297. };
  298. struct rcar_csi2_info {
  299. int (*init_phtw)(struct rcar_csi2 *priv, unsigned int mbps);
  300. int (*confirm_start)(struct rcar_csi2 *priv);
  301. const struct rcsi2_mbps_reg *hsfreqrange;
  302. unsigned int csi0clkfreqrange;
  303. bool clear_ulps;
  304. };
  305. struct rcar_csi2 {
  306. struct device *dev;
  307. void __iomem *base;
  308. const struct rcar_csi2_info *info;
  309. struct v4l2_subdev subdev;
  310. struct media_pad pads[NR_OF_RCAR_CSI2_PAD];
  311. struct v4l2_async_notifier notifier;
  312. struct v4l2_async_subdev asd;
  313. struct v4l2_subdev *remote;
  314. struct v4l2_mbus_framefmt mf;
  315. struct mutex lock;
  316. int stream_count;
  317. unsigned short lanes;
  318. unsigned char lane_swap[4];
  319. };
  320. static inline struct rcar_csi2 *sd_to_csi2(struct v4l2_subdev *sd)
  321. {
  322. return container_of(sd, struct rcar_csi2, subdev);
  323. }
  324. static inline struct rcar_csi2 *notifier_to_csi2(struct v4l2_async_notifier *n)
  325. {
  326. return container_of(n, struct rcar_csi2, notifier);
  327. }
  328. static u32 rcsi2_read(struct rcar_csi2 *priv, unsigned int reg)
  329. {
  330. return ioread32(priv->base + reg);
  331. }
  332. static void rcsi2_write(struct rcar_csi2 *priv, unsigned int reg, u32 data)
  333. {
  334. iowrite32(data, priv->base + reg);
  335. }
  336. static void rcsi2_reset(struct rcar_csi2 *priv)
  337. {
  338. rcsi2_write(priv, SRST_REG, SRST_SRST);
  339. usleep_range(100, 150);
  340. rcsi2_write(priv, SRST_REG, 0);
  341. }
  342. static int rcsi2_wait_phy_start(struct rcar_csi2 *priv)
  343. {
  344. unsigned int timeout;
  345. /* Wait for the clock and data lanes to enter LP-11 state. */
  346. for (timeout = 0; timeout <= 20; timeout++) {
  347. const u32 lane_mask = (1 << priv->lanes) - 1;
  348. if ((rcsi2_read(priv, PHCLM_REG) & PHCLM_STOPSTATECKL) &&
  349. (rcsi2_read(priv, PHDLM_REG) & lane_mask) == lane_mask)
  350. return 0;
  351. usleep_range(1000, 2000);
  352. }
  353. dev_err(priv->dev, "Timeout waiting for LP-11 state\n");
  354. return -ETIMEDOUT;
  355. }
  356. static int rcsi2_set_phypll(struct rcar_csi2 *priv, unsigned int mbps)
  357. {
  358. const struct rcsi2_mbps_reg *hsfreq;
  359. for (hsfreq = priv->info->hsfreqrange; hsfreq->mbps != 0; hsfreq++)
  360. if (hsfreq->mbps >= mbps)
  361. break;
  362. if (!hsfreq->mbps) {
  363. dev_err(priv->dev, "Unsupported PHY speed (%u Mbps)", mbps);
  364. return -ERANGE;
  365. }
  366. rcsi2_write(priv, PHYPLL_REG, PHYPLL_HSFREQRANGE(hsfreq->reg));
  367. return 0;
  368. }
  369. static int rcsi2_calc_mbps(struct rcar_csi2 *priv, unsigned int bpp)
  370. {
  371. struct v4l2_subdev *source;
  372. struct v4l2_ctrl *ctrl;
  373. u64 mbps;
  374. if (!priv->remote)
  375. return -ENODEV;
  376. source = priv->remote;
  377. /* Read the pixel rate control from remote. */
  378. ctrl = v4l2_ctrl_find(source->ctrl_handler, V4L2_CID_PIXEL_RATE);
  379. if (!ctrl) {
  380. dev_err(priv->dev, "no pixel rate control in subdev %s\n",
  381. source->name);
  382. return -EINVAL;
  383. }
  384. /*
  385. * Calculate the phypll in mbps.
  386. * link_freq = (pixel_rate * bits_per_sample) / (2 * nr_of_lanes)
  387. * bps = link_freq * 2
  388. */
  389. mbps = v4l2_ctrl_g_ctrl_int64(ctrl) * bpp;
  390. do_div(mbps, priv->lanes * 1000000);
  391. return mbps;
  392. }
  393. static int rcsi2_start(struct rcar_csi2 *priv)
  394. {
  395. const struct rcar_csi2_format *format;
  396. u32 phycnt, vcdt = 0, vcdt2 = 0;
  397. unsigned int i;
  398. int mbps, ret;
  399. dev_dbg(priv->dev, "Input size (%ux%u%c)\n",
  400. priv->mf.width, priv->mf.height,
  401. priv->mf.field == V4L2_FIELD_NONE ? 'p' : 'i');
  402. /* Code is validated in set_fmt. */
  403. format = rcsi2_code_to_fmt(priv->mf.code);
  404. /*
  405. * Enable all Virtual Channels.
  406. *
  407. * NOTE: It's not possible to get individual datatype for each
  408. * source virtual channel. Once this is possible in V4L2
  409. * it should be used here.
  410. */
  411. for (i = 0; i < 4; i++) {
  412. u32 vcdt_part;
  413. vcdt_part = VCDT_SEL_VC(i) | VCDT_VCDTN_EN | VCDT_SEL_DTN_ON |
  414. VCDT_SEL_DT(format->datatype);
  415. /* Store in correct reg and offset. */
  416. if (i < 2)
  417. vcdt |= vcdt_part << ((i % 2) * 16);
  418. else
  419. vcdt2 |= vcdt_part << ((i % 2) * 16);
  420. }
  421. phycnt = PHYCNT_ENABLECLK;
  422. phycnt |= (1 << priv->lanes) - 1;
  423. mbps = rcsi2_calc_mbps(priv, format->bpp);
  424. if (mbps < 0)
  425. return mbps;
  426. /* Init */
  427. rcsi2_write(priv, TREF_REG, TREF_TREF);
  428. rcsi2_reset(priv);
  429. rcsi2_write(priv, PHTC_REG, 0);
  430. /* Configure */
  431. rcsi2_write(priv, FLD_REG, FLD_FLD_NUM(2) | FLD_FLD_EN4 |
  432. FLD_FLD_EN3 | FLD_FLD_EN2 | FLD_FLD_EN);
  433. rcsi2_write(priv, VCDT_REG, vcdt);
  434. rcsi2_write(priv, VCDT2_REG, vcdt2);
  435. /* Lanes are zero indexed. */
  436. rcsi2_write(priv, LSWAP_REG,
  437. LSWAP_L0SEL(priv->lane_swap[0] - 1) |
  438. LSWAP_L1SEL(priv->lane_swap[1] - 1) |
  439. LSWAP_L2SEL(priv->lane_swap[2] - 1) |
  440. LSWAP_L3SEL(priv->lane_swap[3] - 1));
  441. /* Start */
  442. if (priv->info->init_phtw) {
  443. ret = priv->info->init_phtw(priv, mbps);
  444. if (ret)
  445. return ret;
  446. }
  447. if (priv->info->hsfreqrange) {
  448. ret = rcsi2_set_phypll(priv, mbps);
  449. if (ret)
  450. return ret;
  451. }
  452. if (priv->info->csi0clkfreqrange)
  453. rcsi2_write(priv, CSI0CLKFCPR_REG,
  454. CSI0CLKFREQRANGE(priv->info->csi0clkfreqrange));
  455. rcsi2_write(priv, PHYCNT_REG, phycnt);
  456. rcsi2_write(priv, LINKCNT_REG, LINKCNT_MONITOR_EN |
  457. LINKCNT_REG_MONI_PACT_EN | LINKCNT_ICLK_NONSTOP);
  458. rcsi2_write(priv, PHYCNT_REG, phycnt | PHYCNT_SHUTDOWNZ);
  459. rcsi2_write(priv, PHYCNT_REG, phycnt | PHYCNT_SHUTDOWNZ | PHYCNT_RSTZ);
  460. ret = rcsi2_wait_phy_start(priv);
  461. if (ret)
  462. return ret;
  463. /* Confirm start */
  464. if (priv->info->confirm_start) {
  465. ret = priv->info->confirm_start(priv);
  466. if (ret)
  467. return ret;
  468. }
  469. /* Clear Ultra Low Power interrupt. */
  470. if (priv->info->clear_ulps)
  471. rcsi2_write(priv, INTSTATE_REG,
  472. INTSTATE_INT_ULPS_START |
  473. INTSTATE_INT_ULPS_END);
  474. return 0;
  475. }
  476. static void rcsi2_stop(struct rcar_csi2 *priv)
  477. {
  478. rcsi2_write(priv, PHYCNT_REG, 0);
  479. rcsi2_reset(priv);
  480. rcsi2_write(priv, PHTC_REG, PHTC_TESTCLR);
  481. }
  482. static int rcsi2_s_stream(struct v4l2_subdev *sd, int enable)
  483. {
  484. struct rcar_csi2 *priv = sd_to_csi2(sd);
  485. struct v4l2_subdev *nextsd;
  486. int ret = 0;
  487. mutex_lock(&priv->lock);
  488. if (!priv->remote) {
  489. ret = -ENODEV;
  490. goto out;
  491. }
  492. nextsd = priv->remote;
  493. if (enable && priv->stream_count == 0) {
  494. pm_runtime_get_sync(priv->dev);
  495. ret = rcsi2_start(priv);
  496. if (ret) {
  497. pm_runtime_put(priv->dev);
  498. goto out;
  499. }
  500. ret = v4l2_subdev_call(nextsd, video, s_stream, 1);
  501. if (ret) {
  502. rcsi2_stop(priv);
  503. pm_runtime_put(priv->dev);
  504. goto out;
  505. }
  506. } else if (!enable && priv->stream_count == 1) {
  507. rcsi2_stop(priv);
  508. v4l2_subdev_call(nextsd, video, s_stream, 0);
  509. pm_runtime_put(priv->dev);
  510. }
  511. priv->stream_count += enable ? 1 : -1;
  512. out:
  513. mutex_unlock(&priv->lock);
  514. return ret;
  515. }
  516. static int rcsi2_set_pad_format(struct v4l2_subdev *sd,
  517. struct v4l2_subdev_pad_config *cfg,
  518. struct v4l2_subdev_format *format)
  519. {
  520. struct rcar_csi2 *priv = sd_to_csi2(sd);
  521. struct v4l2_mbus_framefmt *framefmt;
  522. if (!rcsi2_code_to_fmt(format->format.code))
  523. format->format.code = rcar_csi2_formats[0].code;
  524. if (format->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
  525. priv->mf = format->format;
  526. } else {
  527. framefmt = v4l2_subdev_get_try_format(sd, cfg, 0);
  528. *framefmt = format->format;
  529. }
  530. return 0;
  531. }
  532. static int rcsi2_get_pad_format(struct v4l2_subdev *sd,
  533. struct v4l2_subdev_pad_config *cfg,
  534. struct v4l2_subdev_format *format)
  535. {
  536. struct rcar_csi2 *priv = sd_to_csi2(sd);
  537. if (format->which == V4L2_SUBDEV_FORMAT_ACTIVE)
  538. format->format = priv->mf;
  539. else
  540. format->format = *v4l2_subdev_get_try_format(sd, cfg, 0);
  541. return 0;
  542. }
  543. static const struct v4l2_subdev_video_ops rcar_csi2_video_ops = {
  544. .s_stream = rcsi2_s_stream,
  545. };
  546. static const struct v4l2_subdev_pad_ops rcar_csi2_pad_ops = {
  547. .set_fmt = rcsi2_set_pad_format,
  548. .get_fmt = rcsi2_get_pad_format,
  549. };
  550. static const struct v4l2_subdev_ops rcar_csi2_subdev_ops = {
  551. .video = &rcar_csi2_video_ops,
  552. .pad = &rcar_csi2_pad_ops,
  553. };
  554. /* -----------------------------------------------------------------------------
  555. * Async handling and registration of subdevices and links.
  556. */
  557. static int rcsi2_notify_bound(struct v4l2_async_notifier *notifier,
  558. struct v4l2_subdev *subdev,
  559. struct v4l2_async_subdev *asd)
  560. {
  561. struct rcar_csi2 *priv = notifier_to_csi2(notifier);
  562. int pad;
  563. pad = media_entity_get_fwnode_pad(&subdev->entity, asd->match.fwnode,
  564. MEDIA_PAD_FL_SOURCE);
  565. if (pad < 0) {
  566. dev_err(priv->dev, "Failed to find pad for %s\n", subdev->name);
  567. return pad;
  568. }
  569. priv->remote = subdev;
  570. dev_dbg(priv->dev, "Bound %s pad: %d\n", subdev->name, pad);
  571. return media_create_pad_link(&subdev->entity, pad,
  572. &priv->subdev.entity, 0,
  573. MEDIA_LNK_FL_ENABLED |
  574. MEDIA_LNK_FL_IMMUTABLE);
  575. }
  576. static void rcsi2_notify_unbind(struct v4l2_async_notifier *notifier,
  577. struct v4l2_subdev *subdev,
  578. struct v4l2_async_subdev *asd)
  579. {
  580. struct rcar_csi2 *priv = notifier_to_csi2(notifier);
  581. priv->remote = NULL;
  582. dev_dbg(priv->dev, "Unbind %s\n", subdev->name);
  583. }
  584. static const struct v4l2_async_notifier_operations rcar_csi2_notify_ops = {
  585. .bound = rcsi2_notify_bound,
  586. .unbind = rcsi2_notify_unbind,
  587. };
  588. static int rcsi2_parse_v4l2(struct rcar_csi2 *priv,
  589. struct v4l2_fwnode_endpoint *vep)
  590. {
  591. unsigned int i;
  592. /* Only port 0 endpoint 0 is valid. */
  593. if (vep->base.port || vep->base.id)
  594. return -ENOTCONN;
  595. if (vep->bus_type != V4L2_MBUS_CSI2) {
  596. dev_err(priv->dev, "Unsupported bus: %u\n", vep->bus_type);
  597. return -EINVAL;
  598. }
  599. priv->lanes = vep->bus.mipi_csi2.num_data_lanes;
  600. if (priv->lanes != 1 && priv->lanes != 2 && priv->lanes != 4) {
  601. dev_err(priv->dev, "Unsupported number of data-lanes: %u\n",
  602. priv->lanes);
  603. return -EINVAL;
  604. }
  605. for (i = 0; i < ARRAY_SIZE(priv->lane_swap); i++) {
  606. priv->lane_swap[i] = i < priv->lanes ?
  607. vep->bus.mipi_csi2.data_lanes[i] : i;
  608. /* Check for valid lane number. */
  609. if (priv->lane_swap[i] < 1 || priv->lane_swap[i] > 4) {
  610. dev_err(priv->dev, "data-lanes must be in 1-4 range\n");
  611. return -EINVAL;
  612. }
  613. }
  614. return 0;
  615. }
  616. static int rcsi2_parse_dt(struct rcar_csi2 *priv)
  617. {
  618. struct device_node *ep;
  619. struct v4l2_fwnode_endpoint v4l2_ep;
  620. int ret;
  621. ep = of_graph_get_endpoint_by_regs(priv->dev->of_node, 0, 0);
  622. if (!ep) {
  623. dev_err(priv->dev, "Not connected to subdevice\n");
  624. return -EINVAL;
  625. }
  626. ret = v4l2_fwnode_endpoint_parse(of_fwnode_handle(ep), &v4l2_ep);
  627. if (ret) {
  628. dev_err(priv->dev, "Could not parse v4l2 endpoint\n");
  629. of_node_put(ep);
  630. return -EINVAL;
  631. }
  632. ret = rcsi2_parse_v4l2(priv, &v4l2_ep);
  633. if (ret) {
  634. of_node_put(ep);
  635. return ret;
  636. }
  637. priv->asd.match.fwnode =
  638. fwnode_graph_get_remote_endpoint(of_fwnode_handle(ep));
  639. priv->asd.match_type = V4L2_ASYNC_MATCH_FWNODE;
  640. of_node_put(ep);
  641. priv->notifier.subdevs = devm_kzalloc(priv->dev,
  642. sizeof(*priv->notifier.subdevs),
  643. GFP_KERNEL);
  644. if (!priv->notifier.subdevs)
  645. return -ENOMEM;
  646. priv->notifier.num_subdevs = 1;
  647. priv->notifier.subdevs[0] = &priv->asd;
  648. priv->notifier.ops = &rcar_csi2_notify_ops;
  649. dev_dbg(priv->dev, "Found '%pOF'\n",
  650. to_of_node(priv->asd.match.fwnode));
  651. return v4l2_async_subdev_notifier_register(&priv->subdev,
  652. &priv->notifier);
  653. }
  654. /* -----------------------------------------------------------------------------
  655. * PHTW initialization sequences.
  656. *
  657. * NOTE: Magic values are from the datasheet and lack documentation.
  658. */
  659. static int rcsi2_phtw_write(struct rcar_csi2 *priv, u16 data, u16 code)
  660. {
  661. unsigned int timeout;
  662. rcsi2_write(priv, PHTW_REG,
  663. PHTW_DWEN | PHTW_TESTDIN_DATA(data) |
  664. PHTW_CWEN | PHTW_TESTDIN_CODE(code));
  665. /* Wait for DWEN and CWEN to be cleared by hardware. */
  666. for (timeout = 0; timeout <= 20; timeout++) {
  667. if (!(rcsi2_read(priv, PHTW_REG) & (PHTW_DWEN | PHTW_CWEN)))
  668. return 0;
  669. usleep_range(1000, 2000);
  670. }
  671. dev_err(priv->dev, "Timeout waiting for PHTW_DWEN and/or PHTW_CWEN\n");
  672. return -ETIMEDOUT;
  673. }
  674. static int rcsi2_phtw_write_array(struct rcar_csi2 *priv,
  675. const struct phtw_value *values)
  676. {
  677. const struct phtw_value *value;
  678. int ret;
  679. for (value = values; value->data || value->code; value++) {
  680. ret = rcsi2_phtw_write(priv, value->data, value->code);
  681. if (ret)
  682. return ret;
  683. }
  684. return 0;
  685. }
  686. static int rcsi2_phtw_write_mbps(struct rcar_csi2 *priv, unsigned int mbps,
  687. const struct rcsi2_mbps_reg *values, u16 code)
  688. {
  689. const struct rcsi2_mbps_reg *value;
  690. for (value = values; value->mbps; value++)
  691. if (value->mbps >= mbps)
  692. break;
  693. if (!value->mbps) {
  694. dev_err(priv->dev, "Unsupported PHY speed (%u Mbps)", mbps);
  695. return -ERANGE;
  696. }
  697. return rcsi2_phtw_write(priv, value->reg, code);
  698. }
  699. static int rcsi2_init_phtw_h3_v3h_m3n(struct rcar_csi2 *priv, unsigned int mbps)
  700. {
  701. static const struct phtw_value step1[] = {
  702. { .data = 0xcc, .code = 0xe2 },
  703. { .data = 0x01, .code = 0xe3 },
  704. { .data = 0x11, .code = 0xe4 },
  705. { .data = 0x01, .code = 0xe5 },
  706. { .data = 0x10, .code = 0x04 },
  707. { /* sentinel */ },
  708. };
  709. static const struct phtw_value step2[] = {
  710. { .data = 0x38, .code = 0x08 },
  711. { .data = 0x01, .code = 0x00 },
  712. { .data = 0x4b, .code = 0xac },
  713. { .data = 0x03, .code = 0x00 },
  714. { .data = 0x80, .code = 0x07 },
  715. { /* sentinel */ },
  716. };
  717. int ret;
  718. ret = rcsi2_phtw_write_array(priv, step1);
  719. if (ret)
  720. return ret;
  721. if (mbps <= 250) {
  722. ret = rcsi2_phtw_write(priv, 0x39, 0x05);
  723. if (ret)
  724. return ret;
  725. ret = rcsi2_phtw_write_mbps(priv, mbps, phtw_mbps_h3_v3h_m3n,
  726. 0xf1);
  727. if (ret)
  728. return ret;
  729. }
  730. return rcsi2_phtw_write_array(priv, step2);
  731. }
  732. static int rcsi2_init_phtw_v3m_e3(struct rcar_csi2 *priv, unsigned int mbps)
  733. {
  734. return rcsi2_phtw_write_mbps(priv, mbps, phtw_mbps_v3m_e3, 0x44);
  735. }
  736. static int rcsi2_confirm_start_v3m_e3(struct rcar_csi2 *priv)
  737. {
  738. static const struct phtw_value step1[] = {
  739. { .data = 0xed, .code = 0x34 },
  740. { .data = 0xed, .code = 0x44 },
  741. { .data = 0xed, .code = 0x54 },
  742. { .data = 0xed, .code = 0x84 },
  743. { .data = 0xed, .code = 0x94 },
  744. { /* sentinel */ },
  745. };
  746. return rcsi2_phtw_write_array(priv, step1);
  747. }
  748. /* -----------------------------------------------------------------------------
  749. * Platform Device Driver.
  750. */
  751. static const struct media_entity_operations rcar_csi2_entity_ops = {
  752. .link_validate = v4l2_subdev_link_validate,
  753. };
  754. static int rcsi2_probe_resources(struct rcar_csi2 *priv,
  755. struct platform_device *pdev)
  756. {
  757. struct resource *res;
  758. int irq;
  759. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  760. priv->base = devm_ioremap_resource(&pdev->dev, res);
  761. if (IS_ERR(priv->base))
  762. return PTR_ERR(priv->base);
  763. irq = platform_get_irq(pdev, 0);
  764. if (irq < 0)
  765. return irq;
  766. return 0;
  767. }
  768. static const struct rcar_csi2_info rcar_csi2_info_r8a7795 = {
  769. .init_phtw = rcsi2_init_phtw_h3_v3h_m3n,
  770. .hsfreqrange = hsfreqrange_h3_v3h_m3n,
  771. .csi0clkfreqrange = 0x20,
  772. .clear_ulps = true,
  773. };
  774. static const struct rcar_csi2_info rcar_csi2_info_r8a7795es1 = {
  775. .hsfreqrange = hsfreqrange_m3w_h3es1,
  776. };
  777. static const struct rcar_csi2_info rcar_csi2_info_r8a7796 = {
  778. .hsfreqrange = hsfreqrange_m3w_h3es1,
  779. };
  780. static const struct rcar_csi2_info rcar_csi2_info_r8a77965 = {
  781. .init_phtw = rcsi2_init_phtw_h3_v3h_m3n,
  782. .hsfreqrange = hsfreqrange_h3_v3h_m3n,
  783. .csi0clkfreqrange = 0x20,
  784. .clear_ulps = true,
  785. };
  786. static const struct rcar_csi2_info rcar_csi2_info_r8a77970 = {
  787. .init_phtw = rcsi2_init_phtw_v3m_e3,
  788. .confirm_start = rcsi2_confirm_start_v3m_e3,
  789. };
  790. static const struct of_device_id rcar_csi2_of_table[] = {
  791. {
  792. .compatible = "renesas,r8a7795-csi2",
  793. .data = &rcar_csi2_info_r8a7795,
  794. },
  795. {
  796. .compatible = "renesas,r8a7796-csi2",
  797. .data = &rcar_csi2_info_r8a7796,
  798. },
  799. {
  800. .compatible = "renesas,r8a77965-csi2",
  801. .data = &rcar_csi2_info_r8a77965,
  802. },
  803. {
  804. .compatible = "renesas,r8a77970-csi2",
  805. .data = &rcar_csi2_info_r8a77970,
  806. },
  807. { /* sentinel */ },
  808. };
  809. MODULE_DEVICE_TABLE(of, rcar_csi2_of_table);
  810. static const struct soc_device_attribute r8a7795es1[] = {
  811. {
  812. .soc_id = "r8a7795", .revision = "ES1.*",
  813. .data = &rcar_csi2_info_r8a7795es1,
  814. },
  815. { /* sentinel */ },
  816. };
  817. static int rcsi2_probe(struct platform_device *pdev)
  818. {
  819. const struct soc_device_attribute *attr;
  820. struct rcar_csi2 *priv;
  821. unsigned int i;
  822. int ret;
  823. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  824. if (!priv)
  825. return -ENOMEM;
  826. priv->info = of_device_get_match_data(&pdev->dev);
  827. /*
  828. * r8a7795 ES1.x behaves differently than the ES2.0+ but doesn't
  829. * have it's own compatible string.
  830. */
  831. attr = soc_device_match(r8a7795es1);
  832. if (attr)
  833. priv->info = attr->data;
  834. priv->dev = &pdev->dev;
  835. mutex_init(&priv->lock);
  836. priv->stream_count = 0;
  837. ret = rcsi2_probe_resources(priv, pdev);
  838. if (ret) {
  839. dev_err(priv->dev, "Failed to get resources\n");
  840. return ret;
  841. }
  842. platform_set_drvdata(pdev, priv);
  843. ret = rcsi2_parse_dt(priv);
  844. if (ret)
  845. return ret;
  846. priv->subdev.owner = THIS_MODULE;
  847. priv->subdev.dev = &pdev->dev;
  848. v4l2_subdev_init(&priv->subdev, &rcar_csi2_subdev_ops);
  849. v4l2_set_subdevdata(&priv->subdev, &pdev->dev);
  850. snprintf(priv->subdev.name, V4L2_SUBDEV_NAME_SIZE, "%s %s",
  851. KBUILD_MODNAME, dev_name(&pdev->dev));
  852. priv->subdev.flags = V4L2_SUBDEV_FL_HAS_DEVNODE;
  853. priv->subdev.entity.function = MEDIA_ENT_F_PROC_VIDEO_PIXEL_FORMATTER;
  854. priv->subdev.entity.ops = &rcar_csi2_entity_ops;
  855. priv->pads[RCAR_CSI2_SINK].flags = MEDIA_PAD_FL_SINK;
  856. for (i = RCAR_CSI2_SOURCE_VC0; i < NR_OF_RCAR_CSI2_PAD; i++)
  857. priv->pads[i].flags = MEDIA_PAD_FL_SOURCE;
  858. ret = media_entity_pads_init(&priv->subdev.entity, NR_OF_RCAR_CSI2_PAD,
  859. priv->pads);
  860. if (ret)
  861. goto error;
  862. pm_runtime_enable(&pdev->dev);
  863. ret = v4l2_async_register_subdev(&priv->subdev);
  864. if (ret < 0)
  865. goto error;
  866. dev_info(priv->dev, "%d lanes found\n", priv->lanes);
  867. return 0;
  868. error:
  869. v4l2_async_notifier_unregister(&priv->notifier);
  870. v4l2_async_notifier_cleanup(&priv->notifier);
  871. return ret;
  872. }
  873. static int rcsi2_remove(struct platform_device *pdev)
  874. {
  875. struct rcar_csi2 *priv = platform_get_drvdata(pdev);
  876. v4l2_async_notifier_unregister(&priv->notifier);
  877. v4l2_async_notifier_cleanup(&priv->notifier);
  878. v4l2_async_unregister_subdev(&priv->subdev);
  879. pm_runtime_disable(&pdev->dev);
  880. return 0;
  881. }
  882. static struct platform_driver rcar_csi2_pdrv = {
  883. .remove = rcsi2_remove,
  884. .probe = rcsi2_probe,
  885. .driver = {
  886. .name = "rcar-csi2",
  887. .of_match_table = rcar_csi2_of_table,
  888. },
  889. };
  890. module_platform_driver(rcar_csi2_pdrv);
  891. MODULE_AUTHOR("Niklas Söderlund <niklas.soderlund@ragnatech.se>");
  892. MODULE_DESCRIPTION("Renesas R-Car MIPI CSI-2 receiver driver");
  893. MODULE_LICENSE("GPL");