mipi-csis.c 27 KB

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