sr030pc30.c 22 KB

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