mipi-csis.c 24 KB

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