mipi-csis.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891
  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. struct spinlock 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. };
  204. #define s5pcsis_write(__csis, __r, __v) writel(__v, __csis->regs + __r)
  205. #define s5pcsis_read(__csis, __r) readl(__csis->regs + __r)
  206. static struct csis_state *sd_to_csis_state(struct v4l2_subdev *sdev)
  207. {
  208. return container_of(sdev, struct csis_state, sd);
  209. }
  210. static const struct csis_pix_format *find_csis_format(
  211. struct v4l2_mbus_framefmt *mf)
  212. {
  213. int i;
  214. for (i = 0; i < ARRAY_SIZE(s5pcsis_formats); i++)
  215. if (mf->code == s5pcsis_formats[i].code)
  216. return &s5pcsis_formats[i];
  217. return NULL;
  218. }
  219. static void s5pcsis_enable_interrupts(struct csis_state *state, bool on)
  220. {
  221. u32 val = s5pcsis_read(state, S5PCSIS_INTMSK);
  222. val = on ? val | S5PCSIS_INTMSK_EN_ALL :
  223. val & ~S5PCSIS_INTMSK_EN_ALL;
  224. s5pcsis_write(state, S5PCSIS_INTMSK, val);
  225. }
  226. static void s5pcsis_reset(struct csis_state *state)
  227. {
  228. u32 val = s5pcsis_read(state, S5PCSIS_CTRL);
  229. s5pcsis_write(state, S5PCSIS_CTRL, val | S5PCSIS_CTRL_RESET);
  230. udelay(10);
  231. }
  232. static void s5pcsis_system_enable(struct csis_state *state, int on)
  233. {
  234. u32 val;
  235. val = s5pcsis_read(state, S5PCSIS_CTRL);
  236. if (on)
  237. val |= S5PCSIS_CTRL_ENABLE;
  238. else
  239. val &= ~S5PCSIS_CTRL_ENABLE;
  240. s5pcsis_write(state, S5PCSIS_CTRL, val);
  241. val = s5pcsis_read(state, S5PCSIS_DPHYCTRL);
  242. if (on)
  243. val |= S5PCSIS_DPHYCTRL_ENABLE;
  244. else
  245. val &= ~S5PCSIS_DPHYCTRL_ENABLE;
  246. s5pcsis_write(state, S5PCSIS_DPHYCTRL, val);
  247. }
  248. /* Called with the state.lock mutex held */
  249. static void __s5pcsis_set_format(struct csis_state *state)
  250. {
  251. struct v4l2_mbus_framefmt *mf = &state->format;
  252. u32 val;
  253. v4l2_dbg(1, debug, &state->sd, "fmt: %#x, %d x %d\n",
  254. mf->code, mf->width, mf->height);
  255. /* Color format */
  256. val = s5pcsis_read(state, S5PCSIS_CONFIG);
  257. val = (val & ~S5PCSIS_CFG_FMT_MASK) | state->csis_fmt->fmt_reg;
  258. s5pcsis_write(state, S5PCSIS_CONFIG, val);
  259. /* Pixel resolution */
  260. val = (mf->width << 16) | mf->height;
  261. s5pcsis_write(state, S5PCSIS_RESOL, val);
  262. }
  263. static void s5pcsis_set_hsync_settle(struct csis_state *state, int settle)
  264. {
  265. u32 val = s5pcsis_read(state, S5PCSIS_DPHYCTRL);
  266. val = (val & ~S5PCSIS_DPHYCTRL_HSS_MASK) | (settle << 27);
  267. s5pcsis_write(state, S5PCSIS_DPHYCTRL, val);
  268. }
  269. static void s5pcsis_set_params(struct csis_state *state)
  270. {
  271. struct s5p_platform_mipi_csis *pdata = state->pdev->dev.platform_data;
  272. u32 val;
  273. val = s5pcsis_read(state, S5PCSIS_CONFIG);
  274. val = (val & ~S5PCSIS_CFG_NR_LANE_MASK) | (pdata->lanes - 1);
  275. s5pcsis_write(state, S5PCSIS_CONFIG, val);
  276. __s5pcsis_set_format(state);
  277. s5pcsis_set_hsync_settle(state, pdata->hs_settle);
  278. val = s5pcsis_read(state, S5PCSIS_CTRL);
  279. if (state->csis_fmt->data_alignment == 32)
  280. val |= S5PCSIS_CTRL_ALIGN_32BIT;
  281. else /* 24-bits */
  282. val &= ~S5PCSIS_CTRL_ALIGN_32BIT;
  283. val &= ~S5PCSIS_CTRL_WCLK_EXTCLK;
  284. if (pdata->wclk_source)
  285. val |= S5PCSIS_CTRL_WCLK_EXTCLK;
  286. s5pcsis_write(state, S5PCSIS_CTRL, val);
  287. /* Update the shadow register. */
  288. val = s5pcsis_read(state, S5PCSIS_CTRL);
  289. s5pcsis_write(state, S5PCSIS_CTRL, val | S5PCSIS_CTRL_UPDATE_SHADOW);
  290. }
  291. static void s5pcsis_clk_put(struct csis_state *state)
  292. {
  293. int i;
  294. for (i = 0; i < NUM_CSIS_CLOCKS; i++) {
  295. if (IS_ERR_OR_NULL(state->clock[i]))
  296. continue;
  297. clk_unprepare(state->clock[i]);
  298. clk_put(state->clock[i]);
  299. state->clock[i] = NULL;
  300. }
  301. }
  302. static int s5pcsis_clk_get(struct csis_state *state)
  303. {
  304. struct device *dev = &state->pdev->dev;
  305. int i, ret;
  306. for (i = 0; i < NUM_CSIS_CLOCKS; i++) {
  307. state->clock[i] = clk_get(dev, csi_clock_name[i]);
  308. if (IS_ERR(state->clock[i]))
  309. goto err;
  310. ret = clk_prepare(state->clock[i]);
  311. if (ret < 0) {
  312. clk_put(state->clock[i]);
  313. state->clock[i] = NULL;
  314. goto err;
  315. }
  316. }
  317. return 0;
  318. err:
  319. s5pcsis_clk_put(state);
  320. dev_err(dev, "failed to get clock: %s\n", csi_clock_name[i]);
  321. return -ENXIO;
  322. }
  323. static void s5pcsis_start_stream(struct csis_state *state)
  324. {
  325. s5pcsis_reset(state);
  326. s5pcsis_set_params(state);
  327. s5pcsis_system_enable(state, true);
  328. s5pcsis_enable_interrupts(state, true);
  329. }
  330. static void s5pcsis_stop_stream(struct csis_state *state)
  331. {
  332. s5pcsis_enable_interrupts(state, false);
  333. s5pcsis_system_enable(state, false);
  334. }
  335. static void s5pcsis_clear_counters(struct csis_state *state)
  336. {
  337. unsigned long flags;
  338. int i;
  339. spin_lock_irqsave(&state->slock, flags);
  340. for (i = 0; i < S5PCSIS_NUM_EVENTS; i++)
  341. state->events[i].counter = 0;
  342. spin_unlock_irqrestore(&state->slock, flags);
  343. }
  344. static void s5pcsis_log_counters(struct csis_state *state, bool non_errors)
  345. {
  346. int i = non_errors ? S5PCSIS_NUM_EVENTS : S5PCSIS_NUM_EVENTS - 4;
  347. unsigned long flags;
  348. spin_lock_irqsave(&state->slock, flags);
  349. for (i--; i >= 0; i--)
  350. if (state->events[i].counter >= 0)
  351. v4l2_info(&state->sd, "%s events: %d\n",
  352. state->events[i].name,
  353. state->events[i].counter);
  354. spin_unlock_irqrestore(&state->slock, flags);
  355. }
  356. /*
  357. * V4L2 subdev operations
  358. */
  359. static int s5pcsis_s_power(struct v4l2_subdev *sd, int on)
  360. {
  361. struct csis_state *state = sd_to_csis_state(sd);
  362. struct device *dev = &state->pdev->dev;
  363. if (on)
  364. return pm_runtime_get_sync(dev);
  365. return pm_runtime_put_sync(dev);
  366. }
  367. static int s5pcsis_s_stream(struct v4l2_subdev *sd, int enable)
  368. {
  369. struct csis_state *state = sd_to_csis_state(sd);
  370. int ret = 0;
  371. v4l2_dbg(1, debug, sd, "%s: %d, state: 0x%x\n",
  372. __func__, enable, state->flags);
  373. if (enable) {
  374. s5pcsis_clear_counters(state);
  375. ret = pm_runtime_get_sync(&state->pdev->dev);
  376. if (ret && ret != 1)
  377. return ret;
  378. }
  379. mutex_lock(&state->lock);
  380. if (enable) {
  381. if (state->flags & ST_SUSPENDED) {
  382. ret = -EBUSY;
  383. goto unlock;
  384. }
  385. s5pcsis_start_stream(state);
  386. state->flags |= ST_STREAMING;
  387. } else {
  388. s5pcsis_stop_stream(state);
  389. state->flags &= ~ST_STREAMING;
  390. if (debug > 0)
  391. s5pcsis_log_counters(state, true);
  392. }
  393. unlock:
  394. mutex_unlock(&state->lock);
  395. if (!enable)
  396. pm_runtime_put(&state->pdev->dev);
  397. return ret == 1 ? 0 : ret;
  398. }
  399. static int s5pcsis_enum_mbus_code(struct v4l2_subdev *sd,
  400. struct v4l2_subdev_fh *fh,
  401. struct v4l2_subdev_mbus_code_enum *code)
  402. {
  403. if (code->index >= ARRAY_SIZE(s5pcsis_formats))
  404. return -EINVAL;
  405. code->code = s5pcsis_formats[code->index].code;
  406. return 0;
  407. }
  408. static struct csis_pix_format const *s5pcsis_try_format(
  409. struct v4l2_mbus_framefmt *mf)
  410. {
  411. struct csis_pix_format const *csis_fmt;
  412. csis_fmt = find_csis_format(mf);
  413. if (csis_fmt == NULL)
  414. csis_fmt = &s5pcsis_formats[0];
  415. mf->code = csis_fmt->code;
  416. v4l_bound_align_image(&mf->width, 1, CSIS_MAX_PIX_WIDTH,
  417. csis_fmt->pix_width_alignment,
  418. &mf->height, 1, CSIS_MAX_PIX_HEIGHT, 1,
  419. 0);
  420. return csis_fmt;
  421. }
  422. static struct v4l2_mbus_framefmt *__s5pcsis_get_format(
  423. struct csis_state *state, struct v4l2_subdev_fh *fh,
  424. u32 pad, enum v4l2_subdev_format_whence which)
  425. {
  426. if (which == V4L2_SUBDEV_FORMAT_TRY)
  427. return fh ? v4l2_subdev_get_try_format(fh, pad) : NULL;
  428. return &state->format;
  429. }
  430. static int s5pcsis_set_fmt(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh,
  431. struct v4l2_subdev_format *fmt)
  432. {
  433. struct csis_state *state = sd_to_csis_state(sd);
  434. struct csis_pix_format const *csis_fmt;
  435. struct v4l2_mbus_framefmt *mf;
  436. if (fmt->pad != CSIS_PAD_SOURCE && fmt->pad != CSIS_PAD_SINK)
  437. return -EINVAL;
  438. mf = __s5pcsis_get_format(state, fh, fmt->pad, fmt->which);
  439. if (fmt->pad == CSIS_PAD_SOURCE) {
  440. if (mf) {
  441. mutex_lock(&state->lock);
  442. fmt->format = *mf;
  443. mutex_unlock(&state->lock);
  444. }
  445. return 0;
  446. }
  447. csis_fmt = s5pcsis_try_format(&fmt->format);
  448. if (mf) {
  449. mutex_lock(&state->lock);
  450. *mf = fmt->format;
  451. if (fmt->which == V4L2_SUBDEV_FORMAT_ACTIVE)
  452. state->csis_fmt = csis_fmt;
  453. mutex_unlock(&state->lock);
  454. }
  455. return 0;
  456. }
  457. static int s5pcsis_get_fmt(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh,
  458. struct v4l2_subdev_format *fmt)
  459. {
  460. struct csis_state *state = sd_to_csis_state(sd);
  461. struct v4l2_mbus_framefmt *mf;
  462. if (fmt->pad != CSIS_PAD_SOURCE && fmt->pad != CSIS_PAD_SINK)
  463. return -EINVAL;
  464. mf = __s5pcsis_get_format(state, fh, fmt->pad, fmt->which);
  465. if (!mf)
  466. return -EINVAL;
  467. mutex_lock(&state->lock);
  468. fmt->format = *mf;
  469. mutex_unlock(&state->lock);
  470. return 0;
  471. }
  472. static int s5pcsis_s_rx_buffer(struct v4l2_subdev *sd, void *buf,
  473. unsigned int *size)
  474. {
  475. struct csis_state *state = sd_to_csis_state(sd);
  476. unsigned long flags;
  477. *size = min_t(unsigned int, *size, S5PCSIS_PKTDATA_SIZE);
  478. spin_lock_irqsave(&state->slock, flags);
  479. state->pkt_buf.data = buf;
  480. state->pkt_buf.len = *size;
  481. spin_unlock_irqrestore(&state->slock, flags);
  482. return 0;
  483. }
  484. static int s5pcsis_log_status(struct v4l2_subdev *sd)
  485. {
  486. struct csis_state *state = sd_to_csis_state(sd);
  487. s5pcsis_log_counters(state, true);
  488. return 0;
  489. }
  490. static int s5pcsis_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
  491. {
  492. struct v4l2_mbus_framefmt *format = v4l2_subdev_get_try_format(fh, 0);
  493. format->colorspace = V4L2_COLORSPACE_JPEG;
  494. format->code = s5pcsis_formats[0].code;
  495. format->width = S5PCSIS_DEF_PIX_WIDTH;
  496. format->height = S5PCSIS_DEF_PIX_HEIGHT;
  497. format->field = V4L2_FIELD_NONE;
  498. return 0;
  499. }
  500. static const struct v4l2_subdev_internal_ops s5pcsis_sd_internal_ops = {
  501. .open = s5pcsis_open,
  502. };
  503. static struct v4l2_subdev_core_ops s5pcsis_core_ops = {
  504. .s_power = s5pcsis_s_power,
  505. .log_status = s5pcsis_log_status,
  506. };
  507. static struct v4l2_subdev_pad_ops s5pcsis_pad_ops = {
  508. .enum_mbus_code = s5pcsis_enum_mbus_code,
  509. .get_fmt = s5pcsis_get_fmt,
  510. .set_fmt = s5pcsis_set_fmt,
  511. };
  512. static struct v4l2_subdev_video_ops s5pcsis_video_ops = {
  513. .s_rx_buffer = s5pcsis_s_rx_buffer,
  514. .s_stream = s5pcsis_s_stream,
  515. };
  516. static struct v4l2_subdev_ops s5pcsis_subdev_ops = {
  517. .core = &s5pcsis_core_ops,
  518. .pad = &s5pcsis_pad_ops,
  519. .video = &s5pcsis_video_ops,
  520. };
  521. static irqreturn_t s5pcsis_irq_handler(int irq, void *dev_id)
  522. {
  523. struct csis_state *state = dev_id;
  524. struct csis_pktbuf *pktbuf = &state->pkt_buf;
  525. unsigned long flags;
  526. u32 status;
  527. status = s5pcsis_read(state, S5PCSIS_INTSRC);
  528. spin_lock_irqsave(&state->slock, flags);
  529. if ((status & S5PCSIS_INTSRC_NON_IMAGE_DATA) && pktbuf->data) {
  530. u32 offset;
  531. if (status & S5PCSIS_INTSRC_EVEN)
  532. offset = S5PCSIS_PKTDATA_EVEN;
  533. else
  534. offset = S5PCSIS_PKTDATA_ODD;
  535. memcpy(pktbuf->data, state->regs + offset, pktbuf->len);
  536. pktbuf->data = NULL;
  537. rmb();
  538. }
  539. /* Update the event/error counters */
  540. if ((status & S5PCSIS_INTSRC_ERRORS) || debug) {
  541. int i;
  542. for (i = 0; i < S5PCSIS_NUM_EVENTS; i++) {
  543. if (!(status & state->events[i].mask))
  544. continue;
  545. state->events[i].counter++;
  546. v4l2_dbg(2, debug, &state->sd, "%s: %d\n",
  547. state->events[i].name,
  548. state->events[i].counter);
  549. }
  550. v4l2_dbg(2, debug, &state->sd, "status: %08x\n", status);
  551. }
  552. spin_unlock_irqrestore(&state->slock, flags);
  553. s5pcsis_write(state, S5PCSIS_INTSRC, status);
  554. return IRQ_HANDLED;
  555. }
  556. static int s5pcsis_probe(struct platform_device *pdev)
  557. {
  558. struct s5p_platform_mipi_csis *pdata;
  559. struct resource *mem_res;
  560. struct csis_state *state;
  561. int ret = -ENOMEM;
  562. int i;
  563. state = devm_kzalloc(&pdev->dev, sizeof(*state), GFP_KERNEL);
  564. if (!state)
  565. return -ENOMEM;
  566. mutex_init(&state->lock);
  567. spin_lock_init(&state->slock);
  568. state->pdev = pdev;
  569. state->index = max(0, pdev->id);
  570. pdata = pdev->dev.platform_data;
  571. if (pdata == NULL) {
  572. dev_err(&pdev->dev, "Platform data not fully specified\n");
  573. return -EINVAL;
  574. }
  575. if ((state->index == 1 && pdata->lanes > CSIS1_MAX_LANES) ||
  576. pdata->lanes > CSIS0_MAX_LANES) {
  577. dev_err(&pdev->dev, "Unsupported number of data lanes: %d\n",
  578. pdata->lanes);
  579. return -EINVAL;
  580. }
  581. mem_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  582. state->regs = devm_request_and_ioremap(&pdev->dev, mem_res);
  583. if (state->regs == NULL) {
  584. dev_err(&pdev->dev, "Failed to request and remap io memory\n");
  585. return -ENXIO;
  586. }
  587. state->irq = platform_get_irq(pdev, 0);
  588. if (state->irq < 0) {
  589. dev_err(&pdev->dev, "Failed to get irq\n");
  590. return state->irq;
  591. }
  592. for (i = 0; i < CSIS_NUM_SUPPLIES; i++)
  593. state->supplies[i].supply = csis_supply_name[i];
  594. ret = regulator_bulk_get(&pdev->dev, CSIS_NUM_SUPPLIES,
  595. state->supplies);
  596. if (ret)
  597. return ret;
  598. ret = s5pcsis_clk_get(state);
  599. if (ret)
  600. goto e_clkput;
  601. clk_enable(state->clock[CSIS_CLK_MUX]);
  602. if (pdata->clk_rate)
  603. clk_set_rate(state->clock[CSIS_CLK_MUX], pdata->clk_rate);
  604. else
  605. dev_WARN(&pdev->dev, "No clock frequency specified!\n");
  606. ret = devm_request_irq(&pdev->dev, state->irq, s5pcsis_irq_handler,
  607. 0, dev_name(&pdev->dev), state);
  608. if (ret) {
  609. dev_err(&pdev->dev, "Interrupt request failed\n");
  610. goto e_regput;
  611. }
  612. v4l2_subdev_init(&state->sd, &s5pcsis_subdev_ops);
  613. state->sd.owner = THIS_MODULE;
  614. strlcpy(state->sd.name, dev_name(&pdev->dev), sizeof(state->sd.name));
  615. state->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  616. state->csis_fmt = &s5pcsis_formats[0];
  617. state->format.code = s5pcsis_formats[0].code;
  618. state->format.width = S5PCSIS_DEF_PIX_WIDTH;
  619. state->format.height = S5PCSIS_DEF_PIX_HEIGHT;
  620. state->pads[CSIS_PAD_SINK].flags = MEDIA_PAD_FL_SINK;
  621. state->pads[CSIS_PAD_SOURCE].flags = MEDIA_PAD_FL_SOURCE;
  622. ret = media_entity_init(&state->sd.entity,
  623. CSIS_PADS_NUM, state->pads, 0);
  624. if (ret < 0)
  625. goto e_clkput;
  626. /* This allows to retrieve the platform device id by the host driver */
  627. v4l2_set_subdevdata(&state->sd, pdev);
  628. /* .. and a pointer to the subdev. */
  629. platform_set_drvdata(pdev, &state->sd);
  630. memcpy(state->events, s5pcsis_events, sizeof(state->events));
  631. pm_runtime_enable(&pdev->dev);
  632. return 0;
  633. e_regput:
  634. regulator_bulk_free(CSIS_NUM_SUPPLIES, state->supplies);
  635. e_clkput:
  636. clk_disable(state->clock[CSIS_CLK_MUX]);
  637. s5pcsis_clk_put(state);
  638. return ret;
  639. }
  640. static int s5pcsis_pm_suspend(struct device *dev, bool runtime)
  641. {
  642. struct platform_device *pdev = to_platform_device(dev);
  643. struct v4l2_subdev *sd = platform_get_drvdata(pdev);
  644. struct csis_state *state = sd_to_csis_state(sd);
  645. int ret = 0;
  646. v4l2_dbg(1, debug, sd, "%s: flags: 0x%x\n",
  647. __func__, state->flags);
  648. mutex_lock(&state->lock);
  649. if (state->flags & ST_POWERED) {
  650. s5pcsis_stop_stream(state);
  651. ret = s5p_csis_phy_enable(state->index, false);
  652. if (ret)
  653. goto unlock;
  654. ret = regulator_bulk_disable(CSIS_NUM_SUPPLIES,
  655. state->supplies);
  656. if (ret)
  657. goto unlock;
  658. clk_disable(state->clock[CSIS_CLK_GATE]);
  659. state->flags &= ~ST_POWERED;
  660. if (!runtime)
  661. state->flags |= ST_SUSPENDED;
  662. }
  663. unlock:
  664. mutex_unlock(&state->lock);
  665. return ret ? -EAGAIN : 0;
  666. }
  667. static int s5pcsis_pm_resume(struct device *dev, bool runtime)
  668. {
  669. struct platform_device *pdev = to_platform_device(dev);
  670. struct v4l2_subdev *sd = platform_get_drvdata(pdev);
  671. struct csis_state *state = sd_to_csis_state(sd);
  672. int ret = 0;
  673. v4l2_dbg(1, debug, sd, "%s: flags: 0x%x\n",
  674. __func__, state->flags);
  675. mutex_lock(&state->lock);
  676. if (!runtime && !(state->flags & ST_SUSPENDED))
  677. goto unlock;
  678. if (!(state->flags & ST_POWERED)) {
  679. ret = regulator_bulk_enable(CSIS_NUM_SUPPLIES,
  680. state->supplies);
  681. if (ret)
  682. goto unlock;
  683. ret = s5p_csis_phy_enable(state->index, true);
  684. if (!ret) {
  685. state->flags |= ST_POWERED;
  686. } else {
  687. regulator_bulk_disable(CSIS_NUM_SUPPLIES,
  688. state->supplies);
  689. goto unlock;
  690. }
  691. clk_enable(state->clock[CSIS_CLK_GATE]);
  692. }
  693. if (state->flags & ST_STREAMING)
  694. s5pcsis_start_stream(state);
  695. state->flags &= ~ST_SUSPENDED;
  696. unlock:
  697. mutex_unlock(&state->lock);
  698. return ret ? -EAGAIN : 0;
  699. }
  700. #ifdef CONFIG_PM_SLEEP
  701. static int s5pcsis_suspend(struct device *dev)
  702. {
  703. return s5pcsis_pm_suspend(dev, false);
  704. }
  705. static int s5pcsis_resume(struct device *dev)
  706. {
  707. return s5pcsis_pm_resume(dev, false);
  708. }
  709. #endif
  710. #ifdef CONFIG_PM_RUNTIME
  711. static int s5pcsis_runtime_suspend(struct device *dev)
  712. {
  713. return s5pcsis_pm_suspend(dev, true);
  714. }
  715. static int s5pcsis_runtime_resume(struct device *dev)
  716. {
  717. return s5pcsis_pm_resume(dev, true);
  718. }
  719. #endif
  720. static int s5pcsis_remove(struct platform_device *pdev)
  721. {
  722. struct v4l2_subdev *sd = platform_get_drvdata(pdev);
  723. struct csis_state *state = sd_to_csis_state(sd);
  724. pm_runtime_disable(&pdev->dev);
  725. s5pcsis_pm_suspend(&pdev->dev, false);
  726. clk_disable(state->clock[CSIS_CLK_MUX]);
  727. pm_runtime_set_suspended(&pdev->dev);
  728. s5pcsis_clk_put(state);
  729. regulator_bulk_free(CSIS_NUM_SUPPLIES, state->supplies);
  730. media_entity_cleanup(&state->sd.entity);
  731. return 0;
  732. }
  733. static const struct dev_pm_ops s5pcsis_pm_ops = {
  734. SET_RUNTIME_PM_OPS(s5pcsis_runtime_suspend, s5pcsis_runtime_resume,
  735. NULL)
  736. SET_SYSTEM_SLEEP_PM_OPS(s5pcsis_suspend, s5pcsis_resume)
  737. };
  738. static struct platform_driver s5pcsis_driver = {
  739. .probe = s5pcsis_probe,
  740. .remove = s5pcsis_remove,
  741. .driver = {
  742. .name = CSIS_DRIVER_NAME,
  743. .owner = THIS_MODULE,
  744. .pm = &s5pcsis_pm_ops,
  745. },
  746. };
  747. module_platform_driver(s5pcsis_driver);
  748. MODULE_AUTHOR("Sylwester Nawrocki <s.nawrocki@samsung.com>");
  749. MODULE_DESCRIPTION("Samsung S5P/EXYNOS SoC MIPI-CSI2 receiver driver");
  750. MODULE_LICENSE("GPL");