mipi-csis.c 26 KB

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