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 || debug)
  351. v4l2_info(&state->sd, "%s events: %d\n",
  352. state->events[i].name,
  353. state->events[i].counter);
  354. }
  355. spin_unlock_irqrestore(&state->slock, flags);
  356. }
  357. /*
  358. * V4L2 subdev operations
  359. */
  360. static int s5pcsis_s_power(struct v4l2_subdev *sd, int on)
  361. {
  362. struct csis_state *state = sd_to_csis_state(sd);
  363. struct device *dev = &state->pdev->dev;
  364. if (on)
  365. return pm_runtime_get_sync(dev);
  366. return pm_runtime_put_sync(dev);
  367. }
  368. static int s5pcsis_s_stream(struct v4l2_subdev *sd, int enable)
  369. {
  370. struct csis_state *state = sd_to_csis_state(sd);
  371. int ret = 0;
  372. v4l2_dbg(1, debug, sd, "%s: %d, state: 0x%x\n",
  373. __func__, enable, state->flags);
  374. if (enable) {
  375. s5pcsis_clear_counters(state);
  376. ret = pm_runtime_get_sync(&state->pdev->dev);
  377. if (ret && ret != 1)
  378. return ret;
  379. }
  380. mutex_lock(&state->lock);
  381. if (enable) {
  382. if (state->flags & ST_SUSPENDED) {
  383. ret = -EBUSY;
  384. goto unlock;
  385. }
  386. s5pcsis_start_stream(state);
  387. state->flags |= ST_STREAMING;
  388. } else {
  389. s5pcsis_stop_stream(state);
  390. state->flags &= ~ST_STREAMING;
  391. if (debug > 0)
  392. s5pcsis_log_counters(state, true);
  393. }
  394. unlock:
  395. mutex_unlock(&state->lock);
  396. if (!enable)
  397. pm_runtime_put(&state->pdev->dev);
  398. return ret == 1 ? 0 : ret;
  399. }
  400. static int s5pcsis_enum_mbus_code(struct v4l2_subdev *sd,
  401. struct v4l2_subdev_fh *fh,
  402. struct v4l2_subdev_mbus_code_enum *code)
  403. {
  404. if (code->index >= ARRAY_SIZE(s5pcsis_formats))
  405. return -EINVAL;
  406. code->code = s5pcsis_formats[code->index].code;
  407. return 0;
  408. }
  409. static struct csis_pix_format const *s5pcsis_try_format(
  410. struct v4l2_mbus_framefmt *mf)
  411. {
  412. struct csis_pix_format const *csis_fmt;
  413. csis_fmt = find_csis_format(mf);
  414. if (csis_fmt == NULL)
  415. csis_fmt = &s5pcsis_formats[0];
  416. mf->code = csis_fmt->code;
  417. v4l_bound_align_image(&mf->width, 1, CSIS_MAX_PIX_WIDTH,
  418. csis_fmt->pix_width_alignment,
  419. &mf->height, 1, CSIS_MAX_PIX_HEIGHT, 1,
  420. 0);
  421. return csis_fmt;
  422. }
  423. static struct v4l2_mbus_framefmt *__s5pcsis_get_format(
  424. struct csis_state *state, struct v4l2_subdev_fh *fh,
  425. u32 pad, enum v4l2_subdev_format_whence which)
  426. {
  427. if (which == V4L2_SUBDEV_FORMAT_TRY)
  428. return fh ? v4l2_subdev_get_try_format(fh, pad) : NULL;
  429. return &state->format;
  430. }
  431. static int s5pcsis_set_fmt(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh,
  432. struct v4l2_subdev_format *fmt)
  433. {
  434. struct csis_state *state = sd_to_csis_state(sd);
  435. struct csis_pix_format const *csis_fmt;
  436. struct v4l2_mbus_framefmt *mf;
  437. if (fmt->pad != CSIS_PAD_SOURCE && fmt->pad != CSIS_PAD_SINK)
  438. return -EINVAL;
  439. mf = __s5pcsis_get_format(state, fh, fmt->pad, fmt->which);
  440. if (fmt->pad == CSIS_PAD_SOURCE) {
  441. if (mf) {
  442. mutex_lock(&state->lock);
  443. fmt->format = *mf;
  444. mutex_unlock(&state->lock);
  445. }
  446. return 0;
  447. }
  448. csis_fmt = s5pcsis_try_format(&fmt->format);
  449. if (mf) {
  450. mutex_lock(&state->lock);
  451. *mf = fmt->format;
  452. if (fmt->which == V4L2_SUBDEV_FORMAT_ACTIVE)
  453. state->csis_fmt = csis_fmt;
  454. mutex_unlock(&state->lock);
  455. }
  456. return 0;
  457. }
  458. static int s5pcsis_get_fmt(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh,
  459. struct v4l2_subdev_format *fmt)
  460. {
  461. struct csis_state *state = sd_to_csis_state(sd);
  462. struct v4l2_mbus_framefmt *mf;
  463. if (fmt->pad != CSIS_PAD_SOURCE && fmt->pad != CSIS_PAD_SINK)
  464. return -EINVAL;
  465. mf = __s5pcsis_get_format(state, fh, fmt->pad, fmt->which);
  466. if (!mf)
  467. return -EINVAL;
  468. mutex_lock(&state->lock);
  469. fmt->format = *mf;
  470. mutex_unlock(&state->lock);
  471. return 0;
  472. }
  473. static int s5pcsis_s_rx_buffer(struct v4l2_subdev *sd, void *buf,
  474. unsigned int *size)
  475. {
  476. struct csis_state *state = sd_to_csis_state(sd);
  477. unsigned long flags;
  478. *size = min_t(unsigned int, *size, S5PCSIS_PKTDATA_SIZE);
  479. spin_lock_irqsave(&state->slock, flags);
  480. state->pkt_buf.data = buf;
  481. state->pkt_buf.len = *size;
  482. spin_unlock_irqrestore(&state->slock, flags);
  483. return 0;
  484. }
  485. static int s5pcsis_log_status(struct v4l2_subdev *sd)
  486. {
  487. struct csis_state *state = sd_to_csis_state(sd);
  488. s5pcsis_log_counters(state, true);
  489. return 0;
  490. }
  491. static int s5pcsis_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
  492. {
  493. struct v4l2_mbus_framefmt *format = v4l2_subdev_get_try_format(fh, 0);
  494. format->colorspace = V4L2_COLORSPACE_JPEG;
  495. format->code = s5pcsis_formats[0].code;
  496. format->width = S5PCSIS_DEF_PIX_WIDTH;
  497. format->height = S5PCSIS_DEF_PIX_HEIGHT;
  498. format->field = V4L2_FIELD_NONE;
  499. return 0;
  500. }
  501. static const struct v4l2_subdev_internal_ops s5pcsis_sd_internal_ops = {
  502. .open = s5pcsis_open,
  503. };
  504. static struct v4l2_subdev_core_ops s5pcsis_core_ops = {
  505. .s_power = s5pcsis_s_power,
  506. .log_status = s5pcsis_log_status,
  507. };
  508. static struct v4l2_subdev_pad_ops s5pcsis_pad_ops = {
  509. .enum_mbus_code = s5pcsis_enum_mbus_code,
  510. .get_fmt = s5pcsis_get_fmt,
  511. .set_fmt = s5pcsis_set_fmt,
  512. };
  513. static struct v4l2_subdev_video_ops s5pcsis_video_ops = {
  514. .s_rx_buffer = s5pcsis_s_rx_buffer,
  515. .s_stream = s5pcsis_s_stream,
  516. };
  517. static struct v4l2_subdev_ops s5pcsis_subdev_ops = {
  518. .core = &s5pcsis_core_ops,
  519. .pad = &s5pcsis_pad_ops,
  520. .video = &s5pcsis_video_ops,
  521. };
  522. static irqreturn_t s5pcsis_irq_handler(int irq, void *dev_id)
  523. {
  524. struct csis_state *state = dev_id;
  525. struct csis_pktbuf *pktbuf = &state->pkt_buf;
  526. unsigned long flags;
  527. u32 status;
  528. status = s5pcsis_read(state, S5PCSIS_INTSRC);
  529. spin_lock_irqsave(&state->slock, flags);
  530. if ((status & S5PCSIS_INTSRC_NON_IMAGE_DATA) && pktbuf->data) {
  531. u32 offset;
  532. if (status & S5PCSIS_INTSRC_EVEN)
  533. offset = S5PCSIS_PKTDATA_EVEN;
  534. else
  535. offset = S5PCSIS_PKTDATA_ODD;
  536. memcpy(pktbuf->data, state->regs + offset, pktbuf->len);
  537. pktbuf->data = NULL;
  538. rmb();
  539. }
  540. /* Update the event/error counters */
  541. if ((status & S5PCSIS_INTSRC_ERRORS) || debug) {
  542. int i;
  543. for (i = 0; i < S5PCSIS_NUM_EVENTS; i++) {
  544. if (!(status & state->events[i].mask))
  545. continue;
  546. state->events[i].counter++;
  547. v4l2_dbg(2, debug, &state->sd, "%s: %d\n",
  548. state->events[i].name,
  549. state->events[i].counter);
  550. }
  551. v4l2_dbg(2, debug, &state->sd, "status: %08x\n", status);
  552. }
  553. spin_unlock_irqrestore(&state->slock, flags);
  554. s5pcsis_write(state, S5PCSIS_INTSRC, status);
  555. return IRQ_HANDLED;
  556. }
  557. static int __devinit s5pcsis_probe(struct platform_device *pdev)
  558. {
  559. struct s5p_platform_mipi_csis *pdata;
  560. struct resource *mem_res;
  561. struct csis_state *state;
  562. int ret = -ENOMEM;
  563. int i;
  564. state = devm_kzalloc(&pdev->dev, sizeof(*state), GFP_KERNEL);
  565. if (!state)
  566. return -ENOMEM;
  567. mutex_init(&state->lock);
  568. spin_lock_init(&state->slock);
  569. state->pdev = pdev;
  570. state->index = max(0, pdev->id);
  571. pdata = pdev->dev.platform_data;
  572. if (pdata == NULL) {
  573. dev_err(&pdev->dev, "Platform data not fully specified\n");
  574. return -EINVAL;
  575. }
  576. if ((state->index == 1 && pdata->lanes > CSIS1_MAX_LANES) ||
  577. pdata->lanes > CSIS0_MAX_LANES) {
  578. dev_err(&pdev->dev, "Unsupported number of data lanes: %d\n",
  579. pdata->lanes);
  580. return -EINVAL;
  581. }
  582. mem_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  583. state->regs = devm_request_and_ioremap(&pdev->dev, mem_res);
  584. if (state->regs == NULL) {
  585. dev_err(&pdev->dev, "Failed to request and remap io memory\n");
  586. return -ENXIO;
  587. }
  588. state->irq = platform_get_irq(pdev, 0);
  589. if (state->irq < 0) {
  590. dev_err(&pdev->dev, "Failed to get irq\n");
  591. return state->irq;
  592. }
  593. for (i = 0; i < CSIS_NUM_SUPPLIES; i++)
  594. state->supplies[i].supply = csis_supply_name[i];
  595. ret = regulator_bulk_get(&pdev->dev, CSIS_NUM_SUPPLIES,
  596. state->supplies);
  597. if (ret)
  598. return ret;
  599. ret = s5pcsis_clk_get(state);
  600. if (ret)
  601. goto e_clkput;
  602. clk_enable(state->clock[CSIS_CLK_MUX]);
  603. if (pdata->clk_rate)
  604. clk_set_rate(state->clock[CSIS_CLK_MUX], pdata->clk_rate);
  605. else
  606. dev_WARN(&pdev->dev, "No clock frequency specified!\n");
  607. ret = devm_request_irq(&pdev->dev, state->irq, s5pcsis_irq_handler,
  608. 0, dev_name(&pdev->dev), state);
  609. if (ret) {
  610. dev_err(&pdev->dev, "Interrupt request failed\n");
  611. goto e_regput;
  612. }
  613. v4l2_subdev_init(&state->sd, &s5pcsis_subdev_ops);
  614. state->sd.owner = THIS_MODULE;
  615. strlcpy(state->sd.name, dev_name(&pdev->dev), sizeof(state->sd.name));
  616. state->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  617. state->csis_fmt = &s5pcsis_formats[0];
  618. state->format.code = s5pcsis_formats[0].code;
  619. state->format.width = S5PCSIS_DEF_PIX_WIDTH;
  620. state->format.height = S5PCSIS_DEF_PIX_HEIGHT;
  621. state->pads[CSIS_PAD_SINK].flags = MEDIA_PAD_FL_SINK;
  622. state->pads[CSIS_PAD_SOURCE].flags = MEDIA_PAD_FL_SOURCE;
  623. ret = media_entity_init(&state->sd.entity,
  624. CSIS_PADS_NUM, state->pads, 0);
  625. if (ret < 0)
  626. goto e_clkput;
  627. /* This allows to retrieve the platform device id by the host driver */
  628. v4l2_set_subdevdata(&state->sd, pdev);
  629. /* .. and a pointer to the subdev. */
  630. platform_set_drvdata(pdev, &state->sd);
  631. memcpy(state->events, s5pcsis_events, sizeof(state->events));
  632. pm_runtime_enable(&pdev->dev);
  633. return 0;
  634. e_regput:
  635. regulator_bulk_free(CSIS_NUM_SUPPLIES, state->supplies);
  636. e_clkput:
  637. clk_disable(state->clock[CSIS_CLK_MUX]);
  638. s5pcsis_clk_put(state);
  639. return ret;
  640. }
  641. static int s5pcsis_pm_suspend(struct device *dev, bool runtime)
  642. {
  643. struct platform_device *pdev = to_platform_device(dev);
  644. struct v4l2_subdev *sd = platform_get_drvdata(pdev);
  645. struct csis_state *state = sd_to_csis_state(sd);
  646. int ret = 0;
  647. v4l2_dbg(1, debug, sd, "%s: flags: 0x%x\n",
  648. __func__, state->flags);
  649. mutex_lock(&state->lock);
  650. if (state->flags & ST_POWERED) {
  651. s5pcsis_stop_stream(state);
  652. ret = s5p_csis_phy_enable(state->index, false);
  653. if (ret)
  654. goto unlock;
  655. ret = regulator_bulk_disable(CSIS_NUM_SUPPLIES,
  656. state->supplies);
  657. if (ret)
  658. goto unlock;
  659. clk_disable(state->clock[CSIS_CLK_GATE]);
  660. state->flags &= ~ST_POWERED;
  661. if (!runtime)
  662. state->flags |= ST_SUSPENDED;
  663. }
  664. unlock:
  665. mutex_unlock(&state->lock);
  666. return ret ? -EAGAIN : 0;
  667. }
  668. static int s5pcsis_pm_resume(struct device *dev, bool runtime)
  669. {
  670. struct platform_device *pdev = to_platform_device(dev);
  671. struct v4l2_subdev *sd = platform_get_drvdata(pdev);
  672. struct csis_state *state = sd_to_csis_state(sd);
  673. int ret = 0;
  674. v4l2_dbg(1, debug, sd, "%s: flags: 0x%x\n",
  675. __func__, state->flags);
  676. mutex_lock(&state->lock);
  677. if (!runtime && !(state->flags & ST_SUSPENDED))
  678. goto unlock;
  679. if (!(state->flags & ST_POWERED)) {
  680. ret = regulator_bulk_enable(CSIS_NUM_SUPPLIES,
  681. state->supplies);
  682. if (ret)
  683. goto unlock;
  684. ret = s5p_csis_phy_enable(state->index, true);
  685. if (!ret) {
  686. state->flags |= ST_POWERED;
  687. } else {
  688. regulator_bulk_disable(CSIS_NUM_SUPPLIES,
  689. state->supplies);
  690. goto unlock;
  691. }
  692. clk_enable(state->clock[CSIS_CLK_GATE]);
  693. }
  694. if (state->flags & ST_STREAMING)
  695. s5pcsis_start_stream(state);
  696. state->flags &= ~ST_SUSPENDED;
  697. unlock:
  698. mutex_unlock(&state->lock);
  699. return ret ? -EAGAIN : 0;
  700. }
  701. #ifdef CONFIG_PM_SLEEP
  702. static int s5pcsis_suspend(struct device *dev)
  703. {
  704. return s5pcsis_pm_suspend(dev, false);
  705. }
  706. static int s5pcsis_resume(struct device *dev)
  707. {
  708. return s5pcsis_pm_resume(dev, false);
  709. }
  710. #endif
  711. #ifdef CONFIG_PM_RUNTIME
  712. static int s5pcsis_runtime_suspend(struct device *dev)
  713. {
  714. return s5pcsis_pm_suspend(dev, true);
  715. }
  716. static int s5pcsis_runtime_resume(struct device *dev)
  717. {
  718. return s5pcsis_pm_resume(dev, true);
  719. }
  720. #endif
  721. static int __devexit s5pcsis_remove(struct platform_device *pdev)
  722. {
  723. struct v4l2_subdev *sd = platform_get_drvdata(pdev);
  724. struct csis_state *state = sd_to_csis_state(sd);
  725. pm_runtime_disable(&pdev->dev);
  726. s5pcsis_pm_suspend(&pdev->dev, false);
  727. clk_disable(state->clock[CSIS_CLK_MUX]);
  728. pm_runtime_set_suspended(&pdev->dev);
  729. s5pcsis_clk_put(state);
  730. regulator_bulk_free(CSIS_NUM_SUPPLIES, state->supplies);
  731. media_entity_cleanup(&state->sd.entity);
  732. return 0;
  733. }
  734. static const struct dev_pm_ops s5pcsis_pm_ops = {
  735. SET_RUNTIME_PM_OPS(s5pcsis_runtime_suspend, s5pcsis_runtime_resume,
  736. NULL)
  737. SET_SYSTEM_SLEEP_PM_OPS(s5pcsis_suspend, s5pcsis_resume)
  738. };
  739. static struct platform_driver s5pcsis_driver = {
  740. .probe = s5pcsis_probe,
  741. .remove = __devexit_p(s5pcsis_remove),
  742. .driver = {
  743. .name = CSIS_DRIVER_NAME,
  744. .owner = THIS_MODULE,
  745. .pm = &s5pcsis_pm_ops,
  746. },
  747. };
  748. module_platform_driver(s5pcsis_driver);
  749. MODULE_AUTHOR("Sylwester Nawrocki <s.nawrocki@samsung.com>");
  750. MODULE_DESCRIPTION("Samsung S5P/EXYNOS SoC MIPI-CSI2 receiver driver");
  751. MODULE_LICENSE("GPL");