sr030pc30.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769
  1. /*
  2. * Driver for SiliconFile SR030PC30 VGA (1/10-Inch) Image Sensor with ISP
  3. *
  4. * Copyright (C) 2010 Samsung Electronics Co., Ltd
  5. * Author: Sylwester Nawrocki, s.nawrocki@samsung.com
  6. *
  7. * Based on original driver authored by Dongsoo Nathaniel Kim
  8. * and HeungJun Kim <riverful.kim@samsung.com>.
  9. *
  10. * Based on mt9v011 Micron Digital Image Sensor driver
  11. * Copyright (c) 2009 Mauro Carvalho Chehab (mchehab@redhat.com)
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation; either version 2 of the License, or
  16. * (at your option) any later version.
  17. */
  18. #include <linux/i2c.h>
  19. #include <linux/delay.h>
  20. #include <linux/slab.h>
  21. #include <linux/module.h>
  22. #include <media/v4l2-device.h>
  23. #include <media/v4l2-subdev.h>
  24. #include <media/v4l2-mediabus.h>
  25. #include <media/v4l2-ctrls.h>
  26. #include <media/sr030pc30.h>
  27. static int debug;
  28. module_param(debug, int, 0644);
  29. #define MODULE_NAME "SR030PC30"
  30. /*
  31. * Register offsets within a page
  32. * b15..b8 - page id, b7..b0 - register address
  33. */
  34. #define POWER_CTRL_REG 0x0001
  35. #define PAGEMODE_REG 0x03
  36. #define DEVICE_ID_REG 0x0004
  37. #define NOON010PC30_ID 0x86
  38. #define SR030PC30_ID 0x8C
  39. #define VDO_CTL1_REG 0x0010
  40. #define SUBSAMPL_NONE_VGA 0
  41. #define SUBSAMPL_QVGA 0x10
  42. #define SUBSAMPL_QQVGA 0x20
  43. #define VDO_CTL2_REG 0x0011
  44. #define SYNC_CTL_REG 0x0012
  45. #define WIN_ROWH_REG 0x0020
  46. #define WIN_ROWL_REG 0x0021
  47. #define WIN_COLH_REG 0x0022
  48. #define WIN_COLL_REG 0x0023
  49. #define WIN_HEIGHTH_REG 0x0024
  50. #define WIN_HEIGHTL_REG 0x0025
  51. #define WIN_WIDTHH_REG 0x0026
  52. #define WIN_WIDTHL_REG 0x0027
  53. #define HBLANKH_REG 0x0040
  54. #define HBLANKL_REG 0x0041
  55. #define VSYNCH_REG 0x0042
  56. #define VSYNCL_REG 0x0043
  57. /* page 10 */
  58. #define ISP_CTL_REG(n) (0x1010 + (n))
  59. #define YOFS_REG 0x1040
  60. #define DARK_YOFS_REG 0x1041
  61. #define AG_ABRTH_REG 0x1050
  62. #define SAT_CTL_REG 0x1060
  63. #define BSAT_REG 0x1061
  64. #define RSAT_REG 0x1062
  65. #define AG_SAT_TH_REG 0x1063
  66. /* page 11 */
  67. #define ZLPF_CTRL_REG 0x1110
  68. #define ZLPF_CTRL2_REG 0x1112
  69. #define ZLPF_AGH_THR_REG 0x1121
  70. #define ZLPF_THR_REG 0x1160
  71. #define ZLPF_DYN_THR_REG 0x1160
  72. /* page 12 */
  73. #define YCLPF_CTL1_REG 0x1240
  74. #define YCLPF_CTL2_REG 0x1241
  75. #define YCLPF_THR_REG 0x1250
  76. #define BLPF_CTL_REG 0x1270
  77. #define BLPF_THR1_REG 0x1274
  78. #define BLPF_THR2_REG 0x1275
  79. /* page 14 - Lens Shading Compensation */
  80. #define LENS_CTRL_REG 0x1410
  81. #define LENS_XCEN_REG 0x1420
  82. #define LENS_YCEN_REG 0x1421
  83. #define LENS_R_COMP_REG 0x1422
  84. #define LENS_G_COMP_REG 0x1423
  85. #define LENS_B_COMP_REG 0x1424
  86. /* page 15 - Color correction */
  87. #define CMC_CTL_REG 0x1510
  88. #define CMC_OFSGH_REG 0x1514
  89. #define CMC_OFSGL_REG 0x1516
  90. #define CMC_SIGN_REG 0x1517
  91. /* Color correction coefficients */
  92. #define CMC_COEF_REG(n) (0x1530 + (n))
  93. /* Color correction offset coefficients */
  94. #define CMC_OFS_REG(n) (0x1540 + (n))
  95. /* page 16 - Gamma correction */
  96. #define GMA_CTL_REG 0x1610
  97. /* Gamma correction coefficients 0.14 */
  98. #define GMA_COEF_REG(n) (0x1630 + (n))
  99. /* page 20 - Auto Exposure */
  100. #define AE_CTL1_REG 0x2010
  101. #define AE_CTL2_REG 0x2011
  102. #define AE_FRM_CTL_REG 0x2020
  103. #define AE_FINE_CTL_REG(n) (0x2028 + (n))
  104. #define EXP_TIMEH_REG 0x2083
  105. #define EXP_TIMEM_REG 0x2084
  106. #define EXP_TIMEL_REG 0x2085
  107. #define EXP_MMINH_REG 0x2086
  108. #define EXP_MMINL_REG 0x2087
  109. #define EXP_MMAXH_REG 0x2088
  110. #define EXP_MMAXM_REG 0x2089
  111. #define EXP_MMAXL_REG 0x208A
  112. /* page 22 - Auto White Balance */
  113. #define AWB_CTL1_REG 0x2210
  114. #define AWB_ENABLE 0x80
  115. #define AWB_CTL2_REG 0x2211
  116. #define MWB_ENABLE 0x01
  117. /* RGB gain control (manual WB) when AWB_CTL1[7]=0 */
  118. #define AWB_RGAIN_REG 0x2280
  119. #define AWB_GGAIN_REG 0x2281
  120. #define AWB_BGAIN_REG 0x2282
  121. #define AWB_RMAX_REG 0x2283
  122. #define AWB_RMIN_REG 0x2284
  123. #define AWB_BMAX_REG 0x2285
  124. #define AWB_BMIN_REG 0x2286
  125. /* R, B gain range in bright light conditions */
  126. #define AWB_RMAXB_REG 0x2287
  127. #define AWB_RMINB_REG 0x2288
  128. #define AWB_BMAXB_REG 0x2289
  129. #define AWB_BMINB_REG 0x228A
  130. /* manual white balance, when AWB_CTL2[0]=1 */
  131. #define MWB_RGAIN_REG 0x22B2
  132. #define MWB_BGAIN_REG 0x22B3
  133. /* the token to mark an array end */
  134. #define REG_TERM 0xFFFF
  135. /* Minimum and maximum exposure time in ms */
  136. #define EXPOS_MIN_MS 1
  137. #define EXPOS_MAX_MS 125
  138. struct sr030pc30_info {
  139. struct v4l2_subdev sd;
  140. struct v4l2_ctrl_handler hdl;
  141. const struct sr030pc30_platform_data *pdata;
  142. const struct sr030pc30_format *curr_fmt;
  143. const struct sr030pc30_frmsize *curr_win;
  144. unsigned int hflip:1;
  145. unsigned int vflip:1;
  146. unsigned int sleep:1;
  147. struct {
  148. /* auto whitebalance control cluster */
  149. struct v4l2_ctrl *awb;
  150. struct v4l2_ctrl *red;
  151. struct v4l2_ctrl *blue;
  152. };
  153. struct {
  154. /* auto exposure control cluster */
  155. struct v4l2_ctrl *autoexp;
  156. struct v4l2_ctrl *exp;
  157. };
  158. u8 i2c_reg_page;
  159. };
  160. struct sr030pc30_format {
  161. enum v4l2_mbus_pixelcode code;
  162. enum v4l2_colorspace colorspace;
  163. u16 ispctl1_reg;
  164. };
  165. struct sr030pc30_frmsize {
  166. u16 width;
  167. u16 height;
  168. int vid_ctl1;
  169. };
  170. struct i2c_regval {
  171. u16 addr;
  172. u16 val;
  173. };
  174. /* supported resolutions */
  175. static const struct sr030pc30_frmsize sr030pc30_sizes[] = {
  176. {
  177. .width = 640,
  178. .height = 480,
  179. .vid_ctl1 = SUBSAMPL_NONE_VGA,
  180. }, {
  181. .width = 320,
  182. .height = 240,
  183. .vid_ctl1 = SUBSAMPL_QVGA,
  184. }, {
  185. .width = 160,
  186. .height = 120,
  187. .vid_ctl1 = SUBSAMPL_QQVGA,
  188. },
  189. };
  190. /* supported pixel formats */
  191. static const struct sr030pc30_format sr030pc30_formats[] = {
  192. {
  193. .code = V4L2_MBUS_FMT_YUYV8_2X8,
  194. .colorspace = V4L2_COLORSPACE_JPEG,
  195. .ispctl1_reg = 0x03,
  196. }, {
  197. .code = V4L2_MBUS_FMT_YVYU8_2X8,
  198. .colorspace = V4L2_COLORSPACE_JPEG,
  199. .ispctl1_reg = 0x02,
  200. }, {
  201. .code = V4L2_MBUS_FMT_VYUY8_2X8,
  202. .colorspace = V4L2_COLORSPACE_JPEG,
  203. .ispctl1_reg = 0,
  204. }, {
  205. .code = V4L2_MBUS_FMT_UYVY8_2X8,
  206. .colorspace = V4L2_COLORSPACE_JPEG,
  207. .ispctl1_reg = 0x01,
  208. }, {
  209. .code = V4L2_MBUS_FMT_RGB565_2X8_BE,
  210. .colorspace = V4L2_COLORSPACE_JPEG,
  211. .ispctl1_reg = 0x40,
  212. },
  213. };
  214. static const struct i2c_regval sr030pc30_base_regs[] = {
  215. /* Window size and position within pixel matrix */
  216. { WIN_ROWH_REG, 0x00 }, { WIN_ROWL_REG, 0x06 },
  217. { WIN_COLH_REG, 0x00 }, { WIN_COLL_REG, 0x06 },
  218. { WIN_HEIGHTH_REG, 0x01 }, { WIN_HEIGHTL_REG, 0xE0 },
  219. { WIN_WIDTHH_REG, 0x02 }, { WIN_WIDTHL_REG, 0x80 },
  220. { HBLANKH_REG, 0x01 }, { HBLANKL_REG, 0x50 },
  221. { VSYNCH_REG, 0x00 }, { VSYNCL_REG, 0x14 },
  222. { SYNC_CTL_REG, 0 },
  223. /* Color corection and saturation */
  224. { ISP_CTL_REG(0), 0x30 }, { YOFS_REG, 0x80 },
  225. { DARK_YOFS_REG, 0x04 }, { AG_ABRTH_REG, 0x78 },
  226. { SAT_CTL_REG, 0x1F }, { BSAT_REG, 0x90 },
  227. { AG_SAT_TH_REG, 0xF0 }, { 0x1064, 0x80 },
  228. { CMC_CTL_REG, 0x03 }, { CMC_OFSGH_REG, 0x3C },
  229. { CMC_OFSGL_REG, 0x2C }, { CMC_SIGN_REG, 0x2F },
  230. { CMC_COEF_REG(0), 0xCB }, { CMC_OFS_REG(0), 0x87 },
  231. { CMC_COEF_REG(1), 0x61 }, { CMC_OFS_REG(1), 0x18 },
  232. { CMC_COEF_REG(2), 0x16 }, { CMC_OFS_REG(2), 0x91 },
  233. { CMC_COEF_REG(3), 0x23 }, { CMC_OFS_REG(3), 0x94 },
  234. { CMC_COEF_REG(4), 0xCE }, { CMC_OFS_REG(4), 0x9f },
  235. { CMC_COEF_REG(5), 0x2B }, { CMC_OFS_REG(5), 0x33 },
  236. { CMC_COEF_REG(6), 0x01 }, { CMC_OFS_REG(6), 0x00 },
  237. { CMC_COEF_REG(7), 0x34 }, { CMC_OFS_REG(7), 0x94 },
  238. { CMC_COEF_REG(8), 0x75 }, { CMC_OFS_REG(8), 0x14 },
  239. /* Color corection coefficients */
  240. { GMA_CTL_REG, 0x03 }, { GMA_COEF_REG(0), 0x00 },
  241. { GMA_COEF_REG(1), 0x19 }, { GMA_COEF_REG(2), 0x26 },
  242. { GMA_COEF_REG(3), 0x3B }, { GMA_COEF_REG(4), 0x5D },
  243. { GMA_COEF_REG(5), 0x79 }, { GMA_COEF_REG(6), 0x8E },
  244. { GMA_COEF_REG(7), 0x9F }, { GMA_COEF_REG(8), 0xAF },
  245. { GMA_COEF_REG(9), 0xBD }, { GMA_COEF_REG(10), 0xCA },
  246. { GMA_COEF_REG(11), 0xDD }, { GMA_COEF_REG(12), 0xEC },
  247. { GMA_COEF_REG(13), 0xF7 }, { GMA_COEF_REG(14), 0xFF },
  248. /* Noise reduction, Z-LPF, YC-LPF and BLPF filters setup */
  249. { ZLPF_CTRL_REG, 0x99 }, { ZLPF_CTRL2_REG, 0x0E },
  250. { ZLPF_AGH_THR_REG, 0x29 }, { ZLPF_THR_REG, 0x0F },
  251. { ZLPF_DYN_THR_REG, 0x63 }, { YCLPF_CTL1_REG, 0x23 },
  252. { YCLPF_CTL2_REG, 0x3B }, { YCLPF_THR_REG, 0x05 },
  253. { BLPF_CTL_REG, 0x1D }, { BLPF_THR1_REG, 0x05 },
  254. { BLPF_THR2_REG, 0x04 },
  255. /* Automatic white balance */
  256. { AWB_CTL1_REG, 0xFB }, { AWB_CTL2_REG, 0x26 },
  257. { AWB_RMAX_REG, 0x54 }, { AWB_RMIN_REG, 0x2B },
  258. { AWB_BMAX_REG, 0x57 }, { AWB_BMIN_REG, 0x29 },
  259. { AWB_RMAXB_REG, 0x50 }, { AWB_RMINB_REG, 0x43 },
  260. { AWB_BMAXB_REG, 0x30 }, { AWB_BMINB_REG, 0x22 },
  261. /* Auto exposure */
  262. { AE_CTL1_REG, 0x8C }, { AE_CTL2_REG, 0x04 },
  263. { AE_FRM_CTL_REG, 0x01 }, { AE_FINE_CTL_REG(0), 0x3F },
  264. { AE_FINE_CTL_REG(1), 0xA3 }, { AE_FINE_CTL_REG(3), 0x34 },
  265. /* Lens shading compensation */
  266. { LENS_CTRL_REG, 0x01 }, { LENS_XCEN_REG, 0x80 },
  267. { LENS_YCEN_REG, 0x70 }, { LENS_R_COMP_REG, 0x53 },
  268. { LENS_G_COMP_REG, 0x40 }, { LENS_B_COMP_REG, 0x3e },
  269. { REG_TERM, 0 },
  270. };
  271. static inline struct sr030pc30_info *to_sr030pc30(struct v4l2_subdev *sd)
  272. {
  273. return container_of(sd, struct sr030pc30_info, sd);
  274. }
  275. static inline int set_i2c_page(struct sr030pc30_info *info,
  276. struct i2c_client *client, unsigned int reg)
  277. {
  278. int ret = 0;
  279. u32 page = reg >> 8 & 0xFF;
  280. if (info->i2c_reg_page != page && (reg & 0xFF) != 0x03) {
  281. ret = i2c_smbus_write_byte_data(client, PAGEMODE_REG, page);
  282. if (!ret)
  283. info->i2c_reg_page = page;
  284. }
  285. return ret;
  286. }
  287. static int cam_i2c_read(struct v4l2_subdev *sd, u32 reg_addr)
  288. {
  289. struct i2c_client *client = v4l2_get_subdevdata(sd);
  290. struct sr030pc30_info *info = to_sr030pc30(sd);
  291. int ret = set_i2c_page(info, client, reg_addr);
  292. if (!ret)
  293. ret = i2c_smbus_read_byte_data(client, reg_addr & 0xFF);
  294. return ret;
  295. }
  296. static int cam_i2c_write(struct v4l2_subdev *sd, u32 reg_addr, u32 val)
  297. {
  298. struct i2c_client *client = v4l2_get_subdevdata(sd);
  299. struct sr030pc30_info *info = to_sr030pc30(sd);
  300. int ret = set_i2c_page(info, client, reg_addr);
  301. if (!ret)
  302. ret = i2c_smbus_write_byte_data(
  303. client, reg_addr & 0xFF, val);
  304. return ret;
  305. }
  306. static inline int sr030pc30_bulk_write_reg(struct v4l2_subdev *sd,
  307. const struct i2c_regval *msg)
  308. {
  309. while (msg->addr != REG_TERM) {
  310. int ret = cam_i2c_write(sd, msg->addr, msg->val);
  311. if (ret)
  312. return ret;
  313. msg++;
  314. }
  315. return 0;
  316. }
  317. /* Device reset and sleep mode control */
  318. static int sr030pc30_pwr_ctrl(struct v4l2_subdev *sd,
  319. bool reset, bool sleep)
  320. {
  321. struct sr030pc30_info *info = to_sr030pc30(sd);
  322. u8 reg = sleep ? 0xF1 : 0xF0;
  323. int ret = 0;
  324. if (reset)
  325. ret = cam_i2c_write(sd, POWER_CTRL_REG, reg | 0x02);
  326. if (!ret) {
  327. ret = cam_i2c_write(sd, POWER_CTRL_REG, reg);
  328. if (!ret) {
  329. info->sleep = sleep;
  330. if (reset)
  331. info->i2c_reg_page = -1;
  332. }
  333. }
  334. return ret;
  335. }
  336. static int sr030pc30_set_flip(struct v4l2_subdev *sd)
  337. {
  338. struct sr030pc30_info *info = to_sr030pc30(sd);
  339. s32 reg = cam_i2c_read(sd, VDO_CTL2_REG);
  340. if (reg < 0)
  341. return reg;
  342. reg &= 0x7C;
  343. if (info->hflip)
  344. reg |= 0x01;
  345. if (info->vflip)
  346. reg |= 0x02;
  347. return cam_i2c_write(sd, VDO_CTL2_REG, reg | 0x80);
  348. }
  349. /* Configure resolution, color format and image flip */
  350. static int sr030pc30_set_params(struct v4l2_subdev *sd)
  351. {
  352. struct sr030pc30_info *info = to_sr030pc30(sd);
  353. int ret;
  354. if (!info->curr_win)
  355. return -EINVAL;
  356. /* Configure the resolution through subsampling */
  357. ret = cam_i2c_write(sd, VDO_CTL1_REG,
  358. info->curr_win->vid_ctl1);
  359. if (!ret && info->curr_fmt)
  360. ret = cam_i2c_write(sd, ISP_CTL_REG(0),
  361. info->curr_fmt->ispctl1_reg);
  362. if (!ret)
  363. ret = sr030pc30_set_flip(sd);
  364. return ret;
  365. }
  366. /* Find nearest matching image pixel size. */
  367. static int sr030pc30_try_frame_size(struct v4l2_mbus_framefmt *mf)
  368. {
  369. unsigned int min_err = ~0;
  370. int i = ARRAY_SIZE(sr030pc30_sizes);
  371. const struct sr030pc30_frmsize *fsize = &sr030pc30_sizes[0],
  372. *match = NULL;
  373. while (i--) {
  374. int err = abs(fsize->width - mf->width)
  375. + abs(fsize->height - mf->height);
  376. if (err < min_err) {
  377. min_err = err;
  378. match = fsize;
  379. }
  380. fsize++;
  381. }
  382. if (match) {
  383. mf->width = match->width;
  384. mf->height = match->height;
  385. return 0;
  386. }
  387. return -EINVAL;
  388. }
  389. static int sr030pc30_s_ctrl(struct v4l2_ctrl *ctrl)
  390. {
  391. struct sr030pc30_info *info =
  392. container_of(ctrl->handler, struct sr030pc30_info, hdl);
  393. struct v4l2_subdev *sd = &info->sd;
  394. int ret = 0;
  395. v4l2_dbg(1, debug, sd, "%s: ctrl_id: %d, value: %d\n",
  396. __func__, ctrl->id, ctrl->val);
  397. switch (ctrl->id) {
  398. case V4L2_CID_AUTO_WHITE_BALANCE:
  399. if (ctrl->is_new) {
  400. ret = cam_i2c_write(sd, AWB_CTL2_REG,
  401. ctrl->val ? 0x2E : 0x2F);
  402. if (!ret)
  403. ret = cam_i2c_write(sd, AWB_CTL1_REG,
  404. ctrl->val ? 0xFB : 0x7B);
  405. }
  406. if (!ret && info->blue->is_new)
  407. ret = cam_i2c_write(sd, MWB_BGAIN_REG, info->blue->val);
  408. if (!ret && info->red->is_new)
  409. ret = cam_i2c_write(sd, MWB_RGAIN_REG, info->red->val);
  410. return ret;
  411. case V4L2_CID_EXPOSURE_AUTO:
  412. /* auto anti-flicker is also enabled here */
  413. if (ctrl->is_new)
  414. ret = cam_i2c_write(sd, AE_CTL1_REG,
  415. ctrl->val == V4L2_EXPOSURE_AUTO ? 0xDC : 0x0C);
  416. if (info->exp->is_new) {
  417. unsigned long expos = info->exp->val;
  418. expos = expos * info->pdata->clk_rate / (8 * 1000);
  419. if (!ret)
  420. ret = cam_i2c_write(sd, EXP_TIMEH_REG,
  421. expos >> 16 & 0xFF);
  422. if (!ret)
  423. ret = cam_i2c_write(sd, EXP_TIMEM_REG,
  424. expos >> 8 & 0xFF);
  425. if (!ret)
  426. ret = cam_i2c_write(sd, EXP_TIMEL_REG,
  427. expos & 0xFF);
  428. }
  429. return ret;
  430. default:
  431. return -EINVAL;
  432. }
  433. return 0;
  434. }
  435. static int sr030pc30_enum_fmt(struct v4l2_subdev *sd, unsigned int index,
  436. enum v4l2_mbus_pixelcode *code)
  437. {
  438. if (!code || index >= ARRAY_SIZE(sr030pc30_formats))
  439. return -EINVAL;
  440. *code = sr030pc30_formats[index].code;
  441. return 0;
  442. }
  443. static int sr030pc30_g_fmt(struct v4l2_subdev *sd,
  444. struct v4l2_mbus_framefmt *mf)
  445. {
  446. struct sr030pc30_info *info = to_sr030pc30(sd);
  447. int ret;
  448. if (!mf)
  449. return -EINVAL;
  450. if (!info->curr_win || !info->curr_fmt) {
  451. ret = sr030pc30_set_params(sd);
  452. if (ret)
  453. return ret;
  454. }
  455. mf->width = info->curr_win->width;
  456. mf->height = info->curr_win->height;
  457. mf->code = info->curr_fmt->code;
  458. mf->colorspace = info->curr_fmt->colorspace;
  459. mf->field = V4L2_FIELD_NONE;
  460. return 0;
  461. }
  462. /* Return nearest media bus frame format. */
  463. static const struct sr030pc30_format *try_fmt(struct v4l2_subdev *sd,
  464. struct v4l2_mbus_framefmt *mf)
  465. {
  466. int i = ARRAY_SIZE(sr030pc30_formats);
  467. sr030pc30_try_frame_size(mf);
  468. while (i--)
  469. if (mf->code == sr030pc30_formats[i].code)
  470. break;
  471. mf->code = sr030pc30_formats[i].code;
  472. return &sr030pc30_formats[i];
  473. }
  474. /* Return nearest media bus frame format. */
  475. static int sr030pc30_try_fmt(struct v4l2_subdev *sd,
  476. struct v4l2_mbus_framefmt *mf)
  477. {
  478. if (!sd || !mf)
  479. return -EINVAL;
  480. try_fmt(sd, mf);
  481. return 0;
  482. }
  483. static int sr030pc30_s_fmt(struct v4l2_subdev *sd,
  484. struct v4l2_mbus_framefmt *mf)
  485. {
  486. struct sr030pc30_info *info = to_sr030pc30(sd);
  487. if (!sd || !mf)
  488. return -EINVAL;
  489. info->curr_fmt = try_fmt(sd, mf);
  490. return sr030pc30_set_params(sd);
  491. }
  492. static int sr030pc30_base_config(struct v4l2_subdev *sd)
  493. {
  494. struct sr030pc30_info *info = to_sr030pc30(sd);
  495. int ret;
  496. unsigned long expmin, expmax;
  497. ret = sr030pc30_bulk_write_reg(sd, sr030pc30_base_regs);
  498. if (!ret) {
  499. info->curr_fmt = &sr030pc30_formats[0];
  500. info->curr_win = &sr030pc30_sizes[0];
  501. ret = sr030pc30_set_params(sd);
  502. }
  503. if (!ret)
  504. ret = sr030pc30_pwr_ctrl(sd, false, false);
  505. if (!ret && !info->pdata)
  506. return ret;
  507. expmin = EXPOS_MIN_MS * info->pdata->clk_rate / (8 * 1000);
  508. expmax = EXPOS_MAX_MS * info->pdata->clk_rate / (8 * 1000);
  509. v4l2_dbg(1, debug, sd, "%s: expmin= %lx, expmax= %lx", __func__,
  510. expmin, expmax);
  511. /* Setting up manual exposure time range */
  512. ret = cam_i2c_write(sd, EXP_MMINH_REG, expmin >> 8 & 0xFF);
  513. if (!ret)
  514. ret = cam_i2c_write(sd, EXP_MMINL_REG, expmin & 0xFF);
  515. if (!ret)
  516. ret = cam_i2c_write(sd, EXP_MMAXH_REG, expmax >> 16 & 0xFF);
  517. if (!ret)
  518. ret = cam_i2c_write(sd, EXP_MMAXM_REG, expmax >> 8 & 0xFF);
  519. if (!ret)
  520. ret = cam_i2c_write(sd, EXP_MMAXL_REG, expmax & 0xFF);
  521. return ret;
  522. }
  523. static int sr030pc30_s_power(struct v4l2_subdev *sd, int on)
  524. {
  525. struct i2c_client *client = v4l2_get_subdevdata(sd);
  526. struct sr030pc30_info *info = to_sr030pc30(sd);
  527. const struct sr030pc30_platform_data *pdata = info->pdata;
  528. int ret;
  529. if (pdata == NULL) {
  530. WARN(1, "No platform data!\n");
  531. return -EINVAL;
  532. }
  533. /*
  534. * Put sensor into power sleep mode before switching off
  535. * power and disabling MCLK.
  536. */
  537. if (!on)
  538. sr030pc30_pwr_ctrl(sd, false, true);
  539. /* set_power controls sensor's power and clock */
  540. if (pdata->set_power) {
  541. ret = pdata->set_power(&client->dev, on);
  542. if (ret)
  543. return ret;
  544. }
  545. if (on) {
  546. ret = sr030pc30_base_config(sd);
  547. } else {
  548. ret = 0;
  549. info->curr_win = NULL;
  550. info->curr_fmt = NULL;
  551. }
  552. return ret;
  553. }
  554. static const struct v4l2_ctrl_ops sr030pc30_ctrl_ops = {
  555. .s_ctrl = sr030pc30_s_ctrl,
  556. };
  557. static const struct v4l2_subdev_core_ops sr030pc30_core_ops = {
  558. .s_power = sr030pc30_s_power,
  559. .g_ext_ctrls = v4l2_subdev_g_ext_ctrls,
  560. .try_ext_ctrls = v4l2_subdev_try_ext_ctrls,
  561. .s_ext_ctrls = v4l2_subdev_s_ext_ctrls,
  562. .g_ctrl = v4l2_subdev_g_ctrl,
  563. .s_ctrl = v4l2_subdev_s_ctrl,
  564. .queryctrl = v4l2_subdev_queryctrl,
  565. .querymenu = v4l2_subdev_querymenu,
  566. };
  567. static const struct v4l2_subdev_video_ops sr030pc30_video_ops = {
  568. .g_mbus_fmt = sr030pc30_g_fmt,
  569. .s_mbus_fmt = sr030pc30_s_fmt,
  570. .try_mbus_fmt = sr030pc30_try_fmt,
  571. .enum_mbus_fmt = sr030pc30_enum_fmt,
  572. };
  573. static const struct v4l2_subdev_ops sr030pc30_ops = {
  574. .core = &sr030pc30_core_ops,
  575. .video = &sr030pc30_video_ops,
  576. };
  577. /*
  578. * Detect sensor type. Return 0 if SR030PC30 was detected
  579. * or -ENODEV otherwise.
  580. */
  581. static int sr030pc30_detect(struct i2c_client *client)
  582. {
  583. const struct sr030pc30_platform_data *pdata
  584. = client->dev.platform_data;
  585. int ret;
  586. /* Enable sensor's power and clock */
  587. if (pdata->set_power) {
  588. ret = pdata->set_power(&client->dev, 1);
  589. if (ret)
  590. return ret;
  591. }
  592. ret = i2c_smbus_read_byte_data(client, DEVICE_ID_REG);
  593. if (pdata->set_power)
  594. pdata->set_power(&client->dev, 0);
  595. if (ret < 0) {
  596. dev_err(&client->dev, "%s: I2C read failed\n", __func__);
  597. return ret;
  598. }
  599. return ret == SR030PC30_ID ? 0 : -ENODEV;
  600. }
  601. static int sr030pc30_probe(struct i2c_client *client,
  602. const struct i2c_device_id *id)
  603. {
  604. struct sr030pc30_info *info;
  605. struct v4l2_subdev *sd;
  606. struct v4l2_ctrl_handler *hdl;
  607. const struct sr030pc30_platform_data *pdata
  608. = client->dev.platform_data;
  609. int ret;
  610. if (!pdata) {
  611. dev_err(&client->dev, "No platform data!");
  612. return -EIO;
  613. }
  614. ret = sr030pc30_detect(client);
  615. if (ret)
  616. return ret;
  617. info = devm_kzalloc(&client->dev, sizeof(*info), GFP_KERNEL);
  618. if (!info)
  619. return -ENOMEM;
  620. sd = &info->sd;
  621. strcpy(sd->name, MODULE_NAME);
  622. info->pdata = client->dev.platform_data;
  623. v4l2_i2c_subdev_init(sd, client, &sr030pc30_ops);
  624. hdl = &info->hdl;
  625. v4l2_ctrl_handler_init(hdl, 6);
  626. info->awb = v4l2_ctrl_new_std(hdl, &sr030pc30_ctrl_ops,
  627. V4L2_CID_AUTO_WHITE_BALANCE, 0, 1, 1, 1);
  628. info->red = v4l2_ctrl_new_std(hdl, &sr030pc30_ctrl_ops,
  629. V4L2_CID_RED_BALANCE, 0, 127, 1, 64);
  630. info->blue = v4l2_ctrl_new_std(hdl, &sr030pc30_ctrl_ops,
  631. V4L2_CID_BLUE_BALANCE, 0, 127, 1, 64);
  632. info->autoexp = v4l2_ctrl_new_std(hdl, &sr030pc30_ctrl_ops,
  633. V4L2_CID_EXPOSURE_AUTO, 0, 1, 1, 1);
  634. info->exp = v4l2_ctrl_new_std(hdl, &sr030pc30_ctrl_ops,
  635. V4L2_CID_EXPOSURE, EXPOS_MIN_MS, EXPOS_MAX_MS, 1, 30);
  636. sd->ctrl_handler = hdl;
  637. if (hdl->error) {
  638. int err = hdl->error;
  639. v4l2_ctrl_handler_free(hdl);
  640. return err;
  641. }
  642. v4l2_ctrl_auto_cluster(3, &info->awb, 0, false);
  643. v4l2_ctrl_auto_cluster(2, &info->autoexp, V4L2_EXPOSURE_MANUAL, false);
  644. v4l2_ctrl_handler_setup(hdl);
  645. info->i2c_reg_page = -1;
  646. info->hflip = 1;
  647. return 0;
  648. }
  649. static int sr030pc30_remove(struct i2c_client *client)
  650. {
  651. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  652. v4l2_device_unregister_subdev(sd);
  653. v4l2_ctrl_handler_free(sd->ctrl_handler);
  654. return 0;
  655. }
  656. static const struct i2c_device_id sr030pc30_id[] = {
  657. { MODULE_NAME, 0 },
  658. { },
  659. };
  660. MODULE_DEVICE_TABLE(i2c, sr030pc30_id);
  661. static struct i2c_driver sr030pc30_i2c_driver = {
  662. .driver = {
  663. .name = MODULE_NAME
  664. },
  665. .probe = sr030pc30_probe,
  666. .remove = sr030pc30_remove,
  667. .id_table = sr030pc30_id,
  668. };
  669. module_i2c_driver(sr030pc30_i2c_driver);
  670. MODULE_DESCRIPTION("Siliconfile SR030PC30 camera driver");
  671. MODULE_AUTHOR("Sylwester Nawrocki <s.nawrocki@samsung.com>");
  672. MODULE_LICENSE("GPL");