mipi-csis.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053
  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/exynos-fimc.h>
  32. #include <media/v4l2-of.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. * @clock_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 int s5pcsis_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
  563. {
  564. struct v4l2_mbus_framefmt *format = v4l2_subdev_get_try_format(sd, fh->pad, 0);
  565. format->colorspace = V4L2_COLORSPACE_JPEG;
  566. format->code = s5pcsis_formats[0].code;
  567. format->width = S5PCSIS_DEF_PIX_WIDTH;
  568. format->height = S5PCSIS_DEF_PIX_HEIGHT;
  569. format->field = V4L2_FIELD_NONE;
  570. return 0;
  571. }
  572. static const struct v4l2_subdev_internal_ops s5pcsis_sd_internal_ops = {
  573. .open = s5pcsis_open,
  574. };
  575. static struct v4l2_subdev_core_ops s5pcsis_core_ops = {
  576. .s_power = s5pcsis_s_power,
  577. .log_status = s5pcsis_log_status,
  578. };
  579. static struct v4l2_subdev_pad_ops s5pcsis_pad_ops = {
  580. .enum_mbus_code = s5pcsis_enum_mbus_code,
  581. .get_fmt = s5pcsis_get_fmt,
  582. .set_fmt = s5pcsis_set_fmt,
  583. };
  584. static struct v4l2_subdev_video_ops s5pcsis_video_ops = {
  585. .s_rx_buffer = s5pcsis_s_rx_buffer,
  586. .s_stream = s5pcsis_s_stream,
  587. };
  588. static struct v4l2_subdev_ops s5pcsis_subdev_ops = {
  589. .core = &s5pcsis_core_ops,
  590. .pad = &s5pcsis_pad_ops,
  591. .video = &s5pcsis_video_ops,
  592. };
  593. static irqreturn_t s5pcsis_irq_handler(int irq, void *dev_id)
  594. {
  595. struct csis_state *state = dev_id;
  596. struct csis_pktbuf *pktbuf = &state->pkt_buf;
  597. unsigned long flags;
  598. u32 status;
  599. status = s5pcsis_read(state, S5PCSIS_INTSRC);
  600. spin_lock_irqsave(&state->slock, flags);
  601. if ((status & S5PCSIS_INTSRC_NON_IMAGE_DATA) && pktbuf->data) {
  602. u32 offset;
  603. if (status & S5PCSIS_INTSRC_EVEN)
  604. offset = S5PCSIS_PKTDATA_EVEN;
  605. else
  606. offset = S5PCSIS_PKTDATA_ODD;
  607. memcpy(pktbuf->data, (u8 __force *)state->regs + offset,
  608. pktbuf->len);
  609. pktbuf->data = NULL;
  610. rmb();
  611. }
  612. /* Update the event/error counters */
  613. if ((status & S5PCSIS_INTSRC_ERRORS) || debug) {
  614. int i;
  615. for (i = 0; i < S5PCSIS_NUM_EVENTS; i++) {
  616. if (!(status & state->events[i].mask))
  617. continue;
  618. state->events[i].counter++;
  619. v4l2_dbg(2, debug, &state->sd, "%s: %d\n",
  620. state->events[i].name,
  621. state->events[i].counter);
  622. }
  623. v4l2_dbg(2, debug, &state->sd, "status: %08x\n", status);
  624. }
  625. spin_unlock_irqrestore(&state->slock, flags);
  626. s5pcsis_write(state, S5PCSIS_INTSRC, status);
  627. return IRQ_HANDLED;
  628. }
  629. static int s5pcsis_parse_dt(struct platform_device *pdev,
  630. struct csis_state *state)
  631. {
  632. struct device_node *node = pdev->dev.of_node;
  633. struct v4l2_of_endpoint endpoint;
  634. if (of_property_read_u32(node, "clock-frequency",
  635. &state->clk_frequency))
  636. state->clk_frequency = DEFAULT_SCLK_CSIS_FREQ;
  637. if (of_property_read_u32(node, "bus-width",
  638. &state->max_num_lanes))
  639. return -EINVAL;
  640. node = of_graph_get_next_endpoint(node, NULL);
  641. if (!node) {
  642. dev_err(&pdev->dev, "No port node at %s\n",
  643. pdev->dev.of_node->full_name);
  644. return -EINVAL;
  645. }
  646. /* Get port node and validate MIPI-CSI channel id. */
  647. v4l2_of_parse_endpoint(node, &endpoint);
  648. state->index = endpoint.base.port - FIMC_INPUT_MIPI_CSI2_0;
  649. if (state->index >= CSIS_MAX_ENTITIES)
  650. return -ENXIO;
  651. /* Get MIPI CSI-2 bus configration from the endpoint node. */
  652. of_property_read_u32(node, "samsung,csis-hs-settle",
  653. &state->hs_settle);
  654. state->wclk_ext = of_property_read_bool(node,
  655. "samsung,csis-wclk");
  656. state->num_lanes = endpoint.bus.mipi_csi2.num_data_lanes;
  657. of_node_put(node);
  658. return 0;
  659. }
  660. static int s5pcsis_pm_resume(struct device *dev, bool runtime);
  661. static const struct of_device_id s5pcsis_of_match[];
  662. static int s5pcsis_probe(struct platform_device *pdev)
  663. {
  664. const struct of_device_id *of_id;
  665. const struct csis_drvdata *drv_data;
  666. struct device *dev = &pdev->dev;
  667. struct resource *mem_res;
  668. struct csis_state *state;
  669. int ret = -ENOMEM;
  670. int i;
  671. state = devm_kzalloc(dev, sizeof(*state), GFP_KERNEL);
  672. if (!state)
  673. return -ENOMEM;
  674. mutex_init(&state->lock);
  675. spin_lock_init(&state->slock);
  676. state->pdev = pdev;
  677. of_id = of_match_node(s5pcsis_of_match, dev->of_node);
  678. if (WARN_ON(of_id == NULL))
  679. return -EINVAL;
  680. drv_data = of_id->data;
  681. state->interrupt_mask = drv_data->interrupt_mask;
  682. ret = s5pcsis_parse_dt(pdev, state);
  683. if (ret < 0)
  684. return ret;
  685. if (state->num_lanes == 0 || state->num_lanes > state->max_num_lanes) {
  686. dev_err(dev, "Unsupported number of data lanes: %d (max. %d)\n",
  687. state->num_lanes, state->max_num_lanes);
  688. return -EINVAL;
  689. }
  690. state->phy = devm_phy_get(dev, "csis");
  691. if (IS_ERR(state->phy))
  692. return PTR_ERR(state->phy);
  693. mem_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  694. state->regs = devm_ioremap_resource(dev, mem_res);
  695. if (IS_ERR(state->regs))
  696. return PTR_ERR(state->regs);
  697. state->irq = platform_get_irq(pdev, 0);
  698. if (state->irq < 0) {
  699. dev_err(dev, "Failed to get irq\n");
  700. return state->irq;
  701. }
  702. for (i = 0; i < CSIS_NUM_SUPPLIES; i++)
  703. state->supplies[i].supply = csis_supply_name[i];
  704. ret = devm_regulator_bulk_get(dev, CSIS_NUM_SUPPLIES,
  705. state->supplies);
  706. if (ret)
  707. return ret;
  708. ret = s5pcsis_clk_get(state);
  709. if (ret < 0)
  710. return ret;
  711. if (state->clk_frequency)
  712. ret = clk_set_rate(state->clock[CSIS_CLK_MUX],
  713. state->clk_frequency);
  714. else
  715. dev_WARN(dev, "No clock frequency specified!\n");
  716. if (ret < 0)
  717. goto e_clkput;
  718. ret = clk_enable(state->clock[CSIS_CLK_MUX]);
  719. if (ret < 0)
  720. goto e_clkput;
  721. ret = devm_request_irq(dev, state->irq, s5pcsis_irq_handler,
  722. 0, dev_name(dev), state);
  723. if (ret) {
  724. dev_err(dev, "Interrupt request failed\n");
  725. goto e_clkdis;
  726. }
  727. v4l2_subdev_init(&state->sd, &s5pcsis_subdev_ops);
  728. state->sd.owner = THIS_MODULE;
  729. snprintf(state->sd.name, sizeof(state->sd.name), "%s.%d",
  730. CSIS_SUBDEV_NAME, state->index);
  731. state->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  732. state->csis_fmt = &s5pcsis_formats[0];
  733. state->format.code = s5pcsis_formats[0].code;
  734. state->format.width = S5PCSIS_DEF_PIX_WIDTH;
  735. state->format.height = S5PCSIS_DEF_PIX_HEIGHT;
  736. state->pads[CSIS_PAD_SINK].flags = MEDIA_PAD_FL_SINK;
  737. state->pads[CSIS_PAD_SOURCE].flags = MEDIA_PAD_FL_SOURCE;
  738. ret = media_entity_init(&state->sd.entity,
  739. CSIS_PADS_NUM, state->pads, 0);
  740. if (ret < 0)
  741. goto e_clkdis;
  742. /* This allows to retrieve the platform device id by the host driver */
  743. v4l2_set_subdevdata(&state->sd, pdev);
  744. /* .. and a pointer to the subdev. */
  745. platform_set_drvdata(pdev, &state->sd);
  746. memcpy(state->events, s5pcsis_events, sizeof(state->events));
  747. pm_runtime_enable(dev);
  748. if (!pm_runtime_enabled(dev)) {
  749. ret = s5pcsis_pm_resume(dev, true);
  750. if (ret < 0)
  751. goto e_m_ent;
  752. }
  753. dev_info(&pdev->dev, "lanes: %d, hs_settle: %d, wclk: %d, freq: %u\n",
  754. state->num_lanes, state->hs_settle, state->wclk_ext,
  755. state->clk_frequency);
  756. return 0;
  757. e_m_ent:
  758. media_entity_cleanup(&state->sd.entity);
  759. e_clkdis:
  760. clk_disable(state->clock[CSIS_CLK_MUX]);
  761. e_clkput:
  762. s5pcsis_clk_put(state);
  763. return ret;
  764. }
  765. static int s5pcsis_pm_suspend(struct device *dev, bool runtime)
  766. {
  767. struct platform_device *pdev = to_platform_device(dev);
  768. struct v4l2_subdev *sd = platform_get_drvdata(pdev);
  769. struct csis_state *state = sd_to_csis_state(sd);
  770. int ret = 0;
  771. v4l2_dbg(1, debug, sd, "%s: flags: 0x%x\n",
  772. __func__, state->flags);
  773. mutex_lock(&state->lock);
  774. if (state->flags & ST_POWERED) {
  775. s5pcsis_stop_stream(state);
  776. ret = phy_power_off(state->phy);
  777. if (ret)
  778. goto unlock;
  779. ret = regulator_bulk_disable(CSIS_NUM_SUPPLIES,
  780. state->supplies);
  781. if (ret)
  782. goto unlock;
  783. clk_disable(state->clock[CSIS_CLK_GATE]);
  784. state->flags &= ~ST_POWERED;
  785. if (!runtime)
  786. state->flags |= ST_SUSPENDED;
  787. }
  788. unlock:
  789. mutex_unlock(&state->lock);
  790. return ret ? -EAGAIN : 0;
  791. }
  792. static int s5pcsis_pm_resume(struct device *dev, bool runtime)
  793. {
  794. struct platform_device *pdev = to_platform_device(dev);
  795. struct v4l2_subdev *sd = platform_get_drvdata(pdev);
  796. struct csis_state *state = sd_to_csis_state(sd);
  797. int ret = 0;
  798. v4l2_dbg(1, debug, sd, "%s: flags: 0x%x\n",
  799. __func__, state->flags);
  800. mutex_lock(&state->lock);
  801. if (!runtime && !(state->flags & ST_SUSPENDED))
  802. goto unlock;
  803. if (!(state->flags & ST_POWERED)) {
  804. ret = regulator_bulk_enable(CSIS_NUM_SUPPLIES,
  805. state->supplies);
  806. if (ret)
  807. goto unlock;
  808. ret = phy_power_on(state->phy);
  809. if (!ret) {
  810. state->flags |= ST_POWERED;
  811. } else {
  812. regulator_bulk_disable(CSIS_NUM_SUPPLIES,
  813. state->supplies);
  814. goto unlock;
  815. }
  816. clk_enable(state->clock[CSIS_CLK_GATE]);
  817. }
  818. if (state->flags & ST_STREAMING)
  819. s5pcsis_start_stream(state);
  820. state->flags &= ~ST_SUSPENDED;
  821. unlock:
  822. mutex_unlock(&state->lock);
  823. return ret ? -EAGAIN : 0;
  824. }
  825. #ifdef CONFIG_PM_SLEEP
  826. static int s5pcsis_suspend(struct device *dev)
  827. {
  828. return s5pcsis_pm_suspend(dev, false);
  829. }
  830. static int s5pcsis_resume(struct device *dev)
  831. {
  832. return s5pcsis_pm_resume(dev, false);
  833. }
  834. #endif
  835. #ifdef CONFIG_PM
  836. static int s5pcsis_runtime_suspend(struct device *dev)
  837. {
  838. return s5pcsis_pm_suspend(dev, true);
  839. }
  840. static int s5pcsis_runtime_resume(struct device *dev)
  841. {
  842. return s5pcsis_pm_resume(dev, true);
  843. }
  844. #endif
  845. static int s5pcsis_remove(struct platform_device *pdev)
  846. {
  847. struct v4l2_subdev *sd = platform_get_drvdata(pdev);
  848. struct csis_state *state = sd_to_csis_state(sd);
  849. pm_runtime_disable(&pdev->dev);
  850. s5pcsis_pm_suspend(&pdev->dev, true);
  851. clk_disable(state->clock[CSIS_CLK_MUX]);
  852. pm_runtime_set_suspended(&pdev->dev);
  853. s5pcsis_clk_put(state);
  854. media_entity_cleanup(&state->sd.entity);
  855. return 0;
  856. }
  857. static const struct dev_pm_ops s5pcsis_pm_ops = {
  858. SET_RUNTIME_PM_OPS(s5pcsis_runtime_suspend, s5pcsis_runtime_resume,
  859. NULL)
  860. SET_SYSTEM_SLEEP_PM_OPS(s5pcsis_suspend, s5pcsis_resume)
  861. };
  862. static const struct csis_drvdata exynos4_csis_drvdata = {
  863. .interrupt_mask = S5PCSIS_INTMSK_EXYNOS4_EN_ALL,
  864. };
  865. static const struct csis_drvdata exynos5_csis_drvdata = {
  866. .interrupt_mask = S5PCSIS_INTMSK_EXYNOS5_EN_ALL,
  867. };
  868. static const struct of_device_id s5pcsis_of_match[] = {
  869. {
  870. .compatible = "samsung,s5pv210-csis",
  871. .data = &exynos4_csis_drvdata,
  872. }, {
  873. .compatible = "samsung,exynos4210-csis",
  874. .data = &exynos4_csis_drvdata,
  875. }, {
  876. .compatible = "samsung,exynos5250-csis",
  877. .data = &exynos5_csis_drvdata,
  878. },
  879. { /* sentinel */ },
  880. };
  881. MODULE_DEVICE_TABLE(of, s5pcsis_of_match);
  882. static struct platform_driver s5pcsis_driver = {
  883. .probe = s5pcsis_probe,
  884. .remove = s5pcsis_remove,
  885. .driver = {
  886. .of_match_table = s5pcsis_of_match,
  887. .name = CSIS_DRIVER_NAME,
  888. .pm = &s5pcsis_pm_ops,
  889. },
  890. };
  891. module_platform_driver(s5pcsis_driver);
  892. MODULE_AUTHOR("Sylwester Nawrocki <s.nawrocki@samsung.com>");
  893. MODULE_DESCRIPTION("Samsung S5P/EXYNOS SoC MIPI-CSI2 receiver driver");
  894. MODULE_LICENSE("GPL");