mipi-csis.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042
  1. /*
  2. * Samsung S5P/EXYNOS SoC series MIPI-CSI receiver driver
  3. *
  4. * Copyright (C) 2011 - 2013 Samsung Electronics Co., Ltd.
  5. * Author: Sylwester Nawrocki <s.nawrocki@samsung.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/clk.h>
  12. #include <linux/delay.h>
  13. #include <linux/device.h>
  14. #include <linux/errno.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/io.h>
  17. #include <linux/irq.h>
  18. #include <linux/kernel.h>
  19. #include <linux/memory.h>
  20. #include <linux/module.h>
  21. #include <linux/of.h>
  22. #include <linux/of_graph.h>
  23. #include <linux/phy/phy.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/pm_runtime.h>
  26. #include <linux/regulator/consumer.h>
  27. #include <linux/sizes.h>
  28. #include <linux/slab.h>
  29. #include <linux/spinlock.h>
  30. #include <linux/videodev2.h>
  31. #include <media/drv-intf/exynos-fimc.h>
  32. #include <media/v4l2-fwnode.h>
  33. #include <media/v4l2-subdev.h>
  34. #include "mipi-csis.h"
  35. static int debug;
  36. module_param(debug, int, 0644);
  37. MODULE_PARM_DESC(debug, "Debug level (0-2)");
  38. /* Register map definition */
  39. /* CSIS global control */
  40. #define S5PCSIS_CTRL 0x00
  41. #define S5PCSIS_CTRL_DPDN_DEFAULT (0 << 31)
  42. #define S5PCSIS_CTRL_DPDN_SWAP (1 << 31)
  43. #define S5PCSIS_CTRL_ALIGN_32BIT (1 << 20)
  44. #define S5PCSIS_CTRL_UPDATE_SHADOW (1 << 16)
  45. #define S5PCSIS_CTRL_WCLK_EXTCLK (1 << 8)
  46. #define S5PCSIS_CTRL_RESET (1 << 4)
  47. #define S5PCSIS_CTRL_ENABLE (1 << 0)
  48. /* D-PHY control */
  49. #define S5PCSIS_DPHYCTRL 0x04
  50. #define S5PCSIS_DPHYCTRL_HSS_MASK (0x1f << 27)
  51. #define S5PCSIS_DPHYCTRL_ENABLE (0x1f << 0)
  52. #define S5PCSIS_CONFIG 0x08
  53. #define S5PCSIS_CFG_FMT_YCBCR422_8BIT (0x1e << 2)
  54. #define S5PCSIS_CFG_FMT_RAW8 (0x2a << 2)
  55. #define S5PCSIS_CFG_FMT_RAW10 (0x2b << 2)
  56. #define S5PCSIS_CFG_FMT_RAW12 (0x2c << 2)
  57. /* User defined formats, x = 1...4 */
  58. #define S5PCSIS_CFG_FMT_USER(x) ((0x30 + x - 1) << 2)
  59. #define S5PCSIS_CFG_FMT_MASK (0x3f << 2)
  60. #define S5PCSIS_CFG_NR_LANE_MASK 3
  61. /* Interrupt mask */
  62. #define S5PCSIS_INTMSK 0x10
  63. #define S5PCSIS_INTMSK_EVEN_BEFORE (1 << 31)
  64. #define S5PCSIS_INTMSK_EVEN_AFTER (1 << 30)
  65. #define S5PCSIS_INTMSK_ODD_BEFORE (1 << 29)
  66. #define S5PCSIS_INTMSK_ODD_AFTER (1 << 28)
  67. #define S5PCSIS_INTMSK_FRAME_START (1 << 27)
  68. #define S5PCSIS_INTMSK_FRAME_END (1 << 26)
  69. #define S5PCSIS_INTMSK_ERR_SOT_HS (1 << 12)
  70. #define S5PCSIS_INTMSK_ERR_LOST_FS (1 << 5)
  71. #define S5PCSIS_INTMSK_ERR_LOST_FE (1 << 4)
  72. #define S5PCSIS_INTMSK_ERR_OVER (1 << 3)
  73. #define S5PCSIS_INTMSK_ERR_ECC (1 << 2)
  74. #define S5PCSIS_INTMSK_ERR_CRC (1 << 1)
  75. #define S5PCSIS_INTMSK_ERR_UNKNOWN (1 << 0)
  76. #define S5PCSIS_INTMSK_EXYNOS4_EN_ALL 0xf000103f
  77. #define S5PCSIS_INTMSK_EXYNOS5_EN_ALL 0xfc00103f
  78. /* Interrupt source */
  79. #define S5PCSIS_INTSRC 0x14
  80. #define S5PCSIS_INTSRC_EVEN_BEFORE (1 << 31)
  81. #define S5PCSIS_INTSRC_EVEN_AFTER (1 << 30)
  82. #define S5PCSIS_INTSRC_EVEN (0x3 << 30)
  83. #define S5PCSIS_INTSRC_ODD_BEFORE (1 << 29)
  84. #define S5PCSIS_INTSRC_ODD_AFTER (1 << 28)
  85. #define S5PCSIS_INTSRC_ODD (0x3 << 28)
  86. #define S5PCSIS_INTSRC_NON_IMAGE_DATA (0xf << 28)
  87. #define S5PCSIS_INTSRC_FRAME_START (1 << 27)
  88. #define S5PCSIS_INTSRC_FRAME_END (1 << 26)
  89. #define S5PCSIS_INTSRC_ERR_SOT_HS (0xf << 12)
  90. #define S5PCSIS_INTSRC_ERR_LOST_FS (1 << 5)
  91. #define S5PCSIS_INTSRC_ERR_LOST_FE (1 << 4)
  92. #define S5PCSIS_INTSRC_ERR_OVER (1 << 3)
  93. #define S5PCSIS_INTSRC_ERR_ECC (1 << 2)
  94. #define S5PCSIS_INTSRC_ERR_CRC (1 << 1)
  95. #define S5PCSIS_INTSRC_ERR_UNKNOWN (1 << 0)
  96. #define S5PCSIS_INTSRC_ERRORS 0xf03f
  97. /* Pixel resolution */
  98. #define S5PCSIS_RESOL 0x2c
  99. #define CSIS_MAX_PIX_WIDTH 0xffff
  100. #define CSIS_MAX_PIX_HEIGHT 0xffff
  101. /* Non-image packet data buffers */
  102. #define S5PCSIS_PKTDATA_ODD 0x2000
  103. #define S5PCSIS_PKTDATA_EVEN 0x3000
  104. #define S5PCSIS_PKTDATA_SIZE SZ_4K
  105. enum {
  106. CSIS_CLK_MUX,
  107. CSIS_CLK_GATE,
  108. };
  109. static char *csi_clock_name[] = {
  110. [CSIS_CLK_MUX] = "sclk_csis",
  111. [CSIS_CLK_GATE] = "csis",
  112. };
  113. #define NUM_CSIS_CLOCKS ARRAY_SIZE(csi_clock_name)
  114. #define DEFAULT_SCLK_CSIS_FREQ 166000000UL
  115. static const char * const csis_supply_name[] = {
  116. "vddcore", /* CSIS Core (1.0V, 1.1V or 1.2V) suppply */
  117. "vddio", /* CSIS I/O and PLL (1.8V) supply */
  118. };
  119. #define CSIS_NUM_SUPPLIES ARRAY_SIZE(csis_supply_name)
  120. enum {
  121. ST_POWERED = 1,
  122. ST_STREAMING = 2,
  123. ST_SUSPENDED = 4,
  124. };
  125. struct s5pcsis_event {
  126. u32 mask;
  127. const char * const name;
  128. unsigned int counter;
  129. };
  130. static const struct s5pcsis_event s5pcsis_events[] = {
  131. /* Errors */
  132. { S5PCSIS_INTSRC_ERR_SOT_HS, "SOT Error" },
  133. { S5PCSIS_INTSRC_ERR_LOST_FS, "Lost Frame Start Error" },
  134. { S5PCSIS_INTSRC_ERR_LOST_FE, "Lost Frame End Error" },
  135. { S5PCSIS_INTSRC_ERR_OVER, "FIFO Overflow Error" },
  136. { S5PCSIS_INTSRC_ERR_ECC, "ECC Error" },
  137. { S5PCSIS_INTSRC_ERR_CRC, "CRC Error" },
  138. { S5PCSIS_INTSRC_ERR_UNKNOWN, "Unknown Error" },
  139. /* Non-image data receive events */
  140. { S5PCSIS_INTSRC_EVEN_BEFORE, "Non-image data before even frame" },
  141. { S5PCSIS_INTSRC_EVEN_AFTER, "Non-image data after even frame" },
  142. { S5PCSIS_INTSRC_ODD_BEFORE, "Non-image data before odd frame" },
  143. { S5PCSIS_INTSRC_ODD_AFTER, "Non-image data after odd frame" },
  144. /* Frame start/end */
  145. { S5PCSIS_INTSRC_FRAME_START, "Frame Start" },
  146. { S5PCSIS_INTSRC_FRAME_END, "Frame End" },
  147. };
  148. #define S5PCSIS_NUM_EVENTS ARRAY_SIZE(s5pcsis_events)
  149. struct csis_pktbuf {
  150. u32 *data;
  151. unsigned int len;
  152. };
  153. struct csis_drvdata {
  154. /* Mask of all used interrupts in S5PCSIS_INTMSK register */
  155. u32 interrupt_mask;
  156. };
  157. /**
  158. * struct csis_state - the driver's internal state data structure
  159. * @lock: mutex serializing the subdev and power management operations,
  160. * protecting @format and @flags members
  161. * @pads: CSIS pads array
  162. * @sd: v4l2_subdev associated with CSIS device instance
  163. * @index: the hardware instance index
  164. * @pdev: CSIS platform device
  165. * @phy: pointer to the CSIS generic PHY
  166. * @regs: mmaped I/O registers memory
  167. * @supplies: CSIS regulator supplies
  168. * @clock: CSIS clocks
  169. * @irq: requested s5p-mipi-csis irq number
  170. * @interrupt_mask: interrupt mask of the all used interrupts
  171. * @flags: the state variable for power and streaming control
  172. * @clk_frequency: device bus clock frequency
  173. * @hs_settle: HS-RX settle time
  174. * @num_lanes: number of MIPI-CSI data lanes used
  175. * @max_num_lanes: maximum number of MIPI-CSI data lanes supported
  176. * @wclk_ext: CSI wrapper clock: 0 - bus clock, 1 - external SCLK_CAM
  177. * @csis_fmt: current CSIS pixel format
  178. * @format: common media bus format for the source and sink pad
  179. * @slock: spinlock protecting structure members below
  180. * @pkt_buf: the frame embedded (non-image) data buffer
  181. * @events: MIPI-CSIS event (error) counters
  182. */
  183. struct csis_state {
  184. struct mutex lock;
  185. struct media_pad pads[CSIS_PADS_NUM];
  186. struct v4l2_subdev sd;
  187. u8 index;
  188. struct platform_device *pdev;
  189. struct phy *phy;
  190. void __iomem *regs;
  191. struct regulator_bulk_data supplies[CSIS_NUM_SUPPLIES];
  192. struct clk *clock[NUM_CSIS_CLOCKS];
  193. int irq;
  194. u32 interrupt_mask;
  195. u32 flags;
  196. u32 clk_frequency;
  197. u32 hs_settle;
  198. u32 num_lanes;
  199. u32 max_num_lanes;
  200. u8 wclk_ext;
  201. const struct csis_pix_format *csis_fmt;
  202. struct v4l2_mbus_framefmt format;
  203. spinlock_t slock;
  204. struct csis_pktbuf pkt_buf;
  205. struct s5pcsis_event events[S5PCSIS_NUM_EVENTS];
  206. };
  207. /**
  208. * struct csis_pix_format - CSIS pixel format description
  209. * @pix_width_alignment: horizontal pixel alignment, width will be
  210. * multiple of 2^pix_width_alignment
  211. * @code: corresponding media bus code
  212. * @fmt_reg: S5PCSIS_CONFIG register value
  213. * @data_alignment: MIPI-CSI data alignment in bits
  214. */
  215. struct csis_pix_format {
  216. unsigned int pix_width_alignment;
  217. u32 code;
  218. u32 fmt_reg;
  219. u8 data_alignment;
  220. };
  221. static const struct csis_pix_format s5pcsis_formats[] = {
  222. {
  223. .code = MEDIA_BUS_FMT_VYUY8_2X8,
  224. .fmt_reg = S5PCSIS_CFG_FMT_YCBCR422_8BIT,
  225. .data_alignment = 32,
  226. }, {
  227. .code = MEDIA_BUS_FMT_JPEG_1X8,
  228. .fmt_reg = S5PCSIS_CFG_FMT_USER(1),
  229. .data_alignment = 32,
  230. }, {
  231. .code = MEDIA_BUS_FMT_S5C_UYVY_JPEG_1X8,
  232. .fmt_reg = S5PCSIS_CFG_FMT_USER(1),
  233. .data_alignment = 32,
  234. }, {
  235. .code = MEDIA_BUS_FMT_SGRBG8_1X8,
  236. .fmt_reg = S5PCSIS_CFG_FMT_RAW8,
  237. .data_alignment = 24,
  238. }, {
  239. .code = MEDIA_BUS_FMT_SGRBG10_1X10,
  240. .fmt_reg = S5PCSIS_CFG_FMT_RAW10,
  241. .data_alignment = 24,
  242. }, {
  243. .code = MEDIA_BUS_FMT_SGRBG12_1X12,
  244. .fmt_reg = S5PCSIS_CFG_FMT_RAW12,
  245. .data_alignment = 24,
  246. }
  247. };
  248. #define s5pcsis_write(__csis, __r, __v) writel(__v, __csis->regs + __r)
  249. #define s5pcsis_read(__csis, __r) readl(__csis->regs + __r)
  250. static struct csis_state *sd_to_csis_state(struct v4l2_subdev *sdev)
  251. {
  252. return container_of(sdev, struct csis_state, sd);
  253. }
  254. static const struct csis_pix_format *find_csis_format(
  255. struct v4l2_mbus_framefmt *mf)
  256. {
  257. int i;
  258. for (i = 0; i < ARRAY_SIZE(s5pcsis_formats); i++)
  259. if (mf->code == s5pcsis_formats[i].code)
  260. return &s5pcsis_formats[i];
  261. return NULL;
  262. }
  263. static void s5pcsis_enable_interrupts(struct csis_state *state, bool on)
  264. {
  265. u32 val = s5pcsis_read(state, S5PCSIS_INTMSK);
  266. if (on)
  267. val |= state->interrupt_mask;
  268. else
  269. val &= ~state->interrupt_mask;
  270. s5pcsis_write(state, S5PCSIS_INTMSK, val);
  271. }
  272. static void s5pcsis_reset(struct csis_state *state)
  273. {
  274. u32 val = s5pcsis_read(state, S5PCSIS_CTRL);
  275. s5pcsis_write(state, S5PCSIS_CTRL, val | S5PCSIS_CTRL_RESET);
  276. udelay(10);
  277. }
  278. static void s5pcsis_system_enable(struct csis_state *state, int on)
  279. {
  280. u32 val, mask;
  281. val = s5pcsis_read(state, S5PCSIS_CTRL);
  282. if (on)
  283. val |= S5PCSIS_CTRL_ENABLE;
  284. else
  285. val &= ~S5PCSIS_CTRL_ENABLE;
  286. s5pcsis_write(state, S5PCSIS_CTRL, val);
  287. val = s5pcsis_read(state, S5PCSIS_DPHYCTRL);
  288. val &= ~S5PCSIS_DPHYCTRL_ENABLE;
  289. if (on) {
  290. mask = (1 << (state->num_lanes + 1)) - 1;
  291. val |= (mask & S5PCSIS_DPHYCTRL_ENABLE);
  292. }
  293. s5pcsis_write(state, S5PCSIS_DPHYCTRL, val);
  294. }
  295. /* Called with the state.lock mutex held */
  296. static void __s5pcsis_set_format(struct csis_state *state)
  297. {
  298. struct v4l2_mbus_framefmt *mf = &state->format;
  299. u32 val;
  300. v4l2_dbg(1, debug, &state->sd, "fmt: %#x, %d x %d\n",
  301. mf->code, mf->width, mf->height);
  302. /* Color format */
  303. val = s5pcsis_read(state, S5PCSIS_CONFIG);
  304. val = (val & ~S5PCSIS_CFG_FMT_MASK) | state->csis_fmt->fmt_reg;
  305. s5pcsis_write(state, S5PCSIS_CONFIG, val);
  306. /* Pixel resolution */
  307. val = (mf->width << 16) | mf->height;
  308. s5pcsis_write(state, S5PCSIS_RESOL, val);
  309. }
  310. static void s5pcsis_set_hsync_settle(struct csis_state *state, int settle)
  311. {
  312. u32 val = s5pcsis_read(state, S5PCSIS_DPHYCTRL);
  313. val = (val & ~S5PCSIS_DPHYCTRL_HSS_MASK) | (settle << 27);
  314. s5pcsis_write(state, S5PCSIS_DPHYCTRL, val);
  315. }
  316. static void s5pcsis_set_params(struct csis_state *state)
  317. {
  318. u32 val;
  319. val = s5pcsis_read(state, S5PCSIS_CONFIG);
  320. val = (val & ~S5PCSIS_CFG_NR_LANE_MASK) | (state->num_lanes - 1);
  321. s5pcsis_write(state, S5PCSIS_CONFIG, val);
  322. __s5pcsis_set_format(state);
  323. s5pcsis_set_hsync_settle(state, state->hs_settle);
  324. val = s5pcsis_read(state, S5PCSIS_CTRL);
  325. if (state->csis_fmt->data_alignment == 32)
  326. val |= S5PCSIS_CTRL_ALIGN_32BIT;
  327. else /* 24-bits */
  328. val &= ~S5PCSIS_CTRL_ALIGN_32BIT;
  329. val &= ~S5PCSIS_CTRL_WCLK_EXTCLK;
  330. if (state->wclk_ext)
  331. val |= S5PCSIS_CTRL_WCLK_EXTCLK;
  332. s5pcsis_write(state, S5PCSIS_CTRL, val);
  333. /* Update the shadow register. */
  334. val = s5pcsis_read(state, S5PCSIS_CTRL);
  335. s5pcsis_write(state, S5PCSIS_CTRL, val | S5PCSIS_CTRL_UPDATE_SHADOW);
  336. }
  337. static void s5pcsis_clk_put(struct csis_state *state)
  338. {
  339. int i;
  340. for (i = 0; i < NUM_CSIS_CLOCKS; i++) {
  341. if (IS_ERR(state->clock[i]))
  342. continue;
  343. clk_unprepare(state->clock[i]);
  344. clk_put(state->clock[i]);
  345. state->clock[i] = ERR_PTR(-EINVAL);
  346. }
  347. }
  348. static int s5pcsis_clk_get(struct csis_state *state)
  349. {
  350. struct device *dev = &state->pdev->dev;
  351. int i, ret;
  352. for (i = 0; i < NUM_CSIS_CLOCKS; i++)
  353. state->clock[i] = ERR_PTR(-EINVAL);
  354. for (i = 0; i < NUM_CSIS_CLOCKS; i++) {
  355. state->clock[i] = clk_get(dev, csi_clock_name[i]);
  356. if (IS_ERR(state->clock[i])) {
  357. ret = PTR_ERR(state->clock[i]);
  358. goto err;
  359. }
  360. ret = clk_prepare(state->clock[i]);
  361. if (ret < 0) {
  362. clk_put(state->clock[i]);
  363. state->clock[i] = ERR_PTR(-EINVAL);
  364. goto err;
  365. }
  366. }
  367. return 0;
  368. err:
  369. s5pcsis_clk_put(state);
  370. dev_err(dev, "failed to get clock: %s\n", csi_clock_name[i]);
  371. return ret;
  372. }
  373. static void dump_regs(struct csis_state *state, const char *label)
  374. {
  375. struct {
  376. u32 offset;
  377. const char * const name;
  378. } registers[] = {
  379. { 0x00, "CTRL" },
  380. { 0x04, "DPHYCTRL" },
  381. { 0x08, "CONFIG" },
  382. { 0x0c, "DPHYSTS" },
  383. { 0x10, "INTMSK" },
  384. { 0x2c, "RESOL" },
  385. { 0x38, "SDW_CONFIG" },
  386. };
  387. u32 i;
  388. v4l2_info(&state->sd, "--- %s ---\n", label);
  389. for (i = 0; i < ARRAY_SIZE(registers); i++) {
  390. u32 cfg = s5pcsis_read(state, registers[i].offset);
  391. v4l2_info(&state->sd, "%10s: 0x%08x\n", registers[i].name, cfg);
  392. }
  393. }
  394. static void s5pcsis_start_stream(struct csis_state *state)
  395. {
  396. s5pcsis_reset(state);
  397. s5pcsis_set_params(state);
  398. s5pcsis_system_enable(state, true);
  399. s5pcsis_enable_interrupts(state, true);
  400. }
  401. static void s5pcsis_stop_stream(struct csis_state *state)
  402. {
  403. s5pcsis_enable_interrupts(state, false);
  404. s5pcsis_system_enable(state, false);
  405. }
  406. static void s5pcsis_clear_counters(struct csis_state *state)
  407. {
  408. unsigned long flags;
  409. int i;
  410. spin_lock_irqsave(&state->slock, flags);
  411. for (i = 0; i < S5PCSIS_NUM_EVENTS; i++)
  412. state->events[i].counter = 0;
  413. spin_unlock_irqrestore(&state->slock, flags);
  414. }
  415. static void s5pcsis_log_counters(struct csis_state *state, bool non_errors)
  416. {
  417. int i = non_errors ? S5PCSIS_NUM_EVENTS : S5PCSIS_NUM_EVENTS - 4;
  418. unsigned long flags;
  419. spin_lock_irqsave(&state->slock, flags);
  420. for (i--; i >= 0; i--) {
  421. if (state->events[i].counter > 0 || debug)
  422. v4l2_info(&state->sd, "%s events: %d\n",
  423. state->events[i].name,
  424. state->events[i].counter);
  425. }
  426. spin_unlock_irqrestore(&state->slock, flags);
  427. }
  428. /*
  429. * V4L2 subdev operations
  430. */
  431. static int s5pcsis_s_power(struct v4l2_subdev *sd, int on)
  432. {
  433. struct csis_state *state = sd_to_csis_state(sd);
  434. struct device *dev = &state->pdev->dev;
  435. if (on)
  436. return pm_runtime_get_sync(dev);
  437. return pm_runtime_put_sync(dev);
  438. }
  439. static int s5pcsis_s_stream(struct v4l2_subdev *sd, int enable)
  440. {
  441. struct csis_state *state = sd_to_csis_state(sd);
  442. int ret = 0;
  443. v4l2_dbg(1, debug, sd, "%s: %d, state: 0x%x\n",
  444. __func__, enable, state->flags);
  445. if (enable) {
  446. s5pcsis_clear_counters(state);
  447. ret = pm_runtime_get_sync(&state->pdev->dev);
  448. if (ret && ret != 1)
  449. return ret;
  450. }
  451. mutex_lock(&state->lock);
  452. if (enable) {
  453. if (state->flags & ST_SUSPENDED) {
  454. ret = -EBUSY;
  455. goto unlock;
  456. }
  457. s5pcsis_start_stream(state);
  458. state->flags |= ST_STREAMING;
  459. } else {
  460. s5pcsis_stop_stream(state);
  461. state->flags &= ~ST_STREAMING;
  462. if (debug > 0)
  463. s5pcsis_log_counters(state, true);
  464. }
  465. unlock:
  466. mutex_unlock(&state->lock);
  467. if (!enable)
  468. pm_runtime_put(&state->pdev->dev);
  469. return ret == 1 ? 0 : ret;
  470. }
  471. static int s5pcsis_enum_mbus_code(struct v4l2_subdev *sd,
  472. struct v4l2_subdev_pad_config *cfg,
  473. struct v4l2_subdev_mbus_code_enum *code)
  474. {
  475. if (code->index >= ARRAY_SIZE(s5pcsis_formats))
  476. return -EINVAL;
  477. code->code = s5pcsis_formats[code->index].code;
  478. return 0;
  479. }
  480. static struct csis_pix_format const *s5pcsis_try_format(
  481. struct v4l2_mbus_framefmt *mf)
  482. {
  483. struct csis_pix_format const *csis_fmt;
  484. csis_fmt = find_csis_format(mf);
  485. if (csis_fmt == NULL)
  486. csis_fmt = &s5pcsis_formats[0];
  487. mf->code = csis_fmt->code;
  488. v4l_bound_align_image(&mf->width, 1, CSIS_MAX_PIX_WIDTH,
  489. csis_fmt->pix_width_alignment,
  490. &mf->height, 1, CSIS_MAX_PIX_HEIGHT, 1,
  491. 0);
  492. return csis_fmt;
  493. }
  494. static struct v4l2_mbus_framefmt *__s5pcsis_get_format(
  495. struct csis_state *state, struct v4l2_subdev_pad_config *cfg,
  496. enum v4l2_subdev_format_whence which)
  497. {
  498. if (which == V4L2_SUBDEV_FORMAT_TRY)
  499. return cfg ? v4l2_subdev_get_try_format(&state->sd, cfg, 0) : NULL;
  500. return &state->format;
  501. }
  502. static int s5pcsis_set_fmt(struct v4l2_subdev *sd, struct v4l2_subdev_pad_config *cfg,
  503. struct v4l2_subdev_format *fmt)
  504. {
  505. struct csis_state *state = sd_to_csis_state(sd);
  506. struct csis_pix_format const *csis_fmt;
  507. struct v4l2_mbus_framefmt *mf;
  508. mf = __s5pcsis_get_format(state, cfg, fmt->which);
  509. if (fmt->pad == CSIS_PAD_SOURCE) {
  510. if (mf) {
  511. mutex_lock(&state->lock);
  512. fmt->format = *mf;
  513. mutex_unlock(&state->lock);
  514. }
  515. return 0;
  516. }
  517. csis_fmt = s5pcsis_try_format(&fmt->format);
  518. if (mf) {
  519. mutex_lock(&state->lock);
  520. *mf = fmt->format;
  521. if (fmt->which == V4L2_SUBDEV_FORMAT_ACTIVE)
  522. state->csis_fmt = csis_fmt;
  523. mutex_unlock(&state->lock);
  524. }
  525. return 0;
  526. }
  527. static int s5pcsis_get_fmt(struct v4l2_subdev *sd, struct v4l2_subdev_pad_config *cfg,
  528. struct v4l2_subdev_format *fmt)
  529. {
  530. struct csis_state *state = sd_to_csis_state(sd);
  531. struct v4l2_mbus_framefmt *mf;
  532. mf = __s5pcsis_get_format(state, cfg, fmt->which);
  533. if (!mf)
  534. return -EINVAL;
  535. mutex_lock(&state->lock);
  536. fmt->format = *mf;
  537. mutex_unlock(&state->lock);
  538. return 0;
  539. }
  540. static int s5pcsis_s_rx_buffer(struct v4l2_subdev *sd, void *buf,
  541. unsigned int *size)
  542. {
  543. struct csis_state *state = sd_to_csis_state(sd);
  544. unsigned long flags;
  545. *size = min_t(unsigned int, *size, S5PCSIS_PKTDATA_SIZE);
  546. spin_lock_irqsave(&state->slock, flags);
  547. state->pkt_buf.data = buf;
  548. state->pkt_buf.len = *size;
  549. spin_unlock_irqrestore(&state->slock, flags);
  550. return 0;
  551. }
  552. static int s5pcsis_log_status(struct v4l2_subdev *sd)
  553. {
  554. struct csis_state *state = sd_to_csis_state(sd);
  555. mutex_lock(&state->lock);
  556. s5pcsis_log_counters(state, true);
  557. if (debug && (state->flags & ST_POWERED))
  558. dump_regs(state, __func__);
  559. mutex_unlock(&state->lock);
  560. return 0;
  561. }
  562. static const struct v4l2_subdev_core_ops s5pcsis_core_ops = {
  563. .s_power = s5pcsis_s_power,
  564. .log_status = s5pcsis_log_status,
  565. };
  566. static const struct v4l2_subdev_pad_ops s5pcsis_pad_ops = {
  567. .enum_mbus_code = s5pcsis_enum_mbus_code,
  568. .get_fmt = s5pcsis_get_fmt,
  569. .set_fmt = s5pcsis_set_fmt,
  570. };
  571. static const struct v4l2_subdev_video_ops s5pcsis_video_ops = {
  572. .s_rx_buffer = s5pcsis_s_rx_buffer,
  573. .s_stream = s5pcsis_s_stream,
  574. };
  575. static const struct v4l2_subdev_ops s5pcsis_subdev_ops = {
  576. .core = &s5pcsis_core_ops,
  577. .pad = &s5pcsis_pad_ops,
  578. .video = &s5pcsis_video_ops,
  579. };
  580. static irqreturn_t s5pcsis_irq_handler(int irq, void *dev_id)
  581. {
  582. struct csis_state *state = dev_id;
  583. struct csis_pktbuf *pktbuf = &state->pkt_buf;
  584. unsigned long flags;
  585. u32 status;
  586. status = s5pcsis_read(state, S5PCSIS_INTSRC);
  587. spin_lock_irqsave(&state->slock, flags);
  588. if ((status & S5PCSIS_INTSRC_NON_IMAGE_DATA) && pktbuf->data) {
  589. u32 offset;
  590. if (status & S5PCSIS_INTSRC_EVEN)
  591. offset = S5PCSIS_PKTDATA_EVEN;
  592. else
  593. offset = S5PCSIS_PKTDATA_ODD;
  594. memcpy(pktbuf->data, (u8 __force *)state->regs + offset,
  595. pktbuf->len);
  596. pktbuf->data = NULL;
  597. rmb();
  598. }
  599. /* Update the event/error counters */
  600. if ((status & S5PCSIS_INTSRC_ERRORS) || debug) {
  601. int i;
  602. for (i = 0; i < S5PCSIS_NUM_EVENTS; i++) {
  603. if (!(status & state->events[i].mask))
  604. continue;
  605. state->events[i].counter++;
  606. v4l2_dbg(2, debug, &state->sd, "%s: %d\n",
  607. state->events[i].name,
  608. state->events[i].counter);
  609. }
  610. v4l2_dbg(2, debug, &state->sd, "status: %08x\n", status);
  611. }
  612. spin_unlock_irqrestore(&state->slock, flags);
  613. s5pcsis_write(state, S5PCSIS_INTSRC, status);
  614. return IRQ_HANDLED;
  615. }
  616. static int s5pcsis_parse_dt(struct platform_device *pdev,
  617. struct csis_state *state)
  618. {
  619. struct device_node *node = pdev->dev.of_node;
  620. struct v4l2_fwnode_endpoint endpoint;
  621. int ret;
  622. if (of_property_read_u32(node, "clock-frequency",
  623. &state->clk_frequency))
  624. state->clk_frequency = DEFAULT_SCLK_CSIS_FREQ;
  625. if (of_property_read_u32(node, "bus-width",
  626. &state->max_num_lanes))
  627. return -EINVAL;
  628. node = of_graph_get_next_endpoint(node, NULL);
  629. if (!node) {
  630. dev_err(&pdev->dev, "No port node at %pOF\n",
  631. pdev->dev.of_node);
  632. return -EINVAL;
  633. }
  634. /* Get port node and validate MIPI-CSI channel id. */
  635. ret = v4l2_fwnode_endpoint_parse(of_fwnode_handle(node), &endpoint);
  636. if (ret)
  637. goto err;
  638. state->index = endpoint.base.port - FIMC_INPUT_MIPI_CSI2_0;
  639. if (state->index >= CSIS_MAX_ENTITIES) {
  640. ret = -ENXIO;
  641. goto err;
  642. }
  643. /* Get MIPI CSI-2 bus configration from the endpoint node. */
  644. of_property_read_u32(node, "samsung,csis-hs-settle",
  645. &state->hs_settle);
  646. state->wclk_ext = of_property_read_bool(node,
  647. "samsung,csis-wclk");
  648. state->num_lanes = endpoint.bus.mipi_csi2.num_data_lanes;
  649. err:
  650. of_node_put(node);
  651. return ret;
  652. }
  653. static int s5pcsis_pm_resume(struct device *dev, bool runtime);
  654. static const struct of_device_id s5pcsis_of_match[];
  655. static int s5pcsis_probe(struct platform_device *pdev)
  656. {
  657. const struct of_device_id *of_id;
  658. const struct csis_drvdata *drv_data;
  659. struct device *dev = &pdev->dev;
  660. struct resource *mem_res;
  661. struct csis_state *state;
  662. int ret = -ENOMEM;
  663. int i;
  664. state = devm_kzalloc(dev, sizeof(*state), GFP_KERNEL);
  665. if (!state)
  666. return -ENOMEM;
  667. mutex_init(&state->lock);
  668. spin_lock_init(&state->slock);
  669. state->pdev = pdev;
  670. of_id = of_match_node(s5pcsis_of_match, dev->of_node);
  671. if (WARN_ON(of_id == NULL))
  672. return -EINVAL;
  673. drv_data = of_id->data;
  674. state->interrupt_mask = drv_data->interrupt_mask;
  675. ret = s5pcsis_parse_dt(pdev, state);
  676. if (ret < 0)
  677. return ret;
  678. if (state->num_lanes == 0 || state->num_lanes > state->max_num_lanes) {
  679. dev_err(dev, "Unsupported number of data lanes: %d (max. %d)\n",
  680. state->num_lanes, state->max_num_lanes);
  681. return -EINVAL;
  682. }
  683. state->phy = devm_phy_get(dev, "csis");
  684. if (IS_ERR(state->phy))
  685. return PTR_ERR(state->phy);
  686. mem_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  687. state->regs = devm_ioremap_resource(dev, mem_res);
  688. if (IS_ERR(state->regs))
  689. return PTR_ERR(state->regs);
  690. state->irq = platform_get_irq(pdev, 0);
  691. if (state->irq < 0) {
  692. dev_err(dev, "Failed to get irq\n");
  693. return state->irq;
  694. }
  695. for (i = 0; i < CSIS_NUM_SUPPLIES; i++)
  696. state->supplies[i].supply = csis_supply_name[i];
  697. ret = devm_regulator_bulk_get(dev, CSIS_NUM_SUPPLIES,
  698. state->supplies);
  699. if (ret)
  700. return ret;
  701. ret = s5pcsis_clk_get(state);
  702. if (ret < 0)
  703. return ret;
  704. if (state->clk_frequency)
  705. ret = clk_set_rate(state->clock[CSIS_CLK_MUX],
  706. state->clk_frequency);
  707. else
  708. dev_WARN(dev, "No clock frequency specified!\n");
  709. if (ret < 0)
  710. goto e_clkput;
  711. ret = clk_enable(state->clock[CSIS_CLK_MUX]);
  712. if (ret < 0)
  713. goto e_clkput;
  714. ret = devm_request_irq(dev, state->irq, s5pcsis_irq_handler,
  715. 0, dev_name(dev), state);
  716. if (ret) {
  717. dev_err(dev, "Interrupt request failed\n");
  718. goto e_clkdis;
  719. }
  720. v4l2_subdev_init(&state->sd, &s5pcsis_subdev_ops);
  721. state->sd.owner = THIS_MODULE;
  722. snprintf(state->sd.name, sizeof(state->sd.name), "%s.%d",
  723. CSIS_SUBDEV_NAME, state->index);
  724. state->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  725. state->csis_fmt = &s5pcsis_formats[0];
  726. state->format.code = s5pcsis_formats[0].code;
  727. state->format.width = S5PCSIS_DEF_PIX_WIDTH;
  728. state->format.height = S5PCSIS_DEF_PIX_HEIGHT;
  729. state->sd.entity.function = MEDIA_ENT_F_IO_V4L;
  730. state->pads[CSIS_PAD_SINK].flags = MEDIA_PAD_FL_SINK;
  731. state->pads[CSIS_PAD_SOURCE].flags = MEDIA_PAD_FL_SOURCE;
  732. ret = media_entity_pads_init(&state->sd.entity,
  733. CSIS_PADS_NUM, state->pads);
  734. if (ret < 0)
  735. goto e_clkdis;
  736. /* This allows to retrieve the platform device id by the host driver */
  737. v4l2_set_subdevdata(&state->sd, pdev);
  738. /* .. and a pointer to the subdev. */
  739. platform_set_drvdata(pdev, &state->sd);
  740. memcpy(state->events, s5pcsis_events, sizeof(state->events));
  741. pm_runtime_enable(dev);
  742. if (!pm_runtime_enabled(dev)) {
  743. ret = s5pcsis_pm_resume(dev, true);
  744. if (ret < 0)
  745. goto e_m_ent;
  746. }
  747. dev_info(&pdev->dev, "lanes: %d, hs_settle: %d, wclk: %d, freq: %u\n",
  748. state->num_lanes, state->hs_settle, state->wclk_ext,
  749. state->clk_frequency);
  750. return 0;
  751. e_m_ent:
  752. media_entity_cleanup(&state->sd.entity);
  753. e_clkdis:
  754. clk_disable(state->clock[CSIS_CLK_MUX]);
  755. e_clkput:
  756. s5pcsis_clk_put(state);
  757. return ret;
  758. }
  759. static int s5pcsis_pm_suspend(struct device *dev, bool runtime)
  760. {
  761. struct v4l2_subdev *sd = dev_get_drvdata(dev);
  762. struct csis_state *state = sd_to_csis_state(sd);
  763. int ret = 0;
  764. v4l2_dbg(1, debug, sd, "%s: flags: 0x%x\n",
  765. __func__, state->flags);
  766. mutex_lock(&state->lock);
  767. if (state->flags & ST_POWERED) {
  768. s5pcsis_stop_stream(state);
  769. ret = phy_power_off(state->phy);
  770. if (ret)
  771. goto unlock;
  772. ret = regulator_bulk_disable(CSIS_NUM_SUPPLIES,
  773. state->supplies);
  774. if (ret)
  775. goto unlock;
  776. clk_disable(state->clock[CSIS_CLK_GATE]);
  777. state->flags &= ~ST_POWERED;
  778. if (!runtime)
  779. state->flags |= ST_SUSPENDED;
  780. }
  781. unlock:
  782. mutex_unlock(&state->lock);
  783. return ret ? -EAGAIN : 0;
  784. }
  785. static int s5pcsis_pm_resume(struct device *dev, bool runtime)
  786. {
  787. struct v4l2_subdev *sd = dev_get_drvdata(dev);
  788. struct csis_state *state = sd_to_csis_state(sd);
  789. int ret = 0;
  790. v4l2_dbg(1, debug, sd, "%s: flags: 0x%x\n",
  791. __func__, state->flags);
  792. mutex_lock(&state->lock);
  793. if (!runtime && !(state->flags & ST_SUSPENDED))
  794. goto unlock;
  795. if (!(state->flags & ST_POWERED)) {
  796. ret = regulator_bulk_enable(CSIS_NUM_SUPPLIES,
  797. state->supplies);
  798. if (ret)
  799. goto unlock;
  800. ret = phy_power_on(state->phy);
  801. if (!ret) {
  802. state->flags |= ST_POWERED;
  803. } else {
  804. regulator_bulk_disable(CSIS_NUM_SUPPLIES,
  805. state->supplies);
  806. goto unlock;
  807. }
  808. clk_enable(state->clock[CSIS_CLK_GATE]);
  809. }
  810. if (state->flags & ST_STREAMING)
  811. s5pcsis_start_stream(state);
  812. state->flags &= ~ST_SUSPENDED;
  813. unlock:
  814. mutex_unlock(&state->lock);
  815. return ret ? -EAGAIN : 0;
  816. }
  817. #ifdef CONFIG_PM_SLEEP
  818. static int s5pcsis_suspend(struct device *dev)
  819. {
  820. return s5pcsis_pm_suspend(dev, false);
  821. }
  822. static int s5pcsis_resume(struct device *dev)
  823. {
  824. return s5pcsis_pm_resume(dev, false);
  825. }
  826. #endif
  827. #ifdef CONFIG_PM
  828. static int s5pcsis_runtime_suspend(struct device *dev)
  829. {
  830. return s5pcsis_pm_suspend(dev, true);
  831. }
  832. static int s5pcsis_runtime_resume(struct device *dev)
  833. {
  834. return s5pcsis_pm_resume(dev, true);
  835. }
  836. #endif
  837. static int s5pcsis_remove(struct platform_device *pdev)
  838. {
  839. struct v4l2_subdev *sd = platform_get_drvdata(pdev);
  840. struct csis_state *state = sd_to_csis_state(sd);
  841. pm_runtime_disable(&pdev->dev);
  842. s5pcsis_pm_suspend(&pdev->dev, true);
  843. clk_disable(state->clock[CSIS_CLK_MUX]);
  844. pm_runtime_set_suspended(&pdev->dev);
  845. s5pcsis_clk_put(state);
  846. media_entity_cleanup(&state->sd.entity);
  847. return 0;
  848. }
  849. static const struct dev_pm_ops s5pcsis_pm_ops = {
  850. SET_RUNTIME_PM_OPS(s5pcsis_runtime_suspend, s5pcsis_runtime_resume,
  851. NULL)
  852. SET_SYSTEM_SLEEP_PM_OPS(s5pcsis_suspend, s5pcsis_resume)
  853. };
  854. static const struct csis_drvdata exynos4_csis_drvdata = {
  855. .interrupt_mask = S5PCSIS_INTMSK_EXYNOS4_EN_ALL,
  856. };
  857. static const struct csis_drvdata exynos5_csis_drvdata = {
  858. .interrupt_mask = S5PCSIS_INTMSK_EXYNOS5_EN_ALL,
  859. };
  860. static const struct of_device_id s5pcsis_of_match[] = {
  861. {
  862. .compatible = "samsung,s5pv210-csis",
  863. .data = &exynos4_csis_drvdata,
  864. }, {
  865. .compatible = "samsung,exynos4210-csis",
  866. .data = &exynos4_csis_drvdata,
  867. }, {
  868. .compatible = "samsung,exynos5250-csis",
  869. .data = &exynos5_csis_drvdata,
  870. },
  871. { /* sentinel */ },
  872. };
  873. MODULE_DEVICE_TABLE(of, s5pcsis_of_match);
  874. static struct platform_driver s5pcsis_driver = {
  875. .probe = s5pcsis_probe,
  876. .remove = s5pcsis_remove,
  877. .driver = {
  878. .of_match_table = s5pcsis_of_match,
  879. .name = CSIS_DRIVER_NAME,
  880. .pm = &s5pcsis_pm_ops,
  881. },
  882. };
  883. module_platform_driver(s5pcsis_driver);
  884. MODULE_AUTHOR("Sylwester Nawrocki <s.nawrocki@samsung.com>");
  885. MODULE_DESCRIPTION("Samsung S5P/EXYNOS SoC MIPI-CSI2 receiver driver");
  886. MODULE_LICENSE("GPL");