mipi-csis.c 28 KB

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