mipi-csis.c 27 KB

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