mipi-csis.c 28 KB

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