atmel-isc.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521
  1. /*
  2. * Atmel Image Sensor Controller (ISC) driver
  3. *
  4. * Copyright (C) 2016 Atmel
  5. *
  6. * Author: Songjun Wu <songjun.wu@microchip.com>
  7. *
  8. * This program is free software; you may redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; version 2 of the License.
  11. *
  12. * Sensor-->PFE-->WB-->CFA-->CC-->GAM-->CSC-->CBC-->SUB-->RLP-->DMA
  13. *
  14. * ISC video pipeline integrates the following submodules:
  15. * PFE: Parallel Front End to sample the camera sensor input stream
  16. * WB: Programmable white balance in the Bayer domain
  17. * CFA: Color filter array interpolation module
  18. * CC: Programmable color correction
  19. * GAM: Gamma correction
  20. * CSC: Programmable color space conversion
  21. * CBC: Contrast and Brightness control
  22. * SUB: This module performs YCbCr444 to YCbCr420 chrominance subsampling
  23. * RLP: This module performs rounding, range limiting
  24. * and packing of the incoming data
  25. */
  26. #include <linux/clk.h>
  27. #include <linux/clkdev.h>
  28. #include <linux/clk-provider.h>
  29. #include <linux/delay.h>
  30. #include <linux/interrupt.h>
  31. #include <linux/module.h>
  32. #include <linux/of.h>
  33. #include <linux/platform_device.h>
  34. #include <linux/pm_runtime.h>
  35. #include <linux/regmap.h>
  36. #include <linux/videodev2.h>
  37. #include <media/v4l2-device.h>
  38. #include <media/v4l2-image-sizes.h>
  39. #include <media/v4l2-ioctl.h>
  40. #include <media/v4l2-of.h>
  41. #include <media/v4l2-subdev.h>
  42. #include <media/videobuf2-dma-contig.h>
  43. #include "atmel-isc-regs.h"
  44. #define ATMEL_ISC_NAME "atmel_isc"
  45. #define ISC_MAX_SUPPORT_WIDTH 2592
  46. #define ISC_MAX_SUPPORT_HEIGHT 1944
  47. #define ISC_CLK_MAX_DIV 255
  48. enum isc_clk_id {
  49. ISC_ISPCK = 0,
  50. ISC_MCK = 1,
  51. };
  52. struct isc_clk {
  53. struct clk_hw hw;
  54. struct clk *clk;
  55. struct regmap *regmap;
  56. u8 id;
  57. u8 parent_id;
  58. u32 div;
  59. struct device *dev;
  60. };
  61. #define to_isc_clk(hw) container_of(hw, struct isc_clk, hw)
  62. struct isc_buffer {
  63. struct vb2_v4l2_buffer vb;
  64. struct list_head list;
  65. };
  66. struct isc_subdev_entity {
  67. struct v4l2_subdev *sd;
  68. struct v4l2_async_subdev *asd;
  69. struct v4l2_async_notifier notifier;
  70. struct v4l2_subdev_pad_config *config;
  71. u32 pfe_cfg0;
  72. struct list_head list;
  73. };
  74. /*
  75. * struct isc_format - ISC media bus format information
  76. * @fourcc: Fourcc code for this format
  77. * @mbus_code: V4L2 media bus format code.
  78. * @bpp: Bytes per pixel (when stored in memory)
  79. * @reg_bps: reg value for bits per sample
  80. * (when transferred over a bus)
  81. * @support: Indicates format supported by subdev
  82. */
  83. struct isc_format {
  84. u32 fourcc;
  85. u32 mbus_code;
  86. u8 bpp;
  87. u32 reg_bps;
  88. u32 reg_rlp_mode;
  89. u32 reg_dcfg_imode;
  90. u32 reg_dctrl_dview;
  91. bool support;
  92. };
  93. #define ISC_PIPE_LINE_NODE_NUM 11
  94. struct isc_device {
  95. struct regmap *regmap;
  96. struct clk *hclock;
  97. struct clk *ispck;
  98. struct isc_clk isc_clks[2];
  99. struct device *dev;
  100. struct v4l2_device v4l2_dev;
  101. struct video_device video_dev;
  102. struct vb2_queue vb2_vidq;
  103. spinlock_t dma_queue_lock;
  104. struct list_head dma_queue;
  105. struct isc_buffer *cur_frm;
  106. unsigned int sequence;
  107. bool stop;
  108. struct completion comp;
  109. struct v4l2_format fmt;
  110. struct isc_format **user_formats;
  111. unsigned int num_user_formats;
  112. const struct isc_format *current_fmt;
  113. struct mutex lock;
  114. struct regmap_field *pipeline[ISC_PIPE_LINE_NODE_NUM];
  115. struct isc_subdev_entity *current_subdev;
  116. struct list_head subdev_entities;
  117. };
  118. static struct isc_format isc_formats[] = {
  119. { V4L2_PIX_FMT_SBGGR8, MEDIA_BUS_FMT_SBGGR8_1X8,
  120. 1, ISC_PFE_CFG0_BPS_EIGHT, ISC_RLP_CFG_MODE_DAT8,
  121. ISC_DCFG_IMODE_PACKED8, ISC_DCTRL_DVIEW_PACKED, false },
  122. { V4L2_PIX_FMT_SGBRG8, MEDIA_BUS_FMT_SGBRG8_1X8,
  123. 1, ISC_PFE_CFG0_BPS_EIGHT, ISC_RLP_CFG_MODE_DAT8,
  124. ISC_DCFG_IMODE_PACKED8, ISC_DCTRL_DVIEW_PACKED, false },
  125. { V4L2_PIX_FMT_SGRBG8, MEDIA_BUS_FMT_SGRBG8_1X8,
  126. 1, ISC_PFE_CFG0_BPS_EIGHT, ISC_RLP_CFG_MODE_DAT8,
  127. ISC_DCFG_IMODE_PACKED8, ISC_DCTRL_DVIEW_PACKED, false },
  128. { V4L2_PIX_FMT_SRGGB8, MEDIA_BUS_FMT_SRGGB8_1X8,
  129. 1, ISC_PFE_CFG0_BPS_EIGHT, ISC_RLP_CFG_MODE_DAT8,
  130. ISC_DCFG_IMODE_PACKED8, ISC_DCTRL_DVIEW_PACKED, false },
  131. { V4L2_PIX_FMT_SBGGR10, MEDIA_BUS_FMT_SBGGR10_1X10,
  132. 2, ISC_PFG_CFG0_BPS_TEN, ISC_RLP_CFG_MODE_DAT10,
  133. ISC_DCFG_IMODE_PACKED16, ISC_DCTRL_DVIEW_PACKED, false },
  134. { V4L2_PIX_FMT_SGBRG10, MEDIA_BUS_FMT_SGBRG10_1X10,
  135. 2, ISC_PFG_CFG0_BPS_TEN, ISC_RLP_CFG_MODE_DAT10,
  136. ISC_DCFG_IMODE_PACKED16, ISC_DCTRL_DVIEW_PACKED, false },
  137. { V4L2_PIX_FMT_SGRBG10, MEDIA_BUS_FMT_SGRBG10_1X10,
  138. 2, ISC_PFG_CFG0_BPS_TEN, ISC_RLP_CFG_MODE_DAT10,
  139. ISC_DCFG_IMODE_PACKED16, ISC_DCTRL_DVIEW_PACKED, false },
  140. { V4L2_PIX_FMT_SRGGB10, MEDIA_BUS_FMT_SRGGB10_1X10,
  141. 2, ISC_PFG_CFG0_BPS_TEN, ISC_RLP_CFG_MODE_DAT10,
  142. ISC_DCFG_IMODE_PACKED16, ISC_DCTRL_DVIEW_PACKED, false },
  143. { V4L2_PIX_FMT_SBGGR12, MEDIA_BUS_FMT_SBGGR12_1X12,
  144. 2, ISC_PFG_CFG0_BPS_TWELVE, ISC_RLP_CFG_MODE_DAT12,
  145. ISC_DCFG_IMODE_PACKED16, ISC_DCTRL_DVIEW_PACKED, false },
  146. { V4L2_PIX_FMT_SGBRG12, MEDIA_BUS_FMT_SGBRG12_1X12,
  147. 2, ISC_PFG_CFG0_BPS_TWELVE, ISC_RLP_CFG_MODE_DAT12,
  148. ISC_DCFG_IMODE_PACKED16, ISC_DCTRL_DVIEW_PACKED, false },
  149. { V4L2_PIX_FMT_SGRBG12, MEDIA_BUS_FMT_SGRBG12_1X12,
  150. 2, ISC_PFG_CFG0_BPS_TWELVE, ISC_RLP_CFG_MODE_DAT12,
  151. ISC_DCFG_IMODE_PACKED16, ISC_DCTRL_DVIEW_PACKED, false },
  152. { V4L2_PIX_FMT_SRGGB12, MEDIA_BUS_FMT_SRGGB12_1X12,
  153. 2, ISC_PFG_CFG0_BPS_TWELVE, ISC_RLP_CFG_MODE_DAT12,
  154. ISC_DCFG_IMODE_PACKED16, ISC_DCTRL_DVIEW_PACKED, false },
  155. { V4L2_PIX_FMT_YUYV, MEDIA_BUS_FMT_YUYV8_2X8,
  156. 2, ISC_PFE_CFG0_BPS_EIGHT, ISC_RLP_CFG_MODE_DAT8,
  157. ISC_DCFG_IMODE_PACKED8, ISC_DCTRL_DVIEW_PACKED, false },
  158. };
  159. static int isc_clk_enable(struct clk_hw *hw)
  160. {
  161. struct isc_clk *isc_clk = to_isc_clk(hw);
  162. u32 id = isc_clk->id;
  163. struct regmap *regmap = isc_clk->regmap;
  164. dev_dbg(isc_clk->dev, "ISC CLK: %s, div = %d, parent id = %d\n",
  165. __func__, isc_clk->div, isc_clk->parent_id);
  166. regmap_update_bits(regmap, ISC_CLKCFG,
  167. ISC_CLKCFG_DIV_MASK(id) | ISC_CLKCFG_SEL_MASK(id),
  168. (isc_clk->div << ISC_CLKCFG_DIV_SHIFT(id)) |
  169. (isc_clk->parent_id << ISC_CLKCFG_SEL_SHIFT(id)));
  170. regmap_write(regmap, ISC_CLKEN, ISC_CLK(id));
  171. return 0;
  172. }
  173. static void isc_clk_disable(struct clk_hw *hw)
  174. {
  175. struct isc_clk *isc_clk = to_isc_clk(hw);
  176. u32 id = isc_clk->id;
  177. regmap_write(isc_clk->regmap, ISC_CLKDIS, ISC_CLK(id));
  178. }
  179. static int isc_clk_is_enabled(struct clk_hw *hw)
  180. {
  181. struct isc_clk *isc_clk = to_isc_clk(hw);
  182. u32 status;
  183. regmap_read(isc_clk->regmap, ISC_CLKSR, &status);
  184. return status & ISC_CLK(isc_clk->id) ? 1 : 0;
  185. }
  186. static unsigned long
  187. isc_clk_recalc_rate(struct clk_hw *hw, unsigned long parent_rate)
  188. {
  189. struct isc_clk *isc_clk = to_isc_clk(hw);
  190. return DIV_ROUND_CLOSEST(parent_rate, isc_clk->div + 1);
  191. }
  192. static int isc_clk_determine_rate(struct clk_hw *hw,
  193. struct clk_rate_request *req)
  194. {
  195. struct isc_clk *isc_clk = to_isc_clk(hw);
  196. long best_rate = -EINVAL;
  197. int best_diff = -1;
  198. unsigned int i, div;
  199. for (i = 0; i < clk_hw_get_num_parents(hw); i++) {
  200. struct clk_hw *parent;
  201. unsigned long parent_rate;
  202. parent = clk_hw_get_parent_by_index(hw, i);
  203. if (!parent)
  204. continue;
  205. parent_rate = clk_hw_get_rate(parent);
  206. if (!parent_rate)
  207. continue;
  208. for (div = 1; div < ISC_CLK_MAX_DIV + 2; div++) {
  209. unsigned long rate;
  210. int diff;
  211. rate = DIV_ROUND_CLOSEST(parent_rate, div);
  212. diff = abs(req->rate - rate);
  213. if (best_diff < 0 || best_diff > diff) {
  214. best_rate = rate;
  215. best_diff = diff;
  216. req->best_parent_rate = parent_rate;
  217. req->best_parent_hw = parent;
  218. }
  219. if (!best_diff || rate < req->rate)
  220. break;
  221. }
  222. if (!best_diff)
  223. break;
  224. }
  225. dev_dbg(isc_clk->dev,
  226. "ISC CLK: %s, best_rate = %ld, parent clk: %s @ %ld\n",
  227. __func__, best_rate,
  228. __clk_get_name((req->best_parent_hw)->clk),
  229. req->best_parent_rate);
  230. if (best_rate < 0)
  231. return best_rate;
  232. req->rate = best_rate;
  233. return 0;
  234. }
  235. static int isc_clk_set_parent(struct clk_hw *hw, u8 index)
  236. {
  237. struct isc_clk *isc_clk = to_isc_clk(hw);
  238. if (index >= clk_hw_get_num_parents(hw))
  239. return -EINVAL;
  240. isc_clk->parent_id = index;
  241. return 0;
  242. }
  243. static u8 isc_clk_get_parent(struct clk_hw *hw)
  244. {
  245. struct isc_clk *isc_clk = to_isc_clk(hw);
  246. return isc_clk->parent_id;
  247. }
  248. static int isc_clk_set_rate(struct clk_hw *hw,
  249. unsigned long rate,
  250. unsigned long parent_rate)
  251. {
  252. struct isc_clk *isc_clk = to_isc_clk(hw);
  253. u32 div;
  254. if (!rate)
  255. return -EINVAL;
  256. div = DIV_ROUND_CLOSEST(parent_rate, rate);
  257. if (div > (ISC_CLK_MAX_DIV + 1) || !div)
  258. return -EINVAL;
  259. isc_clk->div = div - 1;
  260. return 0;
  261. }
  262. static const struct clk_ops isc_clk_ops = {
  263. .enable = isc_clk_enable,
  264. .disable = isc_clk_disable,
  265. .is_enabled = isc_clk_is_enabled,
  266. .recalc_rate = isc_clk_recalc_rate,
  267. .determine_rate = isc_clk_determine_rate,
  268. .set_parent = isc_clk_set_parent,
  269. .get_parent = isc_clk_get_parent,
  270. .set_rate = isc_clk_set_rate,
  271. };
  272. static int isc_clk_register(struct isc_device *isc, unsigned int id)
  273. {
  274. struct regmap *regmap = isc->regmap;
  275. struct device_node *np = isc->dev->of_node;
  276. struct isc_clk *isc_clk;
  277. struct clk_init_data init;
  278. const char *clk_name = np->name;
  279. const char *parent_names[3];
  280. int num_parents;
  281. num_parents = of_clk_get_parent_count(np);
  282. if (num_parents < 1 || num_parents > 3)
  283. return -EINVAL;
  284. if (num_parents > 2 && id == ISC_ISPCK)
  285. num_parents = 2;
  286. of_clk_parent_fill(np, parent_names, num_parents);
  287. if (id == ISC_MCK)
  288. of_property_read_string(np, "clock-output-names", &clk_name);
  289. else
  290. clk_name = "isc-ispck";
  291. init.parent_names = parent_names;
  292. init.num_parents = num_parents;
  293. init.name = clk_name;
  294. init.ops = &isc_clk_ops;
  295. init.flags = CLK_SET_RATE_GATE | CLK_SET_PARENT_GATE;
  296. isc_clk = &isc->isc_clks[id];
  297. isc_clk->hw.init = &init;
  298. isc_clk->regmap = regmap;
  299. isc_clk->id = id;
  300. isc_clk->dev = isc->dev;
  301. isc_clk->clk = clk_register(isc->dev, &isc_clk->hw);
  302. if (IS_ERR(isc_clk->clk)) {
  303. dev_err(isc->dev, "%s: clock register fail\n", clk_name);
  304. return PTR_ERR(isc_clk->clk);
  305. } else if (id == ISC_MCK)
  306. of_clk_add_provider(np, of_clk_src_simple_get, isc_clk->clk);
  307. return 0;
  308. }
  309. static int isc_clk_init(struct isc_device *isc)
  310. {
  311. unsigned int i;
  312. int ret;
  313. for (i = 0; i < ARRAY_SIZE(isc->isc_clks); i++)
  314. isc->isc_clks[i].clk = ERR_PTR(-EINVAL);
  315. for (i = 0; i < ARRAY_SIZE(isc->isc_clks); i++) {
  316. ret = isc_clk_register(isc, i);
  317. if (ret)
  318. return ret;
  319. }
  320. return 0;
  321. }
  322. static void isc_clk_cleanup(struct isc_device *isc)
  323. {
  324. unsigned int i;
  325. of_clk_del_provider(isc->dev->of_node);
  326. for (i = 0; i < ARRAY_SIZE(isc->isc_clks); i++) {
  327. struct isc_clk *isc_clk = &isc->isc_clks[i];
  328. if (!IS_ERR(isc_clk->clk))
  329. clk_unregister(isc_clk->clk);
  330. }
  331. }
  332. static int isc_queue_setup(struct vb2_queue *vq,
  333. unsigned int *nbuffers, unsigned int *nplanes,
  334. unsigned int sizes[], struct device *alloc_devs[])
  335. {
  336. struct isc_device *isc = vb2_get_drv_priv(vq);
  337. unsigned int size = isc->fmt.fmt.pix.sizeimage;
  338. if (*nplanes)
  339. return sizes[0] < size ? -EINVAL : 0;
  340. *nplanes = 1;
  341. sizes[0] = size;
  342. return 0;
  343. }
  344. static int isc_buffer_prepare(struct vb2_buffer *vb)
  345. {
  346. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  347. struct isc_device *isc = vb2_get_drv_priv(vb->vb2_queue);
  348. unsigned long size = isc->fmt.fmt.pix.sizeimage;
  349. if (vb2_plane_size(vb, 0) < size) {
  350. v4l2_err(&isc->v4l2_dev, "buffer too small (%lu < %lu)\n",
  351. vb2_plane_size(vb, 0), size);
  352. return -EINVAL;
  353. }
  354. vb2_set_plane_payload(vb, 0, size);
  355. vbuf->field = isc->fmt.fmt.pix.field;
  356. return 0;
  357. }
  358. static inline void isc_start_dma(struct regmap *regmap,
  359. struct isc_buffer *frm, u32 dview)
  360. {
  361. dma_addr_t addr;
  362. addr = vb2_dma_contig_plane_dma_addr(&frm->vb.vb2_buf, 0);
  363. regmap_write(regmap, ISC_DCTRL, dview | ISC_DCTRL_IE_IS);
  364. regmap_write(regmap, ISC_DAD0, addr);
  365. regmap_write(regmap, ISC_CTRLEN, ISC_CTRL_CAPTURE);
  366. }
  367. static void isc_set_pipeline(struct isc_device *isc, u32 pipeline)
  368. {
  369. u32 val;
  370. unsigned int i;
  371. for (i = 0; i < ISC_PIPE_LINE_NODE_NUM; i++) {
  372. val = pipeline & BIT(i) ? 1 : 0;
  373. regmap_field_write(isc->pipeline[i], val);
  374. }
  375. }
  376. static int isc_configure(struct isc_device *isc)
  377. {
  378. struct regmap *regmap = isc->regmap;
  379. const struct isc_format *current_fmt = isc->current_fmt;
  380. struct isc_subdev_entity *subdev = isc->current_subdev;
  381. u32 val, mask;
  382. int counter = 10;
  383. val = current_fmt->reg_bps | subdev->pfe_cfg0 |
  384. ISC_PFE_CFG0_MODE_PROGRESSIVE;
  385. mask = ISC_PFE_CFG0_BPS_MASK | ISC_PFE_CFG0_HPOL_LOW |
  386. ISC_PFE_CFG0_VPOL_LOW | ISC_PFE_CFG0_PPOL_LOW |
  387. ISC_PFE_CFG0_MODE_MASK;
  388. regmap_update_bits(regmap, ISC_PFE_CFG0, mask, val);
  389. regmap_update_bits(regmap, ISC_RLP_CFG, ISC_RLP_CFG_MODE_MASK,
  390. current_fmt->reg_rlp_mode);
  391. regmap_update_bits(regmap, ISC_DCFG, ISC_DCFG_IMODE_MASK,
  392. current_fmt->reg_dcfg_imode);
  393. /* Disable the pipeline */
  394. isc_set_pipeline(isc, 0x0);
  395. /* Update profile */
  396. regmap_write(regmap, ISC_CTRLEN, ISC_CTRL_UPPRO);
  397. regmap_read(regmap, ISC_CTRLSR, &val);
  398. while ((val & ISC_CTRL_UPPRO) && counter--) {
  399. usleep_range(1000, 2000);
  400. regmap_read(regmap, ISC_CTRLSR, &val);
  401. }
  402. if (counter < 0)
  403. return -ETIMEDOUT;
  404. return 0;
  405. }
  406. static int isc_start_streaming(struct vb2_queue *vq, unsigned int count)
  407. {
  408. struct isc_device *isc = vb2_get_drv_priv(vq);
  409. struct regmap *regmap = isc->regmap;
  410. struct isc_buffer *buf;
  411. unsigned long flags;
  412. int ret;
  413. u32 val;
  414. /* Enable stream on the sub device */
  415. ret = v4l2_subdev_call(isc->current_subdev->sd, video, s_stream, 1);
  416. if (ret && ret != -ENOIOCTLCMD) {
  417. v4l2_err(&isc->v4l2_dev, "stream on failed in subdev\n");
  418. goto err_start_stream;
  419. }
  420. pm_runtime_get_sync(isc->dev);
  421. /* Disable all the interrupts */
  422. regmap_write(isc->regmap, ISC_INTDIS, (u32)~0UL);
  423. /* Clean the interrupt status register */
  424. regmap_read(regmap, ISC_INTSR, &val);
  425. ret = isc_configure(isc);
  426. if (unlikely(ret))
  427. goto err_configure;
  428. /* Enable DMA interrupt */
  429. regmap_write(regmap, ISC_INTEN, ISC_INT_DDONE);
  430. spin_lock_irqsave(&isc->dma_queue_lock, flags);
  431. isc->sequence = 0;
  432. isc->stop = false;
  433. reinit_completion(&isc->comp);
  434. isc->cur_frm = list_first_entry(&isc->dma_queue,
  435. struct isc_buffer, list);
  436. list_del(&isc->cur_frm->list);
  437. isc_start_dma(regmap, isc->cur_frm, isc->current_fmt->reg_dctrl_dview);
  438. spin_unlock_irqrestore(&isc->dma_queue_lock, flags);
  439. return 0;
  440. err_configure:
  441. pm_runtime_put_sync(isc->dev);
  442. v4l2_subdev_call(isc->current_subdev->sd, video, s_stream, 0);
  443. err_start_stream:
  444. spin_lock_irqsave(&isc->dma_queue_lock, flags);
  445. list_for_each_entry(buf, &isc->dma_queue, list)
  446. vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_QUEUED);
  447. INIT_LIST_HEAD(&isc->dma_queue);
  448. spin_unlock_irqrestore(&isc->dma_queue_lock, flags);
  449. return ret;
  450. }
  451. static void isc_stop_streaming(struct vb2_queue *vq)
  452. {
  453. struct isc_device *isc = vb2_get_drv_priv(vq);
  454. unsigned long flags;
  455. struct isc_buffer *buf;
  456. int ret;
  457. isc->stop = true;
  458. /* Wait until the end of the current frame */
  459. if (isc->cur_frm && !wait_for_completion_timeout(&isc->comp, 5 * HZ))
  460. v4l2_err(&isc->v4l2_dev,
  461. "Timeout waiting for end of the capture\n");
  462. /* Disable DMA interrupt */
  463. regmap_write(isc->regmap, ISC_INTDIS, ISC_INT_DDONE);
  464. pm_runtime_put_sync(isc->dev);
  465. /* Disable stream on the sub device */
  466. ret = v4l2_subdev_call(isc->current_subdev->sd, video, s_stream, 0);
  467. if (ret && ret != -ENOIOCTLCMD)
  468. v4l2_err(&isc->v4l2_dev, "stream off failed in subdev\n");
  469. /* Release all active buffers */
  470. spin_lock_irqsave(&isc->dma_queue_lock, flags);
  471. if (unlikely(isc->cur_frm)) {
  472. vb2_buffer_done(&isc->cur_frm->vb.vb2_buf,
  473. VB2_BUF_STATE_ERROR);
  474. isc->cur_frm = NULL;
  475. }
  476. list_for_each_entry(buf, &isc->dma_queue, list)
  477. vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
  478. INIT_LIST_HEAD(&isc->dma_queue);
  479. spin_unlock_irqrestore(&isc->dma_queue_lock, flags);
  480. }
  481. static void isc_buffer_queue(struct vb2_buffer *vb)
  482. {
  483. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  484. struct isc_buffer *buf = container_of(vbuf, struct isc_buffer, vb);
  485. struct isc_device *isc = vb2_get_drv_priv(vb->vb2_queue);
  486. unsigned long flags;
  487. spin_lock_irqsave(&isc->dma_queue_lock, flags);
  488. list_add_tail(&buf->list, &isc->dma_queue);
  489. spin_unlock_irqrestore(&isc->dma_queue_lock, flags);
  490. }
  491. static struct vb2_ops isc_vb2_ops = {
  492. .queue_setup = isc_queue_setup,
  493. .wait_prepare = vb2_ops_wait_prepare,
  494. .wait_finish = vb2_ops_wait_finish,
  495. .buf_prepare = isc_buffer_prepare,
  496. .start_streaming = isc_start_streaming,
  497. .stop_streaming = isc_stop_streaming,
  498. .buf_queue = isc_buffer_queue,
  499. };
  500. static int isc_querycap(struct file *file, void *priv,
  501. struct v4l2_capability *cap)
  502. {
  503. struct isc_device *isc = video_drvdata(file);
  504. strcpy(cap->driver, ATMEL_ISC_NAME);
  505. strcpy(cap->card, "Atmel Image Sensor Controller");
  506. snprintf(cap->bus_info, sizeof(cap->bus_info),
  507. "platform:%s", isc->v4l2_dev.name);
  508. return 0;
  509. }
  510. static int isc_enum_fmt_vid_cap(struct file *file, void *priv,
  511. struct v4l2_fmtdesc *f)
  512. {
  513. struct isc_device *isc = video_drvdata(file);
  514. u32 index = f->index;
  515. if (index >= isc->num_user_formats)
  516. return -EINVAL;
  517. f->pixelformat = isc->user_formats[index]->fourcc;
  518. return 0;
  519. }
  520. static int isc_g_fmt_vid_cap(struct file *file, void *priv,
  521. struct v4l2_format *fmt)
  522. {
  523. struct isc_device *isc = video_drvdata(file);
  524. *fmt = isc->fmt;
  525. return 0;
  526. }
  527. static struct isc_format *find_format_by_fourcc(struct isc_device *isc,
  528. unsigned int fourcc)
  529. {
  530. unsigned int num_formats = isc->num_user_formats;
  531. struct isc_format *fmt;
  532. unsigned int i;
  533. for (i = 0; i < num_formats; i++) {
  534. fmt = isc->user_formats[i];
  535. if (fmt->fourcc == fourcc)
  536. return fmt;
  537. }
  538. return NULL;
  539. }
  540. static int isc_try_fmt(struct isc_device *isc, struct v4l2_format *f,
  541. struct isc_format **current_fmt)
  542. {
  543. struct isc_format *isc_fmt;
  544. struct v4l2_pix_format *pixfmt = &f->fmt.pix;
  545. struct v4l2_subdev_format format = {
  546. .which = V4L2_SUBDEV_FORMAT_TRY,
  547. };
  548. int ret;
  549. if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  550. return -EINVAL;
  551. isc_fmt = find_format_by_fourcc(isc, pixfmt->pixelformat);
  552. if (!isc_fmt) {
  553. v4l2_warn(&isc->v4l2_dev, "Format 0x%x not found\n",
  554. pixfmt->pixelformat);
  555. isc_fmt = isc->user_formats[isc->num_user_formats - 1];
  556. pixfmt->pixelformat = isc_fmt->fourcc;
  557. }
  558. /* Limit to Atmel ISC hardware capabilities */
  559. if (pixfmt->width > ISC_MAX_SUPPORT_WIDTH)
  560. pixfmt->width = ISC_MAX_SUPPORT_WIDTH;
  561. if (pixfmt->height > ISC_MAX_SUPPORT_HEIGHT)
  562. pixfmt->height = ISC_MAX_SUPPORT_HEIGHT;
  563. v4l2_fill_mbus_format(&format.format, pixfmt, isc_fmt->mbus_code);
  564. ret = v4l2_subdev_call(isc->current_subdev->sd, pad, set_fmt,
  565. isc->current_subdev->config, &format);
  566. if (ret < 0)
  567. return ret;
  568. v4l2_fill_pix_format(pixfmt, &format.format);
  569. pixfmt->field = V4L2_FIELD_NONE;
  570. pixfmt->bytesperline = pixfmt->width * isc_fmt->bpp;
  571. pixfmt->sizeimage = pixfmt->bytesperline * pixfmt->height;
  572. if (current_fmt)
  573. *current_fmt = isc_fmt;
  574. return 0;
  575. }
  576. static int isc_set_fmt(struct isc_device *isc, struct v4l2_format *f)
  577. {
  578. struct v4l2_subdev_format format = {
  579. .which = V4L2_SUBDEV_FORMAT_ACTIVE,
  580. };
  581. struct isc_format *current_fmt;
  582. int ret;
  583. ret = isc_try_fmt(isc, f, &current_fmt);
  584. if (ret)
  585. return ret;
  586. v4l2_fill_mbus_format(&format.format, &f->fmt.pix,
  587. current_fmt->mbus_code);
  588. ret = v4l2_subdev_call(isc->current_subdev->sd, pad,
  589. set_fmt, NULL, &format);
  590. if (ret < 0)
  591. return ret;
  592. isc->fmt = *f;
  593. isc->current_fmt = current_fmt;
  594. return 0;
  595. }
  596. static int isc_s_fmt_vid_cap(struct file *file, void *priv,
  597. struct v4l2_format *f)
  598. {
  599. struct isc_device *isc = video_drvdata(file);
  600. if (vb2_is_streaming(&isc->vb2_vidq))
  601. return -EBUSY;
  602. return isc_set_fmt(isc, f);
  603. }
  604. static int isc_try_fmt_vid_cap(struct file *file, void *priv,
  605. struct v4l2_format *f)
  606. {
  607. struct isc_device *isc = video_drvdata(file);
  608. return isc_try_fmt(isc, f, NULL);
  609. }
  610. static int isc_enum_input(struct file *file, void *priv,
  611. struct v4l2_input *inp)
  612. {
  613. if (inp->index != 0)
  614. return -EINVAL;
  615. inp->type = V4L2_INPUT_TYPE_CAMERA;
  616. inp->std = 0;
  617. strcpy(inp->name, "Camera");
  618. return 0;
  619. }
  620. static int isc_g_input(struct file *file, void *priv, unsigned int *i)
  621. {
  622. *i = 0;
  623. return 0;
  624. }
  625. static int isc_s_input(struct file *file, void *priv, unsigned int i)
  626. {
  627. if (i > 0)
  628. return -EINVAL;
  629. return 0;
  630. }
  631. static int isc_g_parm(struct file *file, void *fh, struct v4l2_streamparm *a)
  632. {
  633. struct isc_device *isc = video_drvdata(file);
  634. if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  635. return -EINVAL;
  636. return v4l2_subdev_call(isc->current_subdev->sd, video, g_parm, a);
  637. }
  638. static int isc_s_parm(struct file *file, void *fh, struct v4l2_streamparm *a)
  639. {
  640. struct isc_device *isc = video_drvdata(file);
  641. if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  642. return -EINVAL;
  643. return v4l2_subdev_call(isc->current_subdev->sd, video, s_parm, a);
  644. }
  645. static int isc_enum_framesizes(struct file *file, void *fh,
  646. struct v4l2_frmsizeenum *fsize)
  647. {
  648. struct isc_device *isc = video_drvdata(file);
  649. const struct isc_format *isc_fmt;
  650. struct v4l2_subdev_frame_size_enum fse = {
  651. .index = fsize->index,
  652. .which = V4L2_SUBDEV_FORMAT_ACTIVE,
  653. };
  654. int ret;
  655. isc_fmt = find_format_by_fourcc(isc, fsize->pixel_format);
  656. if (!isc_fmt)
  657. return -EINVAL;
  658. fse.code = isc_fmt->mbus_code;
  659. ret = v4l2_subdev_call(isc->current_subdev->sd, pad, enum_frame_size,
  660. NULL, &fse);
  661. if (ret)
  662. return ret;
  663. fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
  664. fsize->discrete.width = fse.max_width;
  665. fsize->discrete.height = fse.max_height;
  666. return 0;
  667. }
  668. static int isc_enum_frameintervals(struct file *file, void *fh,
  669. struct v4l2_frmivalenum *fival)
  670. {
  671. struct isc_device *isc = video_drvdata(file);
  672. const struct isc_format *isc_fmt;
  673. struct v4l2_subdev_frame_interval_enum fie = {
  674. .index = fival->index,
  675. .width = fival->width,
  676. .height = fival->height,
  677. .which = V4L2_SUBDEV_FORMAT_ACTIVE,
  678. };
  679. int ret;
  680. isc_fmt = find_format_by_fourcc(isc, fival->pixel_format);
  681. if (!isc_fmt)
  682. return -EINVAL;
  683. fie.code = isc_fmt->mbus_code;
  684. ret = v4l2_subdev_call(isc->current_subdev->sd, pad,
  685. enum_frame_interval, NULL, &fie);
  686. if (ret)
  687. return ret;
  688. fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
  689. fival->discrete = fie.interval;
  690. return 0;
  691. }
  692. static const struct v4l2_ioctl_ops isc_ioctl_ops = {
  693. .vidioc_querycap = isc_querycap,
  694. .vidioc_enum_fmt_vid_cap = isc_enum_fmt_vid_cap,
  695. .vidioc_g_fmt_vid_cap = isc_g_fmt_vid_cap,
  696. .vidioc_s_fmt_vid_cap = isc_s_fmt_vid_cap,
  697. .vidioc_try_fmt_vid_cap = isc_try_fmt_vid_cap,
  698. .vidioc_enum_input = isc_enum_input,
  699. .vidioc_g_input = isc_g_input,
  700. .vidioc_s_input = isc_s_input,
  701. .vidioc_reqbufs = vb2_ioctl_reqbufs,
  702. .vidioc_querybuf = vb2_ioctl_querybuf,
  703. .vidioc_qbuf = vb2_ioctl_qbuf,
  704. .vidioc_expbuf = vb2_ioctl_expbuf,
  705. .vidioc_dqbuf = vb2_ioctl_dqbuf,
  706. .vidioc_create_bufs = vb2_ioctl_create_bufs,
  707. .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
  708. .vidioc_streamon = vb2_ioctl_streamon,
  709. .vidioc_streamoff = vb2_ioctl_streamoff,
  710. .vidioc_g_parm = isc_g_parm,
  711. .vidioc_s_parm = isc_s_parm,
  712. .vidioc_enum_framesizes = isc_enum_framesizes,
  713. .vidioc_enum_frameintervals = isc_enum_frameintervals,
  714. };
  715. static int isc_open(struct file *file)
  716. {
  717. struct isc_device *isc = video_drvdata(file);
  718. struct v4l2_subdev *sd = isc->current_subdev->sd;
  719. int ret;
  720. if (mutex_lock_interruptible(&isc->lock))
  721. return -ERESTARTSYS;
  722. ret = v4l2_fh_open(file);
  723. if (ret < 0)
  724. goto unlock;
  725. if (!v4l2_fh_is_singular_file(file))
  726. goto unlock;
  727. ret = v4l2_subdev_call(sd, core, s_power, 1);
  728. if (ret < 0 && ret != -ENOIOCTLCMD) {
  729. v4l2_fh_release(file);
  730. goto unlock;
  731. }
  732. ret = isc_set_fmt(isc, &isc->fmt);
  733. if (ret) {
  734. v4l2_subdev_call(sd, core, s_power, 0);
  735. v4l2_fh_release(file);
  736. }
  737. unlock:
  738. mutex_unlock(&isc->lock);
  739. return ret;
  740. }
  741. static int isc_release(struct file *file)
  742. {
  743. struct isc_device *isc = video_drvdata(file);
  744. struct v4l2_subdev *sd = isc->current_subdev->sd;
  745. bool fh_singular;
  746. int ret;
  747. mutex_lock(&isc->lock);
  748. fh_singular = v4l2_fh_is_singular_file(file);
  749. ret = _vb2_fop_release(file, NULL);
  750. if (fh_singular)
  751. v4l2_subdev_call(sd, core, s_power, 0);
  752. mutex_unlock(&isc->lock);
  753. return ret;
  754. }
  755. static const struct v4l2_file_operations isc_fops = {
  756. .owner = THIS_MODULE,
  757. .open = isc_open,
  758. .release = isc_release,
  759. .unlocked_ioctl = video_ioctl2,
  760. .read = vb2_fop_read,
  761. .mmap = vb2_fop_mmap,
  762. .poll = vb2_fop_poll,
  763. };
  764. static irqreturn_t isc_interrupt(int irq, void *dev_id)
  765. {
  766. struct isc_device *isc = (struct isc_device *)dev_id;
  767. struct regmap *regmap = isc->regmap;
  768. u32 isc_intsr, isc_intmask, pending;
  769. irqreturn_t ret = IRQ_NONE;
  770. spin_lock(&isc->dma_queue_lock);
  771. regmap_read(regmap, ISC_INTSR, &isc_intsr);
  772. regmap_read(regmap, ISC_INTMASK, &isc_intmask);
  773. pending = isc_intsr & isc_intmask;
  774. if (likely(pending & ISC_INT_DDONE)) {
  775. if (isc->cur_frm) {
  776. struct vb2_v4l2_buffer *vbuf = &isc->cur_frm->vb;
  777. struct vb2_buffer *vb = &vbuf->vb2_buf;
  778. vb->timestamp = ktime_get_ns();
  779. vbuf->sequence = isc->sequence++;
  780. vb2_buffer_done(vb, VB2_BUF_STATE_DONE);
  781. isc->cur_frm = NULL;
  782. }
  783. if (!list_empty(&isc->dma_queue) && !isc->stop) {
  784. isc->cur_frm = list_first_entry(&isc->dma_queue,
  785. struct isc_buffer, list);
  786. list_del(&isc->cur_frm->list);
  787. isc_start_dma(regmap, isc->cur_frm,
  788. isc->current_fmt->reg_dctrl_dview);
  789. }
  790. if (isc->stop)
  791. complete(&isc->comp);
  792. ret = IRQ_HANDLED;
  793. }
  794. spin_unlock(&isc->dma_queue_lock);
  795. return ret;
  796. }
  797. static int isc_async_bound(struct v4l2_async_notifier *notifier,
  798. struct v4l2_subdev *subdev,
  799. struct v4l2_async_subdev *asd)
  800. {
  801. struct isc_device *isc = container_of(notifier->v4l2_dev,
  802. struct isc_device, v4l2_dev);
  803. struct isc_subdev_entity *subdev_entity =
  804. container_of(notifier, struct isc_subdev_entity, notifier);
  805. if (video_is_registered(&isc->video_dev)) {
  806. v4l2_err(&isc->v4l2_dev, "only supports one sub-device.\n");
  807. return -EBUSY;
  808. }
  809. subdev_entity->sd = subdev;
  810. return 0;
  811. }
  812. static void isc_async_unbind(struct v4l2_async_notifier *notifier,
  813. struct v4l2_subdev *subdev,
  814. struct v4l2_async_subdev *asd)
  815. {
  816. struct isc_device *isc = container_of(notifier->v4l2_dev,
  817. struct isc_device, v4l2_dev);
  818. video_unregister_device(&isc->video_dev);
  819. if (isc->current_subdev->config)
  820. v4l2_subdev_free_pad_config(isc->current_subdev->config);
  821. }
  822. static struct isc_format *find_format_by_code(unsigned int code, int *index)
  823. {
  824. struct isc_format *fmt = &isc_formats[0];
  825. unsigned int i;
  826. for (i = 0; i < ARRAY_SIZE(isc_formats); i++) {
  827. if (fmt->mbus_code == code) {
  828. *index = i;
  829. return fmt;
  830. }
  831. fmt++;
  832. }
  833. return NULL;
  834. }
  835. static int isc_formats_init(struct isc_device *isc)
  836. {
  837. struct isc_format *fmt;
  838. struct v4l2_subdev *subdev = isc->current_subdev->sd;
  839. int num_fmts = 0, i, j;
  840. struct v4l2_subdev_mbus_code_enum mbus_code = {
  841. .which = V4L2_SUBDEV_FORMAT_ACTIVE,
  842. };
  843. fmt = &isc_formats[0];
  844. for (i = 0; i < ARRAY_SIZE(isc_formats); i++) {
  845. fmt->support = false;
  846. fmt++;
  847. }
  848. while (!v4l2_subdev_call(subdev, pad, enum_mbus_code,
  849. NULL, &mbus_code)) {
  850. mbus_code.index++;
  851. fmt = find_format_by_code(mbus_code.code, &i);
  852. if (!fmt)
  853. continue;
  854. fmt->support = true;
  855. num_fmts++;
  856. }
  857. if (!num_fmts)
  858. return -ENXIO;
  859. isc->num_user_formats = num_fmts;
  860. isc->user_formats = devm_kcalloc(isc->dev,
  861. num_fmts, sizeof(struct isc_format *),
  862. GFP_KERNEL);
  863. if (!isc->user_formats) {
  864. v4l2_err(&isc->v4l2_dev, "could not allocate memory\n");
  865. return -ENOMEM;
  866. }
  867. fmt = &isc_formats[0];
  868. for (i = 0, j = 0; i < ARRAY_SIZE(isc_formats); i++) {
  869. if (fmt->support)
  870. isc->user_formats[j++] = fmt;
  871. fmt++;
  872. }
  873. return 0;
  874. }
  875. static int isc_set_default_fmt(struct isc_device *isc)
  876. {
  877. struct v4l2_format f = {
  878. .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
  879. .fmt.pix = {
  880. .width = VGA_WIDTH,
  881. .height = VGA_HEIGHT,
  882. .field = V4L2_FIELD_NONE,
  883. .pixelformat = isc->user_formats[0]->fourcc,
  884. },
  885. };
  886. int ret;
  887. ret = isc_try_fmt(isc, &f, NULL);
  888. if (ret)
  889. return ret;
  890. isc->current_fmt = isc->user_formats[0];
  891. isc->fmt = f;
  892. return 0;
  893. }
  894. static int isc_async_complete(struct v4l2_async_notifier *notifier)
  895. {
  896. struct isc_device *isc = container_of(notifier->v4l2_dev,
  897. struct isc_device, v4l2_dev);
  898. struct isc_subdev_entity *sd_entity;
  899. struct video_device *vdev = &isc->video_dev;
  900. struct vb2_queue *q = &isc->vb2_vidq;
  901. int ret;
  902. isc->current_subdev = container_of(notifier,
  903. struct isc_subdev_entity, notifier);
  904. sd_entity = isc->current_subdev;
  905. mutex_init(&isc->lock);
  906. init_completion(&isc->comp);
  907. /* Initialize videobuf2 queue */
  908. q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  909. q->io_modes = VB2_MMAP | VB2_DMABUF | VB2_READ;
  910. q->drv_priv = isc;
  911. q->buf_struct_size = sizeof(struct isc_buffer);
  912. q->ops = &isc_vb2_ops;
  913. q->mem_ops = &vb2_dma_contig_memops;
  914. q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
  915. q->lock = &isc->lock;
  916. q->min_buffers_needed = 1;
  917. q->dev = isc->dev;
  918. ret = vb2_queue_init(q);
  919. if (ret < 0) {
  920. v4l2_err(&isc->v4l2_dev,
  921. "vb2_queue_init() failed: %d\n", ret);
  922. return ret;
  923. }
  924. /* Init video dma queues */
  925. INIT_LIST_HEAD(&isc->dma_queue);
  926. spin_lock_init(&isc->dma_queue_lock);
  927. sd_entity->config = v4l2_subdev_alloc_pad_config(sd_entity->sd);
  928. if (sd_entity->config == NULL)
  929. return -ENOMEM;
  930. ret = isc_formats_init(isc);
  931. if (ret < 0) {
  932. v4l2_err(&isc->v4l2_dev,
  933. "Init format failed: %d\n", ret);
  934. return ret;
  935. }
  936. ret = isc_set_default_fmt(isc);
  937. if (ret) {
  938. v4l2_err(&isc->v4l2_dev, "Could not set default format\n");
  939. return ret;
  940. }
  941. /* Register video device */
  942. strlcpy(vdev->name, ATMEL_ISC_NAME, sizeof(vdev->name));
  943. vdev->release = video_device_release_empty;
  944. vdev->fops = &isc_fops;
  945. vdev->ioctl_ops = &isc_ioctl_ops;
  946. vdev->v4l2_dev = &isc->v4l2_dev;
  947. vdev->vfl_dir = VFL_DIR_RX;
  948. vdev->queue = q;
  949. vdev->lock = &isc->lock;
  950. vdev->ctrl_handler = isc->current_subdev->sd->ctrl_handler;
  951. vdev->device_caps = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_CAPTURE;
  952. video_set_drvdata(vdev, isc);
  953. ret = video_register_device(vdev, VFL_TYPE_GRABBER, -1);
  954. if (ret < 0) {
  955. v4l2_err(&isc->v4l2_dev,
  956. "video_register_device failed: %d\n", ret);
  957. return ret;
  958. }
  959. return 0;
  960. }
  961. static void isc_subdev_cleanup(struct isc_device *isc)
  962. {
  963. struct isc_subdev_entity *subdev_entity;
  964. list_for_each_entry(subdev_entity, &isc->subdev_entities, list)
  965. v4l2_async_notifier_unregister(&subdev_entity->notifier);
  966. INIT_LIST_HEAD(&isc->subdev_entities);
  967. }
  968. static int isc_pipeline_init(struct isc_device *isc)
  969. {
  970. struct device *dev = isc->dev;
  971. struct regmap *regmap = isc->regmap;
  972. struct regmap_field *regs;
  973. unsigned int i;
  974. /* WB-->CFA-->CC-->GAM-->CSC-->CBC-->SUB422-->SUB420 */
  975. const struct reg_field regfields[ISC_PIPE_LINE_NODE_NUM] = {
  976. REG_FIELD(ISC_WB_CTRL, 0, 0),
  977. REG_FIELD(ISC_CFA_CTRL, 0, 0),
  978. REG_FIELD(ISC_CC_CTRL, 0, 0),
  979. REG_FIELD(ISC_GAM_CTRL, 0, 0),
  980. REG_FIELD(ISC_GAM_CTRL, 1, 1),
  981. REG_FIELD(ISC_GAM_CTRL, 2, 2),
  982. REG_FIELD(ISC_GAM_CTRL, 3, 3),
  983. REG_FIELD(ISC_CSC_CTRL, 0, 0),
  984. REG_FIELD(ISC_CBC_CTRL, 0, 0),
  985. REG_FIELD(ISC_SUB422_CTRL, 0, 0),
  986. REG_FIELD(ISC_SUB420_CTRL, 0, 0),
  987. };
  988. for (i = 0; i < ISC_PIPE_LINE_NODE_NUM; i++) {
  989. regs = devm_regmap_field_alloc(dev, regmap, regfields[i]);
  990. if (IS_ERR(regs))
  991. return PTR_ERR(regs);
  992. isc->pipeline[i] = regs;
  993. }
  994. return 0;
  995. }
  996. static int isc_parse_dt(struct device *dev, struct isc_device *isc)
  997. {
  998. struct device_node *np = dev->of_node;
  999. struct device_node *epn = NULL, *rem;
  1000. struct v4l2_of_endpoint v4l2_epn;
  1001. struct isc_subdev_entity *subdev_entity;
  1002. unsigned int flags;
  1003. int ret;
  1004. INIT_LIST_HEAD(&isc->subdev_entities);
  1005. for (; ;) {
  1006. epn = of_graph_get_next_endpoint(np, epn);
  1007. if (!epn)
  1008. break;
  1009. rem = of_graph_get_remote_port_parent(epn);
  1010. if (!rem) {
  1011. dev_notice(dev, "Remote device at %s not found\n",
  1012. of_node_full_name(epn));
  1013. continue;
  1014. }
  1015. ret = v4l2_of_parse_endpoint(epn, &v4l2_epn);
  1016. if (ret) {
  1017. of_node_put(rem);
  1018. ret = -EINVAL;
  1019. dev_err(dev, "Could not parse the endpoint\n");
  1020. break;
  1021. }
  1022. subdev_entity = devm_kzalloc(dev,
  1023. sizeof(*subdev_entity), GFP_KERNEL);
  1024. if (subdev_entity == NULL) {
  1025. of_node_put(rem);
  1026. ret = -ENOMEM;
  1027. break;
  1028. }
  1029. subdev_entity->asd = devm_kzalloc(dev,
  1030. sizeof(*subdev_entity->asd), GFP_KERNEL);
  1031. if (subdev_entity->asd == NULL) {
  1032. of_node_put(rem);
  1033. ret = -ENOMEM;
  1034. break;
  1035. }
  1036. flags = v4l2_epn.bus.parallel.flags;
  1037. if (flags & V4L2_MBUS_HSYNC_ACTIVE_LOW)
  1038. subdev_entity->pfe_cfg0 = ISC_PFE_CFG0_HPOL_LOW;
  1039. if (flags & V4L2_MBUS_VSYNC_ACTIVE_LOW)
  1040. subdev_entity->pfe_cfg0 |= ISC_PFE_CFG0_VPOL_LOW;
  1041. if (flags & V4L2_MBUS_PCLK_SAMPLE_FALLING)
  1042. subdev_entity->pfe_cfg0 |= ISC_PFE_CFG0_PPOL_LOW;
  1043. subdev_entity->asd->match_type = V4L2_ASYNC_MATCH_OF;
  1044. subdev_entity->asd->match.of.node = rem;
  1045. list_add_tail(&subdev_entity->list, &isc->subdev_entities);
  1046. }
  1047. of_node_put(epn);
  1048. return ret;
  1049. }
  1050. /* regmap configuration */
  1051. #define ATMEL_ISC_REG_MAX 0xbfc
  1052. static const struct regmap_config isc_regmap_config = {
  1053. .reg_bits = 32,
  1054. .reg_stride = 4,
  1055. .val_bits = 32,
  1056. .max_register = ATMEL_ISC_REG_MAX,
  1057. };
  1058. static int atmel_isc_probe(struct platform_device *pdev)
  1059. {
  1060. struct device *dev = &pdev->dev;
  1061. struct isc_device *isc;
  1062. struct resource *res;
  1063. void __iomem *io_base;
  1064. struct isc_subdev_entity *subdev_entity;
  1065. int irq;
  1066. int ret;
  1067. isc = devm_kzalloc(dev, sizeof(*isc), GFP_KERNEL);
  1068. if (!isc)
  1069. return -ENOMEM;
  1070. platform_set_drvdata(pdev, isc);
  1071. isc->dev = dev;
  1072. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1073. io_base = devm_ioremap_resource(dev, res);
  1074. if (IS_ERR(io_base))
  1075. return PTR_ERR(io_base);
  1076. isc->regmap = devm_regmap_init_mmio(dev, io_base, &isc_regmap_config);
  1077. if (IS_ERR(isc->regmap)) {
  1078. ret = PTR_ERR(isc->regmap);
  1079. dev_err(dev, "failed to init register map: %d\n", ret);
  1080. return ret;
  1081. }
  1082. irq = platform_get_irq(pdev, 0);
  1083. if (irq < 0) {
  1084. ret = irq;
  1085. dev_err(dev, "failed to get irq: %d\n", ret);
  1086. return ret;
  1087. }
  1088. ret = devm_request_irq(dev, irq, isc_interrupt, 0,
  1089. ATMEL_ISC_NAME, isc);
  1090. if (ret < 0) {
  1091. dev_err(dev, "can't register ISR for IRQ %u (ret=%i)\n",
  1092. irq, ret);
  1093. return ret;
  1094. }
  1095. ret = isc_pipeline_init(isc);
  1096. if (ret)
  1097. return ret;
  1098. isc->hclock = devm_clk_get(dev, "hclock");
  1099. if (IS_ERR(isc->hclock)) {
  1100. ret = PTR_ERR(isc->hclock);
  1101. dev_err(dev, "failed to get hclock: %d\n", ret);
  1102. return ret;
  1103. }
  1104. ret = isc_clk_init(isc);
  1105. if (ret) {
  1106. dev_err(dev, "failed to init isc clock: %d\n", ret);
  1107. goto clean_isc_clk;
  1108. }
  1109. isc->ispck = isc->isc_clks[ISC_ISPCK].clk;
  1110. /* ispck should be greater or equal to hclock */
  1111. ret = clk_set_rate(isc->ispck, clk_get_rate(isc->hclock));
  1112. if (ret) {
  1113. dev_err(dev, "failed to set ispck rate: %d\n", ret);
  1114. goto clean_isc_clk;
  1115. }
  1116. ret = v4l2_device_register(dev, &isc->v4l2_dev);
  1117. if (ret) {
  1118. dev_err(dev, "unable to register v4l2 device.\n");
  1119. goto clean_isc_clk;
  1120. }
  1121. ret = isc_parse_dt(dev, isc);
  1122. if (ret) {
  1123. dev_err(dev, "fail to parse device tree\n");
  1124. goto unregister_v4l2_device;
  1125. }
  1126. if (list_empty(&isc->subdev_entities)) {
  1127. dev_err(dev, "no subdev found\n");
  1128. goto unregister_v4l2_device;
  1129. }
  1130. list_for_each_entry(subdev_entity, &isc->subdev_entities, list) {
  1131. subdev_entity->notifier.subdevs = &subdev_entity->asd;
  1132. subdev_entity->notifier.num_subdevs = 1;
  1133. subdev_entity->notifier.bound = isc_async_bound;
  1134. subdev_entity->notifier.unbind = isc_async_unbind;
  1135. subdev_entity->notifier.complete = isc_async_complete;
  1136. ret = v4l2_async_notifier_register(&isc->v4l2_dev,
  1137. &subdev_entity->notifier);
  1138. if (ret) {
  1139. dev_err(dev, "fail to register async notifier\n");
  1140. goto cleanup_subdev;
  1141. }
  1142. if (video_is_registered(&isc->video_dev))
  1143. break;
  1144. }
  1145. pm_runtime_enable(dev);
  1146. return 0;
  1147. cleanup_subdev:
  1148. isc_subdev_cleanup(isc);
  1149. unregister_v4l2_device:
  1150. v4l2_device_unregister(&isc->v4l2_dev);
  1151. clean_isc_clk:
  1152. isc_clk_cleanup(isc);
  1153. return ret;
  1154. }
  1155. static int atmel_isc_remove(struct platform_device *pdev)
  1156. {
  1157. struct isc_device *isc = platform_get_drvdata(pdev);
  1158. pm_runtime_disable(&pdev->dev);
  1159. isc_subdev_cleanup(isc);
  1160. v4l2_device_unregister(&isc->v4l2_dev);
  1161. isc_clk_cleanup(isc);
  1162. return 0;
  1163. }
  1164. static int __maybe_unused isc_runtime_suspend(struct device *dev)
  1165. {
  1166. struct isc_device *isc = dev_get_drvdata(dev);
  1167. clk_disable_unprepare(isc->ispck);
  1168. clk_disable_unprepare(isc->hclock);
  1169. return 0;
  1170. }
  1171. static int __maybe_unused isc_runtime_resume(struct device *dev)
  1172. {
  1173. struct isc_device *isc = dev_get_drvdata(dev);
  1174. int ret;
  1175. ret = clk_prepare_enable(isc->hclock);
  1176. if (ret)
  1177. return ret;
  1178. return clk_prepare_enable(isc->ispck);
  1179. }
  1180. static const struct dev_pm_ops atmel_isc_dev_pm_ops = {
  1181. SET_RUNTIME_PM_OPS(isc_runtime_suspend, isc_runtime_resume, NULL)
  1182. };
  1183. static const struct of_device_id atmel_isc_of_match[] = {
  1184. { .compatible = "atmel,sama5d2-isc" },
  1185. { }
  1186. };
  1187. MODULE_DEVICE_TABLE(of, atmel_isc_of_match);
  1188. static struct platform_driver atmel_isc_driver = {
  1189. .probe = atmel_isc_probe,
  1190. .remove = atmel_isc_remove,
  1191. .driver = {
  1192. .name = ATMEL_ISC_NAME,
  1193. .pm = &atmel_isc_dev_pm_ops,
  1194. .of_match_table = of_match_ptr(atmel_isc_of_match),
  1195. },
  1196. };
  1197. module_platform_driver(atmel_isc_driver);
  1198. MODULE_AUTHOR("Songjun Wu <songjun.wu@microchip.com>");
  1199. MODULE_DESCRIPTION("The V4L2 driver for Atmel-ISC");
  1200. MODULE_LICENSE("GPL v2");
  1201. MODULE_SUPPORTED_DEVICE("video");