sr030pc30.c 22 KB

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