mipi-csis.c 24 KB

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